drm_drv.c 16.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/**
2
 * \file drm_drv.c
Linus Torvalds's avatar
Linus Torvalds committed
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
 * 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.
 */

49
#include <linux/debugfs.h>
50
#include <linux/slab.h>
51
#include <linux/export.h>
Linus Torvalds's avatar
Linus Torvalds committed
52 53 54
#include "drmP.h"
#include "drm_core.h"

55

56 57
static int drm_version(struct drm_device *dev, void *data,
		       struct drm_file *file_priv);
Dave Airlie's avatar
Dave Airlie committed
58

59 60 61
#define DRM_IOCTL_DEF(ioctl, _func, _flags) \
	[DRM_IOCTL_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, .cmd_drv = 0}

Linus Torvalds's avatar
Linus Torvalds committed
62
/** Ioctl table */
63 64 65 66 67 68 69 70
static struct drm_ioctl_desc drm_ioctls[] = {
	DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, 0),
	DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
	DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
	DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, 0),
	DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, 0),
	DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, 0),
71
	DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, 0),
72
	DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER),
73 74 75 76

	DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
77
	DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER),
78 79 80 81 82 83 84

	DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_rmmap_ioctl, DRM_AUTH),

	DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_getsareactx, DRM_AUTH),

85 86 87
	DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_ROOT_ONLY),

88 89 90 91 92 93 94 95
	DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_addctx, DRM_AUTH|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_modctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_getctx, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_resctx, DRM_AUTH),

96 97
	DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
98 99 100 101 102 103 104 105 106 107 108

	DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_lock, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_unlock, DRM_AUTH),

	DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),

	DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_infobufs, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_mapbufs, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_freebufs, DRM_AUTH),
Linus Torvalds's avatar
Linus Torvalds committed
109
	/* The DRM_IOCTL_DMA ioctl should be defined by the driver. */
110
	DRM_IOCTL_DEF(DRM_IOCTL_DMA, NULL, DRM_AUTH),
Linus Torvalds's avatar
Linus Torvalds committed
111

112
	DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Linus Torvalds's avatar
Linus Torvalds committed
113 114

#if __OS_HAS_AGP
115 116 117 118 119 120 121 122
	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Linus Torvalds's avatar
Linus Torvalds committed
123 124
#endif

125 126
	DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_sg_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Linus Torvalds's avatar
Linus Torvalds committed
127

128
	DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, 0),
129

130 131
	DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0),

132
	DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
133

134 135 136 137 138
	DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED),

	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes's avatar
Jesse Barnes committed
139
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
140 141
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes's avatar
Jesse Barnes committed
142 143
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
144 145 146 147 148 149 150 151 152 153 154 155
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_MASTER|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_mode_attachmode_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_mode_detachmode_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_MASTER | DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_mode_connector_property_set_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
156
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
157 158
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
159 160 161 162
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED)
Linus Torvalds's avatar
Linus Torvalds committed
163 164
};

165
#define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
Linus Torvalds's avatar
Linus Torvalds committed
166 167 168 169 170 171 172 173

/**
 * Take down the DRM device.
 *
 * \param dev DRM device structure.
 *
 * Frees every resource in \p dev.
 *
174
 * \sa drm_device
Linus Torvalds's avatar
Linus Torvalds committed
175
 */
176
int drm_lastclose(struct drm_device * dev)
Linus Torvalds's avatar
Linus Torvalds committed
177
{
178
	struct drm_vma_entry *vma, *vma_temp;
Linus Torvalds's avatar
Linus Torvalds committed
179 180
	int i;

181
	DRM_DEBUG("\n");
Linus Torvalds's avatar
Linus Torvalds committed
182

183 184 185
	if (dev->driver->lastclose)
		dev->driver->lastclose(dev);
	DRM_DEBUG("driver lastclose completed\n");
Linus Torvalds's avatar
Linus Torvalds committed
186

Dave Airlie's avatar
Dave Airlie committed
187
	if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
188
		drm_irq_uninstall(dev);
Linus Torvalds's avatar
Linus Torvalds committed
189

Dave Airlie's avatar
Dave Airlie committed
190
	mutex_lock(&dev->struct_mutex);
191

192
	/* Clear AGP information */
Dave Airlie's avatar
Dave Airlie committed
193
	if (drm_core_has_AGP(dev) && dev->agp &&
194
			!drm_core_check_feature(dev, DRIVER_MODESET)) {
195
		struct drm_agp_mem *entry, *tempe;
Linus Torvalds's avatar
Linus Torvalds committed
196

197 198
		/* Remove AGP resources, but leave dev->agp
		   intact until drv_cleanup is called. */
199
		list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
200 201 202
			if (entry->bound)
				drm_unbind_agp(entry->memory);
			drm_free_agp(entry->memory, entry->pages);
203
			kfree(entry);
Linus Torvalds's avatar
Linus Torvalds committed
204
		}
205
		INIT_LIST_HEAD(&dev->agp->memory);
Linus Torvalds's avatar
Linus Torvalds committed
206

207
		if (dev->agp->acquired)
208
			drm_agp_release(dev);
Linus Torvalds's avatar
Linus Torvalds committed
209 210

		dev->agp->acquired = 0;
211
		dev->agp->enabled = 0;
Linus Torvalds's avatar
Linus Torvalds committed
212
	}
Dave Airlie's avatar
Dave Airlie committed
213 214
	if (drm_core_check_feature(dev, DRIVER_SG) && dev->sg &&
	    !drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie's avatar
Dave Airlie committed
215 216 217
		drm_sg_cleanup(dev->sg);
		dev->sg = NULL;
	}
Linus Torvalds's avatar
Linus Torvalds committed
218

219
	/* Clear vma list (only built for debugging) */
220 221
	list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
		list_del(&vma->head);
222
		kfree(vma);
Linus Torvalds's avatar
Linus Torvalds committed
223 224
	}

225 226
	if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE) && dev->queuelist) {
		for (i = 0; i < dev->queue_count; i++) {
227 228
			kfree(dev->queuelist[i]);
			dev->queuelist[i] = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
229
		}
230
		kfree(dev->queuelist);
Linus Torvalds's avatar
Linus Torvalds committed
231 232 233 234
		dev->queuelist = NULL;
	}
	dev->queue_count = 0;

Dave Airlie's avatar
Dave Airlie committed
235 236
	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
	    !drm_core_check_feature(dev, DRIVER_MODESET))
237
		drm_dma_takedown(dev);
Linus Torvalds's avatar
Linus Torvalds committed
238

Jesse Barnes's avatar
Jesse Barnes committed
239
	dev->dev_mapping = NULL;
Dave Airlie's avatar
Dave Airlie committed
240
	mutex_unlock(&dev->struct_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
241

242
	DRM_DEBUG("lastclose completed\n");
Linus Torvalds's avatar
Linus Torvalds committed
243 244 245 246
	return 0;
}

/** File operations structure */
247
static const struct file_operations drm_stub_fops = {
Linus Torvalds's avatar
Linus Torvalds committed
248
	.owner = THIS_MODULE,
249 250
	.open = drm_stub_open,
	.llseek = noop_llseek,
Linus Torvalds's avatar
Linus Torvalds committed
251 252 253 254 255
};

static int __init drm_core_init(void)
{
	int ret = -ENOMEM;
256

257
	drm_global_init();
258
	idr_init(&drm_minors_idr);
259

Linus Torvalds's avatar
Linus Torvalds committed
260 261
	if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
		goto err_p1;
262

Linus Torvalds's avatar
Linus Torvalds committed
263 264
	drm_class = drm_sysfs_create(THIS_MODULE, "drm");
	if (IS_ERR(drm_class)) {
265
		printk(KERN_ERR "DRM: Error creating drm class.\n");
Linus Torvalds's avatar
Linus Torvalds committed
266 267 268 269
		ret = PTR_ERR(drm_class);
		goto err_p2;
	}

270
	drm_proc_root = proc_mkdir("dri", NULL);
Linus Torvalds's avatar
Linus Torvalds committed
271 272 273 274 275
	if (!drm_proc_root) {
		DRM_ERROR("Cannot create /proc/dri\n");
		ret = -1;
		goto err_p3;
	}
276

277 278
	drm_debugfs_root = debugfs_create_dir("dri", NULL);
	if (!drm_debugfs_root) {
279
		DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
280 281 282 283
		ret = -1;
		goto err_p3;
	}

284 285
	DRM_INFO("Initialized %s %d.%d.%d %s\n",
		 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
Linus Torvalds's avatar
Linus Torvalds committed
286
	return 0;
Jesse Barnes's avatar
Jesse Barnes committed
287 288 289
err_p3:
	drm_sysfs_destroy();
err_p2:
Linus Torvalds's avatar
Linus Torvalds committed
290
	unregister_chrdev(DRM_MAJOR, "drm");
291 292

	idr_destroy(&drm_minors_idr);
Jesse Barnes's avatar
Jesse Barnes committed
293
err_p1:
Linus Torvalds's avatar
Linus Torvalds committed
294 295 296
	return ret;
}

297
static void __exit drm_core_exit(void)
Linus Torvalds's avatar
Linus Torvalds committed
298 299
{
	remove_proc_entry("dri", NULL);
300
	debugfs_remove(drm_debugfs_root);
Jesse Barnes's avatar
Jesse Barnes committed
301
	drm_sysfs_destroy();
Linus Torvalds's avatar
Linus Torvalds committed
302 303 304

	unregister_chrdev(DRM_MAJOR, "drm");

305
	idr_remove_all(&drm_minors_idr);
306
	idr_destroy(&drm_minors_idr);
Linus Torvalds's avatar
Linus Torvalds committed
307 308
}

309 310
module_init(drm_core_init);
module_exit(drm_core_exit);
Linus Torvalds's avatar
Linus Torvalds committed
311

312 313 314
/**
 * Copy and IOCTL return string to user space
 */
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
static int drm_copy_field(char *buf, size_t *buf_len, const char *value)
{
	int len;

	/* don't overflow userbuf */
	len = strlen(value);
	if (len > *buf_len)
		len = *buf_len;

	/* let userspace know exact length of driver value (which could be
	 * larger than the userspace-supplied buffer) */
	*buf_len = strlen(value);

	/* finally, try filling in the userbuf */
	if (len && buf)
		if (copy_to_user(buf, value, len))
			return -EFAULT;
	return 0;
}
334

Linus Torvalds's avatar
Linus Torvalds committed
335 336 337 338 339 340 341 342 343 344 345
/**
 * 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.
 */
346 347
static int drm_version(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
Linus Torvalds's avatar
Linus Torvalds committed
348
{
349
	struct drm_version *version = data;
350
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
351

352 353 354
	version->version_major = dev->driver->major;
	version->version_minor = dev->driver->minor;
	version->version_patchlevel = dev->driver->patchlevel;
355 356 357 358 359 360 361 362 363 364
	err = drm_copy_field(version->name, &version->name_len,
			dev->driver->name);
	if (!err)
		err = drm_copy_field(version->date, &version->date_len,
				dev->driver->date);
	if (!err)
		err = drm_copy_field(version->desc, &version->desc_len,
				dev->driver->desc);

	return err;
Linus Torvalds's avatar
Linus Torvalds committed
365 366
}

367
/**
Linus Torvalds's avatar
Linus Torvalds committed
368 369 370
 * Called whenever a process performs an ioctl on /dev/drm.
 *
 * \param inode device inode.
371
 * \param file_priv DRM file private.
Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377 378
 * \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.
 */
379
long drm_ioctl(struct file *filp,
380
	      unsigned int cmd, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
381
{
382
	struct drm_file *file_priv = filp->private_data;
383
	struct drm_device *dev;
384
	struct drm_ioctl_desc *ioctl;
Linus Torvalds's avatar
Linus Torvalds committed
385 386 387
	drm_ioctl_t *func;
	unsigned int nr = DRM_IOCTL_NR(cmd);
	int retcode = -EINVAL;
388
	char stack_kdata[128];
389
	char *kdata = NULL;
390
	unsigned int usize, asize;
Linus Torvalds's avatar
Linus Torvalds committed
391

392
	dev = file_priv->minor->dev;
393 394
	atomic_inc(&dev->ioctl_count);
	atomic_inc(&dev->counts[_DRM_STAT_IOCTLS]);
395
	++file_priv->ioctl_count;
Linus Torvalds's avatar
Linus Torvalds committed
396

397
	DRM_DEBUG("pid=%d, cmd=0x%02x, nr=0x%02x, dev 0x%lx, auth=%d\n",
398
		  task_pid_nr(current), cmd, nr,
399
		  (long)old_encode_dev(file_priv->minor->device),
400
		  file_priv->authenticated);
401

402
	if ((nr >= DRM_CORE_IOCTL_COUNT) &&
403 404
	    ((nr < DRM_COMMAND_BASE) || (nr >= DRM_COMMAND_END)))
		goto err_i1;
405
	if ((nr >= DRM_COMMAND_BASE) && (nr < DRM_COMMAND_END) &&
406 407
	    (nr < DRM_COMMAND_BASE + dev->driver->num_ioctls)) {
		u32 drv_size;
Linus Torvalds's avatar
Linus Torvalds committed
408
		ioctl = &dev->driver->ioctls[nr - DRM_COMMAND_BASE];
409 410 411 412 413
		drv_size = _IOC_SIZE(ioctl->cmd_drv);
		usize = asize = _IOC_SIZE(cmd);
		if (drv_size > asize)
			asize = drv_size;
	}
414
	else if ((nr >= DRM_COMMAND_END) || (nr < DRM_COMMAND_BASE)) {
415
		ioctl = &drm_ioctls[nr];
416
		cmd = ioctl->cmd;
417
		usize = asize = _IOC_SIZE(cmd);
418
	} else
Linus Torvalds's avatar
Linus Torvalds committed
419
		goto err_i1;
420

421
	/* Do not trust userspace, use our own definition */
Linus Torvalds's avatar
Linus Torvalds committed
422 423 424 425
	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;
426 427 428

	if (!func) {
		DRM_DEBUG("no function\n");
Linus Torvalds's avatar
Linus Torvalds committed
429
		retcode = -EINVAL;
430
	} else if (((ioctl->flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)) ||
431
		   ((ioctl->flags & DRM_AUTH) && !file_priv->authenticated) ||
432 433
		   ((ioctl->flags & DRM_MASTER) && !file_priv->is_master) ||
		   (!(ioctl->flags & DRM_CONTROL_ALLOW) && (file_priv->minor->type == DRM_MINOR_CONTROL))) {
Linus Torvalds's avatar
Linus Torvalds committed
434 435
		retcode = -EACCES;
	} else {
436
		if (cmd & (IOC_IN | IOC_OUT)) {
437
			if (asize <= sizeof(stack_kdata)) {
438 439
				kdata = stack_kdata;
			} else {
440
				kdata = kmalloc(asize, GFP_KERNEL);
441 442 443 444
				if (!kdata) {
					retcode = -ENOMEM;
					goto err_i1;
				}
445
			}
446 447
			if (asize > usize)
				memset(kdata + usize, 0, asize - usize);
448 449 450 451
		}

		if (cmd & IOC_IN) {
			if (copy_from_user(kdata, (void __user *)arg,
452
					   usize) != 0) {
453
				retcode = -EFAULT;
454 455
				goto err_i1;
			}
456
		} else
457
			memset(kdata, 0, usize);
458

459 460 461
		if (ioctl->flags & DRM_UNLOCKED)
			retcode = func(dev, kdata, file_priv);
		else {
462
			mutex_lock(&drm_global_mutex);
463
			retcode = func(dev, kdata, file_priv);
464
			mutex_unlock(&drm_global_mutex);
465
		}
466

467
		if (cmd & IOC_OUT) {
468
			if (copy_to_user((void __user *)arg, kdata,
469
					 usize) != 0)
470
				retcode = -EFAULT;
471
		}
Linus Torvalds's avatar
Linus Torvalds committed
472
	}
473 474

      err_i1:
475
	if (kdata != stack_kdata)
476
		kfree(kdata);
477 478 479
	atomic_dec(&dev->ioctl_count);
	if (retcode)
		DRM_DEBUG("ret = %x\n", retcode);
Linus Torvalds's avatar
Linus Torvalds committed
480 481 482
	return retcode;
}

483
EXPORT_SYMBOL(drm_ioctl);
484

485
struct drm_local_map *drm_getsarea(struct drm_device *dev)
486
{
487
	struct drm_map_list *entry;
488

489
	list_for_each_entry(entry, &dev->maplist, head) {
490 491 492 493 494 495 496 497
		if (entry->map && entry->map->type == _DRM_SHM &&
		    (entry->map->flags & _DRM_CONTAINS_LOCK)) {
			return entry->map;
		}
	}
	return NULL;
}
EXPORT_SYMBOL(drm_getsarea);