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