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