blob: f03fbc2b0f599fcdab4da4b5011a14aab0080843 [file] [log] [blame]
Olof Johansson31c56d82007-02-04 16:36:55 -06001/*
Olof Johanssonf724bf72008-04-04 13:06:33 -05002 * Copyright (C) 2005-2008, PA Semi, Inc
Olof Johansson31c56d82007-02-04 16:36:55 -06003 *
4 * Maintained by: Olof Johansson <olof@lixom.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#undef DEBUG
21
22#include <linux/types.h>
23#include <linux/spinlock.h>
24#include <linux/pci.h>
25#include <asm/iommu.h>
26#include <asm/machdep.h>
Olof Johanssonaf289e82007-10-03 13:03:54 -050027#include <asm/firmware.h>
Olof Johansson31c56d82007-02-04 16:36:55 -060028
Olof Johansson31c56d82007-02-04 16:36:55 -060029#define IOBMAP_PAGE_SHIFT 12
30#define IOBMAP_PAGE_SIZE (1 << IOBMAP_PAGE_SHIFT)
31#define IOBMAP_PAGE_MASK (IOBMAP_PAGE_SIZE - 1)
32
Olof Johansson31c56d82007-02-04 16:36:55 -060033#define IOB_BASE 0xe0000000
34#define IOB_SIZE 0x3000
35/* Configuration registers */
Olof Johanssonf724bf72008-04-04 13:06:33 -050036#define IOBCAP_REG 0x40
37#define IOBCOM_REG 0x100
Olof Johansson31c56d82007-02-04 16:36:55 -060038/* Enable IOB address translation */
39#define IOBCOM_ATEN 0x00000100
40
41/* Address decode configuration register */
Olof Johanssonf724bf72008-04-04 13:06:33 -050042#define IOB_AD_REG 0x14c
Olof Johansson31c56d82007-02-04 16:36:55 -060043/* IOBCOM_AD_REG fields */
44#define IOB_AD_VGPRT 0x00000e00
45#define IOB_AD_VGAEN 0x00000100
46/* Direct mapping settings */
47#define IOB_AD_MPSEL_MASK 0x00000030
48#define IOB_AD_MPSEL_B38 0x00000000
49#define IOB_AD_MPSEL_B40 0x00000010
50#define IOB_AD_MPSEL_B42 0x00000020
51/* Translation window size / enable */
52#define IOB_AD_TRNG_MASK 0x00000003
53#define IOB_AD_TRNG_256M 0x00000000
54#define IOB_AD_TRNG_2G 0x00000001
55#define IOB_AD_TRNG_128G 0x00000003
56
Olof Johanssonf724bf72008-04-04 13:06:33 -050057#define IOB_TABLEBASE_REG 0x154
Olof Johansson31c56d82007-02-04 16:36:55 -060058
59/* Base of the 64 4-byte L1 registers */
Olof Johanssonf724bf72008-04-04 13:06:33 -050060#define IOB_XLT_L1_REGBASE 0x2b00
Olof Johansson31c56d82007-02-04 16:36:55 -060061
62/* Register to invalidate TLB entries */
Olof Johanssonf724bf72008-04-04 13:06:33 -050063#define IOB_AT_INVAL_TLB_REG 0x2d00
Olof Johansson31c56d82007-02-04 16:36:55 -060064
65/* The top two bits of the level 1 entry contains valid and type flags */
66#define IOBMAP_L1E_V 0x40000000
67#define IOBMAP_L1E_V_B 0x80000000
68
69/* For big page entries, the bottom two bits contains flags */
70#define IOBMAP_L1E_BIG_CACHED 0x00000002
71#define IOBMAP_L1E_BIG_PRIORITY 0x00000001
72
73/* For regular level 2 entries, top 2 bits contain valid and cache flags */
74#define IOBMAP_L2E_V 0x80000000
75#define IOBMAP_L2E_V_CACHED 0xc0000000
76
Olof Johanssonf724bf72008-04-04 13:06:33 -050077static void __iomem *iob;
Olof Johansson31c56d82007-02-04 16:36:55 -060078static u32 iob_l1_emptyval;
79static u32 iob_l2_emptyval;
80static u32 *iob_l2_base;
81
82static struct iommu_table iommu_table_iobmap;
83static int iommu_table_iobmap_inited;
84
Robert Jennings6490c492008-07-24 04:31:16 +100085static int iobmap_build(struct iommu_table *tbl, long index,
Olof Johansson31c56d82007-02-04 16:36:55 -060086 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +100087 enum dma_data_direction direction,
88 struct dma_attrs *attrs)
Olof Johansson31c56d82007-02-04 16:36:55 -060089{
90 u32 *ip;
91 u32 rpn;
92 unsigned long bus_addr;
93
94 pr_debug("iobmap: build at: %lx, %lx, addr: %lx\n", index, npages, uaddr);
95
Olof Johanssondfa70f82007-08-17 13:57:39 +100096 bus_addr = (tbl->it_offset + index) << IOBMAP_PAGE_SHIFT;
Olof Johansson31c56d82007-02-04 16:36:55 -060097
Olof Johansson31c56d82007-02-04 16:36:55 -060098 ip = ((u32 *)tbl->it_base) + index;
99
100 while (npages--) {
Michael Ellerman7db90c02012-07-25 21:19:51 +0000101 rpn = __pa(uaddr) >> IOBMAP_PAGE_SHIFT;
Olof Johansson31c56d82007-02-04 16:36:55 -0600102
103 *(ip++) = IOBMAP_L2E_V | rpn;
104 /* invalidate tlb, can be optimized more */
105 out_le32(iob+IOB_AT_INVAL_TLB_REG, bus_addr >> 14);
106
107 uaddr += IOBMAP_PAGE_SIZE;
108 bus_addr += IOBMAP_PAGE_SIZE;
109 }
Robert Jennings6490c492008-07-24 04:31:16 +1000110 return 0;
Olof Johansson31c56d82007-02-04 16:36:55 -0600111}
112
113
114static void iobmap_free(struct iommu_table *tbl, long index,
115 long npages)
116{
117 u32 *ip;
118 unsigned long bus_addr;
119
120 pr_debug("iobmap: free at: %lx, %lx\n", index, npages);
121
Olof Johanssondfa70f82007-08-17 13:57:39 +1000122 bus_addr = (tbl->it_offset + index) << IOBMAP_PAGE_SHIFT;
Olof Johansson31c56d82007-02-04 16:36:55 -0600123
Olof Johansson31c56d82007-02-04 16:36:55 -0600124 ip = ((u32 *)tbl->it_base) + index;
125
126 while (npages--) {
127 *(ip++) = iob_l2_emptyval;
128 /* invalidate tlb, can be optimized more */
129 out_le32(iob+IOB_AT_INVAL_TLB_REG, bus_addr >> 14);
130 bus_addr += IOBMAP_PAGE_SIZE;
131 }
132}
133
134
135static void iommu_table_iobmap_setup(void)
136{
137 pr_debug(" -> %s\n", __func__);
138 iommu_table_iobmap.it_busno = 0;
139 iommu_table_iobmap.it_offset = 0;
140 /* it_size is in number of entries */
Olof Johanssondfa70f82007-08-17 13:57:39 +1000141 iommu_table_iobmap.it_size = 0x80000000 >> IOBMAP_PAGE_SHIFT;
Olof Johansson31c56d82007-02-04 16:36:55 -0600142
143 /* Initialize the common IOMMU code */
144 iommu_table_iobmap.it_base = (unsigned long)iob_l2_base;
145 iommu_table_iobmap.it_index = 0;
146 /* XXXOJN tune this to avoid IOB cache invals.
147 * Should probably be 8 (64 bytes)
148 */
149 iommu_table_iobmap.it_blocksize = 4;
150 iommu_init_table(&iommu_table_iobmap, 0);
151 pr_debug(" <- %s\n", __func__);
152}
153
154
155
156static void pci_dma_bus_setup_pasemi(struct pci_bus *bus)
157{
Olof Johansson31c56d82007-02-04 16:36:55 -0600158 pr_debug("pci_dma_bus_setup, bus %p, bus->self %p\n", bus, bus->self);
159
160 if (!iommu_table_iobmap_inited) {
161 iommu_table_iobmap_inited = 1;
162 iommu_table_iobmap_setup();
163 }
Olof Johansson31c56d82007-02-04 16:36:55 -0600164}
165
166
167static void pci_dma_dev_setup_pasemi(struct pci_dev *dev)
168{
169 pr_debug("pci_dma_dev_setup, dev %p (%s)\n", dev, pci_name(dev));
170
Olof Johanssonaf289e82007-10-03 13:03:54 -0500171#if !defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
172 /* For non-LPAR environment, don't translate anything for the DMA
173 * engine. The exception to this is if the user has enabled
174 * CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE at build time.
Olof Johansson31c56d82007-02-04 16:36:55 -0600175 */
Olof Johanssonaf289e82007-10-03 13:03:54 -0500176 if (dev->vendor == 0x1959 && dev->device == 0xa007 &&
Olof Johansson24af8cb2008-01-31 00:41:30 -0600177 !firmware_has_feature(FW_FEATURE_LPAR)) {
Olof Johansson31c56d82007-02-04 16:36:55 -0600178 dev->dev.archdata.dma_ops = &dma_direct_ops;
Olof Johansson2da53b02008-01-31 17:50:02 -0600179 return;
Olof Johansson24af8cb2008-01-31 00:41:30 -0600180 }
Olof Johanssonaf289e82007-10-03 13:03:54 -0500181#endif
182
Becky Bruce738ef422009-09-21 08:26:35 +0000183 set_iommu_table_base(&dev->dev, &iommu_table_iobmap);
Olof Johansson31c56d82007-02-04 16:36:55 -0600184}
185
Stephen Rothwell750d1d12007-08-15 20:58:23 +1000186int __init iob_init(struct device_node *dn)
Olof Johansson31c56d82007-02-04 16:36:55 -0600187{
188 unsigned long tmp;
189 u32 regword;
190 int i;
191
192 pr_debug(" -> %s\n", __func__);
193
194 /* Allocate a spare page to map all invalid IOTLB pages. */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000195 tmp = memblock_alloc(IOBMAP_PAGE_SIZE, IOBMAP_PAGE_SIZE);
Olof Johansson31c56d82007-02-04 16:36:55 -0600196 if (!tmp)
197 panic("IOBMAP: Cannot allocate spare page!");
198 /* Empty l1 is marked invalid */
199 iob_l1_emptyval = 0;
200 /* Empty l2 is mapped to dummy page */
201 iob_l2_emptyval = IOBMAP_L2E_V | (tmp >> IOBMAP_PAGE_SHIFT);
202
203 iob = ioremap(IOB_BASE, IOB_SIZE);
204 if (!iob)
205 panic("IOBMAP: Cannot map registers!");
206
207 /* setup direct mapping of the L1 entries */
208 for (i = 0; i < 64; i++) {
209 /* Each L1 covers 32MB, i.e. 8K entries = 32K of ram */
210 regword = IOBMAP_L1E_V | (__pa(iob_l2_base + i*0x2000) >> 12);
Olof Johanssonf724bf72008-04-04 13:06:33 -0500211 out_le32(iob+IOB_XLT_L1_REGBASE+i*4, regword);
Olof Johansson31c56d82007-02-04 16:36:55 -0600212 }
213
214 /* set 2GB translation window, based at 0 */
215 regword = in_le32(iob+IOB_AD_REG);
216 regword &= ~IOB_AD_TRNG_MASK;
217 regword |= IOB_AD_TRNG_2G;
218 out_le32(iob+IOB_AD_REG, regword);
219
220 /* Enable translation */
221 regword = in_le32(iob+IOBCOM_REG);
222 regword |= IOBCOM_ATEN;
223 out_le32(iob+IOBCOM_REG, regword);
224
225 pr_debug(" <- %s\n", __func__);
226
227 return 0;
228}
229
230
231/* These are called very early. */
Stephen Rothwell750d1d12007-08-15 20:58:23 +1000232void __init iommu_init_early_pasemi(void)
Olof Johansson31c56d82007-02-04 16:36:55 -0600233{
234 int iommu_off;
235
236#ifndef CONFIG_PPC_PASEMI_IOMMU
237 iommu_off = 1;
238#else
239 iommu_off = of_chosen &&
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000240 of_get_property(of_chosen, "linux,iommu-off", NULL);
Olof Johansson31c56d82007-02-04 16:36:55 -0600241#endif
Nishanth Aravamudan2f9c9be2010-10-18 07:27:00 +0000242 if (iommu_off)
Olof Johansson31c56d82007-02-04 16:36:55 -0600243 return;
Olof Johansson31c56d82007-02-04 16:36:55 -0600244
245 iob_init(NULL);
246
247 ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pasemi;
248 ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pasemi;
249 ppc_md.tce_build = iobmap_build;
250 ppc_md.tce_free = iobmap_free;
Stephen Rothwell98747772007-03-04 16:58:39 +1100251 set_pci_dma_ops(&dma_iommu_ops);
Olof Johansson31c56d82007-02-04 16:36:55 -0600252}
253
254void __init alloc_iobmap_l2(void)
255{
256#ifndef CONFIG_PPC_PASEMI_IOMMU
257 return;
258#endif
259 /* For 2G space, 8x64 pages (2^21 bytes) is max total l2 size */
Michael Ellerman7db90c02012-07-25 21:19:51 +0000260 iob_l2_base = (u32 *)__va(memblock_alloc_base(1UL<<21, 1UL<<21, 0x80000000));
Olof Johansson31c56d82007-02-04 16:36:55 -0600261
262 printk(KERN_INFO "IOBMAP L2 allocated at: %p\n", iob_l2_base);
263}