drm_crtc.c revision 1.15 1 /* $NetBSD: drm_crtc.c,v 1.15 2020/02/14 04:37:27 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2006-2008 Intel Corporation
5 * Copyright (c) 2007 Dave Airlie <airlied (at) linux.ie>
6 * Copyright (c) 2008 Red Hat Inc.
7 *
8 * DRM core CRTC related functions
9 *
10 * Permission to use, copy, modify, distribute, and sell this software and its
11 * documentation for any purpose is hereby granted without fee, provided that
12 * the above copyright notice appear in all copies and that both that copyright
13 * notice and this permission notice appear in supporting documentation, and
14 * that the name of the copyright holders not be used in advertising or
15 * publicity pertaining to distribution of the software without specific,
16 * written prior permission. The copyright holders make no representations
17 * about the suitability of this software for any purpose. It is provided "as
18 * is" without express or implied warranty.
19 *
20 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
22 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
23 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
24 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
25 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26 * OF THIS SOFTWARE.
27 *
28 * Authors:
29 * Keith Packard
30 * Eric Anholt <eric (at) anholt.net>
31 * Dave Airlie <airlied (at) linux.ie>
32 * Jesse Barnes <jesse.barnes (at) intel.com>
33 */
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: drm_crtc.c,v 1.15 2020/02/14 04:37:27 riastradh Exp $");
36
37 #include <linux/err.h>
38 #include <linux/spinlock.h>
39 #include <linux/ctype.h>
40 #include <linux/list.h>
41 #include <linux/slab.h>
42 #include <linux/export.h>
43 #include <linux/errno.h>
44 #include <asm/bug.h>
45 #include <drm/drmP.h>
46 #include <drm/drm_crtc.h>
47 #include <drm/drm_edid.h>
48 #include <drm/drm_fourcc.h>
49 #include <drm/drm_modeset_lock.h>
50 #include <drm/drm_atomic.h>
51
52 #include "drm_crtc_internal.h"
53 #include "drm_internal.h"
54
55 #include <linux/nbsd-namespace.h>
56
57 static struct drm_framebuffer *
58 internal_framebuffer_create(struct drm_device *dev,
59 struct drm_mode_fb_cmd2 *r,
60 struct drm_file *file_priv);
61
62 /* Avoid boilerplate. I'm tired of typing. */
63 #define DRM_ENUM_NAME_FN(fnname, list) \
64 const char *fnname(int val) \
65 { \
66 int i; \
67 for (i = 0; i < ARRAY_SIZE(list); i++) { \
68 if (list[i].type == val) \
69 return list[i].name; \
70 } \
71 return "(unknown)"; \
72 }
73
74 /*
75 * Global properties
76 */
77 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
78 { DRM_MODE_DPMS_ON, "On" },
79 { DRM_MODE_DPMS_STANDBY, "Standby" },
80 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
81 { DRM_MODE_DPMS_OFF, "Off" }
82 };
83
84 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
85
86 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
87 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
88 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
89 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
90 };
91
92 /*
93 * Optional properties
94 */
95 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
96 { DRM_MODE_SCALE_NONE, "None" },
97 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
98 { DRM_MODE_SCALE_CENTER, "Center" },
99 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
100 };
101
102 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
103 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
104 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
105 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
106 };
107
108 /*
109 * Non-global properties, but "required" for certain connectors.
110 */
111 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
112 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
113 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
114 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
115 };
116
117 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
118
119 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
120 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
121 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
122 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
123 };
124
125 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
126 drm_dvi_i_subconnector_enum_list)
127
128 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
129 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
130 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
132 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
133 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
134 };
135
136 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
137
138 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
139 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
140 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
141 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
142 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
143 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
144 };
145
146 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
147 drm_tv_subconnector_enum_list)
148
149 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
150 { DRM_MODE_DIRTY_OFF, "Off" },
151 { DRM_MODE_DIRTY_ON, "On" },
152 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
153 };
154
155 struct drm_conn_prop_enum_list {
156 int type;
157 const char *name;
158 struct ida ida;
159 };
160
161 /*
162 * Connector and encoder types.
163 */
164 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
165 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
166 { DRM_MODE_CONNECTOR_VGA, "VGA" },
167 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
168 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
169 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
170 { DRM_MODE_CONNECTOR_Composite, "Composite" },
171 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
172 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
173 { DRM_MODE_CONNECTOR_Component, "Component" },
174 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
175 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
176 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
177 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
178 { DRM_MODE_CONNECTOR_TV, "TV" },
179 { DRM_MODE_CONNECTOR_eDP, "eDP" },
180 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
181 { DRM_MODE_CONNECTOR_DSI, "DSI" },
182 };
183
184 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
185 { DRM_MODE_ENCODER_NONE, "None" },
186 { DRM_MODE_ENCODER_DAC, "DAC" },
187 { DRM_MODE_ENCODER_TMDS, "TMDS" },
188 { DRM_MODE_ENCODER_LVDS, "LVDS" },
189 { DRM_MODE_ENCODER_TVDAC, "TV" },
190 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
191 { DRM_MODE_ENCODER_DSI, "DSI" },
192 { DRM_MODE_ENCODER_DPMST, "DP MST" },
193 };
194
195 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
196 { SubPixelUnknown, "Unknown" },
197 { SubPixelHorizontalRGB, "Horizontal RGB" },
198 { SubPixelHorizontalBGR, "Horizontal BGR" },
199 { SubPixelVerticalRGB, "Vertical RGB" },
200 { SubPixelVerticalBGR, "Vertical BGR" },
201 { SubPixelNone, "None" },
202 };
203
204 void drm_connector_ida_init(void)
205 {
206 int i;
207
208 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
209 ida_init(&drm_connector_enum_list[i].ida);
210 }
211
212 void drm_connector_ida_destroy(void)
213 {
214 int i;
215
216 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
217 ida_destroy(&drm_connector_enum_list[i].ida);
218 }
219
220 /**
221 * drm_get_connector_status_name - return a string for connector status
222 * @status: connector status to compute name of
223 *
224 * In contrast to the other drm_get_*_name functions this one here returns a
225 * const pointer and hence is threadsafe.
226 */
227 const char *drm_get_connector_status_name(enum drm_connector_status status)
228 {
229 if (status == connector_status_connected)
230 return "connected";
231 else if (status == connector_status_disconnected)
232 return "disconnected";
233 else
234 return "unknown";
235 }
236 EXPORT_SYMBOL(drm_get_connector_status_name);
237
238 /**
239 * drm_get_subpixel_order_name - return a string for a given subpixel enum
240 * @order: enum of subpixel_order
241 *
242 * Note you could abuse this and return something out of bounds, but that
243 * would be a caller error. No unscrubbed user data should make it here.
244 */
245 const char *drm_get_subpixel_order_name(enum subpixel_order order)
246 {
247 return drm_subpixel_enum_list[order].name;
248 }
249 EXPORT_SYMBOL(drm_get_subpixel_order_name);
250
251 static char printable_char(int c)
252 {
253 return isascii(c) && isprint(c) ? c : '?';
254 }
255
256 /**
257 * drm_get_format_name - return a string for drm fourcc format
258 * @format: format to compute name of
259 *
260 * Note that the buffer used by this function is globally shared and owned by
261 * the function itself.
262 *
263 * FIXME: This isn't really multithreading safe.
264 */
265 const char *drm_get_format_name(uint32_t format)
266 {
267 static char buf[32];
268
269 snprintf(buf, sizeof(buf),
270 "%c%c%c%c %s-endian (0x%08x)",
271 printable_char(format & 0xff),
272 printable_char((format >> 8) & 0xff),
273 printable_char((format >> 16) & 0xff),
274 printable_char((format >> 24) & 0x7f),
275 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
276 format);
277
278 return buf;
279 }
280 EXPORT_SYMBOL(drm_get_format_name);
281
282 /*
283 * Internal function to assign a slot in the object idr and optionally
284 * register the object into the idr.
285 */
286 static int drm_mode_object_get_reg(struct drm_device *dev,
287 struct drm_mode_object *obj,
288 uint32_t obj_type,
289 bool register_obj)
290 {
291 int ret;
292
293 idr_preload(GFP_KERNEL);
294 mutex_lock(&dev->mode_config.idr_mutex);
295 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
296 if (ret >= 0) {
297 /*
298 * Set up the object linking under the protection of the idr
299 * lock so that other users can't see inconsistent state.
300 */
301 obj->id = ret;
302 obj->type = obj_type;
303 }
304 mutex_unlock(&dev->mode_config.idr_mutex);
305 idr_preload_end();
306
307 return ret < 0 ? ret : 0;
308 }
309
310 /**
311 * drm_mode_object_get - allocate a new modeset identifier
312 * @dev: DRM device
313 * @obj: object pointer, used to generate unique ID
314 * @obj_type: object type
315 *
316 * Create a unique identifier based on @ptr in @dev's identifier space. Used
317 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
318 * modeset identifiers are _not_ reference counted. Hence don't use this for
319 * reference counted modeset objects like framebuffers.
320 *
321 * Returns:
322 * Zero on success, error code on failure.
323 */
324 int drm_mode_object_get(struct drm_device *dev,
325 struct drm_mode_object *obj, uint32_t obj_type)
326 {
327 return drm_mode_object_get_reg(dev, obj, obj_type, true);
328 }
329
330 static void drm_mode_object_register(struct drm_device *dev,
331 struct drm_mode_object *obj)
332 {
333 mutex_lock(&dev->mode_config.idr_mutex);
334 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
335 mutex_unlock(&dev->mode_config.idr_mutex);
336 }
337
338 /**
339 * drm_mode_object_put - free a modeset identifer
340 * @dev: DRM device
341 * @object: object to free
342 *
343 * Free @id from @dev's unique identifier pool. Note that despite the _get
344 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
345 * for reference counted modeset objects like framebuffers.
346 */
347 void drm_mode_object_put(struct drm_device *dev,
348 struct drm_mode_object *object)
349 {
350 mutex_lock(&dev->mode_config.idr_mutex);
351 idr_remove(&dev->mode_config.crtc_idr, object->id);
352 mutex_unlock(&dev->mode_config.idr_mutex);
353 }
354
355 static struct drm_mode_object *_object_find(struct drm_device *dev,
356 uint32_t id, uint32_t type)
357 {
358 struct drm_mode_object *obj = NULL;
359
360 mutex_lock(&dev->mode_config.idr_mutex);
361 obj = idr_find(&dev->mode_config.crtc_idr, id);
362 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
363 obj = NULL;
364 if (obj && obj->id != id)
365 obj = NULL;
366 /* don't leak out unref'd fb's */
367 if (obj &&
368 (obj->type == DRM_MODE_OBJECT_FB ||
369 obj->type == DRM_MODE_OBJECT_BLOB))
370 obj = NULL;
371 mutex_unlock(&dev->mode_config.idr_mutex);
372
373 return obj;
374 }
375
376 /**
377 * drm_mode_object_find - look up a drm object with static lifetime
378 * @dev: drm device
379 * @id: id of the mode object
380 * @type: type of the mode object
381 *
382 * Note that framebuffers cannot be looked up with this functions - since those
383 * are reference counted, they need special treatment. Even with
384 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
385 * rather than WARN_ON()).
386 */
387 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
388 uint32_t id, uint32_t type)
389 {
390 struct drm_mode_object *obj = NULL;
391
392 /* Framebuffers are reference counted and need their own lookup
393 * function.*/
394 WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
395 obj = _object_find(dev, id, type);
396 return obj;
397 }
398 EXPORT_SYMBOL(drm_mode_object_find);
399
400 /**
401 * drm_framebuffer_init - initialize a framebuffer
402 * @dev: DRM device
403 * @fb: framebuffer to be initialized
404 * @funcs: ... with these functions
405 *
406 * Allocates an ID for the framebuffer's parent mode object, sets its mode
407 * functions & device file and adds it to the master fd list.
408 *
409 * IMPORTANT:
410 * This functions publishes the fb and makes it available for concurrent access
411 * by other users. Which means by this point the fb _must_ be fully set up -
412 * since all the fb attributes are invariant over its lifetime, no further
413 * locking but only correct reference counting is required.
414 *
415 * Returns:
416 * Zero on success, error code on failure.
417 */
418 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
419 const struct drm_framebuffer_funcs *funcs)
420 {
421 int ret;
422
423 mutex_lock(&dev->mode_config.fb_lock);
424 kref_init(&fb->refcount);
425 INIT_LIST_HEAD(&fb->filp_head);
426 fb->dev = dev;
427 fb->funcs = funcs;
428
429 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
430 if (ret)
431 goto out;
432
433 dev->mode_config.num_fb++;
434 list_add(&fb->head, &dev->mode_config.fb_list);
435 out:
436 mutex_unlock(&dev->mode_config.fb_lock);
437
438 return ret;
439 }
440 EXPORT_SYMBOL(drm_framebuffer_init);
441
442 /* dev->mode_config.fb_lock must be held! */
443 static void __drm_framebuffer_unregister(struct drm_device *dev,
444 struct drm_framebuffer *fb)
445 {
446 mutex_lock(&dev->mode_config.idr_mutex);
447 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
448 mutex_unlock(&dev->mode_config.idr_mutex);
449
450 fb->base.id = 0;
451 }
452
453 static void drm_framebuffer_free(struct kref *kref)
454 {
455 struct drm_framebuffer *fb =
456 container_of(kref, struct drm_framebuffer, refcount);
457 struct drm_device *dev = fb->dev;
458
459 /*
460 * The lookup idr holds a weak reference, which has not necessarily been
461 * removed at this point. Check for that.
462 */
463 mutex_lock(&dev->mode_config.fb_lock);
464 if (fb->base.id) {
465 /* Mark fb as reaped and drop idr ref. */
466 __drm_framebuffer_unregister(dev, fb);
467 }
468 mutex_unlock(&dev->mode_config.fb_lock);
469
470 fb->funcs->destroy(fb);
471 }
472
473 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
474 uint32_t id)
475 {
476 struct drm_mode_object *obj = NULL;
477 struct drm_framebuffer *fb;
478
479 mutex_lock(&dev->mode_config.idr_mutex);
480 obj = idr_find(&dev->mode_config.crtc_idr, id);
481 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
482 fb = NULL;
483 else
484 fb = obj_to_fb(obj);
485 mutex_unlock(&dev->mode_config.idr_mutex);
486
487 return fb;
488 }
489
490 /**
491 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
492 * @dev: drm device
493 * @id: id of the fb object
494 *
495 * If successful, this grabs an additional reference to the framebuffer -
496 * callers need to make sure to eventually unreference the returned framebuffer
497 * again, using @drm_framebuffer_unreference.
498 */
499 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
500 uint32_t id)
501 {
502 struct drm_framebuffer *fb;
503
504 mutex_lock(&dev->mode_config.fb_lock);
505 fb = __drm_framebuffer_lookup(dev, id);
506 if (fb) {
507 if (!kref_get_unless_zero(&fb->refcount))
508 fb = NULL;
509 }
510 mutex_unlock(&dev->mode_config.fb_lock);
511
512 return fb;
513 }
514 EXPORT_SYMBOL(drm_framebuffer_lookup);
515
516 /**
517 * drm_framebuffer_unreference - unref a framebuffer
518 * @fb: framebuffer to unref
519 *
520 * This functions decrements the fb's refcount and frees it if it drops to zero.
521 */
522 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
523 {
524 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, fb->refcount.kr_count);
525 kref_put(&fb->refcount, drm_framebuffer_free);
526 }
527 EXPORT_SYMBOL(drm_framebuffer_unreference);
528
529 /**
530 * drm_framebuffer_reference - incr the fb refcnt
531 * @fb: framebuffer
532 *
533 * This functions increments the fb's refcount.
534 */
535 void drm_framebuffer_reference(struct drm_framebuffer *fb)
536 {
537 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, fb->refcount.kr_count);
538 kref_get(&fb->refcount);
539 }
540 EXPORT_SYMBOL(drm_framebuffer_reference);
541
542 /**
543 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
544 * @fb: fb to unregister
545 *
546 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
547 * those used for fbdev. Note that the caller must hold a reference of it's own,
548 * i.e. the object may not be destroyed through this call (since it'll lead to a
549 * locking inversion).
550 */
551 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
552 {
553 struct drm_device *dev;
554
555 if (!fb)
556 return;
557
558 dev = fb->dev;
559
560 mutex_lock(&dev->mode_config.fb_lock);
561 /* Mark fb as reaped and drop idr ref. */
562 __drm_framebuffer_unregister(dev, fb);
563 mutex_unlock(&dev->mode_config.fb_lock);
564 }
565 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
566
567 /**
568 * drm_framebuffer_cleanup - remove a framebuffer object
569 * @fb: framebuffer to remove
570 *
571 * Cleanup framebuffer. This function is intended to be used from the drivers
572 * ->destroy callback. It can also be used to clean up driver private
573 * framebuffers embedded into a larger structure.
574 *
575 * Note that this function does not remove the fb from active usuage - if it is
576 * still used anywhere, hilarity can ensue since userspace could call getfb on
577 * the id and get back -EINVAL. Obviously no concern at driver unload time.
578 *
579 * Also, the framebuffer will not be removed from the lookup idr - for
580 * user-created framebuffers this will happen in in the rmfb ioctl. For
581 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
582 * drm_framebuffer_unregister_private.
583 */
584 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
585 {
586 struct drm_device *dev = fb->dev;
587
588 mutex_lock(&dev->mode_config.fb_lock);
589 list_del(&fb->head);
590 dev->mode_config.num_fb--;
591 mutex_unlock(&dev->mode_config.fb_lock);
592 }
593 EXPORT_SYMBOL(drm_framebuffer_cleanup);
594
595 /**
596 * drm_framebuffer_remove - remove and unreference a framebuffer object
597 * @fb: framebuffer to remove
598 *
599 * Scans all the CRTCs and planes in @dev's mode_config. If they're
600 * using @fb, removes it, setting it to NULL. Then drops the reference to the
601 * passed-in framebuffer. Might take the modeset locks.
602 *
603 * Note that this function optimizes the cleanup away if the caller holds the
604 * last reference to the framebuffer. It is also guaranteed to not take the
605 * modeset locks in this case.
606 */
607 void drm_framebuffer_remove(struct drm_framebuffer *fb)
608 {
609 struct drm_device *dev;
610 struct drm_crtc *crtc;
611 struct drm_plane *plane;
612 struct drm_mode_set set;
613 int ret;
614
615 if (!fb)
616 return;
617
618 dev = fb->dev;
619
620 WARN_ON(!list_empty(&fb->filp_head));
621
622 /*
623 * drm ABI mandates that we remove any deleted framebuffers from active
624 * useage. But since most sane clients only remove framebuffers they no
625 * longer need, try to optimize this away.
626 *
627 * Since we're holding a reference ourselves, observing a refcount of 1
628 * means that we're the last holder and can skip it. Also, the refcount
629 * can never increase from 1 again, so we don't need any barriers or
630 * locks.
631 *
632 * Note that userspace could try to race with use and instate a new
633 * usage _after_ we've cleared all current ones. End result will be an
634 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
635 * in this manner.
636 */
637 if (!kref_exclusive_p(&fb->refcount)) {
638 drm_modeset_lock_all(dev);
639 /* remove from any CRTC */
640 drm_for_each_crtc(crtc, dev) {
641 if (crtc->primary->fb == fb) {
642 /* should turn off the crtc */
643 memset(&set, 0, sizeof(struct drm_mode_set));
644 set.crtc = crtc;
645 set.fb = NULL;
646 ret = drm_mode_set_config_internal(&set);
647 if (ret)
648 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
649 }
650 }
651
652 drm_for_each_plane(plane, dev) {
653 if (plane->fb == fb)
654 drm_plane_force_disable(plane);
655 }
656 drm_modeset_unlock_all(dev);
657 }
658
659 drm_framebuffer_unreference(fb);
660 }
661 EXPORT_SYMBOL(drm_framebuffer_remove);
662
663 DEFINE_WW_CLASS(crtc_ww_class);
664
665 /**
666 * drm_crtc_init_with_planes - Initialise a new CRTC object with
667 * specified primary and cursor planes.
668 * @dev: DRM device
669 * @crtc: CRTC object to init
670 * @primary: Primary plane for CRTC
671 * @cursor: Cursor plane for CRTC
672 * @funcs: callbacks for the new CRTC
673 *
674 * Inits a new object created as base part of a driver crtc object.
675 *
676 * Returns:
677 * Zero on success, error code on failure.
678 */
679 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
680 struct drm_plane *primary,
681 struct drm_plane *cursor,
682 const struct drm_crtc_funcs *funcs)
683 {
684 struct drm_mode_config *config = &dev->mode_config;
685 int ret;
686
687 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
688 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
689
690 crtc->dev = dev;
691 crtc->funcs = funcs;
692
693 drm_modeset_lock_init(&crtc->mutex);
694 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
695 if (ret)
696 return ret;
697
698 crtc->base.properties = &crtc->properties;
699
700 list_add_tail(&crtc->head, &config->crtc_list);
701 config->num_crtc++;
702
703 crtc->primary = primary;
704 crtc->cursor = cursor;
705 if (primary)
706 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
707 if (cursor)
708 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
709
710 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
711 drm_object_attach_property(&crtc->base, config->prop_active, 0);
712 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
713 }
714
715 return 0;
716 }
717 EXPORT_SYMBOL(drm_crtc_init_with_planes);
718
719 /**
720 * drm_crtc_cleanup - Clean up the core crtc usage
721 * @crtc: CRTC to cleanup
722 *
723 * This function cleans up @crtc and removes it from the DRM mode setting
724 * core. Note that the function does *not* free the crtc structure itself,
725 * this is the responsibility of the caller.
726 */
727 void drm_crtc_cleanup(struct drm_crtc *crtc)
728 {
729 struct drm_device *dev = crtc->dev;
730
731 kfree(crtc->gamma_store);
732 crtc->gamma_store = NULL;
733
734 drm_modeset_lock_fini(&crtc->mutex);
735
736 drm_mode_object_put(dev, &crtc->base);
737 list_del(&crtc->head);
738 dev->mode_config.num_crtc--;
739
740 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
741 if (crtc->state && crtc->funcs->atomic_destroy_state)
742 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
743
744 memset(crtc, 0, sizeof(*crtc));
745 }
746 EXPORT_SYMBOL(drm_crtc_cleanup);
747
748 /**
749 * drm_crtc_index - find the index of a registered CRTC
750 * @crtc: CRTC to find index for
751 *
752 * Given a registered CRTC, return the index of that CRTC within a DRM
753 * device's list of CRTCs.
754 */
755 unsigned int drm_crtc_index(struct drm_crtc *crtc)
756 {
757 unsigned int index = 0;
758 struct drm_crtc *tmp;
759
760 drm_for_each_crtc(tmp, crtc->dev) {
761 if (tmp == crtc)
762 return index;
763
764 index++;
765 }
766
767 BUG();
768 }
769 EXPORT_SYMBOL(drm_crtc_index);
770
771 /*
772 * drm_mode_remove - remove and free a mode
773 * @connector: connector list to modify
774 * @mode: mode to remove
775 *
776 * Remove @mode from @connector's mode list, then free it.
777 */
778 static void drm_mode_remove(struct drm_connector *connector,
779 struct drm_display_mode *mode)
780 {
781 list_del(&mode->head);
782 drm_mode_destroy(connector->dev, mode);
783 }
784
785 /**
786 * drm_display_info_set_bus_formats - set the supported bus formats
787 * @info: display info to store bus formats in
788 * @formats: array containing the supported bus formats
789 * @num_formats: the number of entries in the fmts array
790 *
791 * Store the supported bus formats in display info structure.
792 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
793 * a full list of available formats.
794 */
795 int drm_display_info_set_bus_formats(struct drm_display_info *info,
796 const u32 *formats,
797 unsigned int num_formats)
798 {
799 u32 *fmts = NULL;
800
801 if (!formats && num_formats)
802 return -EINVAL;
803
804 if (formats && num_formats) {
805 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
806 GFP_KERNEL);
807 if (!fmts)
808 return -ENOMEM;
809 }
810
811 kfree(info->bus_formats);
812 info->bus_formats = fmts;
813 info->num_bus_formats = num_formats;
814
815 return 0;
816 }
817 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
818
819 /**
820 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
821 * @connector: connector to quwery
822 *
823 * The kernel supports per-connector configration of its consoles through
824 * use of the video= parameter. This function parses that option and
825 * extracts the user's specified mode (or enable/disable status) for a
826 * particular connector. This is typically only used during the early fbdev
827 * setup.
828 */
829 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
830 {
831 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
832 char *option = NULL;
833
834 #ifdef __NetBSD__
835 prop_dictionary_t prop = device_properties(connector->dev->dev);
836 if (!prop_dictionary_get_cstring(prop, connector->name, &option))
837 return;
838 #else
839 if (fb_get_options(connector->name, &option))
840 return;
841 #endif
842
843 if (!drm_mode_parse_command_line_for_connector(option,
844 connector,
845 mode))
846 return;
847
848 if (mode->force) {
849 const char *s;
850
851 switch (mode->force) {
852 case DRM_FORCE_OFF:
853 s = "OFF";
854 break;
855 case DRM_FORCE_ON_DIGITAL:
856 s = "ON - dig";
857 break;
858 default:
859 case DRM_FORCE_ON:
860 s = "ON";
861 break;
862 }
863
864 DRM_INFO("forcing %s connector %s\n", connector->name, s);
865 connector->force = mode->force;
866 }
867
868 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
869 connector->name,
870 mode->xres, mode->yres,
871 mode->refresh_specified ? mode->refresh : 60,
872 mode->rb ? " reduced blanking" : "",
873 mode->margins ? " with margins" : "",
874 mode->interlace ? " interlaced" : "");
875 }
876
877 /**
878 * drm_connector_init - Init a preallocated connector
879 * @dev: DRM device
880 * @connector: the connector to init
881 * @funcs: callbacks for this connector
882 * @connector_type: user visible type of the connector
883 *
884 * Initialises a preallocated connector. Connectors should be
885 * subclassed as part of driver connector objects.
886 *
887 * Returns:
888 * Zero on success, error code on failure.
889 */
890 int drm_connector_init(struct drm_device *dev,
891 struct drm_connector *connector,
892 const struct drm_connector_funcs *funcs,
893 int connector_type)
894 {
895 struct drm_mode_config *config = &dev->mode_config;
896 int ret;
897 struct ida *connector_ida =
898 &drm_connector_enum_list[connector_type].ida;
899
900 drm_modeset_lock_all(dev);
901
902 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
903 if (ret)
904 goto out_unlock;
905
906 connector->base.properties = &connector->properties;
907 connector->dev = dev;
908 connector->kdev = dev->dev;
909 connector->funcs = funcs;
910 connector->connector_type = connector_type;
911 connector->connector_type_id =
912 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
913 if (connector->connector_type_id < 0) {
914 ret = connector->connector_type_id;
915 goto out_put;
916 }
917 connector->name =
918 kasprintf(GFP_KERNEL, "%s-%d",
919 drm_connector_enum_list[connector_type].name,
920 connector->connector_type_id);
921 if (!connector->name) {
922 ret = -ENOMEM;
923 goto out_put;
924 }
925
926 INIT_LIST_HEAD(&connector->probed_modes);
927 INIT_LIST_HEAD(&connector->modes);
928 connector->edid_blob_ptr = NULL;
929 connector->status = connector_status_unknown;
930
931 drm_connector_get_cmdline_mode(connector);
932
933 /* We should add connectors at the end to avoid upsetting the connector
934 * index too much. */
935 list_add_tail(&connector->head, &config->connector_list);
936 config->num_connector++;
937
938 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
939 drm_object_attach_property(&connector->base,
940 config->edid_property,
941 0);
942
943 drm_object_attach_property(&connector->base,
944 config->dpms_property, 0);
945
946 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
947 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
948 }
949
950 connector->debugfs_entry = NULL;
951
952 out_put:
953 if (ret)
954 drm_mode_object_put(dev, &connector->base);
955
956 out_unlock:
957 drm_modeset_unlock_all(dev);
958
959 return ret;
960 }
961 EXPORT_SYMBOL(drm_connector_init);
962
963 /**
964 * drm_connector_cleanup - cleans up an initialised connector
965 * @connector: connector to cleanup
966 *
967 * Cleans up the connector but doesn't free the object.
968 */
969 void drm_connector_cleanup(struct drm_connector *connector)
970 {
971 struct drm_device *dev = connector->dev;
972 struct drm_display_mode *mode, *t;
973
974 if (connector->tile_group) {
975 drm_mode_put_tile_group(dev, connector->tile_group);
976 connector->tile_group = NULL;
977 }
978
979 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
980 drm_mode_remove(connector, mode);
981
982 list_for_each_entry_safe(mode, t, &connector->modes, head)
983 drm_mode_remove(connector, mode);
984
985 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
986 connector->connector_type_id);
987
988 kfree(connector->display_info.bus_formats);
989 drm_mode_object_put(dev, &connector->base);
990 kfree(connector->name);
991 connector->name = NULL;
992 list_del(&connector->head);
993 dev->mode_config.num_connector--;
994
995 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
996 if (connector->state && connector->funcs->atomic_destroy_state)
997 connector->funcs->atomic_destroy_state(connector,
998 connector->state);
999
1000 memset(connector, 0, sizeof(*connector));
1001 }
1002 EXPORT_SYMBOL(drm_connector_cleanup);
1003
1004 /**
1005 * drm_connector_index - find the index of a registered connector
1006 * @connector: connector to find index for
1007 *
1008 * Given a registered connector, return the index of that connector within a DRM
1009 * device's list of connectors.
1010 */
1011 unsigned int drm_connector_index(struct drm_connector *connector)
1012 {
1013 unsigned int index = 0;
1014 struct drm_connector *tmp;
1015 struct drm_mode_config *config = &connector->dev->mode_config;
1016
1017 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
1018
1019 drm_for_each_connector(tmp, connector->dev) {
1020 if (tmp == connector)
1021 return index;
1022
1023 index++;
1024 }
1025
1026 BUG();
1027 }
1028 EXPORT_SYMBOL(drm_connector_index);
1029
1030 /**
1031 * drm_connector_register - register a connector
1032 * @connector: the connector to register
1033 *
1034 * Register userspace interfaces for a connector
1035 *
1036 * Returns:
1037 * Zero on success, error code on failure.
1038 */
1039 int drm_connector_register(struct drm_connector *connector)
1040 {
1041 #ifndef __NetBSD__ /* XXX sysfs, debugfs */
1042 int ret;
1043 #endif
1044
1045 drm_mode_object_register(connector->dev, &connector->base);
1046
1047 #ifndef __NetBSD__ /* XXX sysfs, debugfs */
1048 ret = drm_sysfs_connector_add(connector);
1049 if (ret)
1050 return ret;
1051
1052 ret = drm_debugfs_connector_add(connector);
1053 if (ret) {
1054 drm_sysfs_connector_remove(connector);
1055 return ret;
1056 }
1057 #endif
1058
1059 return 0;
1060 }
1061 EXPORT_SYMBOL(drm_connector_register);
1062
1063 /**
1064 * drm_connector_unregister - unregister a connector
1065 * @connector: the connector to unregister
1066 *
1067 * Unregister userspace interfaces for a connector
1068 */
1069 void drm_connector_unregister(struct drm_connector *connector)
1070 {
1071 #ifndef __NetBSD__ /* XXX sysfs, debugfs */
1072 drm_sysfs_connector_remove(connector);
1073 drm_debugfs_connector_remove(connector);
1074 #endif
1075 }
1076 EXPORT_SYMBOL(drm_connector_unregister);
1077
1078
1079 /**
1080 * drm_connector_unplug_all - unregister connector userspace interfaces
1081 * @dev: drm device
1082 *
1083 * This function unregisters all connector userspace interfaces in sysfs. Should
1084 * be call when the device is disconnected, e.g. from an usb driver's
1085 * ->disconnect callback.
1086 */
1087 void drm_connector_unplug_all(struct drm_device *dev)
1088 {
1089 struct drm_connector *connector;
1090
1091 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1092 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1093 drm_connector_unregister(connector);
1094
1095 }
1096 EXPORT_SYMBOL(drm_connector_unplug_all);
1097
1098 /**
1099 * drm_encoder_init - Init a preallocated encoder
1100 * @dev: drm device
1101 * @encoder: the encoder to init
1102 * @funcs: callbacks for this encoder
1103 * @encoder_type: user visible type of the encoder
1104 *
1105 * Initialises a preallocated encoder. Encoder should be
1106 * subclassed as part of driver encoder objects.
1107 *
1108 * Returns:
1109 * Zero on success, error code on failure.
1110 */
1111 int drm_encoder_init(struct drm_device *dev,
1112 struct drm_encoder *encoder,
1113 const struct drm_encoder_funcs *funcs,
1114 int encoder_type)
1115 {
1116 int ret;
1117
1118 drm_modeset_lock_all(dev);
1119
1120 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1121 if (ret)
1122 goto out_unlock;
1123
1124 encoder->dev = dev;
1125 encoder->encoder_type = encoder_type;
1126 encoder->funcs = funcs;
1127 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1128 drm_encoder_enum_list[encoder_type].name,
1129 encoder->base.id);
1130 if (!encoder->name) {
1131 ret = -ENOMEM;
1132 goto out_put;
1133 }
1134
1135 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1136 dev->mode_config.num_encoder++;
1137
1138 out_put:
1139 if (ret)
1140 drm_mode_object_put(dev, &encoder->base);
1141
1142 out_unlock:
1143 drm_modeset_unlock_all(dev);
1144
1145 return ret;
1146 }
1147 EXPORT_SYMBOL(drm_encoder_init);
1148
1149 /**
1150 * drm_encoder_cleanup - cleans up an initialised encoder
1151 * @encoder: encoder to cleanup
1152 *
1153 * Cleans up the encoder but doesn't free the object.
1154 */
1155 void drm_encoder_cleanup(struct drm_encoder *encoder)
1156 {
1157 struct drm_device *dev = encoder->dev;
1158
1159 drm_modeset_lock_all(dev);
1160 drm_mode_object_put(dev, &encoder->base);
1161 kfree(encoder->name);
1162 list_del(&encoder->head);
1163 dev->mode_config.num_encoder--;
1164 drm_modeset_unlock_all(dev);
1165
1166 memset(encoder, 0, sizeof(*encoder));
1167 }
1168 EXPORT_SYMBOL(drm_encoder_cleanup);
1169
1170 /**
1171 * drm_universal_plane_init - Initialize a new universal plane object
1172 * @dev: DRM device
1173 * @plane: plane object to init
1174 * @possible_crtcs: bitmask of possible CRTCs
1175 * @funcs: callbacks for the new plane
1176 * @formats: array of supported formats (%DRM_FORMAT_*)
1177 * @format_count: number of elements in @formats
1178 * @type: type of plane (overlay, primary, cursor)
1179 *
1180 * Initializes a plane object of type @type.
1181 *
1182 * Returns:
1183 * Zero on success, error code on failure.
1184 */
1185 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1186 unsigned long possible_crtcs,
1187 const struct drm_plane_funcs *funcs,
1188 const uint32_t *formats, unsigned int format_count,
1189 enum drm_plane_type type)
1190 {
1191 struct drm_mode_config *config = &dev->mode_config;
1192 int ret;
1193
1194 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1195 if (ret)
1196 return ret;
1197
1198 drm_modeset_lock_init(&plane->mutex);
1199
1200 plane->base.properties = &plane->properties;
1201 plane->dev = dev;
1202 plane->funcs = funcs;
1203 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1204 GFP_KERNEL);
1205 if (!plane->format_types) {
1206 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1207 drm_mode_object_put(dev, &plane->base);
1208 return -ENOMEM;
1209 }
1210
1211 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1212 plane->format_count = format_count;
1213 plane->possible_crtcs = possible_crtcs;
1214 plane->type = type;
1215
1216 list_add_tail(&plane->head, &config->plane_list);
1217 config->num_total_plane++;
1218 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1219 config->num_overlay_plane++;
1220
1221 drm_object_attach_property(&plane->base,
1222 config->plane_type_property,
1223 plane->type);
1224
1225 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1226 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1227 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1228 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1229 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1230 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1231 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1232 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1233 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1234 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1235 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1236 }
1237
1238 return 0;
1239 }
1240 EXPORT_SYMBOL(drm_universal_plane_init);
1241
1242 /**
1243 * drm_plane_init - Initialize a legacy plane
1244 * @dev: DRM device
1245 * @plane: plane object to init
1246 * @possible_crtcs: bitmask of possible CRTCs
1247 * @funcs: callbacks for the new plane
1248 * @formats: array of supported formats (%DRM_FORMAT_*)
1249 * @format_count: number of elements in @formats
1250 * @is_primary: plane type (primary vs overlay)
1251 *
1252 * Legacy API to initialize a DRM plane.
1253 *
1254 * New drivers should call drm_universal_plane_init() instead.
1255 *
1256 * Returns:
1257 * Zero on success, error code on failure.
1258 */
1259 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1260 unsigned long possible_crtcs,
1261 const struct drm_plane_funcs *funcs,
1262 const uint32_t *formats, unsigned int format_count,
1263 bool is_primary)
1264 {
1265 enum drm_plane_type type;
1266
1267 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1268 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1269 formats, format_count, type);
1270 }
1271 EXPORT_SYMBOL(drm_plane_init);
1272
1273 /**
1274 * drm_plane_cleanup - Clean up the core plane usage
1275 * @plane: plane to cleanup
1276 *
1277 * This function cleans up @plane and removes it from the DRM mode setting
1278 * core. Note that the function does *not* free the plane structure itself,
1279 * this is the responsibility of the caller.
1280 */
1281 void drm_plane_cleanup(struct drm_plane *plane)
1282 {
1283 struct drm_device *dev = plane->dev;
1284
1285 drm_modeset_lock_all(dev);
1286 kfree(plane->format_types);
1287 drm_mode_object_put(dev, &plane->base);
1288
1289 BUG_ON(list_empty(&plane->head));
1290
1291 list_del(&plane->head);
1292 dev->mode_config.num_total_plane--;
1293 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1294 dev->mode_config.num_overlay_plane--;
1295 drm_modeset_unlock_all(dev);
1296
1297 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1298 if (plane->state && plane->funcs->atomic_destroy_state)
1299 plane->funcs->atomic_destroy_state(plane, plane->state);
1300 drm_modeset_lock_fini(&plane->mutex);
1301
1302 memset(plane, 0, sizeof(*plane));
1303 }
1304 EXPORT_SYMBOL(drm_plane_cleanup);
1305
1306 /**
1307 * drm_plane_index - find the index of a registered plane
1308 * @plane: plane to find index for
1309 *
1310 * Given a registered plane, return the index of that CRTC within a DRM
1311 * device's list of planes.
1312 */
1313 unsigned int drm_plane_index(struct drm_plane *plane)
1314 {
1315 unsigned int index = 0;
1316 struct drm_plane *tmp;
1317
1318 drm_for_each_plane(tmp, plane->dev) {
1319 if (tmp == plane)
1320 return index;
1321
1322 index++;
1323 }
1324
1325 BUG();
1326 }
1327 EXPORT_SYMBOL(drm_plane_index);
1328
1329 /**
1330 * drm_plane_from_index - find the registered plane at an index
1331 * @dev: DRM device
1332 * @idx: index of registered plane to find for
1333 *
1334 * Given a plane index, return the registered plane from DRM device's
1335 * list of planes with matching index.
1336 */
1337 struct drm_plane *
1338 drm_plane_from_index(struct drm_device *dev, int idx)
1339 {
1340 struct drm_plane *plane;
1341 unsigned int i = 0;
1342
1343 drm_for_each_plane(plane, dev) {
1344 if (i == idx)
1345 return plane;
1346 i++;
1347 }
1348 return NULL;
1349 }
1350 EXPORT_SYMBOL(drm_plane_from_index);
1351
1352 /**
1353 * drm_plane_force_disable - Forcibly disable a plane
1354 * @plane: plane to disable
1355 *
1356 * Forces the plane to be disabled.
1357 *
1358 * Used when the plane's current framebuffer is destroyed,
1359 * and when restoring fbdev mode.
1360 */
1361 void drm_plane_force_disable(struct drm_plane *plane)
1362 {
1363 int ret;
1364
1365 if (!plane->fb)
1366 return;
1367
1368 plane->old_fb = plane->fb;
1369 ret = plane->funcs->disable_plane(plane);
1370 if (ret) {
1371 DRM_ERROR("failed to disable plane with busy fb\n");
1372 plane->old_fb = NULL;
1373 return;
1374 }
1375 /* disconnect the plane from the fb and crtc: */
1376 drm_framebuffer_unreference(plane->old_fb);
1377 plane->old_fb = NULL;
1378 plane->fb = NULL;
1379 plane->crtc = NULL;
1380 }
1381 EXPORT_SYMBOL(drm_plane_force_disable);
1382
1383 static int drm_mode_create_standard_properties(struct drm_device *dev)
1384 {
1385 struct drm_property *prop;
1386
1387 /*
1388 * Standard properties (apply to all connectors)
1389 */
1390 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1391 DRM_MODE_PROP_IMMUTABLE,
1392 "EDID", 0);
1393 if (!prop)
1394 return -ENOMEM;
1395 dev->mode_config.edid_property = prop;
1396
1397 prop = drm_property_create_enum(dev, 0,
1398 "DPMS", drm_dpms_enum_list,
1399 ARRAY_SIZE(drm_dpms_enum_list));
1400 if (!prop)
1401 return -ENOMEM;
1402 dev->mode_config.dpms_property = prop;
1403
1404 prop = drm_property_create(dev,
1405 DRM_MODE_PROP_BLOB |
1406 DRM_MODE_PROP_IMMUTABLE,
1407 "PATH", 0);
1408 if (!prop)
1409 return -ENOMEM;
1410 dev->mode_config.path_property = prop;
1411
1412 prop = drm_property_create(dev,
1413 DRM_MODE_PROP_BLOB |
1414 DRM_MODE_PROP_IMMUTABLE,
1415 "TILE", 0);
1416 if (!prop)
1417 return -ENOMEM;
1418 dev->mode_config.tile_property = prop;
1419
1420 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1421 "type", drm_plane_type_enum_list,
1422 ARRAY_SIZE(drm_plane_type_enum_list));
1423 if (!prop)
1424 return -ENOMEM;
1425 dev->mode_config.plane_type_property = prop;
1426
1427 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1428 "SRC_X", 0, UINT_MAX);
1429 if (!prop)
1430 return -ENOMEM;
1431 dev->mode_config.prop_src_x = prop;
1432
1433 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1434 "SRC_Y", 0, UINT_MAX);
1435 if (!prop)
1436 return -ENOMEM;
1437 dev->mode_config.prop_src_y = prop;
1438
1439 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1440 "SRC_W", 0, UINT_MAX);
1441 if (!prop)
1442 return -ENOMEM;
1443 dev->mode_config.prop_src_w = prop;
1444
1445 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1446 "SRC_H", 0, UINT_MAX);
1447 if (!prop)
1448 return -ENOMEM;
1449 dev->mode_config.prop_src_h = prop;
1450
1451 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1452 "CRTC_X", INT_MIN, INT_MAX);
1453 if (!prop)
1454 return -ENOMEM;
1455 dev->mode_config.prop_crtc_x = prop;
1456
1457 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1458 "CRTC_Y", INT_MIN, INT_MAX);
1459 if (!prop)
1460 return -ENOMEM;
1461 dev->mode_config.prop_crtc_y = prop;
1462
1463 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1464 "CRTC_W", 0, INT_MAX);
1465 if (!prop)
1466 return -ENOMEM;
1467 dev->mode_config.prop_crtc_w = prop;
1468
1469 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1470 "CRTC_H", 0, INT_MAX);
1471 if (!prop)
1472 return -ENOMEM;
1473 dev->mode_config.prop_crtc_h = prop;
1474
1475 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1476 "FB_ID", DRM_MODE_OBJECT_FB);
1477 if (!prop)
1478 return -ENOMEM;
1479 dev->mode_config.prop_fb_id = prop;
1480
1481 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1482 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1483 if (!prop)
1484 return -ENOMEM;
1485 dev->mode_config.prop_crtc_id = prop;
1486
1487 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1488 "ACTIVE");
1489 if (!prop)
1490 return -ENOMEM;
1491 dev->mode_config.prop_active = prop;
1492
1493 prop = drm_property_create(dev,
1494 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1495 "MODE_ID", 0);
1496 if (!prop)
1497 return -ENOMEM;
1498 dev->mode_config.prop_mode_id = prop;
1499
1500 return 0;
1501 }
1502
1503 /**
1504 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1505 * @dev: DRM device
1506 *
1507 * Called by a driver the first time a DVI-I connector is made.
1508 */
1509 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1510 {
1511 struct drm_property *dvi_i_selector;
1512 struct drm_property *dvi_i_subconnector;
1513
1514 if (dev->mode_config.dvi_i_select_subconnector_property)
1515 return 0;
1516
1517 dvi_i_selector =
1518 drm_property_create_enum(dev, 0,
1519 "select subconnector",
1520 drm_dvi_i_select_enum_list,
1521 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1522 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1523
1524 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1525 "subconnector",
1526 drm_dvi_i_subconnector_enum_list,
1527 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1528 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1529
1530 return 0;
1531 }
1532 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1533
1534 /**
1535 * drm_create_tv_properties - create TV specific connector properties
1536 * @dev: DRM device
1537 * @num_modes: number of different TV formats (modes) supported
1538 * @modes: array of pointers to strings containing name of each format
1539 *
1540 * Called by a driver's TV initialization routine, this function creates
1541 * the TV specific connector properties for a given device. Caller is
1542 * responsible for allocating a list of format names and passing them to
1543 * this routine.
1544 */
1545 int drm_mode_create_tv_properties(struct drm_device *dev,
1546 unsigned int num_modes,
1547 const char * const modes[])
1548 {
1549 struct drm_property *tv_selector;
1550 struct drm_property *tv_subconnector;
1551 unsigned int i;
1552
1553 if (dev->mode_config.tv_select_subconnector_property)
1554 return 0;
1555
1556 /*
1557 * Basic connector properties
1558 */
1559 tv_selector = drm_property_create_enum(dev, 0,
1560 "select subconnector",
1561 drm_tv_select_enum_list,
1562 ARRAY_SIZE(drm_tv_select_enum_list));
1563 if (!tv_selector)
1564 goto nomem;
1565
1566 dev->mode_config.tv_select_subconnector_property = tv_selector;
1567
1568 tv_subconnector =
1569 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1570 "subconnector",
1571 drm_tv_subconnector_enum_list,
1572 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1573 if (!tv_subconnector)
1574 goto nomem;
1575 dev->mode_config.tv_subconnector_property = tv_subconnector;
1576
1577 /*
1578 * Other, TV specific properties: margins & TV modes.
1579 */
1580 dev->mode_config.tv_left_margin_property =
1581 drm_property_create_range(dev, 0, "left margin", 0, 100);
1582 if (!dev->mode_config.tv_left_margin_property)
1583 goto nomem;
1584
1585 dev->mode_config.tv_right_margin_property =
1586 drm_property_create_range(dev, 0, "right margin", 0, 100);
1587 if (!dev->mode_config.tv_right_margin_property)
1588 goto nomem;
1589
1590 dev->mode_config.tv_top_margin_property =
1591 drm_property_create_range(dev, 0, "top margin", 0, 100);
1592 if (!dev->mode_config.tv_top_margin_property)
1593 goto nomem;
1594
1595 dev->mode_config.tv_bottom_margin_property =
1596 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1597 if (!dev->mode_config.tv_bottom_margin_property)
1598 goto nomem;
1599
1600 dev->mode_config.tv_mode_property =
1601 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1602 "mode", num_modes);
1603 if (!dev->mode_config.tv_mode_property)
1604 goto nomem;
1605
1606 for (i = 0; i < num_modes; i++)
1607 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1608 i, modes[i]);
1609
1610 dev->mode_config.tv_brightness_property =
1611 drm_property_create_range(dev, 0, "brightness", 0, 100);
1612 if (!dev->mode_config.tv_brightness_property)
1613 goto nomem;
1614
1615 dev->mode_config.tv_contrast_property =
1616 drm_property_create_range(dev, 0, "contrast", 0, 100);
1617 if (!dev->mode_config.tv_contrast_property)
1618 goto nomem;
1619
1620 dev->mode_config.tv_flicker_reduction_property =
1621 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1622 if (!dev->mode_config.tv_flicker_reduction_property)
1623 goto nomem;
1624
1625 dev->mode_config.tv_overscan_property =
1626 drm_property_create_range(dev, 0, "overscan", 0, 100);
1627 if (!dev->mode_config.tv_overscan_property)
1628 goto nomem;
1629
1630 dev->mode_config.tv_saturation_property =
1631 drm_property_create_range(dev, 0, "saturation", 0, 100);
1632 if (!dev->mode_config.tv_saturation_property)
1633 goto nomem;
1634
1635 dev->mode_config.tv_hue_property =
1636 drm_property_create_range(dev, 0, "hue", 0, 100);
1637 if (!dev->mode_config.tv_hue_property)
1638 goto nomem;
1639
1640 return 0;
1641 nomem:
1642 return -ENOMEM;
1643 }
1644 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1645
1646 /**
1647 * drm_mode_create_scaling_mode_property - create scaling mode property
1648 * @dev: DRM device
1649 *
1650 * Called by a driver the first time it's needed, must be attached to desired
1651 * connectors.
1652 */
1653 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1654 {
1655 struct drm_property *scaling_mode;
1656
1657 if (dev->mode_config.scaling_mode_property)
1658 return 0;
1659
1660 scaling_mode =
1661 drm_property_create_enum(dev, 0, "scaling mode",
1662 drm_scaling_mode_enum_list,
1663 ARRAY_SIZE(drm_scaling_mode_enum_list));
1664
1665 dev->mode_config.scaling_mode_property = scaling_mode;
1666
1667 return 0;
1668 }
1669 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1670
1671 /**
1672 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1673 * @dev: DRM device
1674 *
1675 * Called by a driver the first time it's needed, must be attached to desired
1676 * connectors.
1677 *
1678 * Returns:
1679 * Zero on success, negative errno on failure.
1680 */
1681 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1682 {
1683 if (dev->mode_config.aspect_ratio_property)
1684 return 0;
1685
1686 dev->mode_config.aspect_ratio_property =
1687 drm_property_create_enum(dev, 0, "aspect ratio",
1688 drm_aspect_ratio_enum_list,
1689 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1690
1691 if (dev->mode_config.aspect_ratio_property == NULL)
1692 return -ENOMEM;
1693
1694 return 0;
1695 }
1696 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1697
1698 /**
1699 * drm_mode_create_dirty_property - create dirty property
1700 * @dev: DRM device
1701 *
1702 * Called by a driver the first time it's needed, must be attached to desired
1703 * connectors.
1704 */
1705 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1706 {
1707 struct drm_property *dirty_info;
1708
1709 if (dev->mode_config.dirty_info_property)
1710 return 0;
1711
1712 dirty_info =
1713 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1714 "dirty",
1715 drm_dirty_info_enum_list,
1716 ARRAY_SIZE(drm_dirty_info_enum_list));
1717 dev->mode_config.dirty_info_property = dirty_info;
1718
1719 return 0;
1720 }
1721 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1722
1723 /**
1724 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1725 * @dev: DRM device
1726 *
1727 * Create the the suggested x/y offset property for connectors.
1728 */
1729 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1730 {
1731 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1732 return 0;
1733
1734 dev->mode_config.suggested_x_property =
1735 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1736
1737 dev->mode_config.suggested_y_property =
1738 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1739
1740 if (dev->mode_config.suggested_x_property == NULL ||
1741 dev->mode_config.suggested_y_property == NULL)
1742 return -ENOMEM;
1743 return 0;
1744 }
1745 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1746
1747 /**
1748 * drm_mode_getresources - get graphics configuration
1749 * @dev: drm device for the ioctl
1750 * @data: data pointer for the ioctl
1751 * @file_priv: drm file for the ioctl call
1752 *
1753 * Construct a set of configuration description structures and return
1754 * them to the user, including CRTC, connector and framebuffer configuration.
1755 *
1756 * Called by the user via ioctl.
1757 *
1758 * Returns:
1759 * Zero on success, negative errno on failure.
1760 */
1761 int drm_mode_getresources(struct drm_device *dev, void *data,
1762 struct drm_file *file_priv)
1763 {
1764 struct drm_mode_card_res *card_res = data;
1765 struct list_head *lh;
1766 struct drm_framebuffer *fb;
1767 struct drm_connector *connector;
1768 struct drm_crtc *crtc;
1769 struct drm_encoder *encoder;
1770 int ret = 0;
1771 int connector_count = 0;
1772 int crtc_count = 0;
1773 int fb_count = 0;
1774 int encoder_count = 0;
1775 int copied = 0;
1776 uint32_t __user *fb_id;
1777 uint32_t __user *crtc_id;
1778 uint32_t __user *connector_id;
1779 uint32_t __user *encoder_id;
1780
1781 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1782 return -EINVAL;
1783
1784
1785 mutex_lock(&file_priv->fbs_lock);
1786 /*
1787 * For the non-control nodes we need to limit the list of resources
1788 * by IDs in the group list for this node
1789 */
1790 list_for_each(lh, &file_priv->fbs)
1791 fb_count++;
1792
1793 /* handle this in 4 parts */
1794 /* FBs */
1795 if (card_res->count_fbs >= fb_count) {
1796 copied = 0;
1797 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1798 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1799 if (put_user(fb->base.id, fb_id + copied)) {
1800 mutex_unlock(&file_priv->fbs_lock);
1801 return -EFAULT;
1802 }
1803 copied++;
1804 }
1805 }
1806 card_res->count_fbs = fb_count;
1807 mutex_unlock(&file_priv->fbs_lock);
1808
1809 /* mode_config.mutex protects the connector list against e.g. DP MST
1810 * connector hot-adding. CRTC/Plane lists are invariant. */
1811 mutex_lock(&dev->mode_config.mutex);
1812 drm_for_each_crtc(crtc, dev)
1813 crtc_count++;
1814
1815 drm_for_each_connector(connector, dev)
1816 connector_count++;
1817
1818 drm_for_each_encoder(encoder, dev)
1819 encoder_count++;
1820
1821 card_res->max_height = dev->mode_config.max_height;
1822 card_res->min_height = dev->mode_config.min_height;
1823 card_res->max_width = dev->mode_config.max_width;
1824 card_res->min_width = dev->mode_config.min_width;
1825
1826 /* CRTCs */
1827 if (card_res->count_crtcs >= crtc_count) {
1828 copied = 0;
1829 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1830 drm_for_each_crtc(crtc, dev) {
1831 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1832 if (put_user(crtc->base.id, crtc_id + copied)) {
1833 ret = -EFAULT;
1834 goto out;
1835 }
1836 copied++;
1837 }
1838 }
1839 card_res->count_crtcs = crtc_count;
1840
1841 /* Encoders */
1842 if (card_res->count_encoders >= encoder_count) {
1843 copied = 0;
1844 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1845 drm_for_each_encoder(encoder, dev) {
1846 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1847 encoder->name);
1848 if (put_user(encoder->base.id, encoder_id +
1849 copied)) {
1850 ret = -EFAULT;
1851 goto out;
1852 }
1853 copied++;
1854 }
1855 }
1856 card_res->count_encoders = encoder_count;
1857
1858 /* Connectors */
1859 if (card_res->count_connectors >= connector_count) {
1860 copied = 0;
1861 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1862 drm_for_each_connector(connector, dev) {
1863 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1864 connector->base.id,
1865 connector->name);
1866 if (put_user(connector->base.id,
1867 connector_id + copied)) {
1868 ret = -EFAULT;
1869 goto out;
1870 }
1871 copied++;
1872 }
1873 }
1874 card_res->count_connectors = connector_count;
1875
1876 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1877 card_res->count_connectors, card_res->count_encoders);
1878
1879 out:
1880 mutex_unlock(&dev->mode_config.mutex);
1881 return ret;
1882 }
1883
1884 /**
1885 * drm_mode_getcrtc - get CRTC configuration
1886 * @dev: drm device for the ioctl
1887 * @data: data pointer for the ioctl
1888 * @file_priv: drm file for the ioctl call
1889 *
1890 * Construct a CRTC configuration structure to return to the user.
1891 *
1892 * Called by the user via ioctl.
1893 *
1894 * Returns:
1895 * Zero on success, negative errno on failure.
1896 */
1897 int drm_mode_getcrtc(struct drm_device *dev,
1898 void *data, struct drm_file *file_priv)
1899 {
1900 struct drm_mode_crtc *crtc_resp = data;
1901 struct drm_crtc *crtc;
1902
1903 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1904 return -EINVAL;
1905
1906 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1907 if (!crtc)
1908 return -ENOENT;
1909
1910 drm_modeset_lock_crtc(crtc, crtc->primary);
1911 crtc_resp->gamma_size = crtc->gamma_size;
1912 if (crtc->primary->fb)
1913 crtc_resp->fb_id = crtc->primary->fb->base.id;
1914 else
1915 crtc_resp->fb_id = 0;
1916
1917 if (crtc->state) {
1918 crtc_resp->x = crtc->primary->state->src_x >> 16;
1919 crtc_resp->y = crtc->primary->state->src_y >> 16;
1920 if (crtc->state->enable) {
1921 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
1922 crtc_resp->mode_valid = 1;
1923
1924 } else {
1925 crtc_resp->mode_valid = 0;
1926 }
1927 } else {
1928 crtc_resp->x = crtc->x;
1929 crtc_resp->y = crtc->y;
1930 if (crtc->enabled) {
1931 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1932 crtc_resp->mode_valid = 1;
1933
1934 } else {
1935 crtc_resp->mode_valid = 0;
1936 }
1937 }
1938 drm_modeset_unlock_crtc(crtc);
1939
1940 return 0;
1941 }
1942
1943 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1944 const struct drm_file *file_priv)
1945 {
1946 /*
1947 * If user-space hasn't configured the driver to expose the stereo 3D
1948 * modes, don't expose them.
1949 */
1950 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1951 return false;
1952
1953 return true;
1954 }
1955
1956 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1957 {
1958 /* For atomic drivers only state objects are synchronously updated and
1959 * protected by modeset locks, so check those first. */
1960 if (connector->state)
1961 return connector->state->best_encoder;
1962 return connector->encoder;
1963 }
1964
1965 /* helper for getconnector and getproperties ioctls */
1966 static int get_properties(struct drm_mode_object *obj, bool atomic,
1967 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
1968 uint32_t *arg_count_props)
1969 {
1970 int props_count;
1971 int i, ret, copied;
1972
1973 props_count = obj->properties->count;
1974 if (!atomic)
1975 props_count -= obj->properties->atomic_count;
1976
1977 if ((*arg_count_props >= props_count) && props_count) {
1978 for (i = 0, copied = 0; copied < props_count; i++) {
1979 struct drm_property *prop = obj->properties->properties[i];
1980 uint64_t val;
1981
1982 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
1983 continue;
1984
1985 ret = drm_object_property_get_value(obj, prop, &val);
1986 if (ret)
1987 return ret;
1988
1989 if (put_user(prop->base.id, prop_ptr + copied))
1990 return -EFAULT;
1991
1992 if (put_user(val, prop_values + copied))
1993 return -EFAULT;
1994
1995 copied++;
1996 }
1997 }
1998 *arg_count_props = props_count;
1999
2000 return 0;
2001 }
2002
2003 /**
2004 * drm_mode_getconnector - get connector configuration
2005 * @dev: drm device for the ioctl
2006 * @data: data pointer for the ioctl
2007 * @file_priv: drm file for the ioctl call
2008 *
2009 * Construct a connector configuration structure to return to the user.
2010 *
2011 * Called by the user via ioctl.
2012 *
2013 * Returns:
2014 * Zero on success, negative errno on failure.
2015 */
2016 int drm_mode_getconnector(struct drm_device *dev, void *data,
2017 struct drm_file *file_priv)
2018 {
2019 struct drm_mode_get_connector *out_resp = data;
2020 struct drm_connector *connector;
2021 struct drm_encoder *encoder;
2022 struct drm_display_mode *mode;
2023 int mode_count = 0;
2024 int encoders_count = 0;
2025 int ret = 0;
2026 int copied = 0;
2027 int i;
2028 struct drm_mode_modeinfo u_mode;
2029 struct drm_mode_modeinfo __user *mode_ptr;
2030 uint32_t __user *encoder_ptr;
2031
2032 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2033 return -EINVAL;
2034
2035 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2036
2037 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
2038
2039 mutex_lock(&dev->mode_config.mutex);
2040
2041 connector = drm_connector_find(dev, out_resp->connector_id);
2042 if (!connector) {
2043 ret = -ENOENT;
2044 goto out_unlock;
2045 }
2046
2047 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2048 if (connector->encoder_ids[i] != 0)
2049 encoders_count++;
2050
2051 if (out_resp->count_modes == 0) {
2052 connector->funcs->fill_modes(connector,
2053 dev->mode_config.max_width,
2054 dev->mode_config.max_height);
2055 }
2056
2057 /* delayed so we get modes regardless of pre-fill_modes state */
2058 list_for_each_entry(mode, &connector->modes, head)
2059 if (drm_mode_expose_to_userspace(mode, file_priv))
2060 mode_count++;
2061
2062 out_resp->connector_id = connector->base.id;
2063 out_resp->connector_type = connector->connector_type;
2064 out_resp->connector_type_id = connector->connector_type_id;
2065 out_resp->mm_width = connector->display_info.width_mm;
2066 out_resp->mm_height = connector->display_info.height_mm;
2067 out_resp->subpixel = connector->display_info.subpixel_order;
2068 out_resp->connection = connector->status;
2069
2070 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2071 encoder = drm_connector_get_encoder(connector);
2072 if (encoder)
2073 out_resp->encoder_id = encoder->base.id;
2074 else
2075 out_resp->encoder_id = 0;
2076
2077 /*
2078 * This ioctl is called twice, once to determine how much space is
2079 * needed, and the 2nd time to fill it.
2080 */
2081 if ((out_resp->count_modes >= mode_count) && mode_count) {
2082 copied = 0;
2083 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2084 list_for_each_entry(mode, &connector->modes, head) {
2085 if (!drm_mode_expose_to_userspace(mode, file_priv))
2086 continue;
2087
2088 drm_mode_convert_to_umode(&u_mode, mode);
2089 if (copy_to_user(mode_ptr + copied,
2090 &u_mode, sizeof(u_mode))) {
2091 ret = -EFAULT;
2092 goto out;
2093 }
2094 copied++;
2095 }
2096 }
2097 out_resp->count_modes = mode_count;
2098
2099 ret = get_properties(&connector->base, file_priv->atomic,
2100 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2101 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2102 &out_resp->count_props);
2103 if (ret)
2104 goto out;
2105
2106 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2107 copied = 0;
2108 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2109 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2110 if (connector->encoder_ids[i] != 0) {
2111 if (put_user(connector->encoder_ids[i],
2112 encoder_ptr + copied)) {
2113 ret = -EFAULT;
2114 goto out;
2115 }
2116 copied++;
2117 }
2118 }
2119 }
2120 out_resp->count_encoders = encoders_count;
2121
2122 out:
2123 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2124
2125 out_unlock:
2126 mutex_unlock(&dev->mode_config.mutex);
2127
2128 return ret;
2129 }
2130
2131 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2132 {
2133 struct drm_connector *connector;
2134 struct drm_device *dev = encoder->dev;
2135 bool uses_atomic = false;
2136
2137 /* For atomic drivers only state objects are synchronously updated and
2138 * protected by modeset locks, so check those first. */
2139 drm_for_each_connector(connector, dev) {
2140 if (!connector->state)
2141 continue;
2142
2143 uses_atomic = true;
2144
2145 if (connector->state->best_encoder != encoder)
2146 continue;
2147
2148 return connector->state->crtc;
2149 }
2150
2151 /* Don't return stale data (e.g. pending async disable). */
2152 if (uses_atomic)
2153 return NULL;
2154
2155 return encoder->crtc;
2156 }
2157
2158 /**
2159 * drm_mode_getencoder - get encoder configuration
2160 * @dev: drm device for the ioctl
2161 * @data: data pointer for the ioctl
2162 * @file_priv: drm file for the ioctl call
2163 *
2164 * Construct a encoder configuration structure to return to the user.
2165 *
2166 * Called by the user via ioctl.
2167 *
2168 * Returns:
2169 * Zero on success, negative errno on failure.
2170 */
2171 int drm_mode_getencoder(struct drm_device *dev, void *data,
2172 struct drm_file *file_priv)
2173 {
2174 struct drm_mode_get_encoder *enc_resp = data;
2175 struct drm_encoder *encoder;
2176 struct drm_crtc *crtc;
2177
2178 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2179 return -EINVAL;
2180
2181 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2182 if (!encoder)
2183 return -ENOENT;
2184
2185 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2186 crtc = drm_encoder_get_crtc(encoder);
2187 if (crtc)
2188 enc_resp->crtc_id = crtc->base.id;
2189 else
2190 enc_resp->crtc_id = 0;
2191 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2192
2193 enc_resp->encoder_type = encoder->encoder_type;
2194 enc_resp->encoder_id = encoder->base.id;
2195 enc_resp->possible_crtcs = encoder->possible_crtcs;
2196 enc_resp->possible_clones = encoder->possible_clones;
2197
2198 return 0;
2199 }
2200
2201 /**
2202 * drm_mode_getplane_res - enumerate all plane resources
2203 * @dev: DRM device
2204 * @data: ioctl data
2205 * @file_priv: DRM file info
2206 *
2207 * Construct a list of plane ids to return to the user.
2208 *
2209 * Called by the user via ioctl.
2210 *
2211 * Returns:
2212 * Zero on success, negative errno on failure.
2213 */
2214 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2215 struct drm_file *file_priv)
2216 {
2217 struct drm_mode_get_plane_res *plane_resp = data;
2218 struct drm_mode_config *config;
2219 struct drm_plane *plane;
2220 uint32_t __user *plane_ptr;
2221 int copied = 0;
2222 unsigned num_planes;
2223
2224 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2225 return -EINVAL;
2226
2227 config = &dev->mode_config;
2228
2229 if (file_priv->universal_planes)
2230 num_planes = config->num_total_plane;
2231 else
2232 num_planes = config->num_overlay_plane;
2233
2234 /*
2235 * This ioctl is called twice, once to determine how much space is
2236 * needed, and the 2nd time to fill it.
2237 */
2238 if (num_planes &&
2239 (plane_resp->count_planes >= num_planes)) {
2240 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2241
2242 /* Plane lists are invariant, no locking needed. */
2243 drm_for_each_plane(plane, dev) {
2244 /*
2245 * Unless userspace set the 'universal planes'
2246 * capability bit, only advertise overlays.
2247 */
2248 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2249 !file_priv->universal_planes)
2250 continue;
2251
2252 if (put_user(plane->base.id, plane_ptr + copied))
2253 return -EFAULT;
2254 copied++;
2255 }
2256 }
2257 plane_resp->count_planes = num_planes;
2258
2259 return 0;
2260 }
2261
2262 /**
2263 * drm_mode_getplane - get plane configuration
2264 * @dev: DRM device
2265 * @data: ioctl data
2266 * @file_priv: DRM file info
2267 *
2268 * Construct a plane configuration structure to return to the user.
2269 *
2270 * Called by the user via ioctl.
2271 *
2272 * Returns:
2273 * Zero on success, negative errno on failure.
2274 */
2275 int drm_mode_getplane(struct drm_device *dev, void *data,
2276 struct drm_file *file_priv)
2277 {
2278 struct drm_mode_get_plane *plane_resp = data;
2279 struct drm_plane *plane;
2280 uint32_t __user *format_ptr;
2281
2282 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2283 return -EINVAL;
2284
2285 plane = drm_plane_find(dev, plane_resp->plane_id);
2286 if (!plane)
2287 return -ENOENT;
2288
2289 drm_modeset_lock(&plane->mutex, NULL);
2290 if (plane->crtc)
2291 plane_resp->crtc_id = plane->crtc->base.id;
2292 else
2293 plane_resp->crtc_id = 0;
2294
2295 if (plane->fb)
2296 plane_resp->fb_id = plane->fb->base.id;
2297 else
2298 plane_resp->fb_id = 0;
2299 drm_modeset_unlock(&plane->mutex);
2300
2301 plane_resp->plane_id = plane->base.id;
2302 plane_resp->possible_crtcs = plane->possible_crtcs;
2303 plane_resp->gamma_size = 0;
2304
2305 /*
2306 * This ioctl is called twice, once to determine how much space is
2307 * needed, and the 2nd time to fill it.
2308 */
2309 if (plane->format_count &&
2310 (plane_resp->count_format_types >= plane->format_count)) {
2311 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2312 if (copy_to_user(format_ptr,
2313 plane->format_types,
2314 sizeof(uint32_t) * plane->format_count)) {
2315 return -EFAULT;
2316 }
2317 }
2318 plane_resp->count_format_types = plane->format_count;
2319
2320 return 0;
2321 }
2322
2323 /**
2324 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2325 * @plane: plane to check for format support
2326 * @format: the pixel format
2327 *
2328 * Returns:
2329 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2330 * otherwise.
2331 */
2332 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2333 {
2334 unsigned int i;
2335
2336 for (i = 0; i < plane->format_count; i++) {
2337 if (format == plane->format_types[i])
2338 return 0;
2339 }
2340
2341 return -EINVAL;
2342 }
2343
2344 static int check_src_coords(uint32_t src_x, uint32_t src_y,
2345 uint32_t src_w, uint32_t src_h,
2346 const struct drm_framebuffer *fb)
2347 {
2348 unsigned int fb_width, fb_height;
2349
2350 fb_width = fb->width << 16;
2351 fb_height = fb->height << 16;
2352
2353 /* Make sure source coordinates are inside the fb. */
2354 if (src_w > fb_width ||
2355 src_x > fb_width - src_w ||
2356 src_h > fb_height ||
2357 src_y > fb_height - src_h) {
2358 DRM_DEBUG_KMS("Invalid source coordinates "
2359 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2360 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2361 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2362 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2363 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2364 return -ENOSPC;
2365 }
2366
2367 return 0;
2368 }
2369
2370 /*
2371 * setplane_internal - setplane handler for internal callers
2372 *
2373 * Note that we assume an extra reference has already been taken on fb. If the
2374 * update fails, this reference will be dropped before return; if it succeeds,
2375 * the previous framebuffer (if any) will be unreferenced instead.
2376 *
2377 * src_{x,y,w,h} are provided in 16.16 fixed point format
2378 */
2379 static int __setplane_internal(struct drm_plane *plane,
2380 struct drm_crtc *crtc,
2381 struct drm_framebuffer *fb,
2382 int32_t crtc_x, int32_t crtc_y,
2383 uint32_t crtc_w, uint32_t crtc_h,
2384 /* src_{x,y,w,h} values are 16.16 fixed point */
2385 uint32_t src_x, uint32_t src_y,
2386 uint32_t src_w, uint32_t src_h)
2387 {
2388 int ret = 0;
2389
2390 /* No fb means shut it down */
2391 if (!fb) {
2392 plane->old_fb = plane->fb;
2393 ret = plane->funcs->disable_plane(plane);
2394 if (!ret) {
2395 plane->crtc = NULL;
2396 plane->fb = NULL;
2397 } else {
2398 plane->old_fb = NULL;
2399 }
2400 goto out;
2401 }
2402
2403 /* Check whether this plane is usable on this CRTC */
2404 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2405 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2406 ret = -EINVAL;
2407 goto out;
2408 }
2409
2410 /* Check whether this plane supports the fb pixel format. */
2411 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2412 if (ret) {
2413 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2414 drm_get_format_name(fb->pixel_format));
2415 goto out;
2416 }
2417
2418 /* Give drivers some help against integer overflows */
2419 if (crtc_w > INT_MAX ||
2420 crtc_x > INT_MAX - (int32_t) crtc_w ||
2421 crtc_h > INT_MAX ||
2422 crtc_y > INT_MAX - (int32_t) crtc_h) {
2423 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2424 crtc_w, crtc_h, crtc_x, crtc_y);
2425 ret = -ERANGE;
2426 goto out;
2427 }
2428
2429 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2430 if (ret)
2431 goto out;
2432
2433 plane->old_fb = plane->fb;
2434 ret = plane->funcs->update_plane(plane, crtc, fb,
2435 crtc_x, crtc_y, crtc_w, crtc_h,
2436 src_x, src_y, src_w, src_h);
2437 if (!ret) {
2438 plane->crtc = crtc;
2439 plane->fb = fb;
2440 fb = NULL;
2441 } else {
2442 plane->old_fb = NULL;
2443 }
2444
2445 out:
2446 if (fb)
2447 drm_framebuffer_unreference(fb);
2448 if (plane->old_fb)
2449 drm_framebuffer_unreference(plane->old_fb);
2450 plane->old_fb = NULL;
2451
2452 return ret;
2453 }
2454
2455 static int setplane_internal(struct drm_plane *plane,
2456 struct drm_crtc *crtc,
2457 struct drm_framebuffer *fb,
2458 int32_t crtc_x, int32_t crtc_y,
2459 uint32_t crtc_w, uint32_t crtc_h,
2460 /* src_{x,y,w,h} values are 16.16 fixed point */
2461 uint32_t src_x, uint32_t src_y,
2462 uint32_t src_w, uint32_t src_h)
2463 {
2464 int ret;
2465
2466 drm_modeset_lock_all(plane->dev);
2467 ret = __setplane_internal(plane, crtc, fb,
2468 crtc_x, crtc_y, crtc_w, crtc_h,
2469 src_x, src_y, src_w, src_h);
2470 drm_modeset_unlock_all(plane->dev);
2471
2472 return ret;
2473 }
2474
2475 /**
2476 * drm_mode_setplane - configure a plane's configuration
2477 * @dev: DRM device
2478 * @data: ioctl data*
2479 * @file_priv: DRM file info
2480 *
2481 * Set plane configuration, including placement, fb, scaling, and other factors.
2482 * Or pass a NULL fb to disable (planes may be disabled without providing a
2483 * valid crtc).
2484 *
2485 * Returns:
2486 * Zero on success, negative errno on failure.
2487 */
2488 int drm_mode_setplane(struct drm_device *dev, void *data,
2489 struct drm_file *file_priv)
2490 {
2491 struct drm_mode_set_plane *plane_req = data;
2492 struct drm_plane *plane;
2493 struct drm_crtc *crtc = NULL;
2494 struct drm_framebuffer *fb = NULL;
2495
2496 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2497 return -EINVAL;
2498
2499 /*
2500 * First, find the plane, crtc, and fb objects. If not available,
2501 * we don't bother to call the driver.
2502 */
2503 plane = drm_plane_find(dev, plane_req->plane_id);
2504 if (!plane) {
2505 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2506 plane_req->plane_id);
2507 return -ENOENT;
2508 }
2509
2510 if (plane_req->fb_id) {
2511 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2512 if (!fb) {
2513 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2514 plane_req->fb_id);
2515 return -ENOENT;
2516 }
2517
2518 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2519 if (!crtc) {
2520 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2521 plane_req->crtc_id);
2522 return -ENOENT;
2523 }
2524 }
2525
2526 /*
2527 * setplane_internal will take care of deref'ing either the old or new
2528 * framebuffer depending on success.
2529 */
2530 return setplane_internal(plane, crtc, fb,
2531 plane_req->crtc_x, plane_req->crtc_y,
2532 plane_req->crtc_w, plane_req->crtc_h,
2533 plane_req->src_x, plane_req->src_y,
2534 plane_req->src_w, plane_req->src_h);
2535 }
2536
2537 /**
2538 * drm_mode_set_config_internal - helper to call ->set_config
2539 * @set: modeset config to set
2540 *
2541 * This is a little helper to wrap internal calls to the ->set_config driver
2542 * interface. The only thing it adds is correct refcounting dance.
2543 *
2544 * Returns:
2545 * Zero on success, negative errno on failure.
2546 */
2547 int drm_mode_set_config_internal(struct drm_mode_set *set)
2548 {
2549 struct drm_crtc *crtc = set->crtc;
2550 struct drm_framebuffer *fb;
2551 struct drm_crtc *tmp;
2552 int ret;
2553
2554 /*
2555 * NOTE: ->set_config can also disable other crtcs (if we steal all
2556 * connectors from it), hence we need to refcount the fbs across all
2557 * crtcs. Atomic modeset will have saner semantics ...
2558 */
2559 drm_for_each_crtc(tmp, crtc->dev)
2560 tmp->primary->old_fb = tmp->primary->fb;
2561
2562 fb = set->fb;
2563
2564 ret = crtc->funcs->set_config(set);
2565 if (ret == 0) {
2566 crtc->primary->crtc = crtc;
2567 crtc->primary->fb = fb;
2568 }
2569
2570 drm_for_each_crtc(tmp, crtc->dev) {
2571 if (tmp->primary->fb)
2572 drm_framebuffer_reference(tmp->primary->fb);
2573 if (tmp->primary->old_fb)
2574 drm_framebuffer_unreference(tmp->primary->old_fb);
2575 tmp->primary->old_fb = NULL;
2576 }
2577
2578 return ret;
2579 }
2580 EXPORT_SYMBOL(drm_mode_set_config_internal);
2581
2582 /**
2583 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2584 * @mode: mode to query
2585 * @hdisplay: hdisplay value to fill in
2586 * @vdisplay: vdisplay value to fill in
2587 *
2588 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2589 * the appropriate layout.
2590 */
2591 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2592 int *hdisplay, int *vdisplay)
2593 {
2594 struct drm_display_mode adjusted;
2595
2596 drm_mode_copy(&adjusted, mode);
2597 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2598 *hdisplay = adjusted.crtc_hdisplay;
2599 *vdisplay = adjusted.crtc_vdisplay;
2600 }
2601 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2602
2603 /**
2604 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2605 * CRTC viewport
2606 * @crtc: CRTC that framebuffer will be displayed on
2607 * @x: x panning
2608 * @y: y panning
2609 * @mode: mode that framebuffer will be displayed under
2610 * @fb: framebuffer to check size of
2611 */
2612 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2613 int x, int y,
2614 const struct drm_display_mode *mode,
2615 const struct drm_framebuffer *fb)
2616
2617 {
2618 int hdisplay, vdisplay;
2619
2620 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
2621
2622 if (crtc->state &&
2623 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2624 BIT(DRM_ROTATE_270)))
2625 swap(hdisplay, vdisplay);
2626
2627 return check_src_coords(x << 16, y << 16,
2628 hdisplay << 16, vdisplay << 16, fb);
2629 }
2630 EXPORT_SYMBOL(drm_crtc_check_viewport);
2631
2632 /**
2633 * drm_mode_setcrtc - set CRTC configuration
2634 * @dev: drm device for the ioctl
2635 * @data: data pointer for the ioctl
2636 * @file_priv: drm file for the ioctl call
2637 *
2638 * Build a new CRTC configuration based on user request.
2639 *
2640 * Called by the user via ioctl.
2641 *
2642 * Returns:
2643 * Zero on success, negative errno on failure.
2644 */
2645 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2646 struct drm_file *file_priv)
2647 {
2648 struct drm_mode_config *config = &dev->mode_config;
2649 struct drm_mode_crtc *crtc_req = data;
2650 struct drm_crtc *crtc;
2651 struct drm_connector **connector_set = NULL, *connector;
2652 struct drm_framebuffer *fb = NULL;
2653 struct drm_display_mode *mode = NULL;
2654 struct drm_mode_set set;
2655 uint32_t __user *set_connectors_ptr;
2656 int ret;
2657 int i;
2658
2659 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2660 return -EINVAL;
2661
2662 /*
2663 * Universal plane src offsets are only 16.16, prevent havoc for
2664 * drivers using universal plane code internally.
2665 */
2666 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
2667 return -ERANGE;
2668
2669 drm_modeset_lock_all(dev);
2670 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2671 if (!crtc) {
2672 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2673 ret = -ENOENT;
2674 goto out;
2675 }
2676 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2677
2678 if (crtc_req->mode_valid) {
2679 /* If we have a mode we need a framebuffer. */
2680 /* If we pass -1, set the mode with the currently bound fb */
2681 if (crtc_req->fb_id == -1) {
2682 if (!crtc->primary->fb) {
2683 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2684 ret = -EINVAL;
2685 goto out;
2686 }
2687 fb = crtc->primary->fb;
2688 /* Make refcounting symmetric with the lookup path. */
2689 drm_framebuffer_reference(fb);
2690 } else {
2691 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2692 if (!fb) {
2693 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2694 crtc_req->fb_id);
2695 ret = -ENOENT;
2696 goto out;
2697 }
2698 }
2699
2700 mode = drm_mode_create(dev);
2701 if (!mode) {
2702 ret = -ENOMEM;
2703 goto out;
2704 }
2705
2706 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
2707 if (ret) {
2708 DRM_DEBUG_KMS("Invalid mode\n");
2709 goto out;
2710 }
2711
2712 /*
2713 * Check whether the primary plane supports the fb pixel format.
2714 * Drivers not implementing the universal planes API use a
2715 * default formats list provided by the DRM core which doesn't
2716 * match real hardware capabilities. Skip the check in that
2717 * case.
2718 */
2719 if (!crtc->primary->format_default) {
2720 ret = drm_plane_check_pixel_format(crtc->primary,
2721 fb->pixel_format);
2722 if (ret) {
2723 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2724 drm_get_format_name(fb->pixel_format));
2725 goto out;
2726 }
2727 }
2728
2729 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2730 mode, fb);
2731 if (ret)
2732 goto out;
2733
2734 }
2735
2736 if (crtc_req->count_connectors == 0 && mode) {
2737 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2738 ret = -EINVAL;
2739 goto out;
2740 }
2741
2742 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2743 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2744 crtc_req->count_connectors);
2745 ret = -EINVAL;
2746 goto out;
2747 }
2748
2749 if (crtc_req->count_connectors > 0) {
2750 u32 out_id;
2751
2752 /* Avoid unbounded kernel memory allocation */
2753 if (crtc_req->count_connectors > config->num_connector) {
2754 ret = -EINVAL;
2755 goto out;
2756 }
2757
2758 connector_set = kmalloc_array(crtc_req->count_connectors,
2759 sizeof(struct drm_connector *),
2760 GFP_KERNEL);
2761 if (!connector_set) {
2762 ret = -ENOMEM;
2763 goto out;
2764 }
2765
2766 for (i = 0; i < crtc_req->count_connectors; i++) {
2767 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2768 if (get_user(out_id, &set_connectors_ptr[i])) {
2769 ret = -EFAULT;
2770 goto out;
2771 }
2772
2773 connector = drm_connector_find(dev, out_id);
2774 if (!connector) {
2775 DRM_DEBUG_KMS("Connector id %d unknown\n",
2776 out_id);
2777 ret = -ENOENT;
2778 goto out;
2779 }
2780 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2781 connector->base.id,
2782 connector->name);
2783
2784 connector_set[i] = connector;
2785 }
2786 }
2787
2788 set.crtc = crtc;
2789 set.x = crtc_req->x;
2790 set.y = crtc_req->y;
2791 set.mode = mode;
2792 set.connectors = connector_set;
2793 set.num_connectors = crtc_req->count_connectors;
2794 set.fb = fb;
2795 ret = drm_mode_set_config_internal(&set);
2796
2797 out:
2798 if (fb)
2799 drm_framebuffer_unreference(fb);
2800
2801 kfree(connector_set);
2802 drm_mode_destroy(dev, mode);
2803 drm_modeset_unlock_all(dev);
2804 return ret;
2805 }
2806
2807 /**
2808 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2809 * universal plane handler call
2810 * @crtc: crtc to update cursor for
2811 * @req: data pointer for the ioctl
2812 * @file_priv: drm file for the ioctl call
2813 *
2814 * Legacy cursor ioctl's work directly with driver buffer handles. To
2815 * translate legacy ioctl calls into universal plane handler calls, we need to
2816 * wrap the native buffer handle in a drm_framebuffer.
2817 *
2818 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2819 * buffer with a pitch of 4*width; the universal plane interface should be used
2820 * directly in cases where the hardware can support other buffer settings and
2821 * userspace wants to make use of these capabilities.
2822 *
2823 * Returns:
2824 * Zero on success, negative errno on failure.
2825 */
2826 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2827 struct drm_mode_cursor2 *req,
2828 struct drm_file *file_priv)
2829 {
2830 struct drm_device *dev = crtc->dev;
2831 struct drm_framebuffer *fb = NULL;
2832 struct drm_mode_fb_cmd2 fbreq = {
2833 .width = req->width,
2834 .height = req->height,
2835 .pixel_format = DRM_FORMAT_ARGB8888,
2836 .pitches = { req->width * 4 },
2837 .handles = { req->handle },
2838 };
2839 int32_t crtc_x, crtc_y;
2840 uint32_t crtc_w = 0, crtc_h = 0;
2841 uint32_t src_w = 0, src_h = 0;
2842 int ret = 0;
2843
2844 BUG_ON(!crtc->cursor);
2845 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2846
2847 /*
2848 * Obtain fb we'll be using (either new or existing) and take an extra
2849 * reference to it if fb != null. setplane will take care of dropping
2850 * the reference if the plane update fails.
2851 */
2852 if (req->flags & DRM_MODE_CURSOR_BO) {
2853 if (req->handle) {
2854 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
2855 if (IS_ERR(fb)) {
2856 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2857 return PTR_ERR(fb);
2858 }
2859 } else {
2860 fb = NULL;
2861 }
2862 } else {
2863 fb = crtc->cursor->fb;
2864 if (fb)
2865 drm_framebuffer_reference(fb);
2866 }
2867
2868 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2869 crtc_x = req->x;
2870 crtc_y = req->y;
2871 } else {
2872 crtc_x = crtc->cursor_x;
2873 crtc_y = crtc->cursor_y;
2874 }
2875
2876 if (fb) {
2877 crtc_w = fb->width;
2878 crtc_h = fb->height;
2879 src_w = fb->width << 16;
2880 src_h = fb->height << 16;
2881 }
2882
2883 /*
2884 * setplane_internal will take care of deref'ing either the old or new
2885 * framebuffer depending on success.
2886 */
2887 ret = __setplane_internal(crtc->cursor, crtc, fb,
2888 crtc_x, crtc_y, crtc_w, crtc_h,
2889 0, 0, src_w, src_h);
2890
2891 /* Update successful; save new cursor position, if necessary */
2892 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2893 crtc->cursor_x = req->x;
2894 crtc->cursor_y = req->y;
2895 }
2896
2897 return ret;
2898 }
2899
2900 static int drm_mode_cursor_common(struct drm_device *dev,
2901 struct drm_mode_cursor2 *req,
2902 struct drm_file *file_priv)
2903 {
2904 struct drm_crtc *crtc;
2905 int ret = 0;
2906
2907 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2908 return -EINVAL;
2909
2910 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2911 return -EINVAL;
2912
2913 crtc = drm_crtc_find(dev, req->crtc_id);
2914 if (!crtc) {
2915 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2916 return -ENOENT;
2917 }
2918
2919 /*
2920 * If this crtc has a universal cursor plane, call that plane's update
2921 * handler rather than using legacy cursor handlers.
2922 */
2923 drm_modeset_lock_crtc(crtc, crtc->cursor);
2924 if (crtc->cursor) {
2925 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2926 goto out;
2927 }
2928
2929 if (req->flags & DRM_MODE_CURSOR_BO) {
2930 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2931 ret = -ENXIO;
2932 goto out;
2933 }
2934 /* Turns off the cursor if handle is 0 */
2935 if (crtc->funcs->cursor_set2)
2936 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2937 req->width, req->height, req->hot_x, req->hot_y);
2938 else
2939 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2940 req->width, req->height);
2941 }
2942
2943 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2944 if (crtc->funcs->cursor_move) {
2945 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2946 } else {
2947 ret = -EFAULT;
2948 goto out;
2949 }
2950 }
2951 out:
2952 drm_modeset_unlock_crtc(crtc);
2953
2954 return ret;
2955
2956 }
2957
2958
2959 /**
2960 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2961 * @dev: drm device for the ioctl
2962 * @data: data pointer for the ioctl
2963 * @file_priv: drm file for the ioctl call
2964 *
2965 * Set the cursor configuration based on user request.
2966 *
2967 * Called by the user via ioctl.
2968 *
2969 * Returns:
2970 * Zero on success, negative errno on failure.
2971 */
2972 int drm_mode_cursor_ioctl(struct drm_device *dev,
2973 void *data, struct drm_file *file_priv)
2974 {
2975 struct drm_mode_cursor *req = data;
2976 struct drm_mode_cursor2 new_req;
2977
2978 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2979 new_req.hot_x = new_req.hot_y = 0;
2980
2981 return drm_mode_cursor_common(dev, &new_req, file_priv);
2982 }
2983
2984 /**
2985 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2986 * @dev: drm device for the ioctl
2987 * @data: data pointer for the ioctl
2988 * @file_priv: drm file for the ioctl call
2989 *
2990 * Set the cursor configuration based on user request. This implements the 2nd
2991 * version of the cursor ioctl, which allows userspace to additionally specify
2992 * the hotspot of the pointer.
2993 *
2994 * Called by the user via ioctl.
2995 *
2996 * Returns:
2997 * Zero on success, negative errno on failure.
2998 */
2999 int drm_mode_cursor2_ioctl(struct drm_device *dev,
3000 void *data, struct drm_file *file_priv)
3001 {
3002 struct drm_mode_cursor2 *req = data;
3003
3004 return drm_mode_cursor_common(dev, req, file_priv);
3005 }
3006
3007 /**
3008 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3009 * @bpp: bits per pixels
3010 * @depth: bit depth per pixel
3011 *
3012 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3013 * Useful in fbdev emulation code, since that deals in those values.
3014 */
3015 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3016 {
3017 uint32_t fmt;
3018
3019 switch (bpp) {
3020 case 8:
3021 fmt = DRM_FORMAT_C8;
3022 break;
3023 case 16:
3024 if (depth == 15)
3025 fmt = DRM_FORMAT_XRGB1555;
3026 else
3027 fmt = DRM_FORMAT_RGB565;
3028 break;
3029 case 24:
3030 fmt = DRM_FORMAT_RGB888;
3031 break;
3032 case 32:
3033 if (depth == 24)
3034 fmt = DRM_FORMAT_XRGB8888;
3035 else if (depth == 30)
3036 fmt = DRM_FORMAT_XRGB2101010;
3037 else
3038 fmt = DRM_FORMAT_ARGB8888;
3039 break;
3040 default:
3041 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3042 fmt = DRM_FORMAT_XRGB8888;
3043 break;
3044 }
3045
3046 return fmt;
3047 }
3048 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3049
3050 /**
3051 * drm_mode_addfb - add an FB to the graphics configuration
3052 * @dev: drm device for the ioctl
3053 * @data: data pointer for the ioctl
3054 * @file_priv: drm file for the ioctl call
3055 *
3056 * Add a new FB to the specified CRTC, given a user request. This is the
3057 * original addfb ioctl which only supported RGB formats.
3058 *
3059 * Called by the user via ioctl.
3060 *
3061 * Returns:
3062 * Zero on success, negative errno on failure.
3063 */
3064 int drm_mode_addfb(struct drm_device *dev,
3065 void *data, struct drm_file *file_priv)
3066 {
3067 struct drm_mode_fb_cmd *or = data;
3068 struct drm_mode_fb_cmd2 r = {};
3069 int ret;
3070
3071 /* convert to new format and call new ioctl */
3072 r.fb_id = or->fb_id;
3073 r.width = or->width;
3074 r.height = or->height;
3075 r.pitches[0] = or->pitch;
3076 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3077 r.handles[0] = or->handle;
3078
3079 ret = drm_mode_addfb2(dev, &r, file_priv);
3080 if (ret)
3081 return ret;
3082
3083 or->fb_id = r.fb_id;
3084
3085 return 0;
3086 }
3087
3088 static int format_check(const struct drm_mode_fb_cmd2 *r)
3089 {
3090 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3091
3092 switch (format) {
3093 case DRM_FORMAT_C8:
3094 case DRM_FORMAT_RGB332:
3095 case DRM_FORMAT_BGR233:
3096 case DRM_FORMAT_XRGB4444:
3097 case DRM_FORMAT_XBGR4444:
3098 case DRM_FORMAT_RGBX4444:
3099 case DRM_FORMAT_BGRX4444:
3100 case DRM_FORMAT_ARGB4444:
3101 case DRM_FORMAT_ABGR4444:
3102 case DRM_FORMAT_RGBA4444:
3103 case DRM_FORMAT_BGRA4444:
3104 case DRM_FORMAT_XRGB1555:
3105 case DRM_FORMAT_XBGR1555:
3106 case DRM_FORMAT_RGBX5551:
3107 case DRM_FORMAT_BGRX5551:
3108 case DRM_FORMAT_ARGB1555:
3109 case DRM_FORMAT_ABGR1555:
3110 case DRM_FORMAT_RGBA5551:
3111 case DRM_FORMAT_BGRA5551:
3112 case DRM_FORMAT_RGB565:
3113 case DRM_FORMAT_BGR565:
3114 case DRM_FORMAT_RGB888:
3115 case DRM_FORMAT_BGR888:
3116 case DRM_FORMAT_XRGB8888:
3117 case DRM_FORMAT_XBGR8888:
3118 case DRM_FORMAT_RGBX8888:
3119 case DRM_FORMAT_BGRX8888:
3120 case DRM_FORMAT_ARGB8888:
3121 case DRM_FORMAT_ABGR8888:
3122 case DRM_FORMAT_RGBA8888:
3123 case DRM_FORMAT_BGRA8888:
3124 case DRM_FORMAT_XRGB2101010:
3125 case DRM_FORMAT_XBGR2101010:
3126 case DRM_FORMAT_RGBX1010102:
3127 case DRM_FORMAT_BGRX1010102:
3128 case DRM_FORMAT_ARGB2101010:
3129 case DRM_FORMAT_ABGR2101010:
3130 case DRM_FORMAT_RGBA1010102:
3131 case DRM_FORMAT_BGRA1010102:
3132 case DRM_FORMAT_YUYV:
3133 case DRM_FORMAT_YVYU:
3134 case DRM_FORMAT_UYVY:
3135 case DRM_FORMAT_VYUY:
3136 case DRM_FORMAT_AYUV:
3137 case DRM_FORMAT_NV12:
3138 case DRM_FORMAT_NV21:
3139 case DRM_FORMAT_NV16:
3140 case DRM_FORMAT_NV61:
3141 case DRM_FORMAT_NV24:
3142 case DRM_FORMAT_NV42:
3143 case DRM_FORMAT_YUV410:
3144 case DRM_FORMAT_YVU410:
3145 case DRM_FORMAT_YUV411:
3146 case DRM_FORMAT_YVU411:
3147 case DRM_FORMAT_YUV420:
3148 case DRM_FORMAT_YVU420:
3149 case DRM_FORMAT_YUV422:
3150 case DRM_FORMAT_YVU422:
3151 case DRM_FORMAT_YUV444:
3152 case DRM_FORMAT_YVU444:
3153 return 0;
3154 default:
3155 DRM_DEBUG_KMS("invalid pixel format %s\n",
3156 drm_get_format_name(r->pixel_format));
3157 return -EINVAL;
3158 }
3159 }
3160
3161 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3162 {
3163 int ret, hsub, vsub, num_planes, i;
3164
3165 ret = format_check(r);
3166 if (ret) {
3167 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3168 drm_get_format_name(r->pixel_format));
3169 return ret;
3170 }
3171
3172 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3173 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3174 num_planes = drm_format_num_planes(r->pixel_format);
3175
3176 if (r->width == 0 || r->width % hsub) {
3177 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3178 return -EINVAL;
3179 }
3180
3181 if (r->height == 0 || r->height % vsub) {
3182 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3183 return -EINVAL;
3184 }
3185
3186 for (i = 0; i < num_planes; i++) {
3187 unsigned int width = r->width / (i != 0 ? hsub : 1);
3188 unsigned int height = r->height / (i != 0 ? vsub : 1);
3189 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3190
3191 if (!r->handles[i]) {
3192 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3193 return -EINVAL;
3194 }
3195
3196 if ((uint64_t) width * cpp > UINT_MAX)
3197 return -ERANGE;
3198
3199 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3200 return -ERANGE;
3201
3202 if (r->pitches[i] < width * cpp) {
3203 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3204 return -EINVAL;
3205 }
3206
3207 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3208 DRM_DEBUG_KMS("bad fb modifier %"PRIu64" for plane %d\n",
3209 r->modifier[i], i);
3210 return -EINVAL;
3211 }
3212
3213 /* modifier specific checks: */
3214 switch (r->modifier[i]) {
3215 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3216 /* NOTE: the pitch restriction may be lifted later if it turns
3217 * out that no hw has this restriction:
3218 */
3219 if (r->pixel_format != DRM_FORMAT_NV12 ||
3220 width % 128 || height % 32 ||
3221 r->pitches[i] % 128) {
3222 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3223 return -EINVAL;
3224 }
3225 break;
3226
3227 default:
3228 break;
3229 }
3230 }
3231
3232 for (i = num_planes; i < 4; i++) {
3233 if (r->modifier[i]) {
3234 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3235 return -EINVAL;
3236 }
3237
3238 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3239 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3240 continue;
3241
3242 if (r->handles[i]) {
3243 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3244 return -EINVAL;
3245 }
3246
3247 if (r->pitches[i]) {
3248 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3249 return -EINVAL;
3250 }
3251
3252 if (r->offsets[i]) {
3253 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3254 return -EINVAL;
3255 }
3256 }
3257
3258 return 0;
3259 }
3260
3261 static struct drm_framebuffer *
3262 internal_framebuffer_create(struct drm_device *dev,
3263 struct drm_mode_fb_cmd2 *r,
3264 struct drm_file *file_priv)
3265 {
3266 struct drm_mode_config *config = &dev->mode_config;
3267 struct drm_framebuffer *fb;
3268 int ret;
3269
3270 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
3271 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3272 return ERR_PTR(-EINVAL);
3273 }
3274
3275 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3276 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3277 r->width, config->min_width, config->max_width);
3278 return ERR_PTR(-EINVAL);
3279 }
3280 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3281 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3282 r->height, config->min_height, config->max_height);
3283 return ERR_PTR(-EINVAL);
3284 }
3285
3286 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3287 !dev->mode_config.allow_fb_modifiers) {
3288 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3289 return ERR_PTR(-EINVAL);
3290 }
3291
3292 ret = framebuffer_check(r);
3293 if (ret)
3294 return ERR_PTR(ret);
3295
3296 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3297 if (IS_ERR(fb)) {
3298 DRM_DEBUG_KMS("could not create framebuffer\n");
3299 return fb;
3300 }
3301
3302 return fb;
3303 }
3304
3305 /**
3306 * drm_mode_addfb2 - add an FB to the graphics configuration
3307 * @dev: drm device for the ioctl
3308 * @data: data pointer for the ioctl
3309 * @file_priv: drm file for the ioctl call
3310 *
3311 * Add a new FB to the specified CRTC, given a user request with format. This is
3312 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3313 * and uses fourcc codes as pixel format specifiers.
3314 *
3315 * Called by the user via ioctl.
3316 *
3317 * Returns:
3318 * Zero on success, negative errno on failure.
3319 */
3320 int drm_mode_addfb2(struct drm_device *dev,
3321 void *data, struct drm_file *file_priv)
3322 {
3323 struct drm_mode_fb_cmd2 *r = data;
3324 struct drm_framebuffer *fb;
3325
3326 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3327 return -EINVAL;
3328
3329 fb = internal_framebuffer_create(dev, r, file_priv);
3330 if (IS_ERR(fb))
3331 return PTR_ERR(fb);
3332
3333 /* Transfer ownership to the filp for reaping on close */
3334
3335 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3336 mutex_lock(&file_priv->fbs_lock);
3337 r->fb_id = fb->base.id;
3338 list_add(&fb->filp_head, &file_priv->fbs);
3339 mutex_unlock(&file_priv->fbs_lock);
3340
3341 return 0;
3342 }
3343
3344 struct drm_mode_rmfb_work {
3345 struct work_struct work;
3346 struct list_head fbs;
3347 };
3348
3349 static void drm_mode_rmfb_work_fn(struct work_struct *w)
3350 {
3351 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3352
3353 while (!list_empty(&arg->fbs)) {
3354 struct drm_framebuffer *fb =
3355 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3356
3357 list_del_init(&fb->filp_head);
3358 drm_framebuffer_remove(fb);
3359 }
3360 }
3361
3362 /**
3363 * drm_mode_rmfb - remove an FB from the configuration
3364 * @dev: drm device for the ioctl
3365 * @data: data pointer for the ioctl
3366 * @file_priv: drm file for the ioctl call
3367 *
3368 * Remove the FB specified by the user.
3369 *
3370 * Called by the user via ioctl.
3371 *
3372 * Returns:
3373 * Zero on success, negative errno on failure.
3374 */
3375 int drm_mode_rmfb(struct drm_device *dev,
3376 void *data, struct drm_file *file_priv)
3377 {
3378 struct drm_framebuffer *fb = NULL;
3379 struct drm_framebuffer *fbl = NULL;
3380 uint32_t *id = data;
3381 int found = 0;
3382
3383 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3384 return -EINVAL;
3385
3386 mutex_lock(&file_priv->fbs_lock);
3387 mutex_lock(&dev->mode_config.fb_lock);
3388 fb = __drm_framebuffer_lookup(dev, *id);
3389 if (!fb)
3390 goto fail_lookup;
3391
3392 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3393 if (fb == fbl)
3394 found = 1;
3395 if (!found)
3396 goto fail_lookup;
3397
3398 list_del_init(&fb->filp_head);
3399 mutex_unlock(&dev->mode_config.fb_lock);
3400 mutex_unlock(&file_priv->fbs_lock);
3401
3402 /*
3403 * we now own the reference that was stored in the fbs list
3404 *
3405 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3406 * so run this in a separate stack as there's no way to correctly
3407 * handle this after the fb is already removed from the lookup table.
3408 */
3409 if (!kref_exclusive_p(&fb->refcount)) {
3410 struct drm_mode_rmfb_work arg;
3411
3412 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3413 INIT_LIST_HEAD(&arg.fbs);
3414 list_add_tail(&fb->filp_head, &arg.fbs);
3415
3416 schedule_work(&arg.work);
3417 flush_work(&arg.work);
3418 destroy_work_on_stack(&arg.work);
3419 } else
3420 drm_framebuffer_unreference(fb);
3421
3422 return 0;
3423
3424 fail_lookup:
3425 mutex_unlock(&dev->mode_config.fb_lock);
3426 mutex_unlock(&file_priv->fbs_lock);
3427
3428 return -ENOENT;
3429 }
3430
3431 /**
3432 * drm_mode_getfb - get FB info
3433 * @dev: drm device for the ioctl
3434 * @data: data pointer for the ioctl
3435 * @file_priv: drm file for the ioctl call
3436 *
3437 * Lookup the FB given its ID and return info about it.
3438 *
3439 * Called by the user via ioctl.
3440 *
3441 * Returns:
3442 * Zero on success, negative errno on failure.
3443 */
3444 int drm_mode_getfb(struct drm_device *dev,
3445 void *data, struct drm_file *file_priv)
3446 {
3447 struct drm_mode_fb_cmd *r = data;
3448 struct drm_framebuffer *fb;
3449 int ret;
3450
3451 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3452 return -EINVAL;
3453
3454 fb = drm_framebuffer_lookup(dev, r->fb_id);
3455 if (!fb)
3456 return -ENOENT;
3457
3458 r->height = fb->height;
3459 r->width = fb->width;
3460 r->depth = fb->depth;
3461 r->bpp = fb->bits_per_pixel;
3462 r->pitch = fb->pitches[0];
3463 if (fb->funcs->create_handle) {
3464 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3465 drm_is_control_client(file_priv)) {
3466 ret = fb->funcs->create_handle(fb, file_priv,
3467 &r->handle);
3468 } else {
3469 /* GET_FB() is an unprivileged ioctl so we must not
3470 * return a buffer-handle to non-master processes! For
3471 * backwards-compatibility reasons, we cannot make
3472 * GET_FB() privileged, so just return an invalid handle
3473 * for non-masters. */
3474 r->handle = 0;
3475 ret = 0;
3476 }
3477 } else {
3478 ret = -ENODEV;
3479 }
3480
3481 drm_framebuffer_unreference(fb);
3482
3483 return ret;
3484 }
3485
3486 /**
3487 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3488 * @dev: drm device for the ioctl
3489 * @data: data pointer for the ioctl
3490 * @file_priv: drm file for the ioctl call
3491 *
3492 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3493 * rectangle list. Generic userspace which does frontbuffer rendering must call
3494 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3495 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3496 *
3497 * Modesetting drivers which always update the frontbuffer do not need to
3498 * implement the corresponding ->dirty framebuffer callback.
3499 *
3500 * Called by the user via ioctl.
3501 *
3502 * Returns:
3503 * Zero on success, negative errno on failure.
3504 */
3505 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3506 void *data, struct drm_file *file_priv)
3507 {
3508 struct drm_clip_rect __user *clips_ptr;
3509 struct drm_clip_rect *clips = NULL;
3510 struct drm_mode_fb_dirty_cmd *r = data;
3511 struct drm_framebuffer *fb;
3512 unsigned flags;
3513 int num_clips;
3514 int ret;
3515
3516 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3517 return -EINVAL;
3518
3519 fb = drm_framebuffer_lookup(dev, r->fb_id);
3520 if (!fb)
3521 return -ENOENT;
3522
3523 num_clips = r->num_clips;
3524 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3525
3526 if (!num_clips != !clips_ptr) {
3527 ret = -EINVAL;
3528 goto out_err1;
3529 }
3530
3531 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3532
3533 /* If userspace annotates copy, clips must come in pairs */
3534 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3535 ret = -EINVAL;
3536 goto out_err1;
3537 }
3538
3539 if (num_clips && clips_ptr) {
3540 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3541 ret = -EINVAL;
3542 goto out_err1;
3543 }
3544 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3545 if (!clips) {
3546 ret = -ENOMEM;
3547 goto out_err1;
3548 }
3549
3550 ret = copy_from_user(clips, clips_ptr,
3551 num_clips * sizeof(*clips));
3552 if (ret) {
3553 ret = -EFAULT;
3554 goto out_err2;
3555 }
3556 }
3557
3558 if (fb->funcs->dirty) {
3559 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3560 clips, num_clips);
3561 } else {
3562 ret = -ENOSYS;
3563 }
3564
3565 out_err2:
3566 kfree(clips);
3567 out_err1:
3568 drm_framebuffer_unreference(fb);
3569
3570 return ret;
3571 }
3572
3573 /**
3574 * drm_fb_release - remove and free the FBs on this file
3575 * @priv: drm file for the ioctl
3576 *
3577 * Destroy all the FBs associated with @filp.
3578 *
3579 * Called by the user via ioctl.
3580 *
3581 * Returns:
3582 * Zero on success, negative errno on failure.
3583 */
3584 void drm_fb_release(struct drm_file *priv)
3585 {
3586 struct drm_framebuffer *fb, *tfb;
3587 struct drm_mode_rmfb_work arg;
3588
3589 INIT_LIST_HEAD(&arg.fbs);
3590
3591 /*
3592 * When the file gets released that means no one else can access the fb
3593 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3594 * avoid upsetting lockdep since the universal cursor code adds a
3595 * framebuffer while holding mutex locks.
3596 *
3597 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3598 * locks is impossible here since no one else but this function can get
3599 * at it any more.
3600 */
3601 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3602 if (!kref_exclusive_p(&fb->refcount)) {
3603 list_move_tail(&fb->filp_head, &arg.fbs);
3604 } else {
3605 list_del_init(&fb->filp_head);
3606
3607 /* This drops the fpriv->fbs reference. */
3608 drm_framebuffer_unreference(fb);
3609 }
3610 }
3611
3612 if (!list_empty(&arg.fbs)) {
3613 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3614
3615 schedule_work(&arg.work);
3616 flush_work(&arg.work);
3617 destroy_work_on_stack(&arg.work);
3618 }
3619 }
3620
3621 /**
3622 * drm_property_create - create a new property type
3623 * @dev: drm device
3624 * @flags: flags specifying the property type
3625 * @name: name of the property
3626 * @num_values: number of pre-defined values
3627 *
3628 * This creates a new generic drm property which can then be attached to a drm
3629 * object with drm_object_attach_property. The returned property object must be
3630 * freed with drm_property_destroy.
3631 *
3632 * Note that the DRM core keeps a per-device list of properties and that, if
3633 * drm_mode_config_cleanup() is called, it will destroy all properties created
3634 * by the driver.
3635 *
3636 * Returns:
3637 * A pointer to the newly created property on success, NULL on failure.
3638 */
3639 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3640 const char *name, int num_values)
3641 {
3642 struct drm_property *property = NULL;
3643 int ret;
3644
3645 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3646 if (!property)
3647 return NULL;
3648
3649 property->dev = dev;
3650
3651 if (num_values) {
3652 property->values = kcalloc(num_values, sizeof(uint64_t),
3653 GFP_KERNEL);
3654 if (!property->values)
3655 goto fail;
3656 }
3657
3658 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3659 if (ret)
3660 goto fail;
3661
3662 property->flags = flags;
3663 property->num_values = num_values;
3664 INIT_LIST_HEAD(&property->enum_list);
3665
3666 if (name) {
3667 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3668 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3669 }
3670
3671 list_add_tail(&property->head, &dev->mode_config.property_list);
3672
3673 WARN_ON(!drm_property_type_valid(property));
3674
3675 return property;
3676 fail:
3677 kfree(property->values);
3678 kfree(property);
3679 return NULL;
3680 }
3681 EXPORT_SYMBOL(drm_property_create);
3682
3683 /**
3684 * drm_property_create_enum - create a new enumeration property type
3685 * @dev: drm device
3686 * @flags: flags specifying the property type
3687 * @name: name of the property
3688 * @props: enumeration lists with property values
3689 * @num_values: number of pre-defined values
3690 *
3691 * This creates a new generic drm property which can then be attached to a drm
3692 * object with drm_object_attach_property. The returned property object must be
3693 * freed with drm_property_destroy.
3694 *
3695 * Userspace is only allowed to set one of the predefined values for enumeration
3696 * properties.
3697 *
3698 * Returns:
3699 * A pointer to the newly created property on success, NULL on failure.
3700 */
3701 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3702 const char *name,
3703 const struct drm_prop_enum_list *props,
3704 int num_values)
3705 {
3706 struct drm_property *property;
3707 int i, ret;
3708
3709 flags |= DRM_MODE_PROP_ENUM;
3710
3711 property = drm_property_create(dev, flags, name, num_values);
3712 if (!property)
3713 return NULL;
3714
3715 for (i = 0; i < num_values; i++) {
3716 ret = drm_property_add_enum(property, i,
3717 props[i].type,
3718 props[i].name);
3719 if (ret) {
3720 drm_property_destroy(dev, property);
3721 return NULL;
3722 }
3723 }
3724
3725 return property;
3726 }
3727 EXPORT_SYMBOL(drm_property_create_enum);
3728
3729 /**
3730 * drm_property_create_bitmask - create a new bitmask property type
3731 * @dev: drm device
3732 * @flags: flags specifying the property type
3733 * @name: name of the property
3734 * @props: enumeration lists with property bitflags
3735 * @num_props: size of the @props array
3736 * @supported_bits: bitmask of all supported enumeration values
3737 *
3738 * This creates a new bitmask drm property which can then be attached to a drm
3739 * object with drm_object_attach_property. The returned property object must be
3740 * freed with drm_property_destroy.
3741 *
3742 * Compared to plain enumeration properties userspace is allowed to set any
3743 * or'ed together combination of the predefined property bitflag values
3744 *
3745 * Returns:
3746 * A pointer to the newly created property on success, NULL on failure.
3747 */
3748 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3749 int flags, const char *name,
3750 const struct drm_prop_enum_list *props,
3751 int num_props,
3752 uint64_t supported_bits)
3753 {
3754 struct drm_property *property;
3755 int i, ret, index = 0;
3756 int num_values = hweight64(supported_bits);
3757
3758 flags |= DRM_MODE_PROP_BITMASK;
3759
3760 property = drm_property_create(dev, flags, name, num_values);
3761 if (!property)
3762 return NULL;
3763 for (i = 0; i < num_props; i++) {
3764 if (!(supported_bits & (1ULL << props[i].type)))
3765 continue;
3766
3767 if (WARN_ON(index >= num_values)) {
3768 drm_property_destroy(dev, property);
3769 return NULL;
3770 }
3771
3772 ret = drm_property_add_enum(property, index++,
3773 props[i].type,
3774 props[i].name);
3775 if (ret) {
3776 drm_property_destroy(dev, property);
3777 return NULL;
3778 }
3779 }
3780
3781 return property;
3782 }
3783 EXPORT_SYMBOL(drm_property_create_bitmask);
3784
3785 static struct drm_property *property_create_range(struct drm_device *dev,
3786 int flags, const char *name,
3787 uint64_t min, uint64_t max)
3788 {
3789 struct drm_property *property;
3790
3791 property = drm_property_create(dev, flags, name, 2);
3792 if (!property)
3793 return NULL;
3794
3795 property->values[0] = min;
3796 property->values[1] = max;
3797
3798 return property;
3799 }
3800
3801 /**
3802 * drm_property_create_range - create a new unsigned ranged property type
3803 * @dev: drm device
3804 * @flags: flags specifying the property type
3805 * @name: name of the property
3806 * @min: minimum value of the property
3807 * @max: maximum value of the property
3808 *
3809 * This creates a new generic drm property which can then be attached to a drm
3810 * object with drm_object_attach_property. The returned property object must be
3811 * freed with drm_property_destroy.
3812 *
3813 * Userspace is allowed to set any unsigned integer value in the (min, max)
3814 * range inclusive.
3815 *
3816 * Returns:
3817 * A pointer to the newly created property on success, NULL on failure.
3818 */
3819 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3820 const char *name,
3821 uint64_t min, uint64_t max)
3822 {
3823 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3824 name, min, max);
3825 }
3826 EXPORT_SYMBOL(drm_property_create_range);
3827
3828 /**
3829 * drm_property_create_signed_range - create a new signed ranged property type
3830 * @dev: drm device
3831 * @flags: flags specifying the property type
3832 * @name: name of the property
3833 * @min: minimum value of the property
3834 * @max: maximum value of the property
3835 *
3836 * This creates a new generic drm property which can then be attached to a drm
3837 * object with drm_object_attach_property. The returned property object must be
3838 * freed with drm_property_destroy.
3839 *
3840 * Userspace is allowed to set any signed integer value in the (min, max)
3841 * range inclusive.
3842 *
3843 * Returns:
3844 * A pointer to the newly created property on success, NULL on failure.
3845 */
3846 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3847 int flags, const char *name,
3848 int64_t min, int64_t max)
3849 {
3850 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3851 name, I642U64(min), I642U64(max));
3852 }
3853 EXPORT_SYMBOL(drm_property_create_signed_range);
3854
3855 /**
3856 * drm_property_create_object - create a new object property type
3857 * @dev: drm device
3858 * @flags: flags specifying the property type
3859 * @name: name of the property
3860 * @type: object type from DRM_MODE_OBJECT_* defines
3861 *
3862 * This creates a new generic drm property which can then be attached to a drm
3863 * object with drm_object_attach_property. The returned property object must be
3864 * freed with drm_property_destroy.
3865 *
3866 * Userspace is only allowed to set this to any property value of the given
3867 * @type. Only useful for atomic properties, which is enforced.
3868 *
3869 * Returns:
3870 * A pointer to the newly created property on success, NULL on failure.
3871 */
3872 struct drm_property *drm_property_create_object(struct drm_device *dev,
3873 int flags, const char *name, uint32_t type)
3874 {
3875 struct drm_property *property;
3876
3877 flags |= DRM_MODE_PROP_OBJECT;
3878
3879 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3880 return NULL;
3881
3882 property = drm_property_create(dev, flags, name, 1);
3883 if (!property)
3884 return NULL;
3885
3886 property->values[0] = type;
3887
3888 return property;
3889 }
3890 EXPORT_SYMBOL(drm_property_create_object);
3891
3892 /**
3893 * drm_property_create_bool - create a new boolean property type
3894 * @dev: drm device
3895 * @flags: flags specifying the property type
3896 * @name: name of the property
3897 *
3898 * This creates a new generic drm property which can then be attached to a drm
3899 * object with drm_object_attach_property. The returned property object must be
3900 * freed with drm_property_destroy.
3901 *
3902 * This is implemented as a ranged property with only {0, 1} as valid values.
3903 *
3904 * Returns:
3905 * A pointer to the newly created property on success, NULL on failure.
3906 */
3907 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3908 const char *name)
3909 {
3910 return drm_property_create_range(dev, flags, name, 0, 1);
3911 }
3912 EXPORT_SYMBOL(drm_property_create_bool);
3913
3914 /**
3915 * drm_property_add_enum - add a possible value to an enumeration property
3916 * @property: enumeration property to change
3917 * @index: index of the new enumeration
3918 * @value: value of the new enumeration
3919 * @name: symbolic name of the new enumeration
3920 *
3921 * This functions adds enumerations to a property.
3922 *
3923 * It's use is deprecated, drivers should use one of the more specific helpers
3924 * to directly create the property with all enumerations already attached.
3925 *
3926 * Returns:
3927 * Zero on success, error code on failure.
3928 */
3929 int drm_property_add_enum(struct drm_property *property, int index,
3930 uint64_t value, const char *name)
3931 {
3932 struct drm_property_enum *prop_enum;
3933
3934 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3935 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3936 return -EINVAL;
3937
3938 /*
3939 * Bitmask enum properties have the additional constraint of values
3940 * from 0 to 63
3941 */
3942 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3943 (value > 63))
3944 return -EINVAL;
3945
3946 if (!list_empty(&property->enum_list)) {
3947 list_for_each_entry(prop_enum, &property->enum_list, head) {
3948 if (prop_enum->value == value) {
3949 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3950 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3951 return 0;
3952 }
3953 }
3954 }
3955
3956 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3957 if (!prop_enum)
3958 return -ENOMEM;
3959
3960 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3961 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3962 prop_enum->value = value;
3963
3964 property->values[index] = value;
3965 list_add_tail(&prop_enum->head, &property->enum_list);
3966 return 0;
3967 }
3968 EXPORT_SYMBOL(drm_property_add_enum);
3969
3970 /**
3971 * drm_property_destroy - destroy a drm property
3972 * @dev: drm device
3973 * @property: property to destry
3974 *
3975 * This function frees a property including any attached resources like
3976 * enumeration values.
3977 */
3978 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3979 {
3980 struct drm_property_enum *prop_enum, *pt;
3981
3982 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
3983 list_del(&prop_enum->head);
3984 kfree(prop_enum);
3985 }
3986
3987 if (property->num_values)
3988 kfree(property->values);
3989 drm_mode_object_put(dev, &property->base);
3990 list_del(&property->head);
3991 kfree(property);
3992 }
3993 EXPORT_SYMBOL(drm_property_destroy);
3994
3995 /**
3996 * drm_object_attach_property - attach a property to a modeset object
3997 * @obj: drm modeset object
3998 * @property: property to attach
3999 * @init_val: initial value of the property
4000 *
4001 * This attaches the given property to the modeset object with the given initial
4002 * value. Currently this function cannot fail since the properties are stored in
4003 * a statically sized array.
4004 */
4005 void drm_object_attach_property(struct drm_mode_object *obj,
4006 struct drm_property *property,
4007 uint64_t init_val)
4008 {
4009 int count = obj->properties->count;
4010
4011 if (count == DRM_OBJECT_MAX_PROPERTY) {
4012 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4013 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4014 "you see this message on the same object type.\n",
4015 obj->type);
4016 return;
4017 }
4018
4019 obj->properties->properties[count] = property;
4020 obj->properties->values[count] = init_val;
4021 obj->properties->count++;
4022 if (property->flags & DRM_MODE_PROP_ATOMIC)
4023 obj->properties->atomic_count++;
4024 }
4025 EXPORT_SYMBOL(drm_object_attach_property);
4026
4027 /**
4028 * drm_object_property_set_value - set the value of a property
4029 * @obj: drm mode object to set property value for
4030 * @property: property to set
4031 * @val: value the property should be set to
4032 *
4033 * This functions sets a given property on a given object. This function only
4034 * changes the software state of the property, it does not call into the
4035 * driver's ->set_property callback.
4036 *
4037 * Returns:
4038 * Zero on success, error code on failure.
4039 */
4040 int drm_object_property_set_value(struct drm_mode_object *obj,
4041 struct drm_property *property, uint64_t val)
4042 {
4043 int i;
4044
4045 for (i = 0; i < obj->properties->count; i++) {
4046 if (obj->properties->properties[i] == property) {
4047 obj->properties->values[i] = val;
4048 return 0;
4049 }
4050 }
4051
4052 return -EINVAL;
4053 }
4054 EXPORT_SYMBOL(drm_object_property_set_value);
4055
4056 /**
4057 * drm_object_property_get_value - retrieve the value of a property
4058 * @obj: drm mode object to get property value from
4059 * @property: property to retrieve
4060 * @val: storage for the property value
4061 *
4062 * This function retrieves the softare state of the given property for the given
4063 * property. Since there is no driver callback to retrieve the current property
4064 * value this might be out of sync with the hardware, depending upon the driver
4065 * and property.
4066 *
4067 * Returns:
4068 * Zero on success, error code on failure.
4069 */
4070 int drm_object_property_get_value(struct drm_mode_object *obj,
4071 struct drm_property *property, uint64_t *val)
4072 {
4073 int i;
4074
4075 /* read-only properties bypass atomic mechanism and still store
4076 * their value in obj->properties->values[].. mostly to avoid
4077 * having to deal w/ EDID and similar props in atomic paths:
4078 */
4079 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4080 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4081 return drm_atomic_get_property(obj, property, val);
4082
4083 for (i = 0; i < obj->properties->count; i++) {
4084 if (obj->properties->properties[i] == property) {
4085 *val = obj->properties->values[i];
4086 return 0;
4087 }
4088 }
4089
4090 return -EINVAL;
4091 }
4092 EXPORT_SYMBOL(drm_object_property_get_value);
4093
4094 /**
4095 * drm_mode_getproperty_ioctl - get the property metadata
4096 * @dev: DRM device
4097 * @data: ioctl data
4098 * @file_priv: DRM file info
4099 *
4100 * This function retrieves the metadata for a given property, like the different
4101 * possible values for an enum property or the limits for a range property.
4102 *
4103 * Blob properties are special
4104 *
4105 * Called by the user via ioctl.
4106 *
4107 * Returns:
4108 * Zero on success, negative errno on failure.
4109 */
4110 int drm_mode_getproperty_ioctl(struct drm_device *dev,
4111 void *data, struct drm_file *file_priv)
4112 {
4113 struct drm_mode_get_property *out_resp = data;
4114 struct drm_property *property;
4115 int enum_count = 0;
4116 int value_count = 0;
4117 int ret = 0, i;
4118 int copied;
4119 struct drm_property_enum *prop_enum;
4120 struct drm_mode_property_enum __user *enum_ptr;
4121 uint64_t __user *values_ptr;
4122
4123 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4124 return -EINVAL;
4125
4126 drm_modeset_lock_all(dev);
4127 property = drm_property_find(dev, out_resp->prop_id);
4128 if (!property) {
4129 ret = -ENOENT;
4130 goto done;
4131 }
4132
4133 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4134 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4135 list_for_each_entry(prop_enum, &property->enum_list, head)
4136 enum_count++;
4137 }
4138
4139 value_count = property->num_values;
4140
4141 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4142 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4143 out_resp->flags = property->flags;
4144
4145 if ((out_resp->count_values >= value_count) && value_count) {
4146 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
4147 for (i = 0; i < value_count; i++) {
4148 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4149 ret = -EFAULT;
4150 goto done;
4151 }
4152 }
4153 }
4154 out_resp->count_values = value_count;
4155
4156 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4157 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4158 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4159 copied = 0;
4160 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
4161 list_for_each_entry(prop_enum, &property->enum_list, head) {
4162
4163 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4164 ret = -EFAULT;
4165 goto done;
4166 }
4167
4168 if (copy_to_user(&enum_ptr[copied].name,
4169 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4170 ret = -EFAULT;
4171 goto done;
4172 }
4173 copied++;
4174 }
4175 }
4176 out_resp->count_enum_blobs = enum_count;
4177 }
4178
4179 /*
4180 * NOTE: The idea seems to have been to use this to read all the blob
4181 * property values. But nothing ever added them to the corresponding
4182 * list, userspace always used the special-purpose get_blob ioctl to
4183 * read the value for a blob property. It also doesn't make a lot of
4184 * sense to return values here when everything else is just metadata for
4185 * the property itself.
4186 */
4187 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4188 out_resp->count_enum_blobs = 0;
4189 done:
4190 drm_modeset_unlock_all(dev);
4191 return ret;
4192 }
4193
4194 /**
4195 * drm_property_create_blob - Create new blob property
4196 *
4197 * Creates a new blob property for a specified DRM device, optionally
4198 * copying data.
4199 *
4200 * @dev: DRM device to create property for
4201 * @length: Length to allocate for blob data
4202 * @data: If specified, copies data into blob
4203 *
4204 * Returns:
4205 * New blob property with a single reference on success, or an ERR_PTR
4206 * value on failure.
4207 */
4208 struct drm_property_blob *
4209 drm_property_create_blob(struct drm_device *dev, size_t length,
4210 const void *data)
4211 {
4212 struct drm_property_blob *blob;
4213 int ret;
4214
4215 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
4216 return ERR_PTR(-EINVAL);
4217
4218 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4219 if (!blob)
4220 return ERR_PTR(-ENOMEM);
4221
4222 /* This must be explicitly initialised, so we can safely call list_del
4223 * on it in the removal handler, even if it isn't in a file list. */
4224 INIT_LIST_HEAD(&blob->head_file);
4225 blob->length = length;
4226 blob->dev = dev;
4227
4228 if (data)
4229 memcpy(blob->data, data, length);
4230
4231 mutex_lock(&dev->mode_config.blob_lock);
4232
4233 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4234 if (ret) {
4235 kfree(blob);
4236 mutex_unlock(&dev->mode_config.blob_lock);
4237 return ERR_PTR(-EINVAL);
4238 }
4239
4240 kref_init(&blob->refcount);
4241
4242 list_add_tail(&blob->head_global,
4243 &dev->mode_config.property_blob_list);
4244
4245 mutex_unlock(&dev->mode_config.blob_lock);
4246
4247 return blob;
4248 }
4249 EXPORT_SYMBOL(drm_property_create_blob);
4250
4251 /**
4252 * drm_property_free_blob - Blob property destructor
4253 *
4254 * Internal free function for blob properties; must not be used directly.
4255 *
4256 * @kref: Reference
4257 */
4258 static void drm_property_free_blob(struct kref *kref)
4259 {
4260 struct drm_property_blob *blob =
4261 container_of(kref, struct drm_property_blob, refcount);
4262
4263 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4264
4265 list_del(&blob->head_global);
4266 list_del(&blob->head_file);
4267 drm_mode_object_put(blob->dev, &blob->base);
4268
4269 kfree(blob);
4270 }
4271
4272 /**
4273 * drm_property_unreference_blob - Unreference a blob property
4274 *
4275 * Drop a reference on a blob property. May free the object.
4276 *
4277 * @blob: Pointer to blob property
4278 */
4279 void drm_property_unreference_blob(struct drm_property_blob *blob)
4280 {
4281 struct drm_device *dev;
4282
4283 if (!blob)
4284 return;
4285
4286 dev = blob->dev;
4287
4288 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
4289
4290 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4291 &dev->mode_config.blob_lock))
4292 mutex_unlock(&dev->mode_config.blob_lock);
4293 else
4294 might_lock(&dev->mode_config.blob_lock);
4295 }
4296 EXPORT_SYMBOL(drm_property_unreference_blob);
4297
4298 /**
4299 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4300 *
4301 * Drop a reference on a blob property. May free the object. This must be
4302 * called with blob_lock held.
4303 *
4304 * @blob: Pointer to blob property
4305 */
4306 static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4307 {
4308 if (!blob)
4309 return;
4310
4311 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
4312
4313 kref_put(&blob->refcount, drm_property_free_blob);
4314 }
4315
4316 /**
4317 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4318 * @dev: DRM device
4319 * @file_priv: destroy all blobs owned by this file handle
4320 */
4321 void drm_property_destroy_user_blobs(struct drm_device *dev,
4322 struct drm_file *file_priv)
4323 {
4324 struct drm_property_blob *blob, *bt;
4325
4326 mutex_lock(&dev->mode_config.blob_lock);
4327
4328 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4329 list_del_init(&blob->head_file);
4330 drm_property_unreference_blob_locked(blob);
4331 }
4332
4333 mutex_unlock(&dev->mode_config.blob_lock);
4334 }
4335
4336 /**
4337 * drm_property_reference_blob - Take a reference on an existing property
4338 *
4339 * Take a new reference on an existing blob property.
4340 *
4341 * @blob: Pointer to blob property
4342 */
4343 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4344 {
4345 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
4346 kref_get(&blob->refcount);
4347 return blob;
4348 }
4349 EXPORT_SYMBOL(drm_property_reference_blob);
4350
4351 /*
4352 * Like drm_property_lookup_blob, but does not return an additional reference.
4353 * Must be called with blob_lock held.
4354 */
4355 static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4356 uint32_t id)
4357 {
4358 struct drm_mode_object *obj = NULL;
4359 struct drm_property_blob *blob;
4360
4361 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4362
4363 mutex_lock(&dev->mode_config.idr_mutex);
4364 obj = idr_find(&dev->mode_config.crtc_idr, id);
4365 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4366 blob = NULL;
4367 else
4368 blob = obj_to_blob(obj);
4369 mutex_unlock(&dev->mode_config.idr_mutex);
4370
4371 return blob;
4372 }
4373
4374 /**
4375 * drm_property_lookup_blob - look up a blob property and take a reference
4376 * @dev: drm device
4377 * @id: id of the blob property
4378 *
4379 * If successful, this takes an additional reference to the blob property.
4380 * callers need to make sure to eventually unreference the returned property
4381 * again, using @drm_property_unreference_blob.
4382 */
4383 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4384 uint32_t id)
4385 {
4386 struct drm_property_blob *blob;
4387
4388 mutex_lock(&dev->mode_config.blob_lock);
4389 blob = __drm_property_lookup_blob(dev, id);
4390 if (blob) {
4391 if (!kref_get_unless_zero(&blob->refcount))
4392 blob = NULL;
4393 }
4394 mutex_unlock(&dev->mode_config.blob_lock);
4395
4396 return blob;
4397 }
4398 EXPORT_SYMBOL(drm_property_lookup_blob);
4399
4400 /**
4401 * drm_property_replace_global_blob - atomically replace existing blob property
4402 * @dev: drm device
4403 * @replace: location of blob property pointer to be replaced
4404 * @length: length of data for new blob, or 0 for no data
4405 * @data: content for new blob, or NULL for no data
4406 * @obj_holds_id: optional object for property holding blob ID
4407 * @prop_holds_id: optional property holding blob ID
4408 * @return 0 on success or error on failure
4409 *
4410 * This function will atomically replace a global property in the blob list,
4411 * optionally updating a property which holds the ID of that property. It is
4412 * guaranteed to be atomic: no caller will be allowed to see intermediate
4413 * results, and either the entire operation will succeed and clean up the
4414 * previous property, or it will fail and the state will be unchanged.
4415 *
4416 * If length is 0 or data is NULL, no new blob will be created, and the holding
4417 * property, if specified, will be set to 0.
4418 *
4419 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4420 * by holding the relevant modesetting object lock for its parent.
4421 *
4422 * For example, a drm_connector has a 'PATH' property, which contains the ID
4423 * of a blob property with the value of the MST path information. Calling this
4424 * function with replace pointing to the connector's path_blob_ptr, length and
4425 * data set for the new path information, obj_holds_id set to the connector's
4426 * base object, and prop_holds_id set to the path property name, will perform
4427 * a completely atomic update. The access to path_blob_ptr is protected by the
4428 * caller holding a lock on the connector.
4429 */
4430 static int drm_property_replace_global_blob(struct drm_device *dev,
4431 struct drm_property_blob **replace,
4432 size_t length,
4433 const void *data,
4434 struct drm_mode_object *obj_holds_id,
4435 struct drm_property *prop_holds_id)
4436 {
4437 struct drm_property_blob *new_blob = NULL;
4438 struct drm_property_blob *old_blob = NULL;
4439 int ret;
4440
4441 WARN_ON(replace == NULL);
4442
4443 old_blob = *replace;
4444
4445 if (length && data) {
4446 new_blob = drm_property_create_blob(dev, length, data);
4447 if (IS_ERR(new_blob))
4448 return PTR_ERR(new_blob);
4449 }
4450
4451 /* This does not need to be synchronised with blob_lock, as the
4452 * get_properties ioctl locks all modesetting objects, and
4453 * obj_holds_id must be locked before calling here, so we cannot
4454 * have its value out of sync with the list membership modified
4455 * below under blob_lock. */
4456 if (obj_holds_id) {
4457 ret = drm_object_property_set_value(obj_holds_id,
4458 prop_holds_id,
4459 new_blob ?
4460 new_blob->base.id : 0);
4461 if (ret != 0)
4462 goto err_created;
4463 }
4464
4465 drm_property_unreference_blob(old_blob);
4466 *replace = new_blob;
4467
4468 return 0;
4469
4470 err_created:
4471 drm_property_unreference_blob(new_blob);
4472 return ret;
4473 }
4474
4475 /**
4476 * drm_mode_getblob_ioctl - get the contents of a blob property value
4477 * @dev: DRM device
4478 * @data: ioctl data
4479 * @file_priv: DRM file info
4480 *
4481 * This function retrieves the contents of a blob property. The value stored in
4482 * an object's blob property is just a normal modeset object id.
4483 *
4484 * Called by the user via ioctl.
4485 *
4486 * Returns:
4487 * Zero on success, negative errno on failure.
4488 */
4489 int drm_mode_getblob_ioctl(struct drm_device *dev,
4490 void *data, struct drm_file *file_priv)
4491 {
4492 struct drm_mode_get_blob *out_resp = data;
4493 struct drm_property_blob *blob;
4494 int ret = 0;
4495 void __user *blob_ptr;
4496
4497 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4498 return -EINVAL;
4499
4500 drm_modeset_lock_all(dev);
4501 mutex_lock(&dev->mode_config.blob_lock);
4502 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4503 if (!blob) {
4504 ret = -ENOENT;
4505 goto done;
4506 }
4507
4508 if (out_resp->length == blob->length) {
4509 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4510 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4511 ret = -EFAULT;
4512 goto done;
4513 }
4514 }
4515 out_resp->length = blob->length;
4516
4517 done:
4518 mutex_unlock(&dev->mode_config.blob_lock);
4519 drm_modeset_unlock_all(dev);
4520 return ret;
4521 }
4522
4523 /**
4524 * drm_mode_createblob_ioctl - create a new blob property
4525 * @dev: DRM device
4526 * @data: ioctl data
4527 * @file_priv: DRM file info
4528 *
4529 * This function creates a new blob property with user-defined values. In order
4530 * to give us sensible validation and checking when creating, rather than at
4531 * every potential use, we also require a type to be provided upfront.
4532 *
4533 * Called by the user via ioctl.
4534 *
4535 * Returns:
4536 * Zero on success, negative errno on failure.
4537 */
4538 int drm_mode_createblob_ioctl(struct drm_device *dev,
4539 void *data, struct drm_file *file_priv)
4540 {
4541 struct drm_mode_create_blob *out_resp = data;
4542 struct drm_property_blob *blob;
4543 void __user *blob_ptr;
4544 int ret = 0;
4545
4546 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4547 return -EINVAL;
4548
4549 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4550 if (IS_ERR(blob))
4551 return PTR_ERR(blob);
4552
4553 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4554 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4555 ret = -EFAULT;
4556 goto out_blob;
4557 }
4558
4559 /* Dropping the lock between create_blob and our access here is safe
4560 * as only the same file_priv can remove the blob; at this point, it is
4561 * not associated with any file_priv. */
4562 mutex_lock(&dev->mode_config.blob_lock);
4563 out_resp->blob_id = blob->base.id;
4564 list_add_tail(&blob->head_file, &file_priv->blobs);
4565 mutex_unlock(&dev->mode_config.blob_lock);
4566
4567 return 0;
4568
4569 out_blob:
4570 drm_property_unreference_blob(blob);
4571 return ret;
4572 }
4573
4574 /**
4575 * drm_mode_destroyblob_ioctl - destroy a user blob property
4576 * @dev: DRM device
4577 * @data: ioctl data
4578 * @file_priv: DRM file info
4579 *
4580 * Destroy an existing user-defined blob property.
4581 *
4582 * Called by the user via ioctl.
4583 *
4584 * Returns:
4585 * Zero on success, negative errno on failure.
4586 */
4587 int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4588 void *data, struct drm_file *file_priv)
4589 {
4590 struct drm_mode_destroy_blob *out_resp = data;
4591 struct drm_property_blob *blob = NULL, *bt;
4592 bool found = false;
4593 int ret = 0;
4594
4595 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4596 return -EINVAL;
4597
4598 mutex_lock(&dev->mode_config.blob_lock);
4599 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4600 if (!blob) {
4601 ret = -ENOENT;
4602 goto err;
4603 }
4604
4605 /* Ensure the property was actually created by this user. */
4606 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4607 if (bt == blob) {
4608 found = true;
4609 break;
4610 }
4611 }
4612
4613 if (!found) {
4614 ret = -EPERM;
4615 goto err;
4616 }
4617
4618 /* We must drop head_file here, because we may not be the last
4619 * reference on the blob. */
4620 list_del_init(&blob->head_file);
4621 drm_property_unreference_blob_locked(blob);
4622 mutex_unlock(&dev->mode_config.blob_lock);
4623
4624 return 0;
4625
4626 err:
4627 mutex_unlock(&dev->mode_config.blob_lock);
4628 return ret;
4629 }
4630
4631 /**
4632 * drm_mode_connector_set_path_property - set tile property on connector
4633 * @connector: connector to set property on.
4634 * @path: path to use for property; must not be NULL.
4635 *
4636 * This creates a property to expose to userspace to specify a
4637 * connector path. This is mainly used for DisplayPort MST where
4638 * connectors have a topology and we want to allow userspace to give
4639 * them more meaningful names.
4640 *
4641 * Returns:
4642 * Zero on success, negative errno on failure.
4643 */
4644 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4645 const char *path)
4646 {
4647 struct drm_device *dev = connector->dev;
4648 int ret;
4649
4650 ret = drm_property_replace_global_blob(dev,
4651 &connector->path_blob_ptr,
4652 strlen(path) + 1,
4653 path,
4654 &connector->base,
4655 dev->mode_config.path_property);
4656 return ret;
4657 }
4658 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4659
4660 /**
4661 * drm_mode_connector_set_tile_property - set tile property on connector
4662 * @connector: connector to set property on.
4663 *
4664 * This looks up the tile information for a connector, and creates a
4665 * property for userspace to parse if it exists. The property is of
4666 * the form of 8 integers using ':' as a separator.
4667 *
4668 * Returns:
4669 * Zero on success, errno on failure.
4670 */
4671 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4672 {
4673 struct drm_device *dev = connector->dev;
4674 char tile[256];
4675 int ret;
4676
4677 if (!connector->has_tile) {
4678 ret = drm_property_replace_global_blob(dev,
4679 &connector->tile_blob_ptr,
4680 0,
4681 NULL,
4682 &connector->base,
4683 dev->mode_config.tile_property);
4684 return ret;
4685 }
4686
4687 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4688 connector->tile_group->id, connector->tile_is_single_monitor,
4689 connector->num_h_tile, connector->num_v_tile,
4690 connector->tile_h_loc, connector->tile_v_loc,
4691 connector->tile_h_size, connector->tile_v_size);
4692
4693 ret = drm_property_replace_global_blob(dev,
4694 &connector->tile_blob_ptr,
4695 strlen(tile) + 1,
4696 tile,
4697 &connector->base,
4698 dev->mode_config.tile_property);
4699 return ret;
4700 }
4701 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4702
4703 /**
4704 * drm_mode_connector_update_edid_property - update the edid property of a connector
4705 * @connector: drm connector
4706 * @edid: new value of the edid property
4707 *
4708 * This function creates a new blob modeset object and assigns its id to the
4709 * connector's edid property.
4710 *
4711 * Returns:
4712 * Zero on success, negative errno on failure.
4713 */
4714 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4715 const struct edid *edid)
4716 {
4717 struct drm_device *dev = connector->dev;
4718 size_t size = 0;
4719 int ret;
4720
4721 /* ignore requests to set edid when overridden */
4722 if (connector->override_edid)
4723 return 0;
4724
4725 if (edid)
4726 size = EDID_LENGTH * (1 + edid->extensions);
4727
4728 ret = drm_property_replace_global_blob(dev,
4729 &connector->edid_blob_ptr,
4730 size,
4731 edid,
4732 &connector->base,
4733 dev->mode_config.edid_property);
4734 return ret;
4735 }
4736 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4737
4738 /* Some properties could refer to dynamic refcnt'd objects, or things that
4739 * need special locking to handle lifetime issues (ie. to ensure the prop
4740 * value doesn't become invalid part way through the property update due to
4741 * race). The value returned by reference via 'obj' should be passed back
4742 * to drm_property_change_valid_put() after the property is set (and the
4743 * object to which the property is attached has a chance to take it's own
4744 * reference).
4745 */
4746 bool drm_property_change_valid_get(struct drm_property *property,
4747 uint64_t value, struct drm_mode_object **ref)
4748 {
4749 int i;
4750
4751 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4752 return false;
4753
4754 *ref = NULL;
4755
4756 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4757 if (value < property->values[0] || value > property->values[1])
4758 return false;
4759 return true;
4760 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4761 int64_t svalue = U642I64(value);
4762
4763 if (svalue < U642I64(property->values[0]) ||
4764 svalue > U642I64(property->values[1]))
4765 return false;
4766 return true;
4767 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4768 uint64_t valid_mask = 0;
4769
4770 for (i = 0; i < property->num_values; i++)
4771 valid_mask |= (1ULL << property->values[i]);
4772 return !(value & ~valid_mask);
4773 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4774 struct drm_property_blob *blob;
4775
4776 if (value == 0)
4777 return true;
4778
4779 blob = drm_property_lookup_blob(property->dev, value);
4780 if (blob) {
4781 *ref = &blob->base;
4782 return true;
4783 } else {
4784 return false;
4785 }
4786 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4787 /* a zero value for an object property translates to null: */
4788 if (value == 0)
4789 return true;
4790
4791 /* handle refcnt'd objects specially: */
4792 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4793 struct drm_framebuffer *fb;
4794 fb = drm_framebuffer_lookup(property->dev, value);
4795 if (fb) {
4796 *ref = &fb->base;
4797 return true;
4798 } else {
4799 return false;
4800 }
4801 } else {
4802 return _object_find(property->dev, value, property->values[0]) != NULL;
4803 }
4804 }
4805
4806 for (i = 0; i < property->num_values; i++)
4807 if (property->values[i] == value)
4808 return true;
4809 return false;
4810 }
4811
4812 void drm_property_change_valid_put(struct drm_property *property,
4813 struct drm_mode_object *ref)
4814 {
4815 if (!ref)
4816 return;
4817
4818 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4819 if (property->values[0] == DRM_MODE_OBJECT_FB)
4820 drm_framebuffer_unreference(obj_to_fb(ref));
4821 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4822 drm_property_unreference_blob(obj_to_blob(ref));
4823 }
4824
4825 /**
4826 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4827 * @dev: DRM device
4828 * @data: ioctl data
4829 * @file_priv: DRM file info
4830 *
4831 * This function sets the current value for a connectors's property. It also
4832 * calls into a driver's ->set_property callback to update the hardware state
4833 *
4834 * Called by the user via ioctl.
4835 *
4836 * Returns:
4837 * Zero on success, negative errno on failure.
4838 */
4839 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4840 void *data, struct drm_file *file_priv)
4841 {
4842 struct drm_mode_connector_set_property *conn_set_prop = data;
4843 struct drm_mode_obj_set_property obj_set_prop = {
4844 .value = conn_set_prop->value,
4845 .prop_id = conn_set_prop->prop_id,
4846 .obj_id = conn_set_prop->connector_id,
4847 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4848 };
4849
4850 /* It does all the locking and checking we need */
4851 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4852 }
4853
4854 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4855 struct drm_property *property,
4856 uint64_t value)
4857 {
4858 int ret = -EINVAL;
4859 struct drm_connector *connector = obj_to_connector(obj);
4860
4861 /* Do DPMS ourselves */
4862 if (property == connector->dev->mode_config.dpms_property) {
4863 ret = 0;
4864 if (connector->funcs->dpms)
4865 ret = (*connector->funcs->dpms)(connector, (int)value);
4866 } else if (connector->funcs->set_property)
4867 ret = connector->funcs->set_property(connector, property, value);
4868
4869 /* store the property value if successful */
4870 if (!ret)
4871 drm_object_property_set_value(&connector->base, property, value);
4872 return ret;
4873 }
4874
4875 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4876 struct drm_property *property,
4877 uint64_t value)
4878 {
4879 int ret = -EINVAL;
4880 struct drm_crtc *crtc = obj_to_crtc(obj);
4881
4882 if (crtc->funcs->set_property)
4883 ret = crtc->funcs->set_property(crtc, property, value);
4884 if (!ret)
4885 drm_object_property_set_value(obj, property, value);
4886
4887 return ret;
4888 }
4889
4890 /**
4891 * drm_mode_plane_set_obj_prop - set the value of a property
4892 * @plane: drm plane object to set property value for
4893 * @property: property to set
4894 * @value: value the property should be set to
4895 *
4896 * This functions sets a given property on a given plane object. This function
4897 * calls the driver's ->set_property callback and changes the software state of
4898 * the property if the callback succeeds.
4899 *
4900 * Returns:
4901 * Zero on success, error code on failure.
4902 */
4903 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4904 struct drm_property *property,
4905 uint64_t value)
4906 {
4907 int ret = -EINVAL;
4908 struct drm_mode_object *obj = &plane->base;
4909
4910 if (plane->funcs->set_property)
4911 ret = plane->funcs->set_property(plane, property, value);
4912 if (!ret)
4913 drm_object_property_set_value(obj, property, value);
4914
4915 return ret;
4916 }
4917 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4918
4919 /**
4920 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4921 * @dev: DRM device
4922 * @data: ioctl data
4923 * @file_priv: DRM file info
4924 *
4925 * This function retrieves the current value for an object's property. Compared
4926 * to the connector specific ioctl this one is extended to also work on crtc and
4927 * plane objects.
4928 *
4929 * Called by the user via ioctl.
4930 *
4931 * Returns:
4932 * Zero on success, negative errno on failure.
4933 */
4934 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4935 struct drm_file *file_priv)
4936 {
4937 struct drm_mode_obj_get_properties *arg = data;
4938 struct drm_mode_object *obj;
4939 int ret = 0;
4940
4941 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4942 return -EINVAL;
4943
4944 drm_modeset_lock_all(dev);
4945
4946 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4947 if (!obj) {
4948 ret = -ENOENT;
4949 goto out;
4950 }
4951 if (!obj->properties) {
4952 ret = -EINVAL;
4953 goto out;
4954 }
4955
4956 ret = get_properties(obj, file_priv->atomic,
4957 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4958 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4959 &arg->count_props);
4960
4961 out:
4962 drm_modeset_unlock_all(dev);
4963 return ret;
4964 }
4965
4966 /**
4967 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4968 * @dev: DRM device
4969 * @data: ioctl data
4970 * @file_priv: DRM file info
4971 *
4972 * This function sets the current value for an object's property. It also calls
4973 * into a driver's ->set_property callback to update the hardware state.
4974 * Compared to the connector specific ioctl this one is extended to also work on
4975 * crtc and plane objects.
4976 *
4977 * Called by the user via ioctl.
4978 *
4979 * Returns:
4980 * Zero on success, negative errno on failure.
4981 */
4982 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4983 struct drm_file *file_priv)
4984 {
4985 struct drm_mode_obj_set_property *arg = data;
4986 struct drm_mode_object *arg_obj;
4987 struct drm_mode_object *prop_obj;
4988 struct drm_property *property;
4989 int i, ret = -EINVAL;
4990 struct drm_mode_object *ref;
4991
4992 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4993 return -EINVAL;
4994
4995 drm_modeset_lock_all(dev);
4996
4997 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4998 if (!arg_obj) {
4999 ret = -ENOENT;
5000 goto out;
5001 }
5002 if (!arg_obj->properties)
5003 goto out;
5004
5005 for (i = 0; i < arg_obj->properties->count; i++)
5006 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
5007 break;
5008
5009 if (i == arg_obj->properties->count)
5010 goto out;
5011
5012 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5013 DRM_MODE_OBJECT_PROPERTY);
5014 if (!prop_obj) {
5015 ret = -ENOENT;
5016 goto out;
5017 }
5018 property = obj_to_property(prop_obj);
5019
5020 if (!drm_property_change_valid_get(property, arg->value, &ref))
5021 goto out;
5022
5023 switch (arg_obj->type) {
5024 case DRM_MODE_OBJECT_CONNECTOR:
5025 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5026 arg->value);
5027 break;
5028 case DRM_MODE_OBJECT_CRTC:
5029 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5030 break;
5031 case DRM_MODE_OBJECT_PLANE:
5032 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5033 property, arg->value);
5034 break;
5035 }
5036
5037 drm_property_change_valid_put(property, ref);
5038
5039 out:
5040 drm_modeset_unlock_all(dev);
5041 return ret;
5042 }
5043
5044 /**
5045 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5046 * @connector: connector to attach
5047 * @encoder: encoder to attach @connector to
5048 *
5049 * This function links up a connector to an encoder. Note that the routing
5050 * restrictions between encoders and crtcs are exposed to userspace through the
5051 * possible_clones and possible_crtcs bitmasks.
5052 *
5053 * Returns:
5054 * Zero on success, negative errno on failure.
5055 */
5056 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5057 struct drm_encoder *encoder)
5058 {
5059 int i;
5060
5061 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5062 if (connector->encoder_ids[i] == 0) {
5063 connector->encoder_ids[i] = encoder->base.id;
5064 return 0;
5065 }
5066 }
5067 return -ENOMEM;
5068 }
5069 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5070
5071 /**
5072 * drm_mode_crtc_set_gamma_size - set the gamma table size
5073 * @crtc: CRTC to set the gamma table size for
5074 * @gamma_size: size of the gamma table
5075 *
5076 * Drivers which support gamma tables should set this to the supported gamma
5077 * table size when initializing the CRTC. Currently the drm core only supports a
5078 * fixed gamma table size.
5079 *
5080 * Returns:
5081 * Zero on success, negative errno on failure.
5082 */
5083 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
5084 int gamma_size)
5085 {
5086 crtc->gamma_size = gamma_size;
5087
5088 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5089 GFP_KERNEL);
5090 if (!crtc->gamma_store) {
5091 crtc->gamma_size = 0;
5092 return -ENOMEM;
5093 }
5094
5095 return 0;
5096 }
5097 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5098
5099 /**
5100 * drm_mode_gamma_set_ioctl - set the gamma table
5101 * @dev: DRM device
5102 * @data: ioctl data
5103 * @file_priv: DRM file info
5104 *
5105 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5106 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5107 *
5108 * Called by the user via ioctl.
5109 *
5110 * Returns:
5111 * Zero on success, negative errno on failure.
5112 */
5113 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5114 void *data, struct drm_file *file_priv)
5115 {
5116 struct drm_mode_crtc_lut *crtc_lut = data;
5117 struct drm_crtc *crtc;
5118 void *r_base, *g_base, *b_base;
5119 int size;
5120 int ret = 0;
5121
5122 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5123 return -EINVAL;
5124
5125 drm_modeset_lock_all(dev);
5126 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5127 if (!crtc) {
5128 ret = -ENOENT;
5129 goto out;
5130 }
5131
5132 if (crtc->funcs->gamma_set == NULL) {
5133 ret = -ENOSYS;
5134 goto out;
5135 }
5136
5137 /* memcpy into gamma store */
5138 if (crtc_lut->gamma_size != crtc->gamma_size) {
5139 ret = -EINVAL;
5140 goto out;
5141 }
5142
5143 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5144 r_base = crtc->gamma_store;
5145 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5146 ret = -EFAULT;
5147 goto out;
5148 }
5149
5150 g_base = r_base + size;
5151 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5152 ret = -EFAULT;
5153 goto out;
5154 }
5155
5156 b_base = g_base + size;
5157 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5158 ret = -EFAULT;
5159 goto out;
5160 }
5161
5162 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
5163
5164 out:
5165 drm_modeset_unlock_all(dev);
5166 return ret;
5167
5168 }
5169
5170 /**
5171 * drm_mode_gamma_get_ioctl - get the gamma table
5172 * @dev: DRM device
5173 * @data: ioctl data
5174 * @file_priv: DRM file info
5175 *
5176 * Copy the current gamma table into the storage provided. This also provides
5177 * the gamma table size the driver expects, which can be used to size the
5178 * allocated storage.
5179 *
5180 * Called by the user via ioctl.
5181 *
5182 * Returns:
5183 * Zero on success, negative errno on failure.
5184 */
5185 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5186 void *data, struct drm_file *file_priv)
5187 {
5188 struct drm_mode_crtc_lut *crtc_lut = data;
5189 struct drm_crtc *crtc;
5190 void *r_base, *g_base, *b_base;
5191 int size;
5192 int ret = 0;
5193
5194 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5195 return -EINVAL;
5196
5197 drm_modeset_lock_all(dev);
5198 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5199 if (!crtc) {
5200 ret = -ENOENT;
5201 goto out;
5202 }
5203
5204 /* memcpy into gamma store */
5205 if (crtc_lut->gamma_size != crtc->gamma_size) {
5206 ret = -EINVAL;
5207 goto out;
5208 }
5209
5210 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5211 r_base = crtc->gamma_store;
5212 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5213 ret = -EFAULT;
5214 goto out;
5215 }
5216
5217 g_base = r_base + size;
5218 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5219 ret = -EFAULT;
5220 goto out;
5221 }
5222
5223 b_base = g_base + size;
5224 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5225 ret = -EFAULT;
5226 goto out;
5227 }
5228 out:
5229 drm_modeset_unlock_all(dev);
5230 return ret;
5231 }
5232
5233 /**
5234 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5235 * @dev: DRM device
5236 * @data: ioctl data
5237 * @file_priv: DRM file info
5238 *
5239 * This schedules an asynchronous update on a given CRTC, called page flip.
5240 * Optionally a drm event is generated to signal the completion of the event.
5241 * Generic drivers cannot assume that a pageflip with changed framebuffer
5242 * properties (including driver specific metadata like tiling layout) will work,
5243 * but some drivers support e.g. pixel format changes through the pageflip
5244 * ioctl.
5245 *
5246 * Called by the user via ioctl.
5247 *
5248 * Returns:
5249 * Zero on success, negative errno on failure.
5250 */
5251 int drm_mode_page_flip_ioctl(struct drm_device *dev,
5252 void *data, struct drm_file *file_priv)
5253 {
5254 struct drm_mode_crtc_page_flip *page_flip = data;
5255 struct drm_crtc *crtc;
5256 struct drm_framebuffer *fb = NULL;
5257 struct drm_pending_vblank_event *e = NULL;
5258 unsigned long flags;
5259 int ret = -EINVAL;
5260
5261 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5262 return -EINVAL;
5263
5264 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5265 page_flip->reserved != 0)
5266 return -EINVAL;
5267
5268 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5269 return -EINVAL;
5270
5271 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5272 if (!crtc)
5273 return -ENOENT;
5274
5275 drm_modeset_lock_crtc(crtc, crtc->primary);
5276 if (crtc->primary->fb == NULL) {
5277 /* The framebuffer is currently unbound, presumably
5278 * due to a hotplug event, that userspace has not
5279 * yet discovered.
5280 */
5281 ret = -EBUSY;
5282 goto out;
5283 }
5284
5285 if (crtc->funcs->page_flip == NULL)
5286 goto out;
5287
5288 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
5289 if (!fb) {
5290 ret = -ENOENT;
5291 goto out;
5292 }
5293
5294 if (crtc->state) {
5295 const struct drm_plane_state *state = crtc->primary->state;
5296
5297 ret = check_src_coords(state->src_x, state->src_y,
5298 state->src_w, state->src_h, fb);
5299 } else {
5300 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5301 }
5302 if (ret)
5303 goto out;
5304
5305 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
5306 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5307 ret = -EINVAL;
5308 goto out;
5309 }
5310
5311 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5312 ret = -ENOMEM;
5313 spin_lock_irqsave(&dev->event_lock, flags);
5314 if (file_priv->event_space < sizeof(e->event)) {
5315 spin_unlock_irqrestore(&dev->event_lock, flags);
5316 goto out;
5317 }
5318 file_priv->event_space -= sizeof(e->event);
5319 spin_unlock_irqrestore(&dev->event_lock, flags);
5320
5321 e = kzalloc(sizeof(*e), GFP_KERNEL);
5322 if (e == NULL) {
5323 spin_lock_irqsave(&dev->event_lock, flags);
5324 file_priv->event_space += sizeof(e->event);
5325 spin_unlock_irqrestore(&dev->event_lock, flags);
5326 goto out;
5327 }
5328
5329 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
5330 e->event.base.length = sizeof(e->event);
5331 e->event.user_data = page_flip->user_data;
5332 e->base.event = &e->event.base;
5333 e->base.file_priv = file_priv;
5334 e->base.destroy =
5335 (void (*) (struct drm_pending_event *)) kfree;
5336 }
5337
5338 crtc->primary->old_fb = crtc->primary->fb;
5339 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
5340 if (ret) {
5341 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5342 spin_lock_irqsave(&dev->event_lock, flags);
5343 file_priv->event_space += sizeof(e->event);
5344 spin_unlock_irqrestore(&dev->event_lock, flags);
5345 kfree(e);
5346 }
5347 /* Keep the old fb, don't unref it. */
5348 crtc->primary->old_fb = NULL;
5349 } else {
5350 crtc->primary->fb = fb;
5351 /* Unref only the old framebuffer. */
5352 fb = NULL;
5353 }
5354
5355 out:
5356 if (fb)
5357 drm_framebuffer_unreference(fb);
5358 if (crtc->primary->old_fb)
5359 drm_framebuffer_unreference(crtc->primary->old_fb);
5360 crtc->primary->old_fb = NULL;
5361 drm_modeset_unlock_crtc(crtc);
5362
5363 return ret;
5364 }
5365
5366 /**
5367 * drm_mode_config_reset - call ->reset callbacks
5368 * @dev: drm device
5369 *
5370 * This functions calls all the crtc's, encoder's and connector's ->reset
5371 * callback. Drivers can use this in e.g. their driver load or resume code to
5372 * reset hardware and software state.
5373 */
5374 void drm_mode_config_reset(struct drm_device *dev)
5375 {
5376 struct drm_crtc *crtc;
5377 struct drm_plane *plane;
5378 struct drm_encoder *encoder;
5379 struct drm_connector *connector;
5380
5381 drm_for_each_plane(plane, dev)
5382 if (plane->funcs->reset)
5383 plane->funcs->reset(plane);
5384
5385 drm_for_each_crtc(crtc, dev)
5386 if (crtc->funcs->reset)
5387 crtc->funcs->reset(crtc);
5388
5389 drm_for_each_encoder(encoder, dev)
5390 if (encoder->funcs->reset)
5391 encoder->funcs->reset(encoder);
5392
5393 mutex_lock(&dev->mode_config.mutex);
5394 drm_for_each_connector(connector, dev)
5395 if (connector->funcs->reset)
5396 connector->funcs->reset(connector);
5397 mutex_unlock(&dev->mode_config.mutex);
5398 }
5399 EXPORT_SYMBOL(drm_mode_config_reset);
5400
5401 /**
5402 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5403 * @dev: DRM device
5404 * @data: ioctl data
5405 * @file_priv: DRM file info
5406 *
5407 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5408 * TTM or something else entirely) and returns the resulting buffer handle. This
5409 * handle can then be wrapped up into a framebuffer modeset object.
5410 *
5411 * Note that userspace is not allowed to use such objects for render
5412 * acceleration - drivers must create their own private ioctls for such a use
5413 * case.
5414 *
5415 * Called by the user via ioctl.
5416 *
5417 * Returns:
5418 * Zero on success, negative errno on failure.
5419 */
5420 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5421 void *data, struct drm_file *file_priv)
5422 {
5423 struct drm_mode_create_dumb *args = data;
5424 u32 cpp, stride, size;
5425
5426 if (!dev->driver->dumb_create)
5427 return -ENOSYS;
5428 if (!args->width || !args->height || !args->bpp)
5429 return -EINVAL;
5430
5431 /* overflow checks for 32bit size calculations */
5432 /* NOTE: DIV_ROUND_UP() can overflow */
5433 cpp = DIV_ROUND_UP(args->bpp, 8);
5434 if (!cpp || cpp > 0xffffffffU / args->width)
5435 return -EINVAL;
5436 stride = cpp * args->width;
5437 if (args->height > 0xffffffffU / stride)
5438 return -EINVAL;
5439
5440 /* test for wrap-around */
5441 size = args->height * stride;
5442 if (PAGE_ALIGN(size) == 0)
5443 return -EINVAL;
5444
5445 /*
5446 * handle, pitch and size are output parameters. Zero them out to
5447 * prevent drivers from accidentally using uninitialized data. Since
5448 * not all existing userspace is clearing these fields properly we
5449 * cannot reject IOCTL with garbage in them.
5450 */
5451 args->handle = 0;
5452 args->pitch = 0;
5453 args->size = 0;
5454
5455 return dev->driver->dumb_create(file_priv, dev, args);
5456 }
5457
5458 /**
5459 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5460 * @dev: DRM device
5461 * @data: ioctl data
5462 * @file_priv: DRM file info
5463 *
5464 * Allocate an offset in the drm device node's address space to be able to
5465 * memory map a dumb buffer.
5466 *
5467 * Called by the user via ioctl.
5468 *
5469 * Returns:
5470 * Zero on success, negative errno on failure.
5471 */
5472 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5473 void *data, struct drm_file *file_priv)
5474 {
5475 struct drm_mode_map_dumb *args = data;
5476
5477 /* call driver ioctl to get mmap offset */
5478 if (!dev->driver->dumb_map_offset)
5479 return -ENOSYS;
5480
5481 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5482 }
5483
5484 /**
5485 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5486 * @dev: DRM device
5487 * @data: ioctl data
5488 * @file_priv: DRM file info
5489 *
5490 * This destroys the userspace handle for the given dumb backing storage buffer.
5491 * Since buffer objects must be reference counted in the kernel a buffer object
5492 * won't be immediately freed if a framebuffer modeset object still uses it.
5493 *
5494 * Called by the user via ioctl.
5495 *
5496 * Returns:
5497 * Zero on success, negative errno on failure.
5498 */
5499 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5500 void *data, struct drm_file *file_priv)
5501 {
5502 struct drm_mode_destroy_dumb *args = data;
5503
5504 if (!dev->driver->dumb_destroy)
5505 return -ENOSYS;
5506
5507 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5508 }
5509
5510 /**
5511 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5512 * @format: pixel format (DRM_FORMAT_*)
5513 * @depth: storage for the depth value
5514 * @bpp: storage for the bpp value
5515 *
5516 * This only supports RGB formats here for compat with code that doesn't use
5517 * pixel formats directly yet.
5518 */
5519 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5520 int *bpp)
5521 {
5522 switch (format) {
5523 case DRM_FORMAT_C8:
5524 case DRM_FORMAT_RGB332:
5525 case DRM_FORMAT_BGR233:
5526 *depth = 8;
5527 *bpp = 8;
5528 break;
5529 case DRM_FORMAT_XRGB1555:
5530 case DRM_FORMAT_XBGR1555:
5531 case DRM_FORMAT_RGBX5551:
5532 case DRM_FORMAT_BGRX5551:
5533 case DRM_FORMAT_ARGB1555:
5534 case DRM_FORMAT_ABGR1555:
5535 case DRM_FORMAT_RGBA5551:
5536 case DRM_FORMAT_BGRA5551:
5537 *depth = 15;
5538 *bpp = 16;
5539 break;
5540 case DRM_FORMAT_RGB565:
5541 case DRM_FORMAT_BGR565:
5542 *depth = 16;
5543 *bpp = 16;
5544 break;
5545 case DRM_FORMAT_RGB888:
5546 case DRM_FORMAT_BGR888:
5547 *depth = 24;
5548 *bpp = 24;
5549 break;
5550 case DRM_FORMAT_XRGB8888:
5551 case DRM_FORMAT_XBGR8888:
5552 case DRM_FORMAT_RGBX8888:
5553 case DRM_FORMAT_BGRX8888:
5554 *depth = 24;
5555 *bpp = 32;
5556 break;
5557 case DRM_FORMAT_XRGB2101010:
5558 case DRM_FORMAT_XBGR2101010:
5559 case DRM_FORMAT_RGBX1010102:
5560 case DRM_FORMAT_BGRX1010102:
5561 case DRM_FORMAT_ARGB2101010:
5562 case DRM_FORMAT_ABGR2101010:
5563 case DRM_FORMAT_RGBA1010102:
5564 case DRM_FORMAT_BGRA1010102:
5565 *depth = 30;
5566 *bpp = 32;
5567 break;
5568 case DRM_FORMAT_ARGB8888:
5569 case DRM_FORMAT_ABGR8888:
5570 case DRM_FORMAT_RGBA8888:
5571 case DRM_FORMAT_BGRA8888:
5572 *depth = 32;
5573 *bpp = 32;
5574 break;
5575 default:
5576 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5577 drm_get_format_name(format));
5578 *depth = 0;
5579 *bpp = 0;
5580 break;
5581 }
5582 }
5583 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
5584
5585 /**
5586 * drm_format_num_planes - get the number of planes for format
5587 * @format: pixel format (DRM_FORMAT_*)
5588 *
5589 * Returns:
5590 * The number of planes used by the specified pixel format.
5591 */
5592 int drm_format_num_planes(uint32_t format)
5593 {
5594 switch (format) {
5595 case DRM_FORMAT_YUV410:
5596 case DRM_FORMAT_YVU410:
5597 case DRM_FORMAT_YUV411:
5598 case DRM_FORMAT_YVU411:
5599 case DRM_FORMAT_YUV420:
5600 case DRM_FORMAT_YVU420:
5601 case DRM_FORMAT_YUV422:
5602 case DRM_FORMAT_YVU422:
5603 case DRM_FORMAT_YUV444:
5604 case DRM_FORMAT_YVU444:
5605 return 3;
5606 case DRM_FORMAT_NV12:
5607 case DRM_FORMAT_NV21:
5608 case DRM_FORMAT_NV16:
5609 case DRM_FORMAT_NV61:
5610 case DRM_FORMAT_NV24:
5611 case DRM_FORMAT_NV42:
5612 return 2;
5613 default:
5614 return 1;
5615 }
5616 }
5617 EXPORT_SYMBOL(drm_format_num_planes);
5618
5619 /**
5620 * drm_format_plane_cpp - determine the bytes per pixel value
5621 * @format: pixel format (DRM_FORMAT_*)
5622 * @plane: plane index
5623 *
5624 * Returns:
5625 * The bytes per pixel value for the specified plane.
5626 */
5627 int drm_format_plane_cpp(uint32_t format, int plane)
5628 {
5629 unsigned int depth;
5630 int bpp;
5631
5632 if (plane >= drm_format_num_planes(format))
5633 return 0;
5634
5635 switch (format) {
5636 case DRM_FORMAT_YUYV:
5637 case DRM_FORMAT_YVYU:
5638 case DRM_FORMAT_UYVY:
5639 case DRM_FORMAT_VYUY:
5640 return 2;
5641 case DRM_FORMAT_NV12:
5642 case DRM_FORMAT_NV21:
5643 case DRM_FORMAT_NV16:
5644 case DRM_FORMAT_NV61:
5645 case DRM_FORMAT_NV24:
5646 case DRM_FORMAT_NV42:
5647 return plane ? 2 : 1;
5648 case DRM_FORMAT_YUV410:
5649 case DRM_FORMAT_YVU410:
5650 case DRM_FORMAT_YUV411:
5651 case DRM_FORMAT_YVU411:
5652 case DRM_FORMAT_YUV420:
5653 case DRM_FORMAT_YVU420:
5654 case DRM_FORMAT_YUV422:
5655 case DRM_FORMAT_YVU422:
5656 case DRM_FORMAT_YUV444:
5657 case DRM_FORMAT_YVU444:
5658 return 1;
5659 default:
5660 drm_fb_get_bpp_depth(format, &depth, &bpp);
5661 return bpp >> 3;
5662 }
5663 }
5664 EXPORT_SYMBOL(drm_format_plane_cpp);
5665
5666 /**
5667 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5668 * @format: pixel format (DRM_FORMAT_*)
5669 *
5670 * Returns:
5671 * The horizontal chroma subsampling factor for the
5672 * specified pixel format.
5673 */
5674 int drm_format_horz_chroma_subsampling(uint32_t format)
5675 {
5676 switch (format) {
5677 case DRM_FORMAT_YUV411:
5678 case DRM_FORMAT_YVU411:
5679 case DRM_FORMAT_YUV410:
5680 case DRM_FORMAT_YVU410:
5681 return 4;
5682 case DRM_FORMAT_YUYV:
5683 case DRM_FORMAT_YVYU:
5684 case DRM_FORMAT_UYVY:
5685 case DRM_FORMAT_VYUY:
5686 case DRM_FORMAT_NV12:
5687 case DRM_FORMAT_NV21:
5688 case DRM_FORMAT_NV16:
5689 case DRM_FORMAT_NV61:
5690 case DRM_FORMAT_YUV422:
5691 case DRM_FORMAT_YVU422:
5692 case DRM_FORMAT_YUV420:
5693 case DRM_FORMAT_YVU420:
5694 return 2;
5695 default:
5696 return 1;
5697 }
5698 }
5699 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5700
5701 /**
5702 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5703 * @format: pixel format (DRM_FORMAT_*)
5704 *
5705 * Returns:
5706 * The vertical chroma subsampling factor for the
5707 * specified pixel format.
5708 */
5709 int drm_format_vert_chroma_subsampling(uint32_t format)
5710 {
5711 switch (format) {
5712 case DRM_FORMAT_YUV410:
5713 case DRM_FORMAT_YVU410:
5714 return 4;
5715 case DRM_FORMAT_YUV420:
5716 case DRM_FORMAT_YVU420:
5717 case DRM_FORMAT_NV12:
5718 case DRM_FORMAT_NV21:
5719 return 2;
5720 default:
5721 return 1;
5722 }
5723 }
5724 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5725
5726 /**
5727 * drm_rotation_simplify() - Try to simplify the rotation
5728 * @rotation: Rotation to be simplified
5729 * @supported_rotations: Supported rotations
5730 *
5731 * Attempt to simplify the rotation to a form that is supported.
5732 * Eg. if the hardware supports everything except DRM_REFLECT_X
5733 * one could call this function like this:
5734 *
5735 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5736 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5737 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5738 *
5739 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5740 * transforms the hardware supports, this function may not
5741 * be able to produce a supported transform, so the caller should
5742 * check the result afterwards.
5743 */
5744 unsigned int drm_rotation_simplify(unsigned int rotation,
5745 unsigned int supported_rotations)
5746 {
5747 if (rotation & ~supported_rotations) {
5748 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5749 rotation = (rotation & DRM_REFLECT_MASK) |
5750 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
5751 }
5752
5753 return rotation;
5754 }
5755 EXPORT_SYMBOL(drm_rotation_simplify);
5756
5757 /**
5758 * drm_mode_config_init - initialize DRM mode_configuration structure
5759 * @dev: DRM device
5760 *
5761 * Initialize @dev's mode_config structure, used for tracking the graphics
5762 * configuration of @dev.
5763 *
5764 * Since this initializes the modeset locks, no locking is possible. Which is no
5765 * problem, since this should happen single threaded at init time. It is the
5766 * driver's problem to ensure this guarantee.
5767 *
5768 */
5769 void drm_mode_config_init(struct drm_device *dev)
5770 {
5771 mutex_init(&dev->mode_config.mutex);
5772 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5773 mutex_init(&dev->mode_config.idr_mutex);
5774 mutex_init(&dev->mode_config.fb_lock);
5775 mutex_init(&dev->mode_config.blob_lock);
5776 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5777 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5778 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5779 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5780 INIT_LIST_HEAD(&dev->mode_config.property_list);
5781 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5782 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5783 idr_init(&dev->mode_config.crtc_idr);
5784 idr_init(&dev->mode_config.tile_idr);
5785
5786 drm_modeset_lock_all(dev);
5787 drm_mode_create_standard_properties(dev);
5788 drm_modeset_unlock_all(dev);
5789
5790 /* Just to be sure */
5791 dev->mode_config.num_fb = 0;
5792 dev->mode_config.num_connector = 0;
5793 dev->mode_config.num_crtc = 0;
5794 dev->mode_config.num_encoder = 0;
5795 dev->mode_config.num_overlay_plane = 0;
5796 dev->mode_config.num_total_plane = 0;
5797 }
5798 EXPORT_SYMBOL(drm_mode_config_init);
5799
5800 /**
5801 * drm_mode_config_cleanup - free up DRM mode_config info
5802 * @dev: DRM device
5803 *
5804 * Free up all the connectors and CRTCs associated with this DRM device, then
5805 * free up the framebuffers and associated buffer objects.
5806 *
5807 * Note that since this /should/ happen single-threaded at driver/device
5808 * teardown time, no locking is required. It's the driver's job to ensure that
5809 * this guarantee actually holds true.
5810 *
5811 * FIXME: cleanup any dangling user buffer objects too
5812 */
5813 void drm_mode_config_cleanup(struct drm_device *dev)
5814 {
5815 struct drm_connector *connector, *ot;
5816 struct drm_crtc *crtc, *ct;
5817 struct drm_encoder *encoder, *enct;
5818 struct drm_framebuffer *fb, *fbt;
5819 struct drm_property *property, *pt;
5820 struct drm_property_blob *blob, *bt;
5821 struct drm_plane *plane, *plt;
5822
5823 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5824 head) {
5825 encoder->funcs->destroy(encoder);
5826 }
5827
5828 list_for_each_entry_safe(connector, ot,
5829 &dev->mode_config.connector_list, head) {
5830 connector->funcs->destroy(connector);
5831 }
5832
5833 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5834 head) {
5835 drm_property_destroy(dev, property);
5836 }
5837
5838 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5839 head_global) {
5840 drm_property_unreference_blob(blob);
5841 }
5842
5843 /*
5844 * Single-threaded teardown context, so it's not required to grab the
5845 * fb_lock to protect against concurrent fb_list access. Contrary, it
5846 * would actually deadlock with the drm_framebuffer_cleanup function.
5847 *
5848 * Also, if there are any framebuffers left, that's a driver leak now,
5849 * so politely WARN about this.
5850 */
5851 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5852 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5853 drm_framebuffer_free(&fb->refcount);
5854 }
5855
5856 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5857 head) {
5858 plane->funcs->destroy(plane);
5859 }
5860
5861 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5862 crtc->funcs->destroy(crtc);
5863 }
5864
5865 idr_destroy(&dev->mode_config.tile_idr);
5866 idr_destroy(&dev->mode_config.crtc_idr);
5867 mutex_destroy(&dev->mode_config.blob_lock);
5868 mutex_destroy(&dev->mode_config.fb_lock);
5869 mutex_destroy(&dev->mode_config.idr_mutex);
5870 mutex_destroy(&dev->mode_config.mutex);
5871 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5872 }
5873 EXPORT_SYMBOL(drm_mode_config_cleanup);
5874
5875 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5876 unsigned int supported_rotations)
5877 {
5878 static const struct drm_prop_enum_list props[] = {
5879 { DRM_ROTATE_0, "rotate-0" },
5880 { DRM_ROTATE_90, "rotate-90" },
5881 { DRM_ROTATE_180, "rotate-180" },
5882 { DRM_ROTATE_270, "rotate-270" },
5883 { DRM_REFLECT_X, "reflect-x" },
5884 { DRM_REFLECT_Y, "reflect-y" },
5885 };
5886
5887 return drm_property_create_bitmask(dev, 0, "rotation",
5888 props, ARRAY_SIZE(props),
5889 supported_rotations);
5890 }
5891 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5892
5893 /**
5894 * DOC: Tile group
5895 *
5896 * Tile groups are used to represent tiled monitors with a unique
5897 * integer identifier. Tiled monitors using DisplayID v1.3 have
5898 * a unique 8-byte handle, we store this in a tile group, so we
5899 * have a common identifier for all tiles in a monitor group.
5900 */
5901 static void drm_tile_group_free(struct kref *kref)
5902 {
5903 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5904 struct drm_device *dev = tg->dev;
5905 mutex_lock(&dev->mode_config.idr_mutex);
5906 idr_remove(&dev->mode_config.tile_idr, tg->id);
5907 mutex_unlock(&dev->mode_config.idr_mutex);
5908 kfree(tg);
5909 }
5910
5911 /**
5912 * drm_mode_put_tile_group - drop a reference to a tile group.
5913 * @dev: DRM device
5914 * @tg: tile group to drop reference to.
5915 *
5916 * drop reference to tile group and free if 0.
5917 */
5918 void drm_mode_put_tile_group(struct drm_device *dev,
5919 struct drm_tile_group *tg)
5920 {
5921 kref_put(&tg->refcount, drm_tile_group_free);
5922 }
5923
5924 /**
5925 * drm_mode_get_tile_group - get a reference to an existing tile group
5926 * @dev: DRM device
5927 * @topology: 8-bytes unique per monitor.
5928 *
5929 * Use the unique bytes to get a reference to an existing tile group.
5930 *
5931 * RETURNS:
5932 * tile group or NULL if not found.
5933 */
5934 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5935 char topology[8])
5936 {
5937 struct drm_tile_group *tg;
5938 int id;
5939 mutex_lock(&dev->mode_config.idr_mutex);
5940 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5941 if (!memcmp(tg->group_data, topology, 8)) {
5942 if (!kref_get_unless_zero(&tg->refcount))
5943 tg = NULL;
5944 mutex_unlock(&dev->mode_config.idr_mutex);
5945 return tg;
5946 }
5947 }
5948 mutex_unlock(&dev->mode_config.idr_mutex);
5949 return NULL;
5950 }
5951 EXPORT_SYMBOL(drm_mode_get_tile_group);
5952
5953 /**
5954 * drm_mode_create_tile_group - create a tile group from a displayid description
5955 * @dev: DRM device
5956 * @topology: 8-bytes unique per monitor.
5957 *
5958 * Create a tile group for the unique monitor, and get a unique
5959 * identifier for the tile group.
5960 *
5961 * RETURNS:
5962 * new tile group or error.
5963 */
5964 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5965 char topology[8])
5966 {
5967 struct drm_tile_group *tg;
5968 int ret;
5969
5970 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5971 if (!tg)
5972 return ERR_PTR(-ENOMEM);
5973
5974 kref_init(&tg->refcount);
5975 memcpy(tg->group_data, topology, 8);
5976 tg->dev = dev;
5977
5978 idr_preload(GFP_KERNEL);
5979 mutex_lock(&dev->mode_config.idr_mutex);
5980 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5981 if (ret >= 0) {
5982 tg->id = ret;
5983 } else {
5984 kfree(tg);
5985 tg = ERR_PTR(ret);
5986 }
5987
5988 mutex_unlock(&dev->mode_config.idr_mutex);
5989 idr_preload_end();
5990 return tg;
5991 }
5992 EXPORT_SYMBOL(drm_mode_create_tile_group);
5993