2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2000 Ani Joshi <ajoshi@unixbox.com>
7 * Copyright (C) 2000, 2001 Ralf Baechle <ralf@gnu.org>
8 * Copyright (C) 2005 Ilya A. Volynets-Evenbakh <ilya@total-knowledge.com>
9 * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
10 * IP32 changes by Ilya.
11 * Copyright (C) 2010 Cavium Networks, Inc.
13 #include <linux/dma-direct.h>
14 #include <linux/bootmem.h>
15 #include <linux/swiotlb.h>
16 #include <linux/types.h>
17 #include <linux/init.h>
20 #include <asm/bootinfo.h>
22 #include <asm/octeon/octeon.h>
25 #include <linux/pci.h>
26 #include <asm/octeon/pci-octeon.h>
27 #include <asm/octeon/cvmx-npi-defs.h>
28 #include <asm/octeon/cvmx-pci-defs.h>
30 struct octeon_dma_map_ops {
31 dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
32 phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
35 static dma_addr_t octeon_hole_phys_to_dma(phys_addr_t paddr)
37 if (paddr >= CVMX_PCIE_BAR1_PHYS_BASE && paddr < (CVMX_PCIE_BAR1_PHYS_BASE + CVMX_PCIE_BAR1_PHYS_SIZE))
38 return paddr - CVMX_PCIE_BAR1_PHYS_BASE + CVMX_PCIE_BAR1_RC_BASE;
43 static phys_addr_t octeon_hole_dma_to_phys(dma_addr_t daddr)
45 if (daddr >= CVMX_PCIE_BAR1_RC_BASE)
46 return daddr + CVMX_PCIE_BAR1_PHYS_BASE - CVMX_PCIE_BAR1_RC_BASE;
51 static dma_addr_t octeon_gen1_phys_to_dma(struct device *dev, phys_addr_t paddr)
53 if (paddr >= 0x410000000ull && paddr < 0x420000000ull)
54 paddr -= 0x400000000ull;
55 return octeon_hole_phys_to_dma(paddr);
58 static phys_addr_t octeon_gen1_dma_to_phys(struct device *dev, dma_addr_t daddr)
60 daddr = octeon_hole_dma_to_phys(daddr);
62 if (daddr >= 0x10000000ull && daddr < 0x20000000ull)
63 daddr += 0x400000000ull;
68 static const struct octeon_dma_map_ops octeon_gen1_ops = {
69 .phys_to_dma = octeon_gen1_phys_to_dma,
70 .dma_to_phys = octeon_gen1_dma_to_phys,
73 static dma_addr_t octeon_gen2_phys_to_dma(struct device *dev, phys_addr_t paddr)
75 return octeon_hole_phys_to_dma(paddr);
78 static phys_addr_t octeon_gen2_dma_to_phys(struct device *dev, dma_addr_t daddr)
80 return octeon_hole_dma_to_phys(daddr);
83 static const struct octeon_dma_map_ops octeon_gen2_ops = {
84 .phys_to_dma = octeon_gen2_phys_to_dma,
85 .dma_to_phys = octeon_gen2_dma_to_phys,
88 static dma_addr_t octeon_big_phys_to_dma(struct device *dev, phys_addr_t paddr)
90 if (paddr >= 0x410000000ull && paddr < 0x420000000ull)
91 paddr -= 0x400000000ull;
93 /* Anything in the BAR1 hole or above goes via BAR2 */
94 if (paddr >= 0xf0000000ull)
95 paddr = OCTEON_BAR2_PCI_ADDRESS + paddr;
100 static phys_addr_t octeon_big_dma_to_phys(struct device *dev, dma_addr_t daddr)
102 if (daddr >= OCTEON_BAR2_PCI_ADDRESS)
103 daddr -= OCTEON_BAR2_PCI_ADDRESS;
105 if (daddr >= 0x10000000ull && daddr < 0x20000000ull)
106 daddr += 0x400000000ull;
110 static const struct octeon_dma_map_ops octeon_big_ops = {
111 .phys_to_dma = octeon_big_phys_to_dma,
112 .dma_to_phys = octeon_big_dma_to_phys,
115 static dma_addr_t octeon_small_phys_to_dma(struct device *dev,
118 if (paddr >= 0x410000000ull && paddr < 0x420000000ull)
119 paddr -= 0x400000000ull;
121 /* Anything not in the BAR1 range goes via BAR2 */
122 if (paddr >= octeon_bar1_pci_phys && paddr < octeon_bar1_pci_phys + 0x8000000ull)
123 paddr = paddr - octeon_bar1_pci_phys;
125 paddr = OCTEON_BAR2_PCI_ADDRESS + paddr;
130 static phys_addr_t octeon_small_dma_to_phys(struct device *dev,
133 if (daddr >= OCTEON_BAR2_PCI_ADDRESS)
134 daddr -= OCTEON_BAR2_PCI_ADDRESS;
136 daddr += octeon_bar1_pci_phys;
138 if (daddr >= 0x10000000ull && daddr < 0x20000000ull)
139 daddr += 0x400000000ull;
143 static const struct octeon_dma_map_ops octeon_small_ops = {
144 .phys_to_dma = octeon_small_phys_to_dma,
145 .dma_to_phys = octeon_small_dma_to_phys,
148 static const struct octeon_dma_map_ops *octeon_pci_dma_ops;
150 void __init octeon_pci_dma_init(void)
152 switch (octeon_dma_bar_type) {
153 case OCTEON_DMA_BAR_TYPE_PCIE:
154 octeon_pci_dma_ops = &octeon_gen1_ops;
156 case OCTEON_DMA_BAR_TYPE_PCIE2:
157 octeon_pci_dma_ops = &octeon_gen2_ops;
159 case OCTEON_DMA_BAR_TYPE_BIG:
160 octeon_pci_dma_ops = &octeon_big_ops;
162 case OCTEON_DMA_BAR_TYPE_SMALL:
163 octeon_pci_dma_ops = &octeon_small_ops;
169 #endif /* CONFIG_PCI */
171 dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr)
174 if (dev && dev_is_pci(dev))
175 return octeon_pci_dma_ops->phys_to_dma(dev, paddr);
180 phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr)
183 if (dev && dev_is_pci(dev))
184 return octeon_pci_dma_ops->dma_to_phys(dev, daddr);
189 char *octeon_swiotlb;
191 void __init plat_swiotlb_setup(void)
194 phys_addr_t max_addr;
195 phys_addr_t addr_size;
197 unsigned long swiotlb_nslabs;
202 for (i = 0 ; i < boot_mem_map.nr_map; i++) {
203 struct boot_mem_map_entry *e = &boot_mem_map.map[i];
204 if (e->type != BOOT_MEM_RAM && e->type != BOOT_MEM_INIT_RAM)
207 /* These addresses map low for PCI. */
208 if (e->addr > 0x410000000ull && !OCTEON_IS_OCTEON2())
211 addr_size += e->size;
213 if (max_addr < e->addr + e->size)
214 max_addr = e->addr + e->size;
218 swiotlbsize = PAGE_SIZE;
222 * For OCTEON_DMA_BAR_TYPE_SMALL, size the iotlb at 1/4 memory
223 * size to a maximum of 64MB
225 if (OCTEON_IS_MODEL(OCTEON_CN31XX)
226 || OCTEON_IS_MODEL(OCTEON_CN38XX_PASS2)) {
227 swiotlbsize = addr_size / 4;
228 if (swiotlbsize > 64 * (1<<20))
229 swiotlbsize = 64 * (1<<20);
230 } else if (max_addr > 0xf0000000ul) {
232 * Otherwise only allocate a big iotlb if there is
233 * memory past the BAR1 hole.
235 swiotlbsize = 64 * (1<<20);
238 #ifdef CONFIG_USB_OHCI_HCD_PLATFORM
239 /* OCTEON II ohci is only 32-bit. */
240 if (OCTEON_IS_OCTEON2() && max_addr >= 0x100000000ul)
241 swiotlbsize = 64 * (1<<20);
243 swiotlb_nslabs = swiotlbsize >> IO_TLB_SHIFT;
244 swiotlb_nslabs = ALIGN(swiotlb_nslabs, IO_TLB_SEGSIZE);
245 swiotlbsize = swiotlb_nslabs << IO_TLB_SHIFT;
247 octeon_swiotlb = alloc_bootmem_low_pages(swiotlbsize);
249 if (swiotlb_init_with_tbl(octeon_swiotlb, swiotlb_nslabs, 1) == -ENOMEM)
250 panic("Cannot allocate SWIOTLB buffer");