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);
385 EXPORT_SYMBOL(drm_crtc_probe_output_modes);
388 * drm_crtc_set_mode - set a mode
389 * @crtc: CRTC to program
395 * Caller must hold mode config lock.
397 * Try to set @mode on @crtc. Give @crtc and its associated outputs a chance
398 * to fixup or reject the mode prior to trying to set it.
401 * True if the mode was set successfully, or false otherwise.
403 bool drm_crtc_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
406 struct drm_device *dev = crtc->dev;
407 struct drm_display_mode *adjusted_mode, saved_mode;
408 int saved_x, saved_y;
409 bool didLock = false;
411 struct drm_output *output;
413 adjusted_mode = drm_mode_duplicate(dev, mode);
415 crtc->enabled = drm_crtc_in_use(crtc);
417 if (!crtc->enabled) {
421 didLock = crtc->funcs->lock(crtc);
423 saved_mode = crtc->mode;
427 /* Update crtc values up front so the driver can rely on them for mode
434 /* XXX short-circuit changes to base location only */
436 /* Pass our mode to the outputs and the CRTC to give them a chance to
437 * adjust it according to limitations or output properties, and also
438 * a chance to reject the mode entirely.
440 list_for_each_entry(output, &dev->mode_config.output_list, head) {
442 if (output->crtc != crtc)
445 if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
450 if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
454 /* Prepare the outputs and CRTCs before setting the mode. */
455 list_for_each_entry(output, &dev->mode_config.output_list, head) {
457 if (output->crtc != crtc)
460 /* Disable the output as the first thing we do. */
461 output->funcs->prepare(output);
464 crtc->funcs->prepare(crtc);
466 /* Set up the DPLL and any output state that needs to adjust or depend
469 crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
471 list_for_each_entry(output, &dev->mode_config.output_list, head) {
473 if (output->crtc != crtc)
476 DRM_INFO("%s: set mode %s %x\n", output->name, mode->name, mode->mode_id);
478 output->funcs->mode_set(output, mode, adjusted_mode);
481 /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
482 crtc->funcs->commit(crtc);
484 list_for_each_entry(output, &dev->mode_config.output_list, head) {
486 if (output->crtc != crtc)
489 output->funcs->commit(output);
491 #if 0 // TODO def RANDR_12_INTERFACE
492 if (output->randr_output)
493 RRPostPendingProperties (output->randr_output);
497 /* XXX free adjustedmode */
498 drm_mode_destroy(dev, adjusted_mode);
501 // if (scrn->pScreen)
502 // drm_crtc_set_screen_sub_pixel_order(dev);
508 crtc->mode = saved_mode;
512 crtc->funcs->unlock (crtc);
516 EXPORT_SYMBOL(drm_crtc_set_mode);
519 * drm_disable_unused_functions - disable unused objects
523 * Caller must hold mode config lock.
525 * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
526 * by calling its dpms function, which should power it off.
528 void drm_disable_unused_functions(struct drm_device *dev)
530 struct drm_output *output;
531 struct drm_crtc *crtc;
533 list_for_each_entry(output, &dev->mode_config.output_list, head) {
535 (*output->funcs->dpms)(output, DPMSModeOff);
538 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
540 crtc->funcs->dpms(crtc, DPMSModeOff);
543 EXPORT_SYMBOL(drm_disable_unused_functions);
546 * drm_mode_probed_add - add a mode to the specified output's probed mode list
547 * @output: output the new mode
551 * Caller must hold mode config lock.
553 * Add @mode to @output's mode list for later use.
555 void drm_mode_probed_add(struct drm_output *output,
556 struct drm_display_mode *mode)
558 list_add(&mode->head, &output->probed_modes);
560 EXPORT_SYMBOL(drm_mode_probed_add);
563 * drm_mode_remove - remove and free a mode
564 * @output: output list to modify
565 * @mode: mode to remove
568 * Caller must hold mode config lock.
570 * Remove @mode from @output's mode list, then free it.
572 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
574 list_del(&mode->head);
577 EXPORT_SYMBOL(drm_mode_remove);
580 * drm_output_create - create a new output
582 * @funcs: callbacks for this output
583 * @name: user visible name of the output
586 * Caller must hold @dev's mode_config lock.
588 * Creates a new drm_output structure and adds it to @dev's mode_config
592 * Pointer to the new output or NULL on error.
594 struct drm_output *drm_output_create(struct drm_device *dev,
595 const struct drm_output_funcs *funcs,
598 struct drm_output *output = NULL;
600 output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
605 output->funcs = funcs;
606 output->id = drm_idr_get(dev, output);
608 strncpy(output->name, name, DRM_OUTPUT_LEN);
609 output->name[DRM_OUTPUT_LEN - 1] = 0;
610 output->subpixel_order = SubPixelUnknown;
611 INIT_LIST_HEAD(&output->user_modes);
612 INIT_LIST_HEAD(&output->probed_modes);
613 INIT_LIST_HEAD(&output->modes);
615 /* output_set_monitor(output)? */
616 /* check for output_ignored(output)? */
618 mutex_lock(&dev->mode_config.mutex);
619 list_add_tail(&output->head, &dev->mode_config.output_list);
620 dev->mode_config.num_output++;
622 drm_output_attach_property(output, dev->mode_config.edid_property, 0);
624 drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
626 mutex_unlock(&dev->mode_config.mutex);
631 EXPORT_SYMBOL(drm_output_create);
634 * drm_output_destroy - remove an output
635 * @output: output to remove
638 * Caller must hold @dev's mode_config lock.
640 * Call @output's cleanup function, then remove the output from the DRM
641 * mode_config after freeing @output's modes.
643 void drm_output_destroy(struct drm_output *output)
645 struct drm_device *dev = output->dev;
646 struct drm_display_mode *mode, *t;
648 if (*output->funcs->cleanup)
649 (*output->funcs->cleanup)(output);
651 list_for_each_entry_safe(mode, t, &output->probed_modes, head)
652 drm_mode_remove(output, mode);
654 list_for_each_entry_safe(mode, t, &output->modes, head)
655 drm_mode_remove(output, mode);
657 list_for_each_entry_safe(mode, t, &output->user_modes, head)
658 drm_mode_remove(output, mode);
660 mutex_lock(&dev->mode_config.mutex);
661 drm_idr_put(dev, output->id);
662 list_del(&output->head);
663 mutex_unlock(&dev->mode_config.mutex);
666 EXPORT_SYMBOL(drm_output_destroy);
669 * drm_output_rename - rename an output
670 * @output: output to rename
671 * @name: new user visible name
676 * Simply stuff a new name into @output's name field, based on @name.
679 * True if the name was changed, false otherwise.
681 bool drm_output_rename(struct drm_output *output, const char *name)
686 strncpy(output->name, name, DRM_OUTPUT_LEN);
687 output->name[DRM_OUTPUT_LEN - 1] = 0;
689 DRM_DEBUG("Changed name to %s\n", output->name);
690 // drm_output_set_monitor(output);
691 // if (drm_output_ignored(output))
696 EXPORT_SYMBOL(drm_output_rename);
699 * drm_mode_create - create a new display mode
705 * Create a new drm_display_mode, give it an ID, and return it.
708 * Pointer to new mode on success, NULL on error.
710 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
712 struct drm_display_mode *nmode;
714 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
718 nmode->mode_id = drm_idr_get(dev, nmode);
721 EXPORT_SYMBOL(drm_mode_create);
724 * drm_mode_destroy - remove a mode
726 * @mode: mode to remove
729 * Caller must hold mode config lock.
731 * Free @mode's unique identifier, then free it.
733 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
735 drm_idr_put(dev, mode->mode_id);
739 EXPORT_SYMBOL(drm_mode_destroy);
741 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
745 dev->mode_config.edid_property =
746 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
749 dev->mode_config.dpms_property =
750 drm_property_create(dev, DRM_MODE_PROP_ENUM, "DPMS", 4);
752 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
753 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
755 dev->mode_config.connector_type_property =
756 drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
757 "Connector Type", 10);
758 for (i = 0; i < ARRAY_SIZE(drm_conn_enum_list); i++)
759 drm_property_add_enum(dev->mode_config.connector_type_property, i, drm_conn_enum_list[i].type, drm_conn_enum_list[i].name);
761 dev->mode_config.connector_num_property =
762 drm_property_create(dev, DRM_MODE_PROP_RANGE | DRM_MODE_PROP_IMMUTABLE,
764 dev->mode_config.connector_num_property->values[0] = 0;
765 dev->mode_config.connector_num_property->values[1] = 20;
770 * drm_mode_config_init - initialize DRM mode_configuration structure
774 * None, should happen single threaded at init time.
776 * Initialize @dev's mode_config structure, used for tracking the graphics
777 * configuration of @dev.
779 void drm_mode_config_init(struct drm_device *dev)
781 mutex_init(&dev->mode_config.mutex);
782 INIT_LIST_HEAD(&dev->mode_config.fb_list);
783 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
784 INIT_LIST_HEAD(&dev->mode_config.output_list);
785 INIT_LIST_HEAD(&dev->mode_config.property_list);
786 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
787 idr_init(&dev->mode_config.crtc_idr);
789 drm_mode_create_standard_output_properties(dev);
792 EXPORT_SYMBOL(drm_mode_config_init);
795 * drm_get_buffer_object - find the buffer object for a given handle
797 * @bo: pointer to caller's buffer_object pointer
798 * @handle: handle to lookup
801 * Must take @dev's struct_mutex to protect buffer object lookup.
803 * Given @handle, lookup the buffer object in @dev and put it in the caller's
807 * Zero on success, -EINVAL if the handle couldn't be found.
809 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
811 struct drm_user_object *uo;
812 struct drm_hash_item *hash;
817 mutex_lock(&dev->struct_mutex);
818 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
820 DRM_ERROR("Couldn't find handle.\n");
825 uo = drm_hash_entry(hash, struct drm_user_object, hash);
826 if (uo->type != drm_buffer_type) {
831 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
834 mutex_unlock(&dev->struct_mutex);
839 * drm_pick_crtcs - pick crtcs for output devices
843 * Caller must hold mode config lock.
845 static void drm_pick_crtcs (struct drm_device *dev)
848 struct drm_output *output, *output_equal;
849 struct drm_crtc *crtc;
850 struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
852 list_for_each_entry(output, &dev->mode_config.output_list, head) {
855 /* Don't hook up outputs that are disconnected ??
857 * This is debateable. Do we want fixed /dev/fbX or
858 * dynamic on hotplug (need mode code for that though) ?
860 * If we don't hook up outputs now, then we only create
861 * /dev/fbX for the output that's enabled, that's good as
862 * the users console will be on that output.
864 * If we do hook up outputs that are disconnected now, then
865 * the user may end up having to muck about with the fbcon
866 * map flags to assign his console to the enabled output. Ugh.
868 if (output->status != output_status_connected)
872 list_for_each_entry(des_mode, &output->modes, head) {
873 if (des_mode->type & DRM_MODE_TYPE_PREFERRED)
877 /* No preferred mode, let's just select the first available */
878 if (!des_mode || !(des_mode->type & DRM_MODE_TYPE_PREFERRED)) {
879 list_for_each_entry(des_mode, &output->modes, head) {
886 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
890 if ((output->possible_crtcs & (1 << c)) == 0)
893 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
894 if (output->id == output_equal->id)
897 /* Find out if crtc has been assigned before */
898 if (output_equal->crtc == crtc)
902 #if 1 /* continue for now */
908 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
910 if (output->id == output_equal->id)
913 list_for_each_entry(modes, &output->modes, head) {
914 list_for_each_entry(modes_equal, &output_equal->modes, head) {
915 if (drm_mode_equal (modes, modes_equal)) {
916 if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
917 printk("Cloning %s (0x%lx) to %s (0x%lx)\n",output->name,output->possible_clones,output_equal->name,output_equal->possible_clones);
927 /* crtc has been assigned skip it */
931 /* Found a CRTC to attach to, do it ! */
933 output->crtc->desired_mode = des_mode;
934 output->initial_x = 0;
935 output->initial_y = 0;
936 DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
941 EXPORT_SYMBOL(drm_pick_crtcs);
944 * drm_initial_config - setup a sane initial output configuration
946 * @can_grow: this configuration is growable
949 * Called at init time, must take mode config lock.
951 * Scan the CRTCs and outputs and try to put together an initial setup.
952 * At the moment, this is a cloned configuration across all heads with
953 * a new framebuffer object as the backing store.
956 * Zero if everything went ok, nonzero otherwise.
958 bool drm_initial_config(struct drm_device *dev, bool can_grow)
960 struct drm_output *output;
961 struct drm_crtc *crtc;
964 mutex_lock(&dev->mode_config.mutex);
966 drm_crtc_probe_output_modes(dev, 2048, 2048);
970 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
972 /* can't setup the crtc if there's no assigned mode */
973 if (!crtc->desired_mode)
976 /* Now setup the fbdev for attached crtcs */
977 dev->driver->fb_probe(dev, crtc);
980 /* This is a little screwy, as we've already walked the outputs
981 * above, but it's a little bit of magic too. There's the potential
982 * for things not to get setup above if an existing device gets
983 * re-assigned thus confusing the hardware. By walking the outputs
984 * this fixes up their crtc's.
986 list_for_each_entry(output, &dev->mode_config.output_list, head) {
988 /* can't setup the output if there's no assigned mode */
989 if (!output->crtc || !output->crtc->desired_mode)
992 /* and needs an attached fb */
993 if (output->crtc->fb)
994 drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
997 drm_disable_unused_functions(dev);
999 mutex_unlock(&dev->mode_config.mutex);
1002 EXPORT_SYMBOL(drm_initial_config);
1005 * drm_mode_config_cleanup - free up DRM mode_config info
1009 * Caller must hold mode config lock.
1011 * Free up all the outputs and CRTCs associated with this DRM device, then
1012 * free up the framebuffers and associated buffer objects.
1014 * FIXME: cleanup any dangling user buffer objects too
1016 void drm_mode_config_cleanup(struct drm_device *dev)
1018 struct drm_output *output, *ot;
1019 struct drm_crtc *crtc, *ct;
1020 struct drm_framebuffer *fb, *fbt;
1021 struct drm_property *property, *pt;
1023 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1024 drm_output_destroy(output);
1027 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1028 drm_property_destroy(dev, property);
1031 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1032 if (fb->bo->type != drm_bo_type_kernel)
1033 drm_framebuffer_destroy(fb);
1035 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1038 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1039 drm_crtc_destroy(crtc);
1043 EXPORT_SYMBOL(drm_mode_config_cleanup);
1046 * drm_crtc_set_config - set a new config from userspace
1047 * @crtc: CRTC to setup
1048 * @crtc_info: user provided configuration
1049 * @new_mode: new mode to set
1050 * @output_set: set of outputs for the new config
1051 * @fb: new framebuffer
1054 * Caller must hold mode config lock.
1056 * Setup a new configuration, provided by the user in @crtc_info, and enable
1062 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)
1064 struct drm_device *dev = crtc->dev;
1065 struct drm_crtc **save_crtcs, *new_crtc;
1066 bool save_enabled = crtc->enabled;
1067 bool changed = false;
1068 struct drm_output *output;
1071 save_crtcs = kzalloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc *), GFP_KERNEL);
1078 if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1081 if (new_mode && !drm_mode_equal(new_mode, &crtc->mode))
1084 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1085 save_crtcs[count++] = output->crtc;
1087 if (output->crtc == crtc)
1090 new_crtc = output->crtc;
1092 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1093 if (output_set[ro] == output)
1096 if (new_crtc != output->crtc) {
1098 output->crtc = new_crtc;
1104 crtc->enabled = (new_mode != NULL);
1105 if (new_mode != NULL) {
1106 DRM_DEBUG("attempting to set mode from userspace\n");
1107 drm_mode_debug_printmodeline(dev, new_mode);
1108 if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1110 crtc->enabled = save_enabled;
1112 list_for_each_entry(output, &dev->mode_config.output_list, head)
1113 output->crtc = save_crtcs[count++];
1117 crtc->desired_x = crtc_info->x;
1118 crtc->desired_y = crtc_info->y;
1119 crtc->desired_mode = new_mode;
1121 drm_disable_unused_functions(dev);
1128 * drm_hotplug_stage_two
1130 * @output hotpluged output
1133 * Caller must hold mode config lock, function might grap struct lock.
1135 * Stage two of a hotplug.
1138 * Zero on success, errno on failure.
1140 int drm_hotplug_stage_two(struct drm_device *dev, struct drm_output *output)
1144 if (output->crtc && output->crtc->desired_mode) {
1145 DRM_DEBUG("drm thinks that the output already has a config\n");
1149 drm_crtc_probe_output_modes(dev, 2048, 2048);
1152 drm_pick_crtcs(dev);
1154 if (!output->crtc || !output->crtc->desired_mode) {
1155 DRM_DEBUG("could not find a desired mode or crtc for output\n");
1159 /* We should realy check if there is a fb using this crtc */
1161 dev->driver->fb_probe(dev, output->crtc);
1163 dev->driver->fb_resize(dev, output->crtc);
1165 if (!drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0))
1166 DRM_ERROR("failed to set mode after hotplug\n");
1169 drm_disable_unused_functions(dev);
1173 EXPORT_SYMBOL(drm_hotplug_stage_two);
1176 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1177 * @out: drm_mode_modeinfo struct to return to the user
1178 * @in: drm_display_mode to use
1183 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1186 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1188 out->clock = in->clock;
1189 out->hdisplay = in->hdisplay;
1190 out->hsync_start = in->hsync_start;
1191 out->hsync_end = in->hsync_end;
1192 out->htotal = in->htotal;
1193 out->hskew = in->hskew;
1194 out->vdisplay = in->vdisplay;
1195 out->vsync_start = in->vsync_start;
1196 out->vsync_end = in->vsync_end;
1197 out->vtotal = in->vtotal;
1198 out->vscan = in->vscan;
1199 out->vrefresh = in->vrefresh;
1200 out->flags = in->flags;
1201 out->type = in->type;
1202 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1203 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1207 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1208 * @out: drm_display_mode to return to the user
1209 * @in: drm_mode_modeinfo to use
1214 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1217 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1219 out->clock = in->clock;
1220 out->hdisplay = in->hdisplay;
1221 out->hsync_start = in->hsync_start;
1222 out->hsync_end = in->hsync_end;
1223 out->htotal = in->htotal;
1224 out->hskew = in->hskew;
1225 out->vdisplay = in->vdisplay;
1226 out->vsync_start = in->vsync_start;
1227 out->vsync_end = in->vsync_end;
1228 out->vtotal = in->vtotal;
1229 out->vscan = in->vscan;
1230 out->vrefresh = in->vrefresh;
1231 out->flags = in->flags;
1232 out->type = in->type;
1233 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1234 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1238 * drm_mode_getresources - get graphics configuration
1239 * @inode: inode from the ioctl
1240 * @filp: file * from the ioctl
1241 * @cmd: cmd from ioctl
1242 * @arg: arg from ioctl
1245 * Takes mode config lock.
1247 * Construct a set of configuration description structures and return
1248 * them to the user, including CRTC, output and framebuffer configuration.
1250 * Called by the user via ioctl.
1253 * Zero on success, errno on failure.
1255 int drm_mode_getresources(struct drm_device *dev,
1256 void *data, struct drm_file *file_priv)
1258 struct drm_mode_card_res *card_res = data;
1259 struct list_head *lh;
1260 struct drm_framebuffer *fb;
1261 struct drm_output *output;
1262 struct drm_crtc *crtc;
1264 int output_count = 0;
1268 uint32_t __user *fb_id;
1269 uint32_t __user *crtc_id;
1270 uint32_t __user *output_id;
1272 mutex_lock(&dev->mode_config.mutex);
1274 list_for_each(lh, &dev->mode_config.fb_list)
1277 list_for_each(lh, &dev->mode_config.crtc_list)
1280 list_for_each(lh, &dev->mode_config.output_list)
1283 card_res->max_height = dev->mode_config.max_height;
1284 card_res->min_height = dev->mode_config.min_height;
1285 card_res->max_width = dev->mode_config.max_width;
1286 card_res->min_width = dev->mode_config.min_width;
1288 /* handle this in 4 parts */
1290 if (card_res->count_fbs >= fb_count) {
1292 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1293 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1294 if (put_user(fb->id, fb_id + copied)) {
1301 card_res->count_fbs = fb_count;
1304 if (card_res->count_crtcs >= crtc_count) {
1306 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1307 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1308 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1309 if (put_user(crtc->id, crtc_id + copied)) {
1316 card_res->count_crtcs = crtc_count;
1320 if (card_res->count_outputs >= output_count) {
1322 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1323 list_for_each_entry(output, &dev->mode_config.output_list,
1325 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1326 if (put_user(output->id, output_id + copied)) {
1333 card_res->count_outputs = output_count;
1335 DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1336 card_res->count_outputs);
1339 mutex_unlock(&dev->mode_config.mutex);
1344 * drm_mode_getcrtc - get CRTC configuration
1345 * @inode: inode from the ioctl
1346 * @filp: file * from the ioctl
1347 * @cmd: cmd from ioctl
1348 * @arg: arg from ioctl
1353 * Construct a CRTC configuration structure to return to the user.
1355 * Called by the user via ioctl.
1358 * Zero on success, errno on failure.
1360 int drm_mode_getcrtc(struct drm_device *dev,
1361 void *data, struct drm_file *file_priv)
1363 struct drm_mode_crtc *crtc_resp = data;
1364 struct drm_crtc *crtc;
1365 struct drm_output *output;
1369 mutex_lock(&dev->mode_config.mutex);
1370 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1371 if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1376 crtc_resp->x = crtc->x;
1377 crtc_resp->y = crtc->y;
1380 crtc_resp->fb_id = crtc->fb->id;
1382 crtc_resp->fb_id = 0;
1384 crtc_resp->outputs = 0;
1385 if (crtc->enabled) {
1387 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1388 crtc_resp->mode_valid = 1;
1390 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1391 if (output->crtc == crtc)
1392 crtc_resp->outputs |= 1 << (ocount++);
1396 crtc_resp->mode_valid = 0;
1400 mutex_unlock(&dev->mode_config.mutex);
1405 * drm_mode_getoutput - get output configuration
1406 * @inode: inode from the ioctl
1407 * @filp: file * from the ioctl
1408 * @cmd: cmd from ioctl
1409 * @arg: arg from ioctl
1414 * Construct a output configuration structure to return to the user.
1416 * Called by the user via ioctl.
1419 * Zero on success, errno on failure.
1421 int drm_mode_getoutput(struct drm_device *dev,
1422 void *data, struct drm_file *file_priv)
1424 struct drm_mode_get_output *out_resp = data;
1425 struct drm_output *output;
1426 struct drm_display_mode *mode;
1428 int props_count = 0;
1432 struct drm_mode_modeinfo u_mode;
1433 struct drm_mode_modeinfo __user *mode_ptr;
1434 uint32_t __user *prop_ptr;
1435 uint64_t __user *prop_values;
1437 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1439 DRM_DEBUG("output id %d:\n", out_resp->output);
1441 mutex_lock(&dev->mode_config.mutex);
1442 output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1443 if (!output || (output->id != out_resp->output)) {
1448 list_for_each_entry(mode, &output->modes, head)
1451 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1452 if (output->property_ids[i] != 0) {
1457 if (out_resp->count_modes == 0) {
1458 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1461 strncpy(out_resp->name, output->name, DRM_OUTPUT_NAME_LEN);
1462 out_resp->name[DRM_OUTPUT_NAME_LEN-1] = 0;
1464 out_resp->mm_width = output->mm_width;
1465 out_resp->mm_height = output->mm_height;
1466 out_resp->subpixel = output->subpixel_order;
1467 out_resp->connection = output->status;
1469 out_resp->crtc = output->crtc->id;
1473 out_resp->crtcs = output->possible_crtcs;
1474 out_resp->clones = output->possible_clones;
1476 if ((out_resp->count_modes >= mode_count) && mode_count) {
1478 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1479 list_for_each_entry(mode, &output->modes, head) {
1480 drm_crtc_convert_to_umode(&u_mode, mode);
1481 if (copy_to_user(mode_ptr + copied,
1482 &u_mode, sizeof(u_mode))) {
1490 out_resp->count_modes = mode_count;
1492 if ((out_resp->count_props >= props_count) && props_count) {
1494 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1495 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1496 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1497 if (output->property_ids[i] != 0) {
1498 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1503 if (put_user(output->property_values[i], prop_values + copied)) {
1511 out_resp->count_props = props_count;
1514 mutex_unlock(&dev->mode_config.mutex);
1519 * drm_mode_setcrtc - set CRTC configuration
1520 * @inode: inode from the ioctl
1521 * @filp: file * from the ioctl
1522 * @cmd: cmd from ioctl
1523 * @arg: arg from ioctl
1528 * Build a new CRTC configuration based on user request.
1530 * Called by the user via ioctl.
1533 * Zero on success, errno on failure.
1535 int drm_mode_setcrtc(struct drm_device *dev,
1536 void *data, struct drm_file *file_priv)
1538 struct drm_mode_crtc *crtc_req = data;
1539 struct drm_crtc *crtc;
1540 struct drm_output **output_set = NULL, *output;
1541 struct drm_framebuffer *fb = NULL;
1542 struct drm_display_mode *mode = NULL;
1545 uint32_t __user *set_outputs_ptr;
1547 mutex_lock(&dev->mode_config.mutex);
1548 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1549 if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1550 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1555 if (crtc_req->mode_valid) {
1556 /* if we have a mode we need a framebuffer */
1557 if (crtc_req->fb_id) {
1558 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1559 if (!fb || (fb->id != crtc_req->fb_id)) {
1560 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1566 mode = drm_mode_create(dev);
1567 drm_crtc_convert_umode(mode, &crtc_req->mode);
1570 if (crtc_req->count_outputs == 0 && mode) {
1571 DRM_DEBUG("Count outputs is 0 but mode set\n");
1576 if (crtc_req->count_outputs > 0 && !mode && !fb) {
1577 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1582 if (crtc_req->count_outputs > 0) {
1584 output_set = kmalloc(crtc_req->count_outputs *
1585 sizeof(struct drm_output *), GFP_KERNEL);
1591 for (i = 0; i < crtc_req->count_outputs; i++) {
1592 set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1593 if (get_user(out_id, &set_outputs_ptr[i])) {
1598 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1599 if (!output || (out_id != output->id)) {
1600 DRM_DEBUG("Output id %d unknown\n", out_id);
1605 output_set[i] = output;
1609 ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1612 mutex_unlock(&dev->mode_config.mutex);
1617 * drm_mode_addfb - add an FB to the graphics configuration
1618 * @inode: inode from the ioctl
1619 * @filp: file * from the ioctl
1620 * @cmd: cmd from ioctl
1621 * @arg: arg from ioctl
1624 * Takes mode config lock.
1626 * Add a new FB to the specified CRTC, given a user request.
1628 * Called by the user via ioctl.
1631 * Zero on success, errno on failure.
1633 int drm_mode_addfb(struct drm_device *dev,
1634 void *data, struct drm_file *file_priv)
1636 struct drm_mode_fb_cmd *r = data;
1637 struct drm_mode_config *config = &dev->mode_config;
1638 struct drm_framebuffer *fb;
1639 struct drm_buffer_object *bo;
1640 struct drm_crtc *crtc;
1643 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1644 DRM_ERROR("mode new framebuffer width not within limits\n");
1647 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1648 DRM_ERROR("mode new framebuffer height not within limits\n");
1652 mutex_lock(&dev->mode_config.mutex);
1653 /* TODO check limits are okay */
1654 ret = drm_get_buffer_object(dev, &bo, r->handle);
1660 /* TODO check buffer is sufficently large */
1661 /* TODO setup destructor callback */
1663 fb = drm_framebuffer_create(dev);
1669 fb->width = r->width;
1670 fb->height = r->height;
1671 fb->pitch = r->pitch;
1672 fb->bits_per_pixel = r->bpp;
1673 fb->depth = r->depth;
1674 fb->offset = bo->offset;
1677 r->buffer_id = fb->id;
1679 list_add(&fb->filp_head, &file_priv->fbs);
1681 /* FIXME: bind the fb to the right crtc */
1682 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1684 dev->driver->fb_probe(dev, crtc);
1688 mutex_unlock(&dev->mode_config.mutex);
1693 * drm_mode_rmfb - remove an FB from the configuration
1694 * @inode: inode from the ioctl
1695 * @filp: file * from the ioctl
1696 * @cmd: cmd from ioctl
1697 * @arg: arg from ioctl
1700 * Takes mode config lock.
1702 * Remove the FB specified by the user.
1704 * Called by the user via ioctl.
1707 * Zero on success, errno on failure.
1709 int drm_mode_rmfb(struct drm_device *dev,
1710 void *data, struct drm_file *file_priv)
1712 struct drm_framebuffer *fb = 0;
1713 uint32_t *id = data;
1716 mutex_lock(&dev->mode_config.mutex);
1717 fb = idr_find(&dev->mode_config.crtc_idr, *id);
1718 /* TODO check that we realy get a framebuffer back. */
1719 if (!fb || (*id != fb->id)) {
1720 DRM_ERROR("mode invalid framebuffer id\n");
1725 /* TODO check if we own the buffer */
1726 /* TODO release all crtc connected to the framebuffer */
1727 /* bind the fb to the crtc for now */
1728 /* TODO unhock the destructor from the buffer object */
1730 if (fb->bo->type != drm_bo_type_kernel)
1731 drm_framebuffer_destroy(fb);
1733 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1736 mutex_unlock(&dev->mode_config.mutex);
1741 * drm_mode_getfb - get FB info
1742 * @inode: inode from the ioctl
1743 * @filp: file * from the ioctl
1744 * @cmd: cmd from ioctl
1745 * @arg: arg from ioctl
1750 * Lookup the FB given its ID and return info about it.
1752 * Called by the user via ioctl.
1755 * Zero on success, errno on failure.
1757 int drm_mode_getfb(struct drm_device *dev,
1758 void *data, struct drm_file *file_priv)
1760 struct drm_mode_fb_cmd *r = data;
1761 struct drm_framebuffer *fb;
1764 mutex_lock(&dev->mode_config.mutex);
1765 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1766 if (!fb || (r->buffer_id != fb->id)) {
1767 DRM_ERROR("invalid framebuffer id\n");
1772 r->height = fb->height;
1773 r->width = fb->width;
1774 r->depth = fb->depth;
1775 r->bpp = fb->bits_per_pixel;
1776 r->handle = fb->bo->base.hash.key;
1777 r->pitch = fb->pitch;
1780 mutex_unlock(&dev->mode_config.mutex);
1785 * drm_fb_release - remove and free the FBs on this file
1786 * @filp: file * from the ioctl
1789 * Takes mode config lock.
1791 * Destroy all the FBs associated with @filp.
1793 * Called by the user via ioctl.
1796 * Zero on success, errno on failure.
1798 void drm_fb_release(struct file *filp)
1800 struct drm_file *priv = filp->private_data;
1801 struct drm_device *dev = priv->head->dev;
1802 struct drm_framebuffer *fb, *tfb;
1804 mutex_lock(&dev->mode_config.mutex);
1805 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1806 list_del(&fb->filp_head);
1807 if (fb->bo->type != drm_bo_type_kernel)
1808 drm_framebuffer_destroy(fb);
1810 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1812 mutex_unlock(&dev->mode_config.mutex);
1819 static int drm_mode_attachmode(struct drm_device *dev,
1820 struct drm_output *output,
1821 struct drm_display_mode *mode)
1825 list_add_tail(&mode->head, &output->user_modes);
1829 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1830 struct drm_display_mode *mode)
1832 struct drm_output *output;
1834 struct drm_display_mode *dup_mode;
1836 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1837 if (output->crtc == crtc) {
1839 dup_mode = drm_mode_duplicate(dev, mode);
1842 ret = drm_mode_attachmode(dev, output, dup_mode);
1850 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1852 static int drm_mode_detachmode(struct drm_device *dev,
1853 struct drm_output *output,
1854 struct drm_display_mode *mode)
1858 struct drm_display_mode *match_mode, *t;
1860 list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1861 if (drm_mode_equal(match_mode, mode)) {
1862 list_del(&match_mode->head);
1863 drm_mode_destroy(dev, match_mode);
1875 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1877 struct drm_output *output;
1879 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1880 drm_mode_detachmode(dev, output, mode);
1884 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1887 * drm_fb_attachmode - Attach a user mode to an output
1888 * @inode: inode from the ioctl
1889 * @filp: file * from the ioctl
1890 * @cmd: cmd from ioctl
1891 * @arg: arg from ioctl
1893 * This attaches a user specified mode to an output.
1894 * Called by the user via ioctl.
1897 * Zero on success, errno on failure.
1899 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1900 void *data, struct drm_file *file_priv)
1902 struct drm_mode_mode_cmd *mode_cmd = data;
1903 struct drm_output *output;
1904 struct drm_display_mode *mode;
1905 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1908 mutex_lock(&dev->mode_config.mutex);
1910 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1911 if (!output || (output->id != mode_cmd->output_id)) {
1916 mode = drm_mode_create(dev);
1922 drm_crtc_convert_umode(mode, umode);
1924 ret = drm_mode_attachmode(dev, output, mode);
1926 mutex_unlock(&dev->mode_config.mutex);
1932 * drm_fb_detachmode - Detach a user specified mode from an output
1933 * @inode: inode from the ioctl
1934 * @filp: file * from the ioctl
1935 * @cmd: cmd from ioctl
1936 * @arg: arg from ioctl
1938 * Called by the user via ioctl.
1941 * Zero on success, errno on failure.
1943 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1944 void *data, struct drm_file *file_priv)
1946 struct drm_mode_mode_cmd *mode_cmd = data;
1947 struct drm_output *output;
1948 struct drm_display_mode mode;
1949 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1952 mutex_lock(&dev->mode_config.mutex);
1954 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1955 if (!output || (output->id != mode_cmd->output_id)) {
1960 drm_crtc_convert_umode(&mode, umode);
1961 ret = drm_mode_detachmode(dev, output, &mode);
1963 mutex_unlock(&dev->mode_config.mutex);
1967 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1968 const char *name, int num_values)
1970 struct drm_property *property = NULL;
1972 property = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
1977 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1978 if (!property->values)
1982 property->id = drm_idr_get(dev, property);
1983 property->flags = flags;
1984 property->num_values = num_values;
1985 INIT_LIST_HEAD(&property->enum_blob_list);
1988 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1990 list_add_tail(&property->head, &dev->mode_config.property_list);
1996 EXPORT_SYMBOL(drm_property_create);
1998 int drm_property_add_enum(struct drm_property *property, int index,
1999 uint64_t value, const char *name)
2001 struct drm_property_enum *prop_enum;
2003 if (!(property->flags & DRM_MODE_PROP_ENUM))
2006 if (!list_empty(&property->enum_blob_list)) {
2007 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2008 if (prop_enum->value == value) {
2009 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2010 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2016 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2020 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2021 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2022 prop_enum->value = value;
2024 property->values[index] = value;
2025 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2028 EXPORT_SYMBOL(drm_property_add_enum);
2030 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2032 struct drm_property_enum *prop_enum, *pt;
2034 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2035 list_del(&prop_enum->head);
2039 if (property->num_values)
2040 kfree(property->values);
2041 drm_idr_put(dev, property->id);
2042 list_del(&property->head);
2045 EXPORT_SYMBOL(drm_property_destroy);
2047 int drm_output_attach_property(struct drm_output *output,
2048 struct drm_property *property, uint64_t init_val)
2052 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2053 if (output->property_ids[i] == 0) {
2054 output->property_ids[i] = property->id;
2055 output->property_values[i] = init_val;
2060 if (i == DRM_OUTPUT_MAX_PROPERTY)
2064 EXPORT_SYMBOL(drm_output_attach_property);
2066 int drm_output_property_set_value(struct drm_output *output,
2067 struct drm_property *property, uint64_t value)
2071 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2072 if (output->property_ids[i] == property->id) {
2073 output->property_values[i] = value;
2078 if (i == DRM_OUTPUT_MAX_PROPERTY)
2082 EXPORT_SYMBOL(drm_output_property_set_value);
2084 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2085 void *data, struct drm_file *file_priv)
2087 struct drm_mode_get_property *out_resp = data;
2088 struct drm_property *property;
2091 int value_count = 0;
2094 struct drm_property_enum *prop_enum;
2095 struct drm_mode_property_enum __user *enum_ptr;
2096 struct drm_property_blob *prop_blob;
2097 uint32_t *blob_id_ptr;
2098 uint64_t __user *values_ptr;
2099 uint32_t __user *blob_length_ptr;
2101 mutex_lock(&dev->mode_config.mutex);
2102 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2103 if (!property || (property->id != out_resp->prop_id)) {
2108 if (property->flags & DRM_MODE_PROP_ENUM) {
2109 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2111 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2112 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2116 value_count = property->num_values;
2118 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2119 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2120 out_resp->flags = property->flags;
2122 if ((out_resp->count_values >= value_count) && value_count) {
2123 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2124 for (i = 0; i < value_count; i++) {
2125 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2131 out_resp->count_values = value_count;
2133 if (property->flags & DRM_MODE_PROP_ENUM) {
2135 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2137 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2138 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2140 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2145 if (copy_to_user(&enum_ptr[copied].name,
2146 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2153 out_resp->count_enum_blobs = enum_count;
2156 if (property->flags & DRM_MODE_PROP_BLOB) {
2157 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2159 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2160 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2162 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2163 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2168 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2176 out_resp->count_enum_blobs = enum_count;
2179 mutex_unlock(&dev->mode_config.mutex);
2183 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2186 struct drm_property_blob *blob;
2188 if (!length || !data)
2191 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2195 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2196 blob->length = length;
2198 memcpy(blob->data, data, length);
2200 blob->id = drm_idr_get(dev, blob);
2202 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2206 static void drm_property_destroy_blob(struct drm_device *dev,
2207 struct drm_property_blob *blob)
2209 drm_idr_put(dev, blob->id);
2210 list_del(&blob->head);
2214 int drm_mode_getblob_ioctl(struct drm_device *dev,
2215 void *data, struct drm_file *file_priv)
2217 struct drm_mode_get_blob *out_resp = data;
2218 struct drm_property_blob *blob;
2222 mutex_lock(&dev->mode_config.mutex);
2224 blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2225 if (!blob || (blob->id != out_resp->blob_id)) {
2230 if (out_resp->length == blob->length) {
2231 blob_ptr = (void *)(unsigned long)out_resp->data;
2232 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2237 out_resp->length = blob->length;
2240 mutex_unlock(&dev->mode_config.mutex);
2244 int drm_mode_output_update_edid_property(struct drm_output *output, unsigned char *edid)
2246 struct drm_device *dev = output->dev;
2248 if (output->edid_blob_ptr)
2249 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2251 output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2253 ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2256 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2258 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2259 void *data, struct drm_file *file_priv)
2261 struct drm_mode_output_set_property *out_resp = data;
2262 struct drm_property *property;
2263 struct drm_output *output;
2267 mutex_lock(&dev->mode_config.mutex);
2268 output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2269 if (!output || (output->id != out_resp->output_id)) {
2273 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2274 if (output->property_ids[i] == out_resp->prop_id)
2278 if (i == DRM_OUTPUT_MAX_PROPERTY) {
2282 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2283 if (!property || (property->id != out_resp->prop_id)) {
2287 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2290 if (property->flags & DRM_MODE_PROP_RANGE) {
2291 if (out_resp->value < property->values[0])
2294 if (out_resp->value > property->values[1])
2298 for (i = 0; i < property->num_values; i++) {
2299 if (property->values[i] == out_resp->value) {
2309 if (output->funcs->set_property)
2310 ret = output->funcs->set_property(output, property, out_resp->value);
2313 mutex_unlock(&dev->mode_config.mutex);