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