Subversion Repositories Kolibri OS

Rev

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