Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3031 serge 1
/*
2
 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
3
 *
4
 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
5
 * All Rights Reserved.
6
 *
5060 serge 7
 * Author Rickard E. (Rik) Faith 
8
 *
3031 serge 9
 * Permission is hereby granted, free of charge, to any person obtaining a
10
 * copy of this software and associated documentation files (the "Software"),
11
 * to deal in the Software without restriction, including without limitation
12
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13
 * and/or sell copies of the Software, and to permit persons to whom the
14
 * Software is furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice (including the next
17
 * paragraph) shall be included in all copies or substantial portions of the
18
 * Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26
 * DEALINGS IN THE SOFTWARE.
27
 */
28
 
5060 serge 29
#include 
3031 serge 30
#include 
4560 Serge 31
#include 
3031 serge 32
#include 
33
#include 
4104 Serge 34
#include 
3031 serge 35
 
36
unsigned int drm_debug = 0;	/* 1 to enable debug output */
37
EXPORT_SYMBOL(drm_debug);
38
 
4104 Serge 39
unsigned int drm_rnodes = 0;	/* 1 to enable experimental render nodes API */
40
EXPORT_SYMBOL(drm_rnodes);
41
 
5060 serge 42
/* 1 to allow user space to request universal planes (experimental) */
43
unsigned int drm_universal_planes = 0;
44
EXPORT_SYMBOL(drm_universal_planes);
45
 
3031 serge 46
unsigned int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
47
EXPORT_SYMBOL(drm_vblank_offdelay);
48
 
49
unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
50
EXPORT_SYMBOL(drm_timestamp_precision);
51
 
4104 Serge 52
struct idr drm_minors_idr;
5271 serge 53
 
54
void drm_err(const char *format, ...)
3031 serge 55
{
5271 serge 56
    struct va_format vaf;
57
    va_list args;
3031 serge 58
 
5271 serge 59
    va_start(args, format);
3031 serge 60
 
5271 serge 61
    vaf.fmt = format;
62
    vaf.va = &args;
3031 serge 63
 
5271 serge 64
    printk(KERN_ERR "[" DRM_NAME ":%pf] *ERROR* %pV",
65
           __builtin_return_address(0), &vaf);
3031 serge 66
 
5271 serge 67
    va_end(args);
3031 serge 68
}
69
EXPORT_SYMBOL(drm_err);
70
 
5060 serge 71
void drm_ut_debug_printk(const char *function_name, const char *format, ...)
3031 serge 72
{
4560 Serge 73
	struct va_format vaf;
3031 serge 74
	va_list args;
75
 
76
//   if (drm_debug & request_level) {
77
//       if (function_name)
78
//           printk(KERN_DEBUG "[%s:%s], ", prefix, function_name);
79
//       va_start(args, format);
80
//       vprintk(format, args);
81
//       va_end(args);
82
//   }
83
}
84
EXPORT_SYMBOL(drm_ut_debug_printk);
85
 
5060 serge 86
#if 0
87
struct drm_master *drm_master_create(struct drm_minor *minor)
88
{
89
	struct drm_master *master;
90
 
91
	master = kzalloc(sizeof(*master), GFP_KERNEL);
92
	if (!master)
93
		return NULL;
94
 
95
	kref_init(&master->refcount);
96
	spin_lock_init(&master->lock.spinlock);
97
	init_waitqueue_head(&master->lock.lock_queue);
98
	if (drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER)) {
99
		kfree(master);
100
		return NULL;
101
	}
102
	INIT_LIST_HEAD(&master->magicfree);
103
	master->minor = minor;
104
 
105
	return master;
106
}
107
 
108
struct drm_master *drm_master_get(struct drm_master *master)
109
{
110
	kref_get(&master->refcount);
111
	return master;
112
}
113
EXPORT_SYMBOL(drm_master_get);
114
 
115
static void drm_master_destroy(struct kref *kref)
116
{
117
	struct drm_master *master = container_of(kref, struct drm_master, refcount);
118
	struct drm_magic_entry *pt, *next;
119
	struct drm_device *dev = master->minor->dev;
120
	struct drm_map_list *r_list, *list_temp;
121
 
122
	mutex_lock(&dev->struct_mutex);
123
	if (dev->driver->master_destroy)
124
		dev->driver->master_destroy(dev, master);
125
 
126
	list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
127
		if (r_list->master == master) {
128
			drm_rmmap_locked(dev, r_list->map);
129
			r_list = NULL;
130
		}
131
	}
132
 
133
	if (master->unique) {
134
		kfree(master->unique);
135
		master->unique = NULL;
136
		master->unique_len = 0;
137
	}
138
 
139
	list_for_each_entry_safe(pt, next, &master->magicfree, head) {
140
		list_del(&pt->head);
141
		drm_ht_remove_item(&master->magiclist, &pt->hash_item);
142
		kfree(pt);
143
	}
144
 
145
	drm_ht_remove(&master->magiclist);
146
 
147
	mutex_unlock(&dev->struct_mutex);
148
	kfree(master);
149
}
150
 
151
void drm_master_put(struct drm_master **master)
152
{
153
	kref_put(&(*master)->refcount, drm_master_destroy);
154
	*master = NULL;
155
}
156
EXPORT_SYMBOL(drm_master_put);
157
 
158
int drm_setmaster_ioctl(struct drm_device *dev, void *data,
159
			struct drm_file *file_priv)
160
{
161
	int ret = 0;
162
 
163
	mutex_lock(&dev->master_mutex);
164
	if (file_priv->is_master)
165
		goto out_unlock;
166
 
167
	if (file_priv->minor->master) {
168
		ret = -EINVAL;
169
		goto out_unlock;
170
	}
171
 
172
	if (!file_priv->master) {
173
		ret = -EINVAL;
174
		goto out_unlock;
175
	}
176
 
177
	file_priv->minor->master = drm_master_get(file_priv->master);
178
	file_priv->is_master = 1;
179
	if (dev->driver->master_set) {
180
		ret = dev->driver->master_set(dev, file_priv, false);
181
		if (unlikely(ret != 0)) {
182
			file_priv->is_master = 0;
183
			drm_master_put(&file_priv->minor->master);
184
		}
185
	}
186
 
187
out_unlock:
188
	mutex_unlock(&dev->master_mutex);
189
	return ret;
190
}
191
 
192
int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
193
			 struct drm_file *file_priv)
194
{
195
	int ret = -EINVAL;
196
 
197
	mutex_lock(&dev->master_mutex);
198
	if (!file_priv->is_master)
199
		goto out_unlock;
200
 
201
	if (!file_priv->minor->master)
202
		goto out_unlock;
203
 
204
	ret = 0;
205
	if (dev->driver->master_drop)
206
		dev->driver->master_drop(dev, file_priv, false);
207
	drm_master_put(&file_priv->minor->master);
208
	file_priv->is_master = 0;
209
 
210
out_unlock:
211
	mutex_unlock(&dev->master_mutex);
212
	return ret;
213
}
214
 
215
/*
216
 * DRM Minors
217
 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
218
 * of them is represented by a drm_minor object. Depending on the capabilities
219
 * of the device-driver, different interfaces are registered.
220
 *
221
 * Minors can be accessed via dev->$minor_name. This pointer is either
222
 * NULL or a valid drm_minor pointer and stays valid as long as the device is
223
 * valid. This means, DRM minors have the same life-time as the underlying
224
 * device. However, this doesn't mean that the minor is active. Minors are
225
 * registered and unregistered dynamically according to device-state.
226
 */
227
 
228
static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
229
					     unsigned int type)
230
{
231
	switch (type) {
232
	case DRM_MINOR_LEGACY:
233
		return &dev->primary;
234
	case DRM_MINOR_RENDER:
235
		return &dev->render;
236
	case DRM_MINOR_CONTROL:
237
		return &dev->control;
238
	default:
239
		return NULL;
240
	}
241
}
242
 
243
static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
244
{
245
	struct drm_minor *minor;
246
 
247
	minor = kzalloc(sizeof(*minor), GFP_KERNEL);
248
	if (!minor)
249
		return -ENOMEM;
250
 
251
	minor->type = type;
252
	minor->dev = dev;
253
 
254
	*drm_minor_get_slot(dev, type) = minor;
255
	return 0;
256
}
257
 
258
static void drm_minor_free(struct drm_device *dev, unsigned int type)
259
{
260
	struct drm_minor **slot;
261
 
262
	slot = drm_minor_get_slot(dev, type);
263
	if (*slot) {
264
		drm_mode_group_destroy(&(*slot)->mode_group);
265
		kfree(*slot);
266
		*slot = NULL;
267
	}
268
}
269
 
270
static int drm_minor_register(struct drm_device *dev, unsigned int type)
271
{
272
	struct drm_minor *new_minor;
273
	unsigned long flags;
274
	int ret;
275
	int minor_id;
276
 
277
	DRM_DEBUG("\n");
278
 
279
	new_minor = *drm_minor_get_slot(dev, type);
280
	if (!new_minor)
281
		return 0;
282
 
283
	idr_preload(GFP_KERNEL);
284
	spin_lock_irqsave(&drm_minor_lock, flags);
285
	minor_id = idr_alloc(&drm_minors_idr,
286
			     NULL,
287
			     64 * type,
288
			     64 * (type + 1),
289
			     GFP_NOWAIT);
290
	spin_unlock_irqrestore(&drm_minor_lock, flags);
291
	idr_preload_end();
292
 
293
	if (minor_id < 0)
294
		return minor_id;
295
 
296
	new_minor->index = minor_id;
297
 
298
	ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
299
	if (ret) {
300
		DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
301
		goto err_id;
302
	}
303
 
304
	ret = drm_sysfs_device_add(new_minor);
305
	if (ret) {
306
		DRM_ERROR("DRM: Error sysfs_device_add.\n");
307
		goto err_debugfs;
308
	}
309
 
310
	/* replace NULL with @minor so lookups will succeed from now on */
311
	spin_lock_irqsave(&drm_minor_lock, flags);
312
	idr_replace(&drm_minors_idr, new_minor, new_minor->index);
313
	spin_unlock_irqrestore(&drm_minor_lock, flags);
314
 
315
	DRM_DEBUG("new minor assigned %d\n", minor_id);
316
	return 0;
317
 
318
err_debugfs:
319
	drm_debugfs_cleanup(new_minor);
320
err_id:
321
	spin_lock_irqsave(&drm_minor_lock, flags);
322
	idr_remove(&drm_minors_idr, minor_id);
323
	spin_unlock_irqrestore(&drm_minor_lock, flags);
324
	new_minor->index = 0;
325
	return ret;
326
}
327
 
328
static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
329
{
330
	struct drm_minor *minor;
331
	unsigned long flags;
332
 
333
	minor = *drm_minor_get_slot(dev, type);
334
	if (!minor || !minor->kdev)
335
		return;
336
 
337
	spin_lock_irqsave(&drm_minor_lock, flags);
338
	idr_remove(&drm_minors_idr, minor->index);
339
	spin_unlock_irqrestore(&drm_minor_lock, flags);
340
	minor->index = 0;
341
 
342
	drm_debugfs_cleanup(minor);
343
	drm_sysfs_device_remove(minor);
344
}
345
 
346
/**
347
 * drm_minor_acquire - Acquire a DRM minor
348
 * @minor_id: Minor ID of the DRM-minor
349
 *
350
 * Looks up the given minor-ID and returns the respective DRM-minor object. The
351
 * refence-count of the underlying device is increased so you must release this
352
 * object with drm_minor_release().
353
 *
354
 * As long as you hold this minor, it is guaranteed that the object and the
355
 * minor->dev pointer will stay valid! However, the device may get unplugged and
356
 * unregistered while you hold the minor.
357
 *
358
 * Returns:
359
 * Pointer to minor-object with increased device-refcount, or PTR_ERR on
360
 * failure.
361
 */
362
struct drm_minor *drm_minor_acquire(unsigned int minor_id)
363
{
364
	struct drm_minor *minor;
365
	unsigned long flags;
366
 
367
	spin_lock_irqsave(&drm_minor_lock, flags);
368
	minor = idr_find(&drm_minors_idr, minor_id);
369
	if (minor)
370
		drm_dev_ref(minor->dev);
371
	spin_unlock_irqrestore(&drm_minor_lock, flags);
372
 
373
	if (!minor) {
374
		return ERR_PTR(-ENODEV);
375
	} else if (drm_device_is_unplugged(minor->dev)) {
376
		drm_dev_unref(minor->dev);
377
		return ERR_PTR(-ENODEV);
378
	}
379
 
380
	return minor;
381
}
382
 
383
/**
384
 * drm_minor_release - Release DRM minor
385
 * @minor: Pointer to DRM minor object
386
 *
387
 * Release a minor that was previously acquired via drm_minor_acquire().
388
 */
389
void drm_minor_release(struct drm_minor *minor)
390
{
391
	drm_dev_unref(minor->dev);
392
}
393
 
394
/**
395
 * drm_put_dev - Unregister and release a DRM device
396
 * @dev: DRM device
397
 *
398
 * Called at module unload time or when a PCI device is unplugged.
399
 *
400
 * Use of this function is discouraged. It will eventually go away completely.
401
 * Please use drm_dev_unregister() and drm_dev_unref() explicitly instead.
402
 *
403
 * Cleans up all DRM device, calling drm_lastclose().
404
 */
405
void drm_put_dev(struct drm_device *dev)
406
{
407
	DRM_DEBUG("\n");
408
 
409
	if (!dev) {
410
		DRM_ERROR("cleanup called no dev\n");
411
		return;
412
	}
413
 
414
	drm_dev_unregister(dev);
415
	drm_dev_unref(dev);
416
}
417
EXPORT_SYMBOL(drm_put_dev);
418
 
419
void drm_unplug_dev(struct drm_device *dev)
420
{
421
	/* for a USB device */
422
	drm_minor_unregister(dev, DRM_MINOR_LEGACY);
423
	drm_minor_unregister(dev, DRM_MINOR_RENDER);
424
	drm_minor_unregister(dev, DRM_MINOR_CONTROL);
425
 
426
	mutex_lock(&drm_global_mutex);
427
 
428
	drm_device_set_unplugged(dev);
429
 
430
	if (dev->open_count == 0) {
431
		drm_put_dev(dev);
432
	}
433
	mutex_unlock(&drm_global_mutex);
434
}
435
EXPORT_SYMBOL(drm_unplug_dev);
436
 
437
/*
438
 * DRM internal mount
439
 * We want to be able to allocate our own "struct address_space" to control
440
 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
441
 * stand-alone address_space objects, so we need an underlying inode. As there
442
 * is no way to allocate an independent inode easily, we need a fake internal
443
 * VFS mount-point.
444
 *
445
 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
446
 * frees it again. You are allowed to use iget() and iput() to get references to
447
 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
448
 * drm_fs_inode_free() call (which does not have to be the last iput()).
449
 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
450
 * between multiple inode-users. You could, technically, call
451
 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
452
 * iput(), but this way you'd end up with a new vfsmount for each inode.
453
 */
454
 
455
static int drm_fs_cnt;
456
static struct vfsmount *drm_fs_mnt;
457
 
458
static const struct dentry_operations drm_fs_dops = {
459
	.d_dname	= simple_dname,
460
};
461
 
462
static const struct super_operations drm_fs_sops = {
463
	.statfs		= simple_statfs,
464
};
465
 
466
static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
467
				   const char *dev_name, void *data)
468
{
469
	return mount_pseudo(fs_type,
470
			    "drm:",
471
			    &drm_fs_sops,
472
			    &drm_fs_dops,
473
			    0x010203ff);
474
}
475
 
476
static struct file_system_type drm_fs_type = {
477
	.name		= "drm",
478
	.owner		= THIS_MODULE,
479
	.mount		= drm_fs_mount,
480
	.kill_sb	= kill_anon_super,
481
};
482
 
483
#endif
484
 
485
 
486
 
487
 
488
 
4104 Serge 489
int drm_fill_in_dev(struct drm_device *dev,
490
			   const struct pci_device_id *ent,
491
			   struct drm_driver *driver)
492
{
5060 serge 493
	int ret;
494
	dev->driver = driver;
4104 Serge 495
 
496
	INIT_LIST_HEAD(&dev->filelist);
497
	INIT_LIST_HEAD(&dev->ctxlist);
498
	INIT_LIST_HEAD(&dev->vmalist);
499
	INIT_LIST_HEAD(&dev->maplist);
500
	INIT_LIST_HEAD(&dev->vblank_event_list);
501
 
5060 serge 502
	spin_lock_init(&dev->buf_lock);
4104 Serge 503
	spin_lock_init(&dev->event_lock);
504
	mutex_init(&dev->struct_mutex);
505
	mutex_init(&dev->ctxlist_mutex);
506
 
507
//	if (drm_ht_create(&dev->map_hash, 12)) {
508
//		return -ENOMEM;
509
//	}
510
 
511
 
5060 serge 512
 
4104 Serge 513
	if (driver->driver_features & DRIVER_GEM) {
5060 serge 514
		ret = drm_gem_init(dev);
515
		if (ret) {
516
			DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
517
			goto err_ctxbitmap;
4104 Serge 518
		}
519
	}
520
 
521
	return 0;
522
 
5060 serge 523
err_ctxbitmap:
4104 Serge 524
//   drm_lastclose(dev);
5060 serge 525
	return ret;
4104 Serge 526
}
527
EXPORT_SYMBOL(drm_fill_in_dev);
3031 serge 528
/**
529
 * Compute size order.  Returns the exponent of the smaller power of two which
530
 * is greater or equal to given number.
531
 *
532
 * \param size size.
533
 * \return order.
534
 *
535
 * \todo Can be made faster.
536
 */
537
int drm_order(unsigned long size)
538
{
539
    int order;
540
    unsigned long tmp;
541
 
542
    for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
543
 
544
    if (size & (size - 1))
545
        ++order;
546
 
547
    return order;
548
}
3260 Serge 549
 
6084 serge 550
int drm_sysfs_connector_add(struct drm_connector *connector)
551
{
552
    return 0;
553
}
5060 serge 554
 
6084 serge 555
void drm_sysfs_connector_remove(struct drm_connector *connector)
556
{
557
}