2 * Copyright (c) 2006-2008 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>
33 #include <linux/slab.h>
34 #include <linux/export.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_edid.h>
38 #include <drm/drm_fourcc.h>
40 /* Avoid boilerplate. I'm tired of typing. */
41 #define DRM_ENUM_NAME_FN(fnname, list) \
42 char *fnname(int val) \
45 for (i = 0; i < ARRAY_SIZE(list); i++) { \
46 if (list[i].type == val) \
47 return list[i].name; \
55 static struct drm_prop_enum_list drm_dpms_enum_list[] =
56 { { DRM_MODE_DPMS_ON, "On" },
57 { DRM_MODE_DPMS_STANDBY, "Standby" },
58 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
59 { DRM_MODE_DPMS_OFF, "Off" }
62 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
67 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
69 { DRM_MODE_SCALE_NONE, "None" },
70 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
71 { DRM_MODE_SCALE_CENTER, "Center" },
72 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
75 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
77 { DRM_MODE_DITHERING_OFF, "Off" },
78 { DRM_MODE_DITHERING_ON, "On" },
79 { DRM_MODE_DITHERING_AUTO, "Automatic" },
83 * Non-global properties, but "required" for certain connectors.
85 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
87 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
88 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
89 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
92 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
94 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
96 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
97 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
98 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
101 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
102 drm_dvi_i_subconnector_enum_list)
104 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
106 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
107 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
108 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
113 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
117 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
118 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
119 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
124 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
125 drm_tv_subconnector_enum_list)
127 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
128 { DRM_MODE_DIRTY_OFF, "Off" },
129 { DRM_MODE_DIRTY_ON, "On" },
130 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
133 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
134 drm_dirty_info_enum_list)
136 struct drm_conn_prop_enum_list {
143 * Connector and encoder types.
145 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
146 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
147 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
148 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
149 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
150 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
151 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
152 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
153 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
154 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
155 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
156 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
157 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
158 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
159 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
160 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
161 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
164 static struct drm_prop_enum_list drm_encoder_enum_list[] =
165 { { DRM_MODE_ENCODER_NONE, "None" },
166 { DRM_MODE_ENCODER_DAC, "DAC" },
167 { DRM_MODE_ENCODER_TMDS, "TMDS" },
168 { DRM_MODE_ENCODER_LVDS, "LVDS" },
169 { DRM_MODE_ENCODER_TVDAC, "TV" },
170 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
173 char *drm_get_encoder_name(struct drm_encoder *encoder)
177 snprintf(buf, 32, "%s-%d",
178 drm_encoder_enum_list[encoder->encoder_type].name,
182 EXPORT_SYMBOL(drm_get_encoder_name);
184 char *drm_get_connector_name(struct drm_connector *connector)
188 snprintf(buf, 32, "%s-%d",
189 drm_connector_enum_list[connector->connector_type].name,
190 connector->connector_type_id);
193 EXPORT_SYMBOL(drm_get_connector_name);
195 char *drm_get_connector_status_name(enum drm_connector_status status)
197 if (status == connector_status_connected)
199 else if (status == connector_status_disconnected)
200 return "disconnected";
206 * drm_mode_object_get - allocate a new identifier
208 * @ptr: object pointer, used to generate unique ID
213 * Create a unique identifier based on @ptr in @dev's identifier space. Used
214 * for tracking modes, CRTCs and connectors.
217 * New unique (relative to other objects in @dev) integer identifier for the
220 static int drm_mode_object_get(struct drm_device *dev,
221 struct drm_mode_object *obj, uint32_t obj_type)
227 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
228 DRM_ERROR("Ran out memory getting a mode number\n");
232 mutex_lock(&dev->mode_config.idr_mutex);
233 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
234 mutex_unlock(&dev->mode_config.idr_mutex);
241 obj->type = obj_type;
246 * drm_mode_object_put - free an identifer
251 * Caller must hold DRM mode_config lock.
253 * Free @id from @dev's unique identifier pool.
255 static void drm_mode_object_put(struct drm_device *dev,
256 struct drm_mode_object *object)
258 mutex_lock(&dev->mode_config.idr_mutex);
259 idr_remove(&dev->mode_config.crtc_idr, object->id);
260 mutex_unlock(&dev->mode_config.idr_mutex);
263 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
264 uint32_t id, uint32_t type)
266 struct drm_mode_object *obj = NULL;
268 mutex_lock(&dev->mode_config.idr_mutex);
269 obj = idr_find(&dev->mode_config.crtc_idr, id);
270 if (!obj || (obj->type != type) || (obj->id != id))
272 mutex_unlock(&dev->mode_config.idr_mutex);
276 EXPORT_SYMBOL(drm_mode_object_find);
279 * drm_framebuffer_init - initialize a framebuffer
283 * Caller must hold mode config lock.
285 * Allocates an ID for the framebuffer's parent mode object, sets its mode
286 * functions & device file and adds it to the master fd list.
289 * Zero on success, error code on failure.
291 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
292 const struct drm_framebuffer_funcs *funcs)
296 kref_init(&fb->refcount);
298 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
304 dev->mode_config.num_fb++;
305 list_add(&fb->head, &dev->mode_config.fb_list);
309 EXPORT_SYMBOL(drm_framebuffer_init);
311 static void drm_framebuffer_free(struct kref *kref)
313 struct drm_framebuffer *fb =
314 container_of(kref, struct drm_framebuffer, refcount);
315 fb->funcs->destroy(fb);
319 * drm_framebuffer_unreference - unref a framebuffer
322 * Caller must hold mode config lock.
324 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
326 struct drm_device *dev = fb->dev;
327 DRM_DEBUG("FB ID: %d\n", fb->base.id);
328 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
329 kref_put(&fb->refcount, drm_framebuffer_free);
331 EXPORT_SYMBOL(drm_framebuffer_unreference);
334 * drm_framebuffer_reference - incr the fb refcnt
336 void drm_framebuffer_reference(struct drm_framebuffer *fb)
338 DRM_DEBUG("FB ID: %d\n", fb->base.id);
339 kref_get(&fb->refcount);
341 EXPORT_SYMBOL(drm_framebuffer_reference);
344 * drm_framebuffer_cleanup - remove a framebuffer object
345 * @fb: framebuffer to remove
348 * Caller must hold mode config lock.
350 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
351 * it, setting it to NULL.
353 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
355 struct drm_device *dev = fb->dev;
357 * This could be moved to drm_framebuffer_remove(), but for
358 * debugging is nice to keep around the list of fb's that are
359 * no longer associated w/ a drm_file but are not unreferenced
360 * yet. (i915 and omapdrm have debugfs files which will show
363 drm_mode_object_put(dev, &fb->base);
365 dev->mode_config.num_fb--;
367 EXPORT_SYMBOL(drm_framebuffer_cleanup);
370 * drm_framebuffer_remove - remove and unreference a framebuffer object
371 * @fb: framebuffer to remove
374 * Caller must hold mode config lock.
376 * Scans all the CRTCs and planes in @dev's mode_config. If they're
377 * using @fb, removes it, setting it to NULL.
379 void drm_framebuffer_remove(struct drm_framebuffer *fb)
381 struct drm_device *dev = fb->dev;
382 struct drm_crtc *crtc;
383 struct drm_plane *plane;
384 struct drm_mode_set set;
387 /* remove from any CRTC */
388 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
389 if (crtc->fb == fb) {
390 /* should turn off the crtc */
391 memset(&set, 0, sizeof(struct drm_mode_set));
394 ret = crtc->funcs->set_config(&set);
396 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
400 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
401 if (plane->fb == fb) {
402 /* should turn off the crtc */
403 ret = plane->funcs->disable_plane(plane);
405 DRM_ERROR("failed to disable plane with busy fb\n");
406 /* disconnect the plane from the fb and crtc: */
412 list_del(&fb->filp_head);
414 drm_framebuffer_unreference(fb);
416 EXPORT_SYMBOL(drm_framebuffer_remove);
419 * drm_crtc_init - Initialise a new CRTC object
421 * @crtc: CRTC object to init
422 * @funcs: callbacks for the new CRTC
425 * Takes mode_config lock.
427 * Inits a new object created as base part of an driver crtc object.
430 * Zero on success, error code on failure.
432 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
433 const struct drm_crtc_funcs *funcs)
439 crtc->invert_dimensions = false;
441 mutex_lock(&dev->mode_config.mutex);
443 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
447 crtc->base.properties = &crtc->properties;
449 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
450 dev->mode_config.num_crtc++;
453 mutex_unlock(&dev->mode_config.mutex);
457 EXPORT_SYMBOL(drm_crtc_init);
460 * drm_crtc_cleanup - Cleans up the core crtc usage.
461 * @crtc: CRTC to cleanup
464 * Caller must hold mode config lock.
466 * Cleanup @crtc. Removes from drm modesetting space
467 * does NOT free object, caller does that.
469 void drm_crtc_cleanup(struct drm_crtc *crtc)
471 struct drm_device *dev = crtc->dev;
473 kfree(crtc->gamma_store);
474 crtc->gamma_store = NULL;
476 drm_mode_object_put(dev, &crtc->base);
477 list_del(&crtc->head);
478 dev->mode_config.num_crtc--;
480 EXPORT_SYMBOL(drm_crtc_cleanup);
483 * drm_mode_probed_add - add a mode to a connector's probed mode list
484 * @connector: connector the new mode
488 * Caller must hold mode config lock.
490 * Add @mode to @connector's mode list for later use.
492 void drm_mode_probed_add(struct drm_connector *connector,
493 struct drm_display_mode *mode)
495 list_add(&mode->head, &connector->probed_modes);
497 EXPORT_SYMBOL(drm_mode_probed_add);
500 * drm_mode_remove - remove and free a mode
501 * @connector: connector list to modify
502 * @mode: mode to remove
505 * Caller must hold mode config lock.
507 * Remove @mode from @connector's mode list, then free it.
509 void drm_mode_remove(struct drm_connector *connector,
510 struct drm_display_mode *mode)
512 list_del(&mode->head);
513 drm_mode_destroy(connector->dev, mode);
515 EXPORT_SYMBOL(drm_mode_remove);
518 * drm_connector_init - Init a preallocated connector
520 * @connector: the connector to init
521 * @funcs: callbacks for this connector
522 * @name: user visible name of the connector
525 * Takes mode config lock.
527 * Initialises a preallocated connector. Connectors should be
528 * subclassed as part of driver connector objects.
531 * Zero on success, error code on failure.
533 int drm_connector_init(struct drm_device *dev,
534 struct drm_connector *connector,
535 const struct drm_connector_funcs *funcs,
540 mutex_lock(&dev->mode_config.mutex);
542 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
546 connector->base.properties = &connector->properties;
547 connector->dev = dev;
548 connector->funcs = funcs;
549 connector->connector_type = connector_type;
550 connector->connector_type_id =
551 ++drm_connector_enum_list[connector_type].count; /* TODO */
552 INIT_LIST_HEAD(&connector->user_modes);
553 INIT_LIST_HEAD(&connector->probed_modes);
554 INIT_LIST_HEAD(&connector->modes);
555 connector->edid_blob_ptr = NULL;
556 connector->status = connector_status_unknown;
558 list_add_tail(&connector->head, &dev->mode_config.connector_list);
559 dev->mode_config.num_connector++;
561 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
562 drm_object_attach_property(&connector->base,
563 dev->mode_config.edid_property,
566 drm_object_attach_property(&connector->base,
567 dev->mode_config.dpms_property, 0);
570 mutex_unlock(&dev->mode_config.mutex);
574 EXPORT_SYMBOL(drm_connector_init);
577 * drm_connector_cleanup - cleans up an initialised connector
578 * @connector: connector to cleanup
581 * Takes mode config lock.
583 * Cleans up the connector but doesn't free the object.
585 void drm_connector_cleanup(struct drm_connector *connector)
587 struct drm_device *dev = connector->dev;
588 struct drm_display_mode *mode, *t;
590 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
591 drm_mode_remove(connector, mode);
593 list_for_each_entry_safe(mode, t, &connector->modes, head)
594 drm_mode_remove(connector, mode);
596 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
597 drm_mode_remove(connector, mode);
599 mutex_lock(&dev->mode_config.mutex);
600 drm_mode_object_put(dev, &connector->base);
601 list_del(&connector->head);
602 dev->mode_config.num_connector--;
603 mutex_unlock(&dev->mode_config.mutex);
605 EXPORT_SYMBOL(drm_connector_cleanup);
607 void drm_connector_unplug_all(struct drm_device *dev)
609 struct drm_connector *connector;
611 /* taking the mode config mutex ends up in a clash with sysfs */
612 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
613 drm_sysfs_connector_remove(connector);
616 EXPORT_SYMBOL(drm_connector_unplug_all);
618 int drm_encoder_init(struct drm_device *dev,
619 struct drm_encoder *encoder,
620 const struct drm_encoder_funcs *funcs,
625 mutex_lock(&dev->mode_config.mutex);
627 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
632 encoder->encoder_type = encoder_type;
633 encoder->funcs = funcs;
635 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
636 dev->mode_config.num_encoder++;
639 mutex_unlock(&dev->mode_config.mutex);
643 EXPORT_SYMBOL(drm_encoder_init);
645 void drm_encoder_cleanup(struct drm_encoder *encoder)
647 struct drm_device *dev = encoder->dev;
648 mutex_lock(&dev->mode_config.mutex);
649 drm_mode_object_put(dev, &encoder->base);
650 list_del(&encoder->head);
651 dev->mode_config.num_encoder--;
652 mutex_unlock(&dev->mode_config.mutex);
654 EXPORT_SYMBOL(drm_encoder_cleanup);
656 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
657 unsigned long possible_crtcs,
658 const struct drm_plane_funcs *funcs,
659 const uint32_t *formats, uint32_t format_count,
664 mutex_lock(&dev->mode_config.mutex);
666 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
670 plane->base.properties = &plane->properties;
672 plane->funcs = funcs;
673 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
675 if (!plane->format_types) {
676 DRM_DEBUG_KMS("out of memory when allocating plane\n");
677 drm_mode_object_put(dev, &plane->base);
682 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
683 plane->format_count = format_count;
684 plane->possible_crtcs = possible_crtcs;
686 /* private planes are not exposed to userspace, but depending on
687 * display hardware, might be convenient to allow sharing programming
688 * for the scanout engine with the crtc implementation.
691 list_add_tail(&plane->head, &dev->mode_config.plane_list);
692 dev->mode_config.num_plane++;
694 INIT_LIST_HEAD(&plane->head);
698 mutex_unlock(&dev->mode_config.mutex);
702 EXPORT_SYMBOL(drm_plane_init);
704 void drm_plane_cleanup(struct drm_plane *plane)
706 struct drm_device *dev = plane->dev;
708 mutex_lock(&dev->mode_config.mutex);
709 kfree(plane->format_types);
710 drm_mode_object_put(dev, &plane->base);
711 /* if not added to a list, it must be a private plane */
712 if (!list_empty(&plane->head)) {
713 list_del(&plane->head);
714 dev->mode_config.num_plane--;
716 mutex_unlock(&dev->mode_config.mutex);
718 EXPORT_SYMBOL(drm_plane_cleanup);
721 * drm_mode_create - create a new display mode
725 * Caller must hold DRM mode_config lock.
727 * Create a new drm_display_mode, give it an ID, and return it.
730 * Pointer to new mode on success, NULL on error.
732 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
734 struct drm_display_mode *nmode;
736 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
740 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
747 EXPORT_SYMBOL(drm_mode_create);
750 * drm_mode_destroy - remove a mode
752 * @mode: mode to remove
755 * Caller must hold mode config lock.
757 * Free @mode's unique identifier, then free it.
759 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
764 drm_mode_object_put(dev, &mode->base);
768 EXPORT_SYMBOL(drm_mode_destroy);
770 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
772 struct drm_property *edid;
773 struct drm_property *dpms;
776 * Standard properties (apply to all connectors)
778 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
779 DRM_MODE_PROP_IMMUTABLE,
781 dev->mode_config.edid_property = edid;
783 dpms = drm_property_create_enum(dev, 0,
784 "DPMS", drm_dpms_enum_list,
785 ARRAY_SIZE(drm_dpms_enum_list));
786 dev->mode_config.dpms_property = dpms;
792 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
795 * Called by a driver the first time a DVI-I connector is made.
797 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
799 struct drm_property *dvi_i_selector;
800 struct drm_property *dvi_i_subconnector;
802 if (dev->mode_config.dvi_i_select_subconnector_property)
806 drm_property_create_enum(dev, 0,
807 "select subconnector",
808 drm_dvi_i_select_enum_list,
809 ARRAY_SIZE(drm_dvi_i_select_enum_list));
810 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
812 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
814 drm_dvi_i_subconnector_enum_list,
815 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
816 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
820 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
823 * drm_create_tv_properties - create TV specific connector properties
825 * @num_modes: number of different TV formats (modes) supported
826 * @modes: array of pointers to strings containing name of each format
828 * Called by a driver's TV initialization routine, this function creates
829 * the TV specific connector properties for a given device. Caller is
830 * responsible for allocating a list of format names and passing them to
833 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
836 struct drm_property *tv_selector;
837 struct drm_property *tv_subconnector;
840 if (dev->mode_config.tv_select_subconnector_property)
844 * Basic connector properties
846 tv_selector = drm_property_create_enum(dev, 0,
847 "select subconnector",
848 drm_tv_select_enum_list,
849 ARRAY_SIZE(drm_tv_select_enum_list));
850 dev->mode_config.tv_select_subconnector_property = tv_selector;
853 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
855 drm_tv_subconnector_enum_list,
856 ARRAY_SIZE(drm_tv_subconnector_enum_list));
857 dev->mode_config.tv_subconnector_property = tv_subconnector;
860 * Other, TV specific properties: margins & TV modes.
862 dev->mode_config.tv_left_margin_property =
863 drm_property_create_range(dev, 0, "left margin", 0, 100);
865 dev->mode_config.tv_right_margin_property =
866 drm_property_create_range(dev, 0, "right margin", 0, 100);
868 dev->mode_config.tv_top_margin_property =
869 drm_property_create_range(dev, 0, "top margin", 0, 100);
871 dev->mode_config.tv_bottom_margin_property =
872 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
874 dev->mode_config.tv_mode_property =
875 drm_property_create(dev, DRM_MODE_PROP_ENUM,
877 for (i = 0; i < num_modes; i++)
878 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
881 dev->mode_config.tv_brightness_property =
882 drm_property_create_range(dev, 0, "brightness", 0, 100);
884 dev->mode_config.tv_contrast_property =
885 drm_property_create_range(dev, 0, "contrast", 0, 100);
887 dev->mode_config.tv_flicker_reduction_property =
888 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
890 dev->mode_config.tv_overscan_property =
891 drm_property_create_range(dev, 0, "overscan", 0, 100);
893 dev->mode_config.tv_saturation_property =
894 drm_property_create_range(dev, 0, "saturation", 0, 100);
896 dev->mode_config.tv_hue_property =
897 drm_property_create_range(dev, 0, "hue", 0, 100);
901 EXPORT_SYMBOL(drm_mode_create_tv_properties);
904 * drm_mode_create_scaling_mode_property - create scaling mode property
907 * Called by a driver the first time it's needed, must be attached to desired
910 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
912 struct drm_property *scaling_mode;
914 if (dev->mode_config.scaling_mode_property)
918 drm_property_create_enum(dev, 0, "scaling mode",
919 drm_scaling_mode_enum_list,
920 ARRAY_SIZE(drm_scaling_mode_enum_list));
922 dev->mode_config.scaling_mode_property = scaling_mode;
926 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
929 * drm_mode_create_dithering_property - create dithering property
932 * Called by a driver the first time it's needed, must be attached to desired
935 int drm_mode_create_dithering_property(struct drm_device *dev)
937 struct drm_property *dithering_mode;
939 if (dev->mode_config.dithering_mode_property)
943 drm_property_create_enum(dev, 0, "dithering",
944 drm_dithering_mode_enum_list,
945 ARRAY_SIZE(drm_dithering_mode_enum_list));
946 dev->mode_config.dithering_mode_property = dithering_mode;
950 EXPORT_SYMBOL(drm_mode_create_dithering_property);
953 * drm_mode_create_dirty_property - create dirty property
956 * Called by a driver the first time it's needed, must be attached to desired
959 int drm_mode_create_dirty_info_property(struct drm_device *dev)
961 struct drm_property *dirty_info;
963 if (dev->mode_config.dirty_info_property)
967 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
969 drm_dirty_info_enum_list,
970 ARRAY_SIZE(drm_dirty_info_enum_list));
971 dev->mode_config.dirty_info_property = dirty_info;
975 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
978 * drm_mode_config_init - initialize DRM mode_configuration structure
982 * None, should happen single threaded at init time.
984 * Initialize @dev's mode_config structure, used for tracking the graphics
985 * configuration of @dev.
987 void drm_mode_config_init(struct drm_device *dev)
989 mutex_init(&dev->mode_config.mutex);
990 mutex_init(&dev->mode_config.idr_mutex);
991 INIT_LIST_HEAD(&dev->mode_config.fb_list);
992 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
993 INIT_LIST_HEAD(&dev->mode_config.connector_list);
994 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
995 INIT_LIST_HEAD(&dev->mode_config.property_list);
996 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
997 INIT_LIST_HEAD(&dev->mode_config.plane_list);
998 idr_init(&dev->mode_config.crtc_idr);
1000 mutex_lock(&dev->mode_config.mutex);
1001 drm_mode_create_standard_connector_properties(dev);
1002 mutex_unlock(&dev->mode_config.mutex);
1004 /* Just to be sure */
1005 dev->mode_config.num_fb = 0;
1006 dev->mode_config.num_connector = 0;
1007 dev->mode_config.num_crtc = 0;
1008 dev->mode_config.num_encoder = 0;
1010 EXPORT_SYMBOL(drm_mode_config_init);
1012 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1014 uint32_t total_objects = 0;
1016 total_objects += dev->mode_config.num_crtc;
1017 total_objects += dev->mode_config.num_connector;
1018 total_objects += dev->mode_config.num_encoder;
1020 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1021 if (!group->id_list)
1024 group->num_crtcs = 0;
1025 group->num_connectors = 0;
1026 group->num_encoders = 0;
1030 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1031 struct drm_mode_group *group)
1033 struct drm_crtc *crtc;
1034 struct drm_encoder *encoder;
1035 struct drm_connector *connector;
1038 if ((ret = drm_mode_group_init(dev, group)))
1041 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1042 group->id_list[group->num_crtcs++] = crtc->base.id;
1044 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1045 group->id_list[group->num_crtcs + group->num_encoders++] =
1048 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1049 group->id_list[group->num_crtcs + group->num_encoders +
1050 group->num_connectors++] = connector->base.id;
1054 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1057 * drm_mode_config_cleanup - free up DRM mode_config info
1061 * Caller must hold mode config lock.
1063 * Free up all the connectors and CRTCs associated with this DRM device, then
1064 * free up the framebuffers and associated buffer objects.
1066 * FIXME: cleanup any dangling user buffer objects too
1068 void drm_mode_config_cleanup(struct drm_device *dev)
1070 struct drm_connector *connector, *ot;
1071 struct drm_crtc *crtc, *ct;
1072 struct drm_encoder *encoder, *enct;
1073 struct drm_framebuffer *fb, *fbt;
1074 struct drm_property *property, *pt;
1075 struct drm_plane *plane, *plt;
1077 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1079 encoder->funcs->destroy(encoder);
1082 list_for_each_entry_safe(connector, ot,
1083 &dev->mode_config.connector_list, head) {
1084 connector->funcs->destroy(connector);
1087 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1089 drm_property_destroy(dev, property);
1092 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1093 drm_framebuffer_remove(fb);
1096 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1098 plane->funcs->destroy(plane);
1101 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1102 crtc->funcs->destroy(crtc);
1105 idr_remove_all(&dev->mode_config.crtc_idr);
1106 idr_destroy(&dev->mode_config.crtc_idr);
1108 EXPORT_SYMBOL(drm_mode_config_cleanup);
1111 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1112 * @out: drm_mode_modeinfo struct to return to the user
1113 * @in: drm_display_mode to use
1118 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1121 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1122 const struct drm_display_mode *in)
1124 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1125 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1126 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1127 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1128 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1129 "timing values too large for mode info\n");
1131 out->clock = in->clock;
1132 out->hdisplay = in->hdisplay;
1133 out->hsync_start = in->hsync_start;
1134 out->hsync_end = in->hsync_end;
1135 out->htotal = in->htotal;
1136 out->hskew = in->hskew;
1137 out->vdisplay = in->vdisplay;
1138 out->vsync_start = in->vsync_start;
1139 out->vsync_end = in->vsync_end;
1140 out->vtotal = in->vtotal;
1141 out->vscan = in->vscan;
1142 out->vrefresh = in->vrefresh;
1143 out->flags = in->flags;
1144 out->type = in->type;
1145 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1146 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1150 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1151 * @out: drm_display_mode to return to the user
1152 * @in: drm_mode_modeinfo to use
1157 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1161 * Zero on success, errno on failure.
1163 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1164 const struct drm_mode_modeinfo *in)
1166 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1169 out->clock = in->clock;
1170 out->hdisplay = in->hdisplay;
1171 out->hsync_start = in->hsync_start;
1172 out->hsync_end = in->hsync_end;
1173 out->htotal = in->htotal;
1174 out->hskew = in->hskew;
1175 out->vdisplay = in->vdisplay;
1176 out->vsync_start = in->vsync_start;
1177 out->vsync_end = in->vsync_end;
1178 out->vtotal = in->vtotal;
1179 out->vscan = in->vscan;
1180 out->vrefresh = in->vrefresh;
1181 out->flags = in->flags;
1182 out->type = in->type;
1183 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1184 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1190 * drm_mode_getresources - get graphics configuration
1191 * @inode: inode from the ioctl
1192 * @filp: file * from the ioctl
1193 * @cmd: cmd from ioctl
1194 * @arg: arg from ioctl
1197 * Takes mode config lock.
1199 * Construct a set of configuration description structures and return
1200 * them to the user, including CRTC, connector and framebuffer configuration.
1202 * Called by the user via ioctl.
1205 * Zero on success, errno on failure.
1207 int drm_mode_getresources(struct drm_device *dev, void *data,
1208 struct drm_file *file_priv)
1210 struct drm_mode_card_res *card_res = data;
1211 struct list_head *lh;
1212 struct drm_framebuffer *fb;
1213 struct drm_connector *connector;
1214 struct drm_crtc *crtc;
1215 struct drm_encoder *encoder;
1217 int connector_count = 0;
1220 int encoder_count = 0;
1222 uint32_t __user *fb_id;
1223 uint32_t __user *crtc_id;
1224 uint32_t __user *connector_id;
1225 uint32_t __user *encoder_id;
1226 struct drm_mode_group *mode_group;
1228 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1231 mutex_lock(&dev->mode_config.mutex);
1234 * For the non-control nodes we need to limit the list of resources
1235 * by IDs in the group list for this node
1237 list_for_each(lh, &file_priv->fbs)
1240 mode_group = &file_priv->master->minor->mode_group;
1241 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1243 list_for_each(lh, &dev->mode_config.crtc_list)
1246 list_for_each(lh, &dev->mode_config.connector_list)
1249 list_for_each(lh, &dev->mode_config.encoder_list)
1253 crtc_count = mode_group->num_crtcs;
1254 connector_count = mode_group->num_connectors;
1255 encoder_count = mode_group->num_encoders;
1258 card_res->max_height = dev->mode_config.max_height;
1259 card_res->min_height = dev->mode_config.min_height;
1260 card_res->max_width = dev->mode_config.max_width;
1261 card_res->min_width = dev->mode_config.min_width;
1263 /* handle this in 4 parts */
1265 if (card_res->count_fbs >= fb_count) {
1267 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1268 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1269 if (put_user(fb->base.id, fb_id + copied)) {
1276 card_res->count_fbs = fb_count;
1279 if (card_res->count_crtcs >= crtc_count) {
1281 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1282 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1283 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1285 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1286 if (put_user(crtc->base.id, crtc_id + copied)) {
1293 for (i = 0; i < mode_group->num_crtcs; i++) {
1294 if (put_user(mode_group->id_list[i],
1295 crtc_id + copied)) {
1303 card_res->count_crtcs = crtc_count;
1306 if (card_res->count_encoders >= encoder_count) {
1308 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1309 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1310 list_for_each_entry(encoder,
1311 &dev->mode_config.encoder_list,
1313 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1314 drm_get_encoder_name(encoder));
1315 if (put_user(encoder->base.id, encoder_id +
1323 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1324 if (put_user(mode_group->id_list[i],
1325 encoder_id + copied)) {
1334 card_res->count_encoders = encoder_count;
1337 if (card_res->count_connectors >= connector_count) {
1339 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1340 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1341 list_for_each_entry(connector,
1342 &dev->mode_config.connector_list,
1344 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1346 drm_get_connector_name(connector));
1347 if (put_user(connector->base.id,
1348 connector_id + copied)) {
1355 int start = mode_group->num_crtcs +
1356 mode_group->num_encoders;
1357 for (i = start; i < start + mode_group->num_connectors; i++) {
1358 if (put_user(mode_group->id_list[i],
1359 connector_id + copied)) {
1367 card_res->count_connectors = connector_count;
1369 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1370 card_res->count_connectors, card_res->count_encoders);
1373 mutex_unlock(&dev->mode_config.mutex);
1378 * drm_mode_getcrtc - get CRTC configuration
1379 * @inode: inode from the ioctl
1380 * @filp: file * from the ioctl
1381 * @cmd: cmd from ioctl
1382 * @arg: arg from ioctl
1385 * Takes mode config lock.
1387 * Construct a CRTC configuration structure to return to the user.
1389 * Called by the user via ioctl.
1392 * Zero on success, errno on failure.
1394 int drm_mode_getcrtc(struct drm_device *dev,
1395 void *data, struct drm_file *file_priv)
1397 struct drm_mode_crtc *crtc_resp = data;
1398 struct drm_crtc *crtc;
1399 struct drm_mode_object *obj;
1402 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1405 mutex_lock(&dev->mode_config.mutex);
1407 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1408 DRM_MODE_OBJECT_CRTC);
1413 crtc = obj_to_crtc(obj);
1415 crtc_resp->x = crtc->x;
1416 crtc_resp->y = crtc->y;
1417 crtc_resp->gamma_size = crtc->gamma_size;
1419 crtc_resp->fb_id = crtc->fb->base.id;
1421 crtc_resp->fb_id = 0;
1423 if (crtc->enabled) {
1425 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1426 crtc_resp->mode_valid = 1;
1429 crtc_resp->mode_valid = 0;
1433 mutex_unlock(&dev->mode_config.mutex);
1438 * drm_mode_getconnector - get connector configuration
1439 * @inode: inode from the ioctl
1440 * @filp: file * from the ioctl
1441 * @cmd: cmd from ioctl
1442 * @arg: arg from ioctl
1445 * Takes mode config lock.
1447 * Construct a connector configuration structure to return to the user.
1449 * Called by the user via ioctl.
1452 * Zero on success, errno on failure.
1454 int drm_mode_getconnector(struct drm_device *dev, void *data,
1455 struct drm_file *file_priv)
1457 struct drm_mode_get_connector *out_resp = data;
1458 struct drm_mode_object *obj;
1459 struct drm_connector *connector;
1460 struct drm_display_mode *mode;
1462 int props_count = 0;
1463 int encoders_count = 0;
1467 struct drm_mode_modeinfo u_mode;
1468 struct drm_mode_modeinfo __user *mode_ptr;
1469 uint32_t __user *prop_ptr;
1470 uint64_t __user *prop_values;
1471 uint32_t __user *encoder_ptr;
1473 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1476 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1478 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1480 mutex_lock(&dev->mode_config.mutex);
1482 obj = drm_mode_object_find(dev, out_resp->connector_id,
1483 DRM_MODE_OBJECT_CONNECTOR);
1488 connector = obj_to_connector(obj);
1490 props_count = connector->properties.count;
1492 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1493 if (connector->encoder_ids[i] != 0) {
1498 if (out_resp->count_modes == 0) {
1499 connector->funcs->fill_modes(connector,
1500 dev->mode_config.max_width,
1501 dev->mode_config.max_height);
1504 /* delayed so we get modes regardless of pre-fill_modes state */
1505 list_for_each_entry(mode, &connector->modes, head)
1508 out_resp->connector_id = connector->base.id;
1509 out_resp->connector_type = connector->connector_type;
1510 out_resp->connector_type_id = connector->connector_type_id;
1511 out_resp->mm_width = connector->display_info.width_mm;
1512 out_resp->mm_height = connector->display_info.height_mm;
1513 out_resp->subpixel = connector->display_info.subpixel_order;
1514 out_resp->connection = connector->status;
1515 if (connector->encoder)
1516 out_resp->encoder_id = connector->encoder->base.id;
1518 out_resp->encoder_id = 0;
1521 * This ioctl is called twice, once to determine how much space is
1522 * needed, and the 2nd time to fill it.
1524 if ((out_resp->count_modes >= mode_count) && mode_count) {
1526 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1527 list_for_each_entry(mode, &connector->modes, head) {
1528 drm_crtc_convert_to_umode(&u_mode, mode);
1529 if (copy_to_user(mode_ptr + copied,
1530 &u_mode, sizeof(u_mode))) {
1537 out_resp->count_modes = mode_count;
1539 if ((out_resp->count_props >= props_count) && props_count) {
1541 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1542 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1543 for (i = 0; i < connector->properties.count; i++) {
1544 if (put_user(connector->properties.ids[i],
1545 prop_ptr + copied)) {
1550 if (put_user(connector->properties.values[i],
1551 prop_values + copied)) {
1558 out_resp->count_props = props_count;
1560 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1562 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1563 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1564 if (connector->encoder_ids[i] != 0) {
1565 if (put_user(connector->encoder_ids[i],
1566 encoder_ptr + copied)) {
1574 out_resp->count_encoders = encoders_count;
1577 mutex_unlock(&dev->mode_config.mutex);
1581 int drm_mode_getencoder(struct drm_device *dev, void *data,
1582 struct drm_file *file_priv)
1584 struct drm_mode_get_encoder *enc_resp = data;
1585 struct drm_mode_object *obj;
1586 struct drm_encoder *encoder;
1589 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1592 mutex_lock(&dev->mode_config.mutex);
1593 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1594 DRM_MODE_OBJECT_ENCODER);
1599 encoder = obj_to_encoder(obj);
1602 enc_resp->crtc_id = encoder->crtc->base.id;
1604 enc_resp->crtc_id = 0;
1605 enc_resp->encoder_type = encoder->encoder_type;
1606 enc_resp->encoder_id = encoder->base.id;
1607 enc_resp->possible_crtcs = encoder->possible_crtcs;
1608 enc_resp->possible_clones = encoder->possible_clones;
1611 mutex_unlock(&dev->mode_config.mutex);
1616 * drm_mode_getplane_res - get plane info
1619 * @file_priv: DRM file info
1622 * Takes mode config lock.
1624 * Return an plane count and set of IDs.
1626 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1627 struct drm_file *file_priv)
1629 struct drm_mode_get_plane_res *plane_resp = data;
1630 struct drm_mode_config *config;
1631 struct drm_plane *plane;
1632 uint32_t __user *plane_ptr;
1633 int copied = 0, ret = 0;
1635 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1638 mutex_lock(&dev->mode_config.mutex);
1639 config = &dev->mode_config;
1642 * This ioctl is called twice, once to determine how much space is
1643 * needed, and the 2nd time to fill it.
1645 if (config->num_plane &&
1646 (plane_resp->count_planes >= config->num_plane)) {
1647 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1649 list_for_each_entry(plane, &config->plane_list, head) {
1650 if (put_user(plane->base.id, plane_ptr + copied)) {
1657 plane_resp->count_planes = config->num_plane;
1660 mutex_unlock(&dev->mode_config.mutex);
1665 * drm_mode_getplane - get plane info
1668 * @file_priv: DRM file info
1671 * Takes mode config lock.
1673 * Return plane info, including formats supported, gamma size, any
1676 int drm_mode_getplane(struct drm_device *dev, void *data,
1677 struct drm_file *file_priv)
1679 struct drm_mode_get_plane *plane_resp = data;
1680 struct drm_mode_object *obj;
1681 struct drm_plane *plane;
1682 uint32_t __user *format_ptr;
1685 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1688 mutex_lock(&dev->mode_config.mutex);
1689 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1690 DRM_MODE_OBJECT_PLANE);
1695 plane = obj_to_plane(obj);
1698 plane_resp->crtc_id = plane->crtc->base.id;
1700 plane_resp->crtc_id = 0;
1703 plane_resp->fb_id = plane->fb->base.id;
1705 plane_resp->fb_id = 0;
1707 plane_resp->plane_id = plane->base.id;
1708 plane_resp->possible_crtcs = plane->possible_crtcs;
1709 plane_resp->gamma_size = plane->gamma_size;
1712 * This ioctl is called twice, once to determine how much space is
1713 * needed, and the 2nd time to fill it.
1715 if (plane->format_count &&
1716 (plane_resp->count_format_types >= plane->format_count)) {
1717 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1718 if (copy_to_user(format_ptr,
1719 plane->format_types,
1720 sizeof(uint32_t) * plane->format_count)) {
1725 plane_resp->count_format_types = plane->format_count;
1728 mutex_unlock(&dev->mode_config.mutex);
1733 * drm_mode_setplane - set up or tear down an plane
1735 * @data: ioctl data*
1736 * @file_prive: DRM file info
1739 * Takes mode config lock.
1741 * Set plane info, including placement, fb, scaling, and other factors.
1742 * Or pass a NULL fb to disable.
1744 int drm_mode_setplane(struct drm_device *dev, void *data,
1745 struct drm_file *file_priv)
1747 struct drm_mode_set_plane *plane_req = data;
1748 struct drm_mode_object *obj;
1749 struct drm_plane *plane;
1750 struct drm_crtc *crtc;
1751 struct drm_framebuffer *fb;
1753 unsigned int fb_width, fb_height;
1756 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1759 mutex_lock(&dev->mode_config.mutex);
1762 * First, find the plane, crtc, and fb objects. If not available,
1763 * we don't bother to call the driver.
1765 obj = drm_mode_object_find(dev, plane_req->plane_id,
1766 DRM_MODE_OBJECT_PLANE);
1768 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1769 plane_req->plane_id);
1773 plane = obj_to_plane(obj);
1775 /* No fb means shut it down */
1776 if (!plane_req->fb_id) {
1777 plane->funcs->disable_plane(plane);
1783 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1784 DRM_MODE_OBJECT_CRTC);
1786 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1787 plane_req->crtc_id);
1791 crtc = obj_to_crtc(obj);
1793 obj = drm_mode_object_find(dev, plane_req->fb_id,
1794 DRM_MODE_OBJECT_FB);
1796 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1801 fb = obj_to_fb(obj);
1803 /* Check whether this plane supports the fb pixel format. */
1804 for (i = 0; i < plane->format_count; i++)
1805 if (fb->pixel_format == plane->format_types[i])
1807 if (i == plane->format_count) {
1808 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1813 fb_width = fb->width << 16;
1814 fb_height = fb->height << 16;
1816 /* Make sure source coordinates are inside the fb. */
1817 if (plane_req->src_w > fb_width ||
1818 plane_req->src_x > fb_width - plane_req->src_w ||
1819 plane_req->src_h > fb_height ||
1820 plane_req->src_y > fb_height - plane_req->src_h) {
1821 DRM_DEBUG_KMS("Invalid source coordinates "
1822 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1823 plane_req->src_w >> 16,
1824 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1825 plane_req->src_h >> 16,
1826 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1827 plane_req->src_x >> 16,
1828 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1829 plane_req->src_y >> 16,
1830 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1835 /* Give drivers some help against integer overflows */
1836 if (plane_req->crtc_w > INT_MAX ||
1837 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1838 plane_req->crtc_h > INT_MAX ||
1839 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1840 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1841 plane_req->crtc_w, plane_req->crtc_h,
1842 plane_req->crtc_x, plane_req->crtc_y);
1847 ret = plane->funcs->update_plane(plane, crtc, fb,
1848 plane_req->crtc_x, plane_req->crtc_y,
1849 plane_req->crtc_w, plane_req->crtc_h,
1850 plane_req->src_x, plane_req->src_y,
1851 plane_req->src_w, plane_req->src_h);
1858 mutex_unlock(&dev->mode_config.mutex);
1864 * drm_mode_setcrtc - set CRTC configuration
1865 * @inode: inode from the ioctl
1866 * @filp: file * from the ioctl
1867 * @cmd: cmd from ioctl
1868 * @arg: arg from ioctl
1871 * Takes mode config lock.
1873 * Build a new CRTC configuration based on user request.
1875 * Called by the user via ioctl.
1878 * Zero on success, errno on failure.
1880 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1881 struct drm_file *file_priv)
1883 struct drm_mode_config *config = &dev->mode_config;
1884 struct drm_mode_crtc *crtc_req = data;
1885 struct drm_mode_object *obj;
1886 struct drm_crtc *crtc;
1887 struct drm_connector **connector_set = NULL, *connector;
1888 struct drm_framebuffer *fb = NULL;
1889 struct drm_display_mode *mode = NULL;
1890 struct drm_mode_set set;
1891 uint32_t __user *set_connectors_ptr;
1895 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1898 /* For some reason crtc x/y offsets are signed internally. */
1899 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1902 mutex_lock(&dev->mode_config.mutex);
1903 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1904 DRM_MODE_OBJECT_CRTC);
1906 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1910 crtc = obj_to_crtc(obj);
1911 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1913 if (crtc_req->mode_valid) {
1914 int hdisplay, vdisplay;
1915 /* If we have a mode we need a framebuffer. */
1916 /* If we pass -1, set the mode with the currently bound fb */
1917 if (crtc_req->fb_id == -1) {
1919 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1925 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1926 DRM_MODE_OBJECT_FB);
1928 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1933 fb = obj_to_fb(obj);
1936 mode = drm_mode_create(dev);
1942 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1944 DRM_DEBUG_KMS("Invalid mode\n");
1948 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1950 hdisplay = mode->hdisplay;
1951 vdisplay = mode->vdisplay;
1953 if (crtc->invert_dimensions)
1954 swap(hdisplay, vdisplay);
1956 if (hdisplay > fb->width ||
1957 vdisplay > fb->height ||
1958 crtc_req->x > fb->width - hdisplay ||
1959 crtc_req->y > fb->height - vdisplay) {
1960 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1961 fb->width, fb->height,
1962 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1963 crtc->invert_dimensions ? " (inverted)" : "");
1969 if (crtc_req->count_connectors == 0 && mode) {
1970 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1975 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1976 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1977 crtc_req->count_connectors);
1982 if (crtc_req->count_connectors > 0) {
1985 /* Avoid unbounded kernel memory allocation */
1986 if (crtc_req->count_connectors > config->num_connector) {
1991 connector_set = kmalloc(crtc_req->count_connectors *
1992 sizeof(struct drm_connector *),
1994 if (!connector_set) {
1999 for (i = 0; i < crtc_req->count_connectors; i++) {
2000 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2001 if (get_user(out_id, &set_connectors_ptr[i])) {
2006 obj = drm_mode_object_find(dev, out_id,
2007 DRM_MODE_OBJECT_CONNECTOR);
2009 DRM_DEBUG_KMS("Connector id %d unknown\n",
2014 connector = obj_to_connector(obj);
2015 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2017 drm_get_connector_name(connector));
2019 connector_set[i] = connector;
2024 set.x = crtc_req->x;
2025 set.y = crtc_req->y;
2027 set.connectors = connector_set;
2028 set.num_connectors = crtc_req->count_connectors;
2030 ret = crtc->funcs->set_config(&set);
2033 kfree(connector_set);
2034 drm_mode_destroy(dev, mode);
2035 mutex_unlock(&dev->mode_config.mutex);
2039 int drm_mode_cursor_ioctl(struct drm_device *dev,
2040 void *data, struct drm_file *file_priv)
2042 struct drm_mode_cursor *req = data;
2043 struct drm_mode_object *obj;
2044 struct drm_crtc *crtc;
2047 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2050 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2053 mutex_lock(&dev->mode_config.mutex);
2054 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2056 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2060 crtc = obj_to_crtc(obj);
2062 if (req->flags & DRM_MODE_CURSOR_BO) {
2063 if (!crtc->funcs->cursor_set) {
2067 /* Turns off the cursor if handle is 0 */
2068 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2069 req->width, req->height);
2072 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2073 if (crtc->funcs->cursor_move) {
2074 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2081 mutex_unlock(&dev->mode_config.mutex);
2085 /* Original addfb only supported RGB formats, so figure out which one */
2086 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2092 fmt = DRM_FORMAT_RGB332;
2096 fmt = DRM_FORMAT_XRGB1555;
2098 fmt = DRM_FORMAT_RGB565;
2101 fmt = DRM_FORMAT_RGB888;
2105 fmt = DRM_FORMAT_XRGB8888;
2106 else if (depth == 30)
2107 fmt = DRM_FORMAT_XRGB2101010;
2109 fmt = DRM_FORMAT_ARGB8888;
2112 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2113 fmt = DRM_FORMAT_XRGB8888;
2119 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2122 * drm_mode_addfb - add an FB to the graphics configuration
2123 * @inode: inode from the ioctl
2124 * @filp: file * from the ioctl
2125 * @cmd: cmd from ioctl
2126 * @arg: arg from ioctl
2129 * Takes mode config lock.
2131 * Add a new FB to the specified CRTC, given a user request.
2133 * Called by the user via ioctl.
2136 * Zero on success, errno on failure.
2138 int drm_mode_addfb(struct drm_device *dev,
2139 void *data, struct drm_file *file_priv)
2141 struct drm_mode_fb_cmd *or = data;
2142 struct drm_mode_fb_cmd2 r = {};
2143 struct drm_mode_config *config = &dev->mode_config;
2144 struct drm_framebuffer *fb;
2147 /* Use new struct with format internally */
2148 r.fb_id = or->fb_id;
2149 r.width = or->width;
2150 r.height = or->height;
2151 r.pitches[0] = or->pitch;
2152 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2153 r.handles[0] = or->handle;
2155 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2158 if ((config->min_width > r.width) || (r.width > config->max_width))
2161 if ((config->min_height > r.height) || (r.height > config->max_height))
2164 mutex_lock(&dev->mode_config.mutex);
2166 /* TODO check buffer is sufficiently large */
2167 /* TODO setup destructor callback */
2169 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2171 DRM_DEBUG_KMS("could not create framebuffer\n");
2176 or->fb_id = fb->base.id;
2177 list_add(&fb->filp_head, &file_priv->fbs);
2178 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2181 mutex_unlock(&dev->mode_config.mutex);
2185 static int format_check(const struct drm_mode_fb_cmd2 *r)
2187 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2191 case DRM_FORMAT_RGB332:
2192 case DRM_FORMAT_BGR233:
2193 case DRM_FORMAT_XRGB4444:
2194 case DRM_FORMAT_XBGR4444:
2195 case DRM_FORMAT_RGBX4444:
2196 case DRM_FORMAT_BGRX4444:
2197 case DRM_FORMAT_ARGB4444:
2198 case DRM_FORMAT_ABGR4444:
2199 case DRM_FORMAT_RGBA4444:
2200 case DRM_FORMAT_BGRA4444:
2201 case DRM_FORMAT_XRGB1555:
2202 case DRM_FORMAT_XBGR1555:
2203 case DRM_FORMAT_RGBX5551:
2204 case DRM_FORMAT_BGRX5551:
2205 case DRM_FORMAT_ARGB1555:
2206 case DRM_FORMAT_ABGR1555:
2207 case DRM_FORMAT_RGBA5551:
2208 case DRM_FORMAT_BGRA5551:
2209 case DRM_FORMAT_RGB565:
2210 case DRM_FORMAT_BGR565:
2211 case DRM_FORMAT_RGB888:
2212 case DRM_FORMAT_BGR888:
2213 case DRM_FORMAT_XRGB8888:
2214 case DRM_FORMAT_XBGR8888:
2215 case DRM_FORMAT_RGBX8888:
2216 case DRM_FORMAT_BGRX8888:
2217 case DRM_FORMAT_ARGB8888:
2218 case DRM_FORMAT_ABGR8888:
2219 case DRM_FORMAT_RGBA8888:
2220 case DRM_FORMAT_BGRA8888:
2221 case DRM_FORMAT_XRGB2101010:
2222 case DRM_FORMAT_XBGR2101010:
2223 case DRM_FORMAT_RGBX1010102:
2224 case DRM_FORMAT_BGRX1010102:
2225 case DRM_FORMAT_ARGB2101010:
2226 case DRM_FORMAT_ABGR2101010:
2227 case DRM_FORMAT_RGBA1010102:
2228 case DRM_FORMAT_BGRA1010102:
2229 case DRM_FORMAT_YUYV:
2230 case DRM_FORMAT_YVYU:
2231 case DRM_FORMAT_UYVY:
2232 case DRM_FORMAT_VYUY:
2233 case DRM_FORMAT_AYUV:
2234 case DRM_FORMAT_NV12:
2235 case DRM_FORMAT_NV21:
2236 case DRM_FORMAT_NV16:
2237 case DRM_FORMAT_NV61:
2238 case DRM_FORMAT_NV24:
2239 case DRM_FORMAT_NV42:
2240 case DRM_FORMAT_YUV410:
2241 case DRM_FORMAT_YVU410:
2242 case DRM_FORMAT_YUV411:
2243 case DRM_FORMAT_YVU411:
2244 case DRM_FORMAT_YUV420:
2245 case DRM_FORMAT_YVU420:
2246 case DRM_FORMAT_YUV422:
2247 case DRM_FORMAT_YVU422:
2248 case DRM_FORMAT_YUV444:
2249 case DRM_FORMAT_YVU444:
2256 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2258 int ret, hsub, vsub, num_planes, i;
2260 ret = format_check(r);
2262 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
2266 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2267 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2268 num_planes = drm_format_num_planes(r->pixel_format);
2270 if (r->width == 0 || r->width % hsub) {
2271 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2275 if (r->height == 0 || r->height % vsub) {
2276 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2280 for (i = 0; i < num_planes; i++) {
2281 unsigned int width = r->width / (i != 0 ? hsub : 1);
2282 unsigned int height = r->height / (i != 0 ? vsub : 1);
2283 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2285 if (!r->handles[i]) {
2286 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2290 if ((uint64_t) width * cpp > UINT_MAX)
2293 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2296 if (r->pitches[i] < width * cpp) {
2297 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2306 * drm_mode_addfb2 - add an FB to the graphics configuration
2307 * @inode: inode from the ioctl
2308 * @filp: file * from the ioctl
2309 * @cmd: cmd from ioctl
2310 * @arg: arg from ioctl
2313 * Takes mode config lock.
2315 * Add a new FB to the specified CRTC, given a user request with format.
2317 * Called by the user via ioctl.
2320 * Zero on success, errno on failure.
2322 int drm_mode_addfb2(struct drm_device *dev,
2323 void *data, struct drm_file *file_priv)
2325 struct drm_mode_fb_cmd2 *r = data;
2326 struct drm_mode_config *config = &dev->mode_config;
2327 struct drm_framebuffer *fb;
2330 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2333 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2334 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2338 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2339 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2340 r->width, config->min_width, config->max_width);
2343 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2344 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2345 r->height, config->min_height, config->max_height);
2349 ret = framebuffer_check(r);
2353 mutex_lock(&dev->mode_config.mutex);
2355 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2357 DRM_DEBUG_KMS("could not create framebuffer\n");
2362 r->fb_id = fb->base.id;
2363 list_add(&fb->filp_head, &file_priv->fbs);
2364 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2367 mutex_unlock(&dev->mode_config.mutex);
2372 * drm_mode_rmfb - remove an FB from the configuration
2373 * @inode: inode from the ioctl
2374 * @filp: file * from the ioctl
2375 * @cmd: cmd from ioctl
2376 * @arg: arg from ioctl
2379 * Takes mode config lock.
2381 * Remove the FB specified by the user.
2383 * Called by the user via ioctl.
2386 * Zero on success, errno on failure.
2388 int drm_mode_rmfb(struct drm_device *dev,
2389 void *data, struct drm_file *file_priv)
2391 struct drm_mode_object *obj;
2392 struct drm_framebuffer *fb = NULL;
2393 struct drm_framebuffer *fbl = NULL;
2394 uint32_t *id = data;
2398 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2401 mutex_lock(&dev->mode_config.mutex);
2402 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
2403 /* TODO check that we really get a framebuffer back. */
2408 fb = obj_to_fb(obj);
2410 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2419 drm_framebuffer_remove(fb);
2422 mutex_unlock(&dev->mode_config.mutex);
2427 * drm_mode_getfb - get FB info
2428 * @inode: inode from the ioctl
2429 * @filp: file * from the ioctl
2430 * @cmd: cmd from ioctl
2431 * @arg: arg from ioctl
2434 * Takes mode config lock.
2436 * Lookup the FB given its ID and return info about it.
2438 * Called by the user via ioctl.
2441 * Zero on success, errno on failure.
2443 int drm_mode_getfb(struct drm_device *dev,
2444 void *data, struct drm_file *file_priv)
2446 struct drm_mode_fb_cmd *r = data;
2447 struct drm_mode_object *obj;
2448 struct drm_framebuffer *fb;
2451 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2454 mutex_lock(&dev->mode_config.mutex);
2455 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2460 fb = obj_to_fb(obj);
2462 r->height = fb->height;
2463 r->width = fb->width;
2464 r->depth = fb->depth;
2465 r->bpp = fb->bits_per_pixel;
2466 r->pitch = fb->pitches[0];
2467 fb->funcs->create_handle(fb, file_priv, &r->handle);
2470 mutex_unlock(&dev->mode_config.mutex);
2474 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2475 void *data, struct drm_file *file_priv)
2477 struct drm_clip_rect __user *clips_ptr;
2478 struct drm_clip_rect *clips = NULL;
2479 struct drm_mode_fb_dirty_cmd *r = data;
2480 struct drm_mode_object *obj;
2481 struct drm_framebuffer *fb;
2486 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2489 mutex_lock(&dev->mode_config.mutex);
2490 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2495 fb = obj_to_fb(obj);
2497 num_clips = r->num_clips;
2498 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2500 if (!num_clips != !clips_ptr) {
2505 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2507 /* If userspace annotates copy, clips must come in pairs */
2508 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2513 if (num_clips && clips_ptr) {
2514 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2518 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2524 ret = copy_from_user(clips, clips_ptr,
2525 num_clips * sizeof(*clips));
2532 if (fb->funcs->dirty) {
2533 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2543 mutex_unlock(&dev->mode_config.mutex);
2549 * drm_fb_release - remove and free the FBs on this file
2550 * @filp: file * from the ioctl
2553 * Takes mode config lock.
2555 * Destroy all the FBs associated with @filp.
2557 * Called by the user via ioctl.
2560 * Zero on success, errno on failure.
2562 void drm_fb_release(struct drm_file *priv)
2564 struct drm_device *dev = priv->minor->dev;
2565 struct drm_framebuffer *fb, *tfb;
2567 mutex_lock(&dev->mode_config.mutex);
2568 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2569 drm_framebuffer_remove(fb);
2571 mutex_unlock(&dev->mode_config.mutex);
2575 * drm_mode_attachmode - add a mode to the user mode list
2577 * @connector: connector to add the mode to
2578 * @mode: mode to add
2580 * Add @mode to @connector's user mode list.
2582 static void drm_mode_attachmode(struct drm_device *dev,
2583 struct drm_connector *connector,
2584 struct drm_display_mode *mode)
2586 list_add_tail(&mode->head, &connector->user_modes);
2589 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2590 const struct drm_display_mode *mode)
2592 struct drm_connector *connector;
2594 struct drm_display_mode *dup_mode, *next;
2597 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2598 if (!connector->encoder)
2600 if (connector->encoder->crtc == crtc) {
2601 dup_mode = drm_mode_duplicate(dev, mode);
2606 list_add_tail(&dup_mode->head, &list);
2610 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2611 if (!connector->encoder)
2613 if (connector->encoder->crtc == crtc)
2614 list_move_tail(list.next, &connector->user_modes);
2617 WARN_ON(!list_empty(&list));
2620 list_for_each_entry_safe(dup_mode, next, &list, head)
2621 drm_mode_destroy(dev, dup_mode);
2625 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2627 static int drm_mode_detachmode(struct drm_device *dev,
2628 struct drm_connector *connector,
2629 struct drm_display_mode *mode)
2633 struct drm_display_mode *match_mode, *t;
2635 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2636 if (drm_mode_equal(match_mode, mode)) {
2637 list_del(&match_mode->head);
2638 drm_mode_destroy(dev, match_mode);
2650 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2652 struct drm_connector *connector;
2654 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2655 drm_mode_detachmode(dev, connector, mode);
2659 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2662 * drm_fb_attachmode - Attach a user mode to an connector
2663 * @inode: inode from the ioctl
2664 * @filp: file * from the ioctl
2665 * @cmd: cmd from ioctl
2666 * @arg: arg from ioctl
2668 * This attaches a user specified mode to an connector.
2669 * Called by the user via ioctl.
2672 * Zero on success, errno on failure.
2674 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2675 void *data, struct drm_file *file_priv)
2677 struct drm_mode_mode_cmd *mode_cmd = data;
2678 struct drm_connector *connector;
2679 struct drm_display_mode *mode;
2680 struct drm_mode_object *obj;
2681 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2684 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2687 mutex_lock(&dev->mode_config.mutex);
2689 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2694 connector = obj_to_connector(obj);
2696 mode = drm_mode_create(dev);
2702 ret = drm_crtc_convert_umode(mode, umode);
2704 DRM_DEBUG_KMS("Invalid mode\n");
2705 drm_mode_destroy(dev, mode);
2709 drm_mode_attachmode(dev, connector, mode);
2711 mutex_unlock(&dev->mode_config.mutex);
2717 * drm_fb_detachmode - Detach a user specified mode from an connector
2718 * @inode: inode from the ioctl
2719 * @filp: file * from the ioctl
2720 * @cmd: cmd from ioctl
2721 * @arg: arg from ioctl
2723 * Called by the user via ioctl.
2726 * Zero on success, errno on failure.
2728 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2729 void *data, struct drm_file *file_priv)
2731 struct drm_mode_object *obj;
2732 struct drm_mode_mode_cmd *mode_cmd = data;
2733 struct drm_connector *connector;
2734 struct drm_display_mode mode;
2735 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2738 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2741 mutex_lock(&dev->mode_config.mutex);
2743 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2748 connector = obj_to_connector(obj);
2750 ret = drm_crtc_convert_umode(&mode, umode);
2752 DRM_DEBUG_KMS("Invalid mode\n");
2756 ret = drm_mode_detachmode(dev, connector, &mode);
2758 mutex_unlock(&dev->mode_config.mutex);
2762 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2763 const char *name, int num_values)
2765 struct drm_property *property = NULL;
2768 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2773 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2774 if (!property->values)
2778 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2782 property->flags = flags;
2783 property->num_values = num_values;
2784 INIT_LIST_HEAD(&property->enum_blob_list);
2787 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2788 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2791 list_add_tail(&property->head, &dev->mode_config.property_list);
2794 kfree(property->values);
2798 EXPORT_SYMBOL(drm_property_create);
2800 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2802 const struct drm_prop_enum_list *props,
2805 struct drm_property *property;
2808 flags |= DRM_MODE_PROP_ENUM;
2810 property = drm_property_create(dev, flags, name, num_values);
2814 for (i = 0; i < num_values; i++) {
2815 ret = drm_property_add_enum(property, i,
2819 drm_property_destroy(dev, property);
2826 EXPORT_SYMBOL(drm_property_create_enum);
2828 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2829 int flags, const char *name,
2830 const struct drm_prop_enum_list *props,
2833 struct drm_property *property;
2836 flags |= DRM_MODE_PROP_BITMASK;
2838 property = drm_property_create(dev, flags, name, num_values);
2842 for (i = 0; i < num_values; i++) {
2843 ret = drm_property_add_enum(property, i,
2847 drm_property_destroy(dev, property);
2854 EXPORT_SYMBOL(drm_property_create_bitmask);
2856 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2858 uint64_t min, uint64_t max)
2860 struct drm_property *property;
2862 flags |= DRM_MODE_PROP_RANGE;
2864 property = drm_property_create(dev, flags, name, 2);
2868 property->values[0] = min;
2869 property->values[1] = max;
2873 EXPORT_SYMBOL(drm_property_create_range);
2875 int drm_property_add_enum(struct drm_property *property, int index,
2876 uint64_t value, const char *name)
2878 struct drm_property_enum *prop_enum;
2880 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2884 * Bitmask enum properties have the additional constraint of values
2887 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2890 if (!list_empty(&property->enum_blob_list)) {
2891 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2892 if (prop_enum->value == value) {
2893 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2894 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2900 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2904 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2905 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2906 prop_enum->value = value;
2908 property->values[index] = value;
2909 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2912 EXPORT_SYMBOL(drm_property_add_enum);
2914 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2916 struct drm_property_enum *prop_enum, *pt;
2918 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2919 list_del(&prop_enum->head);
2923 if (property->num_values)
2924 kfree(property->values);
2925 drm_mode_object_put(dev, &property->base);
2926 list_del(&property->head);
2929 EXPORT_SYMBOL(drm_property_destroy);
2931 void drm_object_attach_property(struct drm_mode_object *obj,
2932 struct drm_property *property,
2935 int count = obj->properties->count;
2937 if (count == DRM_OBJECT_MAX_PROPERTY) {
2938 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2939 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2940 "you see this message on the same object type.\n",
2945 obj->properties->ids[count] = property->base.id;
2946 obj->properties->values[count] = init_val;
2947 obj->properties->count++;
2949 EXPORT_SYMBOL(drm_object_attach_property);
2951 int drm_object_property_set_value(struct drm_mode_object *obj,
2952 struct drm_property *property, uint64_t val)
2956 for (i = 0; i < obj->properties->count; i++) {
2957 if (obj->properties->ids[i] == property->base.id) {
2958 obj->properties->values[i] = val;
2965 EXPORT_SYMBOL(drm_object_property_set_value);
2967 int drm_object_property_get_value(struct drm_mode_object *obj,
2968 struct drm_property *property, uint64_t *val)
2972 for (i = 0; i < obj->properties->count; i++) {
2973 if (obj->properties->ids[i] == property->base.id) {
2974 *val = obj->properties->values[i];
2981 EXPORT_SYMBOL(drm_object_property_get_value);
2983 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2984 void *data, struct drm_file *file_priv)
2986 struct drm_mode_object *obj;
2987 struct drm_mode_get_property *out_resp = data;
2988 struct drm_property *property;
2991 int value_count = 0;
2994 struct drm_property_enum *prop_enum;
2995 struct drm_mode_property_enum __user *enum_ptr;
2996 struct drm_property_blob *prop_blob;
2997 uint32_t __user *blob_id_ptr;
2998 uint64_t __user *values_ptr;
2999 uint32_t __user *blob_length_ptr;
3001 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3004 mutex_lock(&dev->mode_config.mutex);
3005 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3010 property = obj_to_property(obj);
3012 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3013 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3015 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3016 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3020 value_count = property->num_values;
3022 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3023 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3024 out_resp->flags = property->flags;
3026 if ((out_resp->count_values >= value_count) && value_count) {
3027 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3028 for (i = 0; i < value_count; i++) {
3029 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3035 out_resp->count_values = value_count;
3037 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3038 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3040 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3041 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3043 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3048 if (copy_to_user(&enum_ptr[copied].name,
3049 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3056 out_resp->count_enum_blobs = enum_count;
3059 if (property->flags & DRM_MODE_PROP_BLOB) {
3060 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3062 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3063 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3065 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3066 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3071 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3079 out_resp->count_enum_blobs = blob_count;
3082 mutex_unlock(&dev->mode_config.mutex);
3086 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3089 struct drm_property_blob *blob;
3092 if (!length || !data)
3095 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3099 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3105 blob->length = length;
3107 memcpy(blob->data, data, length);
3109 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3113 static void drm_property_destroy_blob(struct drm_device *dev,
3114 struct drm_property_blob *blob)
3116 drm_mode_object_put(dev, &blob->base);
3117 list_del(&blob->head);
3121 int drm_mode_getblob_ioctl(struct drm_device *dev,
3122 void *data, struct drm_file *file_priv)
3124 struct drm_mode_object *obj;
3125 struct drm_mode_get_blob *out_resp = data;
3126 struct drm_property_blob *blob;
3128 void __user *blob_ptr;
3130 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3133 mutex_lock(&dev->mode_config.mutex);
3134 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3139 blob = obj_to_blob(obj);
3141 if (out_resp->length == blob->length) {
3142 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3143 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3148 out_resp->length = blob->length;
3151 mutex_unlock(&dev->mode_config.mutex);
3155 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3158 struct drm_device *dev = connector->dev;
3161 if (connector->edid_blob_ptr)
3162 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3164 /* Delete edid, when there is none. */
3166 connector->edid_blob_ptr = NULL;
3167 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3171 size = EDID_LENGTH * (1 + edid->extensions);
3172 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3174 if (!connector->edid_blob_ptr)
3177 ret = drm_object_property_set_value(&connector->base,
3178 dev->mode_config.edid_property,
3179 connector->edid_blob_ptr->base.id);
3183 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3185 static bool drm_property_change_is_valid(struct drm_property *property,
3188 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3190 if (property->flags & DRM_MODE_PROP_RANGE) {
3191 if (value < property->values[0] || value > property->values[1])
3194 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3196 uint64_t valid_mask = 0;
3197 for (i = 0; i < property->num_values; i++)
3198 valid_mask |= (1ULL << property->values[i]);
3199 return !(value & ~valid_mask);
3200 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3201 /* Only the driver knows */
3205 for (i = 0; i < property->num_values; i++)
3206 if (property->values[i] == value)
3212 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3213 void *data, struct drm_file *file_priv)
3215 struct drm_mode_connector_set_property *conn_set_prop = data;
3216 struct drm_mode_obj_set_property obj_set_prop = {
3217 .value = conn_set_prop->value,
3218 .prop_id = conn_set_prop->prop_id,
3219 .obj_id = conn_set_prop->connector_id,
3220 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3223 /* It does all the locking and checking we need */
3224 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3227 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3228 struct drm_property *property,
3232 struct drm_connector *connector = obj_to_connector(obj);
3234 /* Do DPMS ourselves */
3235 if (property == connector->dev->mode_config.dpms_property) {
3236 if (connector->funcs->dpms)
3237 (*connector->funcs->dpms)(connector, (int)value);
3239 } else if (connector->funcs->set_property)
3240 ret = connector->funcs->set_property(connector, property, value);
3242 /* store the property value if successful */
3244 drm_object_property_set_value(&connector->base, property, value);
3248 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3249 struct drm_property *property,
3253 struct drm_crtc *crtc = obj_to_crtc(obj);
3255 if (crtc->funcs->set_property)
3256 ret = crtc->funcs->set_property(crtc, property, value);
3258 drm_object_property_set_value(obj, property, value);
3263 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3264 struct drm_property *property,
3268 struct drm_plane *plane = obj_to_plane(obj);
3270 if (plane->funcs->set_property)
3271 ret = plane->funcs->set_property(plane, property, value);
3273 drm_object_property_set_value(obj, property, value);
3278 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3279 struct drm_file *file_priv)
3281 struct drm_mode_obj_get_properties *arg = data;
3282 struct drm_mode_object *obj;
3286 int props_count = 0;
3287 uint32_t __user *props_ptr;
3288 uint64_t __user *prop_values_ptr;
3290 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3293 mutex_lock(&dev->mode_config.mutex);
3295 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3300 if (!obj->properties) {
3305 props_count = obj->properties->count;
3307 /* This ioctl is called twice, once to determine how much space is
3308 * needed, and the 2nd time to fill it. */
3309 if ((arg->count_props >= props_count) && props_count) {
3311 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3312 prop_values_ptr = (uint64_t __user *)(unsigned long)
3313 (arg->prop_values_ptr);
3314 for (i = 0; i < props_count; i++) {
3315 if (put_user(obj->properties->ids[i],
3316 props_ptr + copied)) {
3320 if (put_user(obj->properties->values[i],
3321 prop_values_ptr + copied)) {
3328 arg->count_props = props_count;
3330 mutex_unlock(&dev->mode_config.mutex);
3334 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3335 struct drm_file *file_priv)
3337 struct drm_mode_obj_set_property *arg = data;
3338 struct drm_mode_object *arg_obj;
3339 struct drm_mode_object *prop_obj;
3340 struct drm_property *property;
3344 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3347 mutex_lock(&dev->mode_config.mutex);
3349 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3352 if (!arg_obj->properties)
3355 for (i = 0; i < arg_obj->properties->count; i++)
3356 if (arg_obj->properties->ids[i] == arg->prop_id)
3359 if (i == arg_obj->properties->count)
3362 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3363 DRM_MODE_OBJECT_PROPERTY);
3366 property = obj_to_property(prop_obj);
3368 if (!drm_property_change_is_valid(property, arg->value))
3371 switch (arg_obj->type) {
3372 case DRM_MODE_OBJECT_CONNECTOR:
3373 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3376 case DRM_MODE_OBJECT_CRTC:
3377 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3379 case DRM_MODE_OBJECT_PLANE:
3380 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3385 mutex_unlock(&dev->mode_config.mutex);
3389 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3390 struct drm_encoder *encoder)
3394 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3395 if (connector->encoder_ids[i] == 0) {
3396 connector->encoder_ids[i] = encoder->base.id;
3402 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3404 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3405 struct drm_encoder *encoder)
3408 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3409 if (connector->encoder_ids[i] == encoder->base.id) {
3410 connector->encoder_ids[i] = 0;
3411 if (connector->encoder == encoder)
3412 connector->encoder = NULL;
3417 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3419 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3422 crtc->gamma_size = gamma_size;
3424 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3425 if (!crtc->gamma_store) {
3426 crtc->gamma_size = 0;
3432 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3434 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3435 void *data, struct drm_file *file_priv)
3437 struct drm_mode_crtc_lut *crtc_lut = data;
3438 struct drm_mode_object *obj;
3439 struct drm_crtc *crtc;
3440 void *r_base, *g_base, *b_base;
3444 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3447 mutex_lock(&dev->mode_config.mutex);
3448 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3453 crtc = obj_to_crtc(obj);
3455 if (crtc->funcs->gamma_set == NULL) {
3460 /* memcpy into gamma store */
3461 if (crtc_lut->gamma_size != crtc->gamma_size) {
3466 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3467 r_base = crtc->gamma_store;
3468 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3473 g_base = r_base + size;
3474 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3479 b_base = g_base + size;
3480 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3485 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3488 mutex_unlock(&dev->mode_config.mutex);
3493 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3494 void *data, struct drm_file *file_priv)
3496 struct drm_mode_crtc_lut *crtc_lut = data;
3497 struct drm_mode_object *obj;
3498 struct drm_crtc *crtc;
3499 void *r_base, *g_base, *b_base;
3503 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3506 mutex_lock(&dev->mode_config.mutex);
3507 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3512 crtc = obj_to_crtc(obj);
3514 /* memcpy into gamma store */
3515 if (crtc_lut->gamma_size != crtc->gamma_size) {
3520 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3521 r_base = crtc->gamma_store;
3522 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3527 g_base = r_base + size;
3528 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3533 b_base = g_base + size;
3534 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3539 mutex_unlock(&dev->mode_config.mutex);
3543 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3544 void *data, struct drm_file *file_priv)
3546 struct drm_mode_crtc_page_flip *page_flip = data;
3547 struct drm_mode_object *obj;
3548 struct drm_crtc *crtc;
3549 struct drm_framebuffer *fb;
3550 struct drm_pending_vblank_event *e = NULL;
3551 unsigned long flags;
3552 int hdisplay, vdisplay;
3555 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3556 page_flip->reserved != 0)
3559 mutex_lock(&dev->mode_config.mutex);
3560 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3563 crtc = obj_to_crtc(obj);
3565 if (crtc->fb == NULL) {
3566 /* The framebuffer is currently unbound, presumably
3567 * due to a hotplug event, that userspace has not
3574 if (crtc->funcs->page_flip == NULL)
3577 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3580 fb = obj_to_fb(obj);
3582 hdisplay = crtc->mode.hdisplay;
3583 vdisplay = crtc->mode.vdisplay;
3585 if (crtc->invert_dimensions)
3586 swap(hdisplay, vdisplay);
3588 if (hdisplay > fb->width ||
3589 vdisplay > fb->height ||
3590 crtc->x > fb->width - hdisplay ||
3591 crtc->y > fb->height - vdisplay) {
3592 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3593 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3594 crtc->invert_dimensions ? " (inverted)" : "");
3599 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3601 spin_lock_irqsave(&dev->event_lock, flags);
3602 if (file_priv->event_space < sizeof e->event) {
3603 spin_unlock_irqrestore(&dev->event_lock, flags);
3606 file_priv->event_space -= sizeof e->event;
3607 spin_unlock_irqrestore(&dev->event_lock, flags);
3609 e = kzalloc(sizeof *e, GFP_KERNEL);
3611 spin_lock_irqsave(&dev->event_lock, flags);
3612 file_priv->event_space += sizeof e->event;
3613 spin_unlock_irqrestore(&dev->event_lock, flags);
3617 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3618 e->event.base.length = sizeof e->event;
3619 e->event.user_data = page_flip->user_data;
3620 e->base.event = &e->event.base;
3621 e->base.file_priv = file_priv;
3623 (void (*) (struct drm_pending_event *)) kfree;
3626 ret = crtc->funcs->page_flip(crtc, fb, e);
3628 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3629 spin_lock_irqsave(&dev->event_lock, flags);
3630 file_priv->event_space += sizeof e->event;
3631 spin_unlock_irqrestore(&dev->event_lock, flags);
3637 mutex_unlock(&dev->mode_config.mutex);
3641 void drm_mode_config_reset(struct drm_device *dev)
3643 struct drm_crtc *crtc;
3644 struct drm_encoder *encoder;
3645 struct drm_connector *connector;
3647 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3648 if (crtc->funcs->reset)
3649 crtc->funcs->reset(crtc);
3651 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3652 if (encoder->funcs->reset)
3653 encoder->funcs->reset(encoder);
3655 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3656 connector->status = connector_status_unknown;
3658 if (connector->funcs->reset)
3659 connector->funcs->reset(connector);
3662 EXPORT_SYMBOL(drm_mode_config_reset);
3664 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3665 void *data, struct drm_file *file_priv)
3667 struct drm_mode_create_dumb *args = data;
3669 if (!dev->driver->dumb_create)
3671 return dev->driver->dumb_create(file_priv, dev, args);
3674 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3675 void *data, struct drm_file *file_priv)
3677 struct drm_mode_map_dumb *args = data;
3679 /* call driver ioctl to get mmap offset */
3680 if (!dev->driver->dumb_map_offset)
3683 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3686 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3687 void *data, struct drm_file *file_priv)
3689 struct drm_mode_destroy_dumb *args = data;
3691 if (!dev->driver->dumb_destroy)
3694 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3698 * Just need to support RGB formats here for compat with code that doesn't
3699 * use pixel formats directly yet.
3701 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3705 case DRM_FORMAT_RGB332:
3706 case DRM_FORMAT_BGR233:
3710 case DRM_FORMAT_XRGB1555:
3711 case DRM_FORMAT_XBGR1555:
3712 case DRM_FORMAT_RGBX5551:
3713 case DRM_FORMAT_BGRX5551:
3714 case DRM_FORMAT_ARGB1555:
3715 case DRM_FORMAT_ABGR1555:
3716 case DRM_FORMAT_RGBA5551:
3717 case DRM_FORMAT_BGRA5551:
3721 case DRM_FORMAT_RGB565:
3722 case DRM_FORMAT_BGR565:
3726 case DRM_FORMAT_RGB888:
3727 case DRM_FORMAT_BGR888:
3731 case DRM_FORMAT_XRGB8888:
3732 case DRM_FORMAT_XBGR8888:
3733 case DRM_FORMAT_RGBX8888:
3734 case DRM_FORMAT_BGRX8888:
3738 case DRM_FORMAT_XRGB2101010:
3739 case DRM_FORMAT_XBGR2101010:
3740 case DRM_FORMAT_RGBX1010102:
3741 case DRM_FORMAT_BGRX1010102:
3742 case DRM_FORMAT_ARGB2101010:
3743 case DRM_FORMAT_ABGR2101010:
3744 case DRM_FORMAT_RGBA1010102:
3745 case DRM_FORMAT_BGRA1010102:
3749 case DRM_FORMAT_ARGB8888:
3750 case DRM_FORMAT_ABGR8888:
3751 case DRM_FORMAT_RGBA8888:
3752 case DRM_FORMAT_BGRA8888:
3757 DRM_DEBUG_KMS("unsupported pixel format\n");
3763 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3766 * drm_format_num_planes - get the number of planes for format
3767 * @format: pixel format (DRM_FORMAT_*)
3770 * The number of planes used by the specified pixel format.
3772 int drm_format_num_planes(uint32_t format)
3775 case DRM_FORMAT_YUV410:
3776 case DRM_FORMAT_YVU410:
3777 case DRM_FORMAT_YUV411:
3778 case DRM_FORMAT_YVU411:
3779 case DRM_FORMAT_YUV420:
3780 case DRM_FORMAT_YVU420:
3781 case DRM_FORMAT_YUV422:
3782 case DRM_FORMAT_YVU422:
3783 case DRM_FORMAT_YUV444:
3784 case DRM_FORMAT_YVU444:
3786 case DRM_FORMAT_NV12:
3787 case DRM_FORMAT_NV21:
3788 case DRM_FORMAT_NV16:
3789 case DRM_FORMAT_NV61:
3790 case DRM_FORMAT_NV24:
3791 case DRM_FORMAT_NV42:
3797 EXPORT_SYMBOL(drm_format_num_planes);
3800 * drm_format_plane_cpp - determine the bytes per pixel value
3801 * @format: pixel format (DRM_FORMAT_*)
3802 * @plane: plane index
3805 * The bytes per pixel value for the specified plane.
3807 int drm_format_plane_cpp(uint32_t format, int plane)
3812 if (plane >= drm_format_num_planes(format))
3816 case DRM_FORMAT_YUYV:
3817 case DRM_FORMAT_YVYU:
3818 case DRM_FORMAT_UYVY:
3819 case DRM_FORMAT_VYUY:
3821 case DRM_FORMAT_NV12:
3822 case DRM_FORMAT_NV21:
3823 case DRM_FORMAT_NV16:
3824 case DRM_FORMAT_NV61:
3825 case DRM_FORMAT_NV24:
3826 case DRM_FORMAT_NV42:
3827 return plane ? 2 : 1;
3828 case DRM_FORMAT_YUV410:
3829 case DRM_FORMAT_YVU410:
3830 case DRM_FORMAT_YUV411:
3831 case DRM_FORMAT_YVU411:
3832 case DRM_FORMAT_YUV420:
3833 case DRM_FORMAT_YVU420:
3834 case DRM_FORMAT_YUV422:
3835 case DRM_FORMAT_YVU422:
3836 case DRM_FORMAT_YUV444:
3837 case DRM_FORMAT_YVU444:
3840 drm_fb_get_bpp_depth(format, &depth, &bpp);
3844 EXPORT_SYMBOL(drm_format_plane_cpp);
3847 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3848 * @format: pixel format (DRM_FORMAT_*)
3851 * The horizontal chroma subsampling factor for the
3852 * specified pixel format.
3854 int drm_format_horz_chroma_subsampling(uint32_t format)
3857 case DRM_FORMAT_YUV411:
3858 case DRM_FORMAT_YVU411:
3859 case DRM_FORMAT_YUV410:
3860 case DRM_FORMAT_YVU410:
3862 case DRM_FORMAT_YUYV:
3863 case DRM_FORMAT_YVYU:
3864 case DRM_FORMAT_UYVY:
3865 case DRM_FORMAT_VYUY:
3866 case DRM_FORMAT_NV12:
3867 case DRM_FORMAT_NV21:
3868 case DRM_FORMAT_NV16:
3869 case DRM_FORMAT_NV61:
3870 case DRM_FORMAT_YUV422:
3871 case DRM_FORMAT_YVU422:
3872 case DRM_FORMAT_YUV420:
3873 case DRM_FORMAT_YVU420:
3879 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3882 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3883 * @format: pixel format (DRM_FORMAT_*)
3886 * The vertical chroma subsampling factor for the
3887 * specified pixel format.
3889 int drm_format_vert_chroma_subsampling(uint32_t format)
3892 case DRM_FORMAT_YUV410:
3893 case DRM_FORMAT_YVU410:
3895 case DRM_FORMAT_YUV420:
3896 case DRM_FORMAT_YVU420:
3897 case DRM_FORMAT_NV12:
3898 case DRM_FORMAT_NV21:
3904 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);