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>
37 struct drm_prop_enum_list {
42 /* Avoid boilerplate. I'm tired of typing. */
43 #define DRM_ENUM_NAME_FN(fnname, list) \
44 char *fnname(int val) \
47 for (i = 0; i < ARRAY_SIZE(list); i++) { \
48 if (list[i].type == val) \
49 return list[i].name; \
57 static struct drm_prop_enum_list drm_dpms_enum_list[] =
58 { { DRM_MODE_DPMS_ON, "On" },
59 { DRM_MODE_DPMS_STANDBY, "Standby" },
60 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
61 { DRM_MODE_DPMS_OFF, "Off" }
64 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
69 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
71 { DRM_MODE_SCALE_NON_GPU, "Non-GPU" },
72 { DRM_MODE_SCALE_FULLSCREEN, "Fullscreen" },
73 { DRM_MODE_SCALE_NO_SCALE, "No scale" },
74 { DRM_MODE_SCALE_ASPECT, "Aspect" },
77 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
79 { DRM_MODE_DITHERING_OFF, "Off" },
80 { DRM_MODE_DITHERING_ON, "On" },
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 */
113 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
117 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
118 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
119 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
123 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
124 drm_tv_subconnector_enum_list)
126 struct drm_conn_prop_enum_list {
133 * Connector and encoder types.
135 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
136 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
137 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
138 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
139 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
140 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
141 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
142 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
143 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
144 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
145 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN", 0 },
146 { DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort", 0 },
147 { DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A", 0 },
148 { DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B", 0 },
151 static struct drm_prop_enum_list drm_encoder_enum_list[] =
152 { { DRM_MODE_ENCODER_NONE, "None" },
153 { DRM_MODE_ENCODER_DAC, "DAC" },
154 { DRM_MODE_ENCODER_TMDS, "TMDS" },
155 { DRM_MODE_ENCODER_LVDS, "LVDS" },
156 { DRM_MODE_ENCODER_TVDAC, "TV" },
159 char *drm_get_encoder_name(struct drm_encoder *encoder)
163 snprintf(buf, 32, "%s-%d",
164 drm_encoder_enum_list[encoder->encoder_type].name,
169 char *drm_get_connector_name(struct drm_connector *connector)
173 snprintf(buf, 32, "%s-%d",
174 drm_connector_enum_list[connector->connector_type].name,
175 connector->connector_type_id);
178 EXPORT_SYMBOL(drm_get_connector_name);
180 char *drm_get_connector_status_name(enum drm_connector_status status)
182 if (status == connector_status_connected)
184 else if (status == connector_status_disconnected)
185 return "disconnected";
191 * drm_mode_object_get - allocate a new identifier
193 * @ptr: object pointer, used to generate unique ID
198 * Create a unique identifier based on @ptr in @dev's identifier space. Used
199 * for tracking modes, CRTCs and connectors.
202 * New unique (relative to other objects in @dev) integer identifier for the
205 static int drm_mode_object_get(struct drm_device *dev,
206 struct drm_mode_object *obj, uint32_t obj_type)
212 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
213 DRM_ERROR("Ran out memory getting a mode number\n");
217 mutex_lock(&dev->mode_config.idr_mutex);
218 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
219 mutex_unlock(&dev->mode_config.idr_mutex);
224 obj->type = obj_type;
229 * drm_mode_object_put - free an identifer
234 * Caller must hold DRM mode_config lock.
236 * Free @id from @dev's unique identifier pool.
238 static void drm_mode_object_put(struct drm_device *dev,
239 struct drm_mode_object *object)
241 mutex_lock(&dev->mode_config.idr_mutex);
242 idr_remove(&dev->mode_config.crtc_idr, object->id);
243 mutex_unlock(&dev->mode_config.idr_mutex);
246 void *drm_mode_object_find(struct drm_device *dev, uint32_t id, uint32_t type)
248 struct drm_mode_object *obj = NULL;
250 mutex_lock(&dev->mode_config.idr_mutex);
251 obj = idr_find(&dev->mode_config.crtc_idr, id);
252 if (!obj || (obj->type != type) || (obj->id != id))
254 mutex_unlock(&dev->mode_config.idr_mutex);
258 EXPORT_SYMBOL(drm_mode_object_find);
261 * drm_crtc_from_fb - find the CRTC structure associated with an fb
263 * @fb: framebuffer in question
266 * Caller must hold mode_config lock.
268 * Find CRTC in the mode_config structure that matches @fb.
271 * Pointer to the CRTC or NULL if it wasn't found.
273 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
274 struct drm_framebuffer *fb)
276 struct drm_crtc *crtc;
278 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
286 * drm_framebuffer_init - initialize a framebuffer
290 * Caller must hold mode config lock.
292 * Allocates an ID for the framebuffer's parent mode object, sets its mode
293 * functions & device file and adds it to the master fd list.
296 * Zero on success, error code on falure.
298 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
299 const struct drm_framebuffer_funcs *funcs)
303 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
310 dev->mode_config.num_fb++;
311 list_add(&fb->head, &dev->mode_config.fb_list);
315 EXPORT_SYMBOL(drm_framebuffer_init);
318 * drm_framebuffer_cleanup - remove a framebuffer object
319 * @fb: framebuffer to remove
322 * Caller must hold mode config lock.
324 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
325 * it, setting it to NULL.
327 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
329 struct drm_device *dev = fb->dev;
330 struct drm_crtc *crtc;
332 /* remove from any CRTC */
333 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
338 drm_mode_object_put(dev, &fb->base);
340 dev->mode_config.num_fb--;
342 EXPORT_SYMBOL(drm_framebuffer_cleanup);
345 * drm_crtc_init - Initialise a new CRTC object
347 * @crtc: CRTC object to init
348 * @funcs: callbacks for the new CRTC
351 * Caller must hold mode config lock.
353 * Inits a new object created as base part of an driver crtc object.
355 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
356 const struct drm_crtc_funcs *funcs)
361 mutex_lock(&dev->mode_config.mutex);
362 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
364 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
365 dev->mode_config.num_crtc++;
366 mutex_unlock(&dev->mode_config.mutex);
368 EXPORT_SYMBOL(drm_crtc_init);
371 * drm_crtc_cleanup - Cleans up the core crtc usage.
372 * @crtc: CRTC to cleanup
375 * Caller must hold mode config lock.
377 * Cleanup @crtc. Removes from drm modesetting space
378 * does NOT free object, caller does that.
380 void drm_crtc_cleanup(struct drm_crtc *crtc)
382 struct drm_device *dev = crtc->dev;
384 if (crtc->gamma_store) {
385 kfree(crtc->gamma_store);
386 crtc->gamma_store = NULL;
389 drm_mode_object_put(dev, &crtc->base);
390 list_del(&crtc->head);
391 dev->mode_config.num_crtc--;
393 EXPORT_SYMBOL(drm_crtc_cleanup);
396 * drm_mode_probed_add - add a mode to a connector's probed mode list
397 * @connector: connector the new mode
401 * Caller must hold mode config lock.
403 * Add @mode to @connector's mode list for later use.
405 void drm_mode_probed_add(struct drm_connector *connector,
406 struct drm_display_mode *mode)
408 list_add(&mode->head, &connector->probed_modes);
410 EXPORT_SYMBOL(drm_mode_probed_add);
413 * drm_mode_remove - remove and free a mode
414 * @connector: connector list to modify
415 * @mode: mode to remove
418 * Caller must hold mode config lock.
420 * Remove @mode from @connector's mode list, then free it.
422 void drm_mode_remove(struct drm_connector *connector,
423 struct drm_display_mode *mode)
425 list_del(&mode->head);
428 EXPORT_SYMBOL(drm_mode_remove);
431 * drm_connector_init - Init a preallocated connector
433 * @connector: the connector to init
434 * @funcs: callbacks for this connector
435 * @name: user visible name of the connector
438 * Caller must hold @dev's mode_config lock.
440 * Initialises a preallocated connector. Connectors should be
441 * subclassed as part of driver connector objects.
443 void drm_connector_init(struct drm_device *dev,
444 struct drm_connector *connector,
445 const struct drm_connector_funcs *funcs,
448 mutex_lock(&dev->mode_config.mutex);
450 connector->dev = dev;
451 connector->funcs = funcs;
452 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
453 connector->connector_type = connector_type;
454 connector->connector_type_id =
455 ++drm_connector_enum_list[connector_type].count; /* TODO */
456 INIT_LIST_HEAD(&connector->user_modes);
457 INIT_LIST_HEAD(&connector->probed_modes);
458 INIT_LIST_HEAD(&connector->modes);
459 connector->edid_blob_ptr = NULL;
461 list_add_tail(&connector->head, &dev->mode_config.connector_list);
462 dev->mode_config.num_connector++;
464 drm_connector_attach_property(connector,
465 dev->mode_config.edid_property, 0);
467 drm_connector_attach_property(connector,
468 dev->mode_config.dpms_property, 0);
470 mutex_unlock(&dev->mode_config.mutex);
472 EXPORT_SYMBOL(drm_connector_init);
475 * drm_connector_cleanup - cleans up an initialised connector
476 * @connector: connector to cleanup
479 * Caller must hold @dev's mode_config lock.
481 * Cleans up the connector but doesn't free the object.
483 void drm_connector_cleanup(struct drm_connector *connector)
485 struct drm_device *dev = connector->dev;
486 struct drm_display_mode *mode, *t;
488 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
489 drm_mode_remove(connector, mode);
491 list_for_each_entry_safe(mode, t, &connector->modes, head)
492 drm_mode_remove(connector, mode);
494 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
495 drm_mode_remove(connector, mode);
497 mutex_lock(&dev->mode_config.mutex);
498 drm_mode_object_put(dev, &connector->base);
499 list_del(&connector->head);
500 mutex_unlock(&dev->mode_config.mutex);
502 EXPORT_SYMBOL(drm_connector_cleanup);
504 void drm_encoder_init(struct drm_device *dev,
505 struct drm_encoder *encoder,
506 const struct drm_encoder_funcs *funcs,
509 mutex_lock(&dev->mode_config.mutex);
513 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
514 encoder->encoder_type = encoder_type;
515 encoder->funcs = funcs;
517 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
518 dev->mode_config.num_encoder++;
520 mutex_unlock(&dev->mode_config.mutex);
522 EXPORT_SYMBOL(drm_encoder_init);
524 void drm_encoder_cleanup(struct drm_encoder *encoder)
526 struct drm_device *dev = encoder->dev;
527 mutex_lock(&dev->mode_config.mutex);
528 drm_mode_object_put(dev, &encoder->base);
529 list_del(&encoder->head);
530 mutex_unlock(&dev->mode_config.mutex);
532 EXPORT_SYMBOL(drm_encoder_cleanup);
535 * drm_mode_create - create a new display mode
539 * Caller must hold DRM mode_config lock.
541 * Create a new drm_display_mode, give it an ID, and return it.
544 * Pointer to new mode on success, NULL on error.
546 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
548 struct drm_display_mode *nmode;
550 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
554 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
557 EXPORT_SYMBOL(drm_mode_create);
560 * drm_mode_destroy - remove a mode
562 * @mode: mode to remove
565 * Caller must hold mode config lock.
567 * Free @mode's unique identifier, then free it.
569 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
571 drm_mode_object_put(dev, &mode->base);
575 EXPORT_SYMBOL(drm_mode_destroy);
577 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
579 struct drm_property *edid;
580 struct drm_property *dpms;
584 * Standard properties (apply to all connectors)
586 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
587 DRM_MODE_PROP_IMMUTABLE,
589 dev->mode_config.edid_property = edid;
591 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
592 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
593 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
594 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
595 drm_dpms_enum_list[i].name);
596 dev->mode_config.dpms_property = dpms;
602 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
605 * Called by a driver the first time a DVI-I connector is made.
607 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
609 struct drm_property *dvi_i_selector;
610 struct drm_property *dvi_i_subconnector;
613 if (dev->mode_config.dvi_i_select_subconnector_property)
617 drm_property_create(dev, DRM_MODE_PROP_ENUM,
618 "select subconnector",
619 ARRAY_SIZE(drm_dvi_i_select_enum_list));
620 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
621 drm_property_add_enum(dvi_i_selector, i,
622 drm_dvi_i_select_enum_list[i].type,
623 drm_dvi_i_select_enum_list[i].name);
624 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
627 drm_property_create(dev, DRM_MODE_PROP_ENUM |
628 DRM_MODE_PROP_IMMUTABLE,
630 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
631 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
632 drm_property_add_enum(dvi_i_subconnector, i,
633 drm_dvi_i_subconnector_enum_list[i].type,
634 drm_dvi_i_subconnector_enum_list[i].name);
635 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
639 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
642 * drm_create_tv_properties - create TV specific connector properties
644 * @num_modes: number of different TV formats (modes) supported
645 * @modes: array of pointers to strings containing name of each format
647 * Called by a driver's TV initialization routine, this function creates
648 * the TV specific connector properties for a given device. Caller is
649 * responsible for allocating a list of format names and passing them to
652 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
655 struct drm_property *tv_selector;
656 struct drm_property *tv_subconnector;
659 if (dev->mode_config.tv_select_subconnector_property)
663 * Basic connector properties
665 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
666 "select subconnector",
667 ARRAY_SIZE(drm_tv_select_enum_list));
668 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
669 drm_property_add_enum(tv_selector, i,
670 drm_tv_select_enum_list[i].type,
671 drm_tv_select_enum_list[i].name);
672 dev->mode_config.tv_select_subconnector_property = tv_selector;
675 drm_property_create(dev, DRM_MODE_PROP_ENUM |
676 DRM_MODE_PROP_IMMUTABLE, "subconnector",
677 ARRAY_SIZE(drm_tv_subconnector_enum_list));
678 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
679 drm_property_add_enum(tv_subconnector, i,
680 drm_tv_subconnector_enum_list[i].type,
681 drm_tv_subconnector_enum_list[i].name);
682 dev->mode_config.tv_subconnector_property = tv_subconnector;
685 * Other, TV specific properties: margins & TV modes.
687 dev->mode_config.tv_left_margin_property =
688 drm_property_create(dev, DRM_MODE_PROP_RANGE,
690 dev->mode_config.tv_left_margin_property->values[0] = 0;
691 dev->mode_config.tv_left_margin_property->values[1] = 100;
693 dev->mode_config.tv_right_margin_property =
694 drm_property_create(dev, DRM_MODE_PROP_RANGE,
696 dev->mode_config.tv_right_margin_property->values[0] = 0;
697 dev->mode_config.tv_right_margin_property->values[1] = 100;
699 dev->mode_config.tv_top_margin_property =
700 drm_property_create(dev, DRM_MODE_PROP_RANGE,
702 dev->mode_config.tv_top_margin_property->values[0] = 0;
703 dev->mode_config.tv_top_margin_property->values[1] = 100;
705 dev->mode_config.tv_bottom_margin_property =
706 drm_property_create(dev, DRM_MODE_PROP_RANGE,
708 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
709 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
711 dev->mode_config.tv_mode_property =
712 drm_property_create(dev, DRM_MODE_PROP_ENUM,
714 for (i = 0; i < num_modes; i++)
715 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
720 EXPORT_SYMBOL(drm_mode_create_tv_properties);
723 * drm_mode_create_scaling_mode_property - create scaling mode property
726 * Called by a driver the first time it's needed, must be attached to desired
729 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
731 struct drm_property *scaling_mode;
734 if (dev->mode_config.scaling_mode_property)
738 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
739 ARRAY_SIZE(drm_scaling_mode_enum_list));
740 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
741 drm_property_add_enum(scaling_mode, i,
742 drm_scaling_mode_enum_list[i].type,
743 drm_scaling_mode_enum_list[i].name);
745 dev->mode_config.scaling_mode_property = scaling_mode;
749 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
752 * drm_mode_create_dithering_property - create dithering property
755 * Called by a driver the first time it's needed, must be attached to desired
758 int drm_mode_create_dithering_property(struct drm_device *dev)
760 struct drm_property *dithering_mode;
763 if (dev->mode_config.dithering_mode_property)
767 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
768 ARRAY_SIZE(drm_dithering_mode_enum_list));
769 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
770 drm_property_add_enum(dithering_mode, i,
771 drm_dithering_mode_enum_list[i].type,
772 drm_dithering_mode_enum_list[i].name);
773 dev->mode_config.dithering_mode_property = dithering_mode;
777 EXPORT_SYMBOL(drm_mode_create_dithering_property);
780 * drm_mode_config_init - initialize DRM mode_configuration structure
784 * None, should happen single threaded at init time.
786 * Initialize @dev's mode_config structure, used for tracking the graphics
787 * configuration of @dev.
789 void drm_mode_config_init(struct drm_device *dev)
791 mutex_init(&dev->mode_config.mutex);
792 mutex_init(&dev->mode_config.idr_mutex);
793 INIT_LIST_HEAD(&dev->mode_config.fb_list);
794 INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
795 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
796 INIT_LIST_HEAD(&dev->mode_config.connector_list);
797 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
798 INIT_LIST_HEAD(&dev->mode_config.property_list);
799 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
800 idr_init(&dev->mode_config.crtc_idr);
802 mutex_lock(&dev->mode_config.mutex);
803 drm_mode_create_standard_connector_properties(dev);
804 mutex_unlock(&dev->mode_config.mutex);
806 /* Just to be sure */
807 dev->mode_config.num_fb = 0;
808 dev->mode_config.num_connector = 0;
809 dev->mode_config.num_crtc = 0;
810 dev->mode_config.num_encoder = 0;
812 EXPORT_SYMBOL(drm_mode_config_init);
814 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
816 uint32_t total_objects = 0;
818 total_objects += dev->mode_config.num_crtc;
819 total_objects += dev->mode_config.num_connector;
820 total_objects += dev->mode_config.num_encoder;
822 if (total_objects == 0)
825 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
829 group->num_crtcs = 0;
830 group->num_connectors = 0;
831 group->num_encoders = 0;
835 int drm_mode_group_init_legacy_group(struct drm_device *dev,
836 struct drm_mode_group *group)
838 struct drm_crtc *crtc;
839 struct drm_encoder *encoder;
840 struct drm_connector *connector;
843 if ((ret = drm_mode_group_init(dev, group)))
846 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
847 group->id_list[group->num_crtcs++] = crtc->base.id;
849 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
850 group->id_list[group->num_crtcs + group->num_encoders++] =
853 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
854 group->id_list[group->num_crtcs + group->num_encoders +
855 group->num_connectors++] = connector->base.id;
861 * drm_mode_config_cleanup - free up DRM mode_config info
865 * Caller must hold mode config lock.
867 * Free up all the connectors and CRTCs associated with this DRM device, then
868 * free up the framebuffers and associated buffer objects.
870 * FIXME: cleanup any dangling user buffer objects too
872 void drm_mode_config_cleanup(struct drm_device *dev)
874 struct drm_connector *connector, *ot;
875 struct drm_crtc *crtc, *ct;
876 struct drm_encoder *encoder, *enct;
877 struct drm_framebuffer *fb, *fbt;
878 struct drm_property *property, *pt;
880 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
882 encoder->funcs->destroy(encoder);
885 list_for_each_entry_safe(connector, ot,
886 &dev->mode_config.connector_list, head) {
887 connector->funcs->destroy(connector);
890 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
892 drm_property_destroy(dev, property);
895 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
896 fb->funcs->destroy(fb);
899 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
900 crtc->funcs->destroy(crtc);
904 EXPORT_SYMBOL(drm_mode_config_cleanup);
907 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
908 * @out: drm_mode_modeinfo struct to return to the user
909 * @in: drm_display_mode to use
914 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
917 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
918 struct drm_display_mode *in)
920 out->clock = in->clock;
921 out->hdisplay = in->hdisplay;
922 out->hsync_start = in->hsync_start;
923 out->hsync_end = in->hsync_end;
924 out->htotal = in->htotal;
925 out->hskew = in->hskew;
926 out->vdisplay = in->vdisplay;
927 out->vsync_start = in->vsync_start;
928 out->vsync_end = in->vsync_end;
929 out->vtotal = in->vtotal;
930 out->vscan = in->vscan;
931 out->vrefresh = in->vrefresh;
932 out->flags = in->flags;
933 out->type = in->type;
934 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
935 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
939 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
940 * @out: drm_display_mode to return to the user
941 * @in: drm_mode_modeinfo to use
946 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
949 void drm_crtc_convert_umode(struct drm_display_mode *out,
950 struct drm_mode_modeinfo *in)
952 out->clock = in->clock;
953 out->hdisplay = in->hdisplay;
954 out->hsync_start = in->hsync_start;
955 out->hsync_end = in->hsync_end;
956 out->htotal = in->htotal;
957 out->hskew = in->hskew;
958 out->vdisplay = in->vdisplay;
959 out->vsync_start = in->vsync_start;
960 out->vsync_end = in->vsync_end;
961 out->vtotal = in->vtotal;
962 out->vscan = in->vscan;
963 out->vrefresh = in->vrefresh;
964 out->flags = in->flags;
965 out->type = in->type;
966 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
967 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
971 * drm_mode_getresources - get graphics configuration
972 * @inode: inode from the ioctl
973 * @filp: file * from the ioctl
974 * @cmd: cmd from ioctl
975 * @arg: arg from ioctl
978 * Takes mode config lock.
980 * Construct a set of configuration description structures and return
981 * them to the user, including CRTC, connector and framebuffer configuration.
983 * Called by the user via ioctl.
986 * Zero on success, errno on failure.
988 int drm_mode_getresources(struct drm_device *dev, void *data,
989 struct drm_file *file_priv)
991 struct drm_mode_card_res *card_res = data;
992 struct list_head *lh;
993 struct drm_framebuffer *fb;
994 struct drm_connector *connector;
995 struct drm_crtc *crtc;
996 struct drm_encoder *encoder;
998 int connector_count = 0;
1001 int encoder_count = 0;
1003 uint32_t __user *fb_id;
1004 uint32_t __user *crtc_id;
1005 uint32_t __user *connector_id;
1006 uint32_t __user *encoder_id;
1007 struct drm_mode_group *mode_group;
1009 mutex_lock(&dev->mode_config.mutex);
1012 * For the non-control nodes we need to limit the list of resources
1013 * by IDs in the group list for this node
1015 list_for_each(lh, &file_priv->fbs)
1018 mode_group = &file_priv->master->minor->mode_group;
1019 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1021 list_for_each(lh, &dev->mode_config.crtc_list)
1024 list_for_each(lh, &dev->mode_config.connector_list)
1027 list_for_each(lh, &dev->mode_config.encoder_list)
1031 crtc_count = mode_group->num_crtcs;
1032 connector_count = mode_group->num_connectors;
1033 encoder_count = mode_group->num_encoders;
1036 card_res->max_height = dev->mode_config.max_height;
1037 card_res->min_height = dev->mode_config.min_height;
1038 card_res->max_width = dev->mode_config.max_width;
1039 card_res->min_width = dev->mode_config.min_width;
1041 /* handle this in 4 parts */
1043 if (card_res->count_fbs >= fb_count) {
1045 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1046 list_for_each_entry(fb, &file_priv->fbs, head) {
1047 if (put_user(fb->base.id, fb_id + copied)) {
1054 card_res->count_fbs = fb_count;
1057 if (card_res->count_crtcs >= crtc_count) {
1059 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1060 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1061 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1063 DRM_DEBUG("CRTC ID is %d\n", crtc->base.id);
1064 if (put_user(crtc->base.id, crtc_id + copied)) {
1071 for (i = 0; i < mode_group->num_crtcs; i++) {
1072 if (put_user(mode_group->id_list[i],
1073 crtc_id + copied)) {
1081 card_res->count_crtcs = crtc_count;
1084 if (card_res->count_encoders >= encoder_count) {
1086 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1087 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1088 list_for_each_entry(encoder,
1089 &dev->mode_config.encoder_list,
1091 DRM_DEBUG("ENCODER ID is %d\n",
1093 if (put_user(encoder->base.id, encoder_id +
1101 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1102 if (put_user(mode_group->id_list[i],
1103 encoder_id + copied)) {
1112 card_res->count_encoders = encoder_count;
1115 if (card_res->count_connectors >= connector_count) {
1117 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1118 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1119 list_for_each_entry(connector,
1120 &dev->mode_config.connector_list,
1122 DRM_DEBUG("CONNECTOR ID is %d\n",
1123 connector->base.id);
1124 if (put_user(connector->base.id,
1125 connector_id + copied)) {
1132 int start = mode_group->num_crtcs +
1133 mode_group->num_encoders;
1134 for (i = start; i < start + mode_group->num_connectors; i++) {
1135 if (put_user(mode_group->id_list[i],
1136 connector_id + copied)) {
1144 card_res->count_connectors = connector_count;
1146 DRM_DEBUG("Counted %d %d %d\n", card_res->count_crtcs,
1147 card_res->count_connectors, card_res->count_encoders);
1150 mutex_unlock(&dev->mode_config.mutex);
1155 * drm_mode_getcrtc - get CRTC configuration
1156 * @inode: inode from the ioctl
1157 * @filp: file * from the ioctl
1158 * @cmd: cmd from ioctl
1159 * @arg: arg from ioctl
1164 * Construct a CRTC configuration structure to return to the user.
1166 * Called by the user via ioctl.
1169 * Zero on success, errno on failure.
1171 int drm_mode_getcrtc(struct drm_device *dev,
1172 void *data, struct drm_file *file_priv)
1174 struct drm_mode_crtc *crtc_resp = data;
1175 struct drm_crtc *crtc;
1176 struct drm_mode_object *obj;
1179 mutex_lock(&dev->mode_config.mutex);
1181 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1182 DRM_MODE_OBJECT_CRTC);
1187 crtc = obj_to_crtc(obj);
1189 crtc_resp->x = crtc->x;
1190 crtc_resp->y = crtc->y;
1191 crtc_resp->gamma_size = crtc->gamma_size;
1193 crtc_resp->fb_id = crtc->fb->base.id;
1195 crtc_resp->fb_id = 0;
1197 if (crtc->enabled) {
1199 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1200 crtc_resp->mode_valid = 1;
1203 crtc_resp->mode_valid = 0;
1207 mutex_unlock(&dev->mode_config.mutex);
1212 * drm_mode_getconnector - get connector configuration
1213 * @inode: inode from the ioctl
1214 * @filp: file * from the ioctl
1215 * @cmd: cmd from ioctl
1216 * @arg: arg from ioctl
1221 * Construct a connector configuration structure to return to the user.
1223 * Called by the user via ioctl.
1226 * Zero on success, errno on failure.
1228 int drm_mode_getconnector(struct drm_device *dev, void *data,
1229 struct drm_file *file_priv)
1231 struct drm_mode_get_connector *out_resp = data;
1232 struct drm_mode_object *obj;
1233 struct drm_connector *connector;
1234 struct drm_display_mode *mode;
1236 int props_count = 0;
1237 int encoders_count = 0;
1241 struct drm_mode_modeinfo u_mode;
1242 struct drm_mode_modeinfo __user *mode_ptr;
1243 uint32_t __user *prop_ptr;
1244 uint64_t __user *prop_values;
1245 uint32_t __user *encoder_ptr;
1247 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1249 DRM_DEBUG("connector id %d:\n", out_resp->connector_id);
1251 mutex_lock(&dev->mode_config.mutex);
1253 obj = drm_mode_object_find(dev, out_resp->connector_id,
1254 DRM_MODE_OBJECT_CONNECTOR);
1259 connector = obj_to_connector(obj);
1261 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1262 if (connector->property_ids[i] != 0) {
1267 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1268 if (connector->encoder_ids[i] != 0) {
1273 if (out_resp->count_modes == 0) {
1274 connector->funcs->fill_modes(connector,
1275 dev->mode_config.max_width,
1276 dev->mode_config.max_height);
1279 /* delayed so we get modes regardless of pre-fill_modes state */
1280 list_for_each_entry(mode, &connector->modes, head)
1283 out_resp->connector_id = connector->base.id;
1284 out_resp->connector_type = connector->connector_type;
1285 out_resp->connector_type_id = connector->connector_type_id;
1286 out_resp->mm_width = connector->display_info.width_mm;
1287 out_resp->mm_height = connector->display_info.height_mm;
1288 out_resp->subpixel = connector->display_info.subpixel_order;
1289 out_resp->connection = connector->status;
1290 if (connector->encoder)
1291 out_resp->encoder_id = connector->encoder->base.id;
1293 out_resp->encoder_id = 0;
1296 * This ioctl is called twice, once to determine how much space is
1297 * needed, and the 2nd time to fill it.
1299 if ((out_resp->count_modes >= mode_count) && mode_count) {
1301 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1302 list_for_each_entry(mode, &connector->modes, head) {
1303 drm_crtc_convert_to_umode(&u_mode, mode);
1304 if (copy_to_user(mode_ptr + copied,
1305 &u_mode, sizeof(u_mode))) {
1312 out_resp->count_modes = mode_count;
1314 if ((out_resp->count_props >= props_count) && props_count) {
1316 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1317 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1318 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1319 if (connector->property_ids[i] != 0) {
1320 if (put_user(connector->property_ids[i],
1321 prop_ptr + copied)) {
1326 if (put_user(connector->property_values[i],
1327 prop_values + copied)) {
1335 out_resp->count_props = props_count;
1337 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1339 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1340 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1341 if (connector->encoder_ids[i] != 0) {
1342 if (put_user(connector->encoder_ids[i],
1343 encoder_ptr + copied)) {
1351 out_resp->count_encoders = encoders_count;
1354 mutex_unlock(&dev->mode_config.mutex);
1358 int drm_mode_getencoder(struct drm_device *dev, void *data,
1359 struct drm_file *file_priv)
1361 struct drm_mode_get_encoder *enc_resp = data;
1362 struct drm_mode_object *obj;
1363 struct drm_encoder *encoder;
1366 mutex_lock(&dev->mode_config.mutex);
1367 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1368 DRM_MODE_OBJECT_ENCODER);
1373 encoder = obj_to_encoder(obj);
1376 enc_resp->crtc_id = encoder->crtc->base.id;
1378 enc_resp->crtc_id = 0;
1379 enc_resp->encoder_type = encoder->encoder_type;
1380 enc_resp->encoder_id = encoder->base.id;
1381 enc_resp->possible_crtcs = encoder->possible_crtcs;
1382 enc_resp->possible_clones = encoder->possible_clones;
1385 mutex_unlock(&dev->mode_config.mutex);
1390 * drm_mode_setcrtc - set CRTC configuration
1391 * @inode: inode from the ioctl
1392 * @filp: file * from the ioctl
1393 * @cmd: cmd from ioctl
1394 * @arg: arg from ioctl
1399 * Build a new CRTC configuration based on user request.
1401 * Called by the user via ioctl.
1404 * Zero on success, errno on failure.
1406 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1407 struct drm_file *file_priv)
1409 struct drm_mode_config *config = &dev->mode_config;
1410 struct drm_mode_crtc *crtc_req = data;
1411 struct drm_mode_object *obj;
1412 struct drm_crtc *crtc, *crtcfb;
1413 struct drm_connector **connector_set = NULL, *connector;
1414 struct drm_framebuffer *fb = NULL;
1415 struct drm_display_mode *mode = NULL;
1416 struct drm_mode_set set;
1417 uint32_t __user *set_connectors_ptr;
1421 mutex_lock(&dev->mode_config.mutex);
1422 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1423 DRM_MODE_OBJECT_CRTC);
1425 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1429 crtc = obj_to_crtc(obj);
1431 if (crtc_req->mode_valid) {
1432 /* If we have a mode we need a framebuffer. */
1433 /* If we pass -1, set the mode with the currently bound fb */
1434 if (crtc_req->fb_id == -1) {
1435 list_for_each_entry(crtcfb,
1436 &dev->mode_config.crtc_list, head) {
1437 if (crtcfb == crtc) {
1438 DRM_DEBUG("Using current fb for setmode\n");
1443 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1444 DRM_MODE_OBJECT_FB);
1446 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1450 fb = obj_to_fb(obj);
1453 mode = drm_mode_create(dev);
1454 drm_crtc_convert_umode(mode, &crtc_req->mode);
1455 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1458 if (crtc_req->count_connectors == 0 && mode) {
1459 DRM_DEBUG("Count connectors is 0 but mode set\n");
1464 if (crtc_req->count_connectors > 0 && !mode && !fb) {
1465 DRM_DEBUG("Count connectors is %d but no mode or fb set\n",
1466 crtc_req->count_connectors);
1471 if (crtc_req->count_connectors > 0) {
1474 /* Avoid unbounded kernel memory allocation */
1475 if (crtc_req->count_connectors > config->num_connector) {
1480 connector_set = kmalloc(crtc_req->count_connectors *
1481 sizeof(struct drm_connector *),
1483 if (!connector_set) {
1488 for (i = 0; i < crtc_req->count_connectors; i++) {
1489 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1490 if (get_user(out_id, &set_connectors_ptr[i])) {
1495 obj = drm_mode_object_find(dev, out_id,
1496 DRM_MODE_OBJECT_CONNECTOR);
1498 DRM_DEBUG("Connector id %d unknown\n", out_id);
1502 connector = obj_to_connector(obj);
1504 connector_set[i] = connector;
1509 set.x = crtc_req->x;
1510 set.y = crtc_req->y;
1512 set.connectors = connector_set;
1513 set.num_connectors = crtc_req->count_connectors;
1515 ret = crtc->funcs->set_config(&set);
1518 kfree(connector_set);
1519 mutex_unlock(&dev->mode_config.mutex);
1523 int drm_mode_cursor_ioctl(struct drm_device *dev,
1524 void *data, struct drm_file *file_priv)
1526 struct drm_mode_cursor *req = data;
1527 struct drm_mode_object *obj;
1528 struct drm_crtc *crtc;
1534 DRM_ERROR("no operation set\n");
1538 mutex_lock(&dev->mode_config.mutex);
1539 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1541 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc_id);
1545 crtc = obj_to_crtc(obj);
1547 if (req->flags & DRM_MODE_CURSOR_BO) {
1548 if (!crtc->funcs->cursor_set) {
1549 DRM_ERROR("crtc does not support cursor\n");
1553 /* Turns off the cursor if handle is 0 */
1554 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1555 req->width, req->height);
1558 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1559 if (crtc->funcs->cursor_move) {
1560 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1562 DRM_ERROR("crtc does not support cursor\n");
1568 mutex_unlock(&dev->mode_config.mutex);
1573 * drm_mode_addfb - add an FB to the graphics configuration
1574 * @inode: inode from the ioctl
1575 * @filp: file * from the ioctl
1576 * @cmd: cmd from ioctl
1577 * @arg: arg from ioctl
1580 * Takes mode config lock.
1582 * Add a new FB to the specified CRTC, given a user request.
1584 * Called by the user via ioctl.
1587 * Zero on success, errno on failure.
1589 int drm_mode_addfb(struct drm_device *dev,
1590 void *data, struct drm_file *file_priv)
1592 struct drm_mode_fb_cmd *r = data;
1593 struct drm_mode_config *config = &dev->mode_config;
1594 struct drm_framebuffer *fb;
1597 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1598 DRM_ERROR("mode new framebuffer width not within limits\n");
1601 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1602 DRM_ERROR("mode new framebuffer height not within limits\n");
1606 mutex_lock(&dev->mode_config.mutex);
1608 /* TODO check buffer is sufficently large */
1609 /* TODO setup destructor callback */
1611 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1613 DRM_ERROR("could not create framebuffer\n");
1618 r->fb_id = fb->base.id;
1619 list_add(&fb->filp_head, &file_priv->fbs);
1622 mutex_unlock(&dev->mode_config.mutex);
1627 * drm_mode_rmfb - remove an FB from the configuration
1628 * @inode: inode from the ioctl
1629 * @filp: file * from the ioctl
1630 * @cmd: cmd from ioctl
1631 * @arg: arg from ioctl
1634 * Takes mode config lock.
1636 * Remove the FB specified by the user.
1638 * Called by the user via ioctl.
1641 * Zero on success, errno on failure.
1643 int drm_mode_rmfb(struct drm_device *dev,
1644 void *data, struct drm_file *file_priv)
1646 struct drm_mode_object *obj;
1647 struct drm_framebuffer *fb = NULL;
1648 struct drm_framebuffer *fbl = NULL;
1649 uint32_t *id = data;
1653 mutex_lock(&dev->mode_config.mutex);
1654 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1655 /* TODO check that we realy get a framebuffer back. */
1657 DRM_ERROR("mode invalid framebuffer id\n");
1661 fb = obj_to_fb(obj);
1663 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1668 DRM_ERROR("tried to remove a fb that we didn't own\n");
1673 /* TODO release all crtc connected to the framebuffer */
1674 /* TODO unhock the destructor from the buffer object */
1676 list_del(&fb->filp_head);
1677 fb->funcs->destroy(fb);
1680 mutex_unlock(&dev->mode_config.mutex);
1685 * drm_mode_getfb - get FB info
1686 * @inode: inode from the ioctl
1687 * @filp: file * from the ioctl
1688 * @cmd: cmd from ioctl
1689 * @arg: arg from ioctl
1694 * Lookup the FB given its ID and return info about it.
1696 * Called by the user via ioctl.
1699 * Zero on success, errno on failure.
1701 int drm_mode_getfb(struct drm_device *dev,
1702 void *data, struct drm_file *file_priv)
1704 struct drm_mode_fb_cmd *r = data;
1705 struct drm_mode_object *obj;
1706 struct drm_framebuffer *fb;
1709 mutex_lock(&dev->mode_config.mutex);
1710 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1712 DRM_ERROR("invalid framebuffer id\n");
1716 fb = obj_to_fb(obj);
1718 r->height = fb->height;
1719 r->width = fb->width;
1720 r->depth = fb->depth;
1721 r->bpp = fb->bits_per_pixel;
1722 r->pitch = fb->pitch;
1723 fb->funcs->create_handle(fb, file_priv, &r->handle);
1726 mutex_unlock(&dev->mode_config.mutex);
1731 * drm_fb_release - remove and free the FBs on this file
1732 * @filp: file * from the ioctl
1735 * Takes mode config lock.
1737 * Destroy all the FBs associated with @filp.
1739 * Called by the user via ioctl.
1742 * Zero on success, errno on failure.
1744 void drm_fb_release(struct file *filp)
1746 struct drm_file *priv = filp->private_data;
1747 struct drm_device *dev = priv->minor->dev;
1748 struct drm_framebuffer *fb, *tfb;
1750 mutex_lock(&dev->mode_config.mutex);
1751 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1752 list_del(&fb->filp_head);
1753 fb->funcs->destroy(fb);
1755 mutex_unlock(&dev->mode_config.mutex);
1759 * drm_mode_attachmode - add a mode to the user mode list
1761 * @connector: connector to add the mode to
1762 * @mode: mode to add
1764 * Add @mode to @connector's user mode list.
1766 static int drm_mode_attachmode(struct drm_device *dev,
1767 struct drm_connector *connector,
1768 struct drm_display_mode *mode)
1772 list_add_tail(&mode->head, &connector->user_modes);
1776 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1777 struct drm_display_mode *mode)
1779 struct drm_connector *connector;
1781 struct drm_display_mode *dup_mode;
1783 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1784 if (!connector->encoder)
1786 if (connector->encoder->crtc == crtc) {
1788 dup_mode = drm_mode_duplicate(dev, mode);
1791 ret = drm_mode_attachmode(dev, connector, dup_mode);
1799 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1801 static int drm_mode_detachmode(struct drm_device *dev,
1802 struct drm_connector *connector,
1803 struct drm_display_mode *mode)
1807 struct drm_display_mode *match_mode, *t;
1809 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1810 if (drm_mode_equal(match_mode, mode)) {
1811 list_del(&match_mode->head);
1812 drm_mode_destroy(dev, match_mode);
1824 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1826 struct drm_connector *connector;
1828 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1829 drm_mode_detachmode(dev, connector, mode);
1833 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1836 * drm_fb_attachmode - Attach a user mode to an connector
1837 * @inode: inode from the ioctl
1838 * @filp: file * from the ioctl
1839 * @cmd: cmd from ioctl
1840 * @arg: arg from ioctl
1842 * This attaches a user specified mode to an connector.
1843 * Called by the user via ioctl.
1846 * Zero on success, errno on failure.
1848 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1849 void *data, struct drm_file *file_priv)
1851 struct drm_mode_mode_cmd *mode_cmd = data;
1852 struct drm_connector *connector;
1853 struct drm_display_mode *mode;
1854 struct drm_mode_object *obj;
1855 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1858 mutex_lock(&dev->mode_config.mutex);
1860 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1865 connector = obj_to_connector(obj);
1867 mode = drm_mode_create(dev);
1873 drm_crtc_convert_umode(mode, umode);
1875 ret = drm_mode_attachmode(dev, connector, mode);
1877 mutex_unlock(&dev->mode_config.mutex);
1883 * drm_fb_detachmode - Detach a user specified mode from an connector
1884 * @inode: inode from the ioctl
1885 * @filp: file * from the ioctl
1886 * @cmd: cmd from ioctl
1887 * @arg: arg from ioctl
1889 * Called by the user via ioctl.
1892 * Zero on success, errno on failure.
1894 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1895 void *data, struct drm_file *file_priv)
1897 struct drm_mode_object *obj;
1898 struct drm_mode_mode_cmd *mode_cmd = data;
1899 struct drm_connector *connector;
1900 struct drm_display_mode mode;
1901 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1904 mutex_lock(&dev->mode_config.mutex);
1906 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1911 connector = obj_to_connector(obj);
1913 drm_crtc_convert_umode(&mode, umode);
1914 ret = drm_mode_detachmode(dev, connector, &mode);
1916 mutex_unlock(&dev->mode_config.mutex);
1920 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1921 const char *name, int num_values)
1923 struct drm_property *property = NULL;
1925 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1930 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1931 if (!property->values)
1935 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1936 property->flags = flags;
1937 property->num_values = num_values;
1938 INIT_LIST_HEAD(&property->enum_blob_list);
1941 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1943 list_add_tail(&property->head, &dev->mode_config.property_list);
1949 EXPORT_SYMBOL(drm_property_create);
1951 int drm_property_add_enum(struct drm_property *property, int index,
1952 uint64_t value, const char *name)
1954 struct drm_property_enum *prop_enum;
1956 if (!(property->flags & DRM_MODE_PROP_ENUM))
1959 if (!list_empty(&property->enum_blob_list)) {
1960 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1961 if (prop_enum->value == value) {
1962 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1963 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1969 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1973 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1974 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1975 prop_enum->value = value;
1977 property->values[index] = value;
1978 list_add_tail(&prop_enum->head, &property->enum_blob_list);
1981 EXPORT_SYMBOL(drm_property_add_enum);
1983 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1985 struct drm_property_enum *prop_enum, *pt;
1987 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1988 list_del(&prop_enum->head);
1992 if (property->num_values)
1993 kfree(property->values);
1994 drm_mode_object_put(dev, &property->base);
1995 list_del(&property->head);
1998 EXPORT_SYMBOL(drm_property_destroy);
2000 int drm_connector_attach_property(struct drm_connector *connector,
2001 struct drm_property *property, uint64_t init_val)
2005 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2006 if (connector->property_ids[i] == 0) {
2007 connector->property_ids[i] = property->base.id;
2008 connector->property_values[i] = init_val;
2013 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2017 EXPORT_SYMBOL(drm_connector_attach_property);
2019 int drm_connector_property_set_value(struct drm_connector *connector,
2020 struct drm_property *property, uint64_t value)
2024 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2025 if (connector->property_ids[i] == property->base.id) {
2026 connector->property_values[i] = value;
2031 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2035 EXPORT_SYMBOL(drm_connector_property_set_value);
2037 int drm_connector_property_get_value(struct drm_connector *connector,
2038 struct drm_property *property, uint64_t *val)
2042 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2043 if (connector->property_ids[i] == property->base.id) {
2044 *val = connector->property_values[i];
2049 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2053 EXPORT_SYMBOL(drm_connector_property_get_value);
2055 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2056 void *data, struct drm_file *file_priv)
2058 struct drm_mode_object *obj;
2059 struct drm_mode_get_property *out_resp = data;
2060 struct drm_property *property;
2063 int value_count = 0;
2066 struct drm_property_enum *prop_enum;
2067 struct drm_mode_property_enum __user *enum_ptr;
2068 struct drm_property_blob *prop_blob;
2069 uint32_t *blob_id_ptr;
2070 uint64_t __user *values_ptr;
2071 uint32_t __user *blob_length_ptr;
2073 mutex_lock(&dev->mode_config.mutex);
2074 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2079 property = obj_to_property(obj);
2081 if (property->flags & DRM_MODE_PROP_ENUM) {
2082 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2084 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2085 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2089 value_count = property->num_values;
2091 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2092 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2093 out_resp->flags = property->flags;
2095 if ((out_resp->count_values >= value_count) && value_count) {
2096 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2097 for (i = 0; i < value_count; i++) {
2098 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2104 out_resp->count_values = value_count;
2106 if (property->flags & DRM_MODE_PROP_ENUM) {
2107 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2109 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2110 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2112 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2117 if (copy_to_user(&enum_ptr[copied].name,
2118 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2125 out_resp->count_enum_blobs = enum_count;
2128 if (property->flags & DRM_MODE_PROP_BLOB) {
2129 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2131 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2132 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2134 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2135 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2140 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2148 out_resp->count_enum_blobs = blob_count;
2151 mutex_unlock(&dev->mode_config.mutex);
2155 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2158 struct drm_property_blob *blob;
2160 if (!length || !data)
2163 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2167 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2168 blob->length = length;
2170 memcpy(blob->data, data, length);
2172 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2174 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2178 static void drm_property_destroy_blob(struct drm_device *dev,
2179 struct drm_property_blob *blob)
2181 drm_mode_object_put(dev, &blob->base);
2182 list_del(&blob->head);
2186 int drm_mode_getblob_ioctl(struct drm_device *dev,
2187 void *data, struct drm_file *file_priv)
2189 struct drm_mode_object *obj;
2190 struct drm_mode_get_blob *out_resp = data;
2191 struct drm_property_blob *blob;
2195 mutex_lock(&dev->mode_config.mutex);
2196 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2201 blob = obj_to_blob(obj);
2203 if (out_resp->length == blob->length) {
2204 blob_ptr = (void *)(unsigned long)out_resp->data;
2205 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2210 out_resp->length = blob->length;
2213 mutex_unlock(&dev->mode_config.mutex);
2217 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2220 struct drm_device *dev = connector->dev;
2223 if (connector->edid_blob_ptr)
2224 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2226 /* Delete edid, when there is none. */
2228 connector->edid_blob_ptr = NULL;
2229 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2233 connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
2235 ret = drm_connector_property_set_value(connector,
2236 dev->mode_config.edid_property,
2237 connector->edid_blob_ptr->base.id);
2241 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2243 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2244 void *data, struct drm_file *file_priv)
2246 struct drm_mode_connector_set_property *out_resp = data;
2247 struct drm_mode_object *obj;
2248 struct drm_property *property;
2249 struct drm_connector *connector;
2253 mutex_lock(&dev->mode_config.mutex);
2255 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2259 connector = obj_to_connector(obj);
2261 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2262 if (connector->property_ids[i] == out_resp->prop_id)
2266 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2270 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2274 property = obj_to_property(obj);
2276 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2279 if (property->flags & DRM_MODE_PROP_RANGE) {
2280 if (out_resp->value < property->values[0])
2283 if (out_resp->value > property->values[1])
2287 for (i = 0; i < property->num_values; i++) {
2288 if (property->values[i] == out_resp->value) {
2298 if (connector->funcs->set_property)
2299 ret = connector->funcs->set_property(connector, property, out_resp->value);
2301 /* store the property value if succesful */
2303 drm_connector_property_set_value(connector, property, out_resp->value);
2305 mutex_unlock(&dev->mode_config.mutex);
2309 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2310 struct drm_encoder *encoder)
2314 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2315 if (connector->encoder_ids[i] == 0) {
2316 connector->encoder_ids[i] = encoder->base.id;
2322 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2324 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2325 struct drm_encoder *encoder)
2328 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2329 if (connector->encoder_ids[i] == encoder->base.id) {
2330 connector->encoder_ids[i] = 0;
2331 if (connector->encoder == encoder)
2332 connector->encoder = NULL;
2337 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2339 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2342 crtc->gamma_size = gamma_size;
2344 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2345 if (!crtc->gamma_store) {
2346 crtc->gamma_size = 0;
2352 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2354 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2355 void *data, struct drm_file *file_priv)
2357 struct drm_mode_crtc_lut *crtc_lut = data;
2358 struct drm_mode_object *obj;
2359 struct drm_crtc *crtc;
2360 void *r_base, *g_base, *b_base;
2364 mutex_lock(&dev->mode_config.mutex);
2365 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2370 crtc = obj_to_crtc(obj);
2372 /* memcpy into gamma store */
2373 if (crtc_lut->gamma_size != crtc->gamma_size) {
2378 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2379 r_base = crtc->gamma_store;
2380 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2385 g_base = r_base + size;
2386 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2391 b_base = g_base + size;
2392 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2397 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2400 mutex_unlock(&dev->mode_config.mutex);
2405 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2406 void *data, struct drm_file *file_priv)
2408 struct drm_mode_crtc_lut *crtc_lut = data;
2409 struct drm_mode_object *obj;
2410 struct drm_crtc *crtc;
2411 void *r_base, *g_base, *b_base;
2415 mutex_lock(&dev->mode_config.mutex);
2416 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2421 crtc = obj_to_crtc(obj);
2423 /* memcpy into gamma store */
2424 if (crtc_lut->gamma_size != crtc->gamma_size) {
2429 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2430 r_base = crtc->gamma_store;
2431 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2436 g_base = r_base + size;
2437 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2442 b_base = g_base + size;
2443 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2448 mutex_unlock(&dev->mode_config.mutex);