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