drm/gma500: Check connector status before restoring sdvo
[deliverable/linux.git] / drivers / gpu / drm / drm_crtc.c
CommitLineData
f453ba04
DA
1/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
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
SH
1275 idr_remove_all(&dev->mode_config.crtc_idr);
1276 idr_destroy(&dev->mode_config.crtc_idr);
f453ba04
DA
1277}
1278EXPORT_SYMBOL(drm_mode_config_cleanup);
1279
f453ba04
DA
1280/**
1281 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1282 * @out: drm_mode_modeinfo struct to return to the user
1283 * @in: drm_display_mode to use
1284 *
f453ba04
DA
1285 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1286 * the user.
1287 */
93bbf6db
VS
1288static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1289 const struct drm_display_mode *in)
f453ba04 1290{
e36fae38
VS
1291 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1292 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1293 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1294 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1295 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1296 "timing values too large for mode info\n");
1297
f453ba04
DA
1298 out->clock = in->clock;
1299 out->hdisplay = in->hdisplay;
1300 out->hsync_start = in->hsync_start;
1301 out->hsync_end = in->hsync_end;
1302 out->htotal = in->htotal;
1303 out->hskew = in->hskew;
1304 out->vdisplay = in->vdisplay;
1305 out->vsync_start = in->vsync_start;
1306 out->vsync_end = in->vsync_end;
1307 out->vtotal = in->vtotal;
1308 out->vscan = in->vscan;
1309 out->vrefresh = in->vrefresh;
1310 out->flags = in->flags;
1311 out->type = in->type;
1312 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1313 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1314}
1315
1316/**
1317 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1318 * @out: drm_display_mode to return to the user
1319 * @in: drm_mode_modeinfo to use
1320 *
f453ba04
DA
1321 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1322 * the caller.
90367bf6
VS
1323 *
1324 * RETURNS:
1325 * Zero on success, errno on failure.
f453ba04 1326 */
93bbf6db
VS
1327static int drm_crtc_convert_umode(struct drm_display_mode *out,
1328 const struct drm_mode_modeinfo *in)
f453ba04 1329{
90367bf6
VS
1330 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1331 return -ERANGE;
1332
f453ba04
DA
1333 out->clock = in->clock;
1334 out->hdisplay = in->hdisplay;
1335 out->hsync_start = in->hsync_start;
1336 out->hsync_end = in->hsync_end;
1337 out->htotal = in->htotal;
1338 out->hskew = in->hskew;
1339 out->vdisplay = in->vdisplay;
1340 out->vsync_start = in->vsync_start;
1341 out->vsync_end = in->vsync_end;
1342 out->vtotal = in->vtotal;
1343 out->vscan = in->vscan;
1344 out->vrefresh = in->vrefresh;
1345 out->flags = in->flags;
1346 out->type = in->type;
1347 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1348 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
90367bf6
VS
1349
1350 return 0;
f453ba04
DA
1351}
1352
1353/**
1354 * drm_mode_getresources - get graphics configuration
065a50ed
DV
1355 * @dev: drm device for the ioctl
1356 * @data: data pointer for the ioctl
1357 * @file_priv: drm file for the ioctl call
f453ba04 1358 *
f453ba04
DA
1359 * Construct a set of configuration description structures and return
1360 * them to the user, including CRTC, connector and framebuffer configuration.
1361 *
1362 * Called by the user via ioctl.
1363 *
1364 * RETURNS:
1365 * Zero on success, errno on failure.
1366 */
1367int drm_mode_getresources(struct drm_device *dev, void *data,
1368 struct drm_file *file_priv)
1369{
1370 struct drm_mode_card_res *card_res = data;
1371 struct list_head *lh;
1372 struct drm_framebuffer *fb;
1373 struct drm_connector *connector;
1374 struct drm_crtc *crtc;
1375 struct drm_encoder *encoder;
1376 int ret = 0;
1377 int connector_count = 0;
1378 int crtc_count = 0;
1379 int fb_count = 0;
1380 int encoder_count = 0;
1381 int copied = 0, i;
1382 uint32_t __user *fb_id;
1383 uint32_t __user *crtc_id;
1384 uint32_t __user *connector_id;
1385 uint32_t __user *encoder_id;
1386 struct drm_mode_group *mode_group;
1387
fb3b06c8
DA
1388 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1389 return -EINVAL;
1390
f453ba04 1391
4b096ac1 1392 mutex_lock(&file_priv->fbs_lock);
f453ba04
DA
1393 /*
1394 * For the non-control nodes we need to limit the list of resources
1395 * by IDs in the group list for this node
1396 */
1397 list_for_each(lh, &file_priv->fbs)
1398 fb_count++;
1399
4b096ac1
DV
1400 /* handle this in 4 parts */
1401 /* FBs */
1402 if (card_res->count_fbs >= fb_count) {
1403 copied = 0;
1404 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1405 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1406 if (put_user(fb->base.id, fb_id + copied)) {
1407 mutex_unlock(&file_priv->fbs_lock);
1408 return -EFAULT;
1409 }
1410 copied++;
1411 }
1412 }
1413 card_res->count_fbs = fb_count;
1414 mutex_unlock(&file_priv->fbs_lock);
1415
1416 drm_modeset_lock_all(dev);
f453ba04
DA
1417 mode_group = &file_priv->master->minor->mode_group;
1418 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1419
1420 list_for_each(lh, &dev->mode_config.crtc_list)
1421 crtc_count++;
1422
1423 list_for_each(lh, &dev->mode_config.connector_list)
1424 connector_count++;
1425
1426 list_for_each(lh, &dev->mode_config.encoder_list)
1427 encoder_count++;
1428 } else {
1429
1430 crtc_count = mode_group->num_crtcs;
1431 connector_count = mode_group->num_connectors;
1432 encoder_count = mode_group->num_encoders;
1433 }
1434
1435 card_res->max_height = dev->mode_config.max_height;
1436 card_res->min_height = dev->mode_config.min_height;
1437 card_res->max_width = dev->mode_config.max_width;
1438 card_res->min_width = dev->mode_config.min_width;
1439
f453ba04
DA
1440 /* CRTCs */
1441 if (card_res->count_crtcs >= crtc_count) {
1442 copied = 0;
1443 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1444 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1445 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1446 head) {
9440106b 1447 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
f453ba04
DA
1448 if (put_user(crtc->base.id, crtc_id + copied)) {
1449 ret = -EFAULT;
1450 goto out;
1451 }
1452 copied++;
1453 }
1454 } else {
1455 for (i = 0; i < mode_group->num_crtcs; i++) {
1456 if (put_user(mode_group->id_list[i],
1457 crtc_id + copied)) {
1458 ret = -EFAULT;
1459 goto out;
1460 }
1461 copied++;
1462 }
1463 }
1464 }
1465 card_res->count_crtcs = crtc_count;
1466
1467 /* Encoders */
1468 if (card_res->count_encoders >= encoder_count) {
1469 copied = 0;
1470 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1471 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1472 list_for_each_entry(encoder,
1473 &dev->mode_config.encoder_list,
1474 head) {
9440106b
JG
1475 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1476 drm_get_encoder_name(encoder));
f453ba04
DA
1477 if (put_user(encoder->base.id, encoder_id +
1478 copied)) {
1479 ret = -EFAULT;
1480 goto out;
1481 }
1482 copied++;
1483 }
1484 } else {
1485 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1486 if (put_user(mode_group->id_list[i],
1487 encoder_id + copied)) {
1488 ret = -EFAULT;
1489 goto out;
1490 }
1491 copied++;
1492 }
1493
1494 }
1495 }
1496 card_res->count_encoders = encoder_count;
1497
1498 /* Connectors */
1499 if (card_res->count_connectors >= connector_count) {
1500 copied = 0;
1501 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1502 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1503 list_for_each_entry(connector,
1504 &dev->mode_config.connector_list,
1505 head) {
9440106b
JG
1506 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1507 connector->base.id,
1508 drm_get_connector_name(connector));
f453ba04
DA
1509 if (put_user(connector->base.id,
1510 connector_id + copied)) {
1511 ret = -EFAULT;
1512 goto out;
1513 }
1514 copied++;
1515 }
1516 } else {
1517 int start = mode_group->num_crtcs +
1518 mode_group->num_encoders;
1519 for (i = start; i < start + mode_group->num_connectors; i++) {
1520 if (put_user(mode_group->id_list[i],
1521 connector_id + copied)) {
1522 ret = -EFAULT;
1523 goto out;
1524 }
1525 copied++;
1526 }
1527 }
1528 }
1529 card_res->count_connectors = connector_count;
1530
9440106b 1531 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
f453ba04
DA
1532 card_res->count_connectors, card_res->count_encoders);
1533
1534out:
84849903 1535 drm_modeset_unlock_all(dev);
f453ba04
DA
1536 return ret;
1537}
1538
1539/**
1540 * drm_mode_getcrtc - get CRTC configuration
065a50ed
DV
1541 * @dev: drm device for the ioctl
1542 * @data: data pointer for the ioctl
1543 * @file_priv: drm file for the ioctl call
f453ba04 1544 *
f453ba04
DA
1545 * Construct a CRTC configuration structure to return to the user.
1546 *
1547 * Called by the user via ioctl.
1548 *
1549 * RETURNS:
1550 * Zero on success, errno on failure.
1551 */
1552int drm_mode_getcrtc(struct drm_device *dev,
1553 void *data, struct drm_file *file_priv)
1554{
1555 struct drm_mode_crtc *crtc_resp = data;
1556 struct drm_crtc *crtc;
1557 struct drm_mode_object *obj;
1558 int ret = 0;
1559
fb3b06c8
DA
1560 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1561 return -EINVAL;
1562
84849903 1563 drm_modeset_lock_all(dev);
f453ba04
DA
1564
1565 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1566 DRM_MODE_OBJECT_CRTC);
1567 if (!obj) {
1568 ret = -EINVAL;
1569 goto out;
1570 }
1571 crtc = obj_to_crtc(obj);
1572
1573 crtc_resp->x = crtc->x;
1574 crtc_resp->y = crtc->y;
1575 crtc_resp->gamma_size = crtc->gamma_size;
1576 if (crtc->fb)
1577 crtc_resp->fb_id = crtc->fb->base.id;
1578 else
1579 crtc_resp->fb_id = 0;
1580
1581 if (crtc->enabled) {
1582
1583 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1584 crtc_resp->mode_valid = 1;
1585
1586 } else {
1587 crtc_resp->mode_valid = 0;
1588 }
1589
1590out:
84849903 1591 drm_modeset_unlock_all(dev);
f453ba04
DA
1592 return ret;
1593}
1594
1595/**
1596 * drm_mode_getconnector - get connector configuration
065a50ed
DV
1597 * @dev: drm device for the ioctl
1598 * @data: data pointer for the ioctl
1599 * @file_priv: drm file for the ioctl call
f453ba04 1600 *
f453ba04
DA
1601 * Construct a connector configuration structure to return to the user.
1602 *
1603 * Called by the user via ioctl.
1604 *
1605 * RETURNS:
1606 * Zero on success, errno on failure.
1607 */
1608int drm_mode_getconnector(struct drm_device *dev, void *data,
1609 struct drm_file *file_priv)
1610{
1611 struct drm_mode_get_connector *out_resp = data;
1612 struct drm_mode_object *obj;
1613 struct drm_connector *connector;
1614 struct drm_display_mode *mode;
1615 int mode_count = 0;
1616 int props_count = 0;
1617 int encoders_count = 0;
1618 int ret = 0;
1619 int copied = 0;
1620 int i;
1621 struct drm_mode_modeinfo u_mode;
1622 struct drm_mode_modeinfo __user *mode_ptr;
1623 uint32_t __user *prop_ptr;
1624 uint64_t __user *prop_values;
1625 uint32_t __user *encoder_ptr;
1626
fb3b06c8
DA
1627 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1628 return -EINVAL;
1629
f453ba04
DA
1630 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1631
9440106b 1632 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
f453ba04 1633
7b24056b 1634 mutex_lock(&dev->mode_config.mutex);
f453ba04
DA
1635
1636 obj = drm_mode_object_find(dev, out_resp->connector_id,
1637 DRM_MODE_OBJECT_CONNECTOR);
1638 if (!obj) {
1639 ret = -EINVAL;
1640 goto out;
1641 }
1642 connector = obj_to_connector(obj);
1643
7f88a9be 1644 props_count = connector->properties.count;
f453ba04
DA
1645
1646 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1647 if (connector->encoder_ids[i] != 0) {
1648 encoders_count++;
1649 }
1650 }
1651
1652 if (out_resp->count_modes == 0) {
1653 connector->funcs->fill_modes(connector,
1654 dev->mode_config.max_width,
1655 dev->mode_config.max_height);
1656 }
1657
1658 /* delayed so we get modes regardless of pre-fill_modes state */
1659 list_for_each_entry(mode, &connector->modes, head)
1660 mode_count++;
1661
1662 out_resp->connector_id = connector->base.id;
1663 out_resp->connector_type = connector->connector_type;
1664 out_resp->connector_type_id = connector->connector_type_id;
1665 out_resp->mm_width = connector->display_info.width_mm;
1666 out_resp->mm_height = connector->display_info.height_mm;
1667 out_resp->subpixel = connector->display_info.subpixel_order;
1668 out_resp->connection = connector->status;
1669 if (connector->encoder)
1670 out_resp->encoder_id = connector->encoder->base.id;
1671 else
1672 out_resp->encoder_id = 0;
1673
1674 /*
1675 * This ioctl is called twice, once to determine how much space is
1676 * needed, and the 2nd time to fill it.
1677 */
1678 if ((out_resp->count_modes >= mode_count) && mode_count) {
1679 copied = 0;
81f6c7f8 1680 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
f453ba04
DA
1681 list_for_each_entry(mode, &connector->modes, head) {
1682 drm_crtc_convert_to_umode(&u_mode, mode);
1683 if (copy_to_user(mode_ptr + copied,
1684 &u_mode, sizeof(u_mode))) {
1685 ret = -EFAULT;
1686 goto out;
1687 }
1688 copied++;
1689 }
1690 }
1691 out_resp->count_modes = mode_count;
1692
1693 if ((out_resp->count_props >= props_count) && props_count) {
1694 copied = 0;
81f6c7f8
VS
1695 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1696 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
7f88a9be
PZ
1697 for (i = 0; i < connector->properties.count; i++) {
1698 if (put_user(connector->properties.ids[i],
1699 prop_ptr + copied)) {
1700 ret = -EFAULT;
1701 goto out;
1702 }
f453ba04 1703
7f88a9be
PZ
1704 if (put_user(connector->properties.values[i],
1705 prop_values + copied)) {
1706 ret = -EFAULT;
1707 goto out;
f453ba04 1708 }
7f88a9be 1709 copied++;
f453ba04
DA
1710 }
1711 }
1712 out_resp->count_props = props_count;
1713
1714 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1715 copied = 0;
81f6c7f8 1716 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
f453ba04
DA
1717 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1718 if (connector->encoder_ids[i] != 0) {
1719 if (put_user(connector->encoder_ids[i],
1720 encoder_ptr + copied)) {
1721 ret = -EFAULT;
1722 goto out;
1723 }
1724 copied++;
1725 }
1726 }
1727 }
1728 out_resp->count_encoders = encoders_count;
1729
1730out:
7b24056b
DV
1731 mutex_unlock(&dev->mode_config.mutex);
1732
f453ba04
DA
1733 return ret;
1734}
1735
1736int drm_mode_getencoder(struct drm_device *dev, void *data,
1737 struct drm_file *file_priv)
1738{
1739 struct drm_mode_get_encoder *enc_resp = data;
1740 struct drm_mode_object *obj;
1741 struct drm_encoder *encoder;
1742 int ret = 0;
1743
fb3b06c8
DA
1744 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1745 return -EINVAL;
1746
84849903 1747 drm_modeset_lock_all(dev);
f453ba04
DA
1748 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1749 DRM_MODE_OBJECT_ENCODER);
1750 if (!obj) {
1751 ret = -EINVAL;
1752 goto out;
1753 }
1754 encoder = obj_to_encoder(obj);
1755
1756 if (encoder->crtc)
1757 enc_resp->crtc_id = encoder->crtc->base.id;
1758 else
1759 enc_resp->crtc_id = 0;
1760 enc_resp->encoder_type = encoder->encoder_type;
1761 enc_resp->encoder_id = encoder->base.id;
1762 enc_resp->possible_crtcs = encoder->possible_crtcs;
1763 enc_resp->possible_clones = encoder->possible_clones;
1764
1765out:
84849903 1766 drm_modeset_unlock_all(dev);
f453ba04
DA
1767 return ret;
1768}
1769
8cf5c917
JB
1770/**
1771 * drm_mode_getplane_res - get plane info
1772 * @dev: DRM device
1773 * @data: ioctl data
1774 * @file_priv: DRM file info
1775 *
1776 * Return an plane count and set of IDs.
1777 */
1778int drm_mode_getplane_res(struct drm_device *dev, void *data,
1779 struct drm_file *file_priv)
1780{
1781 struct drm_mode_get_plane_res *plane_resp = data;
1782 struct drm_mode_config *config;
1783 struct drm_plane *plane;
1784 uint32_t __user *plane_ptr;
1785 int copied = 0, ret = 0;
1786
1787 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1788 return -EINVAL;
1789
84849903 1790 drm_modeset_lock_all(dev);
8cf5c917
JB
1791 config = &dev->mode_config;
1792
1793 /*
1794 * This ioctl is called twice, once to determine how much space is
1795 * needed, and the 2nd time to fill it.
1796 */
1797 if (config->num_plane &&
1798 (plane_resp->count_planes >= config->num_plane)) {
81f6c7f8 1799 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
8cf5c917
JB
1800
1801 list_for_each_entry(plane, &config->plane_list, head) {
1802 if (put_user(plane->base.id, plane_ptr + copied)) {
1803 ret = -EFAULT;
1804 goto out;
1805 }
1806 copied++;
1807 }
1808 }
1809 plane_resp->count_planes = config->num_plane;
1810
1811out:
84849903 1812 drm_modeset_unlock_all(dev);
8cf5c917
JB
1813 return ret;
1814}
1815
1816/**
1817 * drm_mode_getplane - get plane info
1818 * @dev: DRM device
1819 * @data: ioctl data
1820 * @file_priv: DRM file info
1821 *
1822 * Return plane info, including formats supported, gamma size, any
1823 * current fb, etc.
1824 */
1825int drm_mode_getplane(struct drm_device *dev, void *data,
1826 struct drm_file *file_priv)
1827{
1828 struct drm_mode_get_plane *plane_resp = data;
1829 struct drm_mode_object *obj;
1830 struct drm_plane *plane;
1831 uint32_t __user *format_ptr;
1832 int ret = 0;
1833
1834 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1835 return -EINVAL;
1836
84849903 1837 drm_modeset_lock_all(dev);
8cf5c917
JB
1838 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1839 DRM_MODE_OBJECT_PLANE);
1840 if (!obj) {
1841 ret = -ENOENT;
1842 goto out;
1843 }
1844 plane = obj_to_plane(obj);
1845
1846 if (plane->crtc)
1847 plane_resp->crtc_id = plane->crtc->base.id;
1848 else
1849 plane_resp->crtc_id = 0;
1850
1851 if (plane->fb)
1852 plane_resp->fb_id = plane->fb->base.id;
1853 else
1854 plane_resp->fb_id = 0;
1855
1856 plane_resp->plane_id = plane->base.id;
1857 plane_resp->possible_crtcs = plane->possible_crtcs;
1858 plane_resp->gamma_size = plane->gamma_size;
1859
1860 /*
1861 * This ioctl is called twice, once to determine how much space is
1862 * needed, and the 2nd time to fill it.
1863 */
1864 if (plane->format_count &&
1865 (plane_resp->count_format_types >= plane->format_count)) {
81f6c7f8 1866 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
8cf5c917
JB
1867 if (copy_to_user(format_ptr,
1868 plane->format_types,
1869 sizeof(uint32_t) * plane->format_count)) {
1870 ret = -EFAULT;
1871 goto out;
1872 }
1873 }
1874 plane_resp->count_format_types = plane->format_count;
1875
1876out:
84849903 1877 drm_modeset_unlock_all(dev);
8cf5c917
JB
1878 return ret;
1879}
1880
1881/**
1882 * drm_mode_setplane - set up or tear down an plane
1883 * @dev: DRM device
1884 * @data: ioctl data*
065a50ed 1885 * @file_priv: DRM file info
8cf5c917
JB
1886 *
1887 * Set plane info, including placement, fb, scaling, and other factors.
1888 * Or pass a NULL fb to disable.
1889 */
1890int drm_mode_setplane(struct drm_device *dev, void *data,
1891 struct drm_file *file_priv)
1892{
1893 struct drm_mode_set_plane *plane_req = data;
1894 struct drm_mode_object *obj;
1895 struct drm_plane *plane;
1896 struct drm_crtc *crtc;
6c2a7532 1897 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
8cf5c917 1898 int ret = 0;
42ef8789 1899 unsigned int fb_width, fb_height;
62443be6 1900 int i;
8cf5c917
JB
1901
1902 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1903 return -EINVAL;
1904
8cf5c917
JB
1905 /*
1906 * First, find the plane, crtc, and fb objects. If not available,
1907 * we don't bother to call the driver.
1908 */
1909 obj = drm_mode_object_find(dev, plane_req->plane_id,
1910 DRM_MODE_OBJECT_PLANE);
1911 if (!obj) {
1912 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1913 plane_req->plane_id);
6c2a7532 1914 return -ENOENT;
8cf5c917
JB
1915 }
1916 plane = obj_to_plane(obj);
1917
1918 /* No fb means shut it down */
1919 if (!plane_req->fb_id) {
6c2a7532
DV
1920 drm_modeset_lock_all(dev);
1921 old_fb = plane->fb;
8cf5c917 1922 plane->funcs->disable_plane(plane);
e5e3b44c
VS
1923 plane->crtc = NULL;
1924 plane->fb = NULL;
6c2a7532 1925 drm_modeset_unlock_all(dev);
8cf5c917
JB
1926 goto out;
1927 }
1928
1929 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1930 DRM_MODE_OBJECT_CRTC);
1931 if (!obj) {
1932 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1933 plane_req->crtc_id);
1934 ret = -ENOENT;
1935 goto out;
1936 }
1937 crtc = obj_to_crtc(obj);
1938
786b99ed
DV
1939 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1940 if (!fb) {
8cf5c917
JB
1941 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1942 plane_req->fb_id);
1943 ret = -ENOENT;
1944 goto out;
1945 }
8cf5c917 1946
62443be6
VS
1947 /* Check whether this plane supports the fb pixel format. */
1948 for (i = 0; i < plane->format_count; i++)
1949 if (fb->pixel_format == plane->format_types[i])
1950 break;
1951 if (i == plane->format_count) {
1952 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1953 ret = -EINVAL;
1954 goto out;
1955 }
1956
42ef8789
VS
1957 fb_width = fb->width << 16;
1958 fb_height = fb->height << 16;
1959
1960 /* Make sure source coordinates are inside the fb. */
1961 if (plane_req->src_w > fb_width ||
1962 plane_req->src_x > fb_width - plane_req->src_w ||
1963 plane_req->src_h > fb_height ||
1964 plane_req->src_y > fb_height - plane_req->src_h) {
1965 DRM_DEBUG_KMS("Invalid source coordinates "
1966 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1967 plane_req->src_w >> 16,
1968 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1969 plane_req->src_h >> 16,
1970 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1971 plane_req->src_x >> 16,
1972 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1973 plane_req->src_y >> 16,
1974 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1975 ret = -ENOSPC;
1976 goto out;
1977 }
1978
687a0400
VS
1979 /* Give drivers some help against integer overflows */
1980 if (plane_req->crtc_w > INT_MAX ||
1981 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1982 plane_req->crtc_h > INT_MAX ||
1983 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1984 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1985 plane_req->crtc_w, plane_req->crtc_h,
1986 plane_req->crtc_x, plane_req->crtc_y);
1987 ret = -ERANGE;
1988 goto out;
1989 }
1990
6c2a7532 1991 drm_modeset_lock_all(dev);
8cf5c917
JB
1992 ret = plane->funcs->update_plane(plane, crtc, fb,
1993 plane_req->crtc_x, plane_req->crtc_y,
1994 plane_req->crtc_w, plane_req->crtc_h,
1995 plane_req->src_x, plane_req->src_y,
1996 plane_req->src_w, plane_req->src_h);
1997 if (!ret) {
6c2a7532 1998 old_fb = plane->fb;
8cf5c917
JB
1999 plane->crtc = crtc;
2000 plane->fb = fb;
35f8badc 2001 fb = NULL;
8cf5c917 2002 }
6c2a7532 2003 drm_modeset_unlock_all(dev);
8cf5c917
JB
2004
2005out:
6c2a7532
DV
2006 if (fb)
2007 drm_framebuffer_unreference(fb);
2008 if (old_fb)
2009 drm_framebuffer_unreference(old_fb);
8cf5c917
JB
2010
2011 return ret;
2012}
2013
2d13b679
DV
2014/**
2015 * drm_mode_set_config_internal - helper to call ->set_config
2016 * @set: modeset config to set
2017 *
2018 * This is a little helper to wrap internal calls to the ->set_config driver
2019 * interface. The only thing it adds is correct refcounting dance.
2020 */
2021int drm_mode_set_config_internal(struct drm_mode_set *set)
2022{
2023 struct drm_crtc *crtc = set->crtc;
b0d12325
DV
2024 struct drm_framebuffer *fb, *old_fb;
2025 int ret;
2026
2027 old_fb = crtc->fb;
2028 fb = set->fb;
2d13b679 2029
b0d12325
DV
2030 ret = crtc->funcs->set_config(set);
2031 if (ret == 0) {
2032 if (old_fb)
2033 drm_framebuffer_unreference(old_fb);
2034 if (fb)
2035 drm_framebuffer_reference(fb);
2036 }
2037
2038 return ret;
2d13b679
DV
2039}
2040EXPORT_SYMBOL(drm_mode_set_config_internal);
2041
f453ba04
DA
2042/**
2043 * drm_mode_setcrtc - set CRTC configuration
065a50ed
DV
2044 * @dev: drm device for the ioctl
2045 * @data: data pointer for the ioctl
2046 * @file_priv: drm file for the ioctl call
f453ba04 2047 *
f453ba04
DA
2048 * Build a new CRTC configuration based on user request.
2049 *
2050 * Called by the user via ioctl.
2051 *
2052 * RETURNS:
2053 * Zero on success, errno on failure.
2054 */
2055int drm_mode_setcrtc(struct drm_device *dev, void *data,
2056 struct drm_file *file_priv)
2057{
2058 struct drm_mode_config *config = &dev->mode_config;
2059 struct drm_mode_crtc *crtc_req = data;
2060 struct drm_mode_object *obj;
6653cc8d 2061 struct drm_crtc *crtc;
f453ba04
DA
2062 struct drm_connector **connector_set = NULL, *connector;
2063 struct drm_framebuffer *fb = NULL;
2064 struct drm_display_mode *mode = NULL;
2065 struct drm_mode_set set;
2066 uint32_t __user *set_connectors_ptr;
4a1b0714 2067 int ret;
f453ba04
DA
2068 int i;
2069
fb3b06c8
DA
2070 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2071 return -EINVAL;
2072
1d97e915
VS
2073 /* For some reason crtc x/y offsets are signed internally. */
2074 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2075 return -ERANGE;
2076
84849903 2077 drm_modeset_lock_all(dev);
f453ba04
DA
2078 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2079 DRM_MODE_OBJECT_CRTC);
2080 if (!obj) {
58367ed6 2081 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
f453ba04
DA
2082 ret = -EINVAL;
2083 goto out;
2084 }
2085 crtc = obj_to_crtc(obj);
9440106b 2086 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
f453ba04
DA
2087
2088 if (crtc_req->mode_valid) {
7c80e128 2089 int hdisplay, vdisplay;
f453ba04
DA
2090 /* If we have a mode we need a framebuffer. */
2091 /* If we pass -1, set the mode with the currently bound fb */
2092 if (crtc_req->fb_id == -1) {
6653cc8d
VS
2093 if (!crtc->fb) {
2094 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2095 ret = -EINVAL;
2096 goto out;
f453ba04 2097 }
6653cc8d 2098 fb = crtc->fb;
b0d12325
DV
2099 /* Make refcounting symmetric with the lookup path. */
2100 drm_framebuffer_reference(fb);
f453ba04 2101 } else {
786b99ed
DV
2102 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2103 if (!fb) {
58367ed6
ZY
2104 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2105 crtc_req->fb_id);
f453ba04
DA
2106 ret = -EINVAL;
2107 goto out;
2108 }
f453ba04
DA
2109 }
2110
2111 mode = drm_mode_create(dev);
ee34ab5b
VS
2112 if (!mode) {
2113 ret = -ENOMEM;
2114 goto out;
2115 }
2116
90367bf6
VS
2117 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2118 if (ret) {
2119 DRM_DEBUG_KMS("Invalid mode\n");
2120 goto out;
2121 }
2122
f453ba04 2123 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
5f61bb42 2124
7c80e128
RC
2125 hdisplay = mode->hdisplay;
2126 vdisplay = mode->vdisplay;
2127
2128 if (crtc->invert_dimensions)
2129 swap(hdisplay, vdisplay);
2130
2131 if (hdisplay > fb->width ||
2132 vdisplay > fb->height ||
2133 crtc_req->x > fb->width - hdisplay ||
2134 crtc_req->y > fb->height - vdisplay) {
2135 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2136 fb->width, fb->height,
2137 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2138 crtc->invert_dimensions ? " (inverted)" : "");
5f61bb42
VS
2139 ret = -ENOSPC;
2140 goto out;
2141 }
f453ba04
DA
2142 }
2143
2144 if (crtc_req->count_connectors == 0 && mode) {
58367ed6 2145 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
f453ba04
DA
2146 ret = -EINVAL;
2147 goto out;
2148 }
2149
7781de74 2150 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
58367ed6 2151 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
f453ba04
DA
2152 crtc_req->count_connectors);
2153 ret = -EINVAL;
2154 goto out;
2155 }
2156
2157 if (crtc_req->count_connectors > 0) {
2158 u32 out_id;
2159
2160 /* Avoid unbounded kernel memory allocation */
2161 if (crtc_req->count_connectors > config->num_connector) {
2162 ret = -EINVAL;
2163 goto out;
2164 }
2165
2166 connector_set = kmalloc(crtc_req->count_connectors *
2167 sizeof(struct drm_connector *),
2168 GFP_KERNEL);
2169 if (!connector_set) {
2170 ret = -ENOMEM;
2171 goto out;
2172 }
2173
2174 for (i = 0; i < crtc_req->count_connectors; i++) {
81f6c7f8 2175 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
f453ba04
DA
2176 if (get_user(out_id, &set_connectors_ptr[i])) {
2177 ret = -EFAULT;
2178 goto out;
2179 }
2180
2181 obj = drm_mode_object_find(dev, out_id,
2182 DRM_MODE_OBJECT_CONNECTOR);
2183 if (!obj) {
58367ed6
ZY
2184 DRM_DEBUG_KMS("Connector id %d unknown\n",
2185 out_id);
f453ba04
DA
2186 ret = -EINVAL;
2187 goto out;
2188 }
2189 connector = obj_to_connector(obj);
9440106b
JG
2190 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2191 connector->base.id,
2192 drm_get_connector_name(connector));
f453ba04
DA
2193
2194 connector_set[i] = connector;
2195 }
2196 }
2197
2198 set.crtc = crtc;
2199 set.x = crtc_req->x;
2200 set.y = crtc_req->y;
2201 set.mode = mode;
2202 set.connectors = connector_set;
2203 set.num_connectors = crtc_req->count_connectors;
5ef5f72f 2204 set.fb = fb;
2d13b679 2205 ret = drm_mode_set_config_internal(&set);
f453ba04
DA
2206
2207out:
b0d12325
DV
2208 if (fb)
2209 drm_framebuffer_unreference(fb);
2210
f453ba04 2211 kfree(connector_set);
ee34ab5b 2212 drm_mode_destroy(dev, mode);
84849903 2213 drm_modeset_unlock_all(dev);
f453ba04
DA
2214 return ret;
2215}
2216
2217int drm_mode_cursor_ioctl(struct drm_device *dev,
2218 void *data, struct drm_file *file_priv)
2219{
2220 struct drm_mode_cursor *req = data;
2221 struct drm_mode_object *obj;
2222 struct drm_crtc *crtc;
2223 int ret = 0;
2224
fb3b06c8
DA
2225 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2226 return -EINVAL;
2227
7c4eaca4 2228 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
f453ba04 2229 return -EINVAL;
f453ba04 2230
e0c8463a 2231 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
f453ba04 2232 if (!obj) {
58367ed6 2233 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
dac35663 2234 return -EINVAL;
f453ba04
DA
2235 }
2236 crtc = obj_to_crtc(obj);
2237
dac35663 2238 mutex_lock(&crtc->mutex);
f453ba04
DA
2239 if (req->flags & DRM_MODE_CURSOR_BO) {
2240 if (!crtc->funcs->cursor_set) {
f453ba04
DA
2241 ret = -ENXIO;
2242 goto out;
2243 }
2244 /* Turns off the cursor if handle is 0 */
2245 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2246 req->width, req->height);
2247 }
2248
2249 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2250 if (crtc->funcs->cursor_move) {
2251 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2252 } else {
f453ba04
DA
2253 ret = -EFAULT;
2254 goto out;
2255 }
2256 }
2257out:
dac35663
DV
2258 mutex_unlock(&crtc->mutex);
2259
f453ba04
DA
2260 return ret;
2261}
2262
308e5bcb
JB
2263/* Original addfb only supported RGB formats, so figure out which one */
2264uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2265{
2266 uint32_t fmt;
2267
2268 switch (bpp) {
2269 case 8:
d84f031b 2270 fmt = DRM_FORMAT_C8;
308e5bcb
JB
2271 break;
2272 case 16:
2273 if (depth == 15)
04b3924d 2274 fmt = DRM_FORMAT_XRGB1555;
308e5bcb 2275 else
04b3924d 2276 fmt = DRM_FORMAT_RGB565;
308e5bcb
JB
2277 break;
2278 case 24:
04b3924d 2279 fmt = DRM_FORMAT_RGB888;
308e5bcb
JB
2280 break;
2281 case 32:
2282 if (depth == 24)
04b3924d 2283 fmt = DRM_FORMAT_XRGB8888;
308e5bcb 2284 else if (depth == 30)
04b3924d 2285 fmt = DRM_FORMAT_XRGB2101010;
308e5bcb 2286 else
04b3924d 2287 fmt = DRM_FORMAT_ARGB8888;
308e5bcb
JB
2288 break;
2289 default:
04b3924d
VS
2290 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2291 fmt = DRM_FORMAT_XRGB8888;
308e5bcb
JB
2292 break;
2293 }
2294
2295 return fmt;
2296}
2297EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2298
f453ba04
DA
2299/**
2300 * drm_mode_addfb - add an FB to the graphics configuration
065a50ed
DV
2301 * @dev: drm device for the ioctl
2302 * @data: data pointer for the ioctl
2303 * @file_priv: drm file for the ioctl call
f453ba04 2304 *
f453ba04
DA
2305 * Add a new FB to the specified CRTC, given a user request.
2306 *
2307 * Called by the user via ioctl.
2308 *
2309 * RETURNS:
2310 * Zero on success, errno on failure.
2311 */
2312int drm_mode_addfb(struct drm_device *dev,
2313 void *data, struct drm_file *file_priv)
2314{
308e5bcb
JB
2315 struct drm_mode_fb_cmd *or = data;
2316 struct drm_mode_fb_cmd2 r = {};
2317 struct drm_mode_config *config = &dev->mode_config;
2318 struct drm_framebuffer *fb;
2319 int ret = 0;
2320
2321 /* Use new struct with format internally */
2322 r.fb_id = or->fb_id;
2323 r.width = or->width;
2324 r.height = or->height;
2325 r.pitches[0] = or->pitch;
2326 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2327 r.handles[0] = or->handle;
2328
2329 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2330 return -EINVAL;
2331
acb4b992 2332 if ((config->min_width > r.width) || (r.width > config->max_width))
308e5bcb 2333 return -EINVAL;
acb4b992
JB
2334
2335 if ((config->min_height > r.height) || (r.height > config->max_height))
308e5bcb 2336 return -EINVAL;
308e5bcb 2337
308e5bcb
JB
2338 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2339 if (IS_ERR(fb)) {
1aa1b11c 2340 DRM_DEBUG_KMS("could not create framebuffer\n");
4b096ac1
DV
2341 drm_modeset_unlock_all(dev);
2342 return PTR_ERR(fb);
308e5bcb
JB
2343 }
2344
4b096ac1 2345 mutex_lock(&file_priv->fbs_lock);
308e5bcb
JB
2346 or->fb_id = fb->base.id;
2347 list_add(&fb->filp_head, &file_priv->fbs);
2348 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
4b096ac1 2349 mutex_unlock(&file_priv->fbs_lock);
4b096ac1 2350
308e5bcb
JB
2351 return ret;
2352}
2353
cff91b62 2354static int format_check(const struct drm_mode_fb_cmd2 *r)
935b5977
VS
2355{
2356 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2357
2358 switch (format) {
2359 case DRM_FORMAT_C8:
2360 case DRM_FORMAT_RGB332:
2361 case DRM_FORMAT_BGR233:
2362 case DRM_FORMAT_XRGB4444:
2363 case DRM_FORMAT_XBGR4444:
2364 case DRM_FORMAT_RGBX4444:
2365 case DRM_FORMAT_BGRX4444:
2366 case DRM_FORMAT_ARGB4444:
2367 case DRM_FORMAT_ABGR4444:
2368 case DRM_FORMAT_RGBA4444:
2369 case DRM_FORMAT_BGRA4444:
2370 case DRM_FORMAT_XRGB1555:
2371 case DRM_FORMAT_XBGR1555:
2372 case DRM_FORMAT_RGBX5551:
2373 case DRM_FORMAT_BGRX5551:
2374 case DRM_FORMAT_ARGB1555:
2375 case DRM_FORMAT_ABGR1555:
2376 case DRM_FORMAT_RGBA5551:
2377 case DRM_FORMAT_BGRA5551:
2378 case DRM_FORMAT_RGB565:
2379 case DRM_FORMAT_BGR565:
2380 case DRM_FORMAT_RGB888:
2381 case DRM_FORMAT_BGR888:
2382 case DRM_FORMAT_XRGB8888:
2383 case DRM_FORMAT_XBGR8888:
2384 case DRM_FORMAT_RGBX8888:
2385 case DRM_FORMAT_BGRX8888:
2386 case DRM_FORMAT_ARGB8888:
2387 case DRM_FORMAT_ABGR8888:
2388 case DRM_FORMAT_RGBA8888:
2389 case DRM_FORMAT_BGRA8888:
2390 case DRM_FORMAT_XRGB2101010:
2391 case DRM_FORMAT_XBGR2101010:
2392 case DRM_FORMAT_RGBX1010102:
2393 case DRM_FORMAT_BGRX1010102:
2394 case DRM_FORMAT_ARGB2101010:
2395 case DRM_FORMAT_ABGR2101010:
2396 case DRM_FORMAT_RGBA1010102:
2397 case DRM_FORMAT_BGRA1010102:
2398 case DRM_FORMAT_YUYV:
2399 case DRM_FORMAT_YVYU:
2400 case DRM_FORMAT_UYVY:
2401 case DRM_FORMAT_VYUY:
2402 case DRM_FORMAT_AYUV:
2403 case DRM_FORMAT_NV12:
2404 case DRM_FORMAT_NV21:
2405 case DRM_FORMAT_NV16:
2406 case DRM_FORMAT_NV61:
ba623f6a
LP
2407 case DRM_FORMAT_NV24:
2408 case DRM_FORMAT_NV42:
935b5977
VS
2409 case DRM_FORMAT_YUV410:
2410 case DRM_FORMAT_YVU410:
2411 case DRM_FORMAT_YUV411:
2412 case DRM_FORMAT_YVU411:
2413 case DRM_FORMAT_YUV420:
2414 case DRM_FORMAT_YVU420:
2415 case DRM_FORMAT_YUV422:
2416 case DRM_FORMAT_YVU422:
2417 case DRM_FORMAT_YUV444:
2418 case DRM_FORMAT_YVU444:
2419 return 0;
2420 default:
2421 return -EINVAL;
2422 }
2423}
2424
cff91b62 2425static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
d1b45d5f
VS
2426{
2427 int ret, hsub, vsub, num_planes, i;
2428
2429 ret = format_check(r);
2430 if (ret) {
1aa1b11c 2431 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
d1b45d5f
VS
2432 return ret;
2433 }
2434
2435 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2436 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2437 num_planes = drm_format_num_planes(r->pixel_format);
2438
2439 if (r->width == 0 || r->width % hsub) {
1aa1b11c 2440 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
d1b45d5f
VS
2441 return -EINVAL;
2442 }
2443
2444 if (r->height == 0 || r->height % vsub) {
1aa1b11c 2445 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
d1b45d5f
VS
2446 return -EINVAL;
2447 }
2448
2449 for (i = 0; i < num_planes; i++) {
2450 unsigned int width = r->width / (i != 0 ? hsub : 1);
b180b5d1
VS
2451 unsigned int height = r->height / (i != 0 ? vsub : 1);
2452 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
d1b45d5f
VS
2453
2454 if (!r->handles[i]) {
1aa1b11c 2455 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
d1b45d5f
VS
2456 return -EINVAL;
2457 }
2458
b180b5d1
VS
2459 if ((uint64_t) width * cpp > UINT_MAX)
2460 return -ERANGE;
2461
2462 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2463 return -ERANGE;
2464
2465 if (r->pitches[i] < width * cpp) {
1aa1b11c 2466 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
d1b45d5f
VS
2467 return -EINVAL;
2468 }
2469 }
2470
2471 return 0;
2472}
2473
308e5bcb
JB
2474/**
2475 * drm_mode_addfb2 - add an FB to the graphics configuration
065a50ed
DV
2476 * @dev: drm device for the ioctl
2477 * @data: data pointer for the ioctl
2478 * @file_priv: drm file for the ioctl call
308e5bcb 2479 *
308e5bcb
JB
2480 * Add a new FB to the specified CRTC, given a user request with format.
2481 *
2482 * Called by the user via ioctl.
2483 *
2484 * RETURNS:
2485 * Zero on success, errno on failure.
2486 */
2487int drm_mode_addfb2(struct drm_device *dev,
2488 void *data, struct drm_file *file_priv)
2489{
2490 struct drm_mode_fb_cmd2 *r = data;
f453ba04
DA
2491 struct drm_mode_config *config = &dev->mode_config;
2492 struct drm_framebuffer *fb;
4a1b0714 2493 int ret;
f453ba04 2494
fb3b06c8
DA
2495 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2496 return -EINVAL;
2497
e3cc3520
VS
2498 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2499 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2500 return -EINVAL;
2501 }
2502
f453ba04 2503 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1aa1b11c 2504 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
8cf5c917 2505 r->width, config->min_width, config->max_width);
f453ba04
DA
2506 return -EINVAL;
2507 }
2508 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1aa1b11c 2509 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
8cf5c917 2510 r->height, config->min_height, config->max_height);
f453ba04
DA
2511 return -EINVAL;
2512 }
2513
d1b45d5f
VS
2514 ret = framebuffer_check(r);
2515 if (ret)
935b5977 2516 return ret;
935b5977 2517
f453ba04 2518 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
cce13ff7 2519 if (IS_ERR(fb)) {
1aa1b11c 2520 DRM_DEBUG_KMS("could not create framebuffer\n");
4b096ac1
DV
2521 drm_modeset_unlock_all(dev);
2522 return PTR_ERR(fb);
f453ba04
DA
2523 }
2524
4b096ac1 2525 mutex_lock(&file_priv->fbs_lock);
e0c8463a 2526 r->fb_id = fb->base.id;
f453ba04 2527 list_add(&fb->filp_head, &file_priv->fbs);
9440106b 2528 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
4b096ac1 2529 mutex_unlock(&file_priv->fbs_lock);
f453ba04 2530
4b096ac1 2531
f453ba04
DA
2532 return ret;
2533}
2534
2535/**
2536 * drm_mode_rmfb - remove an FB from the configuration
065a50ed
DV
2537 * @dev: drm device for the ioctl
2538 * @data: data pointer for the ioctl
2539 * @file_priv: drm file for the ioctl call
f453ba04 2540 *
f453ba04
DA
2541 * Remove the FB specified by the user.
2542 *
2543 * Called by the user via ioctl.
2544 *
2545 * RETURNS:
2546 * Zero on success, errno on failure.
2547 */
2548int drm_mode_rmfb(struct drm_device *dev,
2549 void *data, struct drm_file *file_priv)
2550{
f453ba04
DA
2551 struct drm_framebuffer *fb = NULL;
2552 struct drm_framebuffer *fbl = NULL;
2553 uint32_t *id = data;
f453ba04
DA
2554 int found = 0;
2555
fb3b06c8
DA
2556 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2557 return -EINVAL;
2558
4b096ac1 2559 mutex_lock(&file_priv->fbs_lock);
2b677e8c
DV
2560 mutex_lock(&dev->mode_config.fb_lock);
2561 fb = __drm_framebuffer_lookup(dev, *id);
2562 if (!fb)
2563 goto fail_lookup;
2564
f453ba04
DA
2565 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2566 if (fb == fbl)
2567 found = 1;
2b677e8c
DV
2568 if (!found)
2569 goto fail_lookup;
2570
2571 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2572 __drm_framebuffer_unregister(dev, fb);
f453ba04 2573
4b096ac1 2574 list_del_init(&fb->filp_head);
2b677e8c 2575 mutex_unlock(&dev->mode_config.fb_lock);
4b096ac1 2576 mutex_unlock(&file_priv->fbs_lock);
f453ba04 2577
4b096ac1 2578 drm_framebuffer_remove(fb);
4b096ac1 2579
2b677e8c
DV
2580 return 0;
2581
2582fail_lookup:
2583 mutex_unlock(&dev->mode_config.fb_lock);
2584 mutex_unlock(&file_priv->fbs_lock);
2585
2586 return -EINVAL;
f453ba04
DA
2587}
2588
2589/**
2590 * drm_mode_getfb - get FB info
065a50ed
DV
2591 * @dev: drm device for the ioctl
2592 * @data: data pointer for the ioctl
2593 * @file_priv: drm file for the ioctl call
f453ba04 2594 *
f453ba04
DA
2595 * Lookup the FB given its ID and return info about it.
2596 *
2597 * Called by the user via ioctl.
2598 *
2599 * RETURNS:
2600 * Zero on success, errno on failure.
2601 */
2602int drm_mode_getfb(struct drm_device *dev,
2603 void *data, struct drm_file *file_priv)
2604{
2605 struct drm_mode_fb_cmd *r = data;
f453ba04 2606 struct drm_framebuffer *fb;
58c0dca1 2607 int ret;
f453ba04 2608
fb3b06c8
DA
2609 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2610 return -EINVAL;
2611
786b99ed 2612 fb = drm_framebuffer_lookup(dev, r->fb_id);
58c0dca1
DV
2613 if (!fb)
2614 return -EINVAL;
f453ba04
DA
2615
2616 r->height = fb->height;
2617 r->width = fb->width;
2618 r->depth = fb->depth;
2619 r->bpp = fb->bits_per_pixel;
01f2c773 2620 r->pitch = fb->pitches[0];
af26ef3b
DV
2621 if (fb->funcs->create_handle)
2622 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2623 else
2624 ret = -ENODEV;
f453ba04 2625
58c0dca1
DV
2626 drm_framebuffer_unreference(fb);
2627
f453ba04
DA
2628 return ret;
2629}
2630
884840aa
JB
2631int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2632 void *data, struct drm_file *file_priv)
2633{
2634 struct drm_clip_rect __user *clips_ptr;
2635 struct drm_clip_rect *clips = NULL;
2636 struct drm_mode_fb_dirty_cmd *r = data;
884840aa
JB
2637 struct drm_framebuffer *fb;
2638 unsigned flags;
2639 int num_clips;
4a1b0714 2640 int ret;
884840aa 2641
fb3b06c8
DA
2642 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2643 return -EINVAL;
2644
786b99ed 2645 fb = drm_framebuffer_lookup(dev, r->fb_id);
4ccf097f
DV
2646 if (!fb)
2647 return -EINVAL;
884840aa
JB
2648
2649 num_clips = r->num_clips;
81f6c7f8 2650 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
884840aa
JB
2651
2652 if (!num_clips != !clips_ptr) {
2653 ret = -EINVAL;
2654 goto out_err1;
2655 }
2656
2657 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2658
2659 /* If userspace annotates copy, clips must come in pairs */
2660 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2661 ret = -EINVAL;
2662 goto out_err1;
2663 }
2664
2665 if (num_clips && clips_ptr) {
a5cd3351
XW
2666 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2667 ret = -EINVAL;
2668 goto out_err1;
2669 }
884840aa
JB
2670 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2671 if (!clips) {
2672 ret = -ENOMEM;
2673 goto out_err1;
2674 }
2675
2676 ret = copy_from_user(clips, clips_ptr,
2677 num_clips * sizeof(*clips));
e902a358
DC
2678 if (ret) {
2679 ret = -EFAULT;
884840aa 2680 goto out_err2;
e902a358 2681 }
884840aa
JB
2682 }
2683
2684 if (fb->funcs->dirty) {
4ccf097f 2685 drm_modeset_lock_all(dev);
02b00162
TH
2686 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2687 clips, num_clips);
4ccf097f 2688 drm_modeset_unlock_all(dev);
884840aa
JB
2689 } else {
2690 ret = -ENOSYS;
884840aa
JB
2691 }
2692
2693out_err2:
2694 kfree(clips);
2695out_err1:
4ccf097f
DV
2696 drm_framebuffer_unreference(fb);
2697
884840aa
JB
2698 return ret;
2699}
2700
2701
f453ba04
DA
2702/**
2703 * drm_fb_release - remove and free the FBs on this file
065a50ed 2704 * @priv: drm file for the ioctl
f453ba04 2705 *
f453ba04
DA
2706 * Destroy all the FBs associated with @filp.
2707 *
2708 * Called by the user via ioctl.
2709 *
2710 * RETURNS:
2711 * Zero on success, errno on failure.
2712 */
ea39f835 2713void drm_fb_release(struct drm_file *priv)
f453ba04 2714{
f453ba04
DA
2715 struct drm_device *dev = priv->minor->dev;
2716 struct drm_framebuffer *fb, *tfb;
2717
4b096ac1 2718 mutex_lock(&priv->fbs_lock);
f453ba04 2719 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2b677e8c
DV
2720
2721 mutex_lock(&dev->mode_config.fb_lock);
2722 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2723 __drm_framebuffer_unregister(dev, fb);
2724 mutex_unlock(&dev->mode_config.fb_lock);
2725
4b096ac1 2726 list_del_init(&fb->filp_head);
2b677e8c
DV
2727
2728 /* This will also drop the fpriv->fbs reference. */
f7eff60e 2729 drm_framebuffer_remove(fb);
f453ba04 2730 }
4b096ac1 2731 mutex_unlock(&priv->fbs_lock);
f453ba04
DA
2732}
2733
2734/**
2735 * drm_mode_attachmode - add a mode to the user mode list
2736 * @dev: DRM device
2737 * @connector: connector to add the mode to
2738 * @mode: mode to add
2739 *
2740 * Add @mode to @connector's user mode list.
2741 */
1dd6c8bd
VS
2742static void drm_mode_attachmode(struct drm_device *dev,
2743 struct drm_connector *connector,
2744 struct drm_display_mode *mode)
f453ba04 2745{
f453ba04 2746 list_add_tail(&mode->head, &connector->user_modes);
f453ba04
DA
2747}
2748
2749int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
ac235daf 2750 const struct drm_display_mode *mode)
f453ba04
DA
2751{
2752 struct drm_connector *connector;
ac235daf
VS
2753 int ret = 0;
2754 struct drm_display_mode *dup_mode, *next;
2755 LIST_HEAD(list);
2756
f453ba04
DA
2757 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2758 if (!connector->encoder)
ac235daf 2759 continue;
f453ba04 2760 if (connector->encoder->crtc == crtc) {
ac235daf
VS
2761 dup_mode = drm_mode_duplicate(dev, mode);
2762 if (!dup_mode) {
2763 ret = -ENOMEM;
2764 goto out;
2765 }
2766 list_add_tail(&dup_mode->head, &list);
f453ba04
DA
2767 }
2768 }
ac235daf
VS
2769
2770 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2771 if (!connector->encoder)
2772 continue;
2773 if (connector->encoder->crtc == crtc)
2774 list_move_tail(list.next, &connector->user_modes);
2775 }
2776
2777 WARN_ON(!list_empty(&list));
2778
2779 out:
2780 list_for_each_entry_safe(dup_mode, next, &list, head)
2781 drm_mode_destroy(dev, dup_mode);
2782
2783 return ret;
f453ba04
DA
2784}
2785EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2786
2787static int drm_mode_detachmode(struct drm_device *dev,
2788 struct drm_connector *connector,
2789 struct drm_display_mode *mode)
2790{
2791 int found = 0;
2792 int ret = 0;
2793 struct drm_display_mode *match_mode, *t;
2794
2795 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2796 if (drm_mode_equal(match_mode, mode)) {
2797 list_del(&match_mode->head);
2798 drm_mode_destroy(dev, match_mode);
2799 found = 1;
2800 break;
2801 }
2802 }
2803
2804 if (!found)
2805 ret = -EINVAL;
2806
2807 return ret;
2808}
2809
2810int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2811{
2812 struct drm_connector *connector;
2813
2814 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2815 drm_mode_detachmode(dev, connector, mode);
2816 }
2817 return 0;
2818}
2819EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2820
2821/**
2822 * drm_fb_attachmode - Attach a user mode to an connector
065a50ed
DV
2823 * @dev: drm device for the ioctl
2824 * @data: data pointer for the ioctl
2825 * @file_priv: drm file for the ioctl call
f453ba04
DA
2826 *
2827 * This attaches a user specified mode to an connector.
2828 * Called by the user via ioctl.
2829 *
2830 * RETURNS:
2831 * Zero on success, errno on failure.
2832 */
2833int drm_mode_attachmode_ioctl(struct drm_device *dev,
2834 void *data, struct drm_file *file_priv)
2835{
2836 struct drm_mode_mode_cmd *mode_cmd = data;
2837 struct drm_connector *connector;
2838 struct drm_display_mode *mode;
2839 struct drm_mode_object *obj;
2840 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
4a1b0714 2841 int ret;
f453ba04 2842
fb3b06c8
DA
2843 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2844 return -EINVAL;
2845
84849903 2846 drm_modeset_lock_all(dev);
f453ba04
DA
2847
2848 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2849 if (!obj) {
2850 ret = -EINVAL;
2851 goto out;
2852 }
2853 connector = obj_to_connector(obj);
2854
2855 mode = drm_mode_create(dev);
2856 if (!mode) {
2857 ret = -ENOMEM;
2858 goto out;
2859 }
2860
90367bf6
VS
2861 ret = drm_crtc_convert_umode(mode, umode);
2862 if (ret) {
2863 DRM_DEBUG_KMS("Invalid mode\n");
2864 drm_mode_destroy(dev, mode);
2865 goto out;
2866 }
f453ba04 2867
1dd6c8bd 2868 drm_mode_attachmode(dev, connector, mode);
f453ba04 2869out:
84849903 2870 drm_modeset_unlock_all(dev);
f453ba04
DA
2871 return ret;
2872}
2873
2874
2875/**
2876 * drm_fb_detachmode - Detach a user specified mode from an connector
065a50ed
DV
2877 * @dev: drm device for the ioctl
2878 * @data: data pointer for the ioctl
2879 * @file_priv: drm file for the ioctl call
f453ba04
DA
2880 *
2881 * Called by the user via ioctl.
2882 *
2883 * RETURNS:
2884 * Zero on success, errno on failure.
2885 */
2886int drm_mode_detachmode_ioctl(struct drm_device *dev,
2887 void *data, struct drm_file *file_priv)
2888{
2889 struct drm_mode_object *obj;
2890 struct drm_mode_mode_cmd *mode_cmd = data;
2891 struct drm_connector *connector;
2892 struct drm_display_mode mode;
2893 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
4a1b0714 2894 int ret;
f453ba04 2895
fb3b06c8
DA
2896 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2897 return -EINVAL;
2898
84849903 2899 drm_modeset_lock_all(dev);
f453ba04
DA
2900
2901 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2902 if (!obj) {
2903 ret = -EINVAL;
2904 goto out;
2905 }
2906 connector = obj_to_connector(obj);
2907
90367bf6
VS
2908 ret = drm_crtc_convert_umode(&mode, umode);
2909 if (ret) {
2910 DRM_DEBUG_KMS("Invalid mode\n");
2911 goto out;
2912 }
2913
f453ba04
DA
2914 ret = drm_mode_detachmode(dev, connector, &mode);
2915out:
84849903 2916 drm_modeset_unlock_all(dev);
f453ba04
DA
2917 return ret;
2918}
2919
2920struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2921 const char *name, int num_values)
2922{
2923 struct drm_property *property = NULL;
6bfc56aa 2924 int ret;
f453ba04
DA
2925
2926 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2927 if (!property)
2928 return NULL;
2929
2930 if (num_values) {
2931 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2932 if (!property->values)
2933 goto fail;
2934 }
2935
6bfc56aa
VS
2936 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2937 if (ret)
2938 goto fail;
2939
f453ba04
DA
2940 property->flags = flags;
2941 property->num_values = num_values;
2942 INIT_LIST_HEAD(&property->enum_blob_list);
2943
471dd2ef 2944 if (name) {
f453ba04 2945 strncpy(property->name, name, DRM_PROP_NAME_LEN);
471dd2ef
VL
2946 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2947 }
f453ba04
DA
2948
2949 list_add_tail(&property->head, &dev->mode_config.property_list);
2950 return property;
2951fail:
6bfc56aa 2952 kfree(property->values);
f453ba04
DA
2953 kfree(property);
2954 return NULL;
2955}
2956EXPORT_SYMBOL(drm_property_create);
2957
4a67d391
SH
2958struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2959 const char *name,
2960 const struct drm_prop_enum_list *props,
2961 int num_values)
2962{
2963 struct drm_property *property;
2964 int i, ret;
2965
2966 flags |= DRM_MODE_PROP_ENUM;
2967
2968 property = drm_property_create(dev, flags, name, num_values);
2969 if (!property)
2970 return NULL;
2971
2972 for (i = 0; i < num_values; i++) {
2973 ret = drm_property_add_enum(property, i,
2974 props[i].type,
2975 props[i].name);
2976 if (ret) {
2977 drm_property_destroy(dev, property);
2978 return NULL;
2979 }
2980 }
2981
2982 return property;
2983}
2984EXPORT_SYMBOL(drm_property_create_enum);
2985
49e27545
RC
2986struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2987 int flags, const char *name,
2988 const struct drm_prop_enum_list *props,
2989 int num_values)
2990{
2991 struct drm_property *property;
2992 int i, ret;
2993
2994 flags |= DRM_MODE_PROP_BITMASK;
2995
2996 property = drm_property_create(dev, flags, name, num_values);
2997 if (!property)
2998 return NULL;
2999
3000 for (i = 0; i < num_values; i++) {
3001 ret = drm_property_add_enum(property, i,
3002 props[i].type,
3003 props[i].name);
3004 if (ret) {
3005 drm_property_destroy(dev, property);
3006 return NULL;
3007 }
3008 }
3009
3010 return property;
3011}
3012EXPORT_SYMBOL(drm_property_create_bitmask);
3013
d9bc3c02
SH
3014struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3015 const char *name,
3016 uint64_t min, uint64_t max)
3017{
3018 struct drm_property *property;
3019
3020 flags |= DRM_MODE_PROP_RANGE;
3021
3022 property = drm_property_create(dev, flags, name, 2);
3023 if (!property)
3024 return NULL;
3025
3026 property->values[0] = min;
3027 property->values[1] = max;
3028
3029 return property;
3030}
3031EXPORT_SYMBOL(drm_property_create_range);
3032
f453ba04
DA
3033int drm_property_add_enum(struct drm_property *property, int index,
3034 uint64_t value, const char *name)
3035{
3036 struct drm_property_enum *prop_enum;
3037
49e27545
RC
3038 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3039 return -EINVAL;
3040
3041 /*
3042 * Bitmask enum properties have the additional constraint of values
3043 * from 0 to 63
3044 */
3045 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
f453ba04
DA
3046 return -EINVAL;
3047
3048 if (!list_empty(&property->enum_blob_list)) {
3049 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3050 if (prop_enum->value == value) {
3051 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3052 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3053 return 0;
3054 }
3055 }
3056 }
3057
3058 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3059 if (!prop_enum)
3060 return -ENOMEM;
3061
3062 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3063 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3064 prop_enum->value = value;
3065
3066 property->values[index] = value;
3067 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3068 return 0;
3069}
3070EXPORT_SYMBOL(drm_property_add_enum);
3071
3072void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3073{
3074 struct drm_property_enum *prop_enum, *pt;
3075
3076 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3077 list_del(&prop_enum->head);
3078 kfree(prop_enum);
3079 }
3080
3081 if (property->num_values)
3082 kfree(property->values);
3083 drm_mode_object_put(dev, &property->base);
3084 list_del(&property->head);
3085 kfree(property);
3086}
3087EXPORT_SYMBOL(drm_property_destroy);
3088
c543188a
PZ
3089void drm_object_attach_property(struct drm_mode_object *obj,
3090 struct drm_property *property,
3091 uint64_t init_val)
3092{
7f88a9be 3093 int count = obj->properties->count;
c543188a 3094
7f88a9be
PZ
3095 if (count == DRM_OBJECT_MAX_PROPERTY) {
3096 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3097 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3098 "you see this message on the same object type.\n",
3099 obj->type);
3100 return;
c543188a
PZ
3101 }
3102
7f88a9be
PZ
3103 obj->properties->ids[count] = property->base.id;
3104 obj->properties->values[count] = init_val;
3105 obj->properties->count++;
c543188a
PZ
3106}
3107EXPORT_SYMBOL(drm_object_attach_property);
3108
3109int drm_object_property_set_value(struct drm_mode_object *obj,
3110 struct drm_property *property, uint64_t val)
3111{
3112 int i;
3113
7f88a9be 3114 for (i = 0; i < obj->properties->count; i++) {
c543188a
PZ
3115 if (obj->properties->ids[i] == property->base.id) {
3116 obj->properties->values[i] = val;
3117 return 0;
3118 }
3119 }
3120
3121 return -EINVAL;
3122}
3123EXPORT_SYMBOL(drm_object_property_set_value);
3124
3125int drm_object_property_get_value(struct drm_mode_object *obj,
3126 struct drm_property *property, uint64_t *val)
3127{
3128 int i;
3129
7f88a9be 3130 for (i = 0; i < obj->properties->count; i++) {
c543188a
PZ
3131 if (obj->properties->ids[i] == property->base.id) {
3132 *val = obj->properties->values[i];
3133 return 0;
3134 }
3135 }
3136
3137 return -EINVAL;
3138}
3139EXPORT_SYMBOL(drm_object_property_get_value);
3140
f453ba04
DA
3141int drm_mode_getproperty_ioctl(struct drm_device *dev,
3142 void *data, struct drm_file *file_priv)
3143{
3144 struct drm_mode_object *obj;
3145 struct drm_mode_get_property *out_resp = data;
3146 struct drm_property *property;
3147 int enum_count = 0;
3148 int blob_count = 0;
3149 int value_count = 0;
3150 int ret = 0, i;
3151 int copied;
3152 struct drm_property_enum *prop_enum;
3153 struct drm_mode_property_enum __user *enum_ptr;
3154 struct drm_property_blob *prop_blob;
81f6c7f8 3155 uint32_t __user *blob_id_ptr;
f453ba04
DA
3156 uint64_t __user *values_ptr;
3157 uint32_t __user *blob_length_ptr;
3158
fb3b06c8
DA
3159 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3160 return -EINVAL;
3161
84849903 3162 drm_modeset_lock_all(dev);
f453ba04
DA
3163 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3164 if (!obj) {
3165 ret = -EINVAL;
3166 goto done;
3167 }
3168 property = obj_to_property(obj);
3169
49e27545 3170 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
f453ba04
DA
3171 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3172 enum_count++;
3173 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3174 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3175 blob_count++;
3176 }
3177
3178 value_count = property->num_values;
3179
3180 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3181 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3182 out_resp->flags = property->flags;
3183
3184 if ((out_resp->count_values >= value_count) && value_count) {
81f6c7f8 3185 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
f453ba04
DA
3186 for (i = 0; i < value_count; i++) {
3187 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3188 ret = -EFAULT;
3189 goto done;
3190 }
3191 }
3192 }
3193 out_resp->count_values = value_count;
3194
49e27545 3195 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
f453ba04
DA
3196 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3197 copied = 0;
81f6c7f8 3198 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
f453ba04
DA
3199 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3200
3201 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3202 ret = -EFAULT;
3203 goto done;
3204 }
3205
3206 if (copy_to_user(&enum_ptr[copied].name,
3207 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3208 ret = -EFAULT;
3209 goto done;
3210 }
3211 copied++;
3212 }
3213 }
3214 out_resp->count_enum_blobs = enum_count;
3215 }
3216
3217 if (property->flags & DRM_MODE_PROP_BLOB) {
3218 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3219 copied = 0;
81f6c7f8
VS
3220 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3221 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
f453ba04
DA
3222
3223 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3224 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3225 ret = -EFAULT;
3226 goto done;
3227 }
3228
3229 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3230 ret = -EFAULT;
3231 goto done;
3232 }
3233
3234 copied++;
3235 }
3236 }
3237 out_resp->count_enum_blobs = blob_count;
3238 }
3239done:
84849903 3240 drm_modeset_unlock_all(dev);
f453ba04
DA
3241 return ret;
3242}
3243
3244static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3245 void *data)
3246{
3247 struct drm_property_blob *blob;
6bfc56aa 3248 int ret;
f453ba04
DA
3249
3250 if (!length || !data)
3251 return NULL;
3252
3253 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3254 if (!blob)
3255 return NULL;
3256
6bfc56aa
VS
3257 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3258 if (ret) {
3259 kfree(blob);
3260 return NULL;
3261 }
3262
f453ba04
DA
3263 blob->length = length;
3264
3265 memcpy(blob->data, data, length);
3266
f453ba04
DA
3267 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3268 return blob;
3269}
3270
3271static void drm_property_destroy_blob(struct drm_device *dev,
3272 struct drm_property_blob *blob)
3273{
3274 drm_mode_object_put(dev, &blob->base);
3275 list_del(&blob->head);
3276 kfree(blob);
3277}
3278
3279int drm_mode_getblob_ioctl(struct drm_device *dev,
3280 void *data, struct drm_file *file_priv)
3281{
3282 struct drm_mode_object *obj;
3283 struct drm_mode_get_blob *out_resp = data;
3284 struct drm_property_blob *blob;
3285 int ret = 0;
81f6c7f8 3286 void __user *blob_ptr;
f453ba04 3287
fb3b06c8
DA
3288 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3289 return -EINVAL;
3290
84849903 3291 drm_modeset_lock_all(dev);
f453ba04
DA
3292 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3293 if (!obj) {
3294 ret = -EINVAL;
3295 goto done;
3296 }
3297 blob = obj_to_blob(obj);
3298
3299 if (out_resp->length == blob->length) {
81f6c7f8 3300 blob_ptr = (void __user *)(unsigned long)out_resp->data;
f453ba04
DA
3301 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3302 ret = -EFAULT;
3303 goto done;
3304 }
3305 }
3306 out_resp->length = blob->length;
3307
3308done:
84849903 3309 drm_modeset_unlock_all(dev);
f453ba04
DA
3310 return ret;
3311}
3312
3313int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3314 struct edid *edid)
3315{
3316 struct drm_device *dev = connector->dev;
4a1b0714 3317 int ret, size;
f453ba04
DA
3318
3319 if (connector->edid_blob_ptr)
3320 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3321
3322 /* Delete edid, when there is none. */
3323 if (!edid) {
3324 connector->edid_blob_ptr = NULL;
58495563 3325 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
f453ba04
DA
3326 return ret;
3327 }
3328
7466f4cc
AJ
3329 size = EDID_LENGTH * (1 + edid->extensions);
3330 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3331 size, edid);
e655d122
SK
3332 if (!connector->edid_blob_ptr)
3333 return -EINVAL;
f453ba04 3334
58495563 3335 ret = drm_object_property_set_value(&connector->base,
f453ba04
DA
3336 dev->mode_config.edid_property,
3337 connector->edid_blob_ptr->base.id);
3338
3339 return ret;
3340}
3341EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3342
26a34815 3343static bool drm_property_change_is_valid(struct drm_property *property,
592c20ee 3344 uint64_t value)
26a34815
PZ
3345{
3346 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3347 return false;
3348 if (property->flags & DRM_MODE_PROP_RANGE) {
3349 if (value < property->values[0] || value > property->values[1])
3350 return false;
3351 return true;
49e27545
RC
3352 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3353 int i;
592c20ee 3354 uint64_t valid_mask = 0;
49e27545
RC
3355 for (i = 0; i < property->num_values; i++)
3356 valid_mask |= (1ULL << property->values[i]);
3357 return !(value & ~valid_mask);
c4a56750
VS
3358 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3359 /* Only the driver knows */
3360 return true;
26a34815
PZ
3361 } else {
3362 int i;
3363 for (i = 0; i < property->num_values; i++)
3364 if (property->values[i] == value)
3365 return true;
3366 return false;
3367 }
3368}
3369
f453ba04
DA
3370int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3371 void *data, struct drm_file *file_priv)
3372{
0057d8dd
PZ
3373 struct drm_mode_connector_set_property *conn_set_prop = data;
3374 struct drm_mode_obj_set_property obj_set_prop = {
3375 .value = conn_set_prop->value,
3376 .prop_id = conn_set_prop->prop_id,
3377 .obj_id = conn_set_prop->connector_id,
3378 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3379 };
fb3b06c8 3380
0057d8dd
PZ
3381 /* It does all the locking and checking we need */
3382 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
f453ba04
DA
3383}
3384
c543188a
PZ
3385static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3386 struct drm_property *property,
3387 uint64_t value)
3388{
3389 int ret = -EINVAL;
3390 struct drm_connector *connector = obj_to_connector(obj);
3391
3392 /* Do DPMS ourselves */
3393 if (property == connector->dev->mode_config.dpms_property) {
3394 if (connector->funcs->dpms)
3395 (*connector->funcs->dpms)(connector, (int)value);
3396 ret = 0;
3397 } else if (connector->funcs->set_property)
3398 ret = connector->funcs->set_property(connector, property, value);
3399
3400 /* store the property value if successful */
3401 if (!ret)
58495563 3402 drm_object_property_set_value(&connector->base, property, value);
c543188a
PZ
3403 return ret;
3404}
3405
bffd9de0
PZ
3406static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3407 struct drm_property *property,
3408 uint64_t value)
3409{
3410 int ret = -EINVAL;
3411 struct drm_crtc *crtc = obj_to_crtc(obj);
3412
3413 if (crtc->funcs->set_property)
3414 ret = crtc->funcs->set_property(crtc, property, value);
3415 if (!ret)
3416 drm_object_property_set_value(obj, property, value);
3417
3418 return ret;
3419}
3420
4d93914a
RC
3421static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3422 struct drm_property *property,
3423 uint64_t value)
3424{
3425 int ret = -EINVAL;
3426 struct drm_plane *plane = obj_to_plane(obj);
3427
3428 if (plane->funcs->set_property)
3429 ret = plane->funcs->set_property(plane, property, value);
3430 if (!ret)
3431 drm_object_property_set_value(obj, property, value);
3432
3433 return ret;
3434}
3435
c543188a
PZ
3436int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3437 struct drm_file *file_priv)
3438{
3439 struct drm_mode_obj_get_properties *arg = data;
3440 struct drm_mode_object *obj;
3441 int ret = 0;
3442 int i;
3443 int copied = 0;
3444 int props_count = 0;
3445 uint32_t __user *props_ptr;
3446 uint64_t __user *prop_values_ptr;
3447
3448 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3449 return -EINVAL;
3450
84849903 3451 drm_modeset_lock_all(dev);
c543188a
PZ
3452
3453 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3454 if (!obj) {
3455 ret = -EINVAL;
3456 goto out;
3457 }
3458 if (!obj->properties) {
3459 ret = -EINVAL;
3460 goto out;
3461 }
3462
7f88a9be 3463 props_count = obj->properties->count;
c543188a
PZ
3464
3465 /* This ioctl is called twice, once to determine how much space is
3466 * needed, and the 2nd time to fill it. */
3467 if ((arg->count_props >= props_count) && props_count) {
3468 copied = 0;
3469 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3470 prop_values_ptr = (uint64_t __user *)(unsigned long)
3471 (arg->prop_values_ptr);
3472 for (i = 0; i < props_count; i++) {
3473 if (put_user(obj->properties->ids[i],
3474 props_ptr + copied)) {
3475 ret = -EFAULT;
3476 goto out;
3477 }
3478 if (put_user(obj->properties->values[i],
3479 prop_values_ptr + copied)) {
3480 ret = -EFAULT;
3481 goto out;
3482 }
3483 copied++;
3484 }
3485 }
3486 arg->count_props = props_count;
3487out:
84849903 3488 drm_modeset_unlock_all(dev);
c543188a
PZ
3489 return ret;
3490}
3491
3492int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3493 struct drm_file *file_priv)
3494{
3495 struct drm_mode_obj_set_property *arg = data;
3496 struct drm_mode_object *arg_obj;
3497 struct drm_mode_object *prop_obj;
3498 struct drm_property *property;
3499 int ret = -EINVAL;
3500 int i;
3501
3502 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3503 return -EINVAL;
3504
84849903 3505 drm_modeset_lock_all(dev);
c543188a
PZ
3506
3507 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3508 if (!arg_obj)
3509 goto out;
3510 if (!arg_obj->properties)
3511 goto out;
3512
7f88a9be 3513 for (i = 0; i < arg_obj->properties->count; i++)
c543188a
PZ
3514 if (arg_obj->properties->ids[i] == arg->prop_id)
3515 break;
3516
7f88a9be 3517 if (i == arg_obj->properties->count)
c543188a
PZ
3518 goto out;
3519
3520 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3521 DRM_MODE_OBJECT_PROPERTY);
3522 if (!prop_obj)
3523 goto out;
3524 property = obj_to_property(prop_obj);
3525
3526 if (!drm_property_change_is_valid(property, arg->value))
3527 goto out;
3528
3529 switch (arg_obj->type) {
3530 case DRM_MODE_OBJECT_CONNECTOR:
3531 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3532 arg->value);
3533 break;
bffd9de0
PZ
3534 case DRM_MODE_OBJECT_CRTC:
3535 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3536 break;
4d93914a
RC
3537 case DRM_MODE_OBJECT_PLANE:
3538 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3539 break;
c543188a
PZ
3540 }
3541
3542out:
84849903 3543 drm_modeset_unlock_all(dev);
c543188a
PZ
3544 return ret;
3545}
3546
f453ba04
DA
3547int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3548 struct drm_encoder *encoder)
3549{
3550 int i;
3551
3552 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3553 if (connector->encoder_ids[i] == 0) {
3554 connector->encoder_ids[i] = encoder->base.id;
3555 return 0;
3556 }
3557 }
3558 return -ENOMEM;
3559}
3560EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3561
3562void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3563 struct drm_encoder *encoder)
3564{
3565 int i;
3566 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3567 if (connector->encoder_ids[i] == encoder->base.id) {
3568 connector->encoder_ids[i] = 0;
3569 if (connector->encoder == encoder)
3570 connector->encoder = NULL;
3571 break;
3572 }
3573 }
3574}
3575EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3576
4cae5b84 3577int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
f453ba04
DA
3578 int gamma_size)
3579{
3580 crtc->gamma_size = gamma_size;
3581
3582 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3583 if (!crtc->gamma_store) {
3584 crtc->gamma_size = 0;
4cae5b84 3585 return -ENOMEM;
f453ba04
DA
3586 }
3587
4cae5b84 3588 return 0;
f453ba04
DA
3589}
3590EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3591
3592int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3593 void *data, struct drm_file *file_priv)
3594{
3595 struct drm_mode_crtc_lut *crtc_lut = data;
3596 struct drm_mode_object *obj;
3597 struct drm_crtc *crtc;
3598 void *r_base, *g_base, *b_base;
3599 int size;
3600 int ret = 0;
3601
fb3b06c8
DA
3602 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3603 return -EINVAL;
3604
84849903 3605 drm_modeset_lock_all(dev);
f453ba04
DA
3606 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3607 if (!obj) {
3608 ret = -EINVAL;
3609 goto out;
3610 }
3611 crtc = obj_to_crtc(obj);
3612
ebe0f244
LP
3613 if (crtc->funcs->gamma_set == NULL) {
3614 ret = -ENOSYS;
3615 goto out;
3616 }
3617
f453ba04
DA
3618 /* memcpy into gamma store */
3619 if (crtc_lut->gamma_size != crtc->gamma_size) {
3620 ret = -EINVAL;
3621 goto out;
3622 }
3623
3624 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3625 r_base = crtc->gamma_store;
3626 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3627 ret = -EFAULT;
3628 goto out;
3629 }
3630
3631 g_base = r_base + size;
3632 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3633 ret = -EFAULT;
3634 goto out;
3635 }
3636
3637 b_base = g_base + size;
3638 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3639 ret = -EFAULT;
3640 goto out;
3641 }
3642
7203425a 3643 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
f453ba04
DA
3644
3645out:
84849903 3646 drm_modeset_unlock_all(dev);
f453ba04
DA
3647 return ret;
3648
3649}
3650
3651int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3652 void *data, struct drm_file *file_priv)
3653{
3654 struct drm_mode_crtc_lut *crtc_lut = data;
3655 struct drm_mode_object *obj;
3656 struct drm_crtc *crtc;
3657 void *r_base, *g_base, *b_base;
3658 int size;
3659 int ret = 0;
3660
fb3b06c8
DA
3661 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3662 return -EINVAL;
3663
84849903 3664 drm_modeset_lock_all(dev);
f453ba04
DA
3665 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3666 if (!obj) {
3667 ret = -EINVAL;
3668 goto out;
3669 }
3670 crtc = obj_to_crtc(obj);
3671
3672 /* memcpy into gamma store */
3673 if (crtc_lut->gamma_size != crtc->gamma_size) {
3674 ret = -EINVAL;
3675 goto out;
3676 }
3677
3678 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3679 r_base = crtc->gamma_store;
3680 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3681 ret = -EFAULT;
3682 goto out;
3683 }
3684
3685 g_base = r_base + size;
3686 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3687 ret = -EFAULT;
3688 goto out;
3689 }
3690
3691 b_base = g_base + size;
3692 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3693 ret = -EFAULT;
3694 goto out;
3695 }
3696out:
84849903 3697 drm_modeset_unlock_all(dev);
f453ba04
DA
3698 return ret;
3699}
d91d8a3f
KH
3700
3701int drm_mode_page_flip_ioctl(struct drm_device *dev,
3702 void *data, struct drm_file *file_priv)
3703{
3704 struct drm_mode_crtc_page_flip *page_flip = data;
3705 struct drm_mode_object *obj;
3706 struct drm_crtc *crtc;
b0d12325 3707 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
d91d8a3f
KH
3708 struct drm_pending_vblank_event *e = NULL;
3709 unsigned long flags;
7c80e128 3710 int hdisplay, vdisplay;
d91d8a3f
KH
3711 int ret = -EINVAL;
3712
3713 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3714 page_flip->reserved != 0)
3715 return -EINVAL;
3716
d91d8a3f
KH
3717 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3718 if (!obj)
b4d5e7d1 3719 return -EINVAL;
d91d8a3f
KH
3720 crtc = obj_to_crtc(obj);
3721
b4d5e7d1 3722 mutex_lock(&crtc->mutex);
90c1efdd
CW
3723 if (crtc->fb == NULL) {
3724 /* The framebuffer is currently unbound, presumably
3725 * due to a hotplug event, that userspace has not
3726 * yet discovered.
3727 */
3728 ret = -EBUSY;
3729 goto out;
3730 }
3731
d91d8a3f
KH
3732 if (crtc->funcs->page_flip == NULL)
3733 goto out;
3734
786b99ed
DV
3735 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3736 if (!fb)
d91d8a3f 3737 goto out;
d91d8a3f 3738
7c80e128
RC
3739 hdisplay = crtc->mode.hdisplay;
3740 vdisplay = crtc->mode.vdisplay;
3741
3742 if (crtc->invert_dimensions)
3743 swap(hdisplay, vdisplay);
3744
3745 if (hdisplay > fb->width ||
3746 vdisplay > fb->height ||
3747 crtc->x > fb->width - hdisplay ||
3748 crtc->y > fb->height - vdisplay) {
3749 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3750 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3751 crtc->invert_dimensions ? " (inverted)" : "");
5f61bb42
VS
3752 ret = -ENOSPC;
3753 goto out;
3754 }
3755
d91d8a3f
KH
3756 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3757 ret = -ENOMEM;
3758 spin_lock_irqsave(&dev->event_lock, flags);
3759 if (file_priv->event_space < sizeof e->event) {
3760 spin_unlock_irqrestore(&dev->event_lock, flags);
3761 goto out;
3762 }
3763 file_priv->event_space -= sizeof e->event;
3764 spin_unlock_irqrestore(&dev->event_lock, flags);
3765
3766 e = kzalloc(sizeof *e, GFP_KERNEL);
3767 if (e == NULL) {
3768 spin_lock_irqsave(&dev->event_lock, flags);
3769 file_priv->event_space += sizeof e->event;
3770 spin_unlock_irqrestore(&dev->event_lock, flags);
3771 goto out;
3772 }
3773
7bd4d7be 3774 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
d91d8a3f
KH
3775 e->event.base.length = sizeof e->event;
3776 e->event.user_data = page_flip->user_data;
3777 e->base.event = &e->event.base;
3778 e->base.file_priv = file_priv;
3779 e->base.destroy =
3780 (void (*) (struct drm_pending_event *)) kfree;
3781 }
3782
b0d12325 3783 old_fb = crtc->fb;
d91d8a3f
KH
3784 ret = crtc->funcs->page_flip(crtc, fb, e);
3785 if (ret) {
aef6a7ee
JS
3786 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3787 spin_lock_irqsave(&dev->event_lock, flags);
3788 file_priv->event_space += sizeof e->event;
3789 spin_unlock_irqrestore(&dev->event_lock, flags);
3790 kfree(e);
3791 }
b0d12325
DV
3792 /* Keep the old fb, don't unref it. */
3793 old_fb = NULL;
3794 } else {
8cf1e981
TR
3795 /*
3796 * Warn if the driver hasn't properly updated the crtc->fb
3797 * field to reflect that the new framebuffer is now used.
3798 * Failing to do so will screw with the reference counting
3799 * on framebuffers.
3800 */
3801 WARN_ON(crtc->fb != fb);
b0d12325
DV
3802 /* Unref only the old framebuffer. */
3803 fb = NULL;
d91d8a3f
KH
3804 }
3805
3806out:
b0d12325
DV
3807 if (fb)
3808 drm_framebuffer_unreference(fb);
3809 if (old_fb)
3810 drm_framebuffer_unreference(old_fb);
b4d5e7d1
DV
3811 mutex_unlock(&crtc->mutex);
3812
d91d8a3f
KH
3813 return ret;
3814}
eb033556
CW
3815
3816void drm_mode_config_reset(struct drm_device *dev)
3817{
3818 struct drm_crtc *crtc;
3819 struct drm_encoder *encoder;
3820 struct drm_connector *connector;
3821
3822 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3823 if (crtc->funcs->reset)
3824 crtc->funcs->reset(crtc);
3825
3826 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3827 if (encoder->funcs->reset)
3828 encoder->funcs->reset(encoder);
3829
5e2cb2f6
DV
3830 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3831 connector->status = connector_status_unknown;
3832
eb033556
CW
3833 if (connector->funcs->reset)
3834 connector->funcs->reset(connector);
5e2cb2f6 3835 }
eb033556
CW
3836}
3837EXPORT_SYMBOL(drm_mode_config_reset);
ff72145b
DA
3838
3839int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3840 void *data, struct drm_file *file_priv)
3841{
3842 struct drm_mode_create_dumb *args = data;
3843
3844 if (!dev->driver->dumb_create)
3845 return -ENOSYS;
3846 return dev->driver->dumb_create(file_priv, dev, args);
3847}
3848
3849int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3850 void *data, struct drm_file *file_priv)
3851{
3852 struct drm_mode_map_dumb *args = data;
3853
3854 /* call driver ioctl to get mmap offset */
3855 if (!dev->driver->dumb_map_offset)
3856 return -ENOSYS;
3857
3858 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3859}
3860
3861int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3862 void *data, struct drm_file *file_priv)
3863{
3864 struct drm_mode_destroy_dumb *args = data;
3865
3866 if (!dev->driver->dumb_destroy)
3867 return -ENOSYS;
3868
3869 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3870}
248dbc23
DA
3871
3872/*
3873 * Just need to support RGB formats here for compat with code that doesn't
3874 * use pixel formats directly yet.
3875 */
3876void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3877 int *bpp)
3878{
3879 switch (format) {
c51a6bc5 3880 case DRM_FORMAT_C8:
04b3924d
VS
3881 case DRM_FORMAT_RGB332:
3882 case DRM_FORMAT_BGR233:
248dbc23
DA
3883 *depth = 8;
3884 *bpp = 8;
3885 break;
04b3924d
VS
3886 case DRM_FORMAT_XRGB1555:
3887 case DRM_FORMAT_XBGR1555:
3888 case DRM_FORMAT_RGBX5551:
3889 case DRM_FORMAT_BGRX5551:
3890 case DRM_FORMAT_ARGB1555:
3891 case DRM_FORMAT_ABGR1555:
3892 case DRM_FORMAT_RGBA5551:
3893 case DRM_FORMAT_BGRA5551:
248dbc23
DA
3894 *depth = 15;
3895 *bpp = 16;
3896 break;
04b3924d
VS
3897 case DRM_FORMAT_RGB565:
3898 case DRM_FORMAT_BGR565:
248dbc23
DA
3899 *depth = 16;
3900 *bpp = 16;
3901 break;
04b3924d
VS
3902 case DRM_FORMAT_RGB888:
3903 case DRM_FORMAT_BGR888:
3904 *depth = 24;
3905 *bpp = 24;
3906 break;
3907 case DRM_FORMAT_XRGB8888:
3908 case DRM_FORMAT_XBGR8888:
3909 case DRM_FORMAT_RGBX8888:
3910 case DRM_FORMAT_BGRX8888:
248dbc23
DA
3911 *depth = 24;
3912 *bpp = 32;
3913 break;
04b3924d
VS
3914 case DRM_FORMAT_XRGB2101010:
3915 case DRM_FORMAT_XBGR2101010:
3916 case DRM_FORMAT_RGBX1010102:
3917 case DRM_FORMAT_BGRX1010102:
3918 case DRM_FORMAT_ARGB2101010:
3919 case DRM_FORMAT_ABGR2101010:
3920 case DRM_FORMAT_RGBA1010102:
3921 case DRM_FORMAT_BGRA1010102:
248dbc23
DA
3922 *depth = 30;
3923 *bpp = 32;
3924 break;
04b3924d
VS
3925 case DRM_FORMAT_ARGB8888:
3926 case DRM_FORMAT_ABGR8888:
3927 case DRM_FORMAT_RGBA8888:
3928 case DRM_FORMAT_BGRA8888:
248dbc23
DA
3929 *depth = 32;
3930 *bpp = 32;
3931 break;
3932 default:
3933 DRM_DEBUG_KMS("unsupported pixel format\n");
3934 *depth = 0;
3935 *bpp = 0;
3936 break;
3937 }
3938}
3939EXPORT_SYMBOL(drm_fb_get_bpp_depth);
141670e9
VS
3940
3941/**
3942 * drm_format_num_planes - get the number of planes for format
3943 * @format: pixel format (DRM_FORMAT_*)
3944 *
3945 * RETURNS:
3946 * The number of planes used by the specified pixel format.
3947 */
3948int drm_format_num_planes(uint32_t format)
3949{
3950 switch (format) {
3951 case DRM_FORMAT_YUV410:
3952 case DRM_FORMAT_YVU410:
3953 case DRM_FORMAT_YUV411:
3954 case DRM_FORMAT_YVU411:
3955 case DRM_FORMAT_YUV420:
3956 case DRM_FORMAT_YVU420:
3957 case DRM_FORMAT_YUV422:
3958 case DRM_FORMAT_YVU422:
3959 case DRM_FORMAT_YUV444:
3960 case DRM_FORMAT_YVU444:
3961 return 3;
3962 case DRM_FORMAT_NV12:
3963 case DRM_FORMAT_NV21:
3964 case DRM_FORMAT_NV16:
3965 case DRM_FORMAT_NV61:
ba623f6a
LP
3966 case DRM_FORMAT_NV24:
3967 case DRM_FORMAT_NV42:
141670e9
VS
3968 return 2;
3969 default:
3970 return 1;
3971 }
3972}
3973EXPORT_SYMBOL(drm_format_num_planes);
5a86bd55
VS
3974
3975/**
3976 * drm_format_plane_cpp - determine the bytes per pixel value
3977 * @format: pixel format (DRM_FORMAT_*)
3978 * @plane: plane index
3979 *
3980 * RETURNS:
3981 * The bytes per pixel value for the specified plane.
3982 */
3983int drm_format_plane_cpp(uint32_t format, int plane)
3984{
3985 unsigned int depth;
3986 int bpp;
3987
3988 if (plane >= drm_format_num_planes(format))
3989 return 0;
3990
3991 switch (format) {
3992 case DRM_FORMAT_YUYV:
3993 case DRM_FORMAT_YVYU:
3994 case DRM_FORMAT_UYVY:
3995 case DRM_FORMAT_VYUY:
3996 return 2;
3997 case DRM_FORMAT_NV12:
3998 case DRM_FORMAT_NV21:
3999 case DRM_FORMAT_NV16:
4000 case DRM_FORMAT_NV61:
ba623f6a
LP
4001 case DRM_FORMAT_NV24:
4002 case DRM_FORMAT_NV42:
5a86bd55
VS
4003 return plane ? 2 : 1;
4004 case DRM_FORMAT_YUV410:
4005 case DRM_FORMAT_YVU410:
4006 case DRM_FORMAT_YUV411:
4007 case DRM_FORMAT_YVU411:
4008 case DRM_FORMAT_YUV420:
4009 case DRM_FORMAT_YVU420:
4010 case DRM_FORMAT_YUV422:
4011 case DRM_FORMAT_YVU422:
4012 case DRM_FORMAT_YUV444:
4013 case DRM_FORMAT_YVU444:
4014 return 1;
4015 default:
4016 drm_fb_get_bpp_depth(format, &depth, &bpp);
4017 return bpp >> 3;
4018 }
4019}
4020EXPORT_SYMBOL(drm_format_plane_cpp);
01b68b04
VS
4021
4022/**
4023 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4024 * @format: pixel format (DRM_FORMAT_*)
4025 *
4026 * RETURNS:
4027 * The horizontal chroma subsampling factor for the
4028 * specified pixel format.
4029 */
4030int drm_format_horz_chroma_subsampling(uint32_t format)
4031{
4032 switch (format) {
4033 case DRM_FORMAT_YUV411:
4034 case DRM_FORMAT_YVU411:
4035 case DRM_FORMAT_YUV410:
4036 case DRM_FORMAT_YVU410:
4037 return 4;
4038 case DRM_FORMAT_YUYV:
4039 case DRM_FORMAT_YVYU:
4040 case DRM_FORMAT_UYVY:
4041 case DRM_FORMAT_VYUY:
4042 case DRM_FORMAT_NV12:
4043 case DRM_FORMAT_NV21:
4044 case DRM_FORMAT_NV16:
4045 case DRM_FORMAT_NV61:
4046 case DRM_FORMAT_YUV422:
4047 case DRM_FORMAT_YVU422:
4048 case DRM_FORMAT_YUV420:
4049 case DRM_FORMAT_YVU420:
4050 return 2;
4051 default:
4052 return 1;
4053 }
4054}
4055EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4056
4057/**
4058 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4059 * @format: pixel format (DRM_FORMAT_*)
4060 *
4061 * RETURNS:
4062 * The vertical chroma subsampling factor for the
4063 * specified pixel format.
4064 */
4065int drm_format_vert_chroma_subsampling(uint32_t format)
4066{
4067 switch (format) {
4068 case DRM_FORMAT_YUV410:
4069 case DRM_FORMAT_YVU410:
4070 return 4;
4071 case DRM_FORMAT_YUV420:
4072 case DRM_FORMAT_YVU420:
4073 case DRM_FORMAT_NV12:
4074 case DRM_FORMAT_NV21:
4075 return 2;
4076 default:
4077 return 1;
4078 }
4079}
4080EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
This page took 0.559423 seconds and 5 git commands to generate.