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;
421 struct drm_output *output;
423 adjusted_mode = drm_mode_duplicate(dev, mode);
425 crtc->enabled = drm_crtc_in_use(crtc);
427 if (!crtc->enabled) {
431 didLock = crtc->funcs->lock(crtc);
433 saved_mode = crtc->mode;
437 /* Update crtc values up front so the driver can rely on them for mode
444 if (drm_mode_equal(&saved_mode, &crtc->mode)) {
445 if (saved_x != crtc->x || saved_y != crtc->y) {
446 crtc->funcs->mode_set_base(crtc, crtc->x, crtc->y);
451 /* Pass our mode to the outputs and the CRTC to give them a chance to
452 * adjust it according to limitations or output properties, and also
453 * a chance to reject the mode entirely.
455 list_for_each_entry(output, &dev->mode_config.output_list, head) {
457 if (output->crtc != crtc)
460 if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
465 if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
469 /* Prepare the outputs and CRTCs before setting the mode. */
470 list_for_each_entry(output, &dev->mode_config.output_list, head) {
472 if (output->crtc != crtc)
475 /* Disable the output as the first thing we do. */
476 output->funcs->prepare(output);
479 crtc->funcs->prepare(crtc);
481 /* Set up the DPLL and any output state that needs to adjust or depend
484 crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
486 list_for_each_entry(output, &dev->mode_config.output_list, head) {
488 if (output->crtc != crtc)
491 DRM_INFO("%s: set mode %s %x\n", drm_get_output_name(output), mode->name, mode->mode_id);
493 output->funcs->mode_set(output, mode, adjusted_mode);
496 /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
497 crtc->funcs->commit(crtc);
499 list_for_each_entry(output, &dev->mode_config.output_list, head) {
501 if (output->crtc != crtc)
504 output->funcs->commit(output);
506 #if 0 // TODO def RANDR_12_INTERFACE
507 if (output->randr_output)
508 RRPostPendingProperties (output->randr_output);
512 /* XXX free adjustedmode */
513 drm_mode_destroy(dev, adjusted_mode);
515 // if (scrn->pScreen)
516 // drm_crtc_set_screen_sub_pixel_order(dev);
520 crtc->funcs->unlock (crtc);
524 EXPORT_SYMBOL(drm_crtc_set_mode);
527 * drm_disable_unused_functions - disable unused objects
531 * Caller must hold mode config lock.
533 * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
534 * by calling its dpms function, which should power it off.
536 void drm_disable_unused_functions(struct drm_device *dev)
538 struct drm_output *output;
539 struct drm_crtc *crtc;
541 list_for_each_entry(output, &dev->mode_config.output_list, head) {
543 (*output->funcs->dpms)(output, DPMSModeOff);
546 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
548 crtc->funcs->dpms(crtc, DPMSModeOff);
551 EXPORT_SYMBOL(drm_disable_unused_functions);
554 * drm_mode_probed_add - add a mode to the specified output's probed mode list
555 * @output: output the new mode
559 * Caller must hold mode config lock.
561 * Add @mode to @output's mode list for later use.
563 void drm_mode_probed_add(struct drm_output *output,
564 struct drm_display_mode *mode)
566 list_add(&mode->head, &output->probed_modes);
568 EXPORT_SYMBOL(drm_mode_probed_add);
571 * drm_mode_remove - remove and free a mode
572 * @output: output list to modify
573 * @mode: mode to remove
576 * Caller must hold mode config lock.
578 * Remove @mode from @output's mode list, then free it.
580 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
582 list_del(&mode->head);
585 EXPORT_SYMBOL(drm_mode_remove);
588 * drm_output_create - create a new output
590 * @funcs: callbacks for this output
591 * @name: user visible name of the output
594 * Caller must hold @dev's mode_config lock.
596 * Creates a new drm_output structure and adds it to @dev's mode_config
600 * Pointer to the new output or NULL on error.
602 struct drm_output *drm_output_create(struct drm_device *dev,
603 const struct drm_output_funcs *funcs,
606 struct drm_output *output = NULL;
608 output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
613 output->funcs = funcs;
614 output->id = drm_idr_get(dev, output);
615 output->output_type = output_type;
616 output->output_type_id = 1; /* TODO */
617 output->subpixel_order = SubPixelUnknown;
618 INIT_LIST_HEAD(&output->user_modes);
619 INIT_LIST_HEAD(&output->probed_modes);
620 INIT_LIST_HEAD(&output->modes);
622 /* output_set_monitor(output)? */
623 /* check for output_ignored(output)? */
625 mutex_lock(&dev->mode_config.mutex);
626 list_add_tail(&output->head, &dev->mode_config.output_list);
627 dev->mode_config.num_output++;
629 drm_output_attach_property(output, dev->mode_config.edid_property, 0);
631 drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
633 mutex_unlock(&dev->mode_config.mutex);
638 EXPORT_SYMBOL(drm_output_create);
641 * drm_output_destroy - remove an output
642 * @output: output to remove
645 * Caller must hold @dev's mode_config lock.
647 * Call @output's cleanup function, then remove the output from the DRM
648 * mode_config after freeing @output's modes.
650 void drm_output_destroy(struct drm_output *output)
652 struct drm_device *dev = output->dev;
653 struct drm_display_mode *mode, *t;
655 if (*output->funcs->cleanup)
656 (*output->funcs->cleanup)(output);
658 list_for_each_entry_safe(mode, t, &output->probed_modes, head)
659 drm_mode_remove(output, mode);
661 list_for_each_entry_safe(mode, t, &output->modes, head)
662 drm_mode_remove(output, mode);
664 list_for_each_entry_safe(mode, t, &output->user_modes, head)
665 drm_mode_remove(output, mode);
667 mutex_lock(&dev->mode_config.mutex);
668 drm_idr_put(dev, output->id);
669 list_del(&output->head);
670 mutex_unlock(&dev->mode_config.mutex);
673 EXPORT_SYMBOL(drm_output_destroy);
677 * drm_mode_create - create a new display mode
683 * Create a new drm_display_mode, give it an ID, and return it.
686 * Pointer to new mode on success, NULL on error.
688 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
690 struct drm_display_mode *nmode;
692 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
696 nmode->mode_id = drm_idr_get(dev, nmode);
699 EXPORT_SYMBOL(drm_mode_create);
702 * drm_mode_destroy - remove a mode
704 * @mode: mode to remove
707 * Caller must hold mode config lock.
709 * Free @mode's unique identifier, then free it.
711 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
713 drm_idr_put(dev, mode->mode_id);
717 EXPORT_SYMBOL(drm_mode_destroy);
719 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
724 * Standard properties (apply to all outputs)
726 dev->mode_config.edid_property =
727 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
730 dev->mode_config.dpms_property =
731 drm_property_create(dev, DRM_MODE_PROP_ENUM, "DPMS", 4);
733 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
734 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
736 dev->mode_config.connector_type_property =
737 drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
738 "Connector Type", 10);
739 for (i = 0; i < ARRAY_SIZE(drm_conn_enum_list); i++)
740 drm_property_add_enum(dev->mode_config.connector_type_property, i, drm_conn_enum_list[i].type, drm_conn_enum_list[i].name);
742 dev->mode_config.connector_num_property =
743 drm_property_create(dev, DRM_MODE_PROP_RANGE | DRM_MODE_PROP_IMMUTABLE,
745 dev->mode_config.connector_num_property->values[0] = 0;
746 dev->mode_config.connector_num_property->values[1] = 20;
749 * TV specific properties
751 dev->mode_config.tv_left_margin_property =
752 drm_property_create(dev, DRM_MODE_PROP_RANGE |
753 DRM_MODE_PROP_IMMUTABLE,
755 dev->mode_config.tv_left_margin_property->values[0] = 0;
756 dev->mode_config.tv_left_margin_property->values[1] = 100;
758 dev->mode_config.tv_right_margin_property =
759 drm_property_create(dev, DRM_MODE_PROP_RANGE |
760 DRM_MODE_PROP_IMMUTABLE,
762 dev->mode_config.tv_right_margin_property->values[0] = 0;
763 dev->mode_config.tv_right_margin_property->values[1] = 100;
765 dev->mode_config.tv_top_margin_property =
766 drm_property_create(dev, DRM_MODE_PROP_RANGE |
767 DRM_MODE_PROP_IMMUTABLE,
769 dev->mode_config.tv_top_margin_property->values[0] = 0;
770 dev->mode_config.tv_top_margin_property->values[1] = 100;
772 dev->mode_config.tv_bottom_margin_property =
773 drm_property_create(dev, DRM_MODE_PROP_RANGE |
774 DRM_MODE_PROP_IMMUTABLE,
776 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
777 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
784 * drm_mode_config_init - initialize DRM mode_configuration structure
788 * None, should happen single threaded at init time.
790 * Initialize @dev's mode_config structure, used for tracking the graphics
791 * configuration of @dev.
793 void drm_mode_config_init(struct drm_device *dev)
795 mutex_init(&dev->mode_config.mutex);
796 INIT_LIST_HEAD(&dev->mode_config.fb_list);
797 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
798 INIT_LIST_HEAD(&dev->mode_config.output_list);
799 INIT_LIST_HEAD(&dev->mode_config.property_list);
800 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
801 idr_init(&dev->mode_config.crtc_idr);
803 drm_mode_create_standard_output_properties(dev);
805 /* Just to be sure */
806 dev->mode_config.num_fb = 0;
807 dev->mode_config.num_output = 0;
808 dev->mode_config.num_crtc = 0;
809 dev->mode_config.hotplug_counter = 0;
811 EXPORT_SYMBOL(drm_mode_config_init);
814 * drm_get_buffer_object - find the buffer object for a given handle
816 * @bo: pointer to caller's buffer_object pointer
817 * @handle: handle to lookup
820 * Must take @dev's struct_mutex to protect buffer object lookup.
822 * Given @handle, lookup the buffer object in @dev and put it in the caller's
826 * Zero on success, -EINVAL if the handle couldn't be found.
828 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
830 struct drm_user_object *uo;
831 struct drm_hash_item *hash;
836 mutex_lock(&dev->struct_mutex);
837 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
839 DRM_ERROR("Couldn't find handle.\n");
844 uo = drm_hash_entry(hash, struct drm_user_object, hash);
845 if (uo->type != drm_buffer_type) {
850 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
853 mutex_unlock(&dev->struct_mutex);
858 * drm_pick_crtcs - pick crtcs for output devices
862 * Caller must hold mode config lock.
864 static void drm_pick_crtcs (struct drm_device *dev)
867 struct drm_output *output, *output_equal;
868 struct drm_crtc *crtc;
869 struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
872 list_for_each_entry(output, &dev->mode_config.output_list, head) {
875 /* Don't hook up outputs that are disconnected ??
877 * This is debateable. Do we want fixed /dev/fbX or
878 * dynamic on hotplug (need mode code for that though) ?
880 * If we don't hook up outputs now, then we only create
881 * /dev/fbX for the output that's enabled, that's good as
882 * the users console will be on that output.
884 * If we do hook up outputs that are disconnected now, then
885 * the user may end up having to muck about with the fbcon
886 * map flags to assign his console to the enabled output. Ugh.
888 if (output->status != output_status_connected)
891 if (list_empty(&output->modes))
896 list_for_each_entry(des_mode, &output->modes, head) {
897 if (des_mode->type & DRM_MODE_TYPE_PREFERRED) {
903 /* No preferred mode, let's just select the first available */
906 list_for_each_entry(des_mode, &output->modes, head) {
912 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
916 if ((output->possible_crtcs & (1 << c)) == 0)
919 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
920 if (output->id == output_equal->id)
923 /* Find out if crtc has been assigned before */
924 if (output_equal->crtc == crtc)
928 #if 1 /* continue for now */
934 list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
936 if (output->id == output_equal->id)
939 list_for_each_entry(modes, &output->modes, head) {
940 list_for_each_entry(modes_equal, &output_equal->modes, head) {
941 if (drm_mode_equal (modes, modes_equal)) {
942 if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
943 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);
953 /* crtc has been assigned skip it */
957 /* Found a CRTC to attach to, do it ! */
959 output->crtc->desired_mode = des_mode;
960 output->initial_x = 0;
961 output->initial_y = 0;
962 DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
967 EXPORT_SYMBOL(drm_pick_crtcs);
970 * drm_initial_config - setup a sane initial output configuration
972 * @can_grow: this configuration is growable
975 * Called at init time, must take mode config lock.
977 * Scan the CRTCs and outputs and try to put together an initial setup.
978 * At the moment, this is a cloned configuration across all heads with
979 * a new framebuffer object as the backing store.
982 * Zero if everything went ok, nonzero otherwise.
984 bool drm_initial_config(struct drm_device *dev, bool can_grow)
986 struct drm_output *output;
987 struct drm_crtc *crtc;
990 mutex_lock(&dev->mode_config.mutex);
992 drm_crtc_probe_output_modes(dev, 2048, 2048);
996 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
998 /* can't setup the crtc if there's no assigned mode */
999 if (!crtc->desired_mode)
1002 /* Now setup the fbdev for attached crtcs */
1003 dev->driver->fb_probe(dev, crtc);
1006 /* This is a little screwy, as we've already walked the outputs
1007 * above, but it's a little bit of magic too. There's the potential
1008 * for things not to get setup above if an existing device gets
1009 * re-assigned thus confusing the hardware. By walking the outputs
1010 * this fixes up their crtc's.
1012 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1014 /* can't setup the output if there's no assigned mode */
1015 if (!output->crtc || !output->crtc->desired_mode)
1018 /* and needs an attached fb */
1019 if (output->crtc->fb)
1020 drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
1023 drm_disable_unused_functions(dev);
1025 mutex_unlock(&dev->mode_config.mutex);
1028 EXPORT_SYMBOL(drm_initial_config);
1031 * drm_mode_config_cleanup - free up DRM mode_config info
1035 * Caller must hold mode config lock.
1037 * Free up all the outputs and CRTCs associated with this DRM device, then
1038 * free up the framebuffers and associated buffer objects.
1040 * FIXME: cleanup any dangling user buffer objects too
1042 void drm_mode_config_cleanup(struct drm_device *dev)
1044 struct drm_output *output, *ot;
1045 struct drm_crtc *crtc, *ct;
1046 struct drm_framebuffer *fb, *fbt;
1047 struct drm_property *property, *pt;
1049 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
1050 drm_output_destroy(output);
1053 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
1054 drm_property_destroy(dev, property);
1057 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1058 /* there should only be bo of kernel type left */
1059 if (fb->bo->type != drm_bo_type_kernel)
1060 drm_framebuffer_destroy(fb);
1062 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1065 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1066 drm_crtc_destroy(crtc);
1070 EXPORT_SYMBOL(drm_mode_config_cleanup);
1073 * drm_crtc_set_config - set a new config from userspace
1074 * @crtc: CRTC to setup
1075 * @crtc_info: user provided configuration
1076 * @new_mode: new mode to set
1077 * @output_set: set of outputs for the new config
1078 * @fb: new framebuffer
1081 * Caller must hold mode config lock.
1083 * Setup a new configuration, provided by the user in @crtc_info, and enable
1089 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)
1091 struct drm_device *dev = crtc->dev;
1092 struct drm_crtc **save_crtcs, *new_crtc;
1093 bool save_enabled = crtc->enabled;
1094 bool changed = false;
1095 bool flip_or_move = false;
1096 struct drm_output *output;
1099 /* this is meant to be num_output not num_crtc */
1100 save_crtcs = kzalloc(dev->mode_config.num_output * sizeof(struct drm_crtc *), GFP_KERNEL);
1104 /* We should be able to check here if the fb has the same properties
1105 * and then just flip_or_move it */
1109 if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1110 flip_or_move = true;
1112 if (new_mode && !drm_mode_equal(new_mode, &crtc->mode))
1115 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1116 save_crtcs[count++] = output->crtc;
1118 if (output->crtc == crtc)
1121 new_crtc = output->crtc;
1123 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1124 if (output_set[ro] == output)
1127 if (new_crtc != output->crtc) {
1129 output->crtc = new_crtc;
1133 /* mode_set_base is not a required function */
1134 if (flip_or_move && !crtc->funcs->mode_set_base)
1139 crtc->enabled = (new_mode != NULL);
1140 if (new_mode != NULL) {
1141 DRM_DEBUG("attempting to set mode from userspace\n");
1142 drm_mode_debug_printmodeline(dev, new_mode);
1143 if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1145 crtc->enabled = save_enabled;
1147 list_for_each_entry(output, &dev->mode_config.output_list, head)
1148 output->crtc = save_crtcs[count++];
1152 crtc->desired_x = crtc_info->x;
1153 crtc->desired_y = crtc_info->y;
1154 crtc->desired_mode = new_mode;
1156 drm_disable_unused_functions(dev);
1157 } else if (flip_or_move) {
1158 crtc->funcs->mode_set_base(crtc, crtc_info->x, crtc_info->y);
1166 * drm_hotplug_stage_two
1168 * @output hotpluged output
1171 * Caller must hold mode config lock, function might grap struct lock.
1173 * Stage two of a hotplug.
1176 * Zero on success, errno on failure.
1178 int drm_hotplug_stage_two(struct drm_device *dev, struct drm_output *output,
1183 /* We might want to do something more here */
1185 DRM_DEBUG("not connected\n");
1186 dev->mode_config.hotplug_counter++;
1190 if (output->crtc && output->crtc->desired_mode) {
1191 DRM_DEBUG("drm thinks that the output already has a config\n");
1195 drm_crtc_probe_output_modes(dev, 2048, 2048);
1198 drm_pick_crtcs(dev);
1200 if (!output->crtc || !output->crtc->desired_mode) {
1201 DRM_DEBUG("could not find a desired mode or crtc for output\n");
1205 /* We should realy check if there is a fb using this crtc */
1207 dev->driver->fb_probe(dev, output->crtc);
1209 dev->driver->fb_resize(dev, output->crtc);
1211 if (!drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0))
1212 DRM_ERROR("failed to set mode after hotplug\n");
1215 drm_disable_unused_functions(dev);
1217 dev->mode_config.hotplug_counter++;
1221 dev->mode_config.hotplug_counter++;
1224 EXPORT_SYMBOL(drm_hotplug_stage_two);
1226 int drm_mode_hotplug_ioctl(struct drm_device *dev,
1227 void *data, struct drm_file *file_priv)
1229 struct drm_mode_hotplug *arg = data;
1231 arg->counter = dev->mode_config.hotplug_counter;
1237 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1238 * @out: drm_mode_modeinfo struct to return to the user
1239 * @in: drm_display_mode to use
1244 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1247 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1249 out->clock = in->clock;
1250 out->hdisplay = in->hdisplay;
1251 out->hsync_start = in->hsync_start;
1252 out->hsync_end = in->hsync_end;
1253 out->htotal = in->htotal;
1254 out->hskew = in->hskew;
1255 out->vdisplay = in->vdisplay;
1256 out->vsync_start = in->vsync_start;
1257 out->vsync_end = in->vsync_end;
1258 out->vtotal = in->vtotal;
1259 out->vscan = in->vscan;
1260 out->vrefresh = in->vrefresh;
1261 out->flags = in->flags;
1262 out->type = in->type;
1263 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1264 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1268 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1269 * @out: drm_display_mode to return to the user
1270 * @in: drm_mode_modeinfo to use
1275 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1278 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1280 out->clock = in->clock;
1281 out->hdisplay = in->hdisplay;
1282 out->hsync_start = in->hsync_start;
1283 out->hsync_end = in->hsync_end;
1284 out->htotal = in->htotal;
1285 out->hskew = in->hskew;
1286 out->vdisplay = in->vdisplay;
1287 out->vsync_start = in->vsync_start;
1288 out->vsync_end = in->vsync_end;
1289 out->vtotal = in->vtotal;
1290 out->vscan = in->vscan;
1291 out->vrefresh = in->vrefresh;
1292 out->flags = in->flags;
1293 out->type = in->type;
1294 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1295 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1299 * drm_mode_getresources - get graphics configuration
1300 * @inode: inode from the ioctl
1301 * @filp: file * from the ioctl
1302 * @cmd: cmd from ioctl
1303 * @arg: arg from ioctl
1306 * Takes mode config lock.
1308 * Construct a set of configuration description structures and return
1309 * them to the user, including CRTC, output and framebuffer configuration.
1311 * Called by the user via ioctl.
1314 * Zero on success, errno on failure.
1316 int drm_mode_getresources(struct drm_device *dev,
1317 void *data, struct drm_file *file_priv)
1319 struct drm_mode_card_res *card_res = data;
1320 struct list_head *lh;
1321 struct drm_framebuffer *fb;
1322 struct drm_output *output;
1323 struct drm_crtc *crtc;
1325 int output_count = 0;
1329 uint32_t __user *fb_id;
1330 uint32_t __user *crtc_id;
1331 uint32_t __user *output_id;
1333 mutex_lock(&dev->mode_config.mutex);
1335 list_for_each(lh, &dev->mode_config.fb_list)
1338 list_for_each(lh, &dev->mode_config.crtc_list)
1341 list_for_each(lh, &dev->mode_config.output_list)
1344 card_res->max_height = dev->mode_config.max_height;
1345 card_res->min_height = dev->mode_config.min_height;
1346 card_res->max_width = dev->mode_config.max_width;
1347 card_res->min_width = dev->mode_config.min_width;
1349 /* handle this in 4 parts */
1351 if (card_res->count_fbs >= fb_count) {
1353 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1354 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1355 if (put_user(fb->id, fb_id + copied)) {
1362 card_res->count_fbs = fb_count;
1365 if (card_res->count_crtcs >= crtc_count) {
1367 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1368 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1369 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1370 if (put_user(crtc->id, crtc_id + copied)) {
1377 card_res->count_crtcs = crtc_count;
1381 if (card_res->count_outputs >= output_count) {
1383 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1384 list_for_each_entry(output, &dev->mode_config.output_list,
1386 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1387 if (put_user(output->id, output_id + copied)) {
1394 card_res->count_outputs = output_count;
1396 DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1397 card_res->count_outputs);
1400 mutex_unlock(&dev->mode_config.mutex);
1405 * drm_mode_getcrtc - get CRTC configuration
1406 * @inode: inode from the ioctl
1407 * @filp: file * from the ioctl
1408 * @cmd: cmd from ioctl
1409 * @arg: arg from ioctl
1414 * Construct a CRTC configuration structure to return to the user.
1416 * Called by the user via ioctl.
1419 * Zero on success, errno on failure.
1421 int drm_mode_getcrtc(struct drm_device *dev,
1422 void *data, struct drm_file *file_priv)
1424 struct drm_mode_crtc *crtc_resp = data;
1425 struct drm_crtc *crtc;
1426 struct drm_output *output;
1430 mutex_lock(&dev->mode_config.mutex);
1431 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1432 if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1437 crtc_resp->x = crtc->x;
1438 crtc_resp->y = crtc->y;
1441 crtc_resp->fb_id = crtc->fb->id;
1443 crtc_resp->fb_id = 0;
1445 crtc_resp->outputs = 0;
1446 if (crtc->enabled) {
1448 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1449 crtc_resp->mode_valid = 1;
1451 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1452 if (output->crtc == crtc)
1453 crtc_resp->outputs |= 1 << (ocount++);
1457 crtc_resp->mode_valid = 0;
1461 mutex_unlock(&dev->mode_config.mutex);
1466 * drm_mode_getoutput - get output configuration
1467 * @inode: inode from the ioctl
1468 * @filp: file * from the ioctl
1469 * @cmd: cmd from ioctl
1470 * @arg: arg from ioctl
1475 * Construct a output configuration structure to return to the user.
1477 * Called by the user via ioctl.
1480 * Zero on success, errno on failure.
1482 int drm_mode_getoutput(struct drm_device *dev,
1483 void *data, struct drm_file *file_priv)
1485 struct drm_mode_get_output *out_resp = data;
1486 struct drm_output *output;
1487 struct drm_display_mode *mode;
1489 int props_count = 0;
1493 struct drm_mode_modeinfo u_mode;
1494 struct drm_mode_modeinfo __user *mode_ptr;
1495 uint32_t __user *prop_ptr;
1496 uint64_t __user *prop_values;
1498 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1500 DRM_DEBUG("output id %d:\n", out_resp->output);
1502 mutex_lock(&dev->mode_config.mutex);
1503 output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1504 if (!output || (output->id != out_resp->output)) {
1509 list_for_each_entry(mode, &output->modes, head)
1512 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1513 if (output->property_ids[i] != 0) {
1518 if (out_resp->count_modes == 0) {
1519 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1522 out_resp->output_type = output->output_type;
1523 out_resp->output_type_id = output->output_type_id;
1524 out_resp->mm_width = output->mm_width;
1525 out_resp->mm_height = output->mm_height;
1526 out_resp->subpixel = output->subpixel_order;
1527 out_resp->connection = output->status;
1529 out_resp->crtc = output->crtc->id;
1533 out_resp->crtcs = output->possible_crtcs;
1534 out_resp->clones = output->possible_clones;
1536 if ((out_resp->count_modes >= mode_count) && mode_count) {
1538 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1539 list_for_each_entry(mode, &output->modes, head) {
1540 drm_crtc_convert_to_umode(&u_mode, mode);
1541 if (copy_to_user(mode_ptr + copied,
1542 &u_mode, sizeof(u_mode))) {
1550 out_resp->count_modes = mode_count;
1552 if ((out_resp->count_props >= props_count) && props_count) {
1554 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1555 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1556 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1557 if (output->property_ids[i] != 0) {
1558 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1563 if (put_user(output->property_values[i], prop_values + copied)) {
1571 out_resp->count_props = props_count;
1574 mutex_unlock(&dev->mode_config.mutex);
1579 * drm_mode_setcrtc - set CRTC configuration
1580 * @inode: inode from the ioctl
1581 * @filp: file * from the ioctl
1582 * @cmd: cmd from ioctl
1583 * @arg: arg from ioctl
1588 * Build a new CRTC configuration based on user request.
1590 * Called by the user via ioctl.
1593 * Zero on success, errno on failure.
1595 int drm_mode_setcrtc(struct drm_device *dev,
1596 void *data, struct drm_file *file_priv)
1598 struct drm_mode_crtc *crtc_req = data;
1599 struct drm_crtc *crtc, *crtcfb;
1600 struct drm_output **output_set = NULL, *output;
1601 struct drm_framebuffer *fb = NULL;
1602 struct drm_display_mode *mode = NULL;
1603 uint32_t __user *set_outputs_ptr;
1607 mutex_lock(&dev->mode_config.mutex);
1608 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1609 if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1610 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1615 if (crtc_req->mode_valid) {
1616 /* If we have a mode we need a framebuffer. */
1617 /* If we pass -1, set the mode with the currently bound fb */
1618 if (crtc_req->fb_id == -1) {
1619 list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1620 if (crtcfb == crtc) {
1621 DRM_INFO("Using current fb for setmode\n");
1626 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1627 if (!fb || (fb->id != crtc_req->fb_id)) {
1628 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1634 mode = drm_mode_create(dev);
1635 drm_crtc_convert_umode(mode, &crtc_req->mode);
1636 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1639 if (crtc_req->count_outputs == 0 && mode) {
1640 DRM_DEBUG("Count outputs is 0 but mode set\n");
1645 if (crtc_req->count_outputs > 0 && !mode && !fb) {
1646 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1651 if (crtc_req->count_outputs > 0) {
1653 /* Maybe we should check that count_outputs is a sensible value. */
1654 output_set = kmalloc(crtc_req->count_outputs *
1655 sizeof(struct drm_output *), GFP_KERNEL);
1661 for (i = 0; i < crtc_req->count_outputs; i++) {
1662 set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1663 if (get_user(out_id, &set_outputs_ptr[i])) {
1668 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1669 if (!output || (out_id != output->id)) {
1670 DRM_DEBUG("Output id %d unknown\n", out_id);
1675 output_set[i] = output;
1679 ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1683 mutex_unlock(&dev->mode_config.mutex);
1687 int drm_mode_cursor_ioctl(struct drm_device *dev,
1688 void *data, struct drm_file *file_priv)
1690 struct drm_mode_cursor *req = data;
1691 struct drm_crtc *crtc;
1692 struct drm_buffer_object *bo = NULL; /* must be set */
1698 DRM_ERROR("no operation set\n");
1702 mutex_lock(&dev->mode_config.mutex);
1703 crtc = idr_find(&dev->mode_config.crtc_idr, req->crtc);
1704 if (!crtc || (crtc->id != req->crtc)) {
1705 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1710 if (req->flags & DRM_MODE_CURSOR_BO) {
1711 /* Turn of the cursor if handle is 0 */
1713 ret = drm_get_buffer_object(dev, &bo, req->handle);
1716 DRM_ERROR("invalid buffer id\n");
1721 if (crtc->funcs->cursor_set) {
1722 ret = crtc->funcs->cursor_set(crtc, bo, req->width, req->height);
1724 DRM_ERROR("crtc does not support cursor\n");
1730 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1731 if (crtc->funcs->cursor_move) {
1732 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1734 DRM_ERROR("crtc does not support cursor\n");
1740 mutex_unlock(&dev->mode_config.mutex);
1745 * drm_mode_addfb - add an FB to the graphics configuration
1746 * @inode: inode from the ioctl
1747 * @filp: file * from the ioctl
1748 * @cmd: cmd from ioctl
1749 * @arg: arg from ioctl
1752 * Takes mode config lock.
1754 * Add a new FB to the specified CRTC, given a user request.
1756 * Called by the user via ioctl.
1759 * Zero on success, errno on failure.
1761 int drm_mode_addfb(struct drm_device *dev,
1762 void *data, struct drm_file *file_priv)
1764 struct drm_mode_fb_cmd *r = data;
1765 struct drm_mode_config *config = &dev->mode_config;
1766 struct drm_framebuffer *fb;
1767 struct drm_buffer_object *bo;
1770 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1771 DRM_ERROR("mode new framebuffer width not within limits\n");
1774 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1775 DRM_ERROR("mode new framebuffer height not within limits\n");
1779 mutex_lock(&dev->mode_config.mutex);
1780 /* TODO check limits are okay */
1781 ret = drm_get_buffer_object(dev, &bo, r->handle);
1783 DRM_ERROR("BO handle not valid\n");
1788 /* TODO check buffer is sufficently large */
1789 /* TODO setup destructor callback */
1791 fb = drm_framebuffer_create(dev);
1793 DRM_ERROR("could not create framebuffer\n");
1798 fb->width = r->width;
1799 fb->height = r->height;
1800 fb->pitch = r->pitch;
1801 fb->bits_per_pixel = r->bpp;
1802 fb->depth = r->depth;
1805 r->buffer_id = fb->id;
1807 list_add(&fb->filp_head, &file_priv->fbs);
1810 mutex_unlock(&dev->mode_config.mutex);
1815 * drm_mode_rmfb - remove an FB from the configuration
1816 * @inode: inode from the ioctl
1817 * @filp: file * from the ioctl
1818 * @cmd: cmd from ioctl
1819 * @arg: arg from ioctl
1822 * Takes mode config lock.
1824 * Remove the FB specified by the user.
1826 * Called by the user via ioctl.
1829 * Zero on success, errno on failure.
1831 int drm_mode_rmfb(struct drm_device *dev,
1832 void *data, struct drm_file *file_priv)
1834 struct drm_framebuffer *fb = 0;
1835 struct drm_framebuffer *fbl = 0;
1836 uint32_t *id = data;
1840 mutex_lock(&dev->mode_config.mutex);
1841 fb = idr_find(&dev->mode_config.crtc_idr, *id);
1842 /* TODO check that we realy get a framebuffer back. */
1843 if (!fb || (*id != fb->id)) {
1844 DRM_ERROR("mode invalid framebuffer id\n");
1849 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1854 DRM_ERROR("tried to remove a fb that we didn't own\n");
1859 /* TODO release all crtc connected to the framebuffer */
1860 /* TODO unhock the destructor from the buffer object */
1862 if (fb->bo->type == drm_bo_type_kernel)
1863 DRM_ERROR("the bo type should not be of kernel type\n");
1865 list_del(&fb->filp_head);
1866 drm_framebuffer_destroy(fb);
1869 mutex_unlock(&dev->mode_config.mutex);
1874 * drm_mode_getfb - get FB info
1875 * @inode: inode from the ioctl
1876 * @filp: file * from the ioctl
1877 * @cmd: cmd from ioctl
1878 * @arg: arg from ioctl
1883 * Lookup the FB given its ID and return info about it.
1885 * Called by the user via ioctl.
1888 * Zero on success, errno on failure.
1890 int drm_mode_getfb(struct drm_device *dev,
1891 void *data, struct drm_file *file_priv)
1893 struct drm_mode_fb_cmd *r = data;
1894 struct drm_framebuffer *fb;
1897 mutex_lock(&dev->mode_config.mutex);
1898 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1899 if (!fb || (r->buffer_id != fb->id)) {
1900 DRM_ERROR("invalid framebuffer id\n");
1905 r->height = fb->height;
1906 r->width = fb->width;
1907 r->depth = fb->depth;
1908 r->bpp = fb->bits_per_pixel;
1909 r->handle = fb->bo->base.hash.key;
1910 r->pitch = fb->pitch;
1913 mutex_unlock(&dev->mode_config.mutex);
1918 * drm_fb_release - remove and free the FBs on this file
1919 * @filp: file * from the ioctl
1922 * Takes mode config lock.
1924 * Destroy all the FBs associated with @filp.
1926 * Called by the user via ioctl.
1929 * Zero on success, errno on failure.
1931 void drm_fb_release(struct file *filp)
1933 struct drm_file *priv = filp->private_data;
1934 struct drm_device *dev = priv->minor->dev;
1935 struct drm_framebuffer *fb, *tfb;
1937 mutex_lock(&dev->mode_config.mutex);
1938 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1939 list_del(&fb->filp_head);
1940 if (fb->bo->type == drm_bo_type_kernel)
1941 DRM_ERROR("the bo type should not be of kernel_type, the kernel will probably explode, why Dave\n");
1943 drm_framebuffer_destroy(fb);
1945 mutex_unlock(&dev->mode_config.mutex);
1952 static int drm_mode_attachmode(struct drm_device *dev,
1953 struct drm_output *output,
1954 struct drm_display_mode *mode)
1958 list_add_tail(&mode->head, &output->user_modes);
1962 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1963 struct drm_display_mode *mode)
1965 struct drm_output *output;
1967 struct drm_display_mode *dup_mode;
1969 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1970 if (output->crtc == crtc) {
1972 dup_mode = drm_mode_duplicate(dev, mode);
1975 ret = drm_mode_attachmode(dev, output, dup_mode);
1983 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1985 static int drm_mode_detachmode(struct drm_device *dev,
1986 struct drm_output *output,
1987 struct drm_display_mode *mode)
1991 struct drm_display_mode *match_mode, *t;
1993 list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1994 if (drm_mode_equal(match_mode, mode)) {
1995 list_del(&match_mode->head);
1996 drm_mode_destroy(dev, match_mode);
2008 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2010 struct drm_output *output;
2012 list_for_each_entry(output, &dev->mode_config.output_list, head) {
2013 drm_mode_detachmode(dev, output, mode);
2017 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2020 * drm_fb_attachmode - Attach a user mode to an output
2021 * @inode: inode from the ioctl
2022 * @filp: file * from the ioctl
2023 * @cmd: cmd from ioctl
2024 * @arg: arg from ioctl
2026 * This attaches a user specified mode to an output.
2027 * Called by the user via ioctl.
2030 * Zero on success, errno on failure.
2032 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2033 void *data, struct drm_file *file_priv)
2035 struct drm_mode_mode_cmd *mode_cmd = data;
2036 struct drm_output *output;
2037 struct drm_display_mode *mode;
2038 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2041 mutex_lock(&dev->mode_config.mutex);
2043 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2044 if (!output || (output->id != mode_cmd->output_id)) {
2049 mode = drm_mode_create(dev);
2055 drm_crtc_convert_umode(mode, umode);
2057 ret = drm_mode_attachmode(dev, output, mode);
2059 mutex_unlock(&dev->mode_config.mutex);
2065 * drm_fb_detachmode - Detach a user specified mode from an output
2066 * @inode: inode from the ioctl
2067 * @filp: file * from the ioctl
2068 * @cmd: cmd from ioctl
2069 * @arg: arg from ioctl
2071 * Called by the user via ioctl.
2074 * Zero on success, errno on failure.
2076 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2077 void *data, struct drm_file *file_priv)
2079 struct drm_mode_mode_cmd *mode_cmd = data;
2080 struct drm_output *output;
2081 struct drm_display_mode mode;
2082 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2085 mutex_lock(&dev->mode_config.mutex);
2087 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
2088 if (!output || (output->id != mode_cmd->output_id)) {
2093 drm_crtc_convert_umode(&mode, umode);
2094 ret = drm_mode_detachmode(dev, output, &mode);
2096 mutex_unlock(&dev->mode_config.mutex);
2100 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2101 const char *name, int num_values)
2103 struct drm_property *property = NULL;
2105 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2110 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2111 if (!property->values)
2115 property->id = drm_idr_get(dev, property);
2116 property->flags = flags;
2117 property->num_values = num_values;
2118 INIT_LIST_HEAD(&property->enum_blob_list);
2121 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2123 list_add_tail(&property->head, &dev->mode_config.property_list);
2129 EXPORT_SYMBOL(drm_property_create);
2131 int drm_property_add_enum(struct drm_property *property, int index,
2132 uint64_t value, const char *name)
2134 struct drm_property_enum *prop_enum;
2136 if (!(property->flags & DRM_MODE_PROP_ENUM))
2139 if (!list_empty(&property->enum_blob_list)) {
2140 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2141 if (prop_enum->value == value) {
2142 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2143 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2149 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2153 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2154 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2155 prop_enum->value = value;
2157 property->values[index] = value;
2158 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2161 EXPORT_SYMBOL(drm_property_add_enum);
2163 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2165 struct drm_property_enum *prop_enum, *pt;
2167 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2168 list_del(&prop_enum->head);
2172 if (property->num_values)
2173 kfree(property->values);
2174 drm_idr_put(dev, property->id);
2175 list_del(&property->head);
2178 EXPORT_SYMBOL(drm_property_destroy);
2180 int drm_output_attach_property(struct drm_output *output,
2181 struct drm_property *property, uint64_t init_val)
2185 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2186 if (output->property_ids[i] == 0) {
2187 output->property_ids[i] = property->id;
2188 output->property_values[i] = init_val;
2193 if (i == DRM_OUTPUT_MAX_PROPERTY)
2197 EXPORT_SYMBOL(drm_output_attach_property);
2199 int drm_output_property_set_value(struct drm_output *output,
2200 struct drm_property *property, uint64_t value)
2204 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2205 if (output->property_ids[i] == property->id) {
2206 output->property_values[i] = value;
2211 if (i == DRM_OUTPUT_MAX_PROPERTY)
2215 EXPORT_SYMBOL(drm_output_property_set_value);
2217 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2218 void *data, struct drm_file *file_priv)
2220 struct drm_mode_get_property *out_resp = data;
2221 struct drm_property *property;
2224 int value_count = 0;
2227 struct drm_property_enum *prop_enum;
2228 struct drm_mode_property_enum __user *enum_ptr;
2229 struct drm_property_blob *prop_blob;
2230 uint32_t *blob_id_ptr;
2231 uint64_t __user *values_ptr;
2232 uint32_t __user *blob_length_ptr;
2234 mutex_lock(&dev->mode_config.mutex);
2235 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2236 if (!property || (property->id != out_resp->prop_id)) {
2241 if (property->flags & DRM_MODE_PROP_ENUM) {
2242 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2244 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2245 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2249 value_count = property->num_values;
2251 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2252 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2253 out_resp->flags = property->flags;
2255 if ((out_resp->count_values >= value_count) && value_count) {
2256 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2257 for (i = 0; i < value_count; i++) {
2258 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2264 out_resp->count_values = value_count;
2266 if (property->flags & DRM_MODE_PROP_ENUM) {
2268 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2270 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2271 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2273 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2278 if (copy_to_user(&enum_ptr[copied].name,
2279 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2286 out_resp->count_enum_blobs = enum_count;
2289 if (property->flags & DRM_MODE_PROP_BLOB) {
2290 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2292 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2293 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2295 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2296 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2301 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2309 out_resp->count_enum_blobs = enum_count;
2312 mutex_unlock(&dev->mode_config.mutex);
2316 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2319 struct drm_property_blob *blob;
2321 if (!length || !data)
2324 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2328 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2329 blob->length = length;
2331 memcpy(blob->data, data, length);
2333 blob->id = drm_idr_get(dev, blob);
2335 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2339 static void drm_property_destroy_blob(struct drm_device *dev,
2340 struct drm_property_blob *blob)
2342 drm_idr_put(dev, blob->id);
2343 list_del(&blob->head);
2347 int drm_mode_getblob_ioctl(struct drm_device *dev,
2348 void *data, struct drm_file *file_priv)
2350 struct drm_mode_get_blob *out_resp = data;
2351 struct drm_property_blob *blob;
2355 mutex_lock(&dev->mode_config.mutex);
2357 blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2358 if (!blob || (blob->id != out_resp->blob_id)) {
2363 if (out_resp->length == blob->length) {
2364 blob_ptr = (void *)(unsigned long)out_resp->data;
2365 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2370 out_resp->length = blob->length;
2373 mutex_unlock(&dev->mode_config.mutex);
2377 int drm_mode_output_update_edid_property(struct drm_output *output, struct edid *edid)
2379 struct drm_device *dev = output->dev;
2381 if (output->edid_blob_ptr)
2382 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2384 output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2386 ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2389 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2391 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2392 void *data, struct drm_file *file_priv)
2394 struct drm_mode_output_set_property *out_resp = data;
2395 struct drm_property *property;
2396 struct drm_output *output;
2400 mutex_lock(&dev->mode_config.mutex);
2401 output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2402 if (!output || (output->id != out_resp->output_id)) {
2406 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2407 if (output->property_ids[i] == out_resp->prop_id)
2411 if (i == DRM_OUTPUT_MAX_PROPERTY) {
2415 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2416 if (!property || (property->id != out_resp->prop_id)) {
2420 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2423 if (property->flags & DRM_MODE_PROP_RANGE) {
2424 if (out_resp->value < property->values[0])
2427 if (out_resp->value > property->values[1])
2431 for (i = 0; i < property->num_values; i++) {
2432 if (property->values[i] == out_resp->value) {
2442 if (output->funcs->set_property)
2443 ret = output->funcs->set_property(output, property, out_resp->value);
2446 mutex_unlock(&dev->mode_config.mutex);