blob: e150093d417a3355a6b0ba729f1d2c31aa7f5371 [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>
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
Milton Miller8d3d5892011-06-29 20:58:33 +000054static void tce_invalidate_pSeries_sw(struct iommu_table *tbl,
55 u64 *startp, u64 *endp)
56{
57 u64 __iomem *invalidate = (u64 __iomem *)tbl->it_index;
58 unsigned long start, end, inc;
59
60 start = __pa(startp);
61 end = __pa(endp);
62 inc = L1_CACHE_BYTES; /* invalidate a cacheline of TCEs at a time */
63
64 /* If this is non-zero, change the format. We shift the
65 * address and or in the magic from the device tree. */
66 if (tbl->it_busno) {
67 start <<= 12;
68 end <<= 12;
69 inc <<= 12;
70 start |= tbl->it_busno;
71 end |= tbl->it_busno;
72 }
73
74 end |= inc - 1; /* round up end to be different than start */
75
76 mb(); /* Make sure TCEs in memory are written */
77 while (start <= end) {
78 out_be64(invalidate, start);
79 start += inc;
80 }
81}
82
Robert Jennings6490c492008-07-24 04:31:16 +100083static int tce_build_pSeries(struct iommu_table *tbl, long index,
Olof Johanssonbc97ce92006-04-28 22:51:59 -050084 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +100085 enum dma_data_direction direction,
86 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Olof Johanssonbc97ce92006-04-28 22:51:59 -050088 u64 proto_tce;
Milton Miller8d3d5892011-06-29 20:58:33 +000089 u64 *tcep, *tces;
Olof Johanssonbc97ce92006-04-28 22:51:59 -050090 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Olof Johanssonbc97ce92006-04-28 22:51:59 -050092 proto_tce = TCE_PCI_READ; // Read allowed
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -050095 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Milton Miller8d3d5892011-06-29 20:58:33 +000097 tces = tcep = ((u64 *)tbl->it_base) + index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 while (npages--) {
Yinghai Lu95f72d12010-07-12 14:36:09 +1000100 /* can't move this out since we might cross MEMBLOCK boundary */
Michael Ellerman474e3d52012-07-25 21:19:57 +0000101 rpn = __pa(uaddr) >> TCE_SHIFT;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500102 *tcep = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Olof Johanssond0035c622005-09-20 13:46:44 +1000104 uaddr += TCE_PAGE_SIZE;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500105 tcep++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Milton Miller8d3d5892011-06-29 20:58:33 +0000107
Michael Neulingbc6dc752012-06-26 21:26:37 +0000108 if (tbl->it_type & TCE_PCI_SWINV_CREATE)
Milton Miller8d3d5892011-06-29 20:58:33 +0000109 tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
Robert Jennings6490c492008-07-24 04:31:16 +1000110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113
114static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
115{
Milton Miller8d3d5892011-06-29 20:58:33 +0000116 u64 *tcep, *tces;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Milton Miller8d3d5892011-06-29 20:58:33 +0000118 tces = tcep = ((u64 *)tbl->it_base) + index;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500119
120 while (npages--)
121 *(tcep++) = 0;
Milton Miller8d3d5892011-06-29 20:58:33 +0000122
Michael Neulingbc6dc752012-06-26 21:26:37 +0000123 if (tbl->it_type & TCE_PCI_SWINV_FREE)
Milton Miller8d3d5892011-06-29 20:58:33 +0000124 tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Haren Myneni5f508672006-06-22 23:35:10 -0700127static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
128{
129 u64 *tcep;
130
Haren Myneni5f508672006-06-22 23:35:10 -0700131 tcep = ((u64 *)tbl->it_base) + index;
132
133 return *tcep;
134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Robert Jennings6490c492008-07-24 04:31:16 +1000136static void tce_free_pSeriesLP(struct iommu_table*, long, long);
137static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
138
139static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000141 enum dma_data_direction direction,
142 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Robert Jennings6490c492008-07-24 04:31:16 +1000144 u64 rc = 0;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500145 u64 proto_tce, tce;
146 u64 rpn;
Robert Jennings6490c492008-07-24 04:31:16 +1000147 int ret = 0;
148 long tcenum_start = tcenum, npages_start = npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Michael Ellerman474e3d52012-07-25 21:19:57 +0000150 rpn = __pa(uaddr) >> TCE_SHIFT;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500151 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500153 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500156 tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
157 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
158
Robert Jennings6490c492008-07-24 04:31:16 +1000159 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
160 ret = (int)rc;
161 tce_free_pSeriesLP(tbl, tcenum_start,
162 (npages_start - (npages + 1)));
163 break;
164 }
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000167 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
168 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
169 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
170 printk("\ttce val = 0x%llx\n", tce );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 show_stack(current, (unsigned long *)__get_SP());
172 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 tcenum++;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500175 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Robert Jennings6490c492008-07-24 04:31:16 +1000177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Nishanth Aravamudana8daac82010-10-18 07:27:03 +0000180static DEFINE_PER_CPU(u64 *, tce_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Robert Jennings6490c492008-07-24 04:31:16 +1000182static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000184 enum dma_data_direction direction,
185 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Robert Jennings6490c492008-07-24 04:31:16 +1000187 u64 rc = 0;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500188 u64 proto_tce;
189 u64 *tcep;
190 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 long l, limit;
Robert Jennings6490c492008-07-24 04:31:16 +1000192 long tcenum_start = tcenum, npages_start = npages;
193 int ret = 0;
Anton Blanchardc1703e82012-06-03 19:42:13 +0000194 unsigned long flags;
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
Anton Blanchardc1703e82012-06-03 19:42:13 +0000201 local_irq_save(flags); /* to protect tcep and the page behind it */
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 tcep = __get_cpu_var(tce_page);
204
205 /* This is safe to do since interrupts are off when we're called
206 * from iommu_alloc{,_sg}()
207 */
208 if (!tcep) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500209 tcep = (u64 *)__get_free_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* If allocation fails, fall back to the loop implementation */
Michael Ellerman541b2752008-05-08 14:27:23 +1000211 if (!tcep) {
Anton Blanchardc1703e82012-06-03 19:42:13 +0000212 local_irq_restore(flags);
Robert Jennings6490c492008-07-24 04:31:16 +1000213 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000214 direction, attrs);
Michael Ellerman541b2752008-05-08 14:27:23 +1000215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 __get_cpu_var(tce_page) = tcep;
217 }
218
Michael Ellerman474e3d52012-07-25 21:19:57 +0000219 rpn = __pa(uaddr) >> TCE_SHIFT;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500220 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500222 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* We can map max one pageful of TCEs at a time */
225 do {
226 /*
227 * Set up the page with TCE data, looping through and setting
228 * the values.
229 */
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500230 limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 for (l = 0; l < limit; l++) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500233 tcep[l] = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
234 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
237 rc = plpar_tce_put_indirect((u64)tbl->it_index,
238 (u64)tcenum << 12,
Michael Ellerman474e3d52012-07-25 21:19:57 +0000239 (u64)__pa(tcep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 limit);
241
242 npages -= limit;
243 tcenum += limit;
244 } while (npages > 0 && !rc);
245
Anton Blanchardc1703e82012-06-03 19:42:13 +0000246 local_irq_restore(flags);
247
Robert Jennings6490c492008-07-24 04:31:16 +1000248 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
249 ret = (int)rc;
250 tce_freemulti_pSeriesLP(tbl, tcenum_start,
251 (npages_start - (npages + limit)));
252 return ret;
253 }
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000256 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
257 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
258 printk("\tnpages = 0x%llx\n", (u64)npages);
259 printk("\ttce[0] val = 0x%llx\n", tcep[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 show_stack(current, (unsigned long *)__get_SP());
261 }
Robert Jennings6490c492008-07-24 04:31:16 +1000262 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
266{
267 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500270 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000273 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
274 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
275 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 show_stack(current, (unsigned long *)__get_SP());
277 }
278
279 tcenum++;
280 }
281}
282
283
284static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
285{
286 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500288 rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (rc && printk_ratelimit()) {
291 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
Ingo Molnarfe333322009-01-06 14:26:03 +0000292 printk("\trc = %lld\n", rc);
293 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
294 printk("\tnpages = 0x%llx\n", (u64)npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 show_stack(current, (unsigned long *)__get_SP());
296 }
297}
298
Haren Myneni5f508672006-06-22 23:35:10 -0700299static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
300{
301 u64 rc;
302 unsigned long tce_ret;
303
Haren Myneni5f508672006-06-22 23:35:10 -0700304 rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
305
306 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000307 printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc);
308 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
309 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
Haren Myneni5f508672006-06-22 23:35:10 -0700310 show_stack(current, (unsigned long *)__get_SP());
311 }
312
313 return tce_ret;
314}
315
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300316/* this is compatible with cells for the device tree property */
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000317struct dynamic_dma_window_prop {
318 __be32 liobn; /* tce table number */
319 __be64 dma_base; /* address hi,lo */
320 __be32 tce_shift; /* ilog2(tce_page_size) */
321 __be32 window_shift; /* ilog2(tce_window_size) */
322};
323
324struct direct_window {
325 struct device_node *device;
326 const struct dynamic_dma_window_prop *prop;
327 struct list_head list;
328};
329
330/* Dynamic DMA Window support */
331struct ddw_query_response {
332 u32 windows_available;
333 u32 largest_available_block;
334 u32 page_size;
335 u32 migration_capable;
336};
337
338struct ddw_create_response {
339 u32 liobn;
340 u32 addr_hi;
341 u32 addr_lo;
342};
343
344static LIST_HEAD(direct_window_list);
345/* prevents races between memory on/offline and window creation */
346static DEFINE_SPINLOCK(direct_window_list_lock);
347/* protects initializing window twice for same device */
348static DEFINE_MUTEX(direct_window_init_mutex);
349#define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
350
351static int tce_clearrange_multi_pSeriesLP(unsigned long start_pfn,
352 unsigned long num_pfn, const void *arg)
353{
354 const struct dynamic_dma_window_prop *maprange = arg;
355 int rc;
356 u64 tce_size, num_tce, dma_offset, next;
357 u32 tce_shift;
358 long limit;
359
360 tce_shift = be32_to_cpu(maprange->tce_shift);
361 tce_size = 1ULL << tce_shift;
362 next = start_pfn << PAGE_SHIFT;
363 num_tce = num_pfn << PAGE_SHIFT;
364
365 /* round back to the beginning of the tce page size */
366 num_tce += next & (tce_size - 1);
367 next &= ~(tce_size - 1);
368
369 /* covert to number of tces */
370 num_tce |= tce_size - 1;
371 num_tce >>= tce_shift;
372
373 do {
374 /*
375 * Set up the page with TCE data, looping through and setting
376 * the values.
377 */
378 limit = min_t(long, num_tce, 512);
379 dma_offset = next + be64_to_cpu(maprange->dma_base);
380
381 rc = plpar_tce_stuff((u64)be32_to_cpu(maprange->liobn),
382 dma_offset,
383 0, limit);
384 num_tce -= limit;
385 } while (num_tce > 0 && !rc);
386
387 return rc;
388}
389
390static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
391 unsigned long num_pfn, const void *arg)
392{
393 const struct dynamic_dma_window_prop *maprange = arg;
394 u64 *tcep, tce_size, num_tce, dma_offset, next, proto_tce, liobn;
395 u32 tce_shift;
396 u64 rc = 0;
397 long l, limit;
398
399 local_irq_disable(); /* to protect tcep and the page behind it */
400 tcep = __get_cpu_var(tce_page);
401
402 if (!tcep) {
403 tcep = (u64 *)__get_free_page(GFP_ATOMIC);
404 if (!tcep) {
405 local_irq_enable();
406 return -ENOMEM;
407 }
408 __get_cpu_var(tce_page) = tcep;
409 }
410
411 proto_tce = TCE_PCI_READ | TCE_PCI_WRITE;
412
413 liobn = (u64)be32_to_cpu(maprange->liobn);
414 tce_shift = be32_to_cpu(maprange->tce_shift);
415 tce_size = 1ULL << tce_shift;
416 next = start_pfn << PAGE_SHIFT;
417 num_tce = num_pfn << PAGE_SHIFT;
418
419 /* round back to the beginning of the tce page size */
420 num_tce += next & (tce_size - 1);
421 next &= ~(tce_size - 1);
422
423 /* covert to number of tces */
424 num_tce |= tce_size - 1;
425 num_tce >>= tce_shift;
426
427 /* We can map max one pageful of TCEs at a time */
428 do {
429 /*
430 * Set up the page with TCE data, looping through and setting
431 * the values.
432 */
433 limit = min_t(long, num_tce, 4096/TCE_ENTRY_SIZE);
434 dma_offset = next + be64_to_cpu(maprange->dma_base);
435
436 for (l = 0; l < limit; l++) {
437 tcep[l] = proto_tce | next;
438 next += tce_size;
439 }
440
441 rc = plpar_tce_put_indirect(liobn,
442 dma_offset,
Michael Ellerman474e3d52012-07-25 21:19:57 +0000443 (u64)__pa(tcep),
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000444 limit);
445
446 num_tce -= limit;
447 } while (num_tce > 0 && !rc);
448
449 /* error cleanup: caller will clear whole range */
450
451 local_irq_enable();
452 return rc;
453}
454
455static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,
456 unsigned long num_pfn, void *arg)
457{
458 return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
459}
460
461
Stephen Rothwellbed59272007-03-04 17:04:44 +1100462#ifdef CONFIG_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static void iommu_table_setparms(struct pci_controller *phb,
464 struct device_node *dn,
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500465 struct iommu_table *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct device_node *node;
Milton Miller8d3d5892011-06-29 20:58:33 +0000468 const unsigned long *basep, *sw_inval;
Nathan Lynch9938c472006-10-04 22:28:00 -0500469 const u32 *sizep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100471 node = phb->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000473 basep = of_get_property(node, "linux,tce-base", NULL);
474 sizep = of_get_property(node, "linux,tce-size", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (basep == NULL || sizep == NULL) {
476 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
477 "missing tce entries !\n", dn->full_name);
478 return;
479 }
480
481 tbl->it_base = (unsigned long)__va(*basep);
Haren Myneni5f508672006-06-22 23:35:10 -0700482
Milton Miller62a8bd62008-10-22 15:39:04 -0500483 if (!is_kdump_kernel())
Mohan Kumar M54622f12008-10-21 17:38:10 +0000484 memset((void *)tbl->it_base, 0, *sizep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 tbl->it_busno = phb->bus->number;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* Units of tce entries */
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100489 tbl->it_offset = phb->dma_window_base_cur >> IOMMU_PAGE_SHIFT;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* Test if we are going over 2GB of DMA space */
Olof Johansson3c2822c2005-09-21 09:55:31 -0700492 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
493 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500494 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johansson3c2822c2005-09-21 09:55:31 -0700495 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 phb->dma_window_base_cur += phb->dma_window_size;
498
499 /* Set the tce table size - measured in entries */
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100500 tbl->it_size = phb->dma_window_size >> IOMMU_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 tbl->it_index = 0;
503 tbl->it_blocksize = 16;
504 tbl->it_type = TCE_PCI;
Milton Miller8d3d5892011-06-29 20:58:33 +0000505
506 sw_inval = of_get_property(node, "linux,tce-sw-invalidate-info", NULL);
507 if (sw_inval) {
508 /*
509 * This property contains information on how to
510 * invalidate the TCE entry. The first property is
511 * the base MMIO address used to invalidate entries.
512 * The second property tells us the format of the TCE
513 * invalidate (whether it needs to be shifted) and
514 * some magic routing info to add to our invalidate
515 * command.
516 */
517 tbl->it_index = (unsigned long) ioremap(sw_inval[0], 8);
518 tbl->it_busno = sw_inval[1]; /* overload this with magic */
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000519 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
Milton Miller8d3d5892011-06-29 20:58:33 +0000520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523/*
524 * iommu_table_setparms_lpar
525 *
526 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
528static void iommu_table_setparms_lpar(struct pci_controller *phb,
529 struct device_node *dn,
530 struct iommu_table *tbl,
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +1100531 const void *dma_window)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000533 unsigned long offset, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000535 of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
536
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +1100537 tbl->it_busno = phb->bus->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 tbl->it_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 tbl->it_blocksize = 16;
540 tbl->it_type = TCE_PCI;
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100541 tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
542 tbl->it_size = size >> IOMMU_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100545static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Olof Johansson3c2822c2005-09-21 09:55:31 -0700547 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct iommu_table *tbl;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700549 struct device_node *isa_dn, *isa_dn_orig;
550 struct device_node *tmp;
551 struct pci_dn *pci;
552 int children;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Olof Johansson3c2822c2005-09-21 09:55:31 -0700554 dn = pci_bus_to_OF_node(bus);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100555
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000556 pr_debug("pci_dma_bus_setup_pSeries: setting up bus %s\n", dn->full_name);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700557
558 if (bus->self) {
559 /* This is not a root bus, any setup will be done for the
560 * device-side of the bridge in iommu_dev_setup_pSeries().
561 */
562 return;
563 }
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100564 pci = PCI_DN(dn);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700565
566 /* Check if the ISA bus on the system is under
567 * this PHB.
568 */
569 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
570
571 while (isa_dn && isa_dn != dn)
572 isa_dn = isa_dn->parent;
573
574 if (isa_dn_orig)
575 of_node_put(isa_dn_orig);
576
Anton Blanchardd3c58fb2006-06-20 18:00:30 +1000577 /* Count number of direct PCI children of the PHB. */
Olof Johansson3c2822c2005-09-21 09:55:31 -0700578 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
Anton Blanchardd3c58fb2006-06-20 18:00:30 +1000579 children++;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700580
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000581 pr_debug("Children: %d\n", children);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700582
583 /* Calculate amount of DMA window per slot. Each window must be
584 * a power of two (due to pci_alloc_consistent requirements).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
Olof Johansson3c2822c2005-09-21 09:55:31 -0700586 * Keep 256MB aside for PHBs with ISA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
588
Olof Johansson3c2822c2005-09-21 09:55:31 -0700589 if (!isa_dn) {
590 /* No ISA/IDE - just set window size and return */
591 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Olof Johansson3c2822c2005-09-21 09:55:31 -0700593 while (pci->phb->dma_window_size * children > 0x80000000ul)
594 pci->phb->dma_window_size >>= 1;
Stephen Rothwell41febbc2009-06-02 18:21:30 +0000595 pr_debug("No ISA/IDE, window size is 0x%llx\n",
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000596 pci->phb->dma_window_size);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700597 pci->phb->dma_window_base_cur = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Olof Johansson3c2822c2005-09-21 09:55:31 -0700599 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Olof Johansson3c2822c2005-09-21 09:55:31 -0700601
602 /* If we have ISA, then we probably have an IDE
603 * controller too. Allocate a 128MB table but
604 * skip the first 128MB to avoid stepping on ISA
605 * space.
606 */
607 pci->phb->dma_window_size = 0x8000000ul;
608 pci->phb->dma_window_base_cur = 0x8000000ul;
609
Anton Blanchard7aa241f2010-08-11 16:42:48 +0000610 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Anton Blanchardca1588e2006-06-10 20:58:08 +1000611 pci->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700612
613 iommu_table_setparms(pci->phb, dn, tbl);
Anton Blanchardca1588e2006-06-10 20:58:08 +1000614 pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700615
616 /* Divide the rest (1.75GB) among the children */
617 pci->phb->dma_window_size = 0x80000000ul;
618 while (pci->phb->dma_window_size * children > 0x70000000ul)
619 pci->phb->dma_window_size >>= 1;
620
Stephen Rothwell41febbc2009-06-02 18:21:30 +0000621 pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100625static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct iommu_table *tbl;
628 struct device_node *dn, *pdn;
Paul Mackerras16353172005-09-06 13:17:54 +1000629 struct pci_dn *ppci;
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000630 const void *dma_window = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 dn = pci_bus_to_OF_node(bus);
633
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000634 pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %s\n",
635 dn->full_name);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* Find nearest ibm,dma-window, walking up the device tree */
638 for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000639 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (dma_window != NULL)
641 break;
642 }
643
644 if (dma_window == NULL) {
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000645 pr_debug(" no ibm,dma-window property !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return;
647 }
648
linase07102d2005-12-05 19:37:35 -0600649 ppci = PCI_DN(pdn);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100650
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000651 pr_debug(" parent is %s, iommu_table: 0x%p\n",
652 pdn->full_name, ppci->iommu_table);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100653
Paul Mackerras16353172005-09-06 13:17:54 +1000654 if (!ppci->iommu_table) {
Anton Blanchard7aa241f2010-08-11 16:42:48 +0000655 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Anton Blanchardca1588e2006-06-10 20:58:08 +1000656 ppci->phb->node);
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +1100657 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
Anton Blanchardca1588e2006-06-10 20:58:08 +1000658 ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node);
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000659 pr_debug(" created table: %p\n", ppci->iommu_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100664static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100666 struct device_node *dn;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700667 struct iommu_table *tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000669 pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
Olof Johansson3c2822c2005-09-21 09:55:31 -0700670
Grant Likely58f9b0b2010-04-13 16:12:56 -0700671 dn = dev->dev.of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Olof Johansson3c2822c2005-09-21 09:55:31 -0700673 /* If we're the direct child of a root bus, then we need to allocate
674 * an iommu table ourselves. The bus setup code should have setup
675 * the window sizes already.
676 */
677 if (!dev->bus->self) {
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100678 struct pci_controller *phb = PCI_DN(dn)->phb;
679
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000680 pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
Anton Blanchard7aa241f2010-08-11 16:42:48 +0000681 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100682 phb->node);
683 iommu_table_setparms(phb, dn, tbl);
Linas Vepstas77319252007-01-10 19:16:29 -0600684 PCI_DN(dn)->iommu_table = iommu_init_table(tbl, phb->node);
Becky Bruce738ef422009-09-21 08:26:35 +0000685 set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700686 return;
687 }
688
689 /* If this device is further down the bus tree, search upwards until
690 * an already allocated iommu table is found and use that.
691 */
692
linase07102d2005-12-05 19:37:35 -0600693 while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 dn = dn->parent;
695
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100696 if (dn && PCI_DN(dn))
Becky Bruce738ef422009-09-21 08:26:35 +0000697 set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100698 else
699 printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
700 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000703static int __read_mostly disable_ddw;
704
705static int __init disable_ddw_setup(char *str)
706{
707 disable_ddw = 1;
708 printk(KERN_INFO "ppc iommu: disabling ddw.\n");
709
710 return 0;
711}
712
713early_param("disable_ddw", disable_ddw_setup);
714
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +0000715static inline void __remove_ddw(struct device_node *np, const u32 *ddw_avail, u64 liobn)
716{
717 int ret;
718
719 ret = rtas_call(ddw_avail[2], 1, 1, NULL, liobn);
720 if (ret)
721 pr_warning("%s: failed to remove DMA window: rtas returned "
722 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
723 np->full_name, ret, ddw_avail[2], liobn);
724 else
725 pr_debug("%s: successfully removed DMA window: rtas returned "
726 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
727 np->full_name, ret, ddw_avail[2], liobn);
728}
729
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000730static void remove_ddw(struct device_node *np)
731{
732 struct dynamic_dma_window_prop *dwp;
733 struct property *win64;
Milton Millerb73a6352011-05-11 12:25:00 +0000734 const u32 *ddw_avail;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000735 u64 liobn;
736 int len, ret;
737
Milton Millerb73a6352011-05-11 12:25:00 +0000738 ddw_avail = of_get_property(np, "ibm,ddw-applicable", &len);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000739 win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
Milton Miller2573f682011-05-11 12:24:58 +0000740 if (!win64)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000741 return;
742
Milton Millerb73a6352011-05-11 12:25:00 +0000743 if (!ddw_avail || len < 3 * sizeof(u32) || win64->length < sizeof(*dwp))
Milton Miller2573f682011-05-11 12:24:58 +0000744 goto delprop;
745
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000746 dwp = win64->value;
747 liobn = (u64)be32_to_cpu(dwp->liobn);
748
749 /* clear the whole window, note the arg is in kernel pages */
750 ret = tce_clearrange_multi_pSeriesLP(0,
751 1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp);
752 if (ret)
753 pr_warning("%s failed to clear tces in window.\n",
754 np->full_name);
755 else
756 pr_debug("%s successfully cleared tces in window.\n",
757 np->full_name);
758
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +0000759 __remove_ddw(np, ddw_avail, liobn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000760
Milton Miller2573f682011-05-11 12:24:58 +0000761delprop:
Milton Millerc8566782011-05-11 12:24:59 +0000762 ret = prom_remove_property(np, win64);
Milton Miller2573f682011-05-11 12:24:58 +0000763 if (ret)
Milton Millerc8566782011-05-11 12:24:59 +0000764 pr_warning("%s: failed to remove direct window property: %d\n",
Milton Miller2573f682011-05-11 12:24:58 +0000765 np->full_name, ret);
766}
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000767
Milton Millerb73a6352011-05-11 12:25:00 +0000768static u64 find_existing_ddw(struct device_node *pdn)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000769{
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000770 struct direct_window *window;
771 const struct dynamic_dma_window_prop *direct64;
772 u64 dma_addr = 0;
773
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000774 spin_lock(&direct_window_list_lock);
775 /* check if we already created a window and dupe that config if so */
776 list_for_each_entry(window, &direct_window_list, list) {
777 if (window->device == pdn) {
778 direct64 = window->prop;
779 dma_addr = direct64->dma_base;
780 break;
781 }
782 }
783 spin_unlock(&direct_window_list_lock);
784
785 return dma_addr;
786}
787
Milton Millerc8566782011-05-11 12:24:59 +0000788static int find_existing_ddw_windows(void)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000789{
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000790 int len;
Milton Millerc8566782011-05-11 12:24:59 +0000791 struct device_node *pdn;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000792 struct direct_window *window;
793 const struct dynamic_dma_window_prop *direct64;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000794
Milton Millerc8566782011-05-11 12:24:59 +0000795 if (!firmware_has_feature(FW_FEATURE_LPAR))
796 return 0;
797
798 for_each_node_with_property(pdn, DIRECT64_PROPNAME) {
799 direct64 = of_get_property(pdn, DIRECT64_PROPNAME, &len);
800 if (!direct64)
801 continue;
802
803 window = kzalloc(sizeof(*window), GFP_KERNEL);
804 if (!window || len < sizeof(struct dynamic_dma_window_prop)) {
805 kfree(window);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000806 remove_ddw(pdn);
Milton Millerc8566782011-05-11 12:24:59 +0000807 continue;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000808 }
Milton Millerc8566782011-05-11 12:24:59 +0000809
810 window->device = pdn;
811 window->prop = direct64;
812 spin_lock(&direct_window_list_lock);
813 list_add(&window->list, &direct_window_list);
814 spin_unlock(&direct_window_list_lock);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000815 }
816
Milton Millerc8566782011-05-11 12:24:59 +0000817 return 0;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000818}
Milton Millerc8566782011-05-11 12:24:59 +0000819machine_arch_initcall(pseries, find_existing_ddw_windows);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000820
Milton Millerb73a6352011-05-11 12:25:00 +0000821static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000822 struct ddw_query_response *query)
823{
Gavin Shan39baadb2012-03-20 21:30:28 +0000824 struct eeh_dev *edev;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000825 u32 cfg_addr;
826 u64 buid;
827 int ret;
828
829 /*
830 * Get the config address and phb buid of the PE window.
831 * Rely on eeh to retrieve this for us.
832 * Retrieve them from the pci device, not the node with the
833 * dma-window property
834 */
Gavin Shan39baadb2012-03-20 21:30:28 +0000835 edev = pci_dev_to_eeh_dev(dev);
836 cfg_addr = edev->config_addr;
837 if (edev->pe_config_addr)
838 cfg_addr = edev->pe_config_addr;
839 buid = edev->phb->buid;
840
Milton Millerb73a6352011-05-11 12:25:00 +0000841 ret = rtas_call(ddw_avail[0], 3, 5, (u32 *)query,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000842 cfg_addr, BUID_HI(buid), BUID_LO(buid));
843 dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
Milton Millerb73a6352011-05-11 12:25:00 +0000844 " returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000845 BUID_LO(buid), ret);
846 return ret;
847}
848
Milton Millerb73a6352011-05-11 12:25:00 +0000849static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000850 struct ddw_create_response *create, int page_shift,
851 int window_shift)
852{
Gavin Shan39baadb2012-03-20 21:30:28 +0000853 struct eeh_dev *edev;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000854 u32 cfg_addr;
855 u64 buid;
856 int ret;
857
858 /*
859 * Get the config address and phb buid of the PE window.
860 * Rely on eeh to retrieve this for us.
861 * Retrieve them from the pci device, not the node with the
862 * dma-window property
863 */
Gavin Shan39baadb2012-03-20 21:30:28 +0000864 edev = pci_dev_to_eeh_dev(dev);
865 cfg_addr = edev->config_addr;
866 if (edev->pe_config_addr)
867 cfg_addr = edev->pe_config_addr;
868 buid = edev->phb->buid;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000869
870 do {
871 /* extra outputs are LIOBN and dma-addr (hi, lo) */
Milton Millerb73a6352011-05-11 12:25:00 +0000872 ret = rtas_call(ddw_avail[1], 5, 4, (u32 *)create, cfg_addr,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000873 BUID_HI(buid), BUID_LO(buid), page_shift, window_shift);
874 } while (rtas_busy_delay(ret));
875 dev_info(&dev->dev,
876 "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
Milton Millerb73a6352011-05-11 12:25:00 +0000877 "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000878 cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
879 window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
880
881 return ret;
882}
883
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +0000884static void restore_default_window(struct pci_dev *dev,
885 u32 ddw_restore_token, unsigned long liobn)
886{
887 struct eeh_dev *edev;
888 u32 cfg_addr;
889 u64 buid;
890 int ret;
891
892 /*
893 * Get the config address and phb buid of the PE window.
894 * Rely on eeh to retrieve this for us.
895 * Retrieve them from the pci device, not the node with the
896 * dma-window property
897 */
898 edev = pci_dev_to_eeh_dev(dev);
899 cfg_addr = edev->config_addr;
900 if (edev->pe_config_addr)
901 cfg_addr = edev->pe_config_addr;
902 buid = edev->phb->buid;
903
904 do {
905 ret = rtas_call(ddw_restore_token, 3, 1, NULL, cfg_addr,
906 BUID_HI(buid), BUID_LO(buid));
907 } while (rtas_busy_delay(ret));
908 dev_info(&dev->dev,
909 "ibm,reset-pe-dma-windows(%x) %x %x %x returned %d\n",
910 ddw_restore_token, cfg_addr, BUID_HI(buid), BUID_LO(buid), ret);
911}
912
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000913/*
914 * If the PE supports dynamic dma windows, and there is space for a table
915 * that can map all pages in a linear offset, then setup such a table,
916 * and record the dma-offset in the struct device.
917 *
918 * dev: the pci device we are checking
919 * pdn: the parent pe node with the ibm,dma_window property
920 * Future: also check if we can remap the base window for our base page size
921 *
922 * returns the dma offset for use by dma_set_mask
923 */
924static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
925{
926 int len, ret;
927 struct ddw_query_response query;
928 struct ddw_create_response create;
929 int page_shift;
930 u64 dma_addr, max_addr;
931 struct device_node *dn;
Milton Millerb73a6352011-05-11 12:25:00 +0000932 const u32 *uninitialized_var(ddw_avail);
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +0000933 const u32 *uninitialized_var(ddw_extensions);
934 u32 ddw_restore_token = 0;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000935 struct direct_window *window;
Nishanth Aravamudan76730332011-05-06 13:27:30 +0000936 struct property *win64;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000937 struct dynamic_dma_window_prop *ddwprop;
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +0000938 const void *dma_window = NULL;
939 unsigned long liobn, offset, size;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000940
941 mutex_lock(&direct_window_init_mutex);
942
Milton Millerb73a6352011-05-11 12:25:00 +0000943 dma_addr = find_existing_ddw(pdn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000944 if (dma_addr != 0)
945 goto out_unlock;
946
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000947 /*
948 * the ibm,ddw-applicable property holds the tokens for:
949 * ibm,query-pe-dma-window
950 * ibm,create-pe-dma-window
951 * ibm,remove-pe-dma-window
952 * for the given node in that order.
953 * the property is actually in the parent, not the PE
954 */
Milton Millerb73a6352011-05-11 12:25:00 +0000955 ddw_avail = of_get_property(pdn, "ibm,ddw-applicable", &len);
956 if (!ddw_avail || len < 3 * sizeof(u32))
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000957 goto out_unlock;
958
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +0000959 /*
960 * the extensions property is only required to exist in certain
961 * levels of firmware and later
962 * the ibm,ddw-extensions property is a list with the first
963 * element containing the number of extensions and each
964 * subsequent entry is a value corresponding to that extension
965 */
966 ddw_extensions = of_get_property(pdn, "ibm,ddw-extensions", &len);
967 if (ddw_extensions) {
968 /*
969 * each new defined extension length should be added to
970 * the top of the switch so the "earlier" entries also
971 * get picked up
972 */
973 switch (ddw_extensions[0]) {
974 /* ibm,reset-pe-dma-windows */
975 case 1:
976 ddw_restore_token = ddw_extensions[1];
977 break;
978 }
979 }
980
981 /*
982 * Only remove the existing DMA window if we can restore back to
983 * the default state. Removing the existing window maximizes the
984 * resources available to firmware for dynamic window creation.
985 */
986 if (ddw_restore_token) {
987 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
988 of_parse_dma_window(pdn, dma_window, &liobn, &offset, &size);
989 __remove_ddw(pdn, ddw_avail, liobn);
990 }
991
992 /*
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000993 * Query if there is a second window of size to map the
994 * whole partition. Query returns number of windows, largest
995 * block assigned to PE (partition endpoint), and two bitmasks
996 * of page sizes: supported and supported for migrate-dma.
997 */
998 dn = pci_device_to_OF_node(dev);
Milton Millerb73a6352011-05-11 12:25:00 +0000999 ret = query_ddw(dev, ddw_avail, &query);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001000 if (ret != 0)
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +00001001 goto out_restore_window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001002
1003 if (query.windows_available == 0) {
1004 /*
1005 * no additional windows are available for this device.
1006 * We might be able to reallocate the existing window,
1007 * trading in for a larger page size.
1008 */
1009 dev_dbg(&dev->dev, "no free dynamic windows");
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +00001010 goto out_restore_window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001011 }
1012 if (query.page_size & 4) {
1013 page_shift = 24; /* 16MB */
1014 } else if (query.page_size & 2) {
1015 page_shift = 16; /* 64kB */
1016 } else if (query.page_size & 1) {
1017 page_shift = 12; /* 4kB */
1018 } else {
1019 dev_dbg(&dev->dev, "no supported direct page size in mask %x",
1020 query.page_size);
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +00001021 goto out_restore_window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001022 }
1023 /* verify the window * number of ptes will map the partition */
1024 /* check largest block * page size > max memory hotplug addr */
1025 max_addr = memory_hotplug_max();
1026 if (query.largest_available_block < (max_addr >> page_shift)) {
1027 dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u "
1028 "%llu-sized pages\n", max_addr, query.largest_available_block,
1029 1ULL << page_shift);
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +00001030 goto out_restore_window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001031 }
1032 len = order_base_2(max_addr);
1033 win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
1034 if (!win64) {
1035 dev_info(&dev->dev,
1036 "couldn't allocate property for 64bit dma window\n");
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +00001037 goto out_restore_window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001038 }
1039 win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
1040 win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
Nishanth Aravamudan76730332011-05-06 13:27:30 +00001041 win64->length = sizeof(*ddwprop);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001042 if (!win64->name || !win64->value) {
1043 dev_info(&dev->dev,
1044 "couldn't allocate property name and value\n");
1045 goto out_free_prop;
1046 }
1047
Milton Millerb73a6352011-05-11 12:25:00 +00001048 ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001049 if (ret != 0)
1050 goto out_free_prop;
1051
1052 ddwprop->liobn = cpu_to_be32(create.liobn);
1053 ddwprop->dma_base = cpu_to_be64(of_read_number(&create.addr_hi, 2));
1054 ddwprop->tce_shift = cpu_to_be32(page_shift);
1055 ddwprop->window_shift = cpu_to_be32(len);
1056
1057 dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n",
1058 create.liobn, dn->full_name);
1059
1060 window = kzalloc(sizeof(*window), GFP_KERNEL);
1061 if (!window)
1062 goto out_clear_window;
1063
1064 ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
1065 win64->value, tce_setrange_multi_pSeriesLP_walk);
1066 if (ret) {
1067 dev_info(&dev->dev, "failed to map direct window for %s: %d\n",
1068 dn->full_name, ret);
Julia Lawall7a190812011-08-08 01:18:00 +00001069 goto out_free_window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001070 }
1071
1072 ret = prom_add_property(pdn, win64);
1073 if (ret) {
1074 dev_err(&dev->dev, "unable to add dma window property for %s: %d",
1075 pdn->full_name, ret);
Julia Lawall7a190812011-08-08 01:18:00 +00001076 goto out_free_window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001077 }
1078
1079 window->device = pdn;
1080 window->prop = ddwprop;
1081 spin_lock(&direct_window_list_lock);
1082 list_add(&window->list, &direct_window_list);
1083 spin_unlock(&direct_window_list_lock);
1084
1085 dma_addr = of_read_number(&create.addr_hi, 2);
1086 goto out_unlock;
1087
Julia Lawall7a190812011-08-08 01:18:00 +00001088out_free_window:
1089 kfree(window);
1090
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001091out_clear_window:
1092 remove_ddw(pdn);
1093
1094out_free_prop:
1095 kfree(win64->name);
1096 kfree(win64->value);
1097 kfree(win64);
1098
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +00001099out_restore_window:
1100 if (ddw_restore_token)
1101 restore_default_window(dev, ddw_restore_token, liobn);
1102
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001103out_unlock:
1104 mutex_unlock(&direct_window_init_mutex);
1105 return dma_addr;
1106}
1107
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001108static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 struct device_node *pdn, *dn;
1111 struct iommu_table *tbl;
Jeremy Kerr954a46e2006-07-12 15:39:43 +10001112 const void *dma_window = NULL;
Paul Mackerras16353172005-09-06 13:17:54 +10001113 struct pci_dn *pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001115 pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* dev setup for LPAR is a little tricky, since the device tree might
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001118 * contain the dma-window properties per-device and not necessarily
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 * for the bus. So we need to search upwards in the tree until we
1120 * either hit a dma-window property, OR find a parent with a table
1121 * already allocated.
1122 */
1123 dn = pci_device_to_OF_node(dev);
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001124 pr_debug(" node is %s\n", dn->full_name);
Linas Vepstas5d2efba2006-10-30 16:15:59 +11001125
linase07102d2005-12-05 19:37:35 -06001126 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
Paul Mackerras16353172005-09-06 13:17:54 +10001127 pdn = pdn->parent) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +10001128 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (dma_window)
1130 break;
1131 }
1132
Linas Vepstas650f7b32007-04-11 06:11:23 +10001133 if (!pdn || !PCI_DN(pdn)) {
1134 printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
1135 "no DMA window found for pci dev=%s dn=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001136 pci_name(dev), of_node_full_name(dn));
Linas Vepstas650f7b32007-04-11 06:11:23 +10001137 return;
1138 }
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001139 pr_debug(" parent is %s\n", pdn->full_name);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001140
linase07102d2005-12-05 19:37:35 -06001141 pci = PCI_DN(pdn);
Paul Mackerras16353172005-09-06 13:17:54 +10001142 if (!pci->iommu_table) {
Anton Blanchard7aa241f2010-08-11 16:42:48 +00001143 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
Anton Blanchardca1588e2006-06-10 20:58:08 +10001144 pci->phb->node);
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +11001145 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
Anton Blanchardca1588e2006-06-10 20:58:08 +10001146 pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001147 pr_debug(" created table: %p\n", pci->iommu_table);
Michael Neulingde113212007-05-10 15:16:27 +10001148 } else {
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001149 pr_debug(" found DMA window, table: %p\n", pci->iommu_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
Becky Bruce738ef422009-09-21 08:26:35 +00001152 set_iommu_table_base(&dev->dev, pci->iommu_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001154
1155static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask)
1156{
1157 bool ddw_enabled = false;
1158 struct device_node *pdn, *dn;
1159 struct pci_dev *pdev;
1160 const void *dma_window = NULL;
1161 u64 dma_offset;
1162
Milton Miller64ac8222011-05-11 12:24:57 +00001163 if (!dev->dma_mask)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001164 return -EIO;
1165
Milton Miller64ac8222011-05-11 12:24:57 +00001166 if (!dev_is_pci(dev))
1167 goto check_mask;
1168
Nishanth Aravamudaneb0dd412011-05-09 12:58:03 +00001169 pdev = to_pci_dev(dev);
1170
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001171 /* only attempt to use a new window if 64-bit DMA is requested */
1172 if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) {
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001173 dn = pci_device_to_OF_node(pdev);
1174 dev_dbg(dev, "node is %s\n", dn->full_name);
1175
1176 /*
1177 * the device tree might contain the dma-window properties
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001178 * per-device and not necessarily for the bus. So we need to
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001179 * search upwards in the tree until we either hit a dma-window
1180 * property, OR find a parent with a table already allocated.
1181 */
1182 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
1183 pdn = pdn->parent) {
1184 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1185 if (dma_window)
1186 break;
1187 }
1188 if (pdn && PCI_DN(pdn)) {
1189 dma_offset = enable_ddw(pdev, pdn);
1190 if (dma_offset != 0) {
1191 dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset);
1192 set_dma_offset(dev, dma_offset);
1193 set_dma_ops(dev, &dma_direct_ops);
1194 ddw_enabled = true;
1195 }
1196 }
1197 }
1198
Milton Miller64ac8222011-05-11 12:24:57 +00001199 /* fall back on iommu ops, restore table pointer with ops */
1200 if (!ddw_enabled && get_dma_ops(dev) != &dma_iommu_ops) {
1201 dev_info(dev, "Restoring 32-bit DMA via iommu\n");
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001202 set_dma_ops(dev, &dma_iommu_ops);
Nishanth Aravamudaneb0dd412011-05-09 12:58:03 +00001203 pci_dma_dev_setup_pSeriesLP(pdev);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001204 }
1205
Milton Miller64ac8222011-05-11 12:24:57 +00001206check_mask:
1207 if (!dma_supported(dev, dma_mask))
1208 return -EIO;
1209
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001210 *dev->dma_mask = dma_mask;
1211 return 0;
1212}
1213
Milton Miller6a5c7be2011-06-24 09:05:22 +00001214static u64 dma_get_required_mask_pSeriesLP(struct device *dev)
1215{
1216 if (!dev->dma_mask)
1217 return 0;
1218
1219 if (!disable_ddw && dev_is_pci(dev)) {
1220 struct pci_dev *pdev = to_pci_dev(dev);
1221 struct device_node *dn;
1222
1223 dn = pci_device_to_OF_node(pdev);
1224
1225 /* search upwards for ibm,dma-window */
1226 for (; dn && PCI_DN(dn) && !PCI_DN(dn)->iommu_table;
1227 dn = dn->parent)
1228 if (of_get_property(dn, "ibm,dma-window", NULL))
1229 break;
1230 /* if there is a ibm,ddw-applicable property require 64 bits */
1231 if (dn && PCI_DN(dn) &&
1232 of_get_property(dn, "ibm,ddw-applicable", NULL))
1233 return DMA_BIT_MASK(64);
1234 }
1235
Milton Millerd24f9c62011-06-24 09:05:24 +00001236 return dma_iommu_ops.get_required_mask(dev);
Milton Miller6a5c7be2011-06-24 09:05:22 +00001237}
1238
Stephen Rothwellbed59272007-03-04 17:04:44 +11001239#else /* CONFIG_PCI */
1240#define pci_dma_bus_setup_pSeries NULL
1241#define pci_dma_dev_setup_pSeries NULL
1242#define pci_dma_bus_setup_pSeriesLP NULL
1243#define pci_dma_dev_setup_pSeriesLP NULL
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001244#define dma_set_mask_pSeriesLP NULL
Milton Miller6a5c7be2011-06-24 09:05:22 +00001245#define dma_get_required_mask_pSeriesLP NULL
Stephen Rothwellbed59272007-03-04 17:04:44 +11001246#endif /* !CONFIG_PCI */
1247
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001248static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
1249 void *data)
1250{
1251 struct direct_window *window;
1252 struct memory_notify *arg = data;
1253 int ret = 0;
1254
1255 switch (action) {
1256 case MEM_GOING_ONLINE:
1257 spin_lock(&direct_window_list_lock);
1258 list_for_each_entry(window, &direct_window_list, list) {
1259 ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
1260 arg->nr_pages, window->prop);
1261 /* XXX log error */
1262 }
1263 spin_unlock(&direct_window_list_lock);
1264 break;
1265 case MEM_CANCEL_ONLINE:
1266 case MEM_OFFLINE:
1267 spin_lock(&direct_window_list_lock);
1268 list_for_each_entry(window, &direct_window_list, list) {
1269 ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
1270 arg->nr_pages, window->prop);
1271 /* XXX log error */
1272 }
1273 spin_unlock(&direct_window_list_lock);
1274 break;
1275 default:
1276 break;
1277 }
1278 if (ret && action != MEM_CANCEL_ONLINE)
1279 return NOTIFY_BAD;
1280
1281 return NOTIFY_OK;
1282}
1283
1284static struct notifier_block iommu_mem_nb = {
1285 .notifier_call = iommu_mem_notifier,
1286};
1287
Stephen Rothwellbed59272007-03-04 17:04:44 +11001288static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
1289{
1290 int err = NOTIFY_OK;
1291 struct device_node *np = node;
1292 struct pci_dn *pci = PCI_DN(np);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001293 struct direct_window *window;
Stephen Rothwellbed59272007-03-04 17:04:44 +11001294
1295 switch (action) {
1296 case PSERIES_RECONFIG_REMOVE:
Nishanth Aravamudan7372cfb2010-10-26 17:35:13 +00001297 if (pci && pci->iommu_table)
Stephen Rothwell68d315f2007-12-06 13:39:19 +11001298 iommu_free_table(pci->iommu_table, np->full_name);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001299
1300 spin_lock(&direct_window_list_lock);
1301 list_for_each_entry(window, &direct_window_list, list) {
1302 if (window->device == np) {
1303 list_del(&window->list);
1304 kfree(window);
1305 break;
1306 }
1307 }
1308 spin_unlock(&direct_window_list_lock);
1309
1310 /*
1311 * Because the notifier runs after isolation of the
1312 * slot, we are guaranteed any DMA window has already
1313 * been revoked and the TCEs have been marked invalid,
1314 * so we don't need a call to remove_ddw(np). However,
1315 * if an additional notifier action is added before the
1316 * isolate call, we should update this code for
1317 * completeness with such a call.
1318 */
Stephen Rothwellbed59272007-03-04 17:04:44 +11001319 break;
1320 default:
1321 err = NOTIFY_DONE;
1322 break;
1323 }
1324 return err;
1325}
1326
1327static struct notifier_block iommu_reconfig_nb = {
1328 .notifier_call = iommu_reconfig_notifier,
1329};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331/* These are called very early. */
1332void iommu_init_early_pSeries(void)
1333{
Nishanth Aravamudana8daac82010-10-18 07:27:03 +00001334 if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Michael Ellerman57cfb812006-03-21 20:45:59 +11001337 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Stephen Rothwell1ababe12005-08-03 14:35:25 +10001338 if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 ppc_md.tce_build = tce_buildmulti_pSeriesLP;
1340 ppc_md.tce_free = tce_freemulti_pSeriesLP;
1341 } else {
1342 ppc_md.tce_build = tce_build_pSeriesLP;
1343 ppc_md.tce_free = tce_free_pSeriesLP;
1344 }
Haren Myneni5f508672006-06-22 23:35:10 -07001345 ppc_md.tce_get = tce_get_pSeriesLP;
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001346 ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
1347 ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001348 ppc_md.dma_set_mask = dma_set_mask_pSeriesLP;
Milton Miller6a5c7be2011-06-24 09:05:22 +00001349 ppc_md.dma_get_required_mask = dma_get_required_mask_pSeriesLP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 } else {
1351 ppc_md.tce_build = tce_build_pSeries;
1352 ppc_md.tce_free = tce_free_pSeries;
Haren Myneni5f508672006-06-22 23:35:10 -07001353 ppc_md.tce_get = tce_get_pseries;
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001354 ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeries;
1355 ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357
1358
1359 pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001360 register_memory_notifier(&iommu_mem_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Stephen Rothwell98747772007-03-04 16:58:39 +11001362 set_pci_dma_ops(&dma_iommu_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Will Schmidt4e89a2d2010-09-28 15:33:12 +00001365static int __init disable_multitce(char *str)
1366{
1367 if (strcmp(str, "off") == 0 &&
1368 firmware_has_feature(FW_FEATURE_LPAR) &&
1369 firmware_has_feature(FW_FEATURE_MULTITCE)) {
1370 printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
1371 ppc_md.tce_build = tce_build_pSeriesLP;
1372 ppc_md.tce_free = tce_free_pSeriesLP;
1373 powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
1374 }
1375 return 1;
1376}
1377
1378__setup("multitce=", disable_multitce);