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