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