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