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 <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <dev/drm2/drmP.h>
36 #include <dev/drm2/drm_crtc.h>
37 #include <dev/drm2/drm_edid.h>
38 #include <dev/drm2/drm_fourcc.h>
40 static void drm_property_destroy_blob(struct drm_device *dev,
41 struct drm_property_blob *blob);
43 /* Avoid boilerplate. I'm tired of typing. */
44 #define DRM_ENUM_NAME_FN(fnname, list) \
45 char *fnname(int val) \
48 for (i = 0; i < ARRAY_SIZE(list); i++) { \
49 if (list[i].type == val) \
50 return list[i].name; \
58 static struct drm_prop_enum_list drm_dpms_enum_list[] =
59 { { DRM_MODE_DPMS_ON, "On" },
60 { DRM_MODE_DPMS_STANDBY, "Standby" },
61 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
62 { DRM_MODE_DPMS_OFF, "Off" }
65 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
70 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
72 { DRM_MODE_SCALE_NONE, "None" },
73 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
74 { DRM_MODE_SCALE_CENTER, "Center" },
75 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
78 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
80 { DRM_MODE_DITHERING_OFF, "Off" },
81 { DRM_MODE_DITHERING_ON, "On" },
82 { DRM_MODE_DITHERING_AUTO, "Automatic" },
86 * Non-global properties, but "required" for certain connectors.
88 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
90 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
91 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
92 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
95 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
97 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
99 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
100 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
101 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
104 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
105 drm_dvi_i_subconnector_enum_list)
107 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
109 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
110 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
111 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
112 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
113 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
116 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
118 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
120 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
121 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
123 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
124 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
127 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
128 drm_tv_subconnector_enum_list)
130 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
131 { DRM_MODE_DIRTY_OFF, "Off" },
132 { DRM_MODE_DIRTY_ON, "On" },
133 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
136 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
137 drm_dirty_info_enum_list)
139 struct drm_conn_prop_enum_list {
146 * Connector and encoder types.
148 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
149 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
150 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
151 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
152 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
153 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
154 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
155 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
156 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
157 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
158 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
159 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
160 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
161 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
162 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
163 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
164 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
167 static struct drm_prop_enum_list drm_encoder_enum_list[] =
168 { { DRM_MODE_ENCODER_NONE, "None" },
169 { DRM_MODE_ENCODER_DAC, "DAC" },
170 { DRM_MODE_ENCODER_TMDS, "TMDS" },
171 { DRM_MODE_ENCODER_LVDS, "LVDS" },
172 { DRM_MODE_ENCODER_TVDAC, "TV" },
173 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
176 char *drm_get_encoder_name(struct drm_encoder *encoder)
180 snprintf(buf, 32, "%s-%d",
181 drm_encoder_enum_list[encoder->encoder_type].name,
185 EXPORT_SYMBOL(drm_get_encoder_name);
187 char *drm_get_connector_name(struct drm_connector *connector)
191 snprintf(buf, 32, "%s-%d",
192 drm_connector_enum_list[connector->connector_type].name,
193 connector->connector_type_id);
196 EXPORT_SYMBOL(drm_get_connector_name);
198 char *drm_get_connector_status_name(enum drm_connector_status status)
200 if (status == connector_status_connected)
202 else if (status == connector_status_disconnected)
203 return "disconnected";
209 * drm_mode_object_get - allocate a new identifier
211 * @ptr: object pointer, used to generate unique ID
216 * Create a unique identifier based on @ptr in @dev's identifier space. Used
217 * for tracking modes, CRTCs and connectors.
220 * New unique (relative to other objects in @dev) integer identifier for the
223 static int drm_mode_object_get(struct drm_device *dev,
224 struct drm_mode_object *obj, uint32_t obj_type)
229 ret = drm_gem_name_create(&dev->mode_config.crtc_names, obj, &new_id);
234 obj->type = obj_type;
239 * drm_mode_object_put - free an identifer
244 * Caller must hold DRM mode_config lock.
246 * Free @id from @dev's unique identifier pool.
248 static void drm_mode_object_put(struct drm_device *dev,
249 struct drm_mode_object *object)
252 drm_gem_names_remove(&dev->mode_config.crtc_names, object->id);
255 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
256 uint32_t id, uint32_t type)
258 struct drm_mode_object *obj = NULL;
260 obj = drm_gem_name_ref(&dev->mode_config.crtc_names, id, NULL);
261 if (!obj || (obj->type != type) || (obj->id != id))
266 EXPORT_SYMBOL(drm_mode_object_find);
269 * drm_framebuffer_init - initialize a framebuffer
273 * Caller must hold mode config lock.
275 * Allocates an ID for the framebuffer's parent mode object, sets its mode
276 * functions & device file and adds it to the master fd list.
279 * Zero on success, error code on failure.
281 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
282 const struct drm_framebuffer_funcs *funcs)
286 refcount_init(&fb->refcount, 1);
288 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
294 dev->mode_config.num_fb++;
295 list_add(&fb->head, &dev->mode_config.fb_list);
299 EXPORT_SYMBOL(drm_framebuffer_init);
301 static void drm_framebuffer_free(struct drm_framebuffer *fb)
303 fb->funcs->destroy(fb);
307 * drm_framebuffer_unreference - unref a framebuffer
310 * Caller must hold mode config lock.
312 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
314 struct drm_device *dev = fb->dev;
315 DRM_DEBUG("FB ID: %d\n", fb->base.id);
316 if (!sx_xlocked(&dev->mode_config.mutex))
317 DRM_WARNING("%s: dev->mode_config.mutex not locked\n", __func__);
318 if (refcount_release(&fb->refcount))
319 drm_framebuffer_free(fb);
321 EXPORT_SYMBOL(drm_framebuffer_unreference);
324 * drm_framebuffer_reference - incr the fb refcnt
326 void drm_framebuffer_reference(struct drm_framebuffer *fb)
328 DRM_DEBUG("FB ID: %d\n", fb->base.id);
329 refcount_acquire(&fb->refcount);
331 EXPORT_SYMBOL(drm_framebuffer_reference);
334 * drm_framebuffer_cleanup - remove a framebuffer object
335 * @fb: framebuffer to remove
338 * Caller must hold mode config lock.
340 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
341 * it, setting it to NULL.
343 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
345 struct drm_device *dev = fb->dev;
347 * This could be moved to drm_framebuffer_remove(), but for
348 * debugging is nice to keep around the list of fb's that are
349 * no longer associated w/ a drm_file but are not unreferenced
350 * yet. (i915 and omapdrm have debugfs files which will show
353 drm_mode_object_put(dev, &fb->base);
355 dev->mode_config.num_fb--;
357 EXPORT_SYMBOL(drm_framebuffer_cleanup);
360 * drm_framebuffer_remove - remove and unreference a framebuffer object
361 * @fb: framebuffer to remove
364 * Caller must hold mode config lock.
366 * Scans all the CRTCs and planes in @dev's mode_config. If they're
367 * using @fb, removes it, setting it to NULL.
369 void drm_framebuffer_remove(struct drm_framebuffer *fb)
371 struct drm_device *dev = fb->dev;
372 struct drm_crtc *crtc;
373 struct drm_plane *plane;
374 struct drm_mode_set set;
377 /* remove from any CRTC */
378 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
379 if (crtc->fb == fb) {
380 /* should turn off the crtc */
381 memset(&set, 0, sizeof(struct drm_mode_set));
384 ret = crtc->funcs->set_config(&set);
386 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
390 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
391 if (plane->fb == fb) {
392 /* should turn off the crtc */
393 ret = plane->funcs->disable_plane(plane);
395 DRM_ERROR("failed to disable plane with busy fb\n");
396 /* disconnect the plane from the fb and crtc: */
402 list_del(&fb->filp_head);
404 drm_framebuffer_unreference(fb);
406 EXPORT_SYMBOL(drm_framebuffer_remove);
409 * drm_crtc_init - Initialise a new CRTC object
411 * @crtc: CRTC object to init
412 * @funcs: callbacks for the new CRTC
415 * Takes mode_config lock.
417 * Inits a new object created as base part of an driver crtc object.
420 * Zero on success, error code on failure.
422 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
423 const struct drm_crtc_funcs *funcs)
429 crtc->invert_dimensions = false;
431 sx_xlock(&dev->mode_config.mutex);
433 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
437 crtc->base.properties = &crtc->properties;
439 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
440 dev->mode_config.num_crtc++;
443 sx_xunlock(&dev->mode_config.mutex);
447 EXPORT_SYMBOL(drm_crtc_init);
450 * drm_crtc_cleanup - Cleans up the core crtc usage.
451 * @crtc: CRTC to cleanup
454 * Caller must hold mode config lock.
456 * Cleanup @crtc. Removes from drm modesetting space
457 * does NOT free object, caller does that.
459 void drm_crtc_cleanup(struct drm_crtc *crtc)
461 struct drm_device *dev = crtc->dev;
463 free(crtc->gamma_store, DRM_MEM_KMS);
464 crtc->gamma_store = NULL;
466 drm_mode_object_put(dev, &crtc->base);
467 list_del(&crtc->head);
468 dev->mode_config.num_crtc--;
470 EXPORT_SYMBOL(drm_crtc_cleanup);
473 * drm_mode_probed_add - add a mode to a connector's probed mode list
474 * @connector: connector the new mode
478 * Caller must hold mode config lock.
480 * Add @mode to @connector's mode list for later use.
482 void drm_mode_probed_add(struct drm_connector *connector,
483 struct drm_display_mode *mode)
485 list_add(&mode->head, &connector->probed_modes);
487 EXPORT_SYMBOL(drm_mode_probed_add);
490 * drm_mode_remove - remove and free a mode
491 * @connector: connector list to modify
492 * @mode: mode to remove
495 * Caller must hold mode config lock.
497 * Remove @mode from @connector's mode list, then free it.
499 void drm_mode_remove(struct drm_connector *connector,
500 struct drm_display_mode *mode)
502 list_del(&mode->head);
503 drm_mode_destroy(connector->dev, mode);
505 EXPORT_SYMBOL(drm_mode_remove);
508 * drm_connector_init - Init a preallocated connector
510 * @connector: the connector to init
511 * @funcs: callbacks for this connector
512 * @name: user visible name of the connector
515 * Takes mode config lock.
517 * Initialises a preallocated connector. Connectors should be
518 * subclassed as part of driver connector objects.
521 * Zero on success, error code on failure.
523 int drm_connector_init(struct drm_device *dev,
524 struct drm_connector *connector,
525 const struct drm_connector_funcs *funcs,
530 sx_xlock(&dev->mode_config.mutex);
532 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
536 connector->base.properties = &connector->properties;
537 connector->dev = dev;
538 connector->funcs = funcs;
539 connector->connector_type = connector_type;
540 connector->connector_type_id =
541 ++drm_connector_enum_list[connector_type].count; /* TODO */
542 INIT_LIST_HEAD(&connector->user_modes);
543 INIT_LIST_HEAD(&connector->probed_modes);
544 INIT_LIST_HEAD(&connector->modes);
545 connector->edid_blob_ptr = NULL;
546 connector->status = connector_status_unknown;
548 list_add_tail(&connector->head, &dev->mode_config.connector_list);
549 dev->mode_config.num_connector++;
551 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
552 drm_object_attach_property(&connector->base,
553 dev->mode_config.edid_property,
556 drm_object_attach_property(&connector->base,
557 dev->mode_config.dpms_property, 0);
560 sx_xunlock(&dev->mode_config.mutex);
564 EXPORT_SYMBOL(drm_connector_init);
567 * drm_connector_cleanup - cleans up an initialised connector
568 * @connector: connector to cleanup
571 * Takes mode config lock.
573 * Cleans up the connector but doesn't free the object.
575 void drm_connector_cleanup(struct drm_connector *connector)
577 struct drm_device *dev = connector->dev;
578 struct drm_display_mode *mode, *t;
580 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
581 drm_mode_remove(connector, mode);
583 list_for_each_entry_safe(mode, t, &connector->modes, head)
584 drm_mode_remove(connector, mode);
586 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
587 drm_mode_remove(connector, mode);
589 sx_xlock(&dev->mode_config.mutex);
590 drm_mode_object_put(dev, &connector->base);
591 list_del(&connector->head);
592 dev->mode_config.num_connector--;
593 sx_xunlock(&dev->mode_config.mutex);
595 EXPORT_SYMBOL(drm_connector_cleanup);
597 void drm_connector_unplug_all(struct drm_device *dev)
599 #ifdef FREEBSD_NOTYET
600 struct drm_connector *connector;
602 /* taking the mode config mutex ends up in a clash with sysfs */
603 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
604 drm_sysfs_connector_remove(connector);
605 #endif /* FREEBSD_NOTYET */
608 EXPORT_SYMBOL(drm_connector_unplug_all);
610 int drm_encoder_init(struct drm_device *dev,
611 struct drm_encoder *encoder,
612 const struct drm_encoder_funcs *funcs,
617 sx_xlock(&dev->mode_config.mutex);
619 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
624 encoder->encoder_type = encoder_type;
625 encoder->funcs = funcs;
627 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
628 dev->mode_config.num_encoder++;
631 sx_xunlock(&dev->mode_config.mutex);
635 EXPORT_SYMBOL(drm_encoder_init);
637 void drm_encoder_cleanup(struct drm_encoder *encoder)
639 struct drm_device *dev = encoder->dev;
640 sx_xlock(&dev->mode_config.mutex);
641 drm_mode_object_put(dev, &encoder->base);
642 list_del(&encoder->head);
643 dev->mode_config.num_encoder--;
644 sx_xunlock(&dev->mode_config.mutex);
646 EXPORT_SYMBOL(drm_encoder_cleanup);
648 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
649 unsigned long possible_crtcs,
650 const struct drm_plane_funcs *funcs,
651 const uint32_t *formats, uint32_t format_count,
656 sx_xlock(&dev->mode_config.mutex);
658 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
662 plane->base.properties = &plane->properties;
664 plane->funcs = funcs;
665 plane->format_types = malloc(sizeof(uint32_t) * format_count,
666 DRM_MEM_KMS, M_WAITOK);
667 if (!plane->format_types) {
668 DRM_DEBUG_KMS("out of memory when allocating plane\n");
669 drm_mode_object_put(dev, &plane->base);
674 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
675 plane->format_count = format_count;
676 plane->possible_crtcs = possible_crtcs;
678 /* private planes are not exposed to userspace, but depending on
679 * display hardware, might be convenient to allow sharing programming
680 * for the scanout engine with the crtc implementation.
683 list_add_tail(&plane->head, &dev->mode_config.plane_list);
684 dev->mode_config.num_plane++;
686 INIT_LIST_HEAD(&plane->head);
690 sx_xunlock(&dev->mode_config.mutex);
694 EXPORT_SYMBOL(drm_plane_init);
696 void drm_plane_cleanup(struct drm_plane *plane)
698 struct drm_device *dev = plane->dev;
700 sx_xlock(&dev->mode_config.mutex);
701 free(plane->format_types, DRM_MEM_KMS);
702 drm_mode_object_put(dev, &plane->base);
703 /* if not added to a list, it must be a private plane */
704 if (!list_empty(&plane->head)) {
705 list_del(&plane->head);
706 dev->mode_config.num_plane--;
708 sx_xunlock(&dev->mode_config.mutex);
710 EXPORT_SYMBOL(drm_plane_cleanup);
713 * drm_mode_create - create a new display mode
717 * Caller must hold DRM mode_config lock.
719 * Create a new drm_display_mode, give it an ID, and return it.
722 * Pointer to new mode on success, NULL on error.
724 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
726 struct drm_display_mode *nmode;
728 nmode = malloc(sizeof(struct drm_display_mode), DRM_MEM_KMS,
733 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
734 free(nmode, DRM_MEM_KMS);
740 EXPORT_SYMBOL(drm_mode_create);
743 * drm_mode_destroy - remove a mode
745 * @mode: mode to remove
748 * Caller must hold mode config lock.
750 * Free @mode's unique identifier, then free it.
752 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
757 drm_mode_object_put(dev, &mode->base);
759 free(mode, DRM_MEM_KMS);
761 EXPORT_SYMBOL(drm_mode_destroy);
763 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
765 struct drm_property *edid;
766 struct drm_property *dpms;
769 * Standard properties (apply to all connectors)
771 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
772 DRM_MODE_PROP_IMMUTABLE,
774 dev->mode_config.edid_property = edid;
776 dpms = drm_property_create_enum(dev, 0,
777 "DPMS", drm_dpms_enum_list,
778 ARRAY_SIZE(drm_dpms_enum_list));
779 dev->mode_config.dpms_property = dpms;
785 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
788 * Called by a driver the first time a DVI-I connector is made.
790 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
792 struct drm_property *dvi_i_selector;
793 struct drm_property *dvi_i_subconnector;
795 if (dev->mode_config.dvi_i_select_subconnector_property)
799 drm_property_create_enum(dev, 0,
800 "select subconnector",
801 drm_dvi_i_select_enum_list,
802 ARRAY_SIZE(drm_dvi_i_select_enum_list));
803 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
805 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
807 drm_dvi_i_subconnector_enum_list,
808 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
809 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
813 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
816 * drm_create_tv_properties - create TV specific connector properties
818 * @num_modes: number of different TV formats (modes) supported
819 * @modes: array of pointers to strings containing name of each format
821 * Called by a driver's TV initialization routine, this function creates
822 * the TV specific connector properties for a given device. Caller is
823 * responsible for allocating a list of format names and passing them to
826 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
829 struct drm_property *tv_selector;
830 struct drm_property *tv_subconnector;
833 if (dev->mode_config.tv_select_subconnector_property)
837 * Basic connector properties
839 tv_selector = drm_property_create_enum(dev, 0,
840 "select subconnector",
841 drm_tv_select_enum_list,
842 ARRAY_SIZE(drm_tv_select_enum_list));
843 dev->mode_config.tv_select_subconnector_property = tv_selector;
846 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
848 drm_tv_subconnector_enum_list,
849 ARRAY_SIZE(drm_tv_subconnector_enum_list));
850 dev->mode_config.tv_subconnector_property = tv_subconnector;
853 * Other, TV specific properties: margins & TV modes.
855 dev->mode_config.tv_left_margin_property =
856 drm_property_create_range(dev, 0, "left margin", 0, 100);
858 dev->mode_config.tv_right_margin_property =
859 drm_property_create_range(dev, 0, "right margin", 0, 100);
861 dev->mode_config.tv_top_margin_property =
862 drm_property_create_range(dev, 0, "top margin", 0, 100);
864 dev->mode_config.tv_bottom_margin_property =
865 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
867 dev->mode_config.tv_mode_property =
868 drm_property_create(dev, DRM_MODE_PROP_ENUM,
870 for (i = 0; i < num_modes; i++)
871 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
874 dev->mode_config.tv_brightness_property =
875 drm_property_create_range(dev, 0, "brightness", 0, 100);
877 dev->mode_config.tv_contrast_property =
878 drm_property_create_range(dev, 0, "contrast", 0, 100);
880 dev->mode_config.tv_flicker_reduction_property =
881 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
883 dev->mode_config.tv_overscan_property =
884 drm_property_create_range(dev, 0, "overscan", 0, 100);
886 dev->mode_config.tv_saturation_property =
887 drm_property_create_range(dev, 0, "saturation", 0, 100);
889 dev->mode_config.tv_hue_property =
890 drm_property_create_range(dev, 0, "hue", 0, 100);
894 EXPORT_SYMBOL(drm_mode_create_tv_properties);
897 * drm_mode_create_scaling_mode_property - create scaling mode property
900 * Called by a driver the first time it's needed, must be attached to desired
903 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
905 struct drm_property *scaling_mode;
907 if (dev->mode_config.scaling_mode_property)
911 drm_property_create_enum(dev, 0, "scaling mode",
912 drm_scaling_mode_enum_list,
913 ARRAY_SIZE(drm_scaling_mode_enum_list));
915 dev->mode_config.scaling_mode_property = scaling_mode;
919 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
922 * drm_mode_create_dithering_property - create dithering property
925 * Called by a driver the first time it's needed, must be attached to desired
928 int drm_mode_create_dithering_property(struct drm_device *dev)
930 struct drm_property *dithering_mode;
932 if (dev->mode_config.dithering_mode_property)
936 drm_property_create_enum(dev, 0, "dithering",
937 drm_dithering_mode_enum_list,
938 ARRAY_SIZE(drm_dithering_mode_enum_list));
939 dev->mode_config.dithering_mode_property = dithering_mode;
943 EXPORT_SYMBOL(drm_mode_create_dithering_property);
946 * drm_mode_create_dirty_property - create dirty property
949 * Called by a driver the first time it's needed, must be attached to desired
952 int drm_mode_create_dirty_info_property(struct drm_device *dev)
954 struct drm_property *dirty_info;
956 if (dev->mode_config.dirty_info_property)
960 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
962 drm_dirty_info_enum_list,
963 ARRAY_SIZE(drm_dirty_info_enum_list));
964 dev->mode_config.dirty_info_property = dirty_info;
968 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
971 * drm_mode_config_init - initialize DRM mode_configuration structure
975 * None, should happen single threaded at init time.
977 * Initialize @dev's mode_config structure, used for tracking the graphics
978 * configuration of @dev.
980 void drm_mode_config_init(struct drm_device *dev)
982 sx_init(&dev->mode_config.mutex, "kmslk");
983 INIT_LIST_HEAD(&dev->mode_config.fb_list);
984 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
985 INIT_LIST_HEAD(&dev->mode_config.connector_list);
986 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
987 INIT_LIST_HEAD(&dev->mode_config.property_list);
988 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
989 INIT_LIST_HEAD(&dev->mode_config.plane_list);
990 drm_gem_names_init(&dev->mode_config.crtc_names);
992 sx_xlock(&dev->mode_config.mutex);
993 drm_mode_create_standard_connector_properties(dev);
994 sx_xunlock(&dev->mode_config.mutex);
996 /* Just to be sure */
997 dev->mode_config.num_fb = 0;
998 dev->mode_config.num_connector = 0;
999 dev->mode_config.num_crtc = 0;
1000 dev->mode_config.num_encoder = 0;
1002 EXPORT_SYMBOL(drm_mode_config_init);
1004 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1006 uint32_t total_objects = 0;
1008 total_objects += dev->mode_config.num_crtc;
1009 total_objects += dev->mode_config.num_connector;
1010 total_objects += dev->mode_config.num_encoder;
1012 group->id_list = malloc(total_objects * sizeof(uint32_t),
1013 DRM_MEM_KMS, M_WAITOK | M_ZERO);
1014 if (!group->id_list)
1017 group->num_crtcs = 0;
1018 group->num_connectors = 0;
1019 group->num_encoders = 0;
1023 void drm_mode_group_free(struct drm_mode_group *group)
1025 free(group->id_list, DRM_MEM_KMS);
1026 group->id_list = NULL;
1029 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1030 struct drm_mode_group *group)
1032 struct drm_crtc *crtc;
1033 struct drm_encoder *encoder;
1034 struct drm_connector *connector;
1037 if ((ret = drm_mode_group_init(dev, group)))
1040 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1041 group->id_list[group->num_crtcs++] = crtc->base.id;
1043 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1044 group->id_list[group->num_crtcs + group->num_encoders++] =
1047 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1048 group->id_list[group->num_crtcs + group->num_encoders +
1049 group->num_connectors++] = connector->base.id;
1053 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1056 * drm_mode_config_cleanup - free up DRM mode_config info
1060 * Caller must hold mode config lock.
1062 * Free up all the connectors and CRTCs associated with this DRM device, then
1063 * free up the framebuffers and associated buffer objects.
1065 * FIXME: cleanup any dangling user buffer objects too
1067 void drm_mode_config_cleanup(struct drm_device *dev)
1069 struct drm_connector *connector, *ot;
1070 struct drm_crtc *crtc, *ct;
1071 struct drm_encoder *encoder, *enct;
1072 struct drm_framebuffer *fb, *fbt;
1073 struct drm_property *property, *pt;
1074 struct drm_property_blob *blob, *bt;
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(blob, bt, &dev->mode_config.property_blob_list,
1094 drm_property_destroy_blob(dev, blob);
1097 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1098 drm_framebuffer_remove(fb);
1101 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1103 plane->funcs->destroy(plane);
1106 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1107 crtc->funcs->destroy(crtc);
1110 drm_gem_names_fini(&dev->mode_config.crtc_names);
1112 EXPORT_SYMBOL(drm_mode_config_cleanup);
1115 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1116 * @out: drm_mode_modeinfo struct to return to the user
1117 * @in: drm_display_mode to use
1122 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1125 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1126 const struct drm_display_mode *in)
1128 if (in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1129 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1130 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1131 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1132 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX)
1133 DRM_WARNING("timing values too large for mode info\n");
1135 out->clock = in->clock;
1136 out->hdisplay = in->hdisplay;
1137 out->hsync_start = in->hsync_start;
1138 out->hsync_end = in->hsync_end;
1139 out->htotal = in->htotal;
1140 out->hskew = in->hskew;
1141 out->vdisplay = in->vdisplay;
1142 out->vsync_start = in->vsync_start;
1143 out->vsync_end = in->vsync_end;
1144 out->vtotal = in->vtotal;
1145 out->vscan = in->vscan;
1146 out->vrefresh = in->vrefresh;
1147 out->flags = in->flags;
1148 out->type = in->type;
1149 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1150 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1154 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1155 * @out: drm_display_mode to return to the user
1156 * @in: drm_mode_modeinfo to use
1161 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1165 * Zero on success, errno on failure.
1167 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1168 const struct drm_mode_modeinfo *in)
1170 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1173 out->clock = in->clock;
1174 out->hdisplay = in->hdisplay;
1175 out->hsync_start = in->hsync_start;
1176 out->hsync_end = in->hsync_end;
1177 out->htotal = in->htotal;
1178 out->hskew = in->hskew;
1179 out->vdisplay = in->vdisplay;
1180 out->vsync_start = in->vsync_start;
1181 out->vsync_end = in->vsync_end;
1182 out->vtotal = in->vtotal;
1183 out->vscan = in->vscan;
1184 out->vrefresh = in->vrefresh;
1185 out->flags = in->flags;
1186 out->type = in->type;
1187 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1188 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1194 * drm_mode_getresources - get graphics configuration
1195 * @inode: inode from the ioctl
1196 * @filp: file * from the ioctl
1197 * @cmd: cmd from ioctl
1198 * @arg: arg from ioctl
1201 * Takes mode config lock.
1203 * Construct a set of configuration description structures and return
1204 * them to the user, including CRTC, connector and framebuffer configuration.
1206 * Called by the user via ioctl.
1209 * Zero on success, errno on failure.
1211 int drm_mode_getresources(struct drm_device *dev, void *data,
1212 struct drm_file *file_priv)
1214 struct drm_mode_card_res *card_res = data;
1215 struct list_head *lh;
1216 struct drm_framebuffer *fb;
1217 struct drm_connector *connector;
1218 struct drm_crtc *crtc;
1219 struct drm_encoder *encoder;
1221 int connector_count = 0;
1224 int encoder_count = 0;
1226 uint32_t __user *fb_id;
1227 uint32_t __user *crtc_id;
1228 uint32_t __user *connector_id;
1229 uint32_t __user *encoder_id;
1230 struct drm_mode_group *mode_group;
1232 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1235 sx_xlock(&dev->mode_config.mutex);
1238 * For the non-control nodes we need to limit the list of resources
1239 * by IDs in the group list for this node
1241 list_for_each(lh, &file_priv->fbs)
1244 mode_group = &file_priv->master->minor->mode_group;
1245 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1247 list_for_each(lh, &dev->mode_config.crtc_list)
1250 list_for_each(lh, &dev->mode_config.connector_list)
1253 list_for_each(lh, &dev->mode_config.encoder_list)
1257 crtc_count = mode_group->num_crtcs;
1258 connector_count = mode_group->num_connectors;
1259 encoder_count = mode_group->num_encoders;
1262 card_res->max_height = dev->mode_config.max_height;
1263 card_res->min_height = dev->mode_config.min_height;
1264 card_res->max_width = dev->mode_config.max_width;
1265 card_res->min_width = dev->mode_config.min_width;
1267 /* handle this in 4 parts */
1269 if (card_res->count_fbs >= fb_count) {
1271 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1272 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1273 if (put_user(fb->base.id, fb_id + copied)) {
1280 card_res->count_fbs = fb_count;
1283 if (card_res->count_crtcs >= crtc_count) {
1285 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1286 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1287 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1289 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1290 if (put_user(crtc->base.id, crtc_id + copied)) {
1297 for (i = 0; i < mode_group->num_crtcs; i++) {
1298 if (put_user(mode_group->id_list[i],
1299 crtc_id + copied)) {
1307 card_res->count_crtcs = crtc_count;
1310 if (card_res->count_encoders >= encoder_count) {
1312 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1313 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1314 list_for_each_entry(encoder,
1315 &dev->mode_config.encoder_list,
1317 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1318 drm_get_encoder_name(encoder));
1319 if (put_user(encoder->base.id, encoder_id +
1327 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1328 if (put_user(mode_group->id_list[i],
1329 encoder_id + copied)) {
1338 card_res->count_encoders = encoder_count;
1341 if (card_res->count_connectors >= connector_count) {
1343 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1344 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1345 list_for_each_entry(connector,
1346 &dev->mode_config.connector_list,
1348 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1350 drm_get_connector_name(connector));
1351 if (put_user(connector->base.id,
1352 connector_id + copied)) {
1359 int start = mode_group->num_crtcs +
1360 mode_group->num_encoders;
1361 for (i = start; i < start + mode_group->num_connectors; i++) {
1362 if (put_user(mode_group->id_list[i],
1363 connector_id + copied)) {
1371 card_res->count_connectors = connector_count;
1373 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1374 card_res->count_connectors, card_res->count_encoders);
1377 sx_xunlock(&dev->mode_config.mutex);
1382 * drm_mode_getcrtc - get CRTC configuration
1383 * @inode: inode from the ioctl
1384 * @filp: file * from the ioctl
1385 * @cmd: cmd from ioctl
1386 * @arg: arg from ioctl
1389 * Takes mode config lock.
1391 * Construct a CRTC configuration structure to return to the user.
1393 * Called by the user via ioctl.
1396 * Zero on success, errno on failure.
1398 int drm_mode_getcrtc(struct drm_device *dev,
1399 void *data, struct drm_file *file_priv)
1401 struct drm_mode_crtc *crtc_resp = data;
1402 struct drm_crtc *crtc;
1403 struct drm_mode_object *obj;
1406 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1409 sx_xlock(&dev->mode_config.mutex);
1411 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1412 DRM_MODE_OBJECT_CRTC);
1417 crtc = obj_to_crtc(obj);
1419 crtc_resp->x = crtc->x;
1420 crtc_resp->y = crtc->y;
1421 crtc_resp->gamma_size = crtc->gamma_size;
1423 crtc_resp->fb_id = crtc->fb->base.id;
1425 crtc_resp->fb_id = 0;
1427 if (crtc->enabled) {
1429 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1430 crtc_resp->mode_valid = 1;
1433 crtc_resp->mode_valid = 0;
1437 sx_xunlock(&dev->mode_config.mutex);
1442 * drm_mode_getconnector - get connector configuration
1443 * @inode: inode from the ioctl
1444 * @filp: file * from the ioctl
1445 * @cmd: cmd from ioctl
1446 * @arg: arg from ioctl
1449 * Takes mode config lock.
1451 * Construct a connector configuration structure to return to the user.
1453 * Called by the user via ioctl.
1456 * Zero on success, errno on failure.
1458 int drm_mode_getconnector(struct drm_device *dev, void *data,
1459 struct drm_file *file_priv)
1461 struct drm_mode_get_connector *out_resp = data;
1462 struct drm_mode_object *obj;
1463 struct drm_connector *connector;
1464 struct drm_display_mode *mode;
1466 int props_count = 0;
1467 int encoders_count = 0;
1471 struct drm_mode_modeinfo u_mode;
1472 struct drm_mode_modeinfo __user *mode_ptr;
1473 uint32_t __user *prop_ptr;
1474 uint64_t __user *prop_values;
1475 uint32_t __user *encoder_ptr;
1477 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1480 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1482 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1484 sx_xlock(&dev->mode_config.mutex);
1486 obj = drm_mode_object_find(dev, out_resp->connector_id,
1487 DRM_MODE_OBJECT_CONNECTOR);
1492 connector = obj_to_connector(obj);
1494 props_count = connector->properties.count;
1496 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1497 if (connector->encoder_ids[i] != 0) {
1502 if (out_resp->count_modes == 0) {
1503 connector->funcs->fill_modes(connector,
1504 dev->mode_config.max_width,
1505 dev->mode_config.max_height);
1508 /* delayed so we get modes regardless of pre-fill_modes state */
1509 list_for_each_entry(mode, &connector->modes, head)
1512 out_resp->connector_id = connector->base.id;
1513 out_resp->connector_type = connector->connector_type;
1514 out_resp->connector_type_id = connector->connector_type_id;
1515 out_resp->mm_width = connector->display_info.width_mm;
1516 out_resp->mm_height = connector->display_info.height_mm;
1517 out_resp->subpixel = connector->display_info.subpixel_order;
1518 out_resp->connection = connector->status;
1519 if (connector->encoder)
1520 out_resp->encoder_id = connector->encoder->base.id;
1522 out_resp->encoder_id = 0;
1525 * This ioctl is called twice, once to determine how much space is
1526 * needed, and the 2nd time to fill it.
1528 if ((out_resp->count_modes >= mode_count) && mode_count) {
1530 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1531 list_for_each_entry(mode, &connector->modes, head) {
1532 drm_crtc_convert_to_umode(&u_mode, mode);
1533 if (copy_to_user(mode_ptr + copied,
1534 &u_mode, sizeof(u_mode))) {
1541 out_resp->count_modes = mode_count;
1543 if ((out_resp->count_props >= props_count) && props_count) {
1545 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1546 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1547 for (i = 0; i < connector->properties.count; i++) {
1548 if (put_user(connector->properties.ids[i],
1549 prop_ptr + copied)) {
1554 if (put_user(connector->properties.values[i],
1555 prop_values + copied)) {
1562 out_resp->count_props = props_count;
1564 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1566 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1567 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1568 if (connector->encoder_ids[i] != 0) {
1569 if (put_user(connector->encoder_ids[i],
1570 encoder_ptr + copied)) {
1578 out_resp->count_encoders = encoders_count;
1581 sx_xunlock(&dev->mode_config.mutex);
1585 int drm_mode_getencoder(struct drm_device *dev, void *data,
1586 struct drm_file *file_priv)
1588 struct drm_mode_get_encoder *enc_resp = data;
1589 struct drm_mode_object *obj;
1590 struct drm_encoder *encoder;
1593 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1596 sx_xlock(&dev->mode_config.mutex);
1597 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1598 DRM_MODE_OBJECT_ENCODER);
1603 encoder = obj_to_encoder(obj);
1606 enc_resp->crtc_id = encoder->crtc->base.id;
1608 enc_resp->crtc_id = 0;
1609 enc_resp->encoder_type = encoder->encoder_type;
1610 enc_resp->encoder_id = encoder->base.id;
1611 enc_resp->possible_crtcs = encoder->possible_crtcs;
1612 enc_resp->possible_clones = encoder->possible_clones;
1615 sx_xunlock(&dev->mode_config.mutex);
1620 * drm_mode_getplane_res - get plane info
1623 * @file_priv: DRM file info
1626 * Takes mode config lock.
1628 * Return an plane count and set of IDs.
1630 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1631 struct drm_file *file_priv)
1633 struct drm_mode_get_plane_res *plane_resp = data;
1634 struct drm_mode_config *config;
1635 struct drm_plane *plane;
1636 uint32_t __user *plane_ptr;
1637 int copied = 0, ret = 0;
1639 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1642 sx_xlock(&dev->mode_config.mutex);
1643 config = &dev->mode_config;
1646 * This ioctl is called twice, once to determine how much space is
1647 * needed, and the 2nd time to fill it.
1649 if (config->num_plane &&
1650 (plane_resp->count_planes >= config->num_plane)) {
1651 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1653 list_for_each_entry(plane, &config->plane_list, head) {
1654 if (put_user(plane->base.id, plane_ptr + copied)) {
1661 plane_resp->count_planes = config->num_plane;
1664 sx_xunlock(&dev->mode_config.mutex);
1669 * drm_mode_getplane - get plane info
1672 * @file_priv: DRM file info
1675 * Takes mode config lock.
1677 * Return plane info, including formats supported, gamma size, any
1680 int drm_mode_getplane(struct drm_device *dev, void *data,
1681 struct drm_file *file_priv)
1683 struct drm_mode_get_plane *plane_resp = data;
1684 struct drm_mode_object *obj;
1685 struct drm_plane *plane;
1686 uint32_t __user *format_ptr;
1689 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1692 sx_xlock(&dev->mode_config.mutex);
1693 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1694 DRM_MODE_OBJECT_PLANE);
1699 plane = obj_to_plane(obj);
1702 plane_resp->crtc_id = plane->crtc->base.id;
1704 plane_resp->crtc_id = 0;
1707 plane_resp->fb_id = plane->fb->base.id;
1709 plane_resp->fb_id = 0;
1711 plane_resp->plane_id = plane->base.id;
1712 plane_resp->possible_crtcs = plane->possible_crtcs;
1713 plane_resp->gamma_size = plane->gamma_size;
1716 * This ioctl is called twice, once to determine how much space is
1717 * needed, and the 2nd time to fill it.
1719 if (plane->format_count &&
1720 (plane_resp->count_format_types >= plane->format_count)) {
1721 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1722 if (copy_to_user(format_ptr,
1723 plane->format_types,
1724 sizeof(uint32_t) * plane->format_count)) {
1729 plane_resp->count_format_types = plane->format_count;
1732 sx_xunlock(&dev->mode_config.mutex);
1737 * drm_mode_setplane - set up or tear down an plane
1739 * @data: ioctl data*
1740 * @file_prive: DRM file info
1743 * Takes mode config lock.
1745 * Set plane info, including placement, fb, scaling, and other factors.
1746 * Or pass a NULL fb to disable.
1748 int drm_mode_setplane(struct drm_device *dev, void *data,
1749 struct drm_file *file_priv)
1751 struct drm_mode_set_plane *plane_req = data;
1752 struct drm_mode_object *obj;
1753 struct drm_plane *plane;
1754 struct drm_crtc *crtc;
1755 struct drm_framebuffer *fb;
1757 unsigned int fb_width, fb_height;
1760 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1763 sx_xlock(&dev->mode_config.mutex);
1766 * First, find the plane, crtc, and fb objects. If not available,
1767 * we don't bother to call the driver.
1769 obj = drm_mode_object_find(dev, plane_req->plane_id,
1770 DRM_MODE_OBJECT_PLANE);
1772 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1773 plane_req->plane_id);
1777 plane = obj_to_plane(obj);
1779 /* No fb means shut it down */
1780 if (!plane_req->fb_id) {
1781 plane->funcs->disable_plane(plane);
1787 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1788 DRM_MODE_OBJECT_CRTC);
1790 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1791 plane_req->crtc_id);
1795 crtc = obj_to_crtc(obj);
1797 obj = drm_mode_object_find(dev, plane_req->fb_id,
1798 DRM_MODE_OBJECT_FB);
1800 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1805 fb = obj_to_fb(obj);
1807 /* Check whether this plane supports the fb pixel format. */
1808 for (i = 0; i < plane->format_count; i++)
1809 if (fb->pixel_format == plane->format_types[i])
1811 if (i == plane->format_count) {
1812 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1817 fb_width = fb->width << 16;
1818 fb_height = fb->height << 16;
1820 /* Make sure source coordinates are inside the fb. */
1821 if (plane_req->src_w > fb_width ||
1822 plane_req->src_x > fb_width - plane_req->src_w ||
1823 plane_req->src_h > fb_height ||
1824 plane_req->src_y > fb_height - plane_req->src_h) {
1825 DRM_DEBUG_KMS("Invalid source coordinates "
1826 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1827 plane_req->src_w >> 16,
1828 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1829 plane_req->src_h >> 16,
1830 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1831 plane_req->src_x >> 16,
1832 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1833 plane_req->src_y >> 16,
1834 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1839 /* Give drivers some help against integer overflows */
1840 if (plane_req->crtc_w > INT_MAX ||
1841 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1842 plane_req->crtc_h > INT_MAX ||
1843 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1844 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1845 plane_req->crtc_w, plane_req->crtc_h,
1846 plane_req->crtc_x, plane_req->crtc_y);
1851 ret = plane->funcs->update_plane(plane, crtc, fb,
1852 plane_req->crtc_x, plane_req->crtc_y,
1853 plane_req->crtc_w, plane_req->crtc_h,
1854 plane_req->src_x, plane_req->src_y,
1855 plane_req->src_w, plane_req->src_h);
1862 sx_xunlock(&dev->mode_config.mutex);
1868 * drm_mode_setcrtc - set CRTC configuration
1869 * @inode: inode from the ioctl
1870 * @filp: file * from the ioctl
1871 * @cmd: cmd from ioctl
1872 * @arg: arg from ioctl
1875 * Takes mode config lock.
1877 * Build a new CRTC configuration based on user request.
1879 * Called by the user via ioctl.
1882 * Zero on success, errno on failure.
1884 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1885 struct drm_file *file_priv)
1887 struct drm_mode_config *config = &dev->mode_config;
1888 struct drm_mode_crtc *crtc_req = data;
1889 struct drm_mode_object *obj;
1890 struct drm_crtc *crtc;
1891 struct drm_connector **connector_set = NULL, *connector;
1892 struct drm_framebuffer *fb = NULL;
1893 struct drm_display_mode *mode = NULL;
1894 struct drm_mode_set set;
1895 uint32_t __user *set_connectors_ptr;
1899 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1902 /* For some reason crtc x/y offsets are signed internally. */
1903 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1906 sx_xlock(&dev->mode_config.mutex);
1907 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1908 DRM_MODE_OBJECT_CRTC);
1910 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1914 crtc = obj_to_crtc(obj);
1915 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1917 if (crtc_req->mode_valid) {
1918 int hdisplay, vdisplay;
1919 /* If we have a mode we need a framebuffer. */
1920 /* If we pass -1, set the mode with the currently bound fb */
1921 if (crtc_req->fb_id == -1) {
1923 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1929 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1930 DRM_MODE_OBJECT_FB);
1932 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1937 fb = obj_to_fb(obj);
1940 mode = drm_mode_create(dev);
1946 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1948 DRM_DEBUG_KMS("Invalid mode\n");
1952 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1954 hdisplay = mode->hdisplay;
1955 vdisplay = mode->vdisplay;
1957 if (crtc->invert_dimensions) {
1960 vdisplay = hdisplay;
1964 if (hdisplay > fb->width ||
1965 vdisplay > fb->height ||
1966 crtc_req->x > fb->width - hdisplay ||
1967 crtc_req->y > fb->height - vdisplay) {
1968 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1969 fb->width, fb->height,
1970 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1971 crtc->invert_dimensions ? " (inverted)" : "");
1977 if (crtc_req->count_connectors == 0 && mode) {
1978 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1983 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1984 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1985 crtc_req->count_connectors);
1990 if (crtc_req->count_connectors > 0) {
1993 /* Avoid unbounded kernel memory allocation */
1994 if (crtc_req->count_connectors > config->num_connector) {
1999 connector_set = malloc(crtc_req->count_connectors *
2000 sizeof(struct drm_connector *),
2001 DRM_MEM_KMS, M_WAITOK);
2002 if (!connector_set) {
2007 for (i = 0; i < crtc_req->count_connectors; i++) {
2008 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2009 if (get_user(out_id, &set_connectors_ptr[i])) {
2014 obj = drm_mode_object_find(dev, out_id,
2015 DRM_MODE_OBJECT_CONNECTOR);
2017 DRM_DEBUG_KMS("Connector id %d unknown\n",
2022 connector = obj_to_connector(obj);
2023 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2025 drm_get_connector_name(connector));
2027 connector_set[i] = connector;
2032 set.x = crtc_req->x;
2033 set.y = crtc_req->y;
2035 set.connectors = connector_set;
2036 set.num_connectors = crtc_req->count_connectors;
2038 ret = crtc->funcs->set_config(&set);
2041 free(connector_set, DRM_MEM_KMS);
2042 drm_mode_destroy(dev, mode);
2043 sx_xunlock(&dev->mode_config.mutex);
2047 int drm_mode_cursor_ioctl(struct drm_device *dev,
2048 void *data, struct drm_file *file_priv)
2050 struct drm_mode_cursor *req = data;
2051 struct drm_mode_object *obj;
2052 struct drm_crtc *crtc;
2055 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2058 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2061 sx_xlock(&dev->mode_config.mutex);
2062 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2064 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2068 crtc = obj_to_crtc(obj);
2070 if (req->flags & DRM_MODE_CURSOR_BO) {
2071 if (!crtc->funcs->cursor_set) {
2075 /* Turns off the cursor if handle is 0 */
2076 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2077 req->width, req->height);
2080 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2081 if (crtc->funcs->cursor_move) {
2082 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2089 sx_xunlock(&dev->mode_config.mutex);
2093 /* Original addfb only supported RGB formats, so figure out which one */
2094 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2100 fmt = DRM_FORMAT_C8;
2104 fmt = DRM_FORMAT_XRGB1555;
2106 fmt = DRM_FORMAT_RGB565;
2109 fmt = DRM_FORMAT_RGB888;
2113 fmt = DRM_FORMAT_XRGB8888;
2114 else if (depth == 30)
2115 fmt = DRM_FORMAT_XRGB2101010;
2117 fmt = DRM_FORMAT_ARGB8888;
2120 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2121 fmt = DRM_FORMAT_XRGB8888;
2127 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2130 * drm_mode_addfb - add an FB to the graphics configuration
2131 * @inode: inode from the ioctl
2132 * @filp: file * from the ioctl
2133 * @cmd: cmd from ioctl
2134 * @arg: arg from ioctl
2137 * Takes mode config lock.
2139 * Add a new FB to the specified CRTC, given a user request.
2141 * Called by the user via ioctl.
2144 * Zero on success, errno on failure.
2146 int drm_mode_addfb(struct drm_device *dev,
2147 void *data, struct drm_file *file_priv)
2149 struct drm_mode_fb_cmd *or = data;
2150 struct drm_mode_fb_cmd2 r = {};
2151 struct drm_mode_config *config = &dev->mode_config;
2152 struct drm_framebuffer *fb;
2155 /* Use new struct with format internally */
2156 r.fb_id = or->fb_id;
2157 r.width = or->width;
2158 r.height = or->height;
2159 r.pitches[0] = or->pitch;
2160 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2161 r.handles[0] = or->handle;
2163 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2166 if ((config->min_width > r.width) || (r.width > config->max_width))
2169 if ((config->min_height > r.height) || (r.height > config->max_height))
2172 sx_xlock(&dev->mode_config.mutex);
2174 /* TODO check buffer is sufficiently large */
2175 /* TODO setup destructor callback */
2177 ret = dev->mode_config.funcs->fb_create(dev, file_priv, &r, &fb);
2179 DRM_DEBUG_KMS("could not create framebuffer\n");
2183 or->fb_id = fb->base.id;
2184 list_add(&fb->filp_head, &file_priv->fbs);
2185 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2188 sx_xunlock(&dev->mode_config.mutex);
2192 static int format_check(const struct drm_mode_fb_cmd2 *r)
2194 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2198 case DRM_FORMAT_RGB332:
2199 case DRM_FORMAT_BGR233:
2200 case DRM_FORMAT_XRGB4444:
2201 case DRM_FORMAT_XBGR4444:
2202 case DRM_FORMAT_RGBX4444:
2203 case DRM_FORMAT_BGRX4444:
2204 case DRM_FORMAT_ARGB4444:
2205 case DRM_FORMAT_ABGR4444:
2206 case DRM_FORMAT_RGBA4444:
2207 case DRM_FORMAT_BGRA4444:
2208 case DRM_FORMAT_XRGB1555:
2209 case DRM_FORMAT_XBGR1555:
2210 case DRM_FORMAT_RGBX5551:
2211 case DRM_FORMAT_BGRX5551:
2212 case DRM_FORMAT_ARGB1555:
2213 case DRM_FORMAT_ABGR1555:
2214 case DRM_FORMAT_RGBA5551:
2215 case DRM_FORMAT_BGRA5551:
2216 case DRM_FORMAT_RGB565:
2217 case DRM_FORMAT_BGR565:
2218 case DRM_FORMAT_RGB888:
2219 case DRM_FORMAT_BGR888:
2220 case DRM_FORMAT_XRGB8888:
2221 case DRM_FORMAT_XBGR8888:
2222 case DRM_FORMAT_RGBX8888:
2223 case DRM_FORMAT_BGRX8888:
2224 case DRM_FORMAT_ARGB8888:
2225 case DRM_FORMAT_ABGR8888:
2226 case DRM_FORMAT_RGBA8888:
2227 case DRM_FORMAT_BGRA8888:
2228 case DRM_FORMAT_XRGB2101010:
2229 case DRM_FORMAT_XBGR2101010:
2230 case DRM_FORMAT_RGBX1010102:
2231 case DRM_FORMAT_BGRX1010102:
2232 case DRM_FORMAT_ARGB2101010:
2233 case DRM_FORMAT_ABGR2101010:
2234 case DRM_FORMAT_RGBA1010102:
2235 case DRM_FORMAT_BGRA1010102:
2236 case DRM_FORMAT_YUYV:
2237 case DRM_FORMAT_YVYU:
2238 case DRM_FORMAT_UYVY:
2239 case DRM_FORMAT_VYUY:
2240 case DRM_FORMAT_AYUV:
2241 case DRM_FORMAT_NV12:
2242 case DRM_FORMAT_NV21:
2243 case DRM_FORMAT_NV16:
2244 case DRM_FORMAT_NV61:
2245 case DRM_FORMAT_NV24:
2246 case DRM_FORMAT_NV42:
2247 case DRM_FORMAT_YUV410:
2248 case DRM_FORMAT_YVU410:
2249 case DRM_FORMAT_YUV411:
2250 case DRM_FORMAT_YVU411:
2251 case DRM_FORMAT_YUV420:
2252 case DRM_FORMAT_YVU420:
2253 case DRM_FORMAT_YUV422:
2254 case DRM_FORMAT_YVU422:
2255 case DRM_FORMAT_YUV444:
2256 case DRM_FORMAT_YVU444:
2263 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2265 int ret, hsub, vsub, num_planes, i;
2267 ret = format_check(r);
2269 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
2273 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2274 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2275 num_planes = drm_format_num_planes(r->pixel_format);
2277 if (r->width == 0 || r->width % hsub) {
2278 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2282 if (r->height == 0 || r->height % vsub) {
2283 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2287 for (i = 0; i < num_planes; i++) {
2288 unsigned int width = r->width / (i != 0 ? hsub : 1);
2289 unsigned int height = r->height / (i != 0 ? vsub : 1);
2290 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2292 if (!r->handles[i]) {
2293 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2297 if ((uint64_t) width * cpp > UINT_MAX)
2300 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2303 if (r->pitches[i] < width * cpp) {
2304 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2313 * drm_mode_addfb2 - add an FB to the graphics configuration
2314 * @inode: inode from the ioctl
2315 * @filp: file * from the ioctl
2316 * @cmd: cmd from ioctl
2317 * @arg: arg from ioctl
2320 * Takes mode config lock.
2322 * Add a new FB to the specified CRTC, given a user request with format.
2324 * Called by the user via ioctl.
2327 * Zero on success, errno on failure.
2329 int drm_mode_addfb2(struct drm_device *dev,
2330 void *data, struct drm_file *file_priv)
2332 struct drm_mode_fb_cmd2 *r = data;
2333 struct drm_mode_config *config = &dev->mode_config;
2334 struct drm_framebuffer *fb;
2337 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2340 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2341 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2345 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2346 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2347 r->width, config->min_width, config->max_width);
2350 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2351 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2352 r->height, config->min_height, config->max_height);
2356 ret = framebuffer_check(r);
2360 sx_xlock(&dev->mode_config.mutex);
2362 ret = dev->mode_config.funcs->fb_create(dev, file_priv, r, &fb);
2364 DRM_DEBUG_KMS("could not create framebuffer\n");
2368 r->fb_id = fb->base.id;
2369 list_add(&fb->filp_head, &file_priv->fbs);
2370 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2373 sx_xunlock(&dev->mode_config.mutex);
2378 * drm_mode_rmfb - remove an FB from the configuration
2379 * @inode: inode from the ioctl
2380 * @filp: file * from the ioctl
2381 * @cmd: cmd from ioctl
2382 * @arg: arg from ioctl
2385 * Takes mode config lock.
2387 * Remove the FB specified by the user.
2389 * Called by the user via ioctl.
2392 * Zero on success, errno on failure.
2394 int drm_mode_rmfb(struct drm_device *dev,
2395 void *data, struct drm_file *file_priv)
2397 struct drm_mode_object *obj;
2398 struct drm_framebuffer *fb = NULL;
2399 struct drm_framebuffer *fbl = NULL;
2400 uint32_t *id = data;
2404 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2407 sx_xlock(&dev->mode_config.mutex);
2408 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
2409 /* TODO check that we really get a framebuffer back. */
2414 fb = obj_to_fb(obj);
2416 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2425 drm_framebuffer_remove(fb);
2428 sx_xunlock(&dev->mode_config.mutex);
2433 * drm_mode_getfb - get FB info
2434 * @inode: inode from the ioctl
2435 * @filp: file * from the ioctl
2436 * @cmd: cmd from ioctl
2437 * @arg: arg from ioctl
2440 * Takes mode config lock.
2442 * Lookup the FB given its ID and return info about it.
2444 * Called by the user via ioctl.
2447 * Zero on success, errno on failure.
2449 int drm_mode_getfb(struct drm_device *dev,
2450 void *data, struct drm_file *file_priv)
2452 struct drm_mode_fb_cmd *r = data;
2453 struct drm_mode_object *obj;
2454 struct drm_framebuffer *fb;
2457 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2460 sx_xlock(&dev->mode_config.mutex);
2461 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2466 fb = obj_to_fb(obj);
2468 r->height = fb->height;
2469 r->width = fb->width;
2470 r->depth = fb->depth;
2471 r->bpp = fb->bits_per_pixel;
2472 r->pitch = fb->pitches[0];
2474 fb->funcs->create_handle(fb, file_priv, &r->handle);
2477 sx_xunlock(&dev->mode_config.mutex);
2481 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2482 void *data, struct drm_file *file_priv)
2484 struct drm_clip_rect __user *clips_ptr;
2485 struct drm_clip_rect *clips = NULL;
2486 struct drm_mode_fb_dirty_cmd *r = data;
2487 struct drm_mode_object *obj;
2488 struct drm_framebuffer *fb;
2493 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2496 sx_xlock(&dev->mode_config.mutex);
2497 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2502 fb = obj_to_fb(obj);
2504 num_clips = r->num_clips;
2505 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2507 if (!num_clips != !clips_ptr) {
2512 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2514 /* If userspace annotates copy, clips must come in pairs */
2515 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2520 if (num_clips && clips_ptr) {
2521 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2525 clips = malloc(num_clips * sizeof(*clips), DRM_MEM_KMS,
2532 ret = copy_from_user(clips, clips_ptr,
2533 num_clips * sizeof(*clips));
2540 if (fb->funcs->dirty) {
2541 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2549 free(clips, DRM_MEM_KMS);
2551 sx_xunlock(&dev->mode_config.mutex);
2557 * drm_fb_release - remove and free the FBs on this file
2558 * @filp: file * from the ioctl
2561 * Takes mode config lock.
2563 * Destroy all the FBs associated with @filp.
2565 * Called by the user via ioctl.
2568 * Zero on success, errno on failure.
2570 void drm_fb_release(struct drm_file *priv)
2572 struct drm_device *dev = priv->minor->dev;
2573 struct drm_framebuffer *fb, *tfb;
2575 sx_xlock(&dev->mode_config.mutex);
2576 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2577 drm_framebuffer_remove(fb);
2579 sx_xunlock(&dev->mode_config.mutex);
2583 * drm_mode_attachmode - add a mode to the user mode list
2585 * @connector: connector to add the mode to
2586 * @mode: mode to add
2588 * Add @mode to @connector's user mode list.
2590 static void drm_mode_attachmode(struct drm_device *dev,
2591 struct drm_connector *connector,
2592 struct drm_display_mode *mode)
2594 list_add_tail(&mode->head, &connector->user_modes);
2597 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2598 const struct drm_display_mode *mode)
2600 struct drm_connector *connector;
2602 struct drm_display_mode *dup_mode, *next;
2603 DRM_LIST_HEAD(list);
2605 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2606 if (!connector->encoder)
2608 if (connector->encoder->crtc == crtc) {
2609 dup_mode = drm_mode_duplicate(dev, mode);
2614 list_add_tail(&dup_mode->head, &list);
2618 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2619 if (!connector->encoder)
2621 if (connector->encoder->crtc == crtc)
2622 list_move_tail(list.next, &connector->user_modes);
2625 MPASS(!list_empty(&list));
2628 list_for_each_entry_safe(dup_mode, next, &list, head)
2629 drm_mode_destroy(dev, dup_mode);
2633 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2635 static int drm_mode_detachmode(struct drm_device *dev,
2636 struct drm_connector *connector,
2637 struct drm_display_mode *mode)
2641 struct drm_display_mode *match_mode, *t;
2643 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2644 if (drm_mode_equal(match_mode, mode)) {
2645 list_del(&match_mode->head);
2646 drm_mode_destroy(dev, match_mode);
2658 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2660 struct drm_connector *connector;
2662 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2663 drm_mode_detachmode(dev, connector, mode);
2667 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2670 * drm_fb_attachmode - Attach a user mode to an connector
2671 * @inode: inode from the ioctl
2672 * @filp: file * from the ioctl
2673 * @cmd: cmd from ioctl
2674 * @arg: arg from ioctl
2676 * This attaches a user specified mode to an connector.
2677 * Called by the user via ioctl.
2680 * Zero on success, errno on failure.
2682 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2683 void *data, struct drm_file *file_priv)
2685 struct drm_mode_mode_cmd *mode_cmd = data;
2686 struct drm_connector *connector;
2687 struct drm_display_mode *mode;
2688 struct drm_mode_object *obj;
2689 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2692 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2695 sx_xlock(&dev->mode_config.mutex);
2697 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2702 connector = obj_to_connector(obj);
2704 mode = drm_mode_create(dev);
2710 ret = drm_crtc_convert_umode(mode, umode);
2712 DRM_DEBUG_KMS("Invalid mode\n");
2713 drm_mode_destroy(dev, mode);
2717 drm_mode_attachmode(dev, connector, mode);
2719 sx_xunlock(&dev->mode_config.mutex);
2725 * drm_fb_detachmode - Detach a user specified mode from an connector
2726 * @inode: inode from the ioctl
2727 * @filp: file * from the ioctl
2728 * @cmd: cmd from ioctl
2729 * @arg: arg from ioctl
2731 * Called by the user via ioctl.
2734 * Zero on success, errno on failure.
2736 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2737 void *data, struct drm_file *file_priv)
2739 struct drm_mode_object *obj;
2740 struct drm_mode_mode_cmd *mode_cmd = data;
2741 struct drm_connector *connector;
2742 struct drm_display_mode mode;
2743 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2746 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2749 sx_xlock(&dev->mode_config.mutex);
2751 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2756 connector = obj_to_connector(obj);
2758 ret = drm_crtc_convert_umode(&mode, umode);
2760 DRM_DEBUG_KMS("Invalid mode\n");
2764 ret = drm_mode_detachmode(dev, connector, &mode);
2766 sx_xunlock(&dev->mode_config.mutex);
2770 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2771 const char *name, int num_values)
2773 struct drm_property *property = NULL;
2776 property = malloc(sizeof(struct drm_property), DRM_MEM_KMS,
2782 property->values = malloc(sizeof(uint64_t)*num_values, DRM_MEM_KMS,
2784 if (!property->values)
2788 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2792 property->flags = flags;
2793 property->num_values = num_values;
2794 INIT_LIST_HEAD(&property->enum_blob_list);
2797 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2798 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2801 list_add_tail(&property->head, &dev->mode_config.property_list);
2804 free(property->values, DRM_MEM_KMS);
2805 free(property, DRM_MEM_KMS);
2808 EXPORT_SYMBOL(drm_property_create);
2810 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2812 const struct drm_prop_enum_list *props,
2815 struct drm_property *property;
2818 flags |= DRM_MODE_PROP_ENUM;
2820 property = drm_property_create(dev, flags, name, num_values);
2824 for (i = 0; i < num_values; i++) {
2825 ret = drm_property_add_enum(property, i,
2829 drm_property_destroy(dev, property);
2836 EXPORT_SYMBOL(drm_property_create_enum);
2838 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2839 int flags, const char *name,
2840 const struct drm_prop_enum_list *props,
2843 struct drm_property *property;
2846 flags |= DRM_MODE_PROP_BITMASK;
2848 property = drm_property_create(dev, flags, name, num_values);
2852 for (i = 0; i < num_values; i++) {
2853 ret = drm_property_add_enum(property, i,
2857 drm_property_destroy(dev, property);
2864 EXPORT_SYMBOL(drm_property_create_bitmask);
2866 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2868 uint64_t min, uint64_t max)
2870 struct drm_property *property;
2872 flags |= DRM_MODE_PROP_RANGE;
2874 property = drm_property_create(dev, flags, name, 2);
2878 property->values[0] = min;
2879 property->values[1] = max;
2883 EXPORT_SYMBOL(drm_property_create_range);
2885 int drm_property_add_enum(struct drm_property *property, int index,
2886 uint64_t value, const char *name)
2888 struct drm_property_enum *prop_enum;
2890 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2894 * Bitmask enum properties have the additional constraint of values
2897 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2900 if (!list_empty(&property->enum_blob_list)) {
2901 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2902 if (prop_enum->value == value) {
2903 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2904 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2910 prop_enum = malloc(sizeof(struct drm_property_enum), DRM_MEM_KMS,
2915 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2916 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2917 prop_enum->value = value;
2919 property->values[index] = value;
2920 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2923 EXPORT_SYMBOL(drm_property_add_enum);
2925 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2927 struct drm_property_enum *prop_enum, *pt;
2929 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2930 list_del(&prop_enum->head);
2931 free(prop_enum, DRM_MEM_KMS);
2934 if (property->num_values)
2935 free(property->values, DRM_MEM_KMS);
2936 drm_mode_object_put(dev, &property->base);
2937 list_del(&property->head);
2938 free(property, DRM_MEM_KMS);
2940 EXPORT_SYMBOL(drm_property_destroy);
2942 void drm_object_attach_property(struct drm_mode_object *obj,
2943 struct drm_property *property,
2946 int count = obj->properties->count;
2948 if (count == DRM_OBJECT_MAX_PROPERTY) {
2949 DRM_WARNING("Failed to attach object property (type: 0x%x). Please "
2950 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2951 "you see this message on the same object type.\n",
2956 obj->properties->ids[count] = property->base.id;
2957 obj->properties->values[count] = init_val;
2958 obj->properties->count++;
2960 EXPORT_SYMBOL(drm_object_attach_property);
2962 int drm_object_property_set_value(struct drm_mode_object *obj,
2963 struct drm_property *property, uint64_t val)
2967 for (i = 0; i < obj->properties->count; i++) {
2968 if (obj->properties->ids[i] == property->base.id) {
2969 obj->properties->values[i] = val;
2976 EXPORT_SYMBOL(drm_object_property_set_value);
2978 int drm_object_property_get_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 *val = obj->properties->values[i];
2992 EXPORT_SYMBOL(drm_object_property_get_value);
2994 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2995 void *data, struct drm_file *file_priv)
2997 struct drm_mode_object *obj;
2998 struct drm_mode_get_property *out_resp = data;
2999 struct drm_property *property;
3002 int value_count = 0;
3005 struct drm_property_enum *prop_enum;
3006 struct drm_mode_property_enum __user *enum_ptr;
3007 struct drm_property_blob *prop_blob;
3008 uint32_t __user *blob_id_ptr;
3009 uint64_t __user *values_ptr;
3010 uint32_t __user *blob_length_ptr;
3012 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3015 sx_xlock(&dev->mode_config.mutex);
3016 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3021 property = obj_to_property(obj);
3023 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3024 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3026 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3027 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3031 value_count = property->num_values;
3033 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3034 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3035 out_resp->flags = property->flags;
3037 if ((out_resp->count_values >= value_count) && value_count) {
3038 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3039 for (i = 0; i < value_count; i++) {
3040 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3046 out_resp->count_values = value_count;
3048 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3049 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3051 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3052 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3054 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3059 if (copy_to_user(&enum_ptr[copied].name,
3060 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3067 out_resp->count_enum_blobs = enum_count;
3070 if (property->flags & DRM_MODE_PROP_BLOB) {
3071 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3073 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3074 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3076 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3077 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3082 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3090 out_resp->count_enum_blobs = blob_count;
3093 sx_xunlock(&dev->mode_config.mutex);
3097 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3100 struct drm_property_blob *blob;
3103 if (!length || !data)
3106 blob = malloc(sizeof(struct drm_property_blob)+length, DRM_MEM_KMS,
3111 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3113 free(blob, DRM_MEM_KMS);
3117 blob->length = length;
3119 memcpy(blob->data, data, length);
3121 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3125 static void drm_property_destroy_blob(struct drm_device *dev,
3126 struct drm_property_blob *blob)
3128 drm_mode_object_put(dev, &blob->base);
3129 list_del(&blob->head);
3130 free(blob, DRM_MEM_KMS);
3133 int drm_mode_getblob_ioctl(struct drm_device *dev,
3134 void *data, struct drm_file *file_priv)
3136 struct drm_mode_object *obj;
3137 struct drm_mode_get_blob *out_resp = data;
3138 struct drm_property_blob *blob;
3140 void __user *blob_ptr;
3142 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3145 sx_xlock(&dev->mode_config.mutex);
3146 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3151 blob = obj_to_blob(obj);
3153 if (out_resp->length == blob->length) {
3154 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3155 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3160 out_resp->length = blob->length;
3163 sx_xunlock(&dev->mode_config.mutex);
3167 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3170 struct drm_device *dev = connector->dev;
3173 if (connector->edid_blob_ptr)
3174 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3176 /* Delete edid, when there is none. */
3178 connector->edid_blob_ptr = NULL;
3179 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3183 size = EDID_LENGTH * (1 + edid->extensions);
3184 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3186 if (!connector->edid_blob_ptr)
3189 ret = drm_object_property_set_value(&connector->base,
3190 dev->mode_config.edid_property,
3191 connector->edid_blob_ptr->base.id);
3195 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3197 static bool drm_property_change_is_valid(struct drm_property *property,
3200 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3202 if (property->flags & DRM_MODE_PROP_RANGE) {
3203 if (value < property->values[0] || value > property->values[1])
3206 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3208 uint64_t valid_mask = 0;
3209 for (i = 0; i < property->num_values; i++)
3210 valid_mask |= (1ULL << property->values[i]);
3211 return !(value & ~valid_mask);
3212 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3213 /* Only the driver knows */
3217 for (i = 0; i < property->num_values; i++)
3218 if (property->values[i] == value)
3224 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3225 void *data, struct drm_file *file_priv)
3227 struct drm_mode_connector_set_property *conn_set_prop = data;
3228 struct drm_mode_obj_set_property obj_set_prop = {
3229 .value = conn_set_prop->value,
3230 .prop_id = conn_set_prop->prop_id,
3231 .obj_id = conn_set_prop->connector_id,
3232 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3235 /* It does all the locking and checking we need */
3236 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3239 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3240 struct drm_property *property,
3244 struct drm_connector *connector = obj_to_connector(obj);
3246 /* Do DPMS ourselves */
3247 if (property == connector->dev->mode_config.dpms_property) {
3248 if (connector->funcs->dpms)
3249 (*connector->funcs->dpms)(connector, (int)value);
3251 } else if (connector->funcs->set_property)
3252 ret = connector->funcs->set_property(connector, property, value);
3254 /* store the property value if successful */
3256 drm_object_property_set_value(&connector->base, property, value);
3260 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3261 struct drm_property *property,
3265 struct drm_crtc *crtc = obj_to_crtc(obj);
3267 if (crtc->funcs->set_property)
3268 ret = crtc->funcs->set_property(crtc, property, value);
3270 drm_object_property_set_value(obj, property, value);
3275 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3276 struct drm_property *property,
3280 struct drm_plane *plane = obj_to_plane(obj);
3282 if (plane->funcs->set_property)
3283 ret = plane->funcs->set_property(plane, property, value);
3285 drm_object_property_set_value(obj, property, value);
3290 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3291 struct drm_file *file_priv)
3293 struct drm_mode_obj_get_properties *arg = data;
3294 struct drm_mode_object *obj;
3298 int props_count = 0;
3299 uint32_t __user *props_ptr;
3300 uint64_t __user *prop_values_ptr;
3302 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3305 sx_xlock(&dev->mode_config.mutex);
3307 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3312 if (!obj->properties) {
3317 props_count = obj->properties->count;
3319 /* This ioctl is called twice, once to determine how much space is
3320 * needed, and the 2nd time to fill it. */
3321 if ((arg->count_props >= props_count) && props_count) {
3323 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3324 prop_values_ptr = (uint64_t __user *)(unsigned long)
3325 (arg->prop_values_ptr);
3326 for (i = 0; i < props_count; i++) {
3327 if (put_user(obj->properties->ids[i],
3328 props_ptr + copied)) {
3332 if (put_user(obj->properties->values[i],
3333 prop_values_ptr + copied)) {
3340 arg->count_props = props_count;
3342 sx_xunlock(&dev->mode_config.mutex);
3346 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3347 struct drm_file *file_priv)
3349 struct drm_mode_obj_set_property *arg = data;
3350 struct drm_mode_object *arg_obj;
3351 struct drm_mode_object *prop_obj;
3352 struct drm_property *property;
3356 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3359 sx_xlock(&dev->mode_config.mutex);
3361 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3364 if (!arg_obj->properties)
3367 for (i = 0; i < arg_obj->properties->count; i++)
3368 if (arg_obj->properties->ids[i] == arg->prop_id)
3371 if (i == arg_obj->properties->count)
3374 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3375 DRM_MODE_OBJECT_PROPERTY);
3378 property = obj_to_property(prop_obj);
3380 if (!drm_property_change_is_valid(property, arg->value))
3383 switch (arg_obj->type) {
3384 case DRM_MODE_OBJECT_CONNECTOR:
3385 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3388 case DRM_MODE_OBJECT_CRTC:
3389 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3391 case DRM_MODE_OBJECT_PLANE:
3392 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3397 sx_xunlock(&dev->mode_config.mutex);
3401 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3402 struct drm_encoder *encoder)
3406 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3407 if (connector->encoder_ids[i] == 0) {
3408 connector->encoder_ids[i] = encoder->base.id;
3414 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3416 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3417 struct drm_encoder *encoder)
3420 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3421 if (connector->encoder_ids[i] == encoder->base.id) {
3422 connector->encoder_ids[i] = 0;
3423 if (connector->encoder == encoder)
3424 connector->encoder = NULL;
3429 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3431 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3434 crtc->gamma_size = gamma_size;
3436 crtc->gamma_store = malloc(gamma_size * sizeof(uint16_t) * 3,
3437 DRM_MEM_KMS, M_WAITOK | M_ZERO);
3438 if (!crtc->gamma_store) {
3439 crtc->gamma_size = 0;
3445 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3447 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3448 void *data, struct drm_file *file_priv)
3450 struct drm_mode_crtc_lut *crtc_lut = data;
3451 struct drm_mode_object *obj;
3452 struct drm_crtc *crtc;
3453 void *r_base, *g_base, *b_base;
3457 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3460 sx_xlock(&dev->mode_config.mutex);
3461 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3466 crtc = obj_to_crtc(obj);
3468 if (crtc->funcs->gamma_set == NULL) {
3473 /* memcpy into gamma store */
3474 if (crtc_lut->gamma_size != crtc->gamma_size) {
3479 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3480 r_base = crtc->gamma_store;
3481 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3486 g_base = (char *)r_base + size;
3487 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3492 b_base = (char *)g_base + size;
3493 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3498 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3501 sx_xunlock(&dev->mode_config.mutex);
3506 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3507 void *data, struct drm_file *file_priv)
3509 struct drm_mode_crtc_lut *crtc_lut = data;
3510 struct drm_mode_object *obj;
3511 struct drm_crtc *crtc;
3512 void *r_base, *g_base, *b_base;
3516 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3519 sx_xlock(&dev->mode_config.mutex);
3520 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3525 crtc = obj_to_crtc(obj);
3527 /* memcpy into gamma store */
3528 if (crtc_lut->gamma_size != crtc->gamma_size) {
3533 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3534 r_base = crtc->gamma_store;
3535 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3540 g_base = (char *)r_base + size;
3541 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3546 b_base = (char *)g_base + size;
3547 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3552 sx_xunlock(&dev->mode_config.mutex);
3557 free_vblank_event(void *arg)
3560 free(arg, DRM_MEM_KMS);
3563 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3564 void *data, struct drm_file *file_priv)
3566 struct drm_mode_crtc_page_flip *page_flip = data;
3567 struct drm_mode_object *obj;
3568 struct drm_crtc *crtc;
3569 struct drm_framebuffer *fb;
3570 struct drm_pending_vblank_event *e = NULL;
3572 unsigned long flags;
3574 int hdisplay, vdisplay;
3577 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3578 page_flip->reserved != 0)
3581 sx_xlock(&dev->mode_config.mutex);
3582 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3585 crtc = obj_to_crtc(obj);
3587 if (crtc->fb == NULL) {
3588 /* The framebuffer is currently unbound, presumably
3589 * due to a hotplug event, that userspace has not
3596 if (crtc->funcs->page_flip == NULL)
3599 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3602 fb = obj_to_fb(obj);
3604 hdisplay = crtc->mode.hdisplay;
3605 vdisplay = crtc->mode.vdisplay;
3607 if (crtc->invert_dimensions) {
3610 vdisplay = hdisplay;
3614 if (hdisplay > fb->width ||
3615 vdisplay > fb->height ||
3616 crtc->x > fb->width - hdisplay ||
3617 crtc->y > fb->height - vdisplay) {
3618 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3619 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3620 crtc->invert_dimensions ? " (inverted)" : "");
3625 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3627 mtx_lock(&dev->event_lock);
3628 if (file_priv->event_space < sizeof e->event) {
3629 mtx_unlock(&dev->event_lock);
3632 file_priv->event_space -= sizeof e->event;
3633 mtx_unlock(&dev->event_lock);
3635 e = malloc(sizeof *e, DRM_MEM_KMS, M_WAITOK | M_ZERO);
3637 mtx_lock(&dev->event_lock);
3638 file_priv->event_space += sizeof e->event;
3639 mtx_unlock(&dev->event_lock);
3643 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3644 e->event.base.length = sizeof e->event;
3645 e->event.user_data = page_flip->user_data;
3646 e->base.event = &e->event.base;
3647 e->base.file_priv = file_priv;
3649 (void (*) (struct drm_pending_event *)) free_vblank_event;
3652 ret = crtc->funcs->page_flip(crtc, fb, e);
3654 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3655 mtx_lock(&dev->event_lock);
3656 file_priv->event_space += sizeof e->event;
3657 mtx_unlock(&dev->event_lock);
3658 free(e, DRM_MEM_KMS);
3663 sx_xunlock(&dev->mode_config.mutex);
3667 void drm_mode_config_reset(struct drm_device *dev)
3669 struct drm_crtc *crtc;
3670 struct drm_encoder *encoder;
3671 struct drm_connector *connector;
3673 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3674 if (crtc->funcs->reset)
3675 crtc->funcs->reset(crtc);
3677 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3678 if (encoder->funcs->reset)
3679 encoder->funcs->reset(encoder);
3681 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3682 connector->status = connector_status_unknown;
3684 if (connector->funcs->reset)
3685 connector->funcs->reset(connector);
3688 EXPORT_SYMBOL(drm_mode_config_reset);
3690 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3691 void *data, struct drm_file *file_priv)
3693 struct drm_mode_create_dumb *args = data;
3695 if (!dev->driver->dumb_create)
3697 return dev->driver->dumb_create(file_priv, dev, args);
3700 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3701 void *data, struct drm_file *file_priv)
3703 struct drm_mode_map_dumb *args = data;
3705 /* call driver ioctl to get mmap offset */
3706 if (!dev->driver->dumb_map_offset)
3709 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3712 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3713 void *data, struct drm_file *file_priv)
3715 struct drm_mode_destroy_dumb *args = data;
3717 if (!dev->driver->dumb_destroy)
3720 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3724 * Just need to support RGB formats here for compat with code that doesn't
3725 * use pixel formats directly yet.
3727 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3732 case DRM_FORMAT_RGB332:
3733 case DRM_FORMAT_BGR233:
3737 case DRM_FORMAT_XRGB1555:
3738 case DRM_FORMAT_XBGR1555:
3739 case DRM_FORMAT_RGBX5551:
3740 case DRM_FORMAT_BGRX5551:
3741 case DRM_FORMAT_ARGB1555:
3742 case DRM_FORMAT_ABGR1555:
3743 case DRM_FORMAT_RGBA5551:
3744 case DRM_FORMAT_BGRA5551:
3748 case DRM_FORMAT_RGB565:
3749 case DRM_FORMAT_BGR565:
3753 case DRM_FORMAT_RGB888:
3754 case DRM_FORMAT_BGR888:
3758 case DRM_FORMAT_XRGB8888:
3759 case DRM_FORMAT_XBGR8888:
3760 case DRM_FORMAT_RGBX8888:
3761 case DRM_FORMAT_BGRX8888:
3765 case DRM_FORMAT_XRGB2101010:
3766 case DRM_FORMAT_XBGR2101010:
3767 case DRM_FORMAT_RGBX1010102:
3768 case DRM_FORMAT_BGRX1010102:
3769 case DRM_FORMAT_ARGB2101010:
3770 case DRM_FORMAT_ABGR2101010:
3771 case DRM_FORMAT_RGBA1010102:
3772 case DRM_FORMAT_BGRA1010102:
3776 case DRM_FORMAT_ARGB8888:
3777 case DRM_FORMAT_ABGR8888:
3778 case DRM_FORMAT_RGBA8888:
3779 case DRM_FORMAT_BGRA8888:
3784 DRM_DEBUG_KMS("unsupported pixel format\n");
3790 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3793 * drm_format_num_planes - get the number of planes for format
3794 * @format: pixel format (DRM_FORMAT_*)
3797 * The number of planes used by the specified pixel format.
3799 int drm_format_num_planes(uint32_t format)
3802 case DRM_FORMAT_YUV410:
3803 case DRM_FORMAT_YVU410:
3804 case DRM_FORMAT_YUV411:
3805 case DRM_FORMAT_YVU411:
3806 case DRM_FORMAT_YUV420:
3807 case DRM_FORMAT_YVU420:
3808 case DRM_FORMAT_YUV422:
3809 case DRM_FORMAT_YVU422:
3810 case DRM_FORMAT_YUV444:
3811 case DRM_FORMAT_YVU444:
3813 case DRM_FORMAT_NV12:
3814 case DRM_FORMAT_NV21:
3815 case DRM_FORMAT_NV16:
3816 case DRM_FORMAT_NV61:
3817 case DRM_FORMAT_NV24:
3818 case DRM_FORMAT_NV42:
3824 EXPORT_SYMBOL(drm_format_num_planes);
3827 * drm_format_plane_cpp - determine the bytes per pixel value
3828 * @format: pixel format (DRM_FORMAT_*)
3829 * @plane: plane index
3832 * The bytes per pixel value for the specified plane.
3834 int drm_format_plane_cpp(uint32_t format, int plane)
3839 if (plane >= drm_format_num_planes(format))
3843 case DRM_FORMAT_YUYV:
3844 case DRM_FORMAT_YVYU:
3845 case DRM_FORMAT_UYVY:
3846 case DRM_FORMAT_VYUY:
3848 case DRM_FORMAT_NV12:
3849 case DRM_FORMAT_NV21:
3850 case DRM_FORMAT_NV16:
3851 case DRM_FORMAT_NV61:
3852 case DRM_FORMAT_NV24:
3853 case DRM_FORMAT_NV42:
3854 return plane ? 2 : 1;
3855 case DRM_FORMAT_YUV410:
3856 case DRM_FORMAT_YVU410:
3857 case DRM_FORMAT_YUV411:
3858 case DRM_FORMAT_YVU411:
3859 case DRM_FORMAT_YUV420:
3860 case DRM_FORMAT_YVU420:
3861 case DRM_FORMAT_YUV422:
3862 case DRM_FORMAT_YVU422:
3863 case DRM_FORMAT_YUV444:
3864 case DRM_FORMAT_YVU444:
3867 drm_fb_get_bpp_depth(format, &depth, &bpp);
3871 EXPORT_SYMBOL(drm_format_plane_cpp);
3874 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3875 * @format: pixel format (DRM_FORMAT_*)
3878 * The horizontal chroma subsampling factor for the
3879 * specified pixel format.
3881 int drm_format_horz_chroma_subsampling(uint32_t format)
3884 case DRM_FORMAT_YUV411:
3885 case DRM_FORMAT_YVU411:
3886 case DRM_FORMAT_YUV410:
3887 case DRM_FORMAT_YVU410:
3889 case DRM_FORMAT_YUYV:
3890 case DRM_FORMAT_YVYU:
3891 case DRM_FORMAT_UYVY:
3892 case DRM_FORMAT_VYUY:
3893 case DRM_FORMAT_NV12:
3894 case DRM_FORMAT_NV21:
3895 case DRM_FORMAT_NV16:
3896 case DRM_FORMAT_NV61:
3897 case DRM_FORMAT_YUV422:
3898 case DRM_FORMAT_YVU422:
3899 case DRM_FORMAT_YUV420:
3900 case DRM_FORMAT_YVU420:
3906 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3909 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3910 * @format: pixel format (DRM_FORMAT_*)
3913 * The vertical chroma subsampling factor for the
3914 * specified pixel format.
3916 int drm_format_vert_chroma_subsampling(uint32_t format)
3919 case DRM_FORMAT_YUV410:
3920 case DRM_FORMAT_YVU410:
3922 case DRM_FORMAT_YUV420:
3923 case DRM_FORMAT_YVU420:
3924 case DRM_FORMAT_NV12:
3925 case DRM_FORMAT_NV21:
3931 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);