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