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 #ifndef __DRM_PLANE_H__ 24 #define __DRM_PLANE_H__ 25 26 #include <linux/list.h> 27 #include <linux/ctype.h> 28 #include <drm/drm_mode_object.h> 29 30 struct drm_crtc; 31 struct drm_printer; 32 struct drm_modeset_acquire_ctx; 33 34 /** 35 * struct drm_plane_state - mutable plane state 36 * @plane: backpointer to the plane 37 * @crtc_w: width of visible portion of plane on crtc 38 * @crtc_h: height of visible portion of plane on crtc 39 * @src_x: left position of visible portion of plane within 40 * plane (in 16.16) 41 * @src_y: upper position of visible portion of plane within 42 * plane (in 16.16) 43 * @src_w: width of visible portion of plane (in 16.16) 44 * @src_h: height of visible portion of plane (in 16.16) 45 * @rotation: rotation of the plane 46 * @zpos: priority of the given plane on crtc (optional) 47 * Note that multiple active planes on the same crtc can have an identical 48 * zpos value. The rule to solving the conflict is to compare the plane 49 * object IDs; the plane with a higher ID must be stacked on top of a 50 * plane with a lower ID. 51 * @normalized_zpos: normalized value of zpos: unique, range from 0 to N-1 52 * where N is the number of active planes for given crtc. Note that 53 * the driver must call drm_atomic_normalize_zpos() to update this before 54 * it can be trusted. 55 * @src: clipped source coordinates of the plane (in 16.16) 56 * @dst: clipped destination coordinates of the plane 57 * @state: backpointer to global drm_atomic_state 58 */ 59 struct drm_plane_state { 60 struct drm_plane *plane; 61 62 /** 63 * @crtc: 64 * 65 * Currently bound CRTC, NULL if disabled. Do not this write directly, 66 * use drm_atomic_set_crtc_for_plane() 67 */ 68 struct drm_crtc *crtc; 69 70 /** 71 * @fb: 72 * 73 * Currently bound framebuffer. Do not write this directly, use 74 * drm_atomic_set_fb_for_plane() 75 */ 76 struct drm_framebuffer *fb; 77 78 /** 79 * @fence: 80 * 81 * Optional fence to wait for before scanning out @fb. Do not write this 82 * directly, use drm_atomic_set_fence_for_plane() 83 */ 84 struct dma_fence *fence; 85 86 /** 87 * @crtc_x: 88 * 89 * Left position of visible portion of plane on crtc, signed dest 90 * location allows it to be partially off screen. 91 */ 92 93 int32_t crtc_x; 94 /** 95 * @crtc_y: 96 * 97 * Upper position of visible portion of plane on crtc, signed dest 98 * location allows it to be partially off screen. 99 */ 100 int32_t crtc_y; 101 102 uint32_t crtc_w, crtc_h; 103 104 /* Source values are 16.16 fixed point */ 105 uint32_t src_x, src_y; 106 uint32_t src_h, src_w; 107 108 /* Plane rotation */ 109 unsigned int rotation; 110 111 /* Plane zpos */ 112 unsigned int zpos; 113 unsigned int normalized_zpos; 114 115 /* Clipped coordinates */ 116 struct drm_rect src, dst; 117 118 /** 119 * @visible: 120 * 121 * Visibility of the plane. This can be false even if fb!=NULL and 122 * crtc!=NULL, due to clipping. 123 */ 124 bool visible; 125 126 struct drm_atomic_state *state; 127 }; 128 129 static inline struct drm_rect 130 drm_plane_state_src(const struct drm_plane_state *state) 131 { 132 struct drm_rect src = { 133 .x1 = state->src_x, 134 .y1 = state->src_y, 135 .x2 = state->src_x + state->src_w, 136 .y2 = state->src_y + state->src_h, 137 }; 138 return src; 139 } 140 141 static inline struct drm_rect 142 drm_plane_state_dest(const struct drm_plane_state *state) 143 { 144 struct drm_rect dest = { 145 .x1 = state->crtc_x, 146 .y1 = state->crtc_y, 147 .x2 = state->crtc_x + state->crtc_w, 148 .y2 = state->crtc_y + state->crtc_h, 149 }; 150 return dest; 151 } 152 153 /** 154 * struct drm_plane_funcs - driver plane control functions 155 */ 156 struct drm_plane_funcs { 157 /** 158 * @update_plane: 159 * 160 * This is the legacy entry point to enable and configure the plane for 161 * the given CRTC and framebuffer. It is never called to disable the 162 * plane, i.e. the passed-in crtc and fb paramters are never NULL. 163 * 164 * The source rectangle in frame buffer memory coordinates is given by 165 * the src_x, src_y, src_w and src_h parameters (as 16.16 fixed point 166 * values). Devices that don't support subpixel plane coordinates can 167 * ignore the fractional part. 168 * 169 * The destination rectangle in CRTC coordinates is given by the 170 * crtc_x, crtc_y, crtc_w and crtc_h parameters (as integer values). 171 * Devices scale the source rectangle to the destination rectangle. If 172 * scaling is not supported, and the source rectangle size doesn't match 173 * the destination rectangle size, the driver must return a 174 * -<errorname>EINVAL</errorname> error. 175 * 176 * Drivers implementing atomic modeset should use 177 * drm_atomic_helper_update_plane() to implement this hook. 178 * 179 * RETURNS: 180 * 181 * 0 on success or a negative error code on failure. 182 */ 183 int (*update_plane)(struct drm_plane *plane, 184 struct drm_crtc *crtc, struct drm_framebuffer *fb, 185 int crtc_x, int crtc_y, 186 unsigned int crtc_w, unsigned int crtc_h, 187 uint32_t src_x, uint32_t src_y, 188 uint32_t src_w, uint32_t src_h, 189 struct drm_modeset_acquire_ctx *ctx); 190 191 /** 192 * @disable_plane: 193 * 194 * This is the legacy entry point to disable the plane. The DRM core 195 * calls this method in response to a DRM_IOCTL_MODE_SETPLANE IOCTL call 196 * with the frame buffer ID set to 0. Disabled planes must not be 197 * processed by the CRTC. 198 * 199 * Drivers implementing atomic modeset should use 200 * drm_atomic_helper_disable_plane() to implement this hook. 201 * 202 * RETURNS: 203 * 204 * 0 on success or a negative error code on failure. 205 */ 206 int (*disable_plane)(struct drm_plane *plane); 207 208 /** 209 * @destroy: 210 * 211 * Clean up plane resources. This is only called at driver unload time 212 * through drm_mode_config_cleanup() since a plane cannot be hotplugged 213 * in DRM. 214 */ 215 void (*destroy)(struct drm_plane *plane); 216 217 /** 218 * @reset: 219 * 220 * Reset plane hardware and software state to off. This function isn't 221 * called by the core directly, only through drm_mode_config_reset(). 222 * It's not a helper hook only for historical reasons. 223 * 224 * Atomic drivers can use drm_atomic_helper_plane_reset() to reset 225 * atomic state using this hook. 226 */ 227 void (*reset)(struct drm_plane *plane); 228 229 /** 230 * @set_property: 231 * 232 * This is the legacy entry point to update a property attached to the 233 * plane. 234 * 235 * Drivers implementing atomic modeset should use 236 * drm_atomic_helper_plane_set_property() to implement this hook. 237 * 238 * This callback is optional if the driver does not support any legacy 239 * driver-private properties. 240 * 241 * RETURNS: 242 * 243 * 0 on success or a negative error code on failure. 244 */ 245 int (*set_property)(struct drm_plane *plane, 246 struct drm_property *property, uint64_t val); 247 248 /** 249 * @atomic_duplicate_state: 250 * 251 * Duplicate the current atomic state for this plane and return it. 252 * The core and helpers guarantee that any atomic state duplicated with 253 * this hook and still owned by the caller (i.e. not transferred to the 254 * driver by calling &drm_mode_config_funcs.atomic_commit) will be 255 * cleaned up by calling the @atomic_destroy_state hook in this 256 * structure. 257 * 258 * Atomic drivers which don't subclass &struct drm_plane_state should use 259 * drm_atomic_helper_plane_duplicate_state(). Drivers that subclass the 260 * state structure to extend it with driver-private state should use 261 * __drm_atomic_helper_plane_duplicate_state() to make sure shared state is 262 * duplicated in a consistent fashion across drivers. 263 * 264 * It is an error to call this hook before &drm_plane.state has been 265 * initialized correctly. 266 * 267 * NOTE: 268 * 269 * If the duplicate state references refcounted resources this hook must 270 * acquire a reference for each of them. The driver must release these 271 * references again in @atomic_destroy_state. 272 * 273 * RETURNS: 274 * 275 * Duplicated atomic state or NULL when the allocation failed. 276 */ 277 struct drm_plane_state *(*atomic_duplicate_state)(struct drm_plane *plane); 278 279 /** 280 * @atomic_destroy_state: 281 * 282 * Destroy a state duplicated with @atomic_duplicate_state and release 283 * or unreference all resources it references 284 */ 285 void (*atomic_destroy_state)(struct drm_plane *plane, 286 struct drm_plane_state *state); 287 288 /** 289 * @atomic_set_property: 290 * 291 * Decode a driver-private property value and store the decoded value 292 * into the passed-in state structure. Since the atomic core decodes all 293 * standardized properties (even for extensions beyond the core set of 294 * properties which might not be implemented by all drivers) this 295 * requires drivers to subclass the state structure. 296 * 297 * Such driver-private properties should really only be implemented for 298 * truly hardware/vendor specific state. Instead it is preferred to 299 * standardize atomic extension and decode the properties used to expose 300 * such an extension in the core. 301 * 302 * Do not call this function directly, use 303 * drm_atomic_plane_set_property() instead. 304 * 305 * This callback is optional if the driver does not support any 306 * driver-private atomic properties. 307 * 308 * NOTE: 309 * 310 * This function is called in the state assembly phase of atomic 311 * modesets, which can be aborted for any reason (including on 312 * userspace's request to just check whether a configuration would be 313 * possible). Drivers MUST NOT touch any persistent state (hardware or 314 * software) or data structures except the passed in @state parameter. 315 * 316 * Also since userspace controls in which order properties are set this 317 * function must not do any input validation (since the state update is 318 * incomplete and hence likely inconsistent). Instead any such input 319 * validation must be done in the various atomic_check callbacks. 320 * 321 * RETURNS: 322 * 323 * 0 if the property has been found, -EINVAL if the property isn't 324 * implemented by the driver (which shouldn't ever happen, the core only 325 * asks for properties attached to this plane). No other validation is 326 * allowed by the driver. The core already checks that the property 327 * value is within the range (integer, valid enum value, ...) the driver 328 * set when registering the property. 329 */ 330 int (*atomic_set_property)(struct drm_plane *plane, 331 struct drm_plane_state *state, 332 struct drm_property *property, 333 uint64_t val); 334 335 /** 336 * @atomic_get_property: 337 * 338 * Reads out the decoded driver-private property. This is used to 339 * implement the GETPLANE IOCTL. 340 * 341 * Do not call this function directly, use 342 * drm_atomic_plane_get_property() instead. 343 * 344 * This callback is optional if the driver does not support any 345 * driver-private atomic properties. 346 * 347 * RETURNS: 348 * 349 * 0 on success, -EINVAL if the property isn't implemented by the 350 * driver (which should never happen, the core only asks for 351 * properties attached to this plane). 352 */ 353 int (*atomic_get_property)(struct drm_plane *plane, 354 const struct drm_plane_state *state, 355 struct drm_property *property, 356 uint64_t *val); 357 /** 358 * @late_register: 359 * 360 * This optional hook can be used to register additional userspace 361 * interfaces attached to the plane like debugfs interfaces. 362 * It is called late in the driver load sequence from drm_dev_register(). 363 * Everything added from this callback should be unregistered in 364 * the early_unregister callback. 365 * 366 * Returns: 367 * 368 * 0 on success, or a negative error code on failure. 369 */ 370 int (*late_register)(struct drm_plane *plane); 371 372 /** 373 * @early_unregister: 374 * 375 * This optional hook should be used to unregister the additional 376 * userspace interfaces attached to the plane from 377 * @late_register. It is called from drm_dev_unregister(), 378 * early in the driver unload sequence to disable userspace access 379 * before data structures are torndown. 380 */ 381 void (*early_unregister)(struct drm_plane *plane); 382 383 /** 384 * @atomic_print_state: 385 * 386 * If driver subclasses &struct drm_plane_state, it should implement 387 * this optional hook for printing additional driver specific state. 388 * 389 * Do not call this directly, use drm_atomic_plane_print_state() 390 * instead. 391 */ 392 void (*atomic_print_state)(struct drm_printer *p, 393 const struct drm_plane_state *state); 394 }; 395 396 /** 397 * enum drm_plane_type - uapi plane type enumeration 398 * 399 * For historical reasons not all planes are made the same. This enumeration is 400 * used to tell the different types of planes apart to implement the different 401 * uapi semantics for them. For userspace which is universal plane aware and 402 * which is using that atomic IOCTL there's no difference between these planes 403 * (beyong what the driver and hardware can support of course). 404 * 405 * For compatibility with legacy userspace, only overlay planes are made 406 * available to userspace by default. Userspace clients may set the 407 * DRM_CLIENT_CAP_UNIVERSAL_PLANES client capability bit to indicate that they 408 * wish to receive a universal plane list containing all plane types. See also 409 * drm_for_each_legacy_plane(). 410 * 411 * WARNING: The values of this enum is UABI since they're exposed in the "type" 412 * property. 413 */ 414 enum drm_plane_type { 415 /** 416 * @DRM_PLANE_TYPE_OVERLAY: 417 * 418 * Overlay planes represent all non-primary, non-cursor planes. Some 419 * drivers refer to these types of planes as "sprites" internally. 420 */ 421 DRM_PLANE_TYPE_OVERLAY, 422 423 /** 424 * @DRM_PLANE_TYPE_PRIMARY: 425 * 426 * Primary planes represent a "main" plane for a CRTC. Primary planes 427 * are the planes operated upon by CRTC modesetting and flipping 428 * operations described in the &drm_crtc_funcs.page_flip and 429 * &drm_crtc_funcs.set_config hooks. 430 */ 431 DRM_PLANE_TYPE_PRIMARY, 432 433 /** 434 * @DRM_PLANE_TYPE_CURSOR: 435 * 436 * Cursor planes represent a "cursor" plane for a CRTC. Cursor planes 437 * are the planes operated upon by the DRM_IOCTL_MODE_CURSOR and 438 * DRM_IOCTL_MODE_CURSOR2 IOCTLs. 439 */ 440 DRM_PLANE_TYPE_CURSOR, 441 }; 442 443 444 /** 445 * struct drm_plane - central DRM plane control structure 446 * @dev: DRM device this plane belongs to 447 * @head: for list management 448 * @name: human readable name, can be overwritten by the driver 449 * @base: base mode object 450 * @possible_crtcs: pipes this plane can be bound to 451 * @format_types: array of formats supported by this plane 452 * @format_count: number of formats supported 453 * @format_default: driver hasn't supplied supported formats for the plane 454 * @crtc: currently bound CRTC 455 * @fb: currently bound fb 456 * @old_fb: Temporary tracking of the old fb while a modeset is ongoing. Used by 457 * drm_mode_set_config_internal() to implement correct refcounting. 458 * @funcs: helper functions 459 * @properties: property tracking for this plane 460 * @type: type of plane (overlay, primary, cursor) 461 * @zpos_property: zpos property for this plane 462 * @rotation_property: rotation property for this plane 463 * @helper_private: mid-layer private data 464 */ 465 struct drm_plane { 466 struct drm_device *dev; 467 struct list_head head; 468 469 char *name; 470 471 /** 472 * @mutex: 473 * 474 * Protects modeset plane state, together with the &drm_crtc.mutex of 475 * CRTC this plane is linked to (when active, getting activated or 476 * getting disabled). 477 * 478 * For atomic drivers specifically this protects @state. 479 */ 480 struct drm_modeset_lock mutex; 481 482 struct drm_mode_object base; 483 484 uint32_t possible_crtcs; 485 uint32_t *format_types; 486 unsigned int format_count; 487 bool format_default; 488 489 struct drm_crtc *crtc; 490 struct drm_framebuffer *fb; 491 492 struct drm_framebuffer *old_fb; 493 494 const struct drm_plane_funcs *funcs; 495 496 struct drm_object_properties properties; 497 498 enum drm_plane_type type; 499 500 /** 501 * @index: Position inside the mode_config.list, can be used as an array 502 * index. It is invariant over the lifetime of the plane. 503 */ 504 unsigned index; 505 506 const struct drm_plane_helper_funcs *helper_private; 507 508 /** 509 * @state: 510 * 511 * Current atomic state for this plane. 512 * 513 * This is protected by @mutex. Note that nonblocking atomic commits 514 * access the current plane state without taking locks. Either by going 515 * through the &struct drm_atomic_state pointers, see 516 * for_each_plane_in_state(), for_each_oldnew_plane_in_state(), 517 * for_each_old_plane_in_state() and for_each_new_plane_in_state(). Or 518 * through careful ordering of atomic commit operations as implemented 519 * in the atomic helpers, see &struct drm_crtc_commit. 520 */ 521 struct drm_plane_state *state; 522 523 struct drm_property *zpos_property; 524 struct drm_property *rotation_property; 525 }; 526 527 #define obj_to_plane(x) container_of(x, struct drm_plane, base) 528 529 __printf(8, 9) 530 int drm_universal_plane_init(struct drm_device *dev, 531 struct drm_plane *plane, 532 uint32_t possible_crtcs, 533 const struct drm_plane_funcs *funcs, 534 const uint32_t *formats, 535 unsigned int format_count, 536 enum drm_plane_type type, 537 const char *name, ...); 538 int drm_plane_init(struct drm_device *dev, 539 struct drm_plane *plane, 540 uint32_t possible_crtcs, 541 const struct drm_plane_funcs *funcs, 542 const uint32_t *formats, unsigned int format_count, 543 bool is_primary); 544 void drm_plane_cleanup(struct drm_plane *plane); 545 546 /** 547 * drm_plane_index - find the index of a registered plane 548 * @plane: plane to find index for 549 * 550 * Given a registered plane, return the index of that plane within a DRM 551 * device's list of planes. 552 */ 553 static inline unsigned int drm_plane_index(struct drm_plane *plane) 554 { 555 return plane->index; 556 } 557 struct drm_plane * drm_plane_from_index(struct drm_device *dev, int idx); 558 void drm_plane_force_disable(struct drm_plane *plane); 559 560 int drm_mode_plane_set_obj_prop(struct drm_plane *plane, 561 struct drm_property *property, 562 uint64_t value); 563 564 /** 565 * drm_plane_find - find a &drm_plane 566 * @dev: DRM device 567 * @id: plane id 568 * 569 * Returns the plane with @id, NULL if it doesn't exist. Simple wrapper around 570 * drm_mode_object_find(). 571 */ 572 static inline struct drm_plane *drm_plane_find(struct drm_device *dev, 573 uint32_t id) 574 { 575 struct drm_mode_object *mo; 576 mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PLANE); 577 return mo ? obj_to_plane(mo) : NULL; 578 } 579 580 /** 581 * drm_for_each_plane_mask - iterate over planes specified by bitmask 582 * @plane: the loop cursor 583 * @dev: the DRM device 584 * @plane_mask: bitmask of plane indices 585 * 586 * Iterate over all planes specified by bitmask. 587 */ 588 #define drm_for_each_plane_mask(plane, dev, plane_mask) \ 589 list_for_each_entry((plane), &(dev)->mode_config.plane_list, head) \ 590 for_each_if ((plane_mask) & (1 << drm_plane_index(plane))) 591 592 /** 593 * drm_for_each_legacy_plane - iterate over all planes for legacy userspace 594 * @plane: the loop cursor 595 * @dev: the DRM device 596 * 597 * Iterate over all legacy planes of @dev, excluding primary and cursor planes. 598 * This is useful for implementing userspace apis when userspace is not 599 * universal plane aware. See also &enum drm_plane_type. 600 */ 601 #define drm_for_each_legacy_plane(plane, dev) \ 602 list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) \ 603 for_each_if (plane->type == DRM_PLANE_TYPE_OVERLAY) 604 605 /** 606 * drm_for_each_plane - iterate over all planes 607 * @plane: the loop cursor 608 * @dev: the DRM device 609 * 610 * Iterate over all planes of @dev, include primary and cursor planes. 611 */ 612 #define drm_for_each_plane(plane, dev) \ 613 list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) 614 615 616 #endif 617