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 #include <dev/drm2/drmP.h>
34 #include <dev/drm2/drm_crtc.h>
35 #include <dev/drm2/drm_edid.h>
36 #include <dev/drm2/drm_fourcc.h>
38 static void drm_property_destroy_blob(struct drm_device *dev,
39 struct drm_property_blob *blob);
41 /* Avoid boilerplate. I'm tired of typing. */
42 #define DRM_ENUM_NAME_FN(fnname, list) \
43 char *fnname(int val) \
46 for (i = 0; i < ARRAY_SIZE(list); i++) { \
47 if (list[i].type == val) \
48 return list[i].name; \
56 static struct drm_prop_enum_list drm_dpms_enum_list[] =
57 { { DRM_MODE_DPMS_ON, "On" },
58 { DRM_MODE_DPMS_STANDBY, "Standby" },
59 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
60 { DRM_MODE_DPMS_OFF, "Off" }
63 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
68 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
70 { DRM_MODE_SCALE_NONE, "None" },
71 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
72 { DRM_MODE_SCALE_CENTER, "Center" },
73 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
76 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
78 { DRM_MODE_DITHERING_OFF, "Off" },
79 { DRM_MODE_DITHERING_ON, "On" },
80 { DRM_MODE_DITHERING_AUTO, "Automatic" },
84 * Non-global properties, but "required" for certain connectors.
86 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
88 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
89 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
90 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
93 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
95 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
97 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
98 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
99 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
102 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103 drm_dvi_i_subconnector_enum_list)
105 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
107 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
111 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
114 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
116 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
118 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
125 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
126 drm_tv_subconnector_enum_list)
128 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
129 { DRM_MODE_DIRTY_OFF, "Off" },
130 { DRM_MODE_DIRTY_ON, "On" },
131 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
134 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
135 drm_dirty_info_enum_list)
137 struct drm_conn_prop_enum_list {
144 * Connector and encoder types.
146 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
147 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
148 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
149 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
150 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
151 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
152 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
153 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
154 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
155 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
156 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
157 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
158 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
159 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
160 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
161 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
162 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
165 static struct drm_prop_enum_list drm_encoder_enum_list[] =
166 { { DRM_MODE_ENCODER_NONE, "None" },
167 { DRM_MODE_ENCODER_DAC, "DAC" },
168 { DRM_MODE_ENCODER_TMDS, "TMDS" },
169 { DRM_MODE_ENCODER_LVDS, "LVDS" },
170 { DRM_MODE_ENCODER_TVDAC, "TV" },
171 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
174 char *drm_get_encoder_name(struct drm_encoder *encoder)
178 snprintf(buf, 32, "%s-%d",
179 drm_encoder_enum_list[encoder->encoder_type].name,
183 EXPORT_SYMBOL(drm_get_encoder_name);
185 char *drm_get_connector_name(struct drm_connector *connector)
189 snprintf(buf, 32, "%s-%d",
190 drm_connector_enum_list[connector->connector_type].name,
191 connector->connector_type_id);
194 EXPORT_SYMBOL(drm_get_connector_name);
196 char *drm_get_connector_status_name(enum drm_connector_status status)
198 if (status == connector_status_connected)
200 else if (status == connector_status_disconnected)
201 return "disconnected";
207 * drm_mode_object_get - allocate a new identifier
209 * @ptr: object pointer, used to generate unique ID
214 * Create a unique identifier based on @ptr in @dev's identifier space. Used
215 * for tracking modes, CRTCs and connectors.
218 * New unique (relative to other objects in @dev) integer identifier for the
221 static int drm_mode_object_get(struct drm_device *dev,
222 struct drm_mode_object *obj, uint32_t obj_type)
227 ret = drm_gem_name_create(&dev->mode_config.crtc_names, obj, &new_id);
232 obj->type = obj_type;
237 * drm_mode_object_put - free an identifer
242 * Caller must hold DRM mode_config lock.
244 * Free @id from @dev's unique identifier pool.
246 static void drm_mode_object_put(struct drm_device *dev,
247 struct drm_mode_object *object)
250 drm_gem_names_remove(&dev->mode_config.crtc_names, object->id);
253 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
254 uint32_t id, uint32_t type)
256 struct drm_mode_object *obj = NULL;
258 obj = drm_gem_name_ref(&dev->mode_config.crtc_names, id, NULL);
259 if (!obj || (obj->type != type) || (obj->id != id))
264 EXPORT_SYMBOL(drm_mode_object_find);
267 * drm_framebuffer_init - initialize a framebuffer
271 * Caller must hold mode config lock.
273 * Allocates an ID for the framebuffer's parent mode object, sets its mode
274 * functions & device file and adds it to the master fd list.
277 * Zero on success, error code on failure.
279 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
280 const struct drm_framebuffer_funcs *funcs)
284 refcount_init(&fb->refcount, 1);
286 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
292 dev->mode_config.num_fb++;
293 list_add(&fb->head, &dev->mode_config.fb_list);
297 EXPORT_SYMBOL(drm_framebuffer_init);
299 static void drm_framebuffer_free(struct drm_framebuffer *fb)
301 fb->funcs->destroy(fb);
305 * drm_framebuffer_unreference - unref a framebuffer
308 * Caller must hold mode config lock.
310 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
312 struct drm_device *dev = fb->dev;
313 DRM_DEBUG("FB ID: %d\n", fb->base.id);
314 if (!sx_xlocked(&dev->mode_config.mutex))
315 DRM_WARNING("%s: dev->mode_config.mutex not locked\n", __func__);
316 if (refcount_release(&fb->refcount))
317 drm_framebuffer_free(fb);
319 EXPORT_SYMBOL(drm_framebuffer_unreference);
322 * drm_framebuffer_reference - incr the fb refcnt
324 void drm_framebuffer_reference(struct drm_framebuffer *fb)
326 DRM_DEBUG("FB ID: %d\n", fb->base.id);
327 refcount_acquire(&fb->refcount);
329 EXPORT_SYMBOL(drm_framebuffer_reference);
332 * drm_framebuffer_cleanup - remove a framebuffer object
333 * @fb: framebuffer to remove
336 * Caller must hold mode config lock.
338 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
339 * it, setting it to NULL.
341 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
343 struct drm_device *dev = fb->dev;
345 * This could be moved to drm_framebuffer_remove(), but for
346 * debugging is nice to keep around the list of fb's that are
347 * no longer associated w/ a drm_file but are not unreferenced
348 * yet. (i915 and omapdrm have debugfs files which will show
351 drm_mode_object_put(dev, &fb->base);
353 dev->mode_config.num_fb--;
355 EXPORT_SYMBOL(drm_framebuffer_cleanup);
358 * drm_framebuffer_remove - remove and unreference a framebuffer object
359 * @fb: framebuffer to remove
362 * Caller must hold mode config lock.
364 * Scans all the CRTCs and planes in @dev's mode_config. If they're
365 * using @fb, removes it, setting it to NULL.
367 void drm_framebuffer_remove(struct drm_framebuffer *fb)
369 struct drm_device *dev = fb->dev;
370 struct drm_crtc *crtc;
371 struct drm_plane *plane;
372 struct drm_mode_set set;
375 /* remove from any CRTC */
376 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
377 if (crtc->fb == fb) {
378 /* should turn off the crtc */
379 memset(&set, 0, sizeof(struct drm_mode_set));
382 ret = crtc->funcs->set_config(&set);
384 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
388 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
389 if (plane->fb == fb) {
390 /* should turn off the crtc */
391 ret = plane->funcs->disable_plane(plane);
393 DRM_ERROR("failed to disable plane with busy fb\n");
394 /* disconnect the plane from the fb and crtc: */
400 list_del(&fb->filp_head);
402 drm_framebuffer_unreference(fb);
404 EXPORT_SYMBOL(drm_framebuffer_remove);
407 * drm_crtc_init - Initialise a new CRTC object
409 * @crtc: CRTC object to init
410 * @funcs: callbacks for the new CRTC
413 * Takes mode_config lock.
415 * Inits a new object created as base part of an driver crtc object.
418 * Zero on success, error code on failure.
420 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
421 const struct drm_crtc_funcs *funcs)
427 crtc->invert_dimensions = false;
429 sx_xlock(&dev->mode_config.mutex);
431 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
435 crtc->base.properties = &crtc->properties;
437 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
438 dev->mode_config.num_crtc++;
441 sx_xunlock(&dev->mode_config.mutex);
445 EXPORT_SYMBOL(drm_crtc_init);
448 * drm_crtc_cleanup - Cleans up the core crtc usage.
449 * @crtc: CRTC to cleanup
452 * Caller must hold mode config lock.
454 * Cleanup @crtc. Removes from drm modesetting space
455 * does NOT free object, caller does that.
457 void drm_crtc_cleanup(struct drm_crtc *crtc)
459 struct drm_device *dev = crtc->dev;
461 free(crtc->gamma_store, DRM_MEM_KMS);
462 crtc->gamma_store = NULL;
464 drm_mode_object_put(dev, &crtc->base);
465 list_del(&crtc->head);
466 dev->mode_config.num_crtc--;
468 EXPORT_SYMBOL(drm_crtc_cleanup);
471 * drm_mode_probed_add - add a mode to a connector's probed mode list
472 * @connector: connector the new mode
476 * Caller must hold mode config lock.
478 * Add @mode to @connector's mode list for later use.
480 void drm_mode_probed_add(struct drm_connector *connector,
481 struct drm_display_mode *mode)
483 list_add(&mode->head, &connector->probed_modes);
485 EXPORT_SYMBOL(drm_mode_probed_add);
488 * drm_mode_remove - remove and free a mode
489 * @connector: connector list to modify
490 * @mode: mode to remove
493 * Caller must hold mode config lock.
495 * Remove @mode from @connector's mode list, then free it.
497 void drm_mode_remove(struct drm_connector *connector,
498 struct drm_display_mode *mode)
500 list_del(&mode->head);
501 drm_mode_destroy(connector->dev, mode);
503 EXPORT_SYMBOL(drm_mode_remove);
506 * drm_connector_init - Init a preallocated connector
508 * @connector: the connector to init
509 * @funcs: callbacks for this connector
510 * @name: user visible name of the connector
513 * Takes mode config lock.
515 * Initialises a preallocated connector. Connectors should be
516 * subclassed as part of driver connector objects.
519 * Zero on success, error code on failure.
521 int drm_connector_init(struct drm_device *dev,
522 struct drm_connector *connector,
523 const struct drm_connector_funcs *funcs,
528 sx_xlock(&dev->mode_config.mutex);
530 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
534 connector->base.properties = &connector->properties;
535 connector->dev = dev;
536 connector->funcs = funcs;
537 connector->connector_type = connector_type;
538 connector->connector_type_id =
539 ++drm_connector_enum_list[connector_type].count; /* TODO */
540 INIT_LIST_HEAD(&connector->user_modes);
541 INIT_LIST_HEAD(&connector->probed_modes);
542 INIT_LIST_HEAD(&connector->modes);
543 connector->edid_blob_ptr = NULL;
544 connector->status = connector_status_unknown;
546 list_add_tail(&connector->head, &dev->mode_config.connector_list);
547 dev->mode_config.num_connector++;
549 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
550 drm_object_attach_property(&connector->base,
551 dev->mode_config.edid_property,
554 drm_object_attach_property(&connector->base,
555 dev->mode_config.dpms_property, 0);
558 sx_xunlock(&dev->mode_config.mutex);
562 EXPORT_SYMBOL(drm_connector_init);
565 * drm_connector_cleanup - cleans up an initialised connector
566 * @connector: connector to cleanup
569 * Takes mode config lock.
571 * Cleans up the connector but doesn't free the object.
573 void drm_connector_cleanup(struct drm_connector *connector)
575 struct drm_device *dev = connector->dev;
576 struct drm_display_mode *mode, *t;
578 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
579 drm_mode_remove(connector, mode);
581 list_for_each_entry_safe(mode, t, &connector->modes, head)
582 drm_mode_remove(connector, mode);
584 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
585 drm_mode_remove(connector, mode);
587 sx_xlock(&dev->mode_config.mutex);
588 drm_mode_object_put(dev, &connector->base);
589 list_del(&connector->head);
590 dev->mode_config.num_connector--;
591 sx_xunlock(&dev->mode_config.mutex);
593 EXPORT_SYMBOL(drm_connector_cleanup);
595 void drm_connector_unplug_all(struct drm_device *dev)
597 #ifdef FREEBSD_NOTYET
598 struct drm_connector *connector;
600 /* taking the mode config mutex ends up in a clash with sysfs */
601 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
602 drm_sysfs_connector_remove(connector);
603 #endif /* FREEBSD_NOTYET */
606 EXPORT_SYMBOL(drm_connector_unplug_all);
608 int drm_encoder_init(struct drm_device *dev,
609 struct drm_encoder *encoder,
610 const struct drm_encoder_funcs *funcs,
615 sx_xlock(&dev->mode_config.mutex);
617 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
622 encoder->encoder_type = encoder_type;
623 encoder->funcs = funcs;
625 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
626 dev->mode_config.num_encoder++;
629 sx_xunlock(&dev->mode_config.mutex);
633 EXPORT_SYMBOL(drm_encoder_init);
635 void drm_encoder_cleanup(struct drm_encoder *encoder)
637 struct drm_device *dev = encoder->dev;
638 sx_xlock(&dev->mode_config.mutex);
639 drm_mode_object_put(dev, &encoder->base);
640 list_del(&encoder->head);
641 dev->mode_config.num_encoder--;
642 sx_xunlock(&dev->mode_config.mutex);
644 EXPORT_SYMBOL(drm_encoder_cleanup);
646 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
647 unsigned long possible_crtcs,
648 const struct drm_plane_funcs *funcs,
649 const uint32_t *formats, uint32_t format_count,
654 sx_xlock(&dev->mode_config.mutex);
656 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
660 plane->base.properties = &plane->properties;
662 plane->funcs = funcs;
663 plane->format_types = malloc(sizeof(uint32_t) * format_count,
664 DRM_MEM_KMS, M_WAITOK);
665 if (!plane->format_types) {
666 DRM_DEBUG_KMS("out of memory when allocating plane\n");
667 drm_mode_object_put(dev, &plane->base);
672 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
673 plane->format_count = format_count;
674 plane->possible_crtcs = possible_crtcs;
676 /* private planes are not exposed to userspace, but depending on
677 * display hardware, might be convenient to allow sharing programming
678 * for the scanout engine with the crtc implementation.
681 list_add_tail(&plane->head, &dev->mode_config.plane_list);
682 dev->mode_config.num_plane++;
684 INIT_LIST_HEAD(&plane->head);
688 sx_xunlock(&dev->mode_config.mutex);
692 EXPORT_SYMBOL(drm_plane_init);
694 void drm_plane_cleanup(struct drm_plane *plane)
696 struct drm_device *dev = plane->dev;
698 sx_xlock(&dev->mode_config.mutex);
699 free(plane->format_types, DRM_MEM_KMS);
700 drm_mode_object_put(dev, &plane->base);
701 /* if not added to a list, it must be a private plane */
702 if (!list_empty(&plane->head)) {
703 list_del(&plane->head);
704 dev->mode_config.num_plane--;
706 sx_xunlock(&dev->mode_config.mutex);
708 EXPORT_SYMBOL(drm_plane_cleanup);
711 * drm_mode_create - create a new display mode
715 * Caller must hold DRM mode_config lock.
717 * Create a new drm_display_mode, give it an ID, and return it.
720 * Pointer to new mode on success, NULL on error.
722 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
724 struct drm_display_mode *nmode;
726 nmode = malloc(sizeof(struct drm_display_mode), DRM_MEM_KMS,
731 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
732 free(nmode, DRM_MEM_KMS);
738 EXPORT_SYMBOL(drm_mode_create);
741 * drm_mode_destroy - remove a mode
743 * @mode: mode to remove
746 * Caller must hold mode config lock.
748 * Free @mode's unique identifier, then free it.
750 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
755 drm_mode_object_put(dev, &mode->base);
757 free(mode, DRM_MEM_KMS);
759 EXPORT_SYMBOL(drm_mode_destroy);
761 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
763 struct drm_property *edid;
764 struct drm_property *dpms;
767 * Standard properties (apply to all connectors)
769 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
770 DRM_MODE_PROP_IMMUTABLE,
772 dev->mode_config.edid_property = edid;
774 dpms = drm_property_create_enum(dev, 0,
775 "DPMS", drm_dpms_enum_list,
776 ARRAY_SIZE(drm_dpms_enum_list));
777 dev->mode_config.dpms_property = dpms;
783 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
786 * Called by a driver the first time a DVI-I connector is made.
788 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
790 struct drm_property *dvi_i_selector;
791 struct drm_property *dvi_i_subconnector;
793 if (dev->mode_config.dvi_i_select_subconnector_property)
797 drm_property_create_enum(dev, 0,
798 "select subconnector",
799 drm_dvi_i_select_enum_list,
800 ARRAY_SIZE(drm_dvi_i_select_enum_list));
801 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
803 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
805 drm_dvi_i_subconnector_enum_list,
806 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
807 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
811 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
814 * drm_create_tv_properties - create TV specific connector properties
816 * @num_modes: number of different TV formats (modes) supported
817 * @modes: array of pointers to strings containing name of each format
819 * Called by a driver's TV initialization routine, this function creates
820 * the TV specific connector properties for a given device. Caller is
821 * responsible for allocating a list of format names and passing them to
824 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
827 struct drm_property *tv_selector;
828 struct drm_property *tv_subconnector;
831 if (dev->mode_config.tv_select_subconnector_property)
835 * Basic connector properties
837 tv_selector = drm_property_create_enum(dev, 0,
838 "select subconnector",
839 drm_tv_select_enum_list,
840 ARRAY_SIZE(drm_tv_select_enum_list));
841 dev->mode_config.tv_select_subconnector_property = tv_selector;
844 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
846 drm_tv_subconnector_enum_list,
847 ARRAY_SIZE(drm_tv_subconnector_enum_list));
848 dev->mode_config.tv_subconnector_property = tv_subconnector;
851 * Other, TV specific properties: margins & TV modes.
853 dev->mode_config.tv_left_margin_property =
854 drm_property_create_range(dev, 0, "left margin", 0, 100);
856 dev->mode_config.tv_right_margin_property =
857 drm_property_create_range(dev, 0, "right margin", 0, 100);
859 dev->mode_config.tv_top_margin_property =
860 drm_property_create_range(dev, 0, "top margin", 0, 100);
862 dev->mode_config.tv_bottom_margin_property =
863 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
865 dev->mode_config.tv_mode_property =
866 drm_property_create(dev, DRM_MODE_PROP_ENUM,
868 for (i = 0; i < num_modes; i++)
869 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
872 dev->mode_config.tv_brightness_property =
873 drm_property_create_range(dev, 0, "brightness", 0, 100);
875 dev->mode_config.tv_contrast_property =
876 drm_property_create_range(dev, 0, "contrast", 0, 100);
878 dev->mode_config.tv_flicker_reduction_property =
879 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
881 dev->mode_config.tv_overscan_property =
882 drm_property_create_range(dev, 0, "overscan", 0, 100);
884 dev->mode_config.tv_saturation_property =
885 drm_property_create_range(dev, 0, "saturation", 0, 100);
887 dev->mode_config.tv_hue_property =
888 drm_property_create_range(dev, 0, "hue", 0, 100);
892 EXPORT_SYMBOL(drm_mode_create_tv_properties);
895 * drm_mode_create_scaling_mode_property - create scaling mode property
898 * Called by a driver the first time it's needed, must be attached to desired
901 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
903 struct drm_property *scaling_mode;
905 if (dev->mode_config.scaling_mode_property)
909 drm_property_create_enum(dev, 0, "scaling mode",
910 drm_scaling_mode_enum_list,
911 ARRAY_SIZE(drm_scaling_mode_enum_list));
913 dev->mode_config.scaling_mode_property = scaling_mode;
917 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
920 * drm_mode_create_dithering_property - create dithering property
923 * Called by a driver the first time it's needed, must be attached to desired
926 int drm_mode_create_dithering_property(struct drm_device *dev)
928 struct drm_property *dithering_mode;
930 if (dev->mode_config.dithering_mode_property)
934 drm_property_create_enum(dev, 0, "dithering",
935 drm_dithering_mode_enum_list,
936 ARRAY_SIZE(drm_dithering_mode_enum_list));
937 dev->mode_config.dithering_mode_property = dithering_mode;
941 EXPORT_SYMBOL(drm_mode_create_dithering_property);
944 * drm_mode_create_dirty_property - create dirty property
947 * Called by a driver the first time it's needed, must be attached to desired
950 int drm_mode_create_dirty_info_property(struct drm_device *dev)
952 struct drm_property *dirty_info;
954 if (dev->mode_config.dirty_info_property)
958 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
960 drm_dirty_info_enum_list,
961 ARRAY_SIZE(drm_dirty_info_enum_list));
962 dev->mode_config.dirty_info_property = dirty_info;
966 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
969 * drm_mode_config_init - initialize DRM mode_configuration structure
973 * None, should happen single threaded at init time.
975 * Initialize @dev's mode_config structure, used for tracking the graphics
976 * configuration of @dev.
978 void drm_mode_config_init(struct drm_device *dev)
980 sx_init(&dev->mode_config.mutex, "kmslk");
981 INIT_LIST_HEAD(&dev->mode_config.fb_list);
982 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
983 INIT_LIST_HEAD(&dev->mode_config.connector_list);
984 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
985 INIT_LIST_HEAD(&dev->mode_config.property_list);
986 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
987 INIT_LIST_HEAD(&dev->mode_config.plane_list);
988 drm_gem_names_init(&dev->mode_config.crtc_names);
990 sx_xlock(&dev->mode_config.mutex);
991 drm_mode_create_standard_connector_properties(dev);
992 sx_xunlock(&dev->mode_config.mutex);
994 /* Just to be sure */
995 dev->mode_config.num_fb = 0;
996 dev->mode_config.num_connector = 0;
997 dev->mode_config.num_crtc = 0;
998 dev->mode_config.num_encoder = 0;
1000 EXPORT_SYMBOL(drm_mode_config_init);
1002 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1004 uint32_t total_objects = 0;
1006 total_objects += dev->mode_config.num_crtc;
1007 total_objects += dev->mode_config.num_connector;
1008 total_objects += dev->mode_config.num_encoder;
1010 group->id_list = malloc(total_objects * sizeof(uint32_t),
1011 DRM_MEM_KMS, M_WAITOK | M_ZERO);
1012 if (!group->id_list)
1015 group->num_crtcs = 0;
1016 group->num_connectors = 0;
1017 group->num_encoders = 0;
1021 void drm_mode_group_free(struct drm_mode_group *group)
1023 free(group->id_list, DRM_MEM_KMS);
1024 group->id_list = NULL;
1027 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1028 struct drm_mode_group *group)
1030 struct drm_crtc *crtc;
1031 struct drm_encoder *encoder;
1032 struct drm_connector *connector;
1035 if ((ret = drm_mode_group_init(dev, group)))
1038 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1039 group->id_list[group->num_crtcs++] = crtc->base.id;
1041 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1042 group->id_list[group->num_crtcs + group->num_encoders++] =
1045 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1046 group->id_list[group->num_crtcs + group->num_encoders +
1047 group->num_connectors++] = connector->base.id;
1051 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1054 * drm_mode_config_cleanup - free up DRM mode_config info
1058 * Caller must hold mode config lock.
1060 * Free up all the connectors and CRTCs associated with this DRM device, then
1061 * free up the framebuffers and associated buffer objects.
1063 * FIXME: cleanup any dangling user buffer objects too
1065 void drm_mode_config_cleanup(struct drm_device *dev)
1067 struct drm_connector *connector, *ot;
1068 struct drm_crtc *crtc, *ct;
1069 struct drm_encoder *encoder, *enct;
1070 struct drm_framebuffer *fb, *fbt;
1071 struct drm_property *property, *pt;
1072 struct drm_property_blob *blob, *bt;
1073 struct drm_plane *plane, *plt;
1075 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1077 encoder->funcs->destroy(encoder);
1080 list_for_each_entry_safe(connector, ot,
1081 &dev->mode_config.connector_list, head) {
1082 connector->funcs->destroy(connector);
1085 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1087 drm_property_destroy(dev, property);
1090 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
1092 drm_property_destroy_blob(dev, blob);
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 drm_gem_names_fini(&dev->mode_config.crtc_names);
1110 EXPORT_SYMBOL(drm_mode_config_cleanup);
1113 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1114 * @out: drm_mode_modeinfo struct to return to the user
1115 * @in: drm_display_mode to use
1120 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1123 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1124 const struct drm_display_mode *in)
1126 if (in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1127 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1128 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1129 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1130 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX)
1131 DRM_WARNING("timing values too large for mode info\n");
1133 out->clock = in->clock;
1134 out->hdisplay = in->hdisplay;
1135 out->hsync_start = in->hsync_start;
1136 out->hsync_end = in->hsync_end;
1137 out->htotal = in->htotal;
1138 out->hskew = in->hskew;
1139 out->vdisplay = in->vdisplay;
1140 out->vsync_start = in->vsync_start;
1141 out->vsync_end = in->vsync_end;
1142 out->vtotal = in->vtotal;
1143 out->vscan = in->vscan;
1144 out->vrefresh = in->vrefresh;
1145 out->flags = in->flags;
1146 out->type = in->type;
1147 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1148 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1152 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1153 * @out: drm_display_mode to return to the user
1154 * @in: drm_mode_modeinfo to use
1159 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1163 * Zero on success, errno on failure.
1165 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1166 const struct drm_mode_modeinfo *in)
1168 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1171 out->clock = in->clock;
1172 out->hdisplay = in->hdisplay;
1173 out->hsync_start = in->hsync_start;
1174 out->hsync_end = in->hsync_end;
1175 out->htotal = in->htotal;
1176 out->hskew = in->hskew;
1177 out->vdisplay = in->vdisplay;
1178 out->vsync_start = in->vsync_start;
1179 out->vsync_end = in->vsync_end;
1180 out->vtotal = in->vtotal;
1181 out->vscan = in->vscan;
1182 out->vrefresh = in->vrefresh;
1183 out->flags = in->flags;
1184 out->type = in->type;
1185 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1186 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1192 * drm_mode_getresources - get graphics configuration
1193 * @inode: inode from the ioctl
1194 * @filp: file * from the ioctl
1195 * @cmd: cmd from ioctl
1196 * @arg: arg from ioctl
1199 * Takes mode config lock.
1201 * Construct a set of configuration description structures and return
1202 * them to the user, including CRTC, connector and framebuffer configuration.
1204 * Called by the user via ioctl.
1207 * Zero on success, errno on failure.
1209 int drm_mode_getresources(struct drm_device *dev, void *data,
1210 struct drm_file *file_priv)
1212 struct drm_mode_card_res *card_res = data;
1213 struct list_head *lh;
1214 struct drm_framebuffer *fb;
1215 struct drm_connector *connector;
1216 struct drm_crtc *crtc;
1217 struct drm_encoder *encoder;
1219 int connector_count = 0;
1222 int encoder_count = 0;
1224 uint32_t __user *fb_id;
1225 uint32_t __user *crtc_id;
1226 uint32_t __user *connector_id;
1227 uint32_t __user *encoder_id;
1228 struct drm_mode_group *mode_group;
1230 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1233 sx_xlock(&dev->mode_config.mutex);
1236 * For the non-control nodes we need to limit the list of resources
1237 * by IDs in the group list for this node
1239 list_for_each(lh, &file_priv->fbs)
1242 mode_group = &file_priv->master->minor->mode_group;
1243 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1245 list_for_each(lh, &dev->mode_config.crtc_list)
1248 list_for_each(lh, &dev->mode_config.connector_list)
1251 list_for_each(lh, &dev->mode_config.encoder_list)
1255 crtc_count = mode_group->num_crtcs;
1256 connector_count = mode_group->num_connectors;
1257 encoder_count = mode_group->num_encoders;
1260 card_res->max_height = dev->mode_config.max_height;
1261 card_res->min_height = dev->mode_config.min_height;
1262 card_res->max_width = dev->mode_config.max_width;
1263 card_res->min_width = dev->mode_config.min_width;
1265 /* handle this in 4 parts */
1267 if (card_res->count_fbs >= fb_count) {
1269 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1270 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1271 if (put_user(fb->base.id, fb_id + copied)) {
1278 card_res->count_fbs = fb_count;
1281 if (card_res->count_crtcs >= crtc_count) {
1283 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1284 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1285 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1287 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1288 if (put_user(crtc->base.id, crtc_id + copied)) {
1295 for (i = 0; i < mode_group->num_crtcs; i++) {
1296 if (put_user(mode_group->id_list[i],
1297 crtc_id + copied)) {
1305 card_res->count_crtcs = crtc_count;
1308 if (card_res->count_encoders >= encoder_count) {
1310 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1311 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1312 list_for_each_entry(encoder,
1313 &dev->mode_config.encoder_list,
1315 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1316 drm_get_encoder_name(encoder));
1317 if (put_user(encoder->base.id, encoder_id +
1325 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1326 if (put_user(mode_group->id_list[i],
1327 encoder_id + copied)) {
1336 card_res->count_encoders = encoder_count;
1339 if (card_res->count_connectors >= connector_count) {
1341 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1342 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1343 list_for_each_entry(connector,
1344 &dev->mode_config.connector_list,
1346 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1348 drm_get_connector_name(connector));
1349 if (put_user(connector->base.id,
1350 connector_id + copied)) {
1357 int start = mode_group->num_crtcs +
1358 mode_group->num_encoders;
1359 for (i = start; i < start + mode_group->num_connectors; i++) {
1360 if (put_user(mode_group->id_list[i],
1361 connector_id + copied)) {
1369 card_res->count_connectors = connector_count;
1371 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1372 card_res->count_connectors, card_res->count_encoders);
1375 sx_xunlock(&dev->mode_config.mutex);
1380 * drm_mode_getcrtc - get CRTC configuration
1381 * @inode: inode from the ioctl
1382 * @filp: file * from the ioctl
1383 * @cmd: cmd from ioctl
1384 * @arg: arg from ioctl
1387 * Takes mode config lock.
1389 * Construct a CRTC configuration structure to return to the user.
1391 * Called by the user via ioctl.
1394 * Zero on success, errno on failure.
1396 int drm_mode_getcrtc(struct drm_device *dev,
1397 void *data, struct drm_file *file_priv)
1399 struct drm_mode_crtc *crtc_resp = data;
1400 struct drm_crtc *crtc;
1401 struct drm_mode_object *obj;
1404 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1407 sx_xlock(&dev->mode_config.mutex);
1409 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1410 DRM_MODE_OBJECT_CRTC);
1415 crtc = obj_to_crtc(obj);
1417 crtc_resp->x = crtc->x;
1418 crtc_resp->y = crtc->y;
1419 crtc_resp->gamma_size = crtc->gamma_size;
1421 crtc_resp->fb_id = crtc->fb->base.id;
1423 crtc_resp->fb_id = 0;
1425 if (crtc->enabled) {
1427 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1428 crtc_resp->mode_valid = 1;
1431 crtc_resp->mode_valid = 0;
1435 sx_xunlock(&dev->mode_config.mutex);
1440 * drm_mode_getconnector - get connector configuration
1441 * @inode: inode from the ioctl
1442 * @filp: file * from the ioctl
1443 * @cmd: cmd from ioctl
1444 * @arg: arg from ioctl
1447 * Takes mode config lock.
1449 * Construct a connector configuration structure to return to the user.
1451 * Called by the user via ioctl.
1454 * Zero on success, errno on failure.
1456 int drm_mode_getconnector(struct drm_device *dev, void *data,
1457 struct drm_file *file_priv)
1459 struct drm_mode_get_connector *out_resp = data;
1460 struct drm_mode_object *obj;
1461 struct drm_connector *connector;
1462 struct drm_display_mode *mode;
1464 int props_count = 0;
1465 int encoders_count = 0;
1469 struct drm_mode_modeinfo u_mode;
1470 struct drm_mode_modeinfo __user *mode_ptr;
1471 uint32_t __user *prop_ptr;
1472 uint64_t __user *prop_values;
1473 uint32_t __user *encoder_ptr;
1475 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1478 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1480 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1482 sx_xlock(&dev->mode_config.mutex);
1484 obj = drm_mode_object_find(dev, out_resp->connector_id,
1485 DRM_MODE_OBJECT_CONNECTOR);
1490 connector = obj_to_connector(obj);
1492 props_count = connector->properties.count;
1494 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1495 if (connector->encoder_ids[i] != 0) {
1500 if (out_resp->count_modes == 0) {
1501 connector->funcs->fill_modes(connector,
1502 dev->mode_config.max_width,
1503 dev->mode_config.max_height);
1506 /* delayed so we get modes regardless of pre-fill_modes state */
1507 list_for_each_entry(mode, &connector->modes, head)
1510 out_resp->connector_id = connector->base.id;
1511 out_resp->connector_type = connector->connector_type;
1512 out_resp->connector_type_id = connector->connector_type_id;
1513 out_resp->mm_width = connector->display_info.width_mm;
1514 out_resp->mm_height = connector->display_info.height_mm;
1515 out_resp->subpixel = connector->display_info.subpixel_order;
1516 out_resp->connection = connector->status;
1517 if (connector->encoder)
1518 out_resp->encoder_id = connector->encoder->base.id;
1520 out_resp->encoder_id = 0;
1523 * This ioctl is called twice, once to determine how much space is
1524 * needed, and the 2nd time to fill it.
1526 if ((out_resp->count_modes >= mode_count) && mode_count) {
1528 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1529 list_for_each_entry(mode, &connector->modes, head) {
1530 drm_crtc_convert_to_umode(&u_mode, mode);
1531 if (copy_to_user(mode_ptr + copied,
1532 &u_mode, sizeof(u_mode))) {
1539 out_resp->count_modes = mode_count;
1541 if ((out_resp->count_props >= props_count) && props_count) {
1543 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1544 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1545 for (i = 0; i < connector->properties.count; i++) {
1546 if (put_user(connector->properties.ids[i],
1547 prop_ptr + copied)) {
1552 if (put_user(connector->properties.values[i],
1553 prop_values + copied)) {
1560 out_resp->count_props = props_count;
1562 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1564 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1565 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1566 if (connector->encoder_ids[i] != 0) {
1567 if (put_user(connector->encoder_ids[i],
1568 encoder_ptr + copied)) {
1576 out_resp->count_encoders = encoders_count;
1579 sx_xunlock(&dev->mode_config.mutex);
1583 int drm_mode_getencoder(struct drm_device *dev, void *data,
1584 struct drm_file *file_priv)
1586 struct drm_mode_get_encoder *enc_resp = data;
1587 struct drm_mode_object *obj;
1588 struct drm_encoder *encoder;
1591 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1594 sx_xlock(&dev->mode_config.mutex);
1595 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1596 DRM_MODE_OBJECT_ENCODER);
1601 encoder = obj_to_encoder(obj);
1604 enc_resp->crtc_id = encoder->crtc->base.id;
1606 enc_resp->crtc_id = 0;
1607 enc_resp->encoder_type = encoder->encoder_type;
1608 enc_resp->encoder_id = encoder->base.id;
1609 enc_resp->possible_crtcs = encoder->possible_crtcs;
1610 enc_resp->possible_clones = encoder->possible_clones;
1613 sx_xunlock(&dev->mode_config.mutex);
1618 * drm_mode_getplane_res - get plane info
1621 * @file_priv: DRM file info
1624 * Takes mode config lock.
1626 * Return an plane count and set of IDs.
1628 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1629 struct drm_file *file_priv)
1631 struct drm_mode_get_plane_res *plane_resp = data;
1632 struct drm_mode_config *config;
1633 struct drm_plane *plane;
1634 uint32_t __user *plane_ptr;
1635 int copied = 0, ret = 0;
1637 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1640 sx_xlock(&dev->mode_config.mutex);
1641 config = &dev->mode_config;
1644 * This ioctl is called twice, once to determine how much space is
1645 * needed, and the 2nd time to fill it.
1647 if (config->num_plane &&
1648 (plane_resp->count_planes >= config->num_plane)) {
1649 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1651 list_for_each_entry(plane, &config->plane_list, head) {
1652 if (put_user(plane->base.id, plane_ptr + copied)) {
1659 plane_resp->count_planes = config->num_plane;
1662 sx_xunlock(&dev->mode_config.mutex);
1667 * drm_mode_getplane - get plane info
1670 * @file_priv: DRM file info
1673 * Takes mode config lock.
1675 * Return plane info, including formats supported, gamma size, any
1678 int drm_mode_getplane(struct drm_device *dev, void *data,
1679 struct drm_file *file_priv)
1681 struct drm_mode_get_plane *plane_resp = data;
1682 struct drm_mode_object *obj;
1683 struct drm_plane *plane;
1684 uint32_t __user *format_ptr;
1687 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1690 sx_xlock(&dev->mode_config.mutex);
1691 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1692 DRM_MODE_OBJECT_PLANE);
1697 plane = obj_to_plane(obj);
1700 plane_resp->crtc_id = plane->crtc->base.id;
1702 plane_resp->crtc_id = 0;
1705 plane_resp->fb_id = plane->fb->base.id;
1707 plane_resp->fb_id = 0;
1709 plane_resp->plane_id = plane->base.id;
1710 plane_resp->possible_crtcs = plane->possible_crtcs;
1711 plane_resp->gamma_size = plane->gamma_size;
1714 * This ioctl is called twice, once to determine how much space is
1715 * needed, and the 2nd time to fill it.
1717 if (plane->format_count &&
1718 (plane_resp->count_format_types >= plane->format_count)) {
1719 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1720 if (copy_to_user(format_ptr,
1721 plane->format_types,
1722 sizeof(uint32_t) * plane->format_count)) {
1727 plane_resp->count_format_types = plane->format_count;
1730 sx_xunlock(&dev->mode_config.mutex);
1735 * drm_mode_setplane - set up or tear down an plane
1737 * @data: ioctl data*
1738 * @file_prive: DRM file info
1741 * Takes mode config lock.
1743 * Set plane info, including placement, fb, scaling, and other factors.
1744 * Or pass a NULL fb to disable.
1746 int drm_mode_setplane(struct drm_device *dev, void *data,
1747 struct drm_file *file_priv)
1749 struct drm_mode_set_plane *plane_req = data;
1750 struct drm_mode_object *obj;
1751 struct drm_plane *plane;
1752 struct drm_crtc *crtc;
1753 struct drm_framebuffer *fb;
1755 unsigned int fb_width, fb_height;
1758 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1761 sx_xlock(&dev->mode_config.mutex);
1764 * First, find the plane, crtc, and fb objects. If not available,
1765 * we don't bother to call the driver.
1767 obj = drm_mode_object_find(dev, plane_req->plane_id,
1768 DRM_MODE_OBJECT_PLANE);
1770 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1771 plane_req->plane_id);
1775 plane = obj_to_plane(obj);
1777 /* No fb means shut it down */
1778 if (!plane_req->fb_id) {
1779 plane->funcs->disable_plane(plane);
1785 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1786 DRM_MODE_OBJECT_CRTC);
1788 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1789 plane_req->crtc_id);
1793 crtc = obj_to_crtc(obj);
1795 obj = drm_mode_object_find(dev, plane_req->fb_id,
1796 DRM_MODE_OBJECT_FB);
1798 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1803 fb = obj_to_fb(obj);
1805 /* Check whether this plane supports the fb pixel format. */
1806 for (i = 0; i < plane->format_count; i++)
1807 if (fb->pixel_format == plane->format_types[i])
1809 if (i == plane->format_count) {
1810 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1815 fb_width = fb->width << 16;
1816 fb_height = fb->height << 16;
1818 /* Make sure source coordinates are inside the fb. */
1819 if (plane_req->src_w > fb_width ||
1820 plane_req->src_x > fb_width - plane_req->src_w ||
1821 plane_req->src_h > fb_height ||
1822 plane_req->src_y > fb_height - plane_req->src_h) {
1823 DRM_DEBUG_KMS("Invalid source coordinates "
1824 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1825 plane_req->src_w >> 16,
1826 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1827 plane_req->src_h >> 16,
1828 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1829 plane_req->src_x >> 16,
1830 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1831 plane_req->src_y >> 16,
1832 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1837 /* Give drivers some help against integer overflows */
1838 if (plane_req->crtc_w > INT_MAX ||
1839 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1840 plane_req->crtc_h > INT_MAX ||
1841 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1842 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1843 plane_req->crtc_w, plane_req->crtc_h,
1844 plane_req->crtc_x, plane_req->crtc_y);
1849 ret = plane->funcs->update_plane(plane, crtc, fb,
1850 plane_req->crtc_x, plane_req->crtc_y,
1851 plane_req->crtc_w, plane_req->crtc_h,
1852 plane_req->src_x, plane_req->src_y,
1853 plane_req->src_w, plane_req->src_h);
1860 sx_xunlock(&dev->mode_config.mutex);
1866 * drm_mode_setcrtc - set CRTC configuration
1867 * @inode: inode from the ioctl
1868 * @filp: file * from the ioctl
1869 * @cmd: cmd from ioctl
1870 * @arg: arg from ioctl
1873 * Takes mode config lock.
1875 * Build a new CRTC configuration based on user request.
1877 * Called by the user via ioctl.
1880 * Zero on success, errno on failure.
1882 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1883 struct drm_file *file_priv)
1885 struct drm_mode_config *config = &dev->mode_config;
1886 struct drm_mode_crtc *crtc_req = data;
1887 struct drm_mode_object *obj;
1888 struct drm_crtc *crtc;
1889 struct drm_connector **connector_set = NULL, *connector;
1890 struct drm_framebuffer *fb = NULL;
1891 struct drm_display_mode *mode = NULL;
1892 struct drm_mode_set set;
1893 uint32_t __user *set_connectors_ptr;
1897 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1900 /* For some reason crtc x/y offsets are signed internally. */
1901 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1904 sx_xlock(&dev->mode_config.mutex);
1905 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1906 DRM_MODE_OBJECT_CRTC);
1908 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1912 crtc = obj_to_crtc(obj);
1913 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1915 if (crtc_req->mode_valid) {
1916 int hdisplay, vdisplay;
1917 /* If we have a mode we need a framebuffer. */
1918 /* If we pass -1, set the mode with the currently bound fb */
1919 if (crtc_req->fb_id == -1) {
1921 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1927 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1928 DRM_MODE_OBJECT_FB);
1930 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1935 fb = obj_to_fb(obj);
1938 mode = drm_mode_create(dev);
1944 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1946 DRM_DEBUG_KMS("Invalid mode\n");
1950 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1952 hdisplay = mode->hdisplay;
1953 vdisplay = mode->vdisplay;
1955 if (crtc->invert_dimensions) {
1958 vdisplay = hdisplay;
1962 if (hdisplay > fb->width ||
1963 vdisplay > fb->height ||
1964 crtc_req->x > fb->width - hdisplay ||
1965 crtc_req->y > fb->height - vdisplay) {
1966 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1967 fb->width, fb->height,
1968 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1969 crtc->invert_dimensions ? " (inverted)" : "");
1975 if (crtc_req->count_connectors == 0 && mode) {
1976 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1981 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1982 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1983 crtc_req->count_connectors);
1988 if (crtc_req->count_connectors > 0) {
1991 /* Avoid unbounded kernel memory allocation */
1992 if (crtc_req->count_connectors > config->num_connector) {
1997 connector_set = malloc(crtc_req->count_connectors *
1998 sizeof(struct drm_connector *),
1999 DRM_MEM_KMS, M_WAITOK);
2000 if (!connector_set) {
2005 for (i = 0; i < crtc_req->count_connectors; i++) {
2006 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2007 if (get_user(out_id, &set_connectors_ptr[i])) {
2012 obj = drm_mode_object_find(dev, out_id,
2013 DRM_MODE_OBJECT_CONNECTOR);
2015 DRM_DEBUG_KMS("Connector id %d unknown\n",
2020 connector = obj_to_connector(obj);
2021 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2023 drm_get_connector_name(connector));
2025 connector_set[i] = connector;
2030 set.x = crtc_req->x;
2031 set.y = crtc_req->y;
2033 set.connectors = connector_set;
2034 set.num_connectors = crtc_req->count_connectors;
2036 ret = crtc->funcs->set_config(&set);
2039 free(connector_set, DRM_MEM_KMS);
2040 drm_mode_destroy(dev, mode);
2041 sx_xunlock(&dev->mode_config.mutex);
2045 int drm_mode_cursor_ioctl(struct drm_device *dev,
2046 void *data, struct drm_file *file_priv)
2048 struct drm_mode_cursor *req = data;
2049 struct drm_mode_object *obj;
2050 struct drm_crtc *crtc;
2053 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2056 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2059 sx_xlock(&dev->mode_config.mutex);
2060 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2062 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2066 crtc = obj_to_crtc(obj);
2068 if (req->flags & DRM_MODE_CURSOR_BO) {
2069 if (!crtc->funcs->cursor_set) {
2073 /* Turns off the cursor if handle is 0 */
2074 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2075 req->width, req->height);
2078 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2079 if (crtc->funcs->cursor_move) {
2080 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2087 sx_xunlock(&dev->mode_config.mutex);
2091 /* Original addfb only supported RGB formats, so figure out which one */
2092 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2098 fmt = DRM_FORMAT_C8;
2102 fmt = DRM_FORMAT_XRGB1555;
2104 fmt = DRM_FORMAT_RGB565;
2107 fmt = DRM_FORMAT_RGB888;
2111 fmt = DRM_FORMAT_XRGB8888;
2112 else if (depth == 30)
2113 fmt = DRM_FORMAT_XRGB2101010;
2115 fmt = DRM_FORMAT_ARGB8888;
2118 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2119 fmt = DRM_FORMAT_XRGB8888;
2125 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2128 * drm_mode_addfb - add an FB to the graphics configuration
2129 * @inode: inode from the ioctl
2130 * @filp: file * from the ioctl
2131 * @cmd: cmd from ioctl
2132 * @arg: arg from ioctl
2135 * Takes mode config lock.
2137 * Add a new FB to the specified CRTC, given a user request.
2139 * Called by the user via ioctl.
2142 * Zero on success, errno on failure.
2144 int drm_mode_addfb(struct drm_device *dev,
2145 void *data, struct drm_file *file_priv)
2147 struct drm_mode_fb_cmd *or = data;
2148 struct drm_mode_fb_cmd2 r = {};
2149 struct drm_mode_config *config = &dev->mode_config;
2150 struct drm_framebuffer *fb;
2153 /* Use new struct with format internally */
2154 r.fb_id = or->fb_id;
2155 r.width = or->width;
2156 r.height = or->height;
2157 r.pitches[0] = or->pitch;
2158 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2159 r.handles[0] = or->handle;
2161 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2164 if ((config->min_width > r.width) || (r.width > config->max_width))
2167 if ((config->min_height > r.height) || (r.height > config->max_height))
2170 sx_xlock(&dev->mode_config.mutex);
2172 /* TODO check buffer is sufficiently large */
2173 /* TODO setup destructor callback */
2175 ret = dev->mode_config.funcs->fb_create(dev, file_priv, &r, &fb);
2177 DRM_DEBUG_KMS("could not create framebuffer\n");
2181 or->fb_id = fb->base.id;
2182 list_add(&fb->filp_head, &file_priv->fbs);
2183 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2186 sx_xunlock(&dev->mode_config.mutex);
2190 static int format_check(const struct drm_mode_fb_cmd2 *r)
2192 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2196 case DRM_FORMAT_RGB332:
2197 case DRM_FORMAT_BGR233:
2198 case DRM_FORMAT_XRGB4444:
2199 case DRM_FORMAT_XBGR4444:
2200 case DRM_FORMAT_RGBX4444:
2201 case DRM_FORMAT_BGRX4444:
2202 case DRM_FORMAT_ARGB4444:
2203 case DRM_FORMAT_ABGR4444:
2204 case DRM_FORMAT_RGBA4444:
2205 case DRM_FORMAT_BGRA4444:
2206 case DRM_FORMAT_XRGB1555:
2207 case DRM_FORMAT_XBGR1555:
2208 case DRM_FORMAT_RGBX5551:
2209 case DRM_FORMAT_BGRX5551:
2210 case DRM_FORMAT_ARGB1555:
2211 case DRM_FORMAT_ABGR1555:
2212 case DRM_FORMAT_RGBA5551:
2213 case DRM_FORMAT_BGRA5551:
2214 case DRM_FORMAT_RGB565:
2215 case DRM_FORMAT_BGR565:
2216 case DRM_FORMAT_RGB888:
2217 case DRM_FORMAT_BGR888:
2218 case DRM_FORMAT_XRGB8888:
2219 case DRM_FORMAT_XBGR8888:
2220 case DRM_FORMAT_RGBX8888:
2221 case DRM_FORMAT_BGRX8888:
2222 case DRM_FORMAT_ARGB8888:
2223 case DRM_FORMAT_ABGR8888:
2224 case DRM_FORMAT_RGBA8888:
2225 case DRM_FORMAT_BGRA8888:
2226 case DRM_FORMAT_XRGB2101010:
2227 case DRM_FORMAT_XBGR2101010:
2228 case DRM_FORMAT_RGBX1010102:
2229 case DRM_FORMAT_BGRX1010102:
2230 case DRM_FORMAT_ARGB2101010:
2231 case DRM_FORMAT_ABGR2101010:
2232 case DRM_FORMAT_RGBA1010102:
2233 case DRM_FORMAT_BGRA1010102:
2234 case DRM_FORMAT_YUYV:
2235 case DRM_FORMAT_YVYU:
2236 case DRM_FORMAT_UYVY:
2237 case DRM_FORMAT_VYUY:
2238 case DRM_FORMAT_AYUV:
2239 case DRM_FORMAT_NV12:
2240 case DRM_FORMAT_NV21:
2241 case DRM_FORMAT_NV16:
2242 case DRM_FORMAT_NV61:
2243 case DRM_FORMAT_NV24:
2244 case DRM_FORMAT_NV42:
2245 case DRM_FORMAT_YUV410:
2246 case DRM_FORMAT_YVU410:
2247 case DRM_FORMAT_YUV411:
2248 case DRM_FORMAT_YVU411:
2249 case DRM_FORMAT_YUV420:
2250 case DRM_FORMAT_YVU420:
2251 case DRM_FORMAT_YUV422:
2252 case DRM_FORMAT_YVU422:
2253 case DRM_FORMAT_YUV444:
2254 case DRM_FORMAT_YVU444:
2261 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2263 int ret, hsub, vsub, num_planes, i;
2265 ret = format_check(r);
2267 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
2271 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2272 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2273 num_planes = drm_format_num_planes(r->pixel_format);
2275 if (r->width == 0 || r->width % hsub) {
2276 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2280 if (r->height == 0 || r->height % vsub) {
2281 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2285 for (i = 0; i < num_planes; i++) {
2286 unsigned int width = r->width / (i != 0 ? hsub : 1);
2287 unsigned int height = r->height / (i != 0 ? vsub : 1);
2288 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2290 if (!r->handles[i]) {
2291 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2295 if ((uint64_t) width * cpp > UINT_MAX)
2298 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2301 if (r->pitches[i] < width * cpp) {
2302 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2311 * drm_mode_addfb2 - add an FB to the graphics configuration
2312 * @inode: inode from the ioctl
2313 * @filp: file * from the ioctl
2314 * @cmd: cmd from ioctl
2315 * @arg: arg from ioctl
2318 * Takes mode config lock.
2320 * Add a new FB to the specified CRTC, given a user request with format.
2322 * Called by the user via ioctl.
2325 * Zero on success, errno on failure.
2327 int drm_mode_addfb2(struct drm_device *dev,
2328 void *data, struct drm_file *file_priv)
2330 struct drm_mode_fb_cmd2 *r = data;
2331 struct drm_mode_config *config = &dev->mode_config;
2332 struct drm_framebuffer *fb;
2335 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2338 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2339 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2343 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2344 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2345 r->width, config->min_width, config->max_width);
2348 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2349 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2350 r->height, config->min_height, config->max_height);
2354 ret = framebuffer_check(r);
2358 sx_xlock(&dev->mode_config.mutex);
2360 ret = dev->mode_config.funcs->fb_create(dev, file_priv, r, &fb);
2362 DRM_DEBUG_KMS("could not create framebuffer\n");
2366 r->fb_id = fb->base.id;
2367 list_add(&fb->filp_head, &file_priv->fbs);
2368 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2371 sx_xunlock(&dev->mode_config.mutex);
2376 * drm_mode_rmfb - remove an FB from the configuration
2377 * @inode: inode from the ioctl
2378 * @filp: file * from the ioctl
2379 * @cmd: cmd from ioctl
2380 * @arg: arg from ioctl
2383 * Takes mode config lock.
2385 * Remove the FB specified by the user.
2387 * Called by the user via ioctl.
2390 * Zero on success, errno on failure.
2392 int drm_mode_rmfb(struct drm_device *dev,
2393 void *data, struct drm_file *file_priv)
2395 struct drm_mode_object *obj;
2396 struct drm_framebuffer *fb = NULL;
2397 struct drm_framebuffer *fbl = NULL;
2398 uint32_t *id = data;
2402 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2405 sx_xlock(&dev->mode_config.mutex);
2406 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
2407 /* TODO check that we really get a framebuffer back. */
2412 fb = obj_to_fb(obj);
2414 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2423 drm_framebuffer_remove(fb);
2426 sx_xunlock(&dev->mode_config.mutex);
2431 * drm_mode_getfb - get FB info
2432 * @inode: inode from the ioctl
2433 * @filp: file * from the ioctl
2434 * @cmd: cmd from ioctl
2435 * @arg: arg from ioctl
2438 * Takes mode config lock.
2440 * Lookup the FB given its ID and return info about it.
2442 * Called by the user via ioctl.
2445 * Zero on success, errno on failure.
2447 int drm_mode_getfb(struct drm_device *dev,
2448 void *data, struct drm_file *file_priv)
2450 struct drm_mode_fb_cmd *r = data;
2451 struct drm_mode_object *obj;
2452 struct drm_framebuffer *fb;
2455 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2458 sx_xlock(&dev->mode_config.mutex);
2459 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2464 fb = obj_to_fb(obj);
2466 r->height = fb->height;
2467 r->width = fb->width;
2468 r->depth = fb->depth;
2469 r->bpp = fb->bits_per_pixel;
2470 r->pitch = fb->pitches[0];
2472 fb->funcs->create_handle(fb, file_priv, &r->handle);
2475 sx_xunlock(&dev->mode_config.mutex);
2479 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2480 void *data, struct drm_file *file_priv)
2482 struct drm_clip_rect __user *clips_ptr;
2483 struct drm_clip_rect *clips = NULL;
2484 struct drm_mode_fb_dirty_cmd *r = data;
2485 struct drm_mode_object *obj;
2486 struct drm_framebuffer *fb;
2491 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2494 sx_xlock(&dev->mode_config.mutex);
2495 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2500 fb = obj_to_fb(obj);
2502 num_clips = r->num_clips;
2503 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2505 if (!num_clips != !clips_ptr) {
2510 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2512 /* If userspace annotates copy, clips must come in pairs */
2513 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2518 if (num_clips && clips_ptr) {
2519 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2523 clips = malloc(num_clips * sizeof(*clips), DRM_MEM_KMS,
2530 ret = copy_from_user(clips, clips_ptr,
2531 num_clips * sizeof(*clips));
2538 if (fb->funcs->dirty) {
2539 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2547 free(clips, DRM_MEM_KMS);
2549 sx_xunlock(&dev->mode_config.mutex);
2555 * drm_fb_release - remove and free the FBs on this file
2556 * @filp: file * from the ioctl
2559 * Takes mode config lock.
2561 * Destroy all the FBs associated with @filp.
2563 * Called by the user via ioctl.
2566 * Zero on success, errno on failure.
2568 void drm_fb_release(struct drm_file *priv)
2570 struct drm_device *dev = priv->minor->dev;
2571 struct drm_framebuffer *fb, *tfb;
2573 sx_xlock(&dev->mode_config.mutex);
2574 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2575 drm_framebuffer_remove(fb);
2577 sx_xunlock(&dev->mode_config.mutex);
2581 * drm_mode_attachmode - add a mode to the user mode list
2583 * @connector: connector to add the mode to
2584 * @mode: mode to add
2586 * Add @mode to @connector's user mode list.
2588 static void drm_mode_attachmode(struct drm_device *dev,
2589 struct drm_connector *connector,
2590 struct drm_display_mode *mode)
2592 list_add_tail(&mode->head, &connector->user_modes);
2595 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2596 const struct drm_display_mode *mode)
2598 struct drm_connector *connector;
2600 struct drm_display_mode *dup_mode, *next;
2601 DRM_LIST_HEAD(list);
2603 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2604 if (!connector->encoder)
2606 if (connector->encoder->crtc == crtc) {
2607 dup_mode = drm_mode_duplicate(dev, mode);
2612 list_add_tail(&dup_mode->head, &list);
2616 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2617 if (!connector->encoder)
2619 if (connector->encoder->crtc == crtc)
2620 list_move_tail(list.next, &connector->user_modes);
2623 MPASS(!list_empty(&list));
2626 list_for_each_entry_safe(dup_mode, next, &list, head)
2627 drm_mode_destroy(dev, dup_mode);
2631 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2633 static int drm_mode_detachmode(struct drm_device *dev,
2634 struct drm_connector *connector,
2635 struct drm_display_mode *mode)
2639 struct drm_display_mode *match_mode, *t;
2641 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2642 if (drm_mode_equal(match_mode, mode)) {
2643 list_del(&match_mode->head);
2644 drm_mode_destroy(dev, match_mode);
2656 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2658 struct drm_connector *connector;
2660 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2661 drm_mode_detachmode(dev, connector, mode);
2665 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2668 * drm_fb_attachmode - Attach a user mode to an connector
2669 * @inode: inode from the ioctl
2670 * @filp: file * from the ioctl
2671 * @cmd: cmd from ioctl
2672 * @arg: arg from ioctl
2674 * This attaches a user specified mode to an connector.
2675 * Called by the user via ioctl.
2678 * Zero on success, errno on failure.
2680 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2681 void *data, struct drm_file *file_priv)
2683 struct drm_mode_mode_cmd *mode_cmd = data;
2684 struct drm_connector *connector;
2685 struct drm_display_mode *mode;
2686 struct drm_mode_object *obj;
2687 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2690 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2693 sx_xlock(&dev->mode_config.mutex);
2695 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2700 connector = obj_to_connector(obj);
2702 mode = drm_mode_create(dev);
2708 ret = drm_crtc_convert_umode(mode, umode);
2710 DRM_DEBUG_KMS("Invalid mode\n");
2711 drm_mode_destroy(dev, mode);
2715 drm_mode_attachmode(dev, connector, mode);
2717 sx_xunlock(&dev->mode_config.mutex);
2723 * drm_fb_detachmode - Detach a user specified mode from an connector
2724 * @inode: inode from the ioctl
2725 * @filp: file * from the ioctl
2726 * @cmd: cmd from ioctl
2727 * @arg: arg from ioctl
2729 * Called by the user via ioctl.
2732 * Zero on success, errno on failure.
2734 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2735 void *data, struct drm_file *file_priv)
2737 struct drm_mode_object *obj;
2738 struct drm_mode_mode_cmd *mode_cmd = data;
2739 struct drm_connector *connector;
2740 struct drm_display_mode mode;
2741 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2744 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2747 sx_xlock(&dev->mode_config.mutex);
2749 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2754 connector = obj_to_connector(obj);
2756 ret = drm_crtc_convert_umode(&mode, umode);
2758 DRM_DEBUG_KMS("Invalid mode\n");
2762 ret = drm_mode_detachmode(dev, connector, &mode);
2764 sx_xunlock(&dev->mode_config.mutex);
2768 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2769 const char *name, int num_values)
2771 struct drm_property *property = NULL;
2774 property = malloc(sizeof(struct drm_property), DRM_MEM_KMS,
2780 property->values = malloc(sizeof(uint64_t)*num_values, DRM_MEM_KMS,
2782 if (!property->values)
2786 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2790 property->flags = flags;
2791 property->num_values = num_values;
2792 INIT_LIST_HEAD(&property->enum_blob_list);
2795 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2796 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2799 list_add_tail(&property->head, &dev->mode_config.property_list);
2802 free(property->values, DRM_MEM_KMS);
2803 free(property, DRM_MEM_KMS);
2806 EXPORT_SYMBOL(drm_property_create);
2808 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2810 const struct drm_prop_enum_list *props,
2813 struct drm_property *property;
2816 flags |= DRM_MODE_PROP_ENUM;
2818 property = drm_property_create(dev, flags, name, num_values);
2822 for (i = 0; i < num_values; i++) {
2823 ret = drm_property_add_enum(property, i,
2827 drm_property_destroy(dev, property);
2834 EXPORT_SYMBOL(drm_property_create_enum);
2836 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2837 int flags, const char *name,
2838 const struct drm_prop_enum_list *props,
2841 struct drm_property *property;
2844 flags |= DRM_MODE_PROP_BITMASK;
2846 property = drm_property_create(dev, flags, name, num_values);
2850 for (i = 0; i < num_values; i++) {
2851 ret = drm_property_add_enum(property, i,
2855 drm_property_destroy(dev, property);
2862 EXPORT_SYMBOL(drm_property_create_bitmask);
2864 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2866 uint64_t min, uint64_t max)
2868 struct drm_property *property;
2870 flags |= DRM_MODE_PROP_RANGE;
2872 property = drm_property_create(dev, flags, name, 2);
2876 property->values[0] = min;
2877 property->values[1] = max;
2881 EXPORT_SYMBOL(drm_property_create_range);
2883 int drm_property_add_enum(struct drm_property *property, int index,
2884 uint64_t value, const char *name)
2886 struct drm_property_enum *prop_enum;
2888 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2892 * Bitmask enum properties have the additional constraint of values
2895 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2898 if (!list_empty(&property->enum_blob_list)) {
2899 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2900 if (prop_enum->value == value) {
2901 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2902 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2908 prop_enum = malloc(sizeof(struct drm_property_enum), DRM_MEM_KMS,
2913 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2914 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2915 prop_enum->value = value;
2917 property->values[index] = value;
2918 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2921 EXPORT_SYMBOL(drm_property_add_enum);
2923 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2925 struct drm_property_enum *prop_enum, *pt;
2927 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2928 list_del(&prop_enum->head);
2929 free(prop_enum, DRM_MEM_KMS);
2932 if (property->num_values)
2933 free(property->values, DRM_MEM_KMS);
2934 drm_mode_object_put(dev, &property->base);
2935 list_del(&property->head);
2936 free(property, DRM_MEM_KMS);
2938 EXPORT_SYMBOL(drm_property_destroy);
2940 void drm_object_attach_property(struct drm_mode_object *obj,
2941 struct drm_property *property,
2944 int count = obj->properties->count;
2946 if (count == DRM_OBJECT_MAX_PROPERTY) {
2947 DRM_WARNING("Failed to attach object property (type: 0x%x). Please "
2948 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2949 "you see this message on the same object type.\n",
2954 obj->properties->ids[count] = property->base.id;
2955 obj->properties->values[count] = init_val;
2956 obj->properties->count++;
2958 EXPORT_SYMBOL(drm_object_attach_property);
2960 int drm_object_property_set_value(struct drm_mode_object *obj,
2961 struct drm_property *property, uint64_t val)
2965 for (i = 0; i < obj->properties->count; i++) {
2966 if (obj->properties->ids[i] == property->base.id) {
2967 obj->properties->values[i] = val;
2974 EXPORT_SYMBOL(drm_object_property_set_value);
2976 int drm_object_property_get_value(struct drm_mode_object *obj,
2977 struct drm_property *property, uint64_t *val)
2981 for (i = 0; i < obj->properties->count; i++) {
2982 if (obj->properties->ids[i] == property->base.id) {
2983 *val = obj->properties->values[i];
2990 EXPORT_SYMBOL(drm_object_property_get_value);
2992 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2993 void *data, struct drm_file *file_priv)
2995 struct drm_mode_object *obj;
2996 struct drm_mode_get_property *out_resp = data;
2997 struct drm_property *property;
3000 int value_count = 0;
3003 struct drm_property_enum *prop_enum;
3004 struct drm_mode_property_enum __user *enum_ptr;
3005 struct drm_property_blob *prop_blob;
3006 uint32_t __user *blob_id_ptr;
3007 uint64_t __user *values_ptr;
3008 uint32_t __user *blob_length_ptr;
3010 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3013 sx_xlock(&dev->mode_config.mutex);
3014 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3019 property = obj_to_property(obj);
3021 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3022 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3024 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3025 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3029 value_count = property->num_values;
3031 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3032 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3033 out_resp->flags = property->flags;
3035 if ((out_resp->count_values >= value_count) && value_count) {
3036 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3037 for (i = 0; i < value_count; i++) {
3038 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3044 out_resp->count_values = value_count;
3046 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3047 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3049 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3050 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3052 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3057 if (copy_to_user(&enum_ptr[copied].name,
3058 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3065 out_resp->count_enum_blobs = enum_count;
3068 if (property->flags & DRM_MODE_PROP_BLOB) {
3069 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3071 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3072 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3074 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3075 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3080 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3088 out_resp->count_enum_blobs = blob_count;
3091 sx_xunlock(&dev->mode_config.mutex);
3095 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3098 struct drm_property_blob *blob;
3101 if (!length || !data)
3104 blob = malloc(sizeof(struct drm_property_blob)+length, DRM_MEM_KMS,
3109 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3111 free(blob, DRM_MEM_KMS);
3115 blob->length = length;
3117 memcpy(blob->data, data, length);
3119 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3123 static void drm_property_destroy_blob(struct drm_device *dev,
3124 struct drm_property_blob *blob)
3126 drm_mode_object_put(dev, &blob->base);
3127 list_del(&blob->head);
3128 free(blob, DRM_MEM_KMS);
3131 int drm_mode_getblob_ioctl(struct drm_device *dev,
3132 void *data, struct drm_file *file_priv)
3134 struct drm_mode_object *obj;
3135 struct drm_mode_get_blob *out_resp = data;
3136 struct drm_property_blob *blob;
3138 void __user *blob_ptr;
3140 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3143 sx_xlock(&dev->mode_config.mutex);
3144 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3149 blob = obj_to_blob(obj);
3151 if (out_resp->length == blob->length) {
3152 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3153 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3158 out_resp->length = blob->length;
3161 sx_xunlock(&dev->mode_config.mutex);
3165 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3168 struct drm_device *dev = connector->dev;
3171 if (connector->edid_blob_ptr)
3172 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3174 /* Delete edid, when there is none. */
3176 connector->edid_blob_ptr = NULL;
3177 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3181 size = EDID_LENGTH * (1 + edid->extensions);
3182 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3184 if (!connector->edid_blob_ptr)
3187 ret = drm_object_property_set_value(&connector->base,
3188 dev->mode_config.edid_property,
3189 connector->edid_blob_ptr->base.id);
3193 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3195 static bool drm_property_change_is_valid(struct drm_property *property,
3198 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3200 if (property->flags & DRM_MODE_PROP_RANGE) {
3201 if (value < property->values[0] || value > property->values[1])
3204 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3206 uint64_t valid_mask = 0;
3207 for (i = 0; i < property->num_values; i++)
3208 valid_mask |= (1ULL << property->values[i]);
3209 return !(value & ~valid_mask);
3210 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3211 /* Only the driver knows */
3215 for (i = 0; i < property->num_values; i++)
3216 if (property->values[i] == value)
3222 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3223 void *data, struct drm_file *file_priv)
3225 struct drm_mode_connector_set_property *conn_set_prop = data;
3226 struct drm_mode_obj_set_property obj_set_prop = {
3227 .value = conn_set_prop->value,
3228 .prop_id = conn_set_prop->prop_id,
3229 .obj_id = conn_set_prop->connector_id,
3230 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3233 /* It does all the locking and checking we need */
3234 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3237 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3238 struct drm_property *property,
3242 struct drm_connector *connector = obj_to_connector(obj);
3244 /* Do DPMS ourselves */
3245 if (property == connector->dev->mode_config.dpms_property) {
3246 if (connector->funcs->dpms)
3247 (*connector->funcs->dpms)(connector, (int)value);
3249 } else if (connector->funcs->set_property)
3250 ret = connector->funcs->set_property(connector, property, value);
3252 /* store the property value if successful */
3254 drm_object_property_set_value(&connector->base, property, value);
3258 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3259 struct drm_property *property,
3263 struct drm_crtc *crtc = obj_to_crtc(obj);
3265 if (crtc->funcs->set_property)
3266 ret = crtc->funcs->set_property(crtc, property, value);
3268 drm_object_property_set_value(obj, property, value);
3273 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3274 struct drm_property *property,
3278 struct drm_plane *plane = obj_to_plane(obj);
3280 if (plane->funcs->set_property)
3281 ret = plane->funcs->set_property(plane, property, value);
3283 drm_object_property_set_value(obj, property, value);
3288 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3289 struct drm_file *file_priv)
3291 struct drm_mode_obj_get_properties *arg = data;
3292 struct drm_mode_object *obj;
3296 int props_count = 0;
3297 uint32_t __user *props_ptr;
3298 uint64_t __user *prop_values_ptr;
3300 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3303 sx_xlock(&dev->mode_config.mutex);
3305 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3310 if (!obj->properties) {
3315 props_count = obj->properties->count;
3317 /* This ioctl is called twice, once to determine how much space is
3318 * needed, and the 2nd time to fill it. */
3319 if ((arg->count_props >= props_count) && props_count) {
3321 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3322 prop_values_ptr = (uint64_t __user *)(unsigned long)
3323 (arg->prop_values_ptr);
3324 for (i = 0; i < props_count; i++) {
3325 if (put_user(obj->properties->ids[i],
3326 props_ptr + copied)) {
3330 if (put_user(obj->properties->values[i],
3331 prop_values_ptr + copied)) {
3338 arg->count_props = props_count;
3340 sx_xunlock(&dev->mode_config.mutex);
3344 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3345 struct drm_file *file_priv)
3347 struct drm_mode_obj_set_property *arg = data;
3348 struct drm_mode_object *arg_obj;
3349 struct drm_mode_object *prop_obj;
3350 struct drm_property *property;
3354 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3357 sx_xlock(&dev->mode_config.mutex);
3359 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3362 if (!arg_obj->properties)
3365 for (i = 0; i < arg_obj->properties->count; i++)
3366 if (arg_obj->properties->ids[i] == arg->prop_id)
3369 if (i == arg_obj->properties->count)
3372 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3373 DRM_MODE_OBJECT_PROPERTY);
3376 property = obj_to_property(prop_obj);
3378 if (!drm_property_change_is_valid(property, arg->value))
3381 switch (arg_obj->type) {
3382 case DRM_MODE_OBJECT_CONNECTOR:
3383 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3386 case DRM_MODE_OBJECT_CRTC:
3387 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3389 case DRM_MODE_OBJECT_PLANE:
3390 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3395 sx_xunlock(&dev->mode_config.mutex);
3399 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3400 struct drm_encoder *encoder)
3404 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3405 if (connector->encoder_ids[i] == 0) {
3406 connector->encoder_ids[i] = encoder->base.id;
3412 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3414 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3415 struct drm_encoder *encoder)
3418 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3419 if (connector->encoder_ids[i] == encoder->base.id) {
3420 connector->encoder_ids[i] = 0;
3421 if (connector->encoder == encoder)
3422 connector->encoder = NULL;
3427 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3429 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3432 crtc->gamma_size = gamma_size;
3434 crtc->gamma_store = malloc(gamma_size * sizeof(uint16_t) * 3,
3435 DRM_MEM_KMS, M_WAITOK | M_ZERO);
3436 if (!crtc->gamma_store) {
3437 crtc->gamma_size = 0;
3443 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3445 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3446 void *data, struct drm_file *file_priv)
3448 struct drm_mode_crtc_lut *crtc_lut = data;
3449 struct drm_mode_object *obj;
3450 struct drm_crtc *crtc;
3451 void *r_base, *g_base, *b_base;
3455 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3458 sx_xlock(&dev->mode_config.mutex);
3459 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3464 crtc = obj_to_crtc(obj);
3466 if (crtc->funcs->gamma_set == NULL) {
3471 /* memcpy into gamma store */
3472 if (crtc_lut->gamma_size != crtc->gamma_size) {
3477 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3478 r_base = crtc->gamma_store;
3479 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3484 g_base = (char *)r_base + size;
3485 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3490 b_base = (char *)g_base + size;
3491 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3496 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3499 sx_xunlock(&dev->mode_config.mutex);
3504 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3505 void *data, struct drm_file *file_priv)
3507 struct drm_mode_crtc_lut *crtc_lut = data;
3508 struct drm_mode_object *obj;
3509 struct drm_crtc *crtc;
3510 void *r_base, *g_base, *b_base;
3514 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3517 sx_xlock(&dev->mode_config.mutex);
3518 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3523 crtc = obj_to_crtc(obj);
3525 /* memcpy into gamma store */
3526 if (crtc_lut->gamma_size != crtc->gamma_size) {
3531 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3532 r_base = crtc->gamma_store;
3533 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3538 g_base = (char *)r_base + size;
3539 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3544 b_base = (char *)g_base + size;
3545 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3550 sx_xunlock(&dev->mode_config.mutex);
3555 free_vblank_event(void *arg)
3558 free(arg, DRM_MEM_KMS);
3561 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3562 void *data, struct drm_file *file_priv)
3564 struct drm_mode_crtc_page_flip *page_flip = data;
3565 struct drm_mode_object *obj;
3566 struct drm_crtc *crtc;
3567 struct drm_framebuffer *fb;
3568 struct drm_pending_vblank_event *e = NULL;
3570 unsigned long flags;
3572 int hdisplay, vdisplay;
3575 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3576 page_flip->reserved != 0)
3579 sx_xlock(&dev->mode_config.mutex);
3580 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3583 crtc = obj_to_crtc(obj);
3585 if (crtc->fb == NULL) {
3586 /* The framebuffer is currently unbound, presumably
3587 * due to a hotplug event, that userspace has not
3594 if (crtc->funcs->page_flip == NULL)
3597 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3600 fb = obj_to_fb(obj);
3602 hdisplay = crtc->mode.hdisplay;
3603 vdisplay = crtc->mode.vdisplay;
3605 if (crtc->invert_dimensions) {
3608 vdisplay = hdisplay;
3612 if (hdisplay > fb->width ||
3613 vdisplay > fb->height ||
3614 crtc->x > fb->width - hdisplay ||
3615 crtc->y > fb->height - vdisplay) {
3616 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3617 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3618 crtc->invert_dimensions ? " (inverted)" : "");
3623 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3625 mtx_lock(&dev->event_lock);
3626 if (file_priv->event_space < sizeof e->event) {
3627 mtx_unlock(&dev->event_lock);
3630 file_priv->event_space -= sizeof e->event;
3631 mtx_unlock(&dev->event_lock);
3633 e = malloc(sizeof *e, DRM_MEM_KMS, M_WAITOK | M_ZERO);
3635 mtx_lock(&dev->event_lock);
3636 file_priv->event_space += sizeof e->event;
3637 mtx_unlock(&dev->event_lock);
3641 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3642 e->event.base.length = sizeof e->event;
3643 e->event.user_data = page_flip->user_data;
3644 e->base.event = &e->event.base;
3645 e->base.file_priv = file_priv;
3647 (void (*) (struct drm_pending_event *)) free_vblank_event;
3650 ret = crtc->funcs->page_flip(crtc, fb, e);
3652 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3653 mtx_lock(&dev->event_lock);
3654 file_priv->event_space += sizeof e->event;
3655 mtx_unlock(&dev->event_lock);
3656 free(e, DRM_MEM_KMS);
3661 sx_xunlock(&dev->mode_config.mutex);
3665 void drm_mode_config_reset(struct drm_device *dev)
3667 struct drm_crtc *crtc;
3668 struct drm_encoder *encoder;
3669 struct drm_connector *connector;
3671 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3672 if (crtc->funcs->reset)
3673 crtc->funcs->reset(crtc);
3675 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3676 if (encoder->funcs->reset)
3677 encoder->funcs->reset(encoder);
3679 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3680 connector->status = connector_status_unknown;
3682 if (connector->funcs->reset)
3683 connector->funcs->reset(connector);
3686 EXPORT_SYMBOL(drm_mode_config_reset);
3688 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3689 void *data, struct drm_file *file_priv)
3691 struct drm_mode_create_dumb *args = data;
3693 if (!dev->driver->dumb_create)
3695 return dev->driver->dumb_create(file_priv, dev, args);
3698 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3699 void *data, struct drm_file *file_priv)
3701 struct drm_mode_map_dumb *args = data;
3703 /* call driver ioctl to get mmap offset */
3704 if (!dev->driver->dumb_map_offset)
3707 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3710 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3711 void *data, struct drm_file *file_priv)
3713 struct drm_mode_destroy_dumb *args = data;
3715 if (!dev->driver->dumb_destroy)
3718 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3722 * Just need to support RGB formats here for compat with code that doesn't
3723 * use pixel formats directly yet.
3725 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3730 case DRM_FORMAT_RGB332:
3731 case DRM_FORMAT_BGR233:
3735 case DRM_FORMAT_XRGB1555:
3736 case DRM_FORMAT_XBGR1555:
3737 case DRM_FORMAT_RGBX5551:
3738 case DRM_FORMAT_BGRX5551:
3739 case DRM_FORMAT_ARGB1555:
3740 case DRM_FORMAT_ABGR1555:
3741 case DRM_FORMAT_RGBA5551:
3742 case DRM_FORMAT_BGRA5551:
3746 case DRM_FORMAT_RGB565:
3747 case DRM_FORMAT_BGR565:
3751 case DRM_FORMAT_RGB888:
3752 case DRM_FORMAT_BGR888:
3756 case DRM_FORMAT_XRGB8888:
3757 case DRM_FORMAT_XBGR8888:
3758 case DRM_FORMAT_RGBX8888:
3759 case DRM_FORMAT_BGRX8888:
3763 case DRM_FORMAT_XRGB2101010:
3764 case DRM_FORMAT_XBGR2101010:
3765 case DRM_FORMAT_RGBX1010102:
3766 case DRM_FORMAT_BGRX1010102:
3767 case DRM_FORMAT_ARGB2101010:
3768 case DRM_FORMAT_ABGR2101010:
3769 case DRM_FORMAT_RGBA1010102:
3770 case DRM_FORMAT_BGRA1010102:
3774 case DRM_FORMAT_ARGB8888:
3775 case DRM_FORMAT_ABGR8888:
3776 case DRM_FORMAT_RGBA8888:
3777 case DRM_FORMAT_BGRA8888:
3782 DRM_DEBUG_KMS("unsupported pixel format\n");
3788 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3791 * drm_format_num_planes - get the number of planes for format
3792 * @format: pixel format (DRM_FORMAT_*)
3795 * The number of planes used by the specified pixel format.
3797 int drm_format_num_planes(uint32_t format)
3800 case DRM_FORMAT_YUV410:
3801 case DRM_FORMAT_YVU410:
3802 case DRM_FORMAT_YUV411:
3803 case DRM_FORMAT_YVU411:
3804 case DRM_FORMAT_YUV420:
3805 case DRM_FORMAT_YVU420:
3806 case DRM_FORMAT_YUV422:
3807 case DRM_FORMAT_YVU422:
3808 case DRM_FORMAT_YUV444:
3809 case DRM_FORMAT_YVU444:
3811 case DRM_FORMAT_NV12:
3812 case DRM_FORMAT_NV21:
3813 case DRM_FORMAT_NV16:
3814 case DRM_FORMAT_NV61:
3815 case DRM_FORMAT_NV24:
3816 case DRM_FORMAT_NV42:
3822 EXPORT_SYMBOL(drm_format_num_planes);
3825 * drm_format_plane_cpp - determine the bytes per pixel value
3826 * @format: pixel format (DRM_FORMAT_*)
3827 * @plane: plane index
3830 * The bytes per pixel value for the specified plane.
3832 int drm_format_plane_cpp(uint32_t format, int plane)
3837 if (plane >= drm_format_num_planes(format))
3841 case DRM_FORMAT_YUYV:
3842 case DRM_FORMAT_YVYU:
3843 case DRM_FORMAT_UYVY:
3844 case DRM_FORMAT_VYUY:
3846 case DRM_FORMAT_NV12:
3847 case DRM_FORMAT_NV21:
3848 case DRM_FORMAT_NV16:
3849 case DRM_FORMAT_NV61:
3850 case DRM_FORMAT_NV24:
3851 case DRM_FORMAT_NV42:
3852 return plane ? 2 : 1;
3853 case DRM_FORMAT_YUV410:
3854 case DRM_FORMAT_YVU410:
3855 case DRM_FORMAT_YUV411:
3856 case DRM_FORMAT_YVU411:
3857 case DRM_FORMAT_YUV420:
3858 case DRM_FORMAT_YVU420:
3859 case DRM_FORMAT_YUV422:
3860 case DRM_FORMAT_YVU422:
3861 case DRM_FORMAT_YUV444:
3862 case DRM_FORMAT_YVU444:
3865 drm_fb_get_bpp_depth(format, &depth, &bpp);
3869 EXPORT_SYMBOL(drm_format_plane_cpp);
3872 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3873 * @format: pixel format (DRM_FORMAT_*)
3876 * The horizontal chroma subsampling factor for the
3877 * specified pixel format.
3879 int drm_format_horz_chroma_subsampling(uint32_t format)
3882 case DRM_FORMAT_YUV411:
3883 case DRM_FORMAT_YVU411:
3884 case DRM_FORMAT_YUV410:
3885 case DRM_FORMAT_YVU410:
3887 case DRM_FORMAT_YUYV:
3888 case DRM_FORMAT_YVYU:
3889 case DRM_FORMAT_UYVY:
3890 case DRM_FORMAT_VYUY:
3891 case DRM_FORMAT_NV12:
3892 case DRM_FORMAT_NV21:
3893 case DRM_FORMAT_NV16:
3894 case DRM_FORMAT_NV61:
3895 case DRM_FORMAT_YUV422:
3896 case DRM_FORMAT_YVU422:
3897 case DRM_FORMAT_YUV420:
3898 case DRM_FORMAT_YVU420:
3904 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3907 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3908 * @format: pixel format (DRM_FORMAT_*)
3911 * The vertical chroma subsampling factor for the
3912 * specified pixel format.
3914 int drm_format_vert_chroma_subsampling(uint32_t format)
3917 case DRM_FORMAT_YUV410:
3918 case DRM_FORMAT_YVU410:
3920 case DRM_FORMAT_YUV420:
3921 case DRM_FORMAT_YVU420:
3922 case DRM_FORMAT_NV12:
3923 case DRM_FORMAT_NV21:
3929 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);