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