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" }
51 char *drm_get_dpms_name(int val)
55 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
56 if (drm_dpms_enum_list[i].type == val)
57 return drm_dpms_enum_list[i].name;
62 static struct drm_prop_enum_list drm_conn_enum_list[] =
63 { { ConnectorUnknown, "Unknown" },
64 { ConnectorVGA, "VGA" },
65 { ConnectorDVII, "DVI-I" },
66 { ConnectorDVID, "DVI-D" },
67 { ConnectorDVIA, "DVI-A" },
68 { ConnectorComposite, "Composite" },
69 { ConnectorSVIDEO, "SVIDEO" },
70 { ConnectorLVDS, "LVDS" },
71 { ConnectorComponent, "Component" },
72 { Connector9PinDIN, "9-pin DIN" },
73 { ConnectorDisplayPort, "DisplayPort" },
74 { ConnectorHDMIA, "HDMI Type A" },
75 { ConnectorHDMIB, "HDMI Type B" },
77 static struct drm_prop_enum_list drm_output_enum_list[] =
78 { { DRM_MODE_OUTPUT_NONE, "None" },
79 { DRM_MODE_OUTPUT_DAC, "DAC" },
80 { DRM_MODE_OUTPUT_TMDS, "TMDS" },
81 { DRM_MODE_OUTPUT_LVDS, "LVDS" },
82 { DRM_MODE_OUTPUT_TVDAC, "TV" },
85 char *drm_get_output_name(struct drm_output *output)
89 snprintf(buf, 32, "%s-%d", drm_output_enum_list[output->output_type].name,
90 output->output_type_id);
94 char *drm_get_output_status_name(enum drm_output_status status)
96 if (status == output_status_connected)
98 else if (status == output_status_disconnected)
99 return "disconnected";
105 * drm_idr_get - allocate a new identifier
107 * @ptr: object pointer, used to generate unique ID
110 * Caller must hold DRM mode_config lock.
112 * Create a unique identifier based on @ptr in @dev's identifier space. Used
113 * for tracking modes, CRTCs and outputs.
116 * New unique (relative to other objects in @dev) integer identifier for the
119 int drm_idr_get(struct drm_device *dev, void *ptr)
124 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
125 DRM_ERROR("Ran out memory getting a mode number\n");
129 ret = idr_get_new_above(&dev->mode_config.crtc_idr, ptr, 1, &new_id);
137 * drm_idr_put - free an identifer
142 * Caller must hold DRM mode_config lock.
144 * Free @id from @dev's unique identifier pool.
146 void drm_idr_put(struct drm_device *dev, int id)
148 idr_remove(&dev->mode_config.crtc_idr, id);
152 * drm_crtc_from_fb - find the CRTC structure associated with an fb
154 * @fb: framebuffer in question
157 * Caller must hold mode_config lock.
159 * Find CRTC in the mode_config structure that matches @fb.
162 * Pointer to the CRTC or NULL if it wasn't found.
164 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
165 struct drm_framebuffer *fb)
167 struct drm_crtc *crtc;
169 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
177 * drm_framebuffer_create - create a new framebuffer object
181 * Caller must hold mode config lock.
183 * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
186 * Pointer to new framebuffer or NULL on error.
188 struct drm_framebuffer *drm_framebuffer_create(struct drm_device *dev)
190 struct drm_framebuffer *fb;
192 fb = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
196 fb->id = drm_idr_get(dev, fb);
198 dev->mode_config.num_fb++;
199 list_add(&fb->head, &dev->mode_config.fb_list);
203 EXPORT_SYMBOL(drm_framebuffer_create);
206 * drm_framebuffer_destroy - remove a framebuffer object
207 * @fb: framebuffer to remove
210 * Caller must hold mode config lock.
212 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
213 * it, setting it to NULL.
215 void drm_framebuffer_destroy(struct drm_framebuffer *fb)
217 struct drm_device *dev = fb->dev;
218 struct drm_crtc *crtc;
220 /* remove from any CRTC */
221 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
226 drm_idr_put(dev, fb->id);
228 dev->mode_config.num_fb--;
232 EXPORT_SYMBOL(drm_framebuffer_destroy);
235 * drm_crtc_init - Initialise a new CRTC object
237 * @crtc: CRTC object to init
238 * @funcs: callbacks for the new CRTC
241 * Caller must hold mode config lock.
243 * Inits a new object created as base part of an driver crtc object.
245 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
246 const struct drm_crtc_funcs *funcs)
251 crtc->id = drm_idr_get(dev, crtc);
253 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
254 dev->mode_config.num_crtc++;
256 EXPORT_SYMBOL(drm_crtc_init);
259 * drm_crtc_cleanup - Cleans up the core crtc usage.
260 * @crtc: CRTC to cleanup
263 * Caller must hold mode config lock.
265 * Cleanup @crtc. Removes from drm modesetting space
266 * does NOT free object, caller does that.
268 void drm_crtc_cleanup(struct drm_crtc *crtc)
270 struct drm_device *dev = crtc->dev;
272 drm_idr_put(dev, crtc->id);
273 list_del(&crtc->head);
274 dev->mode_config.num_crtc--;
276 EXPORT_SYMBOL(drm_crtc_cleanup);
279 * drm_crtc_in_use - check if a given CRTC is in a mode_config
280 * @crtc: CRTC to check
283 * Caller must hold mode config lock.
285 * Walk @crtc's DRM device's mode_config and see if it's in use.
288 * True if @crtc is part of the mode_config, false otherwise.
290 bool drm_crtc_in_use(struct drm_crtc *crtc)
292 struct drm_output *output;
293 struct drm_device *dev = crtc->dev;
294 /* FIXME: Locking around list access? */
295 list_for_each_entry(output, &dev->mode_config.output_list, head)
296 if (output->crtc == crtc)
300 EXPORT_SYMBOL(drm_crtc_in_use);
303 * Detailed mode info for a standard 640x480@60Hz monitor
305 static struct drm_display_mode std_mode[] = {
306 { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
307 752, 800, 0, 480, 490, 492, 525, 0,
308 V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
312 * drm_crtc_probe_output_modes - get complete set of display modes
314 * @maxX: max width for modes
315 * @maxY: max height for modes
318 * Caller must hold mode config lock.
320 * Based on @dev's mode_config layout, scan all the outputs and try to detect
321 * modes on them. Modes will first be added to the output's probed_modes
322 * list, then culled (based on validity and the @maxX, @maxY parameters) and
323 * put into the normal modes list.
325 * Intended to be used either at bootup time or when major configuration
326 * changes have occurred.
328 * FIXME: take into account monitor limits
330 void drm_crtc_probe_single_output_modes(struct drm_output *output, int maxX, int maxY)
332 struct drm_device *dev = output->dev;
333 struct drm_display_mode *mode, *t;
335 //if (maxX == 0 || maxY == 0)
338 /* set all modes to the unverified state */
339 list_for_each_entry_safe(mode, t, &output->modes, head)
340 mode->status = MODE_UNVERIFIED;
342 output->status = (*output->funcs->detect)(output);
344 if (output->status == output_status_disconnected) {
345 DRM_DEBUG("%s is disconnected\n", drm_get_output_name(output));
346 /* TODO set EDID to NULL */
350 ret = (*output->funcs->get_modes)(output);
353 drm_mode_output_list_update(output);
357 drm_mode_validate_size(dev, &output->modes, maxX,
359 list_for_each_entry_safe(mode, t, &output->modes, head) {
360 if (mode->status == MODE_OK)
361 mode->status = (*output->funcs->mode_valid)(output,mode);
365 drm_mode_prune_invalid(dev, &output->modes, TRUE);
367 if (list_empty(&output->modes)) {
368 struct drm_display_mode *stdmode;
370 DRM_DEBUG("No valid modes on %s\n", drm_get_output_name(output));
372 /* Should we do this here ???
373 * When no valid EDID modes are available we end up
374 * here and bailed in the past, now we add a standard
375 * 640x480@60Hz mode and carry on.
377 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
378 drm_mode_probed_add(output, stdmode);
379 drm_mode_list_concat(&output->probed_modes,
382 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
383 drm_get_output_name(output));
386 drm_mode_sort(&output->modes);
388 DRM_DEBUG("Probed modes for %s\n", drm_get_output_name(output));
389 list_for_each_entry_safe(mode, t, &output->modes, head) {
390 mode->vrefresh = drm_mode_vrefresh(mode);
392 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
393 drm_mode_debug_printmodeline(mode);
397 void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY)
399 struct drm_output *output;
401 list_for_each_entry(output, &dev->mode_config.output_list, head) {
402 drm_crtc_probe_single_output_modes(output, maxX, maxY);
405 EXPORT_SYMBOL(drm_crtc_probe_output_modes);
409 * drm_disable_unused_functions - disable unused objects
413 * Caller must hold mode config lock.
415 * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
416 * by calling its dpms function, which should power it off.
418 void drm_disable_unused_functions(struct drm_device *dev)
420 struct drm_output *output;
421 struct drm_crtc *crtc;
423 list_for_each_entry(output, &dev->mode_config.output_list, head) {
425 (*output->funcs->dpms)(output, DPMSModeOff);
428 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
430 crtc->funcs->dpms(crtc, DPMSModeOff);
433 EXPORT_SYMBOL(drm_disable_unused_functions);
436 * drm_mode_probed_add - add a mode to the specified output's probed mode list
437 * @output: output the new mode
441 * Caller must hold mode config lock.
443 * Add @mode to @output's mode list for later use.
445 void drm_mode_probed_add(struct drm_output *output,
446 struct drm_display_mode *mode)
448 list_add(&mode->head, &output->probed_modes);
450 EXPORT_SYMBOL(drm_mode_probed_add);
453 * drm_mode_remove - remove and free a mode
454 * @output: output list to modify
455 * @mode: mode to remove
458 * Caller must hold mode config lock.
460 * Remove @mode from @output's mode list, then free it.
462 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
464 list_del(&mode->head);
467 EXPORT_SYMBOL(drm_mode_remove);
470 * drm_output_init - Init a preallocated output
472 * @output: the output to init
473 * @funcs: callbacks for this output
474 * @name: user visible name of the output
477 * Caller must hold @dev's mode_config lock.
479 * Initialises a preallocated output. Outputs should be
480 * subclassed as part of driver output objects.
482 void drm_output_init(struct drm_device *dev,
483 struct drm_output *output,
484 const struct drm_output_funcs *funcs,
488 output->funcs = funcs;
489 output->id = drm_idr_get(dev, output);
490 output->output_type = output_type;
491 output->output_type_id = 1; /* TODO */
492 INIT_LIST_HEAD(&output->user_modes);
493 INIT_LIST_HEAD(&output->probed_modes);
494 INIT_LIST_HEAD(&output->modes);
496 /* output_set_monitor(output)? */
497 /* check for output_ignored(output)? */
499 mutex_lock(&dev->mode_config.mutex);
500 list_add_tail(&output->head, &dev->mode_config.output_list);
501 dev->mode_config.num_output++;
503 drm_output_attach_property(output, dev->mode_config.edid_property, 0);
505 drm_output_attach_property(output, dev->mode_config.dpms_property, 0);
507 mutex_unlock(&dev->mode_config.mutex);
509 EXPORT_SYMBOL(drm_output_init);
512 * drm_output_cleanup - cleans up an initialised output
513 * @output: output to cleanup
516 * Caller must hold @dev's mode_config lock.
518 * Cleans up the output but doesn't free the object.
520 void drm_output_cleanup(struct drm_output *output)
522 struct drm_device *dev = output->dev;
523 struct drm_display_mode *mode, *t;
525 list_for_each_entry_safe(mode, t, &output->probed_modes, head)
526 drm_mode_remove(output, mode);
528 list_for_each_entry_safe(mode, t, &output->modes, head)
529 drm_mode_remove(output, mode);
531 list_for_each_entry_safe(mode, t, &output->user_modes, head)
532 drm_mode_remove(output, mode);
534 mutex_lock(&dev->mode_config.mutex);
535 drm_idr_put(dev, output->id);
536 list_del(&output->head);
537 mutex_unlock(&dev->mode_config.mutex);
539 EXPORT_SYMBOL(drm_output_cleanup);
543 * drm_mode_create - create a new display mode
549 * Create a new drm_display_mode, give it an ID, and return it.
552 * Pointer to new mode on success, NULL on error.
554 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
556 struct drm_display_mode *nmode;
558 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
562 nmode->mode_id = drm_idr_get(dev, nmode);
565 EXPORT_SYMBOL(drm_mode_create);
568 * drm_mode_destroy - remove a mode
570 * @mode: mode to remove
573 * Caller must hold mode config lock.
575 * Free @mode's unique identifier, then free it.
577 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
579 drm_idr_put(dev, mode->mode_id);
583 EXPORT_SYMBOL(drm_mode_destroy);
585 static int drm_mode_create_standard_output_properties(struct drm_device *dev)
590 * Standard properties (apply to all outputs)
592 dev->mode_config.edid_property =
593 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
596 dev->mode_config.dpms_property =
597 drm_property_create(dev, DRM_MODE_PROP_ENUM,
598 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
599 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
600 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
602 dev->mode_config.connector_type_property =
603 drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
604 "Connector Type", ARRAY_SIZE(drm_conn_enum_list));
605 for (i = 0; i < ARRAY_SIZE(drm_conn_enum_list); i++)
606 drm_property_add_enum(dev->mode_config.connector_type_property, i, drm_conn_enum_list[i].type, drm_conn_enum_list[i].name);
608 dev->mode_config.connector_num_property =
609 drm_property_create(dev, DRM_MODE_PROP_RANGE | DRM_MODE_PROP_IMMUTABLE,
611 dev->mode_config.connector_num_property->values[0] = 0;
612 dev->mode_config.connector_num_property->values[1] = 20;
618 * drm_create_tv_properties - create TV specific output properties
620 * @num_modes: number of different TV formats (modes) supported
621 * @modes: array of pointers to strings containing name of each format
623 * Called by a driver's TV initialization routine, this function creates
624 * the TV specific output properties for a given device. Caller is
625 * responsible for allocating a list of format names and passing them to
628 bool drm_create_tv_properties(struct drm_device *dev, int num_modes,
633 dev->mode_config.tv_left_margin_property =
634 drm_property_create(dev, DRM_MODE_PROP_RANGE |
635 DRM_MODE_PROP_IMMUTABLE,
637 dev->mode_config.tv_left_margin_property->values[0] = 0;
638 dev->mode_config.tv_left_margin_property->values[1] = 100;
640 dev->mode_config.tv_right_margin_property =
641 drm_property_create(dev, DRM_MODE_PROP_RANGE,
643 dev->mode_config.tv_right_margin_property->values[0] = 0;
644 dev->mode_config.tv_right_margin_property->values[1] = 100;
646 dev->mode_config.tv_top_margin_property =
647 drm_property_create(dev, DRM_MODE_PROP_RANGE,
649 dev->mode_config.tv_top_margin_property->values[0] = 0;
650 dev->mode_config.tv_top_margin_property->values[1] = 100;
652 dev->mode_config.tv_bottom_margin_property =
653 drm_property_create(dev, DRM_MODE_PROP_RANGE,
655 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
656 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
658 dev->mode_config.tv_mode_property =
659 drm_property_create(dev, DRM_MODE_PROP_ENUM,
661 for (i = 0; i < num_modes; i++)
662 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
667 EXPORT_SYMBOL(drm_create_tv_properties);
670 * drm_mode_config_init - initialize DRM mode_configuration structure
674 * None, should happen single threaded at init time.
676 * Initialize @dev's mode_config structure, used for tracking the graphics
677 * configuration of @dev.
679 void drm_mode_config_init(struct drm_device *dev)
681 mutex_init(&dev->mode_config.mutex);
682 INIT_LIST_HEAD(&dev->mode_config.fb_list);
683 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
684 INIT_LIST_HEAD(&dev->mode_config.output_list);
685 INIT_LIST_HEAD(&dev->mode_config.property_list);
686 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
687 idr_init(&dev->mode_config.crtc_idr);
689 drm_mode_create_standard_output_properties(dev);
691 /* Just to be sure */
692 dev->mode_config.num_fb = 0;
693 dev->mode_config.num_output = 0;
694 dev->mode_config.num_crtc = 0;
695 dev->mode_config.hotplug_counter = 0;
697 EXPORT_SYMBOL(drm_mode_config_init);
700 * drm_get_buffer_object - find the buffer object for a given handle
702 * @bo: pointer to caller's buffer_object pointer
703 * @handle: handle to lookup
706 * Must take @dev's struct_mutex to protect buffer object lookup.
708 * Given @handle, lookup the buffer object in @dev and put it in the caller's
712 * Zero on success, -EINVAL if the handle couldn't be found.
714 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
716 struct drm_user_object *uo;
717 struct drm_hash_item *hash;
722 mutex_lock(&dev->struct_mutex);
723 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
725 DRM_ERROR("Couldn't find handle.\n");
730 uo = drm_hash_entry(hash, struct drm_user_object, hash);
731 if (uo->type != drm_buffer_type) {
736 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
739 mutex_unlock(&dev->struct_mutex);
747 * drm_mode_config_cleanup - free up DRM mode_config info
751 * Caller must hold mode config lock.
753 * Free up all the outputs and CRTCs associated with this DRM device, then
754 * free up the framebuffers and associated buffer objects.
756 * FIXME: cleanup any dangling user buffer objects too
758 void drm_mode_config_cleanup(struct drm_device *dev)
760 struct drm_output *output, *ot;
761 struct drm_crtc *crtc, *ct;
762 struct drm_framebuffer *fb, *fbt;
763 struct drm_property *property, *pt;
765 list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
766 drm_sysfs_output_remove(output);
767 output->funcs->destroy(output);
770 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
771 drm_property_destroy(dev, property);
774 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
775 /* there should only be bo of kernel type left */
776 if (fb->bo->type != drm_bo_type_kernel)
777 drm_framebuffer_destroy(fb);
779 dev->driver->fb_remove(dev, fb);
782 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
783 crtc->funcs->destroy(crtc);
787 EXPORT_SYMBOL(drm_mode_config_cleanup);
791 int drm_mode_hotplug_ioctl(struct drm_device *dev,
792 void *data, struct drm_file *file_priv)
794 struct drm_mode_hotplug *arg = data;
796 arg->counter = dev->mode_config.hotplug_counter;
802 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
803 * @out: drm_mode_modeinfo struct to return to the user
804 * @in: drm_display_mode to use
809 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
812 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
814 out->clock = in->clock;
815 out->hdisplay = in->hdisplay;
816 out->hsync_start = in->hsync_start;
817 out->hsync_end = in->hsync_end;
818 out->htotal = in->htotal;
819 out->hskew = in->hskew;
820 out->vdisplay = in->vdisplay;
821 out->vsync_start = in->vsync_start;
822 out->vsync_end = in->vsync_end;
823 out->vtotal = in->vtotal;
824 out->vscan = in->vscan;
825 out->vrefresh = in->vrefresh;
826 out->flags = in->flags;
827 out->type = in->type;
828 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
829 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
833 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
834 * @out: drm_display_mode to return to the user
835 * @in: drm_mode_modeinfo to use
840 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
843 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
845 out->clock = in->clock;
846 out->hdisplay = in->hdisplay;
847 out->hsync_start = in->hsync_start;
848 out->hsync_end = in->hsync_end;
849 out->htotal = in->htotal;
850 out->hskew = in->hskew;
851 out->vdisplay = in->vdisplay;
852 out->vsync_start = in->vsync_start;
853 out->vsync_end = in->vsync_end;
854 out->vtotal = in->vtotal;
855 out->vscan = in->vscan;
856 out->vrefresh = in->vrefresh;
857 out->flags = in->flags;
858 out->type = in->type;
859 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
860 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
864 * drm_mode_getresources - get graphics configuration
865 * @inode: inode from the ioctl
866 * @filp: file * from the ioctl
867 * @cmd: cmd from ioctl
868 * @arg: arg from ioctl
871 * Takes mode config lock.
873 * Construct a set of configuration description structures and return
874 * them to the user, including CRTC, output and framebuffer configuration.
876 * Called by the user via ioctl.
879 * Zero on success, errno on failure.
881 int drm_mode_getresources(struct drm_device *dev,
882 void *data, struct drm_file *file_priv)
884 struct drm_mode_card_res *card_res = data;
885 struct list_head *lh;
886 struct drm_framebuffer *fb;
887 struct drm_output *output;
888 struct drm_crtc *crtc;
890 int output_count = 0;
894 uint32_t __user *fb_id;
895 uint32_t __user *crtc_id;
896 uint32_t __user *output_id;
898 mutex_lock(&dev->mode_config.mutex);
900 list_for_each(lh, &dev->mode_config.fb_list)
903 list_for_each(lh, &dev->mode_config.crtc_list)
906 list_for_each(lh, &dev->mode_config.output_list)
909 card_res->max_height = dev->mode_config.max_height;
910 card_res->min_height = dev->mode_config.min_height;
911 card_res->max_width = dev->mode_config.max_width;
912 card_res->min_width = dev->mode_config.min_width;
914 /* handle this in 4 parts */
916 if (card_res->count_fbs >= fb_count) {
918 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
919 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
920 if (put_user(fb->id, fb_id + copied)) {
927 card_res->count_fbs = fb_count;
930 if (card_res->count_crtcs >= crtc_count) {
932 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
933 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
934 DRM_DEBUG("CRTC ID is %d\n", crtc->id);
935 if (put_user(crtc->id, crtc_id + copied)) {
942 card_res->count_crtcs = crtc_count;
946 if (card_res->count_outputs >= output_count) {
948 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
949 list_for_each_entry(output, &dev->mode_config.output_list,
951 DRM_DEBUG("OUTPUT ID is %d\n", output->id);
952 if (put_user(output->id, output_id + copied)) {
959 card_res->count_outputs = output_count;
961 DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
962 card_res->count_outputs);
965 mutex_unlock(&dev->mode_config.mutex);
970 * drm_mode_getcrtc - get CRTC configuration
971 * @inode: inode from the ioctl
972 * @filp: file * from the ioctl
973 * @cmd: cmd from ioctl
974 * @arg: arg from ioctl
979 * Construct a CRTC configuration structure to return to the user.
981 * Called by the user via ioctl.
984 * Zero on success, errno on failure.
986 int drm_mode_getcrtc(struct drm_device *dev,
987 void *data, struct drm_file *file_priv)
989 struct drm_mode_crtc *crtc_resp = data;
990 struct drm_crtc *crtc;
991 struct drm_output *output;
995 mutex_lock(&dev->mode_config.mutex);
996 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
997 if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1002 crtc_resp->x = crtc->x;
1003 crtc_resp->y = crtc->y;
1006 crtc_resp->fb_id = crtc->fb->id;
1008 crtc_resp->fb_id = 0;
1010 crtc_resp->outputs = 0;
1011 if (crtc->enabled) {
1013 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1014 crtc_resp->mode_valid = 1;
1016 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1017 if (output->crtc == crtc)
1018 crtc_resp->outputs |= 1 << (ocount++);
1022 crtc_resp->mode_valid = 0;
1026 mutex_unlock(&dev->mode_config.mutex);
1031 * drm_mode_getoutput - get output configuration
1032 * @inode: inode from the ioctl
1033 * @filp: file * from the ioctl
1034 * @cmd: cmd from ioctl
1035 * @arg: arg from ioctl
1040 * Construct a output configuration structure to return to the user.
1042 * Called by the user via ioctl.
1045 * Zero on success, errno on failure.
1047 int drm_mode_getoutput(struct drm_device *dev,
1048 void *data, struct drm_file *file_priv)
1050 struct drm_mode_get_output *out_resp = data;
1051 struct drm_output *output;
1052 struct drm_display_mode *mode;
1054 int props_count = 0;
1058 struct drm_mode_modeinfo u_mode;
1059 struct drm_mode_modeinfo __user *mode_ptr;
1060 uint32_t __user *prop_ptr;
1061 uint64_t __user *prop_values;
1063 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1065 DRM_DEBUG("output id %d:\n", out_resp->output);
1067 mutex_lock(&dev->mode_config.mutex);
1068 output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1069 if (!output || (output->id != out_resp->output)) {
1074 list_for_each_entry(mode, &output->modes, head)
1077 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1078 if (output->property_ids[i] != 0) {
1083 if (out_resp->count_modes == 0) {
1084 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1087 out_resp->output_type = output->output_type;
1088 out_resp->output_type_id = output->output_type_id;
1089 out_resp->mm_width = output->display_info.width_mm;
1090 out_resp->mm_height = output->display_info.height_mm;
1091 out_resp->subpixel = output->display_info.subpixel_order;
1092 out_resp->connection = output->status;
1094 out_resp->crtc = output->crtc->id;
1098 out_resp->crtcs = output->possible_crtcs;
1099 out_resp->clones = output->possible_clones;
1101 if ((out_resp->count_modes >= mode_count) && mode_count) {
1103 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1104 list_for_each_entry(mode, &output->modes, head) {
1105 drm_crtc_convert_to_umode(&u_mode, mode);
1106 if (copy_to_user(mode_ptr + copied,
1107 &u_mode, sizeof(u_mode))) {
1115 out_resp->count_modes = mode_count;
1117 if ((out_resp->count_props >= props_count) && props_count) {
1119 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1120 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1121 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1122 if (output->property_ids[i] != 0) {
1123 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1128 if (put_user(output->property_values[i], prop_values + copied)) {
1136 out_resp->count_props = props_count;
1139 mutex_unlock(&dev->mode_config.mutex);
1144 * drm_mode_setcrtc - set CRTC configuration
1145 * @inode: inode from the ioctl
1146 * @filp: file * from the ioctl
1147 * @cmd: cmd from ioctl
1148 * @arg: arg from ioctl
1153 * Build a new CRTC configuration based on user request.
1155 * Called by the user via ioctl.
1158 * Zero on success, errno on failure.
1160 int drm_mode_setcrtc(struct drm_device *dev,
1161 void *data, struct drm_file *file_priv)
1163 struct drm_mode_crtc *crtc_req = data;
1164 struct drm_crtc *crtc, *crtcfb;
1165 struct drm_output **output_set = NULL, *output;
1166 struct drm_framebuffer *fb = NULL;
1167 struct drm_display_mode *mode = NULL;
1168 struct drm_mode_set set;
1169 uint32_t __user *set_outputs_ptr;
1173 mutex_lock(&dev->mode_config.mutex);
1174 crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1175 if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1176 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1181 if (crtc_req->mode_valid) {
1182 /* If we have a mode we need a framebuffer. */
1183 /* If we pass -1, set the mode with the currently bound fb */
1184 if (crtc_req->fb_id == -1) {
1185 list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1186 if (crtcfb == crtc) {
1187 DRM_DEBUG("Using current fb for setmode\n");
1192 fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1193 if (!fb || (fb->id != crtc_req->fb_id)) {
1194 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1200 mode = drm_mode_create(dev);
1201 drm_crtc_convert_umode(mode, &crtc_req->mode);
1202 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1205 if (crtc_req->count_outputs == 0 && mode) {
1206 DRM_DEBUG("Count outputs is 0 but mode set\n");
1211 if (crtc_req->count_outputs > 0 && !mode && !fb) {
1212 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1217 if (crtc_req->count_outputs > 0) {
1219 /* Maybe we should check that count_outputs is a sensible value. */
1220 output_set = kmalloc(crtc_req->count_outputs *
1221 sizeof(struct drm_output *), GFP_KERNEL);
1227 for (i = 0; i < crtc_req->count_outputs; i++) {
1228 set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1229 if (get_user(out_id, &set_outputs_ptr[i])) {
1234 output = idr_find(&dev->mode_config.crtc_idr, out_id);
1235 if (!output || (out_id != output->id)) {
1236 DRM_DEBUG("Output id %d unknown\n", out_id);
1241 output_set[i] = output;
1246 set.x = crtc_req->x;
1247 set.y = crtc_req->y;
1249 set.outputs = output_set;
1250 set.num_outputs = crtc_req->count_outputs;
1252 ret = crtc->funcs->set_config(&set);
1256 mutex_unlock(&dev->mode_config.mutex);
1260 int drm_mode_cursor_ioctl(struct drm_device *dev,
1261 void *data, struct drm_file *file_priv)
1263 struct drm_mode_cursor *req = data;
1264 struct drm_crtc *crtc;
1265 struct drm_buffer_object *bo = NULL; /* must be set */
1271 DRM_ERROR("no operation set\n");
1275 mutex_lock(&dev->mode_config.mutex);
1276 crtc = idr_find(&dev->mode_config.crtc_idr, req->crtc);
1277 if (!crtc || (crtc->id != req->crtc)) {
1278 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1283 if (req->flags & DRM_MODE_CURSOR_BO) {
1284 /* Turn of the cursor if handle is 0 */
1286 ret = drm_get_buffer_object(dev, &bo, req->handle);
1289 DRM_ERROR("invalid buffer id\n");
1294 if (crtc->funcs->cursor_set) {
1295 ret = crtc->funcs->cursor_set(crtc, bo, req->width, req->height);
1297 DRM_ERROR("crtc does not support cursor\n");
1303 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1304 if (crtc->funcs->cursor_move) {
1305 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1307 DRM_ERROR("crtc does not support cursor\n");
1313 mutex_unlock(&dev->mode_config.mutex);
1318 * drm_mode_addfb - add an FB to the graphics configuration
1319 * @inode: inode from the ioctl
1320 * @filp: file * from the ioctl
1321 * @cmd: cmd from ioctl
1322 * @arg: arg from ioctl
1325 * Takes mode config lock.
1327 * Add a new FB to the specified CRTC, given a user request.
1329 * Called by the user via ioctl.
1332 * Zero on success, errno on failure.
1334 int drm_mode_addfb(struct drm_device *dev,
1335 void *data, struct drm_file *file_priv)
1337 struct drm_mode_fb_cmd *r = data;
1338 struct drm_mode_config *config = &dev->mode_config;
1339 struct drm_framebuffer *fb;
1340 struct drm_buffer_object *bo;
1343 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1344 DRM_ERROR("mode new framebuffer width not within limits\n");
1347 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1348 DRM_ERROR("mode new framebuffer height not within limits\n");
1352 mutex_lock(&dev->mode_config.mutex);
1353 /* TODO check limits are okay */
1354 ret = drm_get_buffer_object(dev, &bo, r->handle);
1356 DRM_ERROR("BO handle not valid\n");
1361 /* TODO check buffer is sufficently large */
1362 /* TODO setup destructor callback */
1364 fb = drm_framebuffer_create(dev);
1366 DRM_ERROR("could not create framebuffer\n");
1371 fb->width = r->width;
1372 fb->height = r->height;
1373 fb->pitch = r->pitch;
1374 fb->bits_per_pixel = r->bpp;
1375 fb->depth = r->depth;
1378 r->buffer_id = fb->id;
1380 list_add(&fb->filp_head, &file_priv->fbs);
1383 mutex_unlock(&dev->mode_config.mutex);
1388 * drm_mode_rmfb - remove an FB from the configuration
1389 * @inode: inode from the ioctl
1390 * @filp: file * from the ioctl
1391 * @cmd: cmd from ioctl
1392 * @arg: arg from ioctl
1395 * Takes mode config lock.
1397 * Remove the FB specified by the user.
1399 * Called by the user via ioctl.
1402 * Zero on success, errno on failure.
1404 int drm_mode_rmfb(struct drm_device *dev,
1405 void *data, struct drm_file *file_priv)
1407 struct drm_framebuffer *fb = NULL;
1408 struct drm_framebuffer *fbl = NULL;
1409 uint32_t *id = data;
1413 mutex_lock(&dev->mode_config.mutex);
1414 fb = idr_find(&dev->mode_config.crtc_idr, *id);
1415 /* TODO check that we realy get a framebuffer back. */
1416 if (!fb || (*id != fb->id)) {
1417 DRM_ERROR("mode invalid framebuffer id\n");
1422 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1427 DRM_ERROR("tried to remove a fb that we didn't own\n");
1432 /* TODO release all crtc connected to the framebuffer */
1433 /* TODO unhock the destructor from the buffer object */
1435 if (fb->bo->type == drm_bo_type_kernel)
1436 DRM_ERROR("the bo type should not be of kernel type\n");
1438 list_del(&fb->filp_head);
1439 drm_framebuffer_destroy(fb);
1442 mutex_unlock(&dev->mode_config.mutex);
1447 * drm_mode_getfb - get FB info
1448 * @inode: inode from the ioctl
1449 * @filp: file * from the ioctl
1450 * @cmd: cmd from ioctl
1451 * @arg: arg from ioctl
1456 * Lookup the FB given its ID and return info about it.
1458 * Called by the user via ioctl.
1461 * Zero on success, errno on failure.
1463 int drm_mode_getfb(struct drm_device *dev,
1464 void *data, struct drm_file *file_priv)
1466 struct drm_mode_fb_cmd *r = data;
1467 struct drm_framebuffer *fb;
1470 mutex_lock(&dev->mode_config.mutex);
1471 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1472 if (!fb || (r->buffer_id != fb->id)) {
1473 DRM_ERROR("invalid framebuffer id\n");
1478 r->height = fb->height;
1479 r->width = fb->width;
1480 r->depth = fb->depth;
1481 r->bpp = fb->bits_per_pixel;
1482 r->handle = fb->bo->base.hash.key;
1483 r->pitch = fb->pitch;
1486 mutex_unlock(&dev->mode_config.mutex);
1491 * drm_fb_release - remove and free the FBs on this file
1492 * @filp: file * from the ioctl
1495 * Takes mode config lock.
1497 * Destroy all the FBs associated with @filp.
1499 * Called by the user via ioctl.
1502 * Zero on success, errno on failure.
1504 void drm_fb_release(struct file *filp)
1506 struct drm_file *priv = filp->private_data;
1507 struct drm_device *dev = priv->minor->dev;
1508 struct drm_framebuffer *fb, *tfb;
1510 mutex_lock(&dev->mode_config.mutex);
1511 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1512 list_del(&fb->filp_head);
1513 if (fb->bo->type == drm_bo_type_kernel)
1514 DRM_ERROR("the bo type should not be of kernel_type, the kernel will probably explode, why Dave\n");
1516 drm_framebuffer_destroy(fb);
1518 mutex_unlock(&dev->mode_config.mutex);
1525 static int drm_mode_attachmode(struct drm_device *dev,
1526 struct drm_output *output,
1527 struct drm_display_mode *mode)
1531 list_add_tail(&mode->head, &output->user_modes);
1535 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1536 struct drm_display_mode *mode)
1538 struct drm_output *output;
1540 struct drm_display_mode *dup_mode;
1542 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1543 if (output->crtc == crtc) {
1545 dup_mode = drm_mode_duplicate(dev, mode);
1548 ret = drm_mode_attachmode(dev, output, dup_mode);
1556 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1558 static int drm_mode_detachmode(struct drm_device *dev,
1559 struct drm_output *output,
1560 struct drm_display_mode *mode)
1564 struct drm_display_mode *match_mode, *t;
1566 list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1567 if (drm_mode_equal(match_mode, mode)) {
1568 list_del(&match_mode->head);
1569 drm_mode_destroy(dev, match_mode);
1581 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1583 struct drm_output *output;
1585 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1586 drm_mode_detachmode(dev, output, mode);
1590 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1593 * drm_fb_attachmode - Attach a user mode to an output
1594 * @inode: inode from the ioctl
1595 * @filp: file * from the ioctl
1596 * @cmd: cmd from ioctl
1597 * @arg: arg from ioctl
1599 * This attaches a user specified mode to an output.
1600 * Called by the user via ioctl.
1603 * Zero on success, errno on failure.
1605 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1606 void *data, struct drm_file *file_priv)
1608 struct drm_mode_mode_cmd *mode_cmd = data;
1609 struct drm_output *output;
1610 struct drm_display_mode *mode;
1611 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1614 mutex_lock(&dev->mode_config.mutex);
1616 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1617 if (!output || (output->id != mode_cmd->output_id)) {
1622 mode = drm_mode_create(dev);
1628 drm_crtc_convert_umode(mode, umode);
1630 ret = drm_mode_attachmode(dev, output, mode);
1632 mutex_unlock(&dev->mode_config.mutex);
1638 * drm_fb_detachmode - Detach a user specified mode from an output
1639 * @inode: inode from the ioctl
1640 * @filp: file * from the ioctl
1641 * @cmd: cmd from ioctl
1642 * @arg: arg from ioctl
1644 * Called by the user via ioctl.
1647 * Zero on success, errno on failure.
1649 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1650 void *data, struct drm_file *file_priv)
1652 struct drm_mode_mode_cmd *mode_cmd = data;
1653 struct drm_output *output;
1654 struct drm_display_mode mode;
1655 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1658 mutex_lock(&dev->mode_config.mutex);
1660 output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1661 if (!output || (output->id != mode_cmd->output_id)) {
1666 drm_crtc_convert_umode(&mode, umode);
1667 ret = drm_mode_detachmode(dev, output, &mode);
1669 mutex_unlock(&dev->mode_config.mutex);
1673 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1674 const char *name, int num_values)
1676 struct drm_property *property = NULL;
1678 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1683 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1684 if (!property->values)
1688 property->id = drm_idr_get(dev, property);
1689 property->flags = flags;
1690 property->num_values = num_values;
1691 INIT_LIST_HEAD(&property->enum_blob_list);
1694 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1696 list_add_tail(&property->head, &dev->mode_config.property_list);
1702 EXPORT_SYMBOL(drm_property_create);
1704 int drm_property_add_enum(struct drm_property *property, int index,
1705 uint64_t value, const char *name)
1707 struct drm_property_enum *prop_enum;
1709 if (!(property->flags & DRM_MODE_PROP_ENUM))
1712 if (!list_empty(&property->enum_blob_list)) {
1713 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1714 if (prop_enum->value == value) {
1715 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1716 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1722 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1726 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1727 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1728 prop_enum->value = value;
1730 property->values[index] = value;
1731 list_add_tail(&prop_enum->head, &property->enum_blob_list);
1734 EXPORT_SYMBOL(drm_property_add_enum);
1736 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1738 struct drm_property_enum *prop_enum, *pt;
1740 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1741 list_del(&prop_enum->head);
1745 if (property->num_values)
1746 kfree(property->values);
1747 drm_idr_put(dev, property->id);
1748 list_del(&property->head);
1751 EXPORT_SYMBOL(drm_property_destroy);
1753 int drm_output_attach_property(struct drm_output *output,
1754 struct drm_property *property, uint64_t init_val)
1758 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1759 if (output->property_ids[i] == 0) {
1760 output->property_ids[i] = property->id;
1761 output->property_values[i] = init_val;
1766 if (i == DRM_OUTPUT_MAX_PROPERTY)
1770 EXPORT_SYMBOL(drm_output_attach_property);
1772 int drm_output_property_set_value(struct drm_output *output,
1773 struct drm_property *property, uint64_t value)
1777 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1778 if (output->property_ids[i] == property->id) {
1779 output->property_values[i] = value;
1784 if (i == DRM_OUTPUT_MAX_PROPERTY)
1788 EXPORT_SYMBOL(drm_output_property_set_value);
1790 int drm_output_property_get_value(struct drm_output *output,
1791 struct drm_property *property, uint64_t *val)
1795 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1796 if (output->property_ids[i] == property->id) {
1797 *val = output->property_values[i];
1802 if (i == DRM_OUTPUT_MAX_PROPERTY)
1806 EXPORT_SYMBOL(drm_output_property_get_value);
1808 int drm_mode_getproperty_ioctl(struct drm_device *dev,
1809 void *data, struct drm_file *file_priv)
1811 struct drm_mode_get_property *out_resp = data;
1812 struct drm_property *property;
1815 int value_count = 0;
1818 struct drm_property_enum *prop_enum;
1819 struct drm_mode_property_enum __user *enum_ptr;
1820 struct drm_property_blob *prop_blob;
1821 uint32_t *blob_id_ptr;
1822 uint64_t __user *values_ptr;
1823 uint32_t __user *blob_length_ptr;
1825 mutex_lock(&dev->mode_config.mutex);
1826 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
1827 if (!property || (property->id != out_resp->prop_id)) {
1832 if (property->flags & DRM_MODE_PROP_ENUM) {
1833 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
1835 } else if (property->flags & DRM_MODE_PROP_BLOB) {
1836 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
1840 value_count = property->num_values;
1842 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
1843 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
1844 out_resp->flags = property->flags;
1846 if ((out_resp->count_values >= value_count) && value_count) {
1847 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
1848 for (i = 0; i < value_count; i++) {
1849 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
1855 out_resp->count_values = value_count;
1857 if (property->flags & DRM_MODE_PROP_ENUM) {
1859 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
1861 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
1862 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1864 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
1869 if (copy_to_user(&enum_ptr[copied].name,
1870 &prop_enum->name, DRM_PROP_NAME_LEN)) {
1877 out_resp->count_enum_blobs = enum_count;
1880 if (property->flags & DRM_MODE_PROP_BLOB) {
1881 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
1883 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
1884 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
1886 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
1887 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
1892 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
1900 out_resp->count_enum_blobs = enum_count;
1903 mutex_unlock(&dev->mode_config.mutex);
1907 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
1910 struct drm_property_blob *blob;
1912 if (!length || !data)
1915 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
1919 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
1920 blob->length = length;
1922 memcpy(blob->data, data, length);
1924 blob->id = drm_idr_get(dev, blob);
1926 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
1930 static void drm_property_destroy_blob(struct drm_device *dev,
1931 struct drm_property_blob *blob)
1933 drm_idr_put(dev, blob->id);
1934 list_del(&blob->head);
1938 int drm_mode_getblob_ioctl(struct drm_device *dev,
1939 void *data, struct drm_file *file_priv)
1941 struct drm_mode_get_blob *out_resp = data;
1942 struct drm_property_blob *blob;
1946 mutex_lock(&dev->mode_config.mutex);
1948 blob = idr_find(&dev->mode_config.crtc_idr, out_resp->blob_id);
1949 if (!blob || (blob->id != out_resp->blob_id)) {
1954 if (out_resp->length == blob->length) {
1955 blob_ptr = (void *)(unsigned long)out_resp->data;
1956 if (copy_to_user(blob_ptr, blob->data, blob->length)){
1961 out_resp->length = blob->length;
1964 mutex_unlock(&dev->mode_config.mutex);
1968 int drm_mode_output_update_edid_property(struct drm_output *output, struct edid *edid)
1970 struct drm_device *dev = output->dev;
1972 if (output->edid_blob_ptr)
1973 drm_property_destroy_blob(dev, output->edid_blob_ptr);
1975 output->edid_blob_ptr = drm_property_create_blob(output->dev, 128, edid);
1977 ret = drm_output_property_set_value(output, dev->mode_config.edid_property, output->edid_blob_ptr->id);
1980 EXPORT_SYMBOL(drm_mode_output_update_edid_property);
1982 int drm_mode_output_property_set_ioctl(struct drm_device *dev,
1983 void *data, struct drm_file *file_priv)
1985 struct drm_mode_output_set_property *out_resp = data;
1986 struct drm_property *property;
1987 struct drm_output *output;
1991 mutex_lock(&dev->mode_config.mutex);
1992 output = idr_find(&dev->mode_config.crtc_idr, out_resp->output_id);
1993 if (!output || (output->id != out_resp->output_id)) {
1997 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1998 if (output->property_ids[i] == out_resp->prop_id)
2002 if (i == DRM_OUTPUT_MAX_PROPERTY) {
2006 property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
2007 if (!property || (property->id != out_resp->prop_id)) {
2011 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2014 if (property->flags & DRM_MODE_PROP_RANGE) {
2015 if (out_resp->value < property->values[0])
2018 if (out_resp->value > property->values[1])
2022 for (i = 0; i < property->num_values; i++) {
2023 if (property->values[i] == out_resp->value) {
2033 if (output->funcs->set_property)
2034 ret = output->funcs->set_property(output, property, out_resp->value);
2037 mutex_unlock(&dev->mode_config.mutex);
2042 int drm_mode_replacefb(struct drm_device *dev,
2043 void *data, struct drm_file *file_priv)
2045 struct drm_mode_fb_cmd *r = data;
2046 struct drm_framebuffer *fb;
2047 struct drm_buffer_object *bo;
2049 struct drm_framebuffer *fbl = NULL;
2051 /* right replace the current bo attached to this fb with a new bo */
2052 mutex_lock(&dev->mode_config.mutex);
2053 ret = drm_get_buffer_object(dev, &bo, r->handle);
2059 fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
2060 if (!fb || (r->buffer_id != fb->id)) {
2065 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2070 DRM_ERROR("tried to replace an fb we didn't own\n");
2075 if (fb->bo->type == drm_bo_type_kernel)
2076 DRM_ERROR("the bo should not be a kernel bo\n");
2078 fb->width = r->width;
2079 fb->height = r->height;
2080 fb->pitch = r->pitch;
2081 fb->bits_per_pixel = r->bpp;
2082 fb->depth = r->depth;
2085 if (dev->mode_config.funcs->resize_fb)
2086 dev->mode_config.funcs->resize_fb(dev, fb);
2090 /* find all crtcs connected to this fb */
2091 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2092 if (crtc->fb->id == r->buffer_id) {
2093 crtc->funcs->mode_set_base(crtc, crtc->x, crtc->y);
2098 mutex_unlock(&dev->mode_config.mutex);