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