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 {
44 static struct drm_prop_enum_list drm_dpms_enum_list[] =
45 { { DPMSModeOn, "On" },
46 { DPMSModeStandby, "Standby" },
47 { DPMSModeSuspend, "Suspend" },
48 { DPMSModeOff, "Off" }
50 static struct drm_prop_enum_list drm_conn_enum_list[] =
51 { { ConnectorUnknown, "Unknown" },
52 { ConnectorVGA, "VGA" },
53 { ConnectorDVII, "DVI-I" },
54 { ConnectorDVID, "DVI-D" },
55 { ConnectorDVIA, "DVI-A" },
56 { ConnectorComposite, "Composite" },
57 { ConnectorSVIDEO, "SVIDEO" },
58 { ConnectorLVDS, "LVDS" },
59 { ConnectorComponent, "Component" },
60 { Connector9PinDIN, "9-pin DIN" },
61 { ConnectorDisplayPort, "DisplayPort" },
62 { ConnectorHDMIA, "HDMI Type A" },
63 { ConnectorHDMIB, "HDMI Type B" },
65 static struct drm_prop_enum_list drm_output_enum_list[] =
66 { { DRM_MODE_OUTPUT_NONE, "None" },
67 { DRM_MODE_OUTPUT_DAC, "DAC" },
68 { DRM_MODE_OUTPUT_TMDS, "TMDS" },
69 { DRM_MODE_OUTPUT_LVDS, "LVDS" },
70 { DRM_MODE_OUTPUT_TVDAC, "TV" },
73 char *drm_get_output_name(struct drm_output *output)
77 snprintf(buf, 32, "%s-%d", drm_output_enum_list[output->output_type].name,
78 output->output_type_id);
83 * drm_idr_get - allocate a new identifier
85 * @ptr: object pointer, used to generate unique ID
88 * Caller must hold DRM mode_config lock.
90 * Create a unique identifier based on @ptr in @dev's identifier space. Used
91 * for tracking modes, CRTCs and outputs.
94 * New unique (relative to other objects in @dev) integer identifier for the
97 int drm_idr_get(struct drm_device *dev, void *ptr)
102 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
103 DRM_ERROR("Ran out memory getting a mode number\n");
107 ret = idr_get_new_above(&dev->mode_config.crtc_idr, ptr, 1, &new_id);
115 * drm_idr_put - free an identifer
120 * Caller must hold DRM mode_config lock.
122 * Free @id from @dev's unique identifier pool.
124 void drm_idr_put(struct drm_device *dev, int id)
126 idr_remove(&dev->mode_config.crtc_idr, id);
130 * drm_crtc_from_fb - find the CRTC structure associated with an fb
132 * @fb: framebuffer in question
135 * Caller must hold mode_config lock.
137 * Find CRTC in the mode_config structure that matches @fb.
140 * Pointer to the CRTC or NULL if it wasn't found.
142 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
143 struct drm_framebuffer *fb)
145 struct drm_crtc *crtc;
147 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
155 * drm_framebuffer_create - create a new framebuffer object
159 * Caller must hold mode config lock.
161 * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
164 * Pointer to new framebuffer or NULL on error.
166 struct drm_framebuffer *drm_framebuffer_create(struct drm_device *dev)
168 struct drm_framebuffer *fb;
170 fb = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
174 fb->id = drm_idr_get(dev, fb);
176 dev->mode_config.num_fb++;
177 list_add(&fb->head, &dev->mode_config.fb_list);
181 EXPORT_SYMBOL(drm_framebuffer_create);
184 * drm_framebuffer_destroy - remove a framebuffer object
185 * @fb: framebuffer to remove
188 * Caller must hold mode config lock.
190 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
191 * it, setting it to NULL.
193 void drm_framebuffer_destroy(struct drm_framebuffer *fb)
195 struct drm_device *dev = fb->dev;
196 struct drm_crtc *crtc;
198 /* remove from any CRTC */
199 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
204 drm_idr_put(dev, fb->id);
206 dev->mode_config.num_fb--;
210 EXPORT_SYMBOL(drm_framebuffer_destroy);
213 * drm_crtc_create - create a new CRTC object
215 * @funcs: callbacks for the new CRTC
218 * Caller must hold mode config lock.
220 * Creates a new CRTC object and adds it to @dev's mode_config structure.
223 * Pointer to new CRTC object or NULL on error.
225 struct drm_crtc *drm_crtc_create(struct drm_device *dev,
226 const struct drm_crtc_funcs *funcs)
228 struct drm_crtc *crtc;
230 crtc = kzalloc(sizeof(struct drm_crtc), GFP_KERNEL);
237 crtc->id = drm_idr_get(dev, crtc);
239 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
240 dev->mode_config.num_crtc++;
244 EXPORT_SYMBOL(drm_crtc_create);
247 * drm_crtc_destroy - remove a CRTC object
248 * @crtc: CRTC to remove
251 * Caller must hold mode config lock.
253 * Cleanup @crtc. Calls @crtc's cleanup function, then removes @crtc from
254 * its associated DRM device's mode_config. Frees it afterwards.
256 void drm_crtc_destroy(struct drm_crtc *crtc)
258 struct drm_device *dev = crtc->dev;
260 if (crtc->funcs->cleanup)
261 (*crtc->funcs->cleanup)(crtc);
263 drm_idr_put(dev, crtc->id);
264 list_del(&crtc->head);
265 dev->mode_config.num_crtc--;
268 EXPORT_SYMBOL(drm_crtc_destroy);
271 * drm_crtc_in_use - check if a given CRTC is in a mode_config
272 * @crtc: CRTC to check
275 * Caller must hold mode config lock.
277 * Walk @crtc's DRM device's mode_config and see if it's in use.
280 * True if @crtc is part of the mode_config, false otherwise.
282 bool drm_crtc_in_use(struct drm_crtc *crtc)
284 struct drm_output *output;
285 struct drm_device *dev = crtc->dev;
286 /* FIXME: Locking around list access? */
287 list_for_each_entry(output, &dev->mode_config.output_list, head)
288 if (output->crtc == crtc)
292 EXPORT_SYMBOL(drm_crtc_in_use);
295 * Detailed mode info for a standard 640x480@60Hz monitor
297 static struct drm_display_mode std_mode[] = {
298 { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
299 752, 800, 0, 480, 490, 492, 525, 0,
300 V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
304 * drm_crtc_probe_output_modes - get complete set of display modes
306 * @maxX: max width for modes
307 * @maxY: max height for modes
310 * Caller must hold mode config lock.
312 * Based on @dev's mode_config layout, scan all the outputs and try to detect
313 * modes on them. Modes will first be added to the output's probed_modes
314 * list, then culled (based on validity and the @maxX, @maxY parameters) and
315 * put into the normal modes list.
317 * Intended to be used either at bootup time or when major configuration
318 * changes have occurred.
320 * FIXME: take into account monitor limits
322 void drm_crtc_probe_single_output_modes(struct drm_output *output, int maxX, int maxY)
324 struct drm_device *dev = output->dev;
325 struct drm_display_mode *mode, *t;
327 //if (maxX == 0 || maxY == 0)
330 /* set all modes to the unverified state */
331 list_for_each_entry_safe(mode, t, &output->modes, head)
332 mode->status = MODE_UNVERIFIED;
334 output->status = (*output->funcs->detect)(output);
336 if (output->status == output_status_disconnected) {
337 DRM_DEBUG("%s is disconnected\n", drm_get_output_name(output));
338 /* TODO set EDID to NULL */
342 ret = (*output->funcs->get_modes)(output);
345 drm_mode_output_list_update(output);
349 drm_mode_validate_size(dev, &output->modes, maxX,
351 list_for_each_entry_safe(mode, t, &output->modes, head) {
352 if (mode->status == MODE_OK)
353 mode->status = (*output->funcs->mode_valid)(output,mode);
357 drm_mode_prune_invalid(dev, &output->modes, TRUE);
359 if (list_empty(&output->modes)) {
360 struct drm_display_mode *stdmode;
362 DRM_DEBUG("No valid modes on %s\n", drm_get_output_name(output));
364 /* Should we do this here ???
365 * When no valid EDID modes are available we end up
366 * here and bailed in the past, now we add a standard
367 * 640x480@60Hz mode and carry on.
369 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
370 drm_mode_probed_add(output, stdmode);
371 drm_mode_list_concat(&output->probed_modes,
374 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
375 drm_get_output_name(output));
378 drm_mode_sort(&output->modes);
380 DRM_DEBUG("Probed modes for %s\n", drm_get_output_name(output));
381 list_for_each_entry_safe(mode, t, &output->modes, head) {
382 mode->vrefresh = drm_mode_vrefresh(mode);
384 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
385 drm_mode_debug_printmodeline(dev, mode);
389 void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY)
391 struct drm_output *output;
393 list_for_each_entry(output, &dev->mode_config.output_list, head) {
394 drm_crtc_probe_single_output_modes(output, maxX, maxY);
397 EXPORT_SYMBOL(drm_crtc_probe_output_modes);
400 * drm_crtc_set_mode - set a mode
401 * @crtc: CRTC to program
407 * Caller must hold mode config lock.
409 * Try to set @mode on @crtc. Give @crtc and its associated outputs a chance
410 * to fixup or reject the mode prior to trying to set it.
413 * True if the mode was set successfully, or false otherwise.
415 bool drm_crtc_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
418 struct drm_device *dev = crtc->dev;
419 struct drm_display_mode *adjusted_mode, saved_mode;
420 int saved_x, saved_y;
421 bool didLock = false;
422 struct drm_output *output;
424 adjusted_mode = drm_mode_duplicate(dev, mode);
426 crtc->enabled = drm_crtc_in_use(crtc);
428 if (!crtc->enabled) {
432 didLock = crtc->funcs->lock(crtc);
434 saved_mode = crtc->mode;
438 /* Update crtc values up front so the driver can rely on them for mode
445 if (drm_mode_equal(&saved_mode, &crtc->mode)) {
446 if (saved_x != crtc->x || saved_y != crtc->y) {
447 crtc->funcs->mode_set_base(crtc, crtc->x, crtc->y);
452 /* Pass our mode to the outputs and the CRTC to give them a chance to
453 * adjust it according to limitations or output properties, and also
454 * a chance to reject the mode entirely.
456 list_for_each_entry(output, &dev->mode_config.output_list, head) {
458 if (output->crtc != crtc)
461 if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
466 if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
470 /* Prepare the outputs and CRTCs before setting the mode. */
471 list_for_each_entry(output, &dev->mode_config.output_list, head) {
473 if (output->crtc != crtc)
476 /* Disable the output as the first thing we do. */
477 output->funcs->prepare(output);
480 crtc->funcs->prepare(crtc);
482 /* Set up the DPLL and any output state that needs to adjust or depend
485 crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
487 list_for_each_entry(output, &dev->mode_config.output_list, head) {
489 if (output->crtc != crtc)
492 DRM_INFO("%s: set mode %s %x\n", drm_get_output_name(output), mode->name, mode->mode_id);
494 output->funcs->mode_set(output, mode, adjusted_mode);
497 /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
498 crtc->funcs->commit(crtc);
500 list_for_each_entry(output, &dev->mode_config.output_list, head) {
502 if (output->crtc != crtc)
505 output->funcs->commit(output);
507 #if 0 // TODO def RANDR_12_INTERFACE
508 if (output->randr_output)
509 RRPostPendingProperties (output->randr_output);
513 /* XXX free adjustedmode */
514 drm_mode_destroy(dev, adjusted_mode);
516 // if (scrn->pScreen)
517 // drm_crtc_set_screen_sub_pixel_order(dev);
521 crtc->funcs->unlock (crtc);
525 EXPORT_SYMBOL(drm_crtc_set_mode);
528 * drm_disable_unused_functions - disable unused objects
532 * Caller must hold mode config lock.
534 * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
535 * by calling its dpms function, which should power it off.
537 void drm_disable_unused_functions(struct drm_device *dev)
539 struct drm_output *output;
540 struct drm_crtc *crtc;
542 list_for_each_entry(output, &dev->mode_config.output_list, head) {
544 (*output->funcs->dpms)(output, DPMSModeOff);
547 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
549 crtc->funcs->dpms(crtc, DPMSModeOff);
552 EXPORT_SYMBOL(drm_disable_unused_functions);
555 * drm_mode_probed_add - add a mode to the specified output's probed mode list
556 * @output: output the new mode
560 * Caller must hold mode config lock.
562 * Add @mode to @output's mode list for later use.
564 void drm_mode_probed_add(struct drm_output *output,
565 struct drm_display_mode *mode)
567 list_add(&mode->head, &output->probed_modes);
569 EXPORT_SYMBOL(drm_mode_probed_add);
572 * drm_mode_remove - remove and free a mode
573 * @output: output list to modify
574 * @mode: mode to remove
577 * Caller must hold mode config lock.
579 * Remove @mode from @output's mode list, then free it.
581 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
583 list_del(&mode->head);
586 EXPORT_SYMBOL(drm_mode_remove);
589 * drm_output_create - create a new output
591 * @funcs: callbacks for this output
592 * @name: user visible name of the output
595 * Caller must hold @dev's mode_config lock.
597 * Creates a new drm_output structure and adds it to @dev's mode_config
601 * Pointer to the new output or NULL on error.
603 struct drm_output *drm_output_create(struct drm_device *dev,
604 const struct drm_output_funcs *funcs,
607 struct drm_output *output = NULL;
609 output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
614 output->funcs = funcs;
615 output->id = drm_idr_get(dev, output);
616 output->output_type = output_type;
617 output->output_type_id = 1; /* TODO */
618 output->subpixel_order = SubPixelUnknown;
619 INIT_LIST_HEAD(&output->user_modes);
620 INIT_LIST_HEAD(&output->probed_modes);
621 INIT_LIST_HEAD(&output->modes);
623 /* output_set_monitor(output)? */
624 /* check for output_ignored(output)? */
626 mutex_lock(&dev->mode_config.mutex);
627 list_add_tail(&output->head, &dev->mode_config.output_list);
628 dev->mode_config.num_output++;
630 drm_output_attach_property(output, dev->mode_config.edid_property, 0);
632 drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
634 mutex_unlock(&dev->mode_config.mutex);
639 EXPORT_SYMBOL(drm_output_create);
642 * drm_output_destroy - remove an output
643 * @output: output to remove
646 * Caller must hold @dev's mode_config lock.
648 * Call @output's cleanup function, then remove the output from the DRM
649 * mode_config after freeing @output's modes.
651 void drm_output_destroy(struct drm_output *output)
653 struct drm_device *dev = output->dev;
654 struct drm_display_mode *mode, *t;
656 if (*output->funcs->cleanup)
657 (*output->funcs->cleanup)(output);
659 list_for_each_entry_safe(mode, t, &output->probed_modes, head)
660 drm_mode_remove(output, mode);
662 list_for_each_entry_safe(mode, t, &output->modes, head)
663 drm_mode_remove(output, mode);
665 list_for_each_entry_safe(mode, t, &output->user_modes, head)
666 drm_mode_remove(output, mode);
668 mutex_lock(&dev->mode_config.mutex);
669 drm_idr_put(dev, output->id);
670 list_del(&output->head);
671 mutex_unlock(&dev->mode_config.mutex);
674 EXPORT_SYMBOL(drm_output_destroy);
678 * drm_mode_create - create a new display mode
684 * Create a new drm_display_mode, give it an ID, and return it.
687 * Pointer to new mode on success, NULL on error.
689 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
691 struct drm_display_mode *nmode;
693 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
697 nmode->mode_id = drm_idr_get(dev, nmode);
700 EXPORT_SYMBOL(drm_mode_create);
703 * drm_mode_destroy - remove a mode
705 * @mode: mode to remove
708 * Caller must hold mode config lock.
710 * Free @mode's unique identifier, then free it.
712 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
714 drm_idr_put(dev, mode->mode_id);
718 EXPORT_SYMBOL(drm_mode_destroy);
720 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
725 * Standard properties (apply to all outputs)
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;
750 * TV specific properties
752 dev->mode_config.tv_left_margin_property =
753 drm_property_create(dev, DRM_MODE_PROP_RANGE |
754 DRM_MODE_PROP_IMMUTABLE,
756 dev->mode_config.tv_left_margin_property->values[0] = 0;
757 dev->mode_config.tv_left_margin_property->values[1] = 100;
759 dev->mode_config.tv_right_margin_property =
760 drm_property_create(dev, DRM_MODE_PROP_RANGE |
761 DRM_MODE_PROP_IMMUTABLE,
763 dev->mode_config.tv_right_margin_property->values[0] = 0;
764 dev->mode_config.tv_right_margin_property->values[1] = 100;
766 dev->mode_config.tv_top_margin_property =
767 drm_property_create(dev, DRM_MODE_PROP_RANGE |
768 DRM_MODE_PROP_IMMUTABLE,
770 dev->mode_config.tv_top_margin_property->values[0] = 0;
771 dev->mode_config.tv_top_margin_property->values[1] = 100;
773 dev->mode_config.tv_bottom_margin_property =
774 drm_property_create(dev, DRM_MODE_PROP_RANGE |
775 DRM_MODE_PROP_IMMUTABLE,
777 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
778 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
785 * drm_mode_config_init - initialize DRM mode_configuration structure
789 * None, should happen single threaded at init time.
791 * Initialize @dev's mode_config structure, used for tracking the graphics
792 * configuration of @dev.
794 void drm_mode_config_init(struct drm_device *dev)
796 mutex_init(&dev->mode_config.mutex);
797 INIT_LIST_HEAD(&dev->mode_config.fb_list);
798 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
799 INIT_LIST_HEAD(&dev->mode_config.output_list);
800 INIT_LIST_HEAD(&dev->mode_config.property_list);
801 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
802 idr_init(&dev->mode_config.crtc_idr);
804 drm_mode_create_standard_output_properties(dev);
806 /* Just to be sure */
807 dev->mode_config.num_fb = 0;
808 dev->mode_config.num_output = 0;
809 dev->mode_config.num_crtc = 0;
810 dev->mode_config.hotplug_counter = 0;
812 EXPORT_SYMBOL(drm_mode_config_init);
815 * drm_get_buffer_object - find the buffer object for a given handle
817 * @bo: pointer to caller's buffer_object pointer
818 * @handle: handle to lookup
821 * Must take @dev's struct_mutex to protect buffer object lookup.
823 * Given @handle, lookup the buffer object in @dev and put it in the caller's
827 * Zero on success, -EINVAL if the handle couldn't be found.
829 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
831 struct drm_user_object *uo;
832 struct drm_hash_item *hash;
837 mutex_lock(&dev->struct_mutex);
838 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
840 DRM_ERROR("Couldn't find handle.\n");
845 uo = drm_hash_entry(hash, struct drm_user_object, hash);
846 if (uo->type != drm_buffer_type) {
851 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
854 mutex_unlock(&dev->struct_mutex);
859 * drm_pick_crtcs - pick crtcs for output devices
863 * Caller must hold mode config lock.
865 static void drm_pick_crtcs (struct drm_device *dev)
868 struct drm_output *output, *output_equal;
869 struct drm_crtc *crtc;
870 struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
873 list_for_each_entry(output, &dev->mode_config.output_list, head) {
876 /* Don't hook up outputs that are disconnected ??
878 * This is debateable. Do we want fixed /dev/fbX or
879 * dynamic on hotplug (need mode code for that though) ?
881 * If we don't hook up outputs now, then we only create
882 * /dev/fbX for the output that's enabled, that's good as
883 * the users console will be on that output.
885 * If we do hook up outputs that are disconnected now, then
886 * the user may end up having to muck about with the fbcon
887 * map flags to assign his console to the enabled output. Ugh.
889 if (output->status != output_status_connected)
892 if (list_empty(&output->modes))
897 list_for_each_entry(des_mode, &output->modes, head) {
898 if (des_mode->type & DRM_MODE_TYPE_PREFERRED) {
904 /* No preferred mode, let's just select the first available */
907 list_for_each_entry(des_mode, &output->modes, head) {
913 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
917 if ((output->possible_crtcs & (1 << c)) == 0)
920 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
921 if (output->id == output_equal->id)
924 /* Find out if crtc has been assigned before */
925 if (output_equal->crtc == crtc)
929 #if 1 /* continue for now */
935 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
937 if (output->id == output_equal->id)
940 list_for_each_entry(modes, &output->modes, head) {
941 list_for_each_entry(modes_equal, &output_equal->modes, head) {
942 if (drm_mode_equal (modes, modes_equal)) {
943 if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
944 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);
954 /* crtc has been assigned skip it */
958 /* Found a CRTC to attach to, do it ! */
960 output->crtc->desired_mode = des_mode;
961 output->initial_x = 0;
962 output->initial_y = 0;
963 DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
968 EXPORT_SYMBOL(drm_pick_crtcs);
971 * drm_initial_config - setup a sane initial output configuration
973 * @can_grow: this configuration is growable
976 * Called at init time, must take mode config lock.
978 * Scan the CRTCs and outputs and try to put together an initial setup.
979 * At the moment, this is a cloned configuration across all heads with
980 * a new framebuffer object as the backing store.
983 * Zero if everything went ok, nonzero otherwise.
985 bool drm_initial_config(struct drm_device *dev, bool can_grow)
987 struct drm_output *output;
988 struct drm_crtc *crtc;
991 mutex_lock(&dev->mode_config.mutex);
993 drm_crtc_probe_output_modes(dev, 2048, 2048);
997 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
999 /* can't setup the crtc if there's no assigned mode */
1000 if (!crtc->desired_mode)
1003 /* Now setup the fbdev for attached crtcs */
1004 dev->driver->fb_probe(dev, crtc);
1007 /* This is a little screwy, as we've already walked the outputs
1008 * above, but it's a little bit of magic too. There's the potential
1009 * for things not to get setup above if an existing device gets
1010 * re-assigned thus confusing the hardware. By walking the outputs
1011 * this fixes up their crtc's.
1013 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1015 /* can't setup the output if there's no assigned mode */
1016 if (!output->crtc || !output->crtc->desired_mode)
1019 /* and needs an attached fb */
1020 if (output->crtc->fb)
1021 drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
1024 drm_disable_unused_functions(dev);
1026 mutex_unlock(&dev->mode_config.mutex);
1029 EXPORT_SYMBOL(drm_initial_config);
1032 * drm_mode_config_cleanup - free up DRM mode_config info
1036 * Caller must hold mode config lock.
1038 * Free up all the outputs and CRTCs associated with this DRM device, then
1039 * free up the framebuffers and associated buffer objects.
1041 * FIXME: cleanup any dangling user buffer objects too
1043 void drm_mode_config_cleanup(struct drm_device *dev)
1045 struct drm_output *output, *ot;
1046 struct drm_crtc *crtc, *ct;
1047 struct drm_framebuffer *fb, *fbt;
1048 struct drm_property *property, *pt;
1050 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1051 drm_output_destroy(output);
1054 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1055 drm_property_destroy(dev, property);
1058 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1059 /* there should only be bo of kernel type left */
1060 if (fb->bo->type != drm_bo_type_kernel)
1061 drm_framebuffer_destroy(fb);
1063 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1066 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1067 drm_crtc_destroy(crtc);
1071 EXPORT_SYMBOL(drm_mode_config_cleanup);
1074 * drm_crtc_set_config - set a new config from userspace
1075 * @crtc: CRTC to setup
1076 * @crtc_info: user provided configuration
1077 * @new_mode: new mode to set
1078 * @output_set: set of outputs for the new config
1079 * @fb: new framebuffer
1082 * Caller must hold mode config lock.
1084 * Setup a new configuration, provided by the user in @crtc_info, and enable
1090 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)
1092 struct drm_device *dev = crtc->dev;
1093 struct drm_crtc **save_crtcs, *new_crtc;
1094 bool save_enabled = crtc->enabled;
1095 bool changed = false;
1096 bool flip_or_move = false;
1097 struct drm_output *output;
1100 /* this is meant to be num_output not num_crtc */
1101 save_crtcs = kzalloc(dev->mode_config.num_output * sizeof(struct drm_crtc *), GFP_KERNEL);
1105 /* We should be able to check here if the fb has the same properties
1106 * and then just flip_or_move it */
1110 if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1111 flip_or_move = true;
1113 if (new_mode && !drm_mode_equal(new_mode, &crtc->mode))
1116 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1117 save_crtcs[count++] = output->crtc;
1119 if (output->crtc == crtc)
1122 new_crtc = output->crtc;
1124 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1125 if (output_set[ro] == output)
1128 if (new_crtc != output->crtc) {
1130 output->crtc = new_crtc;
1134 /* mode_set_base is not a required function */
1135 if (flip_or_move && !crtc->funcs->mode_set_base)
1140 crtc->enabled = (new_mode != NULL);
1141 if (new_mode != NULL) {
1142 DRM_DEBUG("attempting to set mode from userspace\n");
1143 drm_mode_debug_printmodeline(dev, new_mode);
1144 if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1146 crtc->enabled = save_enabled;
1148 list_for_each_entry(output, &dev->mode_config.output_list, head)
1149 output->crtc = save_crtcs[count++];
1153 crtc->desired_x = crtc_info->x;
1154 crtc->desired_y = crtc_info->y;
1155 crtc->desired_mode = new_mode;
1157 drm_disable_unused_functions(dev);
1158 } else if (flip_or_move) {
1159 crtc->funcs->mode_set_base(crtc, crtc_info->x, crtc_info->y);
1167 * drm_hotplug_stage_two
1169 * @output hotpluged output
1172 * Caller must hold mode config lock, function might grap struct lock.
1174 * Stage two of a hotplug.
1177 * Zero on success, errno on failure.
1179 int drm_hotplug_stage_two(struct drm_device *dev, struct drm_output *output,
1184 /* We might want to do something more here */
1186 DRM_DEBUG("not connected\n");
1187 dev->mode_config.hotplug_counter++;
1191 if (output->crtc && output->crtc->desired_mode) {
1192 DRM_DEBUG("drm thinks that the output already has a config\n");
1196 drm_crtc_probe_output_modes(dev, 2048, 2048);
1199 drm_pick_crtcs(dev);
1201 if (!output->crtc || !output->crtc->desired_mode) {
1202 DRM_DEBUG("could not find a desired mode or crtc for output\n");
1206 /* We should realy check if there is a fb using this crtc */
1208 dev->driver->fb_probe(dev, output->crtc);
1210 dev->driver->fb_resize(dev, output->crtc);
1212 if (!drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0))
1213 DRM_ERROR("failed to set mode after hotplug\n");
1216 drm_disable_unused_functions(dev);
1218 dev->mode_config.hotplug_counter++;
1222 dev->mode_config.hotplug_counter++;
1225 EXPORT_SYMBOL(drm_hotplug_stage_two);
1227 int drm_mode_hotplug_ioctl(struct drm_device *dev,
1228 void *data, struct drm_file *file_priv)
1230 struct drm_mode_hotplug *arg = data;
1232 arg->counter = dev->mode_config.hotplug_counter;
1238 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1239 * @out: drm_mode_modeinfo struct to return to the user
1240 * @in: drm_display_mode to use
1245 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1248 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1250 out->clock = in->clock;
1251 out->hdisplay = in->hdisplay;
1252 out->hsync_start = in->hsync_start;
1253 out->hsync_end = in->hsync_end;
1254 out->htotal = in->htotal;
1255 out->hskew = in->hskew;
1256 out->vdisplay = in->vdisplay;
1257 out->vsync_start = in->vsync_start;
1258 out->vsync_end = in->vsync_end;
1259 out->vtotal = in->vtotal;
1260 out->vscan = in->vscan;
1261 out->vrefresh = in->vrefresh;
1262 out->flags = in->flags;
1263 out->type = in->type;
1264 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1265 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1269 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1270 * @out: drm_display_mode to return to the user
1271 * @in: drm_mode_modeinfo to use
1276 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1279 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1281 out->clock = in->clock;
1282 out->hdisplay = in->hdisplay;
1283 out->hsync_start = in->hsync_start;
1284 out->hsync_end = in->hsync_end;
1285 out->htotal = in->htotal;
1286 out->hskew = in->hskew;
1287 out->vdisplay = in->vdisplay;
1288 out->vsync_start = in->vsync_start;
1289 out->vsync_end = in->vsync_end;
1290 out->vtotal = in->vtotal;
1291 out->vscan = in->vscan;
1292 out->vrefresh = in->vrefresh;
1293 out->flags = in->flags;
1294 out->type = in->type;
1295 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1296 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1300 * drm_mode_getresources - get graphics configuration
1301 * @inode: inode from the ioctl
1302 * @filp: file * from the ioctl
1303 * @cmd: cmd from ioctl
1304 * @arg: arg from ioctl
1307 * Takes mode config lock.
1309 * Construct a set of configuration description structures and return
1310 * them to the user, including CRTC, output and framebuffer configuration.
1312 * Called by the user via ioctl.
1315 * Zero on success, errno on failure.
1317 int drm_mode_getresources(struct drm_device *dev,
1318 void *data, struct drm_file *file_priv)
1320 struct drm_mode_card_res *card_res = data;
1321 struct list_head *lh;
1322 struct drm_framebuffer *fb;
1323 struct drm_output *output;
1324 struct drm_crtc *crtc;
1326 int output_count = 0;
1330 uint32_t __user *fb_id;
1331 uint32_t __user *crtc_id;
1332 uint32_t __user *output_id;
1334 mutex_lock(&dev->mode_config.mutex);
1336 list_for_each(lh, &dev->mode_config.fb_list)
1339 list_for_each(lh, &dev->mode_config.crtc_list)
1342 list_for_each(lh, &dev->mode_config.output_list)
1345 card_res->max_height = dev->mode_config.max_height;
1346 card_res->min_height = dev->mode_config.min_height;
1347 card_res->max_width = dev->mode_config.max_width;
1348 card_res->min_width = dev->mode_config.min_width;
1350 /* handle this in 4 parts */
1352 if (card_res->count_fbs >= fb_count) {
1354 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1355 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1356 if (put_user(fb->id, fb_id + copied)) {
1363 card_res->count_fbs = fb_count;
1366 if (card_res->count_crtcs >= crtc_count) {
1368 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1369 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1370 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1371 if (put_user(crtc->id, crtc_id + copied)) {
1378 card_res->count_crtcs = crtc_count;
1382 if (card_res->count_outputs >= output_count) {
1384 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1385 list_for_each_entry(output, &dev->mode_config.output_list,
1387 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1388 if (put_user(output->id, output_id + copied)) {
1395 card_res->count_outputs = output_count;
1397 DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1398 card_res->count_outputs);
1401 mutex_unlock(&dev->mode_config.mutex);
1406 * drm_mode_getcrtc - get CRTC configuration
1407 * @inode: inode from the ioctl
1408 * @filp: file * from the ioctl
1409 * @cmd: cmd from ioctl
1410 * @arg: arg from ioctl
1415 * Construct a CRTC configuration structure to return to the user.
1417 * Called by the user via ioctl.
1420 * Zero on success, errno on failure.
1422 int drm_mode_getcrtc(struct drm_device *dev,
1423 void *data, struct drm_file *file_priv)
1425 struct drm_mode_crtc *crtc_resp = data;
1426 struct drm_crtc *crtc;
1427 struct drm_output *output;
1431 mutex_lock(&dev->mode_config.mutex);
1432 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1433 if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1438 crtc_resp->x = crtc->x;
1439 crtc_resp->y = crtc->y;
1442 crtc_resp->fb_id = crtc->fb->id;
1444 crtc_resp->fb_id = 0;
1446 crtc_resp->outputs = 0;
1447 if (crtc->enabled) {
1449 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1450 crtc_resp->mode_valid = 1;
1452 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1453 if (output->crtc == crtc)
1454 crtc_resp->outputs |= 1 << (ocount++);
1458 crtc_resp->mode_valid = 0;
1462 mutex_unlock(&dev->mode_config.mutex);
1467 * drm_mode_getoutput - get output configuration
1468 * @inode: inode from the ioctl
1469 * @filp: file * from the ioctl
1470 * @cmd: cmd from ioctl
1471 * @arg: arg from ioctl
1476 * Construct a output configuration structure to return to the user.
1478 * Called by the user via ioctl.
1481 * Zero on success, errno on failure.
1483 int drm_mode_getoutput(struct drm_device *dev,
1484 void *data, struct drm_file *file_priv)
1486 struct drm_mode_get_output *out_resp = data;
1487 struct drm_output *output;
1488 struct drm_display_mode *mode;
1490 int props_count = 0;
1494 struct drm_mode_modeinfo u_mode;
1495 struct drm_mode_modeinfo __user *mode_ptr;
1496 uint32_t __user *prop_ptr;
1497 uint64_t __user *prop_values;
1499 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1501 DRM_DEBUG("output id %d:\n", out_resp->output);
1503 mutex_lock(&dev->mode_config.mutex);
1504 output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1505 if (!output || (output->id != out_resp->output)) {
1510 list_for_each_entry(mode, &output->modes, head)
1513 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1514 if (output->property_ids[i] != 0) {
1519 if (out_resp->count_modes == 0) {
1520 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1523 out_resp->output_type = output->output_type;
1524 out_resp->output_type_id = output->output_type_id;
1525 out_resp->mm_width = output->mm_width;
1526 out_resp->mm_height = output->mm_height;
1527 out_resp->subpixel = output->subpixel_order;
1528 out_resp->connection = output->status;
1530 out_resp->crtc = output->crtc->id;
1534 out_resp->crtcs = output->possible_crtcs;
1535 out_resp->clones = output->possible_clones;
1537 if ((out_resp->count_modes >= mode_count) && mode_count) {
1539 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1540 list_for_each_entry(mode, &output->modes, head) {
1541 drm_crtc_convert_to_umode(&u_mode, mode);
1542 if (copy_to_user(mode_ptr + copied,
1543 &u_mode, sizeof(u_mode))) {
1551 out_resp->count_modes = mode_count;
1553 if ((out_resp->count_props >= props_count) && props_count) {
1555 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1556 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1557 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1558 if (output->property_ids[i] != 0) {
1559 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1564 if (put_user(output->property_values[i], prop_values + copied)) {
1572 out_resp->count_props = props_count;
1575 mutex_unlock(&dev->mode_config.mutex);
1580 * drm_mode_setcrtc - set CRTC configuration
1581 * @inode: inode from the ioctl
1582 * @filp: file * from the ioctl
1583 * @cmd: cmd from ioctl
1584 * @arg: arg from ioctl
1589 * Build a new CRTC configuration based on user request.
1591 * Called by the user via ioctl.
1594 * Zero on success, errno on failure.
1596 int drm_mode_setcrtc(struct drm_device *dev,
1597 void *data, struct drm_file *file_priv)
1599 struct drm_mode_crtc *crtc_req = data;
1600 struct drm_crtc *crtc, *crtcfb;
1601 struct drm_output **output_set = NULL, *output;
1602 struct drm_framebuffer *fb = NULL;
1603 struct drm_display_mode *mode = NULL;
1604 uint32_t __user *set_outputs_ptr;
1608 mutex_lock(&dev->mode_config.mutex);
1609 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1610 if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1611 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1616 if (crtc_req->mode_valid) {
1617 /* If we have a mode we need a framebuffer. */
1618 /* If we pass -1, set the mode with the currently bound fb */
1619 if (crtc_req->fb_id == -1) {
1620 list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1621 if (crtcfb == crtc) {
1622 DRM_DEBUG("Using current fb for setmode\n");
1627 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1628 if (!fb || (fb->id != crtc_req->fb_id)) {
1629 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1635 mode = drm_mode_create(dev);
1636 drm_crtc_convert_umode(mode, &crtc_req->mode);
1637 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1640 if (crtc_req->count_outputs == 0 && mode) {
1641 DRM_DEBUG("Count outputs is 0 but mode set\n");
1646 if (crtc_req->count_outputs > 0 && !mode && !fb) {
1647 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1652 if (crtc_req->count_outputs > 0) {
1654 /* Maybe we should check that count_outputs is a sensible value. */
1655 output_set = kmalloc(crtc_req->count_outputs *
1656 sizeof(struct drm_output *), GFP_KERNEL);
1662 for (i = 0; i < crtc_req->count_outputs; i++) {
1663 set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1664 if (get_user(out_id, &set_outputs_ptr[i])) {
1669 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1670 if (!output || (out_id != output->id)) {
1671 DRM_DEBUG("Output id %d unknown\n", out_id);
1676 output_set[i] = output;
1680 ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1684 mutex_unlock(&dev->mode_config.mutex);
1688 int drm_mode_cursor_ioctl(struct drm_device *dev,
1689 void *data, struct drm_file *file_priv)
1691 struct drm_mode_cursor *req = data;
1692 struct drm_crtc *crtc;
1693 struct drm_buffer_object *bo = NULL; /* must be set */
1699 DRM_ERROR("no operation set\n");
1703 mutex_lock(&dev->mode_config.mutex);
1704 crtc = idr_find(&dev->mode_config.crtc_idr, req->crtc);
1705 if (!crtc || (crtc->id != req->crtc)) {
1706 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1711 if (req->flags & DRM_MODE_CURSOR_BO) {
1712 /* Turn of the cursor if handle is 0 */
1714 ret = drm_get_buffer_object(dev, &bo, req->handle);
1717 DRM_ERROR("invalid buffer id\n");
1722 if (crtc->funcs->cursor_set) {
1723 ret = crtc->funcs->cursor_set(crtc, bo, req->width, req->height);
1725 DRM_ERROR("crtc does not support cursor\n");
1731 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1732 if (crtc->funcs->cursor_move) {
1733 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1735 DRM_ERROR("crtc does not support cursor\n");
1741 mutex_unlock(&dev->mode_config.mutex);
1746 * drm_mode_addfb - add an FB to the graphics configuration
1747 * @inode: inode from the ioctl
1748 * @filp: file * from the ioctl
1749 * @cmd: cmd from ioctl
1750 * @arg: arg from ioctl
1753 * Takes mode config lock.
1755 * Add a new FB to the specified CRTC, given a user request.
1757 * Called by the user via ioctl.
1760 * Zero on success, errno on failure.
1762 int drm_mode_addfb(struct drm_device *dev,
1763 void *data, struct drm_file *file_priv)
1765 struct drm_mode_fb_cmd *r = data;
1766 struct drm_mode_config *config = &dev->mode_config;
1767 struct drm_framebuffer *fb;
1768 struct drm_buffer_object *bo;
1771 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1772 DRM_ERROR("mode new framebuffer width not within limits\n");
1775 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1776 DRM_ERROR("mode new framebuffer height not within limits\n");
1780 mutex_lock(&dev->mode_config.mutex);
1781 /* TODO check limits are okay */
1782 ret = drm_get_buffer_object(dev, &bo, r->handle);
1784 DRM_ERROR("BO handle not valid\n");
1789 /* TODO check buffer is sufficently large */
1790 /* TODO setup destructor callback */
1792 fb = drm_framebuffer_create(dev);
1794 DRM_ERROR("could not create framebuffer\n");
1799 fb->width = r->width;
1800 fb->height = r->height;
1801 fb->pitch = r->pitch;
1802 fb->bits_per_pixel = r->bpp;
1803 fb->depth = r->depth;
1806 r->buffer_id = fb->id;
1808 list_add(&fb->filp_head, &file_priv->fbs);
1811 mutex_unlock(&dev->mode_config.mutex);
1816 * drm_mode_rmfb - remove an FB from the configuration
1817 * @inode: inode from the ioctl
1818 * @filp: file * from the ioctl
1819 * @cmd: cmd from ioctl
1820 * @arg: arg from ioctl
1823 * Takes mode config lock.
1825 * Remove the FB specified by the user.
1827 * Called by the user via ioctl.
1830 * Zero on success, errno on failure.
1832 int drm_mode_rmfb(struct drm_device *dev,
1833 void *data, struct drm_file *file_priv)
1835 struct drm_framebuffer *fb = 0;
1836 struct drm_framebuffer *fbl = 0;
1837 uint32_t *id = data;
1841 mutex_lock(&dev->mode_config.mutex);
1842 fb = idr_find(&dev->mode_config.crtc_idr, *id);
1843 /* TODO check that we realy get a framebuffer back. */
1844 if (!fb || (*id != fb->id)) {
1845 DRM_ERROR("mode invalid framebuffer id\n");
1850 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1855 DRM_ERROR("tried to remove a fb that we didn't own\n");
1860 /* TODO release all crtc connected to the framebuffer */
1861 /* TODO unhock the destructor from the buffer object */
1863 if (fb->bo->type == drm_bo_type_kernel)
1864 DRM_ERROR("the bo type should not be of kernel type\n");
1866 list_del(&fb->filp_head);
1867 drm_framebuffer_destroy(fb);
1870 mutex_unlock(&dev->mode_config.mutex);
1875 * drm_mode_getfb - get FB info
1876 * @inode: inode from the ioctl
1877 * @filp: file * from the ioctl
1878 * @cmd: cmd from ioctl
1879 * @arg: arg from ioctl
1884 * Lookup the FB given its ID and return info about it.
1886 * Called by the user via ioctl.
1889 * Zero on success, errno on failure.
1891 int drm_mode_getfb(struct drm_device *dev,
1892 void *data, struct drm_file *file_priv)
1894 struct drm_mode_fb_cmd *r = data;
1895 struct drm_framebuffer *fb;
1898 mutex_lock(&dev->mode_config.mutex);
1899 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1900 if (!fb || (r->buffer_id != fb->id)) {
1901 DRM_ERROR("invalid framebuffer id\n");
1906 r->height = fb->height;
1907 r->width = fb->width;
1908 r->depth = fb->depth;
1909 r->bpp = fb->bits_per_pixel;
1910 r->handle = fb->bo->base.hash.key;
1911 r->pitch = fb->pitch;
1914 mutex_unlock(&dev->mode_config.mutex);
1919 * drm_fb_release - remove and free the FBs on this file
1920 * @filp: file * from the ioctl
1923 * Takes mode config lock.
1925 * Destroy all the FBs associated with @filp.
1927 * Called by the user via ioctl.
1930 * Zero on success, errno on failure.
1932 void drm_fb_release(struct file *filp)
1934 struct drm_file *priv = filp->private_data;
1935 struct drm_device *dev = priv->minor->dev;
1936 struct drm_framebuffer *fb, *tfb;
1938 mutex_lock(&dev->mode_config.mutex);
1939 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1940 list_del(&fb->filp_head);
1941 if (fb->bo->type == drm_bo_type_kernel)
1942 DRM_ERROR("the bo type should not be of kernel_type, the kernel will probably explode, why Dave\n");
1944 drm_framebuffer_destroy(fb);
1946 mutex_unlock(&dev->mode_config.mutex);
1953 static int drm_mode_attachmode(struct drm_device *dev,
1954 struct drm_output *output,
1955 struct drm_display_mode *mode)
1959 list_add_tail(&mode->head, &output->user_modes);
1963 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1964 struct drm_display_mode *mode)
1966 struct drm_output *output;
1968 struct drm_display_mode *dup_mode;
1970 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1971 if (output->crtc == crtc) {
1973 dup_mode = drm_mode_duplicate(dev, mode);
1976 ret = drm_mode_attachmode(dev, output, dup_mode);
1984 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1986 static int drm_mode_detachmode(struct drm_device *dev,
1987 struct drm_output *output,
1988 struct drm_display_mode *mode)
1992 struct drm_display_mode *match_mode, *t;
1994 list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1995 if (drm_mode_equal(match_mode, mode)) {
1996 list_del(&match_mode->head);
1997 drm_mode_destroy(dev, match_mode);
2009 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2011 struct drm_output *output;
2013 list_for_each_entry(output, &dev->mode_config.output_list, head) {
2014 drm_mode_detachmode(dev, output, mode);
2018 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2021 * drm_fb_attachmode - Attach a user mode to an output
2022 * @inode: inode from the ioctl
2023 * @filp: file * from the ioctl
2024 * @cmd: cmd from ioctl
2025 * @arg: arg from ioctl
2027 * This attaches a user specified mode to an output.
2028 * Called by the user via ioctl.
2031 * Zero on success, errno on failure.
2033 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2034 void *data, struct drm_file *file_priv)
2036 struct drm_mode_mode_cmd *mode_cmd = data;
2037 struct drm_output *output;
2038 struct drm_display_mode *mode;
2039 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2042 mutex_lock(&dev->mode_config.mutex);
2044 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2045 if (!output || (output->id != mode_cmd->output_id)) {
2050 mode = drm_mode_create(dev);
2056 drm_crtc_convert_umode(mode, umode);
2058 ret = drm_mode_attachmode(dev, output, mode);
2060 mutex_unlock(&dev->mode_config.mutex);
2066 * drm_fb_detachmode - Detach a user specified mode from an output
2067 * @inode: inode from the ioctl
2068 * @filp: file * from the ioctl
2069 * @cmd: cmd from ioctl
2070 * @arg: arg from ioctl
2072 * Called by the user via ioctl.
2075 * Zero on success, errno on failure.
2077 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2078 void *data, struct drm_file *file_priv)
2080 struct drm_mode_mode_cmd *mode_cmd = data;
2081 struct drm_output *output;
2082 struct drm_display_mode mode;
2083 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2086 mutex_lock(&dev->mode_config.mutex);
2088 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2089 if (!output || (output->id != mode_cmd->output_id)) {
2094 drm_crtc_convert_umode(&mode, umode);
2095 ret = drm_mode_detachmode(dev, output, &mode);
2097 mutex_unlock(&dev->mode_config.mutex);
2101 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2102 const char *name, int num_values)
2104 struct drm_property *property = NULL;
2106 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2111 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2112 if (!property->values)
2116 property->id = drm_idr_get(dev, property);
2117 property->flags = flags;
2118 property->num_values = num_values;
2119 INIT_LIST_HEAD(&property->enum_blob_list);
2122 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2124 list_add_tail(&property->head, &dev->mode_config.property_list);
2130 EXPORT_SYMBOL(drm_property_create);
2132 int drm_property_add_enum(struct drm_property *property, int index,
2133 uint64_t value, const char *name)
2135 struct drm_property_enum *prop_enum;
2137 if (!(property->flags & DRM_MODE_PROP_ENUM))
2140 if (!list_empty(&property->enum_blob_list)) {
2141 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2142 if (prop_enum->value == value) {
2143 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2144 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2150 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2154 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2155 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2156 prop_enum->value = value;
2158 property->values[index] = value;
2159 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2162 EXPORT_SYMBOL(drm_property_add_enum);
2164 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2166 struct drm_property_enum *prop_enum, *pt;
2168 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2169 list_del(&prop_enum->head);
2173 if (property->num_values)
2174 kfree(property->values);
2175 drm_idr_put(dev, property->id);
2176 list_del(&property->head);
2179 EXPORT_SYMBOL(drm_property_destroy);
2181 int drm_output_attach_property(struct drm_output *output,
2182 struct drm_property *property, uint64_t init_val)
2186 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2187 if (output->property_ids[i] == 0) {
2188 output->property_ids[i] = property->id;
2189 output->property_values[i] = init_val;
2194 if (i == DRM_OUTPUT_MAX_PROPERTY)
2198 EXPORT_SYMBOL(drm_output_attach_property);
2200 int drm_output_property_set_value(struct drm_output *output,
2201 struct drm_property *property, uint64_t value)
2205 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2206 if (output->property_ids[i] == property->id) {
2207 output->property_values[i] = value;
2212 if (i == DRM_OUTPUT_MAX_PROPERTY)
2216 EXPORT_SYMBOL(drm_output_property_set_value);
2218 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2219 void *data, struct drm_file *file_priv)
2221 struct drm_mode_get_property *out_resp = data;
2222 struct drm_property *property;
2225 int value_count = 0;
2228 struct drm_property_enum *prop_enum;
2229 struct drm_mode_property_enum __user *enum_ptr;
2230 struct drm_property_blob *prop_blob;
2231 uint32_t *blob_id_ptr;
2232 uint64_t __user *values_ptr;
2233 uint32_t __user *blob_length_ptr;
2235 mutex_lock(&dev->mode_config.mutex);
2236 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2237 if (!property || (property->id != out_resp->prop_id)) {
2242 if (property->flags & DRM_MODE_PROP_ENUM) {
2243 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2245 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2246 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2250 value_count = property->num_values;
2252 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2253 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2254 out_resp->flags = property->flags;
2256 if ((out_resp->count_values >= value_count) && value_count) {
2257 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2258 for (i = 0; i < value_count; i++) {
2259 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2265 out_resp->count_values = value_count;
2267 if (property->flags & DRM_MODE_PROP_ENUM) {
2269 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2271 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2272 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2274 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2279 if (copy_to_user(&enum_ptr[copied].name,
2280 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2287 out_resp->count_enum_blobs = enum_count;
2290 if (property->flags & DRM_MODE_PROP_BLOB) {
2291 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2293 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2294 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2296 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2297 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2302 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2310 out_resp->count_enum_blobs = enum_count;
2313 mutex_unlock(&dev->mode_config.mutex);
2317 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2320 struct drm_property_blob *blob;
2322 if (!length || !data)
2325 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2329 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2330 blob->length = length;
2332 memcpy(blob->data, data, length);
2334 blob->id = drm_idr_get(dev, blob);
2336 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2340 static void drm_property_destroy_blob(struct drm_device *dev,
2341 struct drm_property_blob *blob)
2343 drm_idr_put(dev, blob->id);
2344 list_del(&blob->head);
2348 int drm_mode_getblob_ioctl(struct drm_device *dev,
2349 void *data, struct drm_file *file_priv)
2351 struct drm_mode_get_blob *out_resp = data;
2352 struct drm_property_blob *blob;
2356 mutex_lock(&dev->mode_config.mutex);
2358 blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2359 if (!blob || (blob->id != out_resp->blob_id)) {
2364 if (out_resp->length == blob->length) {
2365 blob_ptr = (void *)(unsigned long)out_resp->data;
2366 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2371 out_resp->length = blob->length;
2374 mutex_unlock(&dev->mode_config.mutex);
2378 int drm_mode_output_update_edid_property(struct drm_output *output, struct edid *edid)
2380 struct drm_device *dev = output->dev;
2382 if (output->edid_blob_ptr)
2383 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2385 output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2387 ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2390 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2392 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2393 void *data, struct drm_file *file_priv)
2395 struct drm_mode_output_set_property *out_resp = data;
2396 struct drm_property *property;
2397 struct drm_output *output;
2401 mutex_lock(&dev->mode_config.mutex);
2402 output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2403 if (!output || (output->id != out_resp->output_id)) {
2407 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2408 if (output->property_ids[i] == out_resp->prop_id)
2412 if (i == DRM_OUTPUT_MAX_PROPERTY) {
2416 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2417 if (!property || (property->id != out_resp->prop_id)) {
2421 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2424 if (property->flags & DRM_MODE_PROP_RANGE) {
2425 if (out_resp->value < property->values[0])
2428 if (out_resp->value > property->values[1])
2432 for (i = 0; i < property->num_values; i++) {
2433 if (property->values[i] == out_resp->value) {
2443 if (output->funcs->set_property)
2444 ret = output->funcs->set_property(output, property, out_resp->value);
2447 mutex_unlock(&dev->mode_config.mutex);