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/list.h>
33 #include <linux/slab.h>
34 #include <linux/export.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_edid.h>
38 #include <drm/drm_fourcc.h>
40 /* Avoid boilerplate. I'm tired of typing. */
41 #define DRM_ENUM_NAME_FN(fnname, list) \
42 char *fnname(int val) \
45 for (i = 0; i < ARRAY_SIZE(list); i++) { \
46 if (list[i].type == val) \
47 return list[i].name; \
55 static struct drm_prop_enum_list drm_dpms_enum_list
[] =
56 { { DRM_MODE_DPMS_ON
, "On" },
57 { DRM_MODE_DPMS_STANDBY
, "Standby" },
58 { DRM_MODE_DPMS_SUSPEND
, "Suspend" },
59 { DRM_MODE_DPMS_OFF
, "Off" }
62 DRM_ENUM_NAME_FN(drm_get_dpms_name
, drm_dpms_enum_list
)
67 static struct drm_prop_enum_list drm_scaling_mode_enum_list
[] =
69 { DRM_MODE_SCALE_NONE
, "None" },
70 { DRM_MODE_SCALE_FULLSCREEN
, "Full" },
71 { DRM_MODE_SCALE_CENTER
, "Center" },
72 { DRM_MODE_SCALE_ASPECT
, "Full aspect" },
75 static struct drm_prop_enum_list drm_dithering_mode_enum_list
[] =
77 { DRM_MODE_DITHERING_OFF
, "Off" },
78 { DRM_MODE_DITHERING_ON
, "On" },
79 { DRM_MODE_DITHERING_AUTO
, "Automatic" },
83 * Non-global properties, but "required" for certain connectors.
85 static struct drm_prop_enum_list drm_dvi_i_select_enum_list
[] =
87 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
88 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
89 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
92 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name
, drm_dvi_i_select_enum_list
)
94 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list
[] =
96 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
97 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
98 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
101 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name
,
102 drm_dvi_i_subconnector_enum_list
)
104 static struct drm_prop_enum_list drm_tv_select_enum_list
[] =
106 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
107 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
108 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
113 DRM_ENUM_NAME_FN(drm_get_tv_select_name
, drm_tv_select_enum_list
)
115 static struct drm_prop_enum_list drm_tv_subconnector_enum_list
[] =
117 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
118 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
119 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
124 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name
,
125 drm_tv_subconnector_enum_list
)
127 static struct drm_prop_enum_list drm_dirty_info_enum_list
[] = {
128 { DRM_MODE_DIRTY_OFF
, "Off" },
129 { DRM_MODE_DIRTY_ON
, "On" },
130 { DRM_MODE_DIRTY_ANNOTATE
, "Annotate" },
133 DRM_ENUM_NAME_FN(drm_get_dirty_info_name
,
134 drm_dirty_info_enum_list
)
136 struct drm_conn_prop_enum_list
{
143 * Connector and encoder types.
145 static struct drm_conn_prop_enum_list drm_connector_enum_list
[] =
146 { { DRM_MODE_CONNECTOR_Unknown
, "Unknown", 0 },
147 { DRM_MODE_CONNECTOR_VGA
, "VGA", 0 },
148 { DRM_MODE_CONNECTOR_DVII
, "DVI-I", 0 },
149 { DRM_MODE_CONNECTOR_DVID
, "DVI-D", 0 },
150 { DRM_MODE_CONNECTOR_DVIA
, "DVI-A", 0 },
151 { DRM_MODE_CONNECTOR_Composite
, "Composite", 0 },
152 { DRM_MODE_CONNECTOR_SVIDEO
, "SVIDEO", 0 },
153 { DRM_MODE_CONNECTOR_LVDS
, "LVDS", 0 },
154 { DRM_MODE_CONNECTOR_Component
, "Component", 0 },
155 { DRM_MODE_CONNECTOR_9PinDIN
, "DIN", 0 },
156 { DRM_MODE_CONNECTOR_DisplayPort
, "DP", 0 },
157 { DRM_MODE_CONNECTOR_HDMIA
, "HDMI-A", 0 },
158 { DRM_MODE_CONNECTOR_HDMIB
, "HDMI-B", 0 },
159 { DRM_MODE_CONNECTOR_TV
, "TV", 0 },
160 { DRM_MODE_CONNECTOR_eDP
, "eDP", 0 },
161 { DRM_MODE_CONNECTOR_VIRTUAL
, "Virtual", 0},
164 static struct drm_prop_enum_list drm_encoder_enum_list
[] =
165 { { DRM_MODE_ENCODER_NONE
, "None" },
166 { DRM_MODE_ENCODER_DAC
, "DAC" },
167 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
168 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
169 { DRM_MODE_ENCODER_TVDAC
, "TV" },
170 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
173 char *drm_get_encoder_name(struct drm_encoder
*encoder
)
177 snprintf(buf
, 32, "%s-%d",
178 drm_encoder_enum_list
[encoder
->encoder_type
].name
,
182 EXPORT_SYMBOL(drm_get_encoder_name
);
184 char *drm_get_connector_name(struct drm_connector
*connector
)
188 snprintf(buf
, 32, "%s-%d",
189 drm_connector_enum_list
[connector
->connector_type
].name
,
190 connector
->connector_type_id
);
193 EXPORT_SYMBOL(drm_get_connector_name
);
195 char *drm_get_connector_status_name(enum drm_connector_status status
)
197 if (status
== connector_status_connected
)
199 else if (status
== connector_status_disconnected
)
200 return "disconnected";
206 * drm_mode_object_get - allocate a new identifier
208 * @ptr: object pointer, used to generate unique ID
213 * Create a unique identifier based on @ptr in @dev's identifier space. Used
214 * for tracking modes, CRTCs and connectors.
217 * New unique (relative to other objects in @dev) integer identifier for the
220 static int drm_mode_object_get(struct drm_device
*dev
,
221 struct drm_mode_object
*obj
, uint32_t obj_type
)
227 if (idr_pre_get(&dev
->mode_config
.crtc_idr
, GFP_KERNEL
) == 0) {
228 DRM_ERROR("Ran out memory getting a mode number\n");
232 mutex_lock(&dev
->mode_config
.idr_mutex
);
233 ret
= idr_get_new_above(&dev
->mode_config
.crtc_idr
, obj
, 1, &new_id
);
234 mutex_unlock(&dev
->mode_config
.idr_mutex
);
241 obj
->type
= obj_type
;
246 * drm_mode_object_put - free an identifer
251 * Caller must hold DRM mode_config lock.
253 * Free @id from @dev's unique identifier pool.
255 static void drm_mode_object_put(struct drm_device
*dev
,
256 struct drm_mode_object
*object
)
258 mutex_lock(&dev
->mode_config
.idr_mutex
);
259 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
260 mutex_unlock(&dev
->mode_config
.idr_mutex
);
263 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
264 uint32_t id
, uint32_t type
)
266 struct drm_mode_object
*obj
= NULL
;
268 mutex_lock(&dev
->mode_config
.idr_mutex
);
269 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
270 if (!obj
|| (obj
->type
!= type
) || (obj
->id
!= id
))
272 mutex_unlock(&dev
->mode_config
.idr_mutex
);
276 EXPORT_SYMBOL(drm_mode_object_find
);
279 * drm_framebuffer_init - initialize a framebuffer
283 * Caller must hold mode config lock.
285 * Allocates an ID for the framebuffer's parent mode object, sets its mode
286 * functions & device file and adds it to the master fd list.
289 * Zero on success, error code on failure.
291 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
292 const struct drm_framebuffer_funcs
*funcs
)
296 ret
= drm_mode_object_get(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
);
302 dev
->mode_config
.num_fb
++;
303 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
307 EXPORT_SYMBOL(drm_framebuffer_init
);
310 * drm_framebuffer_cleanup - remove a framebuffer object
311 * @fb: framebuffer to remove
314 * Caller must hold mode config lock.
316 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
317 * it, setting it to NULL.
319 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
321 struct drm_device
*dev
= fb
->dev
;
322 struct drm_crtc
*crtc
;
323 struct drm_plane
*plane
;
324 struct drm_mode_set set
;
327 /* remove from any CRTC */
328 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
329 if (crtc
->fb
== fb
) {
330 /* should turn off the crtc */
331 memset(&set
, 0, sizeof(struct drm_mode_set
));
334 ret
= crtc
->funcs
->set_config(&set
);
336 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
340 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
341 if (plane
->fb
== fb
) {
342 /* should turn off the crtc */
343 ret
= plane
->funcs
->disable_plane(plane
);
345 DRM_ERROR("failed to disable plane with busy fb\n");
346 /* disconnect the plane from the fb and crtc: */
352 drm_mode_object_put(dev
, &fb
->base
);
354 dev
->mode_config
.num_fb
--;
356 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
359 * drm_crtc_init - Initialise a new CRTC object
361 * @crtc: CRTC object to init
362 * @funcs: callbacks for the new CRTC
365 * Takes mode_config lock.
367 * Inits a new object created as base part of an driver crtc object.
370 * Zero on success, error code on failure.
372 int drm_crtc_init(struct drm_device
*dev
, struct drm_crtc
*crtc
,
373 const struct drm_crtc_funcs
*funcs
)
380 mutex_lock(&dev
->mode_config
.mutex
);
382 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
386 crtc
->base
.properties
= &crtc
->properties
;
388 list_add_tail(&crtc
->head
, &dev
->mode_config
.crtc_list
);
389 dev
->mode_config
.num_crtc
++;
392 mutex_unlock(&dev
->mode_config
.mutex
);
396 EXPORT_SYMBOL(drm_crtc_init
);
399 * drm_crtc_cleanup - Cleans up the core crtc usage.
400 * @crtc: CRTC to cleanup
403 * Caller must hold mode config lock.
405 * Cleanup @crtc. Removes from drm modesetting space
406 * does NOT free object, caller does that.
408 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
410 struct drm_device
*dev
= crtc
->dev
;
412 if (crtc
->gamma_store
) {
413 kfree(crtc
->gamma_store
);
414 crtc
->gamma_store
= NULL
;
417 drm_mode_object_put(dev
, &crtc
->base
);
418 list_del(&crtc
->head
);
419 dev
->mode_config
.num_crtc
--;
421 EXPORT_SYMBOL(drm_crtc_cleanup
);
424 * drm_mode_probed_add - add a mode to a connector's probed mode list
425 * @connector: connector the new mode
429 * Caller must hold mode config lock.
431 * Add @mode to @connector's mode list for later use.
433 void drm_mode_probed_add(struct drm_connector
*connector
,
434 struct drm_display_mode
*mode
)
436 list_add(&mode
->head
, &connector
->probed_modes
);
438 EXPORT_SYMBOL(drm_mode_probed_add
);
441 * drm_mode_remove - remove and free a mode
442 * @connector: connector list to modify
443 * @mode: mode to remove
446 * Caller must hold mode config lock.
448 * Remove @mode from @connector's mode list, then free it.
450 void drm_mode_remove(struct drm_connector
*connector
,
451 struct drm_display_mode
*mode
)
453 list_del(&mode
->head
);
454 drm_mode_destroy(connector
->dev
, mode
);
456 EXPORT_SYMBOL(drm_mode_remove
);
459 * drm_connector_init - Init a preallocated connector
461 * @connector: the connector to init
462 * @funcs: callbacks for this connector
463 * @name: user visible name of the connector
466 * Takes mode config lock.
468 * Initialises a preallocated connector. Connectors should be
469 * subclassed as part of driver connector objects.
472 * Zero on success, error code on failure.
474 int drm_connector_init(struct drm_device
*dev
,
475 struct drm_connector
*connector
,
476 const struct drm_connector_funcs
*funcs
,
481 mutex_lock(&dev
->mode_config
.mutex
);
483 ret
= drm_mode_object_get(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
);
487 connector
->base
.properties
= &connector
->properties
;
488 connector
->dev
= dev
;
489 connector
->funcs
= funcs
;
490 connector
->connector_type
= connector_type
;
491 connector
->connector_type_id
=
492 ++drm_connector_enum_list
[connector_type
].count
; /* TODO */
493 INIT_LIST_HEAD(&connector
->user_modes
);
494 INIT_LIST_HEAD(&connector
->probed_modes
);
495 INIT_LIST_HEAD(&connector
->modes
);
496 connector
->edid_blob_ptr
= NULL
;
498 list_add_tail(&connector
->head
, &dev
->mode_config
.connector_list
);
499 dev
->mode_config
.num_connector
++;
501 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
502 drm_connector_attach_property(connector
,
503 dev
->mode_config
.edid_property
,
506 drm_connector_attach_property(connector
,
507 dev
->mode_config
.dpms_property
, 0);
510 mutex_unlock(&dev
->mode_config
.mutex
);
514 EXPORT_SYMBOL(drm_connector_init
);
517 * drm_connector_cleanup - cleans up an initialised connector
518 * @connector: connector to cleanup
521 * Takes mode config lock.
523 * Cleans up the connector but doesn't free the object.
525 void drm_connector_cleanup(struct drm_connector
*connector
)
527 struct drm_device
*dev
= connector
->dev
;
528 struct drm_display_mode
*mode
, *t
;
530 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
531 drm_mode_remove(connector
, mode
);
533 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
534 drm_mode_remove(connector
, mode
);
536 list_for_each_entry_safe(mode
, t
, &connector
->user_modes
, head
)
537 drm_mode_remove(connector
, mode
);
539 mutex_lock(&dev
->mode_config
.mutex
);
540 drm_mode_object_put(dev
, &connector
->base
);
541 list_del(&connector
->head
);
542 dev
->mode_config
.num_connector
--;
543 mutex_unlock(&dev
->mode_config
.mutex
);
545 EXPORT_SYMBOL(drm_connector_cleanup
);
547 void drm_connector_unplug_all(struct drm_device
*dev
)
549 struct drm_connector
*connector
;
551 /* taking the mode config mutex ends up in a clash with sysfs */
552 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
553 drm_sysfs_connector_remove(connector
);
556 EXPORT_SYMBOL(drm_connector_unplug_all
);
558 int drm_encoder_init(struct drm_device
*dev
,
559 struct drm_encoder
*encoder
,
560 const struct drm_encoder_funcs
*funcs
,
565 mutex_lock(&dev
->mode_config
.mutex
);
567 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
572 encoder
->encoder_type
= encoder_type
;
573 encoder
->funcs
= funcs
;
575 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
576 dev
->mode_config
.num_encoder
++;
579 mutex_unlock(&dev
->mode_config
.mutex
);
583 EXPORT_SYMBOL(drm_encoder_init
);
585 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
587 struct drm_device
*dev
= encoder
->dev
;
588 mutex_lock(&dev
->mode_config
.mutex
);
589 drm_mode_object_put(dev
, &encoder
->base
);
590 list_del(&encoder
->head
);
591 dev
->mode_config
.num_encoder
--;
592 mutex_unlock(&dev
->mode_config
.mutex
);
594 EXPORT_SYMBOL(drm_encoder_cleanup
);
596 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
597 unsigned long possible_crtcs
,
598 const struct drm_plane_funcs
*funcs
,
599 const uint32_t *formats
, uint32_t format_count
,
604 mutex_lock(&dev
->mode_config
.mutex
);
606 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
610 plane
->base
.properties
= &plane
->properties
;
612 plane
->funcs
= funcs
;
613 plane
->format_types
= kmalloc(sizeof(uint32_t) * format_count
,
615 if (!plane
->format_types
) {
616 DRM_DEBUG_KMS("out of memory when allocating plane\n");
617 drm_mode_object_put(dev
, &plane
->base
);
622 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
623 plane
->format_count
= format_count
;
624 plane
->possible_crtcs
= possible_crtcs
;
626 /* private planes are not exposed to userspace, but depending on
627 * display hardware, might be convenient to allow sharing programming
628 * for the scanout engine with the crtc implementation.
631 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
632 dev
->mode_config
.num_plane
++;
634 INIT_LIST_HEAD(&plane
->head
);
638 mutex_unlock(&dev
->mode_config
.mutex
);
642 EXPORT_SYMBOL(drm_plane_init
);
644 void drm_plane_cleanup(struct drm_plane
*plane
)
646 struct drm_device
*dev
= plane
->dev
;
648 mutex_lock(&dev
->mode_config
.mutex
);
649 kfree(plane
->format_types
);
650 drm_mode_object_put(dev
, &plane
->base
);
651 /* if not added to a list, it must be a private plane */
652 if (!list_empty(&plane
->head
)) {
653 list_del(&plane
->head
);
654 dev
->mode_config
.num_plane
--;
656 mutex_unlock(&dev
->mode_config
.mutex
);
658 EXPORT_SYMBOL(drm_plane_cleanup
);
661 * drm_mode_create - create a new display mode
665 * Caller must hold DRM mode_config lock.
667 * Create a new drm_display_mode, give it an ID, and return it.
670 * Pointer to new mode on success, NULL on error.
672 struct drm_display_mode
*drm_mode_create(struct drm_device
*dev
)
674 struct drm_display_mode
*nmode
;
676 nmode
= kzalloc(sizeof(struct drm_display_mode
), GFP_KERNEL
);
680 if (drm_mode_object_get(dev
, &nmode
->base
, DRM_MODE_OBJECT_MODE
)) {
687 EXPORT_SYMBOL(drm_mode_create
);
690 * drm_mode_destroy - remove a mode
692 * @mode: mode to remove
695 * Caller must hold mode config lock.
697 * Free @mode's unique identifier, then free it.
699 void drm_mode_destroy(struct drm_device
*dev
, struct drm_display_mode
*mode
)
704 drm_mode_object_put(dev
, &mode
->base
);
708 EXPORT_SYMBOL(drm_mode_destroy
);
710 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
712 struct drm_property
*edid
;
713 struct drm_property
*dpms
;
716 * Standard properties (apply to all connectors)
718 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
719 DRM_MODE_PROP_IMMUTABLE
,
721 dev
->mode_config
.edid_property
= edid
;
723 dpms
= drm_property_create_enum(dev
, 0,
724 "DPMS", drm_dpms_enum_list
,
725 ARRAY_SIZE(drm_dpms_enum_list
));
726 dev
->mode_config
.dpms_property
= dpms
;
732 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
735 * Called by a driver the first time a DVI-I connector is made.
737 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
739 struct drm_property
*dvi_i_selector
;
740 struct drm_property
*dvi_i_subconnector
;
742 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
746 drm_property_create_enum(dev
, 0,
747 "select subconnector",
748 drm_dvi_i_select_enum_list
,
749 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
750 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
752 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
754 drm_dvi_i_subconnector_enum_list
,
755 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
756 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
760 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
763 * drm_create_tv_properties - create TV specific connector properties
765 * @num_modes: number of different TV formats (modes) supported
766 * @modes: array of pointers to strings containing name of each format
768 * Called by a driver's TV initialization routine, this function creates
769 * the TV specific connector properties for a given device. Caller is
770 * responsible for allocating a list of format names and passing them to
773 int drm_mode_create_tv_properties(struct drm_device
*dev
, int num_modes
,
776 struct drm_property
*tv_selector
;
777 struct drm_property
*tv_subconnector
;
780 if (dev
->mode_config
.tv_select_subconnector_property
)
784 * Basic connector properties
786 tv_selector
= drm_property_create_enum(dev
, 0,
787 "select subconnector",
788 drm_tv_select_enum_list
,
789 ARRAY_SIZE(drm_tv_select_enum_list
));
790 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
793 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
795 drm_tv_subconnector_enum_list
,
796 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
797 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
800 * Other, TV specific properties: margins & TV modes.
802 dev
->mode_config
.tv_left_margin_property
=
803 drm_property_create_range(dev
, 0, "left margin", 0, 100);
805 dev
->mode_config
.tv_right_margin_property
=
806 drm_property_create_range(dev
, 0, "right margin", 0, 100);
808 dev
->mode_config
.tv_top_margin_property
=
809 drm_property_create_range(dev
, 0, "top margin", 0, 100);
811 dev
->mode_config
.tv_bottom_margin_property
=
812 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
814 dev
->mode_config
.tv_mode_property
=
815 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
817 for (i
= 0; i
< num_modes
; i
++)
818 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
821 dev
->mode_config
.tv_brightness_property
=
822 drm_property_create_range(dev
, 0, "brightness", 0, 100);
824 dev
->mode_config
.tv_contrast_property
=
825 drm_property_create_range(dev
, 0, "contrast", 0, 100);
827 dev
->mode_config
.tv_flicker_reduction_property
=
828 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
830 dev
->mode_config
.tv_overscan_property
=
831 drm_property_create_range(dev
, 0, "overscan", 0, 100);
833 dev
->mode_config
.tv_saturation_property
=
834 drm_property_create_range(dev
, 0, "saturation", 0, 100);
836 dev
->mode_config
.tv_hue_property
=
837 drm_property_create_range(dev
, 0, "hue", 0, 100);
841 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
844 * drm_mode_create_scaling_mode_property - create scaling mode property
847 * Called by a driver the first time it's needed, must be attached to desired
850 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
852 struct drm_property
*scaling_mode
;
854 if (dev
->mode_config
.scaling_mode_property
)
858 drm_property_create_enum(dev
, 0, "scaling mode",
859 drm_scaling_mode_enum_list
,
860 ARRAY_SIZE(drm_scaling_mode_enum_list
));
862 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
866 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
869 * drm_mode_create_dithering_property - create dithering property
872 * Called by a driver the first time it's needed, must be attached to desired
875 int drm_mode_create_dithering_property(struct drm_device
*dev
)
877 struct drm_property
*dithering_mode
;
879 if (dev
->mode_config
.dithering_mode_property
)
883 drm_property_create_enum(dev
, 0, "dithering",
884 drm_dithering_mode_enum_list
,
885 ARRAY_SIZE(drm_dithering_mode_enum_list
));
886 dev
->mode_config
.dithering_mode_property
= dithering_mode
;
890 EXPORT_SYMBOL(drm_mode_create_dithering_property
);
893 * drm_mode_create_dirty_property - create dirty property
896 * Called by a driver the first time it's needed, must be attached to desired
899 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
901 struct drm_property
*dirty_info
;
903 if (dev
->mode_config
.dirty_info_property
)
907 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
909 drm_dirty_info_enum_list
,
910 ARRAY_SIZE(drm_dirty_info_enum_list
));
911 dev
->mode_config
.dirty_info_property
= dirty_info
;
915 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
918 * drm_mode_config_init - initialize DRM mode_configuration structure
922 * None, should happen single threaded at init time.
924 * Initialize @dev's mode_config structure, used for tracking the graphics
925 * configuration of @dev.
927 void drm_mode_config_init(struct drm_device
*dev
)
929 mutex_init(&dev
->mode_config
.mutex
);
930 mutex_init(&dev
->mode_config
.idr_mutex
);
931 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
932 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
933 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
934 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
935 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
936 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
937 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
938 idr_init(&dev
->mode_config
.crtc_idr
);
940 mutex_lock(&dev
->mode_config
.mutex
);
941 drm_mode_create_standard_connector_properties(dev
);
942 mutex_unlock(&dev
->mode_config
.mutex
);
944 /* Just to be sure */
945 dev
->mode_config
.num_fb
= 0;
946 dev
->mode_config
.num_connector
= 0;
947 dev
->mode_config
.num_crtc
= 0;
948 dev
->mode_config
.num_encoder
= 0;
950 EXPORT_SYMBOL(drm_mode_config_init
);
952 int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
954 uint32_t total_objects
= 0;
956 total_objects
+= dev
->mode_config
.num_crtc
;
957 total_objects
+= dev
->mode_config
.num_connector
;
958 total_objects
+= dev
->mode_config
.num_encoder
;
960 group
->id_list
= kzalloc(total_objects
* sizeof(uint32_t), GFP_KERNEL
);
964 group
->num_crtcs
= 0;
965 group
->num_connectors
= 0;
966 group
->num_encoders
= 0;
970 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
971 struct drm_mode_group
*group
)
973 struct drm_crtc
*crtc
;
974 struct drm_encoder
*encoder
;
975 struct drm_connector
*connector
;
978 if ((ret
= drm_mode_group_init(dev
, group
)))
981 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
982 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
984 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
985 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
988 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
989 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
990 group
->num_connectors
++] = connector
->base
.id
;
994 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
997 * drm_mode_config_cleanup - free up DRM mode_config info
1001 * Caller must hold mode config lock.
1003 * Free up all the connectors and CRTCs associated with this DRM device, then
1004 * free up the framebuffers and associated buffer objects.
1006 * FIXME: cleanup any dangling user buffer objects too
1008 void drm_mode_config_cleanup(struct drm_device
*dev
)
1010 struct drm_connector
*connector
, *ot
;
1011 struct drm_crtc
*crtc
, *ct
;
1012 struct drm_encoder
*encoder
, *enct
;
1013 struct drm_framebuffer
*fb
, *fbt
;
1014 struct drm_property
*property
, *pt
;
1015 struct drm_plane
*plane
, *plt
;
1017 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
1019 encoder
->funcs
->destroy(encoder
);
1022 list_for_each_entry_safe(connector
, ot
,
1023 &dev
->mode_config
.connector_list
, head
) {
1024 connector
->funcs
->destroy(connector
);
1027 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
1029 drm_property_destroy(dev
, property
);
1032 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
1033 fb
->funcs
->destroy(fb
);
1036 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
1037 crtc
->funcs
->destroy(crtc
);
1040 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
1042 plane
->funcs
->destroy(plane
);
1045 idr_remove_all(&dev
->mode_config
.crtc_idr
);
1046 idr_destroy(&dev
->mode_config
.crtc_idr
);
1048 EXPORT_SYMBOL(drm_mode_config_cleanup
);
1051 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1052 * @out: drm_mode_modeinfo struct to return to the user
1053 * @in: drm_display_mode to use
1058 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1061 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1062 const struct drm_display_mode
*in
)
1064 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1065 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1066 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1067 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1068 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1069 "timing values too large for mode info\n");
1071 out
->clock
= in
->clock
;
1072 out
->hdisplay
= in
->hdisplay
;
1073 out
->hsync_start
= in
->hsync_start
;
1074 out
->hsync_end
= in
->hsync_end
;
1075 out
->htotal
= in
->htotal
;
1076 out
->hskew
= in
->hskew
;
1077 out
->vdisplay
= in
->vdisplay
;
1078 out
->vsync_start
= in
->vsync_start
;
1079 out
->vsync_end
= in
->vsync_end
;
1080 out
->vtotal
= in
->vtotal
;
1081 out
->vscan
= in
->vscan
;
1082 out
->vrefresh
= in
->vrefresh
;
1083 out
->flags
= in
->flags
;
1084 out
->type
= in
->type
;
1085 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1086 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1090 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1091 * @out: drm_display_mode to return to the user
1092 * @in: drm_mode_modeinfo to use
1097 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1101 * Zero on success, errno on failure.
1103 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1104 const struct drm_mode_modeinfo
*in
)
1106 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1109 out
->clock
= in
->clock
;
1110 out
->hdisplay
= in
->hdisplay
;
1111 out
->hsync_start
= in
->hsync_start
;
1112 out
->hsync_end
= in
->hsync_end
;
1113 out
->htotal
= in
->htotal
;
1114 out
->hskew
= in
->hskew
;
1115 out
->vdisplay
= in
->vdisplay
;
1116 out
->vsync_start
= in
->vsync_start
;
1117 out
->vsync_end
= in
->vsync_end
;
1118 out
->vtotal
= in
->vtotal
;
1119 out
->vscan
= in
->vscan
;
1120 out
->vrefresh
= in
->vrefresh
;
1121 out
->flags
= in
->flags
;
1122 out
->type
= in
->type
;
1123 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1124 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1130 * drm_mode_getresources - get graphics configuration
1131 * @inode: inode from the ioctl
1132 * @filp: file * from the ioctl
1133 * @cmd: cmd from ioctl
1134 * @arg: arg from ioctl
1137 * Takes mode config lock.
1139 * Construct a set of configuration description structures and return
1140 * them to the user, including CRTC, connector and framebuffer configuration.
1142 * Called by the user via ioctl.
1145 * Zero on success, errno on failure.
1147 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1148 struct drm_file
*file_priv
)
1150 struct drm_mode_card_res
*card_res
= data
;
1151 struct list_head
*lh
;
1152 struct drm_framebuffer
*fb
;
1153 struct drm_connector
*connector
;
1154 struct drm_crtc
*crtc
;
1155 struct drm_encoder
*encoder
;
1157 int connector_count
= 0;
1160 int encoder_count
= 0;
1162 uint32_t __user
*fb_id
;
1163 uint32_t __user
*crtc_id
;
1164 uint32_t __user
*connector_id
;
1165 uint32_t __user
*encoder_id
;
1166 struct drm_mode_group
*mode_group
;
1168 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1171 mutex_lock(&dev
->mode_config
.mutex
);
1174 * For the non-control nodes we need to limit the list of resources
1175 * by IDs in the group list for this node
1177 list_for_each(lh
, &file_priv
->fbs
)
1180 mode_group
= &file_priv
->master
->minor
->mode_group
;
1181 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1183 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1186 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1189 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1193 crtc_count
= mode_group
->num_crtcs
;
1194 connector_count
= mode_group
->num_connectors
;
1195 encoder_count
= mode_group
->num_encoders
;
1198 card_res
->max_height
= dev
->mode_config
.max_height
;
1199 card_res
->min_height
= dev
->mode_config
.min_height
;
1200 card_res
->max_width
= dev
->mode_config
.max_width
;
1201 card_res
->min_width
= dev
->mode_config
.min_width
;
1203 /* handle this in 4 parts */
1205 if (card_res
->count_fbs
>= fb_count
) {
1207 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1208 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1209 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1216 card_res
->count_fbs
= fb_count
;
1219 if (card_res
->count_crtcs
>= crtc_count
) {
1221 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1222 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1223 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1225 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1226 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1233 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1234 if (put_user(mode_group
->id_list
[i
],
1235 crtc_id
+ copied
)) {
1243 card_res
->count_crtcs
= crtc_count
;
1246 if (card_res
->count_encoders
>= encoder_count
) {
1248 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1249 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1250 list_for_each_entry(encoder
,
1251 &dev
->mode_config
.encoder_list
,
1253 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1254 drm_get_encoder_name(encoder
));
1255 if (put_user(encoder
->base
.id
, encoder_id
+
1263 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1264 if (put_user(mode_group
->id_list
[i
],
1265 encoder_id
+ copied
)) {
1274 card_res
->count_encoders
= encoder_count
;
1277 if (card_res
->count_connectors
>= connector_count
) {
1279 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1280 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1281 list_for_each_entry(connector
,
1282 &dev
->mode_config
.connector_list
,
1284 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1286 drm_get_connector_name(connector
));
1287 if (put_user(connector
->base
.id
,
1288 connector_id
+ copied
)) {
1295 int start
= mode_group
->num_crtcs
+
1296 mode_group
->num_encoders
;
1297 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1298 if (put_user(mode_group
->id_list
[i
],
1299 connector_id
+ copied
)) {
1307 card_res
->count_connectors
= connector_count
;
1309 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1310 card_res
->count_connectors
, card_res
->count_encoders
);
1313 mutex_unlock(&dev
->mode_config
.mutex
);
1318 * drm_mode_getcrtc - get CRTC configuration
1319 * @inode: inode from the ioctl
1320 * @filp: file * from the ioctl
1321 * @cmd: cmd from ioctl
1322 * @arg: arg from ioctl
1325 * Takes mode config lock.
1327 * Construct a CRTC configuration structure to return to the user.
1329 * Called by the user via ioctl.
1332 * Zero on success, errno on failure.
1334 int drm_mode_getcrtc(struct drm_device
*dev
,
1335 void *data
, struct drm_file
*file_priv
)
1337 struct drm_mode_crtc
*crtc_resp
= data
;
1338 struct drm_crtc
*crtc
;
1339 struct drm_mode_object
*obj
;
1342 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1345 mutex_lock(&dev
->mode_config
.mutex
);
1347 obj
= drm_mode_object_find(dev
, crtc_resp
->crtc_id
,
1348 DRM_MODE_OBJECT_CRTC
);
1353 crtc
= obj_to_crtc(obj
);
1355 crtc_resp
->x
= crtc
->x
;
1356 crtc_resp
->y
= crtc
->y
;
1357 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1359 crtc_resp
->fb_id
= crtc
->fb
->base
.id
;
1361 crtc_resp
->fb_id
= 0;
1363 if (crtc
->enabled
) {
1365 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1366 crtc_resp
->mode_valid
= 1;
1369 crtc_resp
->mode_valid
= 0;
1373 mutex_unlock(&dev
->mode_config
.mutex
);
1378 * drm_mode_getconnector - get connector configuration
1379 * @inode: inode from the ioctl
1380 * @filp: file * from the ioctl
1381 * @cmd: cmd from ioctl
1382 * @arg: arg from ioctl
1385 * Takes mode config lock.
1387 * Construct a connector configuration structure to return to the user.
1389 * Called by the user via ioctl.
1392 * Zero on success, errno on failure.
1394 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
1395 struct drm_file
*file_priv
)
1397 struct drm_mode_get_connector
*out_resp
= data
;
1398 struct drm_mode_object
*obj
;
1399 struct drm_connector
*connector
;
1400 struct drm_display_mode
*mode
;
1402 int props_count
= 0;
1403 int encoders_count
= 0;
1407 struct drm_mode_modeinfo u_mode
;
1408 struct drm_mode_modeinfo __user
*mode_ptr
;
1409 uint32_t __user
*prop_ptr
;
1410 uint64_t __user
*prop_values
;
1411 uint32_t __user
*encoder_ptr
;
1413 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1416 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
1418 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
1420 mutex_lock(&dev
->mode_config
.mutex
);
1422 obj
= drm_mode_object_find(dev
, out_resp
->connector_id
,
1423 DRM_MODE_OBJECT_CONNECTOR
);
1428 connector
= obj_to_connector(obj
);
1430 props_count
= connector
->properties
.count
;
1432 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1433 if (connector
->encoder_ids
[i
] != 0) {
1438 if (out_resp
->count_modes
== 0) {
1439 connector
->funcs
->fill_modes(connector
,
1440 dev
->mode_config
.max_width
,
1441 dev
->mode_config
.max_height
);
1444 /* delayed so we get modes regardless of pre-fill_modes state */
1445 list_for_each_entry(mode
, &connector
->modes
, head
)
1448 out_resp
->connector_id
= connector
->base
.id
;
1449 out_resp
->connector_type
= connector
->connector_type
;
1450 out_resp
->connector_type_id
= connector
->connector_type_id
;
1451 out_resp
->mm_width
= connector
->display_info
.width_mm
;
1452 out_resp
->mm_height
= connector
->display_info
.height_mm
;
1453 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
1454 out_resp
->connection
= connector
->status
;
1455 if (connector
->encoder
)
1456 out_resp
->encoder_id
= connector
->encoder
->base
.id
;
1458 out_resp
->encoder_id
= 0;
1461 * This ioctl is called twice, once to determine how much space is
1462 * needed, and the 2nd time to fill it.
1464 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
1466 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
1467 list_for_each_entry(mode
, &connector
->modes
, head
) {
1468 drm_crtc_convert_to_umode(&u_mode
, mode
);
1469 if (copy_to_user(mode_ptr
+ copied
,
1470 &u_mode
, sizeof(u_mode
))) {
1477 out_resp
->count_modes
= mode_count
;
1479 if ((out_resp
->count_props
>= props_count
) && props_count
) {
1481 prop_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
);
1482 prop_values
= (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
);
1483 for (i
= 0; i
< connector
->properties
.count
; i
++) {
1484 if (put_user(connector
->properties
.ids
[i
],
1485 prop_ptr
+ copied
)) {
1490 if (put_user(connector
->properties
.values
[i
],
1491 prop_values
+ copied
)) {
1498 out_resp
->count_props
= props_count
;
1500 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
1502 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
1503 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1504 if (connector
->encoder_ids
[i
] != 0) {
1505 if (put_user(connector
->encoder_ids
[i
],
1506 encoder_ptr
+ copied
)) {
1514 out_resp
->count_encoders
= encoders_count
;
1517 mutex_unlock(&dev
->mode_config
.mutex
);
1521 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
1522 struct drm_file
*file_priv
)
1524 struct drm_mode_get_encoder
*enc_resp
= data
;
1525 struct drm_mode_object
*obj
;
1526 struct drm_encoder
*encoder
;
1529 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1532 mutex_lock(&dev
->mode_config
.mutex
);
1533 obj
= drm_mode_object_find(dev
, enc_resp
->encoder_id
,
1534 DRM_MODE_OBJECT_ENCODER
);
1539 encoder
= obj_to_encoder(obj
);
1542 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
1544 enc_resp
->crtc_id
= 0;
1545 enc_resp
->encoder_type
= encoder
->encoder_type
;
1546 enc_resp
->encoder_id
= encoder
->base
.id
;
1547 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
1548 enc_resp
->possible_clones
= encoder
->possible_clones
;
1551 mutex_unlock(&dev
->mode_config
.mutex
);
1556 * drm_mode_getplane_res - get plane info
1559 * @file_priv: DRM file info
1562 * Takes mode config lock.
1564 * Return an plane count and set of IDs.
1566 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
1567 struct drm_file
*file_priv
)
1569 struct drm_mode_get_plane_res
*plane_resp
= data
;
1570 struct drm_mode_config
*config
;
1571 struct drm_plane
*plane
;
1572 uint32_t __user
*plane_ptr
;
1573 int copied
= 0, ret
= 0;
1575 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1578 mutex_lock(&dev
->mode_config
.mutex
);
1579 config
= &dev
->mode_config
;
1582 * This ioctl is called twice, once to determine how much space is
1583 * needed, and the 2nd time to fill it.
1585 if (config
->num_plane
&&
1586 (plane_resp
->count_planes
>= config
->num_plane
)) {
1587 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
1589 list_for_each_entry(plane
, &config
->plane_list
, head
) {
1590 if (put_user(plane
->base
.id
, plane_ptr
+ copied
)) {
1597 plane_resp
->count_planes
= config
->num_plane
;
1600 mutex_unlock(&dev
->mode_config
.mutex
);
1605 * drm_mode_getplane - get plane info
1608 * @file_priv: DRM file info
1611 * Takes mode config lock.
1613 * Return plane info, including formats supported, gamma size, any
1616 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
1617 struct drm_file
*file_priv
)
1619 struct drm_mode_get_plane
*plane_resp
= data
;
1620 struct drm_mode_object
*obj
;
1621 struct drm_plane
*plane
;
1622 uint32_t __user
*format_ptr
;
1625 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1628 mutex_lock(&dev
->mode_config
.mutex
);
1629 obj
= drm_mode_object_find(dev
, plane_resp
->plane_id
,
1630 DRM_MODE_OBJECT_PLANE
);
1635 plane
= obj_to_plane(obj
);
1638 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
1640 plane_resp
->crtc_id
= 0;
1643 plane_resp
->fb_id
= plane
->fb
->base
.id
;
1645 plane_resp
->fb_id
= 0;
1647 plane_resp
->plane_id
= plane
->base
.id
;
1648 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
1649 plane_resp
->gamma_size
= plane
->gamma_size
;
1652 * This ioctl is called twice, once to determine how much space is
1653 * needed, and the 2nd time to fill it.
1655 if (plane
->format_count
&&
1656 (plane_resp
->count_format_types
>= plane
->format_count
)) {
1657 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
1658 if (copy_to_user(format_ptr
,
1659 plane
->format_types
,
1660 sizeof(uint32_t) * plane
->format_count
)) {
1665 plane_resp
->count_format_types
= plane
->format_count
;
1668 mutex_unlock(&dev
->mode_config
.mutex
);
1673 * drm_mode_setplane - set up or tear down an plane
1675 * @data: ioctl data*
1676 * @file_prive: DRM file info
1679 * Takes mode config lock.
1681 * Set plane info, including placement, fb, scaling, and other factors.
1682 * Or pass a NULL fb to disable.
1684 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
1685 struct drm_file
*file_priv
)
1687 struct drm_mode_set_plane
*plane_req
= data
;
1688 struct drm_mode_object
*obj
;
1689 struct drm_plane
*plane
;
1690 struct drm_crtc
*crtc
;
1691 struct drm_framebuffer
*fb
;
1693 unsigned int fb_width
, fb_height
;
1696 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1699 mutex_lock(&dev
->mode_config
.mutex
);
1702 * First, find the plane, crtc, and fb objects. If not available,
1703 * we don't bother to call the driver.
1705 obj
= drm_mode_object_find(dev
, plane_req
->plane_id
,
1706 DRM_MODE_OBJECT_PLANE
);
1708 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1709 plane_req
->plane_id
);
1713 plane
= obj_to_plane(obj
);
1715 /* No fb means shut it down */
1716 if (!plane_req
->fb_id
) {
1717 plane
->funcs
->disable_plane(plane
);
1723 obj
= drm_mode_object_find(dev
, plane_req
->crtc_id
,
1724 DRM_MODE_OBJECT_CRTC
);
1726 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1727 plane_req
->crtc_id
);
1731 crtc
= obj_to_crtc(obj
);
1733 obj
= drm_mode_object_find(dev
, plane_req
->fb_id
,
1734 DRM_MODE_OBJECT_FB
);
1736 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1741 fb
= obj_to_fb(obj
);
1743 /* Check whether this plane supports the fb pixel format. */
1744 for (i
= 0; i
< plane
->format_count
; i
++)
1745 if (fb
->pixel_format
== plane
->format_types
[i
])
1747 if (i
== plane
->format_count
) {
1748 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb
->pixel_format
);
1753 fb_width
= fb
->width
<< 16;
1754 fb_height
= fb
->height
<< 16;
1756 /* Make sure source coordinates are inside the fb. */
1757 if (plane_req
->src_w
> fb_width
||
1758 plane_req
->src_x
> fb_width
- plane_req
->src_w
||
1759 plane_req
->src_h
> fb_height
||
1760 plane_req
->src_y
> fb_height
- plane_req
->src_h
) {
1761 DRM_DEBUG_KMS("Invalid source coordinates "
1762 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1763 plane_req
->src_w
>> 16,
1764 ((plane_req
->src_w
& 0xffff) * 15625) >> 10,
1765 plane_req
->src_h
>> 16,
1766 ((plane_req
->src_h
& 0xffff) * 15625) >> 10,
1767 plane_req
->src_x
>> 16,
1768 ((plane_req
->src_x
& 0xffff) * 15625) >> 10,
1769 plane_req
->src_y
>> 16,
1770 ((plane_req
->src_y
& 0xffff) * 15625) >> 10);
1775 /* Give drivers some help against integer overflows */
1776 if (plane_req
->crtc_w
> INT_MAX
||
1777 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
1778 plane_req
->crtc_h
> INT_MAX
||
1779 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
1780 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1781 plane_req
->crtc_w
, plane_req
->crtc_h
,
1782 plane_req
->crtc_x
, plane_req
->crtc_y
);
1787 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
1788 plane_req
->crtc_x
, plane_req
->crtc_y
,
1789 plane_req
->crtc_w
, plane_req
->crtc_h
,
1790 plane_req
->src_x
, plane_req
->src_y
,
1791 plane_req
->src_w
, plane_req
->src_h
);
1798 mutex_unlock(&dev
->mode_config
.mutex
);
1804 * drm_mode_setcrtc - set CRTC configuration
1805 * @inode: inode from the ioctl
1806 * @filp: file * from the ioctl
1807 * @cmd: cmd from ioctl
1808 * @arg: arg from ioctl
1811 * Takes mode config lock.
1813 * Build a new CRTC configuration based on user request.
1815 * Called by the user via ioctl.
1818 * Zero on success, errno on failure.
1820 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
1821 struct drm_file
*file_priv
)
1823 struct drm_mode_config
*config
= &dev
->mode_config
;
1824 struct drm_mode_crtc
*crtc_req
= data
;
1825 struct drm_mode_object
*obj
;
1826 struct drm_crtc
*crtc
;
1827 struct drm_connector
**connector_set
= NULL
, *connector
;
1828 struct drm_framebuffer
*fb
= NULL
;
1829 struct drm_display_mode
*mode
= NULL
;
1830 struct drm_mode_set set
;
1831 uint32_t __user
*set_connectors_ptr
;
1835 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1838 /* For some reason crtc x/y offsets are signed internally. */
1839 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
1842 mutex_lock(&dev
->mode_config
.mutex
);
1843 obj
= drm_mode_object_find(dev
, crtc_req
->crtc_id
,
1844 DRM_MODE_OBJECT_CRTC
);
1846 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
1850 crtc
= obj_to_crtc(obj
);
1851 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1853 if (crtc_req
->mode_valid
) {
1854 /* If we have a mode we need a framebuffer. */
1855 /* If we pass -1, set the mode with the currently bound fb */
1856 if (crtc_req
->fb_id
== -1) {
1858 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1864 obj
= drm_mode_object_find(dev
, crtc_req
->fb_id
,
1865 DRM_MODE_OBJECT_FB
);
1867 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1872 fb
= obj_to_fb(obj
);
1875 mode
= drm_mode_create(dev
);
1881 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
1883 DRM_DEBUG_KMS("Invalid mode\n");
1887 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
1889 if (mode
->hdisplay
> fb
->width
||
1890 mode
->vdisplay
> fb
->height
||
1891 crtc_req
->x
> fb
->width
- mode
->hdisplay
||
1892 crtc_req
->y
> fb
->height
- mode
->vdisplay
) {
1893 DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
1894 mode
->hdisplay
, mode
->vdisplay
,
1895 crtc_req
->x
, crtc_req
->y
,
1896 fb
->width
, fb
->height
);
1902 if (crtc_req
->count_connectors
== 0 && mode
) {
1903 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1908 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
1909 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1910 crtc_req
->count_connectors
);
1915 if (crtc_req
->count_connectors
> 0) {
1918 /* Avoid unbounded kernel memory allocation */
1919 if (crtc_req
->count_connectors
> config
->num_connector
) {
1924 connector_set
= kmalloc(crtc_req
->count_connectors
*
1925 sizeof(struct drm_connector
*),
1927 if (!connector_set
) {
1932 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
1933 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
1934 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
1939 obj
= drm_mode_object_find(dev
, out_id
,
1940 DRM_MODE_OBJECT_CONNECTOR
);
1942 DRM_DEBUG_KMS("Connector id %d unknown\n",
1947 connector
= obj_to_connector(obj
);
1948 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1950 drm_get_connector_name(connector
));
1952 connector_set
[i
] = connector
;
1957 set
.x
= crtc_req
->x
;
1958 set
.y
= crtc_req
->y
;
1960 set
.connectors
= connector_set
;
1961 set
.num_connectors
= crtc_req
->count_connectors
;
1963 ret
= crtc
->funcs
->set_config(&set
);
1966 kfree(connector_set
);
1967 drm_mode_destroy(dev
, mode
);
1968 mutex_unlock(&dev
->mode_config
.mutex
);
1972 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
1973 void *data
, struct drm_file
*file_priv
)
1975 struct drm_mode_cursor
*req
= data
;
1976 struct drm_mode_object
*obj
;
1977 struct drm_crtc
*crtc
;
1980 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1983 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
1986 mutex_lock(&dev
->mode_config
.mutex
);
1987 obj
= drm_mode_object_find(dev
, req
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
1989 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
1993 crtc
= obj_to_crtc(obj
);
1995 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
1996 if (!crtc
->funcs
->cursor_set
) {
2000 /* Turns off the cursor if handle is 0 */
2001 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2002 req
->width
, req
->height
);
2005 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2006 if (crtc
->funcs
->cursor_move
) {
2007 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2014 mutex_unlock(&dev
->mode_config
.mutex
);
2018 /* Original addfb only supported RGB formats, so figure out which one */
2019 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2025 fmt
= DRM_FORMAT_RGB332
;
2029 fmt
= DRM_FORMAT_XRGB1555
;
2031 fmt
= DRM_FORMAT_RGB565
;
2034 fmt
= DRM_FORMAT_RGB888
;
2038 fmt
= DRM_FORMAT_XRGB8888
;
2039 else if (depth
== 30)
2040 fmt
= DRM_FORMAT_XRGB2101010
;
2042 fmt
= DRM_FORMAT_ARGB8888
;
2045 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2046 fmt
= DRM_FORMAT_XRGB8888
;
2052 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2055 * drm_mode_addfb - add an FB to the graphics configuration
2056 * @inode: inode from the ioctl
2057 * @filp: file * from the ioctl
2058 * @cmd: cmd from ioctl
2059 * @arg: arg from ioctl
2062 * Takes mode config lock.
2064 * Add a new FB to the specified CRTC, given a user request.
2066 * Called by the user via ioctl.
2069 * Zero on success, errno on failure.
2071 int drm_mode_addfb(struct drm_device
*dev
,
2072 void *data
, struct drm_file
*file_priv
)
2074 struct drm_mode_fb_cmd
*or = data
;
2075 struct drm_mode_fb_cmd2 r
= {};
2076 struct drm_mode_config
*config
= &dev
->mode_config
;
2077 struct drm_framebuffer
*fb
;
2080 /* Use new struct with format internally */
2081 r
.fb_id
= or->fb_id
;
2082 r
.width
= or->width
;
2083 r
.height
= or->height
;
2084 r
.pitches
[0] = or->pitch
;
2085 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2086 r
.handles
[0] = or->handle
;
2088 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2091 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2094 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2097 mutex_lock(&dev
->mode_config
.mutex
);
2099 /* TODO check buffer is sufficiently large */
2100 /* TODO setup destructor callback */
2102 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2104 DRM_DEBUG_KMS("could not create framebuffer\n");
2109 or->fb_id
= fb
->base
.id
;
2110 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2111 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2114 mutex_unlock(&dev
->mode_config
.mutex
);
2118 static int format_check(const struct drm_mode_fb_cmd2
*r
)
2120 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2124 case DRM_FORMAT_RGB332
:
2125 case DRM_FORMAT_BGR233
:
2126 case DRM_FORMAT_XRGB4444
:
2127 case DRM_FORMAT_XBGR4444
:
2128 case DRM_FORMAT_RGBX4444
:
2129 case DRM_FORMAT_BGRX4444
:
2130 case DRM_FORMAT_ARGB4444
:
2131 case DRM_FORMAT_ABGR4444
:
2132 case DRM_FORMAT_RGBA4444
:
2133 case DRM_FORMAT_BGRA4444
:
2134 case DRM_FORMAT_XRGB1555
:
2135 case DRM_FORMAT_XBGR1555
:
2136 case DRM_FORMAT_RGBX5551
:
2137 case DRM_FORMAT_BGRX5551
:
2138 case DRM_FORMAT_ARGB1555
:
2139 case DRM_FORMAT_ABGR1555
:
2140 case DRM_FORMAT_RGBA5551
:
2141 case DRM_FORMAT_BGRA5551
:
2142 case DRM_FORMAT_RGB565
:
2143 case DRM_FORMAT_BGR565
:
2144 case DRM_FORMAT_RGB888
:
2145 case DRM_FORMAT_BGR888
:
2146 case DRM_FORMAT_XRGB8888
:
2147 case DRM_FORMAT_XBGR8888
:
2148 case DRM_FORMAT_RGBX8888
:
2149 case DRM_FORMAT_BGRX8888
:
2150 case DRM_FORMAT_ARGB8888
:
2151 case DRM_FORMAT_ABGR8888
:
2152 case DRM_FORMAT_RGBA8888
:
2153 case DRM_FORMAT_BGRA8888
:
2154 case DRM_FORMAT_XRGB2101010
:
2155 case DRM_FORMAT_XBGR2101010
:
2156 case DRM_FORMAT_RGBX1010102
:
2157 case DRM_FORMAT_BGRX1010102
:
2158 case DRM_FORMAT_ARGB2101010
:
2159 case DRM_FORMAT_ABGR2101010
:
2160 case DRM_FORMAT_RGBA1010102
:
2161 case DRM_FORMAT_BGRA1010102
:
2162 case DRM_FORMAT_YUYV
:
2163 case DRM_FORMAT_YVYU
:
2164 case DRM_FORMAT_UYVY
:
2165 case DRM_FORMAT_VYUY
:
2166 case DRM_FORMAT_AYUV
:
2167 case DRM_FORMAT_NV12
:
2168 case DRM_FORMAT_NV21
:
2169 case DRM_FORMAT_NV16
:
2170 case DRM_FORMAT_NV61
:
2171 case DRM_FORMAT_YUV410
:
2172 case DRM_FORMAT_YVU410
:
2173 case DRM_FORMAT_YUV411
:
2174 case DRM_FORMAT_YVU411
:
2175 case DRM_FORMAT_YUV420
:
2176 case DRM_FORMAT_YVU420
:
2177 case DRM_FORMAT_YUV422
:
2178 case DRM_FORMAT_YVU422
:
2179 case DRM_FORMAT_YUV444
:
2180 case DRM_FORMAT_YVU444
:
2187 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
2189 int ret
, hsub
, vsub
, num_planes
, i
;
2191 ret
= format_check(r
);
2193 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r
->pixel_format
);
2197 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
2198 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
2199 num_planes
= drm_format_num_planes(r
->pixel_format
);
2201 if (r
->width
== 0 || r
->width
% hsub
) {
2202 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
2206 if (r
->height
== 0 || r
->height
% vsub
) {
2207 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
2211 for (i
= 0; i
< num_planes
; i
++) {
2212 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
2214 if (!r
->handles
[i
]) {
2215 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
2219 if (r
->pitches
[i
] < drm_format_plane_cpp(r
->pixel_format
, i
) * width
) {
2220 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
2229 * drm_mode_addfb2 - add an FB to the graphics configuration
2230 * @inode: inode from the ioctl
2231 * @filp: file * from the ioctl
2232 * @cmd: cmd from ioctl
2233 * @arg: arg from ioctl
2236 * Takes mode config lock.
2238 * Add a new FB to the specified CRTC, given a user request with format.
2240 * Called by the user via ioctl.
2243 * Zero on success, errno on failure.
2245 int drm_mode_addfb2(struct drm_device
*dev
,
2246 void *data
, struct drm_file
*file_priv
)
2248 struct drm_mode_fb_cmd2
*r
= data
;
2249 struct drm_mode_config
*config
= &dev
->mode_config
;
2250 struct drm_framebuffer
*fb
;
2253 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2256 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
2257 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2258 r
->width
, config
->min_width
, config
->max_width
);
2261 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
2262 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2263 r
->height
, config
->min_height
, config
->max_height
);
2267 ret
= framebuffer_check(r
);
2271 mutex_lock(&dev
->mode_config
.mutex
);
2273 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
2275 DRM_DEBUG_KMS("could not create framebuffer\n");
2280 r
->fb_id
= fb
->base
.id
;
2281 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2282 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2285 mutex_unlock(&dev
->mode_config
.mutex
);
2290 * drm_mode_rmfb - remove an FB from the configuration
2291 * @inode: inode from the ioctl
2292 * @filp: file * from the ioctl
2293 * @cmd: cmd from ioctl
2294 * @arg: arg from ioctl
2297 * Takes mode config lock.
2299 * Remove the FB specified by the user.
2301 * Called by the user via ioctl.
2304 * Zero on success, errno on failure.
2306 int drm_mode_rmfb(struct drm_device
*dev
,
2307 void *data
, struct drm_file
*file_priv
)
2309 struct drm_mode_object
*obj
;
2310 struct drm_framebuffer
*fb
= NULL
;
2311 struct drm_framebuffer
*fbl
= NULL
;
2312 uint32_t *id
= data
;
2316 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2319 mutex_lock(&dev
->mode_config
.mutex
);
2320 obj
= drm_mode_object_find(dev
, *id
, DRM_MODE_OBJECT_FB
);
2321 /* TODO check that we really get a framebuffer back. */
2326 fb
= obj_to_fb(obj
);
2328 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
2337 /* TODO release all crtc connected to the framebuffer */
2338 /* TODO unhock the destructor from the buffer object */
2340 list_del(&fb
->filp_head
);
2341 fb
->funcs
->destroy(fb
);
2344 mutex_unlock(&dev
->mode_config
.mutex
);
2349 * drm_mode_getfb - get FB info
2350 * @inode: inode from the ioctl
2351 * @filp: file * from the ioctl
2352 * @cmd: cmd from ioctl
2353 * @arg: arg from ioctl
2356 * Takes mode config lock.
2358 * Lookup the FB given its ID and return info about it.
2360 * Called by the user via ioctl.
2363 * Zero on success, errno on failure.
2365 int drm_mode_getfb(struct drm_device
*dev
,
2366 void *data
, struct drm_file
*file_priv
)
2368 struct drm_mode_fb_cmd
*r
= data
;
2369 struct drm_mode_object
*obj
;
2370 struct drm_framebuffer
*fb
;
2373 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2376 mutex_lock(&dev
->mode_config
.mutex
);
2377 obj
= drm_mode_object_find(dev
, r
->fb_id
, DRM_MODE_OBJECT_FB
);
2382 fb
= obj_to_fb(obj
);
2384 r
->height
= fb
->height
;
2385 r
->width
= fb
->width
;
2386 r
->depth
= fb
->depth
;
2387 r
->bpp
= fb
->bits_per_pixel
;
2388 r
->pitch
= fb
->pitches
[0];
2389 fb
->funcs
->create_handle(fb
, file_priv
, &r
->handle
);
2392 mutex_unlock(&dev
->mode_config
.mutex
);
2396 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
2397 void *data
, struct drm_file
*file_priv
)
2399 struct drm_clip_rect __user
*clips_ptr
;
2400 struct drm_clip_rect
*clips
= NULL
;
2401 struct drm_mode_fb_dirty_cmd
*r
= data
;
2402 struct drm_mode_object
*obj
;
2403 struct drm_framebuffer
*fb
;
2408 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2411 mutex_lock(&dev
->mode_config
.mutex
);
2412 obj
= drm_mode_object_find(dev
, r
->fb_id
, DRM_MODE_OBJECT_FB
);
2417 fb
= obj_to_fb(obj
);
2419 num_clips
= r
->num_clips
;
2420 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
2422 if (!num_clips
!= !clips_ptr
) {
2427 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
2429 /* If userspace annotates copy, clips must come in pairs */
2430 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
2435 if (num_clips
&& clips_ptr
) {
2436 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
2440 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
2446 ret
= copy_from_user(clips
, clips_ptr
,
2447 num_clips
* sizeof(*clips
));
2454 if (fb
->funcs
->dirty
) {
2455 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
2465 mutex_unlock(&dev
->mode_config
.mutex
);
2471 * drm_fb_release - remove and free the FBs on this file
2472 * @filp: file * from the ioctl
2475 * Takes mode config lock.
2477 * Destroy all the FBs associated with @filp.
2479 * Called by the user via ioctl.
2482 * Zero on success, errno on failure.
2484 void drm_fb_release(struct drm_file
*priv
)
2486 struct drm_device
*dev
= priv
->minor
->dev
;
2487 struct drm_framebuffer
*fb
, *tfb
;
2489 mutex_lock(&dev
->mode_config
.mutex
);
2490 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
2491 list_del(&fb
->filp_head
);
2492 fb
->funcs
->destroy(fb
);
2494 mutex_unlock(&dev
->mode_config
.mutex
);
2498 * drm_mode_attachmode - add a mode to the user mode list
2500 * @connector: connector to add the mode to
2501 * @mode: mode to add
2503 * Add @mode to @connector's user mode list.
2505 static void drm_mode_attachmode(struct drm_device
*dev
,
2506 struct drm_connector
*connector
,
2507 struct drm_display_mode
*mode
)
2509 list_add_tail(&mode
->head
, &connector
->user_modes
);
2512 int drm_mode_attachmode_crtc(struct drm_device
*dev
, struct drm_crtc
*crtc
,
2513 const struct drm_display_mode
*mode
)
2515 struct drm_connector
*connector
;
2517 struct drm_display_mode
*dup_mode
, *next
;
2520 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
2521 if (!connector
->encoder
)
2523 if (connector
->encoder
->crtc
== crtc
) {
2524 dup_mode
= drm_mode_duplicate(dev
, mode
);
2529 list_add_tail(&dup_mode
->head
, &list
);
2533 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
2534 if (!connector
->encoder
)
2536 if (connector
->encoder
->crtc
== crtc
)
2537 list_move_tail(list
.next
, &connector
->user_modes
);
2540 WARN_ON(!list_empty(&list
));
2543 list_for_each_entry_safe(dup_mode
, next
, &list
, head
)
2544 drm_mode_destroy(dev
, dup_mode
);
2548 EXPORT_SYMBOL(drm_mode_attachmode_crtc
);
2550 static int drm_mode_detachmode(struct drm_device
*dev
,
2551 struct drm_connector
*connector
,
2552 struct drm_display_mode
*mode
)
2556 struct drm_display_mode
*match_mode
, *t
;
2558 list_for_each_entry_safe(match_mode
, t
, &connector
->user_modes
, head
) {
2559 if (drm_mode_equal(match_mode
, mode
)) {
2560 list_del(&match_mode
->head
);
2561 drm_mode_destroy(dev
, match_mode
);
2573 int drm_mode_detachmode_crtc(struct drm_device
*dev
, struct drm_display_mode
*mode
)
2575 struct drm_connector
*connector
;
2577 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
2578 drm_mode_detachmode(dev
, connector
, mode
);
2582 EXPORT_SYMBOL(drm_mode_detachmode_crtc
);
2585 * drm_fb_attachmode - Attach a user mode to an connector
2586 * @inode: inode from the ioctl
2587 * @filp: file * from the ioctl
2588 * @cmd: cmd from ioctl
2589 * @arg: arg from ioctl
2591 * This attaches a user specified mode to an connector.
2592 * Called by the user via ioctl.
2595 * Zero on success, errno on failure.
2597 int drm_mode_attachmode_ioctl(struct drm_device
*dev
,
2598 void *data
, struct drm_file
*file_priv
)
2600 struct drm_mode_mode_cmd
*mode_cmd
= data
;
2601 struct drm_connector
*connector
;
2602 struct drm_display_mode
*mode
;
2603 struct drm_mode_object
*obj
;
2604 struct drm_mode_modeinfo
*umode
= &mode_cmd
->mode
;
2607 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2610 mutex_lock(&dev
->mode_config
.mutex
);
2612 obj
= drm_mode_object_find(dev
, mode_cmd
->connector_id
, DRM_MODE_OBJECT_CONNECTOR
);
2617 connector
= obj_to_connector(obj
);
2619 mode
= drm_mode_create(dev
);
2625 ret
= drm_crtc_convert_umode(mode
, umode
);
2627 DRM_DEBUG_KMS("Invalid mode\n");
2628 drm_mode_destroy(dev
, mode
);
2632 drm_mode_attachmode(dev
, connector
, mode
);
2634 mutex_unlock(&dev
->mode_config
.mutex
);
2640 * drm_fb_detachmode - Detach a user specified mode from an connector
2641 * @inode: inode from the ioctl
2642 * @filp: file * from the ioctl
2643 * @cmd: cmd from ioctl
2644 * @arg: arg from ioctl
2646 * Called by the user via ioctl.
2649 * Zero on success, errno on failure.
2651 int drm_mode_detachmode_ioctl(struct drm_device
*dev
,
2652 void *data
, struct drm_file
*file_priv
)
2654 struct drm_mode_object
*obj
;
2655 struct drm_mode_mode_cmd
*mode_cmd
= data
;
2656 struct drm_connector
*connector
;
2657 struct drm_display_mode mode
;
2658 struct drm_mode_modeinfo
*umode
= &mode_cmd
->mode
;
2661 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2664 mutex_lock(&dev
->mode_config
.mutex
);
2666 obj
= drm_mode_object_find(dev
, mode_cmd
->connector_id
, DRM_MODE_OBJECT_CONNECTOR
);
2671 connector
= obj_to_connector(obj
);
2673 ret
= drm_crtc_convert_umode(&mode
, umode
);
2675 DRM_DEBUG_KMS("Invalid mode\n");
2679 ret
= drm_mode_detachmode(dev
, connector
, &mode
);
2681 mutex_unlock(&dev
->mode_config
.mutex
);
2685 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
2686 const char *name
, int num_values
)
2688 struct drm_property
*property
= NULL
;
2691 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
2696 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
2697 if (!property
->values
)
2701 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
2705 property
->flags
= flags
;
2706 property
->num_values
= num_values
;
2707 INIT_LIST_HEAD(&property
->enum_blob_list
);
2710 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
2711 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2714 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
2717 kfree(property
->values
);
2721 EXPORT_SYMBOL(drm_property_create
);
2723 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
2725 const struct drm_prop_enum_list
*props
,
2728 struct drm_property
*property
;
2731 flags
|= DRM_MODE_PROP_ENUM
;
2733 property
= drm_property_create(dev
, flags
, name
, num_values
);
2737 for (i
= 0; i
< num_values
; i
++) {
2738 ret
= drm_property_add_enum(property
, i
,
2742 drm_property_destroy(dev
, property
);
2749 EXPORT_SYMBOL(drm_property_create_enum
);
2751 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
2752 int flags
, const char *name
,
2753 const struct drm_prop_enum_list
*props
,
2756 struct drm_property
*property
;
2759 flags
|= DRM_MODE_PROP_BITMASK
;
2761 property
= drm_property_create(dev
, flags
, name
, num_values
);
2765 for (i
= 0; i
< num_values
; i
++) {
2766 ret
= drm_property_add_enum(property
, i
,
2770 drm_property_destroy(dev
, property
);
2777 EXPORT_SYMBOL(drm_property_create_bitmask
);
2779 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
2781 uint64_t min
, uint64_t max
)
2783 struct drm_property
*property
;
2785 flags
|= DRM_MODE_PROP_RANGE
;
2787 property
= drm_property_create(dev
, flags
, name
, 2);
2791 property
->values
[0] = min
;
2792 property
->values
[1] = max
;
2796 EXPORT_SYMBOL(drm_property_create_range
);
2798 int drm_property_add_enum(struct drm_property
*property
, int index
,
2799 uint64_t value
, const char *name
)
2801 struct drm_property_enum
*prop_enum
;
2803 if (!(property
->flags
& (DRM_MODE_PROP_ENUM
| DRM_MODE_PROP_BITMASK
)))
2807 * Bitmask enum properties have the additional constraint of values
2810 if ((property
->flags
& DRM_MODE_PROP_BITMASK
) && (value
> 63))
2813 if (!list_empty(&property
->enum_blob_list
)) {
2814 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
2815 if (prop_enum
->value
== value
) {
2816 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
2817 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2823 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
2827 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
2828 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2829 prop_enum
->value
= value
;
2831 property
->values
[index
] = value
;
2832 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
2835 EXPORT_SYMBOL(drm_property_add_enum
);
2837 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
2839 struct drm_property_enum
*prop_enum
, *pt
;
2841 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
2842 list_del(&prop_enum
->head
);
2846 if (property
->num_values
)
2847 kfree(property
->values
);
2848 drm_mode_object_put(dev
, &property
->base
);
2849 list_del(&property
->head
);
2852 EXPORT_SYMBOL(drm_property_destroy
);
2854 void drm_connector_attach_property(struct drm_connector
*connector
,
2855 struct drm_property
*property
, uint64_t init_val
)
2857 drm_object_attach_property(&connector
->base
, property
, init_val
);
2859 EXPORT_SYMBOL(drm_connector_attach_property
);
2861 int drm_connector_property_set_value(struct drm_connector
*connector
,
2862 struct drm_property
*property
, uint64_t value
)
2864 return drm_object_property_set_value(&connector
->base
, property
, value
);
2866 EXPORT_SYMBOL(drm_connector_property_set_value
);
2868 int drm_connector_property_get_value(struct drm_connector
*connector
,
2869 struct drm_property
*property
, uint64_t *val
)
2871 return drm_object_property_get_value(&connector
->base
, property
, val
);
2873 EXPORT_SYMBOL(drm_connector_property_get_value
);
2875 void drm_object_attach_property(struct drm_mode_object
*obj
,
2876 struct drm_property
*property
,
2879 int count
= obj
->properties
->count
;
2881 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
2882 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2883 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2884 "you see this message on the same object type.\n",
2889 obj
->properties
->ids
[count
] = property
->base
.id
;
2890 obj
->properties
->values
[count
] = init_val
;
2891 obj
->properties
->count
++;
2893 EXPORT_SYMBOL(drm_object_attach_property
);
2895 int drm_object_property_set_value(struct drm_mode_object
*obj
,
2896 struct drm_property
*property
, uint64_t val
)
2900 for (i
= 0; i
< obj
->properties
->count
; i
++) {
2901 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
2902 obj
->properties
->values
[i
] = val
;
2909 EXPORT_SYMBOL(drm_object_property_set_value
);
2911 int drm_object_property_get_value(struct drm_mode_object
*obj
,
2912 struct drm_property
*property
, uint64_t *val
)
2916 for (i
= 0; i
< obj
->properties
->count
; i
++) {
2917 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
2918 *val
= obj
->properties
->values
[i
];
2925 EXPORT_SYMBOL(drm_object_property_get_value
);
2927 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
2928 void *data
, struct drm_file
*file_priv
)
2930 struct drm_mode_object
*obj
;
2931 struct drm_mode_get_property
*out_resp
= data
;
2932 struct drm_property
*property
;
2935 int value_count
= 0;
2938 struct drm_property_enum
*prop_enum
;
2939 struct drm_mode_property_enum __user
*enum_ptr
;
2940 struct drm_property_blob
*prop_blob
;
2941 uint32_t __user
*blob_id_ptr
;
2942 uint64_t __user
*values_ptr
;
2943 uint32_t __user
*blob_length_ptr
;
2945 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2948 mutex_lock(&dev
->mode_config
.mutex
);
2949 obj
= drm_mode_object_find(dev
, out_resp
->prop_id
, DRM_MODE_OBJECT_PROPERTY
);
2954 property
= obj_to_property(obj
);
2956 if (property
->flags
& (DRM_MODE_PROP_ENUM
| DRM_MODE_PROP_BITMASK
)) {
2957 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
2959 } else if (property
->flags
& DRM_MODE_PROP_BLOB
) {
2960 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
2964 value_count
= property
->num_values
;
2966 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
2967 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
2968 out_resp
->flags
= property
->flags
;
2970 if ((out_resp
->count_values
>= value_count
) && value_count
) {
2971 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
2972 for (i
= 0; i
< value_count
; i
++) {
2973 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
2979 out_resp
->count_values
= value_count
;
2981 if (property
->flags
& (DRM_MODE_PROP_ENUM
| DRM_MODE_PROP_BITMASK
)) {
2982 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
2984 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
2985 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
2987 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
2992 if (copy_to_user(&enum_ptr
[copied
].name
,
2993 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3000 out_resp
->count_enum_blobs
= enum_count
;
3003 if (property
->flags
& DRM_MODE_PROP_BLOB
) {
3004 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
3006 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3007 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
3009 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
3010 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
3015 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
3023 out_resp
->count_enum_blobs
= blob_count
;
3026 mutex_unlock(&dev
->mode_config
.mutex
);
3030 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
3033 struct drm_property_blob
*blob
;
3036 if (!length
|| !data
)
3039 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
3043 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
3049 blob
->length
= length
;
3051 memcpy(blob
->data
, data
, length
);
3053 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3057 static void drm_property_destroy_blob(struct drm_device
*dev
,
3058 struct drm_property_blob
*blob
)
3060 drm_mode_object_put(dev
, &blob
->base
);
3061 list_del(&blob
->head
);
3065 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3066 void *data
, struct drm_file
*file_priv
)
3068 struct drm_mode_object
*obj
;
3069 struct drm_mode_get_blob
*out_resp
= data
;
3070 struct drm_property_blob
*blob
;
3072 void __user
*blob_ptr
;
3074 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3077 mutex_lock(&dev
->mode_config
.mutex
);
3078 obj
= drm_mode_object_find(dev
, out_resp
->blob_id
, DRM_MODE_OBJECT_BLOB
);
3083 blob
= obj_to_blob(obj
);
3085 if (out_resp
->length
== blob
->length
) {
3086 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3087 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3092 out_resp
->length
= blob
->length
;
3095 mutex_unlock(&dev
->mode_config
.mutex
);
3099 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
3102 struct drm_device
*dev
= connector
->dev
;
3105 if (connector
->edid_blob_ptr
)
3106 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
3108 /* Delete edid, when there is none. */
3110 connector
->edid_blob_ptr
= NULL
;
3111 ret
= drm_connector_property_set_value(connector
, dev
->mode_config
.edid_property
, 0);
3115 size
= EDID_LENGTH
* (1 + edid
->extensions
);
3116 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
3119 ret
= drm_connector_property_set_value(connector
,
3120 dev
->mode_config
.edid_property
,
3121 connector
->edid_blob_ptr
->base
.id
);
3125 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
3127 static bool drm_property_change_is_valid(struct drm_property
*property
,
3130 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
3132 if (property
->flags
& DRM_MODE_PROP_RANGE
) {
3133 if (value
< property
->values
[0] || value
> property
->values
[1])
3136 } else if (property
->flags
& DRM_MODE_PROP_BITMASK
) {
3138 uint64_t valid_mask
= 0;
3139 for (i
= 0; i
< property
->num_values
; i
++)
3140 valid_mask
|= (1ULL << property
->values
[i
]);
3141 return !(value
& ~valid_mask
);
3144 for (i
= 0; i
< property
->num_values
; i
++)
3145 if (property
->values
[i
] == value
)
3151 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
3152 void *data
, struct drm_file
*file_priv
)
3154 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
3155 struct drm_mode_obj_set_property obj_set_prop
= {
3156 .value
= conn_set_prop
->value
,
3157 .prop_id
= conn_set_prop
->prop_id
,
3158 .obj_id
= conn_set_prop
->connector_id
,
3159 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
3162 /* It does all the locking and checking we need */
3163 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
3166 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
3167 struct drm_property
*property
,
3171 struct drm_connector
*connector
= obj_to_connector(obj
);
3173 /* Do DPMS ourselves */
3174 if (property
== connector
->dev
->mode_config
.dpms_property
) {
3175 if (connector
->funcs
->dpms
)
3176 (*connector
->funcs
->dpms
)(connector
, (int)value
);
3178 } else if (connector
->funcs
->set_property
)
3179 ret
= connector
->funcs
->set_property(connector
, property
, value
);
3181 /* store the property value if successful */
3183 drm_connector_property_set_value(connector
, property
, value
);
3187 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
3188 struct drm_property
*property
,
3192 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
3194 if (crtc
->funcs
->set_property
)
3195 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
3197 drm_object_property_set_value(obj
, property
, value
);
3202 static int drm_mode_plane_set_obj_prop(struct drm_mode_object
*obj
,
3203 struct drm_property
*property
,
3207 struct drm_plane
*plane
= obj_to_plane(obj
);
3209 if (plane
->funcs
->set_property
)
3210 ret
= plane
->funcs
->set_property(plane
, property
, value
);
3212 drm_object_property_set_value(obj
, property
, value
);
3217 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
3218 struct drm_file
*file_priv
)
3220 struct drm_mode_obj_get_properties
*arg
= data
;
3221 struct drm_mode_object
*obj
;
3225 int props_count
= 0;
3226 uint32_t __user
*props_ptr
;
3227 uint64_t __user
*prop_values_ptr
;
3229 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3232 mutex_lock(&dev
->mode_config
.mutex
);
3234 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3239 if (!obj
->properties
) {
3244 props_count
= obj
->properties
->count
;
3246 /* This ioctl is called twice, once to determine how much space is
3247 * needed, and the 2nd time to fill it. */
3248 if ((arg
->count_props
>= props_count
) && props_count
) {
3250 props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
3251 prop_values_ptr
= (uint64_t __user
*)(unsigned long)
3252 (arg
->prop_values_ptr
);
3253 for (i
= 0; i
< props_count
; i
++) {
3254 if (put_user(obj
->properties
->ids
[i
],
3255 props_ptr
+ copied
)) {
3259 if (put_user(obj
->properties
->values
[i
],
3260 prop_values_ptr
+ copied
)) {
3267 arg
->count_props
= props_count
;
3269 mutex_unlock(&dev
->mode_config
.mutex
);
3273 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
3274 struct drm_file
*file_priv
)
3276 struct drm_mode_obj_set_property
*arg
= data
;
3277 struct drm_mode_object
*arg_obj
;
3278 struct drm_mode_object
*prop_obj
;
3279 struct drm_property
*property
;
3283 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3286 mutex_lock(&dev
->mode_config
.mutex
);
3288 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3291 if (!arg_obj
->properties
)
3294 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
3295 if (arg_obj
->properties
->ids
[i
] == arg
->prop_id
)
3298 if (i
== arg_obj
->properties
->count
)
3301 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
3302 DRM_MODE_OBJECT_PROPERTY
);
3305 property
= obj_to_property(prop_obj
);
3307 if (!drm_property_change_is_valid(property
, arg
->value
))
3310 switch (arg_obj
->type
) {
3311 case DRM_MODE_OBJECT_CONNECTOR
:
3312 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
3315 case DRM_MODE_OBJECT_CRTC
:
3316 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
3318 case DRM_MODE_OBJECT_PLANE
:
3319 ret
= drm_mode_plane_set_obj_prop(arg_obj
, property
, arg
->value
);
3324 mutex_unlock(&dev
->mode_config
.mutex
);
3328 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
3329 struct drm_encoder
*encoder
)
3333 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
3334 if (connector
->encoder_ids
[i
] == 0) {
3335 connector
->encoder_ids
[i
] = encoder
->base
.id
;
3341 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
3343 void drm_mode_connector_detach_encoder(struct drm_connector
*connector
,
3344 struct drm_encoder
*encoder
)
3347 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
3348 if (connector
->encoder_ids
[i
] == encoder
->base
.id
) {
3349 connector
->encoder_ids
[i
] = 0;
3350 if (connector
->encoder
== encoder
)
3351 connector
->encoder
= NULL
;
3356 EXPORT_SYMBOL(drm_mode_connector_detach_encoder
);
3358 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
3361 crtc
->gamma_size
= gamma_size
;
3363 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
3364 if (!crtc
->gamma_store
) {
3365 crtc
->gamma_size
= 0;
3371 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
3373 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
3374 void *data
, struct drm_file
*file_priv
)
3376 struct drm_mode_crtc_lut
*crtc_lut
= data
;
3377 struct drm_mode_object
*obj
;
3378 struct drm_crtc
*crtc
;
3379 void *r_base
, *g_base
, *b_base
;
3383 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3386 mutex_lock(&dev
->mode_config
.mutex
);
3387 obj
= drm_mode_object_find(dev
, crtc_lut
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3392 crtc
= obj_to_crtc(obj
);
3394 if (crtc
->funcs
->gamma_set
== NULL
) {
3399 /* memcpy into gamma store */
3400 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
3405 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
3406 r_base
= crtc
->gamma_store
;
3407 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
3412 g_base
= r_base
+ size
;
3413 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
3418 b_base
= g_base
+ size
;
3419 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
3424 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
3427 mutex_unlock(&dev
->mode_config
.mutex
);
3432 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
3433 void *data
, struct drm_file
*file_priv
)
3435 struct drm_mode_crtc_lut
*crtc_lut
= data
;
3436 struct drm_mode_object
*obj
;
3437 struct drm_crtc
*crtc
;
3438 void *r_base
, *g_base
, *b_base
;
3442 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3445 mutex_lock(&dev
->mode_config
.mutex
);
3446 obj
= drm_mode_object_find(dev
, crtc_lut
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3451 crtc
= obj_to_crtc(obj
);
3453 /* memcpy into gamma store */
3454 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
3459 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
3460 r_base
= crtc
->gamma_store
;
3461 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
3466 g_base
= r_base
+ size
;
3467 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
3472 b_base
= g_base
+ size
;
3473 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
3478 mutex_unlock(&dev
->mode_config
.mutex
);
3482 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
3483 void *data
, struct drm_file
*file_priv
)
3485 struct drm_mode_crtc_page_flip
*page_flip
= data
;
3486 struct drm_mode_object
*obj
;
3487 struct drm_crtc
*crtc
;
3488 struct drm_framebuffer
*fb
;
3489 struct drm_pending_vblank_event
*e
= NULL
;
3490 unsigned long flags
;
3493 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
3494 page_flip
->reserved
!= 0)
3497 mutex_lock(&dev
->mode_config
.mutex
);
3498 obj
= drm_mode_object_find(dev
, page_flip
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3501 crtc
= obj_to_crtc(obj
);
3503 if (crtc
->fb
== NULL
) {
3504 /* The framebuffer is currently unbound, presumably
3505 * due to a hotplug event, that userspace has not
3512 if (crtc
->funcs
->page_flip
== NULL
)
3515 obj
= drm_mode_object_find(dev
, page_flip
->fb_id
, DRM_MODE_OBJECT_FB
);
3518 fb
= obj_to_fb(obj
);
3520 if (crtc
->mode
.hdisplay
> fb
->width
||
3521 crtc
->mode
.vdisplay
> fb
->height
||
3522 crtc
->x
> fb
->width
- crtc
->mode
.hdisplay
||
3523 crtc
->y
> fb
->height
- crtc
->mode
.vdisplay
) {
3524 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
3525 fb
->width
, fb
->height
,
3526 crtc
->mode
.hdisplay
, crtc
->mode
.vdisplay
,
3532 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
3534 spin_lock_irqsave(&dev
->event_lock
, flags
);
3535 if (file_priv
->event_space
< sizeof e
->event
) {
3536 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3539 file_priv
->event_space
-= sizeof e
->event
;
3540 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3542 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
3544 spin_lock_irqsave(&dev
->event_lock
, flags
);
3545 file_priv
->event_space
+= sizeof e
->event
;
3546 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3550 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
3551 e
->event
.base
.length
= sizeof e
->event
;
3552 e
->event
.user_data
= page_flip
->user_data
;
3553 e
->base
.event
= &e
->event
.base
;
3554 e
->base
.file_priv
= file_priv
;
3556 (void (*) (struct drm_pending_event
*)) kfree
;
3559 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
);
3561 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
3562 spin_lock_irqsave(&dev
->event_lock
, flags
);
3563 file_priv
->event_space
+= sizeof e
->event
;
3564 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3570 mutex_unlock(&dev
->mode_config
.mutex
);
3574 void drm_mode_config_reset(struct drm_device
*dev
)
3576 struct drm_crtc
*crtc
;
3577 struct drm_encoder
*encoder
;
3578 struct drm_connector
*connector
;
3580 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
3581 if (crtc
->funcs
->reset
)
3582 crtc
->funcs
->reset(crtc
);
3584 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
3585 if (encoder
->funcs
->reset
)
3586 encoder
->funcs
->reset(encoder
);
3588 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
3589 if (connector
->funcs
->reset
)
3590 connector
->funcs
->reset(connector
);
3592 EXPORT_SYMBOL(drm_mode_config_reset
);
3594 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
3595 void *data
, struct drm_file
*file_priv
)
3597 struct drm_mode_create_dumb
*args
= data
;
3599 if (!dev
->driver
->dumb_create
)
3601 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
3604 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
3605 void *data
, struct drm_file
*file_priv
)
3607 struct drm_mode_map_dumb
*args
= data
;
3609 /* call driver ioctl to get mmap offset */
3610 if (!dev
->driver
->dumb_map_offset
)
3613 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
3616 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
3617 void *data
, struct drm_file
*file_priv
)
3619 struct drm_mode_destroy_dumb
*args
= data
;
3621 if (!dev
->driver
->dumb_destroy
)
3624 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
3628 * Just need to support RGB formats here for compat with code that doesn't
3629 * use pixel formats directly yet.
3631 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
3635 case DRM_FORMAT_RGB332
:
3636 case DRM_FORMAT_BGR233
:
3640 case DRM_FORMAT_XRGB1555
:
3641 case DRM_FORMAT_XBGR1555
:
3642 case DRM_FORMAT_RGBX5551
:
3643 case DRM_FORMAT_BGRX5551
:
3644 case DRM_FORMAT_ARGB1555
:
3645 case DRM_FORMAT_ABGR1555
:
3646 case DRM_FORMAT_RGBA5551
:
3647 case DRM_FORMAT_BGRA5551
:
3651 case DRM_FORMAT_RGB565
:
3652 case DRM_FORMAT_BGR565
:
3656 case DRM_FORMAT_RGB888
:
3657 case DRM_FORMAT_BGR888
:
3661 case DRM_FORMAT_XRGB8888
:
3662 case DRM_FORMAT_XBGR8888
:
3663 case DRM_FORMAT_RGBX8888
:
3664 case DRM_FORMAT_BGRX8888
:
3668 case DRM_FORMAT_XRGB2101010
:
3669 case DRM_FORMAT_XBGR2101010
:
3670 case DRM_FORMAT_RGBX1010102
:
3671 case DRM_FORMAT_BGRX1010102
:
3672 case DRM_FORMAT_ARGB2101010
:
3673 case DRM_FORMAT_ABGR2101010
:
3674 case DRM_FORMAT_RGBA1010102
:
3675 case DRM_FORMAT_BGRA1010102
:
3679 case DRM_FORMAT_ARGB8888
:
3680 case DRM_FORMAT_ABGR8888
:
3681 case DRM_FORMAT_RGBA8888
:
3682 case DRM_FORMAT_BGRA8888
:
3687 DRM_DEBUG_KMS("unsupported pixel format\n");
3693 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
3696 * drm_format_num_planes - get the number of planes for format
3697 * @format: pixel format (DRM_FORMAT_*)
3700 * The number of planes used by the specified pixel format.
3702 int drm_format_num_planes(uint32_t format
)
3705 case DRM_FORMAT_YUV410
:
3706 case DRM_FORMAT_YVU410
:
3707 case DRM_FORMAT_YUV411
:
3708 case DRM_FORMAT_YVU411
:
3709 case DRM_FORMAT_YUV420
:
3710 case DRM_FORMAT_YVU420
:
3711 case DRM_FORMAT_YUV422
:
3712 case DRM_FORMAT_YVU422
:
3713 case DRM_FORMAT_YUV444
:
3714 case DRM_FORMAT_YVU444
:
3716 case DRM_FORMAT_NV12
:
3717 case DRM_FORMAT_NV21
:
3718 case DRM_FORMAT_NV16
:
3719 case DRM_FORMAT_NV61
:
3725 EXPORT_SYMBOL(drm_format_num_planes
);
3728 * drm_format_plane_cpp - determine the bytes per pixel value
3729 * @format: pixel format (DRM_FORMAT_*)
3730 * @plane: plane index
3733 * The bytes per pixel value for the specified plane.
3735 int drm_format_plane_cpp(uint32_t format
, int plane
)
3740 if (plane
>= drm_format_num_planes(format
))
3744 case DRM_FORMAT_YUYV
:
3745 case DRM_FORMAT_YVYU
:
3746 case DRM_FORMAT_UYVY
:
3747 case DRM_FORMAT_VYUY
:
3749 case DRM_FORMAT_NV12
:
3750 case DRM_FORMAT_NV21
:
3751 case DRM_FORMAT_NV16
:
3752 case DRM_FORMAT_NV61
:
3753 return plane
? 2 : 1;
3754 case DRM_FORMAT_YUV410
:
3755 case DRM_FORMAT_YVU410
:
3756 case DRM_FORMAT_YUV411
:
3757 case DRM_FORMAT_YVU411
:
3758 case DRM_FORMAT_YUV420
:
3759 case DRM_FORMAT_YVU420
:
3760 case DRM_FORMAT_YUV422
:
3761 case DRM_FORMAT_YVU422
:
3762 case DRM_FORMAT_YUV444
:
3763 case DRM_FORMAT_YVU444
:
3766 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
3770 EXPORT_SYMBOL(drm_format_plane_cpp
);
3773 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3774 * @format: pixel format (DRM_FORMAT_*)
3777 * The horizontal chroma subsampling factor for the
3778 * specified pixel format.
3780 int drm_format_horz_chroma_subsampling(uint32_t format
)
3783 case DRM_FORMAT_YUV411
:
3784 case DRM_FORMAT_YVU411
:
3785 case DRM_FORMAT_YUV410
:
3786 case DRM_FORMAT_YVU410
:
3788 case DRM_FORMAT_YUYV
:
3789 case DRM_FORMAT_YVYU
:
3790 case DRM_FORMAT_UYVY
:
3791 case DRM_FORMAT_VYUY
:
3792 case DRM_FORMAT_NV12
:
3793 case DRM_FORMAT_NV21
:
3794 case DRM_FORMAT_NV16
:
3795 case DRM_FORMAT_NV61
:
3796 case DRM_FORMAT_YUV422
:
3797 case DRM_FORMAT_YVU422
:
3798 case DRM_FORMAT_YUV420
:
3799 case DRM_FORMAT_YVU420
:
3805 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
3808 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3809 * @format: pixel format (DRM_FORMAT_*)
3812 * The vertical chroma subsampling factor for the
3813 * specified pixel format.
3815 int drm_format_vert_chroma_subsampling(uint32_t format
)
3818 case DRM_FORMAT_YUV410
:
3819 case DRM_FORMAT_YVU410
:
3821 case DRM_FORMAT_YUV420
:
3822 case DRM_FORMAT_YVU420
:
3823 case DRM_FORMAT_NV12
:
3824 case DRM_FORMAT_NV21
:
3830 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);