2 * Copyright (c) 2006-2007 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 * DRM core CRTC related functions
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting documentation, and
11 * that the name of the copyright holders not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. The copyright holders make no representations
14 * about the suitability of this software for any purpose. It is provided "as
15 * is" without express or implied warranty.
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
31 #include <linux/list.h>
36 struct drm_prop_enum_list {
41 static struct drm_prop_enum_list drm_dpms_enum_list[] =
42 { { DPMSModeOn, "On" },
43 { DPMSModeStandby, "Standby" },
44 { DPMSModeSuspend, "Suspend" },
45 { DPMSModeOff, "Off" }
47 static struct drm_prop_enum_list drm_conn_enum_list[] =
48 { { ConnectorVGA, "VGA" },
49 { ConnectorDVII, "DVI-I" },
50 { ConnectorDVID, "DVI-D" },
51 { ConnectorDVIA, "DVI-A" },
52 { ConnectorComposite, "Composite" },
53 { ConnectorSVIDEO, "SVIDEO" },
54 { ConnectorLVDS, "LVDS" },
55 { ConnectorComponent, "Component" },
56 { Connector9PinDIN, "9-pin DIN" },
57 { ConnectorDisplayPort, "DisplayPort" },
58 { ConnectorHDMIA, "HDMI Type A" },
59 { ConnectorHDMIB, "HDMI Type B" },
61 static struct drm_prop_enum_list drm_output_enum_list[] =
62 { { DRM_MODE_OUTPUT_NONE, "None" },
63 { DRM_MODE_OUTPUT_DAC, "DAC" },
64 { DRM_MODE_OUTPUT_TMDS, "TMDS" },
65 { DRM_MODE_OUTPUT_LVDS, "LVDS" },
66 { DRM_MODE_OUTPUT_TVDAC, "TV" },
69 char *drm_get_output_name(struct drm_output *output)
73 snprintf(buf, 32, "%s-%d", drm_output_enum_list[output->output_type].name,
74 output->output_type_id);
79 * drm_idr_get - allocate a new identifier
81 * @ptr: object pointer, used to generate unique ID
84 * Caller must hold DRM mode_config lock.
86 * Create a unique identifier based on @ptr in @dev's identifier space. Used
87 * for tracking modes, CRTCs and outputs.
90 * New unique (relative to other objects in @dev) integer identifier for the
93 int drm_idr_get(struct drm_device *dev, void *ptr)
98 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
99 DRM_ERROR("Ran out memory getting a mode number\n");
103 ret = idr_get_new_above(&dev->mode_config.crtc_idr, ptr, 1, &new_id);
111 * drm_idr_put - free an identifer
116 * Caller must hold DRM mode_config lock.
118 * Free @id from @dev's unique identifier pool.
120 void drm_idr_put(struct drm_device *dev, int id)
122 idr_remove(&dev->mode_config.crtc_idr, id);
126 * drm_crtc_from_fb - find the CRTC structure associated with an fb
128 * @fb: framebuffer in question
131 * Caller must hold mode_config lock.
133 * Find CRTC in the mode_config structure that matches @fb.
136 * Pointer to the CRTC or NULL if it wasn't found.
138 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
139 struct drm_framebuffer *fb)
141 struct drm_crtc *crtc;
143 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
151 * drm_framebuffer_create - create a new framebuffer object
155 * Caller must hold mode config lock.
157 * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
160 * Pointer to new framebuffer or NULL on error.
162 struct drm_framebuffer *drm_framebuffer_create(struct drm_device *dev)
164 struct drm_framebuffer *fb;
166 fb = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
170 fb->id = drm_idr_get(dev, fb);
172 dev->mode_config.num_fb++;
173 list_add(&fb->head, &dev->mode_config.fb_list);
177 EXPORT_SYMBOL(drm_framebuffer_create);
180 * drm_framebuffer_destroy - remove a framebuffer object
181 * @fb: framebuffer to remove
184 * Caller must hold mode config lock.
186 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
187 * it, setting it to NULL.
189 void drm_framebuffer_destroy(struct drm_framebuffer *fb)
191 struct drm_device *dev = fb->dev;
192 struct drm_crtc *crtc;
194 /* remove from any CRTC */
195 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
200 drm_idr_put(dev, fb->id);
202 dev->mode_config.num_fb--;
206 EXPORT_SYMBOL(drm_framebuffer_destroy);
209 * drm_crtc_create - create a new CRTC object
211 * @funcs: callbacks for the new CRTC
214 * Caller must hold mode config lock.
216 * Creates a new CRTC object and adds it to @dev's mode_config structure.
219 * Pointer to new CRTC object or NULL on error.
221 struct drm_crtc *drm_crtc_create(struct drm_device *dev,
222 const struct drm_crtc_funcs *funcs)
224 struct drm_crtc *crtc;
226 crtc = kzalloc(sizeof(struct drm_crtc), GFP_KERNEL);
233 crtc->id = drm_idr_get(dev, crtc);
235 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
236 dev->mode_config.num_crtc++;
240 EXPORT_SYMBOL(drm_crtc_create);
243 * drm_crtc_destroy - remove a CRTC object
244 * @crtc: CRTC to remove
247 * Caller must hold mode config lock.
249 * Cleanup @crtc. Calls @crtc's cleanup function, then removes @crtc from
250 * its associated DRM device's mode_config. Frees it afterwards.
252 void drm_crtc_destroy(struct drm_crtc *crtc)
254 struct drm_device *dev = crtc->dev;
256 if (crtc->funcs->cleanup)
257 (*crtc->funcs->cleanup)(crtc);
259 drm_idr_put(dev, crtc->id);
260 list_del(&crtc->head);
261 dev->mode_config.num_crtc--;
264 EXPORT_SYMBOL(drm_crtc_destroy);
267 * drm_crtc_in_use - check if a given CRTC is in a mode_config
268 * @crtc: CRTC to check
271 * Caller must hold mode config lock.
273 * Walk @crtc's DRM device's mode_config and see if it's in use.
276 * True if @crtc is part of the mode_config, false otherwise.
278 bool drm_crtc_in_use(struct drm_crtc *crtc)
280 struct drm_output *output;
281 struct drm_device *dev = crtc->dev;
282 /* FIXME: Locking around list access? */
283 list_for_each_entry(output, &dev->mode_config.output_list, head)
284 if (output->crtc == crtc)
288 EXPORT_SYMBOL(drm_crtc_in_use);
291 * Detailed mode info for a standard 640x480@60Hz monitor
293 static struct drm_display_mode std_mode[] = {
294 { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
295 752, 800, 0, 480, 490, 492, 525, 0,
296 V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
300 * drm_crtc_probe_output_modes - get complete set of display modes
302 * @maxX: max width for modes
303 * @maxY: max height for modes
306 * Caller must hold mode config lock.
308 * Based on @dev's mode_config layout, scan all the outputs and try to detect
309 * modes on them. Modes will first be added to the output's probed_modes
310 * list, then culled (based on validity and the @maxX, @maxY parameters) and
311 * put into the normal modes list.
313 * Intended to be used either at bootup time or when major configuration
314 * changes have occurred.
316 * FIXME: take into account monitor limits
318 void drm_crtc_probe_single_output_modes(struct drm_output *output, int maxX, int maxY)
320 struct drm_device *dev = output->dev;
321 struct drm_display_mode *mode, *t;
323 //if (maxX == 0 || maxY == 0)
326 /* set all modes to the unverified state */
327 list_for_each_entry_safe(mode, t, &output->modes, head)
328 mode->status = MODE_UNVERIFIED;
330 output->status = (*output->funcs->detect)(output);
332 if (output->status == output_status_disconnected) {
333 DRM_DEBUG("%s is disconnected\n", drm_get_output_name(output));
334 /* TODO set EDID to NULL */
338 ret = (*output->funcs->get_modes)(output);
341 drm_mode_output_list_update(output);
345 drm_mode_validate_size(dev, &output->modes, maxX,
347 list_for_each_entry_safe(mode, t, &output->modes, head) {
348 if (mode->status == MODE_OK)
349 mode->status = (*output->funcs->mode_valid)(output,mode);
353 drm_mode_prune_invalid(dev, &output->modes, TRUE);
355 if (list_empty(&output->modes)) {
356 struct drm_display_mode *stdmode;
358 DRM_DEBUG("No valid modes on %s\n", drm_get_output_name(output));
360 /* Should we do this here ???
361 * When no valid EDID modes are available we end up
362 * here and bailed in the past, now we add a standard
363 * 640x480@60Hz mode and carry on.
365 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
366 drm_mode_probed_add(output, stdmode);
367 drm_mode_list_concat(&output->probed_modes,
370 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
371 drm_get_output_name(output));
374 drm_mode_sort(&output->modes);
376 DRM_DEBUG("Probed modes for %s\n", drm_get_output_name(output));
377 list_for_each_entry_safe(mode, t, &output->modes, head) {
378 mode->vrefresh = drm_mode_vrefresh(mode);
380 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
381 drm_mode_debug_printmodeline(dev, mode);
385 void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY)
387 struct drm_output *output;
389 list_for_each_entry(output, &dev->mode_config.output_list, head) {
390 drm_crtc_probe_single_output_modes(output, maxX, maxY);
393 EXPORT_SYMBOL(drm_crtc_probe_output_modes);
396 * drm_crtc_set_mode - set a mode
397 * @crtc: CRTC to program
403 * Caller must hold mode config lock.
405 * Try to set @mode on @crtc. Give @crtc and its associated outputs a chance
406 * to fixup or reject the mode prior to trying to set it.
409 * True if the mode was set successfully, or false otherwise.
411 bool drm_crtc_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
414 struct drm_device *dev = crtc->dev;
415 struct drm_display_mode *adjusted_mode, saved_mode;
416 int saved_x, saved_y;
417 bool didLock = false;
419 struct drm_output *output;
421 adjusted_mode = drm_mode_duplicate(dev, mode);
423 crtc->enabled = drm_crtc_in_use(crtc);
425 if (!crtc->enabled) {
429 didLock = crtc->funcs->lock(crtc);
431 saved_mode = crtc->mode;
435 /* Update crtc values up front so the driver can rely on them for mode
442 /* XXX short-circuit changes to base location only */
444 /* Pass our mode to the outputs and the CRTC to give them a chance to
445 * adjust it according to limitations or output properties, and also
446 * a chance to reject the mode entirely.
448 list_for_each_entry(output, &dev->mode_config.output_list, head) {
450 if (output->crtc != crtc)
453 if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
458 if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
462 /* Prepare the outputs and CRTCs before setting the mode. */
463 list_for_each_entry(output, &dev->mode_config.output_list, head) {
465 if (output->crtc != crtc)
468 /* Disable the output as the first thing we do. */
469 output->funcs->prepare(output);
472 crtc->funcs->prepare(crtc);
474 /* Set up the DPLL and any output state that needs to adjust or depend
477 crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
479 list_for_each_entry(output, &dev->mode_config.output_list, head) {
481 if (output->crtc != crtc)
484 DRM_INFO("%s: set mode %s %x\n", drm_get_output_name(output), mode->name, mode->mode_id);
486 output->funcs->mode_set(output, mode, adjusted_mode);
489 /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
490 crtc->funcs->commit(crtc);
492 list_for_each_entry(output, &dev->mode_config.output_list, head) {
494 if (output->crtc != crtc)
497 output->funcs->commit(output);
499 #if 0 // TODO def RANDR_12_INTERFACE
500 if (output->randr_output)
501 RRPostPendingProperties (output->randr_output);
505 /* XXX free adjustedmode */
506 drm_mode_destroy(dev, adjusted_mode);
509 // if (scrn->pScreen)
510 // drm_crtc_set_screen_sub_pixel_order(dev);
516 crtc->mode = saved_mode;
520 crtc->funcs->unlock (crtc);
524 EXPORT_SYMBOL(drm_crtc_set_mode);
527 * drm_disable_unused_functions - disable unused objects
531 * Caller must hold mode config lock.
533 * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
534 * by calling its dpms function, which should power it off.
536 void drm_disable_unused_functions(struct drm_device *dev)
538 struct drm_output *output;
539 struct drm_crtc *crtc;
541 list_for_each_entry(output, &dev->mode_config.output_list, head) {
543 (*output->funcs->dpms)(output, DPMSModeOff);
546 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
548 crtc->funcs->dpms(crtc, DPMSModeOff);
551 EXPORT_SYMBOL(drm_disable_unused_functions);
554 * drm_mode_probed_add - add a mode to the specified output's probed mode list
555 * @output: output the new mode
559 * Caller must hold mode config lock.
561 * Add @mode to @output's mode list for later use.
563 void drm_mode_probed_add(struct drm_output *output,
564 struct drm_display_mode *mode)
566 list_add(&mode->head, &output->probed_modes);
568 EXPORT_SYMBOL(drm_mode_probed_add);
571 * drm_mode_remove - remove and free a mode
572 * @output: output list to modify
573 * @mode: mode to remove
576 * Caller must hold mode config lock.
578 * Remove @mode from @output's mode list, then free it.
580 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
582 list_del(&mode->head);
585 EXPORT_SYMBOL(drm_mode_remove);
588 * drm_output_create - create a new output
590 * @funcs: callbacks for this output
591 * @name: user visible name of the output
594 * Caller must hold @dev's mode_config lock.
596 * Creates a new drm_output structure and adds it to @dev's mode_config
600 * Pointer to the new output or NULL on error.
602 struct drm_output *drm_output_create(struct drm_device *dev,
603 const struct drm_output_funcs *funcs,
606 struct drm_output *output = NULL;
608 output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
613 output->funcs = funcs;
614 output->id = drm_idr_get(dev, output);
615 output->output_type = output_type;
616 output->output_type_id = 1; /* TODO */
617 output->subpixel_order = SubPixelUnknown;
618 INIT_LIST_HEAD(&output->user_modes);
619 INIT_LIST_HEAD(&output->probed_modes);
620 INIT_LIST_HEAD(&output->modes);
622 /* output_set_monitor(output)? */
623 /* check for output_ignored(output)? */
625 mutex_lock(&dev->mode_config.mutex);
626 list_add_tail(&output->head, &dev->mode_config.output_list);
627 dev->mode_config.num_output++;
629 drm_output_attach_property(output, dev->mode_config.edid_property, 0);
631 drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
633 mutex_unlock(&dev->mode_config.mutex);
638 EXPORT_SYMBOL(drm_output_create);
641 * drm_output_destroy - remove an output
642 * @output: output to remove
645 * Caller must hold @dev's mode_config lock.
647 * Call @output's cleanup function, then remove the output from the DRM
648 * mode_config after freeing @output's modes.
650 void drm_output_destroy(struct drm_output *output)
652 struct drm_device *dev = output->dev;
653 struct drm_display_mode *mode, *t;
655 if (*output->funcs->cleanup)
656 (*output->funcs->cleanup)(output);
658 list_for_each_entry_safe(mode, t, &output->probed_modes, head)
659 drm_mode_remove(output, mode);
661 list_for_each_entry_safe(mode, t, &output->modes, head)
662 drm_mode_remove(output, mode);
664 list_for_each_entry_safe(mode, t, &output->user_modes, head)
665 drm_mode_remove(output, mode);
667 mutex_lock(&dev->mode_config.mutex);
668 drm_idr_put(dev, output->id);
669 list_del(&output->head);
670 mutex_unlock(&dev->mode_config.mutex);
673 EXPORT_SYMBOL(drm_output_destroy);
677 * drm_mode_create - create a new display mode
683 * Create a new drm_display_mode, give it an ID, and return it.
686 * Pointer to new mode on success, NULL on error.
688 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
690 struct drm_display_mode *nmode;
692 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
696 nmode->mode_id = drm_idr_get(dev, nmode);
699 EXPORT_SYMBOL(drm_mode_create);
702 * drm_mode_destroy - remove a mode
704 * @mode: mode to remove
707 * Caller must hold mode config lock.
709 * Free @mode's unique identifier, then free it.
711 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
713 drm_idr_put(dev, mode->mode_id);
717 EXPORT_SYMBOL(drm_mode_destroy);
719 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
723 dev->mode_config.edid_property =
724 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
727 dev->mode_config.dpms_property =
728 drm_property_create(dev, DRM_MODE_PROP_ENUM, "DPMS", 4);
730 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
731 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
733 dev->mode_config.connector_type_property =
734 drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
735 "Connector Type", 10);
736 for (i = 0; i < ARRAY_SIZE(drm_conn_enum_list); i++)
737 drm_property_add_enum(dev->mode_config.connector_type_property, i, drm_conn_enum_list[i].type, drm_conn_enum_list[i].name);
739 dev->mode_config.connector_num_property =
740 drm_property_create(dev, DRM_MODE_PROP_RANGE | DRM_MODE_PROP_IMMUTABLE,
742 dev->mode_config.connector_num_property->values[0] = 0;
743 dev->mode_config.connector_num_property->values[1] = 20;
748 * drm_mode_config_init - initialize DRM mode_configuration structure
752 * None, should happen single threaded at init time.
754 * Initialize @dev's mode_config structure, used for tracking the graphics
755 * configuration of @dev.
757 void drm_mode_config_init(struct drm_device *dev)
759 mutex_init(&dev->mode_config.mutex);
760 INIT_LIST_HEAD(&dev->mode_config.fb_list);
761 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
762 INIT_LIST_HEAD(&dev->mode_config.output_list);
763 INIT_LIST_HEAD(&dev->mode_config.property_list);
764 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
765 idr_init(&dev->mode_config.crtc_idr);
767 drm_mode_create_standard_output_properties(dev);
769 /* Just to be sure */
770 dev->mode_config.num_fb = 0;
771 dev->mode_config.num_output = 0;
772 dev->mode_config.num_crtc = 0;
773 dev->mode_config.hotplug_counter = 0;
775 EXPORT_SYMBOL(drm_mode_config_init);
778 * drm_get_buffer_object - find the buffer object for a given handle
780 * @bo: pointer to caller's buffer_object pointer
781 * @handle: handle to lookup
784 * Must take @dev's struct_mutex to protect buffer object lookup.
786 * Given @handle, lookup the buffer object in @dev and put it in the caller's
790 * Zero on success, -EINVAL if the handle couldn't be found.
792 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
794 struct drm_user_object *uo;
795 struct drm_hash_item *hash;
800 mutex_lock(&dev->struct_mutex);
801 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
803 DRM_ERROR("Couldn't find handle.\n");
808 uo = drm_hash_entry(hash, struct drm_user_object, hash);
809 if (uo->type != drm_buffer_type) {
814 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
817 mutex_unlock(&dev->struct_mutex);
822 * drm_pick_crtcs - pick crtcs for output devices
826 * Caller must hold mode config lock.
828 static void drm_pick_crtcs (struct drm_device *dev)
831 struct drm_output *output, *output_equal;
832 struct drm_crtc *crtc;
833 struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
835 list_for_each_entry(output, &dev->mode_config.output_list, head) {
838 /* Don't hook up outputs that are disconnected ??
840 * This is debateable. Do we want fixed /dev/fbX or
841 * dynamic on hotplug (need mode code for that though) ?
843 * If we don't hook up outputs now, then we only create
844 * /dev/fbX for the output that's enabled, that's good as
845 * the users console will be on that output.
847 * If we do hook up outputs that are disconnected now, then
848 * the user may end up having to muck about with the fbcon
849 * map flags to assign his console to the enabled output. Ugh.
851 if (output->status != output_status_connected)
855 list_for_each_entry(des_mode, &output->modes, head) {
856 if (des_mode->type & DRM_MODE_TYPE_PREFERRED)
860 /* No preferred mode, let's just select the first available */
861 if (!des_mode || !(des_mode->type & DRM_MODE_TYPE_PREFERRED)) {
862 list_for_each_entry(des_mode, &output->modes, head) {
869 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
873 if ((output->possible_crtcs & (1 << c)) == 0)
876 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
877 if (output->id == output_equal->id)
880 /* Find out if crtc has been assigned before */
881 if (output_equal->crtc == crtc)
885 #if 1 /* continue for now */
891 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
893 if (output->id == output_equal->id)
896 list_for_each_entry(modes, &output->modes, head) {
897 list_for_each_entry(modes_equal, &output_equal->modes, head) {
898 if (drm_mode_equal (modes, modes_equal)) {
899 if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
900 printk("Cloning %s (0x%lx) to %s (0x%lx)\n",drm_get_output_name(output),output->possible_clones,drm_get_output_name(output_equal),output_equal->possible_clones);
910 /* crtc has been assigned skip it */
914 /* Found a CRTC to attach to, do it ! */
916 output->crtc->desired_mode = des_mode;
917 output->initial_x = 0;
918 output->initial_y = 0;
919 DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
924 EXPORT_SYMBOL(drm_pick_crtcs);
927 * drm_initial_config - setup a sane initial output configuration
929 * @can_grow: this configuration is growable
932 * Called at init time, must take mode config lock.
934 * Scan the CRTCs and outputs and try to put together an initial setup.
935 * At the moment, this is a cloned configuration across all heads with
936 * a new framebuffer object as the backing store.
939 * Zero if everything went ok, nonzero otherwise.
941 bool drm_initial_config(struct drm_device *dev, bool can_grow)
943 struct drm_output *output;
944 struct drm_crtc *crtc;
947 mutex_lock(&dev->mode_config.mutex);
949 drm_crtc_probe_output_modes(dev, 2048, 2048);
953 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
955 /* can't setup the crtc if there's no assigned mode */
956 if (!crtc->desired_mode)
959 /* Now setup the fbdev for attached crtcs */
960 dev->driver->fb_probe(dev, crtc);
963 /* This is a little screwy, as we've already walked the outputs
964 * above, but it's a little bit of magic too. There's the potential
965 * for things not to get setup above if an existing device gets
966 * re-assigned thus confusing the hardware. By walking the outputs
967 * this fixes up their crtc's.
969 list_for_each_entry(output, &dev->mode_config.output_list, head) {
971 /* can't setup the output if there's no assigned mode */
972 if (!output->crtc || !output->crtc->desired_mode)
975 /* and needs an attached fb */
976 if (output->crtc->fb)
977 drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
980 drm_disable_unused_functions(dev);
982 mutex_unlock(&dev->mode_config.mutex);
985 EXPORT_SYMBOL(drm_initial_config);
988 * drm_mode_config_cleanup - free up DRM mode_config info
992 * Caller must hold mode config lock.
994 * Free up all the outputs and CRTCs associated with this DRM device, then
995 * free up the framebuffers and associated buffer objects.
997 * FIXME: cleanup any dangling user buffer objects too
999 void drm_mode_config_cleanup(struct drm_device *dev)
1001 struct drm_output *output, *ot;
1002 struct drm_crtc *crtc, *ct;
1003 struct drm_framebuffer *fb, *fbt;
1004 struct drm_property *property, *pt;
1006 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1007 drm_output_destroy(output);
1010 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1011 drm_property_destroy(dev, property);
1014 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1015 /* there should only be bo of kernel type left */
1016 if (fb->bo->type != drm_bo_type_kernel)
1017 drm_framebuffer_destroy(fb);
1019 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1022 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1023 drm_crtc_destroy(crtc);
1027 EXPORT_SYMBOL(drm_mode_config_cleanup);
1030 * drm_crtc_set_config - set a new config from userspace
1031 * @crtc: CRTC to setup
1032 * @crtc_info: user provided configuration
1033 * @new_mode: new mode to set
1034 * @output_set: set of outputs for the new config
1035 * @fb: new framebuffer
1038 * Caller must hold mode config lock.
1040 * Setup a new configuration, provided by the user in @crtc_info, and enable
1046 int drm_crtc_set_config(struct drm_crtc *crtc, struct drm_mode_crtc *crtc_info, struct drm_display_mode *new_mode, struct drm_output **output_set, struct drm_framebuffer *fb)
1048 struct drm_device *dev = crtc->dev;
1049 struct drm_crtc **save_crtcs, *new_crtc;
1050 bool save_enabled = crtc->enabled;
1051 bool changed = false;
1052 bool flip_or_move = false;
1053 struct drm_output *output;
1056 save_crtcs = kzalloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc *), GFP_KERNEL);
1060 /* We should be able to check here if the fb has the same properties
1061 * and then just flip_or_move it */
1065 if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1066 flip_or_move = true;
1068 if (new_mode && !drm_mode_equal(new_mode, &crtc->mode))
1071 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1072 save_crtcs[count++] = output->crtc;
1074 if (output->crtc == crtc)
1077 new_crtc = output->crtc;
1079 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1080 if (output_set[ro] == output)
1083 if (new_crtc != output->crtc) {
1085 output->crtc = new_crtc;
1089 /* mode_set_base is not a required function */
1090 if (flip_or_move && !crtc->funcs->mode_set_base)
1095 crtc->enabled = (new_mode != NULL);
1096 if (new_mode != NULL) {
1097 DRM_DEBUG("attempting to set mode from userspace %p\n", crtc->fb);
1098 drm_mode_debug_printmodeline(dev, new_mode);
1099 if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1101 crtc->enabled = save_enabled;
1103 list_for_each_entry(output, &dev->mode_config.output_list, head)
1104 output->crtc = save_crtcs[count++];
1108 crtc->desired_x = crtc_info->x;
1109 crtc->desired_y = crtc_info->y;
1110 crtc->desired_mode = new_mode;
1112 drm_disable_unused_functions(dev);
1113 } else if (flip_or_move) {
1114 crtc->funcs->mode_set_base(crtc, crtc_info->x, crtc_info->y);
1122 * drm_hotplug_stage_two
1124 * @output hotpluged output
1127 * Caller must hold mode config lock, function might grap struct lock.
1129 * Stage two of a hotplug.
1132 * Zero on success, errno on failure.
1134 int drm_hotplug_stage_two(struct drm_device *dev, struct drm_output *output,
1139 /* We might want to do something more here */
1141 DRM_DEBUG("not connected\n");
1142 dev->mode_config.hotplug_counter++;
1146 if (output->crtc && output->crtc->desired_mode) {
1147 DRM_DEBUG("drm thinks that the output already has a config\n");
1151 drm_crtc_probe_output_modes(dev, 2048, 2048);
1154 drm_pick_crtcs(dev);
1156 if (!output->crtc || !output->crtc->desired_mode) {
1157 DRM_DEBUG("could not find a desired mode or crtc for output\n");
1161 /* We should realy check if there is a fb using this crtc */
1163 dev->driver->fb_probe(dev, output->crtc);
1165 dev->driver->fb_resize(dev, output->crtc);
1167 if (!drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0))
1168 DRM_ERROR("failed to set mode after hotplug\n");
1171 drm_disable_unused_functions(dev);
1173 dev->mode_config.hotplug_counter++;
1177 dev->mode_config.hotplug_counter++;
1180 EXPORT_SYMBOL(drm_hotplug_stage_two);
1182 int drm_mode_hotplug_ioctl(struct drm_device *dev,
1183 void *data, struct drm_file *file_priv)
1185 struct drm_mode_hotplug *arg = data;
1187 arg->counter = dev->mode_config.hotplug_counter;
1193 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1194 * @out: drm_mode_modeinfo struct to return to the user
1195 * @in: drm_display_mode to use
1200 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1203 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1205 out->clock = in->clock;
1206 out->hdisplay = in->hdisplay;
1207 out->hsync_start = in->hsync_start;
1208 out->hsync_end = in->hsync_end;
1209 out->htotal = in->htotal;
1210 out->hskew = in->hskew;
1211 out->vdisplay = in->vdisplay;
1212 out->vsync_start = in->vsync_start;
1213 out->vsync_end = in->vsync_end;
1214 out->vtotal = in->vtotal;
1215 out->vscan = in->vscan;
1216 out->vrefresh = in->vrefresh;
1217 out->flags = in->flags;
1218 out->type = in->type;
1219 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1220 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1224 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1225 * @out: drm_display_mode to return to the user
1226 * @in: drm_mode_modeinfo to use
1231 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1234 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1236 out->clock = in->clock;
1237 out->hdisplay = in->hdisplay;
1238 out->hsync_start = in->hsync_start;
1239 out->hsync_end = in->hsync_end;
1240 out->htotal = in->htotal;
1241 out->hskew = in->hskew;
1242 out->vdisplay = in->vdisplay;
1243 out->vsync_start = in->vsync_start;
1244 out->vsync_end = in->vsync_end;
1245 out->vtotal = in->vtotal;
1246 out->vscan = in->vscan;
1247 out->vrefresh = in->vrefresh;
1248 out->flags = in->flags;
1249 out->type = in->type;
1250 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1251 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1255 * drm_mode_getresources - get graphics configuration
1256 * @inode: inode from the ioctl
1257 * @filp: file * from the ioctl
1258 * @cmd: cmd from ioctl
1259 * @arg: arg from ioctl
1262 * Takes mode config lock.
1264 * Construct a set of configuration description structures and return
1265 * them to the user, including CRTC, output and framebuffer configuration.
1267 * Called by the user via ioctl.
1270 * Zero on success, errno on failure.
1272 int drm_mode_getresources(struct drm_device *dev,
1273 void *data, struct drm_file *file_priv)
1275 struct drm_mode_card_res *card_res = data;
1276 struct list_head *lh;
1277 struct drm_framebuffer *fb;
1278 struct drm_output *output;
1279 struct drm_crtc *crtc;
1281 int output_count = 0;
1285 uint32_t __user *fb_id;
1286 uint32_t __user *crtc_id;
1287 uint32_t __user *output_id;
1289 mutex_lock(&dev->mode_config.mutex);
1291 list_for_each(lh, &dev->mode_config.fb_list)
1294 list_for_each(lh, &dev->mode_config.crtc_list)
1297 list_for_each(lh, &dev->mode_config.output_list)
1300 card_res->max_height = dev->mode_config.max_height;
1301 card_res->min_height = dev->mode_config.min_height;
1302 card_res->max_width = dev->mode_config.max_width;
1303 card_res->min_width = dev->mode_config.min_width;
1305 /* handle this in 4 parts */
1307 if (card_res->count_fbs >= fb_count) {
1309 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1310 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1311 if (put_user(fb->id, fb_id + copied)) {
1318 card_res->count_fbs = fb_count;
1321 if (card_res->count_crtcs >= crtc_count) {
1323 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1324 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1325 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1326 if (put_user(crtc->id, crtc_id + copied)) {
1333 card_res->count_crtcs = crtc_count;
1337 if (card_res->count_outputs >= output_count) {
1339 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1340 list_for_each_entry(output, &dev->mode_config.output_list,
1342 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1343 if (put_user(output->id, output_id + copied)) {
1350 card_res->count_outputs = output_count;
1352 DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1353 card_res->count_outputs);
1356 mutex_unlock(&dev->mode_config.mutex);
1361 * drm_mode_getcrtc - get CRTC configuration
1362 * @inode: inode from the ioctl
1363 * @filp: file * from the ioctl
1364 * @cmd: cmd from ioctl
1365 * @arg: arg from ioctl
1370 * Construct a CRTC configuration structure to return to the user.
1372 * Called by the user via ioctl.
1375 * Zero on success, errno on failure.
1377 int drm_mode_getcrtc(struct drm_device *dev,
1378 void *data, struct drm_file *file_priv)
1380 struct drm_mode_crtc *crtc_resp = data;
1381 struct drm_crtc *crtc;
1382 struct drm_output *output;
1386 mutex_lock(&dev->mode_config.mutex);
1387 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1388 if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1393 crtc_resp->x = crtc->x;
1394 crtc_resp->y = crtc->y;
1397 crtc_resp->fb_id = crtc->fb->id;
1399 crtc_resp->fb_id = 0;
1401 crtc_resp->outputs = 0;
1402 if (crtc->enabled) {
1404 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1405 crtc_resp->mode_valid = 1;
1407 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1408 if (output->crtc == crtc)
1409 crtc_resp->outputs |= 1 << (ocount++);
1413 crtc_resp->mode_valid = 0;
1417 mutex_unlock(&dev->mode_config.mutex);
1422 * drm_mode_getoutput - get output configuration
1423 * @inode: inode from the ioctl
1424 * @filp: file * from the ioctl
1425 * @cmd: cmd from ioctl
1426 * @arg: arg from ioctl
1431 * Construct a output configuration structure to return to the user.
1433 * Called by the user via ioctl.
1436 * Zero on success, errno on failure.
1438 int drm_mode_getoutput(struct drm_device *dev,
1439 void *data, struct drm_file *file_priv)
1441 struct drm_mode_get_output *out_resp = data;
1442 struct drm_output *output;
1443 struct drm_display_mode *mode;
1445 int props_count = 0;
1449 struct drm_mode_modeinfo u_mode;
1450 struct drm_mode_modeinfo __user *mode_ptr;
1451 uint32_t __user *prop_ptr;
1452 uint64_t __user *prop_values;
1454 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1456 DRM_DEBUG("output id %d:\n", out_resp->output);
1458 mutex_lock(&dev->mode_config.mutex);
1459 output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1460 if (!output || (output->id != out_resp->output)) {
1465 list_for_each_entry(mode, &output->modes, head)
1468 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1469 if (output->property_ids[i] != 0) {
1474 if (out_resp->count_modes == 0) {
1475 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1478 out_resp->output_type = output->output_type;
1479 out_resp->output_type_id = output->output_type_id;
1480 out_resp->mm_width = output->mm_width;
1481 out_resp->mm_height = output->mm_height;
1482 out_resp->subpixel = output->subpixel_order;
1483 out_resp->connection = output->status;
1485 out_resp->crtc = output->crtc->id;
1489 out_resp->crtcs = output->possible_crtcs;
1490 out_resp->clones = output->possible_clones;
1492 if ((out_resp->count_modes >= mode_count) && mode_count) {
1494 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1495 list_for_each_entry(mode, &output->modes, head) {
1496 drm_crtc_convert_to_umode(&u_mode, mode);
1497 if (copy_to_user(mode_ptr + copied,
1498 &u_mode, sizeof(u_mode))) {
1506 out_resp->count_modes = mode_count;
1508 if ((out_resp->count_props >= props_count) && props_count) {
1510 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1511 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1512 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1513 if (output->property_ids[i] != 0) {
1514 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1519 if (put_user(output->property_values[i], prop_values + copied)) {
1527 out_resp->count_props = props_count;
1530 mutex_unlock(&dev->mode_config.mutex);
1535 * drm_mode_setcrtc - set CRTC configuration
1536 * @inode: inode from the ioctl
1537 * @filp: file * from the ioctl
1538 * @cmd: cmd from ioctl
1539 * @arg: arg from ioctl
1544 * Build a new CRTC configuration based on user request.
1546 * Called by the user via ioctl.
1549 * Zero on success, errno on failure.
1551 int drm_mode_setcrtc(struct drm_device *dev,
1552 void *data, struct drm_file *file_priv)
1554 struct drm_mode_crtc *crtc_req = data;
1555 struct drm_crtc *crtc;
1556 struct drm_output **output_set = NULL, *output;
1557 struct drm_framebuffer *fb = NULL;
1558 struct drm_display_mode *mode = NULL;
1561 uint32_t __user *set_outputs_ptr;
1563 mutex_lock(&dev->mode_config.mutex);
1564 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1565 if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1566 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1571 if (crtc_req->mode_valid) {
1572 /* if we have a mode we need a framebuffer */
1573 if (crtc_req->fb_id) {
1574 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1575 if (!fb || (fb->id != crtc_req->fb_id)) {
1576 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1580 DRM_DEBUG("found fb %p for id %d\n", fb, crtc_req->fb_id);
1582 DRM_DEBUG("Unknown FB ID %d\n", crtc_req->fb_id);
1588 mode = drm_mode_create(dev);
1589 drm_crtc_convert_umode(mode, &crtc_req->mode);
1590 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1593 if (crtc_req->count_outputs == 0 && mode) {
1594 DRM_DEBUG("Count outputs is 0 but mode set\n");
1599 if (crtc_req->count_outputs > 0 && !mode && !fb) {
1600 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1605 if (crtc_req->count_outputs > 0) {
1607 /* Maybe we should check that count_outputs is a sensible value. */
1608 output_set = kmalloc(crtc_req->count_outputs *
1609 sizeof(struct drm_output *), GFP_KERNEL);
1615 for (i = 0; i < crtc_req->count_outputs; i++) {
1616 set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1617 if (get_user(out_id, &set_outputs_ptr[i])) {
1622 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1623 if (!output || (out_id != output->id)) {
1624 DRM_DEBUG("Output id %d unknown\n", out_id);
1629 output_set[i] = output;
1633 /* What happens to output_set, leak? */
1634 ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1637 mutex_unlock(&dev->mode_config.mutex);
1641 int drm_mode_cursor_ioctl(struct drm_device *dev,
1642 void *data, struct drm_file *file_priv)
1644 struct drm_mode_cursor *req = data;
1645 struct drm_crtc *crtc;
1646 struct drm_buffer_object *bo = NULL; /* must be set */
1652 DRM_ERROR("no operation set\n");
1656 mutex_lock(&dev->mode_config.mutex);
1657 crtc = idr_find(&dev->mode_config.crtc_idr, req->crtc);
1658 if (!crtc || (crtc->id != req->crtc)) {
1659 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1664 if (req->flags & DRM_MODE_CURSOR_BO) {
1665 /* Turn of the cursor if handle is 0 */
1667 ret = drm_get_buffer_object(dev, &bo, req->handle);
1670 DRM_ERROR("invalid buffer id\n");
1675 if (crtc->funcs->cursor_set) {
1676 ret = crtc->funcs->cursor_set(crtc, bo, req->width, req->height);
1678 DRM_ERROR("crtc does not support cursor\n");
1684 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1685 if (crtc->funcs->cursor_move) {
1686 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1688 DRM_ERROR("crtc does not support cursor\n");
1694 mutex_unlock(&dev->mode_config.mutex);
1699 * drm_mode_addfb - add an FB to the graphics configuration
1700 * @inode: inode from the ioctl
1701 * @filp: file * from the ioctl
1702 * @cmd: cmd from ioctl
1703 * @arg: arg from ioctl
1706 * Takes mode config lock.
1708 * Add a new FB to the specified CRTC, given a user request.
1710 * Called by the user via ioctl.
1713 * Zero on success, errno on failure.
1715 int drm_mode_addfb(struct drm_device *dev,
1716 void *data, struct drm_file *file_priv)
1718 struct drm_mode_fb_cmd *r = data;
1719 struct drm_mode_config *config = &dev->mode_config;
1720 struct drm_framebuffer *fb;
1721 struct drm_buffer_object *bo;
1724 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1725 DRM_ERROR("mode new framebuffer width not within limits\n");
1728 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1729 DRM_ERROR("mode new framebuffer height not within limits\n");
1733 mutex_lock(&dev->mode_config.mutex);
1734 /* TODO check limits are okay */
1735 ret = drm_get_buffer_object(dev, &bo, r->handle);
1737 DRM_ERROR("BO handle not valid\n");
1742 /* TODO check buffer is sufficently large */
1743 /* TODO setup destructor callback */
1745 fb = drm_framebuffer_create(dev);
1747 DRM_ERROR("could not create framebuffer\n");
1752 fb->width = r->width;
1753 fb->height = r->height;
1754 fb->pitch = r->pitch;
1755 fb->bits_per_pixel = r->bpp;
1756 fb->depth = r->depth;
1759 r->buffer_id = fb->id;
1761 list_add(&fb->filp_head, &file_priv->fbs);
1764 mutex_unlock(&dev->mode_config.mutex);
1769 * drm_mode_rmfb - remove an FB from the configuration
1770 * @inode: inode from the ioctl
1771 * @filp: file * from the ioctl
1772 * @cmd: cmd from ioctl
1773 * @arg: arg from ioctl
1776 * Takes mode config lock.
1778 * Remove the FB specified by the user.
1780 * Called by the user via ioctl.
1783 * Zero on success, errno on failure.
1785 int drm_mode_rmfb(struct drm_device *dev,
1786 void *data, struct drm_file *file_priv)
1788 struct drm_framebuffer *fb = 0;
1789 struct drm_framebuffer *fbl = 0;
1790 uint32_t *id = data;
1794 mutex_lock(&dev->mode_config.mutex);
1795 fb = idr_find(&dev->mode_config.crtc_idr, *id);
1796 /* TODO check that we realy get a framebuffer back. */
1797 if (!fb || (*id != fb->id)) {
1798 DRM_ERROR("mode invalid framebuffer id\n");
1803 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1808 DRM_ERROR("tried to remove a fb that we didn't own\n");
1813 /* TODO release all crtc connected to the framebuffer */
1814 /* TODO unhock the destructor from the buffer object */
1816 if (fb->bo->type == drm_bo_type_kernel)
1817 DRM_ERROR("the bo type should not be of kernel type\n");
1819 list_del(&fb->filp_head);
1820 drm_framebuffer_destroy(fb);
1823 mutex_unlock(&dev->mode_config.mutex);
1828 * drm_mode_getfb - get FB info
1829 * @inode: inode from the ioctl
1830 * @filp: file * from the ioctl
1831 * @cmd: cmd from ioctl
1832 * @arg: arg from ioctl
1837 * Lookup the FB given its ID and return info about it.
1839 * Called by the user via ioctl.
1842 * Zero on success, errno on failure.
1844 int drm_mode_getfb(struct drm_device *dev,
1845 void *data, struct drm_file *file_priv)
1847 struct drm_mode_fb_cmd *r = data;
1848 struct drm_framebuffer *fb;
1851 mutex_lock(&dev->mode_config.mutex);
1852 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1853 if (!fb || (r->buffer_id != fb->id)) {
1854 DRM_ERROR("invalid framebuffer id\n");
1859 r->height = fb->height;
1860 r->width = fb->width;
1861 r->depth = fb->depth;
1862 r->bpp = fb->bits_per_pixel;
1863 r->handle = fb->bo->base.hash.key;
1864 r->pitch = fb->pitch;
1867 mutex_unlock(&dev->mode_config.mutex);
1872 * drm_fb_release - remove and free the FBs on this file
1873 * @filp: file * from the ioctl
1876 * Takes mode config lock.
1878 * Destroy all the FBs associated with @filp.
1880 * Called by the user via ioctl.
1883 * Zero on success, errno on failure.
1885 void drm_fb_release(struct file *filp)
1887 struct drm_file *priv = filp->private_data;
1888 struct drm_device *dev = priv->minor->dev;
1889 struct drm_framebuffer *fb, *tfb;
1891 mutex_lock(&dev->mode_config.mutex);
1892 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1893 list_del(&fb->filp_head);
1894 if (fb->bo->type == drm_bo_type_kernel)
1895 DRM_ERROR("the bo type should not be of kernel_type, the kernel will probably explode, why Dave\n");
1897 drm_framebuffer_destroy(fb);
1899 mutex_unlock(&dev->mode_config.mutex);
1906 static int drm_mode_attachmode(struct drm_device *dev,
1907 struct drm_output *output,
1908 struct drm_display_mode *mode)
1912 list_add_tail(&mode->head, &output->user_modes);
1916 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1917 struct drm_display_mode *mode)
1919 struct drm_output *output;
1921 struct drm_display_mode *dup_mode;
1923 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1924 if (output->crtc == crtc) {
1926 dup_mode = drm_mode_duplicate(dev, mode);
1929 ret = drm_mode_attachmode(dev, output, dup_mode);
1937 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1939 static int drm_mode_detachmode(struct drm_device *dev,
1940 struct drm_output *output,
1941 struct drm_display_mode *mode)
1945 struct drm_display_mode *match_mode, *t;
1947 list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1948 if (drm_mode_equal(match_mode, mode)) {
1949 list_del(&match_mode->head);
1950 drm_mode_destroy(dev, match_mode);
1962 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1964 struct drm_output *output;
1966 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1967 drm_mode_detachmode(dev, output, mode);
1971 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1974 * drm_fb_attachmode - Attach a user mode to an output
1975 * @inode: inode from the ioctl
1976 * @filp: file * from the ioctl
1977 * @cmd: cmd from ioctl
1978 * @arg: arg from ioctl
1980 * This attaches a user specified mode to an output.
1981 * Called by the user via ioctl.
1984 * Zero on success, errno on failure.
1986 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1987 void *data, struct drm_file *file_priv)
1989 struct drm_mode_mode_cmd *mode_cmd = data;
1990 struct drm_output *output;
1991 struct drm_display_mode *mode;
1992 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1995 mutex_lock(&dev->mode_config.mutex);
1997 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1998 if (!output || (output->id != mode_cmd->output_id)) {
2003 mode = drm_mode_create(dev);
2009 drm_crtc_convert_umode(mode, umode);
2011 ret = drm_mode_attachmode(dev, output, mode);
2013 mutex_unlock(&dev->mode_config.mutex);
2019 * drm_fb_detachmode - Detach a user specified mode from an output
2020 * @inode: inode from the ioctl
2021 * @filp: file * from the ioctl
2022 * @cmd: cmd from ioctl
2023 * @arg: arg from ioctl
2025 * Called by the user via ioctl.
2028 * Zero on success, errno on failure.
2030 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2031 void *data, struct drm_file *file_priv)
2033 struct drm_mode_mode_cmd *mode_cmd = data;
2034 struct drm_output *output;
2035 struct drm_display_mode mode;
2036 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2039 mutex_lock(&dev->mode_config.mutex);
2041 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2042 if (!output || (output->id != mode_cmd->output_id)) {
2047 drm_crtc_convert_umode(&mode, umode);
2048 ret = drm_mode_detachmode(dev, output, &mode);
2050 mutex_unlock(&dev->mode_config.mutex);
2054 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2055 const char *name, int num_values)
2057 struct drm_property *property = NULL;
2059 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2064 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2065 if (!property->values)
2069 property->id = drm_idr_get(dev, property);
2070 property->flags = flags;
2071 property->num_values = num_values;
2072 INIT_LIST_HEAD(&property->enum_blob_list);
2075 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2077 list_add_tail(&property->head, &dev->mode_config.property_list);
2083 EXPORT_SYMBOL(drm_property_create);
2085 int drm_property_add_enum(struct drm_property *property, int index,
2086 uint64_t value, const char *name)
2088 struct drm_property_enum *prop_enum;
2090 if (!(property->flags & DRM_MODE_PROP_ENUM))
2093 if (!list_empty(&property->enum_blob_list)) {
2094 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2095 if (prop_enum->value == value) {
2096 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2097 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2103 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2107 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2108 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2109 prop_enum->value = value;
2111 property->values[index] = value;
2112 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2115 EXPORT_SYMBOL(drm_property_add_enum);
2117 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2119 struct drm_property_enum *prop_enum, *pt;
2121 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2122 list_del(&prop_enum->head);
2126 if (property->num_values)
2127 kfree(property->values);
2128 drm_idr_put(dev, property->id);
2129 list_del(&property->head);
2132 EXPORT_SYMBOL(drm_property_destroy);
2134 int drm_output_attach_property(struct drm_output *output,
2135 struct drm_property *property, uint64_t init_val)
2139 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2140 if (output->property_ids[i] == 0) {
2141 output->property_ids[i] = property->id;
2142 output->property_values[i] = init_val;
2147 if (i == DRM_OUTPUT_MAX_PROPERTY)
2151 EXPORT_SYMBOL(drm_output_attach_property);
2153 int drm_output_property_set_value(struct drm_output *output,
2154 struct drm_property *property, uint64_t value)
2158 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2159 if (output->property_ids[i] == property->id) {
2160 output->property_values[i] = value;
2165 if (i == DRM_OUTPUT_MAX_PROPERTY)
2169 EXPORT_SYMBOL(drm_output_property_set_value);
2171 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2172 void *data, struct drm_file *file_priv)
2174 struct drm_mode_get_property *out_resp = data;
2175 struct drm_property *property;
2178 int value_count = 0;
2181 struct drm_property_enum *prop_enum;
2182 struct drm_mode_property_enum __user *enum_ptr;
2183 struct drm_property_blob *prop_blob;
2184 uint32_t *blob_id_ptr;
2185 uint64_t __user *values_ptr;
2186 uint32_t __user *blob_length_ptr;
2188 mutex_lock(&dev->mode_config.mutex);
2189 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2190 if (!property || (property->id != out_resp->prop_id)) {
2195 if (property->flags & DRM_MODE_PROP_ENUM) {
2196 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2198 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2199 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2203 value_count = property->num_values;
2205 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2206 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2207 out_resp->flags = property->flags;
2209 if ((out_resp->count_values >= value_count) && value_count) {
2210 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2211 for (i = 0; i < value_count; i++) {
2212 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2218 out_resp->count_values = value_count;
2220 if (property->flags & DRM_MODE_PROP_ENUM) {
2222 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2224 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2225 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2227 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2232 if (copy_to_user(&enum_ptr[copied].name,
2233 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2240 out_resp->count_enum_blobs = enum_count;
2243 if (property->flags & DRM_MODE_PROP_BLOB) {
2244 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2246 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2247 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2249 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2250 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2255 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2263 out_resp->count_enum_blobs = enum_count;
2266 mutex_unlock(&dev->mode_config.mutex);
2270 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2273 struct drm_property_blob *blob;
2275 if (!length || !data)
2278 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2282 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2283 blob->length = length;
2285 memcpy(blob->data, data, length);
2287 blob->id = drm_idr_get(dev, blob);
2289 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2293 static void drm_property_destroy_blob(struct drm_device *dev,
2294 struct drm_property_blob *blob)
2296 drm_idr_put(dev, blob->id);
2297 list_del(&blob->head);
2301 int drm_mode_getblob_ioctl(struct drm_device *dev,
2302 void *data, struct drm_file *file_priv)
2304 struct drm_mode_get_blob *out_resp = data;
2305 struct drm_property_blob *blob;
2309 mutex_lock(&dev->mode_config.mutex);
2311 blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2312 if (!blob || (blob->id != out_resp->blob_id)) {
2317 if (out_resp->length == blob->length) {
2318 blob_ptr = (void *)(unsigned long)out_resp->data;
2319 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2324 out_resp->length = blob->length;
2327 mutex_unlock(&dev->mode_config.mutex);
2331 int drm_mode_output_update_edid_property(struct drm_output *output, struct edid *edid)
2333 struct drm_device *dev = output->dev;
2335 if (output->edid_blob_ptr)
2336 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2338 output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2340 ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2343 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2345 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2346 void *data, struct drm_file *file_priv)
2348 struct drm_mode_output_set_property *out_resp = data;
2349 struct drm_property *property;
2350 struct drm_output *output;
2354 mutex_lock(&dev->mode_config.mutex);
2355 output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2356 if (!output || (output->id != out_resp->output_id)) {
2360 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2361 if (output->property_ids[i] == out_resp->prop_id)
2365 if (i == DRM_OUTPUT_MAX_PROPERTY) {
2369 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2370 if (!property || (property->id != out_resp->prop_id)) {
2374 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2377 if (property->flags & DRM_MODE_PROP_RANGE) {
2378 if (out_resp->value < property->values[0])
2381 if (out_resp->value > property->values[1])
2385 for (i = 0; i < property->num_values; i++) {
2386 if (property->values[i] == out_resp->value) {
2396 if (output->funcs->set_property)
2397 ret = output->funcs->set_property(output, property, out_resp->value);
2400 mutex_unlock(&dev->mode_config.mutex);