2 * UniNorth AGPGART routines.
4 #include <linux/module.h>
6 #include <linux/slab.h>
7 #include <linux/init.h>
8 #include <linux/pagemap.h>
9 #include <linux/agp_backend.h>
10 #include <linux/delay.h>
11 #include <linux/vmalloc.h>
12 #include <asm/uninorth.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
;
30 static u32 scratch_value
;
32 #define DEFAULT_APERTURE_SIZE 256
33 #define DEFAULT_APERTURE_STRING "256"
34 static char *aperture
= NULL
;
36 static int uninorth_fetch_size(void)
39 struct aper_size_info_32
*values
=
40 A_SIZE_32(agp_bridge
->driver
->aperture_sizes
);
43 char *save
= aperture
;
45 size
= memparse(aperture
, &aperture
) >> 20;
48 for (i
= 0; i
< agp_bridge
->driver
->num_aperture_sizes
; i
++)
49 if (size
== values
[i
].size
)
52 if (i
== agp_bridge
->driver
->num_aperture_sizes
) {
53 dev_err(&agp_bridge
->dev
->dev
, "invalid aperture size, "
61 for (i
= 0; i
< agp_bridge
->driver
->num_aperture_sizes
; i
++)
62 if (values
[i
].size
== DEFAULT_APERTURE_SIZE
)
66 agp_bridge
->previous_size
=
67 agp_bridge
->current_size
= (void *)(values
+ i
);
68 agp_bridge
->aperture_size_idx
= i
;
69 return values
[i
].size
;
72 static void uninorth_tlbflush(struct agp_memory
*mem
)
74 u32 ctrl
= UNI_N_CFG_GART_ENABLE
;
77 ctrl
|= U3_N_CFG_GART_PERFRD
;
78 pci_write_config_dword(agp_bridge
->dev
, UNI_N_CFG_GART_CTRL
,
79 ctrl
| UNI_N_CFG_GART_INVAL
);
80 pci_write_config_dword(agp_bridge
->dev
, UNI_N_CFG_GART_CTRL
, ctrl
);
82 if (!mem
&& uninorth_rev
<= 0x30) {
83 pci_write_config_dword(agp_bridge
->dev
, UNI_N_CFG_GART_CTRL
,
84 ctrl
| UNI_N_CFG_GART_2xRESET
);
85 pci_write_config_dword(agp_bridge
->dev
, UNI_N_CFG_GART_CTRL
,
90 static void uninorth_cleanup(void)
94 pci_read_config_dword(agp_bridge
->dev
, UNI_N_CFG_GART_CTRL
, &tmp
);
95 if (!(tmp
& UNI_N_CFG_GART_ENABLE
))
97 tmp
|= UNI_N_CFG_GART_INVAL
;
98 pci_write_config_dword(agp_bridge
->dev
, UNI_N_CFG_GART_CTRL
, tmp
);
99 pci_write_config_dword(agp_bridge
->dev
, UNI_N_CFG_GART_CTRL
, 0);
101 if (uninorth_rev
<= 0x30) {
102 pci_write_config_dword(agp_bridge
->dev
, UNI_N_CFG_GART_CTRL
,
103 UNI_N_CFG_GART_2xRESET
);
104 pci_write_config_dword(agp_bridge
->dev
, UNI_N_CFG_GART_CTRL
,
109 static int uninorth_configure(void)
111 struct aper_size_info_32
*current_size
;
113 current_size
= A_SIZE_32(agp_bridge
->current_size
);
115 dev_info(&agp_bridge
->dev
->dev
, "configuring for size idx: %d\n",
116 current_size
->size_value
);
118 /* aperture size and gatt addr */
119 pci_write_config_dword(agp_bridge
->dev
,
121 (agp_bridge
->gatt_bus_addr
& 0xfffff000)
122 | current_size
->size_value
);
125 * UniNorth seem to be buggy enough not to handle properly when
126 * the AGP aperture isn't mapped at bus physical address 0
128 agp_bridge
->gart_bus_addr
= 0;
130 /* Assume U3 or later on PPC64 systems */
131 /* high 4 bits of GART physical address go in UNI_N_CFG_AGP_BASE */
132 pci_write_config_dword(agp_bridge
->dev
, UNI_N_CFG_AGP_BASE
,
133 (agp_bridge
->gatt_bus_addr
>> 32) & 0xf);
135 pci_write_config_dword(agp_bridge
->dev
,
136 UNI_N_CFG_AGP_BASE
, agp_bridge
->gart_bus_addr
);
140 pci_write_config_dword(agp_bridge
->dev
,
141 UNI_N_CFG_GART_DUMMY_PAGE
,
142 page_to_phys(agp_bridge
->scratch_page_page
) >> 12);
148 static int uninorth_insert_memory(struct agp_memory
*mem
, off_t pg_start
, int type
)
155 if (type
!= mem
->type
)
158 mask_type
= agp_bridge
->driver
->agp_type_to_mask_type(agp_bridge
, type
);
159 if (mask_type
!= 0) {
160 /* We know nothing of memory types */
164 if (mem
->page_count
== 0)
167 temp
= agp_bridge
->current_size
;
168 num_entries
= A_SIZE_32(temp
)->num_entries
;
170 if ((pg_start
+ mem
->page_count
) > num_entries
)
173 gp
= (u32
*) &agp_bridge
->gatt_table
[pg_start
];
174 for (i
= 0; i
< mem
->page_count
; ++i
) {
175 if (gp
[i
] != scratch_value
) {
176 dev_info(&agp_bridge
->dev
->dev
,
177 "uninorth_insert_memory: entry 0x%x occupied (%x)\n",
183 for (i
= 0; i
< mem
->page_count
; i
++) {
185 gp
[i
] = (page_to_phys(mem
->pages
[i
]) >> PAGE_SHIFT
) | 0x80000000UL
;
187 gp
[i
] = cpu_to_le32((page_to_phys(mem
->pages
[i
]) & 0xFFFFF000UL
) |
189 flush_dcache_range((unsigned long)__va(page_to_phys(mem
->pages
[i
])),
190 (unsigned long)__va(page_to_phys(mem
->pages
[i
]))+0x1000);
193 uninorth_tlbflush(mem
);
198 int uninorth_remove_memory(struct agp_memory
*mem
, off_t pg_start
, int type
)
204 if (type
!= mem
->type
)
207 mask_type
= agp_bridge
->driver
->agp_type_to_mask_type(agp_bridge
, type
);
208 if (mask_type
!= 0) {
209 /* We know nothing of memory types */
213 if (mem
->page_count
== 0)
216 gp
= (u32
*) &agp_bridge
->gatt_table
[pg_start
];
217 for (i
= 0; i
< mem
->page_count
; ++i
) {
218 gp
[i
] = scratch_value
;
221 uninorth_tlbflush(mem
);
226 static void uninorth_agp_enable(struct agp_bridge_data
*bridge
, u32 mode
)
228 u32 command
, scratch
, status
;
231 pci_read_config_dword(bridge
->dev
,
232 bridge
->capndx
+ PCI_AGP_STATUS
,
235 command
= agp_collect_device_status(bridge
, mode
, status
);
236 command
|= PCI_AGP_COMMAND_AGP
;
238 if (uninorth_rev
== 0x21) {
240 * Darwin disable AGP 4x on this revision, thus we
241 * may assume it's broken. This is an AGP2 controller.
243 command
&= ~AGPSTAT2_4X
;
246 if ((uninorth_rev
>= 0x30) && (uninorth_rev
<= 0x33)) {
248 * We need to set REQ_DEPTH to 7 for U3 versions 1.0, 2.1,
249 * 2.2 and 2.3, Darwin do so.
251 if ((command
>> AGPSTAT_RQ_DEPTH_SHIFT
) > 7)
252 command
= (command
& ~AGPSTAT_RQ_DEPTH
)
253 | (7 << AGPSTAT_RQ_DEPTH_SHIFT
);
256 uninorth_tlbflush(NULL
);
260 pci_write_config_dword(bridge
->dev
,
261 bridge
->capndx
+ PCI_AGP_COMMAND
,
263 pci_read_config_dword(bridge
->dev
,
264 bridge
->capndx
+ PCI_AGP_COMMAND
,
266 } while ((scratch
& PCI_AGP_COMMAND_AGP
) == 0 && ++timeout
< 1000);
267 if ((scratch
& PCI_AGP_COMMAND_AGP
) == 0)
268 dev_err(&bridge
->dev
->dev
, "can't write UniNorth AGP "
269 "command register\n");
271 if (uninorth_rev
>= 0x30) {
272 /* This is an AGP V3 */
273 agp_device_command(command
, (status
& AGPSTAT_MODE_3_0
) != 0);
276 agp_device_command(command
, false);
279 uninorth_tlbflush(NULL
);
284 * These Power Management routines are _not_ called by the normal PCI PM layer,
285 * but directly by the video driver through function pointers in the device
288 static int agp_uninorth_suspend(struct pci_dev
*pdev
)
290 struct agp_bridge_data
*bridge
;
293 struct pci_dev
*device
= NULL
;
295 bridge
= agp_find_bridge(pdev
);
299 /* Only one suspend supported */
300 if (bridge
->dev_private_data
)
303 /* turn off AGP on the video chip, if it was enabled */
304 for_each_pci_dev(device
) {
305 /* Don't touch the bridge yet, device first */
308 /* Only deal with devices on the same bus here, no Mac has a P2P
309 * bridge on the AGP port, and mucking around the entire PCI
310 * tree is source of problems on some machines because of a bug
311 * in some versions of pci_find_capability() when hitting a dead
314 if (device
->bus
!= pdev
->bus
)
316 agp
= pci_find_capability(device
, PCI_CAP_ID_AGP
);
319 pci_read_config_dword(device
, agp
+ PCI_AGP_COMMAND
, &cmd
);
320 if (!(cmd
& PCI_AGP_COMMAND_AGP
))
322 dev_info(&pdev
->dev
, "disabling AGP on device %s\n",
324 cmd
&= ~PCI_AGP_COMMAND_AGP
;
325 pci_write_config_dword(device
, agp
+ PCI_AGP_COMMAND
, cmd
);
328 /* turn off AGP on the bridge */
329 agp
= pci_find_capability(pdev
, PCI_CAP_ID_AGP
);
330 pci_read_config_dword(pdev
, agp
+ PCI_AGP_COMMAND
, &cmd
);
331 bridge
->dev_private_data
= (void *)(long)cmd
;
332 if (cmd
& PCI_AGP_COMMAND_AGP
) {
333 dev_info(&pdev
->dev
, "disabling AGP on bridge\n");
334 cmd
&= ~PCI_AGP_COMMAND_AGP
;
335 pci_write_config_dword(pdev
, agp
+ PCI_AGP_COMMAND
, cmd
);
337 /* turn off the GART */
343 static int agp_uninorth_resume(struct pci_dev
*pdev
)
345 struct agp_bridge_data
*bridge
;
348 bridge
= agp_find_bridge(pdev
);
352 command
= (long)bridge
->dev_private_data
;
353 bridge
->dev_private_data
= NULL
;
354 if (!(command
& PCI_AGP_COMMAND_AGP
))
357 uninorth_agp_enable(bridge
, command
);
361 #endif /* CONFIG_PM */
364 struct page
**pages_arr
;
367 static int uninorth_create_gatt_table(struct agp_bridge_data
*bridge
)
378 /* We can't handle 2 level gatt's */
379 if (bridge
->driver
->size_type
== LVL2_APER_SIZE
)
383 i
= bridge
->aperture_size_idx
;
384 temp
= bridge
->current_size
;
385 size
= page_order
= num_entries
= 0;
388 size
= A_SIZE_32(temp
)->size
;
389 page_order
= A_SIZE_32(temp
)->page_order
;
390 num_entries
= A_SIZE_32(temp
)->num_entries
;
392 table
= (char *) __get_free_pages(GFP_KERNEL
, page_order
);
396 bridge
->current_size
= A_IDX32(bridge
);
398 bridge
->aperture_size_idx
= i
;
400 } while (!table
&& (i
< bridge
->driver
->num_aperture_sizes
));
405 uninorth_priv
.pages_arr
= kmalloc((1 << page_order
) * sizeof(struct page
*), GFP_KERNEL
);
406 if (uninorth_priv
.pages_arr
== NULL
)
409 table_end
= table
+ ((PAGE_SIZE
* (1 << page_order
)) - 1);
411 for (page
= virt_to_page(table
), i
= 0; page
<= virt_to_page(table_end
);
413 SetPageReserved(page
);
414 uninorth_priv
.pages_arr
[i
] = page
;
417 bridge
->gatt_table_real
= (u32
*) table
;
418 /* Need to clear out any dirty data still sitting in caches */
419 flush_dcache_range((unsigned long)table
,
420 (unsigned long)table_end
+ 1);
421 bridge
->gatt_table
= vmap(uninorth_priv
.pages_arr
, (1 << page_order
), 0, PAGE_KERNEL_NCG
);
423 if (bridge
->gatt_table
== NULL
)
426 bridge
->gatt_bus_addr
= virt_to_phys(table
);
429 scratch_value
= (page_to_phys(agp_bridge
->scratch_page_page
) >> PAGE_SHIFT
) | 0x80000000UL
;
431 scratch_value
= cpu_to_le32((page_to_phys(agp_bridge
->scratch_page_page
) & 0xFFFFF000UL
) |
433 for (i
= 0; i
< num_entries
; i
++)
434 bridge
->gatt_table
[i
] = scratch_value
;
439 kfree(uninorth_priv
.pages_arr
);
441 free_pages((unsigned long)table
, page_order
);
445 static int uninorth_free_gatt_table(struct agp_bridge_data
*bridge
)
448 char *table
, *table_end
;
452 temp
= bridge
->current_size
;
453 page_order
= A_SIZE_32(temp
)->page_order
;
455 /* Do not worry about freeing memory, because if this is
456 * called, then all agp memory is deallocated and removed
460 vunmap(bridge
->gatt_table
);
461 kfree(uninorth_priv
.pages_arr
);
462 table
= (char *) bridge
->gatt_table_real
;
463 table_end
= table
+ ((PAGE_SIZE
* (1 << page_order
)) - 1);
465 for (page
= virt_to_page(table
); page
<= virt_to_page(table_end
); page
++)
466 ClearPageReserved(page
);
468 free_pages((unsigned long) bridge
->gatt_table_real
, page_order
);
473 void null_cache_flush(void)
480 static const struct aper_size_info_32 uninorth_sizes
[] =
492 * Not sure that u3 supports that high aperture sizes but it
493 * would strange if it did not :)
495 static const struct aper_size_info_32 u3_sizes
[] =
497 {512, 131072, 7, 128},
507 const struct agp_bridge_driver uninorth_agp_driver
= {
508 .owner
= THIS_MODULE
,
509 .aperture_sizes
= (void *)uninorth_sizes
,
510 .size_type
= U32_APER_SIZE
,
511 .num_aperture_sizes
= ARRAY_SIZE(uninorth_sizes
),
512 .configure
= uninorth_configure
,
513 .fetch_size
= uninorth_fetch_size
,
514 .cleanup
= uninorth_cleanup
,
515 .tlb_flush
= uninorth_tlbflush
,
516 .mask_memory
= agp_generic_mask_memory
,
518 .cache_flush
= null_cache_flush
,
519 .agp_enable
= uninorth_agp_enable
,
520 .create_gatt_table
= uninorth_create_gatt_table
,
521 .free_gatt_table
= uninorth_free_gatt_table
,
522 .insert_memory
= uninorth_insert_memory
,
523 .remove_memory
= uninorth_remove_memory
,
524 .alloc_by_type
= agp_generic_alloc_by_type
,
525 .free_by_type
= agp_generic_free_by_type
,
526 .agp_alloc_page
= agp_generic_alloc_page
,
527 .agp_alloc_pages
= agp_generic_alloc_pages
,
528 .agp_destroy_page
= agp_generic_destroy_page
,
529 .agp_destroy_pages
= agp_generic_destroy_pages
,
530 .agp_type_to_mask_type
= agp_generic_type_to_mask_type
,
531 .cant_use_aperture
= true,
532 .needs_scratch_page
= true,
535 const struct agp_bridge_driver u3_agp_driver
= {
536 .owner
= THIS_MODULE
,
537 .aperture_sizes
= (void *)u3_sizes
,
538 .size_type
= U32_APER_SIZE
,
539 .num_aperture_sizes
= ARRAY_SIZE(u3_sizes
),
540 .configure
= uninorth_configure
,
541 .fetch_size
= uninorth_fetch_size
,
542 .cleanup
= uninorth_cleanup
,
543 .tlb_flush
= uninorth_tlbflush
,
544 .mask_memory
= agp_generic_mask_memory
,
546 .cache_flush
= null_cache_flush
,
547 .agp_enable
= uninorth_agp_enable
,
548 .create_gatt_table
= uninorth_create_gatt_table
,
549 .free_gatt_table
= uninorth_free_gatt_table
,
550 .insert_memory
= uninorth_insert_memory
,
551 .remove_memory
= uninorth_remove_memory
,
552 .alloc_by_type
= agp_generic_alloc_by_type
,
553 .free_by_type
= agp_generic_free_by_type
,
554 .agp_alloc_page
= agp_generic_alloc_page
,
555 .agp_alloc_pages
= agp_generic_alloc_pages
,
556 .agp_destroy_page
= agp_generic_destroy_page
,
557 .agp_destroy_pages
= agp_generic_destroy_pages
,
558 .agp_type_to_mask_type
= agp_generic_type_to_mask_type
,
559 .cant_use_aperture
= true,
560 .needs_scratch_page
= true,
563 static struct agp_device_ids uninorth_agp_device_ids
[] = {
565 .device_id
= PCI_DEVICE_ID_APPLE_UNI_N_AGP
,
566 .chipset_name
= "UniNorth",
569 .device_id
= PCI_DEVICE_ID_APPLE_UNI_N_AGP_P
,
570 .chipset_name
= "UniNorth/Pangea",
573 .device_id
= PCI_DEVICE_ID_APPLE_UNI_N_AGP15
,
574 .chipset_name
= "UniNorth 1.5",
577 .device_id
= PCI_DEVICE_ID_APPLE_UNI_N_AGP2
,
578 .chipset_name
= "UniNorth 2",
581 .device_id
= PCI_DEVICE_ID_APPLE_U3_AGP
,
582 .chipset_name
= "U3",
585 .device_id
= PCI_DEVICE_ID_APPLE_U3L_AGP
,
586 .chipset_name
= "U3L",
589 .device_id
= PCI_DEVICE_ID_APPLE_U3H_AGP
,
590 .chipset_name
= "U3H",
593 .device_id
= PCI_DEVICE_ID_APPLE_IPID2_AGP
,
594 .chipset_name
= "UniNorth/Intrepid2",
598 static int agp_uninorth_probe(struct pci_dev
*pdev
,
599 const struct pci_device_id
*ent
)
601 struct agp_device_ids
*devs
= uninorth_agp_device_ids
;
602 struct agp_bridge_data
*bridge
;
603 struct device_node
*uninorth_node
;
607 cap_ptr
= pci_find_capability(pdev
, PCI_CAP_ID_AGP
);
611 /* probe for known chipsets */
612 for (j
= 0; devs
[j
].chipset_name
!= NULL
; ++j
) {
613 if (pdev
->device
== devs
[j
].device_id
) {
614 dev_info(&pdev
->dev
, "Apple %s chipset\n",
615 devs
[j
].chipset_name
);
620 dev_err(&pdev
->dev
, "unsupported Apple chipset [%04x/%04x]\n",
621 pdev
->vendor
, pdev
->device
);
625 /* Set revision to 0 if we could not read it. */
628 /* Locate core99 Uni-N */
629 uninorth_node
= of_find_node_by_name(NULL
, "uni-n");
631 if (uninorth_node
== NULL
) {
633 uninorth_node
= of_find_node_by_name(NULL
, "u3");
636 const int *revprop
= of_get_property(uninorth_node
,
639 uninorth_rev
= *revprop
& 0x3f;
640 of_node_put(uninorth_node
);
644 /* Inform platform of our suspend/resume caps */
645 pmac_register_agp_pm(pdev
, agp_uninorth_suspend
, agp_uninorth_resume
);
648 /* Allocate & setup our driver */
649 bridge
= agp_alloc_bridge();
654 bridge
->driver
= &u3_agp_driver
;
656 bridge
->driver
= &uninorth_agp_driver
;
659 bridge
->capndx
= cap_ptr
;
660 bridge
->flags
= AGP_ERRATA_FASTWRITES
;
662 /* Fill in the mode register */
663 pci_read_config_dword(pdev
, cap_ptr
+PCI_AGP_STATUS
, &bridge
->mode
);
665 pci_set_drvdata(pdev
, bridge
);
666 return agp_add_bridge(bridge
);
669 static void agp_uninorth_remove(struct pci_dev
*pdev
)
671 struct agp_bridge_data
*bridge
= pci_get_drvdata(pdev
);
674 /* Inform platform of our suspend/resume caps */
675 pmac_register_agp_pm(pdev
, NULL
, NULL
);
678 agp_remove_bridge(bridge
);
679 agp_put_bridge(bridge
);
682 static const struct pci_device_id agp_uninorth_pci_table
[] = {
684 .class = (PCI_CLASS_BRIDGE_HOST
<< 8),
686 .vendor
= PCI_VENDOR_ID_APPLE
,
687 .device
= PCI_ANY_ID
,
688 .subvendor
= PCI_ANY_ID
,
689 .subdevice
= PCI_ANY_ID
,
694 MODULE_DEVICE_TABLE(pci
, agp_uninorth_pci_table
);
696 static struct pci_driver agp_uninorth_pci_driver
= {
697 .name
= "agpgart-uninorth",
698 .id_table
= agp_uninorth_pci_table
,
699 .probe
= agp_uninorth_probe
,
700 .remove
= agp_uninorth_remove
,
703 static int __init
agp_uninorth_init(void)
707 return pci_register_driver(&agp_uninorth_pci_driver
);
710 static void __exit
agp_uninorth_cleanup(void)
712 pci_unregister_driver(&agp_uninorth_pci_driver
);
715 module_init(agp_uninorth_init
);
716 module_exit(agp_uninorth_cleanup
);
718 module_param(aperture
, charp
, 0);
719 MODULE_PARM_DESC(aperture
,
720 "Aperture size, must be power of two between 4MB and an\n"
721 "\t\tupper limit specific to the UniNorth revision.\n"
722 "\t\tDefault: " DEFAULT_APERTURE_STRING
"M");
724 MODULE_AUTHOR("Ben Herrenschmidt & Paul Mackerras");
725 MODULE_LICENSE("GPL");