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;
774 EXPORT_SYMBOL(drm_mode_config_init);
777 * drm_get_buffer_object - find the buffer object for a given handle
779 * @bo: pointer to caller's buffer_object pointer
780 * @handle: handle to lookup
783 * Must take @dev's struct_mutex to protect buffer object lookup.
785 * Given @handle, lookup the buffer object in @dev and put it in the caller's
789 * Zero on success, -EINVAL if the handle couldn't be found.
791 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
793 struct drm_user_object *uo;
794 struct drm_hash_item *hash;
799 mutex_lock(&dev->struct_mutex);
800 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
802 DRM_ERROR("Couldn't find handle.\n");
807 uo = drm_hash_entry(hash, struct drm_user_object, hash);
808 if (uo->type != drm_buffer_type) {
813 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
816 mutex_unlock(&dev->struct_mutex);
821 * drm_pick_crtcs - pick crtcs for output devices
825 * Caller must hold mode config lock.
827 static void drm_pick_crtcs (struct drm_device *dev)
830 struct drm_output *output, *output_equal;
831 struct drm_crtc *crtc;
832 struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
834 list_for_each_entry(output, &dev->mode_config.output_list, head) {
837 /* Don't hook up outputs that are disconnected ??
839 * This is debateable. Do we want fixed /dev/fbX or
840 * dynamic on hotplug (need mode code for that though) ?
842 * If we don't hook up outputs now, then we only create
843 * /dev/fbX for the output that's enabled, that's good as
844 * the users console will be on that output.
846 * If we do hook up outputs that are disconnected now, then
847 * the user may end up having to muck about with the fbcon
848 * map flags to assign his console to the enabled output. Ugh.
850 if (output->status != output_status_connected)
854 list_for_each_entry(des_mode, &output->modes, head) {
855 if (des_mode->type & DRM_MODE_TYPE_PREFERRED)
859 /* No preferred mode, let's just select the first available */
860 if (!des_mode || !(des_mode->type & DRM_MODE_TYPE_PREFERRED)) {
861 list_for_each_entry(des_mode, &output->modes, head) {
868 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
872 if ((output->possible_crtcs & (1 << c)) == 0)
875 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
876 if (output->id == output_equal->id)
879 /* Find out if crtc has been assigned before */
880 if (output_equal->crtc == crtc)
884 #if 1 /* continue for now */
890 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
892 if (output->id == output_equal->id)
895 list_for_each_entry(modes, &output->modes, head) {
896 list_for_each_entry(modes_equal, &output_equal->modes, head) {
897 if (drm_mode_equal (modes, modes_equal)) {
898 if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
899 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);
909 /* crtc has been assigned skip it */
913 /* Found a CRTC to attach to, do it ! */
915 output->crtc->desired_mode = des_mode;
916 output->initial_x = 0;
917 output->initial_y = 0;
918 DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
923 EXPORT_SYMBOL(drm_pick_crtcs);
926 * drm_initial_config - setup a sane initial output configuration
928 * @can_grow: this configuration is growable
931 * Called at init time, must take mode config lock.
933 * Scan the CRTCs and outputs and try to put together an initial setup.
934 * At the moment, this is a cloned configuration across all heads with
935 * a new framebuffer object as the backing store.
938 * Zero if everything went ok, nonzero otherwise.
940 bool drm_initial_config(struct drm_device *dev, bool can_grow)
942 struct drm_output *output;
943 struct drm_crtc *crtc;
946 mutex_lock(&dev->mode_config.mutex);
948 drm_crtc_probe_output_modes(dev, 2048, 2048);
952 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
954 /* can't setup the crtc if there's no assigned mode */
955 if (!crtc->desired_mode)
958 /* Now setup the fbdev for attached crtcs */
959 dev->driver->fb_probe(dev, crtc);
962 /* This is a little screwy, as we've already walked the outputs
963 * above, but it's a little bit of magic too. There's the potential
964 * for things not to get setup above if an existing device gets
965 * re-assigned thus confusing the hardware. By walking the outputs
966 * this fixes up their crtc's.
968 list_for_each_entry(output, &dev->mode_config.output_list, head) {
970 /* can't setup the output if there's no assigned mode */
971 if (!output->crtc || !output->crtc->desired_mode)
974 /* and needs an attached fb */
975 if (output->crtc->fb)
976 drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
979 drm_disable_unused_functions(dev);
981 mutex_unlock(&dev->mode_config.mutex);
984 EXPORT_SYMBOL(drm_initial_config);
987 * drm_mode_config_cleanup - free up DRM mode_config info
991 * Caller must hold mode config lock.
993 * Free up all the outputs and CRTCs associated with this DRM device, then
994 * free up the framebuffers and associated buffer objects.
996 * FIXME: cleanup any dangling user buffer objects too
998 void drm_mode_config_cleanup(struct drm_device *dev)
1000 struct drm_output *output, *ot;
1001 struct drm_crtc *crtc, *ct;
1002 struct drm_framebuffer *fb, *fbt;
1003 struct drm_property *property, *pt;
1005 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1006 drm_output_destroy(output);
1009 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1010 drm_property_destroy(dev, property);
1013 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1014 /* there should only be bo of kernel type left */
1015 if (fb->bo->type != drm_bo_type_kernel)
1016 drm_framebuffer_destroy(fb);
1018 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1021 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1022 drm_crtc_destroy(crtc);
1026 EXPORT_SYMBOL(drm_mode_config_cleanup);
1029 * drm_crtc_set_config - set a new config from userspace
1030 * @crtc: CRTC to setup
1031 * @crtc_info: user provided configuration
1032 * @new_mode: new mode to set
1033 * @output_set: set of outputs for the new config
1034 * @fb: new framebuffer
1037 * Caller must hold mode config lock.
1039 * Setup a new configuration, provided by the user in @crtc_info, and enable
1045 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)
1047 struct drm_device *dev = crtc->dev;
1048 struct drm_crtc **save_crtcs, *new_crtc;
1049 bool save_enabled = crtc->enabled;
1050 bool changed = false;
1051 bool flip_or_move = false;
1052 struct drm_output *output;
1055 save_crtcs = kzalloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc *), GFP_KERNEL);
1059 /* We should be able to check here if the fb has the same properties
1060 * and then just flip_or_move it */
1064 if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1065 flip_or_move = true;
1067 if (new_mode && !drm_mode_equal(new_mode, &crtc->mode))
1070 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1071 save_crtcs[count++] = output->crtc;
1073 if (output->crtc == crtc)
1076 new_crtc = output->crtc;
1078 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1079 if (output_set[ro] == output)
1082 if (new_crtc != output->crtc) {
1084 output->crtc = new_crtc;
1088 /* mode_set_base is not a required function */
1089 if (flip_or_move && !crtc->funcs->mode_set_base)
1094 crtc->enabled = (new_mode != NULL);
1095 if (new_mode != NULL) {
1096 DRM_DEBUG("attempting to set mode from userspace\n");
1097 drm_mode_debug_printmodeline(dev, new_mode);
1098 if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1100 crtc->enabled = save_enabled;
1102 list_for_each_entry(output, &dev->mode_config.output_list, head)
1103 output->crtc = save_crtcs[count++];
1107 crtc->desired_x = crtc_info->x;
1108 crtc->desired_y = crtc_info->y;
1109 crtc->desired_mode = new_mode;
1111 drm_disable_unused_functions(dev);
1112 } else if (flip_or_move) {
1113 crtc->funcs->mode_set_base(crtc, crtc_info->x, crtc_info->y);
1121 * drm_hotplug_stage_two
1123 * @output hotpluged output
1126 * Caller must hold mode config lock, function might grap struct lock.
1128 * Stage two of a hotplug.
1131 * Zero on success, errno on failure.
1133 int drm_hotplug_stage_two(struct drm_device *dev, struct drm_output *output)
1137 if (output->crtc && output->crtc->desired_mode) {
1138 DRM_DEBUG("drm thinks that the output already has a config\n");
1142 drm_crtc_probe_output_modes(dev, 2048, 2048);
1145 drm_pick_crtcs(dev);
1147 if (!output->crtc || !output->crtc->desired_mode) {
1148 DRM_DEBUG("could not find a desired mode or crtc for output\n");
1152 /* We should realy check if there is a fb using this crtc */
1154 dev->driver->fb_probe(dev, output->crtc);
1156 dev->driver->fb_resize(dev, output->crtc);
1158 if (!drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0))
1159 DRM_ERROR("failed to set mode after hotplug\n");
1162 drm_disable_unused_functions(dev);
1166 EXPORT_SYMBOL(drm_hotplug_stage_two);
1169 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1170 * @out: drm_mode_modeinfo struct to return to the user
1171 * @in: drm_display_mode to use
1176 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1179 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1181 out->clock = in->clock;
1182 out->hdisplay = in->hdisplay;
1183 out->hsync_start = in->hsync_start;
1184 out->hsync_end = in->hsync_end;
1185 out->htotal = in->htotal;
1186 out->hskew = in->hskew;
1187 out->vdisplay = in->vdisplay;
1188 out->vsync_start = in->vsync_start;
1189 out->vsync_end = in->vsync_end;
1190 out->vtotal = in->vtotal;
1191 out->vscan = in->vscan;
1192 out->vrefresh = in->vrefresh;
1193 out->flags = in->flags;
1194 out->type = in->type;
1195 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1196 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1200 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1201 * @out: drm_display_mode to return to the user
1202 * @in: drm_mode_modeinfo to use
1207 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1210 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1212 out->clock = in->clock;
1213 out->hdisplay = in->hdisplay;
1214 out->hsync_start = in->hsync_start;
1215 out->hsync_end = in->hsync_end;
1216 out->htotal = in->htotal;
1217 out->hskew = in->hskew;
1218 out->vdisplay = in->vdisplay;
1219 out->vsync_start = in->vsync_start;
1220 out->vsync_end = in->vsync_end;
1221 out->vtotal = in->vtotal;
1222 out->vscan = in->vscan;
1223 out->vrefresh = in->vrefresh;
1224 out->flags = in->flags;
1225 out->type = in->type;
1226 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1227 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1231 * drm_mode_getresources - get graphics configuration
1232 * @inode: inode from the ioctl
1233 * @filp: file * from the ioctl
1234 * @cmd: cmd from ioctl
1235 * @arg: arg from ioctl
1238 * Takes mode config lock.
1240 * Construct a set of configuration description structures and return
1241 * them to the user, including CRTC, output and framebuffer configuration.
1243 * Called by the user via ioctl.
1246 * Zero on success, errno on failure.
1248 int drm_mode_getresources(struct drm_device *dev,
1249 void *data, struct drm_file *file_priv)
1251 struct drm_mode_card_res *card_res = data;
1252 struct list_head *lh;
1253 struct drm_framebuffer *fb;
1254 struct drm_output *output;
1255 struct drm_crtc *crtc;
1257 int output_count = 0;
1261 uint32_t __user *fb_id;
1262 uint32_t __user *crtc_id;
1263 uint32_t __user *output_id;
1265 mutex_lock(&dev->mode_config.mutex);
1267 list_for_each(lh, &dev->mode_config.fb_list)
1270 list_for_each(lh, &dev->mode_config.crtc_list)
1273 list_for_each(lh, &dev->mode_config.output_list)
1276 card_res->max_height = dev->mode_config.max_height;
1277 card_res->min_height = dev->mode_config.min_height;
1278 card_res->max_width = dev->mode_config.max_width;
1279 card_res->min_width = dev->mode_config.min_width;
1281 /* handle this in 4 parts */
1283 if (card_res->count_fbs >= fb_count) {
1285 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1286 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1287 if (put_user(fb->id, fb_id + copied)) {
1294 card_res->count_fbs = fb_count;
1297 if (card_res->count_crtcs >= crtc_count) {
1299 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1300 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1301 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1302 if (put_user(crtc->id, crtc_id + copied)) {
1309 card_res->count_crtcs = crtc_count;
1313 if (card_res->count_outputs >= output_count) {
1315 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1316 list_for_each_entry(output, &dev->mode_config.output_list,
1318 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1319 if (put_user(output->id, output_id + copied)) {
1326 card_res->count_outputs = output_count;
1328 DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1329 card_res->count_outputs);
1332 mutex_unlock(&dev->mode_config.mutex);
1337 * drm_mode_getcrtc - get CRTC configuration
1338 * @inode: inode from the ioctl
1339 * @filp: file * from the ioctl
1340 * @cmd: cmd from ioctl
1341 * @arg: arg from ioctl
1346 * Construct a CRTC configuration structure to return to the user.
1348 * Called by the user via ioctl.
1351 * Zero on success, errno on failure.
1353 int drm_mode_getcrtc(struct drm_device *dev,
1354 void *data, struct drm_file *file_priv)
1356 struct drm_mode_crtc *crtc_resp = data;
1357 struct drm_crtc *crtc;
1358 struct drm_output *output;
1362 mutex_lock(&dev->mode_config.mutex);
1363 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1364 if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1369 crtc_resp->x = crtc->x;
1370 crtc_resp->y = crtc->y;
1373 crtc_resp->fb_id = crtc->fb->id;
1375 crtc_resp->fb_id = 0;
1377 crtc_resp->outputs = 0;
1378 if (crtc->enabled) {
1380 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1381 crtc_resp->mode_valid = 1;
1383 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1384 if (output->crtc == crtc)
1385 crtc_resp->outputs |= 1 << (ocount++);
1389 crtc_resp->mode_valid = 0;
1393 mutex_unlock(&dev->mode_config.mutex);
1398 * drm_mode_getoutput - get output configuration
1399 * @inode: inode from the ioctl
1400 * @filp: file * from the ioctl
1401 * @cmd: cmd from ioctl
1402 * @arg: arg from ioctl
1407 * Construct a output configuration structure to return to the user.
1409 * Called by the user via ioctl.
1412 * Zero on success, errno on failure.
1414 int drm_mode_getoutput(struct drm_device *dev,
1415 void *data, struct drm_file *file_priv)
1417 struct drm_mode_get_output *out_resp = data;
1418 struct drm_output *output;
1419 struct drm_display_mode *mode;
1421 int props_count = 0;
1425 struct drm_mode_modeinfo u_mode;
1426 struct drm_mode_modeinfo __user *mode_ptr;
1427 uint32_t __user *prop_ptr;
1428 uint64_t __user *prop_values;
1430 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1432 DRM_DEBUG("output id %d:\n", out_resp->output);
1434 mutex_lock(&dev->mode_config.mutex);
1435 output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1436 if (!output || (output->id != out_resp->output)) {
1441 list_for_each_entry(mode, &output->modes, head)
1444 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1445 if (output->property_ids[i] != 0) {
1450 if (out_resp->count_modes == 0) {
1451 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1454 out_resp->output_type = output->output_type;
1455 out_resp->output_type_id = output->output_type_id;
1456 out_resp->mm_width = output->mm_width;
1457 out_resp->mm_height = output->mm_height;
1458 out_resp->subpixel = output->subpixel_order;
1459 out_resp->connection = output->status;
1461 out_resp->crtc = output->crtc->id;
1465 out_resp->crtcs = output->possible_crtcs;
1466 out_resp->clones = output->possible_clones;
1468 if ((out_resp->count_modes >= mode_count) && mode_count) {
1470 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1471 list_for_each_entry(mode, &output->modes, head) {
1472 drm_crtc_convert_to_umode(&u_mode, mode);
1473 if (copy_to_user(mode_ptr + copied,
1474 &u_mode, sizeof(u_mode))) {
1482 out_resp->count_modes = mode_count;
1484 if ((out_resp->count_props >= props_count) && props_count) {
1486 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1487 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1488 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1489 if (output->property_ids[i] != 0) {
1490 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1495 if (put_user(output->property_values[i], prop_values + copied)) {
1503 out_resp->count_props = props_count;
1506 mutex_unlock(&dev->mode_config.mutex);
1511 * drm_mode_setcrtc - set CRTC configuration
1512 * @inode: inode from the ioctl
1513 * @filp: file * from the ioctl
1514 * @cmd: cmd from ioctl
1515 * @arg: arg from ioctl
1520 * Build a new CRTC configuration based on user request.
1522 * Called by the user via ioctl.
1525 * Zero on success, errno on failure.
1527 int drm_mode_setcrtc(struct drm_device *dev,
1528 void *data, struct drm_file *file_priv)
1530 struct drm_mode_crtc *crtc_req = data;
1531 struct drm_crtc *crtc;
1532 struct drm_output **output_set = NULL, *output;
1533 struct drm_framebuffer *fb = NULL;
1534 struct drm_display_mode *mode = NULL;
1537 uint32_t __user *set_outputs_ptr;
1539 mutex_lock(&dev->mode_config.mutex);
1540 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1541 if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1542 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1547 if (crtc_req->mode_valid) {
1548 /* if we have a mode we need a framebuffer */
1549 if (crtc_req->fb_id) {
1550 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1551 if (!fb || (fb->id != crtc_req->fb_id)) {
1552 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1558 mode = drm_mode_create(dev);
1559 drm_crtc_convert_umode(mode, &crtc_req->mode);
1560 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1563 if (crtc_req->count_outputs == 0 && mode) {
1564 DRM_DEBUG("Count outputs is 0 but mode set\n");
1569 if (crtc_req->count_outputs > 0 && !mode && !fb) {
1570 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1575 if (crtc_req->count_outputs > 0) {
1577 /* Maybe we should check that count_outputs is a sensible value. */
1578 output_set = kmalloc(crtc_req->count_outputs *
1579 sizeof(struct drm_output *), GFP_KERNEL);
1585 for (i = 0; i < crtc_req->count_outputs; i++) {
1586 set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1587 if (get_user(out_id, &set_outputs_ptr[i])) {
1592 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1593 if (!output || (out_id != output->id)) {
1594 DRM_DEBUG("Output id %d unknown\n", out_id);
1599 output_set[i] = output;
1603 /* What happens to output_set, leak? */
1604 ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1607 mutex_unlock(&dev->mode_config.mutex);
1612 * drm_mode_addfb - add an FB to the graphics configuration
1613 * @inode: inode from the ioctl
1614 * @filp: file * from the ioctl
1615 * @cmd: cmd from ioctl
1616 * @arg: arg from ioctl
1619 * Takes mode config lock.
1621 * Add a new FB to the specified CRTC, given a user request.
1623 * Called by the user via ioctl.
1626 * Zero on success, errno on failure.
1628 int drm_mode_addfb(struct drm_device *dev,
1629 void *data, struct drm_file *file_priv)
1631 struct drm_mode_fb_cmd *r = data;
1632 struct drm_mode_config *config = &dev->mode_config;
1633 struct drm_framebuffer *fb;
1634 struct drm_buffer_object *bo;
1637 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1638 DRM_ERROR("mode new framebuffer width not within limits\n");
1641 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1642 DRM_ERROR("mode new framebuffer height not within limits\n");
1646 mutex_lock(&dev->mode_config.mutex);
1647 /* TODO check limits are okay */
1648 ret = drm_get_buffer_object(dev, &bo, r->handle);
1650 DRM_ERROR("BO handle not valid\n");
1655 /* TODO check buffer is sufficently large */
1656 /* TODO setup destructor callback */
1658 fb = drm_framebuffer_create(dev);
1660 DRM_ERROR("could not create framebuffer\n");
1665 fb->width = r->width;
1666 fb->height = r->height;
1667 fb->pitch = r->pitch;
1668 fb->bits_per_pixel = r->bpp;
1669 fb->depth = r->depth;
1670 fb->offset = bo->offset;
1673 r->buffer_id = fb->id;
1675 list_add(&fb->filp_head, &file_priv->fbs);
1678 mutex_unlock(&dev->mode_config.mutex);
1683 * drm_mode_rmfb - remove an FB from the configuration
1684 * @inode: inode from the ioctl
1685 * @filp: file * from the ioctl
1686 * @cmd: cmd from ioctl
1687 * @arg: arg from ioctl
1690 * Takes mode config lock.
1692 * Remove the FB specified by the user.
1694 * Called by the user via ioctl.
1697 * Zero on success, errno on failure.
1699 int drm_mode_rmfb(struct drm_device *dev,
1700 void *data, struct drm_file *file_priv)
1702 struct drm_framebuffer *fb = 0;
1703 struct drm_framebuffer *fbl = 0;
1704 uint32_t *id = data;
1708 mutex_lock(&dev->mode_config.mutex);
1709 fb = idr_find(&dev->mode_config.crtc_idr, *id);
1710 /* TODO check that we realy get a framebuffer back. */
1711 if (!fb || (*id != fb->id)) {
1712 DRM_ERROR("mode invalid framebuffer id\n");
1717 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1722 DRM_ERROR("tried to remove a fb that we didn't own\n");
1727 /* TODO release all crtc connected to the framebuffer */
1728 /* TODO unhock the destructor from the buffer object */
1730 if (fb->bo->type == drm_bo_type_kernel)
1731 DRM_ERROR("the bo type should not be of kernel type\n");
1733 list_del(&fb->filp_head);
1734 drm_framebuffer_destroy(fb);
1737 mutex_unlock(&dev->mode_config.mutex);
1742 * drm_mode_getfb - get FB info
1743 * @inode: inode from the ioctl
1744 * @filp: file * from the ioctl
1745 * @cmd: cmd from ioctl
1746 * @arg: arg from ioctl
1751 * Lookup the FB given its ID and return info about it.
1753 * Called by the user via ioctl.
1756 * Zero on success, errno on failure.
1758 int drm_mode_getfb(struct drm_device *dev,
1759 void *data, struct drm_file *file_priv)
1761 struct drm_mode_fb_cmd *r = data;
1762 struct drm_framebuffer *fb;
1765 mutex_lock(&dev->mode_config.mutex);
1766 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1767 if (!fb || (r->buffer_id != fb->id)) {
1768 DRM_ERROR("invalid framebuffer id\n");
1773 r->height = fb->height;
1774 r->width = fb->width;
1775 r->depth = fb->depth;
1776 r->bpp = fb->bits_per_pixel;
1777 r->handle = fb->bo->base.hash.key;
1778 r->pitch = fb->pitch;
1781 mutex_unlock(&dev->mode_config.mutex);
1786 * drm_fb_release - remove and free the FBs on this file
1787 * @filp: file * from the ioctl
1790 * Takes mode config lock.
1792 * Destroy all the FBs associated with @filp.
1794 * Called by the user via ioctl.
1797 * Zero on success, errno on failure.
1799 void drm_fb_release(struct file *filp)
1801 struct drm_file *priv = filp->private_data;
1802 struct drm_device *dev = priv->minor->dev;
1803 struct drm_framebuffer *fb, *tfb;
1805 mutex_lock(&dev->mode_config.mutex);
1806 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1807 list_del(&fb->filp_head);
1808 if (fb->bo->type == drm_bo_type_kernel)
1809 DRM_ERROR("the bo type should not be of kernel_type, the kernel will probably explode, why Dave\n");
1811 drm_framebuffer_destroy(fb);
1813 mutex_unlock(&dev->mode_config.mutex);
1820 static int drm_mode_attachmode(struct drm_device *dev,
1821 struct drm_output *output,
1822 struct drm_display_mode *mode)
1826 list_add_tail(&mode->head, &output->user_modes);
1830 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1831 struct drm_display_mode *mode)
1833 struct drm_output *output;
1835 struct drm_display_mode *dup_mode;
1837 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1838 if (output->crtc == crtc) {
1840 dup_mode = drm_mode_duplicate(dev, mode);
1843 ret = drm_mode_attachmode(dev, output, dup_mode);
1851 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1853 static int drm_mode_detachmode(struct drm_device *dev,
1854 struct drm_output *output,
1855 struct drm_display_mode *mode)
1859 struct drm_display_mode *match_mode, *t;
1861 list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1862 if (drm_mode_equal(match_mode, mode)) {
1863 list_del(&match_mode->head);
1864 drm_mode_destroy(dev, match_mode);
1876 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1878 struct drm_output *output;
1880 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1881 drm_mode_detachmode(dev, output, mode);
1885 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1888 * drm_fb_attachmode - Attach a user mode to an output
1889 * @inode: inode from the ioctl
1890 * @filp: file * from the ioctl
1891 * @cmd: cmd from ioctl
1892 * @arg: arg from ioctl
1894 * This attaches a user specified mode to an output.
1895 * Called by the user via ioctl.
1898 * Zero on success, errno on failure.
1900 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1901 void *data, struct drm_file *file_priv)
1903 struct drm_mode_mode_cmd *mode_cmd = data;
1904 struct drm_output *output;
1905 struct drm_display_mode *mode;
1906 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1909 mutex_lock(&dev->mode_config.mutex);
1911 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1912 if (!output || (output->id != mode_cmd->output_id)) {
1917 mode = drm_mode_create(dev);
1923 drm_crtc_convert_umode(mode, umode);
1925 ret = drm_mode_attachmode(dev, output, mode);
1927 mutex_unlock(&dev->mode_config.mutex);
1933 * drm_fb_detachmode - Detach a user specified mode from an output
1934 * @inode: inode from the ioctl
1935 * @filp: file * from the ioctl
1936 * @cmd: cmd from ioctl
1937 * @arg: arg from ioctl
1939 * Called by the user via ioctl.
1942 * Zero on success, errno on failure.
1944 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1945 void *data, struct drm_file *file_priv)
1947 struct drm_mode_mode_cmd *mode_cmd = data;
1948 struct drm_output *output;
1949 struct drm_display_mode mode;
1950 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1953 mutex_lock(&dev->mode_config.mutex);
1955 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1956 if (!output || (output->id != mode_cmd->output_id)) {
1961 drm_crtc_convert_umode(&mode, umode);
1962 ret = drm_mode_detachmode(dev, output, &mode);
1964 mutex_unlock(&dev->mode_config.mutex);
1968 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1969 const char *name, int num_values)
1971 struct drm_property *property = NULL;
1973 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1978 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1979 if (!property->values)
1983 property->id = drm_idr_get(dev, property);
1984 property->flags = flags;
1985 property->num_values = num_values;
1986 INIT_LIST_HEAD(&property->enum_blob_list);
1989 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1991 list_add_tail(&property->head, &dev->mode_config.property_list);
1997 EXPORT_SYMBOL(drm_property_create);
1999 int drm_property_add_enum(struct drm_property *property, int index,
2000 uint64_t value, const char *name)
2002 struct drm_property_enum *prop_enum;
2004 if (!(property->flags & DRM_MODE_PROP_ENUM))
2007 if (!list_empty(&property->enum_blob_list)) {
2008 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2009 if (prop_enum->value == value) {
2010 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2011 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2017 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2021 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2022 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2023 prop_enum->value = value;
2025 property->values[index] = value;
2026 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2029 EXPORT_SYMBOL(drm_property_add_enum);
2031 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2033 struct drm_property_enum *prop_enum, *pt;
2035 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2036 list_del(&prop_enum->head);
2040 if (property->num_values)
2041 kfree(property->values);
2042 drm_idr_put(dev, property->id);
2043 list_del(&property->head);
2046 EXPORT_SYMBOL(drm_property_destroy);
2048 int drm_output_attach_property(struct drm_output *output,
2049 struct drm_property *property, uint64_t init_val)
2053 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2054 if (output->property_ids[i] == 0) {
2055 output->property_ids[i] = property->id;
2056 output->property_values[i] = init_val;
2061 if (i == DRM_OUTPUT_MAX_PROPERTY)
2065 EXPORT_SYMBOL(drm_output_attach_property);
2067 int drm_output_property_set_value(struct drm_output *output,
2068 struct drm_property *property, uint64_t value)
2072 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2073 if (output->property_ids[i] == property->id) {
2074 output->property_values[i] = value;
2079 if (i == DRM_OUTPUT_MAX_PROPERTY)
2083 EXPORT_SYMBOL(drm_output_property_set_value);
2085 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2086 void *data, struct drm_file *file_priv)
2088 struct drm_mode_get_property *out_resp = data;
2089 struct drm_property *property;
2092 int value_count = 0;
2095 struct drm_property_enum *prop_enum;
2096 struct drm_mode_property_enum __user *enum_ptr;
2097 struct drm_property_blob *prop_blob;
2098 uint32_t *blob_id_ptr;
2099 uint64_t __user *values_ptr;
2100 uint32_t __user *blob_length_ptr;
2102 mutex_lock(&dev->mode_config.mutex);
2103 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2104 if (!property || (property->id != out_resp->prop_id)) {
2109 if (property->flags & DRM_MODE_PROP_ENUM) {
2110 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2112 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2113 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2117 value_count = property->num_values;
2119 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2120 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2121 out_resp->flags = property->flags;
2123 if ((out_resp->count_values >= value_count) && value_count) {
2124 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2125 for (i = 0; i < value_count; i++) {
2126 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2132 out_resp->count_values = value_count;
2134 if (property->flags & DRM_MODE_PROP_ENUM) {
2136 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2138 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2139 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2141 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2146 if (copy_to_user(&enum_ptr[copied].name,
2147 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2154 out_resp->count_enum_blobs = enum_count;
2157 if (property->flags & DRM_MODE_PROP_BLOB) {
2158 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2160 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2161 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2163 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2164 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2169 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2177 out_resp->count_enum_blobs = enum_count;
2180 mutex_unlock(&dev->mode_config.mutex);
2184 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2187 struct drm_property_blob *blob;
2189 if (!length || !data)
2192 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2196 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2197 blob->length = length;
2199 memcpy(blob->data, data, length);
2201 blob->id = drm_idr_get(dev, blob);
2203 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2207 static void drm_property_destroy_blob(struct drm_device *dev,
2208 struct drm_property_blob *blob)
2210 drm_idr_put(dev, blob->id);
2211 list_del(&blob->head);
2215 int drm_mode_getblob_ioctl(struct drm_device *dev,
2216 void *data, struct drm_file *file_priv)
2218 struct drm_mode_get_blob *out_resp = data;
2219 struct drm_property_blob *blob;
2223 mutex_lock(&dev->mode_config.mutex);
2225 blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2226 if (!blob || (blob->id != out_resp->blob_id)) {
2231 if (out_resp->length == blob->length) {
2232 blob_ptr = (void *)(unsigned long)out_resp->data;
2233 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2238 out_resp->length = blob->length;
2241 mutex_unlock(&dev->mode_config.mutex);
2245 int drm_mode_output_update_edid_property(struct drm_output *output, unsigned char *edid)
2247 struct drm_device *dev = output->dev;
2249 if (output->edid_blob_ptr)
2250 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2252 output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2254 ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2257 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2259 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2260 void *data, struct drm_file *file_priv)
2262 struct drm_mode_output_set_property *out_resp = data;
2263 struct drm_property *property;
2264 struct drm_output *output;
2268 mutex_lock(&dev->mode_config.mutex);
2269 output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2270 if (!output || (output->id != out_resp->output_id)) {
2274 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2275 if (output->property_ids[i] == out_resp->prop_id)
2279 if (i == DRM_OUTPUT_MAX_PROPERTY) {
2283 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2284 if (!property || (property->id != out_resp->prop_id)) {
2288 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2291 if (property->flags & DRM_MODE_PROP_RANGE) {
2292 if (out_resp->value < property->values[0])
2295 if (out_resp->value > property->values[1])
2299 for (i = 0; i < property->num_values; i++) {
2300 if (property->values[i] == out_resp->value) {
2310 if (output->funcs->set_property)
2311 ret = output->funcs->set_property(output, property, out_resp->value);
2314 mutex_unlock(&dev->mode_config.mutex);