2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
42 #include "drm_crtc_internal.h"
44 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
45 struct drm_mode_fb_cmd2
*r
,
46 struct drm_file
*file_priv
);
49 * drm_modeset_lock_all - take all modeset locks
52 * This function takes all modeset locks, suitable where a more fine-grained
53 * scheme isn't (yet) implemented. Locks must be dropped with
54 * drm_modeset_unlock_all.
56 void drm_modeset_lock_all(struct drm_device
*dev
)
58 struct drm_mode_config
*config
= &dev
->mode_config
;
59 struct drm_modeset_acquire_ctx
*ctx
;
62 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
66 mutex_lock(&config
->mutex
);
68 drm_modeset_acquire_init(ctx
, 0);
71 ret
= drm_modeset_lock(&config
->connection_mutex
, ctx
);
74 ret
= drm_modeset_lock_all_crtcs(dev
, ctx
);
78 WARN_ON(config
->acquire_ctx
);
80 /* now we hold the locks, so now that it is safe, stash the
81 * ctx for drm_modeset_unlock_all():
83 config
->acquire_ctx
= ctx
;
85 drm_warn_on_modeset_not_all_locked(dev
);
90 if (ret
== -EDEADLK
) {
91 drm_modeset_backoff(ctx
);
95 EXPORT_SYMBOL(drm_modeset_lock_all
);
98 * drm_modeset_unlock_all - drop all modeset locks
101 * This function drop all modeset locks taken by drm_modeset_lock_all.
103 void drm_modeset_unlock_all(struct drm_device
*dev
)
105 struct drm_mode_config
*config
= &dev
->mode_config
;
106 struct drm_modeset_acquire_ctx
*ctx
= config
->acquire_ctx
;
111 config
->acquire_ctx
= NULL
;
112 drm_modeset_drop_locks(ctx
);
113 drm_modeset_acquire_fini(ctx
);
117 mutex_unlock(&dev
->mode_config
.mutex
);
119 EXPORT_SYMBOL(drm_modeset_unlock_all
);
122 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
125 * Useful as a debug assert.
127 void drm_warn_on_modeset_not_all_locked(struct drm_device
*dev
)
129 struct drm_crtc
*crtc
;
131 /* Locking is currently fubar in the panic handler. */
132 if (oops_in_progress
)
135 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
136 WARN_ON(!drm_modeset_is_locked(&crtc
->mutex
));
138 WARN_ON(!drm_modeset_is_locked(&dev
->mode_config
.connection_mutex
));
139 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
141 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked
);
143 /* Avoid boilerplate. I'm tired of typing. */
144 #define DRM_ENUM_NAME_FN(fnname, list) \
145 const char *fnname(int val) \
148 for (i = 0; i < ARRAY_SIZE(list); i++) { \
149 if (list[i].type == val) \
150 return list[i].name; \
152 return "(unknown)"; \
158 static const struct drm_prop_enum_list drm_dpms_enum_list
[] =
159 { { DRM_MODE_DPMS_ON
, "On" },
160 { DRM_MODE_DPMS_STANDBY
, "Standby" },
161 { DRM_MODE_DPMS_SUSPEND
, "Suspend" },
162 { DRM_MODE_DPMS_OFF
, "Off" }
165 DRM_ENUM_NAME_FN(drm_get_dpms_name
, drm_dpms_enum_list
)
167 static const struct drm_prop_enum_list drm_plane_type_enum_list
[] =
169 { DRM_PLANE_TYPE_OVERLAY
, "Overlay" },
170 { DRM_PLANE_TYPE_PRIMARY
, "Primary" },
171 { DRM_PLANE_TYPE_CURSOR
, "Cursor" },
175 * Optional properties
177 static const struct drm_prop_enum_list drm_scaling_mode_enum_list
[] =
179 { DRM_MODE_SCALE_NONE
, "None" },
180 { DRM_MODE_SCALE_FULLSCREEN
, "Full" },
181 { DRM_MODE_SCALE_CENTER
, "Center" },
182 { DRM_MODE_SCALE_ASPECT
, "Full aspect" },
185 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list
[] = {
186 { DRM_MODE_PICTURE_ASPECT_NONE
, "Automatic" },
187 { DRM_MODE_PICTURE_ASPECT_4_3
, "4:3" },
188 { DRM_MODE_PICTURE_ASPECT_16_9
, "16:9" },
192 * Non-global properties, but "required" for certain connectors.
194 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list
[] =
196 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
197 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
198 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
201 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name
, drm_dvi_i_select_enum_list
)
203 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list
[] =
205 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
206 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
207 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
210 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name
,
211 drm_dvi_i_subconnector_enum_list
)
213 static const struct drm_prop_enum_list drm_tv_select_enum_list
[] =
215 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
216 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
217 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
218 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
219 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
222 DRM_ENUM_NAME_FN(drm_get_tv_select_name
, drm_tv_select_enum_list
)
224 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list
[] =
226 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
227 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
228 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
229 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
230 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
233 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name
,
234 drm_tv_subconnector_enum_list
)
236 static const struct drm_prop_enum_list drm_dirty_info_enum_list
[] = {
237 { DRM_MODE_DIRTY_OFF
, "Off" },
238 { DRM_MODE_DIRTY_ON
, "On" },
239 { DRM_MODE_DIRTY_ANNOTATE
, "Annotate" },
242 struct drm_conn_prop_enum_list
{
249 * Connector and encoder types.
251 static struct drm_conn_prop_enum_list drm_connector_enum_list
[] =
252 { { DRM_MODE_CONNECTOR_Unknown
, "Unknown" },
253 { DRM_MODE_CONNECTOR_VGA
, "VGA" },
254 { DRM_MODE_CONNECTOR_DVII
, "DVI-I" },
255 { DRM_MODE_CONNECTOR_DVID
, "DVI-D" },
256 { DRM_MODE_CONNECTOR_DVIA
, "DVI-A" },
257 { DRM_MODE_CONNECTOR_Composite
, "Composite" },
258 { DRM_MODE_CONNECTOR_SVIDEO
, "SVIDEO" },
259 { DRM_MODE_CONNECTOR_LVDS
, "LVDS" },
260 { DRM_MODE_CONNECTOR_Component
, "Component" },
261 { DRM_MODE_CONNECTOR_9PinDIN
, "DIN" },
262 { DRM_MODE_CONNECTOR_DisplayPort
, "DP" },
263 { DRM_MODE_CONNECTOR_HDMIA
, "HDMI-A" },
264 { DRM_MODE_CONNECTOR_HDMIB
, "HDMI-B" },
265 { DRM_MODE_CONNECTOR_TV
, "TV" },
266 { DRM_MODE_CONNECTOR_eDP
, "eDP" },
267 { DRM_MODE_CONNECTOR_VIRTUAL
, "Virtual" },
268 { DRM_MODE_CONNECTOR_DSI
, "DSI" },
271 static const struct drm_prop_enum_list drm_encoder_enum_list
[] =
272 { { DRM_MODE_ENCODER_NONE
, "None" },
273 { DRM_MODE_ENCODER_DAC
, "DAC" },
274 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
275 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
276 { DRM_MODE_ENCODER_TVDAC
, "TV" },
277 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
278 { DRM_MODE_ENCODER_DSI
, "DSI" },
279 { DRM_MODE_ENCODER_DPMST
, "DP MST" },
282 static const struct drm_prop_enum_list drm_subpixel_enum_list
[] =
284 { SubPixelUnknown
, "Unknown" },
285 { SubPixelHorizontalRGB
, "Horizontal RGB" },
286 { SubPixelHorizontalBGR
, "Horizontal BGR" },
287 { SubPixelVerticalRGB
, "Vertical RGB" },
288 { SubPixelVerticalBGR
, "Vertical BGR" },
289 { SubPixelNone
, "None" },
292 void drm_connector_ida_init(void)
296 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
297 ida_init(&drm_connector_enum_list
[i
].ida
);
300 void drm_connector_ida_destroy(void)
304 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
305 ida_destroy(&drm_connector_enum_list
[i
].ida
);
309 * drm_get_connector_status_name - return a string for connector status
310 * @status: connector status to compute name of
312 * In contrast to the other drm_get_*_name functions this one here returns a
313 * const pointer and hence is threadsafe.
315 const char *drm_get_connector_status_name(enum drm_connector_status status
)
317 if (status
== connector_status_connected
)
319 else if (status
== connector_status_disconnected
)
320 return "disconnected";
324 EXPORT_SYMBOL(drm_get_connector_status_name
);
327 * drm_get_subpixel_order_name - return a string for a given subpixel enum
328 * @order: enum of subpixel_order
330 * Note you could abuse this and return something out of bounds, but that
331 * would be a caller error. No unscrubbed user data should make it here.
333 const char *drm_get_subpixel_order_name(enum subpixel_order order
)
335 return drm_subpixel_enum_list
[order
].name
;
337 EXPORT_SYMBOL(drm_get_subpixel_order_name
);
339 static char printable_char(int c
)
341 return isascii(c
) && isprint(c
) ? c
: '?';
345 * drm_get_format_name - return a string for drm fourcc format
346 * @format: format to compute name of
348 * Note that the buffer used by this function is globally shared and owned by
349 * the function itself.
351 * FIXME: This isn't really multithreading safe.
353 const char *drm_get_format_name(uint32_t format
)
357 snprintf(buf
, sizeof(buf
),
358 "%c%c%c%c %s-endian (0x%08x)",
359 printable_char(format
& 0xff),
360 printable_char((format
>> 8) & 0xff),
361 printable_char((format
>> 16) & 0xff),
362 printable_char((format
>> 24) & 0x7f),
363 format
& DRM_FORMAT_BIG_ENDIAN
? "big" : "little",
368 EXPORT_SYMBOL(drm_get_format_name
);
371 * Internal function to assign a slot in the object idr and optionally
372 * register the object into the idr.
374 static int drm_mode_object_get_reg(struct drm_device
*dev
,
375 struct drm_mode_object
*obj
,
381 mutex_lock(&dev
->mode_config
.idr_mutex
);
382 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, register_obj
? obj
: NULL
, 1, 0, GFP_KERNEL
);
385 * Set up the object linking under the protection of the idr
386 * lock so that other users can't see inconsistent state.
389 obj
->type
= obj_type
;
391 mutex_unlock(&dev
->mode_config
.idr_mutex
);
393 return ret
< 0 ? ret
: 0;
397 * drm_mode_object_get - allocate a new modeset identifier
399 * @obj: object pointer, used to generate unique ID
400 * @obj_type: object type
402 * Create a unique identifier based on @ptr in @dev's identifier space. Used
403 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
404 * modeset identifiers are _not_ reference counted. Hence don't use this for
405 * reference counted modeset objects like framebuffers.
408 * New unique (relative to other objects in @dev) integer identifier for the
411 int drm_mode_object_get(struct drm_device
*dev
,
412 struct drm_mode_object
*obj
, uint32_t obj_type
)
414 return drm_mode_object_get_reg(dev
, obj
, obj_type
, true);
417 static void drm_mode_object_register(struct drm_device
*dev
,
418 struct drm_mode_object
*obj
)
420 mutex_lock(&dev
->mode_config
.idr_mutex
);
421 idr_replace(&dev
->mode_config
.crtc_idr
, obj
, obj
->id
);
422 mutex_unlock(&dev
->mode_config
.idr_mutex
);
426 * drm_mode_object_put - free a modeset identifer
428 * @object: object to free
430 * Free @id from @dev's unique identifier pool. Note that despite the _get
431 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
432 * for reference counted modeset objects like framebuffers.
434 void drm_mode_object_put(struct drm_device
*dev
,
435 struct drm_mode_object
*object
)
437 mutex_lock(&dev
->mode_config
.idr_mutex
);
438 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
439 mutex_unlock(&dev
->mode_config
.idr_mutex
);
442 static struct drm_mode_object
*_object_find(struct drm_device
*dev
,
443 uint32_t id
, uint32_t type
)
445 struct drm_mode_object
*obj
= NULL
;
447 mutex_lock(&dev
->mode_config
.idr_mutex
);
448 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
449 if (obj
&& type
!= DRM_MODE_OBJECT_ANY
&& obj
->type
!= type
)
451 if (obj
&& obj
->id
!= id
)
453 /* don't leak out unref'd fb's */
454 if (obj
&& (obj
->type
== DRM_MODE_OBJECT_FB
))
456 mutex_unlock(&dev
->mode_config
.idr_mutex
);
462 * drm_mode_object_find - look up a drm object with static lifetime
464 * @id: id of the mode object
465 * @type: type of the mode object
467 * Note that framebuffers cannot be looked up with this functions - since those
468 * are reference counted, they need special treatment. Even with
469 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
470 * rather than WARN_ON()).
472 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
473 uint32_t id
, uint32_t type
)
475 struct drm_mode_object
*obj
= NULL
;
477 /* Framebuffers are reference counted and need their own lookup
479 WARN_ON(type
== DRM_MODE_OBJECT_FB
);
480 obj
= _object_find(dev
, id
, type
);
483 EXPORT_SYMBOL(drm_mode_object_find
);
486 * drm_framebuffer_init - initialize a framebuffer
488 * @fb: framebuffer to be initialized
489 * @funcs: ... with these functions
491 * Allocates an ID for the framebuffer's parent mode object, sets its mode
492 * functions & device file and adds it to the master fd list.
495 * This functions publishes the fb and makes it available for concurrent access
496 * by other users. Which means by this point the fb _must_ be fully set up -
497 * since all the fb attributes are invariant over its lifetime, no further
498 * locking but only correct reference counting is required.
501 * Zero on success, error code on failure.
503 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
504 const struct drm_framebuffer_funcs
*funcs
)
508 mutex_lock(&dev
->mode_config
.fb_lock
);
509 kref_init(&fb
->refcount
);
510 INIT_LIST_HEAD(&fb
->filp_head
);
514 ret
= drm_mode_object_get(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
);
518 /* Grab the idr reference. */
519 drm_framebuffer_reference(fb
);
521 dev
->mode_config
.num_fb
++;
522 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
524 mutex_unlock(&dev
->mode_config
.fb_lock
);
528 EXPORT_SYMBOL(drm_framebuffer_init
);
530 static void drm_framebuffer_free(struct kref
*kref
)
532 struct drm_framebuffer
*fb
=
533 container_of(kref
, struct drm_framebuffer
, refcount
);
534 fb
->funcs
->destroy(fb
);
537 static struct drm_framebuffer
*__drm_framebuffer_lookup(struct drm_device
*dev
,
540 struct drm_mode_object
*obj
= NULL
;
541 struct drm_framebuffer
*fb
;
543 mutex_lock(&dev
->mode_config
.idr_mutex
);
544 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
545 if (!obj
|| (obj
->type
!= DRM_MODE_OBJECT_FB
) || (obj
->id
!= id
))
549 mutex_unlock(&dev
->mode_config
.idr_mutex
);
555 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
557 * @id: id of the fb object
559 * If successful, this grabs an additional reference to the framebuffer -
560 * callers need to make sure to eventually unreference the returned framebuffer
561 * again, using @drm_framebuffer_unreference.
563 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
566 struct drm_framebuffer
*fb
;
568 mutex_lock(&dev
->mode_config
.fb_lock
);
569 fb
= __drm_framebuffer_lookup(dev
, id
);
571 drm_framebuffer_reference(fb
);
572 mutex_unlock(&dev
->mode_config
.fb_lock
);
576 EXPORT_SYMBOL(drm_framebuffer_lookup
);
579 * drm_framebuffer_unreference - unref a framebuffer
580 * @fb: framebuffer to unref
582 * This functions decrements the fb's refcount and frees it if it drops to zero.
584 void drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
586 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
587 kref_put(&fb
->refcount
, drm_framebuffer_free
);
589 EXPORT_SYMBOL(drm_framebuffer_unreference
);
592 * drm_framebuffer_reference - incr the fb refcnt
595 * This functions increments the fb's refcount.
597 void drm_framebuffer_reference(struct drm_framebuffer
*fb
)
599 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
600 kref_get(&fb
->refcount
);
602 EXPORT_SYMBOL(drm_framebuffer_reference
);
604 static void drm_framebuffer_free_bug(struct kref
*kref
)
609 static void __drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
611 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
612 kref_put(&fb
->refcount
, drm_framebuffer_free_bug
);
615 /* dev->mode_config.fb_lock must be held! */
616 static void __drm_framebuffer_unregister(struct drm_device
*dev
,
617 struct drm_framebuffer
*fb
)
619 mutex_lock(&dev
->mode_config
.idr_mutex
);
620 idr_remove(&dev
->mode_config
.crtc_idr
, fb
->base
.id
);
621 mutex_unlock(&dev
->mode_config
.idr_mutex
);
625 __drm_framebuffer_unreference(fb
);
629 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
630 * @fb: fb to unregister
632 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
633 * those used for fbdev. Note that the caller must hold a reference of it's own,
634 * i.e. the object may not be destroyed through this call (since it'll lead to a
635 * locking inversion).
637 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
639 struct drm_device
*dev
= fb
->dev
;
641 mutex_lock(&dev
->mode_config
.fb_lock
);
642 /* Mark fb as reaped and drop idr ref. */
643 __drm_framebuffer_unregister(dev
, fb
);
644 mutex_unlock(&dev
->mode_config
.fb_lock
);
646 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
649 * drm_framebuffer_cleanup - remove a framebuffer object
650 * @fb: framebuffer to remove
652 * Cleanup framebuffer. This function is intended to be used from the drivers
653 * ->destroy callback. It can also be used to clean up driver private
654 * framebuffers embedded into a larger structure.
656 * Note that this function does not remove the fb from active usuage - if it is
657 * still used anywhere, hilarity can ensue since userspace could call getfb on
658 * the id and get back -EINVAL. Obviously no concern at driver unload time.
660 * Also, the framebuffer will not be removed from the lookup idr - for
661 * user-created framebuffers this will happen in in the rmfb ioctl. For
662 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
663 * drm_framebuffer_unregister_private.
665 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
667 struct drm_device
*dev
= fb
->dev
;
669 mutex_lock(&dev
->mode_config
.fb_lock
);
671 dev
->mode_config
.num_fb
--;
672 mutex_unlock(&dev
->mode_config
.fb_lock
);
674 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
677 * drm_framebuffer_remove - remove and unreference a framebuffer object
678 * @fb: framebuffer to remove
680 * Scans all the CRTCs and planes in @dev's mode_config. If they're
681 * using @fb, removes it, setting it to NULL. Then drops the reference to the
682 * passed-in framebuffer. Might take the modeset locks.
684 * Note that this function optimizes the cleanup away if the caller holds the
685 * last reference to the framebuffer. It is also guaranteed to not take the
686 * modeset locks in this case.
688 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
690 struct drm_device
*dev
= fb
->dev
;
691 struct drm_crtc
*crtc
;
692 struct drm_plane
*plane
;
693 struct drm_mode_set set
;
696 WARN_ON(!list_empty(&fb
->filp_head
));
699 * drm ABI mandates that we remove any deleted framebuffers from active
700 * useage. But since most sane clients only remove framebuffers they no
701 * longer need, try to optimize this away.
703 * Since we're holding a reference ourselves, observing a refcount of 1
704 * means that we're the last holder and can skip it. Also, the refcount
705 * can never increase from 1 again, so we don't need any barriers or
708 * Note that userspace could try to race with use and instate a new
709 * usage _after_ we've cleared all current ones. End result will be an
710 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
713 if (atomic_read(&fb
->refcount
.refcount
) > 1) {
714 drm_modeset_lock_all(dev
);
715 /* remove from any CRTC */
716 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
717 if (crtc
->primary
->fb
== fb
) {
718 /* should turn off the crtc */
719 memset(&set
, 0, sizeof(struct drm_mode_set
));
722 ret
= drm_mode_set_config_internal(&set
);
724 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
728 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
730 drm_plane_force_disable(plane
);
732 drm_modeset_unlock_all(dev
);
735 drm_framebuffer_unreference(fb
);
737 EXPORT_SYMBOL(drm_framebuffer_remove
);
739 DEFINE_WW_CLASS(crtc_ww_class
);
742 * drm_crtc_init_with_planes - Initialise a new CRTC object with
743 * specified primary and cursor planes.
745 * @crtc: CRTC object to init
746 * @primary: Primary plane for CRTC
747 * @cursor: Cursor plane for CRTC
748 * @funcs: callbacks for the new CRTC
750 * Inits a new object created as base part of a driver crtc object.
753 * Zero on success, error code on failure.
755 int drm_crtc_init_with_planes(struct drm_device
*dev
, struct drm_crtc
*crtc
,
756 struct drm_plane
*primary
,
757 struct drm_plane
*cursor
,
758 const struct drm_crtc_funcs
*funcs
)
760 struct drm_mode_config
*config
= &dev
->mode_config
;
765 crtc
->invert_dimensions
= false;
767 drm_modeset_lock_all(dev
);
768 drm_modeset_lock_init(&crtc
->mutex
);
769 /* dropped by _unlock_all(): */
770 drm_modeset_lock(&crtc
->mutex
, config
->acquire_ctx
);
772 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
776 crtc
->base
.properties
= &crtc
->properties
;
778 list_add_tail(&crtc
->head
, &config
->crtc_list
);
781 crtc
->primary
= primary
;
782 crtc
->cursor
= cursor
;
784 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
786 cursor
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
789 drm_modeset_unlock_all(dev
);
793 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
796 * drm_crtc_cleanup - Clean up the core crtc usage
797 * @crtc: CRTC to cleanup
799 * This function cleans up @crtc and removes it from the DRM mode setting
800 * core. Note that the function does *not* free the crtc structure itself,
801 * this is the responsibility of the caller.
803 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
805 struct drm_device
*dev
= crtc
->dev
;
807 kfree(crtc
->gamma_store
);
808 crtc
->gamma_store
= NULL
;
810 drm_modeset_lock_fini(&crtc
->mutex
);
812 drm_mode_object_put(dev
, &crtc
->base
);
813 list_del(&crtc
->head
);
814 dev
->mode_config
.num_crtc
--;
816 EXPORT_SYMBOL(drm_crtc_cleanup
);
819 * drm_crtc_index - find the index of a registered CRTC
820 * @crtc: CRTC to find index for
822 * Given a registered CRTC, return the index of that CRTC within a DRM
823 * device's list of CRTCs.
825 unsigned int drm_crtc_index(struct drm_crtc
*crtc
)
827 unsigned int index
= 0;
828 struct drm_crtc
*tmp
;
830 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
839 EXPORT_SYMBOL(drm_crtc_index
);
842 * drm_mode_remove - remove and free a mode
843 * @connector: connector list to modify
844 * @mode: mode to remove
846 * Remove @mode from @connector's mode list, then free it.
848 static void drm_mode_remove(struct drm_connector
*connector
,
849 struct drm_display_mode
*mode
)
851 list_del(&mode
->head
);
852 drm_mode_destroy(connector
->dev
, mode
);
856 * drm_connector_init - Init a preallocated connector
858 * @connector: the connector to init
859 * @funcs: callbacks for this connector
860 * @connector_type: user visible type of the connector
862 * Initialises a preallocated connector. Connectors should be
863 * subclassed as part of driver connector objects.
866 * Zero on success, error code on failure.
868 int drm_connector_init(struct drm_device
*dev
,
869 struct drm_connector
*connector
,
870 const struct drm_connector_funcs
*funcs
,
874 struct ida
*connector_ida
=
875 &drm_connector_enum_list
[connector_type
].ida
;
877 drm_modeset_lock_all(dev
);
879 ret
= drm_mode_object_get_reg(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
, false);
883 connector
->base
.properties
= &connector
->properties
;
884 connector
->dev
= dev
;
885 connector
->funcs
= funcs
;
886 connector
->connector_type
= connector_type
;
887 connector
->connector_type_id
=
888 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
889 if (connector
->connector_type_id
< 0) {
890 ret
= connector
->connector_type_id
;
894 kasprintf(GFP_KERNEL
, "%s-%d",
895 drm_connector_enum_list
[connector_type
].name
,
896 connector
->connector_type_id
);
897 if (!connector
->name
) {
902 INIT_LIST_HEAD(&connector
->probed_modes
);
903 INIT_LIST_HEAD(&connector
->modes
);
904 connector
->edid_blob_ptr
= NULL
;
905 connector
->status
= connector_status_unknown
;
907 list_add_tail(&connector
->head
, &dev
->mode_config
.connector_list
);
908 dev
->mode_config
.num_connector
++;
910 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
911 drm_object_attach_property(&connector
->base
,
912 dev
->mode_config
.edid_property
,
915 drm_object_attach_property(&connector
->base
,
916 dev
->mode_config
.dpms_property
, 0);
918 connector
->debugfs_entry
= NULL
;
922 drm_mode_object_put(dev
, &connector
->base
);
925 drm_modeset_unlock_all(dev
);
929 EXPORT_SYMBOL(drm_connector_init
);
932 * drm_connector_cleanup - cleans up an initialised connector
933 * @connector: connector to cleanup
935 * Cleans up the connector but doesn't free the object.
937 void drm_connector_cleanup(struct drm_connector
*connector
)
939 struct drm_device
*dev
= connector
->dev
;
940 struct drm_display_mode
*mode
, *t
;
942 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
943 drm_mode_remove(connector
, mode
);
945 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
946 drm_mode_remove(connector
, mode
);
948 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
949 connector
->connector_type_id
);
951 drm_mode_object_put(dev
, &connector
->base
);
952 kfree(connector
->name
);
953 connector
->name
= NULL
;
954 list_del(&connector
->head
);
955 dev
->mode_config
.num_connector
--;
957 EXPORT_SYMBOL(drm_connector_cleanup
);
960 * drm_connector_register - register a connector
961 * @connector: the connector to register
963 * Register userspace interfaces for a connector
966 * Zero on success, error code on failure.
968 int drm_connector_register(struct drm_connector
*connector
)
972 drm_mode_object_register(connector
->dev
, &connector
->base
);
974 ret
= drm_sysfs_connector_add(connector
);
978 ret
= drm_debugfs_connector_add(connector
);
980 drm_sysfs_connector_remove(connector
);
986 EXPORT_SYMBOL(drm_connector_register
);
989 * drm_connector_unregister - unregister a connector
990 * @connector: the connector to unregister
992 * Unregister userspace interfaces for a connector
994 void drm_connector_unregister(struct drm_connector
*connector
)
996 drm_sysfs_connector_remove(connector
);
997 drm_debugfs_connector_remove(connector
);
999 EXPORT_SYMBOL(drm_connector_unregister
);
1003 * drm_connector_unplug_all - unregister connector userspace interfaces
1006 * This function unregisters all connector userspace interfaces in sysfs. Should
1007 * be call when the device is disconnected, e.g. from an usb driver's
1008 * ->disconnect callback.
1010 void drm_connector_unplug_all(struct drm_device
*dev
)
1012 struct drm_connector
*connector
;
1014 /* taking the mode config mutex ends up in a clash with sysfs */
1015 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1016 drm_connector_unregister(connector
);
1019 EXPORT_SYMBOL(drm_connector_unplug_all
);
1022 * drm_bridge_init - initialize a drm transcoder/bridge
1024 * @bridge: transcoder/bridge to set up
1025 * @funcs: bridge function table
1027 * Initialises a preallocated bridge. Bridges should be
1028 * subclassed as part of driver connector objects.
1031 * Zero on success, error code on failure.
1033 int drm_bridge_init(struct drm_device
*dev
, struct drm_bridge
*bridge
,
1034 const struct drm_bridge_funcs
*funcs
)
1038 drm_modeset_lock_all(dev
);
1040 ret
= drm_mode_object_get(dev
, &bridge
->base
, DRM_MODE_OBJECT_BRIDGE
);
1045 bridge
->funcs
= funcs
;
1047 list_add_tail(&bridge
->head
, &dev
->mode_config
.bridge_list
);
1048 dev
->mode_config
.num_bridge
++;
1051 drm_modeset_unlock_all(dev
);
1054 EXPORT_SYMBOL(drm_bridge_init
);
1057 * drm_bridge_cleanup - cleans up an initialised bridge
1058 * @bridge: bridge to cleanup
1060 * Cleans up the bridge but doesn't free the object.
1062 void drm_bridge_cleanup(struct drm_bridge
*bridge
)
1064 struct drm_device
*dev
= bridge
->dev
;
1066 drm_modeset_lock_all(dev
);
1067 drm_mode_object_put(dev
, &bridge
->base
);
1068 list_del(&bridge
->head
);
1069 dev
->mode_config
.num_bridge
--;
1070 drm_modeset_unlock_all(dev
);
1072 EXPORT_SYMBOL(drm_bridge_cleanup
);
1075 * drm_encoder_init - Init a preallocated encoder
1077 * @encoder: the encoder to init
1078 * @funcs: callbacks for this encoder
1079 * @encoder_type: user visible type of the encoder
1081 * Initialises a preallocated encoder. Encoder should be
1082 * subclassed as part of driver encoder objects.
1085 * Zero on success, error code on failure.
1087 int drm_encoder_init(struct drm_device
*dev
,
1088 struct drm_encoder
*encoder
,
1089 const struct drm_encoder_funcs
*funcs
,
1094 drm_modeset_lock_all(dev
);
1096 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1101 encoder
->encoder_type
= encoder_type
;
1102 encoder
->funcs
= funcs
;
1103 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1104 drm_encoder_enum_list
[encoder_type
].name
,
1106 if (!encoder
->name
) {
1111 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1112 dev
->mode_config
.num_encoder
++;
1116 drm_mode_object_put(dev
, &encoder
->base
);
1119 drm_modeset_unlock_all(dev
);
1123 EXPORT_SYMBOL(drm_encoder_init
);
1126 * drm_encoder_cleanup - cleans up an initialised encoder
1127 * @encoder: encoder to cleanup
1129 * Cleans up the encoder but doesn't free the object.
1131 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1133 struct drm_device
*dev
= encoder
->dev
;
1134 drm_modeset_lock_all(dev
);
1135 drm_mode_object_put(dev
, &encoder
->base
);
1136 kfree(encoder
->name
);
1137 encoder
->name
= NULL
;
1138 list_del(&encoder
->head
);
1139 dev
->mode_config
.num_encoder
--;
1140 drm_modeset_unlock_all(dev
);
1142 EXPORT_SYMBOL(drm_encoder_cleanup
);
1145 * drm_universal_plane_init - Initialize a new universal plane object
1147 * @plane: plane object to init
1148 * @possible_crtcs: bitmask of possible CRTCs
1149 * @funcs: callbacks for the new plane
1150 * @formats: array of supported formats (%DRM_FORMAT_*)
1151 * @format_count: number of elements in @formats
1152 * @type: type of plane (overlay, primary, cursor)
1154 * Initializes a plane object of type @type.
1157 * Zero on success, error code on failure.
1159 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1160 unsigned long possible_crtcs
,
1161 const struct drm_plane_funcs
*funcs
,
1162 const uint32_t *formats
, uint32_t format_count
,
1163 enum drm_plane_type type
)
1167 drm_modeset_lock_all(dev
);
1169 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1173 plane
->base
.properties
= &plane
->properties
;
1175 plane
->funcs
= funcs
;
1176 plane
->format_types
= kmalloc(sizeof(uint32_t) * format_count
,
1178 if (!plane
->format_types
) {
1179 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1180 drm_mode_object_put(dev
, &plane
->base
);
1185 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1186 plane
->format_count
= format_count
;
1187 plane
->possible_crtcs
= possible_crtcs
;
1190 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
1191 dev
->mode_config
.num_total_plane
++;
1192 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1193 dev
->mode_config
.num_overlay_plane
++;
1195 drm_object_attach_property(&plane
->base
,
1196 dev
->mode_config
.plane_type_property
,
1200 drm_modeset_unlock_all(dev
);
1204 EXPORT_SYMBOL(drm_universal_plane_init
);
1207 * drm_plane_init - Initialize a legacy plane
1209 * @plane: plane object to init
1210 * @possible_crtcs: bitmask of possible CRTCs
1211 * @funcs: callbacks for the new plane
1212 * @formats: array of supported formats (%DRM_FORMAT_*)
1213 * @format_count: number of elements in @formats
1214 * @is_primary: plane type (primary vs overlay)
1216 * Legacy API to initialize a DRM plane.
1218 * New drivers should call drm_universal_plane_init() instead.
1221 * Zero on success, error code on failure.
1223 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1224 unsigned long possible_crtcs
,
1225 const struct drm_plane_funcs
*funcs
,
1226 const uint32_t *formats
, uint32_t format_count
,
1229 enum drm_plane_type type
;
1231 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1232 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1233 formats
, format_count
, type
);
1235 EXPORT_SYMBOL(drm_plane_init
);
1238 * drm_plane_cleanup - Clean up the core plane usage
1239 * @plane: plane to cleanup
1241 * This function cleans up @plane and removes it from the DRM mode setting
1242 * core. Note that the function does *not* free the plane structure itself,
1243 * this is the responsibility of the caller.
1245 void drm_plane_cleanup(struct drm_plane
*plane
)
1247 struct drm_device
*dev
= plane
->dev
;
1249 drm_modeset_lock_all(dev
);
1250 kfree(plane
->format_types
);
1251 drm_mode_object_put(dev
, &plane
->base
);
1253 BUG_ON(list_empty(&plane
->head
));
1255 list_del(&plane
->head
);
1256 dev
->mode_config
.num_total_plane
--;
1257 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1258 dev
->mode_config
.num_overlay_plane
--;
1259 drm_modeset_unlock_all(dev
);
1261 EXPORT_SYMBOL(drm_plane_cleanup
);
1264 * drm_plane_force_disable - Forcibly disable a plane
1265 * @plane: plane to disable
1267 * Forces the plane to be disabled.
1269 * Used when the plane's current framebuffer is destroyed,
1270 * and when restoring fbdev mode.
1272 void drm_plane_force_disable(struct drm_plane
*plane
)
1274 struct drm_framebuffer
*old_fb
= plane
->fb
;
1280 ret
= plane
->funcs
->disable_plane(plane
);
1282 DRM_ERROR("failed to disable plane with busy fb\n");
1285 /* disconnect the plane from the fb and crtc: */
1286 __drm_framebuffer_unreference(old_fb
);
1290 EXPORT_SYMBOL(drm_plane_force_disable
);
1292 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
1294 struct drm_property
*edid
;
1295 struct drm_property
*dpms
;
1296 struct drm_property
*dev_path
;
1299 * Standard properties (apply to all connectors)
1301 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1302 DRM_MODE_PROP_IMMUTABLE
,
1304 dev
->mode_config
.edid_property
= edid
;
1306 dpms
= drm_property_create_enum(dev
, 0,
1307 "DPMS", drm_dpms_enum_list
,
1308 ARRAY_SIZE(drm_dpms_enum_list
));
1309 dev
->mode_config
.dpms_property
= dpms
;
1311 dev_path
= drm_property_create(dev
,
1312 DRM_MODE_PROP_BLOB
|
1313 DRM_MODE_PROP_IMMUTABLE
,
1315 dev
->mode_config
.path_property
= dev_path
;
1320 static int drm_mode_create_standard_plane_properties(struct drm_device
*dev
)
1322 struct drm_property
*type
;
1325 * Standard properties (apply to all planes)
1327 type
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1328 "type", drm_plane_type_enum_list
,
1329 ARRAY_SIZE(drm_plane_type_enum_list
));
1330 dev
->mode_config
.plane_type_property
= type
;
1336 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1339 * Called by a driver the first time a DVI-I connector is made.
1341 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1343 struct drm_property
*dvi_i_selector
;
1344 struct drm_property
*dvi_i_subconnector
;
1346 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1350 drm_property_create_enum(dev
, 0,
1351 "select subconnector",
1352 drm_dvi_i_select_enum_list
,
1353 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1354 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1356 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1358 drm_dvi_i_subconnector_enum_list
,
1359 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1360 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1364 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1367 * drm_create_tv_properties - create TV specific connector properties
1369 * @num_modes: number of different TV formats (modes) supported
1370 * @modes: array of pointers to strings containing name of each format
1372 * Called by a driver's TV initialization routine, this function creates
1373 * the TV specific connector properties for a given device. Caller is
1374 * responsible for allocating a list of format names and passing them to
1377 int drm_mode_create_tv_properties(struct drm_device
*dev
, int num_modes
,
1380 struct drm_property
*tv_selector
;
1381 struct drm_property
*tv_subconnector
;
1384 if (dev
->mode_config
.tv_select_subconnector_property
)
1388 * Basic connector properties
1390 tv_selector
= drm_property_create_enum(dev
, 0,
1391 "select subconnector",
1392 drm_tv_select_enum_list
,
1393 ARRAY_SIZE(drm_tv_select_enum_list
));
1394 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1397 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1399 drm_tv_subconnector_enum_list
,
1400 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1401 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1404 * Other, TV specific properties: margins & TV modes.
1406 dev
->mode_config
.tv_left_margin_property
=
1407 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1409 dev
->mode_config
.tv_right_margin_property
=
1410 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1412 dev
->mode_config
.tv_top_margin_property
=
1413 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1415 dev
->mode_config
.tv_bottom_margin_property
=
1416 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1418 dev
->mode_config
.tv_mode_property
=
1419 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1421 for (i
= 0; i
< num_modes
; i
++)
1422 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1425 dev
->mode_config
.tv_brightness_property
=
1426 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1428 dev
->mode_config
.tv_contrast_property
=
1429 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1431 dev
->mode_config
.tv_flicker_reduction_property
=
1432 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1434 dev
->mode_config
.tv_overscan_property
=
1435 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1437 dev
->mode_config
.tv_saturation_property
=
1438 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1440 dev
->mode_config
.tv_hue_property
=
1441 drm_property_create_range(dev
, 0, "hue", 0, 100);
1445 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1448 * drm_mode_create_scaling_mode_property - create scaling mode property
1451 * Called by a driver the first time it's needed, must be attached to desired
1454 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1456 struct drm_property
*scaling_mode
;
1458 if (dev
->mode_config
.scaling_mode_property
)
1462 drm_property_create_enum(dev
, 0, "scaling mode",
1463 drm_scaling_mode_enum_list
,
1464 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1466 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1470 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1473 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1476 * Called by a driver the first time it's needed, must be attached to desired
1480 * Zero on success, errno on failure.
1482 int drm_mode_create_aspect_ratio_property(struct drm_device
*dev
)
1484 if (dev
->mode_config
.aspect_ratio_property
)
1487 dev
->mode_config
.aspect_ratio_property
=
1488 drm_property_create_enum(dev
, 0, "aspect ratio",
1489 drm_aspect_ratio_enum_list
,
1490 ARRAY_SIZE(drm_aspect_ratio_enum_list
));
1492 if (dev
->mode_config
.aspect_ratio_property
== NULL
)
1497 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property
);
1500 * drm_mode_create_dirty_property - create dirty property
1503 * Called by a driver the first time it's needed, must be attached to desired
1506 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1508 struct drm_property
*dirty_info
;
1510 if (dev
->mode_config
.dirty_info_property
)
1514 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1516 drm_dirty_info_enum_list
,
1517 ARRAY_SIZE(drm_dirty_info_enum_list
));
1518 dev
->mode_config
.dirty_info_property
= dirty_info
;
1522 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1524 static int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
1526 uint32_t total_objects
= 0;
1528 total_objects
+= dev
->mode_config
.num_crtc
;
1529 total_objects
+= dev
->mode_config
.num_connector
;
1530 total_objects
+= dev
->mode_config
.num_encoder
;
1531 total_objects
+= dev
->mode_config
.num_bridge
;
1533 group
->id_list
= kzalloc(total_objects
* sizeof(uint32_t), GFP_KERNEL
);
1534 if (!group
->id_list
)
1537 group
->num_crtcs
= 0;
1538 group
->num_connectors
= 0;
1539 group
->num_encoders
= 0;
1540 group
->num_bridges
= 0;
1544 void drm_mode_group_destroy(struct drm_mode_group
*group
)
1546 kfree(group
->id_list
);
1547 group
->id_list
= NULL
;
1551 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1552 * the drm core's responsibility to set up mode control groups.
1554 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
1555 struct drm_mode_group
*group
)
1557 struct drm_crtc
*crtc
;
1558 struct drm_encoder
*encoder
;
1559 struct drm_connector
*connector
;
1560 struct drm_bridge
*bridge
;
1563 if ((ret
= drm_mode_group_init(dev
, group
)))
1566 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
1567 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
1569 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
1570 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
1573 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1574 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1575 group
->num_connectors
++] = connector
->base
.id
;
1577 list_for_each_entry(bridge
, &dev
->mode_config
.bridge_list
, head
)
1578 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1579 group
->num_connectors
+ group
->num_bridges
++] =
1584 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
1586 void drm_reinit_primary_mode_group(struct drm_device
*dev
)
1588 drm_modeset_lock_all(dev
);
1589 drm_mode_group_destroy(&dev
->primary
->mode_group
);
1590 drm_mode_group_init_legacy_group(dev
, &dev
->primary
->mode_group
);
1591 drm_modeset_unlock_all(dev
);
1593 EXPORT_SYMBOL(drm_reinit_primary_mode_group
);
1596 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1597 * @out: drm_mode_modeinfo struct to return to the user
1598 * @in: drm_display_mode to use
1600 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1603 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1604 const struct drm_display_mode
*in
)
1606 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1607 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1608 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1609 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1610 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1611 "timing values too large for mode info\n");
1613 out
->clock
= in
->clock
;
1614 out
->hdisplay
= in
->hdisplay
;
1615 out
->hsync_start
= in
->hsync_start
;
1616 out
->hsync_end
= in
->hsync_end
;
1617 out
->htotal
= in
->htotal
;
1618 out
->hskew
= in
->hskew
;
1619 out
->vdisplay
= in
->vdisplay
;
1620 out
->vsync_start
= in
->vsync_start
;
1621 out
->vsync_end
= in
->vsync_end
;
1622 out
->vtotal
= in
->vtotal
;
1623 out
->vscan
= in
->vscan
;
1624 out
->vrefresh
= in
->vrefresh
;
1625 out
->flags
= in
->flags
;
1626 out
->type
= in
->type
;
1627 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1628 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1632 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1633 * @out: drm_display_mode to return to the user
1634 * @in: drm_mode_modeinfo to use
1636 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1640 * Zero on success, errno on failure.
1642 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1643 const struct drm_mode_modeinfo
*in
)
1645 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1648 if ((in
->flags
& DRM_MODE_FLAG_3D_MASK
) > DRM_MODE_FLAG_3D_MAX
)
1651 out
->clock
= in
->clock
;
1652 out
->hdisplay
= in
->hdisplay
;
1653 out
->hsync_start
= in
->hsync_start
;
1654 out
->hsync_end
= in
->hsync_end
;
1655 out
->htotal
= in
->htotal
;
1656 out
->hskew
= in
->hskew
;
1657 out
->vdisplay
= in
->vdisplay
;
1658 out
->vsync_start
= in
->vsync_start
;
1659 out
->vsync_end
= in
->vsync_end
;
1660 out
->vtotal
= in
->vtotal
;
1661 out
->vscan
= in
->vscan
;
1662 out
->vrefresh
= in
->vrefresh
;
1663 out
->flags
= in
->flags
;
1664 out
->type
= in
->type
;
1665 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1666 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1672 * drm_mode_getresources - get graphics configuration
1673 * @dev: drm device for the ioctl
1674 * @data: data pointer for the ioctl
1675 * @file_priv: drm file for the ioctl call
1677 * Construct a set of configuration description structures and return
1678 * them to the user, including CRTC, connector and framebuffer configuration.
1680 * Called by the user via ioctl.
1683 * Zero on success, errno on failure.
1685 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1686 struct drm_file
*file_priv
)
1688 struct drm_mode_card_res
*card_res
= data
;
1689 struct list_head
*lh
;
1690 struct drm_framebuffer
*fb
;
1691 struct drm_connector
*connector
;
1692 struct drm_crtc
*crtc
;
1693 struct drm_encoder
*encoder
;
1695 int connector_count
= 0;
1698 int encoder_count
= 0;
1700 uint32_t __user
*fb_id
;
1701 uint32_t __user
*crtc_id
;
1702 uint32_t __user
*connector_id
;
1703 uint32_t __user
*encoder_id
;
1704 struct drm_mode_group
*mode_group
;
1706 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1710 mutex_lock(&file_priv
->fbs_lock
);
1712 * For the non-control nodes we need to limit the list of resources
1713 * by IDs in the group list for this node
1715 list_for_each(lh
, &file_priv
->fbs
)
1718 /* handle this in 4 parts */
1720 if (card_res
->count_fbs
>= fb_count
) {
1722 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1723 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1724 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1725 mutex_unlock(&file_priv
->fbs_lock
);
1731 card_res
->count_fbs
= fb_count
;
1732 mutex_unlock(&file_priv
->fbs_lock
);
1734 drm_modeset_lock_all(dev
);
1735 if (!drm_is_primary_client(file_priv
)) {
1738 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1741 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1744 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1748 mode_group
= &file_priv
->master
->minor
->mode_group
;
1749 crtc_count
= mode_group
->num_crtcs
;
1750 connector_count
= mode_group
->num_connectors
;
1751 encoder_count
= mode_group
->num_encoders
;
1754 card_res
->max_height
= dev
->mode_config
.max_height
;
1755 card_res
->min_height
= dev
->mode_config
.min_height
;
1756 card_res
->max_width
= dev
->mode_config
.max_width
;
1757 card_res
->min_width
= dev
->mode_config
.min_width
;
1760 if (card_res
->count_crtcs
>= crtc_count
) {
1762 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1764 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1766 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1767 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1774 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1775 if (put_user(mode_group
->id_list
[i
],
1776 crtc_id
+ copied
)) {
1784 card_res
->count_crtcs
= crtc_count
;
1787 if (card_res
->count_encoders
>= encoder_count
) {
1789 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1791 list_for_each_entry(encoder
,
1792 &dev
->mode_config
.encoder_list
,
1794 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1796 if (put_user(encoder
->base
.id
, encoder_id
+
1804 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1805 if (put_user(mode_group
->id_list
[i
],
1806 encoder_id
+ copied
)) {
1815 card_res
->count_encoders
= encoder_count
;
1818 if (card_res
->count_connectors
>= connector_count
) {
1820 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1822 list_for_each_entry(connector
,
1823 &dev
->mode_config
.connector_list
,
1825 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1828 if (put_user(connector
->base
.id
,
1829 connector_id
+ copied
)) {
1836 int start
= mode_group
->num_crtcs
+
1837 mode_group
->num_encoders
;
1838 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1839 if (put_user(mode_group
->id_list
[i
],
1840 connector_id
+ copied
)) {
1848 card_res
->count_connectors
= connector_count
;
1850 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1851 card_res
->count_connectors
, card_res
->count_encoders
);
1854 drm_modeset_unlock_all(dev
);
1859 * drm_mode_getcrtc - get CRTC configuration
1860 * @dev: drm device for the ioctl
1861 * @data: data pointer for the ioctl
1862 * @file_priv: drm file for the ioctl call
1864 * Construct a CRTC configuration structure to return to the user.
1866 * Called by the user via ioctl.
1869 * Zero on success, errno on failure.
1871 int drm_mode_getcrtc(struct drm_device
*dev
,
1872 void *data
, struct drm_file
*file_priv
)
1874 struct drm_mode_crtc
*crtc_resp
= data
;
1875 struct drm_crtc
*crtc
;
1878 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1881 drm_modeset_lock_all(dev
);
1883 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
1889 crtc_resp
->x
= crtc
->x
;
1890 crtc_resp
->y
= crtc
->y
;
1891 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1892 if (crtc
->primary
->fb
)
1893 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
1895 crtc_resp
->fb_id
= 0;
1897 if (crtc
->enabled
) {
1899 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1900 crtc_resp
->mode_valid
= 1;
1903 crtc_resp
->mode_valid
= 0;
1907 drm_modeset_unlock_all(dev
);
1911 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
1912 const struct drm_file
*file_priv
)
1915 * If user-space hasn't configured the driver to expose the stereo 3D
1916 * modes, don't expose them.
1918 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
1925 * drm_mode_getconnector - get connector configuration
1926 * @dev: drm device for the ioctl
1927 * @data: data pointer for the ioctl
1928 * @file_priv: drm file for the ioctl call
1930 * Construct a connector configuration structure to return to the user.
1932 * Called by the user via ioctl.
1935 * Zero on success, errno on failure.
1937 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
1938 struct drm_file
*file_priv
)
1940 struct drm_mode_get_connector
*out_resp
= data
;
1941 struct drm_connector
*connector
;
1942 struct drm_display_mode
*mode
;
1944 int props_count
= 0;
1945 int encoders_count
= 0;
1949 struct drm_mode_modeinfo u_mode
;
1950 struct drm_mode_modeinfo __user
*mode_ptr
;
1951 uint32_t __user
*prop_ptr
;
1952 uint64_t __user
*prop_values
;
1953 uint32_t __user
*encoder_ptr
;
1955 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1958 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
1960 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
1962 mutex_lock(&dev
->mode_config
.mutex
);
1964 connector
= drm_connector_find(dev
, out_resp
->connector_id
);
1970 props_count
= connector
->properties
.count
;
1972 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1973 if (connector
->encoder_ids
[i
] != 0) {
1978 if (out_resp
->count_modes
== 0) {
1979 connector
->funcs
->fill_modes(connector
,
1980 dev
->mode_config
.max_width
,
1981 dev
->mode_config
.max_height
);
1984 /* delayed so we get modes regardless of pre-fill_modes state */
1985 list_for_each_entry(mode
, &connector
->modes
, head
)
1986 if (drm_mode_expose_to_userspace(mode
, file_priv
))
1989 out_resp
->connector_id
= connector
->base
.id
;
1990 out_resp
->connector_type
= connector
->connector_type
;
1991 out_resp
->connector_type_id
= connector
->connector_type_id
;
1992 out_resp
->mm_width
= connector
->display_info
.width_mm
;
1993 out_resp
->mm_height
= connector
->display_info
.height_mm
;
1994 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
1995 out_resp
->connection
= connector
->status
;
1996 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1997 if (connector
->encoder
)
1998 out_resp
->encoder_id
= connector
->encoder
->base
.id
;
2000 out_resp
->encoder_id
= 0;
2001 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2004 * This ioctl is called twice, once to determine how much space is
2005 * needed, and the 2nd time to fill it.
2007 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
2009 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
2010 list_for_each_entry(mode
, &connector
->modes
, head
) {
2011 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
2014 drm_crtc_convert_to_umode(&u_mode
, mode
);
2015 if (copy_to_user(mode_ptr
+ copied
,
2016 &u_mode
, sizeof(u_mode
))) {
2023 out_resp
->count_modes
= mode_count
;
2025 if ((out_resp
->count_props
>= props_count
) && props_count
) {
2027 prop_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
);
2028 prop_values
= (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
);
2029 for (i
= 0; i
< connector
->properties
.count
; i
++) {
2030 if (put_user(connector
->properties
.ids
[i
],
2031 prop_ptr
+ copied
)) {
2036 if (put_user(connector
->properties
.values
[i
],
2037 prop_values
+ copied
)) {
2044 out_resp
->count_props
= props_count
;
2046 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
2048 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
2049 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
2050 if (connector
->encoder_ids
[i
] != 0) {
2051 if (put_user(connector
->encoder_ids
[i
],
2052 encoder_ptr
+ copied
)) {
2060 out_resp
->count_encoders
= encoders_count
;
2063 mutex_unlock(&dev
->mode_config
.mutex
);
2069 * drm_mode_getencoder - get encoder configuration
2070 * @dev: drm device for the ioctl
2071 * @data: data pointer for the ioctl
2072 * @file_priv: drm file for the ioctl call
2074 * Construct a encoder configuration structure to return to the user.
2076 * Called by the user via ioctl.
2079 * Zero on success, errno on failure.
2081 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
2082 struct drm_file
*file_priv
)
2084 struct drm_mode_get_encoder
*enc_resp
= data
;
2085 struct drm_encoder
*encoder
;
2088 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2091 drm_modeset_lock_all(dev
);
2092 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
2099 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
2101 enc_resp
->crtc_id
= 0;
2102 enc_resp
->encoder_type
= encoder
->encoder_type
;
2103 enc_resp
->encoder_id
= encoder
->base
.id
;
2104 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
2105 enc_resp
->possible_clones
= encoder
->possible_clones
;
2108 drm_modeset_unlock_all(dev
);
2113 * drm_mode_getplane_res - enumerate all plane resources
2116 * @file_priv: DRM file info
2118 * Construct a list of plane ids to return to the user.
2120 * Called by the user via ioctl.
2123 * Zero on success, errno on failure.
2125 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2126 struct drm_file
*file_priv
)
2128 struct drm_mode_get_plane_res
*plane_resp
= data
;
2129 struct drm_mode_config
*config
;
2130 struct drm_plane
*plane
;
2131 uint32_t __user
*plane_ptr
;
2132 int copied
= 0, ret
= 0;
2133 unsigned num_planes
;
2135 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2138 drm_modeset_lock_all(dev
);
2139 config
= &dev
->mode_config
;
2141 if (file_priv
->universal_planes
)
2142 num_planes
= config
->num_total_plane
;
2144 num_planes
= config
->num_overlay_plane
;
2147 * This ioctl is called twice, once to determine how much space is
2148 * needed, and the 2nd time to fill it.
2151 (plane_resp
->count_planes
>= num_planes
)) {
2152 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2154 list_for_each_entry(plane
, &config
->plane_list
, head
) {
2156 * Unless userspace set the 'universal planes'
2157 * capability bit, only advertise overlays.
2159 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2160 !file_priv
->universal_planes
)
2163 if (put_user(plane
->base
.id
, plane_ptr
+ copied
)) {
2170 plane_resp
->count_planes
= num_planes
;
2173 drm_modeset_unlock_all(dev
);
2178 * drm_mode_getplane - get plane configuration
2181 * @file_priv: DRM file info
2183 * Construct a plane configuration structure to return to the user.
2185 * Called by the user via ioctl.
2188 * Zero on success, errno on failure.
2190 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2191 struct drm_file
*file_priv
)
2193 struct drm_mode_get_plane
*plane_resp
= data
;
2194 struct drm_plane
*plane
;
2195 uint32_t __user
*format_ptr
;
2198 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2201 drm_modeset_lock_all(dev
);
2202 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2209 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2211 plane_resp
->crtc_id
= 0;
2214 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2216 plane_resp
->fb_id
= 0;
2218 plane_resp
->plane_id
= plane
->base
.id
;
2219 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2220 plane_resp
->gamma_size
= 0;
2223 * This ioctl is called twice, once to determine how much space is
2224 * needed, and the 2nd time to fill it.
2226 if (plane
->format_count
&&
2227 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2228 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2229 if (copy_to_user(format_ptr
,
2230 plane
->format_types
,
2231 sizeof(uint32_t) * plane
->format_count
)) {
2236 plane_resp
->count_format_types
= plane
->format_count
;
2239 drm_modeset_unlock_all(dev
);
2244 * setplane_internal - setplane handler for internal callers
2246 * Note that we assume an extra reference has already been taken on fb. If the
2247 * update fails, this reference will be dropped before return; if it succeeds,
2248 * the previous framebuffer (if any) will be unreferenced instead.
2250 * src_{x,y,w,h} are provided in 16.16 fixed point format
2252 static int setplane_internal(struct drm_plane
*plane
,
2253 struct drm_crtc
*crtc
,
2254 struct drm_framebuffer
*fb
,
2255 int32_t crtc_x
, int32_t crtc_y
,
2256 uint32_t crtc_w
, uint32_t crtc_h
,
2257 /* src_{x,y,w,h} values are 16.16 fixed point */
2258 uint32_t src_x
, uint32_t src_y
,
2259 uint32_t src_w
, uint32_t src_h
)
2261 struct drm_device
*dev
= plane
->dev
;
2262 struct drm_framebuffer
*old_fb
= NULL
;
2264 unsigned int fb_width
, fb_height
;
2267 /* No fb means shut it down */
2269 drm_modeset_lock_all(dev
);
2271 ret
= plane
->funcs
->disable_plane(plane
);
2278 drm_modeset_unlock_all(dev
);
2282 /* Check whether this plane is usable on this CRTC */
2283 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2284 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2289 /* Check whether this plane supports the fb pixel format. */
2290 for (i
= 0; i
< plane
->format_count
; i
++)
2291 if (fb
->pixel_format
== plane
->format_types
[i
])
2293 if (i
== plane
->format_count
) {
2294 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2295 drm_get_format_name(fb
->pixel_format
));
2300 fb_width
= fb
->width
<< 16;
2301 fb_height
= fb
->height
<< 16;
2303 /* Make sure source coordinates are inside the fb. */
2304 if (src_w
> fb_width
||
2305 src_x
> fb_width
- src_w
||
2306 src_h
> fb_height
||
2307 src_y
> fb_height
- src_h
) {
2308 DRM_DEBUG_KMS("Invalid source coordinates "
2309 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2310 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
2311 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
2312 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
2313 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
2318 drm_modeset_lock_all(dev
);
2320 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2321 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2322 src_x
, src_y
, src_w
, src_h
);
2330 drm_modeset_unlock_all(dev
);
2334 drm_framebuffer_unreference(fb
);
2336 drm_framebuffer_unreference(old_fb
);
2343 * drm_mode_setplane - configure a plane's configuration
2345 * @data: ioctl data*
2346 * @file_priv: DRM file info
2348 * Set plane configuration, including placement, fb, scaling, and other factors.
2349 * Or pass a NULL fb to disable (planes may be disabled without providing a
2353 * Zero on success, errno on failure.
2355 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2356 struct drm_file
*file_priv
)
2358 struct drm_mode_set_plane
*plane_req
= data
;
2359 struct drm_mode_object
*obj
;
2360 struct drm_plane
*plane
;
2361 struct drm_crtc
*crtc
= NULL
;
2362 struct drm_framebuffer
*fb
= NULL
;
2364 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2367 /* Give drivers some help against integer overflows */
2368 if (plane_req
->crtc_w
> INT_MAX
||
2369 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
2370 plane_req
->crtc_h
> INT_MAX
||
2371 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
2372 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2373 plane_req
->crtc_w
, plane_req
->crtc_h
,
2374 plane_req
->crtc_x
, plane_req
->crtc_y
);
2379 * First, find the plane, crtc, and fb objects. If not available,
2380 * we don't bother to call the driver.
2382 obj
= drm_mode_object_find(dev
, plane_req
->plane_id
,
2383 DRM_MODE_OBJECT_PLANE
);
2385 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2386 plane_req
->plane_id
);
2389 plane
= obj_to_plane(obj
);
2391 if (plane_req
->fb_id
) {
2392 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2394 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2399 obj
= drm_mode_object_find(dev
, plane_req
->crtc_id
,
2400 DRM_MODE_OBJECT_CRTC
);
2402 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2403 plane_req
->crtc_id
);
2406 crtc
= obj_to_crtc(obj
);
2410 * setplane_internal will take care of deref'ing either the old or new
2411 * framebuffer depending on success.
2413 return setplane_internal(plane
, crtc
, fb
,
2414 plane_req
->crtc_x
, plane_req
->crtc_y
,
2415 plane_req
->crtc_w
, plane_req
->crtc_h
,
2416 plane_req
->src_x
, plane_req
->src_y
,
2417 plane_req
->src_w
, plane_req
->src_h
);
2421 * drm_mode_set_config_internal - helper to call ->set_config
2422 * @set: modeset config to set
2424 * This is a little helper to wrap internal calls to the ->set_config driver
2425 * interface. The only thing it adds is correct refcounting dance.
2428 * Zero on success, errno on failure.
2430 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2432 struct drm_crtc
*crtc
= set
->crtc
;
2433 struct drm_framebuffer
*fb
;
2434 struct drm_crtc
*tmp
;
2438 * NOTE: ->set_config can also disable other crtcs (if we steal all
2439 * connectors from it), hence we need to refcount the fbs across all
2440 * crtcs. Atomic modeset will have saner semantics ...
2442 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
)
2443 tmp
->old_fb
= tmp
->primary
->fb
;
2447 ret
= crtc
->funcs
->set_config(set
);
2449 crtc
->primary
->crtc
= crtc
;
2450 crtc
->primary
->fb
= fb
;
2453 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
2454 if (tmp
->primary
->fb
)
2455 drm_framebuffer_reference(tmp
->primary
->fb
);
2457 drm_framebuffer_unreference(tmp
->old_fb
);
2462 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2465 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2467 * @crtc: CRTC that framebuffer will be displayed on
2470 * @mode: mode that framebuffer will be displayed under
2471 * @fb: framebuffer to check size of
2473 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2475 const struct drm_display_mode
*mode
,
2476 const struct drm_framebuffer
*fb
)
2479 int hdisplay
, vdisplay
;
2481 hdisplay
= mode
->hdisplay
;
2482 vdisplay
= mode
->vdisplay
;
2484 if (drm_mode_is_stereo(mode
)) {
2485 struct drm_display_mode adjusted
= *mode
;
2487 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE
);
2488 hdisplay
= adjusted
.crtc_hdisplay
;
2489 vdisplay
= adjusted
.crtc_vdisplay
;
2492 if (crtc
->invert_dimensions
)
2493 swap(hdisplay
, vdisplay
);
2495 if (hdisplay
> fb
->width
||
2496 vdisplay
> fb
->height
||
2497 x
> fb
->width
- hdisplay
||
2498 y
> fb
->height
- vdisplay
) {
2499 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2500 fb
->width
, fb
->height
, hdisplay
, vdisplay
, x
, y
,
2501 crtc
->invert_dimensions
? " (inverted)" : "");
2507 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2510 * drm_mode_setcrtc - set CRTC configuration
2511 * @dev: drm device for the ioctl
2512 * @data: data pointer for the ioctl
2513 * @file_priv: drm file for the ioctl call
2515 * Build a new CRTC configuration based on user request.
2517 * Called by the user via ioctl.
2520 * Zero on success, errno on failure.
2522 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2523 struct drm_file
*file_priv
)
2525 struct drm_mode_config
*config
= &dev
->mode_config
;
2526 struct drm_mode_crtc
*crtc_req
= data
;
2527 struct drm_crtc
*crtc
;
2528 struct drm_connector
**connector_set
= NULL
, *connector
;
2529 struct drm_framebuffer
*fb
= NULL
;
2530 struct drm_display_mode
*mode
= NULL
;
2531 struct drm_mode_set set
;
2532 uint32_t __user
*set_connectors_ptr
;
2536 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2539 /* For some reason crtc x/y offsets are signed internally. */
2540 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
2543 drm_modeset_lock_all(dev
);
2544 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2546 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2550 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
2552 if (crtc_req
->mode_valid
) {
2553 /* If we have a mode we need a framebuffer. */
2554 /* If we pass -1, set the mode with the currently bound fb */
2555 if (crtc_req
->fb_id
== -1) {
2556 if (!crtc
->primary
->fb
) {
2557 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2561 fb
= crtc
->primary
->fb
;
2562 /* Make refcounting symmetric with the lookup path. */
2563 drm_framebuffer_reference(fb
);
2565 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2567 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2574 mode
= drm_mode_create(dev
);
2580 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
2582 DRM_DEBUG_KMS("Invalid mode\n");
2586 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2588 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2595 if (crtc_req
->count_connectors
== 0 && mode
) {
2596 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2601 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2602 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2603 crtc_req
->count_connectors
);
2608 if (crtc_req
->count_connectors
> 0) {
2611 /* Avoid unbounded kernel memory allocation */
2612 if (crtc_req
->count_connectors
> config
->num_connector
) {
2617 connector_set
= kmalloc(crtc_req
->count_connectors
*
2618 sizeof(struct drm_connector
*),
2620 if (!connector_set
) {
2625 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2626 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2627 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2632 connector
= drm_connector_find(dev
, out_id
);
2634 DRM_DEBUG_KMS("Connector id %d unknown\n",
2639 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2643 connector_set
[i
] = connector
;
2648 set
.x
= crtc_req
->x
;
2649 set
.y
= crtc_req
->y
;
2651 set
.connectors
= connector_set
;
2652 set
.num_connectors
= crtc_req
->count_connectors
;
2654 ret
= drm_mode_set_config_internal(&set
);
2658 drm_framebuffer_unreference(fb
);
2660 kfree(connector_set
);
2661 drm_mode_destroy(dev
, mode
);
2662 drm_modeset_unlock_all(dev
);
2667 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2668 * universal plane handler call
2669 * @crtc: crtc to update cursor for
2670 * @req: data pointer for the ioctl
2671 * @file_priv: drm file for the ioctl call
2673 * Legacy cursor ioctl's work directly with driver buffer handles. To
2674 * translate legacy ioctl calls into universal plane handler calls, we need to
2675 * wrap the native buffer handle in a drm_framebuffer.
2677 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2678 * buffer with a pitch of 4*width; the universal plane interface should be used
2679 * directly in cases where the hardware can support other buffer settings and
2680 * userspace wants to make use of these capabilities.
2683 * Zero on success, errno on failure.
2685 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
2686 struct drm_mode_cursor2
*req
,
2687 struct drm_file
*file_priv
)
2689 struct drm_device
*dev
= crtc
->dev
;
2690 struct drm_framebuffer
*fb
= NULL
;
2691 struct drm_mode_fb_cmd2 fbreq
= {
2692 .width
= req
->width
,
2693 .height
= req
->height
,
2694 .pixel_format
= DRM_FORMAT_ARGB8888
,
2695 .pitches
= { req
->width
* 4 },
2696 .handles
= { req
->handle
},
2698 int32_t crtc_x
, crtc_y
;
2699 uint32_t crtc_w
= 0, crtc_h
= 0;
2700 uint32_t src_w
= 0, src_h
= 0;
2703 BUG_ON(!crtc
->cursor
);
2706 * Obtain fb we'll be using (either new or existing) and take an extra
2707 * reference to it if fb != null. setplane will take care of dropping
2708 * the reference if the plane update fails.
2710 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2712 fb
= add_framebuffer_internal(dev
, &fbreq
, file_priv
);
2714 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2718 drm_framebuffer_reference(fb
);
2723 mutex_lock(&dev
->mode_config
.mutex
);
2724 fb
= crtc
->cursor
->fb
;
2726 drm_framebuffer_reference(fb
);
2727 mutex_unlock(&dev
->mode_config
.mutex
);
2730 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2734 crtc_x
= crtc
->cursor_x
;
2735 crtc_y
= crtc
->cursor_y
;
2740 crtc_h
= fb
->height
;
2741 src_w
= fb
->width
<< 16;
2742 src_h
= fb
->height
<< 16;
2746 * setplane_internal will take care of deref'ing either the old or new
2747 * framebuffer depending on success.
2749 ret
= setplane_internal(crtc
->cursor
, crtc
, fb
,
2750 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2751 0, 0, src_w
, src_h
);
2753 /* Update successful; save new cursor position, if necessary */
2754 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2755 crtc
->cursor_x
= req
->x
;
2756 crtc
->cursor_y
= req
->y
;
2762 static int drm_mode_cursor_common(struct drm_device
*dev
,
2763 struct drm_mode_cursor2
*req
,
2764 struct drm_file
*file_priv
)
2766 struct drm_crtc
*crtc
;
2769 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2772 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2775 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
2777 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2782 * If this crtc has a universal cursor plane, call that plane's update
2783 * handler rather than using legacy cursor handlers.
2786 return drm_mode_cursor_universal(crtc
, req
, file_priv
);
2788 drm_modeset_lock(&crtc
->mutex
, NULL
);
2789 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2790 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2794 /* Turns off the cursor if handle is 0 */
2795 if (crtc
->funcs
->cursor_set2
)
2796 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2797 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2799 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2800 req
->width
, req
->height
);
2803 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2804 if (crtc
->funcs
->cursor_move
) {
2805 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2812 drm_modeset_unlock(&crtc
->mutex
);
2820 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2821 * @dev: drm device for the ioctl
2822 * @data: data pointer for the ioctl
2823 * @file_priv: drm file for the ioctl call
2825 * Set the cursor configuration based on user request.
2827 * Called by the user via ioctl.
2830 * Zero on success, errno on failure.
2832 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
2833 void *data
, struct drm_file
*file_priv
)
2835 struct drm_mode_cursor
*req
= data
;
2836 struct drm_mode_cursor2 new_req
;
2838 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
2839 new_req
.hot_x
= new_req
.hot_y
= 0;
2841 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
2845 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2846 * @dev: drm device for the ioctl
2847 * @data: data pointer for the ioctl
2848 * @file_priv: drm file for the ioctl call
2850 * Set the cursor configuration based on user request. This implements the 2nd
2851 * version of the cursor ioctl, which allows userspace to additionally specify
2852 * the hotspot of the pointer.
2854 * Called by the user via ioctl.
2857 * Zero on success, errno on failure.
2859 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
2860 void *data
, struct drm_file
*file_priv
)
2862 struct drm_mode_cursor2
*req
= data
;
2863 return drm_mode_cursor_common(dev
, req
, file_priv
);
2867 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2868 * @bpp: bits per pixels
2869 * @depth: bit depth per pixel
2871 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2872 * Useful in fbdev emulation code, since that deals in those values.
2874 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2880 fmt
= DRM_FORMAT_C8
;
2884 fmt
= DRM_FORMAT_XRGB1555
;
2886 fmt
= DRM_FORMAT_RGB565
;
2889 fmt
= DRM_FORMAT_RGB888
;
2893 fmt
= DRM_FORMAT_XRGB8888
;
2894 else if (depth
== 30)
2895 fmt
= DRM_FORMAT_XRGB2101010
;
2897 fmt
= DRM_FORMAT_ARGB8888
;
2900 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2901 fmt
= DRM_FORMAT_XRGB8888
;
2907 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2910 * drm_mode_addfb - add an FB to the graphics configuration
2911 * @dev: drm device for the ioctl
2912 * @data: data pointer for the ioctl
2913 * @file_priv: drm file for the ioctl call
2915 * Add a new FB to the specified CRTC, given a user request. This is the
2916 * original addfb ioclt which only supported RGB formats.
2918 * Called by the user via ioctl.
2921 * Zero on success, errno on failure.
2923 int drm_mode_addfb(struct drm_device
*dev
,
2924 void *data
, struct drm_file
*file_priv
)
2926 struct drm_mode_fb_cmd
*or = data
;
2927 struct drm_mode_fb_cmd2 r
= {};
2928 struct drm_mode_config
*config
= &dev
->mode_config
;
2929 struct drm_framebuffer
*fb
;
2932 /* Use new struct with format internally */
2933 r
.fb_id
= or->fb_id
;
2934 r
.width
= or->width
;
2935 r
.height
= or->height
;
2936 r
.pitches
[0] = or->pitch
;
2937 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2938 r
.handles
[0] = or->handle
;
2940 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2943 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2946 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2949 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2951 DRM_DEBUG_KMS("could not create framebuffer\n");
2955 mutex_lock(&file_priv
->fbs_lock
);
2956 or->fb_id
= fb
->base
.id
;
2957 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2958 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2959 mutex_unlock(&file_priv
->fbs_lock
);
2964 static int format_check(const struct drm_mode_fb_cmd2
*r
)
2966 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2970 case DRM_FORMAT_RGB332
:
2971 case DRM_FORMAT_BGR233
:
2972 case DRM_FORMAT_XRGB4444
:
2973 case DRM_FORMAT_XBGR4444
:
2974 case DRM_FORMAT_RGBX4444
:
2975 case DRM_FORMAT_BGRX4444
:
2976 case DRM_FORMAT_ARGB4444
:
2977 case DRM_FORMAT_ABGR4444
:
2978 case DRM_FORMAT_RGBA4444
:
2979 case DRM_FORMAT_BGRA4444
:
2980 case DRM_FORMAT_XRGB1555
:
2981 case DRM_FORMAT_XBGR1555
:
2982 case DRM_FORMAT_RGBX5551
:
2983 case DRM_FORMAT_BGRX5551
:
2984 case DRM_FORMAT_ARGB1555
:
2985 case DRM_FORMAT_ABGR1555
:
2986 case DRM_FORMAT_RGBA5551
:
2987 case DRM_FORMAT_BGRA5551
:
2988 case DRM_FORMAT_RGB565
:
2989 case DRM_FORMAT_BGR565
:
2990 case DRM_FORMAT_RGB888
:
2991 case DRM_FORMAT_BGR888
:
2992 case DRM_FORMAT_XRGB8888
:
2993 case DRM_FORMAT_XBGR8888
:
2994 case DRM_FORMAT_RGBX8888
:
2995 case DRM_FORMAT_BGRX8888
:
2996 case DRM_FORMAT_ARGB8888
:
2997 case DRM_FORMAT_ABGR8888
:
2998 case DRM_FORMAT_RGBA8888
:
2999 case DRM_FORMAT_BGRA8888
:
3000 case DRM_FORMAT_XRGB2101010
:
3001 case DRM_FORMAT_XBGR2101010
:
3002 case DRM_FORMAT_RGBX1010102
:
3003 case DRM_FORMAT_BGRX1010102
:
3004 case DRM_FORMAT_ARGB2101010
:
3005 case DRM_FORMAT_ABGR2101010
:
3006 case DRM_FORMAT_RGBA1010102
:
3007 case DRM_FORMAT_BGRA1010102
:
3008 case DRM_FORMAT_YUYV
:
3009 case DRM_FORMAT_YVYU
:
3010 case DRM_FORMAT_UYVY
:
3011 case DRM_FORMAT_VYUY
:
3012 case DRM_FORMAT_AYUV
:
3013 case DRM_FORMAT_NV12
:
3014 case DRM_FORMAT_NV21
:
3015 case DRM_FORMAT_NV16
:
3016 case DRM_FORMAT_NV61
:
3017 case DRM_FORMAT_NV24
:
3018 case DRM_FORMAT_NV42
:
3019 case DRM_FORMAT_YUV410
:
3020 case DRM_FORMAT_YVU410
:
3021 case DRM_FORMAT_YUV411
:
3022 case DRM_FORMAT_YVU411
:
3023 case DRM_FORMAT_YUV420
:
3024 case DRM_FORMAT_YVU420
:
3025 case DRM_FORMAT_YUV422
:
3026 case DRM_FORMAT_YVU422
:
3027 case DRM_FORMAT_YUV444
:
3028 case DRM_FORMAT_YVU444
:
3031 DRM_DEBUG_KMS("invalid pixel format %s\n",
3032 drm_get_format_name(r
->pixel_format
));
3037 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
3039 int ret
, hsub
, vsub
, num_planes
, i
;
3041 ret
= format_check(r
);
3043 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3044 drm_get_format_name(r
->pixel_format
));
3048 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
3049 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
3050 num_planes
= drm_format_num_planes(r
->pixel_format
);
3052 if (r
->width
== 0 || r
->width
% hsub
) {
3053 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
3057 if (r
->height
== 0 || r
->height
% vsub
) {
3058 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
3062 for (i
= 0; i
< num_planes
; i
++) {
3063 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
3064 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
3065 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
3067 if (!r
->handles
[i
]) {
3068 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
3072 if ((uint64_t) width
* cpp
> UINT_MAX
)
3075 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
3078 if (r
->pitches
[i
] < width
* cpp
) {
3079 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
3087 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
3088 struct drm_mode_fb_cmd2
*r
,
3089 struct drm_file
*file_priv
)
3091 struct drm_mode_config
*config
= &dev
->mode_config
;
3092 struct drm_framebuffer
*fb
;
3095 if (r
->flags
& ~DRM_MODE_FB_INTERLACED
) {
3096 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
3097 return ERR_PTR(-EINVAL
);
3100 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
3101 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3102 r
->width
, config
->min_width
, config
->max_width
);
3103 return ERR_PTR(-EINVAL
);
3105 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
3106 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3107 r
->height
, config
->min_height
, config
->max_height
);
3108 return ERR_PTR(-EINVAL
);
3111 ret
= framebuffer_check(r
);
3113 return ERR_PTR(ret
);
3115 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
3117 DRM_DEBUG_KMS("could not create framebuffer\n");
3121 mutex_lock(&file_priv
->fbs_lock
);
3122 r
->fb_id
= fb
->base
.id
;
3123 list_add(&fb
->filp_head
, &file_priv
->fbs
);
3124 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
3125 mutex_unlock(&file_priv
->fbs_lock
);
3131 * drm_mode_addfb2 - add an FB to the graphics configuration
3132 * @dev: drm device for the ioctl
3133 * @data: data pointer for the ioctl
3134 * @file_priv: drm file for the ioctl call
3136 * Add a new FB to the specified CRTC, given a user request with format. This is
3137 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3138 * and uses fourcc codes as pixel format specifiers.
3140 * Called by the user via ioctl.
3143 * Zero on success, errno on failure.
3145 int drm_mode_addfb2(struct drm_device
*dev
,
3146 void *data
, struct drm_file
*file_priv
)
3148 struct drm_framebuffer
*fb
;
3150 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3153 fb
= add_framebuffer_internal(dev
, data
, file_priv
);
3161 * drm_mode_rmfb - remove an FB from the configuration
3162 * @dev: drm device for the ioctl
3163 * @data: data pointer for the ioctl
3164 * @file_priv: drm file for the ioctl call
3166 * Remove the FB specified by the user.
3168 * Called by the user via ioctl.
3171 * Zero on success, errno on failure.
3173 int drm_mode_rmfb(struct drm_device
*dev
,
3174 void *data
, struct drm_file
*file_priv
)
3176 struct drm_framebuffer
*fb
= NULL
;
3177 struct drm_framebuffer
*fbl
= NULL
;
3178 uint32_t *id
= data
;
3181 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3184 mutex_lock(&file_priv
->fbs_lock
);
3185 mutex_lock(&dev
->mode_config
.fb_lock
);
3186 fb
= __drm_framebuffer_lookup(dev
, *id
);
3190 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
3196 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3197 __drm_framebuffer_unregister(dev
, fb
);
3199 list_del_init(&fb
->filp_head
);
3200 mutex_unlock(&dev
->mode_config
.fb_lock
);
3201 mutex_unlock(&file_priv
->fbs_lock
);
3203 drm_framebuffer_remove(fb
);
3208 mutex_unlock(&dev
->mode_config
.fb_lock
);
3209 mutex_unlock(&file_priv
->fbs_lock
);
3215 * drm_mode_getfb - get FB info
3216 * @dev: drm device for the ioctl
3217 * @data: data pointer for the ioctl
3218 * @file_priv: drm file for the ioctl call
3220 * Lookup the FB given its ID and return info about it.
3222 * Called by the user via ioctl.
3225 * Zero on success, errno on failure.
3227 int drm_mode_getfb(struct drm_device
*dev
,
3228 void *data
, struct drm_file
*file_priv
)
3230 struct drm_mode_fb_cmd
*r
= data
;
3231 struct drm_framebuffer
*fb
;
3234 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3237 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3241 r
->height
= fb
->height
;
3242 r
->width
= fb
->width
;
3243 r
->depth
= fb
->depth
;
3244 r
->bpp
= fb
->bits_per_pixel
;
3245 r
->pitch
= fb
->pitches
[0];
3246 if (fb
->funcs
->create_handle
) {
3247 if (drm_is_master(file_priv
) || capable(CAP_SYS_ADMIN
) ||
3248 drm_is_control_client(file_priv
)) {
3249 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3252 /* GET_FB() is an unprivileged ioctl so we must not
3253 * return a buffer-handle to non-master processes! For
3254 * backwards-compatibility reasons, we cannot make
3255 * GET_FB() privileged, so just return an invalid handle
3256 * for non-masters. */
3264 drm_framebuffer_unreference(fb
);
3270 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3271 * @dev: drm device for the ioctl
3272 * @data: data pointer for the ioctl
3273 * @file_priv: drm file for the ioctl call
3275 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3276 * rectangle list. Generic userspace which does frontbuffer rendering must call
3277 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3278 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3280 * Modesetting drivers which always update the frontbuffer do not need to
3281 * implement the corresponding ->dirty framebuffer callback.
3283 * Called by the user via ioctl.
3286 * Zero on success, errno on failure.
3288 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3289 void *data
, struct drm_file
*file_priv
)
3291 struct drm_clip_rect __user
*clips_ptr
;
3292 struct drm_clip_rect
*clips
= NULL
;
3293 struct drm_mode_fb_dirty_cmd
*r
= data
;
3294 struct drm_framebuffer
*fb
;
3299 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3302 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3306 num_clips
= r
->num_clips
;
3307 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3309 if (!num_clips
!= !clips_ptr
) {
3314 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3316 /* If userspace annotates copy, clips must come in pairs */
3317 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3322 if (num_clips
&& clips_ptr
) {
3323 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3327 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
3333 ret
= copy_from_user(clips
, clips_ptr
,
3334 num_clips
* sizeof(*clips
));
3341 if (fb
->funcs
->dirty
) {
3342 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3351 drm_framebuffer_unreference(fb
);
3358 * drm_fb_release - remove and free the FBs on this file
3359 * @priv: drm file for the ioctl
3361 * Destroy all the FBs associated with @filp.
3363 * Called by the user via ioctl.
3366 * Zero on success, errno on failure.
3368 void drm_fb_release(struct drm_file
*priv
)
3370 struct drm_device
*dev
= priv
->minor
->dev
;
3371 struct drm_framebuffer
*fb
, *tfb
;
3373 mutex_lock(&priv
->fbs_lock
);
3374 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3376 mutex_lock(&dev
->mode_config
.fb_lock
);
3377 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3378 __drm_framebuffer_unregister(dev
, fb
);
3379 mutex_unlock(&dev
->mode_config
.fb_lock
);
3381 list_del_init(&fb
->filp_head
);
3383 /* This will also drop the fpriv->fbs reference. */
3384 drm_framebuffer_remove(fb
);
3386 mutex_unlock(&priv
->fbs_lock
);
3390 * drm_property_create - create a new property type
3392 * @flags: flags specifying the property type
3393 * @name: name of the property
3394 * @num_values: number of pre-defined values
3396 * This creates a new generic drm property which can then be attached to a drm
3397 * object with drm_object_attach_property. The returned property object must be
3398 * freed with drm_property_destroy.
3401 * A pointer to the newly created property on success, NULL on failure.
3403 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3404 const char *name
, int num_values
)
3406 struct drm_property
*property
= NULL
;
3409 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3413 property
->dev
= dev
;
3416 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
3417 if (!property
->values
)
3421 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3425 property
->flags
= flags
;
3426 property
->num_values
= num_values
;
3427 INIT_LIST_HEAD(&property
->enum_blob_list
);
3430 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3431 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3434 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3436 WARN_ON(!drm_property_type_valid(property
));
3440 kfree(property
->values
);
3444 EXPORT_SYMBOL(drm_property_create
);
3447 * drm_property_create_enum - create a new enumeration property type
3449 * @flags: flags specifying the property type
3450 * @name: name of the property
3451 * @props: enumeration lists with property values
3452 * @num_values: number of pre-defined values
3454 * This creates a new generic drm property which can then be attached to a drm
3455 * object with drm_object_attach_property. The returned property object must be
3456 * freed with drm_property_destroy.
3458 * Userspace is only allowed to set one of the predefined values for enumeration
3462 * A pointer to the newly created property on success, NULL on failure.
3464 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3466 const struct drm_prop_enum_list
*props
,
3469 struct drm_property
*property
;
3472 flags
|= DRM_MODE_PROP_ENUM
;
3474 property
= drm_property_create(dev
, flags
, name
, num_values
);
3478 for (i
= 0; i
< num_values
; i
++) {
3479 ret
= drm_property_add_enum(property
, i
,
3483 drm_property_destroy(dev
, property
);
3490 EXPORT_SYMBOL(drm_property_create_enum
);
3493 * drm_property_create_bitmask - create a new bitmask property type
3495 * @flags: flags specifying the property type
3496 * @name: name of the property
3497 * @props: enumeration lists with property bitflags
3498 * @num_values: number of pre-defined values
3500 * This creates a new generic drm property which can then be attached to a drm
3501 * object with drm_object_attach_property. The returned property object must be
3502 * freed with drm_property_destroy.
3504 * Compared to plain enumeration properties userspace is allowed to set any
3505 * or'ed together combination of the predefined property bitflag values
3508 * A pointer to the newly created property on success, NULL on failure.
3510 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3511 int flags
, const char *name
,
3512 const struct drm_prop_enum_list
*props
,
3514 uint64_t supported_bits
)
3516 struct drm_property
*property
;
3517 int i
, ret
, index
= 0;
3518 int num_values
= hweight64(supported_bits
);
3520 flags
|= DRM_MODE_PROP_BITMASK
;
3522 property
= drm_property_create(dev
, flags
, name
, num_values
);
3525 for (i
= 0; i
< num_props
; i
++) {
3526 if (!(supported_bits
& (1ULL << props
[i
].type
)))
3529 if (WARN_ON(index
>= num_values
)) {
3530 drm_property_destroy(dev
, property
);
3534 ret
= drm_property_add_enum(property
, index
++,
3538 drm_property_destroy(dev
, property
);
3545 EXPORT_SYMBOL(drm_property_create_bitmask
);
3547 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3548 int flags
, const char *name
,
3549 uint64_t min
, uint64_t max
)
3551 struct drm_property
*property
;
3553 property
= drm_property_create(dev
, flags
, name
, 2);
3557 property
->values
[0] = min
;
3558 property
->values
[1] = max
;
3564 * drm_property_create_range - create a new ranged property type
3566 * @flags: flags specifying the property type
3567 * @name: name of the property
3568 * @min: minimum value of the property
3569 * @max: maximum value of the property
3571 * This creates a new generic drm property which can then be attached to a drm
3572 * object with drm_object_attach_property. The returned property object must be
3573 * freed with drm_property_destroy.
3575 * Userspace is allowed to set any interger value in the (min, max) range
3579 * A pointer to the newly created property on success, NULL on failure.
3581 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3583 uint64_t min
, uint64_t max
)
3585 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3588 EXPORT_SYMBOL(drm_property_create_range
);
3590 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3591 int flags
, const char *name
,
3592 int64_t min
, int64_t max
)
3594 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3595 name
, I642U64(min
), I642U64(max
));
3597 EXPORT_SYMBOL(drm_property_create_signed_range
);
3599 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3600 int flags
, const char *name
, uint32_t type
)
3602 struct drm_property
*property
;
3604 flags
|= DRM_MODE_PROP_OBJECT
;
3606 property
= drm_property_create(dev
, flags
, name
, 1);
3610 property
->values
[0] = type
;
3614 EXPORT_SYMBOL(drm_property_create_object
);
3617 * drm_property_add_enum - add a possible value to an enumeration property
3618 * @property: enumeration property to change
3619 * @index: index of the new enumeration
3620 * @value: value of the new enumeration
3621 * @name: symbolic name of the new enumeration
3623 * This functions adds enumerations to a property.
3625 * It's use is deprecated, drivers should use one of the more specific helpers
3626 * to directly create the property with all enumerations already attached.
3629 * Zero on success, error code on failure.
3631 int drm_property_add_enum(struct drm_property
*property
, int index
,
3632 uint64_t value
, const char *name
)
3634 struct drm_property_enum
*prop_enum
;
3636 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3637 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
3641 * Bitmask enum properties have the additional constraint of values
3644 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
3648 if (!list_empty(&property
->enum_blob_list
)) {
3649 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3650 if (prop_enum
->value
== value
) {
3651 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3652 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3658 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3662 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3663 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3664 prop_enum
->value
= value
;
3666 property
->values
[index
] = value
;
3667 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
3670 EXPORT_SYMBOL(drm_property_add_enum
);
3673 * drm_property_destroy - destroy a drm property
3675 * @property: property to destry
3677 * This function frees a property including any attached resources like
3678 * enumeration values.
3680 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
3682 struct drm_property_enum
*prop_enum
, *pt
;
3684 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
3685 list_del(&prop_enum
->head
);
3689 if (property
->num_values
)
3690 kfree(property
->values
);
3691 drm_mode_object_put(dev
, &property
->base
);
3692 list_del(&property
->head
);
3695 EXPORT_SYMBOL(drm_property_destroy
);
3698 * drm_object_attach_property - attach a property to a modeset object
3699 * @obj: drm modeset object
3700 * @property: property to attach
3701 * @init_val: initial value of the property
3703 * This attaches the given property to the modeset object with the given initial
3704 * value. Currently this function cannot fail since the properties are stored in
3705 * a statically sized array.
3707 void drm_object_attach_property(struct drm_mode_object
*obj
,
3708 struct drm_property
*property
,
3711 int count
= obj
->properties
->count
;
3713 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
3714 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3715 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3716 "you see this message on the same object type.\n",
3721 obj
->properties
->ids
[count
] = property
->base
.id
;
3722 obj
->properties
->values
[count
] = init_val
;
3723 obj
->properties
->count
++;
3725 EXPORT_SYMBOL(drm_object_attach_property
);
3728 * drm_object_property_set_value - set the value of a property
3729 * @obj: drm mode object to set property value for
3730 * @property: property to set
3731 * @val: value the property should be set to
3733 * This functions sets a given property on a given object. This function only
3734 * changes the software state of the property, it does not call into the
3735 * driver's ->set_property callback.
3738 * Zero on success, error code on failure.
3740 int drm_object_property_set_value(struct drm_mode_object
*obj
,
3741 struct drm_property
*property
, uint64_t val
)
3745 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3746 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3747 obj
->properties
->values
[i
] = val
;
3754 EXPORT_SYMBOL(drm_object_property_set_value
);
3757 * drm_object_property_get_value - retrieve the value of a property
3758 * @obj: drm mode object to get property value from
3759 * @property: property to retrieve
3760 * @val: storage for the property value
3762 * This function retrieves the softare state of the given property for the given
3763 * property. Since there is no driver callback to retrieve the current property
3764 * value this might be out of sync with the hardware, depending upon the driver
3768 * Zero on success, error code on failure.
3770 int drm_object_property_get_value(struct drm_mode_object
*obj
,
3771 struct drm_property
*property
, uint64_t *val
)
3775 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3776 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3777 *val
= obj
->properties
->values
[i
];
3784 EXPORT_SYMBOL(drm_object_property_get_value
);
3787 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3790 * @file_priv: DRM file info
3792 * This function retrieves the current value for an connectors's property.
3794 * Called by the user via ioctl.
3797 * Zero on success, errno on failure.
3799 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
3800 void *data
, struct drm_file
*file_priv
)
3802 struct drm_mode_get_property
*out_resp
= data
;
3803 struct drm_property
*property
;
3806 int value_count
= 0;
3809 struct drm_property_enum
*prop_enum
;
3810 struct drm_mode_property_enum __user
*enum_ptr
;
3811 struct drm_property_blob
*prop_blob
;
3812 uint32_t __user
*blob_id_ptr
;
3813 uint64_t __user
*values_ptr
;
3814 uint32_t __user
*blob_length_ptr
;
3816 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3819 drm_modeset_lock_all(dev
);
3820 property
= drm_property_find(dev
, out_resp
->prop_id
);
3826 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3827 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3828 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
3830 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3831 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
3835 value_count
= property
->num_values
;
3837 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
3838 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
3839 out_resp
->flags
= property
->flags
;
3841 if ((out_resp
->count_values
>= value_count
) && value_count
) {
3842 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
3843 for (i
= 0; i
< value_count
; i
++) {
3844 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
3850 out_resp
->count_values
= value_count
;
3852 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3853 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3854 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
3856 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3857 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3859 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
3864 if (copy_to_user(&enum_ptr
[copied
].name
,
3865 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3872 out_resp
->count_enum_blobs
= enum_count
;
3875 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3876 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
3878 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3879 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
3881 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
3882 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
3887 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
3895 out_resp
->count_enum_blobs
= blob_count
;
3898 drm_modeset_unlock_all(dev
);
3902 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
3905 struct drm_property_blob
*blob
;
3908 if (!length
|| !data
)
3911 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
3915 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
3921 blob
->length
= length
;
3923 memcpy(blob
->data
, data
, length
);
3925 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3929 static void drm_property_destroy_blob(struct drm_device
*dev
,
3930 struct drm_property_blob
*blob
)
3932 drm_mode_object_put(dev
, &blob
->base
);
3933 list_del(&blob
->head
);
3938 * drm_mode_getblob_ioctl - get the contents of a blob property value
3941 * @file_priv: DRM file info
3943 * This function retrieves the contents of a blob property. The value stored in
3944 * an object's blob property is just a normal modeset object id.
3946 * Called by the user via ioctl.
3949 * Zero on success, errno on failure.
3951 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3952 void *data
, struct drm_file
*file_priv
)
3954 struct drm_mode_get_blob
*out_resp
= data
;
3955 struct drm_property_blob
*blob
;
3957 void __user
*blob_ptr
;
3959 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3962 drm_modeset_lock_all(dev
);
3963 blob
= drm_property_blob_find(dev
, out_resp
->blob_id
);
3969 if (out_resp
->length
== blob
->length
) {
3970 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3971 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3976 out_resp
->length
= blob
->length
;
3979 drm_modeset_unlock_all(dev
);
3983 int drm_mode_connector_set_path_property(struct drm_connector
*connector
,
3986 struct drm_device
*dev
= connector
->dev
;
3988 size
= strlen(path
) + 1;
3990 connector
->path_blob_ptr
= drm_property_create_blob(connector
->dev
,
3992 if (!connector
->path_blob_ptr
)
3995 ret
= drm_object_property_set_value(&connector
->base
,
3996 dev
->mode_config
.path_property
,
3997 connector
->path_blob_ptr
->base
.id
);
4000 EXPORT_SYMBOL(drm_mode_connector_set_path_property
);
4003 * drm_mode_connector_update_edid_property - update the edid property of a connector
4004 * @connector: drm connector
4005 * @edid: new value of the edid property
4007 * This function creates a new blob modeset object and assigns its id to the
4008 * connector's edid property.
4011 * Zero on success, errno on failure.
4013 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
4016 struct drm_device
*dev
= connector
->dev
;
4019 /* ignore requests to set edid when overridden */
4020 if (connector
->override_edid
)
4023 if (connector
->edid_blob_ptr
)
4024 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
4026 /* Delete edid, when there is none. */
4028 connector
->edid_blob_ptr
= NULL
;
4029 ret
= drm_object_property_set_value(&connector
->base
, dev
->mode_config
.edid_property
, 0);
4033 size
= EDID_LENGTH
* (1 + edid
->extensions
);
4034 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
4036 if (!connector
->edid_blob_ptr
)
4039 ret
= drm_object_property_set_value(&connector
->base
,
4040 dev
->mode_config
.edid_property
,
4041 connector
->edid_blob_ptr
->base
.id
);
4045 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
4047 static bool drm_property_change_is_valid(struct drm_property
*property
,
4050 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
4053 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
4054 if (value
< property
->values
[0] || value
> property
->values
[1])
4057 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
4058 int64_t svalue
= U642I64(value
);
4059 if (svalue
< U642I64(property
->values
[0]) ||
4060 svalue
> U642I64(property
->values
[1]))
4063 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4065 uint64_t valid_mask
= 0;
4066 for (i
= 0; i
< property
->num_values
; i
++)
4067 valid_mask
|= (1ULL << property
->values
[i
]);
4068 return !(value
& ~valid_mask
);
4069 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
4070 /* Only the driver knows */
4072 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4073 struct drm_mode_object
*obj
;
4074 /* a zero value for an object property translates to null: */
4078 * NOTE: use _object_find() directly to bypass restriction on
4079 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4080 * object this could race against object finalization, so it
4081 * simply tells us that the object *was* valid. Which is good
4084 obj
= _object_find(property
->dev
, value
, property
->values
[0]);
4088 for (i
= 0; i
< property
->num_values
; i
++)
4089 if (property
->values
[i
] == value
)
4096 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4099 * @file_priv: DRM file info
4101 * This function sets the current value for a connectors's property. It also
4102 * calls into a driver's ->set_property callback to update the hardware state
4104 * Called by the user via ioctl.
4107 * Zero on success, errno on failure.
4109 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
4110 void *data
, struct drm_file
*file_priv
)
4112 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
4113 struct drm_mode_obj_set_property obj_set_prop
= {
4114 .value
= conn_set_prop
->value
,
4115 .prop_id
= conn_set_prop
->prop_id
,
4116 .obj_id
= conn_set_prop
->connector_id
,
4117 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
4120 /* It does all the locking and checking we need */
4121 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
4124 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
4125 struct drm_property
*property
,
4129 struct drm_connector
*connector
= obj_to_connector(obj
);
4131 /* Do DPMS ourselves */
4132 if (property
== connector
->dev
->mode_config
.dpms_property
) {
4133 if (connector
->funcs
->dpms
)
4134 (*connector
->funcs
->dpms
)(connector
, (int)value
);
4136 } else if (connector
->funcs
->set_property
)
4137 ret
= connector
->funcs
->set_property(connector
, property
, value
);
4139 /* store the property value if successful */
4141 drm_object_property_set_value(&connector
->base
, property
, value
);
4145 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
4146 struct drm_property
*property
,
4150 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
4152 if (crtc
->funcs
->set_property
)
4153 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
4155 drm_object_property_set_value(obj
, property
, value
);
4160 static int drm_mode_plane_set_obj_prop(struct drm_mode_object
*obj
,
4161 struct drm_property
*property
,
4165 struct drm_plane
*plane
= obj_to_plane(obj
);
4167 if (plane
->funcs
->set_property
)
4168 ret
= plane
->funcs
->set_property(plane
, property
, value
);
4170 drm_object_property_set_value(obj
, property
, value
);
4176 * drm_mode_getproperty_ioctl - get the current value of a object's property
4179 * @file_priv: DRM file info
4181 * This function retrieves the current value for an object's property. Compared
4182 * to the connector specific ioctl this one is extended to also work on crtc and
4185 * Called by the user via ioctl.
4188 * Zero on success, errno on failure.
4190 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
4191 struct drm_file
*file_priv
)
4193 struct drm_mode_obj_get_properties
*arg
= data
;
4194 struct drm_mode_object
*obj
;
4198 int props_count
= 0;
4199 uint32_t __user
*props_ptr
;
4200 uint64_t __user
*prop_values_ptr
;
4202 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4205 drm_modeset_lock_all(dev
);
4207 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4212 if (!obj
->properties
) {
4217 props_count
= obj
->properties
->count
;
4219 /* This ioctl is called twice, once to determine how much space is
4220 * needed, and the 2nd time to fill it. */
4221 if ((arg
->count_props
>= props_count
) && props_count
) {
4223 props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
4224 prop_values_ptr
= (uint64_t __user
*)(unsigned long)
4225 (arg
->prop_values_ptr
);
4226 for (i
= 0; i
< props_count
; i
++) {
4227 if (put_user(obj
->properties
->ids
[i
],
4228 props_ptr
+ copied
)) {
4232 if (put_user(obj
->properties
->values
[i
],
4233 prop_values_ptr
+ copied
)) {
4240 arg
->count_props
= props_count
;
4242 drm_modeset_unlock_all(dev
);
4247 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4250 * @file_priv: DRM file info
4252 * This function sets the current value for an object's property. It also calls
4253 * into a driver's ->set_property callback to update the hardware state.
4254 * Compared to the connector specific ioctl this one is extended to also work on
4255 * crtc and plane objects.
4257 * Called by the user via ioctl.
4260 * Zero on success, errno on failure.
4262 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
4263 struct drm_file
*file_priv
)
4265 struct drm_mode_obj_set_property
*arg
= data
;
4266 struct drm_mode_object
*arg_obj
;
4267 struct drm_mode_object
*prop_obj
;
4268 struct drm_property
*property
;
4272 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4275 drm_modeset_lock_all(dev
);
4277 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4282 if (!arg_obj
->properties
)
4285 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
4286 if (arg_obj
->properties
->ids
[i
] == arg
->prop_id
)
4289 if (i
== arg_obj
->properties
->count
)
4292 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
4293 DRM_MODE_OBJECT_PROPERTY
);
4298 property
= obj_to_property(prop_obj
);
4300 if (!drm_property_change_is_valid(property
, arg
->value
))
4303 switch (arg_obj
->type
) {
4304 case DRM_MODE_OBJECT_CONNECTOR
:
4305 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
4308 case DRM_MODE_OBJECT_CRTC
:
4309 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
4311 case DRM_MODE_OBJECT_PLANE
:
4312 ret
= drm_mode_plane_set_obj_prop(arg_obj
, property
, arg
->value
);
4317 drm_modeset_unlock_all(dev
);
4322 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4323 * @connector: connector to attach
4324 * @encoder: encoder to attach @connector to
4326 * This function links up a connector to an encoder. Note that the routing
4327 * restrictions between encoders and crtcs are exposed to userspace through the
4328 * possible_clones and possible_crtcs bitmasks.
4331 * Zero on success, errno on failure.
4333 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
4334 struct drm_encoder
*encoder
)
4338 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
4339 if (connector
->encoder_ids
[i
] == 0) {
4340 connector
->encoder_ids
[i
] = encoder
->base
.id
;
4346 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
4349 * drm_mode_crtc_set_gamma_size - set the gamma table size
4350 * @crtc: CRTC to set the gamma table size for
4351 * @gamma_size: size of the gamma table
4353 * Drivers which support gamma tables should set this to the supported gamma
4354 * table size when initializing the CRTC. Currently the drm core only supports a
4355 * fixed gamma table size.
4358 * Zero on success, errno on failure.
4360 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
4363 crtc
->gamma_size
= gamma_size
;
4365 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
4366 if (!crtc
->gamma_store
) {
4367 crtc
->gamma_size
= 0;
4373 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
4376 * drm_mode_gamma_set_ioctl - set the gamma table
4379 * @file_priv: DRM file info
4381 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4382 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4384 * Called by the user via ioctl.
4387 * Zero on success, errno on failure.
4389 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
4390 void *data
, struct drm_file
*file_priv
)
4392 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4393 struct drm_crtc
*crtc
;
4394 void *r_base
, *g_base
, *b_base
;
4398 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4401 drm_modeset_lock_all(dev
);
4402 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4408 if (crtc
->funcs
->gamma_set
== NULL
) {
4413 /* memcpy into gamma store */
4414 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4419 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4420 r_base
= crtc
->gamma_store
;
4421 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
4426 g_base
= r_base
+ size
;
4427 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
4432 b_base
= g_base
+ size
;
4433 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
4438 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
4441 drm_modeset_unlock_all(dev
);
4447 * drm_mode_gamma_get_ioctl - get the gamma table
4450 * @file_priv: DRM file info
4452 * Copy the current gamma table into the storage provided. This also provides
4453 * the gamma table size the driver expects, which can be used to size the
4454 * allocated storage.
4456 * Called by the user via ioctl.
4459 * Zero on success, errno on failure.
4461 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
4462 void *data
, struct drm_file
*file_priv
)
4464 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4465 struct drm_crtc
*crtc
;
4466 void *r_base
, *g_base
, *b_base
;
4470 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4473 drm_modeset_lock_all(dev
);
4474 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4480 /* memcpy into gamma store */
4481 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4486 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4487 r_base
= crtc
->gamma_store
;
4488 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
4493 g_base
= r_base
+ size
;
4494 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
4499 b_base
= g_base
+ size
;
4500 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
4505 drm_modeset_unlock_all(dev
);
4510 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4513 * @file_priv: DRM file info
4515 * This schedules an asynchronous update on a given CRTC, called page flip.
4516 * Optionally a drm event is generated to signal the completion of the event.
4517 * Generic drivers cannot assume that a pageflip with changed framebuffer
4518 * properties (including driver specific metadata like tiling layout) will work,
4519 * but some drivers support e.g. pixel format changes through the pageflip
4522 * Called by the user via ioctl.
4525 * Zero on success, errno on failure.
4527 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
4528 void *data
, struct drm_file
*file_priv
)
4530 struct drm_mode_crtc_page_flip
*page_flip
= data
;
4531 struct drm_crtc
*crtc
;
4532 struct drm_framebuffer
*fb
= NULL
, *old_fb
= NULL
;
4533 struct drm_pending_vblank_event
*e
= NULL
;
4534 unsigned long flags
;
4537 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
4538 page_flip
->reserved
!= 0)
4541 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
4544 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
4548 drm_modeset_lock(&crtc
->mutex
, NULL
);
4549 if (crtc
->primary
->fb
== NULL
) {
4550 /* The framebuffer is currently unbound, presumably
4551 * due to a hotplug event, that userspace has not
4558 if (crtc
->funcs
->page_flip
== NULL
)
4561 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
4567 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
4571 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
4572 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4577 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4579 spin_lock_irqsave(&dev
->event_lock
, flags
);
4580 if (file_priv
->event_space
< sizeof e
->event
) {
4581 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4584 file_priv
->event_space
-= sizeof e
->event
;
4585 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4587 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
4589 spin_lock_irqsave(&dev
->event_lock
, flags
);
4590 file_priv
->event_space
+= sizeof e
->event
;
4591 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4595 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
4596 e
->event
.base
.length
= sizeof e
->event
;
4597 e
->event
.user_data
= page_flip
->user_data
;
4598 e
->base
.event
= &e
->event
.base
;
4599 e
->base
.file_priv
= file_priv
;
4601 (void (*) (struct drm_pending_event
*)) kfree
;
4604 old_fb
= crtc
->primary
->fb
;
4605 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
4607 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4608 spin_lock_irqsave(&dev
->event_lock
, flags
);
4609 file_priv
->event_space
+= sizeof e
->event
;
4610 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4613 /* Keep the old fb, don't unref it. */
4617 * Warn if the driver hasn't properly updated the crtc->fb
4618 * field to reflect that the new framebuffer is now used.
4619 * Failing to do so will screw with the reference counting
4622 WARN_ON(crtc
->primary
->fb
!= fb
);
4623 /* Unref only the old framebuffer. */
4629 drm_framebuffer_unreference(fb
);
4631 drm_framebuffer_unreference(old_fb
);
4632 drm_modeset_unlock(&crtc
->mutex
);
4638 * drm_mode_config_reset - call ->reset callbacks
4641 * This functions calls all the crtc's, encoder's and connector's ->reset
4642 * callback. Drivers can use this in e.g. their driver load or resume code to
4643 * reset hardware and software state.
4645 void drm_mode_config_reset(struct drm_device
*dev
)
4647 struct drm_crtc
*crtc
;
4648 struct drm_encoder
*encoder
;
4649 struct drm_connector
*connector
;
4651 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
4652 if (crtc
->funcs
->reset
)
4653 crtc
->funcs
->reset(crtc
);
4655 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
4656 if (encoder
->funcs
->reset
)
4657 encoder
->funcs
->reset(encoder
);
4659 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
4660 connector
->status
= connector_status_unknown
;
4662 if (connector
->funcs
->reset
)
4663 connector
->funcs
->reset(connector
);
4666 EXPORT_SYMBOL(drm_mode_config_reset
);
4669 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4672 * @file_priv: DRM file info
4674 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4675 * TTM or something else entirely) and returns the resulting buffer handle. This
4676 * handle can then be wrapped up into a framebuffer modeset object.
4678 * Note that userspace is not allowed to use such objects for render
4679 * acceleration - drivers must create their own private ioctls for such a use
4682 * Called by the user via ioctl.
4685 * Zero on success, errno on failure.
4687 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
4688 void *data
, struct drm_file
*file_priv
)
4690 struct drm_mode_create_dumb
*args
= data
;
4691 u32 cpp
, stride
, size
;
4693 if (!dev
->driver
->dumb_create
)
4695 if (!args
->width
|| !args
->height
|| !args
->bpp
)
4698 /* overflow checks for 32bit size calculations */
4699 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
4700 if (cpp
> 0xffffffffU
/ args
->width
)
4702 stride
= cpp
* args
->width
;
4703 if (args
->height
> 0xffffffffU
/ stride
)
4706 /* test for wrap-around */
4707 size
= args
->height
* stride
;
4708 if (PAGE_ALIGN(size
) == 0)
4711 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
4715 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4718 * @file_priv: DRM file info
4720 * Allocate an offset in the drm device node's address space to be able to
4721 * memory map a dumb buffer.
4723 * Called by the user via ioctl.
4726 * Zero on success, errno on failure.
4728 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
4729 void *data
, struct drm_file
*file_priv
)
4731 struct drm_mode_map_dumb
*args
= data
;
4733 /* call driver ioctl to get mmap offset */
4734 if (!dev
->driver
->dumb_map_offset
)
4737 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
4741 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4744 * @file_priv: DRM file info
4746 * This destroys the userspace handle for the given dumb backing storage buffer.
4747 * Since buffer objects must be reference counted in the kernel a buffer object
4748 * won't be immediately freed if a framebuffer modeset object still uses it.
4750 * Called by the user via ioctl.
4753 * Zero on success, errno on failure.
4755 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
4756 void *data
, struct drm_file
*file_priv
)
4758 struct drm_mode_destroy_dumb
*args
= data
;
4760 if (!dev
->driver
->dumb_destroy
)
4763 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
4767 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4768 * @format: pixel format (DRM_FORMAT_*)
4769 * @depth: storage for the depth value
4770 * @bpp: storage for the bpp value
4772 * This only supports RGB formats here for compat with code that doesn't use
4773 * pixel formats directly yet.
4775 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
4780 case DRM_FORMAT_RGB332
:
4781 case DRM_FORMAT_BGR233
:
4785 case DRM_FORMAT_XRGB1555
:
4786 case DRM_FORMAT_XBGR1555
:
4787 case DRM_FORMAT_RGBX5551
:
4788 case DRM_FORMAT_BGRX5551
:
4789 case DRM_FORMAT_ARGB1555
:
4790 case DRM_FORMAT_ABGR1555
:
4791 case DRM_FORMAT_RGBA5551
:
4792 case DRM_FORMAT_BGRA5551
:
4796 case DRM_FORMAT_RGB565
:
4797 case DRM_FORMAT_BGR565
:
4801 case DRM_FORMAT_RGB888
:
4802 case DRM_FORMAT_BGR888
:
4806 case DRM_FORMAT_XRGB8888
:
4807 case DRM_FORMAT_XBGR8888
:
4808 case DRM_FORMAT_RGBX8888
:
4809 case DRM_FORMAT_BGRX8888
:
4813 case DRM_FORMAT_XRGB2101010
:
4814 case DRM_FORMAT_XBGR2101010
:
4815 case DRM_FORMAT_RGBX1010102
:
4816 case DRM_FORMAT_BGRX1010102
:
4817 case DRM_FORMAT_ARGB2101010
:
4818 case DRM_FORMAT_ABGR2101010
:
4819 case DRM_FORMAT_RGBA1010102
:
4820 case DRM_FORMAT_BGRA1010102
:
4824 case DRM_FORMAT_ARGB8888
:
4825 case DRM_FORMAT_ABGR8888
:
4826 case DRM_FORMAT_RGBA8888
:
4827 case DRM_FORMAT_BGRA8888
:
4832 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4833 drm_get_format_name(format
));
4839 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
4842 * drm_format_num_planes - get the number of planes for format
4843 * @format: pixel format (DRM_FORMAT_*)
4846 * The number of planes used by the specified pixel format.
4848 int drm_format_num_planes(uint32_t format
)
4851 case DRM_FORMAT_YUV410
:
4852 case DRM_FORMAT_YVU410
:
4853 case DRM_FORMAT_YUV411
:
4854 case DRM_FORMAT_YVU411
:
4855 case DRM_FORMAT_YUV420
:
4856 case DRM_FORMAT_YVU420
:
4857 case DRM_FORMAT_YUV422
:
4858 case DRM_FORMAT_YVU422
:
4859 case DRM_FORMAT_YUV444
:
4860 case DRM_FORMAT_YVU444
:
4862 case DRM_FORMAT_NV12
:
4863 case DRM_FORMAT_NV21
:
4864 case DRM_FORMAT_NV16
:
4865 case DRM_FORMAT_NV61
:
4866 case DRM_FORMAT_NV24
:
4867 case DRM_FORMAT_NV42
:
4873 EXPORT_SYMBOL(drm_format_num_planes
);
4876 * drm_format_plane_cpp - determine the bytes per pixel value
4877 * @format: pixel format (DRM_FORMAT_*)
4878 * @plane: plane index
4881 * The bytes per pixel value for the specified plane.
4883 int drm_format_plane_cpp(uint32_t format
, int plane
)
4888 if (plane
>= drm_format_num_planes(format
))
4892 case DRM_FORMAT_YUYV
:
4893 case DRM_FORMAT_YVYU
:
4894 case DRM_FORMAT_UYVY
:
4895 case DRM_FORMAT_VYUY
:
4897 case DRM_FORMAT_NV12
:
4898 case DRM_FORMAT_NV21
:
4899 case DRM_FORMAT_NV16
:
4900 case DRM_FORMAT_NV61
:
4901 case DRM_FORMAT_NV24
:
4902 case DRM_FORMAT_NV42
:
4903 return plane
? 2 : 1;
4904 case DRM_FORMAT_YUV410
:
4905 case DRM_FORMAT_YVU410
:
4906 case DRM_FORMAT_YUV411
:
4907 case DRM_FORMAT_YVU411
:
4908 case DRM_FORMAT_YUV420
:
4909 case DRM_FORMAT_YVU420
:
4910 case DRM_FORMAT_YUV422
:
4911 case DRM_FORMAT_YVU422
:
4912 case DRM_FORMAT_YUV444
:
4913 case DRM_FORMAT_YVU444
:
4916 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
4920 EXPORT_SYMBOL(drm_format_plane_cpp
);
4923 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4924 * @format: pixel format (DRM_FORMAT_*)
4927 * The horizontal chroma subsampling factor for the
4928 * specified pixel format.
4930 int drm_format_horz_chroma_subsampling(uint32_t format
)
4933 case DRM_FORMAT_YUV411
:
4934 case DRM_FORMAT_YVU411
:
4935 case DRM_FORMAT_YUV410
:
4936 case DRM_FORMAT_YVU410
:
4938 case DRM_FORMAT_YUYV
:
4939 case DRM_FORMAT_YVYU
:
4940 case DRM_FORMAT_UYVY
:
4941 case DRM_FORMAT_VYUY
:
4942 case DRM_FORMAT_NV12
:
4943 case DRM_FORMAT_NV21
:
4944 case DRM_FORMAT_NV16
:
4945 case DRM_FORMAT_NV61
:
4946 case DRM_FORMAT_YUV422
:
4947 case DRM_FORMAT_YVU422
:
4948 case DRM_FORMAT_YUV420
:
4949 case DRM_FORMAT_YVU420
:
4955 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
4958 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4959 * @format: pixel format (DRM_FORMAT_*)
4962 * The vertical chroma subsampling factor for the
4963 * specified pixel format.
4965 int drm_format_vert_chroma_subsampling(uint32_t format
)
4968 case DRM_FORMAT_YUV410
:
4969 case DRM_FORMAT_YVU410
:
4971 case DRM_FORMAT_YUV420
:
4972 case DRM_FORMAT_YVU420
:
4973 case DRM_FORMAT_NV12
:
4974 case DRM_FORMAT_NV21
:
4980 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
4983 * drm_rotation_simplify() - Try to simplify the rotation
4984 * @rotation: Rotation to be simplified
4985 * @supported_rotations: Supported rotations
4987 * Attempt to simplify the rotation to a form that is supported.
4988 * Eg. if the hardware supports everything except DRM_REFLECT_X
4989 * one could call this function like this:
4991 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
4992 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
4993 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
4995 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
4996 * transforms the hardware supports, this function may not
4997 * be able to produce a supported transform, so the caller should
4998 * check the result afterwards.
5000 unsigned int drm_rotation_simplify(unsigned int rotation
,
5001 unsigned int supported_rotations
)
5003 if (rotation
& ~supported_rotations
) {
5004 rotation
^= BIT(DRM_REFLECT_X
) | BIT(DRM_REFLECT_Y
);
5005 rotation
= (rotation
& ~0xf) | BIT((ffs(rotation
& 0xf) + 1) % 4);
5010 EXPORT_SYMBOL(drm_rotation_simplify
);
5013 * drm_mode_config_init - initialize DRM mode_configuration structure
5016 * Initialize @dev's mode_config structure, used for tracking the graphics
5017 * configuration of @dev.
5019 * Since this initializes the modeset locks, no locking is possible. Which is no
5020 * problem, since this should happen single threaded at init time. It is the
5021 * driver's problem to ensure this guarantee.
5024 void drm_mode_config_init(struct drm_device
*dev
)
5026 mutex_init(&dev
->mode_config
.mutex
);
5027 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
5028 mutex_init(&dev
->mode_config
.idr_mutex
);
5029 mutex_init(&dev
->mode_config
.fb_lock
);
5030 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
5031 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
5032 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
5033 INIT_LIST_HEAD(&dev
->mode_config
.bridge_list
);
5034 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
5035 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
5036 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
5037 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
5038 idr_init(&dev
->mode_config
.crtc_idr
);
5040 drm_modeset_lock_all(dev
);
5041 drm_mode_create_standard_connector_properties(dev
);
5042 drm_mode_create_standard_plane_properties(dev
);
5043 drm_modeset_unlock_all(dev
);
5045 /* Just to be sure */
5046 dev
->mode_config
.num_fb
= 0;
5047 dev
->mode_config
.num_connector
= 0;
5048 dev
->mode_config
.num_crtc
= 0;
5049 dev
->mode_config
.num_encoder
= 0;
5050 dev
->mode_config
.num_overlay_plane
= 0;
5051 dev
->mode_config
.num_total_plane
= 0;
5053 EXPORT_SYMBOL(drm_mode_config_init
);
5056 * drm_mode_config_cleanup - free up DRM mode_config info
5059 * Free up all the connectors and CRTCs associated with this DRM device, then
5060 * free up the framebuffers and associated buffer objects.
5062 * Note that since this /should/ happen single-threaded at driver/device
5063 * teardown time, no locking is required. It's the driver's job to ensure that
5064 * this guarantee actually holds true.
5066 * FIXME: cleanup any dangling user buffer objects too
5068 void drm_mode_config_cleanup(struct drm_device
*dev
)
5070 struct drm_connector
*connector
, *ot
;
5071 struct drm_crtc
*crtc
, *ct
;
5072 struct drm_encoder
*encoder
, *enct
;
5073 struct drm_bridge
*bridge
, *brt
;
5074 struct drm_framebuffer
*fb
, *fbt
;
5075 struct drm_property
*property
, *pt
;
5076 struct drm_property_blob
*blob
, *bt
;
5077 struct drm_plane
*plane
, *plt
;
5079 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
5081 encoder
->funcs
->destroy(encoder
);
5084 list_for_each_entry_safe(bridge
, brt
,
5085 &dev
->mode_config
.bridge_list
, head
) {
5086 bridge
->funcs
->destroy(bridge
);
5089 list_for_each_entry_safe(connector
, ot
,
5090 &dev
->mode_config
.connector_list
, head
) {
5091 connector
->funcs
->destroy(connector
);
5094 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
5096 drm_property_destroy(dev
, property
);
5099 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
5101 drm_property_destroy_blob(dev
, blob
);
5105 * Single-threaded teardown context, so it's not required to grab the
5106 * fb_lock to protect against concurrent fb_list access. Contrary, it
5107 * would actually deadlock with the drm_framebuffer_cleanup function.
5109 * Also, if there are any framebuffers left, that's a driver leak now,
5110 * so politely WARN about this.
5112 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
5113 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
5114 drm_framebuffer_remove(fb
);
5117 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
5119 plane
->funcs
->destroy(plane
);
5122 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
5123 crtc
->funcs
->destroy(crtc
);
5126 idr_destroy(&dev
->mode_config
.crtc_idr
);
5127 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
5129 EXPORT_SYMBOL(drm_mode_config_cleanup
);
5131 struct drm_property
*drm_mode_create_rotation_property(struct drm_device
*dev
,
5132 unsigned int supported_rotations
)
5134 static const struct drm_prop_enum_list props
[] = {
5135 { DRM_ROTATE_0
, "rotate-0" },
5136 { DRM_ROTATE_90
, "rotate-90" },
5137 { DRM_ROTATE_180
, "rotate-180" },
5138 { DRM_ROTATE_270
, "rotate-270" },
5139 { DRM_REFLECT_X
, "reflect-x" },
5140 { DRM_REFLECT_Y
, "reflect-y" },
5143 return drm_property_create_bitmask(dev
, 0, "rotation",
5144 props
, ARRAY_SIZE(props
),
5145 supported_rotations
);
5147 EXPORT_SYMBOL(drm_mode_create_rotation_property
);