xref: /openbmc/linux/include/drm/drm_crtc.h (revision 1cac4f26)
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/fb.h>
32 #include <linux/hdmi.h>
33 #include <linux/media-bus-format.h>
34 #include <uapi/drm/drm_mode.h>
35 #include <uapi/drm/drm_fourcc.h>
36 #include <drm/drm_modeset_lock.h>
37 #include <drm/drm_rect.h>
38 #include <drm/drm_mode_object.h>
39 #include <drm/drm_framebuffer.h>
40 #include <drm/drm_modes.h>
41 #include <drm/drm_connector.h>
42 #include <drm/drm_property.h>
43 #include <drm/drm_bridge.h>
44 #include <drm/drm_edid.h>
45 #include <drm/drm_plane.h>
46 #include <drm/drm_blend.h>
47 #include <drm/drm_color_mgmt.h>
48 #include <drm/drm_debugfs_crc.h>
49 #include <drm/drm_mode_config.h>
50 
51 struct drm_device;
52 struct drm_mode_set;
53 struct drm_file;
54 struct drm_clip_rect;
55 struct drm_printer;
56 struct device_node;
57 struct dma_fence;
58 struct edid;
59 
60 static inline int64_t U642I64(uint64_t val)
61 {
62 	return (int64_t)*((int64_t *)&val);
63 }
64 static inline uint64_t I642U64(int64_t val)
65 {
66 	return (uint64_t)*((uint64_t *)&val);
67 }
68 
69 struct drm_crtc;
70 struct drm_pending_vblank_event;
71 struct drm_plane;
72 struct drm_bridge;
73 struct drm_atomic_state;
74 
75 struct drm_crtc_helper_funcs;
76 struct drm_plane_helper_funcs;
77 
78 /**
79  * struct drm_crtc_state - mutable CRTC state
80  *
81  * Note that the distinction between @enable and @active is rather subtile:
82  * Flipping @active while @enable is set without changing anything else may
83  * never return in a failure from the &drm_mode_config_funcs.atomic_check
84  * callback. Userspace assumes that a DPMS On will always succeed. In other
85  * words: @enable controls resource assignment, @active controls the actual
86  * hardware state.
87  *
88  * The three booleans active_changed, connectors_changed and mode_changed are
89  * intended to indicate whether a full modeset is needed, rather than strictly
90  * describing what has changed in a commit. See also:
91  * drm_atomic_crtc_needs_modeset()
92  *
93  * WARNING: Transitional helpers (like drm_helper_crtc_mode_set() or
94  * drm_helper_crtc_mode_set_base()) do not maintain many of the derived control
95  * state like @plane_mask so drivers not converted over to atomic helpers should
96  * not rely on these being accurate!
97  */
98 struct drm_crtc_state {
99 	/** @crtc: backpointer to the CRTC */
100 	struct drm_crtc *crtc;
101 
102 	/**
103 	 * @enable: Whether the CRTC should be enabled, gates all other state.
104 	 * This controls reservations of shared resources. Actual hardware state
105 	 * is controlled by @active.
106 	 */
107 	bool enable;
108 
109 	/**
110 	 * @active: Whether the CRTC is actively displaying (used for DPMS).
111 	 * Implies that @enable is set. The driver must not release any shared
112 	 * resources if @active is set to false but @enable still true, because
113 	 * userspace expects that a DPMS ON always succeeds.
114 	 *
115 	 * Hence drivers must not consult @active in their various
116 	 * &drm_mode_config_funcs.atomic_check callback to reject an atomic
117 	 * commit. They can consult it to aid in the computation of derived
118 	 * hardware state, since even in the DPMS OFF state the display hardware
119 	 * should be as much powered down as when the CRTC is completely
120 	 * disabled through setting @enable to false.
121 	 */
122 	bool active;
123 
124 	/**
125 	 * @planes_changed: Planes on this crtc are updated. Used by the atomic
126 	 * helpers and drivers to steer the atomic commit control flow.
127 	 */
128 	bool planes_changed : 1;
129 
130 	/**
131 	 * @mode_changed: @mode or @enable has been changed. Used by the atomic
132 	 * helpers and drivers to steer the atomic commit control flow. See also
133 	 * drm_atomic_crtc_needs_modeset().
134 	 *
135 	 * Drivers are supposed to set this for any CRTC state changes that
136 	 * require a full modeset. They can also reset it to false if e.g. a
137 	 * @mode change can be done without a full modeset by only changing
138 	 * scaler settings.
139 	 */
140 	bool mode_changed : 1;
141 
142 	/**
143 	 * @active_changed: @active has been toggled. Used by the atomic
144 	 * helpers and drivers to steer the atomic commit control flow. See also
145 	 * drm_atomic_crtc_needs_modeset().
146 	 */
147 	bool active_changed : 1;
148 
149 	/**
150 	 * @connectors_changed: Connectors to this crtc have been updated,
151 	 * either in their state or routing. Used by the atomic
152 	 * helpers and drivers to steer the atomic commit control flow. See also
153 	 * drm_atomic_crtc_needs_modeset().
154 	 *
155 	 * Drivers are supposed to set this as-needed from their own atomic
156 	 * check code, e.g. from &drm_encoder_helper_funcs.atomic_check
157 	 */
158 	bool connectors_changed : 1;
159 	/**
160 	 * @zpos_changed: zpos values of planes on this crtc have been updated.
161 	 * Used by the atomic helpers and drivers to steer the atomic commit
162 	 * control flow.
163 	 */
164 	bool zpos_changed : 1;
165 	/**
166 	 * @color_mgmt_changed: Color management properties have changed
167 	 * (@gamma_lut, @degamma_lut or @ctm). Used by the atomic helpers and
168 	 * drivers to steer the atomic commit control flow.
169 	 */
170 	bool color_mgmt_changed : 1;
171 
172 	/**
173 	 * @no_vblank:
174 	 *
175 	 * Reflects the ability of a CRTC to send VBLANK events. This state
176 	 * usually depends on the pipeline configuration, and the main usuage
177 	 * is CRTCs feeding a writeback connector operating in oneshot mode.
178 	 * In this case the VBLANK event is only generated when a job is queued
179 	 * to the writeback connector, and we want the core to fake VBLANK
180 	 * events when this part of the pipeline hasn't changed but others had
181 	 * or when the CRTC and connectors are being disabled.
182 	 *
183 	 * __drm_atomic_helper_crtc_duplicate_state() will not reset the value
184 	 * from the current state, the CRTC driver is then responsible for
185 	 * updating this field when needed.
186 	 *
187 	 * Note that the combination of &drm_crtc_state.event == NULL and
188 	 * &drm_crtc_state.no_blank == true is valid and usually used when the
189 	 * writeback connector attached to the CRTC has a new job queued. In
190 	 * this case the driver will send the VBLANK event on its own when the
191 	 * writeback job is complete.
192 	 */
193 	bool no_vblank : 1;
194 
195 	/**
196 	 * @plane_mask: Bitmask of drm_plane_mask(plane) of planes attached to
197 	 * this CRTC.
198 	 */
199 	u32 plane_mask;
200 
201 	/**
202 	 * @connector_mask: Bitmask of drm_connector_mask(connector) of
203 	 * connectors attached to this CRTC.
204 	 */
205 	u32 connector_mask;
206 
207 	/**
208 	 * @encoder_mask: Bitmask of drm_encoder_mask(encoder) of encoders
209 	 * attached to this CRTC.
210 	 */
211 	u32 encoder_mask;
212 
213 	/**
214 	 * @adjusted_mode:
215 	 *
216 	 * Internal display timings which can be used by the driver to handle
217 	 * differences between the mode requested by userspace in @mode and what
218 	 * is actually programmed into the hardware.
219 	 *
220 	 * For drivers using &drm_bridge, this stores hardware display timings
221 	 * used between the CRTC and the first bridge. For other drivers, the
222 	 * meaning of the adjusted_mode field is purely driver implementation
223 	 * defined information, and will usually be used to store the hardware
224 	 * display timings used between the CRTC and encoder blocks.
225 	 */
226 	struct drm_display_mode adjusted_mode;
227 
228 	/**
229 	 * @mode:
230 	 *
231 	 * Display timings requested by userspace. The driver should try to
232 	 * match the refresh rate as close as possible (but note that it's
233 	 * undefined what exactly is close enough, e.g. some of the HDMI modes
234 	 * only differ in less than 1% of the refresh rate). The active width
235 	 * and height as observed by userspace for positioning planes must match
236 	 * exactly.
237 	 *
238 	 * For external connectors where the sink isn't fixed (like with a
239 	 * built-in panel), this mode here should match the physical mode on the
240 	 * wire to the last details (i.e. including sync polarities and
241 	 * everything).
242 	 */
243 	struct drm_display_mode mode;
244 
245 	/**
246 	 * @mode_blob: &drm_property_blob for @mode, for exposing the mode to
247 	 * atomic userspace.
248 	 */
249 	struct drm_property_blob *mode_blob;
250 
251 	/**
252 	 * @degamma_lut:
253 	 *
254 	 * Lookup table for converting framebuffer pixel data before apply the
255 	 * color conversion matrix @ctm. See drm_crtc_enable_color_mgmt(). The
256 	 * blob (if not NULL) is an array of &struct drm_color_lut.
257 	 */
258 	struct drm_property_blob *degamma_lut;
259 
260 	/**
261 	 * @ctm:
262 	 *
263 	 * Color transformation matrix. See drm_crtc_enable_color_mgmt(). The
264 	 * blob (if not NULL) is a &struct drm_color_ctm.
265 	 */
266 	struct drm_property_blob *ctm;
267 
268 	/**
269 	 * @gamma_lut:
270 	 *
271 	 * Lookup table for converting pixel data after the color conversion
272 	 * matrix @ctm.  See drm_crtc_enable_color_mgmt(). The blob (if not
273 	 * NULL) is an array of &struct drm_color_lut.
274 	 */
275 	struct drm_property_blob *gamma_lut;
276 
277 	/**
278 	 * @target_vblank:
279 	 *
280 	 * Target vertical blank period when a page flip
281 	 * should take effect.
282 	 */
283 	u32 target_vblank;
284 
285 	/**
286 	 * @pageflip_flags:
287 	 *
288 	 * DRM_MODE_PAGE_FLIP_* flags, as passed to the page flip ioctl.
289 	 * Zero in any other case.
290 	 */
291 	u32 pageflip_flags;
292 
293 	/**
294 	 * @vrr_enabled:
295 	 *
296 	 * Indicates if variable refresh rate should be enabled for the CRTC.
297 	 * Support for the requested vrr state will depend on driver and
298 	 * hardware capabiltiy - lacking support is not treated as failure.
299 	 */
300 	bool vrr_enabled;
301 
302 	/**
303 	 * @event:
304 	 *
305 	 * Optional pointer to a DRM event to signal upon completion of the
306 	 * state update. The driver must send out the event when the atomic
307 	 * commit operation completes. There are two cases:
308 	 *
309 	 *  - The event is for a CRTC which is being disabled through this
310 	 *    atomic commit. In that case the event can be send out any time
311 	 *    after the hardware has stopped scanning out the current
312 	 *    framebuffers. It should contain the timestamp and counter for the
313 	 *    last vblank before the display pipeline was shut off. The simplest
314 	 *    way to achieve that is calling drm_crtc_send_vblank_event()
315 	 *    somewhen after drm_crtc_vblank_off() has been called.
316 	 *
317 	 *  - For a CRTC which is enabled at the end of the commit (even when it
318 	 *    undergoes an full modeset) the vblank timestamp and counter must
319 	 *    be for the vblank right before the first frame that scans out the
320 	 *    new set of buffers. Again the event can only be sent out after the
321 	 *    hardware has stopped scanning out the old buffers.
322 	 *
323 	 *  - Events for disabled CRTCs are not allowed, and drivers can ignore
324 	 *    that case.
325 	 *
326 	 * This can be handled by the drm_crtc_send_vblank_event() function,
327 	 * which the driver should call on the provided event upon completion of
328 	 * the atomic commit. Note that if the driver supports vblank signalling
329 	 * and timestamping the vblank counters and timestamps must agree with
330 	 * the ones returned from page flip events. With the current vblank
331 	 * helper infrastructure this can be achieved by holding a vblank
332 	 * reference while the page flip is pending, acquired through
333 	 * drm_crtc_vblank_get() and released with drm_crtc_vblank_put().
334 	 * Drivers are free to implement their own vblank counter and timestamp
335 	 * tracking though, e.g. if they have accurate timestamp registers in
336 	 * hardware.
337 	 *
338 	 * For hardware which supports some means to synchronize vblank
339 	 * interrupt delivery with committing display state there's also
340 	 * drm_crtc_arm_vblank_event(). See the documentation of that function
341 	 * for a detailed discussion of the constraints it needs to be used
342 	 * safely.
343 	 *
344 	 * If the device can't notify of flip completion in a race-free way
345 	 * at all, then the event should be armed just after the page flip is
346 	 * committed. In the worst case the driver will send the event to
347 	 * userspace one frame too late. This doesn't allow for a real atomic
348 	 * update, but it should avoid tearing.
349 	 */
350 	struct drm_pending_vblank_event *event;
351 
352 	/**
353 	 * @commit:
354 	 *
355 	 * This tracks how the commit for this update proceeds through the
356 	 * various phases. This is never cleared, except when we destroy the
357 	 * state, so that subsequent commits can synchronize with previous ones.
358 	 */
359 	struct drm_crtc_commit *commit;
360 
361 	/** @state: backpointer to global drm_atomic_state */
362 	struct drm_atomic_state *state;
363 };
364 
365 /**
366  * struct drm_crtc_funcs - control CRTCs for a given device
367  *
368  * The drm_crtc_funcs structure is the central CRTC management structure
369  * in the DRM.  Each CRTC controls one or more connectors (note that the name
370  * CRTC is simply historical, a CRTC may control LVDS, VGA, DVI, TV out, etc.
371  * connectors, not just CRTs).
372  *
373  * Each driver is responsible for filling out this structure at startup time,
374  * in addition to providing other modesetting features, like i2c and DDC
375  * bus accessors.
376  */
377 struct drm_crtc_funcs {
378 	/**
379 	 * @reset:
380 	 *
381 	 * Reset CRTC hardware and software state to off. This function isn't
382 	 * called by the core directly, only through drm_mode_config_reset().
383 	 * It's not a helper hook only for historical reasons.
384 	 *
385 	 * Atomic drivers can use drm_atomic_helper_crtc_reset() to reset
386 	 * atomic state using this hook.
387 	 */
388 	void (*reset)(struct drm_crtc *crtc);
389 
390 	/**
391 	 * @cursor_set:
392 	 *
393 	 * Update the cursor image. The cursor position is relative to the CRTC
394 	 * and can be partially or fully outside of the visible area.
395 	 *
396 	 * Note that contrary to all other KMS functions the legacy cursor entry
397 	 * points don't take a framebuffer object, but instead take directly a
398 	 * raw buffer object id from the driver's buffer manager (which is
399 	 * either GEM or TTM for current drivers).
400 	 *
401 	 * This entry point is deprecated, drivers should instead implement
402 	 * universal plane support and register a proper cursor plane using
403 	 * drm_crtc_init_with_planes().
404 	 *
405 	 * This callback is optional
406 	 *
407 	 * RETURNS:
408 	 *
409 	 * 0 on success or a negative error code on failure.
410 	 */
411 	int (*cursor_set)(struct drm_crtc *crtc, struct drm_file *file_priv,
412 			  uint32_t handle, uint32_t width, uint32_t height);
413 
414 	/**
415 	 * @cursor_set2:
416 	 *
417 	 * Update the cursor image, including hotspot information. The hotspot
418 	 * must not affect the cursor position in CRTC coordinates, but is only
419 	 * meant as a hint for virtualized display hardware to coordinate the
420 	 * guests and hosts cursor position. The cursor hotspot is relative to
421 	 * the cursor image. Otherwise this works exactly like @cursor_set.
422 	 *
423 	 * This entry point is deprecated, drivers should instead implement
424 	 * universal plane support and register a proper cursor plane using
425 	 * drm_crtc_init_with_planes().
426 	 *
427 	 * This callback is optional.
428 	 *
429 	 * RETURNS:
430 	 *
431 	 * 0 on success or a negative error code on failure.
432 	 */
433 	int (*cursor_set2)(struct drm_crtc *crtc, struct drm_file *file_priv,
434 			   uint32_t handle, uint32_t width, uint32_t height,
435 			   int32_t hot_x, int32_t hot_y);
436 
437 	/**
438 	 * @cursor_move:
439 	 *
440 	 * Update the cursor position. The cursor does not need to be visible
441 	 * when this hook is called.
442 	 *
443 	 * This entry point is deprecated, drivers should instead implement
444 	 * universal plane support and register a proper cursor plane using
445 	 * drm_crtc_init_with_planes().
446 	 *
447 	 * This callback is optional.
448 	 *
449 	 * RETURNS:
450 	 *
451 	 * 0 on success or a negative error code on failure.
452 	 */
453 	int (*cursor_move)(struct drm_crtc *crtc, int x, int y);
454 
455 	/**
456 	 * @gamma_set:
457 	 *
458 	 * Set gamma on the CRTC.
459 	 *
460 	 * This callback is optional.
461 	 *
462 	 * Atomic drivers who want to support gamma tables should implement the
463 	 * atomic color management support, enabled by calling
464 	 * drm_crtc_enable_color_mgmt(), which then supports the legacy gamma
465 	 * interface through the drm_atomic_helper_legacy_gamma_set()
466 	 * compatibility implementation.
467 	 */
468 	int (*gamma_set)(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
469 			 uint32_t size,
470 			 struct drm_modeset_acquire_ctx *ctx);
471 
472 	/**
473 	 * @destroy:
474 	 *
475 	 * Clean up plane resources. This is only called at driver unload time
476 	 * through drm_mode_config_cleanup() since a CRTC cannot be hotplugged
477 	 * in DRM.
478 	 */
479 	void (*destroy)(struct drm_crtc *crtc);
480 
481 	/**
482 	 * @set_config:
483 	 *
484 	 * This is the main legacy entry point to change the modeset state on a
485 	 * CRTC. All the details of the desired configuration are passed in a
486 	 * &struct drm_mode_set - see there for details.
487 	 *
488 	 * Drivers implementing atomic modeset should use
489 	 * drm_atomic_helper_set_config() to implement this hook.
490 	 *
491 	 * RETURNS:
492 	 *
493 	 * 0 on success or a negative error code on failure.
494 	 */
495 	int (*set_config)(struct drm_mode_set *set,
496 			  struct drm_modeset_acquire_ctx *ctx);
497 
498 	/**
499 	 * @page_flip:
500 	 *
501 	 * Legacy entry point to schedule a flip to the given framebuffer.
502 	 *
503 	 * Page flipping is a synchronization mechanism that replaces the frame
504 	 * buffer being scanned out by the CRTC with a new frame buffer during
505 	 * vertical blanking, avoiding tearing (except when requested otherwise
506 	 * through the DRM_MODE_PAGE_FLIP_ASYNC flag). When an application
507 	 * requests a page flip the DRM core verifies that the new frame buffer
508 	 * is large enough to be scanned out by the CRTC in the currently
509 	 * configured mode and then calls this hook with a pointer to the new
510 	 * frame buffer.
511 	 *
512 	 * The driver must wait for any pending rendering to the new framebuffer
513 	 * to complete before executing the flip. It should also wait for any
514 	 * pending rendering from other drivers if the underlying buffer is a
515 	 * shared dma-buf.
516 	 *
517 	 * An application can request to be notified when the page flip has
518 	 * completed. The drm core will supply a &struct drm_event in the event
519 	 * parameter in this case. This can be handled by the
520 	 * drm_crtc_send_vblank_event() function, which the driver should call on
521 	 * the provided event upon completion of the flip. Note that if
522 	 * the driver supports vblank signalling and timestamping the vblank
523 	 * counters and timestamps must agree with the ones returned from page
524 	 * flip events. With the current vblank helper infrastructure this can
525 	 * be achieved by holding a vblank reference while the page flip is
526 	 * pending, acquired through drm_crtc_vblank_get() and released with
527 	 * drm_crtc_vblank_put(). Drivers are free to implement their own vblank
528 	 * counter and timestamp tracking though, e.g. if they have accurate
529 	 * timestamp registers in hardware.
530 	 *
531 	 * This callback is optional.
532 	 *
533 	 * NOTE:
534 	 *
535 	 * Very early versions of the KMS ABI mandated that the driver must
536 	 * block (but not reject) any rendering to the old framebuffer until the
537 	 * flip operation has completed and the old framebuffer is no longer
538 	 * visible. This requirement has been lifted, and userspace is instead
539 	 * expected to request delivery of an event and wait with recycling old
540 	 * buffers until such has been received.
541 	 *
542 	 * RETURNS:
543 	 *
544 	 * 0 on success or a negative error code on failure. Note that if a
545 	 * page flip operation is already pending the callback should return
546 	 * -EBUSY. Pageflips on a disabled CRTC (either by setting a NULL mode
547 	 * or just runtime disabled through DPMS respectively the new atomic
548 	 * "ACTIVE" state) should result in an -EINVAL error code. Note that
549 	 * drm_atomic_helper_page_flip() checks this already for atomic drivers.
550 	 */
551 	int (*page_flip)(struct drm_crtc *crtc,
552 			 struct drm_framebuffer *fb,
553 			 struct drm_pending_vblank_event *event,
554 			 uint32_t flags,
555 			 struct drm_modeset_acquire_ctx *ctx);
556 
557 	/**
558 	 * @page_flip_target:
559 	 *
560 	 * Same as @page_flip but with an additional parameter specifying the
561 	 * absolute target vertical blank period (as reported by
562 	 * drm_crtc_vblank_count()) when the flip should take effect.
563 	 *
564 	 * Note that the core code calls drm_crtc_vblank_get before this entry
565 	 * point, and will call drm_crtc_vblank_put if this entry point returns
566 	 * any non-0 error code. It's the driver's responsibility to call
567 	 * drm_crtc_vblank_put after this entry point returns 0, typically when
568 	 * the flip completes.
569 	 */
570 	int (*page_flip_target)(struct drm_crtc *crtc,
571 				struct drm_framebuffer *fb,
572 				struct drm_pending_vblank_event *event,
573 				uint32_t flags, uint32_t target,
574 				struct drm_modeset_acquire_ctx *ctx);
575 
576 	/**
577 	 * @set_property:
578 	 *
579 	 * This is the legacy entry point to update a property attached to the
580 	 * CRTC.
581 	 *
582 	 * This callback is optional if the driver does not support any legacy
583 	 * driver-private properties. For atomic drivers it is not used because
584 	 * property handling is done entirely in the DRM core.
585 	 *
586 	 * RETURNS:
587 	 *
588 	 * 0 on success or a negative error code on failure.
589 	 */
590 	int (*set_property)(struct drm_crtc *crtc,
591 			    struct drm_property *property, uint64_t val);
592 
593 	/**
594 	 * @atomic_duplicate_state:
595 	 *
596 	 * Duplicate the current atomic state for this CRTC and return it.
597 	 * The core and helpers guarantee that any atomic state duplicated with
598 	 * this hook and still owned by the caller (i.e. not transferred to the
599 	 * driver by calling &drm_mode_config_funcs.atomic_commit) will be
600 	 * cleaned up by calling the @atomic_destroy_state hook in this
601 	 * structure.
602 	 *
603 	 * This callback is mandatory for atomic drivers.
604 	 *
605 	 * Atomic drivers which don't subclass &struct drm_crtc_state should use
606 	 * drm_atomic_helper_crtc_duplicate_state(). Drivers that subclass the
607 	 * state structure to extend it with driver-private state should use
608 	 * __drm_atomic_helper_crtc_duplicate_state() to make sure shared state is
609 	 * duplicated in a consistent fashion across drivers.
610 	 *
611 	 * It is an error to call this hook before &drm_crtc.state has been
612 	 * initialized correctly.
613 	 *
614 	 * NOTE:
615 	 *
616 	 * If the duplicate state references refcounted resources this hook must
617 	 * acquire a reference for each of them. The driver must release these
618 	 * references again in @atomic_destroy_state.
619 	 *
620 	 * RETURNS:
621 	 *
622 	 * Duplicated atomic state or NULL when the allocation failed.
623 	 */
624 	struct drm_crtc_state *(*atomic_duplicate_state)(struct drm_crtc *crtc);
625 
626 	/**
627 	 * @atomic_destroy_state:
628 	 *
629 	 * Destroy a state duplicated with @atomic_duplicate_state and release
630 	 * or unreference all resources it references
631 	 *
632 	 * This callback is mandatory for atomic drivers.
633 	 */
634 	void (*atomic_destroy_state)(struct drm_crtc *crtc,
635 				     struct drm_crtc_state *state);
636 
637 	/**
638 	 * @atomic_set_property:
639 	 *
640 	 * Decode a driver-private property value and store the decoded value
641 	 * into the passed-in state structure. Since the atomic core decodes all
642 	 * standardized properties (even for extensions beyond the core set of
643 	 * properties which might not be implemented by all drivers) this
644 	 * requires drivers to subclass the state structure.
645 	 *
646 	 * Such driver-private properties should really only be implemented for
647 	 * truly hardware/vendor specific state. Instead it is preferred to
648 	 * standardize atomic extension and decode the properties used to expose
649 	 * such an extension in the core.
650 	 *
651 	 * Do not call this function directly, use
652 	 * drm_atomic_crtc_set_property() instead.
653 	 *
654 	 * This callback is optional if the driver does not support any
655 	 * driver-private atomic properties.
656 	 *
657 	 * NOTE:
658 	 *
659 	 * This function is called in the state assembly phase of atomic
660 	 * modesets, which can be aborted for any reason (including on
661 	 * userspace's request to just check whether a configuration would be
662 	 * possible). Drivers MUST NOT touch any persistent state (hardware or
663 	 * software) or data structures except the passed in @state parameter.
664 	 *
665 	 * Also since userspace controls in which order properties are set this
666 	 * function must not do any input validation (since the state update is
667 	 * incomplete and hence likely inconsistent). Instead any such input
668 	 * validation must be done in the various atomic_check callbacks.
669 	 *
670 	 * RETURNS:
671 	 *
672 	 * 0 if the property has been found, -EINVAL if the property isn't
673 	 * implemented by the driver (which should never happen, the core only
674 	 * asks for properties attached to this CRTC). No other validation is
675 	 * allowed by the driver. The core already checks that the property
676 	 * value is within the range (integer, valid enum value, ...) the driver
677 	 * set when registering the property.
678 	 */
679 	int (*atomic_set_property)(struct drm_crtc *crtc,
680 				   struct drm_crtc_state *state,
681 				   struct drm_property *property,
682 				   uint64_t val);
683 	/**
684 	 * @atomic_get_property:
685 	 *
686 	 * Reads out the decoded driver-private property. This is used to
687 	 * implement the GETCRTC IOCTL.
688 	 *
689 	 * Do not call this function directly, use
690 	 * drm_atomic_crtc_get_property() instead.
691 	 *
692 	 * This callback is optional if the driver does not support any
693 	 * driver-private atomic properties.
694 	 *
695 	 * RETURNS:
696 	 *
697 	 * 0 on success, -EINVAL if the property isn't implemented by the
698 	 * driver (which should never happen, the core only asks for
699 	 * properties attached to this CRTC).
700 	 */
701 	int (*atomic_get_property)(struct drm_crtc *crtc,
702 				   const struct drm_crtc_state *state,
703 				   struct drm_property *property,
704 				   uint64_t *val);
705 
706 	/**
707 	 * @late_register:
708 	 *
709 	 * This optional hook can be used to register additional userspace
710 	 * interfaces attached to the crtc like debugfs interfaces.
711 	 * It is called late in the driver load sequence from drm_dev_register().
712 	 * Everything added from this callback should be unregistered in
713 	 * the early_unregister callback.
714 	 *
715 	 * Returns:
716 	 *
717 	 * 0 on success, or a negative error code on failure.
718 	 */
719 	int (*late_register)(struct drm_crtc *crtc);
720 
721 	/**
722 	 * @early_unregister:
723 	 *
724 	 * This optional hook should be used to unregister the additional
725 	 * userspace interfaces attached to the crtc from
726 	 * @late_register. It is called from drm_dev_unregister(),
727 	 * early in the driver unload sequence to disable userspace access
728 	 * before data structures are torndown.
729 	 */
730 	void (*early_unregister)(struct drm_crtc *crtc);
731 
732 	/**
733 	 * @set_crc_source:
734 	 *
735 	 * Changes the source of CRC checksums of frames at the request of
736 	 * userspace, typically for testing purposes. The sources available are
737 	 * specific of each driver and a %NULL value indicates that CRC
738 	 * generation is to be switched off.
739 	 *
740 	 * When CRC generation is enabled, the driver should call
741 	 * drm_crtc_add_crc_entry() at each frame, providing any information
742 	 * that characterizes the frame contents in the crcN arguments, as
743 	 * provided from the configured source. Drivers must accept an "auto"
744 	 * source name that will select a default source for this CRTC.
745 	 *
746 	 * Note that "auto" can depend upon the current modeset configuration,
747 	 * e.g. it could pick an encoder or output specific CRC sampling point.
748 	 *
749 	 * This callback is optional if the driver does not support any CRC
750 	 * generation functionality.
751 	 *
752 	 * RETURNS:
753 	 *
754 	 * 0 on success or a negative error code on failure.
755 	 */
756 	int (*set_crc_source)(struct drm_crtc *crtc, const char *source);
757 	/**
758 	 * @verify_crc_source:
759 	 *
760 	 * verifies the source of CRC checksums of frames before setting the
761 	 * source for CRC and during crc open. Source parameter can be NULL
762 	 * while disabling crc source.
763 	 *
764 	 * This callback is optional if the driver does not support any CRC
765 	 * generation functionality.
766 	 *
767 	 * RETURNS:
768 	 *
769 	 * 0 on success or a negative error code on failure.
770 	 */
771 	int (*verify_crc_source)(struct drm_crtc *crtc, const char *source,
772 				 size_t *values_cnt);
773 	/**
774 	 * @get_crc_sources:
775 	 *
776 	 * Driver callback for getting a list of all the available sources for
777 	 * CRC generation. This callback depends upon verify_crc_source, So
778 	 * verify_crc_source callback should be implemented before implementing
779 	 * this. Driver can pass full list of available crc sources, this
780 	 * callback does the verification on each crc-source before passing it
781 	 * to userspace.
782 	 *
783 	 * This callback is optional if the driver does not support exporting of
784 	 * possible CRC sources list.
785 	 *
786 	 * RETURNS:
787 	 *
788 	 * a constant character pointer to the list of all the available CRC
789 	 * sources. On failure driver should return NULL. count should be
790 	 * updated with number of sources in list. if zero we don't process any
791 	 * source from the list.
792 	 */
793 	const char *const *(*get_crc_sources)(struct drm_crtc *crtc,
794 					      size_t *count);
795 
796 	/**
797 	 * @atomic_print_state:
798 	 *
799 	 * If driver subclasses &struct drm_crtc_state, it should implement
800 	 * this optional hook for printing additional driver specific state.
801 	 *
802 	 * Do not call this directly, use drm_atomic_crtc_print_state()
803 	 * instead.
804 	 */
805 	void (*atomic_print_state)(struct drm_printer *p,
806 				   const struct drm_crtc_state *state);
807 
808 	/**
809 	 * @get_vblank_counter:
810 	 *
811 	 * Driver callback for fetching a raw hardware vblank counter for the
812 	 * CRTC. It's meant to be used by new drivers as the replacement of
813 	 * &drm_driver.get_vblank_counter hook.
814 	 *
815 	 * This callback is optional. If a device doesn't have a hardware
816 	 * counter, the driver can simply leave the hook as NULL. The DRM core
817 	 * will account for missed vblank events while interrupts where disabled
818 	 * based on system timestamps.
819 	 *
820 	 * Wraparound handling and loss of events due to modesetting is dealt
821 	 * with in the DRM core code, as long as drivers call
822 	 * drm_crtc_vblank_off() and drm_crtc_vblank_on() when disabling or
823 	 * enabling a CRTC.
824 	 *
825 	 * See also &drm_device.vblank_disable_immediate and
826 	 * &drm_device.max_vblank_count.
827 	 *
828 	 * Returns:
829 	 *
830 	 * Raw vblank counter value.
831 	 */
832 	u32 (*get_vblank_counter)(struct drm_crtc *crtc);
833 
834 	/**
835 	 * @enable_vblank:
836 	 *
837 	 * Enable vblank interrupts for the CRTC. It's meant to be used by
838 	 * new drivers as the replacement of &drm_driver.enable_vblank hook.
839 	 *
840 	 * Returns:
841 	 *
842 	 * Zero on success, appropriate errno if the vblank interrupt cannot
843 	 * be enabled.
844 	 */
845 	int (*enable_vblank)(struct drm_crtc *crtc);
846 
847 	/**
848 	 * @disable_vblank:
849 	 *
850 	 * Disable vblank interrupts for the CRTC. It's meant to be used by
851 	 * new drivers as the replacement of &drm_driver.disable_vblank hook.
852 	 */
853 	void (*disable_vblank)(struct drm_crtc *crtc);
854 };
855 
856 /**
857  * struct drm_crtc - central CRTC control structure
858  *
859  * Each CRTC may have one or more connectors associated with it.  This structure
860  * allows the CRTC to be controlled.
861  */
862 struct drm_crtc {
863 	/** @dev: parent DRM device */
864 	struct drm_device *dev;
865 	/** @port: OF node used by drm_of_find_possible_crtcs(). */
866 	struct device_node *port;
867 	/**
868 	 * @head:
869 	 *
870 	 * List of all CRTCs on @dev, linked from &drm_mode_config.crtc_list.
871 	 * Invariant over the lifetime of @dev and therefore does not need
872 	 * locking.
873 	 */
874 	struct list_head head;
875 
876 	/** @name: human readable name, can be overwritten by the driver */
877 	char *name;
878 
879 	/**
880 	 * @mutex:
881 	 *
882 	 * This provides a read lock for the overall CRTC state (mode, dpms
883 	 * state, ...) and a write lock for everything which can be update
884 	 * without a full modeset (fb, cursor data, CRTC properties ...). A full
885 	 * modeset also need to grab &drm_mode_config.connection_mutex.
886 	 *
887 	 * For atomic drivers specifically this protects @state.
888 	 */
889 	struct drm_modeset_lock mutex;
890 
891 	/** @base: base KMS object for ID tracking etc. */
892 	struct drm_mode_object base;
893 
894 	/**
895 	 * @primary:
896 	 * Primary plane for this CRTC. Note that this is only
897 	 * relevant for legacy IOCTL, it specifies the plane implicitly used by
898 	 * the SETCRTC and PAGE_FLIP IOCTLs. It does not have any significance
899 	 * beyond that.
900 	 */
901 	struct drm_plane *primary;
902 
903 	/**
904 	 * @cursor:
905 	 * Cursor plane for this CRTC. Note that this is only relevant for
906 	 * legacy IOCTL, it specifies the plane implicitly used by the SETCURSOR
907 	 * and SETCURSOR2 IOCTLs. It does not have any significance
908 	 * beyond that.
909 	 */
910 	struct drm_plane *cursor;
911 
912 	/**
913 	 * @index: Position inside the mode_config.list, can be used as an array
914 	 * index. It is invariant over the lifetime of the CRTC.
915 	 */
916 	unsigned index;
917 
918 	/**
919 	 * @cursor_x: Current x position of the cursor, used for universal
920 	 * cursor planes because the SETCURSOR IOCTL only can update the
921 	 * framebuffer without supplying the coordinates. Drivers should not use
922 	 * this directly, atomic drivers should look at &drm_plane_state.crtc_x
923 	 * of the cursor plane instead.
924 	 */
925 	int cursor_x;
926 	/**
927 	 * @cursor_y: Current y position of the cursor, used for universal
928 	 * cursor planes because the SETCURSOR IOCTL only can update the
929 	 * framebuffer without supplying the coordinates. Drivers should not use
930 	 * this directly, atomic drivers should look at &drm_plane_state.crtc_y
931 	 * of the cursor plane instead.
932 	 */
933 	int cursor_y;
934 
935 	/**
936 	 * @enabled:
937 	 *
938 	 * Is this CRTC enabled? Should only be used by legacy drivers, atomic
939 	 * drivers should instead consult &drm_crtc_state.enable and
940 	 * &drm_crtc_state.active. Atomic drivers can update this by calling
941 	 * drm_atomic_helper_update_legacy_modeset_state().
942 	 */
943 	bool enabled;
944 
945 	/**
946 	 * @mode:
947 	 *
948 	 * Current mode timings. Should only be used by legacy drivers, atomic
949 	 * drivers should instead consult &drm_crtc_state.mode. Atomic drivers
950 	 * can update this by calling
951 	 * drm_atomic_helper_update_legacy_modeset_state().
952 	 */
953 	struct drm_display_mode mode;
954 
955 	/**
956 	 * @hwmode:
957 	 *
958 	 * Programmed mode in hw, after adjustments for encoders, crtc, panel
959 	 * scaling etc. Should only be used by legacy drivers, for high
960 	 * precision vblank timestamps in
961 	 * drm_calc_vbltimestamp_from_scanoutpos().
962 	 *
963 	 * Note that atomic drivers should not use this, but instead use
964 	 * &drm_crtc_state.adjusted_mode. And for high-precision timestamps
965 	 * drm_calc_vbltimestamp_from_scanoutpos() used &drm_vblank_crtc.hwmode,
966 	 * which is filled out by calling drm_calc_timestamping_constants().
967 	 */
968 	struct drm_display_mode hwmode;
969 
970 	/**
971 	 * @x:
972 	 * x position on screen. Should only be used by legacy drivers, atomic
973 	 * drivers should look at &drm_plane_state.crtc_x of the primary plane
974 	 * instead. Updated by calling
975 	 * drm_atomic_helper_update_legacy_modeset_state().
976 	 */
977 	int x;
978 	/**
979 	 * @y:
980 	 * y position on screen. Should only be used by legacy drivers, atomic
981 	 * drivers should look at &drm_plane_state.crtc_y of the primary plane
982 	 * instead. Updated by calling
983 	 * drm_atomic_helper_update_legacy_modeset_state().
984 	 */
985 	int y;
986 
987 	/** @funcs: CRTC control functions */
988 	const struct drm_crtc_funcs *funcs;
989 
990 	/**
991 	 * @gamma_size: Size of legacy gamma ramp reported to userspace. Set up
992 	 * by calling drm_mode_crtc_set_gamma_size().
993 	 */
994 	uint32_t gamma_size;
995 
996 	/**
997 	 * @gamma_store: Gamma ramp values used by the legacy SETGAMMA and
998 	 * GETGAMMA IOCTls. Set up by calling drm_mode_crtc_set_gamma_size().
999 	 */
1000 	uint16_t *gamma_store;
1001 
1002 	/** @helper_private: mid-layer private data */
1003 	const struct drm_crtc_helper_funcs *helper_private;
1004 
1005 	/** @properties: property tracking for this CRTC */
1006 	struct drm_object_properties properties;
1007 
1008 	/**
1009 	 * @state:
1010 	 *
1011 	 * Current atomic state for this CRTC.
1012 	 *
1013 	 * This is protected by @mutex. Note that nonblocking atomic commits
1014 	 * access the current CRTC state without taking locks. Either by going
1015 	 * through the &struct drm_atomic_state pointers, see
1016 	 * for_each_oldnew_crtc_in_state(), for_each_old_crtc_in_state() and
1017 	 * for_each_new_crtc_in_state(). Or through careful ordering of atomic
1018 	 * commit operations as implemented in the atomic helpers, see
1019 	 * &struct drm_crtc_commit.
1020 	 */
1021 	struct drm_crtc_state *state;
1022 
1023 	/**
1024 	 * @commit_list:
1025 	 *
1026 	 * List of &drm_crtc_commit structures tracking pending commits.
1027 	 * Protected by @commit_lock. This list holds its own full reference,
1028 	 * as does the ongoing commit.
1029 	 *
1030 	 * "Note that the commit for a state change is also tracked in
1031 	 * &drm_crtc_state.commit. For accessing the immediately preceding
1032 	 * commit in an atomic update it is recommended to just use that
1033 	 * pointer in the old CRTC state, since accessing that doesn't need
1034 	 * any locking or list-walking. @commit_list should only be used to
1035 	 * stall for framebuffer cleanup that's signalled through
1036 	 * &drm_crtc_commit.cleanup_done."
1037 	 */
1038 	struct list_head commit_list;
1039 
1040 	/**
1041 	 * @commit_lock:
1042 	 *
1043 	 * Spinlock to protect @commit_list.
1044 	 */
1045 	spinlock_t commit_lock;
1046 
1047 #ifdef CONFIG_DEBUG_FS
1048 	/**
1049 	 * @debugfs_entry:
1050 	 *
1051 	 * Debugfs directory for this CRTC.
1052 	 */
1053 	struct dentry *debugfs_entry;
1054 #endif
1055 
1056 	/**
1057 	 * @crc:
1058 	 *
1059 	 * Configuration settings of CRC capture.
1060 	 */
1061 	struct drm_crtc_crc crc;
1062 
1063 	/**
1064 	 * @fence_context:
1065 	 *
1066 	 * timeline context used for fence operations.
1067 	 */
1068 	unsigned int fence_context;
1069 
1070 	/**
1071 	 * @fence_lock:
1072 	 *
1073 	 * spinlock to protect the fences in the fence_context.
1074 	 */
1075 	spinlock_t fence_lock;
1076 	/**
1077 	 * @fence_seqno:
1078 	 *
1079 	 * Seqno variable used as monotonic counter for the fences
1080 	 * created on the CRTC's timeline.
1081 	 */
1082 	unsigned long fence_seqno;
1083 
1084 	/**
1085 	 * @timeline_name:
1086 	 *
1087 	 * The name of the CRTC's fence timeline.
1088 	 */
1089 	char timeline_name[32];
1090 };
1091 
1092 /**
1093  * struct drm_mode_set - new values for a CRTC config change
1094  * @fb: framebuffer to use for new config
1095  * @crtc: CRTC whose configuration we're about to change
1096  * @mode: mode timings to use
1097  * @x: position of this CRTC relative to @fb
1098  * @y: position of this CRTC relative to @fb
1099  * @connectors: array of connectors to drive with this CRTC if possible
1100  * @num_connectors: size of @connectors array
1101  *
1102  * This represents a modeset configuration for the legacy SETCRTC ioctl and is
1103  * also used internally. Atomic drivers instead use &drm_atomic_state.
1104  */
1105 struct drm_mode_set {
1106 	struct drm_framebuffer *fb;
1107 	struct drm_crtc *crtc;
1108 	struct drm_display_mode *mode;
1109 
1110 	uint32_t x;
1111 	uint32_t y;
1112 
1113 	struct drm_connector **connectors;
1114 	size_t num_connectors;
1115 };
1116 
1117 #define obj_to_crtc(x) container_of(x, struct drm_crtc, base)
1118 
1119 __printf(6, 7)
1120 int drm_crtc_init_with_planes(struct drm_device *dev,
1121 			      struct drm_crtc *crtc,
1122 			      struct drm_plane *primary,
1123 			      struct drm_plane *cursor,
1124 			      const struct drm_crtc_funcs *funcs,
1125 			      const char *name, ...);
1126 void drm_crtc_cleanup(struct drm_crtc *crtc);
1127 
1128 /**
1129  * drm_crtc_index - find the index of a registered CRTC
1130  * @crtc: CRTC to find index for
1131  *
1132  * Given a registered CRTC, return the index of that CRTC within a DRM
1133  * device's list of CRTCs.
1134  */
1135 static inline unsigned int drm_crtc_index(const struct drm_crtc *crtc)
1136 {
1137 	return crtc->index;
1138 }
1139 
1140 /**
1141  * drm_crtc_mask - find the mask of a registered CRTC
1142  * @crtc: CRTC to find mask for
1143  *
1144  * Given a registered CRTC, return the mask bit of that CRTC for the
1145  * &drm_encoder.possible_crtcs and &drm_plane.possible_crtcs fields.
1146  */
1147 static inline uint32_t drm_crtc_mask(const struct drm_crtc *crtc)
1148 {
1149 	return 1 << drm_crtc_index(crtc);
1150 }
1151 
1152 int drm_crtc_force_disable(struct drm_crtc *crtc);
1153 int drm_crtc_force_disable_all(struct drm_device *dev);
1154 
1155 int drm_mode_set_config_internal(struct drm_mode_set *set);
1156 struct drm_crtc *drm_crtc_from_index(struct drm_device *dev, int idx);
1157 
1158 /**
1159  * drm_crtc_find - look up a CRTC object from its ID
1160  * @dev: DRM device
1161  * @file_priv: drm file to check for lease against.
1162  * @id: &drm_mode_object ID
1163  *
1164  * This can be used to look up a CRTC from its userspace ID. Only used by
1165  * drivers for legacy IOCTLs and interface, nowadays extensions to the KMS
1166  * userspace interface should be done using &drm_property.
1167  */
1168 static inline struct drm_crtc *drm_crtc_find(struct drm_device *dev,
1169 		struct drm_file *file_priv,
1170 		uint32_t id)
1171 {
1172 	struct drm_mode_object *mo;
1173 	mo = drm_mode_object_find(dev, file_priv, id, DRM_MODE_OBJECT_CRTC);
1174 	return mo ? obj_to_crtc(mo) : NULL;
1175 }
1176 
1177 /**
1178  * drm_for_each_crtc - iterate over all CRTCs
1179  * @crtc: a &struct drm_crtc as the loop cursor
1180  * @dev: the &struct drm_device
1181  *
1182  * Iterate over all CRTCs of @dev.
1183  */
1184 #define drm_for_each_crtc(crtc, dev) \
1185 	list_for_each_entry(crtc, &(dev)->mode_config.crtc_list, head)
1186 
1187 #endif /* __DRM_CRTC_H__ */
1188