blob: 6b58a395dff67f2cf29aa3c2a3bef33b1ba93142 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/mm.h>
31#include <linux/spinlock.h>
Paul Gortmaker62fe91b2011-05-27 14:25:11 -040032#include <linux/sched.h> /* for show_stack */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/string.h>
34#include <linux/pci.h>
35#include <linux/dma-mapping.h>
Milton Miller62a8bd62008-10-22 15:39:04 -050036#include <linux/crash_dump.h>
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +000037#include <linux/memory.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/io.h>
39#include <asm/prom.h>
40#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/iommu.h>
42#include <asm/pci-bridge.h>
43#include <asm/machdep.h>
44#include <asm/abs_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/pSeries_reconfig.h>
Stephen Rothwell1ababe12005-08-03 14:35:25 +100046#include <asm/firmware.h>
Olof Johanssonc707ffc2005-09-20 13:45:41 +100047#include <asm/tce.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100048#include <asm/ppc-pci.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110049#include <asm/udbg.h>
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +000050#include <asm/mmzone.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Michael Ellermana1218722005-11-03 15:33:31 +110052#include "plpar_wrappers.h"
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Milton Miller8d3d5892011-06-29 20:58:33 +000055static void tce_invalidate_pSeries_sw(struct iommu_table *tbl,
56 u64 *startp, u64 *endp)
57{
58 u64 __iomem *invalidate = (u64 __iomem *)tbl->it_index;
59 unsigned long start, end, inc;
60
61 start = __pa(startp);
62 end = __pa(endp);
63 inc = L1_CACHE_BYTES; /* invalidate a cacheline of TCEs at a time */
64
65 /* If this is non-zero, change the format. We shift the
66 * address and or in the magic from the device tree. */
67 if (tbl->it_busno) {
68 start <<= 12;
69 end <<= 12;
70 inc <<= 12;
71 start |= tbl->it_busno;
72 end |= tbl->it_busno;
73 }
74
75 end |= inc - 1; /* round up end to be different than start */
76
77 mb(); /* Make sure TCEs in memory are written */
78 while (start <= end) {
79 out_be64(invalidate, start);
80 start += inc;
81 }
82}
83
Robert Jennings6490c492008-07-24 04:31:16 +100084static int tce_build_pSeries(struct iommu_table *tbl, long index,
Olof Johanssonbc97ce92006-04-28 22:51:59 -050085 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +100086 enum dma_data_direction direction,
87 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Olof Johanssonbc97ce92006-04-28 22:51:59 -050089 u64 proto_tce;
Milton Miller8d3d5892011-06-29 20:58:33 +000090 u64 *tcep, *tces;
Olof Johanssonbc97ce92006-04-28 22:51:59 -050091 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Olof Johanssonbc97ce92006-04-28 22:51:59 -050093 proto_tce = TCE_PCI_READ; // Read allowed
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -050096 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Milton Miller8d3d5892011-06-29 20:58:33 +000098 tces = tcep = ((u64 *)tbl->it_base) + index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 while (npages--) {
Yinghai Lu95f72d12010-07-12 14:36:09 +1000101 /* can't move this out since we might cross MEMBLOCK boundary */
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500102 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
103 *tcep = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Olof Johanssond0035c622005-09-20 13:46:44 +1000105 uaddr += TCE_PAGE_SIZE;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500106 tcep++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
Milton Miller8d3d5892011-06-29 20:58:33 +0000108
Michael Neulingbc6dc752012-06-26 21:26:37 +0000109 if (tbl->it_type & TCE_PCI_SWINV_CREATE)
Milton Miller8d3d5892011-06-29 20:58:33 +0000110 tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
Robert Jennings6490c492008-07-24 04:31:16 +1000111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114
115static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
116{
Milton Miller8d3d5892011-06-29 20:58:33 +0000117 u64 *tcep, *tces;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Milton Miller8d3d5892011-06-29 20:58:33 +0000119 tces = tcep = ((u64 *)tbl->it_base) + index;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500120
121 while (npages--)
122 *(tcep++) = 0;
Milton Miller8d3d5892011-06-29 20:58:33 +0000123
Michael Neulingbc6dc752012-06-26 21:26:37 +0000124 if (tbl->it_type & TCE_PCI_SWINV_FREE)
Milton Miller8d3d5892011-06-29 20:58:33 +0000125 tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Haren Myneni5f508672006-06-22 23:35:10 -0700128static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
129{
130 u64 *tcep;
131
Haren Myneni5f508672006-06-22 23:35:10 -0700132 tcep = ((u64 *)tbl->it_base) + index;
133
134 return *tcep;
135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Robert Jennings6490c492008-07-24 04:31:16 +1000137static void tce_free_pSeriesLP(struct iommu_table*, long, long);
138static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
139
140static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000142 enum dma_data_direction direction,
143 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Robert Jennings6490c492008-07-24 04:31:16 +1000145 u64 rc = 0;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500146 u64 proto_tce, tce;
147 u64 rpn;
Robert Jennings6490c492008-07-24 04:31:16 +1000148 int ret = 0;
149 long tcenum_start = tcenum, npages_start = npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500151 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
152 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500154 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500157 tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
158 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
159
Robert Jennings6490c492008-07-24 04:31:16 +1000160 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
161 ret = (int)rc;
162 tce_free_pSeriesLP(tbl, tcenum_start,
163 (npages_start - (npages + 1)));
164 break;
165 }
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000168 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
169 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
170 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
171 printk("\ttce val = 0x%llx\n", tce );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 show_stack(current, (unsigned long *)__get_SP());
173 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 tcenum++;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500176 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Robert Jennings6490c492008-07-24 04:31:16 +1000178 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Nishanth Aravamudana8daac82010-10-18 07:27:03 +0000181static DEFINE_PER_CPU(u64 *, tce_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Robert Jennings6490c492008-07-24 04:31:16 +1000183static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000185 enum dma_data_direction direction,
186 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Robert Jennings6490c492008-07-24 04:31:16 +1000188 u64 rc = 0;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500189 u64 proto_tce;
190 u64 *tcep;
191 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 long l, limit;
Robert Jennings6490c492008-07-24 04:31:16 +1000193 long tcenum_start = tcenum, npages_start = npages;
194 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Michael Ellerman541b2752008-05-08 14:27:23 +1000196 if (npages == 1) {
Robert Jennings6490c492008-07-24 04:31:16 +1000197 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
198 direction, attrs);
Michael Ellerman541b2752008-05-08 14:27:23 +1000199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 tcep = __get_cpu_var(tce_page);
202
203 /* This is safe to do since interrupts are off when we're called
204 * from iommu_alloc{,_sg}()
205 */
206 if (!tcep) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500207 tcep = (u64 *)__get_free_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* If allocation fails, fall back to the loop implementation */
Michael Ellerman541b2752008-05-08 14:27:23 +1000209 if (!tcep) {
Robert Jennings6490c492008-07-24 04:31:16 +1000210 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000211 direction, attrs);
Michael Ellerman541b2752008-05-08 14:27:23 +1000212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 __get_cpu_var(tce_page) = tcep;
214 }
215
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500216 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
217 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500219 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* We can map max one pageful of TCEs at a time */
222 do {
223 /*
224 * Set up the page with TCE data, looping through and setting
225 * the values.
226 */
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500227 limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 for (l = 0; l < limit; l++) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500230 tcep[l] = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
231 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
234 rc = plpar_tce_put_indirect((u64)tbl->it_index,
235 (u64)tcenum << 12,
236 (u64)virt_to_abs(tcep),
237 limit);
238
239 npages -= limit;
240 tcenum += limit;
241 } while (npages > 0 && !rc);
242
Robert Jennings6490c492008-07-24 04:31:16 +1000243 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
244 ret = (int)rc;
245 tce_freemulti_pSeriesLP(tbl, tcenum_start,
246 (npages_start - (npages + limit)));
247 return ret;
248 }
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000251 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
252 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
253 printk("\tnpages = 0x%llx\n", (u64)npages);
254 printk("\ttce[0] val = 0x%llx\n", tcep[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 show_stack(current, (unsigned long *)__get_SP());
256 }
Robert Jennings6490c492008-07-24 04:31:16 +1000257 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
261{
262 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500265 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000268 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
269 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
270 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 show_stack(current, (unsigned long *)__get_SP());
272 }
273
274 tcenum++;
275 }
276}
277
278
279static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
280{
281 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500283 rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (rc && printk_ratelimit()) {
286 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
Ingo Molnarfe333322009-01-06 14:26:03 +0000287 printk("\trc = %lld\n", rc);
288 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
289 printk("\tnpages = 0x%llx\n", (u64)npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 show_stack(current, (unsigned long *)__get_SP());
291 }
292}
293
Haren Myneni5f508672006-06-22 23:35:10 -0700294static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
295{
296 u64 rc;
297 unsigned long tce_ret;
298
Haren Myneni5f508672006-06-22 23:35:10 -0700299 rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
300
301 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000302 printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc);
303 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
304 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
Haren Myneni5f508672006-06-22 23:35:10 -0700305 show_stack(current, (unsigned long *)__get_SP());
306 }
307
308 return tce_ret;
309}
310
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300311/* this is compatible with cells for the device tree property */
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000312struct dynamic_dma_window_prop {
313 __be32 liobn; /* tce table number */
314 __be64 dma_base; /* address hi,lo */
315 __be32 tce_shift; /* ilog2(tce_page_size) */
316 __be32 window_shift; /* ilog2(tce_window_size) */
317};
318
319struct direct_window {
320 struct device_node *device;
321 const struct dynamic_dma_window_prop *prop;
322 struct list_head list;
323};
324
325/* Dynamic DMA Window support */
326struct ddw_query_response {
327 u32 windows_available;
328 u32 largest_available_block;
329 u32 page_size;
330 u32 migration_capable;
331};
332
333struct ddw_create_response {
334 u32 liobn;
335 u32 addr_hi;
336 u32 addr_lo;
337};
338
339static LIST_HEAD(direct_window_list);
340/* prevents races between memory on/offline and window creation */
341static DEFINE_SPINLOCK(direct_window_list_lock);
342/* protects initializing window twice for same device */
343static DEFINE_MUTEX(direct_window_init_mutex);
344#define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
345
346static int tce_clearrange_multi_pSeriesLP(unsigned long start_pfn,
347 unsigned long num_pfn, const void *arg)
348{
349 const struct dynamic_dma_window_prop *maprange = arg;
350 int rc;
351 u64 tce_size, num_tce, dma_offset, next;
352 u32 tce_shift;
353 long limit;
354
355 tce_shift = be32_to_cpu(maprange->tce_shift);
356 tce_size = 1ULL << tce_shift;
357 next = start_pfn << PAGE_SHIFT;
358 num_tce = num_pfn << PAGE_SHIFT;
359
360 /* round back to the beginning of the tce page size */
361 num_tce += next & (tce_size - 1);
362 next &= ~(tce_size - 1);
363
364 /* covert to number of tces */
365 num_tce |= tce_size - 1;
366 num_tce >>= tce_shift;
367
368 do {
369 /*
370 * Set up the page with TCE data, looping through and setting
371 * the values.
372 */
373 limit = min_t(long, num_tce, 512);
374 dma_offset = next + be64_to_cpu(maprange->dma_base);
375
376 rc = plpar_tce_stuff((u64)be32_to_cpu(maprange->liobn),
377 dma_offset,
378 0, limit);
379 num_tce -= limit;
380 } while (num_tce > 0 && !rc);
381
382 return rc;
383}
384
385static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
386 unsigned long num_pfn, const void *arg)
387{
388 const struct dynamic_dma_window_prop *maprange = arg;
389 u64 *tcep, tce_size, num_tce, dma_offset, next, proto_tce, liobn;
390 u32 tce_shift;
391 u64 rc = 0;
392 long l, limit;
393
394 local_irq_disable(); /* to protect tcep and the page behind it */
395 tcep = __get_cpu_var(tce_page);
396
397 if (!tcep) {
398 tcep = (u64 *)__get_free_page(GFP_ATOMIC);
399 if (!tcep) {
400 local_irq_enable();
401 return -ENOMEM;
402 }
403 __get_cpu_var(tce_page) = tcep;
404 }
405
406 proto_tce = TCE_PCI_READ | TCE_PCI_WRITE;
407
408 liobn = (u64)be32_to_cpu(maprange->liobn);
409 tce_shift = be32_to_cpu(maprange->tce_shift);
410 tce_size = 1ULL << tce_shift;
411 next = start_pfn << PAGE_SHIFT;
412 num_tce = num_pfn << PAGE_SHIFT;
413
414 /* round back to the beginning of the tce page size */
415 num_tce += next & (tce_size - 1);
416 next &= ~(tce_size - 1);
417
418 /* covert to number of tces */
419 num_tce |= tce_size - 1;
420 num_tce >>= tce_shift;
421
422 /* We can map max one pageful of TCEs at a time */
423 do {
424 /*
425 * Set up the page with TCE data, looping through and setting
426 * the values.
427 */
428 limit = min_t(long, num_tce, 4096/TCE_ENTRY_SIZE);
429 dma_offset = next + be64_to_cpu(maprange->dma_base);
430
431 for (l = 0; l < limit; l++) {
432 tcep[l] = proto_tce | next;
433 next += tce_size;
434 }
435
436 rc = plpar_tce_put_indirect(liobn,
437 dma_offset,
438 (u64)virt_to_abs(tcep),
439 limit);
440
441 num_tce -= limit;
442 } while (num_tce > 0 && !rc);
443
444 /* error cleanup: caller will clear whole range */
445
446 local_irq_enable();
447 return rc;
448}
449
450static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,
451 unsigned long num_pfn, void *arg)
452{
453 return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
454}
455
456
Stephen Rothwellbed59272007-03-04 17:04:44 +1100457#ifdef CONFIG_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static void iommu_table_setparms(struct pci_controller *phb,
459 struct device_node *dn,
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500460 struct iommu_table *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct device_node *node;
Milton Miller8d3d5892011-06-29 20:58:33 +0000463 const unsigned long *basep, *sw_inval;
Nathan Lynch9938c472006-10-04 22:28:00 -0500464 const u32 *sizep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100466 node = phb->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000468 basep = of_get_property(node, "linux,tce-base", NULL);
469 sizep = of_get_property(node, "linux,tce-size", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (basep == NULL || sizep == NULL) {
471 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
472 "missing tce entries !\n", dn->full_name);
473 return;
474 }
475
476 tbl->it_base = (unsigned long)__va(*basep);
Haren Myneni5f508672006-06-22 23:35:10 -0700477
Milton Miller62a8bd62008-10-22 15:39:04 -0500478 if (!is_kdump_kernel())
Mohan Kumar M54622f12008-10-21 17:38:10 +0000479 memset((void *)tbl->it_base, 0, *sizep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 tbl->it_busno = phb->bus->number;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* Units of tce entries */
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100484 tbl->it_offset = phb->dma_window_base_cur >> IOMMU_PAGE_SHIFT;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* Test if we are going over 2GB of DMA space */
Olof Johansson3c2822c2005-09-21 09:55:31 -0700487 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
488 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500489 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johansson3c2822c2005-09-21 09:55:31 -0700490 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 phb->dma_window_base_cur += phb->dma_window_size;
493
494 /* Set the tce table size - measured in entries */
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100495 tbl->it_size = phb->dma_window_size >> IOMMU_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 tbl->it_index = 0;
498 tbl->it_blocksize = 16;
499 tbl->it_type = TCE_PCI;
Milton Miller8d3d5892011-06-29 20:58:33 +0000500
501 sw_inval = of_get_property(node, "linux,tce-sw-invalidate-info", NULL);
502 if (sw_inval) {
503 /*
504 * This property contains information on how to
505 * invalidate the TCE entry. The first property is
506 * the base MMIO address used to invalidate entries.
507 * The second property tells us the format of the TCE
508 * invalidate (whether it needs to be shifted) and
509 * some magic routing info to add to our invalidate
510 * command.
511 */
512 tbl->it_index = (unsigned long) ioremap(sw_inval[0], 8);
513 tbl->it_busno = sw_inval[1]; /* overload this with magic */
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000514 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
Milton Miller8d3d5892011-06-29 20:58:33 +0000515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518/*
519 * iommu_table_setparms_lpar
520 *
521 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
523static void iommu_table_setparms_lpar(struct pci_controller *phb,
524 struct device_node *dn,
525 struct iommu_table *tbl,
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +1100526 const void *dma_window)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000528 unsigned long offset, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000530 of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
531
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +1100532 tbl->it_busno = phb->bus->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 tbl->it_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 tbl->it_blocksize = 16;
535 tbl->it_type = TCE_PCI;
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100536 tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
537 tbl->it_size = size >> IOMMU_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100540static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Olof Johansson3c2822c2005-09-21 09:55:31 -0700542 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct iommu_table *tbl;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700544 struct device_node *isa_dn, *isa_dn_orig;
545 struct device_node *tmp;
546 struct pci_dn *pci;
547 int children;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Olof Johansson3c2822c2005-09-21 09:55:31 -0700549 dn = pci_bus_to_OF_node(bus);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100550
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000551 pr_debug("pci_dma_bus_setup_pSeries: setting up bus %s\n", dn->full_name);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700552
553 if (bus->self) {
554 /* This is not a root bus, any setup will be done for the
555 * device-side of the bridge in iommu_dev_setup_pSeries().
556 */
557 return;
558 }
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100559 pci = PCI_DN(dn);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700560
561 /* Check if the ISA bus on the system is under
562 * this PHB.
563 */
564 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
565
566 while (isa_dn && isa_dn != dn)
567 isa_dn = isa_dn->parent;
568
569 if (isa_dn_orig)
570 of_node_put(isa_dn_orig);
571
Anton Blanchardd3c58fb2006-06-20 18:00:30 +1000572 /* Count number of direct PCI children of the PHB. */
Olof Johansson3c2822c2005-09-21 09:55:31 -0700573 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
Anton Blanchardd3c58fb2006-06-20 18:00:30 +1000574 children++;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700575
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000576 pr_debug("Children: %d\n", children);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700577
578 /* Calculate amount of DMA window per slot. Each window must be
579 * a power of two (due to pci_alloc_consistent requirements).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 *
Olof Johansson3c2822c2005-09-21 09:55:31 -0700581 * Keep 256MB aside for PHBs with ISA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
583
Olof Johansson3c2822c2005-09-21 09:55:31 -0700584 if (!isa_dn) {
585 /* No ISA/IDE - just set window size and return */
586 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Olof Johansson3c2822c2005-09-21 09:55:31 -0700588 while (pci->phb->dma_window_size * children > 0x80000000ul)
589 pci->phb->dma_window_size >>= 1;
Stephen Rothwell41febbc2009-06-02 18:21:30 +0000590 pr_debug("No ISA/IDE, window size is 0x%llx\n",
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000591 pci->phb->dma_window_size);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700592 pci->phb->dma_window_base_cur = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Olof Johansson3c2822c2005-09-21 09:55:31 -0700594 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Olof Johansson3c2822c2005-09-21 09:55:31 -0700596
597 /* If we have ISA, then we probably have an IDE
598 * controller too. Allocate a 128MB table but
599 * skip the first 128MB to avoid stepping on ISA
600 * space.
601 */
602 pci->phb->dma_window_size = 0x8000000ul;
603 pci->phb->dma_window_base_cur = 0x8000000ul;
604
Anton Blanchard7aa241f2010-08-11 16:42:48 +0000605 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Anton Blanchardca1588e2006-06-10 20:58:08 +1000606 pci->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700607
608 iommu_table_setparms(pci->phb, dn, tbl);
Anton Blanchardca1588e2006-06-10 20:58:08 +1000609 pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700610
611 /* Divide the rest (1.75GB) among the children */
612 pci->phb->dma_window_size = 0x80000000ul;
613 while (pci->phb->dma_window_size * children > 0x70000000ul)
614 pci->phb->dma_window_size >>= 1;
615
Stephen Rothwell41febbc2009-06-02 18:21:30 +0000616 pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100620static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 struct iommu_table *tbl;
623 struct device_node *dn, *pdn;
Paul Mackerras16353172005-09-06 13:17:54 +1000624 struct pci_dn *ppci;
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000625 const void *dma_window = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 dn = pci_bus_to_OF_node(bus);
628
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000629 pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %s\n",
630 dn->full_name);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* Find nearest ibm,dma-window, walking up the device tree */
633 for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000634 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (dma_window != NULL)
636 break;
637 }
638
639 if (dma_window == NULL) {
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000640 pr_debug(" no ibm,dma-window property !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return;
642 }
643
linase07102d2005-12-05 19:37:35 -0600644 ppci = PCI_DN(pdn);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100645
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000646 pr_debug(" parent is %s, iommu_table: 0x%p\n",
647 pdn->full_name, ppci->iommu_table);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100648
Paul Mackerras16353172005-09-06 13:17:54 +1000649 if (!ppci->iommu_table) {
Anton Blanchard7aa241f2010-08-11 16:42:48 +0000650 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Anton Blanchardca1588e2006-06-10 20:58:08 +1000651 ppci->phb->node);
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +1100652 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
Anton Blanchardca1588e2006-06-10 20:58:08 +1000653 ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node);
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000654 pr_debug(" created table: %p\n", ppci->iommu_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100659static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100661 struct device_node *dn;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700662 struct iommu_table *tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000664 pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
Olof Johansson3c2822c2005-09-21 09:55:31 -0700665
Grant Likely58f9b0b2010-04-13 16:12:56 -0700666 dn = dev->dev.of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Olof Johansson3c2822c2005-09-21 09:55:31 -0700668 /* If we're the direct child of a root bus, then we need to allocate
669 * an iommu table ourselves. The bus setup code should have setup
670 * the window sizes already.
671 */
672 if (!dev->bus->self) {
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100673 struct pci_controller *phb = PCI_DN(dn)->phb;
674
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000675 pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
Anton Blanchard7aa241f2010-08-11 16:42:48 +0000676 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100677 phb->node);
678 iommu_table_setparms(phb, dn, tbl);
Linas Vepstas77319252007-01-10 19:16:29 -0600679 PCI_DN(dn)->iommu_table = iommu_init_table(tbl, phb->node);
Becky Bruce738ef422009-09-21 08:26:35 +0000680 set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700681 return;
682 }
683
684 /* If this device is further down the bus tree, search upwards until
685 * an already allocated iommu table is found and use that.
686 */
687
linase07102d2005-12-05 19:37:35 -0600688 while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 dn = dn->parent;
690
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100691 if (dn && PCI_DN(dn))
Becky Bruce738ef422009-09-21 08:26:35 +0000692 set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100693 else
694 printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
695 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000698static int __read_mostly disable_ddw;
699
700static int __init disable_ddw_setup(char *str)
701{
702 disable_ddw = 1;
703 printk(KERN_INFO "ppc iommu: disabling ddw.\n");
704
705 return 0;
706}
707
708early_param("disable_ddw", disable_ddw_setup);
709
710static void remove_ddw(struct device_node *np)
711{
712 struct dynamic_dma_window_prop *dwp;
713 struct property *win64;
Milton Millerb73a6352011-05-11 12:25:00 +0000714 const u32 *ddw_avail;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000715 u64 liobn;
716 int len, ret;
717
Milton Millerb73a6352011-05-11 12:25:00 +0000718 ddw_avail = of_get_property(np, "ibm,ddw-applicable", &len);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000719 win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
Milton Miller2573f682011-05-11 12:24:58 +0000720 if (!win64)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000721 return;
722
Milton Millerb73a6352011-05-11 12:25:00 +0000723 if (!ddw_avail || len < 3 * sizeof(u32) || win64->length < sizeof(*dwp))
Milton Miller2573f682011-05-11 12:24:58 +0000724 goto delprop;
725
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000726 dwp = win64->value;
727 liobn = (u64)be32_to_cpu(dwp->liobn);
728
729 /* clear the whole window, note the arg is in kernel pages */
730 ret = tce_clearrange_multi_pSeriesLP(0,
731 1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp);
732 if (ret)
733 pr_warning("%s failed to clear tces in window.\n",
734 np->full_name);
735 else
736 pr_debug("%s successfully cleared tces in window.\n",
737 np->full_name);
738
Milton Millerb73a6352011-05-11 12:25:00 +0000739 ret = rtas_call(ddw_avail[2], 1, 1, NULL, liobn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000740 if (ret)
741 pr_warning("%s: failed to remove direct window: rtas returned "
742 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
Milton Millerb73a6352011-05-11 12:25:00 +0000743 np->full_name, ret, ddw_avail[2], liobn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000744 else
745 pr_debug("%s: successfully removed direct window: rtas returned "
746 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
Milton Millerb73a6352011-05-11 12:25:00 +0000747 np->full_name, ret, ddw_avail[2], liobn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000748
Milton Miller2573f682011-05-11 12:24:58 +0000749delprop:
Milton Millerc8566782011-05-11 12:24:59 +0000750 ret = prom_remove_property(np, win64);
Milton Miller2573f682011-05-11 12:24:58 +0000751 if (ret)
Milton Millerc8566782011-05-11 12:24:59 +0000752 pr_warning("%s: failed to remove direct window property: %d\n",
Milton Miller2573f682011-05-11 12:24:58 +0000753 np->full_name, ret);
754}
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000755
Milton Millerb73a6352011-05-11 12:25:00 +0000756static u64 find_existing_ddw(struct device_node *pdn)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000757{
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000758 struct direct_window *window;
759 const struct dynamic_dma_window_prop *direct64;
760 u64 dma_addr = 0;
761
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000762 spin_lock(&direct_window_list_lock);
763 /* check if we already created a window and dupe that config if so */
764 list_for_each_entry(window, &direct_window_list, list) {
765 if (window->device == pdn) {
766 direct64 = window->prop;
767 dma_addr = direct64->dma_base;
768 break;
769 }
770 }
771 spin_unlock(&direct_window_list_lock);
772
773 return dma_addr;
774}
775
Milton Millerc8566782011-05-11 12:24:59 +0000776static int find_existing_ddw_windows(void)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000777{
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000778 int len;
Milton Millerc8566782011-05-11 12:24:59 +0000779 struct device_node *pdn;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000780 struct direct_window *window;
781 const struct dynamic_dma_window_prop *direct64;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000782
Milton Millerc8566782011-05-11 12:24:59 +0000783 if (!firmware_has_feature(FW_FEATURE_LPAR))
784 return 0;
785
786 for_each_node_with_property(pdn, DIRECT64_PROPNAME) {
787 direct64 = of_get_property(pdn, DIRECT64_PROPNAME, &len);
788 if (!direct64)
789 continue;
790
791 window = kzalloc(sizeof(*window), GFP_KERNEL);
792 if (!window || len < sizeof(struct dynamic_dma_window_prop)) {
793 kfree(window);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000794 remove_ddw(pdn);
Milton Millerc8566782011-05-11 12:24:59 +0000795 continue;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000796 }
Milton Millerc8566782011-05-11 12:24:59 +0000797
798 window->device = pdn;
799 window->prop = direct64;
800 spin_lock(&direct_window_list_lock);
801 list_add(&window->list, &direct_window_list);
802 spin_unlock(&direct_window_list_lock);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000803 }
804
Milton Millerc8566782011-05-11 12:24:59 +0000805 return 0;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000806}
Milton Millerc8566782011-05-11 12:24:59 +0000807machine_arch_initcall(pseries, find_existing_ddw_windows);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000808
Milton Millerb73a6352011-05-11 12:25:00 +0000809static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000810 struct ddw_query_response *query)
811{
Gavin Shan39baadb2012-03-20 21:30:28 +0000812 struct eeh_dev *edev;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000813 u32 cfg_addr;
814 u64 buid;
815 int ret;
816
817 /*
818 * Get the config address and phb buid of the PE window.
819 * Rely on eeh to retrieve this for us.
820 * Retrieve them from the pci device, not the node with the
821 * dma-window property
822 */
Gavin Shan39baadb2012-03-20 21:30:28 +0000823 edev = pci_dev_to_eeh_dev(dev);
824 cfg_addr = edev->config_addr;
825 if (edev->pe_config_addr)
826 cfg_addr = edev->pe_config_addr;
827 buid = edev->phb->buid;
828
Milton Millerb73a6352011-05-11 12:25:00 +0000829 ret = rtas_call(ddw_avail[0], 3, 5, (u32 *)query,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000830 cfg_addr, BUID_HI(buid), BUID_LO(buid));
831 dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
Milton Millerb73a6352011-05-11 12:25:00 +0000832 " returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000833 BUID_LO(buid), ret);
834 return ret;
835}
836
Milton Millerb73a6352011-05-11 12:25:00 +0000837static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000838 struct ddw_create_response *create, int page_shift,
839 int window_shift)
840{
Gavin Shan39baadb2012-03-20 21:30:28 +0000841 struct eeh_dev *edev;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000842 u32 cfg_addr;
843 u64 buid;
844 int ret;
845
846 /*
847 * Get the config address and phb buid of the PE window.
848 * Rely on eeh to retrieve this for us.
849 * Retrieve them from the pci device, not the node with the
850 * dma-window property
851 */
Gavin Shan39baadb2012-03-20 21:30:28 +0000852 edev = pci_dev_to_eeh_dev(dev);
853 cfg_addr = edev->config_addr;
854 if (edev->pe_config_addr)
855 cfg_addr = edev->pe_config_addr;
856 buid = edev->phb->buid;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000857
858 do {
859 /* extra outputs are LIOBN and dma-addr (hi, lo) */
Milton Millerb73a6352011-05-11 12:25:00 +0000860 ret = rtas_call(ddw_avail[1], 5, 4, (u32 *)create, cfg_addr,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000861 BUID_HI(buid), BUID_LO(buid), page_shift, window_shift);
862 } while (rtas_busy_delay(ret));
863 dev_info(&dev->dev,
864 "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
Milton Millerb73a6352011-05-11 12:25:00 +0000865 "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000866 cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
867 window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
868
869 return ret;
870}
871
872/*
873 * If the PE supports dynamic dma windows, and there is space for a table
874 * that can map all pages in a linear offset, then setup such a table,
875 * and record the dma-offset in the struct device.
876 *
877 * dev: the pci device we are checking
878 * pdn: the parent pe node with the ibm,dma_window property
879 * Future: also check if we can remap the base window for our base page size
880 *
881 * returns the dma offset for use by dma_set_mask
882 */
883static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
884{
885 int len, ret;
886 struct ddw_query_response query;
887 struct ddw_create_response create;
888 int page_shift;
889 u64 dma_addr, max_addr;
890 struct device_node *dn;
Milton Millerb73a6352011-05-11 12:25:00 +0000891 const u32 *uninitialized_var(ddw_avail);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000892 struct direct_window *window;
Nishanth Aravamudan76730332011-05-06 13:27:30 +0000893 struct property *win64;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000894 struct dynamic_dma_window_prop *ddwprop;
895
896 mutex_lock(&direct_window_init_mutex);
897
Milton Millerb73a6352011-05-11 12:25:00 +0000898 dma_addr = find_existing_ddw(pdn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000899 if (dma_addr != 0)
900 goto out_unlock;
901
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000902 /*
903 * the ibm,ddw-applicable property holds the tokens for:
904 * ibm,query-pe-dma-window
905 * ibm,create-pe-dma-window
906 * ibm,remove-pe-dma-window
907 * for the given node in that order.
908 * the property is actually in the parent, not the PE
909 */
Milton Millerb73a6352011-05-11 12:25:00 +0000910 ddw_avail = of_get_property(pdn, "ibm,ddw-applicable", &len);
911 if (!ddw_avail || len < 3 * sizeof(u32))
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000912 goto out_unlock;
913
914 /*
915 * Query if there is a second window of size to map the
916 * whole partition. Query returns number of windows, largest
917 * block assigned to PE (partition endpoint), and two bitmasks
918 * of page sizes: supported and supported for migrate-dma.
919 */
920 dn = pci_device_to_OF_node(dev);
Milton Millerb73a6352011-05-11 12:25:00 +0000921 ret = query_ddw(dev, ddw_avail, &query);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000922 if (ret != 0)
923 goto out_unlock;
924
925 if (query.windows_available == 0) {
926 /*
927 * no additional windows are available for this device.
928 * We might be able to reallocate the existing window,
929 * trading in for a larger page size.
930 */
931 dev_dbg(&dev->dev, "no free dynamic windows");
932 goto out_unlock;
933 }
934 if (query.page_size & 4) {
935 page_shift = 24; /* 16MB */
936 } else if (query.page_size & 2) {
937 page_shift = 16; /* 64kB */
938 } else if (query.page_size & 1) {
939 page_shift = 12; /* 4kB */
940 } else {
941 dev_dbg(&dev->dev, "no supported direct page size in mask %x",
942 query.page_size);
943 goto out_unlock;
944 }
945 /* verify the window * number of ptes will map the partition */
946 /* check largest block * page size > max memory hotplug addr */
947 max_addr = memory_hotplug_max();
948 if (query.largest_available_block < (max_addr >> page_shift)) {
949 dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u "
950 "%llu-sized pages\n", max_addr, query.largest_available_block,
951 1ULL << page_shift);
952 goto out_unlock;
953 }
954 len = order_base_2(max_addr);
955 win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
956 if (!win64) {
957 dev_info(&dev->dev,
958 "couldn't allocate property for 64bit dma window\n");
959 goto out_unlock;
960 }
961 win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
962 win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
Nishanth Aravamudan76730332011-05-06 13:27:30 +0000963 win64->length = sizeof(*ddwprop);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000964 if (!win64->name || !win64->value) {
965 dev_info(&dev->dev,
966 "couldn't allocate property name and value\n");
967 goto out_free_prop;
968 }
969
Milton Millerb73a6352011-05-11 12:25:00 +0000970 ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000971 if (ret != 0)
972 goto out_free_prop;
973
974 ddwprop->liobn = cpu_to_be32(create.liobn);
975 ddwprop->dma_base = cpu_to_be64(of_read_number(&create.addr_hi, 2));
976 ddwprop->tce_shift = cpu_to_be32(page_shift);
977 ddwprop->window_shift = cpu_to_be32(len);
978
979 dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n",
980 create.liobn, dn->full_name);
981
982 window = kzalloc(sizeof(*window), GFP_KERNEL);
983 if (!window)
984 goto out_clear_window;
985
986 ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
987 win64->value, tce_setrange_multi_pSeriesLP_walk);
988 if (ret) {
989 dev_info(&dev->dev, "failed to map direct window for %s: %d\n",
990 dn->full_name, ret);
Julia Lawall7a190812011-08-08 01:18:00 +0000991 goto out_free_window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000992 }
993
994 ret = prom_add_property(pdn, win64);
995 if (ret) {
996 dev_err(&dev->dev, "unable to add dma window property for %s: %d",
997 pdn->full_name, ret);
Julia Lawall7a190812011-08-08 01:18:00 +0000998 goto out_free_window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000999 }
1000
1001 window->device = pdn;
1002 window->prop = ddwprop;
1003 spin_lock(&direct_window_list_lock);
1004 list_add(&window->list, &direct_window_list);
1005 spin_unlock(&direct_window_list_lock);
1006
1007 dma_addr = of_read_number(&create.addr_hi, 2);
1008 goto out_unlock;
1009
Julia Lawall7a190812011-08-08 01:18:00 +00001010out_free_window:
1011 kfree(window);
1012
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001013out_clear_window:
1014 remove_ddw(pdn);
1015
1016out_free_prop:
1017 kfree(win64->name);
1018 kfree(win64->value);
1019 kfree(win64);
1020
1021out_unlock:
1022 mutex_unlock(&direct_window_init_mutex);
1023 return dma_addr;
1024}
1025
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001026static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 struct device_node *pdn, *dn;
1029 struct iommu_table *tbl;
Jeremy Kerr954a46e2006-07-12 15:39:43 +10001030 const void *dma_window = NULL;
Paul Mackerras16353172005-09-06 13:17:54 +10001031 struct pci_dn *pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001033 pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /* dev setup for LPAR is a little tricky, since the device tree might
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001036 * contain the dma-window properties per-device and not necessarily
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 * for the bus. So we need to search upwards in the tree until we
1038 * either hit a dma-window property, OR find a parent with a table
1039 * already allocated.
1040 */
1041 dn = pci_device_to_OF_node(dev);
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001042 pr_debug(" node is %s\n", dn->full_name);
Linas Vepstas5d2efba2006-10-30 16:15:59 +11001043
linase07102d2005-12-05 19:37:35 -06001044 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
Paul Mackerras16353172005-09-06 13:17:54 +10001045 pdn = pdn->parent) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +10001046 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (dma_window)
1048 break;
1049 }
1050
Linas Vepstas650f7b32007-04-11 06:11:23 +10001051 if (!pdn || !PCI_DN(pdn)) {
1052 printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
1053 "no DMA window found for pci dev=%s dn=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001054 pci_name(dev), of_node_full_name(dn));
Linas Vepstas650f7b32007-04-11 06:11:23 +10001055 return;
1056 }
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001057 pr_debug(" parent is %s\n", pdn->full_name);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001058
linase07102d2005-12-05 19:37:35 -06001059 pci = PCI_DN(pdn);
Paul Mackerras16353172005-09-06 13:17:54 +10001060 if (!pci->iommu_table) {
Anton Blanchard7aa241f2010-08-11 16:42:48 +00001061 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Anton Blanchardca1588e2006-06-10 20:58:08 +10001062 pci->phb->node);
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +11001063 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
Anton Blanchardca1588e2006-06-10 20:58:08 +10001064 pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001065 pr_debug(" created table: %p\n", pci->iommu_table);
Michael Neulingde113212007-05-10 15:16:27 +10001066 } else {
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001067 pr_debug(" found DMA window, table: %p\n", pci->iommu_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
Becky Bruce738ef422009-09-21 08:26:35 +00001070 set_iommu_table_base(&dev->dev, pci->iommu_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001072
1073static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask)
1074{
1075 bool ddw_enabled = false;
1076 struct device_node *pdn, *dn;
1077 struct pci_dev *pdev;
1078 const void *dma_window = NULL;
1079 u64 dma_offset;
1080
Milton Miller64ac8222011-05-11 12:24:57 +00001081 if (!dev->dma_mask)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001082 return -EIO;
1083
Milton Miller64ac8222011-05-11 12:24:57 +00001084 if (!dev_is_pci(dev))
1085 goto check_mask;
1086
Nishanth Aravamudaneb0dd412011-05-09 12:58:03 +00001087 pdev = to_pci_dev(dev);
1088
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001089 /* only attempt to use a new window if 64-bit DMA is requested */
1090 if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) {
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001091 dn = pci_device_to_OF_node(pdev);
1092 dev_dbg(dev, "node is %s\n", dn->full_name);
1093
1094 /*
1095 * the device tree might contain the dma-window properties
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001096 * per-device and not necessarily for the bus. So we need to
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001097 * search upwards in the tree until we either hit a dma-window
1098 * property, OR find a parent with a table already allocated.
1099 */
1100 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
1101 pdn = pdn->parent) {
1102 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1103 if (dma_window)
1104 break;
1105 }
1106 if (pdn && PCI_DN(pdn)) {
1107 dma_offset = enable_ddw(pdev, pdn);
1108 if (dma_offset != 0) {
1109 dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset);
1110 set_dma_offset(dev, dma_offset);
1111 set_dma_ops(dev, &dma_direct_ops);
1112 ddw_enabled = true;
1113 }
1114 }
1115 }
1116
Milton Miller64ac8222011-05-11 12:24:57 +00001117 /* fall back on iommu ops, restore table pointer with ops */
1118 if (!ddw_enabled && get_dma_ops(dev) != &dma_iommu_ops) {
1119 dev_info(dev, "Restoring 32-bit DMA via iommu\n");
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001120 set_dma_ops(dev, &dma_iommu_ops);
Nishanth Aravamudaneb0dd412011-05-09 12:58:03 +00001121 pci_dma_dev_setup_pSeriesLP(pdev);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001122 }
1123
Milton Miller64ac8222011-05-11 12:24:57 +00001124check_mask:
1125 if (!dma_supported(dev, dma_mask))
1126 return -EIO;
1127
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001128 *dev->dma_mask = dma_mask;
1129 return 0;
1130}
1131
Milton Miller6a5c7be2011-06-24 09:05:22 +00001132static u64 dma_get_required_mask_pSeriesLP(struct device *dev)
1133{
1134 if (!dev->dma_mask)
1135 return 0;
1136
1137 if (!disable_ddw && dev_is_pci(dev)) {
1138 struct pci_dev *pdev = to_pci_dev(dev);
1139 struct device_node *dn;
1140
1141 dn = pci_device_to_OF_node(pdev);
1142
1143 /* search upwards for ibm,dma-window */
1144 for (; dn && PCI_DN(dn) && !PCI_DN(dn)->iommu_table;
1145 dn = dn->parent)
1146 if (of_get_property(dn, "ibm,dma-window", NULL))
1147 break;
1148 /* if there is a ibm,ddw-applicable property require 64 bits */
1149 if (dn && PCI_DN(dn) &&
1150 of_get_property(dn, "ibm,ddw-applicable", NULL))
1151 return DMA_BIT_MASK(64);
1152 }
1153
Milton Millerd24f9c62011-06-24 09:05:24 +00001154 return dma_iommu_ops.get_required_mask(dev);
Milton Miller6a5c7be2011-06-24 09:05:22 +00001155}
1156
Stephen Rothwellbed59272007-03-04 17:04:44 +11001157#else /* CONFIG_PCI */
1158#define pci_dma_bus_setup_pSeries NULL
1159#define pci_dma_dev_setup_pSeries NULL
1160#define pci_dma_bus_setup_pSeriesLP NULL
1161#define pci_dma_dev_setup_pSeriesLP NULL
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001162#define dma_set_mask_pSeriesLP NULL
Milton Miller6a5c7be2011-06-24 09:05:22 +00001163#define dma_get_required_mask_pSeriesLP NULL
Stephen Rothwellbed59272007-03-04 17:04:44 +11001164#endif /* !CONFIG_PCI */
1165
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001166static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
1167 void *data)
1168{
1169 struct direct_window *window;
1170 struct memory_notify *arg = data;
1171 int ret = 0;
1172
1173 switch (action) {
1174 case MEM_GOING_ONLINE:
1175 spin_lock(&direct_window_list_lock);
1176 list_for_each_entry(window, &direct_window_list, list) {
1177 ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
1178 arg->nr_pages, window->prop);
1179 /* XXX log error */
1180 }
1181 spin_unlock(&direct_window_list_lock);
1182 break;
1183 case MEM_CANCEL_ONLINE:
1184 case MEM_OFFLINE:
1185 spin_lock(&direct_window_list_lock);
1186 list_for_each_entry(window, &direct_window_list, list) {
1187 ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
1188 arg->nr_pages, window->prop);
1189 /* XXX log error */
1190 }
1191 spin_unlock(&direct_window_list_lock);
1192 break;
1193 default:
1194 break;
1195 }
1196 if (ret && action != MEM_CANCEL_ONLINE)
1197 return NOTIFY_BAD;
1198
1199 return NOTIFY_OK;
1200}
1201
1202static struct notifier_block iommu_mem_nb = {
1203 .notifier_call = iommu_mem_notifier,
1204};
1205
Stephen Rothwellbed59272007-03-04 17:04:44 +11001206static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
1207{
1208 int err = NOTIFY_OK;
1209 struct device_node *np = node;
1210 struct pci_dn *pci = PCI_DN(np);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001211 struct direct_window *window;
Stephen Rothwellbed59272007-03-04 17:04:44 +11001212
1213 switch (action) {
1214 case PSERIES_RECONFIG_REMOVE:
Nishanth Aravamudan7372cfb2010-10-26 17:35:13 +00001215 if (pci && pci->iommu_table)
Stephen Rothwell68d315f2007-12-06 13:39:19 +11001216 iommu_free_table(pci->iommu_table, np->full_name);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001217
1218 spin_lock(&direct_window_list_lock);
1219 list_for_each_entry(window, &direct_window_list, list) {
1220 if (window->device == np) {
1221 list_del(&window->list);
1222 kfree(window);
1223 break;
1224 }
1225 }
1226 spin_unlock(&direct_window_list_lock);
1227
1228 /*
1229 * Because the notifier runs after isolation of the
1230 * slot, we are guaranteed any DMA window has already
1231 * been revoked and the TCEs have been marked invalid,
1232 * so we don't need a call to remove_ddw(np). However,
1233 * if an additional notifier action is added before the
1234 * isolate call, we should update this code for
1235 * completeness with such a call.
1236 */
Stephen Rothwellbed59272007-03-04 17:04:44 +11001237 break;
1238 default:
1239 err = NOTIFY_DONE;
1240 break;
1241 }
1242 return err;
1243}
1244
1245static struct notifier_block iommu_reconfig_nb = {
1246 .notifier_call = iommu_reconfig_notifier,
1247};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/* These are called very early. */
1250void iommu_init_early_pSeries(void)
1251{
Nishanth Aravamudana8daac82010-10-18 07:27:03 +00001252 if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Michael Ellerman57cfb812006-03-21 20:45:59 +11001255 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Stephen Rothwell1ababe12005-08-03 14:35:25 +10001256 if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 ppc_md.tce_build = tce_buildmulti_pSeriesLP;
1258 ppc_md.tce_free = tce_freemulti_pSeriesLP;
1259 } else {
1260 ppc_md.tce_build = tce_build_pSeriesLP;
1261 ppc_md.tce_free = tce_free_pSeriesLP;
1262 }
Haren Myneni5f508672006-06-22 23:35:10 -07001263 ppc_md.tce_get = tce_get_pSeriesLP;
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001264 ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
1265 ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001266 ppc_md.dma_set_mask = dma_set_mask_pSeriesLP;
Milton Miller6a5c7be2011-06-24 09:05:22 +00001267 ppc_md.dma_get_required_mask = dma_get_required_mask_pSeriesLP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 } else {
1269 ppc_md.tce_build = tce_build_pSeries;
1270 ppc_md.tce_free = tce_free_pSeries;
Haren Myneni5f508672006-06-22 23:35:10 -07001271 ppc_md.tce_get = tce_get_pseries;
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001272 ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeries;
1273 ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
1276
1277 pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001278 register_memory_notifier(&iommu_mem_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Stephen Rothwell98747772007-03-04 16:58:39 +11001280 set_pci_dma_ops(&dma_iommu_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Will Schmidt4e89a2d2010-09-28 15:33:12 +00001283static int __init disable_multitce(char *str)
1284{
1285 if (strcmp(str, "off") == 0 &&
1286 firmware_has_feature(FW_FEATURE_LPAR) &&
1287 firmware_has_feature(FW_FEATURE_MULTITCE)) {
1288 printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
1289 ppc_md.tce_build = tce_build_pSeriesLP;
1290 ppc_md.tce_free = tce_free_pSeriesLP;
1291 powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
1292 }
1293 return 1;
1294}
1295
1296__setup("multitce=", disable_multitce);