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