a65f8827c2831d40f33e0baf5970211e5bba07d9
[linux-2.6.git] / drivers / char / agp / ati-agp.c
1 /*
2  * ATi AGPGART routines.
3  */
4
5 #include <linux/types.h>
6 #include <linux/module.h>
7 #include <linux/pci.h>
8 #include <linux/init.h>
9 #include <linux/agp_backend.h>
10 #include <asm/agp.h>
11 #include "agp.h"
12
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
23
24
25 static struct aper_size_info_lvl2 ati_generic_sizes[7] =
26 {
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}
34 };
35
36 static struct gatt_mask ati_generic_masks[] =
37 {
38         { .mask = 1, .type = 0}
39 };
40
41
42
43 typedef struct _ati_page_map {
44         unsigned long *real;
45         unsigned long __iomem *remapped;
46 } ati_page_map;
47
48 static struct _ati_generic_private {
49         volatile u8 __iomem *registers;
50         ati_page_map **gatt_pages;
51         int num_tables;
52 } ati_generic_private;
53
54 static int ati_create_page_map(ati_page_map *page_map)
55 {
56         int i, err = 0;
57
58         page_map->real = (unsigned long *) __get_free_page(GFP_KERNEL);
59         if (page_map->real == NULL)
60                 return -ENOMEM;
61
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_phys(page_map->real),
65                                             PAGE_SIZE);
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;
70                 return -ENOMEM;
71         }
72         /*CACHE_FLUSH();*/
73         global_cache_flush();
74
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. */
78         }
79
80         return 0;
81 }
82
83
84 static void ati_free_page_map(ati_page_map *page_map)
85 {
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);
90 }
91
92
93 static void ati_free_gatt_pages(void)
94 {
95         int i;
96         ati_page_map **tables;
97         ati_page_map *entry;
98
99         tables = ati_generic_private.gatt_pages;
100         for(i = 0; i < ati_generic_private.num_tables; i++) {
101                 entry = tables[i];
102                 if (entry != NULL) {
103                         if (entry->real != NULL)
104                                 ati_free_page_map(entry);
105                         kfree(entry);
106                 }
107         }
108         kfree(tables);
109 }
110
111
112 static int ati_create_gatt_pages(int nr_tables)
113 {
114         ati_page_map **tables;
115         ati_page_map *entry;
116         int retval = 0;
117         int i;
118
119         tables = kmalloc((nr_tables + 1) * sizeof(ati_page_map *),
120                          GFP_KERNEL);
121         if (tables == NULL)
122                 return -ENOMEM;
123
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);
127                 if (entry == NULL) {
128                         while (i>0) {
129                                 kfree (tables[i-1]);
130                                 i--;
131                         }
132                         kfree (tables);
133                         tables = NULL;
134                         retval = -ENOMEM;
135                         break;
136                 }
137                 memset(entry, 0, sizeof(ati_page_map));
138                 tables[i] = entry;
139                 retval = ati_create_page_map(entry);
140                 if (retval != 0) break;
141         }
142         ati_generic_private.num_tables = nr_tables;
143         ati_generic_private.gatt_pages = tables;
144
145         if (retval != 0) ati_free_gatt_pages();
146
147         return retval;
148 }
149
150 static int is_r200(void)
151 {
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))
156                 return 1;
157         return 0;
158 }
159
160 static int ati_fetch_size(void)
161 {
162         int i;
163         u32 temp;
164         struct aper_size_info_lvl2 *values;
165
166         if (is_r200())
167                 pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
168         else
169                 pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
170
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);
177
178                         agp_bridge->aperture_size_idx = i;
179                         return values[i].size;
180                 }
181         }
182
183         return 0;
184 }
185
186 static void ati_tlbflush(struct agp_memory * mem)
187 {
188         writel(1, ati_generic_private.registers+ATI_GART_CACHE_CNTRL);
189         readl(ati_generic_private.registers+ATI_GART_CACHE_CNTRL);      /* PCI Posting. */
190 }
191
192 static void ati_cleanup(void)
193 {
194         struct aper_size_info_lvl2 *previous_size;
195         u32 temp;
196
197         previous_size = A_SIZE_LVL2(agp_bridge->previous_size);
198
199         /* Write back the previous size and disable gart translation */
200         if (is_r200()) {
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);
204         } else {
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);
208         }
209         iounmap((volatile u8 __iomem *)ati_generic_private.registers);
210 }
211
212
213 static int ati_configure(void)
214 {
215         u32 temp;
216
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);
221
222         if (is_r200())
223         pci_write_config_dword(agp_bridge->dev, ATI_RS100_IG_AGPMODE, 0x20000);
224         else
225                 pci_write_config_dword(agp_bridge->dev, ATI_RS300_IG_AGPMODE, 0x20000);
226
227         /* address to map too */
228         /*
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);
232         */
233         writel(0x60000, ati_generic_private.registers+ATI_GART_FEATURE_ID);
234         readl(ati_generic_private.registers+ATI_GART_FEATURE_ID);       /* PCI Posting.*/
235
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));
239
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. */
243
244         return 0;
245 }
246
247
248 /*
249  *Since we don't need contigious memory we just try
250  * to get the gatt table once
251  */
252
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)
257 #undef  GET_GATT
258 #define GET_GATT(addr) (ati_generic_private.gatt_pages[\
259         GET_PAGE_DIR_IDX(addr)]->remapped)
260
261 static int ati_insert_memory(struct agp_memory * mem,
262                              off_t pg_start, int type)
263 {
264         int i, j, num_entries;
265         unsigned long __iomem *cur_gatt;
266         unsigned long addr;
267
268         num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
269
270         if (type != 0 || mem->type != 0)
271                 return -EINVAL;
272
273         if ((pg_start + mem->page_count) > num_entries)
274                 return -EINVAL;
275
276         j = pg_start;
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))))
281                         return -EBUSY;
282                 j++;
283         }
284
285         if (mem->is_flushed == FALSE) {
286                 /*CACHE_FLUSH(); */
287                 global_cache_flush();
288                 mem->is_flushed = TRUE;
289         }
290
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. */
297         }
298         agp_bridge->driver->tlb_flush(mem);
299         return 0;
300 }
301
302 static int ati_remove_memory(struct agp_memory * mem, off_t pg_start,
303                              int type)
304 {
305         int i;
306         unsigned long __iomem *cur_gatt;
307         unsigned long addr;
308
309         if (type != 0 || mem->type != 0) {
310                 return -EINVAL;
311         }
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. */
317         }
318
319         agp_bridge->driver->tlb_flush(mem);
320         return 0;
321 }
322
323 static int ati_create_gatt_table(struct agp_bridge_data *bridge)
324 {
325         struct aper_size_info_lvl2 *value;
326         ati_page_map page_dir;
327         unsigned long addr;
328         int retval;
329         u32 temp;
330         int i;
331         struct aper_size_info_lvl2 *current_size;
332
333         value = A_SIZE_LVL2(agp_bridge->current_size);
334         retval = ati_create_page_map(&page_dir);
335         if (retval != 0)
336                 return retval;
337
338         retval = ati_create_gatt_pages(value->num_entries / 1024);
339         if (retval != 0) {
340                 ati_free_page_map(&page_dir);
341                 return retval;
342         }
343
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_bus(page_dir.real);
347
348         /* Write out the size register */
349         current_size = A_SIZE_LVL2(agp_bridge->current_size);
350
351         if (is_r200()) {
352                 pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
353                 temp = (((temp & ~(0x0000000e)) | current_size->size_value)
354                         | 0x00000001);
355                 pci_write_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, temp);
356                 pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
357         } else {
358                 pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
359                 temp = (((temp & ~(0x0000000e)) | current_size->size_value)
360                         | 0x00000001);
361                 pci_write_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, temp);
362                 pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
363         }
364
365         /*
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
369          */
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;
373
374         /* Calculate the agp offset */
375         for(i = 0; i < value->num_entries / 1024; i++, addr += 0x00400000) {
376                 writel(virt_to_bus(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. */
379         }
380
381         return 0;
382 }
383
384 static int ati_free_gatt_table(struct agp_bridge_data *bridge)
385 {
386         ati_page_map page_dir;
387
388         page_dir.real = (unsigned long *)agp_bridge->gatt_table_real;
389         page_dir.remapped = (unsigned long __iomem *)agp_bridge->gatt_table;
390
391         ati_free_gatt_pages();
392         ati_free_page_map(&page_dir);
393         return 0;
394 }
395
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,
417 };
418
419
420 static struct agp_device_ids ati_agp_device_ids[] __devinitdata =
421 {
422         {
423                 .device_id      = PCI_DEVICE_ID_ATI_RS100,
424                 .chipset_name   = "IGP320/M",
425         },
426         {
427                 .device_id      = PCI_DEVICE_ID_ATI_RS200,
428                 .chipset_name   = "IGP330/340/345/350/M",
429         },
430         {
431                 .device_id      = PCI_DEVICE_ID_ATI_RS200_B,
432                 .chipset_name   = "IGP345M",
433         },
434         {
435                 .device_id      = PCI_DEVICE_ID_ATI_RS250,
436                 .chipset_name   = "IGP7000/M",
437         },
438         {
439                 .device_id      = PCI_DEVICE_ID_ATI_RS300_100,
440                 .chipset_name   = "IGP9100/M",
441         },
442         {
443                 .device_id      = PCI_DEVICE_ID_ATI_RS300_133,
444                 .chipset_name   = "IGP9100/M",
445         },
446         {
447                 .device_id      = PCI_DEVICE_ID_ATI_RS300_166,
448                 .chipset_name   = "IGP9100/M",
449         },
450         {
451                 .device_id      = PCI_DEVICE_ID_ATI_RS300_200,
452                 .chipset_name   = "IGP9100/M",
453         },
454         { }, /* dummy final entry, always present */
455 };
456
457 static int __devinit agp_ati_probe(struct pci_dev *pdev,
458                                    const struct pci_device_id *ent)
459 {
460         struct agp_device_ids *devs = ati_agp_device_ids;
461         struct agp_bridge_data *bridge;
462         u8 cap_ptr;
463         int j;
464
465         cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
466         if (!cap_ptr)
467                 return -ENODEV;
468
469         /* probe for known chipsets */
470         for (j = 0; devs[j].chipset_name; j++) {
471                 if (pdev->device == devs[j].device_id)
472                         goto found;
473         }
474
475         printk(KERN_ERR PFX
476              "Unsupported Ati chipset (device id: %04x)\n", pdev->device);
477         return -ENODEV;
478
479 found:
480         bridge = agp_alloc_bridge();
481         if (!bridge)
482                 return -ENOMEM;
483
484         bridge->dev = pdev;
485         bridge->capndx = cap_ptr;
486         
487         bridge->driver = &ati_generic_bridge;
488
489
490         printk(KERN_INFO PFX "Detected Ati %s chipset\n",
491                         devs[j].chipset_name);
492
493         /* Fill in the mode register */
494         pci_read_config_dword(pdev,
495                         bridge->capndx+PCI_AGP_STATUS,
496                         &bridge->mode);
497
498         pci_set_drvdata(pdev, bridge);
499         return agp_add_bridge(bridge);
500 }
501
502 static void __devexit agp_ati_remove(struct pci_dev *pdev)
503 {
504         struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
505
506         agp_remove_bridge(bridge);
507         agp_put_bridge(bridge);
508 }
509
510 static struct pci_device_id agp_ati_pci_table[] = {
511         {
512         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
513         .class_mask     = ~0,
514         .vendor         = PCI_VENDOR_ID_ATI,
515         .device         = PCI_ANY_ID,
516         .subvendor      = PCI_ANY_ID,
517         .subdevice      = PCI_ANY_ID,
518         },
519         { }
520 };
521
522 MODULE_DEVICE_TABLE(pci, agp_ati_pci_table);
523
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,
529 };
530
531 static int __init agp_ati_init(void)
532 {
533         if (agp_off)
534                 return -EINVAL;
535         return pci_register_driver(&agp_ati_pci_driver);
536 }
537
538 static void __exit agp_ati_cleanup(void)
539 {
540         pci_unregister_driver(&agp_ati_pci_driver);
541 }
542
543 module_init(agp_ati_init);
544 module_exit(agp_ati_cleanup);
545
546 MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>");
547 MODULE_LICENSE("GPL and additional rights");
548