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