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 { { ConnectorVGA, "VGA" },
52 { ConnectorDVII, "DVI-I" },
53 { ConnectorDVID, "DVI-D" },
54 { ConnectorDVIA, "DVI-A" },
55 { ConnectorComposite, "Composite" },
56 { ConnectorSVIDEO, "SVIDEO" },
57 { ConnectorLVDS, "LVDS" },
58 { ConnectorComponent, "Component" },
59 { Connector9PinDIN, "9-pin DIN" },
60 { ConnectorDisplayPort, "DisplayPort" },
61 { ConnectorHDMIA, "HDMI Type A" },
62 { ConnectorHDMIB, "HDMI Type B" },
64 static struct drm_prop_enum_list drm_output_enum_list[] =
65 { { DRM_MODE_OUTPUT_NONE, "None" },
66 { DRM_MODE_OUTPUT_DAC, "DAC" },
67 { DRM_MODE_OUTPUT_TMDS, "TMDS" },
68 { DRM_MODE_OUTPUT_LVDS, "LVDS" },
69 { DRM_MODE_OUTPUT_TVDAC, "TV" },
72 char *drm_get_output_name(struct drm_output *output)
76 snprintf(buf, 32, "%s-%d", drm_output_enum_list[output->output_type].name,
77 output->output_type_id);
82 * drm_idr_get - allocate a new identifier
84 * @ptr: object pointer, used to generate unique ID
87 * Caller must hold DRM mode_config lock.
89 * Create a unique identifier based on @ptr in @dev's identifier space. Used
90 * for tracking modes, CRTCs and outputs.
93 * New unique (relative to other objects in @dev) integer identifier for the
96 int drm_idr_get(struct drm_device *dev, void *ptr)
101 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
102 DRM_ERROR("Ran out memory getting a mode number\n");
106 ret = idr_get_new_above(&dev->mode_config.crtc_idr, ptr, 1, &new_id);
114 * drm_idr_put - free an identifer
119 * Caller must hold DRM mode_config lock.
121 * Free @id from @dev's unique identifier pool.
123 void drm_idr_put(struct drm_device *dev, int id)
125 idr_remove(&dev->mode_config.crtc_idr, id);
129 * drm_crtc_from_fb - find the CRTC structure associated with an fb
131 * @fb: framebuffer in question
134 * Caller must hold mode_config lock.
136 * Find CRTC in the mode_config structure that matches @fb.
139 * Pointer to the CRTC or NULL if it wasn't found.
141 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
142 struct drm_framebuffer *fb)
144 struct drm_crtc *crtc;
146 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
154 * drm_framebuffer_create - create a new framebuffer object
158 * Caller must hold mode config lock.
160 * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
163 * Pointer to new framebuffer or NULL on error.
165 struct drm_framebuffer *drm_framebuffer_create(struct drm_device *dev)
167 struct drm_framebuffer *fb;
169 fb = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
173 fb->id = drm_idr_get(dev, fb);
175 dev->mode_config.num_fb++;
176 list_add(&fb->head, &dev->mode_config.fb_list);
180 EXPORT_SYMBOL(drm_framebuffer_create);
183 * drm_framebuffer_destroy - remove a framebuffer object
184 * @fb: framebuffer to remove
187 * Caller must hold mode config lock.
189 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
190 * it, setting it to NULL.
192 void drm_framebuffer_destroy(struct drm_framebuffer *fb)
194 struct drm_device *dev = fb->dev;
195 struct drm_crtc *crtc;
197 /* remove from any CRTC */
198 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
203 drm_idr_put(dev, fb->id);
205 dev->mode_config.num_fb--;
209 EXPORT_SYMBOL(drm_framebuffer_destroy);
212 * drm_crtc_create - create a new CRTC object
214 * @funcs: callbacks for the new CRTC
217 * Caller must hold mode config lock.
219 * Creates a new CRTC object and adds it to @dev's mode_config structure.
222 * Pointer to new CRTC object or NULL on error.
224 struct drm_crtc *drm_crtc_create(struct drm_device *dev,
225 const struct drm_crtc_funcs *funcs)
227 struct drm_crtc *crtc;
229 crtc = kzalloc(sizeof(struct drm_crtc), GFP_KERNEL);
236 crtc->id = drm_idr_get(dev, crtc);
238 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
239 dev->mode_config.num_crtc++;
243 EXPORT_SYMBOL(drm_crtc_create);
246 * drm_crtc_destroy - remove a CRTC object
247 * @crtc: CRTC to remove
250 * Caller must hold mode config lock.
252 * Cleanup @crtc. Calls @crtc's cleanup function, then removes @crtc from
253 * its associated DRM device's mode_config. Frees it afterwards.
255 void drm_crtc_destroy(struct drm_crtc *crtc)
257 struct drm_device *dev = crtc->dev;
259 if (crtc->funcs->cleanup)
260 (*crtc->funcs->cleanup)(crtc);
262 drm_idr_put(dev, crtc->id);
263 list_del(&crtc->head);
264 dev->mode_config.num_crtc--;
267 EXPORT_SYMBOL(drm_crtc_destroy);
270 * drm_crtc_in_use - check if a given CRTC is in a mode_config
271 * @crtc: CRTC to check
274 * Caller must hold mode config lock.
276 * Walk @crtc's DRM device's mode_config and see if it's in use.
279 * True if @crtc is part of the mode_config, false otherwise.
281 bool drm_crtc_in_use(struct drm_crtc *crtc)
283 struct drm_output *output;
284 struct drm_device *dev = crtc->dev;
285 /* FIXME: Locking around list access? */
286 list_for_each_entry(output, &dev->mode_config.output_list, head)
287 if (output->crtc == crtc)
291 EXPORT_SYMBOL(drm_crtc_in_use);
294 * Detailed mode info for a standard 640x480@60Hz monitor
296 static struct drm_display_mode std_mode[] = {
297 { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
298 752, 800, 0, 480, 490, 492, 525, 0,
299 V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
303 * drm_crtc_probe_output_modes - get complete set of display modes
305 * @maxX: max width for modes
306 * @maxY: max height for modes
309 * Caller must hold mode config lock.
311 * Based on @dev's mode_config layout, scan all the outputs and try to detect
312 * modes on them. Modes will first be added to the output's probed_modes
313 * list, then culled (based on validity and the @maxX, @maxY parameters) and
314 * put into the normal modes list.
316 * Intended to be used either at bootup time or when major configuration
317 * changes have occurred.
319 * FIXME: take into account monitor limits
321 void drm_crtc_probe_single_output_modes(struct drm_output *output, int maxX, int maxY)
323 struct drm_device *dev = output->dev;
324 struct drm_display_mode *mode, *t;
326 //if (maxX == 0 || maxY == 0)
329 /* set all modes to the unverified state */
330 list_for_each_entry_safe(mode, t, &output->modes, head)
331 mode->status = MODE_UNVERIFIED;
333 output->status = (*output->funcs->detect)(output);
335 if (output->status == output_status_disconnected) {
336 DRM_DEBUG("%s is disconnected\n", drm_get_output_name(output));
337 /* TODO set EDID to NULL */
341 ret = (*output->funcs->get_modes)(output);
344 drm_mode_output_list_update(output);
348 drm_mode_validate_size(dev, &output->modes, maxX,
350 list_for_each_entry_safe(mode, t, &output->modes, head) {
351 if (mode->status == MODE_OK)
352 mode->status = (*output->funcs->mode_valid)(output,mode);
356 drm_mode_prune_invalid(dev, &output->modes, TRUE);
358 if (list_empty(&output->modes)) {
359 struct drm_display_mode *stdmode;
361 DRM_DEBUG("No valid modes on %s\n", drm_get_output_name(output));
363 /* Should we do this here ???
364 * When no valid EDID modes are available we end up
365 * here and bailed in the past, now we add a standard
366 * 640x480@60Hz mode and carry on.
368 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
369 drm_mode_probed_add(output, stdmode);
370 drm_mode_list_concat(&output->probed_modes,
373 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
374 drm_get_output_name(output));
377 drm_mode_sort(&output->modes);
379 DRM_DEBUG("Probed modes for %s\n", drm_get_output_name(output));
380 list_for_each_entry_safe(mode, t, &output->modes, head) {
381 mode->vrefresh = drm_mode_vrefresh(mode);
383 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
384 drm_mode_debug_printmodeline(dev, mode);
388 void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY)
390 struct drm_output *output;
392 list_for_each_entry(output, &dev->mode_config.output_list, head) {
393 drm_crtc_probe_single_output_modes(output, maxX, maxY);
396 EXPORT_SYMBOL(drm_crtc_probe_output_modes);
399 * drm_crtc_set_mode - set a mode
400 * @crtc: CRTC to program
406 * Caller must hold mode config lock.
408 * Try to set @mode on @crtc. Give @crtc and its associated outputs a chance
409 * to fixup or reject the mode prior to trying to set it.
412 * True if the mode was set successfully, or false otherwise.
414 bool drm_crtc_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
417 struct drm_device *dev = crtc->dev;
418 struct drm_display_mode *adjusted_mode, saved_mode;
419 int saved_x, saved_y;
420 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 /* XXX short-circuit changes to base location only */
447 /* Pass our mode to the outputs and the CRTC to give them a chance to
448 * adjust it according to limitations or output properties, and also
449 * a chance to reject the mode entirely.
451 list_for_each_entry(output, &dev->mode_config.output_list, head) {
453 if (output->crtc != crtc)
456 if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
461 if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
465 /* Prepare the outputs and CRTCs before setting the mode. */
466 list_for_each_entry(output, &dev->mode_config.output_list, head) {
468 if (output->crtc != crtc)
471 /* Disable the output as the first thing we do. */
472 output->funcs->prepare(output);
475 crtc->funcs->prepare(crtc);
477 /* Set up the DPLL and any output state that needs to adjust or depend
480 crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
482 list_for_each_entry(output, &dev->mode_config.output_list, head) {
484 if (output->crtc != crtc)
487 DRM_INFO("%s: set mode %s %x\n", drm_get_output_name(output), mode->name, mode->mode_id);
489 output->funcs->mode_set(output, mode, adjusted_mode);
492 /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
493 crtc->funcs->commit(crtc);
495 list_for_each_entry(output, &dev->mode_config.output_list, head) {
497 if (output->crtc != crtc)
500 output->funcs->commit(output);
502 #if 0 // TODO def RANDR_12_INTERFACE
503 if (output->randr_output)
504 RRPostPendingProperties (output->randr_output);
508 /* XXX free adjustedmode */
509 drm_mode_destroy(dev, adjusted_mode);
512 // if (scrn->pScreen)
513 // drm_crtc_set_screen_sub_pixel_order(dev);
519 crtc->mode = saved_mode;
523 crtc->funcs->unlock (crtc);
527 EXPORT_SYMBOL(drm_crtc_set_mode);
530 * drm_disable_unused_functions - disable unused objects
534 * Caller must hold mode config lock.
536 * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
537 * by calling its dpms function, which should power it off.
539 void drm_disable_unused_functions(struct drm_device *dev)
541 struct drm_output *output;
542 struct drm_crtc *crtc;
544 list_for_each_entry(output, &dev->mode_config.output_list, head) {
546 (*output->funcs->dpms)(output, DPMSModeOff);
549 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
551 crtc->funcs->dpms(crtc, DPMSModeOff);
554 EXPORT_SYMBOL(drm_disable_unused_functions);
557 * drm_mode_probed_add - add a mode to the specified output's probed mode list
558 * @output: output the new mode
562 * Caller must hold mode config lock.
564 * Add @mode to @output's mode list for later use.
566 void drm_mode_probed_add(struct drm_output *output,
567 struct drm_display_mode *mode)
569 list_add(&mode->head, &output->probed_modes);
571 EXPORT_SYMBOL(drm_mode_probed_add);
574 * drm_mode_remove - remove and free a mode
575 * @output: output list to modify
576 * @mode: mode to remove
579 * Caller must hold mode config lock.
581 * Remove @mode from @output's mode list, then free it.
583 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
585 list_del(&mode->head);
588 EXPORT_SYMBOL(drm_mode_remove);
591 * drm_output_create - create a new output
593 * @funcs: callbacks for this output
594 * @name: user visible name of the output
597 * Caller must hold @dev's mode_config lock.
599 * Creates a new drm_output structure and adds it to @dev's mode_config
603 * Pointer to the new output or NULL on error.
605 struct drm_output *drm_output_create(struct drm_device *dev,
606 const struct drm_output_funcs *funcs,
609 struct drm_output *output = NULL;
611 output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
616 output->funcs = funcs;
617 output->id = drm_idr_get(dev, output);
618 output->output_type = output_type;
619 output->output_type_id = 1; /* TODO */
620 output->subpixel_order = SubPixelUnknown;
621 INIT_LIST_HEAD(&output->user_modes);
622 INIT_LIST_HEAD(&output->probed_modes);
623 INIT_LIST_HEAD(&output->modes);
625 /* output_set_monitor(output)? */
626 /* check for output_ignored(output)? */
628 mutex_lock(&dev->mode_config.mutex);
629 list_add_tail(&output->head, &dev->mode_config.output_list);
630 dev->mode_config.num_output++;
632 drm_output_attach_property(output, dev->mode_config.edid_property, 0);
634 drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
636 mutex_unlock(&dev->mode_config.mutex);
641 EXPORT_SYMBOL(drm_output_create);
644 * drm_output_destroy - remove an output
645 * @output: output to remove
648 * Caller must hold @dev's mode_config lock.
650 * Call @output's cleanup function, then remove the output from the DRM
651 * mode_config after freeing @output's modes.
653 void drm_output_destroy(struct drm_output *output)
655 struct drm_device *dev = output->dev;
656 struct drm_display_mode *mode, *t;
658 if (*output->funcs->cleanup)
659 (*output->funcs->cleanup)(output);
661 list_for_each_entry_safe(mode, t, &output->probed_modes, head)
662 drm_mode_remove(output, mode);
664 list_for_each_entry_safe(mode, t, &output->modes, head)
665 drm_mode_remove(output, mode);
667 list_for_each_entry_safe(mode, t, &output->user_modes, head)
668 drm_mode_remove(output, mode);
670 mutex_lock(&dev->mode_config.mutex);
671 drm_idr_put(dev, output->id);
672 list_del(&output->head);
673 mutex_unlock(&dev->mode_config.mutex);
676 EXPORT_SYMBOL(drm_output_destroy);
680 * drm_mode_create - create a new display mode
686 * Create a new drm_display_mode, give it an ID, and return it.
689 * Pointer to new mode on success, NULL on error.
691 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
693 struct drm_display_mode *nmode;
695 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
699 nmode->mode_id = drm_idr_get(dev, nmode);
702 EXPORT_SYMBOL(drm_mode_create);
705 * drm_mode_destroy - remove a mode
707 * @mode: mode to remove
710 * Caller must hold mode config lock.
712 * Free @mode's unique identifier, then free it.
714 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
716 drm_idr_put(dev, mode->mode_id);
720 EXPORT_SYMBOL(drm_mode_destroy);
722 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
727 * Standard properties (apply to all outputs)
729 dev->mode_config.edid_property =
730 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
733 dev->mode_config.dpms_property =
734 drm_property_create(dev, DRM_MODE_PROP_ENUM, "DPMS", 4);
736 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
737 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
739 dev->mode_config.connector_type_property =
740 drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
741 "Connector Type", 10);
742 for (i = 0; i < ARRAY_SIZE(drm_conn_enum_list); i++)
743 drm_property_add_enum(dev->mode_config.connector_type_property, i, drm_conn_enum_list[i].type, drm_conn_enum_list[i].name);
745 dev->mode_config.connector_num_property =
746 drm_property_create(dev, DRM_MODE_PROP_RANGE | DRM_MODE_PROP_IMMUTABLE,
748 dev->mode_config.connector_num_property->values[0] = 0;
749 dev->mode_config.connector_num_property->values[1] = 20;
752 * TV specific properties
754 dev->mode_config.tv_left_margin_property =
755 drm_property_create(dev, DRM_MODE_PROP_RANGE |
756 DRM_MODE_PROP_IMMUTABLE,
758 dev->mode_config.tv_left_margin_property->values[0] = 0;
759 dev->mode_config.tv_left_margin_property->values[1] = 100;
761 dev->mode_config.tv_right_margin_property =
762 drm_property_create(dev, DRM_MODE_PROP_RANGE |
763 DRM_MODE_PROP_IMMUTABLE,
765 dev->mode_config.tv_right_margin_property->values[0] = 0;
766 dev->mode_config.tv_right_margin_property->values[1] = 100;
768 dev->mode_config.tv_top_margin_property =
769 drm_property_create(dev, DRM_MODE_PROP_RANGE |
770 DRM_MODE_PROP_IMMUTABLE,
772 dev->mode_config.tv_top_margin_property->values[0] = 0;
773 dev->mode_config.tv_top_margin_property->values[1] = 100;
775 dev->mode_config.tv_bottom_margin_property =
776 drm_property_create(dev, DRM_MODE_PROP_RANGE |
777 DRM_MODE_PROP_IMMUTABLE,
779 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
780 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
787 * drm_mode_config_init - initialize DRM mode_configuration structure
791 * None, should happen single threaded at init time.
793 * Initialize @dev's mode_config structure, used for tracking the graphics
794 * configuration of @dev.
796 void drm_mode_config_init(struct drm_device *dev)
798 mutex_init(&dev->mode_config.mutex);
799 INIT_LIST_HEAD(&dev->mode_config.fb_list);
800 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
801 INIT_LIST_HEAD(&dev->mode_config.output_list);
802 INIT_LIST_HEAD(&dev->mode_config.property_list);
803 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
804 idr_init(&dev->mode_config.crtc_idr);
806 drm_mode_create_standard_output_properties(dev);
808 /* Just to be sure */
809 dev->mode_config.num_fb = 0;
810 dev->mode_config.num_output = 0;
811 dev->mode_config.num_crtc = 0;
812 dev->mode_config.hotplug_counter = 0;
814 EXPORT_SYMBOL(drm_mode_config_init);
817 * drm_get_buffer_object - find the buffer object for a given handle
819 * @bo: pointer to caller's buffer_object pointer
820 * @handle: handle to lookup
823 * Must take @dev's struct_mutex to protect buffer object lookup.
825 * Given @handle, lookup the buffer object in @dev and put it in the caller's
829 * Zero on success, -EINVAL if the handle couldn't be found.
831 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
833 struct drm_user_object *uo;
834 struct drm_hash_item *hash;
839 mutex_lock(&dev->struct_mutex);
840 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
842 DRM_ERROR("Couldn't find handle.\n");
847 uo = drm_hash_entry(hash, struct drm_user_object, hash);
848 if (uo->type != drm_buffer_type) {
853 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
856 mutex_unlock(&dev->struct_mutex);
861 * drm_pick_crtcs - pick crtcs for output devices
865 * Caller must hold mode config lock.
867 static void drm_pick_crtcs (struct drm_device *dev)
870 struct drm_output *output, *output_equal;
871 struct drm_crtc *crtc;
872 struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
874 list_for_each_entry(output, &dev->mode_config.output_list, head) {
877 /* Don't hook up outputs that are disconnected ??
879 * This is debateable. Do we want fixed /dev/fbX or
880 * dynamic on hotplug (need mode code for that though) ?
882 * If we don't hook up outputs now, then we only create
883 * /dev/fbX for the output that's enabled, that's good as
884 * the users console will be on that output.
886 * If we do hook up outputs that are disconnected now, then
887 * the user may end up having to muck about with the fbcon
888 * map flags to assign his console to the enabled output. Ugh.
890 if (output->status != output_status_connected)
894 list_for_each_entry(des_mode, &output->modes, head) {
895 if (des_mode->type & DRM_MODE_TYPE_PREFERRED)
899 /* No preferred mode, let's just select the first available */
900 if (!des_mode || !(des_mode->type & DRM_MODE_TYPE_PREFERRED)) {
901 list_for_each_entry(des_mode, &output->modes, head) {
908 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
912 if ((output->possible_crtcs & (1 << c)) == 0)
915 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
916 if (output->id == output_equal->id)
919 /* Find out if crtc has been assigned before */
920 if (output_equal->crtc == crtc)
924 #if 1 /* continue for now */
930 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
932 if (output->id == output_equal->id)
935 list_for_each_entry(modes, &output->modes, head) {
936 list_for_each_entry(modes_equal, &output_equal->modes, head) {
937 if (drm_mode_equal (modes, modes_equal)) {
938 if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
939 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);
949 /* crtc has been assigned skip it */
953 /* Found a CRTC to attach to, do it ! */
955 output->crtc->desired_mode = des_mode;
956 output->initial_x = 0;
957 output->initial_y = 0;
958 DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
963 EXPORT_SYMBOL(drm_pick_crtcs);
966 * drm_initial_config - setup a sane initial output configuration
968 * @can_grow: this configuration is growable
971 * Called at init time, must take mode config lock.
973 * Scan the CRTCs and outputs and try to put together an initial setup.
974 * At the moment, this is a cloned configuration across all heads with
975 * a new framebuffer object as the backing store.
978 * Zero if everything went ok, nonzero otherwise.
980 bool drm_initial_config(struct drm_device *dev, bool can_grow)
982 struct drm_output *output;
983 struct drm_crtc *crtc;
986 mutex_lock(&dev->mode_config.mutex);
988 drm_crtc_probe_output_modes(dev, 2048, 2048);
992 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
994 /* can't setup the crtc if there's no assigned mode */
995 if (!crtc->desired_mode)
998 /* Now setup the fbdev for attached crtcs */
999 dev->driver->fb_probe(dev, crtc);
1002 /* This is a little screwy, as we've already walked the outputs
1003 * above, but it's a little bit of magic too. There's the potential
1004 * for things not to get setup above if an existing device gets
1005 * re-assigned thus confusing the hardware. By walking the outputs
1006 * this fixes up their crtc's.
1008 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1010 /* can't setup the output if there's no assigned mode */
1011 if (!output->crtc || !output->crtc->desired_mode)
1014 /* and needs an attached fb */
1015 if (output->crtc->fb)
1016 drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
1019 drm_disable_unused_functions(dev);
1021 mutex_unlock(&dev->mode_config.mutex);
1024 EXPORT_SYMBOL(drm_initial_config);
1027 * drm_mode_config_cleanup - free up DRM mode_config info
1031 * Caller must hold mode config lock.
1033 * Free up all the outputs and CRTCs associated with this DRM device, then
1034 * free up the framebuffers and associated buffer objects.
1036 * FIXME: cleanup any dangling user buffer objects too
1038 void drm_mode_config_cleanup(struct drm_device *dev)
1040 struct drm_output *output, *ot;
1041 struct drm_crtc *crtc, *ct;
1042 struct drm_framebuffer *fb, *fbt;
1043 struct drm_property *property, *pt;
1045 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1046 drm_output_destroy(output);
1049 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1050 drm_property_destroy(dev, property);
1053 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1054 /* there should only be bo of kernel type left */
1055 if (fb->bo->type != drm_bo_type_kernel)
1056 drm_framebuffer_destroy(fb);
1058 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1061 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1062 drm_crtc_destroy(crtc);
1066 EXPORT_SYMBOL(drm_mode_config_cleanup);
1069 * drm_crtc_set_config - set a new config from userspace
1070 * @crtc: CRTC to setup
1071 * @crtc_info: user provided configuration
1072 * @new_mode: new mode to set
1073 * @output_set: set of outputs for the new config
1074 * @fb: new framebuffer
1077 * Caller must hold mode config lock.
1079 * Setup a new configuration, provided by the user in @crtc_info, and enable
1085 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)
1087 struct drm_device *dev = crtc->dev;
1088 struct drm_crtc **save_crtcs, *new_crtc;
1089 bool save_enabled = crtc->enabled;
1090 bool changed = false;
1091 bool flip_or_move = false;
1092 struct drm_output *output;
1095 save_crtcs = kzalloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc *), GFP_KERNEL);
1099 /* We should be able to check here if the fb has the same properties
1100 * and then just flip_or_move it */
1104 if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1105 flip_or_move = true;
1107 if (new_mode && !drm_mode_equal(new_mode, &crtc->mode))
1110 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1111 save_crtcs[count++] = output->crtc;
1113 if (output->crtc == crtc)
1116 new_crtc = output->crtc;
1118 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1119 if (output_set[ro] == output)
1122 if (new_crtc != output->crtc) {
1124 output->crtc = new_crtc;
1128 /* mode_set_base is not a required function */
1129 if (flip_or_move && !crtc->funcs->mode_set_base)
1134 crtc->enabled = (new_mode != NULL);
1135 if (new_mode != NULL) {
1136 DRM_DEBUG("attempting to set mode from userspace\n");
1137 drm_mode_debug_printmodeline(dev, new_mode);
1138 if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1140 crtc->enabled = save_enabled;
1142 list_for_each_entry(output, &dev->mode_config.output_list, head)
1143 output->crtc = save_crtcs[count++];
1147 crtc->desired_x = crtc_info->x;
1148 crtc->desired_y = crtc_info->y;
1149 crtc->desired_mode = new_mode;
1151 drm_disable_unused_functions(dev);
1152 } else if (flip_or_move) {
1153 crtc->funcs->mode_set_base(crtc, crtc_info->x, crtc_info->y);
1161 * drm_hotplug_stage_two
1163 * @output hotpluged output
1166 * Caller must hold mode config lock, function might grap struct lock.
1168 * Stage two of a hotplug.
1171 * Zero on success, errno on failure.
1173 int drm_hotplug_stage_two(struct drm_device *dev, struct drm_output *output,
1178 /* We might want to do something more here */
1180 DRM_DEBUG("not connected\n");
1181 dev->mode_config.hotplug_counter++;
1185 if (output->crtc && output->crtc->desired_mode) {
1186 DRM_DEBUG("drm thinks that the output already has a config\n");
1190 drm_crtc_probe_output_modes(dev, 2048, 2048);
1193 drm_pick_crtcs(dev);
1195 if (!output->crtc || !output->crtc->desired_mode) {
1196 DRM_DEBUG("could not find a desired mode or crtc for output\n");
1200 /* We should realy check if there is a fb using this crtc */
1202 dev->driver->fb_probe(dev, output->crtc);
1204 dev->driver->fb_resize(dev, output->crtc);
1206 if (!drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0))
1207 DRM_ERROR("failed to set mode after hotplug\n");
1210 drm_disable_unused_functions(dev);
1212 dev->mode_config.hotplug_counter++;
1216 dev->mode_config.hotplug_counter++;
1219 EXPORT_SYMBOL(drm_hotplug_stage_two);
1221 int drm_mode_hotplug_ioctl(struct drm_device *dev,
1222 void *data, struct drm_file *file_priv)
1224 struct drm_mode_hotplug *arg = data;
1226 arg->counter = dev->mode_config.hotplug_counter;
1232 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1233 * @out: drm_mode_modeinfo struct to return to the user
1234 * @in: drm_display_mode to use
1239 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1242 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1244 out->clock = in->clock;
1245 out->hdisplay = in->hdisplay;
1246 out->hsync_start = in->hsync_start;
1247 out->hsync_end = in->hsync_end;
1248 out->htotal = in->htotal;
1249 out->hskew = in->hskew;
1250 out->vdisplay = in->vdisplay;
1251 out->vsync_start = in->vsync_start;
1252 out->vsync_end = in->vsync_end;
1253 out->vtotal = in->vtotal;
1254 out->vscan = in->vscan;
1255 out->vrefresh = in->vrefresh;
1256 out->flags = in->flags;
1257 out->type = in->type;
1258 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1259 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1263 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1264 * @out: drm_display_mode to return to the user
1265 * @in: drm_mode_modeinfo to use
1270 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1273 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1275 out->clock = in->clock;
1276 out->hdisplay = in->hdisplay;
1277 out->hsync_start = in->hsync_start;
1278 out->hsync_end = in->hsync_end;
1279 out->htotal = in->htotal;
1280 out->hskew = in->hskew;
1281 out->vdisplay = in->vdisplay;
1282 out->vsync_start = in->vsync_start;
1283 out->vsync_end = in->vsync_end;
1284 out->vtotal = in->vtotal;
1285 out->vscan = in->vscan;
1286 out->vrefresh = in->vrefresh;
1287 out->flags = in->flags;
1288 out->type = in->type;
1289 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1290 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1294 * drm_mode_getresources - get graphics configuration
1295 * @inode: inode from the ioctl
1296 * @filp: file * from the ioctl
1297 * @cmd: cmd from ioctl
1298 * @arg: arg from ioctl
1301 * Takes mode config lock.
1303 * Construct a set of configuration description structures and return
1304 * them to the user, including CRTC, output and framebuffer configuration.
1306 * Called by the user via ioctl.
1309 * Zero on success, errno on failure.
1311 int drm_mode_getresources(struct drm_device *dev,
1312 void *data, struct drm_file *file_priv)
1314 struct drm_mode_card_res *card_res = data;
1315 struct list_head *lh;
1316 struct drm_framebuffer *fb;
1317 struct drm_output *output;
1318 struct drm_crtc *crtc;
1320 int output_count = 0;
1324 uint32_t __user *fb_id;
1325 uint32_t __user *crtc_id;
1326 uint32_t __user *output_id;
1328 mutex_lock(&dev->mode_config.mutex);
1330 list_for_each(lh, &dev->mode_config.fb_list)
1333 list_for_each(lh, &dev->mode_config.crtc_list)
1336 list_for_each(lh, &dev->mode_config.output_list)
1339 card_res->max_height = dev->mode_config.max_height;
1340 card_res->min_height = dev->mode_config.min_height;
1341 card_res->max_width = dev->mode_config.max_width;
1342 card_res->min_width = dev->mode_config.min_width;
1344 /* handle this in 4 parts */
1346 if (card_res->count_fbs >= fb_count) {
1348 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1349 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1350 if (put_user(fb->id, fb_id + copied)) {
1357 card_res->count_fbs = fb_count;
1360 if (card_res->count_crtcs >= crtc_count) {
1362 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1363 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1364 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1365 if (put_user(crtc->id, crtc_id + copied)) {
1372 card_res->count_crtcs = crtc_count;
1376 if (card_res->count_outputs >= output_count) {
1378 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1379 list_for_each_entry(output, &dev->mode_config.output_list,
1381 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1382 if (put_user(output->id, output_id + copied)) {
1389 card_res->count_outputs = output_count;
1391 DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1392 card_res->count_outputs);
1395 mutex_unlock(&dev->mode_config.mutex);
1400 * drm_mode_getcrtc - get CRTC configuration
1401 * @inode: inode from the ioctl
1402 * @filp: file * from the ioctl
1403 * @cmd: cmd from ioctl
1404 * @arg: arg from ioctl
1409 * Construct a CRTC configuration structure to return to the user.
1411 * Called by the user via ioctl.
1414 * Zero on success, errno on failure.
1416 int drm_mode_getcrtc(struct drm_device *dev,
1417 void *data, struct drm_file *file_priv)
1419 struct drm_mode_crtc *crtc_resp = data;
1420 struct drm_crtc *crtc;
1421 struct drm_output *output;
1425 mutex_lock(&dev->mode_config.mutex);
1426 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1427 if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1432 crtc_resp->x = crtc->x;
1433 crtc_resp->y = crtc->y;
1436 crtc_resp->fb_id = crtc->fb->id;
1438 crtc_resp->fb_id = 0;
1440 crtc_resp->outputs = 0;
1441 if (crtc->enabled) {
1443 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1444 crtc_resp->mode_valid = 1;
1446 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1447 if (output->crtc == crtc)
1448 crtc_resp->outputs |= 1 << (ocount++);
1452 crtc_resp->mode_valid = 0;
1456 mutex_unlock(&dev->mode_config.mutex);
1461 * drm_mode_getoutput - get output configuration
1462 * @inode: inode from the ioctl
1463 * @filp: file * from the ioctl
1464 * @cmd: cmd from ioctl
1465 * @arg: arg from ioctl
1470 * Construct a output configuration structure to return to the user.
1472 * Called by the user via ioctl.
1475 * Zero on success, errno on failure.
1477 int drm_mode_getoutput(struct drm_device *dev,
1478 void *data, struct drm_file *file_priv)
1480 struct drm_mode_get_output *out_resp = data;
1481 struct drm_output *output;
1482 struct drm_display_mode *mode;
1484 int props_count = 0;
1488 struct drm_mode_modeinfo u_mode;
1489 struct drm_mode_modeinfo __user *mode_ptr;
1490 uint32_t __user *prop_ptr;
1491 uint64_t __user *prop_values;
1493 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1495 DRM_DEBUG("output id %d:\n", out_resp->output);
1497 mutex_lock(&dev->mode_config.mutex);
1498 output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1499 if (!output || (output->id != out_resp->output)) {
1504 list_for_each_entry(mode, &output->modes, head)
1507 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1508 if (output->property_ids[i] != 0) {
1513 if (out_resp->count_modes == 0) {
1514 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1517 out_resp->output_type = output->output_type;
1518 out_resp->output_type_id = output->output_type_id;
1519 out_resp->mm_width = output->mm_width;
1520 out_resp->mm_height = output->mm_height;
1521 out_resp->subpixel = output->subpixel_order;
1522 out_resp->connection = output->status;
1524 out_resp->crtc = output->crtc->id;
1528 out_resp->crtcs = output->possible_crtcs;
1529 out_resp->clones = output->possible_clones;
1531 if ((out_resp->count_modes >= mode_count) && mode_count) {
1533 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1534 list_for_each_entry(mode, &output->modes, head) {
1535 drm_crtc_convert_to_umode(&u_mode, mode);
1536 if (copy_to_user(mode_ptr + copied,
1537 &u_mode, sizeof(u_mode))) {
1545 out_resp->count_modes = mode_count;
1547 if ((out_resp->count_props >= props_count) && props_count) {
1549 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1550 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1551 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1552 if (output->property_ids[i] != 0) {
1553 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1558 if (put_user(output->property_values[i], prop_values + copied)) {
1566 out_resp->count_props = props_count;
1569 mutex_unlock(&dev->mode_config.mutex);
1574 * drm_mode_setcrtc - set CRTC configuration
1575 * @inode: inode from the ioctl
1576 * @filp: file * from the ioctl
1577 * @cmd: cmd from ioctl
1578 * @arg: arg from ioctl
1583 * Build a new CRTC configuration based on user request.
1585 * Called by the user via ioctl.
1588 * Zero on success, errno on failure.
1590 int drm_mode_setcrtc(struct drm_device *dev,
1591 void *data, struct drm_file *file_priv)
1593 struct drm_mode_crtc *crtc_req = data;
1594 struct drm_crtc *crtc;
1595 struct drm_output **output_set = NULL, *output;
1596 struct drm_framebuffer *fb = NULL;
1597 struct drm_display_mode *mode = NULL;
1600 uint32_t __user *set_outputs_ptr;
1602 mutex_lock(&dev->mode_config.mutex);
1603 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1604 if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1605 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1610 if (crtc_req->mode_valid) {
1611 /* if we have a mode we need a framebuffer */
1612 if (crtc_req->fb_id) {
1613 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1614 if (!fb || (fb->id != crtc_req->fb_id)) {
1615 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1621 mode = drm_mode_create(dev);
1622 drm_crtc_convert_umode(mode, &crtc_req->mode);
1623 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1626 if (crtc_req->count_outputs == 0 && mode) {
1627 DRM_DEBUG("Count outputs is 0 but mode set\n");
1632 if (crtc_req->count_outputs > 0 && !mode && !fb) {
1633 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1638 if (crtc_req->count_outputs > 0) {
1640 /* Maybe we should check that count_outputs is a sensible value. */
1641 output_set = kmalloc(crtc_req->count_outputs *
1642 sizeof(struct drm_output *), GFP_KERNEL);
1648 for (i = 0; i < crtc_req->count_outputs; i++) {
1649 set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1650 if (get_user(out_id, &set_outputs_ptr[i])) {
1655 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1656 if (!output || (out_id != output->id)) {
1657 DRM_DEBUG("Output id %d unknown\n", out_id);
1662 output_set[i] = output;
1666 /* What happens to output_set, leak? */
1667 ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1670 mutex_unlock(&dev->mode_config.mutex);
1674 int drm_mode_cursor_ioctl(struct drm_device *dev,
1675 void *data, struct drm_file *file_priv)
1677 struct drm_mode_cursor *req = data;
1678 struct drm_crtc *crtc;
1679 struct drm_buffer_object *bo = NULL; /* must be set */
1685 DRM_ERROR("no operation set\n");
1689 mutex_lock(&dev->mode_config.mutex);
1690 crtc = idr_find(&dev->mode_config.crtc_idr, req->crtc);
1691 if (!crtc || (crtc->id != req->crtc)) {
1692 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1697 if (req->flags & DRM_MODE_CURSOR_BO) {
1698 /* Turn of the cursor if handle is 0 */
1700 ret = drm_get_buffer_object(dev, &bo, req->handle);
1703 DRM_ERROR("invalid buffer id\n");
1708 if (crtc->funcs->cursor_set) {
1709 ret = crtc->funcs->cursor_set(crtc, bo, req->width, req->height);
1711 DRM_ERROR("crtc does not support cursor\n");
1717 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1718 if (crtc->funcs->cursor_move) {
1719 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1721 DRM_ERROR("crtc does not support cursor\n");
1727 mutex_unlock(&dev->mode_config.mutex);
1732 * drm_mode_addfb - add an FB to the graphics configuration
1733 * @inode: inode from the ioctl
1734 * @filp: file * from the ioctl
1735 * @cmd: cmd from ioctl
1736 * @arg: arg from ioctl
1739 * Takes mode config lock.
1741 * Add a new FB to the specified CRTC, given a user request.
1743 * Called by the user via ioctl.
1746 * Zero on success, errno on failure.
1748 int drm_mode_addfb(struct drm_device *dev,
1749 void *data, struct drm_file *file_priv)
1751 struct drm_mode_fb_cmd *r = data;
1752 struct drm_mode_config *config = &dev->mode_config;
1753 struct drm_framebuffer *fb;
1754 struct drm_buffer_object *bo;
1757 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1758 DRM_ERROR("mode new framebuffer width not within limits\n");
1761 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1762 DRM_ERROR("mode new framebuffer height not within limits\n");
1766 mutex_lock(&dev->mode_config.mutex);
1767 /* TODO check limits are okay */
1768 ret = drm_get_buffer_object(dev, &bo, r->handle);
1770 DRM_ERROR("BO handle not valid\n");
1775 /* TODO check buffer is sufficently large */
1776 /* TODO setup destructor callback */
1778 fb = drm_framebuffer_create(dev);
1780 DRM_ERROR("could not create framebuffer\n");
1785 fb->width = r->width;
1786 fb->height = r->height;
1787 fb->pitch = r->pitch;
1788 fb->bits_per_pixel = r->bpp;
1789 fb->depth = r->depth;
1792 r->buffer_id = fb->id;
1794 list_add(&fb->filp_head, &file_priv->fbs);
1797 mutex_unlock(&dev->mode_config.mutex);
1802 * drm_mode_rmfb - remove an FB from the configuration
1803 * @inode: inode from the ioctl
1804 * @filp: file * from the ioctl
1805 * @cmd: cmd from ioctl
1806 * @arg: arg from ioctl
1809 * Takes mode config lock.
1811 * Remove the FB specified by the user.
1813 * Called by the user via ioctl.
1816 * Zero on success, errno on failure.
1818 int drm_mode_rmfb(struct drm_device *dev,
1819 void *data, struct drm_file *file_priv)
1821 struct drm_framebuffer *fb = 0;
1822 struct drm_framebuffer *fbl = 0;
1823 uint32_t *id = data;
1827 mutex_lock(&dev->mode_config.mutex);
1828 fb = idr_find(&dev->mode_config.crtc_idr, *id);
1829 /* TODO check that we realy get a framebuffer back. */
1830 if (!fb || (*id != fb->id)) {
1831 DRM_ERROR("mode invalid framebuffer id\n");
1836 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1841 DRM_ERROR("tried to remove a fb that we didn't own\n");
1846 /* TODO release all crtc connected to the framebuffer */
1847 /* TODO unhock the destructor from the buffer object */
1849 if (fb->bo->type == drm_bo_type_kernel)
1850 DRM_ERROR("the bo type should not be of kernel type\n");
1852 list_del(&fb->filp_head);
1853 drm_framebuffer_destroy(fb);
1856 mutex_unlock(&dev->mode_config.mutex);
1861 * drm_mode_getfb - get FB info
1862 * @inode: inode from the ioctl
1863 * @filp: file * from the ioctl
1864 * @cmd: cmd from ioctl
1865 * @arg: arg from ioctl
1870 * Lookup the FB given its ID and return info about it.
1872 * Called by the user via ioctl.
1875 * Zero on success, errno on failure.
1877 int drm_mode_getfb(struct drm_device *dev,
1878 void *data, struct drm_file *file_priv)
1880 struct drm_mode_fb_cmd *r = data;
1881 struct drm_framebuffer *fb;
1884 mutex_lock(&dev->mode_config.mutex);
1885 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1886 if (!fb || (r->buffer_id != fb->id)) {
1887 DRM_ERROR("invalid framebuffer id\n");
1892 r->height = fb->height;
1893 r->width = fb->width;
1894 r->depth = fb->depth;
1895 r->bpp = fb->bits_per_pixel;
1896 r->handle = fb->bo->base.hash.key;
1897 r->pitch = fb->pitch;
1900 mutex_unlock(&dev->mode_config.mutex);
1905 * drm_fb_release - remove and free the FBs on this file
1906 * @filp: file * from the ioctl
1909 * Takes mode config lock.
1911 * Destroy all the FBs associated with @filp.
1913 * Called by the user via ioctl.
1916 * Zero on success, errno on failure.
1918 void drm_fb_release(struct file *filp)
1920 struct drm_file *priv = filp->private_data;
1921 struct drm_device *dev = priv->minor->dev;
1922 struct drm_framebuffer *fb, *tfb;
1924 mutex_lock(&dev->mode_config.mutex);
1925 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1926 list_del(&fb->filp_head);
1927 if (fb->bo->type == drm_bo_type_kernel)
1928 DRM_ERROR("the bo type should not be of kernel_type, the kernel will probably explode, why Dave\n");
1930 drm_framebuffer_destroy(fb);
1932 mutex_unlock(&dev->mode_config.mutex);
1939 static int drm_mode_attachmode(struct drm_device *dev,
1940 struct drm_output *output,
1941 struct drm_display_mode *mode)
1945 list_add_tail(&mode->head, &output->user_modes);
1949 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1950 struct drm_display_mode *mode)
1952 struct drm_output *output;
1954 struct drm_display_mode *dup_mode;
1956 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1957 if (output->crtc == crtc) {
1959 dup_mode = drm_mode_duplicate(dev, mode);
1962 ret = drm_mode_attachmode(dev, output, dup_mode);
1970 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1972 static int drm_mode_detachmode(struct drm_device *dev,
1973 struct drm_output *output,
1974 struct drm_display_mode *mode)
1978 struct drm_display_mode *match_mode, *t;
1980 list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1981 if (drm_mode_equal(match_mode, mode)) {
1982 list_del(&match_mode->head);
1983 drm_mode_destroy(dev, match_mode);
1995 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1997 struct drm_output *output;
1999 list_for_each_entry(output, &dev->mode_config.output_list, head) {
2000 drm_mode_detachmode(dev, output, mode);
2004 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2007 * drm_fb_attachmode - Attach a user mode to an output
2008 * @inode: inode from the ioctl
2009 * @filp: file * from the ioctl
2010 * @cmd: cmd from ioctl
2011 * @arg: arg from ioctl
2013 * This attaches a user specified mode to an output.
2014 * Called by the user via ioctl.
2017 * Zero on success, errno on failure.
2019 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2020 void *data, struct drm_file *file_priv)
2022 struct drm_mode_mode_cmd *mode_cmd = data;
2023 struct drm_output *output;
2024 struct drm_display_mode *mode;
2025 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2028 mutex_lock(&dev->mode_config.mutex);
2030 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2031 if (!output || (output->id != mode_cmd->output_id)) {
2036 mode = drm_mode_create(dev);
2042 drm_crtc_convert_umode(mode, umode);
2044 ret = drm_mode_attachmode(dev, output, mode);
2046 mutex_unlock(&dev->mode_config.mutex);
2052 * drm_fb_detachmode - Detach a user specified mode from an output
2053 * @inode: inode from the ioctl
2054 * @filp: file * from the ioctl
2055 * @cmd: cmd from ioctl
2056 * @arg: arg from ioctl
2058 * Called by the user via ioctl.
2061 * Zero on success, errno on failure.
2063 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2064 void *data, struct drm_file *file_priv)
2066 struct drm_mode_mode_cmd *mode_cmd = data;
2067 struct drm_output *output;
2068 struct drm_display_mode mode;
2069 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2072 mutex_lock(&dev->mode_config.mutex);
2074 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2075 if (!output || (output->id != mode_cmd->output_id)) {
2080 drm_crtc_convert_umode(&mode, umode);
2081 ret = drm_mode_detachmode(dev, output, &mode);
2083 mutex_unlock(&dev->mode_config.mutex);
2087 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2088 const char *name, int num_values)
2090 struct drm_property *property = NULL;
2092 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2097 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2098 if (!property->values)
2102 property->id = drm_idr_get(dev, property);
2103 property->flags = flags;
2104 property->num_values = num_values;
2105 INIT_LIST_HEAD(&property->enum_blob_list);
2108 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2110 list_add_tail(&property->head, &dev->mode_config.property_list);
2116 EXPORT_SYMBOL(drm_property_create);
2118 int drm_property_add_enum(struct drm_property *property, int index,
2119 uint64_t value, const char *name)
2121 struct drm_property_enum *prop_enum;
2123 if (!(property->flags & DRM_MODE_PROP_ENUM))
2126 if (!list_empty(&property->enum_blob_list)) {
2127 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2128 if (prop_enum->value == value) {
2129 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2130 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2136 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2140 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2141 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2142 prop_enum->value = value;
2144 property->values[index] = value;
2145 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2148 EXPORT_SYMBOL(drm_property_add_enum);
2150 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2152 struct drm_property_enum *prop_enum, *pt;
2154 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2155 list_del(&prop_enum->head);
2159 if (property->num_values)
2160 kfree(property->values);
2161 drm_idr_put(dev, property->id);
2162 list_del(&property->head);
2165 EXPORT_SYMBOL(drm_property_destroy);
2167 int drm_output_attach_property(struct drm_output *output,
2168 struct drm_property *property, uint64_t init_val)
2172 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2173 if (output->property_ids[i] == 0) {
2174 output->property_ids[i] = property->id;
2175 output->property_values[i] = init_val;
2180 if (i == DRM_OUTPUT_MAX_PROPERTY)
2184 EXPORT_SYMBOL(drm_output_attach_property);
2186 int drm_output_property_set_value(struct drm_output *output,
2187 struct drm_property *property, uint64_t value)
2191 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2192 if (output->property_ids[i] == property->id) {
2193 output->property_values[i] = value;
2198 if (i == DRM_OUTPUT_MAX_PROPERTY)
2202 EXPORT_SYMBOL(drm_output_property_set_value);
2204 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2205 void *data, struct drm_file *file_priv)
2207 struct drm_mode_get_property *out_resp = data;
2208 struct drm_property *property;
2211 int value_count = 0;
2214 struct drm_property_enum *prop_enum;
2215 struct drm_mode_property_enum __user *enum_ptr;
2216 struct drm_property_blob *prop_blob;
2217 uint32_t *blob_id_ptr;
2218 uint64_t __user *values_ptr;
2219 uint32_t __user *blob_length_ptr;
2221 mutex_lock(&dev->mode_config.mutex);
2222 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2223 if (!property || (property->id != out_resp->prop_id)) {
2228 if (property->flags & DRM_MODE_PROP_ENUM) {
2229 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2231 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2232 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2236 value_count = property->num_values;
2238 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2239 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2240 out_resp->flags = property->flags;
2242 if ((out_resp->count_values >= value_count) && value_count) {
2243 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2244 for (i = 0; i < value_count; i++) {
2245 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2251 out_resp->count_values = value_count;
2253 if (property->flags & DRM_MODE_PROP_ENUM) {
2255 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2257 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2258 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2260 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2265 if (copy_to_user(&enum_ptr[copied].name,
2266 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2273 out_resp->count_enum_blobs = enum_count;
2276 if (property->flags & DRM_MODE_PROP_BLOB) {
2277 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2279 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2280 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2282 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2283 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2288 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2296 out_resp->count_enum_blobs = enum_count;
2299 mutex_unlock(&dev->mode_config.mutex);
2303 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2306 struct drm_property_blob *blob;
2308 if (!length || !data)
2311 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2315 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2316 blob->length = length;
2318 memcpy(blob->data, data, length);
2320 blob->id = drm_idr_get(dev, blob);
2322 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2326 static void drm_property_destroy_blob(struct drm_device *dev,
2327 struct drm_property_blob *blob)
2329 drm_idr_put(dev, blob->id);
2330 list_del(&blob->head);
2334 int drm_mode_getblob_ioctl(struct drm_device *dev,
2335 void *data, struct drm_file *file_priv)
2337 struct drm_mode_get_blob *out_resp = data;
2338 struct drm_property_blob *blob;
2342 mutex_lock(&dev->mode_config.mutex);
2344 blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2345 if (!blob || (blob->id != out_resp->blob_id)) {
2350 if (out_resp->length == blob->length) {
2351 blob_ptr = (void *)(unsigned long)out_resp->data;
2352 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2357 out_resp->length = blob->length;
2360 mutex_unlock(&dev->mode_config.mutex);
2364 int drm_mode_output_update_edid_property(struct drm_output *output, struct edid *edid)
2366 struct drm_device *dev = output->dev;
2368 if (output->edid_blob_ptr)
2369 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2371 output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2373 ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2376 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2378 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2379 void *data, struct drm_file *file_priv)
2381 struct drm_mode_output_set_property *out_resp = data;
2382 struct drm_property *property;
2383 struct drm_output *output;
2387 mutex_lock(&dev->mode_config.mutex);
2388 output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2389 if (!output || (output->id != out_resp->output_id)) {
2393 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2394 if (output->property_ids[i] == out_resp->prop_id)
2398 if (i == DRM_OUTPUT_MAX_PROPERTY) {
2402 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2403 if (!property || (property->id != out_resp->prop_id)) {
2407 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2410 if (property->flags & DRM_MODE_PROP_RANGE) {
2411 if (out_resp->value < property->values[0])
2414 if (out_resp->value > property->values[1])
2418 for (i = 0; i < property->num_values; i++) {
2419 if (property->values[i] == out_resp->value) {
2429 if (output->funcs->set_property)
2430 ret = output->funcs->set_property(output, property, out_resp->value);
2433 mutex_unlock(&dev->mode_config.mutex);