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