drm_crtc.c revision 1.13 1 /* $NetBSD: drm_crtc.c,v 1.13 2020/02/14 04:35:19 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.13 2020/02/14 04:35:19 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 static const struct drm_mode_fb_cmd2 zero_fbcmd;
3069 struct drm_mode_fb_cmd2 r = zero_fbcmd;
3070 int ret;
3071
3072 /* convert to new format and call new ioctl */
3073 r.fb_id = or->fb_id;
3074 r.width = or->width;
3075 r.height = or->height;
3076 r.pitches[0] = or->pitch;
3077 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3078 r.handles[0] = or->handle;
3079
3080 ret = drm_mode_addfb2(dev, &r, file_priv);
3081 if (ret)
3082 return ret;
3083
3084 or->fb_id = r.fb_id;
3085
3086 return 0;
3087 }
3088
3089 static int format_check(const struct drm_mode_fb_cmd2 *r)
3090 {
3091 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3092
3093 switch (format) {
3094 case DRM_FORMAT_C8:
3095 case DRM_FORMAT_RGB332:
3096 case DRM_FORMAT_BGR233:
3097 case DRM_FORMAT_XRGB4444:
3098 case DRM_FORMAT_XBGR4444:
3099 case DRM_FORMAT_RGBX4444:
3100 case DRM_FORMAT_BGRX4444:
3101 case DRM_FORMAT_ARGB4444:
3102 case DRM_FORMAT_ABGR4444:
3103 case DRM_FORMAT_RGBA4444:
3104 case DRM_FORMAT_BGRA4444:
3105 case DRM_FORMAT_XRGB1555:
3106 case DRM_FORMAT_XBGR1555:
3107 case DRM_FORMAT_RGBX5551:
3108 case DRM_FORMAT_BGRX5551:
3109 case DRM_FORMAT_ARGB1555:
3110 case DRM_FORMAT_ABGR1555:
3111 case DRM_FORMAT_RGBA5551:
3112 case DRM_FORMAT_BGRA5551:
3113 case DRM_FORMAT_RGB565:
3114 case DRM_FORMAT_BGR565:
3115 case DRM_FORMAT_RGB888:
3116 case DRM_FORMAT_BGR888:
3117 case DRM_FORMAT_XRGB8888:
3118 case DRM_FORMAT_XBGR8888:
3119 case DRM_FORMAT_RGBX8888:
3120 case DRM_FORMAT_BGRX8888:
3121 case DRM_FORMAT_ARGB8888:
3122 case DRM_FORMAT_ABGR8888:
3123 case DRM_FORMAT_RGBA8888:
3124 case DRM_FORMAT_BGRA8888:
3125 case DRM_FORMAT_XRGB2101010:
3126 case DRM_FORMAT_XBGR2101010:
3127 case DRM_FORMAT_RGBX1010102:
3128 case DRM_FORMAT_BGRX1010102:
3129 case DRM_FORMAT_ARGB2101010:
3130 case DRM_FORMAT_ABGR2101010:
3131 case DRM_FORMAT_RGBA1010102:
3132 case DRM_FORMAT_BGRA1010102:
3133 case DRM_FORMAT_YUYV:
3134 case DRM_FORMAT_YVYU:
3135 case DRM_FORMAT_UYVY:
3136 case DRM_FORMAT_VYUY:
3137 case DRM_FORMAT_AYUV:
3138 case DRM_FORMAT_NV12:
3139 case DRM_FORMAT_NV21:
3140 case DRM_FORMAT_NV16:
3141 case DRM_FORMAT_NV61:
3142 case DRM_FORMAT_NV24:
3143 case DRM_FORMAT_NV42:
3144 case DRM_FORMAT_YUV410:
3145 case DRM_FORMAT_YVU410:
3146 case DRM_FORMAT_YUV411:
3147 case DRM_FORMAT_YVU411:
3148 case DRM_FORMAT_YUV420:
3149 case DRM_FORMAT_YVU420:
3150 case DRM_FORMAT_YUV422:
3151 case DRM_FORMAT_YVU422:
3152 case DRM_FORMAT_YUV444:
3153 case DRM_FORMAT_YVU444:
3154 return 0;
3155 default:
3156 DRM_DEBUG_KMS("invalid pixel format %s\n",
3157 drm_get_format_name(r->pixel_format));
3158 return -EINVAL;
3159 }
3160 }
3161
3162 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3163 {
3164 int ret, hsub, vsub, num_planes, i;
3165
3166 ret = format_check(r);
3167 if (ret) {
3168 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3169 drm_get_format_name(r->pixel_format));
3170 return ret;
3171 }
3172
3173 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3174 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3175 num_planes = drm_format_num_planes(r->pixel_format);
3176
3177 if (r->width == 0 || r->width % hsub) {
3178 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3179 return -EINVAL;
3180 }
3181
3182 if (r->height == 0 || r->height % vsub) {
3183 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3184 return -EINVAL;
3185 }
3186
3187 for (i = 0; i < num_planes; i++) {
3188 unsigned int width = r->width / (i != 0 ? hsub : 1);
3189 unsigned int height = r->height / (i != 0 ? vsub : 1);
3190 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3191
3192 if (!r->handles[i]) {
3193 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3194 return -EINVAL;
3195 }
3196
3197 if ((uint64_t) width * cpp > UINT_MAX)
3198 return -ERANGE;
3199
3200 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3201 return -ERANGE;
3202
3203 if (r->pitches[i] < width * cpp) {
3204 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3205 return -EINVAL;
3206 }
3207
3208 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3209 DRM_DEBUG_KMS("bad fb modifier %"PRIu64" for plane %d\n",
3210 r->modifier[i], i);
3211 return -EINVAL;
3212 }
3213
3214 /* modifier specific checks: */
3215 switch (r->modifier[i]) {
3216 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3217 /* NOTE: the pitch restriction may be lifted later if it turns
3218 * out that no hw has this restriction:
3219 */
3220 if (r->pixel_format != DRM_FORMAT_NV12 ||
3221 width % 128 || height % 32 ||
3222 r->pitches[i] % 128) {
3223 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3224 return -EINVAL;
3225 }
3226 break;
3227
3228 default:
3229 break;
3230 }
3231 }
3232
3233 for (i = num_planes; i < 4; i++) {
3234 if (r->modifier[i]) {
3235 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3236 return -EINVAL;
3237 }
3238
3239 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3240 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3241 continue;
3242
3243 if (r->handles[i]) {
3244 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3245 return -EINVAL;
3246 }
3247
3248 if (r->pitches[i]) {
3249 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3250 return -EINVAL;
3251 }
3252
3253 if (r->offsets[i]) {
3254 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3255 return -EINVAL;
3256 }
3257 }
3258
3259 return 0;
3260 }
3261
3262 static struct drm_framebuffer *
3263 internal_framebuffer_create(struct drm_device *dev,
3264 struct drm_mode_fb_cmd2 *r,
3265 struct drm_file *file_priv)
3266 {
3267 struct drm_mode_config *config = &dev->mode_config;
3268 struct drm_framebuffer *fb;
3269 int ret;
3270
3271 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
3272 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3273 return ERR_PTR(-EINVAL);
3274 }
3275
3276 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3277 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3278 r->width, config->min_width, config->max_width);
3279 return ERR_PTR(-EINVAL);
3280 }
3281 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3282 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3283 r->height, config->min_height, config->max_height);
3284 return ERR_PTR(-EINVAL);
3285 }
3286
3287 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3288 !dev->mode_config.allow_fb_modifiers) {
3289 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3290 return ERR_PTR(-EINVAL);
3291 }
3292
3293 ret = framebuffer_check(r);
3294 if (ret)
3295 return ERR_PTR(ret);
3296
3297 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3298 if (IS_ERR(fb)) {
3299 DRM_DEBUG_KMS("could not create framebuffer\n");
3300 return fb;
3301 }
3302
3303 return fb;
3304 }
3305
3306 /**
3307 * drm_mode_addfb2 - add an FB to the graphics configuration
3308 * @dev: drm device for the ioctl
3309 * @data: data pointer for the ioctl
3310 * @file_priv: drm file for the ioctl call
3311 *
3312 * Add a new FB to the specified CRTC, given a user request with format. This is
3313 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3314 * and uses fourcc codes as pixel format specifiers.
3315 *
3316 * Called by the user via ioctl.
3317 *
3318 * Returns:
3319 * Zero on success, negative errno on failure.
3320 */
3321 int drm_mode_addfb2(struct drm_device *dev,
3322 void *data, struct drm_file *file_priv)
3323 {
3324 struct drm_mode_fb_cmd2 *r = data;
3325 struct drm_framebuffer *fb;
3326
3327 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3328 return -EINVAL;
3329
3330 fb = internal_framebuffer_create(dev, r, file_priv);
3331 if (IS_ERR(fb))
3332 return PTR_ERR(fb);
3333
3334 /* Transfer ownership to the filp for reaping on close */
3335
3336 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3337 mutex_lock(&file_priv->fbs_lock);
3338 r->fb_id = fb->base.id;
3339 list_add(&fb->filp_head, &file_priv->fbs);
3340 mutex_unlock(&file_priv->fbs_lock);
3341
3342 return 0;
3343 }
3344
3345 struct drm_mode_rmfb_work {
3346 struct work_struct work;
3347 struct list_head fbs;
3348 };
3349
3350 static void drm_mode_rmfb_work_fn(struct work_struct *w)
3351 {
3352 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3353
3354 while (!list_empty(&arg->fbs)) {
3355 struct drm_framebuffer *fb =
3356 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3357
3358 list_del_init(&fb->filp_head);
3359 drm_framebuffer_remove(fb);
3360 }
3361 }
3362
3363 /**
3364 * drm_mode_rmfb - remove an FB from the configuration
3365 * @dev: drm device for the ioctl
3366 * @data: data pointer for the ioctl
3367 * @file_priv: drm file for the ioctl call
3368 *
3369 * Remove the FB specified by the user.
3370 *
3371 * Called by the user via ioctl.
3372 *
3373 * Returns:
3374 * Zero on success, negative errno on failure.
3375 */
3376 int drm_mode_rmfb(struct drm_device *dev,
3377 void *data, struct drm_file *file_priv)
3378 {
3379 struct drm_framebuffer *fb = NULL;
3380 struct drm_framebuffer *fbl = NULL;
3381 uint32_t *id = data;
3382 int found = 0;
3383
3384 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3385 return -EINVAL;
3386
3387 mutex_lock(&file_priv->fbs_lock);
3388 mutex_lock(&dev->mode_config.fb_lock);
3389 fb = __drm_framebuffer_lookup(dev, *id);
3390 if (!fb)
3391 goto fail_lookup;
3392
3393 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3394 if (fb == fbl)
3395 found = 1;
3396 if (!found)
3397 goto fail_lookup;
3398
3399 list_del_init(&fb->filp_head);
3400 mutex_unlock(&dev->mode_config.fb_lock);
3401 mutex_unlock(&file_priv->fbs_lock);
3402
3403 /*
3404 * we now own the reference that was stored in the fbs list
3405 *
3406 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3407 * so run this in a separate stack as there's no way to correctly
3408 * handle this after the fb is already removed from the lookup table.
3409 */
3410 if (!kref_exclusive_p(&fb->refcount)) {
3411 struct drm_mode_rmfb_work arg;
3412
3413 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3414 INIT_LIST_HEAD(&arg.fbs);
3415 list_add_tail(&fb->filp_head, &arg.fbs);
3416
3417 schedule_work(&arg.work);
3418 flush_work(&arg.work);
3419 destroy_work_on_stack(&arg.work);
3420 } else
3421 drm_framebuffer_unreference(fb);
3422
3423 return 0;
3424
3425 fail_lookup:
3426 mutex_unlock(&dev->mode_config.fb_lock);
3427 mutex_unlock(&file_priv->fbs_lock);
3428
3429 return -ENOENT;
3430 }
3431
3432 /**
3433 * drm_mode_getfb - get FB info
3434 * @dev: drm device for the ioctl
3435 * @data: data pointer for the ioctl
3436 * @file_priv: drm file for the ioctl call
3437 *
3438 * Lookup the FB given its ID and return info about it.
3439 *
3440 * Called by the user via ioctl.
3441 *
3442 * Returns:
3443 * Zero on success, negative errno on failure.
3444 */
3445 int drm_mode_getfb(struct drm_device *dev,
3446 void *data, struct drm_file *file_priv)
3447 {
3448 struct drm_mode_fb_cmd *r = data;
3449 struct drm_framebuffer *fb;
3450 int ret;
3451
3452 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3453 return -EINVAL;
3454
3455 fb = drm_framebuffer_lookup(dev, r->fb_id);
3456 if (!fb)
3457 return -ENOENT;
3458
3459 r->height = fb->height;
3460 r->width = fb->width;
3461 r->depth = fb->depth;
3462 r->bpp = fb->bits_per_pixel;
3463 r->pitch = fb->pitches[0];
3464 if (fb->funcs->create_handle) {
3465 if (file_priv->is_master ||
3466 #ifdef __NetBSD__
3467 DRM_SUSER() ||
3468 #else
3469 capable(CAP_SYS_ADMIN) ||
3470 #endif
3471 drm_is_control_client(file_priv)) {
3472 ret = fb->funcs->create_handle(fb, file_priv,
3473 &r->handle);
3474 } else {
3475 /* GET_FB() is an unprivileged ioctl so we must not
3476 * return a buffer-handle to non-master processes! For
3477 * backwards-compatibility reasons, we cannot make
3478 * GET_FB() privileged, so just return an invalid handle
3479 * for non-masters. */
3480 r->handle = 0;
3481 ret = 0;
3482 }
3483 } else {
3484 ret = -ENODEV;
3485 }
3486
3487 drm_framebuffer_unreference(fb);
3488
3489 return ret;
3490 }
3491
3492 /**
3493 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3494 * @dev: drm device for the ioctl
3495 * @data: data pointer for the ioctl
3496 * @file_priv: drm file for the ioctl call
3497 *
3498 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3499 * rectangle list. Generic userspace which does frontbuffer rendering must call
3500 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3501 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3502 *
3503 * Modesetting drivers which always update the frontbuffer do not need to
3504 * implement the corresponding ->dirty framebuffer callback.
3505 *
3506 * Called by the user via ioctl.
3507 *
3508 * Returns:
3509 * Zero on success, negative errno on failure.
3510 */
3511 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3512 void *data, struct drm_file *file_priv)
3513 {
3514 struct drm_clip_rect __user *clips_ptr;
3515 struct drm_clip_rect *clips = NULL;
3516 struct drm_mode_fb_dirty_cmd *r = data;
3517 struct drm_framebuffer *fb;
3518 unsigned flags;
3519 int num_clips;
3520 int ret;
3521
3522 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3523 return -EINVAL;
3524
3525 fb = drm_framebuffer_lookup(dev, r->fb_id);
3526 if (!fb)
3527 return -ENOENT;
3528
3529 num_clips = r->num_clips;
3530 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3531
3532 if (!num_clips != !clips_ptr) {
3533 ret = -EINVAL;
3534 goto out_err1;
3535 }
3536
3537 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3538
3539 /* If userspace annotates copy, clips must come in pairs */
3540 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3541 ret = -EINVAL;
3542 goto out_err1;
3543 }
3544
3545 if (num_clips && clips_ptr) {
3546 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3547 ret = -EINVAL;
3548 goto out_err1;
3549 }
3550 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3551 if (!clips) {
3552 ret = -ENOMEM;
3553 goto out_err1;
3554 }
3555
3556 ret = copy_from_user(clips, clips_ptr,
3557 num_clips * sizeof(*clips));
3558 if (ret) {
3559 ret = -EFAULT;
3560 goto out_err2;
3561 }
3562 }
3563
3564 if (fb->funcs->dirty) {
3565 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3566 clips, num_clips);
3567 } else {
3568 ret = -ENOSYS;
3569 }
3570
3571 out_err2:
3572 kfree(clips);
3573 out_err1:
3574 drm_framebuffer_unreference(fb);
3575
3576 return ret;
3577 }
3578
3579 /**
3580 * drm_fb_release - remove and free the FBs on this file
3581 * @priv: drm file for the ioctl
3582 *
3583 * Destroy all the FBs associated with @filp.
3584 *
3585 * Called by the user via ioctl.
3586 *
3587 * Returns:
3588 * Zero on success, negative errno on failure.
3589 */
3590 void drm_fb_release(struct drm_file *priv)
3591 {
3592 struct drm_framebuffer *fb, *tfb;
3593 struct drm_mode_rmfb_work arg;
3594
3595 INIT_LIST_HEAD(&arg.fbs);
3596
3597 /*
3598 * When the file gets released that means no one else can access the fb
3599 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3600 * avoid upsetting lockdep since the universal cursor code adds a
3601 * framebuffer while holding mutex locks.
3602 *
3603 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3604 * locks is impossible here since no one else but this function can get
3605 * at it any more.
3606 */
3607 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3608 if (!kref_exclusive_p(&fb->refcount)) {
3609 list_move_tail(&fb->filp_head, &arg.fbs);
3610 } else {
3611 list_del_init(&fb->filp_head);
3612
3613 /* This drops the fpriv->fbs reference. */
3614 drm_framebuffer_unreference(fb);
3615 }
3616 }
3617
3618 if (!list_empty(&arg.fbs)) {
3619 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3620
3621 schedule_work(&arg.work);
3622 flush_work(&arg.work);
3623 destroy_work_on_stack(&arg.work);
3624 }
3625 }
3626
3627 /**
3628 * drm_property_create - create a new property type
3629 * @dev: drm device
3630 * @flags: flags specifying the property type
3631 * @name: name of the property
3632 * @num_values: number of pre-defined values
3633 *
3634 * This creates a new generic drm property which can then be attached to a drm
3635 * object with drm_object_attach_property. The returned property object must be
3636 * freed with drm_property_destroy.
3637 *
3638 * Note that the DRM core keeps a per-device list of properties and that, if
3639 * drm_mode_config_cleanup() is called, it will destroy all properties created
3640 * by the driver.
3641 *
3642 * Returns:
3643 * A pointer to the newly created property on success, NULL on failure.
3644 */
3645 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3646 const char *name, int num_values)
3647 {
3648 struct drm_property *property = NULL;
3649 int ret;
3650
3651 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3652 if (!property)
3653 return NULL;
3654
3655 property->dev = dev;
3656
3657 if (num_values) {
3658 property->values = kcalloc(num_values, sizeof(uint64_t),
3659 GFP_KERNEL);
3660 if (!property->values)
3661 goto fail;
3662 }
3663
3664 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3665 if (ret)
3666 goto fail;
3667
3668 property->flags = flags;
3669 property->num_values = num_values;
3670 INIT_LIST_HEAD(&property->enum_list);
3671
3672 if (name) {
3673 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3674 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3675 }
3676
3677 list_add_tail(&property->head, &dev->mode_config.property_list);
3678
3679 WARN_ON(!drm_property_type_valid(property));
3680
3681 return property;
3682 fail:
3683 kfree(property->values);
3684 kfree(property);
3685 return NULL;
3686 }
3687 EXPORT_SYMBOL(drm_property_create);
3688
3689 /**
3690 * drm_property_create_enum - create a new enumeration property type
3691 * @dev: drm device
3692 * @flags: flags specifying the property type
3693 * @name: name of the property
3694 * @props: enumeration lists with property values
3695 * @num_values: number of pre-defined values
3696 *
3697 * This creates a new generic drm property which can then be attached to a drm
3698 * object with drm_object_attach_property. The returned property object must be
3699 * freed with drm_property_destroy.
3700 *
3701 * Userspace is only allowed to set one of the predefined values for enumeration
3702 * properties.
3703 *
3704 * Returns:
3705 * A pointer to the newly created property on success, NULL on failure.
3706 */
3707 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3708 const char *name,
3709 const struct drm_prop_enum_list *props,
3710 int num_values)
3711 {
3712 struct drm_property *property;
3713 int i, ret;
3714
3715 flags |= DRM_MODE_PROP_ENUM;
3716
3717 property = drm_property_create(dev, flags, name, num_values);
3718 if (!property)
3719 return NULL;
3720
3721 for (i = 0; i < num_values; i++) {
3722 ret = drm_property_add_enum(property, i,
3723 props[i].type,
3724 props[i].name);
3725 if (ret) {
3726 drm_property_destroy(dev, property);
3727 return NULL;
3728 }
3729 }
3730
3731 return property;
3732 }
3733 EXPORT_SYMBOL(drm_property_create_enum);
3734
3735 /**
3736 * drm_property_create_bitmask - create a new bitmask property type
3737 * @dev: drm device
3738 * @flags: flags specifying the property type
3739 * @name: name of the property
3740 * @props: enumeration lists with property bitflags
3741 * @num_props: size of the @props array
3742 * @supported_bits: bitmask of all supported enumeration values
3743 *
3744 * This creates a new bitmask drm property which can then be attached to a drm
3745 * object with drm_object_attach_property. The returned property object must be
3746 * freed with drm_property_destroy.
3747 *
3748 * Compared to plain enumeration properties userspace is allowed to set any
3749 * or'ed together combination of the predefined property bitflag values
3750 *
3751 * Returns:
3752 * A pointer to the newly created property on success, NULL on failure.
3753 */
3754 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3755 int flags, const char *name,
3756 const struct drm_prop_enum_list *props,
3757 int num_props,
3758 uint64_t supported_bits)
3759 {
3760 struct drm_property *property;
3761 int i, ret, index = 0;
3762 int num_values = hweight64(supported_bits);
3763
3764 flags |= DRM_MODE_PROP_BITMASK;
3765
3766 property = drm_property_create(dev, flags, name, num_values);
3767 if (!property)
3768 return NULL;
3769 for (i = 0; i < num_props; i++) {
3770 if (!(supported_bits & (1ULL << props[i].type)))
3771 continue;
3772
3773 if (WARN_ON(index >= num_values)) {
3774 drm_property_destroy(dev, property);
3775 return NULL;
3776 }
3777
3778 ret = drm_property_add_enum(property, index++,
3779 props[i].type,
3780 props[i].name);
3781 if (ret) {
3782 drm_property_destroy(dev, property);
3783 return NULL;
3784 }
3785 }
3786
3787 return property;
3788 }
3789 EXPORT_SYMBOL(drm_property_create_bitmask);
3790
3791 static struct drm_property *property_create_range(struct drm_device *dev,
3792 int flags, const char *name,
3793 uint64_t min, uint64_t max)
3794 {
3795 struct drm_property *property;
3796
3797 property = drm_property_create(dev, flags, name, 2);
3798 if (!property)
3799 return NULL;
3800
3801 property->values[0] = min;
3802 property->values[1] = max;
3803
3804 return property;
3805 }
3806
3807 /**
3808 * drm_property_create_range - create a new unsigned ranged property type
3809 * @dev: drm device
3810 * @flags: flags specifying the property type
3811 * @name: name of the property
3812 * @min: minimum value of the property
3813 * @max: maximum value of the property
3814 *
3815 * This creates a new generic drm property which can then be attached to a drm
3816 * object with drm_object_attach_property. The returned property object must be
3817 * freed with drm_property_destroy.
3818 *
3819 * Userspace is allowed to set any unsigned integer value in the (min, max)
3820 * range inclusive.
3821 *
3822 * Returns:
3823 * A pointer to the newly created property on success, NULL on failure.
3824 */
3825 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3826 const char *name,
3827 uint64_t min, uint64_t max)
3828 {
3829 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3830 name, min, max);
3831 }
3832 EXPORT_SYMBOL(drm_property_create_range);
3833
3834 /**
3835 * drm_property_create_signed_range - create a new signed ranged property type
3836 * @dev: drm device
3837 * @flags: flags specifying the property type
3838 * @name: name of the property
3839 * @min: minimum value of the property
3840 * @max: maximum value of the property
3841 *
3842 * This creates a new generic drm property which can then be attached to a drm
3843 * object with drm_object_attach_property. The returned property object must be
3844 * freed with drm_property_destroy.
3845 *
3846 * Userspace is allowed to set any signed integer value in the (min, max)
3847 * range inclusive.
3848 *
3849 * Returns:
3850 * A pointer to the newly created property on success, NULL on failure.
3851 */
3852 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3853 int flags, const char *name,
3854 int64_t min, int64_t max)
3855 {
3856 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3857 name, I642U64(min), I642U64(max));
3858 }
3859 EXPORT_SYMBOL(drm_property_create_signed_range);
3860
3861 /**
3862 * drm_property_create_object - create a new object property type
3863 * @dev: drm device
3864 * @flags: flags specifying the property type
3865 * @name: name of the property
3866 * @type: object type from DRM_MODE_OBJECT_* defines
3867 *
3868 * This creates a new generic drm property which can then be attached to a drm
3869 * object with drm_object_attach_property. The returned property object must be
3870 * freed with drm_property_destroy.
3871 *
3872 * Userspace is only allowed to set this to any property value of the given
3873 * @type. Only useful for atomic properties, which is enforced.
3874 *
3875 * Returns:
3876 * A pointer to the newly created property on success, NULL on failure.
3877 */
3878 struct drm_property *drm_property_create_object(struct drm_device *dev,
3879 int flags, const char *name, uint32_t type)
3880 {
3881 struct drm_property *property;
3882
3883 flags |= DRM_MODE_PROP_OBJECT;
3884
3885 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3886 return NULL;
3887
3888 property = drm_property_create(dev, flags, name, 1);
3889 if (!property)
3890 return NULL;
3891
3892 property->values[0] = type;
3893
3894 return property;
3895 }
3896 EXPORT_SYMBOL(drm_property_create_object);
3897
3898 /**
3899 * drm_property_create_bool - create a new boolean property type
3900 * @dev: drm device
3901 * @flags: flags specifying the property type
3902 * @name: name of the property
3903 *
3904 * This creates a new generic drm property which can then be attached to a drm
3905 * object with drm_object_attach_property. The returned property object must be
3906 * freed with drm_property_destroy.
3907 *
3908 * This is implemented as a ranged property with only {0, 1} as valid values.
3909 *
3910 * Returns:
3911 * A pointer to the newly created property on success, NULL on failure.
3912 */
3913 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3914 const char *name)
3915 {
3916 return drm_property_create_range(dev, flags, name, 0, 1);
3917 }
3918 EXPORT_SYMBOL(drm_property_create_bool);
3919
3920 /**
3921 * drm_property_add_enum - add a possible value to an enumeration property
3922 * @property: enumeration property to change
3923 * @index: index of the new enumeration
3924 * @value: value of the new enumeration
3925 * @name: symbolic name of the new enumeration
3926 *
3927 * This functions adds enumerations to a property.
3928 *
3929 * It's use is deprecated, drivers should use one of the more specific helpers
3930 * to directly create the property with all enumerations already attached.
3931 *
3932 * Returns:
3933 * Zero on success, error code on failure.
3934 */
3935 int drm_property_add_enum(struct drm_property *property, int index,
3936 uint64_t value, const char *name)
3937 {
3938 struct drm_property_enum *prop_enum;
3939
3940 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3941 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3942 return -EINVAL;
3943
3944 /*
3945 * Bitmask enum properties have the additional constraint of values
3946 * from 0 to 63
3947 */
3948 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3949 (value > 63))
3950 return -EINVAL;
3951
3952 if (!list_empty(&property->enum_list)) {
3953 list_for_each_entry(prop_enum, &property->enum_list, head) {
3954 if (prop_enum->value == value) {
3955 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3956 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3957 return 0;
3958 }
3959 }
3960 }
3961
3962 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3963 if (!prop_enum)
3964 return -ENOMEM;
3965
3966 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3967 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3968 prop_enum->value = value;
3969
3970 property->values[index] = value;
3971 list_add_tail(&prop_enum->head, &property->enum_list);
3972 return 0;
3973 }
3974 EXPORT_SYMBOL(drm_property_add_enum);
3975
3976 /**
3977 * drm_property_destroy - destroy a drm property
3978 * @dev: drm device
3979 * @property: property to destry
3980 *
3981 * This function frees a property including any attached resources like
3982 * enumeration values.
3983 */
3984 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3985 {
3986 struct drm_property_enum *prop_enum, *pt;
3987
3988 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
3989 list_del(&prop_enum->head);
3990 kfree(prop_enum);
3991 }
3992
3993 if (property->num_values)
3994 kfree(property->values);
3995 drm_mode_object_put(dev, &property->base);
3996 list_del(&property->head);
3997 kfree(property);
3998 }
3999 EXPORT_SYMBOL(drm_property_destroy);
4000
4001 /**
4002 * drm_object_attach_property - attach a property to a modeset object
4003 * @obj: drm modeset object
4004 * @property: property to attach
4005 * @init_val: initial value of the property
4006 *
4007 * This attaches the given property to the modeset object with the given initial
4008 * value. Currently this function cannot fail since the properties are stored in
4009 * a statically sized array.
4010 */
4011 void drm_object_attach_property(struct drm_mode_object *obj,
4012 struct drm_property *property,
4013 uint64_t init_val)
4014 {
4015 int count = obj->properties->count;
4016
4017 if (count == DRM_OBJECT_MAX_PROPERTY) {
4018 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4019 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4020 "you see this message on the same object type.\n",
4021 obj->type);
4022 return;
4023 }
4024
4025 obj->properties->properties[count] = property;
4026 obj->properties->values[count] = init_val;
4027 obj->properties->count++;
4028 if (property->flags & DRM_MODE_PROP_ATOMIC)
4029 obj->properties->atomic_count++;
4030 }
4031 EXPORT_SYMBOL(drm_object_attach_property);
4032
4033 /**
4034 * drm_object_property_set_value - set the value of a property
4035 * @obj: drm mode object to set property value for
4036 * @property: property to set
4037 * @val: value the property should be set to
4038 *
4039 * This functions sets a given property on a given object. This function only
4040 * changes the software state of the property, it does not call into the
4041 * driver's ->set_property callback.
4042 *
4043 * Returns:
4044 * Zero on success, error code on failure.
4045 */
4046 int drm_object_property_set_value(struct drm_mode_object *obj,
4047 struct drm_property *property, uint64_t val)
4048 {
4049 int i;
4050
4051 for (i = 0; i < obj->properties->count; i++) {
4052 if (obj->properties->properties[i] == property) {
4053 obj->properties->values[i] = val;
4054 return 0;
4055 }
4056 }
4057
4058 return -EINVAL;
4059 }
4060 EXPORT_SYMBOL(drm_object_property_set_value);
4061
4062 /**
4063 * drm_object_property_get_value - retrieve the value of a property
4064 * @obj: drm mode object to get property value from
4065 * @property: property to retrieve
4066 * @val: storage for the property value
4067 *
4068 * This function retrieves the softare state of the given property for the given
4069 * property. Since there is no driver callback to retrieve the current property
4070 * value this might be out of sync with the hardware, depending upon the driver
4071 * and property.
4072 *
4073 * Returns:
4074 * Zero on success, error code on failure.
4075 */
4076 int drm_object_property_get_value(struct drm_mode_object *obj,
4077 struct drm_property *property, uint64_t *val)
4078 {
4079 int i;
4080
4081 /* read-only properties bypass atomic mechanism and still store
4082 * their value in obj->properties->values[].. mostly to avoid
4083 * having to deal w/ EDID and similar props in atomic paths:
4084 */
4085 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4086 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4087 return drm_atomic_get_property(obj, property, val);
4088
4089 for (i = 0; i < obj->properties->count; i++) {
4090 if (obj->properties->properties[i] == property) {
4091 *val = obj->properties->values[i];
4092 return 0;
4093 }
4094 }
4095
4096 return -EINVAL;
4097 }
4098 EXPORT_SYMBOL(drm_object_property_get_value);
4099
4100 /**
4101 * drm_mode_getproperty_ioctl - get the property metadata
4102 * @dev: DRM device
4103 * @data: ioctl data
4104 * @file_priv: DRM file info
4105 *
4106 * This function retrieves the metadata for a given property, like the different
4107 * possible values for an enum property or the limits for a range property.
4108 *
4109 * Blob properties are special
4110 *
4111 * Called by the user via ioctl.
4112 *
4113 * Returns:
4114 * Zero on success, negative errno on failure.
4115 */
4116 int drm_mode_getproperty_ioctl(struct drm_device *dev,
4117 void *data, struct drm_file *file_priv)
4118 {
4119 struct drm_mode_get_property *out_resp = data;
4120 struct drm_property *property;
4121 int enum_count = 0;
4122 int value_count = 0;
4123 int ret = 0, i;
4124 int copied;
4125 struct drm_property_enum *prop_enum;
4126 struct drm_mode_property_enum __user *enum_ptr;
4127 uint64_t __user *values_ptr;
4128
4129 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4130 return -EINVAL;
4131
4132 drm_modeset_lock_all(dev);
4133 property = drm_property_find(dev, out_resp->prop_id);
4134 if (!property) {
4135 ret = -ENOENT;
4136 goto done;
4137 }
4138
4139 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4140 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4141 list_for_each_entry(prop_enum, &property->enum_list, head)
4142 enum_count++;
4143 }
4144
4145 value_count = property->num_values;
4146
4147 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4148 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4149 out_resp->flags = property->flags;
4150
4151 if ((out_resp->count_values >= value_count) && value_count) {
4152 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
4153 for (i = 0; i < value_count; i++) {
4154 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4155 ret = -EFAULT;
4156 goto done;
4157 }
4158 }
4159 }
4160 out_resp->count_values = value_count;
4161
4162 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4163 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4164 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4165 copied = 0;
4166 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
4167 list_for_each_entry(prop_enum, &property->enum_list, head) {
4168
4169 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4170 ret = -EFAULT;
4171 goto done;
4172 }
4173
4174 if (copy_to_user(&enum_ptr[copied].name,
4175 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4176 ret = -EFAULT;
4177 goto done;
4178 }
4179 copied++;
4180 }
4181 }
4182 out_resp->count_enum_blobs = enum_count;
4183 }
4184
4185 /*
4186 * NOTE: The idea seems to have been to use this to read all the blob
4187 * property values. But nothing ever added them to the corresponding
4188 * list, userspace always used the special-purpose get_blob ioctl to
4189 * read the value for a blob property. It also doesn't make a lot of
4190 * sense to return values here when everything else is just metadata for
4191 * the property itself.
4192 */
4193 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4194 out_resp->count_enum_blobs = 0;
4195 done:
4196 drm_modeset_unlock_all(dev);
4197 return ret;
4198 }
4199
4200 /**
4201 * drm_property_create_blob - Create new blob property
4202 *
4203 * Creates a new blob property for a specified DRM device, optionally
4204 * copying data.
4205 *
4206 * @dev: DRM device to create property for
4207 * @length: Length to allocate for blob data
4208 * @data: If specified, copies data into blob
4209 *
4210 * Returns:
4211 * New blob property with a single reference on success, or an ERR_PTR
4212 * value on failure.
4213 */
4214 struct drm_property_blob *
4215 drm_property_create_blob(struct drm_device *dev, size_t length,
4216 const void *data)
4217 {
4218 struct drm_property_blob *blob;
4219 int ret;
4220
4221 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
4222 return ERR_PTR(-EINVAL);
4223
4224 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4225 if (!blob)
4226 return ERR_PTR(-ENOMEM);
4227
4228 /* This must be explicitly initialised, so we can safely call list_del
4229 * on it in the removal handler, even if it isn't in a file list. */
4230 INIT_LIST_HEAD(&blob->head_file);
4231 blob->length = length;
4232 blob->dev = dev;
4233
4234 if (data)
4235 memcpy(blob->data, data, length);
4236
4237 mutex_lock(&dev->mode_config.blob_lock);
4238
4239 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4240 if (ret) {
4241 kfree(blob);
4242 mutex_unlock(&dev->mode_config.blob_lock);
4243 return ERR_PTR(-EINVAL);
4244 }
4245
4246 kref_init(&blob->refcount);
4247
4248 list_add_tail(&blob->head_global,
4249 &dev->mode_config.property_blob_list);
4250
4251 mutex_unlock(&dev->mode_config.blob_lock);
4252
4253 return blob;
4254 }
4255 EXPORT_SYMBOL(drm_property_create_blob);
4256
4257 /**
4258 * drm_property_free_blob - Blob property destructor
4259 *
4260 * Internal free function for blob properties; must not be used directly.
4261 *
4262 * @kref: Reference
4263 */
4264 static void drm_property_free_blob(struct kref *kref)
4265 {
4266 struct drm_property_blob *blob =
4267 container_of(kref, struct drm_property_blob, refcount);
4268
4269 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4270
4271 list_del(&blob->head_global);
4272 list_del(&blob->head_file);
4273 drm_mode_object_put(blob->dev, &blob->base);
4274
4275 kfree(blob);
4276 }
4277
4278 /**
4279 * drm_property_unreference_blob - Unreference a blob property
4280 *
4281 * Drop a reference on a blob property. May free the object.
4282 *
4283 * @blob: Pointer to blob property
4284 */
4285 void drm_property_unreference_blob(struct drm_property_blob *blob)
4286 {
4287 struct drm_device *dev;
4288
4289 if (!blob)
4290 return;
4291
4292 dev = blob->dev;
4293
4294 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
4295
4296 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4297 &dev->mode_config.blob_lock))
4298 mutex_unlock(&dev->mode_config.blob_lock);
4299 else
4300 might_lock(&dev->mode_config.blob_lock);
4301 }
4302 EXPORT_SYMBOL(drm_property_unreference_blob);
4303
4304 /**
4305 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4306 *
4307 * Drop a reference on a blob property. May free the object. This must be
4308 * called with blob_lock held.
4309 *
4310 * @blob: Pointer to blob property
4311 */
4312 static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4313 {
4314 if (!blob)
4315 return;
4316
4317 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
4318
4319 kref_put(&blob->refcount, drm_property_free_blob);
4320 }
4321
4322 /**
4323 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4324 * @dev: DRM device
4325 * @file_priv: destroy all blobs owned by this file handle
4326 */
4327 void drm_property_destroy_user_blobs(struct drm_device *dev,
4328 struct drm_file *file_priv)
4329 {
4330 struct drm_property_blob *blob, *bt;
4331
4332 mutex_lock(&dev->mode_config.blob_lock);
4333
4334 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4335 list_del_init(&blob->head_file);
4336 drm_property_unreference_blob_locked(blob);
4337 }
4338
4339 mutex_unlock(&dev->mode_config.blob_lock);
4340 }
4341
4342 /**
4343 * drm_property_reference_blob - Take a reference on an existing property
4344 *
4345 * Take a new reference on an existing blob property.
4346 *
4347 * @blob: Pointer to blob property
4348 */
4349 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4350 {
4351 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
4352 kref_get(&blob->refcount);
4353 return blob;
4354 }
4355 EXPORT_SYMBOL(drm_property_reference_blob);
4356
4357 /*
4358 * Like drm_property_lookup_blob, but does not return an additional reference.
4359 * Must be called with blob_lock held.
4360 */
4361 static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4362 uint32_t id)
4363 {
4364 struct drm_mode_object *obj = NULL;
4365 struct drm_property_blob *blob;
4366
4367 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4368
4369 mutex_lock(&dev->mode_config.idr_mutex);
4370 obj = idr_find(&dev->mode_config.crtc_idr, id);
4371 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4372 blob = NULL;
4373 else
4374 blob = obj_to_blob(obj);
4375 mutex_unlock(&dev->mode_config.idr_mutex);
4376
4377 return blob;
4378 }
4379
4380 /**
4381 * drm_property_lookup_blob - look up a blob property and take a reference
4382 * @dev: drm device
4383 * @id: id of the blob property
4384 *
4385 * If successful, this takes an additional reference to the blob property.
4386 * callers need to make sure to eventually unreference the returned property
4387 * again, using @drm_property_unreference_blob.
4388 */
4389 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4390 uint32_t id)
4391 {
4392 struct drm_property_blob *blob;
4393
4394 mutex_lock(&dev->mode_config.blob_lock);
4395 blob = __drm_property_lookup_blob(dev, id);
4396 if (blob) {
4397 if (!kref_get_unless_zero(&blob->refcount))
4398 blob = NULL;
4399 }
4400 mutex_unlock(&dev->mode_config.blob_lock);
4401
4402 return blob;
4403 }
4404 EXPORT_SYMBOL(drm_property_lookup_blob);
4405
4406 /**
4407 * drm_property_replace_global_blob - atomically replace existing blob property
4408 * @dev: drm device
4409 * @replace: location of blob property pointer to be replaced
4410 * @length: length of data for new blob, or 0 for no data
4411 * @data: content for new blob, or NULL for no data
4412 * @obj_holds_id: optional object for property holding blob ID
4413 * @prop_holds_id: optional property holding blob ID
4414 * @return 0 on success or error on failure
4415 *
4416 * This function will atomically replace a global property in the blob list,
4417 * optionally updating a property which holds the ID of that property. It is
4418 * guaranteed to be atomic: no caller will be allowed to see intermediate
4419 * results, and either the entire operation will succeed and clean up the
4420 * previous property, or it will fail and the state will be unchanged.
4421 *
4422 * If length is 0 or data is NULL, no new blob will be created, and the holding
4423 * property, if specified, will be set to 0.
4424 *
4425 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4426 * by holding the relevant modesetting object lock for its parent.
4427 *
4428 * For example, a drm_connector has a 'PATH' property, which contains the ID
4429 * of a blob property with the value of the MST path information. Calling this
4430 * function with replace pointing to the connector's path_blob_ptr, length and
4431 * data set for the new path information, obj_holds_id set to the connector's
4432 * base object, and prop_holds_id set to the path property name, will perform
4433 * a completely atomic update. The access to path_blob_ptr is protected by the
4434 * caller holding a lock on the connector.
4435 */
4436 static int drm_property_replace_global_blob(struct drm_device *dev,
4437 struct drm_property_blob **replace,
4438 size_t length,
4439 const void *data,
4440 struct drm_mode_object *obj_holds_id,
4441 struct drm_property *prop_holds_id)
4442 {
4443 struct drm_property_blob *new_blob = NULL;
4444 struct drm_property_blob *old_blob = NULL;
4445 int ret;
4446
4447 WARN_ON(replace == NULL);
4448
4449 old_blob = *replace;
4450
4451 if (length && data) {
4452 new_blob = drm_property_create_blob(dev, length, data);
4453 if (IS_ERR(new_blob))
4454 return PTR_ERR(new_blob);
4455 }
4456
4457 /* This does not need to be synchronised with blob_lock, as the
4458 * get_properties ioctl locks all modesetting objects, and
4459 * obj_holds_id must be locked before calling here, so we cannot
4460 * have its value out of sync with the list membership modified
4461 * below under blob_lock. */
4462 if (obj_holds_id) {
4463 ret = drm_object_property_set_value(obj_holds_id,
4464 prop_holds_id,
4465 new_blob ?
4466 new_blob->base.id : 0);
4467 if (ret != 0)
4468 goto err_created;
4469 }
4470
4471 drm_property_unreference_blob(old_blob);
4472 *replace = new_blob;
4473
4474 return 0;
4475
4476 err_created:
4477 drm_property_unreference_blob(new_blob);
4478 return ret;
4479 }
4480
4481 /**
4482 * drm_mode_getblob_ioctl - get the contents of a blob property value
4483 * @dev: DRM device
4484 * @data: ioctl data
4485 * @file_priv: DRM file info
4486 *
4487 * This function retrieves the contents of a blob property. The value stored in
4488 * an object's blob property is just a normal modeset object id.
4489 *
4490 * Called by the user via ioctl.
4491 *
4492 * Returns:
4493 * Zero on success, negative errno on failure.
4494 */
4495 int drm_mode_getblob_ioctl(struct drm_device *dev,
4496 void *data, struct drm_file *file_priv)
4497 {
4498 struct drm_mode_get_blob *out_resp = data;
4499 struct drm_property_blob *blob;
4500 int ret = 0;
4501 void __user *blob_ptr;
4502
4503 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4504 return -EINVAL;
4505
4506 drm_modeset_lock_all(dev);
4507 mutex_lock(&dev->mode_config.blob_lock);
4508 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4509 if (!blob) {
4510 ret = -ENOENT;
4511 goto done;
4512 }
4513
4514 if (out_resp->length == blob->length) {
4515 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4516 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4517 ret = -EFAULT;
4518 goto done;
4519 }
4520 }
4521 out_resp->length = blob->length;
4522
4523 done:
4524 mutex_unlock(&dev->mode_config.blob_lock);
4525 drm_modeset_unlock_all(dev);
4526 return ret;
4527 }
4528
4529 /**
4530 * drm_mode_createblob_ioctl - create a new blob property
4531 * @dev: DRM device
4532 * @data: ioctl data
4533 * @file_priv: DRM file info
4534 *
4535 * This function creates a new blob property with user-defined values. In order
4536 * to give us sensible validation and checking when creating, rather than at
4537 * every potential use, we also require a type to be provided upfront.
4538 *
4539 * Called by the user via ioctl.
4540 *
4541 * Returns:
4542 * Zero on success, negative errno on failure.
4543 */
4544 int drm_mode_createblob_ioctl(struct drm_device *dev,
4545 void *data, struct drm_file *file_priv)
4546 {
4547 struct drm_mode_create_blob *out_resp = data;
4548 struct drm_property_blob *blob;
4549 void __user *blob_ptr;
4550 int ret = 0;
4551
4552 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4553 return -EINVAL;
4554
4555 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4556 if (IS_ERR(blob))
4557 return PTR_ERR(blob);
4558
4559 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4560 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4561 ret = -EFAULT;
4562 goto out_blob;
4563 }
4564
4565 /* Dropping the lock between create_blob and our access here is safe
4566 * as only the same file_priv can remove the blob; at this point, it is
4567 * not associated with any file_priv. */
4568 mutex_lock(&dev->mode_config.blob_lock);
4569 out_resp->blob_id = blob->base.id;
4570 list_add_tail(&blob->head_file, &file_priv->blobs);
4571 mutex_unlock(&dev->mode_config.blob_lock);
4572
4573 return 0;
4574
4575 out_blob:
4576 drm_property_unreference_blob(blob);
4577 return ret;
4578 }
4579
4580 /**
4581 * drm_mode_destroyblob_ioctl - destroy a user blob property
4582 * @dev: DRM device
4583 * @data: ioctl data
4584 * @file_priv: DRM file info
4585 *
4586 * Destroy an existing user-defined blob property.
4587 *
4588 * Called by the user via ioctl.
4589 *
4590 * Returns:
4591 * Zero on success, negative errno on failure.
4592 */
4593 int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4594 void *data, struct drm_file *file_priv)
4595 {
4596 struct drm_mode_destroy_blob *out_resp = data;
4597 struct drm_property_blob *blob = NULL, *bt;
4598 bool found = false;
4599 int ret = 0;
4600
4601 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4602 return -EINVAL;
4603
4604 mutex_lock(&dev->mode_config.blob_lock);
4605 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4606 if (!blob) {
4607 ret = -ENOENT;
4608 goto err;
4609 }
4610
4611 /* Ensure the property was actually created by this user. */
4612 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4613 if (bt == blob) {
4614 found = true;
4615 break;
4616 }
4617 }
4618
4619 if (!found) {
4620 ret = -EPERM;
4621 goto err;
4622 }
4623
4624 /* We must drop head_file here, because we may not be the last
4625 * reference on the blob. */
4626 list_del_init(&blob->head_file);
4627 drm_property_unreference_blob_locked(blob);
4628 mutex_unlock(&dev->mode_config.blob_lock);
4629
4630 return 0;
4631
4632 err:
4633 mutex_unlock(&dev->mode_config.blob_lock);
4634 return ret;
4635 }
4636
4637 /**
4638 * drm_mode_connector_set_path_property - set tile property on connector
4639 * @connector: connector to set property on.
4640 * @path: path to use for property; must not be NULL.
4641 *
4642 * This creates a property to expose to userspace to specify a
4643 * connector path. This is mainly used for DisplayPort MST where
4644 * connectors have a topology and we want to allow userspace to give
4645 * them more meaningful names.
4646 *
4647 * Returns:
4648 * Zero on success, negative errno on failure.
4649 */
4650 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4651 const char *path)
4652 {
4653 struct drm_device *dev = connector->dev;
4654 int ret;
4655
4656 ret = drm_property_replace_global_blob(dev,
4657 &connector->path_blob_ptr,
4658 strlen(path) + 1,
4659 path,
4660 &connector->base,
4661 dev->mode_config.path_property);
4662 return ret;
4663 }
4664 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4665
4666 /**
4667 * drm_mode_connector_set_tile_property - set tile property on connector
4668 * @connector: connector to set property on.
4669 *
4670 * This looks up the tile information for a connector, and creates a
4671 * property for userspace to parse if it exists. The property is of
4672 * the form of 8 integers using ':' as a separator.
4673 *
4674 * Returns:
4675 * Zero on success, errno on failure.
4676 */
4677 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4678 {
4679 struct drm_device *dev = connector->dev;
4680 char tile[256];
4681 int ret;
4682
4683 if (!connector->has_tile) {
4684 ret = drm_property_replace_global_blob(dev,
4685 &connector->tile_blob_ptr,
4686 0,
4687 NULL,
4688 &connector->base,
4689 dev->mode_config.tile_property);
4690 return ret;
4691 }
4692
4693 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4694 connector->tile_group->id, connector->tile_is_single_monitor,
4695 connector->num_h_tile, connector->num_v_tile,
4696 connector->tile_h_loc, connector->tile_v_loc,
4697 connector->tile_h_size, connector->tile_v_size);
4698
4699 ret = drm_property_replace_global_blob(dev,
4700 &connector->tile_blob_ptr,
4701 strlen(tile) + 1,
4702 tile,
4703 &connector->base,
4704 dev->mode_config.tile_property);
4705 return ret;
4706 }
4707 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4708
4709 /**
4710 * drm_mode_connector_update_edid_property - update the edid property of a connector
4711 * @connector: drm connector
4712 * @edid: new value of the edid property
4713 *
4714 * This function creates a new blob modeset object and assigns its id to the
4715 * connector's edid property.
4716 *
4717 * Returns:
4718 * Zero on success, negative errno on failure.
4719 */
4720 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4721 const struct edid *edid)
4722 {
4723 struct drm_device *dev = connector->dev;
4724 size_t size = 0;
4725 int ret;
4726
4727 /* ignore requests to set edid when overridden */
4728 if (connector->override_edid)
4729 return 0;
4730
4731 if (edid)
4732 size = EDID_LENGTH * (1 + edid->extensions);
4733
4734 ret = drm_property_replace_global_blob(dev,
4735 &connector->edid_blob_ptr,
4736 size,
4737 edid,
4738 &connector->base,
4739 dev->mode_config.edid_property);
4740 return ret;
4741 }
4742 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4743
4744 /* Some properties could refer to dynamic refcnt'd objects, or things that
4745 * need special locking to handle lifetime issues (ie. to ensure the prop
4746 * value doesn't become invalid part way through the property update due to
4747 * race). The value returned by reference via 'obj' should be passed back
4748 * to drm_property_change_valid_put() after the property is set (and the
4749 * object to which the property is attached has a chance to take it's own
4750 * reference).
4751 */
4752 bool drm_property_change_valid_get(struct drm_property *property,
4753 uint64_t value, struct drm_mode_object **ref)
4754 {
4755 int i;
4756
4757 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4758 return false;
4759
4760 *ref = NULL;
4761
4762 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4763 if (value < property->values[0] || value > property->values[1])
4764 return false;
4765 return true;
4766 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4767 int64_t svalue = U642I64(value);
4768
4769 if (svalue < U642I64(property->values[0]) ||
4770 svalue > U642I64(property->values[1]))
4771 return false;
4772 return true;
4773 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4774 uint64_t valid_mask = 0;
4775
4776 for (i = 0; i < property->num_values; i++)
4777 valid_mask |= (1ULL << property->values[i]);
4778 return !(value & ~valid_mask);
4779 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4780 struct drm_property_blob *blob;
4781
4782 if (value == 0)
4783 return true;
4784
4785 blob = drm_property_lookup_blob(property->dev, value);
4786 if (blob) {
4787 *ref = &blob->base;
4788 return true;
4789 } else {
4790 return false;
4791 }
4792 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4793 /* a zero value for an object property translates to null: */
4794 if (value == 0)
4795 return true;
4796
4797 /* handle refcnt'd objects specially: */
4798 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4799 struct drm_framebuffer *fb;
4800 fb = drm_framebuffer_lookup(property->dev, value);
4801 if (fb) {
4802 *ref = &fb->base;
4803 return true;
4804 } else {
4805 return false;
4806 }
4807 } else {
4808 return _object_find(property->dev, value, property->values[0]) != NULL;
4809 }
4810 }
4811
4812 for (i = 0; i < property->num_values; i++)
4813 if (property->values[i] == value)
4814 return true;
4815 return false;
4816 }
4817
4818 void drm_property_change_valid_put(struct drm_property *property,
4819 struct drm_mode_object *ref)
4820 {
4821 if (!ref)
4822 return;
4823
4824 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4825 if (property->values[0] == DRM_MODE_OBJECT_FB)
4826 drm_framebuffer_unreference(obj_to_fb(ref));
4827 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4828 drm_property_unreference_blob(obj_to_blob(ref));
4829 }
4830
4831 /**
4832 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4833 * @dev: DRM device
4834 * @data: ioctl data
4835 * @file_priv: DRM file info
4836 *
4837 * This function sets the current value for a connectors's property. It also
4838 * calls into a driver's ->set_property callback to update the hardware state
4839 *
4840 * Called by the user via ioctl.
4841 *
4842 * Returns:
4843 * Zero on success, negative errno on failure.
4844 */
4845 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4846 void *data, struct drm_file *file_priv)
4847 {
4848 struct drm_mode_connector_set_property *conn_set_prop = data;
4849 struct drm_mode_obj_set_property obj_set_prop = {
4850 .value = conn_set_prop->value,
4851 .prop_id = conn_set_prop->prop_id,
4852 .obj_id = conn_set_prop->connector_id,
4853 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4854 };
4855
4856 /* It does all the locking and checking we need */
4857 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4858 }
4859
4860 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4861 struct drm_property *property,
4862 uint64_t value)
4863 {
4864 int ret = -EINVAL;
4865 struct drm_connector *connector = obj_to_connector(obj);
4866
4867 /* Do DPMS ourselves */
4868 if (property == connector->dev->mode_config.dpms_property) {
4869 ret = 0;
4870 if (connector->funcs->dpms)
4871 ret = (*connector->funcs->dpms)(connector, (int)value);
4872 } else if (connector->funcs->set_property)
4873 ret = connector->funcs->set_property(connector, property, value);
4874
4875 /* store the property value if successful */
4876 if (!ret)
4877 drm_object_property_set_value(&connector->base, property, value);
4878 return ret;
4879 }
4880
4881 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4882 struct drm_property *property,
4883 uint64_t value)
4884 {
4885 int ret = -EINVAL;
4886 struct drm_crtc *crtc = obj_to_crtc(obj);
4887
4888 if (crtc->funcs->set_property)
4889 ret = crtc->funcs->set_property(crtc, property, value);
4890 if (!ret)
4891 drm_object_property_set_value(obj, property, value);
4892
4893 return ret;
4894 }
4895
4896 /**
4897 * drm_mode_plane_set_obj_prop - set the value of a property
4898 * @plane: drm plane object to set property value for
4899 * @property: property to set
4900 * @value: value the property should be set to
4901 *
4902 * This functions sets a given property on a given plane object. This function
4903 * calls the driver's ->set_property callback and changes the software state of
4904 * the property if the callback succeeds.
4905 *
4906 * Returns:
4907 * Zero on success, error code on failure.
4908 */
4909 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4910 struct drm_property *property,
4911 uint64_t value)
4912 {
4913 int ret = -EINVAL;
4914 struct drm_mode_object *obj = &plane->base;
4915
4916 if (plane->funcs->set_property)
4917 ret = plane->funcs->set_property(plane, property, value);
4918 if (!ret)
4919 drm_object_property_set_value(obj, property, value);
4920
4921 return ret;
4922 }
4923 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4924
4925 /**
4926 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4927 * @dev: DRM device
4928 * @data: ioctl data
4929 * @file_priv: DRM file info
4930 *
4931 * This function retrieves the current value for an object's property. Compared
4932 * to the connector specific ioctl this one is extended to also work on crtc and
4933 * plane objects.
4934 *
4935 * Called by the user via ioctl.
4936 *
4937 * Returns:
4938 * Zero on success, negative errno on failure.
4939 */
4940 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4941 struct drm_file *file_priv)
4942 {
4943 struct drm_mode_obj_get_properties *arg = data;
4944 struct drm_mode_object *obj;
4945 int ret = 0;
4946
4947 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4948 return -EINVAL;
4949
4950 drm_modeset_lock_all(dev);
4951
4952 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4953 if (!obj) {
4954 ret = -ENOENT;
4955 goto out;
4956 }
4957 if (!obj->properties) {
4958 ret = -EINVAL;
4959 goto out;
4960 }
4961
4962 ret = get_properties(obj, file_priv->atomic,
4963 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4964 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4965 &arg->count_props);
4966
4967 out:
4968 drm_modeset_unlock_all(dev);
4969 return ret;
4970 }
4971
4972 /**
4973 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4974 * @dev: DRM device
4975 * @data: ioctl data
4976 * @file_priv: DRM file info
4977 *
4978 * This function sets the current value for an object's property. It also calls
4979 * into a driver's ->set_property callback to update the hardware state.
4980 * Compared to the connector specific ioctl this one is extended to also work on
4981 * crtc and plane objects.
4982 *
4983 * Called by the user via ioctl.
4984 *
4985 * Returns:
4986 * Zero on success, negative errno on failure.
4987 */
4988 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4989 struct drm_file *file_priv)
4990 {
4991 struct drm_mode_obj_set_property *arg = data;
4992 struct drm_mode_object *arg_obj;
4993 struct drm_mode_object *prop_obj;
4994 struct drm_property *property;
4995 int i, ret = -EINVAL;
4996 struct drm_mode_object *ref;
4997
4998 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4999 return -EINVAL;
5000
5001 drm_modeset_lock_all(dev);
5002
5003 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5004 if (!arg_obj) {
5005 ret = -ENOENT;
5006 goto out;
5007 }
5008 if (!arg_obj->properties)
5009 goto out;
5010
5011 for (i = 0; i < arg_obj->properties->count; i++)
5012 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
5013 break;
5014
5015 if (i == arg_obj->properties->count)
5016 goto out;
5017
5018 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5019 DRM_MODE_OBJECT_PROPERTY);
5020 if (!prop_obj) {
5021 ret = -ENOENT;
5022 goto out;
5023 }
5024 property = obj_to_property(prop_obj);
5025
5026 if (!drm_property_change_valid_get(property, arg->value, &ref))
5027 goto out;
5028
5029 switch (arg_obj->type) {
5030 case DRM_MODE_OBJECT_CONNECTOR:
5031 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5032 arg->value);
5033 break;
5034 case DRM_MODE_OBJECT_CRTC:
5035 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5036 break;
5037 case DRM_MODE_OBJECT_PLANE:
5038 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5039 property, arg->value);
5040 break;
5041 }
5042
5043 drm_property_change_valid_put(property, ref);
5044
5045 out:
5046 drm_modeset_unlock_all(dev);
5047 return ret;
5048 }
5049
5050 /**
5051 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5052 * @connector: connector to attach
5053 * @encoder: encoder to attach @connector to
5054 *
5055 * This function links up a connector to an encoder. Note that the routing
5056 * restrictions between encoders and crtcs are exposed to userspace through the
5057 * possible_clones and possible_crtcs bitmasks.
5058 *
5059 * Returns:
5060 * Zero on success, negative errno on failure.
5061 */
5062 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5063 struct drm_encoder *encoder)
5064 {
5065 int i;
5066
5067 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5068 if (connector->encoder_ids[i] == 0) {
5069 connector->encoder_ids[i] = encoder->base.id;
5070 return 0;
5071 }
5072 }
5073 return -ENOMEM;
5074 }
5075 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5076
5077 /**
5078 * drm_mode_crtc_set_gamma_size - set the gamma table size
5079 * @crtc: CRTC to set the gamma table size for
5080 * @gamma_size: size of the gamma table
5081 *
5082 * Drivers which support gamma tables should set this to the supported gamma
5083 * table size when initializing the CRTC. Currently the drm core only supports a
5084 * fixed gamma table size.
5085 *
5086 * Returns:
5087 * Zero on success, negative errno on failure.
5088 */
5089 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
5090 int gamma_size)
5091 {
5092 crtc->gamma_size = gamma_size;
5093
5094 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5095 GFP_KERNEL);
5096 if (!crtc->gamma_store) {
5097 crtc->gamma_size = 0;
5098 return -ENOMEM;
5099 }
5100
5101 return 0;
5102 }
5103 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5104
5105 /**
5106 * drm_mode_gamma_set_ioctl - set the gamma table
5107 * @dev: DRM device
5108 * @data: ioctl data
5109 * @file_priv: DRM file info
5110 *
5111 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5112 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5113 *
5114 * Called by the user via ioctl.
5115 *
5116 * Returns:
5117 * Zero on success, negative errno on failure.
5118 */
5119 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5120 void *data, struct drm_file *file_priv)
5121 {
5122 struct drm_mode_crtc_lut *crtc_lut = data;
5123 struct drm_crtc *crtc;
5124 void *r_base, *g_base, *b_base;
5125 int size;
5126 int ret = 0;
5127
5128 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5129 return -EINVAL;
5130
5131 drm_modeset_lock_all(dev);
5132 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5133 if (!crtc) {
5134 ret = -ENOENT;
5135 goto out;
5136 }
5137
5138 if (crtc->funcs->gamma_set == NULL) {
5139 ret = -ENOSYS;
5140 goto out;
5141 }
5142
5143 /* memcpy into gamma store */
5144 if (crtc_lut->gamma_size != crtc->gamma_size) {
5145 ret = -EINVAL;
5146 goto out;
5147 }
5148
5149 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5150 r_base = crtc->gamma_store;
5151 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5152 ret = -EFAULT;
5153 goto out;
5154 }
5155
5156 g_base = r_base + size;
5157 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5158 ret = -EFAULT;
5159 goto out;
5160 }
5161
5162 b_base = g_base + size;
5163 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5164 ret = -EFAULT;
5165 goto out;
5166 }
5167
5168 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
5169
5170 out:
5171 drm_modeset_unlock_all(dev);
5172 return ret;
5173
5174 }
5175
5176 /**
5177 * drm_mode_gamma_get_ioctl - get the gamma table
5178 * @dev: DRM device
5179 * @data: ioctl data
5180 * @file_priv: DRM file info
5181 *
5182 * Copy the current gamma table into the storage provided. This also provides
5183 * the gamma table size the driver expects, which can be used to size the
5184 * allocated storage.
5185 *
5186 * Called by the user via ioctl.
5187 *
5188 * Returns:
5189 * Zero on success, negative errno on failure.
5190 */
5191 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5192 void *data, struct drm_file *file_priv)
5193 {
5194 struct drm_mode_crtc_lut *crtc_lut = data;
5195 struct drm_crtc *crtc;
5196 void *r_base, *g_base, *b_base;
5197 int size;
5198 int ret = 0;
5199
5200 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5201 return -EINVAL;
5202
5203 drm_modeset_lock_all(dev);
5204 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5205 if (!crtc) {
5206 ret = -ENOENT;
5207 goto out;
5208 }
5209
5210 /* memcpy into gamma store */
5211 if (crtc_lut->gamma_size != crtc->gamma_size) {
5212 ret = -EINVAL;
5213 goto out;
5214 }
5215
5216 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5217 r_base = crtc->gamma_store;
5218 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5219 ret = -EFAULT;
5220 goto out;
5221 }
5222
5223 g_base = r_base + size;
5224 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5225 ret = -EFAULT;
5226 goto out;
5227 }
5228
5229 b_base = g_base + size;
5230 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5231 ret = -EFAULT;
5232 goto out;
5233 }
5234 out:
5235 drm_modeset_unlock_all(dev);
5236 return ret;
5237 }
5238
5239 /**
5240 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5241 * @dev: DRM device
5242 * @data: ioctl data
5243 * @file_priv: DRM file info
5244 *
5245 * This schedules an asynchronous update on a given CRTC, called page flip.
5246 * Optionally a drm event is generated to signal the completion of the event.
5247 * Generic drivers cannot assume that a pageflip with changed framebuffer
5248 * properties (including driver specific metadata like tiling layout) will work,
5249 * but some drivers support e.g. pixel format changes through the pageflip
5250 * ioctl.
5251 *
5252 * Called by the user via ioctl.
5253 *
5254 * Returns:
5255 * Zero on success, negative errno on failure.
5256 */
5257 int drm_mode_page_flip_ioctl(struct drm_device *dev,
5258 void *data, struct drm_file *file_priv)
5259 {
5260 struct drm_mode_crtc_page_flip *page_flip = data;
5261 struct drm_crtc *crtc;
5262 struct drm_framebuffer *fb = NULL;
5263 struct drm_pending_vblank_event *e = NULL;
5264 unsigned long flags;
5265 int ret = -EINVAL;
5266
5267 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5268 return -EINVAL;
5269
5270 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5271 page_flip->reserved != 0)
5272 return -EINVAL;
5273
5274 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5275 return -EINVAL;
5276
5277 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5278 if (!crtc)
5279 return -ENOENT;
5280
5281 drm_modeset_lock_crtc(crtc, crtc->primary);
5282 if (crtc->primary->fb == NULL) {
5283 /* The framebuffer is currently unbound, presumably
5284 * due to a hotplug event, that userspace has not
5285 * yet discovered.
5286 */
5287 ret = -EBUSY;
5288 goto out;
5289 }
5290
5291 if (crtc->funcs->page_flip == NULL)
5292 goto out;
5293
5294 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
5295 if (!fb) {
5296 ret = -ENOENT;
5297 goto out;
5298 }
5299
5300 if (crtc->state) {
5301 const struct drm_plane_state *state = crtc->primary->state;
5302
5303 ret = check_src_coords(state->src_x, state->src_y,
5304 state->src_w, state->src_h, fb);
5305 } else {
5306 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5307 }
5308 if (ret)
5309 goto out;
5310
5311 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
5312 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5313 ret = -EINVAL;
5314 goto out;
5315 }
5316
5317 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5318 ret = -ENOMEM;
5319 spin_lock_irqsave(&dev->event_lock, flags);
5320 if (file_priv->event_space < sizeof(e->event)) {
5321 spin_unlock_irqrestore(&dev->event_lock, flags);
5322 goto out;
5323 }
5324 file_priv->event_space -= sizeof(e->event);
5325 spin_unlock_irqrestore(&dev->event_lock, flags);
5326
5327 e = kzalloc(sizeof(*e), GFP_KERNEL);
5328 if (e == NULL) {
5329 spin_lock_irqsave(&dev->event_lock, flags);
5330 file_priv->event_space += sizeof(e->event);
5331 spin_unlock_irqrestore(&dev->event_lock, flags);
5332 goto out;
5333 }
5334
5335 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
5336 e->event.base.length = sizeof(e->event);
5337 e->event.user_data = page_flip->user_data;
5338 e->base.event = &e->event.base;
5339 e->base.file_priv = file_priv;
5340 e->base.destroy =
5341 (void (*) (struct drm_pending_event *)) kfree;
5342 }
5343
5344 crtc->primary->old_fb = crtc->primary->fb;
5345 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
5346 if (ret) {
5347 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5348 spin_lock_irqsave(&dev->event_lock, flags);
5349 file_priv->event_space += sizeof(e->event);
5350 spin_unlock_irqrestore(&dev->event_lock, flags);
5351 kfree(e);
5352 }
5353 /* Keep the old fb, don't unref it. */
5354 crtc->primary->old_fb = NULL;
5355 } else {
5356 crtc->primary->fb = fb;
5357 /* Unref only the old framebuffer. */
5358 fb = NULL;
5359 }
5360
5361 out:
5362 if (fb)
5363 drm_framebuffer_unreference(fb);
5364 if (crtc->primary->old_fb)
5365 drm_framebuffer_unreference(crtc->primary->old_fb);
5366 crtc->primary->old_fb = NULL;
5367 drm_modeset_unlock_crtc(crtc);
5368
5369 return ret;
5370 }
5371
5372 /**
5373 * drm_mode_config_reset - call ->reset callbacks
5374 * @dev: drm device
5375 *
5376 * This functions calls all the crtc's, encoder's and connector's ->reset
5377 * callback. Drivers can use this in e.g. their driver load or resume code to
5378 * reset hardware and software state.
5379 */
5380 void drm_mode_config_reset(struct drm_device *dev)
5381 {
5382 struct drm_crtc *crtc;
5383 struct drm_plane *plane;
5384 struct drm_encoder *encoder;
5385 struct drm_connector *connector;
5386
5387 drm_for_each_plane(plane, dev)
5388 if (plane->funcs->reset)
5389 plane->funcs->reset(plane);
5390
5391 drm_for_each_crtc(crtc, dev)
5392 if (crtc->funcs->reset)
5393 crtc->funcs->reset(crtc);
5394
5395 drm_for_each_encoder(encoder, dev)
5396 if (encoder->funcs->reset)
5397 encoder->funcs->reset(encoder);
5398
5399 mutex_lock(&dev->mode_config.mutex);
5400 drm_for_each_connector(connector, dev)
5401 if (connector->funcs->reset)
5402 connector->funcs->reset(connector);
5403 mutex_unlock(&dev->mode_config.mutex);
5404 }
5405 EXPORT_SYMBOL(drm_mode_config_reset);
5406
5407 /**
5408 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5409 * @dev: DRM device
5410 * @data: ioctl data
5411 * @file_priv: DRM file info
5412 *
5413 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5414 * TTM or something else entirely) and returns the resulting buffer handle. This
5415 * handle can then be wrapped up into a framebuffer modeset object.
5416 *
5417 * Note that userspace is not allowed to use such objects for render
5418 * acceleration - drivers must create their own private ioctls for such a use
5419 * case.
5420 *
5421 * Called by the user via ioctl.
5422 *
5423 * Returns:
5424 * Zero on success, negative errno on failure.
5425 */
5426 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5427 void *data, struct drm_file *file_priv)
5428 {
5429 struct drm_mode_create_dumb *args = data;
5430 u32 cpp, stride, size;
5431
5432 if (!dev->driver->dumb_create)
5433 return -ENOSYS;
5434 if (!args->width || !args->height || !args->bpp)
5435 return -EINVAL;
5436
5437 /* overflow checks for 32bit size calculations */
5438 /* NOTE: DIV_ROUND_UP() can overflow */
5439 cpp = DIV_ROUND_UP(args->bpp, 8);
5440 if (!cpp || cpp > 0xffffffffU / args->width)
5441 return -EINVAL;
5442 stride = cpp * args->width;
5443 if (args->height > 0xffffffffU / stride)
5444 return -EINVAL;
5445
5446 /* test for wrap-around */
5447 size = args->height * stride;
5448 if (PAGE_ALIGN(size) == 0)
5449 return -EINVAL;
5450
5451 /*
5452 * handle, pitch and size are output parameters. Zero them out to
5453 * prevent drivers from accidentally using uninitialized data. Since
5454 * not all existing userspace is clearing these fields properly we
5455 * cannot reject IOCTL with garbage in them.
5456 */
5457 args->handle = 0;
5458 args->pitch = 0;
5459 args->size = 0;
5460
5461 return dev->driver->dumb_create(file_priv, dev, args);
5462 }
5463
5464 /**
5465 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5466 * @dev: DRM device
5467 * @data: ioctl data
5468 * @file_priv: DRM file info
5469 *
5470 * Allocate an offset in the drm device node's address space to be able to
5471 * memory map a dumb buffer.
5472 *
5473 * Called by the user via ioctl.
5474 *
5475 * Returns:
5476 * Zero on success, negative errno on failure.
5477 */
5478 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5479 void *data, struct drm_file *file_priv)
5480 {
5481 struct drm_mode_map_dumb *args = data;
5482
5483 /* call driver ioctl to get mmap offset */
5484 if (!dev->driver->dumb_map_offset)
5485 return -ENOSYS;
5486
5487 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5488 }
5489
5490 /**
5491 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5492 * @dev: DRM device
5493 * @data: ioctl data
5494 * @file_priv: DRM file info
5495 *
5496 * This destroys the userspace handle for the given dumb backing storage buffer.
5497 * Since buffer objects must be reference counted in the kernel a buffer object
5498 * won't be immediately freed if a framebuffer modeset object still uses it.
5499 *
5500 * Called by the user via ioctl.
5501 *
5502 * Returns:
5503 * Zero on success, negative errno on failure.
5504 */
5505 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5506 void *data, struct drm_file *file_priv)
5507 {
5508 struct drm_mode_destroy_dumb *args = data;
5509
5510 if (!dev->driver->dumb_destroy)
5511 return -ENOSYS;
5512
5513 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5514 }
5515
5516 /**
5517 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5518 * @format: pixel format (DRM_FORMAT_*)
5519 * @depth: storage for the depth value
5520 * @bpp: storage for the bpp value
5521 *
5522 * This only supports RGB formats here for compat with code that doesn't use
5523 * pixel formats directly yet.
5524 */
5525 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5526 int *bpp)
5527 {
5528 switch (format) {
5529 case DRM_FORMAT_C8:
5530 case DRM_FORMAT_RGB332:
5531 case DRM_FORMAT_BGR233:
5532 *depth = 8;
5533 *bpp = 8;
5534 break;
5535 case DRM_FORMAT_XRGB1555:
5536 case DRM_FORMAT_XBGR1555:
5537 case DRM_FORMAT_RGBX5551:
5538 case DRM_FORMAT_BGRX5551:
5539 case DRM_FORMAT_ARGB1555:
5540 case DRM_FORMAT_ABGR1555:
5541 case DRM_FORMAT_RGBA5551:
5542 case DRM_FORMAT_BGRA5551:
5543 *depth = 15;
5544 *bpp = 16;
5545 break;
5546 case DRM_FORMAT_RGB565:
5547 case DRM_FORMAT_BGR565:
5548 *depth = 16;
5549 *bpp = 16;
5550 break;
5551 case DRM_FORMAT_RGB888:
5552 case DRM_FORMAT_BGR888:
5553 *depth = 24;
5554 *bpp = 24;
5555 break;
5556 case DRM_FORMAT_XRGB8888:
5557 case DRM_FORMAT_XBGR8888:
5558 case DRM_FORMAT_RGBX8888:
5559 case DRM_FORMAT_BGRX8888:
5560 *depth = 24;
5561 *bpp = 32;
5562 break;
5563 case DRM_FORMAT_XRGB2101010:
5564 case DRM_FORMAT_XBGR2101010:
5565 case DRM_FORMAT_RGBX1010102:
5566 case DRM_FORMAT_BGRX1010102:
5567 case DRM_FORMAT_ARGB2101010:
5568 case DRM_FORMAT_ABGR2101010:
5569 case DRM_FORMAT_RGBA1010102:
5570 case DRM_FORMAT_BGRA1010102:
5571 *depth = 30;
5572 *bpp = 32;
5573 break;
5574 case DRM_FORMAT_ARGB8888:
5575 case DRM_FORMAT_ABGR8888:
5576 case DRM_FORMAT_RGBA8888:
5577 case DRM_FORMAT_BGRA8888:
5578 *depth = 32;
5579 *bpp = 32;
5580 break;
5581 default:
5582 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5583 drm_get_format_name(format));
5584 *depth = 0;
5585 *bpp = 0;
5586 break;
5587 }
5588 }
5589 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
5590
5591 /**
5592 * drm_format_num_planes - get the number of planes for format
5593 * @format: pixel format (DRM_FORMAT_*)
5594 *
5595 * Returns:
5596 * The number of planes used by the specified pixel format.
5597 */
5598 int drm_format_num_planes(uint32_t format)
5599 {
5600 switch (format) {
5601 case DRM_FORMAT_YUV410:
5602 case DRM_FORMAT_YVU410:
5603 case DRM_FORMAT_YUV411:
5604 case DRM_FORMAT_YVU411:
5605 case DRM_FORMAT_YUV420:
5606 case DRM_FORMAT_YVU420:
5607 case DRM_FORMAT_YUV422:
5608 case DRM_FORMAT_YVU422:
5609 case DRM_FORMAT_YUV444:
5610 case DRM_FORMAT_YVU444:
5611 return 3;
5612 case DRM_FORMAT_NV12:
5613 case DRM_FORMAT_NV21:
5614 case DRM_FORMAT_NV16:
5615 case DRM_FORMAT_NV61:
5616 case DRM_FORMAT_NV24:
5617 case DRM_FORMAT_NV42:
5618 return 2;
5619 default:
5620 return 1;
5621 }
5622 }
5623 EXPORT_SYMBOL(drm_format_num_planes);
5624
5625 /**
5626 * drm_format_plane_cpp - determine the bytes per pixel value
5627 * @format: pixel format (DRM_FORMAT_*)
5628 * @plane: plane index
5629 *
5630 * Returns:
5631 * The bytes per pixel value for the specified plane.
5632 */
5633 int drm_format_plane_cpp(uint32_t format, int plane)
5634 {
5635 unsigned int depth;
5636 int bpp;
5637
5638 if (plane >= drm_format_num_planes(format))
5639 return 0;
5640
5641 switch (format) {
5642 case DRM_FORMAT_YUYV:
5643 case DRM_FORMAT_YVYU:
5644 case DRM_FORMAT_UYVY:
5645 case DRM_FORMAT_VYUY:
5646 return 2;
5647 case DRM_FORMAT_NV12:
5648 case DRM_FORMAT_NV21:
5649 case DRM_FORMAT_NV16:
5650 case DRM_FORMAT_NV61:
5651 case DRM_FORMAT_NV24:
5652 case DRM_FORMAT_NV42:
5653 return plane ? 2 : 1;
5654 case DRM_FORMAT_YUV410:
5655 case DRM_FORMAT_YVU410:
5656 case DRM_FORMAT_YUV411:
5657 case DRM_FORMAT_YVU411:
5658 case DRM_FORMAT_YUV420:
5659 case DRM_FORMAT_YVU420:
5660 case DRM_FORMAT_YUV422:
5661 case DRM_FORMAT_YVU422:
5662 case DRM_FORMAT_YUV444:
5663 case DRM_FORMAT_YVU444:
5664 return 1;
5665 default:
5666 drm_fb_get_bpp_depth(format, &depth, &bpp);
5667 return bpp >> 3;
5668 }
5669 }
5670 EXPORT_SYMBOL(drm_format_plane_cpp);
5671
5672 /**
5673 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5674 * @format: pixel format (DRM_FORMAT_*)
5675 *
5676 * Returns:
5677 * The horizontal chroma subsampling factor for the
5678 * specified pixel format.
5679 */
5680 int drm_format_horz_chroma_subsampling(uint32_t format)
5681 {
5682 switch (format) {
5683 case DRM_FORMAT_YUV411:
5684 case DRM_FORMAT_YVU411:
5685 case DRM_FORMAT_YUV410:
5686 case DRM_FORMAT_YVU410:
5687 return 4;
5688 case DRM_FORMAT_YUYV:
5689 case DRM_FORMAT_YVYU:
5690 case DRM_FORMAT_UYVY:
5691 case DRM_FORMAT_VYUY:
5692 case DRM_FORMAT_NV12:
5693 case DRM_FORMAT_NV21:
5694 case DRM_FORMAT_NV16:
5695 case DRM_FORMAT_NV61:
5696 case DRM_FORMAT_YUV422:
5697 case DRM_FORMAT_YVU422:
5698 case DRM_FORMAT_YUV420:
5699 case DRM_FORMAT_YVU420:
5700 return 2;
5701 default:
5702 return 1;
5703 }
5704 }
5705 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5706
5707 /**
5708 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5709 * @format: pixel format (DRM_FORMAT_*)
5710 *
5711 * Returns:
5712 * The vertical chroma subsampling factor for the
5713 * specified pixel format.
5714 */
5715 int drm_format_vert_chroma_subsampling(uint32_t format)
5716 {
5717 switch (format) {
5718 case DRM_FORMAT_YUV410:
5719 case DRM_FORMAT_YVU410:
5720 return 4;
5721 case DRM_FORMAT_YUV420:
5722 case DRM_FORMAT_YVU420:
5723 case DRM_FORMAT_NV12:
5724 case DRM_FORMAT_NV21:
5725 return 2;
5726 default:
5727 return 1;
5728 }
5729 }
5730 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5731
5732 /**
5733 * drm_rotation_simplify() - Try to simplify the rotation
5734 * @rotation: Rotation to be simplified
5735 * @supported_rotations: Supported rotations
5736 *
5737 * Attempt to simplify the rotation to a form that is supported.
5738 * Eg. if the hardware supports everything except DRM_REFLECT_X
5739 * one could call this function like this:
5740 *
5741 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5742 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5743 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5744 *
5745 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5746 * transforms the hardware supports, this function may not
5747 * be able to produce a supported transform, so the caller should
5748 * check the result afterwards.
5749 */
5750 unsigned int drm_rotation_simplify(unsigned int rotation,
5751 unsigned int supported_rotations)
5752 {
5753 if (rotation & ~supported_rotations) {
5754 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5755 rotation = (rotation & DRM_REFLECT_MASK) |
5756 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
5757 }
5758
5759 return rotation;
5760 }
5761 EXPORT_SYMBOL(drm_rotation_simplify);
5762
5763 /**
5764 * drm_mode_config_init - initialize DRM mode_configuration structure
5765 * @dev: DRM device
5766 *
5767 * Initialize @dev's mode_config structure, used for tracking the graphics
5768 * configuration of @dev.
5769 *
5770 * Since this initializes the modeset locks, no locking is possible. Which is no
5771 * problem, since this should happen single threaded at init time. It is the
5772 * driver's problem to ensure this guarantee.
5773 *
5774 */
5775 void drm_mode_config_init(struct drm_device *dev)
5776 {
5777 mutex_init(&dev->mode_config.mutex);
5778 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5779 mutex_init(&dev->mode_config.idr_mutex);
5780 mutex_init(&dev->mode_config.fb_lock);
5781 mutex_init(&dev->mode_config.blob_lock);
5782 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5783 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5784 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5785 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5786 INIT_LIST_HEAD(&dev->mode_config.property_list);
5787 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5788 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5789 idr_init(&dev->mode_config.crtc_idr);
5790 idr_init(&dev->mode_config.tile_idr);
5791
5792 drm_modeset_lock_all(dev);
5793 drm_mode_create_standard_properties(dev);
5794 drm_modeset_unlock_all(dev);
5795
5796 /* Just to be sure */
5797 dev->mode_config.num_fb = 0;
5798 dev->mode_config.num_connector = 0;
5799 dev->mode_config.num_crtc = 0;
5800 dev->mode_config.num_encoder = 0;
5801 dev->mode_config.num_overlay_plane = 0;
5802 dev->mode_config.num_total_plane = 0;
5803 }
5804 EXPORT_SYMBOL(drm_mode_config_init);
5805
5806 /**
5807 * drm_mode_config_cleanup - free up DRM mode_config info
5808 * @dev: DRM device
5809 *
5810 * Free up all the connectors and CRTCs associated with this DRM device, then
5811 * free up the framebuffers and associated buffer objects.
5812 *
5813 * Note that since this /should/ happen single-threaded at driver/device
5814 * teardown time, no locking is required. It's the driver's job to ensure that
5815 * this guarantee actually holds true.
5816 *
5817 * FIXME: cleanup any dangling user buffer objects too
5818 */
5819 void drm_mode_config_cleanup(struct drm_device *dev)
5820 {
5821 struct drm_connector *connector, *ot;
5822 struct drm_crtc *crtc, *ct;
5823 struct drm_encoder *encoder, *enct;
5824 struct drm_framebuffer *fb, *fbt;
5825 struct drm_property *property, *pt;
5826 struct drm_property_blob *blob, *bt;
5827 struct drm_plane *plane, *plt;
5828
5829 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5830 head) {
5831 encoder->funcs->destroy(encoder);
5832 }
5833
5834 list_for_each_entry_safe(connector, ot,
5835 &dev->mode_config.connector_list, head) {
5836 connector->funcs->destroy(connector);
5837 }
5838
5839 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5840 head) {
5841 drm_property_destroy(dev, property);
5842 }
5843
5844 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5845 head_global) {
5846 drm_property_unreference_blob(blob);
5847 }
5848
5849 /*
5850 * Single-threaded teardown context, so it's not required to grab the
5851 * fb_lock to protect against concurrent fb_list access. Contrary, it
5852 * would actually deadlock with the drm_framebuffer_cleanup function.
5853 *
5854 * Also, if there are any framebuffers left, that's a driver leak now,
5855 * so politely WARN about this.
5856 */
5857 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5858 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5859 drm_framebuffer_free(&fb->refcount);
5860 }
5861
5862 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5863 head) {
5864 plane->funcs->destroy(plane);
5865 }
5866
5867 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5868 crtc->funcs->destroy(crtc);
5869 }
5870
5871 idr_destroy(&dev->mode_config.tile_idr);
5872 idr_destroy(&dev->mode_config.crtc_idr);
5873 mutex_destroy(&dev->mode_config.blob_lock);
5874 mutex_destroy(&dev->mode_config.fb_lock);
5875 mutex_destroy(&dev->mode_config.idr_mutex);
5876 mutex_destroy(&dev->mode_config.mutex);
5877 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5878 }
5879 EXPORT_SYMBOL(drm_mode_config_cleanup);
5880
5881 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5882 unsigned int supported_rotations)
5883 {
5884 static const struct drm_prop_enum_list props[] = {
5885 { DRM_ROTATE_0, "rotate-0" },
5886 { DRM_ROTATE_90, "rotate-90" },
5887 { DRM_ROTATE_180, "rotate-180" },
5888 { DRM_ROTATE_270, "rotate-270" },
5889 { DRM_REFLECT_X, "reflect-x" },
5890 { DRM_REFLECT_Y, "reflect-y" },
5891 };
5892
5893 return drm_property_create_bitmask(dev, 0, "rotation",
5894 props, ARRAY_SIZE(props),
5895 supported_rotations);
5896 }
5897 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5898
5899 /**
5900 * DOC: Tile group
5901 *
5902 * Tile groups are used to represent tiled monitors with a unique
5903 * integer identifier. Tiled monitors using DisplayID v1.3 have
5904 * a unique 8-byte handle, we store this in a tile group, so we
5905 * have a common identifier for all tiles in a monitor group.
5906 */
5907 static void drm_tile_group_free(struct kref *kref)
5908 {
5909 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5910 struct drm_device *dev = tg->dev;
5911 mutex_lock(&dev->mode_config.idr_mutex);
5912 idr_remove(&dev->mode_config.tile_idr, tg->id);
5913 mutex_unlock(&dev->mode_config.idr_mutex);
5914 kfree(tg);
5915 }
5916
5917 /**
5918 * drm_mode_put_tile_group - drop a reference to a tile group.
5919 * @dev: DRM device
5920 * @tg: tile group to drop reference to.
5921 *
5922 * drop reference to tile group and free if 0.
5923 */
5924 void drm_mode_put_tile_group(struct drm_device *dev,
5925 struct drm_tile_group *tg)
5926 {
5927 kref_put(&tg->refcount, drm_tile_group_free);
5928 }
5929
5930 /**
5931 * drm_mode_get_tile_group - get a reference to an existing tile group
5932 * @dev: DRM device
5933 * @topology: 8-bytes unique per monitor.
5934 *
5935 * Use the unique bytes to get a reference to an existing tile group.
5936 *
5937 * RETURNS:
5938 * tile group or NULL if not found.
5939 */
5940 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5941 char topology[8])
5942 {
5943 struct drm_tile_group *tg;
5944 int id;
5945 mutex_lock(&dev->mode_config.idr_mutex);
5946 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5947 if (!memcmp(tg->group_data, topology, 8)) {
5948 if (!kref_get_unless_zero(&tg->refcount))
5949 tg = NULL;
5950 mutex_unlock(&dev->mode_config.idr_mutex);
5951 return tg;
5952 }
5953 }
5954 mutex_unlock(&dev->mode_config.idr_mutex);
5955 return NULL;
5956 }
5957 EXPORT_SYMBOL(drm_mode_get_tile_group);
5958
5959 /**
5960 * drm_mode_create_tile_group - create a tile group from a displayid description
5961 * @dev: DRM device
5962 * @topology: 8-bytes unique per monitor.
5963 *
5964 * Create a tile group for the unique monitor, and get a unique
5965 * identifier for the tile group.
5966 *
5967 * RETURNS:
5968 * new tile group or error.
5969 */
5970 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5971 char topology[8])
5972 {
5973 struct drm_tile_group *tg;
5974 int ret;
5975
5976 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5977 if (!tg)
5978 return ERR_PTR(-ENOMEM);
5979
5980 kref_init(&tg->refcount);
5981 memcpy(tg->group_data, topology, 8);
5982 tg->dev = dev;
5983
5984 idr_preload(GFP_KERNEL);
5985 mutex_lock(&dev->mode_config.idr_mutex);
5986 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5987 if (ret >= 0) {
5988 tg->id = ret;
5989 } else {
5990 kfree(tg);
5991 tg = ERR_PTR(ret);
5992 }
5993
5994 mutex_unlock(&dev->mode_config.idr_mutex);
5995 idr_preload_end();
5996 return tg;
5997 }
5998 EXPORT_SYMBOL(drm_mode_create_tile_group);
5999