]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/drm/drm_crtc.h
add cursor hotspot to drm_framebuffer
[karo-tx-linux.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
39 struct drm_device;
40 struct drm_mode_set;
41 struct drm_framebuffer;
42 struct drm_object_properties;
43 struct drm_file;
44 struct drm_clip_rect;
45 struct device_node;
46 struct fence;
47
48 struct drm_mode_object {
49         uint32_t id;
50         uint32_t type;
51         struct drm_object_properties *properties;
52         struct kref refcount;
53         void (*free_cb)(struct kref *kref);
54 };
55
56 #define DRM_OBJECT_MAX_PROPERTY 24
57 struct drm_object_properties {
58         int count, atomic_count;
59         /* NOTE: if we ever start dynamically destroying properties (ie.
60          * not at drm_mode_config_cleanup() time), then we'd have to do
61          * a better job of detaching property from mode objects to avoid
62          * dangling property pointers:
63          */
64         struct drm_property *properties[DRM_OBJECT_MAX_PROPERTY];
65         /* do not read/write values directly, but use drm_object_property_get_value()
66          * and drm_object_property_set_value():
67          */
68         uint64_t values[DRM_OBJECT_MAX_PROPERTY];
69 };
70
71 static inline int64_t U642I64(uint64_t val)
72 {
73         return (int64_t)*((int64_t *)&val);
74 }
75 static inline uint64_t I642U64(int64_t val)
76 {
77         return (uint64_t)*((uint64_t *)&val);
78 }
79
80 /*
81  * Rotation property bits. DRM_ROTATE_<degrees> rotates the image by the
82  * specified amount in degrees in counter clockwise direction. DRM_REFLECT_X and
83  * DRM_REFLECT_Y reflects the image along the specified axis prior to rotation
84  */
85 #define DRM_ROTATE_MASK 0x0f
86 #define DRM_ROTATE_0    0
87 #define DRM_ROTATE_90   1
88 #define DRM_ROTATE_180  2
89 #define DRM_ROTATE_270  3
90 #define DRM_REFLECT_MASK (~DRM_ROTATE_MASK)
91 #define DRM_REFLECT_X   4
92 #define DRM_REFLECT_Y   5
93
94 enum drm_connector_force {
95         DRM_FORCE_UNSPECIFIED,
96         DRM_FORCE_OFF,
97         DRM_FORCE_ON,         /* force on analog part normally */
98         DRM_FORCE_ON_DIGITAL, /* for DVI-I use digital connector */
99 };
100
101 #include <drm/drm_modes.h>
102
103 enum drm_connector_status {
104         connector_status_connected = 1,
105         connector_status_disconnected = 2,
106         connector_status_unknown = 3,
107 };
108
109 enum subpixel_order {
110         SubPixelUnknown = 0,
111         SubPixelHorizontalRGB,
112         SubPixelHorizontalBGR,
113         SubPixelVerticalRGB,
114         SubPixelVerticalBGR,
115         SubPixelNone,
116 };
117
118 #define DRM_COLOR_FORMAT_RGB444         (1<<0)
119 #define DRM_COLOR_FORMAT_YCRCB444       (1<<1)
120 #define DRM_COLOR_FORMAT_YCRCB422       (1<<2)
121
122 #define DRM_BUS_FLAG_DE_LOW             (1<<0)
123 #define DRM_BUS_FLAG_DE_HIGH            (1<<1)
124 /* drive data on pos. edge */
125 #define DRM_BUS_FLAG_PIXDATA_POSEDGE    (1<<2)
126 /* drive data on neg. edge */
127 #define DRM_BUS_FLAG_PIXDATA_NEGEDGE    (1<<3)
128
129 /*
130  * Describes a given display (e.g. CRT or flat panel) and its limitations.
131  */
132 struct drm_display_info {
133         char name[DRM_DISPLAY_INFO_LEN];
134
135         /* Physical size */
136         unsigned int width_mm;
137         unsigned int height_mm;
138
139         /* Clock limits FIXME: storage format */
140         unsigned int min_vfreq, max_vfreq;
141         unsigned int min_hfreq, max_hfreq;
142         unsigned int pixel_clock;
143         unsigned int bpc;
144
145         enum subpixel_order subpixel_order;
146         u32 color_formats;
147
148         const u32 *bus_formats;
149         unsigned int num_bus_formats;
150         u32 bus_flags;
151
152         /* Mask of supported hdmi deep color modes */
153         u8 edid_hdmi_dc_modes;
154
155         u8 cea_rev;
156 };
157
158 /* data corresponds to displayid vend/prod/serial */
159 struct drm_tile_group {
160         struct kref refcount;
161         struct drm_device *dev;
162         int id;
163         u8 group_data[8];
164 };
165
166 /**
167  * struct drm_framebuffer_funcs - framebuffer hooks
168  */
169 struct drm_framebuffer_funcs {
170         /**
171          * @destroy:
172          *
173          * Clean up framebuffer resources, specifically also unreference the
174          * backing storage. The core guarantees to call this function for every
175          * framebuffer successfully created by ->fb_create() in
176          * &drm_mode_config_funcs. Drivers must also call
177          * drm_framebuffer_cleanup() to release DRM core resources for this
178          * framebuffer.
179          */
180         void (*destroy)(struct drm_framebuffer *framebuffer);
181
182         /**
183          * @create_handle:
184          *
185          * Create a buffer handle in the driver-specific buffer manager (either
186          * GEM or TTM) valid for the passed-in struct &drm_file. This is used by
187          * the core to implement the GETFB IOCTL, which returns (for
188          * sufficiently priviledged user) also a native buffer handle. This can
189          * be used for seamless transitions between modesetting clients by
190          * copying the current screen contents to a private buffer and blending
191          * between that and the new contents.
192          *
193          * GEM based drivers should call drm_gem_handle_create() to create the
194          * handle.
195          *
196          * RETURNS:
197          *
198          * 0 on success or a negative error code on failure.
199          */
200         int (*create_handle)(struct drm_framebuffer *fb,
201                              struct drm_file *file_priv,
202                              unsigned int *handle);
203         /**
204          * @dirty:
205          *
206          * Optional callback for the dirty fb IOCTL.
207          *
208          * Userspace can notify the driver via this callback that an area of the
209          * framebuffer has changed and should be flushed to the display
210          * hardware. This can also be used internally, e.g. by the fbdev
211          * emulation, though that's not the case currently.
212          *
213          * See documentation in drm_mode.h for the struct drm_mode_fb_dirty_cmd
214          * for more information as all the semantics and arguments have a one to
215          * one mapping on this function.
216          *
217          * RETURNS:
218          *
219          * 0 on success or a negative error code on failure.
220          */
221         int (*dirty)(struct drm_framebuffer *framebuffer,
222                      struct drm_file *file_priv, unsigned flags,
223                      unsigned color, struct drm_clip_rect *clips,
224                      unsigned num_clips);
225 };
226
227 struct drm_framebuffer {
228         struct drm_device *dev;
229         /*
230          * Note that the fb is refcounted for the benefit of driver internals,
231          * for example some hw, disabling a CRTC/plane is asynchronous, and
232          * scanout does not actually complete until the next vblank.  So some
233          * cleanup (like releasing the reference(s) on the backing GEM bo(s))
234          * should be deferred.  In cases like this, the driver would like to
235          * hold a ref to the fb even though it has already been removed from
236          * userspace perspective.
237          * The refcount is stored inside the mode object.
238          */
239         /*
240          * Place on the dev->mode_config.fb_list, access protected by
241          * dev->mode_config.fb_lock.
242          */
243         struct list_head head;
244         struct drm_mode_object base;
245         const struct drm_framebuffer_funcs *funcs;
246         unsigned int pitches[4];
247         unsigned int offsets[4];
248         uint64_t modifier[4];
249         unsigned int width;
250         unsigned int height;
251         /* depth can be 15 or 16 */
252         unsigned int depth;
253         int bits_per_pixel;
254         int flags;
255         uint32_t pixel_format; /* fourcc format */
256         int hot_x;
257         int hot_y;
258         struct list_head filp_head;
259 };
260
261 struct drm_property_blob {
262         struct drm_mode_object base;
263         struct drm_device *dev;
264         struct list_head head_global;
265         struct list_head head_file;
266         size_t length;
267         unsigned char data[];
268 };
269
270 struct drm_property_enum {
271         uint64_t value;
272         struct list_head head;
273         char name[DRM_PROP_NAME_LEN];
274 };
275
276 struct drm_property {
277         struct list_head head;
278         struct drm_mode_object base;
279         uint32_t flags;
280         char name[DRM_PROP_NAME_LEN];
281         uint32_t num_values;
282         uint64_t *values;
283         struct drm_device *dev;
284
285         struct list_head enum_list;
286 };
287
288 struct drm_crtc;
289 struct drm_connector;
290 struct drm_encoder;
291 struct drm_pending_vblank_event;
292 struct drm_plane;
293 struct drm_bridge;
294 struct drm_atomic_state;
295
296 struct drm_crtc_helper_funcs;
297 struct drm_encoder_helper_funcs;
298 struct drm_connector_helper_funcs;
299 struct drm_plane_helper_funcs;
300
301 /**
302  * struct drm_crtc_state - mutable CRTC state
303  * @crtc: backpointer to the CRTC
304  * @enable: whether the CRTC should be enabled, gates all other state
305  * @active: whether the CRTC is actively displaying (used for DPMS)
306  * @planes_changed: planes on this crtc are updated
307  * @mode_changed: crtc_state->mode or crtc_state->enable has been changed
308  * @active_changed: crtc_state->active has been toggled.
309  * @connectors_changed: connectors to this crtc have been updated
310  * @color_mgmt_changed: color management properties have changed (degamma or
311  *      gamma LUT or CSC matrix)
312  * @plane_mask: bitmask of (1 << drm_plane_index(plane)) of attached planes
313  * @connector_mask: bitmask of (1 << drm_connector_index(connector)) of attached connectors
314  * @encoder_mask: bitmask of (1 << drm_encoder_index(encoder)) of attached encoders
315  * @last_vblank_count: for helpers and drivers to capture the vblank of the
316  *      update to ensure framebuffer cleanup isn't done too early
317  * @adjusted_mode: for use by helpers and drivers to compute adjusted mode timings
318  * @mode: current mode timings
319  * @degamma_lut: Lookup table for converting framebuffer pixel data
320  *      before apply the conversion matrix
321  * @ctm: Transformation matrix
322  * @gamma_lut: Lookup table for converting pixel data after the
323  *      conversion matrix
324  * @event: optional pointer to a DRM event to signal upon completion of the
325  *      state update
326  * @state: backpointer to global drm_atomic_state
327  *
328  * Note that the distinction between @enable and @active is rather subtile:
329  * Flipping @active while @enable is set without changing anything else may
330  * never return in a failure from the ->atomic_check callback. Userspace assumes
331  * that a DPMS On will always succeed. In other words: @enable controls resource
332  * assignment, @active controls the actual hardware state.
333  */
334 struct drm_crtc_state {
335         struct drm_crtc *crtc;
336
337         bool enable;
338         bool active;
339
340         /* computed state bits used by helpers and drivers */
341         bool planes_changed : 1;
342         bool mode_changed : 1;
343         bool active_changed : 1;
344         bool connectors_changed : 1;
345         bool color_mgmt_changed : 1;
346
347         /* attached planes bitmask:
348          * WARNING: transitional helpers do not maintain plane_mask so
349          * drivers not converted over to atomic helpers should not rely
350          * on plane_mask being accurate!
351          */
352         u32 plane_mask;
353
354         u32 connector_mask;
355         u32 encoder_mask;
356
357         /* last_vblank_count: for vblank waits before cleanup */
358         u32 last_vblank_count;
359
360         /* adjusted_mode: for use by helpers and drivers */
361         struct drm_display_mode adjusted_mode;
362
363         struct drm_display_mode mode;
364
365         /* blob property to expose current mode to atomic userspace */
366         struct drm_property_blob *mode_blob;
367
368         /* blob property to expose color management to userspace */
369         struct drm_property_blob *degamma_lut;
370         struct drm_property_blob *ctm;
371         struct drm_property_blob *gamma_lut;
372
373         struct drm_pending_vblank_event *event;
374
375         struct drm_atomic_state *state;
376 };
377
378 /**
379  * struct drm_crtc_funcs - control CRTCs for a given device
380  *
381  * The drm_crtc_funcs structure is the central CRTC management structure
382  * in the DRM.  Each CRTC controls one or more connectors (note that the name
383  * CRTC is simply historical, a CRTC may control LVDS, VGA, DVI, TV out, etc.
384  * connectors, not just CRTs).
385  *
386  * Each driver is responsible for filling out this structure at startup time,
387  * in addition to providing other modesetting features, like i2c and DDC
388  * bus accessors.
389  */
390 struct drm_crtc_funcs {
391         /**
392          * @reset:
393          *
394          * Reset CRTC hardware and software state to off. This function isn't
395          * called by the core directly, only through drm_mode_config_reset().
396          * It's not a helper hook only for historical reasons.
397          *
398          * Atomic drivers can use drm_atomic_helper_crtc_reset() to reset
399          * atomic state using this hook.
400          */
401         void (*reset)(struct drm_crtc *crtc);
402
403         /**
404          * @cursor_set:
405          *
406          * Update the cursor image. The cursor position is relative to the CRTC
407          * and can be partially or fully outside of the visible area.
408          *
409          * Note that contrary to all other KMS functions the legacy cursor entry
410          * points don't take a framebuffer object, but instead take directly a
411          * raw buffer object id from the driver's buffer manager (which is
412          * either GEM or TTM for current drivers).
413          *
414          * This entry point is deprecated, drivers should instead implement
415          * universal plane support and register a proper cursor plane using
416          * drm_crtc_init_with_planes().
417          *
418          * This callback is optional
419          *
420          * RETURNS:
421          *
422          * 0 on success or a negative error code on failure.
423          */
424         int (*cursor_set)(struct drm_crtc *crtc, struct drm_file *file_priv,
425                           uint32_t handle, uint32_t width, uint32_t height);
426
427         /**
428          * @cursor_set2:
429          *
430          * Update the cursor image, including hotspot information. The hotspot
431          * must not affect the cursor position in CRTC coordinates, but is only
432          * meant as a hint for virtualized display hardware to coordinate the
433          * guests and hosts cursor position. The cursor hotspot is relative to
434          * the cursor image. Otherwise this works exactly like @cursor_set.
435          *
436          * This entry point is deprecated, drivers should instead implement
437          * universal plane support and register a proper cursor plane using
438          * drm_crtc_init_with_planes().
439          *
440          * This callback is optional.
441          *
442          * RETURNS:
443          *
444          * 0 on success or a negative error code on failure.
445          */
446         int (*cursor_set2)(struct drm_crtc *crtc, struct drm_file *file_priv,
447                            uint32_t handle, uint32_t width, uint32_t height,
448                            int32_t hot_x, int32_t hot_y);
449
450         /**
451          * @cursor_move:
452          *
453          * Update the cursor position. The cursor does not need to be visible
454          * when this hook is called.
455          *
456          * This entry point is deprecated, drivers should instead implement
457          * universal plane support and register a proper cursor plane using
458          * drm_crtc_init_with_planes().
459          *
460          * This callback is optional.
461          *
462          * RETURNS:
463          *
464          * 0 on success or a negative error code on failure.
465          */
466         int (*cursor_move)(struct drm_crtc *crtc, int x, int y);
467
468         /**
469          * @gamma_set:
470          *
471          * Set gamma on the CRTC.
472          *
473          * This callback is optional.
474          *
475          * NOTE:
476          *
477          * Drivers that support gamma tables and also fbdev emulation through
478          * the provided helper library need to take care to fill out the gamma
479          * hooks for both. Currently there's a bit an unfortunate duplication
480          * going on, which should eventually be unified to just one set of
481          * hooks.
482          */
483         void (*gamma_set)(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
484                           uint32_t start, uint32_t size);
485
486         /**
487          * @destroy:
488          *
489          * Clean up plane resources. This is only called at driver unload time
490          * through drm_mode_config_cleanup() since a CRTC cannot be hotplugged
491          * in DRM.
492          */
493         void (*destroy)(struct drm_crtc *crtc);
494
495         /**
496          * @set_config:
497          *
498          * This is the main legacy entry point to change the modeset state on a
499          * CRTC. All the details of the desired configuration are passed in a
500          * struct &drm_mode_set - see there for details.
501          *
502          * Drivers implementing atomic modeset should use
503          * drm_atomic_helper_set_config() to implement this hook.
504          *
505          * RETURNS:
506          *
507          * 0 on success or a negative error code on failure.
508          */
509         int (*set_config)(struct drm_mode_set *set);
510
511         /**
512          * @page_flip:
513          *
514          * Legacy entry point to schedule a flip to the given framebuffer.
515          *
516          * Page flipping is a synchronization mechanism that replaces the frame
517          * buffer being scanned out by the CRTC with a new frame buffer during
518          * vertical blanking, avoiding tearing (except when requested otherwise
519          * through the DRM_MODE_PAGE_FLIP_ASYNC flag). When an application
520          * requests a page flip the DRM core verifies that the new frame buffer
521          * is large enough to be scanned out by the CRTC in the currently
522          * configured mode and then calls the CRTC ->page_flip() operation with a
523          * pointer to the new frame buffer.
524          *
525          * The driver must wait for any pending rendering to the new framebuffer
526          * to complete before executing the flip. It should also wait for any
527          * pending rendering from other drivers if the underlying buffer is a
528          * shared dma-buf.
529          *
530          * An application can request to be notified when the page flip has
531          * completed. The drm core will supply a struct &drm_event in the event
532          * parameter in this case. This can be handled by the
533          * drm_crtc_send_vblank_event() function, which the driver should call on
534          * the provided event upon completion of the flip. Note that if
535          * the driver supports vblank signalling and timestamping the vblank
536          * counters and timestamps must agree with the ones returned from page
537          * flip events. With the current vblank helper infrastructure this can
538          * be achieved by holding a vblank reference while the page flip is
539          * pending, acquired through drm_crtc_vblank_get() and released with
540          * drm_crtc_vblank_put(). Drivers are free to implement their own vblank
541          * counter and timestamp tracking though, e.g. if they have accurate
542          * timestamp registers in hardware.
543          *
544          * FIXME:
545          *
546          * Up to that point drivers need to manage events themselves and can use
547          * even->base.list freely for that. Specifically they need to ensure
548          * that they don't send out page flip (or vblank) events for which the
549          * corresponding drm file has been closed already. The drm core
550          * unfortunately does not (yet) take care of that. Therefore drivers
551          * currently must clean up and release pending events in their
552          * ->preclose driver function.
553          *
554          * This callback is optional.
555          *
556          * NOTE:
557          *
558          * Very early versions of the KMS ABI mandated that the driver must
559          * block (but not reject) any rendering to the old framebuffer until the
560          * flip operation has completed and the old framebuffer is no longer
561          * visible. This requirement has been lifted, and userspace is instead
562          * expected to request delivery of an event and wait with recycling old
563          * buffers until such has been received.
564          *
565          * RETURNS:
566          *
567          * 0 on success or a negative error code on failure. Note that if a
568          * ->page_flip() operation is already pending the callback should return
569          * -EBUSY. Pageflips on a disabled CRTC (either by setting a NULL mode
570          * or just runtime disabled through DPMS respectively the new atomic
571          * "ACTIVE" state) should result in an -EINVAL error code. Note that
572          * drm_atomic_helper_page_flip() checks this already for atomic drivers.
573          */
574         int (*page_flip)(struct drm_crtc *crtc,
575                          struct drm_framebuffer *fb,
576                          struct drm_pending_vblank_event *event,
577                          uint32_t flags);
578
579         /**
580          * @set_property:
581          *
582          * This is the legacy entry point to update a property attached to the
583          * CRTC.
584          *
585          * Drivers implementing atomic modeset should use
586          * drm_atomic_helper_crtc_set_property() to implement this hook.
587          *
588          * This callback is optional if the driver does not support any legacy
589          * driver-private properties.
590          *
591          * RETURNS:
592          *
593          * 0 on success or a negative error code on failure.
594          */
595         int (*set_property)(struct drm_crtc *crtc,
596                             struct drm_property *property, uint64_t val);
597
598         /**
599          * @atomic_duplicate_state:
600          *
601          * Duplicate the current atomic state for this CRTC and return it.
602          * The core and helpers gurantee that any atomic state duplicated with
603          * this hook and still owned by the caller (i.e. not transferred to the
604          * driver by calling ->atomic_commit() from struct
605          * &drm_mode_config_funcs) will be cleaned up by calling the
606          * @atomic_destroy_state hook in this structure.
607          *
608          * Atomic drivers which don't subclass struct &drm_crtc should use
609          * drm_atomic_helper_crtc_duplicate_state(). Drivers that subclass the
610          * state structure to extend it with driver-private state should use
611          * __drm_atomic_helper_crtc_duplicate_state() to make sure shared state is
612          * duplicated in a consistent fashion across drivers.
613          *
614          * It is an error to call this hook before crtc->state has been
615          * initialized correctly.
616          *
617          * NOTE:
618          *
619          * If the duplicate state references refcounted resources this hook must
620          * acquire a reference for each of them. The driver must release these
621          * references again in @atomic_destroy_state.
622          *
623          * RETURNS:
624          *
625          * Duplicated atomic state or NULL when the allocation failed.
626          */
627         struct drm_crtc_state *(*atomic_duplicate_state)(struct drm_crtc *crtc);
628
629         /**
630          * @atomic_destroy_state:
631          *
632          * Destroy a state duplicated with @atomic_duplicate_state and release
633          * or unreference all resources it references
634          */
635         void (*atomic_destroy_state)(struct drm_crtc *crtc,
636                                      struct drm_crtc_state *state);
637
638         /**
639          * @atomic_set_property:
640          *
641          * Decode a driver-private property value and store the decoded value
642          * into the passed-in state structure. Since the atomic core decodes all
643          * standardized properties (even for extensions beyond the core set of
644          * properties which might not be implemented by all drivers) this
645          * requires drivers to subclass the state structure.
646          *
647          * Such driver-private properties should really only be implemented for
648          * truly hardware/vendor specific state. Instead it is preferred to
649          * standardize atomic extension and decode the properties used to expose
650          * such an extension in the core.
651          *
652          * Do not call this function directly, use
653          * drm_atomic_crtc_set_property() instead.
654          *
655          * This callback is optional if the driver does not support any
656          * driver-private atomic properties.
657          *
658          * NOTE:
659          *
660          * This function is called in the state assembly phase of atomic
661          * modesets, which can be aborted for any reason (including on
662          * userspace's request to just check whether a configuration would be
663          * possible). Drivers MUST NOT touch any persistent state (hardware or
664          * software) or data structures except the passed in @state parameter.
665          *
666          * Also since userspace controls in which order properties are set this
667          * function must not do any input validation (since the state update is
668          * incomplete and hence likely inconsistent). Instead any such input
669          * validation must be done in the various atomic_check callbacks.
670          *
671          * RETURNS:
672          *
673          * 0 if the property has been found, -EINVAL if the property isn't
674          * implemented by the driver (which should never happen, the core only
675          * asks for properties attached to this CRTC). No other validation is
676          * allowed by the driver. The core already checks that the property
677          * value is within the range (integer, valid enum value, ...) the driver
678          * set when registering the property.
679          */
680         int (*atomic_set_property)(struct drm_crtc *crtc,
681                                    struct drm_crtc_state *state,
682                                    struct drm_property *property,
683                                    uint64_t val);
684         /**
685          * @atomic_get_property:
686          *
687          * Reads out the decoded driver-private property. This is used to
688          * implement the GETCRTC IOCTL.
689          *
690          * Do not call this function directly, use
691          * drm_atomic_crtc_get_property() instead.
692          *
693          * This callback is optional if the driver does not support any
694          * driver-private atomic properties.
695          *
696          * RETURNS:
697          *
698          * 0 on success, -EINVAL if the property isn't implemented by the
699          * driver (which should never happen, the core only asks for
700          * properties attached to this CRTC).
701          */
702         int (*atomic_get_property)(struct drm_crtc *crtc,
703                                    const struct drm_crtc_state *state,
704                                    struct drm_property *property,
705                                    uint64_t *val);
706 };
707
708 /**
709  * struct drm_crtc - central CRTC control structure
710  * @dev: parent DRM device
711  * @port: OF node used by drm_of_find_possible_crtcs()
712  * @head: list management
713  * @mutex: per-CRTC locking
714  * @base: base KMS object for ID tracking etc.
715  * @primary: primary plane for this CRTC
716  * @cursor: cursor plane for this CRTC
717  * @cursor_x: current x position of the cursor, used for universal cursor planes
718  * @cursor_y: current y position of the cursor, used for universal cursor planes
719  * @enabled: is this CRTC enabled?
720  * @mode: current mode timings
721  * @hwmode: mode timings as programmed to hw regs
722  * @x: x position on screen
723  * @y: y position on screen
724  * @funcs: CRTC control functions
725  * @gamma_size: size of gamma ramp
726  * @gamma_store: gamma ramp values
727  * @helper_private: mid-layer private data
728  * @properties: property tracking for this CRTC
729  * @state: current atomic state for this CRTC
730  * @acquire_ctx: per-CRTC implicit acquire context used by atomic drivers for
731  *      legacy IOCTLs
732  *
733  * Each CRTC may have one or more connectors associated with it.  This structure
734  * allows the CRTC to be controlled.
735  */
736 struct drm_crtc {
737         struct drm_device *dev;
738         struct device_node *port;
739         struct list_head head;
740
741         char *name;
742
743         /*
744          * crtc mutex
745          *
746          * This provides a read lock for the overall crtc state (mode, dpms
747          * state, ...) and a write lock for everything which can be update
748          * without a full modeset (fb, cursor data, ...)
749          */
750         struct drm_modeset_lock mutex;
751
752         struct drm_mode_object base;
753
754         /* primary and cursor planes for CRTC */
755         struct drm_plane *primary;
756         struct drm_plane *cursor;
757
758         /* position of cursor plane on crtc */
759         int cursor_x;
760         int cursor_y;
761
762         bool enabled;
763
764         /* Requested mode from modesetting. */
765         struct drm_display_mode mode;
766
767         /* Programmed mode in hw, after adjustments for encoders,
768          * crtc, panel scaling etc. Needed for timestamping etc.
769          */
770         struct drm_display_mode hwmode;
771
772         int x, y;
773         const struct drm_crtc_funcs *funcs;
774
775         /* Legacy FB CRTC gamma size for reporting to userspace */
776         uint32_t gamma_size;
777         uint16_t *gamma_store;
778
779         /* if you are using the helper */
780         const struct drm_crtc_helper_funcs *helper_private;
781
782         struct drm_object_properties properties;
783
784         struct drm_crtc_state *state;
785
786         /*
787          * For legacy crtc IOCTLs so that atomic drivers can get at the locking
788          * acquire context.
789          */
790         struct drm_modeset_acquire_ctx *acquire_ctx;
791 };
792
793 /**
794  * struct drm_connector_state - mutable connector state
795  * @connector: backpointer to the connector
796  * @crtc: CRTC to connect connector to, NULL if disabled
797  * @best_encoder: can be used by helpers and drivers to select the encoder
798  * @state: backpointer to global drm_atomic_state
799  */
800 struct drm_connector_state {
801         struct drm_connector *connector;
802
803         struct drm_crtc *crtc;  /* do not write directly, use drm_atomic_set_crtc_for_connector() */
804
805         struct drm_encoder *best_encoder;
806
807         struct drm_atomic_state *state;
808 };
809
810 /**
811  * struct drm_connector_funcs - control connectors on a given device
812  *
813  * Each CRTC may have one or more connectors attached to it.  The functions
814  * below allow the core DRM code to control connectors, enumerate available modes,
815  * etc.
816  */
817 struct drm_connector_funcs {
818         /**
819          * @dpms:
820          *
821          * Legacy entry point to set the per-connector DPMS state. Legacy DPMS
822          * is exposed as a standard property on the connector, but diverted to
823          * this callback in the drm core. Note that atomic drivers don't
824          * implement the 4 level DPMS support on the connector any more, but
825          * instead only have an on/off "ACTIVE" property on the CRTC object.
826          *
827          * Drivers implementing atomic modeset should use
828          * drm_atomic_helper_connector_dpms() to implement this hook.
829          *
830          * RETURNS:
831          *
832          * 0 on success or a negative error code on failure.
833          */
834         int (*dpms)(struct drm_connector *connector, int mode);
835
836         /**
837          * @reset:
838          *
839          * Reset connector hardware and software state to off. This function isn't
840          * called by the core directly, only through drm_mode_config_reset().
841          * It's not a helper hook only for historical reasons.
842          *
843          * Atomic drivers can use drm_atomic_helper_connector_reset() to reset
844          * atomic state using this hook.
845          */
846         void (*reset)(struct drm_connector *connector);
847
848         /**
849          * @detect:
850          *
851          * Check to see if anything is attached to the connector. The parameter
852          * force is set to false whilst polling, true when checking the
853          * connector due to a user request. force can be used by the driver to
854          * avoid expensive, destructive operations during automated probing.
855          *
856          * FIXME:
857          *
858          * Note that this hook is only called by the probe helper. It's not in
859          * the helper library vtable purely for historical reasons. The only DRM
860          * core entry point to probe connector state is @fill_modes.
861          *
862          * RETURNS:
863          *
864          * drm_connector_status indicating the connector's status.
865          */
866         enum drm_connector_status (*detect)(struct drm_connector *connector,
867                                             bool force);
868
869         /**
870          * @force:
871          *
872          * This function is called to update internal encoder state when the
873          * connector is forced to a certain state by userspace, either through
874          * the sysfs interfaces or on the kernel cmdline. In that case the
875          * @detect callback isn't called.
876          *
877          * FIXME:
878          *
879          * Note that this hook is only called by the probe helper. It's not in
880          * the helper library vtable purely for historical reasons. The only DRM
881          * core entry point to probe connector state is @fill_modes.
882          */
883         void (*force)(struct drm_connector *connector);
884
885         /**
886          * @fill_modes:
887          *
888          * Entry point for output detection and basic mode validation. The
889          * driver should reprobe the output if needed (e.g. when hotplug
890          * handling is unreliable), add all detected modes to connector->modes
891          * and filter out any the device can't support in any configuration. It
892          * also needs to filter out any modes wider or higher than the
893          * parameters max_width and max_height indicate.
894          *
895          * The drivers must also prune any modes no longer valid from
896          * connector->modes. Furthermore it must update connector->status and
897          * connector->edid.  If no EDID has been received for this output
898          * connector->edid must be NULL.
899          *
900          * Drivers using the probe helpers should use
901          * drm_helper_probe_single_connector_modes() or
902          * drm_helper_probe_single_connector_modes_nomerge() to implement this
903          * function.
904          *
905          * RETURNS:
906          *
907          * The number of modes detected and filled into connector->modes.
908          */
909         int (*fill_modes)(struct drm_connector *connector, uint32_t max_width, uint32_t max_height);
910
911         /**
912          * @set_property:
913          *
914          * This is the legacy entry point to update a property attached to the
915          * connector.
916          *
917          * Drivers implementing atomic modeset should use
918          * drm_atomic_helper_connector_set_property() to implement this hook.
919          *
920          * This callback is optional if the driver does not support any legacy
921          * driver-private properties.
922          *
923          * RETURNS:
924          *
925          * 0 on success or a negative error code on failure.
926          */
927         int (*set_property)(struct drm_connector *connector, struct drm_property *property,
928                              uint64_t val);
929
930         /**
931          * @destroy:
932          *
933          * Clean up connector resources. This is called at driver unload time
934          * through drm_mode_config_cleanup(). It can also be called at runtime
935          * when a connector is being hot-unplugged for drivers that support
936          * connector hotplugging (e.g. DisplayPort MST).
937          */
938         void (*destroy)(struct drm_connector *connector);
939
940         /**
941          * @atomic_duplicate_state:
942          *
943          * Duplicate the current atomic state for this connector and return it.
944          * The core and helpers gurantee that any atomic state duplicated with
945          * this hook and still owned by the caller (i.e. not transferred to the
946          * driver by calling ->atomic_commit() from struct
947          * &drm_mode_config_funcs) will be cleaned up by calling the
948          * @atomic_destroy_state hook in this structure.
949          *
950          * Atomic drivers which don't subclass struct &drm_connector_state should use
951          * drm_atomic_helper_connector_duplicate_state(). Drivers that subclass the
952          * state structure to extend it with driver-private state should use
953          * __drm_atomic_helper_connector_duplicate_state() to make sure shared state is
954          * duplicated in a consistent fashion across drivers.
955          *
956          * It is an error to call this hook before connector->state has been
957          * initialized correctly.
958          *
959          * NOTE:
960          *
961          * If the duplicate state references refcounted resources this hook must
962          * acquire a reference for each of them. The driver must release these
963          * references again in @atomic_destroy_state.
964          *
965          * RETURNS:
966          *
967          * Duplicated atomic state or NULL when the allocation failed.
968          */
969         struct drm_connector_state *(*atomic_duplicate_state)(struct drm_connector *connector);
970
971         /**
972          * @atomic_destroy_state:
973          *
974          * Destroy a state duplicated with @atomic_duplicate_state and release
975          * or unreference all resources it references
976          */
977         void (*atomic_destroy_state)(struct drm_connector *connector,
978                                      struct drm_connector_state *state);
979
980         /**
981          * @atomic_set_property:
982          *
983          * Decode a driver-private property value and store the decoded value
984          * into the passed-in state structure. Since the atomic core decodes all
985          * standardized properties (even for extensions beyond the core set of
986          * properties which might not be implemented by all drivers) this
987          * requires drivers to subclass the state structure.
988          *
989          * Such driver-private properties should really only be implemented for
990          * truly hardware/vendor specific state. Instead it is preferred to
991          * standardize atomic extension and decode the properties used to expose
992          * such an extension in the core.
993          *
994          * Do not call this function directly, use
995          * drm_atomic_connector_set_property() instead.
996          *
997          * This callback is optional if the driver does not support any
998          * driver-private atomic properties.
999          *
1000          * NOTE:
1001          *
1002          * This function is called in the state assembly phase of atomic
1003          * modesets, which can be aborted for any reason (including on
1004          * userspace's request to just check whether a configuration would be
1005          * possible). Drivers MUST NOT touch any persistent state (hardware or
1006          * software) or data structures except the passed in @state parameter.
1007          *
1008          * Also since userspace controls in which order properties are set this
1009          * function must not do any input validation (since the state update is
1010          * incomplete and hence likely inconsistent). Instead any such input
1011          * validation must be done in the various atomic_check callbacks.
1012          *
1013          * RETURNS:
1014          *
1015          * 0 if the property has been found, -EINVAL if the property isn't
1016          * implemented by the driver (which shouldn't ever happen, the core only
1017          * asks for properties attached to this connector). No other validation
1018          * is allowed by the driver. The core already checks that the property
1019          * value is within the range (integer, valid enum value, ...) the driver
1020          * set when registering the property.
1021          */
1022         int (*atomic_set_property)(struct drm_connector *connector,
1023                                    struct drm_connector_state *state,
1024                                    struct drm_property *property,
1025                                    uint64_t val);
1026
1027         /**
1028          * @atomic_get_property:
1029          *
1030          * Reads out the decoded driver-private property. This is used to
1031          * implement the GETCONNECTOR IOCTL.
1032          *
1033          * Do not call this function directly, use
1034          * drm_atomic_connector_get_property() instead.
1035          *
1036          * This callback is optional if the driver does not support any
1037          * driver-private atomic properties.
1038          *
1039          * RETURNS:
1040          *
1041          * 0 on success, -EINVAL if the property isn't implemented by the
1042          * driver (which shouldn't ever happen, the core only asks for
1043          * properties attached to this connector).
1044          */
1045         int (*atomic_get_property)(struct drm_connector *connector,
1046                                    const struct drm_connector_state *state,
1047                                    struct drm_property *property,
1048                                    uint64_t *val);
1049 };
1050
1051 /**
1052  * struct drm_encoder_funcs - encoder controls
1053  *
1054  * Encoders sit between CRTCs and connectors.
1055  */
1056 struct drm_encoder_funcs {
1057         /**
1058          * @reset:
1059          *
1060          * Reset encoder hardware and software state to off. This function isn't
1061          * called by the core directly, only through drm_mode_config_reset().
1062          * It's not a helper hook only for historical reasons.
1063          */
1064         void (*reset)(struct drm_encoder *encoder);
1065
1066         /**
1067          * @destroy:
1068          *
1069          * Clean up encoder resources. This is only called at driver unload time
1070          * through drm_mode_config_cleanup() since an encoder cannot be
1071          * hotplugged in DRM.
1072          */
1073         void (*destroy)(struct drm_encoder *encoder);
1074 };
1075
1076 #define DRM_CONNECTOR_MAX_ENCODER 3
1077
1078 /**
1079  * struct drm_encoder - central DRM encoder structure
1080  * @dev: parent DRM device
1081  * @head: list management
1082  * @base: base KMS object
1083  * @name: encoder name
1084  * @encoder_type: one of the %DRM_MODE_ENCODER_<foo> types in drm_mode.h
1085  * @possible_crtcs: bitmask of potential CRTC bindings
1086  * @possible_clones: bitmask of potential sibling encoders for cloning
1087  * @crtc: currently bound CRTC
1088  * @bridge: bridge associated to the encoder
1089  * @funcs: control functions
1090  * @helper_private: mid-layer private data
1091  *
1092  * CRTCs drive pixels to encoders, which convert them into signals
1093  * appropriate for a given connector or set of connectors.
1094  */
1095 struct drm_encoder {
1096         struct drm_device *dev;
1097         struct list_head head;
1098
1099         struct drm_mode_object base;
1100         char *name;
1101         int encoder_type;
1102         uint32_t possible_crtcs;
1103         uint32_t possible_clones;
1104
1105         struct drm_crtc *crtc;
1106         struct drm_bridge *bridge;
1107         const struct drm_encoder_funcs *funcs;
1108         const struct drm_encoder_helper_funcs *helper_private;
1109 };
1110
1111 /* should we poll this connector for connects and disconnects */
1112 /* hot plug detectable */
1113 #define DRM_CONNECTOR_POLL_HPD (1 << 0)
1114 /* poll for connections */
1115 #define DRM_CONNECTOR_POLL_CONNECT (1 << 1)
1116 /* can cleanly poll for disconnections without flickering the screen */
1117 /* DACs should rarely do this without a lot of testing */
1118 #define DRM_CONNECTOR_POLL_DISCONNECT (1 << 2)
1119
1120 #define MAX_ELD_BYTES   128
1121
1122 /**
1123  * struct drm_connector - central DRM connector control structure
1124  * @dev: parent DRM device
1125  * @kdev: kernel device for sysfs attributes
1126  * @attr: sysfs attributes
1127  * @head: list management
1128  * @base: base KMS object
1129  * @name: connector name
1130  * @connector_type: one of the %DRM_MODE_CONNECTOR_<foo> types from drm_mode.h
1131  * @connector_type_id: index into connector type enum
1132  * @interlace_allowed: can this connector handle interlaced modes?
1133  * @doublescan_allowed: can this connector handle doublescan?
1134  * @stereo_allowed: can this connector handle stereo modes?
1135  * @modes: modes available on this connector (from fill_modes() + user)
1136  * @status: one of the drm_connector_status enums (connected, not, or unknown)
1137  * @probed_modes: list of modes derived directly from the display
1138  * @display_info: information about attached display (e.g. from EDID)
1139  * @funcs: connector control functions
1140  * @edid_blob_ptr: DRM property containing EDID if present
1141  * @properties: property tracking for this connector
1142  * @path_blob_ptr: DRM blob property data for the DP MST path property
1143  * @polled: a %DRM_CONNECTOR_POLL_<foo> value for core driven polling
1144  * @dpms: current dpms state
1145  * @helper_private: mid-layer private data
1146  * @cmdline_mode: mode line parsed from the kernel cmdline for this connector
1147  * @force: a %DRM_FORCE_<foo> state for forced mode sets
1148  * @override_edid: has the EDID been overwritten through debugfs for testing?
1149  * @encoder_ids: valid encoders for this connector
1150  * @encoder: encoder driving this connector, if any
1151  * @eld: EDID-like data, if present
1152  * @dvi_dual: dual link DVI, if found
1153  * @max_tmds_clock: max clock rate, if found
1154  * @latency_present: AV delay info from ELD, if found
1155  * @video_latency: video latency info from ELD, if found
1156  * @audio_latency: audio latency info from ELD, if found
1157  * @null_edid_counter: track sinks that give us all zeros for the EDID
1158  * @bad_edid_counter: track sinks that give us an EDID with invalid checksum
1159  * @edid_corrupt: indicates whether the last read EDID was corrupt
1160  * @debugfs_entry: debugfs directory for this connector
1161  * @state: current atomic state for this connector
1162  * @has_tile: is this connector connected to a tiled monitor
1163  * @tile_group: tile group for the connected monitor
1164  * @tile_is_single_monitor: whether the tile is one monitor housing
1165  * @num_h_tile: number of horizontal tiles in the tile group
1166  * @num_v_tile: number of vertical tiles in the tile group
1167  * @tile_h_loc: horizontal location of this tile
1168  * @tile_v_loc: vertical location of this tile
1169  * @tile_h_size: horizontal size of this tile.
1170  * @tile_v_size: vertical size of this tile.
1171  *
1172  * Each connector may be connected to one or more CRTCs, or may be clonable by
1173  * another connector if they can share a CRTC.  Each connector also has a specific
1174  * position in the broader display (referred to as a 'screen' though it could
1175  * span multiple monitors).
1176  */
1177 struct drm_connector {
1178         struct drm_device *dev;
1179         struct device *kdev;
1180         struct device_attribute *attr;
1181         struct list_head head;
1182
1183         struct drm_mode_object base;
1184
1185         char *name;
1186         int connector_id;
1187         int connector_type;
1188         int connector_type_id;
1189         bool interlace_allowed;
1190         bool doublescan_allowed;
1191         bool stereo_allowed;
1192         struct list_head modes; /* list of modes on this connector */
1193
1194         enum drm_connector_status status;
1195
1196         /* these are modes added by probing with DDC or the BIOS */
1197         struct list_head probed_modes;
1198
1199         struct drm_display_info display_info;
1200         const struct drm_connector_funcs *funcs;
1201
1202         struct drm_property_blob *edid_blob_ptr;
1203         struct drm_object_properties properties;
1204
1205         struct drm_property_blob *path_blob_ptr;
1206
1207         struct drm_property_blob *tile_blob_ptr;
1208
1209         uint8_t polled; /* DRM_CONNECTOR_POLL_* */
1210
1211         /* requested DPMS state */
1212         int dpms;
1213
1214         const struct drm_connector_helper_funcs *helper_private;
1215
1216         /* forced on connector */
1217         struct drm_cmdline_mode cmdline_mode;
1218         enum drm_connector_force force;
1219         bool override_edid;
1220         uint32_t encoder_ids[DRM_CONNECTOR_MAX_ENCODER];
1221         struct drm_encoder *encoder; /* currently active encoder */
1222
1223         /* EDID bits */
1224         uint8_t eld[MAX_ELD_BYTES];
1225         bool dvi_dual;
1226         int max_tmds_clock;     /* in MHz */
1227         bool latency_present[2];
1228         int video_latency[2];   /* [0]: progressive, [1]: interlaced */
1229         int audio_latency[2];
1230         int null_edid_counter; /* needed to workaround some HW bugs where we get all 0s */
1231         unsigned bad_edid_counter;
1232
1233         /* Flag for raw EDID header corruption - used in Displayport
1234          * compliance testing - * Displayport Link CTS Core 1.2 rev1.1 4.2.2.6
1235          */
1236         bool edid_corrupt;
1237
1238         struct dentry *debugfs_entry;
1239
1240         struct drm_connector_state *state;
1241
1242         /* DisplayID bits */
1243         bool has_tile;
1244         struct drm_tile_group *tile_group;
1245         bool tile_is_single_monitor;
1246
1247         uint8_t num_h_tile, num_v_tile;
1248         uint8_t tile_h_loc, tile_v_loc;
1249         uint16_t tile_h_size, tile_v_size;
1250 };
1251
1252 /**
1253  * struct drm_plane_state - mutable plane state
1254  * @plane: backpointer to the plane
1255  * @crtc: currently bound CRTC, NULL if disabled
1256  * @fb: currently bound framebuffer
1257  * @fence: optional fence to wait for before scanning out @fb
1258  * @crtc_x: left position of visible portion of plane on crtc
1259  * @crtc_y: upper position of visible portion of plane on crtc
1260  * @crtc_w: width of visible portion of plane on crtc
1261  * @crtc_h: height of visible portion of plane on crtc
1262  * @src_x: left position of visible portion of plane within
1263  *      plane (in 16.16)
1264  * @src_y: upper position of visible portion of plane within
1265  *      plane (in 16.16)
1266  * @src_w: width of visible portion of plane (in 16.16)
1267  * @src_h: height of visible portion of plane (in 16.16)
1268  * @state: backpointer to global drm_atomic_state
1269  */
1270 struct drm_plane_state {
1271         struct drm_plane *plane;
1272
1273         struct drm_crtc *crtc;   /* do not write directly, use drm_atomic_set_crtc_for_plane() */
1274         struct drm_framebuffer *fb;  /* do not write directly, use drm_atomic_set_fb_for_plane() */
1275         struct fence *fence;
1276
1277         /* Signed dest location allows it to be partially off screen */
1278         int32_t crtc_x, crtc_y;
1279         uint32_t crtc_w, crtc_h;
1280
1281         /* Source values are 16.16 fixed point */
1282         uint32_t src_x, src_y;
1283         uint32_t src_h, src_w;
1284
1285         /* Plane rotation */
1286         unsigned int rotation;
1287
1288         struct drm_atomic_state *state;
1289 };
1290
1291
1292 /**
1293  * struct drm_plane_funcs - driver plane control functions
1294  */
1295 struct drm_plane_funcs {
1296         /**
1297          * @update_plane:
1298          *
1299          * This is the legacy entry point to enable and configure the plane for
1300          * the given CRTC and framebuffer. It is never called to disable the
1301          * plane, i.e. the passed-in crtc and fb paramters are never NULL.
1302          *
1303          * The source rectangle in frame buffer memory coordinates is given by
1304          * the src_x, src_y, src_w and src_h parameters (as 16.16 fixed point
1305          * values). Devices that don't support subpixel plane coordinates can
1306          * ignore the fractional part.
1307          *
1308          * The destination rectangle in CRTC coordinates is given by the
1309          * crtc_x, crtc_y, crtc_w and crtc_h parameters (as integer values).
1310          * Devices scale the source rectangle to the destination rectangle. If
1311          * scaling is not supported, and the source rectangle size doesn't match
1312          * the destination rectangle size, the driver must return a
1313          * -<errorname>EINVAL</errorname> error.
1314          *
1315          * Drivers implementing atomic modeset should use
1316          * drm_atomic_helper_update_plane() to implement this hook.
1317          *
1318          * RETURNS:
1319          *
1320          * 0 on success or a negative error code on failure.
1321          */
1322         int (*update_plane)(struct drm_plane *plane,
1323                             struct drm_crtc *crtc, struct drm_framebuffer *fb,
1324                             int crtc_x, int crtc_y,
1325                             unsigned int crtc_w, unsigned int crtc_h,
1326                             uint32_t src_x, uint32_t src_y,
1327                             uint32_t src_w, uint32_t src_h);
1328
1329         /**
1330          * @disable_plane:
1331          *
1332          * This is the legacy entry point to disable the plane. The DRM core
1333          * calls this method in response to a DRM_IOCTL_MODE_SETPLANE IOCTL call
1334          * with the frame buffer ID set to 0.  Disabled planes must not be
1335          * processed by the CRTC.
1336          *
1337          * Drivers implementing atomic modeset should use
1338          * drm_atomic_helper_disable_plane() to implement this hook.
1339          *
1340          * RETURNS:
1341          *
1342          * 0 on success or a negative error code on failure.
1343          */
1344         int (*disable_plane)(struct drm_plane *plane);
1345
1346         /**
1347          * @destroy:
1348          *
1349          * Clean up plane resources. This is only called at driver unload time
1350          * through drm_mode_config_cleanup() since a plane cannot be hotplugged
1351          * in DRM.
1352          */
1353         void (*destroy)(struct drm_plane *plane);
1354
1355         /**
1356          * @reset:
1357          *
1358          * Reset plane hardware and software state to off. This function isn't
1359          * called by the core directly, only through drm_mode_config_reset().
1360          * It's not a helper hook only for historical reasons.
1361          *
1362          * Atomic drivers can use drm_atomic_helper_plane_reset() to reset
1363          * atomic state using this hook.
1364          */
1365         void (*reset)(struct drm_plane *plane);
1366
1367         /**
1368          * @set_property:
1369          *
1370          * This is the legacy entry point to update a property attached to the
1371          * plane.
1372          *
1373          * Drivers implementing atomic modeset should use
1374          * drm_atomic_helper_plane_set_property() to implement this hook.
1375          *
1376          * This callback is optional if the driver does not support any legacy
1377          * driver-private properties.
1378          *
1379          * RETURNS:
1380          *
1381          * 0 on success or a negative error code on failure.
1382          */
1383         int (*set_property)(struct drm_plane *plane,
1384                             struct drm_property *property, uint64_t val);
1385
1386         /**
1387          * @atomic_duplicate_state:
1388          *
1389          * Duplicate the current atomic state for this plane and return it.
1390          * The core and helpers gurantee that any atomic state duplicated with
1391          * this hook and still owned by the caller (i.e. not transferred to the
1392          * driver by calling ->atomic_commit() from struct
1393          * &drm_mode_config_funcs) will be cleaned up by calling the
1394          * @atomic_destroy_state hook in this structure.
1395          *
1396          * Atomic drivers which don't subclass struct &drm_plane_state should use
1397          * drm_atomic_helper_plane_duplicate_state(). Drivers that subclass the
1398          * state structure to extend it with driver-private state should use
1399          * __drm_atomic_helper_plane_duplicate_state() to make sure shared state is
1400          * duplicated in a consistent fashion across drivers.
1401          *
1402          * It is an error to call this hook before plane->state has been
1403          * initialized correctly.
1404          *
1405          * NOTE:
1406          *
1407          * If the duplicate state references refcounted resources this hook must
1408          * acquire a reference for each of them. The driver must release these
1409          * references again in @atomic_destroy_state.
1410          *
1411          * RETURNS:
1412          *
1413          * Duplicated atomic state or NULL when the allocation failed.
1414          */
1415         struct drm_plane_state *(*atomic_duplicate_state)(struct drm_plane *plane);
1416
1417         /**
1418          * @atomic_destroy_state:
1419          *
1420          * Destroy a state duplicated with @atomic_duplicate_state and release
1421          * or unreference all resources it references
1422          */
1423         void (*atomic_destroy_state)(struct drm_plane *plane,
1424                                      struct drm_plane_state *state);
1425
1426         /**
1427          * @atomic_set_property:
1428          *
1429          * Decode a driver-private property value and store the decoded value
1430          * into the passed-in state structure. Since the atomic core decodes all
1431          * standardized properties (even for extensions beyond the core set of
1432          * properties which might not be implemented by all drivers) this
1433          * requires drivers to subclass the state structure.
1434          *
1435          * Such driver-private properties should really only be implemented for
1436          * truly hardware/vendor specific state. Instead it is preferred to
1437          * standardize atomic extension and decode the properties used to expose
1438          * such an extension in the core.
1439          *
1440          * Do not call this function directly, use
1441          * drm_atomic_plane_set_property() instead.
1442          *
1443          * This callback is optional if the driver does not support any
1444          * driver-private atomic properties.
1445          *
1446          * NOTE:
1447          *
1448          * This function is called in the state assembly phase of atomic
1449          * modesets, which can be aborted for any reason (including on
1450          * userspace's request to just check whether a configuration would be
1451          * possible). Drivers MUST NOT touch any persistent state (hardware or
1452          * software) or data structures except the passed in @state parameter.
1453          *
1454          * Also since userspace controls in which order properties are set this
1455          * function must not do any input validation (since the state update is
1456          * incomplete and hence likely inconsistent). Instead any such input
1457          * validation must be done in the various atomic_check callbacks.
1458          *
1459          * RETURNS:
1460          *
1461          * 0 if the property has been found, -EINVAL if the property isn't
1462          * implemented by the driver (which shouldn't ever happen, the core only
1463          * asks for properties attached to this plane). No other validation is
1464          * allowed by the driver. The core already checks that the property
1465          * value is within the range (integer, valid enum value, ...) the driver
1466          * set when registering the property.
1467          */
1468         int (*atomic_set_property)(struct drm_plane *plane,
1469                                    struct drm_plane_state *state,
1470                                    struct drm_property *property,
1471                                    uint64_t val);
1472
1473         /**
1474          * @atomic_get_property:
1475          *
1476          * Reads out the decoded driver-private property. This is used to
1477          * implement the GETPLANE IOCTL.
1478          *
1479          * Do not call this function directly, use
1480          * drm_atomic_plane_get_property() instead.
1481          *
1482          * This callback is optional if the driver does not support any
1483          * driver-private atomic properties.
1484          *
1485          * RETURNS:
1486          *
1487          * 0 on success, -EINVAL if the property isn't implemented by the
1488          * driver (which should never happen, the core only asks for
1489          * properties attached to this plane).
1490          */
1491         int (*atomic_get_property)(struct drm_plane *plane,
1492                                    const struct drm_plane_state *state,
1493                                    struct drm_property *property,
1494                                    uint64_t *val);
1495 };
1496
1497 enum drm_plane_type {
1498         DRM_PLANE_TYPE_OVERLAY,
1499         DRM_PLANE_TYPE_PRIMARY,
1500         DRM_PLANE_TYPE_CURSOR,
1501 };
1502
1503
1504 /**
1505  * struct drm_plane - central DRM plane control structure
1506  * @dev: DRM device this plane belongs to
1507  * @head: for list management
1508  * @base: base mode object
1509  * @possible_crtcs: pipes this plane can be bound to
1510  * @format_types: array of formats supported by this plane
1511  * @format_count: number of formats supported
1512  * @format_default: driver hasn't supplied supported formats for the plane
1513  * @crtc: currently bound CRTC
1514  * @fb: currently bound fb
1515  * @old_fb: Temporary tracking of the old fb while a modeset is ongoing. Used by
1516  *      drm_mode_set_config_internal() to implement correct refcounting.
1517  * @funcs: helper functions
1518  * @properties: property tracking for this plane
1519  * @type: type of plane (overlay, primary, cursor)
1520  * @state: current atomic state for this plane
1521  */
1522 struct drm_plane {
1523         struct drm_device *dev;
1524         struct list_head head;
1525
1526         char *name;
1527
1528         struct drm_modeset_lock mutex;
1529
1530         struct drm_mode_object base;
1531
1532         uint32_t possible_crtcs;
1533         uint32_t *format_types;
1534         unsigned int format_count;
1535         bool format_default;
1536
1537         struct drm_crtc *crtc;
1538         struct drm_framebuffer *fb;
1539
1540         struct drm_framebuffer *old_fb;
1541
1542         const struct drm_plane_funcs *funcs;
1543
1544         struct drm_object_properties properties;
1545
1546         enum drm_plane_type type;
1547
1548         const struct drm_plane_helper_funcs *helper_private;
1549
1550         struct drm_plane_state *state;
1551 };
1552
1553 /**
1554  * struct drm_bridge_funcs - drm_bridge control functions
1555  * @attach: Called during drm_bridge_attach
1556  */
1557 struct drm_bridge_funcs {
1558         int (*attach)(struct drm_bridge *bridge);
1559
1560         /**
1561          * @mode_fixup:
1562          *
1563          * This callback is used to validate and adjust a mode. The paramater
1564          * mode is the display mode that should be fed to the next element in
1565          * the display chain, either the final &drm_connector or the next
1566          * &drm_bridge. The parameter adjusted_mode is the input mode the bridge
1567          * requires. It can be modified by this callback and does not need to
1568          * match mode.
1569          *
1570          * This is the only hook that allows a bridge to reject a modeset. If
1571          * this function passes all other callbacks must succeed for this
1572          * configuration.
1573          *
1574          * NOTE:
1575          *
1576          * This function is called in the check phase of atomic modesets, which
1577          * can be aborted for any reason (including on userspace's request to
1578          * just check whether a configuration would be possible). Drivers MUST
1579          * NOT touch any persistent state (hardware or software) or data
1580          * structures except the passed in @state parameter.
1581          *
1582          * RETURNS:
1583          *
1584          * True if an acceptable configuration is possible, false if the modeset
1585          * operation should be rejected.
1586          */
1587         bool (*mode_fixup)(struct drm_bridge *bridge,
1588                            const struct drm_display_mode *mode,
1589                            struct drm_display_mode *adjusted_mode);
1590         /**
1591          * @disable:
1592          *
1593          * This callback should disable the bridge. It is called right before
1594          * the preceding element in the display pipe is disabled. If the
1595          * preceding element is a bridge this means it's called before that
1596          * bridge's ->disable() function. If the preceding element is a
1597          * &drm_encoder it's called right before the encoder's ->disable(),
1598          * ->prepare() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1599          *
1600          * The bridge can assume that the display pipe (i.e. clocks and timing
1601          * signals) feeding it is still running when this callback is called.
1602          *
1603          * The disable callback is optional.
1604          */
1605         void (*disable)(struct drm_bridge *bridge);
1606
1607         /**
1608          * @post_disable:
1609          *
1610          * This callback should disable the bridge. It is called right after
1611          * the preceding element in the display pipe is disabled. If the
1612          * preceding element is a bridge this means it's called after that
1613          * bridge's ->post_disable() function. If the preceding element is a
1614          * &drm_encoder it's called right after the encoder's ->disable(),
1615          * ->prepare() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1616          *
1617          * The bridge must assume that the display pipe (i.e. clocks and timing
1618          * singals) feeding it is no longer running when this callback is
1619          * called.
1620          *
1621          * The post_disable callback is optional.
1622          */
1623         void (*post_disable)(struct drm_bridge *bridge);
1624
1625         /**
1626          * @mode_set:
1627          *
1628          * This callback should set the given mode on the bridge. It is called
1629          * after the ->mode_set() callback for the preceding element in the
1630          * display pipeline has been called already. The display pipe (i.e.
1631          * clocks and timing signals) is off when this function is called.
1632          */
1633         void (*mode_set)(struct drm_bridge *bridge,
1634                          struct drm_display_mode *mode,
1635                          struct drm_display_mode *adjusted_mode);
1636         /**
1637          * @pre_enable:
1638          *
1639          * This callback should enable the bridge. It is called right before
1640          * the preceding element in the display pipe is enabled. If the
1641          * preceding element is a bridge this means it's called before that
1642          * bridge's ->pre_enable() function. If the preceding element is a
1643          * &drm_encoder it's called right before the encoder's ->enable(),
1644          * ->commit() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1645          *
1646          * The display pipe (i.e. clocks and timing signals) feeding this bridge
1647          * will not yet be running when this callback is called. The bridge must
1648          * not enable the display link feeding the next bridge in the chain (if
1649          * there is one) when this callback is called.
1650          *
1651          * The pre_enable callback is optional.
1652          */
1653         void (*pre_enable)(struct drm_bridge *bridge);
1654
1655         /**
1656          * @enable:
1657          *
1658          * This callback should enable the bridge. It is called right after
1659          * the preceding element in the display pipe is enabled. If the
1660          * preceding element is a bridge this means it's called after that
1661          * bridge's ->enable() function. If the preceding element is a
1662          * &drm_encoder it's called right after the encoder's ->enable(),
1663          * ->commit() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1664          *
1665          * The bridge can assume that the display pipe (i.e. clocks and timing
1666          * signals) feeding it is running when this callback is called. This
1667          * callback must enable the display link feeding the next bridge in the
1668          * chain if there is one.
1669          *
1670          * The enable callback is optional.
1671          */
1672         void (*enable)(struct drm_bridge *bridge);
1673 };
1674
1675 /**
1676  * struct drm_bridge - central DRM bridge control structure
1677  * @dev: DRM device this bridge belongs to
1678  * @encoder: encoder to which this bridge is connected
1679  * @next: the next bridge in the encoder chain
1680  * @of_node: device node pointer to the bridge
1681  * @list: to keep track of all added bridges
1682  * @funcs: control functions
1683  * @driver_private: pointer to the bridge driver's internal context
1684  */
1685 struct drm_bridge {
1686         struct drm_device *dev;
1687         struct drm_encoder *encoder;
1688         struct drm_bridge *next;
1689 #ifdef CONFIG_OF
1690         struct device_node *of_node;
1691 #endif
1692         struct list_head list;
1693
1694         const struct drm_bridge_funcs *funcs;
1695         void *driver_private;
1696 };
1697
1698 /**
1699  * struct drm_atomic_state - the global state object for atomic updates
1700  * @dev: parent DRM device
1701  * @allow_modeset: allow full modeset
1702  * @legacy_cursor_update: hint to enforce legacy cursor IOCTL semantics
1703  * @legacy_set_config: Disable conflicting encoders instead of failing with -EINVAL.
1704  * @planes: pointer to array of plane pointers
1705  * @plane_states: pointer to array of plane states pointers
1706  * @crtcs: pointer to array of CRTC pointers
1707  * @crtc_states: pointer to array of CRTC states pointers
1708  * @num_connector: size of the @connectors and @connector_states arrays
1709  * @connectors: pointer to array of connector pointers
1710  * @connector_states: pointer to array of connector states pointers
1711  * @acquire_ctx: acquire context for this atomic modeset state update
1712  */
1713 struct drm_atomic_state {
1714         struct drm_device *dev;
1715         bool allow_modeset : 1;
1716         bool legacy_cursor_update : 1;
1717         bool legacy_set_config : 1;
1718         struct drm_plane **planes;
1719         struct drm_plane_state **plane_states;
1720         struct drm_crtc **crtcs;
1721         struct drm_crtc_state **crtc_states;
1722         int num_connector;
1723         struct drm_connector **connectors;
1724         struct drm_connector_state **connector_states;
1725
1726         struct drm_modeset_acquire_ctx *acquire_ctx;
1727 };
1728
1729
1730 /**
1731  * struct drm_mode_set - new values for a CRTC config change
1732  * @fb: framebuffer to use for new config
1733  * @crtc: CRTC whose configuration we're about to change
1734  * @mode: mode timings to use
1735  * @x: position of this CRTC relative to @fb
1736  * @y: position of this CRTC relative to @fb
1737  * @connectors: array of connectors to drive with this CRTC if possible
1738  * @num_connectors: size of @connectors array
1739  *
1740  * Represents a single crtc the connectors that it drives with what mode
1741  * and from which framebuffer it scans out from.
1742  *
1743  * This is used to set modes.
1744  */
1745 struct drm_mode_set {
1746         struct drm_framebuffer *fb;
1747         struct drm_crtc *crtc;
1748         struct drm_display_mode *mode;
1749
1750         uint32_t x;
1751         uint32_t y;
1752
1753         struct drm_connector **connectors;
1754         size_t num_connectors;
1755 };
1756
1757 /**
1758  * struct drm_mode_config_funcs - basic driver provided mode setting functions
1759  *
1760  * Some global (i.e. not per-CRTC, connector, etc) mode setting functions that
1761  * involve drivers.
1762  */
1763 struct drm_mode_config_funcs {
1764         /**
1765          * @fb_create:
1766          *
1767          * Create a new framebuffer object. The core does basic checks on the
1768          * requested metadata, but most of that is left to the driver. See
1769          * struct &drm_mode_fb_cmd2 for details.
1770          *
1771          * If the parameters are deemed valid and the backing storage objects in
1772          * the underlying memory manager all exist, then the driver allocates
1773          * a new &drm_framebuffer structure, subclassed to contain
1774          * driver-specific information (like the internal native buffer object
1775          * references). It also needs to fill out all relevant metadata, which
1776          * should be done by calling drm_helper_mode_fill_fb_struct().
1777          *
1778          * The initialization is finalized by calling drm_framebuffer_init(),
1779          * which registers the framebuffer and makes it accessible to other
1780          * threads.
1781          *
1782          * RETURNS:
1783          *
1784          * A new framebuffer with an initial reference count of 1 or a negative
1785          * error code encoded with ERR_PTR().
1786          */
1787         struct drm_framebuffer *(*fb_create)(struct drm_device *dev,
1788                                              struct drm_file *file_priv,
1789                                              const struct drm_mode_fb_cmd2 *mode_cmd);
1790
1791         /**
1792          * @output_poll_changed:
1793          *
1794          * Callback used by helpers to inform the driver of output configuration
1795          * changes.
1796          *
1797          * Drivers implementing fbdev emulation with the helpers can call
1798          * drm_fb_helper_hotplug_changed from this hook to inform the fbdev
1799          * helper of output changes.
1800          *
1801          * FIXME:
1802          *
1803          * Except that there's no vtable for device-level helper callbacks
1804          * there's no reason this is a core function.
1805          */
1806         void (*output_poll_changed)(struct drm_device *dev);
1807
1808         /**
1809          * @atomic_check:
1810          *
1811          * This is the only hook to validate an atomic modeset update. This
1812          * function must reject any modeset and state changes which the hardware
1813          * or driver doesn't support. This includes but is of course not limited
1814          * to:
1815          *
1816          *  - Checking that the modes, framebuffers, scaling and placement
1817          *    requirements and so on are within the limits of the hardware.
1818          *
1819          *  - Checking that any hidden shared resources are not oversubscribed.
1820          *    This can be shared PLLs, shared lanes, overall memory bandwidth,
1821          *    display fifo space (where shared between planes or maybe even
1822          *    CRTCs).
1823          *
1824          *  - Checking that virtualized resources exported to userspace are not
1825          *    oversubscribed. For various reasons it can make sense to expose
1826          *    more planes, crtcs or encoders than which are physically there. One
1827          *    example is dual-pipe operations (which generally should be hidden
1828          *    from userspace if when lockstepped in hardware, exposed otherwise),
1829          *    where a plane might need 1 hardware plane (if it's just on one
1830          *    pipe), 2 hardware planes (when it spans both pipes) or maybe even
1831          *    shared a hardware plane with a 2nd plane (if there's a compatible
1832          *    plane requested on the area handled by the other pipe).
1833          *
1834          *  - Check that any transitional state is possible and that if
1835          *    requested, the update can indeed be done in the vblank period
1836          *    without temporarily disabling some functions.
1837          *
1838          *  - Check any other constraints the driver or hardware might have.
1839          *
1840          *  - This callback also needs to correctly fill out the &drm_crtc_state
1841          *    in this update to make sure that drm_atomic_crtc_needs_modeset()
1842          *    reflects the nature of the possible update and returns true if and
1843          *    only if the update cannot be applied without tearing within one
1844          *    vblank on that CRTC. The core uses that information to reject
1845          *    updates which require a full modeset (i.e. blanking the screen, or
1846          *    at least pausing updates for a substantial amount of time) if
1847          *    userspace has disallowed that in its request.
1848          *
1849          *  - The driver also does not need to repeat basic input validation
1850          *    like done for the corresponding legacy entry points. The core does
1851          *    that before calling this hook.
1852          *
1853          * See the documentation of @atomic_commit for an exhaustive list of
1854          * error conditions which don't have to be checked at the
1855          * ->atomic_check() stage?
1856          *
1857          * See the documentation for struct &drm_atomic_state for how exactly
1858          * an atomic modeset update is described.
1859          *
1860          * Drivers using the atomic helpers can implement this hook using
1861          * drm_atomic_helper_check(), or one of the exported sub-functions of
1862          * it.
1863          *
1864          * RETURNS:
1865          *
1866          * 0 on success or one of the below negative error codes:
1867          *
1868          *  - -EINVAL, if any of the above constraints are violated.
1869          *
1870          *  - -EDEADLK, when returned from an attempt to acquire an additional
1871          *    &drm_modeset_lock through drm_modeset_lock().
1872          *
1873          *  - -ENOMEM, if allocating additional state sub-structures failed due
1874          *    to lack of memory.
1875          *
1876          *  - -EINTR, -EAGAIN or -ERESTARTSYS, if the IOCTL should be restarted.
1877          *    This can either be due to a pending signal, or because the driver
1878          *    needs to completely bail out to recover from an exceptional
1879          *    situation like a GPU hang. From a userspace point all errors are
1880          *    treated equally.
1881          */
1882         int (*atomic_check)(struct drm_device *dev,
1883                             struct drm_atomic_state *state);
1884
1885         /**
1886          * @atomic_commit:
1887          *
1888          * This is the only hook to commit an atomic modeset update. The core
1889          * guarantees that @atomic_check has been called successfully before
1890          * calling this function, and that nothing has been changed in the
1891          * interim.
1892          *
1893          * See the documentation for struct &drm_atomic_state for how exactly
1894          * an atomic modeset update is described.
1895          *
1896          * Drivers using the atomic helpers can implement this hook using
1897          * drm_atomic_helper_commit(), or one of the exported sub-functions of
1898          * it.
1899          *
1900          * Nonblocking commits (as indicated with the nonblock parameter) must
1901          * do any preparatory work which might result in an unsuccessful commit
1902          * in the context of this callback. The only exceptions are hardware
1903          * errors resulting in -EIO. But even in that case the driver must
1904          * ensure that the display pipe is at least running, to avoid
1905          * compositors crashing when pageflips don't work. Anything else,
1906          * specifically committing the update to the hardware, should be done
1907          * without blocking the caller. For updates which do not require a
1908          * modeset this must be guaranteed.
1909          *
1910          * The driver must wait for any pending rendering to the new
1911          * framebuffers to complete before executing the flip. It should also
1912          * wait for any pending rendering from other drivers if the underlying
1913          * buffer is a shared dma-buf. Nonblocking commits must not wait for
1914          * rendering in the context of this callback.
1915          *
1916          * An application can request to be notified when the atomic commit has
1917          * completed. These events are per-CRTC and can be distinguished by the
1918          * CRTC index supplied in &drm_event to userspace.
1919          *
1920          * The drm core will supply a struct &drm_event in the event
1921          * member of each CRTC's &drm_crtc_state structure. This can be handled by the
1922          * drm_crtc_send_vblank_event() function, which the driver should call on
1923          * the provided event upon completion of the atomic commit. Note that if
1924          * the driver supports vblank signalling and timestamping the vblank
1925          * counters and timestamps must agree with the ones returned from page
1926          * flip events. With the current vblank helper infrastructure this can
1927          * be achieved by holding a vblank reference while the page flip is
1928          * pending, acquired through drm_crtc_vblank_get() and released with
1929          * drm_crtc_vblank_put(). Drivers are free to implement their own vblank
1930          * counter and timestamp tracking though, e.g. if they have accurate
1931          * timestamp registers in hardware.
1932          *
1933          * NOTE:
1934          *
1935          * Drivers are not allowed to shut down any display pipe successfully
1936          * enabled through an atomic commit on their own. Doing so can result in
1937          * compositors crashing if a page flip is suddenly rejected because the
1938          * pipe is off.
1939          *
1940          * RETURNS:
1941          *
1942          * 0 on success or one of the below negative error codes:
1943          *
1944          *  - -EBUSY, if a nonblocking updated is requested and there is
1945          *    an earlier updated pending. Drivers are allowed to support a queue
1946          *    of outstanding updates, but currently no driver supports that.
1947          *    Note that drivers must wait for preceding updates to complete if a
1948          *    synchronous update is requested, they are not allowed to fail the
1949          *    commit in that case.
1950          *
1951          *  - -ENOMEM, if the driver failed to allocate memory. Specifically
1952          *    this can happen when trying to pin framebuffers, which must only
1953          *    be done when committing the state.
1954          *
1955          *  - -ENOSPC, as a refinement of the more generic -ENOMEM to indicate
1956          *    that the driver has run out of vram, iommu space or similar GPU
1957          *    address space needed for framebuffer.
1958          *
1959          *  - -EIO, if the hardware completely died.
1960          *
1961          *  - -EINTR, -EAGAIN or -ERESTARTSYS, if the IOCTL should be restarted.
1962          *    This can either be due to a pending signal, or because the driver
1963          *    needs to completely bail out to recover from an exceptional
1964          *    situation like a GPU hang. From a userspace point of view all errors are
1965          *    treated equally.
1966          *
1967          * This list is exhaustive. Specifically this hook is not allowed to
1968          * return -EINVAL (any invalid requests should be caught in
1969          * @atomic_check) or -EDEADLK (this function must not acquire
1970          * additional modeset locks).
1971          */
1972         int (*atomic_commit)(struct drm_device *dev,
1973                              struct drm_atomic_state *state,
1974                              bool nonblock);
1975
1976         /**
1977          * @atomic_state_alloc:
1978          *
1979          * This optional hook can be used by drivers that want to subclass struct
1980          * &drm_atomic_state to be able to track their own driver-private global
1981          * state easily. If this hook is implemented, drivers must also
1982          * implement @atomic_state_clear and @atomic_state_free.
1983          *
1984          * RETURNS:
1985          *
1986          * A new &drm_atomic_state on success or NULL on failure.
1987          */
1988         struct drm_atomic_state *(*atomic_state_alloc)(struct drm_device *dev);
1989
1990         /**
1991          * @atomic_state_clear:
1992          *
1993          * This hook must clear any driver private state duplicated into the
1994          * passed-in &drm_atomic_state. This hook is called when the caller
1995          * encountered a &drm_modeset_lock deadlock and needs to drop all
1996          * already acquired locks as part of the deadlock avoidance dance
1997          * implemented in drm_modeset_lock_backoff().
1998          *
1999          * Any duplicated state must be invalidated since a concurrent atomic
2000          * update might change it, and the drm atomic interfaces always apply
2001          * updates as relative changes to the current state.
2002          *
2003          * Drivers that implement this must call drm_atomic_state_default_clear()
2004          * to clear common state.
2005          */
2006         void (*atomic_state_clear)(struct drm_atomic_state *state);
2007
2008         /**
2009          * @atomic_state_free:
2010          *
2011          * This hook needs driver private resources and the &drm_atomic_state
2012          * itself. Note that the core first calls drm_atomic_state_clear() to
2013          * avoid code duplicate between the clear and free hooks.
2014          *
2015          * Drivers that implement this must call drm_atomic_state_default_free()
2016          * to release common resources.
2017          */
2018         void (*atomic_state_free)(struct drm_atomic_state *state);
2019 };
2020
2021 /**
2022  * struct drm_mode_config - Mode configuration control structure
2023  * @mutex: mutex protecting KMS related lists and structures
2024  * @connection_mutex: ww mutex protecting connector state and routing
2025  * @acquire_ctx: global implicit acquire context used by atomic drivers for
2026  *      legacy IOCTLs
2027  * @idr_mutex: mutex for KMS ID allocation and management
2028  * @crtc_idr: main KMS ID tracking object
2029  * @fb_lock: mutex to protect fb state and lists
2030  * @num_fb: number of fbs available
2031  * @fb_list: list of framebuffers available
2032  * @num_connector: number of connectors on this device
2033  * @connector_list: list of connector objects
2034  * @num_encoder: number of encoders on this device
2035  * @encoder_list: list of encoder objects
2036  * @num_overlay_plane: number of overlay planes on this device
2037  * @num_total_plane: number of universal (i.e. with primary/curso) planes on this device
2038  * @plane_list: list of plane objects
2039  * @num_crtc: number of CRTCs on this device
2040  * @crtc_list: list of CRTC objects
2041  * @property_list: list of property objects
2042  * @min_width: minimum pixel width on this device
2043  * @min_height: minimum pixel height on this device
2044  * @max_width: maximum pixel width on this device
2045  * @max_height: maximum pixel height on this device
2046  * @funcs: core driver provided mode setting functions
2047  * @fb_base: base address of the framebuffer
2048  * @poll_enabled: track polling support for this device
2049  * @poll_running: track polling status for this device
2050  * @output_poll_work: delayed work for polling in process context
2051  * @property_blob_list: list of all the blob property objects
2052  * @blob_lock: mutex for blob property allocation and management
2053  * @*_property: core property tracking
2054  * @degamma_lut_property: LUT used to convert the framebuffer's colors to linear
2055  *      gamma
2056  * @degamma_lut_size_property: size of the degamma LUT as supported by the
2057  *      driver (read-only)
2058  * @ctm_property: Matrix used to convert colors after the lookup in the
2059  *      degamma LUT
2060  * @gamma_lut_property: LUT used to convert the colors, after the CSC matrix, to
2061  *      the gamma space of the connected screen (read-only)
2062  * @gamma_lut_size_property: size of the gamma LUT as supported by the driver
2063  * @preferred_depth: preferred RBG pixel depth, used by fb helpers
2064  * @prefer_shadow: hint to userspace to prefer shadow-fb rendering
2065  * @async_page_flip: does this device support async flips on the primary plane?
2066  * @cursor_width: hint to userspace for max cursor width
2067  * @cursor_height: hint to userspace for max cursor height
2068  *
2069  * Core mode resource tracking structure.  All CRTC, encoders, and connectors
2070  * enumerated by the driver are added here, as are global properties.  Some
2071  * global restrictions are also here, e.g. dimension restrictions.
2072  */
2073 struct drm_mode_config {
2074         struct mutex mutex; /* protects configuration (mode lists etc.) */
2075         struct drm_modeset_lock connection_mutex; /* protects connector->encoder and encoder->crtc links */
2076         struct drm_modeset_acquire_ctx *acquire_ctx; /* for legacy _lock_all() / _unlock_all() */
2077         struct mutex idr_mutex; /* for IDR management */
2078         struct idr crtc_idr; /* use this idr for all IDs, fb, crtc, connector, modes - just makes life easier */
2079         struct idr tile_idr; /* use this idr for all IDs, fb, crtc, connector, modes - just makes life easier */
2080         /* this is limited to one for now */
2081
2082         struct mutex fb_lock; /* proctects global and per-file fb lists */
2083         int num_fb;
2084         struct list_head fb_list;
2085
2086         int num_connector;
2087         struct ida connector_ida;
2088         struct list_head connector_list;
2089         int num_encoder;
2090         struct list_head encoder_list;
2091
2092         /*
2093          * Track # of overlay planes separately from # of total planes.  By
2094          * default we only advertise overlay planes to userspace; if userspace
2095          * sets the "universal plane" capability bit, we'll go ahead and
2096          * expose all planes.
2097          */
2098         int num_overlay_plane;
2099         int num_total_plane;
2100         struct list_head plane_list;
2101
2102         int num_crtc;
2103         struct list_head crtc_list;
2104
2105         struct list_head property_list;
2106
2107         int min_width, min_height;
2108         int max_width, max_height;
2109         const struct drm_mode_config_funcs *funcs;
2110         resource_size_t fb_base;
2111
2112         /* output poll support */
2113         bool poll_enabled;
2114         bool poll_running;
2115         bool delayed_event;
2116         struct delayed_work output_poll_work;
2117
2118         struct mutex blob_lock;
2119
2120         /* pointers to standard properties */
2121         struct list_head property_blob_list;
2122         struct drm_property *edid_property;
2123         struct drm_property *dpms_property;
2124         struct drm_property *path_property;
2125         struct drm_property *tile_property;
2126         struct drm_property *plane_type_property;
2127         struct drm_property *rotation_property;
2128         struct drm_property *prop_src_x;
2129         struct drm_property *prop_src_y;
2130         struct drm_property *prop_src_w;
2131         struct drm_property *prop_src_h;
2132         struct drm_property *prop_crtc_x;
2133         struct drm_property *prop_crtc_y;
2134         struct drm_property *prop_crtc_w;
2135         struct drm_property *prop_crtc_h;
2136         struct drm_property *prop_fb_id;
2137         struct drm_property *prop_crtc_id;
2138         struct drm_property *prop_active;
2139         struct drm_property *prop_mode_id;
2140
2141         /* DVI-I properties */
2142         struct drm_property *dvi_i_subconnector_property;
2143         struct drm_property *dvi_i_select_subconnector_property;
2144
2145         /* TV properties */
2146         struct drm_property *tv_subconnector_property;
2147         struct drm_property *tv_select_subconnector_property;
2148         struct drm_property *tv_mode_property;
2149         struct drm_property *tv_left_margin_property;
2150         struct drm_property *tv_right_margin_property;
2151         struct drm_property *tv_top_margin_property;
2152         struct drm_property *tv_bottom_margin_property;
2153         struct drm_property *tv_brightness_property;
2154         struct drm_property *tv_contrast_property;
2155         struct drm_property *tv_flicker_reduction_property;
2156         struct drm_property *tv_overscan_property;
2157         struct drm_property *tv_saturation_property;
2158         struct drm_property *tv_hue_property;
2159
2160         /* Optional properties */
2161         struct drm_property *scaling_mode_property;
2162         struct drm_property *aspect_ratio_property;
2163         struct drm_property *dirty_info_property;
2164
2165         /* Optional color correction properties */
2166         struct drm_property *degamma_lut_property;
2167         struct drm_property *degamma_lut_size_property;
2168         struct drm_property *ctm_property;
2169         struct drm_property *gamma_lut_property;
2170         struct drm_property *gamma_lut_size_property;
2171
2172         /* properties for virtual machine layout */
2173         struct drm_property *suggested_x_property;
2174         struct drm_property *suggested_y_property;
2175
2176         /* dumb ioctl parameters */
2177         uint32_t preferred_depth, prefer_shadow;
2178
2179         /* whether async page flip is supported or not */
2180         bool async_page_flip;
2181
2182         /* whether the driver supports fb modifiers */
2183         bool allow_fb_modifiers;
2184
2185         /* cursor size */
2186         uint32_t cursor_width, cursor_height;
2187 };
2188
2189 /**
2190  * drm_for_each_plane_mask - iterate over planes specified by bitmask
2191  * @plane: the loop cursor
2192  * @dev: the DRM device
2193  * @plane_mask: bitmask of plane indices
2194  *
2195  * Iterate over all planes specified by bitmask.
2196  */
2197 #define drm_for_each_plane_mask(plane, dev, plane_mask) \
2198         list_for_each_entry((plane), &(dev)->mode_config.plane_list, head) \
2199                 for_each_if ((plane_mask) & (1 << drm_plane_index(plane)))
2200
2201 /**
2202  * drm_for_each_encoder_mask - iterate over encoders specified by bitmask
2203  * @encoder: the loop cursor
2204  * @dev: the DRM device
2205  * @encoder_mask: bitmask of encoder indices
2206  *
2207  * Iterate over all encoders specified by bitmask.
2208  */
2209 #define drm_for_each_encoder_mask(encoder, dev, encoder_mask) \
2210         list_for_each_entry((encoder), &(dev)->mode_config.encoder_list, head) \
2211                 for_each_if ((encoder_mask) & (1 << drm_encoder_index(encoder)))
2212
2213 #define obj_to_crtc(x) container_of(x, struct drm_crtc, base)
2214 #define obj_to_connector(x) container_of(x, struct drm_connector, base)
2215 #define obj_to_encoder(x) container_of(x, struct drm_encoder, base)
2216 #define obj_to_mode(x) container_of(x, struct drm_display_mode, base)
2217 #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
2218 #define obj_to_property(x) container_of(x, struct drm_property, base)
2219 #define obj_to_blob(x) container_of(x, struct drm_property_blob, base)
2220 #define obj_to_plane(x) container_of(x, struct drm_plane, base)
2221
2222 struct drm_prop_enum_list {
2223         int type;
2224         char *name;
2225 };
2226
2227 extern __printf(6, 7)
2228 int drm_crtc_init_with_planes(struct drm_device *dev,
2229                               struct drm_crtc *crtc,
2230                               struct drm_plane *primary,
2231                               struct drm_plane *cursor,
2232                               const struct drm_crtc_funcs *funcs,
2233                               const char *name, ...);
2234 extern void drm_crtc_cleanup(struct drm_crtc *crtc);
2235 extern unsigned int drm_crtc_index(struct drm_crtc *crtc);
2236
2237 /**
2238  * drm_crtc_mask - find the mask of a registered CRTC
2239  * @crtc: CRTC to find mask for
2240  *
2241  * Given a registered CRTC, return the mask bit of that CRTC for an
2242  * encoder's possible_crtcs field.
2243  */
2244 static inline uint32_t drm_crtc_mask(struct drm_crtc *crtc)
2245 {
2246         return 1 << drm_crtc_index(crtc);
2247 }
2248
2249 extern void drm_connector_ida_init(void);
2250 extern void drm_connector_ida_destroy(void);
2251 extern int drm_connector_init(struct drm_device *dev,
2252                               struct drm_connector *connector,
2253                               const struct drm_connector_funcs *funcs,
2254                               int connector_type);
2255 int drm_connector_register(struct drm_connector *connector);
2256 void drm_connector_unregister(struct drm_connector *connector);
2257
2258 extern void drm_connector_cleanup(struct drm_connector *connector);
2259 static inline unsigned drm_connector_index(struct drm_connector *connector)
2260 {
2261         return connector->connector_id;
2262 }
2263
2264 /* helpers to {un}register all connectors from sysfs for device */
2265 extern int drm_connector_register_all(struct drm_device *dev);
2266 extern void drm_connector_unregister_all(struct drm_device *dev);
2267
2268 extern int drm_bridge_add(struct drm_bridge *bridge);
2269 extern void drm_bridge_remove(struct drm_bridge *bridge);
2270 extern struct drm_bridge *of_drm_find_bridge(struct device_node *np);
2271 extern int drm_bridge_attach(struct drm_device *dev, struct drm_bridge *bridge);
2272
2273 bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
2274                         const struct drm_display_mode *mode,
2275                         struct drm_display_mode *adjusted_mode);
2276 void drm_bridge_disable(struct drm_bridge *bridge);
2277 void drm_bridge_post_disable(struct drm_bridge *bridge);
2278 void drm_bridge_mode_set(struct drm_bridge *bridge,
2279                         struct drm_display_mode *mode,
2280                         struct drm_display_mode *adjusted_mode);
2281 void drm_bridge_pre_enable(struct drm_bridge *bridge);
2282 void drm_bridge_enable(struct drm_bridge *bridge);
2283
2284 extern __printf(5, 6)
2285 int drm_encoder_init(struct drm_device *dev,
2286                      struct drm_encoder *encoder,
2287                      const struct drm_encoder_funcs *funcs,
2288                      int encoder_type, const char *name, ...);
2289 extern unsigned int drm_encoder_index(struct drm_encoder *encoder);
2290
2291 /**
2292  * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
2293  * @encoder: encoder to test
2294  * @crtc: crtc to test
2295  *
2296  * Return false if @encoder can't be driven by @crtc, true otherwise.
2297  */
2298 static inline bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
2299                                        struct drm_crtc *crtc)
2300 {
2301         return !!(encoder->possible_crtcs & drm_crtc_mask(crtc));
2302 }
2303
2304 extern __printf(8, 9)
2305 int drm_universal_plane_init(struct drm_device *dev,
2306                              struct drm_plane *plane,
2307                              unsigned long possible_crtcs,
2308                              const struct drm_plane_funcs *funcs,
2309                              const uint32_t *formats,
2310                              unsigned int format_count,
2311                              enum drm_plane_type type,
2312                              const char *name, ...);
2313 extern int drm_plane_init(struct drm_device *dev,
2314                           struct drm_plane *plane,
2315                           unsigned long possible_crtcs,
2316                           const struct drm_plane_funcs *funcs,
2317                           const uint32_t *formats, unsigned int format_count,
2318                           bool is_primary);
2319 extern void drm_plane_cleanup(struct drm_plane *plane);
2320 extern unsigned int drm_plane_index(struct drm_plane *plane);
2321 extern struct drm_plane * drm_plane_from_index(struct drm_device *dev, int idx);
2322 extern void drm_plane_force_disable(struct drm_plane *plane);
2323 extern int drm_plane_check_pixel_format(const struct drm_plane *plane,
2324                                         u32 format);
2325 extern void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2326                                    int *hdisplay, int *vdisplay);
2327 extern int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2328                                    int x, int y,
2329                                    const struct drm_display_mode *mode,
2330                                    const struct drm_framebuffer *fb);
2331
2332 extern void drm_encoder_cleanup(struct drm_encoder *encoder);
2333
2334 extern const char *drm_get_connector_status_name(enum drm_connector_status status);
2335 extern const char *drm_get_subpixel_order_name(enum subpixel_order order);
2336 extern const char *drm_get_dpms_name(int val);
2337 extern const char *drm_get_dvi_i_subconnector_name(int val);
2338 extern const char *drm_get_dvi_i_select_name(int val);
2339 extern const char *drm_get_tv_subconnector_name(int val);
2340 extern const char *drm_get_tv_select_name(int val);
2341 extern void drm_fb_release(struct drm_file *file_priv);
2342 extern void drm_property_destroy_user_blobs(struct drm_device *dev,
2343                                             struct drm_file *file_priv);
2344 extern bool drm_probe_ddc(struct i2c_adapter *adapter);
2345 extern struct edid *drm_get_edid(struct drm_connector *connector,
2346                                  struct i2c_adapter *adapter);
2347 extern struct edid *drm_get_edid_switcheroo(struct drm_connector *connector,
2348                                             struct i2c_adapter *adapter);
2349 extern struct edid *drm_edid_duplicate(const struct edid *edid);
2350 extern int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid);
2351 extern void drm_mode_config_init(struct drm_device *dev);
2352 extern void drm_mode_config_reset(struct drm_device *dev);
2353 extern void drm_mode_config_cleanup(struct drm_device *dev);
2354
2355 extern int drm_mode_connector_set_path_property(struct drm_connector *connector,
2356                                                 const char *path);
2357 int drm_mode_connector_set_tile_property(struct drm_connector *connector);
2358 extern int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2359                                                    const struct edid *edid);
2360
2361 extern int drm_display_info_set_bus_formats(struct drm_display_info *info,
2362                                             const u32 *formats,
2363                                             unsigned int num_formats);
2364
2365 static inline bool drm_property_type_is(struct drm_property *property,
2366                 uint32_t type)
2367 {
2368         /* instanceof for props.. handles extended type vs original types: */
2369         if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
2370                 return (property->flags & DRM_MODE_PROP_EXTENDED_TYPE) == type;
2371         return property->flags & type;
2372 }
2373
2374 static inline bool drm_property_type_valid(struct drm_property *property)
2375 {
2376         if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
2377                 return !(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
2378         return !!(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
2379 }
2380
2381 extern int drm_object_property_set_value(struct drm_mode_object *obj,
2382                                          struct drm_property *property,
2383                                          uint64_t val);
2384 extern int drm_object_property_get_value(struct drm_mode_object *obj,
2385                                          struct drm_property *property,
2386                                          uint64_t *value);
2387 extern int drm_framebuffer_init(struct drm_device *dev,
2388                                 struct drm_framebuffer *fb,
2389                                 const struct drm_framebuffer_funcs *funcs);
2390 extern struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
2391                                                       uint32_t id);
2392 extern void drm_framebuffer_remove(struct drm_framebuffer *fb);
2393 extern void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
2394 extern void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
2395
2396 extern void drm_object_attach_property(struct drm_mode_object *obj,
2397                                        struct drm_property *property,
2398                                        uint64_t init_val);
2399 extern struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2400                                                 const char *name, int num_values);
2401 extern struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2402                                          const char *name,
2403                                          const struct drm_prop_enum_list *props,
2404                                          int num_values);
2405 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2406                                          int flags, const char *name,
2407                                          const struct drm_prop_enum_list *props,
2408                                          int num_props,
2409                                          uint64_t supported_bits);
2410 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2411                                          const char *name,
2412                                          uint64_t min, uint64_t max);
2413 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
2414                                          int flags, const char *name,
2415                                          int64_t min, int64_t max);
2416 struct drm_property *drm_property_create_object(struct drm_device *dev,
2417                                          int flags, const char *name, uint32_t type);
2418 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
2419                                          const char *name);
2420 struct drm_property_blob *drm_property_create_blob(struct drm_device *dev,
2421                                                    size_t length,
2422                                                    const void *data);
2423 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
2424                                                    uint32_t id);
2425 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob);
2426 void drm_property_unreference_blob(struct drm_property_blob *blob);
2427 extern void drm_property_destroy(struct drm_device *dev, struct drm_property *property);
2428 extern int drm_property_add_enum(struct drm_property *property, int index,
2429                                  uint64_t value, const char *name);
2430 extern int drm_mode_create_dvi_i_properties(struct drm_device *dev);
2431 extern int drm_mode_create_tv_properties(struct drm_device *dev,
2432                                          unsigned int num_modes,
2433                                          const char * const modes[]);
2434 extern int drm_mode_create_scaling_mode_property(struct drm_device *dev);
2435 extern int drm_mode_create_aspect_ratio_property(struct drm_device *dev);
2436 extern int drm_mode_create_dirty_info_property(struct drm_device *dev);
2437 extern int drm_mode_create_suggested_offset_properties(struct drm_device *dev);
2438 extern bool drm_property_change_valid_get(struct drm_property *property,
2439                                          uint64_t value, struct drm_mode_object **ref);
2440 extern void drm_property_change_valid_put(struct drm_property *property,
2441                 struct drm_mode_object *ref);
2442
2443 extern int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2444                                              struct drm_encoder *encoder);
2445 extern int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2446                                          int gamma_size);
2447 extern struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
2448                 uint32_t id, uint32_t type);
2449 void drm_mode_object_reference(struct drm_mode_object *obj);
2450 void drm_mode_object_unreference(struct drm_mode_object *obj);
2451
2452 /* IOCTLs */
2453 extern int drm_mode_getresources(struct drm_device *dev,
2454                                  void *data, struct drm_file *file_priv);
2455 extern int drm_mode_getplane_res(struct drm_device *dev, void *data,
2456                                    struct drm_file *file_priv);
2457 extern int drm_mode_getcrtc(struct drm_device *dev,
2458                             void *data, struct drm_file *file_priv);
2459 extern int drm_mode_getconnector(struct drm_device *dev,
2460                               void *data, struct drm_file *file_priv);
2461 extern int drm_mode_set_config_internal(struct drm_mode_set *set);
2462 extern int drm_mode_setcrtc(struct drm_device *dev,
2463                             void *data, struct drm_file *file_priv);
2464 extern int drm_mode_getplane(struct drm_device *dev,
2465                                void *data, struct drm_file *file_priv);
2466 extern int drm_mode_setplane(struct drm_device *dev,
2467                                void *data, struct drm_file *file_priv);
2468 extern int drm_mode_cursor_ioctl(struct drm_device *dev,
2469                                 void *data, struct drm_file *file_priv);
2470 extern int drm_mode_cursor2_ioctl(struct drm_device *dev,
2471                                 void *data, struct drm_file *file_priv);
2472 extern int drm_mode_addfb(struct drm_device *dev,
2473                           void *data, struct drm_file *file_priv);
2474 extern int drm_mode_addfb2(struct drm_device *dev,
2475                            void *data, struct drm_file *file_priv);
2476 extern uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth);
2477 extern int drm_mode_rmfb(struct drm_device *dev,
2478                          void *data, struct drm_file *file_priv);
2479 extern int drm_mode_getfb(struct drm_device *dev,
2480                           void *data, struct drm_file *file_priv);
2481 extern int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2482                                   void *data, struct drm_file *file_priv);
2483
2484 extern int drm_mode_getproperty_ioctl(struct drm_device *dev,
2485                                       void *data, struct drm_file *file_priv);
2486 extern int drm_mode_getblob_ioctl(struct drm_device *dev,
2487                                   void *data, struct drm_file *file_priv);
2488 extern int drm_mode_createblob_ioctl(struct drm_device *dev,
2489                                      void *data, struct drm_file *file_priv);
2490 extern int drm_mode_destroyblob_ioctl(struct drm_device *dev,
2491                                       void *data, struct drm_file *file_priv);
2492 extern int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2493                                               void *data, struct drm_file *file_priv);
2494 extern int drm_mode_getencoder(struct drm_device *dev,
2495                                void *data, struct drm_file *file_priv);
2496 extern int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2497                                     void *data, struct drm_file *file_priv);
2498 extern int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2499                                     void *data, struct drm_file *file_priv);
2500 extern u8 drm_match_cea_mode(const struct drm_display_mode *to_match);
2501 extern enum hdmi_picture_aspect drm_get_cea_aspect_ratio(const u8 video_code);
2502 extern bool drm_detect_hdmi_monitor(struct edid *edid);
2503 extern bool drm_detect_monitor_audio(struct edid *edid);
2504 extern bool drm_rgb_quant_range_selectable(struct edid *edid);
2505 extern int drm_mode_page_flip_ioctl(struct drm_device *dev,
2506                                     void *data, struct drm_file *file_priv);
2507 extern int drm_add_modes_noedid(struct drm_connector *connector,
2508                                 int hdisplay, int vdisplay);
2509 extern void drm_set_preferred_mode(struct drm_connector *connector,
2510                                    int hpref, int vpref);
2511
2512 extern int drm_edid_header_is_valid(const u8 *raw_edid);
2513 extern bool drm_edid_block_valid(u8 *raw_edid, int block, bool print_bad_edid,
2514                                  bool *edid_corrupt);
2515 extern bool drm_edid_is_valid(struct edid *edid);
2516 extern void drm_edid_get_monitor_name(struct edid *edid, char *name,
2517                                       int buflen);
2518
2519 extern struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
2520                                                          char topology[8]);
2521 extern struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
2522                                                char topology[8]);
2523 extern void drm_mode_put_tile_group(struct drm_device *dev,
2524                                    struct drm_tile_group *tg);
2525 struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
2526                                            int hsize, int vsize, int fresh,
2527                                            bool rb);
2528
2529 extern int drm_mode_create_dumb_ioctl(struct drm_device *dev,
2530                                       void *data, struct drm_file *file_priv);
2531 extern int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
2532                                     void *data, struct drm_file *file_priv);
2533 extern int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
2534                                       void *data, struct drm_file *file_priv);
2535 extern int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
2536                                              struct drm_file *file_priv);
2537 extern int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
2538                                            struct drm_file *file_priv);
2539 extern int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
2540                                        struct drm_property *property,
2541                                        uint64_t value);
2542 extern int drm_mode_atomic_ioctl(struct drm_device *dev,
2543                                  void *data, struct drm_file *file_priv);
2544
2545 extern void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
2546                                  int *bpp);
2547 extern int drm_format_num_planes(uint32_t format);
2548 extern int drm_format_plane_cpp(uint32_t format, int plane);
2549 extern int drm_format_horz_chroma_subsampling(uint32_t format);
2550 extern int drm_format_vert_chroma_subsampling(uint32_t format);
2551 extern int drm_format_plane_width(int width, uint32_t format, int plane);
2552 extern int drm_format_plane_height(int height, uint32_t format, int plane);
2553 extern const char *drm_get_format_name(uint32_t format);
2554 extern struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
2555                                                               unsigned int supported_rotations);
2556 extern unsigned int drm_rotation_simplify(unsigned int rotation,
2557                                           unsigned int supported_rotations);
2558
2559 /* Helpers */
2560
2561 static inline struct drm_plane *drm_plane_find(struct drm_device *dev,
2562                 uint32_t id)
2563 {
2564         struct drm_mode_object *mo;
2565         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PLANE);
2566         return mo ? obj_to_plane(mo) : NULL;
2567 }
2568
2569 static inline struct drm_crtc *drm_crtc_find(struct drm_device *dev,
2570         uint32_t id)
2571 {
2572         struct drm_mode_object *mo;
2573         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_CRTC);
2574         return mo ? obj_to_crtc(mo) : NULL;
2575 }
2576
2577 static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,
2578         uint32_t id)
2579 {
2580         struct drm_mode_object *mo;
2581         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
2582         return mo ? obj_to_encoder(mo) : NULL;
2583 }
2584
2585 /**
2586  * drm_connector_lookup - lookup connector object
2587  * @dev: DRM device
2588  * @id: connector object id
2589  *
2590  * This function looks up the connector object specified by id
2591  * add takes a reference to it.
2592  */
2593 static inline struct drm_connector *drm_connector_lookup(struct drm_device *dev,
2594                 uint32_t id)
2595 {
2596         struct drm_mode_object *mo;
2597         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_CONNECTOR);
2598         return mo ? obj_to_connector(mo) : NULL;
2599 }
2600
2601 static inline struct drm_property *drm_property_find(struct drm_device *dev,
2602                 uint32_t id)
2603 {
2604         struct drm_mode_object *mo;
2605         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PROPERTY);
2606         return mo ? obj_to_property(mo) : NULL;
2607 }
2608
2609 /*
2610  * Extract a degamma/gamma LUT value provided by user and round it to the
2611  * precision supported by the hardware.
2612  */
2613 static inline uint32_t drm_color_lut_extract(uint32_t user_input,
2614                                              uint32_t bit_precision)
2615 {
2616         uint32_t val = user_input;
2617         uint32_t max = 0xffff >> (16 - bit_precision);
2618
2619         /* Round only if we're not using full precision. */
2620         if (bit_precision < 16) {
2621                 val += 1UL << (16 - bit_precision - 1);
2622                 val >>= 16 - bit_precision;
2623         }
2624
2625         return clamp_val(val, 0, max);
2626 }
2627
2628 /**
2629  * drm_framebuffer_reference - incr the fb refcnt
2630  * @fb: framebuffer
2631  *
2632  * This functions increments the fb's refcount.
2633  */
2634 static inline void drm_framebuffer_reference(struct drm_framebuffer *fb)
2635 {
2636         drm_mode_object_reference(&fb->base);
2637 }
2638
2639 /**
2640  * drm_framebuffer_unreference - unref a framebuffer
2641  * @fb: framebuffer to unref
2642  *
2643  * This functions decrements the fb's refcount and frees it if it drops to zero.
2644  */
2645 static inline void drm_framebuffer_unreference(struct drm_framebuffer *fb)
2646 {
2647         drm_mode_object_unreference(&fb->base);
2648 }
2649
2650 /**
2651  * drm_framebuffer_read_refcount - read the framebuffer reference count.
2652  * @fb: framebuffer
2653  *
2654  * This functions returns the framebuffer's reference count.
2655  */
2656 static inline uint32_t drm_framebuffer_read_refcount(struct drm_framebuffer *fb)
2657 {
2658         return atomic_read(&fb->base.refcount.refcount);
2659 }
2660
2661 /**
2662  * drm_connector_reference - incr the connector refcnt
2663  * @connector: connector
2664  *
2665  * This function increments the connector's refcount.
2666  */
2667 static inline void drm_connector_reference(struct drm_connector *connector)
2668 {
2669         drm_mode_object_reference(&connector->base);
2670 }
2671
2672 /**
2673  * drm_connector_unreference - unref a connector
2674  * @connector: connector to unref
2675  *
2676  * This function decrements the connector's refcount and frees it if it drops to zero.
2677  */
2678 static inline void drm_connector_unreference(struct drm_connector *connector)
2679 {
2680         drm_mode_object_unreference(&connector->base);
2681 }
2682
2683 /* Plane list iterator for legacy (overlay only) planes. */
2684 #define drm_for_each_legacy_plane(plane, dev) \
2685         list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) \
2686                 for_each_if (plane->type == DRM_PLANE_TYPE_OVERLAY)
2687
2688 #define drm_for_each_plane(plane, dev) \
2689         list_for_each_entry(plane, &(dev)->mode_config.plane_list, head)
2690
2691 #define drm_for_each_crtc(crtc, dev) \
2692         list_for_each_entry(crtc, &(dev)->mode_config.crtc_list, head)
2693
2694 static inline void
2695 assert_drm_connector_list_read_locked(struct drm_mode_config *mode_config)
2696 {
2697         /*
2698          * The connector hotadd/remove code currently grabs both locks when
2699          * updating lists. Hence readers need only hold either of them to be
2700          * safe and the check amounts to
2701          *
2702          * WARN_ON(not_holding(A) && not_holding(B)).
2703          */
2704         WARN_ON(!mutex_is_locked(&mode_config->mutex) &&
2705                 !drm_modeset_is_locked(&mode_config->connection_mutex));
2706 }
2707
2708 #define drm_for_each_connector(connector, dev) \
2709         for (assert_drm_connector_list_read_locked(&(dev)->mode_config),        \
2710              connector = list_first_entry(&(dev)->mode_config.connector_list,   \
2711                                           struct drm_connector, head);          \
2712              &connector->head != (&(dev)->mode_config.connector_list);          \
2713              connector = list_next_entry(connector, head))
2714
2715 #define drm_for_each_encoder(encoder, dev) \
2716         list_for_each_entry(encoder, &(dev)->mode_config.encoder_list, head)
2717
2718 #define drm_for_each_fb(fb, dev) \
2719         for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)),            \
2720              fb = list_first_entry(&(dev)->mode_config.fb_list, \
2721                                           struct drm_framebuffer, head);        \
2722              &fb->head != (&(dev)->mode_config.fb_list);                        \
2723              fb = list_next_entry(fb, head))
2724
2725 #endif /* __DRM_CRTC_H__ */