25 #include <linux/kernel.h>
31 #include <linux/slab.h>
35 #include <asm/iommu.h>
36 #include <asm/machdep.h>
37 #include <asm/pci-bridge.h>
49 #define CELL_IOMMU_REAL_UNMAP
56 #define CELL_IOMMU_STRICT_PROTECTION
62 #define IOC_Reg_Size 0x2000
64 #define IOC_IOPT_CacheInvd 0x908
65 #define IOC_IOPT_CacheInvd_NE_Mask 0xffe0000000000000ul
66 #define IOC_IOPT_CacheInvd_IOPTE_Mask 0x000003fffffffff8ul
67 #define IOC_IOPT_CacheInvd_Busy 0x0000000000000001ul
69 #define IOC_IOST_Origin 0x918
70 #define IOC_IOST_Origin_E 0x8000000000000000ul
71 #define IOC_IOST_Origin_HW 0x0000000000000800ul
72 #define IOC_IOST_Origin_HL 0x0000000000000400ul
74 #define IOC_IO_ExcpStat 0x920
75 #define IOC_IO_ExcpStat_V 0x8000000000000000ul
76 #define IOC_IO_ExcpStat_SPF_Mask 0x6000000000000000ul
77 #define IOC_IO_ExcpStat_SPF_S 0x6000000000000000ul
78 #define IOC_IO_ExcpStat_SPF_P 0x2000000000000000ul
79 #define IOC_IO_ExcpStat_ADDR_Mask 0x00000007fffff000ul
80 #define IOC_IO_ExcpStat_RW_Mask 0x0000000000000800ul
81 #define IOC_IO_ExcpStat_IOID_Mask 0x00000000000007fful
83 #define IOC_IO_ExcpMask 0x928
84 #define IOC_IO_ExcpMask_SFE 0x4000000000000000ul
85 #define IOC_IO_ExcpMask_PFE 0x2000000000000000ul
87 #define IOC_IOCmd_Offset 0x1000
89 #define IOC_IOCmd_Cfg 0xc00
90 #define IOC_IOCmd_Cfg_TE 0x0000800000000000ul
94 #define IOSTE_V 0x8000000000000000ul
95 #define IOSTE_H 0x4000000000000000ul
96 #define IOSTE_PT_Base_RPN_Mask 0x3ffffffffffff000ul
97 #define IOSTE_NPPT_Mask 0x0000000000000fe0ul
98 #define IOSTE_PS_Mask 0x0000000000000007ul
99 #define IOSTE_PS_4K 0x0000000000000001ul
100 #define IOSTE_PS_64K 0x0000000000000003ul
101 #define IOSTE_PS_1M 0x0000000000000005ul
102 #define IOSTE_PS_16M 0x0000000000000007ul
106 #define IO_SEGMENT_SHIFT 28
107 #define IO_PAGENO_BITS(shift) (IO_SEGMENT_SHIFT - (shift))
110 #define SPIDER_DMA_OFFSET 0x80000000ul
139 static int cbe_nr_iommus;
152 n =
min(n_ptes, 1
l << 11);
171 unsigned long *io_pte, base_pte;
178 #ifdef CELL_IOMMU_STRICT_PROTECTION
185 const unsigned long prot = 0xc48;
198 io_pte = (
unsigned long *)tbl->
it_base + (index - tbl->it_offset);
200 for (i = 0; i < npages; i++, uaddr += IOMMU_PAGE_SIZE)
205 invalidate_tce_cache(window->
iommu, io_pte, npages);
207 pr_debug(
"tce_build_cell(index=%lx,n=%lx,dir=%d,base_pte=%lx)\n",
208 index, npages, direction, base_pte);
212 static void tce_free_cell(
struct iommu_table *tbl,
long index,
long npages)
216 unsigned long *io_pte,
pte;
220 pr_debug(
"tce_free_cell(index=%lx,n=%lx)\n", index, npages);
222 #ifdef CELL_IOMMU_REAL_UNMAP
232 io_pte = (
unsigned long *)tbl->
it_base + (index - tbl->it_offset);
234 for (i = 0; i < npages; i++)
239 invalidate_tce_cache(window->
iommu, io_pte, npages);
244 unsigned long stat, spf;
262 stat &= ~IOC_IO_ExcpStat_V;
268 static int cell_iommu_find_ioc(
int nid,
unsigned long *base)
276 for_each_node_by_name(np,
"ioc") {
290 for_each_node_by_type(np,
"cpu") {
291 const unsigned int *nidp;
292 const unsigned long *
tmp;
295 if (nidp && *nidp == nid) {
308 static void cell_iommu_setup_stab(
struct cbe_iommu *iommu,
309 unsigned long dbase,
unsigned long dsize,
310 unsigned long fbase,
unsigned long fsize)
313 unsigned long segments, stab_size;
317 pr_debug(
"%s: iommu[%d]: segments: %lu\n",
318 __func__, iommu->
nid, segments);
321 stab_size = segments *
sizeof(
unsigned long);
328 static unsigned long *cell_iommu_alloc_ptab(
struct cbe_iommu *iommu,
329 unsigned long base,
unsigned long size,
unsigned long gap_base,
330 unsigned long gap_size,
unsigned long page_shift)
334 unsigned long reg, segments, pages_per_segment, ptab_size,
335 n_pte_pages, start_seg, *ptab;
341 pages_per_segment =
max(pages_per_segment,
342 (1 << 12) /
sizeof(
unsigned long));
344 ptab_size = segments * pages_per_segment *
sizeof(
unsigned long);
345 pr_debug(
"%s: iommu[%d]: ptab_size: %lu, order: %d\n", __func__,
351 memset(ptab, 0, ptab_size);
354 n_pte_pages = (pages_per_segment *
sizeof(
unsigned long)) >> 12;
356 pr_debug(
"%s: iommu[%d]: stab at %p, ptab at %p, n_pte_pages: %lu\n",
357 __func__, iommu->
nid, iommu->
stab, ptab,
361 reg =
IOSTE_V | ((n_pte_pages - 1) << 5);
363 switch (page_shift) {
374 pr_debug(
"Setting up IOMMU stab:\n");
375 for (i = start_seg; i < (start_seg + segments); i++) {
376 if (i >= gap_base && i < (gap_base + gap_size)) {
377 pr_debug(
"\toverlap at %d, skipping\n", i);
380 iommu->
stab[
i] = reg | (
__pa(ptab) + (n_pte_pages << 12) *
388 static void cell_iommu_enable_hardware(
struct cbe_iommu *iommu)
391 unsigned long reg, xlate_base;
394 if (cell_iommu_find_ioc(iommu->
nid, &xlate_base))
395 panic(
"%s: missing IOC register mappings for node %d\n",
396 __func__, iommu->
nid);
407 reg & ~IOC_IO_ExcpStat_V);
412 IIC_IRQ_IOEX_ATI | (iommu->
nid << IIC_IRQ_NODE_SHIFT));
428 static void cell_iommu_setup_hardware(
struct cbe_iommu *iommu,
429 unsigned long base,
unsigned long size)
431 cell_iommu_setup_stab(iommu, base, size, 0, 0);
432 iommu->
ptab = cell_iommu_alloc_ptab(iommu, base, size, 0, 0,
434 cell_iommu_enable_hardware(iommu);
439 unsigned long offset,
unsigned long size)
446 if (window->
offset == offset && window->
size == size)
470 unsigned long offset,
unsigned long size,
477 ioid = cell_iommu_get_ioid(np);
479 window = kzalloc_node(
sizeof(*window),
GFP_KERNEL, iommu->
nid);
485 window->
iommu = iommu;
487 window->
table.it_blocksize = 16;
489 window->
table.it_index = iommu->
nid;
519 tce_build_cell(&window->
table, window->
table.it_offset, 1,
525 static struct cbe_iommu *cell_iommu_for_node(
int nid)
529 for (i = 0; i < cbe_nr_iommus; i++)
530 if (iommus[i].nid == nid)
535 static unsigned long cell_dma_direct_offset;
537 static unsigned long dma_iommu_fixed_base;
540 static int iommu_fixed_is_weak;
551 iommu = cell_iommu_for_node(dev_to_node(dev));
552 if (iommu ==
NULL || list_empty(&iommu->
windows)) {
554 of_node_full_name(dev->
of_node), dev_to_node(dev));
559 return &window->
table;
564 static void *dma_fixed_alloc_coherent(
struct device *dev,
size_t size,
568 if (iommu_fixed_is_weak)
571 device_to_mask(dev), flag,
578 static void dma_fixed_free_coherent(
struct device *dev,
size_t size,
582 if (iommu_fixed_is_weak)
590 unsigned long offset,
size_t size,
599 offset, size, device_to_mask(dev),
622 return iommu_map_sg(dev, cell_get_iommu_table(dev), sg, nents,
623 device_to_mask(dev), direction, attrs);
637 static int dma_fixed_dma_supported(
struct device *dev,
u64 mask)
642 static int dma_set_mask_and_switch(
struct device *dev,
u64 dma_mask);
645 .alloc = dma_fixed_alloc_coherent,
646 .free = dma_fixed_free_coherent,
647 .map_sg = dma_fixed_map_sg,
648 .unmap_sg = dma_fixed_unmap_sg,
649 .dma_supported = dma_fixed_dma_supported,
650 .set_dma_mask = dma_set_mask_and_switch,
651 .map_page = dma_fixed_map_page,
652 .unmap_page = dma_fixed_unmap_page,
655 static void cell_dma_dev_setup_fixed(
struct device *dev);
657 static void cell_dma_dev_setup(
struct device *dev)
661 cell_dma_dev_setup_fixed(dev);
663 set_iommu_table_base(dev, cell_get_iommu_table(dev));
665 set_dma_offset(dev, cell_dma_direct_offset);
670 static void cell_pci_dma_dev_setup(
struct pci_dev *dev)
672 cell_dma_dev_setup(&dev->
dev);
687 cell_dma_dev_setup(dev);
693 .notifier_call = cell_of_bus_notify
700 const void *dma_window;
705 if (dma_window ==
NULL) {
727 pr_debug(
"iommu: setting up iommu for node %d (%s)\n",
750 INIT_LIST_HEAD(&iommu->
windows);
756 unsigned long offset)
759 unsigned long base,
size;
761 iommu = cell_iommu_alloc(np);
766 cell_iommu_get_window(np, &base, &size);
768 pr_debug(
"\ttranslating window 0x%lx...0x%lx\n",
769 base, base + size - 1);
772 cell_iommu_setup_hardware(iommu, base, size);
775 cell_iommu_setup_window(iommu, np, base, size,
776 offset >> IOMMU_PAGE_SHIFT);
779 static void __init cell_disable_iommus(
void)
782 unsigned long base,
val;
787 if (cell_iommu_find_ioc(node, &base))
794 pr_debug(
"iommu: cleaning up iommu on node %d\n", node);
807 static int __init cell_iommu_init_disabled(
void)
810 unsigned long base = 0,
size;
816 cell_disable_iommus();
828 for_each_node_by_name(np,
"axon") {
831 if (cell_iommu_get_window(np, &base, &size) == 0)
835 for_each_node_by_name(np,
"pci-internal") {
838 if (cell_iommu_get_window(np, &base, &size) == 0)
849 " (%ldMB) smaller than total memory (%lldMB)\n",
854 cell_dma_direct_offset += base;
856 if (cell_dma_direct_offset != 0)
857 ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;
859 printk(
"iommu: disabled, direct DMA offset is 0x%lx\n",
860 cell_dma_direct_offset);
891 static u64 cell_iommu_get_fixed_address(
struct device *dev)
898 np = of_node_get(dev->
of_node);
909 if (ranges && len > 0)
914 dev_dbg(dev,
"iommu: no dma-ranges found\n");
921 range_size = naddr + nsize + pna;
928 for (i = 0, best = -1, best_size = 0; i < len; i +=
range_size) {
930 size = of_read_number(ranges + i + naddr + pna, nsize);
932 if (cpu_addr == 0 && size > best_size) {
939 dev_addr = of_read_number(ranges + best, naddr);
941 dev_dbg(dev,
"iommu: no suitable range found!\n");
949 static int dma_set_mask_and_switch(
struct device *dev,
u64 dma_mask)
955 cell_iommu_get_fixed_address(dev) != OF_BAD_ADDR)
957 dev_dbg(dev,
"iommu: 64-bit OK, using fixed ops\n");
958 set_dma_ops(dev, &dma_iommu_fixed_ops);
960 dev_dbg(dev,
"iommu: not 64-bit, using default ops\n");
964 cell_dma_dev_setup(dev);
971 static void cell_dma_dev_setup_fixed(
struct device *dev)
975 addr = cell_iommu_get_fixed_address(dev) + dma_iommu_fixed_base;
976 set_dma_offset(dev, addr);
978 dev_dbg(dev,
"iommu: fixed addr = %llx\n", addr);
981 static void insert_16M_pte(
unsigned long addr,
unsigned long *ptab,
982 unsigned long base_pte)
988 ptab = ptab + (segment * (1 << 12) /
sizeof(
unsigned long));
990 pr_debug(
"iommu: addr %lx ptab %p segment %lx offset %lx\n",
991 addr, ptab, segment, offset);
996 static void cell_iommu_setup_fixed_ptab(
struct cbe_iommu *iommu,
997 struct device_node *np,
unsigned long dbase,
unsigned long dsize,
998 unsigned long fbase,
unsigned long fsize)
1000 unsigned long base_pte,
uaddr, ioaddr, *ptab;
1002 ptab = cell_iommu_alloc_ptab(iommu, fbase, fsize, dbase, dsize, 24);
1004 dma_iommu_fixed_base = fbase;
1006 pr_debug(
"iommu: mapping 0x%lx pages from 0x%lx\n", fsize, fbase);
1011 if (iommu_fixed_is_weak)
1012 pr_info(
"IOMMU: Using weak ordering for fixed mapping\n");
1014 pr_info(
"IOMMU: Using strong ordering for fixed mapping\n");
1018 for (uaddr = 0; uaddr < fsize; uaddr += (1 << 24)) {
1020 ioaddr = uaddr + fbase;
1021 if (ioaddr >= dbase && ioaddr < (dbase + dsize)) {
1022 pr_debug(
"iommu: fixed/dynamic overlap, skipping\n");
1026 insert_16M_pte(uaddr, ptab, base_pte);
1032 static int __init cell_iommu_fixed_mapping_init(
void)
1034 unsigned long dbase, dsize, fbase, fsize,
hbase, hend;
1043 pr_debug(
"iommu: fixed mapping disabled, no axons found\n");
1052 pr_debug(
"iommu: no dma-ranges found, no fixed mapping\n");
1062 for_each_node_by_name(np,
"axon") {
1063 cell_iommu_get_window(np, &dbase, &dsize);
1064 fbase =
max(fbase, dbase + dsize);
1067 fbase =
_ALIGN_UP(fbase, 1 << IO_SEGMENT_SHIFT);
1070 if ((fbase + fsize) <= 0x800000000ul)
1080 pr_debug(
"iommu: htab is NULL, on LPAR? Huh?\n");
1087 if ((hbase !=
_ALIGN_UP(hbase, 1 << IO_SEGMENT_SHIFT)) ||
1088 (hend !=
_ALIGN_UP(hend, 1 << IO_SEGMENT_SHIFT))) {
1089 pr_debug(
"iommu: hash window not segment aligned\n");
1094 for_each_node_by_name(np,
"axon") {
1095 cell_iommu_get_window(np, &dbase, &dsize);
1097 if (hbase < dbase || (hend > (dbase + dsize))) {
1098 pr_debug(
"iommu: hash window doesn't fit in"
1099 "real DMA window\n");
1108 for_each_node_by_name(np,
"axon") {
1109 iommu = cell_iommu_alloc(np);
1113 cell_iommu_get_window(np, &dbase, &dsize);
1120 "fixed window 0x%lx-0x%lx\n", iommu->
nid, dbase,
1121 dbase + dsize, fbase, fbase + fsize);
1123 cell_iommu_setup_stab(iommu, dbase, dsize, fbase, fsize);
1124 iommu->
ptab = cell_iommu_alloc_ptab(iommu, dbase, dsize, 0, 0,
1126 cell_iommu_setup_fixed_ptab(iommu, np, dbase, dsize,
1128 cell_iommu_enable_hardware(iommu);
1129 cell_iommu_setup_window(iommu, np, dbase, dsize, 0);
1138 static int iommu_fixed_disabled;
1140 static int __init setup_iommu_fixed(
char *
str)
1144 if (
strcmp(str,
"off") == 0)
1145 iommu_fixed_disabled = 1;
1154 if (
strcmp(str,
"weak") == 0 || (pciep &&
strcmp(str,
"strong") != 0))
1155 iommu_fixed_is_weak = 1;
1161 __setup(
"iommu_fixed=", setup_iommu_fixed);
1163 static u64 cell_dma_get_required_mask(
struct device *dev)
1170 if (!iommu_fixed_disabled &&
1171 cell_iommu_get_fixed_address(dev) != OF_BAD_ADDR)
1175 if (dma_ops->get_required_mask)
1176 return dma_ops->get_required_mask(dev);
1178 WARN_ONCE(1,
"no get_required_mask in %p ops", dma_ops);
1183 static int __init cell_iommu_init(
void)
1194 if (cell_iommu_init_disabled() == 0)
1198 ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;
1199 ppc_md.dma_get_required_mask = cell_dma_get_required_mask;
1200 ppc_md.tce_build = tce_build_cell;
1201 ppc_md.tce_free = tce_free_cell;
1203 if (!iommu_fixed_disabled && cell_iommu_fixed_mapping_init() == 0)
1207 for_each_node_by_name(np,
"axon") {
1210 cell_iommu_init_one(np, 0);
1216 for_each_node_by_name(np,
"pci-internal") {