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" },
64 * drm_idr_get - allocate a new identifier
66 * @ptr: object pointer, used to generate unique ID
69 * Caller must hold DRM mode_config lock.
71 * Create a unique identifier based on @ptr in @dev's identifier space. Used
72 * for tracking modes, CRTCs and outputs.
75 * New unique (relative to other objects in @dev) integer identifier for the
78 int drm_idr_get(struct drm_device *dev, void *ptr)
83 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
84 DRM_ERROR("Ran out memory getting a mode number\n");
88 ret = idr_get_new_above(&dev->mode_config.crtc_idr, ptr, 1, &new_id);
96 * drm_idr_put - free an identifer
101 * Caller must hold DRM mode_config lock.
103 * Free @id from @dev's unique identifier pool.
105 void drm_idr_put(struct drm_device *dev, int id)
107 idr_remove(&dev->mode_config.crtc_idr, id);
111 * drm_crtc_from_fb - find the CRTC structure associated with an fb
113 * @fb: framebuffer in question
116 * Caller must hold mode_config lock.
118 * Find CRTC in the mode_config structure that matches @fb.
121 * Pointer to the CRTC or NULL if it wasn't found.
123 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
124 struct drm_framebuffer *fb)
126 struct drm_crtc *crtc;
128 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
136 * drm_framebuffer_create - create a new framebuffer object
140 * Caller must hold mode config lock.
142 * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
145 * Pointer to new framebuffer or NULL on error.
147 struct drm_framebuffer *drm_framebuffer_create(struct drm_device *dev)
149 struct drm_framebuffer *fb;
151 /* Limit to single framebuffer for now */
152 if (dev->mode_config.num_fb > 1) {
153 mutex_unlock(&dev->mode_config.mutex);
154 DRM_ERROR("Attempt to add multiple framebuffers failed\n");
158 fb = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
162 fb->id = drm_idr_get(dev, fb);
164 dev->mode_config.num_fb++;
165 list_add(&fb->head, &dev->mode_config.fb_list);
169 EXPORT_SYMBOL(drm_framebuffer_create);
172 * drm_framebuffer_destroy - remove a framebuffer object
173 * @fb: framebuffer to remove
176 * Caller must hold mode config lock.
178 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
179 * it, setting it to NULL.
181 void drm_framebuffer_destroy(struct drm_framebuffer *fb)
183 struct drm_device *dev = fb->dev;
184 struct drm_crtc *crtc;
186 /* remove from any CRTC */
187 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
192 drm_idr_put(dev, fb->id);
194 dev->mode_config.num_fb--;
198 EXPORT_SYMBOL(drm_framebuffer_destroy);
201 * drm_crtc_create - create a new CRTC object
203 * @funcs: callbacks for the new CRTC
206 * Caller must hold mode config lock.
208 * Creates a new CRTC object and adds it to @dev's mode_config structure.
211 * Pointer to new CRTC object or NULL on error.
213 struct drm_crtc *drm_crtc_create(struct drm_device *dev,
214 const struct drm_crtc_funcs *funcs)
216 struct drm_crtc *crtc;
218 crtc = kzalloc(sizeof(struct drm_crtc), GFP_KERNEL);
225 crtc->id = drm_idr_get(dev, crtc);
227 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
228 dev->mode_config.num_crtc++;
232 EXPORT_SYMBOL(drm_crtc_create);
235 * drm_crtc_destroy - remove a CRTC object
236 * @crtc: CRTC to remove
239 * Caller must hold mode config lock.
241 * Cleanup @crtc. Calls @crtc's cleanup function, then removes @crtc from
242 * its associated DRM device's mode_config. Frees it afterwards.
244 void drm_crtc_destroy(struct drm_crtc *crtc)
246 struct drm_device *dev = crtc->dev;
248 if (crtc->funcs->cleanup)
249 (*crtc->funcs->cleanup)(crtc);
251 drm_idr_put(dev, crtc->id);
252 list_del(&crtc->head);
253 dev->mode_config.num_crtc--;
256 EXPORT_SYMBOL(drm_crtc_destroy);
259 * drm_crtc_in_use - check if a given CRTC is in a mode_config
260 * @crtc: CRTC to check
263 * Caller must hold mode config lock.
265 * Walk @crtc's DRM device's mode_config and see if it's in use.
268 * True if @crtc is part of the mode_config, false otherwise.
270 bool drm_crtc_in_use(struct drm_crtc *crtc)
272 struct drm_output *output;
273 struct drm_device *dev = crtc->dev;
274 /* FIXME: Locking around list access? */
275 list_for_each_entry(output, &dev->mode_config.output_list, head)
276 if (output->crtc == crtc)
280 EXPORT_SYMBOL(drm_crtc_in_use);
283 * Detailed mode info for a standard 640x480@60Hz monitor
285 static struct drm_display_mode std_mode[] = {
286 { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
287 752, 800, 0, 480, 490, 492, 525, 0,
288 V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
292 * drm_crtc_probe_output_modes - get complete set of display modes
294 * @maxX: max width for modes
295 * @maxY: max height for modes
298 * Caller must hold mode config lock.
300 * Based on @dev's mode_config layout, scan all the outputs and try to detect
301 * modes on them. Modes will first be added to the output's probed_modes
302 * list, then culled (based on validity and the @maxX, @maxY parameters) and
303 * put into the normal modes list.
305 * Intended to be used either at bootup time or when major configuration
306 * changes have occurred.
308 * FIXME: take into account monitor limits
310 void drm_crtc_probe_single_output_modes(struct drm_output *output, int maxX, int maxY)
312 struct drm_device *dev = output->dev;
313 struct drm_display_mode *mode, *t;
315 //if (maxX == 0 || maxY == 0)
318 /* set all modes to the unverified state */
319 list_for_each_entry_safe(mode, t, &output->modes, head)
320 mode->status = MODE_UNVERIFIED;
322 output->status = (*output->funcs->detect)(output);
324 if (output->status == output_status_disconnected) {
325 DRM_DEBUG("%s is disconnected\n", output->name);
326 /* TODO set EDID to NULL */
330 ret = (*output->funcs->get_modes)(output);
333 drm_mode_output_list_update(output);
337 drm_mode_validate_size(dev, &output->modes, maxX,
339 list_for_each_entry_safe(mode, t, &output->modes, head) {
340 if (mode->status == MODE_OK)
341 mode->status = (*output->funcs->mode_valid)(output,mode);
345 drm_mode_prune_invalid(dev, &output->modes, TRUE);
347 if (list_empty(&output->modes)) {
348 struct drm_display_mode *stdmode;
350 DRM_DEBUG("No valid modes on %s\n", output->name);
352 /* Should we do this here ???
353 * When no valid EDID modes are available we end up
354 * here and bailed in the past, now we add a standard
355 * 640x480@60Hz mode and carry on.
357 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
358 drm_mode_probed_add(output, stdmode);
359 drm_mode_list_concat(&output->probed_modes,
362 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
366 drm_mode_sort(&output->modes);
368 DRM_DEBUG("Probed modes for %s\n", output->name);
369 list_for_each_entry_safe(mode, t, &output->modes, head) {
370 mode->vrefresh = drm_mode_vrefresh(mode);
372 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
373 drm_mode_debug_printmodeline(dev, mode);
377 void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY)
379 struct drm_output *output;
381 list_for_each_entry(output, &dev->mode_config.output_list, head) {
382 drm_crtc_probe_single_output_modes(output, maxX, maxY);
387 * drm_crtc_set_mode - set a mode
388 * @crtc: CRTC to program
394 * Caller must hold mode config lock.
396 * Try to set @mode on @crtc. Give @crtc and its associated outputs a chance
397 * to fixup or reject the mode prior to trying to set it.
400 * True if the mode was set successfully, or false otherwise.
402 bool drm_crtc_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
405 struct drm_device *dev = crtc->dev;
406 struct drm_display_mode *adjusted_mode, saved_mode;
407 int saved_x, saved_y;
408 bool didLock = false;
410 struct drm_output *output;
412 adjusted_mode = drm_mode_duplicate(dev, mode);
414 crtc->enabled = drm_crtc_in_use(crtc);
416 if (!crtc->enabled) {
420 didLock = crtc->funcs->lock(crtc);
422 saved_mode = crtc->mode;
426 /* Update crtc values up front so the driver can rely on them for mode
433 /* XXX short-circuit changes to base location only */
435 /* Pass our mode to the outputs and the CRTC to give them a chance to
436 * adjust it according to limitations or output properties, and also
437 * a chance to reject the mode entirely.
439 list_for_each_entry(output, &dev->mode_config.output_list, head) {
441 if (output->crtc != crtc)
444 if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
449 if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
453 /* Prepare the outputs and CRTCs before setting the mode. */
454 list_for_each_entry(output, &dev->mode_config.output_list, head) {
456 if (output->crtc != crtc)
459 /* Disable the output as the first thing we do. */
460 output->funcs->prepare(output);
463 crtc->funcs->prepare(crtc);
465 /* Set up the DPLL and any output state that needs to adjust or depend
468 crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
470 list_for_each_entry(output, &dev->mode_config.output_list, head) {
472 if (output->crtc != crtc)
475 DRM_INFO("%s: set mode %s %x\n", output->name, mode->name, mode->mode_id);
477 output->funcs->mode_set(output, mode, adjusted_mode);
480 /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
481 crtc->funcs->commit(crtc);
483 list_for_each_entry(output, &dev->mode_config.output_list, head) {
485 if (output->crtc != crtc)
488 output->funcs->commit(output);
490 #if 0 // TODO def RANDR_12_INTERFACE
491 if (output->randr_output)
492 RRPostPendingProperties (output->randr_output);
496 /* XXX free adjustedmode */
497 drm_mode_destroy(dev, adjusted_mode);
500 // if (scrn->pScreen)
501 // drm_crtc_set_screen_sub_pixel_order(dev);
507 crtc->mode = saved_mode;
511 crtc->funcs->unlock (crtc);
515 EXPORT_SYMBOL(drm_crtc_set_mode);
518 * drm_disable_unused_functions - disable unused objects
522 * Caller must hold mode config lock.
524 * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
525 * by calling its dpms function, which should power it off.
527 void drm_disable_unused_functions(struct drm_device *dev)
529 struct drm_output *output;
530 struct drm_crtc *crtc;
532 list_for_each_entry(output, &dev->mode_config.output_list, head) {
534 (*output->funcs->dpms)(output, DPMSModeOff);
537 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
539 crtc->funcs->dpms(crtc, DPMSModeOff);
544 * drm_mode_probed_add - add a mode to the specified output's probed mode list
545 * @output: output the new mode
549 * Caller must hold mode config lock.
551 * Add @mode to @output's mode list for later use.
553 void drm_mode_probed_add(struct drm_output *output,
554 struct drm_display_mode *mode)
556 list_add(&mode->head, &output->probed_modes);
558 EXPORT_SYMBOL(drm_mode_probed_add);
561 * drm_mode_remove - remove and free a mode
562 * @output: output list to modify
563 * @mode: mode to remove
566 * Caller must hold mode config lock.
568 * Remove @mode from @output's mode list, then free it.
570 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
572 list_del(&mode->head);
575 EXPORT_SYMBOL(drm_mode_remove);
578 * drm_output_create - create a new output
580 * @funcs: callbacks for this output
581 * @name: user visible name of the output
584 * Caller must hold @dev's mode_config lock.
586 * Creates a new drm_output structure and adds it to @dev's mode_config
590 * Pointer to the new output or NULL on error.
592 struct drm_output *drm_output_create(struct drm_device *dev,
593 const struct drm_output_funcs *funcs,
596 struct drm_output *output = NULL;
598 output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
603 output->funcs = funcs;
604 output->id = drm_idr_get(dev, output);
606 strncpy(output->name, name, DRM_OUTPUT_LEN);
607 output->name[DRM_OUTPUT_LEN - 1] = 0;
608 output->subpixel_order = SubPixelUnknown;
609 INIT_LIST_HEAD(&output->user_modes);
610 INIT_LIST_HEAD(&output->probed_modes);
611 INIT_LIST_HEAD(&output->modes);
613 /* output_set_monitor(output)? */
614 /* check for output_ignored(output)? */
616 mutex_lock(&dev->mode_config.mutex);
617 list_add_tail(&output->head, &dev->mode_config.output_list);
618 dev->mode_config.num_output++;
620 drm_output_attach_property(output, dev->mode_config.edid_property, 0);
622 drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
624 mutex_unlock(&dev->mode_config.mutex);
629 EXPORT_SYMBOL(drm_output_create);
632 * drm_output_destroy - remove an output
633 * @output: output to remove
636 * Caller must hold @dev's mode_config lock.
638 * Call @output's cleanup function, then remove the output from the DRM
639 * mode_config after freeing @output's modes.
641 void drm_output_destroy(struct drm_output *output)
643 struct drm_device *dev = output->dev;
644 struct drm_display_mode *mode, *t;
646 if (*output->funcs->cleanup)
647 (*output->funcs->cleanup)(output);
649 list_for_each_entry_safe(mode, t, &output->probed_modes, head)
650 drm_mode_remove(output, mode);
652 list_for_each_entry_safe(mode, t, &output->modes, head)
653 drm_mode_remove(output, mode);
655 list_for_each_entry_safe(mode, t, &output->user_modes, head)
656 drm_mode_remove(output, mode);
658 mutex_lock(&dev->mode_config.mutex);
659 drm_idr_put(dev, output->id);
660 list_del(&output->head);
661 mutex_unlock(&dev->mode_config.mutex);
664 EXPORT_SYMBOL(drm_output_destroy);
667 * drm_output_rename - rename an output
668 * @output: output to rename
669 * @name: new user visible name
674 * Simply stuff a new name into @output's name field, based on @name.
677 * True if the name was changed, false otherwise.
679 bool drm_output_rename(struct drm_output *output, const char *name)
684 strncpy(output->name, name, DRM_OUTPUT_LEN);
685 output->name[DRM_OUTPUT_LEN - 1] = 0;
687 DRM_DEBUG("Changed name to %s\n", output->name);
688 // drm_output_set_monitor(output);
689 // if (drm_output_ignored(output))
694 EXPORT_SYMBOL(drm_output_rename);
697 * drm_mode_create - create a new display mode
703 * Create a new drm_display_mode, give it an ID, and return it.
706 * Pointer to new mode on success, NULL on error.
708 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
710 struct drm_display_mode *nmode;
712 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
716 nmode->mode_id = drm_idr_get(dev, nmode);
719 EXPORT_SYMBOL(drm_mode_create);
722 * drm_mode_destroy - remove a mode
724 * @mode: mode to remove
727 * Caller must hold mode config lock.
729 * Free @mode's unique identifier, then free it.
731 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
733 drm_idr_put(dev, mode->mode_id);
737 EXPORT_SYMBOL(drm_mode_destroy);
739 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
743 dev->mode_config.edid_property =
744 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
747 dev->mode_config.dpms_property =
748 drm_property_create(dev, DRM_MODE_PROP_ENUM, "DPMS", 4);
750 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
751 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
753 dev->mode_config.connector_type_property =
754 drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
755 "Connector Type", 10);
756 for (i = 0; i < ARRAY_SIZE(drm_conn_enum_list); i++)
757 drm_property_add_enum(dev->mode_config.connector_type_property, i, drm_conn_enum_list[i].type, drm_conn_enum_list[i].name);
759 dev->mode_config.connector_num_property =
760 drm_property_create(dev, DRM_MODE_PROP_RANGE | DRM_MODE_PROP_IMMUTABLE,
762 dev->mode_config.connector_num_property->values[0] = 0;
763 dev->mode_config.connector_num_property->values[1] = 20;
768 * drm_mode_config_init - initialize DRM mode_configuration structure
772 * None, should happen single threaded at init time.
774 * Initialize @dev's mode_config structure, used for tracking the graphics
775 * configuration of @dev.
777 void drm_mode_config_init(struct drm_device *dev)
779 mutex_init(&dev->mode_config.mutex);
780 INIT_LIST_HEAD(&dev->mode_config.fb_list);
781 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
782 INIT_LIST_HEAD(&dev->mode_config.output_list);
783 INIT_LIST_HEAD(&dev->mode_config.property_list);
784 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
785 idr_init(&dev->mode_config.crtc_idr);
787 drm_mode_create_standard_output_properties(dev);
790 EXPORT_SYMBOL(drm_mode_config_init);
793 * drm_get_buffer_object - find the buffer object for a given handle
795 * @bo: pointer to caller's buffer_object pointer
796 * @handle: handle to lookup
799 * Must take @dev's struct_mutex to protect buffer object lookup.
801 * Given @handle, lookup the buffer object in @dev and put it in the caller's
805 * Zero on success, -EINVAL if the handle couldn't be found.
807 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
809 struct drm_user_object *uo;
810 struct drm_hash_item *hash;
815 mutex_lock(&dev->struct_mutex);
816 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
818 DRM_ERROR("Couldn't find handle.\n");
823 uo = drm_hash_entry(hash, struct drm_user_object, hash);
824 if (uo->type != drm_buffer_type) {
829 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
832 mutex_unlock(&dev->struct_mutex);
837 * drm_pick_crtcs - pick crtcs for output devices
841 * Caller must hold mode config lock.
843 static void drm_pick_crtcs (struct drm_device *dev)
846 struct drm_output *output, *output_equal;
847 struct drm_crtc *crtc;
848 struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
850 list_for_each_entry(output, &dev->mode_config.output_list, head) {
853 /* Don't hook up outputs that are disconnected ??
855 * This is debateable. Do we want fixed /dev/fbX or
856 * dynamic on hotplug (need mode code for that though) ?
858 * If we don't hook up outputs now, then we only create
859 * /dev/fbX for the output that's enabled, that's good as
860 * the users console will be on that output.
862 * If we do hook up outputs that are disconnected now, then
863 * the user may end up having to muck about with the fbcon
864 * map flags to assign his console to the enabled output. Ugh.
866 if (output->status != output_status_connected)
870 list_for_each_entry(des_mode, &output->modes, head) {
871 if (des_mode->type & DRM_MODE_TYPE_PREFERRED)
875 /* No preferred mode, let's just select the first available */
876 if (!des_mode || !(des_mode->type & DRM_MODE_TYPE_PREFERRED)) {
877 list_for_each_entry(des_mode, &output->modes, head) {
884 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
888 if ((output->possible_crtcs & (1 << c)) == 0)
891 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
892 if (output->id == output_equal->id)
895 /* Find out if crtc has been assigned before */
896 if (output_equal->crtc == crtc)
900 #if 1 /* continue for now */
906 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
908 if (output->id == output_equal->id)
911 list_for_each_entry(modes, &output->modes, head) {
912 list_for_each_entry(modes_equal, &output_equal->modes, head) {
913 if (drm_mode_equal (modes, modes_equal)) {
914 if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
915 printk("Cloning %s (0x%lx) to %s (0x%lx)\n",output->name,output->possible_clones,output_equal->name,output_equal->possible_clones);
925 /* crtc has been assigned skip it */
929 /* Found a CRTC to attach to, do it ! */
931 output->crtc->desired_mode = des_mode;
932 output->initial_x = 0;
933 output->initial_y = 0;
934 DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
942 * drm_initial_config - setup a sane initial output configuration
944 * @can_grow: this configuration is growable
947 * Called at init time, must take mode config lock.
949 * Scan the CRTCs and outputs and try to put together an initial setup.
950 * At the moment, this is a cloned configuration across all heads with
951 * a new framebuffer object as the backing store.
954 * Zero if everything went ok, nonzero otherwise.
956 bool drm_initial_config(struct drm_device *dev, bool can_grow)
958 struct drm_output *output;
959 struct drm_crtc *crtc;
962 mutex_lock(&dev->mode_config.mutex);
964 drm_crtc_probe_output_modes(dev, 2048, 2048);
968 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
970 /* can't setup the crtc if there's no assigned mode */
971 if (!crtc->desired_mode)
974 /* Now setup the fbdev for attached crtcs */
975 dev->driver->fb_probe(dev, crtc);
978 /* This is a little screwy, as we've already walked the outputs
979 * above, but it's a little bit of magic too. There's the potential
980 * for things not to get setup above if an existing device gets
981 * re-assigned thus confusing the hardware. By walking the outputs
982 * this fixes up their crtc's.
984 list_for_each_entry(output, &dev->mode_config.output_list, head) {
986 /* can't setup the output if there's no assigned mode */
987 if (!output->crtc || !output->crtc->desired_mode)
990 /* and needs an attached fb */
991 if (output->crtc->fb)
992 drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
995 drm_disable_unused_functions(dev);
997 mutex_unlock(&dev->mode_config.mutex);
1000 EXPORT_SYMBOL(drm_initial_config);
1003 * drm_mode_config_cleanup - free up DRM mode_config info
1007 * Caller must hold mode config lock.
1009 * Free up all the outputs and CRTCs associated with this DRM device, then
1010 * free up the framebuffers and associated buffer objects.
1012 * FIXME: cleanup any dangling user buffer objects too
1014 void drm_mode_config_cleanup(struct drm_device *dev)
1016 struct drm_output *output, *ot;
1017 struct drm_crtc *crtc, *ct;
1018 struct drm_framebuffer *fb, *fbt;
1019 struct drm_property *property, *pt;
1021 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1022 drm_output_destroy(output);
1025 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1026 drm_property_destroy(dev, property);
1029 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1030 if (fb->bo->type != drm_bo_type_kernel)
1031 drm_framebuffer_destroy(fb);
1033 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1036 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1037 drm_crtc_destroy(crtc);
1041 EXPORT_SYMBOL(drm_mode_config_cleanup);
1044 * drm_crtc_set_config - set a new config from userspace
1045 * @crtc: CRTC to setup
1046 * @crtc_info: user provided configuration
1047 * @new_mode: new mode to set
1048 * @output_set: set of outputs for the new config
1049 * @fb: new framebuffer
1052 * Caller must hold mode config lock.
1054 * Setup a new configuration, provided by the user in @crtc_info, and enable
1060 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)
1062 struct drm_device *dev = crtc->dev;
1063 struct drm_crtc **save_crtcs, *new_crtc;
1064 bool save_enabled = crtc->enabled;
1065 bool changed = false;
1066 struct drm_output *output;
1069 save_crtcs = kzalloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc *), GFP_KERNEL);
1076 if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1079 if (new_mode && !drm_mode_equal(new_mode, &crtc->mode))
1082 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1083 save_crtcs[count++] = output->crtc;
1085 if (output->crtc == crtc)
1088 new_crtc = output->crtc;
1090 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1091 if (output_set[ro] == output)
1094 if (new_crtc != output->crtc) {
1096 output->crtc = new_crtc;
1102 crtc->enabled = (new_mode != NULL);
1103 if (new_mode != NULL) {
1104 DRM_DEBUG("attempting to set mode from userspace\n");
1105 drm_mode_debug_printmodeline(dev, new_mode);
1106 if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1108 crtc->enabled = save_enabled;
1110 list_for_each_entry(output, &dev->mode_config.output_list, head)
1111 output->crtc = save_crtcs[count++];
1115 crtc->desired_x = crtc_info->x;
1116 crtc->desired_y = crtc_info->y;
1117 crtc->desired_mode = new_mode;
1119 drm_disable_unused_functions(dev);
1126 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1127 * @out: drm_mode_modeinfo struct to return to the user
1128 * @in: drm_display_mode to use
1133 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1136 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1138 out->clock = in->clock;
1139 out->hdisplay = in->hdisplay;
1140 out->hsync_start = in->hsync_start;
1141 out->hsync_end = in->hsync_end;
1142 out->htotal = in->htotal;
1143 out->hskew = in->hskew;
1144 out->vdisplay = in->vdisplay;
1145 out->vsync_start = in->vsync_start;
1146 out->vsync_end = in->vsync_end;
1147 out->vtotal = in->vtotal;
1148 out->vscan = in->vscan;
1149 out->vrefresh = in->vrefresh;
1150 out->flags = in->flags;
1151 out->type = in->type;
1152 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1153 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1157 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1158 * @out: drm_display_mode to return to the user
1159 * @in: drm_mode_modeinfo to use
1164 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1167 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1169 out->clock = in->clock;
1170 out->hdisplay = in->hdisplay;
1171 out->hsync_start = in->hsync_start;
1172 out->hsync_end = in->hsync_end;
1173 out->htotal = in->htotal;
1174 out->hskew = in->hskew;
1175 out->vdisplay = in->vdisplay;
1176 out->vsync_start = in->vsync_start;
1177 out->vsync_end = in->vsync_end;
1178 out->vtotal = in->vtotal;
1179 out->vscan = in->vscan;
1180 out->vrefresh = in->vrefresh;
1181 out->flags = in->flags;
1182 out->type = in->type;
1183 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1184 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1188 * drm_mode_getresources - get graphics configuration
1189 * @inode: inode from the ioctl
1190 * @filp: file * from the ioctl
1191 * @cmd: cmd from ioctl
1192 * @arg: arg from ioctl
1195 * Takes mode config lock.
1197 * Construct a set of configuration description structures and return
1198 * them to the user, including CRTC, output and framebuffer configuration.
1200 * Called by the user via ioctl.
1203 * Zero on success, errno on failure.
1205 int drm_mode_getresources(struct drm_device *dev,
1206 void *data, struct drm_file *file_priv)
1208 struct drm_mode_card_res *card_res = data;
1209 struct list_head *lh;
1210 struct drm_framebuffer *fb;
1211 struct drm_output *output;
1212 struct drm_crtc *crtc;
1214 int output_count = 0;
1218 uint32_t __user *fb_id;
1219 uint32_t __user *crtc_id;
1220 uint32_t __user *output_id;
1222 mutex_lock(&dev->mode_config.mutex);
1224 list_for_each(lh, &dev->mode_config.fb_list)
1227 list_for_each(lh, &dev->mode_config.crtc_list)
1230 list_for_each(lh, &dev->mode_config.output_list)
1233 card_res->max_height = dev->mode_config.max_height;
1234 card_res->min_height = dev->mode_config.min_height;
1235 card_res->max_width = dev->mode_config.max_width;
1236 card_res->min_width = dev->mode_config.min_width;
1238 /* handle this in 4 parts */
1240 if (card_res->count_fbs >= fb_count) {
1242 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1243 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1244 if (put_user(fb->id, fb_id + copied)) {
1251 card_res->count_fbs = fb_count;
1254 if (card_res->count_crtcs >= crtc_count) {
1256 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1257 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1258 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1259 if (put_user(crtc->id, crtc_id + copied)) {
1266 card_res->count_crtcs = crtc_count;
1270 if (card_res->count_outputs >= output_count) {
1272 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1273 list_for_each_entry(output, &dev->mode_config.output_list,
1275 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1276 if (put_user(output->id, output_id + copied)) {
1283 card_res->count_outputs = output_count;
1285 DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1286 card_res->count_outputs);
1289 mutex_unlock(&dev->mode_config.mutex);
1294 * drm_mode_getcrtc - get CRTC configuration
1295 * @inode: inode from the ioctl
1296 * @filp: file * from the ioctl
1297 * @cmd: cmd from ioctl
1298 * @arg: arg from ioctl
1303 * Construct a CRTC configuration structure to return to the user.
1305 * Called by the user via ioctl.
1308 * Zero on success, errno on failure.
1310 int drm_mode_getcrtc(struct drm_device *dev,
1311 void *data, struct drm_file *file_priv)
1313 struct drm_mode_crtc *crtc_resp = data;
1314 struct drm_crtc *crtc;
1315 struct drm_output *output;
1319 mutex_lock(&dev->mode_config.mutex);
1320 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1321 if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1326 crtc_resp->x = crtc->x;
1327 crtc_resp->y = crtc->y;
1330 crtc_resp->fb_id = crtc->fb->id;
1332 crtc_resp->fb_id = 0;
1334 crtc_resp->outputs = 0;
1335 if (crtc->enabled) {
1337 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1338 crtc_resp->mode_valid = 1;
1340 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1341 if (output->crtc == crtc)
1342 crtc_resp->outputs |= 1 << (ocount++);
1346 crtc_resp->mode_valid = 0;
1350 mutex_unlock(&dev->mode_config.mutex);
1355 * drm_mode_getoutput - get output configuration
1356 * @inode: inode from the ioctl
1357 * @filp: file * from the ioctl
1358 * @cmd: cmd from ioctl
1359 * @arg: arg from ioctl
1364 * Construct a output configuration structure to return to the user.
1366 * Called by the user via ioctl.
1369 * Zero on success, errno on failure.
1371 int drm_mode_getoutput(struct drm_device *dev,
1372 void *data, struct drm_file *file_priv)
1374 struct drm_mode_get_output *out_resp = data;
1375 struct drm_output *output;
1376 struct drm_display_mode *mode;
1378 int props_count = 0;
1382 struct drm_mode_modeinfo u_mode;
1383 struct drm_mode_modeinfo __user *mode_ptr;
1384 uint32_t __user *prop_ptr;
1385 uint64_t __user *prop_values;
1387 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1389 DRM_DEBUG("output id %d:\n", out_resp->output);
1391 mutex_lock(&dev->mode_config.mutex);
1392 output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1393 if (!output || (output->id != out_resp->output)) {
1398 list_for_each_entry(mode, &output->modes, head)
1401 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1402 if (output->property_ids[i] != 0) {
1407 if (out_resp->count_modes == 0) {
1408 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1411 strncpy(out_resp->name, output->name, DRM_OUTPUT_NAME_LEN);
1412 out_resp->name[DRM_OUTPUT_NAME_LEN-1] = 0;
1414 out_resp->mm_width = output->mm_width;
1415 out_resp->mm_height = output->mm_height;
1416 out_resp->subpixel = output->subpixel_order;
1417 out_resp->connection = output->status;
1419 out_resp->crtc = output->crtc->id;
1423 out_resp->crtcs = output->possible_crtcs;
1424 out_resp->clones = output->possible_clones;
1426 if ((out_resp->count_modes >= mode_count) && mode_count) {
1428 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1429 list_for_each_entry(mode, &output->modes, head) {
1430 drm_crtc_convert_to_umode(&u_mode, mode);
1431 if (copy_to_user(mode_ptr + copied,
1432 &u_mode, sizeof(u_mode))) {
1440 out_resp->count_modes = mode_count;
1442 if ((out_resp->count_props >= props_count) && props_count) {
1444 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1445 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1446 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1447 if (output->property_ids[i] != 0) {
1448 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1453 if (put_user(output->property_values[i], prop_values + copied)) {
1461 out_resp->count_props = props_count;
1464 mutex_unlock(&dev->mode_config.mutex);
1469 * drm_mode_setcrtc - set CRTC configuration
1470 * @inode: inode from the ioctl
1471 * @filp: file * from the ioctl
1472 * @cmd: cmd from ioctl
1473 * @arg: arg from ioctl
1478 * Build a new CRTC configuration based on user request.
1480 * Called by the user via ioctl.
1483 * Zero on success, errno on failure.
1485 int drm_mode_setcrtc(struct drm_device *dev,
1486 void *data, struct drm_file *file_priv)
1488 struct drm_mode_crtc *crtc_req = data;
1489 struct drm_crtc *crtc;
1490 struct drm_output **output_set = NULL, *output;
1491 struct drm_framebuffer *fb = NULL;
1492 struct drm_display_mode *mode = NULL;
1495 uint32_t __user *set_outputs_ptr;
1497 mutex_lock(&dev->mode_config.mutex);
1498 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1499 if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1500 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1505 if (crtc_req->mode_valid) {
1506 /* if we have a mode we need a framebuffer */
1507 if (crtc_req->fb_id) {
1508 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1509 if (!fb || (fb->id != crtc_req->fb_id)) {
1510 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1516 mode = drm_mode_create(dev);
1517 drm_crtc_convert_umode(mode, &crtc_req->mode);
1520 if (crtc_req->count_outputs == 0 && mode) {
1521 DRM_DEBUG("Count outputs is 0 but mode set\n");
1526 if (crtc_req->count_outputs > 0 && !mode && !fb) {
1527 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1532 if (crtc_req->count_outputs > 0) {
1534 output_set = kmalloc(crtc_req->count_outputs *
1535 sizeof(struct drm_output *), GFP_KERNEL);
1541 for (i = 0; i < crtc_req->count_outputs; i++) {
1542 set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1543 if (get_user(out_id, &set_outputs_ptr[i])) {
1548 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1549 if (!output || (out_id != output->id)) {
1550 DRM_DEBUG("Output id %d unknown\n", out_id);
1555 output_set[i] = output;
1559 ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1562 mutex_unlock(&dev->mode_config.mutex);
1567 * drm_mode_addfb - add an FB to the graphics configuration
1568 * @inode: inode from the ioctl
1569 * @filp: file * from the ioctl
1570 * @cmd: cmd from ioctl
1571 * @arg: arg from ioctl
1574 * Takes mode config lock.
1576 * Add a new FB to the specified CRTC, given a user request.
1578 * Called by the user via ioctl.
1581 * Zero on success, errno on failure.
1583 int drm_mode_addfb(struct drm_device *dev,
1584 void *data, struct drm_file *file_priv)
1586 struct drm_mode_fb_cmd *r = data;
1587 struct drm_mode_config *config = &dev->mode_config;
1588 struct drm_framebuffer *fb;
1589 struct drm_buffer_object *bo;
1590 struct drm_crtc *crtc;
1593 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1594 DRM_ERROR("mode new framebuffer width not within limits\n");
1597 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1598 DRM_ERROR("mode new framebuffer height not within limits\n");
1602 mutex_lock(&dev->mode_config.mutex);
1603 /* TODO check limits are okay */
1604 ret = drm_get_buffer_object(dev, &bo, r->handle);
1610 /* TODO check buffer is sufficently large */
1611 /* TODO setup destructor callback */
1613 fb = drm_framebuffer_create(dev);
1619 fb->width = r->width;
1620 fb->height = r->height;
1621 fb->pitch = r->pitch;
1622 fb->bits_per_pixel = r->bpp;
1623 fb->depth = r->depth;
1624 fb->offset = bo->offset;
1627 r->buffer_id = fb->id;
1629 list_add(&fb->filp_head, &file_priv->fbs);
1631 /* FIXME: bind the fb to the right crtc */
1632 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1634 dev->driver->fb_probe(dev, crtc);
1638 mutex_unlock(&dev->mode_config.mutex);
1643 * drm_mode_rmfb - remove an FB from the configuration
1644 * @inode: inode from the ioctl
1645 * @filp: file * from the ioctl
1646 * @cmd: cmd from ioctl
1647 * @arg: arg from ioctl
1650 * Takes mode config lock.
1652 * Remove the FB specified by the user.
1654 * Called by the user via ioctl.
1657 * Zero on success, errno on failure.
1659 int drm_mode_rmfb(struct drm_device *dev,
1660 void *data, struct drm_file *file_priv)
1662 struct drm_framebuffer *fb = 0;
1663 uint32_t *id = data;
1666 mutex_lock(&dev->mode_config.mutex);
1667 fb = idr_find(&dev->mode_config.crtc_idr, *id);
1668 /* TODO check that we realy get a framebuffer back. */
1669 if (!fb || (*id != fb->id)) {
1670 DRM_ERROR("mode invalid framebuffer id\n");
1675 /* TODO check if we own the buffer */
1676 /* TODO release all crtc connected to the framebuffer */
1677 /* bind the fb to the crtc for now */
1678 /* TODO unhock the destructor from the buffer object */
1680 if (fb->bo->type != drm_bo_type_kernel)
1681 drm_framebuffer_destroy(fb);
1683 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1686 mutex_unlock(&dev->mode_config.mutex);
1691 * drm_mode_getfb - get FB info
1692 * @inode: inode from the ioctl
1693 * @filp: file * from the ioctl
1694 * @cmd: cmd from ioctl
1695 * @arg: arg from ioctl
1700 * Lookup the FB given its ID and return info about it.
1702 * Called by the user via ioctl.
1705 * Zero on success, errno on failure.
1707 int drm_mode_getfb(struct drm_device *dev,
1708 void *data, struct drm_file *file_priv)
1710 struct drm_mode_fb_cmd *r = data;
1711 struct drm_framebuffer *fb;
1714 mutex_lock(&dev->mode_config.mutex);
1715 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1716 if (!fb || (r->buffer_id != fb->id)) {
1717 DRM_ERROR("invalid framebuffer id\n");
1722 r->height = fb->height;
1723 r->width = fb->width;
1724 r->depth = fb->depth;
1725 r->bpp = fb->bits_per_pixel;
1726 r->handle = fb->bo->base.hash.key;
1727 r->pitch = fb->pitch;
1730 mutex_unlock(&dev->mode_config.mutex);
1735 * drm_fb_release - remove and free the FBs on this file
1736 * @filp: file * from the ioctl
1739 * Takes mode config lock.
1741 * Destroy all the FBs associated with @filp.
1743 * Called by the user via ioctl.
1746 * Zero on success, errno on failure.
1748 void drm_fb_release(struct file *filp)
1750 struct drm_file *priv = filp->private_data;
1751 struct drm_device *dev = priv->head->dev;
1752 struct drm_framebuffer *fb, *tfb;
1754 mutex_lock(&dev->mode_config.mutex);
1755 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1756 list_del(&fb->filp_head);
1757 if (fb->bo->type != drm_bo_type_kernel)
1758 drm_framebuffer_destroy(fb);
1760 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1762 mutex_unlock(&dev->mode_config.mutex);
1769 static int drm_mode_attachmode(struct drm_device *dev,
1770 struct drm_output *output,
1771 struct drm_display_mode *mode)
1775 list_add_tail(&mode->head, &output->user_modes);
1779 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1780 struct drm_display_mode *mode)
1782 struct drm_output *output;
1784 struct drm_display_mode *dup_mode;
1786 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1787 if (output->crtc == crtc) {
1789 dup_mode = drm_mode_duplicate(dev, mode);
1792 ret = drm_mode_attachmode(dev, output, dup_mode);
1800 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1802 static int drm_mode_detachmode(struct drm_device *dev,
1803 struct drm_output *output,
1804 struct drm_display_mode *mode)
1808 struct drm_display_mode *match_mode, *t;
1810 list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1811 if (drm_mode_equal(match_mode, mode)) {
1812 list_del(&match_mode->head);
1813 drm_mode_destroy(dev, match_mode);
1825 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1827 struct drm_output *output;
1829 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1830 drm_mode_detachmode(dev, output, mode);
1834 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1837 * drm_fb_attachmode - Attach a user mode to an output
1838 * @inode: inode from the ioctl
1839 * @filp: file * from the ioctl
1840 * @cmd: cmd from ioctl
1841 * @arg: arg from ioctl
1843 * This attaches a user specified mode to an output.
1844 * Called by the user via ioctl.
1847 * Zero on success, errno on failure.
1849 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1850 void *data, struct drm_file *file_priv)
1852 struct drm_mode_mode_cmd *mode_cmd = data;
1853 struct drm_output *output;
1854 struct drm_display_mode *mode;
1855 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1858 mutex_lock(&dev->mode_config.mutex);
1860 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1861 if (!output || (output->id != mode_cmd->output_id)) {
1866 mode = drm_mode_create(dev);
1872 drm_crtc_convert_umode(mode, umode);
1874 ret = drm_mode_attachmode(dev, output, mode);
1876 mutex_unlock(&dev->mode_config.mutex);
1882 * drm_fb_detachmode - Detach a user specified mode from an output
1883 * @inode: inode from the ioctl
1884 * @filp: file * from the ioctl
1885 * @cmd: cmd from ioctl
1886 * @arg: arg from ioctl
1888 * Called by the user via ioctl.
1891 * Zero on success, errno on failure.
1893 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1894 void *data, struct drm_file *file_priv)
1896 struct drm_mode_mode_cmd *mode_cmd = data;
1897 struct drm_output *output;
1898 struct drm_display_mode mode;
1899 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1902 mutex_lock(&dev->mode_config.mutex);
1904 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1905 if (!output || (output->id != mode_cmd->output_id)) {
1910 drm_crtc_convert_umode(&mode, umode);
1911 ret = drm_mode_detachmode(dev, output, &mode);
1913 mutex_unlock(&dev->mode_config.mutex);
1917 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1918 const char *name, int num_values)
1920 struct drm_property *property = NULL;
1922 property = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
1927 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1928 if (!property->values)
1932 property->id = drm_idr_get(dev, property);
1933 property->flags = flags;
1934 property->num_values = num_values;
1935 INIT_LIST_HEAD(&property->enum_blob_list);
1938 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1940 list_add_tail(&property->head, &dev->mode_config.property_list);
1946 EXPORT_SYMBOL(drm_property_create);
1948 int drm_property_add_enum(struct drm_property *property, int index,
1949 uint64_t value, const char *name)
1951 struct drm_property_enum *prop_enum;
1953 if (!(property->flags & DRM_MODE_PROP_ENUM))
1956 if (!list_empty(&property->enum_blob_list)) {
1957 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1958 if (prop_enum->value == value) {
1959 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1960 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1966 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1970 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1971 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1972 prop_enum->value = value;
1974 property->values[index] = value;
1975 list_add_tail(&prop_enum->head, &property->enum_blob_list);
1978 EXPORT_SYMBOL(drm_property_add_enum);
1980 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1982 struct drm_property_enum *prop_enum, *pt;
1984 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1985 list_del(&prop_enum->head);
1989 if (property->num_values)
1990 kfree(property->values);
1991 drm_idr_put(dev, property->id);
1992 list_del(&property->head);
1995 EXPORT_SYMBOL(drm_property_destroy);
1997 int drm_output_attach_property(struct drm_output *output,
1998 struct drm_property *property, uint64_t init_val)
2002 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2003 if (output->property_ids[i] == 0) {
2004 output->property_ids[i] = property->id;
2005 output->property_values[i] = init_val;
2010 if (i == DRM_OUTPUT_MAX_PROPERTY)
2014 EXPORT_SYMBOL(drm_output_attach_property);
2016 int drm_output_property_set_value(struct drm_output *output,
2017 struct drm_property *property, uint64_t value)
2021 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2022 if (output->property_ids[i] == property->id) {
2023 output->property_values[i] = value;
2028 if (i == DRM_OUTPUT_MAX_PROPERTY)
2032 EXPORT_SYMBOL(drm_output_property_set_value);
2034 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2035 void *data, struct drm_file *file_priv)
2037 struct drm_mode_get_property *out_resp = data;
2038 struct drm_property *property;
2041 int value_count = 0;
2044 struct drm_property_enum *prop_enum;
2045 struct drm_mode_property_enum __user *enum_ptr;
2046 struct drm_property_blob *prop_blob;
2047 uint32_t *blob_id_ptr;
2048 uint64_t __user *values_ptr;
2049 uint32_t __user *blob_length_ptr;
2051 mutex_lock(&dev->mode_config.mutex);
2052 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2053 if (!property || (property->id != out_resp->prop_id)) {
2058 if (property->flags & DRM_MODE_PROP_ENUM) {
2059 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2061 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2062 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2066 value_count = property->num_values;
2068 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2069 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2070 out_resp->flags = property->flags;
2072 if ((out_resp->count_values >= value_count) && value_count) {
2073 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2074 for (i = 0; i < value_count; i++) {
2075 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2081 out_resp->count_values = value_count;
2083 if (property->flags & DRM_MODE_PROP_ENUM) {
2085 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2087 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2088 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2090 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2095 if (copy_to_user(&enum_ptr[copied].name,
2096 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2103 out_resp->count_enum_blobs = enum_count;
2106 if (property->flags & DRM_MODE_PROP_BLOB) {
2107 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2109 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2110 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2112 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2113 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2118 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2126 out_resp->count_enum_blobs = enum_count;
2129 mutex_unlock(&dev->mode_config.mutex);
2133 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2136 struct drm_property_blob *blob;
2138 if (!length || !data)
2141 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2145 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2146 blob->length = length;
2148 memcpy(blob->data, data, length);
2150 blob->id = drm_idr_get(dev, blob);
2152 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2156 static void drm_property_destroy_blob(struct drm_device *dev,
2157 struct drm_property_blob *blob)
2159 drm_idr_put(dev, blob->id);
2160 list_del(&blob->head);
2164 int drm_mode_getblob_ioctl(struct drm_device *dev,
2165 void *data, struct drm_file *file_priv)
2167 struct drm_mode_get_blob *out_resp = data;
2168 struct drm_property_blob *blob;
2172 mutex_lock(&dev->mode_config.mutex);
2174 blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2175 if (!blob || (blob->id != out_resp->blob_id)) {
2180 if (out_resp->length == blob->length) {
2181 blob_ptr = (void *)(unsigned long)out_resp->data;
2182 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2187 out_resp->length = blob->length;
2190 mutex_unlock(&dev->mode_config.mutex);
2194 int drm_mode_output_update_edid_property(struct drm_output *output, unsigned char *edid)
2196 struct drm_device *dev = output->dev;
2198 if (output->edid_blob_ptr)
2199 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2201 output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2203 ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2206 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2208 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2209 void *data, struct drm_file *file_priv)
2211 struct drm_mode_output_set_property *out_resp = data;
2212 struct drm_property *property;
2213 struct drm_output *output;
2217 mutex_lock(&dev->mode_config.mutex);
2218 output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2219 if (!output || (output->id != out_resp->output_id)) {
2223 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2224 if (output->property_ids[i] == out_resp->prop_id)
2228 if (i == DRM_OUTPUT_MAX_PROPERTY) {
2232 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2233 if (!property || (property->id != out_resp->prop_id)) {
2237 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2240 if (property->flags & DRM_MODE_PROP_RANGE) {
2241 if (out_resp->value < property->values[0])
2244 if (out_resp->value > property->values[1])
2248 for (i = 0; i < property->num_values; i++) {
2249 if (property->values[i] == out_resp->value) {
2259 if (output->funcs->set_property)
2260 ret = output->funcs->set_property(output, property, out_resp->value);
2263 mutex_unlock(&dev->mode_config.mutex);