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