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>
33 #include <linux/err.h>
34 #include <linux/list.h>
35 #include <linux/export.h>
36 #include <linux/kernel.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_edid.h>
40 #include <uapi_drm/drm_fourcc.h>
42 /* Avoid boilerplate. I'm tired of typing. */
43 #define DRM_ENUM_NAME_FN(fnname, list) \
44 char *fnname(int val) \
47 for (i = 0; i < ARRAY_SIZE(list); i++) { \
48 if (list[i].type == val) \
49 return list[i].name; \
57 static struct drm_prop_enum_list drm_dpms_enum_list[] =
58 { { DRM_MODE_DPMS_ON, "On" },
59 { DRM_MODE_DPMS_STANDBY, "Standby" },
60 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
61 { DRM_MODE_DPMS_OFF, "Off" }
64 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
69 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
71 { DRM_MODE_SCALE_NONE, "None" },
72 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
73 { DRM_MODE_SCALE_CENTER, "Center" },
74 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
77 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
79 { DRM_MODE_DITHERING_OFF, "Off" },
80 { DRM_MODE_DITHERING_ON, "On" },
81 { DRM_MODE_DITHERING_AUTO, "Automatic" },
85 * Non-global properties, but "required" for certain connectors.
87 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
89 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
90 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
91 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
94 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
96 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
98 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
99 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
100 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
103 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
104 drm_dvi_i_subconnector_enum_list)
106 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
108 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
109 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
111 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
112 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
115 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
117 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
119 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
120 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
123 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
126 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
127 drm_tv_subconnector_enum_list)
129 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
130 { DRM_MODE_DIRTY_OFF, "Off" },
131 { DRM_MODE_DIRTY_ON, "On" },
132 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
135 struct drm_conn_prop_enum_list {
142 * Connector and encoder types.
144 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
145 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
146 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
147 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
148 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
149 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
150 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
151 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
152 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
153 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
154 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
155 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
156 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
157 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
158 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
159 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
160 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
163 static struct drm_prop_enum_list drm_encoder_enum_list[] =
164 { { DRM_MODE_ENCODER_NONE, "None" },
165 { DRM_MODE_ENCODER_DAC, "DAC" },
166 { DRM_MODE_ENCODER_TMDS, "TMDS" },
167 { DRM_MODE_ENCODER_LVDS, "LVDS" },
168 { DRM_MODE_ENCODER_TVDAC, "TV" },
169 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
172 char *drm_get_encoder_name(struct drm_encoder *encoder)
176 ksnprintf(buf, 32, "%s-%d",
177 drm_encoder_enum_list[encoder->encoder_type].name,
181 EXPORT_SYMBOL(drm_get_encoder_name);
183 char *drm_get_connector_name(struct drm_connector *connector)
187 ksnprintf(buf, 32, "%s-%d",
188 drm_connector_enum_list[connector->connector_type].name,
189 connector->connector_type_id);
192 EXPORT_SYMBOL(drm_get_connector_name);
194 char *drm_get_connector_status_name(enum drm_connector_status status)
196 if (status == connector_status_connected)
198 else if (status == connector_status_disconnected)
199 return "disconnected";
205 * drm_mode_object_get - allocate a new identifier
207 * @ptr: object pointer, used to generate unique ID
212 * Create a unique identifier based on @ptr in @dev's identifier space. Used
213 * for tracking modes, CRTCs and connectors.
216 * New unique (relative to other objects in @dev) integer identifier for the
219 static int drm_mode_object_get(struct drm_device *dev,
220 struct drm_mode_object *obj, uint32_t obj_type)
226 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
227 DRM_ERROR("Ran out memory getting a mode number\n");
231 lockmgr(&dev->mode_config.idr_mutex, LK_EXCLUSIVE);
232 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
233 lockmgr(&dev->mode_config.idr_mutex, LK_RELEASE);
240 obj->type = obj_type;
245 * drm_mode_object_put - free an identifer
250 * Caller must hold DRM mode_config lock.
252 * Free @id from @dev's unique identifier pool.
254 static void drm_mode_object_put(struct drm_device *dev,
255 struct drm_mode_object *object)
257 lockmgr(&dev->mode_config.idr_mutex, LK_EXCLUSIVE);
258 idr_remove(&dev->mode_config.crtc_idr, object->id);
259 lockmgr(&dev->mode_config.idr_mutex, LK_RELEASE);
262 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
263 uint32_t id, uint32_t type)
265 struct drm_mode_object *obj = NULL;
267 lockmgr(&dev->mode_config.idr_mutex, LK_EXCLUSIVE);
268 obj = idr_find(&dev->mode_config.crtc_idr, id);
269 if (!obj || (obj->type != type) || (obj->id != id))
271 lockmgr(&dev->mode_config.idr_mutex, LK_RELEASE);
275 EXPORT_SYMBOL(drm_mode_object_find);
278 * drm_framebuffer_init - initialize a framebuffer
282 * Caller must hold mode config lock.
284 * Allocates an ID for the framebuffer's parent mode object, sets its mode
285 * functions & device file and adds it to the master fd list.
288 * Zero on success, error code on failure.
290 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
291 const struct drm_framebuffer_funcs *funcs)
295 kref_init(&fb->refcount);
297 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
303 dev->mode_config.num_fb++;
304 list_add(&fb->head, &dev->mode_config.fb_list);
308 EXPORT_SYMBOL(drm_framebuffer_init);
310 static void drm_framebuffer_free(struct kref *kref)
312 struct drm_framebuffer *fb =
313 container_of(kref, struct drm_framebuffer, refcount);
314 fb->funcs->destroy(fb);
318 * drm_framebuffer_unreference - unref a framebuffer
321 * Caller must hold mode config lock.
323 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
325 struct drm_device *dev = fb->dev;
326 DRM_DEBUG("FB ID: %d\n", fb->base.id);
327 WARN_ON(!lockstatus(&dev->mode_config.mutex, curthread));
328 kref_put(&fb->refcount, drm_framebuffer_free);
330 EXPORT_SYMBOL(drm_framebuffer_unreference);
333 * drm_framebuffer_reference - incr the fb refcnt
335 void drm_framebuffer_reference(struct drm_framebuffer *fb)
337 DRM_DEBUG("FB ID: %d\n", fb->base.id);
338 kref_get(&fb->refcount);
340 EXPORT_SYMBOL(drm_framebuffer_reference);
343 * drm_framebuffer_cleanup - remove a framebuffer object
344 * @fb: framebuffer to remove
347 * Caller must hold mode config lock.
349 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
350 * it, setting it to NULL.
352 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
354 struct drm_device *dev = fb->dev;
356 * This could be moved to drm_framebuffer_remove(), but for
357 * debugging is nice to keep around the list of fb's that are
358 * no longer associated w/ a drm_file but are not unreferenced
359 * yet. (i915 and omapdrm have debugfs files which will show
362 drm_mode_object_put(dev, &fb->base);
364 dev->mode_config.num_fb--;
366 EXPORT_SYMBOL(drm_framebuffer_cleanup);
369 * drm_framebuffer_remove - remove and unreference a framebuffer object
370 * @fb: framebuffer to remove
373 * Caller must hold mode config lock.
375 * Scans all the CRTCs and planes in @dev's mode_config. If they're
376 * using @fb, removes it, setting it to NULL.
378 void drm_framebuffer_remove(struct drm_framebuffer *fb)
380 struct drm_device *dev = fb->dev;
381 struct drm_crtc *crtc;
382 struct drm_plane *plane;
383 struct drm_mode_set set;
386 /* remove from any CRTC */
387 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
388 if (crtc->fb == fb) {
389 /* should turn off the crtc */
390 memset(&set, 0, sizeof(struct drm_mode_set));
393 ret = crtc->funcs->set_config(&set);
395 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
399 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
400 if (plane->fb == fb) {
401 /* should turn off the crtc */
402 ret = plane->funcs->disable_plane(plane);
404 DRM_ERROR("failed to disable plane with busy fb\n");
405 /* disconnect the plane from the fb and crtc: */
411 list_del(&fb->filp_head);
413 drm_framebuffer_unreference(fb);
415 EXPORT_SYMBOL(drm_framebuffer_remove);
418 * drm_crtc_init - Initialise a new CRTC object
420 * @crtc: CRTC object to init
421 * @funcs: callbacks for the new CRTC
424 * Takes mode_config lock.
426 * Inits a new object created as base part of an driver crtc object.
429 * Zero on success, error code on failure.
431 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
432 const struct drm_crtc_funcs *funcs)
438 crtc->invert_dimensions = false;
440 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
442 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
446 crtc->base.properties = &crtc->properties;
448 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
449 dev->mode_config.num_crtc++;
452 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
456 EXPORT_SYMBOL(drm_crtc_init);
459 * drm_crtc_cleanup - Cleans up the core crtc usage.
460 * @crtc: CRTC to cleanup
463 * Caller must hold mode config lock.
465 * Cleanup @crtc. Removes from drm modesetting space
466 * does NOT free object, caller does that.
468 void drm_crtc_cleanup(struct drm_crtc *crtc)
470 struct drm_device *dev = crtc->dev;
472 drm_free(crtc->gamma_store, DRM_MEM_KMS);
473 crtc->gamma_store = NULL;
475 drm_mode_object_put(dev, &crtc->base);
476 list_del(&crtc->head);
477 dev->mode_config.num_crtc--;
479 EXPORT_SYMBOL(drm_crtc_cleanup);
482 * drm_mode_probed_add - add a mode to a connector's probed mode list
483 * @connector: connector the new mode
487 * Caller must hold mode config lock.
489 * Add @mode to @connector's mode list for later use.
491 void drm_mode_probed_add(struct drm_connector *connector,
492 struct drm_display_mode *mode)
494 list_add(&mode->head, &connector->probed_modes);
496 EXPORT_SYMBOL(drm_mode_probed_add);
499 * drm_mode_remove - remove and free a mode
500 * @connector: connector list to modify
501 * @mode: mode to remove
504 * Caller must hold mode config lock.
506 * Remove @mode from @connector's mode list, then free it.
508 void drm_mode_remove(struct drm_connector *connector,
509 struct drm_display_mode *mode)
511 list_del(&mode->head);
512 drm_mode_destroy(connector->dev, mode);
514 EXPORT_SYMBOL(drm_mode_remove);
517 * drm_connector_init - Init a preallocated connector
519 * @connector: the connector to init
520 * @funcs: callbacks for this connector
521 * @name: user visible name of the connector
524 * Takes mode config lock.
526 * Initialises a preallocated connector. Connectors should be
527 * subclassed as part of driver connector objects.
530 * Zero on success, error code on failure.
532 int drm_connector_init(struct drm_device *dev,
533 struct drm_connector *connector,
534 const struct drm_connector_funcs *funcs,
539 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
541 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
545 connector->base.properties = &connector->properties;
546 connector->dev = dev;
547 connector->funcs = funcs;
548 connector->connector_type = connector_type;
549 connector->connector_type_id =
550 ++drm_connector_enum_list[connector_type].count; /* TODO */
551 INIT_LIST_HEAD(&connector->user_modes);
552 INIT_LIST_HEAD(&connector->probed_modes);
553 INIT_LIST_HEAD(&connector->modes);
554 connector->edid_blob_ptr = NULL;
555 connector->status = connector_status_unknown;
557 list_add_tail(&connector->head, &dev->mode_config.connector_list);
558 dev->mode_config.num_connector++;
560 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
561 drm_object_attach_property(&connector->base,
562 dev->mode_config.edid_property,
565 drm_object_attach_property(&connector->base,
566 dev->mode_config.dpms_property, 0);
569 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
573 EXPORT_SYMBOL(drm_connector_init);
576 * drm_connector_cleanup - cleans up an initialised connector
577 * @connector: connector to cleanup
580 * Takes mode config lock.
582 * Cleans up the connector but doesn't free the object.
584 void drm_connector_cleanup(struct drm_connector *connector)
586 struct drm_device *dev = connector->dev;
587 struct drm_display_mode *mode, *t;
589 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
590 drm_mode_remove(connector, mode);
592 list_for_each_entry_safe(mode, t, &connector->modes, head)
593 drm_mode_remove(connector, mode);
595 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
596 drm_mode_remove(connector, mode);
598 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
599 drm_mode_object_put(dev, &connector->base);
600 list_del(&connector->head);
601 dev->mode_config.num_connector--;
602 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
604 EXPORT_SYMBOL(drm_connector_cleanup);
606 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);
617 EXPORT_SYMBOL(drm_connector_unplug_all);
619 int drm_encoder_init(struct drm_device *dev,
620 struct drm_encoder *encoder,
621 const struct drm_encoder_funcs *funcs,
626 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
628 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
633 encoder->encoder_type = encoder_type;
634 encoder->funcs = funcs;
636 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
637 dev->mode_config.num_encoder++;
640 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
644 EXPORT_SYMBOL(drm_encoder_init);
646 void drm_encoder_cleanup(struct drm_encoder *encoder)
648 struct drm_device *dev = encoder->dev;
649 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
650 drm_mode_object_put(dev, &encoder->base);
651 list_del(&encoder->head);
652 dev->mode_config.num_encoder--;
653 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
655 EXPORT_SYMBOL(drm_encoder_cleanup);
657 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
658 unsigned long possible_crtcs,
659 const struct drm_plane_funcs *funcs,
660 const uint32_t *formats, uint32_t format_count,
665 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
667 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
671 plane->base.properties = &plane->properties;
673 plane->funcs = funcs;
674 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
675 DRM_MEM_KMS, M_WAITOK);
676 if (!plane->format_types) {
677 DRM_DEBUG_KMS("out of memory when allocating plane\n");
678 drm_mode_object_put(dev, &plane->base);
683 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
684 plane->format_count = format_count;
685 plane->possible_crtcs = possible_crtcs;
687 /* private planes are not exposed to userspace, but depending on
688 * display hardware, might be convenient to allow sharing programming
689 * for the scanout engine with the crtc implementation.
692 list_add_tail(&plane->head, &dev->mode_config.plane_list);
693 dev->mode_config.num_plane++;
695 INIT_LIST_HEAD(&plane->head);
699 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
703 EXPORT_SYMBOL(drm_plane_init);
705 void drm_plane_cleanup(struct drm_plane *plane)
707 struct drm_device *dev = plane->dev;
709 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
710 drm_free(plane->format_types, DRM_MEM_KMS);
711 drm_mode_object_put(dev, &plane->base);
712 /* if not added to a list, it must be a private plane */
713 if (!list_empty(&plane->head)) {
714 list_del(&plane->head);
715 dev->mode_config.num_plane--;
717 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
719 EXPORT_SYMBOL(drm_plane_cleanup);
722 * drm_mode_create - create a new display mode
726 * Caller must hold DRM mode_config lock.
728 * Create a new drm_display_mode, give it an ID, and return it.
731 * Pointer to new mode on success, NULL on error.
733 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
735 struct drm_display_mode *nmode;
737 nmode = kmalloc(sizeof(struct drm_display_mode), DRM_MEM_KMS,
742 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
743 drm_free(nmode, DRM_MEM_KMS);
749 EXPORT_SYMBOL(drm_mode_create);
752 * drm_mode_destroy - remove a mode
754 * @mode: mode to remove
757 * Caller must hold mode config lock.
759 * Free @mode's unique identifier, then free it.
761 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
766 drm_mode_object_put(dev, &mode->base);
768 drm_free(mode, DRM_MEM_KMS);
770 EXPORT_SYMBOL(drm_mode_destroy);
772 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
774 struct drm_property *edid;
775 struct drm_property *dpms;
778 * Standard properties (apply to all connectors)
780 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
781 DRM_MODE_PROP_IMMUTABLE,
783 dev->mode_config.edid_property = edid;
785 dpms = drm_property_create_enum(dev, 0,
786 "DPMS", drm_dpms_enum_list,
787 ARRAY_SIZE(drm_dpms_enum_list));
788 dev->mode_config.dpms_property = dpms;
794 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
797 * Called by a driver the first time a DVI-I connector is made.
799 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
801 struct drm_property *dvi_i_selector;
802 struct drm_property *dvi_i_subconnector;
804 if (dev->mode_config.dvi_i_select_subconnector_property)
808 drm_property_create_enum(dev, 0,
809 "select subconnector",
810 drm_dvi_i_select_enum_list,
811 ARRAY_SIZE(drm_dvi_i_select_enum_list));
812 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
814 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
816 drm_dvi_i_subconnector_enum_list,
817 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
818 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
822 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
825 * drm_create_tv_properties - create TV specific connector properties
827 * @num_modes: number of different TV formats (modes) supported
828 * @modes: array of pointers to strings containing name of each format
830 * Called by a driver's TV initialization routine, this function creates
831 * the TV specific connector properties for a given device. Caller is
832 * responsible for allocating a list of format names and passing them to
835 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
838 struct drm_property *tv_selector;
839 struct drm_property *tv_subconnector;
842 if (dev->mode_config.tv_select_subconnector_property)
846 * Basic connector properties
848 tv_selector = drm_property_create_enum(dev, 0,
849 "select subconnector",
850 drm_tv_select_enum_list,
851 ARRAY_SIZE(drm_tv_select_enum_list));
852 dev->mode_config.tv_select_subconnector_property = tv_selector;
855 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
857 drm_tv_subconnector_enum_list,
858 ARRAY_SIZE(drm_tv_subconnector_enum_list));
859 dev->mode_config.tv_subconnector_property = tv_subconnector;
862 * Other, TV specific properties: margins & TV modes.
864 dev->mode_config.tv_left_margin_property =
865 drm_property_create_range(dev, 0, "left margin", 0, 100);
867 dev->mode_config.tv_right_margin_property =
868 drm_property_create_range(dev, 0, "right margin", 0, 100);
870 dev->mode_config.tv_top_margin_property =
871 drm_property_create_range(dev, 0, "top margin", 0, 100);
873 dev->mode_config.tv_bottom_margin_property =
874 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
876 dev->mode_config.tv_mode_property =
877 drm_property_create(dev, DRM_MODE_PROP_ENUM,
879 for (i = 0; i < num_modes; i++)
880 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
883 dev->mode_config.tv_brightness_property =
884 drm_property_create_range(dev, 0, "brightness", 0, 100);
886 dev->mode_config.tv_contrast_property =
887 drm_property_create_range(dev, 0, "contrast", 0, 100);
889 dev->mode_config.tv_flicker_reduction_property =
890 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
892 dev->mode_config.tv_overscan_property =
893 drm_property_create_range(dev, 0, "overscan", 0, 100);
895 dev->mode_config.tv_saturation_property =
896 drm_property_create_range(dev, 0, "saturation", 0, 100);
898 dev->mode_config.tv_hue_property =
899 drm_property_create_range(dev, 0, "hue", 0, 100);
903 EXPORT_SYMBOL(drm_mode_create_tv_properties);
906 * drm_mode_create_scaling_mode_property - create scaling mode property
909 * Called by a driver the first time it's needed, must be attached to desired
912 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
914 struct drm_property *scaling_mode;
916 if (dev->mode_config.scaling_mode_property)
920 drm_property_create_enum(dev, 0, "scaling mode",
921 drm_scaling_mode_enum_list,
922 ARRAY_SIZE(drm_scaling_mode_enum_list));
924 dev->mode_config.scaling_mode_property = scaling_mode;
928 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
931 * drm_mode_create_dithering_property - create dithering property
934 * Called by a driver the first time it's needed, must be attached to desired
937 int drm_mode_create_dithering_property(struct drm_device *dev)
939 struct drm_property *dithering_mode;
941 if (dev->mode_config.dithering_mode_property)
945 drm_property_create_enum(dev, 0, "dithering",
946 drm_dithering_mode_enum_list,
947 ARRAY_SIZE(drm_dithering_mode_enum_list));
948 dev->mode_config.dithering_mode_property = dithering_mode;
952 EXPORT_SYMBOL(drm_mode_create_dithering_property);
955 * drm_mode_create_dirty_property - create dirty property
958 * Called by a driver the first time it's needed, must be attached to desired
961 int drm_mode_create_dirty_info_property(struct drm_device *dev)
963 struct drm_property *dirty_info;
965 if (dev->mode_config.dirty_info_property)
969 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
971 drm_dirty_info_enum_list,
972 ARRAY_SIZE(drm_dirty_info_enum_list));
973 dev->mode_config.dirty_info_property = dirty_info;
977 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
980 * drm_mode_config_init - initialize DRM mode_configuration structure
984 * None, should happen single threaded at init time.
986 * Initialize @dev's mode_config structure, used for tracking the graphics
987 * configuration of @dev.
989 void drm_mode_config_init(struct drm_device *dev)
991 lockinit(&dev->mode_config.mutex, "kmslk", 0, LK_CANRECURSE);
992 lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE);
993 INIT_LIST_HEAD(&dev->mode_config.fb_list);
994 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
995 INIT_LIST_HEAD(&dev->mode_config.connector_list);
996 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
997 INIT_LIST_HEAD(&dev->mode_config.property_list);
998 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
999 INIT_LIST_HEAD(&dev->mode_config.plane_list);
1000 idr_init(&dev->mode_config.crtc_idr);
1002 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
1003 drm_mode_create_standard_connector_properties(dev);
1004 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
1006 /* Just to be sure */
1007 dev->mode_config.num_fb = 0;
1008 dev->mode_config.num_connector = 0;
1009 dev->mode_config.num_crtc = 0;
1010 dev->mode_config.num_encoder = 0;
1012 EXPORT_SYMBOL(drm_mode_config_init);
1014 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1016 uint32_t total_objects = 0;
1018 total_objects += dev->mode_config.num_crtc;
1019 total_objects += dev->mode_config.num_connector;
1020 total_objects += dev->mode_config.num_encoder;
1022 group->id_list = kmalloc(total_objects * sizeof(uint32_t),
1023 DRM_MEM_KMS, M_WAITOK | M_ZERO);
1024 if (!group->id_list)
1027 group->num_crtcs = 0;
1028 group->num_connectors = 0;
1029 group->num_encoders = 0;
1033 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1034 struct drm_mode_group *group)
1036 struct drm_crtc *crtc;
1037 struct drm_encoder *encoder;
1038 struct drm_connector *connector;
1041 if ((ret = drm_mode_group_init(dev, group)))
1044 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1045 group->id_list[group->num_crtcs++] = crtc->base.id;
1047 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1048 group->id_list[group->num_crtcs + group->num_encoders++] =
1051 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1052 group->id_list[group->num_crtcs + group->num_encoders +
1053 group->num_connectors++] = connector->base.id;
1057 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1060 * drm_mode_config_cleanup - free up DRM mode_config info
1064 * Caller must hold mode config lock.
1066 * Free up all the connectors and CRTCs associated with this DRM device, then
1067 * free up the framebuffers and associated buffer objects.
1069 * FIXME: cleanup any dangling user buffer objects too
1071 void drm_mode_config_cleanup(struct drm_device *dev)
1073 struct drm_connector *connector, *ot;
1074 struct drm_crtc *crtc, *ct;
1075 struct drm_encoder *encoder, *enct;
1076 struct drm_framebuffer *fb, *fbt;
1077 struct drm_property *property, *pt;
1078 struct drm_plane *plane, *plt;
1080 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1082 encoder->funcs->destroy(encoder);
1085 list_for_each_entry_safe(connector, ot,
1086 &dev->mode_config.connector_list, head) {
1087 connector->funcs->destroy(connector);
1090 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1092 drm_property_destroy(dev, property);
1095 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1096 drm_framebuffer_remove(fb);
1099 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1101 plane->funcs->destroy(plane);
1104 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1105 crtc->funcs->destroy(crtc);
1108 idr_remove_all(&dev->mode_config.crtc_idr);
1109 idr_destroy(&dev->mode_config.crtc_idr);
1111 EXPORT_SYMBOL(drm_mode_config_cleanup);
1114 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1115 * @out: drm_mode_modeinfo struct to return to the user
1116 * @in: drm_display_mode to use
1121 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1124 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1125 const struct drm_display_mode *in)
1127 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1128 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1129 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1130 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1131 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1132 "timing values too large for mode info\n");
1134 out->clock = in->clock;
1135 out->hdisplay = in->hdisplay;
1136 out->hsync_start = in->hsync_start;
1137 out->hsync_end = in->hsync_end;
1138 out->htotal = in->htotal;
1139 out->hskew = in->hskew;
1140 out->vdisplay = in->vdisplay;
1141 out->vsync_start = in->vsync_start;
1142 out->vsync_end = in->vsync_end;
1143 out->vtotal = in->vtotal;
1144 out->vscan = in->vscan;
1145 out->vrefresh = in->vrefresh;
1146 out->flags = in->flags;
1147 out->type = in->type;
1148 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1149 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1153 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1154 * @out: drm_display_mode to return to the user
1155 * @in: drm_mode_modeinfo to use
1160 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1164 * Zero on success, errno on failure.
1166 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1167 const struct drm_mode_modeinfo *in)
1169 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1172 out->clock = in->clock;
1173 out->hdisplay = in->hdisplay;
1174 out->hsync_start = in->hsync_start;
1175 out->hsync_end = in->hsync_end;
1176 out->htotal = in->htotal;
1177 out->hskew = in->hskew;
1178 out->vdisplay = in->vdisplay;
1179 out->vsync_start = in->vsync_start;
1180 out->vsync_end = in->vsync_end;
1181 out->vtotal = in->vtotal;
1182 out->vscan = in->vscan;
1183 out->vrefresh = in->vrefresh;
1184 out->flags = in->flags;
1185 out->type = in->type;
1186 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1187 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1193 * drm_mode_getresources - get graphics configuration
1194 * @inode: inode from the ioctl
1195 * @filp: file * from the ioctl
1196 * @cmd: cmd from ioctl
1197 * @arg: arg from ioctl
1200 * Takes mode config lock.
1202 * Construct a set of configuration description structures and return
1203 * them to the user, including CRTC, connector and framebuffer configuration.
1205 * Called by the user via ioctl.
1208 * Zero on success, errno on failure.
1210 int drm_mode_getresources(struct drm_device *dev, void *data,
1211 struct drm_file *file_priv)
1213 struct drm_mode_card_res *card_res = data;
1214 struct list_head *lh;
1215 struct drm_framebuffer *fb;
1216 struct drm_connector *connector;
1217 struct drm_crtc *crtc;
1218 struct drm_encoder *encoder;
1220 int connector_count = 0;
1223 int encoder_count = 0;
1225 uint32_t __user *fb_id;
1226 uint32_t __user *crtc_id;
1227 uint32_t __user *connector_id;
1228 uint32_t __user *encoder_id;
1229 struct drm_mode_group *mode_group;
1231 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1234 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
1237 * For the non-control nodes we need to limit the list of resources
1238 * by IDs in the group list for this node
1240 list_for_each(lh, &file_priv->fbs)
1244 mode_group = NULL; /* XXXKIB */
1245 if (1 || file_priv->master) {
1247 mode_group = &file_priv->masterp->minor->mode_group;
1248 if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
1251 list_for_each(lh, &dev->mode_config.crtc_list)
1254 list_for_each(lh, &dev->mode_config.connector_list)
1257 list_for_each(lh, &dev->mode_config.encoder_list)
1261 crtc_count = mode_group->num_crtcs;
1262 connector_count = mode_group->num_connectors;
1263 encoder_count = mode_group->num_encoders;
1266 card_res->max_height = dev->mode_config.max_height;
1267 card_res->min_height = dev->mode_config.min_height;
1268 card_res->max_width = dev->mode_config.max_width;
1269 card_res->min_width = dev->mode_config.min_width;
1271 /* handle this in 4 parts */
1273 if (card_res->count_fbs >= fb_count) {
1275 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1276 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1277 if (put_user(fb->base.id, fb_id + copied)) {
1284 card_res->count_fbs = fb_count;
1287 if (card_res->count_crtcs >= crtc_count) {
1289 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1291 if (1 || file_priv->master) {
1293 if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
1295 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1297 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1298 if (put_user(crtc->base.id, crtc_id + copied)) {
1305 for (i = 0; i < mode_group->num_crtcs; i++) {
1306 if (put_user(mode_group->id_list[i],
1307 crtc_id + copied)) {
1315 card_res->count_crtcs = crtc_count;
1318 if (card_res->count_encoders >= encoder_count) {
1320 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1322 if (file_priv->master) {
1324 if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
1326 list_for_each_entry(encoder,
1327 &dev->mode_config.encoder_list,
1329 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1330 drm_get_encoder_name(encoder));
1331 if (put_user(encoder->base.id, encoder_id +
1339 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1340 if (put_user(mode_group->id_list[i],
1341 encoder_id + copied)) {
1350 card_res->count_encoders = encoder_count;
1353 if (card_res->count_connectors >= connector_count) {
1355 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1357 if (file_priv->master) {
1359 if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
1361 list_for_each_entry(connector,
1362 &dev->mode_config.connector_list,
1364 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1366 drm_get_connector_name(connector));
1367 if (put_user(connector->base.id,
1368 connector_id + copied)) {
1375 int start = mode_group->num_crtcs +
1376 mode_group->num_encoders;
1377 for (i = start; i < start + mode_group->num_connectors; i++) {
1378 if (put_user(mode_group->id_list[i],
1379 connector_id + copied)) {
1387 card_res->count_connectors = connector_count;
1389 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1390 card_res->count_connectors, card_res->count_encoders);
1393 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
1398 * drm_mode_getcrtc - get CRTC configuration
1399 * @inode: inode from the ioctl
1400 * @filp: file * from the ioctl
1401 * @cmd: cmd from ioctl
1402 * @arg: arg from ioctl
1405 * Takes mode config lock.
1407 * Construct a CRTC configuration structure to return to the user.
1409 * Called by the user via ioctl.
1412 * Zero on success, errno on failure.
1414 int drm_mode_getcrtc(struct drm_device *dev,
1415 void *data, struct drm_file *file_priv)
1417 struct drm_mode_crtc *crtc_resp = data;
1418 struct drm_crtc *crtc;
1419 struct drm_mode_object *obj;
1422 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1425 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
1427 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1428 DRM_MODE_OBJECT_CRTC);
1433 crtc = obj_to_crtc(obj);
1435 crtc_resp->x = crtc->x;
1436 crtc_resp->y = crtc->y;
1437 crtc_resp->gamma_size = crtc->gamma_size;
1439 crtc_resp->fb_id = crtc->fb->base.id;
1441 crtc_resp->fb_id = 0;
1443 if (crtc->enabled) {
1445 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1446 crtc_resp->mode_valid = 1;
1449 crtc_resp->mode_valid = 0;
1453 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
1458 * drm_mode_getconnector - get connector configuration
1459 * @inode: inode from the ioctl
1460 * @filp: file * from the ioctl
1461 * @cmd: cmd from ioctl
1462 * @arg: arg from ioctl
1465 * Takes mode config lock.
1467 * Construct a connector configuration structure to return to the user.
1469 * Called by the user via ioctl.
1472 * Zero on success, errno on failure.
1474 int drm_mode_getconnector(struct drm_device *dev, void *data,
1475 struct drm_file *file_priv)
1477 struct drm_mode_get_connector *out_resp = data;
1478 struct drm_mode_object *obj;
1479 struct drm_connector *connector;
1480 struct drm_display_mode *mode;
1482 int props_count = 0;
1483 int encoders_count = 0;
1487 struct drm_mode_modeinfo u_mode;
1488 struct drm_mode_modeinfo __user *mode_ptr;
1489 uint32_t __user *prop_ptr;
1490 uint64_t __user *prop_values;
1491 uint32_t __user *encoder_ptr;
1493 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1496 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1498 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1500 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
1502 obj = drm_mode_object_find(dev, out_resp->connector_id,
1503 DRM_MODE_OBJECT_CONNECTOR);
1508 connector = obj_to_connector(obj);
1510 props_count = connector->properties.count;
1512 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1513 if (connector->encoder_ids[i] != 0) {
1518 if (out_resp->count_modes == 0) {
1519 connector->funcs->fill_modes(connector,
1520 dev->mode_config.max_width,
1521 dev->mode_config.max_height);
1524 /* delayed so we get modes regardless of pre-fill_modes state */
1525 list_for_each_entry(mode, &connector->modes, head)
1528 out_resp->connector_id = connector->base.id;
1529 out_resp->connector_type = connector->connector_type;
1530 out_resp->connector_type_id = connector->connector_type_id;
1531 out_resp->mm_width = connector->display_info.width_mm;
1532 out_resp->mm_height = connector->display_info.height_mm;
1533 out_resp->subpixel = connector->display_info.subpixel_order;
1534 out_resp->connection = connector->status;
1535 if (connector->encoder)
1536 out_resp->encoder_id = connector->encoder->base.id;
1538 out_resp->encoder_id = 0;
1541 * This ioctl is called twice, once to determine how much space is
1542 * needed, and the 2nd time to fill it.
1544 if ((out_resp->count_modes >= mode_count) && mode_count) {
1546 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1547 list_for_each_entry(mode, &connector->modes, head) {
1548 drm_crtc_convert_to_umode(&u_mode, mode);
1549 if (copy_to_user(mode_ptr + copied,
1550 &u_mode, sizeof(u_mode))) {
1557 out_resp->count_modes = mode_count;
1559 if ((out_resp->count_props >= props_count) && props_count) {
1561 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1562 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1563 for (i = 0; i < connector->properties.count; i++) {
1564 if (put_user(connector->properties.ids[i],
1565 prop_ptr + copied)) {
1570 if (put_user(connector->properties.values[i],
1571 prop_values + copied)) {
1578 out_resp->count_props = props_count;
1580 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1582 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1583 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1584 if (connector->encoder_ids[i] != 0) {
1585 if (put_user(connector->encoder_ids[i],
1586 encoder_ptr + copied)) {
1594 out_resp->count_encoders = encoders_count;
1597 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
1601 int drm_mode_getencoder(struct drm_device *dev, void *data,
1602 struct drm_file *file_priv)
1604 struct drm_mode_get_encoder *enc_resp = data;
1605 struct drm_mode_object *obj;
1606 struct drm_encoder *encoder;
1609 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1612 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
1613 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1614 DRM_MODE_OBJECT_ENCODER);
1619 encoder = obj_to_encoder(obj);
1622 enc_resp->crtc_id = encoder->crtc->base.id;
1624 enc_resp->crtc_id = 0;
1625 enc_resp->encoder_type = encoder->encoder_type;
1626 enc_resp->encoder_id = encoder->base.id;
1627 enc_resp->possible_crtcs = encoder->possible_crtcs;
1628 enc_resp->possible_clones = encoder->possible_clones;
1631 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
1636 * drm_mode_getplane_res - get plane info
1639 * @file_priv: DRM file info
1642 * Takes mode config lock.
1644 * Return an plane count and set of IDs.
1646 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1647 struct drm_file *file_priv)
1649 struct drm_mode_get_plane_res *plane_resp = data;
1650 struct drm_mode_config *config;
1651 struct drm_plane *plane;
1652 uint32_t __user *plane_ptr;
1653 int copied = 0, ret = 0;
1655 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1658 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
1659 config = &dev->mode_config;
1662 * This ioctl is called twice, once to determine how much space is
1663 * needed, and the 2nd time to fill it.
1665 if (config->num_plane &&
1666 (plane_resp->count_planes >= config->num_plane)) {
1667 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1669 list_for_each_entry(plane, &config->plane_list, head) {
1670 if (put_user(plane->base.id, plane_ptr + copied)) {
1677 plane_resp->count_planes = config->num_plane;
1680 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
1685 * drm_mode_getplane - get plane info
1688 * @file_priv: DRM file info
1691 * Takes mode config lock.
1693 * Return plane info, including formats supported, gamma size, any
1696 int drm_mode_getplane(struct drm_device *dev, void *data,
1697 struct drm_file *file_priv)
1699 struct drm_mode_get_plane *plane_resp = data;
1700 struct drm_mode_object *obj;
1701 struct drm_plane *plane;
1702 uint32_t __user *format_ptr;
1705 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1708 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
1709 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1710 DRM_MODE_OBJECT_PLANE);
1715 plane = obj_to_plane(obj);
1718 plane_resp->crtc_id = plane->crtc->base.id;
1720 plane_resp->crtc_id = 0;
1723 plane_resp->fb_id = plane->fb->base.id;
1725 plane_resp->fb_id = 0;
1727 plane_resp->plane_id = plane->base.id;
1728 plane_resp->possible_crtcs = plane->possible_crtcs;
1729 plane_resp->gamma_size = plane->gamma_size;
1732 * This ioctl is called twice, once to determine how much space is
1733 * needed, and the 2nd time to fill it.
1735 if (plane->format_count &&
1736 (plane_resp->count_format_types >= plane->format_count)) {
1737 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1738 if (copy_to_user(format_ptr,
1739 plane->format_types,
1740 sizeof(uint32_t) * plane->format_count)) {
1745 plane_resp->count_format_types = plane->format_count;
1748 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
1753 * drm_mode_setplane - set up or tear down an plane
1755 * @data: ioctl data*
1756 * @file_prive: DRM file info
1759 * Takes mode config lock.
1761 * Set plane info, including placement, fb, scaling, and other factors.
1762 * Or pass a NULL fb to disable.
1764 int drm_mode_setplane(struct drm_device *dev, void *data,
1765 struct drm_file *file_priv)
1767 struct drm_mode_set_plane *plane_req = data;
1768 struct drm_mode_object *obj;
1769 struct drm_plane *plane;
1770 struct drm_crtc *crtc;
1771 struct drm_framebuffer *fb;
1773 unsigned int fb_width, fb_height;
1776 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1779 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
1782 * First, find the plane, crtc, and fb objects. If not available,
1783 * we don't bother to call the driver.
1785 obj = drm_mode_object_find(dev, plane_req->plane_id,
1786 DRM_MODE_OBJECT_PLANE);
1788 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1789 plane_req->plane_id);
1793 plane = obj_to_plane(obj);
1795 /* No fb means shut it down */
1796 if (!plane_req->fb_id) {
1797 plane->funcs->disable_plane(plane);
1803 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1804 DRM_MODE_OBJECT_CRTC);
1806 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1807 plane_req->crtc_id);
1811 crtc = obj_to_crtc(obj);
1813 obj = drm_mode_object_find(dev, plane_req->fb_id,
1814 DRM_MODE_OBJECT_FB);
1816 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1821 fb = obj_to_fb(obj);
1823 /* Check whether this plane supports the fb pixel format. */
1824 for (i = 0; i < plane->format_count; i++)
1825 if (fb->pixel_format == plane->format_types[i])
1827 if (i == plane->format_count) {
1828 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1833 fb_width = fb->width << 16;
1834 fb_height = fb->height << 16;
1836 /* Make sure source coordinates are inside the fb. */
1837 if (plane_req->src_w > fb_width ||
1838 plane_req->src_x > fb_width - plane_req->src_w ||
1839 plane_req->src_h > fb_height ||
1840 plane_req->src_y > fb_height - plane_req->src_h) {
1841 DRM_DEBUG_KMS("Invalid source coordinates "
1842 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1843 plane_req->src_w >> 16,
1844 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1845 plane_req->src_h >> 16,
1846 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1847 plane_req->src_x >> 16,
1848 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1849 plane_req->src_y >> 16,
1850 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1855 /* Give drivers some help against integer overflows */
1856 if (plane_req->crtc_w > INT_MAX ||
1857 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1858 plane_req->crtc_h > INT_MAX ||
1859 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1860 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1861 plane_req->crtc_w, plane_req->crtc_h,
1862 plane_req->crtc_x, plane_req->crtc_y);
1867 ret = plane->funcs->update_plane(plane, crtc, fb,
1868 plane_req->crtc_x, plane_req->crtc_y,
1869 plane_req->crtc_w, plane_req->crtc_h,
1870 plane_req->src_x, plane_req->src_y,
1871 plane_req->src_w, plane_req->src_h);
1878 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
1884 * drm_mode_setcrtc - set CRTC configuration
1885 * @inode: inode from the ioctl
1886 * @filp: file * from the ioctl
1887 * @cmd: cmd from ioctl
1888 * @arg: arg from ioctl
1891 * Takes mode config lock.
1893 * Build a new CRTC configuration based on user request.
1895 * Called by the user via ioctl.
1898 * Zero on success, errno on failure.
1900 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1901 struct drm_file *file_priv)
1903 struct drm_mode_config *config = &dev->mode_config;
1904 struct drm_mode_crtc *crtc_req = data;
1905 struct drm_mode_object *obj;
1906 struct drm_crtc *crtc;
1907 struct drm_connector **connector_set = NULL, *connector;
1908 struct drm_framebuffer *fb = NULL;
1909 struct drm_display_mode *mode = NULL;
1910 struct drm_mode_set set;
1911 uint32_t __user *set_connectors_ptr;
1915 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1918 /* For some reason crtc x/y offsets are signed internally. */
1919 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1922 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
1923 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1924 DRM_MODE_OBJECT_CRTC);
1926 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1930 crtc = obj_to_crtc(obj);
1931 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1933 if (crtc_req->mode_valid) {
1934 int hdisplay, vdisplay;
1935 /* If we have a mode we need a framebuffer. */
1936 /* If we pass -1, set the mode with the currently bound fb */
1937 if (crtc_req->fb_id == -1) {
1939 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1945 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1946 DRM_MODE_OBJECT_FB);
1948 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1953 fb = obj_to_fb(obj);
1956 mode = drm_mode_create(dev);
1962 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1964 DRM_DEBUG_KMS("Invalid mode\n");
1968 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1970 hdisplay = mode->hdisplay;
1971 vdisplay = mode->vdisplay;
1973 if (crtc->invert_dimensions)
1974 swap(hdisplay, vdisplay);
1976 if (hdisplay > fb->width ||
1977 vdisplay > fb->height ||
1978 crtc_req->x > fb->width - hdisplay ||
1979 crtc_req->y > fb->height - vdisplay) {
1980 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1981 fb->width, fb->height,
1982 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1983 crtc->invert_dimensions ? " (inverted)" : "");
1989 if (crtc_req->count_connectors == 0 && mode) {
1990 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1995 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1996 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1997 crtc_req->count_connectors);
2002 if (crtc_req->count_connectors > 0) {
2005 /* Avoid unbounded kernel memory allocation */
2006 if (crtc_req->count_connectors > config->num_connector) {
2011 connector_set = kmalloc(crtc_req->count_connectors *
2012 sizeof(struct drm_connector *), DRM_MEM_KMS, M_WAITOK);
2013 if (!connector_set) {
2018 for (i = 0; i < crtc_req->count_connectors; i++) {
2019 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2020 if (get_user(out_id, &set_connectors_ptr[i])) {
2025 obj = drm_mode_object_find(dev, out_id,
2026 DRM_MODE_OBJECT_CONNECTOR);
2028 DRM_DEBUG_KMS("Connector id %d unknown\n",
2033 connector = obj_to_connector(obj);
2034 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2036 drm_get_connector_name(connector));
2038 connector_set[i] = connector;
2043 set.x = crtc_req->x;
2044 set.y = crtc_req->y;
2046 set.connectors = connector_set;
2047 set.num_connectors = crtc_req->count_connectors;
2049 ret = crtc->funcs->set_config(&set);
2052 drm_free(connector_set, DRM_MEM_KMS);
2053 drm_mode_destroy(dev, mode);
2054 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
2058 int drm_mode_cursor_ioctl(struct drm_device *dev,
2059 void *data, struct drm_file *file_priv)
2061 struct drm_mode_cursor *req = data;
2062 struct drm_mode_object *obj;
2063 struct drm_crtc *crtc;
2066 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2069 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2072 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
2073 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2075 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2079 crtc = obj_to_crtc(obj);
2081 if (req->flags & DRM_MODE_CURSOR_BO) {
2082 if (!crtc->funcs->cursor_set) {
2086 /* Turns off the cursor if handle is 0 */
2087 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2088 req->width, req->height);
2091 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2092 if (crtc->funcs->cursor_move) {
2093 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2100 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
2104 /* Original addfb only supported RGB formats, so figure out which one */
2105 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2111 fmt = DRM_FORMAT_RGB332;
2115 fmt = DRM_FORMAT_XRGB1555;
2117 fmt = DRM_FORMAT_RGB565;
2120 fmt = DRM_FORMAT_RGB888;
2124 fmt = DRM_FORMAT_XRGB8888;
2125 else if (depth == 30)
2126 fmt = DRM_FORMAT_XRGB2101010;
2128 fmt = DRM_FORMAT_ARGB8888;
2131 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2132 fmt = DRM_FORMAT_XRGB8888;
2138 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2141 * drm_mode_addfb - add an FB to the graphics configuration
2142 * @inode: inode from the ioctl
2143 * @filp: file * from the ioctl
2144 * @cmd: cmd from ioctl
2145 * @arg: arg from ioctl
2148 * Takes mode config lock.
2150 * Add a new FB to the specified CRTC, given a user request.
2152 * Called by the user via ioctl.
2155 * Zero on success, errno on failure.
2157 int drm_mode_addfb(struct drm_device *dev,
2158 void *data, struct drm_file *file_priv)
2160 struct drm_mode_fb_cmd *or = data;
2161 struct drm_mode_fb_cmd2 r = {};
2162 struct drm_mode_config *config = &dev->mode_config;
2163 struct drm_framebuffer *fb;
2166 /* Use new struct with format internally */
2167 r.fb_id = or->fb_id;
2168 r.width = or->width;
2169 r.height = or->height;
2170 r.pitches[0] = or->pitch;
2171 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2172 r.handles[0] = or->handle;
2174 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2177 if ((config->min_width > r.width) || (r.width > config->max_width))
2180 if ((config->min_height > r.height) || (r.height > config->max_height))
2183 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
2185 /* TODO check buffer is sufficiently large */
2186 /* TODO setup destructor callback */
2188 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2190 DRM_DEBUG_KMS("could not create framebuffer\n");
2195 or->fb_id = fb->base.id;
2196 list_add(&fb->filp_head, &file_priv->fbs);
2197 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2200 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
2204 static int format_check(const struct drm_mode_fb_cmd2 *r)
2206 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2210 case DRM_FORMAT_RGB332:
2211 case DRM_FORMAT_BGR233:
2212 case DRM_FORMAT_XRGB4444:
2213 case DRM_FORMAT_XBGR4444:
2214 case DRM_FORMAT_RGBX4444:
2215 case DRM_FORMAT_BGRX4444:
2216 case DRM_FORMAT_ARGB4444:
2217 case DRM_FORMAT_ABGR4444:
2218 case DRM_FORMAT_RGBA4444:
2219 case DRM_FORMAT_BGRA4444:
2220 case DRM_FORMAT_XRGB1555:
2221 case DRM_FORMAT_XBGR1555:
2222 case DRM_FORMAT_RGBX5551:
2223 case DRM_FORMAT_BGRX5551:
2224 case DRM_FORMAT_ARGB1555:
2225 case DRM_FORMAT_ABGR1555:
2226 case DRM_FORMAT_RGBA5551:
2227 case DRM_FORMAT_BGRA5551:
2228 case DRM_FORMAT_RGB565:
2229 case DRM_FORMAT_BGR565:
2230 case DRM_FORMAT_RGB888:
2231 case DRM_FORMAT_BGR888:
2232 case DRM_FORMAT_XRGB8888:
2233 case DRM_FORMAT_XBGR8888:
2234 case DRM_FORMAT_RGBX8888:
2235 case DRM_FORMAT_BGRX8888:
2236 case DRM_FORMAT_ARGB8888:
2237 case DRM_FORMAT_ABGR8888:
2238 case DRM_FORMAT_RGBA8888:
2239 case DRM_FORMAT_BGRA8888:
2240 case DRM_FORMAT_XRGB2101010:
2241 case DRM_FORMAT_XBGR2101010:
2242 case DRM_FORMAT_RGBX1010102:
2243 case DRM_FORMAT_BGRX1010102:
2244 case DRM_FORMAT_ARGB2101010:
2245 case DRM_FORMAT_ABGR2101010:
2246 case DRM_FORMAT_RGBA1010102:
2247 case DRM_FORMAT_BGRA1010102:
2248 case DRM_FORMAT_YUYV:
2249 case DRM_FORMAT_YVYU:
2250 case DRM_FORMAT_UYVY:
2251 case DRM_FORMAT_VYUY:
2252 case DRM_FORMAT_AYUV:
2253 case DRM_FORMAT_NV12:
2254 case DRM_FORMAT_NV21:
2255 case DRM_FORMAT_NV16:
2256 case DRM_FORMAT_NV61:
2257 case DRM_FORMAT_NV24:
2258 case DRM_FORMAT_NV42:
2259 case DRM_FORMAT_YUV410:
2260 case DRM_FORMAT_YVU410:
2261 case DRM_FORMAT_YUV411:
2262 case DRM_FORMAT_YVU411:
2263 case DRM_FORMAT_YUV420:
2264 case DRM_FORMAT_YVU420:
2265 case DRM_FORMAT_YUV422:
2266 case DRM_FORMAT_YVU422:
2267 case DRM_FORMAT_YUV444:
2268 case DRM_FORMAT_YVU444:
2275 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2277 int ret, hsub, vsub, num_planes, i;
2279 ret = format_check(r);
2281 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
2285 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2286 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2287 num_planes = drm_format_num_planes(r->pixel_format);
2289 if (r->width == 0 || r->width % hsub) {
2290 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2294 if (r->height == 0 || r->height % vsub) {
2295 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2299 for (i = 0; i < num_planes; i++) {
2300 unsigned int width = r->width / (i != 0 ? hsub : 1);
2301 unsigned int height = r->height / (i != 0 ? vsub : 1);
2302 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2304 if (!r->handles[i]) {
2305 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2309 if ((uint64_t) width * cpp > UINT_MAX)
2312 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2315 if (r->pitches[i] < width * cpp) {
2316 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2325 * drm_mode_addfb2 - add an FB to the graphics configuration
2326 * @inode: inode from the ioctl
2327 * @filp: file * from the ioctl
2328 * @cmd: cmd from ioctl
2329 * @arg: arg from ioctl
2332 * Takes mode config lock.
2334 * Add a new FB to the specified CRTC, given a user request with format.
2336 * Called by the user via ioctl.
2339 * Zero on success, errno on failure.
2341 int drm_mode_addfb2(struct drm_device *dev,
2342 void *data, struct drm_file *file_priv)
2344 struct drm_mode_fb_cmd2 *r = data;
2345 struct drm_mode_config *config = &dev->mode_config;
2346 struct drm_framebuffer *fb;
2349 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2352 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2353 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2357 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2358 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2359 r->width, config->min_width, config->max_width);
2362 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2363 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2364 r->height, config->min_height, config->max_height);
2368 ret = framebuffer_check(r);
2372 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
2374 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2376 DRM_DEBUG_KMS("could not create framebuffer\n");
2381 r->fb_id = fb->base.id;
2382 list_add(&fb->filp_head, &file_priv->fbs);
2383 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2386 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
2391 * drm_mode_rmfb - remove an FB from the configuration
2392 * @inode: inode from the ioctl
2393 * @filp: file * from the ioctl
2394 * @cmd: cmd from ioctl
2395 * @arg: arg from ioctl
2398 * Takes mode config lock.
2400 * Remove the FB specified by the user.
2402 * Called by the user via ioctl.
2405 * Zero on success, errno on failure.
2407 int drm_mode_rmfb(struct drm_device *dev,
2408 void *data, struct drm_file *file_priv)
2410 struct drm_mode_object *obj;
2411 struct drm_framebuffer *fb = NULL;
2412 struct drm_framebuffer *fbl = NULL;
2413 uint32_t *id = data;
2417 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2420 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
2421 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
2422 /* TODO check that we really get a framebuffer back. */
2427 fb = obj_to_fb(obj);
2429 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2438 drm_framebuffer_remove(fb);
2441 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
2446 * drm_mode_getfb - get FB info
2447 * @inode: inode from the ioctl
2448 * @filp: file * from the ioctl
2449 * @cmd: cmd from ioctl
2450 * @arg: arg from ioctl
2453 * Takes mode config lock.
2455 * Lookup the FB given its ID and return info about it.
2457 * Called by the user via ioctl.
2460 * Zero on success, errno on failure.
2462 int drm_mode_getfb(struct drm_device *dev,
2463 void *data, struct drm_file *file_priv)
2465 struct drm_mode_fb_cmd *r = data;
2466 struct drm_mode_object *obj;
2467 struct drm_framebuffer *fb;
2470 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2473 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
2474 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2479 fb = obj_to_fb(obj);
2481 r->height = fb->height;
2482 r->width = fb->width;
2483 r->depth = fb->depth;
2484 r->bpp = fb->bits_per_pixel;
2485 r->pitch = fb->pitches[0];
2486 fb->funcs->create_handle(fb, file_priv, &r->handle);
2489 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
2493 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2494 void *data, struct drm_file *file_priv)
2496 struct drm_clip_rect __user *clips_ptr;
2497 struct drm_clip_rect *clips = NULL;
2498 struct drm_mode_fb_dirty_cmd *r = data;
2499 struct drm_mode_object *obj;
2500 struct drm_framebuffer *fb;
2505 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2508 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
2509 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2514 fb = obj_to_fb(obj);
2516 num_clips = r->num_clips;
2517 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2519 if (!num_clips != !clips_ptr) {
2524 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2526 /* If userspace annotates copy, clips must come in pairs */
2527 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2532 if (num_clips && clips_ptr) {
2533 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2537 clips = kmalloc(num_clips * sizeof(*clips), DRM_MEM_KMS,
2544 ret = copy_from_user(clips, clips_ptr,
2545 num_clips * sizeof(*clips));
2552 if (fb->funcs->dirty) {
2553 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2561 drm_free(clips, DRM_MEM_KMS);
2563 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
2569 * drm_fb_release - remove and free the FBs on this file
2570 * @filp: file * from the ioctl
2573 * Takes mode config lock.
2575 * Destroy all the FBs associated with @filp.
2577 * Called by the user via ioctl.
2580 * Zero on success, errno on failure.
2582 void drm_fb_release(struct drm_file *priv)
2585 struct drm_device *dev = priv->dev;
2587 struct drm_device *dev = priv->minor->dev;
2589 struct drm_framebuffer *fb, *tfb;
2591 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
2592 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2593 drm_framebuffer_remove(fb);
2595 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
2599 * drm_mode_attachmode - add a mode to the user mode list
2601 * @connector: connector to add the mode to
2602 * @mode: mode to add
2604 * Add @mode to @connector's user mode list.
2606 static void drm_mode_attachmode(struct drm_device *dev,
2607 struct drm_connector *connector,
2608 struct drm_display_mode *mode)
2610 list_add_tail(&mode->head, &connector->user_modes);
2613 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2614 const struct drm_display_mode *mode)
2616 struct drm_connector *connector;
2618 struct drm_display_mode *dup_mode, *next;
2619 LINUX_LIST_HEAD(list);
2621 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2622 if (!connector->encoder)
2624 if (connector->encoder->crtc == crtc) {
2625 dup_mode = drm_mode_duplicate(dev, mode);
2630 list_add_tail(&dup_mode->head, &list);
2634 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2635 if (!connector->encoder)
2637 if (connector->encoder->crtc == crtc)
2638 list_move_tail(list.next, &connector->user_modes);
2641 WARN_ON(!list_empty(&list));
2644 list_for_each_entry_safe(dup_mode, next, &list, head)
2645 drm_mode_destroy(dev, dup_mode);
2649 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2651 static int drm_mode_detachmode(struct drm_device *dev,
2652 struct drm_connector *connector,
2653 struct drm_display_mode *mode)
2657 struct drm_display_mode *match_mode, *t;
2659 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2660 if (drm_mode_equal(match_mode, mode)) {
2661 list_del(&match_mode->head);
2662 drm_mode_destroy(dev, match_mode);
2674 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2676 struct drm_connector *connector;
2678 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2679 drm_mode_detachmode(dev, connector, mode);
2683 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2686 * drm_fb_attachmode - Attach a user mode to an connector
2687 * @inode: inode from the ioctl
2688 * @filp: file * from the ioctl
2689 * @cmd: cmd from ioctl
2690 * @arg: arg from ioctl
2692 * This attaches a user specified mode to an connector.
2693 * Called by the user via ioctl.
2696 * Zero on success, errno on failure.
2698 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2699 void *data, struct drm_file *file_priv)
2701 struct drm_mode_mode_cmd *mode_cmd = data;
2702 struct drm_connector *connector;
2703 struct drm_display_mode *mode;
2704 struct drm_mode_object *obj;
2705 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2708 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2711 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
2713 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2718 connector = obj_to_connector(obj);
2720 mode = drm_mode_create(dev);
2726 ret = drm_crtc_convert_umode(mode, umode);
2728 DRM_DEBUG_KMS("Invalid mode\n");
2729 drm_mode_destroy(dev, mode);
2733 drm_mode_attachmode(dev, connector, mode);
2735 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
2741 * drm_fb_detachmode - Detach a user specified mode from an connector
2742 * @inode: inode from the ioctl
2743 * @filp: file * from the ioctl
2744 * @cmd: cmd from ioctl
2745 * @arg: arg from ioctl
2747 * Called by the user via ioctl.
2750 * Zero on success, errno on failure.
2752 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2753 void *data, struct drm_file *file_priv)
2755 struct drm_mode_object *obj;
2756 struct drm_mode_mode_cmd *mode_cmd = data;
2757 struct drm_connector *connector;
2758 struct drm_display_mode mode;
2759 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2762 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2765 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
2767 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2772 connector = obj_to_connector(obj);
2774 ret = drm_crtc_convert_umode(&mode, umode);
2776 DRM_DEBUG_KMS("Invalid mode\n");
2780 ret = drm_mode_detachmode(dev, connector, &mode);
2782 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
2786 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2787 const char *name, int num_values)
2789 struct drm_property *property = NULL;
2792 property = kmalloc(sizeof(struct drm_property), DRM_MEM_KMS,
2798 property->values = kmalloc(sizeof(uint64_t)*num_values, DRM_MEM_KMS,
2800 if (!property->values)
2804 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2808 property->flags = flags;
2809 property->num_values = num_values;
2810 INIT_LIST_HEAD(&property->enum_blob_list);
2813 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2814 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2817 list_add_tail(&property->head, &dev->mode_config.property_list);
2820 drm_free(property->values, DRM_MEM_KMS);
2821 drm_free(property, DRM_MEM_KMS);
2824 EXPORT_SYMBOL(drm_property_create);
2826 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2828 const struct drm_prop_enum_list *props,
2831 struct drm_property *property;
2834 flags |= DRM_MODE_PROP_ENUM;
2836 property = drm_property_create(dev, flags, name, num_values);
2840 for (i = 0; i < num_values; i++) {
2841 ret = drm_property_add_enum(property, i,
2845 drm_property_destroy(dev, property);
2852 EXPORT_SYMBOL(drm_property_create_enum);
2854 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2855 int flags, const char *name,
2856 const struct drm_prop_enum_list *props,
2859 struct drm_property *property;
2862 flags |= DRM_MODE_PROP_BITMASK;
2864 property = drm_property_create(dev, flags, name, num_values);
2868 for (i = 0; i < num_values; i++) {
2869 ret = drm_property_add_enum(property, i,
2873 drm_property_destroy(dev, property);
2880 EXPORT_SYMBOL(drm_property_create_bitmask);
2882 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2884 uint64_t min, uint64_t max)
2886 struct drm_property *property;
2888 flags |= DRM_MODE_PROP_RANGE;
2890 property = drm_property_create(dev, flags, name, 2);
2894 property->values[0] = min;
2895 property->values[1] = max;
2899 EXPORT_SYMBOL(drm_property_create_range);
2901 int drm_property_add_enum(struct drm_property *property, int index,
2902 uint64_t value, const char *name)
2904 struct drm_property_enum *prop_enum;
2906 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2910 * Bitmask enum properties have the additional constraint of values
2913 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2916 if (!list_empty(&property->enum_blob_list)) {
2917 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2918 if (prop_enum->value == value) {
2919 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2920 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2926 prop_enum = kmalloc(sizeof(struct drm_property_enum), DRM_MEM_KMS,
2931 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2932 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2933 prop_enum->value = value;
2935 property->values[index] = value;
2936 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2939 EXPORT_SYMBOL(drm_property_add_enum);
2941 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2943 struct drm_property_enum *prop_enum, *pt;
2945 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2946 list_del(&prop_enum->head);
2947 drm_free(prop_enum, DRM_MEM_KMS);
2950 if (property->num_values)
2951 drm_free(property->values, DRM_MEM_KMS);
2952 drm_mode_object_put(dev, &property->base);
2953 list_del(&property->head);
2954 drm_free(property, DRM_MEM_KMS);
2956 EXPORT_SYMBOL(drm_property_destroy);
2958 void drm_object_attach_property(struct drm_mode_object *obj,
2959 struct drm_property *property,
2962 int count = obj->properties->count;
2964 if (count == DRM_OBJECT_MAX_PROPERTY) {
2965 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2966 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2967 "you see this message on the same object type.\n",
2972 obj->properties->ids[count] = property->base.id;
2973 obj->properties->values[count] = init_val;
2974 obj->properties->count++;
2976 EXPORT_SYMBOL(drm_object_attach_property);
2978 int drm_object_property_set_value(struct drm_mode_object *obj,
2979 struct drm_property *property, uint64_t val)
2983 for (i = 0; i < obj->properties->count; i++) {
2984 if (obj->properties->ids[i] == property->base.id) {
2985 obj->properties->values[i] = val;
2992 EXPORT_SYMBOL(drm_object_property_set_value);
2994 int drm_object_property_get_value(struct drm_mode_object *obj,
2995 struct drm_property *property, uint64_t *val)
2999 for (i = 0; i < obj->properties->count; i++) {
3000 if (obj->properties->ids[i] == property->base.id) {
3001 *val = obj->properties->values[i];
3008 EXPORT_SYMBOL(drm_object_property_get_value);
3010 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3011 void *data, struct drm_file *file_priv)
3013 struct drm_mode_object *obj;
3014 struct drm_mode_get_property *out_resp = data;
3015 struct drm_property *property;
3018 int value_count = 0;
3021 struct drm_property_enum *prop_enum;
3022 struct drm_mode_property_enum __user *enum_ptr;
3023 struct drm_property_blob *prop_blob;
3024 uint32_t __user *blob_id_ptr;
3025 uint64_t __user *values_ptr;
3026 uint32_t __user *blob_length_ptr;
3028 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3031 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
3032 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3037 property = obj_to_property(obj);
3039 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3040 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3042 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3043 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3047 value_count = property->num_values;
3049 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3050 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3051 out_resp->flags = property->flags;
3053 if ((out_resp->count_values >= value_count) && value_count) {
3054 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3055 for (i = 0; i < value_count; i++) {
3056 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3062 out_resp->count_values = value_count;
3064 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3065 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3067 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3068 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3070 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3075 if (copy_to_user(&enum_ptr[copied].name,
3076 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3083 out_resp->count_enum_blobs = enum_count;
3086 if (property->flags & DRM_MODE_PROP_BLOB) {
3087 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3089 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3090 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3092 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3093 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3098 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3106 out_resp->count_enum_blobs = blob_count;
3109 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
3113 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3116 struct drm_property_blob *blob;
3119 if (!length || !data)
3122 blob = kmalloc(sizeof(struct drm_property_blob) + length, DRM_MEM_KMS,
3127 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3129 drm_free(blob, DRM_MEM_KMS);
3133 blob->length = length;
3135 memcpy(blob->data, data, length);
3137 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3141 static void drm_property_destroy_blob(struct drm_device *dev,
3142 struct drm_property_blob *blob)
3144 drm_mode_object_put(dev, &blob->base);
3145 list_del(&blob->head);
3146 drm_free(blob, DRM_MEM_KMS);
3149 int drm_mode_getblob_ioctl(struct drm_device *dev,
3150 void *data, struct drm_file *file_priv)
3152 struct drm_mode_object *obj;
3153 struct drm_mode_get_blob *out_resp = data;
3154 struct drm_property_blob *blob;
3156 void __user *blob_ptr;
3158 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3161 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
3162 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3167 blob = obj_to_blob(obj);
3169 if (out_resp->length == blob->length) {
3170 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3171 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3176 out_resp->length = blob->length;
3179 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
3183 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3186 struct drm_device *dev = connector->dev;
3189 if (connector->edid_blob_ptr)
3190 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3192 /* Delete edid, when there is none. */
3194 connector->edid_blob_ptr = NULL;
3195 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3199 size = EDID_LENGTH * (1 + edid->extensions);
3200 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3202 if (!connector->edid_blob_ptr)
3205 ret = drm_object_property_set_value(&connector->base,
3206 dev->mode_config.edid_property,
3207 connector->edid_blob_ptr->base.id);
3211 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3213 static bool drm_property_change_is_valid(struct drm_property *property,
3216 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3218 if (property->flags & DRM_MODE_PROP_RANGE) {
3219 if (value < property->values[0] || value > property->values[1])
3222 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3224 uint64_t valid_mask = 0;
3225 for (i = 0; i < property->num_values; i++)
3226 valid_mask |= (1ULL << property->values[i]);
3227 return !(value & ~valid_mask);
3228 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3229 /* Only the driver knows */
3233 for (i = 0; i < property->num_values; i++)
3234 if (property->values[i] == value)
3240 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3241 void *data, struct drm_file *file_priv)
3243 struct drm_mode_connector_set_property *conn_set_prop = data;
3244 struct drm_mode_obj_set_property obj_set_prop = {
3245 .value = conn_set_prop->value,
3246 .prop_id = conn_set_prop->prop_id,
3247 .obj_id = conn_set_prop->connector_id,
3248 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3251 /* It does all the locking and checking we need */
3252 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3255 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3256 struct drm_property *property,
3260 struct drm_connector *connector = obj_to_connector(obj);
3262 /* Do DPMS ourselves */
3263 if (property == connector->dev->mode_config.dpms_property) {
3264 if (connector->funcs->dpms)
3265 (*connector->funcs->dpms)(connector, (int)value);
3267 } else if (connector->funcs->set_property)
3268 ret = connector->funcs->set_property(connector, property, value);
3270 /* store the property value if successful */
3272 drm_object_property_set_value(&connector->base, property, value);
3276 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3277 struct drm_property *property,
3281 struct drm_crtc *crtc = obj_to_crtc(obj);
3283 if (crtc->funcs->set_property)
3284 ret = crtc->funcs->set_property(crtc, property, value);
3286 drm_object_property_set_value(obj, property, value);
3291 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3292 struct drm_property *property,
3296 struct drm_plane *plane = obj_to_plane(obj);
3298 if (plane->funcs->set_property)
3299 ret = plane->funcs->set_property(plane, property, value);
3301 drm_object_property_set_value(obj, property, value);
3306 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3307 struct drm_file *file_priv)
3309 struct drm_mode_obj_get_properties *arg = data;
3310 struct drm_mode_object *obj;
3314 int props_count = 0;
3315 uint32_t __user *props_ptr;
3316 uint64_t __user *prop_values_ptr;
3318 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3321 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
3323 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3328 if (!obj->properties) {
3333 props_count = obj->properties->count;
3335 /* This ioctl is called twice, once to determine how much space is
3336 * needed, and the 2nd time to fill it. */
3337 if ((arg->count_props >= props_count) && props_count) {
3339 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3340 prop_values_ptr = (uint64_t __user *)(unsigned long)
3341 (arg->prop_values_ptr);
3342 for (i = 0; i < props_count; i++) {
3343 if (put_user(obj->properties->ids[i],
3344 props_ptr + copied)) {
3348 if (put_user(obj->properties->values[i],
3349 prop_values_ptr + copied)) {
3356 arg->count_props = props_count;
3358 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
3362 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3363 struct drm_file *file_priv)
3365 struct drm_mode_obj_set_property *arg = data;
3366 struct drm_mode_object *arg_obj;
3367 struct drm_mode_object *prop_obj;
3368 struct drm_property *property;
3372 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3375 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
3377 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3380 if (!arg_obj->properties)
3383 for (i = 0; i < arg_obj->properties->count; i++)
3384 if (arg_obj->properties->ids[i] == arg->prop_id)
3387 if (i == arg_obj->properties->count)
3390 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3391 DRM_MODE_OBJECT_PROPERTY);
3394 property = obj_to_property(prop_obj);
3396 if (!drm_property_change_is_valid(property, arg->value))
3399 switch (arg_obj->type) {
3400 case DRM_MODE_OBJECT_CONNECTOR:
3401 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3404 case DRM_MODE_OBJECT_CRTC:
3405 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3407 case DRM_MODE_OBJECT_PLANE:
3408 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3413 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
3417 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3418 struct drm_encoder *encoder)
3422 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3423 if (connector->encoder_ids[i] == 0) {
3424 connector->encoder_ids[i] = encoder->base.id;
3430 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3432 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3433 struct drm_encoder *encoder)
3436 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3437 if (connector->encoder_ids[i] == encoder->base.id) {
3438 connector->encoder_ids[i] = 0;
3439 if (connector->encoder == encoder)
3440 connector->encoder = NULL;
3445 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3447 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3450 crtc->gamma_size = gamma_size;
3452 crtc->gamma_store = kmalloc(gamma_size * sizeof(uint16_t) * 3,
3453 DRM_MEM_KMS, M_WAITOK | M_ZERO);
3454 if (!crtc->gamma_store) {
3455 crtc->gamma_size = 0;
3461 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3463 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3464 void *data, struct drm_file *file_priv)
3466 struct drm_mode_crtc_lut *crtc_lut = data;
3467 struct drm_mode_object *obj;
3468 struct drm_crtc *crtc;
3469 void *r_base, *g_base, *b_base;
3473 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3476 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
3477 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3482 crtc = obj_to_crtc(obj);
3484 if (crtc->funcs->gamma_set == NULL) {
3489 /* memcpy into gamma store */
3490 if (crtc_lut->gamma_size != crtc->gamma_size) {
3495 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3496 r_base = crtc->gamma_store;
3497 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3502 g_base = (char *)r_base + size;
3503 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3508 b_base = (char *)g_base + size;
3509 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3514 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3517 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
3522 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3523 void *data, struct drm_file *file_priv)
3525 struct drm_mode_crtc_lut *crtc_lut = data;
3526 struct drm_mode_object *obj;
3527 struct drm_crtc *crtc;
3528 void *r_base, *g_base, *b_base;
3532 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3535 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
3536 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3541 crtc = obj_to_crtc(obj);
3543 /* memcpy into gamma store */
3544 if (crtc_lut->gamma_size != crtc->gamma_size) {
3549 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3550 r_base = crtc->gamma_store;
3551 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3556 g_base = (char *)r_base + size;
3557 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3562 b_base = (char *)g_base + size;
3563 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3568 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
3573 drm_kms_free(void *arg)
3576 drm_free(arg, DRM_MEM_KMS);
3579 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3580 void *data, struct drm_file *file_priv)
3582 struct drm_mode_crtc_page_flip *page_flip = data;
3583 struct drm_mode_object *obj;
3584 struct drm_crtc *crtc;
3585 struct drm_framebuffer *fb;
3586 struct drm_pending_vblank_event *e = NULL;
3587 int hdisplay, vdisplay;
3590 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3591 page_flip->reserved != 0)
3594 lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
3595 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3598 crtc = obj_to_crtc(obj);
3600 if (crtc->fb == NULL) {
3601 /* The framebuffer is currently unbound, presumably
3602 * due to a hotplug event, that userspace has not
3609 if (crtc->funcs->page_flip == NULL)
3612 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3615 fb = obj_to_fb(obj);
3617 hdisplay = crtc->mode.hdisplay;
3618 vdisplay = crtc->mode.vdisplay;
3620 if (crtc->invert_dimensions)
3621 swap(hdisplay, vdisplay);
3623 if (hdisplay > fb->width ||
3624 vdisplay > fb->height ||
3625 crtc->x > fb->width - hdisplay ||
3626 crtc->y > fb->height - vdisplay) {
3627 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3628 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3629 crtc->invert_dimensions ? " (inverted)" : "");
3634 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3636 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3637 if (file_priv->event_space < sizeof e->event) {
3638 lockmgr(&dev->event_lock, LK_RELEASE);
3641 file_priv->event_space -= sizeof e->event;
3642 lockmgr(&dev->event_lock, LK_RELEASE);
3644 e = kmalloc(sizeof *e, DRM_MEM_KMS, M_WAITOK | M_ZERO);
3646 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3647 file_priv->event_space += sizeof e->event;
3648 lockmgr(&dev->event_lock, LK_RELEASE);
3652 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3653 e->event.base.length = sizeof e->event;
3654 e->event.user_data = page_flip->user_data;
3655 e->base.event = &e->event.base;
3656 e->base.file_priv = file_priv;
3658 (void (*) (struct drm_pending_event *))drm_kms_free;
3661 ret = crtc->funcs->page_flip(crtc, fb, e);
3663 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3664 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3665 file_priv->event_space += sizeof e->event;
3666 lockmgr(&dev->event_lock, LK_RELEASE);
3667 drm_free(e, DRM_MEM_KMS);
3672 lockmgr(&dev->mode_config.mutex, LK_RELEASE);
3676 void drm_mode_config_reset(struct drm_device *dev)
3678 struct drm_crtc *crtc;
3679 struct drm_encoder *encoder;
3680 struct drm_connector *connector;
3682 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3683 if (crtc->funcs->reset)
3684 crtc->funcs->reset(crtc);
3686 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3687 if (encoder->funcs->reset)
3688 encoder->funcs->reset(encoder);
3690 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3691 connector->status = connector_status_unknown;
3693 if (connector->funcs->reset)
3694 connector->funcs->reset(connector);
3697 EXPORT_SYMBOL(drm_mode_config_reset);
3699 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3700 void *data, struct drm_file *file_priv)
3702 struct drm_mode_create_dumb *args = data;
3704 if (!dev->driver->dumb_create)
3706 return dev->driver->dumb_create(file_priv, dev, args);
3709 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3710 void *data, struct drm_file *file_priv)
3712 struct drm_mode_map_dumb *args = data;
3714 /* call driver ioctl to get mmap offset */
3715 if (!dev->driver->dumb_map_offset)
3718 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3721 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3722 void *data, struct drm_file *file_priv)
3724 struct drm_mode_destroy_dumb *args = data;
3726 if (!dev->driver->dumb_destroy)
3729 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3733 * Just need to support RGB formats here for compat with code that doesn't
3734 * use pixel formats directly yet.
3736 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3740 case DRM_FORMAT_RGB332:
3741 case DRM_FORMAT_BGR233:
3745 case DRM_FORMAT_XRGB1555:
3746 case DRM_FORMAT_XBGR1555:
3747 case DRM_FORMAT_RGBX5551:
3748 case DRM_FORMAT_BGRX5551:
3749 case DRM_FORMAT_ARGB1555:
3750 case DRM_FORMAT_ABGR1555:
3751 case DRM_FORMAT_RGBA5551:
3752 case DRM_FORMAT_BGRA5551:
3756 case DRM_FORMAT_RGB565:
3757 case DRM_FORMAT_BGR565:
3761 case DRM_FORMAT_RGB888:
3762 case DRM_FORMAT_BGR888:
3766 case DRM_FORMAT_XRGB8888:
3767 case DRM_FORMAT_XBGR8888:
3768 case DRM_FORMAT_RGBX8888:
3769 case DRM_FORMAT_BGRX8888:
3773 case DRM_FORMAT_XRGB2101010:
3774 case DRM_FORMAT_XBGR2101010:
3775 case DRM_FORMAT_RGBX1010102:
3776 case DRM_FORMAT_BGRX1010102:
3777 case DRM_FORMAT_ARGB2101010:
3778 case DRM_FORMAT_ABGR2101010:
3779 case DRM_FORMAT_RGBA1010102:
3780 case DRM_FORMAT_BGRA1010102:
3784 case DRM_FORMAT_ARGB8888:
3785 case DRM_FORMAT_ABGR8888:
3786 case DRM_FORMAT_RGBA8888:
3787 case DRM_FORMAT_BGRA8888:
3792 DRM_DEBUG_KMS("unsupported pixel format\n");
3798 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3801 * drm_format_num_planes - get the number of planes for format
3802 * @format: pixel format (DRM_FORMAT_*)
3805 * The number of planes used by the specified pixel format.
3807 int drm_format_num_planes(uint32_t format)
3810 case DRM_FORMAT_YUV410:
3811 case DRM_FORMAT_YVU410:
3812 case DRM_FORMAT_YUV411:
3813 case DRM_FORMAT_YVU411:
3814 case DRM_FORMAT_YUV420:
3815 case DRM_FORMAT_YVU420:
3816 case DRM_FORMAT_YUV422:
3817 case DRM_FORMAT_YVU422:
3818 case DRM_FORMAT_YUV444:
3819 case DRM_FORMAT_YVU444:
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:
3832 EXPORT_SYMBOL(drm_format_num_planes);
3835 * drm_format_plane_cpp - determine the bytes per pixel value
3836 * @format: pixel format (DRM_FORMAT_*)
3837 * @plane: plane index
3840 * The bytes per pixel value for the specified plane.
3842 int drm_format_plane_cpp(uint32_t format, int plane)
3847 if (plane >= drm_format_num_planes(format))
3851 case DRM_FORMAT_YUYV:
3852 case DRM_FORMAT_YVYU:
3853 case DRM_FORMAT_UYVY:
3854 case DRM_FORMAT_VYUY:
3856 case DRM_FORMAT_NV12:
3857 case DRM_FORMAT_NV21:
3858 case DRM_FORMAT_NV16:
3859 case DRM_FORMAT_NV61:
3860 case DRM_FORMAT_NV24:
3861 case DRM_FORMAT_NV42:
3862 return plane ? 2 : 1;
3863 case DRM_FORMAT_YUV410:
3864 case DRM_FORMAT_YVU410:
3865 case DRM_FORMAT_YUV411:
3866 case DRM_FORMAT_YVU411:
3867 case DRM_FORMAT_YUV420:
3868 case DRM_FORMAT_YVU420:
3869 case DRM_FORMAT_YUV422:
3870 case DRM_FORMAT_YVU422:
3871 case DRM_FORMAT_YUV444:
3872 case DRM_FORMAT_YVU444:
3875 drm_fb_get_bpp_depth(format, &depth, &bpp);
3879 EXPORT_SYMBOL(drm_format_plane_cpp);
3882 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3883 * @format: pixel format (DRM_FORMAT_*)
3886 * The horizontal chroma subsampling factor for the
3887 * specified pixel format.
3889 int drm_format_horz_chroma_subsampling(uint32_t format)
3892 case DRM_FORMAT_YUV411:
3893 case DRM_FORMAT_YVU411:
3894 case DRM_FORMAT_YUV410:
3895 case DRM_FORMAT_YVU410:
3897 case DRM_FORMAT_YUYV:
3898 case DRM_FORMAT_YVYU:
3899 case DRM_FORMAT_UYVY:
3900 case DRM_FORMAT_VYUY:
3901 case DRM_FORMAT_NV12:
3902 case DRM_FORMAT_NV21:
3903 case DRM_FORMAT_NV16:
3904 case DRM_FORMAT_NV61:
3905 case DRM_FORMAT_YUV422:
3906 case DRM_FORMAT_YVU422:
3907 case DRM_FORMAT_YUV420:
3908 case DRM_FORMAT_YVU420:
3914 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3917 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3918 * @format: pixel format (DRM_FORMAT_*)
3921 * The vertical chroma subsampling factor for the
3922 * specified pixel format.
3924 int drm_format_vert_chroma_subsampling(uint32_t format)
3927 case DRM_FORMAT_YUV410:
3928 case DRM_FORMAT_YVU410:
3930 case DRM_FORMAT_YUV420:
3931 case DRM_FORMAT_YVU420:
3932 case DRM_FORMAT_NV12:
3933 case DRM_FORMAT_NV21:
3939 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);