2 * Copyright (c) 2006-2007 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 * DRM core CRTC related functions
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting documentation, and
11 * that the name of the copyright holders not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. The copyright holders make no representations
14 * about the suitability of this software for any purpose. It is provided "as
15 * is" without express or implied warranty.
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
31 #include <linux/list.h>
36 struct drm_prop_enum_list {
41 static struct drm_prop_enum_list drm_dpms_enum_list[] =
42 { { DPMSModeOn, "On" },
43 { DPMSModeStandby, "Standby" },
44 { DPMSModeSuspend, "Suspend" },
45 { DPMSModeOff, "Off" }
47 static struct drm_prop_enum_list drm_conn_enum_list[] =
48 { { ConnectorVGA, "VGA" },
49 { ConnectorDVII, "DVI-I" },
50 { ConnectorDVID, "DVI-D" },
51 { ConnectorDVIA, "DVI-A" },
52 { ConnectorComposite, "Composite" },
53 { ConnectorSVIDEO, "SVIDEO" },
54 { ConnectorLVDS, "LVDS" },
55 { ConnectorComponent, "Component" },
56 { Connector9PinDIN, "9-pin DIN" },
57 { ConnectorDisplayPort, "DisplayPort" },
58 { ConnectorHDMIA, "HDMI Type A" },
59 { ConnectorHDMIB, "HDMI Type B" },
61 static struct drm_prop_enum_list drm_output_enum_list[] =
62 { { DRM_MODE_OUTPUT_NONE, "None" },
63 { DRM_MODE_OUTPUT_DAC, "DAC" },
64 { DRM_MODE_OUTPUT_TMDS, "TMDS" },
65 { DRM_MODE_OUTPUT_LVDS, "LVDS" },
66 { DRM_MODE_OUTPUT_TVDAC, "TV" },
69 char *drm_get_output_name(struct drm_output *output)
73 snprintf(buf, 32, "%s-%d", drm_output_enum_list[output->output_type].name,
74 output->output_type_id);
79 * drm_idr_get - allocate a new identifier
81 * @ptr: object pointer, used to generate unique ID
84 * Caller must hold DRM mode_config lock.
86 * Create a unique identifier based on @ptr in @dev's identifier space. Used
87 * for tracking modes, CRTCs and outputs.
90 * New unique (relative to other objects in @dev) integer identifier for the
93 int drm_idr_get(struct drm_device *dev, void *ptr)
98 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
99 DRM_ERROR("Ran out memory getting a mode number\n");
103 ret = idr_get_new_above(&dev->mode_config.crtc_idr, ptr, 1, &new_id);
111 * drm_idr_put - free an identifer
116 * Caller must hold DRM mode_config lock.
118 * Free @id from @dev's unique identifier pool.
120 void drm_idr_put(struct drm_device *dev, int id)
122 idr_remove(&dev->mode_config.crtc_idr, id);
126 * drm_crtc_from_fb - find the CRTC structure associated with an fb
128 * @fb: framebuffer in question
131 * Caller must hold mode_config lock.
133 * Find CRTC in the mode_config structure that matches @fb.
136 * Pointer to the CRTC or NULL if it wasn't found.
138 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
139 struct drm_framebuffer *fb)
141 struct drm_crtc *crtc;
143 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
151 * drm_framebuffer_create - create a new framebuffer object
155 * Caller must hold mode config lock.
157 * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
160 * Pointer to new framebuffer or NULL on error.
162 struct drm_framebuffer *drm_framebuffer_create(struct drm_device *dev)
164 struct drm_framebuffer *fb;
166 /* Limit to single framebuffer for now */
167 if (dev->mode_config.num_fb > 1) {
168 DRM_ERROR("Attempt to add multiple framebuffers failed\n");
172 fb = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
176 fb->id = drm_idr_get(dev, fb);
178 dev->mode_config.num_fb++;
179 list_add(&fb->head, &dev->mode_config.fb_list);
183 EXPORT_SYMBOL(drm_framebuffer_create);
186 * drm_framebuffer_destroy - remove a framebuffer object
187 * @fb: framebuffer to remove
190 * Caller must hold mode config lock.
192 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
193 * it, setting it to NULL.
195 void drm_framebuffer_destroy(struct drm_framebuffer *fb)
197 struct drm_device *dev = fb->dev;
198 struct drm_crtc *crtc;
200 /* remove from any CRTC */
201 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
206 drm_idr_put(dev, fb->id);
208 dev->mode_config.num_fb--;
212 EXPORT_SYMBOL(drm_framebuffer_destroy);
215 * drm_crtc_create - create a new CRTC object
217 * @funcs: callbacks for the new CRTC
220 * Caller must hold mode config lock.
222 * Creates a new CRTC object and adds it to @dev's mode_config structure.
225 * Pointer to new CRTC object or NULL on error.
227 struct drm_crtc *drm_crtc_create(struct drm_device *dev,
228 const struct drm_crtc_funcs *funcs)
230 struct drm_crtc *crtc;
232 crtc = kzalloc(sizeof(struct drm_crtc), GFP_KERNEL);
239 crtc->id = drm_idr_get(dev, crtc);
241 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
242 dev->mode_config.num_crtc++;
246 EXPORT_SYMBOL(drm_crtc_create);
249 * drm_crtc_destroy - remove a CRTC object
250 * @crtc: CRTC to remove
253 * Caller must hold mode config lock.
255 * Cleanup @crtc. Calls @crtc's cleanup function, then removes @crtc from
256 * its associated DRM device's mode_config. Frees it afterwards.
258 void drm_crtc_destroy(struct drm_crtc *crtc)
260 struct drm_device *dev = crtc->dev;
262 if (crtc->funcs->cleanup)
263 (*crtc->funcs->cleanup)(crtc);
265 drm_idr_put(dev, crtc->id);
266 list_del(&crtc->head);
267 dev->mode_config.num_crtc--;
270 EXPORT_SYMBOL(drm_crtc_destroy);
273 * drm_crtc_in_use - check if a given CRTC is in a mode_config
274 * @crtc: CRTC to check
277 * Caller must hold mode config lock.
279 * Walk @crtc's DRM device's mode_config and see if it's in use.
282 * True if @crtc is part of the mode_config, false otherwise.
284 bool drm_crtc_in_use(struct drm_crtc *crtc)
286 struct drm_output *output;
287 struct drm_device *dev = crtc->dev;
288 /* FIXME: Locking around list access? */
289 list_for_each_entry(output, &dev->mode_config.output_list, head)
290 if (output->crtc == crtc)
294 EXPORT_SYMBOL(drm_crtc_in_use);
297 * Detailed mode info for a standard 640x480@60Hz monitor
299 static struct drm_display_mode std_mode[] = {
300 { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
301 752, 800, 0, 480, 490, 492, 525, 0,
302 V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
306 * drm_crtc_probe_output_modes - get complete set of display modes
308 * @maxX: max width for modes
309 * @maxY: max height for modes
312 * Caller must hold mode config lock.
314 * Based on @dev's mode_config layout, scan all the outputs and try to detect
315 * modes on them. Modes will first be added to the output's probed_modes
316 * list, then culled (based on validity and the @maxX, @maxY parameters) and
317 * put into the normal modes list.
319 * Intended to be used either at bootup time or when major configuration
320 * changes have occurred.
322 * FIXME: take into account monitor limits
324 void drm_crtc_probe_single_output_modes(struct drm_output *output, int maxX, int maxY)
326 struct drm_device *dev = output->dev;
327 struct drm_display_mode *mode, *t;
329 //if (maxX == 0 || maxY == 0)
332 /* set all modes to the unverified state */
333 list_for_each_entry_safe(mode, t, &output->modes, head)
334 mode->status = MODE_UNVERIFIED;
336 output->status = (*output->funcs->detect)(output);
338 if (output->status == output_status_disconnected) {
339 DRM_DEBUG("%s is disconnected\n", drm_get_output_name(output));
340 /* TODO set EDID to NULL */
344 ret = (*output->funcs->get_modes)(output);
347 drm_mode_output_list_update(output);
351 drm_mode_validate_size(dev, &output->modes, maxX,
353 list_for_each_entry_safe(mode, t, &output->modes, head) {
354 if (mode->status == MODE_OK)
355 mode->status = (*output->funcs->mode_valid)(output,mode);
359 drm_mode_prune_invalid(dev, &output->modes, TRUE);
361 if (list_empty(&output->modes)) {
362 struct drm_display_mode *stdmode;
364 DRM_DEBUG("No valid modes on %s\n", drm_get_output_name(output));
366 /* Should we do this here ???
367 * When no valid EDID modes are available we end up
368 * here and bailed in the past, now we add a standard
369 * 640x480@60Hz mode and carry on.
371 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
372 drm_mode_probed_add(output, stdmode);
373 drm_mode_list_concat(&output->probed_modes,
376 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
377 drm_get_output_name(output));
380 drm_mode_sort(&output->modes);
382 DRM_DEBUG("Probed modes for %s\n", drm_get_output_name(output));
383 list_for_each_entry_safe(mode, t, &output->modes, head) {
384 mode->vrefresh = drm_mode_vrefresh(mode);
386 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
387 drm_mode_debug_printmodeline(dev, mode);
391 void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY)
393 struct drm_output *output;
395 list_for_each_entry(output, &dev->mode_config.output_list, head) {
396 drm_crtc_probe_single_output_modes(output, maxX, maxY);
401 * drm_crtc_set_mode - set a mode
402 * @crtc: CRTC to program
408 * Caller must hold mode config lock.
410 * Try to set @mode on @crtc. Give @crtc and its associated outputs a chance
411 * to fixup or reject the mode prior to trying to set it.
414 * True if the mode was set successfully, or false otherwise.
416 bool drm_crtc_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
419 struct drm_device *dev = crtc->dev;
420 struct drm_display_mode *adjusted_mode, saved_mode;
421 int saved_x, saved_y;
422 bool didLock = false;
424 struct drm_output *output;
426 adjusted_mode = drm_mode_duplicate(dev, mode);
428 crtc->enabled = drm_crtc_in_use(crtc);
430 if (!crtc->enabled) {
434 didLock = crtc->funcs->lock(crtc);
436 saved_mode = crtc->mode;
440 /* Update crtc values up front so the driver can rely on them for mode
447 /* XXX short-circuit changes to base location only */
449 /* Pass our mode to the outputs and the CRTC to give them a chance to
450 * adjust it according to limitations or output properties, and also
451 * a chance to reject the mode entirely.
453 list_for_each_entry(output, &dev->mode_config.output_list, head) {
455 if (output->crtc != crtc)
458 if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
463 if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
467 /* Prepare the outputs and CRTCs before setting the mode. */
468 list_for_each_entry(output, &dev->mode_config.output_list, head) {
470 if (output->crtc != crtc)
473 /* Disable the output as the first thing we do. */
474 output->funcs->prepare(output);
477 crtc->funcs->prepare(crtc);
479 /* Set up the DPLL and any output state that needs to adjust or depend
482 crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
484 list_for_each_entry(output, &dev->mode_config.output_list, head) {
486 if (output->crtc != crtc)
489 DRM_INFO("%s: set mode %s %x\n", drm_get_output_name(output), mode->name, mode->mode_id);
491 output->funcs->mode_set(output, mode, adjusted_mode);
494 /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
495 crtc->funcs->commit(crtc);
497 list_for_each_entry(output, &dev->mode_config.output_list, head) {
499 if (output->crtc != crtc)
502 output->funcs->commit(output);
504 #if 0 // TODO def RANDR_12_INTERFACE
505 if (output->randr_output)
506 RRPostPendingProperties (output->randr_output);
510 /* XXX free adjustedmode */
511 drm_mode_destroy(dev, adjusted_mode);
514 // if (scrn->pScreen)
515 // drm_crtc_set_screen_sub_pixel_order(dev);
521 crtc->mode = saved_mode;
525 crtc->funcs->unlock (crtc);
529 EXPORT_SYMBOL(drm_crtc_set_mode);
532 * drm_disable_unused_functions - disable unused objects
536 * Caller must hold mode config lock.
538 * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
539 * by calling its dpms function, which should power it off.
541 void drm_disable_unused_functions(struct drm_device *dev)
543 struct drm_output *output;
544 struct drm_crtc *crtc;
546 list_for_each_entry(output, &dev->mode_config.output_list, head) {
548 (*output->funcs->dpms)(output, DPMSModeOff);
551 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
553 crtc->funcs->dpms(crtc, DPMSModeOff);
558 * drm_mode_probed_add - add a mode to the specified output's probed mode list
559 * @output: output the new mode
563 * Caller must hold mode config lock.
565 * Add @mode to @output's mode list for later use.
567 void drm_mode_probed_add(struct drm_output *output,
568 struct drm_display_mode *mode)
570 list_add(&mode->head, &output->probed_modes);
572 EXPORT_SYMBOL(drm_mode_probed_add);
575 * drm_mode_remove - remove and free a mode
576 * @output: output list to modify
577 * @mode: mode to remove
580 * Caller must hold mode config lock.
582 * Remove @mode from @output's mode list, then free it.
584 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
586 list_del(&mode->head);
589 EXPORT_SYMBOL(drm_mode_remove);
592 * drm_output_create - create a new output
594 * @funcs: callbacks for this output
595 * @name: user visible name of the output
598 * Caller must hold @dev's mode_config lock.
600 * Creates a new drm_output structure and adds it to @dev's mode_config
604 * Pointer to the new output or NULL on error.
606 struct drm_output *drm_output_create(struct drm_device *dev,
607 const struct drm_output_funcs *funcs,
610 struct drm_output *output = NULL;
612 output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
617 output->funcs = funcs;
618 output->id = drm_idr_get(dev, output);
619 output->output_type = output_type;
620 output->output_type_id = 1; /* TODO */
621 output->subpixel_order = SubPixelUnknown;
622 INIT_LIST_HEAD(&output->user_modes);
623 INIT_LIST_HEAD(&output->probed_modes);
624 INIT_LIST_HEAD(&output->modes);
626 /* output_set_monitor(output)? */
627 /* check for output_ignored(output)? */
629 mutex_lock(&dev->mode_config.mutex);
630 list_add_tail(&output->head, &dev->mode_config.output_list);
631 dev->mode_config.num_output++;
633 drm_output_attach_property(output, dev->mode_config.edid_property, 0);
635 drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
637 mutex_unlock(&dev->mode_config.mutex);
642 EXPORT_SYMBOL(drm_output_create);
645 * drm_output_destroy - remove an output
646 * @output: output to remove
649 * Caller must hold @dev's mode_config lock.
651 * Call @output's cleanup function, then remove the output from the DRM
652 * mode_config after freeing @output's modes.
654 void drm_output_destroy(struct drm_output *output)
656 struct drm_device *dev = output->dev;
657 struct drm_display_mode *mode, *t;
659 if (*output->funcs->cleanup)
660 (*output->funcs->cleanup)(output);
662 list_for_each_entry_safe(mode, t, &output->probed_modes, head)
663 drm_mode_remove(output, mode);
665 list_for_each_entry_safe(mode, t, &output->modes, head)
666 drm_mode_remove(output, mode);
668 list_for_each_entry_safe(mode, t, &output->user_modes, head)
669 drm_mode_remove(output, mode);
671 mutex_lock(&dev->mode_config.mutex);
672 drm_idr_put(dev, output->id);
673 list_del(&output->head);
674 mutex_unlock(&dev->mode_config.mutex);
677 EXPORT_SYMBOL(drm_output_destroy);
681 * drm_mode_create - create a new display mode
687 * Create a new drm_display_mode, give it an ID, and return it.
690 * Pointer to new mode on success, NULL on error.
692 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
694 struct drm_display_mode *nmode;
696 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
700 nmode->mode_id = drm_idr_get(dev, nmode);
703 EXPORT_SYMBOL(drm_mode_create);
706 * drm_mode_destroy - remove a mode
708 * @mode: mode to remove
711 * Caller must hold mode config lock.
713 * Free @mode's unique identifier, then free it.
715 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
717 drm_idr_put(dev, mode->mode_id);
721 EXPORT_SYMBOL(drm_mode_destroy);
723 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
727 dev->mode_config.edid_property =
728 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
731 dev->mode_config.dpms_property =
732 drm_property_create(dev, DRM_MODE_PROP_ENUM, "DPMS", 4);
734 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
735 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
737 dev->mode_config.connector_type_property =
738 drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
739 "Connector Type", 10);
740 for (i = 0; i < ARRAY_SIZE(drm_conn_enum_list); i++)
741 drm_property_add_enum(dev->mode_config.connector_type_property, i, drm_conn_enum_list[i].type, drm_conn_enum_list[i].name);
743 dev->mode_config.connector_num_property =
744 drm_property_create(dev, DRM_MODE_PROP_RANGE | DRM_MODE_PROP_IMMUTABLE,
746 dev->mode_config.connector_num_property->values[0] = 0;
747 dev->mode_config.connector_num_property->values[1] = 20;
752 * drm_mode_config_init - initialize DRM mode_configuration structure
756 * None, should happen single threaded at init time.
758 * Initialize @dev's mode_config structure, used for tracking the graphics
759 * configuration of @dev.
761 void drm_mode_config_init(struct drm_device *dev)
763 mutex_init(&dev->mode_config.mutex);
764 INIT_LIST_HEAD(&dev->mode_config.fb_list);
765 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
766 INIT_LIST_HEAD(&dev->mode_config.output_list);
767 INIT_LIST_HEAD(&dev->mode_config.property_list);
768 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
769 idr_init(&dev->mode_config.crtc_idr);
771 drm_mode_create_standard_output_properties(dev);
774 EXPORT_SYMBOL(drm_mode_config_init);
777 * drm_get_buffer_object - find the buffer object for a given handle
779 * @bo: pointer to caller's buffer_object pointer
780 * @handle: handle to lookup
783 * Must take @dev's struct_mutex to protect buffer object lookup.
785 * Given @handle, lookup the buffer object in @dev and put it in the caller's
789 * Zero on success, -EINVAL if the handle couldn't be found.
791 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
793 struct drm_user_object *uo;
794 struct drm_hash_item *hash;
799 mutex_lock(&dev->struct_mutex);
800 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
802 DRM_ERROR("Couldn't find handle.\n");
807 uo = drm_hash_entry(hash, struct drm_user_object, hash);
808 if (uo->type != drm_buffer_type) {
813 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
816 mutex_unlock(&dev->struct_mutex);
821 * drm_pick_crtcs - pick crtcs for output devices
825 * Caller must hold mode config lock.
827 static void drm_pick_crtcs (struct drm_device *dev)
830 struct drm_output *output, *output_equal;
831 struct drm_crtc *crtc;
832 struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
834 list_for_each_entry(output, &dev->mode_config.output_list, head) {
837 /* Don't hook up outputs that are disconnected ??
839 * This is debateable. Do we want fixed /dev/fbX or
840 * dynamic on hotplug (need mode code for that though) ?
842 * If we don't hook up outputs now, then we only create
843 * /dev/fbX for the output that's enabled, that's good as
844 * the users console will be on that output.
846 * If we do hook up outputs that are disconnected now, then
847 * the user may end up having to muck about with the fbcon
848 * map flags to assign his console to the enabled output. Ugh.
850 if (output->status != output_status_connected)
854 list_for_each_entry(des_mode, &output->modes, head) {
855 if (des_mode->type & DRM_MODE_TYPE_PREFERRED)
859 /* No preferred mode, let's just select the first available */
860 if (!des_mode || !(des_mode->type & DRM_MODE_TYPE_PREFERRED)) {
861 list_for_each_entry(des_mode, &output->modes, head) {
868 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
872 if ((output->possible_crtcs & (1 << c)) == 0)
875 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
876 if (output->id == output_equal->id)
879 /* Find out if crtc has been assigned before */
880 if (output_equal->crtc == crtc)
884 #if 1 /* continue for now */
890 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
892 if (output->id == output_equal->id)
895 list_for_each_entry(modes, &output->modes, head) {
896 list_for_each_entry(modes_equal, &output_equal->modes, head) {
897 if (drm_mode_equal (modes, modes_equal)) {
898 if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
899 printk("Cloning %s (0x%lx) to %s (0x%lx)\n",drm_get_output_name(output),output->possible_clones,drm_get_output_name(output_equal),output_equal->possible_clones);
909 /* crtc has been assigned skip it */
913 /* Found a CRTC to attach to, do it ! */
915 output->crtc->desired_mode = des_mode;
916 output->initial_x = 0;
917 output->initial_y = 0;
918 DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
926 * drm_initial_config - setup a sane initial output configuration
928 * @can_grow: this configuration is growable
931 * Called at init time, must take mode config lock.
933 * Scan the CRTCs and outputs and try to put together an initial setup.
934 * At the moment, this is a cloned configuration across all heads with
935 * a new framebuffer object as the backing store.
938 * Zero if everything went ok, nonzero otherwise.
940 bool drm_initial_config(struct drm_device *dev, bool can_grow)
942 struct drm_output *output;
943 struct drm_crtc *crtc;
946 mutex_lock(&dev->mode_config.mutex);
948 drm_crtc_probe_output_modes(dev, 2048, 2048);
952 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
954 /* can't setup the crtc if there's no assigned mode */
955 if (!crtc->desired_mode)
958 /* Now setup the fbdev for attached crtcs */
959 dev->driver->fb_probe(dev, crtc);
962 /* This is a little screwy, as we've already walked the outputs
963 * above, but it's a little bit of magic too. There's the potential
964 * for things not to get setup above if an existing device gets
965 * re-assigned thus confusing the hardware. By walking the outputs
966 * this fixes up their crtc's.
968 list_for_each_entry(output, &dev->mode_config.output_list, head) {
970 /* can't setup the output if there's no assigned mode */
971 if (!output->crtc || !output->crtc->desired_mode)
974 /* and needs an attached fb */
975 if (output->crtc->fb)
976 drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
979 drm_disable_unused_functions(dev);
981 mutex_unlock(&dev->mode_config.mutex);
984 EXPORT_SYMBOL(drm_initial_config);
987 * drm_mode_config_cleanup - free up DRM mode_config info
991 * Caller must hold mode config lock.
993 * Free up all the outputs and CRTCs associated with this DRM device, then
994 * free up the framebuffers and associated buffer objects.
996 * FIXME: cleanup any dangling user buffer objects too
998 void drm_mode_config_cleanup(struct drm_device *dev)
1000 struct drm_output *output, *ot;
1001 struct drm_crtc *crtc, *ct;
1002 struct drm_framebuffer *fb, *fbt;
1003 struct drm_property *property, *pt;
1005 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1006 drm_output_destroy(output);
1009 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1010 drm_property_destroy(dev, property);
1013 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1014 if (fb->bo->type != drm_bo_type_kernel)
1015 drm_framebuffer_destroy(fb);
1017 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1020 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1021 drm_crtc_destroy(crtc);
1025 EXPORT_SYMBOL(drm_mode_config_cleanup);
1028 * drm_crtc_set_config - set a new config from userspace
1029 * @crtc: CRTC to setup
1030 * @crtc_info: user provided configuration
1031 * @new_mode: new mode to set
1032 * @output_set: set of outputs for the new config
1033 * @fb: new framebuffer
1036 * Caller must hold mode config lock.
1038 * Setup a new configuration, provided by the user in @crtc_info, and enable
1044 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)
1046 struct drm_device *dev = crtc->dev;
1047 struct drm_crtc **save_crtcs, *new_crtc;
1048 bool save_enabled = crtc->enabled;
1049 bool changed = false;
1050 struct drm_output *output;
1053 save_crtcs = kzalloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc *), GFP_KERNEL);
1060 if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1063 if (new_mode && !drm_mode_equal(new_mode, &crtc->mode))
1066 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1067 save_crtcs[count++] = output->crtc;
1069 if (output->crtc == crtc)
1072 new_crtc = output->crtc;
1074 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1075 if (output_set[ro] == output)
1078 if (new_crtc != output->crtc) {
1080 output->crtc = new_crtc;
1086 crtc->enabled = (new_mode != NULL);
1087 if (new_mode != NULL) {
1088 DRM_DEBUG("attempting to set mode from userspace\n");
1089 drm_mode_debug_printmodeline(dev, new_mode);
1090 if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1092 crtc->enabled = save_enabled;
1094 list_for_each_entry(output, &dev->mode_config.output_list, head)
1095 output->crtc = save_crtcs[count++];
1099 crtc->desired_x = crtc_info->x;
1100 crtc->desired_y = crtc_info->y;
1101 crtc->desired_mode = new_mode;
1103 drm_disable_unused_functions(dev);
1110 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1111 * @out: drm_mode_modeinfo struct to return to the user
1112 * @in: drm_display_mode to use
1117 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1120 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1122 out->clock = in->clock;
1123 out->hdisplay = in->hdisplay;
1124 out->hsync_start = in->hsync_start;
1125 out->hsync_end = in->hsync_end;
1126 out->htotal = in->htotal;
1127 out->hskew = in->hskew;
1128 out->vdisplay = in->vdisplay;
1129 out->vsync_start = in->vsync_start;
1130 out->vsync_end = in->vsync_end;
1131 out->vtotal = in->vtotal;
1132 out->vscan = in->vscan;
1133 out->vrefresh = in->vrefresh;
1134 out->flags = in->flags;
1135 out->type = in->type;
1136 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1137 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1141 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1142 * @out: drm_display_mode to return to the user
1143 * @in: drm_mode_modeinfo to use
1148 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1151 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1153 out->clock = in->clock;
1154 out->hdisplay = in->hdisplay;
1155 out->hsync_start = in->hsync_start;
1156 out->hsync_end = in->hsync_end;
1157 out->htotal = in->htotal;
1158 out->hskew = in->hskew;
1159 out->vdisplay = in->vdisplay;
1160 out->vsync_start = in->vsync_start;
1161 out->vsync_end = in->vsync_end;
1162 out->vtotal = in->vtotal;
1163 out->vscan = in->vscan;
1164 out->vrefresh = in->vrefresh;
1165 out->flags = in->flags;
1166 out->type = in->type;
1167 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1168 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1172 * drm_mode_getresources - get graphics configuration
1173 * @inode: inode from the ioctl
1174 * @filp: file * from the ioctl
1175 * @cmd: cmd from ioctl
1176 * @arg: arg from ioctl
1179 * Takes mode config lock.
1181 * Construct a set of configuration description structures and return
1182 * them to the user, including CRTC, output and framebuffer configuration.
1184 * Called by the user via ioctl.
1187 * Zero on success, errno on failure.
1189 int drm_mode_getresources(struct drm_device *dev,
1190 void *data, struct drm_file *file_priv)
1192 struct drm_mode_card_res *card_res = data;
1193 struct list_head *lh;
1194 struct drm_framebuffer *fb;
1195 struct drm_output *output;
1196 struct drm_crtc *crtc;
1198 int output_count = 0;
1202 uint32_t __user *fb_id;
1203 uint32_t __user *crtc_id;
1204 uint32_t __user *output_id;
1206 mutex_lock(&dev->mode_config.mutex);
1208 list_for_each(lh, &dev->mode_config.fb_list)
1211 list_for_each(lh, &dev->mode_config.crtc_list)
1214 list_for_each(lh, &dev->mode_config.output_list)
1217 card_res->max_height = dev->mode_config.max_height;
1218 card_res->min_height = dev->mode_config.min_height;
1219 card_res->max_width = dev->mode_config.max_width;
1220 card_res->min_width = dev->mode_config.min_width;
1222 /* handle this in 4 parts */
1224 if (card_res->count_fbs >= fb_count) {
1226 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1227 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1228 if (put_user(fb->id, fb_id + copied)) {
1235 card_res->count_fbs = fb_count;
1238 if (card_res->count_crtcs >= crtc_count) {
1240 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1241 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1242 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1243 if (put_user(crtc->id, crtc_id + copied)) {
1250 card_res->count_crtcs = crtc_count;
1254 if (card_res->count_outputs >= output_count) {
1256 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1257 list_for_each_entry(output, &dev->mode_config.output_list,
1259 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1260 if (put_user(output->id, output_id + copied)) {
1267 card_res->count_outputs = output_count;
1269 DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1270 card_res->count_outputs);
1273 mutex_unlock(&dev->mode_config.mutex);
1278 * drm_mode_getcrtc - get CRTC configuration
1279 * @inode: inode from the ioctl
1280 * @filp: file * from the ioctl
1281 * @cmd: cmd from ioctl
1282 * @arg: arg from ioctl
1287 * Construct a CRTC configuration structure to return to the user.
1289 * Called by the user via ioctl.
1292 * Zero on success, errno on failure.
1294 int drm_mode_getcrtc(struct drm_device *dev,
1295 void *data, struct drm_file *file_priv)
1297 struct drm_mode_crtc *crtc_resp = data;
1298 struct drm_crtc *crtc;
1299 struct drm_output *output;
1303 mutex_lock(&dev->mode_config.mutex);
1304 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1305 if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1310 crtc_resp->x = crtc->x;
1311 crtc_resp->y = crtc->y;
1314 crtc_resp->fb_id = crtc->fb->id;
1316 crtc_resp->fb_id = 0;
1318 crtc_resp->outputs = 0;
1319 if (crtc->enabled) {
1321 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1322 crtc_resp->mode_valid = 1;
1324 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1325 if (output->crtc == crtc)
1326 crtc_resp->outputs |= 1 << (ocount++);
1330 crtc_resp->mode_valid = 0;
1334 mutex_unlock(&dev->mode_config.mutex);
1339 * drm_mode_getoutput - get output configuration
1340 * @inode: inode from the ioctl
1341 * @filp: file * from the ioctl
1342 * @cmd: cmd from ioctl
1343 * @arg: arg from ioctl
1348 * Construct a output configuration structure to return to the user.
1350 * Called by the user via ioctl.
1353 * Zero on success, errno on failure.
1355 int drm_mode_getoutput(struct drm_device *dev,
1356 void *data, struct drm_file *file_priv)
1358 struct drm_mode_get_output *out_resp = data;
1359 struct drm_output *output;
1360 struct drm_display_mode *mode;
1362 int props_count = 0;
1366 struct drm_mode_modeinfo u_mode;
1367 struct drm_mode_modeinfo __user *mode_ptr;
1368 uint32_t __user *prop_ptr;
1369 uint64_t __user *prop_values;
1371 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1373 DRM_DEBUG("output id %d:\n", out_resp->output);
1375 mutex_lock(&dev->mode_config.mutex);
1376 output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1377 if (!output || (output->id != out_resp->output)) {
1382 list_for_each_entry(mode, &output->modes, head)
1385 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1386 if (output->property_ids[i] != 0) {
1391 if (out_resp->count_modes == 0) {
1392 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1395 out_resp->output_type = output->output_type;
1396 out_resp->output_type_id = output->output_type_id;
1397 out_resp->mm_width = output->mm_width;
1398 out_resp->mm_height = output->mm_height;
1399 out_resp->subpixel = output->subpixel_order;
1400 out_resp->connection = output->status;
1402 out_resp->crtc = output->crtc->id;
1406 out_resp->crtcs = output->possible_crtcs;
1407 out_resp->clones = output->possible_clones;
1409 if ((out_resp->count_modes >= mode_count) && mode_count) {
1411 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1412 list_for_each_entry(mode, &output->modes, head) {
1413 drm_crtc_convert_to_umode(&u_mode, mode);
1414 if (copy_to_user(mode_ptr + copied,
1415 &u_mode, sizeof(u_mode))) {
1423 out_resp->count_modes = mode_count;
1425 if ((out_resp->count_props >= props_count) && props_count) {
1427 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1428 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1429 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1430 if (output->property_ids[i] != 0) {
1431 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1436 if (put_user(output->property_values[i], prop_values + copied)) {
1444 out_resp->count_props = props_count;
1447 mutex_unlock(&dev->mode_config.mutex);
1452 * drm_mode_setcrtc - set CRTC configuration
1453 * @inode: inode from the ioctl
1454 * @filp: file * from the ioctl
1455 * @cmd: cmd from ioctl
1456 * @arg: arg from ioctl
1461 * Build a new CRTC configuration based on user request.
1463 * Called by the user via ioctl.
1466 * Zero on success, errno on failure.
1468 int drm_mode_setcrtc(struct drm_device *dev,
1469 void *data, struct drm_file *file_priv)
1471 struct drm_mode_crtc *crtc_req = data;
1472 struct drm_crtc *crtc;
1473 struct drm_output **output_set = NULL, *output;
1474 struct drm_framebuffer *fb = NULL;
1475 struct drm_display_mode *mode = NULL;
1478 uint32_t __user *set_outputs_ptr;
1480 mutex_lock(&dev->mode_config.mutex);
1481 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1482 if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1483 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1488 if (crtc_req->mode_valid) {
1489 /* if we have a mode we need a framebuffer */
1490 if (crtc_req->fb_id) {
1491 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1492 if (!fb || (fb->id != crtc_req->fb_id)) {
1493 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1499 mode = drm_mode_create(dev);
1500 drm_crtc_convert_umode(mode, &crtc_req->mode);
1503 if (crtc_req->count_outputs == 0 && mode) {
1504 DRM_DEBUG("Count outputs is 0 but mode set\n");
1509 if (crtc_req->count_outputs > 0 && !mode && !fb) {
1510 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1515 if (crtc_req->count_outputs > 0) {
1517 output_set = kmalloc(crtc_req->count_outputs *
1518 sizeof(struct drm_output *), GFP_KERNEL);
1524 for (i = 0; i < crtc_req->count_outputs; i++) {
1525 set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1526 if (get_user(out_id, &set_outputs_ptr[i])) {
1531 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1532 if (!output || (out_id != output->id)) {
1533 DRM_DEBUG("Output id %d unknown\n", out_id);
1538 output_set[i] = output;
1542 ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1545 mutex_unlock(&dev->mode_config.mutex);
1550 * drm_mode_addfb - add an FB to the graphics configuration
1551 * @inode: inode from the ioctl
1552 * @filp: file * from the ioctl
1553 * @cmd: cmd from ioctl
1554 * @arg: arg from ioctl
1557 * Takes mode config lock.
1559 * Add a new FB to the specified CRTC, given a user request.
1561 * Called by the user via ioctl.
1564 * Zero on success, errno on failure.
1566 int drm_mode_addfb(struct drm_device *dev,
1567 void *data, struct drm_file *file_priv)
1569 struct drm_mode_fb_cmd *r = data;
1570 struct drm_mode_config *config = &dev->mode_config;
1571 struct drm_framebuffer *fb;
1572 struct drm_buffer_object *bo;
1573 struct drm_crtc *crtc;
1576 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1577 DRM_ERROR("mode new framebuffer width not within limits\n");
1580 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1581 DRM_ERROR("mode new framebuffer height not within limits\n");
1585 mutex_lock(&dev->mode_config.mutex);
1586 /* TODO check limits are okay */
1587 ret = drm_get_buffer_object(dev, &bo, r->handle);
1593 /* TODO check buffer is sufficently large */
1594 /* TODO setup destructor callback */
1596 fb = drm_framebuffer_create(dev);
1602 fb->width = r->width;
1603 fb->height = r->height;
1604 fb->pitch = r->pitch;
1605 fb->bits_per_pixel = r->bpp;
1606 fb->depth = r->depth;
1607 fb->offset = bo->offset;
1610 r->buffer_id = fb->id;
1612 list_add(&fb->filp_head, &file_priv->fbs);
1614 /* FIXME: bind the fb to the right crtc */
1615 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1617 dev->driver->fb_probe(dev, crtc);
1621 mutex_unlock(&dev->mode_config.mutex);
1626 * drm_mode_rmfb - remove an FB from the configuration
1627 * @inode: inode from the ioctl
1628 * @filp: file * from the ioctl
1629 * @cmd: cmd from ioctl
1630 * @arg: arg from ioctl
1633 * Takes mode config lock.
1635 * Remove the FB specified by the user.
1637 * Called by the user via ioctl.
1640 * Zero on success, errno on failure.
1642 int drm_mode_rmfb(struct drm_device *dev,
1643 void *data, struct drm_file *file_priv)
1645 struct drm_framebuffer *fb = 0;
1646 uint32_t *id = data;
1649 mutex_lock(&dev->mode_config.mutex);
1650 fb = idr_find(&dev->mode_config.crtc_idr, *id);
1651 /* TODO check that we realy get a framebuffer back. */
1652 if (!fb || (*id != fb->id)) {
1653 DRM_ERROR("mode invalid framebuffer id\n");
1658 /* TODO check if we own the buffer */
1659 /* TODO release all crtc connected to the framebuffer */
1660 /* bind the fb to the crtc for now */
1661 /* TODO unhock the destructor from the buffer object */
1663 if (fb->bo->type != drm_bo_type_kernel)
1664 drm_framebuffer_destroy(fb);
1666 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1669 mutex_unlock(&dev->mode_config.mutex);
1674 * drm_mode_getfb - get FB info
1675 * @inode: inode from the ioctl
1676 * @filp: file * from the ioctl
1677 * @cmd: cmd from ioctl
1678 * @arg: arg from ioctl
1683 * Lookup the FB given its ID and return info about it.
1685 * Called by the user via ioctl.
1688 * Zero on success, errno on failure.
1690 int drm_mode_getfb(struct drm_device *dev,
1691 void *data, struct drm_file *file_priv)
1693 struct drm_mode_fb_cmd *r = data;
1694 struct drm_framebuffer *fb;
1697 mutex_lock(&dev->mode_config.mutex);
1698 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1699 if (!fb || (r->buffer_id != fb->id)) {
1700 DRM_ERROR("invalid framebuffer id\n");
1705 r->height = fb->height;
1706 r->width = fb->width;
1707 r->depth = fb->depth;
1708 r->bpp = fb->bits_per_pixel;
1709 r->handle = fb->bo->base.hash.key;
1710 r->pitch = fb->pitch;
1713 mutex_unlock(&dev->mode_config.mutex);
1718 * drm_fb_release - remove and free the FBs on this file
1719 * @filp: file * from the ioctl
1722 * Takes mode config lock.
1724 * Destroy all the FBs associated with @filp.
1726 * Called by the user via ioctl.
1729 * Zero on success, errno on failure.
1731 void drm_fb_release(struct file *filp)
1733 struct drm_file *priv = filp->private_data;
1734 struct drm_device *dev = priv->head->dev;
1735 struct drm_framebuffer *fb, *tfb;
1737 mutex_lock(&dev->mode_config.mutex);
1738 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1739 list_del(&fb->filp_head);
1740 if (fb->bo->type != drm_bo_type_kernel)
1741 drm_framebuffer_destroy(fb);
1743 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1745 mutex_unlock(&dev->mode_config.mutex);
1752 static int drm_mode_attachmode(struct drm_device *dev,
1753 struct drm_output *output,
1754 struct drm_display_mode *mode)
1758 list_add_tail(&mode->head, &output->user_modes);
1762 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1763 struct drm_display_mode *mode)
1765 struct drm_output *output;
1767 struct drm_display_mode *dup_mode;
1769 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1770 if (output->crtc == crtc) {
1772 dup_mode = drm_mode_duplicate(dev, mode);
1775 ret = drm_mode_attachmode(dev, output, dup_mode);
1783 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1785 static int drm_mode_detachmode(struct drm_device *dev,
1786 struct drm_output *output,
1787 struct drm_display_mode *mode)
1791 struct drm_display_mode *match_mode, *t;
1793 list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1794 if (drm_mode_equal(match_mode, mode)) {
1795 list_del(&match_mode->head);
1796 drm_mode_destroy(dev, match_mode);
1808 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1810 struct drm_output *output;
1812 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1813 drm_mode_detachmode(dev, output, mode);
1817 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1820 * drm_fb_attachmode - Attach a user mode to an output
1821 * @inode: inode from the ioctl
1822 * @filp: file * from the ioctl
1823 * @cmd: cmd from ioctl
1824 * @arg: arg from ioctl
1826 * This attaches a user specified mode to an output.
1827 * Called by the user via ioctl.
1830 * Zero on success, errno on failure.
1832 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1833 void *data, struct drm_file *file_priv)
1835 struct drm_mode_mode_cmd *mode_cmd = data;
1836 struct drm_output *output;
1837 struct drm_display_mode *mode;
1838 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1841 mutex_lock(&dev->mode_config.mutex);
1843 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1844 if (!output || (output->id != mode_cmd->output_id)) {
1849 mode = drm_mode_create(dev);
1855 drm_crtc_convert_umode(mode, umode);
1857 ret = drm_mode_attachmode(dev, output, mode);
1859 mutex_unlock(&dev->mode_config.mutex);
1865 * drm_fb_detachmode - Detach a user specified mode from an output
1866 * @inode: inode from the ioctl
1867 * @filp: file * from the ioctl
1868 * @cmd: cmd from ioctl
1869 * @arg: arg from ioctl
1871 * Called by the user via ioctl.
1874 * Zero on success, errno on failure.
1876 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1877 void *data, struct drm_file *file_priv)
1879 struct drm_mode_mode_cmd *mode_cmd = data;
1880 struct drm_output *output;
1881 struct drm_display_mode mode;
1882 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1885 mutex_lock(&dev->mode_config.mutex);
1887 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1888 if (!output || (output->id != mode_cmd->output_id)) {
1893 drm_crtc_convert_umode(&mode, umode);
1894 ret = drm_mode_detachmode(dev, output, &mode);
1896 mutex_unlock(&dev->mode_config.mutex);
1900 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1901 const char *name, int num_values)
1903 struct drm_property *property = NULL;
1905 property = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
1910 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1911 if (!property->values)
1915 property->id = drm_idr_get(dev, property);
1916 property->flags = flags;
1917 property->num_values = num_values;
1918 INIT_LIST_HEAD(&property->enum_blob_list);
1921 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1923 list_add_tail(&property->head, &dev->mode_config.property_list);
1929 EXPORT_SYMBOL(drm_property_create);
1931 int drm_property_add_enum(struct drm_property *property, int index,
1932 uint64_t value, const char *name)
1934 struct drm_property_enum *prop_enum;
1936 if (!(property->flags & DRM_MODE_PROP_ENUM))
1939 if (!list_empty(&property->enum_blob_list)) {
1940 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1941 if (prop_enum->value == value) {
1942 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1943 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1949 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1953 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1954 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1955 prop_enum->value = value;
1957 property->values[index] = value;
1958 list_add_tail(&prop_enum->head, &property->enum_blob_list);
1961 EXPORT_SYMBOL(drm_property_add_enum);
1963 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1965 struct drm_property_enum *prop_enum, *pt;
1967 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1968 list_del(&prop_enum->head);
1972 if (property->num_values)
1973 kfree(property->values);
1974 drm_idr_put(dev, property->id);
1975 list_del(&property->head);
1978 EXPORT_SYMBOL(drm_property_destroy);
1980 int drm_output_attach_property(struct drm_output *output,
1981 struct drm_property *property, uint64_t init_val)
1985 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1986 if (output->property_ids[i] == 0) {
1987 output->property_ids[i] = property->id;
1988 output->property_values[i] = init_val;
1993 if (i == DRM_OUTPUT_MAX_PROPERTY)
1997 EXPORT_SYMBOL(drm_output_attach_property);
1999 int drm_output_property_set_value(struct drm_output *output,
2000 struct drm_property *property, uint64_t value)
2004 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2005 if (output->property_ids[i] == property->id) {
2006 output->property_values[i] = value;
2011 if (i == DRM_OUTPUT_MAX_PROPERTY)
2015 EXPORT_SYMBOL(drm_output_property_set_value);
2017 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2018 void *data, struct drm_file *file_priv)
2020 struct drm_mode_get_property *out_resp = data;
2021 struct drm_property *property;
2024 int value_count = 0;
2027 struct drm_property_enum *prop_enum;
2028 struct drm_mode_property_enum __user *enum_ptr;
2029 struct drm_property_blob *prop_blob;
2030 uint32_t *blob_id_ptr;
2031 uint64_t __user *values_ptr;
2032 uint32_t __user *blob_length_ptr;
2034 mutex_lock(&dev->mode_config.mutex);
2035 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2036 if (!property || (property->id != out_resp->prop_id)) {
2041 if (property->flags & DRM_MODE_PROP_ENUM) {
2042 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2044 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2045 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2049 value_count = property->num_values;
2051 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2052 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2053 out_resp->flags = property->flags;
2055 if ((out_resp->count_values >= value_count) && value_count) {
2056 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2057 for (i = 0; i < value_count; i++) {
2058 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2064 out_resp->count_values = value_count;
2066 if (property->flags & DRM_MODE_PROP_ENUM) {
2068 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2070 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2071 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2073 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2078 if (copy_to_user(&enum_ptr[copied].name,
2079 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2086 out_resp->count_enum_blobs = enum_count;
2089 if (property->flags & DRM_MODE_PROP_BLOB) {
2090 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2092 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2093 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2095 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2096 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2101 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2109 out_resp->count_enum_blobs = enum_count;
2112 mutex_unlock(&dev->mode_config.mutex);
2116 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2119 struct drm_property_blob *blob;
2121 if (!length || !data)
2124 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2128 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2129 blob->length = length;
2131 memcpy(blob->data, data, length);
2133 blob->id = drm_idr_get(dev, blob);
2135 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2139 static void drm_property_destroy_blob(struct drm_device *dev,
2140 struct drm_property_blob *blob)
2142 drm_idr_put(dev, blob->id);
2143 list_del(&blob->head);
2147 int drm_mode_getblob_ioctl(struct drm_device *dev,
2148 void *data, struct drm_file *file_priv)
2150 struct drm_mode_get_blob *out_resp = data;
2151 struct drm_property_blob *blob;
2155 mutex_lock(&dev->mode_config.mutex);
2157 blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2158 if (!blob || (blob->id != out_resp->blob_id)) {
2163 if (out_resp->length == blob->length) {
2164 blob_ptr = (void *)(unsigned long)out_resp->data;
2165 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2170 out_resp->length = blob->length;
2173 mutex_unlock(&dev->mode_config.mutex);
2177 int drm_mode_output_update_edid_property(struct drm_output *output, unsigned char *edid)
2179 struct drm_device *dev = output->dev;
2181 if (output->edid_blob_ptr)
2182 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2184 output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2186 ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2189 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2191 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2192 void *data, struct drm_file *file_priv)
2194 struct drm_mode_output_set_property *out_resp = data;
2195 struct drm_property *property;
2196 struct drm_output *output;
2200 mutex_lock(&dev->mode_config.mutex);
2201 output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2202 if (!output || (output->id != out_resp->output_id)) {
2206 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2207 if (output->property_ids[i] == out_resp->prop_id)
2211 if (i == DRM_OUTPUT_MAX_PROPERTY) {
2215 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2216 if (!property || (property->id != out_resp->prop_id)) {
2220 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2223 if (property->flags & DRM_MODE_PROP_RANGE) {
2224 if (out_resp->value < property->values[0])
2227 if (out_resp->value > property->values[1])
2231 for (i = 0; i < property->num_values; i++) {
2232 if (property->values[i] == out_resp->value) {
2242 if (output->funcs->set_property)
2243 ret = output->funcs->set_property(output, property, out_resp->value);
2246 mutex_unlock(&dev->mode_config.mutex);