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