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