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