blob: 8cfb5706790ecd395bd7039463a435a6a0574405 [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
Haren Myneni5f508672006-06-22 23:35:10 -070095static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
96{
97 u64 *tcep;
98
99 index <<= TCE_PAGE_FACTOR;
100 tcep = ((u64 *)tbl->it_base) + index;
101
102 return *tcep;
103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
106 long npages, unsigned long uaddr,
107 enum dma_data_direction direction)
108{
109 u64 rc;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500110 u64 proto_tce, tce;
111 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Michal Ostrowskicc8b5c92005-12-02 13:09:13 +1100113 tcenum <<= TCE_PAGE_FACTOR;
114 npages <<= TCE_PAGE_FACTOR;
115
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500116 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
117 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500119 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500122 tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
123 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (rc && printk_ratelimit()) {
126 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
127 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
128 printk("\ttcenum = 0x%lx\n", (u64)tcenum);
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500129 printk("\ttce val = 0x%lx\n", tce );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 show_stack(current, (unsigned long *)__get_SP());
131 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 tcenum++;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500134 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136}
137
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500138static DEFINE_PER_CPU(u64 *, tce_page) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140static void tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
141 long npages, unsigned long uaddr,
142 enum dma_data_direction direction)
143{
144 u64 rc;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500145 u64 proto_tce;
146 u64 *tcep;
147 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 long l, limit;
149
Paul Mackerras6fbb6182005-12-05 14:19:10 +1100150 if (TCE_PAGE_FACTOR == 0 && npages == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
152 direction);
153
154 tcep = __get_cpu_var(tce_page);
155
156 /* This is safe to do since interrupts are off when we're called
157 * from iommu_alloc{,_sg}()
158 */
159 if (!tcep) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500160 tcep = (u64 *)__get_free_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* If allocation fails, fall back to the loop implementation */
162 if (!tcep)
163 return tce_build_pSeriesLP(tbl, tcenum, npages,
164 uaddr, direction);
165 __get_cpu_var(tce_page) = tcep;
166 }
167
Michal Ostrowskicc8b5c92005-12-02 13:09:13 +1100168 tcenum <<= TCE_PAGE_FACTOR;
169 npages <<= TCE_PAGE_FACTOR;
170
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500171 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
172 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500174 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /* We can map max one pageful of TCEs at a time */
177 do {
178 /*
179 * Set up the page with TCE data, looping through and setting
180 * the values.
181 */
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500182 limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 for (l = 0; l < limit; l++) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500185 tcep[l] = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
186 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
189 rc = plpar_tce_put_indirect((u64)tbl->it_index,
190 (u64)tcenum << 12,
191 (u64)virt_to_abs(tcep),
192 limit);
193
194 npages -= limit;
195 tcenum += limit;
196 } while (npages > 0 && !rc);
197
198 if (rc && printk_ratelimit()) {
199 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
200 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
201 printk("\tnpages = 0x%lx\n", (u64)npages);
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500202 printk("\ttce[0] val = 0x%lx\n", tcep[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 show_stack(current, (unsigned long *)__get_SP());
204 }
205}
206
207static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
208{
209 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Olof Johanssond0035c622005-09-20 13:46:44 +1000211 tcenum <<= TCE_PAGE_FACTOR;
212 npages <<= TCE_PAGE_FACTOR;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500215 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 if (rc && printk_ratelimit()) {
218 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
219 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
220 printk("\ttcenum = 0x%lx\n", (u64)tcenum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 show_stack(current, (unsigned long *)__get_SP());
222 }
223
224 tcenum++;
225 }
226}
227
228
229static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
230{
231 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Olof Johanssond0035c622005-09-20 13:46:44 +1000233 tcenum <<= TCE_PAGE_FACTOR;
234 npages <<= TCE_PAGE_FACTOR;
235
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500236 rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (rc && printk_ratelimit()) {
239 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
240 printk("\trc = %ld\n", rc);
241 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
242 printk("\tnpages = 0x%lx\n", (u64)npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 show_stack(current, (unsigned long *)__get_SP());
244 }
245}
246
Haren Myneni5f508672006-06-22 23:35:10 -0700247static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
248{
249 u64 rc;
250 unsigned long tce_ret;
251
252 tcenum <<= TCE_PAGE_FACTOR;
253 rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
254
255 if (rc && printk_ratelimit()) {
256 printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%ld\n",
257 rc);
258 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
259 printk("\ttcenum = 0x%lx\n", (u64)tcenum);
260 show_stack(current, (unsigned long *)__get_SP());
261 }
262
263 return tce_ret;
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static void iommu_table_setparms(struct pci_controller *phb,
267 struct device_node *dn,
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500268 struct iommu_table *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct device_node *node;
271 unsigned long *basep;
272 unsigned int *sizep;
273
274 node = (struct device_node *)phb->arch_data;
275
276 basep = (unsigned long *)get_property(node, "linux,tce-base", NULL);
277 sizep = (unsigned int *)get_property(node, "linux,tce-size", NULL);
278 if (basep == NULL || sizep == NULL) {
279 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
280 "missing tce entries !\n", dn->full_name);
281 return;
282 }
283
284 tbl->it_base = (unsigned long)__va(*basep);
Haren Myneni5f508672006-06-22 23:35:10 -0700285
286#ifndef CONFIG_CRASH_DUMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 memset((void *)tbl->it_base, 0, *sizep);
Haren Myneni5f508672006-06-22 23:35:10 -0700288#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 tbl->it_busno = phb->bus->number;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* Units of tce entries */
293 tbl->it_offset = phb->dma_window_base_cur >> PAGE_SHIFT;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* Test if we are going over 2GB of DMA space */
Olof Johansson3c2822c2005-09-21 09:55:31 -0700296 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
297 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500298 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johansson3c2822c2005-09-21 09:55:31 -0700299 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 phb->dma_window_base_cur += phb->dma_window_size;
302
303 /* Set the tce table size - measured in entries */
304 tbl->it_size = phb->dma_window_size >> PAGE_SHIFT;
305
306 tbl->it_index = 0;
307 tbl->it_blocksize = 16;
308 tbl->it_type = TCE_PCI;
309}
310
311/*
312 * iommu_table_setparms_lpar
313 *
314 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
316static void iommu_table_setparms_lpar(struct pci_controller *phb,
317 struct device_node *dn,
318 struct iommu_table *tbl,
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000319 unsigned char *dma_window)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000321 unsigned long offset, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000323 tbl->it_busno = PCI_DN(dn)->bussubno;
324 of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 tbl->it_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 tbl->it_blocksize = 16;
328 tbl->it_type = TCE_PCI;
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000329 tbl->it_offset = offset >> PAGE_SHIFT;
330 tbl->it_size = size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333static void iommu_bus_setup_pSeries(struct pci_bus *bus)
334{
Olof Johansson3c2822c2005-09-21 09:55:31 -0700335 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct iommu_table *tbl;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700337 struct device_node *isa_dn, *isa_dn_orig;
338 struct device_node *tmp;
339 struct pci_dn *pci;
340 int children;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus, bus->self);
343
Olof Johansson3c2822c2005-09-21 09:55:31 -0700344 dn = pci_bus_to_OF_node(bus);
345 pci = PCI_DN(dn);
346
347 if (bus->self) {
348 /* This is not a root bus, any setup will be done for the
349 * device-side of the bridge in iommu_dev_setup_pSeries().
350 */
351 return;
352 }
353
354 /* Check if the ISA bus on the system is under
355 * this PHB.
356 */
357 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
358
359 while (isa_dn && isa_dn != dn)
360 isa_dn = isa_dn->parent;
361
362 if (isa_dn_orig)
363 of_node_put(isa_dn_orig);
364
Anton Blanchardd3c58fb2006-06-20 18:00:30 +1000365 /* Count number of direct PCI children of the PHB. */
Olof Johansson3c2822c2005-09-21 09:55:31 -0700366 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
Anton Blanchardd3c58fb2006-06-20 18:00:30 +1000367 children++;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700368
369 DBG("Children: %d\n", children);
370
371 /* Calculate amount of DMA window per slot. Each window must be
372 * a power of two (due to pci_alloc_consistent requirements).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *
Olof Johansson3c2822c2005-09-21 09:55:31 -0700374 * Keep 256MB aside for PHBs with ISA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 */
376
Olof Johansson3c2822c2005-09-21 09:55:31 -0700377 if (!isa_dn) {
378 /* No ISA/IDE - just set window size and return */
379 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Olof Johansson3c2822c2005-09-21 09:55:31 -0700381 while (pci->phb->dma_window_size * children > 0x80000000ul)
382 pci->phb->dma_window_size >>= 1;
Anton Blanchardf951da32005-09-22 21:44:05 -0700383 DBG("No ISA/IDE, window size is 0x%lx\n",
384 pci->phb->dma_window_size);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700385 pci->phb->dma_window_base_cur = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Olof Johansson3c2822c2005-09-21 09:55:31 -0700387 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Olof Johansson3c2822c2005-09-21 09:55:31 -0700389
390 /* If we have ISA, then we probably have an IDE
391 * controller too. Allocate a 128MB table but
392 * skip the first 128MB to avoid stepping on ISA
393 * space.
394 */
395 pci->phb->dma_window_size = 0x8000000ul;
396 pci->phb->dma_window_base_cur = 0x8000000ul;
397
Anton Blanchardca1588e2006-06-10 20:58:08 +1000398 tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
399 pci->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700400
401 iommu_table_setparms(pci->phb, dn, tbl);
Anton Blanchardca1588e2006-06-10 20:58:08 +1000402 pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700403
404 /* Divide the rest (1.75GB) among the children */
405 pci->phb->dma_window_size = 0x80000000ul;
406 while (pci->phb->dma_window_size * children > 0x70000000ul)
407 pci->phb->dma_window_size >>= 1;
408
Anton Blanchardf951da32005-09-22 21:44:05 -0700409 DBG("ISA/IDE, window size is 0x%lx\n", pci->phb->dma_window_size);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413
414static void iommu_bus_setup_pSeriesLP(struct pci_bus *bus)
415{
416 struct iommu_table *tbl;
417 struct device_node *dn, *pdn;
Paul Mackerras16353172005-09-06 13:17:54 +1000418 struct pci_dn *ppci;
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000419 unsigned char *dma_window = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus, bus->self);
422
423 dn = pci_bus_to_OF_node(bus);
424
425 /* Find nearest ibm,dma-window, walking up the device tree */
426 for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000427 dma_window = get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (dma_window != NULL)
429 break;
430 }
431
432 if (dma_window == NULL) {
433 DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn->full_name);
434 return;
435 }
436
linase07102d2005-12-05 19:37:35 -0600437 ppci = PCI_DN(pdn);
Paul Mackerras16353172005-09-06 13:17:54 +1000438 if (!ppci->iommu_table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /* Bussubno hasn't been copied yet.
440 * Do it now because iommu_table_setparms_lpar needs it.
441 */
Paul Mackerras16353172005-09-06 13:17:54 +1000442
443 ppci->bussubno = bus->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Anton Blanchardca1588e2006-06-10 20:58:08 +1000445 tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
446 ppci->phb->node);
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500447
Paul Mackerras16353172005-09-06 13:17:54 +1000448 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Anton Blanchardca1588e2006-06-10 20:58:08 +1000450 ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
453 if (pdn != dn)
Paul Mackerras16353172005-09-06 13:17:54 +1000454 PCI_DN(dn)->iommu_table = ppci->iommu_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457
458static void iommu_dev_setup_pSeries(struct pci_dev *dev)
459{
460 struct device_node *dn, *mydn;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700461 struct iommu_table *tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Anton Blanchardf951da32005-09-22 21:44:05 -0700463 DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev, pci_name(dev));
Olof Johansson3c2822c2005-09-21 09:55:31 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 mydn = dn = pci_device_to_OF_node(dev);
466
Olof Johansson3c2822c2005-09-21 09:55:31 -0700467 /* If we're the direct child of a root bus, then we need to allocate
468 * an iommu table ourselves. The bus setup code should have setup
469 * the window sizes already.
470 */
471 if (!dev->bus->self) {
472 DBG(" --> first child, no bridge. Allocating iommu table.\n");
Anton Blanchardca1588e2006-06-10 20:58:08 +1000473 tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
474 PCI_DN(dn)->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700475 iommu_table_setparms(PCI_DN(dn)->phb, dn, tbl);
Anton Blanchardca1588e2006-06-10 20:58:08 +1000476 PCI_DN(dn)->iommu_table = iommu_init_table(tbl,
477 PCI_DN(dn)->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700478
479 return;
480 }
481
482 /* If this device is further down the bus tree, search upwards until
483 * an already allocated iommu table is found and use that.
484 */
485
linase07102d2005-12-05 19:37:35 -0600486 while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 dn = dn->parent;
488
linase07102d2005-12-05 19:37:35 -0600489 if (dn && PCI_DN(dn)) {
Paul Mackerras16353172005-09-06 13:17:54 +1000490 PCI_DN(mydn)->iommu_table = PCI_DN(dn)->iommu_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 } else {
Anton Blanchardf951da32005-09-22 21:44:05 -0700492 DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev, pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494}
495
496static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
497{
498 int err = NOTIFY_OK;
499 struct device_node *np = node;
linase07102d2005-12-05 19:37:35 -0600500 struct pci_dn *pci = PCI_DN(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 switch (action) {
503 case PSERIES_RECONFIG_REMOVE:
John Rose8902e872005-11-02 10:29:55 -0600504 if (pci && pci->iommu_table &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 get_property(np, "ibm,dma-window", NULL))
506 iommu_free_table(np);
507 break;
508 default:
509 err = NOTIFY_DONE;
510 break;
511 }
512 return err;
513}
514
515static struct notifier_block iommu_reconfig_nb = {
516 .notifier_call = iommu_reconfig_notifier,
517};
518
519static void iommu_dev_setup_pSeriesLP(struct pci_dev *dev)
520{
521 struct device_node *pdn, *dn;
522 struct iommu_table *tbl;
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000523 unsigned char *dma_window = NULL;
Paul Mackerras16353172005-09-06 13:17:54 +1000524 struct pci_dn *pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Anton Blanchardf951da32005-09-22 21:44:05 -0700526 DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev, pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /* dev setup for LPAR is a little tricky, since the device tree might
529 * contain the dma-window properties per-device and not neccesarily
530 * for the bus. So we need to search upwards in the tree until we
531 * either hit a dma-window property, OR find a parent with a table
532 * already allocated.
533 */
534 dn = pci_device_to_OF_node(dev);
535
linase07102d2005-12-05 19:37:35 -0600536 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
Paul Mackerras16353172005-09-06 13:17:54 +1000537 pdn = pdn->parent) {
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000538 dma_window = get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (dma_window)
540 break;
541 }
542
543 /* Check for parent == NULL so we don't try to setup the empty EADS
544 * slots on POWER4 machines.
545 */
546 if (dma_window == NULL || pdn->parent == NULL) {
Anton Blanchard586a90e2005-09-22 21:44:04 -0700547 DBG("No dma window for device, linking to parent\n");
548 PCI_DN(dn)->iommu_table = PCI_DN(pdn)->iommu_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return;
550 } else {
551 DBG("Found DMA window, allocating table\n");
552 }
553
linase07102d2005-12-05 19:37:35 -0600554 pci = PCI_DN(pdn);
Paul Mackerras16353172005-09-06 13:17:54 +1000555 if (!pci->iommu_table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* iommu_table_setparms_lpar needs bussubno. */
Paul Mackerras16353172005-09-06 13:17:54 +1000557 pci->bussubno = pci->phb->bus->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Anton Blanchardca1588e2006-06-10 20:58:08 +1000559 tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
560 pci->phb->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Paul Mackerras16353172005-09-06 13:17:54 +1000562 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Anton Blanchardca1588e2006-06-10 20:58:08 +1000564 pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
567 if (pdn != dn)
Paul Mackerras16353172005-09-06 13:17:54 +1000568 PCI_DN(dn)->iommu_table = pci->iommu_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571static void iommu_bus_setup_null(struct pci_bus *b) { }
572static void iommu_dev_setup_null(struct pci_dev *d) { }
573
574/* These are called very early. */
575void iommu_init_early_pSeries(void)
576{
577 if (of_chosen && get_property(of_chosen, "linux,iommu-off", NULL)) {
578 /* Direct I/O, IOMMU off */
579 ppc_md.iommu_dev_setup = iommu_dev_setup_null;
580 ppc_md.iommu_bus_setup = iommu_bus_setup_null;
581 pci_direct_iommu_init();
582
583 return;
584 }
585
Michael Ellerman57cfb812006-03-21 20:45:59 +1100586 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Stephen Rothwell1ababe12005-08-03 14:35:25 +1000587 if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ppc_md.tce_build = tce_buildmulti_pSeriesLP;
589 ppc_md.tce_free = tce_freemulti_pSeriesLP;
590 } else {
591 ppc_md.tce_build = tce_build_pSeriesLP;
592 ppc_md.tce_free = tce_free_pSeriesLP;
593 }
Haren Myneni5f508672006-06-22 23:35:10 -0700594 ppc_md.tce_get = tce_get_pSeriesLP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeriesLP;
596 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeriesLP;
597 } else {
598 ppc_md.tce_build = tce_build_pSeries;
599 ppc_md.tce_free = tce_free_pSeries;
Haren Myneni5f508672006-06-22 23:35:10 -0700600 ppc_md.tce_get = tce_get_pseries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeries;
602 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeries;
603 }
604
605
606 pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
607
608 pci_iommu_init();
609}
610