2 * Copyright (c) 2006-2007 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/list.h>
37 struct drm_prop_enum_list {
45 static struct drm_prop_enum_list drm_dpms_enum_list[] =
46 { { DPMSModeOn, "On" },
47 { DPMSModeStandby, "Standby" },
48 { DPMSModeSuspend, "Suspend" },
49 { DPMSModeOff, "Off" }
52 char *drm_get_dpms_name(int val)
56 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
57 if (drm_dpms_enum_list[i].type == val)
58 return drm_dpms_enum_list[i].name;
63 static struct drm_prop_enum_list drm_conn_enum_list[] =
64 { { ConnectorUnknown, "Unknown" },
65 { ConnectorVGA, "VGA" },
66 { ConnectorDVII, "DVI-I" },
67 { ConnectorDVID, "DVI-D" },
68 { ConnectorDVIA, "DVI-A" },
69 { ConnectorComposite, "Composite" },
70 { ConnectorSVIDEO, "SVIDEO" },
71 { ConnectorLVDS, "LVDS" },
72 { ConnectorComponent, "Component" },
73 { Connector9PinDIN, "9-pin DIN" },
74 { ConnectorDisplayPort, "DisplayPort" },
75 { ConnectorHDMIA, "HDMI Type A" },
76 { ConnectorHDMIB, "HDMI Type B" },
78 static struct drm_prop_enum_list drm_output_enum_list[] =
79 { { DRM_MODE_OUTPUT_NONE, "None" },
80 { DRM_MODE_OUTPUT_VGA, "VGA" },
81 { DRM_MODE_OUTPUT_TMDS, "TMDS" },
82 { DRM_MODE_OUTPUT_LVDS, "LVDS" },
83 { DRM_MODE_OUTPUT_TVDAC, "TV" },
86 char *drm_get_output_name(struct drm_output *output)
90 snprintf(buf, 32, "%s-%d", drm_output_enum_list[output->output_type].name,
91 output->output_type_id);
95 char *drm_get_output_status_name(enum drm_output_status status)
97 if (status == output_status_connected)
99 else if (status == output_status_disconnected)
100 return "disconnected";
106 * drm_idr_get - allocate a new identifier
108 * @ptr: object pointer, used to generate unique ID
111 * Caller must hold DRM mode_config lock.
113 * Create a unique identifier based on @ptr in @dev's identifier space. Used
114 * for tracking modes, CRTCs and outputs.
117 * New unique (relative to other objects in @dev) integer identifier for the
120 int drm_idr_get(struct drm_device *dev, void *ptr)
125 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
126 DRM_ERROR("Ran out memory getting a mode number\n");
130 ret = idr_get_new_above(&dev->mode_config.crtc_idr, ptr, 1, &new_id);
138 * drm_idr_put - free an identifer
143 * Caller must hold DRM mode_config lock.
145 * Free @id from @dev's unique identifier pool.
147 void drm_idr_put(struct drm_device *dev, int id)
149 idr_remove(&dev->mode_config.crtc_idr, id);
153 * drm_crtc_from_fb - find the CRTC structure associated with an fb
155 * @fb: framebuffer in question
158 * Caller must hold mode_config lock.
160 * Find CRTC in the mode_config structure that matches @fb.
163 * Pointer to the CRTC or NULL if it wasn't found.
165 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
166 struct drm_framebuffer *fb)
168 struct drm_crtc *crtc;
170 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
178 * drm_framebuffer_create - create a new framebuffer object
182 * Caller must hold mode config lock.
184 * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
187 * Pointer to new framebuffer or NULL on error.
189 struct drm_framebuffer *drm_framebuffer_create(struct drm_device *dev)
191 struct drm_framebuffer *fb;
193 fb = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
197 fb->id = drm_idr_get(dev, fb);
199 dev->mode_config.num_fb++;
200 list_add(&fb->head, &dev->mode_config.fb_list);
204 EXPORT_SYMBOL(drm_framebuffer_create);
207 * drm_framebuffer_destroy - remove a framebuffer object
208 * @fb: framebuffer to remove
211 * Caller must hold mode config lock.
213 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
214 * it, setting it to NULL.
216 void drm_framebuffer_destroy(struct drm_framebuffer *fb)
218 struct drm_device *dev = fb->dev;
219 struct drm_crtc *crtc;
221 /* remove from any CRTC */
222 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
227 drm_idr_put(dev, fb->id);
229 dev->mode_config.num_fb--;
233 EXPORT_SYMBOL(drm_framebuffer_destroy);
236 * drm_crtc_init - Initialise a new CRTC object
238 * @crtc: CRTC object to init
239 * @funcs: callbacks for the new CRTC
242 * Caller must hold mode config lock.
244 * Inits a new object created as base part of an driver crtc object.
246 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
247 const struct drm_crtc_funcs *funcs)
252 crtc->id = drm_idr_get(dev, crtc);
254 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
255 dev->mode_config.num_crtc++;
257 EXPORT_SYMBOL(drm_crtc_init);
260 * drm_crtc_cleanup - Cleans up the core crtc usage.
261 * @crtc: CRTC to cleanup
264 * Caller must hold mode config lock.
266 * Cleanup @crtc. Removes from drm modesetting space
267 * does NOT free object, caller does that.
269 void drm_crtc_cleanup(struct drm_crtc *crtc)
271 struct drm_device *dev = crtc->dev;
273 drm_idr_put(dev, crtc->id);
274 list_del(&crtc->head);
275 dev->mode_config.num_crtc--;
277 EXPORT_SYMBOL(drm_crtc_cleanup);
280 * drm_crtc_in_use - check if a given CRTC is in a mode_config
281 * @crtc: CRTC to check
284 * Caller must hold mode config lock.
286 * Walk @crtc's DRM device's mode_config and see if it's in use.
289 * True if @crtc is part of the mode_config, false otherwise.
291 bool drm_crtc_in_use(struct drm_crtc *crtc)
293 struct drm_output *output;
294 struct drm_device *dev = crtc->dev;
295 /* FIXME: Locking around list access? */
296 list_for_each_entry(output, &dev->mode_config.output_list, head)
297 if (output->crtc == crtc)
301 EXPORT_SYMBOL(drm_crtc_in_use);
304 * Detailed mode info for a standard 640x480@60Hz monitor
306 static struct drm_display_mode std_mode[] = {
307 { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
308 752, 800, 0, 480, 490, 492, 525, 0,
309 V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
313 * drm_crtc_probe_output_modes - get complete set of display modes
315 * @maxX: max width for modes
316 * @maxY: max height for modes
319 * Caller must hold mode config lock.
321 * Based on @dev's mode_config layout, scan all the outputs and try to detect
322 * modes on them. Modes will first be added to the output's probed_modes
323 * list, then culled (based on validity and the @maxX, @maxY parameters) and
324 * put into the normal modes list.
326 * Intended to be used either at bootup time or when major configuration
327 * changes have occurred.
329 * FIXME: take into account monitor limits
331 void drm_crtc_probe_single_output_modes(struct drm_output *output, int maxX, int maxY)
333 struct drm_device *dev = output->dev;
334 struct drm_display_mode *mode, *t;
336 //if (maxX == 0 || maxY == 0)
339 /* set all modes to the unverified state */
340 list_for_each_entry_safe(mode, t, &output->modes, head)
341 mode->status = MODE_UNVERIFIED;
343 output->status = (*output->funcs->detect)(output);
345 if (output->status == output_status_disconnected) {
346 DRM_DEBUG("%s is disconnected\n", drm_get_output_name(output));
347 /* TODO set EDID to NULL */
351 ret = (*output->funcs->get_modes)(output);
354 drm_mode_output_list_update(output);
358 drm_mode_validate_size(dev, &output->modes, maxX,
360 list_for_each_entry_safe(mode, t, &output->modes, head) {
361 if (mode->status == MODE_OK)
362 mode->status = (*output->funcs->mode_valid)(output,mode);
366 drm_mode_prune_invalid(dev, &output->modes, TRUE);
368 if (list_empty(&output->modes)) {
369 struct drm_display_mode *stdmode;
371 DRM_DEBUG("No valid modes on %s\n", drm_get_output_name(output));
373 /* Should we do this here ???
374 * When no valid EDID modes are available we end up
375 * here and bailed in the past, now we add a standard
376 * 640x480@60Hz mode and carry on.
378 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
379 drm_mode_probed_add(output, stdmode);
380 drm_mode_list_concat(&output->probed_modes,
383 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
384 drm_get_output_name(output));
387 drm_mode_sort(&output->modes);
389 DRM_DEBUG("Probed modes for %s\n", drm_get_output_name(output));
390 list_for_each_entry_safe(mode, t, &output->modes, head) {
391 mode->vrefresh = drm_mode_vrefresh(mode);
393 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
394 drm_mode_debug_printmodeline(mode);
398 void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY)
400 struct drm_output *output;
402 list_for_each_entry(output, &dev->mode_config.output_list, head) {
403 drm_crtc_probe_single_output_modes(output, maxX, maxY);
406 EXPORT_SYMBOL(drm_crtc_probe_output_modes);
410 * drm_disable_unused_functions - disable unused objects
414 * Caller must hold mode config lock.
416 * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
417 * by calling its dpms function, which should power it off.
419 void drm_disable_unused_functions(struct drm_device *dev)
421 struct drm_output *output;
422 struct drm_crtc *crtc;
424 list_for_each_entry(output, &dev->mode_config.output_list, head) {
426 (*output->funcs->dpms)(output, DPMSModeOff);
429 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
431 crtc->funcs->dpms(crtc, DPMSModeOff);
434 EXPORT_SYMBOL(drm_disable_unused_functions);
437 * drm_mode_probed_add - add a mode to the specified output's probed mode list
438 * @output: output the new mode
442 * Caller must hold mode config lock.
444 * Add @mode to @output's mode list for later use.
446 void drm_mode_probed_add(struct drm_output *output,
447 struct drm_display_mode *mode)
449 list_add(&mode->head, &output->probed_modes);
451 EXPORT_SYMBOL(drm_mode_probed_add);
454 * drm_mode_remove - remove and free a mode
455 * @output: output list to modify
456 * @mode: mode to remove
459 * Caller must hold mode config lock.
461 * Remove @mode from @output's mode list, then free it.
463 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
465 list_del(&mode->head);
468 EXPORT_SYMBOL(drm_mode_remove);
471 * drm_output_init - Init a preallocated output
473 * @output: the output to init
474 * @funcs: callbacks for this output
475 * @name: user visible name of the output
478 * Caller must hold @dev's mode_config lock.
480 * Initialises a preallocated output. Outputs should be
481 * subclassed as part of driver output objects.
483 void drm_output_init(struct drm_device *dev,
484 struct drm_output *output,
485 const struct drm_output_funcs *funcs,
489 output->funcs = funcs;
490 output->id = drm_idr_get(dev, output);
491 output->output_type = output_type;
492 output->output_type_id = 1; /* TODO */
493 INIT_LIST_HEAD(&output->user_modes);
494 INIT_LIST_HEAD(&output->probed_modes);
495 INIT_LIST_HEAD(&output->modes);
497 /* output_set_monitor(output)? */
498 /* check for output_ignored(output)? */
500 mutex_lock(&dev->mode_config.mutex);
501 list_add_tail(&output->head, &dev->mode_config.output_list);
502 dev->mode_config.num_output++;
504 drm_output_attach_property(output, dev->mode_config.edid_property, 0);
506 drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
508 mutex_unlock(&dev->mode_config.mutex);
510 EXPORT_SYMBOL(drm_output_init);
513 * drm_output_cleanup - cleans up an initialised output
514 * @output: output to cleanup
517 * Caller must hold @dev's mode_config lock.
519 * Cleans up the output but doesn't free the object.
521 void drm_output_cleanup(struct drm_output *output)
523 struct drm_device *dev = output->dev;
524 struct drm_display_mode *mode, *t;
526 list_for_each_entry_safe(mode, t, &output->probed_modes, head)
527 drm_mode_remove(output, mode);
529 list_for_each_entry_safe(mode, t, &output->modes, head)
530 drm_mode_remove(output, mode);
532 list_for_each_entry_safe(mode, t, &output->user_modes, head)
533 drm_mode_remove(output, mode);
535 mutex_lock(&dev->mode_config.mutex);
536 drm_idr_put(dev, output->id);
537 list_del(&output->head);
538 mutex_unlock(&dev->mode_config.mutex);
540 EXPORT_SYMBOL(drm_output_cleanup);
542 void drm_encoder_init(struct drm_device *dev,
543 struct drm_encoder *encoder,
544 const struct drm_encoder_funcs *funcs,
548 encoder->id = drm_idr_get(dev, encoder);
549 encoder->encoder_type = encoder_type;
550 encoder->funcs = funcs;
552 mutex_lock(&dev->mode_config.mutex);
553 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
554 dev->mode_config.num_encoder++;
556 mutex_unlock(&dev->mode_config.mutex);
558 EXPORT_SYMBOL(drm_encoder_init);
560 void drm_encoder_cleanup(struct drm_encoder *encoder)
562 struct drm_device *dev = encoder->dev;
563 mutex_lock(&dev->mode_config.mutex);
564 drm_idr_put(dev, encoder->id);
565 list_del(&encoder->head);
566 mutex_unlock(&dev->mode_config.mutex);
568 EXPORT_SYMBOL(drm_encoder_cleanup);
571 * drm_mode_create - create a new display mode
577 * Create a new drm_display_mode, give it an ID, and return it.
580 * Pointer to new mode on success, NULL on error.
582 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
584 struct drm_display_mode *nmode;
586 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
590 nmode->mode_id = drm_idr_get(dev, nmode);
593 EXPORT_SYMBOL(drm_mode_create);
596 * drm_mode_destroy - remove a mode
598 * @mode: mode to remove
601 * Caller must hold mode config lock.
603 * Free @mode's unique identifier, then free it.
605 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
607 drm_idr_put(dev, mode->mode_id);
611 EXPORT_SYMBOL(drm_mode_destroy);
613 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
618 * Standard properties (apply to all outputs)
620 dev->mode_config.edid_property =
621 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
624 dev->mode_config.dpms_property =
625 drm_property_create(dev, DRM_MODE_PROP_ENUM,
626 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
627 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
628 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
630 dev->mode_config.connector_type_property =
631 drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
632 "Connector Type", ARRAY_SIZE(drm_conn_enum_list));
633 for (i = 0; i < ARRAY_SIZE(drm_conn_enum_list); i++)
634 drm_property_add_enum(dev->mode_config.connector_type_property, i, drm_conn_enum_list[i].type, drm_conn_enum_list[i].name);
636 dev->mode_config.connector_num_property =
637 drm_property_create(dev, DRM_MODE_PROP_RANGE | DRM_MODE_PROP_IMMUTABLE,
639 dev->mode_config.connector_num_property->values[0] = 0;
640 dev->mode_config.connector_num_property->values[1] = 20;
646 * drm_create_tv_properties - create TV specific output properties
648 * @num_modes: number of different TV formats (modes) supported
649 * @modes: array of pointers to strings containing name of each format
651 * Called by a driver's TV initialization routine, this function creates
652 * the TV specific output properties for a given device. Caller is
653 * responsible for allocating a list of format names and passing them to
656 bool drm_create_tv_properties(struct drm_device *dev, int num_modes,
661 dev->mode_config.tv_left_margin_property =
662 drm_property_create(dev, DRM_MODE_PROP_RANGE |
663 DRM_MODE_PROP_IMMUTABLE,
665 dev->mode_config.tv_left_margin_property->values[0] = 0;
666 dev->mode_config.tv_left_margin_property->values[1] = 100;
668 dev->mode_config.tv_right_margin_property =
669 drm_property_create(dev, DRM_MODE_PROP_RANGE,
671 dev->mode_config.tv_right_margin_property->values[0] = 0;
672 dev->mode_config.tv_right_margin_property->values[1] = 100;
674 dev->mode_config.tv_top_margin_property =
675 drm_property_create(dev, DRM_MODE_PROP_RANGE,
677 dev->mode_config.tv_top_margin_property->values[0] = 0;
678 dev->mode_config.tv_top_margin_property->values[1] = 100;
680 dev->mode_config.tv_bottom_margin_property =
681 drm_property_create(dev, DRM_MODE_PROP_RANGE,
683 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
684 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
686 dev->mode_config.tv_mode_property =
687 drm_property_create(dev, DRM_MODE_PROP_ENUM,
689 for (i = 0; i < num_modes; i++)
690 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
695 EXPORT_SYMBOL(drm_create_tv_properties);
698 * drm_mode_config_init - initialize DRM mode_configuration structure
702 * None, should happen single threaded at init time.
704 * Initialize @dev's mode_config structure, used for tracking the graphics
705 * configuration of @dev.
707 void drm_mode_config_init(struct drm_device *dev)
709 mutex_init(&dev->mode_config.mutex);
710 INIT_LIST_HEAD(&dev->mode_config.fb_list);
711 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
712 INIT_LIST_HEAD(&dev->mode_config.output_list);
713 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
714 INIT_LIST_HEAD(&dev->mode_config.property_list);
715 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
716 idr_init(&dev->mode_config.crtc_idr);
718 drm_mode_create_standard_output_properties(dev);
720 /* Just to be sure */
721 dev->mode_config.num_fb = 0;
722 dev->mode_config.num_output = 0;
723 dev->mode_config.num_crtc = 0;
724 dev->mode_config.num_encoder = 0;
725 dev->mode_config.hotplug_counter = 0;
727 EXPORT_SYMBOL(drm_mode_config_init);
730 * drm_get_buffer_object - find the buffer object for a given handle
732 * @bo: pointer to caller's buffer_object pointer
733 * @handle: handle to lookup
736 * Must take @dev's struct_mutex to protect buffer object lookup.
738 * Given @handle, lookup the buffer object in @dev and put it in the caller's
742 * Zero on success, -EINVAL if the handle couldn't be found.
744 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
746 struct drm_user_object *uo;
747 struct drm_hash_item *hash;
752 mutex_lock(&dev->struct_mutex);
753 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
755 DRM_ERROR("Couldn't find handle.\n");
760 uo = drm_hash_entry(hash, struct drm_user_object, hash);
761 if (uo->type != drm_buffer_type) {
766 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
769 mutex_unlock(&dev->struct_mutex);
777 * drm_mode_config_cleanup - free up DRM mode_config info
781 * Caller must hold mode config lock.
783 * Free up all the outputs and CRTCs associated with this DRM device, then
784 * free up the framebuffers and associated buffer objects.
786 * FIXME: cleanup any dangling user buffer objects too
788 void drm_mode_config_cleanup(struct drm_device *dev)
790 struct drm_output *output, *ot;
791 struct drm_crtc *crtc, *ct;
792 struct drm_encoder *encoder, *enct;
793 struct drm_framebuffer *fb, *fbt;
794 struct drm_property *property, *pt;
796 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list, head) {
797 encoder->funcs->destroy(encoder);
800 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
801 drm_sysfs_output_remove(output);
802 output->funcs->destroy(output);
805 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
806 drm_property_destroy(dev, property);
809 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
810 /* there should only be bo of kernel type left */
811 if (fb->bo->type != drm_bo_type_kernel)
812 drm_framebuffer_destroy(fb);
814 dev->driver->fb_remove(dev, fb);
817 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
818 crtc->funcs->destroy(crtc);
822 EXPORT_SYMBOL(drm_mode_config_cleanup);
826 int drm_mode_hotplug_ioctl(struct drm_device *dev,
827 void *data, struct drm_file *file_priv)
829 struct drm_mode_hotplug *arg = data;
831 arg->counter = dev->mode_config.hotplug_counter;
837 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
838 * @out: drm_mode_modeinfo struct to return to the user
839 * @in: drm_display_mode to use
844 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
847 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
849 out->clock = in->clock;
850 out->hdisplay = in->hdisplay;
851 out->hsync_start = in->hsync_start;
852 out->hsync_end = in->hsync_end;
853 out->htotal = in->htotal;
854 out->hskew = in->hskew;
855 out->vdisplay = in->vdisplay;
856 out->vsync_start = in->vsync_start;
857 out->vsync_end = in->vsync_end;
858 out->vtotal = in->vtotal;
859 out->vscan = in->vscan;
860 out->vrefresh = in->vrefresh;
861 out->flags = in->flags;
862 out->type = in->type;
863 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
864 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
868 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
869 * @out: drm_display_mode to return to the user
870 * @in: drm_mode_modeinfo to use
875 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
878 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
880 out->clock = in->clock;
881 out->hdisplay = in->hdisplay;
882 out->hsync_start = in->hsync_start;
883 out->hsync_end = in->hsync_end;
884 out->htotal = in->htotal;
885 out->hskew = in->hskew;
886 out->vdisplay = in->vdisplay;
887 out->vsync_start = in->vsync_start;
888 out->vsync_end = in->vsync_end;
889 out->vtotal = in->vtotal;
890 out->vscan = in->vscan;
891 out->vrefresh = in->vrefresh;
892 out->flags = in->flags;
893 out->type = in->type;
894 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
895 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
899 * drm_mode_getresources - get graphics configuration
900 * @inode: inode from the ioctl
901 * @filp: file * from the ioctl
902 * @cmd: cmd from ioctl
903 * @arg: arg from ioctl
906 * Takes mode config lock.
908 * Construct a set of configuration description structures and return
909 * them to the user, including CRTC, output and framebuffer configuration.
911 * Called by the user via ioctl.
914 * Zero on success, errno on failure.
916 int drm_mode_getresources(struct drm_device *dev,
917 void *data, struct drm_file *file_priv)
919 struct drm_mode_card_res *card_res = data;
920 struct list_head *lh;
921 struct drm_framebuffer *fb;
922 struct drm_output *output;
923 struct drm_crtc *crtc;
924 struct drm_encoder *encoder;
926 int output_count = 0;
929 int encoder_count = 0;
931 uint32_t __user *fb_id;
932 uint32_t __user *crtc_id;
933 uint32_t __user *output_id;
934 uint32_t __user *encoder_id;
936 mutex_lock(&dev->mode_config.mutex);
938 list_for_each(lh, &dev->mode_config.fb_list)
941 list_for_each(lh, &dev->mode_config.crtc_list)
944 list_for_each(lh, &dev->mode_config.output_list)
947 list_for_each(lh, &dev->mode_config.encoder_list)
950 card_res->max_height = dev->mode_config.max_height;
951 card_res->min_height = dev->mode_config.min_height;
952 card_res->max_width = dev->mode_config.max_width;
953 card_res->min_width = dev->mode_config.min_width;
955 /* handle this in 4 parts */
957 if (card_res->count_fbs >= fb_count) {
959 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
960 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
961 if (put_user(fb->id, fb_id + copied)) {
968 card_res->count_fbs = fb_count;
971 if (card_res->count_crtcs >= crtc_count) {
973 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
974 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
975 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
976 if (put_user(crtc->id, crtc_id + copied)) {
983 card_res->count_crtcs = crtc_count;
987 if (card_res->count_outputs >= output_count) {
989 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
990 list_for_each_entry(output, &dev->mode_config.output_list,
992 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
993 if (put_user(output->id, output_id + copied)) {
1000 card_res->count_outputs = output_count;
1003 if (card_res->count_encoders >= encoder_count) {
1005 encoder_id = (uint32_t *)(unsigned long)card_res->encoder_id_ptr;
1006 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
1008 DRM_DEBUG("ENCODER ID is %d\n", encoder->id);
1009 if (put_user(encoder->id, encoder_id + copied)) {
1016 card_res->count_encoders = encoder_count;
1018 DRM_DEBUG("Counted %d %d %d\n", card_res->count_crtcs,
1019 card_res->count_outputs, card_res->count_encoders);
1022 mutex_unlock(&dev->mode_config.mutex);
1027 * drm_mode_getcrtc - get CRTC configuration
1028 * @inode: inode from the ioctl
1029 * @filp: file * from the ioctl
1030 * @cmd: cmd from ioctl
1031 * @arg: arg from ioctl
1036 * Construct a CRTC configuration structure to return to the user.
1038 * Called by the user via ioctl.
1041 * Zero on success, errno on failure.
1043 int drm_mode_getcrtc(struct drm_device *dev,
1044 void *data, struct drm_file *file_priv)
1046 struct drm_mode_crtc *crtc_resp = data;
1047 struct drm_crtc *crtc;
1048 struct drm_output *output;
1052 mutex_lock(&dev->mode_config.mutex);
1053 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1054 if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1059 crtc_resp->x = crtc->x;
1060 crtc_resp->y = crtc->y;
1063 crtc_resp->fb_id = crtc->fb->id;
1065 crtc_resp->fb_id = 0;
1067 crtc_resp->outputs = 0;
1068 if (crtc->enabled) {
1070 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1071 crtc_resp->mode_valid = 1;
1073 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1074 if (output->crtc == crtc)
1075 crtc_resp->outputs |= 1 << (ocount++);
1079 crtc_resp->mode_valid = 0;
1083 mutex_unlock(&dev->mode_config.mutex);
1088 * drm_mode_getoutput - get output configuration
1089 * @inode: inode from the ioctl
1090 * @filp: file * from the ioctl
1091 * @cmd: cmd from ioctl
1092 * @arg: arg from ioctl
1097 * Construct a output configuration structure to return to the user.
1099 * Called by the user via ioctl.
1102 * Zero on success, errno on failure.
1104 int drm_mode_getoutput(struct drm_device *dev,
1105 void *data, struct drm_file *file_priv)
1107 struct drm_mode_get_output *out_resp = data;
1108 struct drm_output *output;
1109 struct drm_display_mode *mode;
1111 int props_count = 0;
1112 int encoders_count = 0;
1116 struct drm_mode_modeinfo u_mode;
1117 struct drm_mode_modeinfo __user *mode_ptr;
1118 uint32_t __user *prop_ptr;
1119 uint64_t __user *prop_values;
1120 uint32_t __user *encoder_ptr;
1122 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1124 DRM_DEBUG("output id %d:\n", out_resp->output);
1126 mutex_lock(&dev->mode_config.mutex);
1127 output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1128 if (!output || (output->id != out_resp->output)) {
1133 list_for_each_entry(mode, &output->modes, head)
1136 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1137 if (output->property_ids[i] != 0) {
1142 for (i = 0; i < DRM_OUTPUT_MAX_ENCODER; i++) {
1143 if (output->encoder_ids[i] != 0) {
1148 if (out_resp->count_modes == 0) {
1149 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1152 out_resp->output_type = output->output_type;
1153 out_resp->output_type_id = output->output_type_id;
1154 out_resp->mm_width = output->display_info.width_mm;
1155 out_resp->mm_height = output->display_info.height_mm;
1156 out_resp->subpixel = output->display_info.subpixel_order;
1157 out_resp->connection = output->status;
1159 out_resp->crtc = output->crtc->id;
1163 out_resp->crtcs = output->possible_crtcs;
1164 out_resp->clones = output->possible_clones;
1166 if ((out_resp->count_modes >= mode_count) && mode_count) {
1168 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1169 list_for_each_entry(mode, &output->modes, head) {
1170 drm_crtc_convert_to_umode(&u_mode, mode);
1171 if (copy_to_user(mode_ptr + copied,
1172 &u_mode, sizeof(u_mode))) {
1180 out_resp->count_modes = mode_count;
1182 if ((out_resp->count_props >= props_count) && props_count) {
1184 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1185 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1186 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1187 if (output->property_ids[i] != 0) {
1188 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1193 if (put_user(output->property_values[i], prop_values + copied)) {
1201 out_resp->count_props = props_count;
1203 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1205 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1206 for (i = 0; i < DRM_OUTPUT_MAX_ENCODER; i++) {
1207 if (output->encoder_ids[i] != 0) {
1208 if (put_user(output->encoder_ids[i], encoder_ptr + copied)) {
1216 out_resp->count_encoders = encoders_count;
1219 mutex_unlock(&dev->mode_config.mutex);
1223 int drm_mode_getencoder(struct drm_device *dev,
1224 void *data, struct drm_file *file_priv)
1226 struct drm_mode_get_encoder *enc_resp = data;
1227 struct drm_encoder *encoder;
1230 mutex_lock(&dev->mode_config.mutex);
1231 encoder = idr_find(&dev->mode_config.crtc_idr, enc_resp->encoder_id);
1232 if (!encoder || (encoder->id != enc_resp->encoder_id)) {
1233 DRM_DEBUG("Unknown encoder ID %d\n", enc_resp->encoder_id);
1238 enc_resp->encoder_type = encoder->encoder_type;
1239 enc_resp->encoder_id = encoder->id;
1240 enc_resp->crtcs = encoder->possible_crtcs;
1241 enc_resp->clones = encoder->possible_clones;
1244 mutex_unlock(&dev->mode_config.mutex);
1249 * drm_mode_setcrtc - set CRTC configuration
1250 * @inode: inode from the ioctl
1251 * @filp: file * from the ioctl
1252 * @cmd: cmd from ioctl
1253 * @arg: arg from ioctl
1258 * Build a new CRTC configuration based on user request.
1260 * Called by the user via ioctl.
1263 * Zero on success, errno on failure.
1265 int drm_mode_setcrtc(struct drm_device *dev,
1266 void *data, struct drm_file *file_priv)
1268 struct drm_mode_crtc *crtc_req = data;
1269 struct drm_crtc *crtc, *crtcfb;
1270 struct drm_output **output_set = NULL, *output;
1271 struct drm_framebuffer *fb = NULL;
1272 struct drm_display_mode *mode = NULL;
1273 struct drm_mode_set set;
1274 uint32_t __user *set_outputs_ptr;
1278 mutex_lock(&dev->mode_config.mutex);
1279 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1280 if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1281 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1286 if (crtc_req->mode_valid) {
1287 /* If we have a mode we need a framebuffer. */
1288 /* If we pass -1, set the mode with the currently bound fb */
1289 if (crtc_req->fb_id == -1) {
1290 list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1291 if (crtcfb == crtc) {
1292 DRM_DEBUG("Using current fb for setmode\n");
1297 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1298 if (!fb || (fb->id != crtc_req->fb_id)) {
1299 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1305 mode = drm_mode_create(dev);
1306 drm_crtc_convert_umode(mode, &crtc_req->mode);
1307 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1310 if (crtc_req->count_outputs == 0 && mode) {
1311 DRM_DEBUG("Count outputs is 0 but mode set\n");
1316 if (crtc_req->count_outputs > 0 && !mode && !fb) {
1317 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1322 if (crtc_req->count_outputs > 0) {
1324 /* Maybe we should check that count_outputs is a sensible value. */
1325 output_set = kmalloc(crtc_req->count_outputs *
1326 sizeof(struct drm_output *), GFP_KERNEL);
1332 for (i = 0; i < crtc_req->count_outputs; i++) {
1333 set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1334 if (get_user(out_id, &set_outputs_ptr[i])) {
1339 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1340 if (!output || (out_id != output->id)) {
1341 DRM_DEBUG("Output id %d unknown\n", out_id);
1346 output_set[i] = output;
1351 set.x = crtc_req->x;
1352 set.y = crtc_req->y;
1354 set.outputs = output_set;
1355 set.num_outputs = crtc_req->count_outputs;
1357 ret = crtc->funcs->set_config(&set);
1361 mutex_unlock(&dev->mode_config.mutex);
1365 int drm_mode_cursor_ioctl(struct drm_device *dev,
1366 void *data, struct drm_file *file_priv)
1368 struct drm_mode_cursor *req = data;
1369 struct drm_crtc *crtc;
1370 struct drm_buffer_object *bo = NULL; /* must be set */
1376 DRM_ERROR("no operation set\n");
1380 mutex_lock(&dev->mode_config.mutex);
1381 crtc = idr_find(&dev->mode_config.crtc_idr, req->crtc);
1382 if (!crtc || (crtc->id != req->crtc)) {
1383 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1388 if (req->flags & DRM_MODE_CURSOR_BO) {
1389 /* Turn of the cursor if handle is 0 */
1391 ret = drm_get_buffer_object(dev, &bo, req->handle);
1394 DRM_ERROR("invalid buffer id\n");
1399 if (crtc->funcs->cursor_set) {
1400 ret = crtc->funcs->cursor_set(crtc, bo, req->width, req->height);
1402 DRM_ERROR("crtc does not support cursor\n");
1408 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1409 if (crtc->funcs->cursor_move) {
1410 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1412 DRM_ERROR("crtc does not support cursor\n");
1418 mutex_unlock(&dev->mode_config.mutex);
1423 * drm_mode_addfb - add an FB to the graphics configuration
1424 * @inode: inode from the ioctl
1425 * @filp: file * from the ioctl
1426 * @cmd: cmd from ioctl
1427 * @arg: arg from ioctl
1430 * Takes mode config lock.
1432 * Add a new FB to the specified CRTC, given a user request.
1434 * Called by the user via ioctl.
1437 * Zero on success, errno on failure.
1439 int drm_mode_addfb(struct drm_device *dev,
1440 void *data, struct drm_file *file_priv)
1442 struct drm_mode_fb_cmd *r = data;
1443 struct drm_mode_config *config = &dev->mode_config;
1444 struct drm_framebuffer *fb;
1445 struct drm_buffer_object *bo;
1448 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1449 DRM_ERROR("mode new framebuffer width not within limits\n");
1452 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1453 DRM_ERROR("mode new framebuffer height not within limits\n");
1457 mutex_lock(&dev->mode_config.mutex);
1458 /* TODO check limits are okay */
1459 ret = drm_get_buffer_object(dev, &bo, r->handle);
1461 DRM_ERROR("BO handle not valid\n");
1466 /* TODO check buffer is sufficently large */
1467 /* TODO setup destructor callback */
1469 fb = drm_framebuffer_create(dev);
1471 DRM_ERROR("could not create framebuffer\n");
1476 fb->width = r->width;
1477 fb->height = r->height;
1478 fb->pitch = r->pitch;
1479 fb->bits_per_pixel = r->bpp;
1480 fb->depth = r->depth;
1483 r->buffer_id = fb->id;
1485 list_add(&fb->filp_head, &file_priv->fbs);
1488 mutex_unlock(&dev->mode_config.mutex);
1493 * drm_mode_rmfb - remove an FB from the configuration
1494 * @inode: inode from the ioctl
1495 * @filp: file * from the ioctl
1496 * @cmd: cmd from ioctl
1497 * @arg: arg from ioctl
1500 * Takes mode config lock.
1502 * Remove the FB specified by the user.
1504 * Called by the user via ioctl.
1507 * Zero on success, errno on failure.
1509 int drm_mode_rmfb(struct drm_device *dev,
1510 void *data, struct drm_file *file_priv)
1512 struct drm_framebuffer *fb = NULL;
1513 struct drm_framebuffer *fbl = NULL;
1514 uint32_t *id = data;
1518 mutex_lock(&dev->mode_config.mutex);
1519 fb = idr_find(&dev->mode_config.crtc_idr, *id);
1520 /* TODO check that we realy get a framebuffer back. */
1521 if (!fb || (*id != fb->id)) {
1522 DRM_ERROR("mode invalid framebuffer id\n");
1527 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1532 DRM_ERROR("tried to remove a fb that we didn't own\n");
1537 /* TODO release all crtc connected to the framebuffer */
1538 /* TODO unhock the destructor from the buffer object */
1540 if (fb->bo->type == drm_bo_type_kernel)
1541 DRM_ERROR("the bo type should not be of kernel type\n");
1543 list_del(&fb->filp_head);
1544 drm_framebuffer_destroy(fb);
1547 mutex_unlock(&dev->mode_config.mutex);
1552 * drm_mode_getfb - get FB info
1553 * @inode: inode from the ioctl
1554 * @filp: file * from the ioctl
1555 * @cmd: cmd from ioctl
1556 * @arg: arg from ioctl
1561 * Lookup the FB given its ID and return info about it.
1563 * Called by the user via ioctl.
1566 * Zero on success, errno on failure.
1568 int drm_mode_getfb(struct drm_device *dev,
1569 void *data, struct drm_file *file_priv)
1571 struct drm_mode_fb_cmd *r = data;
1572 struct drm_framebuffer *fb;
1575 mutex_lock(&dev->mode_config.mutex);
1576 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1577 if (!fb || (r->buffer_id != fb->id)) {
1578 DRM_ERROR("invalid framebuffer id\n");
1583 r->height = fb->height;
1584 r->width = fb->width;
1585 r->depth = fb->depth;
1586 r->bpp = fb->bits_per_pixel;
1587 r->handle = fb->bo->base.hash.key;
1588 r->pitch = fb->pitch;
1591 mutex_unlock(&dev->mode_config.mutex);
1596 * drm_fb_release - remove and free the FBs on this file
1597 * @filp: file * from the ioctl
1600 * Takes mode config lock.
1602 * Destroy all the FBs associated with @filp.
1604 * Called by the user via ioctl.
1607 * Zero on success, errno on failure.
1609 void drm_fb_release(struct file *filp)
1611 struct drm_file *priv = filp->private_data;
1612 struct drm_device *dev = priv->minor->dev;
1613 struct drm_framebuffer *fb, *tfb;
1615 mutex_lock(&dev->mode_config.mutex);
1616 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1617 list_del(&fb->filp_head);
1618 if (fb->bo->type == drm_bo_type_kernel)
1619 DRM_ERROR("the bo type should not be of kernel_type, the kernel will probably explode, why Dave\n");
1621 drm_framebuffer_destroy(fb);
1623 mutex_unlock(&dev->mode_config.mutex);
1630 static int drm_mode_attachmode(struct drm_device *dev,
1631 struct drm_output *output,
1632 struct drm_display_mode *mode)
1636 list_add_tail(&mode->head, &output->user_modes);
1640 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1641 struct drm_display_mode *mode)
1643 struct drm_output *output;
1645 struct drm_display_mode *dup_mode;
1647 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1648 if (output->crtc == crtc) {
1650 dup_mode = drm_mode_duplicate(dev, mode);
1653 ret = drm_mode_attachmode(dev, output, dup_mode);
1661 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1663 static int drm_mode_detachmode(struct drm_device *dev,
1664 struct drm_output *output,
1665 struct drm_display_mode *mode)
1669 struct drm_display_mode *match_mode, *t;
1671 list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1672 if (drm_mode_equal(match_mode, mode)) {
1673 list_del(&match_mode->head);
1674 drm_mode_destroy(dev, match_mode);
1686 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1688 struct drm_output *output;
1690 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1691 drm_mode_detachmode(dev, output, mode);
1695 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1698 * drm_fb_attachmode - Attach a user mode to an output
1699 * @inode: inode from the ioctl
1700 * @filp: file * from the ioctl
1701 * @cmd: cmd from ioctl
1702 * @arg: arg from ioctl
1704 * This attaches a user specified mode to an output.
1705 * Called by the user via ioctl.
1708 * Zero on success, errno on failure.
1710 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1711 void *data, struct drm_file *file_priv)
1713 struct drm_mode_mode_cmd *mode_cmd = data;
1714 struct drm_output *output;
1715 struct drm_display_mode *mode;
1716 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1719 mutex_lock(&dev->mode_config.mutex);
1721 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1722 if (!output || (output->id != mode_cmd->output_id)) {
1727 mode = drm_mode_create(dev);
1733 drm_crtc_convert_umode(mode, umode);
1735 ret = drm_mode_attachmode(dev, output, mode);
1737 mutex_unlock(&dev->mode_config.mutex);
1743 * drm_fb_detachmode - Detach a user specified mode from an output
1744 * @inode: inode from the ioctl
1745 * @filp: file * from the ioctl
1746 * @cmd: cmd from ioctl
1747 * @arg: arg from ioctl
1749 * Called by the user via ioctl.
1752 * Zero on success, errno on failure.
1754 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1755 void *data, struct drm_file *file_priv)
1757 struct drm_mode_mode_cmd *mode_cmd = data;
1758 struct drm_output *output;
1759 struct drm_display_mode mode;
1760 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1763 mutex_lock(&dev->mode_config.mutex);
1765 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1766 if (!output || (output->id != mode_cmd->output_id)) {
1771 drm_crtc_convert_umode(&mode, umode);
1772 ret = drm_mode_detachmode(dev, output, &mode);
1774 mutex_unlock(&dev->mode_config.mutex);
1778 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1779 const char *name, int num_values)
1781 struct drm_property *property = NULL;
1783 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1788 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1789 if (!property->values)
1793 property->id = drm_idr_get(dev, property);
1794 property->flags = flags;
1795 property->num_values = num_values;
1796 INIT_LIST_HEAD(&property->enum_blob_list);
1799 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1801 list_add_tail(&property->head, &dev->mode_config.property_list);
1807 EXPORT_SYMBOL(drm_property_create);
1809 int drm_property_add_enum(struct drm_property *property, int index,
1810 uint64_t value, const char *name)
1812 struct drm_property_enum *prop_enum;
1814 if (!(property->flags & DRM_MODE_PROP_ENUM))
1817 if (!list_empty(&property->enum_blob_list)) {
1818 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1819 if (prop_enum->value == value) {
1820 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1821 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1827 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1831 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1832 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1833 prop_enum->value = value;
1835 property->values[index] = value;
1836 list_add_tail(&prop_enum->head, &property->enum_blob_list);
1839 EXPORT_SYMBOL(drm_property_add_enum);
1841 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1843 struct drm_property_enum *prop_enum, *pt;
1845 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1846 list_del(&prop_enum->head);
1850 if (property->num_values)
1851 kfree(property->values);
1852 drm_idr_put(dev, property->id);
1853 list_del(&property->head);
1856 EXPORT_SYMBOL(drm_property_destroy);
1858 int drm_output_attach_property(struct drm_output *output,
1859 struct drm_property *property, uint64_t init_val)
1863 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1864 if (output->property_ids[i] == 0) {
1865 output->property_ids[i] = property->id;
1866 output->property_values[i] = init_val;
1871 if (i == DRM_OUTPUT_MAX_PROPERTY)
1875 EXPORT_SYMBOL(drm_output_attach_property);
1877 int drm_output_property_set_value(struct drm_output *output,
1878 struct drm_property *property, uint64_t value)
1882 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1883 if (output->property_ids[i] == property->id) {
1884 output->property_values[i] = value;
1889 if (i == DRM_OUTPUT_MAX_PROPERTY)
1893 EXPORT_SYMBOL(drm_output_property_set_value);
1895 int drm_output_property_get_value(struct drm_output *output,
1896 struct drm_property *property, uint64_t *val)
1900 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1901 if (output->property_ids[i] == property->id) {
1902 *val = output->property_values[i];
1907 if (i == DRM_OUTPUT_MAX_PROPERTY)
1911 EXPORT_SYMBOL(drm_output_property_get_value);
1913 int drm_mode_getproperty_ioctl(struct drm_device *dev,
1914 void *data, struct drm_file *file_priv)
1916 struct drm_mode_get_property *out_resp = data;
1917 struct drm_property *property;
1920 int value_count = 0;
1923 struct drm_property_enum *prop_enum;
1924 struct drm_mode_property_enum __user *enum_ptr;
1925 struct drm_property_blob *prop_blob;
1926 uint32_t *blob_id_ptr;
1927 uint64_t __user *values_ptr;
1928 uint32_t __user *blob_length_ptr;
1930 mutex_lock(&dev->mode_config.mutex);
1931 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
1932 if (!property || (property->id != out_resp->prop_id)) {
1937 if (property->flags & DRM_MODE_PROP_ENUM) {
1938 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
1940 } else if (property->flags & DRM_MODE_PROP_BLOB) {
1941 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
1945 value_count = property->num_values;
1947 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
1948 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
1949 out_resp->flags = property->flags;
1951 if ((out_resp->count_values >= value_count) && value_count) {
1952 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
1953 for (i = 0; i < value_count; i++) {
1954 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
1960 out_resp->count_values = value_count;
1962 if (property->flags & DRM_MODE_PROP_ENUM) {
1964 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
1966 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
1967 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1969 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
1974 if (copy_to_user(&enum_ptr[copied].name,
1975 &prop_enum->name, DRM_PROP_NAME_LEN)) {
1982 out_resp->count_enum_blobs = enum_count;
1985 if (property->flags & DRM_MODE_PROP_BLOB) {
1986 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
1988 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
1989 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
1991 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
1992 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
1997 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2005 out_resp->count_enum_blobs = enum_count;
2008 mutex_unlock(&dev->mode_config.mutex);
2012 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2015 struct drm_property_blob *blob;
2017 if (!length || !data)
2020 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2024 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2025 blob->length = length;
2027 memcpy(blob->data, data, length);
2029 blob->id = drm_idr_get(dev, blob);
2031 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2035 static void drm_property_destroy_blob(struct drm_device *dev,
2036 struct drm_property_blob *blob)
2038 drm_idr_put(dev, blob->id);
2039 list_del(&blob->head);
2043 int drm_mode_getblob_ioctl(struct drm_device *dev,
2044 void *data, struct drm_file *file_priv)
2046 struct drm_mode_get_blob *out_resp = data;
2047 struct drm_property_blob *blob;
2051 mutex_lock(&dev->mode_config.mutex);
2053 blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
2054 if (!blob || (blob->id != out_resp->blob_id)) {
2059 if (out_resp->length == blob->length) {
2060 blob_ptr = (void *)(unsigned long)out_resp->data;
2061 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2066 out_resp->length = blob->length;
2069 mutex_unlock(&dev->mode_config.mutex);
2073 int drm_mode_output_update_edid_property(struct drm_output *output, struct edid *edid)
2075 struct drm_device *dev = output->dev;
2077 if (output->edid_blob_ptr)
2078 drm_property_destroy_blob(dev, output->edid_blob_ptr);
2080 output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
2082 ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
2085 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
2087 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
2088 void *data, struct drm_file *file_priv)
2090 struct drm_mode_output_set_property *out_resp = data;
2091 struct drm_property *property;
2092 struct drm_output *output;
2096 mutex_lock(&dev->mode_config.mutex);
2097 output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
2098 if (!output || (output->id != out_resp->output_id)) {
2102 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
2103 if (output->property_ids[i] == out_resp->prop_id)
2107 if (i == DRM_OUTPUT_MAX_PROPERTY) {
2111 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2112 if (!property || (property->id != out_resp->prop_id)) {
2116 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2119 if (property->flags & DRM_MODE_PROP_RANGE) {
2120 if (out_resp->value < property->values[0])
2123 if (out_resp->value > property->values[1])
2127 for (i = 0; i < property->num_values; i++) {
2128 if (property->values[i] == out_resp->value) {
2138 if (output->funcs->set_property)
2139 ret = output->funcs->set_property(output, property, out_resp->value);
2142 mutex_unlock(&dev->mode_config.mutex);
2147 int drm_mode_replacefb(struct drm_device *dev,
2148 void *data, struct drm_file *file_priv)
2150 struct drm_mode_fb_cmd *r = data;
2151 struct drm_framebuffer *fb;
2152 struct drm_buffer_object *bo;
2154 struct drm_framebuffer *fbl = NULL;
2156 /* right replace the current bo attached to this fb with a new bo */
2157 mutex_lock(&dev->mode_config.mutex);
2158 ret = drm_get_buffer_object(dev, &bo, r->handle);
2164 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
2165 if (!fb || (r->buffer_id != fb->id)) {
2170 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2175 DRM_ERROR("tried to replace an fb we didn't own\n");
2180 if (fb->bo->type == drm_bo_type_kernel)
2181 DRM_ERROR("the bo should not be a kernel bo\n");
2183 fb->width = r->width;
2184 fb->height = r->height;
2185 fb->pitch = r->pitch;
2186 fb->bits_per_pixel = r->bpp;
2187 fb->depth = r->depth;
2190 if (dev->mode_config.funcs->resize_fb)
2191 dev->mode_config.funcs->resize_fb(dev, fb);
2195 /* find all crtcs connected to this fb */
2196 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2197 if (crtc->fb->id == r->buffer_id) {
2198 crtc->funcs->mode_set_base(crtc, crtc->x, crtc->y);
2203 mutex_unlock(&dev->mode_config.mutex);
2208 int drm_mode_output_attach_encoder(struct drm_output *output,
2209 struct drm_encoder *encoder)
2213 for (i = 0; i < DRM_OUTPUT_MAX_ENCODER; i++) {
2214 if (output->encoder_ids[i] == 0) {
2215 output->encoder_ids[i] = encoder->id;
2221 EXPORT_SYMBOL(drm_mode_output_attach_encoder);
2223 void drm_mode_output_detach_encoder(struct drm_output *output,
2224 struct drm_encoder *encoder)
2227 for (i = 0; i < DRM_OUTPUT_MAX_ENCODER; i++) {
2228 if (output->encoder_ids[i] == encoder->id) {
2229 output->encoder_ids[i] = 0;
2234 EXPORT_SYMBOL(drm_mode_output_detach_encoder);