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