drm: only take the crtc lock for ->cursor_move
[linux-2.6-block.git] / drivers / gpu / drm / drm_crtc.c
CommitLineData
f453ba04
DA
1/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32#include <linux/list.h>
5a0e3ad6 33#include <linux/slab.h>
2d1a8a48 34#include <linux/export.h>
760285e7
DH
35#include <drm/drmP.h>
36#include <drm/drm_crtc.h>
37#include <drm/drm_edid.h>
38#include <drm/drm_fourcc.h>
f453ba04 39
84849903
DV
40/**
41 * drm_modeset_lock_all - take all modeset locks
42 * @dev: drm device
43 *
44 * This function takes all modeset locks, suitable where a more fine-grained
45 * scheme isn't (yet) implemented.
46 */
47void drm_modeset_lock_all(struct drm_device *dev)
48{
29494c17
DV
49 struct drm_crtc *crtc;
50
84849903 51 mutex_lock(&dev->mode_config.mutex);
29494c17
DV
52
53 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
54 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
84849903
DV
55}
56EXPORT_SYMBOL(drm_modeset_lock_all);
57
58/**
59 * drm_modeset_unlock_all - drop all modeset locks
60 * @dev: device
61 */
62void drm_modeset_unlock_all(struct drm_device *dev)
63{
29494c17
DV
64 struct drm_crtc *crtc;
65
66 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
67 mutex_unlock(&crtc->mutex);
68
84849903
DV
69 mutex_unlock(&dev->mode_config.mutex);
70}
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);
dac35663 2042 return -EINVAL;
f453ba04
DA
2043 }
2044 crtc = obj_to_crtc(obj);
2045
dac35663 2046 mutex_lock(&crtc->mutex);
f453ba04
DA
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 */
2053 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2054 req->width, req->height);
2055 }
2056
2057 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2058 if (crtc->funcs->cursor_move) {
2059 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2060 } else {
f453ba04
DA
2061 ret = -EFAULT;
2062 goto out;
2063 }
2064 }
2065out:
dac35663
DV
2066 mutex_unlock(&crtc->mutex);
2067
f453ba04
DA
2068 return ret;
2069}
2070
308e5bcb
JB
2071/* Original addfb only supported RGB formats, so figure out which one */
2072uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2073{
2074 uint32_t fmt;
2075
2076 switch (bpp) {
2077 case 8:
04b3924d 2078 fmt = DRM_FORMAT_RGB332;
308e5bcb
JB
2079 break;
2080 case 16:
2081 if (depth == 15)
04b3924d 2082 fmt = DRM_FORMAT_XRGB1555;
308e5bcb 2083 else
04b3924d 2084 fmt = DRM_FORMAT_RGB565;
308e5bcb
JB
2085 break;
2086 case 24:
04b3924d 2087 fmt = DRM_FORMAT_RGB888;
308e5bcb
JB
2088 break;
2089 case 32:
2090 if (depth == 24)
04b3924d 2091 fmt = DRM_FORMAT_XRGB8888;
308e5bcb 2092 else if (depth == 30)
04b3924d 2093 fmt = DRM_FORMAT_XRGB2101010;
308e5bcb 2094 else
04b3924d 2095 fmt = DRM_FORMAT_ARGB8888;
308e5bcb
JB
2096 break;
2097 default:
04b3924d
VS
2098 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2099 fmt = DRM_FORMAT_XRGB8888;
308e5bcb
JB
2100 break;
2101 }
2102
2103 return fmt;
2104}
2105EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2106
f453ba04
DA
2107/**
2108 * drm_mode_addfb - add an FB to the graphics configuration
065a50ed
DV
2109 * @dev: drm device for the ioctl
2110 * @data: data pointer for the ioctl
2111 * @file_priv: drm file for the ioctl call
f453ba04 2112 *
f453ba04
DA
2113 * Add a new FB to the specified CRTC, given a user request.
2114 *
2115 * Called by the user via ioctl.
2116 *
2117 * RETURNS:
2118 * Zero on success, errno on failure.
2119 */
2120int drm_mode_addfb(struct drm_device *dev,
2121 void *data, struct drm_file *file_priv)
2122{
308e5bcb
JB
2123 struct drm_mode_fb_cmd *or = data;
2124 struct drm_mode_fb_cmd2 r = {};
2125 struct drm_mode_config *config = &dev->mode_config;
2126 struct drm_framebuffer *fb;
2127 int ret = 0;
2128
2129 /* Use new struct with format internally */
2130 r.fb_id = or->fb_id;
2131 r.width = or->width;
2132 r.height = or->height;
2133 r.pitches[0] = or->pitch;
2134 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2135 r.handles[0] = or->handle;
2136
2137 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2138 return -EINVAL;
2139
acb4b992 2140 if ((config->min_width > r.width) || (r.width > config->max_width))
308e5bcb 2141 return -EINVAL;
acb4b992
JB
2142
2143 if ((config->min_height > r.height) || (r.height > config->max_height))
308e5bcb 2144 return -EINVAL;
308e5bcb 2145
84849903 2146 drm_modeset_lock_all(dev);
308e5bcb
JB
2147
2148 /* TODO check buffer is sufficiently large */
2149 /* TODO setup destructor callback */
2150
2151 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2152 if (IS_ERR(fb)) {
1aa1b11c 2153 DRM_DEBUG_KMS("could not create framebuffer\n");
308e5bcb
JB
2154 ret = PTR_ERR(fb);
2155 goto out;
2156 }
2157
2158 or->fb_id = fb->base.id;
2159 list_add(&fb->filp_head, &file_priv->fbs);
2160 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2161
2162out:
84849903 2163 drm_modeset_unlock_all(dev);
308e5bcb
JB
2164 return ret;
2165}
2166
cff91b62 2167static int format_check(const struct drm_mode_fb_cmd2 *r)
935b5977
VS
2168{
2169 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2170
2171 switch (format) {
2172 case DRM_FORMAT_C8:
2173 case DRM_FORMAT_RGB332:
2174 case DRM_FORMAT_BGR233:
2175 case DRM_FORMAT_XRGB4444:
2176 case DRM_FORMAT_XBGR4444:
2177 case DRM_FORMAT_RGBX4444:
2178 case DRM_FORMAT_BGRX4444:
2179 case DRM_FORMAT_ARGB4444:
2180 case DRM_FORMAT_ABGR4444:
2181 case DRM_FORMAT_RGBA4444:
2182 case DRM_FORMAT_BGRA4444:
2183 case DRM_FORMAT_XRGB1555:
2184 case DRM_FORMAT_XBGR1555:
2185 case DRM_FORMAT_RGBX5551:
2186 case DRM_FORMAT_BGRX5551:
2187 case DRM_FORMAT_ARGB1555:
2188 case DRM_FORMAT_ABGR1555:
2189 case DRM_FORMAT_RGBA5551:
2190 case DRM_FORMAT_BGRA5551:
2191 case DRM_FORMAT_RGB565:
2192 case DRM_FORMAT_BGR565:
2193 case DRM_FORMAT_RGB888:
2194 case DRM_FORMAT_BGR888:
2195 case DRM_FORMAT_XRGB8888:
2196 case DRM_FORMAT_XBGR8888:
2197 case DRM_FORMAT_RGBX8888:
2198 case DRM_FORMAT_BGRX8888:
2199 case DRM_FORMAT_ARGB8888:
2200 case DRM_FORMAT_ABGR8888:
2201 case DRM_FORMAT_RGBA8888:
2202 case DRM_FORMAT_BGRA8888:
2203 case DRM_FORMAT_XRGB2101010:
2204 case DRM_FORMAT_XBGR2101010:
2205 case DRM_FORMAT_RGBX1010102:
2206 case DRM_FORMAT_BGRX1010102:
2207 case DRM_FORMAT_ARGB2101010:
2208 case DRM_FORMAT_ABGR2101010:
2209 case DRM_FORMAT_RGBA1010102:
2210 case DRM_FORMAT_BGRA1010102:
2211 case DRM_FORMAT_YUYV:
2212 case DRM_FORMAT_YVYU:
2213 case DRM_FORMAT_UYVY:
2214 case DRM_FORMAT_VYUY:
2215 case DRM_FORMAT_AYUV:
2216 case DRM_FORMAT_NV12:
2217 case DRM_FORMAT_NV21:
2218 case DRM_FORMAT_NV16:
2219 case DRM_FORMAT_NV61:
ba623f6a
LP
2220 case DRM_FORMAT_NV24:
2221 case DRM_FORMAT_NV42:
935b5977
VS
2222 case DRM_FORMAT_YUV410:
2223 case DRM_FORMAT_YVU410:
2224 case DRM_FORMAT_YUV411:
2225 case DRM_FORMAT_YVU411:
2226 case DRM_FORMAT_YUV420:
2227 case DRM_FORMAT_YVU420:
2228 case DRM_FORMAT_YUV422:
2229 case DRM_FORMAT_YVU422:
2230 case DRM_FORMAT_YUV444:
2231 case DRM_FORMAT_YVU444:
2232 return 0;
2233 default:
2234 return -EINVAL;
2235 }
2236}
2237
cff91b62 2238static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
d1b45d5f
VS
2239{
2240 int ret, hsub, vsub, num_planes, i;
2241
2242 ret = format_check(r);
2243 if (ret) {
1aa1b11c 2244 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
d1b45d5f
VS
2245 return ret;
2246 }
2247
2248 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2249 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2250 num_planes = drm_format_num_planes(r->pixel_format);
2251
2252 if (r->width == 0 || r->width % hsub) {
1aa1b11c 2253 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
d1b45d5f
VS
2254 return -EINVAL;
2255 }
2256
2257 if (r->height == 0 || r->height % vsub) {
1aa1b11c 2258 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
d1b45d5f
VS
2259 return -EINVAL;
2260 }
2261
2262 for (i = 0; i < num_planes; i++) {
2263 unsigned int width = r->width / (i != 0 ? hsub : 1);
b180b5d1
VS
2264 unsigned int height = r->height / (i != 0 ? vsub : 1);
2265 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
d1b45d5f
VS
2266
2267 if (!r->handles[i]) {
1aa1b11c 2268 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
d1b45d5f
VS
2269 return -EINVAL;
2270 }
2271
b180b5d1
VS
2272 if ((uint64_t) width * cpp > UINT_MAX)
2273 return -ERANGE;
2274
2275 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2276 return -ERANGE;
2277
2278 if (r->pitches[i] < width * cpp) {
1aa1b11c 2279 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
d1b45d5f
VS
2280 return -EINVAL;
2281 }
2282 }
2283
2284 return 0;
2285}
2286
308e5bcb
JB
2287/**
2288 * drm_mode_addfb2 - add an FB to the graphics configuration
065a50ed
DV
2289 * @dev: drm device for the ioctl
2290 * @data: data pointer for the ioctl
2291 * @file_priv: drm file for the ioctl call
308e5bcb 2292 *
308e5bcb
JB
2293 * Add a new FB to the specified CRTC, given a user request with format.
2294 *
2295 * Called by the user via ioctl.
2296 *
2297 * RETURNS:
2298 * Zero on success, errno on failure.
2299 */
2300int drm_mode_addfb2(struct drm_device *dev,
2301 void *data, struct drm_file *file_priv)
2302{
2303 struct drm_mode_fb_cmd2 *r = data;
f453ba04
DA
2304 struct drm_mode_config *config = &dev->mode_config;
2305 struct drm_framebuffer *fb;
4a1b0714 2306 int ret;
f453ba04 2307
fb3b06c8
DA
2308 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2309 return -EINVAL;
2310
e3cc3520
VS
2311 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2312 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2313 return -EINVAL;
2314 }
2315
f453ba04 2316 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1aa1b11c 2317 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
8cf5c917 2318 r->width, config->min_width, config->max_width);
f453ba04
DA
2319 return -EINVAL;
2320 }
2321 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1aa1b11c 2322 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
8cf5c917 2323 r->height, config->min_height, config->max_height);
f453ba04
DA
2324 return -EINVAL;
2325 }
2326
d1b45d5f
VS
2327 ret = framebuffer_check(r);
2328 if (ret)
935b5977 2329 return ret;
935b5977 2330
84849903 2331 drm_modeset_lock_all(dev);
f453ba04 2332
f453ba04 2333 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
cce13ff7 2334 if (IS_ERR(fb)) {
1aa1b11c 2335 DRM_DEBUG_KMS("could not create framebuffer\n");
cce13ff7 2336 ret = PTR_ERR(fb);
f453ba04
DA
2337 goto out;
2338 }
2339
e0c8463a 2340 r->fb_id = fb->base.id;
f453ba04 2341 list_add(&fb->filp_head, &file_priv->fbs);
9440106b 2342 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
f453ba04
DA
2343
2344out:
84849903 2345 drm_modeset_unlock_all(dev);
f453ba04
DA
2346 return ret;
2347}
2348
2349/**
2350 * drm_mode_rmfb - remove an FB from the configuration
065a50ed
DV
2351 * @dev: drm device for the ioctl
2352 * @data: data pointer for the ioctl
2353 * @file_priv: drm file for the ioctl call
f453ba04 2354 *
f453ba04
DA
2355 * Remove the FB specified by the user.
2356 *
2357 * Called by the user via ioctl.
2358 *
2359 * RETURNS:
2360 * Zero on success, errno on failure.
2361 */
2362int drm_mode_rmfb(struct drm_device *dev,
2363 void *data, struct drm_file *file_priv)
2364{
2365 struct drm_mode_object *obj;
2366 struct drm_framebuffer *fb = NULL;
2367 struct drm_framebuffer *fbl = NULL;
2368 uint32_t *id = data;
2369 int ret = 0;
2370 int found = 0;
2371
fb3b06c8
DA
2372 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2373 return -EINVAL;
2374
84849903 2375 drm_modeset_lock_all(dev);
f453ba04 2376 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
25985edc 2377 /* TODO check that we really get a framebuffer back. */
f453ba04 2378 if (!obj) {
f453ba04
DA
2379 ret = -EINVAL;
2380 goto out;
2381 }
2382 fb = obj_to_fb(obj);
2383
2384 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2385 if (fb == fbl)
2386 found = 1;
2387
2388 if (!found) {
f453ba04
DA
2389 ret = -EINVAL;
2390 goto out;
2391 }
2392
f7eff60e 2393 drm_framebuffer_remove(fb);
f453ba04
DA
2394
2395out:
84849903 2396 drm_modeset_unlock_all(dev);
f453ba04
DA
2397 return ret;
2398}
2399
2400/**
2401 * drm_mode_getfb - get FB info
065a50ed
DV
2402 * @dev: drm device for the ioctl
2403 * @data: data pointer for the ioctl
2404 * @file_priv: drm file for the ioctl call
f453ba04 2405 *
f453ba04
DA
2406 * Lookup the FB given its ID and return info about it.
2407 *
2408 * Called by the user via ioctl.
2409 *
2410 * RETURNS:
2411 * Zero on success, errno on failure.
2412 */
2413int drm_mode_getfb(struct drm_device *dev,
2414 void *data, struct drm_file *file_priv)
2415{
2416 struct drm_mode_fb_cmd *r = data;
2417 struct drm_mode_object *obj;
2418 struct drm_framebuffer *fb;
2419 int ret = 0;
2420
fb3b06c8
DA
2421 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2422 return -EINVAL;
2423
84849903 2424 drm_modeset_lock_all(dev);
e0c8463a 2425 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
f453ba04 2426 if (!obj) {
f453ba04
DA
2427 ret = -EINVAL;
2428 goto out;
2429 }
2430 fb = obj_to_fb(obj);
2431
2432 r->height = fb->height;
2433 r->width = fb->width;
2434 r->depth = fb->depth;
2435 r->bpp = fb->bits_per_pixel;
01f2c773 2436 r->pitch = fb->pitches[0];
af26ef3b
DV
2437 if (fb->funcs->create_handle)
2438 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2439 else
2440 ret = -ENODEV;
f453ba04
DA
2441
2442out:
84849903 2443 drm_modeset_unlock_all(dev);
f453ba04
DA
2444 return ret;
2445}
2446
884840aa
JB
2447int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2448 void *data, struct drm_file *file_priv)
2449{
2450 struct drm_clip_rect __user *clips_ptr;
2451 struct drm_clip_rect *clips = NULL;
2452 struct drm_mode_fb_dirty_cmd *r = data;
2453 struct drm_mode_object *obj;
2454 struct drm_framebuffer *fb;
2455 unsigned flags;
2456 int num_clips;
4a1b0714 2457 int ret;
884840aa 2458
fb3b06c8
DA
2459 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2460 return -EINVAL;
2461
84849903 2462 drm_modeset_lock_all(dev);
884840aa
JB
2463 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2464 if (!obj) {
884840aa
JB
2465 ret = -EINVAL;
2466 goto out_err1;
2467 }
2468 fb = obj_to_fb(obj);
2469
2470 num_clips = r->num_clips;
81f6c7f8 2471 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
884840aa
JB
2472
2473 if (!num_clips != !clips_ptr) {
2474 ret = -EINVAL;
2475 goto out_err1;
2476 }
2477
2478 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2479
2480 /* If userspace annotates copy, clips must come in pairs */
2481 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2482 ret = -EINVAL;
2483 goto out_err1;
2484 }
2485
2486 if (num_clips && clips_ptr) {
a5cd3351
XW
2487 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2488 ret = -EINVAL;
2489 goto out_err1;
2490 }
884840aa
JB
2491 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2492 if (!clips) {
2493 ret = -ENOMEM;
2494 goto out_err1;
2495 }
2496
2497 ret = copy_from_user(clips, clips_ptr,
2498 num_clips * sizeof(*clips));
e902a358
DC
2499 if (ret) {
2500 ret = -EFAULT;
884840aa 2501 goto out_err2;
e902a358 2502 }
884840aa
JB
2503 }
2504
2505 if (fb->funcs->dirty) {
02b00162
TH
2506 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2507 clips, num_clips);
884840aa
JB
2508 } else {
2509 ret = -ENOSYS;
2510 goto out_err2;
2511 }
2512
2513out_err2:
2514 kfree(clips);
2515out_err1:
84849903 2516 drm_modeset_unlock_all(dev);
884840aa
JB
2517 return ret;
2518}
2519
2520
f453ba04
DA
2521/**
2522 * drm_fb_release - remove and free the FBs on this file
065a50ed 2523 * @priv: drm file for the ioctl
f453ba04 2524 *
f453ba04
DA
2525 * Destroy all the FBs associated with @filp.
2526 *
2527 * Called by the user via ioctl.
2528 *
2529 * RETURNS:
2530 * Zero on success, errno on failure.
2531 */
ea39f835 2532void drm_fb_release(struct drm_file *priv)
f453ba04 2533{
f453ba04
DA
2534 struct drm_device *dev = priv->minor->dev;
2535 struct drm_framebuffer *fb, *tfb;
2536
84849903 2537 drm_modeset_lock_all(dev);
f453ba04 2538 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
f7eff60e 2539 drm_framebuffer_remove(fb);
f453ba04 2540 }
84849903 2541 drm_modeset_unlock_all(dev);
f453ba04
DA
2542}
2543
2544/**
2545 * drm_mode_attachmode - add a mode to the user mode list
2546 * @dev: DRM device
2547 * @connector: connector to add the mode to
2548 * @mode: mode to add
2549 *
2550 * Add @mode to @connector's user mode list.
2551 */
1dd6c8bd
VS
2552static void drm_mode_attachmode(struct drm_device *dev,
2553 struct drm_connector *connector,
2554 struct drm_display_mode *mode)
f453ba04 2555{
f453ba04 2556 list_add_tail(&mode->head, &connector->user_modes);
f453ba04
DA
2557}
2558
2559int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
ac235daf 2560 const struct drm_display_mode *mode)
f453ba04
DA
2561{
2562 struct drm_connector *connector;
ac235daf
VS
2563 int ret = 0;
2564 struct drm_display_mode *dup_mode, *next;
2565 LIST_HEAD(list);
2566
f453ba04
DA
2567 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2568 if (!connector->encoder)
ac235daf 2569 continue;
f453ba04 2570 if (connector->encoder->crtc == crtc) {
ac235daf
VS
2571 dup_mode = drm_mode_duplicate(dev, mode);
2572 if (!dup_mode) {
2573 ret = -ENOMEM;
2574 goto out;
2575 }
2576 list_add_tail(&dup_mode->head, &list);
f453ba04
DA
2577 }
2578 }
ac235daf
VS
2579
2580 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2581 if (!connector->encoder)
2582 continue;
2583 if (connector->encoder->crtc == crtc)
2584 list_move_tail(list.next, &connector->user_modes);
2585 }
2586
2587 WARN_ON(!list_empty(&list));
2588
2589 out:
2590 list_for_each_entry_safe(dup_mode, next, &list, head)
2591 drm_mode_destroy(dev, dup_mode);
2592
2593 return ret;
f453ba04
DA
2594}
2595EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2596
2597static int drm_mode_detachmode(struct drm_device *dev,
2598 struct drm_connector *connector,
2599 struct drm_display_mode *mode)
2600{
2601 int found = 0;
2602 int ret = 0;
2603 struct drm_display_mode *match_mode, *t;
2604
2605 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2606 if (drm_mode_equal(match_mode, mode)) {
2607 list_del(&match_mode->head);
2608 drm_mode_destroy(dev, match_mode);
2609 found = 1;
2610 break;
2611 }
2612 }
2613
2614 if (!found)
2615 ret = -EINVAL;
2616
2617 return ret;
2618}
2619
2620int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2621{
2622 struct drm_connector *connector;
2623
2624 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2625 drm_mode_detachmode(dev, connector, mode);
2626 }
2627 return 0;
2628}
2629EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2630
2631/**
2632 * drm_fb_attachmode - Attach a user mode to an connector
065a50ed
DV
2633 * @dev: drm device for the ioctl
2634 * @data: data pointer for the ioctl
2635 * @file_priv: drm file for the ioctl call
f453ba04
DA
2636 *
2637 * This attaches a user specified mode to an connector.
2638 * Called by the user via ioctl.
2639 *
2640 * RETURNS:
2641 * Zero on success, errno on failure.
2642 */
2643int drm_mode_attachmode_ioctl(struct drm_device *dev,
2644 void *data, struct drm_file *file_priv)
2645{
2646 struct drm_mode_mode_cmd *mode_cmd = data;
2647 struct drm_connector *connector;
2648 struct drm_display_mode *mode;
2649 struct drm_mode_object *obj;
2650 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
4a1b0714 2651 int ret;
f453ba04 2652
fb3b06c8
DA
2653 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2654 return -EINVAL;
2655
84849903 2656 drm_modeset_lock_all(dev);
f453ba04
DA
2657
2658 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2659 if (!obj) {
2660 ret = -EINVAL;
2661 goto out;
2662 }
2663 connector = obj_to_connector(obj);
2664
2665 mode = drm_mode_create(dev);
2666 if (!mode) {
2667 ret = -ENOMEM;
2668 goto out;
2669 }
2670
90367bf6
VS
2671 ret = drm_crtc_convert_umode(mode, umode);
2672 if (ret) {
2673 DRM_DEBUG_KMS("Invalid mode\n");
2674 drm_mode_destroy(dev, mode);
2675 goto out;
2676 }
f453ba04 2677
1dd6c8bd 2678 drm_mode_attachmode(dev, connector, mode);
f453ba04 2679out:
84849903 2680 drm_modeset_unlock_all(dev);
f453ba04
DA
2681 return ret;
2682}
2683
2684
2685/**
2686 * drm_fb_detachmode - Detach a user specified mode from an connector
065a50ed
DV
2687 * @dev: drm device for the ioctl
2688 * @data: data pointer for the ioctl
2689 * @file_priv: drm file for the ioctl call
f453ba04
DA
2690 *
2691 * Called by the user via ioctl.
2692 *
2693 * RETURNS:
2694 * Zero on success, errno on failure.
2695 */
2696int drm_mode_detachmode_ioctl(struct drm_device *dev,
2697 void *data, struct drm_file *file_priv)
2698{
2699 struct drm_mode_object *obj;
2700 struct drm_mode_mode_cmd *mode_cmd = data;
2701 struct drm_connector *connector;
2702 struct drm_display_mode mode;
2703 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
4a1b0714 2704 int ret;
f453ba04 2705
fb3b06c8
DA
2706 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2707 return -EINVAL;
2708
84849903 2709 drm_modeset_lock_all(dev);
f453ba04
DA
2710
2711 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2712 if (!obj) {
2713 ret = -EINVAL;
2714 goto out;
2715 }
2716 connector = obj_to_connector(obj);
2717
90367bf6
VS
2718 ret = drm_crtc_convert_umode(&mode, umode);
2719 if (ret) {
2720 DRM_DEBUG_KMS("Invalid mode\n");
2721 goto out;
2722 }
2723
f453ba04
DA
2724 ret = drm_mode_detachmode(dev, connector, &mode);
2725out:
84849903 2726 drm_modeset_unlock_all(dev);
f453ba04
DA
2727 return ret;
2728}
2729
2730struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2731 const char *name, int num_values)
2732{
2733 struct drm_property *property = NULL;
6bfc56aa 2734 int ret;
f453ba04
DA
2735
2736 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2737 if (!property)
2738 return NULL;
2739
2740 if (num_values) {
2741 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2742 if (!property->values)
2743 goto fail;
2744 }
2745
6bfc56aa
VS
2746 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2747 if (ret)
2748 goto fail;
2749
f453ba04
DA
2750 property->flags = flags;
2751 property->num_values = num_values;
2752 INIT_LIST_HEAD(&property->enum_blob_list);
2753
471dd2ef 2754 if (name) {
f453ba04 2755 strncpy(property->name, name, DRM_PROP_NAME_LEN);
471dd2ef
VL
2756 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2757 }
f453ba04
DA
2758
2759 list_add_tail(&property->head, &dev->mode_config.property_list);
2760 return property;
2761fail:
6bfc56aa 2762 kfree(property->values);
f453ba04
DA
2763 kfree(property);
2764 return NULL;
2765}
2766EXPORT_SYMBOL(drm_property_create);
2767
4a67d391
SH
2768struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2769 const char *name,
2770 const struct drm_prop_enum_list *props,
2771 int num_values)
2772{
2773 struct drm_property *property;
2774 int i, ret;
2775
2776 flags |= DRM_MODE_PROP_ENUM;
2777
2778 property = drm_property_create(dev, flags, name, num_values);
2779 if (!property)
2780 return NULL;
2781
2782 for (i = 0; i < num_values; i++) {
2783 ret = drm_property_add_enum(property, i,
2784 props[i].type,
2785 props[i].name);
2786 if (ret) {
2787 drm_property_destroy(dev, property);
2788 return NULL;
2789 }
2790 }
2791
2792 return property;
2793}
2794EXPORT_SYMBOL(drm_property_create_enum);
2795
49e27545
RC
2796struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2797 int flags, const char *name,
2798 const struct drm_prop_enum_list *props,
2799 int num_values)
2800{
2801 struct drm_property *property;
2802 int i, ret;
2803
2804 flags |= DRM_MODE_PROP_BITMASK;
2805
2806 property = drm_property_create(dev, flags, name, num_values);
2807 if (!property)
2808 return NULL;
2809
2810 for (i = 0; i < num_values; i++) {
2811 ret = drm_property_add_enum(property, i,
2812 props[i].type,
2813 props[i].name);
2814 if (ret) {
2815 drm_property_destroy(dev, property);
2816 return NULL;
2817 }
2818 }
2819
2820 return property;
2821}
2822EXPORT_SYMBOL(drm_property_create_bitmask);
2823
d9bc3c02
SH
2824struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2825 const char *name,
2826 uint64_t min, uint64_t max)
2827{
2828 struct drm_property *property;
2829
2830 flags |= DRM_MODE_PROP_RANGE;
2831
2832 property = drm_property_create(dev, flags, name, 2);
2833 if (!property)
2834 return NULL;
2835
2836 property->values[0] = min;
2837 property->values[1] = max;
2838
2839 return property;
2840}
2841EXPORT_SYMBOL(drm_property_create_range);
2842
f453ba04
DA
2843int drm_property_add_enum(struct drm_property *property, int index,
2844 uint64_t value, const char *name)
2845{
2846 struct drm_property_enum *prop_enum;
2847
49e27545
RC
2848 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2849 return -EINVAL;
2850
2851 /*
2852 * Bitmask enum properties have the additional constraint of values
2853 * from 0 to 63
2854 */
2855 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
f453ba04
DA
2856 return -EINVAL;
2857
2858 if (!list_empty(&property->enum_blob_list)) {
2859 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2860 if (prop_enum->value == value) {
2861 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2862 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2863 return 0;
2864 }
2865 }
2866 }
2867
2868 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2869 if (!prop_enum)
2870 return -ENOMEM;
2871
2872 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2873 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2874 prop_enum->value = value;
2875
2876 property->values[index] = value;
2877 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2878 return 0;
2879}
2880EXPORT_SYMBOL(drm_property_add_enum);
2881
2882void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2883{
2884 struct drm_property_enum *prop_enum, *pt;
2885
2886 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2887 list_del(&prop_enum->head);
2888 kfree(prop_enum);
2889 }
2890
2891 if (property->num_values)
2892 kfree(property->values);
2893 drm_mode_object_put(dev, &property->base);
2894 list_del(&property->head);
2895 kfree(property);
2896}
2897EXPORT_SYMBOL(drm_property_destroy);
2898
c543188a
PZ
2899void drm_object_attach_property(struct drm_mode_object *obj,
2900 struct drm_property *property,
2901 uint64_t init_val)
2902{
7f88a9be 2903 int count = obj->properties->count;
c543188a 2904
7f88a9be
PZ
2905 if (count == DRM_OBJECT_MAX_PROPERTY) {
2906 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2907 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2908 "you see this message on the same object type.\n",
2909 obj->type);
2910 return;
c543188a
PZ
2911 }
2912
7f88a9be
PZ
2913 obj->properties->ids[count] = property->base.id;
2914 obj->properties->values[count] = init_val;
2915 obj->properties->count++;
c543188a
PZ
2916}
2917EXPORT_SYMBOL(drm_object_attach_property);
2918
2919int drm_object_property_set_value(struct drm_mode_object *obj,
2920 struct drm_property *property, uint64_t val)
2921{
2922 int i;
2923
7f88a9be 2924 for (i = 0; i < obj->properties->count; i++) {
c543188a
PZ
2925 if (obj->properties->ids[i] == property->base.id) {
2926 obj->properties->values[i] = val;
2927 return 0;
2928 }
2929 }
2930
2931 return -EINVAL;
2932}
2933EXPORT_SYMBOL(drm_object_property_set_value);
2934
2935int drm_object_property_get_value(struct drm_mode_object *obj,
2936 struct drm_property *property, uint64_t *val)
2937{
2938 int i;
2939
7f88a9be 2940 for (i = 0; i < obj->properties->count; i++) {
c543188a
PZ
2941 if (obj->properties->ids[i] == property->base.id) {
2942 *val = obj->properties->values[i];
2943 return 0;
2944 }
2945 }
2946
2947 return -EINVAL;
2948}
2949EXPORT_SYMBOL(drm_object_property_get_value);
2950
f453ba04
DA
2951int drm_mode_getproperty_ioctl(struct drm_device *dev,
2952 void *data, struct drm_file *file_priv)
2953{
2954 struct drm_mode_object *obj;
2955 struct drm_mode_get_property *out_resp = data;
2956 struct drm_property *property;
2957 int enum_count = 0;
2958 int blob_count = 0;
2959 int value_count = 0;
2960 int ret = 0, i;
2961 int copied;
2962 struct drm_property_enum *prop_enum;
2963 struct drm_mode_property_enum __user *enum_ptr;
2964 struct drm_property_blob *prop_blob;
81f6c7f8 2965 uint32_t __user *blob_id_ptr;
f453ba04
DA
2966 uint64_t __user *values_ptr;
2967 uint32_t __user *blob_length_ptr;
2968
fb3b06c8
DA
2969 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2970 return -EINVAL;
2971
84849903 2972 drm_modeset_lock_all(dev);
f453ba04
DA
2973 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2974 if (!obj) {
2975 ret = -EINVAL;
2976 goto done;
2977 }
2978 property = obj_to_property(obj);
2979
49e27545 2980 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
f453ba04
DA
2981 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2982 enum_count++;
2983 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2984 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2985 blob_count++;
2986 }
2987
2988 value_count = property->num_values;
2989
2990 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2991 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2992 out_resp->flags = property->flags;
2993
2994 if ((out_resp->count_values >= value_count) && value_count) {
81f6c7f8 2995 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
f453ba04
DA
2996 for (i = 0; i < value_count; i++) {
2997 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2998 ret = -EFAULT;
2999 goto done;
3000 }
3001 }
3002 }
3003 out_resp->count_values = value_count;
3004
49e27545 3005 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
f453ba04
DA
3006 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3007 copied = 0;
81f6c7f8 3008 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
f453ba04
DA
3009 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3010
3011 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3012 ret = -EFAULT;
3013 goto done;
3014 }
3015
3016 if (copy_to_user(&enum_ptr[copied].name,
3017 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3018 ret = -EFAULT;
3019 goto done;
3020 }
3021 copied++;
3022 }
3023 }
3024 out_resp->count_enum_blobs = enum_count;
3025 }
3026
3027 if (property->flags & DRM_MODE_PROP_BLOB) {
3028 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3029 copied = 0;
81f6c7f8
VS
3030 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3031 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
f453ba04
DA
3032
3033 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3034 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3035 ret = -EFAULT;
3036 goto done;
3037 }
3038
3039 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3040 ret = -EFAULT;
3041 goto done;
3042 }
3043
3044 copied++;
3045 }
3046 }
3047 out_resp->count_enum_blobs = blob_count;
3048 }
3049done:
84849903 3050 drm_modeset_unlock_all(dev);
f453ba04
DA
3051 return ret;
3052}
3053
3054static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3055 void *data)
3056{
3057 struct drm_property_blob *blob;
6bfc56aa 3058 int ret;
f453ba04
DA
3059
3060 if (!length || !data)
3061 return NULL;
3062
3063 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3064 if (!blob)
3065 return NULL;
3066
6bfc56aa
VS
3067 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3068 if (ret) {
3069 kfree(blob);
3070 return NULL;
3071 }
3072
f453ba04
DA
3073 blob->length = length;
3074
3075 memcpy(blob->data, data, length);
3076
f453ba04
DA
3077 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3078 return blob;
3079}
3080
3081static void drm_property_destroy_blob(struct drm_device *dev,
3082 struct drm_property_blob *blob)
3083{
3084 drm_mode_object_put(dev, &blob->base);
3085 list_del(&blob->head);
3086 kfree(blob);
3087}
3088
3089int drm_mode_getblob_ioctl(struct drm_device *dev,
3090 void *data, struct drm_file *file_priv)
3091{
3092 struct drm_mode_object *obj;
3093 struct drm_mode_get_blob *out_resp = data;
3094 struct drm_property_blob *blob;
3095 int ret = 0;
81f6c7f8 3096 void __user *blob_ptr;
f453ba04 3097
fb3b06c8
DA
3098 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3099 return -EINVAL;
3100
84849903 3101 drm_modeset_lock_all(dev);
f453ba04
DA
3102 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3103 if (!obj) {
3104 ret = -EINVAL;
3105 goto done;
3106 }
3107 blob = obj_to_blob(obj);
3108
3109 if (out_resp->length == blob->length) {
81f6c7f8 3110 blob_ptr = (void __user *)(unsigned long)out_resp->data;
f453ba04
DA
3111 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3112 ret = -EFAULT;
3113 goto done;
3114 }
3115 }
3116 out_resp->length = blob->length;
3117
3118done:
84849903 3119 drm_modeset_unlock_all(dev);
f453ba04
DA
3120 return ret;
3121}
3122
3123int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3124 struct edid *edid)
3125{
3126 struct drm_device *dev = connector->dev;
4a1b0714 3127 int ret, size;
f453ba04
DA
3128
3129 if (connector->edid_blob_ptr)
3130 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3131
3132 /* Delete edid, when there is none. */
3133 if (!edid) {
3134 connector->edid_blob_ptr = NULL;
58495563 3135 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
f453ba04
DA
3136 return ret;
3137 }
3138
7466f4cc
AJ
3139 size = EDID_LENGTH * (1 + edid->extensions);
3140 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3141 size, edid);
e655d122
SK
3142 if (!connector->edid_blob_ptr)
3143 return -EINVAL;
f453ba04 3144
58495563 3145 ret = drm_object_property_set_value(&connector->base,
f453ba04
DA
3146 dev->mode_config.edid_property,
3147 connector->edid_blob_ptr->base.id);
3148
3149 return ret;
3150}
3151EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3152
26a34815 3153static bool drm_property_change_is_valid(struct drm_property *property,
592c20ee 3154 uint64_t value)
26a34815
PZ
3155{
3156 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3157 return false;
3158 if (property->flags & DRM_MODE_PROP_RANGE) {
3159 if (value < property->values[0] || value > property->values[1])
3160 return false;
3161 return true;
49e27545
RC
3162 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3163 int i;
592c20ee 3164 uint64_t valid_mask = 0;
49e27545
RC
3165 for (i = 0; i < property->num_values; i++)
3166 valid_mask |= (1ULL << property->values[i]);
3167 return !(value & ~valid_mask);
c4a56750
VS
3168 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3169 /* Only the driver knows */
3170 return true;
26a34815
PZ
3171 } else {
3172 int i;
3173 for (i = 0; i < property->num_values; i++)
3174 if (property->values[i] == value)
3175 return true;
3176 return false;
3177 }
3178}
3179
f453ba04
DA
3180int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3181 void *data, struct drm_file *file_priv)
3182{
0057d8dd
PZ
3183 struct drm_mode_connector_set_property *conn_set_prop = data;
3184 struct drm_mode_obj_set_property obj_set_prop = {
3185 .value = conn_set_prop->value,
3186 .prop_id = conn_set_prop->prop_id,
3187 .obj_id = conn_set_prop->connector_id,
3188 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3189 };
fb3b06c8 3190
0057d8dd
PZ
3191 /* It does all the locking and checking we need */
3192 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
f453ba04
DA
3193}
3194
c543188a
PZ
3195static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3196 struct drm_property *property,
3197 uint64_t value)
3198{
3199 int ret = -EINVAL;
3200 struct drm_connector *connector = obj_to_connector(obj);
3201
3202 /* Do DPMS ourselves */
3203 if (property == connector->dev->mode_config.dpms_property) {
3204 if (connector->funcs->dpms)
3205 (*connector->funcs->dpms)(connector, (int)value);
3206 ret = 0;
3207 } else if (connector->funcs->set_property)
3208 ret = connector->funcs->set_property(connector, property, value);
3209
3210 /* store the property value if successful */
3211 if (!ret)
58495563 3212 drm_object_property_set_value(&connector->base, property, value);
c543188a
PZ
3213 return ret;
3214}
3215
bffd9de0
PZ
3216static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3217 struct drm_property *property,
3218 uint64_t value)
3219{
3220 int ret = -EINVAL;
3221 struct drm_crtc *crtc = obj_to_crtc(obj);
3222
3223 if (crtc->funcs->set_property)
3224 ret = crtc->funcs->set_property(crtc, property, value);
3225 if (!ret)
3226 drm_object_property_set_value(obj, property, value);
3227
3228 return ret;
3229}
3230
4d93914a
RC
3231static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3232 struct drm_property *property,
3233 uint64_t value)
3234{
3235 int ret = -EINVAL;
3236 struct drm_plane *plane = obj_to_plane(obj);
3237
3238 if (plane->funcs->set_property)
3239 ret = plane->funcs->set_property(plane, property, value);
3240 if (!ret)
3241 drm_object_property_set_value(obj, property, value);
3242
3243 return ret;
3244}
3245
c543188a
PZ
3246int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3247 struct drm_file *file_priv)
3248{
3249 struct drm_mode_obj_get_properties *arg = data;
3250 struct drm_mode_object *obj;
3251 int ret = 0;
3252 int i;
3253 int copied = 0;
3254 int props_count = 0;
3255 uint32_t __user *props_ptr;
3256 uint64_t __user *prop_values_ptr;
3257
3258 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3259 return -EINVAL;
3260
84849903 3261 drm_modeset_lock_all(dev);
c543188a
PZ
3262
3263 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3264 if (!obj) {
3265 ret = -EINVAL;
3266 goto out;
3267 }
3268 if (!obj->properties) {
3269 ret = -EINVAL;
3270 goto out;
3271 }
3272
7f88a9be 3273 props_count = obj->properties->count;
c543188a
PZ
3274
3275 /* This ioctl is called twice, once to determine how much space is
3276 * needed, and the 2nd time to fill it. */
3277 if ((arg->count_props >= props_count) && props_count) {
3278 copied = 0;
3279 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3280 prop_values_ptr = (uint64_t __user *)(unsigned long)
3281 (arg->prop_values_ptr);
3282 for (i = 0; i < props_count; i++) {
3283 if (put_user(obj->properties->ids[i],
3284 props_ptr + copied)) {
3285 ret = -EFAULT;
3286 goto out;
3287 }
3288 if (put_user(obj->properties->values[i],
3289 prop_values_ptr + copied)) {
3290 ret = -EFAULT;
3291 goto out;
3292 }
3293 copied++;
3294 }
3295 }
3296 arg->count_props = props_count;
3297out:
84849903 3298 drm_modeset_unlock_all(dev);
c543188a
PZ
3299 return ret;
3300}
3301
3302int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3303 struct drm_file *file_priv)
3304{
3305 struct drm_mode_obj_set_property *arg = data;
3306 struct drm_mode_object *arg_obj;
3307 struct drm_mode_object *prop_obj;
3308 struct drm_property *property;
3309 int ret = -EINVAL;
3310 int i;
3311
3312 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3313 return -EINVAL;
3314
84849903 3315 drm_modeset_lock_all(dev);
c543188a
PZ
3316
3317 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3318 if (!arg_obj)
3319 goto out;
3320 if (!arg_obj->properties)
3321 goto out;
3322
7f88a9be 3323 for (i = 0; i < arg_obj->properties->count; i++)
c543188a
PZ
3324 if (arg_obj->properties->ids[i] == arg->prop_id)
3325 break;
3326
7f88a9be 3327 if (i == arg_obj->properties->count)
c543188a
PZ
3328 goto out;
3329
3330 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3331 DRM_MODE_OBJECT_PROPERTY);
3332 if (!prop_obj)
3333 goto out;
3334 property = obj_to_property(prop_obj);
3335
3336 if (!drm_property_change_is_valid(property, arg->value))
3337 goto out;
3338
3339 switch (arg_obj->type) {
3340 case DRM_MODE_OBJECT_CONNECTOR:
3341 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3342 arg->value);
3343 break;
bffd9de0
PZ
3344 case DRM_MODE_OBJECT_CRTC:
3345 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3346 break;
4d93914a
RC
3347 case DRM_MODE_OBJECT_PLANE:
3348 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3349 break;
c543188a
PZ
3350 }
3351
3352out:
84849903 3353 drm_modeset_unlock_all(dev);
c543188a
PZ
3354 return ret;
3355}
3356
f453ba04
DA
3357int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3358 struct drm_encoder *encoder)
3359{
3360 int i;
3361
3362 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3363 if (connector->encoder_ids[i] == 0) {
3364 connector->encoder_ids[i] = encoder->base.id;
3365 return 0;
3366 }
3367 }
3368 return -ENOMEM;
3369}
3370EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3371
3372void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3373 struct drm_encoder *encoder)
3374{
3375 int i;
3376 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3377 if (connector->encoder_ids[i] == encoder->base.id) {
3378 connector->encoder_ids[i] = 0;
3379 if (connector->encoder == encoder)
3380 connector->encoder = NULL;
3381 break;
3382 }
3383 }
3384}
3385EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3386
4cae5b84 3387int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
f453ba04
DA
3388 int gamma_size)
3389{
3390 crtc->gamma_size = gamma_size;
3391
3392 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3393 if (!crtc->gamma_store) {
3394 crtc->gamma_size = 0;
4cae5b84 3395 return -ENOMEM;
f453ba04
DA
3396 }
3397
4cae5b84 3398 return 0;
f453ba04
DA
3399}
3400EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3401
3402int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3403 void *data, struct drm_file *file_priv)
3404{
3405 struct drm_mode_crtc_lut *crtc_lut = data;
3406 struct drm_mode_object *obj;
3407 struct drm_crtc *crtc;
3408 void *r_base, *g_base, *b_base;
3409 int size;
3410 int ret = 0;
3411
fb3b06c8
DA
3412 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3413 return -EINVAL;
3414
84849903 3415 drm_modeset_lock_all(dev);
f453ba04
DA
3416 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3417 if (!obj) {
3418 ret = -EINVAL;
3419 goto out;
3420 }
3421 crtc = obj_to_crtc(obj);
3422
ebe0f244
LP
3423 if (crtc->funcs->gamma_set == NULL) {
3424 ret = -ENOSYS;
3425 goto out;
3426 }
3427
f453ba04
DA
3428 /* memcpy into gamma store */
3429 if (crtc_lut->gamma_size != crtc->gamma_size) {
3430 ret = -EINVAL;
3431 goto out;
3432 }
3433
3434 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3435 r_base = crtc->gamma_store;
3436 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3437 ret = -EFAULT;
3438 goto out;
3439 }
3440
3441 g_base = r_base + size;
3442 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3443 ret = -EFAULT;
3444 goto out;
3445 }
3446
3447 b_base = g_base + size;
3448 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3449 ret = -EFAULT;
3450 goto out;
3451 }
3452
7203425a 3453 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
f453ba04
DA
3454
3455out:
84849903 3456 drm_modeset_unlock_all(dev);
f453ba04
DA
3457 return ret;
3458
3459}
3460
3461int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3462 void *data, struct drm_file *file_priv)
3463{
3464 struct drm_mode_crtc_lut *crtc_lut = data;
3465 struct drm_mode_object *obj;
3466 struct drm_crtc *crtc;
3467 void *r_base, *g_base, *b_base;
3468 int size;
3469 int ret = 0;
3470
fb3b06c8
DA
3471 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3472 return -EINVAL;
3473
84849903 3474 drm_modeset_lock_all(dev);
f453ba04
DA
3475 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3476 if (!obj) {
3477 ret = -EINVAL;
3478 goto out;
3479 }
3480 crtc = obj_to_crtc(obj);
3481
3482 /* memcpy into gamma store */
3483 if (crtc_lut->gamma_size != crtc->gamma_size) {
3484 ret = -EINVAL;
3485 goto out;
3486 }
3487
3488 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3489 r_base = crtc->gamma_store;
3490 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3491 ret = -EFAULT;
3492 goto out;
3493 }
3494
3495 g_base = r_base + size;
3496 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3497 ret = -EFAULT;
3498 goto out;
3499 }
3500
3501 b_base = g_base + size;
3502 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3503 ret = -EFAULT;
3504 goto out;
3505 }
3506out:
84849903 3507 drm_modeset_unlock_all(dev);
f453ba04
DA
3508 return ret;
3509}
d91d8a3f
KH
3510
3511int drm_mode_page_flip_ioctl(struct drm_device *dev,
3512 void *data, struct drm_file *file_priv)
3513{
3514 struct drm_mode_crtc_page_flip *page_flip = data;
3515 struct drm_mode_object *obj;
3516 struct drm_crtc *crtc;
3517 struct drm_framebuffer *fb;
3518 struct drm_pending_vblank_event *e = NULL;
3519 unsigned long flags;
7c80e128 3520 int hdisplay, vdisplay;
d91d8a3f
KH
3521 int ret = -EINVAL;
3522
3523 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3524 page_flip->reserved != 0)
3525 return -EINVAL;
3526
84849903 3527 drm_modeset_lock_all(dev);
d91d8a3f
KH
3528 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3529 if (!obj)
3530 goto out;
3531 crtc = obj_to_crtc(obj);
3532
90c1efdd
CW
3533 if (crtc->fb == NULL) {
3534 /* The framebuffer is currently unbound, presumably
3535 * due to a hotplug event, that userspace has not
3536 * yet discovered.
3537 */
3538 ret = -EBUSY;
3539 goto out;
3540 }
3541
d91d8a3f
KH
3542 if (crtc->funcs->page_flip == NULL)
3543 goto out;
3544
3545 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3546 if (!obj)
3547 goto out;
3548 fb = obj_to_fb(obj);
3549
7c80e128
RC
3550 hdisplay = crtc->mode.hdisplay;
3551 vdisplay = crtc->mode.vdisplay;
3552
3553 if (crtc->invert_dimensions)
3554 swap(hdisplay, vdisplay);
3555
3556 if (hdisplay > fb->width ||
3557 vdisplay > fb->height ||
3558 crtc->x > fb->width - hdisplay ||
3559 crtc->y > fb->height - vdisplay) {
3560 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3561 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3562 crtc->invert_dimensions ? " (inverted)" : "");
5f61bb42
VS
3563 ret = -ENOSPC;
3564 goto out;
3565 }
3566
d91d8a3f
KH
3567 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3568 ret = -ENOMEM;
3569 spin_lock_irqsave(&dev->event_lock, flags);
3570 if (file_priv->event_space < sizeof e->event) {
3571 spin_unlock_irqrestore(&dev->event_lock, flags);
3572 goto out;
3573 }
3574 file_priv->event_space -= sizeof e->event;
3575 spin_unlock_irqrestore(&dev->event_lock, flags);
3576
3577 e = kzalloc(sizeof *e, GFP_KERNEL);
3578 if (e == NULL) {
3579 spin_lock_irqsave(&dev->event_lock, flags);
3580 file_priv->event_space += sizeof e->event;
3581 spin_unlock_irqrestore(&dev->event_lock, flags);
3582 goto out;
3583 }
3584
7bd4d7be 3585 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
d91d8a3f
KH
3586 e->event.base.length = sizeof e->event;
3587 e->event.user_data = page_flip->user_data;
3588 e->base.event = &e->event.base;
3589 e->base.file_priv = file_priv;
3590 e->base.destroy =
3591 (void (*) (struct drm_pending_event *)) kfree;
3592 }
3593
3594 ret = crtc->funcs->page_flip(crtc, fb, e);
3595 if (ret) {
aef6a7ee
JS
3596 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3597 spin_lock_irqsave(&dev->event_lock, flags);
3598 file_priv->event_space += sizeof e->event;
3599 spin_unlock_irqrestore(&dev->event_lock, flags);
3600 kfree(e);
3601 }
d91d8a3f
KH
3602 }
3603
3604out:
84849903 3605 drm_modeset_unlock_all(dev);
d91d8a3f
KH
3606 return ret;
3607}
eb033556
CW
3608
3609void drm_mode_config_reset(struct drm_device *dev)
3610{
3611 struct drm_crtc *crtc;
3612 struct drm_encoder *encoder;
3613 struct drm_connector *connector;
3614
3615 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3616 if (crtc->funcs->reset)
3617 crtc->funcs->reset(crtc);
3618
3619 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3620 if (encoder->funcs->reset)
3621 encoder->funcs->reset(encoder);
3622
5e2cb2f6
DV
3623 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3624 connector->status = connector_status_unknown;
3625
eb033556
CW
3626 if (connector->funcs->reset)
3627 connector->funcs->reset(connector);
5e2cb2f6 3628 }
eb033556
CW
3629}
3630EXPORT_SYMBOL(drm_mode_config_reset);
ff72145b
DA
3631
3632int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3633 void *data, struct drm_file *file_priv)
3634{
3635 struct drm_mode_create_dumb *args = data;
3636
3637 if (!dev->driver->dumb_create)
3638 return -ENOSYS;
3639 return dev->driver->dumb_create(file_priv, dev, args);
3640}
3641
3642int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3643 void *data, struct drm_file *file_priv)
3644{
3645 struct drm_mode_map_dumb *args = data;
3646
3647 /* call driver ioctl to get mmap offset */
3648 if (!dev->driver->dumb_map_offset)
3649 return -ENOSYS;
3650
3651 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3652}
3653
3654int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3655 void *data, struct drm_file *file_priv)
3656{
3657 struct drm_mode_destroy_dumb *args = data;
3658
3659 if (!dev->driver->dumb_destroy)
3660 return -ENOSYS;
3661
3662 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3663}
248dbc23
DA
3664
3665/*
3666 * Just need to support RGB formats here for compat with code that doesn't
3667 * use pixel formats directly yet.
3668 */
3669void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3670 int *bpp)
3671{
3672 switch (format) {
04b3924d
VS
3673 case DRM_FORMAT_RGB332:
3674 case DRM_FORMAT_BGR233:
248dbc23
DA
3675 *depth = 8;
3676 *bpp = 8;
3677 break;
04b3924d
VS
3678 case DRM_FORMAT_XRGB1555:
3679 case DRM_FORMAT_XBGR1555:
3680 case DRM_FORMAT_RGBX5551:
3681 case DRM_FORMAT_BGRX5551:
3682 case DRM_FORMAT_ARGB1555:
3683 case DRM_FORMAT_ABGR1555:
3684 case DRM_FORMAT_RGBA5551:
3685 case DRM_FORMAT_BGRA5551:
248dbc23
DA
3686 *depth = 15;
3687 *bpp = 16;
3688 break;
04b3924d
VS
3689 case DRM_FORMAT_RGB565:
3690 case DRM_FORMAT_BGR565:
248dbc23
DA
3691 *depth = 16;
3692 *bpp = 16;
3693 break;
04b3924d
VS
3694 case DRM_FORMAT_RGB888:
3695 case DRM_FORMAT_BGR888:
3696 *depth = 24;
3697 *bpp = 24;
3698 break;
3699 case DRM_FORMAT_XRGB8888:
3700 case DRM_FORMAT_XBGR8888:
3701 case DRM_FORMAT_RGBX8888:
3702 case DRM_FORMAT_BGRX8888:
248dbc23
DA
3703 *depth = 24;
3704 *bpp = 32;
3705 break;
04b3924d
VS
3706 case DRM_FORMAT_XRGB2101010:
3707 case DRM_FORMAT_XBGR2101010:
3708 case DRM_FORMAT_RGBX1010102:
3709 case DRM_FORMAT_BGRX1010102:
3710 case DRM_FORMAT_ARGB2101010:
3711 case DRM_FORMAT_ABGR2101010:
3712 case DRM_FORMAT_RGBA1010102:
3713 case DRM_FORMAT_BGRA1010102:
248dbc23
DA
3714 *depth = 30;
3715 *bpp = 32;
3716 break;
04b3924d
VS
3717 case DRM_FORMAT_ARGB8888:
3718 case DRM_FORMAT_ABGR8888:
3719 case DRM_FORMAT_RGBA8888:
3720 case DRM_FORMAT_BGRA8888:
248dbc23
DA
3721 *depth = 32;
3722 *bpp = 32;
3723 break;
3724 default:
3725 DRM_DEBUG_KMS("unsupported pixel format\n");
3726 *depth = 0;
3727 *bpp = 0;
3728 break;
3729 }
3730}
3731EXPORT_SYMBOL(drm_fb_get_bpp_depth);
141670e9
VS
3732
3733/**
3734 * drm_format_num_planes - get the number of planes for format
3735 * @format: pixel format (DRM_FORMAT_*)
3736 *
3737 * RETURNS:
3738 * The number of planes used by the specified pixel format.
3739 */
3740int drm_format_num_planes(uint32_t format)
3741{
3742 switch (format) {
3743 case DRM_FORMAT_YUV410:
3744 case DRM_FORMAT_YVU410:
3745 case DRM_FORMAT_YUV411:
3746 case DRM_FORMAT_YVU411:
3747 case DRM_FORMAT_YUV420:
3748 case DRM_FORMAT_YVU420:
3749 case DRM_FORMAT_YUV422:
3750 case DRM_FORMAT_YVU422:
3751 case DRM_FORMAT_YUV444:
3752 case DRM_FORMAT_YVU444:
3753 return 3;
3754 case DRM_FORMAT_NV12:
3755 case DRM_FORMAT_NV21:
3756 case DRM_FORMAT_NV16:
3757 case DRM_FORMAT_NV61:
ba623f6a
LP
3758 case DRM_FORMAT_NV24:
3759 case DRM_FORMAT_NV42:
141670e9
VS
3760 return 2;
3761 default:
3762 return 1;
3763 }
3764}
3765EXPORT_SYMBOL(drm_format_num_planes);
5a86bd55
VS
3766
3767/**
3768 * drm_format_plane_cpp - determine the bytes per pixel value
3769 * @format: pixel format (DRM_FORMAT_*)
3770 * @plane: plane index
3771 *
3772 * RETURNS:
3773 * The bytes per pixel value for the specified plane.
3774 */
3775int drm_format_plane_cpp(uint32_t format, int plane)
3776{
3777 unsigned int depth;
3778 int bpp;
3779
3780 if (plane >= drm_format_num_planes(format))
3781 return 0;
3782
3783 switch (format) {
3784 case DRM_FORMAT_YUYV:
3785 case DRM_FORMAT_YVYU:
3786 case DRM_FORMAT_UYVY:
3787 case DRM_FORMAT_VYUY:
3788 return 2;
3789 case DRM_FORMAT_NV12:
3790 case DRM_FORMAT_NV21:
3791 case DRM_FORMAT_NV16:
3792 case DRM_FORMAT_NV61:
ba623f6a
LP
3793 case DRM_FORMAT_NV24:
3794 case DRM_FORMAT_NV42:
5a86bd55
VS
3795 return plane ? 2 : 1;
3796 case DRM_FORMAT_YUV410:
3797 case DRM_FORMAT_YVU410:
3798 case DRM_FORMAT_YUV411:
3799 case DRM_FORMAT_YVU411:
3800 case DRM_FORMAT_YUV420:
3801 case DRM_FORMAT_YVU420:
3802 case DRM_FORMAT_YUV422:
3803 case DRM_FORMAT_YVU422:
3804 case DRM_FORMAT_YUV444:
3805 case DRM_FORMAT_YVU444:
3806 return 1;
3807 default:
3808 drm_fb_get_bpp_depth(format, &depth, &bpp);
3809 return bpp >> 3;
3810 }
3811}
3812EXPORT_SYMBOL(drm_format_plane_cpp);
01b68b04
VS
3813
3814/**
3815 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3816 * @format: pixel format (DRM_FORMAT_*)
3817 *
3818 * RETURNS:
3819 * The horizontal chroma subsampling factor for the
3820 * specified pixel format.
3821 */
3822int drm_format_horz_chroma_subsampling(uint32_t format)
3823{
3824 switch (format) {
3825 case DRM_FORMAT_YUV411:
3826 case DRM_FORMAT_YVU411:
3827 case DRM_FORMAT_YUV410:
3828 case DRM_FORMAT_YVU410:
3829 return 4;
3830 case DRM_FORMAT_YUYV:
3831 case DRM_FORMAT_YVYU:
3832 case DRM_FORMAT_UYVY:
3833 case DRM_FORMAT_VYUY:
3834 case DRM_FORMAT_NV12:
3835 case DRM_FORMAT_NV21:
3836 case DRM_FORMAT_NV16:
3837 case DRM_FORMAT_NV61:
3838 case DRM_FORMAT_YUV422:
3839 case DRM_FORMAT_YVU422:
3840 case DRM_FORMAT_YUV420:
3841 case DRM_FORMAT_YVU420:
3842 return 2;
3843 default:
3844 return 1;
3845 }
3846}
3847EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3848
3849/**
3850 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3851 * @format: pixel format (DRM_FORMAT_*)
3852 *
3853 * RETURNS:
3854 * The vertical chroma subsampling factor for the
3855 * specified pixel format.
3856 */
3857int drm_format_vert_chroma_subsampling(uint32_t format)
3858{
3859 switch (format) {
3860 case DRM_FORMAT_YUV410:
3861 case DRM_FORMAT_YVU410:
3862 return 4;
3863 case DRM_FORMAT_YUV420:
3864 case DRM_FORMAT_YVU420:
3865 case DRM_FORMAT_NV12:
3866 case DRM_FORMAT_NV21:
3867 return 2;
3868 default:
3869 return 1;
3870 }
3871}
3872EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);