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