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