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>
39 #include "drm_fourcc.h"
41 /* Avoid boilerplate. I'm tired of typing. */
42 #define DRM_ENUM_NAME_FN(fnname, list) \
43 char *fnname(int val) \
46 for (i = 0; i < ARRAY_SIZE(list); i++) { \
47 if (list[i].type == val) \
48 return list[i].name; \
56 static struct drm_prop_enum_list drm_dpms_enum_list
[] =
57 { { DRM_MODE_DPMS_ON
, "On" },
58 { DRM_MODE_DPMS_STANDBY
, "Standby" },
59 { DRM_MODE_DPMS_SUSPEND
, "Suspend" },
60 { DRM_MODE_DPMS_OFF
, "Off" }
63 DRM_ENUM_NAME_FN(drm_get_dpms_name
, drm_dpms_enum_list
)
68 static struct drm_prop_enum_list drm_scaling_mode_enum_list
[] =
70 { DRM_MODE_SCALE_NONE
, "None" },
71 { DRM_MODE_SCALE_FULLSCREEN
, "Full" },
72 { DRM_MODE_SCALE_CENTER
, "Center" },
73 { DRM_MODE_SCALE_ASPECT
, "Full aspect" },
76 static struct drm_prop_enum_list drm_dithering_mode_enum_list
[] =
78 { DRM_MODE_DITHERING_OFF
, "Off" },
79 { DRM_MODE_DITHERING_ON
, "On" },
80 { DRM_MODE_DITHERING_AUTO
, "Automatic" },
84 * Non-global properties, but "required" for certain connectors.
86 static struct drm_prop_enum_list drm_dvi_i_select_enum_list
[] =
88 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
89 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
90 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
93 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name
, drm_dvi_i_select_enum_list
)
95 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list
[] =
97 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
98 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
99 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
102 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name
,
103 drm_dvi_i_subconnector_enum_list
)
105 static struct drm_prop_enum_list drm_tv_select_enum_list
[] =
107 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
111 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
114 DRM_ENUM_NAME_FN(drm_get_tv_select_name
, drm_tv_select_enum_list
)
116 static struct drm_prop_enum_list drm_tv_subconnector_enum_list
[] =
118 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
125 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name
,
126 drm_tv_subconnector_enum_list
)
128 static struct drm_prop_enum_list drm_dirty_info_enum_list
[] = {
129 { DRM_MODE_DIRTY_OFF
, "Off" },
130 { DRM_MODE_DIRTY_ON
, "On" },
131 { DRM_MODE_DIRTY_ANNOTATE
, "Annotate" },
134 DRM_ENUM_NAME_FN(drm_get_dirty_info_name
,
135 drm_dirty_info_enum_list
)
137 struct drm_conn_prop_enum_list
{
144 * Connector and encoder types.
146 static struct drm_conn_prop_enum_list drm_connector_enum_list
[] =
147 { { DRM_MODE_CONNECTOR_Unknown
, "Unknown", 0 },
148 { DRM_MODE_CONNECTOR_VGA
, "VGA", 0 },
149 { DRM_MODE_CONNECTOR_DVII
, "DVI-I", 0 },
150 { DRM_MODE_CONNECTOR_DVID
, "DVI-D", 0 },
151 { DRM_MODE_CONNECTOR_DVIA
, "DVI-A", 0 },
152 { DRM_MODE_CONNECTOR_Composite
, "Composite", 0 },
153 { DRM_MODE_CONNECTOR_SVIDEO
, "SVIDEO", 0 },
154 { DRM_MODE_CONNECTOR_LVDS
, "LVDS", 0 },
155 { DRM_MODE_CONNECTOR_Component
, "Component", 0 },
156 { DRM_MODE_CONNECTOR_9PinDIN
, "DIN", 0 },
157 { DRM_MODE_CONNECTOR_DisplayPort
, "DP", 0 },
158 { DRM_MODE_CONNECTOR_HDMIA
, "HDMI-A", 0 },
159 { DRM_MODE_CONNECTOR_HDMIB
, "HDMI-B", 0 },
160 { DRM_MODE_CONNECTOR_TV
, "TV", 0 },
161 { DRM_MODE_CONNECTOR_eDP
, "eDP", 0 },
162 { DRM_MODE_CONNECTOR_VIRTUAL
, "Virtual", 0},
165 static struct drm_prop_enum_list drm_encoder_enum_list
[] =
166 { { DRM_MODE_ENCODER_NONE
, "None" },
167 { DRM_MODE_ENCODER_DAC
, "DAC" },
168 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
169 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
170 { DRM_MODE_ENCODER_TVDAC
, "TV" },
171 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
174 char *drm_get_encoder_name(struct drm_encoder
*encoder
)
178 snprintf(buf
, 32, "%s-%d",
179 drm_encoder_enum_list
[encoder
->encoder_type
].name
,
183 EXPORT_SYMBOL(drm_get_encoder_name
);
185 char *drm_get_connector_name(struct drm_connector
*connector
)
189 snprintf(buf
, 32, "%s-%d",
190 drm_connector_enum_list
[connector
->connector_type
].name
,
191 connector
->connector_type_id
);
194 EXPORT_SYMBOL(drm_get_connector_name
);
196 char *drm_get_connector_status_name(enum drm_connector_status status
)
198 if (status
== connector_status_connected
)
200 else if (status
== connector_status_disconnected
)
201 return "disconnected";
207 * drm_mode_object_get - allocate a new identifier
209 * @ptr: object pointer, used to generate unique ID
214 * Create a unique identifier based on @ptr in @dev's identifier space. Used
215 * for tracking modes, CRTCs and connectors.
218 * New unique (relative to other objects in @dev) integer identifier for the
221 static int drm_mode_object_get(struct drm_device
*dev
,
222 struct drm_mode_object
*obj
, uint32_t obj_type
)
228 if (idr_pre_get(&dev
->mode_config
.crtc_idr
, GFP_KERNEL
) == 0) {
229 DRM_ERROR("Ran out memory getting a mode number\n");
233 mutex_lock(&dev
->mode_config
.idr_mutex
);
234 ret
= idr_get_new_above(&dev
->mode_config
.crtc_idr
, obj
, 1, &new_id
);
235 mutex_unlock(&dev
->mode_config
.idr_mutex
);
242 obj
->type
= obj_type
;
247 * drm_mode_object_put - free an identifer
252 * Caller must hold DRM mode_config lock.
254 * Free @id from @dev's unique identifier pool.
256 static void drm_mode_object_put(struct drm_device
*dev
,
257 struct drm_mode_object
*object
)
259 mutex_lock(&dev
->mode_config
.idr_mutex
);
260 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
261 mutex_unlock(&dev
->mode_config
.idr_mutex
);
264 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
265 uint32_t id
, uint32_t type
)
267 struct drm_mode_object
*obj
= NULL
;
269 mutex_lock(&dev
->mode_config
.idr_mutex
);
270 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
271 if (!obj
|| (obj
->type
!= type
) || (obj
->id
!= id
))
273 mutex_unlock(&dev
->mode_config
.idr_mutex
);
277 EXPORT_SYMBOL(drm_mode_object_find
);
280 * drm_framebuffer_init - initialize a framebuffer
284 * Caller must hold mode config lock.
286 * Allocates an ID for the framebuffer's parent mode object, sets its mode
287 * functions & device file and adds it to the master fd list.
290 * Zero on success, error code on failure.
292 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
293 const struct drm_framebuffer_funcs
*funcs
)
297 ret
= drm_mode_object_get(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
);
303 dev
->mode_config
.num_fb
++;
304 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
308 EXPORT_SYMBOL(drm_framebuffer_init
);
311 * drm_framebuffer_cleanup - remove a framebuffer object
312 * @fb: framebuffer to remove
315 * Caller must hold mode config lock.
317 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
318 * it, setting it to NULL.
320 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
322 struct drm_device
*dev
= fb
->dev
;
323 struct drm_crtc
*crtc
;
324 struct drm_plane
*plane
;
325 struct drm_mode_set set
;
328 /* remove from any CRTC */
329 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
330 if (crtc
->fb
== fb
) {
331 /* should turn off the crtc */
332 memset(&set
, 0, sizeof(struct drm_mode_set
));
335 ret
= crtc
->funcs
->set_config(&set
);
337 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
341 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
342 if (plane
->fb
== fb
) {
343 /* should turn off the crtc */
344 ret
= plane
->funcs
->disable_plane(plane
);
346 DRM_ERROR("failed to disable plane with busy fb\n");
347 /* disconnect the plane from the fb and crtc: */
353 drm_mode_object_put(dev
, &fb
->base
);
355 dev
->mode_config
.num_fb
--;
357 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
360 * drm_crtc_init - Initialise a new CRTC object
362 * @crtc: CRTC object to init
363 * @funcs: callbacks for the new CRTC
366 * Takes mode_config lock.
368 * Inits a new object created as base part of an driver crtc object.
371 * Zero on success, error code on failure.
373 int drm_crtc_init(struct drm_device
*dev
, struct drm_crtc
*crtc
,
374 const struct drm_crtc_funcs
*funcs
)
381 mutex_lock(&dev
->mode_config
.mutex
);
383 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
387 crtc
->base
.properties
= &crtc
->properties
;
389 list_add_tail(&crtc
->head
, &dev
->mode_config
.crtc_list
);
390 dev
->mode_config
.num_crtc
++;
393 mutex_unlock(&dev
->mode_config
.mutex
);
397 EXPORT_SYMBOL(drm_crtc_init
);
400 * drm_crtc_cleanup - Cleans up the core crtc usage.
401 * @crtc: CRTC to cleanup
404 * Caller must hold mode config lock.
406 * Cleanup @crtc. Removes from drm modesetting space
407 * does NOT free object, caller does that.
409 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
411 struct drm_device
*dev
= crtc
->dev
;
413 if (crtc
->gamma_store
) {
414 kfree(crtc
->gamma_store
);
415 crtc
->gamma_store
= NULL
;
418 drm_mode_object_put(dev
, &crtc
->base
);
419 list_del(&crtc
->head
);
420 dev
->mode_config
.num_crtc
--;
422 EXPORT_SYMBOL(drm_crtc_cleanup
);
425 * drm_mode_probed_add - add a mode to a connector's probed mode list
426 * @connector: connector the new mode
430 * Caller must hold mode config lock.
432 * Add @mode to @connector's mode list for later use.
434 void drm_mode_probed_add(struct drm_connector
*connector
,
435 struct drm_display_mode
*mode
)
437 list_add(&mode
->head
, &connector
->probed_modes
);
439 EXPORT_SYMBOL(drm_mode_probed_add
);
442 * drm_mode_remove - remove and free a mode
443 * @connector: connector list to modify
444 * @mode: mode to remove
447 * Caller must hold mode config lock.
449 * Remove @mode from @connector's mode list, then free it.
451 void drm_mode_remove(struct drm_connector
*connector
,
452 struct drm_display_mode
*mode
)
454 list_del(&mode
->head
);
455 drm_mode_destroy(connector
->dev
, mode
);
457 EXPORT_SYMBOL(drm_mode_remove
);
460 * drm_connector_init - Init a preallocated connector
462 * @connector: the connector to init
463 * @funcs: callbacks for this connector
464 * @name: user visible name of the connector
467 * Takes mode config lock.
469 * Initialises a preallocated connector. Connectors should be
470 * subclassed as part of driver connector objects.
473 * Zero on success, error code on failure.
475 int drm_connector_init(struct drm_device
*dev
,
476 struct drm_connector
*connector
,
477 const struct drm_connector_funcs
*funcs
,
482 mutex_lock(&dev
->mode_config
.mutex
);
484 ret
= drm_mode_object_get(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
);
488 connector
->base
.properties
= &connector
->properties
;
489 connector
->dev
= dev
;
490 connector
->funcs
= funcs
;
491 connector
->connector_type
= connector_type
;
492 connector
->connector_type_id
=
493 ++drm_connector_enum_list
[connector_type
].count
; /* TODO */
494 INIT_LIST_HEAD(&connector
->user_modes
);
495 INIT_LIST_HEAD(&connector
->probed_modes
);
496 INIT_LIST_HEAD(&connector
->modes
);
497 connector
->edid_blob_ptr
= NULL
;
499 list_add_tail(&connector
->head
, &dev
->mode_config
.connector_list
);
500 dev
->mode_config
.num_connector
++;
502 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
503 drm_connector_attach_property(connector
,
504 dev
->mode_config
.edid_property
,
507 drm_connector_attach_property(connector
,
508 dev
->mode_config
.dpms_property
, 0);
511 mutex_unlock(&dev
->mode_config
.mutex
);
515 EXPORT_SYMBOL(drm_connector_init
);
518 * drm_connector_cleanup - cleans up an initialised connector
519 * @connector: connector to cleanup
522 * Takes mode config lock.
524 * Cleans up the connector but doesn't free the object.
526 void drm_connector_cleanup(struct drm_connector
*connector
)
528 struct drm_device
*dev
= connector
->dev
;
529 struct drm_display_mode
*mode
, *t
;
531 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
532 drm_mode_remove(connector
, mode
);
534 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
535 drm_mode_remove(connector
, mode
);
537 list_for_each_entry_safe(mode
, t
, &connector
->user_modes
, head
)
538 drm_mode_remove(connector
, mode
);
540 mutex_lock(&dev
->mode_config
.mutex
);
541 drm_mode_object_put(dev
, &connector
->base
);
542 list_del(&connector
->head
);
543 dev
->mode_config
.num_connector
--;
544 mutex_unlock(&dev
->mode_config
.mutex
);
546 EXPORT_SYMBOL(drm_connector_cleanup
);
548 void drm_connector_unplug_all(struct drm_device
*dev
)
550 struct drm_connector
*connector
;
552 /* taking the mode config mutex ends up in a clash with sysfs */
553 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
554 drm_sysfs_connector_remove(connector
);
557 EXPORT_SYMBOL(drm_connector_unplug_all
);
559 int drm_encoder_init(struct drm_device
*dev
,
560 struct drm_encoder
*encoder
,
561 const struct drm_encoder_funcs
*funcs
,
566 mutex_lock(&dev
->mode_config
.mutex
);
568 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
573 encoder
->encoder_type
= encoder_type
;
574 encoder
->funcs
= funcs
;
576 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
577 dev
->mode_config
.num_encoder
++;
580 mutex_unlock(&dev
->mode_config
.mutex
);
584 EXPORT_SYMBOL(drm_encoder_init
);
586 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
588 struct drm_device
*dev
= encoder
->dev
;
589 mutex_lock(&dev
->mode_config
.mutex
);
590 drm_mode_object_put(dev
, &encoder
->base
);
591 list_del(&encoder
->head
);
592 dev
->mode_config
.num_encoder
--;
593 mutex_unlock(&dev
->mode_config
.mutex
);
595 EXPORT_SYMBOL(drm_encoder_cleanup
);
597 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
598 unsigned long possible_crtcs
,
599 const struct drm_plane_funcs
*funcs
,
600 const uint32_t *formats
, uint32_t format_count
,
605 mutex_lock(&dev
->mode_config
.mutex
);
607 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
611 plane
->base
.properties
= &plane
->properties
;
613 plane
->funcs
= funcs
;
614 plane
->format_types
= kmalloc(sizeof(uint32_t) * format_count
,
616 if (!plane
->format_types
) {
617 DRM_DEBUG_KMS("out of memory when allocating plane\n");
618 drm_mode_object_put(dev
, &plane
->base
);
623 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
624 plane
->format_count
= format_count
;
625 plane
->possible_crtcs
= possible_crtcs
;
627 /* private planes are not exposed to userspace, but depending on
628 * display hardware, might be convenient to allow sharing programming
629 * for the scanout engine with the crtc implementation.
632 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
633 dev
->mode_config
.num_plane
++;
635 INIT_LIST_HEAD(&plane
->head
);
639 mutex_unlock(&dev
->mode_config
.mutex
);
643 EXPORT_SYMBOL(drm_plane_init
);
645 void drm_plane_cleanup(struct drm_plane
*plane
)
647 struct drm_device
*dev
= plane
->dev
;
649 mutex_lock(&dev
->mode_config
.mutex
);
650 kfree(plane
->format_types
);
651 drm_mode_object_put(dev
, &plane
->base
);
652 /* if not added to a list, it must be a private plane */
653 if (!list_empty(&plane
->head
)) {
654 list_del(&plane
->head
);
655 dev
->mode_config
.num_plane
--;
657 mutex_unlock(&dev
->mode_config
.mutex
);
659 EXPORT_SYMBOL(drm_plane_cleanup
);
662 * drm_mode_create - create a new display mode
666 * Caller must hold DRM mode_config lock.
668 * Create a new drm_display_mode, give it an ID, and return it.
671 * Pointer to new mode on success, NULL on error.
673 struct drm_display_mode
*drm_mode_create(struct drm_device
*dev
)
675 struct drm_display_mode
*nmode
;
677 nmode
= kzalloc(sizeof(struct drm_display_mode
), GFP_KERNEL
);
681 if (drm_mode_object_get(dev
, &nmode
->base
, DRM_MODE_OBJECT_MODE
)) {
688 EXPORT_SYMBOL(drm_mode_create
);
691 * drm_mode_destroy - remove a mode
693 * @mode: mode to remove
696 * Caller must hold mode config lock.
698 * Free @mode's unique identifier, then free it.
700 void drm_mode_destroy(struct drm_device
*dev
, struct drm_display_mode
*mode
)
705 drm_mode_object_put(dev
, &mode
->base
);
709 EXPORT_SYMBOL(drm_mode_destroy
);
711 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
713 struct drm_property
*edid
;
714 struct drm_property
*dpms
;
717 * Standard properties (apply to all connectors)
719 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
720 DRM_MODE_PROP_IMMUTABLE
,
722 dev
->mode_config
.edid_property
= edid
;
724 dpms
= drm_property_create_enum(dev
, 0,
725 "DPMS", drm_dpms_enum_list
,
726 ARRAY_SIZE(drm_dpms_enum_list
));
727 dev
->mode_config
.dpms_property
= dpms
;
733 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
736 * Called by a driver the first time a DVI-I connector is made.
738 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
740 struct drm_property
*dvi_i_selector
;
741 struct drm_property
*dvi_i_subconnector
;
743 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
747 drm_property_create_enum(dev
, 0,
748 "select subconnector",
749 drm_dvi_i_select_enum_list
,
750 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
751 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
753 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
755 drm_dvi_i_subconnector_enum_list
,
756 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
757 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
761 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
764 * drm_create_tv_properties - create TV specific connector properties
766 * @num_modes: number of different TV formats (modes) supported
767 * @modes: array of pointers to strings containing name of each format
769 * Called by a driver's TV initialization routine, this function creates
770 * the TV specific connector properties for a given device. Caller is
771 * responsible for allocating a list of format names and passing them to
774 int drm_mode_create_tv_properties(struct drm_device
*dev
, int num_modes
,
777 struct drm_property
*tv_selector
;
778 struct drm_property
*tv_subconnector
;
781 if (dev
->mode_config
.tv_select_subconnector_property
)
785 * Basic connector properties
787 tv_selector
= drm_property_create_enum(dev
, 0,
788 "select subconnector",
789 drm_tv_select_enum_list
,
790 ARRAY_SIZE(drm_tv_select_enum_list
));
791 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
794 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
796 drm_tv_subconnector_enum_list
,
797 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
798 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
801 * Other, TV specific properties: margins & TV modes.
803 dev
->mode_config
.tv_left_margin_property
=
804 drm_property_create_range(dev
, 0, "left margin", 0, 100);
806 dev
->mode_config
.tv_right_margin_property
=
807 drm_property_create_range(dev
, 0, "right margin", 0, 100);
809 dev
->mode_config
.tv_top_margin_property
=
810 drm_property_create_range(dev
, 0, "top margin", 0, 100);
812 dev
->mode_config
.tv_bottom_margin_property
=
813 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
815 dev
->mode_config
.tv_mode_property
=
816 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
818 for (i
= 0; i
< num_modes
; i
++)
819 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
822 dev
->mode_config
.tv_brightness_property
=
823 drm_property_create_range(dev
, 0, "brightness", 0, 100);
825 dev
->mode_config
.tv_contrast_property
=
826 drm_property_create_range(dev
, 0, "contrast", 0, 100);
828 dev
->mode_config
.tv_flicker_reduction_property
=
829 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
831 dev
->mode_config
.tv_overscan_property
=
832 drm_property_create_range(dev
, 0, "overscan", 0, 100);
834 dev
->mode_config
.tv_saturation_property
=
835 drm_property_create_range(dev
, 0, "saturation", 0, 100);
837 dev
->mode_config
.tv_hue_property
=
838 drm_property_create_range(dev
, 0, "hue", 0, 100);
842 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
845 * drm_mode_create_scaling_mode_property - create scaling mode property
848 * Called by a driver the first time it's needed, must be attached to desired
851 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
853 struct drm_property
*scaling_mode
;
855 if (dev
->mode_config
.scaling_mode_property
)
859 drm_property_create_enum(dev
, 0, "scaling mode",
860 drm_scaling_mode_enum_list
,
861 ARRAY_SIZE(drm_scaling_mode_enum_list
));
863 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
867 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
870 * drm_mode_create_dithering_property - create dithering property
873 * Called by a driver the first time it's needed, must be attached to desired
876 int drm_mode_create_dithering_property(struct drm_device
*dev
)
878 struct drm_property
*dithering_mode
;
880 if (dev
->mode_config
.dithering_mode_property
)
884 drm_property_create_enum(dev
, 0, "dithering",
885 drm_dithering_mode_enum_list
,
886 ARRAY_SIZE(drm_dithering_mode_enum_list
));
887 dev
->mode_config
.dithering_mode_property
= dithering_mode
;
891 EXPORT_SYMBOL(drm_mode_create_dithering_property
);
894 * drm_mode_create_dirty_property - create dirty property
897 * Called by a driver the first time it's needed, must be attached to desired
900 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
902 struct drm_property
*dirty_info
;
904 if (dev
->mode_config
.dirty_info_property
)
908 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
910 drm_dirty_info_enum_list
,
911 ARRAY_SIZE(drm_dirty_info_enum_list
));
912 dev
->mode_config
.dirty_info_property
= dirty_info
;
916 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
919 * drm_mode_config_init - initialize DRM mode_configuration structure
923 * None, should happen single threaded at init time.
925 * Initialize @dev's mode_config structure, used for tracking the graphics
926 * configuration of @dev.
928 void drm_mode_config_init(struct drm_device
*dev
)
930 mutex_init(&dev
->mode_config
.mutex
);
931 mutex_init(&dev
->mode_config
.idr_mutex
);
932 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
933 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
934 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
935 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
936 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
937 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
938 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
939 idr_init(&dev
->mode_config
.crtc_idr
);
941 mutex_lock(&dev
->mode_config
.mutex
);
942 drm_mode_create_standard_connector_properties(dev
);
943 mutex_unlock(&dev
->mode_config
.mutex
);
945 /* Just to be sure */
946 dev
->mode_config
.num_fb
= 0;
947 dev
->mode_config
.num_connector
= 0;
948 dev
->mode_config
.num_crtc
= 0;
949 dev
->mode_config
.num_encoder
= 0;
951 EXPORT_SYMBOL(drm_mode_config_init
);
953 int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
955 uint32_t total_objects
= 0;
957 total_objects
+= dev
->mode_config
.num_crtc
;
958 total_objects
+= dev
->mode_config
.num_connector
;
959 total_objects
+= dev
->mode_config
.num_encoder
;
961 group
->id_list
= kzalloc(total_objects
* sizeof(uint32_t), GFP_KERNEL
);
965 group
->num_crtcs
= 0;
966 group
->num_connectors
= 0;
967 group
->num_encoders
= 0;
971 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
972 struct drm_mode_group
*group
)
974 struct drm_crtc
*crtc
;
975 struct drm_encoder
*encoder
;
976 struct drm_connector
*connector
;
979 if ((ret
= drm_mode_group_init(dev
, group
)))
982 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
983 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
985 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
986 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
989 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
990 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
991 group
->num_connectors
++] = connector
->base
.id
;
995 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
998 * drm_mode_config_cleanup - free up DRM mode_config info
1002 * Caller must hold mode config lock.
1004 * Free up all the connectors and CRTCs associated with this DRM device, then
1005 * free up the framebuffers and associated buffer objects.
1007 * FIXME: cleanup any dangling user buffer objects too
1009 void drm_mode_config_cleanup(struct drm_device
*dev
)
1011 struct drm_connector
*connector
, *ot
;
1012 struct drm_crtc
*crtc
, *ct
;
1013 struct drm_encoder
*encoder
, *enct
;
1014 struct drm_framebuffer
*fb
, *fbt
;
1015 struct drm_property
*property
, *pt
;
1016 struct drm_plane
*plane
, *plt
;
1018 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
1020 encoder
->funcs
->destroy(encoder
);
1023 list_for_each_entry_safe(connector
, ot
,
1024 &dev
->mode_config
.connector_list
, head
) {
1025 connector
->funcs
->destroy(connector
);
1028 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
1030 drm_property_destroy(dev
, property
);
1033 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
1034 fb
->funcs
->destroy(fb
);
1037 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
1039 plane
->funcs
->destroy(plane
);
1042 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
1043 crtc
->funcs
->destroy(crtc
);
1046 idr_remove_all(&dev
->mode_config
.crtc_idr
);
1047 idr_destroy(&dev
->mode_config
.crtc_idr
);
1049 EXPORT_SYMBOL(drm_mode_config_cleanup
);
1052 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1053 * @out: drm_mode_modeinfo struct to return to the user
1054 * @in: drm_display_mode to use
1059 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1062 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1063 const struct drm_display_mode
*in
)
1065 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1066 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1067 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1068 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1069 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1070 "timing values too large for mode info\n");
1072 out
->clock
= in
->clock
;
1073 out
->hdisplay
= in
->hdisplay
;
1074 out
->hsync_start
= in
->hsync_start
;
1075 out
->hsync_end
= in
->hsync_end
;
1076 out
->htotal
= in
->htotal
;
1077 out
->hskew
= in
->hskew
;
1078 out
->vdisplay
= in
->vdisplay
;
1079 out
->vsync_start
= in
->vsync_start
;
1080 out
->vsync_end
= in
->vsync_end
;
1081 out
->vtotal
= in
->vtotal
;
1082 out
->vscan
= in
->vscan
;
1083 out
->vrefresh
= in
->vrefresh
;
1084 out
->flags
= in
->flags
;
1085 out
->type
= in
->type
;
1086 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1087 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1091 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1092 * @out: drm_display_mode to return to the user
1093 * @in: drm_mode_modeinfo to use
1098 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1102 * Zero on success, errno on failure.
1104 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1105 const struct drm_mode_modeinfo
*in
)
1107 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1110 out
->clock
= in
->clock
;
1111 out
->hdisplay
= in
->hdisplay
;
1112 out
->hsync_start
= in
->hsync_start
;
1113 out
->hsync_end
= in
->hsync_end
;
1114 out
->htotal
= in
->htotal
;
1115 out
->hskew
= in
->hskew
;
1116 out
->vdisplay
= in
->vdisplay
;
1117 out
->vsync_start
= in
->vsync_start
;
1118 out
->vsync_end
= in
->vsync_end
;
1119 out
->vtotal
= in
->vtotal
;
1120 out
->vscan
= in
->vscan
;
1121 out
->vrefresh
= in
->vrefresh
;
1122 out
->flags
= in
->flags
;
1123 out
->type
= in
->type
;
1124 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1125 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1131 * drm_mode_getresources - get graphics configuration
1132 * @inode: inode from the ioctl
1133 * @filp: file * from the ioctl
1134 * @cmd: cmd from ioctl
1135 * @arg: arg from ioctl
1138 * Takes mode config lock.
1140 * Construct a set of configuration description structures and return
1141 * them to the user, including CRTC, connector and framebuffer configuration.
1143 * Called by the user via ioctl.
1146 * Zero on success, errno on failure.
1148 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1149 struct drm_file
*file_priv
)
1151 struct drm_mode_card_res
*card_res
= data
;
1152 struct list_head
*lh
;
1153 struct drm_framebuffer
*fb
;
1154 struct drm_connector
*connector
;
1155 struct drm_crtc
*crtc
;
1156 struct drm_encoder
*encoder
;
1158 int connector_count
= 0;
1161 int encoder_count
= 0;
1163 uint32_t __user
*fb_id
;
1164 uint32_t __user
*crtc_id
;
1165 uint32_t __user
*connector_id
;
1166 uint32_t __user
*encoder_id
;
1167 struct drm_mode_group
*mode_group
;
1169 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1172 mutex_lock(&dev
->mode_config
.mutex
);
1175 * For the non-control nodes we need to limit the list of resources
1176 * by IDs in the group list for this node
1178 list_for_each(lh
, &file_priv
->fbs
)
1181 mode_group
= &file_priv
->master
->minor
->mode_group
;
1182 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1184 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1187 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1190 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1194 crtc_count
= mode_group
->num_crtcs
;
1195 connector_count
= mode_group
->num_connectors
;
1196 encoder_count
= mode_group
->num_encoders
;
1199 card_res
->max_height
= dev
->mode_config
.max_height
;
1200 card_res
->min_height
= dev
->mode_config
.min_height
;
1201 card_res
->max_width
= dev
->mode_config
.max_width
;
1202 card_res
->min_width
= dev
->mode_config
.min_width
;
1204 /* handle this in 4 parts */
1206 if (card_res
->count_fbs
>= fb_count
) {
1208 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1209 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1210 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1217 card_res
->count_fbs
= fb_count
;
1220 if (card_res
->count_crtcs
>= crtc_count
) {
1222 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1223 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1224 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1226 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1227 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1234 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1235 if (put_user(mode_group
->id_list
[i
],
1236 crtc_id
+ copied
)) {
1244 card_res
->count_crtcs
= crtc_count
;
1247 if (card_res
->count_encoders
>= encoder_count
) {
1249 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1250 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1251 list_for_each_entry(encoder
,
1252 &dev
->mode_config
.encoder_list
,
1254 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1255 drm_get_encoder_name(encoder
));
1256 if (put_user(encoder
->base
.id
, encoder_id
+
1264 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1265 if (put_user(mode_group
->id_list
[i
],
1266 encoder_id
+ copied
)) {
1275 card_res
->count_encoders
= encoder_count
;
1278 if (card_res
->count_connectors
>= connector_count
) {
1280 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1281 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1282 list_for_each_entry(connector
,
1283 &dev
->mode_config
.connector_list
,
1285 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1287 drm_get_connector_name(connector
));
1288 if (put_user(connector
->base
.id
,
1289 connector_id
+ copied
)) {
1296 int start
= mode_group
->num_crtcs
+
1297 mode_group
->num_encoders
;
1298 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1299 if (put_user(mode_group
->id_list
[i
],
1300 connector_id
+ copied
)) {
1308 card_res
->count_connectors
= connector_count
;
1310 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1311 card_res
->count_connectors
, card_res
->count_encoders
);
1314 mutex_unlock(&dev
->mode_config
.mutex
);
1319 * drm_mode_getcrtc - get CRTC configuration
1320 * @inode: inode from the ioctl
1321 * @filp: file * from the ioctl
1322 * @cmd: cmd from ioctl
1323 * @arg: arg from ioctl
1326 * Takes mode config lock.
1328 * Construct a CRTC configuration structure to return to the user.
1330 * Called by the user via ioctl.
1333 * Zero on success, errno on failure.
1335 int drm_mode_getcrtc(struct drm_device
*dev
,
1336 void *data
, struct drm_file
*file_priv
)
1338 struct drm_mode_crtc
*crtc_resp
= data
;
1339 struct drm_crtc
*crtc
;
1340 struct drm_mode_object
*obj
;
1343 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1346 mutex_lock(&dev
->mode_config
.mutex
);
1348 obj
= drm_mode_object_find(dev
, crtc_resp
->crtc_id
,
1349 DRM_MODE_OBJECT_CRTC
);
1354 crtc
= obj_to_crtc(obj
);
1356 crtc_resp
->x
= crtc
->x
;
1357 crtc_resp
->y
= crtc
->y
;
1358 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1360 crtc_resp
->fb_id
= crtc
->fb
->base
.id
;
1362 crtc_resp
->fb_id
= 0;
1364 if (crtc
->enabled
) {
1366 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1367 crtc_resp
->mode_valid
= 1;
1370 crtc_resp
->mode_valid
= 0;
1374 mutex_unlock(&dev
->mode_config
.mutex
);
1379 * drm_mode_getconnector - get connector configuration
1380 * @inode: inode from the ioctl
1381 * @filp: file * from the ioctl
1382 * @cmd: cmd from ioctl
1383 * @arg: arg from ioctl
1386 * Takes mode config lock.
1388 * Construct a connector configuration structure to return to the user.
1390 * Called by the user via ioctl.
1393 * Zero on success, errno on failure.
1395 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
1396 struct drm_file
*file_priv
)
1398 struct drm_mode_get_connector
*out_resp
= data
;
1399 struct drm_mode_object
*obj
;
1400 struct drm_connector
*connector
;
1401 struct drm_display_mode
*mode
;
1403 int props_count
= 0;
1404 int encoders_count
= 0;
1408 struct drm_mode_modeinfo u_mode
;
1409 struct drm_mode_modeinfo __user
*mode_ptr
;
1410 uint32_t __user
*prop_ptr
;
1411 uint64_t __user
*prop_values
;
1412 uint32_t __user
*encoder_ptr
;
1414 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1417 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
1419 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
1421 mutex_lock(&dev
->mode_config
.mutex
);
1423 obj
= drm_mode_object_find(dev
, out_resp
->connector_id
,
1424 DRM_MODE_OBJECT_CONNECTOR
);
1429 connector
= obj_to_connector(obj
);
1431 props_count
= connector
->properties
.count
;
1433 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1434 if (connector
->encoder_ids
[i
] != 0) {
1439 if (out_resp
->count_modes
== 0) {
1440 connector
->funcs
->fill_modes(connector
,
1441 dev
->mode_config
.max_width
,
1442 dev
->mode_config
.max_height
);
1445 /* delayed so we get modes regardless of pre-fill_modes state */
1446 list_for_each_entry(mode
, &connector
->modes
, head
)
1449 out_resp
->connector_id
= connector
->base
.id
;
1450 out_resp
->connector_type
= connector
->connector_type
;
1451 out_resp
->connector_type_id
= connector
->connector_type_id
;
1452 out_resp
->mm_width
= connector
->display_info
.width_mm
;
1453 out_resp
->mm_height
= connector
->display_info
.height_mm
;
1454 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
1455 out_resp
->connection
= connector
->status
;
1456 if (connector
->encoder
)
1457 out_resp
->encoder_id
= connector
->encoder
->base
.id
;
1459 out_resp
->encoder_id
= 0;
1462 * This ioctl is called twice, once to determine how much space is
1463 * needed, and the 2nd time to fill it.
1465 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
1467 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
1468 list_for_each_entry(mode
, &connector
->modes
, head
) {
1469 drm_crtc_convert_to_umode(&u_mode
, mode
);
1470 if (copy_to_user(mode_ptr
+ copied
,
1471 &u_mode
, sizeof(u_mode
))) {
1478 out_resp
->count_modes
= mode_count
;
1480 if ((out_resp
->count_props
>= props_count
) && props_count
) {
1482 prop_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
);
1483 prop_values
= (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
);
1484 for (i
= 0; i
< connector
->properties
.count
; i
++) {
1485 if (put_user(connector
->properties
.ids
[i
],
1486 prop_ptr
+ copied
)) {
1491 if (put_user(connector
->properties
.values
[i
],
1492 prop_values
+ copied
)) {
1499 out_resp
->count_props
= props_count
;
1501 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
1503 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
1504 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1505 if (connector
->encoder_ids
[i
] != 0) {
1506 if (put_user(connector
->encoder_ids
[i
],
1507 encoder_ptr
+ copied
)) {
1515 out_resp
->count_encoders
= encoders_count
;
1518 mutex_unlock(&dev
->mode_config
.mutex
);
1522 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
1523 struct drm_file
*file_priv
)
1525 struct drm_mode_get_encoder
*enc_resp
= data
;
1526 struct drm_mode_object
*obj
;
1527 struct drm_encoder
*encoder
;
1530 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1533 mutex_lock(&dev
->mode_config
.mutex
);
1534 obj
= drm_mode_object_find(dev
, enc_resp
->encoder_id
,
1535 DRM_MODE_OBJECT_ENCODER
);
1540 encoder
= obj_to_encoder(obj
);
1543 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
1545 enc_resp
->crtc_id
= 0;
1546 enc_resp
->encoder_type
= encoder
->encoder_type
;
1547 enc_resp
->encoder_id
= encoder
->base
.id
;
1548 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
1549 enc_resp
->possible_clones
= encoder
->possible_clones
;
1552 mutex_unlock(&dev
->mode_config
.mutex
);
1557 * drm_mode_getplane_res - get plane info
1560 * @file_priv: DRM file info
1563 * Takes mode config lock.
1565 * Return an plane count and set of IDs.
1567 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
1568 struct drm_file
*file_priv
)
1570 struct drm_mode_get_plane_res
*plane_resp
= data
;
1571 struct drm_mode_config
*config
;
1572 struct drm_plane
*plane
;
1573 uint32_t __user
*plane_ptr
;
1574 int copied
= 0, ret
= 0;
1576 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1579 mutex_lock(&dev
->mode_config
.mutex
);
1580 config
= &dev
->mode_config
;
1583 * This ioctl is called twice, once to determine how much space is
1584 * needed, and the 2nd time to fill it.
1586 if (config
->num_plane
&&
1587 (plane_resp
->count_planes
>= config
->num_plane
)) {
1588 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
1590 list_for_each_entry(plane
, &config
->plane_list
, head
) {
1591 if (put_user(plane
->base
.id
, plane_ptr
+ copied
)) {
1598 plane_resp
->count_planes
= config
->num_plane
;
1601 mutex_unlock(&dev
->mode_config
.mutex
);
1606 * drm_mode_getplane - get plane info
1609 * @file_priv: DRM file info
1612 * Takes mode config lock.
1614 * Return plane info, including formats supported, gamma size, any
1617 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
1618 struct drm_file
*file_priv
)
1620 struct drm_mode_get_plane
*plane_resp
= data
;
1621 struct drm_mode_object
*obj
;
1622 struct drm_plane
*plane
;
1623 uint32_t __user
*format_ptr
;
1626 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1629 mutex_lock(&dev
->mode_config
.mutex
);
1630 obj
= drm_mode_object_find(dev
, plane_resp
->plane_id
,
1631 DRM_MODE_OBJECT_PLANE
);
1636 plane
= obj_to_plane(obj
);
1639 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
1641 plane_resp
->crtc_id
= 0;
1644 plane_resp
->fb_id
= plane
->fb
->base
.id
;
1646 plane_resp
->fb_id
= 0;
1648 plane_resp
->plane_id
= plane
->base
.id
;
1649 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
1650 plane_resp
->gamma_size
= plane
->gamma_size
;
1653 * This ioctl is called twice, once to determine how much space is
1654 * needed, and the 2nd time to fill it.
1656 if (plane
->format_count
&&
1657 (plane_resp
->count_format_types
>= plane
->format_count
)) {
1658 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
1659 if (copy_to_user(format_ptr
,
1660 plane
->format_types
,
1661 sizeof(uint32_t) * plane
->format_count
)) {
1666 plane_resp
->count_format_types
= plane
->format_count
;
1669 mutex_unlock(&dev
->mode_config
.mutex
);
1674 * drm_mode_setplane - set up or tear down an plane
1676 * @data: ioctl data*
1677 * @file_prive: DRM file info
1680 * Takes mode config lock.
1682 * Set plane info, including placement, fb, scaling, and other factors.
1683 * Or pass a NULL fb to disable.
1685 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
1686 struct drm_file
*file_priv
)
1688 struct drm_mode_set_plane
*plane_req
= data
;
1689 struct drm_mode_object
*obj
;
1690 struct drm_plane
*plane
;
1691 struct drm_crtc
*crtc
;
1692 struct drm_framebuffer
*fb
;
1694 unsigned int fb_width
, fb_height
;
1697 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1700 mutex_lock(&dev
->mode_config
.mutex
);
1703 * First, find the plane, crtc, and fb objects. If not available,
1704 * we don't bother to call the driver.
1706 obj
= drm_mode_object_find(dev
, plane_req
->plane_id
,
1707 DRM_MODE_OBJECT_PLANE
);
1709 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1710 plane_req
->plane_id
);
1714 plane
= obj_to_plane(obj
);
1716 /* No fb means shut it down */
1717 if (!plane_req
->fb_id
) {
1718 plane
->funcs
->disable_plane(plane
);
1724 obj
= drm_mode_object_find(dev
, plane_req
->crtc_id
,
1725 DRM_MODE_OBJECT_CRTC
);
1727 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1728 plane_req
->crtc_id
);
1732 crtc
= obj_to_crtc(obj
);
1734 obj
= drm_mode_object_find(dev
, plane_req
->fb_id
,
1735 DRM_MODE_OBJECT_FB
);
1737 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1742 fb
= obj_to_fb(obj
);
1744 /* Check whether this plane supports the fb pixel format. */
1745 for (i
= 0; i
< plane
->format_count
; i
++)
1746 if (fb
->pixel_format
== plane
->format_types
[i
])
1748 if (i
== plane
->format_count
) {
1749 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb
->pixel_format
);
1754 fb_width
= fb
->width
<< 16;
1755 fb_height
= fb
->height
<< 16;
1757 /* Make sure source coordinates are inside the fb. */
1758 if (plane_req
->src_w
> fb_width
||
1759 plane_req
->src_x
> fb_width
- plane_req
->src_w
||
1760 plane_req
->src_h
> fb_height
||
1761 plane_req
->src_y
> fb_height
- plane_req
->src_h
) {
1762 DRM_DEBUG_KMS("Invalid source coordinates "
1763 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1764 plane_req
->src_w
>> 16,
1765 ((plane_req
->src_w
& 0xffff) * 15625) >> 10,
1766 plane_req
->src_h
>> 16,
1767 ((plane_req
->src_h
& 0xffff) * 15625) >> 10,
1768 plane_req
->src_x
>> 16,
1769 ((plane_req
->src_x
& 0xffff) * 15625) >> 10,
1770 plane_req
->src_y
>> 16,
1771 ((plane_req
->src_y
& 0xffff) * 15625) >> 10);
1776 /* Give drivers some help against integer overflows */
1777 if (plane_req
->crtc_w
> INT_MAX
||
1778 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
1779 plane_req
->crtc_h
> INT_MAX
||
1780 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
1781 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1782 plane_req
->crtc_w
, plane_req
->crtc_h
,
1783 plane_req
->crtc_x
, plane_req
->crtc_y
);
1788 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
1789 plane_req
->crtc_x
, plane_req
->crtc_y
,
1790 plane_req
->crtc_w
, plane_req
->crtc_h
,
1791 plane_req
->src_x
, plane_req
->src_y
,
1792 plane_req
->src_w
, plane_req
->src_h
);
1799 mutex_unlock(&dev
->mode_config
.mutex
);
1805 * drm_mode_setcrtc - set CRTC configuration
1806 * @inode: inode from the ioctl
1807 * @filp: file * from the ioctl
1808 * @cmd: cmd from ioctl
1809 * @arg: arg from ioctl
1812 * Takes mode config lock.
1814 * Build a new CRTC configuration based on user request.
1816 * Called by the user via ioctl.
1819 * Zero on success, errno on failure.
1821 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
1822 struct drm_file
*file_priv
)
1824 struct drm_mode_config
*config
= &dev
->mode_config
;
1825 struct drm_mode_crtc
*crtc_req
= data
;
1826 struct drm_mode_object
*obj
;
1827 struct drm_crtc
*crtc
;
1828 struct drm_connector
**connector_set
= NULL
, *connector
;
1829 struct drm_framebuffer
*fb
= NULL
;
1830 struct drm_display_mode
*mode
= NULL
;
1831 struct drm_mode_set set
;
1832 uint32_t __user
*set_connectors_ptr
;
1836 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1839 /* For some reason crtc x/y offsets are signed internally. */
1840 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
1843 mutex_lock(&dev
->mode_config
.mutex
);
1844 obj
= drm_mode_object_find(dev
, crtc_req
->crtc_id
,
1845 DRM_MODE_OBJECT_CRTC
);
1847 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
1851 crtc
= obj_to_crtc(obj
);
1852 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1854 if (crtc_req
->mode_valid
) {
1855 /* If we have a mode we need a framebuffer. */
1856 /* If we pass -1, set the mode with the currently bound fb */
1857 if (crtc_req
->fb_id
== -1) {
1859 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1865 obj
= drm_mode_object_find(dev
, crtc_req
->fb_id
,
1866 DRM_MODE_OBJECT_FB
);
1868 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1873 fb
= obj_to_fb(obj
);
1876 mode
= drm_mode_create(dev
);
1882 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
1884 DRM_DEBUG_KMS("Invalid mode\n");
1888 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
1890 if (mode
->hdisplay
> fb
->width
||
1891 mode
->vdisplay
> fb
->height
||
1892 crtc_req
->x
> fb
->width
- mode
->hdisplay
||
1893 crtc_req
->y
> fb
->height
- mode
->vdisplay
) {
1894 DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
1895 mode
->hdisplay
, mode
->vdisplay
,
1896 crtc_req
->x
, crtc_req
->y
,
1897 fb
->width
, fb
->height
);
1903 if (crtc_req
->count_connectors
== 0 && mode
) {
1904 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1909 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
1910 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1911 crtc_req
->count_connectors
);
1916 if (crtc_req
->count_connectors
> 0) {
1919 /* Avoid unbounded kernel memory allocation */
1920 if (crtc_req
->count_connectors
> config
->num_connector
) {
1925 connector_set
= kmalloc(crtc_req
->count_connectors
*
1926 sizeof(struct drm_connector
*),
1928 if (!connector_set
) {
1933 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
1934 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
1935 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
1940 obj
= drm_mode_object_find(dev
, out_id
,
1941 DRM_MODE_OBJECT_CONNECTOR
);
1943 DRM_DEBUG_KMS("Connector id %d unknown\n",
1948 connector
= obj_to_connector(obj
);
1949 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1951 drm_get_connector_name(connector
));
1953 connector_set
[i
] = connector
;
1958 set
.x
= crtc_req
->x
;
1959 set
.y
= crtc_req
->y
;
1961 set
.connectors
= connector_set
;
1962 set
.num_connectors
= crtc_req
->count_connectors
;
1964 ret
= crtc
->funcs
->set_config(&set
);
1967 kfree(connector_set
);
1968 drm_mode_destroy(dev
, mode
);
1969 mutex_unlock(&dev
->mode_config
.mutex
);
1973 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
1974 void *data
, struct drm_file
*file_priv
)
1976 struct drm_mode_cursor
*req
= data
;
1977 struct drm_mode_object
*obj
;
1978 struct drm_crtc
*crtc
;
1981 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1984 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
1987 mutex_lock(&dev
->mode_config
.mutex
);
1988 obj
= drm_mode_object_find(dev
, req
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
1990 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
1994 crtc
= obj_to_crtc(obj
);
1996 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
1997 if (!crtc
->funcs
->cursor_set
) {
2001 /* Turns off the cursor if handle is 0 */
2002 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2003 req
->width
, req
->height
);
2006 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2007 if (crtc
->funcs
->cursor_move
) {
2008 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2015 mutex_unlock(&dev
->mode_config
.mutex
);
2019 /* Original addfb only supported RGB formats, so figure out which one */
2020 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2026 fmt
= DRM_FORMAT_RGB332
;
2030 fmt
= DRM_FORMAT_XRGB1555
;
2032 fmt
= DRM_FORMAT_RGB565
;
2035 fmt
= DRM_FORMAT_RGB888
;
2039 fmt
= DRM_FORMAT_XRGB8888
;
2040 else if (depth
== 30)
2041 fmt
= DRM_FORMAT_XRGB2101010
;
2043 fmt
= DRM_FORMAT_ARGB8888
;
2046 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2047 fmt
= DRM_FORMAT_XRGB8888
;
2053 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2056 * drm_mode_addfb - add an FB to the graphics configuration
2057 * @inode: inode from the ioctl
2058 * @filp: file * from the ioctl
2059 * @cmd: cmd from ioctl
2060 * @arg: arg from ioctl
2063 * Takes mode config lock.
2065 * Add a new FB to the specified CRTC, given a user request.
2067 * Called by the user via ioctl.
2070 * Zero on success, errno on failure.
2072 int drm_mode_addfb(struct drm_device
*dev
,
2073 void *data
, struct drm_file
*file_priv
)
2075 struct drm_mode_fb_cmd
*or = data
;
2076 struct drm_mode_fb_cmd2 r
= {};
2077 struct drm_mode_config
*config
= &dev
->mode_config
;
2078 struct drm_framebuffer
*fb
;
2081 /* Use new struct with format internally */
2082 r
.fb_id
= or->fb_id
;
2083 r
.width
= or->width
;
2084 r
.height
= or->height
;
2085 r
.pitches
[0] = or->pitch
;
2086 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2087 r
.handles
[0] = or->handle
;
2089 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2092 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2095 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2098 mutex_lock(&dev
->mode_config
.mutex
);
2100 /* TODO check buffer is sufficiently large */
2101 /* TODO setup destructor callback */
2103 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2105 DRM_DEBUG_KMS("could not create framebuffer\n");
2110 or->fb_id
= fb
->base
.id
;
2111 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2112 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2115 mutex_unlock(&dev
->mode_config
.mutex
);
2119 static int format_check(const struct drm_mode_fb_cmd2
*r
)
2121 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2125 case DRM_FORMAT_RGB332
:
2126 case DRM_FORMAT_BGR233
:
2127 case DRM_FORMAT_XRGB4444
:
2128 case DRM_FORMAT_XBGR4444
:
2129 case DRM_FORMAT_RGBX4444
:
2130 case DRM_FORMAT_BGRX4444
:
2131 case DRM_FORMAT_ARGB4444
:
2132 case DRM_FORMAT_ABGR4444
:
2133 case DRM_FORMAT_RGBA4444
:
2134 case DRM_FORMAT_BGRA4444
:
2135 case DRM_FORMAT_XRGB1555
:
2136 case DRM_FORMAT_XBGR1555
:
2137 case DRM_FORMAT_RGBX5551
:
2138 case DRM_FORMAT_BGRX5551
:
2139 case DRM_FORMAT_ARGB1555
:
2140 case DRM_FORMAT_ABGR1555
:
2141 case DRM_FORMAT_RGBA5551
:
2142 case DRM_FORMAT_BGRA5551
:
2143 case DRM_FORMAT_RGB565
:
2144 case DRM_FORMAT_BGR565
:
2145 case DRM_FORMAT_RGB888
:
2146 case DRM_FORMAT_BGR888
:
2147 case DRM_FORMAT_XRGB8888
:
2148 case DRM_FORMAT_XBGR8888
:
2149 case DRM_FORMAT_RGBX8888
:
2150 case DRM_FORMAT_BGRX8888
:
2151 case DRM_FORMAT_ARGB8888
:
2152 case DRM_FORMAT_ABGR8888
:
2153 case DRM_FORMAT_RGBA8888
:
2154 case DRM_FORMAT_BGRA8888
:
2155 case DRM_FORMAT_XRGB2101010
:
2156 case DRM_FORMAT_XBGR2101010
:
2157 case DRM_FORMAT_RGBX1010102
:
2158 case DRM_FORMAT_BGRX1010102
:
2159 case DRM_FORMAT_ARGB2101010
:
2160 case DRM_FORMAT_ABGR2101010
:
2161 case DRM_FORMAT_RGBA1010102
:
2162 case DRM_FORMAT_BGRA1010102
:
2163 case DRM_FORMAT_YUYV
:
2164 case DRM_FORMAT_YVYU
:
2165 case DRM_FORMAT_UYVY
:
2166 case DRM_FORMAT_VYUY
:
2167 case DRM_FORMAT_AYUV
:
2168 case DRM_FORMAT_NV12
:
2169 case DRM_FORMAT_NV21
:
2170 case DRM_FORMAT_NV16
:
2171 case DRM_FORMAT_NV61
:
2172 case DRM_FORMAT_YUV410
:
2173 case DRM_FORMAT_YVU410
:
2174 case DRM_FORMAT_YUV411
:
2175 case DRM_FORMAT_YVU411
:
2176 case DRM_FORMAT_YUV420
:
2177 case DRM_FORMAT_YVU420
:
2178 case DRM_FORMAT_YUV422
:
2179 case DRM_FORMAT_YVU422
:
2180 case DRM_FORMAT_YUV444
:
2181 case DRM_FORMAT_YVU444
:
2188 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
2190 int ret
, hsub
, vsub
, num_planes
, i
;
2192 ret
= format_check(r
);
2194 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r
->pixel_format
);
2198 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
2199 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
2200 num_planes
= drm_format_num_planes(r
->pixel_format
);
2202 if (r
->width
== 0 || r
->width
% hsub
) {
2203 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
2207 if (r
->height
== 0 || r
->height
% vsub
) {
2208 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
2212 for (i
= 0; i
< num_planes
; i
++) {
2213 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
2215 if (!r
->handles
[i
]) {
2216 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
2220 if (r
->pitches
[i
] < drm_format_plane_cpp(r
->pixel_format
, i
) * width
) {
2221 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
2230 * drm_mode_addfb2 - add an FB to the graphics configuration
2231 * @inode: inode from the ioctl
2232 * @filp: file * from the ioctl
2233 * @cmd: cmd from ioctl
2234 * @arg: arg from ioctl
2237 * Takes mode config lock.
2239 * Add a new FB to the specified CRTC, given a user request with format.
2241 * Called by the user via ioctl.
2244 * Zero on success, errno on failure.
2246 int drm_mode_addfb2(struct drm_device
*dev
,
2247 void *data
, struct drm_file
*file_priv
)
2249 struct drm_mode_fb_cmd2
*r
= data
;
2250 struct drm_mode_config
*config
= &dev
->mode_config
;
2251 struct drm_framebuffer
*fb
;
2254 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2257 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
2258 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2259 r
->width
, config
->min_width
, config
->max_width
);
2262 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
2263 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2264 r
->height
, config
->min_height
, config
->max_height
);
2268 ret
= framebuffer_check(r
);
2272 mutex_lock(&dev
->mode_config
.mutex
);
2274 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
2276 DRM_DEBUG_KMS("could not create framebuffer\n");
2281 r
->fb_id
= fb
->base
.id
;
2282 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2283 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2286 mutex_unlock(&dev
->mode_config
.mutex
);
2291 * drm_mode_rmfb - remove an FB from the configuration
2292 * @inode: inode from the ioctl
2293 * @filp: file * from the ioctl
2294 * @cmd: cmd from ioctl
2295 * @arg: arg from ioctl
2298 * Takes mode config lock.
2300 * Remove the FB specified by the user.
2302 * Called by the user via ioctl.
2305 * Zero on success, errno on failure.
2307 int drm_mode_rmfb(struct drm_device
*dev
,
2308 void *data
, struct drm_file
*file_priv
)
2310 struct drm_mode_object
*obj
;
2311 struct drm_framebuffer
*fb
= NULL
;
2312 struct drm_framebuffer
*fbl
= NULL
;
2313 uint32_t *id
= data
;
2317 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2320 mutex_lock(&dev
->mode_config
.mutex
);
2321 obj
= drm_mode_object_find(dev
, *id
, DRM_MODE_OBJECT_FB
);
2322 /* TODO check that we really get a framebuffer back. */
2327 fb
= obj_to_fb(obj
);
2329 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
2338 /* TODO release all crtc connected to the framebuffer */
2339 /* TODO unhock the destructor from the buffer object */
2341 list_del(&fb
->filp_head
);
2342 fb
->funcs
->destroy(fb
);
2345 mutex_unlock(&dev
->mode_config
.mutex
);
2350 * drm_mode_getfb - get FB info
2351 * @inode: inode from the ioctl
2352 * @filp: file * from the ioctl
2353 * @cmd: cmd from ioctl
2354 * @arg: arg from ioctl
2357 * Takes mode config lock.
2359 * Lookup the FB given its ID and return info about it.
2361 * Called by the user via ioctl.
2364 * Zero on success, errno on failure.
2366 int drm_mode_getfb(struct drm_device
*dev
,
2367 void *data
, struct drm_file
*file_priv
)
2369 struct drm_mode_fb_cmd
*r
= data
;
2370 struct drm_mode_object
*obj
;
2371 struct drm_framebuffer
*fb
;
2374 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2377 mutex_lock(&dev
->mode_config
.mutex
);
2378 obj
= drm_mode_object_find(dev
, r
->fb_id
, DRM_MODE_OBJECT_FB
);
2383 fb
= obj_to_fb(obj
);
2385 r
->height
= fb
->height
;
2386 r
->width
= fb
->width
;
2387 r
->depth
= fb
->depth
;
2388 r
->bpp
= fb
->bits_per_pixel
;
2389 r
->pitch
= fb
->pitches
[0];
2390 fb
->funcs
->create_handle(fb
, file_priv
, &r
->handle
);
2393 mutex_unlock(&dev
->mode_config
.mutex
);
2397 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
2398 void *data
, struct drm_file
*file_priv
)
2400 struct drm_clip_rect __user
*clips_ptr
;
2401 struct drm_clip_rect
*clips
= NULL
;
2402 struct drm_mode_fb_dirty_cmd
*r
= data
;
2403 struct drm_mode_object
*obj
;
2404 struct drm_framebuffer
*fb
;
2409 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2412 mutex_lock(&dev
->mode_config
.mutex
);
2413 obj
= drm_mode_object_find(dev
, r
->fb_id
, DRM_MODE_OBJECT_FB
);
2418 fb
= obj_to_fb(obj
);
2420 num_clips
= r
->num_clips
;
2421 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
2423 if (!num_clips
!= !clips_ptr
) {
2428 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
2430 /* If userspace annotates copy, clips must come in pairs */
2431 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
2436 if (num_clips
&& clips_ptr
) {
2437 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
2441 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
2447 ret
= copy_from_user(clips
, clips_ptr
,
2448 num_clips
* sizeof(*clips
));
2455 if (fb
->funcs
->dirty
) {
2456 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
2466 mutex_unlock(&dev
->mode_config
.mutex
);
2472 * drm_fb_release - remove and free the FBs on this file
2473 * @filp: file * from the ioctl
2476 * Takes mode config lock.
2478 * Destroy all the FBs associated with @filp.
2480 * Called by the user via ioctl.
2483 * Zero on success, errno on failure.
2485 void drm_fb_release(struct drm_file
*priv
)
2487 struct drm_device
*dev
= priv
->minor
->dev
;
2488 struct drm_framebuffer
*fb
, *tfb
;
2490 mutex_lock(&dev
->mode_config
.mutex
);
2491 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
2492 list_del(&fb
->filp_head
);
2493 fb
->funcs
->destroy(fb
);
2495 mutex_unlock(&dev
->mode_config
.mutex
);
2499 * drm_mode_attachmode - add a mode to the user mode list
2501 * @connector: connector to add the mode to
2502 * @mode: mode to add
2504 * Add @mode to @connector's user mode list.
2506 static void drm_mode_attachmode(struct drm_device
*dev
,
2507 struct drm_connector
*connector
,
2508 struct drm_display_mode
*mode
)
2510 list_add_tail(&mode
->head
, &connector
->user_modes
);
2513 int drm_mode_attachmode_crtc(struct drm_device
*dev
, struct drm_crtc
*crtc
,
2514 const struct drm_display_mode
*mode
)
2516 struct drm_connector
*connector
;
2518 struct drm_display_mode
*dup_mode
, *next
;
2521 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
2522 if (!connector
->encoder
)
2524 if (connector
->encoder
->crtc
== crtc
) {
2525 dup_mode
= drm_mode_duplicate(dev
, mode
);
2530 list_add_tail(&dup_mode
->head
, &list
);
2534 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
2535 if (!connector
->encoder
)
2537 if (connector
->encoder
->crtc
== crtc
)
2538 list_move_tail(list
.next
, &connector
->user_modes
);
2541 WARN_ON(!list_empty(&list
));
2544 list_for_each_entry_safe(dup_mode
, next
, &list
, head
)
2545 drm_mode_destroy(dev
, dup_mode
);
2549 EXPORT_SYMBOL(drm_mode_attachmode_crtc
);
2551 static int drm_mode_detachmode(struct drm_device
*dev
,
2552 struct drm_connector
*connector
,
2553 struct drm_display_mode
*mode
)
2557 struct drm_display_mode
*match_mode
, *t
;
2559 list_for_each_entry_safe(match_mode
, t
, &connector
->user_modes
, head
) {
2560 if (drm_mode_equal(match_mode
, mode
)) {
2561 list_del(&match_mode
->head
);
2562 drm_mode_destroy(dev
, match_mode
);
2574 int drm_mode_detachmode_crtc(struct drm_device
*dev
, struct drm_display_mode
*mode
)
2576 struct drm_connector
*connector
;
2578 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
2579 drm_mode_detachmode(dev
, connector
, mode
);
2583 EXPORT_SYMBOL(drm_mode_detachmode_crtc
);
2586 * drm_fb_attachmode - Attach a user mode to an connector
2587 * @inode: inode from the ioctl
2588 * @filp: file * from the ioctl
2589 * @cmd: cmd from ioctl
2590 * @arg: arg from ioctl
2592 * This attaches a user specified mode to an connector.
2593 * Called by the user via ioctl.
2596 * Zero on success, errno on failure.
2598 int drm_mode_attachmode_ioctl(struct drm_device
*dev
,
2599 void *data
, struct drm_file
*file_priv
)
2601 struct drm_mode_mode_cmd
*mode_cmd
= data
;
2602 struct drm_connector
*connector
;
2603 struct drm_display_mode
*mode
;
2604 struct drm_mode_object
*obj
;
2605 struct drm_mode_modeinfo
*umode
= &mode_cmd
->mode
;
2608 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2611 mutex_lock(&dev
->mode_config
.mutex
);
2613 obj
= drm_mode_object_find(dev
, mode_cmd
->connector_id
, DRM_MODE_OBJECT_CONNECTOR
);
2618 connector
= obj_to_connector(obj
);
2620 mode
= drm_mode_create(dev
);
2626 ret
= drm_crtc_convert_umode(mode
, umode
);
2628 DRM_DEBUG_KMS("Invalid mode\n");
2629 drm_mode_destroy(dev
, mode
);
2633 drm_mode_attachmode(dev
, connector
, mode
);
2635 mutex_unlock(&dev
->mode_config
.mutex
);
2641 * drm_fb_detachmode - Detach a user specified mode from an connector
2642 * @inode: inode from the ioctl
2643 * @filp: file * from the ioctl
2644 * @cmd: cmd from ioctl
2645 * @arg: arg from ioctl
2647 * Called by the user via ioctl.
2650 * Zero on success, errno on failure.
2652 int drm_mode_detachmode_ioctl(struct drm_device
*dev
,
2653 void *data
, struct drm_file
*file_priv
)
2655 struct drm_mode_object
*obj
;
2656 struct drm_mode_mode_cmd
*mode_cmd
= data
;
2657 struct drm_connector
*connector
;
2658 struct drm_display_mode mode
;
2659 struct drm_mode_modeinfo
*umode
= &mode_cmd
->mode
;
2662 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2665 mutex_lock(&dev
->mode_config
.mutex
);
2667 obj
= drm_mode_object_find(dev
, mode_cmd
->connector_id
, DRM_MODE_OBJECT_CONNECTOR
);
2672 connector
= obj_to_connector(obj
);
2674 ret
= drm_crtc_convert_umode(&mode
, umode
);
2676 DRM_DEBUG_KMS("Invalid mode\n");
2680 ret
= drm_mode_detachmode(dev
, connector
, &mode
);
2682 mutex_unlock(&dev
->mode_config
.mutex
);
2686 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
2687 const char *name
, int num_values
)
2689 struct drm_property
*property
= NULL
;
2692 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
2697 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
2698 if (!property
->values
)
2702 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
2706 property
->flags
= flags
;
2707 property
->num_values
= num_values
;
2708 INIT_LIST_HEAD(&property
->enum_blob_list
);
2711 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
2712 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2715 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
2718 kfree(property
->values
);
2722 EXPORT_SYMBOL(drm_property_create
);
2724 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
2726 const struct drm_prop_enum_list
*props
,
2729 struct drm_property
*property
;
2732 flags
|= DRM_MODE_PROP_ENUM
;
2734 property
= drm_property_create(dev
, flags
, name
, num_values
);
2738 for (i
= 0; i
< num_values
; i
++) {
2739 ret
= drm_property_add_enum(property
, i
,
2743 drm_property_destroy(dev
, property
);
2750 EXPORT_SYMBOL(drm_property_create_enum
);
2752 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
2753 int flags
, const char *name
,
2754 const struct drm_prop_enum_list
*props
,
2757 struct drm_property
*property
;
2760 flags
|= DRM_MODE_PROP_BITMASK
;
2762 property
= drm_property_create(dev
, flags
, name
, num_values
);
2766 for (i
= 0; i
< num_values
; i
++) {
2767 ret
= drm_property_add_enum(property
, i
,
2771 drm_property_destroy(dev
, property
);
2778 EXPORT_SYMBOL(drm_property_create_bitmask
);
2780 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
2782 uint64_t min
, uint64_t max
)
2784 struct drm_property
*property
;
2786 flags
|= DRM_MODE_PROP_RANGE
;
2788 property
= drm_property_create(dev
, flags
, name
, 2);
2792 property
->values
[0] = min
;
2793 property
->values
[1] = max
;
2797 EXPORT_SYMBOL(drm_property_create_range
);
2799 int drm_property_add_enum(struct drm_property
*property
, int index
,
2800 uint64_t value
, const char *name
)
2802 struct drm_property_enum
*prop_enum
;
2804 if (!(property
->flags
& (DRM_MODE_PROP_ENUM
| DRM_MODE_PROP_BITMASK
)))
2808 * Bitmask enum properties have the additional constraint of values
2811 if ((property
->flags
& DRM_MODE_PROP_BITMASK
) && (value
> 63))
2814 if (!list_empty(&property
->enum_blob_list
)) {
2815 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
2816 if (prop_enum
->value
== value
) {
2817 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
2818 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2824 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
2828 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
2829 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2830 prop_enum
->value
= value
;
2832 property
->values
[index
] = value
;
2833 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
2836 EXPORT_SYMBOL(drm_property_add_enum
);
2838 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
2840 struct drm_property_enum
*prop_enum
, *pt
;
2842 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
2843 list_del(&prop_enum
->head
);
2847 if (property
->num_values
)
2848 kfree(property
->values
);
2849 drm_mode_object_put(dev
, &property
->base
);
2850 list_del(&property
->head
);
2853 EXPORT_SYMBOL(drm_property_destroy
);
2855 void drm_connector_attach_property(struct drm_connector
*connector
,
2856 struct drm_property
*property
, uint64_t init_val
)
2858 drm_object_attach_property(&connector
->base
, property
, init_val
);
2860 EXPORT_SYMBOL(drm_connector_attach_property
);
2862 int drm_connector_property_set_value(struct drm_connector
*connector
,
2863 struct drm_property
*property
, uint64_t value
)
2865 return drm_object_property_set_value(&connector
->base
, property
, value
);
2867 EXPORT_SYMBOL(drm_connector_property_set_value
);
2869 int drm_connector_property_get_value(struct drm_connector
*connector
,
2870 struct drm_property
*property
, uint64_t *val
)
2872 return drm_object_property_get_value(&connector
->base
, property
, val
);
2874 EXPORT_SYMBOL(drm_connector_property_get_value
);
2876 void drm_object_attach_property(struct drm_mode_object
*obj
,
2877 struct drm_property
*property
,
2880 int count
= obj
->properties
->count
;
2882 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
2883 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2884 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2885 "you see this message on the same object type.\n",
2890 obj
->properties
->ids
[count
] = property
->base
.id
;
2891 obj
->properties
->values
[count
] = init_val
;
2892 obj
->properties
->count
++;
2894 EXPORT_SYMBOL(drm_object_attach_property
);
2896 int drm_object_property_set_value(struct drm_mode_object
*obj
,
2897 struct drm_property
*property
, uint64_t val
)
2901 for (i
= 0; i
< obj
->properties
->count
; i
++) {
2902 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
2903 obj
->properties
->values
[i
] = val
;
2910 EXPORT_SYMBOL(drm_object_property_set_value
);
2912 int drm_object_property_get_value(struct drm_mode_object
*obj
,
2913 struct drm_property
*property
, uint64_t *val
)
2917 for (i
= 0; i
< obj
->properties
->count
; i
++) {
2918 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
2919 *val
= obj
->properties
->values
[i
];
2926 EXPORT_SYMBOL(drm_object_property_get_value
);
2928 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
2929 void *data
, struct drm_file
*file_priv
)
2931 struct drm_mode_object
*obj
;
2932 struct drm_mode_get_property
*out_resp
= data
;
2933 struct drm_property
*property
;
2936 int value_count
= 0;
2939 struct drm_property_enum
*prop_enum
;
2940 struct drm_mode_property_enum __user
*enum_ptr
;
2941 struct drm_property_blob
*prop_blob
;
2942 uint32_t __user
*blob_id_ptr
;
2943 uint64_t __user
*values_ptr
;
2944 uint32_t __user
*blob_length_ptr
;
2946 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2949 mutex_lock(&dev
->mode_config
.mutex
);
2950 obj
= drm_mode_object_find(dev
, out_resp
->prop_id
, DRM_MODE_OBJECT_PROPERTY
);
2955 property
= obj_to_property(obj
);
2957 if (property
->flags
& (DRM_MODE_PROP_ENUM
| DRM_MODE_PROP_BITMASK
)) {
2958 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
2960 } else if (property
->flags
& DRM_MODE_PROP_BLOB
) {
2961 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
2965 value_count
= property
->num_values
;
2967 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
2968 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
2969 out_resp
->flags
= property
->flags
;
2971 if ((out_resp
->count_values
>= value_count
) && value_count
) {
2972 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
2973 for (i
= 0; i
< value_count
; i
++) {
2974 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
2980 out_resp
->count_values
= value_count
;
2982 if (property
->flags
& (DRM_MODE_PROP_ENUM
| DRM_MODE_PROP_BITMASK
)) {
2983 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
2985 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
2986 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
2988 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
2993 if (copy_to_user(&enum_ptr
[copied
].name
,
2994 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3001 out_resp
->count_enum_blobs
= enum_count
;
3004 if (property
->flags
& DRM_MODE_PROP_BLOB
) {
3005 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
3007 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3008 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
3010 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
3011 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
3016 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
3024 out_resp
->count_enum_blobs
= blob_count
;
3027 mutex_unlock(&dev
->mode_config
.mutex
);
3031 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
3034 struct drm_property_blob
*blob
;
3037 if (!length
|| !data
)
3040 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
3044 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
3050 blob
->length
= length
;
3052 memcpy(blob
->data
, data
, length
);
3054 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3058 static void drm_property_destroy_blob(struct drm_device
*dev
,
3059 struct drm_property_blob
*blob
)
3061 drm_mode_object_put(dev
, &blob
->base
);
3062 list_del(&blob
->head
);
3066 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3067 void *data
, struct drm_file
*file_priv
)
3069 struct drm_mode_object
*obj
;
3070 struct drm_mode_get_blob
*out_resp
= data
;
3071 struct drm_property_blob
*blob
;
3073 void __user
*blob_ptr
;
3075 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3078 mutex_lock(&dev
->mode_config
.mutex
);
3079 obj
= drm_mode_object_find(dev
, out_resp
->blob_id
, DRM_MODE_OBJECT_BLOB
);
3084 blob
= obj_to_blob(obj
);
3086 if (out_resp
->length
== blob
->length
) {
3087 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3088 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3093 out_resp
->length
= blob
->length
;
3096 mutex_unlock(&dev
->mode_config
.mutex
);
3100 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
3103 struct drm_device
*dev
= connector
->dev
;
3106 if (connector
->edid_blob_ptr
)
3107 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
3109 /* Delete edid, when there is none. */
3111 connector
->edid_blob_ptr
= NULL
;
3112 ret
= drm_connector_property_set_value(connector
, dev
->mode_config
.edid_property
, 0);
3116 size
= EDID_LENGTH
* (1 + edid
->extensions
);
3117 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
3120 ret
= drm_connector_property_set_value(connector
,
3121 dev
->mode_config
.edid_property
,
3122 connector
->edid_blob_ptr
->base
.id
);
3126 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
3128 static bool drm_property_change_is_valid(struct drm_property
*property
,
3131 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
3133 if (property
->flags
& DRM_MODE_PROP_RANGE
) {
3134 if (value
< property
->values
[0] || value
> property
->values
[1])
3137 } else if (property
->flags
& DRM_MODE_PROP_BITMASK
) {
3139 uint64_t valid_mask
= 0;
3140 for (i
= 0; i
< property
->num_values
; i
++)
3141 valid_mask
|= (1ULL << property
->values
[i
]);
3142 return !(value
& ~valid_mask
);
3145 for (i
= 0; i
< property
->num_values
; i
++)
3146 if (property
->values
[i
] == value
)
3152 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
3153 void *data
, struct drm_file
*file_priv
)
3155 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
3156 struct drm_mode_obj_set_property obj_set_prop
= {
3157 .value
= conn_set_prop
->value
,
3158 .prop_id
= conn_set_prop
->prop_id
,
3159 .obj_id
= conn_set_prop
->connector_id
,
3160 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
3163 /* It does all the locking and checking we need */
3164 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
3167 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
3168 struct drm_property
*property
,
3172 struct drm_connector
*connector
= obj_to_connector(obj
);
3174 /* Do DPMS ourselves */
3175 if (property
== connector
->dev
->mode_config
.dpms_property
) {
3176 if (connector
->funcs
->dpms
)
3177 (*connector
->funcs
->dpms
)(connector
, (int)value
);
3179 } else if (connector
->funcs
->set_property
)
3180 ret
= connector
->funcs
->set_property(connector
, property
, value
);
3182 /* store the property value if successful */
3184 drm_connector_property_set_value(connector
, property
, value
);
3188 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
3189 struct drm_property
*property
,
3193 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
3195 if (crtc
->funcs
->set_property
)
3196 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
3198 drm_object_property_set_value(obj
, property
, value
);
3203 static int drm_mode_plane_set_obj_prop(struct drm_mode_object
*obj
,
3204 struct drm_property
*property
,
3208 struct drm_plane
*plane
= obj_to_plane(obj
);
3210 if (plane
->funcs
->set_property
)
3211 ret
= plane
->funcs
->set_property(plane
, property
, value
);
3213 drm_object_property_set_value(obj
, property
, value
);
3218 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
3219 struct drm_file
*file_priv
)
3221 struct drm_mode_obj_get_properties
*arg
= data
;
3222 struct drm_mode_object
*obj
;
3226 int props_count
= 0;
3227 uint32_t __user
*props_ptr
;
3228 uint64_t __user
*prop_values_ptr
;
3230 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3233 mutex_lock(&dev
->mode_config
.mutex
);
3235 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3240 if (!obj
->properties
) {
3245 props_count
= obj
->properties
->count
;
3247 /* This ioctl is called twice, once to determine how much space is
3248 * needed, and the 2nd time to fill it. */
3249 if ((arg
->count_props
>= props_count
) && props_count
) {
3251 props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
3252 prop_values_ptr
= (uint64_t __user
*)(unsigned long)
3253 (arg
->prop_values_ptr
);
3254 for (i
= 0; i
< props_count
; i
++) {
3255 if (put_user(obj
->properties
->ids
[i
],
3256 props_ptr
+ copied
)) {
3260 if (put_user(obj
->properties
->values
[i
],
3261 prop_values_ptr
+ copied
)) {
3268 arg
->count_props
= props_count
;
3270 mutex_unlock(&dev
->mode_config
.mutex
);
3274 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
3275 struct drm_file
*file_priv
)
3277 struct drm_mode_obj_set_property
*arg
= data
;
3278 struct drm_mode_object
*arg_obj
;
3279 struct drm_mode_object
*prop_obj
;
3280 struct drm_property
*property
;
3284 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3287 mutex_lock(&dev
->mode_config
.mutex
);
3289 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3292 if (!arg_obj
->properties
)
3295 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
3296 if (arg_obj
->properties
->ids
[i
] == arg
->prop_id
)
3299 if (i
== arg_obj
->properties
->count
)
3302 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
3303 DRM_MODE_OBJECT_PROPERTY
);
3306 property
= obj_to_property(prop_obj
);
3308 if (!drm_property_change_is_valid(property
, arg
->value
))
3311 switch (arg_obj
->type
) {
3312 case DRM_MODE_OBJECT_CONNECTOR
:
3313 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
3316 case DRM_MODE_OBJECT_CRTC
:
3317 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
3319 case DRM_MODE_OBJECT_PLANE
:
3320 ret
= drm_mode_plane_set_obj_prop(arg_obj
, property
, arg
->value
);
3325 mutex_unlock(&dev
->mode_config
.mutex
);
3329 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
3330 struct drm_encoder
*encoder
)
3334 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
3335 if (connector
->encoder_ids
[i
] == 0) {
3336 connector
->encoder_ids
[i
] = encoder
->base
.id
;
3342 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
3344 void drm_mode_connector_detach_encoder(struct drm_connector
*connector
,
3345 struct drm_encoder
*encoder
)
3348 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
3349 if (connector
->encoder_ids
[i
] == encoder
->base
.id
) {
3350 connector
->encoder_ids
[i
] = 0;
3351 if (connector
->encoder
== encoder
)
3352 connector
->encoder
= NULL
;
3357 EXPORT_SYMBOL(drm_mode_connector_detach_encoder
);
3359 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
3362 crtc
->gamma_size
= gamma_size
;
3364 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
3365 if (!crtc
->gamma_store
) {
3366 crtc
->gamma_size
= 0;
3372 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
3374 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
3375 void *data
, struct drm_file
*file_priv
)
3377 struct drm_mode_crtc_lut
*crtc_lut
= data
;
3378 struct drm_mode_object
*obj
;
3379 struct drm_crtc
*crtc
;
3380 void *r_base
, *g_base
, *b_base
;
3384 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3387 mutex_lock(&dev
->mode_config
.mutex
);
3388 obj
= drm_mode_object_find(dev
, crtc_lut
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3393 crtc
= obj_to_crtc(obj
);
3395 if (crtc
->funcs
->gamma_set
== NULL
) {
3400 /* memcpy into gamma store */
3401 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
3406 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
3407 r_base
= crtc
->gamma_store
;
3408 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
3413 g_base
= r_base
+ size
;
3414 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
3419 b_base
= g_base
+ size
;
3420 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
3425 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
3428 mutex_unlock(&dev
->mode_config
.mutex
);
3433 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
3434 void *data
, struct drm_file
*file_priv
)
3436 struct drm_mode_crtc_lut
*crtc_lut
= data
;
3437 struct drm_mode_object
*obj
;
3438 struct drm_crtc
*crtc
;
3439 void *r_base
, *g_base
, *b_base
;
3443 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3446 mutex_lock(&dev
->mode_config
.mutex
);
3447 obj
= drm_mode_object_find(dev
, crtc_lut
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3452 crtc
= obj_to_crtc(obj
);
3454 /* memcpy into gamma store */
3455 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
3460 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
3461 r_base
= crtc
->gamma_store
;
3462 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
3467 g_base
= r_base
+ size
;
3468 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
3473 b_base
= g_base
+ size
;
3474 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
3479 mutex_unlock(&dev
->mode_config
.mutex
);
3483 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
3484 void *data
, struct drm_file
*file_priv
)
3486 struct drm_mode_crtc_page_flip
*page_flip
= data
;
3487 struct drm_mode_object
*obj
;
3488 struct drm_crtc
*crtc
;
3489 struct drm_framebuffer
*fb
;
3490 struct drm_pending_vblank_event
*e
= NULL
;
3491 unsigned long flags
;
3494 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
3495 page_flip
->reserved
!= 0)
3498 mutex_lock(&dev
->mode_config
.mutex
);
3499 obj
= drm_mode_object_find(dev
, page_flip
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3502 crtc
= obj_to_crtc(obj
);
3504 if (crtc
->fb
== NULL
) {
3505 /* The framebuffer is currently unbound, presumably
3506 * due to a hotplug event, that userspace has not
3513 if (crtc
->funcs
->page_flip
== NULL
)
3516 obj
= drm_mode_object_find(dev
, page_flip
->fb_id
, DRM_MODE_OBJECT_FB
);
3519 fb
= obj_to_fb(obj
);
3521 if (crtc
->mode
.hdisplay
> fb
->width
||
3522 crtc
->mode
.vdisplay
> fb
->height
||
3523 crtc
->x
> fb
->width
- crtc
->mode
.hdisplay
||
3524 crtc
->y
> fb
->height
- crtc
->mode
.vdisplay
) {
3525 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
3526 fb
->width
, fb
->height
,
3527 crtc
->mode
.hdisplay
, crtc
->mode
.vdisplay
,
3533 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
3535 spin_lock_irqsave(&dev
->event_lock
, flags
);
3536 if (file_priv
->event_space
< sizeof e
->event
) {
3537 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3540 file_priv
->event_space
-= sizeof e
->event
;
3541 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3543 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
3545 spin_lock_irqsave(&dev
->event_lock
, flags
);
3546 file_priv
->event_space
+= sizeof e
->event
;
3547 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3551 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
3552 e
->event
.base
.length
= sizeof e
->event
;
3553 e
->event
.user_data
= page_flip
->user_data
;
3554 e
->base
.event
= &e
->event
.base
;
3555 e
->base
.file_priv
= file_priv
;
3557 (void (*) (struct drm_pending_event
*)) kfree
;
3560 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
);
3562 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
3563 spin_lock_irqsave(&dev
->event_lock
, flags
);
3564 file_priv
->event_space
+= sizeof e
->event
;
3565 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3571 mutex_unlock(&dev
->mode_config
.mutex
);
3575 void drm_mode_config_reset(struct drm_device
*dev
)
3577 struct drm_crtc
*crtc
;
3578 struct drm_encoder
*encoder
;
3579 struct drm_connector
*connector
;
3581 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
3582 if (crtc
->funcs
->reset
)
3583 crtc
->funcs
->reset(crtc
);
3585 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
3586 if (encoder
->funcs
->reset
)
3587 encoder
->funcs
->reset(encoder
);
3589 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
3590 if (connector
->funcs
->reset
)
3591 connector
->funcs
->reset(connector
);
3593 EXPORT_SYMBOL(drm_mode_config_reset
);
3595 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
3596 void *data
, struct drm_file
*file_priv
)
3598 struct drm_mode_create_dumb
*args
= data
;
3600 if (!dev
->driver
->dumb_create
)
3602 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
3605 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
3606 void *data
, struct drm_file
*file_priv
)
3608 struct drm_mode_map_dumb
*args
= data
;
3610 /* call driver ioctl to get mmap offset */
3611 if (!dev
->driver
->dumb_map_offset
)
3614 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
3617 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
3618 void *data
, struct drm_file
*file_priv
)
3620 struct drm_mode_destroy_dumb
*args
= data
;
3622 if (!dev
->driver
->dumb_destroy
)
3625 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
3629 * Just need to support RGB formats here for compat with code that doesn't
3630 * use pixel formats directly yet.
3632 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
3636 case DRM_FORMAT_RGB332
:
3637 case DRM_FORMAT_BGR233
:
3641 case DRM_FORMAT_XRGB1555
:
3642 case DRM_FORMAT_XBGR1555
:
3643 case DRM_FORMAT_RGBX5551
:
3644 case DRM_FORMAT_BGRX5551
:
3645 case DRM_FORMAT_ARGB1555
:
3646 case DRM_FORMAT_ABGR1555
:
3647 case DRM_FORMAT_RGBA5551
:
3648 case DRM_FORMAT_BGRA5551
:
3652 case DRM_FORMAT_RGB565
:
3653 case DRM_FORMAT_BGR565
:
3657 case DRM_FORMAT_RGB888
:
3658 case DRM_FORMAT_BGR888
:
3662 case DRM_FORMAT_XRGB8888
:
3663 case DRM_FORMAT_XBGR8888
:
3664 case DRM_FORMAT_RGBX8888
:
3665 case DRM_FORMAT_BGRX8888
:
3669 case DRM_FORMAT_XRGB2101010
:
3670 case DRM_FORMAT_XBGR2101010
:
3671 case DRM_FORMAT_RGBX1010102
:
3672 case DRM_FORMAT_BGRX1010102
:
3673 case DRM_FORMAT_ARGB2101010
:
3674 case DRM_FORMAT_ABGR2101010
:
3675 case DRM_FORMAT_RGBA1010102
:
3676 case DRM_FORMAT_BGRA1010102
:
3680 case DRM_FORMAT_ARGB8888
:
3681 case DRM_FORMAT_ABGR8888
:
3682 case DRM_FORMAT_RGBA8888
:
3683 case DRM_FORMAT_BGRA8888
:
3688 DRM_DEBUG_KMS("unsupported pixel format\n");
3694 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
3697 * drm_format_num_planes - get the number of planes for format
3698 * @format: pixel format (DRM_FORMAT_*)
3701 * The number of planes used by the specified pixel format.
3703 int drm_format_num_planes(uint32_t format
)
3706 case DRM_FORMAT_YUV410
:
3707 case DRM_FORMAT_YVU410
:
3708 case DRM_FORMAT_YUV411
:
3709 case DRM_FORMAT_YVU411
:
3710 case DRM_FORMAT_YUV420
:
3711 case DRM_FORMAT_YVU420
:
3712 case DRM_FORMAT_YUV422
:
3713 case DRM_FORMAT_YVU422
:
3714 case DRM_FORMAT_YUV444
:
3715 case DRM_FORMAT_YVU444
:
3717 case DRM_FORMAT_NV12
:
3718 case DRM_FORMAT_NV21
:
3719 case DRM_FORMAT_NV16
:
3720 case DRM_FORMAT_NV61
:
3726 EXPORT_SYMBOL(drm_format_num_planes
);
3729 * drm_format_plane_cpp - determine the bytes per pixel value
3730 * @format: pixel format (DRM_FORMAT_*)
3731 * @plane: plane index
3734 * The bytes per pixel value for the specified plane.
3736 int drm_format_plane_cpp(uint32_t format
, int plane
)
3741 if (plane
>= drm_format_num_planes(format
))
3745 case DRM_FORMAT_YUYV
:
3746 case DRM_FORMAT_YVYU
:
3747 case DRM_FORMAT_UYVY
:
3748 case DRM_FORMAT_VYUY
:
3750 case DRM_FORMAT_NV12
:
3751 case DRM_FORMAT_NV21
:
3752 case DRM_FORMAT_NV16
:
3753 case DRM_FORMAT_NV61
:
3754 return plane
? 2 : 1;
3755 case DRM_FORMAT_YUV410
:
3756 case DRM_FORMAT_YVU410
:
3757 case DRM_FORMAT_YUV411
:
3758 case DRM_FORMAT_YVU411
:
3759 case DRM_FORMAT_YUV420
:
3760 case DRM_FORMAT_YVU420
:
3761 case DRM_FORMAT_YUV422
:
3762 case DRM_FORMAT_YVU422
:
3763 case DRM_FORMAT_YUV444
:
3764 case DRM_FORMAT_YVU444
:
3767 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
3771 EXPORT_SYMBOL(drm_format_plane_cpp
);
3774 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3775 * @format: pixel format (DRM_FORMAT_*)
3778 * The horizontal chroma subsampling factor for the
3779 * specified pixel format.
3781 int drm_format_horz_chroma_subsampling(uint32_t format
)
3784 case DRM_FORMAT_YUV411
:
3785 case DRM_FORMAT_YVU411
:
3786 case DRM_FORMAT_YUV410
:
3787 case DRM_FORMAT_YVU410
:
3789 case DRM_FORMAT_YUYV
:
3790 case DRM_FORMAT_YVYU
:
3791 case DRM_FORMAT_UYVY
:
3792 case DRM_FORMAT_VYUY
:
3793 case DRM_FORMAT_NV12
:
3794 case DRM_FORMAT_NV21
:
3795 case DRM_FORMAT_NV16
:
3796 case DRM_FORMAT_NV61
:
3797 case DRM_FORMAT_YUV422
:
3798 case DRM_FORMAT_YVU422
:
3799 case DRM_FORMAT_YUV420
:
3800 case DRM_FORMAT_YVU420
:
3806 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
3809 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3810 * @format: pixel format (DRM_FORMAT_*)
3813 * The vertical chroma subsampling factor for the
3814 * specified pixel format.
3816 int drm_format_vert_chroma_subsampling(uint32_t format
)
3819 case DRM_FORMAT_YUV410
:
3820 case DRM_FORMAT_YVU410
:
3822 case DRM_FORMAT_YUV420
:
3823 case DRM_FORMAT_YVU420
:
3824 case DRM_FORMAT_NV12
:
3825 case DRM_FORMAT_NV21
:
3831 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);