Linux Audio

Check our new training course

Loading...
v5.9
  1/*
  2 * Copyright (c) 2006-2008 Intel Corporation
  3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4 * Copyright (c) 2008 Red Hat Inc.
  5 *
  6 * DRM core CRTC related functions
  7 *
  8 * Permission to use, copy, modify, distribute, and sell this software and its
  9 * documentation for any purpose is hereby granted without fee, provided that
 10 * the above copyright notice appear in all copies and that both that copyright
 11 * notice and this permission notice appear in supporting documentation, and
 12 * that the name of the copyright holders not be used in advertising or
 13 * publicity pertaining to distribution of the software without specific,
 14 * written prior permission.  The copyright holders make no representations
 15 * about the suitability of this software for any purpose.  It is provided "as
 16 * is" without express or implied warranty.
 17 *
 18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 24 * OF THIS SOFTWARE.
 25 *
 26 * Authors:
 27 *      Keith Packard
 28 *	Eric Anholt <eric@anholt.net>
 29 *      Dave Airlie <airlied@linux.ie>
 30 *      Jesse Barnes <jesse.barnes@intel.com>
 31 */
 32#include <linux/ctype.h>
 33#include <linux/list.h>
 34#include <linux/slab.h>
 35#include <linux/export.h>
 36#include <linux/dma-fence.h>
 37#include <linux/uaccess.h>
 38#include <drm/drm_crtc.h>
 39#include <drm/drm_edid.h>
 40#include <drm/drm_fourcc.h>
 41#include <drm/drm_modeset_lock.h>
 42#include <drm/drm_atomic.h>
 43#include <drm/drm_auth.h>
 44#include <drm/drm_debugfs_crc.h>
 45#include <drm/drm_drv.h>
 46#include <drm/drm_print.h>
 47#include <drm/drm_file.h>
 48
 49#include "drm_crtc_internal.h"
 50#include "drm_internal.h"
 51
 52/**
 53 * DOC: overview
 54 *
 55 * A CRTC represents the overall display pipeline. It receives pixel data from
 56 * &drm_plane and blends them together. The &drm_display_mode is also attached
 57 * to the CRTC, specifying display timings. On the output side the data is fed
 58 * to one or more &drm_encoder, which are then each connected to one
 59 * &drm_connector.
 60 *
 61 * To create a CRTC, a KMS drivers allocates and zeroes an instances of
 62 * &struct drm_crtc (possibly as part of a larger structure) and registers it
 63 * with a call to drm_crtc_init_with_planes().
 64 *
 65 * The CRTC is also the entry point for legacy modeset operations, see
 66 * &drm_crtc_funcs.set_config, legacy plane operations, see
 67 * &drm_crtc_funcs.page_flip and &drm_crtc_funcs.cursor_set2, and other legacy
 68 * operations like &drm_crtc_funcs.gamma_set. For atomic drivers all these
 69 * features are controlled through &drm_property and
 70 * &drm_mode_config_funcs.atomic_check and &drm_mode_config_funcs.atomic_check.
 71 */
 
 
 
 
 
 
 72
 73/**
 74 * drm_crtc_from_index - find the registered CRTC at an index
 75 * @dev: DRM device
 76 * @idx: index of registered CRTC to find for
 77 *
 78 * Given a CRTC index, return the registered CRTC from DRM device's
 79 * list of CRTCs with matching index. This is the inverse of drm_crtc_index().
 80 * It's useful in the vblank callbacks (like &drm_driver.enable_vblank or
 81 * &drm_driver.disable_vblank), since that still deals with indices instead
 82 * of pointers to &struct drm_crtc."
 83 */
 84struct drm_crtc *drm_crtc_from_index(struct drm_device *dev, int idx)
 85{
 86	struct drm_crtc *crtc;
 
 
 
 
 87
 88	drm_for_each_crtc(crtc, dev)
 89		if (idx == crtc->index)
 90			return crtc;
 
 
 
 91
 92	return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 93}
 94EXPORT_SYMBOL(drm_crtc_from_index);
 95
 96int drm_crtc_force_disable(struct drm_crtc *crtc)
 97{
 98	struct drm_mode_set set = {
 99		.crtc = crtc,
100	};
101
102	WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
 
 
 
 
 
103
104	return drm_mode_set_config_internal(&set);
 
 
 
 
 
 
 
105}
106
107static unsigned int drm_num_crtcs(struct drm_device *dev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108{
109	unsigned int num = 0;
110	struct drm_crtc *tmp;
111
112	drm_for_each_crtc(tmp, dev) {
113		num++;
 
 
114	}
115
116	return num;
 
 
 
 
 
 
 
 
117}
118
119int drm_crtc_register_all(struct drm_device *dev)
 
 
 
 
 
 
 
 
 
 
 
120{
121	struct drm_crtc *crtc;
122	int ret = 0;
 
 
 
 
 
 
 
123
124	drm_for_each_crtc(crtc, dev) {
125		drm_debugfs_crtc_add(crtc);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
127		if (crtc->funcs->late_register)
128			ret = crtc->funcs->late_register(crtc);
129		if (ret)
130			return ret;
131	}
132
 
 
 
 
 
133	return 0;
134}
 
135
136void drm_crtc_unregister_all(struct drm_device *dev)
 
 
 
 
 
 
 
 
 
 
137{
 
138	struct drm_crtc *crtc;
 
 
139
140	drm_for_each_crtc(crtc, dev) {
141		if (crtc->funcs->early_unregister)
142			crtc->funcs->early_unregister(crtc);
143		drm_debugfs_crtc_remove(crtc);
 
 
 
 
 
 
 
144	}
 
 
 
 
145}
 
146
147static int drm_crtc_crc_init(struct drm_crtc *crtc)
 
 
 
 
 
 
 
 
 
 
 
 
148{
149#ifdef CONFIG_DEBUG_FS
150	spin_lock_init(&crtc->crc.lock);
151	init_waitqueue_head(&crtc->crc.wq);
152	crtc->crc.source = kstrdup("auto", GFP_KERNEL);
153	if (!crtc->crc.source)
154		return -ENOMEM;
155#endif
156	return 0;
 
157}
 
158
159static void drm_crtc_crc_fini(struct drm_crtc *crtc)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160{
161#ifdef CONFIG_DEBUG_FS
162	kfree(crtc->crc.source);
163#endif
164}
 
165
166static const struct dma_fence_ops drm_crtc_fence_ops;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
168static struct drm_crtc *fence_to_crtc(struct dma_fence *fence)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169{
170	BUG_ON(fence->ops != &drm_crtc_fence_ops);
171	return container_of(fence->lock, struct drm_crtc, fence_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172}
 
173
174static const char *drm_crtc_fence_get_driver_name(struct dma_fence *fence)
 
 
 
 
 
 
 
 
 
175{
176	struct drm_crtc *crtc = fence_to_crtc(fence);
 
 
 
 
177
178	return crtc->dev->driver->name;
 
 
 
 
 
 
 
 
 
 
179}
 
180
181static const char *drm_crtc_fence_get_timeline_name(struct dma_fence *fence)
 
 
 
182{
183	struct drm_crtc *crtc = fence_to_crtc(fence);
 
 
184
185	return crtc->timeline_name;
 
 
 
 
 
 
 
186}
 
187
188static const struct dma_fence_ops drm_crtc_fence_ops = {
189	.get_driver_name = drm_crtc_fence_get_driver_name,
190	.get_timeline_name = drm_crtc_fence_get_timeline_name,
191};
 
 
 
 
 
 
192
193struct dma_fence *drm_crtc_create_fence(struct drm_crtc *crtc)
 
 
 
 
 
 
 
 
 
 
 
 
194{
195	struct dma_fence *fence;
196
197	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
198	if (!fence)
199		return NULL;
200
201	dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
202		       crtc->fence_context, ++crtc->fence_seqno);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
204	return fence;
205}
206
207/**
208 * DOC: standard CRTC properties
 
209 *
210 * DRM CRTCs have a few standardized properties:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211 *
212 * ACTIVE:
213 * 	Atomic property for setting the power state of the CRTC. When set to 1
214 * 	the CRTC will actively display content. When set to 0 the CRTC will be
215 * 	powered off. There is no expectation that user-space will reset CRTC
216 * 	resources like the mode and planes when setting ACTIVE to 0.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217 *
218 * 	User-space can rely on an ACTIVE change to 1 to never fail an atomic
219 * 	test as long as no other property has changed. If a change to ACTIVE
220 * 	fails an atomic test, this is a driver bug. For this reason setting
221 * 	ACTIVE to 0 must not release internal resources (like reserved memory
222 * 	bandwidth or clock generators).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223 *
224 * 	Note that the legacy DPMS property on connectors is internally routed
225 * 	to control this property for atomic drivers.
226 * MODE_ID:
227 * 	Atomic property for setting the CRTC display timings. The value is the
228 * 	ID of a blob containing the DRM mode info. To disable the CRTC,
229 * 	user-space must set this property to 0.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230 *
231 * 	Setting MODE_ID to 0 will release reserved resources for the CRTC.
 
232 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
234/**
235 * drm_crtc_init_with_planes - Initialise a new CRTC object with
236 *    specified primary and cursor planes.
237 * @dev: DRM device
238 * @crtc: CRTC object to init
239 * @primary: Primary plane for CRTC
240 * @cursor: Cursor plane for CRTC
241 * @funcs: callbacks for the new CRTC
242 * @name: printf style format string for the CRTC name, or NULL for default name
243 *
244 * Inits a new object created as base part of a driver crtc object. Drivers
245 * should use this function instead of drm_crtc_init(), which is only provided
246 * for backwards compatibility with drivers which do not yet support universal
247 * planes). For really simple hardware which has only 1 plane look at
248 * drm_simple_display_pipe_init() instead.
249 *
250 * Returns:
251 * Zero on success, error code on failure.
252 */
253int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
254			      struct drm_plane *primary,
255			      struct drm_plane *cursor,
256			      const struct drm_crtc_funcs *funcs,
257			      const char *name, ...)
258{
259	struct drm_mode_config *config = &dev->mode_config;
260	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
262	WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
263	WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
 
264
265	/* crtc index is used with 32bit bitmasks */
266	if (WARN_ON(config->num_crtc >= 32))
267		return -EINVAL;
268
269	WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
270		(!funcs->atomic_destroy_state ||
271		 !funcs->atomic_duplicate_state));
272
273	crtc->dev = dev;
274	crtc->funcs = funcs;
 
 
 
275
276	INIT_LIST_HEAD(&crtc->commit_list);
277	spin_lock_init(&crtc->commit_lock);
 
 
 
 
 
278
279	drm_modeset_lock_init(&crtc->mutex);
280	ret = drm_mode_object_add(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
281	if (ret)
282		return ret;
283
284	if (name) {
285		va_list ap;
286
287		va_start(ap, name);
288		crtc->name = kvasprintf(GFP_KERNEL, name, ap);
289		va_end(ap);
290	} else {
291		crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
292				       drm_num_crtcs(dev));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293	}
294	if (!crtc->name) {
295		drm_mode_object_unregister(dev, &crtc->base);
296		return -ENOMEM;
 
297	}
298
299	crtc->fence_context = dma_fence_context_alloc(1);
300	spin_lock_init(&crtc->fence_lock);
301	snprintf(crtc->timeline_name, sizeof(crtc->timeline_name),
302		 "CRTC:%d-%s", crtc->base.id, crtc->name);
303
304	crtc->base.properties = &crtc->properties;
305
306	list_add_tail(&crtc->head, &config->crtc_list);
307	crtc->index = config->num_crtc++;
308
309	crtc->primary = primary;
310	crtc->cursor = cursor;
311	if (primary && !primary->possible_crtcs)
312		primary->possible_crtcs = drm_crtc_mask(crtc);
313	if (cursor && !cursor->possible_crtcs)
314		cursor->possible_crtcs = drm_crtc_mask(crtc);
315
316	ret = drm_crtc_crc_init(crtc);
317	if (ret) {
318		drm_mode_object_unregister(dev, &crtc->base);
319		return ret;
320	}
321
322	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
323		drm_object_attach_property(&crtc->base, config->prop_active, 0);
324		drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
325		drm_object_attach_property(&crtc->base,
326					   config->prop_out_fence_ptr, 0);
327		drm_object_attach_property(&crtc->base,
328					   config->prop_vrr_enabled, 0);
329	}
330
331	return 0;
332}
333EXPORT_SYMBOL(drm_crtc_init_with_planes);
334
335/**
336 * drm_crtc_cleanup - Clean up the core crtc usage
337 * @crtc: CRTC to cleanup
 
338 *
339 * This function cleans up @crtc and removes it from the DRM mode setting
340 * core. Note that the function does *not* free the crtc structure itself,
341 * this is the responsibility of the caller.
 
 
342 */
343void drm_crtc_cleanup(struct drm_crtc *crtc)
 
344{
345	struct drm_device *dev = crtc->dev;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
347	/* Note that the crtc_list is considered to be static; should we
348	 * remove the drm_crtc at runtime we would have to decrement all
349	 * the indices on the drm_crtc after us in the crtc_list.
 
 
350	 */
 
 
351
352	drm_crtc_crc_fini(crtc);
 
353
354	kfree(crtc->gamma_store);
355	crtc->gamma_store = NULL;
356
357	drm_modeset_lock_fini(&crtc->mutex);
 
358
359	drm_mode_object_unregister(dev, &crtc->base);
360	list_del(&crtc->head);
361	dev->mode_config.num_crtc--;
362
363	WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
364	if (crtc->state && crtc->funcs->atomic_destroy_state)
365		crtc->funcs->atomic_destroy_state(crtc, crtc->state);
 
366
367	kfree(crtc->name);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
368
369	memset(crtc, 0, sizeof(*crtc));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370}
371EXPORT_SYMBOL(drm_crtc_cleanup);
372
373/**
374 * drm_mode_getcrtc - get CRTC configuration
375 * @dev: drm device for the ioctl
376 * @data: data pointer for the ioctl
377 * @file_priv: drm file for the ioctl call
 
 
 
 
378 *
379 * Construct a CRTC configuration structure to return to the user.
380 *
381 * Called by the user via ioctl.
382 *
383 * Returns:
384 * Zero on success, negative errno on failure.
385 */
386int drm_mode_getcrtc(struct drm_device *dev,
387		     void *data, struct drm_file *file_priv)
388{
389	struct drm_mode_crtc *crtc_resp = data;
390	struct drm_crtc *crtc;
391	struct drm_plane *plane;
 
392
393	if (!drm_core_check_feature(dev, DRIVER_MODESET))
394		return -EOPNOTSUPP;
395
396	crtc = drm_crtc_find(dev, file_priv, crtc_resp->crtc_id);
397	if (!crtc)
398		return -ENOENT;
399
400	plane = crtc->primary;
 
 
 
 
 
 
401
 
 
402	crtc_resp->gamma_size = crtc->gamma_size;
403
404	drm_modeset_lock(&plane->mutex, NULL);
405	if (plane->state && plane->state->fb)
406		crtc_resp->fb_id = plane->state->fb->base.id;
407	else if (!plane->state && plane->fb)
408		crtc_resp->fb_id = plane->fb->base.id;
409	else
410		crtc_resp->fb_id = 0;
411
412	if (plane->state) {
413		crtc_resp->x = plane->state->src_x >> 16;
414		crtc_resp->y = plane->state->src_y >> 16;
415	}
416	drm_modeset_unlock(&plane->mutex);
417
418	drm_modeset_lock(&crtc->mutex, NULL);
419	if (crtc->state) {
420		if (crtc->state->enable) {
421			drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
422			crtc_resp->mode_valid = 1;
423		} else {
424			crtc_resp->mode_valid = 0;
425		}
426	} else {
427		crtc_resp->x = crtc->x;
428		crtc_resp->y = crtc->y;
429
430		if (crtc->enabled) {
431			drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
432			crtc_resp->mode_valid = 1;
433
434		} else {
435			crtc_resp->mode_valid = 0;
436		}
437	}
438	if (!file_priv->aspect_ratio_allowed)
439		crtc_resp->mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
440	drm_modeset_unlock(&crtc->mutex);
441
442	return 0;
 
 
443}
444
445static int __drm_mode_set_config_internal(struct drm_mode_set *set,
446					  struct drm_modeset_acquire_ctx *ctx)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
447{
448	struct drm_crtc *crtc = set->crtc;
449	struct drm_framebuffer *fb;
450	struct drm_crtc *tmp;
451	int ret;
 
 
 
 
 
 
 
 
 
 
 
452
453	WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
 
454
455	/*
456	 * NOTE: ->set_config can also disable other crtcs (if we steal all
457	 * connectors from it), hence we need to refcount the fbs across all
458	 * crtcs. Atomic modeset will have saner semantics ...
459	 */
460	drm_for_each_crtc(tmp, crtc->dev) {
461		struct drm_plane *plane = tmp->primary;
462
463		plane->old_fb = plane->fb;
 
 
 
 
464	}
 
465
466	fb = set->fb;
 
 
 
 
467
468	ret = crtc->funcs->set_config(set, ctx);
469	if (ret == 0) {
470		struct drm_plane *plane = crtc->primary;
 
 
471
472		plane->crtc = fb ? crtc : NULL;
473		plane->fb = fb;
 
 
474	}
475
476	drm_for_each_crtc(tmp, crtc->dev) {
477		struct drm_plane *plane = tmp->primary;
 
 
 
 
 
 
 
 
 
 
 
 
 
478
479		if (plane->fb)
480			drm_framebuffer_get(plane->fb);
481		if (plane->old_fb)
482			drm_framebuffer_put(plane->old_fb);
483		plane->old_fb = NULL;
 
 
 
 
 
 
 
 
 
 
 
484	}
 
485
486	return ret;
487}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
488
489/**
490 * drm_mode_set_config_internal - helper to call &drm_mode_config_funcs.set_config
491 * @set: modeset config to set
492 *
493 * This is a little helper to wrap internal calls to the
494 * &drm_mode_config_funcs.set_config driver interface. The only thing it adds is
495 * correct refcounting dance.
496 *
497 * This should only be used by non-atomic legacy drivers.
498 *
499 * Returns:
500 * Zero on success, negative errno on failure.
501 */
502int drm_mode_set_config_internal(struct drm_mode_set *set)
503{
504	WARN_ON(drm_drv_uses_atomic_modeset(set->crtc->dev));
505
506	return __drm_mode_set_config_internal(set, NULL);
 
 
507}
508EXPORT_SYMBOL(drm_mode_set_config_internal);
509
510/**
511 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
512 *     CRTC viewport
513 * @crtc: CRTC that framebuffer will be displayed on
514 * @x: x panning
515 * @y: y panning
516 * @mode: mode that framebuffer will be displayed under
517 * @fb: framebuffer to check size of
518 */
519int drm_crtc_check_viewport(const struct drm_crtc *crtc,
520			    int x, int y,
521			    const struct drm_display_mode *mode,
522			    const struct drm_framebuffer *fb)
523
 
 
524{
525	int hdisplay, vdisplay;
 
 
 
526
527	drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
 
528
529	if (crtc->state &&
530	    drm_rotation_90_or_270(crtc->primary->state->rotation))
531		swap(hdisplay, vdisplay);
 
 
 
 
 
532
533	return drm_framebuffer_check_src_coords(x << 16, y << 16,
534						hdisplay << 16, vdisplay << 16,
535						fb);
 
 
 
 
 
 
 
 
 
536}
537EXPORT_SYMBOL(drm_crtc_check_viewport);
538
539/**
540 * drm_mode_setcrtc - set CRTC configuration
541 * @dev: drm device for the ioctl
542 * @data: data pointer for the ioctl
543 * @file_priv: drm file for the ioctl call
 
 
 
 
544 *
545 * Build a new CRTC configuration based on user request.
546 *
547 * Called by the user via ioctl.
548 *
549 * Returns:
550 * Zero on success, negative errno on failure.
551 */
552int drm_mode_setcrtc(struct drm_device *dev, void *data,
553		     struct drm_file *file_priv)
554{
555	struct drm_mode_config *config = &dev->mode_config;
556	struct drm_mode_crtc *crtc_req = data;
557	struct drm_crtc *crtc;
558	struct drm_plane *plane;
559	struct drm_connector **connector_set = NULL, *connector;
560	struct drm_framebuffer *fb = NULL;
561	struct drm_display_mode *mode = NULL;
562	struct drm_mode_set set;
563	uint32_t __user *set_connectors_ptr;
564	struct drm_modeset_acquire_ctx ctx;
565	int ret;
566	int i;
567
568	if (!drm_core_check_feature(dev, DRIVER_MODESET))
569		return -EOPNOTSUPP;
570
571	/*
572	 * Universal plane src offsets are only 16.16, prevent havoc for
573	 * drivers using universal plane code internally.
574	 */
575	if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
576		return -ERANGE;
577
578	crtc = drm_crtc_find(dev, file_priv, crtc_req->crtc_id);
579	if (!crtc) {
 
 
580		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
581		return -ENOENT;
 
582	}
583	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
584
585	plane = crtc->primary;
586
587	/* allow disabling with the primary plane leased */
588	if (crtc_req->mode_valid && !drm_lease_held(file_priv, plane->base.id))
589		return -EACCES;
590
591	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx,
592				   DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
593
594	if (crtc_req->mode_valid) {
595		/* If we have a mode we need a framebuffer. */
596		/* If we pass -1, set the mode with the currently bound fb */
597		if (crtc_req->fb_id == -1) {
598			struct drm_framebuffer *old_fb;
599
600			if (plane->state)
601				old_fb = plane->state->fb;
602			else
603				old_fb = plane->fb;
604
605			if (!old_fb) {
606				DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
607				ret = -EINVAL;
608				goto out;
609			}
610
611			fb = old_fb;
612			/* Make refcounting symmetric with the lookup path. */
613			drm_framebuffer_get(fb);
614		} else {
615			fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id);
616			if (!fb) {
 
617				DRM_DEBUG_KMS("Unknown FB ID%d\n",
618						crtc_req->fb_id);
619				ret = -ENOENT;
620				goto out;
621			}
 
622		}
623
624		mode = drm_mode_create(dev);
625		if (!mode) {
626			ret = -ENOMEM;
627			goto out;
628		}
629		if (!file_priv->aspect_ratio_allowed &&
630		    (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != DRM_MODE_FLAG_PIC_AR_NONE) {
631			DRM_DEBUG_KMS("Unexpected aspect-ratio flag bits\n");
632			ret = -EINVAL;
633			goto out;
634		}
635
636
637		ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode);
638		if (ret) {
639			DRM_DEBUG_KMS("Invalid mode (ret=%d, status=%s)\n",
640				      ret, drm_get_mode_status_name(mode->status));
641			drm_mode_debug_printmodeline(mode);
642			goto out;
643		}
644
645		/*
646		 * Check whether the primary plane supports the fb pixel format.
647		 * Drivers not implementing the universal planes API use a
648		 * default formats list provided by the DRM core which doesn't
649		 * match real hardware capabilities. Skip the check in that
650		 * case.
651		 */
652		if (!plane->format_default) {
653			ret = drm_plane_check_pixel_format(plane,
654							   fb->format->format,
655							   fb->modifier);
656			if (ret) {
657				struct drm_format_name_buf format_name;
658
659				DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n",
660					      drm_get_format_name(fb->format->format,
661								  &format_name),
662					      fb->modifier);
663				goto out;
664			}
665		}
666
667		ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
668					      mode, fb);
669		if (ret)
670			goto out;
671
672	}
673
674	if (crtc_req->count_connectors == 0 && mode) {
675		DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
676		ret = -EINVAL;
677		goto out;
678	}
679
680	if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
681		DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
682			  crtc_req->count_connectors);
683		ret = -EINVAL;
684		goto out;
685	}
686
687	if (crtc_req->count_connectors > 0) {
688		u32 out_id;
689
690		/* Avoid unbounded kernel memory allocation */
691		if (crtc_req->count_connectors > config->num_connector) {
692			ret = -EINVAL;
693			goto out;
694		}
695
696		connector_set = kmalloc_array(crtc_req->count_connectors,
697					      sizeof(struct drm_connector *),
698					      GFP_KERNEL);
699		if (!connector_set) {
700			ret = -ENOMEM;
701			goto out;
702		}
703
704		for (i = 0; i < crtc_req->count_connectors; i++) {
705			connector_set[i] = NULL;
706			set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
707			if (get_user(out_id, &set_connectors_ptr[i])) {
708				ret = -EFAULT;
709				goto out;
710			}
711
712			connector = drm_connector_lookup(dev, file_priv, out_id);
713			if (!connector) {
 
714				DRM_DEBUG_KMS("Connector id %d unknown\n",
715						out_id);
716				ret = -ENOENT;
717				goto out;
718			}
 
719			DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
720					connector->base.id,
721					connector->name);
722
723			connector_set[i] = connector;
724		}
725	}
726
727	set.crtc = crtc;
728	set.x = crtc_req->x;
729	set.y = crtc_req->y;
730	set.mode = mode;
731	set.connectors = connector_set;
732	set.num_connectors = crtc_req->count_connectors;
733	set.fb = fb;
 
734
735	if (drm_drv_uses_atomic_modeset(dev))
736		ret = crtc->funcs->set_config(&set, &ctx);
737	else
738		ret = __drm_mode_set_config_internal(&set, &ctx);
 
739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740out:
741	if (fb)
742		drm_framebuffer_put(fb);
 
743
744	if (connector_set) {
745		for (i = 0; i < crtc_req->count_connectors; i++) {
746			if (connector_set[i])
747				drm_connector_put(connector_set[i]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
748		}
749	}
750	kfree(connector_set);
751	drm_mode_destroy(dev, mode);
752
753	/* In case we need to retry... */
754	connector_set = NULL;
755	fb = NULL;
756	mode = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
757
758	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760	return ret;
761}
762
763int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
764			       struct drm_property *property,
765			       uint64_t value)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
766{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
767	int ret = -EINVAL;
768	struct drm_crtc *crtc = obj_to_crtc(obj);
769
770	if (crtc->funcs->set_property)
771		ret = crtc->funcs->set_property(crtc, property, value);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
772	if (!ret)
773		drm_object_property_set_value(obj, property, value);
 
 
 
 
 
 
 
 
 
774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
775	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
776}
v3.1
   1/*
   2 * Copyright (c) 2006-2008 Intel Corporation
   3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
   4 * Copyright (c) 2008 Red Hat Inc.
   5 *
   6 * DRM core CRTC related functions
   7 *
   8 * Permission to use, copy, modify, distribute, and sell this software and its
   9 * documentation for any purpose is hereby granted without fee, provided that
  10 * the above copyright notice appear in all copies and that both that copyright
  11 * notice and this permission notice appear in supporting documentation, and
  12 * that the name of the copyright holders not be used in advertising or
  13 * publicity pertaining to distribution of the software without specific,
  14 * written prior permission.  The copyright holders make no representations
  15 * about the suitability of this software for any purpose.  It is provided "as
  16 * is" without express or implied warranty.
  17 *
  18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24 * OF THIS SOFTWARE.
  25 *
  26 * Authors:
  27 *      Keith Packard
  28 *	Eric Anholt <eric@anholt.net>
  29 *      Dave Airlie <airlied@linux.ie>
  30 *      Jesse Barnes <jesse.barnes@intel.com>
  31 */
 
  32#include <linux/list.h>
  33#include <linux/slab.h>
  34#include "drm.h"
  35#include "drmP.h"
  36#include "drm_crtc.h"
  37#include "drm_edid.h"
  38
  39struct drm_prop_enum_list {
  40	int type;
  41	char *name;
  42};
  43
  44/* Avoid boilerplate.  I'm tired of typing. */
  45#define DRM_ENUM_NAME_FN(fnname, list)				\
  46	char *fnname(int val)					\
  47	{							\
  48		int i;						\
  49		for (i = 0; i < ARRAY_SIZE(list); i++) {	\
  50			if (list[i].type == val)		\
  51				return list[i].name;		\
  52		}						\
  53		return "(unknown)";				\
  54	}
  55
  56/*
  57 * Global properties
 
 
 
 
 
 
 
 
 
 
 
 
  58 */
  59static struct drm_prop_enum_list drm_dpms_enum_list[] =
  60{	{ DRM_MODE_DPMS_ON, "On" },
  61	{ DRM_MODE_DPMS_STANDBY, "Standby" },
  62	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
  63	{ DRM_MODE_DPMS_OFF, "Off" }
  64};
  65
  66DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  67
  68/*
  69 * Optional properties
 
 
 
 
 
 
  70 */
  71static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
  72{
  73	{ DRM_MODE_SCALE_NONE, "None" },
  74	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
  75	{ DRM_MODE_SCALE_CENTER, "Center" },
  76	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
  77};
  78
  79static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
  80{
  81	{ DRM_MODE_DITHERING_OFF, "Off" },
  82	{ DRM_MODE_DITHERING_ON, "On" },
  83	{ DRM_MODE_DITHERING_AUTO, "Automatic" },
  84};
  85
  86/*
  87 * Non-global properties, but "required" for certain connectors.
  88 */
  89static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
  90{
  91	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  92	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
  93	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
  94};
  95
  96DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  97
  98static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
  99{
 100	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
 101	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
 102	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
 103};
 104
 105DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
 106		 drm_dvi_i_subconnector_enum_list)
 107
 108static struct drm_prop_enum_list drm_tv_select_enum_list[] =
 109{
 110	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
 111	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
 112	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
 113	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
 114	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
 115};
 116
 117DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
 118
 119static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
 120{
 121	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
 122	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
 123	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
 124	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
 125	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
 126};
 127
 128DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
 129		 drm_tv_subconnector_enum_list)
 130
 131static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
 132	{ DRM_MODE_DIRTY_OFF,      "Off"      },
 133	{ DRM_MODE_DIRTY_ON,       "On"       },
 134	{ DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
 135};
 136
 137DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
 138		 drm_dirty_info_enum_list)
 139
 140struct drm_conn_prop_enum_list {
 141	int type;
 142	char *name;
 143	int count;
 144};
 145
 146/*
 147 * Connector and encoder types.
 148 */
 149static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
 150{	{ DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
 151	{ DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
 152	{ DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
 153	{ DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
 154	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
 155	{ DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
 156	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
 157	{ DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
 158	{ DRM_MODE_CONNECTOR_Component, "Component", 0 },
 159	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
 160	{ DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
 161	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
 162	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
 163	{ DRM_MODE_CONNECTOR_TV, "TV", 0 },
 164	{ DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
 165};
 166
 167static struct drm_prop_enum_list drm_encoder_enum_list[] =
 168{	{ DRM_MODE_ENCODER_NONE, "None" },
 169	{ DRM_MODE_ENCODER_DAC, "DAC" },
 170	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
 171	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
 172	{ DRM_MODE_ENCODER_TVDAC, "TV" },
 173};
 174
 175char *drm_get_encoder_name(struct drm_encoder *encoder)
 176{
 177	static char buf[32];
 178
 179	snprintf(buf, 32, "%s-%d",
 180		 drm_encoder_enum_list[encoder->encoder_type].name,
 181		 encoder->base.id);
 182	return buf;
 183}
 184EXPORT_SYMBOL(drm_get_encoder_name);
 185
 186char *drm_get_connector_name(struct drm_connector *connector)
 187{
 188	static char buf[32];
 
 
 189
 190	snprintf(buf, 32, "%s-%d",
 191		 drm_connector_enum_list[connector->connector_type].name,
 192		 connector->connector_type_id);
 193	return buf;
 194}
 195EXPORT_SYMBOL(drm_get_connector_name);
 196
 197char *drm_get_connector_status_name(enum drm_connector_status status)
 198{
 199	if (status == connector_status_connected)
 200		return "connected";
 201	else if (status == connector_status_disconnected)
 202		return "disconnected";
 203	else
 204		return "unknown";
 205}
 206
 207/**
 208 * drm_mode_object_get - allocate a new identifier
 209 * @dev: DRM device
 210 * @ptr: object pointer, used to generate unique ID
 211 * @type: object type
 212 *
 213 * LOCKING:
 214 *
 215 * Create a unique identifier based on @ptr in @dev's identifier space.  Used
 216 * for tracking modes, CRTCs and connectors.
 217 *
 218 * RETURNS:
 219 * New unique (relative to other objects in @dev) integer identifier for the
 220 * object.
 221 */
 222static int drm_mode_object_get(struct drm_device *dev,
 223			       struct drm_mode_object *obj, uint32_t obj_type)
 224{
 225	int new_id = 0;
 226	int ret;
 227
 228again:
 229	if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
 230		DRM_ERROR("Ran out memory getting a mode number\n");
 231		return -EINVAL;
 232	}
 233
 234	mutex_lock(&dev->mode_config.idr_mutex);
 235	ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
 236	mutex_unlock(&dev->mode_config.idr_mutex);
 237	if (ret == -EAGAIN)
 238		goto again;
 239
 240	obj->id = new_id;
 241	obj->type = obj_type;
 242	return 0;
 243}
 244
 245/**
 246 * drm_mode_object_put - free an identifer
 247 * @dev: DRM device
 248 * @id: ID to free
 249 *
 250 * LOCKING:
 251 * Caller must hold DRM mode_config lock.
 252 *
 253 * Free @id from @dev's unique identifier pool.
 254 */
 255static void drm_mode_object_put(struct drm_device *dev,
 256				struct drm_mode_object *object)
 257{
 258	mutex_lock(&dev->mode_config.idr_mutex);
 259	idr_remove(&dev->mode_config.crtc_idr, object->id);
 260	mutex_unlock(&dev->mode_config.idr_mutex);
 261}
 262
 263struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
 264		uint32_t id, uint32_t type)
 265{
 266	struct drm_mode_object *obj = NULL;
 267
 268	mutex_lock(&dev->mode_config.idr_mutex);
 269	obj = idr_find(&dev->mode_config.crtc_idr, id);
 270	if (!obj || (obj->type != type) || (obj->id != id))
 271		obj = NULL;
 272	mutex_unlock(&dev->mode_config.idr_mutex);
 273
 274	return obj;
 275}
 276EXPORT_SYMBOL(drm_mode_object_find);
 277
 278/**
 279 * drm_framebuffer_init - initialize a framebuffer
 280 * @dev: DRM device
 281 *
 282 * LOCKING:
 283 * Caller must hold mode config lock.
 284 *
 285 * Allocates an ID for the framebuffer's parent mode object, sets its mode
 286 * functions & device file and adds it to the master fd list.
 287 *
 288 * RETURNS:
 289 * Zero on success, error code on failure.
 290 */
 291int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
 292			 const struct drm_framebuffer_funcs *funcs)
 293{
 294	int ret;
 295
 296	ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
 297	if (ret) {
 298		return ret;
 
 299	}
 300
 301	fb->dev = dev;
 302	fb->funcs = funcs;
 303	dev->mode_config.num_fb++;
 304	list_add(&fb->head, &dev->mode_config.fb_list);
 305
 306	return 0;
 307}
 308EXPORT_SYMBOL(drm_framebuffer_init);
 309
 310/**
 311 * drm_framebuffer_cleanup - remove a framebuffer object
 312 * @fb: framebuffer to remove
 313 *
 314 * LOCKING:
 315 * Caller must hold mode config lock.
 316 *
 317 * Scans all the CRTCs in @dev's mode_config.  If they're using @fb, removes
 318 * it, setting it to NULL.
 319 */
 320void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
 321{
 322	struct drm_device *dev = fb->dev;
 323	struct drm_crtc *crtc;
 324	struct drm_mode_set set;
 325	int ret;
 326
 327	/* remove from any CRTC */
 328	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
 329		if (crtc->fb == fb) {
 330			/* should turn off the crtc */
 331			memset(&set, 0, sizeof(struct drm_mode_set));
 332			set.crtc = crtc;
 333			set.fb = NULL;
 334			ret = crtc->funcs->set_config(&set);
 335			if (ret)
 336				DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
 337		}
 338	}
 339
 340	drm_mode_object_put(dev, &fb->base);
 341	list_del(&fb->head);
 342	dev->mode_config.num_fb--;
 343}
 344EXPORT_SYMBOL(drm_framebuffer_cleanup);
 345
 346/**
 347 * drm_crtc_init - Initialise a new CRTC object
 348 * @dev: DRM device
 349 * @crtc: CRTC object to init
 350 * @funcs: callbacks for the new CRTC
 351 *
 352 * LOCKING:
 353 * Caller must hold mode config lock.
 354 *
 355 * Inits a new object created as base part of an driver crtc object.
 356 */
 357void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
 358		   const struct drm_crtc_funcs *funcs)
 359{
 360	crtc->dev = dev;
 361	crtc->funcs = funcs;
 362
 363	mutex_lock(&dev->mode_config.mutex);
 364	drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
 365
 366	list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
 367	dev->mode_config.num_crtc++;
 368	mutex_unlock(&dev->mode_config.mutex);
 369}
 370EXPORT_SYMBOL(drm_crtc_init);
 371
 372/**
 373 * drm_crtc_cleanup - Cleans up the core crtc usage.
 374 * @crtc: CRTC to cleanup
 375 *
 376 * LOCKING:
 377 * Caller must hold mode config lock.
 378 *
 379 * Cleanup @crtc. Removes from drm modesetting space
 380 * does NOT free object, caller does that.
 381 */
 382void drm_crtc_cleanup(struct drm_crtc *crtc)
 383{
 384	struct drm_device *dev = crtc->dev;
 385
 386	if (crtc->gamma_store) {
 387		kfree(crtc->gamma_store);
 388		crtc->gamma_store = NULL;
 389	}
 390
 391	drm_mode_object_put(dev, &crtc->base);
 392	list_del(&crtc->head);
 393	dev->mode_config.num_crtc--;
 394}
 395EXPORT_SYMBOL(drm_crtc_cleanup);
 396
 397/**
 398 * drm_mode_probed_add - add a mode to a connector's probed mode list
 399 * @connector: connector the new mode
 400 * @mode: mode data
 401 *
 402 * LOCKING:
 403 * Caller must hold mode config lock.
 404 *
 405 * Add @mode to @connector's mode list for later use.
 406 */
 407void drm_mode_probed_add(struct drm_connector *connector,
 408			 struct drm_display_mode *mode)
 409{
 410	list_add(&mode->head, &connector->probed_modes);
 
 
 411}
 412EXPORT_SYMBOL(drm_mode_probed_add);
 413
 414/**
 415 * drm_mode_remove - remove and free a mode
 416 * @connector: connector list to modify
 417 * @mode: mode to remove
 418 *
 419 * LOCKING:
 420 * Caller must hold mode config lock.
 421 *
 422 * Remove @mode from @connector's mode list, then free it.
 423 */
 424void drm_mode_remove(struct drm_connector *connector,
 425		     struct drm_display_mode *mode)
 426{
 427	list_del(&mode->head);
 428	kfree(mode);
 429}
 430EXPORT_SYMBOL(drm_mode_remove);
 431
 432/**
 433 * drm_connector_init - Init a preallocated connector
 434 * @dev: DRM device
 435 * @connector: the connector to init
 436 * @funcs: callbacks for this connector
 437 * @name: user visible name of the connector
 438 *
 439 * LOCKING:
 440 * Caller must hold @dev's mode_config lock.
 441 *
 442 * Initialises a preallocated connector. Connectors should be
 443 * subclassed as part of driver connector objects.
 444 */
 445void drm_connector_init(struct drm_device *dev,
 446		     struct drm_connector *connector,
 447		     const struct drm_connector_funcs *funcs,
 448		     int connector_type)
 449{
 450	mutex_lock(&dev->mode_config.mutex);
 451
 452	connector->dev = dev;
 453	connector->funcs = funcs;
 454	drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
 455	connector->connector_type = connector_type;
 456	connector->connector_type_id =
 457		++drm_connector_enum_list[connector_type].count; /* TODO */
 458	INIT_LIST_HEAD(&connector->user_modes);
 459	INIT_LIST_HEAD(&connector->probed_modes);
 460	INIT_LIST_HEAD(&connector->modes);
 461	connector->edid_blob_ptr = NULL;
 462
 463	list_add_tail(&connector->head, &dev->mode_config.connector_list);
 464	dev->mode_config.num_connector++;
 465
 466	drm_connector_attach_property(connector,
 467				      dev->mode_config.edid_property, 0);
 468
 469	drm_connector_attach_property(connector,
 470				      dev->mode_config.dpms_property, 0);
 471
 472	mutex_unlock(&dev->mode_config.mutex);
 473}
 474EXPORT_SYMBOL(drm_connector_init);
 475
 476/**
 477 * drm_connector_cleanup - cleans up an initialised connector
 478 * @connector: connector to cleanup
 479 *
 480 * LOCKING:
 481 * Caller must hold @dev's mode_config lock.
 482 *
 483 * Cleans up the connector but doesn't free the object.
 484 */
 485void drm_connector_cleanup(struct drm_connector *connector)
 486{
 487	struct drm_device *dev = connector->dev;
 488	struct drm_display_mode *mode, *t;
 489
 490	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
 491		drm_mode_remove(connector, mode);
 492
 493	list_for_each_entry_safe(mode, t, &connector->modes, head)
 494		drm_mode_remove(connector, mode);
 495
 496	list_for_each_entry_safe(mode, t, &connector->user_modes, head)
 497		drm_mode_remove(connector, mode);
 498
 499	mutex_lock(&dev->mode_config.mutex);
 500	drm_mode_object_put(dev, &connector->base);
 501	list_del(&connector->head);
 502	dev->mode_config.num_connector--;
 503	mutex_unlock(&dev->mode_config.mutex);
 504}
 505EXPORT_SYMBOL(drm_connector_cleanup);
 506
 507void drm_encoder_init(struct drm_device *dev,
 508		      struct drm_encoder *encoder,
 509		      const struct drm_encoder_funcs *funcs,
 510		      int encoder_type)
 511{
 512	mutex_lock(&dev->mode_config.mutex);
 513
 514	encoder->dev = dev;
 515
 516	drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
 517	encoder->encoder_type = encoder_type;
 518	encoder->funcs = funcs;
 519
 520	list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
 521	dev->mode_config.num_encoder++;
 522
 523	mutex_unlock(&dev->mode_config.mutex);
 524}
 525EXPORT_SYMBOL(drm_encoder_init);
 526
 527void drm_encoder_cleanup(struct drm_encoder *encoder)
 528{
 529	struct drm_device *dev = encoder->dev;
 530	mutex_lock(&dev->mode_config.mutex);
 531	drm_mode_object_put(dev, &encoder->base);
 532	list_del(&encoder->head);
 533	dev->mode_config.num_encoder--;
 534	mutex_unlock(&dev->mode_config.mutex);
 535}
 536EXPORT_SYMBOL(drm_encoder_cleanup);
 537
 538/**
 539 * drm_mode_create - create a new display mode
 540 * @dev: DRM device
 541 *
 542 * LOCKING:
 543 * Caller must hold DRM mode_config lock.
 544 *
 545 * Create a new drm_display_mode, give it an ID, and return it.
 546 *
 547 * RETURNS:
 548 * Pointer to new mode on success, NULL on error.
 549 */
 550struct drm_display_mode *drm_mode_create(struct drm_device *dev)
 551{
 552	struct drm_display_mode *nmode;
 553
 554	nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
 555	if (!nmode)
 556		return NULL;
 557
 558	drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
 559	return nmode;
 560}
 561EXPORT_SYMBOL(drm_mode_create);
 562
 563/**
 564 * drm_mode_destroy - remove a mode
 565 * @dev: DRM device
 566 * @mode: mode to remove
 567 *
 568 * LOCKING:
 569 * Caller must hold mode config lock.
 570 *
 571 * Free @mode's unique identifier, then free it.
 572 */
 573void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
 574{
 575	drm_mode_object_put(dev, &mode->base);
 576
 577	kfree(mode);
 578}
 579EXPORT_SYMBOL(drm_mode_destroy);
 580
 581static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
 582{
 583	struct drm_property *edid;
 584	struct drm_property *dpms;
 585	int i;
 586
 587	/*
 588	 * Standard properties (apply to all connectors)
 589	 */
 590	edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
 591				   DRM_MODE_PROP_IMMUTABLE,
 592				   "EDID", 0);
 593	dev->mode_config.edid_property = edid;
 594
 595	dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
 596				   "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
 597	for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
 598		drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
 599				      drm_dpms_enum_list[i].name);
 600	dev->mode_config.dpms_property = dpms;
 601
 602	return 0;
 603}
 604
 605/**
 606 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
 607 * @dev: DRM device
 608 *
 609 * Called by a driver the first time a DVI-I connector is made.
 610 */
 611int drm_mode_create_dvi_i_properties(struct drm_device *dev)
 612{
 613	struct drm_property *dvi_i_selector;
 614	struct drm_property *dvi_i_subconnector;
 615	int i;
 616
 617	if (dev->mode_config.dvi_i_select_subconnector_property)
 618		return 0;
 619
 620	dvi_i_selector =
 621		drm_property_create(dev, DRM_MODE_PROP_ENUM,
 622				    "select subconnector",
 623				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
 624	for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
 625		drm_property_add_enum(dvi_i_selector, i,
 626				      drm_dvi_i_select_enum_list[i].type,
 627				      drm_dvi_i_select_enum_list[i].name);
 628	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
 629
 630	dvi_i_subconnector =
 631		drm_property_create(dev, DRM_MODE_PROP_ENUM |
 632				    DRM_MODE_PROP_IMMUTABLE,
 633				    "subconnector",
 634				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
 635	for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
 636		drm_property_add_enum(dvi_i_subconnector, i,
 637				      drm_dvi_i_subconnector_enum_list[i].type,
 638				      drm_dvi_i_subconnector_enum_list[i].name);
 639	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
 640
 641	return 0;
 642}
 643EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
 644
 645/**
 646 * drm_create_tv_properties - create TV specific connector properties
 647 * @dev: DRM device
 648 * @num_modes: number of different TV formats (modes) supported
 649 * @modes: array of pointers to strings containing name of each format
 650 *
 651 * Called by a driver's TV initialization routine, this function creates
 652 * the TV specific connector properties for a given device.  Caller is
 653 * responsible for allocating a list of format names and passing them to
 654 * this routine.
 655 */
 656int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
 657				  char *modes[])
 658{
 659	struct drm_property *tv_selector;
 660	struct drm_property *tv_subconnector;
 661	int i;
 662
 663	if (dev->mode_config.tv_select_subconnector_property)
 664		return 0;
 665
 666	/*
 667	 * Basic connector properties
 668	 */
 669	tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
 670					  "select subconnector",
 671					  ARRAY_SIZE(drm_tv_select_enum_list));
 672	for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
 673		drm_property_add_enum(tv_selector, i,
 674				      drm_tv_select_enum_list[i].type,
 675				      drm_tv_select_enum_list[i].name);
 676	dev->mode_config.tv_select_subconnector_property = tv_selector;
 677
 678	tv_subconnector =
 679		drm_property_create(dev, DRM_MODE_PROP_ENUM |
 680				    DRM_MODE_PROP_IMMUTABLE, "subconnector",
 681				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
 682	for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
 683		drm_property_add_enum(tv_subconnector, i,
 684				      drm_tv_subconnector_enum_list[i].type,
 685				      drm_tv_subconnector_enum_list[i].name);
 686	dev->mode_config.tv_subconnector_property = tv_subconnector;
 687
 688	/*
 689	 * Other, TV specific properties: margins & TV modes.
 690	 */
 691	dev->mode_config.tv_left_margin_property =
 692		drm_property_create(dev, DRM_MODE_PROP_RANGE,
 693				    "left margin", 2);
 694	dev->mode_config.tv_left_margin_property->values[0] = 0;
 695	dev->mode_config.tv_left_margin_property->values[1] = 100;
 696
 697	dev->mode_config.tv_right_margin_property =
 698		drm_property_create(dev, DRM_MODE_PROP_RANGE,
 699				    "right margin", 2);
 700	dev->mode_config.tv_right_margin_property->values[0] = 0;
 701	dev->mode_config.tv_right_margin_property->values[1] = 100;
 702
 703	dev->mode_config.tv_top_margin_property =
 704		drm_property_create(dev, DRM_MODE_PROP_RANGE,
 705				    "top margin", 2);
 706	dev->mode_config.tv_top_margin_property->values[0] = 0;
 707	dev->mode_config.tv_top_margin_property->values[1] = 100;
 708
 709	dev->mode_config.tv_bottom_margin_property =
 710		drm_property_create(dev, DRM_MODE_PROP_RANGE,
 711				    "bottom margin", 2);
 712	dev->mode_config.tv_bottom_margin_property->values[0] = 0;
 713	dev->mode_config.tv_bottom_margin_property->values[1] = 100;
 714
 715	dev->mode_config.tv_mode_property =
 716		drm_property_create(dev, DRM_MODE_PROP_ENUM,
 717				    "mode", num_modes);
 718	for (i = 0; i < num_modes; i++)
 719		drm_property_add_enum(dev->mode_config.tv_mode_property, i,
 720				      i, modes[i]);
 721
 722	dev->mode_config.tv_brightness_property =
 723		drm_property_create(dev, DRM_MODE_PROP_RANGE,
 724				    "brightness", 2);
 725	dev->mode_config.tv_brightness_property->values[0] = 0;
 726	dev->mode_config.tv_brightness_property->values[1] = 100;
 727
 728	dev->mode_config.tv_contrast_property =
 729		drm_property_create(dev, DRM_MODE_PROP_RANGE,
 730				    "contrast", 2);
 731	dev->mode_config.tv_contrast_property->values[0] = 0;
 732	dev->mode_config.tv_contrast_property->values[1] = 100;
 733
 734	dev->mode_config.tv_flicker_reduction_property =
 735		drm_property_create(dev, DRM_MODE_PROP_RANGE,
 736				    "flicker reduction", 2);
 737	dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
 738	dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
 739
 740	dev->mode_config.tv_overscan_property =
 741		drm_property_create(dev, DRM_MODE_PROP_RANGE,
 742				    "overscan", 2);
 743	dev->mode_config.tv_overscan_property->values[0] = 0;
 744	dev->mode_config.tv_overscan_property->values[1] = 100;
 745
 746	dev->mode_config.tv_saturation_property =
 747		drm_property_create(dev, DRM_MODE_PROP_RANGE,
 748				    "saturation", 2);
 749	dev->mode_config.tv_saturation_property->values[0] = 0;
 750	dev->mode_config.tv_saturation_property->values[1] = 100;
 751
 752	dev->mode_config.tv_hue_property =
 753		drm_property_create(dev, DRM_MODE_PROP_RANGE,
 754				    "hue", 2);
 755	dev->mode_config.tv_hue_property->values[0] = 0;
 756	dev->mode_config.tv_hue_property->values[1] = 100;
 757
 758	return 0;
 759}
 760EXPORT_SYMBOL(drm_mode_create_tv_properties);
 761
 762/**
 763 * drm_mode_create_scaling_mode_property - create scaling mode property
 764 * @dev: DRM device
 765 *
 766 * Called by a driver the first time it's needed, must be attached to desired
 767 * connectors.
 768 */
 769int drm_mode_create_scaling_mode_property(struct drm_device *dev)
 770{
 771	struct drm_property *scaling_mode;
 772	int i;
 773
 774	if (dev->mode_config.scaling_mode_property)
 775		return 0;
 776
 777	scaling_mode =
 778		drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
 779				    ARRAY_SIZE(drm_scaling_mode_enum_list));
 780	for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
 781		drm_property_add_enum(scaling_mode, i,
 782				      drm_scaling_mode_enum_list[i].type,
 783				      drm_scaling_mode_enum_list[i].name);
 784
 785	dev->mode_config.scaling_mode_property = scaling_mode;
 786
 787	return 0;
 788}
 789EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
 790
 791/**
 792 * drm_mode_create_dithering_property - create dithering property
 793 * @dev: DRM device
 794 *
 795 * Called by a driver the first time it's needed, must be attached to desired
 796 * connectors.
 797 */
 798int drm_mode_create_dithering_property(struct drm_device *dev)
 799{
 800	struct drm_property *dithering_mode;
 801	int i;
 802
 803	if (dev->mode_config.dithering_mode_property)
 804		return 0;
 805
 806	dithering_mode =
 807		drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
 808				    ARRAY_SIZE(drm_dithering_mode_enum_list));
 809	for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
 810		drm_property_add_enum(dithering_mode, i,
 811				      drm_dithering_mode_enum_list[i].type,
 812				      drm_dithering_mode_enum_list[i].name);
 813	dev->mode_config.dithering_mode_property = dithering_mode;
 814
 815	return 0;
 816}
 817EXPORT_SYMBOL(drm_mode_create_dithering_property);
 818
 819/**
 820 * drm_mode_create_dirty_property - create dirty property
 821 * @dev: DRM device
 822 *
 823 * Called by a driver the first time it's needed, must be attached to desired
 824 * connectors.
 825 */
 826int drm_mode_create_dirty_info_property(struct drm_device *dev)
 827{
 828	struct drm_property *dirty_info;
 829	int i;
 830
 831	if (dev->mode_config.dirty_info_property)
 832		return 0;
 833
 834	dirty_info =
 835		drm_property_create(dev, DRM_MODE_PROP_ENUM |
 836				    DRM_MODE_PROP_IMMUTABLE,
 837				    "dirty",
 838				    ARRAY_SIZE(drm_dirty_info_enum_list));
 839	for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
 840		drm_property_add_enum(dirty_info, i,
 841				      drm_dirty_info_enum_list[i].type,
 842				      drm_dirty_info_enum_list[i].name);
 843	dev->mode_config.dirty_info_property = dirty_info;
 844
 845	return 0;
 846}
 847EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
 848
 849/**
 850 * drm_mode_config_init - initialize DRM mode_configuration structure
 
 851 * @dev: DRM device
 
 
 
 
 
 852 *
 853 * LOCKING:
 854 * None, should happen single threaded at init time.
 
 
 
 855 *
 856 * Initialize @dev's mode_config structure, used for tracking the graphics
 857 * configuration of @dev.
 858 */
 859void drm_mode_config_init(struct drm_device *dev)
 
 
 
 
 860{
 861	mutex_init(&dev->mode_config.mutex);
 862	mutex_init(&dev->mode_config.idr_mutex);
 863	INIT_LIST_HEAD(&dev->mode_config.fb_list);
 864	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
 865	INIT_LIST_HEAD(&dev->mode_config.connector_list);
 866	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
 867	INIT_LIST_HEAD(&dev->mode_config.property_list);
 868	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
 869	idr_init(&dev->mode_config.crtc_idr);
 870
 871	mutex_lock(&dev->mode_config.mutex);
 872	drm_mode_create_standard_connector_properties(dev);
 873	mutex_unlock(&dev->mode_config.mutex);
 874
 875	/* Just to be sure */
 876	dev->mode_config.num_fb = 0;
 877	dev->mode_config.num_connector = 0;
 878	dev->mode_config.num_crtc = 0;
 879	dev->mode_config.num_encoder = 0;
 880}
 881EXPORT_SYMBOL(drm_mode_config_init);
 882
 883int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
 884{
 885	uint32_t total_objects = 0;
 886
 887	total_objects += dev->mode_config.num_crtc;
 888	total_objects += dev->mode_config.num_connector;
 889	total_objects += dev->mode_config.num_encoder;
 890
 891	group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
 892	if (!group->id_list)
 893		return -ENOMEM;
 894
 895	group->num_crtcs = 0;
 896	group->num_connectors = 0;
 897	group->num_encoders = 0;
 898	return 0;
 899}
 900
 901int drm_mode_group_init_legacy_group(struct drm_device *dev,
 902				     struct drm_mode_group *group)
 903{
 904	struct drm_crtc *crtc;
 905	struct drm_encoder *encoder;
 906	struct drm_connector *connector;
 907	int ret;
 908
 909	if ((ret = drm_mode_group_init(dev, group)))
 
 
 910		return ret;
 911
 912	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
 913		group->id_list[group->num_crtcs++] = crtc->base.id;
 914
 915	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
 916		group->id_list[group->num_crtcs + group->num_encoders++] =
 917		encoder->base.id;
 918
 919	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
 920		group->id_list[group->num_crtcs + group->num_encoders +
 921			       group->num_connectors++] = connector->base.id;
 922
 923	return 0;
 924}
 925
 926/**
 927 * drm_mode_config_cleanup - free up DRM mode_config info
 928 * @dev: DRM device
 929 *
 930 * LOCKING:
 931 * Caller must hold mode config lock.
 932 *
 933 * Free up all the connectors and CRTCs associated with this DRM device, then
 934 * free up the framebuffers and associated buffer objects.
 935 *
 936 * FIXME: cleanup any dangling user buffer objects too
 937 */
 938void drm_mode_config_cleanup(struct drm_device *dev)
 939{
 940	struct drm_connector *connector, *ot;
 941	struct drm_crtc *crtc, *ct;
 942	struct drm_encoder *encoder, *enct;
 943	struct drm_framebuffer *fb, *fbt;
 944	struct drm_property *property, *pt;
 945
 946	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
 947				 head) {
 948		encoder->funcs->destroy(encoder);
 949	}
 950
 951	list_for_each_entry_safe(connector, ot,
 952				 &dev->mode_config.connector_list, head) {
 953		connector->funcs->destroy(connector);
 954	}
 955
 956	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
 957				 head) {
 958		drm_property_destroy(dev, property);
 959	}
 
 
 
 
 
 
 
 
 
 
 
 
 960
 961	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
 962		fb->funcs->destroy(fb);
 
 
 963	}
 964
 965	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
 966		crtc->funcs->destroy(crtc);
 
 
 
 
 
 967	}
 968
 
 969}
 970EXPORT_SYMBOL(drm_mode_config_cleanup);
 971
 972/**
 973 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
 974 * @out: drm_mode_modeinfo struct to return to the user
 975 * @in: drm_display_mode to use
 976 *
 977 * LOCKING:
 978 * None.
 979 *
 980 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
 981 * the user.
 982 */
 983void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
 984			       struct drm_display_mode *in)
 985{
 986	out->clock = in->clock;
 987	out->hdisplay = in->hdisplay;
 988	out->hsync_start = in->hsync_start;
 989	out->hsync_end = in->hsync_end;
 990	out->htotal = in->htotal;
 991	out->hskew = in->hskew;
 992	out->vdisplay = in->vdisplay;
 993	out->vsync_start = in->vsync_start;
 994	out->vsync_end = in->vsync_end;
 995	out->vtotal = in->vtotal;
 996	out->vscan = in->vscan;
 997	out->vrefresh = in->vrefresh;
 998	out->flags = in->flags;
 999	out->type = in->type;
1000	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1001	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1002}
1003
1004/**
1005 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1006 * @out: drm_display_mode to return to the user
1007 * @in: drm_mode_modeinfo to use
1008 *
1009 * LOCKING:
1010 * None.
1011 *
1012 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1013 * the caller.
1014 */
1015void drm_crtc_convert_umode(struct drm_display_mode *out,
1016			    struct drm_mode_modeinfo *in)
1017{
1018	out->clock = in->clock;
1019	out->hdisplay = in->hdisplay;
1020	out->hsync_start = in->hsync_start;
1021	out->hsync_end = in->hsync_end;
1022	out->htotal = in->htotal;
1023	out->hskew = in->hskew;
1024	out->vdisplay = in->vdisplay;
1025	out->vsync_start = in->vsync_start;
1026	out->vsync_end = in->vsync_end;
1027	out->vtotal = in->vtotal;
1028	out->vscan = in->vscan;
1029	out->vrefresh = in->vrefresh;
1030	out->flags = in->flags;
1031	out->type = in->type;
1032	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1033	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1034}
1035
1036/**
1037 * drm_mode_getresources - get graphics configuration
1038 * @inode: inode from the ioctl
1039 * @filp: file * from the ioctl
1040 * @cmd: cmd from ioctl
1041 * @arg: arg from ioctl
1042 *
1043 * LOCKING:
1044 * Takes mode config lock.
1045 *
1046 * Construct a set of configuration description structures and return
1047 * them to the user, including CRTC, connector and framebuffer configuration.
1048 *
1049 * Called by the user via ioctl.
1050 *
1051 * RETURNS:
1052 * Zero on success, errno on failure.
1053 */
1054int drm_mode_getresources(struct drm_device *dev, void *data,
1055			  struct drm_file *file_priv)
1056{
1057	struct drm_mode_card_res *card_res = data;
1058	struct list_head *lh;
1059	struct drm_framebuffer *fb;
1060	struct drm_connector *connector;
1061	struct drm_crtc *crtc;
1062	struct drm_encoder *encoder;
1063	int ret = 0;
1064	int connector_count = 0;
1065	int crtc_count = 0;
1066	int fb_count = 0;
1067	int encoder_count = 0;
1068	int copied = 0, i;
1069	uint32_t __user *fb_id;
1070	uint32_t __user *crtc_id;
1071	uint32_t __user *connector_id;
1072	uint32_t __user *encoder_id;
1073	struct drm_mode_group *mode_group;
1074
1075	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1076		return -EINVAL;
1077
1078	mutex_lock(&dev->mode_config.mutex);
1079
1080	/*
1081	 * For the non-control nodes we need to limit the list of resources
1082	 * by IDs in the group list for this node
1083	 */
1084	list_for_each(lh, &file_priv->fbs)
1085		fb_count++;
1086
1087	mode_group = &file_priv->master->minor->mode_group;
1088	if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1089
1090		list_for_each(lh, &dev->mode_config.crtc_list)
1091			crtc_count++;
1092
1093		list_for_each(lh, &dev->mode_config.connector_list)
1094			connector_count++;
1095
1096		list_for_each(lh, &dev->mode_config.encoder_list)
1097			encoder_count++;
1098	} else {
1099
1100		crtc_count = mode_group->num_crtcs;
1101		connector_count = mode_group->num_connectors;
1102		encoder_count = mode_group->num_encoders;
1103	}
1104
1105	card_res->max_height = dev->mode_config.max_height;
1106	card_res->min_height = dev->mode_config.min_height;
1107	card_res->max_width = dev->mode_config.max_width;
1108	card_res->min_width = dev->mode_config.min_width;
1109
1110	/* handle this in 4 parts */
1111	/* FBs */
1112	if (card_res->count_fbs >= fb_count) {
1113		copied = 0;
1114		fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1115		list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1116			if (put_user(fb->base.id, fb_id + copied)) {
1117				ret = -EFAULT;
1118				goto out;
1119			}
1120			copied++;
1121		}
1122	}
1123	card_res->count_fbs = fb_count;
1124
1125	/* CRTCs */
1126	if (card_res->count_crtcs >= crtc_count) {
1127		copied = 0;
1128		crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1129		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1130			list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1131					    head) {
1132				DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1133				if (put_user(crtc->base.id, crtc_id + copied)) {
1134					ret = -EFAULT;
1135					goto out;
1136				}
1137				copied++;
1138			}
1139		} else {
1140			for (i = 0; i < mode_group->num_crtcs; i++) {
1141				if (put_user(mode_group->id_list[i],
1142					     crtc_id + copied)) {
1143					ret = -EFAULT;
1144					goto out;
1145				}
1146				copied++;
1147			}
1148		}
1149	}
1150	card_res->count_crtcs = crtc_count;
1151
1152	/* Encoders */
1153	if (card_res->count_encoders >= encoder_count) {
1154		copied = 0;
1155		encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1156		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1157			list_for_each_entry(encoder,
1158					    &dev->mode_config.encoder_list,
1159					    head) {
1160				DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1161						drm_get_encoder_name(encoder));
1162				if (put_user(encoder->base.id, encoder_id +
1163					     copied)) {
1164					ret = -EFAULT;
1165					goto out;
1166				}
1167				copied++;
1168			}
1169		} else {
1170			for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1171				if (put_user(mode_group->id_list[i],
1172					     encoder_id + copied)) {
1173					ret = -EFAULT;
1174					goto out;
1175				}
1176				copied++;
1177			}
1178
1179		}
1180	}
1181	card_res->count_encoders = encoder_count;
1182
1183	/* Connectors */
1184	if (card_res->count_connectors >= connector_count) {
1185		copied = 0;
1186		connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1187		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1188			list_for_each_entry(connector,
1189					    &dev->mode_config.connector_list,
1190					    head) {
1191				DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1192					connector->base.id,
1193					drm_get_connector_name(connector));
1194				if (put_user(connector->base.id,
1195					     connector_id + copied)) {
1196					ret = -EFAULT;
1197					goto out;
1198				}
1199				copied++;
1200			}
1201		} else {
1202			int start = mode_group->num_crtcs +
1203				mode_group->num_encoders;
1204			for (i = start; i < start + mode_group->num_connectors; i++) {
1205				if (put_user(mode_group->id_list[i],
1206					     connector_id + copied)) {
1207					ret = -EFAULT;
1208					goto out;
1209				}
1210				copied++;
1211			}
1212		}
1213	}
1214	card_res->count_connectors = connector_count;
1215
1216	DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1217		  card_res->count_connectors, card_res->count_encoders);
1218
1219out:
1220	mutex_unlock(&dev->mode_config.mutex);
1221	return ret;
1222}
 
1223
1224/**
1225 * drm_mode_getcrtc - get CRTC configuration
1226 * @inode: inode from the ioctl
1227 * @filp: file * from the ioctl
1228 * @cmd: cmd from ioctl
1229 * @arg: arg from ioctl
1230 *
1231 * LOCKING:
1232 * Caller? (FIXME)
1233 *
1234 * Construct a CRTC configuration structure to return to the user.
1235 *
1236 * Called by the user via ioctl.
1237 *
1238 * RETURNS:
1239 * Zero on success, errno on failure.
1240 */
1241int drm_mode_getcrtc(struct drm_device *dev,
1242		     void *data, struct drm_file *file_priv)
1243{
1244	struct drm_mode_crtc *crtc_resp = data;
1245	struct drm_crtc *crtc;
1246	struct drm_mode_object *obj;
1247	int ret = 0;
1248
1249	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1250		return -EINVAL;
1251
1252	mutex_lock(&dev->mode_config.mutex);
 
 
1253
1254	obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1255				   DRM_MODE_OBJECT_CRTC);
1256	if (!obj) {
1257		ret = -EINVAL;
1258		goto out;
1259	}
1260	crtc = obj_to_crtc(obj);
1261
1262	crtc_resp->x = crtc->x;
1263	crtc_resp->y = crtc->y;
1264	crtc_resp->gamma_size = crtc->gamma_size;
1265	if (crtc->fb)
1266		crtc_resp->fb_id = crtc->fb->base.id;
 
 
 
 
1267	else
1268		crtc_resp->fb_id = 0;
1269
1270	if (crtc->enabled) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1271
1272		drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1273		crtc_resp->mode_valid = 1;
 
1274
1275	} else {
1276		crtc_resp->mode_valid = 0;
 
1277	}
 
 
 
1278
1279out:
1280	mutex_unlock(&dev->mode_config.mutex);
1281	return ret;
1282}
1283
1284/**
1285 * drm_mode_getconnector - get connector configuration
1286 * @inode: inode from the ioctl
1287 * @filp: file * from the ioctl
1288 * @cmd: cmd from ioctl
1289 * @arg: arg from ioctl
1290 *
1291 * LOCKING:
1292 * Caller? (FIXME)
1293 *
1294 * Construct a connector configuration structure to return to the user.
1295 *
1296 * Called by the user via ioctl.
1297 *
1298 * RETURNS:
1299 * Zero on success, errno on failure.
1300 */
1301int drm_mode_getconnector(struct drm_device *dev, void *data,
1302			  struct drm_file *file_priv)
1303{
1304	struct drm_mode_get_connector *out_resp = data;
1305	struct drm_mode_object *obj;
1306	struct drm_connector *connector;
1307	struct drm_display_mode *mode;
1308	int mode_count = 0;
1309	int props_count = 0;
1310	int encoders_count = 0;
1311	int ret = 0;
1312	int copied = 0;
1313	int i;
1314	struct drm_mode_modeinfo u_mode;
1315	struct drm_mode_modeinfo __user *mode_ptr;
1316	uint32_t __user *prop_ptr;
1317	uint64_t __user *prop_values;
1318	uint32_t __user *encoder_ptr;
1319
1320	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1321		return -EINVAL;
1322
1323	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1324
1325	DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1326
1327	mutex_lock(&dev->mode_config.mutex);
 
 
1328
1329	obj = drm_mode_object_find(dev, out_resp->connector_id,
1330				   DRM_MODE_OBJECT_CONNECTOR);
1331	if (!obj) {
1332		ret = -EINVAL;
1333		goto out;
1334	}
1335	connector = obj_to_connector(obj);
1336
1337	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1338		if (connector->property_ids[i] != 0) {
1339			props_count++;
1340		}
1341	}
1342
1343	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1344		if (connector->encoder_ids[i] != 0) {
1345			encoders_count++;
1346		}
1347	}
1348
1349	if (out_resp->count_modes == 0) {
1350		connector->funcs->fill_modes(connector,
1351					     dev->mode_config.max_width,
1352					     dev->mode_config.max_height);
1353	}
1354
1355	/* delayed so we get modes regardless of pre-fill_modes state */
1356	list_for_each_entry(mode, &connector->modes, head)
1357		mode_count++;
1358
1359	out_resp->connector_id = connector->base.id;
1360	out_resp->connector_type = connector->connector_type;
1361	out_resp->connector_type_id = connector->connector_type_id;
1362	out_resp->mm_width = connector->display_info.width_mm;
1363	out_resp->mm_height = connector->display_info.height_mm;
1364	out_resp->subpixel = connector->display_info.subpixel_order;
1365	out_resp->connection = connector->status;
1366	if (connector->encoder)
1367		out_resp->encoder_id = connector->encoder->base.id;
1368	else
1369		out_resp->encoder_id = 0;
1370
1371	/*
1372	 * This ioctl is called twice, once to determine how much space is
1373	 * needed, and the 2nd time to fill it.
1374	 */
1375	if ((out_resp->count_modes >= mode_count) && mode_count) {
1376		copied = 0;
1377		mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1378		list_for_each_entry(mode, &connector->modes, head) {
1379			drm_crtc_convert_to_umode(&u_mode, mode);
1380			if (copy_to_user(mode_ptr + copied,
1381					 &u_mode, sizeof(u_mode))) {
1382				ret = -EFAULT;
1383				goto out;
1384			}
1385			copied++;
1386		}
1387	}
1388	out_resp->count_modes = mode_count;
1389
1390	if ((out_resp->count_props >= props_count) && props_count) {
1391		copied = 0;
1392		prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1393		prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1394		for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1395			if (connector->property_ids[i] != 0) {
1396				if (put_user(connector->property_ids[i],
1397					     prop_ptr + copied)) {
1398					ret = -EFAULT;
1399					goto out;
1400				}
1401
1402				if (put_user(connector->property_values[i],
1403					     prop_values + copied)) {
1404					ret = -EFAULT;
1405					goto out;
1406				}
1407				copied++;
1408			}
1409		}
1410	}
1411	out_resp->count_props = props_count;
1412
1413	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1414		copied = 0;
1415		encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1416		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1417			if (connector->encoder_ids[i] != 0) {
1418				if (put_user(connector->encoder_ids[i],
1419					     encoder_ptr + copied)) {
1420					ret = -EFAULT;
1421					goto out;
1422				}
1423				copied++;
1424			}
1425		}
1426	}
1427	out_resp->count_encoders = encoders_count;
 
1428
1429out:
1430	mutex_unlock(&dev->mode_config.mutex);
1431	return ret;
1432}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1433
1434int drm_mode_getencoder(struct drm_device *dev, void *data,
1435			struct drm_file *file_priv)
1436{
1437	struct drm_mode_get_encoder *enc_resp = data;
1438	struct drm_mode_object *obj;
1439	struct drm_encoder *encoder;
1440	int ret = 0;
1441
1442	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1443		return -EINVAL;
1444
1445	mutex_lock(&dev->mode_config.mutex);
1446	obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1447				   DRM_MODE_OBJECT_ENCODER);
1448	if (!obj) {
1449		ret = -EINVAL;
1450		goto out;
1451	}
1452	encoder = obj_to_encoder(obj);
1453
1454	if (encoder->crtc)
1455		enc_resp->crtc_id = encoder->crtc->base.id;
1456	else
1457		enc_resp->crtc_id = 0;
1458	enc_resp->encoder_type = encoder->encoder_type;
1459	enc_resp->encoder_id = encoder->base.id;
1460	enc_resp->possible_crtcs = encoder->possible_crtcs;
1461	enc_resp->possible_clones = encoder->possible_clones;
1462
1463out:
1464	mutex_unlock(&dev->mode_config.mutex);
1465	return ret;
1466}
 
1467
1468/**
1469 * drm_mode_setcrtc - set CRTC configuration
1470 * @inode: inode from the ioctl
1471 * @filp: file * from the ioctl
1472 * @cmd: cmd from ioctl
1473 * @arg: arg from ioctl
1474 *
1475 * LOCKING:
1476 * Caller? (FIXME)
1477 *
1478 * Build a new CRTC configuration based on user request.
1479 *
1480 * Called by the user via ioctl.
1481 *
1482 * RETURNS:
1483 * Zero on success, errno on failure.
1484 */
1485int drm_mode_setcrtc(struct drm_device *dev, void *data,
1486		     struct drm_file *file_priv)
1487{
1488	struct drm_mode_config *config = &dev->mode_config;
1489	struct drm_mode_crtc *crtc_req = data;
1490	struct drm_mode_object *obj;
1491	struct drm_crtc *crtc, *crtcfb;
1492	struct drm_connector **connector_set = NULL, *connector;
1493	struct drm_framebuffer *fb = NULL;
1494	struct drm_display_mode *mode = NULL;
1495	struct drm_mode_set set;
1496	uint32_t __user *set_connectors_ptr;
1497	int ret = 0;
 
1498	int i;
1499
1500	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1501		return -EINVAL;
 
 
 
 
 
 
 
1502
1503	mutex_lock(&dev->mode_config.mutex);
1504	obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1505				   DRM_MODE_OBJECT_CRTC);
1506	if (!obj) {
1507		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1508		ret = -EINVAL;
1509		goto out;
1510	}
1511	crtc = obj_to_crtc(obj);
1512	DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
 
 
 
 
 
 
 
 
1513
1514	if (crtc_req->mode_valid) {
1515		/* If we have a mode we need a framebuffer. */
1516		/* If we pass -1, set the mode with the currently bound fb */
1517		if (crtc_req->fb_id == -1) {
1518			list_for_each_entry(crtcfb,
1519					    &dev->mode_config.crtc_list, head) {
1520				if (crtcfb == crtc) {
1521					DRM_DEBUG_KMS("Using current fb for "
1522							"setmode\n");
1523					fb = crtc->fb;
1524				}
 
 
 
 
1525			}
 
 
 
 
1526		} else {
1527			obj = drm_mode_object_find(dev, crtc_req->fb_id,
1528						   DRM_MODE_OBJECT_FB);
1529			if (!obj) {
1530				DRM_DEBUG_KMS("Unknown FB ID%d\n",
1531						crtc_req->fb_id);
1532				ret = -EINVAL;
1533				goto out;
1534			}
1535			fb = obj_to_fb(obj);
1536		}
1537
1538		mode = drm_mode_create(dev);
1539		drm_crtc_convert_umode(mode, &crtc_req->mode);
1540		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1541	}
1542
1543	if (crtc_req->count_connectors == 0 && mode) {
1544		DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1545		ret = -EINVAL;
1546		goto out;
1547	}
1548
1549	if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1550		DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1551			  crtc_req->count_connectors);
1552		ret = -EINVAL;
1553		goto out;
1554	}
1555
1556	if (crtc_req->count_connectors > 0) {
1557		u32 out_id;
1558
1559		/* Avoid unbounded kernel memory allocation */
1560		if (crtc_req->count_connectors > config->num_connector) {
1561			ret = -EINVAL;
1562			goto out;
1563		}
1564
1565		connector_set = kmalloc(crtc_req->count_connectors *
1566					sizeof(struct drm_connector *),
1567					GFP_KERNEL);
1568		if (!connector_set) {
1569			ret = -ENOMEM;
1570			goto out;
1571		}
1572
1573		for (i = 0; i < crtc_req->count_connectors; i++) {
1574			set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
 
1575			if (get_user(out_id, &set_connectors_ptr[i])) {
1576				ret = -EFAULT;
1577				goto out;
1578			}
1579
1580			obj = drm_mode_object_find(dev, out_id,
1581						   DRM_MODE_OBJECT_CONNECTOR);
1582			if (!obj) {
1583				DRM_DEBUG_KMS("Connector id %d unknown\n",
1584						out_id);
1585				ret = -EINVAL;
1586				goto out;
1587			}
1588			connector = obj_to_connector(obj);
1589			DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1590					connector->base.id,
1591					drm_get_connector_name(connector));
1592
1593			connector_set[i] = connector;
1594		}
1595	}
1596
1597	set.crtc = crtc;
1598	set.x = crtc_req->x;
1599	set.y = crtc_req->y;
1600	set.mode = mode;
1601	set.connectors = connector_set;
1602	set.num_connectors = crtc_req->count_connectors;
1603	set.fb = fb;
1604	ret = crtc->funcs->set_config(&set);
1605
1606out:
1607	kfree(connector_set);
1608	mutex_unlock(&dev->mode_config.mutex);
1609	return ret;
1610}
1611
1612int drm_mode_cursor_ioctl(struct drm_device *dev,
1613			void *data, struct drm_file *file_priv)
1614{
1615	struct drm_mode_cursor *req = data;
1616	struct drm_mode_object *obj;
1617	struct drm_crtc *crtc;
1618	int ret = 0;
1619
1620	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1621		return -EINVAL;
1622
1623	if (!req->flags) {
1624		DRM_ERROR("no operation set\n");
1625		return -EINVAL;
1626	}
1627
1628	mutex_lock(&dev->mode_config.mutex);
1629	obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1630	if (!obj) {
1631		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1632		ret = -EINVAL;
1633		goto out;
1634	}
1635	crtc = obj_to_crtc(obj);
1636
1637	if (req->flags & DRM_MODE_CURSOR_BO) {
1638		if (!crtc->funcs->cursor_set) {
1639			DRM_ERROR("crtc does not support cursor\n");
1640			ret = -ENXIO;
1641			goto out;
1642		}
1643		/* Turns off the cursor if handle is 0 */
1644		ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1645					      req->width, req->height);
1646	}
1647
1648	if (req->flags & DRM_MODE_CURSOR_MOVE) {
1649		if (crtc->funcs->cursor_move) {
1650			ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1651		} else {
1652			DRM_ERROR("crtc does not support cursor\n");
1653			ret = -EFAULT;
1654			goto out;
1655		}
1656	}
1657out:
1658	mutex_unlock(&dev->mode_config.mutex);
1659	return ret;
1660}
1661
1662/**
1663 * drm_mode_addfb - add an FB to the graphics configuration
1664 * @inode: inode from the ioctl
1665 * @filp: file * from the ioctl
1666 * @cmd: cmd from ioctl
1667 * @arg: arg from ioctl
1668 *
1669 * LOCKING:
1670 * Takes mode config lock.
1671 *
1672 * Add a new FB to the specified CRTC, given a user request.
1673 *
1674 * Called by the user via ioctl.
1675 *
1676 * RETURNS:
1677 * Zero on success, errno on failure.
1678 */
1679int drm_mode_addfb(struct drm_device *dev,
1680		   void *data, struct drm_file *file_priv)
1681{
1682	struct drm_mode_fb_cmd *r = data;
1683	struct drm_mode_config *config = &dev->mode_config;
1684	struct drm_framebuffer *fb;
1685	int ret = 0;
1686
1687	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1688		return -EINVAL;
1689
1690	if ((config->min_width > r->width) || (r->width > config->max_width)) {
1691		DRM_ERROR("mode new framebuffer width not within limits\n");
1692		return -EINVAL;
1693	}
1694	if ((config->min_height > r->height) || (r->height > config->max_height)) {
1695		DRM_ERROR("mode new framebuffer height not within limits\n");
1696		return -EINVAL;
1697	}
1698
1699	mutex_lock(&dev->mode_config.mutex);
1700
1701	/* TODO check buffer is sufficiently large */
1702	/* TODO setup destructor callback */
1703
1704	fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1705	if (IS_ERR(fb)) {
1706		DRM_ERROR("could not create framebuffer\n");
1707		ret = PTR_ERR(fb);
1708		goto out;
1709	}
1710
1711	r->fb_id = fb->base.id;
1712	list_add(&fb->filp_head, &file_priv->fbs);
1713	DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
1714
1715out:
1716	mutex_unlock(&dev->mode_config.mutex);
1717	return ret;
1718}
1719
1720/**
1721 * drm_mode_rmfb - remove an FB from the configuration
1722 * @inode: inode from the ioctl
1723 * @filp: file * from the ioctl
1724 * @cmd: cmd from ioctl
1725 * @arg: arg from ioctl
1726 *
1727 * LOCKING:
1728 * Takes mode config lock.
1729 *
1730 * Remove the FB specified by the user.
1731 *
1732 * Called by the user via ioctl.
1733 *
1734 * RETURNS:
1735 * Zero on success, errno on failure.
1736 */
1737int drm_mode_rmfb(struct drm_device *dev,
1738		   void *data, struct drm_file *file_priv)
1739{
1740	struct drm_mode_object *obj;
1741	struct drm_framebuffer *fb = NULL;
1742	struct drm_framebuffer *fbl = NULL;
1743	uint32_t *id = data;
1744	int ret = 0;
1745	int found = 0;
1746
1747	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1748		return -EINVAL;
1749
1750	mutex_lock(&dev->mode_config.mutex);
1751	obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1752	/* TODO check that we really get a framebuffer back. */
1753	if (!obj) {
1754		DRM_ERROR("mode invalid framebuffer id\n");
1755		ret = -EINVAL;
1756		goto out;
1757	}
1758	fb = obj_to_fb(obj);
1759
1760	list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1761		if (fb == fbl)
1762			found = 1;
1763
1764	if (!found) {
1765		DRM_ERROR("tried to remove a fb that we didn't own\n");
1766		ret = -EINVAL;
1767		goto out;
1768	}
1769
1770	/* TODO release all crtc connected to the framebuffer */
1771	/* TODO unhock the destructor from the buffer object */
1772
1773	list_del(&fb->filp_head);
1774	fb->funcs->destroy(fb);
1775
1776out:
1777	mutex_unlock(&dev->mode_config.mutex);
1778	return ret;
1779}
1780
1781/**
1782 * drm_mode_getfb - get FB info
1783 * @inode: inode from the ioctl
1784 * @filp: file * from the ioctl
1785 * @cmd: cmd from ioctl
1786 * @arg: arg from ioctl
1787 *
1788 * LOCKING:
1789 * Caller? (FIXME)
1790 *
1791 * Lookup the FB given its ID and return info about it.
1792 *
1793 * Called by the user via ioctl.
1794 *
1795 * RETURNS:
1796 * Zero on success, errno on failure.
1797 */
1798int drm_mode_getfb(struct drm_device *dev,
1799		   void *data, struct drm_file *file_priv)
1800{
1801	struct drm_mode_fb_cmd *r = data;
1802	struct drm_mode_object *obj;
1803	struct drm_framebuffer *fb;
1804	int ret = 0;
1805
1806	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1807		return -EINVAL;
1808
1809	mutex_lock(&dev->mode_config.mutex);
1810	obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1811	if (!obj) {
1812		DRM_ERROR("invalid framebuffer id\n");
1813		ret = -EINVAL;
1814		goto out;
1815	}
1816	fb = obj_to_fb(obj);
1817
1818	r->height = fb->height;
1819	r->width = fb->width;
1820	r->depth = fb->depth;
1821	r->bpp = fb->bits_per_pixel;
1822	r->pitch = fb->pitch;
1823	fb->funcs->create_handle(fb, file_priv, &r->handle);
1824
1825out:
1826	mutex_unlock(&dev->mode_config.mutex);
1827	return ret;
1828}
1829
1830int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
1831			   void *data, struct drm_file *file_priv)
1832{
1833	struct drm_clip_rect __user *clips_ptr;
1834	struct drm_clip_rect *clips = NULL;
1835	struct drm_mode_fb_dirty_cmd *r = data;
1836	struct drm_mode_object *obj;
1837	struct drm_framebuffer *fb;
1838	unsigned flags;
1839	int num_clips;
1840	int ret = 0;
1841
1842	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1843		return -EINVAL;
1844
1845	mutex_lock(&dev->mode_config.mutex);
1846	obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1847	if (!obj) {
1848		DRM_ERROR("invalid framebuffer id\n");
1849		ret = -EINVAL;
1850		goto out_err1;
1851	}
1852	fb = obj_to_fb(obj);
1853
1854	num_clips = r->num_clips;
1855	clips_ptr = (struct drm_clip_rect *)(unsigned long)r->clips_ptr;
1856
1857	if (!num_clips != !clips_ptr) {
1858		ret = -EINVAL;
1859		goto out_err1;
1860	}
1861
1862	flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
1863
1864	/* If userspace annotates copy, clips must come in pairs */
1865	if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
1866		ret = -EINVAL;
1867		goto out_err1;
1868	}
1869
1870	if (num_clips && clips_ptr) {
1871		clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
1872		if (!clips) {
1873			ret = -ENOMEM;
1874			goto out_err1;
1875		}
1876
1877		ret = copy_from_user(clips, clips_ptr,
1878				     num_clips * sizeof(*clips));
1879		if (ret) {
1880			ret = -EFAULT;
1881			goto out_err2;
1882		}
1883	}
 
 
1884
1885	if (fb->funcs->dirty) {
1886		ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
1887				       clips, num_clips);
1888	} else {
1889		ret = -ENOSYS;
1890		goto out_err2;
1891	}
1892
1893out_err2:
1894	kfree(clips);
1895out_err1:
1896	mutex_unlock(&dev->mode_config.mutex);
1897	return ret;
1898}
1899
1900
1901/**
1902 * drm_fb_release - remove and free the FBs on this file
1903 * @filp: file * from the ioctl
1904 *
1905 * LOCKING:
1906 * Takes mode config lock.
1907 *
1908 * Destroy all the FBs associated with @filp.
1909 *
1910 * Called by the user via ioctl.
1911 *
1912 * RETURNS:
1913 * Zero on success, errno on failure.
1914 */
1915void drm_fb_release(struct drm_file *priv)
1916{
1917	struct drm_device *dev = priv->minor->dev;
1918	struct drm_framebuffer *fb, *tfb;
1919
1920	mutex_lock(&dev->mode_config.mutex);
1921	list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1922		list_del(&fb->filp_head);
1923		fb->funcs->destroy(fb);
1924	}
1925	mutex_unlock(&dev->mode_config.mutex);
1926}
1927
1928/**
1929 * drm_mode_attachmode - add a mode to the user mode list
1930 * @dev: DRM device
1931 * @connector: connector to add the mode to
1932 * @mode: mode to add
1933 *
1934 * Add @mode to @connector's user mode list.
1935 */
1936static int drm_mode_attachmode(struct drm_device *dev,
1937			       struct drm_connector *connector,
1938			       struct drm_display_mode *mode)
1939{
1940	int ret = 0;
1941
1942	list_add_tail(&mode->head, &connector->user_modes);
1943	return ret;
1944}
1945
1946int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1947			     struct drm_display_mode *mode)
1948{
1949	struct drm_connector *connector;
1950	int ret = 0;
1951	struct drm_display_mode *dup_mode;
1952	int need_dup = 0;
1953	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1954		if (!connector->encoder)
1955			break;
1956		if (connector->encoder->crtc == crtc) {
1957			if (need_dup)
1958				dup_mode = drm_mode_duplicate(dev, mode);
1959			else
1960				dup_mode = mode;
1961			ret = drm_mode_attachmode(dev, connector, dup_mode);
1962			if (ret)
1963				return ret;
1964			need_dup = 1;
1965		}
1966	}
1967	return 0;
1968}
1969EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1970
1971static int drm_mode_detachmode(struct drm_device *dev,
1972			       struct drm_connector *connector,
1973			       struct drm_display_mode *mode)
1974{
1975	int found = 0;
1976	int ret = 0;
1977	struct drm_display_mode *match_mode, *t;
1978
1979	list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1980		if (drm_mode_equal(match_mode, mode)) {
1981			list_del(&match_mode->head);
1982			drm_mode_destroy(dev, match_mode);
1983			found = 1;
1984			break;
1985		}
1986	}
1987
1988	if (!found)
1989		ret = -EINVAL;
1990
1991	return ret;
1992}
1993
1994int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1995{
1996	struct drm_connector *connector;
1997
1998	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1999		drm_mode_detachmode(dev, connector, mode);
2000	}
2001	return 0;
2002}
2003EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2004
2005/**
2006 * drm_fb_attachmode - Attach a user mode to an connector
2007 * @inode: inode from the ioctl
2008 * @filp: file * from the ioctl
2009 * @cmd: cmd from ioctl
2010 * @arg: arg from ioctl
2011 *
2012 * This attaches a user specified mode to an connector.
2013 * Called by the user via ioctl.
2014 *
2015 * RETURNS:
2016 * Zero on success, errno on failure.
2017 */
2018int drm_mode_attachmode_ioctl(struct drm_device *dev,
2019			      void *data, struct drm_file *file_priv)
2020{
2021	struct drm_mode_mode_cmd *mode_cmd = data;
2022	struct drm_connector *connector;
2023	struct drm_display_mode *mode;
2024	struct drm_mode_object *obj;
2025	struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2026	int ret = 0;
2027
2028	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2029		return -EINVAL;
2030
2031	mutex_lock(&dev->mode_config.mutex);
2032
2033	obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2034	if (!obj) {
2035		ret = -EINVAL;
2036		goto out;
2037	}
2038	connector = obj_to_connector(obj);
2039
2040	mode = drm_mode_create(dev);
2041	if (!mode) {
2042		ret = -ENOMEM;
2043		goto out;
2044	}
2045
2046	drm_crtc_convert_umode(mode, umode);
2047
2048	ret = drm_mode_attachmode(dev, connector, mode);
2049out:
2050	mutex_unlock(&dev->mode_config.mutex);
2051	return ret;
2052}
2053
2054
2055/**
2056 * drm_fb_detachmode - Detach a user specified mode from an connector
2057 * @inode: inode from the ioctl
2058 * @filp: file * from the ioctl
2059 * @cmd: cmd from ioctl
2060 * @arg: arg from ioctl
2061 *
2062 * Called by the user via ioctl.
2063 *
2064 * RETURNS:
2065 * Zero on success, errno on failure.
2066 */
2067int drm_mode_detachmode_ioctl(struct drm_device *dev,
2068			      void *data, struct drm_file *file_priv)
2069{
2070	struct drm_mode_object *obj;
2071	struct drm_mode_mode_cmd *mode_cmd = data;
2072	struct drm_connector *connector;
2073	struct drm_display_mode mode;
2074	struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2075	int ret = 0;
2076
2077	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2078		return -EINVAL;
2079
2080	mutex_lock(&dev->mode_config.mutex);
2081
2082	obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2083	if (!obj) {
2084		ret = -EINVAL;
2085		goto out;
2086	}
2087	connector = obj_to_connector(obj);
2088
2089	drm_crtc_convert_umode(&mode, umode);
2090	ret = drm_mode_detachmode(dev, connector, &mode);
2091out:
2092	mutex_unlock(&dev->mode_config.mutex);
2093	return ret;
2094}
2095
2096struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2097					 const char *name, int num_values)
2098{
2099	struct drm_property *property = NULL;
2100
2101	property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2102	if (!property)
2103		return NULL;
2104
2105	if (num_values) {
2106		property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2107		if (!property->values)
2108			goto fail;
2109	}
2110
2111	drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2112	property->flags = flags;
2113	property->num_values = num_values;
2114	INIT_LIST_HEAD(&property->enum_blob_list);
2115
2116	if (name)
2117		strncpy(property->name, name, DRM_PROP_NAME_LEN);
2118
2119	list_add_tail(&property->head, &dev->mode_config.property_list);
2120	return property;
2121fail:
2122	kfree(property);
2123	return NULL;
2124}
2125EXPORT_SYMBOL(drm_property_create);
2126
2127int drm_property_add_enum(struct drm_property *property, int index,
2128			  uint64_t value, const char *name)
2129{
2130	struct drm_property_enum *prop_enum;
2131
2132	if (!(property->flags & DRM_MODE_PROP_ENUM))
2133		return -EINVAL;
2134
2135	if (!list_empty(&property->enum_blob_list)) {
2136		list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2137			if (prop_enum->value == value) {
2138				strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2139				prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2140				return 0;
2141			}
2142		}
2143	}
2144
2145	prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2146	if (!prop_enum)
2147		return -ENOMEM;
2148
2149	strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2150	prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2151	prop_enum->value = value;
2152
2153	property->values[index] = value;
2154	list_add_tail(&prop_enum->head, &property->enum_blob_list);
2155	return 0;
2156}
2157EXPORT_SYMBOL(drm_property_add_enum);
2158
2159void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2160{
2161	struct drm_property_enum *prop_enum, *pt;
2162
2163	list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2164		list_del(&prop_enum->head);
2165		kfree(prop_enum);
2166	}
2167
2168	if (property->num_values)
2169		kfree(property->values);
2170	drm_mode_object_put(dev, &property->base);
2171	list_del(&property->head);
2172	kfree(property);
2173}
2174EXPORT_SYMBOL(drm_property_destroy);
2175
2176int drm_connector_attach_property(struct drm_connector *connector,
2177			       struct drm_property *property, uint64_t init_val)
2178{
2179	int i;
2180
2181	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2182		if (connector->property_ids[i] == 0) {
2183			connector->property_ids[i] = property->base.id;
2184			connector->property_values[i] = init_val;
2185			break;
2186		}
2187	}
2188
2189	if (i == DRM_CONNECTOR_MAX_PROPERTY)
2190		return -EINVAL;
2191	return 0;
2192}
2193EXPORT_SYMBOL(drm_connector_attach_property);
2194
2195int drm_connector_property_set_value(struct drm_connector *connector,
2196				  struct drm_property *property, uint64_t value)
2197{
2198	int i;
2199
2200	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2201		if (connector->property_ids[i] == property->base.id) {
2202			connector->property_values[i] = value;
2203			break;
2204		}
2205	}
2206
2207	if (i == DRM_CONNECTOR_MAX_PROPERTY)
2208		return -EINVAL;
2209	return 0;
2210}
2211EXPORT_SYMBOL(drm_connector_property_set_value);
2212
2213int drm_connector_property_get_value(struct drm_connector *connector,
2214				  struct drm_property *property, uint64_t *val)
2215{
2216	int i;
2217
2218	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2219		if (connector->property_ids[i] == property->base.id) {
2220			*val = connector->property_values[i];
2221			break;
2222		}
2223	}
2224
2225	if (i == DRM_CONNECTOR_MAX_PROPERTY)
2226		return -EINVAL;
2227	return 0;
2228}
2229EXPORT_SYMBOL(drm_connector_property_get_value);
2230
2231int drm_mode_getproperty_ioctl(struct drm_device *dev,
2232			       void *data, struct drm_file *file_priv)
2233{
2234	struct drm_mode_object *obj;
2235	struct drm_mode_get_property *out_resp = data;
2236	struct drm_property *property;
2237	int enum_count = 0;
2238	int blob_count = 0;
2239	int value_count = 0;
2240	int ret = 0, i;
2241	int copied;
2242	struct drm_property_enum *prop_enum;
2243	struct drm_mode_property_enum __user *enum_ptr;
2244	struct drm_property_blob *prop_blob;
2245	uint32_t *blob_id_ptr;
2246	uint64_t __user *values_ptr;
2247	uint32_t __user *blob_length_ptr;
2248
2249	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2250		return -EINVAL;
2251
2252	mutex_lock(&dev->mode_config.mutex);
2253	obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2254	if (!obj) {
2255		ret = -EINVAL;
2256		goto done;
2257	}
2258	property = obj_to_property(obj);
2259
2260	if (property->flags & DRM_MODE_PROP_ENUM) {
2261		list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2262			enum_count++;
2263	} else if (property->flags & DRM_MODE_PROP_BLOB) {
2264		list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2265			blob_count++;
2266	}
2267
2268	value_count = property->num_values;
2269
2270	strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2271	out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2272	out_resp->flags = property->flags;
2273
2274	if ((out_resp->count_values >= value_count) && value_count) {
2275		values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2276		for (i = 0; i < value_count; i++) {
2277			if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2278				ret = -EFAULT;
2279				goto done;
2280			}
2281		}
2282	}
2283	out_resp->count_values = value_count;
2284
2285	if (property->flags & DRM_MODE_PROP_ENUM) {
2286		if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2287			copied = 0;
2288			enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2289			list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2290
2291				if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2292					ret = -EFAULT;
2293					goto done;
2294				}
2295
2296				if (copy_to_user(&enum_ptr[copied].name,
2297						 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2298					ret = -EFAULT;
2299					goto done;
2300				}
2301				copied++;
2302			}
2303		}
2304		out_resp->count_enum_blobs = enum_count;
2305	}
2306
2307	if (property->flags & DRM_MODE_PROP_BLOB) {
2308		if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2309			copied = 0;
2310			blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2311			blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2312
2313			list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2314				if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2315					ret = -EFAULT;
2316					goto done;
2317				}
2318
2319				if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2320					ret = -EFAULT;
2321					goto done;
2322				}
2323
2324				copied++;
2325			}
2326		}
2327		out_resp->count_enum_blobs = blob_count;
2328	}
2329done:
2330	mutex_unlock(&dev->mode_config.mutex);
2331	return ret;
2332}
2333
2334static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2335							  void *data)
2336{
2337	struct drm_property_blob *blob;
2338
2339	if (!length || !data)
2340		return NULL;
2341
2342	blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2343	if (!blob)
2344		return NULL;
2345
2346	blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2347	blob->length = length;
2348
2349	memcpy(blob->data, data, length);
2350
2351	drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2352
2353	list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2354	return blob;
2355}
2356
2357static void drm_property_destroy_blob(struct drm_device *dev,
2358			       struct drm_property_blob *blob)
2359{
2360	drm_mode_object_put(dev, &blob->base);
2361	list_del(&blob->head);
2362	kfree(blob);
2363}
2364
2365int drm_mode_getblob_ioctl(struct drm_device *dev,
2366			   void *data, struct drm_file *file_priv)
2367{
2368	struct drm_mode_object *obj;
2369	struct drm_mode_get_blob *out_resp = data;
2370	struct drm_property_blob *blob;
2371	int ret = 0;
2372	void *blob_ptr;
2373
2374	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2375		return -EINVAL;
2376
2377	mutex_lock(&dev->mode_config.mutex);
2378	obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2379	if (!obj) {
2380		ret = -EINVAL;
2381		goto done;
2382	}
2383	blob = obj_to_blob(obj);
2384
2385	if (out_resp->length == blob->length) {
2386		blob_ptr = (void *)(unsigned long)out_resp->data;
2387		if (copy_to_user(blob_ptr, blob->data, blob->length)){
2388			ret = -EFAULT;
2389			goto done;
2390		}
2391	}
2392	out_resp->length = blob->length;
2393
2394done:
2395	mutex_unlock(&dev->mode_config.mutex);
2396	return ret;
2397}
2398
2399int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2400					    struct edid *edid)
2401{
2402	struct drm_device *dev = connector->dev;
2403	int ret = 0, size;
2404
2405	if (connector->edid_blob_ptr)
2406		drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2407
2408	/* Delete edid, when there is none. */
2409	if (!edid) {
2410		connector->edid_blob_ptr = NULL;
2411		ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2412		return ret;
2413	}
2414
2415	size = EDID_LENGTH * (1 + edid->extensions);
2416	connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2417							    size, edid);
2418
2419	ret = drm_connector_property_set_value(connector,
2420					       dev->mode_config.edid_property,
2421					       connector->edid_blob_ptr->base.id);
2422
2423	return ret;
2424}
2425EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2426
2427int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2428				       void *data, struct drm_file *file_priv)
2429{
2430	struct drm_mode_connector_set_property *out_resp = data;
2431	struct drm_mode_object *obj;
2432	struct drm_property *property;
2433	struct drm_connector *connector;
2434	int ret = -EINVAL;
2435	int i;
2436
2437	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2438		return -EINVAL;
2439
2440	mutex_lock(&dev->mode_config.mutex);
2441
2442	obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2443	if (!obj) {
2444		goto out;
2445	}
2446	connector = obj_to_connector(obj);
2447
2448	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2449		if (connector->property_ids[i] == out_resp->prop_id)
2450			break;
2451	}
2452
2453	if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2454		goto out;
2455	}
2456
2457	obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2458	if (!obj) {
2459		goto out;
2460	}
2461	property = obj_to_property(obj);
2462
2463	if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2464		goto out;
2465
2466	if (property->flags & DRM_MODE_PROP_RANGE) {
2467		if (out_resp->value < property->values[0])
2468			goto out;
2469
2470		if (out_resp->value > property->values[1])
2471			goto out;
2472	} else {
2473		int found = 0;
2474		for (i = 0; i < property->num_values; i++) {
2475			if (property->values[i] == out_resp->value) {
2476				found = 1;
2477				break;
2478			}
2479		}
2480		if (!found) {
2481			goto out;
2482		}
2483	}
2484
2485	/* Do DPMS ourselves */
2486	if (property == connector->dev->mode_config.dpms_property) {
2487		if (connector->funcs->dpms)
2488			(*connector->funcs->dpms)(connector, (int) out_resp->value);
2489		ret = 0;
2490	} else if (connector->funcs->set_property)
2491		ret = connector->funcs->set_property(connector, property, out_resp->value);
2492
2493	/* store the property value if successful */
2494	if (!ret)
2495		drm_connector_property_set_value(connector, property, out_resp->value);
2496out:
2497	mutex_unlock(&dev->mode_config.mutex);
2498	return ret;
2499}
2500
2501int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2502				      struct drm_encoder *encoder)
2503{
2504	int i;
2505
2506	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2507		if (connector->encoder_ids[i] == 0) {
2508			connector->encoder_ids[i] = encoder->base.id;
2509			return 0;
2510		}
2511	}
2512	return -ENOMEM;
2513}
2514EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2515
2516void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2517				    struct drm_encoder *encoder)
2518{
2519	int i;
2520	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2521		if (connector->encoder_ids[i] == encoder->base.id) {
2522			connector->encoder_ids[i] = 0;
2523			if (connector->encoder == encoder)
2524				connector->encoder = NULL;
2525			break;
2526		}
2527	}
2528}
2529EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2530
2531bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2532				  int gamma_size)
2533{
2534	crtc->gamma_size = gamma_size;
2535
2536	crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2537	if (!crtc->gamma_store) {
2538		crtc->gamma_size = 0;
2539		return false;
2540	}
2541
2542	return true;
2543}
2544EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2545
2546int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2547			     void *data, struct drm_file *file_priv)
2548{
2549	struct drm_mode_crtc_lut *crtc_lut = data;
2550	struct drm_mode_object *obj;
2551	struct drm_crtc *crtc;
2552	void *r_base, *g_base, *b_base;
2553	int size;
2554	int ret = 0;
2555
2556	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2557		return -EINVAL;
2558
2559	mutex_lock(&dev->mode_config.mutex);
2560	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2561	if (!obj) {
2562		ret = -EINVAL;
2563		goto out;
2564	}
2565	crtc = obj_to_crtc(obj);
2566
2567	/* memcpy into gamma store */
2568	if (crtc_lut->gamma_size != crtc->gamma_size) {
2569		ret = -EINVAL;
2570		goto out;
2571	}
2572
2573	size = crtc_lut->gamma_size * (sizeof(uint16_t));
2574	r_base = crtc->gamma_store;
2575	if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2576		ret = -EFAULT;
2577		goto out;
2578	}
2579
2580	g_base = r_base + size;
2581	if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2582		ret = -EFAULT;
2583		goto out;
2584	}
2585
2586	b_base = g_base + size;
2587	if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2588		ret = -EFAULT;
2589		goto out;
2590	}
2591
2592	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
2593
2594out:
2595	mutex_unlock(&dev->mode_config.mutex);
2596	return ret;
2597
2598}
2599
2600int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2601			     void *data, struct drm_file *file_priv)
2602{
2603	struct drm_mode_crtc_lut *crtc_lut = data;
2604	struct drm_mode_object *obj;
2605	struct drm_crtc *crtc;
2606	void *r_base, *g_base, *b_base;
2607	int size;
2608	int ret = 0;
2609
2610	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2611		return -EINVAL;
2612
2613	mutex_lock(&dev->mode_config.mutex);
2614	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2615	if (!obj) {
2616		ret = -EINVAL;
2617		goto out;
2618	}
2619	crtc = obj_to_crtc(obj);
2620
2621	/* memcpy into gamma store */
2622	if (crtc_lut->gamma_size != crtc->gamma_size) {
2623		ret = -EINVAL;
2624		goto out;
2625	}
2626
2627	size = crtc_lut->gamma_size * (sizeof(uint16_t));
2628	r_base = crtc->gamma_store;
2629	if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2630		ret = -EFAULT;
2631		goto out;
2632	}
2633
2634	g_base = r_base + size;
2635	if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2636		ret = -EFAULT;
2637		goto out;
2638	}
2639
2640	b_base = g_base + size;
2641	if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2642		ret = -EFAULT;
2643		goto out;
2644	}
2645out:
2646	mutex_unlock(&dev->mode_config.mutex);
2647	return ret;
2648}
2649
2650int drm_mode_page_flip_ioctl(struct drm_device *dev,
2651			     void *data, struct drm_file *file_priv)
2652{
2653	struct drm_mode_crtc_page_flip *page_flip = data;
2654	struct drm_mode_object *obj;
2655	struct drm_crtc *crtc;
2656	struct drm_framebuffer *fb;
2657	struct drm_pending_vblank_event *e = NULL;
2658	unsigned long flags;
2659	int ret = -EINVAL;
2660
2661	if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
2662	    page_flip->reserved != 0)
2663		return -EINVAL;
2664
2665	mutex_lock(&dev->mode_config.mutex);
2666	obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
2667	if (!obj)
2668		goto out;
2669	crtc = obj_to_crtc(obj);
2670
2671	if (crtc->fb == NULL) {
2672		/* The framebuffer is currently unbound, presumably
2673		 * due to a hotplug event, that userspace has not
2674		 * yet discovered.
2675		 */
2676		ret = -EBUSY;
2677		goto out;
2678	}
2679
2680	if (crtc->funcs->page_flip == NULL)
2681		goto out;
2682
2683	obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
2684	if (!obj)
2685		goto out;
2686	fb = obj_to_fb(obj);
2687
2688	if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2689		ret = -ENOMEM;
2690		spin_lock_irqsave(&dev->event_lock, flags);
2691		if (file_priv->event_space < sizeof e->event) {
2692			spin_unlock_irqrestore(&dev->event_lock, flags);
2693			goto out;
2694		}
2695		file_priv->event_space -= sizeof e->event;
2696		spin_unlock_irqrestore(&dev->event_lock, flags);
2697
2698		e = kzalloc(sizeof *e, GFP_KERNEL);
2699		if (e == NULL) {
2700			spin_lock_irqsave(&dev->event_lock, flags);
2701			file_priv->event_space += sizeof e->event;
2702			spin_unlock_irqrestore(&dev->event_lock, flags);
2703			goto out;
2704		}
2705
2706		e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2707		e->event.base.length = sizeof e->event;
2708		e->event.user_data = page_flip->user_data;
2709		e->base.event = &e->event.base;
2710		e->base.file_priv = file_priv;
2711		e->base.destroy =
2712			(void (*) (struct drm_pending_event *)) kfree;
2713	}
2714
2715	ret = crtc->funcs->page_flip(crtc, fb, e);
2716	if (ret) {
2717		spin_lock_irqsave(&dev->event_lock, flags);
2718		file_priv->event_space += sizeof e->event;
2719		spin_unlock_irqrestore(&dev->event_lock, flags);
2720		kfree(e);
2721	}
2722
2723out:
2724	mutex_unlock(&dev->mode_config.mutex);
2725	return ret;
2726}
2727
2728void drm_mode_config_reset(struct drm_device *dev)
2729{
2730	struct drm_crtc *crtc;
2731	struct drm_encoder *encoder;
2732	struct drm_connector *connector;
2733
2734	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2735		if (crtc->funcs->reset)
2736			crtc->funcs->reset(crtc);
2737
2738	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
2739		if (encoder->funcs->reset)
2740			encoder->funcs->reset(encoder);
2741
2742	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
2743		if (connector->funcs->reset)
2744			connector->funcs->reset(connector);
2745}
2746EXPORT_SYMBOL(drm_mode_config_reset);
2747
2748int drm_mode_create_dumb_ioctl(struct drm_device *dev,
2749			       void *data, struct drm_file *file_priv)
2750{
2751	struct drm_mode_create_dumb *args = data;
2752
2753	if (!dev->driver->dumb_create)
2754		return -ENOSYS;
2755	return dev->driver->dumb_create(file_priv, dev, args);
2756}
2757
2758int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
2759			     void *data, struct drm_file *file_priv)
2760{
2761	struct drm_mode_map_dumb *args = data;
2762
2763	/* call driver ioctl to get mmap offset */
2764	if (!dev->driver->dumb_map_offset)
2765		return -ENOSYS;
2766
2767	return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
2768}
2769
2770int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
2771				void *data, struct drm_file *file_priv)
2772{
2773	struct drm_mode_destroy_dumb *args = data;
2774
2775	if (!dev->driver->dumb_destroy)
2776		return -ENOSYS;
2777
2778	return dev->driver->dumb_destroy(file_priv, dev, args->handle);
2779}