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