| 1 | |
| 2 | * Copyright (c) 2016 Intel Corporation
|
| 3 | *
|
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its
|
| 5 | * documentation for any purpose is hereby granted without fee, provided that
|
| 6 | * the above copyright notice appear in all copies and that both that copyright
|
| 7 | * notice and this permission notice appear in supporting documentation, and
|
| 8 | * that the name of the copyright holders not be used in advertising or
|
| 9 | * publicity pertaining to distribution of the software without specific,
|
| 10 | * written prior permission. The copyright holders make no representations
|
| 11 | * about the suitability of this software for any purpose. It is provided "as
|
| 12 | * is" without express or implied warranty.
|
| 13 | *
|
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
| 16 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
| 20 | * OF THIS SOFTWARE.
|
| 21 |
|
| 22 |
|
| 23 | #include <linux/export.h>
|
| 24 | #include <linux/uaccess.h>
|
| 25 |
|
| 26 | #include <drm/drm_atomic.h>
|
| 27 | #include <drm/drm_drv.h>
|
| 28 | #include <drm/drm_encoder.h>
|
| 29 | #include <drm/drm_file.h>
|
| 30 | #include <drm/drm_framebuffer.h>
|
| 31 | #include <drm/drm_managed.h>
|
| 32 | #include <drm/drm_mode_config.h>
|
| 33 | #include <drm/drm_print.h>
|
| 34 | #include <drm/drm_colorop.h>
|
| 35 | #include <linux/dma-resv.h>
|
| 36 |
|
| 37 | #include "drm_crtc_internal.h"
|
| 38 | #include "drm_internal.h"
|
| 39 |
|
| 40 | int drm_modeset_register_all(struct drm_device *dev)
|
| 41 | {
|
| 42 | int ret;
|
| 43 |
|
| 44 | ret = drm_plane_register_all(dev);
|
| 45 | if (ret)
|
| 46 | goto err_plane;
|
| 47 |
|
| 48 | ret = drm_crtc_register_all(dev);
|
| 49 | if (ret)
|
| 50 | goto err_crtc;
|
| 51 |
|
| 52 | ret = drm_encoder_register_all(dev);
|
| 53 | if (ret)
|
| 54 | goto err_encoder;
|
| 55 |
|
| 56 | ret = drm_connector_register_all(dev);
|
| 57 | if (ret)
|
| 58 | goto err_connector;
|
| 59 |
|
| 60 | return 0;
|
| 61 |
|
| 62 | err_connector:
|
| 63 | drm_encoder_unregister_all(dev);
|
| 64 | err_encoder:
|
| 65 | drm_crtc_unregister_all(dev);
|
| 66 | err_crtc:
|
| 67 | drm_plane_unregister_all(dev);
|
| 68 | err_plane:
|
| 69 | return ret;
|
| 70 | }
|
| 71 |
|
| 72 | void drm_modeset_unregister_all(struct drm_device *dev)
|
| 73 | {
|
| 74 | drm_connector_unregister_all(dev);
|
| 75 | drm_encoder_unregister_all(dev);
|
| 76 | drm_crtc_unregister_all(dev);
|
| 77 | drm_plane_unregister_all(dev);
|
| 78 | }
|
| 79 |
|
| 80 | |
| 81 | * drm_mode_getresources - get graphics configuration
|
| 82 | * @dev: drm device for the ioctl
|
| 83 | * @data: data pointer for the ioctl
|
| 84 | * @file_priv: drm file for the ioctl call
|
| 85 | *
|
| 86 | * Construct a set of configuration description structures and return
|
| 87 | * them to the user, including CRTC, connector and framebuffer configuration.
|
| 88 | *
|
| 89 | * Called by the user via ioctl.
|
| 90 | *
|
| 91 | * Returns:
|
| 92 | * Zero on success, negative errno on failure.
|
| 93 |
|
| 94 | int drm_mode_getresources(struct drm_device *dev, void *data,
|
| 95 | struct drm_file *file_priv)
|
| 96 | {
|
| 97 | struct drm_mode_card_res *card_res = data;
|
| 98 | struct drm_framebuffer *fb;
|
| 99 | struct drm_connector *connector;
|
| 100 | struct drm_crtc *crtc;
|
| 101 | struct drm_encoder *encoder;
|
| 102 | int count, ret = 0;
|
| 103 | uint32_t __user *fb_id;
|
| 104 | uint32_t __user *crtc_id;
|
| 105 | uint32_t __user *connector_id;
|
| 106 | uint32_t __user *encoder_id;
|
| 107 | struct drm_connector_list_iter conn_iter;
|
| 108 |
|
| 109 | if (!drm_core_check_feature(dev, DRIVER_MODESET))
|
| 110 | return -EOPNOTSUPP;
|
| 111 |
|
| 112 | mutex_lock(&file_priv->fbs_lock);
|
| 113 | count = 0;
|
| 114 | fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
|
| 115 | list_for_each_entry(fb, &file_priv->fbs, filp_head) {
|
| 116 | if (count < card_res->count_fbs &&
|
| 117 | put_user(fb->base.id, fb_id + count)) {
|
| 118 | mutex_unlock(&file_priv->fbs_lock);
|
| 119 | return -EFAULT;
|
| 120 | }
|
| 121 | count++;
|
| 122 | }
|
| 123 | card_res->count_fbs = count;
|
| 124 | mutex_unlock(&file_priv->fbs_lock);
|
| 125 |
|
| 126 | card_res->max_height = dev->mode_config.max_height;
|
| 127 | card_res->min_height = dev->mode_config.min_height;
|
| 128 | card_res->max_width = dev->mode_config.max_width;
|
| 129 | card_res->min_width = dev->mode_config.min_width;
|
| 130 |
|
| 131 | count = 0;
|
| 132 | crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
|
| 133 | drm_for_each_crtc(crtc, dev) {
|
| 134 | if (drm_lease_held(file_priv, crtc->base.id)) {
|
| 135 | if (count < card_res->count_crtcs &&
|
| 136 | put_user(crtc->base.id, crtc_id + count))
|
| 137 | return -EFAULT;
|
| 138 | count++;
|
| 139 | }
|
| 140 | }
|
| 141 | card_res->count_crtcs = count;
|
| 142 |
|
| 143 | count = 0;
|
| 144 | encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
|
| 145 | drm_for_each_encoder(encoder, dev) {
|
| 146 | if (count < card_res->count_encoders &&
|
| 147 | put_user(encoder->base.id, encoder_id + count))
|
| 148 | return -EFAULT;
|
| 149 | count++;
|
| 150 | }
|
| 151 | card_res->count_encoders = count;
|
| 152 |
|
| 153 | drm_connector_list_iter_begin(dev, &conn_iter);
|
| 154 | count = 0;
|
| 155 | connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
|
| 156 | |
| 157 | * FIXME: the connectors on the list may not be fully initialized yet,
|
| 158 | * if the ioctl is called before the connectors are registered. (See
|
| 159 | * drm_dev_register()->drm_modeset_register_all() for static and
|
| 160 | * drm_connector_dynamic_register() for dynamic connectors.)
|
| 161 | * The driver should only get registered after static connectors are
|
| 162 | * fully initialized and dynamic connectors should be added to the
|
| 163 | * connector list only after fully initializing them.
|
| 164 |
|
| 165 | drm_for_each_connector_iter(connector, &conn_iter) {
|
| 166 |
|
| 167 | if (!file_priv->writeback_connectors &&
|
| 168 | (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK))
|
| 169 | continue;
|
| 170 |
|
| 171 | if (drm_lease_held(file_priv, connector->base.id)) {
|
| 172 | if (count < card_res->count_connectors &&
|
| 173 | put_user(connector->base.id, connector_id + count)) {
|
| 174 | drm_connector_list_iter_end(&conn_iter);
|
| 175 | return -EFAULT;
|
| 176 | }
|
| 177 | count++;
|
| 178 | }
|
| 179 | }
|
| 180 | card_res->count_connectors = count;
|
| 181 | drm_connector_list_iter_end(&conn_iter);
|
| 182 |
|
| 183 | return ret;
|
| 184 | }
|
| 185 |
|
| 186 | static int drm_mode_config_plane_create_state(struct drm_plane *plane)
|
| 187 | {
|
| 188 | struct drm_plane_state *plane_state;
|
| 189 |
|
| 190 | if (!plane->funcs->atomic_create_state)
|
| 191 | return 0;
|
| 192 |
|
| 193 | plane_state = plane->funcs->atomic_create_state(plane);
|
| 194 | if (IS_ERR(plane_state))
|
| 195 | return PTR_ERR(plane_state);
|
| 196 |
|
| 197 | plane->state = plane_state;
|
| 198 |
|
| 199 | return 0;
|
| 200 | }
|
| 201 |
|
| 202 | static int drm_mode_config_plane_reset_with_create_state(struct drm_plane *plane)
|
| 203 | {
|
| 204 | if (plane->state) {
|
| 205 | plane->funcs->atomic_destroy_state(plane, plane->state);
|
| 206 | plane->state = NULL;
|
| 207 | }
|
| 208 |
|
| 209 | return drm_mode_config_plane_create_state(plane);
|
| 210 | }
|
| 211 |
|
| 212 | static int drm_mode_config_crtc_create_state(struct drm_crtc *crtc)
|
| 213 | {
|
| 214 | struct drm_crtc_state *crtc_state;
|
| 215 |
|
| 216 | if (!crtc->funcs->atomic_create_state)
|
| 217 | return 0;
|
| 218 |
|
| 219 | crtc_state = crtc->funcs->atomic_create_state(crtc);
|
| 220 | if (IS_ERR(crtc_state))
|
| 221 | return PTR_ERR(crtc_state);
|
| 222 |
|
| 223 | if (drm_dev_has_vblank(crtc->dev))
|
| 224 | drm_crtc_vblank_reset(crtc);
|
| 225 |
|
| 226 | crtc->state = crtc_state;
|
| 227 |
|
| 228 | return 0;
|
| 229 | }
|
| 230 |
|
| 231 | static int drm_mode_config_crtc_reset_with_create_state(struct drm_crtc *crtc)
|
| 232 | {
|
| 233 | if (crtc->state) {
|
| 234 | crtc->funcs->atomic_destroy_state(crtc, crtc->state);
|
| 235 | crtc->state = NULL;
|
| 236 | }
|
| 237 |
|
| 238 | return drm_mode_config_crtc_create_state(crtc);
|
| 239 | }
|
| 240 |
|
| 241 | static int drm_mode_config_connector_create_state(struct drm_connector *connector)
|
| 242 | {
|
| 243 | struct drm_connector_state *conn_state;
|
| 244 |
|
| 245 | if (!connector->funcs->atomic_create_state)
|
| 246 | return 0;
|
| 247 |
|
| 248 | conn_state = connector->funcs->atomic_create_state(connector);
|
| 249 | if (IS_ERR(conn_state))
|
| 250 | return PTR_ERR(conn_state);
|
| 251 |
|
| 252 | connector->state = conn_state;
|
| 253 |
|
| 254 | return 0;
|
| 255 | }
|
| 256 |
|
| 257 | static int drm_mode_config_connector_reset_with_create_state(struct drm_connector *connector)
|
| 258 | {
|
| 259 | if (connector->state) {
|
| 260 | connector->funcs->atomic_destroy_state(connector, connector->state);
|
| 261 | connector->state = NULL;
|
| 262 | }
|
| 263 |
|
| 264 | return drm_mode_config_connector_create_state(connector);
|
| 265 | }
|
| 266 |
|
| 267 | |
| 268 | * drm_mode_config_reset - call ->reset callbacks
|
| 269 | * @dev: drm device
|
| 270 | *
|
| 271 | * This functions calls all the crtc's, encoder's and connector's ->reset
|
| 272 | * callback. Drivers can use this in e.g. their driver load or resume code to
|
| 273 | * reset hardware and software state.
|
| 274 | *
|
| 275 | * Note that &drm_private_obj structures are expected to be stable across
|
| 276 | * suspend/resume cycles, and drm_mode_config_reset() does not affect these
|
| 277 | * structures.
|
| 278 |
|
| 279 | void drm_mode_config_reset(struct drm_device *dev)
|
| 280 | {
|
| 281 | struct drm_crtc *crtc;
|
| 282 | struct drm_colorop *colorop;
|
| 283 | struct drm_plane *plane;
|
| 284 | struct drm_encoder *encoder;
|
| 285 | struct drm_connector *connector;
|
| 286 | struct drm_connector_list_iter conn_iter;
|
| 287 |
|
| 288 | drm_for_each_colorop(colorop, dev)
|
| 289 | drm_colorop_reset(colorop);
|
| 290 |
|
| 291 | drm_for_each_plane(plane, dev) {
|
| 292 | if (plane->funcs->reset)
|
| 293 | plane->funcs->reset(plane);
|
| 294 | else if (plane->funcs->atomic_create_state)
|
| 295 | drm_mode_config_plane_reset_with_create_state(plane);
|
| 296 | }
|
| 297 |
|
| 298 | drm_for_each_crtc(crtc, dev) {
|
| 299 | if (crtc->funcs->reset)
|
| 300 | crtc->funcs->reset(crtc);
|
| 301 | else if (crtc->funcs->atomic_create_state)
|
| 302 | drm_mode_config_crtc_reset_with_create_state(crtc);
|
| 303 | }
|
| 304 |
|
| 305 | drm_for_each_encoder(encoder, dev)
|
| 306 | if (encoder->funcs && encoder->funcs->reset)
|
| 307 | encoder->funcs->reset(encoder);
|
| 308 |
|
| 309 | drm_connector_list_iter_begin(dev, &conn_iter);
|
| 310 | drm_for_each_connector_iter(connector, &conn_iter) {
|
| 311 | if (connector->funcs->reset)
|
| 312 | connector->funcs->reset(connector);
|
| 313 | else if (connector->funcs->atomic_create_state)
|
| 314 | drm_mode_config_connector_reset_with_create_state(connector);
|
| 315 | }
|
| 316 | drm_connector_list_iter_end(&conn_iter);
|
| 317 | }
|
| 318 | EXPORT_SYMBOL(drm_mode_config_reset);
|
| 319 |
|
| 320 | |
| 321 | * drm_mode_config_create_initial_state - Allocates the initial state
|
| 322 | * @dev: drm device
|
| 323 | *
|
| 324 | * This functions creates the initial state for all the objects. Drivers
|
| 325 | * can use this in e.g. probe to initialize their software state.
|
| 326 | *
|
| 327 | * It has two main differences with drm_mode_config_reset(): the reset()
|
| 328 | * hooks aren't called and thus the hardware will be left untouched, but
|
| 329 | * also the &drm_private_obj structures will be initialized as opposed
|
| 330 | * to drm_mode_config_reset() that skips them.
|
| 331 | *
|
| 332 | * Returns: 0 on success, negative error value on failure.
|
| 333 |
|
| 334 | int drm_mode_config_create_initial_state(struct drm_device *dev)
|
| 335 | {
|
| 336 | struct drm_crtc *crtc;
|
| 337 | struct drm_colorop *colorop;
|
| 338 | struct drm_plane *plane;
|
| 339 | struct drm_connector *connector;
|
| 340 | struct drm_connector_list_iter conn_iter;
|
| 341 | struct drm_private_obj *privobj;
|
| 342 | int ret;
|
| 343 |
|
| 344 | drm_for_each_privobj(privobj, dev) {
|
| 345 | struct drm_private_state *privobj_state;
|
| 346 |
|
| 347 | if (privobj->state)
|
| 348 | continue;
|
| 349 |
|
| 350 | if (!privobj->funcs->atomic_create_state)
|
| 351 | continue;
|
| 352 |
|
| 353 | privobj_state = privobj->funcs->atomic_create_state(privobj);
|
| 354 | if (IS_ERR(privobj_state))
|
| 355 | return PTR_ERR(privobj_state);
|
| 356 |
|
| 357 | privobj->state = privobj_state;
|
| 358 | }
|
| 359 |
|
| 360 | drm_for_each_colorop(colorop, dev) {
|
| 361 | struct drm_colorop_state *colorop_state;
|
| 362 |
|
| 363 | if (colorop->state)
|
| 364 | continue;
|
| 365 |
|
| 366 | colorop_state = drm_atomic_helper_colorop_create_state(colorop);
|
| 367 | if (IS_ERR(colorop_state))
|
| 368 | return PTR_ERR(colorop_state);
|
| 369 |
|
| 370 | colorop->state = colorop_state;
|
| 371 | }
|
| 372 |
|
| 373 | drm_for_each_plane(plane, dev) {
|
| 374 | if (plane->state)
|
| 375 | continue;
|
| 376 |
|
| 377 | ret = drm_mode_config_plane_create_state(plane);
|
| 378 | if (ret)
|
| 379 | return ret;
|
| 380 | }
|
| 381 |
|
| 382 | drm_for_each_crtc(crtc, dev) {
|
| 383 | if (crtc->state)
|
| 384 | continue;
|
| 385 |
|
| 386 | ret = drm_mode_config_crtc_create_state(crtc);
|
| 387 | if (ret)
|
| 388 | return ret;
|
| 389 | }
|
| 390 |
|
| 391 | drm_connector_list_iter_begin(dev, &conn_iter);
|
| 392 | drm_for_each_connector_iter(connector, &conn_iter) {
|
| 393 | if (connector->state)
|
| 394 | continue;
|
| 395 |
|
| 396 | ret = drm_mode_config_connector_create_state(connector);
|
| 397 | if (ret) {
|
| 398 | drm_connector_list_iter_end(&conn_iter);
|
| 399 | return ret;
|
| 400 | }
|
| 401 | }
|
| 402 | drm_connector_list_iter_end(&conn_iter);
|
| 403 |
|
| 404 | return 0;
|
| 405 | }
|
| 406 | EXPORT_SYMBOL(drm_mode_config_create_initial_state);
|
| 407 |
|
| 408 | |
| 409 | * Global properties
|
| 410 |
|
| 411 | static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
|
| 412 | { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
|
| 413 | { DRM_PLANE_TYPE_PRIMARY, "Primary" },
|
| 414 | { DRM_PLANE_TYPE_CURSOR, "Cursor" },
|
| 415 | };
|
| 416 |
|
| 417 | static int drm_mode_create_standard_properties(struct drm_device *dev)
|
| 418 | {
|
| 419 | struct drm_property *prop;
|
| 420 | int ret;
|
| 421 |
|
| 422 | ret = drm_connector_create_standard_properties(dev);
|
| 423 | if (ret)
|
| 424 | return ret;
|
| 425 |
|
| 426 | prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
|
| 427 | "type", drm_plane_type_enum_list,
|
| 428 | ARRAY_SIZE(drm_plane_type_enum_list));
|
| 429 | if (!prop)
|
| 430 | return -ENOMEM;
|
| 431 | dev->mode_config.plane_type_property = prop;
|
| 432 |
|
| 433 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
|
| 434 | "SRC_X", 0, UINT_MAX);
|
| 435 | if (!prop)
|
| 436 | return -ENOMEM;
|
| 437 | dev->mode_config.prop_src_x = prop;
|
| 438 |
|
| 439 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
|
| 440 | "SRC_Y", 0, UINT_MAX);
|
| 441 | if (!prop)
|
| 442 | return -ENOMEM;
|
| 443 | dev->mode_config.prop_src_y = prop;
|
| 444 |
|
| 445 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
|
| 446 | "SRC_W", 0, UINT_MAX);
|
| 447 | if (!prop)
|
| 448 | return -ENOMEM;
|
| 449 | dev->mode_config.prop_src_w = prop;
|
| 450 |
|
| 451 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
|
| 452 | "SRC_H", 0, UINT_MAX);
|
| 453 | if (!prop)
|
| 454 | return -ENOMEM;
|
| 455 | dev->mode_config.prop_src_h = prop;
|
| 456 |
|
| 457 | prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
|
| 458 | "CRTC_X", INT_MIN, INT_MAX);
|
| 459 | if (!prop)
|
| 460 | return -ENOMEM;
|
| 461 | dev->mode_config.prop_crtc_x = prop;
|
| 462 |
|
| 463 | prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
|
| 464 | "CRTC_Y", INT_MIN, INT_MAX);
|
| 465 | if (!prop)
|
| 466 | return -ENOMEM;
|
| 467 | dev->mode_config.prop_crtc_y = prop;
|
| 468 |
|
| 469 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
|
| 470 | "CRTC_W", 0, INT_MAX);
|
| 471 | if (!prop)
|
| 472 | return -ENOMEM;
|
| 473 | dev->mode_config.prop_crtc_w = prop;
|
| 474 |
|
| 475 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
|
| 476 | "CRTC_H", 0, INT_MAX);
|
| 477 | if (!prop)
|
| 478 | return -ENOMEM;
|
| 479 | dev->mode_config.prop_crtc_h = prop;
|
| 480 |
|
| 481 | prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
|
| 482 | "FB_ID", DRM_MODE_OBJECT_FB);
|
| 483 | if (!prop)
|
| 484 | return -ENOMEM;
|
| 485 | dev->mode_config.prop_fb_id = prop;
|
| 486 |
|
| 487 | prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
|
| 488 | "IN_FENCE_FD", -1, INT_MAX);
|
| 489 | if (!prop)
|
| 490 | return -ENOMEM;
|
| 491 | dev->mode_config.prop_in_fence_fd = prop;
|
| 492 |
|
| 493 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
|
| 494 | "OUT_FENCE_PTR", 0, U64_MAX);
|
| 495 | if (!prop)
|
| 496 | return -ENOMEM;
|
| 497 | dev->mode_config.prop_out_fence_ptr = prop;
|
| 498 |
|
| 499 | prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
|
| 500 | "CRTC_ID", DRM_MODE_OBJECT_CRTC);
|
| 501 | if (!prop)
|
| 502 | return -ENOMEM;
|
| 503 | dev->mode_config.prop_crtc_id = prop;
|
| 504 |
|
| 505 | prop = drm_property_create(dev,
|
| 506 | DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
|
| 507 | "FB_DAMAGE_CLIPS", 0);
|
| 508 | if (!prop)
|
| 509 | return -ENOMEM;
|
| 510 | dev->mode_config.prop_fb_damage_clips = prop;
|
| 511 |
|
| 512 | prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
|
| 513 | "ACTIVE");
|
| 514 | if (!prop)
|
| 515 | return -ENOMEM;
|
| 516 | dev->mode_config.prop_active = prop;
|
| 517 |
|
| 518 | prop = drm_property_create(dev,
|
| 519 | DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
|
| 520 | "MODE_ID", 0);
|
| 521 | if (!prop)
|
| 522 | return -ENOMEM;
|
| 523 | dev->mode_config.prop_mode_id = prop;
|
| 524 |
|
| 525 | prop = drm_property_create_bool(dev, 0,
|
| 526 | "VRR_ENABLED");
|
| 527 | if (!prop)
|
| 528 | return -ENOMEM;
|
| 529 | dev->mode_config.prop_vrr_enabled = prop;
|
| 530 |
|
| 531 | prop = drm_property_create(dev,
|
| 532 | DRM_MODE_PROP_BLOB,
|
| 533 | "DEGAMMA_LUT", 0);
|
| 534 | if (!prop)
|
| 535 | return -ENOMEM;
|
| 536 | dev->mode_config.degamma_lut_property = prop;
|
| 537 |
|
| 538 | prop = drm_property_create_range(dev,
|
| 539 | DRM_MODE_PROP_IMMUTABLE,
|
| 540 | "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
|
| 541 | if (!prop)
|
| 542 | return -ENOMEM;
|
| 543 | dev->mode_config.degamma_lut_size_property = prop;
|
| 544 |
|
| 545 | prop = drm_property_create(dev,
|
| 546 | DRM_MODE_PROP_BLOB,
|
| 547 | "CTM", 0);
|
| 548 | if (!prop)
|
| 549 | return -ENOMEM;
|
| 550 | dev->mode_config.ctm_property = prop;
|
| 551 |
|
| 552 | prop = drm_property_create(dev,
|
| 553 | DRM_MODE_PROP_BLOB,
|
| 554 | "GAMMA_LUT", 0);
|
| 555 | if (!prop)
|
| 556 | return -ENOMEM;
|
| 557 | dev->mode_config.gamma_lut_property = prop;
|
| 558 |
|
| 559 | prop = drm_property_create_range(dev,
|
| 560 | DRM_MODE_PROP_IMMUTABLE,
|
| 561 | "GAMMA_LUT_SIZE", 0, UINT_MAX);
|
| 562 | if (!prop)
|
| 563 | return -ENOMEM;
|
| 564 | dev->mode_config.gamma_lut_size_property = prop;
|
| 565 |
|
| 566 | prop = drm_property_create_range(dev, 0,
|
| 567 | "BACKGROUND_COLOR", 0, U64_MAX);
|
| 568 | if (!prop)
|
| 569 | return -ENOMEM;
|
| 570 | dev->mode_config.background_color_property = prop;
|
| 571 |
|
| 572 | prop = drm_property_create(dev,
|
| 573 | DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
|
| 574 | "IN_FORMATS", 0);
|
| 575 | if (!prop)
|
| 576 | return -ENOMEM;
|
| 577 | dev->mode_config.modifiers_property = prop;
|
| 578 |
|
| 579 | prop = drm_property_create(dev,
|
| 580 | DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
|
| 581 | "IN_FORMATS_ASYNC", 0);
|
| 582 | if (!prop)
|
| 583 | return -ENOMEM;
|
| 584 | dev->mode_config.async_modifiers_property = prop;
|
| 585 |
|
| 586 | prop = drm_property_create(dev,
|
| 587 | DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
|
| 588 | "SIZE_HINTS", 0);
|
| 589 | if (!prop)
|
| 590 | return -ENOMEM;
|
| 591 | dev->mode_config.size_hints_property = prop;
|
| 592 |
|
| 593 | return 0;
|
| 594 | }
|
| 595 |
|
| 596 | static void drm_mode_config_init_release(struct drm_device *dev, void *ptr)
|
| 597 | {
|
| 598 | drm_mode_config_cleanup(dev);
|
| 599 | }
|
| 600 |
|
| 601 | |
| 602 | * drmm_mode_config_init - managed DRM mode_configuration structure
|
| 603 | * initialization
|
| 604 | * @dev: DRM device
|
| 605 | *
|
| 606 | * Initialize @dev's mode_config structure, used for tracking the graphics
|
| 607 | * configuration of @dev.
|
| 608 | *
|
| 609 | * Since this initializes the modeset locks, no locking is possible. Which is no
|
| 610 | * problem, since this should happen single threaded at init time. It is the
|
| 611 | * driver's problem to ensure this guarantee.
|
| 612 | *
|
| 613 | * Cleanup is automatically handled through registering drm_mode_config_cleanup
|
| 614 | * with drmm_add_action().
|
| 615 | *
|
| 616 | * Returns: 0 on success, negative error value on failure.
|
| 617 |
|
| 618 | int drmm_mode_config_init(struct drm_device *dev)
|
| 619 | {
|
| 620 | int ret;
|
| 621 |
|
| 622 | mutex_init(&dev->mode_config.mutex);
|
| 623 | drm_modeset_lock_init(&dev->mode_config.connection_mutex);
|
| 624 | mutex_init(&dev->mode_config.idr_mutex);
|
| 625 | mutex_init(&dev->mode_config.fb_lock);
|
| 626 | mutex_init(&dev->mode_config.blob_lock);
|
| 627 | INIT_LIST_HEAD(&dev->mode_config.fb_list);
|
| 628 | INIT_LIST_HEAD(&dev->mode_config.crtc_list);
|
| 629 | INIT_LIST_HEAD(&dev->mode_config.connector_list);
|
| 630 | INIT_LIST_HEAD(&dev->mode_config.encoder_list);
|
| 631 | INIT_LIST_HEAD(&dev->mode_config.property_list);
|
| 632 | INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
|
| 633 | INIT_LIST_HEAD(&dev->mode_config.plane_list);
|
| 634 | INIT_LIST_HEAD(&dev->mode_config.colorop_list);
|
| 635 | INIT_LIST_HEAD(&dev->mode_config.privobj_list);
|
| 636 | idr_init_base(&dev->mode_config.object_idr, 1);
|
| 637 | idr_init_base(&dev->mode_config.tile_idr, 1);
|
| 638 | ida_init(&dev->mode_config.connector_ida);
|
| 639 | spin_lock_init(&dev->mode_config.connector_list_lock);
|
| 640 |
|
| 641 | init_llist_head(&dev->mode_config.connector_free_list);
|
| 642 | INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
|
| 643 |
|
| 644 | ret = drm_mode_create_standard_properties(dev);
|
| 645 | if (ret) {
|
| 646 | drm_mode_config_cleanup(dev);
|
| 647 | return ret;
|
| 648 | }
|
| 649 |
|
| 650 |
|
| 651 | dev->mode_config.num_fb = 0;
|
| 652 | dev->mode_config.num_connector = 0;
|
| 653 | dev->mode_config.num_crtc = 0;
|
| 654 | dev->mode_config.num_encoder = 0;
|
| 655 | dev->mode_config.num_total_plane = 0;
|
| 656 | dev->mode_config.num_colorop = 0;
|
| 657 |
|
| 658 | if (IS_ENABLED(CONFIG_LOCKDEP)) {
|
| 659 | struct drm_modeset_acquire_ctx modeset_ctx;
|
| 660 | struct ww_acquire_ctx resv_ctx;
|
| 661 | struct dma_resv resv;
|
| 662 | int ret;
|
| 663 |
|
| 664 | dma_resv_init(&resv);
|
| 665 |
|
| 666 | drm_modeset_acquire_init(&modeset_ctx, 0);
|
| 667 | ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
|
| 668 | &modeset_ctx);
|
| 669 | if (ret == -EDEADLK)
|
| 670 | ret = drm_modeset_backoff(&modeset_ctx);
|
| 671 |
|
| 672 | might_fault();
|
| 673 |
|
| 674 | ww_acquire_init(&resv_ctx, &reservation_ww_class);
|
| 675 | ret = dma_resv_lock(&resv, &resv_ctx);
|
| 676 | if (ret == -EDEADLK)
|
| 677 | dma_resv_lock_slow(&resv, &resv_ctx);
|
| 678 |
|
| 679 | dma_resv_unlock(&resv);
|
| 680 | ww_acquire_fini(&resv_ctx);
|
| 681 |
|
| 682 | drm_modeset_drop_locks(&modeset_ctx);
|
| 683 | drm_modeset_acquire_fini(&modeset_ctx);
|
| 684 | dma_resv_fini(&resv);
|
| 685 | }
|
| 686 |
|
| 687 | return drmm_add_action_or_reset(dev, drm_mode_config_init_release,
|
| 688 | NULL);
|
| 689 | }
|
| 690 | EXPORT_SYMBOL(drmm_mode_config_init);
|
| 691 |
|
| 692 | |
| 693 | * drm_mode_config_cleanup - free up DRM mode_config info
|
| 694 | * @dev: DRM device
|
| 695 | *
|
| 696 | * Free up all the connectors and CRTCs associated with this DRM device, then
|
| 697 | * free up the framebuffers and associated buffer objects.
|
| 698 | *
|
| 699 | * Note that since this /should/ happen single-threaded at driver/device
|
| 700 | * teardown time, no locking is required. It's the driver's job to ensure that
|
| 701 | * this guarantee actually holds true.
|
| 702 | *
|
| 703 | * FIXME: With the managed drmm_mode_config_init() it is no longer necessary for
|
| 704 | * drivers to explicitly call this function.
|
| 705 |
|
| 706 | void drm_mode_config_cleanup(struct drm_device *dev)
|
| 707 | {
|
| 708 | struct drm_connector *connector;
|
| 709 | struct drm_connector_list_iter conn_iter;
|
| 710 | struct drm_crtc *crtc, *ct;
|
| 711 | struct drm_encoder *encoder, *enct;
|
| 712 | struct drm_framebuffer *fb, *fbt;
|
| 713 | struct drm_property *property, *pt;
|
| 714 | struct drm_property_blob *blob, *bt;
|
| 715 | struct drm_plane *plane, *plt;
|
| 716 | struct drm_colorop *colorop, *copt;
|
| 717 |
|
| 718 | list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
|
| 719 | head) {
|
| 720 | encoder->funcs->destroy(encoder);
|
| 721 | }
|
| 722 |
|
| 723 | drm_connector_list_iter_begin(dev, &conn_iter);
|
| 724 | drm_for_each_connector_iter(connector, &conn_iter) {
|
| 725 | |
| 726 | * current connector itself, which means it is inherently safe
|
| 727 | * against unreferencing the current connector - but not against
|
| 728 | * deleting it right away.
|
| 729 | drm_connector_put(connector);
|
| 730 | }
|
| 731 | drm_connector_list_iter_end(&conn_iter);
|
| 732 |
|
| 733 | flush_work(&dev->mode_config.connector_free_work);
|
| 734 | if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
|
| 735 | drm_connector_list_iter_begin(dev, &conn_iter);
|
| 736 | drm_for_each_connector_iter(connector, &conn_iter)
|
| 737 | DRM_ERROR("connector %s leaked!\n", connector->name);
|
| 738 | drm_connector_list_iter_end(&conn_iter);
|
| 739 | }
|
| 740 |
|
| 741 | list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
|
| 742 | head) {
|
| 743 | drm_property_destroy(dev, property);
|
| 744 | }
|
| 745 |
|
| 746 | list_for_each_entry_safe(colorop, copt, &dev->mode_config.colorop_list,
|
| 747 | head) {
|
| 748 | colorop->funcs->destroy(colorop);
|
| 749 | }
|
| 750 |
|
| 751 | list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
|
| 752 | head) {
|
| 753 | plane->funcs->destroy(plane);
|
| 754 | }
|
| 755 |
|
| 756 | list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
|
| 757 | crtc->funcs->destroy(crtc);
|
| 758 | }
|
| 759 |
|
| 760 | list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
|
| 761 | head_global) {
|
| 762 | drm_property_blob_put(blob);
|
| 763 | }
|
| 764 |
|
| 765 | |
| 766 | * Single-threaded teardown context, so it's not required to grab the
|
| 767 | * fb_lock to protect against concurrent fb_list access. Contrary, it
|
| 768 | * would actually deadlock with the drm_framebuffer_cleanup function.
|
| 769 | *
|
| 770 | * Also, if there are any framebuffers left, that's a driver leak now,
|
| 771 | * so politely WARN about this.
|
| 772 |
|
| 773 | WARN_ON(!list_empty(&dev->mode_config.fb_list));
|
| 774 | list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
|
| 775 | struct drm_printer p = drm_dbg_printer(dev, DRM_UT_KMS, "[leaked fb]");
|
| 776 |
|
| 777 | drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
|
| 778 | drm_framebuffer_print_info(&p, 1, fb);
|
| 779 | drm_framebuffer_free(&fb->base.refcount);
|
| 780 | }
|
| 781 |
|
| 782 | ida_destroy(&dev->mode_config.connector_ida);
|
| 783 | idr_destroy(&dev->mode_config.tile_idr);
|
| 784 | idr_destroy(&dev->mode_config.object_idr);
|
| 785 | drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
|
| 786 | }
|
| 787 | EXPORT_SYMBOL(drm_mode_config_cleanup);
|
| 788 |
|
| 789 | static u32 full_encoder_mask(struct drm_device *dev)
|
| 790 | {
|
| 791 | struct drm_encoder *encoder;
|
| 792 | u32 encoder_mask = 0;
|
| 793 |
|
| 794 | drm_for_each_encoder(encoder, dev)
|
| 795 | encoder_mask |= drm_encoder_mask(encoder);
|
| 796 |
|
| 797 | return encoder_mask;
|
| 798 | }
|
| 799 |
|
| 800 | |
| 801 | * For some reason we want the encoder itself included in
|
| 802 | * possible_clones. Make life easy for drivers by allowing them
|
| 803 | * to leave possible_clones unset if no cloning is possible.
|
| 804 |
|
| 805 | static void fixup_encoder_possible_clones(struct drm_encoder *encoder)
|
| 806 | {
|
| 807 | if (encoder->possible_clones == 0)
|
| 808 | encoder->possible_clones = drm_encoder_mask(encoder);
|
| 809 | }
|
| 810 |
|
| 811 | static void validate_encoder_possible_clones(struct drm_encoder *encoder)
|
| 812 | {
|
| 813 | struct drm_device *dev = encoder->dev;
|
| 814 | u32 encoder_mask = full_encoder_mask(dev);
|
| 815 | struct drm_encoder *other;
|
| 816 |
|
| 817 | drm_for_each_encoder(other, dev) {
|
| 818 | WARN(!!(encoder->possible_clones & drm_encoder_mask(other)) !=
|
| 819 | !!(other->possible_clones & drm_encoder_mask(encoder)),
|
| 820 | "possible_clones mismatch: "
|
| 821 | "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x vs. "
|
| 822 | "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x\n",
|
| 823 | encoder->base.id, encoder->name,
|
| 824 | drm_encoder_mask(encoder), encoder->possible_clones,
|
| 825 | other->base.id, other->name,
|
| 826 | drm_encoder_mask(other), other->possible_clones);
|
| 827 | }
|
| 828 |
|
| 829 | WARN((encoder->possible_clones & drm_encoder_mask(encoder)) == 0 ||
|
| 830 | (encoder->possible_clones & ~encoder_mask) != 0,
|
| 831 | "Bogus possible_clones: "
|
| 832 | "[ENCODER:%d:%s] possible_clones=0x%x (full encoder mask=0x%x)\n",
|
| 833 | encoder->base.id, encoder->name,
|
| 834 | encoder->possible_clones, encoder_mask);
|
| 835 | }
|
| 836 |
|
| 837 | static u32 full_crtc_mask(struct drm_device *dev)
|
| 838 | {
|
| 839 | struct drm_crtc *crtc;
|
| 840 | u32 crtc_mask = 0;
|
| 841 |
|
| 842 | drm_for_each_crtc(crtc, dev)
|
| 843 | crtc_mask |= drm_crtc_mask(crtc);
|
| 844 |
|
| 845 | return crtc_mask;
|
| 846 | }
|
| 847 |
|
| 848 | static void validate_encoder_possible_crtcs(struct drm_encoder *encoder)
|
| 849 | {
|
| 850 | u32 crtc_mask = full_crtc_mask(encoder->dev);
|
| 851 |
|
| 852 | WARN((encoder->possible_crtcs & crtc_mask) == 0 ||
|
| 853 | (encoder->possible_crtcs & ~crtc_mask) != 0,
|
| 854 | "Bogus possible_crtcs: "
|
| 855 | "[ENCODER:%d:%s] possible_crtcs=0x%x (full crtc mask=0x%x)\n",
|
| 856 | encoder->base.id, encoder->name,
|
| 857 | encoder->possible_crtcs, crtc_mask);
|
| 858 | }
|
| 859 |
|
| 860 | static void validate_blend_mode_for_alpha_formats(struct drm_plane *plane)
|
| 861 | {
|
| 862 | const struct drm_format_info *fmt;
|
| 863 | u32 i;
|
| 864 |
|
| 865 |
|
| 866 | if (plane->blend_mode_property)
|
| 867 | return;
|
| 868 |
|
| 869 | for (i = 0; i < plane->format_count; i++) {
|
| 870 | fmt = drm_format_info(plane->format_types[i]);
|
| 871 | if (fmt->has_alpha) {
|
| 872 | drm_warn(plane->dev,
|
| 873 | "[PLANE:%d:%s] pixel format with alpha exposed but blend mode not setup. Please fix.\n",
|
| 874 | plane->base.id, plane->name);
|
| 875 | break;
|
| 876 | }
|
| 877 | }
|
| 878 | }
|
| 879 |
|
| 880 | void drm_mode_config_validate(struct drm_device *dev)
|
| 881 | {
|
| 882 | struct drm_encoder *encoder;
|
| 883 | struct drm_crtc *crtc;
|
| 884 | struct drm_plane *plane;
|
| 885 | u32 primary_with_crtc = 0, cursor_with_crtc = 0;
|
| 886 | unsigned int num_primary = 0;
|
| 887 |
|
| 888 | if (!drm_core_check_feature(dev, DRIVER_MODESET))
|
| 889 | return;
|
| 890 |
|
| 891 | drm_for_each_encoder(encoder, dev)
|
| 892 | fixup_encoder_possible_clones(encoder);
|
| 893 |
|
| 894 | drm_for_each_encoder(encoder, dev) {
|
| 895 | validate_encoder_possible_clones(encoder);
|
| 896 | validate_encoder_possible_crtcs(encoder);
|
| 897 | }
|
| 898 |
|
| 899 | drm_for_each_crtc(crtc, dev) {
|
| 900 | WARN(!crtc->primary, "Missing primary plane on [CRTC:%d:%s]\n",
|
| 901 | crtc->base.id, crtc->name);
|
| 902 |
|
| 903 | WARN(crtc->cursor && crtc->funcs->cursor_set,
|
| 904 | "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set func",
|
| 905 | crtc->base.id, crtc->name);
|
| 906 | WARN(crtc->cursor && crtc->funcs->cursor_set2,
|
| 907 | "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set2 func",
|
| 908 | crtc->base.id, crtc->name);
|
| 909 | WARN(crtc->cursor && crtc->funcs->cursor_move,
|
| 910 | "[CRTC:%d:%s] must not have both a cursor plane and a cursor_move func",
|
| 911 | crtc->base.id, crtc->name);
|
| 912 |
|
| 913 | if (crtc->primary) {
|
| 914 | WARN(!(crtc->primary->possible_crtcs & drm_crtc_mask(crtc)),
|
| 915 | "Bogus primary plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
|
| 916 | crtc->primary->base.id, crtc->primary->name,
|
| 917 | crtc->base.id, crtc->name);
|
| 918 | WARN(primary_with_crtc & drm_plane_mask(crtc->primary),
|
| 919 | "Primary plane [PLANE:%d:%s] used for multiple CRTCs",
|
| 920 | crtc->primary->base.id, crtc->primary->name);
|
| 921 | primary_with_crtc |= drm_plane_mask(crtc->primary);
|
| 922 | }
|
| 923 | if (crtc->cursor) {
|
| 924 | WARN(!(crtc->cursor->possible_crtcs & drm_crtc_mask(crtc)),
|
| 925 | "Bogus cursor plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
|
| 926 | crtc->cursor->base.id, crtc->cursor->name,
|
| 927 | crtc->base.id, crtc->name);
|
| 928 | WARN(cursor_with_crtc & drm_plane_mask(crtc->cursor),
|
| 929 | "Cursor plane [PLANE:%d:%s] used for multiple CRTCs",
|
| 930 | crtc->cursor->base.id, crtc->cursor->name);
|
| 931 | cursor_with_crtc |= drm_plane_mask(crtc->cursor);
|
| 932 | }
|
| 933 | }
|
| 934 |
|
| 935 | drm_for_each_plane(plane, dev) {
|
| 936 | if (plane->type == DRM_PLANE_TYPE_PRIMARY)
|
| 937 | num_primary++;
|
| 938 |
|
| 939 | validate_blend_mode_for_alpha_formats(plane);
|
| 940 | }
|
| 941 |
|
| 942 | WARN(num_primary != dev->mode_config.num_crtc,
|
| 943 | "Must have as many primary planes as there are CRTCs, but have %u primary planes and %u CRTCs",
|
| 944 | num_primary, dev->mode_config.num_crtc);
|
| 945 | }
|