• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  * Copyright (c) 2008 Red Hat Inc.
5  *
6  * DRM core CRTC related functions
7  *
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.
17  *
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
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Keith Packard
28  *	Eric Anholt <eric@anholt.net>
29  *      Dave Airlie <airlied@linux.ie>
30  *      Jesse Barnes <jesse.barnes@intel.com>
31  */
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
36 #include <linux/fence.h>
37 #include <drm/drmP.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_edid.h>
40 #include <drm/drm_fourcc.h>
41 #include <drm/drm_modeset_lock.h>
42 #include <drm/drm_atomic.h>
43 #include <drm/drm_auth.h>
44 #include <drm/drm_framebuffer.h>
45 
46 #include "drm_crtc_internal.h"
47 #include "drm_internal.h"
48 
49 /*
50  * Global properties
51  */
52 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
53 	{ DRM_PLANE_TYPE_OVERLAY, "Overlay" },
54 	{ DRM_PLANE_TYPE_PRIMARY, "Primary" },
55 	{ DRM_PLANE_TYPE_CURSOR, "Cursor" },
56 };
57 
58 /*
59  * Optional properties
60  */
61 /**
62  * drm_crtc_force_disable - Forcibly turn off a CRTC
63  * @crtc: CRTC to turn off
64  *
65  * Returns:
66  * Zero on success, error code on failure.
67  */
drm_crtc_force_disable(struct drm_crtc * crtc)68 int drm_crtc_force_disable(struct drm_crtc *crtc)
69 {
70 	struct drm_mode_set set = {
71 		.crtc = crtc,
72 	};
73 
74 	return drm_mode_set_config_internal(&set);
75 }
76 EXPORT_SYMBOL(drm_crtc_force_disable);
77 
78 /**
79  * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
80  * @dev: DRM device whose CRTCs to turn off
81  *
82  * Drivers may want to call this on unload to ensure that all displays are
83  * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
84  *
85  * Returns:
86  * Zero on success, error code on failure.
87  */
drm_crtc_force_disable_all(struct drm_device * dev)88 int drm_crtc_force_disable_all(struct drm_device *dev)
89 {
90 	struct drm_crtc *crtc;
91 	int ret = 0;
92 
93 	drm_modeset_lock_all(dev);
94 	drm_for_each_crtc(crtc, dev)
95 		if (crtc->enabled) {
96 			ret = drm_crtc_force_disable(crtc);
97 			if (ret)
98 				goto out;
99 		}
100 out:
101 	drm_modeset_unlock_all(dev);
102 	return ret;
103 }
104 EXPORT_SYMBOL(drm_crtc_force_disable_all);
105 
106 DEFINE_WW_CLASS(crtc_ww_class);
107 
drm_num_crtcs(struct drm_device * dev)108 static unsigned int drm_num_crtcs(struct drm_device *dev)
109 {
110 	unsigned int num = 0;
111 	struct drm_crtc *tmp;
112 
113 	drm_for_each_crtc(tmp, dev) {
114 		num++;
115 	}
116 
117 	return num;
118 }
119 
drm_crtc_register_all(struct drm_device * dev)120 static int drm_crtc_register_all(struct drm_device *dev)
121 {
122 	struct drm_crtc *crtc;
123 	int ret = 0;
124 
125 	drm_for_each_crtc(crtc, dev) {
126 		if (crtc->funcs->late_register)
127 			ret = crtc->funcs->late_register(crtc);
128 		if (ret)
129 			return ret;
130 	}
131 
132 	return 0;
133 }
134 
drm_crtc_unregister_all(struct drm_device * dev)135 static void drm_crtc_unregister_all(struct drm_device *dev)
136 {
137 	struct drm_crtc *crtc;
138 
139 	drm_for_each_crtc(crtc, dev) {
140 		if (crtc->funcs->early_unregister)
141 			crtc->funcs->early_unregister(crtc);
142 	}
143 }
144 
145 static const struct fence_ops drm_crtc_fence_ops;
146 
fence_to_crtc(struct fence * fence)147 static struct drm_crtc *fence_to_crtc(struct fence *fence)
148 {
149 	BUG_ON(fence->ops != &drm_crtc_fence_ops);
150 	return container_of(fence->lock, struct drm_crtc, fence_lock);
151 }
152 
drm_crtc_fence_get_driver_name(struct fence * fence)153 static const char *drm_crtc_fence_get_driver_name(struct fence *fence)
154 {
155 	struct drm_crtc *crtc = fence_to_crtc(fence);
156 
157 	return crtc->dev->driver->name;
158 }
159 
drm_crtc_fence_get_timeline_name(struct fence * fence)160 static const char *drm_crtc_fence_get_timeline_name(struct fence *fence)
161 {
162 	struct drm_crtc *crtc = fence_to_crtc(fence);
163 
164 	return crtc->timeline_name;
165 }
166 
drm_crtc_fence_enable_signaling(struct fence * fence)167 static bool drm_crtc_fence_enable_signaling(struct fence *fence)
168 {
169 	return true;
170 }
171 
172 static const struct fence_ops drm_crtc_fence_ops = {
173 	.get_driver_name = drm_crtc_fence_get_driver_name,
174 	.get_timeline_name = drm_crtc_fence_get_timeline_name,
175 	.enable_signaling = drm_crtc_fence_enable_signaling,
176 	.wait = fence_default_wait,
177 };
178 
drm_crtc_create_fence(struct drm_crtc * crtc)179 struct fence *drm_crtc_create_fence(struct drm_crtc *crtc)
180 {
181 	struct fence *fence;
182 
183 	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
184 	if (!fence)
185 		return NULL;
186 
187 	fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
188 		       crtc->fence_context, ++crtc->fence_seqno);
189 
190 	return fence;
191 }
192 
193 /**
194  * drm_crtc_init_with_planes - Initialise a new CRTC object with
195  *    specified primary and cursor planes.
196  * @dev: DRM device
197  * @crtc: CRTC object to init
198  * @primary: Primary plane for CRTC
199  * @cursor: Cursor plane for CRTC
200  * @funcs: callbacks for the new CRTC
201  * @name: printf style format string for the CRTC name, or NULL for default name
202  *
203  * Inits a new object created as base part of a driver crtc object. Drivers
204  * should use this function instead of drm_crtc_init(), which is only provided
205  * for backwards compatibility with drivers which do not yet support universal
206  * planes). For really simple hardware which has only 1 plane look at
207  * drm_simple_display_pipe_init() instead.
208  *
209  * Returns:
210  * Zero on success, error code on failure.
211  */
drm_crtc_init_with_planes(struct drm_device * dev,struct drm_crtc * crtc,struct drm_plane * primary,struct drm_plane * cursor,const struct drm_crtc_funcs * funcs,const char * name,...)212 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
213 			      struct drm_plane *primary,
214 			      struct drm_plane *cursor,
215 			      const struct drm_crtc_funcs *funcs,
216 			      const char *name, ...)
217 {
218 	struct drm_mode_config *config = &dev->mode_config;
219 	int ret;
220 
221 	WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
222 	WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
223 
224 	crtc->dev = dev;
225 	crtc->funcs = funcs;
226 
227 	INIT_LIST_HEAD(&crtc->commit_list);
228 	spin_lock_init(&crtc->commit_lock);
229 
230 	drm_modeset_lock_init(&crtc->mutex);
231 	ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
232 	if (ret)
233 		return ret;
234 
235 	if (name) {
236 		va_list ap;
237 
238 		va_start(ap, name);
239 		crtc->name = kvasprintf(GFP_KERNEL, name, ap);
240 		va_end(ap);
241 	} else {
242 		crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
243 				       drm_num_crtcs(dev));
244 	}
245 	if (!crtc->name) {
246 		drm_mode_object_unregister(dev, &crtc->base);
247 		return -ENOMEM;
248 	}
249 
250 	crtc->fence_context = fence_context_alloc(1);
251 	spin_lock_init(&crtc->fence_lock);
252 	snprintf(crtc->timeline_name, sizeof(crtc->timeline_name),
253 		 "CRTC:%d-%s", crtc->base.id, crtc->name);
254 
255 	crtc->base.properties = &crtc->properties;
256 
257 	list_add_tail(&crtc->head, &config->crtc_list);
258 	crtc->index = config->num_crtc++;
259 
260 	crtc->primary = primary;
261 	crtc->cursor = cursor;
262 	if (primary)
263 		primary->possible_crtcs = 1 << drm_crtc_index(crtc);
264 	if (cursor)
265 		cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
266 
267 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
268 		drm_object_attach_property(&crtc->base, config->prop_active, 0);
269 		drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
270 		drm_object_attach_property(&crtc->base,
271 					   config->prop_out_fence_ptr, 0);
272 	}
273 
274 	return 0;
275 }
276 EXPORT_SYMBOL(drm_crtc_init_with_planes);
277 
278 /**
279  * drm_crtc_cleanup - Clean up the core crtc usage
280  * @crtc: CRTC to cleanup
281  *
282  * This function cleans up @crtc and removes it from the DRM mode setting
283  * core. Note that the function does *not* free the crtc structure itself,
284  * this is the responsibility of the caller.
285  */
drm_crtc_cleanup(struct drm_crtc * crtc)286 void drm_crtc_cleanup(struct drm_crtc *crtc)
287 {
288 	struct drm_device *dev = crtc->dev;
289 
290 	/* Note that the crtc_list is considered to be static; should we
291 	 * remove the drm_crtc at runtime we would have to decrement all
292 	 * the indices on the drm_crtc after us in the crtc_list.
293 	 */
294 
295 	kfree(crtc->gamma_store);
296 	crtc->gamma_store = NULL;
297 
298 	drm_modeset_lock_fini(&crtc->mutex);
299 
300 	drm_mode_object_unregister(dev, &crtc->base);
301 	list_del(&crtc->head);
302 	dev->mode_config.num_crtc--;
303 
304 	WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
305 	if (crtc->state && crtc->funcs->atomic_destroy_state)
306 		crtc->funcs->atomic_destroy_state(crtc, crtc->state);
307 
308 	kfree(crtc->name);
309 
310 	memset(crtc, 0, sizeof(*crtc));
311 }
312 EXPORT_SYMBOL(drm_crtc_cleanup);
313 
drm_modeset_register_all(struct drm_device * dev)314 int drm_modeset_register_all(struct drm_device *dev)
315 {
316 	int ret;
317 
318 	ret = drm_plane_register_all(dev);
319 	if (ret)
320 		goto err_plane;
321 
322 	ret = drm_crtc_register_all(dev);
323 	if  (ret)
324 		goto err_crtc;
325 
326 	ret = drm_encoder_register_all(dev);
327 	if (ret)
328 		goto err_encoder;
329 
330 	ret = drm_connector_register_all(dev);
331 	if (ret)
332 		goto err_connector;
333 
334 	return 0;
335 
336 err_connector:
337 	drm_encoder_unregister_all(dev);
338 err_encoder:
339 	drm_crtc_unregister_all(dev);
340 err_crtc:
341 	drm_plane_unregister_all(dev);
342 err_plane:
343 	return ret;
344 }
345 
drm_modeset_unregister_all(struct drm_device * dev)346 void drm_modeset_unregister_all(struct drm_device *dev)
347 {
348 	drm_connector_unregister_all(dev);
349 	drm_encoder_unregister_all(dev);
350 	drm_crtc_unregister_all(dev);
351 	drm_plane_unregister_all(dev);
352 }
353 
drm_mode_create_standard_properties(struct drm_device * dev)354 static int drm_mode_create_standard_properties(struct drm_device *dev)
355 {
356 	struct drm_property *prop;
357 	int ret;
358 
359 	ret = drm_connector_create_standard_properties(dev);
360 	if (ret)
361 		return ret;
362 
363 	prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
364 					"type", drm_plane_type_enum_list,
365 					ARRAY_SIZE(drm_plane_type_enum_list));
366 	if (!prop)
367 		return -ENOMEM;
368 	dev->mode_config.plane_type_property = prop;
369 
370 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
371 			"SRC_X", 0, UINT_MAX);
372 	if (!prop)
373 		return -ENOMEM;
374 	dev->mode_config.prop_src_x = prop;
375 
376 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
377 			"SRC_Y", 0, UINT_MAX);
378 	if (!prop)
379 		return -ENOMEM;
380 	dev->mode_config.prop_src_y = prop;
381 
382 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
383 			"SRC_W", 0, UINT_MAX);
384 	if (!prop)
385 		return -ENOMEM;
386 	dev->mode_config.prop_src_w = prop;
387 
388 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
389 			"SRC_H", 0, UINT_MAX);
390 	if (!prop)
391 		return -ENOMEM;
392 	dev->mode_config.prop_src_h = prop;
393 
394 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
395 			"CRTC_X", INT_MIN, INT_MAX);
396 	if (!prop)
397 		return -ENOMEM;
398 	dev->mode_config.prop_crtc_x = prop;
399 
400 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
401 			"CRTC_Y", INT_MIN, INT_MAX);
402 	if (!prop)
403 		return -ENOMEM;
404 	dev->mode_config.prop_crtc_y = prop;
405 
406 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
407 			"CRTC_W", 0, INT_MAX);
408 	if (!prop)
409 		return -ENOMEM;
410 	dev->mode_config.prop_crtc_w = prop;
411 
412 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
413 			"CRTC_H", 0, INT_MAX);
414 	if (!prop)
415 		return -ENOMEM;
416 	dev->mode_config.prop_crtc_h = prop;
417 
418 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
419 			"FB_ID", DRM_MODE_OBJECT_FB);
420 	if (!prop)
421 		return -ENOMEM;
422 	dev->mode_config.prop_fb_id = prop;
423 
424 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
425 			"IN_FENCE_FD", -1, INT_MAX);
426 	if (!prop)
427 		return -ENOMEM;
428 	dev->mode_config.prop_in_fence_fd = prop;
429 
430 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
431 			"OUT_FENCE_PTR", 0, U64_MAX);
432 	if (!prop)
433 		return -ENOMEM;
434 	dev->mode_config.prop_out_fence_ptr = prop;
435 
436 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
437 			"CRTC_ID", DRM_MODE_OBJECT_CRTC);
438 	if (!prop)
439 		return -ENOMEM;
440 	dev->mode_config.prop_crtc_id = prop;
441 
442 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
443 			"ACTIVE");
444 	if (!prop)
445 		return -ENOMEM;
446 	dev->mode_config.prop_active = prop;
447 
448 	prop = drm_property_create(dev,
449 			DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
450 			"MODE_ID", 0);
451 	if (!prop)
452 		return -ENOMEM;
453 	dev->mode_config.prop_mode_id = prop;
454 
455 	prop = drm_property_create(dev,
456 			DRM_MODE_PROP_BLOB,
457 			"DEGAMMA_LUT", 0);
458 	if (!prop)
459 		return -ENOMEM;
460 	dev->mode_config.degamma_lut_property = prop;
461 
462 	prop = drm_property_create_range(dev,
463 			DRM_MODE_PROP_IMMUTABLE,
464 			"DEGAMMA_LUT_SIZE", 0, UINT_MAX);
465 	if (!prop)
466 		return -ENOMEM;
467 	dev->mode_config.degamma_lut_size_property = prop;
468 
469 	prop = drm_property_create(dev,
470 			DRM_MODE_PROP_BLOB,
471 			"CTM", 0);
472 	if (!prop)
473 		return -ENOMEM;
474 	dev->mode_config.ctm_property = prop;
475 
476 	prop = drm_property_create(dev,
477 			DRM_MODE_PROP_BLOB,
478 			"GAMMA_LUT", 0);
479 	if (!prop)
480 		return -ENOMEM;
481 	dev->mode_config.gamma_lut_property = prop;
482 
483 	prop = drm_property_create_range(dev,
484 			DRM_MODE_PROP_IMMUTABLE,
485 			"GAMMA_LUT_SIZE", 0, UINT_MAX);
486 	if (!prop)
487 		return -ENOMEM;
488 	dev->mode_config.gamma_lut_size_property = prop;
489 
490 	return 0;
491 }
492 
493 /**
494  * drm_mode_getresources - get graphics configuration
495  * @dev: drm device for the ioctl
496  * @data: data pointer for the ioctl
497  * @file_priv: drm file for the ioctl call
498  *
499  * Construct a set of configuration description structures and return
500  * them to the user, including CRTC, connector and framebuffer configuration.
501  *
502  * Called by the user via ioctl.
503  *
504  * Returns:
505  * Zero on success, negative errno on failure.
506  */
drm_mode_getresources(struct drm_device * dev,void * data,struct drm_file * file_priv)507 int drm_mode_getresources(struct drm_device *dev, void *data,
508 			  struct drm_file *file_priv)
509 {
510 	struct drm_mode_card_res *card_res = data;
511 	struct list_head *lh;
512 	struct drm_framebuffer *fb;
513 	struct drm_connector *connector;
514 	struct drm_crtc *crtc;
515 	struct drm_encoder *encoder;
516 	int ret = 0;
517 	int connector_count = 0;
518 	int crtc_count = 0;
519 	int fb_count = 0;
520 	int encoder_count = 0;
521 	int copied = 0;
522 	uint32_t __user *fb_id;
523 	uint32_t __user *crtc_id;
524 	uint32_t __user *connector_id;
525 	uint32_t __user *encoder_id;
526 
527 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
528 		return -EINVAL;
529 
530 
531 	mutex_lock(&file_priv->fbs_lock);
532 	/*
533 	 * For the non-control nodes we need to limit the list of resources
534 	 * by IDs in the group list for this node
535 	 */
536 	list_for_each(lh, &file_priv->fbs)
537 		fb_count++;
538 
539 	/* handle this in 4 parts */
540 	/* FBs */
541 	if (card_res->count_fbs >= fb_count) {
542 		copied = 0;
543 		fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
544 		list_for_each_entry(fb, &file_priv->fbs, filp_head) {
545 			if (put_user(fb->base.id, fb_id + copied)) {
546 				mutex_unlock(&file_priv->fbs_lock);
547 				return -EFAULT;
548 			}
549 			copied++;
550 		}
551 	}
552 	card_res->count_fbs = fb_count;
553 	mutex_unlock(&file_priv->fbs_lock);
554 
555 	/* mode_config.mutex protects the connector list against e.g. DP MST
556 	 * connector hot-adding. CRTC/Plane lists are invariant. */
557 	mutex_lock(&dev->mode_config.mutex);
558 	drm_for_each_crtc(crtc, dev)
559 		crtc_count++;
560 
561 	drm_for_each_connector(connector, dev)
562 		connector_count++;
563 
564 	drm_for_each_encoder(encoder, dev)
565 		encoder_count++;
566 
567 	card_res->max_height = dev->mode_config.max_height;
568 	card_res->min_height = dev->mode_config.min_height;
569 	card_res->max_width = dev->mode_config.max_width;
570 	card_res->min_width = dev->mode_config.min_width;
571 
572 	/* CRTCs */
573 	if (card_res->count_crtcs >= crtc_count) {
574 		copied = 0;
575 		crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
576 		drm_for_each_crtc(crtc, dev) {
577 			if (put_user(crtc->base.id, crtc_id + copied)) {
578 				ret = -EFAULT;
579 				goto out;
580 			}
581 			copied++;
582 		}
583 	}
584 	card_res->count_crtcs = crtc_count;
585 
586 	/* Encoders */
587 	if (card_res->count_encoders >= encoder_count) {
588 		copied = 0;
589 		encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
590 		drm_for_each_encoder(encoder, dev) {
591 			if (put_user(encoder->base.id, encoder_id +
592 				     copied)) {
593 				ret = -EFAULT;
594 				goto out;
595 			}
596 			copied++;
597 		}
598 	}
599 	card_res->count_encoders = encoder_count;
600 
601 	/* Connectors */
602 	if (card_res->count_connectors >= connector_count) {
603 		copied = 0;
604 		connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
605 		drm_for_each_connector(connector, dev) {
606 			if (put_user(connector->base.id,
607 				     connector_id + copied)) {
608 				ret = -EFAULT;
609 				goto out;
610 			}
611 			copied++;
612 		}
613 	}
614 	card_res->count_connectors = connector_count;
615 
616 out:
617 	mutex_unlock(&dev->mode_config.mutex);
618 	return ret;
619 }
620 
621 /**
622  * drm_mode_getcrtc - get CRTC configuration
623  * @dev: drm device for the ioctl
624  * @data: data pointer for the ioctl
625  * @file_priv: drm file for the ioctl call
626  *
627  * Construct a CRTC configuration structure to return to the user.
628  *
629  * Called by the user via ioctl.
630  *
631  * Returns:
632  * Zero on success, negative errno on failure.
633  */
drm_mode_getcrtc(struct drm_device * dev,void * data,struct drm_file * file_priv)634 int drm_mode_getcrtc(struct drm_device *dev,
635 		     void *data, struct drm_file *file_priv)
636 {
637 	struct drm_mode_crtc *crtc_resp = data;
638 	struct drm_crtc *crtc;
639 
640 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
641 		return -EINVAL;
642 
643 	crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
644 	if (!crtc)
645 		return -ENOENT;
646 
647 	drm_modeset_lock_crtc(crtc, crtc->primary);
648 	crtc_resp->gamma_size = crtc->gamma_size;
649 	if (crtc->primary->fb)
650 		crtc_resp->fb_id = crtc->primary->fb->base.id;
651 	else
652 		crtc_resp->fb_id = 0;
653 
654 	if (crtc->state) {
655 		crtc_resp->x = crtc->primary->state->src_x >> 16;
656 		crtc_resp->y = crtc->primary->state->src_y >> 16;
657 		if (crtc->state->enable) {
658 			drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
659 			crtc_resp->mode_valid = 1;
660 
661 		} else {
662 			crtc_resp->mode_valid = 0;
663 		}
664 	} else {
665 		crtc_resp->x = crtc->x;
666 		crtc_resp->y = crtc->y;
667 		if (crtc->enabled) {
668 			drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
669 			crtc_resp->mode_valid = 1;
670 
671 		} else {
672 			crtc_resp->mode_valid = 0;
673 		}
674 	}
675 	drm_modeset_unlock_crtc(crtc);
676 
677 	return 0;
678 }
679 
680 /**
681  * drm_mode_set_config_internal - helper to call ->set_config
682  * @set: modeset config to set
683  *
684  * This is a little helper to wrap internal calls to the ->set_config driver
685  * interface. The only thing it adds is correct refcounting dance.
686  *
687  * Returns:
688  * Zero on success, negative errno on failure.
689  */
drm_mode_set_config_internal(struct drm_mode_set * set)690 int drm_mode_set_config_internal(struct drm_mode_set *set)
691 {
692 	struct drm_crtc *crtc = set->crtc;
693 	struct drm_framebuffer *fb;
694 	struct drm_crtc *tmp;
695 	int ret;
696 
697 	/*
698 	 * NOTE: ->set_config can also disable other crtcs (if we steal all
699 	 * connectors from it), hence we need to refcount the fbs across all
700 	 * crtcs. Atomic modeset will have saner semantics ...
701 	 */
702 	drm_for_each_crtc(tmp, crtc->dev)
703 		tmp->primary->old_fb = tmp->primary->fb;
704 
705 	fb = set->fb;
706 
707 	ret = crtc->funcs->set_config(set);
708 	if (ret == 0) {
709 		crtc->primary->crtc = crtc;
710 		crtc->primary->fb = fb;
711 	}
712 
713 	drm_for_each_crtc(tmp, crtc->dev) {
714 		if (tmp->primary->fb)
715 			drm_framebuffer_reference(tmp->primary->fb);
716 		if (tmp->primary->old_fb)
717 			drm_framebuffer_unreference(tmp->primary->old_fb);
718 		tmp->primary->old_fb = NULL;
719 	}
720 
721 	return ret;
722 }
723 EXPORT_SYMBOL(drm_mode_set_config_internal);
724 
725 /**
726  * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
727  * @mode: mode to query
728  * @hdisplay: hdisplay value to fill in
729  * @vdisplay: vdisplay value to fill in
730  *
731  * The vdisplay value will be doubled if the specified mode is a stereo mode of
732  * the appropriate layout.
733  */
drm_crtc_get_hv_timing(const struct drm_display_mode * mode,int * hdisplay,int * vdisplay)734 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
735 			    int *hdisplay, int *vdisplay)
736 {
737 	struct drm_display_mode adjusted;
738 
739 	drm_mode_copy(&adjusted, mode);
740 	drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
741 	*hdisplay = adjusted.crtc_hdisplay;
742 	*vdisplay = adjusted.crtc_vdisplay;
743 }
744 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
745 
746 /**
747  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
748  *     CRTC viewport
749  * @crtc: CRTC that framebuffer will be displayed on
750  * @x: x panning
751  * @y: y panning
752  * @mode: mode that framebuffer will be displayed under
753  * @fb: framebuffer to check size of
754  */
drm_crtc_check_viewport(const struct drm_crtc * crtc,int x,int y,const struct drm_display_mode * mode,const struct drm_framebuffer * fb)755 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
756 			    int x, int y,
757 			    const struct drm_display_mode *mode,
758 			    const struct drm_framebuffer *fb)
759 
760 {
761 	int hdisplay, vdisplay;
762 
763 	drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
764 
765 	if (crtc->state &&
766 	    crtc->primary->state->rotation & (DRM_ROTATE_90 |
767 					      DRM_ROTATE_270))
768 		swap(hdisplay, vdisplay);
769 
770 	return drm_framebuffer_check_src_coords(x << 16, y << 16,
771 						hdisplay << 16, vdisplay << 16,
772 						fb);
773 }
774 EXPORT_SYMBOL(drm_crtc_check_viewport);
775 
776 /**
777  * drm_mode_setcrtc - set CRTC configuration
778  * @dev: drm device for the ioctl
779  * @data: data pointer for the ioctl
780  * @file_priv: drm file for the ioctl call
781  *
782  * Build a new CRTC configuration based on user request.
783  *
784  * Called by the user via ioctl.
785  *
786  * Returns:
787  * Zero on success, negative errno on failure.
788  */
drm_mode_setcrtc(struct drm_device * dev,void * data,struct drm_file * file_priv)789 int drm_mode_setcrtc(struct drm_device *dev, void *data,
790 		     struct drm_file *file_priv)
791 {
792 	struct drm_mode_config *config = &dev->mode_config;
793 	struct drm_mode_crtc *crtc_req = data;
794 	struct drm_crtc *crtc;
795 	struct drm_connector **connector_set = NULL, *connector;
796 	struct drm_framebuffer *fb = NULL;
797 	struct drm_display_mode *mode = NULL;
798 	struct drm_mode_set set;
799 	uint32_t __user *set_connectors_ptr;
800 	int ret;
801 	int i;
802 
803 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
804 		return -EINVAL;
805 
806 	/*
807 	 * Universal plane src offsets are only 16.16, prevent havoc for
808 	 * drivers using universal plane code internally.
809 	 */
810 	if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
811 		return -ERANGE;
812 
813 	drm_modeset_lock_all(dev);
814 	crtc = drm_crtc_find(dev, crtc_req->crtc_id);
815 	if (!crtc) {
816 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
817 		ret = -ENOENT;
818 		goto out;
819 	}
820 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
821 
822 	if (crtc_req->mode_valid) {
823 		/* If we have a mode we need a framebuffer. */
824 		/* If we pass -1, set the mode with the currently bound fb */
825 		if (crtc_req->fb_id == -1) {
826 			if (!crtc->primary->fb) {
827 				DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
828 				ret = -EINVAL;
829 				goto out;
830 			}
831 			fb = crtc->primary->fb;
832 			/* Make refcounting symmetric with the lookup path. */
833 			drm_framebuffer_reference(fb);
834 		} else {
835 			fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
836 			if (!fb) {
837 				DRM_DEBUG_KMS("Unknown FB ID%d\n",
838 						crtc_req->fb_id);
839 				ret = -ENOENT;
840 				goto out;
841 			}
842 		}
843 
844 		mode = drm_mode_create(dev);
845 		if (!mode) {
846 			ret = -ENOMEM;
847 			goto out;
848 		}
849 
850 		ret = drm_mode_convert_umode(mode, &crtc_req->mode);
851 		if (ret) {
852 			DRM_DEBUG_KMS("Invalid mode\n");
853 			goto out;
854 		}
855 
856 		/*
857 		 * Check whether the primary plane supports the fb pixel format.
858 		 * Drivers not implementing the universal planes API use a
859 		 * default formats list provided by the DRM core which doesn't
860 		 * match real hardware capabilities. Skip the check in that
861 		 * case.
862 		 */
863 		if (!crtc->primary->format_default) {
864 			ret = drm_plane_check_pixel_format(crtc->primary,
865 							   fb->pixel_format);
866 			if (ret) {
867 				char *format_name = drm_get_format_name(fb->pixel_format);
868 				DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
869 				kfree(format_name);
870 				goto out;
871 			}
872 		}
873 
874 		ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
875 					      mode, fb);
876 		if (ret)
877 			goto out;
878 
879 	}
880 
881 	if (crtc_req->count_connectors == 0 && mode) {
882 		DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
883 		ret = -EINVAL;
884 		goto out;
885 	}
886 
887 	if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
888 		DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
889 			  crtc_req->count_connectors);
890 		ret = -EINVAL;
891 		goto out;
892 	}
893 
894 	if (crtc_req->count_connectors > 0) {
895 		u32 out_id;
896 
897 		/* Avoid unbounded kernel memory allocation */
898 		if (crtc_req->count_connectors > config->num_connector) {
899 			ret = -EINVAL;
900 			goto out;
901 		}
902 
903 		connector_set = kmalloc_array(crtc_req->count_connectors,
904 					      sizeof(struct drm_connector *),
905 					      GFP_KERNEL);
906 		if (!connector_set) {
907 			ret = -ENOMEM;
908 			goto out;
909 		}
910 
911 		for (i = 0; i < crtc_req->count_connectors; i++) {
912 			connector_set[i] = NULL;
913 			set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
914 			if (get_user(out_id, &set_connectors_ptr[i])) {
915 				ret = -EFAULT;
916 				goto out;
917 			}
918 
919 			connector = drm_connector_lookup(dev, out_id);
920 			if (!connector) {
921 				DRM_DEBUG_KMS("Connector id %d unknown\n",
922 						out_id);
923 				ret = -ENOENT;
924 				goto out;
925 			}
926 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
927 					connector->base.id,
928 					connector->name);
929 
930 			connector_set[i] = connector;
931 		}
932 	}
933 
934 	set.crtc = crtc;
935 	set.x = crtc_req->x;
936 	set.y = crtc_req->y;
937 	set.mode = mode;
938 	set.connectors = connector_set;
939 	set.num_connectors = crtc_req->count_connectors;
940 	set.fb = fb;
941 	ret = drm_mode_set_config_internal(&set);
942 
943 out:
944 	if (fb)
945 		drm_framebuffer_unreference(fb);
946 
947 	if (connector_set) {
948 		for (i = 0; i < crtc_req->count_connectors; i++) {
949 			if (connector_set[i])
950 				drm_connector_unreference(connector_set[i]);
951 		}
952 	}
953 	kfree(connector_set);
954 	drm_mode_destroy(dev, mode);
955 	drm_modeset_unlock_all(dev);
956 	return ret;
957 }
958 
drm_mode_crtc_set_obj_prop(struct drm_mode_object * obj,struct drm_property * property,uint64_t value)959 int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
960 			       struct drm_property *property,
961 			       uint64_t value)
962 {
963 	int ret = -EINVAL;
964 	struct drm_crtc *crtc = obj_to_crtc(obj);
965 
966 	if (crtc->funcs->set_property)
967 		ret = crtc->funcs->set_property(crtc, property, value);
968 	if (!ret)
969 		drm_object_property_set_value(obj, property, value);
970 
971 	return ret;
972 }
973 
974 /**
975  * drm_mode_config_reset - call ->reset callbacks
976  * @dev: drm device
977  *
978  * This functions calls all the crtc's, encoder's and connector's ->reset
979  * callback. Drivers can use this in e.g. their driver load or resume code to
980  * reset hardware and software state.
981  */
drm_mode_config_reset(struct drm_device * dev)982 void drm_mode_config_reset(struct drm_device *dev)
983 {
984 	struct drm_crtc *crtc;
985 	struct drm_plane *plane;
986 	struct drm_encoder *encoder;
987 	struct drm_connector *connector;
988 
989 	drm_for_each_plane(plane, dev)
990 		if (plane->funcs->reset)
991 			plane->funcs->reset(plane);
992 
993 	drm_for_each_crtc(crtc, dev)
994 		if (crtc->funcs->reset)
995 			crtc->funcs->reset(crtc);
996 
997 	drm_for_each_encoder(encoder, dev)
998 		if (encoder->funcs->reset)
999 			encoder->funcs->reset(encoder);
1000 
1001 	mutex_lock(&dev->mode_config.mutex);
1002 	drm_for_each_connector(connector, dev)
1003 		if (connector->funcs->reset)
1004 			connector->funcs->reset(connector);
1005 	mutex_unlock(&dev->mode_config.mutex);
1006 }
1007 EXPORT_SYMBOL(drm_mode_config_reset);
1008 
1009 /**
1010  * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
1011  * @dev: DRM device
1012  * @data: ioctl data
1013  * @file_priv: DRM file info
1014  *
1015  * This creates a new dumb buffer in the driver's backing storage manager (GEM,
1016  * TTM or something else entirely) and returns the resulting buffer handle. This
1017  * handle can then be wrapped up into a framebuffer modeset object.
1018  *
1019  * Note that userspace is not allowed to use such objects for render
1020  * acceleration - drivers must create their own private ioctls for such a use
1021  * case.
1022  *
1023  * Called by the user via ioctl.
1024  *
1025  * Returns:
1026  * Zero on success, negative errno on failure.
1027  */
drm_mode_create_dumb_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1028 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
1029 			       void *data, struct drm_file *file_priv)
1030 {
1031 	struct drm_mode_create_dumb *args = data;
1032 	u32 cpp, stride, size;
1033 
1034 	if (!dev->driver->dumb_create)
1035 		return -ENOSYS;
1036 	if (!args->width || !args->height || !args->bpp)
1037 		return -EINVAL;
1038 
1039 	/* overflow checks for 32bit size calculations */
1040 	/* NOTE: DIV_ROUND_UP() can overflow */
1041 	cpp = DIV_ROUND_UP(args->bpp, 8);
1042 	if (!cpp || cpp > 0xffffffffU / args->width)
1043 		return -EINVAL;
1044 	stride = cpp * args->width;
1045 	if (args->height > 0xffffffffU / stride)
1046 		return -EINVAL;
1047 
1048 	/* test for wrap-around */
1049 	size = args->height * stride;
1050 	if (PAGE_ALIGN(size) == 0)
1051 		return -EINVAL;
1052 
1053 	/*
1054 	 * handle, pitch and size are output parameters. Zero them out to
1055 	 * prevent drivers from accidentally using uninitialized data. Since
1056 	 * not all existing userspace is clearing these fields properly we
1057 	 * cannot reject IOCTL with garbage in them.
1058 	 */
1059 	args->handle = 0;
1060 	args->pitch = 0;
1061 	args->size = 0;
1062 
1063 	return dev->driver->dumb_create(file_priv, dev, args);
1064 }
1065 
1066 /**
1067  * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
1068  * @dev: DRM device
1069  * @data: ioctl data
1070  * @file_priv: DRM file info
1071  *
1072  * Allocate an offset in the drm device node's address space to be able to
1073  * memory map a dumb buffer.
1074  *
1075  * Called by the user via ioctl.
1076  *
1077  * Returns:
1078  * Zero on success, negative errno on failure.
1079  */
drm_mode_mmap_dumb_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1080 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
1081 			     void *data, struct drm_file *file_priv)
1082 {
1083 	struct drm_mode_map_dumb *args = data;
1084 
1085 	/* call driver ioctl to get mmap offset */
1086 	if (!dev->driver->dumb_map_offset)
1087 		return -ENOSYS;
1088 
1089 	return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
1090 }
1091 
1092 /**
1093  * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
1094  * @dev: DRM device
1095  * @data: ioctl data
1096  * @file_priv: DRM file info
1097  *
1098  * This destroys the userspace handle for the given dumb backing storage buffer.
1099  * Since buffer objects must be reference counted in the kernel a buffer object
1100  * won't be immediately freed if a framebuffer modeset object still uses it.
1101  *
1102  * Called by the user via ioctl.
1103  *
1104  * Returns:
1105  * Zero on success, negative errno on failure.
1106  */
drm_mode_destroy_dumb_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1107 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
1108 				void *data, struct drm_file *file_priv)
1109 {
1110 	struct drm_mode_destroy_dumb *args = data;
1111 
1112 	if (!dev->driver->dumb_destroy)
1113 		return -ENOSYS;
1114 
1115 	return dev->driver->dumb_destroy(file_priv, dev, args->handle);
1116 }
1117 
1118 /**
1119  * drm_mode_config_init - initialize DRM mode_configuration structure
1120  * @dev: DRM device
1121  *
1122  * Initialize @dev's mode_config structure, used for tracking the graphics
1123  * configuration of @dev.
1124  *
1125  * Since this initializes the modeset locks, no locking is possible. Which is no
1126  * problem, since this should happen single threaded at init time. It is the
1127  * driver's problem to ensure this guarantee.
1128  *
1129  */
drm_mode_config_init(struct drm_device * dev)1130 void drm_mode_config_init(struct drm_device *dev)
1131 {
1132 	mutex_init(&dev->mode_config.mutex);
1133 	drm_modeset_lock_init(&dev->mode_config.connection_mutex);
1134 	mutex_init(&dev->mode_config.idr_mutex);
1135 	mutex_init(&dev->mode_config.fb_lock);
1136 	mutex_init(&dev->mode_config.blob_lock);
1137 	INIT_LIST_HEAD(&dev->mode_config.fb_list);
1138 	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1139 	INIT_LIST_HEAD(&dev->mode_config.connector_list);
1140 	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1141 	INIT_LIST_HEAD(&dev->mode_config.property_list);
1142 	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
1143 	INIT_LIST_HEAD(&dev->mode_config.plane_list);
1144 	idr_init(&dev->mode_config.crtc_idr);
1145 	idr_init(&dev->mode_config.tile_idr);
1146 	ida_init(&dev->mode_config.connector_ida);
1147 
1148 	drm_modeset_lock_all(dev);
1149 	drm_mode_create_standard_properties(dev);
1150 	drm_modeset_unlock_all(dev);
1151 
1152 	/* Just to be sure */
1153 	dev->mode_config.num_fb = 0;
1154 	dev->mode_config.num_connector = 0;
1155 	dev->mode_config.num_crtc = 0;
1156 	dev->mode_config.num_encoder = 0;
1157 	dev->mode_config.num_overlay_plane = 0;
1158 	dev->mode_config.num_total_plane = 0;
1159 }
1160 EXPORT_SYMBOL(drm_mode_config_init);
1161 
1162 /**
1163  * drm_mode_config_cleanup - free up DRM mode_config info
1164  * @dev: DRM device
1165  *
1166  * Free up all the connectors and CRTCs associated with this DRM device, then
1167  * free up the framebuffers and associated buffer objects.
1168  *
1169  * Note that since this /should/ happen single-threaded at driver/device
1170  * teardown time, no locking is required. It's the driver's job to ensure that
1171  * this guarantee actually holds true.
1172  *
1173  * FIXME: cleanup any dangling user buffer objects too
1174  */
drm_mode_config_cleanup(struct drm_device * dev)1175 void drm_mode_config_cleanup(struct drm_device *dev)
1176 {
1177 	struct drm_connector *connector, *ot;
1178 	struct drm_crtc *crtc, *ct;
1179 	struct drm_encoder *encoder, *enct;
1180 	struct drm_framebuffer *fb, *fbt;
1181 	struct drm_property *property, *pt;
1182 	struct drm_property_blob *blob, *bt;
1183 	struct drm_plane *plane, *plt;
1184 
1185 	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1186 				 head) {
1187 		encoder->funcs->destroy(encoder);
1188 	}
1189 
1190 	list_for_each_entry_safe(connector, ot,
1191 				 &dev->mode_config.connector_list, head) {
1192 		connector->funcs->destroy(connector);
1193 	}
1194 
1195 	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1196 				 head) {
1197 		drm_property_destroy(dev, property);
1198 	}
1199 
1200 	list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1201 				 head) {
1202 		plane->funcs->destroy(plane);
1203 	}
1204 
1205 	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1206 		crtc->funcs->destroy(crtc);
1207 	}
1208 
1209 	list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
1210 				 head_global) {
1211 		drm_property_unreference_blob(blob);
1212 	}
1213 
1214 	/*
1215 	 * Single-threaded teardown context, so it's not required to grab the
1216 	 * fb_lock to protect against concurrent fb_list access. Contrary, it
1217 	 * would actually deadlock with the drm_framebuffer_cleanup function.
1218 	 *
1219 	 * Also, if there are any framebuffers left, that's a driver leak now,
1220 	 * so politely WARN about this.
1221 	 */
1222 	WARN_ON(!list_empty(&dev->mode_config.fb_list));
1223 	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1224 		drm_framebuffer_free(&fb->base.refcount);
1225 	}
1226 
1227 	ida_destroy(&dev->mode_config.connector_ida);
1228 	idr_destroy(&dev->mode_config.tile_idr);
1229 	idr_destroy(&dev->mode_config.crtc_idr);
1230 	drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
1231 }
1232 EXPORT_SYMBOL(drm_mode_config_cleanup);
1233 
1234 /**
1235  * DOC: Tile group
1236  *
1237  * Tile groups are used to represent tiled monitors with a unique
1238  * integer identifier. Tiled monitors using DisplayID v1.3 have
1239  * a unique 8-byte handle, we store this in a tile group, so we
1240  * have a common identifier for all tiles in a monitor group.
1241  */
drm_tile_group_free(struct kref * kref)1242 static void drm_tile_group_free(struct kref *kref)
1243 {
1244 	struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1245 	struct drm_device *dev = tg->dev;
1246 	mutex_lock(&dev->mode_config.idr_mutex);
1247 	idr_remove(&dev->mode_config.tile_idr, tg->id);
1248 	mutex_unlock(&dev->mode_config.idr_mutex);
1249 	kfree(tg);
1250 }
1251 
1252 /**
1253  * drm_mode_put_tile_group - drop a reference to a tile group.
1254  * @dev: DRM device
1255  * @tg: tile group to drop reference to.
1256  *
1257  * drop reference to tile group and free if 0.
1258  */
drm_mode_put_tile_group(struct drm_device * dev,struct drm_tile_group * tg)1259 void drm_mode_put_tile_group(struct drm_device *dev,
1260 			     struct drm_tile_group *tg)
1261 {
1262 	kref_put(&tg->refcount, drm_tile_group_free);
1263 }
1264 
1265 /**
1266  * drm_mode_get_tile_group - get a reference to an existing tile group
1267  * @dev: DRM device
1268  * @topology: 8-bytes unique per monitor.
1269  *
1270  * Use the unique bytes to get a reference to an existing tile group.
1271  *
1272  * RETURNS:
1273  * tile group or NULL if not found.
1274  */
drm_mode_get_tile_group(struct drm_device * dev,char topology[8])1275 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1276 					       char topology[8])
1277 {
1278 	struct drm_tile_group *tg;
1279 	int id;
1280 	mutex_lock(&dev->mode_config.idr_mutex);
1281 	idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1282 		if (!memcmp(tg->group_data, topology, 8)) {
1283 			if (!kref_get_unless_zero(&tg->refcount))
1284 				tg = NULL;
1285 			mutex_unlock(&dev->mode_config.idr_mutex);
1286 			return tg;
1287 		}
1288 	}
1289 	mutex_unlock(&dev->mode_config.idr_mutex);
1290 	return NULL;
1291 }
1292 EXPORT_SYMBOL(drm_mode_get_tile_group);
1293 
1294 /**
1295  * drm_mode_create_tile_group - create a tile group from a displayid description
1296  * @dev: DRM device
1297  * @topology: 8-bytes unique per monitor.
1298  *
1299  * Create a tile group for the unique monitor, and get a unique
1300  * identifier for the tile group.
1301  *
1302  * RETURNS:
1303  * new tile group or error.
1304  */
drm_mode_create_tile_group(struct drm_device * dev,char topology[8])1305 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1306 						  char topology[8])
1307 {
1308 	struct drm_tile_group *tg;
1309 	int ret;
1310 
1311 	tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1312 	if (!tg)
1313 		return ERR_PTR(-ENOMEM);
1314 
1315 	kref_init(&tg->refcount);
1316 	memcpy(tg->group_data, topology, 8);
1317 	tg->dev = dev;
1318 
1319 	mutex_lock(&dev->mode_config.idr_mutex);
1320 	ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1321 	if (ret >= 0) {
1322 		tg->id = ret;
1323 	} else {
1324 		kfree(tg);
1325 		tg = ERR_PTR(ret);
1326 	}
1327 
1328 	mutex_unlock(&dev->mode_config.idr_mutex);
1329 	return tg;
1330 }
1331 EXPORT_SYMBOL(drm_mode_create_tile_group);
1332