blob: dd370be00d689532b74f009142d8e3c71eb845db [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * AGPGART driver.
3 * Copyright (C) 2004 Silicon Graphics, Inc.
4 * Copyright (C) 2002-2005 Dave Jones.
5 * Copyright (C) 1999 Jeff Hartmann.
6 * Copyright (C) 1999 Precision Insight, Inc.
7 * Copyright (C) 1999 Xi Graphics, Inc.
8 *
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 shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * JEFF HARTMANN, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
25 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * TODO:
28 * - Allocate more than order 0 pages to avoid too much linear map splitting.
29 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/pci.h>
32#include <linux/init.h>
33#include <linux/pagemap.h>
34#include <linux/miscdevice.h>
35#include <linux/pm.h>
36#include <linux/agp_backend.h>
37#include <linux/vmalloc.h>
38#include <linux/dma-mapping.h>
39#include <linux/mm.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040040#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
42#include <asm/cacheflush.h>
43#include <asm/pgtable.h>
44#include "agp.h"
45
46__u32 *agp_gatt_table;
47int agp_memory_reserved;
48
49/*
50 * Needed by the Nforce GART driver for the time being. Would be
51 * nice to do this some other way instead of needing this export.
52 */
53EXPORT_SYMBOL_GPL(agp_memory_reserved);
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
56 * Generic routines for handling agp_memory structures -
57 * They use the basic page allocation routines to do the brunt of the work.
58 */
59
60void agp_free_key(int key)
61{
62 if (key < 0)
63 return;
64
65 if (key < MAXKEY)
66 clear_bit(key, agp_bridge->key_list);
67}
68EXPORT_SYMBOL(agp_free_key);
69
70
71static int agp_get_key(void)
72{
73 int bit;
74
75 bit = find_first_zero_bit(agp_bridge->key_list, MAXKEY);
76 if (bit < MAXKEY) {
77 set_bit(bit, agp_bridge->key_list);
78 return bit;
79 }
80 return -1;
81}
82
Dave Airliea13af4b2007-10-29 15:14:03 +100083void agp_flush_chipset(struct agp_bridge_data *bridge)
84{
85 if (bridge->driver->chipset_flush)
86 bridge->driver->chipset_flush(bridge);
87}
88EXPORT_SYMBOL(agp_flush_chipset);
89
Thomas Hellstroma030ce42007-01-23 10:33:43 +010090/*
91 * Use kmalloc if possible for the page list. Otherwise fall back to
92 * vmalloc. This speeds things up and also saves memory for small AGP
93 * regions.
94 */
95
96void agp_alloc_page_array(size_t size, struct agp_memory *mem)
97{
98 mem->memory = NULL;
Dave Airlie9516b032008-06-19 10:42:17 +100099 mem->vmalloc_flag = false;
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100100
Andrew Morton1c14cfb2007-02-05 16:09:35 -0800101 if (size <= 2*PAGE_SIZE)
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100102 mem->memory = kmalloc(size, GFP_KERNEL | __GFP_NORETRY);
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100103 if (mem->memory == NULL) {
104 mem->memory = vmalloc(size);
Dave Airlie9516b032008-06-19 10:42:17 +1000105 mem->vmalloc_flag = true;
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100106 }
107}
108EXPORT_SYMBOL(agp_alloc_page_array);
109
110void agp_free_page_array(struct agp_memory *mem)
111{
112 if (mem->vmalloc_flag) {
113 vfree(mem->memory);
114 } else {
115 kfree(mem->memory);
116 }
117}
118EXPORT_SYMBOL(agp_free_page_array);
119
120
121static struct agp_memory *agp_create_user_memory(unsigned long num_agp_pages)
122{
123 struct agp_memory *new;
124 unsigned long alloc_size = num_agp_pages*sizeof(struct page *);
125
Andrew Morton1c14cfb2007-02-05 16:09:35 -0800126 new = kzalloc(sizeof(struct agp_memory), GFP_KERNEL);
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100127 if (new == NULL)
128 return NULL;
129
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100130 new->key = agp_get_key();
131
132 if (new->key < 0) {
133 kfree(new);
134 return NULL;
135 }
136
137 agp_alloc_page_array(alloc_size, new);
138
139 if (new->memory == NULL) {
140 agp_free_key(new->key);
141 kfree(new);
142 return NULL;
143 }
144 new->num_scratch_pages = 0;
145 return new;
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148struct agp_memory *agp_create_memory(int scratch_pages)
149{
150 struct agp_memory *new;
151
Dave Jones0ea27d92005-10-20 15:12:16 -0700152 new = kzalloc(sizeof(struct agp_memory), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (new == NULL)
154 return NULL;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 new->key = agp_get_key();
157
158 if (new->key < 0) {
159 kfree(new);
160 return NULL;
161 }
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100162
163 agp_alloc_page_array(PAGE_SIZE * scratch_pages, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 if (new->memory == NULL) {
166 agp_free_key(new->key);
167 kfree(new);
168 return NULL;
169 }
170 new->num_scratch_pages = scratch_pages;
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100171 new->type = AGP_NORMAL_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return new;
173}
174EXPORT_SYMBOL(agp_create_memory);
175
176/**
177 * agp_free_memory - free memory associated with an agp_memory pointer.
178 *
179 * @curr: agp_memory pointer to be freed.
180 *
181 * It is the only function that can be called when the backend is not owned
182 * by the caller. (So it can free memory on client death.)
183 */
184void agp_free_memory(struct agp_memory *curr)
185{
186 size_t i;
187
188 if (curr == NULL)
189 return;
190
Joe Perchesc7258012008-03-26 14:10:02 -0700191 if (curr->is_bound)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 agp_unbind_memory(curr);
193
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100194 if (curr->type >= AGP_USER_TYPES) {
195 agp_generic_free_by_type(curr);
196 return;
197 }
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (curr->type != 0) {
200 curr->bridge->driver->free_by_type(curr);
201 return;
202 }
203 if (curr->page_count != 0) {
Shaohua Libd079282008-08-21 10:46:17 +0800204 if (curr->bridge->driver->agp_destroy_pages) {
205 curr->bridge->driver->agp_destroy_pages(curr);
206 } else {
207
208 for (i = 0; i < curr->page_count; i++) {
209 curr->memory[i] = (unsigned long)gart_to_virt(
210 curr->memory[i]);
211 curr->bridge->driver->agp_destroy_page(
212 (void *)curr->memory[i],
213 AGP_PAGE_DESTROY_UNMAP);
214 }
215 for (i = 0; i < curr->page_count; i++) {
216 curr->bridge->driver->agp_destroy_page(
217 (void *)curr->memory[i],
218 AGP_PAGE_DESTROY_FREE);
219 }
Dave Airliea2721e92007-10-15 10:19:16 +1000220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222 agp_free_key(curr->key);
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100223 agp_free_page_array(curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 kfree(curr);
225}
226EXPORT_SYMBOL(agp_free_memory);
227
228#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
229
230/**
231 * agp_allocate_memory - allocate a group of pages of a certain type.
232 *
233 * @page_count: size_t argument of the number of pages
234 * @type: u32 argument of the type of memory to be allocated.
235 *
236 * Every agp bridge device will allow you to allocate AGP_NORMAL_MEMORY which
237 * maps to physical ram. Any other type is device dependent.
238 *
239 * It returns NULL whenever memory is unavailable.
240 */
241struct agp_memory *agp_allocate_memory(struct agp_bridge_data *bridge,
242 size_t page_count, u32 type)
243{
244 int scratch_pages;
245 struct agp_memory *new;
246 size_t i;
247
248 if (!bridge)
249 return NULL;
250
251 if ((atomic_read(&bridge->current_memory_agp) + page_count) > bridge->max_memory_agp)
252 return NULL;
253
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100254 if (type >= AGP_USER_TYPES) {
255 new = agp_generic_alloc_user(page_count, type);
256 if (new)
257 new->bridge = bridge;
258 return new;
259 }
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (type != 0) {
262 new = bridge->driver->alloc_by_type(page_count, type);
263 if (new)
264 new->bridge = bridge;
265 return new;
266 }
267
268 scratch_pages = (page_count + ENTRIES_PER_PAGE - 1) / ENTRIES_PER_PAGE;
269
270 new = agp_create_memory(scratch_pages);
271
272 if (new == NULL)
273 return NULL;
274
Shaohua Li37acee12008-08-21 10:46:11 +0800275 if (bridge->driver->agp_alloc_pages) {
276 if (bridge->driver->agp_alloc_pages(bridge, new, page_count)) {
277 agp_free_memory(new);
278 return NULL;
279 }
280 new->bridge = bridge;
281 return new;
282 }
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 for (i = 0; i < page_count; i++) {
285 void *addr = bridge->driver->agp_alloc_page(bridge);
286
287 if (addr == NULL) {
288 agp_free_memory(new);
289 return NULL;
290 }
Keir Fraser07eee782005-03-30 13:17:04 -0800291 new->memory[i] = virt_to_gart(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 new->page_count++;
293 }
Alan Hourihane88d51962005-11-06 23:35:34 -0800294 new->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return new;
297}
298EXPORT_SYMBOL(agp_allocate_memory);
299
300
301/* End - Generic routines for handling agp_memory structures */
302
303
304static int agp_return_size(void)
305{
306 int current_size;
307 void *temp;
308
309 temp = agp_bridge->current_size;
310
311 switch (agp_bridge->driver->size_type) {
312 case U8_APER_SIZE:
313 current_size = A_SIZE_8(temp)->size;
314 break;
315 case U16_APER_SIZE:
316 current_size = A_SIZE_16(temp)->size;
317 break;
318 case U32_APER_SIZE:
319 current_size = A_SIZE_32(temp)->size;
320 break;
321 case LVL2_APER_SIZE:
322 current_size = A_SIZE_LVL2(temp)->size;
323 break;
324 case FIXED_APER_SIZE:
325 current_size = A_SIZE_FIX(temp)->size;
326 break;
327 default:
328 current_size = 0;
329 break;
330 }
331
332 current_size -= (agp_memory_reserved / (1024*1024));
333 if (current_size <0)
334 current_size = 0;
335 return current_size;
336}
337
338
339int agp_num_entries(void)
340{
341 int num_entries;
342 void *temp;
343
344 temp = agp_bridge->current_size;
345
346 switch (agp_bridge->driver->size_type) {
347 case U8_APER_SIZE:
348 num_entries = A_SIZE_8(temp)->num_entries;
349 break;
350 case U16_APER_SIZE:
351 num_entries = A_SIZE_16(temp)->num_entries;
352 break;
353 case U32_APER_SIZE:
354 num_entries = A_SIZE_32(temp)->num_entries;
355 break;
356 case LVL2_APER_SIZE:
357 num_entries = A_SIZE_LVL2(temp)->num_entries;
358 break;
359 case FIXED_APER_SIZE:
360 num_entries = A_SIZE_FIX(temp)->num_entries;
361 break;
362 default:
363 num_entries = 0;
364 break;
365 }
366
367 num_entries -= agp_memory_reserved>>PAGE_SHIFT;
368 if (num_entries<0)
369 num_entries = 0;
370 return num_entries;
371}
372EXPORT_SYMBOL_GPL(agp_num_entries);
373
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/**
376 * agp_copy_info - copy bridge state information
377 *
Dave Jones6a92a4e2006-02-28 00:54:25 -0500378 * @info: agp_kern_info pointer. The caller should insure that this pointer is valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 *
380 * This function copies information about the agp bridge device and the state of
381 * the agp backend into an agp_kern_info pointer.
382 */
383int agp_copy_info(struct agp_bridge_data *bridge, struct agp_kern_info *info)
384{
385 memset(info, 0, sizeof(struct agp_kern_info));
386 if (!bridge) {
387 info->chipset = NOT_SUPPORTED;
388 return -EIO;
389 }
390
391 info->version.major = bridge->version->major;
392 info->version.minor = bridge->version->minor;
393 info->chipset = SUPPORTED;
394 info->device = bridge->dev;
David Mosberger66bb8bf2005-04-04 13:29:43 -0700395 if (bridge->mode & AGPSTAT_MODE_3_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 info->mode = bridge->mode & ~AGP3_RESERVED_MASK;
397 else
398 info->mode = bridge->mode & ~AGP2_RESERVED_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 info->aper_base = bridge->gart_bus_addr;
400 info->aper_size = agp_return_size();
401 info->max_memory = bridge->max_memory_agp;
402 info->current_memory = atomic_read(&bridge->current_memory_agp);
403 info->cant_use_aperture = bridge->driver->cant_use_aperture;
404 info->vm_ops = bridge->vm_ops;
405 info->page_mask = ~0UL;
406 return 0;
407}
408EXPORT_SYMBOL(agp_copy_info);
409
410/* End - Routine to copy over information structure */
411
412/*
413 * Routines for handling swapping of agp_memory into the GATT -
414 * These routines take agp_memory and insert them into the GATT.
415 * They call device specific routines to actually write to the GATT.
416 */
417
418/**
419 * agp_bind_memory - Bind an agp_memory structure into the GATT.
420 *
421 * @curr: agp_memory pointer
422 * @pg_start: an offset into the graphics aperture translation table
423 *
424 * It returns -EINVAL if the pointer == NULL.
425 * It returns -EBUSY if the area of the table requested is already in use.
426 */
427int agp_bind_memory(struct agp_memory *curr, off_t pg_start)
428{
429 int ret_val;
430
431 if (curr == NULL)
432 return -EINVAL;
433
Joe Perchesc7258012008-03-26 14:10:02 -0700434 if (curr->is_bound) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700435 printk(KERN_INFO PFX "memory %p is already bound!\n", curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -EINVAL;
437 }
Joe Perchesc7258012008-03-26 14:10:02 -0700438 if (!curr->is_flushed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 curr->bridge->driver->cache_flush();
Joe Perchesc7258012008-03-26 14:10:02 -0700440 curr->is_flushed = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442 ret_val = curr->bridge->driver->insert_memory(curr, pg_start, curr->type);
443
444 if (ret_val != 0)
445 return ret_val;
446
Joe Perchesc7258012008-03-26 14:10:02 -0700447 curr->is_bound = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 curr->pg_start = pg_start;
449 return 0;
450}
451EXPORT_SYMBOL(agp_bind_memory);
452
453
454/**
455 * agp_unbind_memory - Removes an agp_memory structure from the GATT
456 *
457 * @curr: agp_memory pointer to be removed from the GATT.
458 *
459 * It returns -EINVAL if this piece of agp_memory is not currently bound to
460 * the graphics aperture translation table or if the agp_memory pointer == NULL
461 */
462int agp_unbind_memory(struct agp_memory *curr)
463{
464 int ret_val;
465
466 if (curr == NULL)
467 return -EINVAL;
468
Joe Perchesc7258012008-03-26 14:10:02 -0700469 if (!curr->is_bound) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700470 printk(KERN_INFO PFX "memory %p was not bound!\n", curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -EINVAL;
472 }
473
474 ret_val = curr->bridge->driver->remove_memory(curr, curr->pg_start, curr->type);
475
476 if (ret_val != 0)
477 return ret_val;
478
Joe Perchesc7258012008-03-26 14:10:02 -0700479 curr->is_bound = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 curr->pg_start = 0;
481 return 0;
482}
483EXPORT_SYMBOL(agp_unbind_memory);
484
485/* End - Routines for handling swapping of agp_memory into the GATT */
486
487
488/* Generic Agp routines - Start */
489static void agp_v2_parse_one(u32 *requested_mode, u32 *bridge_agpstat, u32 *vga_agpstat)
490{
491 u32 tmp;
492
493 if (*requested_mode & AGP2_RESERVED_MASK) {
Dave Jonesc4dd4582005-11-04 15:18:56 -0800494 printk(KERN_INFO PFX "reserved bits set (%x) in mode 0x%x. Fixed.\n",
495 *requested_mode & AGP2_RESERVED_MASK, *requested_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 *requested_mode &= ~AGP2_RESERVED_MASK;
497 }
498
Dave Jones28af24b2006-11-03 15:13:27 -0500499 /*
500 * Some dumb bridges are programmed to disobey the AGP2 spec.
501 * This is likely a BIOS misprogramming rather than poweron default, or
502 * it would be a lot more common.
503 * https://bugs.freedesktop.org/show_bug.cgi?id=8816
504 * AGPv2 spec 6.1.9 states:
505 * The RATE field indicates the data transfer rates supported by this
506 * device. A.G.P. devices must report all that apply.
507 * Fix them up as best we can.
508 */
509 switch (*bridge_agpstat & 7) {
510 case 4:
511 *bridge_agpstat |= (AGPSTAT2_2X | AGPSTAT2_1X);
512 printk(KERN_INFO PFX "BIOS bug. AGP bridge claims to only support x4 rate"
513 "Fixing up support for x2 & x1\n");
514 break;
515 case 2:
516 *bridge_agpstat |= AGPSTAT2_1X;
517 printk(KERN_INFO PFX "BIOS bug. AGP bridge claims to only support x2 rate"
518 "Fixing up support for x1\n");
519 break;
520 default:
521 break;
522 }
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* Check the speed bits make sense. Only one should be set. */
525 tmp = *requested_mode & 7;
526 switch (tmp) {
527 case 0:
Dave Jones8c8b8382005-08-17 23:08:11 -0700528 printk(KERN_INFO PFX "%s tried to set rate=x0. Setting to x1 mode.\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 *requested_mode |= AGPSTAT2_1X;
530 break;
531 case 1:
532 case 2:
533 break;
534 case 3:
535 *requested_mode &= ~(AGPSTAT2_1X); /* rate=2 */
536 break;
537 case 4:
538 break;
539 case 5:
540 case 6:
541 case 7:
542 *requested_mode &= ~(AGPSTAT2_1X|AGPSTAT2_2X); /* rate=4*/
543 break;
544 }
545
546 /* disable SBA if it's not supported */
547 if (!((*bridge_agpstat & AGPSTAT_SBA) && (*vga_agpstat & AGPSTAT_SBA) && (*requested_mode & AGPSTAT_SBA)))
548 *bridge_agpstat &= ~AGPSTAT_SBA;
549
550 /* Set rate */
551 if (!((*bridge_agpstat & AGPSTAT2_4X) && (*vga_agpstat & AGPSTAT2_4X) && (*requested_mode & AGPSTAT2_4X)))
552 *bridge_agpstat &= ~AGPSTAT2_4X;
553
554 if (!((*bridge_agpstat & AGPSTAT2_2X) && (*vga_agpstat & AGPSTAT2_2X) && (*requested_mode & AGPSTAT2_2X)))
555 *bridge_agpstat &= ~AGPSTAT2_2X;
556
557 if (!((*bridge_agpstat & AGPSTAT2_1X) && (*vga_agpstat & AGPSTAT2_1X) && (*requested_mode & AGPSTAT2_1X)))
558 *bridge_agpstat &= ~AGPSTAT2_1X;
559
560 /* Now we know what mode it should be, clear out the unwanted bits. */
561 if (*bridge_agpstat & AGPSTAT2_4X)
562 *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_2X); /* 4X */
563
564 if (*bridge_agpstat & AGPSTAT2_2X)
565 *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_4X); /* 2X */
566
567 if (*bridge_agpstat & AGPSTAT2_1X)
568 *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X); /* 1X */
569
570 /* Apply any errata. */
571 if (agp_bridge->flags & AGP_ERRATA_FASTWRITES)
572 *bridge_agpstat &= ~AGPSTAT_FW;
573
574 if (agp_bridge->flags & AGP_ERRATA_SBA)
575 *bridge_agpstat &= ~AGPSTAT_SBA;
576
577 if (agp_bridge->flags & AGP_ERRATA_1X) {
578 *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X);
579 *bridge_agpstat |= AGPSTAT2_1X;
580 }
581
582 /* If we've dropped down to 1X, disable fast writes. */
583 if (*bridge_agpstat & AGPSTAT2_1X)
584 *bridge_agpstat &= ~AGPSTAT_FW;
585}
586
587/*
588 * requested_mode = Mode requested by (typically) X.
589 * bridge_agpstat = PCI_AGP_STATUS from agp bridge.
590 * vga_agpstat = PCI_AGP_STATUS from graphic card.
591 */
592static void agp_v3_parse_one(u32 *requested_mode, u32 *bridge_agpstat, u32 *vga_agpstat)
593{
594 u32 origbridge=*bridge_agpstat, origvga=*vga_agpstat;
595 u32 tmp;
596
597 if (*requested_mode & AGP3_RESERVED_MASK) {
Dave Jonesc4dd4582005-11-04 15:18:56 -0800598 printk(KERN_INFO PFX "reserved bits set (%x) in mode 0x%x. Fixed.\n",
599 *requested_mode & AGP3_RESERVED_MASK, *requested_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *requested_mode &= ~AGP3_RESERVED_MASK;
601 }
602
603 /* Check the speed bits make sense. */
604 tmp = *requested_mode & 7;
605 if (tmp == 0) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700606 printk(KERN_INFO PFX "%s tried to set rate=x0. Setting to AGP3 x4 mode.\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 *requested_mode |= AGPSTAT3_4X;
608 }
609 if (tmp >= 3) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700610 printk(KERN_INFO PFX "%s tried to set rate=x%d. Setting to AGP3 x8 mode.\n", current->comm, tmp * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 *requested_mode = (*requested_mode & ~7) | AGPSTAT3_8X;
612 }
613
614 /* ARQSZ - Set the value to the maximum one.
615 * Don't allow the mode register to override values. */
616 *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_ARQSZ) |
617 max_t(u32,(*bridge_agpstat & AGPSTAT_ARQSZ),(*vga_agpstat & AGPSTAT_ARQSZ)));
618
619 /* Calibration cycle.
620 * Don't allow the mode register to override values. */
621 *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_CAL_MASK) |
622 min_t(u32,(*bridge_agpstat & AGPSTAT_CAL_MASK),(*vga_agpstat & AGPSTAT_CAL_MASK)));
623
624 /* SBA *must* be supported for AGP v3 */
625 *bridge_agpstat |= AGPSTAT_SBA;
626
627 /*
628 * Set speed.
629 * Check for invalid speeds. This can happen when applications
630 * written before the AGP 3.0 standard pass AGP2.x modes to AGP3 hardware
631 */
632 if (*requested_mode & AGPSTAT_MODE_3_0) {
633 /*
634 * Caller hasn't a clue what it is doing. Bridge is in 3.0 mode,
635 * have been passed a 3.0 mode, but with 2.x speed bits set.
636 * AGP2.x 4x -> AGP3.0 4x.
637 */
638 if (*requested_mode & AGPSTAT2_4X) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700639 printk(KERN_INFO PFX "%s passes broken AGP3 flags (%x). Fixed.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 current->comm, *requested_mode);
641 *requested_mode &= ~AGPSTAT2_4X;
642 *requested_mode |= AGPSTAT3_4X;
643 }
644 } else {
645 /*
646 * The caller doesn't know what they are doing. We are in 3.0 mode,
647 * but have been passed an AGP 2.x mode.
648 * Convert AGP 1x,2x,4x -> AGP 3.0 4x.
649 */
Dave Jones8c8b8382005-08-17 23:08:11 -0700650 printk(KERN_INFO PFX "%s passes broken AGP2 flags (%x) in AGP3 mode. Fixed.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 current->comm, *requested_mode);
652 *requested_mode &= ~(AGPSTAT2_4X | AGPSTAT2_2X | AGPSTAT2_1X);
653 *requested_mode |= AGPSTAT3_4X;
654 }
655
656 if (*requested_mode & AGPSTAT3_8X) {
657 if (!(*bridge_agpstat & AGPSTAT3_8X)) {
658 *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
659 *bridge_agpstat |= AGPSTAT3_4X;
Dave Jones8c8b8382005-08-17 23:08:11 -0700660 printk(KERN_INFO PFX "%s requested AGPx8 but bridge not capable.\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return;
662 }
663 if (!(*vga_agpstat & AGPSTAT3_8X)) {
664 *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
665 *bridge_agpstat |= AGPSTAT3_4X;
Dave Jones8c8b8382005-08-17 23:08:11 -0700666 printk(KERN_INFO PFX "%s requested AGPx8 but graphic card not capable.\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return;
668 }
669 /* All set, bridge & device can do AGP x8*/
670 *bridge_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD);
671 goto done;
672
Dave Jonesedf03fb2006-09-10 21:12:20 -0400673 } else if (*requested_mode & AGPSTAT3_4X) {
674 *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
675 *bridge_agpstat |= AGPSTAT3_4X;
676 goto done;
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 } else {
679
680 /*
Dave Jonesedf03fb2006-09-10 21:12:20 -0400681 * If we didn't specify an AGP mode, we see if both
682 * the graphics card, and the bridge can do x8, and use if so.
683 * If not, we fall back to x4 mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
Dave Jonesedf03fb2006-09-10 21:12:20 -0400685 if ((*bridge_agpstat & AGPSTAT3_8X) && (*vga_agpstat & AGPSTAT3_8X)) {
Dave Jones2cc1a412006-09-28 19:50:07 -0400686 printk(KERN_INFO PFX "No AGP mode specified. Setting to highest mode "
687 "supported by bridge & card (x8).\n");
Dave Jonesedf03fb2006-09-10 21:12:20 -0400688 *bridge_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD);
689 *vga_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD);
690 } else {
691 printk(KERN_INFO PFX "Fell back to AGPx4 mode because");
692 if (!(*bridge_agpstat & AGPSTAT3_8X)) {
Dave Jones2cc1a412006-09-28 19:50:07 -0400693 printk(KERN_INFO PFX "bridge couldn't do x8. bridge_agpstat:%x (orig=%x)\n",
694 *bridge_agpstat, origbridge);
Dave Jonesedf03fb2006-09-10 21:12:20 -0400695 *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
696 *bridge_agpstat |= AGPSTAT3_4X;
697 }
698 if (!(*vga_agpstat & AGPSTAT3_8X)) {
Dave Jones2cc1a412006-09-28 19:50:07 -0400699 printk(KERN_INFO PFX "graphics card couldn't do x8. vga_agpstat:%x (orig=%x)\n",
700 *vga_agpstat, origvga);
Dave Jonesedf03fb2006-09-10 21:12:20 -0400701 *vga_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
702 *vga_agpstat |= AGPSTAT3_4X;
703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 }
706
707done:
708 /* Apply any errata. */
709 if (agp_bridge->flags & AGP_ERRATA_FASTWRITES)
710 *bridge_agpstat &= ~AGPSTAT_FW;
711
712 if (agp_bridge->flags & AGP_ERRATA_SBA)
713 *bridge_agpstat &= ~AGPSTAT_SBA;
714
715 if (agp_bridge->flags & AGP_ERRATA_1X) {
716 *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X);
717 *bridge_agpstat |= AGPSTAT2_1X;
718 }
719}
720
721
722/**
723 * agp_collect_device_status - determine correct agp_cmd from various agp_stat's
724 * @bridge: an agp_bridge_data struct allocated for the AGP host bridge.
725 * @requested_mode: requested agp_stat from userspace (Typically from X)
726 * @bridge_agpstat: current agp_stat from AGP bridge.
727 *
728 * This function will hunt for an AGP graphics card, and try to match
729 * the requested mode to the capabilities of both the bridge and the card.
730 */
731u32 agp_collect_device_status(struct agp_bridge_data *bridge, u32 requested_mode, u32 bridge_agpstat)
732{
733 struct pci_dev *device = NULL;
734 u32 vga_agpstat;
735 u8 cap_ptr;
736
737 for (;;) {
738 device = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, device);
739 if (!device) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700740 printk(KERN_INFO PFX "Couldn't find an AGP VGA controller.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return 0;
742 }
743 cap_ptr = pci_find_capability(device, PCI_CAP_ID_AGP);
744 if (cap_ptr)
745 break;
746 }
747
748 /*
749 * Ok, here we have a AGP device. Disable impossible
750 * settings, and adjust the readqueue to the minimum.
751 */
752 pci_read_config_dword(device, cap_ptr+PCI_AGP_STATUS, &vga_agpstat);
753
754 /* adjust RQ depth */
755 bridge_agpstat = ((bridge_agpstat & ~AGPSTAT_RQ_DEPTH) |
756 min_t(u32, (requested_mode & AGPSTAT_RQ_DEPTH),
757 min_t(u32, (bridge_agpstat & AGPSTAT_RQ_DEPTH), (vga_agpstat & AGPSTAT_RQ_DEPTH))));
758
759 /* disable FW if it's not supported */
760 if (!((bridge_agpstat & AGPSTAT_FW) &&
761 (vga_agpstat & AGPSTAT_FW) &&
762 (requested_mode & AGPSTAT_FW)))
763 bridge_agpstat &= ~AGPSTAT_FW;
764
765 /* Check to see if we are operating in 3.0 mode */
David Mosberger66bb8bf2005-04-04 13:29:43 -0700766 if (agp_bridge->mode & AGPSTAT_MODE_3_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 agp_v3_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat);
768 else
769 agp_v2_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat);
770
771 pci_dev_put(device);
772 return bridge_agpstat;
773}
774EXPORT_SYMBOL(agp_collect_device_status);
775
776
Joe Perchesc7258012008-03-26 14:10:02 -0700777void agp_device_command(u32 bridge_agpstat, bool agp_v3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 struct pci_dev *device = NULL;
780 int mode;
781
782 mode = bridge_agpstat & 0x7;
783 if (agp_v3)
784 mode *= 4;
785
786 for_each_pci_dev(device) {
787 u8 agp = pci_find_capability(device, PCI_CAP_ID_AGP);
788 if (!agp)
789 continue;
790
791 printk(KERN_INFO PFX "Putting AGP V%d device at %s into %dx mode\n",
792 agp_v3 ? 3 : 2, pci_name(device), mode);
793 pci_write_config_dword(device, agp + PCI_AGP_COMMAND, bridge_agpstat);
794 }
795}
796EXPORT_SYMBOL(agp_device_command);
797
798
799void get_agp_version(struct agp_bridge_data *bridge)
800{
801 u32 ncapid;
802
803 /* Exit early if already set by errata workarounds. */
804 if (bridge->major_version != 0)
805 return;
806
807 pci_read_config_dword(bridge->dev, bridge->capndx, &ncapid);
808 bridge->major_version = (ncapid >> AGP_MAJOR_VERSION_SHIFT) & 0xf;
809 bridge->minor_version = (ncapid >> AGP_MINOR_VERSION_SHIFT) & 0xf;
810}
811EXPORT_SYMBOL(get_agp_version);
812
813
814void agp_generic_enable(struct agp_bridge_data *bridge, u32 requested_mode)
815{
816 u32 bridge_agpstat, temp;
817
818 get_agp_version(agp_bridge);
819
820 printk(KERN_INFO PFX "Found an AGP %d.%d compliant device at %s.\n",
821 agp_bridge->major_version,
822 agp_bridge->minor_version,
823 pci_name(agp_bridge->dev));
824
825 pci_read_config_dword(agp_bridge->dev,
826 agp_bridge->capndx + PCI_AGP_STATUS, &bridge_agpstat);
827
828 bridge_agpstat = agp_collect_device_status(agp_bridge, requested_mode, bridge_agpstat);
829 if (bridge_agpstat == 0)
830 /* Something bad happened. FIXME: Return error code? */
831 return;
832
833 bridge_agpstat |= AGPSTAT_AGP_ENABLE;
834
835 /* Do AGP version specific frobbing. */
836 if (bridge->major_version >= 3) {
David Mosberger66bb8bf2005-04-04 13:29:43 -0700837 if (bridge->mode & AGPSTAT_MODE_3_0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* If we have 3.5, we can do the isoch stuff. */
839 if (bridge->minor_version >= 5)
840 agp_3_5_enable(bridge);
Joe Perchesc7258012008-03-26 14:10:02 -0700841 agp_device_command(bridge_agpstat, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return;
843 } else {
844 /* Disable calibration cycle in RX91<1> when not in AGP3.0 mode of operation.*/
845 bridge_agpstat &= ~(7<<10) ;
846 pci_read_config_dword(bridge->dev,
847 bridge->capndx+AGPCTRL, &temp);
848 temp |= (1<<9);
849 pci_write_config_dword(bridge->dev,
850 bridge->capndx+AGPCTRL, temp);
851
Dave Jones8c8b8382005-08-17 23:08:11 -0700852 printk(KERN_INFO PFX "Device is in legacy mode,"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 " falling back to 2.x\n");
854 }
855 }
856
857 /* AGP v<3 */
Joe Perchesc7258012008-03-26 14:10:02 -0700858 agp_device_command(bridge_agpstat, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860EXPORT_SYMBOL(agp_generic_enable);
861
862
863int agp_generic_create_gatt_table(struct agp_bridge_data *bridge)
864{
865 char *table;
866 char *table_end;
867 int size;
868 int page_order;
869 int num_entries;
870 int i;
871 void *temp;
872 struct page *page;
873
874 /* The generic routines can't handle 2 level gatt's */
875 if (bridge->driver->size_type == LVL2_APER_SIZE)
876 return -EINVAL;
877
878 table = NULL;
879 i = bridge->aperture_size_idx;
880 temp = bridge->current_size;
881 size = page_order = num_entries = 0;
882
883 if (bridge->driver->size_type != FIXED_APER_SIZE) {
884 do {
885 switch (bridge->driver->size_type) {
886 case U8_APER_SIZE:
887 size = A_SIZE_8(temp)->size;
888 page_order =
889 A_SIZE_8(temp)->page_order;
890 num_entries =
891 A_SIZE_8(temp)->num_entries;
892 break;
893 case U16_APER_SIZE:
894 size = A_SIZE_16(temp)->size;
895 page_order = A_SIZE_16(temp)->page_order;
896 num_entries = A_SIZE_16(temp)->num_entries;
897 break;
898 case U32_APER_SIZE:
899 size = A_SIZE_32(temp)->size;
900 page_order = A_SIZE_32(temp)->page_order;
901 num_entries = A_SIZE_32(temp)->num_entries;
902 break;
903 /* This case will never really happen. */
904 case FIXED_APER_SIZE:
905 case LVL2_APER_SIZE:
906 default:
907 size = page_order = num_entries = 0;
908 break;
909 }
910
Keir Fraser07eee782005-03-30 13:17:04 -0800911 table = alloc_gatt_pages(page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 if (table == NULL) {
914 i++;
915 switch (bridge->driver->size_type) {
916 case U8_APER_SIZE:
917 bridge->current_size = A_IDX8(bridge);
918 break;
919 case U16_APER_SIZE:
920 bridge->current_size = A_IDX16(bridge);
921 break;
922 case U32_APER_SIZE:
923 bridge->current_size = A_IDX32(bridge);
924 break;
Dave Jones89197e32006-05-30 18:19:39 -0400925 /* These cases will never really happen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 case FIXED_APER_SIZE:
927 case LVL2_APER_SIZE:
928 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 break;
930 }
931 temp = bridge->current_size;
932 } else {
933 bridge->aperture_size_idx = i;
934 }
935 } while (!table && (i < bridge->driver->num_aperture_sizes));
936 } else {
937 size = ((struct aper_size_info_fixed *) temp)->size;
938 page_order = ((struct aper_size_info_fixed *) temp)->page_order;
939 num_entries = ((struct aper_size_info_fixed *) temp)->num_entries;
Keir Fraser07eee782005-03-30 13:17:04 -0800940 table = alloc_gatt_pages(page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
943 if (table == NULL)
944 return -ENOMEM;
945
946 table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
947
948 for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
949 SetPageReserved(page);
950
951 bridge->gatt_table_real = (u32 *) table;
952 agp_gatt_table = (void *)table;
953
954 bridge->driver->cache_flush();
Arjan van dev Venfcea4242008-02-06 05:16:00 +0100955#ifdef CONFIG_X86
956 set_memory_uc((unsigned long)table, 1 << page_order);
957 bridge->gatt_table = (void *)table;
958#else
Keir Fraser07eee782005-03-30 13:17:04 -0800959 bridge->gatt_table = ioremap_nocache(virt_to_gart(table),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 (PAGE_SIZE * (1 << page_order)));
961 bridge->driver->cache_flush();
Arjan van dev Venfcea4242008-02-06 05:16:00 +0100962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (bridge->gatt_table == NULL) {
965 for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
966 ClearPageReserved(page);
967
Keir Fraser07eee782005-03-30 13:17:04 -0800968 free_gatt_pages(table, page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 return -ENOMEM;
971 }
Keir Fraser07eee782005-03-30 13:17:04 -0800972 bridge->gatt_bus_addr = virt_to_gart(bridge->gatt_table_real);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 /* AK: bogus, should encode addresses > 4GB */
975 for (i = 0; i < num_entries; i++) {
976 writel(bridge->scratch_page, bridge->gatt_table+i);
977 readl(bridge->gatt_table+i); /* PCI Posting. */
978 }
979
980 return 0;
981}
982EXPORT_SYMBOL(agp_generic_create_gatt_table);
983
984int agp_generic_free_gatt_table(struct agp_bridge_data *bridge)
985{
986 int page_order;
987 char *table, *table_end;
988 void *temp;
989 struct page *page;
990
991 temp = bridge->current_size;
992
993 switch (bridge->driver->size_type) {
994 case U8_APER_SIZE:
995 page_order = A_SIZE_8(temp)->page_order;
996 break;
997 case U16_APER_SIZE:
998 page_order = A_SIZE_16(temp)->page_order;
999 break;
1000 case U32_APER_SIZE:
1001 page_order = A_SIZE_32(temp)->page_order;
1002 break;
1003 case FIXED_APER_SIZE:
1004 page_order = A_SIZE_FIX(temp)->page_order;
1005 break;
1006 case LVL2_APER_SIZE:
1007 /* The generic routines can't deal with 2 level gatt's */
1008 return -EINVAL;
1009 break;
1010 default:
1011 page_order = 0;
1012 break;
1013 }
1014
1015 /* Do not worry about freeing memory, because if this is
1016 * called, then all agp memory is deallocated and removed
1017 * from the table. */
1018
Arjan van dev Venfcea4242008-02-06 05:16:00 +01001019#ifdef CONFIG_X86
1020 set_memory_wb((unsigned long)bridge->gatt_table, 1 << page_order);
1021#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 iounmap(bridge->gatt_table);
Arjan van dev Venfcea4242008-02-06 05:16:00 +01001023#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 table = (char *) bridge->gatt_table_real;
1025 table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
1026
1027 for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
1028 ClearPageReserved(page);
1029
Keir Fraser07eee782005-03-30 13:17:04 -08001030 free_gatt_pages(bridge->gatt_table_real, page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 agp_gatt_table = NULL;
1033 bridge->gatt_table = NULL;
1034 bridge->gatt_table_real = NULL;
1035 bridge->gatt_bus_addr = 0;
1036
1037 return 0;
1038}
1039EXPORT_SYMBOL(agp_generic_free_gatt_table);
1040
1041
1042int agp_generic_insert_memory(struct agp_memory * mem, off_t pg_start, int type)
1043{
1044 int num_entries;
1045 size_t i;
1046 off_t j;
1047 void *temp;
1048 struct agp_bridge_data *bridge;
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001049 int mask_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 bridge = mem->bridge;
1052 if (!bridge)
1053 return -EINVAL;
1054
Thomas Hellstrom5aa80c72006-12-20 16:33:41 +01001055 if (mem->page_count == 0)
1056 return 0;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 temp = bridge->current_size;
1059
1060 switch (bridge->driver->size_type) {
1061 case U8_APER_SIZE:
1062 num_entries = A_SIZE_8(temp)->num_entries;
1063 break;
1064 case U16_APER_SIZE:
1065 num_entries = A_SIZE_16(temp)->num_entries;
1066 break;
1067 case U32_APER_SIZE:
1068 num_entries = A_SIZE_32(temp)->num_entries;
1069 break;
1070 case FIXED_APER_SIZE:
1071 num_entries = A_SIZE_FIX(temp)->num_entries;
1072 break;
1073 case LVL2_APER_SIZE:
1074 /* The generic routines can't deal with 2 level gatt's */
1075 return -EINVAL;
1076 break;
1077 default:
1078 num_entries = 0;
1079 break;
1080 }
1081
1082 num_entries -= agp_memory_reserved/PAGE_SIZE;
1083 if (num_entries < 0) num_entries = 0;
1084
Andrew Morton1c14cfb2007-02-05 16:09:35 -08001085 if (type != mem->type)
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001086 return -EINVAL;
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001087
1088 mask_type = bridge->driver->agp_type_to_mask_type(bridge, type);
1089 if (mask_type != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 /* The generic routines know nothing of memory types */
1091 return -EINVAL;
1092 }
1093
1094 /* AK: could wrap */
1095 if ((pg_start + mem->page_count) > num_entries)
1096 return -EINVAL;
1097
1098 j = pg_start;
1099
1100 while (j < (pg_start + mem->page_count)) {
1101 if (!PGE_EMPTY(bridge, readl(bridge->gatt_table+j)))
1102 return -EBUSY;
1103 j++;
1104 }
1105
Joe Perchesc7258012008-03-26 14:10:02 -07001106 if (!mem->is_flushed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 bridge->driver->cache_flush();
Joe Perchesc7258012008-03-26 14:10:02 -07001108 mem->is_flushed = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
1111 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001112 writel(bridge->driver->mask_memory(bridge, mem->memory[i], mask_type),
1113 bridge->gatt_table+j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
Thomas Hellstrom5aa80c72006-12-20 16:33:41 +01001115 readl(bridge->gatt_table+j-1); /* PCI Posting. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 bridge->driver->tlb_flush(mem);
1118 return 0;
1119}
1120EXPORT_SYMBOL(agp_generic_insert_memory);
1121
1122
1123int agp_generic_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
1124{
1125 size_t i;
1126 struct agp_bridge_data *bridge;
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001127 int mask_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 bridge = mem->bridge;
1130 if (!bridge)
1131 return -EINVAL;
1132
Thomas Hellstrom5aa80c72006-12-20 16:33:41 +01001133 if (mem->page_count == 0)
1134 return 0;
1135
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001136 if (type != mem->type)
1137 return -EINVAL;
1138
1139 mask_type = bridge->driver->agp_type_to_mask_type(bridge, type);
1140 if (mask_type != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /* The generic routines know nothing of memory types */
1142 return -EINVAL;
1143 }
1144
1145 /* AK: bogus, should encode addresses > 4GB */
1146 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
1147 writel(bridge->scratch_page, bridge->gatt_table+i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Thomas Hellstrom5aa80c72006-12-20 16:33:41 +01001149 readl(bridge->gatt_table+i-1); /* PCI Posting. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 bridge->driver->tlb_flush(mem);
1152 return 0;
1153}
1154EXPORT_SYMBOL(agp_generic_remove_memory);
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156struct agp_memory *agp_generic_alloc_by_type(size_t page_count, int type)
1157{
1158 return NULL;
1159}
1160EXPORT_SYMBOL(agp_generic_alloc_by_type);
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162void agp_generic_free_by_type(struct agp_memory *curr)
1163{
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001164 agp_free_page_array(curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 agp_free_key(curr->key);
1166 kfree(curr);
1167}
1168EXPORT_SYMBOL(agp_generic_free_by_type);
1169
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001170struct agp_memory *agp_generic_alloc_user(size_t page_count, int type)
1171{
1172 struct agp_memory *new;
1173 int i;
1174 int pages;
1175
1176 pages = (page_count + ENTRIES_PER_PAGE - 1) / ENTRIES_PER_PAGE;
1177 new = agp_create_user_memory(page_count);
1178 if (new == NULL)
1179 return NULL;
1180
Andrew Morton1c14cfb2007-02-05 16:09:35 -08001181 for (i = 0; i < page_count; i++)
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001182 new->memory[i] = 0;
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001183 new->page_count = 0;
1184 new->type = type;
1185 new->num_scratch_pages = pages;
1186
1187 return new;
1188}
1189EXPORT_SYMBOL(agp_generic_alloc_user);
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191/*
1192 * Basic Page Allocation Routines -
1193 * These routines handle page allocation and by default they reserve the allocated
1194 * memory. They also handle incrementing the current_memory_agp value, Which is checked
1195 * against a maximum value.
1196 */
1197
Shaohua Li37acee12008-08-21 10:46:11 +08001198int agp_generic_alloc_pages(struct agp_bridge_data *bridge, struct agp_memory *mem, size_t num_pages)
1199{
1200 struct page * page;
1201 int i, ret = -ENOMEM;
1202
1203 for (i = 0; i < num_pages; i++) {
1204 page = alloc_page(GFP_KERNEL | GFP_DMA32);
1205 /* agp_free_memory() needs gart address */
1206 if (page == NULL)
1207 goto out;
1208
1209#ifndef CONFIG_X86
1210 map_page_into_agp(page);
1211#endif
1212 get_page(page);
1213 atomic_inc(&agp_bridge->current_memory_agp);
1214
1215 /* set_memory_array_uc() needs virtual address */
1216 mem->memory[i] = (unsigned long)page_address(page);
1217 mem->page_count++;
1218 }
1219
1220#ifdef CONFIG_X86
1221 set_memory_array_uc(mem->memory, num_pages);
1222#endif
1223 ret = 0;
1224out:
1225 for (i = 0; i < mem->page_count; i++)
1226 mem->memory[i] = virt_to_gart((void *)mem->memory[i]);
1227 return ret;
1228}
1229EXPORT_SYMBOL(agp_generic_alloc_pages);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231void *agp_generic_alloc_page(struct agp_bridge_data *bridge)
1232{
1233 struct page * page;
1234
Linus Torvalds66c669b2006-11-22 14:55:29 -08001235 page = alloc_page(GFP_KERNEL | GFP_DMA32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (page == NULL)
1237 return NULL;
1238
Ingo Molnar9326d612008-08-21 13:46:25 +02001239 map_page_into_agp(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 atomic_inc(&agp_bridge->current_memory_agp);
1243 return page_address(page);
1244}
1245EXPORT_SYMBOL(agp_generic_alloc_page);
1246
Shaohua Libd079282008-08-21 10:46:17 +08001247void agp_generic_destroy_pages(struct agp_memory *mem)
1248{
1249 int i;
1250 void *addr;
1251 struct page *page;
1252
1253 if (!mem)
1254 return;
1255
1256 for (i = 0; i < mem->page_count; i++)
1257 mem->memory[i] = (unsigned long)gart_to_virt(mem->memory[i]);
1258
1259#ifdef CONFIG_X86
1260 set_memory_array_wb(mem->memory, mem->page_count);
1261#endif
1262
1263 for (i = 0; i < mem->page_count; i++) {
1264 addr = (void *)mem->memory[i];
1265 page = virt_to_page(addr);
1266
1267#ifndef CONFIG_X86
1268 unmap_page_from_agp(page);
1269#endif
1270
1271 put_page(page);
1272 free_page((unsigned long)addr);
1273 atomic_dec(&agp_bridge->current_memory_agp);
1274 mem->memory[i] = 0;
1275 }
1276}
1277EXPORT_SYMBOL(agp_generic_destroy_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Dave Airliea2721e92007-10-15 10:19:16 +10001279void agp_generic_destroy_page(void *addr, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct page *page;
1282
1283 if (addr == NULL)
1284 return;
1285
1286 page = virt_to_page(addr);
Dave Airliea2721e92007-10-15 10:19:16 +10001287 if (flags & AGP_PAGE_DESTROY_UNMAP)
1288 unmap_page_from_agp(page);
1289
1290 if (flags & AGP_PAGE_DESTROY_FREE) {
1291 put_page(page);
1292 free_page((unsigned long)addr);
1293 atomic_dec(&agp_bridge->current_memory_agp);
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296EXPORT_SYMBOL(agp_generic_destroy_page);
1297
1298/* End Basic Page Allocation Routines */
1299
1300
1301/**
1302 * agp_enable - initialise the agp point-to-point connection.
1303 *
1304 * @mode: agp mode register value to configure with.
1305 */
1306void agp_enable(struct agp_bridge_data *bridge, u32 mode)
1307{
1308 if (!bridge)
1309 return;
1310 bridge->driver->agp_enable(bridge, mode);
1311}
1312EXPORT_SYMBOL(agp_enable);
1313
1314/* When we remove the global variable agp_bridge from all drivers
1315 * then agp_alloc_bridge and agp_generic_find_bridge need to be updated
1316 */
1317
1318struct agp_bridge_data *agp_generic_find_bridge(struct pci_dev *pdev)
1319{
1320 if (list_empty(&agp_bridges))
1321 return NULL;
1322
1323 return agp_bridge;
1324}
1325
1326static void ipi_handler(void *null)
1327{
1328 flush_agp_cache();
1329}
1330
1331void global_cache_flush(void)
1332{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001333 if (on_each_cpu(ipi_handler, NULL, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 panic(PFX "timed out waiting for the other CPUs!\n");
1335}
1336EXPORT_SYMBOL(global_cache_flush);
1337
1338unsigned long agp_generic_mask_memory(struct agp_bridge_data *bridge,
1339 unsigned long addr, int type)
1340{
1341 /* memory type is ignored in the generic routine */
1342 if (bridge->driver->masks)
1343 return addr | bridge->driver->masks[0].mask;
1344 else
1345 return addr;
1346}
1347EXPORT_SYMBOL(agp_generic_mask_memory);
1348
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001349int agp_generic_type_to_mask_type(struct agp_bridge_data *bridge,
1350 int type)
1351{
1352 if (type >= AGP_USER_TYPES)
1353 return 0;
1354 return type;
1355}
1356EXPORT_SYMBOL(agp_generic_type_to_mask_type);
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358/*
1359 * These functions are implemented according to the AGPv3 spec,
1360 * which covers implementation details that had previously been
1361 * left open.
1362 */
1363
1364int agp3_generic_fetch_size(void)
1365{
1366 u16 temp_size;
1367 int i;
1368 struct aper_size_info_16 *values;
1369
1370 pci_read_config_word(agp_bridge->dev, agp_bridge->capndx+AGPAPSIZE, &temp_size);
1371 values = A_SIZE_16(agp_bridge->driver->aperture_sizes);
1372
1373 for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
1374 if (temp_size == values[i].size_value) {
1375 agp_bridge->previous_size =
1376 agp_bridge->current_size = (void *) (values + i);
1377
1378 agp_bridge->aperture_size_idx = i;
1379 return values[i].size;
1380 }
1381 }
1382 return 0;
1383}
1384EXPORT_SYMBOL(agp3_generic_fetch_size);
1385
1386void agp3_generic_tlbflush(struct agp_memory *mem)
1387{
1388 u32 ctrl;
1389 pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &ctrl);
1390 pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl & ~AGPCTRL_GTLBEN);
1391 pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl);
1392}
1393EXPORT_SYMBOL(agp3_generic_tlbflush);
1394
1395int agp3_generic_configure(void)
1396{
1397 u32 temp;
1398 struct aper_size_info_16 *current_size;
1399
1400 current_size = A_SIZE_16(agp_bridge->current_size);
1401
1402 pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
1403 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
1404
1405 /* set aperture size */
1406 pci_write_config_word(agp_bridge->dev, agp_bridge->capndx+AGPAPSIZE, current_size->size_value);
1407 /* set gart pointer */
1408 pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPGARTLO, agp_bridge->gatt_bus_addr);
1409 /* enable aperture and GTLB */
1410 pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &temp);
1411 pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, temp | AGPCTRL_APERENB | AGPCTRL_GTLBEN);
1412 return 0;
1413}
1414EXPORT_SYMBOL(agp3_generic_configure);
1415
1416void agp3_generic_cleanup(void)
1417{
1418 u32 ctrl;
1419 pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &ctrl);
1420 pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl & ~AGPCTRL_APERENB);
1421}
1422EXPORT_SYMBOL(agp3_generic_cleanup);
1423
Dave Jonese5524f32007-02-22 18:41:28 -05001424const struct aper_size_info_16 agp3_generic_sizes[AGP_GENERIC_SIZES_ENTRIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 {4096, 1048576, 10,0x000},
1427 {2048, 524288, 9, 0x800},
1428 {1024, 262144, 8, 0xc00},
1429 { 512, 131072, 7, 0xe00},
1430 { 256, 65536, 6, 0xf00},
1431 { 128, 32768, 5, 0xf20},
1432 { 64, 16384, 4, 0xf30},
1433 { 32, 8192, 3, 0xf38},
1434 { 16, 4096, 2, 0xf3c},
1435 { 8, 2048, 1, 0xf3e},
1436 { 4, 1024, 0, 0xf3f}
1437};
1438EXPORT_SYMBOL(agp3_generic_sizes);
1439