Merge with /pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[linux-2.6] / 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/string.h>
10 #include <linux/slab.h>
11 #include <linux/agp_backend.h>
12 #include <asm/agp.h>
13 #include "agp.h"
14
15 #define ATI_GART_MMBASE_ADDR    0x14
16 #define ATI_RS100_APSIZE        0xac
17 #define ATI_RS100_IG_AGPMODE    0xb0
18 #define ATI_RS300_APSIZE        0xf8
19 #define ATI_RS300_IG_AGPMODE    0xfc
20 #define ATI_GART_FEATURE_ID             0x00
21 #define ATI_GART_BASE                   0x04
22 #define ATI_GART_CACHE_SZBASE           0x08
23 #define ATI_GART_CACHE_CNTRL            0x0c
24 #define ATI_GART_CACHE_ENTRY_CNTRL      0x10
25
26
27 static struct aper_size_info_lvl2 ati_generic_sizes[7] =
28 {
29         {2048, 524288, 0x0000000c},
30         {1024, 262144, 0x0000000a},
31         {512, 131072, 0x00000008},
32         {256, 65536, 0x00000006},
33         {128, 32768, 0x00000004},
34         {64, 16384, 0x00000002},
35         {32, 8192, 0x00000000}
36 };
37
38 static struct gatt_mask ati_generic_masks[] =
39 {
40         { .mask = 1, .type = 0}
41 };
42
43
44
45 typedef struct _ati_page_map {
46         unsigned long *real;
47         unsigned long __iomem *remapped;
48 } ati_page_map;
49
50 static struct _ati_generic_private {
51         volatile u8 __iomem *registers;
52         ati_page_map **gatt_pages;
53         int num_tables;
54 } ati_generic_private;
55
56 static int ati_create_page_map(ati_page_map *page_map)
57 {
58         int i, err = 0;
59
60         page_map->real = (unsigned long *) __get_free_page(GFP_KERNEL);
61         if (page_map->real == NULL)
62                 return -ENOMEM;
63
64         SetPageReserved(virt_to_page(page_map->real));
65         err = map_page_into_agp(virt_to_page(page_map->real));
66         page_map->remapped = ioremap_nocache(virt_to_gart(page_map->real),
67                                             PAGE_SIZE);
68         if (page_map->remapped == NULL || err) {
69                 ClearPageReserved(virt_to_page(page_map->real));
70                 free_page((unsigned long) page_map->real);
71                 page_map->real = NULL;
72                 return -ENOMEM;
73         }
74         /*CACHE_FLUSH();*/
75         global_cache_flush();
76
77         for(i = 0; i < PAGE_SIZE / sizeof(unsigned long); i++) {
78                 writel(agp_bridge->scratch_page, page_map->remapped+i);
79                 readl(page_map->remapped+i);    /* PCI Posting. */
80         }
81
82         return 0;
83 }
84
85
86 static void ati_free_page_map(ati_page_map *page_map)
87 {
88         unmap_page_from_agp(virt_to_page(page_map->real));
89         iounmap(page_map->remapped);
90         ClearPageReserved(virt_to_page(page_map->real));
91         free_page((unsigned long) page_map->real);
92 }
93
94
95 static void ati_free_gatt_pages(void)
96 {
97         int i;
98         ati_page_map **tables;
99         ati_page_map *entry;
100
101         tables = ati_generic_private.gatt_pages;
102         for(i = 0; i < ati_generic_private.num_tables; i++) {
103                 entry = tables[i];
104                 if (entry != NULL) {
105                         if (entry->real != NULL)
106                                 ati_free_page_map(entry);
107                         kfree(entry);
108                 }
109         }
110         kfree(tables);
111 }
112
113
114 static int ati_create_gatt_pages(int nr_tables)
115 {
116         ati_page_map **tables;
117         ati_page_map *entry;
118         int retval = 0;
119         int i;
120
121         tables = kmalloc((nr_tables + 1) * sizeof(ati_page_map *),
122                          GFP_KERNEL);
123         if (tables == NULL)
124                 return -ENOMEM;
125
126         memset(tables, 0, sizeof(ati_page_map *) * (nr_tables + 1));
127         for (i = 0; i < nr_tables; i++) {
128                 entry = kmalloc(sizeof(ati_page_map), GFP_KERNEL);
129                 if (entry == NULL) {
130                         while (i>0) {
131                                 kfree (tables[i-1]);
132                                 i--;
133                         }
134                         kfree (tables);
135                         tables = NULL;
136                         retval = -ENOMEM;
137                         break;
138                 }
139                 memset(entry, 0, sizeof(ati_page_map));
140                 tables[i] = entry;
141                 retval = ati_create_page_map(entry);
142                 if (retval != 0) break;
143         }
144         ati_generic_private.num_tables = nr_tables;
145         ati_generic_private.gatt_pages = tables;
146
147         if (retval != 0) ati_free_gatt_pages();
148
149         return retval;
150 }
151
152 static int is_r200(void)
153 {
154         if ((agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS100) ||
155             (agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS200) ||
156             (agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS200_B) ||
157             (agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS250))
158                 return 1;
159         return 0;
160 }
161
162 static int ati_fetch_size(void)
163 {
164         int i;
165         u32 temp;
166         struct aper_size_info_lvl2 *values;
167
168         if (is_r200())
169                 pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
170         else
171                 pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
172
173         temp = (temp & 0x0000000e);
174         values = A_SIZE_LVL2(agp_bridge->driver->aperture_sizes);
175         for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
176                 if (temp == values[i].size_value) {
177                         agp_bridge->previous_size =
178                             agp_bridge->current_size = (void *) (values + i);
179
180                         agp_bridge->aperture_size_idx = i;
181                         return values[i].size;
182                 }
183         }
184
185         return 0;
186 }
187
188 static void ati_tlbflush(struct agp_memory * mem)
189 {
190         writel(1, ati_generic_private.registers+ATI_GART_CACHE_CNTRL);
191         readl(ati_generic_private.registers+ATI_GART_CACHE_CNTRL);      /* PCI Posting. */
192 }
193
194 static void ati_cleanup(void)
195 {
196         struct aper_size_info_lvl2 *previous_size;
197         u32 temp;
198
199         previous_size = A_SIZE_LVL2(agp_bridge->previous_size);
200
201         /* Write back the previous size and disable gart translation */
202         if (is_r200()) {
203                 pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
204                 temp = ((temp & ~(0x0000000f)) | previous_size->size_value);
205                 pci_write_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, temp);
206         } else {
207                 pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
208                 temp = ((temp & ~(0x0000000f)) | previous_size->size_value);
209                 pci_write_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, temp);
210         }
211         iounmap((volatile u8 __iomem *)ati_generic_private.registers);
212 }
213
214
215 static int ati_configure(void)
216 {
217         u32 temp;
218
219         /* Get the memory mapped registers */
220         pci_read_config_dword(agp_bridge->dev, ATI_GART_MMBASE_ADDR, &temp);
221         temp = (temp & 0xfffff000);
222         ati_generic_private.registers = (volatile u8 __iomem *) ioremap(temp, 4096);
223
224         if (is_r200())
225         pci_write_config_dword(agp_bridge->dev, ATI_RS100_IG_AGPMODE, 0x20000);
226         else
227                 pci_write_config_dword(agp_bridge->dev, ATI_RS300_IG_AGPMODE, 0x20000);
228
229         /* address to map too */
230         /*
231         pci_read_config_dword(agp_bridge.dev, AGP_APBASE, &temp);
232         agp_bridge.gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
233         printk(KERN_INFO PFX "IGP320 gart_bus_addr: %x\n", agp_bridge.gart_bus_addr);
234         */
235         writel(0x60000, ati_generic_private.registers+ATI_GART_FEATURE_ID);
236         readl(ati_generic_private.registers+ATI_GART_FEATURE_ID);       /* PCI Posting.*/
237
238         /* SIGNALED_SYSTEM_ERROR @ NB_STATUS */
239         pci_read_config_dword(agp_bridge->dev, 4, &temp);
240         pci_write_config_dword(agp_bridge->dev, 4, temp | (1<<14));
241
242         /* Write out the address of the gatt table */
243         writel(agp_bridge->gatt_bus_addr, ati_generic_private.registers+ATI_GART_BASE);
244         readl(ati_generic_private.registers+ATI_GART_BASE);     /* PCI Posting. */
245
246         return 0;
247 }
248
249
250 /*
251  *Since we don't need contigious memory we just try
252  * to get the gatt table once
253  */
254
255 #define GET_PAGE_DIR_OFF(addr) (addr >> 22)
256 #define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
257         GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
258 #define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
259 #undef  GET_GATT
260 #define GET_GATT(addr) (ati_generic_private.gatt_pages[\
261         GET_PAGE_DIR_IDX(addr)]->remapped)
262
263 static int ati_insert_memory(struct agp_memory * mem,
264                              off_t pg_start, int type)
265 {
266         int i, j, num_entries;
267         unsigned long __iomem *cur_gatt;
268         unsigned long addr;
269
270         num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
271
272         if (type != 0 || mem->type != 0)
273                 return -EINVAL;
274
275         if ((pg_start + mem->page_count) > num_entries)
276                 return -EINVAL;
277
278         j = pg_start;
279         while (j < (pg_start + mem->page_count)) {
280                 addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
281                 cur_gatt = GET_GATT(addr);
282                 if (!PGE_EMPTY(agp_bridge,readl(cur_gatt+GET_GATT_OFF(addr))))
283                         return -EBUSY;
284                 j++;
285         }
286
287         if (mem->is_flushed == FALSE) {
288                 /*CACHE_FLUSH(); */
289                 global_cache_flush();
290                 mem->is_flushed = TRUE;
291         }
292
293         for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
294                 addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
295                 cur_gatt = GET_GATT(addr);
296                 writel(agp_bridge->driver->mask_memory(agp_bridge,
297                         mem->memory[i], mem->type), cur_gatt+GET_GATT_OFF(addr));
298                 readl(cur_gatt+GET_GATT_OFF(addr));     /* PCI Posting. */
299         }
300         agp_bridge->driver->tlb_flush(mem);
301         return 0;
302 }
303
304 static int ati_remove_memory(struct agp_memory * mem, off_t pg_start,
305                              int type)
306 {
307         int i;
308         unsigned long __iomem *cur_gatt;
309         unsigned long addr;
310
311         if (type != 0 || mem->type != 0) {
312                 return -EINVAL;
313         }
314         for (i = pg_start; i < (mem->page_count + pg_start); i++) {
315                 addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
316                 cur_gatt = GET_GATT(addr);
317                 writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
318                 readl(cur_gatt+GET_GATT_OFF(addr)); /* PCI Posting. */
319         }
320
321         agp_bridge->driver->tlb_flush(mem);
322         return 0;
323 }
324
325 static int ati_create_gatt_table(struct agp_bridge_data *bridge)
326 {
327         struct aper_size_info_lvl2 *value;
328         ati_page_map page_dir;
329         unsigned long addr;
330         int retval;
331         u32 temp;
332         int i;
333         struct aper_size_info_lvl2 *current_size;
334
335         value = A_SIZE_LVL2(agp_bridge->current_size);
336         retval = ati_create_page_map(&page_dir);
337         if (retval != 0)
338                 return retval;
339
340         retval = ati_create_gatt_pages(value->num_entries / 1024);
341         if (retval != 0) {
342                 ati_free_page_map(&page_dir);
343                 return retval;
344         }
345
346         agp_bridge->gatt_table_real = (u32 *)page_dir.real;
347         agp_bridge->gatt_table = (u32 __iomem *) page_dir.remapped;
348         agp_bridge->gatt_bus_addr = virt_to_gart(page_dir.real);
349
350         /* Write out the size register */
351         current_size = A_SIZE_LVL2(agp_bridge->current_size);
352
353         if (is_r200()) {
354                 pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
355                 temp = (((temp & ~(0x0000000e)) | current_size->size_value)
356                         | 0x00000001);
357                 pci_write_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, temp);
358                 pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
359         } else {
360                 pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
361                 temp = (((temp & ~(0x0000000e)) | current_size->size_value)
362                         | 0x00000001);
363                 pci_write_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, temp);
364                 pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
365         }
366
367         /*
368          * Get the address for the gart region.
369          * This is a bus address even on the alpha, b/c its
370          * used to program the agp master not the cpu
371          */
372         pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
373         addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
374         agp_bridge->gart_bus_addr = addr;
375
376         /* Calculate the agp offset */
377         for(i = 0; i < value->num_entries / 1024; i++, addr += 0x00400000) {
378                 writel(virt_to_gart(ati_generic_private.gatt_pages[i]->real) | 1,
379                         page_dir.remapped+GET_PAGE_DIR_OFF(addr));
380                 readl(page_dir.remapped+GET_PAGE_DIR_OFF(addr));        /* PCI Posting. */
381         }
382
383         return 0;
384 }
385
386 static int ati_free_gatt_table(struct agp_bridge_data *bridge)
387 {
388         ati_page_map page_dir;
389
390         page_dir.real = (unsigned long *)agp_bridge->gatt_table_real;
391         page_dir.remapped = (unsigned long __iomem *)agp_bridge->gatt_table;
392
393         ati_free_gatt_pages();
394         ati_free_page_map(&page_dir);
395         return 0;
396 }
397
398 static struct agp_bridge_driver ati_generic_bridge = {
399         .owner                  = THIS_MODULE,
400         .aperture_sizes         = ati_generic_sizes,
401         .size_type              = LVL2_APER_SIZE,
402         .num_aperture_sizes     = 7,
403         .configure              = ati_configure,
404         .fetch_size             = ati_fetch_size,
405         .cleanup                = ati_cleanup,
406         .tlb_flush              = ati_tlbflush,
407         .mask_memory            = agp_generic_mask_memory,
408         .masks                  = ati_generic_masks,
409         .agp_enable             = agp_generic_enable,
410         .cache_flush            = global_cache_flush,
411         .create_gatt_table      = ati_create_gatt_table,
412         .free_gatt_table        = ati_free_gatt_table,
413         .insert_memory          = ati_insert_memory,
414         .remove_memory          = ati_remove_memory,
415         .alloc_by_type          = agp_generic_alloc_by_type,
416         .free_by_type           = agp_generic_free_by_type,
417         .agp_alloc_page         = agp_generic_alloc_page,
418         .agp_destroy_page       = agp_generic_destroy_page,
419 };
420
421
422 static struct agp_device_ids ati_agp_device_ids[] __devinitdata =
423 {
424         {
425                 .device_id      = PCI_DEVICE_ID_ATI_RS100,
426                 .chipset_name   = "IGP320/M",
427         },
428         {
429                 .device_id      = PCI_DEVICE_ID_ATI_RS200,
430                 .chipset_name   = "IGP330/340/345/350/M",
431         },
432         {
433                 .device_id      = PCI_DEVICE_ID_ATI_RS200_B,
434                 .chipset_name   = "IGP345M",
435         },
436         {
437                 .device_id      = PCI_DEVICE_ID_ATI_RS250,
438                 .chipset_name   = "IGP7000/M",
439         },
440         {
441                 .device_id      = PCI_DEVICE_ID_ATI_RS300_100,
442                 .chipset_name   = "IGP9100/M",
443         },
444         {
445                 .device_id      = PCI_DEVICE_ID_ATI_RS300_133,
446                 .chipset_name   = "IGP9100/M",
447         },
448         {
449                 .device_id      = PCI_DEVICE_ID_ATI_RS300_166,
450                 .chipset_name   = "IGP9100/M",
451         },
452         {
453                 .device_id      = PCI_DEVICE_ID_ATI_RS300_200,
454                 .chipset_name   = "IGP9100/M",
455         },
456         { }, /* dummy final entry, always present */
457 };
458
459 static int __devinit agp_ati_probe(struct pci_dev *pdev,
460                                    const struct pci_device_id *ent)
461 {
462         struct agp_device_ids *devs = ati_agp_device_ids;
463         struct agp_bridge_data *bridge;
464         u8 cap_ptr;
465         int j;
466
467         cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
468         if (!cap_ptr)
469                 return -ENODEV;
470
471         /* probe for known chipsets */
472         for (j = 0; devs[j].chipset_name; j++) {
473                 if (pdev->device == devs[j].device_id)
474                         goto found;
475         }
476
477         printk(KERN_ERR PFX
478              "Unsupported Ati chipset (device id: %04x)\n", pdev->device);
479         return -ENODEV;
480
481 found:
482         bridge = agp_alloc_bridge();
483         if (!bridge)
484                 return -ENOMEM;
485
486         bridge->dev = pdev;
487         bridge->capndx = cap_ptr;
488         
489         bridge->driver = &ati_generic_bridge;
490
491
492         printk(KERN_INFO PFX "Detected Ati %s chipset\n",
493                         devs[j].chipset_name);
494
495         /* Fill in the mode register */
496         pci_read_config_dword(pdev,
497                         bridge->capndx+PCI_AGP_STATUS,
498                         &bridge->mode);
499
500         pci_set_drvdata(pdev, bridge);
501         return agp_add_bridge(bridge);
502 }
503
504 static void __devexit agp_ati_remove(struct pci_dev *pdev)
505 {
506         struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
507
508         agp_remove_bridge(bridge);
509         agp_put_bridge(bridge);
510 }
511
512 static struct pci_device_id agp_ati_pci_table[] = {
513         {
514         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
515         .class_mask     = ~0,
516         .vendor         = PCI_VENDOR_ID_ATI,
517         .device         = PCI_ANY_ID,
518         .subvendor      = PCI_ANY_ID,
519         .subdevice      = PCI_ANY_ID,
520         },
521         { }
522 };
523
524 MODULE_DEVICE_TABLE(pci, agp_ati_pci_table);
525
526 static struct pci_driver agp_ati_pci_driver = {
527         .name           = "agpgart-ati",
528         .id_table       = agp_ati_pci_table,
529         .probe          = agp_ati_probe,
530         .remove         = agp_ati_remove,
531 };
532
533 static int __init agp_ati_init(void)
534 {
535         if (agp_off)
536                 return -EINVAL;
537         return pci_register_driver(&agp_ati_pci_driver);
538 }
539
540 static void __exit agp_ati_cleanup(void)
541 {
542         pci_unregister_driver(&agp_ati_pci_driver);
543 }
544
545 module_init(agp_ati_init);
546 module_exit(agp_ati_cleanup);
547
548 MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>");
549 MODULE_LICENSE("GPL and additional rights");
550