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