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