Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation |
3 | * | |
bc97ce95 | 4 | * Rewrite, cleanup: |
1da177e4 | 5 | * |
91f14480 | 6 | * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation |
bc97ce95 | 7 | * Copyright (C) 2006 Olof Johansson <olof@lixom.net> |
1da177e4 LT |
8 | * |
9 | * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR. | |
10 | * | |
bc97ce95 | 11 | * |
1da177e4 LT |
12 | * 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. | |
bc97ce95 | 16 | * |
1da177e4 LT |
17 | * 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. | |
bc97ce95 | 21 | * |
1da177e4 LT |
22 | * 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 | ||
1da177e4 LT |
27 | #include <linux/init.h> |
28 | #include <linux/types.h> | |
29 | #include <linux/slab.h> | |
30 | #include <linux/mm.h> | |
31 | #include <linux/spinlock.h> | |
32 | #include <linux/string.h> | |
33 | #include <linux/pci.h> | |
34 | #include <linux/dma-mapping.h> | |
62a8bd6c | 35 | #include <linux/crash_dump.h> |
4e8b0cf4 | 36 | #include <linux/memory.h> |
1da177e4 LT |
37 | #include <asm/io.h> |
38 | #include <asm/prom.h> | |
39 | #include <asm/rtas.h> | |
1da177e4 LT |
40 | #include <asm/iommu.h> |
41 | #include <asm/pci-bridge.h> | |
42 | #include <asm/machdep.h> | |
43 | #include <asm/abs_addr.h> | |
1da177e4 | 44 | #include <asm/pSeries_reconfig.h> |
1ababe11 | 45 | #include <asm/firmware.h> |
c707ffcf | 46 | #include <asm/tce.h> |
d387899f | 47 | #include <asm/ppc-pci.h> |
2249ca9d | 48 | #include <asm/udbg.h> |
4e8b0cf4 | 49 | #include <asm/mmzone.h> |
1da177e4 | 50 | |
a1218720 ME |
51 | #include "plpar_wrappers.h" |
52 | ||
1da177e4 | 53 | |
6490c490 | 54 | static int tce_build_pSeries(struct iommu_table *tbl, long index, |
bc97ce95 | 55 | long npages, unsigned long uaddr, |
4f3dd8a0 MN |
56 | enum dma_data_direction direction, |
57 | struct dma_attrs *attrs) | |
1da177e4 | 58 | { |
bc97ce95 OJ |
59 | u64 proto_tce; |
60 | u64 *tcep; | |
61 | u64 rpn; | |
1da177e4 | 62 | |
bc97ce95 | 63 | proto_tce = TCE_PCI_READ; // Read allowed |
1da177e4 LT |
64 | |
65 | if (direction != DMA_TO_DEVICE) | |
bc97ce95 | 66 | proto_tce |= TCE_PCI_WRITE; |
1da177e4 | 67 | |
bc97ce95 | 68 | tcep = ((u64 *)tbl->it_base) + index; |
1da177e4 LT |
69 | |
70 | while (npages--) { | |
95f72d1e | 71 | /* can't move this out since we might cross MEMBLOCK boundary */ |
bc97ce95 OJ |
72 | rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT; |
73 | *tcep = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT; | |
1da177e4 | 74 | |
d0035c62 | 75 | uaddr += TCE_PAGE_SIZE; |
bc97ce95 | 76 | tcep++; |
1da177e4 | 77 | } |
6490c490 | 78 | return 0; |
1da177e4 LT |
79 | } |
80 | ||
81 | ||
82 | static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages) | |
83 | { | |
bc97ce95 | 84 | u64 *tcep; |
1da177e4 | 85 | |
bc97ce95 OJ |
86 | tcep = ((u64 *)tbl->it_base) + index; |
87 | ||
88 | while (npages--) | |
89 | *(tcep++) = 0; | |
1da177e4 LT |
90 | } |
91 | ||
5f50867b HM |
92 | static unsigned long tce_get_pseries(struct iommu_table *tbl, long index) |
93 | { | |
94 | u64 *tcep; | |
95 | ||
5f50867b HM |
96 | tcep = ((u64 *)tbl->it_base) + index; |
97 | ||
98 | return *tcep; | |
99 | } | |
1da177e4 | 100 | |
6490c490 RJ |
101 | static void tce_free_pSeriesLP(struct iommu_table*, long, long); |
102 | static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long); | |
103 | ||
104 | static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum, | |
1da177e4 | 105 | long npages, unsigned long uaddr, |
4f3dd8a0 MN |
106 | enum dma_data_direction direction, |
107 | struct dma_attrs *attrs) | |
1da177e4 | 108 | { |
6490c490 | 109 | u64 rc = 0; |
bc97ce95 OJ |
110 | u64 proto_tce, tce; |
111 | u64 rpn; | |
6490c490 RJ |
112 | int ret = 0; |
113 | long tcenum_start = tcenum, npages_start = npages; | |
1da177e4 | 114 | |
bc97ce95 OJ |
115 | rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT; |
116 | proto_tce = TCE_PCI_READ; | |
1da177e4 | 117 | if (direction != DMA_TO_DEVICE) |
bc97ce95 | 118 | proto_tce |= TCE_PCI_WRITE; |
1da177e4 LT |
119 | |
120 | while (npages--) { | |
bc97ce95 OJ |
121 | tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT; |
122 | rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce); | |
123 | ||
6490c490 RJ |
124 | if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) { |
125 | ret = (int)rc; | |
126 | tce_free_pSeriesLP(tbl, tcenum_start, | |
127 | (npages_start - (npages + 1))); | |
128 | break; | |
129 | } | |
130 | ||
1da177e4 | 131 | if (rc && printk_ratelimit()) { |
fe333321 IM |
132 | printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc); |
133 | printk("\tindex = 0x%llx\n", (u64)tbl->it_index); | |
134 | printk("\ttcenum = 0x%llx\n", (u64)tcenum); | |
135 | printk("\ttce val = 0x%llx\n", tce ); | |
1da177e4 LT |
136 | show_stack(current, (unsigned long *)__get_SP()); |
137 | } | |
bc97ce95 | 138 | |
1da177e4 | 139 | tcenum++; |
bc97ce95 | 140 | rpn++; |
1da177e4 | 141 | } |
6490c490 | 142 | return ret; |
1da177e4 LT |
143 | } |
144 | ||
a8daac8a | 145 | static DEFINE_PER_CPU(u64 *, tce_page); |
1da177e4 | 146 | |
6490c490 | 147 | static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum, |
1da177e4 | 148 | long npages, unsigned long uaddr, |
4f3dd8a0 MN |
149 | enum dma_data_direction direction, |
150 | struct dma_attrs *attrs) | |
1da177e4 | 151 | { |
6490c490 | 152 | u64 rc = 0; |
bc97ce95 OJ |
153 | u64 proto_tce; |
154 | u64 *tcep; | |
155 | u64 rpn; | |
1da177e4 | 156 | long l, limit; |
6490c490 RJ |
157 | long tcenum_start = tcenum, npages_start = npages; |
158 | int ret = 0; | |
1da177e4 | 159 | |
541b2755 | 160 | if (npages == 1) { |
6490c490 RJ |
161 | return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr, |
162 | direction, attrs); | |
541b2755 | 163 | } |
1da177e4 LT |
164 | |
165 | tcep = __get_cpu_var(tce_page); | |
166 | ||
167 | /* This is safe to do since interrupts are off when we're called | |
168 | * from iommu_alloc{,_sg}() | |
169 | */ | |
170 | if (!tcep) { | |
bc97ce95 | 171 | tcep = (u64 *)__get_free_page(GFP_ATOMIC); |
1da177e4 | 172 | /* If allocation fails, fall back to the loop implementation */ |
541b2755 | 173 | if (!tcep) { |
6490c490 | 174 | return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr, |
4f3dd8a0 | 175 | direction, attrs); |
541b2755 | 176 | } |
1da177e4 LT |
177 | __get_cpu_var(tce_page) = tcep; |
178 | } | |
179 | ||
bc97ce95 OJ |
180 | rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT; |
181 | proto_tce = TCE_PCI_READ; | |
1da177e4 | 182 | if (direction != DMA_TO_DEVICE) |
bc97ce95 | 183 | proto_tce |= TCE_PCI_WRITE; |
1da177e4 LT |
184 | |
185 | /* We can map max one pageful of TCEs at a time */ | |
186 | do { | |
187 | /* | |
188 | * Set up the page with TCE data, looping through and setting | |
189 | * the values. | |
190 | */ | |
bc97ce95 | 191 | limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE); |
1da177e4 LT |
192 | |
193 | for (l = 0; l < limit; l++) { | |
bc97ce95 OJ |
194 | tcep[l] = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT; |
195 | rpn++; | |
1da177e4 LT |
196 | } |
197 | ||
198 | rc = plpar_tce_put_indirect((u64)tbl->it_index, | |
199 | (u64)tcenum << 12, | |
200 | (u64)virt_to_abs(tcep), | |
201 | limit); | |
202 | ||
203 | npages -= limit; | |
204 | tcenum += limit; | |
205 | } while (npages > 0 && !rc); | |
206 | ||
6490c490 RJ |
207 | if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) { |
208 | ret = (int)rc; | |
209 | tce_freemulti_pSeriesLP(tbl, tcenum_start, | |
210 | (npages_start - (npages + limit))); | |
211 | return ret; | |
212 | } | |
213 | ||
1da177e4 | 214 | if (rc && printk_ratelimit()) { |
fe333321 IM |
215 | printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc); |
216 | printk("\tindex = 0x%llx\n", (u64)tbl->it_index); | |
217 | printk("\tnpages = 0x%llx\n", (u64)npages); | |
218 | printk("\ttce[0] val = 0x%llx\n", tcep[0]); | |
1da177e4 LT |
219 | show_stack(current, (unsigned long *)__get_SP()); |
220 | } | |
6490c490 | 221 | return ret; |
1da177e4 LT |
222 | } |
223 | ||
224 | static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages) | |
225 | { | |
226 | u64 rc; | |
1da177e4 | 227 | |
1da177e4 | 228 | while (npages--) { |
bc97ce95 | 229 | rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0); |
1da177e4 LT |
230 | |
231 | if (rc && printk_ratelimit()) { | |
fe333321 IM |
232 | printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc); |
233 | printk("\tindex = 0x%llx\n", (u64)tbl->it_index); | |
234 | printk("\ttcenum = 0x%llx\n", (u64)tcenum); | |
1da177e4 LT |
235 | show_stack(current, (unsigned long *)__get_SP()); |
236 | } | |
237 | ||
238 | tcenum++; | |
239 | } | |
240 | } | |
241 | ||
242 | ||
243 | static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages) | |
244 | { | |
245 | u64 rc; | |
1da177e4 | 246 | |
bc97ce95 | 247 | rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages); |
1da177e4 LT |
248 | |
249 | if (rc && printk_ratelimit()) { | |
250 | printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n"); | |
fe333321 IM |
251 | printk("\trc = %lld\n", rc); |
252 | printk("\tindex = 0x%llx\n", (u64)tbl->it_index); | |
253 | printk("\tnpages = 0x%llx\n", (u64)npages); | |
1da177e4 LT |
254 | show_stack(current, (unsigned long *)__get_SP()); |
255 | } | |
256 | } | |
257 | ||
5f50867b HM |
258 | static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum) |
259 | { | |
260 | u64 rc; | |
261 | unsigned long tce_ret; | |
262 | ||
5f50867b HM |
263 | rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret); |
264 | ||
265 | if (rc && printk_ratelimit()) { | |
fe333321 IM |
266 | printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc); |
267 | printk("\tindex = 0x%llx\n", (u64)tbl->it_index); | |
268 | printk("\ttcenum = 0x%llx\n", (u64)tcenum); | |
5f50867b HM |
269 | show_stack(current, (unsigned long *)__get_SP()); |
270 | } | |
271 | ||
272 | return tce_ret; | |
273 | } | |
274 | ||
25985edc | 275 | /* this is compatible with cells for the device tree property */ |
4e8b0cf4 NA |
276 | struct dynamic_dma_window_prop { |
277 | __be32 liobn; /* tce table number */ | |
278 | __be64 dma_base; /* address hi,lo */ | |
279 | __be32 tce_shift; /* ilog2(tce_page_size) */ | |
280 | __be32 window_shift; /* ilog2(tce_window_size) */ | |
281 | }; | |
282 | ||
283 | struct direct_window { | |
284 | struct device_node *device; | |
285 | const struct dynamic_dma_window_prop *prop; | |
286 | struct list_head list; | |
287 | }; | |
288 | ||
289 | /* Dynamic DMA Window support */ | |
290 | struct ddw_query_response { | |
291 | u32 windows_available; | |
292 | u32 largest_available_block; | |
293 | u32 page_size; | |
294 | u32 migration_capable; | |
295 | }; | |
296 | ||
297 | struct ddw_create_response { | |
298 | u32 liobn; | |
299 | u32 addr_hi; | |
300 | u32 addr_lo; | |
301 | }; | |
302 | ||
303 | static LIST_HEAD(direct_window_list); | |
304 | /* prevents races between memory on/offline and window creation */ | |
305 | static DEFINE_SPINLOCK(direct_window_list_lock); | |
306 | /* protects initializing window twice for same device */ | |
307 | static DEFINE_MUTEX(direct_window_init_mutex); | |
308 | #define DIRECT64_PROPNAME "linux,direct64-ddr-window-info" | |
309 | ||
310 | static int tce_clearrange_multi_pSeriesLP(unsigned long start_pfn, | |
311 | unsigned long num_pfn, const void *arg) | |
312 | { | |
313 | const struct dynamic_dma_window_prop *maprange = arg; | |
314 | int rc; | |
315 | u64 tce_size, num_tce, dma_offset, next; | |
316 | u32 tce_shift; | |
317 | long limit; | |
318 | ||
319 | tce_shift = be32_to_cpu(maprange->tce_shift); | |
320 | tce_size = 1ULL << tce_shift; | |
321 | next = start_pfn << PAGE_SHIFT; | |
322 | num_tce = num_pfn << PAGE_SHIFT; | |
323 | ||
324 | /* round back to the beginning of the tce page size */ | |
325 | num_tce += next & (tce_size - 1); | |
326 | next &= ~(tce_size - 1); | |
327 | ||
328 | /* covert to number of tces */ | |
329 | num_tce |= tce_size - 1; | |
330 | num_tce >>= tce_shift; | |
331 | ||
332 | do { | |
333 | /* | |
334 | * Set up the page with TCE data, looping through and setting | |
335 | * the values. | |
336 | */ | |
337 | limit = min_t(long, num_tce, 512); | |
338 | dma_offset = next + be64_to_cpu(maprange->dma_base); | |
339 | ||
340 | rc = plpar_tce_stuff((u64)be32_to_cpu(maprange->liobn), | |
341 | dma_offset, | |
342 | 0, limit); | |
343 | num_tce -= limit; | |
344 | } while (num_tce > 0 && !rc); | |
345 | ||
346 | return rc; | |
347 | } | |
348 | ||
349 | static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn, | |
350 | unsigned long num_pfn, const void *arg) | |
351 | { | |
352 | const struct dynamic_dma_window_prop *maprange = arg; | |
353 | u64 *tcep, tce_size, num_tce, dma_offset, next, proto_tce, liobn; | |
354 | u32 tce_shift; | |
355 | u64 rc = 0; | |
356 | long l, limit; | |
357 | ||
358 | local_irq_disable(); /* to protect tcep and the page behind it */ | |
359 | tcep = __get_cpu_var(tce_page); | |
360 | ||
361 | if (!tcep) { | |
362 | tcep = (u64 *)__get_free_page(GFP_ATOMIC); | |
363 | if (!tcep) { | |
364 | local_irq_enable(); | |
365 | return -ENOMEM; | |
366 | } | |
367 | __get_cpu_var(tce_page) = tcep; | |
368 | } | |
369 | ||
370 | proto_tce = TCE_PCI_READ | TCE_PCI_WRITE; | |
371 | ||
372 | liobn = (u64)be32_to_cpu(maprange->liobn); | |
373 | tce_shift = be32_to_cpu(maprange->tce_shift); | |
374 | tce_size = 1ULL << tce_shift; | |
375 | next = start_pfn << PAGE_SHIFT; | |
376 | num_tce = num_pfn << PAGE_SHIFT; | |
377 | ||
378 | /* round back to the beginning of the tce page size */ | |
379 | num_tce += next & (tce_size - 1); | |
380 | next &= ~(tce_size - 1); | |
381 | ||
382 | /* covert to number of tces */ | |
383 | num_tce |= tce_size - 1; | |
384 | num_tce >>= tce_shift; | |
385 | ||
386 | /* We can map max one pageful of TCEs at a time */ | |
387 | do { | |
388 | /* | |
389 | * Set up the page with TCE data, looping through and setting | |
390 | * the values. | |
391 | */ | |
392 | limit = min_t(long, num_tce, 4096/TCE_ENTRY_SIZE); | |
393 | dma_offset = next + be64_to_cpu(maprange->dma_base); | |
394 | ||
395 | for (l = 0; l < limit; l++) { | |
396 | tcep[l] = proto_tce | next; | |
397 | next += tce_size; | |
398 | } | |
399 | ||
400 | rc = plpar_tce_put_indirect(liobn, | |
401 | dma_offset, | |
402 | (u64)virt_to_abs(tcep), | |
403 | limit); | |
404 | ||
405 | num_tce -= limit; | |
406 | } while (num_tce > 0 && !rc); | |
407 | ||
408 | /* error cleanup: caller will clear whole range */ | |
409 | ||
410 | local_irq_enable(); | |
411 | return rc; | |
412 | } | |
413 | ||
414 | static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn, | |
415 | unsigned long num_pfn, void *arg) | |
416 | { | |
417 | return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg); | |
418 | } | |
419 | ||
420 | ||
bed59275 | 421 | #ifdef CONFIG_PCI |
1da177e4 LT |
422 | static void iommu_table_setparms(struct pci_controller *phb, |
423 | struct device_node *dn, | |
bc97ce95 | 424 | struct iommu_table *tbl) |
1da177e4 LT |
425 | { |
426 | struct device_node *node; | |
9938c474 NL |
427 | const unsigned long *basep; |
428 | const u32 *sizep; | |
1da177e4 | 429 | |
44ef3390 | 430 | node = phb->dn; |
1da177e4 | 431 | |
e2eb6392 SR |
432 | basep = of_get_property(node, "linux,tce-base", NULL); |
433 | sizep = of_get_property(node, "linux,tce-size", NULL); | |
1da177e4 LT |
434 | if (basep == NULL || sizep == NULL) { |
435 | printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has " | |
436 | "missing tce entries !\n", dn->full_name); | |
437 | return; | |
438 | } | |
439 | ||
440 | tbl->it_base = (unsigned long)__va(*basep); | |
5f50867b | 441 | |
62a8bd6c | 442 | if (!is_kdump_kernel()) |
54622f10 | 443 | memset((void *)tbl->it_base, 0, *sizep); |
1da177e4 LT |
444 | |
445 | tbl->it_busno = phb->bus->number; | |
bc97ce95 | 446 | |
1da177e4 | 447 | /* Units of tce entries */ |
5d2efba6 | 448 | tbl->it_offset = phb->dma_window_base_cur >> IOMMU_PAGE_SHIFT; |
bc97ce95 | 449 | |
1da177e4 | 450 | /* Test if we are going over 2GB of DMA space */ |
3c2822cc OJ |
451 | if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) { |
452 | udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n"); | |
bc97ce95 | 453 | panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n"); |
3c2822cc | 454 | } |
bc97ce95 | 455 | |
1da177e4 LT |
456 | phb->dma_window_base_cur += phb->dma_window_size; |
457 | ||
458 | /* Set the tce table size - measured in entries */ | |
5d2efba6 | 459 | tbl->it_size = phb->dma_window_size >> IOMMU_PAGE_SHIFT; |
1da177e4 LT |
460 | |
461 | tbl->it_index = 0; | |
462 | tbl->it_blocksize = 16; | |
463 | tbl->it_type = TCE_PCI; | |
464 | } | |
465 | ||
466 | /* | |
467 | * iommu_table_setparms_lpar | |
468 | * | |
469 | * Function: On pSeries LPAR systems, return TCE table info, given a pci bus. | |
1da177e4 LT |
470 | */ |
471 | static void iommu_table_setparms_lpar(struct pci_controller *phb, | |
472 | struct device_node *dn, | |
473 | struct iommu_table *tbl, | |
b8c49def | 474 | const void *dma_window) |
1da177e4 | 475 | { |
4c76e0bc JK |
476 | unsigned long offset, size; |
477 | ||
4c76e0bc | 478 | of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size); |
1da177e4 | 479 | |
b8c49def | 480 | tbl->it_busno = phb->bus->number; |
1da177e4 | 481 | tbl->it_base = 0; |
1da177e4 LT |
482 | tbl->it_blocksize = 16; |
483 | tbl->it_type = TCE_PCI; | |
5d2efba6 LV |
484 | tbl->it_offset = offset >> IOMMU_PAGE_SHIFT; |
485 | tbl->it_size = size >> IOMMU_PAGE_SHIFT; | |
1da177e4 LT |
486 | } |
487 | ||
12d04eef | 488 | static void pci_dma_bus_setup_pSeries(struct pci_bus *bus) |
1da177e4 | 489 | { |
3c2822cc | 490 | struct device_node *dn; |
1da177e4 | 491 | struct iommu_table *tbl; |
3c2822cc OJ |
492 | struct device_node *isa_dn, *isa_dn_orig; |
493 | struct device_node *tmp; | |
494 | struct pci_dn *pci; | |
495 | int children; | |
1da177e4 | 496 | |
3c2822cc | 497 | dn = pci_bus_to_OF_node(bus); |
12d04eef | 498 | |
f7ebf352 | 499 | pr_debug("pci_dma_bus_setup_pSeries: setting up bus %s\n", dn->full_name); |
3c2822cc OJ |
500 | |
501 | if (bus->self) { | |
502 | /* This is not a root bus, any setup will be done for the | |
503 | * device-side of the bridge in iommu_dev_setup_pSeries(). | |
504 | */ | |
505 | return; | |
506 | } | |
12d04eef | 507 | pci = PCI_DN(dn); |
3c2822cc OJ |
508 | |
509 | /* Check if the ISA bus on the system is under | |
510 | * this PHB. | |
1da177e4 | 511 | */ |
3c2822cc | 512 | isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa"); |
1da177e4 | 513 | |
3c2822cc OJ |
514 | while (isa_dn && isa_dn != dn) |
515 | isa_dn = isa_dn->parent; | |
516 | ||
517 | if (isa_dn_orig) | |
518 | of_node_put(isa_dn_orig); | |
1da177e4 | 519 | |
d3c58fb1 | 520 | /* Count number of direct PCI children of the PHB. */ |
3c2822cc | 521 | for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling) |
d3c58fb1 | 522 | children++; |
1da177e4 | 523 | |
f7ebf352 | 524 | pr_debug("Children: %d\n", children); |
1da177e4 | 525 | |
3c2822cc OJ |
526 | /* Calculate amount of DMA window per slot. Each window must be |
527 | * a power of two (due to pci_alloc_consistent requirements). | |
528 | * | |
529 | * Keep 256MB aside for PHBs with ISA. | |
530 | */ | |
1da177e4 | 531 | |
3c2822cc OJ |
532 | if (!isa_dn) { |
533 | /* No ISA/IDE - just set window size and return */ | |
534 | pci->phb->dma_window_size = 0x80000000ul; /* To be divided */ | |
535 | ||
536 | while (pci->phb->dma_window_size * children > 0x80000000ul) | |
537 | pci->phb->dma_window_size >>= 1; | |
41febbc8 | 538 | pr_debug("No ISA/IDE, window size is 0x%llx\n", |
f7ebf352 | 539 | pci->phb->dma_window_size); |
3c2822cc OJ |
540 | pci->phb->dma_window_base_cur = 0; |
541 | ||
542 | return; | |
1da177e4 | 543 | } |
3c2822cc OJ |
544 | |
545 | /* If we have ISA, then we probably have an IDE | |
546 | * controller too. Allocate a 128MB table but | |
547 | * skip the first 128MB to avoid stepping on ISA | |
548 | * space. | |
549 | */ | |
550 | pci->phb->dma_window_size = 0x8000000ul; | |
551 | pci->phb->dma_window_base_cur = 0x8000000ul; | |
552 | ||
7aa241fd | 553 | tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, |
ca1588e7 | 554 | pci->phb->node); |
3c2822cc OJ |
555 | |
556 | iommu_table_setparms(pci->phb, dn, tbl); | |
ca1588e7 | 557 | pci->iommu_table = iommu_init_table(tbl, pci->phb->node); |
3c2822cc OJ |
558 | |
559 | /* Divide the rest (1.75GB) among the children */ | |
560 | pci->phb->dma_window_size = 0x80000000ul; | |
561 | while (pci->phb->dma_window_size * children > 0x70000000ul) | |
562 | pci->phb->dma_window_size >>= 1; | |
563 | ||
41febbc8 | 564 | pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size); |
1da177e4 LT |
565 | } |
566 | ||
567 | ||
12d04eef | 568 | static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus) |
1da177e4 LT |
569 | { |
570 | struct iommu_table *tbl; | |
571 | struct device_node *dn, *pdn; | |
1635317f | 572 | struct pci_dn *ppci; |
954a46e2 | 573 | const void *dma_window = NULL; |
1da177e4 | 574 | |
1da177e4 LT |
575 | dn = pci_bus_to_OF_node(bus); |
576 | ||
f7ebf352 ME |
577 | pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %s\n", |
578 | dn->full_name); | |
12d04eef | 579 | |
1da177e4 LT |
580 | /* Find nearest ibm,dma-window, walking up the device tree */ |
581 | for (pdn = dn; pdn != NULL; pdn = pdn->parent) { | |
e2eb6392 | 582 | dma_window = of_get_property(pdn, "ibm,dma-window", NULL); |
1da177e4 LT |
583 | if (dma_window != NULL) |
584 | break; | |
585 | } | |
586 | ||
587 | if (dma_window == NULL) { | |
f7ebf352 | 588 | pr_debug(" no ibm,dma-window property !\n"); |
1da177e4 LT |
589 | return; |
590 | } | |
591 | ||
e07102db | 592 | ppci = PCI_DN(pdn); |
12d04eef | 593 | |
f7ebf352 ME |
594 | pr_debug(" parent is %s, iommu_table: 0x%p\n", |
595 | pdn->full_name, ppci->iommu_table); | |
12d04eef | 596 | |
1635317f | 597 | if (!ppci->iommu_table) { |
7aa241fd | 598 | tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, |
ca1588e7 | 599 | ppci->phb->node); |
b8c49def | 600 | iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window); |
ca1588e7 | 601 | ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node); |
f7ebf352 | 602 | pr_debug(" created table: %p\n", ppci->iommu_table); |
1da177e4 | 603 | } |
1da177e4 LT |
604 | } |
605 | ||
606 | ||
12d04eef | 607 | static void pci_dma_dev_setup_pSeries(struct pci_dev *dev) |
1da177e4 | 608 | { |
12d04eef | 609 | struct device_node *dn; |
3c2822cc | 610 | struct iommu_table *tbl; |
1da177e4 | 611 | |
f7ebf352 | 612 | pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev)); |
1da177e4 | 613 | |
58f9b0b0 | 614 | dn = dev->dev.of_node; |
1da177e4 | 615 | |
3c2822cc OJ |
616 | /* If we're the direct child of a root bus, then we need to allocate |
617 | * an iommu table ourselves. The bus setup code should have setup | |
618 | * the window sizes already. | |
619 | */ | |
620 | if (!dev->bus->self) { | |
12d04eef BH |
621 | struct pci_controller *phb = PCI_DN(dn)->phb; |
622 | ||
f7ebf352 | 623 | pr_debug(" --> first child, no bridge. Allocating iommu table.\n"); |
7aa241fd | 624 | tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, |
12d04eef BH |
625 | phb->node); |
626 | iommu_table_setparms(phb, dn, tbl); | |
77319254 | 627 | PCI_DN(dn)->iommu_table = iommu_init_table(tbl, phb->node); |
738ef42e | 628 | set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table); |
3c2822cc OJ |
629 | return; |
630 | } | |
631 | ||
632 | /* If this device is further down the bus tree, search upwards until | |
633 | * an already allocated iommu table is found and use that. | |
634 | */ | |
635 | ||
e07102db | 636 | while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL) |
1da177e4 LT |
637 | dn = dn->parent; |
638 | ||
12d04eef | 639 | if (dn && PCI_DN(dn)) |
738ef42e | 640 | set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table); |
12d04eef BH |
641 | else |
642 | printk(KERN_WARNING "iommu: Device %s has no iommu table\n", | |
643 | pci_name(dev)); | |
1da177e4 LT |
644 | } |
645 | ||
4e8b0cf4 NA |
646 | static int __read_mostly disable_ddw; |
647 | ||
648 | static int __init disable_ddw_setup(char *str) | |
649 | { | |
650 | disable_ddw = 1; | |
651 | printk(KERN_INFO "ppc iommu: disabling ddw.\n"); | |
652 | ||
653 | return 0; | |
654 | } | |
655 | ||
656 | early_param("disable_ddw", disable_ddw_setup); | |
657 | ||
658 | static void remove_ddw(struct device_node *np) | |
659 | { | |
660 | struct dynamic_dma_window_prop *dwp; | |
661 | struct property *win64; | |
662 | const u32 *ddr_avail; | |
663 | u64 liobn; | |
664 | int len, ret; | |
665 | ||
666 | ddr_avail = of_get_property(np, "ibm,ddw-applicable", &len); | |
667 | win64 = of_find_property(np, DIRECT64_PROPNAME, NULL); | |
2573f684 | 668 | if (!win64) |
4e8b0cf4 NA |
669 | return; |
670 | ||
2573f684 MM |
671 | if (!ddr_avail || len < 3 * sizeof(u32) || win64->length < sizeof(*dwp)) |
672 | goto delprop; | |
673 | ||
4e8b0cf4 NA |
674 | dwp = win64->value; |
675 | liobn = (u64)be32_to_cpu(dwp->liobn); | |
676 | ||
677 | /* clear the whole window, note the arg is in kernel pages */ | |
678 | ret = tce_clearrange_multi_pSeriesLP(0, | |
679 | 1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp); | |
680 | if (ret) | |
681 | pr_warning("%s failed to clear tces in window.\n", | |
682 | np->full_name); | |
683 | else | |
684 | pr_debug("%s successfully cleared tces in window.\n", | |
685 | np->full_name); | |
686 | ||
687 | ret = rtas_call(ddr_avail[2], 1, 1, NULL, liobn); | |
688 | if (ret) | |
689 | pr_warning("%s: failed to remove direct window: rtas returned " | |
690 | "%d to ibm,remove-pe-dma-window(%x) %llx\n", | |
691 | np->full_name, ret, ddr_avail[2], liobn); | |
692 | else | |
693 | pr_debug("%s: successfully removed direct window: rtas returned " | |
694 | "%d to ibm,remove-pe-dma-window(%x) %llx\n", | |
695 | np->full_name, ret, ddr_avail[2], liobn); | |
4e8b0cf4 | 696 | |
2573f684 | 697 | delprop: |
c8566780 | 698 | ret = prom_remove_property(np, win64); |
2573f684 | 699 | if (ret) |
c8566780 | 700 | pr_warning("%s: failed to remove direct window property: %d\n", |
2573f684 MM |
701 | np->full_name, ret); |
702 | } | |
4e8b0cf4 | 703 | |
23a6c484 | 704 | static u64 dupe_ddw_if_already_created(struct pci_dev *dev, struct device_node *pdn) |
4e8b0cf4 NA |
705 | { |
706 | struct device_node *dn; | |
707 | struct pci_dn *pcidn; | |
708 | struct direct_window *window; | |
709 | const struct dynamic_dma_window_prop *direct64; | |
710 | u64 dma_addr = 0; | |
711 | ||
712 | dn = pci_device_to_OF_node(dev); | |
713 | pcidn = PCI_DN(dn); | |
714 | spin_lock(&direct_window_list_lock); | |
715 | /* check if we already created a window and dupe that config if so */ | |
716 | list_for_each_entry(window, &direct_window_list, list) { | |
717 | if (window->device == pdn) { | |
718 | direct64 = window->prop; | |
719 | dma_addr = direct64->dma_base; | |
720 | break; | |
721 | } | |
722 | } | |
723 | spin_unlock(&direct_window_list_lock); | |
724 | ||
725 | return dma_addr; | |
726 | } | |
727 | ||
c8566780 | 728 | static int find_existing_ddw_windows(void) |
4e8b0cf4 | 729 | { |
4e8b0cf4 | 730 | int len; |
c8566780 | 731 | struct device_node *pdn; |
4e8b0cf4 NA |
732 | struct direct_window *window; |
733 | const struct dynamic_dma_window_prop *direct64; | |
4e8b0cf4 | 734 | |
c8566780 MM |
735 | if (!firmware_has_feature(FW_FEATURE_LPAR)) |
736 | return 0; | |
737 | ||
738 | for_each_node_with_property(pdn, DIRECT64_PROPNAME) { | |
739 | direct64 = of_get_property(pdn, DIRECT64_PROPNAME, &len); | |
740 | if (!direct64) | |
741 | continue; | |
742 | ||
743 | window = kzalloc(sizeof(*window), GFP_KERNEL); | |
744 | if (!window || len < sizeof(struct dynamic_dma_window_prop)) { | |
745 | kfree(window); | |
4e8b0cf4 | 746 | remove_ddw(pdn); |
c8566780 | 747 | continue; |
4e8b0cf4 | 748 | } |
c8566780 MM |
749 | |
750 | window->device = pdn; | |
751 | window->prop = direct64; | |
752 | spin_lock(&direct_window_list_lock); | |
753 | list_add(&window->list, &direct_window_list); | |
754 | spin_unlock(&direct_window_list_lock); | |
4e8b0cf4 NA |
755 | } |
756 | ||
c8566780 | 757 | return 0; |
4e8b0cf4 | 758 | } |
c8566780 | 759 | machine_arch_initcall(pseries, find_existing_ddw_windows); |
4e8b0cf4 NA |
760 | |
761 | static int query_ddw(struct pci_dev *dev, const u32 *ddr_avail, | |
762 | struct ddw_query_response *query) | |
763 | { | |
764 | struct device_node *dn; | |
765 | struct pci_dn *pcidn; | |
766 | u32 cfg_addr; | |
767 | u64 buid; | |
768 | int ret; | |
769 | ||
770 | /* | |
771 | * Get the config address and phb buid of the PE window. | |
772 | * Rely on eeh to retrieve this for us. | |
773 | * Retrieve them from the pci device, not the node with the | |
774 | * dma-window property | |
775 | */ | |
776 | dn = pci_device_to_OF_node(dev); | |
777 | pcidn = PCI_DN(dn); | |
778 | cfg_addr = pcidn->eeh_config_addr; | |
779 | if (pcidn->eeh_pe_config_addr) | |
780 | cfg_addr = pcidn->eeh_pe_config_addr; | |
781 | buid = pcidn->phb->buid; | |
782 | ret = rtas_call(ddr_avail[0], 3, 5, (u32 *)query, | |
783 | cfg_addr, BUID_HI(buid), BUID_LO(buid)); | |
784 | dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x" | |
785 | " returned %d\n", ddr_avail[0], cfg_addr, BUID_HI(buid), | |
786 | BUID_LO(buid), ret); | |
787 | return ret; | |
788 | } | |
789 | ||
790 | static int create_ddw(struct pci_dev *dev, const u32 *ddr_avail, | |
791 | struct ddw_create_response *create, int page_shift, | |
792 | int window_shift) | |
793 | { | |
794 | struct device_node *dn; | |
795 | struct pci_dn *pcidn; | |
796 | u32 cfg_addr; | |
797 | u64 buid; | |
798 | int ret; | |
799 | ||
800 | /* | |
801 | * Get the config address and phb buid of the PE window. | |
802 | * Rely on eeh to retrieve this for us. | |
803 | * Retrieve them from the pci device, not the node with the | |
804 | * dma-window property | |
805 | */ | |
806 | dn = pci_device_to_OF_node(dev); | |
807 | pcidn = PCI_DN(dn); | |
808 | cfg_addr = pcidn->eeh_config_addr; | |
809 | if (pcidn->eeh_pe_config_addr) | |
810 | cfg_addr = pcidn->eeh_pe_config_addr; | |
811 | buid = pcidn->phb->buid; | |
812 | ||
813 | do { | |
814 | /* extra outputs are LIOBN and dma-addr (hi, lo) */ | |
815 | ret = rtas_call(ddr_avail[1], 5, 4, (u32 *)create, cfg_addr, | |
816 | BUID_HI(buid), BUID_LO(buid), page_shift, window_shift); | |
817 | } while (rtas_busy_delay(ret)); | |
818 | dev_info(&dev->dev, | |
819 | "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d " | |
820 | "(liobn = 0x%x starting addr = %x %x)\n", ddr_avail[1], | |
821 | cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift, | |
822 | window_shift, ret, create->liobn, create->addr_hi, create->addr_lo); | |
823 | ||
824 | return ret; | |
825 | } | |
826 | ||
827 | /* | |
828 | * If the PE supports dynamic dma windows, and there is space for a table | |
829 | * that can map all pages in a linear offset, then setup such a table, | |
830 | * and record the dma-offset in the struct device. | |
831 | * | |
832 | * dev: the pci device we are checking | |
833 | * pdn: the parent pe node with the ibm,dma_window property | |
834 | * Future: also check if we can remap the base window for our base page size | |
835 | * | |
836 | * returns the dma offset for use by dma_set_mask | |
837 | */ | |
838 | static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn) | |
839 | { | |
840 | int len, ret; | |
841 | struct ddw_query_response query; | |
842 | struct ddw_create_response create; | |
843 | int page_shift; | |
844 | u64 dma_addr, max_addr; | |
845 | struct device_node *dn; | |
846 | const u32 *uninitialized_var(ddr_avail); | |
847 | struct direct_window *window; | |
76730334 | 848 | struct property *win64; |
4e8b0cf4 NA |
849 | struct dynamic_dma_window_prop *ddwprop; |
850 | ||
851 | mutex_lock(&direct_window_init_mutex); | |
852 | ||
853 | dma_addr = dupe_ddw_if_already_created(dev, pdn); | |
854 | if (dma_addr != 0) | |
855 | goto out_unlock; | |
856 | ||
4e8b0cf4 NA |
857 | /* |
858 | * the ibm,ddw-applicable property holds the tokens for: | |
859 | * ibm,query-pe-dma-window | |
860 | * ibm,create-pe-dma-window | |
861 | * ibm,remove-pe-dma-window | |
862 | * for the given node in that order. | |
863 | * the property is actually in the parent, not the PE | |
864 | */ | |
865 | ddr_avail = of_get_property(pdn, "ibm,ddw-applicable", &len); | |
866 | if (!ddr_avail || len < 3 * sizeof(u32)) | |
867 | goto out_unlock; | |
868 | ||
869 | /* | |
870 | * Query if there is a second window of size to map the | |
871 | * whole partition. Query returns number of windows, largest | |
872 | * block assigned to PE (partition endpoint), and two bitmasks | |
873 | * of page sizes: supported and supported for migrate-dma. | |
874 | */ | |
875 | dn = pci_device_to_OF_node(dev); | |
876 | ret = query_ddw(dev, ddr_avail, &query); | |
877 | if (ret != 0) | |
878 | goto out_unlock; | |
879 | ||
880 | if (query.windows_available == 0) { | |
881 | /* | |
882 | * no additional windows are available for this device. | |
883 | * We might be able to reallocate the existing window, | |
884 | * trading in for a larger page size. | |
885 | */ | |
886 | dev_dbg(&dev->dev, "no free dynamic windows"); | |
887 | goto out_unlock; | |
888 | } | |
889 | if (query.page_size & 4) { | |
890 | page_shift = 24; /* 16MB */ | |
891 | } else if (query.page_size & 2) { | |
892 | page_shift = 16; /* 64kB */ | |
893 | } else if (query.page_size & 1) { | |
894 | page_shift = 12; /* 4kB */ | |
895 | } else { | |
896 | dev_dbg(&dev->dev, "no supported direct page size in mask %x", | |
897 | query.page_size); | |
898 | goto out_unlock; | |
899 | } | |
900 | /* verify the window * number of ptes will map the partition */ | |
901 | /* check largest block * page size > max memory hotplug addr */ | |
902 | max_addr = memory_hotplug_max(); | |
903 | if (query.largest_available_block < (max_addr >> page_shift)) { | |
904 | dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u " | |
905 | "%llu-sized pages\n", max_addr, query.largest_available_block, | |
906 | 1ULL << page_shift); | |
907 | goto out_unlock; | |
908 | } | |
909 | len = order_base_2(max_addr); | |
910 | win64 = kzalloc(sizeof(struct property), GFP_KERNEL); | |
911 | if (!win64) { | |
912 | dev_info(&dev->dev, | |
913 | "couldn't allocate property for 64bit dma window\n"); | |
914 | goto out_unlock; | |
915 | } | |
916 | win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL); | |
917 | win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL); | |
76730334 | 918 | win64->length = sizeof(*ddwprop); |
4e8b0cf4 NA |
919 | if (!win64->name || !win64->value) { |
920 | dev_info(&dev->dev, | |
921 | "couldn't allocate property name and value\n"); | |
922 | goto out_free_prop; | |
923 | } | |
924 | ||
925 | ret = create_ddw(dev, ddr_avail, &create, page_shift, len); | |
926 | if (ret != 0) | |
927 | goto out_free_prop; | |
928 | ||
929 | ddwprop->liobn = cpu_to_be32(create.liobn); | |
930 | ddwprop->dma_base = cpu_to_be64(of_read_number(&create.addr_hi, 2)); | |
931 | ddwprop->tce_shift = cpu_to_be32(page_shift); | |
932 | ddwprop->window_shift = cpu_to_be32(len); | |
933 | ||
934 | dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n", | |
935 | create.liobn, dn->full_name); | |
936 | ||
937 | window = kzalloc(sizeof(*window), GFP_KERNEL); | |
938 | if (!window) | |
939 | goto out_clear_window; | |
940 | ||
941 | ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT, | |
942 | win64->value, tce_setrange_multi_pSeriesLP_walk); | |
943 | if (ret) { | |
944 | dev_info(&dev->dev, "failed to map direct window for %s: %d\n", | |
945 | dn->full_name, ret); | |
946 | goto out_clear_window; | |
947 | } | |
948 | ||
949 | ret = prom_add_property(pdn, win64); | |
950 | if (ret) { | |
951 | dev_err(&dev->dev, "unable to add dma window property for %s: %d", | |
952 | pdn->full_name, ret); | |
953 | goto out_clear_window; | |
954 | } | |
955 | ||
956 | window->device = pdn; | |
957 | window->prop = ddwprop; | |
958 | spin_lock(&direct_window_list_lock); | |
959 | list_add(&window->list, &direct_window_list); | |
960 | spin_unlock(&direct_window_list_lock); | |
961 | ||
962 | dma_addr = of_read_number(&create.addr_hi, 2); | |
963 | goto out_unlock; | |
964 | ||
965 | out_clear_window: | |
966 | remove_ddw(pdn); | |
967 | ||
968 | out_free_prop: | |
969 | kfree(win64->name); | |
970 | kfree(win64->value); | |
971 | kfree(win64); | |
972 | ||
973 | out_unlock: | |
974 | mutex_unlock(&direct_window_init_mutex); | |
975 | return dma_addr; | |
976 | } | |
977 | ||
12d04eef | 978 | static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev) |
1da177e4 LT |
979 | { |
980 | struct device_node *pdn, *dn; | |
981 | struct iommu_table *tbl; | |
954a46e2 | 982 | const void *dma_window = NULL; |
1635317f | 983 | struct pci_dn *pci; |
1da177e4 | 984 | |
f7ebf352 | 985 | pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev)); |
12d04eef | 986 | |
1da177e4 | 987 | /* dev setup for LPAR is a little tricky, since the device tree might |
25985edc | 988 | * contain the dma-window properties per-device and not necessarily |
1da177e4 LT |
989 | * for the bus. So we need to search upwards in the tree until we |
990 | * either hit a dma-window property, OR find a parent with a table | |
991 | * already allocated. | |
992 | */ | |
993 | dn = pci_device_to_OF_node(dev); | |
f7ebf352 | 994 | pr_debug(" node is %s\n", dn->full_name); |
5d2efba6 | 995 | |
e07102db | 996 | for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table; |
1635317f | 997 | pdn = pdn->parent) { |
e2eb6392 | 998 | dma_window = of_get_property(pdn, "ibm,dma-window", NULL); |
1da177e4 LT |
999 | if (dma_window) |
1000 | break; | |
1001 | } | |
1002 | ||
650f7b3b LV |
1003 | if (!pdn || !PCI_DN(pdn)) { |
1004 | printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: " | |
1005 | "no DMA window found for pci dev=%s dn=%s\n", | |
1006 | pci_name(dev), dn? dn->full_name : "<null>"); | |
1007 | return; | |
1008 | } | |
f7ebf352 | 1009 | pr_debug(" parent is %s\n", pdn->full_name); |
12d04eef | 1010 | |
e07102db | 1011 | pci = PCI_DN(pdn); |
1635317f | 1012 | if (!pci->iommu_table) { |
7aa241fd | 1013 | tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, |
ca1588e7 | 1014 | pci->phb->node); |
b8c49def | 1015 | iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window); |
ca1588e7 | 1016 | pci->iommu_table = iommu_init_table(tbl, pci->phb->node); |
f7ebf352 | 1017 | pr_debug(" created table: %p\n", pci->iommu_table); |
de113217 | 1018 | } else { |
f7ebf352 | 1019 | pr_debug(" found DMA window, table: %p\n", pci->iommu_table); |
1da177e4 LT |
1020 | } |
1021 | ||
738ef42e | 1022 | set_iommu_table_base(&dev->dev, pci->iommu_table); |
1da177e4 | 1023 | } |
4e8b0cf4 NA |
1024 | |
1025 | static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask) | |
1026 | { | |
1027 | bool ddw_enabled = false; | |
1028 | struct device_node *pdn, *dn; | |
1029 | struct pci_dev *pdev; | |
1030 | const void *dma_window = NULL; | |
1031 | u64 dma_offset; | |
1032 | ||
64ac822f | 1033 | if (!dev->dma_mask) |
4e8b0cf4 NA |
1034 | return -EIO; |
1035 | ||
64ac822f MM |
1036 | if (!dev_is_pci(dev)) |
1037 | goto check_mask; | |
1038 | ||
eb0dd411 NA |
1039 | pdev = to_pci_dev(dev); |
1040 | ||
4e8b0cf4 NA |
1041 | /* only attempt to use a new window if 64-bit DMA is requested */ |
1042 | if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) { | |
4e8b0cf4 NA |
1043 | dn = pci_device_to_OF_node(pdev); |
1044 | dev_dbg(dev, "node is %s\n", dn->full_name); | |
1045 | ||
1046 | /* | |
1047 | * the device tree might contain the dma-window properties | |
25985edc | 1048 | * per-device and not necessarily for the bus. So we need to |
4e8b0cf4 NA |
1049 | * search upwards in the tree until we either hit a dma-window |
1050 | * property, OR find a parent with a table already allocated. | |
1051 | */ | |
1052 | for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table; | |
1053 | pdn = pdn->parent) { | |
1054 | dma_window = of_get_property(pdn, "ibm,dma-window", NULL); | |
1055 | if (dma_window) | |
1056 | break; | |
1057 | } | |
1058 | if (pdn && PCI_DN(pdn)) { | |
1059 | dma_offset = enable_ddw(pdev, pdn); | |
1060 | if (dma_offset != 0) { | |
1061 | dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset); | |
1062 | set_dma_offset(dev, dma_offset); | |
1063 | set_dma_ops(dev, &dma_direct_ops); | |
1064 | ddw_enabled = true; | |
1065 | } | |
1066 | } | |
1067 | } | |
1068 | ||
64ac822f MM |
1069 | /* fall back on iommu ops, restore table pointer with ops */ |
1070 | if (!ddw_enabled && get_dma_ops(dev) != &dma_iommu_ops) { | |
1071 | dev_info(dev, "Restoring 32-bit DMA via iommu\n"); | |
4e8b0cf4 | 1072 | set_dma_ops(dev, &dma_iommu_ops); |
eb0dd411 | 1073 | pci_dma_dev_setup_pSeriesLP(pdev); |
4e8b0cf4 NA |
1074 | } |
1075 | ||
64ac822f MM |
1076 | check_mask: |
1077 | if (!dma_supported(dev, dma_mask)) | |
1078 | return -EIO; | |
1079 | ||
4e8b0cf4 NA |
1080 | *dev->dma_mask = dma_mask; |
1081 | return 0; | |
1082 | } | |
1083 | ||
bed59275 SR |
1084 | #else /* CONFIG_PCI */ |
1085 | #define pci_dma_bus_setup_pSeries NULL | |
1086 | #define pci_dma_dev_setup_pSeries NULL | |
1087 | #define pci_dma_bus_setup_pSeriesLP NULL | |
1088 | #define pci_dma_dev_setup_pSeriesLP NULL | |
4e8b0cf4 | 1089 | #define dma_set_mask_pSeriesLP NULL |
bed59275 SR |
1090 | #endif /* !CONFIG_PCI */ |
1091 | ||
4e8b0cf4 NA |
1092 | static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action, |
1093 | void *data) | |
1094 | { | |
1095 | struct direct_window *window; | |
1096 | struct memory_notify *arg = data; | |
1097 | int ret = 0; | |
1098 | ||
1099 | switch (action) { | |
1100 | case MEM_GOING_ONLINE: | |
1101 | spin_lock(&direct_window_list_lock); | |
1102 | list_for_each_entry(window, &direct_window_list, list) { | |
1103 | ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn, | |
1104 | arg->nr_pages, window->prop); | |
1105 | /* XXX log error */ | |
1106 | } | |
1107 | spin_unlock(&direct_window_list_lock); | |
1108 | break; | |
1109 | case MEM_CANCEL_ONLINE: | |
1110 | case MEM_OFFLINE: | |
1111 | spin_lock(&direct_window_list_lock); | |
1112 | list_for_each_entry(window, &direct_window_list, list) { | |
1113 | ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn, | |
1114 | arg->nr_pages, window->prop); | |
1115 | /* XXX log error */ | |
1116 | } | |
1117 | spin_unlock(&direct_window_list_lock); | |
1118 | break; | |
1119 | default: | |
1120 | break; | |
1121 | } | |
1122 | if (ret && action != MEM_CANCEL_ONLINE) | |
1123 | return NOTIFY_BAD; | |
1124 | ||
1125 | return NOTIFY_OK; | |
1126 | } | |
1127 | ||
1128 | static struct notifier_block iommu_mem_nb = { | |
1129 | .notifier_call = iommu_mem_notifier, | |
1130 | }; | |
1131 | ||
bed59275 SR |
1132 | static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node) |
1133 | { | |
1134 | int err = NOTIFY_OK; | |
1135 | struct device_node *np = node; | |
1136 | struct pci_dn *pci = PCI_DN(np); | |
4e8b0cf4 | 1137 | struct direct_window *window; |
bed59275 SR |
1138 | |
1139 | switch (action) { | |
1140 | case PSERIES_RECONFIG_REMOVE: | |
7372cfb8 | 1141 | if (pci && pci->iommu_table) |
68d315f5 | 1142 | iommu_free_table(pci->iommu_table, np->full_name); |
4e8b0cf4 NA |
1143 | |
1144 | spin_lock(&direct_window_list_lock); | |
1145 | list_for_each_entry(window, &direct_window_list, list) { | |
1146 | if (window->device == np) { | |
1147 | list_del(&window->list); | |
1148 | kfree(window); | |
1149 | break; | |
1150 | } | |
1151 | } | |
1152 | spin_unlock(&direct_window_list_lock); | |
1153 | ||
1154 | /* | |
1155 | * Because the notifier runs after isolation of the | |
1156 | * slot, we are guaranteed any DMA window has already | |
1157 | * been revoked and the TCEs have been marked invalid, | |
1158 | * so we don't need a call to remove_ddw(np). However, | |
1159 | * if an additional notifier action is added before the | |
1160 | * isolate call, we should update this code for | |
1161 | * completeness with such a call. | |
1162 | */ | |
bed59275 SR |
1163 | break; |
1164 | default: | |
1165 | err = NOTIFY_DONE; | |
1166 | break; | |
1167 | } | |
1168 | return err; | |
1169 | } | |
1170 | ||
1171 | static struct notifier_block iommu_reconfig_nb = { | |
1172 | .notifier_call = iommu_reconfig_notifier, | |
1173 | }; | |
1da177e4 | 1174 | |
1da177e4 LT |
1175 | /* These are called very early. */ |
1176 | void iommu_init_early_pSeries(void) | |
1177 | { | |
a8daac8a | 1178 | if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL)) |
1da177e4 | 1179 | return; |
1da177e4 | 1180 | |
57cfb814 | 1181 | if (firmware_has_feature(FW_FEATURE_LPAR)) { |
1ababe11 | 1182 | if (firmware_has_feature(FW_FEATURE_MULTITCE)) { |
1da177e4 LT |
1183 | ppc_md.tce_build = tce_buildmulti_pSeriesLP; |
1184 | ppc_md.tce_free = tce_freemulti_pSeriesLP; | |
1185 | } else { | |
1186 | ppc_md.tce_build = tce_build_pSeriesLP; | |
1187 | ppc_md.tce_free = tce_free_pSeriesLP; | |
1188 | } | |
5f50867b | 1189 | ppc_md.tce_get = tce_get_pSeriesLP; |
12d04eef BH |
1190 | ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeriesLP; |
1191 | ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeriesLP; | |
4e8b0cf4 | 1192 | ppc_md.dma_set_mask = dma_set_mask_pSeriesLP; |
1da177e4 LT |
1193 | } else { |
1194 | ppc_md.tce_build = tce_build_pSeries; | |
1195 | ppc_md.tce_free = tce_free_pSeries; | |
5f50867b | 1196 | ppc_md.tce_get = tce_get_pseries; |
12d04eef BH |
1197 | ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeries; |
1198 | ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeries; | |
1da177e4 LT |
1199 | } |
1200 | ||
1201 | ||
1202 | pSeries_reconfig_notifier_register(&iommu_reconfig_nb); | |
4e8b0cf4 | 1203 | register_memory_notifier(&iommu_mem_nb); |
1da177e4 | 1204 | |
98747770 | 1205 | set_pci_dma_ops(&dma_iommu_ops); |
1da177e4 LT |
1206 | } |
1207 | ||
4e89a2d8 WS |
1208 | static int __init disable_multitce(char *str) |
1209 | { | |
1210 | if (strcmp(str, "off") == 0 && | |
1211 | firmware_has_feature(FW_FEATURE_LPAR) && | |
1212 | firmware_has_feature(FW_FEATURE_MULTITCE)) { | |
1213 | printk(KERN_INFO "Disabling MULTITCE firmware feature\n"); | |
1214 | ppc_md.tce_build = tce_build_pSeriesLP; | |
1215 | ppc_md.tce_free = tce_free_pSeriesLP; | |
1216 | powerpc_firmware_features &= ~FW_FEATURE_MULTITCE; | |
1217 | } | |
1218 | return 1; | |
1219 | } | |
1220 | ||
1221 | __setup("multitce=", disable_multitce); |