2 * UniNorth AGPGART routines.
4 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/pagemap.h>
8 #include <linux/agp_backend.h>
9 #include <linux/delay.h>
10 #include <linux/vmalloc.h>
11 #include <asm/uninorth.h>
12 #include <asm/pci-bridge.h>
14 #include <asm/pmac_feature.h>
18 * NOTES for uninorth3 (G5 AGP) supports :
20 * There maybe also possibility to have bigger cache line size for
21 * agp (see pmac_pci.c and look for cache line). Need to be investigated
24 * PAGE size are hardcoded but this may change, see asm/page.h.
26 * Jerome Glisse <j.glisse@gmail.com>
28 static int uninorth_rev;
31 #define DEFAULT_APERTURE_SIZE 256
32 #define DEFAULT_APERTURE_STRING "256"
33 static char *aperture = NULL;
35 static int uninorth_fetch_size(void)
38 struct aper_size_info_32 *values =
39 A_SIZE_32(agp_bridge->driver->aperture_sizes);
42 char *save = aperture;
44 size = memparse(aperture, &aperture) >> 20;
47 for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++)
48 if (size == values[i].size)
51 if (i == agp_bridge->driver->num_aperture_sizes) {
52 dev_err(&agp_bridge->dev->dev, "invalid aperture size, "
60 for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++)
61 if (values[i].size == DEFAULT_APERTURE_SIZE)
65 agp_bridge->previous_size =
66 agp_bridge->current_size = (void *)(values + i);
67 agp_bridge->aperture_size_idx = i;
68 return values[i].size;
71 static void uninorth_tlbflush(struct agp_memory *mem)
73 u32 ctrl = UNI_N_CFG_GART_ENABLE;
76 ctrl |= U3_N_CFG_GART_PERFRD;
77 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
78 ctrl | UNI_N_CFG_GART_INVAL);
79 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, ctrl);
81 if (uninorth_rev <= 0x30) {
82 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
83 ctrl | UNI_N_CFG_GART_2xRESET);
84 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
89 static void uninorth_cleanup(void)
93 pci_read_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, &tmp);
94 if (!(tmp & UNI_N_CFG_GART_ENABLE))
96 tmp |= UNI_N_CFG_GART_INVAL;
97 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, tmp);
98 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, 0);
100 if (uninorth_rev <= 0x30) {
101 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
102 UNI_N_CFG_GART_2xRESET);
103 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
108 static int uninorth_configure(void)
110 struct aper_size_info_32 *current_size;
112 current_size = A_SIZE_32(agp_bridge->current_size);
114 dev_info(&agp_bridge->dev->dev, "configuring for size idx: %d\n",
115 current_size->size_value);
117 /* aperture size and gatt addr */
118 pci_write_config_dword(agp_bridge->dev,
120 (agp_bridge->gatt_bus_addr & 0xfffff000)
121 | current_size->size_value);
124 * UniNorth seem to be buggy enough not to handle properly when
125 * the AGP aperture isn't mapped at bus physical address 0
127 agp_bridge->gart_bus_addr = 0;
129 /* Assume U3 or later on PPC64 systems */
130 /* high 4 bits of GART physical address go in UNI_N_CFG_AGP_BASE */
131 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_AGP_BASE,
132 (agp_bridge->gatt_bus_addr >> 32) & 0xf);
134 pci_write_config_dword(agp_bridge->dev,
135 UNI_N_CFG_AGP_BASE, agp_bridge->gart_bus_addr);
139 pci_write_config_dword(agp_bridge->dev,
140 UNI_N_CFG_GART_DUMMY_PAGE,
141 page_to_phys(agp_bridge->scratch_page_page) >> 12);
147 static int uninorth_insert_memory(struct agp_memory *mem, off_t pg_start,
150 int i, j, num_entries;
154 temp = agp_bridge->current_size;
155 num_entries = A_SIZE_32(temp)->num_entries;
157 if (type != mem->type)
160 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
161 if (mask_type != 0) {
162 /* We know nothing of memory types */
166 if ((pg_start + mem->page_count) > num_entries)
171 while (j < (pg_start + mem->page_count)) {
172 if (agp_bridge->gatt_table[j])
177 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
178 agp_bridge->gatt_table[j] =
179 cpu_to_le32((page_to_phys(mem->pages[i]) & 0xFFFFF000UL) | 0x1UL);
180 flush_dcache_range((unsigned long)__va(page_to_phys(mem->pages[i])),
181 (unsigned long)__va(page_to_phys(mem->pages[i]))+0x1000);
183 (void)in_le32((volatile u32*)&agp_bridge->gatt_table[pg_start]);
186 uninorth_tlbflush(mem);
190 static int u3_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
197 temp = agp_bridge->current_size;
198 num_entries = A_SIZE_32(temp)->num_entries;
200 if (type != mem->type)
203 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
204 if (mask_type != 0) {
205 /* We know nothing of memory types */
209 if ((pg_start + mem->page_count) > num_entries)
212 gp = (u32 *) &agp_bridge->gatt_table[pg_start];
213 for (i = 0; i < mem->page_count; ++i) {
215 dev_info(&agp_bridge->dev->dev,
216 "u3_insert_memory: entry 0x%x occupied (%x)\n",
222 for (i = 0; i < mem->page_count; i++) {
223 gp[i] = (page_to_phys(mem->pages[i]) >> PAGE_SHIFT) | 0x80000000UL;
224 flush_dcache_range((unsigned long)__va(page_to_phys(mem->pages[i])),
225 (unsigned long)__va(page_to_phys(mem->pages[i]))+0x1000);
228 uninorth_tlbflush(mem);
233 int u3_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
238 if (type != 0 || mem->type != 0)
239 /* We know nothing of memory types */
242 gp = (u32 *) &agp_bridge->gatt_table[pg_start];
243 for (i = 0; i < mem->page_count; ++i)
246 uninorth_tlbflush(mem);
251 static void uninorth_agp_enable(struct agp_bridge_data *bridge, u32 mode)
253 u32 command, scratch, status;
256 pci_read_config_dword(bridge->dev,
257 bridge->capndx + PCI_AGP_STATUS,
260 command = agp_collect_device_status(bridge, mode, status);
261 command |= PCI_AGP_COMMAND_AGP;
263 if (uninorth_rev == 0x21) {
265 * Darwin disable AGP 4x on this revision, thus we
266 * may assume it's broken. This is an AGP2 controller.
268 command &= ~AGPSTAT2_4X;
271 if ((uninorth_rev >= 0x30) && (uninorth_rev <= 0x33)) {
273 * We need to to set REQ_DEPTH to 7 for U3 versions 1.0, 2.1,
274 * 2.2 and 2.3, Darwin do so.
276 if ((command >> AGPSTAT_RQ_DEPTH_SHIFT) > 7)
277 command = (command & ~AGPSTAT_RQ_DEPTH)
278 | (7 << AGPSTAT_RQ_DEPTH_SHIFT);
281 uninorth_tlbflush(NULL);
285 pci_write_config_dword(bridge->dev,
286 bridge->capndx + PCI_AGP_COMMAND,
288 pci_read_config_dword(bridge->dev,
289 bridge->capndx + PCI_AGP_COMMAND,
291 } while ((scratch & PCI_AGP_COMMAND_AGP) == 0 && ++timeout < 1000);
292 if ((scratch & PCI_AGP_COMMAND_AGP) == 0)
293 dev_err(&bridge->dev->dev, "can't write UniNorth AGP "
294 "command register\n");
296 if (uninorth_rev >= 0x30) {
297 /* This is an AGP V3 */
298 agp_device_command(command, (status & AGPSTAT_MODE_3_0) != 0);
301 agp_device_command(command, false);
304 uninorth_tlbflush(NULL);
309 * These Power Management routines are _not_ called by the normal PCI PM layer,
310 * but directly by the video driver through function pointers in the device
313 static int agp_uninorth_suspend(struct pci_dev *pdev)
315 struct agp_bridge_data *bridge;
318 struct pci_dev *device = NULL;
320 bridge = agp_find_bridge(pdev);
324 /* Only one suspend supported */
325 if (bridge->dev_private_data)
328 /* turn off AGP on the video chip, if it was enabled */
329 for_each_pci_dev(device) {
330 /* Don't touch the bridge yet, device first */
333 /* Only deal with devices on the same bus here, no Mac has a P2P
334 * bridge on the AGP port, and mucking around the entire PCI
335 * tree is source of problems on some machines because of a bug
336 * in some versions of pci_find_capability() when hitting a dead
339 if (device->bus != pdev->bus)
341 agp = pci_find_capability(device, PCI_CAP_ID_AGP);
344 pci_read_config_dword(device, agp + PCI_AGP_COMMAND, &cmd);
345 if (!(cmd & PCI_AGP_COMMAND_AGP))
347 dev_info(&pdev->dev, "disabling AGP on device %s\n",
349 cmd &= ~PCI_AGP_COMMAND_AGP;
350 pci_write_config_dword(device, agp + PCI_AGP_COMMAND, cmd);
353 /* turn off AGP on the bridge */
354 agp = pci_find_capability(pdev, PCI_CAP_ID_AGP);
355 pci_read_config_dword(pdev, agp + PCI_AGP_COMMAND, &cmd);
356 bridge->dev_private_data = (void *)(long)cmd;
357 if (cmd & PCI_AGP_COMMAND_AGP) {
358 dev_info(&pdev->dev, "disabling AGP on bridge\n");
359 cmd &= ~PCI_AGP_COMMAND_AGP;
360 pci_write_config_dword(pdev, agp + PCI_AGP_COMMAND, cmd);
362 /* turn off the GART */
368 static int agp_uninorth_resume(struct pci_dev *pdev)
370 struct agp_bridge_data *bridge;
373 bridge = agp_find_bridge(pdev);
377 command = (long)bridge->dev_private_data;
378 bridge->dev_private_data = NULL;
379 if (!(command & PCI_AGP_COMMAND_AGP))
382 uninorth_agp_enable(bridge, command);
386 #endif /* CONFIG_PM */
388 static int uninorth_create_gatt_table(struct agp_bridge_data *bridge)
400 /* We can't handle 2 level gatt's */
401 if (bridge->driver->size_type == LVL2_APER_SIZE)
405 i = bridge->aperture_size_idx;
406 temp = bridge->current_size;
407 size = page_order = num_entries = 0;
410 size = A_SIZE_32(temp)->size;
411 page_order = A_SIZE_32(temp)->page_order;
412 num_entries = A_SIZE_32(temp)->num_entries;
414 table = (char *) __get_free_pages(GFP_KERNEL, page_order);
418 bridge->current_size = A_IDX32(bridge);
420 bridge->aperture_size_idx = i;
422 } while (!table && (i < bridge->driver->num_aperture_sizes));
427 pages = kmalloc((1 << page_order) * sizeof(struct page*), GFP_KERNEL);
431 table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
433 for (page = virt_to_page(table), i = 0; page <= virt_to_page(table_end);
435 SetPageReserved(page);
439 bridge->gatt_table_real = (u32 *) table;
440 /* Need to clear out any dirty data still sitting in caches */
441 flush_dcache_range((unsigned long)table,
442 (unsigned long)(table_end + PAGE_SIZE));
443 bridge->gatt_table = vmap(pages, (1 << page_order), 0, PAGE_KERNEL_NCG);
445 if (bridge->gatt_table == NULL)
448 bridge->gatt_bus_addr = virt_to_phys(table);
450 for (i = 0; i < num_entries; i++)
451 bridge->gatt_table[i] = 0;
458 free_pages((unsigned long)table, page_order);
462 static int uninorth_free_gatt_table(struct agp_bridge_data *bridge)
465 char *table, *table_end;
469 temp = bridge->current_size;
470 page_order = A_SIZE_32(temp)->page_order;
472 /* Do not worry about freeing memory, because if this is
473 * called, then all agp memory is deallocated and removed
477 vunmap(bridge->gatt_table);
478 table = (char *) bridge->gatt_table_real;
479 table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
481 for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
482 ClearPageReserved(page);
484 free_pages((unsigned long) bridge->gatt_table_real, page_order);
489 void null_cache_flush(void)
496 static const struct aper_size_info_32 uninorth_sizes[] =
508 * Not sure that u3 supports that high aperture sizes but it
509 * would strange if it did not :)
511 static const struct aper_size_info_32 u3_sizes[] =
513 {512, 131072, 7, 128},
523 const struct agp_bridge_driver uninorth_agp_driver = {
524 .owner = THIS_MODULE,
525 .aperture_sizes = (void *)uninorth_sizes,
526 .size_type = U32_APER_SIZE,
527 .num_aperture_sizes = ARRAY_SIZE(uninorth_sizes),
528 .configure = uninorth_configure,
529 .fetch_size = uninorth_fetch_size,
530 .cleanup = uninorth_cleanup,
531 .tlb_flush = uninorth_tlbflush,
532 .mask_memory = agp_generic_mask_memory,
534 .cache_flush = null_cache_flush,
535 .agp_enable = uninorth_agp_enable,
536 .create_gatt_table = uninorth_create_gatt_table,
537 .free_gatt_table = uninorth_free_gatt_table,
538 .insert_memory = uninorth_insert_memory,
539 .remove_memory = agp_generic_remove_memory,
540 .alloc_by_type = agp_generic_alloc_by_type,
541 .free_by_type = agp_generic_free_by_type,
542 .agp_alloc_page = agp_generic_alloc_page,
543 .agp_alloc_pages = agp_generic_alloc_pages,
544 .agp_destroy_page = agp_generic_destroy_page,
545 .agp_destroy_pages = agp_generic_destroy_pages,
546 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
547 .cant_use_aperture = true,
550 const struct agp_bridge_driver u3_agp_driver = {
551 .owner = THIS_MODULE,
552 .aperture_sizes = (void *)u3_sizes,
553 .size_type = U32_APER_SIZE,
554 .num_aperture_sizes = ARRAY_SIZE(u3_sizes),
555 .configure = uninorth_configure,
556 .fetch_size = uninorth_fetch_size,
557 .cleanup = uninorth_cleanup,
558 .tlb_flush = uninorth_tlbflush,
559 .mask_memory = agp_generic_mask_memory,
561 .cache_flush = null_cache_flush,
562 .agp_enable = uninorth_agp_enable,
563 .create_gatt_table = uninorth_create_gatt_table,
564 .free_gatt_table = uninorth_free_gatt_table,
565 .insert_memory = u3_insert_memory,
566 .remove_memory = u3_remove_memory,
567 .alloc_by_type = agp_generic_alloc_by_type,
568 .free_by_type = agp_generic_free_by_type,
569 .agp_alloc_page = agp_generic_alloc_page,
570 .agp_alloc_pages = agp_generic_alloc_pages,
571 .agp_destroy_page = agp_generic_destroy_page,
572 .agp_destroy_pages = agp_generic_destroy_pages,
573 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
574 .cant_use_aperture = true,
575 .needs_scratch_page = true,
578 static struct agp_device_ids uninorth_agp_device_ids[] __devinitdata = {
580 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP,
581 .chipset_name = "UniNorth",
584 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP_P,
585 .chipset_name = "UniNorth/Pangea",
588 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP15,
589 .chipset_name = "UniNorth 1.5",
592 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP2,
593 .chipset_name = "UniNorth 2",
596 .device_id = PCI_DEVICE_ID_APPLE_U3_AGP,
597 .chipset_name = "U3",
600 .device_id = PCI_DEVICE_ID_APPLE_U3L_AGP,
601 .chipset_name = "U3L",
604 .device_id = PCI_DEVICE_ID_APPLE_U3H_AGP,
605 .chipset_name = "U3H",
608 .device_id = PCI_DEVICE_ID_APPLE_IPID2_AGP,
609 .chipset_name = "UniNorth/Intrepid2",
613 static int __devinit agp_uninorth_probe(struct pci_dev *pdev,
614 const struct pci_device_id *ent)
616 struct agp_device_ids *devs = uninorth_agp_device_ids;
617 struct agp_bridge_data *bridge;
618 struct device_node *uninorth_node;
622 cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
626 /* probe for known chipsets */
627 for (j = 0; devs[j].chipset_name != NULL; ++j) {
628 if (pdev->device == devs[j].device_id) {
629 dev_info(&pdev->dev, "Apple %s chipset\n",
630 devs[j].chipset_name);
635 dev_err(&pdev->dev, "unsupported Apple chipset [%04x/%04x]\n",
636 pdev->vendor, pdev->device);
640 /* Set revision to 0 if we could not read it. */
643 /* Locate core99 Uni-N */
644 uninorth_node = of_find_node_by_name(NULL, "uni-n");
646 if (uninorth_node == NULL) {
648 uninorth_node = of_find_node_by_name(NULL, "u3");
651 const int *revprop = of_get_property(uninorth_node,
654 uninorth_rev = *revprop & 0x3f;
655 of_node_put(uninorth_node);
659 /* Inform platform of our suspend/resume caps */
660 pmac_register_agp_pm(pdev, agp_uninorth_suspend, agp_uninorth_resume);
663 /* Allocate & setup our driver */
664 bridge = agp_alloc_bridge();
669 bridge->driver = &u3_agp_driver;
671 bridge->driver = &uninorth_agp_driver;
674 bridge->capndx = cap_ptr;
675 bridge->flags = AGP_ERRATA_FASTWRITES;
677 /* Fill in the mode register */
678 pci_read_config_dword(pdev, cap_ptr+PCI_AGP_STATUS, &bridge->mode);
680 pci_set_drvdata(pdev, bridge);
681 return agp_add_bridge(bridge);
684 static void __devexit agp_uninorth_remove(struct pci_dev *pdev)
686 struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
689 /* Inform platform of our suspend/resume caps */
690 pmac_register_agp_pm(pdev, NULL, NULL);
693 agp_remove_bridge(bridge);
694 agp_put_bridge(bridge);
697 static struct pci_device_id agp_uninorth_pci_table[] = {
699 .class = (PCI_CLASS_BRIDGE_HOST << 8),
701 .vendor = PCI_VENDOR_ID_APPLE,
702 .device = PCI_ANY_ID,
703 .subvendor = PCI_ANY_ID,
704 .subdevice = PCI_ANY_ID,
709 MODULE_DEVICE_TABLE(pci, agp_uninorth_pci_table);
711 static struct pci_driver agp_uninorth_pci_driver = {
712 .name = "agpgart-uninorth",
713 .id_table = agp_uninorth_pci_table,
714 .probe = agp_uninorth_probe,
715 .remove = agp_uninorth_remove,
718 static int __init agp_uninorth_init(void)
722 return pci_register_driver(&agp_uninorth_pci_driver);
725 static void __exit agp_uninorth_cleanup(void)
727 pci_unregister_driver(&agp_uninorth_pci_driver);
730 module_init(agp_uninorth_init);
731 module_exit(agp_uninorth_cleanup);
733 module_param(aperture, charp, 0);
734 MODULE_PARM_DESC(aperture,
735 "Aperture size, must be power of two between 4MB and an\n"
736 "\t\tupper limit specific to the UniNorth revision.\n"
737 "\t\tDefault: " DEFAULT_APERTURE_STRING "M");
739 MODULE_AUTHOR("Ben Herrenschmidt & Paul Mackerras");
740 MODULE_LICENSE("GPL");