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