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.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
28 * - Allocate more than order 0 pages to avoid too much linear map splitting.
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/init.h>
33 #include <linux/pagemap.h>
34 #include <linux/miscdevice.h>
36 #include <linux/agp_backend.h>
37 #include <linux/vmalloc.h>
38 #include <linux/dma-mapping.h>
41 #include <asm/cacheflush.h>
42 #include <asm/pgtable.h>
45 __u32
*agp_gatt_table
;
46 int agp_memory_reserved
;
49 * Needed by the Nforce GART driver for the time being. Would be
50 * nice to do this some other way instead of needing this export.
52 EXPORT_SYMBOL_GPL(agp_memory_reserved
);
54 #if defined(CONFIG_X86)
55 int map_page_into_agp(struct page
*page
)
58 i
= change_page_attr(page
, 1, PAGE_KERNEL_NOCACHE
);
59 /* Caller's responsibility to call global_flush_tlb() for
60 * performance reasons */
63 EXPORT_SYMBOL_GPL(map_page_into_agp
);
65 int unmap_page_from_agp(struct page
*page
)
68 i
= change_page_attr(page
, 1, PAGE_KERNEL
);
69 /* Caller's responsibility to call global_flush_tlb() for
70 * performance reasons */
73 EXPORT_SYMBOL_GPL(unmap_page_from_agp
);
77 * Generic routines for handling agp_memory structures -
78 * They use the basic page allocation routines to do the brunt of the work.
81 void agp_free_key(int key
)
87 clear_bit(key
, agp_bridge
->key_list
);
89 EXPORT_SYMBOL(agp_free_key
);
92 static int agp_get_key(void)
96 bit
= find_first_zero_bit(agp_bridge
->key_list
, MAXKEY
);
98 set_bit(bit
, agp_bridge
->key_list
);
105 * Use kmalloc if possible for the page list. Otherwise fall back to
106 * vmalloc. This speeds things up and also saves memory for small AGP
110 void agp_alloc_page_array(size_t size
, struct agp_memory
*mem
)
113 mem
->vmalloc_flag
= 0;
115 if (size
<= 2*PAGE_SIZE
)
116 mem
->memory
= kmalloc(size
, GFP_KERNEL
| __GFP_NORETRY
);
117 if (mem
->memory
== NULL
) {
118 mem
->memory
= vmalloc(size
);
119 mem
->vmalloc_flag
= 1;
122 EXPORT_SYMBOL(agp_alloc_page_array
);
124 void agp_free_page_array(struct agp_memory
*mem
)
126 if (mem
->vmalloc_flag
) {
132 EXPORT_SYMBOL(agp_free_page_array
);
135 static struct agp_memory
*agp_create_user_memory(unsigned long num_agp_pages
)
137 struct agp_memory
*new;
138 unsigned long alloc_size
= num_agp_pages
*sizeof(struct page
*);
140 new = kzalloc(sizeof(struct agp_memory
), GFP_KERNEL
);
144 new->key
= agp_get_key();
151 agp_alloc_page_array(alloc_size
, new);
153 if (new->memory
== NULL
) {
154 agp_free_key(new->key
);
158 new->num_scratch_pages
= 0;
162 struct agp_memory
*agp_create_memory(int scratch_pages
)
164 struct agp_memory
*new;
166 new = kzalloc(sizeof(struct agp_memory
), GFP_KERNEL
);
170 new->key
= agp_get_key();
177 agp_alloc_page_array(PAGE_SIZE
* scratch_pages
, new);
179 if (new->memory
== NULL
) {
180 agp_free_key(new->key
);
184 new->num_scratch_pages
= scratch_pages
;
185 new->type
= AGP_NORMAL_MEMORY
;
188 EXPORT_SYMBOL(agp_create_memory
);
191 * agp_free_memory - free memory associated with an agp_memory pointer.
193 * @curr: agp_memory pointer to be freed.
195 * It is the only function that can be called when the backend is not owned
196 * by the caller. (So it can free memory on client death.)
198 void agp_free_memory(struct agp_memory
*curr
)
205 if (curr
->is_bound
== TRUE
)
206 agp_unbind_memory(curr
);
208 if (curr
->type
>= AGP_USER_TYPES
) {
209 agp_generic_free_by_type(curr
);
213 if (curr
->type
!= 0) {
214 curr
->bridge
->driver
->free_by_type(curr
);
217 if (curr
->page_count
!= 0) {
218 for (i
= 0; i
< curr
->page_count
; i
++) {
219 curr
->bridge
->driver
->agp_destroy_page(gart_to_virt(curr
->memory
[i
]));
221 flush_agp_mappings();
223 agp_free_key(curr
->key
);
224 agp_free_page_array(curr
);
227 EXPORT_SYMBOL(agp_free_memory
);
229 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
232 * agp_allocate_memory - allocate a group of pages of a certain type.
234 * @page_count: size_t argument of the number of pages
235 * @type: u32 argument of the type of memory to be allocated.
237 * Every agp bridge device will allow you to allocate AGP_NORMAL_MEMORY which
238 * maps to physical ram. Any other type is device dependent.
240 * It returns NULL whenever memory is unavailable.
242 struct agp_memory
*agp_allocate_memory(struct agp_bridge_data
*bridge
,
243 size_t page_count
, u32 type
)
246 struct agp_memory
*new;
252 if ((atomic_read(&bridge
->current_memory_agp
) + page_count
) > bridge
->max_memory_agp
)
255 if (type
>= AGP_USER_TYPES
) {
256 new = agp_generic_alloc_user(page_count
, type
);
258 new->bridge
= bridge
;
263 new = bridge
->driver
->alloc_by_type(page_count
, type
);
265 new->bridge
= bridge
;
269 scratch_pages
= (page_count
+ ENTRIES_PER_PAGE
- 1) / ENTRIES_PER_PAGE
;
271 new = agp_create_memory(scratch_pages
);
276 for (i
= 0; i
< page_count
; i
++) {
277 void *addr
= bridge
->driver
->agp_alloc_page(bridge
);
280 agp_free_memory(new);
283 new->memory
[i
] = virt_to_gart(addr
);
286 new->bridge
= bridge
;
288 flush_agp_mappings();
292 EXPORT_SYMBOL(agp_allocate_memory
);
295 /* End - Generic routines for handling agp_memory structures */
298 static int agp_return_size(void)
303 temp
= agp_bridge
->current_size
;
305 switch (agp_bridge
->driver
->size_type
) {
307 current_size
= A_SIZE_8(temp
)->size
;
310 current_size
= A_SIZE_16(temp
)->size
;
313 current_size
= A_SIZE_32(temp
)->size
;
316 current_size
= A_SIZE_LVL2(temp
)->size
;
318 case FIXED_APER_SIZE
:
319 current_size
= A_SIZE_FIX(temp
)->size
;
326 current_size
-= (agp_memory_reserved
/ (1024*1024));
333 int agp_num_entries(void)
338 temp
= agp_bridge
->current_size
;
340 switch (agp_bridge
->driver
->size_type
) {
342 num_entries
= A_SIZE_8(temp
)->num_entries
;
345 num_entries
= A_SIZE_16(temp
)->num_entries
;
348 num_entries
= A_SIZE_32(temp
)->num_entries
;
351 num_entries
= A_SIZE_LVL2(temp
)->num_entries
;
353 case FIXED_APER_SIZE
:
354 num_entries
= A_SIZE_FIX(temp
)->num_entries
;
361 num_entries
-= agp_memory_reserved
>>PAGE_SHIFT
;
366 EXPORT_SYMBOL_GPL(agp_num_entries
);
370 * agp_copy_info - copy bridge state information
372 * @info: agp_kern_info pointer. The caller should insure that this pointer is valid.
374 * This function copies information about the agp bridge device and the state of
375 * the agp backend into an agp_kern_info pointer.
377 int agp_copy_info(struct agp_bridge_data
*bridge
, struct agp_kern_info
*info
)
379 memset(info
, 0, sizeof(struct agp_kern_info
));
381 info
->chipset
= NOT_SUPPORTED
;
385 info
->version
.major
= bridge
->version
->major
;
386 info
->version
.minor
= bridge
->version
->minor
;
387 info
->chipset
= SUPPORTED
;
388 info
->device
= bridge
->dev
;
389 if (bridge
->mode
& AGPSTAT_MODE_3_0
)
390 info
->mode
= bridge
->mode
& ~AGP3_RESERVED_MASK
;
392 info
->mode
= bridge
->mode
& ~AGP2_RESERVED_MASK
;
393 info
->aper_base
= bridge
->gart_bus_addr
;
394 info
->aper_size
= agp_return_size();
395 info
->max_memory
= bridge
->max_memory_agp
;
396 info
->current_memory
= atomic_read(&bridge
->current_memory_agp
);
397 info
->cant_use_aperture
= bridge
->driver
->cant_use_aperture
;
398 info
->vm_ops
= bridge
->vm_ops
;
399 info
->page_mask
= ~0UL;
402 EXPORT_SYMBOL(agp_copy_info
);
404 /* End - Routine to copy over information structure */
407 * Routines for handling swapping of agp_memory into the GATT -
408 * These routines take agp_memory and insert them into the GATT.
409 * They call device specific routines to actually write to the GATT.
413 * agp_bind_memory - Bind an agp_memory structure into the GATT.
415 * @curr: agp_memory pointer
416 * @pg_start: an offset into the graphics aperture translation table
418 * It returns -EINVAL if the pointer == NULL.
419 * It returns -EBUSY if the area of the table requested is already in use.
421 int agp_bind_memory(struct agp_memory
*curr
, off_t pg_start
)
428 if (curr
->is_bound
== TRUE
) {
429 printk(KERN_INFO PFX
"memory %p is already bound!\n", curr
);
432 if (curr
->is_flushed
== FALSE
) {
433 curr
->bridge
->driver
->cache_flush();
434 curr
->is_flushed
= TRUE
;
436 ret_val
= curr
->bridge
->driver
->insert_memory(curr
, pg_start
, curr
->type
);
441 curr
->is_bound
= TRUE
;
442 curr
->pg_start
= pg_start
;
445 EXPORT_SYMBOL(agp_bind_memory
);
449 * agp_unbind_memory - Removes an agp_memory structure from the GATT
451 * @curr: agp_memory pointer to be removed from the GATT.
453 * It returns -EINVAL if this piece of agp_memory is not currently bound to
454 * the graphics aperture translation table or if the agp_memory pointer == NULL
456 int agp_unbind_memory(struct agp_memory
*curr
)
463 if (curr
->is_bound
!= TRUE
) {
464 printk(KERN_INFO PFX
"memory %p was not bound!\n", curr
);
468 ret_val
= curr
->bridge
->driver
->remove_memory(curr
, curr
->pg_start
, curr
->type
);
473 curr
->is_bound
= FALSE
;
477 EXPORT_SYMBOL(agp_unbind_memory
);
479 /* End - Routines for handling swapping of agp_memory into the GATT */
482 /* Generic Agp routines - Start */
483 static void agp_v2_parse_one(u32
*requested_mode
, u32
*bridge_agpstat
, u32
*vga_agpstat
)
487 if (*requested_mode
& AGP2_RESERVED_MASK
) {
488 printk(KERN_INFO PFX
"reserved bits set (%x) in mode 0x%x. Fixed.\n",
489 *requested_mode
& AGP2_RESERVED_MASK
, *requested_mode
);
490 *requested_mode
&= ~AGP2_RESERVED_MASK
;
494 * Some dumb bridges are programmed to disobey the AGP2 spec.
495 * This is likely a BIOS misprogramming rather than poweron default, or
496 * it would be a lot more common.
497 * https://bugs.freedesktop.org/show_bug.cgi?id=8816
498 * AGPv2 spec 6.1.9 states:
499 * The RATE field indicates the data transfer rates supported by this
500 * device. A.G.P. devices must report all that apply.
501 * Fix them up as best we can.
503 switch (*bridge_agpstat
& 7) {
505 *bridge_agpstat
|= (AGPSTAT2_2X
| AGPSTAT2_1X
);
506 printk(KERN_INFO PFX
"BIOS bug. AGP bridge claims to only support x4 rate"
507 "Fixing up support for x2 & x1\n");
510 *bridge_agpstat
|= AGPSTAT2_1X
;
511 printk(KERN_INFO PFX
"BIOS bug. AGP bridge claims to only support x2 rate"
512 "Fixing up support for x1\n");
518 /* Check the speed bits make sense. Only one should be set. */
519 tmp
= *requested_mode
& 7;
522 printk(KERN_INFO PFX
"%s tried to set rate=x0. Setting to x1 mode.\n", current
->comm
);
523 *requested_mode
|= AGPSTAT2_1X
;
529 *requested_mode
&= ~(AGPSTAT2_1X
); /* rate=2 */
536 *requested_mode
&= ~(AGPSTAT2_1X
|AGPSTAT2_2X
); /* rate=4*/
540 /* disable SBA if it's not supported */
541 if (!((*bridge_agpstat
& AGPSTAT_SBA
) && (*vga_agpstat
& AGPSTAT_SBA
) && (*requested_mode
& AGPSTAT_SBA
)))
542 *bridge_agpstat
&= ~AGPSTAT_SBA
;
545 if (!((*bridge_agpstat
& AGPSTAT2_4X
) && (*vga_agpstat
& AGPSTAT2_4X
) && (*requested_mode
& AGPSTAT2_4X
)))
546 *bridge_agpstat
&= ~AGPSTAT2_4X
;
548 if (!((*bridge_agpstat
& AGPSTAT2_2X
) && (*vga_agpstat
& AGPSTAT2_2X
) && (*requested_mode
& AGPSTAT2_2X
)))
549 *bridge_agpstat
&= ~AGPSTAT2_2X
;
551 if (!((*bridge_agpstat
& AGPSTAT2_1X
) && (*vga_agpstat
& AGPSTAT2_1X
) && (*requested_mode
& AGPSTAT2_1X
)))
552 *bridge_agpstat
&= ~AGPSTAT2_1X
;
554 /* Now we know what mode it should be, clear out the unwanted bits. */
555 if (*bridge_agpstat
& AGPSTAT2_4X
)
556 *bridge_agpstat
&= ~(AGPSTAT2_1X
| AGPSTAT2_2X
); /* 4X */
558 if (*bridge_agpstat
& AGPSTAT2_2X
)
559 *bridge_agpstat
&= ~(AGPSTAT2_1X
| AGPSTAT2_4X
); /* 2X */
561 if (*bridge_agpstat
& AGPSTAT2_1X
)
562 *bridge_agpstat
&= ~(AGPSTAT2_2X
| AGPSTAT2_4X
); /* 1X */
564 /* Apply any errata. */
565 if (agp_bridge
->flags
& AGP_ERRATA_FASTWRITES
)
566 *bridge_agpstat
&= ~AGPSTAT_FW
;
568 if (agp_bridge
->flags
& AGP_ERRATA_SBA
)
569 *bridge_agpstat
&= ~AGPSTAT_SBA
;
571 if (agp_bridge
->flags
& AGP_ERRATA_1X
) {
572 *bridge_agpstat
&= ~(AGPSTAT2_2X
| AGPSTAT2_4X
);
573 *bridge_agpstat
|= AGPSTAT2_1X
;
576 /* If we've dropped down to 1X, disable fast writes. */
577 if (*bridge_agpstat
& AGPSTAT2_1X
)
578 *bridge_agpstat
&= ~AGPSTAT_FW
;
582 * requested_mode = Mode requested by (typically) X.
583 * bridge_agpstat = PCI_AGP_STATUS from agp bridge.
584 * vga_agpstat = PCI_AGP_STATUS from graphic card.
586 static void agp_v3_parse_one(u32
*requested_mode
, u32
*bridge_agpstat
, u32
*vga_agpstat
)
588 u32 origbridge
=*bridge_agpstat
, origvga
=*vga_agpstat
;
591 if (*requested_mode
& AGP3_RESERVED_MASK
) {
592 printk(KERN_INFO PFX
"reserved bits set (%x) in mode 0x%x. Fixed.\n",
593 *requested_mode
& AGP3_RESERVED_MASK
, *requested_mode
);
594 *requested_mode
&= ~AGP3_RESERVED_MASK
;
597 /* Check the speed bits make sense. */
598 tmp
= *requested_mode
& 7;
600 printk(KERN_INFO PFX
"%s tried to set rate=x0. Setting to AGP3 x4 mode.\n", current
->comm
);
601 *requested_mode
|= AGPSTAT3_4X
;
604 printk(KERN_INFO PFX
"%s tried to set rate=x%d. Setting to AGP3 x8 mode.\n", current
->comm
, tmp
* 4);
605 *requested_mode
= (*requested_mode
& ~7) | AGPSTAT3_8X
;
608 /* ARQSZ - Set the value to the maximum one.
609 * Don't allow the mode register to override values. */
610 *bridge_agpstat
= ((*bridge_agpstat
& ~AGPSTAT_ARQSZ
) |
611 max_t(u32
,(*bridge_agpstat
& AGPSTAT_ARQSZ
),(*vga_agpstat
& AGPSTAT_ARQSZ
)));
613 /* Calibration cycle.
614 * Don't allow the mode register to override values. */
615 *bridge_agpstat
= ((*bridge_agpstat
& ~AGPSTAT_CAL_MASK
) |
616 min_t(u32
,(*bridge_agpstat
& AGPSTAT_CAL_MASK
),(*vga_agpstat
& AGPSTAT_CAL_MASK
)));
618 /* SBA *must* be supported for AGP v3 */
619 *bridge_agpstat
|= AGPSTAT_SBA
;
623 * Check for invalid speeds. This can happen when applications
624 * written before the AGP 3.0 standard pass AGP2.x modes to AGP3 hardware
626 if (*requested_mode
& AGPSTAT_MODE_3_0
) {
628 * Caller hasn't a clue what it is doing. Bridge is in 3.0 mode,
629 * have been passed a 3.0 mode, but with 2.x speed bits set.
630 * AGP2.x 4x -> AGP3.0 4x.
632 if (*requested_mode
& AGPSTAT2_4X
) {
633 printk(KERN_INFO PFX
"%s passes broken AGP3 flags (%x). Fixed.\n",
634 current
->comm
, *requested_mode
);
635 *requested_mode
&= ~AGPSTAT2_4X
;
636 *requested_mode
|= AGPSTAT3_4X
;
640 * The caller doesn't know what they are doing. We are in 3.0 mode,
641 * but have been passed an AGP 2.x mode.
642 * Convert AGP 1x,2x,4x -> AGP 3.0 4x.
644 printk(KERN_INFO PFX
"%s passes broken AGP2 flags (%x) in AGP3 mode. Fixed.\n",
645 current
->comm
, *requested_mode
);
646 *requested_mode
&= ~(AGPSTAT2_4X
| AGPSTAT2_2X
| AGPSTAT2_1X
);
647 *requested_mode
|= AGPSTAT3_4X
;
650 if (*requested_mode
& AGPSTAT3_8X
) {
651 if (!(*bridge_agpstat
& AGPSTAT3_8X
)) {
652 *bridge_agpstat
&= ~(AGPSTAT3_8X
| AGPSTAT3_RSVD
);
653 *bridge_agpstat
|= AGPSTAT3_4X
;
654 printk(KERN_INFO PFX
"%s requested AGPx8 but bridge not capable.\n", current
->comm
);
657 if (!(*vga_agpstat
& AGPSTAT3_8X
)) {
658 *bridge_agpstat
&= ~(AGPSTAT3_8X
| AGPSTAT3_RSVD
);
659 *bridge_agpstat
|= AGPSTAT3_4X
;
660 printk(KERN_INFO PFX
"%s requested AGPx8 but graphic card not capable.\n", current
->comm
);
663 /* All set, bridge & device can do AGP x8*/
664 *bridge_agpstat
&= ~(AGPSTAT3_4X
| AGPSTAT3_RSVD
);
667 } else if (*requested_mode
& AGPSTAT3_4X
) {
668 *bridge_agpstat
&= ~(AGPSTAT3_8X
| AGPSTAT3_RSVD
);
669 *bridge_agpstat
|= AGPSTAT3_4X
;
675 * If we didn't specify an AGP mode, we see if both
676 * the graphics card, and the bridge can do x8, and use if so.
677 * If not, we fall back to x4 mode.
679 if ((*bridge_agpstat
& AGPSTAT3_8X
) && (*vga_agpstat
& AGPSTAT3_8X
)) {
680 printk(KERN_INFO PFX
"No AGP mode specified. Setting to highest mode "
681 "supported by bridge & card (x8).\n");
682 *bridge_agpstat
&= ~(AGPSTAT3_4X
| AGPSTAT3_RSVD
);
683 *vga_agpstat
&= ~(AGPSTAT3_4X
| AGPSTAT3_RSVD
);
685 printk(KERN_INFO PFX
"Fell back to AGPx4 mode because");
686 if (!(*bridge_agpstat
& AGPSTAT3_8X
)) {
687 printk(KERN_INFO PFX
"bridge couldn't do x8. bridge_agpstat:%x (orig=%x)\n",
688 *bridge_agpstat
, origbridge
);
689 *bridge_agpstat
&= ~(AGPSTAT3_8X
| AGPSTAT3_RSVD
);
690 *bridge_agpstat
|= AGPSTAT3_4X
;
692 if (!(*vga_agpstat
& AGPSTAT3_8X
)) {
693 printk(KERN_INFO PFX
"graphics card couldn't do x8. vga_agpstat:%x (orig=%x)\n",
694 *vga_agpstat
, origvga
);
695 *vga_agpstat
&= ~(AGPSTAT3_8X
| AGPSTAT3_RSVD
);
696 *vga_agpstat
|= AGPSTAT3_4X
;
702 /* Apply any errata. */
703 if (agp_bridge
->flags
& AGP_ERRATA_FASTWRITES
)
704 *bridge_agpstat
&= ~AGPSTAT_FW
;
706 if (agp_bridge
->flags
& AGP_ERRATA_SBA
)
707 *bridge_agpstat
&= ~AGPSTAT_SBA
;
709 if (agp_bridge
->flags
& AGP_ERRATA_1X
) {
710 *bridge_agpstat
&= ~(AGPSTAT2_2X
| AGPSTAT2_4X
);
711 *bridge_agpstat
|= AGPSTAT2_1X
;
717 * agp_collect_device_status - determine correct agp_cmd from various agp_stat's
718 * @bridge: an agp_bridge_data struct allocated for the AGP host bridge.
719 * @requested_mode: requested agp_stat from userspace (Typically from X)
720 * @bridge_agpstat: current agp_stat from AGP bridge.
722 * This function will hunt for an AGP graphics card, and try to match
723 * the requested mode to the capabilities of both the bridge and the card.
725 u32
agp_collect_device_status(struct agp_bridge_data
*bridge
, u32 requested_mode
, u32 bridge_agpstat
)
727 struct pci_dev
*device
= NULL
;
732 device
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, device
);
734 printk(KERN_INFO PFX
"Couldn't find an AGP VGA controller.\n");
737 cap_ptr
= pci_find_capability(device
, PCI_CAP_ID_AGP
);
743 * Ok, here we have a AGP device. Disable impossible
744 * settings, and adjust the readqueue to the minimum.
746 pci_read_config_dword(device
, cap_ptr
+PCI_AGP_STATUS
, &vga_agpstat
);
748 /* adjust RQ depth */
749 bridge_agpstat
= ((bridge_agpstat
& ~AGPSTAT_RQ_DEPTH
) |
750 min_t(u32
, (requested_mode
& AGPSTAT_RQ_DEPTH
),
751 min_t(u32
, (bridge_agpstat
& AGPSTAT_RQ_DEPTH
), (vga_agpstat
& AGPSTAT_RQ_DEPTH
))));
753 /* disable FW if it's not supported */
754 if (!((bridge_agpstat
& AGPSTAT_FW
) &&
755 (vga_agpstat
& AGPSTAT_FW
) &&
756 (requested_mode
& AGPSTAT_FW
)))
757 bridge_agpstat
&= ~AGPSTAT_FW
;
759 /* Check to see if we are operating in 3.0 mode */
760 if (agp_bridge
->mode
& AGPSTAT_MODE_3_0
)
761 agp_v3_parse_one(&requested_mode
, &bridge_agpstat
, &vga_agpstat
);
763 agp_v2_parse_one(&requested_mode
, &bridge_agpstat
, &vga_agpstat
);
766 return bridge_agpstat
;
768 EXPORT_SYMBOL(agp_collect_device_status
);
771 void agp_device_command(u32 bridge_agpstat
, int agp_v3
)
773 struct pci_dev
*device
= NULL
;
776 mode
= bridge_agpstat
& 0x7;
780 for_each_pci_dev(device
) {
781 u8 agp
= pci_find_capability(device
, PCI_CAP_ID_AGP
);
785 printk(KERN_INFO PFX
"Putting AGP V%d device at %s into %dx mode\n",
786 agp_v3
? 3 : 2, pci_name(device
), mode
);
787 pci_write_config_dword(device
, agp
+ PCI_AGP_COMMAND
, bridge_agpstat
);
790 EXPORT_SYMBOL(agp_device_command
);
793 void get_agp_version(struct agp_bridge_data
*bridge
)
797 /* Exit early if already set by errata workarounds. */
798 if (bridge
->major_version
!= 0)
801 pci_read_config_dword(bridge
->dev
, bridge
->capndx
, &ncapid
);
802 bridge
->major_version
= (ncapid
>> AGP_MAJOR_VERSION_SHIFT
) & 0xf;
803 bridge
->minor_version
= (ncapid
>> AGP_MINOR_VERSION_SHIFT
) & 0xf;
805 EXPORT_SYMBOL(get_agp_version
);
808 void agp_generic_enable(struct agp_bridge_data
*bridge
, u32 requested_mode
)
810 u32 bridge_agpstat
, temp
;
812 get_agp_version(agp_bridge
);
814 printk(KERN_INFO PFX
"Found an AGP %d.%d compliant device at %s.\n",
815 agp_bridge
->major_version
,
816 agp_bridge
->minor_version
,
817 pci_name(agp_bridge
->dev
));
819 pci_read_config_dword(agp_bridge
->dev
,
820 agp_bridge
->capndx
+ PCI_AGP_STATUS
, &bridge_agpstat
);
822 bridge_agpstat
= agp_collect_device_status(agp_bridge
, requested_mode
, bridge_agpstat
);
823 if (bridge_agpstat
== 0)
824 /* Something bad happened. FIXME: Return error code? */
827 bridge_agpstat
|= AGPSTAT_AGP_ENABLE
;
829 /* Do AGP version specific frobbing. */
830 if (bridge
->major_version
>= 3) {
831 if (bridge
->mode
& AGPSTAT_MODE_3_0
) {
832 /* If we have 3.5, we can do the isoch stuff. */
833 if (bridge
->minor_version
>= 5)
834 agp_3_5_enable(bridge
);
835 agp_device_command(bridge_agpstat
, TRUE
);
838 /* Disable calibration cycle in RX91<1> when not in AGP3.0 mode of operation.*/
839 bridge_agpstat
&= ~(7<<10) ;
840 pci_read_config_dword(bridge
->dev
,
841 bridge
->capndx
+AGPCTRL
, &temp
);
843 pci_write_config_dword(bridge
->dev
,
844 bridge
->capndx
+AGPCTRL
, temp
);
846 printk(KERN_INFO PFX
"Device is in legacy mode,"
847 " falling back to 2.x\n");
852 agp_device_command(bridge_agpstat
, FALSE
);
854 EXPORT_SYMBOL(agp_generic_enable
);
857 int agp_generic_create_gatt_table(struct agp_bridge_data
*bridge
)
868 /* The generic routines can't handle 2 level gatt's */
869 if (bridge
->driver
->size_type
== LVL2_APER_SIZE
)
873 i
= bridge
->aperture_size_idx
;
874 temp
= bridge
->current_size
;
875 size
= page_order
= num_entries
= 0;
877 if (bridge
->driver
->size_type
!= FIXED_APER_SIZE
) {
879 switch (bridge
->driver
->size_type
) {
881 size
= A_SIZE_8(temp
)->size
;
883 A_SIZE_8(temp
)->page_order
;
885 A_SIZE_8(temp
)->num_entries
;
888 size
= A_SIZE_16(temp
)->size
;
889 page_order
= A_SIZE_16(temp
)->page_order
;
890 num_entries
= A_SIZE_16(temp
)->num_entries
;
893 size
= A_SIZE_32(temp
)->size
;
894 page_order
= A_SIZE_32(temp
)->page_order
;
895 num_entries
= A_SIZE_32(temp
)->num_entries
;
897 /* This case will never really happen. */
898 case FIXED_APER_SIZE
:
901 size
= page_order
= num_entries
= 0;
905 table
= alloc_gatt_pages(page_order
);
909 switch (bridge
->driver
->size_type
) {
911 bridge
->current_size
= A_IDX8(bridge
);
914 bridge
->current_size
= A_IDX16(bridge
);
917 bridge
->current_size
= A_IDX32(bridge
);
919 /* These cases will never really happen. */
920 case FIXED_APER_SIZE
:
925 temp
= bridge
->current_size
;
927 bridge
->aperture_size_idx
= i
;
929 } while (!table
&& (i
< bridge
->driver
->num_aperture_sizes
));
931 size
= ((struct aper_size_info_fixed
*) temp
)->size
;
932 page_order
= ((struct aper_size_info_fixed
*) temp
)->page_order
;
933 num_entries
= ((struct aper_size_info_fixed
*) temp
)->num_entries
;
934 table
= alloc_gatt_pages(page_order
);
940 table_end
= table
+ ((PAGE_SIZE
* (1 << page_order
)) - 1);
942 for (page
= virt_to_page(table
); page
<= virt_to_page(table_end
); page
++)
943 SetPageReserved(page
);
945 bridge
->gatt_table_real
= (u32
*) table
;
946 agp_gatt_table
= (void *)table
;
948 bridge
->driver
->cache_flush();
949 bridge
->gatt_table
= ioremap_nocache(virt_to_gart(table
),
950 (PAGE_SIZE
* (1 << page_order
)));
951 bridge
->driver
->cache_flush();
953 if (bridge
->gatt_table
== NULL
) {
954 for (page
= virt_to_page(table
); page
<= virt_to_page(table_end
); page
++)
955 ClearPageReserved(page
);
957 free_gatt_pages(table
, page_order
);
961 bridge
->gatt_bus_addr
= virt_to_gart(bridge
->gatt_table_real
);
963 /* AK: bogus, should encode addresses > 4GB */
964 for (i
= 0; i
< num_entries
; i
++) {
965 writel(bridge
->scratch_page
, bridge
->gatt_table
+i
);
966 readl(bridge
->gatt_table
+i
); /* PCI Posting. */
971 EXPORT_SYMBOL(agp_generic_create_gatt_table
);
973 int agp_generic_free_gatt_table(struct agp_bridge_data
*bridge
)
976 char *table
, *table_end
;
980 temp
= bridge
->current_size
;
982 switch (bridge
->driver
->size_type
) {
984 page_order
= A_SIZE_8(temp
)->page_order
;
987 page_order
= A_SIZE_16(temp
)->page_order
;
990 page_order
= A_SIZE_32(temp
)->page_order
;
992 case FIXED_APER_SIZE
:
993 page_order
= A_SIZE_FIX(temp
)->page_order
;
996 /* The generic routines can't deal with 2 level gatt's */
1004 /* Do not worry about freeing memory, because if this is
1005 * called, then all agp memory is deallocated and removed
1006 * from the table. */
1008 iounmap(bridge
->gatt_table
);
1009 table
= (char *) bridge
->gatt_table_real
;
1010 table_end
= table
+ ((PAGE_SIZE
* (1 << page_order
)) - 1);
1012 for (page
= virt_to_page(table
); page
<= virt_to_page(table_end
); page
++)
1013 ClearPageReserved(page
);
1015 free_gatt_pages(bridge
->gatt_table_real
, page_order
);
1017 agp_gatt_table
= NULL
;
1018 bridge
->gatt_table
= NULL
;
1019 bridge
->gatt_table_real
= NULL
;
1020 bridge
->gatt_bus_addr
= 0;
1024 EXPORT_SYMBOL(agp_generic_free_gatt_table
);
1027 int agp_generic_insert_memory(struct agp_memory
* mem
, off_t pg_start
, int type
)
1033 struct agp_bridge_data
*bridge
;
1036 bridge
= mem
->bridge
;
1040 if (mem
->page_count
== 0)
1043 temp
= bridge
->current_size
;
1045 switch (bridge
->driver
->size_type
) {
1047 num_entries
= A_SIZE_8(temp
)->num_entries
;
1050 num_entries
= A_SIZE_16(temp
)->num_entries
;
1053 num_entries
= A_SIZE_32(temp
)->num_entries
;
1055 case FIXED_APER_SIZE
:
1056 num_entries
= A_SIZE_FIX(temp
)->num_entries
;
1058 case LVL2_APER_SIZE
:
1059 /* The generic routines can't deal with 2 level gatt's */
1067 num_entries
-= agp_memory_reserved
/PAGE_SIZE
;
1068 if (num_entries
< 0) num_entries
= 0;
1070 if (type
!= mem
->type
)
1073 mask_type
= bridge
->driver
->agp_type_to_mask_type(bridge
, type
);
1074 if (mask_type
!= 0) {
1075 /* The generic routines know nothing of memory types */
1079 /* AK: could wrap */
1080 if ((pg_start
+ mem
->page_count
) > num_entries
)
1085 while (j
< (pg_start
+ mem
->page_count
)) {
1086 if (!PGE_EMPTY(bridge
, readl(bridge
->gatt_table
+j
)))
1091 if (mem
->is_flushed
== FALSE
) {
1092 bridge
->driver
->cache_flush();
1093 mem
->is_flushed
= TRUE
;
1096 for (i
= 0, j
= pg_start
; i
< mem
->page_count
; i
++, j
++) {
1097 writel(bridge
->driver
->mask_memory(bridge
, mem
->memory
[i
], mask_type
),
1098 bridge
->gatt_table
+j
);
1100 readl(bridge
->gatt_table
+j
-1); /* PCI Posting. */
1102 bridge
->driver
->tlb_flush(mem
);
1105 EXPORT_SYMBOL(agp_generic_insert_memory
);
1108 int agp_generic_remove_memory(struct agp_memory
*mem
, off_t pg_start
, int type
)
1111 struct agp_bridge_data
*bridge
;
1114 bridge
= mem
->bridge
;
1118 if (mem
->page_count
== 0)
1121 if (type
!= mem
->type
)
1124 mask_type
= bridge
->driver
->agp_type_to_mask_type(bridge
, type
);
1125 if (mask_type
!= 0) {
1126 /* The generic routines know nothing of memory types */
1130 /* AK: bogus, should encode addresses > 4GB */
1131 for (i
= pg_start
; i
< (mem
->page_count
+ pg_start
); i
++) {
1132 writel(bridge
->scratch_page
, bridge
->gatt_table
+i
);
1134 readl(bridge
->gatt_table
+i
-1); /* PCI Posting. */
1136 bridge
->driver
->tlb_flush(mem
);
1139 EXPORT_SYMBOL(agp_generic_remove_memory
);
1141 struct agp_memory
*agp_generic_alloc_by_type(size_t page_count
, int type
)
1145 EXPORT_SYMBOL(agp_generic_alloc_by_type
);
1147 void agp_generic_free_by_type(struct agp_memory
*curr
)
1149 agp_free_page_array(curr
);
1150 agp_free_key(curr
->key
);
1153 EXPORT_SYMBOL(agp_generic_free_by_type
);
1155 struct agp_memory
*agp_generic_alloc_user(size_t page_count
, int type
)
1157 struct agp_memory
*new;
1161 pages
= (page_count
+ ENTRIES_PER_PAGE
- 1) / ENTRIES_PER_PAGE
;
1162 new = agp_create_user_memory(page_count
);
1166 for (i
= 0; i
< page_count
; i
++)
1168 new->page_count
= 0;
1170 new->num_scratch_pages
= pages
;
1174 EXPORT_SYMBOL(agp_generic_alloc_user
);
1177 * Basic Page Allocation Routines -
1178 * These routines handle page allocation and by default they reserve the allocated
1179 * memory. They also handle incrementing the current_memory_agp value, Which is checked
1180 * against a maximum value.
1183 void *agp_generic_alloc_page(struct agp_bridge_data
*bridge
)
1187 page
= alloc_page(GFP_KERNEL
| GFP_DMA32
);
1191 map_page_into_agp(page
);
1194 SetPageLocked(page
);
1195 atomic_inc(&agp_bridge
->current_memory_agp
);
1196 return page_address(page
);
1198 EXPORT_SYMBOL(agp_generic_alloc_page
);
1201 void agp_generic_destroy_page(void *addr
)
1208 page
= virt_to_page(addr
);
1209 unmap_page_from_agp(page
);
1212 free_page((unsigned long)addr
);
1213 atomic_dec(&agp_bridge
->current_memory_agp
);
1215 EXPORT_SYMBOL(agp_generic_destroy_page
);
1217 /* End Basic Page Allocation Routines */
1221 * agp_enable - initialise the agp point-to-point connection.
1223 * @mode: agp mode register value to configure with.
1225 void agp_enable(struct agp_bridge_data
*bridge
, u32 mode
)
1229 bridge
->driver
->agp_enable(bridge
, mode
);
1231 EXPORT_SYMBOL(agp_enable
);
1233 /* When we remove the global variable agp_bridge from all drivers
1234 * then agp_alloc_bridge and agp_generic_find_bridge need to be updated
1237 struct agp_bridge_data
*agp_generic_find_bridge(struct pci_dev
*pdev
)
1239 if (list_empty(&agp_bridges
))
1245 static void ipi_handler(void *null
)
1250 void global_cache_flush(void)
1252 if (on_each_cpu(ipi_handler
, NULL
, 1, 1) != 0)
1253 panic(PFX
"timed out waiting for the other CPUs!\n");
1255 EXPORT_SYMBOL(global_cache_flush
);
1257 unsigned long agp_generic_mask_memory(struct agp_bridge_data
*bridge
,
1258 unsigned long addr
, int type
)
1260 /* memory type is ignored in the generic routine */
1261 if (bridge
->driver
->masks
)
1262 return addr
| bridge
->driver
->masks
[0].mask
;
1266 EXPORT_SYMBOL(agp_generic_mask_memory
);
1268 int agp_generic_type_to_mask_type(struct agp_bridge_data
*bridge
,
1271 if (type
>= AGP_USER_TYPES
)
1275 EXPORT_SYMBOL(agp_generic_type_to_mask_type
);
1278 * These functions are implemented according to the AGPv3 spec,
1279 * which covers implementation details that had previously been
1283 int agp3_generic_fetch_size(void)
1287 struct aper_size_info_16
*values
;
1289 pci_read_config_word(agp_bridge
->dev
, agp_bridge
->capndx
+AGPAPSIZE
, &temp_size
);
1290 values
= A_SIZE_16(agp_bridge
->driver
->aperture_sizes
);
1292 for (i
= 0; i
< agp_bridge
->driver
->num_aperture_sizes
; i
++) {
1293 if (temp_size
== values
[i
].size_value
) {
1294 agp_bridge
->previous_size
=
1295 agp_bridge
->current_size
= (void *) (values
+ i
);
1297 agp_bridge
->aperture_size_idx
= i
;
1298 return values
[i
].size
;
1303 EXPORT_SYMBOL(agp3_generic_fetch_size
);
1305 void agp3_generic_tlbflush(struct agp_memory
*mem
)
1308 pci_read_config_dword(agp_bridge
->dev
, agp_bridge
->capndx
+AGPCTRL
, &ctrl
);
1309 pci_write_config_dword(agp_bridge
->dev
, agp_bridge
->capndx
+AGPCTRL
, ctrl
& ~AGPCTRL_GTLBEN
);
1310 pci_write_config_dword(agp_bridge
->dev
, agp_bridge
->capndx
+AGPCTRL
, ctrl
);
1312 EXPORT_SYMBOL(agp3_generic_tlbflush
);
1314 int agp3_generic_configure(void)
1317 struct aper_size_info_16
*current_size
;
1319 current_size
= A_SIZE_16(agp_bridge
->current_size
);
1321 pci_read_config_dword(agp_bridge
->dev
, AGP_APBASE
, &temp
);
1322 agp_bridge
->gart_bus_addr
= (temp
& PCI_BASE_ADDRESS_MEM_MASK
);
1324 /* set aperture size */
1325 pci_write_config_word(agp_bridge
->dev
, agp_bridge
->capndx
+AGPAPSIZE
, current_size
->size_value
);
1326 /* set gart pointer */
1327 pci_write_config_dword(agp_bridge
->dev
, agp_bridge
->capndx
+AGPGARTLO
, agp_bridge
->gatt_bus_addr
);
1328 /* enable aperture and GTLB */
1329 pci_read_config_dword(agp_bridge
->dev
, agp_bridge
->capndx
+AGPCTRL
, &temp
);
1330 pci_write_config_dword(agp_bridge
->dev
, agp_bridge
->capndx
+AGPCTRL
, temp
| AGPCTRL_APERENB
| AGPCTRL_GTLBEN
);
1333 EXPORT_SYMBOL(agp3_generic_configure
);
1335 void agp3_generic_cleanup(void)
1338 pci_read_config_dword(agp_bridge
->dev
, agp_bridge
->capndx
+AGPCTRL
, &ctrl
);
1339 pci_write_config_dword(agp_bridge
->dev
, agp_bridge
->capndx
+AGPCTRL
, ctrl
& ~AGPCTRL_APERENB
);
1341 EXPORT_SYMBOL(agp3_generic_cleanup
);
1343 struct aper_size_info_16 agp3_generic_sizes
[AGP_GENERIC_SIZES_ENTRIES
] =
1345 {4096, 1048576, 10,0x000},
1346 {2048, 524288, 9, 0x800},
1347 {1024, 262144, 8, 0xc00},
1348 { 512, 131072, 7, 0xe00},
1349 { 256, 65536, 6, 0xf00},
1350 { 128, 32768, 5, 0xf20},
1351 { 64, 16384, 4, 0xf30},
1352 { 32, 8192, 3, 0xf38},
1353 { 16, 4096, 2, 0xf3c},
1354 { 8, 2048, 1, 0xf3e},
1355 { 4, 1024, 0, 0xf3f}
1357 EXPORT_SYMBOL(agp3_generic_sizes
);