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/drm.h>
36 #include <dev/drm2/drmP.h>
37 #include <dev/drm2/drm_crtc.h>
38 #include <dev/drm2/drm_edid.h>
39 #include <dev/drm2/drm_fourcc.h>
40 #include <sys/limits.h>
42 /* Avoid boilerplate. I'm tired of typing. */
43 #define DRM_ENUM_NAME_FN(fnname, list) \
44 char *fnname(int val) \
47 for (i = 0; i < DRM_ARRAY_SIZE(list); i++) { \
48 if (list[i].type == val) \
49 return list[i].name; \
57 static struct drm_prop_enum_list drm_dpms_enum_list[] =
58 { { DRM_MODE_DPMS_ON, "On" },
59 { DRM_MODE_DPMS_STANDBY, "Standby" },
60 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
61 { DRM_MODE_DPMS_OFF, "Off" }
64 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
69 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
71 { DRM_MODE_SCALE_NONE, "None" },
72 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
73 { DRM_MODE_SCALE_CENTER, "Center" },
74 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
77 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
79 { DRM_MODE_DITHERING_OFF, "Off" },
80 { DRM_MODE_DITHERING_ON, "On" },
81 { DRM_MODE_DITHERING_AUTO, "Automatic" },
85 * Non-global properties, but "required" for certain connectors.
87 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
89 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
90 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
91 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
94 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
96 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
98 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
99 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
100 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
103 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
104 drm_dvi_i_subconnector_enum_list)
106 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
108 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
109 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
111 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
112 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
115 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
117 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
119 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
120 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
123 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
126 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
127 drm_tv_subconnector_enum_list)
129 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
130 { DRM_MODE_DIRTY_OFF, "Off" },
131 { DRM_MODE_DIRTY_ON, "On" },
132 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
135 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
136 drm_dirty_info_enum_list)
138 struct drm_conn_prop_enum_list {
145 * Connector and encoder types.
147 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
148 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
149 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
150 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
151 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
152 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
153 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
154 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
155 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
156 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
157 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
158 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
159 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
160 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
161 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
162 { DRM_MODE_CONNECTOR_eDP, "eDP", 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" },
173 static void drm_property_destroy_blob(struct drm_device *dev,
174 struct drm_property_blob *blob);
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,
186 char *drm_get_connector_name(struct drm_connector *connector)
190 snprintf(buf, 32, "%s-%d",
191 drm_connector_enum_list[connector->connector_type].name,
192 connector->connector_type_id);
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)
228 ret = drm_gem_name_create(&dev->mode_config.crtc_names, obj, &new_id);
233 obj->type = obj_type;
238 * drm_mode_object_put - free an identifer
243 * Caller must hold DRM mode_config lock.
245 * Free @id from @dev's unique identifier pool.
247 static void drm_mode_object_put(struct drm_device *dev,
248 struct drm_mode_object *object)
251 drm_gem_names_remove(&dev->mode_config.crtc_names, object->id);
254 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
255 uint32_t id, uint32_t type)
257 struct drm_mode_object *obj;
259 obj = drm_gem_name_ref(&dev->mode_config.crtc_names, id, NULL);
260 if (!obj || (obj->type != type) || (obj->id != id))
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 DRM_MODE_CONFIG_ASSERT_LOCKED(dev);
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);
299 * drm_framebuffer_cleanup - remove a framebuffer object
300 * @fb: framebuffer to remove
303 * Caller must hold mode config lock.
305 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
306 * it, setting it to NULL.
308 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
310 struct drm_device *dev = fb->dev;
311 struct drm_crtc *crtc;
312 struct drm_plane *plane;
313 struct drm_mode_set set;
316 DRM_MODE_CONFIG_ASSERT_LOCKED(dev);
318 /* remove from any CRTC */
319 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
320 if (crtc->fb == fb) {
321 /* should turn off the crtc */
322 memset(&set, 0, sizeof(struct drm_mode_set));
325 ret = crtc->funcs->set_config(&set);
327 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
331 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
332 if (plane->fb == fb) {
333 /* should turn off the crtc */
334 ret = plane->funcs->disable_plane(plane);
336 DRM_ERROR("failed to disable plane with busy fb\n");
337 /* disconnect the plane from the fb and crtc: */
343 drm_mode_object_put(dev, &fb->base);
345 dev->mode_config.num_fb--;
349 * drm_crtc_init - Initialise a new CRTC object
351 * @crtc: CRTC object to init
352 * @funcs: callbacks for the new CRTC
355 * Caller must hold mode config lock.
357 * Inits a new object created as base part of an driver crtc object.
360 * Zero on success, error code on failure.
362 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
363 const struct drm_crtc_funcs *funcs)
370 sx_xlock(&dev->mode_config.mutex);
371 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
375 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
376 dev->mode_config.num_crtc++;
378 sx_xunlock(&dev->mode_config.mutex);
384 * drm_crtc_cleanup - Cleans up the core crtc usage.
385 * @crtc: CRTC to cleanup
388 * Caller must hold mode config lock.
390 * Cleanup @crtc. Removes from drm modesetting space
391 * does NOT free object, caller does that.
393 void drm_crtc_cleanup(struct drm_crtc *crtc)
395 struct drm_device *dev = crtc->dev;
397 DRM_MODE_CONFIG_ASSERT_LOCKED(dev);
399 if (crtc->gamma_store) {
400 free(crtc->gamma_store, DRM_MEM_KMS);
401 crtc->gamma_store = NULL;
404 drm_mode_object_put(dev, &crtc->base);
405 list_del(&crtc->head);
406 dev->mode_config.num_crtc--;
410 * drm_mode_probed_add - add a mode to a connector's probed mode list
411 * @connector: connector the new mode
415 * Caller must hold mode config lock.
417 * Add @mode to @connector's mode list for later use.
419 void drm_mode_probed_add(struct drm_connector *connector,
420 struct drm_display_mode *mode)
423 DRM_MODE_CONFIG_ASSERT_LOCKED(connector->dev);
425 list_add(&mode->head, &connector->probed_modes);
429 * drm_mode_remove - remove and free a mode
430 * @connector: connector list to modify
431 * @mode: mode to remove
434 * Caller must hold mode config lock.
436 * Remove @mode from @connector's mode list, then free it.
438 void drm_mode_remove(struct drm_connector *connector,
439 struct drm_display_mode *mode)
442 DRM_MODE_CONFIG_ASSERT_LOCKED(connector->dev);
444 list_del(&mode->head);
445 drm_mode_destroy(connector->dev, mode);
449 * drm_connector_init - Init a preallocated connector
451 * @connector: the connector to init
452 * @funcs: callbacks for this connector
453 * @name: user visible name of the connector
456 * Takes mode config lock.
458 * Initialises a preallocated connector. Connectors should be
459 * subclassed as part of driver connector objects.
462 * Zero on success, error code on failure.
464 int drm_connector_init(struct drm_device *dev,
465 struct drm_connector *connector,
466 const struct drm_connector_funcs *funcs,
471 sx_xlock(&dev->mode_config.mutex);
473 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
477 connector->dev = dev;
478 connector->funcs = funcs;
479 connector->connector_type = connector_type;
480 connector->connector_type_id =
481 ++drm_connector_enum_list[connector_type].count; /* TODO */
482 INIT_LIST_HEAD(&connector->user_modes);
483 INIT_LIST_HEAD(&connector->probed_modes);
484 INIT_LIST_HEAD(&connector->modes);
485 connector->edid_blob_ptr = NULL;
487 list_add_tail(&connector->head, &dev->mode_config.connector_list);
488 dev->mode_config.num_connector++;
490 drm_connector_attach_property(connector,
491 dev->mode_config.edid_property, 0);
493 drm_connector_attach_property(connector,
494 dev->mode_config.dpms_property, 0);
497 sx_xunlock(&dev->mode_config.mutex);
503 * drm_connector_cleanup - cleans up an initialised connector
504 * @connector: connector to cleanup
507 * Takes mode config lock.
509 * Cleans up the connector but doesn't free the object.
511 void drm_connector_cleanup(struct drm_connector *connector)
513 struct drm_device *dev = connector->dev;
514 struct drm_display_mode *mode, *t;
516 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
517 drm_mode_remove(connector, mode);
519 list_for_each_entry_safe(mode, t, &connector->modes, head)
520 drm_mode_remove(connector, mode);
522 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
523 drm_mode_remove(connector, mode);
525 sx_xlock(&dev->mode_config.mutex);
526 if (connector->edid_blob_ptr)
527 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
528 drm_mode_object_put(dev, &connector->base);
529 list_del(&connector->head);
530 dev->mode_config.num_connector--;
531 sx_xunlock(&dev->mode_config.mutex);
534 int drm_encoder_init(struct drm_device *dev,
535 struct drm_encoder *encoder,
536 const struct drm_encoder_funcs *funcs,
541 sx_xlock(&dev->mode_config.mutex);
543 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
548 encoder->encoder_type = encoder_type;
549 encoder->funcs = funcs;
551 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
552 dev->mode_config.num_encoder++;
555 sx_xunlock(&dev->mode_config.mutex);
560 void drm_encoder_cleanup(struct drm_encoder *encoder)
562 struct drm_device *dev = encoder->dev;
564 sx_xlock(&dev->mode_config.mutex);
565 drm_mode_object_put(dev, &encoder->base);
566 list_del(&encoder->head);
567 dev->mode_config.num_encoder--;
568 sx_xunlock(&dev->mode_config.mutex);
571 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
572 unsigned long possible_crtcs,
573 const struct drm_plane_funcs *funcs,
574 const uint32_t *formats, uint32_t format_count,
579 sx_xlock(&dev->mode_config.mutex);
581 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
586 plane->funcs = funcs;
587 plane->format_types = malloc(sizeof(uint32_t) * format_count,
588 DRM_MEM_KMS, M_WAITOK);
590 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
591 plane->format_count = format_count;
592 plane->possible_crtcs = possible_crtcs;
594 /* private planes are not exposed to userspace, but depending on
595 * display hardware, might be convenient to allow sharing programming
596 * for the scanout engine with the crtc implementation.
599 list_add_tail(&plane->head, &dev->mode_config.plane_list);
600 dev->mode_config.num_plane++;
602 INIT_LIST_HEAD(&plane->head);
606 sx_xunlock(&dev->mode_config.mutex);
611 void drm_plane_cleanup(struct drm_plane *plane)
613 struct drm_device *dev = plane->dev;
615 sx_xlock(&dev->mode_config.mutex);
616 free(plane->format_types, DRM_MEM_KMS);
617 drm_mode_object_put(dev, &plane->base);
618 /* if not added to a list, it must be a private plane */
619 if (!list_empty(&plane->head)) {
620 list_del(&plane->head);
621 dev->mode_config.num_plane--;
623 sx_xunlock(&dev->mode_config.mutex);
627 * drm_mode_create - create a new display mode
631 * Caller must hold DRM mode_config lock.
633 * Create a new drm_display_mode, give it an ID, and return it.
636 * Pointer to new mode on success, NULL on error.
638 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
640 struct drm_display_mode *nmode;
642 nmode = malloc(sizeof(struct drm_display_mode), DRM_MEM_KMS,
645 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
646 free(nmode, DRM_MEM_KMS);
653 * drm_mode_destroy - remove a mode
655 * @mode: mode to remove
658 * Caller must hold mode config lock.
660 * Free @mode's unique identifier, then free it.
662 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
667 drm_mode_object_put(dev, &mode->base);
669 free(mode, DRM_MEM_KMS);
672 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
674 struct drm_property *edid;
675 struct drm_property *dpms;
678 * Standard properties (apply to all connectors)
680 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
681 DRM_MODE_PROP_IMMUTABLE,
683 dev->mode_config.edid_property = edid;
685 dpms = drm_property_create_enum(dev, 0,
686 "DPMS", drm_dpms_enum_list,
687 DRM_ARRAY_SIZE(drm_dpms_enum_list));
688 dev->mode_config.dpms_property = dpms;
694 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
697 * Called by a driver the first time a DVI-I connector is made.
699 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
701 struct drm_property *dvi_i_selector;
702 struct drm_property *dvi_i_subconnector;
704 if (dev->mode_config.dvi_i_select_subconnector_property)
708 drm_property_create_enum(dev, 0,
709 "select subconnector",
710 drm_dvi_i_select_enum_list,
711 DRM_ARRAY_SIZE(drm_dvi_i_select_enum_list));
712 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
714 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
716 drm_dvi_i_subconnector_enum_list,
717 DRM_ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
718 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
724 * drm_create_tv_properties - create TV specific connector properties
726 * @num_modes: number of different TV formats (modes) supported
727 * @modes: array of pointers to strings containing name of each format
729 * Called by a driver's TV initialization routine, this function creates
730 * the TV specific connector properties for a given device. Caller is
731 * responsible for allocating a list of format names and passing them to
734 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
737 struct drm_property *tv_selector;
738 struct drm_property *tv_subconnector;
741 if (dev->mode_config.tv_select_subconnector_property)
745 * Basic connector properties
747 tv_selector = drm_property_create_enum(dev, 0,
748 "select subconnector",
749 drm_tv_select_enum_list,
750 DRM_ARRAY_SIZE(drm_tv_select_enum_list));
751 dev->mode_config.tv_select_subconnector_property = tv_selector;
754 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
756 drm_tv_subconnector_enum_list,
757 DRM_ARRAY_SIZE(drm_tv_subconnector_enum_list));
758 dev->mode_config.tv_subconnector_property = tv_subconnector;
761 * Other, TV specific properties: margins & TV modes.
763 dev->mode_config.tv_left_margin_property =
764 drm_property_create_range(dev, 0, "left margin", 0, 100);
766 dev->mode_config.tv_right_margin_property =
767 drm_property_create_range(dev, 0, "right margin", 0, 100);
769 dev->mode_config.tv_top_margin_property =
770 drm_property_create_range(dev, 0, "top margin", 0, 100);
772 dev->mode_config.tv_bottom_margin_property =
773 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
775 dev->mode_config.tv_mode_property =
776 drm_property_create(dev, DRM_MODE_PROP_ENUM,
778 for (i = 0; i < num_modes; i++)
779 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
782 dev->mode_config.tv_brightness_property =
783 drm_property_create_range(dev, 0, "brightness", 0, 100);
785 dev->mode_config.tv_contrast_property =
786 drm_property_create_range(dev, 0, "contrast", 0, 100);
788 dev->mode_config.tv_flicker_reduction_property =
789 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
791 dev->mode_config.tv_overscan_property =
792 drm_property_create_range(dev, 0, "overscan", 0, 100);
794 dev->mode_config.tv_saturation_property =
795 drm_property_create_range(dev, 0, "saturation", 0, 100);
797 dev->mode_config.tv_hue_property =
798 drm_property_create_range(dev, 0, "hue", 0, 100);
804 * drm_mode_create_scaling_mode_property - create scaling mode property
807 * Called by a driver the first time it's needed, must be attached to desired
810 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
812 struct drm_property *scaling_mode;
814 if (dev->mode_config.scaling_mode_property)
818 drm_property_create_enum(dev, 0, "scaling mode",
819 drm_scaling_mode_enum_list,
820 DRM_ARRAY_SIZE(drm_scaling_mode_enum_list));
822 dev->mode_config.scaling_mode_property = scaling_mode;
828 * drm_mode_create_dithering_property - create dithering property
831 * Called by a driver the first time it's needed, must be attached to desired
834 int drm_mode_create_dithering_property(struct drm_device *dev)
836 struct drm_property *dithering_mode;
838 if (dev->mode_config.dithering_mode_property)
842 drm_property_create_enum(dev, 0, "dithering",
843 drm_dithering_mode_enum_list,
844 DRM_ARRAY_SIZE(drm_dithering_mode_enum_list));
845 dev->mode_config.dithering_mode_property = dithering_mode;
851 * drm_mode_create_dirty_property - create dirty property
854 * Called by a driver the first time it's needed, must be attached to desired
857 int drm_mode_create_dirty_info_property(struct drm_device *dev)
859 struct drm_property *dirty_info;
861 if (dev->mode_config.dirty_info_property)
865 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
867 drm_dirty_info_enum_list,
868 DRM_ARRAY_SIZE(drm_dirty_info_enum_list));
869 dev->mode_config.dirty_info_property = dirty_info;
875 * drm_mode_config_init - initialize DRM mode_configuration structure
879 * None, should happen single threaded at init time.
881 * Initialize @dev's mode_config structure, used for tracking the graphics
882 * configuration of @dev.
884 void drm_mode_config_init(struct drm_device *dev)
886 sx_init(&dev->mode_config.mutex, "kmslk");
887 INIT_LIST_HEAD(&dev->mode_config.fb_list);
888 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
889 INIT_LIST_HEAD(&dev->mode_config.connector_list);
890 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
891 INIT_LIST_HEAD(&dev->mode_config.property_list);
892 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
893 INIT_LIST_HEAD(&dev->mode_config.plane_list);
894 drm_gem_names_init(&dev->mode_config.crtc_names);
896 sx_xlock(&dev->mode_config.mutex);
897 drm_mode_create_standard_connector_properties(dev);
898 sx_xunlock(&dev->mode_config.mutex);
900 /* Just to be sure */
901 dev->mode_config.num_fb = 0;
902 dev->mode_config.num_connector = 0;
903 dev->mode_config.num_crtc = 0;
904 dev->mode_config.num_encoder = 0;
908 drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
910 uint32_t total_objects = 0;
912 total_objects += dev->mode_config.num_crtc;
913 total_objects += dev->mode_config.num_connector;
914 total_objects += dev->mode_config.num_encoder;
916 group->id_list = malloc(total_objects * sizeof(uint32_t),
917 DRM_MEM_KMS, M_WAITOK | M_ZERO);
919 group->num_crtcs = 0;
920 group->num_connectors = 0;
921 group->num_encoders = 0;
925 int drm_mode_group_init_legacy_group(struct drm_device *dev,
926 struct drm_mode_group *group)
928 struct drm_crtc *crtc;
929 struct drm_encoder *encoder;
930 struct drm_connector *connector;
933 if ((ret = drm_mode_group_init(dev, group)))
936 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
937 group->id_list[group->num_crtcs++] = crtc->base.id;
939 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
940 group->id_list[group->num_crtcs + group->num_encoders++] =
943 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
944 group->id_list[group->num_crtcs + group->num_encoders +
945 group->num_connectors++] = connector->base.id;
951 * drm_mode_config_cleanup - free up DRM mode_config info
955 * Caller must hold mode config lock.
957 * Free up all the connectors and CRTCs associated with this DRM device, then
958 * free up the framebuffers and associated buffer objects.
960 * FIXME: cleanup any dangling user buffer objects too
962 void drm_mode_config_cleanup(struct drm_device *dev)
964 struct drm_connector *connector, *ot;
965 struct drm_crtc *crtc, *ct;
966 struct drm_encoder *encoder, *enct;
967 struct drm_framebuffer *fb, *fbt;
968 struct drm_property *property, *pt;
969 struct drm_plane *plane, *plt;
971 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
973 encoder->funcs->destroy(encoder);
976 list_for_each_entry_safe(connector, ot,
977 &dev->mode_config.connector_list, head) {
978 connector->funcs->destroy(connector);
981 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
983 drm_property_destroy(dev, property);
986 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
987 fb->funcs->destroy(fb);
990 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
991 crtc->funcs->destroy(crtc);
994 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
996 plane->funcs->destroy(plane);
998 drm_gem_names_fini(&dev->mode_config.crtc_names);
1002 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1003 * @out: drm_mode_modeinfo struct to return to the user
1004 * @in: drm_display_mode to use
1009 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1012 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1013 const struct drm_display_mode *in)
1015 if (in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1016 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1017 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1018 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1019 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX)
1020 printf("timing values too large for mode info\n");
1022 out->clock = in->clock;
1023 out->hdisplay = in->hdisplay;
1024 out->hsync_start = in->hsync_start;
1025 out->hsync_end = in->hsync_end;
1026 out->htotal = in->htotal;
1027 out->hskew = in->hskew;
1028 out->vdisplay = in->vdisplay;
1029 out->vsync_start = in->vsync_start;
1030 out->vsync_end = in->vsync_end;
1031 out->vtotal = in->vtotal;
1032 out->vscan = in->vscan;
1033 out->vrefresh = in->vrefresh;
1034 out->flags = in->flags;
1035 out->type = in->type;
1036 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1037 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1041 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1042 * @out: drm_display_mode to return to the user
1043 * @in: drm_mode_modeinfo to use
1048 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1052 * Zero on success, errno on failure.
1054 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1055 const struct drm_mode_modeinfo *in)
1057 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1060 out->clock = in->clock;
1061 out->hdisplay = in->hdisplay;
1062 out->hsync_start = in->hsync_start;
1063 out->hsync_end = in->hsync_end;
1064 out->htotal = in->htotal;
1065 out->hskew = in->hskew;
1066 out->vdisplay = in->vdisplay;
1067 out->vsync_start = in->vsync_start;
1068 out->vsync_end = in->vsync_end;
1069 out->vtotal = in->vtotal;
1070 out->vscan = in->vscan;
1071 out->vrefresh = in->vrefresh;
1072 out->flags = in->flags;
1073 out->type = in->type;
1074 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1075 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1081 * drm_mode_getresources - get graphics configuration
1082 * @inode: inode from the ioctl
1083 * @filp: file * from the ioctl
1084 * @cmd: cmd from ioctl
1085 * @arg: arg from ioctl
1088 * Takes mode config lock.
1090 * Construct a set of configuration description structures and return
1091 * them to the user, including CRTC, connector and framebuffer configuration.
1093 * Called by the user via ioctl.
1096 * Zero on success, errno on failure.
1098 int drm_mode_getresources(struct drm_device *dev, void *data,
1099 struct drm_file *file_priv)
1101 struct drm_mode_card_res *card_res = data;
1102 struct list_head *lh;
1103 struct drm_framebuffer *fb;
1104 struct drm_connector *connector;
1105 struct drm_crtc *crtc;
1106 struct drm_encoder *encoder;
1108 int connector_count = 0;
1111 int encoder_count = 0;
1113 uint32_t __user *fb_id;
1114 uint32_t __user *crtc_id;
1115 uint32_t __user *connector_id;
1116 uint32_t __user *encoder_id;
1117 struct drm_mode_group *mode_group;
1119 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1122 sx_xlock(&dev->mode_config.mutex);
1125 * For the non-control nodes we need to limit the list of resources
1126 * by IDs in the group list for this node
1128 list_for_each(lh, &file_priv->fbs)
1132 mode_group = NULL; /* XXXKIB */
1133 if (1 || file_priv->master) {
1135 mode_group = &file_priv->masterp->minor->mode_group;
1136 if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
1139 list_for_each(lh, &dev->mode_config.crtc_list)
1142 list_for_each(lh, &dev->mode_config.connector_list)
1145 list_for_each(lh, &dev->mode_config.encoder_list)
1149 crtc_count = mode_group->num_crtcs;
1150 connector_count = mode_group->num_connectors;
1151 encoder_count = mode_group->num_encoders;
1154 card_res->max_height = dev->mode_config.max_height;
1155 card_res->min_height = dev->mode_config.min_height;
1156 card_res->max_width = dev->mode_config.max_width;
1157 card_res->min_width = dev->mode_config.min_width;
1159 /* handle this in 4 parts */
1161 if (card_res->count_fbs >= fb_count) {
1163 fb_id = (uint32_t *)(uintptr_t)card_res->fb_id_ptr;
1164 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1165 if (copyout(&fb->base.id, fb_id + copied,
1166 sizeof(uint32_t))) {
1173 card_res->count_fbs = fb_count;
1176 if (card_res->count_crtcs >= crtc_count) {
1178 crtc_id = (uint32_t *)(uintptr_t)card_res->crtc_id_ptr;
1180 if (1 || file_priv->master) {
1182 if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
1184 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1186 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1187 if (copyout(&crtc->base.id, crtc_id +
1188 copied, sizeof(uint32_t))) {
1195 for (i = 0; i < mode_group->num_crtcs; i++) {
1196 if (copyout(&mode_group->id_list[i],
1197 crtc_id + copied, sizeof(uint32_t))) {
1205 card_res->count_crtcs = crtc_count;
1208 if (card_res->count_encoders >= encoder_count) {
1210 encoder_id = (uint32_t *)(uintptr_t)card_res->encoder_id_ptr;
1212 if (file_priv->master) {
1214 if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
1216 list_for_each_entry(encoder,
1217 &dev->mode_config.encoder_list,
1219 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1220 drm_get_encoder_name(encoder));
1221 if (copyout(&encoder->base.id, encoder_id +
1222 copied, sizeof(uint32_t))) {
1229 for (i = mode_group->num_crtcs;
1230 i < mode_group->num_crtcs + mode_group->num_encoders;
1232 if (copyout(&mode_group->id_list[i],
1233 encoder_id + copied, sizeof(uint32_t))) {
1242 card_res->count_encoders = encoder_count;
1245 if (card_res->count_connectors >= connector_count) {
1247 connector_id = (uint32_t *)(uintptr_t)card_res->connector_id_ptr;
1249 if (file_priv->master) {
1251 if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
1253 list_for_each_entry(connector,
1254 &dev->mode_config.connector_list,
1256 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1258 drm_get_connector_name(connector));
1259 if (copyout(&connector->base.id,
1260 connector_id + copied, sizeof(uint32_t))) {
1267 int start = mode_group->num_crtcs +
1268 mode_group->num_encoders;
1269 for (i = start; i < start + mode_group->num_connectors; i++) {
1270 if (copyout(&mode_group->id_list[i],
1271 connector_id + copied, sizeof(uint32_t))) {
1279 card_res->count_connectors = connector_count;
1281 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1282 card_res->count_connectors, card_res->count_encoders);
1285 sx_xunlock(&dev->mode_config.mutex);
1290 * drm_mode_getcrtc - get CRTC configuration
1291 * @inode: inode from the ioctl
1292 * @filp: file * from the ioctl
1293 * @cmd: cmd from ioctl
1294 * @arg: arg from ioctl
1297 * Takes mode config lock.
1299 * Construct a CRTC configuration structure to return to the user.
1301 * Called by the user via ioctl.
1304 * Zero on success, errno on failure.
1306 int drm_mode_getcrtc(struct drm_device *dev,
1307 void *data, struct drm_file *file_priv)
1309 struct drm_mode_crtc *crtc_resp = data;
1310 struct drm_crtc *crtc;
1311 struct drm_mode_object *obj;
1314 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1317 sx_xlock(&dev->mode_config.mutex);
1319 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1320 DRM_MODE_OBJECT_CRTC);
1325 crtc = obj_to_crtc(obj);
1327 crtc_resp->x = crtc->x;
1328 crtc_resp->y = crtc->y;
1329 crtc_resp->gamma_size = crtc->gamma_size;
1331 crtc_resp->fb_id = crtc->fb->base.id;
1333 crtc_resp->fb_id = 0;
1335 if (crtc->enabled) {
1337 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1338 crtc_resp->mode_valid = 1;
1341 crtc_resp->mode_valid = 0;
1345 sx_xunlock(&dev->mode_config.mutex);
1350 * drm_mode_getconnector - get connector configuration
1351 * @inode: inode from the ioctl
1352 * @filp: file * from the ioctl
1353 * @cmd: cmd from ioctl
1354 * @arg: arg from ioctl
1357 * Takes mode config lock.
1359 * Construct a connector configuration structure to return to the user.
1361 * Called by the user via ioctl.
1364 * Zero on success, errno on failure.
1366 int drm_mode_getconnector(struct drm_device *dev, void *data,
1367 struct drm_file *file_priv)
1369 struct drm_mode_get_connector *out_resp = data;
1370 struct drm_mode_object *obj;
1371 struct drm_connector *connector;
1372 struct drm_display_mode *mode;
1374 int props_count = 0;
1375 int encoders_count = 0;
1379 struct drm_mode_modeinfo u_mode;
1380 struct drm_mode_modeinfo __user *mode_ptr;
1382 uint64_t *prop_values;
1383 uint32_t *encoder_ptr;
1385 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1388 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1390 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1392 sx_xlock(&dev->mode_config.mutex);
1394 obj = drm_mode_object_find(dev, out_resp->connector_id,
1395 DRM_MODE_OBJECT_CONNECTOR);
1400 connector = obj_to_connector(obj);
1402 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1403 if (connector->property_ids[i] != 0) {
1408 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1409 if (connector->encoder_ids[i] != 0) {
1414 if (out_resp->count_modes == 0) {
1415 connector->funcs->fill_modes(connector,
1416 dev->mode_config.max_width,
1417 dev->mode_config.max_height);
1420 /* delayed so we get modes regardless of pre-fill_modes state */
1421 list_for_each_entry(mode, &connector->modes, head)
1424 out_resp->connector_id = connector->base.id;
1425 out_resp->connector_type = connector->connector_type;
1426 out_resp->connector_type_id = connector->connector_type_id;
1427 out_resp->mm_width = connector->display_info.width_mm;
1428 out_resp->mm_height = connector->display_info.height_mm;
1429 out_resp->subpixel = connector->display_info.subpixel_order;
1430 out_resp->connection = connector->status;
1431 if (connector->encoder)
1432 out_resp->encoder_id = connector->encoder->base.id;
1434 out_resp->encoder_id = 0;
1437 * This ioctl is called twice, once to determine how much space is
1438 * needed, and the 2nd time to fill it.
1440 if ((out_resp->count_modes >= mode_count) && mode_count) {
1442 mode_ptr = (struct drm_mode_modeinfo *)(uintptr_t)out_resp->modes_ptr;
1443 list_for_each_entry(mode, &connector->modes, head) {
1444 drm_crtc_convert_to_umode(&u_mode, mode);
1445 if (copyout(&u_mode, mode_ptr + copied,
1453 out_resp->count_modes = mode_count;
1455 if ((out_resp->count_props >= props_count) && props_count) {
1457 prop_ptr = (uint32_t *)(uintptr_t)(out_resp->props_ptr);
1458 prop_values = (uint64_t *)(uintptr_t)(out_resp->prop_values_ptr);
1459 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1460 if (connector->property_ids[i] != 0) {
1461 if (copyout(&connector->property_ids[i],
1462 prop_ptr + copied, sizeof(uint32_t))) {
1467 if (copyout(&connector->property_values[i],
1468 prop_values + copied, sizeof(uint64_t))) {
1476 out_resp->count_props = props_count;
1478 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1480 encoder_ptr = (uint32_t *)(uintptr_t)(out_resp->encoders_ptr);
1481 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1482 if (connector->encoder_ids[i] != 0) {
1483 if (copyout(&connector->encoder_ids[i],
1484 encoder_ptr + copied, sizeof(uint32_t))) {
1492 out_resp->count_encoders = encoders_count;
1495 sx_xunlock(&dev->mode_config.mutex);
1499 int drm_mode_getencoder(struct drm_device *dev, void *data,
1500 struct drm_file *file_priv)
1502 struct drm_mode_get_encoder *enc_resp = data;
1503 struct drm_mode_object *obj;
1504 struct drm_encoder *encoder;
1507 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1510 sx_xlock(&dev->mode_config.mutex);
1511 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1512 DRM_MODE_OBJECT_ENCODER);
1517 encoder = obj_to_encoder(obj);
1520 enc_resp->crtc_id = encoder->crtc->base.id;
1522 enc_resp->crtc_id = 0;
1523 enc_resp->encoder_type = encoder->encoder_type;
1524 enc_resp->encoder_id = encoder->base.id;
1525 enc_resp->possible_crtcs = encoder->possible_crtcs;
1526 enc_resp->possible_clones = encoder->possible_clones;
1529 sx_xunlock(&dev->mode_config.mutex);
1534 * drm_mode_getplane_res - get plane info
1537 * @file_priv: DRM file info
1540 * Takes mode config lock.
1542 * Return an plane count and set of IDs.
1544 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1545 struct drm_file *file_priv)
1547 struct drm_mode_get_plane_res *plane_resp = data;
1548 struct drm_mode_config *config;
1549 struct drm_plane *plane;
1550 uint32_t *plane_ptr;
1551 int copied = 0, ret = 0;
1553 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1556 sx_xlock(&dev->mode_config.mutex);
1557 config = &dev->mode_config;
1560 * This ioctl is called twice, once to determine how much space is
1561 * needed, and the 2nd time to fill it.
1563 if (config->num_plane &&
1564 (plane_resp->count_planes >= config->num_plane)) {
1565 plane_ptr = (uint32_t *)(unsigned long)plane_resp->plane_id_ptr;
1567 list_for_each_entry(plane, &config->plane_list, head) {
1568 if (copyout(&plane->base.id, plane_ptr + copied,
1569 sizeof(uint32_t))) {
1576 plane_resp->count_planes = config->num_plane;
1579 sx_xunlock(&dev->mode_config.mutex);
1584 * drm_mode_getplane - get plane info
1587 * @file_priv: DRM file info
1590 * Takes mode config lock.
1592 * Return plane info, including formats supported, gamma size, any
1595 int drm_mode_getplane(struct drm_device *dev, void *data,
1596 struct drm_file *file_priv)
1598 struct drm_mode_get_plane *plane_resp = data;
1599 struct drm_mode_object *obj;
1600 struct drm_plane *plane;
1601 uint32_t *format_ptr;
1604 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1607 sx_xlock(&dev->mode_config.mutex);
1608 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1609 DRM_MODE_OBJECT_PLANE);
1614 plane = obj_to_plane(obj);
1617 plane_resp->crtc_id = plane->crtc->base.id;
1619 plane_resp->crtc_id = 0;
1622 plane_resp->fb_id = plane->fb->base.id;
1624 plane_resp->fb_id = 0;
1626 plane_resp->plane_id = plane->base.id;
1627 plane_resp->possible_crtcs = plane->possible_crtcs;
1628 plane_resp->gamma_size = plane->gamma_size;
1631 * This ioctl is called twice, once to determine how much space is
1632 * needed, and the 2nd time to fill it.
1634 if (plane->format_count &&
1635 (plane_resp->count_format_types >= plane->format_count)) {
1636 format_ptr = (uint32_t *)(unsigned long)plane_resp->format_type_ptr;
1637 if (copyout(format_ptr,
1638 plane->format_types,
1639 sizeof(uint32_t) * plane->format_count)) {
1644 plane_resp->count_format_types = plane->format_count;
1647 sx_xunlock(&dev->mode_config.mutex);
1652 * drm_mode_setplane - set up or tear down an plane
1654 * @data: ioctl data*
1655 * @file_prive: DRM file info
1658 * Takes mode config lock.
1660 * Set plane info, including placement, fb, scaling, and other factors.
1661 * Or pass a NULL fb to disable.
1663 int drm_mode_setplane(struct drm_device *dev, void *data,
1664 struct drm_file *file_priv)
1666 struct drm_mode_set_plane *plane_req = data;
1667 struct drm_mode_object *obj;
1668 struct drm_plane *plane;
1669 struct drm_crtc *crtc;
1670 struct drm_framebuffer *fb;
1672 unsigned int fb_width, fb_height;
1675 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1678 sx_xlock(&dev->mode_config.mutex);
1681 * First, find the plane, crtc, and fb objects. If not available,
1682 * we don't bother to call the driver.
1684 obj = drm_mode_object_find(dev, plane_req->plane_id,
1685 DRM_MODE_OBJECT_PLANE);
1687 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1688 plane_req->plane_id);
1692 plane = obj_to_plane(obj);
1694 /* No fb means shut it down */
1695 if (!plane_req->fb_id) {
1696 plane->funcs->disable_plane(plane);
1702 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1703 DRM_MODE_OBJECT_CRTC);
1705 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1706 plane_req->crtc_id);
1710 crtc = obj_to_crtc(obj);
1712 obj = drm_mode_object_find(dev, plane_req->fb_id,
1713 DRM_MODE_OBJECT_FB);
1715 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1720 fb = obj_to_fb(obj);
1722 /* Check whether this plane supports the fb pixel format. */
1723 for (i = 0; i < plane->format_count; i++)
1724 if (fb->pixel_format == plane->format_types[i])
1726 if (i == plane->format_count) {
1727 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1732 fb_width = fb->width << 16;
1733 fb_height = fb->height << 16;
1735 /* Make sure source coordinates are inside the fb. */
1736 if (plane_req->src_w > fb_width ||
1737 plane_req->src_x > fb_width - plane_req->src_w ||
1738 plane_req->src_h > fb_height ||
1739 plane_req->src_y > fb_height - plane_req->src_h) {
1740 DRM_DEBUG_KMS("Invalid source coordinates "
1741 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1742 plane_req->src_w >> 16,
1743 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1744 plane_req->src_h >> 16,
1745 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1746 plane_req->src_x >> 16,
1747 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1748 plane_req->src_y >> 16,
1749 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1754 /* Give drivers some help against integer overflows */
1755 if (plane_req->crtc_w > INT_MAX ||
1756 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1757 plane_req->crtc_h > INT_MAX ||
1758 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1759 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1760 plane_req->crtc_w, plane_req->crtc_h,
1761 plane_req->crtc_x, plane_req->crtc_y);
1766 ret = -plane->funcs->update_plane(plane, crtc, fb,
1767 plane_req->crtc_x, plane_req->crtc_y,
1768 plane_req->crtc_w, plane_req->crtc_h,
1769 plane_req->src_x, plane_req->src_y,
1770 plane_req->src_w, plane_req->src_h);
1777 sx_xunlock(&dev->mode_config.mutex);
1783 * drm_mode_setcrtc - set CRTC configuration
1784 * @inode: inode from the ioctl
1785 * @filp: file * from the ioctl
1786 * @cmd: cmd from ioctl
1787 * @arg: arg from ioctl
1790 * Takes mode config lock.
1792 * Build a new CRTC configuration based on user request.
1794 * Called by the user via ioctl.
1797 * Zero on success, errno on failure.
1799 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1800 struct drm_file *file_priv)
1802 struct drm_mode_config *config = &dev->mode_config;
1803 struct drm_mode_crtc *crtc_req = data;
1804 struct drm_mode_object *obj;
1805 struct drm_crtc *crtc;
1806 struct drm_connector **connector_set = NULL, *connector;
1807 struct drm_framebuffer *fb = NULL;
1808 struct drm_display_mode *mode = NULL;
1809 struct drm_mode_set set;
1810 uint32_t *set_connectors_ptr;
1814 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1817 /* For some reason crtc x/y offsets are signed internally. */
1818 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1821 sx_xlock(&dev->mode_config.mutex);
1822 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1823 DRM_MODE_OBJECT_CRTC);
1825 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1829 crtc = obj_to_crtc(obj);
1830 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1832 if (crtc_req->mode_valid) {
1833 /* If we have a mode we need a framebuffer. */
1834 /* If we pass -1, set the mode with the currently bound fb */
1835 if (crtc_req->fb_id == -1) {
1837 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1843 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1844 DRM_MODE_OBJECT_FB);
1846 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1851 fb = obj_to_fb(obj);
1854 mode = drm_mode_create(dev);
1860 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1862 DRM_DEBUG_KMS("Invalid mode\n");
1866 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1868 if (mode->hdisplay > fb->width ||
1869 mode->vdisplay > fb->height ||
1870 crtc_req->x > fb->width - mode->hdisplay ||
1871 crtc_req->y > fb->height - mode->vdisplay) {
1872 DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
1873 mode->hdisplay, mode->vdisplay,
1874 crtc_req->x, crtc_req->y,
1875 fb->width, fb->height);
1881 if (crtc_req->count_connectors == 0 && mode) {
1882 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1887 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1888 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1889 crtc_req->count_connectors);
1894 if (crtc_req->count_connectors > 0) {
1897 /* Avoid unbounded kernel memory allocation */
1898 if (crtc_req->count_connectors > config->num_connector) {
1903 connector_set = malloc(crtc_req->count_connectors *
1904 sizeof(struct drm_connector *), DRM_MEM_KMS, M_WAITOK);
1906 for (i = 0; i < crtc_req->count_connectors; i++) {
1907 set_connectors_ptr = (uint32_t *)(uintptr_t)crtc_req->set_connectors_ptr;
1908 if (copyin(&set_connectors_ptr[i], &out_id, sizeof(uint32_t))) {
1913 obj = drm_mode_object_find(dev, out_id,
1914 DRM_MODE_OBJECT_CONNECTOR);
1916 DRM_DEBUG_KMS("Connector id %d unknown\n",
1921 connector = obj_to_connector(obj);
1922 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1924 drm_get_connector_name(connector));
1926 connector_set[i] = connector;
1931 set.x = crtc_req->x;
1932 set.y = crtc_req->y;
1934 set.connectors = connector_set;
1935 set.num_connectors = crtc_req->count_connectors;
1937 ret = crtc->funcs->set_config(&set);
1940 free(connector_set, DRM_MEM_KMS);
1941 drm_mode_destroy(dev, mode);
1942 sx_xunlock(&dev->mode_config.mutex);
1946 int drm_mode_cursor_ioctl(struct drm_device *dev,
1947 void *data, struct drm_file *file_priv)
1949 struct drm_mode_cursor *req = data;
1950 struct drm_mode_object *obj;
1951 struct drm_crtc *crtc;
1954 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1960 sx_xlock(&dev->mode_config.mutex);
1961 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1963 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1967 crtc = obj_to_crtc(obj);
1969 if (req->flags & DRM_MODE_CURSOR_BO) {
1970 if (!crtc->funcs->cursor_set) {
1974 /* Turns off the cursor if handle is 0 */
1975 ret = -crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1976 req->width, req->height);
1979 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1980 if (crtc->funcs->cursor_move) {
1981 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1988 sx_xunlock(&dev->mode_config.mutex);
1992 /* Original addfb only supported RGB formats, so figure out which one */
1993 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
1999 fmt = DRM_FORMAT_RGB332;
2003 fmt = DRM_FORMAT_XRGB1555;
2005 fmt = DRM_FORMAT_RGB565;
2008 fmt = DRM_FORMAT_RGB888;
2012 fmt = DRM_FORMAT_XRGB8888;
2013 else if (depth == 30)
2014 fmt = DRM_FORMAT_XRGB2101010;
2016 fmt = DRM_FORMAT_ARGB8888;
2019 DRM_ERROR("bad bpp, assuming RGB24 pixel format\n");
2020 fmt = DRM_FORMAT_XRGB8888;
2028 * drm_mode_addfb - add an FB to the graphics configuration
2029 * @inode: inode from the ioctl
2030 * @filp: file * from the ioctl
2031 * @cmd: cmd from ioctl
2032 * @arg: arg from ioctl
2035 * Takes mode config lock.
2037 * Add a new FB to the specified CRTC, given a user request.
2039 * Called by the user via ioctl.
2042 * Zero on success, errno on failure.
2044 int drm_mode_addfb(struct drm_device *dev,
2045 void *data, struct drm_file *file_priv)
2047 struct drm_mode_fb_cmd *or = data;
2048 struct drm_mode_fb_cmd2 r = {};
2049 struct drm_mode_config *config = &dev->mode_config;
2050 struct drm_framebuffer *fb;
2053 /* Use new struct with format internally */
2054 r.fb_id = or->fb_id;
2055 r.width = or->width;
2056 r.height = or->height;
2057 r.pitches[0] = or->pitch;
2058 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2059 r.handles[0] = or->handle;
2061 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2064 if ((config->min_width > r.width) || (r.width > config->max_width))
2066 if ((config->min_height > r.height) || (r.height > config->max_height))
2069 sx_xlock(&dev->mode_config.mutex);
2071 ret = -dev->mode_config.funcs->fb_create(dev, file_priv, &r, &fb);
2073 DRM_ERROR("could not create framebuffer, error %d\n", ret);
2077 or->fb_id = fb->base.id;
2078 list_add(&fb->filp_head, &file_priv->fbs);
2079 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2082 sx_xunlock(&dev->mode_config.mutex);
2086 static int format_check(struct drm_mode_fb_cmd2 *r)
2088 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2092 case DRM_FORMAT_RGB332:
2093 case DRM_FORMAT_BGR233:
2094 case DRM_FORMAT_XRGB4444:
2095 case DRM_FORMAT_XBGR4444:
2096 case DRM_FORMAT_RGBX4444:
2097 case DRM_FORMAT_BGRX4444:
2098 case DRM_FORMAT_ARGB4444:
2099 case DRM_FORMAT_ABGR4444:
2100 case DRM_FORMAT_RGBA4444:
2101 case DRM_FORMAT_BGRA4444:
2102 case DRM_FORMAT_XRGB1555:
2103 case DRM_FORMAT_XBGR1555:
2104 case DRM_FORMAT_RGBX5551:
2105 case DRM_FORMAT_BGRX5551:
2106 case DRM_FORMAT_ARGB1555:
2107 case DRM_FORMAT_ABGR1555:
2108 case DRM_FORMAT_RGBA5551:
2109 case DRM_FORMAT_BGRA5551:
2110 case DRM_FORMAT_RGB565:
2111 case DRM_FORMAT_BGR565:
2112 case DRM_FORMAT_RGB888:
2113 case DRM_FORMAT_BGR888:
2114 case DRM_FORMAT_XRGB8888:
2115 case DRM_FORMAT_XBGR8888:
2116 case DRM_FORMAT_RGBX8888:
2117 case DRM_FORMAT_BGRX8888:
2118 case DRM_FORMAT_ARGB8888:
2119 case DRM_FORMAT_ABGR8888:
2120 case DRM_FORMAT_RGBA8888:
2121 case DRM_FORMAT_BGRA8888:
2122 case DRM_FORMAT_XRGB2101010:
2123 case DRM_FORMAT_XBGR2101010:
2124 case DRM_FORMAT_RGBX1010102:
2125 case DRM_FORMAT_BGRX1010102:
2126 case DRM_FORMAT_ARGB2101010:
2127 case DRM_FORMAT_ABGR2101010:
2128 case DRM_FORMAT_RGBA1010102:
2129 case DRM_FORMAT_BGRA1010102:
2130 case DRM_FORMAT_YUYV:
2131 case DRM_FORMAT_YVYU:
2132 case DRM_FORMAT_UYVY:
2133 case DRM_FORMAT_VYUY:
2134 case DRM_FORMAT_AYUV:
2135 case DRM_FORMAT_NV12:
2136 case DRM_FORMAT_NV21:
2137 case DRM_FORMAT_NV16:
2138 case DRM_FORMAT_NV61:
2139 case DRM_FORMAT_YUV410:
2140 case DRM_FORMAT_YVU410:
2141 case DRM_FORMAT_YUV411:
2142 case DRM_FORMAT_YVU411:
2143 case DRM_FORMAT_YUV420:
2144 case DRM_FORMAT_YVU420:
2145 case DRM_FORMAT_YUV422:
2146 case DRM_FORMAT_YVU422:
2147 case DRM_FORMAT_YUV444:
2148 case DRM_FORMAT_YVU444:
2156 * drm_mode_addfb2 - add an FB to the graphics configuration
2157 * @inode: inode from the ioctl
2158 * @filp: file * from the ioctl
2159 * @cmd: cmd from ioctl
2160 * @arg: arg from ioctl
2163 * Takes mode config lock.
2165 * Add a new FB to the specified CRTC, given a user request with format.
2167 * Called by the user via ioctl.
2170 * Zero on success, errno on failure.
2172 int drm_mode_addfb2(struct drm_device *dev,
2173 void *data, struct drm_file *file_priv)
2175 struct drm_mode_fb_cmd2 *r = data;
2176 struct drm_mode_config *config = &dev->mode_config;
2177 struct drm_framebuffer *fb;
2180 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2183 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2184 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2185 r->width, config->min_width, config->max_width);
2188 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2189 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2190 r->height, config->min_height, config->max_height);
2194 ret = format_check(r);
2196 DRM_ERROR("bad framebuffer format 0x%08x\n", r->pixel_format);
2200 sx_xlock(&dev->mode_config.mutex);
2202 /* TODO check buffer is sufficiently large */
2203 /* TODO setup destructor callback */
2205 ret = -dev->mode_config.funcs->fb_create(dev, file_priv, r, &fb);
2207 DRM_ERROR("could not create framebuffer, error %d\n", ret);
2211 r->fb_id = fb->base.id;
2212 list_add(&fb->filp_head, &file_priv->fbs);
2213 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2216 sx_xunlock(&dev->mode_config.mutex);
2221 * drm_mode_rmfb - remove an FB from the configuration
2222 * @inode: inode from the ioctl
2223 * @filp: file * from the ioctl
2224 * @cmd: cmd from ioctl
2225 * @arg: arg from ioctl
2228 * Takes mode config lock.
2230 * Remove the FB specified by the user.
2232 * Called by the user via ioctl.
2235 * Zero on success, errno on failure.
2237 int drm_mode_rmfb(struct drm_device *dev,
2238 void *data, struct drm_file *file_priv)
2240 struct drm_mode_object *obj;
2241 struct drm_framebuffer *fb = NULL;
2242 struct drm_framebuffer *fbl = NULL;
2243 uint32_t *id = data;
2247 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2250 sx_xlock(&dev->mode_config.mutex);
2251 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
2252 /* TODO check that we really get a framebuffer back. */
2257 fb = obj_to_fb(obj);
2259 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2268 /* TODO release all crtc connected to the framebuffer */
2269 /* TODO unhock the destructor from the buffer object */
2271 list_del(&fb->filp_head);
2272 fb->funcs->destroy(fb);
2275 sx_xunlock(&dev->mode_config.mutex);
2280 * drm_mode_getfb - get FB info
2281 * @inode: inode from the ioctl
2282 * @filp: file * from the ioctl
2283 * @cmd: cmd from ioctl
2284 * @arg: arg from ioctl
2287 * Takes mode config lock.
2289 * Lookup the FB given its ID and return info about it.
2291 * Called by the user via ioctl.
2294 * Zero on success, errno on failure.
2296 int drm_mode_getfb(struct drm_device *dev,
2297 void *data, struct drm_file *file_priv)
2299 struct drm_mode_fb_cmd *r = data;
2300 struct drm_mode_object *obj;
2301 struct drm_framebuffer *fb;
2304 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2307 sx_xlock(&dev->mode_config.mutex);
2308 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2313 fb = obj_to_fb(obj);
2315 r->height = fb->height;
2316 r->width = fb->width;
2317 r->depth = fb->depth;
2318 r->bpp = fb->bits_per_pixel;
2319 r->pitch = fb->pitches[0];
2321 fb->funcs->create_handle(fb, file_priv, &r->handle);
2324 sx_xunlock(&dev->mode_config.mutex);
2328 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2329 void *data, struct drm_file *file_priv)
2331 struct drm_clip_rect __user *clips_ptr;
2332 struct drm_clip_rect *clips = NULL;
2333 struct drm_mode_fb_dirty_cmd *r = data;
2334 struct drm_mode_object *obj;
2335 struct drm_framebuffer *fb;
2340 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2343 sx_xlock(&dev->mode_config.mutex);
2344 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2349 fb = obj_to_fb(obj);
2351 num_clips = r->num_clips;
2352 clips_ptr = (struct drm_clip_rect *)(unsigned long)r->clips_ptr;
2354 if (!num_clips != !clips_ptr) {
2359 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2361 /* If userspace annotates copy, clips must come in pairs */
2362 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2367 if (num_clips && clips_ptr) {
2368 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2372 clips = malloc(num_clips * sizeof(*clips), DRM_MEM_KMS,
2375 ret = copyin(clips_ptr, clips, num_clips * sizeof(*clips));
2380 if (fb->funcs->dirty) {
2381 ret = -fb->funcs->dirty(fb, file_priv, flags, r->color,
2389 free(clips, DRM_MEM_KMS);
2391 sx_xunlock(&dev->mode_config.mutex);
2397 * drm_fb_release - remove and free the FBs on this file
2398 * @filp: file * from the ioctl
2401 * Takes mode config lock.
2403 * Destroy all the FBs associated with @filp.
2405 * Called by the user via ioctl.
2408 * Zero on success, errno on failure.
2410 void drm_fb_release(struct drm_file *priv)
2413 struct drm_device *dev = priv->dev;
2415 struct drm_device *dev = priv->minor->dev;
2417 struct drm_framebuffer *fb, *tfb;
2419 sx_xlock(&dev->mode_config.mutex);
2420 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2421 list_del(&fb->filp_head);
2422 fb->funcs->destroy(fb);
2424 sx_xunlock(&dev->mode_config.mutex);
2428 * drm_mode_attachmode - add a mode to the user mode list
2430 * @connector: connector to add the mode to
2431 * @mode: mode to add
2433 * Add @mode to @connector's user mode list.
2435 static void drm_mode_attachmode(struct drm_device *dev,
2436 struct drm_connector *connector,
2437 struct drm_display_mode *mode)
2439 list_add_tail(&mode->head, &connector->user_modes);
2442 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2443 const struct drm_display_mode *mode)
2445 struct drm_connector *connector;
2447 struct drm_display_mode *dup_mode, *next;
2448 DRM_LIST_HEAD(list);
2450 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2451 if (!connector->encoder)
2453 if (connector->encoder->crtc == crtc) {
2454 dup_mode = drm_mode_duplicate(dev, mode);
2459 list_add_tail(&dup_mode->head, &list);
2463 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2464 if (!connector->encoder)
2466 if (connector->encoder->crtc == crtc)
2467 list_move_tail(list.next, &connector->user_modes);
2470 MPASS(!list_empty(&list));
2473 list_for_each_entry_safe(dup_mode, next, &list, head)
2474 drm_mode_destroy(dev, dup_mode);
2479 static int drm_mode_detachmode(struct drm_device *dev,
2480 struct drm_connector *connector,
2481 struct drm_display_mode *mode)
2485 struct drm_display_mode *match_mode, *t;
2487 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2488 if (drm_mode_equal(match_mode, mode)) {
2489 list_del(&match_mode->head);
2490 drm_mode_destroy(dev, match_mode);
2502 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2504 struct drm_connector *connector;
2506 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2507 drm_mode_detachmode(dev, connector, mode);
2513 * drm_fb_attachmode - Attach a user mode to an connector
2514 * @inode: inode from the ioctl
2515 * @filp: file * from the ioctl
2516 * @cmd: cmd from ioctl
2517 * @arg: arg from ioctl
2519 * This attaches a user specified mode to an connector.
2520 * Called by the user via ioctl.
2523 * Zero on success, errno on failure.
2525 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2526 void *data, struct drm_file *file_priv)
2528 struct drm_mode_mode_cmd *mode_cmd = data;
2529 struct drm_connector *connector;
2530 struct drm_display_mode *mode;
2531 struct drm_mode_object *obj;
2532 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2535 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2538 sx_xlock(&dev->mode_config.mutex);
2540 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2545 connector = obj_to_connector(obj);
2547 mode = drm_mode_create(dev);
2553 ret = drm_crtc_convert_umode(mode, umode);
2555 DRM_DEBUG_KMS("Invalid mode\n");
2556 drm_mode_destroy(dev, mode);
2560 drm_mode_attachmode(dev, connector, mode);
2562 sx_xunlock(&dev->mode_config.mutex);
2568 * drm_fb_detachmode - Detach a user specified mode from an connector
2569 * @inode: inode from the ioctl
2570 * @filp: file * from the ioctl
2571 * @cmd: cmd from ioctl
2572 * @arg: arg from ioctl
2574 * Called by the user via ioctl.
2577 * Zero on success, errno on failure.
2579 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2580 void *data, struct drm_file *file_priv)
2582 struct drm_mode_object *obj;
2583 struct drm_mode_mode_cmd *mode_cmd = data;
2584 struct drm_connector *connector;
2585 struct drm_display_mode mode;
2586 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2589 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2592 sx_xlock(&dev->mode_config.mutex);
2594 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2599 connector = obj_to_connector(obj);
2601 ret = drm_crtc_convert_umode(&mode, umode);
2603 DRM_DEBUG_KMS("Invalid mode\n");
2607 ret = drm_mode_detachmode(dev, connector, &mode);
2609 sx_xunlock(&dev->mode_config.mutex);
2613 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2614 const char *name, int num_values)
2616 struct drm_property *property = NULL;
2619 property = malloc(sizeof(struct drm_property), DRM_MEM_KMS,
2623 property->values = malloc(sizeof(uint64_t)*num_values, DRM_MEM_KMS,
2627 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2630 property->flags = flags;
2631 property->num_values = num_values;
2632 INIT_LIST_HEAD(&property->enum_blob_list);
2635 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2636 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2639 list_add_tail(&property->head, &dev->mode_config.property_list);
2643 free(property->values, DRM_MEM_KMS);
2644 free(property, DRM_MEM_KMS);
2648 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2650 const struct drm_prop_enum_list *props,
2653 struct drm_property *property;
2656 flags |= DRM_MODE_PROP_ENUM;
2658 property = drm_property_create(dev, flags, name, num_values);
2662 for (i = 0; i < num_values; i++) {
2663 ret = drm_property_add_enum(property, i,
2667 drm_property_destroy(dev, property);
2675 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2677 uint64_t min, uint64_t max)
2679 struct drm_property *property;
2681 flags |= DRM_MODE_PROP_RANGE;
2683 property = drm_property_create(dev, flags, name, 2);
2687 property->values[0] = min;
2688 property->values[1] = max;
2693 int drm_property_add_enum(struct drm_property *property, int index,
2694 uint64_t value, const char *name)
2696 struct drm_property_enum *prop_enum;
2698 if (!(property->flags & DRM_MODE_PROP_ENUM))
2701 if (!list_empty(&property->enum_blob_list)) {
2702 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2703 if (prop_enum->value == value) {
2704 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2705 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2711 prop_enum = malloc(sizeof(struct drm_property_enum), DRM_MEM_KMS,
2714 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2715 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2716 prop_enum->value = value;
2718 property->values[index] = value;
2719 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2723 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2725 struct drm_property_enum *prop_enum, *pt;
2727 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2728 list_del(&prop_enum->head);
2729 free(prop_enum, DRM_MEM_KMS);
2732 if (property->num_values)
2733 free(property->values, DRM_MEM_KMS);
2734 drm_mode_object_put(dev, &property->base);
2735 list_del(&property->head);
2736 free(property, DRM_MEM_KMS);
2739 int drm_connector_attach_property(struct drm_connector *connector,
2740 struct drm_property *property, uint64_t init_val)
2744 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2745 if (connector->property_ids[i] == 0) {
2746 connector->property_ids[i] = property->base.id;
2747 connector->property_values[i] = init_val;
2752 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2757 int drm_connector_property_set_value(struct drm_connector *connector,
2758 struct drm_property *property, uint64_t value)
2762 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2763 if (connector->property_ids[i] == property->base.id) {
2764 connector->property_values[i] = value;
2769 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2774 int drm_connector_property_get_value(struct drm_connector *connector,
2775 struct drm_property *property, uint64_t *val)
2779 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2780 if (connector->property_ids[i] == property->base.id) {
2781 *val = connector->property_values[i];
2786 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2791 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2792 void *data, struct drm_file *file_priv)
2794 struct drm_mode_object *obj;
2795 struct drm_mode_get_property *out_resp = data;
2796 struct drm_property *property;
2799 int value_count = 0;
2802 struct drm_property_enum *prop_enum;
2803 struct drm_mode_property_enum __user *enum_ptr;
2804 struct drm_property_blob *prop_blob;
2805 uint32_t *blob_id_ptr;
2806 uint64_t *values_ptr;
2807 uint32_t *blob_length_ptr;
2809 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2812 sx_xlock(&dev->mode_config.mutex);
2813 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2818 property = obj_to_property(obj);
2820 if (property->flags & DRM_MODE_PROP_ENUM) {
2821 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2823 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2824 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2828 value_count = property->num_values;
2830 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2831 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2832 out_resp->flags = property->flags;
2834 if ((out_resp->count_values >= value_count) && value_count) {
2835 values_ptr = (uint64_t *)(uintptr_t)out_resp->values_ptr;
2836 for (i = 0; i < value_count; i++) {
2837 if (copyout(&property->values[i], values_ptr + i, sizeof(uint64_t))) {
2843 out_resp->count_values = value_count;
2845 if (property->flags & DRM_MODE_PROP_ENUM) {
2846 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2848 enum_ptr = (struct drm_mode_property_enum *)(uintptr_t)out_resp->enum_blob_ptr;
2849 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2851 if (copyout(&prop_enum->value, &enum_ptr[copied].value, sizeof(uint64_t))) {
2856 if (copyout(&prop_enum->name,
2857 &enum_ptr[copied].name,DRM_PROP_NAME_LEN)) {
2864 out_resp->count_enum_blobs = enum_count;
2867 if (property->flags & DRM_MODE_PROP_BLOB) {
2868 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2870 blob_id_ptr = (uint32_t *)(uintptr_t)out_resp->enum_blob_ptr;
2871 blob_length_ptr = (uint32_t *)(uintptr_t)out_resp->values_ptr;
2873 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2874 if (copyout(&prop_blob->base.id,
2875 blob_id_ptr + copied, sizeof(uint32_t))) {
2880 if (copyout(&prop_blob->length,
2881 blob_length_ptr + copied, sizeof(uint32_t))) {
2889 out_resp->count_enum_blobs = blob_count;
2892 sx_xunlock(&dev->mode_config.mutex);
2896 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2899 struct drm_property_blob *blob;
2902 if (!length || !data)
2905 blob = malloc(sizeof(struct drm_property_blob) + length, DRM_MEM_KMS,
2908 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2910 free(blob, DRM_MEM_KMS);
2914 blob->length = length;
2916 memcpy(blob->data, data, length);
2918 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2922 static void drm_property_destroy_blob(struct drm_device *dev,
2923 struct drm_property_blob *blob)
2925 drm_mode_object_put(dev, &blob->base);
2926 list_del(&blob->head);
2927 free(blob, DRM_MEM_KMS);
2930 int drm_mode_getblob_ioctl(struct drm_device *dev,
2931 void *data, struct drm_file *file_priv)
2933 struct drm_mode_object *obj;
2934 struct drm_mode_get_blob *out_resp = data;
2935 struct drm_property_blob *blob;
2939 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2942 sx_xlock(&dev->mode_config.mutex);
2943 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2948 blob = obj_to_blob(obj);
2950 if (out_resp->length == blob->length) {
2951 blob_ptr = (void *)(unsigned long)out_resp->data;
2952 if (copyout(blob->data, blob_ptr, blob->length)){
2957 out_resp->length = blob->length;
2960 sx_xunlock(&dev->mode_config.mutex);
2964 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2967 struct drm_device *dev = connector->dev;
2970 if (connector->edid_blob_ptr)
2971 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2973 /* Delete edid, when there is none. */
2975 connector->edid_blob_ptr = NULL;
2976 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2980 size = EDID_LENGTH * (1 + edid->extensions);
2981 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2984 ret = drm_connector_property_set_value(connector,
2985 dev->mode_config.edid_property,
2986 connector->edid_blob_ptr->base.id);
2991 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2992 void *data, struct drm_file *file_priv)
2994 struct drm_mode_connector_set_property *out_resp = data;
2995 struct drm_mode_object *obj;
2996 struct drm_property *property;
2997 struct drm_connector *connector;
3001 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3004 sx_xlock(&dev->mode_config.mutex);
3006 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
3010 connector = obj_to_connector(obj);
3012 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
3013 if (connector->property_ids[i] == out_resp->prop_id)
3017 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
3021 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3025 property = obj_to_property(obj);
3027 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3030 if (property->flags & DRM_MODE_PROP_RANGE) {
3031 if (out_resp->value < property->values[0])
3034 if (out_resp->value > property->values[1])
3038 for (i = 0; i < property->num_values; i++) {
3039 if (property->values[i] == out_resp->value) {
3049 /* Do DPMS ourselves */
3050 if (property == connector->dev->mode_config.dpms_property) {
3051 if (connector->funcs->dpms)
3052 (*connector->funcs->dpms)(connector, (int) out_resp->value);
3054 } else if (connector->funcs->set_property)
3055 ret = connector->funcs->set_property(connector, property, out_resp->value);
3057 /* store the property value if successful */
3059 drm_connector_property_set_value(connector, property, out_resp->value);
3061 sx_xunlock(&dev->mode_config.mutex);
3065 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3066 struct drm_encoder *encoder)
3070 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3071 if (connector->encoder_ids[i] == 0) {
3072 connector->encoder_ids[i] = encoder->base.id;
3079 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3080 struct drm_encoder *encoder)
3083 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3084 if (connector->encoder_ids[i] == encoder->base.id) {
3085 connector->encoder_ids[i] = 0;
3086 if (connector->encoder == encoder)
3087 connector->encoder = NULL;
3093 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3096 crtc->gamma_size = gamma_size;
3098 crtc->gamma_store = malloc(gamma_size * sizeof(uint16_t) * 3,
3099 DRM_MEM_KMS, M_WAITOK | M_ZERO);
3104 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3105 void *data, struct drm_file *file_priv)
3107 struct drm_mode_crtc_lut *crtc_lut = data;
3108 struct drm_mode_object *obj;
3109 struct drm_crtc *crtc;
3110 void *r_base, *g_base, *b_base;
3114 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3117 sx_xlock(&dev->mode_config.mutex);
3118 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3123 crtc = obj_to_crtc(obj);
3125 /* memcpy into gamma store */
3126 if (crtc_lut->gamma_size != crtc->gamma_size) {
3131 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3132 r_base = crtc->gamma_store;
3133 if (copyin((void *)(uintptr_t)crtc_lut->red, r_base, size)) {
3138 g_base = (char *)r_base + size;
3139 if (copyin((void *)(uintptr_t)crtc_lut->green, g_base, size)) {
3144 b_base = (char *)g_base + size;
3145 if (copyin((void *)(uintptr_t)crtc_lut->blue, b_base, size)) {
3150 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3153 sx_xunlock(&dev->mode_config.mutex);
3158 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3159 void *data, struct drm_file *file_priv)
3161 struct drm_mode_crtc_lut *crtc_lut = data;
3162 struct drm_mode_object *obj;
3163 struct drm_crtc *crtc;
3164 void *r_base, *g_base, *b_base;
3168 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3171 sx_xlock(&dev->mode_config.mutex);
3172 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3177 crtc = obj_to_crtc(obj);
3179 /* memcpy into gamma store */
3180 if (crtc_lut->gamma_size != crtc->gamma_size) {
3185 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3186 r_base = crtc->gamma_store;
3187 if (copyout(r_base, (void *)(uintptr_t)crtc_lut->red, size)) {
3192 g_base = (char *)r_base + size;
3193 if (copyout(g_base, (void *)(uintptr_t)crtc_lut->green, size)) {
3198 b_base = (char *)g_base + size;
3199 if (copyout(b_base, (void *)(uintptr_t)crtc_lut->blue, size)) {
3204 sx_xunlock(&dev->mode_config.mutex);
3209 drm_kms_free(void *arg)
3212 free(arg, DRM_MEM_KMS);
3215 int drm_mode_page_flip_ioctl(struct drm_device *dev, void *data,
3216 struct drm_file *file_priv)
3218 struct drm_mode_crtc_page_flip *page_flip = data;
3219 struct drm_mode_object *obj;
3220 struct drm_crtc *crtc;
3221 struct drm_framebuffer *fb;
3222 struct drm_pending_vblank_event *e = NULL;
3225 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3226 page_flip->reserved != 0)
3229 sx_xlock(&dev->mode_config.mutex);
3230 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3233 crtc = obj_to_crtc(obj);
3235 if (crtc->fb == NULL) {
3236 /* The framebuffer is currently unbound, presumably
3237 * due to a hotplug event, that userspace has not
3244 if (crtc->funcs->page_flip == NULL)
3247 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3250 fb = obj_to_fb(obj);
3252 if (crtc->mode.hdisplay > fb->width ||
3253 crtc->mode.vdisplay > fb->height ||
3254 crtc->x > fb->width - crtc->mode.hdisplay ||
3255 crtc->y > fb->height - crtc->mode.vdisplay) {
3256 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
3257 fb->width, fb->height,
3258 crtc->mode.hdisplay, crtc->mode.vdisplay,
3264 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3266 mtx_lock(&dev->event_lock);
3267 if (file_priv->event_space < sizeof e->event) {
3268 mtx_unlock(&dev->event_lock);
3271 file_priv->event_space -= sizeof e->event;
3272 mtx_unlock(&dev->event_lock);
3274 e = malloc(sizeof *e, DRM_MEM_KMS, M_WAITOK | M_ZERO);
3276 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3277 e->event.base.length = sizeof e->event;
3278 e->event.user_data = page_flip->user_data;
3279 e->base.event = &e->event.base;
3280 e->base.file_priv = file_priv;
3282 (void (*) (struct drm_pending_event *))drm_kms_free;
3285 ret = -crtc->funcs->page_flip(crtc, fb, e);
3287 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3288 mtx_lock(&dev->event_lock);
3289 file_priv->event_space += sizeof e->event;
3290 mtx_unlock(&dev->event_lock);
3291 free(e, DRM_MEM_KMS);
3296 sx_xunlock(&dev->mode_config.mutex);
3297 CTR3(KTR_DRM, "page_flip_ioctl %d %d %d", curproc->p_pid,
3298 page_flip->crtc_id, ret);
3302 void drm_mode_config_reset(struct drm_device *dev)
3304 struct drm_crtc *crtc;
3305 struct drm_encoder *encoder;
3306 struct drm_connector *connector;
3308 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3309 if (crtc->funcs->reset)
3310 crtc->funcs->reset(crtc);
3312 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3313 if (encoder->funcs->reset)
3314 encoder->funcs->reset(encoder);
3316 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3317 if (connector->funcs->reset)
3318 connector->funcs->reset(connector);
3321 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3322 void *data, struct drm_file *file_priv)
3324 struct drm_mode_create_dumb *args = data;
3326 if (!dev->driver->dumb_create)
3328 return dev->driver->dumb_create(file_priv, dev, args);
3331 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3332 void *data, struct drm_file *file_priv)
3334 struct drm_mode_map_dumb *args = data;
3336 /* call driver ioctl to get mmap offset */
3337 if (!dev->driver->dumb_map_offset)
3340 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3343 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3344 void *data, struct drm_file *file_priv)
3346 struct drm_mode_destroy_dumb *args = data;
3348 if (!dev->driver->dumb_destroy)
3351 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3355 * Just need to support RGB formats here for compat with code that doesn't
3356 * use pixel formats directly yet.
3358 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3362 case DRM_FORMAT_RGB332:
3363 case DRM_FORMAT_BGR233:
3367 case DRM_FORMAT_XRGB1555:
3368 case DRM_FORMAT_XBGR1555:
3369 case DRM_FORMAT_RGBX5551:
3370 case DRM_FORMAT_BGRX5551:
3371 case DRM_FORMAT_ARGB1555:
3372 case DRM_FORMAT_ABGR1555:
3373 case DRM_FORMAT_RGBA5551:
3374 case DRM_FORMAT_BGRA5551:
3378 case DRM_FORMAT_RGB565:
3379 case DRM_FORMAT_BGR565:
3383 case DRM_FORMAT_RGB888:
3384 case DRM_FORMAT_BGR888:
3388 case DRM_FORMAT_XRGB8888:
3389 case DRM_FORMAT_XBGR8888:
3390 case DRM_FORMAT_RGBX8888:
3391 case DRM_FORMAT_BGRX8888:
3395 case DRM_FORMAT_XRGB2101010:
3396 case DRM_FORMAT_XBGR2101010:
3397 case DRM_FORMAT_RGBX1010102:
3398 case DRM_FORMAT_BGRX1010102:
3399 case DRM_FORMAT_ARGB2101010:
3400 case DRM_FORMAT_ABGR2101010:
3401 case DRM_FORMAT_RGBA1010102:
3402 case DRM_FORMAT_BGRA1010102:
3406 case DRM_FORMAT_ARGB8888:
3407 case DRM_FORMAT_ABGR8888:
3408 case DRM_FORMAT_RGBA8888:
3409 case DRM_FORMAT_BGRA8888:
3414 DRM_DEBUG_KMS("unsupported pixel format\n");