[go: nahoru, domu]

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