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 mutex_unlock(&dev->mode_config.mutex);
169 DRM_ERROR("Attempt to add multiple framebuffers failed\n");
173 fb = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
177 fb->id = drm_idr_get(dev, fb);
179 dev->mode_config.num_fb++;
180 list_add(&fb->head, &dev->mode_config.fb_list);
184 EXPORT_SYMBOL(drm_framebuffer_create);
187 * drm_framebuffer_destroy - remove a framebuffer object
188 * @fb: framebuffer to remove
191 * Caller must hold mode config lock.
193 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
194 * it, setting it to NULL.
196 void drm_framebuffer_destroy(struct drm_framebuffer *fb)
198 struct drm_device *dev = fb->dev;
199 struct drm_crtc *crtc;
201 /* remove from any CRTC */
202 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
207 drm_idr_put(dev, fb->id);
209 dev->mode_config.num_fb--;
213 EXPORT_SYMBOL(drm_framebuffer_destroy);
216 * drm_crtc_create - create a new CRTC object
218 * @funcs: callbacks for the new CRTC
221 * Caller must hold mode config lock.
223 * Creates a new CRTC object and adds it to @dev's mode_config structure.
226 * Pointer to new CRTC object or NULL on error.
228 struct drm_crtc *drm_crtc_create(struct drm_device *dev,
229 const struct drm_crtc_funcs *funcs)
231 struct drm_crtc *crtc;
233 crtc = kzalloc(sizeof(struct drm_crtc), GFP_KERNEL);
240 crtc->id = drm_idr_get(dev, crtc);
242 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
243 dev->mode_config.num_crtc++;
247 EXPORT_SYMBOL(drm_crtc_create);
250 * drm_crtc_destroy - remove a CRTC object
251 * @crtc: CRTC to remove
254 * Caller must hold mode config lock.
256 * Cleanup @crtc. Calls @crtc's cleanup function, then removes @crtc from
257 * its associated DRM device's mode_config. Frees it afterwards.
259 void drm_crtc_destroy(struct drm_crtc *crtc)
261 struct drm_device *dev = crtc->dev;
263 if (crtc->funcs->cleanup)
264 (*crtc->funcs->cleanup)(crtc);
266 drm_idr_put(dev, crtc->id);
267 list_del(&crtc->head);
268 dev->mode_config.num_crtc--;
271 EXPORT_SYMBOL(drm_crtc_destroy);
274 * drm_crtc_in_use - check if a given CRTC is in a mode_config
275 * @crtc: CRTC to check
278 * Caller must hold mode config lock.
280 * Walk @crtc's DRM device's mode_config and see if it's in use.
283 * True if @crtc is part of the mode_config, false otherwise.
285 bool drm_crtc_in_use(struct drm_crtc *crtc)
287 struct drm_output *output;
288 struct drm_device *dev = crtc->dev;
289 /* FIXME: Locking around list access? */
290 list_for_each_entry(output, &dev->mode_config.output_list, head)
291 if (output->crtc == crtc)
295 EXPORT_SYMBOL(drm_crtc_in_use);
298 * Detailed mode info for a standard 640x480@60Hz monitor
300 static struct drm_display_mode std_mode[] = {
301 { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
302 752, 800, 0, 480, 490, 492, 525, 0,
303 V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
307 * drm_crtc_probe_output_modes - get complete set of display modes
309 * @maxX: max width for modes
310 * @maxY: max height for modes
313 * Caller must hold mode config lock.
315 * Based on @dev's mode_config layout, scan all the outputs and try to detect
316 * modes on them. Modes will first be added to the output's probed_modes
317 * list, then culled (based on validity and the @maxX, @maxY parameters) and
318 * put into the normal modes list.
320 * Intended to be used either at bootup time or when major configuration
321 * changes have occurred.
323 * FIXME: take into account monitor limits
325 void drm_crtc_probe_single_output_modes(struct drm_output *output, int maxX, int maxY)
327 struct drm_device *dev = output->dev;
328 struct drm_display_mode *mode, *t;
330 //if (maxX == 0 || maxY == 0)
333 /* set all modes to the unverified state */
334 list_for_each_entry_safe(mode, t, &output->modes, head)
335 mode->status = MODE_UNVERIFIED;
337 output->status = (*output->funcs->detect)(output);
339 if (output->status == output_status_disconnected) {
340 DRM_DEBUG("%s is disconnected\n", drm_get_output_name(output));
341 /* TODO set EDID to NULL */
345 ret = (*output->funcs->get_modes)(output);
348 drm_mode_output_list_update(output);
352 drm_mode_validate_size(dev, &output->modes, maxX,
354 list_for_each_entry_safe(mode, t, &output->modes, head) {
355 if (mode->status == MODE_OK)
356 mode->status = (*output->funcs->mode_valid)(output,mode);
360 drm_mode_prune_invalid(dev, &output->modes, TRUE);
362 if (list_empty(&output->modes)) {
363 struct drm_display_mode *stdmode;
365 DRM_DEBUG("No valid modes on %s\n", drm_get_output_name(output));
367 /* Should we do this here ???
368 * When no valid EDID modes are available we end up
369 * here and bailed in the past, now we add a standard
370 * 640x480@60Hz mode and carry on.
372 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
373 drm_mode_probed_add(output, stdmode);
374 drm_mode_list_concat(&output->probed_modes,
377 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
378 drm_get_output_name(output));
381 drm_mode_sort(&output->modes);
383 DRM_DEBUG("Probed modes for %s\n", drm_get_output_name(output));
384 list_for_each_entry_safe(mode, t, &output->modes, head) {
385 mode->vrefresh = drm_mode_vrefresh(mode);
387 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
388 drm_mode_debug_printmodeline(dev, mode);
392 void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY)
394 struct drm_output *output;
396 list_for_each_entry(output, &dev->mode_config.output_list, head) {
397 drm_crtc_probe_single_output_modes(output, maxX, maxY);
402 * drm_crtc_set_mode - set a mode
403 * @crtc: CRTC to program
409 * Caller must hold mode config lock.
411 * Try to set @mode on @crtc. Give @crtc and its associated outputs a chance
412 * to fixup or reject the mode prior to trying to set it.
415 * True if the mode was set successfully, or false otherwise.
417 bool drm_crtc_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
420 struct drm_device *dev = crtc->dev;
421 struct drm_display_mode *adjusted_mode, saved_mode;
422 int saved_x, saved_y;
423 bool didLock = false;
425 struct drm_output *output;
427 adjusted_mode = drm_mode_duplicate(dev, mode);
429 crtc->enabled = drm_crtc_in_use(crtc);
431 if (!crtc->enabled) {
435 didLock = crtc->funcs->lock(crtc);
437 saved_mode = crtc->mode;
441 /* Update crtc values up front so the driver can rely on them for mode
448 /* XXX short-circuit changes to base location only */
450 /* Pass our mode to the outputs and the CRTC to give them a chance to
451 * adjust it according to limitations or output properties, and also
452 * a chance to reject the mode entirely.
454 list_for_each_entry(output, &dev->mode_config.output_list, head) {
456 if (output->crtc != crtc)
459 if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
464 if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
468 /* Prepare the outputs and CRTCs before setting the mode. */
469 list_for_each_entry(output, &dev->mode_config.output_list, head) {
471 if (output->crtc != crtc)
474 /* Disable the output as the first thing we do. */
475 output->funcs->prepare(output);
478 crtc->funcs->prepare(crtc);
480 /* Set up the DPLL and any output state that needs to adjust or depend
483 crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
485 list_for_each_entry(output, &dev->mode_config.output_list, head) {
487 if (output->crtc != crtc)
490 DRM_INFO("%s: set mode %s %x\n", drm_get_output_name(output), mode->name, mode->mode_id);
492 output->funcs->mode_set(output, mode, adjusted_mode);
495 /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
496 crtc->funcs->commit(crtc);
498 list_for_each_entry(output, &dev->mode_config.output_list, head) {
500 if (output->crtc != crtc)
503 output->funcs->commit(output);
505 #if 0 // TODO def RANDR_12_INTERFACE
506 if (output->randr_output)
507 RRPostPendingProperties (output->randr_output);
511 /* XXX free adjustedmode */
512 drm_mode_destroy(dev, adjusted_mode);
515 // if (scrn->pScreen)
516 // drm_crtc_set_screen_sub_pixel_order(dev);
522 crtc->mode = saved_mode;
526 crtc->funcs->unlock (crtc);
530 EXPORT_SYMBOL(drm_crtc_set_mode);
533 * drm_disable_unused_functions - disable unused objects
537 * Caller must hold mode config lock.
539 * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
540 * by calling its dpms function, which should power it off.
542 void drm_disable_unused_functions(struct drm_device *dev)
544 struct drm_output *output;
545 struct drm_crtc *crtc;
547 list_for_each_entry(output, &dev->mode_config.output_list, head) {
549 (*output->funcs->dpms)(output, DPMSModeOff);
552 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
554 crtc->funcs->dpms(crtc, DPMSModeOff);
559 * drm_mode_probed_add - add a mode to the specified output's probed mode list
560 * @output: output the new mode
564 * Caller must hold mode config lock.
566 * Add @mode to @output's mode list for later use.
568 void drm_mode_probed_add(struct drm_output *output,
569 struct drm_display_mode *mode)
571 list_add(&mode->head, &output->probed_modes);
573 EXPORT_SYMBOL(drm_mode_probed_add);
576 * drm_mode_remove - remove and free a mode
577 * @output: output list to modify
578 * @mode: mode to remove
581 * Caller must hold mode config lock.
583 * Remove @mode from @output's mode list, then free it.
585 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
587 list_del(&mode->head);
590 EXPORT_SYMBOL(drm_mode_remove);
593 * drm_output_create - create a new output
595 * @funcs: callbacks for this output
596 * @name: user visible name of the output
599 * Caller must hold @dev's mode_config lock.
601 * Creates a new drm_output structure and adds it to @dev's mode_config
605 * Pointer to the new output or NULL on error.
607 struct drm_output *drm_output_create(struct drm_device *dev,
608 const struct drm_output_funcs *funcs,
611 struct drm_output *output = NULL;
613 output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
618 output->funcs = funcs;
619 output->id = drm_idr_get(dev, output);
620 output->output_type = output_type;
621 output->output_type_id = 1; /* TODO */
622 output->subpixel_order = SubPixelUnknown;
623 INIT_LIST_HEAD(&output->user_modes);
624 INIT_LIST_HEAD(&output->probed_modes);
625 INIT_LIST_HEAD(&output->modes);
627 /* output_set_monitor(output)? */
628 /* check for output_ignored(output)? */
630 mutex_lock(&dev->mode_config.mutex);
631 list_add_tail(&output->head, &dev->mode_config.output_list);
632 dev->mode_config.num_output++;
634 drm_output_attach_property(output, dev->mode_config.edid_property, 0);
636 drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
638 mutex_unlock(&dev->mode_config.mutex);
643 EXPORT_SYMBOL(drm_output_create);
646 * drm_output_destroy - remove an output
647 * @output: output to remove
650 * Caller must hold @dev's mode_config lock.
652 * Call @output's cleanup function, then remove the output from the DRM
653 * mode_config after freeing @output's modes.
655 void drm_output_destroy(struct drm_output *output)
657 struct drm_device *dev = output->dev;
658 struct drm_display_mode *mode, *t;
660 if (*output->funcs->cleanup)
661 (*output->funcs->cleanup)(output);
663 list_for_each_entry_safe(mode, t, &output->probed_modes, head)
664 drm_mode_remove(output, mode);
666 list_for_each_entry_safe(mode, t, &output->modes, head)
667 drm_mode_remove(output, mode);
669 list_for_each_entry_safe(mode, t, &output->user_modes, head)
670 drm_mode_remove(output, mode);
672 mutex_lock(&dev->mode_config.mutex);
673 drm_idr_put(dev, output->id);
674 list_del(&output->head);
675 mutex_unlock(&dev->mode_config.mutex);
678 EXPORT_SYMBOL(drm_output_destroy);
682 * drm_mode_create - create a new display mode
688 * Create a new drm_display_mode, give it an ID, and return it.
691 * Pointer to new mode on success, NULL on error.
693 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
695 struct drm_display_mode *nmode;
697 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
701 nmode->mode_id = drm_idr_get(dev, nmode);
704 EXPORT_SYMBOL(drm_mode_create);
707 * drm_mode_destroy - remove a mode
709 * @mode: mode to remove
712 * Caller must hold mode config lock.
714 * Free @mode's unique identifier, then free it.
716 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
718 drm_idr_put(dev, mode->mode_id);
722 EXPORT_SYMBOL(drm_mode_destroy);
724 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
728 dev->mode_config.edid_property =
729 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
732 dev->mode_config.dpms_property =
733 drm_property_create(dev, DRM_MODE_PROP_ENUM, "DPMS", 4);
735 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
736 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
738 dev->mode_config.connector_type_property =
739 drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
740 "Connector Type", 10);
741 for (i = 0; i < ARRAY_SIZE(drm_conn_enum_list); i++)
742 drm_property_add_enum(dev->mode_config.connector_type_property, i, drm_conn_enum_list[i].type, drm_conn_enum_list[i].name);
744 dev->mode_config.connector_num_property =
745 drm_property_create(dev, DRM_MODE_PROP_RANGE | DRM_MODE_PROP_IMMUTABLE,
747 dev->mode_config.connector_num_property->values[0] = 0;
748 dev->mode_config.connector_num_property->values[1] = 20;
753 * drm_mode_config_init - initialize DRM mode_configuration structure
757 * None, should happen single threaded at init time.
759 * Initialize @dev's mode_config structure, used for tracking the graphics
760 * configuration of @dev.
762 void drm_mode_config_init(struct drm_device *dev)
764 mutex_init(&dev->mode_config.mutex);
765 INIT_LIST_HEAD(&dev->mode_config.fb_list);
766 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
767 INIT_LIST_HEAD(&dev->mode_config.output_list);
768 INIT_LIST_HEAD(&dev->mode_config.property_list);
769 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
770 idr_init(&dev->mode_config.crtc_idr);
772 drm_mode_create_standard_output_properties(dev);
775 EXPORT_SYMBOL(drm_mode_config_init);
778 * drm_get_buffer_object - find the buffer object for a given handle
780 * @bo: pointer to caller's buffer_object pointer
781 * @handle: handle to lookup
784 * Must take @dev's struct_mutex to protect buffer object lookup.
786 * Given @handle, lookup the buffer object in @dev and put it in the caller's
790 * Zero on success, -EINVAL if the handle couldn't be found.
792 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
794 struct drm_user_object *uo;
795 struct drm_hash_item *hash;
800 mutex_lock(&dev->struct_mutex);
801 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
803 DRM_ERROR("Couldn't find handle.\n");
808 uo = drm_hash_entry(hash, struct drm_user_object, hash);
809 if (uo->type != drm_buffer_type) {
814 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
817 mutex_unlock(&dev->struct_mutex);
822 * drm_pick_crtcs - pick crtcs for output devices
826 * Caller must hold mode config lock.
828 static void drm_pick_crtcs (struct drm_device *dev)
831 struct drm_output *output, *output_equal;
832 struct drm_crtc *crtc;
833 struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
835 list_for_each_entry(output, &dev->mode_config.output_list, head) {
838 /* Don't hook up outputs that are disconnected ??
840 * This is debateable. Do we want fixed /dev/fbX or
841 * dynamic on hotplug (need mode code for that though) ?
843 * If we don't hook up outputs now, then we only create
844 * /dev/fbX for the output that's enabled, that's good as
845 * the users console will be on that output.
847 * If we do hook up outputs that are disconnected now, then
848 * the user may end up having to muck about with the fbcon
849 * map flags to assign his console to the enabled output. Ugh.
851 if (output->status != output_status_connected)
855 list_for_each_entry(des_mode, &output->modes, head) {
856 if (des_mode->type & DRM_MODE_TYPE_PREFERRED)
860 /* No preferred mode, let's just select the first available */
861 if (!des_mode || !(des_mode->type & DRM_MODE_TYPE_PREFERRED)) {
862 list_for_each_entry(des_mode, &output->modes, head) {
869 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
873 if ((output->possible_crtcs & (1 << c)) == 0)
876 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
877 if (output->id == output_equal->id)
880 /* Find out if crtc has been assigned before */
881 if (output_equal->crtc == crtc)
885 #if 1 /* continue for now */
891 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
893 if (output->id == output_equal->id)
896 list_for_each_entry(modes, &output->modes, head) {
897 list_for_each_entry(modes_equal, &output_equal->modes, head) {
898 if (drm_mode_equal (modes, modes_equal)) {
899 if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
900 printk("Cloning %s (0x%lx) to %s (0x%lx)\n",drm_get_output_name(output),output->possible_clones,drm_get_output_name(output_equal),output_equal->possible_clones);
910 /* crtc has been assigned skip it */
914 /* Found a CRTC to attach to, do it ! */
916 output->crtc->desired_mode = des_mode;
917 output->initial_x = 0;
918 output->initial_y = 0;
919 DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
927 * drm_initial_config - setup a sane initial output configuration
929 * @can_grow: this configuration is growable
932 * Called at init time, must take mode config lock.
934 * Scan the CRTCs and outputs and try to put together an initial setup.
935 * At the moment, this is a cloned configuration across all heads with
936 * a new framebuffer object as the backing store.
939 * Zero if everything went ok, nonzero otherwise.
941 bool drm_initial_config(struct drm_device *dev, bool can_grow)
943 struct drm_output *output;
944 struct drm_crtc *crtc;
947 mutex_lock(&dev->mode_config.mutex);
949 drm_crtc_probe_output_modes(dev, 2048, 2048);
953 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
955 /* can't setup the crtc if there's no assigned mode */
956 if (!crtc->desired_mode)
959 /* Now setup the fbdev for attached crtcs */
960 dev->driver->fb_probe(dev, crtc);
963 /* This is a little screwy, as we've already walked the outputs
964 * above, but it's a little bit of magic too. There's the potential
965 * for things not to get setup above if an existing device gets
966 * re-assigned thus confusing the hardware. By walking the outputs
967 * this fixes up their crtc's.
969 list_for_each_entry(output, &dev->mode_config.output_list, head) {
971 /* can't setup the output if there's no assigned mode */
972 if (!output->crtc || !output->crtc->desired_mode)
975 /* and needs an attached fb */
976 if (output->crtc->fb)
977 drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
980 drm_disable_unused_functions(dev);
982 mutex_unlock(&dev->mode_config.mutex);
985 EXPORT_SYMBOL(drm_initial_config);
988 * drm_mode_config_cleanup - free up DRM mode_config info
992 * Caller must hold mode config lock.
994 * Free up all the outputs and CRTCs associated with this DRM device, then
995 * free up the framebuffers and associated buffer objects.
997 * FIXME: cleanup any dangling user buffer objects too
999 void drm_mode_config_cleanup(struct drm_device *dev)
1001 struct drm_output *output, *ot;
1002 struct drm_crtc *crtc, *ct;
1003 struct drm_framebuffer *fb, *fbt;
1004 struct drm_property *property, *pt;
1006 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1007 drm_output_destroy(output);
1010 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1011 drm_property_destroy(dev, property);
1014 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1015 if (fb->bo->type != drm_bo_type_kernel)
1016 drm_framebuffer_destroy(fb);
1018 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1021 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1022 drm_crtc_destroy(crtc);
1026 EXPORT_SYMBOL(drm_mode_config_cleanup);
1029 * drm_crtc_set_config - set a new config from userspace
1030 * @crtc: CRTC to setup
1031 * @crtc_info: user provided configuration
1032 * @new_mode: new mode to set
1033 * @output_set: set of outputs for the new config
1034 * @fb: new framebuffer
1037 * Caller must hold mode config lock.
1039 * Setup a new configuration, provided by the user in @crtc_info, and enable
1045 int drm_crtc_set_config(struct drm_crtc *crtc, struct drm_mode_crtc *crtc_info, struct drm_display_mode *new_mode, struct drm_output **output_set, struct drm_framebuffer *fb)
1047 struct drm_device *dev = crtc->dev;
1048 struct drm_crtc **save_crtcs, *new_crtc;
1049 bool save_enabled = crtc->enabled;
1050 bool changed = false;
1051 struct drm_output *output;
1054 save_crtcs = kzalloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc *), GFP_KERNEL);
1061 if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1064 if (new_mode && !drm_mode_equal(new_mode, &crtc->mode))
1067 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1068 save_crtcs[count++] = output->crtc;
1070 if (output->crtc == crtc)
1073 new_crtc = output->crtc;
1075 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1076 if (output_set[ro] == output)
1079 if (new_crtc != output->crtc) {
1081 output->crtc = new_crtc;
1087 crtc->enabled = (new_mode != NULL);
1088 if (new_mode != NULL) {
1089 DRM_DEBUG("attempting to set mode from userspace\n");
1090 drm_mode_debug_printmodeline(dev, new_mode);
1091 if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1093 crtc->enabled = save_enabled;
1095 list_for_each_entry(output, &dev->mode_config.output_list, head)
1096 output->crtc = save_crtcs[count++];
1100 crtc->desired_x = crtc_info->x;
1101 crtc->desired_y = crtc_info->y;
1102 crtc->desired_mode = new_mode;
1104 drm_disable_unused_functions(dev);
1111 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1112 * @out: drm_mode_modeinfo struct to return to the user
1113 * @in: drm_display_mode to use
1118 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1121 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1123 out->clock = in->clock;
1124 out->hdisplay = in->hdisplay;
1125 out->hsync_start = in->hsync_start;
1126 out->hsync_end = in->hsync_end;
1127 out->htotal = in->htotal;
1128 out->hskew = in->hskew;
1129 out->vdisplay = in->vdisplay;
1130 out->vsync_start = in->vsync_start;
1131 out->vsync_end = in->vsync_end;
1132 out->vtotal = in->vtotal;
1133 out->vscan = in->vscan;
1134 out->vrefresh = in->vrefresh;
1135 out->flags = in->flags;
1136 out->type = in->type;
1137 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1138 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1142 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1143 * @out: drm_display_mode to return to the user
1144 * @in: drm_mode_modeinfo to use
1149 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1152 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1154 out->clock = in->clock;
1155 out->hdisplay = in->hdisplay;
1156 out->hsync_start = in->hsync_start;
1157 out->hsync_end = in->hsync_end;
1158 out->htotal = in->htotal;
1159 out->hskew = in->hskew;
1160 out->vdisplay = in->vdisplay;
1161 out->vsync_start = in->vsync_start;
1162 out->vsync_end = in->vsync_end;
1163 out->vtotal = in->vtotal;
1164 out->vscan = in->vscan;
1165 out->vrefresh = in->vrefresh;
1166 out->flags = in->flags;
1167 out->type = in->type;
1168 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1169 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1173 * drm_mode_getresources - get graphics configuration
1174 * @inode: inode from the ioctl
1175 * @filp: file * from the ioctl
1176 * @cmd: cmd from ioctl
1177 * @arg: arg from ioctl
1180 * Takes mode config lock.
1182 * Construct a set of configuration description structures and return
1183 * them to the user, including CRTC, output and framebuffer configuration.
1185 * Called by the user via ioctl.
1188 * Zero on success, errno on failure.
1190 int drm_mode_getresources(struct drm_device *dev,
1191 void *data, struct drm_file *file_priv)
1193 struct drm_mode_card_res *card_res = data;
1194 struct list_head *lh;
1195 struct drm_framebuffer *fb;
1196 struct drm_output *output;
1197 struct drm_crtc *crtc;
1199 int output_count = 0;
1203 uint32_t __user *fb_id;
1204 uint32_t __user *crtc_id;
1205 uint32_t __user *output_id;
1207 mutex_lock(&dev->mode_config.mutex);
1209 list_for_each(lh, &dev->mode_config.fb_list)
1212 list_for_each(lh, &dev->mode_config.crtc_list)
1215 list_for_each(lh, &dev->mode_config.output_list)
1218 card_res->max_height = dev->mode_config.max_height;
1219 card_res->min_height = dev->mode_config.min_height;
1220 card_res->max_width = dev->mode_config.max_width;
1221 card_res->min_width = dev->mode_config.min_width;
1223 /* handle this in 4 parts */
1225 if (card_res->count_fbs >= fb_count) {
1227 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1228 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1229 if (put_user(fb->id, fb_id + copied)) {
1236 card_res->count_fbs = fb_count;
1239 if (card_res->count_crtcs >= crtc_count) {
1241 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1242 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1243 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1244 if (put_user(crtc->id, crtc_id + copied)) {
1251 card_res->count_crtcs = crtc_count;
1255 if (card_res->count_outputs >= output_count) {
1257 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1258 list_for_each_entry(output, &dev->mode_config.output_list,
1260 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1261 if (put_user(output->id, output_id + copied)) {
1268 card_res->count_outputs = output_count;
1270 DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1271 card_res->count_outputs);
1274 mutex_unlock(&dev->mode_config.mutex);
1279 * drm_mode_getcrtc - get CRTC configuration
1280 * @inode: inode from the ioctl
1281 * @filp: file * from the ioctl
1282 * @cmd: cmd from ioctl
1283 * @arg: arg from ioctl
1288 * Construct a CRTC configuration structure to return to the user.
1290 * Called by the user via ioctl.
1293 * Zero on success, errno on failure.
1295 int drm_mode_getcrtc(struct drm_device *dev,
1296 void *data, struct drm_file *file_priv)
1298 struct drm_mode_crtc *crtc_resp = data;
1299 struct drm_crtc *crtc;
1300 struct drm_output *output;
1304 mutex_lock(&dev->mode_config.mutex);
1305 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1306 if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1311 crtc_resp->x = crtc->x;
1312 crtc_resp->y = crtc->y;
1315 crtc_resp->fb_id = crtc->fb->id;
1317 crtc_resp->fb_id = 0;
1319 crtc_resp->outputs = 0;
1320 if (crtc->enabled) {
1322 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1323 crtc_resp->mode_valid = 1;
1325 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1326 if (output->crtc == crtc)
1327 crtc_resp->outputs |= 1 << (ocount++);
1331 crtc_resp->mode_valid = 0;
1335 mutex_unlock(&dev->mode_config.mutex);
1340 * drm_mode_getoutput - get output configuration
1341 * @inode: inode from the ioctl
1342 * @filp: file * from the ioctl
1343 * @cmd: cmd from ioctl
1344 * @arg: arg from ioctl
1349 * Construct a output configuration structure to return to the user.
1351 * Called by the user via ioctl.
1354 * Zero on success, errno on failure.
1356 int drm_mode_getoutput(struct drm_device *dev,
1357 void *data, struct drm_file *file_priv)
1359 struct drm_mode_get_output *out_resp = data;
1360 struct drm_output *output;
1361 struct drm_display_mode *mode;
1363 int props_count = 0;
1367 struct drm_mode_modeinfo u_mode;
1368 struct drm_mode_modeinfo __user *mode_ptr;
1369 uint32_t __user *prop_ptr;
1370 uint64_t __user *prop_values;
1372 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1374 DRM_DEBUG("output id %d:\n", out_resp->output);
1376 mutex_lock(&dev->mode_config.mutex);
1377 output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1378 if (!output || (output->id != out_resp->output)) {
1383 list_for_each_entry(mode, &output->modes, head)
1386 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1387 if (output->property_ids[i] != 0) {
1392 if (out_resp->count_modes == 0) {
1393 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1396 out_resp->output_type = output->output_type;
1397 out_resp->output_type_id = output->output_type_id;
1398 out_resp->mm_width = output->mm_width;
1399 out_resp->mm_height = output->mm_height;
1400 out_resp->subpixel = output->subpixel_order;
1401 out_resp->connection = output->status;
1403 out_resp->crtc = output->crtc->id;
1407 out_resp->crtcs = output->possible_crtcs;
1408 out_resp->clones = output->possible_clones;
1410 if ((out_resp->count_modes >= mode_count) && mode_count) {
1412 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1413 list_for_each_entry(mode, &output->modes, head) {
1414 drm_crtc_convert_to_umode(&u_mode, mode);
1415 if (copy_to_user(mode_ptr + copied,
1416 &u_mode, sizeof(u_mode))) {
1424 out_resp->count_modes = mode_count;
1426 if ((out_resp->count_props >= props_count) && props_count) {
1428 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1429 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1430 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1431 if (output->property_ids[i] != 0) {
1432 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1437 if (put_user(output->property_values[i], prop_values + copied)) {
1445 out_resp->count_props = props_count;
1448 mutex_unlock(&dev->mode_config.mutex);
1453 * drm_mode_setcrtc - set CRTC configuration
1454 * @inode: inode from the ioctl
1455 * @filp: file * from the ioctl
1456 * @cmd: cmd from ioctl
1457 * @arg: arg from ioctl
1462 * Build a new CRTC configuration based on user request.
1464 * Called by the user via ioctl.
1467 * Zero on success, errno on failure.
1469 int drm_mode_setcrtc(struct drm_device *dev,
1470 void *data, struct drm_file *file_priv)
1472 struct drm_mode_crtc *crtc_req = data;
1473 struct drm_crtc *crtc;
1474 struct drm_output **output_set = NULL, *output;
1475 struct drm_framebuffer *fb = NULL;
1476 struct drm_display_mode *mode = NULL;
1479 uint32_t __user *set_outputs_ptr;
1481 mutex_lock(&dev->mode_config.mutex);
1482 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1483 if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1484 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1489 if (crtc_req->mode_valid) {
1490 /* if we have a mode we need a framebuffer */
1491 if (crtc_req->fb_id) {
1492 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1493 if (!fb || (fb->id != crtc_req->fb_id)) {
1494 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1500 mode = drm_mode_create(dev);
1501 drm_crtc_convert_umode(mode, &crtc_req->mode);
1504 if (crtc_req->count_outputs == 0 && mode) {
1505 DRM_DEBUG("Count outputs is 0 but mode set\n");
1510 if (crtc_req->count_outputs > 0 && !mode && !fb) {
1511 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1516 if (crtc_req->count_outputs > 0) {
1518 output_set = kmalloc(crtc_req->count_outputs *
1519 sizeof(struct drm_output *), GFP_KERNEL);
1525 for (i = 0; i < crtc_req->count_outputs; i++) {
1526 set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1527 if (get_user(out_id, &set_outputs_ptr[i])) {
1532 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1533 if (!output || (out_id != output->id)) {
1534 DRM_DEBUG("Output id %d unknown\n", out_id);
1539 output_set[i] = output;
1543 ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1546 mutex_unlock(&dev->mode_config.mutex);
1551 * drm_mode_addfb - add an FB to the graphics configuration
1552 * @inode: inode from the ioctl
1553 * @filp: file * from the ioctl
1554 * @cmd: cmd from ioctl
1555 * @arg: arg from ioctl
1558 * Takes mode config lock.
1560 * Add a new FB to the specified CRTC, given a user request.
1562 * Called by the user via ioctl.
1565 * Zero on success, errno on failure.
1567 int drm_mode_addfb(struct drm_device *dev,
1568 void *data, struct drm_file *file_priv)
1570 struct drm_mode_fb_cmd *r = data;
1571 struct drm_mode_config *config = &dev->mode_config;
1572 struct drm_framebuffer *fb;
1573 struct drm_buffer_object *bo;
1574 struct drm_crtc *crtc;
1577 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1578 DRM_ERROR("mode new framebuffer width not within limits\n");
1581 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1582 DRM_ERROR("mode new framebuffer height not within limits\n");
1586 mutex_lock(&dev->mode_config.mutex);
1587 /* TODO check limits are okay */
1588 ret = drm_get_buffer_object(dev, &bo, r->handle);
1594 /* TODO check buffer is sufficently large */
1595 /* TODO setup destructor callback */
1597 fb = drm_framebuffer_create(dev);
1603 fb->width = r->width;
1604 fb->height = r->height;
1605 fb->pitch = r->pitch;
1606 fb->bits_per_pixel = r->bpp;
1607 fb->depth = r->depth;
1608 fb->offset = bo->offset;
1611 r->buffer_id = fb->id;
1613 list_add(&fb->filp_head, &file_priv->fbs);
1615 /* FIXME: bind the fb to the right crtc */
1616 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1618 dev->driver->fb_probe(dev, crtc);
1622 mutex_unlock(&dev->mode_config.mutex);
1627 * drm_mode_rmfb - remove an FB from the configuration
1628 * @inode: inode from the ioctl
1629 * @filp: file * from the ioctl
1630 * @cmd: cmd from ioctl
1631 * @arg: arg from ioctl
1634 * Takes mode config lock.
1636 * Remove the FB specified by the user.
1638 * Called by the user via ioctl.
1641 * Zero on success, errno on failure.
1643 int drm_mode_rmfb(struct drm_device *dev,
1644 void *data, struct drm_file *file_priv)
1646 struct drm_framebuffer *fb = 0;
1647 uint32_t *id = data;
1650 mutex_lock(&dev->mode_config.mutex);
1651 fb = idr_find(&dev->mode_config.crtc_idr, *id);
1652 /* TODO check that we realy get a framebuffer back. */
1653 if (!fb || (*id != fb->id)) {
1654 DRM_ERROR("mode invalid framebuffer id\n");
1659 /* TODO check if we own the buffer */
1660 /* TODO release all crtc connected to the framebuffer */
1661 /* bind the fb to the crtc for now */
1662 /* TODO unhock the destructor from the buffer object */
1664 if (fb->bo->type != drm_bo_type_kernel)
1665 drm_framebuffer_destroy(fb);
1667 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1670 mutex_unlock(&dev->mode_config.mutex);
1675 * drm_mode_getfb - get FB info
1676 * @inode: inode from the ioctl
1677 * @filp: file * from the ioctl
1678 * @cmd: cmd from ioctl
1679 * @arg: arg from ioctl
1684 * Lookup the FB given its ID and return info about it.
1686 * Called by the user via ioctl.
1689 * Zero on success, errno on failure.
1691 int drm_mode_getfb(struct drm_device *dev,
1692 void *data, struct drm_file *file_priv)
1694 struct drm_mode_fb_cmd *r = data;
1695 struct drm_framebuffer *fb;
1698 mutex_lock(&dev->mode_config.mutex);
1699 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1700 if (!fb || (r->buffer_id != fb->id)) {
1701 DRM_ERROR("invalid framebuffer id\n");
1706 r->height = fb->height;
1707 r->width = fb->width;
1708 r->depth = fb->depth;
1709 r->bpp = fb->bits_per_pixel;
1710 r->handle = fb->bo->base.hash.key;
1711 r->pitch = fb->pitch;
1714 mutex_unlock(&dev->mode_config.mutex);
1719 * drm_fb_release - remove and free the FBs on this file
1720 * @filp: file * from the ioctl
1723 * Takes mode config lock.
1725 * Destroy all the FBs associated with @filp.
1727 * Called by the user via ioctl.
1730 * Zero on success, errno on failure.
1732 void drm_fb_release(struct file *filp)
1734 struct drm_file *priv = filp->private_data;
1735 struct drm_device *dev = priv->head->dev;
1736 struct drm_framebuffer *fb, *tfb;
1738 mutex_lock(&dev->mode_config.mutex);
1739 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1740 list_del(&fb->filp_head);
1741 if (fb->bo->type != drm_bo_type_kernel)
1742 drm_framebuffer_destroy(fb);
1744 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1746 mutex_unlock(&dev->mode_config.mutex);
1753 static int drm_mode_attachmode(struct drm_device *dev,
1754 struct drm_output *output,
1755 struct drm_display_mode *mode)
1759 list_add_tail(&mode->head, &output->user_modes);
1763 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1764 struct drm_display_mode *mode)
1766 struct drm_output *output;
1768 struct drm_display_mode *dup_mode;
1770 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1771 if (output->crtc == crtc) {
1773 dup_mode = drm_mode_duplicate(dev, mode);
1776 ret = drm_mode_attachmode(dev, output, dup_mode);
1784 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1786 static int drm_mode_detachmode(struct drm_device *dev,
1787 struct drm_output *output,
1788 struct drm_display_mode *mode)
1792 struct drm_display_mode *match_mode, *t;
1794 list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1795 if (drm_mode_equal(match_mode, mode)) {
1796 list_del(&match_mode->head);
1797 drm_mode_destroy(dev, match_mode);
1809 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1811 struct drm_output *output;
1813 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1814 drm_mode_detachmode(dev, output, mode);
1818 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1821 * drm_fb_attachmode - Attach a user mode to an output
1822 * @inode: inode from the ioctl
1823 * @filp: file * from the ioctl
1824 * @cmd: cmd from ioctl
1825 * @arg: arg from ioctl
1827 * This attaches a user specified mode to an output.
1828 * Called by the user via ioctl.
1831 * Zero on success, errno on failure.
1833 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1834 void *data, struct drm_file *file_priv)
1836 struct drm_mode_mode_cmd *mode_cmd = data;
1837 struct drm_output *output;
1838 struct drm_display_mode *mode;
1839 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1842 mutex_lock(&dev->mode_config.mutex);
1844 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1845 if (!output || (output->id != mode_cmd->output_id)) {
1850 mode = drm_mode_create(dev);
1856 drm_crtc_convert_umode(mode, umode);
1858 ret = drm_mode_attachmode(dev, output, mode);
1860 mutex_unlock(&dev->mode_config.mutex);
1866 * drm_fb_detachmode - Detach a user specified mode from an output
1867 * @inode: inode from the ioctl
1868 * @filp: file * from the ioctl
1869 * @cmd: cmd from ioctl
1870 * @arg: arg from ioctl
1872 * Called by the user via ioctl.
1875 * Zero on success, errno on failure.
1877 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1878 void *data, struct drm_file *file_priv)
1880 struct drm_mode_mode_cmd *mode_cmd = data;
1881 struct drm_output *output;
1882 struct drm_display_mode mode;
1883 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1886 mutex_lock(&dev->mode_config.mutex);
1888 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1889 if (!output || (output->id != mode_cmd->output_id)) {
1894 drm_crtc_convert_umode(&mode, umode);
1895 ret = drm_mode_detachmode(dev, output, &mode);
1897 mutex_unlock(&dev->mode_config.mutex);
1901 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1902 const char *name, int num_values)
1904 struct drm_property *property = NULL;
1906 property = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
1911 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1912 if (!property->values)
1916 property->id = drm_idr_get(dev, property);
1917 property->flags = flags;
1918 property->num_values = num_values;
1919 INIT_LIST_HEAD(&property->enum_blob_list);
1922 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1924 list_add_tail(&property->head, &dev->mode_config.property_list);
1930 EXPORT_SYMBOL(drm_property_create);
1932 int drm_property_add_enum(struct drm_property *property, int index,
1933 uint64_t value, const char *name)
1935 struct drm_property_enum *prop_enum;
1937 if (!(property->flags & DRM_MODE_PROP_ENUM))
1940 if (!list_empty(&property->enum_blob_list)) {
1941 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1942 if (prop_enum->value == value) {
1943 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1944 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1950 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1954 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1955 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1956 prop_enum->value = value;
1958 property->values[index] = value;
1959 list_add_tail(&prop_enum->head, &property->enum_blob_list);
1962 EXPORT_SYMBOL(drm_property_add_enum);
1964 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1966 struct drm_property_enum *prop_enum, *pt;
1968 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1969 list_del(&prop_enum->head);
1973 if (property->num_values)
1974 kfree(property->values);
1975 drm_idr_put(dev, property->id);
1976 list_del(&property->head);
1979 EXPORT_SYMBOL(drm_property_destroy);
1981 int drm_output_attach_property(struct drm_output *output,
1982 struct drm_property *property, uint64_t init_val)
1986 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1987 if (output->property_ids[i] == 0) {
1988 output->property_ids[i] = property->id;
1989 output->property_values[i] = init_val;
1994 if (i == DRM_OUTPUT_MAX_PROPERTY)
1998 EXPORT_SYMBOL(drm_output_attach_property);
2000 int drm_output_property_set_value(struct drm_output *output,
2001 struct drm_property *property, uint64_t value)
2005 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2006 if (output->property_ids[i] == property->id) {
2007 output->property_values[i] = value;
2012 if (i == DRM_OUTPUT_MAX_PROPERTY)
2016 EXPORT_SYMBOL(drm_output_property_set_value);
2018 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2019 void *data, struct drm_file *file_priv)
2021 struct drm_mode_get_property *out_resp = data;
2022 struct drm_property *property;
2025 int value_count = 0;
2028 struct drm_property_enum *prop_enum;
2029 struct drm_mode_property_enum __user *enum_ptr;
2030 struct drm_property_blob *prop_blob;
2031 uint32_t *blob_id_ptr;
2032 uint64_t __user *values_ptr;
2033 uint32_t __user *blob_length_ptr;
2035 mutex_lock(&dev->mode_config.mutex);
2036 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2037 if (!property || (property->id != out_resp->prop_id)) {
2042 if (property->flags & DRM_MODE_PROP_ENUM) {
2043 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2045 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2046 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2050 value_count = property->num_values;
2052 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2053 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2054 out_resp->flags = property->flags;
2056 if ((out_resp->count_values >= value_count) && value_count) {
2057 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2058 for (i = 0; i < value_count; i++) {
2059 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2065 out_resp->count_values = value_count;
2067 if (property->flags & DRM_MODE_PROP_ENUM) {
2069 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2071 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2072 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2074 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2079 if (copy_to_user(&enum_ptr[copied].name,
2080 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2087 out_resp->count_enum_blobs = enum_count;
2090 if (property->flags & DRM_MODE_PROP_BLOB) {
2091 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2093 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2094 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2096 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2097 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2102 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2110 out_resp->count_enum_blobs = enum_count;
2113 mutex_unlock(&dev->mode_config.mutex);
2117 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2120 struct drm_property_blob *blob;
2122 if (!length || !data)
2125 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2129 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2130 blob->length = length;
2132 memcpy(blob->data, data, length);
2134 blob->id = drm_idr_get(dev, blob);
2136 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2140 static void drm_property_destroy_blob(struct drm_device *dev,
2141 struct drm_property_blob *blob)
2143 drm_idr_put(dev, blob->id);
2144 list_del(&blob->head);
2148 int drm_mode_getblob_ioctl(struct drm_device *dev,
2149 void *data, struct drm_file *file_priv)
2151 struct drm_mode_get_blob *out_resp = data;
2152 struct drm_property_blob *blob;
2156 mutex_lock(&dev->mode_config.mutex);
2158 blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2159 if (!blob || (blob->id != out_resp->blob_id)) {
2164 if (out_resp->length == blob->length) {
2165 blob_ptr = (void *)(unsigned long)out_resp->data;
2166 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2171 out_resp->length = blob->length;
2174 mutex_unlock(&dev->mode_config.mutex);
2178 int drm_mode_output_update_edid_property(struct drm_output *output, unsigned char *edid)
2180 struct drm_device *dev = output->dev;
2182 if (output->edid_blob_ptr)
2183 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2185 output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2187 ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2190 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2192 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2193 void *data, struct drm_file *file_priv)
2195 struct drm_mode_output_set_property *out_resp = data;
2196 struct drm_property *property;
2197 struct drm_output *output;
2201 mutex_lock(&dev->mode_config.mutex);
2202 output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2203 if (!output || (output->id != out_resp->output_id)) {
2207 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2208 if (output->property_ids[i] == out_resp->prop_id)
2212 if (i == DRM_OUTPUT_MAX_PROPERTY) {
2216 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2217 if (!property || (property->id != out_resp->prop_id)) {
2221 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2224 if (property->flags & DRM_MODE_PROP_RANGE) {
2225 if (out_resp->value < property->values[0])
2228 if (out_resp->value > property->values[1])
2232 for (i = 0; i < property->num_values; i++) {
2233 if (property->values[i] == out_resp->value) {
2243 if (output->funcs->set_property)
2244 ret = output->funcs->set_property(output, property, out_resp->value);
2247 mutex_unlock(&dev->mode_config.mutex);