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);
240 obj->type = obj_type;
245 * drm_mode_object_put - free an identifer
250 * Caller must hold DRM mode_config lock.
252 * Free @id from @dev's unique identifier pool.
254 static void drm_mode_object_put(struct drm_device *dev,
255 struct drm_mode_object *object)
257 mutex_lock(&dev->mode_config.idr_mutex);
258 idr_remove(&dev->mode_config.crtc_idr, object->id);
259 mutex_unlock(&dev->mode_config.idr_mutex);
262 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
263 uint32_t id, uint32_t type)
265 struct drm_mode_object *obj = NULL;
267 mutex_lock(&dev->mode_config.idr_mutex);
268 obj = idr_find(&dev->mode_config.crtc_idr, id);
269 if (!obj || (obj->type != type) || (obj->id != id))
271 mutex_unlock(&dev->mode_config.idr_mutex);
275 EXPORT_SYMBOL(drm_mode_object_find);
278 * drm_framebuffer_init - initialize a framebuffer
282 * Caller must hold mode config lock.
284 * Allocates an ID for the framebuffer's parent mode object, sets its mode
285 * functions & device file and adds it to the master fd list.
288 * Zero on success, error code on failure.
290 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
291 const struct drm_framebuffer_funcs *funcs)
295 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
302 dev->mode_config.num_fb++;
303 list_add(&fb->head, &dev->mode_config.fb_list);
307 EXPORT_SYMBOL(drm_framebuffer_init);
310 * drm_framebuffer_cleanup - remove a framebuffer object
311 * @fb: framebuffer to remove
314 * Caller must hold mode config lock.
316 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
317 * it, setting it to NULL.
319 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
321 struct drm_device *dev = fb->dev;
322 struct drm_crtc *crtc;
323 struct drm_plane *plane;
324 struct drm_mode_set set;
327 /* remove from any CRTC */
328 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
329 if (crtc->fb == fb) {
330 /* should turn off the crtc */
331 memset(&set, 0, sizeof(struct drm_mode_set));
334 ret = crtc->funcs->set_config(&set);
336 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
340 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
341 if (plane->fb == fb) {
342 /* should turn off the crtc */
343 ret = plane->funcs->disable_plane(plane);
345 DRM_ERROR("failed to disable plane with busy fb\n");
346 /* disconnect the plane from the fb and crtc: */
352 drm_mode_object_put(dev, &fb->base);
354 dev->mode_config.num_fb--;
356 EXPORT_SYMBOL(drm_framebuffer_cleanup);
359 * drm_crtc_init - Initialise a new CRTC object
361 * @crtc: CRTC object to init
362 * @funcs: callbacks for the new CRTC
365 * Caller must hold mode config lock.
367 * Inits a new object created as base part of an driver crtc object.
369 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
370 const struct drm_crtc_funcs *funcs)
375 mutex_lock(&dev->mode_config.mutex);
376 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
378 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
379 dev->mode_config.num_crtc++;
380 mutex_unlock(&dev->mode_config.mutex);
382 EXPORT_SYMBOL(drm_crtc_init);
385 * drm_crtc_cleanup - Cleans up the core crtc usage.
386 * @crtc: CRTC to cleanup
389 * Caller must hold mode config lock.
391 * Cleanup @crtc. Removes from drm modesetting space
392 * does NOT free object, caller does that.
394 void drm_crtc_cleanup(struct drm_crtc *crtc)
396 struct drm_device *dev = crtc->dev;
398 if (crtc->gamma_store) {
399 kfree(crtc->gamma_store);
400 crtc->gamma_store = NULL;
403 drm_mode_object_put(dev, &crtc->base);
404 list_del(&crtc->head);
405 dev->mode_config.num_crtc--;
407 EXPORT_SYMBOL(drm_crtc_cleanup);
410 * drm_mode_probed_add - add a mode to a connector's probed mode list
411 * @connector: connector the new mode
415 * Caller must hold mode config lock.
417 * Add @mode to @connector's mode list for later use.
419 void drm_mode_probed_add(struct drm_connector *connector,
420 struct drm_display_mode *mode)
422 list_add(&mode->head, &connector->probed_modes);
424 EXPORT_SYMBOL(drm_mode_probed_add);
427 * drm_mode_remove - remove and free a mode
428 * @connector: connector list to modify
429 * @mode: mode to remove
432 * Caller must hold mode config lock.
434 * Remove @mode from @connector's mode list, then free it.
436 void drm_mode_remove(struct drm_connector *connector,
437 struct drm_display_mode *mode)
439 list_del(&mode->head);
440 drm_mode_destroy(connector->dev, mode);
442 EXPORT_SYMBOL(drm_mode_remove);
445 * drm_connector_init - Init a preallocated connector
447 * @connector: the connector to init
448 * @funcs: callbacks for this connector
449 * @name: user visible name of the connector
452 * Takes mode config lock.
454 * Initialises a preallocated connector. Connectors should be
455 * subclassed as part of driver connector objects.
457 void drm_connector_init(struct drm_device *dev,
458 struct drm_connector *connector,
459 const struct drm_connector_funcs *funcs,
462 mutex_lock(&dev->mode_config.mutex);
464 connector->dev = dev;
465 connector->funcs = funcs;
466 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
467 connector->connector_type = connector_type;
468 connector->connector_type_id =
469 ++drm_connector_enum_list[connector_type].count; /* TODO */
470 INIT_LIST_HEAD(&connector->user_modes);
471 INIT_LIST_HEAD(&connector->probed_modes);
472 INIT_LIST_HEAD(&connector->modes);
473 connector->edid_blob_ptr = NULL;
475 list_add_tail(&connector->head, &dev->mode_config.connector_list);
476 dev->mode_config.num_connector++;
478 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
479 drm_connector_attach_property(connector,
480 dev->mode_config.edid_property,
483 drm_connector_attach_property(connector,
484 dev->mode_config.dpms_property, 0);
486 mutex_unlock(&dev->mode_config.mutex);
488 EXPORT_SYMBOL(drm_connector_init);
491 * drm_connector_cleanup - cleans up an initialised connector
492 * @connector: connector to cleanup
495 * Takes mode config lock.
497 * Cleans up the connector but doesn't free the object.
499 void drm_connector_cleanup(struct drm_connector *connector)
501 struct drm_device *dev = connector->dev;
502 struct drm_display_mode *mode, *t;
504 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
505 drm_mode_remove(connector, mode);
507 list_for_each_entry_safe(mode, t, &connector->modes, head)
508 drm_mode_remove(connector, mode);
510 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
511 drm_mode_remove(connector, mode);
513 mutex_lock(&dev->mode_config.mutex);
514 drm_mode_object_put(dev, &connector->base);
515 list_del(&connector->head);
516 dev->mode_config.num_connector--;
517 mutex_unlock(&dev->mode_config.mutex);
519 EXPORT_SYMBOL(drm_connector_cleanup);
521 void drm_encoder_init(struct drm_device *dev,
522 struct drm_encoder *encoder,
523 const struct drm_encoder_funcs *funcs,
526 mutex_lock(&dev->mode_config.mutex);
530 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
531 encoder->encoder_type = encoder_type;
532 encoder->funcs = funcs;
534 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
535 dev->mode_config.num_encoder++;
537 mutex_unlock(&dev->mode_config.mutex);
539 EXPORT_SYMBOL(drm_encoder_init);
541 void drm_encoder_cleanup(struct drm_encoder *encoder)
543 struct drm_device *dev = encoder->dev;
544 mutex_lock(&dev->mode_config.mutex);
545 drm_mode_object_put(dev, &encoder->base);
546 list_del(&encoder->head);
547 dev->mode_config.num_encoder--;
548 mutex_unlock(&dev->mode_config.mutex);
550 EXPORT_SYMBOL(drm_encoder_cleanup);
552 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
553 unsigned long possible_crtcs,
554 const struct drm_plane_funcs *funcs,
555 const uint32_t *formats, uint32_t format_count,
558 mutex_lock(&dev->mode_config.mutex);
561 drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
562 plane->funcs = funcs;
563 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
565 if (!plane->format_types) {
566 DRM_DEBUG_KMS("out of memory when allocating plane\n");
567 drm_mode_object_put(dev, &plane->base);
568 mutex_unlock(&dev->mode_config.mutex);
572 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
573 plane->format_count = format_count;
574 plane->possible_crtcs = possible_crtcs;
576 /* private planes are not exposed to userspace, but depending on
577 * display hardware, might be convenient to allow sharing programming
578 * for the scanout engine with the crtc implementation.
581 list_add_tail(&plane->head, &dev->mode_config.plane_list);
582 dev->mode_config.num_plane++;
584 INIT_LIST_HEAD(&plane->head);
587 mutex_unlock(&dev->mode_config.mutex);
591 EXPORT_SYMBOL(drm_plane_init);
593 void drm_plane_cleanup(struct drm_plane *plane)
595 struct drm_device *dev = plane->dev;
597 mutex_lock(&dev->mode_config.mutex);
598 kfree(plane->format_types);
599 drm_mode_object_put(dev, &plane->base);
600 /* if not added to a list, it must be a private plane */
601 if (!list_empty(&plane->head)) {
602 list_del(&plane->head);
603 dev->mode_config.num_plane--;
605 mutex_unlock(&dev->mode_config.mutex);
607 EXPORT_SYMBOL(drm_plane_cleanup);
610 * drm_mode_create - create a new display mode
614 * Caller must hold DRM mode_config lock.
616 * Create a new drm_display_mode, give it an ID, and return it.
619 * Pointer to new mode on success, NULL on error.
621 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
623 struct drm_display_mode *nmode;
625 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
629 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
632 EXPORT_SYMBOL(drm_mode_create);
635 * drm_mode_destroy - remove a mode
637 * @mode: mode to remove
640 * Caller must hold mode config lock.
642 * Free @mode's unique identifier, then free it.
644 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
646 drm_mode_object_put(dev, &mode->base);
650 EXPORT_SYMBOL(drm_mode_destroy);
652 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
654 struct drm_property *edid;
655 struct drm_property *dpms;
658 * Standard properties (apply to all connectors)
660 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
661 DRM_MODE_PROP_IMMUTABLE,
663 dev->mode_config.edid_property = edid;
665 dpms = drm_property_create_enum(dev, 0,
666 "DPMS", drm_dpms_enum_list,
667 ARRAY_SIZE(drm_dpms_enum_list));
668 dev->mode_config.dpms_property = dpms;
674 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
677 * Called by a driver the first time a DVI-I connector is made.
679 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
681 struct drm_property *dvi_i_selector;
682 struct drm_property *dvi_i_subconnector;
684 if (dev->mode_config.dvi_i_select_subconnector_property)
688 drm_property_create_enum(dev, 0,
689 "select subconnector",
690 drm_dvi_i_select_enum_list,
691 ARRAY_SIZE(drm_dvi_i_select_enum_list));
692 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
694 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
696 drm_dvi_i_subconnector_enum_list,
697 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
698 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
702 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
705 * drm_create_tv_properties - create TV specific connector properties
707 * @num_modes: number of different TV formats (modes) supported
708 * @modes: array of pointers to strings containing name of each format
710 * Called by a driver's TV initialization routine, this function creates
711 * the TV specific connector properties for a given device. Caller is
712 * responsible for allocating a list of format names and passing them to
715 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
718 struct drm_property *tv_selector;
719 struct drm_property *tv_subconnector;
722 if (dev->mode_config.tv_select_subconnector_property)
726 * Basic connector properties
728 tv_selector = drm_property_create_enum(dev, 0,
729 "select subconnector",
730 drm_tv_select_enum_list,
731 ARRAY_SIZE(drm_tv_select_enum_list));
732 dev->mode_config.tv_select_subconnector_property = tv_selector;
735 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
737 drm_tv_subconnector_enum_list,
738 ARRAY_SIZE(drm_tv_subconnector_enum_list));
739 dev->mode_config.tv_subconnector_property = tv_subconnector;
742 * Other, TV specific properties: margins & TV modes.
744 dev->mode_config.tv_left_margin_property =
745 drm_property_create_range(dev, 0, "left margin", 0, 100);
747 dev->mode_config.tv_right_margin_property =
748 drm_property_create_range(dev, 0, "right margin", 0, 100);
750 dev->mode_config.tv_top_margin_property =
751 drm_property_create_range(dev, 0, "top margin", 0, 100);
753 dev->mode_config.tv_bottom_margin_property =
754 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
756 dev->mode_config.tv_mode_property =
757 drm_property_create(dev, DRM_MODE_PROP_ENUM,
759 for (i = 0; i < num_modes; i++)
760 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
763 dev->mode_config.tv_brightness_property =
764 drm_property_create_range(dev, 0, "brightness", 0, 100);
766 dev->mode_config.tv_contrast_property =
767 drm_property_create_range(dev, 0, "contrast", 0, 100);
769 dev->mode_config.tv_flicker_reduction_property =
770 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
772 dev->mode_config.tv_overscan_property =
773 drm_property_create_range(dev, 0, "overscan", 0, 100);
775 dev->mode_config.tv_saturation_property =
776 drm_property_create_range(dev, 0, "saturation", 0, 100);
778 dev->mode_config.tv_hue_property =
779 drm_property_create_range(dev, 0, "hue", 0, 100);
783 EXPORT_SYMBOL(drm_mode_create_tv_properties);
786 * drm_mode_create_scaling_mode_property - create scaling mode property
789 * Called by a driver the first time it's needed, must be attached to desired
792 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
794 struct drm_property *scaling_mode;
796 if (dev->mode_config.scaling_mode_property)
800 drm_property_create_enum(dev, 0, "scaling mode",
801 drm_scaling_mode_enum_list,
802 ARRAY_SIZE(drm_scaling_mode_enum_list));
804 dev->mode_config.scaling_mode_property = scaling_mode;
808 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
811 * drm_mode_create_dithering_property - create dithering property
814 * Called by a driver the first time it's needed, must be attached to desired
817 int drm_mode_create_dithering_property(struct drm_device *dev)
819 struct drm_property *dithering_mode;
821 if (dev->mode_config.dithering_mode_property)
825 drm_property_create_enum(dev, 0, "dithering",
826 drm_dithering_mode_enum_list,
827 ARRAY_SIZE(drm_dithering_mode_enum_list));
828 dev->mode_config.dithering_mode_property = dithering_mode;
832 EXPORT_SYMBOL(drm_mode_create_dithering_property);
835 * drm_mode_create_dirty_property - create dirty property
838 * Called by a driver the first time it's needed, must be attached to desired
841 int drm_mode_create_dirty_info_property(struct drm_device *dev)
843 struct drm_property *dirty_info;
845 if (dev->mode_config.dirty_info_property)
849 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
851 drm_dirty_info_enum_list,
852 ARRAY_SIZE(drm_dirty_info_enum_list));
853 dev->mode_config.dirty_info_property = dirty_info;
857 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
860 * drm_mode_config_init - initialize DRM mode_configuration structure
864 * None, should happen single threaded at init time.
866 * Initialize @dev's mode_config structure, used for tracking the graphics
867 * configuration of @dev.
869 void drm_mode_config_init(struct drm_device *dev)
871 mutex_init(&dev->mode_config.mutex);
872 mutex_init(&dev->mode_config.idr_mutex);
873 INIT_LIST_HEAD(&dev->mode_config.fb_list);
874 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
875 INIT_LIST_HEAD(&dev->mode_config.connector_list);
876 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
877 INIT_LIST_HEAD(&dev->mode_config.property_list);
878 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
879 INIT_LIST_HEAD(&dev->mode_config.plane_list);
880 idr_init(&dev->mode_config.crtc_idr);
882 mutex_lock(&dev->mode_config.mutex);
883 drm_mode_create_standard_connector_properties(dev);
884 mutex_unlock(&dev->mode_config.mutex);
886 /* Just to be sure */
887 dev->mode_config.num_fb = 0;
888 dev->mode_config.num_connector = 0;
889 dev->mode_config.num_crtc = 0;
890 dev->mode_config.num_encoder = 0;
892 EXPORT_SYMBOL(drm_mode_config_init);
894 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
896 uint32_t total_objects = 0;
898 total_objects += dev->mode_config.num_crtc;
899 total_objects += dev->mode_config.num_connector;
900 total_objects += dev->mode_config.num_encoder;
902 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
906 group->num_crtcs = 0;
907 group->num_connectors = 0;
908 group->num_encoders = 0;
912 int drm_mode_group_init_legacy_group(struct drm_device *dev,
913 struct drm_mode_group *group)
915 struct drm_crtc *crtc;
916 struct drm_encoder *encoder;
917 struct drm_connector *connector;
920 if ((ret = drm_mode_group_init(dev, group)))
923 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
924 group->id_list[group->num_crtcs++] = crtc->base.id;
926 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
927 group->id_list[group->num_crtcs + group->num_encoders++] =
930 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
931 group->id_list[group->num_crtcs + group->num_encoders +
932 group->num_connectors++] = connector->base.id;
938 * drm_mode_config_cleanup - free up DRM mode_config info
942 * Caller must hold mode config lock.
944 * Free up all the connectors and CRTCs associated with this DRM device, then
945 * free up the framebuffers and associated buffer objects.
947 * FIXME: cleanup any dangling user buffer objects too
949 void drm_mode_config_cleanup(struct drm_device *dev)
951 struct drm_connector *connector, *ot;
952 struct drm_crtc *crtc, *ct;
953 struct drm_encoder *encoder, *enct;
954 struct drm_framebuffer *fb, *fbt;
955 struct drm_property *property, *pt;
956 struct drm_plane *plane, *plt;
958 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
960 encoder->funcs->destroy(encoder);
963 list_for_each_entry_safe(connector, ot,
964 &dev->mode_config.connector_list, head) {
965 connector->funcs->destroy(connector);
968 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
970 drm_property_destroy(dev, property);
973 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
974 fb->funcs->destroy(fb);
977 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
978 crtc->funcs->destroy(crtc);
981 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
983 plane->funcs->destroy(plane);
986 idr_remove_all(&dev->mode_config.crtc_idr);
987 idr_destroy(&dev->mode_config.crtc_idr);
989 EXPORT_SYMBOL(drm_mode_config_cleanup);
992 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
993 * @out: drm_mode_modeinfo struct to return to the user
994 * @in: drm_display_mode to use
999 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1002 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1003 struct drm_display_mode *in)
1005 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1006 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1007 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1008 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1009 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1010 "timing values too large for mode info\n");
1012 out->clock = in->clock;
1013 out->hdisplay = in->hdisplay;
1014 out->hsync_start = in->hsync_start;
1015 out->hsync_end = in->hsync_end;
1016 out->htotal = in->htotal;
1017 out->hskew = in->hskew;
1018 out->vdisplay = in->vdisplay;
1019 out->vsync_start = in->vsync_start;
1020 out->vsync_end = in->vsync_end;
1021 out->vtotal = in->vtotal;
1022 out->vscan = in->vscan;
1023 out->vrefresh = in->vrefresh;
1024 out->flags = in->flags;
1025 out->type = in->type;
1026 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1027 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1031 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1032 * @out: drm_display_mode to return to the user
1033 * @in: drm_mode_modeinfo to use
1038 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1041 void drm_crtc_convert_umode(struct drm_display_mode *out,
1042 struct drm_mode_modeinfo *in)
1044 out->clock = in->clock;
1045 out->hdisplay = in->hdisplay;
1046 out->hsync_start = in->hsync_start;
1047 out->hsync_end = in->hsync_end;
1048 out->htotal = in->htotal;
1049 out->hskew = in->hskew;
1050 out->vdisplay = in->vdisplay;
1051 out->vsync_start = in->vsync_start;
1052 out->vsync_end = in->vsync_end;
1053 out->vtotal = in->vtotal;
1054 out->vscan = in->vscan;
1055 out->vrefresh = in->vrefresh;
1056 out->flags = in->flags;
1057 out->type = in->type;
1058 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1059 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1063 * drm_mode_getresources - get graphics configuration
1064 * @inode: inode from the ioctl
1065 * @filp: file * from the ioctl
1066 * @cmd: cmd from ioctl
1067 * @arg: arg from ioctl
1070 * Takes mode config lock.
1072 * Construct a set of configuration description structures and return
1073 * them to the user, including CRTC, connector and framebuffer configuration.
1075 * Called by the user via ioctl.
1078 * Zero on success, errno on failure.
1080 int drm_mode_getresources(struct drm_device *dev, void *data,
1081 struct drm_file *file_priv)
1083 struct drm_mode_card_res *card_res = data;
1084 struct list_head *lh;
1085 struct drm_framebuffer *fb;
1086 struct drm_connector *connector;
1087 struct drm_crtc *crtc;
1088 struct drm_encoder *encoder;
1090 int connector_count = 0;
1093 int encoder_count = 0;
1095 uint32_t __user *fb_id;
1096 uint32_t __user *crtc_id;
1097 uint32_t __user *connector_id;
1098 uint32_t __user *encoder_id;
1099 struct drm_mode_group *mode_group;
1101 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1104 mutex_lock(&dev->mode_config.mutex);
1107 * For the non-control nodes we need to limit the list of resources
1108 * by IDs in the group list for this node
1110 list_for_each(lh, &file_priv->fbs)
1113 mode_group = &file_priv->master->minor->mode_group;
1114 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1116 list_for_each(lh, &dev->mode_config.crtc_list)
1119 list_for_each(lh, &dev->mode_config.connector_list)
1122 list_for_each(lh, &dev->mode_config.encoder_list)
1126 crtc_count = mode_group->num_crtcs;
1127 connector_count = mode_group->num_connectors;
1128 encoder_count = mode_group->num_encoders;
1131 card_res->max_height = dev->mode_config.max_height;
1132 card_res->min_height = dev->mode_config.min_height;
1133 card_res->max_width = dev->mode_config.max_width;
1134 card_res->min_width = dev->mode_config.min_width;
1136 /* handle this in 4 parts */
1138 if (card_res->count_fbs >= fb_count) {
1140 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1141 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1142 if (put_user(fb->base.id, fb_id + copied)) {
1149 card_res->count_fbs = fb_count;
1152 if (card_res->count_crtcs >= crtc_count) {
1154 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1155 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1156 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1158 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1159 if (put_user(crtc->base.id, crtc_id + copied)) {
1166 for (i = 0; i < mode_group->num_crtcs; i++) {
1167 if (put_user(mode_group->id_list[i],
1168 crtc_id + copied)) {
1176 card_res->count_crtcs = crtc_count;
1179 if (card_res->count_encoders >= encoder_count) {
1181 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1182 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1183 list_for_each_entry(encoder,
1184 &dev->mode_config.encoder_list,
1186 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1187 drm_get_encoder_name(encoder));
1188 if (put_user(encoder->base.id, encoder_id +
1196 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1197 if (put_user(mode_group->id_list[i],
1198 encoder_id + copied)) {
1207 card_res->count_encoders = encoder_count;
1210 if (card_res->count_connectors >= connector_count) {
1212 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1213 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1214 list_for_each_entry(connector,
1215 &dev->mode_config.connector_list,
1217 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1219 drm_get_connector_name(connector));
1220 if (put_user(connector->base.id,
1221 connector_id + copied)) {
1228 int start = mode_group->num_crtcs +
1229 mode_group->num_encoders;
1230 for (i = start; i < start + mode_group->num_connectors; i++) {
1231 if (put_user(mode_group->id_list[i],
1232 connector_id + copied)) {
1240 card_res->count_connectors = connector_count;
1242 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1243 card_res->count_connectors, card_res->count_encoders);
1246 mutex_unlock(&dev->mode_config.mutex);
1251 * drm_mode_getcrtc - get CRTC configuration
1252 * @inode: inode from the ioctl
1253 * @filp: file * from the ioctl
1254 * @cmd: cmd from ioctl
1255 * @arg: arg from ioctl
1258 * Takes mode config lock.
1260 * Construct a CRTC configuration structure to return to the user.
1262 * Called by the user via ioctl.
1265 * Zero on success, errno on failure.
1267 int drm_mode_getcrtc(struct drm_device *dev,
1268 void *data, struct drm_file *file_priv)
1270 struct drm_mode_crtc *crtc_resp = data;
1271 struct drm_crtc *crtc;
1272 struct drm_mode_object *obj;
1275 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1278 mutex_lock(&dev->mode_config.mutex);
1280 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1281 DRM_MODE_OBJECT_CRTC);
1286 crtc = obj_to_crtc(obj);
1288 crtc_resp->x = crtc->x;
1289 crtc_resp->y = crtc->y;
1290 crtc_resp->gamma_size = crtc->gamma_size;
1292 crtc_resp->fb_id = crtc->fb->base.id;
1294 crtc_resp->fb_id = 0;
1296 if (crtc->enabled) {
1298 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1299 crtc_resp->mode_valid = 1;
1302 crtc_resp->mode_valid = 0;
1306 mutex_unlock(&dev->mode_config.mutex);
1311 * drm_mode_getconnector - get connector configuration
1312 * @inode: inode from the ioctl
1313 * @filp: file * from the ioctl
1314 * @cmd: cmd from ioctl
1315 * @arg: arg from ioctl
1318 * Takes mode config lock.
1320 * Construct a connector configuration structure to return to the user.
1322 * Called by the user via ioctl.
1325 * Zero on success, errno on failure.
1327 int drm_mode_getconnector(struct drm_device *dev, void *data,
1328 struct drm_file *file_priv)
1330 struct drm_mode_get_connector *out_resp = data;
1331 struct drm_mode_object *obj;
1332 struct drm_connector *connector;
1333 struct drm_display_mode *mode;
1335 int props_count = 0;
1336 int encoders_count = 0;
1340 struct drm_mode_modeinfo u_mode;
1341 struct drm_mode_modeinfo __user *mode_ptr;
1342 uint32_t __user *prop_ptr;
1343 uint64_t __user *prop_values;
1344 uint32_t __user *encoder_ptr;
1346 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1349 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1351 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1353 mutex_lock(&dev->mode_config.mutex);
1355 obj = drm_mode_object_find(dev, out_resp->connector_id,
1356 DRM_MODE_OBJECT_CONNECTOR);
1361 connector = obj_to_connector(obj);
1363 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1364 if (connector->property_ids[i] != 0) {
1369 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1370 if (connector->encoder_ids[i] != 0) {
1375 if (out_resp->count_modes == 0) {
1376 connector->funcs->fill_modes(connector,
1377 dev->mode_config.max_width,
1378 dev->mode_config.max_height);
1381 /* delayed so we get modes regardless of pre-fill_modes state */
1382 list_for_each_entry(mode, &connector->modes, head)
1385 out_resp->connector_id = connector->base.id;
1386 out_resp->connector_type = connector->connector_type;
1387 out_resp->connector_type_id = connector->connector_type_id;
1388 out_resp->mm_width = connector->display_info.width_mm;
1389 out_resp->mm_height = connector->display_info.height_mm;
1390 out_resp->subpixel = connector->display_info.subpixel_order;
1391 out_resp->connection = connector->status;
1392 if (connector->encoder)
1393 out_resp->encoder_id = connector->encoder->base.id;
1395 out_resp->encoder_id = 0;
1398 * This ioctl is called twice, once to determine how much space is
1399 * needed, and the 2nd time to fill it.
1401 if ((out_resp->count_modes >= mode_count) && mode_count) {
1403 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1404 list_for_each_entry(mode, &connector->modes, head) {
1405 drm_crtc_convert_to_umode(&u_mode, mode);
1406 if (copy_to_user(mode_ptr + copied,
1407 &u_mode, sizeof(u_mode))) {
1414 out_resp->count_modes = mode_count;
1416 if ((out_resp->count_props >= props_count) && props_count) {
1418 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1419 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1420 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1421 if (connector->property_ids[i] != 0) {
1422 if (put_user(connector->property_ids[i],
1423 prop_ptr + copied)) {
1428 if (put_user(connector->property_values[i],
1429 prop_values + copied)) {
1437 out_resp->count_props = props_count;
1439 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1441 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1442 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1443 if (connector->encoder_ids[i] != 0) {
1444 if (put_user(connector->encoder_ids[i],
1445 encoder_ptr + copied)) {
1453 out_resp->count_encoders = encoders_count;
1456 mutex_unlock(&dev->mode_config.mutex);
1460 int drm_mode_getencoder(struct drm_device *dev, void *data,
1461 struct drm_file *file_priv)
1463 struct drm_mode_get_encoder *enc_resp = data;
1464 struct drm_mode_object *obj;
1465 struct drm_encoder *encoder;
1468 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1471 mutex_lock(&dev->mode_config.mutex);
1472 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1473 DRM_MODE_OBJECT_ENCODER);
1478 encoder = obj_to_encoder(obj);
1481 enc_resp->crtc_id = encoder->crtc->base.id;
1483 enc_resp->crtc_id = 0;
1484 enc_resp->encoder_type = encoder->encoder_type;
1485 enc_resp->encoder_id = encoder->base.id;
1486 enc_resp->possible_crtcs = encoder->possible_crtcs;
1487 enc_resp->possible_clones = encoder->possible_clones;
1490 mutex_unlock(&dev->mode_config.mutex);
1495 * drm_mode_getplane_res - get plane info
1498 * @file_priv: DRM file info
1501 * Takes mode config lock.
1503 * Return an plane count and set of IDs.
1505 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1506 struct drm_file *file_priv)
1508 struct drm_mode_get_plane_res *plane_resp = data;
1509 struct drm_mode_config *config;
1510 struct drm_plane *plane;
1511 uint32_t __user *plane_ptr;
1512 int copied = 0, ret = 0;
1514 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1517 mutex_lock(&dev->mode_config.mutex);
1518 config = &dev->mode_config;
1521 * This ioctl is called twice, once to determine how much space is
1522 * needed, and the 2nd time to fill it.
1524 if (config->num_plane &&
1525 (plane_resp->count_planes >= config->num_plane)) {
1526 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1528 list_for_each_entry(plane, &config->plane_list, head) {
1529 if (put_user(plane->base.id, plane_ptr + copied)) {
1536 plane_resp->count_planes = config->num_plane;
1539 mutex_unlock(&dev->mode_config.mutex);
1544 * drm_mode_getplane - get plane info
1547 * @file_priv: DRM file info
1550 * Takes mode config lock.
1552 * Return plane info, including formats supported, gamma size, any
1555 int drm_mode_getplane(struct drm_device *dev, void *data,
1556 struct drm_file *file_priv)
1558 struct drm_mode_get_plane *plane_resp = data;
1559 struct drm_mode_object *obj;
1560 struct drm_plane *plane;
1561 uint32_t __user *format_ptr;
1564 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1567 mutex_lock(&dev->mode_config.mutex);
1568 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1569 DRM_MODE_OBJECT_PLANE);
1574 plane = obj_to_plane(obj);
1577 plane_resp->crtc_id = plane->crtc->base.id;
1579 plane_resp->crtc_id = 0;
1582 plane_resp->fb_id = plane->fb->base.id;
1584 plane_resp->fb_id = 0;
1586 plane_resp->plane_id = plane->base.id;
1587 plane_resp->possible_crtcs = plane->possible_crtcs;
1588 plane_resp->gamma_size = plane->gamma_size;
1591 * This ioctl is called twice, once to determine how much space is
1592 * needed, and the 2nd time to fill it.
1594 if (plane->format_count &&
1595 (plane_resp->count_format_types >= plane->format_count)) {
1596 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1597 if (copy_to_user(format_ptr,
1598 plane->format_types,
1599 sizeof(uint32_t) * plane->format_count)) {
1604 plane_resp->count_format_types = plane->format_count;
1607 mutex_unlock(&dev->mode_config.mutex);
1612 * drm_mode_setplane - set up or tear down an plane
1614 * @data: ioctl data*
1615 * @file_prive: DRM file info
1618 * Takes mode config lock.
1620 * Set plane info, including placement, fb, scaling, and other factors.
1621 * Or pass a NULL fb to disable.
1623 int drm_mode_setplane(struct drm_device *dev, void *data,
1624 struct drm_file *file_priv)
1626 struct drm_mode_set_plane *plane_req = data;
1627 struct drm_mode_object *obj;
1628 struct drm_plane *plane;
1629 struct drm_crtc *crtc;
1630 struct drm_framebuffer *fb;
1632 unsigned int fb_width, fb_height;
1635 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1638 mutex_lock(&dev->mode_config.mutex);
1641 * First, find the plane, crtc, and fb objects. If not available,
1642 * we don't bother to call the driver.
1644 obj = drm_mode_object_find(dev, plane_req->plane_id,
1645 DRM_MODE_OBJECT_PLANE);
1647 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1648 plane_req->plane_id);
1652 plane = obj_to_plane(obj);
1654 /* No fb means shut it down */
1655 if (!plane_req->fb_id) {
1656 plane->funcs->disable_plane(plane);
1662 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1663 DRM_MODE_OBJECT_CRTC);
1665 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1666 plane_req->crtc_id);
1670 crtc = obj_to_crtc(obj);
1672 obj = drm_mode_object_find(dev, plane_req->fb_id,
1673 DRM_MODE_OBJECT_FB);
1675 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1680 fb = obj_to_fb(obj);
1682 /* Check whether this plane supports the fb pixel format. */
1683 for (i = 0; i < plane->format_count; i++)
1684 if (fb->pixel_format == plane->format_types[i])
1686 if (i == plane->format_count) {
1687 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1692 fb_width = fb->width << 16;
1693 fb_height = fb->height << 16;
1695 /* Make sure source coordinates are inside the fb. */
1696 if (plane_req->src_w > fb_width ||
1697 plane_req->src_x > fb_width - plane_req->src_w ||
1698 plane_req->src_h > fb_height ||
1699 plane_req->src_y > fb_height - plane_req->src_h) {
1700 DRM_DEBUG_KMS("Invalid source coordinates "
1701 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1702 plane_req->src_w >> 16,
1703 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1704 plane_req->src_h >> 16,
1705 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1706 plane_req->src_x >> 16,
1707 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1708 plane_req->src_y >> 16,
1709 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1714 /* Give drivers some help against integer overflows */
1715 if (plane_req->crtc_w > INT_MAX ||
1716 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1717 plane_req->crtc_h > INT_MAX ||
1718 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1719 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1720 plane_req->crtc_w, plane_req->crtc_h,
1721 plane_req->crtc_x, plane_req->crtc_y);
1726 ret = plane->funcs->update_plane(plane, crtc, fb,
1727 plane_req->crtc_x, plane_req->crtc_y,
1728 plane_req->crtc_w, plane_req->crtc_h,
1729 plane_req->src_x, plane_req->src_y,
1730 plane_req->src_w, plane_req->src_h);
1737 mutex_unlock(&dev->mode_config.mutex);
1743 * drm_mode_setcrtc - set CRTC configuration
1744 * @inode: inode from the ioctl
1745 * @filp: file * from the ioctl
1746 * @cmd: cmd from ioctl
1747 * @arg: arg from ioctl
1750 * Takes mode config lock.
1752 * Build a new CRTC configuration based on user request.
1754 * Called by the user via ioctl.
1757 * Zero on success, errno on failure.
1759 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1760 struct drm_file *file_priv)
1762 struct drm_mode_config *config = &dev->mode_config;
1763 struct drm_mode_crtc *crtc_req = data;
1764 struct drm_mode_object *obj;
1765 struct drm_crtc *crtc;
1766 struct drm_connector **connector_set = NULL, *connector;
1767 struct drm_framebuffer *fb = NULL;
1768 struct drm_display_mode *mode = NULL;
1769 struct drm_mode_set set;
1770 uint32_t __user *set_connectors_ptr;
1774 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1777 /* For some reason crtc x/y offsets are signed internally. */
1778 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1781 mutex_lock(&dev->mode_config.mutex);
1782 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1783 DRM_MODE_OBJECT_CRTC);
1785 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1789 crtc = obj_to_crtc(obj);
1790 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1792 if (crtc_req->mode_valid) {
1793 /* If we have a mode we need a framebuffer. */
1794 /* If we pass -1, set the mode with the currently bound fb */
1795 if (crtc_req->fb_id == -1) {
1797 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1803 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1804 DRM_MODE_OBJECT_FB);
1806 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1811 fb = obj_to_fb(obj);
1814 mode = drm_mode_create(dev);
1815 drm_crtc_convert_umode(mode, &crtc_req->mode);
1816 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1819 if (crtc_req->count_connectors == 0 && mode) {
1820 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1825 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1826 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1827 crtc_req->count_connectors);
1832 if (crtc_req->count_connectors > 0) {
1835 /* Avoid unbounded kernel memory allocation */
1836 if (crtc_req->count_connectors > config->num_connector) {
1841 connector_set = kmalloc(crtc_req->count_connectors *
1842 sizeof(struct drm_connector *),
1844 if (!connector_set) {
1849 for (i = 0; i < crtc_req->count_connectors; i++) {
1850 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
1851 if (get_user(out_id, &set_connectors_ptr[i])) {
1856 obj = drm_mode_object_find(dev, out_id,
1857 DRM_MODE_OBJECT_CONNECTOR);
1859 DRM_DEBUG_KMS("Connector id %d unknown\n",
1864 connector = obj_to_connector(obj);
1865 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1867 drm_get_connector_name(connector));
1869 connector_set[i] = connector;
1874 set.x = crtc_req->x;
1875 set.y = crtc_req->y;
1877 set.connectors = connector_set;
1878 set.num_connectors = crtc_req->count_connectors;
1880 ret = crtc->funcs->set_config(&set);
1883 kfree(connector_set);
1884 mutex_unlock(&dev->mode_config.mutex);
1888 int drm_mode_cursor_ioctl(struct drm_device *dev,
1889 void *data, struct drm_file *file_priv)
1891 struct drm_mode_cursor *req = data;
1892 struct drm_mode_object *obj;
1893 struct drm_crtc *crtc;
1896 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1902 mutex_lock(&dev->mode_config.mutex);
1903 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1905 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1909 crtc = obj_to_crtc(obj);
1911 if (req->flags & DRM_MODE_CURSOR_BO) {
1912 if (!crtc->funcs->cursor_set) {
1916 /* Turns off the cursor if handle is 0 */
1917 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1918 req->width, req->height);
1921 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1922 if (crtc->funcs->cursor_move) {
1923 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1930 mutex_unlock(&dev->mode_config.mutex);
1934 /* Original addfb only supported RGB formats, so figure out which one */
1935 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
1941 fmt = DRM_FORMAT_RGB332;
1945 fmt = DRM_FORMAT_XRGB1555;
1947 fmt = DRM_FORMAT_RGB565;
1950 fmt = DRM_FORMAT_RGB888;
1954 fmt = DRM_FORMAT_XRGB8888;
1955 else if (depth == 30)
1956 fmt = DRM_FORMAT_XRGB2101010;
1958 fmt = DRM_FORMAT_ARGB8888;
1961 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
1962 fmt = DRM_FORMAT_XRGB8888;
1968 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
1971 * drm_mode_addfb - add an FB to the graphics configuration
1972 * @inode: inode from the ioctl
1973 * @filp: file * from the ioctl
1974 * @cmd: cmd from ioctl
1975 * @arg: arg from ioctl
1978 * Takes mode config lock.
1980 * Add a new FB to the specified CRTC, given a user request.
1982 * Called by the user via ioctl.
1985 * Zero on success, errno on failure.
1987 int drm_mode_addfb(struct drm_device *dev,
1988 void *data, struct drm_file *file_priv)
1990 struct drm_mode_fb_cmd *or = data;
1991 struct drm_mode_fb_cmd2 r = {};
1992 struct drm_mode_config *config = &dev->mode_config;
1993 struct drm_framebuffer *fb;
1996 /* Use new struct with format internally */
1997 r.fb_id = or->fb_id;
1998 r.width = or->width;
1999 r.height = or->height;
2000 r.pitches[0] = or->pitch;
2001 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2002 r.handles[0] = or->handle;
2004 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2007 if ((config->min_width > r.width) || (r.width > config->max_width))
2010 if ((config->min_height > r.height) || (r.height > config->max_height))
2013 mutex_lock(&dev->mode_config.mutex);
2015 /* TODO check buffer is sufficiently large */
2016 /* TODO setup destructor callback */
2018 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2020 DRM_ERROR("could not create framebuffer\n");
2025 or->fb_id = fb->base.id;
2026 list_add(&fb->filp_head, &file_priv->fbs);
2027 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2030 mutex_unlock(&dev->mode_config.mutex);
2034 static int format_check(struct drm_mode_fb_cmd2 *r)
2036 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2040 case DRM_FORMAT_RGB332:
2041 case DRM_FORMAT_BGR233:
2042 case DRM_FORMAT_XRGB4444:
2043 case DRM_FORMAT_XBGR4444:
2044 case DRM_FORMAT_RGBX4444:
2045 case DRM_FORMAT_BGRX4444:
2046 case DRM_FORMAT_ARGB4444:
2047 case DRM_FORMAT_ABGR4444:
2048 case DRM_FORMAT_RGBA4444:
2049 case DRM_FORMAT_BGRA4444:
2050 case DRM_FORMAT_XRGB1555:
2051 case DRM_FORMAT_XBGR1555:
2052 case DRM_FORMAT_RGBX5551:
2053 case DRM_FORMAT_BGRX5551:
2054 case DRM_FORMAT_ARGB1555:
2055 case DRM_FORMAT_ABGR1555:
2056 case DRM_FORMAT_RGBA5551:
2057 case DRM_FORMAT_BGRA5551:
2058 case DRM_FORMAT_RGB565:
2059 case DRM_FORMAT_BGR565:
2060 case DRM_FORMAT_RGB888:
2061 case DRM_FORMAT_BGR888:
2062 case DRM_FORMAT_XRGB8888:
2063 case DRM_FORMAT_XBGR8888:
2064 case DRM_FORMAT_RGBX8888:
2065 case DRM_FORMAT_BGRX8888:
2066 case DRM_FORMAT_ARGB8888:
2067 case DRM_FORMAT_ABGR8888:
2068 case DRM_FORMAT_RGBA8888:
2069 case DRM_FORMAT_BGRA8888:
2070 case DRM_FORMAT_XRGB2101010:
2071 case DRM_FORMAT_XBGR2101010:
2072 case DRM_FORMAT_RGBX1010102:
2073 case DRM_FORMAT_BGRX1010102:
2074 case DRM_FORMAT_ARGB2101010:
2075 case DRM_FORMAT_ABGR2101010:
2076 case DRM_FORMAT_RGBA1010102:
2077 case DRM_FORMAT_BGRA1010102:
2078 case DRM_FORMAT_YUYV:
2079 case DRM_FORMAT_YVYU:
2080 case DRM_FORMAT_UYVY:
2081 case DRM_FORMAT_VYUY:
2082 case DRM_FORMAT_AYUV:
2083 case DRM_FORMAT_NV12:
2084 case DRM_FORMAT_NV21:
2085 case DRM_FORMAT_NV16:
2086 case DRM_FORMAT_NV61:
2087 case DRM_FORMAT_YUV410:
2088 case DRM_FORMAT_YVU410:
2089 case DRM_FORMAT_YUV411:
2090 case DRM_FORMAT_YVU411:
2091 case DRM_FORMAT_YUV420:
2092 case DRM_FORMAT_YVU420:
2093 case DRM_FORMAT_YUV422:
2094 case DRM_FORMAT_YVU422:
2095 case DRM_FORMAT_YUV444:
2096 case DRM_FORMAT_YVU444:
2104 * drm_mode_addfb2 - add an FB to the graphics configuration
2105 * @inode: inode from the ioctl
2106 * @filp: file * from the ioctl
2107 * @cmd: cmd from ioctl
2108 * @arg: arg from ioctl
2111 * Takes mode config lock.
2113 * Add a new FB to the specified CRTC, given a user request with format.
2115 * Called by the user via ioctl.
2118 * Zero on success, errno on failure.
2120 int drm_mode_addfb2(struct drm_device *dev,
2121 void *data, struct drm_file *file_priv)
2123 struct drm_mode_fb_cmd2 *r = data;
2124 struct drm_mode_config *config = &dev->mode_config;
2125 struct drm_framebuffer *fb;
2128 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2131 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2132 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2133 r->width, config->min_width, config->max_width);
2136 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2137 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2138 r->height, config->min_height, config->max_height);
2142 ret = format_check(r);
2144 DRM_ERROR("bad framebuffer format 0x%08x\n", r->pixel_format);
2148 mutex_lock(&dev->mode_config.mutex);
2150 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2152 DRM_ERROR("could not create framebuffer\n");
2157 r->fb_id = fb->base.id;
2158 list_add(&fb->filp_head, &file_priv->fbs);
2159 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2162 mutex_unlock(&dev->mode_config.mutex);
2167 * drm_mode_rmfb - remove an FB from the configuration
2168 * @inode: inode from the ioctl
2169 * @filp: file * from the ioctl
2170 * @cmd: cmd from ioctl
2171 * @arg: arg from ioctl
2174 * Takes mode config lock.
2176 * Remove the FB specified by the user.
2178 * Called by the user via ioctl.
2181 * Zero on success, errno on failure.
2183 int drm_mode_rmfb(struct drm_device *dev,
2184 void *data, struct drm_file *file_priv)
2186 struct drm_mode_object *obj;
2187 struct drm_framebuffer *fb = NULL;
2188 struct drm_framebuffer *fbl = NULL;
2189 uint32_t *id = data;
2193 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2196 mutex_lock(&dev->mode_config.mutex);
2197 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
2198 /* TODO check that we really get a framebuffer back. */
2203 fb = obj_to_fb(obj);
2205 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2214 /* TODO release all crtc connected to the framebuffer */
2215 /* TODO unhock the destructor from the buffer object */
2217 list_del(&fb->filp_head);
2218 fb->funcs->destroy(fb);
2221 mutex_unlock(&dev->mode_config.mutex);
2226 * drm_mode_getfb - get FB info
2227 * @inode: inode from the ioctl
2228 * @filp: file * from the ioctl
2229 * @cmd: cmd from ioctl
2230 * @arg: arg from ioctl
2233 * Takes mode config lock.
2235 * Lookup the FB given its ID and return info about it.
2237 * Called by the user via ioctl.
2240 * Zero on success, errno on failure.
2242 int drm_mode_getfb(struct drm_device *dev,
2243 void *data, struct drm_file *file_priv)
2245 struct drm_mode_fb_cmd *r = data;
2246 struct drm_mode_object *obj;
2247 struct drm_framebuffer *fb;
2250 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2253 mutex_lock(&dev->mode_config.mutex);
2254 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2259 fb = obj_to_fb(obj);
2261 r->height = fb->height;
2262 r->width = fb->width;
2263 r->depth = fb->depth;
2264 r->bpp = fb->bits_per_pixel;
2265 r->pitch = fb->pitches[0];
2266 fb->funcs->create_handle(fb, file_priv, &r->handle);
2269 mutex_unlock(&dev->mode_config.mutex);
2273 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2274 void *data, struct drm_file *file_priv)
2276 struct drm_clip_rect __user *clips_ptr;
2277 struct drm_clip_rect *clips = NULL;
2278 struct drm_mode_fb_dirty_cmd *r = data;
2279 struct drm_mode_object *obj;
2280 struct drm_framebuffer *fb;
2285 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2288 mutex_lock(&dev->mode_config.mutex);
2289 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2294 fb = obj_to_fb(obj);
2296 num_clips = r->num_clips;
2297 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2299 if (!num_clips != !clips_ptr) {
2304 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2306 /* If userspace annotates copy, clips must come in pairs */
2307 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2312 if (num_clips && clips_ptr) {
2313 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2317 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2323 ret = copy_from_user(clips, clips_ptr,
2324 num_clips * sizeof(*clips));
2331 if (fb->funcs->dirty) {
2332 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2342 mutex_unlock(&dev->mode_config.mutex);
2348 * drm_fb_release - remove and free the FBs on this file
2349 * @filp: file * from the ioctl
2352 * Takes mode config lock.
2354 * Destroy all the FBs associated with @filp.
2356 * Called by the user via ioctl.
2359 * Zero on success, errno on failure.
2361 void drm_fb_release(struct drm_file *priv)
2363 struct drm_device *dev = priv->minor->dev;
2364 struct drm_framebuffer *fb, *tfb;
2366 mutex_lock(&dev->mode_config.mutex);
2367 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2368 list_del(&fb->filp_head);
2369 fb->funcs->destroy(fb);
2371 mutex_unlock(&dev->mode_config.mutex);
2375 * drm_mode_attachmode - add a mode to the user mode list
2377 * @connector: connector to add the mode to
2378 * @mode: mode to add
2380 * Add @mode to @connector's user mode list.
2382 static int drm_mode_attachmode(struct drm_device *dev,
2383 struct drm_connector *connector,
2384 struct drm_display_mode *mode)
2388 list_add_tail(&mode->head, &connector->user_modes);
2392 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2393 struct drm_display_mode *mode)
2395 struct drm_connector *connector;
2397 struct drm_display_mode *dup_mode;
2399 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2400 if (!connector->encoder)
2402 if (connector->encoder->crtc == crtc) {
2404 dup_mode = drm_mode_duplicate(dev, mode);
2407 ret = drm_mode_attachmode(dev, connector, dup_mode);
2415 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2417 static int drm_mode_detachmode(struct drm_device *dev,
2418 struct drm_connector *connector,
2419 struct drm_display_mode *mode)
2423 struct drm_display_mode *match_mode, *t;
2425 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2426 if (drm_mode_equal(match_mode, mode)) {
2427 list_del(&match_mode->head);
2428 drm_mode_destroy(dev, match_mode);
2440 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2442 struct drm_connector *connector;
2444 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2445 drm_mode_detachmode(dev, connector, mode);
2449 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2452 * drm_fb_attachmode - Attach a user mode to an connector
2453 * @inode: inode from the ioctl
2454 * @filp: file * from the ioctl
2455 * @cmd: cmd from ioctl
2456 * @arg: arg from ioctl
2458 * This attaches a user specified mode to an connector.
2459 * Called by the user via ioctl.
2462 * Zero on success, errno on failure.
2464 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2465 void *data, struct drm_file *file_priv)
2467 struct drm_mode_mode_cmd *mode_cmd = data;
2468 struct drm_connector *connector;
2469 struct drm_display_mode *mode;
2470 struct drm_mode_object *obj;
2471 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2474 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2477 mutex_lock(&dev->mode_config.mutex);
2479 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2484 connector = obj_to_connector(obj);
2486 mode = drm_mode_create(dev);
2492 drm_crtc_convert_umode(mode, umode);
2494 ret = drm_mode_attachmode(dev, connector, mode);
2496 mutex_unlock(&dev->mode_config.mutex);
2502 * drm_fb_detachmode - Detach a user specified mode from an connector
2503 * @inode: inode from the ioctl
2504 * @filp: file * from the ioctl
2505 * @cmd: cmd from ioctl
2506 * @arg: arg from ioctl
2508 * Called by the user via ioctl.
2511 * Zero on success, errno on failure.
2513 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2514 void *data, struct drm_file *file_priv)
2516 struct drm_mode_object *obj;
2517 struct drm_mode_mode_cmd *mode_cmd = data;
2518 struct drm_connector *connector;
2519 struct drm_display_mode mode;
2520 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2523 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2526 mutex_lock(&dev->mode_config.mutex);
2528 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2533 connector = obj_to_connector(obj);
2535 drm_crtc_convert_umode(&mode, umode);
2536 ret = drm_mode_detachmode(dev, connector, &mode);
2538 mutex_unlock(&dev->mode_config.mutex);
2542 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2543 const char *name, int num_values)
2545 struct drm_property *property = NULL;
2547 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2552 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2553 if (!property->values)
2557 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2558 property->flags = flags;
2559 property->num_values = num_values;
2560 INIT_LIST_HEAD(&property->enum_blob_list);
2563 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2564 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2567 list_add_tail(&property->head, &dev->mode_config.property_list);
2573 EXPORT_SYMBOL(drm_property_create);
2575 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2577 const struct drm_prop_enum_list *props,
2580 struct drm_property *property;
2583 flags |= DRM_MODE_PROP_ENUM;
2585 property = drm_property_create(dev, flags, name, num_values);
2589 for (i = 0; i < num_values; i++) {
2590 ret = drm_property_add_enum(property, i,
2594 drm_property_destroy(dev, property);
2601 EXPORT_SYMBOL(drm_property_create_enum);
2603 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2605 uint64_t min, uint64_t max)
2607 struct drm_property *property;
2609 flags |= DRM_MODE_PROP_RANGE;
2611 property = drm_property_create(dev, flags, name, 2);
2615 property->values[0] = min;
2616 property->values[1] = max;
2620 EXPORT_SYMBOL(drm_property_create_range);
2622 int drm_property_add_enum(struct drm_property *property, int index,
2623 uint64_t value, const char *name)
2625 struct drm_property_enum *prop_enum;
2627 if (!(property->flags & DRM_MODE_PROP_ENUM))
2630 if (!list_empty(&property->enum_blob_list)) {
2631 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2632 if (prop_enum->value == value) {
2633 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2634 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2640 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2644 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2645 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2646 prop_enum->value = value;
2648 property->values[index] = value;
2649 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2652 EXPORT_SYMBOL(drm_property_add_enum);
2654 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2656 struct drm_property_enum *prop_enum, *pt;
2658 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2659 list_del(&prop_enum->head);
2663 if (property->num_values)
2664 kfree(property->values);
2665 drm_mode_object_put(dev, &property->base);
2666 list_del(&property->head);
2669 EXPORT_SYMBOL(drm_property_destroy);
2671 int drm_connector_attach_property(struct drm_connector *connector,
2672 struct drm_property *property, uint64_t init_val)
2676 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2677 if (connector->property_ids[i] == 0) {
2678 connector->property_ids[i] = property->base.id;
2679 connector->property_values[i] = init_val;
2684 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2688 EXPORT_SYMBOL(drm_connector_attach_property);
2690 int drm_connector_property_set_value(struct drm_connector *connector,
2691 struct drm_property *property, uint64_t value)
2695 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2696 if (connector->property_ids[i] == property->base.id) {
2697 connector->property_values[i] = value;
2702 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2706 EXPORT_SYMBOL(drm_connector_property_set_value);
2708 int drm_connector_property_get_value(struct drm_connector *connector,
2709 struct drm_property *property, uint64_t *val)
2713 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2714 if (connector->property_ids[i] == property->base.id) {
2715 *val = connector->property_values[i];
2720 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2724 EXPORT_SYMBOL(drm_connector_property_get_value);
2726 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2727 void *data, struct drm_file *file_priv)
2729 struct drm_mode_object *obj;
2730 struct drm_mode_get_property *out_resp = data;
2731 struct drm_property *property;
2734 int value_count = 0;
2737 struct drm_property_enum *prop_enum;
2738 struct drm_mode_property_enum __user *enum_ptr;
2739 struct drm_property_blob *prop_blob;
2740 uint32_t __user *blob_id_ptr;
2741 uint64_t __user *values_ptr;
2742 uint32_t __user *blob_length_ptr;
2744 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2747 mutex_lock(&dev->mode_config.mutex);
2748 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2753 property = obj_to_property(obj);
2755 if (property->flags & DRM_MODE_PROP_ENUM) {
2756 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2758 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2759 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2763 value_count = property->num_values;
2765 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2766 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2767 out_resp->flags = property->flags;
2769 if ((out_resp->count_values >= value_count) && value_count) {
2770 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
2771 for (i = 0; i < value_count; i++) {
2772 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2778 out_resp->count_values = value_count;
2780 if (property->flags & DRM_MODE_PROP_ENUM) {
2781 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2783 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
2784 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2786 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2791 if (copy_to_user(&enum_ptr[copied].name,
2792 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2799 out_resp->count_enum_blobs = enum_count;
2802 if (property->flags & DRM_MODE_PROP_BLOB) {
2803 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2805 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2806 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
2808 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2809 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2814 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2822 out_resp->count_enum_blobs = blob_count;
2825 mutex_unlock(&dev->mode_config.mutex);
2829 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2832 struct drm_property_blob *blob;
2834 if (!length || !data)
2837 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2841 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2842 blob->length = length;
2844 memcpy(blob->data, data, length);
2846 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2848 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2852 static void drm_property_destroy_blob(struct drm_device *dev,
2853 struct drm_property_blob *blob)
2855 drm_mode_object_put(dev, &blob->base);
2856 list_del(&blob->head);
2860 int drm_mode_getblob_ioctl(struct drm_device *dev,
2861 void *data, struct drm_file *file_priv)
2863 struct drm_mode_object *obj;
2864 struct drm_mode_get_blob *out_resp = data;
2865 struct drm_property_blob *blob;
2867 void __user *blob_ptr;
2869 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2872 mutex_lock(&dev->mode_config.mutex);
2873 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2878 blob = obj_to_blob(obj);
2880 if (out_resp->length == blob->length) {
2881 blob_ptr = (void __user *)(unsigned long)out_resp->data;
2882 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2887 out_resp->length = blob->length;
2890 mutex_unlock(&dev->mode_config.mutex);
2894 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2897 struct drm_device *dev = connector->dev;
2900 if (connector->edid_blob_ptr)
2901 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2903 /* Delete edid, when there is none. */
2905 connector->edid_blob_ptr = NULL;
2906 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2910 size = EDID_LENGTH * (1 + edid->extensions);
2911 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2914 ret = drm_connector_property_set_value(connector,
2915 dev->mode_config.edid_property,
2916 connector->edid_blob_ptr->base.id);
2920 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2922 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2923 void *data, struct drm_file *file_priv)
2925 struct drm_mode_connector_set_property *out_resp = data;
2926 struct drm_mode_object *obj;
2927 struct drm_property *property;
2928 struct drm_connector *connector;
2932 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2935 mutex_lock(&dev->mode_config.mutex);
2937 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2941 connector = obj_to_connector(obj);
2943 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2944 if (connector->property_ids[i] == out_resp->prop_id)
2948 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2952 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2956 property = obj_to_property(obj);
2958 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2961 if (property->flags & DRM_MODE_PROP_RANGE) {
2962 if (out_resp->value < property->values[0])
2965 if (out_resp->value > property->values[1])
2969 for (i = 0; i < property->num_values; i++) {
2970 if (property->values[i] == out_resp->value) {
2980 /* Do DPMS ourselves */
2981 if (property == connector->dev->mode_config.dpms_property) {
2982 if (connector->funcs->dpms)
2983 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2985 } else if (connector->funcs->set_property)
2986 ret = connector->funcs->set_property(connector, property, out_resp->value);
2988 /* store the property value if successful */
2990 drm_connector_property_set_value(connector, property, out_resp->value);
2992 mutex_unlock(&dev->mode_config.mutex);
2996 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2997 struct drm_encoder *encoder)
3001 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3002 if (connector->encoder_ids[i] == 0) {
3003 connector->encoder_ids[i] = encoder->base.id;
3009 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3011 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3012 struct drm_encoder *encoder)
3015 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3016 if (connector->encoder_ids[i] == encoder->base.id) {
3017 connector->encoder_ids[i] = 0;
3018 if (connector->encoder == encoder)
3019 connector->encoder = NULL;
3024 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3026 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3029 crtc->gamma_size = gamma_size;
3031 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3032 if (!crtc->gamma_store) {
3033 crtc->gamma_size = 0;
3039 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3041 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3042 void *data, struct drm_file *file_priv)
3044 struct drm_mode_crtc_lut *crtc_lut = data;
3045 struct drm_mode_object *obj;
3046 struct drm_crtc *crtc;
3047 void *r_base, *g_base, *b_base;
3051 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3054 mutex_lock(&dev->mode_config.mutex);
3055 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3060 crtc = obj_to_crtc(obj);
3062 /* memcpy into gamma store */
3063 if (crtc_lut->gamma_size != crtc->gamma_size) {
3068 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3069 r_base = crtc->gamma_store;
3070 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3075 g_base = r_base + size;
3076 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3081 b_base = g_base + size;
3082 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3087 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3090 mutex_unlock(&dev->mode_config.mutex);
3095 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3096 void *data, struct drm_file *file_priv)
3098 struct drm_mode_crtc_lut *crtc_lut = data;
3099 struct drm_mode_object *obj;
3100 struct drm_crtc *crtc;
3101 void *r_base, *g_base, *b_base;
3105 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3108 mutex_lock(&dev->mode_config.mutex);
3109 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3114 crtc = obj_to_crtc(obj);
3116 /* memcpy into gamma store */
3117 if (crtc_lut->gamma_size != crtc->gamma_size) {
3122 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3123 r_base = crtc->gamma_store;
3124 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3129 g_base = r_base + size;
3130 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3135 b_base = g_base + size;
3136 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3141 mutex_unlock(&dev->mode_config.mutex);
3145 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3146 void *data, struct drm_file *file_priv)
3148 struct drm_mode_crtc_page_flip *page_flip = data;
3149 struct drm_mode_object *obj;
3150 struct drm_crtc *crtc;
3151 struct drm_framebuffer *fb;
3152 struct drm_pending_vblank_event *e = NULL;
3153 unsigned long flags;
3156 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3157 page_flip->reserved != 0)
3160 mutex_lock(&dev->mode_config.mutex);
3161 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3164 crtc = obj_to_crtc(obj);
3166 if (crtc->fb == NULL) {
3167 /* The framebuffer is currently unbound, presumably
3168 * due to a hotplug event, that userspace has not
3175 if (crtc->funcs->page_flip == NULL)
3178 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3181 fb = obj_to_fb(obj);
3183 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3185 spin_lock_irqsave(&dev->event_lock, flags);
3186 if (file_priv->event_space < sizeof e->event) {
3187 spin_unlock_irqrestore(&dev->event_lock, flags);
3190 file_priv->event_space -= sizeof e->event;
3191 spin_unlock_irqrestore(&dev->event_lock, flags);
3193 e = kzalloc(sizeof *e, GFP_KERNEL);
3195 spin_lock_irqsave(&dev->event_lock, flags);
3196 file_priv->event_space += sizeof e->event;
3197 spin_unlock_irqrestore(&dev->event_lock, flags);
3201 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3202 e->event.base.length = sizeof e->event;
3203 e->event.user_data = page_flip->user_data;
3204 e->base.event = &e->event.base;
3205 e->base.file_priv = file_priv;
3207 (void (*) (struct drm_pending_event *)) kfree;
3210 ret = crtc->funcs->page_flip(crtc, fb, e);
3212 spin_lock_irqsave(&dev->event_lock, flags);
3213 file_priv->event_space += sizeof e->event;
3214 spin_unlock_irqrestore(&dev->event_lock, flags);
3219 mutex_unlock(&dev->mode_config.mutex);
3223 void drm_mode_config_reset(struct drm_device *dev)
3225 struct drm_crtc *crtc;
3226 struct drm_encoder *encoder;
3227 struct drm_connector *connector;
3229 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3230 if (crtc->funcs->reset)
3231 crtc->funcs->reset(crtc);
3233 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3234 if (encoder->funcs->reset)
3235 encoder->funcs->reset(encoder);
3237 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3238 if (connector->funcs->reset)
3239 connector->funcs->reset(connector);
3241 EXPORT_SYMBOL(drm_mode_config_reset);
3243 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3244 void *data, struct drm_file *file_priv)
3246 struct drm_mode_create_dumb *args = data;
3248 if (!dev->driver->dumb_create)
3250 return dev->driver->dumb_create(file_priv, dev, args);
3253 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3254 void *data, struct drm_file *file_priv)
3256 struct drm_mode_map_dumb *args = data;
3258 /* call driver ioctl to get mmap offset */
3259 if (!dev->driver->dumb_map_offset)
3262 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3265 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3266 void *data, struct drm_file *file_priv)
3268 struct drm_mode_destroy_dumb *args = data;
3270 if (!dev->driver->dumb_destroy)
3273 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3277 * Just need to support RGB formats here for compat with code that doesn't
3278 * use pixel formats directly yet.
3280 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3284 case DRM_FORMAT_RGB332:
3285 case DRM_FORMAT_BGR233:
3289 case DRM_FORMAT_XRGB1555:
3290 case DRM_FORMAT_XBGR1555:
3291 case DRM_FORMAT_RGBX5551:
3292 case DRM_FORMAT_BGRX5551:
3293 case DRM_FORMAT_ARGB1555:
3294 case DRM_FORMAT_ABGR1555:
3295 case DRM_FORMAT_RGBA5551:
3296 case DRM_FORMAT_BGRA5551:
3300 case DRM_FORMAT_RGB565:
3301 case DRM_FORMAT_BGR565:
3305 case DRM_FORMAT_RGB888:
3306 case DRM_FORMAT_BGR888:
3310 case DRM_FORMAT_XRGB8888:
3311 case DRM_FORMAT_XBGR8888:
3312 case DRM_FORMAT_RGBX8888:
3313 case DRM_FORMAT_BGRX8888:
3317 case DRM_FORMAT_XRGB2101010:
3318 case DRM_FORMAT_XBGR2101010:
3319 case DRM_FORMAT_RGBX1010102:
3320 case DRM_FORMAT_BGRX1010102:
3321 case DRM_FORMAT_ARGB2101010:
3322 case DRM_FORMAT_ABGR2101010:
3323 case DRM_FORMAT_RGBA1010102:
3324 case DRM_FORMAT_BGRA1010102:
3328 case DRM_FORMAT_ARGB8888:
3329 case DRM_FORMAT_ABGR8888:
3330 case DRM_FORMAT_RGBA8888:
3331 case DRM_FORMAT_BGRA8888:
3336 DRM_DEBUG_KMS("unsupported pixel format\n");
3342 EXPORT_SYMBOL(drm_fb_get_bpp_depth);