drm_agpsupport.c 12.2 KB
Newer Older
1
/*
2
 * \file drm_agpsupport.c
L
Linus Torvalds 已提交
3
 * DRM support for AGP/GART backend
D
Dave Airlie 已提交
4
 *
L
Linus Torvalds 已提交
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
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Gareth Hughes <gareth@valinux.com>
 */

/*
 * Copyright 1999 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 <linux/module.h>
S
Sam Ravnborg 已提交
35
#include <linux/pci.h>
36
#include <linux/slab.h>
L
Linus Torvalds 已提交
37

38 39
#include <asm/agp.h>

S
Sam Ravnborg 已提交
40 41 42 43 44 45 46 47
#include <drm/drm_agpsupport.h>
#include <drm/drm_device.h>
#include <drm/drm_drv.h>
#include <drm/drm_file.h>
#include <drm/drm_print.h>

#include "drm_legacy.h"

L
Linus Torvalds 已提交
48
/**
49
 * Get AGP information.
L
Linus Torvalds 已提交
50 51
 *
 * \param inode device inode.
52
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
53 54 55 56 57 58 59
 * \param cmd command.
 * \param arg pointer to a (output) drm_agp_info structure.
 * \return zero on success or a negative number on failure.
 *
 * Verifies the AGP device has been initialized and acquired and fills in the
 * drm_agp_info structure with the information in drm_agp_head::agp_info.
 */
60
int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
L
Linus Torvalds 已提交
61
{
62
	struct agp_kern_info *kern;
L
Linus Torvalds 已提交
63 64 65 66

	if (!dev->agp || !dev->agp->acquired)
		return -EINVAL;

D
Dave Airlie 已提交
67
	kern = &dev->agp->agp_info;
68 69
	info->agp_version_major = kern->version.major;
	info->agp_version_minor = kern->version.minor;
D
Dave Airlie 已提交
70 71 72 73 74 75 76
	info->mode = kern->mode;
	info->aperture_base = kern->aper_base;
	info->aperture_size = kern->aper_size * 1024 * 1024;
	info->memory_allowed = kern->max_memory << PAGE_SHIFT;
	info->memory_used = kern->current_memory << PAGE_SHIFT;
	info->id_vendor = kern->device->vendor;
	info->id_device = kern->device->device;
77 78 79 80 81

	return 0;
}
EXPORT_SYMBOL(drm_agp_info);

82 83
int drm_agp_info_ioctl(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
84
{
85
	struct drm_agp_info *info = data;
86 87
	int err;

88
	err = drm_agp_info(dev, info);
89 90
	if (err)
		return err;
D
Dave Airlie 已提交
91

L
Linus Torvalds 已提交
92 93 94 95
	return 0;
}

/**
96
 * Acquire the AGP device.
L
Linus Torvalds 已提交
97
 *
98
 * \param dev DRM device that is to acquire AGP.
D
Dave Airlie 已提交
99
 * \return zero on success or a negative number on failure.
L
Linus Torvalds 已提交
100 101
 *
 * Verifies the AGP device hasn't been acquired before and calls
102
 * \c agp_backend_acquire.
L
Linus Torvalds 已提交
103
 */
104
int drm_agp_acquire(struct drm_device *dev)
L
Linus Torvalds 已提交
105 106 107 108 109
{
	if (!dev->agp)
		return -ENODEV;
	if (dev->agp->acquired)
		return -EBUSY;
110 111
	dev->agp->bridge = agp_backend_acquire(dev->pdev);
	if (!dev->agp->bridge)
L
Linus Torvalds 已提交
112 113 114 115
		return -ENODEV;
	dev->agp->acquired = 1;
	return 0;
}
116
EXPORT_SYMBOL(drm_agp_acquire);
L
Linus Torvalds 已提交
117 118

/**
119
 * Acquire the AGP device (ioctl).
L
Linus Torvalds 已提交
120 121
 *
 * \param inode device inode.
122
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
123 124 125 126
 * \param cmd command.
 * \param arg user argument.
 * \return zero on success or a negative number on failure.
 *
127 128
 * Verifies the AGP device hasn't been acquired before and calls
 * \c agp_backend_acquire.
L
Linus Torvalds 已提交
129
 */
130 131
int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *file_priv)
L
Linus Torvalds 已提交
132
{
133
	return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
134
}
L
Linus Torvalds 已提交
135

136 137 138
/**
 * Release the AGP device.
 *
139
 * \param dev DRM device that is to release AGP.
140 141 142 143
 * \return zero on success or a negative number on failure.
 *
 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
 */
144
int drm_agp_release(struct drm_device *dev)
145
{
L
Linus Torvalds 已提交
146 147 148 149 150 151
	if (!dev->agp || !dev->agp->acquired)
		return -EINVAL;
	agp_backend_release(dev->agp->bridge);
	dev->agp->acquired = 0;
	return 0;
}
152
EXPORT_SYMBOL(drm_agp_release);
L
Linus Torvalds 已提交
153

154 155
int drm_agp_release_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *file_priv)
L
Linus Torvalds 已提交
156
{
157
	return drm_agp_release(dev);
L
Linus Torvalds 已提交
158 159 160 161
}

/**
 * Enable the AGP bus.
D
Dave Airlie 已提交
162
 *
163 164
 * \param dev DRM device that has previously acquired AGP.
 * \param mode Requested AGP mode.
L
Linus Torvalds 已提交
165 166 167
 * \return zero on success or a negative number on failure.
 *
 * Verifies the AGP device has been acquired but not enabled, and calls
168
 * \c agp_enable.
L
Linus Torvalds 已提交
169
 */
170
int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
L
Linus Torvalds 已提交
171 172 173 174
{
	if (!dev->agp || !dev->agp->acquired)
		return -EINVAL;

D
Dave Airlie 已提交
175
	dev->agp->mode = mode.mode;
L
Linus Torvalds 已提交
176 177 178 179
	agp_enable(dev->agp->bridge, mode.mode);
	dev->agp->enabled = 1;
	return 0;
}
180 181
EXPORT_SYMBOL(drm_agp_enable);

182 183
int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
			 struct drm_file *file_priv)
184
{
185
	struct drm_agp_mode *mode = data;
186

187
	return drm_agp_enable(dev, *mode);
188
}
L
Linus Torvalds 已提交
189 190 191 192 193

/**
 * Allocate AGP memory.
 *
 * \param inode device inode.
194
 * \param file_priv file private pointer.
L
Linus Torvalds 已提交
195 196 197
 * \param cmd command.
 * \param arg pointer to a drm_agp_buffer structure.
 * \return zero on success or a negative number on failure.
D
Dave Airlie 已提交
198
 *
L
Linus Torvalds 已提交
199
 * Verifies the AGP device is present and has been acquired, allocates the
D
Daniel Vetter 已提交
200
 * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
L
Linus Torvalds 已提交
201
 */
202
int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
L
Linus Torvalds 已提交
203
{
D
Dave Airlie 已提交
204
	struct drm_agp_mem *entry;
205
	struct agp_memory *memory;
D
Dave Airlie 已提交
206 207
	unsigned long pages;
	u32 type;
L
Linus Torvalds 已提交
208 209 210

	if (!dev->agp || !dev->agp->acquired)
		return -EINVAL;
211 212
	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
	if (!entry)
L
Linus Torvalds 已提交
213 214
		return -ENOMEM;

215
	pages = DIV_ROUND_UP(request->size, PAGE_SIZE);
216
	type = (u32) request->type;
217 218
	memory = agp_allocate_memory(dev->agp->bridge, pages, type);
	if (!memory) {
219
		kfree(entry);
L
Linus Torvalds 已提交
220 221 222
		return -ENOMEM;
	}

D
Dave Airlie 已提交
223 224 225 226
	entry->handle = (unsigned long)memory->key + 1;
	entry->memory = memory;
	entry->bound = 0;
	entry->pages = pages;
227
	list_add(&entry->head, &dev->agp->memory);
L
Linus Torvalds 已提交
228

229 230 231 232 233 234 235 236
	request->handle = entry->handle;
	request->physical = memory->physical;

	return 0;
}
EXPORT_SYMBOL(drm_agp_alloc);


237 238 239 240
int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
			struct drm_file *file_priv)
{
	struct drm_agp_buffer *request = data;
241

242
	return drm_agp_alloc(dev, request);
L
Linus Torvalds 已提交
243 244 245 246 247 248 249 250
}

/**
 * Search for the AGP memory entry associated with a handle.
 *
 * \param dev DRM device structure.
 * \param handle AGP memory handle.
 * \return pointer to the drm_agp_mem structure associated with \p handle.
D
Dave Airlie 已提交
251
 *
L
Linus Torvalds 已提交
252 253
 * Walks through drm_agp_head::memory until finding a matching handle.
 */
254 255
static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device *dev,
						unsigned long handle)
L
Linus Torvalds 已提交
256
{
D
Dave Airlie 已提交
257
	struct drm_agp_mem *entry;
L
Linus Torvalds 已提交
258

259
	list_for_each_entry(entry, &dev->agp->memory, head) {
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269
		if (entry->handle == handle)
			return entry;
	}
	return NULL;
}

/**
 * Unbind AGP memory from the GATT (ioctl).
 *
 * \param inode device inode.
270
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
271 272 273 274 275 276 277
 * \param cmd command.
 * \param arg pointer to a drm_agp_binding structure.
 * \return zero on success or a negative number on failure.
 *
 * Verifies the AGP device is present and acquired, looks-up the AGP memory
 * entry and passes it to the unbind_agp() function.
 */
278
int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
L
Linus Torvalds 已提交
279
{
D
Dave Airlie 已提交
280
	struct drm_agp_mem *entry;
L
Linus Torvalds 已提交
281 282 283 284
	int ret;

	if (!dev->agp || !dev->agp->acquired)
		return -EINVAL;
285 286
	entry = drm_agp_lookup_entry(dev, request->handle);
	if (!entry || !entry->bound)
L
Linus Torvalds 已提交
287 288 289
		return -EINVAL;
	ret = drm_unbind_agp(entry->memory);
	if (ret == 0)
D
Dave Airlie 已提交
290
		entry->bound = 0;
L
Linus Torvalds 已提交
291 292
	return ret;
}
293 294 295
EXPORT_SYMBOL(drm_agp_unbind);


296 297 298 299
int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
			 struct drm_file *file_priv)
{
	struct drm_agp_binding *request = data;
300

301
	return drm_agp_unbind(dev, request);
302
}
L
Linus Torvalds 已提交
303 304 305 306 307

/**
 * Bind AGP memory into the GATT (ioctl)
 *
 * \param inode device inode.
308
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
309 310 311 312 313 314 315 316
 * \param cmd command.
 * \param arg pointer to a drm_agp_binding structure.
 * \return zero on success or a negative number on failure.
 *
 * Verifies the AGP device is present and has been acquired and that no memory
 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
 * it to bind_agp() function.
 */
317
int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
L
Linus Torvalds 已提交
318
{
D
Dave Airlie 已提交
319
	struct drm_agp_mem *entry;
D
Dave Airlie 已提交
320 321
	int retcode;
	int page;
L
Linus Torvalds 已提交
322 323 324

	if (!dev->agp || !dev->agp->acquired)
		return -EINVAL;
325 326
	entry = drm_agp_lookup_entry(dev, request->handle);
	if (!entry || entry->bound)
L
Linus Torvalds 已提交
327
		return -EINVAL;
328
	page = DIV_ROUND_UP(request->offset, PAGE_SIZE);
329 330
	retcode = drm_bind_agp(entry->memory, page);
	if (retcode)
L
Linus Torvalds 已提交
331 332 333 334 335 336
		return retcode;
	entry->bound = dev->agp->base + (page << PAGE_SHIFT);
	DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
		  dev->agp->base, entry->bound);
	return 0;
}
337 338 339
EXPORT_SYMBOL(drm_agp_bind);


340 341 342 343
int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
{
	struct drm_agp_binding *request = data;
344

345
	return drm_agp_bind(dev, request);
346
}
L
Linus Torvalds 已提交
347 348 349 350 351

/**
 * Free AGP memory (ioctl).
 *
 * \param inode device inode.
352
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
353 354 355 356 357
 * \param cmd command.
 * \param arg pointer to a drm_agp_buffer structure.
 * \return zero on success or a negative number on failure.
 *
 * Verifies the AGP device is present and has been acquired and looks up the
358
 * AGP memory entry. If the memory is currently bound, unbind it via
L
Linus Torvalds 已提交
359 360 361
 * unbind_agp(). Frees it via free_agp() as well as the entry itself
 * and unlinks from the doubly linked list it's inserted in.
 */
362
int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
L
Linus Torvalds 已提交
363
{
D
Dave Airlie 已提交
364
	struct drm_agp_mem *entry;
L
Linus Torvalds 已提交
365 366 367

	if (!dev->agp || !dev->agp->acquired)
		return -EINVAL;
368 369
	entry = drm_agp_lookup_entry(dev, request->handle);
	if (!entry)
L
Linus Torvalds 已提交
370 371 372 373
		return -EINVAL;
	if (entry->bound)
		drm_unbind_agp(entry->memory);

374
	list_del(&entry->head);
L
Linus Torvalds 已提交
375 376

	drm_free_agp(entry->memory, entry->pages);
377
	kfree(entry);
L
Linus Torvalds 已提交
378 379
	return 0;
}
380 381 382
EXPORT_SYMBOL(drm_agp_free);


383 384 385 386 387 388
int drm_agp_free_ioctl(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
{
	struct drm_agp_buffer *request = data;

	return drm_agp_free(dev, request);
389
}
L
Linus Torvalds 已提交
390 391 392 393 394 395

/**
 * Initialize the AGP resources.
 *
 * \return pointer to a drm_agp_head structure.
 *
396 397 398
 * Gets the drm_agp_t structure which is made available by the agpgart module
 * via the inter_module_* functions. Creates and initializes a drm_agp_head
 * structure.
D
Daniel Vetter 已提交
399 400 401
 *
 * Note that final cleanup of the kmalloced structure is directly done in
 * drm_pci_agp_destroy.
L
Linus Torvalds 已提交
402
 */
D
Dave Airlie 已提交
403
struct drm_agp_head *drm_agp_init(struct drm_device *dev)
L
Linus Torvalds 已提交
404
{
D
Dave Airlie 已提交
405
	struct drm_agp_head *head = NULL;
L
Linus Torvalds 已提交
406

407 408
	head = kzalloc(sizeof(*head), GFP_KERNEL);
	if (!head)
L
Linus Torvalds 已提交
409 410 411
		return NULL;
	head->bridge = agp_find_bridge(dev->pdev);
	if (!head->bridge) {
412 413
		head->bridge = agp_backend_acquire(dev->pdev);
		if (!head->bridge) {
414
			kfree(head);
L
Linus Torvalds 已提交
415 416 417 418 419 420 421 422
			return NULL;
		}
		agp_copy_info(head->bridge, &head->agp_info);
		agp_backend_release(head->bridge);
	} else {
		agp_copy_info(head->bridge, &head->agp_info);
	}
	if (head->agp_info.chipset == NOT_SUPPORTED) {
423
		kfree(head);
L
Linus Torvalds 已提交
424 425
		return NULL;
	}
426
	INIT_LIST_HEAD(&head->memory);
L
Linus Torvalds 已提交
427 428
	head->cant_use_aperture = head->agp_info.cant_use_aperture;
	head->page_mask = head->agp_info.page_mask;
429
	head->base = head->agp_info.aper_base;
L
Linus Torvalds 已提交
430 431
	return head;
}
432 433
/* Only exported for i810.ko */
EXPORT_SYMBOL(drm_agp_init);
L
Linus Torvalds 已提交
434

435
/**
436
 * drm_legacy_agp_clear - Clear AGP resource list
437 438 439 440 441 442
 * @dev: DRM device
 *
 * Iterate over all AGP resources and remove them. But keep the AGP head
 * intact so it can still be used. It is safe to call this if AGP is disabled or
 * was already removed.
 *
443
 * Cleanup is only done for drivers who have DRIVER_LEGACY set.
444
 */
445
void drm_legacy_agp_clear(struct drm_device *dev)
446 447 448
{
	struct drm_agp_mem *entry, *tempe;

D
Daniel Vetter 已提交
449
	if (!dev->agp)
450
		return;
451
	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
		return;

	list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
		if (entry->bound)
			drm_unbind_agp(entry->memory);
		drm_free_agp(entry->memory, entry->pages);
		kfree(entry);
	}
	INIT_LIST_HEAD(&dev->agp->memory);

	if (dev->agp->acquired)
		drm_agp_release(dev);

	dev->agp->acquired = 0;
	dev->agp->enabled = 0;
}