2 * ATi AGPGART routines.
5 #include <linux/types.h>
6 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/agp_backend.h>
13 #define ATI_GART_MMBASE_ADDR 0x14
14 #define ATI_RS100_APSIZE 0xac
15 #define ATI_RS100_IG_AGPMODE 0xb0
16 #define ATI_RS300_APSIZE 0xf8
17 #define ATI_RS300_IG_AGPMODE 0xfc
18 #define ATI_GART_FEATURE_ID 0x00
19 #define ATI_GART_BASE 0x04
20 #define ATI_GART_CACHE_SZBASE 0x08
21 #define ATI_GART_CACHE_CNTRL 0x0c
22 #define ATI_GART_CACHE_ENTRY_CNTRL 0x10
25 static struct aper_size_info_lvl2 ati_generic_sizes
[7] =
27 {2048, 524288, 0x0000000c},
28 {1024, 262144, 0x0000000a},
29 {512, 131072, 0x00000008},
30 {256, 65536, 0x00000006},
31 {128, 32768, 0x00000004},
32 {64, 16384, 0x00000002},
33 {32, 8192, 0x00000000}
36 static struct gatt_mask ati_generic_masks
[] =
38 { .mask
= 1, .type
= 0}
43 typedef struct _ati_page_map
{
45 unsigned long __iomem
*remapped
;
48 static struct _ati_generic_private
{
49 volatile u8 __iomem
*registers
;
50 ati_page_map
**gatt_pages
;
52 } ati_generic_private
;
54 static int ati_create_page_map(ati_page_map
*page_map
)
58 page_map
->real
= (unsigned long *) __get_free_page(GFP_KERNEL
);
59 if (page_map
->real
== NULL
)
62 SetPageReserved(virt_to_page(page_map
->real
));
63 err
= map_page_into_agp(virt_to_page(page_map
->real
));
64 page_map
->remapped
= ioremap_nocache(virt_to_gart(page_map
->real
),
66 if (page_map
->remapped
== NULL
|| err
) {
67 ClearPageReserved(virt_to_page(page_map
->real
));
68 free_page((unsigned long) page_map
->real
);
69 page_map
->real
= NULL
;
75 for(i
= 0; i
< PAGE_SIZE
/ sizeof(unsigned long); i
++) {
76 writel(agp_bridge
->scratch_page
, page_map
->remapped
+i
);
77 readl(page_map
->remapped
+i
); /* PCI Posting. */
84 static void ati_free_page_map(ati_page_map
*page_map
)
86 unmap_page_from_agp(virt_to_page(page_map
->real
));
87 iounmap(page_map
->remapped
);
88 ClearPageReserved(virt_to_page(page_map
->real
));
89 free_page((unsigned long) page_map
->real
);
93 static void ati_free_gatt_pages(void)
96 ati_page_map
**tables
;
99 tables
= ati_generic_private
.gatt_pages
;
100 for(i
= 0; i
< ati_generic_private
.num_tables
; i
++) {
103 if (entry
->real
!= NULL
)
104 ati_free_page_map(entry
);
112 static int ati_create_gatt_pages(int nr_tables
)
114 ati_page_map
**tables
;
119 tables
= kmalloc((nr_tables
+ 1) * sizeof(ati_page_map
*),
124 memset(tables
, 0, sizeof(ati_page_map
*) * (nr_tables
+ 1));
125 for (i
= 0; i
< nr_tables
; i
++) {
126 entry
= kmalloc(sizeof(ati_page_map
), GFP_KERNEL
);
137 memset(entry
, 0, sizeof(ati_page_map
));
139 retval
= ati_create_page_map(entry
);
140 if (retval
!= 0) break;
142 ati_generic_private
.num_tables
= nr_tables
;
143 ati_generic_private
.gatt_pages
= tables
;
145 if (retval
!= 0) ati_free_gatt_pages();
150 static int is_r200(void)
152 if ((agp_bridge
->dev
->device
== PCI_DEVICE_ID_ATI_RS100
) ||
153 (agp_bridge
->dev
->device
== PCI_DEVICE_ID_ATI_RS200
) ||
154 (agp_bridge
->dev
->device
== PCI_DEVICE_ID_ATI_RS200_B
) ||
155 (agp_bridge
->dev
->device
== PCI_DEVICE_ID_ATI_RS250
))
160 static int ati_fetch_size(void)
164 struct aper_size_info_lvl2
*values
;
167 pci_read_config_dword(agp_bridge
->dev
, ATI_RS100_APSIZE
, &temp
);
169 pci_read_config_dword(agp_bridge
->dev
, ATI_RS300_APSIZE
, &temp
);
171 temp
= (temp
& 0x0000000e);
172 values
= A_SIZE_LVL2(agp_bridge
->driver
->aperture_sizes
);
173 for (i
= 0; i
< agp_bridge
->driver
->num_aperture_sizes
; i
++) {
174 if (temp
== values
[i
].size_value
) {
175 agp_bridge
->previous_size
=
176 agp_bridge
->current_size
= (void *) (values
+ i
);
178 agp_bridge
->aperture_size_idx
= i
;
179 return values
[i
].size
;
186 static void ati_tlbflush(struct agp_memory
* mem
)
188 writel(1, ati_generic_private
.registers
+ATI_GART_CACHE_CNTRL
);
189 readl(ati_generic_private
.registers
+ATI_GART_CACHE_CNTRL
); /* PCI Posting. */
192 static void ati_cleanup(void)
194 struct aper_size_info_lvl2
*previous_size
;
197 previous_size
= A_SIZE_LVL2(agp_bridge
->previous_size
);
199 /* Write back the previous size and disable gart translation */
201 pci_read_config_dword(agp_bridge
->dev
, ATI_RS100_APSIZE
, &temp
);
202 temp
= ((temp
& ~(0x0000000f)) | previous_size
->size_value
);
203 pci_write_config_dword(agp_bridge
->dev
, ATI_RS100_APSIZE
, temp
);
205 pci_read_config_dword(agp_bridge
->dev
, ATI_RS300_APSIZE
, &temp
);
206 temp
= ((temp
& ~(0x0000000f)) | previous_size
->size_value
);
207 pci_write_config_dword(agp_bridge
->dev
, ATI_RS300_APSIZE
, temp
);
209 iounmap((volatile u8 __iomem
*)ati_generic_private
.registers
);
213 static int ati_configure(void)
217 /* Get the memory mapped registers */
218 pci_read_config_dword(agp_bridge
->dev
, ATI_GART_MMBASE_ADDR
, &temp
);
219 temp
= (temp
& 0xfffff000);
220 ati_generic_private
.registers
= (volatile u8 __iomem
*) ioremap(temp
, 4096);
223 pci_write_config_dword(agp_bridge
->dev
, ATI_RS100_IG_AGPMODE
, 0x20000);
225 pci_write_config_dword(agp_bridge
->dev
, ATI_RS300_IG_AGPMODE
, 0x20000);
227 /* address to map too */
229 pci_read_config_dword(agp_bridge.dev, AGP_APBASE, &temp);
230 agp_bridge.gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
231 printk(KERN_INFO PFX "IGP320 gart_bus_addr: %x\n", agp_bridge.gart_bus_addr);
233 writel(0x60000, ati_generic_private
.registers
+ATI_GART_FEATURE_ID
);
234 readl(ati_generic_private
.registers
+ATI_GART_FEATURE_ID
); /* PCI Posting.*/
236 /* SIGNALED_SYSTEM_ERROR @ NB_STATUS */
237 pci_read_config_dword(agp_bridge
->dev
, 4, &temp
);
238 pci_write_config_dword(agp_bridge
->dev
, 4, temp
| (1<<14));
240 /* Write out the address of the gatt table */
241 writel(agp_bridge
->gatt_bus_addr
, ati_generic_private
.registers
+ATI_GART_BASE
);
242 readl(ati_generic_private
.registers
+ATI_GART_BASE
); /* PCI Posting. */
249 *Since we don't need contigious memory we just try
250 * to get the gatt table once
253 #define GET_PAGE_DIR_OFF(addr) (addr >> 22)
254 #define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
255 GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
256 #define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
258 #define GET_GATT(addr) (ati_generic_private.gatt_pages[\
259 GET_PAGE_DIR_IDX(addr)]->remapped)
261 static int ati_insert_memory(struct agp_memory
* mem
,
262 off_t pg_start
, int type
)
264 int i
, j
, num_entries
;
265 unsigned long __iomem
*cur_gatt
;
268 num_entries
= A_SIZE_LVL2(agp_bridge
->current_size
)->num_entries
;
270 if (type
!= 0 || mem
->type
!= 0)
273 if ((pg_start
+ mem
->page_count
) > num_entries
)
277 while (j
< (pg_start
+ mem
->page_count
)) {
278 addr
= (j
* PAGE_SIZE
) + agp_bridge
->gart_bus_addr
;
279 cur_gatt
= GET_GATT(addr
);
280 if (!PGE_EMPTY(agp_bridge
,readl(cur_gatt
+GET_GATT_OFF(addr
))))
285 if (mem
->is_flushed
== FALSE
) {
287 global_cache_flush();
288 mem
->is_flushed
= TRUE
;
291 for (i
= 0, j
= pg_start
; i
< mem
->page_count
; i
++, j
++) {
292 addr
= (j
* PAGE_SIZE
) + agp_bridge
->gart_bus_addr
;
293 cur_gatt
= GET_GATT(addr
);
294 writel(agp_bridge
->driver
->mask_memory(agp_bridge
,
295 mem
->memory
[i
], mem
->type
), cur_gatt
+GET_GATT_OFF(addr
));
296 readl(cur_gatt
+GET_GATT_OFF(addr
)); /* PCI Posting. */
298 agp_bridge
->driver
->tlb_flush(mem
);
302 static int ati_remove_memory(struct agp_memory
* mem
, off_t pg_start
,
306 unsigned long __iomem
*cur_gatt
;
309 if (type
!= 0 || mem
->type
!= 0) {
312 for (i
= pg_start
; i
< (mem
->page_count
+ pg_start
); i
++) {
313 addr
= (i
* PAGE_SIZE
) + agp_bridge
->gart_bus_addr
;
314 cur_gatt
= GET_GATT(addr
);
315 writel(agp_bridge
->scratch_page
, cur_gatt
+GET_GATT_OFF(addr
));
316 readl(cur_gatt
+GET_GATT_OFF(addr
)); /* PCI Posting. */
319 agp_bridge
->driver
->tlb_flush(mem
);
323 static int ati_create_gatt_table(struct agp_bridge_data
*bridge
)
325 struct aper_size_info_lvl2
*value
;
326 ati_page_map page_dir
;
331 struct aper_size_info_lvl2
*current_size
;
333 value
= A_SIZE_LVL2(agp_bridge
->current_size
);
334 retval
= ati_create_page_map(&page_dir
);
338 retval
= ati_create_gatt_pages(value
->num_entries
/ 1024);
340 ati_free_page_map(&page_dir
);
344 agp_bridge
->gatt_table_real
= (u32
*)page_dir
.real
;
345 agp_bridge
->gatt_table
= (u32 __iomem
*) page_dir
.remapped
;
346 agp_bridge
->gatt_bus_addr
= virt_to_gart(page_dir
.real
);
348 /* Write out the size register */
349 current_size
= A_SIZE_LVL2(agp_bridge
->current_size
);
352 pci_read_config_dword(agp_bridge
->dev
, ATI_RS100_APSIZE
, &temp
);
353 temp
= (((temp
& ~(0x0000000e)) | current_size
->size_value
)
355 pci_write_config_dword(agp_bridge
->dev
, ATI_RS100_APSIZE
, temp
);
356 pci_read_config_dword(agp_bridge
->dev
, ATI_RS100_APSIZE
, &temp
);
358 pci_read_config_dword(agp_bridge
->dev
, ATI_RS300_APSIZE
, &temp
);
359 temp
= (((temp
& ~(0x0000000e)) | current_size
->size_value
)
361 pci_write_config_dword(agp_bridge
->dev
, ATI_RS300_APSIZE
, temp
);
362 pci_read_config_dword(agp_bridge
->dev
, ATI_RS300_APSIZE
, &temp
);
366 * Get the address for the gart region.
367 * This is a bus address even on the alpha, b/c its
368 * used to program the agp master not the cpu
370 pci_read_config_dword(agp_bridge
->dev
, AGP_APBASE
, &temp
);
371 addr
= (temp
& PCI_BASE_ADDRESS_MEM_MASK
);
372 agp_bridge
->gart_bus_addr
= addr
;
374 /* Calculate the agp offset */
375 for(i
= 0; i
< value
->num_entries
/ 1024; i
++, addr
+= 0x00400000) {
376 writel(virt_to_gart(ati_generic_private
.gatt_pages
[i
]->real
) | 1,
377 page_dir
.remapped
+GET_PAGE_DIR_OFF(addr
));
378 readl(page_dir
.remapped
+GET_PAGE_DIR_OFF(addr
)); /* PCI Posting. */
384 static int ati_free_gatt_table(struct agp_bridge_data
*bridge
)
386 ati_page_map page_dir
;
388 page_dir
.real
= (unsigned long *)agp_bridge
->gatt_table_real
;
389 page_dir
.remapped
= (unsigned long __iomem
*)agp_bridge
->gatt_table
;
391 ati_free_gatt_pages();
392 ati_free_page_map(&page_dir
);
396 static struct agp_bridge_driver ati_generic_bridge
= {
397 .owner
= THIS_MODULE
,
398 .aperture_sizes
= ati_generic_sizes
,
399 .size_type
= LVL2_APER_SIZE
,
400 .num_aperture_sizes
= 7,
401 .configure
= ati_configure
,
402 .fetch_size
= ati_fetch_size
,
403 .cleanup
= ati_cleanup
,
404 .tlb_flush
= ati_tlbflush
,
405 .mask_memory
= agp_generic_mask_memory
,
406 .masks
= ati_generic_masks
,
407 .agp_enable
= agp_generic_enable
,
408 .cache_flush
= global_cache_flush
,
409 .create_gatt_table
= ati_create_gatt_table
,
410 .free_gatt_table
= ati_free_gatt_table
,
411 .insert_memory
= ati_insert_memory
,
412 .remove_memory
= ati_remove_memory
,
413 .alloc_by_type
= agp_generic_alloc_by_type
,
414 .free_by_type
= agp_generic_free_by_type
,
415 .agp_alloc_page
= agp_generic_alloc_page
,
416 .agp_destroy_page
= agp_generic_destroy_page
,
420 static struct agp_device_ids ati_agp_device_ids
[] __devinitdata
=
423 .device_id
= PCI_DEVICE_ID_ATI_RS100
,
424 .chipset_name
= "IGP320/M",
427 .device_id
= PCI_DEVICE_ID_ATI_RS200
,
428 .chipset_name
= "IGP330/340/345/350/M",
431 .device_id
= PCI_DEVICE_ID_ATI_RS200_B
,
432 .chipset_name
= "IGP345M",
435 .device_id
= PCI_DEVICE_ID_ATI_RS250
,
436 .chipset_name
= "IGP7000/M",
439 .device_id
= PCI_DEVICE_ID_ATI_RS300_100
,
440 .chipset_name
= "IGP9100/M",
443 .device_id
= PCI_DEVICE_ID_ATI_RS300_133
,
444 .chipset_name
= "IGP9100/M",
447 .device_id
= PCI_DEVICE_ID_ATI_RS300_166
,
448 .chipset_name
= "IGP9100/M",
451 .device_id
= PCI_DEVICE_ID_ATI_RS300_200
,
452 .chipset_name
= "IGP9100/M",
454 { }, /* dummy final entry, always present */
457 static int __devinit
agp_ati_probe(struct pci_dev
*pdev
,
458 const struct pci_device_id
*ent
)
460 struct agp_device_ids
*devs
= ati_agp_device_ids
;
461 struct agp_bridge_data
*bridge
;
465 cap_ptr
= pci_find_capability(pdev
, PCI_CAP_ID_AGP
);
469 /* probe for known chipsets */
470 for (j
= 0; devs
[j
].chipset_name
; j
++) {
471 if (pdev
->device
== devs
[j
].device_id
)
476 "Unsupported Ati chipset (device id: %04x)\n", pdev
->device
);
480 bridge
= agp_alloc_bridge();
485 bridge
->capndx
= cap_ptr
;
487 bridge
->driver
= &ati_generic_bridge
;
490 printk(KERN_INFO PFX
"Detected Ati %s chipset\n",
491 devs
[j
].chipset_name
);
493 /* Fill in the mode register */
494 pci_read_config_dword(pdev
,
495 bridge
->capndx
+PCI_AGP_STATUS
,
498 pci_set_drvdata(pdev
, bridge
);
499 return agp_add_bridge(bridge
);
502 static void __devexit
agp_ati_remove(struct pci_dev
*pdev
)
504 struct agp_bridge_data
*bridge
= pci_get_drvdata(pdev
);
506 agp_remove_bridge(bridge
);
507 agp_put_bridge(bridge
);
510 static struct pci_device_id agp_ati_pci_table
[] = {
512 .class = (PCI_CLASS_BRIDGE_HOST
<< 8),
514 .vendor
= PCI_VENDOR_ID_ATI
,
515 .device
= PCI_ANY_ID
,
516 .subvendor
= PCI_ANY_ID
,
517 .subdevice
= PCI_ANY_ID
,
522 MODULE_DEVICE_TABLE(pci
, agp_ati_pci_table
);
524 static struct pci_driver agp_ati_pci_driver
= {
525 .name
= "agpgart-ati",
526 .id_table
= agp_ati_pci_table
,
527 .probe
= agp_ati_probe
,
528 .remove
= agp_ati_remove
,
531 static int __init
agp_ati_init(void)
535 return pci_register_driver(&agp_ati_pci_driver
);
538 static void __exit
agp_ati_cleanup(void)
540 pci_unregister_driver(&agp_ati_pci_driver
);
543 module_init(agp_ati_init
);
544 module_exit(agp_ati_cleanup
);
546 MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>");
547 MODULE_LICENSE("GPL and additional rights");