blob: fe5ededf0d60e456a6ef3e50290fe76786ecb146 [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>
32#include <linux/string.h>
33#include <linux/pci.h>
34#include <linux/dma-mapping.h>
Milton Miller62a8bd62008-10-22 15:39:04 -050035#include <linux/crash_dump.h>
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +000036#include <linux/memory.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/io.h>
38#include <asm/prom.h>
39#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/iommu.h>
41#include <asm/pci-bridge.h>
42#include <asm/machdep.h>
43#include <asm/abs_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/pSeries_reconfig.h>
Stephen Rothwell1ababe12005-08-03 14:35:25 +100045#include <asm/firmware.h>
Olof Johanssonc707ffc2005-09-20 13:45:41 +100046#include <asm/tce.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100047#include <asm/ppc-pci.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110048#include <asm/udbg.h>
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +000049#include <asm/mmzone.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Michael Ellermana1218722005-11-03 15:33:31 +110051#include "plpar_wrappers.h"
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Robert Jennings6490c492008-07-24 04:31:16 +100054static int tce_build_pSeries(struct iommu_table *tbl, long index,
Olof Johanssonbc97ce92006-04-28 22:51:59 -050055 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +100056 enum dma_data_direction direction,
57 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Olof Johanssonbc97ce92006-04-28 22:51:59 -050059 u64 proto_tce;
60 u64 *tcep;
61 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Olof Johanssonbc97ce92006-04-28 22:51:59 -050063 proto_tce = TCE_PCI_READ; // Read allowed
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -050066 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Olof Johanssonbc97ce92006-04-28 22:51:59 -050068 tcep = ((u64 *)tbl->it_base) + index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 while (npages--) {
Yinghai Lu95f72d12010-07-12 14:36:09 +100071 /* can't move this out since we might cross MEMBLOCK boundary */
Olof Johanssonbc97ce92006-04-28 22:51:59 -050072 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
73 *tcep = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Olof Johanssond0035c622005-09-20 13:46:44 +100075 uaddr += TCE_PAGE_SIZE;
Olof Johanssonbc97ce92006-04-28 22:51:59 -050076 tcep++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
Robert Jennings6490c492008-07-24 04:31:16 +100078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81
82static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
83{
Olof Johanssonbc97ce92006-04-28 22:51:59 -050084 u64 *tcep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Olof Johanssonbc97ce92006-04-28 22:51:59 -050086 tcep = ((u64 *)tbl->it_base) + index;
87
88 while (npages--)
89 *(tcep++) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Haren Myneni5f508672006-06-22 23:35:10 -070092static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
93{
94 u64 *tcep;
95
Haren Myneni5f508672006-06-22 23:35:10 -070096 tcep = ((u64 *)tbl->it_base) + index;
97
98 return *tcep;
99}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Robert Jennings6490c492008-07-24 04:31:16 +1000101static void tce_free_pSeriesLP(struct iommu_table*, long, long);
102static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
103
104static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000106 enum dma_data_direction direction,
107 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Robert Jennings6490c492008-07-24 04:31:16 +1000109 u64 rc = 0;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500110 u64 proto_tce, tce;
111 u64 rpn;
Robert Jennings6490c492008-07-24 04:31:16 +1000112 int ret = 0;
113 long tcenum_start = tcenum, npages_start = npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500115 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
116 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500118 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500121 tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
122 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
123
Robert Jennings6490c492008-07-24 04:31:16 +1000124 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000132 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 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 show_stack(current, (unsigned long *)__get_SP());
137 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 tcenum++;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500140 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Robert Jennings6490c492008-07-24 04:31:16 +1000142 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Nishanth Aravamudana8daac82010-10-18 07:27:03 +0000145static DEFINE_PER_CPU(u64 *, tce_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Robert Jennings6490c492008-07-24 04:31:16 +1000147static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000149 enum dma_data_direction direction,
150 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Robert Jennings6490c492008-07-24 04:31:16 +1000152 u64 rc = 0;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500153 u64 proto_tce;
154 u64 *tcep;
155 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 long l, limit;
Robert Jennings6490c492008-07-24 04:31:16 +1000157 long tcenum_start = tcenum, npages_start = npages;
158 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Michael Ellerman541b2752008-05-08 14:27:23 +1000160 if (npages == 1) {
Robert Jennings6490c492008-07-24 04:31:16 +1000161 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
162 direction, attrs);
Michael Ellerman541b2752008-05-08 14:27:23 +1000163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
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) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500171 tcep = (u64 *)__get_free_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* If allocation fails, fall back to the loop implementation */
Michael Ellerman541b2752008-05-08 14:27:23 +1000173 if (!tcep) {
Robert Jennings6490c492008-07-24 04:31:16 +1000174 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000175 direction, attrs);
Michael Ellerman541b2752008-05-08 14:27:23 +1000176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 __get_cpu_var(tce_page) = tcep;
178 }
179
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500180 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
181 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500183 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
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 */
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500191 limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 for (l = 0; l < limit; l++) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500194 tcep[l] = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
195 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
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
Robert Jennings6490c492008-07-24 04:31:16 +1000207 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000215 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 show_stack(current, (unsigned long *)__get_SP());
220 }
Robert Jennings6490c492008-07-24 04:31:16 +1000221 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
225{
226 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500229 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000232 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 show_stack(current, (unsigned long *)__get_SP());
236 }
237
238 tcenum++;
239 }
240}
241
242
243static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
244{
245 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500247 rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if (rc && printk_ratelimit()) {
250 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
Ingo Molnarfe333322009-01-06 14:26:03 +0000251 printk("\trc = %lld\n", rc);
252 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
253 printk("\tnpages = 0x%llx\n", (u64)npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 show_stack(current, (unsigned long *)__get_SP());
255 }
256}
257
Haren Myneni5f508672006-06-22 23:35:10 -0700258static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
259{
260 u64 rc;
261 unsigned long tce_ret;
262
Haren Myneni5f508672006-06-22 23:35:10 -0700263 rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
264
265 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000266 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);
Haren Myneni5f508672006-06-22 23:35:10 -0700269 show_stack(current, (unsigned long *)__get_SP());
270 }
271
272 return tce_ret;
273}
274
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300275/* this is compatible with cells for the device tree property */
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000276struct 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
283struct 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 */
290struct ddw_query_response {
291 u32 windows_available;
292 u32 largest_available_block;
293 u32 page_size;
294 u32 migration_capable;
295};
296
297struct ddw_create_response {
298 u32 liobn;
299 u32 addr_hi;
300 u32 addr_lo;
301};
302
303static LIST_HEAD(direct_window_list);
304/* prevents races between memory on/offline and window creation */
305static DEFINE_SPINLOCK(direct_window_list_lock);
306/* protects initializing window twice for same device */
307static DEFINE_MUTEX(direct_window_init_mutex);
308#define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
309
310static 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
349static 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
414static 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
Stephen Rothwellbed59272007-03-04 17:04:44 +1100421#ifdef CONFIG_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static void iommu_table_setparms(struct pci_controller *phb,
423 struct device_node *dn,
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500424 struct iommu_table *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct device_node *node;
Nathan Lynch9938c472006-10-04 22:28:00 -0500427 const unsigned long *basep;
428 const u32 *sizep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100430 node = phb->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000432 basep = of_get_property(node, "linux,tce-base", NULL);
433 sizep = of_get_property(node, "linux,tce-size", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 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);
Haren Myneni5f508672006-06-22 23:35:10 -0700441
Milton Miller62a8bd62008-10-22 15:39:04 -0500442 if (!is_kdump_kernel())
Mohan Kumar M54622f12008-10-21 17:38:10 +0000443 memset((void *)tbl->it_base, 0, *sizep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 tbl->it_busno = phb->bus->number;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* Units of tce entries */
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100448 tbl->it_offset = phb->dma_window_base_cur >> IOMMU_PAGE_SHIFT;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* Test if we are going over 2GB of DMA space */
Olof Johansson3c2822c2005-09-21 09:55:31 -0700451 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
452 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500453 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johansson3c2822c2005-09-21 09:55:31 -0700454 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 phb->dma_window_base_cur += phb->dma_window_size;
457
458 /* Set the tce table size - measured in entries */
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100459 tbl->it_size = phb->dma_window_size >> IOMMU_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 */
471static void iommu_table_setparms_lpar(struct pci_controller *phb,
472 struct device_node *dn,
473 struct iommu_table *tbl,
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +1100474 const void *dma_window)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000476 unsigned long offset, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000478 of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
479
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +1100480 tbl->it_busno = phb->bus->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 tbl->it_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 tbl->it_blocksize = 16;
483 tbl->it_type = TCE_PCI;
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100484 tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
485 tbl->it_size = size >> IOMMU_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100488static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Olof Johansson3c2822c2005-09-21 09:55:31 -0700490 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct iommu_table *tbl;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700492 struct device_node *isa_dn, *isa_dn_orig;
493 struct device_node *tmp;
494 struct pci_dn *pci;
495 int children;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Olof Johansson3c2822c2005-09-21 09:55:31 -0700497 dn = pci_bus_to_OF_node(bus);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100498
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000499 pr_debug("pci_dma_bus_setup_pSeries: setting up bus %s\n", dn->full_name);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700500
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 }
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100507 pci = PCI_DN(dn);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700508
509 /* Check if the ISA bus on the system is under
510 * this PHB.
511 */
512 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
513
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);
519
Anton Blanchardd3c58fb2006-06-20 18:00:30 +1000520 /* Count number of direct PCI children of the PHB. */
Olof Johansson3c2822c2005-09-21 09:55:31 -0700521 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
Anton Blanchardd3c58fb2006-06-20 18:00:30 +1000522 children++;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700523
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000524 pr_debug("Children: %d\n", children);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700525
526 /* Calculate amount of DMA window per slot. Each window must be
527 * a power of two (due to pci_alloc_consistent requirements).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *
Olof Johansson3c2822c2005-09-21 09:55:31 -0700529 * Keep 256MB aside for PHBs with ISA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
531
Olof Johansson3c2822c2005-09-21 09:55:31 -0700532 if (!isa_dn) {
533 /* No ISA/IDE - just set window size and return */
534 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Olof Johansson3c2822c2005-09-21 09:55:31 -0700536 while (pci->phb->dma_window_size * children > 0x80000000ul)
537 pci->phb->dma_window_size >>= 1;
Stephen Rothwell41febbc2009-06-02 18:21:30 +0000538 pr_debug("No ISA/IDE, window size is 0x%llx\n",
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000539 pci->phb->dma_window_size);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700540 pci->phb->dma_window_base_cur = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Olof Johansson3c2822c2005-09-21 09:55:31 -0700542 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Olof Johansson3c2822c2005-09-21 09:55:31 -0700544
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
Anton Blanchard7aa241f2010-08-11 16:42:48 +0000553 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Anton Blanchardca1588e2006-06-10 20:58:08 +1000554 pci->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700555
556 iommu_table_setparms(pci->phb, dn, tbl);
Anton Blanchardca1588e2006-06-10 20:58:08 +1000557 pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700558
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
Stephen Rothwell41febbc2009-06-02 18:21:30 +0000564 pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100568static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct iommu_table *tbl;
571 struct device_node *dn, *pdn;
Paul Mackerras16353172005-09-06 13:17:54 +1000572 struct pci_dn *ppci;
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000573 const void *dma_window = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 dn = pci_bus_to_OF_node(bus);
576
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000577 pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %s\n",
578 dn->full_name);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* Find nearest ibm,dma-window, walking up the device tree */
581 for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000582 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (dma_window != NULL)
584 break;
585 }
586
587 if (dma_window == NULL) {
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000588 pr_debug(" no ibm,dma-window property !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return;
590 }
591
linase07102d2005-12-05 19:37:35 -0600592 ppci = PCI_DN(pdn);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100593
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000594 pr_debug(" parent is %s, iommu_table: 0x%p\n",
595 pdn->full_name, ppci->iommu_table);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100596
Paul Mackerras16353172005-09-06 13:17:54 +1000597 if (!ppci->iommu_table) {
Anton Blanchard7aa241f2010-08-11 16:42:48 +0000598 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Anton Blanchardca1588e2006-06-10 20:58:08 +1000599 ppci->phb->node);
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +1100600 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
Anton Blanchardca1588e2006-06-10 20:58:08 +1000601 ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node);
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000602 pr_debug(" created table: %p\n", ppci->iommu_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100607static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100609 struct device_node *dn;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700610 struct iommu_table *tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000612 pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
Olof Johansson3c2822c2005-09-21 09:55:31 -0700613
Grant Likely58f9b0b2010-04-13 16:12:56 -0700614 dn = dev->dev.of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Olof Johansson3c2822c2005-09-21 09:55:31 -0700616 /* 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) {
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100621 struct pci_controller *phb = PCI_DN(dn)->phb;
622
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000623 pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
Anton Blanchard7aa241f2010-08-11 16:42:48 +0000624 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100625 phb->node);
626 iommu_table_setparms(phb, dn, tbl);
Linas Vepstas77319252007-01-10 19:16:29 -0600627 PCI_DN(dn)->iommu_table = iommu_init_table(tbl, phb->node);
Becky Bruce738ef422009-09-21 08:26:35 +0000628 set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700629 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
linase07102d2005-12-05 19:37:35 -0600636 while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 dn = dn->parent;
638
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100639 if (dn && PCI_DN(dn))
Becky Bruce738ef422009-09-21 08:26:35 +0000640 set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100641 else
642 printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
643 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000646static int __read_mostly disable_ddw;
647
648static 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
656early_param("disable_ddw", disable_ddw_setup);
657
658static void remove_ddw(struct device_node *np)
659{
660 struct dynamic_dma_window_prop *dwp;
661 struct property *win64;
Milton Millerb73a6352011-05-11 12:25:00 +0000662 const u32 *ddw_avail;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000663 u64 liobn;
664 int len, ret;
665
Milton Millerb73a6352011-05-11 12:25:00 +0000666 ddw_avail = of_get_property(np, "ibm,ddw-applicable", &len);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000667 win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
Milton Miller2573f682011-05-11 12:24:58 +0000668 if (!win64)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000669 return;
670
Milton Millerb73a6352011-05-11 12:25:00 +0000671 if (!ddw_avail || len < 3 * sizeof(u32) || win64->length < sizeof(*dwp))
Milton Miller2573f682011-05-11 12:24:58 +0000672 goto delprop;
673
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000674 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
Milton Millerb73a6352011-05-11 12:25:00 +0000687 ret = rtas_call(ddw_avail[2], 1, 1, NULL, liobn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000688 if (ret)
689 pr_warning("%s: failed to remove direct window: rtas returned "
690 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
Milton Millerb73a6352011-05-11 12:25:00 +0000691 np->full_name, ret, ddw_avail[2], liobn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000692 else
693 pr_debug("%s: successfully removed direct window: rtas returned "
694 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
Milton Millerb73a6352011-05-11 12:25:00 +0000695 np->full_name, ret, ddw_avail[2], liobn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000696
Milton Miller2573f682011-05-11 12:24:58 +0000697delprop:
Milton Millerc8566782011-05-11 12:24:59 +0000698 ret = prom_remove_property(np, win64);
Milton Miller2573f682011-05-11 12:24:58 +0000699 if (ret)
Milton Millerc8566782011-05-11 12:24:59 +0000700 pr_warning("%s: failed to remove direct window property: %d\n",
Milton Miller2573f682011-05-11 12:24:58 +0000701 np->full_name, ret);
702}
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000703
Milton Millerb73a6352011-05-11 12:25:00 +0000704static u64 find_existing_ddw(struct device_node *pdn)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000705{
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000706 struct direct_window *window;
707 const struct dynamic_dma_window_prop *direct64;
708 u64 dma_addr = 0;
709
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000710 spin_lock(&direct_window_list_lock);
711 /* check if we already created a window and dupe that config if so */
712 list_for_each_entry(window, &direct_window_list, list) {
713 if (window->device == pdn) {
714 direct64 = window->prop;
715 dma_addr = direct64->dma_base;
716 break;
717 }
718 }
719 spin_unlock(&direct_window_list_lock);
720
721 return dma_addr;
722}
723
Milton Millerc8566782011-05-11 12:24:59 +0000724static int find_existing_ddw_windows(void)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000725{
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000726 int len;
Milton Millerc8566782011-05-11 12:24:59 +0000727 struct device_node *pdn;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000728 struct direct_window *window;
729 const struct dynamic_dma_window_prop *direct64;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000730
Milton Millerc8566782011-05-11 12:24:59 +0000731 if (!firmware_has_feature(FW_FEATURE_LPAR))
732 return 0;
733
734 for_each_node_with_property(pdn, DIRECT64_PROPNAME) {
735 direct64 = of_get_property(pdn, DIRECT64_PROPNAME, &len);
736 if (!direct64)
737 continue;
738
739 window = kzalloc(sizeof(*window), GFP_KERNEL);
740 if (!window || len < sizeof(struct dynamic_dma_window_prop)) {
741 kfree(window);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000742 remove_ddw(pdn);
Milton Millerc8566782011-05-11 12:24:59 +0000743 continue;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000744 }
Milton Millerc8566782011-05-11 12:24:59 +0000745
746 window->device = pdn;
747 window->prop = direct64;
748 spin_lock(&direct_window_list_lock);
749 list_add(&window->list, &direct_window_list);
750 spin_unlock(&direct_window_list_lock);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000751 }
752
Milton Millerc8566782011-05-11 12:24:59 +0000753 return 0;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000754}
Milton Millerc8566782011-05-11 12:24:59 +0000755machine_arch_initcall(pseries, find_existing_ddw_windows);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000756
Milton Millerb73a6352011-05-11 12:25:00 +0000757static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000758 struct ddw_query_response *query)
759{
760 struct device_node *dn;
761 struct pci_dn *pcidn;
762 u32 cfg_addr;
763 u64 buid;
764 int ret;
765
766 /*
767 * Get the config address and phb buid of the PE window.
768 * Rely on eeh to retrieve this for us.
769 * Retrieve them from the pci device, not the node with the
770 * dma-window property
771 */
772 dn = pci_device_to_OF_node(dev);
773 pcidn = PCI_DN(dn);
774 cfg_addr = pcidn->eeh_config_addr;
775 if (pcidn->eeh_pe_config_addr)
776 cfg_addr = pcidn->eeh_pe_config_addr;
777 buid = pcidn->phb->buid;
Milton Millerb73a6352011-05-11 12:25:00 +0000778 ret = rtas_call(ddw_avail[0], 3, 5, (u32 *)query,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000779 cfg_addr, BUID_HI(buid), BUID_LO(buid));
780 dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
Milton Millerb73a6352011-05-11 12:25:00 +0000781 " returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000782 BUID_LO(buid), ret);
783 return ret;
784}
785
Milton Millerb73a6352011-05-11 12:25:00 +0000786static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000787 struct ddw_create_response *create, int page_shift,
788 int window_shift)
789{
790 struct device_node *dn;
791 struct pci_dn *pcidn;
792 u32 cfg_addr;
793 u64 buid;
794 int ret;
795
796 /*
797 * Get the config address and phb buid of the PE window.
798 * Rely on eeh to retrieve this for us.
799 * Retrieve them from the pci device, not the node with the
800 * dma-window property
801 */
802 dn = pci_device_to_OF_node(dev);
803 pcidn = PCI_DN(dn);
804 cfg_addr = pcidn->eeh_config_addr;
805 if (pcidn->eeh_pe_config_addr)
806 cfg_addr = pcidn->eeh_pe_config_addr;
807 buid = pcidn->phb->buid;
808
809 do {
810 /* extra outputs are LIOBN and dma-addr (hi, lo) */
Milton Millerb73a6352011-05-11 12:25:00 +0000811 ret = rtas_call(ddw_avail[1], 5, 4, (u32 *)create, cfg_addr,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000812 BUID_HI(buid), BUID_LO(buid), page_shift, window_shift);
813 } while (rtas_busy_delay(ret));
814 dev_info(&dev->dev,
815 "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
Milton Millerb73a6352011-05-11 12:25:00 +0000816 "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000817 cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
818 window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
819
820 return ret;
821}
822
823/*
824 * If the PE supports dynamic dma windows, and there is space for a table
825 * that can map all pages in a linear offset, then setup such a table,
826 * and record the dma-offset in the struct device.
827 *
828 * dev: the pci device we are checking
829 * pdn: the parent pe node with the ibm,dma_window property
830 * Future: also check if we can remap the base window for our base page size
831 *
832 * returns the dma offset for use by dma_set_mask
833 */
834static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
835{
836 int len, ret;
837 struct ddw_query_response query;
838 struct ddw_create_response create;
839 int page_shift;
840 u64 dma_addr, max_addr;
841 struct device_node *dn;
Milton Millerb73a6352011-05-11 12:25:00 +0000842 const u32 *uninitialized_var(ddw_avail);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000843 struct direct_window *window;
Nishanth Aravamudan76730332011-05-06 13:27:30 +0000844 struct property *win64;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000845 struct dynamic_dma_window_prop *ddwprop;
846
847 mutex_lock(&direct_window_init_mutex);
848
Milton Millerb73a6352011-05-11 12:25:00 +0000849 dma_addr = find_existing_ddw(pdn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000850 if (dma_addr != 0)
851 goto out_unlock;
852
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000853 /*
854 * the ibm,ddw-applicable property holds the tokens for:
855 * ibm,query-pe-dma-window
856 * ibm,create-pe-dma-window
857 * ibm,remove-pe-dma-window
858 * for the given node in that order.
859 * the property is actually in the parent, not the PE
860 */
Milton Millerb73a6352011-05-11 12:25:00 +0000861 ddw_avail = of_get_property(pdn, "ibm,ddw-applicable", &len);
862 if (!ddw_avail || len < 3 * sizeof(u32))
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000863 goto out_unlock;
864
865 /*
866 * Query if there is a second window of size to map the
867 * whole partition. Query returns number of windows, largest
868 * block assigned to PE (partition endpoint), and two bitmasks
869 * of page sizes: supported and supported for migrate-dma.
870 */
871 dn = pci_device_to_OF_node(dev);
Milton Millerb73a6352011-05-11 12:25:00 +0000872 ret = query_ddw(dev, ddw_avail, &query);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000873 if (ret != 0)
874 goto out_unlock;
875
876 if (query.windows_available == 0) {
877 /*
878 * no additional windows are available for this device.
879 * We might be able to reallocate the existing window,
880 * trading in for a larger page size.
881 */
882 dev_dbg(&dev->dev, "no free dynamic windows");
883 goto out_unlock;
884 }
885 if (query.page_size & 4) {
886 page_shift = 24; /* 16MB */
887 } else if (query.page_size & 2) {
888 page_shift = 16; /* 64kB */
889 } else if (query.page_size & 1) {
890 page_shift = 12; /* 4kB */
891 } else {
892 dev_dbg(&dev->dev, "no supported direct page size in mask %x",
893 query.page_size);
894 goto out_unlock;
895 }
896 /* verify the window * number of ptes will map the partition */
897 /* check largest block * page size > max memory hotplug addr */
898 max_addr = memory_hotplug_max();
899 if (query.largest_available_block < (max_addr >> page_shift)) {
900 dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u "
901 "%llu-sized pages\n", max_addr, query.largest_available_block,
902 1ULL << page_shift);
903 goto out_unlock;
904 }
905 len = order_base_2(max_addr);
906 win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
907 if (!win64) {
908 dev_info(&dev->dev,
909 "couldn't allocate property for 64bit dma window\n");
910 goto out_unlock;
911 }
912 win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
913 win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
Nishanth Aravamudan76730332011-05-06 13:27:30 +0000914 win64->length = sizeof(*ddwprop);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000915 if (!win64->name || !win64->value) {
916 dev_info(&dev->dev,
917 "couldn't allocate property name and value\n");
918 goto out_free_prop;
919 }
920
Milton Millerb73a6352011-05-11 12:25:00 +0000921 ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000922 if (ret != 0)
923 goto out_free_prop;
924
925 ddwprop->liobn = cpu_to_be32(create.liobn);
926 ddwprop->dma_base = cpu_to_be64(of_read_number(&create.addr_hi, 2));
927 ddwprop->tce_shift = cpu_to_be32(page_shift);
928 ddwprop->window_shift = cpu_to_be32(len);
929
930 dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n",
931 create.liobn, dn->full_name);
932
933 window = kzalloc(sizeof(*window), GFP_KERNEL);
934 if (!window)
935 goto out_clear_window;
936
937 ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
938 win64->value, tce_setrange_multi_pSeriesLP_walk);
939 if (ret) {
940 dev_info(&dev->dev, "failed to map direct window for %s: %d\n",
941 dn->full_name, ret);
942 goto out_clear_window;
943 }
944
945 ret = prom_add_property(pdn, win64);
946 if (ret) {
947 dev_err(&dev->dev, "unable to add dma window property for %s: %d",
948 pdn->full_name, ret);
949 goto out_clear_window;
950 }
951
952 window->device = pdn;
953 window->prop = ddwprop;
954 spin_lock(&direct_window_list_lock);
955 list_add(&window->list, &direct_window_list);
956 spin_unlock(&direct_window_list_lock);
957
958 dma_addr = of_read_number(&create.addr_hi, 2);
959 goto out_unlock;
960
961out_clear_window:
962 remove_ddw(pdn);
963
964out_free_prop:
965 kfree(win64->name);
966 kfree(win64->value);
967 kfree(win64);
968
969out_unlock:
970 mutex_unlock(&direct_window_init_mutex);
971 return dma_addr;
972}
973
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100974static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct device_node *pdn, *dn;
977 struct iommu_table *tbl;
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000978 const void *dma_window = NULL;
Paul Mackerras16353172005-09-06 13:17:54 +1000979 struct pci_dn *pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000981 pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* dev setup for LPAR is a little tricky, since the device tree might
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300984 * contain the dma-window properties per-device and not necessarily
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 * for the bus. So we need to search upwards in the tree until we
986 * either hit a dma-window property, OR find a parent with a table
987 * already allocated.
988 */
989 dn = pci_device_to_OF_node(dev);
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000990 pr_debug(" node is %s\n", dn->full_name);
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100991
linase07102d2005-12-05 19:37:35 -0600992 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
Paul Mackerras16353172005-09-06 13:17:54 +1000993 pdn = pdn->parent) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000994 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (dma_window)
996 break;
997 }
998
Linas Vepstas650f7b32007-04-11 06:11:23 +1000999 if (!pdn || !PCI_DN(pdn)) {
1000 printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
1001 "no DMA window found for pci dev=%s dn=%s\n",
1002 pci_name(dev), dn? dn->full_name : "<null>");
1003 return;
1004 }
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001005 pr_debug(" parent is %s\n", pdn->full_name);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001006
linase07102d2005-12-05 19:37:35 -06001007 pci = PCI_DN(pdn);
Paul Mackerras16353172005-09-06 13:17:54 +10001008 if (!pci->iommu_table) {
Anton Blanchard7aa241f2010-08-11 16:42:48 +00001009 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Anton Blanchardca1588e2006-06-10 20:58:08 +10001010 pci->phb->node);
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +11001011 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
Anton Blanchardca1588e2006-06-10 20:58:08 +10001012 pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001013 pr_debug(" created table: %p\n", pci->iommu_table);
Michael Neulingde113212007-05-10 15:16:27 +10001014 } else {
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001015 pr_debug(" found DMA window, table: %p\n", pci->iommu_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
Becky Bruce738ef422009-09-21 08:26:35 +00001018 set_iommu_table_base(&dev->dev, pci->iommu_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001020
1021static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask)
1022{
1023 bool ddw_enabled = false;
1024 struct device_node *pdn, *dn;
1025 struct pci_dev *pdev;
1026 const void *dma_window = NULL;
1027 u64 dma_offset;
1028
Milton Miller64ac8222011-05-11 12:24:57 +00001029 if (!dev->dma_mask)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001030 return -EIO;
1031
Milton Miller64ac8222011-05-11 12:24:57 +00001032 if (!dev_is_pci(dev))
1033 goto check_mask;
1034
Nishanth Aravamudaneb0dd412011-05-09 12:58:03 +00001035 pdev = to_pci_dev(dev);
1036
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001037 /* only attempt to use a new window if 64-bit DMA is requested */
1038 if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) {
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001039 dn = pci_device_to_OF_node(pdev);
1040 dev_dbg(dev, "node is %s\n", dn->full_name);
1041
1042 /*
1043 * the device tree might contain the dma-window properties
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001044 * per-device and not necessarily for the bus. So we need to
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001045 * search upwards in the tree until we either hit a dma-window
1046 * property, OR find a parent with a table already allocated.
1047 */
1048 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
1049 pdn = pdn->parent) {
1050 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1051 if (dma_window)
1052 break;
1053 }
1054 if (pdn && PCI_DN(pdn)) {
1055 dma_offset = enable_ddw(pdev, pdn);
1056 if (dma_offset != 0) {
1057 dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset);
1058 set_dma_offset(dev, dma_offset);
1059 set_dma_ops(dev, &dma_direct_ops);
1060 ddw_enabled = true;
1061 }
1062 }
1063 }
1064
Milton Miller64ac8222011-05-11 12:24:57 +00001065 /* fall back on iommu ops, restore table pointer with ops */
1066 if (!ddw_enabled && get_dma_ops(dev) != &dma_iommu_ops) {
1067 dev_info(dev, "Restoring 32-bit DMA via iommu\n");
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001068 set_dma_ops(dev, &dma_iommu_ops);
Nishanth Aravamudaneb0dd412011-05-09 12:58:03 +00001069 pci_dma_dev_setup_pSeriesLP(pdev);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001070 }
1071
Milton Miller64ac8222011-05-11 12:24:57 +00001072check_mask:
1073 if (!dma_supported(dev, dma_mask))
1074 return -EIO;
1075
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001076 *dev->dma_mask = dma_mask;
1077 return 0;
1078}
1079
Milton Miller6a5c7be2011-06-24 09:05:22 +00001080static u64 dma_get_required_mask_pSeriesLP(struct device *dev)
1081{
1082 if (!dev->dma_mask)
1083 return 0;
1084
1085 if (!disable_ddw && dev_is_pci(dev)) {
1086 struct pci_dev *pdev = to_pci_dev(dev);
1087 struct device_node *dn;
1088
1089 dn = pci_device_to_OF_node(pdev);
1090
1091 /* search upwards for ibm,dma-window */
1092 for (; dn && PCI_DN(dn) && !PCI_DN(dn)->iommu_table;
1093 dn = dn->parent)
1094 if (of_get_property(dn, "ibm,dma-window", NULL))
1095 break;
1096 /* if there is a ibm,ddw-applicable property require 64 bits */
1097 if (dn && PCI_DN(dn) &&
1098 of_get_property(dn, "ibm,ddw-applicable", NULL))
1099 return DMA_BIT_MASK(64);
1100 }
1101
1102 return dma_iommu_get_required_mask(dev);
1103}
1104
Stephen Rothwellbed59272007-03-04 17:04:44 +11001105#else /* CONFIG_PCI */
1106#define pci_dma_bus_setup_pSeries NULL
1107#define pci_dma_dev_setup_pSeries NULL
1108#define pci_dma_bus_setup_pSeriesLP NULL
1109#define pci_dma_dev_setup_pSeriesLP NULL
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001110#define dma_set_mask_pSeriesLP NULL
Milton Miller6a5c7be2011-06-24 09:05:22 +00001111#define dma_get_required_mask_pSeriesLP NULL
Stephen Rothwellbed59272007-03-04 17:04:44 +11001112#endif /* !CONFIG_PCI */
1113
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001114static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
1115 void *data)
1116{
1117 struct direct_window *window;
1118 struct memory_notify *arg = data;
1119 int ret = 0;
1120
1121 switch (action) {
1122 case MEM_GOING_ONLINE:
1123 spin_lock(&direct_window_list_lock);
1124 list_for_each_entry(window, &direct_window_list, list) {
1125 ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
1126 arg->nr_pages, window->prop);
1127 /* XXX log error */
1128 }
1129 spin_unlock(&direct_window_list_lock);
1130 break;
1131 case MEM_CANCEL_ONLINE:
1132 case MEM_OFFLINE:
1133 spin_lock(&direct_window_list_lock);
1134 list_for_each_entry(window, &direct_window_list, list) {
1135 ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
1136 arg->nr_pages, window->prop);
1137 /* XXX log error */
1138 }
1139 spin_unlock(&direct_window_list_lock);
1140 break;
1141 default:
1142 break;
1143 }
1144 if (ret && action != MEM_CANCEL_ONLINE)
1145 return NOTIFY_BAD;
1146
1147 return NOTIFY_OK;
1148}
1149
1150static struct notifier_block iommu_mem_nb = {
1151 .notifier_call = iommu_mem_notifier,
1152};
1153
Stephen Rothwellbed59272007-03-04 17:04:44 +11001154static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
1155{
1156 int err = NOTIFY_OK;
1157 struct device_node *np = node;
1158 struct pci_dn *pci = PCI_DN(np);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001159 struct direct_window *window;
Stephen Rothwellbed59272007-03-04 17:04:44 +11001160
1161 switch (action) {
1162 case PSERIES_RECONFIG_REMOVE:
Nishanth Aravamudan7372cfb2010-10-26 17:35:13 +00001163 if (pci && pci->iommu_table)
Stephen Rothwell68d315f2007-12-06 13:39:19 +11001164 iommu_free_table(pci->iommu_table, np->full_name);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001165
1166 spin_lock(&direct_window_list_lock);
1167 list_for_each_entry(window, &direct_window_list, list) {
1168 if (window->device == np) {
1169 list_del(&window->list);
1170 kfree(window);
1171 break;
1172 }
1173 }
1174 spin_unlock(&direct_window_list_lock);
1175
1176 /*
1177 * Because the notifier runs after isolation of the
1178 * slot, we are guaranteed any DMA window has already
1179 * been revoked and the TCEs have been marked invalid,
1180 * so we don't need a call to remove_ddw(np). However,
1181 * if an additional notifier action is added before the
1182 * isolate call, we should update this code for
1183 * completeness with such a call.
1184 */
Stephen Rothwellbed59272007-03-04 17:04:44 +11001185 break;
1186 default:
1187 err = NOTIFY_DONE;
1188 break;
1189 }
1190 return err;
1191}
1192
1193static struct notifier_block iommu_reconfig_nb = {
1194 .notifier_call = iommu_reconfig_notifier,
1195};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/* These are called very early. */
1198void iommu_init_early_pSeries(void)
1199{
Nishanth Aravamudana8daac82010-10-18 07:27:03 +00001200 if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Michael Ellerman57cfb812006-03-21 20:45:59 +11001203 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Stephen Rothwell1ababe12005-08-03 14:35:25 +10001204 if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 ppc_md.tce_build = tce_buildmulti_pSeriesLP;
1206 ppc_md.tce_free = tce_freemulti_pSeriesLP;
1207 } else {
1208 ppc_md.tce_build = tce_build_pSeriesLP;
1209 ppc_md.tce_free = tce_free_pSeriesLP;
1210 }
Haren Myneni5f508672006-06-22 23:35:10 -07001211 ppc_md.tce_get = tce_get_pSeriesLP;
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001212 ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
1213 ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001214 ppc_md.dma_set_mask = dma_set_mask_pSeriesLP;
Milton Miller6a5c7be2011-06-24 09:05:22 +00001215 ppc_md.dma_get_required_mask = dma_get_required_mask_pSeriesLP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 } else {
1217 ppc_md.tce_build = tce_build_pSeries;
1218 ppc_md.tce_free = tce_free_pSeries;
Haren Myneni5f508672006-06-22 23:35:10 -07001219 ppc_md.tce_get = tce_get_pseries;
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001220 ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeries;
1221 ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223
1224
1225 pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001226 register_memory_notifier(&iommu_mem_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Stephen Rothwell98747772007-03-04 16:58:39 +11001228 set_pci_dma_ops(&dma_iommu_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Will Schmidt4e89a2d2010-09-28 15:33:12 +00001231static int __init disable_multitce(char *str)
1232{
1233 if (strcmp(str, "off") == 0 &&
1234 firmware_has_feature(FW_FEATURE_LPAR) &&
1235 firmware_has_feature(FW_FEATURE_MULTITCE)) {
1236 printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
1237 ppc_md.tce_build = tce_build_pSeriesLP;
1238 ppc_md.tce_free = tce_free_pSeriesLP;
1239 powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
1240 }
1241 return 1;
1242}
1243
1244__setup("multitce=", disable_multitce);