drm: Extract drm_property.[hc]
[sfrench/cifs-2.6.git] / include / drm / drm_crtc.h
1 /*
2  * Copyright © 2006 Keith Packard
3  * Copyright © 2007-2008 Dave Airlie
4  * Copyright © 2007-2008 Intel Corporation
5  *   Jesse Barnes <jesse.barnes@intel.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef __DRM_CRTC_H__
26 #define __DRM_CRTC_H__
27
28 #include <linux/i2c.h>
29 #include <linux/spinlock.h>
30 #include <linux/types.h>
31 #include <linux/idr.h>
32 #include <linux/fb.h>
33 #include <linux/hdmi.h>
34 #include <linux/media-bus-format.h>
35 #include <uapi/drm/drm_mode.h>
36 #include <uapi/drm/drm_fourcc.h>
37 #include <drm/drm_modeset_lock.h>
38 #include <drm/drm_rect.h>
39 #include <drm/drm_mode_object.h>
40 #include <drm/drm_framebuffer.h>
41 #include <drm/drm_modes.h>
42 #include <drm/drm_connector.h>
43 #include <drm/drm_encoder.h>
44 #include <drm/drm_property.h>
45
46 struct drm_device;
47 struct drm_mode_set;
48 struct drm_file;
49 struct drm_clip_rect;
50 struct device_node;
51 struct fence;
52 struct edid;
53
54 static inline int64_t U642I64(uint64_t val)
55 {
56         return (int64_t)*((int64_t *)&val);
57 }
58 static inline uint64_t I642U64(int64_t val)
59 {
60         return (uint64_t)*((uint64_t *)&val);
61 }
62
63 /*
64  * Rotation property bits. DRM_ROTATE_<degrees> rotates the image by the
65  * specified amount in degrees in counter clockwise direction. DRM_REFLECT_X and
66  * DRM_REFLECT_Y reflects the image along the specified axis prior to rotation
67  */
68 #define DRM_ROTATE_0    BIT(0)
69 #define DRM_ROTATE_90   BIT(1)
70 #define DRM_ROTATE_180  BIT(2)
71 #define DRM_ROTATE_270  BIT(3)
72 #define DRM_ROTATE_MASK (DRM_ROTATE_0   | DRM_ROTATE_90 | \
73                          DRM_ROTATE_180 | DRM_ROTATE_270)
74 #define DRM_REFLECT_X   BIT(4)
75 #define DRM_REFLECT_Y   BIT(5)
76 #define DRM_REFLECT_MASK (DRM_REFLECT_X | DRM_REFLECT_Y)
77
78 /* data corresponds to displayid vend/prod/serial */
79 struct drm_tile_group {
80         struct kref refcount;
81         struct drm_device *dev;
82         int id;
83         u8 group_data[8];
84 };
85
86 struct drm_crtc;
87 struct drm_encoder;
88 struct drm_pending_vblank_event;
89 struct drm_plane;
90 struct drm_bridge;
91 struct drm_atomic_state;
92
93 struct drm_crtc_helper_funcs;
94 struct drm_encoder_helper_funcs;
95 struct drm_plane_helper_funcs;
96
97 /**
98  * struct drm_crtc_state - mutable CRTC state
99  * @crtc: backpointer to the CRTC
100  * @enable: whether the CRTC should be enabled, gates all other state
101  * @active: whether the CRTC is actively displaying (used for DPMS)
102  * @planes_changed: planes on this crtc are updated
103  * @mode_changed: crtc_state->mode or crtc_state->enable has been changed
104  * @active_changed: crtc_state->active has been toggled.
105  * @connectors_changed: connectors to this crtc have been updated
106  * @zpos_changed: zpos values of planes on this crtc have been updated
107  * @color_mgmt_changed: color management properties have changed (degamma or
108  *      gamma LUT or CSC matrix)
109  * @plane_mask: bitmask of (1 << drm_plane_index(plane)) of attached planes
110  * @connector_mask: bitmask of (1 << drm_connector_index(connector)) of attached connectors
111  * @encoder_mask: bitmask of (1 << drm_encoder_index(encoder)) of attached encoders
112  * @last_vblank_count: for helpers and drivers to capture the vblank of the
113  *      update to ensure framebuffer cleanup isn't done too early
114  * @adjusted_mode: for use by helpers and drivers to compute adjusted mode timings
115  * @mode: current mode timings
116  * @mode_blob: &drm_property_blob for @mode
117  * @degamma_lut: Lookup table for converting framebuffer pixel data
118  *      before apply the conversion matrix
119  * @ctm: Transformation matrix
120  * @gamma_lut: Lookup table for converting pixel data after the
121  *      conversion matrix
122  * @event: optional pointer to a DRM event to signal upon completion of the
123  *      state update
124  * @state: backpointer to global drm_atomic_state
125  *
126  * Note that the distinction between @enable and @active is rather subtile:
127  * Flipping @active while @enable is set without changing anything else may
128  * never return in a failure from the ->atomic_check callback. Userspace assumes
129  * that a DPMS On will always succeed. In other words: @enable controls resource
130  * assignment, @active controls the actual hardware state.
131  */
132 struct drm_crtc_state {
133         struct drm_crtc *crtc;
134
135         bool enable;
136         bool active;
137
138         /* computed state bits used by helpers and drivers */
139         bool planes_changed : 1;
140         bool mode_changed : 1;
141         bool active_changed : 1;
142         bool connectors_changed : 1;
143         bool zpos_changed : 1;
144         bool color_mgmt_changed : 1;
145
146         /* attached planes bitmask:
147          * WARNING: transitional helpers do not maintain plane_mask so
148          * drivers not converted over to atomic helpers should not rely
149          * on plane_mask being accurate!
150          */
151         u32 plane_mask;
152
153         u32 connector_mask;
154         u32 encoder_mask;
155
156         /* last_vblank_count: for vblank waits before cleanup */
157         u32 last_vblank_count;
158
159         /* adjusted_mode: for use by helpers and drivers */
160         struct drm_display_mode adjusted_mode;
161
162         struct drm_display_mode mode;
163
164         /* blob property to expose current mode to atomic userspace */
165         struct drm_property_blob *mode_blob;
166
167         /* blob property to expose color management to userspace */
168         struct drm_property_blob *degamma_lut;
169         struct drm_property_blob *ctm;
170         struct drm_property_blob *gamma_lut;
171
172         struct drm_pending_vblank_event *event;
173
174         struct drm_atomic_state *state;
175 };
176
177 /**
178  * struct drm_crtc_funcs - control CRTCs for a given device
179  *
180  * The drm_crtc_funcs structure is the central CRTC management structure
181  * in the DRM.  Each CRTC controls one or more connectors (note that the name
182  * CRTC is simply historical, a CRTC may control LVDS, VGA, DVI, TV out, etc.
183  * connectors, not just CRTs).
184  *
185  * Each driver is responsible for filling out this structure at startup time,
186  * in addition to providing other modesetting features, like i2c and DDC
187  * bus accessors.
188  */
189 struct drm_crtc_funcs {
190         /**
191          * @reset:
192          *
193          * Reset CRTC hardware and software state to off. This function isn't
194          * called by the core directly, only through drm_mode_config_reset().
195          * It's not a helper hook only for historical reasons.
196          *
197          * Atomic drivers can use drm_atomic_helper_crtc_reset() to reset
198          * atomic state using this hook.
199          */
200         void (*reset)(struct drm_crtc *crtc);
201
202         /**
203          * @cursor_set:
204          *
205          * Update the cursor image. The cursor position is relative to the CRTC
206          * and can be partially or fully outside of the visible area.
207          *
208          * Note that contrary to all other KMS functions the legacy cursor entry
209          * points don't take a framebuffer object, but instead take directly a
210          * raw buffer object id from the driver's buffer manager (which is
211          * either GEM or TTM for current drivers).
212          *
213          * This entry point is deprecated, drivers should instead implement
214          * universal plane support and register a proper cursor plane using
215          * drm_crtc_init_with_planes().
216          *
217          * This callback is optional
218          *
219          * RETURNS:
220          *
221          * 0 on success or a negative error code on failure.
222          */
223         int (*cursor_set)(struct drm_crtc *crtc, struct drm_file *file_priv,
224                           uint32_t handle, uint32_t width, uint32_t height);
225
226         /**
227          * @cursor_set2:
228          *
229          * Update the cursor image, including hotspot information. The hotspot
230          * must not affect the cursor position in CRTC coordinates, but is only
231          * meant as a hint for virtualized display hardware to coordinate the
232          * guests and hosts cursor position. The cursor hotspot is relative to
233          * the cursor image. Otherwise this works exactly like @cursor_set.
234          *
235          * This entry point is deprecated, drivers should instead implement
236          * universal plane support and register a proper cursor plane using
237          * drm_crtc_init_with_planes().
238          *
239          * This callback is optional.
240          *
241          * RETURNS:
242          *
243          * 0 on success or a negative error code on failure.
244          */
245         int (*cursor_set2)(struct drm_crtc *crtc, struct drm_file *file_priv,
246                            uint32_t handle, uint32_t width, uint32_t height,
247                            int32_t hot_x, int32_t hot_y);
248
249         /**
250          * @cursor_move:
251          *
252          * Update the cursor position. The cursor does not need to be visible
253          * when this hook is called.
254          *
255          * This entry point is deprecated, drivers should instead implement
256          * universal plane support and register a proper cursor plane using
257          * drm_crtc_init_with_planes().
258          *
259          * This callback is optional.
260          *
261          * RETURNS:
262          *
263          * 0 on success or a negative error code on failure.
264          */
265         int (*cursor_move)(struct drm_crtc *crtc, int x, int y);
266
267         /**
268          * @gamma_set:
269          *
270          * Set gamma on the CRTC.
271          *
272          * This callback is optional.
273          *
274          * NOTE:
275          *
276          * Drivers that support gamma tables and also fbdev emulation through
277          * the provided helper library need to take care to fill out the gamma
278          * hooks for both. Currently there's a bit an unfortunate duplication
279          * going on, which should eventually be unified to just one set of
280          * hooks.
281          */
282         int (*gamma_set)(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
283                          uint32_t size);
284
285         /**
286          * @destroy:
287          *
288          * Clean up plane resources. This is only called at driver unload time
289          * through drm_mode_config_cleanup() since a CRTC cannot be hotplugged
290          * in DRM.
291          */
292         void (*destroy)(struct drm_crtc *crtc);
293
294         /**
295          * @set_config:
296          *
297          * This is the main legacy entry point to change the modeset state on a
298          * CRTC. All the details of the desired configuration are passed in a
299          * struct &drm_mode_set - see there for details.
300          *
301          * Drivers implementing atomic modeset should use
302          * drm_atomic_helper_set_config() to implement this hook.
303          *
304          * RETURNS:
305          *
306          * 0 on success or a negative error code on failure.
307          */
308         int (*set_config)(struct drm_mode_set *set);
309
310         /**
311          * @page_flip:
312          *
313          * Legacy entry point to schedule a flip to the given framebuffer.
314          *
315          * Page flipping is a synchronization mechanism that replaces the frame
316          * buffer being scanned out by the CRTC with a new frame buffer during
317          * vertical blanking, avoiding tearing (except when requested otherwise
318          * through the DRM_MODE_PAGE_FLIP_ASYNC flag). When an application
319          * requests a page flip the DRM core verifies that the new frame buffer
320          * is large enough to be scanned out by the CRTC in the currently
321          * configured mode and then calls the CRTC ->page_flip() operation with a
322          * pointer to the new frame buffer.
323          *
324          * The driver must wait for any pending rendering to the new framebuffer
325          * to complete before executing the flip. It should also wait for any
326          * pending rendering from other drivers if the underlying buffer is a
327          * shared dma-buf.
328          *
329          * An application can request to be notified when the page flip has
330          * completed. The drm core will supply a struct &drm_event in the event
331          * parameter in this case. This can be handled by the
332          * drm_crtc_send_vblank_event() function, which the driver should call on
333          * the provided event upon completion of the flip. Note that if
334          * the driver supports vblank signalling and timestamping the vblank
335          * counters and timestamps must agree with the ones returned from page
336          * flip events. With the current vblank helper infrastructure this can
337          * be achieved by holding a vblank reference while the page flip is
338          * pending, acquired through drm_crtc_vblank_get() and released with
339          * drm_crtc_vblank_put(). Drivers are free to implement their own vblank
340          * counter and timestamp tracking though, e.g. if they have accurate
341          * timestamp registers in hardware.
342          *
343          * This callback is optional.
344          *
345          * NOTE:
346          *
347          * Very early versions of the KMS ABI mandated that the driver must
348          * block (but not reject) any rendering to the old framebuffer until the
349          * flip operation has completed and the old framebuffer is no longer
350          * visible. This requirement has been lifted, and userspace is instead
351          * expected to request delivery of an event and wait with recycling old
352          * buffers until such has been received.
353          *
354          * RETURNS:
355          *
356          * 0 on success or a negative error code on failure. Note that if a
357          * ->page_flip() operation is already pending the callback should return
358          * -EBUSY. Pageflips on a disabled CRTC (either by setting a NULL mode
359          * or just runtime disabled through DPMS respectively the new atomic
360          * "ACTIVE" state) should result in an -EINVAL error code. Note that
361          * drm_atomic_helper_page_flip() checks this already for atomic drivers.
362          */
363         int (*page_flip)(struct drm_crtc *crtc,
364                          struct drm_framebuffer *fb,
365                          struct drm_pending_vblank_event *event,
366                          uint32_t flags);
367
368         /**
369          * @set_property:
370          *
371          * This is the legacy entry point to update a property attached to the
372          * CRTC.
373          *
374          * Drivers implementing atomic modeset should use
375          * drm_atomic_helper_crtc_set_property() to implement this hook.
376          *
377          * This callback is optional if the driver does not support any legacy
378          * driver-private properties.
379          *
380          * RETURNS:
381          *
382          * 0 on success or a negative error code on failure.
383          */
384         int (*set_property)(struct drm_crtc *crtc,
385                             struct drm_property *property, uint64_t val);
386
387         /**
388          * @atomic_duplicate_state:
389          *
390          * Duplicate the current atomic state for this CRTC and return it.
391          * The core and helpers gurantee that any atomic state duplicated with
392          * this hook and still owned by the caller (i.e. not transferred to the
393          * driver by calling ->atomic_commit() from struct
394          * &drm_mode_config_funcs) will be cleaned up by calling the
395          * @atomic_destroy_state hook in this structure.
396          *
397          * Atomic drivers which don't subclass struct &drm_crtc should use
398          * drm_atomic_helper_crtc_duplicate_state(). Drivers that subclass the
399          * state structure to extend it with driver-private state should use
400          * __drm_atomic_helper_crtc_duplicate_state() to make sure shared state is
401          * duplicated in a consistent fashion across drivers.
402          *
403          * It is an error to call this hook before crtc->state has been
404          * initialized correctly.
405          *
406          * NOTE:
407          *
408          * If the duplicate state references refcounted resources this hook must
409          * acquire a reference for each of them. The driver must release these
410          * references again in @atomic_destroy_state.
411          *
412          * RETURNS:
413          *
414          * Duplicated atomic state or NULL when the allocation failed.
415          */
416         struct drm_crtc_state *(*atomic_duplicate_state)(struct drm_crtc *crtc);
417
418         /**
419          * @atomic_destroy_state:
420          *
421          * Destroy a state duplicated with @atomic_duplicate_state and release
422          * or unreference all resources it references
423          */
424         void (*atomic_destroy_state)(struct drm_crtc *crtc,
425                                      struct drm_crtc_state *state);
426
427         /**
428          * @atomic_set_property:
429          *
430          * Decode a driver-private property value and store the decoded value
431          * into the passed-in state structure. Since the atomic core decodes all
432          * standardized properties (even for extensions beyond the core set of
433          * properties which might not be implemented by all drivers) this
434          * requires drivers to subclass the state structure.
435          *
436          * Such driver-private properties should really only be implemented for
437          * truly hardware/vendor specific state. Instead it is preferred to
438          * standardize atomic extension and decode the properties used to expose
439          * such an extension in the core.
440          *
441          * Do not call this function directly, use
442          * drm_atomic_crtc_set_property() instead.
443          *
444          * This callback is optional if the driver does not support any
445          * driver-private atomic properties.
446          *
447          * NOTE:
448          *
449          * This function is called in the state assembly phase of atomic
450          * modesets, which can be aborted for any reason (including on
451          * userspace's request to just check whether a configuration would be
452          * possible). Drivers MUST NOT touch any persistent state (hardware or
453          * software) or data structures except the passed in @state parameter.
454          *
455          * Also since userspace controls in which order properties are set this
456          * function must not do any input validation (since the state update is
457          * incomplete and hence likely inconsistent). Instead any such input
458          * validation must be done in the various atomic_check callbacks.
459          *
460          * RETURNS:
461          *
462          * 0 if the property has been found, -EINVAL if the property isn't
463          * implemented by the driver (which should never happen, the core only
464          * asks for properties attached to this CRTC). No other validation is
465          * allowed by the driver. The core already checks that the property
466          * value is within the range (integer, valid enum value, ...) the driver
467          * set when registering the property.
468          */
469         int (*atomic_set_property)(struct drm_crtc *crtc,
470                                    struct drm_crtc_state *state,
471                                    struct drm_property *property,
472                                    uint64_t val);
473         /**
474          * @atomic_get_property:
475          *
476          * Reads out the decoded driver-private property. This is used to
477          * implement the GETCRTC IOCTL.
478          *
479          * Do not call this function directly, use
480          * drm_atomic_crtc_get_property() instead.
481          *
482          * This callback is optional if the driver does not support any
483          * driver-private atomic properties.
484          *
485          * RETURNS:
486          *
487          * 0 on success, -EINVAL if the property isn't implemented by the
488          * driver (which should never happen, the core only asks for
489          * properties attached to this CRTC).
490          */
491         int (*atomic_get_property)(struct drm_crtc *crtc,
492                                    const struct drm_crtc_state *state,
493                                    struct drm_property *property,
494                                    uint64_t *val);
495
496         /**
497          * @late_register:
498          *
499          * This optional hook can be used to register additional userspace
500          * interfaces attached to the crtc like debugfs interfaces.
501          * It is called late in the driver load sequence from drm_dev_register().
502          * Everything added from this callback should be unregistered in
503          * the early_unregister callback.
504          *
505          * Returns:
506          *
507          * 0 on success, or a negative error code on failure.
508          */
509         int (*late_register)(struct drm_crtc *crtc);
510
511         /**
512          * @early_unregister:
513          *
514          * This optional hook should be used to unregister the additional
515          * userspace interfaces attached to the crtc from
516          * late_unregister(). It is called from drm_dev_unregister(),
517          * early in the driver unload sequence to disable userspace access
518          * before data structures are torndown.
519          */
520         void (*early_unregister)(struct drm_crtc *crtc);
521 };
522
523 /**
524  * struct drm_crtc - central CRTC control structure
525  * @dev: parent DRM device
526  * @port: OF node used by drm_of_find_possible_crtcs()
527  * @head: list management
528  * @name: human readable name, can be overwritten by the driver
529  * @mutex: per-CRTC locking
530  * @base: base KMS object for ID tracking etc.
531  * @primary: primary plane for this CRTC
532  * @cursor: cursor plane for this CRTC
533  * @cursor_x: current x position of the cursor, used for universal cursor planes
534  * @cursor_y: current y position of the cursor, used for universal cursor planes
535  * @enabled: is this CRTC enabled?
536  * @mode: current mode timings
537  * @hwmode: mode timings as programmed to hw regs
538  * @x: x position on screen
539  * @y: y position on screen
540  * @funcs: CRTC control functions
541  * @gamma_size: size of gamma ramp
542  * @gamma_store: gamma ramp values
543  * @helper_private: mid-layer private data
544  * @properties: property tracking for this CRTC
545  *
546  * Each CRTC may have one or more connectors associated with it.  This structure
547  * allows the CRTC to be controlled.
548  */
549 struct drm_crtc {
550         struct drm_device *dev;
551         struct device_node *port;
552         struct list_head head;
553
554         char *name;
555
556         /**
557          * @mutex:
558          *
559          * This provides a read lock for the overall crtc state (mode, dpms
560          * state, ...) and a write lock for everything which can be update
561          * without a full modeset (fb, cursor data, crtc properties ...). Full
562          * modeset also need to grab dev->mode_config.connection_mutex.
563          */
564         struct drm_modeset_lock mutex;
565
566         struct drm_mode_object base;
567
568         /* primary and cursor planes for CRTC */
569         struct drm_plane *primary;
570         struct drm_plane *cursor;
571
572         /**
573          * @index: Position inside the mode_config.list, can be used as an array
574          * index. It is invariant over the lifetime of the CRTC.
575          */
576         unsigned index;
577
578         /* position of cursor plane on crtc */
579         int cursor_x;
580         int cursor_y;
581
582         bool enabled;
583
584         /* Requested mode from modesetting. */
585         struct drm_display_mode mode;
586
587         /* Programmed mode in hw, after adjustments for encoders,
588          * crtc, panel scaling etc. Needed for timestamping etc.
589          */
590         struct drm_display_mode hwmode;
591
592         int x, y;
593         const struct drm_crtc_funcs *funcs;
594
595         /* Legacy FB CRTC gamma size for reporting to userspace */
596         uint32_t gamma_size;
597         uint16_t *gamma_store;
598
599         /* if you are using the helper */
600         const struct drm_crtc_helper_funcs *helper_private;
601
602         struct drm_object_properties properties;
603
604         /**
605          * @state:
606          *
607          * Current atomic state for this CRTC.
608          */
609         struct drm_crtc_state *state;
610
611         /**
612          * @commit_list:
613          *
614          * List of &drm_crtc_commit structures tracking pending commits.
615          * Protected by @commit_lock. This list doesn't hold its own full
616          * reference, but burrows it from the ongoing commit. Commit entries
617          * must be removed from this list once the commit is fully completed,
618          * but before it's correspoding &drm_atomic_state gets destroyed.
619          */
620         struct list_head commit_list;
621
622         /**
623          * @commit_lock:
624          *
625          * Spinlock to protect @commit_list.
626          */
627         spinlock_t commit_lock;
628
629         /**
630          * @acquire_ctx:
631          *
632          * Per-CRTC implicit acquire context used by atomic drivers for legacy
633          * IOCTLs, so that atomic drivers can get at the locking acquire
634          * context.
635          */
636         struct drm_modeset_acquire_ctx *acquire_ctx;
637 };
638
639 /**
640  * struct drm_plane_state - mutable plane state
641  * @plane: backpointer to the plane
642  * @crtc: currently bound CRTC, NULL if disabled
643  * @fb: currently bound framebuffer
644  * @fence: optional fence to wait for before scanning out @fb
645  * @crtc_x: left position of visible portion of plane on crtc
646  * @crtc_y: upper position of visible portion of plane on crtc
647  * @crtc_w: width of visible portion of plane on crtc
648  * @crtc_h: height of visible portion of plane on crtc
649  * @src_x: left position of visible portion of plane within
650  *      plane (in 16.16)
651  * @src_y: upper position of visible portion of plane within
652  *      plane (in 16.16)
653  * @src_w: width of visible portion of plane (in 16.16)
654  * @src_h: height of visible portion of plane (in 16.16)
655  * @rotation: rotation of the plane
656  * @zpos: priority of the given plane on crtc (optional)
657  * @normalized_zpos: normalized value of zpos: unique, range from 0 to N-1
658  *      where N is the number of active planes for given crtc
659  * @src: clipped source coordinates of the plane (in 16.16)
660  * @dst: clipped destination coordinates of the plane
661  * @visible: visibility of the plane
662  * @state: backpointer to global drm_atomic_state
663  */
664 struct drm_plane_state {
665         struct drm_plane *plane;
666
667         struct drm_crtc *crtc;   /* do not write directly, use drm_atomic_set_crtc_for_plane() */
668         struct drm_framebuffer *fb;  /* do not write directly, use drm_atomic_set_fb_for_plane() */
669         struct fence *fence;
670
671         /* Signed dest location allows it to be partially off screen */
672         int32_t crtc_x, crtc_y;
673         uint32_t crtc_w, crtc_h;
674
675         /* Source values are 16.16 fixed point */
676         uint32_t src_x, src_y;
677         uint32_t src_h, src_w;
678
679         /* Plane rotation */
680         unsigned int rotation;
681
682         /* Plane zpos */
683         unsigned int zpos;
684         unsigned int normalized_zpos;
685
686         /* Clipped coordinates */
687         struct drm_rect src, dst;
688
689         /*
690          * Is the plane actually visible? Can be false even
691          * if fb!=NULL and crtc!=NULL, due to clipping.
692          */
693         bool visible;
694
695         struct drm_atomic_state *state;
696 };
697
698
699 /**
700  * struct drm_plane_funcs - driver plane control functions
701  */
702 struct drm_plane_funcs {
703         /**
704          * @update_plane:
705          *
706          * This is the legacy entry point to enable and configure the plane for
707          * the given CRTC and framebuffer. It is never called to disable the
708          * plane, i.e. the passed-in crtc and fb paramters are never NULL.
709          *
710          * The source rectangle in frame buffer memory coordinates is given by
711          * the src_x, src_y, src_w and src_h parameters (as 16.16 fixed point
712          * values). Devices that don't support subpixel plane coordinates can
713          * ignore the fractional part.
714          *
715          * The destination rectangle in CRTC coordinates is given by the
716          * crtc_x, crtc_y, crtc_w and crtc_h parameters (as integer values).
717          * Devices scale the source rectangle to the destination rectangle. If
718          * scaling is not supported, and the source rectangle size doesn't match
719          * the destination rectangle size, the driver must return a
720          * -<errorname>EINVAL</errorname> error.
721          *
722          * Drivers implementing atomic modeset should use
723          * drm_atomic_helper_update_plane() to implement this hook.
724          *
725          * RETURNS:
726          *
727          * 0 on success or a negative error code on failure.
728          */
729         int (*update_plane)(struct drm_plane *plane,
730                             struct drm_crtc *crtc, struct drm_framebuffer *fb,
731                             int crtc_x, int crtc_y,
732                             unsigned int crtc_w, unsigned int crtc_h,
733                             uint32_t src_x, uint32_t src_y,
734                             uint32_t src_w, uint32_t src_h);
735
736         /**
737          * @disable_plane:
738          *
739          * This is the legacy entry point to disable the plane. The DRM core
740          * calls this method in response to a DRM_IOCTL_MODE_SETPLANE IOCTL call
741          * with the frame buffer ID set to 0.  Disabled planes must not be
742          * processed by the CRTC.
743          *
744          * Drivers implementing atomic modeset should use
745          * drm_atomic_helper_disable_plane() to implement this hook.
746          *
747          * RETURNS:
748          *
749          * 0 on success or a negative error code on failure.
750          */
751         int (*disable_plane)(struct drm_plane *plane);
752
753         /**
754          * @destroy:
755          *
756          * Clean up plane resources. This is only called at driver unload time
757          * through drm_mode_config_cleanup() since a plane cannot be hotplugged
758          * in DRM.
759          */
760         void (*destroy)(struct drm_plane *plane);
761
762         /**
763          * @reset:
764          *
765          * Reset plane hardware and software state to off. This function isn't
766          * called by the core directly, only through drm_mode_config_reset().
767          * It's not a helper hook only for historical reasons.
768          *
769          * Atomic drivers can use drm_atomic_helper_plane_reset() to reset
770          * atomic state using this hook.
771          */
772         void (*reset)(struct drm_plane *plane);
773
774         /**
775          * @set_property:
776          *
777          * This is the legacy entry point to update a property attached to the
778          * plane.
779          *
780          * Drivers implementing atomic modeset should use
781          * drm_atomic_helper_plane_set_property() to implement this hook.
782          *
783          * This callback is optional if the driver does not support any legacy
784          * driver-private properties.
785          *
786          * RETURNS:
787          *
788          * 0 on success or a negative error code on failure.
789          */
790         int (*set_property)(struct drm_plane *plane,
791                             struct drm_property *property, uint64_t val);
792
793         /**
794          * @atomic_duplicate_state:
795          *
796          * Duplicate the current atomic state for this plane and return it.
797          * The core and helpers gurantee that any atomic state duplicated with
798          * this hook and still owned by the caller (i.e. not transferred to the
799          * driver by calling ->atomic_commit() from struct
800          * &drm_mode_config_funcs) will be cleaned up by calling the
801          * @atomic_destroy_state hook in this structure.
802          *
803          * Atomic drivers which don't subclass struct &drm_plane_state should use
804          * drm_atomic_helper_plane_duplicate_state(). Drivers that subclass the
805          * state structure to extend it with driver-private state should use
806          * __drm_atomic_helper_plane_duplicate_state() to make sure shared state is
807          * duplicated in a consistent fashion across drivers.
808          *
809          * It is an error to call this hook before plane->state has been
810          * initialized correctly.
811          *
812          * NOTE:
813          *
814          * If the duplicate state references refcounted resources this hook must
815          * acquire a reference for each of them. The driver must release these
816          * references again in @atomic_destroy_state.
817          *
818          * RETURNS:
819          *
820          * Duplicated atomic state or NULL when the allocation failed.
821          */
822         struct drm_plane_state *(*atomic_duplicate_state)(struct drm_plane *plane);
823
824         /**
825          * @atomic_destroy_state:
826          *
827          * Destroy a state duplicated with @atomic_duplicate_state and release
828          * or unreference all resources it references
829          */
830         void (*atomic_destroy_state)(struct drm_plane *plane,
831                                      struct drm_plane_state *state);
832
833         /**
834          * @atomic_set_property:
835          *
836          * Decode a driver-private property value and store the decoded value
837          * into the passed-in state structure. Since the atomic core decodes all
838          * standardized properties (even for extensions beyond the core set of
839          * properties which might not be implemented by all drivers) this
840          * requires drivers to subclass the state structure.
841          *
842          * Such driver-private properties should really only be implemented for
843          * truly hardware/vendor specific state. Instead it is preferred to
844          * standardize atomic extension and decode the properties used to expose
845          * such an extension in the core.
846          *
847          * Do not call this function directly, use
848          * drm_atomic_plane_set_property() instead.
849          *
850          * This callback is optional if the driver does not support any
851          * driver-private atomic properties.
852          *
853          * NOTE:
854          *
855          * This function is called in the state assembly phase of atomic
856          * modesets, which can be aborted for any reason (including on
857          * userspace's request to just check whether a configuration would be
858          * possible). Drivers MUST NOT touch any persistent state (hardware or
859          * software) or data structures except the passed in @state parameter.
860          *
861          * Also since userspace controls in which order properties are set this
862          * function must not do any input validation (since the state update is
863          * incomplete and hence likely inconsistent). Instead any such input
864          * validation must be done in the various atomic_check callbacks.
865          *
866          * RETURNS:
867          *
868          * 0 if the property has been found, -EINVAL if the property isn't
869          * implemented by the driver (which shouldn't ever happen, the core only
870          * asks for properties attached to this plane). No other validation is
871          * allowed by the driver. The core already checks that the property
872          * value is within the range (integer, valid enum value, ...) the driver
873          * set when registering the property.
874          */
875         int (*atomic_set_property)(struct drm_plane *plane,
876                                    struct drm_plane_state *state,
877                                    struct drm_property *property,
878                                    uint64_t val);
879
880         /**
881          * @atomic_get_property:
882          *
883          * Reads out the decoded driver-private property. This is used to
884          * implement the GETPLANE IOCTL.
885          *
886          * Do not call this function directly, use
887          * drm_atomic_plane_get_property() instead.
888          *
889          * This callback is optional if the driver does not support any
890          * driver-private atomic properties.
891          *
892          * RETURNS:
893          *
894          * 0 on success, -EINVAL if the property isn't implemented by the
895          * driver (which should never happen, the core only asks for
896          * properties attached to this plane).
897          */
898         int (*atomic_get_property)(struct drm_plane *plane,
899                                    const struct drm_plane_state *state,
900                                    struct drm_property *property,
901                                    uint64_t *val);
902         /**
903          * @late_register:
904          *
905          * This optional hook can be used to register additional userspace
906          * interfaces attached to the plane like debugfs interfaces.
907          * It is called late in the driver load sequence from drm_dev_register().
908          * Everything added from this callback should be unregistered in
909          * the early_unregister callback.
910          *
911          * Returns:
912          *
913          * 0 on success, or a negative error code on failure.
914          */
915         int (*late_register)(struct drm_plane *plane);
916
917         /**
918          * @early_unregister:
919          *
920          * This optional hook should be used to unregister the additional
921          * userspace interfaces attached to the plane from
922          * late_unregister(). It is called from drm_dev_unregister(),
923          * early in the driver unload sequence to disable userspace access
924          * before data structures are torndown.
925          */
926         void (*early_unregister)(struct drm_plane *plane);
927 };
928
929 enum drm_plane_type {
930         DRM_PLANE_TYPE_OVERLAY,
931         DRM_PLANE_TYPE_PRIMARY,
932         DRM_PLANE_TYPE_CURSOR,
933 };
934
935
936 /**
937  * struct drm_plane - central DRM plane control structure
938  * @dev: DRM device this plane belongs to
939  * @head: for list management
940  * @name: human readable name, can be overwritten by the driver
941  * @base: base mode object
942  * @possible_crtcs: pipes this plane can be bound to
943  * @format_types: array of formats supported by this plane
944  * @format_count: number of formats supported
945  * @format_default: driver hasn't supplied supported formats for the plane
946  * @crtc: currently bound CRTC
947  * @fb: currently bound fb
948  * @old_fb: Temporary tracking of the old fb while a modeset is ongoing. Used by
949  *      drm_mode_set_config_internal() to implement correct refcounting.
950  * @funcs: helper functions
951  * @properties: property tracking for this plane
952  * @type: type of plane (overlay, primary, cursor)
953  * @state: current atomic state for this plane
954  * @zpos_property: zpos property for this plane
955  * @helper_private: mid-layer private data
956  */
957 struct drm_plane {
958         struct drm_device *dev;
959         struct list_head head;
960
961         char *name;
962
963         /**
964          * @mutex:
965          *
966          * Protects modeset plane state, together with the mutex of &drm_crtc
967          * this plane is linked to (when active, getting actived or getting
968          * disabled).
969          */
970         struct drm_modeset_lock mutex;
971
972         struct drm_mode_object base;
973
974         uint32_t possible_crtcs;
975         uint32_t *format_types;
976         unsigned int format_count;
977         bool format_default;
978
979         struct drm_crtc *crtc;
980         struct drm_framebuffer *fb;
981
982         struct drm_framebuffer *old_fb;
983
984         const struct drm_plane_funcs *funcs;
985
986         struct drm_object_properties properties;
987
988         enum drm_plane_type type;
989
990         /**
991          * @index: Position inside the mode_config.list, can be used as an array
992          * index. It is invariant over the lifetime of the plane.
993          */
994         unsigned index;
995
996         const struct drm_plane_helper_funcs *helper_private;
997
998         struct drm_plane_state *state;
999
1000         struct drm_property *zpos_property;
1001 };
1002
1003 /**
1004  * struct drm_bridge_funcs - drm_bridge control functions
1005  */
1006 struct drm_bridge_funcs {
1007         /**
1008          * @attach:
1009          *
1010          * This callback is invoked whenever our bridge is being attached to a
1011          * &drm_encoder.
1012          *
1013          * The attach callback is optional.
1014          *
1015          * RETURNS:
1016          *
1017          * Zero on success, error code on failure.
1018          */
1019         int (*attach)(struct drm_bridge *bridge);
1020
1021         /**
1022          * @detach:
1023          *
1024          * This callback is invoked whenever our bridge is being detached from a
1025          * &drm_encoder.
1026          *
1027          * The detach callback is optional.
1028          */
1029         void (*detach)(struct drm_bridge *bridge);
1030
1031         /**
1032          * @mode_fixup:
1033          *
1034          * This callback is used to validate and adjust a mode. The paramater
1035          * mode is the display mode that should be fed to the next element in
1036          * the display chain, either the final &drm_connector or the next
1037          * &drm_bridge. The parameter adjusted_mode is the input mode the bridge
1038          * requires. It can be modified by this callback and does not need to
1039          * match mode.
1040          *
1041          * This is the only hook that allows a bridge to reject a modeset. If
1042          * this function passes all other callbacks must succeed for this
1043          * configuration.
1044          *
1045          * The mode_fixup callback is optional.
1046          *
1047          * NOTE:
1048          *
1049          * This function is called in the check phase of atomic modesets, which
1050          * can be aborted for any reason (including on userspace's request to
1051          * just check whether a configuration would be possible). Drivers MUST
1052          * NOT touch any persistent state (hardware or software) or data
1053          * structures except the passed in @state parameter.
1054          *
1055          * RETURNS:
1056          *
1057          * True if an acceptable configuration is possible, false if the modeset
1058          * operation should be rejected.
1059          */
1060         bool (*mode_fixup)(struct drm_bridge *bridge,
1061                            const struct drm_display_mode *mode,
1062                            struct drm_display_mode *adjusted_mode);
1063         /**
1064          * @disable:
1065          *
1066          * This callback should disable the bridge. It is called right before
1067          * the preceding element in the display pipe is disabled. If the
1068          * preceding element is a bridge this means it's called before that
1069          * bridge's ->disable() function. If the preceding element is a
1070          * &drm_encoder it's called right before the encoder's ->disable(),
1071          * ->prepare() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1072          *
1073          * The bridge can assume that the display pipe (i.e. clocks and timing
1074          * signals) feeding it is still running when this callback is called.
1075          *
1076          * The disable callback is optional.
1077          */
1078         void (*disable)(struct drm_bridge *bridge);
1079
1080         /**
1081          * @post_disable:
1082          *
1083          * This callback should disable the bridge. It is called right after
1084          * the preceding element in the display pipe is disabled. If the
1085          * preceding element is a bridge this means it's called after that
1086          * bridge's ->post_disable() function. If the preceding element is a
1087          * &drm_encoder it's called right after the encoder's ->disable(),
1088          * ->prepare() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1089          *
1090          * The bridge must assume that the display pipe (i.e. clocks and timing
1091          * singals) feeding it is no longer running when this callback is
1092          * called.
1093          *
1094          * The post_disable callback is optional.
1095          */
1096         void (*post_disable)(struct drm_bridge *bridge);
1097
1098         /**
1099          * @mode_set:
1100          *
1101          * This callback should set the given mode on the bridge. It is called
1102          * after the ->mode_set() callback for the preceding element in the
1103          * display pipeline has been called already. The display pipe (i.e.
1104          * clocks and timing signals) is off when this function is called.
1105          */
1106         void (*mode_set)(struct drm_bridge *bridge,
1107                          struct drm_display_mode *mode,
1108                          struct drm_display_mode *adjusted_mode);
1109         /**
1110          * @pre_enable:
1111          *
1112          * This callback should enable the bridge. It is called right before
1113          * the preceding element in the display pipe is enabled. If the
1114          * preceding element is a bridge this means it's called before that
1115          * bridge's ->pre_enable() function. If the preceding element is a
1116          * &drm_encoder it's called right before the encoder's ->enable(),
1117          * ->commit() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1118          *
1119          * The display pipe (i.e. clocks and timing signals) feeding this bridge
1120          * will not yet be running when this callback is called. The bridge must
1121          * not enable the display link feeding the next bridge in the chain (if
1122          * there is one) when this callback is called.
1123          *
1124          * The pre_enable callback is optional.
1125          */
1126         void (*pre_enable)(struct drm_bridge *bridge);
1127
1128         /**
1129          * @enable:
1130          *
1131          * This callback should enable the bridge. It is called right after
1132          * the preceding element in the display pipe is enabled. If the
1133          * preceding element is a bridge this means it's called after that
1134          * bridge's ->enable() function. If the preceding element is a
1135          * &drm_encoder it's called right after the encoder's ->enable(),
1136          * ->commit() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1137          *
1138          * The bridge can assume that the display pipe (i.e. clocks and timing
1139          * signals) feeding it is running when this callback is called. This
1140          * callback must enable the display link feeding the next bridge in the
1141          * chain if there is one.
1142          *
1143          * The enable callback is optional.
1144          */
1145         void (*enable)(struct drm_bridge *bridge);
1146 };
1147
1148 /**
1149  * struct drm_bridge - central DRM bridge control structure
1150  * @dev: DRM device this bridge belongs to
1151  * @encoder: encoder to which this bridge is connected
1152  * @next: the next bridge in the encoder chain
1153  * @of_node: device node pointer to the bridge
1154  * @list: to keep track of all added bridges
1155  * @funcs: control functions
1156  * @driver_private: pointer to the bridge driver's internal context
1157  */
1158 struct drm_bridge {
1159         struct drm_device *dev;
1160         struct drm_encoder *encoder;
1161         struct drm_bridge *next;
1162 #ifdef CONFIG_OF
1163         struct device_node *of_node;
1164 #endif
1165         struct list_head list;
1166
1167         const struct drm_bridge_funcs *funcs;
1168         void *driver_private;
1169 };
1170
1171 /**
1172  * struct drm_crtc_commit - track modeset commits on a CRTC
1173  *
1174  * This structure is used to track pending modeset changes and atomic commit on
1175  * a per-CRTC basis. Since updating the list should never block this structure
1176  * is reference counted to allow waiters to safely wait on an event to complete,
1177  * without holding any locks.
1178  *
1179  * It has 3 different events in total to allow a fine-grained synchronization
1180  * between outstanding updates::
1181  *
1182  *      atomic commit thread                    hardware
1183  *
1184  *      write new state into hardware   ---->   ...
1185  *      signal hw_done
1186  *                                              switch to new state on next
1187  *      ...                                     v/hblank
1188  *
1189  *      wait for buffers to show up             ...
1190  *
1191  *      ...                                     send completion irq
1192  *                                              irq handler signals flip_done
1193  *      cleanup old buffers
1194  *
1195  *      signal cleanup_done
1196  *
1197  *      wait for flip_done              <----
1198  *      clean up atomic state
1199  *
1200  * The important bit to know is that cleanup_done is the terminal event, but the
1201  * ordering between flip_done and hw_done is entirely up to the specific driver
1202  * and modeset state change.
1203  *
1204  * For an implementation of how to use this look at
1205  * drm_atomic_helper_setup_commit() from the atomic helper library.
1206  */
1207 struct drm_crtc_commit {
1208         /**
1209          * @crtc:
1210          *
1211          * DRM CRTC for this commit.
1212          */
1213         struct drm_crtc *crtc;
1214
1215         /**
1216          * @ref:
1217          *
1218          * Reference count for this structure. Needed to allow blocking on
1219          * completions without the risk of the completion disappearing
1220          * meanwhile.
1221          */
1222         struct kref ref;
1223
1224         /**
1225          * @flip_done:
1226          *
1227          * Will be signaled when the hardware has flipped to the new set of
1228          * buffers. Signals at the same time as when the drm event for this
1229          * commit is sent to userspace, or when an out-fence is singalled. Note
1230          * that for most hardware, in most cases this happens after @hw_done is
1231          * signalled.
1232          */
1233         struct completion flip_done;
1234
1235         /**
1236          * @hw_done:
1237          *
1238          * Will be signalled when all hw register changes for this commit have
1239          * been written out. Especially when disabling a pipe this can be much
1240          * later than than @flip_done, since that can signal already when the
1241          * screen goes black, whereas to fully shut down a pipe more register
1242          * I/O is required.
1243          *
1244          * Note that this does not need to include separately reference-counted
1245          * resources like backing storage buffer pinning, or runtime pm
1246          * management.
1247          */
1248         struct completion hw_done;
1249
1250         /**
1251          * @cleanup_done:
1252          *
1253          * Will be signalled after old buffers have been cleaned up by calling
1254          * drm_atomic_helper_cleanup_planes(). Since this can only happen after
1255          * a vblank wait completed it might be a bit later. This completion is
1256          * useful to throttle updates and avoid hardware updates getting ahead
1257          * of the buffer cleanup too much.
1258          */
1259         struct completion cleanup_done;
1260
1261         /**
1262          * @commit_entry:
1263          *
1264          * Entry on the per-CRTC commit_list. Protected by crtc->commit_lock.
1265          */
1266         struct list_head commit_entry;
1267
1268         /**
1269          * @event:
1270          *
1271          * &drm_pending_vblank_event pointer to clean up private events.
1272          */
1273         struct drm_pending_vblank_event *event;
1274 };
1275
1276 struct __drm_planes_state {
1277         struct drm_plane *ptr;
1278         struct drm_plane_state *state;
1279 };
1280
1281 struct __drm_crtcs_state {
1282         struct drm_crtc *ptr;
1283         struct drm_crtc_state *state;
1284         struct drm_crtc_commit *commit;
1285 };
1286
1287 struct __drm_connnectors_state {
1288         struct drm_connector *ptr;
1289         struct drm_connector_state *state;
1290 };
1291
1292 /**
1293  * struct drm_atomic_state - the global state object for atomic updates
1294  * @dev: parent DRM device
1295  * @allow_modeset: allow full modeset
1296  * @legacy_cursor_update: hint to enforce legacy cursor IOCTL semantics
1297  * @legacy_set_config: Disable conflicting encoders instead of failing with -EINVAL.
1298  * @planes: pointer to array of structures with per-plane data
1299  * @crtcs: pointer to array of CRTC pointers
1300  * @num_connector: size of the @connectors and @connector_states arrays
1301  * @connectors: pointer to array of structures with per-connector data
1302  * @acquire_ctx: acquire context for this atomic modeset state update
1303  */
1304 struct drm_atomic_state {
1305         struct drm_device *dev;
1306         bool allow_modeset : 1;
1307         bool legacy_cursor_update : 1;
1308         bool legacy_set_config : 1;
1309         struct __drm_planes_state *planes;
1310         struct __drm_crtcs_state *crtcs;
1311         int num_connector;
1312         struct __drm_connnectors_state *connectors;
1313
1314         struct drm_modeset_acquire_ctx *acquire_ctx;
1315
1316         /**
1317          * @commit_work:
1318          *
1319          * Work item which can be used by the driver or helpers to execute the
1320          * commit without blocking.
1321          */
1322         struct work_struct commit_work;
1323 };
1324
1325
1326 /**
1327  * struct drm_mode_set - new values for a CRTC config change
1328  * @fb: framebuffer to use for new config
1329  * @crtc: CRTC whose configuration we're about to change
1330  * @mode: mode timings to use
1331  * @x: position of this CRTC relative to @fb
1332  * @y: position of this CRTC relative to @fb
1333  * @connectors: array of connectors to drive with this CRTC if possible
1334  * @num_connectors: size of @connectors array
1335  *
1336  * Represents a single crtc the connectors that it drives with what mode
1337  * and from which framebuffer it scans out from.
1338  *
1339  * This is used to set modes.
1340  */
1341 struct drm_mode_set {
1342         struct drm_framebuffer *fb;
1343         struct drm_crtc *crtc;
1344         struct drm_display_mode *mode;
1345
1346         uint32_t x;
1347         uint32_t y;
1348
1349         struct drm_connector **connectors;
1350         size_t num_connectors;
1351 };
1352
1353 /**
1354  * struct drm_mode_config_funcs - basic driver provided mode setting functions
1355  *
1356  * Some global (i.e. not per-CRTC, connector, etc) mode setting functions that
1357  * involve drivers.
1358  */
1359 struct drm_mode_config_funcs {
1360         /**
1361          * @fb_create:
1362          *
1363          * Create a new framebuffer object. The core does basic checks on the
1364          * requested metadata, but most of that is left to the driver. See
1365          * struct &drm_mode_fb_cmd2 for details.
1366          *
1367          * If the parameters are deemed valid and the backing storage objects in
1368          * the underlying memory manager all exist, then the driver allocates
1369          * a new &drm_framebuffer structure, subclassed to contain
1370          * driver-specific information (like the internal native buffer object
1371          * references). It also needs to fill out all relevant metadata, which
1372          * should be done by calling drm_helper_mode_fill_fb_struct().
1373          *
1374          * The initialization is finalized by calling drm_framebuffer_init(),
1375          * which registers the framebuffer and makes it accessible to other
1376          * threads.
1377          *
1378          * RETURNS:
1379          *
1380          * A new framebuffer with an initial reference count of 1 or a negative
1381          * error code encoded with ERR_PTR().
1382          */
1383         struct drm_framebuffer *(*fb_create)(struct drm_device *dev,
1384                                              struct drm_file *file_priv,
1385                                              const struct drm_mode_fb_cmd2 *mode_cmd);
1386
1387         /**
1388          * @output_poll_changed:
1389          *
1390          * Callback used by helpers to inform the driver of output configuration
1391          * changes.
1392          *
1393          * Drivers implementing fbdev emulation with the helpers can call
1394          * drm_fb_helper_hotplug_changed from this hook to inform the fbdev
1395          * helper of output changes.
1396          *
1397          * FIXME:
1398          *
1399          * Except that there's no vtable for device-level helper callbacks
1400          * there's no reason this is a core function.
1401          */
1402         void (*output_poll_changed)(struct drm_device *dev);
1403
1404         /**
1405          * @atomic_check:
1406          *
1407          * This is the only hook to validate an atomic modeset update. This
1408          * function must reject any modeset and state changes which the hardware
1409          * or driver doesn't support. This includes but is of course not limited
1410          * to:
1411          *
1412          *  - Checking that the modes, framebuffers, scaling and placement
1413          *    requirements and so on are within the limits of the hardware.
1414          *
1415          *  - Checking that any hidden shared resources are not oversubscribed.
1416          *    This can be shared PLLs, shared lanes, overall memory bandwidth,
1417          *    display fifo space (where shared between planes or maybe even
1418          *    CRTCs).
1419          *
1420          *  - Checking that virtualized resources exported to userspace are not
1421          *    oversubscribed. For various reasons it can make sense to expose
1422          *    more planes, crtcs or encoders than which are physically there. One
1423          *    example is dual-pipe operations (which generally should be hidden
1424          *    from userspace if when lockstepped in hardware, exposed otherwise),
1425          *    where a plane might need 1 hardware plane (if it's just on one
1426          *    pipe), 2 hardware planes (when it spans both pipes) or maybe even
1427          *    shared a hardware plane with a 2nd plane (if there's a compatible
1428          *    plane requested on the area handled by the other pipe).
1429          *
1430          *  - Check that any transitional state is possible and that if
1431          *    requested, the update can indeed be done in the vblank period
1432          *    without temporarily disabling some functions.
1433          *
1434          *  - Check any other constraints the driver or hardware might have.
1435          *
1436          *  - This callback also needs to correctly fill out the &drm_crtc_state
1437          *    in this update to make sure that drm_atomic_crtc_needs_modeset()
1438          *    reflects the nature of the possible update and returns true if and
1439          *    only if the update cannot be applied without tearing within one
1440          *    vblank on that CRTC. The core uses that information to reject
1441          *    updates which require a full modeset (i.e. blanking the screen, or
1442          *    at least pausing updates for a substantial amount of time) if
1443          *    userspace has disallowed that in its request.
1444          *
1445          *  - The driver also does not need to repeat basic input validation
1446          *    like done for the corresponding legacy entry points. The core does
1447          *    that before calling this hook.
1448          *
1449          * See the documentation of @atomic_commit for an exhaustive list of
1450          * error conditions which don't have to be checked at the
1451          * ->atomic_check() stage?
1452          *
1453          * See the documentation for struct &drm_atomic_state for how exactly
1454          * an atomic modeset update is described.
1455          *
1456          * Drivers using the atomic helpers can implement this hook using
1457          * drm_atomic_helper_check(), or one of the exported sub-functions of
1458          * it.
1459          *
1460          * RETURNS:
1461          *
1462          * 0 on success or one of the below negative error codes:
1463          *
1464          *  - -EINVAL, if any of the above constraints are violated.
1465          *
1466          *  - -EDEADLK, when returned from an attempt to acquire an additional
1467          *    &drm_modeset_lock through drm_modeset_lock().
1468          *
1469          *  - -ENOMEM, if allocating additional state sub-structures failed due
1470          *    to lack of memory.
1471          *
1472          *  - -EINTR, -EAGAIN or -ERESTARTSYS, if the IOCTL should be restarted.
1473          *    This can either be due to a pending signal, or because the driver
1474          *    needs to completely bail out to recover from an exceptional
1475          *    situation like a GPU hang. From a userspace point all errors are
1476          *    treated equally.
1477          */
1478         int (*atomic_check)(struct drm_device *dev,
1479                             struct drm_atomic_state *state);
1480
1481         /**
1482          * @atomic_commit:
1483          *
1484          * This is the only hook to commit an atomic modeset update. The core
1485          * guarantees that @atomic_check has been called successfully before
1486          * calling this function, and that nothing has been changed in the
1487          * interim.
1488          *
1489          * See the documentation for struct &drm_atomic_state for how exactly
1490          * an atomic modeset update is described.
1491          *
1492          * Drivers using the atomic helpers can implement this hook using
1493          * drm_atomic_helper_commit(), or one of the exported sub-functions of
1494          * it.
1495          *
1496          * Nonblocking commits (as indicated with the nonblock parameter) must
1497          * do any preparatory work which might result in an unsuccessful commit
1498          * in the context of this callback. The only exceptions are hardware
1499          * errors resulting in -EIO. But even in that case the driver must
1500          * ensure that the display pipe is at least running, to avoid
1501          * compositors crashing when pageflips don't work. Anything else,
1502          * specifically committing the update to the hardware, should be done
1503          * without blocking the caller. For updates which do not require a
1504          * modeset this must be guaranteed.
1505          *
1506          * The driver must wait for any pending rendering to the new
1507          * framebuffers to complete before executing the flip. It should also
1508          * wait for any pending rendering from other drivers if the underlying
1509          * buffer is a shared dma-buf. Nonblocking commits must not wait for
1510          * rendering in the context of this callback.
1511          *
1512          * An application can request to be notified when the atomic commit has
1513          * completed. These events are per-CRTC and can be distinguished by the
1514          * CRTC index supplied in &drm_event to userspace.
1515          *
1516          * The drm core will supply a struct &drm_event in the event
1517          * member of each CRTC's &drm_crtc_state structure. This can be handled by the
1518          * drm_crtc_send_vblank_event() function, which the driver should call on
1519          * the provided event upon completion of the atomic commit. Note that if
1520          * the driver supports vblank signalling and timestamping the vblank
1521          * counters and timestamps must agree with the ones returned from page
1522          * flip events. With the current vblank helper infrastructure this can
1523          * be achieved by holding a vblank reference while the page flip is
1524          * pending, acquired through drm_crtc_vblank_get() and released with
1525          * drm_crtc_vblank_put(). Drivers are free to implement their own vblank
1526          * counter and timestamp tracking though, e.g. if they have accurate
1527          * timestamp registers in hardware.
1528          *
1529          * NOTE:
1530          *
1531          * Drivers are not allowed to shut down any display pipe successfully
1532          * enabled through an atomic commit on their own. Doing so can result in
1533          * compositors crashing if a page flip is suddenly rejected because the
1534          * pipe is off.
1535          *
1536          * RETURNS:
1537          *
1538          * 0 on success or one of the below negative error codes:
1539          *
1540          *  - -EBUSY, if a nonblocking updated is requested and there is
1541          *    an earlier updated pending. Drivers are allowed to support a queue
1542          *    of outstanding updates, but currently no driver supports that.
1543          *    Note that drivers must wait for preceding updates to complete if a
1544          *    synchronous update is requested, they are not allowed to fail the
1545          *    commit in that case.
1546          *
1547          *  - -ENOMEM, if the driver failed to allocate memory. Specifically
1548          *    this can happen when trying to pin framebuffers, which must only
1549          *    be done when committing the state.
1550          *
1551          *  - -ENOSPC, as a refinement of the more generic -ENOMEM to indicate
1552          *    that the driver has run out of vram, iommu space or similar GPU
1553          *    address space needed for framebuffer.
1554          *
1555          *  - -EIO, if the hardware completely died.
1556          *
1557          *  - -EINTR, -EAGAIN or -ERESTARTSYS, if the IOCTL should be restarted.
1558          *    This can either be due to a pending signal, or because the driver
1559          *    needs to completely bail out to recover from an exceptional
1560          *    situation like a GPU hang. From a userspace point of view all errors are
1561          *    treated equally.
1562          *
1563          * This list is exhaustive. Specifically this hook is not allowed to
1564          * return -EINVAL (any invalid requests should be caught in
1565          * @atomic_check) or -EDEADLK (this function must not acquire
1566          * additional modeset locks).
1567          */
1568         int (*atomic_commit)(struct drm_device *dev,
1569                              struct drm_atomic_state *state,
1570                              bool nonblock);
1571
1572         /**
1573          * @atomic_state_alloc:
1574          *
1575          * This optional hook can be used by drivers that want to subclass struct
1576          * &drm_atomic_state to be able to track their own driver-private global
1577          * state easily. If this hook is implemented, drivers must also
1578          * implement @atomic_state_clear and @atomic_state_free.
1579          *
1580          * RETURNS:
1581          *
1582          * A new &drm_atomic_state on success or NULL on failure.
1583          */
1584         struct drm_atomic_state *(*atomic_state_alloc)(struct drm_device *dev);
1585
1586         /**
1587          * @atomic_state_clear:
1588          *
1589          * This hook must clear any driver private state duplicated into the
1590          * passed-in &drm_atomic_state. This hook is called when the caller
1591          * encountered a &drm_modeset_lock deadlock and needs to drop all
1592          * already acquired locks as part of the deadlock avoidance dance
1593          * implemented in drm_modeset_lock_backoff().
1594          *
1595          * Any duplicated state must be invalidated since a concurrent atomic
1596          * update might change it, and the drm atomic interfaces always apply
1597          * updates as relative changes to the current state.
1598          *
1599          * Drivers that implement this must call drm_atomic_state_default_clear()
1600          * to clear common state.
1601          */
1602         void (*atomic_state_clear)(struct drm_atomic_state *state);
1603
1604         /**
1605          * @atomic_state_free:
1606          *
1607          * This hook needs driver private resources and the &drm_atomic_state
1608          * itself. Note that the core first calls drm_atomic_state_clear() to
1609          * avoid code duplicate between the clear and free hooks.
1610          *
1611          * Drivers that implement this must call drm_atomic_state_default_free()
1612          * to release common resources.
1613          */
1614         void (*atomic_state_free)(struct drm_atomic_state *state);
1615 };
1616
1617 /**
1618  * struct drm_mode_config - Mode configuration control structure
1619  * @mutex: mutex protecting KMS related lists and structures
1620  * @connection_mutex: ww mutex protecting connector state and routing
1621  * @acquire_ctx: global implicit acquire context used by atomic drivers for
1622  *      legacy IOCTLs
1623  * @fb_lock: mutex to protect fb state and lists
1624  * @num_fb: number of fbs available
1625  * @fb_list: list of framebuffers available
1626  * @num_encoder: number of encoders on this device
1627  * @encoder_list: list of encoder objects
1628  * @num_overlay_plane: number of overlay planes on this device
1629  * @num_total_plane: number of universal (i.e. with primary/curso) planes on this device
1630  * @plane_list: list of plane objects
1631  * @num_crtc: number of CRTCs on this device
1632  * @crtc_list: list of CRTC objects
1633  * @property_list: list of property objects
1634  * @min_width: minimum pixel width on this device
1635  * @min_height: minimum pixel height on this device
1636  * @max_width: maximum pixel width on this device
1637  * @max_height: maximum pixel height on this device
1638  * @funcs: core driver provided mode setting functions
1639  * @fb_base: base address of the framebuffer
1640  * @poll_enabled: track polling support for this device
1641  * @poll_running: track polling status for this device
1642  * @delayed_event: track delayed poll uevent deliver for this device
1643  * @output_poll_work: delayed work for polling in process context
1644  * @property_blob_list: list of all the blob property objects
1645  * @blob_lock: mutex for blob property allocation and management
1646  * @*_property: core property tracking
1647  * @preferred_depth: preferred RBG pixel depth, used by fb helpers
1648  * @prefer_shadow: hint to userspace to prefer shadow-fb rendering
1649  * @cursor_width: hint to userspace for max cursor width
1650  * @cursor_height: hint to userspace for max cursor height
1651  * @helper_private: mid-layer private data
1652  *
1653  * Core mode resource tracking structure.  All CRTC, encoders, and connectors
1654  * enumerated by the driver are added here, as are global properties.  Some
1655  * global restrictions are also here, e.g. dimension restrictions.
1656  */
1657 struct drm_mode_config {
1658         struct mutex mutex; /* protects configuration (mode lists etc.) */
1659         struct drm_modeset_lock connection_mutex; /* protects connector->encoder and encoder->crtc links */
1660         struct drm_modeset_acquire_ctx *acquire_ctx; /* for legacy _lock_all() / _unlock_all() */
1661
1662         /**
1663          * @idr_mutex:
1664          *
1665          * Mutex for KMS ID allocation and management. Protects both @crtc_idr
1666          * and @tile_idr.
1667          */
1668         struct mutex idr_mutex;
1669
1670         /**
1671          * @crtc_idr:
1672          *
1673          * Main KMS ID tracking object. Use this idr for all IDs, fb, crtc,
1674          * connector, modes - just makes life easier to have only one.
1675          */
1676         struct idr crtc_idr;
1677
1678         /**
1679          * @tile_idr:
1680          *
1681          * Use this idr for allocating new IDs for tiled sinks like use in some
1682          * high-res DP MST screens.
1683          */
1684         struct idr tile_idr;
1685
1686         struct mutex fb_lock; /* proctects global and per-file fb lists */
1687         int num_fb;
1688         struct list_head fb_list;
1689
1690         /**
1691          * @num_connector: Number of connectors on this device.
1692          */
1693         int num_connector;
1694         /**
1695          * @connector_ida: ID allocator for connector indices.
1696          */
1697         struct ida connector_ida;
1698         /**
1699          * @connector_list: List of connector objects.
1700          */
1701         struct list_head connector_list;
1702         int num_encoder;
1703         struct list_head encoder_list;
1704
1705         /*
1706          * Track # of overlay planes separately from # of total planes.  By
1707          * default we only advertise overlay planes to userspace; if userspace
1708          * sets the "universal plane" capability bit, we'll go ahead and
1709          * expose all planes.
1710          */
1711         int num_overlay_plane;
1712         int num_total_plane;
1713         struct list_head plane_list;
1714
1715         int num_crtc;
1716         struct list_head crtc_list;
1717
1718         struct list_head property_list;
1719
1720         int min_width, min_height;
1721         int max_width, max_height;
1722         const struct drm_mode_config_funcs *funcs;
1723         resource_size_t fb_base;
1724
1725         /* output poll support */
1726         bool poll_enabled;
1727         bool poll_running;
1728         bool delayed_event;
1729         struct delayed_work output_poll_work;
1730
1731         struct mutex blob_lock;
1732
1733         /* pointers to standard properties */
1734         struct list_head property_blob_list;
1735         /**
1736          * @edid_property: Default connector property to hold the EDID of the
1737          * currently connected sink, if any.
1738          */
1739         struct drm_property *edid_property;
1740         /**
1741          * @dpms_property: Default connector property to control the
1742          * connector's DPMS state.
1743          */
1744         struct drm_property *dpms_property;
1745         /**
1746          * @path_property: Default connector property to hold the DP MST path
1747          * for the port.
1748          */
1749         struct drm_property *path_property;
1750         /**
1751          * @tile_property: Default connector property to store the tile
1752          * position of a tiled screen, for sinks which need to be driven with
1753          * multiple CRTCs.
1754          */
1755         struct drm_property *tile_property;
1756         /**
1757          * @plane_type_property: Default plane property to differentiate
1758          * CURSOR, PRIMARY and OVERLAY legacy uses of planes.
1759          */
1760         struct drm_property *plane_type_property;
1761         /**
1762          * @rotation_property: Optional property for planes or CRTCs to specifiy
1763          * rotation.
1764          */
1765         struct drm_property *rotation_property;
1766         /**
1767          * @prop_src_x: Default atomic plane property for the plane source
1768          * position in the connected &drm_framebuffer.
1769          */
1770         struct drm_property *prop_src_x;
1771         /**
1772          * @prop_src_y: Default atomic plane property for the plane source
1773          * position in the connected &drm_framebuffer.
1774          */
1775         struct drm_property *prop_src_y;
1776         /**
1777          * @prop_src_w: Default atomic plane property for the plane source
1778          * position in the connected &drm_framebuffer.
1779          */
1780         struct drm_property *prop_src_w;
1781         /**
1782          * @prop_src_h: Default atomic plane property for the plane source
1783          * position in the connected &drm_framebuffer.
1784          */
1785         struct drm_property *prop_src_h;
1786         /**
1787          * @prop_crtc_x: Default atomic plane property for the plane destination
1788          * position in the &drm_crtc is is being shown on.
1789          */
1790         struct drm_property *prop_crtc_x;
1791         /**
1792          * @prop_crtc_y: Default atomic plane property for the plane destination
1793          * position in the &drm_crtc is is being shown on.
1794          */
1795         struct drm_property *prop_crtc_y;
1796         /**
1797          * @prop_crtc_w: Default atomic plane property for the plane destination
1798          * position in the &drm_crtc is is being shown on.
1799          */
1800         struct drm_property *prop_crtc_w;
1801         /**
1802          * @prop_crtc_h: Default atomic plane property for the plane destination
1803          * position in the &drm_crtc is is being shown on.
1804          */
1805         struct drm_property *prop_crtc_h;
1806         /**
1807          * @prop_fb_id: Default atomic plane property to specify the
1808          * &drm_framebuffer.
1809          */
1810         struct drm_property *prop_fb_id;
1811         /**
1812          * @prop_crtc_id: Default atomic plane property to specify the
1813          * &drm_crtc.
1814          */
1815         struct drm_property *prop_crtc_id;
1816         /**
1817          * @prop_active: Default atomic CRTC property to control the active
1818          * state, which is the simplified implementation for DPMS in atomic
1819          * drivers.
1820          */
1821         struct drm_property *prop_active;
1822         /**
1823          * @prop_mode_id: Default atomic CRTC property to set the mode for a
1824          * CRTC. A 0 mode implies that the CRTC is entirely disabled - all
1825          * connectors must be of and active must be set to disabled, too.
1826          */
1827         struct drm_property *prop_mode_id;
1828
1829         /**
1830          * @dvi_i_subconnector_property: Optional DVI-I property to
1831          * differentiate between analog or digital mode.
1832          */
1833         struct drm_property *dvi_i_subconnector_property;
1834         /**
1835          * @dvi_i_select_subconnector_property: Optional DVI-I property to
1836          * select between analog or digital mode.
1837          */
1838         struct drm_property *dvi_i_select_subconnector_property;
1839
1840         /**
1841          * @tv_subconnector_property: Optional TV property to differentiate
1842          * between different TV connector types.
1843          */
1844         struct drm_property *tv_subconnector_property;
1845         /**
1846          * @tv_select_subconnector_property: Optional TV property to select
1847          * between different TV connector types.
1848          */
1849         struct drm_property *tv_select_subconnector_property;
1850         /**
1851          * @tv_mode_property: Optional TV property to select
1852          * the output TV mode.
1853          */
1854         struct drm_property *tv_mode_property;
1855         /**
1856          * @tv_left_margin_property: Optional TV property to set the left
1857          * margin.
1858          */
1859         struct drm_property *tv_left_margin_property;
1860         /**
1861          * @tv_right_margin_property: Optional TV property to set the right
1862          * margin.
1863          */
1864         struct drm_property *tv_right_margin_property;
1865         /**
1866          * @tv_top_margin_property: Optional TV property to set the right
1867          * margin.
1868          */
1869         struct drm_property *tv_top_margin_property;
1870         /**
1871          * @tv_bottom_margin_property: Optional TV property to set the right
1872          * margin.
1873          */
1874         struct drm_property *tv_bottom_margin_property;
1875         /**
1876          * @tv_brightness_property: Optional TV property to set the
1877          * brightness.
1878          */
1879         struct drm_property *tv_brightness_property;
1880         /**
1881          * @tv_contrast_property: Optional TV property to set the
1882          * contrast.
1883          */
1884         struct drm_property *tv_contrast_property;
1885         /**
1886          * @tv_flicker_reduction_property: Optional TV property to control the
1887          * flicker reduction mode.
1888          */
1889         struct drm_property *tv_flicker_reduction_property;
1890         /**
1891          * @tv_overscan_property: Optional TV property to control the overscan
1892          * setting.
1893          */
1894         struct drm_property *tv_overscan_property;
1895         /**
1896          * @tv_saturation_property: Optional TV property to set the
1897          * saturation.
1898          */
1899         struct drm_property *tv_saturation_property;
1900         /**
1901          * @tv_hue_property: Optional TV property to set the hue.
1902          */
1903         struct drm_property *tv_hue_property;
1904
1905         /**
1906          * @scaling_mode_property: Optional connector property to control the
1907          * upscaling, mostly used for built-in panels.
1908          */
1909         struct drm_property *scaling_mode_property;
1910         /**
1911          * @aspect_ratio_property: Optional connector property to control the
1912          * HDMI infoframe aspect ratio setting.
1913          */
1914         struct drm_property *aspect_ratio_property;
1915         /**
1916          * @degamma_lut_property: Optional CRTC property to set the LUT used to
1917          * convert the framebuffer's colors to linear gamma.
1918          */
1919         struct drm_property *degamma_lut_property;
1920         /**
1921          * @degamma_lut_size_property: Optional CRTC property for the size of
1922          * the degamma LUT as supported by the driver (read-only).
1923          */
1924         struct drm_property *degamma_lut_size_property;
1925         /**
1926          * @ctm_property: Optional CRTC property to set the
1927          * matrix used to convert colors after the lookup in the
1928          * degamma LUT.
1929          */
1930         struct drm_property *ctm_property;
1931         /**
1932          * @gamma_lut_property: Optional CRTC property to set the LUT used to
1933          * convert the colors, after the CTM matrix, to the gamma space of the
1934          * connected screen.
1935          */
1936         struct drm_property *gamma_lut_property;
1937         /**
1938          * @gamma_lut_size_property: Optional CRTC property for the size of the
1939          * gamma LUT as supported by the driver (read-only).
1940          */
1941         struct drm_property *gamma_lut_size_property;
1942
1943         /**
1944          * @suggested_x_property: Optional connector property with a hint for
1945          * the position of the output on the host's screen.
1946          */
1947         struct drm_property *suggested_x_property;
1948         /**
1949          * @suggested_y_property: Optional connector property with a hint for
1950          * the position of the output on the host's screen.
1951          */
1952         struct drm_property *suggested_y_property;
1953
1954         /* dumb ioctl parameters */
1955         uint32_t preferred_depth, prefer_shadow;
1956
1957         /**
1958          * @async_page_flip: Does this device support async flips on the primary
1959          * plane?
1960          */
1961         bool async_page_flip;
1962
1963         /**
1964          * @allow_fb_modifiers:
1965          *
1966          * Whether the driver supports fb modifiers in the ADDFB2.1 ioctl call.
1967          */
1968         bool allow_fb_modifiers;
1969
1970         /* cursor size */
1971         uint32_t cursor_width, cursor_height;
1972
1973         struct drm_mode_config_helper_funcs *helper_private;
1974 };
1975
1976 /**
1977  * drm_for_each_plane_mask - iterate over planes specified by bitmask
1978  * @plane: the loop cursor
1979  * @dev: the DRM device
1980  * @plane_mask: bitmask of plane indices
1981  *
1982  * Iterate over all planes specified by bitmask.
1983  */
1984 #define drm_for_each_plane_mask(plane, dev, plane_mask) \
1985         list_for_each_entry((plane), &(dev)->mode_config.plane_list, head) \
1986                 for_each_if ((plane_mask) & (1 << drm_plane_index(plane)))
1987
1988 /**
1989  * drm_for_each_encoder_mask - iterate over encoders specified by bitmask
1990  * @encoder: the loop cursor
1991  * @dev: the DRM device
1992  * @encoder_mask: bitmask of encoder indices
1993  *
1994  * Iterate over all encoders specified by bitmask.
1995  */
1996 #define drm_for_each_encoder_mask(encoder, dev, encoder_mask) \
1997         list_for_each_entry((encoder), &(dev)->mode_config.encoder_list, head) \
1998                 for_each_if ((encoder_mask) & (1 << drm_encoder_index(encoder)))
1999
2000 #define obj_to_crtc(x) container_of(x, struct drm_crtc, base)
2001 #define obj_to_mode(x) container_of(x, struct drm_display_mode, base)
2002 #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
2003 #define obj_to_blob(x) container_of(x, struct drm_property_blob, base)
2004 #define obj_to_plane(x) container_of(x, struct drm_plane, base)
2005
2006 extern __printf(6, 7)
2007 int drm_crtc_init_with_planes(struct drm_device *dev,
2008                               struct drm_crtc *crtc,
2009                               struct drm_plane *primary,
2010                               struct drm_plane *cursor,
2011                               const struct drm_crtc_funcs *funcs,
2012                               const char *name, ...);
2013 extern void drm_crtc_cleanup(struct drm_crtc *crtc);
2014
2015 /**
2016  * drm_crtc_index - find the index of a registered CRTC
2017  * @crtc: CRTC to find index for
2018  *
2019  * Given a registered CRTC, return the index of that CRTC within a DRM
2020  * device's list of CRTCs.
2021  */
2022 static inline unsigned int drm_crtc_index(struct drm_crtc *crtc)
2023 {
2024         return crtc->index;
2025 }
2026
2027 /**
2028  * drm_crtc_mask - find the mask of a registered CRTC
2029  * @crtc: CRTC to find mask for
2030  *
2031  * Given a registered CRTC, return the mask bit of that CRTC for an
2032  * encoder's possible_crtcs field.
2033  */
2034 static inline uint32_t drm_crtc_mask(struct drm_crtc *crtc)
2035 {
2036         return 1 << drm_crtc_index(crtc);
2037 }
2038
2039 extern __printf(8, 9)
2040 int drm_universal_plane_init(struct drm_device *dev,
2041                              struct drm_plane *plane,
2042                              unsigned long possible_crtcs,
2043                              const struct drm_plane_funcs *funcs,
2044                              const uint32_t *formats,
2045                              unsigned int format_count,
2046                              enum drm_plane_type type,
2047                              const char *name, ...);
2048 extern int drm_plane_init(struct drm_device *dev,
2049                           struct drm_plane *plane,
2050                           unsigned long possible_crtcs,
2051                           const struct drm_plane_funcs *funcs,
2052                           const uint32_t *formats, unsigned int format_count,
2053                           bool is_primary);
2054 extern void drm_plane_cleanup(struct drm_plane *plane);
2055
2056 /**
2057  * drm_plane_index - find the index of a registered plane
2058  * @plane: plane to find index for
2059  *
2060  * Given a registered plane, return the index of that plane within a DRM
2061  * device's list of planes.
2062  */
2063 static inline unsigned int drm_plane_index(struct drm_plane *plane)
2064 {
2065         return plane->index;
2066 }
2067 extern struct drm_plane * drm_plane_from_index(struct drm_device *dev, int idx);
2068 extern void drm_plane_force_disable(struct drm_plane *plane);
2069 extern void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2070                                    int *hdisplay, int *vdisplay);
2071 extern int drm_crtc_force_disable(struct drm_crtc *crtc);
2072 extern int drm_crtc_force_disable_all(struct drm_device *dev);
2073
2074 extern void drm_mode_config_init(struct drm_device *dev);
2075 extern void drm_mode_config_reset(struct drm_device *dev);
2076 extern void drm_mode_config_cleanup(struct drm_device *dev);
2077
2078 extern int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2079                                          int gamma_size);
2080
2081 extern int drm_mode_set_config_internal(struct drm_mode_set *set);
2082
2083 extern struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
2084                                                          char topology[8]);
2085 extern struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
2086                                                char topology[8]);
2087 extern void drm_mode_put_tile_group(struct drm_device *dev,
2088                                    struct drm_tile_group *tg);
2089
2090 extern int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
2091                                        struct drm_property *property,
2092                                        uint64_t value);
2093
2094 extern struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
2095                                                               unsigned int supported_rotations);
2096 extern unsigned int drm_rotation_simplify(unsigned int rotation,
2097                                           unsigned int supported_rotations);
2098 extern void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
2099                                        uint degamma_lut_size,
2100                                        bool has_ctm,
2101                                        uint gamma_lut_size);
2102
2103 int drm_plane_create_zpos_property(struct drm_plane *plane,
2104                                    unsigned int zpos,
2105                                    unsigned int min, unsigned int max);
2106
2107 int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
2108                                              unsigned int zpos);
2109
2110 /* Helpers */
2111 static inline struct drm_plane *drm_plane_find(struct drm_device *dev,
2112                 uint32_t id)
2113 {
2114         struct drm_mode_object *mo;
2115         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PLANE);
2116         return mo ? obj_to_plane(mo) : NULL;
2117 }
2118
2119 static inline struct drm_crtc *drm_crtc_find(struct drm_device *dev,
2120         uint32_t id)
2121 {
2122         struct drm_mode_object *mo;
2123         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_CRTC);
2124         return mo ? obj_to_crtc(mo) : NULL;
2125 }
2126
2127 /*
2128  * Extract a degamma/gamma LUT value provided by user and round it to the
2129  * precision supported by the hardware.
2130  */
2131 static inline uint32_t drm_color_lut_extract(uint32_t user_input,
2132                                              uint32_t bit_precision)
2133 {
2134         uint32_t val = user_input;
2135         uint32_t max = 0xffff >> (16 - bit_precision);
2136
2137         /* Round only if we're not using full precision. */
2138         if (bit_precision < 16) {
2139                 val += 1UL << (16 - bit_precision - 1);
2140                 val >>= 16 - bit_precision;
2141         }
2142
2143         return clamp_val(val, 0, max);
2144 }
2145
2146 /* Plane list iterator for legacy (overlay only) planes. */
2147 #define drm_for_each_legacy_plane(plane, dev) \
2148         list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) \
2149                 for_each_if (plane->type == DRM_PLANE_TYPE_OVERLAY)
2150
2151 #define drm_for_each_plane(plane, dev) \
2152         list_for_each_entry(plane, &(dev)->mode_config.plane_list, head)
2153
2154 #define drm_for_each_crtc(crtc, dev) \
2155         list_for_each_entry(crtc, &(dev)->mode_config.crtc_list, head)
2156
2157 static inline void
2158 assert_drm_connector_list_read_locked(struct drm_mode_config *mode_config)
2159 {
2160         /*
2161          * The connector hotadd/remove code currently grabs both locks when
2162          * updating lists. Hence readers need only hold either of them to be
2163          * safe and the check amounts to
2164          *
2165          * WARN_ON(not_holding(A) && not_holding(B)).
2166          */
2167         WARN_ON(!mutex_is_locked(&mode_config->mutex) &&
2168                 !drm_modeset_is_locked(&mode_config->connection_mutex));
2169 }
2170
2171 #define drm_for_each_connector(connector, dev) \
2172         for (assert_drm_connector_list_read_locked(&(dev)->mode_config),        \
2173              connector = list_first_entry(&(dev)->mode_config.connector_list,   \
2174                                           struct drm_connector, head);          \
2175              &connector->head != (&(dev)->mode_config.connector_list);          \
2176              connector = list_next_entry(connector, head))
2177
2178 #define drm_for_each_encoder(encoder, dev) \
2179         list_for_each_entry(encoder, &(dev)->mode_config.encoder_list, head)
2180
2181 #define drm_for_each_fb(fb, dev) \
2182         for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)),            \
2183              fb = list_first_entry(&(dev)->mode_config.fb_list, \
2184                                           struct drm_framebuffer, head);        \
2185              &fb->head != (&(dev)->mode_config.fb_list);                        \
2186              fb = list_next_entry(fb, head))
2187
2188 /* drm_edid.c */
2189 bool drm_probe_ddc(struct i2c_adapter *adapter);
2190 struct edid *drm_get_edid(struct drm_connector *connector,
2191                           struct i2c_adapter *adapter);
2192 struct edid *drm_get_edid_switcheroo(struct drm_connector *connector,
2193                                      struct i2c_adapter *adapter);
2194 struct edid *drm_edid_duplicate(const struct edid *edid);
2195 int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid);
2196
2197 u8 drm_match_cea_mode(const struct drm_display_mode *to_match);
2198 enum hdmi_picture_aspect drm_get_cea_aspect_ratio(const u8 video_code);
2199 bool drm_detect_hdmi_monitor(struct edid *edid);
2200 bool drm_detect_monitor_audio(struct edid *edid);
2201 bool drm_rgb_quant_range_selectable(struct edid *edid);
2202 int drm_add_modes_noedid(struct drm_connector *connector,
2203                          int hdisplay, int vdisplay);
2204 void drm_set_preferred_mode(struct drm_connector *connector,
2205                             int hpref, int vpref);
2206
2207 int drm_edid_header_is_valid(const u8 *raw_edid);
2208 bool drm_edid_block_valid(u8 *raw_edid, int block, bool print_bad_edid,
2209                           bool *edid_corrupt);
2210 bool drm_edid_is_valid(struct edid *edid);
2211 void drm_edid_get_monitor_name(struct edid *edid, char *name,
2212                                int buflen);
2213 struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
2214                                            int hsize, int vsize, int fresh,
2215                                            bool rb);
2216
2217 /* drm_bridge.c */
2218 extern int drm_bridge_add(struct drm_bridge *bridge);
2219 extern void drm_bridge_remove(struct drm_bridge *bridge);
2220 extern struct drm_bridge *of_drm_find_bridge(struct device_node *np);
2221 extern int drm_bridge_attach(struct drm_device *dev, struct drm_bridge *bridge);
2222 extern void drm_bridge_detach(struct drm_bridge *bridge);
2223
2224 bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
2225                         const struct drm_display_mode *mode,
2226                         struct drm_display_mode *adjusted_mode);
2227 void drm_bridge_disable(struct drm_bridge *bridge);
2228 void drm_bridge_post_disable(struct drm_bridge *bridge);
2229 void drm_bridge_mode_set(struct drm_bridge *bridge,
2230                         struct drm_display_mode *mode,
2231                         struct drm_display_mode *adjusted_mode);
2232 void drm_bridge_pre_enable(struct drm_bridge *bridge);
2233 void drm_bridge_enable(struct drm_bridge *bridge);
2234
2235 #endif /* __DRM_CRTC_H__ */