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