drm_drv.c 15.8 KB
Newer Older
L
Linus Torvalds 已提交
1
/**
D
Dave Airlie 已提交
2
 * \file drm_drv.c
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 * Generic driver template
 *
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Gareth Hughes <gareth@valinux.com>
 *
 * To use this template, you must at least define the following (samples
 * given for the MGA driver):
 *
 * \code
 * #define DRIVER_AUTHOR	"VA Linux Systems, Inc."
 *
 * #define DRIVER_NAME		"mga"
 * #define DRIVER_DESC		"Matrox G200/G400"
 * #define DRIVER_DATE		"20001127"
 *
 * #define drm_x		mga_##x
 * \endcode
 */

/*
 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
 *
 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "drmP.h"
#include "drm_core.h"

D
Dave Airlie 已提交
52 53 54
static int drm_version(struct inode *inode, struct file *filp,
		       unsigned int cmd, unsigned long arg);

L
Linus Torvalds 已提交
55
/** Ioctl table */
D
Dave Airlie 已提交
56
static drm_ioctl_desc_t drm_ioctls[] = {
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	[DRM_IOCTL_NR(DRM_IOCTL_VERSION)] = {drm_version, 0},
	[DRM_IOCTL_NR(DRM_IOCTL_GET_UNIQUE)] = {drm_getunique, 0},
	[DRM_IOCTL_NR(DRM_IOCTL_GET_MAGIC)] = {drm_getmagic, 0},
	[DRM_IOCTL_NR(DRM_IOCTL_IRQ_BUSID)] = {drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_GET_MAP)] = {drm_getmap, 0},
	[DRM_IOCTL_NR(DRM_IOCTL_GET_CLIENT)] = {drm_getclient, 0},
	[DRM_IOCTL_NR(DRM_IOCTL_GET_STATS)] = {drm_getstats, 0},
	[DRM_IOCTL_NR(DRM_IOCTL_SET_VERSION)] = {drm_setversion, DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_SET_UNIQUE)] = {drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_BLOCK)] = {drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_UNBLOCK)] = {drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_AUTH_MAGIC)] = {drm_authmagic, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},

	[DRM_IOCTL_NR(DRM_IOCTL_ADD_MAP)] = {drm_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_RM_MAP)] = {drm_rmmap_ioctl, DRM_AUTH},

	[DRM_IOCTL_NR(DRM_IOCTL_SET_SAREA_CTX)] = {drm_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_GET_SAREA_CTX)] = {drm_getsareactx, DRM_AUTH},

76 77
	[DRM_IOCTL_NR(DRM_IOCTL_ADD_CTX)] = {drm_addctx, DRM_AUTH|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_RM_CTX)] = {drm_rmctx, DRM_AUTH|DRM_ROOT_ONLY},
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	[DRM_IOCTL_NR(DRM_IOCTL_MOD_CTX)] = {drm_modctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_GET_CTX)] = {drm_getctx, DRM_AUTH},
	[DRM_IOCTL_NR(DRM_IOCTL_SWITCH_CTX)] = {drm_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_NEW_CTX)] = {drm_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_RES_CTX)] = {drm_resctx, DRM_AUTH},

	[DRM_IOCTL_NR(DRM_IOCTL_ADD_DRAW)] = {drm_adddraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_RM_DRAW)] = {drm_rmdraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},

	[DRM_IOCTL_NR(DRM_IOCTL_LOCK)] = {drm_lock, DRM_AUTH},
	[DRM_IOCTL_NR(DRM_IOCTL_UNLOCK)] = {drm_unlock, DRM_AUTH},

	[DRM_IOCTL_NR(DRM_IOCTL_FINISH)] = {drm_noop, DRM_AUTH},

	[DRM_IOCTL_NR(DRM_IOCTL_ADD_BUFS)] = {drm_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS)] = {drm_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_INFO_BUFS)] = {drm_infobufs, DRM_AUTH},
	[DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS)] = {drm_mapbufs, DRM_AUTH},
	[DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS)] = {drm_freebufs, DRM_AUTH},
L
Linus Torvalds 已提交
97
	/* The DRM_IOCTL_DMA ioctl should be defined by the driver. */
98
	[DRM_IOCTL_NR(DRM_IOCTL_DMA)] = {NULL, DRM_AUTH},
L
Linus Torvalds 已提交
99

100
	[DRM_IOCTL_NR(DRM_IOCTL_CONTROL)] = {drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
L
Linus Torvalds 已提交
101 102

#if __OS_HAS_AGP
103 104 105 106 107 108 109 110
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_ACQUIRE)] = {drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_RELEASE)] = {drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_ENABLE)] = {drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_INFO)] = {drm_agp_info_ioctl, DRM_AUTH},
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_ALLOC)] = {drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_FREE)] = {drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_BIND)] = {drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_UNBIND)] = {drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
L
Linus Torvalds 已提交
111 112
#endif

113 114
	[DRM_IOCTL_NR(DRM_IOCTL_SG_ALLOC)] = {drm_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
	[DRM_IOCTL_NR(DRM_IOCTL_SG_FREE)] = {drm_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
L
Linus Torvalds 已提交
115

116
	[DRM_IOCTL_NR(DRM_IOCTL_WAIT_VBLANK)] = {drm_wait_vblank, 0},
117 118

	[DRM_IOCTL_NR(DRM_IOCTL_UPDATE_DRAW)] = {drm_update_drawable_info, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
L
Linus Torvalds 已提交
119 120
};

121
#define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129

/**
 * Take down the DRM device.
 *
 * \param dev DRM device structure.
 *
 * Frees every resource in \p dev.
 *
130
 * \sa drm_device
L
Linus Torvalds 已提交
131
 */
132
int drm_lastclose(struct drm_device * dev)
L
Linus Torvalds 已提交
133
{
134
	struct drm_magic_entry *pt, *next;
135
	drm_map_list_t *r_list, *list_t;
136
	struct drm_vma_entry *vma, *vma_temp;
L
Linus Torvalds 已提交
137 138
	int i;

D
Dave Airlie 已提交
139
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
140

141 142 143
	if (dev->driver->lastclose)
		dev->driver->lastclose(dev);
	DRM_DEBUG("driver lastclose completed\n");
L
Linus Torvalds 已提交
144 145 146 147 148 149 150

	if (dev->unique) {
		drm_free(dev->unique, strlen(dev->unique) + 1, DRM_MEM_DRIVER);
		dev->unique = NULL;
		dev->unique_len = 0;
	}

D
Dave Airlie 已提交
151 152
	if (dev->irq_enabled)
		drm_irq_uninstall(dev);
L
Linus Torvalds 已提交
153

154 155 156
	/* Free drawable information memory */
	for (i = 0; i < dev->drw_bitfield_length / sizeof(*dev->drw_bitfield);
	     i++) {
157
		struct drm_drawable_info *info = drm_get_drawable_info(dev, i);
158 159 160

		if (info) {
			drm_free(info->rects, info->num_rects *
161
				 sizeof(struct drm_clip_rect), DRM_MEM_BUFS);
162 163 164 165
			drm_free(info, sizeof(*info), DRM_MEM_BUFS);
		}
	}

D
Dave Airlie 已提交
166
	mutex_lock(&dev->struct_mutex);
D
Dave Airlie 已提交
167
	del_timer(&dev->timer);
L
Linus Torvalds 已提交
168

D
Dave Airlie 已提交
169
	/* Clear pid list */
170 171 172 173 174 175
	if (dev->magicfree.next) {
		list_for_each_entry_safe(pt, next, &dev->magicfree, head) {
			list_del(&pt->head);
			drm_ht_remove_item(&dev->magiclist, &pt->hash_item);
			drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
		}
176
		drm_ht_remove(&dev->magiclist);
L
Linus Torvalds 已提交
177 178
	}

D
Dave Airlie 已提交
179
	/* Clear AGP information */
L
Linus Torvalds 已提交
180
	if (drm_core_has_AGP(dev) && dev->agp) {
181
		drm_agp_mem_t *entry, *tempe;
L
Linus Torvalds 已提交
182

D
Dave Airlie 已提交
183 184
		/* Remove AGP resources, but leave dev->agp
		   intact until drv_cleanup is called. */
185
		list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
D
Dave Airlie 已提交
186 187 188 189
			if (entry->bound)
				drm_unbind_agp(entry->memory);
			drm_free_agp(entry->memory, entry->pages);
			drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
L
Linus Torvalds 已提交
190
		}
191
		INIT_LIST_HEAD(&dev->agp->memory);
L
Linus Torvalds 已提交
192

193
		if (dev->agp->acquired)
D
Dave Airlie 已提交
194
			drm_agp_release(dev);
L
Linus Torvalds 已提交
195 196

		dev->agp->acquired = 0;
D
Dave Airlie 已提交
197
		dev->agp->enabled = 0;
L
Linus Torvalds 已提交
198
	}
D
Dave Airlie 已提交
199 200 201 202
	if (drm_core_check_feature(dev, DRIVER_SG) && dev->sg) {
		drm_sg_cleanup(dev->sg);
		dev->sg = NULL;
	}
L
Linus Torvalds 已提交
203

D
Dave Airlie 已提交
204
	/* Clear vma list (only built for debugging) */
205 206 207
	list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
		list_del(&vma->head);
		drm_free(vma, sizeof(*vma), DRM_MEM_VMAS);
L
Linus Torvalds 已提交
208 209
	}

210 211 212
	list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
		drm_rmmap_locked(dev, r_list->map);
		r_list = NULL;
D
Dave Airlie 已提交
213 214 215 216 217 218 219 220
	}

	if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE) && dev->queuelist) {
		for (i = 0; i < dev->queue_count; i++) {
			if (dev->queuelist[i]) {
				drm_free(dev->queuelist[i],
					 sizeof(*dev->queuelist[0]),
					 DRM_MEM_QUEUES);
L
Linus Torvalds 已提交
221 222 223
				dev->queuelist[i] = NULL;
			}
		}
D
Dave Airlie 已提交
224 225 226
		drm_free(dev->queuelist,
			 dev->queue_slots * sizeof(*dev->queuelist),
			 DRM_MEM_QUEUES);
L
Linus Torvalds 已提交
227 228 229 230 231
		dev->queuelist = NULL;
	}
	dev->queue_count = 0;

	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
D
Dave Airlie 已提交
232
		drm_dma_takedown(dev);
L
Linus Torvalds 已提交
233

D
Dave Airlie 已提交
234 235
	if (dev->lock.hw_lock) {
		dev->sigdata.lock = dev->lock.hw_lock = NULL;	/* SHM removed */
L
Linus Torvalds 已提交
236
		dev->lock.filp = NULL;
D
Dave Airlie 已提交
237
		wake_up_interruptible(&dev->lock.lock_queue);
L
Linus Torvalds 已提交
238
	}
D
Dave Airlie 已提交
239
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
240

241
	DRM_DEBUG("lastclose completed\n");
L
Linus Torvalds 已提交
242 243 244 245 246 247 248 249 250 251 252 253
	return 0;
}

/**
 * Module initialization. Called via init_module at module load time, or via
 * linux/init/main.c (this is not currently supported).
 *
 * \return zero on success or a negative number on failure.
 *
 * Initializes an array of drm_device structures, and attempts to
 * initialize all available devices, using consecutive minors, registering the
 * stubs and initializing the AGP device.
D
Dave Airlie 已提交
254
 *
L
Linus Torvalds 已提交
255 256 257
 * Expands the \c DRIVER_PREINIT and \c DRIVER_POST_INIT macros before and
 * after the initialization for driver customization.
 */
D
Dave Airlie 已提交
258
int drm_init(struct drm_driver *driver)
L
Linus Torvalds 已提交
259 260 261 262 263
{
	struct pci_dev *pdev = NULL;
	struct pci_device_id *pid;
	int i;

D
Dave Airlie 已提交
264
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
265 266 267

	drm_mem_init();

D
Dave Airlie 已提交
268
	for (i = 0; driver->pci_driver.id_table[i].vendor != 0; i++) {
L
Linus Torvalds 已提交
269
		pid = (struct pci_device_id *)&driver->pci_driver.id_table[i];
D
Dave Airlie 已提交
270 271 272 273 274 275

		pdev = NULL;
		/* pass back in pdev to account for multiple identical cards */
		while ((pdev =
			pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
				       pid->subdevice, pdev)) != NULL) {
L
Linus Torvalds 已提交
276 277 278 279 280 281 282
			/* stealth mode requires a manual probe */
			pci_dev_get(pdev);
			drm_get_dev(pdev, pid, driver);
		}
	}
	return 0;
}
D
Dave Airlie 已提交
283

L
Linus Torvalds 已提交
284 285 286 287 288
EXPORT_SYMBOL(drm_init);

/**
 * Called via cleanup_module() at module unload time.
 *
289
 * Cleans up all DRM device, calling drm_lastclose().
D
Dave Airlie 已提交
290
 *
291
 * \sa drm_init
L
Linus Torvalds 已提交
292
 */
293
static void drm_cleanup(struct drm_device * dev)
L
Linus Torvalds 已提交
294
{
D
Dave Airlie 已提交
295
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
296 297 298 299 300 301

	if (!dev) {
		DRM_ERROR("cleanup called no dev\n");
		return;
	}

302
	drm_lastclose(dev);
L
Linus Torvalds 已提交
303

304
	drm_ht_remove(&dev->map_hash);
D
Dave Airlie 已提交
305

D
Dave Airlie 已提交
306 307
	drm_ctxbitmap_cleanup(dev);

L
Linus Torvalds 已提交
308 309 310
	if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
	    dev->agp && dev->agp->agp_mtrr >= 0) {
		int retval;
D
Dave Airlie 已提交
311 312 313 314
		retval = mtrr_del(dev->agp->agp_mtrr,
				  dev->agp->agp_info.aper_base,
				  dev->agp->agp_info.aper_size * 1024 * 1024);
		DRM_DEBUG("mtrr_del=%d\n", retval);
L
Linus Torvalds 已提交
315
	}
D
Dave Airlie 已提交
316 317 318

	if (drm_core_has_AGP(dev) && dev->agp) {
		drm_free(dev->agp, sizeof(*dev->agp), DRM_MEM_AGPLISTS);
L
Linus Torvalds 已提交
319 320 321
		dev->agp = NULL;
	}

322 323
	if (dev->driver->unload)
		dev->driver->unload(dev);
D
Dave Airlie 已提交
324

L
Linus Torvalds 已提交
325
	drm_put_head(&dev->primary);
D
Dave Airlie 已提交
326 327
	if (drm_put_dev(dev))
		DRM_ERROR("Cannot unload module\n");
L
Linus Torvalds 已提交
328 329
}

D
Dave Airlie 已提交
330
void drm_exit(struct drm_driver *driver)
L
Linus Torvalds 已提交
331 332
{
	int i;
333 334
	struct drm_device *dev = NULL;
	struct drm_head *head;
D
Dave Airlie 已提交
335 336

	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
337 338 339 340 341 342 343

	for (i = 0; i < drm_cards_limit; i++) {
		head = drm_heads[i];
		if (!head)
			continue;
		if (!head->dev)
			continue;
D
Dave Airlie 已提交
344
		if (head->dev->driver != driver)
L
Linus Torvalds 已提交
345
			continue;
D
Dave Airlie 已提交
346
		dev = head->dev;
347 348 349 350 351 352
		if (dev) {
			/* release the pci driver */
			if (dev->pdev)
				pci_dev_put(dev->pdev);
			drm_cleanup(dev);
		}
L
Linus Torvalds 已提交
353
	}
D
Dave Airlie 已提交
354
	DRM_INFO("Module unloaded\n");
L
Linus Torvalds 已提交
355
}
D
Dave Airlie 已提交
356

L
Linus Torvalds 已提交
357 358 359
EXPORT_SYMBOL(drm_exit);

/** File operations structure */
360
static const struct file_operations drm_stub_fops = {
L
Linus Torvalds 已提交
361
	.owner = THIS_MODULE,
D
Dave Airlie 已提交
362
	.open = drm_stub_open
L
Linus Torvalds 已提交
363 364 365 366 367
};

static int __init drm_core_init(void)
{
	int ret = -ENOMEM;
D
Dave Airlie 已提交
368 369 370 371 372 373 374

	drm_cards_limit =
	    (drm_cards_limit <
	     DRM_MAX_MINOR + 1 ? drm_cards_limit : DRM_MAX_MINOR + 1);
	drm_heads =
	    drm_calloc(drm_cards_limit, sizeof(*drm_heads), DRM_MEM_STUB);
	if (!drm_heads)
L
Linus Torvalds 已提交
375
		goto err_p1;
D
Dave Airlie 已提交
376

L
Linus Torvalds 已提交
377 378
	if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
		goto err_p1;
D
Dave Airlie 已提交
379

L
Linus Torvalds 已提交
380 381
	drm_class = drm_sysfs_create(THIS_MODULE, "drm");
	if (IS_ERR(drm_class)) {
D
Dave Airlie 已提交
382
		printk(KERN_ERR "DRM: Error creating drm class.\n");
L
Linus Torvalds 已提交
383 384 385 386
		ret = PTR_ERR(drm_class);
		goto err_p2;
	}

387
	drm_proc_root = proc_mkdir("dri", NULL);
L
Linus Torvalds 已提交
388 389 390 391 392
	if (!drm_proc_root) {
		DRM_ERROR("Cannot create /proc/dri\n");
		ret = -1;
		goto err_p3;
	}
D
Dave Airlie 已提交
393 394 395

	DRM_INFO("Initialized %s %d.%d.%d %s\n",
		 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
L
Linus Torvalds 已提交
396
	return 0;
D
Dave Airlie 已提交
397
      err_p3:
L
Linus Torvalds 已提交
398
	drm_sysfs_destroy(drm_class);
D
Dave Airlie 已提交
399
      err_p2:
L
Linus Torvalds 已提交
400 401
	unregister_chrdev(DRM_MAJOR, "drm");
	drm_free(drm_heads, sizeof(*drm_heads) * drm_cards_limit, DRM_MEM_STUB);
D
Dave Airlie 已提交
402
      err_p1:
L
Linus Torvalds 已提交
403 404 405
	return ret;
}

D
Dave Airlie 已提交
406
static void __exit drm_core_exit(void)
L
Linus Torvalds 已提交
407 408 409 410 411 412
{
	remove_proc_entry("dri", NULL);
	drm_sysfs_destroy(drm_class);

	unregister_chrdev(DRM_MAJOR, "drm");

D
Dave Airlie 已提交
413
	drm_free(drm_heads, sizeof(*drm_heads) * drm_cards_limit, DRM_MEM_STUB);
L
Linus Torvalds 已提交
414 415
}

D
Dave Airlie 已提交
416 417
module_init(drm_core_init);
module_exit(drm_core_exit);
L
Linus Torvalds 已提交
418 419 420 421 422 423 424 425 426 427 428 429

/**
 * Get version information
 *
 * \param inode device inode.
 * \param filp file pointer.
 * \param cmd command.
 * \param arg user argument, pointing to a drm_version structure.
 * \return zero on success or negative number on failure.
 *
 * Fills in the version information in \p arg.
 */
D
Dave Airlie 已提交
430 431
static int drm_version(struct inode *inode, struct file *filp,
		       unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
432
{
433 434
	struct drm_file *priv = filp->private_data;
	struct drm_device *dev = priv->head->dev;
435 436
	struct drm_version __user *argp = (void __user *)arg;
	struct drm_version version;
437
	int len;
L
Linus Torvalds 已提交
438

D
Dave Airlie 已提交
439
	if (copy_from_user(&version, argp, sizeof(version)))
L
Linus Torvalds 已提交
440 441
		return -EFAULT;

442 443 444 445 446 447
	version.version_major = dev->driver->major;
	version.version_minor = dev->driver->minor;
	version.version_patchlevel = dev->driver->patchlevel;
	DRM_COPY(version.name, dev->driver->name);
	DRM_COPY(version.date, dev->driver->date);
	DRM_COPY(version.desc, dev->driver->desc);
D
Dave Airlie 已提交
448 449

	if (copy_to_user(argp, &version, sizeof(version)))
L
Linus Torvalds 已提交
450 451 452 453
		return -EFAULT;
	return 0;
}

D
Dave Airlie 已提交
454
/**
L
Linus Torvalds 已提交
455 456 457 458 459 460 461 462 463 464 465
 * Called whenever a process performs an ioctl on /dev/drm.
 *
 * \param inode device inode.
 * \param filp file pointer.
 * \param cmd command.
 * \param arg user argument.
 * \return zero on success or negative number on failure.
 *
 * Looks up the ioctl function in the ::ioctls table, checking for root
 * previleges if so required, and dispatches to the respective function.
 */
D
Dave Airlie 已提交
466 467
int drm_ioctl(struct inode *inode, struct file *filp,
	      unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
468
{
469 470
	struct drm_file *priv = filp->private_data;
	struct drm_device *dev = priv->head->dev;
L
Linus Torvalds 已提交
471 472 473 474 475
	drm_ioctl_desc_t *ioctl;
	drm_ioctl_t *func;
	unsigned int nr = DRM_IOCTL_NR(cmd);
	int retcode = -EINVAL;

D
Dave Airlie 已提交
476 477
	atomic_inc(&dev->ioctl_count);
	atomic_inc(&dev->counts[_DRM_STAT_IOCTLS]);
L
Linus Torvalds 已提交
478 479
	++priv->ioctl_count;

D
Dave Airlie 已提交
480 481 482 483 484
	DRM_DEBUG("pid=%d, cmd=0x%02x, nr=0x%02x, dev 0x%lx, auth=%d\n",
		  current->pid, cmd, nr,
		  (long)old_encode_dev(priv->head->device),
		  priv->authenticated);

485
	if ((nr >= DRM_CORE_IOCTL_COUNT) &&
486 487
	    ((nr < DRM_COMMAND_BASE) || (nr >= DRM_COMMAND_END)))
		goto err_i1;
488 489
	if ((nr >= DRM_COMMAND_BASE) && (nr < DRM_COMMAND_END) &&
	    (nr < DRM_COMMAND_BASE + dev->driver->num_ioctls))
L
Linus Torvalds 已提交
490
		ioctl = &dev->driver->ioctls[nr - DRM_COMMAND_BASE];
491 492
	else if ((nr >= DRM_COMMAND_END) || (nr < DRM_COMMAND_BASE))
		ioctl = &drm_ioctls[nr];
L
Linus Torvalds 已提交
493 494
	else
		goto err_i1;
D
Dave Airlie 已提交
495

L
Linus Torvalds 已提交
496 497 498 499
	func = ioctl->func;
	/* is there a local override? */
	if ((nr == DRM_IOCTL_NR(DRM_IOCTL_DMA)) && dev->driver->dma_ioctl)
		func = dev->driver->dma_ioctl;
D
Dave Airlie 已提交
500 501 502

	if (!func) {
		DRM_DEBUG("no function\n");
L
Linus Torvalds 已提交
503
		retcode = -EINVAL;
504 505 506
	} else if (((ioctl->flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)) ||
		   ((ioctl->flags & DRM_AUTH) && !priv->authenticated) ||
		   ((ioctl->flags & DRM_MASTER) && !priv->master)) {
L
Linus Torvalds 已提交
507 508
		retcode = -EACCES;
	} else {
D
Dave Airlie 已提交
509
		retcode = func(inode, filp, cmd, arg);
L
Linus Torvalds 已提交
510
	}
D
Dave Airlie 已提交
511 512 513 514 515

      err_i1:
	atomic_dec(&dev->ioctl_count);
	if (retcode)
		DRM_DEBUG("ret = %x\n", retcode);
L
Linus Torvalds 已提交
516 517 518
	return retcode;
}

D
Dave Airlie 已提交
519
EXPORT_SYMBOL(drm_ioctl);
520 521 522 523 524

drm_local_map_t *drm_getsarea(struct drm_device *dev)
{
	drm_map_list_t *entry;

D
Dave Airlie 已提交
525
	list_for_each_entry(entry, &dev->maplist, head) {
526 527 528 529 530 531 532 533
		if (entry->map && entry->map->type == _DRM_SHM &&
		    (entry->map->flags & _DRM_CONTAINS_LOCK)) {
			return entry->map;
		}
	}
	return NULL;
}
EXPORT_SYMBOL(drm_getsarea);