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 drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
291 dev->mode_config.num_fb++;
292 list_add(&fb->head, &dev->mode_config.fb_list);
296 EXPORT_SYMBOL(drm_framebuffer_init);
299 * drm_framebuffer_cleanup - remove a framebuffer object
300 * @fb: framebuffer to remove
303 * Caller must hold mode config lock.
305 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
306 * it, setting it to NULL.
308 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
310 struct drm_device *dev = fb->dev;
311 struct drm_crtc *crtc;
313 /* remove from any CRTC */
314 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
319 drm_mode_object_put(dev, &fb->base);
321 dev->mode_config.num_fb--;
323 EXPORT_SYMBOL(drm_framebuffer_cleanup);
326 * drm_crtc_init - Initialise a new CRTC object
328 * @crtc: CRTC object to init
329 * @funcs: callbacks for the new CRTC
332 * Caller must hold mode config lock.
334 * Inits a new object created as base part of an driver crtc object.
336 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
337 const struct drm_crtc_funcs *funcs)
342 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
344 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
345 dev->mode_config.num_crtc++;
347 EXPORT_SYMBOL(drm_crtc_init);
350 * drm_crtc_cleanup - Cleans up the core crtc usage.
351 * @crtc: CRTC to cleanup
354 * Caller must hold mode config lock.
356 * Cleanup @crtc. Removes from drm modesetting space
357 * does NOT free object, caller does that.
359 void drm_crtc_cleanup(struct drm_crtc *crtc)
361 struct drm_device *dev = crtc->dev;
363 if (crtc->gamma_store) {
364 kfree(crtc->gamma_store);
365 crtc->gamma_store = NULL;
368 drm_mode_object_put(dev, &crtc->base);
369 list_del(&crtc->head);
370 dev->mode_config.num_crtc--;
372 EXPORT_SYMBOL(drm_crtc_cleanup);
375 * drm_mode_probed_add - add a mode to the specified connector's probed mode list
376 * @connector: connector the new mode
380 * Caller must hold mode config lock.
382 * Add @mode to @connector's mode list for later use.
384 void drm_mode_probed_add(struct drm_connector *connector,
385 struct drm_display_mode *mode)
387 list_add(&mode->head, &connector->probed_modes);
389 EXPORT_SYMBOL(drm_mode_probed_add);
392 * drm_mode_remove - remove and free a mode
393 * @connector: connector list to modify
394 * @mode: mode to remove
397 * Caller must hold mode config lock.
399 * Remove @mode from @connector's mode list, then free it.
401 void drm_mode_remove(struct drm_connector *connector, struct drm_display_mode *mode)
403 list_del(&mode->head);
406 EXPORT_SYMBOL(drm_mode_remove);
409 * drm_connector_init - Init a preallocated connector
411 * @connector: the connector to init
412 * @funcs: callbacks for this connector
413 * @name: user visible name of the connector
416 * Caller must hold @dev's mode_config lock.
418 * Initialises a preallocated connector. Connectors should be
419 * subclassed as part of driver connector objects.
421 void drm_connector_init(struct drm_device *dev,
422 struct drm_connector *connector,
423 const struct drm_connector_funcs *funcs,
426 connector->dev = dev;
427 connector->funcs = funcs;
428 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
429 connector->connector_type = connector_type;
430 connector->connector_type_id = ++drm_connector_enum_list[connector_type].count; /* TODO */
431 INIT_LIST_HEAD(&connector->user_modes);
432 INIT_LIST_HEAD(&connector->probed_modes);
433 INIT_LIST_HEAD(&connector->modes);
434 connector->edid_blob_ptr = NULL;
435 /* randr_connector? */
436 /* connector_set_monitor(connector)? */
437 /* check for connector_ignored(connector)? */
439 mutex_lock(&dev->mode_config.mutex);
440 list_add_tail(&connector->head, &dev->mode_config.connector_list);
441 dev->mode_config.num_connector++;
443 drm_connector_attach_property(connector, dev->mode_config.edid_property, 0);
445 drm_connector_attach_property(connector, dev->mode_config.dpms_property, 0);
447 mutex_unlock(&dev->mode_config.mutex);
449 EXPORT_SYMBOL(drm_connector_init);
452 * drm_connector_cleanup - cleans up an initialised connector
453 * @connector: connector to cleanup
456 * Caller must hold @dev's mode_config lock.
458 * Cleans up the connector but doesn't free the object.
460 void drm_connector_cleanup(struct drm_connector *connector)
462 struct drm_device *dev = connector->dev;
463 struct drm_display_mode *mode, *t;
465 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
466 drm_mode_remove(connector, mode);
468 list_for_each_entry_safe(mode, t, &connector->modes, head)
469 drm_mode_remove(connector, mode);
471 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
472 drm_mode_remove(connector, mode);
474 mutex_lock(&dev->mode_config.mutex);
475 drm_mode_object_put(dev, &connector->base);
476 list_del(&connector->head);
477 mutex_unlock(&dev->mode_config.mutex);
479 EXPORT_SYMBOL(drm_connector_cleanup);
481 void drm_encoder_init(struct drm_device *dev,
482 struct drm_encoder *encoder,
483 const struct drm_encoder_funcs *funcs,
488 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
489 encoder->encoder_type = encoder_type;
490 encoder->funcs = funcs;
492 mutex_lock(&dev->mode_config.mutex);
493 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
494 dev->mode_config.num_encoder++;
496 mutex_unlock(&dev->mode_config.mutex);
498 EXPORT_SYMBOL(drm_encoder_init);
500 void drm_encoder_cleanup(struct drm_encoder *encoder)
502 struct drm_device *dev = encoder->dev;
503 mutex_lock(&dev->mode_config.mutex);
504 drm_mode_object_put(dev, &encoder->base);
505 list_del(&encoder->head);
506 mutex_unlock(&dev->mode_config.mutex);
508 EXPORT_SYMBOL(drm_encoder_cleanup);
511 * drm_mode_create - create a new display mode
517 * Create a new drm_display_mode, give it an ID, and return it.
520 * Pointer to new mode on success, NULL on error.
522 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
524 struct drm_display_mode *nmode;
526 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
530 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
533 EXPORT_SYMBOL(drm_mode_create);
536 * drm_mode_destroy - remove a mode
538 * @mode: mode to remove
541 * Caller must hold mode config lock.
543 * Free @mode's unique identifier, then free it.
545 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
547 drm_mode_object_put(dev, &mode->base);
551 EXPORT_SYMBOL(drm_mode_destroy);
553 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
558 * Standard properties (apply to all connectors)
560 dev->mode_config.edid_property =
561 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
564 dev->mode_config.dpms_property =
565 drm_property_create(dev, DRM_MODE_PROP_ENUM,
566 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
567 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
568 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
574 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
577 * Called by a driver the first time a DVI-I connector is made.
579 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
583 if (dev->mode_config.dvi_i_select_subconnector_property)
586 dev->mode_config.dvi_i_select_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM,
587 "select subconnector", 3);
588 /* add enum element 0-2 */
589 for (i = 0; i < 3; i++)
590 drm_property_add_enum(dev->mode_config.dvi_i_select_subconnector_property, i, drm_select_subconnector_enum_list[i].type,
591 drm_select_subconnector_enum_list[i].name);
593 /* This is a property which indicates the most likely thing to be connected. */
594 dev->mode_config.dvi_i_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
596 /* add enum element 0-2 */
597 for (i = 0; i < 3; i++)
598 drm_property_add_enum(dev->mode_config.dvi_i_subconnector_property, i, drm_subconnector_enum_list[i].type,
599 drm_subconnector_enum_list[i].name);
603 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
606 * drm_create_tv_properties - create TV specific connector properties
608 * @num_modes: number of different TV formats (modes) supported
609 * @modes: array of pointers to strings containing name of each format
611 * Called by a driver's TV initialization routine, this function creates
612 * the TV specific connector properties for a given device. Caller is
613 * responsible for allocating a list of format names and passing them to
616 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
621 if (dev->mode_config.tv_select_subconnector_property) /* already done */
624 dev->mode_config.tv_select_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM,
625 "select subconnector", 4);
626 /* add enum element 0 */
627 drm_property_add_enum(dev->mode_config.tv_select_subconnector_property, 0, drm_select_subconnector_enum_list[0].type,
628 drm_select_subconnector_enum_list[0].name);
629 /* add enum element 3-5 */
630 for (i = 1; i < 4; i++)
631 drm_property_add_enum(dev->mode_config.tv_select_subconnector_property, i, drm_select_subconnector_enum_list[i + 2].type,
632 drm_select_subconnector_enum_list[i + 2].name);
634 /* This is a property which indicates the most likely thing to be connected. */
635 dev->mode_config.tv_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
637 /* add enum element 0 */
638 drm_property_add_enum(dev->mode_config.tv_subconnector_property, 0, drm_subconnector_enum_list[0].type,
639 drm_subconnector_enum_list[0].name);
640 /* add enum element 3-5 */
641 for (i = 1; i < 4; i++)
642 drm_property_add_enum(dev->mode_config.tv_subconnector_property, i, drm_subconnector_enum_list[i + 2].type,
643 drm_subconnector_enum_list[i + 2].name);
645 dev->mode_config.tv_left_margin_property =
646 drm_property_create(dev, DRM_MODE_PROP_RANGE,
648 dev->mode_config.tv_left_margin_property->values[0] = 0;
649 dev->mode_config.tv_left_margin_property->values[1] = 100;
651 dev->mode_config.tv_right_margin_property =
652 drm_property_create(dev, DRM_MODE_PROP_RANGE,
654 dev->mode_config.tv_right_margin_property->values[0] = 0;
655 dev->mode_config.tv_right_margin_property->values[1] = 100;
657 dev->mode_config.tv_top_margin_property =
658 drm_property_create(dev, DRM_MODE_PROP_RANGE,
660 dev->mode_config.tv_top_margin_property->values[0] = 0;
661 dev->mode_config.tv_top_margin_property->values[1] = 100;
663 dev->mode_config.tv_bottom_margin_property =
664 drm_property_create(dev, DRM_MODE_PROP_RANGE,
666 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
667 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
669 dev->mode_config.tv_mode_property =
670 drm_property_create(dev, DRM_MODE_PROP_ENUM,
672 for (i = 0; i < num_modes; i++)
673 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
678 EXPORT_SYMBOL(drm_mode_create_tv_properties);
681 * drm_mode_create_scaling_mode_property - create scaling mode property
684 * Called by a driver the first time it's needed, must be attached to desired connectors.
686 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
690 if (dev->mode_config.scaling_mode_property) /* already done */
693 dev->mode_config.scaling_mode_property =
694 drm_property_create(dev, DRM_MODE_PROP_ENUM,
695 "scaling mode", ARRAY_SIZE(drm_scaling_mode_enum_list));
696 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
697 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);
701 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
704 * drm_mode_create_dithering_property - create dithering property
707 * Called by a driver the first time it's needed, must be attached to desired connectors.
709 int drm_mode_create_dithering_property(struct drm_device *dev)
713 if (dev->mode_config.dithering_mode_property) /* already done */
716 dev->mode_config.dithering_mode_property =
717 drm_property_create(dev, DRM_MODE_PROP_ENUM,
718 "dithering", ARRAY_SIZE(drm_dithering_mode_enum_list));
719 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
720 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);
724 EXPORT_SYMBOL(drm_mode_create_dithering_property);
727 * drm_mode_config_init - initialize DRM mode_configuration structure
731 * None, should happen single threaded at init time.
733 * Initialize @dev's mode_config structure, used for tracking the graphics
734 * configuration of @dev.
736 void drm_mode_config_init(struct drm_device *dev)
738 mutex_init(&dev->mode_config.mutex);
739 INIT_LIST_HEAD(&dev->mode_config.fb_list);
740 INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
741 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
742 INIT_LIST_HEAD(&dev->mode_config.connector_list);
743 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
744 INIT_LIST_HEAD(&dev->mode_config.property_list);
745 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
746 idr_init(&dev->mode_config.crtc_idr);
748 drm_mode_create_standard_connector_properties(dev);
750 /* Just to be sure */
751 dev->mode_config.num_fb = 0;
752 dev->mode_config.num_connector = 0;
753 dev->mode_config.num_crtc = 0;
754 dev->mode_config.num_encoder = 0;
755 dev->mode_config.hotplug_counter = 0;
757 EXPORT_SYMBOL(drm_mode_config_init);
759 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
761 uint32_t total_objects = 0;
763 total_objects += dev->mode_config.num_crtc;
764 total_objects += dev->mode_config.num_connector;
765 total_objects += dev->mode_config.num_encoder;
767 if (total_objects == 0)
770 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
774 group->num_crtcs = 0;
775 group->num_connectors = 0;
776 group->num_encoders = 0;
780 int drm_mode_group_init_legacy_group(struct drm_device *dev, struct drm_mode_group *group)
782 struct drm_crtc *crtc;
783 struct drm_encoder *encoder;
784 struct drm_connector *connector;
787 if ((ret = drm_mode_group_init(dev, group)))
790 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
791 group->id_list[group->num_crtcs++] = crtc->base.id;
793 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
794 group->id_list[group->num_crtcs + group->num_encoders++] = encoder->base.id;
796 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
797 group->id_list[group->num_crtcs + group->num_encoders + group->num_connectors++] = connector->base.id;
803 * drm_mode_config_cleanup - free up DRM mode_config info
807 * Caller must hold mode config lock.
809 * Free up all the connectors and CRTCs associated with this DRM device, then
810 * free up the framebuffers and associated buffer objects.
812 * FIXME: cleanup any dangling user buffer objects too
814 void drm_mode_config_cleanup(struct drm_device *dev)
816 struct drm_connector *connector, *ot;
817 struct drm_crtc *crtc, *ct;
818 struct drm_encoder *encoder, *enct;
819 struct drm_framebuffer *fb, *fbt;
820 struct drm_property *property, *pt;
822 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list, head) {
823 encoder->funcs->destroy(encoder);
826 list_for_each_entry_safe(connector, ot, &dev->mode_config.connector_list, head) {
827 connector->funcs->destroy(connector);
830 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
831 drm_property_destroy(dev, property);
834 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
835 fb->funcs->destroy(fb);
838 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
839 crtc->funcs->destroy(crtc);
843 EXPORT_SYMBOL(drm_mode_config_cleanup);
845 int drm_mode_hotplug_ioctl(struct drm_device *dev,
846 void *data, struct drm_file *file_priv)
848 struct drm_mode_hotplug *arg = data;
850 arg->counter = dev->mode_config.hotplug_counter;
856 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
857 * @out: drm_mode_modeinfo struct to return to the user
858 * @in: drm_display_mode to use
863 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
866 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
868 out->clock = in->clock;
869 out->hdisplay = in->hdisplay;
870 out->hsync_start = in->hsync_start;
871 out->hsync_end = in->hsync_end;
872 out->htotal = in->htotal;
873 out->hskew = in->hskew;
874 out->vdisplay = in->vdisplay;
875 out->vsync_start = in->vsync_start;
876 out->vsync_end = in->vsync_end;
877 out->vtotal = in->vtotal;
878 out->vscan = in->vscan;
879 out->vrefresh = in->vrefresh;
880 out->flags = in->flags;
881 out->type = in->type;
882 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
883 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
887 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
888 * @out: drm_display_mode to return to the user
889 * @in: drm_mode_modeinfo to use
894 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
897 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
899 out->clock = in->clock;
900 out->hdisplay = in->hdisplay;
901 out->hsync_start = in->hsync_start;
902 out->hsync_end = in->hsync_end;
903 out->htotal = in->htotal;
904 out->hskew = in->hskew;
905 out->vdisplay = in->vdisplay;
906 out->vsync_start = in->vsync_start;
907 out->vsync_end = in->vsync_end;
908 out->vtotal = in->vtotal;
909 out->vscan = in->vscan;
910 out->vrefresh = in->vrefresh;
911 out->flags = in->flags;
912 out->type = in->type;
913 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
914 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
918 * drm_mode_getresources - get graphics configuration
919 * @inode: inode from the ioctl
920 * @filp: file * from the ioctl
921 * @cmd: cmd from ioctl
922 * @arg: arg from ioctl
925 * Takes mode config lock.
927 * Construct a set of configuration description structures and return
928 * them to the user, including CRTC, connector and framebuffer configuration.
930 * Called by the user via ioctl.
933 * Zero on success, errno on failure.
935 int drm_mode_getresources(struct drm_device *dev,
936 void *data, struct drm_file *file_priv)
938 struct drm_mode_card_res *card_res = data;
939 struct list_head *lh;
940 struct drm_framebuffer *fb;
941 struct drm_connector *connector;
942 struct drm_crtc *crtc;
943 struct drm_encoder *encoder;
945 int connector_count = 0;
948 int encoder_count = 0;
950 uint32_t __user *fb_id;
951 uint32_t __user *crtc_id;
952 uint32_t __user *connector_id;
953 uint32_t __user *encoder_id;
954 struct drm_mode_group *mode_group;
956 mutex_lock(&dev->mode_config.mutex);
958 /* for the non-control nodes we need to limit the list of resources by IDs in the
959 group list for this node */
960 list_for_each(lh, &file_priv->fbs)
963 mode_group = &file_priv->master->minor->mode_group;
964 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
966 list_for_each(lh, &dev->mode_config.crtc_list)
969 list_for_each(lh, &dev->mode_config.connector_list)
972 list_for_each(lh, &dev->mode_config.encoder_list)
976 crtc_count = mode_group->num_crtcs;
977 connector_count = mode_group->num_connectors;
978 encoder_count = mode_group->num_encoders;
981 card_res->max_height = dev->mode_config.max_height;
982 card_res->min_height = dev->mode_config.min_height;
983 card_res->max_width = dev->mode_config.max_width;
984 card_res->min_width = dev->mode_config.min_width;
986 /* handle this in 4 parts */
988 if (card_res->count_fbs >= fb_count) {
990 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
991 list_for_each_entry(fb, &file_priv->fbs, head) {
992 if (put_user(fb->base.id, fb_id + copied)) {
999 card_res->count_fbs = fb_count;
1002 if (card_res->count_crtcs >= crtc_count) {
1004 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1005 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1006 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1007 DRM_DEBUG("CRTC ID is %d\n", crtc->base.id);
1008 if (put_user(crtc->base.id, crtc_id + copied)) {
1015 for (i = 0; i < mode_group->num_crtcs; i++) {
1016 if (put_user(mode_group->id_list[i], crtc_id + copied)) {
1024 card_res->count_crtcs = crtc_count;
1027 if (card_res->count_encoders >= encoder_count) {
1029 encoder_id = (uint32_t *)(unsigned long)card_res->encoder_id_ptr;
1030 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1031 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
1033 DRM_DEBUG("ENCODER ID is %d\n", encoder->base.id);
1034 if (put_user(encoder->base.id, encoder_id + copied)) {
1041 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1042 if (put_user(mode_group->id_list[i], encoder_id + copied)) {
1051 card_res->count_encoders = encoder_count;
1054 if (card_res->count_connectors >= connector_count) {
1056 connector_id = (uint32_t *)(unsigned long)card_res->connector_id_ptr;
1057 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1058 list_for_each_entry(connector, &dev->mode_config.connector_list,
1060 DRM_DEBUG("CONNECTOR ID is %d\n", connector->base.id);
1061 if (put_user(connector->base.id, connector_id + copied)) {
1068 int start = mode_group->num_crtcs + mode_group->num_encoders;
1069 for (i = start; i < start + mode_group->num_connectors; i++) {
1070 if (put_user(mode_group->id_list[i], connector_id + copied)) {
1078 card_res->count_connectors = connector_count;
1080 DRM_DEBUG("Counted %d %d %d\n", card_res->count_crtcs,
1081 card_res->count_connectors, card_res->count_encoders);
1084 mutex_unlock(&dev->mode_config.mutex);
1089 * drm_mode_getcrtc - get CRTC configuration
1090 * @inode: inode from the ioctl
1091 * @filp: file * from the ioctl
1092 * @cmd: cmd from ioctl
1093 * @arg: arg from ioctl
1098 * Construct a CRTC configuration structure to return to the user.
1100 * Called by the user via ioctl.
1103 * Zero on success, errno on failure.
1105 int drm_mode_getcrtc(struct drm_device *dev,
1106 void *data, struct drm_file *file_priv)
1108 struct drm_mode_crtc *crtc_resp = data;
1109 struct drm_crtc *crtc;
1110 struct drm_mode_object *obj;
1113 mutex_lock(&dev->mode_config.mutex);
1115 obj = drm_mode_object_find(dev, crtc_resp->crtc_id, DRM_MODE_OBJECT_CRTC);
1120 crtc = obj_to_crtc(obj);
1122 crtc_resp->x = crtc->x;
1123 crtc_resp->y = crtc->y;
1124 crtc_resp->gamma_size = crtc->gamma_size;
1126 crtc_resp->fb_id = crtc->fb->base.id;
1128 crtc_resp->fb_id = 0;
1130 if (crtc->enabled) {
1132 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1133 crtc_resp->mode_valid = 1;
1136 crtc_resp->mode_valid = 0;
1140 mutex_unlock(&dev->mode_config.mutex);
1145 * drm_mode_getconnector - get connector configuration
1146 * @inode: inode from the ioctl
1147 * @filp: file * from the ioctl
1148 * @cmd: cmd from ioctl
1149 * @arg: arg from ioctl
1154 * Construct a connector configuration structure to return to the user.
1156 * Called by the user via ioctl.
1159 * Zero on success, errno on failure.
1161 int drm_mode_getconnector(struct drm_device *dev,
1162 void *data, struct drm_file *file_priv)
1164 struct drm_mode_get_connector *out_resp = data;
1165 struct drm_mode_object *obj;
1166 struct drm_connector *connector;
1167 struct drm_display_mode *mode;
1169 int props_count = 0;
1170 int encoders_count = 0;
1174 struct drm_mode_modeinfo u_mode;
1175 struct drm_mode_modeinfo __user *mode_ptr;
1176 uint32_t __user *prop_ptr;
1177 uint64_t __user *prop_values;
1178 uint32_t __user *encoder_ptr;
1180 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1182 DRM_DEBUG("connector id %d:\n", out_resp->connector_id);
1184 mutex_lock(&dev->mode_config.mutex);
1186 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1191 connector = obj_to_connector(obj);
1193 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1194 if (connector->property_ids[i] != 0) {
1199 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1200 if (connector->encoder_ids[i] != 0) {
1205 if (out_resp->count_modes == 0) {
1206 connector->funcs->fill_modes(connector, dev->mode_config.max_width, dev->mode_config.max_height);
1209 /* delayed so we get modes regardless of pre-fill_modes state */
1210 list_for_each_entry(mode, &connector->modes, head)
1213 out_resp->connector_id = connector->base.id;
1214 out_resp->connector_type = connector->connector_type;
1215 out_resp->connector_type_id = connector->connector_type_id;
1216 out_resp->mm_width = connector->display_info.width_mm;
1217 out_resp->mm_height = connector->display_info.height_mm;
1218 out_resp->subpixel = connector->display_info.subpixel_order;
1219 out_resp->connection = connector->status;
1220 if (connector->encoder)
1221 out_resp->encoder_id = connector->encoder->base.id;
1223 out_resp->encoder_id = 0;
1225 /* this ioctl is called twice, once to determine how much space is needed, and the 2nd time to fill it */
1226 if ((out_resp->count_modes >= mode_count) && mode_count) {
1228 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1229 list_for_each_entry(mode, &connector->modes, head) {
1230 drm_crtc_convert_to_umode(&u_mode, mode);
1231 if (copy_to_user(mode_ptr + copied,
1232 &u_mode, sizeof(u_mode))) {
1239 out_resp->count_modes = mode_count;
1241 if ((out_resp->count_props >= props_count) && props_count) {
1243 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1244 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1245 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1246 if (connector->property_ids[i] != 0) {
1247 if (put_user(connector->property_ids[i], prop_ptr + copied)) {
1252 if (put_user(connector->property_values[i], prop_values + copied)) {
1260 out_resp->count_props = props_count;
1262 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1264 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1265 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1266 if (connector->encoder_ids[i] != 0) {
1267 if (put_user(connector->encoder_ids[i], encoder_ptr + copied)) {
1275 out_resp->count_encoders = encoders_count;
1278 mutex_unlock(&dev->mode_config.mutex);
1282 int drm_mode_getencoder(struct drm_device *dev,
1283 void *data, struct drm_file *file_priv)
1285 struct drm_mode_get_encoder *enc_resp = data;
1286 struct drm_mode_object *obj;
1287 struct drm_encoder *encoder;
1290 mutex_lock(&dev->mode_config.mutex);
1291 obj = drm_mode_object_find(dev, enc_resp->encoder_id, DRM_MODE_OBJECT_ENCODER);
1296 encoder = obj_to_encoder(obj);
1299 enc_resp->crtc_id = encoder->crtc->base.id;
1301 enc_resp->crtc_id = 0;
1302 enc_resp->encoder_type = encoder->encoder_type;
1303 enc_resp->encoder_id = encoder->base.id;
1304 enc_resp->possible_crtcs = encoder->possible_crtcs;
1305 enc_resp->possible_clones = encoder->possible_clones;
1308 mutex_unlock(&dev->mode_config.mutex);
1313 * drm_mode_setcrtc - set CRTC configuration
1314 * @inode: inode from the ioctl
1315 * @filp: file * from the ioctl
1316 * @cmd: cmd from ioctl
1317 * @arg: arg from ioctl
1322 * Build a new CRTC configuration based on user request.
1324 * Called by the user via ioctl.
1327 * Zero on success, errno on failure.
1329 int drm_mode_setcrtc(struct drm_device *dev,
1330 void *data, struct drm_file *file_priv)
1332 struct drm_mode_crtc *crtc_req = data;
1333 struct drm_mode_object *obj;
1334 struct drm_crtc *crtc, *crtcfb;
1335 struct drm_connector **connector_set = NULL, *connector;
1336 struct drm_framebuffer *fb = NULL;
1337 struct drm_display_mode *mode = NULL;
1338 struct drm_mode_set set;
1339 uint32_t __user *set_connectors_ptr;
1343 mutex_lock(&dev->mode_config.mutex);
1344 obj = drm_mode_object_find(dev, crtc_req->crtc_id, DRM_MODE_OBJECT_CRTC);
1346 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1350 crtc = obj_to_crtc(obj);
1352 if (crtc_req->mode_valid) {
1353 /* If we have a mode we need a framebuffer. */
1354 /* If we pass -1, set the mode with the currently bound fb */
1355 if (crtc_req->fb_id == -1) {
1356 list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1357 if (crtcfb == crtc) {
1358 DRM_DEBUG("Using current fb for setmode\n");
1363 obj = drm_mode_object_find(dev, crtc_req->fb_id, DRM_MODE_OBJECT_FB);
1365 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1369 fb = obj_to_fb(obj);
1372 mode = drm_mode_create(dev);
1373 drm_crtc_convert_umode(mode, &crtc_req->mode);
1374 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1377 if (crtc_req->count_connectors == 0 && mode) {
1378 DRM_DEBUG("Count connectors is 0 but mode set\n");
1383 if (crtc_req->count_connectors > 0 && !mode && !fb) {
1384 DRM_DEBUG("Count connectors is %d but no mode or fb set\n", crtc_req->count_connectors);
1389 if (crtc_req->count_connectors > 0) {
1391 /* Maybe we should check that count_connectors is a sensible value. */
1392 connector_set = kmalloc(crtc_req->count_connectors *
1393 sizeof(struct drm_connector *), GFP_KERNEL);
1394 if (!connector_set) {
1399 for (i = 0; i < crtc_req->count_connectors; i++) {
1400 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1401 if (get_user(out_id, &set_connectors_ptr[i])) {
1406 obj = drm_mode_object_find(dev, out_id, DRM_MODE_OBJECT_CONNECTOR);
1408 DRM_DEBUG("Connector id %d unknown\n", out_id);
1412 connector = obj_to_connector(obj);
1414 connector_set[i] = connector;
1419 set.x = crtc_req->x;
1420 set.y = crtc_req->y;
1422 set.connectors = connector_set;
1423 set.num_connectors = crtc_req->count_connectors;
1425 ret = crtc->funcs->set_config(&set);
1428 kfree(connector_set);
1429 mutex_unlock(&dev->mode_config.mutex);
1433 int drm_mode_cursor_ioctl(struct drm_device *dev,
1434 void *data, struct drm_file *file_priv)
1436 struct drm_mode_cursor *req = data;
1437 struct drm_mode_object *obj;
1438 struct drm_crtc *crtc;
1444 DRM_ERROR("no operation set\n");
1448 mutex_lock(&dev->mode_config.mutex);
1449 obj = drm_mode_object_find(dev, req->crtc, DRM_MODE_OBJECT_CRTC);
1451 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1455 crtc = obj_to_crtc(obj);
1457 if (req->flags & DRM_MODE_CURSOR_BO) {
1458 /* Turn of the cursor if handle is 0 */
1459 if (crtc->funcs->cursor_set) {
1460 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle, req->width, req->height);
1462 DRM_ERROR("crtc does not support cursor\n");
1468 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1469 if (crtc->funcs->cursor_move) {
1470 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1472 DRM_ERROR("crtc does not support cursor\n");
1478 mutex_unlock(&dev->mode_config.mutex);
1483 * drm_mode_addfb - add an FB to the graphics configuration
1484 * @inode: inode from the ioctl
1485 * @filp: file * from the ioctl
1486 * @cmd: cmd from ioctl
1487 * @arg: arg from ioctl
1490 * Takes mode config lock.
1492 * Add a new FB to the specified CRTC, given a user request.
1494 * Called by the user via ioctl.
1497 * Zero on success, errno on failure.
1499 int drm_mode_addfb(struct drm_device *dev,
1500 void *data, struct drm_file *file_priv)
1502 struct drm_mode_fb_cmd *r = data;
1503 struct drm_mode_config *config = &dev->mode_config;
1504 struct drm_framebuffer *fb;
1507 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1508 DRM_ERROR("mode new framebuffer width not within limits\n");
1511 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1512 DRM_ERROR("mode new framebuffer height not within limits\n");
1516 mutex_lock(&dev->mode_config.mutex);
1518 /* TODO check buffer is sufficently large */
1519 /* TODO setup destructor callback */
1521 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1523 DRM_ERROR("could not create framebuffer\n");
1528 r->buffer_id = fb->base.id;
1529 list_add(&fb->filp_head, &file_priv->fbs);
1532 mutex_unlock(&dev->mode_config.mutex);
1537 * drm_mode_rmfb - remove an FB from the configuration
1538 * @inode: inode from the ioctl
1539 * @filp: file * from the ioctl
1540 * @cmd: cmd from ioctl
1541 * @arg: arg from ioctl
1544 * Takes mode config lock.
1546 * Remove the FB specified by the user.
1548 * Called by the user via ioctl.
1551 * Zero on success, errno on failure.
1553 int drm_mode_rmfb(struct drm_device *dev,
1554 void *data, struct drm_file *file_priv)
1556 struct drm_mode_object *obj;
1557 struct drm_framebuffer *fb = NULL;
1558 struct drm_framebuffer *fbl = NULL;
1559 uint32_t *id = data;
1563 mutex_lock(&dev->mode_config.mutex);
1564 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1565 /* TODO check that we realy get a framebuffer back. */
1567 DRM_ERROR("mode invalid framebuffer id\n");
1571 fb = obj_to_fb(obj);
1573 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1578 DRM_ERROR("tried to remove a fb that we didn't own\n");
1583 /* TODO release all crtc connected to the framebuffer */
1584 /* TODO unhock the destructor from the buffer object */
1586 list_del(&fb->filp_head);
1587 fb->funcs->destroy(fb);
1590 mutex_unlock(&dev->mode_config.mutex);
1595 * drm_mode_getfb - get FB info
1596 * @inode: inode from the ioctl
1597 * @filp: file * from the ioctl
1598 * @cmd: cmd from ioctl
1599 * @arg: arg from ioctl
1604 * Lookup the FB given its ID and return info about it.
1606 * Called by the user via ioctl.
1609 * Zero on success, errno on failure.
1611 int drm_mode_getfb(struct drm_device *dev,
1612 void *data, struct drm_file *file_priv)
1614 struct drm_mode_fb_cmd *r = data;
1615 struct drm_mode_object *obj;
1616 struct drm_framebuffer *fb;
1619 mutex_lock(&dev->mode_config.mutex);
1620 obj = drm_mode_object_find(dev, r->buffer_id, DRM_MODE_OBJECT_FB);
1622 DRM_ERROR("invalid framebuffer id\n");
1626 fb = obj_to_fb(obj);
1628 r->height = fb->height;
1629 r->width = fb->width;
1630 r->depth = fb->depth;
1631 r->bpp = fb->bits_per_pixel;
1632 r->handle = fb->mm_handle;
1633 r->pitch = fb->pitch;
1636 mutex_unlock(&dev->mode_config.mutex);
1641 * drm_fb_release - remove and free the FBs on this file
1642 * @filp: file * from the ioctl
1645 * Takes mode config lock.
1647 * Destroy all the FBs associated with @filp.
1649 * Called by the user via ioctl.
1652 * Zero on success, errno on failure.
1654 void drm_fb_release(struct file *filp)
1656 struct drm_file *priv = filp->private_data;
1657 struct drm_device *dev = priv->minor->dev;
1658 struct drm_framebuffer *fb, *tfb;
1660 mutex_lock(&dev->mode_config.mutex);
1661 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1662 list_del(&fb->filp_head);
1663 fb->funcs->destroy(fb);
1665 mutex_unlock(&dev->mode_config.mutex);
1672 static int drm_mode_attachmode(struct drm_device *dev,
1673 struct drm_connector *connector,
1674 struct drm_display_mode *mode)
1678 list_add_tail(&mode->head, &connector->user_modes);
1682 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1683 struct drm_display_mode *mode)
1685 struct drm_connector *connector;
1687 struct drm_display_mode *dup_mode;
1689 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1690 if (!connector->encoder)
1692 if (connector->encoder->crtc == crtc) {
1694 dup_mode = drm_mode_duplicate(dev, mode);
1697 ret = drm_mode_attachmode(dev, connector, dup_mode);
1705 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1707 static int drm_mode_detachmode(struct drm_device *dev,
1708 struct drm_connector *connector,
1709 struct drm_display_mode *mode)
1713 struct drm_display_mode *match_mode, *t;
1715 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1716 if (drm_mode_equal(match_mode, mode)) {
1717 list_del(&match_mode->head);
1718 drm_mode_destroy(dev, match_mode);
1730 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1732 struct drm_connector *connector;
1734 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1735 drm_mode_detachmode(dev, connector, mode);
1739 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1742 * drm_fb_attachmode - Attach a user mode to an connector
1743 * @inode: inode from the ioctl
1744 * @filp: file * from the ioctl
1745 * @cmd: cmd from ioctl
1746 * @arg: arg from ioctl
1748 * This attaches a user specified mode to an connector.
1749 * Called by the user via ioctl.
1752 * Zero on success, errno on failure.
1754 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1755 void *data, struct drm_file *file_priv)
1757 struct drm_mode_mode_cmd *mode_cmd = data;
1758 struct drm_connector *connector;
1759 struct drm_display_mode *mode;
1760 struct drm_mode_object *obj;
1761 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1764 mutex_lock(&dev->mode_config.mutex);
1766 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1771 connector = obj_to_connector(obj);
1773 mode = drm_mode_create(dev);
1779 drm_crtc_convert_umode(mode, umode);
1781 ret = drm_mode_attachmode(dev, connector, mode);
1783 mutex_unlock(&dev->mode_config.mutex);
1789 * drm_fb_detachmode - Detach a user specified mode from an connector
1790 * @inode: inode from the ioctl
1791 * @filp: file * from the ioctl
1792 * @cmd: cmd from ioctl
1793 * @arg: arg from ioctl
1795 * Called by the user via ioctl.
1798 * Zero on success, errno on failure.
1800 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1801 void *data, struct drm_file *file_priv)
1803 struct drm_mode_object *obj;
1804 struct drm_mode_mode_cmd *mode_cmd = data;
1805 struct drm_connector *connector;
1806 struct drm_display_mode mode;
1807 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1810 mutex_lock(&dev->mode_config.mutex);
1812 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1817 connector = obj_to_connector(obj);
1819 drm_crtc_convert_umode(&mode, umode);
1820 ret = drm_mode_detachmode(dev, connector, &mode);
1822 mutex_unlock(&dev->mode_config.mutex);
1826 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1827 const char *name, int num_values)
1829 struct drm_property *property = NULL;
1831 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1836 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1837 if (!property->values)
1841 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1842 property->flags = flags;
1843 property->num_values = num_values;
1844 INIT_LIST_HEAD(&property->enum_blob_list);
1847 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1849 list_add_tail(&property->head, &dev->mode_config.property_list);
1855 EXPORT_SYMBOL(drm_property_create);
1857 int drm_property_add_enum(struct drm_property *property, int index,
1858 uint64_t value, const char *name)
1860 struct drm_property_enum *prop_enum;
1862 if (!(property->flags & DRM_MODE_PROP_ENUM))
1865 if (!list_empty(&property->enum_blob_list)) {
1866 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1867 if (prop_enum->value == value) {
1868 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1869 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1875 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1879 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1880 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1881 prop_enum->value = value;
1883 property->values[index] = value;
1884 list_add_tail(&prop_enum->head, &property->enum_blob_list);
1887 EXPORT_SYMBOL(drm_property_add_enum);
1889 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1891 struct drm_property_enum *prop_enum, *pt;
1893 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1894 list_del(&prop_enum->head);
1898 if (property->num_values)
1899 kfree(property->values);
1900 drm_mode_object_put(dev, &property->base);
1901 list_del(&property->head);
1904 EXPORT_SYMBOL(drm_property_destroy);
1906 int drm_connector_attach_property(struct drm_connector *connector,
1907 struct drm_property *property, uint64_t init_val)
1911 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1912 if (connector->property_ids[i] == 0) {
1913 connector->property_ids[i] = property->base.id;
1914 connector->property_values[i] = init_val;
1919 if (i == DRM_CONNECTOR_MAX_PROPERTY)
1923 EXPORT_SYMBOL(drm_connector_attach_property);
1925 int drm_connector_property_set_value(struct drm_connector *connector,
1926 struct drm_property *property, uint64_t value)
1930 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1931 if (connector->property_ids[i] == property->base.id) {
1932 connector->property_values[i] = value;
1937 if (i == DRM_CONNECTOR_MAX_PROPERTY)
1941 EXPORT_SYMBOL(drm_connector_property_set_value);
1943 int drm_connector_property_get_value(struct drm_connector *connector,
1944 struct drm_property *property, uint64_t *val)
1948 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1949 if (connector->property_ids[i] == property->base.id) {
1950 *val = connector->property_values[i];
1955 if (i == DRM_CONNECTOR_MAX_PROPERTY)
1959 EXPORT_SYMBOL(drm_connector_property_get_value);
1961 int drm_mode_getproperty_ioctl(struct drm_device *dev,
1962 void *data, struct drm_file *file_priv)
1964 struct drm_mode_object *obj;
1965 struct drm_mode_get_property *out_resp = data;
1966 struct drm_property *property;
1969 int value_count = 0;
1972 struct drm_property_enum *prop_enum;
1973 struct drm_mode_property_enum __user *enum_ptr;
1974 struct drm_property_blob *prop_blob;
1975 uint32_t *blob_id_ptr;
1976 uint64_t __user *values_ptr;
1977 uint32_t __user *blob_length_ptr;
1979 mutex_lock(&dev->mode_config.mutex);
1980 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
1985 property = obj_to_property(obj);
1987 if (property->flags & DRM_MODE_PROP_ENUM) {
1988 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
1990 } else if (property->flags & DRM_MODE_PROP_BLOB) {
1991 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
1995 value_count = property->num_values;
1997 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
1998 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
1999 out_resp->flags = property->flags;
2001 if ((out_resp->count_values >= value_count) && value_count) {
2002 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2003 for (i = 0; i < value_count; i++) {
2004 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2010 out_resp->count_values = value_count;
2012 if (property->flags & DRM_MODE_PROP_ENUM) {
2013 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2015 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2016 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2018 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2023 if (copy_to_user(&enum_ptr[copied].name,
2024 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2031 out_resp->count_enum_blobs = enum_count;
2034 if (property->flags & DRM_MODE_PROP_BLOB) {
2035 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2037 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2038 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2040 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2041 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2046 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2054 out_resp->count_enum_blobs = blob_count;
2057 mutex_unlock(&dev->mode_config.mutex);
2061 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2064 struct drm_property_blob *blob;
2066 if (!length || !data)
2069 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2073 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2074 blob->length = length;
2076 memcpy(blob->data, data, length);
2078 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2080 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2084 static void drm_property_destroy_blob(struct drm_device *dev,
2085 struct drm_property_blob *blob)
2087 drm_mode_object_put(dev, &blob->base);
2088 list_del(&blob->head);
2092 int drm_mode_getblob_ioctl(struct drm_device *dev,
2093 void *data, struct drm_file *file_priv)
2095 struct drm_mode_object *obj;
2096 struct drm_mode_get_blob *out_resp = data;
2097 struct drm_property_blob *blob;
2101 mutex_lock(&dev->mode_config.mutex);
2102 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2107 blob = obj_to_blob(obj);
2109 if (out_resp->length == blob->length) {
2110 blob_ptr = (void *)(unsigned long)out_resp->data;
2111 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2116 out_resp->length = blob->length;
2119 mutex_unlock(&dev->mode_config.mutex);
2123 int drm_mode_connector_update_edid_property(struct drm_connector *connector, struct edid *edid)
2125 struct drm_device *dev = connector->dev;
2127 if (connector->edid_blob_ptr)
2128 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2130 /* Delete edid, when there is none. */
2132 connector->edid_blob_ptr = NULL;
2133 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2137 connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
2139 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, connector->edid_blob_ptr->base.id);
2142 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2144 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2145 void *data, struct drm_file *file_priv)
2147 struct drm_mode_connector_set_property *out_resp = data;
2148 struct drm_mode_object *obj;
2149 struct drm_property *property;
2150 struct drm_connector *connector;
2154 mutex_lock(&dev->mode_config.mutex);
2156 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2160 connector = obj_to_connector(obj);
2162 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2163 if (connector->property_ids[i] == out_resp->prop_id)
2167 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2171 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2175 property = obj_to_property(obj);
2177 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2180 if (property->flags & DRM_MODE_PROP_RANGE) {
2181 if (out_resp->value < property->values[0])
2184 if (out_resp->value > property->values[1])
2188 for (i = 0; i < property->num_values; i++) {
2189 if (property->values[i] == out_resp->value) {
2199 if (connector->funcs->set_property)
2200 ret = connector->funcs->set_property(connector, property, out_resp->value);
2202 /* store the property value if succesful */
2204 drm_connector_property_set_value(connector, property, out_resp->value);
2206 mutex_unlock(&dev->mode_config.mutex);
2211 int drm_mode_replacefb(struct drm_device *dev,
2212 void *data, struct drm_file *file_priv)
2214 struct drm_mode_fb_cmd *r = data;
2215 struct drm_mode_object *obj;
2216 struct drm_framebuffer *fb;
2218 struct drm_framebuffer *fbl = NULL;
2221 /* right replace the current bo attached to this fb with a new bo */
2222 mutex_lock(&dev->mode_config.mutex);
2223 obj = drm_mode_object_find(dev, r->buffer_id, DRM_MODE_OBJECT_FB);
2228 fb = obj_to_fb(obj);
2230 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2235 DRM_ERROR("tried to replace an fb we didn't own\n");
2240 if (dev->mode_config.funcs->resize_fb)
2241 ret = dev->mode_config.funcs->resize_fb(dev, file_priv, fb, r);
2245 mutex_unlock(&dev->mode_config.mutex);
2250 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2251 struct drm_encoder *encoder)
2255 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2256 if (connector->encoder_ids[i] == 0) {
2257 connector->encoder_ids[i] = encoder->base.id;
2263 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2265 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2266 struct drm_encoder *encoder)
2269 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2270 if (connector->encoder_ids[i] == encoder->base.id) {
2271 connector->encoder_ids[i] = 0;
2272 if (connector->encoder == encoder)
2273 connector->encoder = NULL;
2278 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2280 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2283 crtc->gamma_size = gamma_size;
2285 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2286 if (!crtc->gamma_store) {
2287 crtc->gamma_size = 0;
2293 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2295 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2296 void *data, struct drm_file *file_priv)
2298 struct drm_mode_crtc_lut *crtc_lut = data;
2299 struct drm_mode_object *obj;
2300 struct drm_crtc *crtc;
2301 void *r_base, *g_base, *b_base;
2305 mutex_lock(&dev->mode_config.mutex);
2306 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2311 crtc = obj_to_crtc(obj);
2313 /* memcpy into gamma store */
2314 if (crtc_lut->gamma_size != crtc->gamma_size) {
2319 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2320 r_base = crtc->gamma_store;
2321 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2326 g_base = r_base + size;
2327 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2332 b_base = g_base + size;
2333 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2338 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2341 mutex_unlock(&dev->mode_config.mutex);
2346 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2347 void *data, struct drm_file *file_priv)
2349 struct drm_mode_crtc_lut *crtc_lut = data;
2350 struct drm_mode_object *obj;
2351 struct drm_crtc *crtc;
2352 void *r_base, *g_base, *b_base;
2356 mutex_lock(&dev->mode_config.mutex);
2357 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2362 crtc = obj_to_crtc(obj);
2364 /* memcpy into gamma store */
2365 if (crtc_lut->gamma_size != crtc->gamma_size) {
2370 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2371 r_base = crtc->gamma_store;
2372 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2377 g_base = r_base + size;
2378 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2383 b_base = g_base + size;
2384 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2389 mutex_unlock(&dev->mode_config.mutex);