2 * Copyright (c) 2006-2007 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 {
45 static struct drm_prop_enum_list drm_dpms_enum_list[] =
46 { { DRM_MODE_DPMS_ON, "On" },
47 { DRM_MODE_DPMS_STANDBY, "Standby" },
48 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
49 { DRM_MODE_DPMS_OFF, "Off" }
52 char *drm_get_dpms_name(int val)
56 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
57 if (drm_dpms_enum_list[i].type == val)
58 return drm_dpms_enum_list[i].name;
66 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
68 { DRM_MODE_SCALE_NON_GPU, "Non-GPU" },
69 { DRM_MODE_SCALE_FULLSCREEN, "Fullscreen" },
70 { DRM_MODE_SCALE_NO_SCALE, "No scale" },
71 { DRM_MODE_SCALE_ASPECT, "Aspect" },
74 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
76 { DRM_MODE_DITHERING_OFF, "Off" },
77 { DRM_MODE_DITHERING_ON, "On" },
81 * Non-global properties, but "required" for certain connectors.
83 static struct drm_prop_enum_list drm_select_subconnector_enum_list[] =
85 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
86 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
87 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
88 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
89 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
90 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
93 char *drm_get_select_subconnector_name(int val)
97 for (i = 0; i < ARRAY_SIZE(drm_select_subconnector_enum_list); i++)
98 if (drm_select_subconnector_enum_list[i].type == val)
99 return drm_select_subconnector_enum_list[i].name;
104 static struct drm_prop_enum_list drm_subconnector_enum_list[] =
106 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
107 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
108 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
109 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
111 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
114 char *drm_get_subconnector_name(int val)
118 for (i = 0; i < ARRAY_SIZE(drm_subconnector_enum_list); i++)
119 if (drm_subconnector_enum_list[i].type == val)
120 return drm_subconnector_enum_list[i].name;
125 struct drm_conn_prop_enum_list {
132 * Connector and encoder types.
134 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
135 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
136 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
137 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
138 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
139 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
140 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
141 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
142 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
143 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
144 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN", 0 },
145 { DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort", 0 },
146 { DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A", 0 },
147 { DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B", 0 },
150 static struct drm_prop_enum_list drm_encoder_enum_list[] =
151 { { DRM_MODE_ENCODER_NONE, "None" },
152 { DRM_MODE_ENCODER_DAC, "DAC" },
153 { DRM_MODE_ENCODER_TMDS, "TMDS" },
154 { DRM_MODE_ENCODER_LVDS, "LVDS" },
155 { DRM_MODE_ENCODER_TVDAC, "TV" },
158 char *drm_get_encoder_name(struct drm_encoder *encoder)
162 snprintf(buf, 32, "%s-%d", drm_encoder_enum_list[encoder->encoder_type].name,
167 char *drm_get_connector_name(struct drm_connector *connector)
171 snprintf(buf, 32, "%s-%d", drm_connector_enum_list[connector->connector_type].name,
172 connector->connector_type_id);
175 EXPORT_SYMBOL(drm_get_connector_name);
177 char *drm_get_connector_status_name(enum drm_connector_status status)
179 if (status == connector_status_connected)
181 else if (status == connector_status_disconnected)
182 return "disconnected";
188 * drm_idr_get - allocate a new identifier
190 * @ptr: object pointer, used to generate unique ID
193 * Caller must hold DRM mode_config lock.
195 * Create a unique identifier based on @ptr in @dev's identifier space. Used
196 * for tracking modes, CRTCs and connectors.
199 * New unique (relative to other objects in @dev) integer identifier for the
202 static int drm_mode_object_get(struct drm_device *dev, struct drm_mode_object *obj, uint32_t obj_type)
207 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
208 DRM_ERROR("Ran out memory getting a mode number\n");
212 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
217 obj->type = obj_type;
222 * drm_mode_object_put - free an identifer
227 * Caller must hold DRM mode_config lock.
229 * Free @id from @dev's unique identifier pool.
231 static void drm_mode_object_put(struct drm_device *dev, struct drm_mode_object *object)
233 idr_remove(&dev->mode_config.crtc_idr, object->id);
236 void *drm_mode_object_find(struct drm_device *dev, uint32_t id, uint32_t type)
238 struct drm_mode_object *obj;
240 obj = idr_find(&dev->mode_config.crtc_idr, id);
241 if (!obj || (obj->type != type) || (obj->id != id))
246 EXPORT_SYMBOL(drm_mode_object_find);
249 * drm_crtc_from_fb - find the CRTC structure associated with an fb
251 * @fb: framebuffer in question
254 * Caller must hold mode_config lock.
256 * Find CRTC in the mode_config structure that matches @fb.
259 * Pointer to the CRTC or NULL if it wasn't found.
261 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
262 struct drm_framebuffer *fb)
264 struct drm_crtc *crtc;
266 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
274 * drm_framebuffer_create - create a new framebuffer object
278 * Caller must hold mode config lock.
280 * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
283 * Pointer to new framebuffer or NULL on error.
285 struct drm_framebuffer *drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
286 const struct drm_framebuffer_funcs *funcs)
288 if(drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB))
293 dev->mode_config.num_fb++;
294 list_add(&fb->head, &dev->mode_config.fb_list);
298 EXPORT_SYMBOL(drm_framebuffer_init);
301 * drm_framebuffer_cleanup - remove a framebuffer object
302 * @fb: framebuffer to remove
305 * Caller must hold mode config lock.
307 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
308 * it, setting it to NULL.
310 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
312 struct drm_device *dev = fb->dev;
313 struct drm_crtc *crtc;
315 /* remove from any CRTC */
316 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
321 drm_mode_object_put(dev, &fb->base);
323 dev->mode_config.num_fb--;
325 EXPORT_SYMBOL(drm_framebuffer_cleanup);
328 * drm_crtc_init - Initialise a new CRTC object
330 * @crtc: CRTC object to init
331 * @funcs: callbacks for the new CRTC
334 * Caller must hold mode config lock.
336 * Inits a new object created as base part of an driver crtc object.
338 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
339 const struct drm_crtc_funcs *funcs)
344 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
346 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
347 dev->mode_config.num_crtc++;
349 EXPORT_SYMBOL(drm_crtc_init);
352 * drm_crtc_cleanup - Cleans up the core crtc usage.
353 * @crtc: CRTC to cleanup
356 * Caller must hold mode config lock.
358 * Cleanup @crtc. Removes from drm modesetting space
359 * does NOT free object, caller does that.
361 void drm_crtc_cleanup(struct drm_crtc *crtc)
363 struct drm_device *dev = crtc->dev;
365 if (crtc->gamma_store) {
366 kfree(crtc->gamma_store);
367 crtc->gamma_store = NULL;
370 drm_mode_object_put(dev, &crtc->base);
371 list_del(&crtc->head);
372 dev->mode_config.num_crtc--;
374 EXPORT_SYMBOL(drm_crtc_cleanup);
377 * drm_mode_probed_add - add a mode to the specified connector's probed mode list
378 * @connector: connector the new mode
382 * Caller must hold mode config lock.
384 * Add @mode to @connector's mode list for later use.
386 void drm_mode_probed_add(struct drm_connector *connector,
387 struct drm_display_mode *mode)
389 list_add(&mode->head, &connector->probed_modes);
391 EXPORT_SYMBOL(drm_mode_probed_add);
394 * drm_mode_remove - remove and free a mode
395 * @connector: connector list to modify
396 * @mode: mode to remove
399 * Caller must hold mode config lock.
401 * Remove @mode from @connector's mode list, then free it.
403 void drm_mode_remove(struct drm_connector *connector, struct drm_display_mode *mode)
405 list_del(&mode->head);
408 EXPORT_SYMBOL(drm_mode_remove);
411 * drm_connector_init - Init a preallocated connector
413 * @connector: the connector to init
414 * @funcs: callbacks for this connector
415 * @name: user visible name of the connector
418 * Caller must hold @dev's mode_config lock.
420 * Initialises a preallocated connector. Connectors should be
421 * subclassed as part of driver connector objects.
423 void drm_connector_init(struct drm_device *dev,
424 struct drm_connector *connector,
425 const struct drm_connector_funcs *funcs,
428 connector->dev = dev;
429 connector->funcs = funcs;
430 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
431 connector->connector_type = connector_type;
432 connector->connector_type_id = ++drm_connector_enum_list[connector_type].count; /* TODO */
433 INIT_LIST_HEAD(&connector->user_modes);
434 INIT_LIST_HEAD(&connector->probed_modes);
435 INIT_LIST_HEAD(&connector->modes);
436 connector->edid_blob_ptr = NULL;
437 /* randr_connector? */
438 /* connector_set_monitor(connector)? */
439 /* check for connector_ignored(connector)? */
441 mutex_lock(&dev->mode_config.mutex);
442 list_add_tail(&connector->head, &dev->mode_config.connector_list);
443 dev->mode_config.num_connector++;
445 drm_connector_attach_property(connector, dev->mode_config.edid_property, 0);
447 drm_connector_attach_property(connector, dev->mode_config.dpms_property, 0);
449 mutex_unlock(&dev->mode_config.mutex);
451 EXPORT_SYMBOL(drm_connector_init);
454 * drm_connector_cleanup - cleans up an initialised connector
455 * @connector: connector to cleanup
458 * Caller must hold @dev's mode_config lock.
460 * Cleans up the connector but doesn't free the object.
462 void drm_connector_cleanup(struct drm_connector *connector)
464 struct drm_device *dev = connector->dev;
465 struct drm_display_mode *mode, *t;
467 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
468 drm_mode_remove(connector, mode);
470 list_for_each_entry_safe(mode, t, &connector->modes, head)
471 drm_mode_remove(connector, mode);
473 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
474 drm_mode_remove(connector, mode);
476 mutex_lock(&dev->mode_config.mutex);
477 drm_mode_object_put(dev, &connector->base);
478 list_del(&connector->head);
479 mutex_unlock(&dev->mode_config.mutex);
481 EXPORT_SYMBOL(drm_connector_cleanup);
483 void drm_encoder_init(struct drm_device *dev,
484 struct drm_encoder *encoder,
485 const struct drm_encoder_funcs *funcs,
490 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
491 encoder->encoder_type = encoder_type;
492 encoder->funcs = funcs;
494 mutex_lock(&dev->mode_config.mutex);
495 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
496 dev->mode_config.num_encoder++;
498 mutex_unlock(&dev->mode_config.mutex);
500 EXPORT_SYMBOL(drm_encoder_init);
502 void drm_encoder_cleanup(struct drm_encoder *encoder)
504 struct drm_device *dev = encoder->dev;
505 mutex_lock(&dev->mode_config.mutex);
506 drm_mode_object_put(dev, &encoder->base);
507 list_del(&encoder->head);
508 mutex_unlock(&dev->mode_config.mutex);
510 EXPORT_SYMBOL(drm_encoder_cleanup);
513 * drm_mode_create - create a new display mode
519 * Create a new drm_display_mode, give it an ID, and return it.
522 * Pointer to new mode on success, NULL on error.
524 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
526 struct drm_display_mode *nmode;
528 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
532 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
535 EXPORT_SYMBOL(drm_mode_create);
538 * drm_mode_destroy - remove a mode
540 * @mode: mode to remove
543 * Caller must hold mode config lock.
545 * Free @mode's unique identifier, then free it.
547 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
549 drm_mode_object_put(dev, &mode->base);
553 EXPORT_SYMBOL(drm_mode_destroy);
555 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
560 * Standard properties (apply to all connectors)
562 dev->mode_config.edid_property =
563 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
566 dev->mode_config.dpms_property =
567 drm_property_create(dev, DRM_MODE_PROP_ENUM,
568 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
569 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
570 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
576 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
579 * Called by a driver the first time a DVI-I connector is made.
581 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
585 if (dev->mode_config.dvi_i_select_subconnector_property)
588 dev->mode_config.dvi_i_select_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM,
589 "select subconnector", 3);
590 /* add enum element 0-2 */
591 for (i = 0; i < 3; i++)
592 drm_property_add_enum(dev->mode_config.dvi_i_select_subconnector_property, i, drm_select_subconnector_enum_list[i].type,
593 drm_select_subconnector_enum_list[i].name);
595 /* This is a property which indicates the most likely thing to be connected. */
596 dev->mode_config.dvi_i_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
598 /* add enum element 0-2 */
599 for (i = 0; i < 3; i++)
600 drm_property_add_enum(dev->mode_config.dvi_i_subconnector_property, i, drm_subconnector_enum_list[i].type,
601 drm_subconnector_enum_list[i].name);
605 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
608 * drm_create_tv_properties - create TV specific connector properties
610 * @num_modes: number of different TV formats (modes) supported
611 * @modes: array of pointers to strings containing name of each format
613 * Called by a driver's TV initialization routine, this function creates
614 * the TV specific connector properties for a given device. Caller is
615 * responsible for allocating a list of format names and passing them to
618 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
623 if (dev->mode_config.tv_select_subconnector_property) /* already done */
626 dev->mode_config.tv_select_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM,
627 "select subconnector", 4);
628 /* add enum element 0 */
629 drm_property_add_enum(dev->mode_config.tv_select_subconnector_property, 0, drm_select_subconnector_enum_list[0].type,
630 drm_select_subconnector_enum_list[0].name);
631 /* add enum element 3-5 */
632 for (i = 1; i < 4; i++)
633 drm_property_add_enum(dev->mode_config.tv_select_subconnector_property, i, drm_select_subconnector_enum_list[i + 2].type,
634 drm_select_subconnector_enum_list[i + 2].name);
636 /* This is a property which indicates the most likely thing to be connected. */
637 dev->mode_config.tv_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
639 /* add enum element 0 */
640 drm_property_add_enum(dev->mode_config.tv_subconnector_property, 0, drm_subconnector_enum_list[0].type,
641 drm_subconnector_enum_list[0].name);
642 /* add enum element 3-5 */
643 for (i = 1; i < 4; i++)
644 drm_property_add_enum(dev->mode_config.tv_subconnector_property, i, drm_subconnector_enum_list[i + 2].type,
645 drm_subconnector_enum_list[i + 2].name);
647 dev->mode_config.tv_left_margin_property =
648 drm_property_create(dev, DRM_MODE_PROP_RANGE,
650 dev->mode_config.tv_left_margin_property->values[0] = 0;
651 dev->mode_config.tv_left_margin_property->values[1] = 100;
653 dev->mode_config.tv_right_margin_property =
654 drm_property_create(dev, DRM_MODE_PROP_RANGE,
656 dev->mode_config.tv_right_margin_property->values[0] = 0;
657 dev->mode_config.tv_right_margin_property->values[1] = 100;
659 dev->mode_config.tv_top_margin_property =
660 drm_property_create(dev, DRM_MODE_PROP_RANGE,
662 dev->mode_config.tv_top_margin_property->values[0] = 0;
663 dev->mode_config.tv_top_margin_property->values[1] = 100;
665 dev->mode_config.tv_bottom_margin_property =
666 drm_property_create(dev, DRM_MODE_PROP_RANGE,
668 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
669 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
671 dev->mode_config.tv_mode_property =
672 drm_property_create(dev, DRM_MODE_PROP_ENUM,
674 for (i = 0; i < num_modes; i++)
675 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
680 EXPORT_SYMBOL(drm_mode_create_tv_properties);
683 * drm_mode_create_scaling_mode_property - create scaling mode property
686 * Called by a driver the first time it's needed, must be attached to desired connectors.
688 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
692 if (dev->mode_config.scaling_mode_property) /* already done */
695 dev->mode_config.scaling_mode_property =
696 drm_property_create(dev, DRM_MODE_PROP_ENUM,
697 "scaling mode", ARRAY_SIZE(drm_scaling_mode_enum_list));
698 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
699 drm_property_add_enum(dev->mode_config.scaling_mode_property, i, drm_scaling_mode_enum_list[i].type, drm_scaling_mode_enum_list[i].name);
703 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
706 * drm_mode_create_dithering_property - create dithering property
709 * Called by a driver the first time it's needed, must be attached to desired connectors.
711 int drm_mode_create_dithering_property(struct drm_device *dev)
715 if (dev->mode_config.dithering_mode_property) /* already done */
718 dev->mode_config.dithering_mode_property =
719 drm_property_create(dev, DRM_MODE_PROP_ENUM,
720 "dithering", ARRAY_SIZE(drm_dithering_mode_enum_list));
721 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
722 drm_property_add_enum(dev->mode_config.dithering_mode_property, i, drm_dithering_mode_enum_list[i].type, drm_dithering_mode_enum_list[i].name);
726 EXPORT_SYMBOL(drm_mode_create_dithering_property);
729 * drm_mode_config_init - initialize DRM mode_configuration structure
733 * None, should happen single threaded at init time.
735 * Initialize @dev's mode_config structure, used for tracking the graphics
736 * configuration of @dev.
738 void drm_mode_config_init(struct drm_device *dev)
740 mutex_init(&dev->mode_config.mutex);
741 INIT_LIST_HEAD(&dev->mode_config.fb_list);
742 INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
743 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
744 INIT_LIST_HEAD(&dev->mode_config.connector_list);
745 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
746 INIT_LIST_HEAD(&dev->mode_config.property_list);
747 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
748 idr_init(&dev->mode_config.crtc_idr);
750 drm_mode_create_standard_connector_properties(dev);
752 /* Just to be sure */
753 dev->mode_config.num_fb = 0;
754 dev->mode_config.num_connector = 0;
755 dev->mode_config.num_crtc = 0;
756 dev->mode_config.num_encoder = 0;
757 dev->mode_config.hotplug_counter = 0;
759 EXPORT_SYMBOL(drm_mode_config_init);
761 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
763 uint32_t total_objects = 0;
765 total_objects += dev->mode_config.num_crtc;
766 total_objects += dev->mode_config.num_connector;
767 total_objects += dev->mode_config.num_encoder;
769 if (total_objects == 0)
772 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
776 group->num_crtcs = 0;
777 group->num_connectors = 0;
778 group->num_encoders = 0;
782 int drm_mode_group_init_legacy_group(struct drm_device *dev, struct drm_mode_group *group)
784 struct drm_crtc *crtc;
785 struct drm_encoder *encoder;
786 struct drm_connector *connector;
789 if ((ret = drm_mode_group_init(dev, group)))
792 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
793 group->id_list[group->num_crtcs++] = crtc->base.id;
795 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
796 group->id_list[group->num_crtcs + group->num_encoders++] = encoder->base.id;
798 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
799 group->id_list[group->num_crtcs + group->num_encoders + group->num_connectors++] = connector->base.id;
805 * drm_mode_config_cleanup - free up DRM mode_config info
809 * Caller must hold mode config lock.
811 * Free up all the connectors and CRTCs associated with this DRM device, then
812 * free up the framebuffers and associated buffer objects.
814 * FIXME: cleanup any dangling user buffer objects too
816 void drm_mode_config_cleanup(struct drm_device *dev)
818 struct drm_connector *connector, *ot;
819 struct drm_crtc *crtc, *ct;
820 struct drm_encoder *encoder, *enct;
821 struct drm_framebuffer *fb, *fbt;
822 struct drm_property *property, *pt;
824 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list, head) {
825 encoder->funcs->destroy(encoder);
828 list_for_each_entry_safe(connector, ot, &dev->mode_config.connector_list, head) {
829 connector->funcs->destroy(connector);
832 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
833 drm_property_destroy(dev, property);
836 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
837 fb->funcs->destroy(fb);
840 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
841 crtc->funcs->destroy(crtc);
845 EXPORT_SYMBOL(drm_mode_config_cleanup);
848 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
849 * @out: drm_mode_modeinfo struct to return to the user
850 * @in: drm_display_mode to use
855 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
858 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
860 out->clock = in->clock;
861 out->hdisplay = in->hdisplay;
862 out->hsync_start = in->hsync_start;
863 out->hsync_end = in->hsync_end;
864 out->htotal = in->htotal;
865 out->hskew = in->hskew;
866 out->vdisplay = in->vdisplay;
867 out->vsync_start = in->vsync_start;
868 out->vsync_end = in->vsync_end;
869 out->vtotal = in->vtotal;
870 out->vscan = in->vscan;
871 out->vrefresh = in->vrefresh;
872 out->flags = in->flags;
873 out->type = in->type;
874 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
875 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
879 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
880 * @out: drm_display_mode to return to the user
881 * @in: drm_mode_modeinfo to use
886 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
889 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
891 out->clock = in->clock;
892 out->hdisplay = in->hdisplay;
893 out->hsync_start = in->hsync_start;
894 out->hsync_end = in->hsync_end;
895 out->htotal = in->htotal;
896 out->hskew = in->hskew;
897 out->vdisplay = in->vdisplay;
898 out->vsync_start = in->vsync_start;
899 out->vsync_end = in->vsync_end;
900 out->vtotal = in->vtotal;
901 out->vscan = in->vscan;
902 out->vrefresh = in->vrefresh;
903 out->flags = in->flags;
904 out->type = in->type;
905 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
906 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
910 * drm_mode_getresources - get graphics configuration
911 * @inode: inode from the ioctl
912 * @filp: file * from the ioctl
913 * @cmd: cmd from ioctl
914 * @arg: arg from ioctl
917 * Takes mode config lock.
919 * Construct a set of configuration description structures and return
920 * them to the user, including CRTC, connector and framebuffer configuration.
922 * Called by the user via ioctl.
925 * Zero on success, errno on failure.
927 int drm_mode_getresources(struct drm_device *dev,
928 void *data, struct drm_file *file_priv)
930 struct drm_mode_card_res *card_res = data;
931 struct list_head *lh;
932 struct drm_framebuffer *fb;
933 struct drm_connector *connector;
934 struct drm_crtc *crtc;
935 struct drm_encoder *encoder;
937 int connector_count = 0;
940 int encoder_count = 0;
942 uint32_t __user *fb_id;
943 uint32_t __user *crtc_id;
944 uint32_t __user *connector_id;
945 uint32_t __user *encoder_id;
946 struct drm_mode_group *mode_group;
948 mutex_lock(&dev->mode_config.mutex);
950 /* for the non-control nodes we need to limit the list of resources by IDs in the
951 group list for this node */
952 list_for_each(lh, &file_priv->fbs)
955 mode_group = &file_priv->master->minor->mode_group;
956 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
958 list_for_each(lh, &dev->mode_config.crtc_list)
961 list_for_each(lh, &dev->mode_config.connector_list)
964 list_for_each(lh, &dev->mode_config.encoder_list)
968 crtc_count = mode_group->num_crtcs;
969 connector_count = mode_group->num_connectors;
970 encoder_count = mode_group->num_encoders;
973 card_res->max_height = dev->mode_config.max_height;
974 card_res->min_height = dev->mode_config.min_height;
975 card_res->max_width = dev->mode_config.max_width;
976 card_res->min_width = dev->mode_config.min_width;
978 /* handle this in 4 parts */
980 if (card_res->count_fbs >= fb_count) {
982 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
983 list_for_each_entry(fb, &file_priv->fbs, head) {
984 if (put_user(fb->base.id, fb_id + copied)) {
991 card_res->count_fbs = fb_count;
994 if (card_res->count_crtcs >= crtc_count) {
996 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
997 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
998 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
999 DRM_DEBUG("CRTC ID is %d\n", crtc->base.id);
1000 if (put_user(crtc->base.id, crtc_id + copied)) {
1007 for (i = 0; i < mode_group->num_crtcs; i++) {
1008 if (put_user(mode_group->id_list[i], crtc_id + copied)) {
1016 card_res->count_crtcs = crtc_count;
1019 if (card_res->count_encoders >= encoder_count) {
1021 encoder_id = (uint32_t *)(unsigned long)card_res->encoder_id_ptr;
1022 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1023 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
1025 DRM_DEBUG("ENCODER ID is %d\n", encoder->base.id);
1026 if (put_user(encoder->base.id, encoder_id + copied)) {
1033 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1034 if (put_user(mode_group->id_list[i], encoder_id + copied)) {
1043 card_res->count_encoders = encoder_count;
1046 if (card_res->count_connectors >= connector_count) {
1048 connector_id = (uint32_t *)(unsigned long)card_res->connector_id_ptr;
1049 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1050 list_for_each_entry(connector, &dev->mode_config.connector_list,
1052 DRM_DEBUG("CONNECTOR ID is %d\n", connector->base.id);
1053 if (put_user(connector->base.id, connector_id + copied)) {
1060 int start = mode_group->num_crtcs + mode_group->num_encoders;
1061 for (i = start; i < start + mode_group->num_connectors; i++) {
1062 if (put_user(mode_group->id_list[i], connector_id + copied)) {
1070 card_res->count_connectors = connector_count;
1072 DRM_DEBUG("Counted %d %d %d\n", card_res->count_crtcs,
1073 card_res->count_connectors, card_res->count_encoders);
1076 mutex_unlock(&dev->mode_config.mutex);
1081 * drm_mode_getcrtc - get CRTC configuration
1082 * @inode: inode from the ioctl
1083 * @filp: file * from the ioctl
1084 * @cmd: cmd from ioctl
1085 * @arg: arg from ioctl
1090 * Construct a CRTC configuration structure to return to the user.
1092 * Called by the user via ioctl.
1095 * Zero on success, errno on failure.
1097 int drm_mode_getcrtc(struct drm_device *dev,
1098 void *data, struct drm_file *file_priv)
1100 struct drm_mode_crtc *crtc_resp = data;
1101 struct drm_crtc *crtc;
1102 struct drm_mode_object *obj;
1105 mutex_lock(&dev->mode_config.mutex);
1107 obj = drm_mode_object_find(dev, crtc_resp->crtc_id, DRM_MODE_OBJECT_CRTC);
1112 crtc = obj_to_crtc(obj);
1114 crtc_resp->x = crtc->x;
1115 crtc_resp->y = crtc->y;
1116 crtc_resp->gamma_size = crtc->gamma_size;
1118 crtc_resp->fb_id = crtc->fb->base.id;
1120 crtc_resp->fb_id = 0;
1122 if (crtc->enabled) {
1124 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1125 crtc_resp->mode_valid = 1;
1128 crtc_resp->mode_valid = 0;
1132 mutex_unlock(&dev->mode_config.mutex);
1137 * drm_mode_getconnector - get connector configuration
1138 * @inode: inode from the ioctl
1139 * @filp: file * from the ioctl
1140 * @cmd: cmd from ioctl
1141 * @arg: arg from ioctl
1146 * Construct a connector configuration structure to return to the user.
1148 * Called by the user via ioctl.
1151 * Zero on success, errno on failure.
1153 int drm_mode_getconnector(struct drm_device *dev,
1154 void *data, struct drm_file *file_priv)
1156 struct drm_mode_get_connector *out_resp = data;
1157 struct drm_mode_object *obj;
1158 struct drm_connector *connector;
1159 struct drm_display_mode *mode;
1161 int props_count = 0;
1162 int encoders_count = 0;
1166 struct drm_mode_modeinfo u_mode;
1167 struct drm_mode_modeinfo __user *mode_ptr;
1168 uint32_t __user *prop_ptr;
1169 uint64_t __user *prop_values;
1170 uint32_t __user *encoder_ptr;
1172 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1174 DRM_DEBUG("connector id %d:\n", out_resp->connector_id);
1176 mutex_lock(&dev->mode_config.mutex);
1178 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1183 connector = obj_to_connector(obj);
1185 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1186 if (connector->property_ids[i] != 0) {
1191 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1192 if (connector->encoder_ids[i] != 0) {
1197 if (out_resp->count_modes == 0) {
1198 connector->funcs->fill_modes(connector, dev->mode_config.max_width, dev->mode_config.max_height);
1201 /* delayed so we get modes regardless of pre-fill_modes state */
1202 list_for_each_entry(mode, &connector->modes, head)
1205 out_resp->connector_id = connector->base.id;
1206 out_resp->connector_type = connector->connector_type;
1207 out_resp->connector_type_id = connector->connector_type_id;
1208 out_resp->mm_width = connector->display_info.width_mm;
1209 out_resp->mm_height = connector->display_info.height_mm;
1210 out_resp->subpixel = connector->display_info.subpixel_order;
1211 out_resp->connection = connector->status;
1212 if (connector->encoder)
1213 out_resp->encoder_id = connector->encoder->base.id;
1215 out_resp->encoder_id = 0;
1217 /* this ioctl is called twice, once to determine how much space is needed, and the 2nd time to fill it */
1218 if ((out_resp->count_modes >= mode_count) && mode_count) {
1220 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1221 list_for_each_entry(mode, &connector->modes, head) {
1222 drm_crtc_convert_to_umode(&u_mode, mode);
1223 if (copy_to_user(mode_ptr + copied,
1224 &u_mode, sizeof(u_mode))) {
1231 out_resp->count_modes = mode_count;
1233 if ((out_resp->count_props >= props_count) && props_count) {
1235 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1236 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1237 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1238 if (connector->property_ids[i] != 0) {
1239 if (put_user(connector->property_ids[i], prop_ptr + copied)) {
1244 if (put_user(connector->property_values[i], prop_values + copied)) {
1252 out_resp->count_props = props_count;
1254 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1256 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1257 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1258 if (connector->encoder_ids[i] != 0) {
1259 if (put_user(connector->encoder_ids[i], encoder_ptr + copied)) {
1267 out_resp->count_encoders = encoders_count;
1270 mutex_unlock(&dev->mode_config.mutex);
1274 int drm_mode_getencoder(struct drm_device *dev,
1275 void *data, struct drm_file *file_priv)
1277 struct drm_mode_get_encoder *enc_resp = data;
1278 struct drm_mode_object *obj;
1279 struct drm_encoder *encoder;
1282 mutex_lock(&dev->mode_config.mutex);
1283 obj = drm_mode_object_find(dev, enc_resp->encoder_id, DRM_MODE_OBJECT_ENCODER);
1288 encoder = obj_to_encoder(obj);
1291 enc_resp->crtc_id = encoder->crtc->base.id;
1293 enc_resp->crtc_id = 0;
1294 enc_resp->encoder_type = encoder->encoder_type;
1295 enc_resp->encoder_id = encoder->base.id;
1296 enc_resp->possible_crtcs = encoder->possible_crtcs;
1297 enc_resp->possible_clones = encoder->possible_clones;
1300 mutex_unlock(&dev->mode_config.mutex);
1305 * drm_mode_setcrtc - set CRTC configuration
1306 * @inode: inode from the ioctl
1307 * @filp: file * from the ioctl
1308 * @cmd: cmd from ioctl
1309 * @arg: arg from ioctl
1314 * Build a new CRTC configuration based on user request.
1316 * Called by the user via ioctl.
1319 * Zero on success, errno on failure.
1321 int drm_mode_setcrtc(struct drm_device *dev,
1322 void *data, struct drm_file *file_priv)
1324 struct drm_mode_crtc *crtc_req = data;
1325 struct drm_mode_object *obj;
1326 struct drm_crtc *crtc, *crtcfb;
1327 struct drm_connector **connector_set = NULL, *connector;
1328 struct drm_framebuffer *fb = NULL;
1329 struct drm_display_mode *mode = NULL;
1330 struct drm_mode_set set;
1331 uint32_t __user *set_connectors_ptr;
1335 mutex_lock(&dev->mode_config.mutex);
1336 obj = drm_mode_object_find(dev, crtc_req->crtc_id, DRM_MODE_OBJECT_CRTC);
1338 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1342 crtc = obj_to_crtc(obj);
1344 if (crtc_req->mode_valid) {
1345 /* If we have a mode we need a framebuffer. */
1346 /* If we pass -1, set the mode with the currently bound fb */
1347 if (crtc_req->fb_id == -1) {
1348 list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1349 if (crtcfb == crtc) {
1350 DRM_DEBUG("Using current fb for setmode\n");
1355 obj = drm_mode_object_find(dev, crtc_req->fb_id, DRM_MODE_OBJECT_FB);
1357 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1361 fb = obj_to_fb(obj);
1364 mode = drm_mode_create(dev);
1365 drm_crtc_convert_umode(mode, &crtc_req->mode);
1366 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1369 if (crtc_req->count_connectors == 0 && mode) {
1370 DRM_DEBUG("Count connectors is 0 but mode set\n");
1375 if (crtc_req->count_connectors > 0 && !mode && !fb) {
1376 DRM_DEBUG("Count connectors is %d but no mode or fb set\n", crtc_req->count_connectors);
1381 if (crtc_req->count_connectors > 0) {
1383 /* Maybe we should check that count_connectors is a sensible value. */
1384 connector_set = kmalloc(crtc_req->count_connectors *
1385 sizeof(struct drm_connector *), GFP_KERNEL);
1386 if (!connector_set) {
1391 for (i = 0; i < crtc_req->count_connectors; i++) {
1392 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1393 if (get_user(out_id, &set_connectors_ptr[i])) {
1398 obj = drm_mode_object_find(dev, out_id, DRM_MODE_OBJECT_CONNECTOR);
1400 DRM_DEBUG("Connector id %d unknown\n", out_id);
1404 connector = obj_to_connector(obj);
1406 connector_set[i] = connector;
1411 set.x = crtc_req->x;
1412 set.y = crtc_req->y;
1414 set.connectors = connector_set;
1415 set.num_connectors = crtc_req->count_connectors;
1417 ret = crtc->funcs->set_config(&set);
1420 kfree(connector_set);
1421 mutex_unlock(&dev->mode_config.mutex);
1425 int drm_mode_cursor_ioctl(struct drm_device *dev,
1426 void *data, struct drm_file *file_priv)
1428 struct drm_mode_cursor *req = data;
1429 struct drm_mode_object *obj;
1430 struct drm_crtc *crtc;
1436 DRM_ERROR("no operation set\n");
1440 mutex_lock(&dev->mode_config.mutex);
1441 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1443 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc_id);
1447 crtc = obj_to_crtc(obj);
1449 if (req->flags & DRM_MODE_CURSOR_BO) {
1450 /* Turn of the cursor if handle is 0 */
1451 if (crtc->funcs->cursor_set) {
1452 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle, req->width, req->height);
1454 DRM_ERROR("crtc does not support cursor\n");
1460 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1461 if (crtc->funcs->cursor_move) {
1462 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1464 DRM_ERROR("crtc does not support cursor\n");
1470 mutex_unlock(&dev->mode_config.mutex);
1475 * drm_mode_addfb - add an FB to the graphics configuration
1476 * @inode: inode from the ioctl
1477 * @filp: file * from the ioctl
1478 * @cmd: cmd from ioctl
1479 * @arg: arg from ioctl
1482 * Takes mode config lock.
1484 * Add a new FB to the specified CRTC, given a user request.
1486 * Called by the user via ioctl.
1489 * Zero on success, errno on failure.
1491 int drm_mode_addfb(struct drm_device *dev,
1492 void *data, struct drm_file *file_priv)
1494 struct drm_mode_fb_cmd *r = data;
1495 struct drm_mode_config *config = &dev->mode_config;
1496 struct drm_framebuffer *fb;
1499 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1500 DRM_ERROR("mode new framebuffer width not within limits\n");
1503 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1504 DRM_ERROR("mode new framebuffer height not within limits\n");
1508 mutex_lock(&dev->mode_config.mutex);
1510 /* TODO check buffer is sufficently large */
1511 /* TODO setup destructor callback */
1513 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1515 DRM_ERROR("could not create framebuffer\n");
1520 r->fb_id = fb->base.id;
1521 list_add(&fb->filp_head, &file_priv->fbs);
1524 mutex_unlock(&dev->mode_config.mutex);
1529 * drm_mode_rmfb - remove an FB from the configuration
1530 * @inode: inode from the ioctl
1531 * @filp: file * from the ioctl
1532 * @cmd: cmd from ioctl
1533 * @arg: arg from ioctl
1536 * Takes mode config lock.
1538 * Remove the FB specified by the user.
1540 * Called by the user via ioctl.
1543 * Zero on success, errno on failure.
1545 int drm_mode_rmfb(struct drm_device *dev,
1546 void *data, struct drm_file *file_priv)
1548 struct drm_mode_object *obj;
1549 struct drm_framebuffer *fb = NULL;
1550 struct drm_framebuffer *fbl = NULL;
1551 uint32_t *id = data;
1555 mutex_lock(&dev->mode_config.mutex);
1556 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1557 /* TODO check that we realy get a framebuffer back. */
1559 DRM_ERROR("mode invalid framebuffer id\n");
1563 fb = obj_to_fb(obj);
1565 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1570 DRM_ERROR("tried to remove a fb that we didn't own\n");
1575 /* TODO release all crtc connected to the framebuffer */
1576 /* TODO unhock the destructor from the buffer object */
1578 list_del(&fb->filp_head);
1579 fb->funcs->destroy(fb);
1582 mutex_unlock(&dev->mode_config.mutex);
1587 * drm_mode_getfb - get FB info
1588 * @inode: inode from the ioctl
1589 * @filp: file * from the ioctl
1590 * @cmd: cmd from ioctl
1591 * @arg: arg from ioctl
1596 * Lookup the FB given its ID and return info about it.
1598 * Called by the user via ioctl.
1601 * Zero on success, errno on failure.
1603 int drm_mode_getfb(struct drm_device *dev,
1604 void *data, struct drm_file *file_priv)
1606 struct drm_mode_fb_cmd *r = data;
1607 struct drm_mode_object *obj;
1608 struct drm_framebuffer *fb;
1611 mutex_lock(&dev->mode_config.mutex);
1612 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1614 DRM_ERROR("invalid framebuffer id\n");
1618 fb = obj_to_fb(obj);
1620 r->height = fb->height;
1621 r->width = fb->width;
1622 r->depth = fb->depth;
1623 r->bpp = fb->bits_per_pixel;
1624 r->pitch = fb->pitch;
1625 fb->funcs->create_handle(fb, file_priv, &r->handle);
1628 mutex_unlock(&dev->mode_config.mutex);
1633 * drm_fb_release - remove and free the FBs on this file
1634 * @filp: file * from the ioctl
1637 * Takes mode config lock.
1639 * Destroy all the FBs associated with @filp.
1641 * Called by the user via ioctl.
1644 * Zero on success, errno on failure.
1646 void drm_fb_release(struct file *filp)
1648 struct drm_file *priv = filp->private_data;
1649 struct drm_device *dev = priv->minor->dev;
1650 struct drm_framebuffer *fb, *tfb;
1652 mutex_lock(&dev->mode_config.mutex);
1653 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1654 list_del(&fb->filp_head);
1655 fb->funcs->destroy(fb);
1657 mutex_unlock(&dev->mode_config.mutex);
1664 static int drm_mode_attachmode(struct drm_device *dev,
1665 struct drm_connector *connector,
1666 struct drm_display_mode *mode)
1670 list_add_tail(&mode->head, &connector->user_modes);
1674 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1675 struct drm_display_mode *mode)
1677 struct drm_connector *connector;
1679 struct drm_display_mode *dup_mode;
1681 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1682 if (!connector->encoder)
1684 if (connector->encoder->crtc == crtc) {
1686 dup_mode = drm_mode_duplicate(dev, mode);
1689 ret = drm_mode_attachmode(dev, connector, dup_mode);
1697 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1699 static int drm_mode_detachmode(struct drm_device *dev,
1700 struct drm_connector *connector,
1701 struct drm_display_mode *mode)
1705 struct drm_display_mode *match_mode, *t;
1707 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1708 if (drm_mode_equal(match_mode, mode)) {
1709 list_del(&match_mode->head);
1710 drm_mode_destroy(dev, match_mode);
1722 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1724 struct drm_connector *connector;
1726 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1727 drm_mode_detachmode(dev, connector, mode);
1731 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1734 * drm_fb_attachmode - Attach a user mode to an connector
1735 * @inode: inode from the ioctl
1736 * @filp: file * from the ioctl
1737 * @cmd: cmd from ioctl
1738 * @arg: arg from ioctl
1740 * This attaches a user specified mode to an connector.
1741 * Called by the user via ioctl.
1744 * Zero on success, errno on failure.
1746 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1747 void *data, struct drm_file *file_priv)
1749 struct drm_mode_mode_cmd *mode_cmd = data;
1750 struct drm_connector *connector;
1751 struct drm_display_mode *mode;
1752 struct drm_mode_object *obj;
1753 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1756 mutex_lock(&dev->mode_config.mutex);
1758 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1763 connector = obj_to_connector(obj);
1765 mode = drm_mode_create(dev);
1771 drm_crtc_convert_umode(mode, umode);
1773 ret = drm_mode_attachmode(dev, connector, mode);
1775 mutex_unlock(&dev->mode_config.mutex);
1781 * drm_fb_detachmode - Detach a user specified mode from an connector
1782 * @inode: inode from the ioctl
1783 * @filp: file * from the ioctl
1784 * @cmd: cmd from ioctl
1785 * @arg: arg from ioctl
1787 * Called by the user via ioctl.
1790 * Zero on success, errno on failure.
1792 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1793 void *data, struct drm_file *file_priv)
1795 struct drm_mode_object *obj;
1796 struct drm_mode_mode_cmd *mode_cmd = data;
1797 struct drm_connector *connector;
1798 struct drm_display_mode mode;
1799 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1802 mutex_lock(&dev->mode_config.mutex);
1804 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1809 connector = obj_to_connector(obj);
1811 drm_crtc_convert_umode(&mode, umode);
1812 ret = drm_mode_detachmode(dev, connector, &mode);
1814 mutex_unlock(&dev->mode_config.mutex);
1818 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1819 const char *name, int num_values)
1821 struct drm_property *property = NULL;
1823 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1828 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1829 if (!property->values)
1833 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1834 property->flags = flags;
1835 property->num_values = num_values;
1836 INIT_LIST_HEAD(&property->enum_blob_list);
1839 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1841 list_add_tail(&property->head, &dev->mode_config.property_list);
1847 EXPORT_SYMBOL(drm_property_create);
1849 int drm_property_add_enum(struct drm_property *property, int index,
1850 uint64_t value, const char *name)
1852 struct drm_property_enum *prop_enum;
1854 if (!(property->flags & DRM_MODE_PROP_ENUM))
1857 if (!list_empty(&property->enum_blob_list)) {
1858 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1859 if (prop_enum->value == value) {
1860 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1861 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1867 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1871 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1872 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1873 prop_enum->value = value;
1875 property->values[index] = value;
1876 list_add_tail(&prop_enum->head, &property->enum_blob_list);
1879 EXPORT_SYMBOL(drm_property_add_enum);
1881 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1883 struct drm_property_enum *prop_enum, *pt;
1885 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1886 list_del(&prop_enum->head);
1890 if (property->num_values)
1891 kfree(property->values);
1892 drm_mode_object_put(dev, &property->base);
1893 list_del(&property->head);
1896 EXPORT_SYMBOL(drm_property_destroy);
1898 int drm_connector_attach_property(struct drm_connector *connector,
1899 struct drm_property *property, uint64_t init_val)
1903 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1904 if (connector->property_ids[i] == 0) {
1905 connector->property_ids[i] = property->base.id;
1906 connector->property_values[i] = init_val;
1911 if (i == DRM_CONNECTOR_MAX_PROPERTY)
1915 EXPORT_SYMBOL(drm_connector_attach_property);
1917 int drm_connector_property_set_value(struct drm_connector *connector,
1918 struct drm_property *property, uint64_t value)
1922 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1923 if (connector->property_ids[i] == property->base.id) {
1924 connector->property_values[i] = value;
1929 if (i == DRM_CONNECTOR_MAX_PROPERTY)
1933 EXPORT_SYMBOL(drm_connector_property_set_value);
1935 int drm_connector_property_get_value(struct drm_connector *connector,
1936 struct drm_property *property, uint64_t *val)
1940 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1941 if (connector->property_ids[i] == property->base.id) {
1942 *val = connector->property_values[i];
1947 if (i == DRM_CONNECTOR_MAX_PROPERTY)
1951 EXPORT_SYMBOL(drm_connector_property_get_value);
1953 int drm_mode_getproperty_ioctl(struct drm_device *dev,
1954 void *data, struct drm_file *file_priv)
1956 struct drm_mode_object *obj;
1957 struct drm_mode_get_property *out_resp = data;
1958 struct drm_property *property;
1961 int value_count = 0;
1964 struct drm_property_enum *prop_enum;
1965 struct drm_mode_property_enum __user *enum_ptr;
1966 struct drm_property_blob *prop_blob;
1967 uint32_t *blob_id_ptr;
1968 uint64_t __user *values_ptr;
1969 uint32_t __user *blob_length_ptr;
1971 mutex_lock(&dev->mode_config.mutex);
1972 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
1977 property = obj_to_property(obj);
1979 if (property->flags & DRM_MODE_PROP_ENUM) {
1980 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
1982 } else if (property->flags & DRM_MODE_PROP_BLOB) {
1983 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
1987 value_count = property->num_values;
1989 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
1990 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
1991 out_resp->flags = property->flags;
1993 if ((out_resp->count_values >= value_count) && value_count) {
1994 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
1995 for (i = 0; i < value_count; i++) {
1996 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2002 out_resp->count_values = value_count;
2004 if (property->flags & DRM_MODE_PROP_ENUM) {
2005 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2007 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2008 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2010 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2015 if (copy_to_user(&enum_ptr[copied].name,
2016 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2023 out_resp->count_enum_blobs = enum_count;
2026 if (property->flags & DRM_MODE_PROP_BLOB) {
2027 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2029 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2030 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2032 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2033 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2038 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2046 out_resp->count_enum_blobs = blob_count;
2049 mutex_unlock(&dev->mode_config.mutex);
2053 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2056 struct drm_property_blob *blob;
2058 if (!length || !data)
2061 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2065 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2066 blob->length = length;
2068 memcpy(blob->data, data, length);
2070 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2072 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2076 static void drm_property_destroy_blob(struct drm_device *dev,
2077 struct drm_property_blob *blob)
2079 drm_mode_object_put(dev, &blob->base);
2080 list_del(&blob->head);
2084 int drm_mode_getblob_ioctl(struct drm_device *dev,
2085 void *data, struct drm_file *file_priv)
2087 struct drm_mode_object *obj;
2088 struct drm_mode_get_blob *out_resp = data;
2089 struct drm_property_blob *blob;
2093 mutex_lock(&dev->mode_config.mutex);
2094 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2099 blob = obj_to_blob(obj);
2101 if (out_resp->length == blob->length) {
2102 blob_ptr = (void *)(unsigned long)out_resp->data;
2103 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2108 out_resp->length = blob->length;
2111 mutex_unlock(&dev->mode_config.mutex);
2115 int drm_mode_connector_update_edid_property(struct drm_connector *connector, struct edid *edid)
2117 struct drm_device *dev = connector->dev;
2119 if (connector->edid_blob_ptr)
2120 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2122 /* Delete edid, when there is none. */
2124 connector->edid_blob_ptr = NULL;
2125 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2129 connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
2131 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, connector->edid_blob_ptr->base.id);
2134 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2136 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2137 void *data, struct drm_file *file_priv)
2139 struct drm_mode_connector_set_property *out_resp = data;
2140 struct drm_mode_object *obj;
2141 struct drm_property *property;
2142 struct drm_connector *connector;
2146 mutex_lock(&dev->mode_config.mutex);
2148 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2152 connector = obj_to_connector(obj);
2154 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2155 if (connector->property_ids[i] == out_resp->prop_id)
2159 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2163 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2167 property = obj_to_property(obj);
2169 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2172 if (property->flags & DRM_MODE_PROP_RANGE) {
2173 if (out_resp->value < property->values[0])
2176 if (out_resp->value > property->values[1])
2180 for (i = 0; i < property->num_values; i++) {
2181 if (property->values[i] == out_resp->value) {
2191 if (connector->funcs->set_property)
2192 ret = connector->funcs->set_property(connector, property, out_resp->value);
2194 /* store the property value if succesful */
2196 drm_connector_property_set_value(connector, property, out_resp->value);
2198 mutex_unlock(&dev->mode_config.mutex);
2203 int drm_mode_replacefb(struct drm_device *dev,
2204 void *data, struct drm_file *file_priv)
2206 struct drm_mode_fb_cmd *r = data;
2207 struct drm_mode_object *obj;
2208 struct drm_framebuffer *fb;
2210 struct drm_framebuffer *fbl = NULL;
2213 /* right replace the current bo attached to this fb with a new bo */
2214 mutex_lock(&dev->mode_config.mutex);
2215 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2220 fb = obj_to_fb(obj);
2222 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2227 DRM_ERROR("tried to replace an fb we didn't own\n");
2232 if (dev->mode_config.funcs->resize_fb)
2233 ret = dev->mode_config.funcs->resize_fb(dev, file_priv, fb, r);
2237 mutex_unlock(&dev->mode_config.mutex);
2242 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2243 struct drm_encoder *encoder)
2247 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2248 if (connector->encoder_ids[i] == 0) {
2249 connector->encoder_ids[i] = encoder->base.id;
2255 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2257 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2258 struct drm_encoder *encoder)
2261 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2262 if (connector->encoder_ids[i] == encoder->base.id) {
2263 connector->encoder_ids[i] = 0;
2264 if (connector->encoder == encoder)
2265 connector->encoder = NULL;
2270 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2272 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2275 crtc->gamma_size = gamma_size;
2277 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2278 if (!crtc->gamma_store) {
2279 crtc->gamma_size = 0;
2285 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2287 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2288 void *data, struct drm_file *file_priv)
2290 struct drm_mode_crtc_lut *crtc_lut = data;
2291 struct drm_mode_object *obj;
2292 struct drm_crtc *crtc;
2293 void *r_base, *g_base, *b_base;
2297 mutex_lock(&dev->mode_config.mutex);
2298 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2303 crtc = obj_to_crtc(obj);
2305 /* memcpy into gamma store */
2306 if (crtc_lut->gamma_size != crtc->gamma_size) {
2311 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2312 r_base = crtc->gamma_store;
2313 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2318 g_base = r_base + size;
2319 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2324 b_base = g_base + size;
2325 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2330 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2333 mutex_unlock(&dev->mode_config.mutex);
2338 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2339 void *data, struct drm_file *file_priv)
2341 struct drm_mode_crtc_lut *crtc_lut = data;
2342 struct drm_mode_object *obj;
2343 struct drm_crtc *crtc;
2344 void *r_base, *g_base, *b_base;
2348 mutex_lock(&dev->mode_config.mutex);
2349 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2354 crtc = obj_to_crtc(obj);
2356 /* memcpy into gamma store */
2357 if (crtc_lut->gamma_size != crtc->gamma_size) {
2362 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2363 r_base = crtc->gamma_store;
2364 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2369 g_base = r_base + size;
2370 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2375 b_base = g_base + size;
2376 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2381 mutex_unlock(&dev->mode_config.mutex);