2 * Copyright (c) 2016 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 #include <linux/uaccess.h>
25 #include <drm/drm_drv.h>
26 #include <drm/drm_encoder.h>
27 #include <drm/drm_file.h>
28 #include <drm/drm_mode_config.h>
29 #include <drm/drm_print.h>
31 #include "drm_crtc_internal.h"
32 #include "drm_internal.h"
34 int drm_modeset_register_all(struct drm_device *dev)
38 ret = drm_plane_register_all(dev);
42 ret = drm_crtc_register_all(dev);
46 ret = drm_encoder_register_all(dev);
50 ret = drm_connector_register_all(dev);
57 drm_encoder_unregister_all(dev);
59 drm_crtc_unregister_all(dev);
61 drm_plane_unregister_all(dev);
66 void drm_modeset_unregister_all(struct drm_device *dev)
68 drm_connector_unregister_all(dev);
69 drm_encoder_unregister_all(dev);
70 drm_crtc_unregister_all(dev);
71 drm_plane_unregister_all(dev);
75 * drm_mode_getresources - get graphics configuration
76 * @dev: drm device for the ioctl
77 * @data: data pointer for the ioctl
78 * @file_priv: drm file for the ioctl call
80 * Construct a set of configuration description structures and return
81 * them to the user, including CRTC, connector and framebuffer configuration.
83 * Called by the user via ioctl.
86 * Zero on success, negative errno on failure.
88 int drm_mode_getresources(struct drm_device *dev, void *data,
89 struct drm_file *file_priv)
91 struct drm_mode_card_res *card_res = data;
92 struct drm_framebuffer *fb;
93 struct drm_connector *connector;
94 struct drm_crtc *crtc;
95 struct drm_encoder *encoder;
97 uint32_t __user *fb_id;
98 uint32_t __user *crtc_id;
99 uint32_t __user *connector_id;
100 uint32_t __user *encoder_id;
101 struct drm_connector_list_iter conn_iter;
103 if (!drm_core_check_feature(dev, DRIVER_MODESET))
106 mutex_lock(&file_priv->fbs_lock);
108 fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
109 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
110 if (count < card_res->count_fbs &&
111 put_user(fb->base.id, fb_id + count)) {
112 mutex_unlock(&file_priv->fbs_lock);
117 card_res->count_fbs = count;
118 mutex_unlock(&file_priv->fbs_lock);
120 card_res->max_height = dev->mode_config.max_height;
121 card_res->min_height = dev->mode_config.min_height;
122 card_res->max_width = dev->mode_config.max_width;
123 card_res->min_width = dev->mode_config.min_width;
126 crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
127 drm_for_each_crtc(crtc, dev) {
128 if (drm_lease_held(file_priv, crtc->base.id)) {
129 if (count < card_res->count_crtcs &&
130 put_user(crtc->base.id, crtc_id + count))
135 card_res->count_crtcs = count;
138 encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
139 drm_for_each_encoder(encoder, dev) {
140 if (count < card_res->count_encoders &&
141 put_user(encoder->base.id, encoder_id + count))
145 card_res->count_encoders = count;
147 drm_connector_list_iter_begin(dev, &conn_iter);
149 connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
150 drm_for_each_connector_iter(connector, &conn_iter) {
151 /* only expose writeback connectors if userspace understands them */
152 if (!file_priv->writeback_connectors &&
153 (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK))
156 if (drm_lease_held(file_priv, connector->base.id)) {
157 if (count < card_res->count_connectors &&
158 put_user(connector->base.id, connector_id + count)) {
159 drm_connector_list_iter_end(&conn_iter);
165 card_res->count_connectors = count;
166 drm_connector_list_iter_end(&conn_iter);
172 * drm_mode_config_reset - call ->reset callbacks
175 * This functions calls all the crtc's, encoder's and connector's ->reset
176 * callback. Drivers can use this in e.g. their driver load or resume code to
177 * reset hardware and software state.
179 void drm_mode_config_reset(struct drm_device *dev)
181 struct drm_crtc *crtc;
182 struct drm_plane *plane;
183 struct drm_encoder *encoder;
184 struct drm_connector *connector;
185 struct drm_connector_list_iter conn_iter;
187 drm_for_each_plane(plane, dev)
188 if (plane->funcs->reset)
189 plane->funcs->reset(plane);
191 drm_for_each_crtc(crtc, dev)
192 if (crtc->funcs->reset)
193 crtc->funcs->reset(crtc);
195 drm_for_each_encoder(encoder, dev)
196 if (encoder->funcs->reset)
197 encoder->funcs->reset(encoder);
199 drm_connector_list_iter_begin(dev, &conn_iter);
200 drm_for_each_connector_iter(connector, &conn_iter)
201 if (connector->funcs->reset)
202 connector->funcs->reset(connector);
203 drm_connector_list_iter_end(&conn_iter);
205 EXPORT_SYMBOL(drm_mode_config_reset);
210 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
211 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
212 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
213 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
216 static int drm_mode_create_standard_properties(struct drm_device *dev)
218 struct drm_property *prop;
221 ret = drm_connector_create_standard_properties(dev);
225 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
226 "type", drm_plane_type_enum_list,
227 ARRAY_SIZE(drm_plane_type_enum_list));
230 dev->mode_config.plane_type_property = prop;
232 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
233 "SRC_X", 0, UINT_MAX);
236 dev->mode_config.prop_src_x = prop;
238 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
239 "SRC_Y", 0, UINT_MAX);
242 dev->mode_config.prop_src_y = prop;
244 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
245 "SRC_W", 0, UINT_MAX);
248 dev->mode_config.prop_src_w = prop;
250 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
251 "SRC_H", 0, UINT_MAX);
254 dev->mode_config.prop_src_h = prop;
256 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
257 "CRTC_X", INT_MIN, INT_MAX);
260 dev->mode_config.prop_crtc_x = prop;
262 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
263 "CRTC_Y", INT_MIN, INT_MAX);
266 dev->mode_config.prop_crtc_y = prop;
268 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
269 "CRTC_W", 0, INT_MAX);
272 dev->mode_config.prop_crtc_w = prop;
274 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
275 "CRTC_H", 0, INT_MAX);
278 dev->mode_config.prop_crtc_h = prop;
280 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
281 "FB_ID", DRM_MODE_OBJECT_FB);
284 dev->mode_config.prop_fb_id = prop;
286 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
287 "IN_FENCE_FD", -1, INT_MAX);
290 dev->mode_config.prop_in_fence_fd = prop;
292 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
293 "OUT_FENCE_PTR", 0, U64_MAX);
296 dev->mode_config.prop_out_fence_ptr = prop;
298 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
299 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
302 dev->mode_config.prop_crtc_id = prop;
304 prop = drm_property_create(dev,
305 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
306 "FB_DAMAGE_CLIPS", 0);
309 dev->mode_config.prop_fb_damage_clips = prop;
311 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
315 dev->mode_config.prop_active = prop;
317 prop = drm_property_create(dev,
318 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
322 dev->mode_config.prop_mode_id = prop;
324 prop = drm_property_create_bool(dev, 0,
328 dev->mode_config.prop_vrr_enabled = prop;
330 prop = drm_property_create(dev,
335 dev->mode_config.degamma_lut_property = prop;
337 prop = drm_property_create_range(dev,
338 DRM_MODE_PROP_IMMUTABLE,
339 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
342 dev->mode_config.degamma_lut_size_property = prop;
344 prop = drm_property_create(dev,
349 dev->mode_config.ctm_property = prop;
351 prop = drm_property_create(dev,
356 dev->mode_config.gamma_lut_property = prop;
358 prop = drm_property_create_range(dev,
359 DRM_MODE_PROP_IMMUTABLE,
360 "GAMMA_LUT_SIZE", 0, UINT_MAX);
363 dev->mode_config.gamma_lut_size_property = prop;
365 prop = drm_property_create(dev,
366 DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
370 dev->mode_config.modifiers_property = prop;
376 * drm_mode_config_init - initialize DRM mode_configuration structure
379 * Initialize @dev's mode_config structure, used for tracking the graphics
380 * configuration of @dev.
382 * Since this initializes the modeset locks, no locking is possible. Which is no
383 * problem, since this should happen single threaded at init time. It is the
384 * driver's problem to ensure this guarantee.
387 void drm_mode_config_init(struct drm_device *dev)
389 mutex_init(&dev->mode_config.mutex);
390 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
391 mutex_init(&dev->mode_config.idr_mutex);
392 mutex_init(&dev->mode_config.fb_lock);
393 mutex_init(&dev->mode_config.blob_lock);
394 INIT_LIST_HEAD(&dev->mode_config.fb_list);
395 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
396 INIT_LIST_HEAD(&dev->mode_config.connector_list);
397 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
398 INIT_LIST_HEAD(&dev->mode_config.property_list);
399 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
400 INIT_LIST_HEAD(&dev->mode_config.plane_list);
401 INIT_LIST_HEAD(&dev->mode_config.privobj_list);
402 idr_init(&dev->mode_config.object_idr);
403 idr_init(&dev->mode_config.tile_idr);
404 ida_init(&dev->mode_config.connector_ida);
405 spin_lock_init(&dev->mode_config.connector_list_lock);
407 init_llist_head(&dev->mode_config.connector_free_list);
408 INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
410 drm_mode_create_standard_properties(dev);
412 /* Just to be sure */
413 dev->mode_config.num_fb = 0;
414 dev->mode_config.num_connector = 0;
415 dev->mode_config.num_crtc = 0;
416 dev->mode_config.num_encoder = 0;
417 dev->mode_config.num_total_plane = 0;
419 EXPORT_SYMBOL(drm_mode_config_init);
422 * drm_mode_config_cleanup - free up DRM mode_config info
425 * Free up all the connectors and CRTCs associated with this DRM device, then
426 * free up the framebuffers and associated buffer objects.
428 * Note that since this /should/ happen single-threaded at driver/device
429 * teardown time, no locking is required. It's the driver's job to ensure that
430 * this guarantee actually holds true.
432 * FIXME: cleanup any dangling user buffer objects too
434 void drm_mode_config_cleanup(struct drm_device *dev)
436 struct drm_connector *connector;
437 struct drm_connector_list_iter conn_iter;
438 struct drm_crtc *crtc, *ct;
439 struct drm_encoder *encoder, *enct;
440 struct drm_framebuffer *fb, *fbt;
441 struct drm_property *property, *pt;
442 struct drm_property_blob *blob, *bt;
443 struct drm_plane *plane, *plt;
445 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
447 encoder->funcs->destroy(encoder);
450 drm_connector_list_iter_begin(dev, &conn_iter);
451 drm_for_each_connector_iter(connector, &conn_iter) {
452 /* drm_connector_list_iter holds an full reference to the
453 * current connector itself, which means it is inherently safe
454 * against unreferencing the current connector - but not against
455 * deleting it right away. */
456 drm_connector_put(connector);
458 drm_connector_list_iter_end(&conn_iter);
459 /* connector_iter drops references in a work item. */
460 flush_work(&dev->mode_config.connector_free_work);
461 if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
462 drm_connector_list_iter_begin(dev, &conn_iter);
463 drm_for_each_connector_iter(connector, &conn_iter)
464 DRM_ERROR("connector %s leaked!\n", connector->name);
465 drm_connector_list_iter_end(&conn_iter);
468 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
470 drm_property_destroy(dev, property);
473 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
475 plane->funcs->destroy(plane);
478 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
479 crtc->funcs->destroy(crtc);
482 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
484 drm_property_blob_put(blob);
488 * Single-threaded teardown context, so it's not required to grab the
489 * fb_lock to protect against concurrent fb_list access. Contrary, it
490 * would actually deadlock with the drm_framebuffer_cleanup function.
492 * Also, if there are any framebuffers left, that's a driver leak now,
493 * so politely WARN about this.
495 WARN_ON(!list_empty(&dev->mode_config.fb_list));
496 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
497 struct drm_printer p = drm_debug_printer("[leaked fb]");
498 drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
499 drm_framebuffer_print_info(&p, 1, fb);
500 drm_framebuffer_free(&fb->base.refcount);
503 ida_destroy(&dev->mode_config.connector_ida);
504 idr_destroy(&dev->mode_config.tile_idr);
505 idr_destroy(&dev->mode_config.object_idr);
506 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
508 EXPORT_SYMBOL(drm_mode_config_cleanup);