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