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