drm: Add support for multiple plane types (v2)
[deliverable/linux.git] / drivers / gpu / drm / drm_crtc.c
CommitLineData
f453ba04
DA
1/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
6ba6d03e 32#include <linux/ctype.h>
f453ba04 33#include <linux/list.h>
5a0e3ad6 34#include <linux/slab.h>
2d1a8a48 35#include <linux/export.h>
760285e7
DH
36#include <drm/drmP.h>
37#include <drm/drm_crtc.h>
38#include <drm/drm_edid.h>
39#include <drm/drm_fourcc.h>
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
VS
1002/**
1003 * drm_plane_init - Initialise a new plane object
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
1010 * @priv: plane is private (hidden from userspace)?
1011 *
c8e32cc1
DV
1012 * Inits a preallocate plane object created as base part of a driver plane
1013 * object.
35f2c3ae 1014 *
c8e32cc1 1015 * Returns:
35f2c3ae
VS
1016 * Zero on success, error code on failure.
1017 */
8cf5c917
JB
1018int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1019 unsigned long possible_crtcs,
1020 const struct drm_plane_funcs *funcs,
0a7eb243
RC
1021 const uint32_t *formats, uint32_t format_count,
1022 bool priv)
8cf5c917 1023{
6bfc56aa
VS
1024 int ret;
1025
84849903 1026 drm_modeset_lock_all(dev);
8cf5c917 1027
6bfc56aa
VS
1028 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1029 if (ret)
1030 goto out;
1031
4d93914a 1032 plane->base.properties = &plane->properties;
8cf5c917 1033 plane->dev = dev;
8cf5c917
JB
1034 plane->funcs = funcs;
1035 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1036 GFP_KERNEL);
1037 if (!plane->format_types) {
1038 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1039 drm_mode_object_put(dev, &plane->base);
6bfc56aa
VS
1040 ret = -ENOMEM;
1041 goto out;
8cf5c917
JB
1042 }
1043
308e5bcb 1044 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
8cf5c917
JB
1045 plane->format_count = format_count;
1046 plane->possible_crtcs = possible_crtcs;
e27dde3e 1047 plane->type = DRM_PLANE_TYPE_OVERLAY;
8cf5c917 1048
0a7eb243
RC
1049 /* private planes are not exposed to userspace, but depending on
1050 * display hardware, might be convenient to allow sharing programming
1051 * for the scanout engine with the crtc implementation.
1052 */
1053 if (!priv) {
1054 list_add_tail(&plane->head, &dev->mode_config.plane_list);
e27dde3e
MR
1055 dev->mode_config.num_total_plane++;
1056 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1057 dev->mode_config.num_overlay_plane++;
0a7eb243
RC
1058 } else {
1059 INIT_LIST_HEAD(&plane->head);
1060 }
8cf5c917 1061
6bfc56aa 1062 out:
84849903 1063 drm_modeset_unlock_all(dev);
8cf5c917 1064
6bfc56aa 1065 return ret;
8cf5c917
JB
1066}
1067EXPORT_SYMBOL(drm_plane_init);
1068
35f2c3ae
VS
1069/**
1070 * drm_plane_cleanup - Clean up the core plane usage
1071 * @plane: plane to cleanup
1072 *
1073 * This function cleans up @plane and removes it from the DRM mode setting
1074 * core. Note that the function does *not* free the plane structure itself,
1075 * this is the responsibility of the caller.
1076 */
8cf5c917
JB
1077void drm_plane_cleanup(struct drm_plane *plane)
1078{
1079 struct drm_device *dev = plane->dev;
1080
84849903 1081 drm_modeset_lock_all(dev);
8cf5c917
JB
1082 kfree(plane->format_types);
1083 drm_mode_object_put(dev, &plane->base);
0a7eb243
RC
1084 /* if not added to a list, it must be a private plane */
1085 if (!list_empty(&plane->head)) {
1086 list_del(&plane->head);
e27dde3e
MR
1087 dev->mode_config.num_total_plane--;
1088 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1089 dev->mode_config.num_overlay_plane--;
0a7eb243 1090 }
84849903 1091 drm_modeset_unlock_all(dev);
8cf5c917
JB
1092}
1093EXPORT_SYMBOL(drm_plane_cleanup);
1094
35f2c3ae
VS
1095/**
1096 * drm_plane_force_disable - Forcibly disable a plane
1097 * @plane: plane to disable
1098 *
1099 * Forces the plane to be disabled.
1100 *
1101 * Used when the plane's current framebuffer is destroyed,
1102 * and when restoring fbdev mode.
1103 */
9125e618
VS
1104void drm_plane_force_disable(struct drm_plane *plane)
1105{
1106 int ret;
1107
1108 if (!plane->fb)
1109 return;
1110
1111 ret = plane->funcs->disable_plane(plane);
1112 if (ret)
1113 DRM_ERROR("failed to disable plane with busy fb\n");
1114 /* disconnect the plane from the fb and crtc: */
1115 __drm_framebuffer_unreference(plane->fb);
1116 plane->fb = NULL;
1117 plane->crtc = NULL;
1118}
1119EXPORT_SYMBOL(drm_plane_force_disable);
1120
f453ba04
DA
1121static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1122{
1123 struct drm_property *edid;
1124 struct drm_property *dpms;
f453ba04
DA
1125
1126 /*
1127 * Standard properties (apply to all connectors)
1128 */
1129 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1130 DRM_MODE_PROP_IMMUTABLE,
1131 "EDID", 0);
1132 dev->mode_config.edid_property = edid;
1133
4a67d391
SH
1134 dpms = drm_property_create_enum(dev, 0,
1135 "DPMS", drm_dpms_enum_list,
1136 ARRAY_SIZE(drm_dpms_enum_list));
f453ba04
DA
1137 dev->mode_config.dpms_property = dpms;
1138
1139 return 0;
1140}
1141
1142/**
1143 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1144 * @dev: DRM device
1145 *
1146 * Called by a driver the first time a DVI-I connector is made.
1147 */
1148int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1149{
1150 struct drm_property *dvi_i_selector;
1151 struct drm_property *dvi_i_subconnector;
f453ba04
DA
1152
1153 if (dev->mode_config.dvi_i_select_subconnector_property)
1154 return 0;
1155
1156 dvi_i_selector =
4a67d391 1157 drm_property_create_enum(dev, 0,
f453ba04 1158 "select subconnector",
4a67d391 1159 drm_dvi_i_select_enum_list,
f453ba04 1160 ARRAY_SIZE(drm_dvi_i_select_enum_list));
f453ba04
DA
1161 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1162
4a67d391 1163 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
f453ba04 1164 "subconnector",
4a67d391 1165 drm_dvi_i_subconnector_enum_list,
f453ba04 1166 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
f453ba04
DA
1167 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1168
1169 return 0;
1170}
1171EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1172
1173/**
1174 * drm_create_tv_properties - create TV specific connector properties
1175 * @dev: DRM device
1176 * @num_modes: number of different TV formats (modes) supported
1177 * @modes: array of pointers to strings containing name of each format
1178 *
1179 * Called by a driver's TV initialization routine, this function creates
1180 * the TV specific connector properties for a given device. Caller is
1181 * responsible for allocating a list of format names and passing them to
1182 * this routine.
1183 */
1184int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1185 char *modes[])
1186{
1187 struct drm_property *tv_selector;
1188 struct drm_property *tv_subconnector;
1189 int i;
1190
1191 if (dev->mode_config.tv_select_subconnector_property)
1192 return 0;
1193
1194 /*
1195 * Basic connector properties
1196 */
4a67d391 1197 tv_selector = drm_property_create_enum(dev, 0,
f453ba04 1198 "select subconnector",
4a67d391 1199 drm_tv_select_enum_list,
f453ba04 1200 ARRAY_SIZE(drm_tv_select_enum_list));
f453ba04
DA
1201 dev->mode_config.tv_select_subconnector_property = tv_selector;
1202
1203 tv_subconnector =
4a67d391
SH
1204 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1205 "subconnector",
1206 drm_tv_subconnector_enum_list,
f453ba04 1207 ARRAY_SIZE(drm_tv_subconnector_enum_list));
f453ba04
DA
1208 dev->mode_config.tv_subconnector_property = tv_subconnector;
1209
1210 /*
1211 * Other, TV specific properties: margins & TV modes.
1212 */
1213 dev->mode_config.tv_left_margin_property =
d9bc3c02 1214 drm_property_create_range(dev, 0, "left margin", 0, 100);
f453ba04
DA
1215
1216 dev->mode_config.tv_right_margin_property =
d9bc3c02 1217 drm_property_create_range(dev, 0, "right margin", 0, 100);
f453ba04
DA
1218
1219 dev->mode_config.tv_top_margin_property =
d9bc3c02 1220 drm_property_create_range(dev, 0, "top margin", 0, 100);
f453ba04
DA
1221
1222 dev->mode_config.tv_bottom_margin_property =
d9bc3c02 1223 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
f453ba04
DA
1224
1225 dev->mode_config.tv_mode_property =
1226 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1227 "mode", num_modes);
1228 for (i = 0; i < num_modes; i++)
1229 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1230 i, modes[i]);
1231
b6b7902e 1232 dev->mode_config.tv_brightness_property =
d9bc3c02 1233 drm_property_create_range(dev, 0, "brightness", 0, 100);
b6b7902e
FJ
1234
1235 dev->mode_config.tv_contrast_property =
d9bc3c02 1236 drm_property_create_range(dev, 0, "contrast", 0, 100);
b6b7902e
FJ
1237
1238 dev->mode_config.tv_flicker_reduction_property =
d9bc3c02 1239 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
b6b7902e 1240
a75f0236 1241 dev->mode_config.tv_overscan_property =
d9bc3c02 1242 drm_property_create_range(dev, 0, "overscan", 0, 100);
a75f0236
FJ
1243
1244 dev->mode_config.tv_saturation_property =
d9bc3c02 1245 drm_property_create_range(dev, 0, "saturation", 0, 100);
a75f0236
FJ
1246
1247 dev->mode_config.tv_hue_property =
d9bc3c02 1248 drm_property_create_range(dev, 0, "hue", 0, 100);
a75f0236 1249
f453ba04
DA
1250 return 0;
1251}
1252EXPORT_SYMBOL(drm_mode_create_tv_properties);
1253
1254/**
1255 * drm_mode_create_scaling_mode_property - create scaling mode property
1256 * @dev: DRM device
1257 *
1258 * Called by a driver the first time it's needed, must be attached to desired
1259 * connectors.
1260 */
1261int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1262{
1263 struct drm_property *scaling_mode;
f453ba04
DA
1264
1265 if (dev->mode_config.scaling_mode_property)
1266 return 0;
1267
1268 scaling_mode =
4a67d391
SH
1269 drm_property_create_enum(dev, 0, "scaling mode",
1270 drm_scaling_mode_enum_list,
f453ba04 1271 ARRAY_SIZE(drm_scaling_mode_enum_list));
f453ba04
DA
1272
1273 dev->mode_config.scaling_mode_property = scaling_mode;
1274
1275 return 0;
1276}
1277EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1278
884840aa
JB
1279/**
1280 * drm_mode_create_dirty_property - create dirty property
1281 * @dev: DRM device
1282 *
1283 * Called by a driver the first time it's needed, must be attached to desired
1284 * connectors.
1285 */
1286int drm_mode_create_dirty_info_property(struct drm_device *dev)
1287{
1288 struct drm_property *dirty_info;
884840aa
JB
1289
1290 if (dev->mode_config.dirty_info_property)
1291 return 0;
1292
1293 dirty_info =
4a67d391 1294 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
884840aa 1295 "dirty",
4a67d391 1296 drm_dirty_info_enum_list,
884840aa 1297 ARRAY_SIZE(drm_dirty_info_enum_list));
884840aa
JB
1298 dev->mode_config.dirty_info_property = dirty_info;
1299
1300 return 0;
1301}
1302EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1303
ea9cbb06 1304static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
f453ba04
DA
1305{
1306 uint32_t total_objects = 0;
1307
1308 total_objects += dev->mode_config.num_crtc;
1309 total_objects += dev->mode_config.num_connector;
1310 total_objects += dev->mode_config.num_encoder;
3b336ec4 1311 total_objects += dev->mode_config.num_bridge;
f453ba04 1312
f453ba04
DA
1313 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1314 if (!group->id_list)
1315 return -ENOMEM;
1316
1317 group->num_crtcs = 0;
1318 group->num_connectors = 0;
1319 group->num_encoders = 0;
3b336ec4 1320 group->num_bridges = 0;
f453ba04
DA
1321 return 0;
1322}
1323
c8e32cc1
DV
1324/*
1325 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1326 * the drm core's responsibility to set up mode control groups.
1327 */
f453ba04
DA
1328int drm_mode_group_init_legacy_group(struct drm_device *dev,
1329 struct drm_mode_group *group)
1330{
1331 struct drm_crtc *crtc;
1332 struct drm_encoder *encoder;
1333 struct drm_connector *connector;
3b336ec4 1334 struct drm_bridge *bridge;
f453ba04
DA
1335 int ret;
1336
1337 if ((ret = drm_mode_group_init(dev, group)))
1338 return ret;
1339
1340 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1341 group->id_list[group->num_crtcs++] = crtc->base.id;
1342
1343 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1344 group->id_list[group->num_crtcs + group->num_encoders++] =
1345 encoder->base.id;
1346
1347 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1348 group->id_list[group->num_crtcs + group->num_encoders +
1349 group->num_connectors++] = connector->base.id;
1350
3b336ec4
SP
1351 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1352 group->id_list[group->num_crtcs + group->num_encoders +
1353 group->num_connectors + group->num_bridges++] =
1354 bridge->base.id;
1355
f453ba04
DA
1356 return 0;
1357}
9c1dfc55 1358EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
f453ba04 1359
f453ba04
DA
1360/**
1361 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1362 * @out: drm_mode_modeinfo struct to return to the user
1363 * @in: drm_display_mode to use
1364 *
f453ba04
DA
1365 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1366 * the user.
1367 */
93bbf6db
VS
1368static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1369 const struct drm_display_mode *in)
f453ba04 1370{
e36fae38
VS
1371 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1372 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1373 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1374 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1375 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1376 "timing values too large for mode info\n");
1377
f453ba04
DA
1378 out->clock = in->clock;
1379 out->hdisplay = in->hdisplay;
1380 out->hsync_start = in->hsync_start;
1381 out->hsync_end = in->hsync_end;
1382 out->htotal = in->htotal;
1383 out->hskew = in->hskew;
1384 out->vdisplay = in->vdisplay;
1385 out->vsync_start = in->vsync_start;
1386 out->vsync_end = in->vsync_end;
1387 out->vtotal = in->vtotal;
1388 out->vscan = in->vscan;
1389 out->vrefresh = in->vrefresh;
1390 out->flags = in->flags;
1391 out->type = in->type;
1392 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1393 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1394}
1395
1396/**
74afee7d 1397 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
f453ba04
DA
1398 * @out: drm_display_mode to return to the user
1399 * @in: drm_mode_modeinfo to use
1400 *
f453ba04
DA
1401 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1402 * the caller.
90367bf6 1403 *
c8e32cc1 1404 * Returns:
90367bf6 1405 * Zero on success, errno on failure.
f453ba04 1406 */
93bbf6db
VS
1407static int drm_crtc_convert_umode(struct drm_display_mode *out,
1408 const struct drm_mode_modeinfo *in)
f453ba04 1409{
90367bf6
VS
1410 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1411 return -ERANGE;
1412
5848ad40
DL
1413 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1414 return -EINVAL;
1415
f453ba04
DA
1416 out->clock = in->clock;
1417 out->hdisplay = in->hdisplay;
1418 out->hsync_start = in->hsync_start;
1419 out->hsync_end = in->hsync_end;
1420 out->htotal = in->htotal;
1421 out->hskew = in->hskew;
1422 out->vdisplay = in->vdisplay;
1423 out->vsync_start = in->vsync_start;
1424 out->vsync_end = in->vsync_end;
1425 out->vtotal = in->vtotal;
1426 out->vscan = in->vscan;
1427 out->vrefresh = in->vrefresh;
1428 out->flags = in->flags;
1429 out->type = in->type;
1430 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1431 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
90367bf6
VS
1432
1433 return 0;
f453ba04
DA
1434}
1435
1436/**
1437 * drm_mode_getresources - get graphics configuration
065a50ed
DV
1438 * @dev: drm device for the ioctl
1439 * @data: data pointer for the ioctl
1440 * @file_priv: drm file for the ioctl call
f453ba04 1441 *
f453ba04
DA
1442 * Construct a set of configuration description structures and return
1443 * them to the user, including CRTC, connector and framebuffer configuration.
1444 *
1445 * Called by the user via ioctl.
1446 *
c8e32cc1 1447 * Returns:
f453ba04
DA
1448 * Zero on success, errno on failure.
1449 */
1450int drm_mode_getresources(struct drm_device *dev, void *data,
1451 struct drm_file *file_priv)
1452{
1453 struct drm_mode_card_res *card_res = data;
1454 struct list_head *lh;
1455 struct drm_framebuffer *fb;
1456 struct drm_connector *connector;
1457 struct drm_crtc *crtc;
1458 struct drm_encoder *encoder;
1459 int ret = 0;
1460 int connector_count = 0;
1461 int crtc_count = 0;
1462 int fb_count = 0;
1463 int encoder_count = 0;
1464 int copied = 0, i;
1465 uint32_t __user *fb_id;
1466 uint32_t __user *crtc_id;
1467 uint32_t __user *connector_id;
1468 uint32_t __user *encoder_id;
1469 struct drm_mode_group *mode_group;
1470
fb3b06c8
DA
1471 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1472 return -EINVAL;
1473
f453ba04 1474
4b096ac1 1475 mutex_lock(&file_priv->fbs_lock);
f453ba04
DA
1476 /*
1477 * For the non-control nodes we need to limit the list of resources
1478 * by IDs in the group list for this node
1479 */
1480 list_for_each(lh, &file_priv->fbs)
1481 fb_count++;
1482
4b096ac1
DV
1483 /* handle this in 4 parts */
1484 /* FBs */
1485 if (card_res->count_fbs >= fb_count) {
1486 copied = 0;
1487 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1488 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1489 if (put_user(fb->base.id, fb_id + copied)) {
1490 mutex_unlock(&file_priv->fbs_lock);
1491 return -EFAULT;
1492 }
1493 copied++;
1494 }
1495 }
1496 card_res->count_fbs = fb_count;
1497 mutex_unlock(&file_priv->fbs_lock);
1498
1499 drm_modeset_lock_all(dev);
43683057 1500 if (!drm_is_primary_client(file_priv)) {
f453ba04 1501
09f308f7 1502 mode_group = NULL;
f453ba04
DA
1503 list_for_each(lh, &dev->mode_config.crtc_list)
1504 crtc_count++;
1505
1506 list_for_each(lh, &dev->mode_config.connector_list)
1507 connector_count++;
1508
1509 list_for_each(lh, &dev->mode_config.encoder_list)
1510 encoder_count++;
1511 } else {
1512
09f308f7 1513 mode_group = &file_priv->master->minor->mode_group;
f453ba04
DA
1514 crtc_count = mode_group->num_crtcs;
1515 connector_count = mode_group->num_connectors;
1516 encoder_count = mode_group->num_encoders;
1517 }
1518
1519 card_res->max_height = dev->mode_config.max_height;
1520 card_res->min_height = dev->mode_config.min_height;
1521 card_res->max_width = dev->mode_config.max_width;
1522 card_res->min_width = dev->mode_config.min_width;
1523
f453ba04
DA
1524 /* CRTCs */
1525 if (card_res->count_crtcs >= crtc_count) {
1526 copied = 0;
1527 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
09f308f7 1528 if (!mode_group) {
f453ba04
DA
1529 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1530 head) {
9440106b 1531 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
f453ba04
DA
1532 if (put_user(crtc->base.id, crtc_id + copied)) {
1533 ret = -EFAULT;
1534 goto out;
1535 }
1536 copied++;
1537 }
1538 } else {
1539 for (i = 0; i < mode_group->num_crtcs; i++) {
1540 if (put_user(mode_group->id_list[i],
1541 crtc_id + copied)) {
1542 ret = -EFAULT;
1543 goto out;
1544 }
1545 copied++;
1546 }
1547 }
1548 }
1549 card_res->count_crtcs = crtc_count;
1550
1551 /* Encoders */
1552 if (card_res->count_encoders >= encoder_count) {
1553 copied = 0;
1554 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
09f308f7 1555 if (!mode_group) {
f453ba04
DA
1556 list_for_each_entry(encoder,
1557 &dev->mode_config.encoder_list,
1558 head) {
9440106b
JG
1559 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1560 drm_get_encoder_name(encoder));
f453ba04
DA
1561 if (put_user(encoder->base.id, encoder_id +
1562 copied)) {
1563 ret = -EFAULT;
1564 goto out;
1565 }
1566 copied++;
1567 }
1568 } else {
1569 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1570 if (put_user(mode_group->id_list[i],
1571 encoder_id + copied)) {
1572 ret = -EFAULT;
1573 goto out;
1574 }
1575 copied++;
1576 }
1577
1578 }
1579 }
1580 card_res->count_encoders = encoder_count;
1581
1582 /* Connectors */
1583 if (card_res->count_connectors >= connector_count) {
1584 copied = 0;
1585 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
09f308f7 1586 if (!mode_group) {
f453ba04
DA
1587 list_for_each_entry(connector,
1588 &dev->mode_config.connector_list,
1589 head) {
9440106b
JG
1590 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1591 connector->base.id,
1592 drm_get_connector_name(connector));
f453ba04
DA
1593 if (put_user(connector->base.id,
1594 connector_id + copied)) {
1595 ret = -EFAULT;
1596 goto out;
1597 }
1598 copied++;
1599 }
1600 } else {
1601 int start = mode_group->num_crtcs +
1602 mode_group->num_encoders;
1603 for (i = start; i < start + mode_group->num_connectors; i++) {
1604 if (put_user(mode_group->id_list[i],
1605 connector_id + copied)) {
1606 ret = -EFAULT;
1607 goto out;
1608 }
1609 copied++;
1610 }
1611 }
1612 }
1613 card_res->count_connectors = connector_count;
1614
9440106b 1615 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
f453ba04
DA
1616 card_res->count_connectors, card_res->count_encoders);
1617
1618out:
84849903 1619 drm_modeset_unlock_all(dev);
f453ba04
DA
1620 return ret;
1621}
1622
1623/**
1624 * drm_mode_getcrtc - get CRTC configuration
065a50ed
DV
1625 * @dev: drm device for the ioctl
1626 * @data: data pointer for the ioctl
1627 * @file_priv: drm file for the ioctl call
f453ba04 1628 *
f453ba04
DA
1629 * Construct a CRTC configuration structure to return to the user.
1630 *
1631 * Called by the user via ioctl.
1632 *
c8e32cc1 1633 * Returns:
f453ba04
DA
1634 * Zero on success, errno on failure.
1635 */
1636int drm_mode_getcrtc(struct drm_device *dev,
1637 void *data, struct drm_file *file_priv)
1638{
1639 struct drm_mode_crtc *crtc_resp = data;
1640 struct drm_crtc *crtc;
1641 struct drm_mode_object *obj;
1642 int ret = 0;
1643
fb3b06c8
DA
1644 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1645 return -EINVAL;
1646
84849903 1647 drm_modeset_lock_all(dev);
f453ba04
DA
1648
1649 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1650 DRM_MODE_OBJECT_CRTC);
1651 if (!obj) {
f27657f2 1652 ret = -ENOENT;
f453ba04
DA
1653 goto out;
1654 }
1655 crtc = obj_to_crtc(obj);
1656
1657 crtc_resp->x = crtc->x;
1658 crtc_resp->y = crtc->y;
1659 crtc_resp->gamma_size = crtc->gamma_size;
1660 if (crtc->fb)
1661 crtc_resp->fb_id = crtc->fb->base.id;
1662 else
1663 crtc_resp->fb_id = 0;
1664
1665 if (crtc->enabled) {
1666
1667 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1668 crtc_resp->mode_valid = 1;
1669
1670 } else {
1671 crtc_resp->mode_valid = 0;
1672 }
1673
1674out:
84849903 1675 drm_modeset_unlock_all(dev);
f453ba04
DA
1676 return ret;
1677}
1678
61d8e328
DL
1679static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1680 const struct drm_file *file_priv)
1681{
1682 /*
1683 * If user-space hasn't configured the driver to expose the stereo 3D
1684 * modes, don't expose them.
1685 */
1686 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1687 return false;
1688
1689 return true;
1690}
1691
f453ba04
DA
1692/**
1693 * drm_mode_getconnector - get connector configuration
065a50ed
DV
1694 * @dev: drm device for the ioctl
1695 * @data: data pointer for the ioctl
1696 * @file_priv: drm file for the ioctl call
f453ba04 1697 *
f453ba04
DA
1698 * Construct a connector configuration structure to return to the user.
1699 *
1700 * Called by the user via ioctl.
1701 *
c8e32cc1 1702 * Returns:
f453ba04
DA
1703 * Zero on success, errno on failure.
1704 */
1705int drm_mode_getconnector(struct drm_device *dev, void *data,
1706 struct drm_file *file_priv)
1707{
1708 struct drm_mode_get_connector *out_resp = data;
1709 struct drm_mode_object *obj;
1710 struct drm_connector *connector;
1711 struct drm_display_mode *mode;
1712 int mode_count = 0;
1713 int props_count = 0;
1714 int encoders_count = 0;
1715 int ret = 0;
1716 int copied = 0;
1717 int i;
1718 struct drm_mode_modeinfo u_mode;
1719 struct drm_mode_modeinfo __user *mode_ptr;
1720 uint32_t __user *prop_ptr;
1721 uint64_t __user *prop_values;
1722 uint32_t __user *encoder_ptr;
1723
fb3b06c8
DA
1724 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1725 return -EINVAL;
1726
f453ba04
DA
1727 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1728
9440106b 1729 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
f453ba04 1730
7b24056b 1731 mutex_lock(&dev->mode_config.mutex);
f453ba04
DA
1732
1733 obj = drm_mode_object_find(dev, out_resp->connector_id,
1734 DRM_MODE_OBJECT_CONNECTOR);
1735 if (!obj) {
f27657f2 1736 ret = -ENOENT;
f453ba04
DA
1737 goto out;
1738 }
1739 connector = obj_to_connector(obj);
1740
7f88a9be 1741 props_count = connector->properties.count;
f453ba04
DA
1742
1743 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1744 if (connector->encoder_ids[i] != 0) {
1745 encoders_count++;
1746 }
1747 }
1748
1749 if (out_resp->count_modes == 0) {
1750 connector->funcs->fill_modes(connector,
1751 dev->mode_config.max_width,
1752 dev->mode_config.max_height);
1753 }
1754
1755 /* delayed so we get modes regardless of pre-fill_modes state */
1756 list_for_each_entry(mode, &connector->modes, head)
61d8e328
DL
1757 if (drm_mode_expose_to_userspace(mode, file_priv))
1758 mode_count++;
f453ba04
DA
1759
1760 out_resp->connector_id = connector->base.id;
1761 out_resp->connector_type = connector->connector_type;
1762 out_resp->connector_type_id = connector->connector_type_id;
1763 out_resp->mm_width = connector->display_info.width_mm;
1764 out_resp->mm_height = connector->display_info.height_mm;
1765 out_resp->subpixel = connector->display_info.subpixel_order;
1766 out_resp->connection = connector->status;
1767 if (connector->encoder)
1768 out_resp->encoder_id = connector->encoder->base.id;
1769 else
1770 out_resp->encoder_id = 0;
1771
1772 /*
1773 * This ioctl is called twice, once to determine how much space is
1774 * needed, and the 2nd time to fill it.
1775 */
1776 if ((out_resp->count_modes >= mode_count) && mode_count) {
1777 copied = 0;
81f6c7f8 1778 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
f453ba04 1779 list_for_each_entry(mode, &connector->modes, head) {
61d8e328
DL
1780 if (!drm_mode_expose_to_userspace(mode, file_priv))
1781 continue;
1782
f453ba04
DA
1783 drm_crtc_convert_to_umode(&u_mode, mode);
1784 if (copy_to_user(mode_ptr + copied,
1785 &u_mode, sizeof(u_mode))) {
1786 ret = -EFAULT;
1787 goto out;
1788 }
1789 copied++;
1790 }
1791 }
1792 out_resp->count_modes = mode_count;
1793
1794 if ((out_resp->count_props >= props_count) && props_count) {
1795 copied = 0;
81f6c7f8
VS
1796 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1797 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
7f88a9be
PZ
1798 for (i = 0; i < connector->properties.count; i++) {
1799 if (put_user(connector->properties.ids[i],
1800 prop_ptr + copied)) {
1801 ret = -EFAULT;
1802 goto out;
1803 }
f453ba04 1804
7f88a9be
PZ
1805 if (put_user(connector->properties.values[i],
1806 prop_values + copied)) {
1807 ret = -EFAULT;
1808 goto out;
f453ba04 1809 }
7f88a9be 1810 copied++;
f453ba04
DA
1811 }
1812 }
1813 out_resp->count_props = props_count;
1814
1815 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1816 copied = 0;
81f6c7f8 1817 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
f453ba04
DA
1818 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1819 if (connector->encoder_ids[i] != 0) {
1820 if (put_user(connector->encoder_ids[i],
1821 encoder_ptr + copied)) {
1822 ret = -EFAULT;
1823 goto out;
1824 }
1825 copied++;
1826 }
1827 }
1828 }
1829 out_resp->count_encoders = encoders_count;
1830
1831out:
7b24056b
DV
1832 mutex_unlock(&dev->mode_config.mutex);
1833
f453ba04
DA
1834 return ret;
1835}
1836
c8e32cc1
DV
1837/**
1838 * drm_mode_getencoder - get encoder configuration
1839 * @dev: drm device for the ioctl
1840 * @data: data pointer for the ioctl
1841 * @file_priv: drm file for the ioctl call
1842 *
1843 * Construct a encoder configuration structure to return to the user.
1844 *
1845 * Called by the user via ioctl.
1846 *
1847 * Returns:
1848 * Zero on success, errno on failure.
1849 */
f453ba04
DA
1850int drm_mode_getencoder(struct drm_device *dev, void *data,
1851 struct drm_file *file_priv)
1852{
1853 struct drm_mode_get_encoder *enc_resp = data;
1854 struct drm_mode_object *obj;
1855 struct drm_encoder *encoder;
1856 int ret = 0;
1857
fb3b06c8
DA
1858 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1859 return -EINVAL;
1860
84849903 1861 drm_modeset_lock_all(dev);
f453ba04
DA
1862 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1863 DRM_MODE_OBJECT_ENCODER);
1864 if (!obj) {
f27657f2 1865 ret = -ENOENT;
f453ba04
DA
1866 goto out;
1867 }
1868 encoder = obj_to_encoder(obj);
1869
1870 if (encoder->crtc)
1871 enc_resp->crtc_id = encoder->crtc->base.id;
1872 else
1873 enc_resp->crtc_id = 0;
1874 enc_resp->encoder_type = encoder->encoder_type;
1875 enc_resp->encoder_id = encoder->base.id;
1876 enc_resp->possible_crtcs = encoder->possible_crtcs;
1877 enc_resp->possible_clones = encoder->possible_clones;
1878
1879out:
84849903 1880 drm_modeset_unlock_all(dev);
f453ba04
DA
1881 return ret;
1882}
1883
8cf5c917 1884/**
c8e32cc1 1885 * drm_mode_getplane_res - enumerate all plane resources
8cf5c917
JB
1886 * @dev: DRM device
1887 * @data: ioctl data
1888 * @file_priv: DRM file info
1889 *
c8e32cc1
DV
1890 * Construct a list of plane ids to return to the user.
1891 *
1892 * Called by the user via ioctl.
1893 *
1894 * Returns:
1895 * Zero on success, errno on failure.
8cf5c917
JB
1896 */
1897int drm_mode_getplane_res(struct drm_device *dev, void *data,
c8e32cc1 1898 struct drm_file *file_priv)
8cf5c917
JB
1899{
1900 struct drm_mode_get_plane_res *plane_resp = data;
1901 struct drm_mode_config *config;
1902 struct drm_plane *plane;
1903 uint32_t __user *plane_ptr;
1904 int copied = 0, ret = 0;
1905
1906 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1907 return -EINVAL;
1908
84849903 1909 drm_modeset_lock_all(dev);
8cf5c917
JB
1910 config = &dev->mode_config;
1911
1912 /*
1913 * This ioctl is called twice, once to determine how much space is
1914 * needed, and the 2nd time to fill it.
1915 */
e27dde3e
MR
1916 if (config->num_overlay_plane &&
1917 (plane_resp->count_planes >= config->num_overlay_plane)) {
81f6c7f8 1918 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
8cf5c917
JB
1919
1920 list_for_each_entry(plane, &config->plane_list, head) {
e27dde3e
MR
1921 /* Only advertise overlays to userspace for now. */
1922 if (plane->type != DRM_PLANE_TYPE_OVERLAY)
1923 continue;
1924
8cf5c917
JB
1925 if (put_user(plane->base.id, plane_ptr + copied)) {
1926 ret = -EFAULT;
1927 goto out;
1928 }
1929 copied++;
1930 }
1931 }
e27dde3e 1932 plane_resp->count_planes = config->num_overlay_plane;
8cf5c917
JB
1933
1934out:
84849903 1935 drm_modeset_unlock_all(dev);
8cf5c917
JB
1936 return ret;
1937}
1938
1939/**
c8e32cc1 1940 * drm_mode_getplane - get plane configuration
8cf5c917
JB
1941 * @dev: DRM device
1942 * @data: ioctl data
1943 * @file_priv: DRM file info
1944 *
c8e32cc1
DV
1945 * Construct a plane configuration structure to return to the user.
1946 *
1947 * Called by the user via ioctl.
1948 *
1949 * Returns:
1950 * Zero on success, errno on failure.
8cf5c917
JB
1951 */
1952int drm_mode_getplane(struct drm_device *dev, void *data,
c8e32cc1 1953 struct drm_file *file_priv)
8cf5c917
JB
1954{
1955 struct drm_mode_get_plane *plane_resp = data;
1956 struct drm_mode_object *obj;
1957 struct drm_plane *plane;
1958 uint32_t __user *format_ptr;
1959 int ret = 0;
1960
1961 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1962 return -EINVAL;
1963
84849903 1964 drm_modeset_lock_all(dev);
8cf5c917
JB
1965 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1966 DRM_MODE_OBJECT_PLANE);
1967 if (!obj) {
1968 ret = -ENOENT;
1969 goto out;
1970 }
1971 plane = obj_to_plane(obj);
1972
1973 if (plane->crtc)
1974 plane_resp->crtc_id = plane->crtc->base.id;
1975 else
1976 plane_resp->crtc_id = 0;
1977
1978 if (plane->fb)
1979 plane_resp->fb_id = plane->fb->base.id;
1980 else
1981 plane_resp->fb_id = 0;
1982
1983 plane_resp->plane_id = plane->base.id;
1984 plane_resp->possible_crtcs = plane->possible_crtcs;
778ad903 1985 plane_resp->gamma_size = 0;
8cf5c917
JB
1986
1987 /*
1988 * This ioctl is called twice, once to determine how much space is
1989 * needed, and the 2nd time to fill it.
1990 */
1991 if (plane->format_count &&
1992 (plane_resp->count_format_types >= plane->format_count)) {
81f6c7f8 1993 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
8cf5c917
JB
1994 if (copy_to_user(format_ptr,
1995 plane->format_types,
1996 sizeof(uint32_t) * plane->format_count)) {
1997 ret = -EFAULT;
1998 goto out;
1999 }
2000 }
2001 plane_resp->count_format_types = plane->format_count;
2002
2003out:
84849903 2004 drm_modeset_unlock_all(dev);
8cf5c917
JB
2005 return ret;
2006}
2007
2008/**
c8e32cc1 2009 * drm_mode_setplane - configure a plane's configuration
8cf5c917
JB
2010 * @dev: DRM device
2011 * @data: ioctl data*
065a50ed 2012 * @file_priv: DRM file info
8cf5c917 2013 *
c8e32cc1 2014 * Set plane configuration, including placement, fb, scaling, and other factors.
8cf5c917 2015 * Or pass a NULL fb to disable.
c8e32cc1
DV
2016 *
2017 * Returns:
2018 * Zero on success, errno on failure.
8cf5c917
JB
2019 */
2020int drm_mode_setplane(struct drm_device *dev, void *data,
c8e32cc1 2021 struct drm_file *file_priv)
8cf5c917
JB
2022{
2023 struct drm_mode_set_plane *plane_req = data;
2024 struct drm_mode_object *obj;
2025 struct drm_plane *plane;
2026 struct drm_crtc *crtc;
6c2a7532 2027 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
8cf5c917 2028 int ret = 0;
42ef8789 2029 unsigned int fb_width, fb_height;
62443be6 2030 int i;
8cf5c917
JB
2031
2032 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2033 return -EINVAL;
2034
8cf5c917
JB
2035 /*
2036 * First, find the plane, crtc, and fb objects. If not available,
2037 * we don't bother to call the driver.
2038 */
2039 obj = drm_mode_object_find(dev, plane_req->plane_id,
2040 DRM_MODE_OBJECT_PLANE);
2041 if (!obj) {
2042 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2043 plane_req->plane_id);
6c2a7532 2044 return -ENOENT;
8cf5c917
JB
2045 }
2046 plane = obj_to_plane(obj);
2047
2048 /* No fb means shut it down */
2049 if (!plane_req->fb_id) {
6c2a7532
DV
2050 drm_modeset_lock_all(dev);
2051 old_fb = plane->fb;
8cf5c917 2052 plane->funcs->disable_plane(plane);
e5e3b44c
VS
2053 plane->crtc = NULL;
2054 plane->fb = NULL;
6c2a7532 2055 drm_modeset_unlock_all(dev);
8cf5c917
JB
2056 goto out;
2057 }
2058
2059 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2060 DRM_MODE_OBJECT_CRTC);
2061 if (!obj) {
2062 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2063 plane_req->crtc_id);
2064 ret = -ENOENT;
2065 goto out;
2066 }
2067 crtc = obj_to_crtc(obj);
2068
786b99ed
DV
2069 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2070 if (!fb) {
8cf5c917
JB
2071 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2072 plane_req->fb_id);
2073 ret = -ENOENT;
2074 goto out;
2075 }
8cf5c917 2076
62443be6
VS
2077 /* Check whether this plane supports the fb pixel format. */
2078 for (i = 0; i < plane->format_count; i++)
2079 if (fb->pixel_format == plane->format_types[i])
2080 break;
2081 if (i == plane->format_count) {
6ba6d03e
VS
2082 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2083 drm_get_format_name(fb->pixel_format));
62443be6
VS
2084 ret = -EINVAL;
2085 goto out;
2086 }
2087
42ef8789
VS
2088 fb_width = fb->width << 16;
2089 fb_height = fb->height << 16;
2090
2091 /* Make sure source coordinates are inside the fb. */
2092 if (plane_req->src_w > fb_width ||
2093 plane_req->src_x > fb_width - plane_req->src_w ||
2094 plane_req->src_h > fb_height ||
2095 plane_req->src_y > fb_height - plane_req->src_h) {
2096 DRM_DEBUG_KMS("Invalid source coordinates "
2097 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2098 plane_req->src_w >> 16,
2099 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2100 plane_req->src_h >> 16,
2101 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2102 plane_req->src_x >> 16,
2103 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2104 plane_req->src_y >> 16,
2105 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2106 ret = -ENOSPC;
2107 goto out;
2108 }
2109
687a0400
VS
2110 /* Give drivers some help against integer overflows */
2111 if (plane_req->crtc_w > INT_MAX ||
2112 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2113 plane_req->crtc_h > INT_MAX ||
2114 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2115 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2116 plane_req->crtc_w, plane_req->crtc_h,
2117 plane_req->crtc_x, plane_req->crtc_y);
2118 ret = -ERANGE;
2119 goto out;
2120 }
2121
6c2a7532 2122 drm_modeset_lock_all(dev);
8cf5c917
JB
2123 ret = plane->funcs->update_plane(plane, crtc, fb,
2124 plane_req->crtc_x, plane_req->crtc_y,
2125 plane_req->crtc_w, plane_req->crtc_h,
2126 plane_req->src_x, plane_req->src_y,
2127 plane_req->src_w, plane_req->src_h);
2128 if (!ret) {
6c2a7532 2129 old_fb = plane->fb;
8cf5c917
JB
2130 plane->crtc = crtc;
2131 plane->fb = fb;
35f8badc 2132 fb = NULL;
8cf5c917 2133 }
6c2a7532 2134 drm_modeset_unlock_all(dev);
8cf5c917
JB
2135
2136out:
6c2a7532
DV
2137 if (fb)
2138 drm_framebuffer_unreference(fb);
2139 if (old_fb)
2140 drm_framebuffer_unreference(old_fb);
8cf5c917
JB
2141
2142 return ret;
2143}
2144
2d13b679
DV
2145/**
2146 * drm_mode_set_config_internal - helper to call ->set_config
2147 * @set: modeset config to set
2148 *
2149 * This is a little helper to wrap internal calls to the ->set_config driver
2150 * interface. The only thing it adds is correct refcounting dance.
c8e32cc1
DV
2151 *
2152 * Returns:
2153 * Zero on success, errno on failure.
2d13b679
DV
2154 */
2155int drm_mode_set_config_internal(struct drm_mode_set *set)
2156{
2157 struct drm_crtc *crtc = set->crtc;
5cef29aa
DV
2158 struct drm_framebuffer *fb;
2159 struct drm_crtc *tmp;
b0d12325
DV
2160 int ret;
2161
5cef29aa
DV
2162 /*
2163 * NOTE: ->set_config can also disable other crtcs (if we steal all
2164 * connectors from it), hence we need to refcount the fbs across all
2165 * crtcs. Atomic modeset will have saner semantics ...
2166 */
2167 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2168 tmp->old_fb = tmp->fb;
2169
b0d12325 2170 fb = set->fb;
2d13b679 2171
b0d12325
DV
2172 ret = crtc->funcs->set_config(set);
2173 if (ret == 0) {
cc85e121
DV
2174 /* crtc->fb must be updated by ->set_config, enforces this. */
2175 WARN_ON(fb != crtc->fb);
5cef29aa 2176 }
cc85e121 2177
5cef29aa
DV
2178 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2179 if (tmp->fb)
2180 drm_framebuffer_reference(tmp->fb);
2181 if (tmp->old_fb)
2182 drm_framebuffer_unreference(tmp->old_fb);
b0d12325
DV
2183 }
2184
2185 return ret;
2d13b679
DV
2186}
2187EXPORT_SYMBOL(drm_mode_set_config_internal);
2188
c11e9283
DL
2189/*
2190 * Checks that the framebuffer is big enough for the CRTC viewport
2191 * (x, y, hdisplay, vdisplay)
2192 */
2193static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2194 int x, int y,
2195 const struct drm_display_mode *mode,
2196 const struct drm_framebuffer *fb)
2197
2198{
2199 int hdisplay, vdisplay;
2200
2201 hdisplay = mode->hdisplay;
2202 vdisplay = mode->vdisplay;
2203
a0c1bbb0
DL
2204 if (drm_mode_is_stereo(mode)) {
2205 struct drm_display_mode adjusted = *mode;
2206
2207 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2208 hdisplay = adjusted.crtc_hdisplay;
2209 vdisplay = adjusted.crtc_vdisplay;
2210 }
2211
c11e9283
DL
2212 if (crtc->invert_dimensions)
2213 swap(hdisplay, vdisplay);
2214
2215 if (hdisplay > fb->width ||
2216 vdisplay > fb->height ||
2217 x > fb->width - hdisplay ||
2218 y > fb->height - vdisplay) {
2219 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2220 fb->width, fb->height, hdisplay, vdisplay, x, y,
2221 crtc->invert_dimensions ? " (inverted)" : "");
2222 return -ENOSPC;
2223 }
2224
2225 return 0;
2226}
2227
f453ba04
DA
2228/**
2229 * drm_mode_setcrtc - set CRTC configuration
065a50ed
DV
2230 * @dev: drm device for the ioctl
2231 * @data: data pointer for the ioctl
2232 * @file_priv: drm file for the ioctl call
f453ba04 2233 *
f453ba04
DA
2234 * Build a new CRTC configuration based on user request.
2235 *
2236 * Called by the user via ioctl.
2237 *
c8e32cc1 2238 * Returns:
f453ba04
DA
2239 * Zero on success, errno on failure.
2240 */
2241int drm_mode_setcrtc(struct drm_device *dev, void *data,
2242 struct drm_file *file_priv)
2243{
2244 struct drm_mode_config *config = &dev->mode_config;
2245 struct drm_mode_crtc *crtc_req = data;
2246 struct drm_mode_object *obj;
6653cc8d 2247 struct drm_crtc *crtc;
f453ba04
DA
2248 struct drm_connector **connector_set = NULL, *connector;
2249 struct drm_framebuffer *fb = NULL;
2250 struct drm_display_mode *mode = NULL;
2251 struct drm_mode_set set;
2252 uint32_t __user *set_connectors_ptr;
4a1b0714 2253 int ret;
f453ba04
DA
2254 int i;
2255
fb3b06c8
DA
2256 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2257 return -EINVAL;
2258
1d97e915
VS
2259 /* For some reason crtc x/y offsets are signed internally. */
2260 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2261 return -ERANGE;
2262
84849903 2263 drm_modeset_lock_all(dev);
f453ba04
DA
2264 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2265 DRM_MODE_OBJECT_CRTC);
2266 if (!obj) {
58367ed6 2267 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
f27657f2 2268 ret = -ENOENT;
f453ba04
DA
2269 goto out;
2270 }
2271 crtc = obj_to_crtc(obj);
9440106b 2272 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
f453ba04
DA
2273
2274 if (crtc_req->mode_valid) {
2275 /* If we have a mode we need a framebuffer. */
2276 /* If we pass -1, set the mode with the currently bound fb */
2277 if (crtc_req->fb_id == -1) {
6653cc8d
VS
2278 if (!crtc->fb) {
2279 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2280 ret = -EINVAL;
2281 goto out;
f453ba04 2282 }
6653cc8d 2283 fb = crtc->fb;
b0d12325
DV
2284 /* Make refcounting symmetric with the lookup path. */
2285 drm_framebuffer_reference(fb);
f453ba04 2286 } else {
786b99ed
DV
2287 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2288 if (!fb) {
58367ed6
ZY
2289 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2290 crtc_req->fb_id);
37c4e705 2291 ret = -ENOENT;
f453ba04
DA
2292 goto out;
2293 }
f453ba04
DA
2294 }
2295
2296 mode = drm_mode_create(dev);
ee34ab5b
VS
2297 if (!mode) {
2298 ret = -ENOMEM;
2299 goto out;
2300 }
2301
90367bf6
VS
2302 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2303 if (ret) {
2304 DRM_DEBUG_KMS("Invalid mode\n");
2305 goto out;
2306 }
2307
f453ba04 2308 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
5f61bb42 2309
c11e9283
DL
2310 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2311 mode, fb);
2312 if (ret)
5f61bb42 2313 goto out;
c11e9283 2314
f453ba04
DA
2315 }
2316
2317 if (crtc_req->count_connectors == 0 && mode) {
58367ed6 2318 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
f453ba04
DA
2319 ret = -EINVAL;
2320 goto out;
2321 }
2322
7781de74 2323 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
58367ed6 2324 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
f453ba04
DA
2325 crtc_req->count_connectors);
2326 ret = -EINVAL;
2327 goto out;
2328 }
2329
2330 if (crtc_req->count_connectors > 0) {
2331 u32 out_id;
2332
2333 /* Avoid unbounded kernel memory allocation */
2334 if (crtc_req->count_connectors > config->num_connector) {
2335 ret = -EINVAL;
2336 goto out;
2337 }
2338
2339 connector_set = kmalloc(crtc_req->count_connectors *
2340 sizeof(struct drm_connector *),
2341 GFP_KERNEL);
2342 if (!connector_set) {
2343 ret = -ENOMEM;
2344 goto out;
2345 }
2346
2347 for (i = 0; i < crtc_req->count_connectors; i++) {
81f6c7f8 2348 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
f453ba04
DA
2349 if (get_user(out_id, &set_connectors_ptr[i])) {
2350 ret = -EFAULT;
2351 goto out;
2352 }
2353
2354 obj = drm_mode_object_find(dev, out_id,
2355 DRM_MODE_OBJECT_CONNECTOR);
2356 if (!obj) {
58367ed6
ZY
2357 DRM_DEBUG_KMS("Connector id %d unknown\n",
2358 out_id);
f27657f2 2359 ret = -ENOENT;
f453ba04
DA
2360 goto out;
2361 }
2362 connector = obj_to_connector(obj);
9440106b
JG
2363 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2364 connector->base.id,
2365 drm_get_connector_name(connector));
f453ba04
DA
2366
2367 connector_set[i] = connector;
2368 }
2369 }
2370
2371 set.crtc = crtc;
2372 set.x = crtc_req->x;
2373 set.y = crtc_req->y;
2374 set.mode = mode;
2375 set.connectors = connector_set;
2376 set.num_connectors = crtc_req->count_connectors;
5ef5f72f 2377 set.fb = fb;
2d13b679 2378 ret = drm_mode_set_config_internal(&set);
f453ba04
DA
2379
2380out:
b0d12325
DV
2381 if (fb)
2382 drm_framebuffer_unreference(fb);
2383
f453ba04 2384 kfree(connector_set);
ee34ab5b 2385 drm_mode_destroy(dev, mode);
84849903 2386 drm_modeset_unlock_all(dev);
f453ba04
DA
2387 return ret;
2388}
2389
4c813d4d
DA
2390static int drm_mode_cursor_common(struct drm_device *dev,
2391 struct drm_mode_cursor2 *req,
2392 struct drm_file *file_priv)
f453ba04 2393{
f453ba04
DA
2394 struct drm_mode_object *obj;
2395 struct drm_crtc *crtc;
2396 int ret = 0;
2397
fb3b06c8
DA
2398 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2399 return -EINVAL;
2400
7c4eaca4 2401 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
f453ba04 2402 return -EINVAL;
f453ba04 2403
e0c8463a 2404 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
f453ba04 2405 if (!obj) {
58367ed6 2406 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
f27657f2 2407 return -ENOENT;
f453ba04
DA
2408 }
2409 crtc = obj_to_crtc(obj);
2410
dac35663 2411 mutex_lock(&crtc->mutex);
f453ba04 2412 if (req->flags & DRM_MODE_CURSOR_BO) {
4c813d4d 2413 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
f453ba04
DA
2414 ret = -ENXIO;
2415 goto out;
2416 }
2417 /* Turns off the cursor if handle is 0 */
4c813d4d
DA
2418 if (crtc->funcs->cursor_set2)
2419 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2420 req->width, req->height, req->hot_x, req->hot_y);
2421 else
2422 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2423 req->width, req->height);
f453ba04
DA
2424 }
2425
2426 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2427 if (crtc->funcs->cursor_move) {
2428 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2429 } else {
f453ba04
DA
2430 ret = -EFAULT;
2431 goto out;
2432 }
2433 }
2434out:
dac35663
DV
2435 mutex_unlock(&crtc->mutex);
2436
f453ba04 2437 return ret;
4c813d4d
DA
2438
2439}
c8e32cc1
DV
2440
2441
2442/**
2443 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2444 * @dev: drm device for the ioctl
2445 * @data: data pointer for the ioctl
2446 * @file_priv: drm file for the ioctl call
2447 *
2448 * Set the cursor configuration based on user request.
2449 *
2450 * Called by the user via ioctl.
2451 *
2452 * Returns:
2453 * Zero on success, errno on failure.
2454 */
4c813d4d 2455int drm_mode_cursor_ioctl(struct drm_device *dev,
c8e32cc1 2456 void *data, struct drm_file *file_priv)
4c813d4d
DA
2457{
2458 struct drm_mode_cursor *req = data;
2459 struct drm_mode_cursor2 new_req;
2460
2461 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2462 new_req.hot_x = new_req.hot_y = 0;
2463
2464 return drm_mode_cursor_common(dev, &new_req, file_priv);
2465}
2466
c8e32cc1
DV
2467/**
2468 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2469 * @dev: drm device for the ioctl
2470 * @data: data pointer for the ioctl
2471 * @file_priv: drm file for the ioctl call
2472 *
2473 * Set the cursor configuration based on user request. This implements the 2nd
2474 * version of the cursor ioctl, which allows userspace to additionally specify
2475 * the hotspot of the pointer.
2476 *
2477 * Called by the user via ioctl.
2478 *
2479 * Returns:
2480 * Zero on success, errno on failure.
2481 */
4c813d4d
DA
2482int drm_mode_cursor2_ioctl(struct drm_device *dev,
2483 void *data, struct drm_file *file_priv)
2484{
2485 struct drm_mode_cursor2 *req = data;
2486 return drm_mode_cursor_common(dev, req, file_priv);
f453ba04
DA
2487}
2488
c8e32cc1
DV
2489/**
2490 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2491 * @bpp: bits per pixels
2492 * @depth: bit depth per pixel
2493 *
2494 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2495 * Useful in fbdev emulation code, since that deals in those values.
2496 */
308e5bcb
JB
2497uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2498{
2499 uint32_t fmt;
2500
2501 switch (bpp) {
2502 case 8:
d84f031b 2503 fmt = DRM_FORMAT_C8;
308e5bcb
JB
2504 break;
2505 case 16:
2506 if (depth == 15)
04b3924d 2507 fmt = DRM_FORMAT_XRGB1555;
308e5bcb 2508 else
04b3924d 2509 fmt = DRM_FORMAT_RGB565;
308e5bcb
JB
2510 break;
2511 case 24:
04b3924d 2512 fmt = DRM_FORMAT_RGB888;
308e5bcb
JB
2513 break;
2514 case 32:
2515 if (depth == 24)
04b3924d 2516 fmt = DRM_FORMAT_XRGB8888;
308e5bcb 2517 else if (depth == 30)
04b3924d 2518 fmt = DRM_FORMAT_XRGB2101010;
308e5bcb 2519 else
04b3924d 2520 fmt = DRM_FORMAT_ARGB8888;
308e5bcb
JB
2521 break;
2522 default:
04b3924d
VS
2523 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2524 fmt = DRM_FORMAT_XRGB8888;
308e5bcb
JB
2525 break;
2526 }
2527
2528 return fmt;
2529}
2530EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2531
f453ba04
DA
2532/**
2533 * drm_mode_addfb - add an FB to the graphics configuration
065a50ed
DV
2534 * @dev: drm device for the ioctl
2535 * @data: data pointer for the ioctl
2536 * @file_priv: drm file for the ioctl call
f453ba04 2537 *
c8e32cc1
DV
2538 * Add a new FB to the specified CRTC, given a user request. This is the
2539 * original addfb ioclt which only supported RGB formats.
f453ba04
DA
2540 *
2541 * Called by the user via ioctl.
2542 *
c8e32cc1 2543 * Returns:
f453ba04
DA
2544 * Zero on success, errno on failure.
2545 */
2546int drm_mode_addfb(struct drm_device *dev,
2547 void *data, struct drm_file *file_priv)
2548{
308e5bcb
JB
2549 struct drm_mode_fb_cmd *or = data;
2550 struct drm_mode_fb_cmd2 r = {};
2551 struct drm_mode_config *config = &dev->mode_config;
2552 struct drm_framebuffer *fb;
2553 int ret = 0;
2554
2555 /* Use new struct with format internally */
2556 r.fb_id = or->fb_id;
2557 r.width = or->width;
2558 r.height = or->height;
2559 r.pitches[0] = or->pitch;
2560 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2561 r.handles[0] = or->handle;
2562
2563 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2564 return -EINVAL;
2565
acb4b992 2566 if ((config->min_width > r.width) || (r.width > config->max_width))
308e5bcb 2567 return -EINVAL;
acb4b992
JB
2568
2569 if ((config->min_height > r.height) || (r.height > config->max_height))
308e5bcb 2570 return -EINVAL;
308e5bcb 2571
308e5bcb
JB
2572 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2573 if (IS_ERR(fb)) {
1aa1b11c 2574 DRM_DEBUG_KMS("could not create framebuffer\n");
4b096ac1 2575 return PTR_ERR(fb);
308e5bcb
JB
2576 }
2577
4b096ac1 2578 mutex_lock(&file_priv->fbs_lock);
308e5bcb
JB
2579 or->fb_id = fb->base.id;
2580 list_add(&fb->filp_head, &file_priv->fbs);
2581 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
4b096ac1 2582 mutex_unlock(&file_priv->fbs_lock);
4b096ac1 2583
308e5bcb
JB
2584 return ret;
2585}
2586
cff91b62 2587static int format_check(const struct drm_mode_fb_cmd2 *r)
935b5977
VS
2588{
2589 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2590
2591 switch (format) {
2592 case DRM_FORMAT_C8:
2593 case DRM_FORMAT_RGB332:
2594 case DRM_FORMAT_BGR233:
2595 case DRM_FORMAT_XRGB4444:
2596 case DRM_FORMAT_XBGR4444:
2597 case DRM_FORMAT_RGBX4444:
2598 case DRM_FORMAT_BGRX4444:
2599 case DRM_FORMAT_ARGB4444:
2600 case DRM_FORMAT_ABGR4444:
2601 case DRM_FORMAT_RGBA4444:
2602 case DRM_FORMAT_BGRA4444:
2603 case DRM_FORMAT_XRGB1555:
2604 case DRM_FORMAT_XBGR1555:
2605 case DRM_FORMAT_RGBX5551:
2606 case DRM_FORMAT_BGRX5551:
2607 case DRM_FORMAT_ARGB1555:
2608 case DRM_FORMAT_ABGR1555:
2609 case DRM_FORMAT_RGBA5551:
2610 case DRM_FORMAT_BGRA5551:
2611 case DRM_FORMAT_RGB565:
2612 case DRM_FORMAT_BGR565:
2613 case DRM_FORMAT_RGB888:
2614 case DRM_FORMAT_BGR888:
2615 case DRM_FORMAT_XRGB8888:
2616 case DRM_FORMAT_XBGR8888:
2617 case DRM_FORMAT_RGBX8888:
2618 case DRM_FORMAT_BGRX8888:
2619 case DRM_FORMAT_ARGB8888:
2620 case DRM_FORMAT_ABGR8888:
2621 case DRM_FORMAT_RGBA8888:
2622 case DRM_FORMAT_BGRA8888:
2623 case DRM_FORMAT_XRGB2101010:
2624 case DRM_FORMAT_XBGR2101010:
2625 case DRM_FORMAT_RGBX1010102:
2626 case DRM_FORMAT_BGRX1010102:
2627 case DRM_FORMAT_ARGB2101010:
2628 case DRM_FORMAT_ABGR2101010:
2629 case DRM_FORMAT_RGBA1010102:
2630 case DRM_FORMAT_BGRA1010102:
2631 case DRM_FORMAT_YUYV:
2632 case DRM_FORMAT_YVYU:
2633 case DRM_FORMAT_UYVY:
2634 case DRM_FORMAT_VYUY:
2635 case DRM_FORMAT_AYUV:
2636 case DRM_FORMAT_NV12:
2637 case DRM_FORMAT_NV21:
2638 case DRM_FORMAT_NV16:
2639 case DRM_FORMAT_NV61:
ba623f6a
LP
2640 case DRM_FORMAT_NV24:
2641 case DRM_FORMAT_NV42:
935b5977
VS
2642 case DRM_FORMAT_YUV410:
2643 case DRM_FORMAT_YVU410:
2644 case DRM_FORMAT_YUV411:
2645 case DRM_FORMAT_YVU411:
2646 case DRM_FORMAT_YUV420:
2647 case DRM_FORMAT_YVU420:
2648 case DRM_FORMAT_YUV422:
2649 case DRM_FORMAT_YVU422:
2650 case DRM_FORMAT_YUV444:
2651 case DRM_FORMAT_YVU444:
2652 return 0;
2653 default:
23c453a4
VS
2654 DRM_DEBUG_KMS("invalid pixel format %s\n",
2655 drm_get_format_name(r->pixel_format));
935b5977
VS
2656 return -EINVAL;
2657 }
2658}
2659
cff91b62 2660static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
d1b45d5f
VS
2661{
2662 int ret, hsub, vsub, num_planes, i;
2663
2664 ret = format_check(r);
2665 if (ret) {
6ba6d03e
VS
2666 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2667 drm_get_format_name(r->pixel_format));
d1b45d5f
VS
2668 return ret;
2669 }
2670
2671 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2672 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2673 num_planes = drm_format_num_planes(r->pixel_format);
2674
2675 if (r->width == 0 || r->width % hsub) {
1aa1b11c 2676 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
d1b45d5f
VS
2677 return -EINVAL;
2678 }
2679
2680 if (r->height == 0 || r->height % vsub) {
1aa1b11c 2681 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
d1b45d5f
VS
2682 return -EINVAL;
2683 }
2684
2685 for (i = 0; i < num_planes; i++) {
2686 unsigned int width = r->width / (i != 0 ? hsub : 1);
b180b5d1
VS
2687 unsigned int height = r->height / (i != 0 ? vsub : 1);
2688 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
d1b45d5f
VS
2689
2690 if (!r->handles[i]) {
1aa1b11c 2691 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
d1b45d5f
VS
2692 return -EINVAL;
2693 }
2694
b180b5d1
VS
2695 if ((uint64_t) width * cpp > UINT_MAX)
2696 return -ERANGE;
2697
2698 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2699 return -ERANGE;
2700
2701 if (r->pitches[i] < width * cpp) {
1aa1b11c 2702 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
d1b45d5f
VS
2703 return -EINVAL;
2704 }
2705 }
2706
2707 return 0;
2708}
2709
308e5bcb
JB
2710/**
2711 * drm_mode_addfb2 - add an FB to the graphics configuration
065a50ed
DV
2712 * @dev: drm device for the ioctl
2713 * @data: data pointer for the ioctl
2714 * @file_priv: drm file for the ioctl call
308e5bcb 2715 *
c8e32cc1
DV
2716 * Add a new FB to the specified CRTC, given a user request with format. This is
2717 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2718 * and uses fourcc codes as pixel format specifiers.
308e5bcb
JB
2719 *
2720 * Called by the user via ioctl.
2721 *
c8e32cc1 2722 * Returns:
308e5bcb
JB
2723 * Zero on success, errno on failure.
2724 */
2725int drm_mode_addfb2(struct drm_device *dev,
2726 void *data, struct drm_file *file_priv)
2727{
2728 struct drm_mode_fb_cmd2 *r = data;
f453ba04
DA
2729 struct drm_mode_config *config = &dev->mode_config;
2730 struct drm_framebuffer *fb;
4a1b0714 2731 int ret;
f453ba04 2732
fb3b06c8
DA
2733 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2734 return -EINVAL;
2735
e3cc3520
VS
2736 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2737 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2738 return -EINVAL;
2739 }
2740
f453ba04 2741 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1aa1b11c 2742 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
8cf5c917 2743 r->width, config->min_width, config->max_width);
f453ba04
DA
2744 return -EINVAL;
2745 }
2746 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1aa1b11c 2747 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
8cf5c917 2748 r->height, config->min_height, config->max_height);
f453ba04
DA
2749 return -EINVAL;
2750 }
2751
d1b45d5f
VS
2752 ret = framebuffer_check(r);
2753 if (ret)
935b5977 2754 return ret;
935b5977 2755
f453ba04 2756 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
cce13ff7 2757 if (IS_ERR(fb)) {
1aa1b11c 2758 DRM_DEBUG_KMS("could not create framebuffer\n");
4b096ac1 2759 return PTR_ERR(fb);
f453ba04
DA
2760 }
2761
4b096ac1 2762 mutex_lock(&file_priv->fbs_lock);
e0c8463a 2763 r->fb_id = fb->base.id;
f453ba04 2764 list_add(&fb->filp_head, &file_priv->fbs);
9440106b 2765 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
4b096ac1 2766 mutex_unlock(&file_priv->fbs_lock);
f453ba04 2767
4b096ac1 2768
f453ba04
DA
2769 return ret;
2770}
2771
2772/**
2773 * drm_mode_rmfb - remove an FB from the configuration
065a50ed
DV
2774 * @dev: drm device for the ioctl
2775 * @data: data pointer for the ioctl
2776 * @file_priv: drm file for the ioctl call
f453ba04 2777 *
f453ba04
DA
2778 * Remove the FB specified by the user.
2779 *
2780 * Called by the user via ioctl.
2781 *
c8e32cc1 2782 * Returns:
f453ba04
DA
2783 * Zero on success, errno on failure.
2784 */
2785int drm_mode_rmfb(struct drm_device *dev,
2786 void *data, struct drm_file *file_priv)
2787{
f453ba04
DA
2788 struct drm_framebuffer *fb = NULL;
2789 struct drm_framebuffer *fbl = NULL;
2790 uint32_t *id = data;
f453ba04
DA
2791 int found = 0;
2792
fb3b06c8
DA
2793 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2794 return -EINVAL;
2795
4b096ac1 2796 mutex_lock(&file_priv->fbs_lock);
2b677e8c
DV
2797 mutex_lock(&dev->mode_config.fb_lock);
2798 fb = __drm_framebuffer_lookup(dev, *id);
2799 if (!fb)
2800 goto fail_lookup;
2801
f453ba04
DA
2802 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2803 if (fb == fbl)
2804 found = 1;
2b677e8c
DV
2805 if (!found)
2806 goto fail_lookup;
2807
2808 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2809 __drm_framebuffer_unregister(dev, fb);
f453ba04 2810
4b096ac1 2811 list_del_init(&fb->filp_head);
2b677e8c 2812 mutex_unlock(&dev->mode_config.fb_lock);
4b096ac1 2813 mutex_unlock(&file_priv->fbs_lock);
f453ba04 2814
4b096ac1 2815 drm_framebuffer_remove(fb);
4b096ac1 2816
2b677e8c
DV
2817 return 0;
2818
2819fail_lookup:
2820 mutex_unlock(&dev->mode_config.fb_lock);
2821 mutex_unlock(&file_priv->fbs_lock);
2822
37c4e705 2823 return -ENOENT;
f453ba04
DA
2824}
2825
2826/**
2827 * drm_mode_getfb - get FB info
065a50ed
DV
2828 * @dev: drm device for the ioctl
2829 * @data: data pointer for the ioctl
2830 * @file_priv: drm file for the ioctl call
f453ba04 2831 *
f453ba04
DA
2832 * Lookup the FB given its ID and return info about it.
2833 *
2834 * Called by the user via ioctl.
2835 *
c8e32cc1 2836 * Returns:
f453ba04
DA
2837 * Zero on success, errno on failure.
2838 */
2839int drm_mode_getfb(struct drm_device *dev,
2840 void *data, struct drm_file *file_priv)
2841{
2842 struct drm_mode_fb_cmd *r = data;
f453ba04 2843 struct drm_framebuffer *fb;
58c0dca1 2844 int ret;
f453ba04 2845
fb3b06c8
DA
2846 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2847 return -EINVAL;
2848
786b99ed 2849 fb = drm_framebuffer_lookup(dev, r->fb_id);
58c0dca1 2850 if (!fb)
37c4e705 2851 return -ENOENT;
f453ba04
DA
2852
2853 r->height = fb->height;
2854 r->width = fb->width;
2855 r->depth = fb->depth;
2856 r->bpp = fb->bits_per_pixel;
01f2c773 2857 r->pitch = fb->pitches[0];
101b96f3 2858 if (fb->funcs->create_handle) {
09f308f7 2859 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
43683057 2860 drm_is_control_client(file_priv)) {
101b96f3
DH
2861 ret = fb->funcs->create_handle(fb, file_priv,
2862 &r->handle);
2863 } else {
2864 /* GET_FB() is an unprivileged ioctl so we must not
2865 * return a buffer-handle to non-master processes! For
2866 * backwards-compatibility reasons, we cannot make
2867 * GET_FB() privileged, so just return an invalid handle
2868 * for non-masters. */
2869 r->handle = 0;
2870 ret = 0;
2871 }
2872 } else {
af26ef3b 2873 ret = -ENODEV;
101b96f3 2874 }
f453ba04 2875
58c0dca1
DV
2876 drm_framebuffer_unreference(fb);
2877
f453ba04
DA
2878 return ret;
2879}
2880
c8e32cc1
DV
2881/**
2882 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
2883 * @dev: drm device for the ioctl
2884 * @data: data pointer for the ioctl
2885 * @file_priv: drm file for the ioctl call
2886 *
2887 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
2888 * rectangle list. Generic userspace which does frontbuffer rendering must call
2889 * this ioctl to flush out the changes on manual-update display outputs, e.g.
2890 * usb display-link, mipi manual update panels or edp panel self refresh modes.
2891 *
2892 * Modesetting drivers which always update the frontbuffer do not need to
2893 * implement the corresponding ->dirty framebuffer callback.
2894 *
2895 * Called by the user via ioctl.
2896 *
2897 * Returns:
2898 * Zero on success, errno on failure.
2899 */
884840aa
JB
2900int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2901 void *data, struct drm_file *file_priv)
2902{
2903 struct drm_clip_rect __user *clips_ptr;
2904 struct drm_clip_rect *clips = NULL;
2905 struct drm_mode_fb_dirty_cmd *r = data;
884840aa
JB
2906 struct drm_framebuffer *fb;
2907 unsigned flags;
2908 int num_clips;
4a1b0714 2909 int ret;
884840aa 2910
fb3b06c8
DA
2911 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2912 return -EINVAL;
2913
786b99ed 2914 fb = drm_framebuffer_lookup(dev, r->fb_id);
4ccf097f 2915 if (!fb)
37c4e705 2916 return -ENOENT;
884840aa
JB
2917
2918 num_clips = r->num_clips;
81f6c7f8 2919 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
884840aa
JB
2920
2921 if (!num_clips != !clips_ptr) {
2922 ret = -EINVAL;
2923 goto out_err1;
2924 }
2925
2926 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2927
2928 /* If userspace annotates copy, clips must come in pairs */
2929 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2930 ret = -EINVAL;
2931 goto out_err1;
2932 }
2933
2934 if (num_clips && clips_ptr) {
a5cd3351
XW
2935 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2936 ret = -EINVAL;
2937 goto out_err1;
2938 }
884840aa
JB
2939 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2940 if (!clips) {
2941 ret = -ENOMEM;
2942 goto out_err1;
2943 }
2944
2945 ret = copy_from_user(clips, clips_ptr,
2946 num_clips * sizeof(*clips));
e902a358
DC
2947 if (ret) {
2948 ret = -EFAULT;
884840aa 2949 goto out_err2;
e902a358 2950 }
884840aa
JB
2951 }
2952
2953 if (fb->funcs->dirty) {
02b00162
TH
2954 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2955 clips, num_clips);
884840aa
JB
2956 } else {
2957 ret = -ENOSYS;
884840aa
JB
2958 }
2959
2960out_err2:
2961 kfree(clips);
2962out_err1:
4ccf097f
DV
2963 drm_framebuffer_unreference(fb);
2964
884840aa
JB
2965 return ret;
2966}
2967
2968
f453ba04
DA
2969/**
2970 * drm_fb_release - remove and free the FBs on this file
065a50ed 2971 * @priv: drm file for the ioctl
f453ba04 2972 *
f453ba04
DA
2973 * Destroy all the FBs associated with @filp.
2974 *
2975 * Called by the user via ioctl.
2976 *
c8e32cc1 2977 * Returns:
f453ba04
DA
2978 * Zero on success, errno on failure.
2979 */
ea39f835 2980void drm_fb_release(struct drm_file *priv)
f453ba04 2981{
f453ba04
DA
2982 struct drm_device *dev = priv->minor->dev;
2983 struct drm_framebuffer *fb, *tfb;
2984
4b096ac1 2985 mutex_lock(&priv->fbs_lock);
f453ba04 2986 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2b677e8c
DV
2987
2988 mutex_lock(&dev->mode_config.fb_lock);
2989 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2990 __drm_framebuffer_unregister(dev, fb);
2991 mutex_unlock(&dev->mode_config.fb_lock);
2992
4b096ac1 2993 list_del_init(&fb->filp_head);
2b677e8c
DV
2994
2995 /* This will also drop the fpriv->fbs reference. */
f7eff60e 2996 drm_framebuffer_remove(fb);
f453ba04 2997 }
4b096ac1 2998 mutex_unlock(&priv->fbs_lock);
f453ba04
DA
2999}
3000
c8e32cc1
DV
3001/**
3002 * drm_property_create - create a new property type
3003 * @dev: drm device
3004 * @flags: flags specifying the property type
3005 * @name: name of the property
3006 * @num_values: number of pre-defined values
3007 *
3008 * This creates a new generic drm property which can then be attached to a drm
3009 * object with drm_object_attach_property. The returned property object must be
3010 * freed with drm_property_destroy.
3011 *
3012 * Returns:
3013 * A pointer to the newly created property on success, NULL on failure.
3014 */
f453ba04
DA
3015struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3016 const char *name, int num_values)
3017{
3018 struct drm_property *property = NULL;
6bfc56aa 3019 int ret;
f453ba04
DA
3020
3021 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3022 if (!property)
3023 return NULL;
3024
3025 if (num_values) {
3026 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3027 if (!property->values)
3028 goto fail;
3029 }
3030
6bfc56aa
VS
3031 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3032 if (ret)
3033 goto fail;
3034
f453ba04
DA
3035 property->flags = flags;
3036 property->num_values = num_values;
3037 INIT_LIST_HEAD(&property->enum_blob_list);
3038
471dd2ef 3039 if (name) {
f453ba04 3040 strncpy(property->name, name, DRM_PROP_NAME_LEN);
471dd2ef
VL
3041 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3042 }
f453ba04
DA
3043
3044 list_add_tail(&property->head, &dev->mode_config.property_list);
3045 return property;
3046fail:
6bfc56aa 3047 kfree(property->values);
f453ba04
DA
3048 kfree(property);
3049 return NULL;
3050}
3051EXPORT_SYMBOL(drm_property_create);
3052
c8e32cc1
DV
3053/**
3054 * drm_property_create - create a new enumeration property type
3055 * @dev: drm device
3056 * @flags: flags specifying the property type
3057 * @name: name of the property
3058 * @props: enumeration lists with property values
3059 * @num_values: number of pre-defined values
3060 *
3061 * This creates a new generic drm property which can then be attached to a drm
3062 * object with drm_object_attach_property. The returned property object must be
3063 * freed with drm_property_destroy.
3064 *
3065 * Userspace is only allowed to set one of the predefined values for enumeration
3066 * properties.
3067 *
3068 * Returns:
3069 * A pointer to the newly created property on success, NULL on failure.
3070 */
4a67d391
SH
3071struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3072 const char *name,
3073 const struct drm_prop_enum_list *props,
3074 int num_values)
3075{
3076 struct drm_property *property;
3077 int i, ret;
3078
3079 flags |= DRM_MODE_PROP_ENUM;
3080
3081 property = drm_property_create(dev, flags, name, num_values);
3082 if (!property)
3083 return NULL;
3084
3085 for (i = 0; i < num_values; i++) {
3086 ret = drm_property_add_enum(property, i,
3087 props[i].type,
3088 props[i].name);
3089 if (ret) {
3090 drm_property_destroy(dev, property);
3091 return NULL;
3092 }
3093 }
3094
3095 return property;
3096}
3097EXPORT_SYMBOL(drm_property_create_enum);
3098
c8e32cc1
DV
3099/**
3100 * drm_property_create - create a new bitmask property type
3101 * @dev: drm device
3102 * @flags: flags specifying the property type
3103 * @name: name of the property
3104 * @props: enumeration lists with property bitflags
3105 * @num_values: number of pre-defined values
3106 *
3107 * This creates a new generic drm property which can then be attached to a drm
3108 * object with drm_object_attach_property. The returned property object must be
3109 * freed with drm_property_destroy.
3110 *
3111 * Compared to plain enumeration properties userspace is allowed to set any
3112 * or'ed together combination of the predefined property bitflag values
3113 *
3114 * Returns:
3115 * A pointer to the newly created property on success, NULL on failure.
3116 */
49e27545
RC
3117struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3118 int flags, const char *name,
3119 const struct drm_prop_enum_list *props,
3120 int num_values)
3121{
3122 struct drm_property *property;
3123 int i, ret;
3124
3125 flags |= DRM_MODE_PROP_BITMASK;
3126
3127 property = drm_property_create(dev, flags, name, num_values);
3128 if (!property)
3129 return NULL;
3130
3131 for (i = 0; i < num_values; i++) {
3132 ret = drm_property_add_enum(property, i,
3133 props[i].type,
3134 props[i].name);
3135 if (ret) {
3136 drm_property_destroy(dev, property);
3137 return NULL;
3138 }
3139 }
3140
3141 return property;
3142}
3143EXPORT_SYMBOL(drm_property_create_bitmask);
3144
c8e32cc1
DV
3145/**
3146 * drm_property_create - create a new ranged property type
3147 * @dev: drm device
3148 * @flags: flags specifying the property type
3149 * @name: name of the property
3150 * @min: minimum value of the property
3151 * @max: maximum value of the property
3152 *
3153 * This creates a new generic drm property which can then be attached to a drm
3154 * object with drm_object_attach_property. The returned property object must be
3155 * freed with drm_property_destroy.
3156 *
3157 * Userspace is allowed to set any interger value in the (min, max) range
3158 * inclusive.
3159 *
3160 * Returns:
3161 * A pointer to the newly created property on success, NULL on failure.
3162 */
d9bc3c02
SH
3163struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3164 const char *name,
3165 uint64_t min, uint64_t max)
3166{
3167 struct drm_property *property;
3168
3169 flags |= DRM_MODE_PROP_RANGE;
3170
3171 property = drm_property_create(dev, flags, name, 2);
3172 if (!property)
3173 return NULL;
3174
3175 property->values[0] = min;
3176 property->values[1] = max;
3177
3178 return property;
3179}
3180EXPORT_SYMBOL(drm_property_create_range);
3181
c8e32cc1
DV
3182/**
3183 * drm_property_add_enum - add a possible value to an enumeration property
3184 * @property: enumeration property to change
3185 * @index: index of the new enumeration
3186 * @value: value of the new enumeration
3187 * @name: symbolic name of the new enumeration
3188 *
3189 * This functions adds enumerations to a property.
3190 *
3191 * It's use is deprecated, drivers should use one of the more specific helpers
3192 * to directly create the property with all enumerations already attached.
3193 *
3194 * Returns:
3195 * Zero on success, error code on failure.
3196 */
f453ba04
DA
3197int drm_property_add_enum(struct drm_property *property, int index,
3198 uint64_t value, const char *name)
3199{
3200 struct drm_property_enum *prop_enum;
3201
49e27545
RC
3202 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3203 return -EINVAL;
3204
3205 /*
3206 * Bitmask enum properties have the additional constraint of values
3207 * from 0 to 63
3208 */
3209 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
f453ba04
DA
3210 return -EINVAL;
3211
3212 if (!list_empty(&property->enum_blob_list)) {
3213 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3214 if (prop_enum->value == value) {
3215 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3216 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3217 return 0;
3218 }
3219 }
3220 }
3221
3222 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3223 if (!prop_enum)
3224 return -ENOMEM;
3225
3226 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3227 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3228 prop_enum->value = value;
3229
3230 property->values[index] = value;
3231 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3232 return 0;
3233}
3234EXPORT_SYMBOL(drm_property_add_enum);
3235
c8e32cc1
DV
3236/**
3237 * drm_property_destroy - destroy a drm property
3238 * @dev: drm device
3239 * @property: property to destry
3240 *
3241 * This function frees a property including any attached resources like
3242 * enumeration values.
3243 */
f453ba04
DA
3244void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3245{
3246 struct drm_property_enum *prop_enum, *pt;
3247
3248 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3249 list_del(&prop_enum->head);
3250 kfree(prop_enum);
3251 }
3252
3253 if (property->num_values)
3254 kfree(property->values);
3255 drm_mode_object_put(dev, &property->base);
3256 list_del(&property->head);
3257 kfree(property);
3258}
3259EXPORT_SYMBOL(drm_property_destroy);
3260
c8e32cc1
DV
3261/**
3262 * drm_object_attach_property - attach a property to a modeset object
3263 * @obj: drm modeset object
3264 * @property: property to attach
3265 * @init_val: initial value of the property
3266 *
3267 * This attaches the given property to the modeset object with the given initial
3268 * value. Currently this function cannot fail since the properties are stored in
3269 * a statically sized array.
3270 */
c543188a
PZ
3271void drm_object_attach_property(struct drm_mode_object *obj,
3272 struct drm_property *property,
3273 uint64_t init_val)
3274{
7f88a9be 3275 int count = obj->properties->count;
c543188a 3276
7f88a9be
PZ
3277 if (count == DRM_OBJECT_MAX_PROPERTY) {
3278 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3279 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3280 "you see this message on the same object type.\n",
3281 obj->type);
3282 return;
c543188a
PZ
3283 }
3284
7f88a9be
PZ
3285 obj->properties->ids[count] = property->base.id;
3286 obj->properties->values[count] = init_val;
3287 obj->properties->count++;
c543188a
PZ
3288}
3289EXPORT_SYMBOL(drm_object_attach_property);
3290
c8e32cc1
DV
3291/**
3292 * drm_object_property_set_value - set the value of a property
3293 * @obj: drm mode object to set property value for
3294 * @property: property to set
3295 * @val: value the property should be set to
3296 *
3297 * This functions sets a given property on a given object. This function only
3298 * changes the software state of the property, it does not call into the
3299 * driver's ->set_property callback.
3300 *
3301 * Returns:
3302 * Zero on success, error code on failure.
3303 */
c543188a
PZ
3304int drm_object_property_set_value(struct drm_mode_object *obj,
3305 struct drm_property *property, uint64_t val)
3306{
3307 int i;
3308
7f88a9be 3309 for (i = 0; i < obj->properties->count; i++) {
c543188a
PZ
3310 if (obj->properties->ids[i] == property->base.id) {
3311 obj->properties->values[i] = val;
3312 return 0;
3313 }
3314 }
3315
3316 return -EINVAL;
3317}
3318EXPORT_SYMBOL(drm_object_property_set_value);
3319
c8e32cc1
DV
3320/**
3321 * drm_object_property_get_value - retrieve the value of a property
3322 * @obj: drm mode object to get property value from
3323 * @property: property to retrieve
3324 * @val: storage for the property value
3325 *
3326 * This function retrieves the softare state of the given property for the given
3327 * property. Since there is no driver callback to retrieve the current property
3328 * value this might be out of sync with the hardware, depending upon the driver
3329 * and property.
3330 *
3331 * Returns:
3332 * Zero on success, error code on failure.
3333 */
c543188a
PZ
3334int drm_object_property_get_value(struct drm_mode_object *obj,
3335 struct drm_property *property, uint64_t *val)
3336{
3337 int i;
3338
7f88a9be 3339 for (i = 0; i < obj->properties->count; i++) {
c543188a
PZ
3340 if (obj->properties->ids[i] == property->base.id) {
3341 *val = obj->properties->values[i];
3342 return 0;
3343 }
3344 }
3345
3346 return -EINVAL;
3347}
3348EXPORT_SYMBOL(drm_object_property_get_value);
3349
c8e32cc1
DV
3350/**
3351 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3352 * @dev: DRM device
3353 * @data: ioctl data
3354 * @file_priv: DRM file info
3355 *
3356 * This function retrieves the current value for an connectors's property.
3357 *
3358 * Called by the user via ioctl.
3359 *
3360 * Returns:
3361 * Zero on success, errno on failure.
3362 */
f453ba04
DA
3363int drm_mode_getproperty_ioctl(struct drm_device *dev,
3364 void *data, struct drm_file *file_priv)
3365{
3366 struct drm_mode_object *obj;
3367 struct drm_mode_get_property *out_resp = data;
3368 struct drm_property *property;
3369 int enum_count = 0;
3370 int blob_count = 0;
3371 int value_count = 0;
3372 int ret = 0, i;
3373 int copied;
3374 struct drm_property_enum *prop_enum;
3375 struct drm_mode_property_enum __user *enum_ptr;
3376 struct drm_property_blob *prop_blob;
81f6c7f8 3377 uint32_t __user *blob_id_ptr;
f453ba04
DA
3378 uint64_t __user *values_ptr;
3379 uint32_t __user *blob_length_ptr;
3380
fb3b06c8
DA
3381 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3382 return -EINVAL;
3383
84849903 3384 drm_modeset_lock_all(dev);
f453ba04
DA
3385 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3386 if (!obj) {
f27657f2 3387 ret = -ENOENT;
f453ba04
DA
3388 goto done;
3389 }
3390 property = obj_to_property(obj);
3391
49e27545 3392 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
f453ba04
DA
3393 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3394 enum_count++;
3395 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3396 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3397 blob_count++;
3398 }
3399
3400 value_count = property->num_values;
3401
3402 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3403 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3404 out_resp->flags = property->flags;
3405
3406 if ((out_resp->count_values >= value_count) && value_count) {
81f6c7f8 3407 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
f453ba04
DA
3408 for (i = 0; i < value_count; i++) {
3409 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3410 ret = -EFAULT;
3411 goto done;
3412 }
3413 }
3414 }
3415 out_resp->count_values = value_count;
3416
49e27545 3417 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
f453ba04
DA
3418 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3419 copied = 0;
81f6c7f8 3420 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
f453ba04
DA
3421 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3422
3423 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3424 ret = -EFAULT;
3425 goto done;
3426 }
3427
3428 if (copy_to_user(&enum_ptr[copied].name,
3429 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3430 ret = -EFAULT;
3431 goto done;
3432 }
3433 copied++;
3434 }
3435 }
3436 out_resp->count_enum_blobs = enum_count;
3437 }
3438
3439 if (property->flags & DRM_MODE_PROP_BLOB) {
3440 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3441 copied = 0;
81f6c7f8
VS
3442 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3443 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
f453ba04
DA
3444
3445 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3446 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3447 ret = -EFAULT;
3448 goto done;
3449 }
3450
3451 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3452 ret = -EFAULT;
3453 goto done;
3454 }
3455
3456 copied++;
3457 }
3458 }
3459 out_resp->count_enum_blobs = blob_count;
3460 }
3461done:
84849903 3462 drm_modeset_unlock_all(dev);
f453ba04
DA
3463 return ret;
3464}
3465
3466static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3467 void *data)
3468{
3469 struct drm_property_blob *blob;
6bfc56aa 3470 int ret;
f453ba04
DA
3471
3472 if (!length || !data)
3473 return NULL;
3474
3475 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3476 if (!blob)
3477 return NULL;
3478
6bfc56aa
VS
3479 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3480 if (ret) {
3481 kfree(blob);
3482 return NULL;
3483 }
3484
f453ba04
DA
3485 blob->length = length;
3486
3487 memcpy(blob->data, data, length);
3488
f453ba04
DA
3489 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3490 return blob;
3491}
3492
3493static void drm_property_destroy_blob(struct drm_device *dev,
3494 struct drm_property_blob *blob)
3495{
3496 drm_mode_object_put(dev, &blob->base);
3497 list_del(&blob->head);
3498 kfree(blob);
3499}
3500
c8e32cc1
DV
3501/**
3502 * drm_mode_getblob_ioctl - get the contents of a blob property value
3503 * @dev: DRM device
3504 * @data: ioctl data
3505 * @file_priv: DRM file info
3506 *
3507 * This function retrieves the contents of a blob property. The value stored in
3508 * an object's blob property is just a normal modeset object id.
3509 *
3510 * Called by the user via ioctl.
3511 *
3512 * Returns:
3513 * Zero on success, errno on failure.
3514 */
f453ba04
DA
3515int drm_mode_getblob_ioctl(struct drm_device *dev,
3516 void *data, struct drm_file *file_priv)
3517{
3518 struct drm_mode_object *obj;
3519 struct drm_mode_get_blob *out_resp = data;
3520 struct drm_property_blob *blob;
3521 int ret = 0;
81f6c7f8 3522 void __user *blob_ptr;
f453ba04 3523
fb3b06c8
DA
3524 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3525 return -EINVAL;
3526
84849903 3527 drm_modeset_lock_all(dev);
f453ba04
DA
3528 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3529 if (!obj) {
f27657f2 3530 ret = -ENOENT;
f453ba04
DA
3531 goto done;
3532 }
3533 blob = obj_to_blob(obj);
3534
3535 if (out_resp->length == blob->length) {
81f6c7f8 3536 blob_ptr = (void __user *)(unsigned long)out_resp->data;
f453ba04
DA
3537 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3538 ret = -EFAULT;
3539 goto done;
3540 }
3541 }
3542 out_resp->length = blob->length;
3543
3544done:
84849903 3545 drm_modeset_unlock_all(dev);
f453ba04
DA
3546 return ret;
3547}
3548
c8e32cc1
DV
3549/**
3550 * drm_mode_connector_update_edid_property - update the edid property of a connector
3551 * @connector: drm connector
3552 * @edid: new value of the edid property
3553 *
3554 * This function creates a new blob modeset object and assigns its id to the
3555 * connector's edid property.
3556 *
3557 * Returns:
3558 * Zero on success, errno on failure.
3559 */
f453ba04
DA
3560int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3561 struct edid *edid)
3562{
3563 struct drm_device *dev = connector->dev;
4a1b0714 3564 int ret, size;
f453ba04
DA
3565
3566 if (connector->edid_blob_ptr)
3567 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3568
3569 /* Delete edid, when there is none. */
3570 if (!edid) {
3571 connector->edid_blob_ptr = NULL;
58495563 3572 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
f453ba04
DA
3573 return ret;
3574 }
3575
7466f4cc
AJ
3576 size = EDID_LENGTH * (1 + edid->extensions);
3577 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3578 size, edid);
e655d122
SK
3579 if (!connector->edid_blob_ptr)
3580 return -EINVAL;
f453ba04 3581
58495563 3582 ret = drm_object_property_set_value(&connector->base,
f453ba04
DA
3583 dev->mode_config.edid_property,
3584 connector->edid_blob_ptr->base.id);
3585
3586 return ret;
3587}
3588EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3589
26a34815 3590static bool drm_property_change_is_valid(struct drm_property *property,
592c20ee 3591 uint64_t value)
26a34815
PZ
3592{
3593 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3594 return false;
3595 if (property->flags & DRM_MODE_PROP_RANGE) {
3596 if (value < property->values[0] || value > property->values[1])
3597 return false;
3598 return true;
49e27545
RC
3599 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3600 int i;
592c20ee 3601 uint64_t valid_mask = 0;
49e27545
RC
3602 for (i = 0; i < property->num_values; i++)
3603 valid_mask |= (1ULL << property->values[i]);
3604 return !(value & ~valid_mask);
c4a56750
VS
3605 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3606 /* Only the driver knows */
3607 return true;
26a34815
PZ
3608 } else {
3609 int i;
3610 for (i = 0; i < property->num_values; i++)
3611 if (property->values[i] == value)
3612 return true;
3613 return false;
3614 }
3615}
3616
c8e32cc1
DV
3617/**
3618 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3619 * @dev: DRM device
3620 * @data: ioctl data
3621 * @file_priv: DRM file info
3622 *
3623 * This function sets the current value for a connectors's property. It also
3624 * calls into a driver's ->set_property callback to update the hardware state
3625 *
3626 * Called by the user via ioctl.
3627 *
3628 * Returns:
3629 * Zero on success, errno on failure.
3630 */
f453ba04
DA
3631int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3632 void *data, struct drm_file *file_priv)
3633{
0057d8dd
PZ
3634 struct drm_mode_connector_set_property *conn_set_prop = data;
3635 struct drm_mode_obj_set_property obj_set_prop = {
3636 .value = conn_set_prop->value,
3637 .prop_id = conn_set_prop->prop_id,
3638 .obj_id = conn_set_prop->connector_id,
3639 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3640 };
fb3b06c8 3641
0057d8dd
PZ
3642 /* It does all the locking and checking we need */
3643 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
f453ba04
DA
3644}
3645
c543188a
PZ
3646static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3647 struct drm_property *property,
3648 uint64_t value)
3649{
3650 int ret = -EINVAL;
3651 struct drm_connector *connector = obj_to_connector(obj);
3652
3653 /* Do DPMS ourselves */
3654 if (property == connector->dev->mode_config.dpms_property) {
3655 if (connector->funcs->dpms)
3656 (*connector->funcs->dpms)(connector, (int)value);
3657 ret = 0;
3658 } else if (connector->funcs->set_property)
3659 ret = connector->funcs->set_property(connector, property, value);
3660
3661 /* store the property value if successful */
3662 if (!ret)
58495563 3663 drm_object_property_set_value(&connector->base, property, value);
c543188a
PZ
3664 return ret;
3665}
3666
bffd9de0
PZ
3667static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3668 struct drm_property *property,
3669 uint64_t value)
3670{
3671 int ret = -EINVAL;
3672 struct drm_crtc *crtc = obj_to_crtc(obj);
3673
3674 if (crtc->funcs->set_property)
3675 ret = crtc->funcs->set_property(crtc, property, value);
3676 if (!ret)
3677 drm_object_property_set_value(obj, property, value);
3678
3679 return ret;
3680}
3681
4d93914a
RC
3682static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3683 struct drm_property *property,
3684 uint64_t value)
3685{
3686 int ret = -EINVAL;
3687 struct drm_plane *plane = obj_to_plane(obj);
3688
3689 if (plane->funcs->set_property)
3690 ret = plane->funcs->set_property(plane, property, value);
3691 if (!ret)
3692 drm_object_property_set_value(obj, property, value);
3693
3694 return ret;
3695}
3696
c8e32cc1
DV
3697/**
3698 * drm_mode_getproperty_ioctl - get the current value of a object's property
3699 * @dev: DRM device
3700 * @data: ioctl data
3701 * @file_priv: DRM file info
3702 *
3703 * This function retrieves the current value for an object's property. Compared
3704 * to the connector specific ioctl this one is extended to also work on crtc and
3705 * plane objects.
3706 *
3707 * Called by the user via ioctl.
3708 *
3709 * Returns:
3710 * Zero on success, errno on failure.
3711 */
c543188a
PZ
3712int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3713 struct drm_file *file_priv)
3714{
3715 struct drm_mode_obj_get_properties *arg = data;
3716 struct drm_mode_object *obj;
3717 int ret = 0;
3718 int i;
3719 int copied = 0;
3720 int props_count = 0;
3721 uint32_t __user *props_ptr;
3722 uint64_t __user *prop_values_ptr;
3723
3724 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3725 return -EINVAL;
3726
84849903 3727 drm_modeset_lock_all(dev);
c543188a
PZ
3728
3729 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3730 if (!obj) {
f27657f2 3731 ret = -ENOENT;
c543188a
PZ
3732 goto out;
3733 }
3734 if (!obj->properties) {
3735 ret = -EINVAL;
3736 goto out;
3737 }
3738
7f88a9be 3739 props_count = obj->properties->count;
c543188a
PZ
3740
3741 /* This ioctl is called twice, once to determine how much space is
3742 * needed, and the 2nd time to fill it. */
3743 if ((arg->count_props >= props_count) && props_count) {
3744 copied = 0;
3745 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3746 prop_values_ptr = (uint64_t __user *)(unsigned long)
3747 (arg->prop_values_ptr);
3748 for (i = 0; i < props_count; i++) {
3749 if (put_user(obj->properties->ids[i],
3750 props_ptr + copied)) {
3751 ret = -EFAULT;
3752 goto out;
3753 }
3754 if (put_user(obj->properties->values[i],
3755 prop_values_ptr + copied)) {
3756 ret = -EFAULT;
3757 goto out;
3758 }
3759 copied++;
3760 }
3761 }
3762 arg->count_props = props_count;
3763out:
84849903 3764 drm_modeset_unlock_all(dev);
c543188a
PZ
3765 return ret;
3766}
3767
c8e32cc1
DV
3768/**
3769 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3770 * @dev: DRM device
3771 * @data: ioctl data
3772 * @file_priv: DRM file info
3773 *
3774 * This function sets the current value for an object's property. It also calls
3775 * into a driver's ->set_property callback to update the hardware state.
3776 * Compared to the connector specific ioctl this one is extended to also work on
3777 * crtc and plane objects.
3778 *
3779 * Called by the user via ioctl.
3780 *
3781 * Returns:
3782 * Zero on success, errno on failure.
3783 */
c543188a
PZ
3784int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3785 struct drm_file *file_priv)
3786{
3787 struct drm_mode_obj_set_property *arg = data;
3788 struct drm_mode_object *arg_obj;
3789 struct drm_mode_object *prop_obj;
3790 struct drm_property *property;
3791 int ret = -EINVAL;
3792 int i;
3793
3794 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3795 return -EINVAL;
3796
84849903 3797 drm_modeset_lock_all(dev);
c543188a
PZ
3798
3799 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
f27657f2
VS
3800 if (!arg_obj) {
3801 ret = -ENOENT;
c543188a 3802 goto out;
f27657f2 3803 }
c543188a
PZ
3804 if (!arg_obj->properties)
3805 goto out;
3806
7f88a9be 3807 for (i = 0; i < arg_obj->properties->count; i++)
c543188a
PZ
3808 if (arg_obj->properties->ids[i] == arg->prop_id)
3809 break;
3810
7f88a9be 3811 if (i == arg_obj->properties->count)
c543188a
PZ
3812 goto out;
3813
3814 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3815 DRM_MODE_OBJECT_PROPERTY);
f27657f2
VS
3816 if (!prop_obj) {
3817 ret = -ENOENT;
c543188a 3818 goto out;
f27657f2 3819 }
c543188a
PZ
3820 property = obj_to_property(prop_obj);
3821
3822 if (!drm_property_change_is_valid(property, arg->value))
3823 goto out;
3824
3825 switch (arg_obj->type) {
3826 case DRM_MODE_OBJECT_CONNECTOR:
3827 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3828 arg->value);
3829 break;
bffd9de0
PZ
3830 case DRM_MODE_OBJECT_CRTC:
3831 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3832 break;
4d93914a
RC
3833 case DRM_MODE_OBJECT_PLANE:
3834 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3835 break;
c543188a
PZ
3836 }
3837
3838out:
84849903 3839 drm_modeset_unlock_all(dev);
c543188a
PZ
3840 return ret;
3841}
3842
c8e32cc1
DV
3843/**
3844 * drm_mode_connector_attach_encoder - attach a connector to an encoder
3845 * @connector: connector to attach
3846 * @encoder: encoder to attach @connector to
3847 *
3848 * This function links up a connector to an encoder. Note that the routing
3849 * restrictions between encoders and crtcs are exposed to userspace through the
3850 * possible_clones and possible_crtcs bitmasks.
3851 *
3852 * Returns:
3853 * Zero on success, errno on failure.
3854 */
f453ba04
DA
3855int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3856 struct drm_encoder *encoder)
3857{
3858 int i;
3859
3860 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3861 if (connector->encoder_ids[i] == 0) {
3862 connector->encoder_ids[i] = encoder->base.id;
3863 return 0;
3864 }
3865 }
3866 return -ENOMEM;
3867}
3868EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3869
c8e32cc1
DV
3870/**
3871 * drm_mode_crtc_set_gamma_size - set the gamma table size
3872 * @crtc: CRTC to set the gamma table size for
3873 * @gamma_size: size of the gamma table
3874 *
3875 * Drivers which support gamma tables should set this to the supported gamma
3876 * table size when initializing the CRTC. Currently the drm core only supports a
3877 * fixed gamma table size.
3878 *
3879 * Returns:
3880 * Zero on success, errno on failure.
3881 */
4cae5b84 3882int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
c8e32cc1 3883 int gamma_size)
f453ba04
DA
3884{
3885 crtc->gamma_size = gamma_size;
3886
3887 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3888 if (!crtc->gamma_store) {
3889 crtc->gamma_size = 0;
4cae5b84 3890 return -ENOMEM;
f453ba04
DA
3891 }
3892
4cae5b84 3893 return 0;
f453ba04
DA
3894}
3895EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3896
c8e32cc1
DV
3897/**
3898 * drm_mode_gamma_set_ioctl - set the gamma table
3899 * @dev: DRM device
3900 * @data: ioctl data
3901 * @file_priv: DRM file info
3902 *
3903 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
3904 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
3905 *
3906 * Called by the user via ioctl.
3907 *
3908 * Returns:
3909 * Zero on success, errno on failure.
3910 */
f453ba04
DA
3911int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3912 void *data, struct drm_file *file_priv)
3913{
3914 struct drm_mode_crtc_lut *crtc_lut = data;
3915 struct drm_mode_object *obj;
3916 struct drm_crtc *crtc;
3917 void *r_base, *g_base, *b_base;
3918 int size;
3919 int ret = 0;
3920
fb3b06c8
DA
3921 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3922 return -EINVAL;
3923
84849903 3924 drm_modeset_lock_all(dev);
f453ba04
DA
3925 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3926 if (!obj) {
f27657f2 3927 ret = -ENOENT;
f453ba04
DA
3928 goto out;
3929 }
3930 crtc = obj_to_crtc(obj);
3931
ebe0f244
LP
3932 if (crtc->funcs->gamma_set == NULL) {
3933 ret = -ENOSYS;
3934 goto out;
3935 }
3936
f453ba04
DA
3937 /* memcpy into gamma store */
3938 if (crtc_lut->gamma_size != crtc->gamma_size) {
3939 ret = -EINVAL;
3940 goto out;
3941 }
3942
3943 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3944 r_base = crtc->gamma_store;
3945 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3946 ret = -EFAULT;
3947 goto out;
3948 }
3949
3950 g_base = r_base + size;
3951 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3952 ret = -EFAULT;
3953 goto out;
3954 }
3955
3956 b_base = g_base + size;
3957 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3958 ret = -EFAULT;
3959 goto out;
3960 }
3961
7203425a 3962 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
f453ba04
DA
3963
3964out:
84849903 3965 drm_modeset_unlock_all(dev);
f453ba04
DA
3966 return ret;
3967
3968}
3969
c8e32cc1
DV
3970/**
3971 * drm_mode_gamma_get_ioctl - get the gamma table
3972 * @dev: DRM device
3973 * @data: ioctl data
3974 * @file_priv: DRM file info
3975 *
3976 * Copy the current gamma table into the storage provided. This also provides
3977 * the gamma table size the driver expects, which can be used to size the
3978 * allocated storage.
3979 *
3980 * Called by the user via ioctl.
3981 *
3982 * Returns:
3983 * Zero on success, errno on failure.
3984 */
f453ba04
DA
3985int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3986 void *data, struct drm_file *file_priv)
3987{
3988 struct drm_mode_crtc_lut *crtc_lut = data;
3989 struct drm_mode_object *obj;
3990 struct drm_crtc *crtc;
3991 void *r_base, *g_base, *b_base;
3992 int size;
3993 int ret = 0;
3994
fb3b06c8
DA
3995 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3996 return -EINVAL;
3997
84849903 3998 drm_modeset_lock_all(dev);
f453ba04
DA
3999 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
4000 if (!obj) {
f27657f2 4001 ret = -ENOENT;
f453ba04
DA
4002 goto out;
4003 }
4004 crtc = obj_to_crtc(obj);
4005
4006 /* memcpy into gamma store */
4007 if (crtc_lut->gamma_size != crtc->gamma_size) {
4008 ret = -EINVAL;
4009 goto out;
4010 }
4011
4012 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4013 r_base = crtc->gamma_store;
4014 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4015 ret = -EFAULT;
4016 goto out;
4017 }
4018
4019 g_base = r_base + size;
4020 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4021 ret = -EFAULT;
4022 goto out;
4023 }
4024
4025 b_base = g_base + size;
4026 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4027 ret = -EFAULT;
4028 goto out;
4029 }
4030out:
84849903 4031 drm_modeset_unlock_all(dev);
f453ba04
DA
4032 return ret;
4033}
d91d8a3f 4034
c8e32cc1
DV
4035/**
4036 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4037 * @dev: DRM device
4038 * @data: ioctl data
4039 * @file_priv: DRM file info
4040 *
4041 * This schedules an asynchronous update on a given CRTC, called page flip.
4042 * Optionally a drm event is generated to signal the completion of the event.
4043 * Generic drivers cannot assume that a pageflip with changed framebuffer
4044 * properties (including driver specific metadata like tiling layout) will work,
4045 * but some drivers support e.g. pixel format changes through the pageflip
4046 * ioctl.
4047 *
4048 * Called by the user via ioctl.
4049 *
4050 * Returns:
4051 * Zero on success, errno on failure.
4052 */
d91d8a3f
KH
4053int drm_mode_page_flip_ioctl(struct drm_device *dev,
4054 void *data, struct drm_file *file_priv)
4055{
4056 struct drm_mode_crtc_page_flip *page_flip = data;
4057 struct drm_mode_object *obj;
4058 struct drm_crtc *crtc;
b0d12325 4059 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
d91d8a3f
KH
4060 struct drm_pending_vblank_event *e = NULL;
4061 unsigned long flags;
4062 int ret = -EINVAL;
4063
4064 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4065 page_flip->reserved != 0)
4066 return -EINVAL;
4067
62f2104f
KP
4068 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4069 return -EINVAL;
4070
d91d8a3f
KH
4071 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
4072 if (!obj)
f27657f2 4073 return -ENOENT;
d91d8a3f
KH
4074 crtc = obj_to_crtc(obj);
4075
b4d5e7d1 4076 mutex_lock(&crtc->mutex);
90c1efdd
CW
4077 if (crtc->fb == NULL) {
4078 /* The framebuffer is currently unbound, presumably
4079 * due to a hotplug event, that userspace has not
4080 * yet discovered.
4081 */
4082 ret = -EBUSY;
4083 goto out;
4084 }
4085
d91d8a3f
KH
4086 if (crtc->funcs->page_flip == NULL)
4087 goto out;
4088
786b99ed 4089 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
37c4e705
VS
4090 if (!fb) {
4091 ret = -ENOENT;
d91d8a3f 4092 goto out;
37c4e705 4093 }
d91d8a3f 4094
c11e9283
DL
4095 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4096 if (ret)
5f61bb42 4097 goto out;
5f61bb42 4098
909d9cda
LP
4099 if (crtc->fb->pixel_format != fb->pixel_format) {
4100 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4101 ret = -EINVAL;
4102 goto out;
4103 }
4104
d91d8a3f
KH
4105 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4106 ret = -ENOMEM;
4107 spin_lock_irqsave(&dev->event_lock, flags);
4108 if (file_priv->event_space < sizeof e->event) {
4109 spin_unlock_irqrestore(&dev->event_lock, flags);
4110 goto out;
4111 }
4112 file_priv->event_space -= sizeof e->event;
4113 spin_unlock_irqrestore(&dev->event_lock, flags);
4114
4115 e = kzalloc(sizeof *e, GFP_KERNEL);
4116 if (e == NULL) {
4117 spin_lock_irqsave(&dev->event_lock, flags);
4118 file_priv->event_space += sizeof e->event;
4119 spin_unlock_irqrestore(&dev->event_lock, flags);
4120 goto out;
4121 }
4122
7bd4d7be 4123 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
d91d8a3f
KH
4124 e->event.base.length = sizeof e->event;
4125 e->event.user_data = page_flip->user_data;
4126 e->base.event = &e->event.base;
4127 e->base.file_priv = file_priv;
4128 e->base.destroy =
4129 (void (*) (struct drm_pending_event *)) kfree;
4130 }
4131
b0d12325 4132 old_fb = crtc->fb;
ed8d1975 4133 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
d91d8a3f 4134 if (ret) {
aef6a7ee
JS
4135 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4136 spin_lock_irqsave(&dev->event_lock, flags);
4137 file_priv->event_space += sizeof e->event;
4138 spin_unlock_irqrestore(&dev->event_lock, flags);
4139 kfree(e);
4140 }
b0d12325
DV
4141 /* Keep the old fb, don't unref it. */
4142 old_fb = NULL;
4143 } else {
8cf1e981
TR
4144 /*
4145 * Warn if the driver hasn't properly updated the crtc->fb
4146 * field to reflect that the new framebuffer is now used.
4147 * Failing to do so will screw with the reference counting
4148 * on framebuffers.
4149 */
4150 WARN_ON(crtc->fb != fb);
b0d12325
DV
4151 /* Unref only the old framebuffer. */
4152 fb = NULL;
d91d8a3f
KH
4153 }
4154
4155out:
b0d12325
DV
4156 if (fb)
4157 drm_framebuffer_unreference(fb);
4158 if (old_fb)
4159 drm_framebuffer_unreference(old_fb);
b4d5e7d1
DV
4160 mutex_unlock(&crtc->mutex);
4161
d91d8a3f
KH
4162 return ret;
4163}
eb033556 4164
c8e32cc1
DV
4165/**
4166 * drm_mode_config_reset - call ->reset callbacks
4167 * @dev: drm device
4168 *
4169 * This functions calls all the crtc's, encoder's and connector's ->reset
4170 * callback. Drivers can use this in e.g. their driver load or resume code to
4171 * reset hardware and software state.
4172 */
eb033556
CW
4173void drm_mode_config_reset(struct drm_device *dev)
4174{
4175 struct drm_crtc *crtc;
4176 struct drm_encoder *encoder;
4177 struct drm_connector *connector;
4178
4179 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4180 if (crtc->funcs->reset)
4181 crtc->funcs->reset(crtc);
4182
4183 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4184 if (encoder->funcs->reset)
4185 encoder->funcs->reset(encoder);
4186
5e2cb2f6
DV
4187 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4188 connector->status = connector_status_unknown;
4189
eb033556
CW
4190 if (connector->funcs->reset)
4191 connector->funcs->reset(connector);
5e2cb2f6 4192 }
eb033556
CW
4193}
4194EXPORT_SYMBOL(drm_mode_config_reset);
ff72145b 4195
c8e32cc1
DV
4196/**
4197 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4198 * @dev: DRM device
4199 * @data: ioctl data
4200 * @file_priv: DRM file info
4201 *
4202 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4203 * TTM or something else entirely) and returns the resulting buffer handle. This
4204 * handle can then be wrapped up into a framebuffer modeset object.
4205 *
4206 * Note that userspace is not allowed to use such objects for render
4207 * acceleration - drivers must create their own private ioctls for such a use
4208 * case.
4209 *
4210 * Called by the user via ioctl.
4211 *
4212 * Returns:
4213 * Zero on success, errno on failure.
4214 */
ff72145b
DA
4215int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4216 void *data, struct drm_file *file_priv)
4217{
4218 struct drm_mode_create_dumb *args = data;
b28cd41f 4219 u32 cpp, stride, size;
ff72145b
DA
4220
4221 if (!dev->driver->dumb_create)
4222 return -ENOSYS;
b28cd41f
DH
4223 if (!args->width || !args->height || !args->bpp)
4224 return -EINVAL;
4225
4226 /* overflow checks for 32bit size calculations */
4227 cpp = DIV_ROUND_UP(args->bpp, 8);
4228 if (cpp > 0xffffffffU / args->width)
4229 return -EINVAL;
4230 stride = cpp * args->width;
4231 if (args->height > 0xffffffffU / stride)
4232 return -EINVAL;
4233
4234 /* test for wrap-around */
4235 size = args->height * stride;
4236 if (PAGE_ALIGN(size) == 0)
4237 return -EINVAL;
4238
ff72145b
DA
4239 return dev->driver->dumb_create(file_priv, dev, args);
4240}
4241
c8e32cc1
DV
4242/**
4243 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4244 * @dev: DRM device
4245 * @data: ioctl data
4246 * @file_priv: DRM file info
4247 *
4248 * Allocate an offset in the drm device node's address space to be able to
4249 * memory map a dumb buffer.
4250 *
4251 * Called by the user via ioctl.
4252 *
4253 * Returns:
4254 * Zero on success, errno on failure.
4255 */
ff72145b
DA
4256int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4257 void *data, struct drm_file *file_priv)
4258{
4259 struct drm_mode_map_dumb *args = data;
4260
4261 /* call driver ioctl to get mmap offset */
4262 if (!dev->driver->dumb_map_offset)
4263 return -ENOSYS;
4264
4265 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4266}
4267
c8e32cc1
DV
4268/**
4269 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4270 * @dev: DRM device
4271 * @data: ioctl data
4272 * @file_priv: DRM file info
4273 *
4274 * This destroys the userspace handle for the given dumb backing storage buffer.
4275 * Since buffer objects must be reference counted in the kernel a buffer object
4276 * won't be immediately freed if a framebuffer modeset object still uses it.
4277 *
4278 * Called by the user via ioctl.
4279 *
4280 * Returns:
4281 * Zero on success, errno on failure.
4282 */
ff72145b
DA
4283int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4284 void *data, struct drm_file *file_priv)
4285{
4286 struct drm_mode_destroy_dumb *args = data;
4287
4288 if (!dev->driver->dumb_destroy)
4289 return -ENOSYS;
4290
4291 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4292}
248dbc23 4293
c8e32cc1
DV
4294/**
4295 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4296 * @format: pixel format (DRM_FORMAT_*)
4297 * @depth: storage for the depth value
4298 * @bpp: storage for the bpp value
4299 *
4300 * This only supports RGB formats here for compat with code that doesn't use
4301 * pixel formats directly yet.
248dbc23
DA
4302 */
4303void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4304 int *bpp)
4305{
4306 switch (format) {
c51a6bc5 4307 case DRM_FORMAT_C8:
04b3924d
VS
4308 case DRM_FORMAT_RGB332:
4309 case DRM_FORMAT_BGR233:
248dbc23
DA
4310 *depth = 8;
4311 *bpp = 8;
4312 break;
04b3924d
VS
4313 case DRM_FORMAT_XRGB1555:
4314 case DRM_FORMAT_XBGR1555:
4315 case DRM_FORMAT_RGBX5551:
4316 case DRM_FORMAT_BGRX5551:
4317 case DRM_FORMAT_ARGB1555:
4318 case DRM_FORMAT_ABGR1555:
4319 case DRM_FORMAT_RGBA5551:
4320 case DRM_FORMAT_BGRA5551:
248dbc23
DA
4321 *depth = 15;
4322 *bpp = 16;
4323 break;
04b3924d
VS
4324 case DRM_FORMAT_RGB565:
4325 case DRM_FORMAT_BGR565:
248dbc23
DA
4326 *depth = 16;
4327 *bpp = 16;
4328 break;
04b3924d
VS
4329 case DRM_FORMAT_RGB888:
4330 case DRM_FORMAT_BGR888:
4331 *depth = 24;
4332 *bpp = 24;
4333 break;
4334 case DRM_FORMAT_XRGB8888:
4335 case DRM_FORMAT_XBGR8888:
4336 case DRM_FORMAT_RGBX8888:
4337 case DRM_FORMAT_BGRX8888:
248dbc23
DA
4338 *depth = 24;
4339 *bpp = 32;
4340 break;
04b3924d
VS
4341 case DRM_FORMAT_XRGB2101010:
4342 case DRM_FORMAT_XBGR2101010:
4343 case DRM_FORMAT_RGBX1010102:
4344 case DRM_FORMAT_BGRX1010102:
4345 case DRM_FORMAT_ARGB2101010:
4346 case DRM_FORMAT_ABGR2101010:
4347 case DRM_FORMAT_RGBA1010102:
4348 case DRM_FORMAT_BGRA1010102:
248dbc23
DA
4349 *depth = 30;
4350 *bpp = 32;
4351 break;
04b3924d
VS
4352 case DRM_FORMAT_ARGB8888:
4353 case DRM_FORMAT_ABGR8888:
4354 case DRM_FORMAT_RGBA8888:
4355 case DRM_FORMAT_BGRA8888:
248dbc23
DA
4356 *depth = 32;
4357 *bpp = 32;
4358 break;
4359 default:
23c453a4
VS
4360 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4361 drm_get_format_name(format));
248dbc23
DA
4362 *depth = 0;
4363 *bpp = 0;
4364 break;
4365 }
4366}
4367EXPORT_SYMBOL(drm_fb_get_bpp_depth);
141670e9
VS
4368
4369/**
4370 * drm_format_num_planes - get the number of planes for format
4371 * @format: pixel format (DRM_FORMAT_*)
4372 *
c8e32cc1 4373 * Returns:
141670e9
VS
4374 * The number of planes used by the specified pixel format.
4375 */
4376int drm_format_num_planes(uint32_t format)
4377{
4378 switch (format) {
4379 case DRM_FORMAT_YUV410:
4380 case DRM_FORMAT_YVU410:
4381 case DRM_FORMAT_YUV411:
4382 case DRM_FORMAT_YVU411:
4383 case DRM_FORMAT_YUV420:
4384 case DRM_FORMAT_YVU420:
4385 case DRM_FORMAT_YUV422:
4386 case DRM_FORMAT_YVU422:
4387 case DRM_FORMAT_YUV444:
4388 case DRM_FORMAT_YVU444:
4389 return 3;
4390 case DRM_FORMAT_NV12:
4391 case DRM_FORMAT_NV21:
4392 case DRM_FORMAT_NV16:
4393 case DRM_FORMAT_NV61:
ba623f6a
LP
4394 case DRM_FORMAT_NV24:
4395 case DRM_FORMAT_NV42:
141670e9
VS
4396 return 2;
4397 default:
4398 return 1;
4399 }
4400}
4401EXPORT_SYMBOL(drm_format_num_planes);
5a86bd55
VS
4402
4403/**
4404 * drm_format_plane_cpp - determine the bytes per pixel value
4405 * @format: pixel format (DRM_FORMAT_*)
4406 * @plane: plane index
4407 *
c8e32cc1 4408 * Returns:
5a86bd55
VS
4409 * The bytes per pixel value for the specified plane.
4410 */
4411int drm_format_plane_cpp(uint32_t format, int plane)
4412{
4413 unsigned int depth;
4414 int bpp;
4415
4416 if (plane >= drm_format_num_planes(format))
4417 return 0;
4418
4419 switch (format) {
4420 case DRM_FORMAT_YUYV:
4421 case DRM_FORMAT_YVYU:
4422 case DRM_FORMAT_UYVY:
4423 case DRM_FORMAT_VYUY:
4424 return 2;
4425 case DRM_FORMAT_NV12:
4426 case DRM_FORMAT_NV21:
4427 case DRM_FORMAT_NV16:
4428 case DRM_FORMAT_NV61:
ba623f6a
LP
4429 case DRM_FORMAT_NV24:
4430 case DRM_FORMAT_NV42:
5a86bd55
VS
4431 return plane ? 2 : 1;
4432 case DRM_FORMAT_YUV410:
4433 case DRM_FORMAT_YVU410:
4434 case DRM_FORMAT_YUV411:
4435 case DRM_FORMAT_YVU411:
4436 case DRM_FORMAT_YUV420:
4437 case DRM_FORMAT_YVU420:
4438 case DRM_FORMAT_YUV422:
4439 case DRM_FORMAT_YVU422:
4440 case DRM_FORMAT_YUV444:
4441 case DRM_FORMAT_YVU444:
4442 return 1;
4443 default:
4444 drm_fb_get_bpp_depth(format, &depth, &bpp);
4445 return bpp >> 3;
4446 }
4447}
4448EXPORT_SYMBOL(drm_format_plane_cpp);
01b68b04
VS
4449
4450/**
4451 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4452 * @format: pixel format (DRM_FORMAT_*)
4453 *
c8e32cc1 4454 * Returns:
01b68b04
VS
4455 * The horizontal chroma subsampling factor for the
4456 * specified pixel format.
4457 */
4458int drm_format_horz_chroma_subsampling(uint32_t format)
4459{
4460 switch (format) {
4461 case DRM_FORMAT_YUV411:
4462 case DRM_FORMAT_YVU411:
4463 case DRM_FORMAT_YUV410:
4464 case DRM_FORMAT_YVU410:
4465 return 4;
4466 case DRM_FORMAT_YUYV:
4467 case DRM_FORMAT_YVYU:
4468 case DRM_FORMAT_UYVY:
4469 case DRM_FORMAT_VYUY:
4470 case DRM_FORMAT_NV12:
4471 case DRM_FORMAT_NV21:
4472 case DRM_FORMAT_NV16:
4473 case DRM_FORMAT_NV61:
4474 case DRM_FORMAT_YUV422:
4475 case DRM_FORMAT_YVU422:
4476 case DRM_FORMAT_YUV420:
4477 case DRM_FORMAT_YVU420:
4478 return 2;
4479 default:
4480 return 1;
4481 }
4482}
4483EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4484
4485/**
4486 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4487 * @format: pixel format (DRM_FORMAT_*)
4488 *
c8e32cc1 4489 * Returns:
01b68b04
VS
4490 * The vertical chroma subsampling factor for the
4491 * specified pixel format.
4492 */
4493int drm_format_vert_chroma_subsampling(uint32_t format)
4494{
4495 switch (format) {
4496 case DRM_FORMAT_YUV410:
4497 case DRM_FORMAT_YVU410:
4498 return 4;
4499 case DRM_FORMAT_YUV420:
4500 case DRM_FORMAT_YVU420:
4501 case DRM_FORMAT_NV12:
4502 case DRM_FORMAT_NV21:
4503 return 2;
4504 default:
4505 return 1;
4506 }
4507}
4508EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
87d24fc3
LP
4509
4510/**
4511 * drm_mode_config_init - initialize DRM mode_configuration structure
4512 * @dev: DRM device
4513 *
4514 * Initialize @dev's mode_config structure, used for tracking the graphics
4515 * configuration of @dev.
4516 *
4517 * Since this initializes the modeset locks, no locking is possible. Which is no
4518 * problem, since this should happen single threaded at init time. It is the
4519 * driver's problem to ensure this guarantee.
4520 *
4521 */
4522void drm_mode_config_init(struct drm_device *dev)
4523{
4524 mutex_init(&dev->mode_config.mutex);
4525 mutex_init(&dev->mode_config.idr_mutex);
4526 mutex_init(&dev->mode_config.fb_lock);
4527 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4528 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4529 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3b336ec4 4530 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
87d24fc3
LP
4531 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4532 INIT_LIST_HEAD(&dev->mode_config.property_list);
4533 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4534 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4535 idr_init(&dev->mode_config.crtc_idr);
4536
4537 drm_modeset_lock_all(dev);
4538 drm_mode_create_standard_connector_properties(dev);
4539 drm_modeset_unlock_all(dev);
4540
4541 /* Just to be sure */
4542 dev->mode_config.num_fb = 0;
4543 dev->mode_config.num_connector = 0;
4544 dev->mode_config.num_crtc = 0;
4545 dev->mode_config.num_encoder = 0;
e27dde3e
MR
4546 dev->mode_config.num_overlay_plane = 0;
4547 dev->mode_config.num_total_plane = 0;
87d24fc3
LP
4548}
4549EXPORT_SYMBOL(drm_mode_config_init);
4550
4551/**
4552 * drm_mode_config_cleanup - free up DRM mode_config info
4553 * @dev: DRM device
4554 *
4555 * Free up all the connectors and CRTCs associated with this DRM device, then
4556 * free up the framebuffers and associated buffer objects.
4557 *
4558 * Note that since this /should/ happen single-threaded at driver/device
4559 * teardown time, no locking is required. It's the driver's job to ensure that
4560 * this guarantee actually holds true.
4561 *
4562 * FIXME: cleanup any dangling user buffer objects too
4563 */
4564void drm_mode_config_cleanup(struct drm_device *dev)
4565{
4566 struct drm_connector *connector, *ot;
4567 struct drm_crtc *crtc, *ct;
4568 struct drm_encoder *encoder, *enct;
3b336ec4 4569 struct drm_bridge *bridge, *brt;
87d24fc3
LP
4570 struct drm_framebuffer *fb, *fbt;
4571 struct drm_property *property, *pt;
4572 struct drm_property_blob *blob, *bt;
4573 struct drm_plane *plane, *plt;
4574
4575 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4576 head) {
4577 encoder->funcs->destroy(encoder);
4578 }
4579
3b336ec4
SP
4580 list_for_each_entry_safe(bridge, brt,
4581 &dev->mode_config.bridge_list, head) {
4582 bridge->funcs->destroy(bridge);
4583 }
4584
87d24fc3
LP
4585 list_for_each_entry_safe(connector, ot,
4586 &dev->mode_config.connector_list, head) {
4587 connector->funcs->destroy(connector);
4588 }
4589
4590 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4591 head) {
4592 drm_property_destroy(dev, property);
4593 }
4594
4595 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4596 head) {
4597 drm_property_destroy_blob(dev, blob);
4598 }
4599
4600 /*
4601 * Single-threaded teardown context, so it's not required to grab the
4602 * fb_lock to protect against concurrent fb_list access. Contrary, it
4603 * would actually deadlock with the drm_framebuffer_cleanup function.
4604 *
4605 * Also, if there are any framebuffers left, that's a driver leak now,
4606 * so politely WARN about this.
4607 */
4608 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4609 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4610 drm_framebuffer_remove(fb);
4611 }
4612
4613 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4614 head) {
4615 plane->funcs->destroy(plane);
4616 }
4617
4618 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4619 crtc->funcs->destroy(crtc);
4620 }
4621
4622 idr_destroy(&dev->mode_config.crtc_idr);
4623}
4624EXPORT_SYMBOL(drm_mode_config_cleanup);
This page took 0.586111 seconds and 5 git commands to generate.