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>
42 * drm_modeset_lock_all - take all modeset locks
45 * This function takes all modeset locks, suitable where a more fine-grained
46 * scheme isn't (yet) implemented.
48 void drm_modeset_lock_all(struct drm_device
*dev
)
50 struct drm_crtc
*crtc
;
52 mutex_lock(&dev
->mode_config
.mutex
);
54 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
55 mutex_lock_nest_lock(&crtc
->mutex
, &dev
->mode_config
.mutex
);
57 EXPORT_SYMBOL(drm_modeset_lock_all
);
60 * drm_modeset_unlock_all - drop all modeset locks
63 void drm_modeset_unlock_all(struct drm_device
*dev
)
65 struct drm_crtc
*crtc
;
67 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
68 mutex_unlock(&crtc
->mutex
);
70 mutex_unlock(&dev
->mode_config
.mutex
);
72 EXPORT_SYMBOL(drm_modeset_unlock_all
);
75 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
78 void drm_warn_on_modeset_not_all_locked(struct drm_device
*dev
)
80 struct drm_crtc
*crtc
;
82 /* Locking is currently fubar in the panic handler. */
86 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
87 WARN_ON(!mutex_is_locked(&crtc
->mutex
));
89 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
91 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked
);
93 /* Avoid boilerplate. I'm tired of typing. */
94 #define DRM_ENUM_NAME_FN(fnname, list) \
95 const char *fnname(int val) \
98 for (i = 0; i < ARRAY_SIZE(list); i++) { \
99 if (list[i].type == val) \
100 return list[i].name; \
102 return "(unknown)"; \
108 static const struct drm_prop_enum_list drm_dpms_enum_list
[] =
109 { { DRM_MODE_DPMS_ON
, "On" },
110 { DRM_MODE_DPMS_STANDBY
, "Standby" },
111 { DRM_MODE_DPMS_SUSPEND
, "Suspend" },
112 { DRM_MODE_DPMS_OFF
, "Off" }
115 DRM_ENUM_NAME_FN(drm_get_dpms_name
, drm_dpms_enum_list
)
118 * Optional properties
120 static const struct drm_prop_enum_list drm_scaling_mode_enum_list
[] =
122 { DRM_MODE_SCALE_NONE
, "None" },
123 { DRM_MODE_SCALE_FULLSCREEN
, "Full" },
124 { DRM_MODE_SCALE_CENTER
, "Center" },
125 { DRM_MODE_SCALE_ASPECT
, "Full aspect" },
129 * Non-global properties, but "required" for certain connectors.
131 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list
[] =
133 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
134 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
135 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
138 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name
, drm_dvi_i_select_enum_list
)
140 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list
[] =
142 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
143 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
144 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
147 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name
,
148 drm_dvi_i_subconnector_enum_list
)
150 static const struct drm_prop_enum_list drm_tv_select_enum_list
[] =
152 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
153 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
154 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
155 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
156 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
159 DRM_ENUM_NAME_FN(drm_get_tv_select_name
, drm_tv_select_enum_list
)
161 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list
[] =
163 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
164 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
165 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
166 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
167 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
170 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name
,
171 drm_tv_subconnector_enum_list
)
173 static const struct drm_prop_enum_list drm_dirty_info_enum_list
[] = {
174 { DRM_MODE_DIRTY_OFF
, "Off" },
175 { DRM_MODE_DIRTY_ON
, "On" },
176 { DRM_MODE_DIRTY_ANNOTATE
, "Annotate" },
179 struct drm_conn_prop_enum_list
{
186 * Connector and encoder types.
188 static struct drm_conn_prop_enum_list drm_connector_enum_list
[] =
189 { { DRM_MODE_CONNECTOR_Unknown
, "Unknown" },
190 { DRM_MODE_CONNECTOR_VGA
, "VGA" },
191 { DRM_MODE_CONNECTOR_DVII
, "DVI-I" },
192 { DRM_MODE_CONNECTOR_DVID
, "DVI-D" },
193 { DRM_MODE_CONNECTOR_DVIA
, "DVI-A" },
194 { DRM_MODE_CONNECTOR_Composite
, "Composite" },
195 { DRM_MODE_CONNECTOR_SVIDEO
, "SVIDEO" },
196 { DRM_MODE_CONNECTOR_LVDS
, "LVDS" },
197 { DRM_MODE_CONNECTOR_Component
, "Component" },
198 { DRM_MODE_CONNECTOR_9PinDIN
, "DIN" },
199 { DRM_MODE_CONNECTOR_DisplayPort
, "DP" },
200 { DRM_MODE_CONNECTOR_HDMIA
, "HDMI-A" },
201 { DRM_MODE_CONNECTOR_HDMIB
, "HDMI-B" },
202 { DRM_MODE_CONNECTOR_TV
, "TV" },
203 { DRM_MODE_CONNECTOR_eDP
, "eDP" },
204 { DRM_MODE_CONNECTOR_VIRTUAL
, "Virtual" },
205 { DRM_MODE_CONNECTOR_DSI
, "DSI" },
208 static const struct drm_prop_enum_list drm_encoder_enum_list
[] =
209 { { DRM_MODE_ENCODER_NONE
, "None" },
210 { DRM_MODE_ENCODER_DAC
, "DAC" },
211 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
212 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
213 { DRM_MODE_ENCODER_TVDAC
, "TV" },
214 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
215 { DRM_MODE_ENCODER_DSI
, "DSI" },
218 static const struct drm_prop_enum_list drm_subpixel_enum_list
[] =
220 { SubPixelUnknown
, "Unknown" },
221 { SubPixelHorizontalRGB
, "Horizontal RGB" },
222 { SubPixelHorizontalBGR
, "Horizontal BGR" },
223 { SubPixelVerticalRGB
, "Vertical RGB" },
224 { SubPixelVerticalBGR
, "Vertical BGR" },
225 { SubPixelNone
, "None" },
228 void drm_connector_ida_init(void)
232 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
233 ida_init(&drm_connector_enum_list
[i
].ida
);
236 void drm_connector_ida_destroy(void)
240 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
241 ida_destroy(&drm_connector_enum_list
[i
].ida
);
244 const char *drm_get_encoder_name(const struct drm_encoder
*encoder
)
248 snprintf(buf
, 32, "%s-%d",
249 drm_encoder_enum_list
[encoder
->encoder_type
].name
,
253 EXPORT_SYMBOL(drm_get_encoder_name
);
255 const char *drm_get_connector_name(const struct drm_connector
*connector
)
259 snprintf(buf
, 32, "%s-%d",
260 drm_connector_enum_list
[connector
->connector_type
].name
,
261 connector
->connector_type_id
);
264 EXPORT_SYMBOL(drm_get_connector_name
);
266 const char *drm_get_connector_status_name(enum drm_connector_status status
)
268 if (status
== connector_status_connected
)
270 else if (status
== connector_status_disconnected
)
271 return "disconnected";
275 EXPORT_SYMBOL(drm_get_connector_status_name
);
278 * drm_get_subpixel_order_name - return a string for a given subpixel enum
279 * @order: enum of subpixel_order
281 * Note you could abuse this and return something out of bounds, but that
282 * would be a caller error. No unscrubbed user data should make it here.
284 const char *drm_get_subpixel_order_name(enum subpixel_order order
)
286 return drm_subpixel_enum_list
[order
].name
;
288 EXPORT_SYMBOL(drm_get_subpixel_order_name
);
290 static char printable_char(int c
)
292 return isascii(c
) && isprint(c
) ? c
: '?';
295 const char *drm_get_format_name(uint32_t format
)
299 snprintf(buf
, sizeof(buf
),
300 "%c%c%c%c %s-endian (0x%08x)",
301 printable_char(format
& 0xff),
302 printable_char((format
>> 8) & 0xff),
303 printable_char((format
>> 16) & 0xff),
304 printable_char((format
>> 24) & 0x7f),
305 format
& DRM_FORMAT_BIG_ENDIAN
? "big" : "little",
310 EXPORT_SYMBOL(drm_get_format_name
);
313 * drm_mode_object_get - allocate a new modeset identifier
315 * @obj: object pointer, used to generate unique ID
316 * @obj_type: object type
318 * Create a unique identifier based on @ptr in @dev's identifier space. Used
319 * for tracking modes, CRTCs and connectors.
322 * New unique (relative to other objects in @dev) integer identifier for the
325 static int drm_mode_object_get(struct drm_device
*dev
,
326 struct drm_mode_object
*obj
, uint32_t obj_type
)
330 mutex_lock(&dev
->mode_config
.idr_mutex
);
331 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, obj
, 1, 0, GFP_KERNEL
);
334 * Set up the object linking under the protection of the idr
335 * lock so that other users can't see inconsistent state.
338 obj
->type
= obj_type
;
340 mutex_unlock(&dev
->mode_config
.idr_mutex
);
342 return ret
< 0 ? ret
: 0;
346 * drm_mode_object_put - free a modeset identifer
348 * @object: object to free
350 * Free @id from @dev's unique identifier pool.
352 static void drm_mode_object_put(struct drm_device
*dev
,
353 struct drm_mode_object
*object
)
355 mutex_lock(&dev
->mode_config
.idr_mutex
);
356 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
357 mutex_unlock(&dev
->mode_config
.idr_mutex
);
361 * drm_mode_object_find - look up a drm object with static lifetime
363 * @id: id of the mode object
364 * @type: type of the mode object
366 * Note that framebuffers cannot be looked up with this functions - since those
367 * are reference counted, they need special treatment.
369 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
370 uint32_t id
, uint32_t type
)
372 struct drm_mode_object
*obj
= NULL
;
374 /* Framebuffers are reference counted and need their own lookup
376 WARN_ON(type
== DRM_MODE_OBJECT_FB
);
378 mutex_lock(&dev
->mode_config
.idr_mutex
);
379 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
380 if (!obj
|| (obj
->type
!= type
) || (obj
->id
!= id
))
382 mutex_unlock(&dev
->mode_config
.idr_mutex
);
386 EXPORT_SYMBOL(drm_mode_object_find
);
389 * drm_framebuffer_init - initialize a framebuffer
391 * @fb: framebuffer to be initialized
392 * @funcs: ... with these functions
394 * Allocates an ID for the framebuffer's parent mode object, sets its mode
395 * functions & device file and adds it to the master fd list.
398 * This functions publishes the fb and makes it available for concurrent access
399 * by other users. Which means by this point the fb _must_ be fully set up -
400 * since all the fb attributes are invariant over its lifetime, no further
401 * locking but only correct reference counting is required.
404 * Zero on success, error code on failure.
406 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
407 const struct drm_framebuffer_funcs
*funcs
)
411 mutex_lock(&dev
->mode_config
.fb_lock
);
412 kref_init(&fb
->refcount
);
413 INIT_LIST_HEAD(&fb
->filp_head
);
417 ret
= drm_mode_object_get(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
);
421 /* Grab the idr reference. */
422 drm_framebuffer_reference(fb
);
424 dev
->mode_config
.num_fb
++;
425 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
427 mutex_unlock(&dev
->mode_config
.fb_lock
);
431 EXPORT_SYMBOL(drm_framebuffer_init
);
433 static void drm_framebuffer_free(struct kref
*kref
)
435 struct drm_framebuffer
*fb
=
436 container_of(kref
, struct drm_framebuffer
, refcount
);
437 fb
->funcs
->destroy(fb
);
440 static struct drm_framebuffer
*__drm_framebuffer_lookup(struct drm_device
*dev
,
443 struct drm_mode_object
*obj
= NULL
;
444 struct drm_framebuffer
*fb
;
446 mutex_lock(&dev
->mode_config
.idr_mutex
);
447 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
448 if (!obj
|| (obj
->type
!= DRM_MODE_OBJECT_FB
) || (obj
->id
!= id
))
452 mutex_unlock(&dev
->mode_config
.idr_mutex
);
458 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
460 * @id: id of the fb object
462 * If successful, this grabs an additional reference to the framebuffer -
463 * callers need to make sure to eventually unreference the returned framebuffer
466 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
469 struct drm_framebuffer
*fb
;
471 mutex_lock(&dev
->mode_config
.fb_lock
);
472 fb
= __drm_framebuffer_lookup(dev
, id
);
474 drm_framebuffer_reference(fb
);
475 mutex_unlock(&dev
->mode_config
.fb_lock
);
479 EXPORT_SYMBOL(drm_framebuffer_lookup
);
482 * drm_framebuffer_unreference - unref a framebuffer
483 * @fb: framebuffer to unref
485 * This functions decrements the fb's refcount and frees it if it drops to zero.
487 void drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
489 DRM_DEBUG("FB ID: %d\n", fb
->base
.id
);
490 kref_put(&fb
->refcount
, drm_framebuffer_free
);
492 EXPORT_SYMBOL(drm_framebuffer_unreference
);
495 * drm_framebuffer_reference - incr the fb refcnt
498 void drm_framebuffer_reference(struct drm_framebuffer
*fb
)
500 DRM_DEBUG("FB ID: %d\n", fb
->base
.id
);
501 kref_get(&fb
->refcount
);
503 EXPORT_SYMBOL(drm_framebuffer_reference
);
505 static void drm_framebuffer_free_bug(struct kref
*kref
)
510 static void __drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
512 DRM_DEBUG("FB ID: %d\n", fb
->base
.id
);
513 kref_put(&fb
->refcount
, drm_framebuffer_free_bug
);
516 /* dev->mode_config.fb_lock must be held! */
517 static void __drm_framebuffer_unregister(struct drm_device
*dev
,
518 struct drm_framebuffer
*fb
)
520 mutex_lock(&dev
->mode_config
.idr_mutex
);
521 idr_remove(&dev
->mode_config
.crtc_idr
, fb
->base
.id
);
522 mutex_unlock(&dev
->mode_config
.idr_mutex
);
526 __drm_framebuffer_unreference(fb
);
530 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
531 * @fb: fb to unregister
533 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
534 * those used for fbdev. Note that the caller must hold a reference of it's own,
535 * i.e. the object may not be destroyed through this call (since it'll lead to a
536 * locking inversion).
538 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
540 struct drm_device
*dev
= fb
->dev
;
542 mutex_lock(&dev
->mode_config
.fb_lock
);
543 /* Mark fb as reaped and drop idr ref. */
544 __drm_framebuffer_unregister(dev
, fb
);
545 mutex_unlock(&dev
->mode_config
.fb_lock
);
547 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
550 * drm_framebuffer_cleanup - remove a framebuffer object
551 * @fb: framebuffer to remove
553 * Cleanup references to a user-created framebuffer. This function is intended
554 * to be used from the drivers ->destroy callback.
556 * Note that this function does not remove the fb from active usuage - if it is
557 * still used anywhere, hilarity can ensue since userspace could call getfb on
558 * the id and get back -EINVAL. Obviously no concern at driver unload time.
560 * Also, the framebuffer will not be removed from the lookup idr - for
561 * user-created framebuffers this will happen in in the rmfb ioctl. For
562 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
563 * drm_framebuffer_unregister_private.
565 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
567 struct drm_device
*dev
= fb
->dev
;
569 mutex_lock(&dev
->mode_config
.fb_lock
);
571 dev
->mode_config
.num_fb
--;
572 mutex_unlock(&dev
->mode_config
.fb_lock
);
574 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
577 * drm_framebuffer_remove - remove and unreference a framebuffer object
578 * @fb: framebuffer to remove
580 * Scans all the CRTCs and planes in @dev's mode_config. If they're
581 * using @fb, removes it, setting it to NULL. Then drops the reference to the
582 * passed-in framebuffer. Might take the modeset locks.
584 * Note that this function optimizes the cleanup away if the caller holds the
585 * last reference to the framebuffer. It is also guaranteed to not take the
586 * modeset locks in this case.
588 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
590 struct drm_device
*dev
= fb
->dev
;
591 struct drm_crtc
*crtc
;
592 struct drm_plane
*plane
;
593 struct drm_mode_set set
;
596 WARN_ON(!list_empty(&fb
->filp_head
));
599 * drm ABI mandates that we remove any deleted framebuffers from active
600 * useage. But since most sane clients only remove framebuffers they no
601 * longer need, try to optimize this away.
603 * Since we're holding a reference ourselves, observing a refcount of 1
604 * means that we're the last holder and can skip it. Also, the refcount
605 * can never increase from 1 again, so we don't need any barriers or
608 * Note that userspace could try to race with use and instate a new
609 * usage _after_ we've cleared all current ones. End result will be an
610 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
613 if (atomic_read(&fb
->refcount
.refcount
) > 1) {
614 drm_modeset_lock_all(dev
);
615 /* remove from any CRTC */
616 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
617 if (crtc
->fb
== fb
) {
618 /* should turn off the crtc */
619 memset(&set
, 0, sizeof(struct drm_mode_set
));
622 ret
= drm_mode_set_config_internal(&set
);
624 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
628 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
630 drm_plane_force_disable(plane
);
632 drm_modeset_unlock_all(dev
);
635 drm_framebuffer_unreference(fb
);
637 EXPORT_SYMBOL(drm_framebuffer_remove
);
640 * drm_crtc_init - Initialise a new CRTC object
642 * @crtc: CRTC object to init
643 * @funcs: callbacks for the new CRTC
645 * Inits a new object created as base part of a driver crtc object.
648 * Zero on success, error code on failure.
650 int drm_crtc_init(struct drm_device
*dev
, struct drm_crtc
*crtc
,
651 const struct drm_crtc_funcs
*funcs
)
657 crtc
->invert_dimensions
= false;
659 drm_modeset_lock_all(dev
);
660 mutex_init(&crtc
->mutex
);
661 mutex_lock_nest_lock(&crtc
->mutex
, &dev
->mode_config
.mutex
);
663 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
667 crtc
->base
.properties
= &crtc
->properties
;
669 list_add_tail(&crtc
->head
, &dev
->mode_config
.crtc_list
);
670 dev
->mode_config
.num_crtc
++;
673 drm_modeset_unlock_all(dev
);
677 EXPORT_SYMBOL(drm_crtc_init
);
680 * drm_crtc_cleanup - Clean up the core crtc usage
681 * @crtc: CRTC to cleanup
683 * This function cleans up @crtc and removes it from the DRM mode setting
684 * core. Note that the function does *not* free the crtc structure itself,
685 * this is the responsibility of the caller.
687 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
689 struct drm_device
*dev
= crtc
->dev
;
691 kfree(crtc
->gamma_store
);
692 crtc
->gamma_store
= NULL
;
694 drm_mode_object_put(dev
, &crtc
->base
);
695 list_del(&crtc
->head
);
696 dev
->mode_config
.num_crtc
--;
698 EXPORT_SYMBOL(drm_crtc_cleanup
);
701 * drm_crtc_index - find the index of a registered CRTC
702 * @crtc: CRTC to find index for
704 * Given a registered CRTC, return the index of that CRTC within a DRM
705 * device's list of CRTCs.
707 unsigned int drm_crtc_index(struct drm_crtc
*crtc
)
709 unsigned int index
= 0;
710 struct drm_crtc
*tmp
;
712 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
721 EXPORT_SYMBOL(drm_crtc_index
);
724 * drm_mode_probed_add - add a mode to a connector's probed mode list
725 * @connector: connector the new mode
728 * Add @mode to @connector's mode list for later use.
730 void drm_mode_probed_add(struct drm_connector
*connector
,
731 struct drm_display_mode
*mode
)
733 list_add_tail(&mode
->head
, &connector
->probed_modes
);
735 EXPORT_SYMBOL(drm_mode_probed_add
);
738 * drm_mode_remove - remove and free a mode
739 * @connector: connector list to modify
740 * @mode: mode to remove
742 * Remove @mode from @connector's mode list, then free it.
744 static void drm_mode_remove(struct drm_connector
*connector
,
745 struct drm_display_mode
*mode
)
747 list_del(&mode
->head
);
748 drm_mode_destroy(connector
->dev
, mode
);
752 * drm_connector_init - Init a preallocated connector
754 * @connector: the connector to init
755 * @funcs: callbacks for this connector
756 * @connector_type: user visible type of the connector
758 * Initialises a preallocated connector. Connectors should be
759 * subclassed as part of driver connector objects.
762 * Zero on success, error code on failure.
764 int drm_connector_init(struct drm_device
*dev
,
765 struct drm_connector
*connector
,
766 const struct drm_connector_funcs
*funcs
,
770 struct ida
*connector_ida
=
771 &drm_connector_enum_list
[connector_type
].ida
;
773 drm_modeset_lock_all(dev
);
775 ret
= drm_mode_object_get(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
);
779 connector
->base
.properties
= &connector
->properties
;
780 connector
->dev
= dev
;
781 connector
->funcs
= funcs
;
782 connector
->connector_type
= connector_type
;
783 connector
->connector_type_id
=
784 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
785 if (connector
->connector_type_id
< 0) {
786 ret
= connector
->connector_type_id
;
787 drm_mode_object_put(dev
, &connector
->base
);
790 INIT_LIST_HEAD(&connector
->probed_modes
);
791 INIT_LIST_HEAD(&connector
->modes
);
792 connector
->edid_blob_ptr
= NULL
;
793 connector
->status
= connector_status_unknown
;
795 list_add_tail(&connector
->head
, &dev
->mode_config
.connector_list
);
796 dev
->mode_config
.num_connector
++;
798 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
799 drm_object_attach_property(&connector
->base
,
800 dev
->mode_config
.edid_property
,
803 drm_object_attach_property(&connector
->base
,
804 dev
->mode_config
.dpms_property
, 0);
807 drm_modeset_unlock_all(dev
);
811 EXPORT_SYMBOL(drm_connector_init
);
814 * drm_connector_cleanup - cleans up an initialised connector
815 * @connector: connector to cleanup
817 * Cleans up the connector but doesn't free the object.
819 void drm_connector_cleanup(struct drm_connector
*connector
)
821 struct drm_device
*dev
= connector
->dev
;
822 struct drm_display_mode
*mode
, *t
;
824 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
825 drm_mode_remove(connector
, mode
);
827 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
828 drm_mode_remove(connector
, mode
);
830 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
831 connector
->connector_type_id
);
833 drm_mode_object_put(dev
, &connector
->base
);
834 list_del(&connector
->head
);
835 dev
->mode_config
.num_connector
--;
837 EXPORT_SYMBOL(drm_connector_cleanup
);
839 void drm_connector_unplug_all(struct drm_device
*dev
)
841 struct drm_connector
*connector
;
843 /* taking the mode config mutex ends up in a clash with sysfs */
844 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
845 drm_sysfs_connector_remove(connector
);
848 EXPORT_SYMBOL(drm_connector_unplug_all
);
850 int drm_bridge_init(struct drm_device
*dev
, struct drm_bridge
*bridge
,
851 const struct drm_bridge_funcs
*funcs
)
855 drm_modeset_lock_all(dev
);
857 ret
= drm_mode_object_get(dev
, &bridge
->base
, DRM_MODE_OBJECT_BRIDGE
);
862 bridge
->funcs
= funcs
;
864 list_add_tail(&bridge
->head
, &dev
->mode_config
.bridge_list
);
865 dev
->mode_config
.num_bridge
++;
868 drm_modeset_unlock_all(dev
);
871 EXPORT_SYMBOL(drm_bridge_init
);
873 void drm_bridge_cleanup(struct drm_bridge
*bridge
)
875 struct drm_device
*dev
= bridge
->dev
;
877 drm_modeset_lock_all(dev
);
878 drm_mode_object_put(dev
, &bridge
->base
);
879 list_del(&bridge
->head
);
880 dev
->mode_config
.num_bridge
--;
881 drm_modeset_unlock_all(dev
);
883 EXPORT_SYMBOL(drm_bridge_cleanup
);
885 int drm_encoder_init(struct drm_device
*dev
,
886 struct drm_encoder
*encoder
,
887 const struct drm_encoder_funcs
*funcs
,
892 drm_modeset_lock_all(dev
);
894 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
899 encoder
->encoder_type
= encoder_type
;
900 encoder
->funcs
= funcs
;
902 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
903 dev
->mode_config
.num_encoder
++;
906 drm_modeset_unlock_all(dev
);
910 EXPORT_SYMBOL(drm_encoder_init
);
912 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
914 struct drm_device
*dev
= encoder
->dev
;
915 drm_modeset_lock_all(dev
);
916 drm_mode_object_put(dev
, &encoder
->base
);
917 list_del(&encoder
->head
);
918 dev
->mode_config
.num_encoder
--;
919 drm_modeset_unlock_all(dev
);
921 EXPORT_SYMBOL(drm_encoder_cleanup
);
924 * drm_plane_init - Initialise a new plane object
926 * @plane: plane object to init
927 * @possible_crtcs: bitmask of possible CRTCs
928 * @funcs: callbacks for the new plane
929 * @formats: array of supported formats (%DRM_FORMAT_*)
930 * @format_count: number of elements in @formats
931 * @priv: plane is private (hidden from userspace)?
933 * Inits a new object created as base part of a driver plane object.
936 * Zero on success, error code on failure.
938 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
939 unsigned long possible_crtcs
,
940 const struct drm_plane_funcs
*funcs
,
941 const uint32_t *formats
, uint32_t format_count
,
946 drm_modeset_lock_all(dev
);
948 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
952 plane
->base
.properties
= &plane
->properties
;
954 plane
->funcs
= funcs
;
955 plane
->format_types
= kmalloc(sizeof(uint32_t) * format_count
,
957 if (!plane
->format_types
) {
958 DRM_DEBUG_KMS("out of memory when allocating plane\n");
959 drm_mode_object_put(dev
, &plane
->base
);
964 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
965 plane
->format_count
= format_count
;
966 plane
->possible_crtcs
= possible_crtcs
;
968 /* private planes are not exposed to userspace, but depending on
969 * display hardware, might be convenient to allow sharing programming
970 * for the scanout engine with the crtc implementation.
973 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
974 dev
->mode_config
.num_plane
++;
976 INIT_LIST_HEAD(&plane
->head
);
980 drm_modeset_unlock_all(dev
);
984 EXPORT_SYMBOL(drm_plane_init
);
987 * drm_plane_cleanup - Clean up the core plane usage
988 * @plane: plane to cleanup
990 * This function cleans up @plane and removes it from the DRM mode setting
991 * core. Note that the function does *not* free the plane structure itself,
992 * this is the responsibility of the caller.
994 void drm_plane_cleanup(struct drm_plane
*plane
)
996 struct drm_device
*dev
= plane
->dev
;
998 drm_modeset_lock_all(dev
);
999 kfree(plane
->format_types
);
1000 drm_mode_object_put(dev
, &plane
->base
);
1001 /* if not added to a list, it must be a private plane */
1002 if (!list_empty(&plane
->head
)) {
1003 list_del(&plane
->head
);
1004 dev
->mode_config
.num_plane
--;
1006 drm_modeset_unlock_all(dev
);
1008 EXPORT_SYMBOL(drm_plane_cleanup
);
1011 * drm_plane_force_disable - Forcibly disable a plane
1012 * @plane: plane to disable
1014 * Forces the plane to be disabled.
1016 * Used when the plane's current framebuffer is destroyed,
1017 * and when restoring fbdev mode.
1019 void drm_plane_force_disable(struct drm_plane
*plane
)
1026 ret
= plane
->funcs
->disable_plane(plane
);
1028 DRM_ERROR("failed to disable plane with busy fb\n");
1029 /* disconnect the plane from the fb and crtc: */
1030 __drm_framebuffer_unreference(plane
->fb
);
1034 EXPORT_SYMBOL(drm_plane_force_disable
);
1037 * drm_mode_create - create a new display mode
1040 * Create a new drm_display_mode, give it an ID, and return it.
1043 * Pointer to new mode on success, NULL on error.
1045 struct drm_display_mode
*drm_mode_create(struct drm_device
*dev
)
1047 struct drm_display_mode
*nmode
;
1049 nmode
= kzalloc(sizeof(struct drm_display_mode
), GFP_KERNEL
);
1053 if (drm_mode_object_get(dev
, &nmode
->base
, DRM_MODE_OBJECT_MODE
)) {
1060 EXPORT_SYMBOL(drm_mode_create
);
1063 * drm_mode_destroy - remove a mode
1065 * @mode: mode to remove
1067 * Free @mode's unique identifier, then free it.
1069 void drm_mode_destroy(struct drm_device
*dev
, struct drm_display_mode
*mode
)
1074 drm_mode_object_put(dev
, &mode
->base
);
1078 EXPORT_SYMBOL(drm_mode_destroy
);
1080 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
1082 struct drm_property
*edid
;
1083 struct drm_property
*dpms
;
1086 * Standard properties (apply to all connectors)
1088 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1089 DRM_MODE_PROP_IMMUTABLE
,
1091 dev
->mode_config
.edid_property
= edid
;
1093 dpms
= drm_property_create_enum(dev
, 0,
1094 "DPMS", drm_dpms_enum_list
,
1095 ARRAY_SIZE(drm_dpms_enum_list
));
1096 dev
->mode_config
.dpms_property
= dpms
;
1102 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1105 * Called by a driver the first time a DVI-I connector is made.
1107 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1109 struct drm_property
*dvi_i_selector
;
1110 struct drm_property
*dvi_i_subconnector
;
1112 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1116 drm_property_create_enum(dev
, 0,
1117 "select subconnector",
1118 drm_dvi_i_select_enum_list
,
1119 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1120 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1122 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1124 drm_dvi_i_subconnector_enum_list
,
1125 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1126 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1130 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1133 * drm_create_tv_properties - create TV specific connector properties
1135 * @num_modes: number of different TV formats (modes) supported
1136 * @modes: array of pointers to strings containing name of each format
1138 * Called by a driver's TV initialization routine, this function creates
1139 * the TV specific connector properties for a given device. Caller is
1140 * responsible for allocating a list of format names and passing them to
1143 int drm_mode_create_tv_properties(struct drm_device
*dev
, int num_modes
,
1146 struct drm_property
*tv_selector
;
1147 struct drm_property
*tv_subconnector
;
1150 if (dev
->mode_config
.tv_select_subconnector_property
)
1154 * Basic connector properties
1156 tv_selector
= drm_property_create_enum(dev
, 0,
1157 "select subconnector",
1158 drm_tv_select_enum_list
,
1159 ARRAY_SIZE(drm_tv_select_enum_list
));
1160 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1163 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1165 drm_tv_subconnector_enum_list
,
1166 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1167 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1170 * Other, TV specific properties: margins & TV modes.
1172 dev
->mode_config
.tv_left_margin_property
=
1173 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1175 dev
->mode_config
.tv_right_margin_property
=
1176 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1178 dev
->mode_config
.tv_top_margin_property
=
1179 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1181 dev
->mode_config
.tv_bottom_margin_property
=
1182 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1184 dev
->mode_config
.tv_mode_property
=
1185 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1187 for (i
= 0; i
< num_modes
; i
++)
1188 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1191 dev
->mode_config
.tv_brightness_property
=
1192 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1194 dev
->mode_config
.tv_contrast_property
=
1195 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1197 dev
->mode_config
.tv_flicker_reduction_property
=
1198 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1200 dev
->mode_config
.tv_overscan_property
=
1201 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1203 dev
->mode_config
.tv_saturation_property
=
1204 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1206 dev
->mode_config
.tv_hue_property
=
1207 drm_property_create_range(dev
, 0, "hue", 0, 100);
1211 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1214 * drm_mode_create_scaling_mode_property - create scaling mode property
1217 * Called by a driver the first time it's needed, must be attached to desired
1220 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1222 struct drm_property
*scaling_mode
;
1224 if (dev
->mode_config
.scaling_mode_property
)
1228 drm_property_create_enum(dev
, 0, "scaling mode",
1229 drm_scaling_mode_enum_list
,
1230 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1232 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1236 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1239 * drm_mode_create_dirty_property - create dirty property
1242 * Called by a driver the first time it's needed, must be attached to desired
1245 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1247 struct drm_property
*dirty_info
;
1249 if (dev
->mode_config
.dirty_info_property
)
1253 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1255 drm_dirty_info_enum_list
,
1256 ARRAY_SIZE(drm_dirty_info_enum_list
));
1257 dev
->mode_config
.dirty_info_property
= dirty_info
;
1261 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1263 static int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
1265 uint32_t total_objects
= 0;
1267 total_objects
+= dev
->mode_config
.num_crtc
;
1268 total_objects
+= dev
->mode_config
.num_connector
;
1269 total_objects
+= dev
->mode_config
.num_encoder
;
1270 total_objects
+= dev
->mode_config
.num_bridge
;
1272 group
->id_list
= kzalloc(total_objects
* sizeof(uint32_t), GFP_KERNEL
);
1273 if (!group
->id_list
)
1276 group
->num_crtcs
= 0;
1277 group
->num_connectors
= 0;
1278 group
->num_encoders
= 0;
1279 group
->num_bridges
= 0;
1283 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
1284 struct drm_mode_group
*group
)
1286 struct drm_crtc
*crtc
;
1287 struct drm_encoder
*encoder
;
1288 struct drm_connector
*connector
;
1289 struct drm_bridge
*bridge
;
1292 if ((ret
= drm_mode_group_init(dev
, group
)))
1295 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
1296 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
1298 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
1299 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
1302 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1303 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1304 group
->num_connectors
++] = connector
->base
.id
;
1306 list_for_each_entry(bridge
, &dev
->mode_config
.bridge_list
, head
)
1307 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1308 group
->num_connectors
+ group
->num_bridges
++] =
1313 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
1316 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1317 * @out: drm_mode_modeinfo struct to return to the user
1318 * @in: drm_display_mode to use
1320 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1323 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1324 const struct drm_display_mode
*in
)
1326 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1327 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1328 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1329 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1330 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1331 "timing values too large for mode info\n");
1333 out
->clock
= in
->clock
;
1334 out
->hdisplay
= in
->hdisplay
;
1335 out
->hsync_start
= in
->hsync_start
;
1336 out
->hsync_end
= in
->hsync_end
;
1337 out
->htotal
= in
->htotal
;
1338 out
->hskew
= in
->hskew
;
1339 out
->vdisplay
= in
->vdisplay
;
1340 out
->vsync_start
= in
->vsync_start
;
1341 out
->vsync_end
= in
->vsync_end
;
1342 out
->vtotal
= in
->vtotal
;
1343 out
->vscan
= in
->vscan
;
1344 out
->vrefresh
= in
->vrefresh
;
1345 out
->flags
= in
->flags
;
1346 out
->type
= in
->type
;
1347 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1348 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1352 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1353 * @out: drm_display_mode to return to the user
1354 * @in: drm_mode_modeinfo to use
1356 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1360 * Zero on success, errno on failure.
1362 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1363 const struct drm_mode_modeinfo
*in
)
1365 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1368 if ((in
->flags
& DRM_MODE_FLAG_3D_MASK
) > DRM_MODE_FLAG_3D_MAX
)
1371 out
->clock
= in
->clock
;
1372 out
->hdisplay
= in
->hdisplay
;
1373 out
->hsync_start
= in
->hsync_start
;
1374 out
->hsync_end
= in
->hsync_end
;
1375 out
->htotal
= in
->htotal
;
1376 out
->hskew
= in
->hskew
;
1377 out
->vdisplay
= in
->vdisplay
;
1378 out
->vsync_start
= in
->vsync_start
;
1379 out
->vsync_end
= in
->vsync_end
;
1380 out
->vtotal
= in
->vtotal
;
1381 out
->vscan
= in
->vscan
;
1382 out
->vrefresh
= in
->vrefresh
;
1383 out
->flags
= in
->flags
;
1384 out
->type
= in
->type
;
1385 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1386 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1392 * drm_mode_getresources - get graphics configuration
1393 * @dev: drm device for the ioctl
1394 * @data: data pointer for the ioctl
1395 * @file_priv: drm file for the ioctl call
1397 * Construct a set of configuration description structures and return
1398 * them to the user, including CRTC, connector and framebuffer configuration.
1400 * Called by the user via ioctl.
1403 * Zero on success, errno on failure.
1405 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1406 struct drm_file
*file_priv
)
1408 struct drm_mode_card_res
*card_res
= data
;
1409 struct list_head
*lh
;
1410 struct drm_framebuffer
*fb
;
1411 struct drm_connector
*connector
;
1412 struct drm_crtc
*crtc
;
1413 struct drm_encoder
*encoder
;
1415 int connector_count
= 0;
1418 int encoder_count
= 0;
1420 uint32_t __user
*fb_id
;
1421 uint32_t __user
*crtc_id
;
1422 uint32_t __user
*connector_id
;
1423 uint32_t __user
*encoder_id
;
1424 struct drm_mode_group
*mode_group
;
1426 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1430 mutex_lock(&file_priv
->fbs_lock
);
1432 * For the non-control nodes we need to limit the list of resources
1433 * by IDs in the group list for this node
1435 list_for_each(lh
, &file_priv
->fbs
)
1438 /* handle this in 4 parts */
1440 if (card_res
->count_fbs
>= fb_count
) {
1442 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1443 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1444 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1445 mutex_unlock(&file_priv
->fbs_lock
);
1451 card_res
->count_fbs
= fb_count
;
1452 mutex_unlock(&file_priv
->fbs_lock
);
1454 drm_modeset_lock_all(dev
);
1455 mode_group
= &file_priv
->master
->minor
->mode_group
;
1456 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1458 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1461 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1464 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1468 crtc_count
= mode_group
->num_crtcs
;
1469 connector_count
= mode_group
->num_connectors
;
1470 encoder_count
= mode_group
->num_encoders
;
1473 card_res
->max_height
= dev
->mode_config
.max_height
;
1474 card_res
->min_height
= dev
->mode_config
.min_height
;
1475 card_res
->max_width
= dev
->mode_config
.max_width
;
1476 card_res
->min_width
= dev
->mode_config
.min_width
;
1479 if (card_res
->count_crtcs
>= crtc_count
) {
1481 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1482 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1483 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1485 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1486 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1493 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1494 if (put_user(mode_group
->id_list
[i
],
1495 crtc_id
+ copied
)) {
1503 card_res
->count_crtcs
= crtc_count
;
1506 if (card_res
->count_encoders
>= encoder_count
) {
1508 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1509 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1510 list_for_each_entry(encoder
,
1511 &dev
->mode_config
.encoder_list
,
1513 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1514 drm_get_encoder_name(encoder
));
1515 if (put_user(encoder
->base
.id
, encoder_id
+
1523 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1524 if (put_user(mode_group
->id_list
[i
],
1525 encoder_id
+ copied
)) {
1534 card_res
->count_encoders
= encoder_count
;
1537 if (card_res
->count_connectors
>= connector_count
) {
1539 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1540 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1541 list_for_each_entry(connector
,
1542 &dev
->mode_config
.connector_list
,
1544 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1546 drm_get_connector_name(connector
));
1547 if (put_user(connector
->base
.id
,
1548 connector_id
+ copied
)) {
1555 int start
= mode_group
->num_crtcs
+
1556 mode_group
->num_encoders
;
1557 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1558 if (put_user(mode_group
->id_list
[i
],
1559 connector_id
+ copied
)) {
1567 card_res
->count_connectors
= connector_count
;
1569 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1570 card_res
->count_connectors
, card_res
->count_encoders
);
1573 drm_modeset_unlock_all(dev
);
1578 * drm_mode_getcrtc - get CRTC configuration
1579 * @dev: drm device for the ioctl
1580 * @data: data pointer for the ioctl
1581 * @file_priv: drm file for the ioctl call
1583 * Construct a CRTC configuration structure to return to the user.
1585 * Called by the user via ioctl.
1588 * Zero on success, errno on failure.
1590 int drm_mode_getcrtc(struct drm_device
*dev
,
1591 void *data
, struct drm_file
*file_priv
)
1593 struct drm_mode_crtc
*crtc_resp
= data
;
1594 struct drm_crtc
*crtc
;
1595 struct drm_mode_object
*obj
;
1598 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1601 drm_modeset_lock_all(dev
);
1603 obj
= drm_mode_object_find(dev
, crtc_resp
->crtc_id
,
1604 DRM_MODE_OBJECT_CRTC
);
1609 crtc
= obj_to_crtc(obj
);
1611 crtc_resp
->x
= crtc
->x
;
1612 crtc_resp
->y
= crtc
->y
;
1613 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1615 crtc_resp
->fb_id
= crtc
->fb
->base
.id
;
1617 crtc_resp
->fb_id
= 0;
1619 if (crtc
->enabled
) {
1621 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1622 crtc_resp
->mode_valid
= 1;
1625 crtc_resp
->mode_valid
= 0;
1629 drm_modeset_unlock_all(dev
);
1633 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
1634 const struct drm_file
*file_priv
)
1637 * If user-space hasn't configured the driver to expose the stereo 3D
1638 * modes, don't expose them.
1640 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
1647 * drm_mode_getconnector - get connector configuration
1648 * @dev: drm device for the ioctl
1649 * @data: data pointer for the ioctl
1650 * @file_priv: drm file for the ioctl call
1652 * Construct a connector configuration structure to return to the user.
1654 * Called by the user via ioctl.
1657 * Zero on success, errno on failure.
1659 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
1660 struct drm_file
*file_priv
)
1662 struct drm_mode_get_connector
*out_resp
= data
;
1663 struct drm_mode_object
*obj
;
1664 struct drm_connector
*connector
;
1665 struct drm_display_mode
*mode
;
1667 int props_count
= 0;
1668 int encoders_count
= 0;
1672 struct drm_mode_modeinfo u_mode
;
1673 struct drm_mode_modeinfo __user
*mode_ptr
;
1674 uint32_t __user
*prop_ptr
;
1675 uint64_t __user
*prop_values
;
1676 uint32_t __user
*encoder_ptr
;
1678 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1681 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
1683 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
1685 mutex_lock(&dev
->mode_config
.mutex
);
1687 obj
= drm_mode_object_find(dev
, out_resp
->connector_id
,
1688 DRM_MODE_OBJECT_CONNECTOR
);
1693 connector
= obj_to_connector(obj
);
1695 props_count
= connector
->properties
.count
;
1697 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1698 if (connector
->encoder_ids
[i
] != 0) {
1703 if (out_resp
->count_modes
== 0) {
1704 connector
->funcs
->fill_modes(connector
,
1705 dev
->mode_config
.max_width
,
1706 dev
->mode_config
.max_height
);
1709 /* delayed so we get modes regardless of pre-fill_modes state */
1710 list_for_each_entry(mode
, &connector
->modes
, head
)
1711 if (drm_mode_expose_to_userspace(mode
, file_priv
))
1714 out_resp
->connector_id
= connector
->base
.id
;
1715 out_resp
->connector_type
= connector
->connector_type
;
1716 out_resp
->connector_type_id
= connector
->connector_type_id
;
1717 out_resp
->mm_width
= connector
->display_info
.width_mm
;
1718 out_resp
->mm_height
= connector
->display_info
.height_mm
;
1719 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
1720 out_resp
->connection
= connector
->status
;
1721 if (connector
->encoder
)
1722 out_resp
->encoder_id
= connector
->encoder
->base
.id
;
1724 out_resp
->encoder_id
= 0;
1727 * This ioctl is called twice, once to determine how much space is
1728 * needed, and the 2nd time to fill it.
1730 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
1732 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
1733 list_for_each_entry(mode
, &connector
->modes
, head
) {
1734 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
1737 drm_crtc_convert_to_umode(&u_mode
, mode
);
1738 if (copy_to_user(mode_ptr
+ copied
,
1739 &u_mode
, sizeof(u_mode
))) {
1746 out_resp
->count_modes
= mode_count
;
1748 if ((out_resp
->count_props
>= props_count
) && props_count
) {
1750 prop_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
);
1751 prop_values
= (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
);
1752 for (i
= 0; i
< connector
->properties
.count
; i
++) {
1753 if (put_user(connector
->properties
.ids
[i
],
1754 prop_ptr
+ copied
)) {
1759 if (put_user(connector
->properties
.values
[i
],
1760 prop_values
+ copied
)) {
1767 out_resp
->count_props
= props_count
;
1769 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
1771 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
1772 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1773 if (connector
->encoder_ids
[i
] != 0) {
1774 if (put_user(connector
->encoder_ids
[i
],
1775 encoder_ptr
+ copied
)) {
1783 out_resp
->count_encoders
= encoders_count
;
1786 mutex_unlock(&dev
->mode_config
.mutex
);
1791 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
1792 struct drm_file
*file_priv
)
1794 struct drm_mode_get_encoder
*enc_resp
= data
;
1795 struct drm_mode_object
*obj
;
1796 struct drm_encoder
*encoder
;
1799 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1802 drm_modeset_lock_all(dev
);
1803 obj
= drm_mode_object_find(dev
, enc_resp
->encoder_id
,
1804 DRM_MODE_OBJECT_ENCODER
);
1809 encoder
= obj_to_encoder(obj
);
1812 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
1814 enc_resp
->crtc_id
= 0;
1815 enc_resp
->encoder_type
= encoder
->encoder_type
;
1816 enc_resp
->encoder_id
= encoder
->base
.id
;
1817 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
1818 enc_resp
->possible_clones
= encoder
->possible_clones
;
1821 drm_modeset_unlock_all(dev
);
1826 * drm_mode_getplane_res - get plane info
1829 * @file_priv: DRM file info
1831 * Return an plane count and set of IDs.
1833 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
1834 struct drm_file
*file_priv
)
1836 struct drm_mode_get_plane_res
*plane_resp
= data
;
1837 struct drm_mode_config
*config
;
1838 struct drm_plane
*plane
;
1839 uint32_t __user
*plane_ptr
;
1840 int copied
= 0, ret
= 0;
1842 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1845 drm_modeset_lock_all(dev
);
1846 config
= &dev
->mode_config
;
1849 * This ioctl is called twice, once to determine how much space is
1850 * needed, and the 2nd time to fill it.
1852 if (config
->num_plane
&&
1853 (plane_resp
->count_planes
>= config
->num_plane
)) {
1854 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
1856 list_for_each_entry(plane
, &config
->plane_list
, head
) {
1857 if (put_user(plane
->base
.id
, plane_ptr
+ copied
)) {
1864 plane_resp
->count_planes
= config
->num_plane
;
1867 drm_modeset_unlock_all(dev
);
1872 * drm_mode_getplane - get plane info
1875 * @file_priv: DRM file info
1877 * Return plane info, including formats supported, gamma size, any
1880 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
1881 struct drm_file
*file_priv
)
1883 struct drm_mode_get_plane
*plane_resp
= data
;
1884 struct drm_mode_object
*obj
;
1885 struct drm_plane
*plane
;
1886 uint32_t __user
*format_ptr
;
1889 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1892 drm_modeset_lock_all(dev
);
1893 obj
= drm_mode_object_find(dev
, plane_resp
->plane_id
,
1894 DRM_MODE_OBJECT_PLANE
);
1899 plane
= obj_to_plane(obj
);
1902 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
1904 plane_resp
->crtc_id
= 0;
1907 plane_resp
->fb_id
= plane
->fb
->base
.id
;
1909 plane_resp
->fb_id
= 0;
1911 plane_resp
->plane_id
= plane
->base
.id
;
1912 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
1913 plane_resp
->gamma_size
= 0;
1916 * This ioctl is called twice, once to determine how much space is
1917 * needed, and the 2nd time to fill it.
1919 if (plane
->format_count
&&
1920 (plane_resp
->count_format_types
>= plane
->format_count
)) {
1921 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
1922 if (copy_to_user(format_ptr
,
1923 plane
->format_types
,
1924 sizeof(uint32_t) * plane
->format_count
)) {
1929 plane_resp
->count_format_types
= plane
->format_count
;
1932 drm_modeset_unlock_all(dev
);
1937 * drm_mode_setplane - set up or tear down an plane
1939 * @data: ioctl data*
1940 * @file_priv: DRM file info
1942 * Set plane info, including placement, fb, scaling, and other factors.
1943 * Or pass a NULL fb to disable.
1945 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
1946 struct drm_file
*file_priv
)
1948 struct drm_mode_set_plane
*plane_req
= data
;
1949 struct drm_mode_object
*obj
;
1950 struct drm_plane
*plane
;
1951 struct drm_crtc
*crtc
;
1952 struct drm_framebuffer
*fb
= NULL
, *old_fb
= NULL
;
1954 unsigned int fb_width
, fb_height
;
1957 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1961 * First, find the plane, crtc, and fb objects. If not available,
1962 * we don't bother to call the driver.
1964 obj
= drm_mode_object_find(dev
, plane_req
->plane_id
,
1965 DRM_MODE_OBJECT_PLANE
);
1967 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1968 plane_req
->plane_id
);
1971 plane
= obj_to_plane(obj
);
1973 /* No fb means shut it down */
1974 if (!plane_req
->fb_id
) {
1975 drm_modeset_lock_all(dev
);
1977 plane
->funcs
->disable_plane(plane
);
1980 drm_modeset_unlock_all(dev
);
1984 obj
= drm_mode_object_find(dev
, plane_req
->crtc_id
,
1985 DRM_MODE_OBJECT_CRTC
);
1987 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1988 plane_req
->crtc_id
);
1992 crtc
= obj_to_crtc(obj
);
1994 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
1996 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2002 /* Check whether this plane supports the fb pixel format. */
2003 for (i
= 0; i
< plane
->format_count
; i
++)
2004 if (fb
->pixel_format
== plane
->format_types
[i
])
2006 if (i
== plane
->format_count
) {
2007 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2008 drm_get_format_name(fb
->pixel_format
));
2013 fb_width
= fb
->width
<< 16;
2014 fb_height
= fb
->height
<< 16;
2016 /* Make sure source coordinates are inside the fb. */
2017 if (plane_req
->src_w
> fb_width
||
2018 plane_req
->src_x
> fb_width
- plane_req
->src_w
||
2019 plane_req
->src_h
> fb_height
||
2020 plane_req
->src_y
> fb_height
- plane_req
->src_h
) {
2021 DRM_DEBUG_KMS("Invalid source coordinates "
2022 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2023 plane_req
->src_w
>> 16,
2024 ((plane_req
->src_w
& 0xffff) * 15625) >> 10,
2025 plane_req
->src_h
>> 16,
2026 ((plane_req
->src_h
& 0xffff) * 15625) >> 10,
2027 plane_req
->src_x
>> 16,
2028 ((plane_req
->src_x
& 0xffff) * 15625) >> 10,
2029 plane_req
->src_y
>> 16,
2030 ((plane_req
->src_y
& 0xffff) * 15625) >> 10);
2035 /* Give drivers some help against integer overflows */
2036 if (plane_req
->crtc_w
> INT_MAX
||
2037 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
2038 plane_req
->crtc_h
> INT_MAX
||
2039 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
2040 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2041 plane_req
->crtc_w
, plane_req
->crtc_h
,
2042 plane_req
->crtc_x
, plane_req
->crtc_y
);
2047 drm_modeset_lock_all(dev
);
2048 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2049 plane_req
->crtc_x
, plane_req
->crtc_y
,
2050 plane_req
->crtc_w
, plane_req
->crtc_h
,
2051 plane_req
->src_x
, plane_req
->src_y
,
2052 plane_req
->src_w
, plane_req
->src_h
);
2059 drm_modeset_unlock_all(dev
);
2063 drm_framebuffer_unreference(fb
);
2065 drm_framebuffer_unreference(old_fb
);
2071 * drm_mode_set_config_internal - helper to call ->set_config
2072 * @set: modeset config to set
2074 * This is a little helper to wrap internal calls to the ->set_config driver
2075 * interface. The only thing it adds is correct refcounting dance.
2077 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2079 struct drm_crtc
*crtc
= set
->crtc
;
2080 struct drm_framebuffer
*fb
;
2081 struct drm_crtc
*tmp
;
2085 * NOTE: ->set_config can also disable other crtcs (if we steal all
2086 * connectors from it), hence we need to refcount the fbs across all
2087 * crtcs. Atomic modeset will have saner semantics ...
2089 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
)
2090 tmp
->old_fb
= tmp
->fb
;
2094 ret
= crtc
->funcs
->set_config(set
);
2096 /* crtc->fb must be updated by ->set_config, enforces this. */
2097 WARN_ON(fb
!= crtc
->fb
);
2100 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
2102 drm_framebuffer_reference(tmp
->fb
);
2104 drm_framebuffer_unreference(tmp
->old_fb
);
2109 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2112 * Checks that the framebuffer is big enough for the CRTC viewport
2113 * (x, y, hdisplay, vdisplay)
2115 static int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2117 const struct drm_display_mode
*mode
,
2118 const struct drm_framebuffer
*fb
)
2121 int hdisplay
, vdisplay
;
2123 hdisplay
= mode
->hdisplay
;
2124 vdisplay
= mode
->vdisplay
;
2126 if (drm_mode_is_stereo(mode
)) {
2127 struct drm_display_mode adjusted
= *mode
;
2129 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE
);
2130 hdisplay
= adjusted
.crtc_hdisplay
;
2131 vdisplay
= adjusted
.crtc_vdisplay
;
2134 if (crtc
->invert_dimensions
)
2135 swap(hdisplay
, vdisplay
);
2137 if (hdisplay
> fb
->width
||
2138 vdisplay
> fb
->height
||
2139 x
> fb
->width
- hdisplay
||
2140 y
> fb
->height
- vdisplay
) {
2141 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2142 fb
->width
, fb
->height
, hdisplay
, vdisplay
, x
, y
,
2143 crtc
->invert_dimensions
? " (inverted)" : "");
2151 * drm_mode_setcrtc - set CRTC configuration
2152 * @dev: drm device for the ioctl
2153 * @data: data pointer for the ioctl
2154 * @file_priv: drm file for the ioctl call
2156 * Build a new CRTC configuration based on user request.
2158 * Called by the user via ioctl.
2161 * Zero on success, errno on failure.
2163 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2164 struct drm_file
*file_priv
)
2166 struct drm_mode_config
*config
= &dev
->mode_config
;
2167 struct drm_mode_crtc
*crtc_req
= data
;
2168 struct drm_mode_object
*obj
;
2169 struct drm_crtc
*crtc
;
2170 struct drm_connector
**connector_set
= NULL
, *connector
;
2171 struct drm_framebuffer
*fb
= NULL
;
2172 struct drm_display_mode
*mode
= NULL
;
2173 struct drm_mode_set set
;
2174 uint32_t __user
*set_connectors_ptr
;
2178 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2181 /* For some reason crtc x/y offsets are signed internally. */
2182 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
2185 drm_modeset_lock_all(dev
);
2186 obj
= drm_mode_object_find(dev
, crtc_req
->crtc_id
,
2187 DRM_MODE_OBJECT_CRTC
);
2189 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2193 crtc
= obj_to_crtc(obj
);
2194 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
2196 if (crtc_req
->mode_valid
) {
2197 /* If we have a mode we need a framebuffer. */
2198 /* If we pass -1, set the mode with the currently bound fb */
2199 if (crtc_req
->fb_id
== -1) {
2201 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2206 /* Make refcounting symmetric with the lookup path. */
2207 drm_framebuffer_reference(fb
);
2209 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2211 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2218 mode
= drm_mode_create(dev
);
2224 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
2226 DRM_DEBUG_KMS("Invalid mode\n");
2230 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2232 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2239 if (crtc_req
->count_connectors
== 0 && mode
) {
2240 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2245 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2246 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2247 crtc_req
->count_connectors
);
2252 if (crtc_req
->count_connectors
> 0) {
2255 /* Avoid unbounded kernel memory allocation */
2256 if (crtc_req
->count_connectors
> config
->num_connector
) {
2261 connector_set
= kmalloc(crtc_req
->count_connectors
*
2262 sizeof(struct drm_connector
*),
2264 if (!connector_set
) {
2269 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2270 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2271 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2276 obj
= drm_mode_object_find(dev
, out_id
,
2277 DRM_MODE_OBJECT_CONNECTOR
);
2279 DRM_DEBUG_KMS("Connector id %d unknown\n",
2284 connector
= obj_to_connector(obj
);
2285 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2287 drm_get_connector_name(connector
));
2289 connector_set
[i
] = connector
;
2294 set
.x
= crtc_req
->x
;
2295 set
.y
= crtc_req
->y
;
2297 set
.connectors
= connector_set
;
2298 set
.num_connectors
= crtc_req
->count_connectors
;
2300 ret
= drm_mode_set_config_internal(&set
);
2304 drm_framebuffer_unreference(fb
);
2306 kfree(connector_set
);
2307 drm_mode_destroy(dev
, mode
);
2308 drm_modeset_unlock_all(dev
);
2312 static int drm_mode_cursor_common(struct drm_device
*dev
,
2313 struct drm_mode_cursor2
*req
,
2314 struct drm_file
*file_priv
)
2316 struct drm_mode_object
*obj
;
2317 struct drm_crtc
*crtc
;
2320 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2323 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2326 obj
= drm_mode_object_find(dev
, req
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
2328 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2331 crtc
= obj_to_crtc(obj
);
2333 mutex_lock(&crtc
->mutex
);
2334 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2335 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2339 /* Turns off the cursor if handle is 0 */
2340 if (crtc
->funcs
->cursor_set2
)
2341 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2342 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2344 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2345 req
->width
, req
->height
);
2348 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2349 if (crtc
->funcs
->cursor_move
) {
2350 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2357 mutex_unlock(&crtc
->mutex
);
2362 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
2363 void *data
, struct drm_file
*file_priv
)
2365 struct drm_mode_cursor
*req
= data
;
2366 struct drm_mode_cursor2 new_req
;
2368 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
2369 new_req
.hot_x
= new_req
.hot_y
= 0;
2371 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
2374 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
2375 void *data
, struct drm_file
*file_priv
)
2377 struct drm_mode_cursor2
*req
= data
;
2378 return drm_mode_cursor_common(dev
, req
, file_priv
);
2381 /* Original addfb only supported RGB formats, so figure out which one */
2382 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2388 fmt
= DRM_FORMAT_C8
;
2392 fmt
= DRM_FORMAT_XRGB1555
;
2394 fmt
= DRM_FORMAT_RGB565
;
2397 fmt
= DRM_FORMAT_RGB888
;
2401 fmt
= DRM_FORMAT_XRGB8888
;
2402 else if (depth
== 30)
2403 fmt
= DRM_FORMAT_XRGB2101010
;
2405 fmt
= DRM_FORMAT_ARGB8888
;
2408 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2409 fmt
= DRM_FORMAT_XRGB8888
;
2415 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2418 * drm_mode_addfb - add an FB to the graphics configuration
2419 * @dev: drm device for the ioctl
2420 * @data: data pointer for the ioctl
2421 * @file_priv: drm file for the ioctl call
2423 * Add a new FB to the specified CRTC, given a user request.
2425 * Called by the user via ioctl.
2428 * Zero on success, errno on failure.
2430 int drm_mode_addfb(struct drm_device
*dev
,
2431 void *data
, struct drm_file
*file_priv
)
2433 struct drm_mode_fb_cmd
*or = data
;
2434 struct drm_mode_fb_cmd2 r
= {};
2435 struct drm_mode_config
*config
= &dev
->mode_config
;
2436 struct drm_framebuffer
*fb
;
2439 /* Use new struct with format internally */
2440 r
.fb_id
= or->fb_id
;
2441 r
.width
= or->width
;
2442 r
.height
= or->height
;
2443 r
.pitches
[0] = or->pitch
;
2444 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2445 r
.handles
[0] = or->handle
;
2447 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2450 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2453 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2456 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2458 DRM_DEBUG_KMS("could not create framebuffer\n");
2462 mutex_lock(&file_priv
->fbs_lock
);
2463 or->fb_id
= fb
->base
.id
;
2464 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2465 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2466 mutex_unlock(&file_priv
->fbs_lock
);
2471 static int format_check(const struct drm_mode_fb_cmd2
*r
)
2473 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2477 case DRM_FORMAT_RGB332
:
2478 case DRM_FORMAT_BGR233
:
2479 case DRM_FORMAT_XRGB4444
:
2480 case DRM_FORMAT_XBGR4444
:
2481 case DRM_FORMAT_RGBX4444
:
2482 case DRM_FORMAT_BGRX4444
:
2483 case DRM_FORMAT_ARGB4444
:
2484 case DRM_FORMAT_ABGR4444
:
2485 case DRM_FORMAT_RGBA4444
:
2486 case DRM_FORMAT_BGRA4444
:
2487 case DRM_FORMAT_XRGB1555
:
2488 case DRM_FORMAT_XBGR1555
:
2489 case DRM_FORMAT_RGBX5551
:
2490 case DRM_FORMAT_BGRX5551
:
2491 case DRM_FORMAT_ARGB1555
:
2492 case DRM_FORMAT_ABGR1555
:
2493 case DRM_FORMAT_RGBA5551
:
2494 case DRM_FORMAT_BGRA5551
:
2495 case DRM_FORMAT_RGB565
:
2496 case DRM_FORMAT_BGR565
:
2497 case DRM_FORMAT_RGB888
:
2498 case DRM_FORMAT_BGR888
:
2499 case DRM_FORMAT_XRGB8888
:
2500 case DRM_FORMAT_XBGR8888
:
2501 case DRM_FORMAT_RGBX8888
:
2502 case DRM_FORMAT_BGRX8888
:
2503 case DRM_FORMAT_ARGB8888
:
2504 case DRM_FORMAT_ABGR8888
:
2505 case DRM_FORMAT_RGBA8888
:
2506 case DRM_FORMAT_BGRA8888
:
2507 case DRM_FORMAT_XRGB2101010
:
2508 case DRM_FORMAT_XBGR2101010
:
2509 case DRM_FORMAT_RGBX1010102
:
2510 case DRM_FORMAT_BGRX1010102
:
2511 case DRM_FORMAT_ARGB2101010
:
2512 case DRM_FORMAT_ABGR2101010
:
2513 case DRM_FORMAT_RGBA1010102
:
2514 case DRM_FORMAT_BGRA1010102
:
2515 case DRM_FORMAT_YUYV
:
2516 case DRM_FORMAT_YVYU
:
2517 case DRM_FORMAT_UYVY
:
2518 case DRM_FORMAT_VYUY
:
2519 case DRM_FORMAT_AYUV
:
2520 case DRM_FORMAT_NV12
:
2521 case DRM_FORMAT_NV21
:
2522 case DRM_FORMAT_NV16
:
2523 case DRM_FORMAT_NV61
:
2524 case DRM_FORMAT_NV24
:
2525 case DRM_FORMAT_NV42
:
2526 case DRM_FORMAT_YUV410
:
2527 case DRM_FORMAT_YVU410
:
2528 case DRM_FORMAT_YUV411
:
2529 case DRM_FORMAT_YVU411
:
2530 case DRM_FORMAT_YUV420
:
2531 case DRM_FORMAT_YVU420
:
2532 case DRM_FORMAT_YUV422
:
2533 case DRM_FORMAT_YVU422
:
2534 case DRM_FORMAT_YUV444
:
2535 case DRM_FORMAT_YVU444
:
2538 DRM_DEBUG_KMS("invalid pixel format %s\n",
2539 drm_get_format_name(r
->pixel_format
));
2544 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
2546 int ret
, hsub
, vsub
, num_planes
, i
;
2548 ret
= format_check(r
);
2550 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2551 drm_get_format_name(r
->pixel_format
));
2555 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
2556 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
2557 num_planes
= drm_format_num_planes(r
->pixel_format
);
2559 if (r
->width
== 0 || r
->width
% hsub
) {
2560 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
2564 if (r
->height
== 0 || r
->height
% vsub
) {
2565 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
2569 for (i
= 0; i
< num_planes
; i
++) {
2570 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
2571 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
2572 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
2574 if (!r
->handles
[i
]) {
2575 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
2579 if ((uint64_t) width
* cpp
> UINT_MAX
)
2582 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
2585 if (r
->pitches
[i
] < width
* cpp
) {
2586 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
2595 * drm_mode_addfb2 - add an FB to the graphics configuration
2596 * @dev: drm device for the ioctl
2597 * @data: data pointer for the ioctl
2598 * @file_priv: drm file for the ioctl call
2600 * Add a new FB to the specified CRTC, given a user request with format.
2602 * Called by the user via ioctl.
2605 * Zero on success, errno on failure.
2607 int drm_mode_addfb2(struct drm_device
*dev
,
2608 void *data
, struct drm_file
*file_priv
)
2610 struct drm_mode_fb_cmd2
*r
= data
;
2611 struct drm_mode_config
*config
= &dev
->mode_config
;
2612 struct drm_framebuffer
*fb
;
2615 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2618 if (r
->flags
& ~DRM_MODE_FB_INTERLACED
) {
2619 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
2623 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
2624 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2625 r
->width
, config
->min_width
, config
->max_width
);
2628 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
2629 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2630 r
->height
, config
->min_height
, config
->max_height
);
2634 ret
= framebuffer_check(r
);
2638 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
2640 DRM_DEBUG_KMS("could not create framebuffer\n");
2644 mutex_lock(&file_priv
->fbs_lock
);
2645 r
->fb_id
= fb
->base
.id
;
2646 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2647 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2648 mutex_unlock(&file_priv
->fbs_lock
);
2655 * drm_mode_rmfb - remove an FB from the configuration
2656 * @dev: drm device for the ioctl
2657 * @data: data pointer for the ioctl
2658 * @file_priv: drm file for the ioctl call
2660 * Remove the FB specified by the user.
2662 * Called by the user via ioctl.
2665 * Zero on success, errno on failure.
2667 int drm_mode_rmfb(struct drm_device
*dev
,
2668 void *data
, struct drm_file
*file_priv
)
2670 struct drm_framebuffer
*fb
= NULL
;
2671 struct drm_framebuffer
*fbl
= NULL
;
2672 uint32_t *id
= data
;
2675 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2678 mutex_lock(&file_priv
->fbs_lock
);
2679 mutex_lock(&dev
->mode_config
.fb_lock
);
2680 fb
= __drm_framebuffer_lookup(dev
, *id
);
2684 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
2690 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2691 __drm_framebuffer_unregister(dev
, fb
);
2693 list_del_init(&fb
->filp_head
);
2694 mutex_unlock(&dev
->mode_config
.fb_lock
);
2695 mutex_unlock(&file_priv
->fbs_lock
);
2697 drm_framebuffer_remove(fb
);
2702 mutex_unlock(&dev
->mode_config
.fb_lock
);
2703 mutex_unlock(&file_priv
->fbs_lock
);
2709 * drm_mode_getfb - get FB info
2710 * @dev: drm device for the ioctl
2711 * @data: data pointer for the ioctl
2712 * @file_priv: drm file for the ioctl call
2714 * Lookup the FB given its ID and return info about it.
2716 * Called by the user via ioctl.
2719 * Zero on success, errno on failure.
2721 int drm_mode_getfb(struct drm_device
*dev
,
2722 void *data
, struct drm_file
*file_priv
)
2724 struct drm_mode_fb_cmd
*r
= data
;
2725 struct drm_framebuffer
*fb
;
2728 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2731 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
2735 r
->height
= fb
->height
;
2736 r
->width
= fb
->width
;
2737 r
->depth
= fb
->depth
;
2738 r
->bpp
= fb
->bits_per_pixel
;
2739 r
->pitch
= fb
->pitches
[0];
2740 if (fb
->funcs
->create_handle
) {
2741 if (file_priv
->is_master
|| capable(CAP_SYS_ADMIN
)) {
2742 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
2745 /* GET_FB() is an unprivileged ioctl so we must not
2746 * return a buffer-handle to non-master processes! For
2747 * backwards-compatibility reasons, we cannot make
2748 * GET_FB() privileged, so just return an invalid handle
2749 * for non-masters. */
2757 drm_framebuffer_unreference(fb
);
2762 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
2763 void *data
, struct drm_file
*file_priv
)
2765 struct drm_clip_rect __user
*clips_ptr
;
2766 struct drm_clip_rect
*clips
= NULL
;
2767 struct drm_mode_fb_dirty_cmd
*r
= data
;
2768 struct drm_framebuffer
*fb
;
2773 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2776 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
2780 num_clips
= r
->num_clips
;
2781 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
2783 if (!num_clips
!= !clips_ptr
) {
2788 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
2790 /* If userspace annotates copy, clips must come in pairs */
2791 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
2796 if (num_clips
&& clips_ptr
) {
2797 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
2801 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
2807 ret
= copy_from_user(clips
, clips_ptr
,
2808 num_clips
* sizeof(*clips
));
2815 if (fb
->funcs
->dirty
) {
2816 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
2825 drm_framebuffer_unreference(fb
);
2832 * drm_fb_release - remove and free the FBs on this file
2833 * @priv: drm file for the ioctl
2835 * Destroy all the FBs associated with @filp.
2837 * Called by the user via ioctl.
2840 * Zero on success, errno on failure.
2842 void drm_fb_release(struct drm_file
*priv
)
2844 struct drm_device
*dev
= priv
->minor
->dev
;
2845 struct drm_framebuffer
*fb
, *tfb
;
2847 mutex_lock(&priv
->fbs_lock
);
2848 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
2850 mutex_lock(&dev
->mode_config
.fb_lock
);
2851 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2852 __drm_framebuffer_unregister(dev
, fb
);
2853 mutex_unlock(&dev
->mode_config
.fb_lock
);
2855 list_del_init(&fb
->filp_head
);
2857 /* This will also drop the fpriv->fbs reference. */
2858 drm_framebuffer_remove(fb
);
2860 mutex_unlock(&priv
->fbs_lock
);
2863 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
2864 const char *name
, int num_values
)
2866 struct drm_property
*property
= NULL
;
2869 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
2874 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
2875 if (!property
->values
)
2879 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
2883 property
->flags
= flags
;
2884 property
->num_values
= num_values
;
2885 INIT_LIST_HEAD(&property
->enum_blob_list
);
2888 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
2889 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2892 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
2895 kfree(property
->values
);
2899 EXPORT_SYMBOL(drm_property_create
);
2901 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
2903 const struct drm_prop_enum_list
*props
,
2906 struct drm_property
*property
;
2909 flags
|= DRM_MODE_PROP_ENUM
;
2911 property
= drm_property_create(dev
, flags
, name
, num_values
);
2915 for (i
= 0; i
< num_values
; i
++) {
2916 ret
= drm_property_add_enum(property
, i
,
2920 drm_property_destroy(dev
, property
);
2927 EXPORT_SYMBOL(drm_property_create_enum
);
2929 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
2930 int flags
, const char *name
,
2931 const struct drm_prop_enum_list
*props
,
2934 struct drm_property
*property
;
2937 flags
|= DRM_MODE_PROP_BITMASK
;
2939 property
= drm_property_create(dev
, flags
, name
, num_values
);
2943 for (i
= 0; i
< num_values
; i
++) {
2944 ret
= drm_property_add_enum(property
, i
,
2948 drm_property_destroy(dev
, property
);
2955 EXPORT_SYMBOL(drm_property_create_bitmask
);
2957 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
2959 uint64_t min
, uint64_t max
)
2961 struct drm_property
*property
;
2963 flags
|= DRM_MODE_PROP_RANGE
;
2965 property
= drm_property_create(dev
, flags
, name
, 2);
2969 property
->values
[0] = min
;
2970 property
->values
[1] = max
;
2974 EXPORT_SYMBOL(drm_property_create_range
);
2976 int drm_property_add_enum(struct drm_property
*property
, int index
,
2977 uint64_t value
, const char *name
)
2979 struct drm_property_enum
*prop_enum
;
2981 if (!(property
->flags
& (DRM_MODE_PROP_ENUM
| DRM_MODE_PROP_BITMASK
)))
2985 * Bitmask enum properties have the additional constraint of values
2988 if ((property
->flags
& DRM_MODE_PROP_BITMASK
) && (value
> 63))
2991 if (!list_empty(&property
->enum_blob_list
)) {
2992 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
2993 if (prop_enum
->value
== value
) {
2994 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
2995 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3001 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3005 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3006 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3007 prop_enum
->value
= value
;
3009 property
->values
[index
] = value
;
3010 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
3013 EXPORT_SYMBOL(drm_property_add_enum
);
3015 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
3017 struct drm_property_enum
*prop_enum
, *pt
;
3019 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
3020 list_del(&prop_enum
->head
);
3024 if (property
->num_values
)
3025 kfree(property
->values
);
3026 drm_mode_object_put(dev
, &property
->base
);
3027 list_del(&property
->head
);
3030 EXPORT_SYMBOL(drm_property_destroy
);
3032 void drm_object_attach_property(struct drm_mode_object
*obj
,
3033 struct drm_property
*property
,
3036 int count
= obj
->properties
->count
;
3038 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
3039 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3040 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3041 "you see this message on the same object type.\n",
3046 obj
->properties
->ids
[count
] = property
->base
.id
;
3047 obj
->properties
->values
[count
] = init_val
;
3048 obj
->properties
->count
++;
3050 EXPORT_SYMBOL(drm_object_attach_property
);
3052 int drm_object_property_set_value(struct drm_mode_object
*obj
,
3053 struct drm_property
*property
, uint64_t val
)
3057 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3058 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3059 obj
->properties
->values
[i
] = val
;
3066 EXPORT_SYMBOL(drm_object_property_set_value
);
3068 int drm_object_property_get_value(struct drm_mode_object
*obj
,
3069 struct drm_property
*property
, uint64_t *val
)
3073 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3074 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3075 *val
= obj
->properties
->values
[i
];
3082 EXPORT_SYMBOL(drm_object_property_get_value
);
3084 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
3085 void *data
, struct drm_file
*file_priv
)
3087 struct drm_mode_object
*obj
;
3088 struct drm_mode_get_property
*out_resp
= data
;
3089 struct drm_property
*property
;
3092 int value_count
= 0;
3095 struct drm_property_enum
*prop_enum
;
3096 struct drm_mode_property_enum __user
*enum_ptr
;
3097 struct drm_property_blob
*prop_blob
;
3098 uint32_t __user
*blob_id_ptr
;
3099 uint64_t __user
*values_ptr
;
3100 uint32_t __user
*blob_length_ptr
;
3102 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3105 drm_modeset_lock_all(dev
);
3106 obj
= drm_mode_object_find(dev
, out_resp
->prop_id
, DRM_MODE_OBJECT_PROPERTY
);
3111 property
= obj_to_property(obj
);
3113 if (property
->flags
& (DRM_MODE_PROP_ENUM
| DRM_MODE_PROP_BITMASK
)) {
3114 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
3116 } else if (property
->flags
& DRM_MODE_PROP_BLOB
) {
3117 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
3121 value_count
= property
->num_values
;
3123 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
3124 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
3125 out_resp
->flags
= property
->flags
;
3127 if ((out_resp
->count_values
>= value_count
) && value_count
) {
3128 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
3129 for (i
= 0; i
< value_count
; i
++) {
3130 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
3136 out_resp
->count_values
= value_count
;
3138 if (property
->flags
& (DRM_MODE_PROP_ENUM
| DRM_MODE_PROP_BITMASK
)) {
3139 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
3141 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3142 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3144 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
3149 if (copy_to_user(&enum_ptr
[copied
].name
,
3150 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3157 out_resp
->count_enum_blobs
= enum_count
;
3160 if (property
->flags
& DRM_MODE_PROP_BLOB
) {
3161 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
3163 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3164 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
3166 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
3167 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
3172 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
3180 out_resp
->count_enum_blobs
= blob_count
;
3183 drm_modeset_unlock_all(dev
);
3187 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
3190 struct drm_property_blob
*blob
;
3193 if (!length
|| !data
)
3196 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
3200 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
3206 blob
->length
= length
;
3208 memcpy(blob
->data
, data
, length
);
3210 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3214 static void drm_property_destroy_blob(struct drm_device
*dev
,
3215 struct drm_property_blob
*blob
)
3217 drm_mode_object_put(dev
, &blob
->base
);
3218 list_del(&blob
->head
);
3222 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3223 void *data
, struct drm_file
*file_priv
)
3225 struct drm_mode_object
*obj
;
3226 struct drm_mode_get_blob
*out_resp
= data
;
3227 struct drm_property_blob
*blob
;
3229 void __user
*blob_ptr
;
3231 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3234 drm_modeset_lock_all(dev
);
3235 obj
= drm_mode_object_find(dev
, out_resp
->blob_id
, DRM_MODE_OBJECT_BLOB
);
3240 blob
= obj_to_blob(obj
);
3242 if (out_resp
->length
== blob
->length
) {
3243 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3244 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3249 out_resp
->length
= blob
->length
;
3252 drm_modeset_unlock_all(dev
);
3256 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
3259 struct drm_device
*dev
= connector
->dev
;
3262 if (connector
->edid_blob_ptr
)
3263 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
3265 /* Delete edid, when there is none. */
3267 connector
->edid_blob_ptr
= NULL
;
3268 ret
= drm_object_property_set_value(&connector
->base
, dev
->mode_config
.edid_property
, 0);
3272 size
= EDID_LENGTH
* (1 + edid
->extensions
);
3273 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
3275 if (!connector
->edid_blob_ptr
)
3278 ret
= drm_object_property_set_value(&connector
->base
,
3279 dev
->mode_config
.edid_property
,
3280 connector
->edid_blob_ptr
->base
.id
);
3284 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
3286 static bool drm_property_change_is_valid(struct drm_property
*property
,
3289 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
3291 if (property
->flags
& DRM_MODE_PROP_RANGE
) {
3292 if (value
< property
->values
[0] || value
> property
->values
[1])
3295 } else if (property
->flags
& DRM_MODE_PROP_BITMASK
) {
3297 uint64_t valid_mask
= 0;
3298 for (i
= 0; i
< property
->num_values
; i
++)
3299 valid_mask
|= (1ULL << property
->values
[i
]);
3300 return !(value
& ~valid_mask
);
3301 } else if (property
->flags
& DRM_MODE_PROP_BLOB
) {
3302 /* Only the driver knows */
3306 for (i
= 0; i
< property
->num_values
; i
++)
3307 if (property
->values
[i
] == value
)
3313 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
3314 void *data
, struct drm_file
*file_priv
)
3316 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
3317 struct drm_mode_obj_set_property obj_set_prop
= {
3318 .value
= conn_set_prop
->value
,
3319 .prop_id
= conn_set_prop
->prop_id
,
3320 .obj_id
= conn_set_prop
->connector_id
,
3321 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
3324 /* It does all the locking and checking we need */
3325 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
3328 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
3329 struct drm_property
*property
,
3333 struct drm_connector
*connector
= obj_to_connector(obj
);
3335 /* Do DPMS ourselves */
3336 if (property
== connector
->dev
->mode_config
.dpms_property
) {
3337 if (connector
->funcs
->dpms
)
3338 (*connector
->funcs
->dpms
)(connector
, (int)value
);
3340 } else if (connector
->funcs
->set_property
)
3341 ret
= connector
->funcs
->set_property(connector
, property
, value
);
3343 /* store the property value if successful */
3345 drm_object_property_set_value(&connector
->base
, property
, value
);
3349 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
3350 struct drm_property
*property
,
3354 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
3356 if (crtc
->funcs
->set_property
)
3357 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
3359 drm_object_property_set_value(obj
, property
, value
);
3364 static int drm_mode_plane_set_obj_prop(struct drm_mode_object
*obj
,
3365 struct drm_property
*property
,
3369 struct drm_plane
*plane
= obj_to_plane(obj
);
3371 if (plane
->funcs
->set_property
)
3372 ret
= plane
->funcs
->set_property(plane
, property
, value
);
3374 drm_object_property_set_value(obj
, property
, value
);
3379 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
3380 struct drm_file
*file_priv
)
3382 struct drm_mode_obj_get_properties
*arg
= data
;
3383 struct drm_mode_object
*obj
;
3387 int props_count
= 0;
3388 uint32_t __user
*props_ptr
;
3389 uint64_t __user
*prop_values_ptr
;
3391 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3394 drm_modeset_lock_all(dev
);
3396 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3401 if (!obj
->properties
) {
3406 props_count
= obj
->properties
->count
;
3408 /* This ioctl is called twice, once to determine how much space is
3409 * needed, and the 2nd time to fill it. */
3410 if ((arg
->count_props
>= props_count
) && props_count
) {
3412 props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
3413 prop_values_ptr
= (uint64_t __user
*)(unsigned long)
3414 (arg
->prop_values_ptr
);
3415 for (i
= 0; i
< props_count
; i
++) {
3416 if (put_user(obj
->properties
->ids
[i
],
3417 props_ptr
+ copied
)) {
3421 if (put_user(obj
->properties
->values
[i
],
3422 prop_values_ptr
+ copied
)) {
3429 arg
->count_props
= props_count
;
3431 drm_modeset_unlock_all(dev
);
3435 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
3436 struct drm_file
*file_priv
)
3438 struct drm_mode_obj_set_property
*arg
= data
;
3439 struct drm_mode_object
*arg_obj
;
3440 struct drm_mode_object
*prop_obj
;
3441 struct drm_property
*property
;
3445 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3448 drm_modeset_lock_all(dev
);
3450 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3455 if (!arg_obj
->properties
)
3458 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
3459 if (arg_obj
->properties
->ids
[i
] == arg
->prop_id
)
3462 if (i
== arg_obj
->properties
->count
)
3465 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
3466 DRM_MODE_OBJECT_PROPERTY
);
3471 property
= obj_to_property(prop_obj
);
3473 if (!drm_property_change_is_valid(property
, arg
->value
))
3476 switch (arg_obj
->type
) {
3477 case DRM_MODE_OBJECT_CONNECTOR
:
3478 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
3481 case DRM_MODE_OBJECT_CRTC
:
3482 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
3484 case DRM_MODE_OBJECT_PLANE
:
3485 ret
= drm_mode_plane_set_obj_prop(arg_obj
, property
, arg
->value
);
3490 drm_modeset_unlock_all(dev
);
3494 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
3495 struct drm_encoder
*encoder
)
3499 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
3500 if (connector
->encoder_ids
[i
] == 0) {
3501 connector
->encoder_ids
[i
] = encoder
->base
.id
;
3507 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
3509 void drm_mode_connector_detach_encoder(struct drm_connector
*connector
,
3510 struct drm_encoder
*encoder
)
3513 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
3514 if (connector
->encoder_ids
[i
] == encoder
->base
.id
) {
3515 connector
->encoder_ids
[i
] = 0;
3516 if (connector
->encoder
== encoder
)
3517 connector
->encoder
= NULL
;
3522 EXPORT_SYMBOL(drm_mode_connector_detach_encoder
);
3524 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
3527 crtc
->gamma_size
= gamma_size
;
3529 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
3530 if (!crtc
->gamma_store
) {
3531 crtc
->gamma_size
= 0;
3537 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
3539 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
3540 void *data
, struct drm_file
*file_priv
)
3542 struct drm_mode_crtc_lut
*crtc_lut
= data
;
3543 struct drm_mode_object
*obj
;
3544 struct drm_crtc
*crtc
;
3545 void *r_base
, *g_base
, *b_base
;
3549 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3552 drm_modeset_lock_all(dev
);
3553 obj
= drm_mode_object_find(dev
, crtc_lut
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3558 crtc
= obj_to_crtc(obj
);
3560 if (crtc
->funcs
->gamma_set
== NULL
) {
3565 /* memcpy into gamma store */
3566 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
3571 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
3572 r_base
= crtc
->gamma_store
;
3573 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
3578 g_base
= r_base
+ size
;
3579 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
3584 b_base
= g_base
+ size
;
3585 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
3590 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
3593 drm_modeset_unlock_all(dev
);
3598 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
3599 void *data
, struct drm_file
*file_priv
)
3601 struct drm_mode_crtc_lut
*crtc_lut
= data
;
3602 struct drm_mode_object
*obj
;
3603 struct drm_crtc
*crtc
;
3604 void *r_base
, *g_base
, *b_base
;
3608 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3611 drm_modeset_lock_all(dev
);
3612 obj
= drm_mode_object_find(dev
, crtc_lut
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3617 crtc
= obj_to_crtc(obj
);
3619 /* memcpy into gamma store */
3620 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
3625 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
3626 r_base
= crtc
->gamma_store
;
3627 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
3632 g_base
= r_base
+ size
;
3633 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
3638 b_base
= g_base
+ size
;
3639 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
3644 drm_modeset_unlock_all(dev
);
3648 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
3649 void *data
, struct drm_file
*file_priv
)
3651 struct drm_mode_crtc_page_flip
*page_flip
= data
;
3652 struct drm_mode_object
*obj
;
3653 struct drm_crtc
*crtc
;
3654 struct drm_framebuffer
*fb
= NULL
, *old_fb
= NULL
;
3655 struct drm_pending_vblank_event
*e
= NULL
;
3656 unsigned long flags
;
3659 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
3660 page_flip
->reserved
!= 0)
3663 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
3666 obj
= drm_mode_object_find(dev
, page_flip
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3669 crtc
= obj_to_crtc(obj
);
3671 mutex_lock(&crtc
->mutex
);
3672 if (crtc
->fb
== NULL
) {
3673 /* The framebuffer is currently unbound, presumably
3674 * due to a hotplug event, that userspace has not
3681 if (crtc
->funcs
->page_flip
== NULL
)
3684 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
3690 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
3694 if (crtc
->fb
->pixel_format
!= fb
->pixel_format
) {
3695 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3700 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
3702 spin_lock_irqsave(&dev
->event_lock
, flags
);
3703 if (file_priv
->event_space
< sizeof e
->event
) {
3704 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3707 file_priv
->event_space
-= sizeof e
->event
;
3708 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3710 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
3712 spin_lock_irqsave(&dev
->event_lock
, flags
);
3713 file_priv
->event_space
+= sizeof e
->event
;
3714 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3718 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
3719 e
->event
.base
.length
= sizeof e
->event
;
3720 e
->event
.user_data
= page_flip
->user_data
;
3721 e
->base
.event
= &e
->event
.base
;
3722 e
->base
.file_priv
= file_priv
;
3724 (void (*) (struct drm_pending_event
*)) kfree
;
3728 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
3730 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
3731 spin_lock_irqsave(&dev
->event_lock
, flags
);
3732 file_priv
->event_space
+= sizeof e
->event
;
3733 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3736 /* Keep the old fb, don't unref it. */
3740 * Warn if the driver hasn't properly updated the crtc->fb
3741 * field to reflect that the new framebuffer is now used.
3742 * Failing to do so will screw with the reference counting
3745 WARN_ON(crtc
->fb
!= fb
);
3746 /* Unref only the old framebuffer. */
3752 drm_framebuffer_unreference(fb
);
3754 drm_framebuffer_unreference(old_fb
);
3755 mutex_unlock(&crtc
->mutex
);
3760 void drm_mode_config_reset(struct drm_device
*dev
)
3762 struct drm_crtc
*crtc
;
3763 struct drm_encoder
*encoder
;
3764 struct drm_connector
*connector
;
3766 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
3767 if (crtc
->funcs
->reset
)
3768 crtc
->funcs
->reset(crtc
);
3770 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
3771 if (encoder
->funcs
->reset
)
3772 encoder
->funcs
->reset(encoder
);
3774 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
3775 connector
->status
= connector_status_unknown
;
3777 if (connector
->funcs
->reset
)
3778 connector
->funcs
->reset(connector
);
3781 EXPORT_SYMBOL(drm_mode_config_reset
);
3783 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
3784 void *data
, struct drm_file
*file_priv
)
3786 struct drm_mode_create_dumb
*args
= data
;
3787 u32 cpp
, stride
, size
;
3789 if (!dev
->driver
->dumb_create
)
3791 if (!args
->width
|| !args
->height
|| !args
->bpp
)
3794 /* overflow checks for 32bit size calculations */
3795 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
3796 if (cpp
> 0xffffffffU
/ args
->width
)
3798 stride
= cpp
* args
->width
;
3799 if (args
->height
> 0xffffffffU
/ stride
)
3802 /* test for wrap-around */
3803 size
= args
->height
* stride
;
3804 if (PAGE_ALIGN(size
) == 0)
3807 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
3810 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
3811 void *data
, struct drm_file
*file_priv
)
3813 struct drm_mode_map_dumb
*args
= data
;
3815 /* call driver ioctl to get mmap offset */
3816 if (!dev
->driver
->dumb_map_offset
)
3819 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
3822 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
3823 void *data
, struct drm_file
*file_priv
)
3825 struct drm_mode_destroy_dumb
*args
= data
;
3827 if (!dev
->driver
->dumb_destroy
)
3830 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
3834 * Just need to support RGB formats here for compat with code that doesn't
3835 * use pixel formats directly yet.
3837 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
3842 case DRM_FORMAT_RGB332
:
3843 case DRM_FORMAT_BGR233
:
3847 case DRM_FORMAT_XRGB1555
:
3848 case DRM_FORMAT_XBGR1555
:
3849 case DRM_FORMAT_RGBX5551
:
3850 case DRM_FORMAT_BGRX5551
:
3851 case DRM_FORMAT_ARGB1555
:
3852 case DRM_FORMAT_ABGR1555
:
3853 case DRM_FORMAT_RGBA5551
:
3854 case DRM_FORMAT_BGRA5551
:
3858 case DRM_FORMAT_RGB565
:
3859 case DRM_FORMAT_BGR565
:
3863 case DRM_FORMAT_RGB888
:
3864 case DRM_FORMAT_BGR888
:
3868 case DRM_FORMAT_XRGB8888
:
3869 case DRM_FORMAT_XBGR8888
:
3870 case DRM_FORMAT_RGBX8888
:
3871 case DRM_FORMAT_BGRX8888
:
3875 case DRM_FORMAT_XRGB2101010
:
3876 case DRM_FORMAT_XBGR2101010
:
3877 case DRM_FORMAT_RGBX1010102
:
3878 case DRM_FORMAT_BGRX1010102
:
3879 case DRM_FORMAT_ARGB2101010
:
3880 case DRM_FORMAT_ABGR2101010
:
3881 case DRM_FORMAT_RGBA1010102
:
3882 case DRM_FORMAT_BGRA1010102
:
3886 case DRM_FORMAT_ARGB8888
:
3887 case DRM_FORMAT_ABGR8888
:
3888 case DRM_FORMAT_RGBA8888
:
3889 case DRM_FORMAT_BGRA8888
:
3894 DRM_DEBUG_KMS("unsupported pixel format %s\n",
3895 drm_get_format_name(format
));
3901 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
3904 * drm_format_num_planes - get the number of planes for format
3905 * @format: pixel format (DRM_FORMAT_*)
3908 * The number of planes used by the specified pixel format.
3910 int drm_format_num_planes(uint32_t format
)
3913 case DRM_FORMAT_YUV410
:
3914 case DRM_FORMAT_YVU410
:
3915 case DRM_FORMAT_YUV411
:
3916 case DRM_FORMAT_YVU411
:
3917 case DRM_FORMAT_YUV420
:
3918 case DRM_FORMAT_YVU420
:
3919 case DRM_FORMAT_YUV422
:
3920 case DRM_FORMAT_YVU422
:
3921 case DRM_FORMAT_YUV444
:
3922 case DRM_FORMAT_YVU444
:
3924 case DRM_FORMAT_NV12
:
3925 case DRM_FORMAT_NV21
:
3926 case DRM_FORMAT_NV16
:
3927 case DRM_FORMAT_NV61
:
3928 case DRM_FORMAT_NV24
:
3929 case DRM_FORMAT_NV42
:
3935 EXPORT_SYMBOL(drm_format_num_planes
);
3938 * drm_format_plane_cpp - determine the bytes per pixel value
3939 * @format: pixel format (DRM_FORMAT_*)
3940 * @plane: plane index
3943 * The bytes per pixel value for the specified plane.
3945 int drm_format_plane_cpp(uint32_t format
, int plane
)
3950 if (plane
>= drm_format_num_planes(format
))
3954 case DRM_FORMAT_YUYV
:
3955 case DRM_FORMAT_YVYU
:
3956 case DRM_FORMAT_UYVY
:
3957 case DRM_FORMAT_VYUY
:
3959 case DRM_FORMAT_NV12
:
3960 case DRM_FORMAT_NV21
:
3961 case DRM_FORMAT_NV16
:
3962 case DRM_FORMAT_NV61
:
3963 case DRM_FORMAT_NV24
:
3964 case DRM_FORMAT_NV42
:
3965 return plane
? 2 : 1;
3966 case DRM_FORMAT_YUV410
:
3967 case DRM_FORMAT_YVU410
:
3968 case DRM_FORMAT_YUV411
:
3969 case DRM_FORMAT_YVU411
:
3970 case DRM_FORMAT_YUV420
:
3971 case DRM_FORMAT_YVU420
:
3972 case DRM_FORMAT_YUV422
:
3973 case DRM_FORMAT_YVU422
:
3974 case DRM_FORMAT_YUV444
:
3975 case DRM_FORMAT_YVU444
:
3978 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
3982 EXPORT_SYMBOL(drm_format_plane_cpp
);
3985 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3986 * @format: pixel format (DRM_FORMAT_*)
3989 * The horizontal chroma subsampling factor for the
3990 * specified pixel format.
3992 int drm_format_horz_chroma_subsampling(uint32_t format
)
3995 case DRM_FORMAT_YUV411
:
3996 case DRM_FORMAT_YVU411
:
3997 case DRM_FORMAT_YUV410
:
3998 case DRM_FORMAT_YVU410
:
4000 case DRM_FORMAT_YUYV
:
4001 case DRM_FORMAT_YVYU
:
4002 case DRM_FORMAT_UYVY
:
4003 case DRM_FORMAT_VYUY
:
4004 case DRM_FORMAT_NV12
:
4005 case DRM_FORMAT_NV21
:
4006 case DRM_FORMAT_NV16
:
4007 case DRM_FORMAT_NV61
:
4008 case DRM_FORMAT_YUV422
:
4009 case DRM_FORMAT_YVU422
:
4010 case DRM_FORMAT_YUV420
:
4011 case DRM_FORMAT_YVU420
:
4017 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
4020 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4021 * @format: pixel format (DRM_FORMAT_*)
4024 * The vertical chroma subsampling factor for the
4025 * specified pixel format.
4027 int drm_format_vert_chroma_subsampling(uint32_t format
)
4030 case DRM_FORMAT_YUV410
:
4031 case DRM_FORMAT_YVU410
:
4033 case DRM_FORMAT_YUV420
:
4034 case DRM_FORMAT_YVU420
:
4035 case DRM_FORMAT_NV12
:
4036 case DRM_FORMAT_NV21
:
4042 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
4045 * drm_mode_config_init - initialize DRM mode_configuration structure
4048 * Initialize @dev's mode_config structure, used for tracking the graphics
4049 * configuration of @dev.
4051 * Since this initializes the modeset locks, no locking is possible. Which is no
4052 * problem, since this should happen single threaded at init time. It is the
4053 * driver's problem to ensure this guarantee.
4056 void drm_mode_config_init(struct drm_device
*dev
)
4058 mutex_init(&dev
->mode_config
.mutex
);
4059 mutex_init(&dev
->mode_config
.idr_mutex
);
4060 mutex_init(&dev
->mode_config
.fb_lock
);
4061 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
4062 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
4063 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
4064 INIT_LIST_HEAD(&dev
->mode_config
.bridge_list
);
4065 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
4066 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
4067 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
4068 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
4069 idr_init(&dev
->mode_config
.crtc_idr
);
4071 drm_modeset_lock_all(dev
);
4072 drm_mode_create_standard_connector_properties(dev
);
4073 drm_modeset_unlock_all(dev
);
4075 /* Just to be sure */
4076 dev
->mode_config
.num_fb
= 0;
4077 dev
->mode_config
.num_connector
= 0;
4078 dev
->mode_config
.num_crtc
= 0;
4079 dev
->mode_config
.num_encoder
= 0;
4081 EXPORT_SYMBOL(drm_mode_config_init
);
4084 * drm_mode_config_cleanup - free up DRM mode_config info
4087 * Free up all the connectors and CRTCs associated with this DRM device, then
4088 * free up the framebuffers and associated buffer objects.
4090 * Note that since this /should/ happen single-threaded at driver/device
4091 * teardown time, no locking is required. It's the driver's job to ensure that
4092 * this guarantee actually holds true.
4094 * FIXME: cleanup any dangling user buffer objects too
4096 void drm_mode_config_cleanup(struct drm_device
*dev
)
4098 struct drm_connector
*connector
, *ot
;
4099 struct drm_crtc
*crtc
, *ct
;
4100 struct drm_encoder
*encoder
, *enct
;
4101 struct drm_bridge
*bridge
, *brt
;
4102 struct drm_framebuffer
*fb
, *fbt
;
4103 struct drm_property
*property
, *pt
;
4104 struct drm_property_blob
*blob
, *bt
;
4105 struct drm_plane
*plane
, *plt
;
4107 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
4109 encoder
->funcs
->destroy(encoder
);
4112 list_for_each_entry_safe(bridge
, brt
,
4113 &dev
->mode_config
.bridge_list
, head
) {
4114 bridge
->funcs
->destroy(bridge
);
4117 list_for_each_entry_safe(connector
, ot
,
4118 &dev
->mode_config
.connector_list
, head
) {
4119 connector
->funcs
->destroy(connector
);
4122 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
4124 drm_property_destroy(dev
, property
);
4127 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
4129 drm_property_destroy_blob(dev
, blob
);
4133 * Single-threaded teardown context, so it's not required to grab the
4134 * fb_lock to protect against concurrent fb_list access. Contrary, it
4135 * would actually deadlock with the drm_framebuffer_cleanup function.
4137 * Also, if there are any framebuffers left, that's a driver leak now,
4138 * so politely WARN about this.
4140 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
4141 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
4142 drm_framebuffer_remove(fb
);
4145 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
4147 plane
->funcs
->destroy(plane
);
4150 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
4151 crtc
->funcs
->destroy(crtc
);
4154 idr_destroy(&dev
->mode_config
.crtc_idr
);
4156 EXPORT_SYMBOL(drm_mode_config_cleanup
);