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