Subversion Repositories Kolibri OS

Rev

Rev 4560 | Rev 5271 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1123 serge 1
/*
2
 * Copyright (c) 2006-2008 Intel Corporation
3
 * Copyright (c) 2007 Dave Airlie 
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 
29
 *      Dave Airlie 
30
 *      Jesse Barnes 
31
 */
4075 Serge 32
#include 
1179 serge 33
#include 
1963 serge 34
#include 
3031 serge 35
#include 
36
#include 
37
#include 
38
#include 
39
#include 
5060 serge 40
#include 
1123 serge 41
 
5060 serge 42
#include "drm_crtc_internal.h"
43
 
44
static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
45
							struct drm_mode_fb_cmd2 *r,
46
							struct drm_file *file_priv);
47
 
3480 Serge 48
/**
49
 * drm_modeset_lock_all - take all modeset locks
50
 * @dev: drm device
51
 *
52
 * This function takes all modeset locks, suitable where a more fine-grained
5060 serge 53
 * scheme isn't (yet) implemented. Locks must be dropped with
54
 * drm_modeset_unlock_all.
3480 Serge 55
 */
56
void drm_modeset_lock_all(struct drm_device *dev)
57
{
5060 serge 58
	struct drm_mode_config *config = &dev->mode_config;
59
	struct drm_modeset_acquire_ctx *ctx;
60
	int ret;
3480 Serge 61
 
5060 serge 62
	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
63
	if (WARN_ON(!ctx))
64
		return;
3480 Serge 65
 
5060 serge 66
	mutex_lock(&config->mutex);
67
 
68
	drm_modeset_acquire_init(ctx, 0);
69
 
70
retry:
71
	ret = drm_modeset_lock(&config->connection_mutex, ctx);
72
	if (ret)
73
		goto fail;
74
	ret = drm_modeset_lock_all_crtcs(dev, ctx);
75
	if (ret)
76
		goto fail;
77
 
78
	WARN_ON(config->acquire_ctx);
79
 
80
	/* now we hold the locks, so now that it is safe, stash the
81
	 * ctx for drm_modeset_unlock_all():
82
	 */
83
	config->acquire_ctx = ctx;
84
 
85
	drm_warn_on_modeset_not_all_locked(dev);
86
 
87
	return;
88
 
89
fail:
90
	if (ret == -EDEADLK) {
91
		drm_modeset_backoff(ctx);
92
		goto retry;
93
	}
3480 Serge 94
}
95
EXPORT_SYMBOL(drm_modeset_lock_all);
96
 
97
/**
98
 * drm_modeset_unlock_all - drop all modeset locks
99
 * @dev: device
5060 serge 100
 *
101
 * This function drop all modeset locks taken by drm_modeset_lock_all.
3480 Serge 102
 */
103
void drm_modeset_unlock_all(struct drm_device *dev)
104
{
5060 serge 105
	struct drm_mode_config *config = &dev->mode_config;
106
	struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
3480 Serge 107
 
5060 serge 108
	if (WARN_ON(!ctx))
109
		return;
3480 Serge 110
 
5060 serge 111
	config->acquire_ctx = NULL;
112
	drm_modeset_drop_locks(ctx);
113
	drm_modeset_acquire_fini(ctx);
114
 
115
	kfree(ctx);
116
 
3480 Serge 117
	mutex_unlock(&dev->mode_config.mutex);
118
}
119
EXPORT_SYMBOL(drm_modeset_unlock_all);
120
 
121
/**
122
 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
123
 * @dev: device
5060 serge 124
 *
125
 * Useful as a debug assert.
3480 Serge 126
 */
127
void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
128
{
129
	struct drm_crtc *crtc;
130
 
131
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
5060 serge 132
		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
3480 Serge 133
 
5060 serge 134
	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
3480 Serge 135
	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
136
}
137
EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
138
 
1123 serge 139
/* Avoid boilerplate.  I'm tired of typing. */
140
#define DRM_ENUM_NAME_FN(fnname, list)				\
4075 Serge 141
	const char *fnname(int val)				\
1123 serge 142
	{							\
143
		int i;						\
144
		for (i = 0; i < ARRAY_SIZE(list); i++) {	\
145
			if (list[i].type == val)		\
146
				return list[i].name;		\
147
		}						\
148
		return "(unknown)";				\
149
	}
150
 
151
/*
152
 * Global properties
153
 */
4075 Serge 154
static const struct drm_prop_enum_list drm_dpms_enum_list[] =
1123 serge 155
{	{ DRM_MODE_DPMS_ON, "On" },
156
	{ DRM_MODE_DPMS_STANDBY, "Standby" },
157
	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
158
	{ DRM_MODE_DPMS_OFF, "Off" }
159
};
160
 
161
DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
162
 
5060 serge 163
static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
164
{
165
	{ DRM_PLANE_TYPE_OVERLAY, "Overlay" },
166
	{ DRM_PLANE_TYPE_PRIMARY, "Primary" },
167
	{ DRM_PLANE_TYPE_CURSOR, "Cursor" },
168
};
169
 
1123 serge 170
/*
171
 * Optional properties
172
 */
4075 Serge 173
static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
1123 serge 174
{
1179 serge 175
	{ DRM_MODE_SCALE_NONE, "None" },
176
	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
177
	{ DRM_MODE_SCALE_CENTER, "Center" },
178
	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
1123 serge 179
};
180
 
5060 serge 181
static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
182
	{ DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
183
	{ DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
184
	{ DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
185
};
186
 
1123 serge 187
/*
188
 * Non-global properties, but "required" for certain connectors.
189
 */
4075 Serge 190
static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
1123 serge 191
{
192
	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
193
	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
194
	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
195
};
196
 
197
DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
198
 
4075 Serge 199
static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
1123 serge 200
{
201
	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
202
	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
203
	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
204
};
205
 
206
DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
207
		 drm_dvi_i_subconnector_enum_list)
208
 
4075 Serge 209
static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
1123 serge 210
{
211
	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
212
	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
213
	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
214
	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1179 serge 215
	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
1123 serge 216
};
217
 
218
DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
219
 
4075 Serge 220
static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
1123 serge 221
{
222
	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
223
	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
224
	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
225
	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1179 serge 226
	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
1123 serge 227
};
228
 
229
DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
230
		 drm_tv_subconnector_enum_list)
231
 
4075 Serge 232
static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
1321 serge 233
	{ DRM_MODE_DIRTY_OFF,      "Off"      },
234
	{ DRM_MODE_DIRTY_ON,       "On"       },
235
	{ DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
236
};
237
 
1123 serge 238
struct drm_conn_prop_enum_list {
239
	int type;
4075 Serge 240
	const char *name;
4104 Serge 241
	struct ida ida;
1123 serge 242
};
243
 
244
/*
245
 * Connector and encoder types.
246
 */
247
static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
4104 Serge 248
{	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
249
	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
250
	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
251
	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
252
	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
253
	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
254
	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
255
	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
256
	{ DRM_MODE_CONNECTOR_Component, "Component" },
257
	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
258
	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
259
	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
260
	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
261
	{ DRM_MODE_CONNECTOR_TV, "TV" },
262
	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
263
	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
4560 Serge 264
	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
1123 serge 265
};
266
 
4075 Serge 267
static const struct drm_prop_enum_list drm_encoder_enum_list[] =
1123 serge 268
{	{ DRM_MODE_ENCODER_NONE, "None" },
269
	{ DRM_MODE_ENCODER_DAC, "DAC" },
270
	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
271
	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
272
	{ DRM_MODE_ENCODER_TVDAC, "TV" },
3031 serge 273
	{ DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
4560 Serge 274
	{ DRM_MODE_ENCODER_DSI, "DSI" },
5060 serge 275
	{ DRM_MODE_ENCODER_DPMST, "DP MST" },
1123 serge 276
};
277
 
5060 serge 278
static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
279
{
280
	{ SubPixelUnknown, "Unknown" },
281
	{ SubPixelHorizontalRGB, "Horizontal RGB" },
282
	{ SubPixelHorizontalBGR, "Horizontal BGR" },
283
	{ SubPixelVerticalRGB, "Vertical RGB" },
284
	{ SubPixelVerticalBGR, "Vertical BGR" },
285
	{ SubPixelNone, "None" },
286
};
287
 
4104 Serge 288
void drm_connector_ida_init(void)
289
{
290
	int i;
291
 
292
	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
293
		ida_init(&drm_connector_enum_list[i].ida);
294
}
295
 
296
void drm_connector_ida_destroy(void)
297
{
298
	int i;
299
 
300
	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
301
		ida_destroy(&drm_connector_enum_list[i].ida);
302
}
303
 
5060 serge 304
/**
305
 * drm_get_connector_status_name - return a string for connector status
306
 * @status: connector status to compute name of
307
 *
308
 * In contrast to the other drm_get_*_name functions this one here returns a
309
 * const pointer and hence is threadsafe.
310
 */
4075 Serge 311
const char *drm_get_connector_status_name(enum drm_connector_status status)
1123 serge 312
{
313
	if (status == connector_status_connected)
314
		return "connected";
315
	else if (status == connector_status_disconnected)
316
		return "disconnected";
317
	else
318
		return "unknown";
319
}
3764 Serge 320
EXPORT_SYMBOL(drm_get_connector_status_name);
1123 serge 321
 
5060 serge 322
/**
323
 * drm_get_subpixel_order_name - return a string for a given subpixel enum
324
 * @order: enum of subpixel_order
325
 *
326
 * Note you could abuse this and return something out of bounds, but that
327
 * would be a caller error.  No unscrubbed user data should make it here.
328
 */
329
const char *drm_get_subpixel_order_name(enum subpixel_order order)
330
{
331
	return drm_subpixel_enum_list[order].name;
332
}
333
EXPORT_SYMBOL(drm_get_subpixel_order_name);
334
 
4075 Serge 335
static char printable_char(int c)
336
{
337
	return isascii(c) && isprint(c) ? c : '?';
338
}
339
 
5060 serge 340
/**
341
 * drm_get_format_name - return a string for drm fourcc format
342
 * @format: format to compute name of
343
 *
344
 * Note that the buffer used by this function is globally shared and owned by
345
 * the function itself.
346
 *
347
 * FIXME: This isn't really multithreading safe.
348
 */
4075 Serge 349
const char *drm_get_format_name(uint32_t format)
350
{
351
	static char buf[32];
352
 
353
	snprintf(buf, sizeof(buf),
354
		 "%c%c%c%c %s-endian (0x%08x)",
355
		 printable_char(format & 0xff),
356
		 printable_char((format >> 8) & 0xff),
357
		 printable_char((format >> 16) & 0xff),
358
		 printable_char((format >> 24) & 0x7f),
359
		 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
360
		 format);
361
 
362
	return buf;
363
}
364
EXPORT_SYMBOL(drm_get_format_name);
365
 
5060 serge 366
/*
367
 * Internal function to assign a slot in the object idr and optionally
368
 * register the object into the idr.
1123 serge 369
 */
5060 serge 370
static int drm_mode_object_get_reg(struct drm_device *dev,
371
				   struct drm_mode_object *obj,
372
				   uint32_t obj_type,
373
				   bool register_obj)
1123 serge 374
{
375
	int ret;
376
 
3480 Serge 377
	mutex_lock(&dev->mode_config.idr_mutex);
5060 serge 378
	ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
3480 Serge 379
	if (ret >= 0) {
380
		/*
381
		 * Set up the object linking under the protection of the idr
382
		 * lock so that other users can't see inconsistent state.
383
		 */
384
		obj->id = ret;
385
		obj->type = obj_type;
1123 serge 386
	}
1179 serge 387
	mutex_unlock(&dev->mode_config.idr_mutex);
1123 serge 388
 
3480 Serge 389
	return ret < 0 ? ret : 0;
1123 serge 390
}
391
 
392
/**
5060 serge 393
 * drm_mode_object_get - allocate a new modeset identifier
394
 * @dev: DRM device
395
 * @obj: object pointer, used to generate unique ID
396
 * @obj_type: object type
397
 *
398
 * Create a unique identifier based on @ptr in @dev's identifier space.  Used
399
 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
400
 * modeset identifiers are _not_ reference counted. Hence don't use this for
401
 * reference counted modeset objects like framebuffers.
402
 *
403
 * Returns:
404
 * New unique (relative to other objects in @dev) integer identifier for the
405
 * object.
406
 */
407
int drm_mode_object_get(struct drm_device *dev,
408
			       struct drm_mode_object *obj, uint32_t obj_type)
409
{
410
	return drm_mode_object_get_reg(dev, obj, obj_type, true);
411
}
412
 
413
static void drm_mode_object_register(struct drm_device *dev,
414
				     struct drm_mode_object *obj)
415
{
416
	mutex_lock(&dev->mode_config.idr_mutex);
417
	idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
418
	mutex_unlock(&dev->mode_config.idr_mutex);
419
}
420
 
421
/**
3480 Serge 422
 * drm_mode_object_put - free a modeset identifer
1123 serge 423
 * @dev: DRM device
3480 Serge 424
 * @object: object to free
1123 serge 425
 *
5060 serge 426
 * Free @id from @dev's unique identifier pool. Note that despite the _get
427
 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
428
 * for reference counted modeset objects like framebuffers.
1123 serge 429
 */
5060 serge 430
void drm_mode_object_put(struct drm_device *dev,
1123 serge 431
				struct drm_mode_object *object)
432
{
1179 serge 433
	mutex_lock(&dev->mode_config.idr_mutex);
1123 serge 434
	idr_remove(&dev->mode_config.crtc_idr, object->id);
1179 serge 435
	mutex_unlock(&dev->mode_config.idr_mutex);
1123 serge 436
}
437
 
5060 serge 438
static struct drm_mode_object *_object_find(struct drm_device *dev,
439
		uint32_t id, uint32_t type)
440
{
441
	struct drm_mode_object *obj = NULL;
442
 
443
	mutex_lock(&dev->mode_config.idr_mutex);
444
	obj = idr_find(&dev->mode_config.crtc_idr, id);
445
	if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
446
		obj = NULL;
447
	if (obj && obj->id != id)
448
		obj = NULL;
449
	/* don't leak out unref'd fb's */
450
	if (obj && (obj->type == DRM_MODE_OBJECT_FB))
451
		obj = NULL;
452
	mutex_unlock(&dev->mode_config.idr_mutex);
453
 
454
	return obj;
455
}
456
 
3480 Serge 457
/**
458
 * drm_mode_object_find - look up a drm object with static lifetime
459
 * @dev: drm device
460
 * @id: id of the mode object
461
 * @type: type of the mode object
462
 *
463
 * Note that framebuffers cannot be looked up with this functions - since those
5060 serge 464
 * are reference counted, they need special treatment.  Even with
465
 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
466
 * rather than WARN_ON()).
3480 Serge 467
 */
1321 serge 468
struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
469
		uint32_t id, uint32_t type)
1123 serge 470
{
471
	struct drm_mode_object *obj = NULL;
472
 
3480 Serge 473
	/* Framebuffers are reference counted and need their own lookup
474
	 * function.*/
475
	WARN_ON(type == DRM_MODE_OBJECT_FB);
5060 serge 476
	obj = _object_find(dev, id, type);
1123 serge 477
	return obj;
478
}
479
EXPORT_SYMBOL(drm_mode_object_find);
480
 
481
/**
482
 * drm_framebuffer_init - initialize a framebuffer
483
 * @dev: DRM device
3480 Serge 484
 * @fb: framebuffer to be initialized
485
 * @funcs: ... with these functions
1123 serge 486
 *
487
 * Allocates an ID for the framebuffer's parent mode object, sets its mode
488
 * functions & device file and adds it to the master fd list.
489
 *
3480 Serge 490
 * IMPORTANT:
491
 * This functions publishes the fb and makes it available for concurrent access
492
 * by other users. Which means by this point the fb _must_ be fully set up -
493
 * since all the fb attributes are invariant over its lifetime, no further
494
 * locking but only correct reference counting is required.
495
 *
5060 serge 496
 * Returns:
1321 serge 497
 * Zero on success, error code on failure.
1123 serge 498
 */
499
int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
500
			 const struct drm_framebuffer_funcs *funcs)
501
{
502
	int ret;
1963 serge 503
 
3480 Serge 504
	mutex_lock(&dev->mode_config.fb_lock);
505
	kref_init(&fb->refcount);
506
	INIT_LIST_HEAD(&fb->filp_head);
507
	fb->dev = dev;
508
	fb->funcs = funcs;
509
 
1123 serge 510
	ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
3031 serge 511
	if (ret)
3480 Serge 512
		goto out;
1123 serge 513
 
3480 Serge 514
	/* Grab the idr reference. */
515
	drm_framebuffer_reference(fb);
516
 
1123 serge 517
	dev->mode_config.num_fb++;
518
	list_add(&fb->head, &dev->mode_config.fb_list);
3480 Serge 519
out:
520
	mutex_unlock(&dev->mode_config.fb_lock);
1123 serge 521
 
522
	return 0;
523
}
524
EXPORT_SYMBOL(drm_framebuffer_init);
525
 
3480 Serge 526
static void drm_framebuffer_free(struct kref *kref)
527
{
528
	struct drm_framebuffer *fb =
529
			container_of(kref, struct drm_framebuffer, refcount);
530
	fb->funcs->destroy(fb);
531
}
532
 
533
static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
534
							uint32_t id)
535
{
536
	struct drm_mode_object *obj = NULL;
537
	struct drm_framebuffer *fb;
538
 
539
	mutex_lock(&dev->mode_config.idr_mutex);
540
	obj = idr_find(&dev->mode_config.crtc_idr, id);
541
	if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
542
		fb = NULL;
543
	else
544
		fb = obj_to_fb(obj);
545
	mutex_unlock(&dev->mode_config.idr_mutex);
546
 
547
	return fb;
548
}
549
 
1123 serge 550
/**
3480 Serge 551
 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
552
 * @dev: drm device
553
 * @id: id of the fb object
554
 *
555
 * If successful, this grabs an additional reference to the framebuffer -
556
 * callers need to make sure to eventually unreference the returned framebuffer
5060 serge 557
 * again, using @drm_framebuffer_unreference.
3480 Serge 558
 */
559
struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
560
					       uint32_t id)
561
{
562
	struct drm_framebuffer *fb;
563
 
564
	mutex_lock(&dev->mode_config.fb_lock);
565
	fb = __drm_framebuffer_lookup(dev, id);
566
	if (fb)
4075 Serge 567
		drm_framebuffer_reference(fb);
3480 Serge 568
	mutex_unlock(&dev->mode_config.fb_lock);
569
 
570
	return fb;
571
}
572
EXPORT_SYMBOL(drm_framebuffer_lookup);
573
 
574
/**
575
 * drm_framebuffer_unreference - unref a framebuffer
576
 * @fb: framebuffer to unref
577
 *
578
 * This functions decrements the fb's refcount and frees it if it drops to zero.
579
 */
580
void drm_framebuffer_unreference(struct drm_framebuffer *fb)
581
{
5060 serge 582
	DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
3480 Serge 583
	kref_put(&fb->refcount, drm_framebuffer_free);
584
}
585
EXPORT_SYMBOL(drm_framebuffer_unreference);
586
 
587
/**
588
 * drm_framebuffer_reference - incr the fb refcnt
589
 * @fb: framebuffer
5060 serge 590
 *
591
 * This functions increments the fb's refcount.
3480 Serge 592
 */
593
void drm_framebuffer_reference(struct drm_framebuffer *fb)
594
{
5060 serge 595
	DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
3480 Serge 596
	kref_get(&fb->refcount);
597
}
598
EXPORT_SYMBOL(drm_framebuffer_reference);
599
 
600
static void drm_framebuffer_free_bug(struct kref *kref)
601
{
602
	BUG();
603
}
604
 
605
static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
606
{
5060 serge 607
	DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
3480 Serge 608
	kref_put(&fb->refcount, drm_framebuffer_free_bug);
609
}
610
 
611
/* dev->mode_config.fb_lock must be held! */
612
static void __drm_framebuffer_unregister(struct drm_device *dev,
613
					 struct drm_framebuffer *fb)
614
{
615
	mutex_lock(&dev->mode_config.idr_mutex);
616
	idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
617
	mutex_unlock(&dev->mode_config.idr_mutex);
618
 
619
	fb->base.id = 0;
620
 
621
	__drm_framebuffer_unreference(fb);
622
}
623
 
624
/**
625
 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
626
 * @fb: fb to unregister
627
 *
628
 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
629
 * those used for fbdev. Note that the caller must hold a reference of it's own,
630
 * i.e. the object may not be destroyed through this call (since it'll lead to a
631
 * locking inversion).
632
 */
633
void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
634
{
635
	struct drm_device *dev = fb->dev;
636
 
637
	mutex_lock(&dev->mode_config.fb_lock);
638
	/* Mark fb as reaped and drop idr ref. */
639
	__drm_framebuffer_unregister(dev, fb);
640
	mutex_unlock(&dev->mode_config.fb_lock);
641
}
642
EXPORT_SYMBOL(drm_framebuffer_unregister_private);
643
 
644
/**
1123 serge 645
 * drm_framebuffer_cleanup - remove a framebuffer object
646
 * @fb: framebuffer to remove
647
 *
5060 serge 648
 * Cleanup framebuffer. This function is intended to be used from the drivers
649
 * ->destroy callback. It can also be used to clean up driver private
650
 *  framebuffers embedded into a larger structure.
1123 serge 651
 *
3480 Serge 652
 * Note that this function does not remove the fb from active usuage - if it is
653
 * still used anywhere, hilarity can ensue since userspace could call getfb on
654
 * the id and get back -EINVAL. Obviously no concern at driver unload time.
655
 *
656
 * Also, the framebuffer will not be removed from the lookup idr - for
657
 * user-created framebuffers this will happen in in the rmfb ioctl. For
658
 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
659
 * drm_framebuffer_unregister_private.
1123 serge 660
 */
661
void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
662
{
663
	struct drm_device *dev = fb->dev;
3480 Serge 664
 
665
	mutex_lock(&dev->mode_config.fb_lock);
1123 serge 666
	list_del(&fb->head);
667
	dev->mode_config.num_fb--;
3480 Serge 668
	mutex_unlock(&dev->mode_config.fb_lock);
1123 serge 669
}
670
EXPORT_SYMBOL(drm_framebuffer_cleanup);
671
 
3480 Serge 672
/**
673
 * drm_framebuffer_remove - remove and unreference a framebuffer object
674
 * @fb: framebuffer to remove
675
 *
676
 * Scans all the CRTCs and planes in @dev's mode_config.  If they're
677
 * using @fb, removes it, setting it to NULL. Then drops the reference to the
678
 * passed-in framebuffer. Might take the modeset locks.
679
 *
680
 * Note that this function optimizes the cleanup away if the caller holds the
681
 * last reference to the framebuffer. It is also guaranteed to not take the
682
 * modeset locks in this case.
683
 */
684
void drm_framebuffer_remove(struct drm_framebuffer *fb)
685
{
686
	struct drm_device *dev = fb->dev;
687
	struct drm_crtc *crtc;
688
	struct drm_plane *plane;
689
	struct drm_mode_set set;
690
	int ret;
3031 serge 691
 
3480 Serge 692
	WARN_ON(!list_empty(&fb->filp_head));
3031 serge 693
 
3480 Serge 694
	/*
695
	 * drm ABI mandates that we remove any deleted framebuffers from active
696
	 * useage. But since most sane clients only remove framebuffers they no
697
	 * longer need, try to optimize this away.
698
	 *
699
	 * Since we're holding a reference ourselves, observing a refcount of 1
700
	 * means that we're the last holder and can skip it. Also, the refcount
701
	 * can never increase from 1 again, so we don't need any barriers or
702
	 * locks.
703
	 *
704
	 * Note that userspace could try to race with use and instate a new
705
	 * usage _after_ we've cleared all current ones. End result will be an
706
	 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
707
	 * in this manner.
708
	 */
709
	if (atomic_read(&fb->refcount.refcount) > 1) {
710
		drm_modeset_lock_all(dev);
711
		/* remove from any CRTC */
712
		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5060 serge 713
			if (crtc->primary->fb == fb) {
3480 Serge 714
				/* should turn off the crtc */
715
				memset(&set, 0, sizeof(struct drm_mode_set));
716
				set.crtc = crtc;
717
				set.fb = NULL;
718
				ret = drm_mode_set_config_internal(&set);
719
				if (ret)
720
					DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
721
			}
722
		}
3031 serge 723
 
3480 Serge 724
		list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
4075 Serge 725
			if (plane->fb == fb)
726
				drm_plane_force_disable(plane);
3480 Serge 727
		}
728
		drm_modeset_unlock_all(dev);
729
	}
3031 serge 730
 
3480 Serge 731
	drm_framebuffer_unreference(fb);
732
}
733
EXPORT_SYMBOL(drm_framebuffer_remove);
734
 
5060 serge 735
DEFINE_WW_CLASS(crtc_ww_class);
736
 
1123 serge 737
/**
5060 serge 738
 * drm_crtc_init_with_planes - Initialise a new CRTC object with
739
 *    specified primary and cursor planes.
1123 serge 740
 * @dev: DRM device
741
 * @crtc: CRTC object to init
5060 serge 742
 * @primary: Primary plane for CRTC
743
 * @cursor: Cursor plane for CRTC
1123 serge 744
 * @funcs: callbacks for the new CRTC
745
 *
4075 Serge 746
 * Inits a new object created as base part of a driver crtc object.
3031 serge 747
 *
5060 serge 748
 * Returns:
3031 serge 749
 * Zero on success, error code on failure.
1123 serge 750
 */
5060 serge 751
int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
752
			      struct drm_plane *primary,
753
			      struct drm_plane *cursor,
1123 serge 754
		   const struct drm_crtc_funcs *funcs)
755
{
5060 serge 756
	struct drm_mode_config *config = &dev->mode_config;
3031 serge 757
	int ret;
758
 
1123 serge 759
	crtc->dev = dev;
760
	crtc->funcs = funcs;
3031 serge 761
	crtc->invert_dimensions = false;
1123 serge 762
 
3480 Serge 763
	drm_modeset_lock_all(dev);
5060 serge 764
	drm_modeset_lock_init(&crtc->mutex);
765
	/* dropped by _unlock_all(): */
766
	drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
1123 serge 767
 
3031 serge 768
	ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
769
	if (ret)
770
		goto out;
771
 
772
	crtc->base.properties = &crtc->properties;
773
 
5060 serge 774
	list_add_tail(&crtc->head, &config->crtc_list);
775
	config->num_crtc++;
3031 serge 776
 
5060 serge 777
	crtc->primary = primary;
778
	crtc->cursor = cursor;
779
	if (primary)
780
		primary->possible_crtcs = 1 << drm_crtc_index(crtc);
781
	if (cursor)
782
		cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
783
 
3031 serge 784
 out:
3480 Serge 785
	drm_modeset_unlock_all(dev);
3031 serge 786
 
787
	return ret;
1123 serge 788
}
5060 serge 789
EXPORT_SYMBOL(drm_crtc_init_with_planes);
1123 serge 790
 
791
/**
4075 Serge 792
 * drm_crtc_cleanup - Clean up the core crtc usage
1123 serge 793
 * @crtc: CRTC to cleanup
794
 *
4075 Serge 795
 * This function cleans up @crtc and removes it from the DRM mode setting
796
 * core. Note that the function does *not* free the crtc structure itself,
797
 * this is the responsibility of the caller.
1123 serge 798
 */
799
void drm_crtc_cleanup(struct drm_crtc *crtc)
800
{
801
	struct drm_device *dev = crtc->dev;
802
 
803
		kfree(crtc->gamma_store);
804
		crtc->gamma_store = NULL;
805
 
5060 serge 806
	drm_modeset_lock_fini(&crtc->mutex);
807
 
1123 serge 808
	drm_mode_object_put(dev, &crtc->base);
809
	list_del(&crtc->head);
810
	dev->mode_config.num_crtc--;
811
}
812
EXPORT_SYMBOL(drm_crtc_cleanup);
813
 
814
/**
4560 Serge 815
 * drm_crtc_index - find the index of a registered CRTC
816
 * @crtc: CRTC to find index for
817
 *
818
 * Given a registered CRTC, return the index of that CRTC within a DRM
819
 * device's list of CRTCs.
820
 */
821
unsigned int drm_crtc_index(struct drm_crtc *crtc)
822
{
823
	unsigned int index = 0;
824
	struct drm_crtc *tmp;
825
 
826
	list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
827
		if (tmp == crtc)
828
			return index;
829
 
830
		index++;
831
	}
832
 
833
	BUG();
834
}
835
EXPORT_SYMBOL(drm_crtc_index);
836
 
4104 Serge 837
/*
1123 serge 838
 * drm_mode_remove - remove and free a mode
839
 * @connector: connector list to modify
840
 * @mode: mode to remove
841
 *
842
 * Remove @mode from @connector's mode list, then free it.
843
 */
4104 Serge 844
static void drm_mode_remove(struct drm_connector *connector,
1123 serge 845
		     struct drm_display_mode *mode)
846
{
847
	list_del(&mode->head);
3031 serge 848
	drm_mode_destroy(connector->dev, mode);
1123 serge 849
}
850
 
851
/**
852
 * drm_connector_init - Init a preallocated connector
853
 * @dev: DRM device
854
 * @connector: the connector to init
855
 * @funcs: callbacks for this connector
3480 Serge 856
 * @connector_type: user visible type of the connector
1123 serge 857
 *
858
 * Initialises a preallocated connector. Connectors should be
859
 * subclassed as part of driver connector objects.
3031 serge 860
 *
5060 serge 861
 * Returns:
3031 serge 862
 * Zero on success, error code on failure.
1123 serge 863
 */
3031 serge 864
int drm_connector_init(struct drm_device *dev,
1123 serge 865
		     struct drm_connector *connector,
866
		     const struct drm_connector_funcs *funcs,
867
		     int connector_type)
868
{
3031 serge 869
	int ret;
4104 Serge 870
	struct ida *connector_ida =
871
		&drm_connector_enum_list[connector_type].ida;
3031 serge 872
 
3480 Serge 873
	drm_modeset_lock_all(dev);
1123 serge 874
 
5060 serge 875
	ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
3031 serge 876
	if (ret)
5060 serge 877
		goto out_unlock;
3031 serge 878
 
879
	connector->base.properties = &connector->properties;
1123 serge 880
	connector->dev = dev;
881
	connector->funcs = funcs;
882
	connector->connector_type = connector_type;
883
	connector->connector_type_id =
4104 Serge 884
		ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
885
	if (connector->connector_type_id < 0) {
886
		ret = connector->connector_type_id;
5060 serge 887
		goto out_put;
4104 Serge 888
	}
5060 serge 889
	connector->name =
890
		kasprintf(GFP_KERNEL, "%s-%d",
891
			  drm_connector_enum_list[connector_type].name,
892
			  connector->connector_type_id);
893
	if (!connector->name) {
894
		ret = -ENOMEM;
895
		goto out_put;
896
	}
897
 
1123 serge 898
	INIT_LIST_HEAD(&connector->probed_modes);
899
	INIT_LIST_HEAD(&connector->modes);
900
	connector->edid_blob_ptr = NULL;
3192 Serge 901
	connector->status = connector_status_unknown;
1123 serge 902
 
903
	list_add_tail(&connector->head, &dev->mode_config.connector_list);
904
	dev->mode_config.num_connector++;
905
 
3031 serge 906
	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
3192 Serge 907
		drm_object_attach_property(&connector->base,
3031 serge 908
					      dev->mode_config.edid_property,
909
					      0);
1123 serge 910
 
3192 Serge 911
	drm_object_attach_property(&connector->base,
1123 serge 912
				      dev->mode_config.dpms_property, 0);
913
 
5060 serge 914
	connector->debugfs_entry = NULL;
915
 
916
out_put:
917
	if (ret)
918
		drm_mode_object_put(dev, &connector->base);
919
 
920
out_unlock:
3480 Serge 921
	drm_modeset_unlock_all(dev);
3031 serge 922
 
923
	return ret;
1123 serge 924
}
925
EXPORT_SYMBOL(drm_connector_init);
926
 
927
/**
928
 * drm_connector_cleanup - cleans up an initialised connector
929
 * @connector: connector to cleanup
930
 *
931
 * Cleans up the connector but doesn't free the object.
932
 */
933
void drm_connector_cleanup(struct drm_connector *connector)
934
{
935
	struct drm_device *dev = connector->dev;
936
	struct drm_display_mode *mode, *t;
937
 
938
	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
939
		drm_mode_remove(connector, mode);
940
 
941
	list_for_each_entry_safe(mode, t, &connector->modes, head)
942
		drm_mode_remove(connector, mode);
943
 
4104 Serge 944
	ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
945
		   connector->connector_type_id);
946
 
1123 serge 947
	drm_mode_object_put(dev, &connector->base);
5060 serge 948
	kfree(connector->name);
949
	connector->name = NULL;
1123 serge 950
	list_del(&connector->head);
2160 serge 951
	dev->mode_config.num_connector--;
1123 serge 952
}
953
EXPORT_SYMBOL(drm_connector_cleanup);
954
 
5060 serge 955
/**
956
 * drm_connector_register - register a connector
957
 * @connector: the connector to register
958
 *
959
 * Register userspace interfaces for a connector
960
 *
961
 * Returns:
962
 * Zero on success, error code on failure.
963
 */
964
int drm_connector_register(struct drm_connector *connector)
965
{
966
	int ret;
967
 
968
	drm_mode_object_register(connector->dev, &connector->base);
969
 
970
	return 0;
971
}
972
EXPORT_SYMBOL(drm_connector_register);
973
 
974
/**
975
 * drm_connector_unregister - unregister a connector
976
 * @connector: the connector to unregister
977
 *
978
 * Unregister userspace interfaces for a connector
979
 */
980
void drm_connector_unregister(struct drm_connector *connector)
981
{
982
}
983
EXPORT_SYMBOL(drm_connector_unregister);
984
 
985
 
986
/**
987
 * drm_connector_unplug_all - unregister connector userspace interfaces
988
 * @dev: drm device
989
 *
990
 * This function unregisters all connector userspace interfaces in sysfs. Should
991
 * be call when the device is disconnected, e.g. from an usb driver's
992
 * ->disconnect callback.
993
 */
3031 serge 994
void drm_connector_unplug_all(struct drm_device *dev)
995
{
996
	struct drm_connector *connector;
997
 
998
	/* taking the mode config mutex ends up in a clash with sysfs */
5060 serge 999
	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1000
		drm_connector_unregister(connector);
3031 serge 1001
 
1002
}
1003
EXPORT_SYMBOL(drm_connector_unplug_all);
1004
 
5060 serge 1005
/**
1006
 * drm_bridge_init - initialize a drm transcoder/bridge
1007
 * @dev: drm device
1008
 * @bridge: transcoder/bridge to set up
1009
 * @funcs: bridge function table
1010
 *
1011
 * Initialises a preallocated bridge. Bridges should be
1012
 * subclassed as part of driver connector objects.
1013
 *
1014
 * Returns:
1015
 * Zero on success, error code on failure.
1016
 */
4104 Serge 1017
int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1018
		const struct drm_bridge_funcs *funcs)
1019
{
1020
	int ret;
1021
 
1022
	drm_modeset_lock_all(dev);
1023
 
1024
	ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1025
	if (ret)
1026
		goto out;
1027
 
1028
	bridge->dev = dev;
1029
	bridge->funcs = funcs;
1030
 
1031
	list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1032
	dev->mode_config.num_bridge++;
1033
 
1034
 out:
1035
	drm_modeset_unlock_all(dev);
1036
	return ret;
1037
}
1038
EXPORT_SYMBOL(drm_bridge_init);
1039
 
5060 serge 1040
/**
1041
 * drm_bridge_cleanup - cleans up an initialised bridge
1042
 * @bridge: bridge to cleanup
1043
 *
1044
 * Cleans up the bridge but doesn't free the object.
1045
 */
4104 Serge 1046
void drm_bridge_cleanup(struct drm_bridge *bridge)
1047
{
1048
	struct drm_device *dev = bridge->dev;
1049
 
1050
	drm_modeset_lock_all(dev);
1051
	drm_mode_object_put(dev, &bridge->base);
1052
	list_del(&bridge->head);
1053
	dev->mode_config.num_bridge--;
1054
	drm_modeset_unlock_all(dev);
1055
}
1056
EXPORT_SYMBOL(drm_bridge_cleanup);
1057
 
5060 serge 1058
/**
1059
 * drm_encoder_init - Init a preallocated encoder
1060
 * @dev: drm device
1061
 * @encoder: the encoder to init
1062
 * @funcs: callbacks for this encoder
1063
 * @encoder_type: user visible type of the encoder
1064
 *
1065
 * Initialises a preallocated encoder. Encoder should be
1066
 * subclassed as part of driver encoder objects.
1067
 *
1068
 * Returns:
1069
 * Zero on success, error code on failure.
1070
 */
3031 serge 1071
int drm_encoder_init(struct drm_device *dev,
1123 serge 1072
		      struct drm_encoder *encoder,
1073
		      const struct drm_encoder_funcs *funcs,
1074
		      int encoder_type)
1075
{
3031 serge 1076
	int ret;
1077
 
3480 Serge 1078
	drm_modeset_lock_all(dev);
1123 serge 1079
 
3031 serge 1080
	ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1081
	if (ret)
5060 serge 1082
		goto out_unlock;
3031 serge 1083
 
1123 serge 1084
	encoder->dev = dev;
1085
	encoder->encoder_type = encoder_type;
1086
	encoder->funcs = funcs;
5060 serge 1087
	encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1088
				  drm_encoder_enum_list[encoder_type].name,
1089
				  encoder->base.id);
1090
	if (!encoder->name) {
1091
		ret = -ENOMEM;
1092
		goto out_put;
1093
	}
1123 serge 1094
 
1095
	list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1096
	dev->mode_config.num_encoder++;
1097
 
5060 serge 1098
out_put:
1099
	if (ret)
1100
		drm_mode_object_put(dev, &encoder->base);
1101
 
1102
out_unlock:
3480 Serge 1103
	drm_modeset_unlock_all(dev);
3031 serge 1104
 
1105
	return ret;
1123 serge 1106
}
1107
EXPORT_SYMBOL(drm_encoder_init);
1108
 
5060 serge 1109
/**
1110
 * drm_encoder_cleanup - cleans up an initialised encoder
1111
 * @encoder: encoder to cleanup
1112
 *
1113
 * Cleans up the encoder but doesn't free the object.
1114
 */
1123 serge 1115
void drm_encoder_cleanup(struct drm_encoder *encoder)
1116
{
1117
	struct drm_device *dev = encoder->dev;
3480 Serge 1118
	drm_modeset_lock_all(dev);
1123 serge 1119
	drm_mode_object_put(dev, &encoder->base);
5060 serge 1120
	kfree(encoder->name);
1121
	encoder->name = NULL;
1123 serge 1122
	list_del(&encoder->head);
2160 serge 1123
	dev->mode_config.num_encoder--;
3480 Serge 1124
	drm_modeset_unlock_all(dev);
1123 serge 1125
}
1126
EXPORT_SYMBOL(drm_encoder_cleanup);
1127
 
4075 Serge 1128
/**
5060 serge 1129
 * drm_universal_plane_init - Initialize a new universal plane object
4075 Serge 1130
 * @dev: DRM device
1131
 * @plane: plane object to init
1132
 * @possible_crtcs: bitmask of possible CRTCs
1133
 * @funcs: callbacks for the new plane
1134
 * @formats: array of supported formats (%DRM_FORMAT_*)
1135
 * @format_count: number of elements in @formats
5060 serge 1136
 * @type: type of plane (overlay, primary, cursor)
4075 Serge 1137
 *
5060 serge 1138
 * Initializes a plane object of type @type.
4075 Serge 1139
 *
5060 serge 1140
 * Returns:
4075 Serge 1141
 * Zero on success, error code on failure.
1142
 */
5060 serge 1143
int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
3031 serge 1144
		   unsigned long possible_crtcs,
1145
		   const struct drm_plane_funcs *funcs,
1146
		   const uint32_t *formats, uint32_t format_count,
5060 serge 1147
			     enum drm_plane_type type)
3031 serge 1148
{
1149
	int ret;
1150
 
3480 Serge 1151
	drm_modeset_lock_all(dev);
3031 serge 1152
 
1153
	ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1154
	if (ret)
1155
		goto out;
1156
 
1157
	plane->base.properties = &plane->properties;
1158
	plane->dev = dev;
1159
	plane->funcs = funcs;
1160
	plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1161
				      GFP_KERNEL);
1162
	if (!plane->format_types) {
1163
		DRM_DEBUG_KMS("out of memory when allocating plane\n");
1164
		drm_mode_object_put(dev, &plane->base);
1165
		ret = -ENOMEM;
1166
		goto out;
1167
	}
1168
 
1169
	memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1170
	plane->format_count = format_count;
1171
	plane->possible_crtcs = possible_crtcs;
5060 serge 1172
	plane->type = type;
3031 serge 1173
 
1174
		list_add_tail(&plane->head, &dev->mode_config.plane_list);
5060 serge 1175
	dev->mode_config.num_total_plane++;
1176
	if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1177
		dev->mode_config.num_overlay_plane++;
3031 serge 1178
 
5060 serge 1179
	drm_object_attach_property(&plane->base,
1180
				   dev->mode_config.plane_type_property,
1181
				   plane->type);
1182
 
3031 serge 1183
 out:
3480 Serge 1184
	drm_modeset_unlock_all(dev);
3031 serge 1185
 
1186
	return ret;
1187
}
5060 serge 1188
EXPORT_SYMBOL(drm_universal_plane_init);
1189
 
1190
/**
1191
 * drm_plane_init - Initialize a legacy plane
1192
 * @dev: DRM device
1193
 * @plane: plane object to init
1194
 * @possible_crtcs: bitmask of possible CRTCs
1195
 * @funcs: callbacks for the new plane
1196
 * @formats: array of supported formats (%DRM_FORMAT_*)
1197
 * @format_count: number of elements in @formats
1198
 * @is_primary: plane type (primary vs overlay)
1199
 *
1200
 * Legacy API to initialize a DRM plane.
1201
 *
1202
 * New drivers should call drm_universal_plane_init() instead.
1203
 *
1204
 * Returns:
1205
 * Zero on success, error code on failure.
1206
 */
1207
int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1208
		   unsigned long possible_crtcs,
1209
		   const struct drm_plane_funcs *funcs,
1210
		   const uint32_t *formats, uint32_t format_count,
1211
		   bool is_primary)
1212
{
1213
	enum drm_plane_type type;
1214
 
1215
	type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1216
	return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1217
					formats, format_count, type);
1218
}
3031 serge 1219
EXPORT_SYMBOL(drm_plane_init);
1220
 
4075 Serge 1221
/**
1222
 * drm_plane_cleanup - Clean up the core plane usage
1223
 * @plane: plane to cleanup
1224
 *
1225
 * This function cleans up @plane and removes it from the DRM mode setting
1226
 * core. Note that the function does *not* free the plane structure itself,
1227
 * this is the responsibility of the caller.
1228
 */
3031 serge 1229
void drm_plane_cleanup(struct drm_plane *plane)
1230
{
1231
	struct drm_device *dev = plane->dev;
1232
 
3480 Serge 1233
	drm_modeset_lock_all(dev);
3031 serge 1234
	kfree(plane->format_types);
1235
	drm_mode_object_put(dev, &plane->base);
5060 serge 1236
 
1237
	BUG_ON(list_empty(&plane->head));
1238
 
3031 serge 1239
		list_del(&plane->head);
5060 serge 1240
	dev->mode_config.num_total_plane--;
1241
	if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1242
		dev->mode_config.num_overlay_plane--;
3480 Serge 1243
	drm_modeset_unlock_all(dev);
3031 serge 1244
}
1245
EXPORT_SYMBOL(drm_plane_cleanup);
1246
 
1123 serge 1247
/**
4075 Serge 1248
 * drm_plane_force_disable - Forcibly disable a plane
1249
 * @plane: plane to disable
1250
 *
1251
 * Forces the plane to be disabled.
1252
 *
1253
 * Used when the plane's current framebuffer is destroyed,
1254
 * and when restoring fbdev mode.
1255
 */
1256
void drm_plane_force_disable(struct drm_plane *plane)
1257
{
5060 serge 1258
	struct drm_framebuffer *old_fb = plane->fb;
4075 Serge 1259
	int ret;
1260
 
5060 serge 1261
	if (!old_fb)
4075 Serge 1262
		return;
1263
 
1264
	ret = plane->funcs->disable_plane(plane);
5060 serge 1265
	if (ret) {
4075 Serge 1266
		DRM_ERROR("failed to disable plane with busy fb\n");
5060 serge 1267
		return;
1268
	}
4075 Serge 1269
	/* disconnect the plane from the fb and crtc: */
5060 serge 1270
	__drm_framebuffer_unreference(old_fb);
4075 Serge 1271
	plane->fb = NULL;
1272
	plane->crtc = NULL;
1273
}
1274
EXPORT_SYMBOL(drm_plane_force_disable);
1275
 
1123 serge 1276
static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1277
{
1278
	struct drm_property *edid;
1279
	struct drm_property *dpms;
5060 serge 1280
	struct drm_property *dev_path;
1123 serge 1281
 
1282
	/*
1283
	 * Standard properties (apply to all connectors)
1284
	 */
1285
	edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1286
				   DRM_MODE_PROP_IMMUTABLE,
1287
				   "EDID", 0);
1288
	dev->mode_config.edid_property = edid;
1289
 
3031 serge 1290
	dpms = drm_property_create_enum(dev, 0,
1291
				   "DPMS", drm_dpms_enum_list,
1292
				   ARRAY_SIZE(drm_dpms_enum_list));
1123 serge 1293
	dev->mode_config.dpms_property = dpms;
1294
 
5060 serge 1295
	dev_path = drm_property_create(dev,
1296
				       DRM_MODE_PROP_BLOB |
1297
				       DRM_MODE_PROP_IMMUTABLE,
1298
				       "PATH", 0);
1299
	dev->mode_config.path_property = dev_path;
1300
 
1123 serge 1301
	return 0;
1302
}
1303
 
5060 serge 1304
static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1305
{
1306
	struct drm_property *type;
1307
 
1308
	/*
1309
	 * Standard properties (apply to all planes)
1310
	 */
1311
	type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1312
					"type", drm_plane_type_enum_list,
1313
					ARRAY_SIZE(drm_plane_type_enum_list));
1314
	dev->mode_config.plane_type_property = type;
1315
 
1316
	return 0;
1317
}
1318
 
1123 serge 1319
/**
1320
 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1321
 * @dev: DRM device
1322
 *
1323
 * Called by a driver the first time a DVI-I connector is made.
1324
 */
1325
int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1326
{
1327
	struct drm_property *dvi_i_selector;
1328
	struct drm_property *dvi_i_subconnector;
1329
 
1330
	if (dev->mode_config.dvi_i_select_subconnector_property)
1331
		return 0;
1332
 
1333
	dvi_i_selector =
3031 serge 1334
		drm_property_create_enum(dev, 0,
1123 serge 1335
				    "select subconnector",
3031 serge 1336
				    drm_dvi_i_select_enum_list,
1123 serge 1337
				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
1338
	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1339
 
3031 serge 1340
	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1123 serge 1341
				    "subconnector",
3031 serge 1342
				    drm_dvi_i_subconnector_enum_list,
1123 serge 1343
				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1344
	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1345
 
1346
	return 0;
1347
}
1348
EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1349
 
1350
/**
1351
 * drm_create_tv_properties - create TV specific connector properties
1352
 * @dev: DRM device
1353
 * @num_modes: number of different TV formats (modes) supported
1354
 * @modes: array of pointers to strings containing name of each format
1355
 *
1356
 * Called by a driver's TV initialization routine, this function creates
1357
 * the TV specific connector properties for a given device.  Caller is
1358
 * responsible for allocating a list of format names and passing them to
1359
 * this routine.
1360
 */
1361
int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1362
				  char *modes[])
1363
{
1364
	struct drm_property *tv_selector;
1365
	struct drm_property *tv_subconnector;
1366
	int i;
1367
 
1368
	if (dev->mode_config.tv_select_subconnector_property)
1369
		return 0;
1370
 
1371
	/*
1372
	 * Basic connector properties
1373
	 */
3031 serge 1374
	tv_selector = drm_property_create_enum(dev, 0,
1123 serge 1375
					  "select subconnector",
3031 serge 1376
					  drm_tv_select_enum_list,
1123 serge 1377
					  ARRAY_SIZE(drm_tv_select_enum_list));
1378
	dev->mode_config.tv_select_subconnector_property = tv_selector;
1379
 
1380
	tv_subconnector =
3031 serge 1381
		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1382
				    "subconnector",
1383
				    drm_tv_subconnector_enum_list,
1123 serge 1384
				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
1385
	dev->mode_config.tv_subconnector_property = tv_subconnector;
1386
 
1387
	/*
1388
	 * Other, TV specific properties: margins & TV modes.
1389
	 */
1390
	dev->mode_config.tv_left_margin_property =
3031 serge 1391
		drm_property_create_range(dev, 0, "left margin", 0, 100);
1123 serge 1392
 
1393
	dev->mode_config.tv_right_margin_property =
3031 serge 1394
		drm_property_create_range(dev, 0, "right margin", 0, 100);
1123 serge 1395
 
1396
	dev->mode_config.tv_top_margin_property =
3031 serge 1397
		drm_property_create_range(dev, 0, "top margin", 0, 100);
1123 serge 1398
 
1399
	dev->mode_config.tv_bottom_margin_property =
3031 serge 1400
		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1123 serge 1401
 
1402
	dev->mode_config.tv_mode_property =
1403
		drm_property_create(dev, DRM_MODE_PROP_ENUM,
1404
				    "mode", num_modes);
1405
	for (i = 0; i < num_modes; i++)
1406
		drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1407
				      i, modes[i]);
1408
 
1179 serge 1409
	dev->mode_config.tv_brightness_property =
3031 serge 1410
		drm_property_create_range(dev, 0, "brightness", 0, 100);
1179 serge 1411
 
1412
	dev->mode_config.tv_contrast_property =
3031 serge 1413
		drm_property_create_range(dev, 0, "contrast", 0, 100);
1179 serge 1414
 
1415
	dev->mode_config.tv_flicker_reduction_property =
3031 serge 1416
		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1179 serge 1417
 
1418
	dev->mode_config.tv_overscan_property =
3031 serge 1419
		drm_property_create_range(dev, 0, "overscan", 0, 100);
1179 serge 1420
 
1421
	dev->mode_config.tv_saturation_property =
3031 serge 1422
		drm_property_create_range(dev, 0, "saturation", 0, 100);
1179 serge 1423
 
1424
	dev->mode_config.tv_hue_property =
3031 serge 1425
		drm_property_create_range(dev, 0, "hue", 0, 100);
1179 serge 1426
 
1123 serge 1427
	return 0;
1428
}
1429
EXPORT_SYMBOL(drm_mode_create_tv_properties);
1430
 
1431
/**
1432
 * drm_mode_create_scaling_mode_property - create scaling mode property
1433
 * @dev: DRM device
1434
 *
1435
 * Called by a driver the first time it's needed, must be attached to desired
1436
 * connectors.
1437
 */
1438
int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1439
{
1440
	struct drm_property *scaling_mode;
1441
 
1442
	if (dev->mode_config.scaling_mode_property)
1443
		return 0;
1444
 
1445
	scaling_mode =
3031 serge 1446
		drm_property_create_enum(dev, 0, "scaling mode",
1447
				drm_scaling_mode_enum_list,
1123 serge 1448
				    ARRAY_SIZE(drm_scaling_mode_enum_list));
1449
 
1450
	dev->mode_config.scaling_mode_property = scaling_mode;
1451
 
1452
	return 0;
1453
}
1454
EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1455
 
1456
/**
5060 serge 1457
 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1458
 * @dev: DRM device
1459
 *
1460
 * Called by a driver the first time it's needed, must be attached to desired
1461
 * connectors.
1462
 *
1463
 * Returns:
1464
 * Zero on success, errno on failure.
1465
 */
1466
int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1467
{
1468
	if (dev->mode_config.aspect_ratio_property)
1469
		return 0;
1470
 
1471
	dev->mode_config.aspect_ratio_property =
1472
		drm_property_create_enum(dev, 0, "aspect ratio",
1473
				drm_aspect_ratio_enum_list,
1474
				ARRAY_SIZE(drm_aspect_ratio_enum_list));
1475
 
1476
	if (dev->mode_config.aspect_ratio_property == NULL)
1477
		return -ENOMEM;
1478
 
1479
	return 0;
1480
}
1481
EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1482
 
1483
/**
1321 serge 1484
 * drm_mode_create_dirty_property - create dirty property
1485
 * @dev: DRM device
1486
 *
1487
 * Called by a driver the first time it's needed, must be attached to desired
1488
 * connectors.
1489
 */
1490
int drm_mode_create_dirty_info_property(struct drm_device *dev)
1491
{
1492
	struct drm_property *dirty_info;
1493
 
1494
	if (dev->mode_config.dirty_info_property)
1495
		return 0;
1496
 
1497
	dirty_info =
3031 serge 1498
		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1321 serge 1499
				    "dirty",
3031 serge 1500
				    drm_dirty_info_enum_list,
1321 serge 1501
				    ARRAY_SIZE(drm_dirty_info_enum_list));
1502
	dev->mode_config.dirty_info_property = dirty_info;
1503
 
1504
	return 0;
1505
}
1506
EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1507
 
3746 Serge 1508
static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1123 serge 1509
{
1510
	uint32_t total_objects = 0;
1511
 
1512
	total_objects += dev->mode_config.num_crtc;
1513
	total_objects += dev->mode_config.num_connector;
1514
	total_objects += dev->mode_config.num_encoder;
4104 Serge 1515
	total_objects += dev->mode_config.num_bridge;
1123 serge 1516
 
1517
	group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1518
	if (!group->id_list)
1519
		return -ENOMEM;
1520
 
1521
	group->num_crtcs = 0;
1522
	group->num_connectors = 0;
1523
	group->num_encoders = 0;
4104 Serge 1524
	group->num_bridges = 0;
1123 serge 1525
	return 0;
1526
}
1527
 
5060 serge 1528
void drm_mode_group_destroy(struct drm_mode_group *group)
1529
{
1530
	kfree(group->id_list);
1531
	group->id_list = NULL;
1532
}
1533
 
1534
/*
1535
 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1536
 * the drm core's responsibility to set up mode control groups.
1537
 */
1123 serge 1538
int drm_mode_group_init_legacy_group(struct drm_device *dev,
1539
				     struct drm_mode_group *group)
1540
{
1541
	struct drm_crtc *crtc;
1542
	struct drm_encoder *encoder;
1543
	struct drm_connector *connector;
4104 Serge 1544
	struct drm_bridge *bridge;
1123 serge 1545
	int ret;
1546
 
1547
	if ((ret = drm_mode_group_init(dev, group)))
1548
		return ret;
1549
 
1550
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1551
		group->id_list[group->num_crtcs++] = crtc->base.id;
1552
 
1553
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1554
		group->id_list[group->num_crtcs + group->num_encoders++] =
1555
		encoder->base.id;
1556
 
1557
	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1558
		group->id_list[group->num_crtcs + group->num_encoders +
1559
			       group->num_connectors++] = connector->base.id;
1560
 
4104 Serge 1561
	list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1562
		group->id_list[group->num_crtcs + group->num_encoders +
1563
			       group->num_connectors + group->num_bridges++] =
1564
					bridge->base.id;
1565
 
1123 serge 1566
	return 0;
1567
}
3031 serge 1568
EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1123 serge 1569
 
5060 serge 1570
void drm_reinit_primary_mode_group(struct drm_device *dev)
1571
{
1572
	drm_modeset_lock_all(dev);
1573
	drm_mode_group_destroy(&dev->primary->mode_group);
1574
	drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1575
	drm_modeset_unlock_all(dev);
1576
}
1577
EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1578
 
1123 serge 1579
/**
1580
 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1581
 * @out: drm_mode_modeinfo struct to return to the user
1582
 * @in: drm_display_mode to use
1583
 *
1584
 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1585
 * the user.
1586
 */
3031 serge 1587
static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1588
				      const struct drm_display_mode *in)
1123 serge 1589
{
3031 serge 1590
	WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1591
	     in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1592
	     in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1593
	     in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1594
	     in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1595
	     "timing values too large for mode info\n");
1596
 
1123 serge 1597
	out->clock = in->clock;
1598
	out->hdisplay = in->hdisplay;
1599
	out->hsync_start = in->hsync_start;
1600
	out->hsync_end = in->hsync_end;
1601
	out->htotal = in->htotal;
1602
	out->hskew = in->hskew;
1603
	out->vdisplay = in->vdisplay;
1604
	out->vsync_start = in->vsync_start;
1605
	out->vsync_end = in->vsync_end;
1606
	out->vtotal = in->vtotal;
1607
	out->vscan = in->vscan;
1608
	out->vrefresh = in->vrefresh;
1609
	out->flags = in->flags;
1610
	out->type = in->type;
1611
	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1612
	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1613
}
1614
 
1615
/**
4560 Serge 1616
 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1123 serge 1617
 * @out: drm_display_mode to return to the user
1618
 * @in: drm_mode_modeinfo to use
1619
 *
1620
 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1621
 * the caller.
3031 serge 1622
 *
5060 serge 1623
 * Returns:
3031 serge 1624
 * Zero on success, errno on failure.
1123 serge 1625
 */
3031 serge 1626
static int drm_crtc_convert_umode(struct drm_display_mode *out,
1627
				  const struct drm_mode_modeinfo *in)
1123 serge 1628
{
3031 serge 1629
	if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1630
		return -ERANGE;
1631
 
4560 Serge 1632
	if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1633
		return -EINVAL;
1634
 
1123 serge 1635
	out->clock = in->clock;
1636
	out->hdisplay = in->hdisplay;
1637
	out->hsync_start = in->hsync_start;
1638
	out->hsync_end = in->hsync_end;
1639
	out->htotal = in->htotal;
1640
	out->hskew = in->hskew;
1641
	out->vdisplay = in->vdisplay;
1642
	out->vsync_start = in->vsync_start;
1643
	out->vsync_end = in->vsync_end;
1644
	out->vtotal = in->vtotal;
1645
	out->vscan = in->vscan;
1646
	out->vrefresh = in->vrefresh;
1647
	out->flags = in->flags;
1648
	out->type = in->type;
1649
	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1650
	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
3031 serge 1651
 
1652
	return 0;
1123 serge 1653
}
1654
 
1655
 
1656
#if 0
1657
/**
1658
 * drm_mode_getresources - get graphics configuration
3480 Serge 1659
 * @dev: drm device for the ioctl
1660
 * @data: data pointer for the ioctl
1661
 * @file_priv: drm file for the ioctl call
1123 serge 1662
 *
1663
 * Construct a set of configuration description structures and return
1664
 * them to the user, including CRTC, connector and framebuffer configuration.
1665
 *
1666
 * Called by the user via ioctl.
1667
 *
5060 serge 1668
 * Returns:
1123 serge 1669
 * Zero on success, errno on failure.
1670
 */
1671
int drm_mode_getresources(struct drm_device *dev, void *data,
1672
			  struct drm_file *file_priv)
1673
{
1674
	struct drm_mode_card_res *card_res = data;
1675
	struct list_head *lh;
1676
	struct drm_framebuffer *fb;
1677
	struct drm_connector *connector;
1678
	struct drm_crtc *crtc;
1679
	struct drm_encoder *encoder;
1680
	int ret = 0;
1681
	int connector_count = 0;
1682
	int crtc_count = 0;
1683
	int fb_count = 0;
1684
	int encoder_count = 0;
1685
	int copied = 0, i;
1686
	uint32_t __user *fb_id;
1687
	uint32_t __user *crtc_id;
1688
	uint32_t __user *connector_id;
1689
	uint32_t __user *encoder_id;
1690
	struct drm_mode_group *mode_group;
1691
 
1963 serge 1692
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1693
		return -EINVAL;
1694
 
1123 serge 1695
 
3480 Serge 1696
	mutex_lock(&file_priv->fbs_lock);
1123 serge 1697
	/*
1698
	 * For the non-control nodes we need to limit the list of resources
1699
	 * by IDs in the group list for this node
1700
	 */
1701
	list_for_each(lh, &file_priv->fbs)
1702
		fb_count++;
1703
 
3480 Serge 1704
	/* handle this in 4 parts */
1705
	/* FBs */
1706
	if (card_res->count_fbs >= fb_count) {
1707
		copied = 0;
1708
		fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1709
		list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1710
			if (put_user(fb->base.id, fb_id + copied)) {
1711
				mutex_unlock(&file_priv->fbs_lock);
1712
				return -EFAULT;
1713
			}
1714
			copied++;
1715
		}
1716
	}
1717
	card_res->count_fbs = fb_count;
1718
	mutex_unlock(&file_priv->fbs_lock);
1719
 
1720
	drm_modeset_lock_all(dev);
5060 serge 1721
	if (!drm_is_primary_client(file_priv)) {
1123 serge 1722
 
5060 serge 1723
		mode_group = NULL;
1123 serge 1724
		list_for_each(lh, &dev->mode_config.crtc_list)
1725
			crtc_count++;
1726
 
1727
		list_for_each(lh, &dev->mode_config.connector_list)
1728
			connector_count++;
1729
 
1730
		list_for_each(lh, &dev->mode_config.encoder_list)
1731
			encoder_count++;
1732
	} else {
1733
 
5060 serge 1734
		mode_group = &file_priv->master->minor->mode_group;
1123 serge 1735
		crtc_count = mode_group->num_crtcs;
1736
		connector_count = mode_group->num_connectors;
1737
		encoder_count = mode_group->num_encoders;
1738
	}
1739
 
1740
	card_res->max_height = dev->mode_config.max_height;
1741
	card_res->min_height = dev->mode_config.min_height;
1742
	card_res->max_width = dev->mode_config.max_width;
1743
	card_res->min_width = dev->mode_config.min_width;
1744
 
1745
	/* CRTCs */
1746
	if (card_res->count_crtcs >= crtc_count) {
1747
		copied = 0;
1748
		crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
5060 serge 1749
		if (!mode_group) {
1123 serge 1750
			list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1751
					    head) {
1963 serge 1752
				DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1123 serge 1753
				if (put_user(crtc->base.id, crtc_id + copied)) {
1754
					ret = -EFAULT;
1755
					goto out;
1756
				}
1757
				copied++;
1758
			}
1759
		} else {
1760
			for (i = 0; i < mode_group->num_crtcs; i++) {
1761
				if (put_user(mode_group->id_list[i],
1762
					     crtc_id + copied)) {
1763
					ret = -EFAULT;
1764
					goto out;
1765
				}
1766
				copied++;
1767
			}
1768
		}
1769
	}
1770
	card_res->count_crtcs = crtc_count;
1771
 
1772
	/* Encoders */
1773
	if (card_res->count_encoders >= encoder_count) {
1774
		copied = 0;
1775
		encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
5060 serge 1776
		if (!mode_group) {
1123 serge 1777
			list_for_each_entry(encoder,
1778
					    &dev->mode_config.encoder_list,
1779
					    head) {
1963 serge 1780
				DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
5060 serge 1781
						encoder->name);
1123 serge 1782
				if (put_user(encoder->base.id, encoder_id +
1783
					     copied)) {
1784
					ret = -EFAULT;
1785
					goto out;
1786
				}
1787
				copied++;
1788
			}
1789
		} else {
1790
			for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1791
				if (put_user(mode_group->id_list[i],
1792
					     encoder_id + copied)) {
1793
					ret = -EFAULT;
1794
					goto out;
1795
				}
1796
				copied++;
1797
			}
1798
 
1799
		}
1800
	}
1801
	card_res->count_encoders = encoder_count;
1802
 
1803
	/* Connectors */
1804
	if (card_res->count_connectors >= connector_count) {
1805
		copied = 0;
1806
		connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
5060 serge 1807
		if (!mode_group) {
1123 serge 1808
			list_for_each_entry(connector,
1809
					    &dev->mode_config.connector_list,
1810
					    head) {
1963 serge 1811
				DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1812
					connector->base.id,
5060 serge 1813
					connector->name);
1123 serge 1814
				if (put_user(connector->base.id,
1815
					     connector_id + copied)) {
1816
					ret = -EFAULT;
1817
					goto out;
1818
				}
1819
				copied++;
1820
			}
1821
		} else {
1822
			int start = mode_group->num_crtcs +
1823
				mode_group->num_encoders;
1824
			for (i = start; i < start + mode_group->num_connectors; i++) {
1825
				if (put_user(mode_group->id_list[i],
1826
					     connector_id + copied)) {
1827
					ret = -EFAULT;
1828
					goto out;
1829
				}
1830
				copied++;
1831
			}
1832
		}
1833
	}
1834
	card_res->count_connectors = connector_count;
1835
 
1963 serge 1836
	DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1123 serge 1837
		  card_res->count_connectors, card_res->count_encoders);
1838
 
1839
out:
3480 Serge 1840
	drm_modeset_unlock_all(dev);
1123 serge 1841
	return ret;
1842
}
1843
 
1844
/**
1845
 * drm_mode_getcrtc - get CRTC configuration
3480 Serge 1846
 * @dev: drm device for the ioctl
1847
 * @data: data pointer for the ioctl
1848
 * @file_priv: drm file for the ioctl call
1123 serge 1849
 *
1850
 * Construct a CRTC configuration structure to return to the user.
1851
 *
1852
 * Called by the user via ioctl.
1853
 *
5060 serge 1854
 * Returns:
1123 serge 1855
 * Zero on success, errno on failure.
1856
 */
1857
int drm_mode_getcrtc(struct drm_device *dev,
1858
		     void *data, struct drm_file *file_priv)
1859
{
1860
	struct drm_mode_crtc *crtc_resp = data;
1861
	struct drm_crtc *crtc;
1862
	int ret = 0;
1863
 
1963 serge 1864
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1865
		return -EINVAL;
1866
 
3480 Serge 1867
	drm_modeset_lock_all(dev);
1123 serge 1868
 
5060 serge 1869
	crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1870
	if (!crtc) {
4560 Serge 1871
		ret = -ENOENT;
1123 serge 1872
		goto out;
1873
	}
1874
 
1875
	crtc_resp->x = crtc->x;
1876
	crtc_resp->y = crtc->y;
1877
	crtc_resp->gamma_size = crtc->gamma_size;
5060 serge 1878
	if (crtc->primary->fb)
1879
		crtc_resp->fb_id = crtc->primary->fb->base.id;
1123 serge 1880
	else
1881
		crtc_resp->fb_id = 0;
1882
 
1883
	if (crtc->enabled) {
1884
 
1885
		drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1886
		crtc_resp->mode_valid = 1;
1887
 
1888
	} else {
1889
		crtc_resp->mode_valid = 0;
1890
	}
1891
 
1892
out:
3480 Serge 1893
	drm_modeset_unlock_all(dev);
1123 serge 1894
	return ret;
1895
}
1896
 
4560 Serge 1897
static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1898
					 const struct drm_file *file_priv)
1899
{
1900
	/*
1901
	 * If user-space hasn't configured the driver to expose the stereo 3D
1902
	 * modes, don't expose them.
1903
	 */
1904
	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1905
		return false;
1906
 
1907
	return true;
1908
}
1909
 
1123 serge 1910
/**
1911
 * drm_mode_getconnector - get connector configuration
3480 Serge 1912
 * @dev: drm device for the ioctl
1913
 * @data: data pointer for the ioctl
1914
 * @file_priv: drm file for the ioctl call
1123 serge 1915
 *
1916
 * Construct a connector configuration structure to return to the user.
1917
 *
1918
 * Called by the user via ioctl.
1919
 *
5060 serge 1920
 * Returns:
1123 serge 1921
 * Zero on success, errno on failure.
1922
 */
1923
int drm_mode_getconnector(struct drm_device *dev, void *data,
1924
			  struct drm_file *file_priv)
1925
{
1926
	struct drm_mode_get_connector *out_resp = data;
1927
	struct drm_connector *connector;
1928
	struct drm_display_mode *mode;
1929
	int mode_count = 0;
1930
	int props_count = 0;
1931
	int encoders_count = 0;
1932
	int ret = 0;
1933
	int copied = 0;
1934
	int i;
1935
	struct drm_mode_modeinfo u_mode;
1936
	struct drm_mode_modeinfo __user *mode_ptr;
1937
	uint32_t __user *prop_ptr;
1938
	uint64_t __user *prop_values;
1939
	uint32_t __user *encoder_ptr;
1940
 
1963 serge 1941
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1942
		return -EINVAL;
1943
 
1123 serge 1944
	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1945
 
1963 serge 1946
	DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1123 serge 1947
 
1948
	mutex_lock(&dev->mode_config.mutex);
1949
 
5060 serge 1950
	connector = drm_connector_find(dev, out_resp->connector_id);
1951
	if (!connector) {
4560 Serge 1952
		ret = -ENOENT;
1123 serge 1953
		goto out;
1954
	}
1955
 
3031 serge 1956
	props_count = connector->properties.count;
1123 serge 1957
 
1958
	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1959
		if (connector->encoder_ids[i] != 0) {
1960
			encoders_count++;
1961
		}
1962
	}
1963
 
1964
	if (out_resp->count_modes == 0) {
1965
		connector->funcs->fill_modes(connector,
1966
					     dev->mode_config.max_width,
1967
					     dev->mode_config.max_height);
1968
	}
1969
 
1970
	/* delayed so we get modes regardless of pre-fill_modes state */
1971
	list_for_each_entry(mode, &connector->modes, head)
4560 Serge 1972
		if (drm_mode_expose_to_userspace(mode, file_priv))
1123 serge 1973
		mode_count++;
1974
 
1975
	out_resp->connector_id = connector->base.id;
1976
	out_resp->connector_type = connector->connector_type;
1977
	out_resp->connector_type_id = connector->connector_type_id;
1978
	out_resp->mm_width = connector->display_info.width_mm;
1979
	out_resp->mm_height = connector->display_info.height_mm;
1980
	out_resp->subpixel = connector->display_info.subpixel_order;
1981
	out_resp->connection = connector->status;
5060 serge 1982
	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1123 serge 1983
	if (connector->encoder)
1984
		out_resp->encoder_id = connector->encoder->base.id;
1985
	else
1986
		out_resp->encoder_id = 0;
5060 serge 1987
	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1123 serge 1988
 
1989
	/*
1990
	 * This ioctl is called twice, once to determine how much space is
1991
	 * needed, and the 2nd time to fill it.
1992
	 */
1993
	if ((out_resp->count_modes >= mode_count) && mode_count) {
1994
		copied = 0;
3031 serge 1995
		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1123 serge 1996
		list_for_each_entry(mode, &connector->modes, head) {
4560 Serge 1997
			if (!drm_mode_expose_to_userspace(mode, file_priv))
1998
				continue;
1999
 
1123 serge 2000
			drm_crtc_convert_to_umode(&u_mode, mode);
2001
			if (copy_to_user(mode_ptr + copied,
2002
					 &u_mode, sizeof(u_mode))) {
2003
				ret = -EFAULT;
2004
				goto out;
2005
			}
2006
			copied++;
2007
		}
2008
	}
2009
	out_resp->count_modes = mode_count;
2010
 
2011
	if ((out_resp->count_props >= props_count) && props_count) {
2012
		copied = 0;
3031 serge 2013
		prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
2014
		prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
2015
		for (i = 0; i < connector->properties.count; i++) {
2016
			if (put_user(connector->properties.ids[i],
1123 serge 2017
					     prop_ptr + copied)) {
2018
					ret = -EFAULT;
2019
					goto out;
2020
				}
2021
 
3031 serge 2022
			if (put_user(connector->properties.values[i],
1123 serge 2023
					     prop_values + copied)) {
2024
					ret = -EFAULT;
2025
					goto out;
2026
				}
2027
				copied++;
2028
			}
2029
		}
2030
	out_resp->count_props = props_count;
2031
 
2032
	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2033
		copied = 0;
3031 serge 2034
		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1123 serge 2035
		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2036
			if (connector->encoder_ids[i] != 0) {
2037
				if (put_user(connector->encoder_ids[i],
2038
					     encoder_ptr + copied)) {
2039
					ret = -EFAULT;
2040
					goto out;
2041
				}
2042
				copied++;
2043
			}
2044
		}
2045
	}
2046
	out_resp->count_encoders = encoders_count;
2047
 
2048
out:
2049
	mutex_unlock(&dev->mode_config.mutex);
3480 Serge 2050
 
1123 serge 2051
	return ret;
2052
}
2053
 
5060 serge 2054
/**
2055
 * drm_mode_getencoder - get encoder configuration
2056
 * @dev: drm device for the ioctl
2057
 * @data: data pointer for the ioctl
2058
 * @file_priv: drm file for the ioctl call
2059
 *
2060
 * Construct a encoder configuration structure to return to the user.
2061
 *
2062
 * Called by the user via ioctl.
2063
 *
2064
 * Returns:
2065
 * Zero on success, errno on failure.
2066
 */
1123 serge 2067
int drm_mode_getencoder(struct drm_device *dev, void *data,
2068
			struct drm_file *file_priv)
2069
{
2070
	struct drm_mode_get_encoder *enc_resp = data;
2071
	struct drm_encoder *encoder;
2072
	int ret = 0;
2073
 
1963 serge 2074
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2075
		return -EINVAL;
2076
 
3480 Serge 2077
	drm_modeset_lock_all(dev);
5060 serge 2078
	encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2079
	if (!encoder) {
4560 Serge 2080
		ret = -ENOENT;
1123 serge 2081
		goto out;
2082
	}
2083
 
2084
	if (encoder->crtc)
2085
		enc_resp->crtc_id = encoder->crtc->base.id;
2086
	else
2087
		enc_resp->crtc_id = 0;
2088
	enc_resp->encoder_type = encoder->encoder_type;
2089
	enc_resp->encoder_id = encoder->base.id;
2090
	enc_resp->possible_crtcs = encoder->possible_crtcs;
2091
	enc_resp->possible_clones = encoder->possible_clones;
2092
 
2093
out:
3480 Serge 2094
	drm_modeset_unlock_all(dev);
1123 serge 2095
	return ret;
2096
}
2097
 
2098
/**
5060 serge 2099
 * drm_mode_getplane_res - enumerate all plane resources
3031 serge 2100
 * @dev: DRM device
2101
 * @data: ioctl data
2102
 * @file_priv: DRM file info
2103
 *
5060 serge 2104
 * Construct a list of plane ids to return to the user.
2105
 *
2106
 * Called by the user via ioctl.
2107
 *
2108
 * Returns:
2109
 * Zero on success, errno on failure.
3031 serge 2110
 */
2111
int drm_mode_getplane_res(struct drm_device *dev, void *data,
2112
			    struct drm_file *file_priv)
2113
{
2114
	struct drm_mode_get_plane_res *plane_resp = data;
2115
	struct drm_mode_config *config;
2116
	struct drm_plane *plane;
2117
	uint32_t __user *plane_ptr;
2118
	int copied = 0, ret = 0;
5060 serge 2119
	unsigned num_planes;
3031 serge 2120
 
2121
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2122
		return -EINVAL;
2123
 
3480 Serge 2124
	drm_modeset_lock_all(dev);
3031 serge 2125
	config = &dev->mode_config;
2126
 
5060 serge 2127
	if (file_priv->universal_planes)
2128
		num_planes = config->num_total_plane;
2129
	else
2130
		num_planes = config->num_overlay_plane;
2131
 
3031 serge 2132
	/*
2133
	 * This ioctl is called twice, once to determine how much space is
2134
	 * needed, and the 2nd time to fill it.
2135
	 */
5060 serge 2136
	if (num_planes &&
2137
	    (plane_resp->count_planes >= num_planes)) {
3031 serge 2138
		plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2139
 
2140
		list_for_each_entry(plane, &config->plane_list, head) {
5060 serge 2141
			/*
2142
			 * Unless userspace set the 'universal planes'
2143
			 * capability bit, only advertise overlays.
2144
			 */
2145
			if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2146
			    !file_priv->universal_planes)
2147
				continue;
2148
 
3031 serge 2149
			if (put_user(plane->base.id, plane_ptr + copied)) {
2150
				ret = -EFAULT;
2151
				goto out;
2152
			}
2153
			copied++;
2154
		}
2155
	}
5060 serge 2156
	plane_resp->count_planes = num_planes;
3031 serge 2157
 
2158
out:
3480 Serge 2159
	drm_modeset_unlock_all(dev);
3031 serge 2160
	return ret;
2161
}
2162
 
2163
/**
5060 serge 2164
 * drm_mode_getplane - get plane configuration
3031 serge 2165
 * @dev: DRM device
2166
 * @data: ioctl data
2167
 * @file_priv: DRM file info
2168
 *
5060 serge 2169
 * Construct a plane configuration structure to return to the user.
2170
 *
2171
 * Called by the user via ioctl.
2172
 *
2173
 * Returns:
2174
 * Zero on success, errno on failure.
3031 serge 2175
 */
2176
int drm_mode_getplane(struct drm_device *dev, void *data,
2177
			struct drm_file *file_priv)
2178
{
2179
	struct drm_mode_get_plane *plane_resp = data;
2180
	struct drm_plane *plane;
2181
	uint32_t __user *format_ptr;
2182
	int ret = 0;
2183
 
2184
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2185
		return -EINVAL;
2186
 
3480 Serge 2187
	drm_modeset_lock_all(dev);
5060 serge 2188
	plane = drm_plane_find(dev, plane_resp->plane_id);
2189
	if (!plane) {
3031 serge 2190
		ret = -ENOENT;
2191
		goto out;
2192
	}
2193
 
2194
	if (plane->crtc)
2195
		plane_resp->crtc_id = plane->crtc->base.id;
2196
	else
2197
		plane_resp->crtc_id = 0;
2198
 
2199
	if (plane->fb)
2200
		plane_resp->fb_id = plane->fb->base.id;
2201
	else
2202
		plane_resp->fb_id = 0;
2203
 
2204
	plane_resp->plane_id = plane->base.id;
2205
	plane_resp->possible_crtcs = plane->possible_crtcs;
4075 Serge 2206
	plane_resp->gamma_size = 0;
3031 serge 2207
 
2208
	/*
2209
	 * This ioctl is called twice, once to determine how much space is
2210
	 * needed, and the 2nd time to fill it.
2211
	 */
2212
	if (plane->format_count &&
2213
	    (plane_resp->count_format_types >= plane->format_count)) {
2214
		format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2215
		if (copy_to_user(format_ptr,
2216
				 plane->format_types,
2217
				 sizeof(uint32_t) * plane->format_count)) {
2218
			ret = -EFAULT;
2219
			goto out;
2220
		}
2221
	}
2222
	plane_resp->count_format_types = plane->format_count;
2223
 
2224
out:
3480 Serge 2225
	drm_modeset_unlock_all(dev);
3031 serge 2226
	return ret;
2227
}
2228
 
5060 serge 2229
/*
2230
 * setplane_internal - setplane handler for internal callers
3031 serge 2231
 *
5060 serge 2232
 * Note that we assume an extra reference has already been taken on fb.  If the
2233
 * update fails, this reference will be dropped before return; if it succeeds,
2234
 * the previous framebuffer (if any) will be unreferenced instead.
2235
 *
2236
 * src_{x,y,w,h} are provided in 16.16 fixed point format
3031 serge 2237
 */
5060 serge 2238
static int setplane_internal(struct drm_plane *plane,
2239
			     struct drm_crtc *crtc,
2240
			     struct drm_framebuffer *fb,
2241
			     int32_t crtc_x, int32_t crtc_y,
2242
			     uint32_t crtc_w, uint32_t crtc_h,
2243
			     /* src_{x,y,w,h} values are 16.16 fixed point */
2244
			     uint32_t src_x, uint32_t src_y,
2245
			     uint32_t src_w, uint32_t src_h)
3031 serge 2246
{
5060 serge 2247
	struct drm_device *dev = plane->dev;
2248
	struct drm_framebuffer *old_fb = NULL;
3031 serge 2249
	int ret = 0;
2250
	unsigned int fb_width, fb_height;
2251
	int i;
2252
 
2253
	/* No fb means shut it down */
5060 serge 2254
	if (!fb) {
3480 Serge 2255
		drm_modeset_lock_all(dev);
2256
		old_fb = plane->fb;
5060 serge 2257
		ret = plane->funcs->disable_plane(plane);
2258
		if (!ret) {
3031 serge 2259
		plane->crtc = NULL;
2260
		plane->fb = NULL;
5060 serge 2261
		} else {
2262
			old_fb = NULL;
2263
		}
3480 Serge 2264
		drm_modeset_unlock_all(dev);
3031 serge 2265
		goto out;
2266
	}
2267
 
5060 serge 2268
	/* Check whether this plane is usable on this CRTC */
2269
	if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2270
		DRM_DEBUG_KMS("Invalid crtc for plane\n");
2271
		ret = -EINVAL;
3031 serge 2272
		goto out;
2273
	}
2274
 
2275
	/* Check whether this plane supports the fb pixel format. */
2276
	for (i = 0; i < plane->format_count; i++)
2277
		if (fb->pixel_format == plane->format_types[i])
2278
			break;
2279
	if (i == plane->format_count) {
4075 Serge 2280
		DRM_DEBUG_KMS("Invalid pixel format %s\n",
2281
			      drm_get_format_name(fb->pixel_format));
3031 serge 2282
		ret = -EINVAL;
2283
		goto out;
2284
	}
2285
 
2286
	fb_width = fb->width << 16;
2287
	fb_height = fb->height << 16;
2288
 
2289
	/* Make sure source coordinates are inside the fb. */
5060 serge 2290
	if (src_w > fb_width ||
2291
	    src_x > fb_width - src_w ||
2292
	    src_h > fb_height ||
2293
	    src_y > fb_height - src_h) {
3031 serge 2294
		DRM_DEBUG_KMS("Invalid source coordinates "
2295
			      "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
5060 serge 2296
			      src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2297
			      src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2298
			      src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2299
			      src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
3031 serge 2300
		ret = -ENOSPC;
2301
		goto out;
2302
	}
2303
 
3480 Serge 2304
	drm_modeset_lock_all(dev);
5060 serge 2305
	old_fb = plane->fb;
3031 serge 2306
	ret = plane->funcs->update_plane(plane, crtc, fb,
5060 serge 2307
					 crtc_x, crtc_y, crtc_w, crtc_h,
2308
					 src_x, src_y, src_w, src_h);
3031 serge 2309
	if (!ret) {
2310
		plane->crtc = crtc;
2311
		plane->fb = fb;
3480 Serge 2312
		fb = NULL;
5060 serge 2313
	} else {
2314
		old_fb = NULL;
3031 serge 2315
	}
3480 Serge 2316
	drm_modeset_unlock_all(dev);
3031 serge 2317
 
2318
out:
3480 Serge 2319
	if (fb)
2320
		drm_framebuffer_unreference(fb);
2321
	if (old_fb)
2322
		drm_framebuffer_unreference(old_fb);
3031 serge 2323
 
2324
	return ret;
5060 serge 2325
 
3031 serge 2326
}
5060 serge 2327
 
2328
/**
2329
 * drm_mode_setplane - configure a plane's configuration
2330
 * @dev: DRM device
2331
 * @data: ioctl data*
2332
 * @file_priv: DRM file info
2333
 *
2334
 * Set plane configuration, including placement, fb, scaling, and other factors.
2335
 * Or pass a NULL fb to disable (planes may be disabled without providing a
2336
 * valid crtc).
2337
 *
2338
 * Returns:
2339
 * Zero on success, errno on failure.
2340
 */
2341
int drm_mode_setplane(struct drm_device *dev, void *data,
2342
		      struct drm_file *file_priv)
2343
{
2344
	struct drm_mode_set_plane *plane_req = data;
2345
	struct drm_mode_object *obj;
2346
	struct drm_plane *plane;
2347
	struct drm_crtc *crtc = NULL;
2348
	struct drm_framebuffer *fb = NULL;
2349
 
2350
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2351
		return -EINVAL;
2352
 
2353
	/* Give drivers some help against integer overflows */
2354
	if (plane_req->crtc_w > INT_MAX ||
2355
	    plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2356
	    plane_req->crtc_h > INT_MAX ||
2357
	    plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2358
		DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2359
			      plane_req->crtc_w, plane_req->crtc_h,
2360
			      plane_req->crtc_x, plane_req->crtc_y);
2361
		return -ERANGE;
2362
	}
2363
 
2364
	/*
2365
	 * First, find the plane, crtc, and fb objects.  If not available,
2366
	 * we don't bother to call the driver.
2367
	 */
2368
	obj = drm_mode_object_find(dev, plane_req->plane_id,
2369
				   DRM_MODE_OBJECT_PLANE);
2370
	if (!obj) {
2371
		DRM_DEBUG_KMS("Unknown plane ID %d\n",
2372
			      plane_req->plane_id);
2373
		return -ENOENT;
2374
	}
2375
	plane = obj_to_plane(obj);
2376
 
2377
	if (plane_req->fb_id) {
2378
		fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2379
		if (!fb) {
2380
			DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2381
				      plane_req->fb_id);
2382
			return -ENOENT;
2383
		}
2384
 
2385
		obj = drm_mode_object_find(dev, plane_req->crtc_id,
2386
					   DRM_MODE_OBJECT_CRTC);
2387
		if (!obj) {
2388
			DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2389
				      plane_req->crtc_id);
2390
			return -ENOENT;
2391
		}
2392
		crtc = obj_to_crtc(obj);
2393
	}
2394
 
2395
	/*
2396
	 * setplane_internal will take care of deref'ing either the old or new
2397
	 * framebuffer depending on success.
2398
	 */
2399
	return setplane_internal(plane, crtc, fb,
2400
				 plane_req->crtc_x, plane_req->crtc_y,
2401
				 plane_req->crtc_w, plane_req->crtc_h,
2402
				 plane_req->src_x, plane_req->src_y,
2403
				 plane_req->src_w, plane_req->src_h);
2404
}
3480 Serge 2405
#endif
3031 serge 2406
 
2407
/**
3480 Serge 2408
 * drm_mode_set_config_internal - helper to call ->set_config
2409
 * @set: modeset config to set
2410
 *
2411
 * This is a little helper to wrap internal calls to the ->set_config driver
2412
 * interface. The only thing it adds is correct refcounting dance.
5060 serge 2413
 *
2414
 * Returns:
2415
 * Zero on success, errno on failure.
3480 Serge 2416
 */
2417
int drm_mode_set_config_internal(struct drm_mode_set *set)
2418
{
2419
	struct drm_crtc *crtc = set->crtc;
4075 Serge 2420
	struct drm_framebuffer *fb;
2421
	struct drm_crtc *tmp;
3480 Serge 2422
	int ret;
2423
 
4075 Serge 2424
	/*
2425
	 * NOTE: ->set_config can also disable other crtcs (if we steal all
2426
	 * connectors from it), hence we need to refcount the fbs across all
2427
	 * crtcs. Atomic modeset will have saner semantics ...
2428
	 */
2429
	list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
5060 serge 2430
		tmp->old_fb = tmp->primary->fb;
4075 Serge 2431
 
3480 Serge 2432
	fb = set->fb;
2433
 
2434
	ret = crtc->funcs->set_config(set);
2435
	if (ret == 0) {
5060 serge 2436
		crtc->primary->crtc = crtc;
2437
		crtc->primary->fb = fb;
3480 Serge 2438
	}
2439
 
4075 Serge 2440
	list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
5060 serge 2441
		if (tmp->primary->fb)
2442
			drm_framebuffer_reference(tmp->primary->fb);
4075 Serge 2443
//		if (tmp->old_fb)
2444
//			drm_framebuffer_unreference(tmp->old_fb);
2445
	}
2446
 
3480 Serge 2447
	return ret;
2448
}
2449
EXPORT_SYMBOL(drm_mode_set_config_internal);
2450
 
2451
#if 0
5060 serge 2452
/**
2453
 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2454
 *     CRTC viewport
2455
 * @crtc: CRTC that framebuffer will be displayed on
2456
 * @x: x panning
2457
 * @y: y panning
2458
 * @mode: mode that framebuffer will be displayed under
2459
 * @fb: framebuffer to check size of
4560 Serge 2460
 */
5060 serge 2461
int drm_crtc_check_viewport(const struct drm_crtc *crtc,
4560 Serge 2462
				   int x, int y,
2463
				   const struct drm_display_mode *mode,
2464
				   const struct drm_framebuffer *fb)
2465
 
2466
{
2467
	int hdisplay, vdisplay;
2468
 
2469
	hdisplay = mode->hdisplay;
2470
	vdisplay = mode->vdisplay;
2471
 
2472
	if (drm_mode_is_stereo(mode)) {
2473
		struct drm_display_mode adjusted = *mode;
2474
 
2475
		drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2476
		hdisplay = adjusted.crtc_hdisplay;
2477
		vdisplay = adjusted.crtc_vdisplay;
2478
	}
2479
 
2480
	if (crtc->invert_dimensions)
2481
		swap(hdisplay, vdisplay);
2482
 
2483
	if (hdisplay > fb->width ||
2484
	    vdisplay > fb->height ||
2485
	    x > fb->width - hdisplay ||
2486
	    y > fb->height - vdisplay) {
2487
		DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2488
			      fb->width, fb->height, hdisplay, vdisplay, x, y,
2489
			      crtc->invert_dimensions ? " (inverted)" : "");
2490
		return -ENOSPC;
2491
	}
2492
 
2493
	return 0;
2494
}
5060 serge 2495
EXPORT_SYMBOL(drm_crtc_check_viewport);
4560 Serge 2496
 
3480 Serge 2497
/**
1123 serge 2498
 * drm_mode_setcrtc - set CRTC configuration
3480 Serge 2499
 * @dev: drm device for the ioctl
2500
 * @data: data pointer for the ioctl
2501
 * @file_priv: drm file for the ioctl call
1123 serge 2502
 *
2503
 * Build a new CRTC configuration based on user request.
2504
 *
2505
 * Called by the user via ioctl.
2506
 *
5060 serge 2507
 * Returns:
1123 serge 2508
 * Zero on success, errno on failure.
2509
 */
2510
int drm_mode_setcrtc(struct drm_device *dev, void *data,
2511
		     struct drm_file *file_priv)
2512
{
2513
	struct drm_mode_config *config = &dev->mode_config;
2514
	struct drm_mode_crtc *crtc_req = data;
3031 serge 2515
	struct drm_crtc *crtc;
1123 serge 2516
	struct drm_connector **connector_set = NULL, *connector;
2517
	struct drm_framebuffer *fb = NULL;
2518
	struct drm_display_mode *mode = NULL;
2519
	struct drm_mode_set set;
2520
	uint32_t __user *set_connectors_ptr;
3031 serge 2521
	int ret;
1123 serge 2522
	int i;
2523
 
1963 serge 2524
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2525
		return -EINVAL;
2526
 
3031 serge 2527
	/* For some reason crtc x/y offsets are signed internally. */
2528
	if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2529
		return -ERANGE;
2530
 
3480 Serge 2531
	drm_modeset_lock_all(dev);
5060 serge 2532
	crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2533
	if (!crtc) {
1179 serge 2534
		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
4560 Serge 2535
		ret = -ENOENT;
1123 serge 2536
		goto out;
2537
	}
1963 serge 2538
	DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1123 serge 2539
 
2540
	if (crtc_req->mode_valid) {
2541
		/* If we have a mode we need a framebuffer. */
2542
		/* If we pass -1, set the mode with the currently bound fb */
2543
		if (crtc_req->fb_id == -1) {
5060 serge 2544
			if (!crtc->primary->fb) {
3031 serge 2545
				DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2546
				ret = -EINVAL;
2547
				goto out;
2548
			}
5060 serge 2549
			fb = crtc->primary->fb;
3480 Serge 2550
			/* Make refcounting symmetric with the lookup path. */
2551
			drm_framebuffer_reference(fb);
1123 serge 2552
		} else {
3480 Serge 2553
			fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2554
			if (!fb) {
1179 serge 2555
				DRM_DEBUG_KMS("Unknown FB ID%d\n",
2556
						crtc_req->fb_id);
4560 Serge 2557
				ret = -ENOENT;
1123 serge 2558
				goto out;
2559
			}
2560
		}
2561
 
2562
		mode = drm_mode_create(dev);
3031 serge 2563
		if (!mode) {
2564
			ret = -ENOMEM;
2565
			goto out;
2566
		}
2567
 
2568
		ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2569
		if (ret) {
2570
			DRM_DEBUG_KMS("Invalid mode\n");
2571
			goto out;
2572
		}
2573
 
1123 serge 2574
		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
3031 serge 2575
 
4560 Serge 2576
		ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2577
					      mode, fb);
2578
		if (ret)
2579
			goto out;
3031 serge 2580
 
1123 serge 2581
	}
2582
 
2583
	if (crtc_req->count_connectors == 0 && mode) {
1179 serge 2584
		DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1123 serge 2585
		ret = -EINVAL;
2586
		goto out;
2587
	}
2588
 
1179 serge 2589
	if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2590
		DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1123 serge 2591
			  crtc_req->count_connectors);
2592
		ret = -EINVAL;
2593
		goto out;
2594
	}
2595
 
2596
	if (crtc_req->count_connectors > 0) {
2597
		u32 out_id;
2598
 
2599
		/* Avoid unbounded kernel memory allocation */
2600
		if (crtc_req->count_connectors > config->num_connector) {
2601
			ret = -EINVAL;
2602
			goto out;
2603
		}
2604
 
2605
		connector_set = kmalloc(crtc_req->count_connectors *
2606
					sizeof(struct drm_connector *),
2607
					GFP_KERNEL);
2608
		if (!connector_set) {
2609
			ret = -ENOMEM;
2610
			goto out;
2611
		}
2612
 
2613
		for (i = 0; i < crtc_req->count_connectors; i++) {
3031 serge 2614
			set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
1123 serge 2615
			if (get_user(out_id, &set_connectors_ptr[i])) {
2616
				ret = -EFAULT;
2617
				goto out;
2618
			}
2619
 
5060 serge 2620
			connector = drm_connector_find(dev, out_id);
2621
			if (!connector) {
1179 serge 2622
				DRM_DEBUG_KMS("Connector id %d unknown\n",
2623
						out_id);
4560 Serge 2624
				ret = -ENOENT;
1123 serge 2625
				goto out;
2626
			}
1963 serge 2627
			DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2628
					connector->base.id,
5060 serge 2629
					connector->name);
1123 serge 2630
 
2631
			connector_set[i] = connector;
2632
		}
2633
	}
2634
 
2635
	set.crtc = crtc;
2636
	set.x = crtc_req->x;
2637
	set.y = crtc_req->y;
2638
	set.mode = mode;
2639
	set.connectors = connector_set;
2640
	set.num_connectors = crtc_req->count_connectors;
1179 serge 2641
	set.fb = fb;
3480 Serge 2642
	ret = drm_mode_set_config_internal(&set);
1123 serge 2643
 
2644
out:
3480 Serge 2645
	if (fb)
2646
		drm_framebuffer_unreference(fb);
2647
 
1123 serge 2648
	kfree(connector_set);
3031 serge 2649
	drm_mode_destroy(dev, mode);
3480 Serge 2650
	drm_modeset_unlock_all(dev);
1123 serge 2651
	return ret;
2652
}
2653
 
4075 Serge 2654
static int drm_mode_cursor_common(struct drm_device *dev,
2655
				  struct drm_mode_cursor2 *req,
2656
				  struct drm_file *file_priv)
1123 serge 2657
{
2658
	struct drm_crtc *crtc;
2659
	int ret = 0;
2660
 
1963 serge 2661
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2662
		return -EINVAL;
2663
 
3031 serge 2664
	if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
1123 serge 2665
		return -EINVAL;
2666
 
5060 serge 2667
	crtc = drm_crtc_find(dev, req->crtc_id);
2668
	if (!crtc) {
1179 serge 2669
		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
4560 Serge 2670
		return -ENOENT;
1123 serge 2671
	}
2672
 
5060 serge 2673
	/*
2674
	 * If this crtc has a universal cursor plane, call that plane's update
2675
	 * handler rather than using legacy cursor handlers.
2676
	 */
2677
	if (crtc->cursor)
2678
		return drm_mode_cursor_universal(crtc, req, file_priv);
2679
 
2680
	drm_modeset_lock(&crtc->mutex, NULL);
1123 serge 2681
	if (req->flags & DRM_MODE_CURSOR_BO) {
4075 Serge 2682
		if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
1123 serge 2683
			ret = -ENXIO;
2684
			goto out;
2685
		}
2686
		/* Turns off the cursor if handle is 0 */
4075 Serge 2687
		if (crtc->funcs->cursor_set2)
2688
			ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2689
						      req->width, req->height, req->hot_x, req->hot_y);
2690
		else
1123 serge 2691
		ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2692
					      req->width, req->height);
2693
	}
2694
 
2695
	if (req->flags & DRM_MODE_CURSOR_MOVE) {
2696
		if (crtc->funcs->cursor_move) {
2697
			ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2698
		} else {
2699
			ret = -EFAULT;
2700
			goto out;
2701
		}
2702
	}
2703
out:
5060 serge 2704
	drm_modeset_unlock(&crtc->mutex);
3480 Serge 2705
 
1123 serge 2706
	return ret;
4075 Serge 2707
 
1123 serge 2708
}
5060 serge 2709
 
2710
 
2711
/**
2712
 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2713
 * @dev: drm device for the ioctl
2714
 * @data: data pointer for the ioctl
2715
 * @file_priv: drm file for the ioctl call
2716
 *
2717
 * Set the cursor configuration based on user request.
2718
 *
2719
 * Called by the user via ioctl.
2720
 *
2721
 * Returns:
2722
 * Zero on success, errno on failure.
2723
 */
4075 Serge 2724
int drm_mode_cursor_ioctl(struct drm_device *dev,
2725
			void *data, struct drm_file *file_priv)
2726
{
2727
	struct drm_mode_cursor *req = data;
2728
	struct drm_mode_cursor2 new_req;
2729
 
2730
	memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2731
	new_req.hot_x = new_req.hot_y = 0;
2732
 
2733
	return drm_mode_cursor_common(dev, &new_req, file_priv);
2734
}
2735
 
5060 serge 2736
/**
2737
 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2738
 * @dev: drm device for the ioctl
2739
 * @data: data pointer for the ioctl
2740
 * @file_priv: drm file for the ioctl call
2741
 *
2742
 * Set the cursor configuration based on user request. This implements the 2nd
2743
 * version of the cursor ioctl, which allows userspace to additionally specify
2744
 * the hotspot of the pointer.
2745
 *
2746
 * Called by the user via ioctl.
2747
 *
2748
 * Returns:
2749
 * Zero on success, errno on failure.
2750
 */
4075 Serge 2751
int drm_mode_cursor2_ioctl(struct drm_device *dev,
2752
			   void *data, struct drm_file *file_priv)
2753
{
2754
	struct drm_mode_cursor2 *req = data;
2755
	return drm_mode_cursor_common(dev, req, file_priv);
2756
}
3031 serge 2757
#endif
4560 Serge 2758
 
5060 serge 2759
/**
2760
 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2761
 * @bpp: bits per pixels
2762
 * @depth: bit depth per pixel
2763
 *
2764
 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2765
 * Useful in fbdev emulation code, since that deals in those values.
2766
 */
3031 serge 2767
uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2768
{
2769
	uint32_t fmt;
1123 serge 2770
 
3031 serge 2771
	switch (bpp) {
2772
	case 8:
3480 Serge 2773
		fmt = DRM_FORMAT_C8;
3031 serge 2774
		break;
2775
	case 16:
2776
		if (depth == 15)
2777
			fmt = DRM_FORMAT_XRGB1555;
2778
		else
2779
			fmt = DRM_FORMAT_RGB565;
2780
		break;
2781
	case 24:
2782
		fmt = DRM_FORMAT_RGB888;
2783
		break;
2784
	case 32:
2785
		if (depth == 24)
2786
			fmt = DRM_FORMAT_XRGB8888;
2787
		else if (depth == 30)
2788
			fmt = DRM_FORMAT_XRGB2101010;
2789
		else
2790
			fmt = DRM_FORMAT_ARGB8888;
2791
		break;
2792
	default:
2793
		DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2794
		fmt = DRM_FORMAT_XRGB8888;
2795
		break;
2796
	}
2797
 
2798
	return fmt;
2799
}
2800
EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2801
#if 0
1123 serge 2802
/**
2803
 * drm_mode_addfb - add an FB to the graphics configuration
3480 Serge 2804
 * @dev: drm device for the ioctl
2805
 * @data: data pointer for the ioctl
2806
 * @file_priv: drm file for the ioctl call
1123 serge 2807
 *
5060 serge 2808
 * Add a new FB to the specified CRTC, given a user request. This is the
2809
 * original addfb ioclt which only supported RGB formats.
1123 serge 2810
 *
2811
 * Called by the user via ioctl.
2812
 *
5060 serge 2813
 * Returns:
1123 serge 2814
 * Zero on success, errno on failure.
2815
 */
2816
int drm_mode_addfb(struct drm_device *dev,
2817
		   void *data, struct drm_file *file_priv)
2818
{
3031 serge 2819
	struct drm_mode_fb_cmd *or = data;
2820
	struct drm_mode_fb_cmd2 r = {};
1123 serge 2821
	struct drm_mode_config *config = &dev->mode_config;
2822
	struct drm_framebuffer *fb;
2823
	int ret = 0;
2824
 
3031 serge 2825
	/* Use new struct with format internally */
2826
	r.fb_id = or->fb_id;
2827
	r.width = or->width;
2828
	r.height = or->height;
2829
	r.pitches[0] = or->pitch;
2830
	r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2831
	r.handles[0] = or->handle;
2832
 
1963 serge 2833
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2834
		return -EINVAL;
2835
 
3031 serge 2836
	if ((config->min_width > r.width) || (r.width > config->max_width))
2837
		return -EINVAL;
2838
 
2839
	if ((config->min_height > r.height) || (r.height > config->max_height))
2840
		return -EINVAL;
2841
 
2842
	fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2843
	if (IS_ERR(fb)) {
2844
		DRM_DEBUG_KMS("could not create framebuffer\n");
3480 Serge 2845
		return PTR_ERR(fb);
3031 serge 2846
	}
2847
 
3480 Serge 2848
	mutex_lock(&file_priv->fbs_lock);
3031 serge 2849
	or->fb_id = fb->base.id;
2850
	list_add(&fb->filp_head, &file_priv->fbs);
2851
	DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3480 Serge 2852
	mutex_unlock(&file_priv->fbs_lock);
3031 serge 2853
 
2854
	return ret;
2855
}
2856
 
2857
static int format_check(const struct drm_mode_fb_cmd2 *r)
2858
{
2859
	uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2860
 
2861
	switch (format) {
2862
	case DRM_FORMAT_C8:
2863
	case DRM_FORMAT_RGB332:
2864
	case DRM_FORMAT_BGR233:
2865
	case DRM_FORMAT_XRGB4444:
2866
	case DRM_FORMAT_XBGR4444:
2867
	case DRM_FORMAT_RGBX4444:
2868
	case DRM_FORMAT_BGRX4444:
2869
	case DRM_FORMAT_ARGB4444:
2870
	case DRM_FORMAT_ABGR4444:
2871
	case DRM_FORMAT_RGBA4444:
2872
	case DRM_FORMAT_BGRA4444:
2873
	case DRM_FORMAT_XRGB1555:
2874
	case DRM_FORMAT_XBGR1555:
2875
	case DRM_FORMAT_RGBX5551:
2876
	case DRM_FORMAT_BGRX5551:
2877
	case DRM_FORMAT_ARGB1555:
2878
	case DRM_FORMAT_ABGR1555:
2879
	case DRM_FORMAT_RGBA5551:
2880
	case DRM_FORMAT_BGRA5551:
2881
	case DRM_FORMAT_RGB565:
2882
	case DRM_FORMAT_BGR565:
2883
	case DRM_FORMAT_RGB888:
2884
	case DRM_FORMAT_BGR888:
2885
	case DRM_FORMAT_XRGB8888:
2886
	case DRM_FORMAT_XBGR8888:
2887
	case DRM_FORMAT_RGBX8888:
2888
	case DRM_FORMAT_BGRX8888:
2889
	case DRM_FORMAT_ARGB8888:
2890
	case DRM_FORMAT_ABGR8888:
2891
	case DRM_FORMAT_RGBA8888:
2892
	case DRM_FORMAT_BGRA8888:
2893
	case DRM_FORMAT_XRGB2101010:
2894
	case DRM_FORMAT_XBGR2101010:
2895
	case DRM_FORMAT_RGBX1010102:
2896
	case DRM_FORMAT_BGRX1010102:
2897
	case DRM_FORMAT_ARGB2101010:
2898
	case DRM_FORMAT_ABGR2101010:
2899
	case DRM_FORMAT_RGBA1010102:
2900
	case DRM_FORMAT_BGRA1010102:
2901
	case DRM_FORMAT_YUYV:
2902
	case DRM_FORMAT_YVYU:
2903
	case DRM_FORMAT_UYVY:
2904
	case DRM_FORMAT_VYUY:
2905
	case DRM_FORMAT_AYUV:
2906
	case DRM_FORMAT_NV12:
2907
	case DRM_FORMAT_NV21:
2908
	case DRM_FORMAT_NV16:
2909
	case DRM_FORMAT_NV61:
2910
	case DRM_FORMAT_NV24:
2911
	case DRM_FORMAT_NV42:
2912
	case DRM_FORMAT_YUV410:
2913
	case DRM_FORMAT_YVU410:
2914
	case DRM_FORMAT_YUV411:
2915
	case DRM_FORMAT_YVU411:
2916
	case DRM_FORMAT_YUV420:
2917
	case DRM_FORMAT_YVU420:
2918
	case DRM_FORMAT_YUV422:
2919
	case DRM_FORMAT_YVU422:
2920
	case DRM_FORMAT_YUV444:
2921
	case DRM_FORMAT_YVU444:
2922
		return 0;
2923
	default:
4560 Serge 2924
		DRM_DEBUG_KMS("invalid pixel format %s\n",
2925
			      drm_get_format_name(r->pixel_format));
3031 serge 2926
		return -EINVAL;
2927
	}
2928
}
2929
 
2930
static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2931
{
2932
	int ret, hsub, vsub, num_planes, i;
2933
 
2934
	ret = format_check(r);
2935
	if (ret) {
4075 Serge 2936
		DRM_DEBUG_KMS("bad framebuffer format %s\n",
2937
			      drm_get_format_name(r->pixel_format));
3031 serge 2938
		return ret;
2939
	}
2940
 
2941
	hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2942
	vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2943
	num_planes = drm_format_num_planes(r->pixel_format);
2944
 
2945
	if (r->width == 0 || r->width % hsub) {
2946
		DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2947
		return -EINVAL;
2948
	}
2949
 
2950
	if (r->height == 0 || r->height % vsub) {
2951
		DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2952
		return -EINVAL;
2953
	}
2954
 
2955
	for (i = 0; i < num_planes; i++) {
2956
		unsigned int width = r->width / (i != 0 ? hsub : 1);
3192 Serge 2957
		unsigned int height = r->height / (i != 0 ? vsub : 1);
2958
		unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3031 serge 2959
 
2960
		if (!r->handles[i]) {
2961
			DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2962
			return -EINVAL;
2963
		}
2964
 
3192 Serge 2965
		if ((uint64_t) width * cpp > UINT_MAX)
2966
			return -ERANGE;
2967
 
2968
		if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2969
			return -ERANGE;
2970
 
2971
		if (r->pitches[i] < width * cpp) {
3031 serge 2972
			DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2973
			return -EINVAL;
2974
		}
2975
	}
2976
 
2977
	return 0;
2978
}
2979
 
5060 serge 2980
static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
2981
							struct drm_mode_fb_cmd2 *r,
2982
							struct drm_file *file_priv)
3031 serge 2983
{
2984
	struct drm_mode_config *config = &dev->mode_config;
2985
	struct drm_framebuffer *fb;
2986
	int ret;
2987
 
3192 Serge 2988
	if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2989
		DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
5060 serge 2990
		return ERR_PTR(-EINVAL);
3192 Serge 2991
	}
2992
 
1123 serge 2993
	if ((config->min_width > r->width) || (r->width > config->max_width)) {
3031 serge 2994
		DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2995
			  r->width, config->min_width, config->max_width);
5060 serge 2996
		return ERR_PTR(-EINVAL);
1123 serge 2997
	}
2998
	if ((config->min_height > r->height) || (r->height > config->max_height)) {
3031 serge 2999
		DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3000
			  r->height, config->min_height, config->max_height);
5060 serge 3001
		return ERR_PTR(-EINVAL);
1123 serge 3002
	}
3003
 
3031 serge 3004
	ret = framebuffer_check(r);
3005
	if (ret)
5060 serge 3006
		return ERR_PTR(ret);
3031 serge 3007
 
1123 serge 3008
	fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1963 serge 3009
	if (IS_ERR(fb)) {
3031 serge 3010
		DRM_DEBUG_KMS("could not create framebuffer\n");
5060 serge 3011
		return fb;
1123 serge 3012
	}
3013
 
3480 Serge 3014
	mutex_lock(&file_priv->fbs_lock);
1123 serge 3015
	r->fb_id = fb->base.id;
3016
	list_add(&fb->filp_head, &file_priv->fbs);
1963 serge 3017
	DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3480 Serge 3018
	mutex_unlock(&file_priv->fbs_lock);
1123 serge 3019
 
5060 serge 3020
	return fb;
3021
}
3480 Serge 3022
 
5060 serge 3023
/**
3024
 * drm_mode_addfb2 - add an FB to the graphics configuration
3025
 * @dev: drm device for the ioctl
3026
 * @data: data pointer for the ioctl
3027
 * @file_priv: drm file for the ioctl call
3028
 *
3029
 * Add a new FB to the specified CRTC, given a user request with format. This is
3030
 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3031
 * and uses fourcc codes as pixel format specifiers.
3032
 *
3033
 * Called by the user via ioctl.
3034
 *
3035
 * Returns:
3036
 * Zero on success, errno on failure.
3037
 */
3038
int drm_mode_addfb2(struct drm_device *dev,
3039
		    void *data, struct drm_file *file_priv)
3040
{
3041
	struct drm_framebuffer *fb;
3042
 
3043
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3044
		return -EINVAL;
3045
 
3046
	fb = add_framebuffer_internal(dev, data, file_priv);
3047
	if (IS_ERR(fb))
3048
		return PTR_ERR(fb);
3049
 
3050
	return 0;
1123 serge 3051
}
3052
 
3053
/**
3054
 * drm_mode_rmfb - remove an FB from the configuration
3480 Serge 3055
 * @dev: drm device for the ioctl
3056
 * @data: data pointer for the ioctl
3057
 * @file_priv: drm file for the ioctl call
1123 serge 3058
 *
3059
 * Remove the FB specified by the user.
3060
 *
3061
 * Called by the user via ioctl.
3062
 *
5060 serge 3063
 * Returns:
1123 serge 3064
 * Zero on success, errno on failure.
3065
 */
3066
int drm_mode_rmfb(struct drm_device *dev,
3067
		   void *data, struct drm_file *file_priv)
3068
{
3069
	struct drm_framebuffer *fb = NULL;
3070
	struct drm_framebuffer *fbl = NULL;
3071
	uint32_t *id = data;
3072
	int found = 0;
3073
 
1963 serge 3074
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3075
		return -EINVAL;
3076
 
3480 Serge 3077
	mutex_lock(&file_priv->fbs_lock);
3078
	mutex_lock(&dev->mode_config.fb_lock);
3079
	fb = __drm_framebuffer_lookup(dev, *id);
3080
	if (!fb)
3081
		goto fail_lookup;
1123 serge 3082
 
3083
	list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3084
		if (fb == fbl)
3085
			found = 1;
3480 Serge 3086
	if (!found)
3087
		goto fail_lookup;
1123 serge 3088
 
3480 Serge 3089
	/* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3090
	__drm_framebuffer_unregister(dev, fb);
1123 serge 3091
 
3480 Serge 3092
	list_del_init(&fb->filp_head);
3093
	mutex_unlock(&dev->mode_config.fb_lock);
3094
	mutex_unlock(&file_priv->fbs_lock);
3095
 
3031 serge 3096
	drm_framebuffer_remove(fb);
1123 serge 3097
 
3480 Serge 3098
	return 0;
3099
 
3100
fail_lookup:
3101
	mutex_unlock(&dev->mode_config.fb_lock);
3102
	mutex_unlock(&file_priv->fbs_lock);
3103
 
4560 Serge 3104
	return -ENOENT;
1123 serge 3105
}
3106
 
3107
/**
3108
 * drm_mode_getfb - get FB info
3480 Serge 3109
 * @dev: drm device for the ioctl
3110
 * @data: data pointer for the ioctl
3111
 * @file_priv: drm file for the ioctl call
1123 serge 3112
 *
3113
 * Lookup the FB given its ID and return info about it.
3114
 *
3115
 * Called by the user via ioctl.
3116
 *
5060 serge 3117
 * Returns:
1123 serge 3118
 * Zero on success, errno on failure.
3119
 */
3120
int drm_mode_getfb(struct drm_device *dev,
3121
		   void *data, struct drm_file *file_priv)
3122
{
3123
	struct drm_mode_fb_cmd *r = data;
3124
	struct drm_framebuffer *fb;
3480 Serge 3125
	int ret;
1123 serge 3126
 
1963 serge 3127
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3128
		return -EINVAL;
3129
 
3480 Serge 3130
	fb = drm_framebuffer_lookup(dev, r->fb_id);
3131
	if (!fb)
4560 Serge 3132
		return -ENOENT;
1123 serge 3133
 
3134
	r->height = fb->height;
3135
	r->width = fb->width;
3136
	r->depth = fb->depth;
3137
	r->bpp = fb->bits_per_pixel;
3031 serge 3138
	r->pitch = fb->pitches[0];
4104 Serge 3139
	if (fb->funcs->create_handle) {
3140
		if (file_priv->is_master || capable(CAP_SYS_ADMIN)) {
3141
			ret = fb->funcs->create_handle(fb, file_priv,
3142
						       &r->handle);
3143
		} else {
3144
			/* GET_FB() is an unprivileged ioctl so we must not
3145
			 * return a buffer-handle to non-master processes! For
3146
			 * backwards-compatibility reasons, we cannot make
3147
			 * GET_FB() privileged, so just return an invalid handle
3148
			 * for non-masters. */
3149
			r->handle = 0;
3150
			ret = 0;
3151
		}
3152
	} else {
3480 Serge 3153
		ret = -ENODEV;
4104 Serge 3154
	}
1123 serge 3155
 
3480 Serge 3156
	drm_framebuffer_unreference(fb);
3157
 
1123 serge 3158
	return ret;
3159
}
3160
 
5060 serge 3161
/**
3162
 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3163
 * @dev: drm device for the ioctl
3164
 * @data: data pointer for the ioctl
3165
 * @file_priv: drm file for the ioctl call
3166
 *
3167
 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3168
 * rectangle list. Generic userspace which does frontbuffer rendering must call
3169
 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3170
 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3171
 *
3172
 * Modesetting drivers which always update the frontbuffer do not need to
3173
 * implement the corresponding ->dirty framebuffer callback.
3174
 *
3175
 * Called by the user via ioctl.
3176
 *
3177
 * Returns:
3178
 * Zero on success, errno on failure.
3179
 */
1412 serge 3180
int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3181
			   void *data, struct drm_file *file_priv)
3182
{
3183
	struct drm_clip_rect __user *clips_ptr;
3184
	struct drm_clip_rect *clips = NULL;
3185
	struct drm_mode_fb_dirty_cmd *r = data;
3186
	struct drm_framebuffer *fb;
3187
	unsigned flags;
3188
	int num_clips;
3031 serge 3189
	int ret;
1412 serge 3190
 
1963 serge 3191
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3192
		return -EINVAL;
3193
 
3480 Serge 3194
	fb = drm_framebuffer_lookup(dev, r->fb_id);
3195
	if (!fb)
4560 Serge 3196
		return -ENOENT;
1412 serge 3197
 
3198
	num_clips = r->num_clips;
3031 serge 3199
	clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
1412 serge 3200
 
3201
	if (!num_clips != !clips_ptr) {
3202
		ret = -EINVAL;
3203
		goto out_err1;
3204
	}
3205
 
3206
	flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3207
 
3208
	/* If userspace annotates copy, clips must come in pairs */
3209
	if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3210
		ret = -EINVAL;
3211
		goto out_err1;
3212
	}
3213
 
3214
	if (num_clips && clips_ptr) {
3031 serge 3215
		if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3216
			ret = -EINVAL;
3217
			goto out_err1;
3218
		}
1412 serge 3219
		clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3220
		if (!clips) {
3221
			ret = -ENOMEM;
3222
			goto out_err1;
3223
		}
3224
 
3225
		ret = copy_from_user(clips, clips_ptr,
3226
				     num_clips * sizeof(*clips));
1963 serge 3227
		if (ret) {
3228
			ret = -EFAULT;
1412 serge 3229
			goto out_err2;
3230
	}
1963 serge 3231
	}
1412 serge 3232
 
3233
	if (fb->funcs->dirty) {
1963 serge 3234
		ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3235
				       clips, num_clips);
1412 serge 3236
	} else {
3237
		ret = -ENOSYS;
3238
	}
3239
 
3240
out_err2:
3241
	kfree(clips);
3242
out_err1:
3480 Serge 3243
	drm_framebuffer_unreference(fb);
3244
 
1412 serge 3245
	return ret;
3246
}
3247
 
3248
 
1123 serge 3249
/**
3250
 * drm_fb_release - remove and free the FBs on this file
3480 Serge 3251
 * @priv: drm file for the ioctl
1123 serge 3252
 *
3253
 * Destroy all the FBs associated with @filp.
3254
 *
3255
 * Called by the user via ioctl.
3256
 *
5060 serge 3257
 * Returns:
1123 serge 3258
 * Zero on success, errno on failure.
3259
 */
3260
void drm_fb_release(struct drm_file *priv)
3261
{
3262
	struct drm_device *dev = priv->minor->dev;
3263
	struct drm_framebuffer *fb, *tfb;
3264
 
3480 Serge 3265
	mutex_lock(&priv->fbs_lock);
1123 serge 3266
	list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3480 Serge 3267
 
3268
		mutex_lock(&dev->mode_config.fb_lock);
3269
		/* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3270
		__drm_framebuffer_unregister(dev, fb);
3271
		mutex_unlock(&dev->mode_config.fb_lock);
3272
 
3273
		list_del_init(&fb->filp_head);
3274
 
3275
		/* This will also drop the fpriv->fbs reference. */
3031 serge 3276
		drm_framebuffer_remove(fb);
1123 serge 3277
	}
3480 Serge 3278
	mutex_unlock(&priv->fbs_lock);
1123 serge 3279
}
3280
#endif
3281
 
3282
 
5060 serge 3283
/**
3284
 * drm_property_create - create a new property type
3285
 * @dev: drm device
3286
 * @flags: flags specifying the property type
3287
 * @name: name of the property
3288
 * @num_values: number of pre-defined values
3289
 *
3290
 * This creates a new generic drm property which can then be attached to a drm
3291
 * object with drm_object_attach_property. The returned property object must be
3292
 * freed with drm_property_destroy.
3293
 *
3294
 * Returns:
3295
 * A pointer to the newly created property on success, NULL on failure.
3296
 */
1123 serge 3297
struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3298
					 const char *name, int num_values)
3299
{
3300
	struct drm_property *property = NULL;
3031 serge 3301
	int ret;
1123 serge 3302
 
3303
	property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3304
	if (!property)
3305
		return NULL;
3306
 
5060 serge 3307
	property->dev = dev;
3308
 
1123 serge 3309
	if (num_values) {
3310
		property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3311
		if (!property->values)
3312
			goto fail;
3313
	}
3314
 
3031 serge 3315
	ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3316
	if (ret)
3317
		goto fail;
3318
 
1123 serge 3319
	property->flags = flags;
3320
	property->num_values = num_values;
3321
	INIT_LIST_HEAD(&property->enum_blob_list);
3322
 
3031 serge 3323
	if (name) {
1123 serge 3324
		strncpy(property->name, name, DRM_PROP_NAME_LEN);
3031 serge 3325
		property->name[DRM_PROP_NAME_LEN-1] = '\0';
3326
	}
1123 serge 3327
 
3328
	list_add_tail(&property->head, &dev->mode_config.property_list);
5060 serge 3329
 
3330
	WARN_ON(!drm_property_type_valid(property));
3331
 
1123 serge 3332
	return property;
3333
fail:
3031 serge 3334
	kfree(property->values);
1123 serge 3335
	kfree(property);
3336
	return NULL;
3337
}
3338
EXPORT_SYMBOL(drm_property_create);
3339
 
5060 serge 3340
/**
3341
 * drm_property_create_enum - create a new enumeration property type
3342
 * @dev: drm device
3343
 * @flags: flags specifying the property type
3344
 * @name: name of the property
3345
 * @props: enumeration lists with property values
3346
 * @num_values: number of pre-defined values
3347
 *
3348
 * This creates a new generic drm property which can then be attached to a drm
3349
 * object with drm_object_attach_property. The returned property object must be
3350
 * freed with drm_property_destroy.
3351
 *
3352
 * Userspace is only allowed to set one of the predefined values for enumeration
3353
 * properties.
3354
 *
3355
 * Returns:
3356
 * A pointer to the newly created property on success, NULL on failure.
3357
 */
3031 serge 3358
struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3359
					 const char *name,
3360
					 const struct drm_prop_enum_list *props,
3361
					 int num_values)
3362
{
3363
	struct drm_property *property;
3364
	int i, ret;
3365
 
3366
	flags |= DRM_MODE_PROP_ENUM;
3367
 
3368
	property = drm_property_create(dev, flags, name, num_values);
3369
	if (!property)
3370
		return NULL;
3371
 
3372
	for (i = 0; i < num_values; i++) {
3373
		ret = drm_property_add_enum(property, i,
3374
				      props[i].type,
3375
				      props[i].name);
3376
		if (ret) {
3377
			drm_property_destroy(dev, property);
3378
			return NULL;
3379
		}
3380
	}
3381
 
3382
	return property;
3383
}
3384
EXPORT_SYMBOL(drm_property_create_enum);
3385
 
5060 serge 3386
/**
3387
 * drm_property_create_bitmask - create a new bitmask property type
3388
 * @dev: drm device
3389
 * @flags: flags specifying the property type
3390
 * @name: name of the property
3391
 * @props: enumeration lists with property bitflags
3392
 * @num_values: number of pre-defined values
3393
 *
3394
 * This creates a new generic drm property which can then be attached to a drm
3395
 * object with drm_object_attach_property. The returned property object must be
3396
 * freed with drm_property_destroy.
3397
 *
3398
 * Compared to plain enumeration properties userspace is allowed to set any
3399
 * or'ed together combination of the predefined property bitflag values
3400
 *
3401
 * Returns:
3402
 * A pointer to the newly created property on success, NULL on failure.
3403
 */
3031 serge 3404
struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3405
					 int flags, const char *name,
3406
					 const struct drm_prop_enum_list *props,
5060 serge 3407
					 int num_props,
3408
					 uint64_t supported_bits)
3031 serge 3409
{
3410
	struct drm_property *property;
5060 serge 3411
	int i, ret, index = 0;
3412
	int num_values = hweight64(supported_bits);
3031 serge 3413
 
3414
	flags |= DRM_MODE_PROP_BITMASK;
3415
 
3416
	property = drm_property_create(dev, flags, name, num_values);
3417
	if (!property)
3418
		return NULL;
5060 serge 3419
	for (i = 0; i < num_props; i++) {
3420
		if (!(supported_bits & (1ULL << props[i].type)))
3421
			continue;
3031 serge 3422
 
5060 serge 3423
		if (WARN_ON(index >= num_values)) {
3424
			drm_property_destroy(dev, property);
3425
			return NULL;
3426
		}
3427
 
3428
		ret = drm_property_add_enum(property, index++,
3031 serge 3429
				      props[i].type,
3430
				      props[i].name);
3431
		if (ret) {
3432
			drm_property_destroy(dev, property);
3433
			return NULL;
3434
		}
3435
	}
3436
 
3437
	return property;
3438
}
3439
EXPORT_SYMBOL(drm_property_create_bitmask);
3440
 
5060 serge 3441
static struct drm_property *property_create_range(struct drm_device *dev,
3442
					 int flags, const char *name,
3031 serge 3443
					 uint64_t min, uint64_t max)
3444
{
3445
	struct drm_property *property;
3446
 
3447
	property = drm_property_create(dev, flags, name, 2);
3448
	if (!property)
3449
		return NULL;
3450
 
3451
	property->values[0] = min;
3452
	property->values[1] = max;
3453
 
3454
	return property;
3455
}
5060 serge 3456
 
3457
/**
3458
 * drm_property_create_range - create a new ranged property type
3459
 * @dev: drm device
3460
 * @flags: flags specifying the property type
3461
 * @name: name of the property
3462
 * @min: minimum value of the property
3463
 * @max: maximum value of the property
3464
 *
3465
 * This creates a new generic drm property which can then be attached to a drm
3466
 * object with drm_object_attach_property. The returned property object must be
3467
 * freed with drm_property_destroy.
3468
 *
3469
 * Userspace is allowed to set any interger value in the (min, max) range
3470
 * inclusive.
3471
 *
3472
 * Returns:
3473
 * A pointer to the newly created property on success, NULL on failure.
3474
 */
3475
struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3476
					 const char *name,
3477
					 uint64_t min, uint64_t max)
3478
{
3479
	return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3480
			name, min, max);
3481
}
3031 serge 3482
EXPORT_SYMBOL(drm_property_create_range);
3483
 
5060 serge 3484
struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3485
					 int flags, const char *name,
3486
					 int64_t min, int64_t max)
3487
{
3488
	return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3489
			name, I642U64(min), I642U64(max));
3490
}
3491
EXPORT_SYMBOL(drm_property_create_signed_range);
3492
 
3493
struct drm_property *drm_property_create_object(struct drm_device *dev,
3494
					 int flags, const char *name, uint32_t type)
3495
{
3496
	struct drm_property *property;
3497
 
3498
	flags |= DRM_MODE_PROP_OBJECT;
3499
 
3500
	property = drm_property_create(dev, flags, name, 1);
3501
	if (!property)
3502
		return NULL;
3503
 
3504
	property->values[0] = type;
3505
 
3506
	return property;
3507
}
3508
EXPORT_SYMBOL(drm_property_create_object);
3509
 
3510
/**
3511
 * drm_property_add_enum - add a possible value to an enumeration property
3512
 * @property: enumeration property to change
3513
 * @index: index of the new enumeration
3514
 * @value: value of the new enumeration
3515
 * @name: symbolic name of the new enumeration
3516
 *
3517
 * This functions adds enumerations to a property.
3518
 *
3519
 * It's use is deprecated, drivers should use one of the more specific helpers
3520
 * to directly create the property with all enumerations already attached.
3521
 *
3522
 * Returns:
3523
 * Zero on success, error code on failure.
3524
 */
1123 serge 3525
int drm_property_add_enum(struct drm_property *property, int index,
3526
			  uint64_t value, const char *name)
3527
{
3528
	struct drm_property_enum *prop_enum;
3529
 
5060 serge 3530
	if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3531
			drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
1123 serge 3532
		return -EINVAL;
3533
 
3031 serge 3534
	/*
3535
	 * Bitmask enum properties have the additional constraint of values
3536
	 * from 0 to 63
3537
	 */
5060 serge 3538
	if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3539
			(value > 63))
3031 serge 3540
		return -EINVAL;
3541
 
1123 serge 3542
	if (!list_empty(&property->enum_blob_list)) {
3543
		list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3544
			if (prop_enum->value == value) {
3545
				strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3546
				prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3547
				return 0;
3548
			}
3549
		}
3550
	}
3551
 
3552
	prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3553
	if (!prop_enum)
3554
		return -ENOMEM;
3555
 
3556
	strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3557
	prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3558
	prop_enum->value = value;
3559
 
3560
	property->values[index] = value;
3561
	list_add_tail(&prop_enum->head, &property->enum_blob_list);
3562
	return 0;
3563
}
3564
EXPORT_SYMBOL(drm_property_add_enum);
3565
 
5060 serge 3566
/**
3567
 * drm_property_destroy - destroy a drm property
3568
 * @dev: drm device
3569
 * @property: property to destry
3570
 *
3571
 * This function frees a property including any attached resources like
3572
 * enumeration values.
3573
 */
1123 serge 3574
void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3575
{
3576
	struct drm_property_enum *prop_enum, *pt;
3577
 
3578
	list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3579
		list_del(&prop_enum->head);
3580
		kfree(prop_enum);
3581
	}
3582
 
3583
	if (property->num_values)
3584
		kfree(property->values);
3585
	drm_mode_object_put(dev, &property->base);
3586
	list_del(&property->head);
3587
	kfree(property);
3588
}
3589
EXPORT_SYMBOL(drm_property_destroy);
3590
 
5060 serge 3591
/**
3592
 * drm_object_attach_property - attach a property to a modeset object
3593
 * @obj: drm modeset object
3594
 * @property: property to attach
3595
 * @init_val: initial value of the property
3596
 *
3597
 * This attaches the given property to the modeset object with the given initial
3598
 * value. Currently this function cannot fail since the properties are stored in
3599
 * a statically sized array.
3600
 */
3031 serge 3601
void drm_object_attach_property(struct drm_mode_object *obj,
3602
				struct drm_property *property,
3603
				uint64_t init_val)
3604
{
3605
	int count = obj->properties->count;
3606
 
3607
	if (count == DRM_OBJECT_MAX_PROPERTY) {
3608
		WARN(1, "Failed to attach object property (type: 0x%x). Please "
3609
			"increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3610
			"you see this message on the same object type.\n",
3611
			obj->type);
3612
		return;
3613
	}
3614
 
3615
	obj->properties->ids[count] = property->base.id;
3616
	obj->properties->values[count] = init_val;
3617
	obj->properties->count++;
3618
}
3619
EXPORT_SYMBOL(drm_object_attach_property);
3620
 
5060 serge 3621
/**
3622
 * drm_object_property_set_value - set the value of a property
3623
 * @obj: drm mode object to set property value for
3624
 * @property: property to set
3625
 * @val: value the property should be set to
3626
 *
3627
 * This functions sets a given property on a given object. This function only
3628
 * changes the software state of the property, it does not call into the
3629
 * driver's ->set_property callback.
3630
 *
3631
 * Returns:
3632
 * Zero on success, error code on failure.
3633
 */
3031 serge 3634
int drm_object_property_set_value(struct drm_mode_object *obj,
3635
				  struct drm_property *property, uint64_t val)
3636
{
1123 serge 3637
	int i;
3638
 
3031 serge 3639
	for (i = 0; i < obj->properties->count; i++) {
3640
		if (obj->properties->ids[i] == property->base.id) {
3641
			obj->properties->values[i] = val;
3642
			return 0;
1123 serge 3643
		}
3644
	}
3645
 
3646
		return -EINVAL;
3647
}
3031 serge 3648
EXPORT_SYMBOL(drm_object_property_set_value);
1123 serge 3649
 
5060 serge 3650
/**
3651
 * drm_object_property_get_value - retrieve the value of a property
3652
 * @obj: drm mode object to get property value from
3653
 * @property: property to retrieve
3654
 * @val: storage for the property value
3655
 *
3656
 * This function retrieves the softare state of the given property for the given
3657
 * property. Since there is no driver callback to retrieve the current property
3658
 * value this might be out of sync with the hardware, depending upon the driver
3659
 * and property.
3660
 *
3661
 * Returns:
3662
 * Zero on success, error code on failure.
3663
 */
3031 serge 3664
int drm_object_property_get_value(struct drm_mode_object *obj,
1123 serge 3665
				  struct drm_property *property, uint64_t *val)
3666
{
3667
	int i;
3668
 
3031 serge 3669
	for (i = 0; i < obj->properties->count; i++) {
3670
		if (obj->properties->ids[i] == property->base.id) {
3671
			*val = obj->properties->values[i];
3672
			return 0;
1123 serge 3673
		}
3674
	}
3675
 
3676
		return -EINVAL;
3677
}
3031 serge 3678
EXPORT_SYMBOL(drm_object_property_get_value);
1123 serge 3679
 
3680
#if 0
5060 serge 3681
/**
3682
 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3683
 * @dev: DRM device
3684
 * @data: ioctl data
3685
 * @file_priv: DRM file info
3686
 *
3687
 * This function retrieves the current value for an connectors's property.
3688
 *
3689
 * Called by the user via ioctl.
3690
 *
3691
 * Returns:
3692
 * Zero on success, errno on failure.
3693
 */
1123 serge 3694
int drm_mode_getproperty_ioctl(struct drm_device *dev,
3695
			       void *data, struct drm_file *file_priv)
3696
{
3697
	struct drm_mode_get_property *out_resp = data;
3698
	struct drm_property *property;
3699
	int enum_count = 0;
3700
	int blob_count = 0;
3701
	int value_count = 0;
3702
	int ret = 0, i;
3703
	int copied;
3704
	struct drm_property_enum *prop_enum;
3705
	struct drm_mode_property_enum __user *enum_ptr;
3706
	struct drm_property_blob *prop_blob;
3031 serge 3707
	uint32_t __user *blob_id_ptr;
1123 serge 3708
	uint64_t __user *values_ptr;
3709
	uint32_t __user *blob_length_ptr;
3710
 
1963 serge 3711
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3712
		return -EINVAL;
3713
 
3480 Serge 3714
	drm_modeset_lock_all(dev);
5060 serge 3715
	property = drm_property_find(dev, out_resp->prop_id);
3716
	if (!property) {
4560 Serge 3717
		ret = -ENOENT;
1123 serge 3718
		goto done;
3719
	}
3720
 
5060 serge 3721
	if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3722
			drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
1123 serge 3723
		list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3724
			enum_count++;
5060 serge 3725
	} else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
1123 serge 3726
		list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3727
			blob_count++;
3728
	}
3729
 
3730
	value_count = property->num_values;
3731
 
3732
	strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3733
	out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3734
	out_resp->flags = property->flags;
3735
 
3736
	if ((out_resp->count_values >= value_count) && value_count) {
3031 serge 3737
		values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
1123 serge 3738
		for (i = 0; i < value_count; i++) {
3739
			if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3740
				ret = -EFAULT;
3741
				goto done;
3742
			}
3743
		}
3744
	}
3745
	out_resp->count_values = value_count;
3746
 
5060 serge 3747
	if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3748
			drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
1123 serge 3749
		if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3750
			copied = 0;
3031 serge 3751
			enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
1123 serge 3752
			list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3753
 
3754
				if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3755
					ret = -EFAULT;
3756
					goto done;
3757
				}
3758
 
3759
				if (copy_to_user(&enum_ptr[copied].name,
3760
						 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3761
					ret = -EFAULT;
3762
					goto done;
3763
				}
3764
				copied++;
3765
			}
3766
		}
3767
		out_resp->count_enum_blobs = enum_count;
3768
	}
3769
 
5060 serge 3770
	if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
1123 serge 3771
		if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3772
			copied = 0;
3031 serge 3773
			blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3774
			blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
1123 serge 3775
 
3776
			list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3777
				if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3778
					ret = -EFAULT;
3779
					goto done;
3780
				}
3781
 
3782
				if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3783
					ret = -EFAULT;
3784
					goto done;
3785
				}
3786
 
3787
				copied++;
3788
			}
3789
		}
3790
		out_resp->count_enum_blobs = blob_count;
3791
	}
3792
done:
3480 Serge 3793
	drm_modeset_unlock_all(dev);
1123 serge 3794
	return ret;
3795
}
3796
#endif
3797
 
3798
static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3799
							  void *data)
3800
{
3801
	struct drm_property_blob *blob;
3031 serge 3802
	int ret;
1123 serge 3803
 
3804
	if (!length || !data)
3805
		return NULL;
3806
 
3807
	blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3808
	if (!blob)
3809
		return NULL;
3810
 
3031 serge 3811
	ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3812
	if (ret) {
3813
		kfree(blob);
3814
		return NULL;
3815
	}
3816
 
1123 serge 3817
	blob->length = length;
3818
 
3819
	memcpy(blob->data, data, length);
3820
 
3821
	list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3822
	return blob;
3823
}
3824
 
3825
static void drm_property_destroy_blob(struct drm_device *dev,
3826
			       struct drm_property_blob *blob)
3827
{
3828
	drm_mode_object_put(dev, &blob->base);
3829
	list_del(&blob->head);
3830
	kfree(blob);
3831
}
3832
 
3833
#if 0
5060 serge 3834
/**
3835
 * drm_mode_getblob_ioctl - get the contents of a blob property value
3836
 * @dev: DRM device
3837
 * @data: ioctl data
3838
 * @file_priv: DRM file info
3839
 *
3840
 * This function retrieves the contents of a blob property. The value stored in
3841
 * an object's blob property is just a normal modeset object id.
3842
 *
3843
 * Called by the user via ioctl.
3844
 *
3845
 * Returns:
3846
 * Zero on success, errno on failure.
3847
 */
1123 serge 3848
int drm_mode_getblob_ioctl(struct drm_device *dev,
3849
			   void *data, struct drm_file *file_priv)
3850
{
3851
	struct drm_mode_get_blob *out_resp = data;
3852
	struct drm_property_blob *blob;
3853
	int ret = 0;
3031 serge 3854
	void __user *blob_ptr;
1123 serge 3855
 
1963 serge 3856
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3857
		return -EINVAL;
3858
 
3480 Serge 3859
	drm_modeset_lock_all(dev);
5060 serge 3860
	blob = drm_property_blob_find(dev, out_resp->blob_id);
3861
	if (!blob) {
4560 Serge 3862
		ret = -ENOENT;
1123 serge 3863
		goto done;
3864
	}
3865
 
3866
	if (out_resp->length == blob->length) {
3031 serge 3867
		blob_ptr = (void __user *)(unsigned long)out_resp->data;
1123 serge 3868
		if (copy_to_user(blob_ptr, blob->data, blob->length)){
3869
			ret = -EFAULT;
3870
			goto done;
3871
		}
3872
	}
3873
	out_resp->length = blob->length;
3874
 
3875
done:
3480 Serge 3876
	drm_modeset_unlock_all(dev);
1123 serge 3877
	return ret;
3878
}
3879
#endif
3880
 
5060 serge 3881
int drm_mode_connector_set_path_property(struct drm_connector *connector,
3882
					 char *path)
3883
{
3884
	struct drm_device *dev = connector->dev;
3885
	int ret, size;
3886
	size = strlen(path) + 1;
3887
 
3888
	connector->path_blob_ptr = drm_property_create_blob(connector->dev,
3889
							    size, path);
3890
	if (!connector->path_blob_ptr)
3891
		return -EINVAL;
3892
 
3893
	ret = drm_object_property_set_value(&connector->base,
3894
					    dev->mode_config.path_property,
3895
					    connector->path_blob_ptr->base.id);
3896
	return ret;
3897
}
3898
EXPORT_SYMBOL(drm_mode_connector_set_path_property);
3899
 
3900
/**
3901
 * drm_mode_connector_update_edid_property - update the edid property of a connector
3902
 * @connector: drm connector
3903
 * @edid: new value of the edid property
3904
 *
3905
 * This function creates a new blob modeset object and assigns its id to the
3906
 * connector's edid property.
3907
 *
3908
 * Returns:
3909
 * Zero on success, errno on failure.
3910
 */
1123 serge 3911
int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3912
					    struct edid *edid)
3913
{
3914
	struct drm_device *dev = connector->dev;
3031 serge 3915
	int ret, size;
1123 serge 3916
 
5060 serge 3917
	/* ignore requests to set edid when overridden */
3918
	if (connector->override_edid)
3919
		return 0;
3920
 
1123 serge 3921
	if (connector->edid_blob_ptr)
3922
		drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3923
 
3924
	/* Delete edid, when there is none. */
3925
	if (!edid) {
3926
		connector->edid_blob_ptr = NULL;
3192 Serge 3927
		ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
1123 serge 3928
		return ret;
3929
	}
3930
 
1963 serge 3931
	size = EDID_LENGTH * (1 + edid->extensions);
3932
	connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3933
							    size, edid);
3192 Serge 3934
	if (!connector->edid_blob_ptr)
3935
		return -EINVAL;
1123 serge 3936
 
3192 Serge 3937
	ret = drm_object_property_set_value(&connector->base,
1123 serge 3938
					       dev->mode_config.edid_property,
3939
					       connector->edid_blob_ptr->base.id);
3940
 
3941
	return ret;
3942
}
3943
EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3944
 
3945
#if 0
3031 serge 3946
 
3947
static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3948
					   struct drm_property *property,
3949
					   uint64_t value)
1123 serge 3950
{
3031 serge 3951
	int ret = -EINVAL;
3952
	struct drm_connector *connector = obj_to_connector(obj);
3953
 
3954
	/* Do DPMS ourselves */
3955
	if (property == connector->dev->mode_config.dpms_property) {
3956
		if (connector->funcs->dpms)
3957
			(*connector->funcs->dpms)(connector, (int)value);
3958
		ret = 0;
3959
	} else if (connector->funcs->set_property)
3960
		ret = connector->funcs->set_property(connector, property, value);
3961
 
3962
	/* store the property value if successful */
3963
	if (!ret)
3192 Serge 3964
		drm_object_property_set_value(&connector->base, property, value);
3031 serge 3965
	return ret;
3966
}
3967
 
3968
static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3969
				      struct drm_property *property,
3970
				      uint64_t value)
3971
{
3972
	int ret = -EINVAL;
3973
	struct drm_crtc *crtc = obj_to_crtc(obj);
3974
 
3975
	if (crtc->funcs->set_property)
3976
		ret = crtc->funcs->set_property(crtc, property, value);
3977
	if (!ret)
3978
		drm_object_property_set_value(obj, property, value);
3979
 
3980
	return ret;
3981
}
3982
 
3983
static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3984
				      struct drm_property *property,
3985
				      uint64_t value)
3986
{
3987
	int ret = -EINVAL;
3988
	struct drm_plane *plane = obj_to_plane(obj);
3989
 
3990
	if (plane->funcs->set_property)
3991
		ret = plane->funcs->set_property(plane, property, value);
3992
	if (!ret)
3993
		drm_object_property_set_value(obj, property, value);
3994
 
3995
	return ret;
3996
}
3997
 
5060 serge 3998
/**
3999
 * drm_mode_getproperty_ioctl - get the current value of a object's property
4000
 * @dev: DRM device
4001
 * @data: ioctl data
4002
 * @file_priv: DRM file info
4003
 *
4004
 * This function retrieves the current value for an object's property. Compared
4005
 * to the connector specific ioctl this one is extended to also work on crtc and
4006
 * plane objects.
4007
 *
4008
 * Called by the user via ioctl.
4009
 *
4010
 * Returns:
4011
 * Zero on success, errno on failure.
4012
 */
3031 serge 4013
int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4014
				      struct drm_file *file_priv)
4015
{
4016
	struct drm_mode_obj_get_properties *arg = data;
1123 serge 4017
	struct drm_mode_object *obj;
3031 serge 4018
	int ret = 0;
1123 serge 4019
	int i;
3031 serge 4020
	int copied = 0;
4021
	int props_count = 0;
4022
	uint32_t __user *props_ptr;
4023
	uint64_t __user *prop_values_ptr;
1123 serge 4024
 
1963 serge 4025
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
4026
		return -EINVAL;
4027
 
3480 Serge 4028
	drm_modeset_lock_all(dev);
1123 serge 4029
 
3031 serge 4030
	obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
1123 serge 4031
	if (!obj) {
4560 Serge 4032
		ret = -ENOENT;
1123 serge 4033
		goto out;
4034
	}
3031 serge 4035
	if (!obj->properties) {
4036
		ret = -EINVAL;
4037
		goto out;
4038
	}
1123 serge 4039
 
3031 serge 4040
	props_count = obj->properties->count;
4041
 
4042
	/* This ioctl is called twice, once to determine how much space is
4043
	 * needed, and the 2nd time to fill it. */
4044
	if ((arg->count_props >= props_count) && props_count) {
4045
		copied = 0;
4046
		props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4047
		prop_values_ptr = (uint64_t __user *)(unsigned long)
4048
				  (arg->prop_values_ptr);
4049
		for (i = 0; i < props_count; i++) {
4050
			if (put_user(obj->properties->ids[i],
4051
				     props_ptr + copied)) {
4052
				ret = -EFAULT;
4053
				goto out;
1123 serge 4054
	}
3031 serge 4055
			if (put_user(obj->properties->values[i],
4056
				     prop_values_ptr + copied)) {
4057
				ret = -EFAULT;
1123 serge 4058
		goto out;
4059
	}
3031 serge 4060
			copied++;
4061
		}
4062
	}
4063
	arg->count_props = props_count;
4064
out:
3480 Serge 4065
	drm_modeset_unlock_all(dev);
3031 serge 4066
	return ret;
4067
}
1123 serge 4068
 
5060 serge 4069
/**
4070
 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4071
 * @dev: DRM device
4072
 * @data: ioctl data
4073
 * @file_priv: DRM file info
4074
 *
4075
 * This function sets the current value for an object's property. It also calls
4076
 * into a driver's ->set_property callback to update the hardware state.
4077
 * Compared to the connector specific ioctl this one is extended to also work on
4078
 * crtc and plane objects.
4079
 *
4080
 * Called by the user via ioctl.
4081
 *
4082
 * Returns:
4083
 * Zero on success, errno on failure.
4084
 */
3031 serge 4085
int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4086
				    struct drm_file *file_priv)
4087
{
4088
	struct drm_mode_obj_set_property *arg = data;
4089
	struct drm_mode_object *arg_obj;
4090
	struct drm_mode_object *prop_obj;
4091
	struct drm_property *property;
4092
	int ret = -EINVAL;
4093
	int i;
4094
 
4095
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
4096
		return -EINVAL;
4097
 
3480 Serge 4098
	drm_modeset_lock_all(dev);
3031 serge 4099
 
4100
	arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4560 Serge 4101
	if (!arg_obj) {
4102
		ret = -ENOENT;
1123 serge 4103
		goto out;
4560 Serge 4104
	}
3031 serge 4105
	if (!arg_obj->properties)
4106
		goto out;
1123 serge 4107
 
3031 serge 4108
	for (i = 0; i < arg_obj->properties->count; i++)
4109
		if (arg_obj->properties->ids[i] == arg->prop_id)
4110
			break;
4111
 
4112
	if (i == arg_obj->properties->count)
1123 serge 4113
		goto out;
4114
 
3031 serge 4115
	prop_obj = drm_mode_object_find(dev, arg->prop_id,
4116
					DRM_MODE_OBJECT_PROPERTY);
4560 Serge 4117
	if (!prop_obj) {
4118
		ret = -ENOENT;
1123 serge 4119
			goto out;
4560 Serge 4120
	}
3031 serge 4121
	property = obj_to_property(prop_obj);
1123 serge 4122
 
3031 serge 4123
	if (!drm_property_change_is_valid(property, arg->value))
1123 serge 4124
			goto out;
3031 serge 4125
 
4126
	switch (arg_obj->type) {
4127
	case DRM_MODE_OBJECT_CONNECTOR:
4128
		ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4129
						      arg->value);
4130
		break;
4131
	case DRM_MODE_OBJECT_CRTC:
4132
		ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4133
		break;
4134
	case DRM_MODE_OBJECT_PLANE:
4135
		ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
1123 serge 4136
				break;
4137
			}
4138
 
4139
out:
3480 Serge 4140
	drm_modeset_unlock_all(dev);
1123 serge 4141
	return ret;
4142
}
4143
#endif
4144
 
5060 serge 4145
/**
4146
 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4147
 * @connector: connector to attach
4148
 * @encoder: encoder to attach @connector to
4149
 *
4150
 * This function links up a connector to an encoder. Note that the routing
4151
 * restrictions between encoders and crtcs are exposed to userspace through the
4152
 * possible_clones and possible_crtcs bitmasks.
4153
 *
4154
 * Returns:
4155
 * Zero on success, errno on failure.
4156
 */
1123 serge 4157
int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4158
				      struct drm_encoder *encoder)
4159
{
4160
	int i;
4161
 
4162
	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4163
		if (connector->encoder_ids[i] == 0) {
4164
			connector->encoder_ids[i] = encoder->base.id;
4165
			return 0;
4166
		}
4167
	}
4168
	return -ENOMEM;
4169
}
4170
EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4171
 
5060 serge 4172
/**
4173
 * drm_mode_crtc_set_gamma_size - set the gamma table size
4174
 * @crtc: CRTC to set the gamma table size for
4175
 * @gamma_size: size of the gamma table
4176
 *
4177
 * Drivers which support gamma tables should set this to the supported gamma
4178
 * table size when initializing the CRTC. Currently the drm core only supports a
4179
 * fixed gamma table size.
4180
 *
4181
 * Returns:
4182
 * Zero on success, errno on failure.
4183
 */
3031 serge 4184
int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
1123 serge 4185
				  int gamma_size)
4186
{
4187
	crtc->gamma_size = gamma_size;
4188
 
4189
	crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4190
	if (!crtc->gamma_store) {
4191
		crtc->gamma_size = 0;
3031 serge 4192
		return -ENOMEM;
1123 serge 4193
	}
4194
 
3031 serge 4195
	return 0;
1123 serge 4196
}
4197
EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4198
 
4199
#if 0
5060 serge 4200
/**
4201
 * drm_mode_gamma_set_ioctl - set the gamma table
4202
 * @dev: DRM device
4203
 * @data: ioctl data
4204
 * @file_priv: DRM file info
4205
 *
4206
 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4207
 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4208
 *
4209
 * Called by the user via ioctl.
4210
 *
4211
 * Returns:
4212
 * Zero on success, errno on failure.
4213
 */
1123 serge 4214
int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4215
			     void *data, struct drm_file *file_priv)
4216
{
4217
	struct drm_mode_crtc_lut *crtc_lut = data;
4218
	struct drm_crtc *crtc;
4219
	void *r_base, *g_base, *b_base;
4220
	int size;
4221
	int ret = 0;
4222
 
1963 serge 4223
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
4224
		return -EINVAL;
4225
 
3480 Serge 4226
	drm_modeset_lock_all(dev);
5060 serge 4227
	crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4228
	if (!crtc) {
4560 Serge 4229
		ret = -ENOENT;
1123 serge 4230
		goto out;
4231
	}
4232
 
3031 serge 4233
	if (crtc->funcs->gamma_set == NULL) {
4234
		ret = -ENOSYS;
4235
		goto out;
4236
	}
4237
 
1123 serge 4238
	/* memcpy into gamma store */
4239
	if (crtc_lut->gamma_size != crtc->gamma_size) {
4240
		ret = -EINVAL;
4241
		goto out;
4242
	}
4243
 
4244
	size = crtc_lut->gamma_size * (sizeof(uint16_t));
4245
	r_base = crtc->gamma_store;
4246
	if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4247
		ret = -EFAULT;
4248
		goto out;
4249
	}
4250
 
4251
	g_base = r_base + size;
4252
	if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4253
		ret = -EFAULT;
4254
		goto out;
4255
	}
4256
 
4257
	b_base = g_base + size;
4258
	if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4259
		ret = -EFAULT;
4260
		goto out;
4261
	}
4262
 
1963 serge 4263
	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
1123 serge 4264
 
4265
out:
3480 Serge 4266
	drm_modeset_unlock_all(dev);
1123 serge 4267
	return ret;
4268
 
4269
}
4270
 
5060 serge 4271
/**
4272
 * drm_mode_gamma_get_ioctl - get the gamma table
4273
 * @dev: DRM device
4274
 * @data: ioctl data
4275
 * @file_priv: DRM file info
4276
 *
4277
 * Copy the current gamma table into the storage provided. This also provides
4278
 * the gamma table size the driver expects, which can be used to size the
4279
 * allocated storage.
4280
 *
4281
 * Called by the user via ioctl.
4282
 *
4283
 * Returns:
4284
 * Zero on success, errno on failure.
4285
 */
1123 serge 4286
int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4287
			     void *data, struct drm_file *file_priv)
4288
{
4289
	struct drm_mode_crtc_lut *crtc_lut = data;
4290
	struct drm_crtc *crtc;
4291
	void *r_base, *g_base, *b_base;
4292
	int size;
4293
	int ret = 0;
4294
 
1963 serge 4295
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
4296
		return -EINVAL;
4297
 
3480 Serge 4298
	drm_modeset_lock_all(dev);
5060 serge 4299
	crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4300
	if (!crtc) {
4560 Serge 4301
		ret = -ENOENT;
1123 serge 4302
		goto out;
4303
	}
4304
 
4305
	/* memcpy into gamma store */
4306
	if (crtc_lut->gamma_size != crtc->gamma_size) {
4307
		ret = -EINVAL;
4308
		goto out;
4309
	}
4310
 
4311
	size = crtc_lut->gamma_size * (sizeof(uint16_t));
4312
	r_base = crtc->gamma_store;
4313
	if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4314
		ret = -EFAULT;
4315
		goto out;
4316
	}
4317
 
4318
	g_base = r_base + size;
4319
	if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4320
		ret = -EFAULT;
4321
		goto out;
4322
	}
4323
 
4324
	b_base = g_base + size;
4325
	if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4326
		ret = -EFAULT;
4327
		goto out;
4328
	}
4329
out:
3480 Serge 4330
	drm_modeset_unlock_all(dev);
1123 serge 4331
	return ret;
4332
}
4333
 
4334
#endif
1179 serge 4335
 
4336
 
5060 serge 4337
/**
4338
 * drm_mode_config_reset - call ->reset callbacks
4339
 * @dev: drm device
4340
 *
4341
 * This functions calls all the crtc's, encoder's and connector's ->reset
4342
 * callback. Drivers can use this in e.g. their driver load or resume code to
4343
 * reset hardware and software state.
4344
 */
1963 serge 4345
void drm_mode_config_reset(struct drm_device *dev)
4346
{
4347
	struct drm_crtc *crtc;
4348
	struct drm_encoder *encoder;
4349
	struct drm_connector *connector;
4350
 
4351
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4352
		if (crtc->funcs->reset)
4353
			crtc->funcs->reset(crtc);
4354
 
4355
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4356
		if (encoder->funcs->reset)
4357
			encoder->funcs->reset(encoder);
4358
 
3192 Serge 4359
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4360
		connector->status = connector_status_unknown;
4361
 
1963 serge 4362
		if (connector->funcs->reset)
4363
			connector->funcs->reset(connector);
3192 Serge 4364
	}
1963 serge 4365
}
4366
EXPORT_SYMBOL(drm_mode_config_reset);
3031 serge 4367
/*
4368
 * Just need to support RGB formats here for compat with code that doesn't
4369
 * use pixel formats directly yet.
4370
 */
4371
void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4372
			  int *bpp)
4373
{
4374
	switch (format) {
3480 Serge 4375
	case DRM_FORMAT_C8:
3031 serge 4376
	case DRM_FORMAT_RGB332:
4377
	case DRM_FORMAT_BGR233:
4378
		*depth = 8;
4379
		*bpp = 8;
4380
		break;
4381
	case DRM_FORMAT_XRGB1555:
4382
	case DRM_FORMAT_XBGR1555:
4383
	case DRM_FORMAT_RGBX5551:
4384
	case DRM_FORMAT_BGRX5551:
4385
	case DRM_FORMAT_ARGB1555:
4386
	case DRM_FORMAT_ABGR1555:
4387
	case DRM_FORMAT_RGBA5551:
4388
	case DRM_FORMAT_BGRA5551:
4389
		*depth = 15;
4390
		*bpp = 16;
4391
		break;
4392
	case DRM_FORMAT_RGB565:
4393
	case DRM_FORMAT_BGR565:
4394
		*depth = 16;
4395
		*bpp = 16;
4396
		break;
4397
	case DRM_FORMAT_RGB888:
4398
	case DRM_FORMAT_BGR888:
4399
		*depth = 24;
4400
		*bpp = 24;
4401
		break;
4402
	case DRM_FORMAT_XRGB8888:
4403
	case DRM_FORMAT_XBGR8888:
4404
	case DRM_FORMAT_RGBX8888:
4405
	case DRM_FORMAT_BGRX8888:
4406
		*depth = 24;
4407
		*bpp = 32;
4408
		break;
4409
	case DRM_FORMAT_XRGB2101010:
4410
	case DRM_FORMAT_XBGR2101010:
4411
	case DRM_FORMAT_RGBX1010102:
4412
	case DRM_FORMAT_BGRX1010102:
4413
	case DRM_FORMAT_ARGB2101010:
4414
	case DRM_FORMAT_ABGR2101010:
4415
	case DRM_FORMAT_RGBA1010102:
4416
	case DRM_FORMAT_BGRA1010102:
4417
		*depth = 30;
4418
		*bpp = 32;
4419
		break;
4420
	case DRM_FORMAT_ARGB8888:
4421
	case DRM_FORMAT_ABGR8888:
4422
	case DRM_FORMAT_RGBA8888:
4423
	case DRM_FORMAT_BGRA8888:
4424
		*depth = 32;
4425
		*bpp = 32;
4426
		break;
4427
	default:
4560 Serge 4428
		DRM_DEBUG_KMS("unsupported pixel format %s\n",
4429
			      drm_get_format_name(format));
3031 serge 4430
		*depth = 0;
4431
		*bpp = 0;
4432
		break;
4433
	}
4434
}
4435
EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4436
 
4437
/**
4438
 * drm_format_num_planes - get the number of planes for format
4439
 * @format: pixel format (DRM_FORMAT_*)
4440
 *
5060 serge 4441
 * Returns:
3031 serge 4442
 * The number of planes used by the specified pixel format.
4443
 */
4444
int drm_format_num_planes(uint32_t format)
4445
{
4446
	switch (format) {
4447
	case DRM_FORMAT_YUV410:
4448
	case DRM_FORMAT_YVU410:
4449
	case DRM_FORMAT_YUV411:
4450
	case DRM_FORMAT_YVU411:
4451
	case DRM_FORMAT_YUV420:
4452
	case DRM_FORMAT_YVU420:
4453
	case DRM_FORMAT_YUV422:
4454
	case DRM_FORMAT_YVU422:
4455
	case DRM_FORMAT_YUV444:
4456
	case DRM_FORMAT_YVU444:
4457
		return 3;
4458
	case DRM_FORMAT_NV12:
4459
	case DRM_FORMAT_NV21:
4460
	case DRM_FORMAT_NV16:
4461
	case DRM_FORMAT_NV61:
4462
	case DRM_FORMAT_NV24:
4463
	case DRM_FORMAT_NV42:
4464
		return 2;
4465
	default:
4466
		return 1;
4467
	}
4468
}
4469
EXPORT_SYMBOL(drm_format_num_planes);
4470
 
4471
/**
4472
 * drm_format_plane_cpp - determine the bytes per pixel value
4473
 * @format: pixel format (DRM_FORMAT_*)
4474
 * @plane: plane index
4475
 *
5060 serge 4476
 * Returns:
3031 serge 4477
 * The bytes per pixel value for the specified plane.
4478
 */
4479
int drm_format_plane_cpp(uint32_t format, int plane)
4480
{
4481
	unsigned int depth;
4482
	int bpp;
4483
 
4484
	if (plane >= drm_format_num_planes(format))
4485
		return 0;
4486
 
4487
	switch (format) {
4488
	case DRM_FORMAT_YUYV:
4489
	case DRM_FORMAT_YVYU:
4490
	case DRM_FORMAT_UYVY:
4491
	case DRM_FORMAT_VYUY:
4492
		return 2;
4493
	case DRM_FORMAT_NV12:
4494
	case DRM_FORMAT_NV21:
4495
	case DRM_FORMAT_NV16:
4496
	case DRM_FORMAT_NV61:
4497
	case DRM_FORMAT_NV24:
4498
	case DRM_FORMAT_NV42:
4499
		return plane ? 2 : 1;
4500
	case DRM_FORMAT_YUV410:
4501
	case DRM_FORMAT_YVU410:
4502
	case DRM_FORMAT_YUV411:
4503
	case DRM_FORMAT_YVU411:
4504
	case DRM_FORMAT_YUV420:
4505
	case DRM_FORMAT_YVU420:
4506
	case DRM_FORMAT_YUV422:
4507
	case DRM_FORMAT_YVU422:
4508
	case DRM_FORMAT_YUV444:
4509
	case DRM_FORMAT_YVU444:
4510
		return 1;
4511
	default:
4512
		drm_fb_get_bpp_depth(format, &depth, &bpp);
4513
		return bpp >> 3;
4514
	}
4515
}
4516
EXPORT_SYMBOL(drm_format_plane_cpp);
4517
 
4518
/**
4519
 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4520
 * @format: pixel format (DRM_FORMAT_*)
4521
 *
5060 serge 4522
 * Returns:
3031 serge 4523
 * The horizontal chroma subsampling factor for the
4524
 * specified pixel format.
4525
 */
4526
int drm_format_horz_chroma_subsampling(uint32_t format)
4527
{
4528
	switch (format) {
4529
	case DRM_FORMAT_YUV411:
4530
	case DRM_FORMAT_YVU411:
4531
	case DRM_FORMAT_YUV410:
4532
	case DRM_FORMAT_YVU410:
4533
		return 4;
4534
	case DRM_FORMAT_YUYV:
4535
	case DRM_FORMAT_YVYU:
4536
	case DRM_FORMAT_UYVY:
4537
	case DRM_FORMAT_VYUY:
4538
	case DRM_FORMAT_NV12:
4539
	case DRM_FORMAT_NV21:
4540
	case DRM_FORMAT_NV16:
4541
	case DRM_FORMAT_NV61:
4542
	case DRM_FORMAT_YUV422:
4543
	case DRM_FORMAT_YVU422:
4544
	case DRM_FORMAT_YUV420:
4545
	case DRM_FORMAT_YVU420:
4546
		return 2;
4547
	default:
4548
		return 1;
4549
	}
4550
}
4551
EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4552
 
4553
/**
4554
 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4555
 * @format: pixel format (DRM_FORMAT_*)
4556
 *
5060 serge 4557
 * Returns:
3031 serge 4558
 * The vertical chroma subsampling factor for the
4559
 * specified pixel format.
4560
 */
4561
int drm_format_vert_chroma_subsampling(uint32_t format)
4562
{
4563
	switch (format) {
4564
	case DRM_FORMAT_YUV410:
4565
	case DRM_FORMAT_YVU410:
4566
		return 4;
4567
	case DRM_FORMAT_YUV420:
4568
	case DRM_FORMAT_YVU420:
4569
	case DRM_FORMAT_NV12:
4570
	case DRM_FORMAT_NV21:
4571
		return 2;
4572
	default:
4573
		return 1;
4574
	}
4575
}
4576
EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
3746 Serge 4577
 
4578
/**
4579
 * drm_mode_config_init - initialize DRM mode_configuration structure
4580
 * @dev: DRM device
4581
 *
4582
 * Initialize @dev's mode_config structure, used for tracking the graphics
4583
 * configuration of @dev.
4584
 *
4585
 * Since this initializes the modeset locks, no locking is possible. Which is no
4586
 * problem, since this should happen single threaded at init time. It is the
4587
 * driver's problem to ensure this guarantee.
4588
 *
4589
 */
4590
void drm_mode_config_init(struct drm_device *dev)
4591
{
4592
	mutex_init(&dev->mode_config.mutex);
5060 serge 4593
	drm_modeset_lock_init(&dev->mode_config.connection_mutex);
3746 Serge 4594
	mutex_init(&dev->mode_config.idr_mutex);
4595
	mutex_init(&dev->mode_config.fb_lock);
4596
	INIT_LIST_HEAD(&dev->mode_config.fb_list);
4597
	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4598
	INIT_LIST_HEAD(&dev->mode_config.connector_list);
4104 Serge 4599
	INIT_LIST_HEAD(&dev->mode_config.bridge_list);
3746 Serge 4600
	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4601
	INIT_LIST_HEAD(&dev->mode_config.property_list);
4602
	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4603
	INIT_LIST_HEAD(&dev->mode_config.plane_list);
4604
	idr_init(&dev->mode_config.crtc_idr);
4605
 
4606
	drm_modeset_lock_all(dev);
4607
	drm_mode_create_standard_connector_properties(dev);
5060 serge 4608
	drm_mode_create_standard_plane_properties(dev);
3746 Serge 4609
	drm_modeset_unlock_all(dev);
4610
 
4611
	/* Just to be sure */
4612
	dev->mode_config.num_fb = 0;
4613
	dev->mode_config.num_connector = 0;
4614
	dev->mode_config.num_crtc = 0;
4615
	dev->mode_config.num_encoder = 0;
5060 serge 4616
	dev->mode_config.num_overlay_plane = 0;
4617
	dev->mode_config.num_total_plane = 0;
3746 Serge 4618
}
4619
EXPORT_SYMBOL(drm_mode_config_init);
4620
 
5060 serge 4621
/**
4622
 * drm_mode_config_cleanup - free up DRM mode_config info
4623
 * @dev: DRM device
4624
 *
4625
 * Free up all the connectors and CRTCs associated with this DRM device, then
4626
 * free up the framebuffers and associated buffer objects.
4627
 *
4628
 * Note that since this /should/ happen single-threaded at driver/device
4629
 * teardown time, no locking is required. It's the driver's job to ensure that
4630
 * this guarantee actually holds true.
4631
 *
4632
 * FIXME: cleanup any dangling user buffer objects too
4633
 */