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