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