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