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