blob: 2f66dc6503ffa822813d2ce4f2c36c4c4761a5a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
3 *
Olof Johanssonbc97ce92006-04-28 22:51:59 -05004 * Rewrite, cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Olof Johansson91f14482005-11-21 02:12:32 -06006 * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
Olof Johanssonbc97ce92006-04-28 22:51:59 -05007 * Copyright (C) 2006 Olof Johansson <olof@lixom.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR.
10 *
Olof Johanssonbc97ce92006-04-28 22:51:59 -050011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
Olof Johanssonbc97ce92006-04-28 22:51:59 -050016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
Olof Johanssonbc97ce92006-04-28 22:51:59 -050021 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#include <linux/config.h>
28#include <linux/init.h>
29#include <linux/types.h>
30#include <linux/slab.h>
31#include <linux/mm.h>
32#include <linux/spinlock.h>
33#include <linux/string.h>
34#include <linux/pci.h>
35#include <linux/dma-mapping.h>
36#include <asm/io.h>
37#include <asm/prom.h>
38#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/iommu.h>
40#include <asm/pci-bridge.h>
41#include <asm/machdep.h>
42#include <asm/abs_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/pSeries_reconfig.h>
Stephen Rothwell1ababe12005-08-03 14:35:25 +100044#include <asm/firmware.h>
Olof Johanssonc707ffc2005-09-20 13:45:41 +100045#include <asm/tce.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100046#include <asm/ppc-pci.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110047#include <asm/udbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Michael Ellermana1218722005-11-03 15:33:31 +110049#include "plpar_wrappers.h"
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define DBG(fmt...)
52
Olof Johanssonbc97ce92006-04-28 22:51:59 -050053static void tce_build_pSeries(struct iommu_table *tbl, long index,
54 long npages, unsigned long uaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 enum dma_data_direction direction)
56{
Olof Johanssonbc97ce92006-04-28 22:51:59 -050057 u64 proto_tce;
58 u64 *tcep;
59 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Olof Johanssond0035c622005-09-20 13:46:44 +100061 index <<= TCE_PAGE_FACTOR;
62 npages <<= TCE_PAGE_FACTOR;
63
Olof Johanssonbc97ce92006-04-28 22:51:59 -050064 proto_tce = TCE_PCI_READ; // Read allowed
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -050067 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Olof Johanssonbc97ce92006-04-28 22:51:59 -050069 tcep = ((u64 *)tbl->it_base) + index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 while (npages--) {
72 /* can't move this out since we might cross LMB boundary */
Olof Johanssonbc97ce92006-04-28 22:51:59 -050073 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
74 *tcep = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Olof Johanssond0035c622005-09-20 13:46:44 +100076 uaddr += TCE_PAGE_SIZE;
Olof Johanssonbc97ce92006-04-28 22:51:59 -050077 tcep++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79}
80
81
82static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
83{
Olof Johanssonbc97ce92006-04-28 22:51:59 -050084 u64 *tcep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Olof Johanssond0035c622005-09-20 13:46:44 +100086 npages <<= TCE_PAGE_FACTOR;
87 index <<= TCE_PAGE_FACTOR;
88
Olof Johanssonbc97ce92006-04-28 22:51:59 -050089 tcep = ((u64 *)tbl->it_base) + index;
90
91 while (npages--)
92 *(tcep++) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95
96static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
97 long npages, unsigned long uaddr,
98 enum dma_data_direction direction)
99{
100 u64 rc;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500101 u64 proto_tce, tce;
102 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Michal Ostrowskicc8b5c92005-12-02 13:09:13 +1100104 tcenum <<= TCE_PAGE_FACTOR;
105 npages <<= TCE_PAGE_FACTOR;
106
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500107 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
108 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500110 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500113 tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
114 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (rc && printk_ratelimit()) {
117 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
118 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
119 printk("\ttcenum = 0x%lx\n", (u64)tcenum);
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500120 printk("\ttce val = 0x%lx\n", tce );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 show_stack(current, (unsigned long *)__get_SP());
122 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 tcenum++;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500125 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127}
128
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500129static DEFINE_PER_CPU(u64 *, tce_page) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131static void tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
132 long npages, unsigned long uaddr,
133 enum dma_data_direction direction)
134{
135 u64 rc;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500136 u64 proto_tce;
137 u64 *tcep;
138 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 long l, limit;
140
Paul Mackerras6fbb6182005-12-05 14:19:10 +1100141 if (TCE_PAGE_FACTOR == 0 && npages == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
143 direction);
144
145 tcep = __get_cpu_var(tce_page);
146
147 /* This is safe to do since interrupts are off when we're called
148 * from iommu_alloc{,_sg}()
149 */
150 if (!tcep) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500151 tcep = (u64 *)__get_free_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* If allocation fails, fall back to the loop implementation */
153 if (!tcep)
154 return tce_build_pSeriesLP(tbl, tcenum, npages,
155 uaddr, direction);
156 __get_cpu_var(tce_page) = tcep;
157 }
158
Michal Ostrowskicc8b5c92005-12-02 13:09:13 +1100159 tcenum <<= TCE_PAGE_FACTOR;
160 npages <<= TCE_PAGE_FACTOR;
161
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500162 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
163 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500165 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /* We can map max one pageful of TCEs at a time */
168 do {
169 /*
170 * Set up the page with TCE data, looping through and setting
171 * the values.
172 */
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500173 limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 for (l = 0; l < limit; l++) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500176 tcep[l] = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
177 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
180 rc = plpar_tce_put_indirect((u64)tbl->it_index,
181 (u64)tcenum << 12,
182 (u64)virt_to_abs(tcep),
183 limit);
184
185 npages -= limit;
186 tcenum += limit;
187 } while (npages > 0 && !rc);
188
189 if (rc && printk_ratelimit()) {
190 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
191 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
192 printk("\tnpages = 0x%lx\n", (u64)npages);
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500193 printk("\ttce[0] val = 0x%lx\n", tcep[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 show_stack(current, (unsigned long *)__get_SP());
195 }
196}
197
198static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
199{
200 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Olof Johanssond0035c622005-09-20 13:46:44 +1000202 tcenum <<= TCE_PAGE_FACTOR;
203 npages <<= TCE_PAGE_FACTOR;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500206 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 if (rc && printk_ratelimit()) {
209 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
210 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
211 printk("\ttcenum = 0x%lx\n", (u64)tcenum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 show_stack(current, (unsigned long *)__get_SP());
213 }
214
215 tcenum++;
216 }
217}
218
219
220static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
221{
222 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Olof Johanssond0035c622005-09-20 13:46:44 +1000224 tcenum <<= TCE_PAGE_FACTOR;
225 npages <<= TCE_PAGE_FACTOR;
226
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500227 rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (rc && printk_ratelimit()) {
230 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
231 printk("\trc = %ld\n", rc);
232 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
233 printk("\tnpages = 0x%lx\n", (u64)npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 show_stack(current, (unsigned long *)__get_SP());
235 }
236}
237
238static void iommu_table_setparms(struct pci_controller *phb,
239 struct device_node *dn,
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500240 struct iommu_table *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct device_node *node;
243 unsigned long *basep;
244 unsigned int *sizep;
245
246 node = (struct device_node *)phb->arch_data;
247
248 basep = (unsigned long *)get_property(node, "linux,tce-base", NULL);
249 sizep = (unsigned int *)get_property(node, "linux,tce-size", NULL);
250 if (basep == NULL || sizep == NULL) {
251 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
252 "missing tce entries !\n", dn->full_name);
253 return;
254 }
255
256 tbl->it_base = (unsigned long)__va(*basep);
257 memset((void *)tbl->it_base, 0, *sizep);
258
259 tbl->it_busno = phb->bus->number;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* Units of tce entries */
262 tbl->it_offset = phb->dma_window_base_cur >> PAGE_SHIFT;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* Test if we are going over 2GB of DMA space */
Olof Johansson3c2822c2005-09-21 09:55:31 -0700265 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
266 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500267 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johansson3c2822c2005-09-21 09:55:31 -0700268 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 phb->dma_window_base_cur += phb->dma_window_size;
271
272 /* Set the tce table size - measured in entries */
273 tbl->it_size = phb->dma_window_size >> PAGE_SHIFT;
274
275 tbl->it_index = 0;
276 tbl->it_blocksize = 16;
277 tbl->it_type = TCE_PCI;
278}
279
280/*
281 * iommu_table_setparms_lpar
282 *
283 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285static void iommu_table_setparms_lpar(struct pci_controller *phb,
286 struct device_node *dn,
287 struct iommu_table *tbl,
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000288 unsigned char *dma_window)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000290 unsigned long offset, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000292 tbl->it_busno = PCI_DN(dn)->bussubno;
293 of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 tbl->it_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 tbl->it_blocksize = 16;
297 tbl->it_type = TCE_PCI;
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000298 tbl->it_offset = offset >> PAGE_SHIFT;
299 tbl->it_size = size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302static void iommu_bus_setup_pSeries(struct pci_bus *bus)
303{
Olof Johansson3c2822c2005-09-21 09:55:31 -0700304 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct iommu_table *tbl;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700306 struct device_node *isa_dn, *isa_dn_orig;
307 struct device_node *tmp;
308 struct pci_dn *pci;
309 int children;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus, bus->self);
312
Olof Johansson3c2822c2005-09-21 09:55:31 -0700313 dn = pci_bus_to_OF_node(bus);
314 pci = PCI_DN(dn);
315
316 if (bus->self) {
317 /* This is not a root bus, any setup will be done for the
318 * device-side of the bridge in iommu_dev_setup_pSeries().
319 */
320 return;
321 }
322
323 /* Check if the ISA bus on the system is under
324 * this PHB.
325 */
326 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
327
328 while (isa_dn && isa_dn != dn)
329 isa_dn = isa_dn->parent;
330
331 if (isa_dn_orig)
332 of_node_put(isa_dn_orig);
333
334 /* Count number of direct PCI children of the PHB.
335 * All PCI device nodes have class-code property, so it's
336 * an easy way to find them.
337 */
338 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
339 if (get_property(tmp, "class-code", NULL))
340 children++;
341
342 DBG("Children: %d\n", children);
343
344 /* Calculate amount of DMA window per slot. Each window must be
345 * a power of two (due to pci_alloc_consistent requirements).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 *
Olof Johansson3c2822c2005-09-21 09:55:31 -0700347 * Keep 256MB aside for PHBs with ISA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
349
Olof Johansson3c2822c2005-09-21 09:55:31 -0700350 if (!isa_dn) {
351 /* No ISA/IDE - just set window size and return */
352 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Olof Johansson3c2822c2005-09-21 09:55:31 -0700354 while (pci->phb->dma_window_size * children > 0x80000000ul)
355 pci->phb->dma_window_size >>= 1;
Anton Blanchardf951da32005-09-22 21:44:05 -0700356 DBG("No ISA/IDE, window size is 0x%lx\n",
357 pci->phb->dma_window_size);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700358 pci->phb->dma_window_base_cur = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Olof Johansson3c2822c2005-09-21 09:55:31 -0700360 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Olof Johansson3c2822c2005-09-21 09:55:31 -0700362
363 /* If we have ISA, then we probably have an IDE
364 * controller too. Allocate a 128MB table but
365 * skip the first 128MB to avoid stepping on ISA
366 * space.
367 */
368 pci->phb->dma_window_size = 0x8000000ul;
369 pci->phb->dma_window_base_cur = 0x8000000ul;
370
Anton Blanchardca1588e2006-06-10 20:58:08 +1000371 tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
372 pci->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700373
374 iommu_table_setparms(pci->phb, dn, tbl);
Anton Blanchardca1588e2006-06-10 20:58:08 +1000375 pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700376
377 /* Divide the rest (1.75GB) among the children */
378 pci->phb->dma_window_size = 0x80000000ul;
379 while (pci->phb->dma_window_size * children > 0x70000000ul)
380 pci->phb->dma_window_size >>= 1;
381
Anton Blanchardf951da32005-09-22 21:44:05 -0700382 DBG("ISA/IDE, window size is 0x%lx\n", pci->phb->dma_window_size);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386
387static void iommu_bus_setup_pSeriesLP(struct pci_bus *bus)
388{
389 struct iommu_table *tbl;
390 struct device_node *dn, *pdn;
Paul Mackerras16353172005-09-06 13:17:54 +1000391 struct pci_dn *ppci;
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000392 unsigned char *dma_window = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus, bus->self);
395
396 dn = pci_bus_to_OF_node(bus);
397
398 /* Find nearest ibm,dma-window, walking up the device tree */
399 for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000400 dma_window = get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (dma_window != NULL)
402 break;
403 }
404
405 if (dma_window == NULL) {
406 DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn->full_name);
407 return;
408 }
409
linase07102d2005-12-05 19:37:35 -0600410 ppci = PCI_DN(pdn);
Paul Mackerras16353172005-09-06 13:17:54 +1000411 if (!ppci->iommu_table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* Bussubno hasn't been copied yet.
413 * Do it now because iommu_table_setparms_lpar needs it.
414 */
Paul Mackerras16353172005-09-06 13:17:54 +1000415
416 ppci->bussubno = bus->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Anton Blanchardca1588e2006-06-10 20:58:08 +1000418 tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
419 ppci->phb->node);
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500420
Paul Mackerras16353172005-09-06 13:17:54 +1000421 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Anton Blanchardca1588e2006-06-10 20:58:08 +1000423 ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 if (pdn != dn)
Paul Mackerras16353172005-09-06 13:17:54 +1000427 PCI_DN(dn)->iommu_table = ppci->iommu_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430
431static void iommu_dev_setup_pSeries(struct pci_dev *dev)
432{
433 struct device_node *dn, *mydn;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700434 struct iommu_table *tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Anton Blanchardf951da32005-09-22 21:44:05 -0700436 DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev, pci_name(dev));
Olof Johansson3c2822c2005-09-21 09:55:31 -0700437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 mydn = dn = pci_device_to_OF_node(dev);
439
Olof Johansson3c2822c2005-09-21 09:55:31 -0700440 /* If we're the direct child of a root bus, then we need to allocate
441 * an iommu table ourselves. The bus setup code should have setup
442 * the window sizes already.
443 */
444 if (!dev->bus->self) {
445 DBG(" --> first child, no bridge. Allocating iommu table.\n");
Anton Blanchardca1588e2006-06-10 20:58:08 +1000446 tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
447 PCI_DN(dn)->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700448 iommu_table_setparms(PCI_DN(dn)->phb, dn, tbl);
Anton Blanchardca1588e2006-06-10 20:58:08 +1000449 PCI_DN(dn)->iommu_table = iommu_init_table(tbl,
450 PCI_DN(dn)->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700451
452 return;
453 }
454
455 /* If this device is further down the bus tree, search upwards until
456 * an already allocated iommu table is found and use that.
457 */
458
linase07102d2005-12-05 19:37:35 -0600459 while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 dn = dn->parent;
461
linase07102d2005-12-05 19:37:35 -0600462 if (dn && PCI_DN(dn)) {
Paul Mackerras16353172005-09-06 13:17:54 +1000463 PCI_DN(mydn)->iommu_table = PCI_DN(dn)->iommu_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 } else {
Anton Blanchardf951da32005-09-22 21:44:05 -0700465 DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev, pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467}
468
469static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
470{
471 int err = NOTIFY_OK;
472 struct device_node *np = node;
linase07102d2005-12-05 19:37:35 -0600473 struct pci_dn *pci = PCI_DN(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 switch (action) {
476 case PSERIES_RECONFIG_REMOVE:
John Rose8902e872005-11-02 10:29:55 -0600477 if (pci && pci->iommu_table &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 get_property(np, "ibm,dma-window", NULL))
479 iommu_free_table(np);
480 break;
481 default:
482 err = NOTIFY_DONE;
483 break;
484 }
485 return err;
486}
487
488static struct notifier_block iommu_reconfig_nb = {
489 .notifier_call = iommu_reconfig_notifier,
490};
491
492static void iommu_dev_setup_pSeriesLP(struct pci_dev *dev)
493{
494 struct device_node *pdn, *dn;
495 struct iommu_table *tbl;
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000496 unsigned char *dma_window = NULL;
Paul Mackerras16353172005-09-06 13:17:54 +1000497 struct pci_dn *pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Anton Blanchardf951da32005-09-22 21:44:05 -0700499 DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev, pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /* dev setup for LPAR is a little tricky, since the device tree might
502 * contain the dma-window properties per-device and not neccesarily
503 * for the bus. So we need to search upwards in the tree until we
504 * either hit a dma-window property, OR find a parent with a table
505 * already allocated.
506 */
507 dn = pci_device_to_OF_node(dev);
508
linase07102d2005-12-05 19:37:35 -0600509 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
Paul Mackerras16353172005-09-06 13:17:54 +1000510 pdn = pdn->parent) {
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000511 dma_window = get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (dma_window)
513 break;
514 }
515
516 /* Check for parent == NULL so we don't try to setup the empty EADS
517 * slots on POWER4 machines.
518 */
519 if (dma_window == NULL || pdn->parent == NULL) {
Anton Blanchard586a90e2005-09-22 21:44:04 -0700520 DBG("No dma window for device, linking to parent\n");
521 PCI_DN(dn)->iommu_table = PCI_DN(pdn)->iommu_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return;
523 } else {
524 DBG("Found DMA window, allocating table\n");
525 }
526
linase07102d2005-12-05 19:37:35 -0600527 pci = PCI_DN(pdn);
Paul Mackerras16353172005-09-06 13:17:54 +1000528 if (!pci->iommu_table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* iommu_table_setparms_lpar needs bussubno. */
Paul Mackerras16353172005-09-06 13:17:54 +1000530 pci->bussubno = pci->phb->bus->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Anton Blanchardca1588e2006-06-10 20:58:08 +1000532 tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
533 pci->phb->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Paul Mackerras16353172005-09-06 13:17:54 +1000535 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Anton Blanchardca1588e2006-06-10 20:58:08 +1000537 pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 if (pdn != dn)
Paul Mackerras16353172005-09-06 13:17:54 +1000541 PCI_DN(dn)->iommu_table = pci->iommu_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544static void iommu_bus_setup_null(struct pci_bus *b) { }
545static void iommu_dev_setup_null(struct pci_dev *d) { }
546
547/* These are called very early. */
548void iommu_init_early_pSeries(void)
549{
550 if (of_chosen && get_property(of_chosen, "linux,iommu-off", NULL)) {
551 /* Direct I/O, IOMMU off */
552 ppc_md.iommu_dev_setup = iommu_dev_setup_null;
553 ppc_md.iommu_bus_setup = iommu_bus_setup_null;
554 pci_direct_iommu_init();
555
556 return;
557 }
558
Michael Ellerman57cfb812006-03-21 20:45:59 +1100559 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Stephen Rothwell1ababe12005-08-03 14:35:25 +1000560 if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 ppc_md.tce_build = tce_buildmulti_pSeriesLP;
562 ppc_md.tce_free = tce_freemulti_pSeriesLP;
563 } else {
564 ppc_md.tce_build = tce_build_pSeriesLP;
565 ppc_md.tce_free = tce_free_pSeriesLP;
566 }
567 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeriesLP;
568 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeriesLP;
569 } else {
570 ppc_md.tce_build = tce_build_pSeries;
571 ppc_md.tce_free = tce_free_pSeries;
572 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeries;
573 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeries;
574 }
575
576
577 pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
578
579 pci_iommu_init();
580}
581