backend.c 9.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 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
/*
 * AGPGART driver backend routines.
 * Copyright (C) 2004 Silicon Graphics, Inc.
 * Copyright (C) 2002-2003 Dave Jones.
 * Copyright (C) 1999 Jeff Hartmann.
 * Copyright (C) 1999 Precision Insight, Inc.
 * Copyright (C) 1999 Xi Graphics, Inc.
 *
 * 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 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
 * JEFF HARTMANN, DAVE JONES, OR ANY OTHER CONTRIBUTORS 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.
 *
 * TODO:
 * - Allocate more than order 0 pages to avoid too much linear map splitting.
 */
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/pagemap.h>
#include <linux/miscdevice.h>
#include <linux/pm.h>
#include <linux/agp_backend.h>
#include <linux/agpgart.h>
#include <linux/vmalloc.h>
#include <asm/io.h>
#include "agp.h"

/* Due to XFree86 brain-damage, we can't go to 1.0 until they
 * fix some real stupidity. It's only by chance we can bump
 * past 0.99 at all due to some boolean logic error. */
#define AGPGART_VERSION_MAJOR 0
46
#define AGPGART_VERSION_MINOR 103
47
static const struct agp_version agp_current_version =
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
{
	.major = AGPGART_VERSION_MAJOR,
	.minor = AGPGART_VERSION_MINOR,
};

struct agp_bridge_data *(*agp_find_bridge)(struct pci_dev *) =
	&agp_generic_find_bridge;

struct agp_bridge_data *agp_bridge;
LIST_HEAD(agp_bridges);
EXPORT_SYMBOL(agp_bridge);
EXPORT_SYMBOL(agp_bridges);
EXPORT_SYMBOL(agp_find_bridge);

/**
 *	agp_backend_acquire  -  attempt to acquire an agp backend.
 *
 */
struct agp_bridge_data *agp_backend_acquire(struct pci_dev *pdev)
{
	struct agp_bridge_data *bridge;

	bridge = agp_find_bridge(pdev);

	if (!bridge)
		return NULL;

	if (atomic_read(&bridge->agp_in_use))
		return NULL;
	atomic_inc(&bridge->agp_in_use);
	return bridge;
}
EXPORT_SYMBOL(agp_backend_acquire);


/**
 *	agp_backend_release  -  release the lock on the agp backend.
 *
 *	The caller must insure that the graphics aperture translation table
 *	is read for use by another entity.
 *
 *	(Ensure that all memory it bound is unbound.)
 */
void agp_backend_release(struct agp_bridge_data *bridge)
{

	if (bridge)
		atomic_dec(&bridge->agp_in_use);
}
EXPORT_SYMBOL(agp_backend_release);


D
Dave Jones 已提交
100
static const struct { int mem, agp; } maxes_table[] = {
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	{0, 0},
	{32, 4},
	{64, 28},
	{128, 96},
	{256, 204},
	{512, 440},
	{1024, 942},
	{2048, 1920},
	{4096, 3932}
};

static int agp_find_max(void)
{
	long memory, index, result;

#if PAGE_SHIFT < 20
117
	memory = totalram_pages >> (20 - PAGE_SHIFT);
L
Linus Torvalds 已提交
118
#else
119
	memory = totalram_pages << (PAGE_SHIFT - 20);
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
#endif
	index = 1;

	while ((memory > maxes_table[index].mem) && (index < 8))
		index++;

	result = maxes_table[index - 1].agp +
	   ( (memory - maxes_table[index - 1].mem)  *
	     (maxes_table[index].agp - maxes_table[index - 1].agp)) /
	   (maxes_table[index].mem - maxes_table[index - 1].mem);

	result = result << (20 - PAGE_SHIFT);
	return result;
}


static int agp_backend_initialize(struct agp_bridge_data *bridge)
{
	int size_value, rc, got_gatt=0, got_keylist=0;

	bridge->max_memory_agp = agp_find_max();
	bridge->version = &agp_current_version;

	if (bridge->driver->needs_scratch_page) {
144
		struct page *page = bridge->driver->agp_alloc_page(bridge);
L
Linus Torvalds 已提交
145

146
		if (!page) {
147 148
			dev_err(&bridge->dev->dev,
				"can't get memory for scratch page\n");
L
Linus Torvalds 已提交
149 150 151
			return -ENOMEM;
		}

152
		bridge->scratch_page_page = page;
153
		if (bridge->driver->agp_map_page) {
154
			if (bridge->driver->agp_map_page(page,
155 156 157 158 159 160 161
							 &bridge->scratch_page_dma)) {
				dev_err(&bridge->dev->dev,
					"unable to dma-map scratch page\n");
				rc = -ENOMEM;
				goto err_out_nounmap;
			}
		} else {
162
			bridge->scratch_page_dma = page_to_phys(page);
163
		}
164 165 166

		bridge->scratch_page = bridge->driver->mask_memory(bridge,
						   bridge->scratch_page_dma, 0);
L
Linus Torvalds 已提交
167 168 169 170
	}

	size_value = bridge->driver->fetch_size();
	if (size_value == 0) {
171
		dev_err(&bridge->dev->dev, "can't determine aperture size\n");
L
Linus Torvalds 已提交
172 173 174 175
		rc = -EINVAL;
		goto err_out;
	}
	if (bridge->driver->create_gatt_table(bridge)) {
176 177
		dev_err(&bridge->dev->dev,
			"can't get memory for graphics translation table\n");
L
Linus Torvalds 已提交
178 179 180 181 182 183 184
		rc = -ENOMEM;
		goto err_out;
	}
	got_gatt = 1;

	bridge->key_list = vmalloc(PAGE_SIZE * 4);
	if (bridge->key_list == NULL) {
185 186
		dev_err(&bridge->dev->dev,
			"can't allocate memory for key lists\n");
L
Linus Torvalds 已提交
187 188 189 190 191 192 193 194 195
		rc = -ENOMEM;
		goto err_out;
	}
	got_keylist = 1;

	/* FIXME vmalloc'd memory not guaranteed contiguous */
	memset(bridge->key_list, 0, PAGE_SIZE * 4);

	if (bridge->driver->configure()) {
196
		dev_err(&bridge->dev->dev, "error configuring host chipset\n");
L
Linus Torvalds 已提交
197 198 199
		rc = -EINVAL;
		goto err_out;
	}
K
Keith Packard 已提交
200 201
	INIT_LIST_HEAD(&bridge->mapped_list);
	spin_lock_init(&bridge->mapped_lock);
L
Linus Torvalds 已提交
202 203 204 205

	return 0;

err_out:
206 207
	if (bridge->driver->needs_scratch_page &&
	    bridge->driver->agp_unmap_page) {
208 209
		bridge->driver->agp_unmap_page(bridge->scratch_page_page,
					       bridge->scratch_page_dma);
210 211
	}
err_out_nounmap:
A
Alan Hourihane 已提交
212
	if (bridge->driver->needs_scratch_page) {
213
		void *va = page_address(bridge->scratch_page_page);
214 215 216

		bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_UNMAP);
		bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_FREE);
A
Alan Hourihane 已提交
217
	}
L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	if (got_gatt)
		bridge->driver->free_gatt_table(bridge);
	if (got_keylist) {
		vfree(bridge->key_list);
		bridge->key_list = NULL;
	}
	return rc;
}

/* cannot be __exit b/c as it could be called from __init code */
static void agp_backend_cleanup(struct agp_bridge_data *bridge)
{
	if (bridge->driver->cleanup)
		bridge->driver->cleanup();
	if (bridge->driver->free_gatt_table)
		bridge->driver->free_gatt_table(bridge);
234 235 236

	vfree(bridge->key_list);
	bridge->key_list = NULL;
L
Linus Torvalds 已提交
237 238

	if (bridge->driver->agp_destroy_page &&
A
Alan Hourihane 已提交
239
	    bridge->driver->needs_scratch_page) {
240
		void *va = page_address(bridge->scratch_page_page);
241

242
		if (bridge->driver->agp_unmap_page)
243 244
			bridge->driver->agp_unmap_page(bridge->scratch_page_page,
						       bridge->scratch_page_dma);
245

246 247
		bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_UNMAP);
		bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_FREE);
A
Alan Hourihane 已提交
248
	}
L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256
}

/* When we remove the global variable agp_bridge from all drivers
 * then agp_alloc_bridge and agp_generic_find_bridge need to be updated
 */

struct agp_bridge_data *agp_alloc_bridge(void)
{
257
	struct agp_bridge_data *bridge;
258

259
	bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	if (!bridge)
		return NULL;

	atomic_set(&bridge->agp_in_use, 0);
	atomic_set(&bridge->current_memory_agp, 0);

	if (list_empty(&agp_bridges))
		agp_bridge = bridge;

	return bridge;
}
EXPORT_SYMBOL(agp_alloc_bridge);


void agp_put_bridge(struct agp_bridge_data *bridge)
{
        kfree(bridge);

        if (list_empty(&agp_bridges))
                agp_bridge = NULL;
}
EXPORT_SYMBOL(agp_put_bridge);


int agp_add_bridge(struct agp_bridge_data *bridge)
{
	int error;

288 289 290 291
	if (agp_off) {
		error = -ENODEV;
		goto err_put_bridge;
	}
L
Linus Torvalds 已提交
292 293 294

	if (!bridge->dev) {
		printk (KERN_DEBUG PFX "Erk, registering with no pci_dev!\n");
295 296
		error = -EINVAL;
		goto err_put_bridge;
L
Linus Torvalds 已提交
297 298 299 300
	}

	/* Grab reference on the chipset driver. */
	if (!try_module_get(bridge->driver->owner)) {
301
		dev_info(&bridge->dev->dev, "can't lock chipset driver\n");
302 303
		error = -EINVAL;
		goto err_put_bridge;
L
Linus Torvalds 已提交
304 305 306 307
	}

	error = agp_backend_initialize(bridge);
	if (error) {
308 309
		dev_info(&bridge->dev->dev,
			 "agp_backend_initialize() failed\n");
L
Linus Torvalds 已提交
310 311 312 313 314 315
		goto err_out;
	}

	if (list_empty(&agp_bridges)) {
		error = agp_frontend_initialize();
		if (error) {
316 317
			dev_info(&bridge->dev->dev,
				 "agp_frontend_initialize() failed\n");
L
Linus Torvalds 已提交
318 319 320
			goto frontend_err;
		}

321 322
		dev_info(&bridge->dev->dev, "AGP aperture is %dM @ 0x%lx\n",
			 bridge->driver->fetch_size(), bridge->gart_bus_addr);
L
Linus Torvalds 已提交
323 324 325 326 327 328 329 330 331 332

	}

	list_add(&bridge->list, &agp_bridges);
	return 0;

frontend_err:
	agp_backend_cleanup(bridge);
err_out:
	module_put(bridge->driver->owner);
333
err_put_bridge:
L
Linus Torvalds 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	agp_put_bridge(bridge);
	return error;
}
EXPORT_SYMBOL_GPL(agp_add_bridge);


void agp_remove_bridge(struct agp_bridge_data *bridge)
{
	agp_backend_cleanup(bridge);
	list_del(&bridge->list);
	if (list_empty(&agp_bridges))
		agp_frontend_cleanup();
	module_put(bridge->driver->owner);
}
EXPORT_SYMBOL_GPL(agp_remove_bridge);

int agp_off;
int agp_try_unsupported_boot;
EXPORT_SYMBOL(agp_off);
EXPORT_SYMBOL(agp_try_unsupported_boot);

static int __init agp_init(void)
{
	if (!agp_off)
358
		printk(KERN_INFO "Linux agpgart interface v%d.%d\n",
L
Linus Torvalds 已提交
359 360 361 362
			AGPGART_VERSION_MAJOR, AGPGART_VERSION_MINOR);
	return 0;
}

A
Adrian Bunk 已提交
363
static void __exit agp_exit(void)
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
{
}

#ifndef MODULE
static __init int agp_setup(char *s)
{
	if (!strcmp(s,"off"))
		agp_off = 1;
	if (!strcmp(s,"try_unsupported"))
		agp_try_unsupported_boot = 1;
	return 1;
}
__setup("agp=", agp_setup);
#endif

D
Dave Jones 已提交
379
MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
L
Linus Torvalds 已提交
380 381 382 383 384 385 386
MODULE_DESCRIPTION("AGP GART driver");
MODULE_LICENSE("GPL and additional rights");
MODULE_ALIAS_MISCDEV(AGPGART_MINOR);

module_init(agp_init);
module_exit(agp_exit);