1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
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.
13  *
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
20  * OF THIS SOFTWARE.
21  */
22 
23 #include <drm/drm_encoder.h>
24 #include <drm/drm_mode_config.h>
25 #include <drm/drmP.h>
26 
27 #include "drm_crtc_internal.h"
28 #include "drm_internal.h"
29 
30 int drm_modeset_register_all(struct drm_device *dev)
31 {
32 	int ret;
33 
34 	ret = drm_plane_register_all(dev);
35 	if (ret)
36 		goto err_plane;
37 
38 	ret = drm_crtc_register_all(dev);
39 	if  (ret)
40 		goto err_crtc;
41 
42 	ret = drm_encoder_register_all(dev);
43 	if (ret)
44 		goto err_encoder;
45 
46 	ret = drm_connector_register_all(dev);
47 	if (ret)
48 		goto err_connector;
49 
50 	return 0;
51 
52 err_connector:
53 	drm_encoder_unregister_all(dev);
54 err_encoder:
55 	drm_crtc_unregister_all(dev);
56 err_crtc:
57 	drm_plane_unregister_all(dev);
58 err_plane:
59 	return ret;
60 }
61 
62 void drm_modeset_unregister_all(struct drm_device *dev)
63 {
64 	drm_connector_unregister_all(dev);
65 	drm_encoder_unregister_all(dev);
66 	drm_crtc_unregister_all(dev);
67 	drm_plane_unregister_all(dev);
68 }
69 
70 /**
71  * drm_mode_getresources - get graphics configuration
72  * @dev: drm device for the ioctl
73  * @data: data pointer for the ioctl
74  * @file_priv: drm file for the ioctl call
75  *
76  * Construct a set of configuration description structures and return
77  * them to the user, including CRTC, connector and framebuffer configuration.
78  *
79  * Called by the user via ioctl.
80  *
81  * Returns:
82  * Zero on success, negative errno on failure.
83  */
84 int drm_mode_getresources(struct drm_device *dev, void *data,
85 			  struct drm_file *file_priv)
86 {
87 	struct drm_mode_card_res *card_res = data;
88 	struct drm_framebuffer *fb;
89 	struct drm_connector *connector;
90 	struct drm_crtc *crtc;
91 	struct drm_encoder *encoder;
92 	int count, ret = 0;
93 	uint32_t __user *fb_id;
94 	uint32_t __user *crtc_id;
95 	uint32_t __user *connector_id;
96 	uint32_t __user *encoder_id;
97 	struct drm_connector_list_iter conn_iter;
98 
99 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
100 		return -EOPNOTSUPP;
101 
102 	mutex_lock(&file_priv->fbs_lock);
103 	count = 0;
104 	fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
105 	list_for_each_entry(fb, &file_priv->fbs, filp_head) {
106 		if (count < card_res->count_fbs &&
107 		    put_user(fb->base.id, fb_id + count)) {
108 			mutex_unlock(&file_priv->fbs_lock);
109 			return -EFAULT;
110 		}
111 		count++;
112 	}
113 	card_res->count_fbs = count;
114 	mutex_unlock(&file_priv->fbs_lock);
115 
116 	card_res->max_height = dev->mode_config.max_height;
117 	card_res->min_height = dev->mode_config.min_height;
118 	card_res->max_width = dev->mode_config.max_width;
119 	card_res->min_width = dev->mode_config.min_width;
120 
121 	count = 0;
122 	crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
123 	drm_for_each_crtc(crtc, dev) {
124 		if (drm_lease_held(file_priv, crtc->base.id)) {
125 			if (count < card_res->count_crtcs &&
126 			    put_user(crtc->base.id, crtc_id + count))
127 				return -EFAULT;
128 			count++;
129 		}
130 	}
131 	card_res->count_crtcs = count;
132 
133 	count = 0;
134 	encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
135 	drm_for_each_encoder(encoder, dev) {
136 		if (count < card_res->count_encoders &&
137 		    put_user(encoder->base.id, encoder_id + count))
138 			return -EFAULT;
139 		count++;
140 	}
141 	card_res->count_encoders = count;
142 
143 	drm_connector_list_iter_begin(dev, &conn_iter);
144 	count = 0;
145 	connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
146 	drm_for_each_connector_iter(connector, &conn_iter) {
147 		/* only expose writeback connectors if userspace understands them */
148 		if (!file_priv->writeback_connectors &&
149 		    (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK))
150 			continue;
151 
152 		if (drm_lease_held(file_priv, connector->base.id)) {
153 			if (count < card_res->count_connectors &&
154 			    put_user(connector->base.id, connector_id + count)) {
155 				drm_connector_list_iter_end(&conn_iter);
156 				return -EFAULT;
157 			}
158 			count++;
159 		}
160 	}
161 	card_res->count_connectors = count;
162 	drm_connector_list_iter_end(&conn_iter);
163 
164 	return ret;
165 }
166 
167 /**
168  * drm_mode_config_reset - call ->reset callbacks
169  * @dev: drm device
170  *
171  * This functions calls all the crtc's, encoder's and connector's ->reset
172  * callback. Drivers can use this in e.g. their driver load or resume code to
173  * reset hardware and software state.
174  */
175 void drm_mode_config_reset(struct drm_device *dev)
176 {
177 	struct drm_crtc *crtc;
178 	struct drm_plane *plane;
179 	struct drm_encoder *encoder;
180 	struct drm_connector *connector;
181 	struct drm_connector_list_iter conn_iter;
182 
183 	drm_for_each_plane(plane, dev)
184 		if (plane->funcs->reset)
185 			plane->funcs->reset(plane);
186 
187 	drm_for_each_crtc(crtc, dev)
188 		if (crtc->funcs->reset)
189 			crtc->funcs->reset(crtc);
190 
191 	drm_for_each_encoder(encoder, dev)
192 		if (encoder->funcs->reset)
193 			encoder->funcs->reset(encoder);
194 
195 	drm_connector_list_iter_begin(dev, &conn_iter);
196 	drm_for_each_connector_iter(connector, &conn_iter)
197 		if (connector->funcs->reset)
198 			connector->funcs->reset(connector);
199 	drm_connector_list_iter_end(&conn_iter);
200 }
201 EXPORT_SYMBOL(drm_mode_config_reset);
202 
203 /*
204  * Global properties
205  */
206 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
207 	{ DRM_PLANE_TYPE_OVERLAY, "Overlay" },
208 	{ DRM_PLANE_TYPE_PRIMARY, "Primary" },
209 	{ DRM_PLANE_TYPE_CURSOR, "Cursor" },
210 };
211 
212 static int drm_mode_create_standard_properties(struct drm_device *dev)
213 {
214 	struct drm_property *prop;
215 	int ret;
216 
217 	ret = drm_connector_create_standard_properties(dev);
218 	if (ret)
219 		return ret;
220 
221 	prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
222 					"type", drm_plane_type_enum_list,
223 					ARRAY_SIZE(drm_plane_type_enum_list));
224 	if (!prop)
225 		return -ENOMEM;
226 	dev->mode_config.plane_type_property = prop;
227 
228 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
229 			"SRC_X", 0, UINT_MAX);
230 	if (!prop)
231 		return -ENOMEM;
232 	dev->mode_config.prop_src_x = prop;
233 
234 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
235 			"SRC_Y", 0, UINT_MAX);
236 	if (!prop)
237 		return -ENOMEM;
238 	dev->mode_config.prop_src_y = prop;
239 
240 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
241 			"SRC_W", 0, UINT_MAX);
242 	if (!prop)
243 		return -ENOMEM;
244 	dev->mode_config.prop_src_w = prop;
245 
246 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
247 			"SRC_H", 0, UINT_MAX);
248 	if (!prop)
249 		return -ENOMEM;
250 	dev->mode_config.prop_src_h = prop;
251 
252 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
253 			"CRTC_X", INT_MIN, INT_MAX);
254 	if (!prop)
255 		return -ENOMEM;
256 	dev->mode_config.prop_crtc_x = prop;
257 
258 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
259 			"CRTC_Y", INT_MIN, INT_MAX);
260 	if (!prop)
261 		return -ENOMEM;
262 	dev->mode_config.prop_crtc_y = prop;
263 
264 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
265 			"CRTC_W", 0, INT_MAX);
266 	if (!prop)
267 		return -ENOMEM;
268 	dev->mode_config.prop_crtc_w = prop;
269 
270 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
271 			"CRTC_H", 0, INT_MAX);
272 	if (!prop)
273 		return -ENOMEM;
274 	dev->mode_config.prop_crtc_h = prop;
275 
276 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
277 			"FB_ID", DRM_MODE_OBJECT_FB);
278 	if (!prop)
279 		return -ENOMEM;
280 	dev->mode_config.prop_fb_id = prop;
281 
282 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
283 			"IN_FENCE_FD", -1, INT_MAX);
284 	if (!prop)
285 		return -ENOMEM;
286 	dev->mode_config.prop_in_fence_fd = prop;
287 
288 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
289 			"OUT_FENCE_PTR", 0, U64_MAX);
290 	if (!prop)
291 		return -ENOMEM;
292 	dev->mode_config.prop_out_fence_ptr = prop;
293 
294 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
295 			"CRTC_ID", DRM_MODE_OBJECT_CRTC);
296 	if (!prop)
297 		return -ENOMEM;
298 	dev->mode_config.prop_crtc_id = prop;
299 
300 	prop = drm_property_create(dev,
301 			DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
302 			"FB_DAMAGE_CLIPS", 0);
303 	if (!prop)
304 		return -ENOMEM;
305 	dev->mode_config.prop_fb_damage_clips = prop;
306 
307 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
308 			"ACTIVE");
309 	if (!prop)
310 		return -ENOMEM;
311 	dev->mode_config.prop_active = prop;
312 
313 	prop = drm_property_create(dev,
314 			DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
315 			"MODE_ID", 0);
316 	if (!prop)
317 		return -ENOMEM;
318 	dev->mode_config.prop_mode_id = prop;
319 
320 	prop = drm_property_create_bool(dev, 0,
321 			"VRR_ENABLED");
322 	if (!prop)
323 		return -ENOMEM;
324 	dev->mode_config.prop_vrr_enabled = prop;
325 
326 	prop = drm_property_create(dev,
327 			DRM_MODE_PROP_BLOB,
328 			"DEGAMMA_LUT", 0);
329 	if (!prop)
330 		return -ENOMEM;
331 	dev->mode_config.degamma_lut_property = prop;
332 
333 	prop = drm_property_create_range(dev,
334 			DRM_MODE_PROP_IMMUTABLE,
335 			"DEGAMMA_LUT_SIZE", 0, UINT_MAX);
336 	if (!prop)
337 		return -ENOMEM;
338 	dev->mode_config.degamma_lut_size_property = prop;
339 
340 	prop = drm_property_create(dev,
341 			DRM_MODE_PROP_BLOB,
342 			"CTM", 0);
343 	if (!prop)
344 		return -ENOMEM;
345 	dev->mode_config.ctm_property = prop;
346 
347 	prop = drm_property_create(dev,
348 			DRM_MODE_PROP_BLOB,
349 			"GAMMA_LUT", 0);
350 	if (!prop)
351 		return -ENOMEM;
352 	dev->mode_config.gamma_lut_property = prop;
353 
354 	prop = drm_property_create_range(dev,
355 			DRM_MODE_PROP_IMMUTABLE,
356 			"GAMMA_LUT_SIZE", 0, UINT_MAX);
357 	if (!prop)
358 		return -ENOMEM;
359 	dev->mode_config.gamma_lut_size_property = prop;
360 
361 	prop = drm_property_create(dev,
362 				   DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
363 				   "IN_FORMATS", 0);
364 	if (!prop)
365 		return -ENOMEM;
366 	dev->mode_config.modifiers_property = prop;
367 
368 	return 0;
369 }
370 
371 /**
372  * drm_mode_config_init - initialize DRM mode_configuration structure
373  * @dev: DRM device
374  *
375  * Initialize @dev's mode_config structure, used for tracking the graphics
376  * configuration of @dev.
377  *
378  * Since this initializes the modeset locks, no locking is possible. Which is no
379  * problem, since this should happen single threaded at init time. It is the
380  * driver's problem to ensure this guarantee.
381  *
382  */
383 void drm_mode_config_init(struct drm_device *dev)
384 {
385 	mutex_init(&dev->mode_config.mutex);
386 	drm_modeset_lock_init(&dev->mode_config.connection_mutex);
387 	mutex_init(&dev->mode_config.idr_mutex);
388 	mutex_init(&dev->mode_config.fb_lock);
389 	mutex_init(&dev->mode_config.blob_lock);
390 	INIT_LIST_HEAD(&dev->mode_config.fb_list);
391 	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
392 	INIT_LIST_HEAD(&dev->mode_config.connector_list);
393 	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
394 	INIT_LIST_HEAD(&dev->mode_config.property_list);
395 	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
396 	INIT_LIST_HEAD(&dev->mode_config.plane_list);
397 	INIT_LIST_HEAD(&dev->mode_config.privobj_list);
398 	idr_init(&dev->mode_config.object_idr);
399 	idr_init(&dev->mode_config.tile_idr);
400 	ida_init(&dev->mode_config.connector_ida);
401 	spin_lock_init(&dev->mode_config.connector_list_lock);
402 
403 	init_llist_head(&dev->mode_config.connector_free_list);
404 	INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
405 
406 	drm_mode_create_standard_properties(dev);
407 
408 	/* Just to be sure */
409 	dev->mode_config.num_fb = 0;
410 	dev->mode_config.num_connector = 0;
411 	dev->mode_config.num_crtc = 0;
412 	dev->mode_config.num_encoder = 0;
413 	dev->mode_config.num_total_plane = 0;
414 }
415 EXPORT_SYMBOL(drm_mode_config_init);
416 
417 /**
418  * drm_mode_config_cleanup - free up DRM mode_config info
419  * @dev: DRM device
420  *
421  * Free up all the connectors and CRTCs associated with this DRM device, then
422  * free up the framebuffers and associated buffer objects.
423  *
424  * Note that since this /should/ happen single-threaded at driver/device
425  * teardown time, no locking is required. It's the driver's job to ensure that
426  * this guarantee actually holds true.
427  *
428  * FIXME: cleanup any dangling user buffer objects too
429  */
430 void drm_mode_config_cleanup(struct drm_device *dev)
431 {
432 	struct drm_connector *connector;
433 	struct drm_connector_list_iter conn_iter;
434 	struct drm_crtc *crtc, *ct;
435 	struct drm_encoder *encoder, *enct;
436 	struct drm_framebuffer *fb, *fbt;
437 	struct drm_property *property, *pt;
438 	struct drm_property_blob *blob, *bt;
439 	struct drm_plane *plane, *plt;
440 
441 	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
442 				 head) {
443 		encoder->funcs->destroy(encoder);
444 	}
445 
446 	drm_connector_list_iter_begin(dev, &conn_iter);
447 	drm_for_each_connector_iter(connector, &conn_iter) {
448 		/* drm_connector_list_iter holds an full reference to the
449 		 * current connector itself, which means it is inherently safe
450 		 * against unreferencing the current connector - but not against
451 		 * deleting it right away. */
452 		drm_connector_put(connector);
453 	}
454 	drm_connector_list_iter_end(&conn_iter);
455 	/* connector_iter drops references in a work item. */
456 	flush_work(&dev->mode_config.connector_free_work);
457 	if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
458 		drm_connector_list_iter_begin(dev, &conn_iter);
459 		drm_for_each_connector_iter(connector, &conn_iter)
460 			DRM_ERROR("connector %s leaked!\n", connector->name);
461 		drm_connector_list_iter_end(&conn_iter);
462 	}
463 
464 	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
465 				 head) {
466 		drm_property_destroy(dev, property);
467 	}
468 
469 	list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
470 				 head) {
471 		plane->funcs->destroy(plane);
472 	}
473 
474 	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
475 		crtc->funcs->destroy(crtc);
476 	}
477 
478 	list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
479 				 head_global) {
480 		drm_property_blob_put(blob);
481 	}
482 
483 	/*
484 	 * Single-threaded teardown context, so it's not required to grab the
485 	 * fb_lock to protect against concurrent fb_list access. Contrary, it
486 	 * would actually deadlock with the drm_framebuffer_cleanup function.
487 	 *
488 	 * Also, if there are any framebuffers left, that's a driver leak now,
489 	 * so politely WARN about this.
490 	 */
491 	WARN_ON(!list_empty(&dev->mode_config.fb_list));
492 	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
493 		struct drm_printer p = drm_debug_printer("[leaked fb]");
494 		drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
495 		drm_framebuffer_print_info(&p, 1, fb);
496 		drm_framebuffer_free(&fb->base.refcount);
497 	}
498 
499 	ida_destroy(&dev->mode_config.connector_ida);
500 	idr_destroy(&dev->mode_config.tile_idr);
501 	idr_destroy(&dev->mode_config.object_idr);
502 	drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
503 }
504 EXPORT_SYMBOL(drm_mode_config_cleanup);
505