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 * Caller must hold 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 list_add_tail(&crtc
->head
, &dev
->mode_config
.crtc_list
);
388 dev
->mode_config
.num_crtc
++;
391 mutex_unlock(&dev
->mode_config
.mutex
);
395 EXPORT_SYMBOL(drm_crtc_init
);
398 * drm_crtc_cleanup - Cleans up the core crtc usage.
399 * @crtc: CRTC to cleanup
402 * Caller must hold mode config lock.
404 * Cleanup @crtc. Removes from drm modesetting space
405 * does NOT free object, caller does that.
407 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
409 struct drm_device
*dev
= crtc
->dev
;
411 if (crtc
->gamma_store
) {
412 kfree(crtc
->gamma_store
);
413 crtc
->gamma_store
= NULL
;
416 drm_mode_object_put(dev
, &crtc
->base
);
417 list_del(&crtc
->head
);
418 dev
->mode_config
.num_crtc
--;
420 EXPORT_SYMBOL(drm_crtc_cleanup
);
423 * drm_mode_probed_add - add a mode to a connector's probed mode list
424 * @connector: connector the new mode
428 * Caller must hold mode config lock.
430 * Add @mode to @connector's mode list for later use.
432 void drm_mode_probed_add(struct drm_connector
*connector
,
433 struct drm_display_mode
*mode
)
435 list_add(&mode
->head
, &connector
->probed_modes
);
437 EXPORT_SYMBOL(drm_mode_probed_add
);
440 * drm_mode_remove - remove and free a mode
441 * @connector: connector list to modify
442 * @mode: mode to remove
445 * Caller must hold mode config lock.
447 * Remove @mode from @connector's mode list, then free it.
449 void drm_mode_remove(struct drm_connector
*connector
,
450 struct drm_display_mode
*mode
)
452 list_del(&mode
->head
);
453 drm_mode_destroy(connector
->dev
, mode
);
455 EXPORT_SYMBOL(drm_mode_remove
);
458 * drm_connector_init - Init a preallocated connector
460 * @connector: the connector to init
461 * @funcs: callbacks for this connector
462 * @name: user visible name of the connector
465 * Takes mode config lock.
467 * Initialises a preallocated connector. Connectors should be
468 * subclassed as part of driver connector objects.
471 * Zero on success, error code on failure.
473 int drm_connector_init(struct drm_device
*dev
,
474 struct drm_connector
*connector
,
475 const struct drm_connector_funcs
*funcs
,
480 mutex_lock(&dev
->mode_config
.mutex
);
482 ret
= drm_mode_object_get(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
);
486 connector
->dev
= dev
;
487 connector
->funcs
= funcs
;
488 connector
->connector_type
= connector_type
;
489 connector
->connector_type_id
=
490 ++drm_connector_enum_list
[connector_type
].count
; /* TODO */
491 INIT_LIST_HEAD(&connector
->user_modes
);
492 INIT_LIST_HEAD(&connector
->probed_modes
);
493 INIT_LIST_HEAD(&connector
->modes
);
494 connector
->edid_blob_ptr
= NULL
;
496 list_add_tail(&connector
->head
, &dev
->mode_config
.connector_list
);
497 dev
->mode_config
.num_connector
++;
499 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
500 drm_connector_attach_property(connector
,
501 dev
->mode_config
.edid_property
,
504 drm_connector_attach_property(connector
,
505 dev
->mode_config
.dpms_property
, 0);
508 mutex_unlock(&dev
->mode_config
.mutex
);
512 EXPORT_SYMBOL(drm_connector_init
);
515 * drm_connector_cleanup - cleans up an initialised connector
516 * @connector: connector to cleanup
519 * Takes mode config lock.
521 * Cleans up the connector but doesn't free the object.
523 void drm_connector_cleanup(struct drm_connector
*connector
)
525 struct drm_device
*dev
= connector
->dev
;
526 struct drm_display_mode
*mode
, *t
;
528 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
529 drm_mode_remove(connector
, mode
);
531 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
532 drm_mode_remove(connector
, mode
);
534 list_for_each_entry_safe(mode
, t
, &connector
->user_modes
, head
)
535 drm_mode_remove(connector
, mode
);
537 mutex_lock(&dev
->mode_config
.mutex
);
538 drm_mode_object_put(dev
, &connector
->base
);
539 list_del(&connector
->head
);
540 dev
->mode_config
.num_connector
--;
541 mutex_unlock(&dev
->mode_config
.mutex
);
543 EXPORT_SYMBOL(drm_connector_cleanup
);
545 void drm_connector_unplug_all(struct drm_device
*dev
)
547 struct drm_connector
*connector
;
549 /* taking the mode config mutex ends up in a clash with sysfs */
550 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
551 drm_sysfs_connector_remove(connector
);
554 EXPORT_SYMBOL(drm_connector_unplug_all
);
556 int drm_encoder_init(struct drm_device
*dev
,
557 struct drm_encoder
*encoder
,
558 const struct drm_encoder_funcs
*funcs
,
563 mutex_lock(&dev
->mode_config
.mutex
);
565 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
570 encoder
->encoder_type
= encoder_type
;
571 encoder
->funcs
= funcs
;
573 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
574 dev
->mode_config
.num_encoder
++;
577 mutex_unlock(&dev
->mode_config
.mutex
);
581 EXPORT_SYMBOL(drm_encoder_init
);
583 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
585 struct drm_device
*dev
= encoder
->dev
;
586 mutex_lock(&dev
->mode_config
.mutex
);
587 drm_mode_object_put(dev
, &encoder
->base
);
588 list_del(&encoder
->head
);
589 dev
->mode_config
.num_encoder
--;
590 mutex_unlock(&dev
->mode_config
.mutex
);
592 EXPORT_SYMBOL(drm_encoder_cleanup
);
594 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
595 unsigned long possible_crtcs
,
596 const struct drm_plane_funcs
*funcs
,
597 const uint32_t *formats
, uint32_t format_count
,
602 mutex_lock(&dev
->mode_config
.mutex
);
604 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
609 plane
->funcs
= funcs
;
610 plane
->format_types
= kmalloc(sizeof(uint32_t) * format_count
,
612 if (!plane
->format_types
) {
613 DRM_DEBUG_KMS("out of memory when allocating plane\n");
614 drm_mode_object_put(dev
, &plane
->base
);
619 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
620 plane
->format_count
= format_count
;
621 plane
->possible_crtcs
= possible_crtcs
;
623 /* private planes are not exposed to userspace, but depending on
624 * display hardware, might be convenient to allow sharing programming
625 * for the scanout engine with the crtc implementation.
628 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
629 dev
->mode_config
.num_plane
++;
631 INIT_LIST_HEAD(&plane
->head
);
635 mutex_unlock(&dev
->mode_config
.mutex
);
639 EXPORT_SYMBOL(drm_plane_init
);
641 void drm_plane_cleanup(struct drm_plane
*plane
)
643 struct drm_device
*dev
= plane
->dev
;
645 mutex_lock(&dev
->mode_config
.mutex
);
646 kfree(plane
->format_types
);
647 drm_mode_object_put(dev
, &plane
->base
);
648 /* if not added to a list, it must be a private plane */
649 if (!list_empty(&plane
->head
)) {
650 list_del(&plane
->head
);
651 dev
->mode_config
.num_plane
--;
653 mutex_unlock(&dev
->mode_config
.mutex
);
655 EXPORT_SYMBOL(drm_plane_cleanup
);
658 * drm_mode_create - create a new display mode
662 * Caller must hold DRM mode_config lock.
664 * Create a new drm_display_mode, give it an ID, and return it.
667 * Pointer to new mode on success, NULL on error.
669 struct drm_display_mode
*drm_mode_create(struct drm_device
*dev
)
671 struct drm_display_mode
*nmode
;
673 nmode
= kzalloc(sizeof(struct drm_display_mode
), GFP_KERNEL
);
677 if (drm_mode_object_get(dev
, &nmode
->base
, DRM_MODE_OBJECT_MODE
)) {
684 EXPORT_SYMBOL(drm_mode_create
);
687 * drm_mode_destroy - remove a mode
689 * @mode: mode to remove
692 * Caller must hold mode config lock.
694 * Free @mode's unique identifier, then free it.
696 void drm_mode_destroy(struct drm_device
*dev
, struct drm_display_mode
*mode
)
701 drm_mode_object_put(dev
, &mode
->base
);
705 EXPORT_SYMBOL(drm_mode_destroy
);
707 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
709 struct drm_property
*edid
;
710 struct drm_property
*dpms
;
713 * Standard properties (apply to all connectors)
715 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
716 DRM_MODE_PROP_IMMUTABLE
,
718 dev
->mode_config
.edid_property
= edid
;
720 dpms
= drm_property_create_enum(dev
, 0,
721 "DPMS", drm_dpms_enum_list
,
722 ARRAY_SIZE(drm_dpms_enum_list
));
723 dev
->mode_config
.dpms_property
= dpms
;
729 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
732 * Called by a driver the first time a DVI-I connector is made.
734 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
736 struct drm_property
*dvi_i_selector
;
737 struct drm_property
*dvi_i_subconnector
;
739 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
743 drm_property_create_enum(dev
, 0,
744 "select subconnector",
745 drm_dvi_i_select_enum_list
,
746 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
747 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
749 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
751 drm_dvi_i_subconnector_enum_list
,
752 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
753 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
757 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
760 * drm_create_tv_properties - create TV specific connector properties
762 * @num_modes: number of different TV formats (modes) supported
763 * @modes: array of pointers to strings containing name of each format
765 * Called by a driver's TV initialization routine, this function creates
766 * the TV specific connector properties for a given device. Caller is
767 * responsible for allocating a list of format names and passing them to
770 int drm_mode_create_tv_properties(struct drm_device
*dev
, int num_modes
,
773 struct drm_property
*tv_selector
;
774 struct drm_property
*tv_subconnector
;
777 if (dev
->mode_config
.tv_select_subconnector_property
)
781 * Basic connector properties
783 tv_selector
= drm_property_create_enum(dev
, 0,
784 "select subconnector",
785 drm_tv_select_enum_list
,
786 ARRAY_SIZE(drm_tv_select_enum_list
));
787 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
790 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
792 drm_tv_subconnector_enum_list
,
793 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
794 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
797 * Other, TV specific properties: margins & TV modes.
799 dev
->mode_config
.tv_left_margin_property
=
800 drm_property_create_range(dev
, 0, "left margin", 0, 100);
802 dev
->mode_config
.tv_right_margin_property
=
803 drm_property_create_range(dev
, 0, "right margin", 0, 100);
805 dev
->mode_config
.tv_top_margin_property
=
806 drm_property_create_range(dev
, 0, "top margin", 0, 100);
808 dev
->mode_config
.tv_bottom_margin_property
=
809 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
811 dev
->mode_config
.tv_mode_property
=
812 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
814 for (i
= 0; i
< num_modes
; i
++)
815 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
818 dev
->mode_config
.tv_brightness_property
=
819 drm_property_create_range(dev
, 0, "brightness", 0, 100);
821 dev
->mode_config
.tv_contrast_property
=
822 drm_property_create_range(dev
, 0, "contrast", 0, 100);
824 dev
->mode_config
.tv_flicker_reduction_property
=
825 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
827 dev
->mode_config
.tv_overscan_property
=
828 drm_property_create_range(dev
, 0, "overscan", 0, 100);
830 dev
->mode_config
.tv_saturation_property
=
831 drm_property_create_range(dev
, 0, "saturation", 0, 100);
833 dev
->mode_config
.tv_hue_property
=
834 drm_property_create_range(dev
, 0, "hue", 0, 100);
838 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
841 * drm_mode_create_scaling_mode_property - create scaling mode property
844 * Called by a driver the first time it's needed, must be attached to desired
847 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
849 struct drm_property
*scaling_mode
;
851 if (dev
->mode_config
.scaling_mode_property
)
855 drm_property_create_enum(dev
, 0, "scaling mode",
856 drm_scaling_mode_enum_list
,
857 ARRAY_SIZE(drm_scaling_mode_enum_list
));
859 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
863 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
866 * drm_mode_create_dithering_property - create dithering property
869 * Called by a driver the first time it's needed, must be attached to desired
872 int drm_mode_create_dithering_property(struct drm_device
*dev
)
874 struct drm_property
*dithering_mode
;
876 if (dev
->mode_config
.dithering_mode_property
)
880 drm_property_create_enum(dev
, 0, "dithering",
881 drm_dithering_mode_enum_list
,
882 ARRAY_SIZE(drm_dithering_mode_enum_list
));
883 dev
->mode_config
.dithering_mode_property
= dithering_mode
;
887 EXPORT_SYMBOL(drm_mode_create_dithering_property
);
890 * drm_mode_create_dirty_property - create dirty property
893 * Called by a driver the first time it's needed, must be attached to desired
896 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
898 struct drm_property
*dirty_info
;
900 if (dev
->mode_config
.dirty_info_property
)
904 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
906 drm_dirty_info_enum_list
,
907 ARRAY_SIZE(drm_dirty_info_enum_list
));
908 dev
->mode_config
.dirty_info_property
= dirty_info
;
912 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
915 * drm_mode_config_init - initialize DRM mode_configuration structure
919 * None, should happen single threaded at init time.
921 * Initialize @dev's mode_config structure, used for tracking the graphics
922 * configuration of @dev.
924 void drm_mode_config_init(struct drm_device
*dev
)
926 mutex_init(&dev
->mode_config
.mutex
);
927 mutex_init(&dev
->mode_config
.idr_mutex
);
928 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
929 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
930 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
931 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
932 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
933 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
934 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
935 idr_init(&dev
->mode_config
.crtc_idr
);
937 mutex_lock(&dev
->mode_config
.mutex
);
938 drm_mode_create_standard_connector_properties(dev
);
939 mutex_unlock(&dev
->mode_config
.mutex
);
941 /* Just to be sure */
942 dev
->mode_config
.num_fb
= 0;
943 dev
->mode_config
.num_connector
= 0;
944 dev
->mode_config
.num_crtc
= 0;
945 dev
->mode_config
.num_encoder
= 0;
947 EXPORT_SYMBOL(drm_mode_config_init
);
949 int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
951 uint32_t total_objects
= 0;
953 total_objects
+= dev
->mode_config
.num_crtc
;
954 total_objects
+= dev
->mode_config
.num_connector
;
955 total_objects
+= dev
->mode_config
.num_encoder
;
957 group
->id_list
= kzalloc(total_objects
* sizeof(uint32_t), GFP_KERNEL
);
961 group
->num_crtcs
= 0;
962 group
->num_connectors
= 0;
963 group
->num_encoders
= 0;
967 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
968 struct drm_mode_group
*group
)
970 struct drm_crtc
*crtc
;
971 struct drm_encoder
*encoder
;
972 struct drm_connector
*connector
;
975 if ((ret
= drm_mode_group_init(dev
, group
)))
978 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
979 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
981 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
982 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
985 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
986 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
987 group
->num_connectors
++] = connector
->base
.id
;
991 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
994 * drm_mode_config_cleanup - free up DRM mode_config info
998 * Caller must hold mode config lock.
1000 * Free up all the connectors and CRTCs associated with this DRM device, then
1001 * free up the framebuffers and associated buffer objects.
1003 * FIXME: cleanup any dangling user buffer objects too
1005 void drm_mode_config_cleanup(struct drm_device
*dev
)
1007 struct drm_connector
*connector
, *ot
;
1008 struct drm_crtc
*crtc
, *ct
;
1009 struct drm_encoder
*encoder
, *enct
;
1010 struct drm_framebuffer
*fb
, *fbt
;
1011 struct drm_property
*property
, *pt
;
1012 struct drm_plane
*plane
, *plt
;
1014 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
1016 encoder
->funcs
->destroy(encoder
);
1019 list_for_each_entry_safe(connector
, ot
,
1020 &dev
->mode_config
.connector_list
, head
) {
1021 connector
->funcs
->destroy(connector
);
1024 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
1026 drm_property_destroy(dev
, property
);
1029 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
1030 fb
->funcs
->destroy(fb
);
1033 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
1034 crtc
->funcs
->destroy(crtc
);
1037 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
1039 plane
->funcs
->destroy(plane
);
1042 idr_remove_all(&dev
->mode_config
.crtc_idr
);
1043 idr_destroy(&dev
->mode_config
.crtc_idr
);
1045 EXPORT_SYMBOL(drm_mode_config_cleanup
);
1048 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1049 * @out: drm_mode_modeinfo struct to return to the user
1050 * @in: drm_display_mode to use
1055 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1058 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1059 const struct drm_display_mode
*in
)
1061 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1062 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1063 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1064 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1065 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1066 "timing values too large for mode info\n");
1068 out
->clock
= in
->clock
;
1069 out
->hdisplay
= in
->hdisplay
;
1070 out
->hsync_start
= in
->hsync_start
;
1071 out
->hsync_end
= in
->hsync_end
;
1072 out
->htotal
= in
->htotal
;
1073 out
->hskew
= in
->hskew
;
1074 out
->vdisplay
= in
->vdisplay
;
1075 out
->vsync_start
= in
->vsync_start
;
1076 out
->vsync_end
= in
->vsync_end
;
1077 out
->vtotal
= in
->vtotal
;
1078 out
->vscan
= in
->vscan
;
1079 out
->vrefresh
= in
->vrefresh
;
1080 out
->flags
= in
->flags
;
1081 out
->type
= in
->type
;
1082 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1083 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1087 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1088 * @out: drm_display_mode to return to the user
1089 * @in: drm_mode_modeinfo to use
1094 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1098 * Zero on success, errno on failure.
1100 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1101 const struct drm_mode_modeinfo
*in
)
1103 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1106 out
->clock
= in
->clock
;
1107 out
->hdisplay
= in
->hdisplay
;
1108 out
->hsync_start
= in
->hsync_start
;
1109 out
->hsync_end
= in
->hsync_end
;
1110 out
->htotal
= in
->htotal
;
1111 out
->hskew
= in
->hskew
;
1112 out
->vdisplay
= in
->vdisplay
;
1113 out
->vsync_start
= in
->vsync_start
;
1114 out
->vsync_end
= in
->vsync_end
;
1115 out
->vtotal
= in
->vtotal
;
1116 out
->vscan
= in
->vscan
;
1117 out
->vrefresh
= in
->vrefresh
;
1118 out
->flags
= in
->flags
;
1119 out
->type
= in
->type
;
1120 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1121 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1127 * drm_mode_getresources - get graphics configuration
1128 * @inode: inode from the ioctl
1129 * @filp: file * from the ioctl
1130 * @cmd: cmd from ioctl
1131 * @arg: arg from ioctl
1134 * Takes mode config lock.
1136 * Construct a set of configuration description structures and return
1137 * them to the user, including CRTC, connector and framebuffer configuration.
1139 * Called by the user via ioctl.
1142 * Zero on success, errno on failure.
1144 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1145 struct drm_file
*file_priv
)
1147 struct drm_mode_card_res
*card_res
= data
;
1148 struct list_head
*lh
;
1149 struct drm_framebuffer
*fb
;
1150 struct drm_connector
*connector
;
1151 struct drm_crtc
*crtc
;
1152 struct drm_encoder
*encoder
;
1154 int connector_count
= 0;
1157 int encoder_count
= 0;
1159 uint32_t __user
*fb_id
;
1160 uint32_t __user
*crtc_id
;
1161 uint32_t __user
*connector_id
;
1162 uint32_t __user
*encoder_id
;
1163 struct drm_mode_group
*mode_group
;
1165 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1168 mutex_lock(&dev
->mode_config
.mutex
);
1171 * For the non-control nodes we need to limit the list of resources
1172 * by IDs in the group list for this node
1174 list_for_each(lh
, &file_priv
->fbs
)
1177 mode_group
= &file_priv
->master
->minor
->mode_group
;
1178 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1180 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1183 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1186 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1190 crtc_count
= mode_group
->num_crtcs
;
1191 connector_count
= mode_group
->num_connectors
;
1192 encoder_count
= mode_group
->num_encoders
;
1195 card_res
->max_height
= dev
->mode_config
.max_height
;
1196 card_res
->min_height
= dev
->mode_config
.min_height
;
1197 card_res
->max_width
= dev
->mode_config
.max_width
;
1198 card_res
->min_width
= dev
->mode_config
.min_width
;
1200 /* handle this in 4 parts */
1202 if (card_res
->count_fbs
>= fb_count
) {
1204 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1205 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1206 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1213 card_res
->count_fbs
= fb_count
;
1216 if (card_res
->count_crtcs
>= crtc_count
) {
1218 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1219 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1220 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1222 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1223 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1230 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1231 if (put_user(mode_group
->id_list
[i
],
1232 crtc_id
+ copied
)) {
1240 card_res
->count_crtcs
= crtc_count
;
1243 if (card_res
->count_encoders
>= encoder_count
) {
1245 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1246 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1247 list_for_each_entry(encoder
,
1248 &dev
->mode_config
.encoder_list
,
1250 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1251 drm_get_encoder_name(encoder
));
1252 if (put_user(encoder
->base
.id
, encoder_id
+
1260 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1261 if (put_user(mode_group
->id_list
[i
],
1262 encoder_id
+ copied
)) {
1271 card_res
->count_encoders
= encoder_count
;
1274 if (card_res
->count_connectors
>= connector_count
) {
1276 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1277 if (file_priv
->master
->minor
->type
== DRM_MINOR_CONTROL
) {
1278 list_for_each_entry(connector
,
1279 &dev
->mode_config
.connector_list
,
1281 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1283 drm_get_connector_name(connector
));
1284 if (put_user(connector
->base
.id
,
1285 connector_id
+ copied
)) {
1292 int start
= mode_group
->num_crtcs
+
1293 mode_group
->num_encoders
;
1294 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1295 if (put_user(mode_group
->id_list
[i
],
1296 connector_id
+ copied
)) {
1304 card_res
->count_connectors
= connector_count
;
1306 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1307 card_res
->count_connectors
, card_res
->count_encoders
);
1310 mutex_unlock(&dev
->mode_config
.mutex
);
1315 * drm_mode_getcrtc - get CRTC configuration
1316 * @inode: inode from the ioctl
1317 * @filp: file * from the ioctl
1318 * @cmd: cmd from ioctl
1319 * @arg: arg from ioctl
1322 * Takes mode config lock.
1324 * Construct a CRTC configuration structure to return to the user.
1326 * Called by the user via ioctl.
1329 * Zero on success, errno on failure.
1331 int drm_mode_getcrtc(struct drm_device
*dev
,
1332 void *data
, struct drm_file
*file_priv
)
1334 struct drm_mode_crtc
*crtc_resp
= data
;
1335 struct drm_crtc
*crtc
;
1336 struct drm_mode_object
*obj
;
1339 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1342 mutex_lock(&dev
->mode_config
.mutex
);
1344 obj
= drm_mode_object_find(dev
, crtc_resp
->crtc_id
,
1345 DRM_MODE_OBJECT_CRTC
);
1350 crtc
= obj_to_crtc(obj
);
1352 crtc_resp
->x
= crtc
->x
;
1353 crtc_resp
->y
= crtc
->y
;
1354 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1356 crtc_resp
->fb_id
= crtc
->fb
->base
.id
;
1358 crtc_resp
->fb_id
= 0;
1360 if (crtc
->enabled
) {
1362 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1363 crtc_resp
->mode_valid
= 1;
1366 crtc_resp
->mode_valid
= 0;
1370 mutex_unlock(&dev
->mode_config
.mutex
);
1375 * drm_mode_getconnector - get connector configuration
1376 * @inode: inode from the ioctl
1377 * @filp: file * from the ioctl
1378 * @cmd: cmd from ioctl
1379 * @arg: arg from ioctl
1382 * Takes mode config lock.
1384 * Construct a connector configuration structure to return to the user.
1386 * Called by the user via ioctl.
1389 * Zero on success, errno on failure.
1391 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
1392 struct drm_file
*file_priv
)
1394 struct drm_mode_get_connector
*out_resp
= data
;
1395 struct drm_mode_object
*obj
;
1396 struct drm_connector
*connector
;
1397 struct drm_display_mode
*mode
;
1399 int props_count
= 0;
1400 int encoders_count
= 0;
1404 struct drm_mode_modeinfo u_mode
;
1405 struct drm_mode_modeinfo __user
*mode_ptr
;
1406 uint32_t __user
*prop_ptr
;
1407 uint64_t __user
*prop_values
;
1408 uint32_t __user
*encoder_ptr
;
1410 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1413 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
1415 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
1417 mutex_lock(&dev
->mode_config
.mutex
);
1419 obj
= drm_mode_object_find(dev
, out_resp
->connector_id
,
1420 DRM_MODE_OBJECT_CONNECTOR
);
1425 connector
= obj_to_connector(obj
);
1427 for (i
= 0; i
< DRM_CONNECTOR_MAX_PROPERTY
; i
++) {
1428 if (connector
->property_ids
[i
] != 0) {
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
< DRM_CONNECTOR_MAX_PROPERTY
; i
++) {
1485 if (connector
->property_ids
[i
] != 0) {
1486 if (put_user(connector
->property_ids
[i
],
1487 prop_ptr
+ copied
)) {
1492 if (put_user(connector
->property_values
[i
],
1493 prop_values
+ copied
)) {
1501 out_resp
->count_props
= props_count
;
1503 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
1505 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
1506 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1507 if (connector
->encoder_ids
[i
] != 0) {
1508 if (put_user(connector
->encoder_ids
[i
],
1509 encoder_ptr
+ copied
)) {
1517 out_resp
->count_encoders
= encoders_count
;
1520 mutex_unlock(&dev
->mode_config
.mutex
);
1524 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
1525 struct drm_file
*file_priv
)
1527 struct drm_mode_get_encoder
*enc_resp
= data
;
1528 struct drm_mode_object
*obj
;
1529 struct drm_encoder
*encoder
;
1532 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1535 mutex_lock(&dev
->mode_config
.mutex
);
1536 obj
= drm_mode_object_find(dev
, enc_resp
->encoder_id
,
1537 DRM_MODE_OBJECT_ENCODER
);
1542 encoder
= obj_to_encoder(obj
);
1545 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
1547 enc_resp
->crtc_id
= 0;
1548 enc_resp
->encoder_type
= encoder
->encoder_type
;
1549 enc_resp
->encoder_id
= encoder
->base
.id
;
1550 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
1551 enc_resp
->possible_clones
= encoder
->possible_clones
;
1554 mutex_unlock(&dev
->mode_config
.mutex
);
1559 * drm_mode_getplane_res - get plane info
1562 * @file_priv: DRM file info
1565 * Takes mode config lock.
1567 * Return an plane count and set of IDs.
1569 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
1570 struct drm_file
*file_priv
)
1572 struct drm_mode_get_plane_res
*plane_resp
= data
;
1573 struct drm_mode_config
*config
;
1574 struct drm_plane
*plane
;
1575 uint32_t __user
*plane_ptr
;
1576 int copied
= 0, ret
= 0;
1578 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1581 mutex_lock(&dev
->mode_config
.mutex
);
1582 config
= &dev
->mode_config
;
1585 * This ioctl is called twice, once to determine how much space is
1586 * needed, and the 2nd time to fill it.
1588 if (config
->num_plane
&&
1589 (plane_resp
->count_planes
>= config
->num_plane
)) {
1590 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
1592 list_for_each_entry(plane
, &config
->plane_list
, head
) {
1593 if (put_user(plane
->base
.id
, plane_ptr
+ copied
)) {
1600 plane_resp
->count_planes
= config
->num_plane
;
1603 mutex_unlock(&dev
->mode_config
.mutex
);
1608 * drm_mode_getplane - get plane info
1611 * @file_priv: DRM file info
1614 * Takes mode config lock.
1616 * Return plane info, including formats supported, gamma size, any
1619 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
1620 struct drm_file
*file_priv
)
1622 struct drm_mode_get_plane
*plane_resp
= data
;
1623 struct drm_mode_object
*obj
;
1624 struct drm_plane
*plane
;
1625 uint32_t __user
*format_ptr
;
1628 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1631 mutex_lock(&dev
->mode_config
.mutex
);
1632 obj
= drm_mode_object_find(dev
, plane_resp
->plane_id
,
1633 DRM_MODE_OBJECT_PLANE
);
1638 plane
= obj_to_plane(obj
);
1641 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
1643 plane_resp
->crtc_id
= 0;
1646 plane_resp
->fb_id
= plane
->fb
->base
.id
;
1648 plane_resp
->fb_id
= 0;
1650 plane_resp
->plane_id
= plane
->base
.id
;
1651 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
1652 plane_resp
->gamma_size
= plane
->gamma_size
;
1655 * This ioctl is called twice, once to determine how much space is
1656 * needed, and the 2nd time to fill it.
1658 if (plane
->format_count
&&
1659 (plane_resp
->count_format_types
>= plane
->format_count
)) {
1660 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
1661 if (copy_to_user(format_ptr
,
1662 plane
->format_types
,
1663 sizeof(uint32_t) * plane
->format_count
)) {
1668 plane_resp
->count_format_types
= plane
->format_count
;
1671 mutex_unlock(&dev
->mode_config
.mutex
);
1676 * drm_mode_setplane - set up or tear down an plane
1678 * @data: ioctl data*
1679 * @file_prive: DRM file info
1682 * Takes mode config lock.
1684 * Set plane info, including placement, fb, scaling, and other factors.
1685 * Or pass a NULL fb to disable.
1687 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
1688 struct drm_file
*file_priv
)
1690 struct drm_mode_set_plane
*plane_req
= data
;
1691 struct drm_mode_object
*obj
;
1692 struct drm_plane
*plane
;
1693 struct drm_crtc
*crtc
;
1694 struct drm_framebuffer
*fb
;
1696 unsigned int fb_width
, fb_height
;
1699 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1702 mutex_lock(&dev
->mode_config
.mutex
);
1705 * First, find the plane, crtc, and fb objects. If not available,
1706 * we don't bother to call the driver.
1708 obj
= drm_mode_object_find(dev
, plane_req
->plane_id
,
1709 DRM_MODE_OBJECT_PLANE
);
1711 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1712 plane_req
->plane_id
);
1716 plane
= obj_to_plane(obj
);
1718 /* No fb means shut it down */
1719 if (!plane_req
->fb_id
) {
1720 plane
->funcs
->disable_plane(plane
);
1726 obj
= drm_mode_object_find(dev
, plane_req
->crtc_id
,
1727 DRM_MODE_OBJECT_CRTC
);
1729 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1730 plane_req
->crtc_id
);
1734 crtc
= obj_to_crtc(obj
);
1736 obj
= drm_mode_object_find(dev
, plane_req
->fb_id
,
1737 DRM_MODE_OBJECT_FB
);
1739 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1744 fb
= obj_to_fb(obj
);
1746 /* Check whether this plane supports the fb pixel format. */
1747 for (i
= 0; i
< plane
->format_count
; i
++)
1748 if (fb
->pixel_format
== plane
->format_types
[i
])
1750 if (i
== plane
->format_count
) {
1751 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb
->pixel_format
);
1756 fb_width
= fb
->width
<< 16;
1757 fb_height
= fb
->height
<< 16;
1759 /* Make sure source coordinates are inside the fb. */
1760 if (plane_req
->src_w
> fb_width
||
1761 plane_req
->src_x
> fb_width
- plane_req
->src_w
||
1762 plane_req
->src_h
> fb_height
||
1763 plane_req
->src_y
> fb_height
- plane_req
->src_h
) {
1764 DRM_DEBUG_KMS("Invalid source coordinates "
1765 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1766 plane_req
->src_w
>> 16,
1767 ((plane_req
->src_w
& 0xffff) * 15625) >> 10,
1768 plane_req
->src_h
>> 16,
1769 ((plane_req
->src_h
& 0xffff) * 15625) >> 10,
1770 plane_req
->src_x
>> 16,
1771 ((plane_req
->src_x
& 0xffff) * 15625) >> 10,
1772 plane_req
->src_y
>> 16,
1773 ((plane_req
->src_y
& 0xffff) * 15625) >> 10);
1778 /* Give drivers some help against integer overflows */
1779 if (plane_req
->crtc_w
> INT_MAX
||
1780 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
1781 plane_req
->crtc_h
> INT_MAX
||
1782 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
1783 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1784 plane_req
->crtc_w
, plane_req
->crtc_h
,
1785 plane_req
->crtc_x
, plane_req
->crtc_y
);
1790 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
1791 plane_req
->crtc_x
, plane_req
->crtc_y
,
1792 plane_req
->crtc_w
, plane_req
->crtc_h
,
1793 plane_req
->src_x
, plane_req
->src_y
,
1794 plane_req
->src_w
, plane_req
->src_h
);
1801 mutex_unlock(&dev
->mode_config
.mutex
);
1807 * drm_mode_setcrtc - set CRTC configuration
1808 * @inode: inode from the ioctl
1809 * @filp: file * from the ioctl
1810 * @cmd: cmd from ioctl
1811 * @arg: arg from ioctl
1814 * Takes mode config lock.
1816 * Build a new CRTC configuration based on user request.
1818 * Called by the user via ioctl.
1821 * Zero on success, errno on failure.
1823 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
1824 struct drm_file
*file_priv
)
1826 struct drm_mode_config
*config
= &dev
->mode_config
;
1827 struct drm_mode_crtc
*crtc_req
= data
;
1828 struct drm_mode_object
*obj
;
1829 struct drm_crtc
*crtc
;
1830 struct drm_connector
**connector_set
= NULL
, *connector
;
1831 struct drm_framebuffer
*fb
= NULL
;
1832 struct drm_display_mode
*mode
= NULL
;
1833 struct drm_mode_set set
;
1834 uint32_t __user
*set_connectors_ptr
;
1838 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1841 /* For some reason crtc x/y offsets are signed internally. */
1842 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
1845 mutex_lock(&dev
->mode_config
.mutex
);
1846 obj
= drm_mode_object_find(dev
, crtc_req
->crtc_id
,
1847 DRM_MODE_OBJECT_CRTC
);
1849 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
1853 crtc
= obj_to_crtc(obj
);
1854 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1856 if (crtc_req
->mode_valid
) {
1857 /* If we have a mode we need a framebuffer. */
1858 /* If we pass -1, set the mode with the currently bound fb */
1859 if (crtc_req
->fb_id
== -1) {
1861 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1867 obj
= drm_mode_object_find(dev
, crtc_req
->fb_id
,
1868 DRM_MODE_OBJECT_FB
);
1870 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1875 fb
= obj_to_fb(obj
);
1878 mode
= drm_mode_create(dev
);
1884 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
1886 DRM_DEBUG_KMS("Invalid mode\n");
1890 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
1892 if (mode
->hdisplay
> fb
->width
||
1893 mode
->vdisplay
> fb
->height
||
1894 crtc_req
->x
> fb
->width
- mode
->hdisplay
||
1895 crtc_req
->y
> fb
->height
- mode
->vdisplay
) {
1896 DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
1897 mode
->hdisplay
, mode
->vdisplay
,
1898 crtc_req
->x
, crtc_req
->y
,
1899 fb
->width
, fb
->height
);
1905 if (crtc_req
->count_connectors
== 0 && mode
) {
1906 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1911 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
1912 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1913 crtc_req
->count_connectors
);
1918 if (crtc_req
->count_connectors
> 0) {
1921 /* Avoid unbounded kernel memory allocation */
1922 if (crtc_req
->count_connectors
> config
->num_connector
) {
1927 connector_set
= kmalloc(crtc_req
->count_connectors
*
1928 sizeof(struct drm_connector
*),
1930 if (!connector_set
) {
1935 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
1936 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
1937 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
1942 obj
= drm_mode_object_find(dev
, out_id
,
1943 DRM_MODE_OBJECT_CONNECTOR
);
1945 DRM_DEBUG_KMS("Connector id %d unknown\n",
1950 connector
= obj_to_connector(obj
);
1951 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1953 drm_get_connector_name(connector
));
1955 connector_set
[i
] = connector
;
1960 set
.x
= crtc_req
->x
;
1961 set
.y
= crtc_req
->y
;
1963 set
.connectors
= connector_set
;
1964 set
.num_connectors
= crtc_req
->count_connectors
;
1966 ret
= crtc
->funcs
->set_config(&set
);
1969 kfree(connector_set
);
1970 drm_mode_destroy(dev
, mode
);
1971 mutex_unlock(&dev
->mode_config
.mutex
);
1975 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
1976 void *data
, struct drm_file
*file_priv
)
1978 struct drm_mode_cursor
*req
= data
;
1979 struct drm_mode_object
*obj
;
1980 struct drm_crtc
*crtc
;
1983 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1989 mutex_lock(&dev
->mode_config
.mutex
);
1990 obj
= drm_mode_object_find(dev
, req
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
1992 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
1996 crtc
= obj_to_crtc(obj
);
1998 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
1999 if (!crtc
->funcs
->cursor_set
) {
2003 /* Turns off the cursor if handle is 0 */
2004 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2005 req
->width
, req
->height
);
2008 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2009 if (crtc
->funcs
->cursor_move
) {
2010 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2017 mutex_unlock(&dev
->mode_config
.mutex
);
2021 /* Original addfb only supported RGB formats, so figure out which one */
2022 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2028 fmt
= DRM_FORMAT_RGB332
;
2032 fmt
= DRM_FORMAT_XRGB1555
;
2034 fmt
= DRM_FORMAT_RGB565
;
2037 fmt
= DRM_FORMAT_RGB888
;
2041 fmt
= DRM_FORMAT_XRGB8888
;
2042 else if (depth
== 30)
2043 fmt
= DRM_FORMAT_XRGB2101010
;
2045 fmt
= DRM_FORMAT_ARGB8888
;
2048 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2049 fmt
= DRM_FORMAT_XRGB8888
;
2055 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2058 * drm_mode_addfb - add an FB to the graphics configuration
2059 * @inode: inode from the ioctl
2060 * @filp: file * from the ioctl
2061 * @cmd: cmd from ioctl
2062 * @arg: arg from ioctl
2065 * Takes mode config lock.
2067 * Add a new FB to the specified CRTC, given a user request.
2069 * Called by the user via ioctl.
2072 * Zero on success, errno on failure.
2074 int drm_mode_addfb(struct drm_device
*dev
,
2075 void *data
, struct drm_file
*file_priv
)
2077 struct drm_mode_fb_cmd
*or = data
;
2078 struct drm_mode_fb_cmd2 r
= {};
2079 struct drm_mode_config
*config
= &dev
->mode_config
;
2080 struct drm_framebuffer
*fb
;
2083 /* Use new struct with format internally */
2084 r
.fb_id
= or->fb_id
;
2085 r
.width
= or->width
;
2086 r
.height
= or->height
;
2087 r
.pitches
[0] = or->pitch
;
2088 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2089 r
.handles
[0] = or->handle
;
2091 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2094 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2097 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2100 mutex_lock(&dev
->mode_config
.mutex
);
2102 /* TODO check buffer is sufficiently large */
2103 /* TODO setup destructor callback */
2105 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2107 DRM_DEBUG_KMS("could not create framebuffer\n");
2112 or->fb_id
= fb
->base
.id
;
2113 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2114 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2117 mutex_unlock(&dev
->mode_config
.mutex
);
2121 static int format_check(struct drm_mode_fb_cmd2
*r
)
2123 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2127 case DRM_FORMAT_RGB332
:
2128 case DRM_FORMAT_BGR233
:
2129 case DRM_FORMAT_XRGB4444
:
2130 case DRM_FORMAT_XBGR4444
:
2131 case DRM_FORMAT_RGBX4444
:
2132 case DRM_FORMAT_BGRX4444
:
2133 case DRM_FORMAT_ARGB4444
:
2134 case DRM_FORMAT_ABGR4444
:
2135 case DRM_FORMAT_RGBA4444
:
2136 case DRM_FORMAT_BGRA4444
:
2137 case DRM_FORMAT_XRGB1555
:
2138 case DRM_FORMAT_XBGR1555
:
2139 case DRM_FORMAT_RGBX5551
:
2140 case DRM_FORMAT_BGRX5551
:
2141 case DRM_FORMAT_ARGB1555
:
2142 case DRM_FORMAT_ABGR1555
:
2143 case DRM_FORMAT_RGBA5551
:
2144 case DRM_FORMAT_BGRA5551
:
2145 case DRM_FORMAT_RGB565
:
2146 case DRM_FORMAT_BGR565
:
2147 case DRM_FORMAT_RGB888
:
2148 case DRM_FORMAT_BGR888
:
2149 case DRM_FORMAT_XRGB8888
:
2150 case DRM_FORMAT_XBGR8888
:
2151 case DRM_FORMAT_RGBX8888
:
2152 case DRM_FORMAT_BGRX8888
:
2153 case DRM_FORMAT_ARGB8888
:
2154 case DRM_FORMAT_ABGR8888
:
2155 case DRM_FORMAT_RGBA8888
:
2156 case DRM_FORMAT_BGRA8888
:
2157 case DRM_FORMAT_XRGB2101010
:
2158 case DRM_FORMAT_XBGR2101010
:
2159 case DRM_FORMAT_RGBX1010102
:
2160 case DRM_FORMAT_BGRX1010102
:
2161 case DRM_FORMAT_ARGB2101010
:
2162 case DRM_FORMAT_ABGR2101010
:
2163 case DRM_FORMAT_RGBA1010102
:
2164 case DRM_FORMAT_BGRA1010102
:
2165 case DRM_FORMAT_YUYV
:
2166 case DRM_FORMAT_YVYU
:
2167 case DRM_FORMAT_UYVY
:
2168 case DRM_FORMAT_VYUY
:
2169 case DRM_FORMAT_AYUV
:
2170 case DRM_FORMAT_NV12
:
2171 case DRM_FORMAT_NV21
:
2172 case DRM_FORMAT_NV16
:
2173 case DRM_FORMAT_NV61
:
2174 case DRM_FORMAT_YUV410
:
2175 case DRM_FORMAT_YVU410
:
2176 case DRM_FORMAT_YUV411
:
2177 case DRM_FORMAT_YVU411
:
2178 case DRM_FORMAT_YUV420
:
2179 case DRM_FORMAT_YVU420
:
2180 case DRM_FORMAT_YUV422
:
2181 case DRM_FORMAT_YVU422
:
2182 case DRM_FORMAT_YUV444
:
2183 case DRM_FORMAT_YVU444
:
2190 static int framebuffer_check(struct drm_mode_fb_cmd2
*r
)
2192 int ret
, hsub
, vsub
, num_planes
, i
;
2194 ret
= format_check(r
);
2196 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r
->pixel_format
);
2200 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
2201 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
2202 num_planes
= drm_format_num_planes(r
->pixel_format
);
2204 if (r
->width
== 0 || r
->width
% hsub
) {
2205 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
2209 if (r
->height
== 0 || r
->height
% vsub
) {
2210 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
2214 for (i
= 0; i
< num_planes
; i
++) {
2215 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
2217 if (!r
->handles
[i
]) {
2218 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
2222 if (r
->pitches
[i
] < drm_format_plane_cpp(r
->pixel_format
, i
) * width
) {
2223 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
2232 * drm_mode_addfb2 - add an FB to the graphics configuration
2233 * @inode: inode from the ioctl
2234 * @filp: file * from the ioctl
2235 * @cmd: cmd from ioctl
2236 * @arg: arg from ioctl
2239 * Takes mode config lock.
2241 * Add a new FB to the specified CRTC, given a user request with format.
2243 * Called by the user via ioctl.
2246 * Zero on success, errno on failure.
2248 int drm_mode_addfb2(struct drm_device
*dev
,
2249 void *data
, struct drm_file
*file_priv
)
2251 struct drm_mode_fb_cmd2
*r
= data
;
2252 struct drm_mode_config
*config
= &dev
->mode_config
;
2253 struct drm_framebuffer
*fb
;
2256 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2259 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
2260 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2261 r
->width
, config
->min_width
, config
->max_width
);
2264 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
2265 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2266 r
->height
, config
->min_height
, config
->max_height
);
2270 ret
= framebuffer_check(r
);
2274 mutex_lock(&dev
->mode_config
.mutex
);
2276 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
2278 DRM_DEBUG_KMS("could not create framebuffer\n");
2283 r
->fb_id
= fb
->base
.id
;
2284 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2285 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2288 mutex_unlock(&dev
->mode_config
.mutex
);
2293 * drm_mode_rmfb - remove an FB from the configuration
2294 * @inode: inode from the ioctl
2295 * @filp: file * from the ioctl
2296 * @cmd: cmd from ioctl
2297 * @arg: arg from ioctl
2300 * Takes mode config lock.
2302 * Remove the FB specified by the user.
2304 * Called by the user via ioctl.
2307 * Zero on success, errno on failure.
2309 int drm_mode_rmfb(struct drm_device
*dev
,
2310 void *data
, struct drm_file
*file_priv
)
2312 struct drm_mode_object
*obj
;
2313 struct drm_framebuffer
*fb
= NULL
;
2314 struct drm_framebuffer
*fbl
= NULL
;
2315 uint32_t *id
= data
;
2319 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2322 mutex_lock(&dev
->mode_config
.mutex
);
2323 obj
= drm_mode_object_find(dev
, *id
, DRM_MODE_OBJECT_FB
);
2324 /* TODO check that we really get a framebuffer back. */
2329 fb
= obj_to_fb(obj
);
2331 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
2340 /* TODO release all crtc connected to the framebuffer */
2341 /* TODO unhock the destructor from the buffer object */
2343 list_del(&fb
->filp_head
);
2344 fb
->funcs
->destroy(fb
);
2347 mutex_unlock(&dev
->mode_config
.mutex
);
2352 * drm_mode_getfb - get FB info
2353 * @inode: inode from the ioctl
2354 * @filp: file * from the ioctl
2355 * @cmd: cmd from ioctl
2356 * @arg: arg from ioctl
2359 * Takes mode config lock.
2361 * Lookup the FB given its ID and return info about it.
2363 * Called by the user via ioctl.
2366 * Zero on success, errno on failure.
2368 int drm_mode_getfb(struct drm_device
*dev
,
2369 void *data
, struct drm_file
*file_priv
)
2371 struct drm_mode_fb_cmd
*r
= data
;
2372 struct drm_mode_object
*obj
;
2373 struct drm_framebuffer
*fb
;
2376 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2379 mutex_lock(&dev
->mode_config
.mutex
);
2380 obj
= drm_mode_object_find(dev
, r
->fb_id
, DRM_MODE_OBJECT_FB
);
2385 fb
= obj_to_fb(obj
);
2387 r
->height
= fb
->height
;
2388 r
->width
= fb
->width
;
2389 r
->depth
= fb
->depth
;
2390 r
->bpp
= fb
->bits_per_pixel
;
2391 r
->pitch
= fb
->pitches
[0];
2392 fb
->funcs
->create_handle(fb
, file_priv
, &r
->handle
);
2395 mutex_unlock(&dev
->mode_config
.mutex
);
2399 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
2400 void *data
, struct drm_file
*file_priv
)
2402 struct drm_clip_rect __user
*clips_ptr
;
2403 struct drm_clip_rect
*clips
= NULL
;
2404 struct drm_mode_fb_dirty_cmd
*r
= data
;
2405 struct drm_mode_object
*obj
;
2406 struct drm_framebuffer
*fb
;
2411 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2414 mutex_lock(&dev
->mode_config
.mutex
);
2415 obj
= drm_mode_object_find(dev
, r
->fb_id
, DRM_MODE_OBJECT_FB
);
2420 fb
= obj_to_fb(obj
);
2422 num_clips
= r
->num_clips
;
2423 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
2425 if (!num_clips
!= !clips_ptr
) {
2430 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
2432 /* If userspace annotates copy, clips must come in pairs */
2433 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
2438 if (num_clips
&& clips_ptr
) {
2439 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
2443 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
2449 ret
= copy_from_user(clips
, clips_ptr
,
2450 num_clips
* sizeof(*clips
));
2457 if (fb
->funcs
->dirty
) {
2458 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
2468 mutex_unlock(&dev
->mode_config
.mutex
);
2474 * drm_fb_release - remove and free the FBs on this file
2475 * @filp: file * from the ioctl
2478 * Takes mode config lock.
2480 * Destroy all the FBs associated with @filp.
2482 * Called by the user via ioctl.
2485 * Zero on success, errno on failure.
2487 void drm_fb_release(struct drm_file
*priv
)
2489 struct drm_device
*dev
= priv
->minor
->dev
;
2490 struct drm_framebuffer
*fb
, *tfb
;
2492 mutex_lock(&dev
->mode_config
.mutex
);
2493 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
2494 list_del(&fb
->filp_head
);
2495 fb
->funcs
->destroy(fb
);
2497 mutex_unlock(&dev
->mode_config
.mutex
);
2501 * drm_mode_attachmode - add a mode to the user mode list
2503 * @connector: connector to add the mode to
2504 * @mode: mode to add
2506 * Add @mode to @connector's user mode list.
2508 static void drm_mode_attachmode(struct drm_device
*dev
,
2509 struct drm_connector
*connector
,
2510 struct drm_display_mode
*mode
)
2512 list_add_tail(&mode
->head
, &connector
->user_modes
);
2515 int drm_mode_attachmode_crtc(struct drm_device
*dev
, struct drm_crtc
*crtc
,
2516 const struct drm_display_mode
*mode
)
2518 struct drm_connector
*connector
;
2520 struct drm_display_mode
*dup_mode
, *next
;
2523 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
2524 if (!connector
->encoder
)
2526 if (connector
->encoder
->crtc
== crtc
) {
2527 dup_mode
= drm_mode_duplicate(dev
, mode
);
2532 list_add_tail(&dup_mode
->head
, &list
);
2536 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
2537 if (!connector
->encoder
)
2539 if (connector
->encoder
->crtc
== crtc
)
2540 list_move_tail(list
.next
, &connector
->user_modes
);
2543 WARN_ON(!list_empty(&list
));
2546 list_for_each_entry_safe(dup_mode
, next
, &list
, head
)
2547 drm_mode_destroy(dev
, dup_mode
);
2551 EXPORT_SYMBOL(drm_mode_attachmode_crtc
);
2553 static int drm_mode_detachmode(struct drm_device
*dev
,
2554 struct drm_connector
*connector
,
2555 struct drm_display_mode
*mode
)
2559 struct drm_display_mode
*match_mode
, *t
;
2561 list_for_each_entry_safe(match_mode
, t
, &connector
->user_modes
, head
) {
2562 if (drm_mode_equal(match_mode
, mode
)) {
2563 list_del(&match_mode
->head
);
2564 drm_mode_destroy(dev
, match_mode
);
2576 int drm_mode_detachmode_crtc(struct drm_device
*dev
, struct drm_display_mode
*mode
)
2578 struct drm_connector
*connector
;
2580 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
2581 drm_mode_detachmode(dev
, connector
, mode
);
2585 EXPORT_SYMBOL(drm_mode_detachmode_crtc
);
2588 * drm_fb_attachmode - Attach a user mode to an connector
2589 * @inode: inode from the ioctl
2590 * @filp: file * from the ioctl
2591 * @cmd: cmd from ioctl
2592 * @arg: arg from ioctl
2594 * This attaches a user specified mode to an connector.
2595 * Called by the user via ioctl.
2598 * Zero on success, errno on failure.
2600 int drm_mode_attachmode_ioctl(struct drm_device
*dev
,
2601 void *data
, struct drm_file
*file_priv
)
2603 struct drm_mode_mode_cmd
*mode_cmd
= data
;
2604 struct drm_connector
*connector
;
2605 struct drm_display_mode
*mode
;
2606 struct drm_mode_object
*obj
;
2607 struct drm_mode_modeinfo
*umode
= &mode_cmd
->mode
;
2610 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2613 mutex_lock(&dev
->mode_config
.mutex
);
2615 obj
= drm_mode_object_find(dev
, mode_cmd
->connector_id
, DRM_MODE_OBJECT_CONNECTOR
);
2620 connector
= obj_to_connector(obj
);
2622 mode
= drm_mode_create(dev
);
2628 ret
= drm_crtc_convert_umode(mode
, umode
);
2630 DRM_DEBUG_KMS("Invalid mode\n");
2631 drm_mode_destroy(dev
, mode
);
2635 drm_mode_attachmode(dev
, connector
, mode
);
2637 mutex_unlock(&dev
->mode_config
.mutex
);
2643 * drm_fb_detachmode - Detach a user specified mode from an connector
2644 * @inode: inode from the ioctl
2645 * @filp: file * from the ioctl
2646 * @cmd: cmd from ioctl
2647 * @arg: arg from ioctl
2649 * Called by the user via ioctl.
2652 * Zero on success, errno on failure.
2654 int drm_mode_detachmode_ioctl(struct drm_device
*dev
,
2655 void *data
, struct drm_file
*file_priv
)
2657 struct drm_mode_object
*obj
;
2658 struct drm_mode_mode_cmd
*mode_cmd
= data
;
2659 struct drm_connector
*connector
;
2660 struct drm_display_mode mode
;
2661 struct drm_mode_modeinfo
*umode
= &mode_cmd
->mode
;
2664 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2667 mutex_lock(&dev
->mode_config
.mutex
);
2669 obj
= drm_mode_object_find(dev
, mode_cmd
->connector_id
, DRM_MODE_OBJECT_CONNECTOR
);
2674 connector
= obj_to_connector(obj
);
2676 ret
= drm_crtc_convert_umode(&mode
, umode
);
2678 DRM_DEBUG_KMS("Invalid mode\n");
2682 ret
= drm_mode_detachmode(dev
, connector
, &mode
);
2684 mutex_unlock(&dev
->mode_config
.mutex
);
2688 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
2689 const char *name
, int num_values
)
2691 struct drm_property
*property
= NULL
;
2694 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
2699 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
2700 if (!property
->values
)
2704 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
2708 property
->flags
= flags
;
2709 property
->num_values
= num_values
;
2710 INIT_LIST_HEAD(&property
->enum_blob_list
);
2713 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
2714 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2717 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
2720 kfree(property
->values
);
2724 EXPORT_SYMBOL(drm_property_create
);
2726 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
2728 const struct drm_prop_enum_list
*props
,
2731 struct drm_property
*property
;
2734 flags
|= DRM_MODE_PROP_ENUM
;
2736 property
= drm_property_create(dev
, flags
, name
, num_values
);
2740 for (i
= 0; i
< num_values
; i
++) {
2741 ret
= drm_property_add_enum(property
, i
,
2745 drm_property_destroy(dev
, property
);
2752 EXPORT_SYMBOL(drm_property_create_enum
);
2754 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
2756 uint64_t min
, uint64_t max
)
2758 struct drm_property
*property
;
2760 flags
|= DRM_MODE_PROP_RANGE
;
2762 property
= drm_property_create(dev
, flags
, name
, 2);
2766 property
->values
[0] = min
;
2767 property
->values
[1] = max
;
2771 EXPORT_SYMBOL(drm_property_create_range
);
2773 int drm_property_add_enum(struct drm_property
*property
, int index
,
2774 uint64_t value
, const char *name
)
2776 struct drm_property_enum
*prop_enum
;
2778 if (!(property
->flags
& DRM_MODE_PROP_ENUM
))
2781 if (!list_empty(&property
->enum_blob_list
)) {
2782 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
2783 if (prop_enum
->value
== value
) {
2784 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
2785 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2791 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
2795 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
2796 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2797 prop_enum
->value
= value
;
2799 property
->values
[index
] = value
;
2800 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
2803 EXPORT_SYMBOL(drm_property_add_enum
);
2805 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
2807 struct drm_property_enum
*prop_enum
, *pt
;
2809 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
2810 list_del(&prop_enum
->head
);
2814 if (property
->num_values
)
2815 kfree(property
->values
);
2816 drm_mode_object_put(dev
, &property
->base
);
2817 list_del(&property
->head
);
2820 EXPORT_SYMBOL(drm_property_destroy
);
2822 int drm_connector_attach_property(struct drm_connector
*connector
,
2823 struct drm_property
*property
, uint64_t init_val
)
2827 for (i
= 0; i
< DRM_CONNECTOR_MAX_PROPERTY
; i
++) {
2828 if (connector
->property_ids
[i
] == 0) {
2829 connector
->property_ids
[i
] = property
->base
.id
;
2830 connector
->property_values
[i
] = init_val
;
2835 if (i
== DRM_CONNECTOR_MAX_PROPERTY
)
2839 EXPORT_SYMBOL(drm_connector_attach_property
);
2841 int drm_connector_property_set_value(struct drm_connector
*connector
,
2842 struct drm_property
*property
, uint64_t value
)
2846 for (i
= 0; i
< DRM_CONNECTOR_MAX_PROPERTY
; i
++) {
2847 if (connector
->property_ids
[i
] == property
->base
.id
) {
2848 connector
->property_values
[i
] = value
;
2853 if (i
== DRM_CONNECTOR_MAX_PROPERTY
)
2857 EXPORT_SYMBOL(drm_connector_property_set_value
);
2859 int drm_connector_property_get_value(struct drm_connector
*connector
,
2860 struct drm_property
*property
, uint64_t *val
)
2864 for (i
= 0; i
< DRM_CONNECTOR_MAX_PROPERTY
; i
++) {
2865 if (connector
->property_ids
[i
] == property
->base
.id
) {
2866 *val
= connector
->property_values
[i
];
2871 if (i
== DRM_CONNECTOR_MAX_PROPERTY
)
2875 EXPORT_SYMBOL(drm_connector_property_get_value
);
2877 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
2878 void *data
, struct drm_file
*file_priv
)
2880 struct drm_mode_object
*obj
;
2881 struct drm_mode_get_property
*out_resp
= data
;
2882 struct drm_property
*property
;
2885 int value_count
= 0;
2888 struct drm_property_enum
*prop_enum
;
2889 struct drm_mode_property_enum __user
*enum_ptr
;
2890 struct drm_property_blob
*prop_blob
;
2891 uint32_t __user
*blob_id_ptr
;
2892 uint64_t __user
*values_ptr
;
2893 uint32_t __user
*blob_length_ptr
;
2895 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2898 mutex_lock(&dev
->mode_config
.mutex
);
2899 obj
= drm_mode_object_find(dev
, out_resp
->prop_id
, DRM_MODE_OBJECT_PROPERTY
);
2904 property
= obj_to_property(obj
);
2906 if (property
->flags
& DRM_MODE_PROP_ENUM
) {
2907 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
2909 } else if (property
->flags
& DRM_MODE_PROP_BLOB
) {
2910 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
2914 value_count
= property
->num_values
;
2916 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
2917 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
2918 out_resp
->flags
= property
->flags
;
2920 if ((out_resp
->count_values
>= value_count
) && value_count
) {
2921 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
2922 for (i
= 0; i
< value_count
; i
++) {
2923 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
2929 out_resp
->count_values
= value_count
;
2931 if (property
->flags
& DRM_MODE_PROP_ENUM
) {
2932 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
2934 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
2935 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
2937 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
2942 if (copy_to_user(&enum_ptr
[copied
].name
,
2943 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
2950 out_resp
->count_enum_blobs
= enum_count
;
2953 if (property
->flags
& DRM_MODE_PROP_BLOB
) {
2954 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
2956 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
2957 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
2959 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
2960 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
2965 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
2973 out_resp
->count_enum_blobs
= blob_count
;
2976 mutex_unlock(&dev
->mode_config
.mutex
);
2980 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
2983 struct drm_property_blob
*blob
;
2986 if (!length
|| !data
)
2989 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
2993 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
2999 blob
->length
= length
;
3001 memcpy(blob
->data
, data
, length
);
3003 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3007 static void drm_property_destroy_blob(struct drm_device
*dev
,
3008 struct drm_property_blob
*blob
)
3010 drm_mode_object_put(dev
, &blob
->base
);
3011 list_del(&blob
->head
);
3015 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3016 void *data
, struct drm_file
*file_priv
)
3018 struct drm_mode_object
*obj
;
3019 struct drm_mode_get_blob
*out_resp
= data
;
3020 struct drm_property_blob
*blob
;
3022 void __user
*blob_ptr
;
3024 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3027 mutex_lock(&dev
->mode_config
.mutex
);
3028 obj
= drm_mode_object_find(dev
, out_resp
->blob_id
, DRM_MODE_OBJECT_BLOB
);
3033 blob
= obj_to_blob(obj
);
3035 if (out_resp
->length
== blob
->length
) {
3036 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3037 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3042 out_resp
->length
= blob
->length
;
3045 mutex_unlock(&dev
->mode_config
.mutex
);
3049 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
3052 struct drm_device
*dev
= connector
->dev
;
3055 if (connector
->edid_blob_ptr
)
3056 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
3058 /* Delete edid, when there is none. */
3060 connector
->edid_blob_ptr
= NULL
;
3061 ret
= drm_connector_property_set_value(connector
, dev
->mode_config
.edid_property
, 0);
3065 size
= EDID_LENGTH
* (1 + edid
->extensions
);
3066 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
3069 ret
= drm_connector_property_set_value(connector
,
3070 dev
->mode_config
.edid_property
,
3071 connector
->edid_blob_ptr
->base
.id
);
3075 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
3077 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
3078 void *data
, struct drm_file
*file_priv
)
3080 struct drm_mode_connector_set_property
*out_resp
= data
;
3081 struct drm_mode_object
*obj
;
3082 struct drm_property
*property
;
3083 struct drm_connector
*connector
;
3087 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3090 mutex_lock(&dev
->mode_config
.mutex
);
3092 obj
= drm_mode_object_find(dev
, out_resp
->connector_id
, DRM_MODE_OBJECT_CONNECTOR
);
3096 connector
= obj_to_connector(obj
);
3098 for (i
= 0; i
< DRM_CONNECTOR_MAX_PROPERTY
; i
++) {
3099 if (connector
->property_ids
[i
] == out_resp
->prop_id
)
3103 if (i
== DRM_CONNECTOR_MAX_PROPERTY
) {
3107 obj
= drm_mode_object_find(dev
, out_resp
->prop_id
, DRM_MODE_OBJECT_PROPERTY
);
3111 property
= obj_to_property(obj
);
3113 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
3116 if (property
->flags
& DRM_MODE_PROP_RANGE
) {
3117 if (out_resp
->value
< property
->values
[0])
3120 if (out_resp
->value
> property
->values
[1])
3124 for (i
= 0; i
< property
->num_values
; i
++) {
3125 if (property
->values
[i
] == out_resp
->value
) {
3135 /* Do DPMS ourselves */
3136 if (property
== connector
->dev
->mode_config
.dpms_property
) {
3137 if (connector
->funcs
->dpms
)
3138 (*connector
->funcs
->dpms
)(connector
, (int) out_resp
->value
);
3140 } else if (connector
->funcs
->set_property
)
3141 ret
= connector
->funcs
->set_property(connector
, property
, out_resp
->value
);
3143 /* store the property value if successful */
3145 drm_connector_property_set_value(connector
, property
, out_resp
->value
);
3147 mutex_unlock(&dev
->mode_config
.mutex
);
3151 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
3152 struct drm_encoder
*encoder
)
3156 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
3157 if (connector
->encoder_ids
[i
] == 0) {
3158 connector
->encoder_ids
[i
] = encoder
->base
.id
;
3164 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
3166 void drm_mode_connector_detach_encoder(struct drm_connector
*connector
,
3167 struct drm_encoder
*encoder
)
3170 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
3171 if (connector
->encoder_ids
[i
] == encoder
->base
.id
) {
3172 connector
->encoder_ids
[i
] = 0;
3173 if (connector
->encoder
== encoder
)
3174 connector
->encoder
= NULL
;
3179 EXPORT_SYMBOL(drm_mode_connector_detach_encoder
);
3181 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
3184 crtc
->gamma_size
= gamma_size
;
3186 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
3187 if (!crtc
->gamma_store
) {
3188 crtc
->gamma_size
= 0;
3194 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
3196 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
3197 void *data
, struct drm_file
*file_priv
)
3199 struct drm_mode_crtc_lut
*crtc_lut
= data
;
3200 struct drm_mode_object
*obj
;
3201 struct drm_crtc
*crtc
;
3202 void *r_base
, *g_base
, *b_base
;
3206 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3209 mutex_lock(&dev
->mode_config
.mutex
);
3210 obj
= drm_mode_object_find(dev
, crtc_lut
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3215 crtc
= obj_to_crtc(obj
);
3217 /* memcpy into gamma store */
3218 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
3223 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
3224 r_base
= crtc
->gamma_store
;
3225 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
3230 g_base
= r_base
+ size
;
3231 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
3236 b_base
= g_base
+ size
;
3237 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
3242 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
3245 mutex_unlock(&dev
->mode_config
.mutex
);
3250 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
3251 void *data
, struct drm_file
*file_priv
)
3253 struct drm_mode_crtc_lut
*crtc_lut
= data
;
3254 struct drm_mode_object
*obj
;
3255 struct drm_crtc
*crtc
;
3256 void *r_base
, *g_base
, *b_base
;
3260 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3263 mutex_lock(&dev
->mode_config
.mutex
);
3264 obj
= drm_mode_object_find(dev
, crtc_lut
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3269 crtc
= obj_to_crtc(obj
);
3271 /* memcpy into gamma store */
3272 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
3277 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
3278 r_base
= crtc
->gamma_store
;
3279 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
3284 g_base
= r_base
+ size
;
3285 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
3290 b_base
= g_base
+ size
;
3291 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
3296 mutex_unlock(&dev
->mode_config
.mutex
);
3300 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
3301 void *data
, struct drm_file
*file_priv
)
3303 struct drm_mode_crtc_page_flip
*page_flip
= data
;
3304 struct drm_mode_object
*obj
;
3305 struct drm_crtc
*crtc
;
3306 struct drm_framebuffer
*fb
;
3307 struct drm_pending_vblank_event
*e
= NULL
;
3308 unsigned long flags
;
3311 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
3312 page_flip
->reserved
!= 0)
3315 mutex_lock(&dev
->mode_config
.mutex
);
3316 obj
= drm_mode_object_find(dev
, page_flip
->crtc_id
, DRM_MODE_OBJECT_CRTC
);
3319 crtc
= obj_to_crtc(obj
);
3321 if (crtc
->fb
== NULL
) {
3322 /* The framebuffer is currently unbound, presumably
3323 * due to a hotplug event, that userspace has not
3330 if (crtc
->funcs
->page_flip
== NULL
)
3333 obj
= drm_mode_object_find(dev
, page_flip
->fb_id
, DRM_MODE_OBJECT_FB
);
3336 fb
= obj_to_fb(obj
);
3338 if (crtc
->mode
.hdisplay
> fb
->width
||
3339 crtc
->mode
.vdisplay
> fb
->height
||
3340 crtc
->x
> fb
->width
- crtc
->mode
.hdisplay
||
3341 crtc
->y
> fb
->height
- crtc
->mode
.vdisplay
) {
3342 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
3343 fb
->width
, fb
->height
,
3344 crtc
->mode
.hdisplay
, crtc
->mode
.vdisplay
,
3350 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
3352 spin_lock_irqsave(&dev
->event_lock
, flags
);
3353 if (file_priv
->event_space
< sizeof e
->event
) {
3354 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3357 file_priv
->event_space
-= sizeof e
->event
;
3358 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3360 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
3362 spin_lock_irqsave(&dev
->event_lock
, flags
);
3363 file_priv
->event_space
+= sizeof e
->event
;
3364 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3368 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
3369 e
->event
.base
.length
= sizeof e
->event
;
3370 e
->event
.user_data
= page_flip
->user_data
;
3371 e
->base
.event
= &e
->event
.base
;
3372 e
->base
.file_priv
= file_priv
;
3374 (void (*) (struct drm_pending_event
*)) kfree
;
3377 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
);
3379 spin_lock_irqsave(&dev
->event_lock
, flags
);
3380 file_priv
->event_space
+= sizeof e
->event
;
3381 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
3386 mutex_unlock(&dev
->mode_config
.mutex
);
3390 void drm_mode_config_reset(struct drm_device
*dev
)
3392 struct drm_crtc
*crtc
;
3393 struct drm_encoder
*encoder
;
3394 struct drm_connector
*connector
;
3396 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
3397 if (crtc
->funcs
->reset
)
3398 crtc
->funcs
->reset(crtc
);
3400 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
3401 if (encoder
->funcs
->reset
)
3402 encoder
->funcs
->reset(encoder
);
3404 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
3405 if (connector
->funcs
->reset
)
3406 connector
->funcs
->reset(connector
);
3408 EXPORT_SYMBOL(drm_mode_config_reset
);
3410 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
3411 void *data
, struct drm_file
*file_priv
)
3413 struct drm_mode_create_dumb
*args
= data
;
3415 if (!dev
->driver
->dumb_create
)
3417 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
3420 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
3421 void *data
, struct drm_file
*file_priv
)
3423 struct drm_mode_map_dumb
*args
= data
;
3425 /* call driver ioctl to get mmap offset */
3426 if (!dev
->driver
->dumb_map_offset
)
3429 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
3432 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
3433 void *data
, struct drm_file
*file_priv
)
3435 struct drm_mode_destroy_dumb
*args
= data
;
3437 if (!dev
->driver
->dumb_destroy
)
3440 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
3444 * Just need to support RGB formats here for compat with code that doesn't
3445 * use pixel formats directly yet.
3447 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
3451 case DRM_FORMAT_RGB332
:
3452 case DRM_FORMAT_BGR233
:
3456 case DRM_FORMAT_XRGB1555
:
3457 case DRM_FORMAT_XBGR1555
:
3458 case DRM_FORMAT_RGBX5551
:
3459 case DRM_FORMAT_BGRX5551
:
3460 case DRM_FORMAT_ARGB1555
:
3461 case DRM_FORMAT_ABGR1555
:
3462 case DRM_FORMAT_RGBA5551
:
3463 case DRM_FORMAT_BGRA5551
:
3467 case DRM_FORMAT_RGB565
:
3468 case DRM_FORMAT_BGR565
:
3472 case DRM_FORMAT_RGB888
:
3473 case DRM_FORMAT_BGR888
:
3477 case DRM_FORMAT_XRGB8888
:
3478 case DRM_FORMAT_XBGR8888
:
3479 case DRM_FORMAT_RGBX8888
:
3480 case DRM_FORMAT_BGRX8888
:
3484 case DRM_FORMAT_XRGB2101010
:
3485 case DRM_FORMAT_XBGR2101010
:
3486 case DRM_FORMAT_RGBX1010102
:
3487 case DRM_FORMAT_BGRX1010102
:
3488 case DRM_FORMAT_ARGB2101010
:
3489 case DRM_FORMAT_ABGR2101010
:
3490 case DRM_FORMAT_RGBA1010102
:
3491 case DRM_FORMAT_BGRA1010102
:
3495 case DRM_FORMAT_ARGB8888
:
3496 case DRM_FORMAT_ABGR8888
:
3497 case DRM_FORMAT_RGBA8888
:
3498 case DRM_FORMAT_BGRA8888
:
3503 DRM_DEBUG_KMS("unsupported pixel format\n");
3509 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
3512 * drm_format_num_planes - get the number of planes for format
3513 * @format: pixel format (DRM_FORMAT_*)
3516 * The number of planes used by the specified pixel format.
3518 int drm_format_num_planes(uint32_t format
)
3521 case DRM_FORMAT_YUV410
:
3522 case DRM_FORMAT_YVU410
:
3523 case DRM_FORMAT_YUV411
:
3524 case DRM_FORMAT_YVU411
:
3525 case DRM_FORMAT_YUV420
:
3526 case DRM_FORMAT_YVU420
:
3527 case DRM_FORMAT_YUV422
:
3528 case DRM_FORMAT_YVU422
:
3529 case DRM_FORMAT_YUV444
:
3530 case DRM_FORMAT_YVU444
:
3532 case DRM_FORMAT_NV12
:
3533 case DRM_FORMAT_NV21
:
3534 case DRM_FORMAT_NV16
:
3535 case DRM_FORMAT_NV61
:
3541 EXPORT_SYMBOL(drm_format_num_planes
);
3544 * drm_format_plane_cpp - determine the bytes per pixel value
3545 * @format: pixel format (DRM_FORMAT_*)
3546 * @plane: plane index
3549 * The bytes per pixel value for the specified plane.
3551 int drm_format_plane_cpp(uint32_t format
, int plane
)
3556 if (plane
>= drm_format_num_planes(format
))
3560 case DRM_FORMAT_YUYV
:
3561 case DRM_FORMAT_YVYU
:
3562 case DRM_FORMAT_UYVY
:
3563 case DRM_FORMAT_VYUY
:
3565 case DRM_FORMAT_NV12
:
3566 case DRM_FORMAT_NV21
:
3567 case DRM_FORMAT_NV16
:
3568 case DRM_FORMAT_NV61
:
3569 return plane
? 2 : 1;
3570 case DRM_FORMAT_YUV410
:
3571 case DRM_FORMAT_YVU410
:
3572 case DRM_FORMAT_YUV411
:
3573 case DRM_FORMAT_YVU411
:
3574 case DRM_FORMAT_YUV420
:
3575 case DRM_FORMAT_YVU420
:
3576 case DRM_FORMAT_YUV422
:
3577 case DRM_FORMAT_YVU422
:
3578 case DRM_FORMAT_YUV444
:
3579 case DRM_FORMAT_YVU444
:
3582 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
3586 EXPORT_SYMBOL(drm_format_plane_cpp
);
3589 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3590 * @format: pixel format (DRM_FORMAT_*)
3593 * The horizontal chroma subsampling factor for the
3594 * specified pixel format.
3596 int drm_format_horz_chroma_subsampling(uint32_t format
)
3599 case DRM_FORMAT_YUV411
:
3600 case DRM_FORMAT_YVU411
:
3601 case DRM_FORMAT_YUV410
:
3602 case DRM_FORMAT_YVU410
:
3604 case DRM_FORMAT_YUYV
:
3605 case DRM_FORMAT_YVYU
:
3606 case DRM_FORMAT_UYVY
:
3607 case DRM_FORMAT_VYUY
:
3608 case DRM_FORMAT_NV12
:
3609 case DRM_FORMAT_NV21
:
3610 case DRM_FORMAT_NV16
:
3611 case DRM_FORMAT_NV61
:
3612 case DRM_FORMAT_YUV422
:
3613 case DRM_FORMAT_YVU422
:
3614 case DRM_FORMAT_YUV420
:
3615 case DRM_FORMAT_YVU420
:
3621 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
3624 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3625 * @format: pixel format (DRM_FORMAT_*)
3628 * The vertical chroma subsampling factor for the
3629 * specified pixel format.
3631 int drm_format_vert_chroma_subsampling(uint32_t format
)
3634 case DRM_FORMAT_YUV410
:
3635 case DRM_FORMAT_YVU410
:
3637 case DRM_FORMAT_YUV420
:
3638 case DRM_FORMAT_YVU420
:
3639 case DRM_FORMAT_NV12
:
3640 case DRM_FORMAT_NV21
:
3646 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);