blob: 0024e451bb36f8ef7665835180d34a5e2807679b [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>
Michael Ellermanbeacc6d2012-07-25 21:20:03 +000031#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/spinlock.h>
33#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>
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +000038#include <linux/of.h>
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +100039#include <linux/iommu.h>
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +100040#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
42#include <asm/prom.h>
43#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/iommu.h>
45#include <asm/pci-bridge.h>
46#include <asm/machdep.h>
Stephen Rothwell1ababe12005-08-03 14:35:25 +100047#include <asm/firmware.h>
Olof Johanssonc707ffc2005-09-20 13:45:41 +100048#include <asm/tce.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100049#include <asm/ppc-pci.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110050#include <asm/udbg.h>
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +000051#include <asm/mmzone.h>
Deepthi Dharwar212bebb2013-08-22 15:23:52 +053052#include <asm/plpar_wrappers.h>
Michael Ellermana1218722005-11-03 15:33:31 +110053
Daniel Axtens38ae9ec2015-03-31 16:00:50 +110054#include "pseries.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +100056static struct iommu_table_group *iommu_pseries_alloc_group(int node)
57{
58 struct iommu_table_group *table_group = NULL;
59 struct iommu_table *tbl = NULL;
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +100060 struct iommu_table_group_link *tgl = NULL;
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +100061
62 table_group = kzalloc_node(sizeof(struct iommu_table_group), GFP_KERNEL,
63 node);
64 if (!table_group)
65 goto fail_exit;
66
67 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, node);
68 if (!tbl)
69 goto fail_exit;
70
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +100071 tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,
72 node);
73 if (!tgl)
74 goto fail_exit;
75
76 INIT_LIST_HEAD_RCU(&tbl->it_group_list);
77 tgl->table_group = table_group;
78 list_add_rcu(&tgl->next, &tbl->it_group_list);
79
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +100080 table_group->tables[0] = tbl;
81
82 return table_group;
83
84fail_exit:
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +100085 kfree(tgl);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +100086 kfree(table_group);
87 kfree(tbl);
88
89 return NULL;
90}
91
92static void iommu_pseries_free_group(struct iommu_table_group *table_group,
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +100093 const char *node_name)
94{
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +100095 struct iommu_table *tbl;
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +100096#ifdef CONFIG_IOMMU_API
97 struct iommu_table_group_link *tgl;
98#endif
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +100099
100 if (!table_group)
101 return;
102
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000103 tbl = table_group->tables[0];
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +1000104#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000105 tgl = list_first_entry_or_null(&tbl->it_group_list,
106 struct iommu_table_group_link, next);
107
108 WARN_ON_ONCE(!tgl);
109 if (tgl) {
110 list_del_rcu(&tgl->next);
111 kfree(tgl);
112 }
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +1000113 if (table_group->group) {
114 iommu_group_put(table_group->group);
115 BUG_ON(table_group->group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +1000116 }
117#endif
118 iommu_free_table(tbl, node_name);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +1000119
120 kfree(table_group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +1000121}
122
Robert Jennings6490c492008-07-24 04:31:16 +1000123static int tce_build_pSeries(struct iommu_table *tbl, long index,
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500124 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000125 enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700126 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500128 u64 proto_tce;
Anton Blancharddf015602013-10-17 23:21:15 +1100129 __be64 *tcep, *tces;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500130 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500132 proto_tce = TCE_PCI_READ; // Read allowed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500135 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Anton Blancharddf015602013-10-17 23:21:15 +1100137 tces = tcep = ((__be64 *)tbl->it_base) + index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 while (npages--) {
Yinghai Lu95f72d12010-07-12 14:36:09 +1000140 /* can't move this out since we might cross MEMBLOCK boundary */
Michael Ellerman474e3d52012-07-25 21:19:57 +0000141 rpn = __pa(uaddr) >> TCE_SHIFT;
Anton Blancharddf015602013-10-17 23:21:15 +1100142 *tcep = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Olof Johanssond0035c622005-09-20 13:46:44 +1000144 uaddr += TCE_PAGE_SIZE;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500145 tcep++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
Robert Jennings6490c492008-07-24 04:31:16 +1000147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150
151static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
152{
Anton Blancharddf015602013-10-17 23:21:15 +1100153 __be64 *tcep, *tces;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Anton Blancharddf015602013-10-17 23:21:15 +1100155 tces = tcep = ((__be64 *)tbl->it_base) + index;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500156
157 while (npages--)
158 *(tcep++) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Haren Myneni5f508672006-06-22 23:35:10 -0700161static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
162{
Anton Blancharddf015602013-10-17 23:21:15 +1100163 __be64 *tcep;
Haren Myneni5f508672006-06-22 23:35:10 -0700164
Anton Blancharddf015602013-10-17 23:21:15 +1100165 tcep = ((__be64 *)tbl->it_base) + index;
Haren Myneni5f508672006-06-22 23:35:10 -0700166
Anton Blancharddf015602013-10-17 23:21:15 +1100167 return be64_to_cpu(*tcep);
Haren Myneni5f508672006-06-22 23:35:10 -0700168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Robert Jennings6490c492008-07-24 04:31:16 +1000170static void tce_free_pSeriesLP(struct iommu_table*, long, long);
171static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
172
173static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000175 enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700176 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Robert Jennings6490c492008-07-24 04:31:16 +1000178 u64 rc = 0;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500179 u64 proto_tce, tce;
180 u64 rpn;
Robert Jennings6490c492008-07-24 04:31:16 +1000181 int ret = 0;
182 long tcenum_start = tcenum, npages_start = npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Michael Ellerman474e3d52012-07-25 21:19:57 +0000184 rpn = __pa(uaddr) >> TCE_SHIFT;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500185 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500187 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500190 tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
191 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
192
Robert Jennings6490c492008-07-24 04:31:16 +1000193 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
194 ret = (int)rc;
195 tce_free_pSeriesLP(tbl, tcenum_start,
196 (npages_start - (npages + 1)));
197 break;
198 }
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000201 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
202 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
203 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
204 printk("\ttce val = 0x%llx\n", tce );
Anton Blanchard4ff52b42014-10-13 19:41:40 +1100205 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 tcenum++;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500209 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Robert Jennings6490c492008-07-24 04:31:16 +1000211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Anton Blancharddf015602013-10-17 23:21:15 +1100214static DEFINE_PER_CPU(__be64 *, tce_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Robert Jennings6490c492008-07-24 04:31:16 +1000216static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 long npages, unsigned long uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000218 enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700219 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Robert Jennings6490c492008-07-24 04:31:16 +1000221 u64 rc = 0;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500222 u64 proto_tce;
Anton Blancharddf015602013-10-17 23:21:15 +1100223 __be64 *tcep;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500224 u64 rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 long l, limit;
Robert Jennings6490c492008-07-24 04:31:16 +1000226 long tcenum_start = tcenum, npages_start = npages;
227 int ret = 0;
Anton Blanchardc1703e82012-06-03 19:42:13 +0000228 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +1000230 if ((npages == 1) || !firmware_has_feature(FW_FEATURE_MULTITCE)) {
Robert Jennings6490c492008-07-24 04:31:16 +1000231 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
232 direction, attrs);
Michael Ellerman541b2752008-05-08 14:27:23 +1000233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Anton Blanchardc1703e82012-06-03 19:42:13 +0000235 local_irq_save(flags); /* to protect tcep and the page behind it */
236
Christoph Lameter69111ba2014-10-21 15:23:25 -0500237 tcep = __this_cpu_read(tce_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /* This is safe to do since interrupts are off when we're called
240 * from iommu_alloc{,_sg}()
241 */
242 if (!tcep) {
Anton Blancharddf015602013-10-17 23:21:15 +1100243 tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* If allocation fails, fall back to the loop implementation */
Michael Ellerman541b2752008-05-08 14:27:23 +1000245 if (!tcep) {
Anton Blanchardc1703e82012-06-03 19:42:13 +0000246 local_irq_restore(flags);
Robert Jennings6490c492008-07-24 04:31:16 +1000247 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000248 direction, attrs);
Michael Ellerman541b2752008-05-08 14:27:23 +1000249 }
Christoph Lameter69111ba2014-10-21 15:23:25 -0500250 __this_cpu_write(tce_page, tcep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
Michael Ellerman474e3d52012-07-25 21:19:57 +0000253 rpn = __pa(uaddr) >> TCE_SHIFT;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500254 proto_tce = TCE_PCI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500256 proto_tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* We can map max one pageful of TCEs at a time */
259 do {
260 /*
261 * Set up the page with TCE data, looping through and setting
262 * the values.
263 */
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500264 limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 for (l = 0; l < limit; l++) {
Anton Blancharddf015602013-10-17 23:21:15 +1100267 tcep[l] = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500268 rpn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 rc = plpar_tce_put_indirect((u64)tbl->it_index,
272 (u64)tcenum << 12,
Michael Ellerman474e3d52012-07-25 21:19:57 +0000273 (u64)__pa(tcep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 limit);
275
276 npages -= limit;
277 tcenum += limit;
278 } while (npages > 0 && !rc);
279
Anton Blanchardc1703e82012-06-03 19:42:13 +0000280 local_irq_restore(flags);
281
Robert Jennings6490c492008-07-24 04:31:16 +1000282 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
283 ret = (int)rc;
284 tce_freemulti_pSeriesLP(tbl, tcenum_start,
285 (npages_start - (npages + limit)));
286 return ret;
287 }
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000290 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
291 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
292 printk("\tnpages = 0x%llx\n", (u64)npages);
293 printk("\ttce[0] val = 0x%llx\n", tcep[0]);
Anton Blanchard4ff52b42014-10-13 19:41:40 +1100294 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Robert Jennings6490c492008-07-24 04:31:16 +1000296 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
300{
301 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500304 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000307 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
308 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
309 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
Anton Blanchard4ff52b42014-10-13 19:41:40 +1100310 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 tcenum++;
314 }
315}
316
317
318static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
319{
320 u64 rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +1000322 if (!firmware_has_feature(FW_FEATURE_MULTITCE))
323 return tce_free_pSeriesLP(tbl, tcenum, npages);
324
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500325 rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if (rc && printk_ratelimit()) {
328 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
Ingo Molnarfe333322009-01-06 14:26:03 +0000329 printk("\trc = %lld\n", rc);
330 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
331 printk("\tnpages = 0x%llx\n", (u64)npages);
Anton Blanchard4ff52b42014-10-13 19:41:40 +1100332 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334}
335
Haren Myneni5f508672006-06-22 23:35:10 -0700336static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
337{
338 u64 rc;
339 unsigned long tce_ret;
340
Haren Myneni5f508672006-06-22 23:35:10 -0700341 rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
342
343 if (rc && printk_ratelimit()) {
Ingo Molnarfe333322009-01-06 14:26:03 +0000344 printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc);
345 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
346 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
Anton Blanchard4ff52b42014-10-13 19:41:40 +1100347 dump_stack();
Haren Myneni5f508672006-06-22 23:35:10 -0700348 }
349
350 return tce_ret;
351}
352
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300353/* this is compatible with cells for the device tree property */
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000354struct dynamic_dma_window_prop {
355 __be32 liobn; /* tce table number */
356 __be64 dma_base; /* address hi,lo */
357 __be32 tce_shift; /* ilog2(tce_page_size) */
358 __be32 window_shift; /* ilog2(tce_window_size) */
359};
360
361struct direct_window {
362 struct device_node *device;
363 const struct dynamic_dma_window_prop *prop;
364 struct list_head list;
365};
366
367/* Dynamic DMA Window support */
368struct ddw_query_response {
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +1000369 u32 windows_available;
370 u32 largest_available_block;
371 u32 page_size;
372 u32 migration_capable;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000373};
374
375struct ddw_create_response {
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +1000376 u32 liobn;
377 u32 addr_hi;
378 u32 addr_lo;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000379};
380
381static LIST_HEAD(direct_window_list);
382/* prevents races between memory on/offline and window creation */
383static DEFINE_SPINLOCK(direct_window_list_lock);
384/* protects initializing window twice for same device */
385static DEFINE_MUTEX(direct_window_init_mutex);
386#define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
387
388static int tce_clearrange_multi_pSeriesLP(unsigned long start_pfn,
389 unsigned long num_pfn, const void *arg)
390{
391 const struct dynamic_dma_window_prop *maprange = arg;
392 int rc;
393 u64 tce_size, num_tce, dma_offset, next;
394 u32 tce_shift;
395 long limit;
396
397 tce_shift = be32_to_cpu(maprange->tce_shift);
398 tce_size = 1ULL << tce_shift;
399 next = start_pfn << PAGE_SHIFT;
400 num_tce = num_pfn << PAGE_SHIFT;
401
402 /* round back to the beginning of the tce page size */
403 num_tce += next & (tce_size - 1);
404 next &= ~(tce_size - 1);
405
406 /* covert to number of tces */
407 num_tce |= tce_size - 1;
408 num_tce >>= tce_shift;
409
410 do {
411 /*
412 * Set up the page with TCE data, looping through and setting
413 * the values.
414 */
415 limit = min_t(long, num_tce, 512);
416 dma_offset = next + be64_to_cpu(maprange->dma_base);
417
418 rc = plpar_tce_stuff((u64)be32_to_cpu(maprange->liobn),
419 dma_offset,
420 0, limit);
Nishanth Aravamudan22b38292013-01-18 09:16:24 +0000421 next += limit * tce_size;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000422 num_tce -= limit;
423 } while (num_tce > 0 && !rc);
424
425 return rc;
426}
427
428static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
429 unsigned long num_pfn, const void *arg)
430{
431 const struct dynamic_dma_window_prop *maprange = arg;
Anton Blancharddf015602013-10-17 23:21:15 +1100432 u64 tce_size, num_tce, dma_offset, next, proto_tce, liobn;
433 __be64 *tcep;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000434 u32 tce_shift;
435 u64 rc = 0;
436 long l, limit;
437
438 local_irq_disable(); /* to protect tcep and the page behind it */
Christoph Lameter69111ba2014-10-21 15:23:25 -0500439 tcep = __this_cpu_read(tce_page);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000440
441 if (!tcep) {
Anton Blancharddf015602013-10-17 23:21:15 +1100442 tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000443 if (!tcep) {
444 local_irq_enable();
445 return -ENOMEM;
446 }
Christoph Lameter69111ba2014-10-21 15:23:25 -0500447 __this_cpu_write(tce_page, tcep);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000448 }
449
450 proto_tce = TCE_PCI_READ | TCE_PCI_WRITE;
451
452 liobn = (u64)be32_to_cpu(maprange->liobn);
453 tce_shift = be32_to_cpu(maprange->tce_shift);
454 tce_size = 1ULL << tce_shift;
455 next = start_pfn << PAGE_SHIFT;
456 num_tce = num_pfn << PAGE_SHIFT;
457
458 /* round back to the beginning of the tce page size */
459 num_tce += next & (tce_size - 1);
460 next &= ~(tce_size - 1);
461
462 /* covert to number of tces */
463 num_tce |= tce_size - 1;
464 num_tce >>= tce_shift;
465
466 /* We can map max one pageful of TCEs at a time */
467 do {
468 /*
469 * Set up the page with TCE data, looping through and setting
470 * the values.
471 */
472 limit = min_t(long, num_tce, 4096/TCE_ENTRY_SIZE);
473 dma_offset = next + be64_to_cpu(maprange->dma_base);
474
475 for (l = 0; l < limit; l++) {
Anton Blancharddf015602013-10-17 23:21:15 +1100476 tcep[l] = cpu_to_be64(proto_tce | next);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000477 next += tce_size;
478 }
479
480 rc = plpar_tce_put_indirect(liobn,
481 dma_offset,
Michael Ellerman474e3d52012-07-25 21:19:57 +0000482 (u64)__pa(tcep),
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000483 limit);
484
485 num_tce -= limit;
486 } while (num_tce > 0 && !rc);
487
488 /* error cleanup: caller will clear whole range */
489
490 local_irq_enable();
491 return rc;
492}
493
494static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,
495 unsigned long num_pfn, void *arg)
496{
497 return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500static void iommu_table_setparms(struct pci_controller *phb,
501 struct device_node *dn,
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500502 struct iommu_table *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 struct device_node *node;
Benjamin Herrenschmidtb7d6bf42016-07-08 16:37:10 +1000505 const unsigned long *basep;
Nathan Lynch9938c472006-10-04 22:28:00 -0500506 const u32 *sizep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100508 node = phb->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000510 basep = of_get_property(node, "linux,tce-base", NULL);
511 sizep = of_get_property(node, "linux,tce-size", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (basep == NULL || sizep == NULL) {
513 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
514 "missing tce entries !\n", dn->full_name);
515 return;
516 }
517
518 tbl->it_base = (unsigned long)__va(*basep);
Haren Myneni5f508672006-06-22 23:35:10 -0700519
Milton Miller62a8bd62008-10-22 15:39:04 -0500520 if (!is_kdump_kernel())
Mohan Kumar M54622f12008-10-21 17:38:10 +0000521 memset((void *)tbl->it_base, 0, *sizep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 tbl->it_busno = phb->bus->number;
Alistair Popple3a553172013-12-09 18:17:02 +1100524 tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Units of tce entries */
Alistair Popple3a553172013-12-09 18:17:02 +1100527 tbl->it_offset = phb->dma_window_base_cur >> tbl->it_page_shift;
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Test if we are going over 2GB of DMA space */
Olof Johansson3c2822c2005-09-21 09:55:31 -0700530 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
531 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500532 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
Olof Johansson3c2822c2005-09-21 09:55:31 -0700533 }
Olof Johanssonbc97ce92006-04-28 22:51:59 -0500534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 phb->dma_window_base_cur += phb->dma_window_size;
536
537 /* Set the tce table size - measured in entries */
Alistair Popple3a553172013-12-09 18:17:02 +1100538 tbl->it_size = phb->dma_window_size >> tbl->it_page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 tbl->it_index = 0;
541 tbl->it_blocksize = 16;
542 tbl->it_type = TCE_PCI;
543}
544
545/*
546 * iommu_table_setparms_lpar
547 *
548 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
550static void iommu_table_setparms_lpar(struct pci_controller *phb,
551 struct device_node *dn,
552 struct iommu_table *tbl,
Anton Blanchard2083f682013-08-07 02:01:36 +1000553 const __be32 *dma_window)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000555 unsigned long offset, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000557 of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
558
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +1100559 tbl->it_busno = phb->bus->number;
Alistair Popple3a553172013-12-09 18:17:02 +1100560 tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 tbl->it_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 tbl->it_blocksize = 16;
563 tbl->it_type = TCE_PCI;
Alistair Popple3a553172013-12-09 18:17:02 +1100564 tbl->it_offset = offset >> tbl->it_page_shift;
565 tbl->it_size = size >> tbl->it_page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +1000568struct iommu_table_ops iommu_table_pseries_ops = {
569 .set = tce_build_pSeries,
570 .clear = tce_free_pSeries,
571 .get = tce_get_pseries
572};
573
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100574static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Olof Johansson3c2822c2005-09-21 09:55:31 -0700576 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct iommu_table *tbl;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700578 struct device_node *isa_dn, *isa_dn_orig;
579 struct device_node *tmp;
580 struct pci_dn *pci;
581 int children;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Olof Johansson3c2822c2005-09-21 09:55:31 -0700583 dn = pci_bus_to_OF_node(bus);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100584
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000585 pr_debug("pci_dma_bus_setup_pSeries: setting up bus %s\n", dn->full_name);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700586
587 if (bus->self) {
588 /* This is not a root bus, any setup will be done for the
589 * device-side of the bridge in iommu_dev_setup_pSeries().
590 */
591 return;
592 }
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100593 pci = PCI_DN(dn);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700594
595 /* Check if the ISA bus on the system is under
596 * this PHB.
597 */
598 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
599
600 while (isa_dn && isa_dn != dn)
601 isa_dn = isa_dn->parent;
602
Julia Lawall498b6512014-08-08 12:07:47 +0200603 of_node_put(isa_dn_orig);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700604
Anton Blanchardd3c58fb2006-06-20 18:00:30 +1000605 /* Count number of direct PCI children of the PHB. */
Olof Johansson3c2822c2005-09-21 09:55:31 -0700606 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
Anton Blanchardd3c58fb2006-06-20 18:00:30 +1000607 children++;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700608
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000609 pr_debug("Children: %d\n", children);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700610
611 /* Calculate amount of DMA window per slot. Each window must be
612 * a power of two (due to pci_alloc_consistent requirements).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *
Olof Johansson3c2822c2005-09-21 09:55:31 -0700614 * Keep 256MB aside for PHBs with ISA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
616
Olof Johansson3c2822c2005-09-21 09:55:31 -0700617 if (!isa_dn) {
618 /* No ISA/IDE - just set window size and return */
619 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Olof Johansson3c2822c2005-09-21 09:55:31 -0700621 while (pci->phb->dma_window_size * children > 0x80000000ul)
622 pci->phb->dma_window_size >>= 1;
Stephen Rothwell41febbc2009-06-02 18:21:30 +0000623 pr_debug("No ISA/IDE, window size is 0x%llx\n",
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000624 pci->phb->dma_window_size);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700625 pci->phb->dma_window_base_cur = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Olof Johansson3c2822c2005-09-21 09:55:31 -0700627 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Olof Johansson3c2822c2005-09-21 09:55:31 -0700629
630 /* If we have ISA, then we probably have an IDE
631 * controller too. Allocate a 128MB table but
632 * skip the first 128MB to avoid stepping on ISA
633 * space.
634 */
635 pci->phb->dma_window_size = 0x8000000ul;
636 pci->phb->dma_window_base_cur = 0x8000000ul;
637
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +1000638 pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
639 tbl = pci->table_group->tables[0];
Olof Johansson3c2822c2005-09-21 09:55:31 -0700640
641 iommu_table_setparms(pci->phb, dn, tbl);
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +1000642 tbl->it_ops = &iommu_table_pseries_ops;
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +1000643 iommu_init_table(tbl, pci->phb->node);
644 iommu_register_group(pci->table_group, pci_domain_nr(bus), 0);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700645
646 /* Divide the rest (1.75GB) among the children */
647 pci->phb->dma_window_size = 0x80000000ul;
648 while (pci->phb->dma_window_size * children > 0x70000000ul)
649 pci->phb->dma_window_size >>= 1;
650
Stephen Rothwell41febbc2009-06-02 18:21:30 +0000651 pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +1000654struct iommu_table_ops iommu_table_lpar_multi_ops = {
655 .set = tce_buildmulti_pSeriesLP,
656 .clear = tce_freemulti_pSeriesLP,
657 .get = tce_get_pSeriesLP
658};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100660static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 struct iommu_table *tbl;
663 struct device_node *dn, *pdn;
Paul Mackerras16353172005-09-06 13:17:54 +1000664 struct pci_dn *ppci;
Anton Blanchard2083f682013-08-07 02:01:36 +1000665 const __be32 *dma_window = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 dn = pci_bus_to_OF_node(bus);
668
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000669 pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %s\n",
670 dn->full_name);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* Find nearest ibm,dma-window, walking up the device tree */
673 for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000674 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (dma_window != NULL)
676 break;
677 }
678
679 if (dma_window == NULL) {
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000680 pr_debug(" no ibm,dma-window property !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return;
682 }
683
linase07102d2005-12-05 19:37:35 -0600684 ppci = PCI_DN(pdn);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100685
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000686 pr_debug(" parent is %s, iommu_table: 0x%p\n",
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +1000687 pdn->full_name, ppci->table_group);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100688
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +1000689 if (!ppci->table_group) {
690 ppci->table_group = iommu_pseries_alloc_group(ppci->phb->node);
691 tbl = ppci->table_group->tables[0];
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +1100692 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +1000693 tbl->it_ops = &iommu_table_lpar_multi_ops;
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +1000694 iommu_init_table(tbl, ppci->phb->node);
695 iommu_register_group(ppci->table_group,
696 pci_domain_nr(bus), 0);
697 pr_debug(" created table: %p\n", ppci->table_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100702static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100704 struct device_node *dn;
Olof Johansson3c2822c2005-09-21 09:55:31 -0700705 struct iommu_table *tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000707 pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
Olof Johansson3c2822c2005-09-21 09:55:31 -0700708
Grant Likely58f9b0b2010-04-13 16:12:56 -0700709 dn = dev->dev.of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Olof Johansson3c2822c2005-09-21 09:55:31 -0700711 /* If we're the direct child of a root bus, then we need to allocate
712 * an iommu table ourselves. The bus setup code should have setup
713 * the window sizes already.
714 */
715 if (!dev->bus->self) {
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100716 struct pci_controller *phb = PCI_DN(dn)->phb;
717
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000718 pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +1000719 PCI_DN(dn)->table_group = iommu_pseries_alloc_group(phb->node);
720 tbl = PCI_DN(dn)->table_group->tables[0];
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100721 iommu_table_setparms(phb, dn, tbl);
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +1000722 tbl->it_ops = &iommu_table_pseries_ops;
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +1000723 iommu_init_table(tbl, phb->node);
724 iommu_register_group(PCI_DN(dn)->table_group,
725 pci_domain_nr(phb->bus), 0);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +1000726 set_iommu_table_base(&dev->dev, tbl);
727 iommu_add_device(&dev->dev);
Olof Johansson3c2822c2005-09-21 09:55:31 -0700728 return;
729 }
730
731 /* If this device is further down the bus tree, search upwards until
732 * an already allocated iommu table is found and use that.
733 */
734
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +1000735 while (dn && PCI_DN(dn) && PCI_DN(dn)->table_group == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 dn = dn->parent;
737
Alexey Kardashevskiy46170822015-06-05 16:34:54 +1000738 if (dn && PCI_DN(dn)) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +1000739 set_iommu_table_base(&dev->dev,
740 PCI_DN(dn)->table_group->tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +1000741 iommu_add_device(&dev->dev);
742 } else
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100743 printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
744 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000747static int __read_mostly disable_ddw;
748
749static int __init disable_ddw_setup(char *str)
750{
751 disable_ddw = 1;
752 printk(KERN_INFO "ppc iommu: disabling ddw.\n");
753
754 return 0;
755}
756
757early_param("disable_ddw", disable_ddw_setup);
758
Gavin Shan5efbabe2014-08-11 19:16:20 +1000759static void remove_ddw(struct device_node *np, bool remove_prop)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000760{
761 struct dynamic_dma_window_prop *dwp;
762 struct property *win64;
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +1000763 u32 ddw_avail[3];
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000764 u64 liobn;
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +1000765 int ret = 0;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000766
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +1000767 ret = of_property_read_u32_array(np, "ibm,ddw-applicable",
768 &ddw_avail[0], 3);
769
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000770 win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
Milton Miller2573f682011-05-11 12:24:58 +0000771 if (!win64)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000772 return;
773
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +1000774 if (ret || win64->length < sizeof(*dwp))
Milton Miller2573f682011-05-11 12:24:58 +0000775 goto delprop;
776
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000777 dwp = win64->value;
778 liobn = (u64)be32_to_cpu(dwp->liobn);
779
780 /* clear the whole window, note the arg is in kernel pages */
781 ret = tce_clearrange_multi_pSeriesLP(0,
782 1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp);
783 if (ret)
784 pr_warning("%s failed to clear tces in window.\n",
785 np->full_name);
786 else
787 pr_debug("%s successfully cleared tces in window.\n",
788 np->full_name);
789
Nishanth Aravamudanae69e1e2014-01-10 15:09:38 -0800790 ret = rtas_call(ddw_avail[2], 1, 1, NULL, liobn);
791 if (ret)
792 pr_warning("%s: failed to remove direct window: rtas returned "
793 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
794 np->full_name, ret, ddw_avail[2], liobn);
795 else
796 pr_debug("%s: successfully removed direct window: rtas returned "
797 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
798 np->full_name, ret, ddw_avail[2], liobn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000799
Milton Miller2573f682011-05-11 12:24:58 +0000800delprop:
Gavin Shan5efbabe2014-08-11 19:16:20 +1000801 if (remove_prop)
802 ret = of_remove_property(np, win64);
Milton Miller2573f682011-05-11 12:24:58 +0000803 if (ret)
Milton Millerc8566782011-05-11 12:24:59 +0000804 pr_warning("%s: failed to remove direct window property: %d\n",
Milton Miller2573f682011-05-11 12:24:58 +0000805 np->full_name, ret);
806}
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000807
Milton Millerb73a6352011-05-11 12:25:00 +0000808static u64 find_existing_ddw(struct device_node *pdn)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000809{
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000810 struct direct_window *window;
811 const struct dynamic_dma_window_prop *direct64;
812 u64 dma_addr = 0;
813
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000814 spin_lock(&direct_window_list_lock);
815 /* check if we already created a window and dupe that config if so */
816 list_for_each_entry(window, &direct_window_list, list) {
817 if (window->device == pdn) {
818 direct64 = window->prop;
Anton Blancharddf015602013-10-17 23:21:15 +1100819 dma_addr = be64_to_cpu(direct64->dma_base);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000820 break;
821 }
822 }
823 spin_unlock(&direct_window_list_lock);
824
825 return dma_addr;
826}
827
Milton Millerc8566782011-05-11 12:24:59 +0000828static int find_existing_ddw_windows(void)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000829{
Nishanth Aravamudan97e7dc52014-01-10 15:10:41 -0800830 int len;
Milton Millerc8566782011-05-11 12:24:59 +0000831 struct device_node *pdn;
Nishanth Aravamudan97e7dc52014-01-10 15:10:41 -0800832 struct direct_window *window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000833 const struct dynamic_dma_window_prop *direct64;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000834
Milton Millerc8566782011-05-11 12:24:59 +0000835 if (!firmware_has_feature(FW_FEATURE_LPAR))
836 return 0;
837
838 for_each_node_with_property(pdn, DIRECT64_PROPNAME) {
Nishanth Aravamudan97e7dc52014-01-10 15:10:41 -0800839 direct64 = of_get_property(pdn, DIRECT64_PROPNAME, &len);
Milton Millerc8566782011-05-11 12:24:59 +0000840 if (!direct64)
841 continue;
842
Nishanth Aravamudan97e7dc52014-01-10 15:10:41 -0800843 window = kzalloc(sizeof(*window), GFP_KERNEL);
844 if (!window || len < sizeof(struct dynamic_dma_window_prop)) {
845 kfree(window);
Gavin Shan5efbabe2014-08-11 19:16:20 +1000846 remove_ddw(pdn, true);
Nishanth Aravamudan97e7dc52014-01-10 15:10:41 -0800847 continue;
848 }
Milton Millerc8566782011-05-11 12:24:59 +0000849
Nishanth Aravamudan97e7dc52014-01-10 15:10:41 -0800850 window->device = pdn;
851 window->prop = direct64;
852 spin_lock(&direct_window_list_lock);
853 list_add(&window->list, &direct_window_list);
854 spin_unlock(&direct_window_list_lock);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000855 }
856
Milton Millerc8566782011-05-11 12:24:59 +0000857 return 0;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000858}
Milton Millerc8566782011-05-11 12:24:59 +0000859machine_arch_initcall(pseries, find_existing_ddw_windows);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000860
Milton Millerb73a6352011-05-11 12:25:00 +0000861static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000862 struct ddw_query_response *query)
863{
Guilherme G. Piccoli8445a872016-04-11 16:17:23 -0300864 struct device_node *dn;
865 struct pci_dn *pdn;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000866 u32 cfg_addr;
867 u64 buid;
868 int ret;
869
870 /*
871 * Get the config address and phb buid of the PE window.
872 * Rely on eeh to retrieve this for us.
873 * Retrieve them from the pci device, not the node with the
874 * dma-window property
875 */
Guilherme G. Piccoli8445a872016-04-11 16:17:23 -0300876 dn = pci_device_to_OF_node(dev);
877 pdn = PCI_DN(dn);
878 buid = pdn->phb->buid;
Gavin Shan8a934ef2016-05-26 09:56:07 +1000879 cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8));
Gavin Shan39baadb2012-03-20 21:30:28 +0000880
Milton Millerb73a6352011-05-11 12:25:00 +0000881 ret = rtas_call(ddw_avail[0], 3, 5, (u32 *)query,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000882 cfg_addr, BUID_HI(buid), BUID_LO(buid));
883 dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
Milton Millerb73a6352011-05-11 12:25:00 +0000884 " returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000885 BUID_LO(buid), ret);
886 return ret;
887}
888
Milton Millerb73a6352011-05-11 12:25:00 +0000889static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000890 struct ddw_create_response *create, int page_shift,
891 int window_shift)
892{
Guilherme G. Piccoli8445a872016-04-11 16:17:23 -0300893 struct device_node *dn;
894 struct pci_dn *pdn;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000895 u32 cfg_addr;
896 u64 buid;
897 int ret;
898
899 /*
900 * Get the config address and phb buid of the PE window.
901 * Rely on eeh to retrieve this for us.
902 * Retrieve them from the pci device, not the node with the
903 * dma-window property
904 */
Guilherme G. Piccoli8445a872016-04-11 16:17:23 -0300905 dn = pci_device_to_OF_node(dev);
906 pdn = PCI_DN(dn);
907 buid = pdn->phb->buid;
Gavin Shan8a934ef2016-05-26 09:56:07 +1000908 cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8));
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000909
910 do {
911 /* extra outputs are LIOBN and dma-addr (hi, lo) */
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +1000912 ret = rtas_call(ddw_avail[1], 5, 4, (u32 *)create,
913 cfg_addr, BUID_HI(buid), BUID_LO(buid),
914 page_shift, window_shift);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000915 } while (rtas_busy_delay(ret));
916 dev_info(&dev->dev,
917 "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
Milton Millerb73a6352011-05-11 12:25:00 +0000918 "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000919 cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
920 window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
921
922 return ret;
923}
924
Nishanth Aravamudan61435692013-03-07 12:33:03 +0000925struct failed_ddw_pdn {
926 struct device_node *pdn;
927 struct list_head list;
928};
929
930static LIST_HEAD(failed_ddw_pdn_list);
931
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000932/*
933 * If the PE supports dynamic dma windows, and there is space for a table
934 * that can map all pages in a linear offset, then setup such a table,
935 * and record the dma-offset in the struct device.
936 *
937 * dev: the pci device we are checking
938 * pdn: the parent pe node with the ibm,dma_window property
939 * Future: also check if we can remap the base window for our base page size
940 *
941 * returns the dma offset for use by dma_set_mask
942 */
943static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
944{
945 int len, ret;
946 struct ddw_query_response query;
947 struct ddw_create_response create;
948 int page_shift;
949 u64 dma_addr, max_addr;
950 struct device_node *dn;
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +1000951 u32 ddw_avail[3];
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000952 struct direct_window *window;
Nishanth Aravamudan76730332011-05-06 13:27:30 +0000953 struct property *win64;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000954 struct dynamic_dma_window_prop *ddwprop;
Nishanth Aravamudan61435692013-03-07 12:33:03 +0000955 struct failed_ddw_pdn *fpdn;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000956
957 mutex_lock(&direct_window_init_mutex);
958
Milton Millerb73a6352011-05-11 12:25:00 +0000959 dma_addr = find_existing_ddw(pdn);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000960 if (dma_addr != 0)
961 goto out_unlock;
962
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000963 /*
Nishanth Aravamudan61435692013-03-07 12:33:03 +0000964 * If we already went through this for a previous function of
965 * the same device and failed, we don't want to muck with the
966 * DMA window again, as it will race with in-flight operations
967 * and can lead to EEHs. The above mutex protects access to the
968 * list.
969 */
970 list_for_each_entry(fpdn, &failed_ddw_pdn_list, list) {
971 if (!strcmp(fpdn->pdn->full_name, pdn->full_name))
972 goto out_unlock;
973 }
974
975 /*
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000976 * the ibm,ddw-applicable property holds the tokens for:
977 * ibm,query-pe-dma-window
978 * ibm,create-pe-dma-window
979 * ibm,remove-pe-dma-window
980 * for the given node in that order.
981 * the property is actually in the parent, not the PE
982 */
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +1000983 ret = of_property_read_u32_array(pdn, "ibm,ddw-applicable",
984 &ddw_avail[0], 3);
985 if (ret)
Nishanth Aravamudanae69e1e2014-01-10 15:09:38 -0800986 goto out_failed;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000987
Nishanth Aravamudanae69e1e2014-01-10 15:09:38 -0800988 /*
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000989 * Query if there is a second window of size to map the
990 * whole partition. Query returns number of windows, largest
991 * block assigned to PE (partition endpoint), and two bitmasks
992 * of page sizes: supported and supported for migrate-dma.
993 */
994 dn = pci_device_to_OF_node(dev);
Milton Millerb73a6352011-05-11 12:25:00 +0000995 ret = query_ddw(dev, ddw_avail, &query);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000996 if (ret != 0)
Nishanth Aravamudanae69e1e2014-01-10 15:09:38 -0800997 goto out_failed;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +0000998
999 if (query.windows_available == 0) {
1000 /*
1001 * no additional windows are available for this device.
1002 * We might be able to reallocate the existing window,
1003 * trading in for a larger page size.
1004 */
1005 dev_dbg(&dev->dev, "no free dynamic windows");
Nishanth Aravamudanae69e1e2014-01-10 15:09:38 -08001006 goto out_failed;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001007 }
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +10001008 if (query.page_size & 4) {
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001009 page_shift = 24; /* 16MB */
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +10001010 } else if (query.page_size & 2) {
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001011 page_shift = 16; /* 64kB */
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +10001012 } else if (query.page_size & 1) {
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001013 page_shift = 12; /* 4kB */
1014 } else {
1015 dev_dbg(&dev->dev, "no supported direct page size in mask %x",
1016 query.page_size);
Nishanth Aravamudanae69e1e2014-01-10 15:09:38 -08001017 goto out_failed;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001018 }
1019 /* verify the window * number of ptes will map the partition */
1020 /* check largest block * page size > max memory hotplug addr */
1021 max_addr = memory_hotplug_max();
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +10001022 if (query.largest_available_block < (max_addr >> page_shift)) {
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001023 dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u "
1024 "%llu-sized pages\n", max_addr, query.largest_available_block,
1025 1ULL << page_shift);
Nishanth Aravamudanae69e1e2014-01-10 15:09:38 -08001026 goto out_failed;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001027 }
1028 len = order_base_2(max_addr);
1029 win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
1030 if (!win64) {
1031 dev_info(&dev->dev,
1032 "couldn't allocate property for 64bit dma window\n");
Nishanth Aravamudanae69e1e2014-01-10 15:09:38 -08001033 goto out_failed;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001034 }
1035 win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
1036 win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
Nishanth Aravamudan76730332011-05-06 13:27:30 +00001037 win64->length = sizeof(*ddwprop);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001038 if (!win64->name || !win64->value) {
1039 dev_info(&dev->dev,
1040 "couldn't allocate property name and value\n");
1041 goto out_free_prop;
1042 }
1043
Milton Millerb73a6352011-05-11 12:25:00 +00001044 ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001045 if (ret != 0)
1046 goto out_free_prop;
1047
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +10001048 ddwprop->liobn = cpu_to_be32(create.liobn);
1049 ddwprop->dma_base = cpu_to_be64(((u64)create.addr_hi << 32) |
1050 create.addr_lo);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001051 ddwprop->tce_shift = cpu_to_be32(page_shift);
1052 ddwprop->window_shift = cpu_to_be32(len);
1053
1054 dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n",
1055 create.liobn, dn->full_name);
1056
1057 window = kzalloc(sizeof(*window), GFP_KERNEL);
1058 if (!window)
1059 goto out_clear_window;
1060
1061 ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
1062 win64->value, tce_setrange_multi_pSeriesLP_walk);
1063 if (ret) {
1064 dev_info(&dev->dev, "failed to map direct window for %s: %d\n",
1065 dn->full_name, ret);
Julia Lawall7a190812011-08-08 01:18:00 +00001066 goto out_free_window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001067 }
1068
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001069 ret = of_add_property(pdn, win64);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001070 if (ret) {
1071 dev_err(&dev->dev, "unable to add dma window property for %s: %d",
1072 pdn->full_name, ret);
Julia Lawall7a190812011-08-08 01:18:00 +00001073 goto out_free_window;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001074 }
1075
1076 window->device = pdn;
1077 window->prop = ddwprop;
1078 spin_lock(&direct_window_list_lock);
1079 list_add(&window->list, &direct_window_list);
1080 spin_unlock(&direct_window_list_lock);
1081
Alexey Kardashevskiy9410e012014-09-25 16:39:18 +10001082 dma_addr = be64_to_cpu(ddwprop->dma_base);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001083 goto out_unlock;
1084
Julia Lawall7a190812011-08-08 01:18:00 +00001085out_free_window:
1086 kfree(window);
1087
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001088out_clear_window:
Gavin Shan5efbabe2014-08-11 19:16:20 +10001089 remove_ddw(pdn, true);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001090
1091out_free_prop:
1092 kfree(win64->name);
1093 kfree(win64->value);
1094 kfree(win64);
1095
Nishanth Aravamudanae69e1e2014-01-10 15:09:38 -08001096out_failed:
Nishanth Aravamudan25ebc452012-05-15 07:04:32 +00001097
Nishanth Aravamudan61435692013-03-07 12:33:03 +00001098 fpdn = kzalloc(sizeof(*fpdn), GFP_KERNEL);
1099 if (!fpdn)
1100 goto out_unlock;
1101 fpdn->pdn = pdn;
1102 list_add(&fpdn->list, &failed_ddw_pdn_list);
1103
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001104out_unlock:
1105 mutex_unlock(&direct_window_init_mutex);
1106 return dma_addr;
1107}
1108
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001109static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 struct device_node *pdn, *dn;
1112 struct iommu_table *tbl;
Anton Blanchard2083f682013-08-07 02:01:36 +10001113 const __be32 *dma_window = NULL;
Paul Mackerras16353172005-09-06 13:17:54 +10001114 struct pci_dn *pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001116 pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /* dev setup for LPAR is a little tricky, since the device tree might
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001119 * contain the dma-window properties per-device and not necessarily
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * for the bus. So we need to search upwards in the tree until we
1121 * either hit a dma-window property, OR find a parent with a table
1122 * already allocated.
1123 */
1124 dn = pci_device_to_OF_node(dev);
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001125 pr_debug(" node is %s\n", dn->full_name);
Linas Vepstas5d2efba2006-10-30 16:15:59 +11001126
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001127 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
Paul Mackerras16353172005-09-06 13:17:54 +10001128 pdn = pdn->parent) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +10001129 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (dma_window)
1131 break;
1132 }
1133
Linas Vepstas650f7b32007-04-11 06:11:23 +10001134 if (!pdn || !PCI_DN(pdn)) {
1135 printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
1136 "no DMA window found for pci dev=%s dn=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001137 pci_name(dev), of_node_full_name(dn));
Linas Vepstas650f7b32007-04-11 06:11:23 +10001138 return;
1139 }
Michael Ellermanf7ebf352008-04-24 15:13:19 +10001140 pr_debug(" parent is %s\n", pdn->full_name);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11001141
linase07102d2005-12-05 19:37:35 -06001142 pci = PCI_DN(pdn);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001143 if (!pci->table_group) {
1144 pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
1145 tbl = pci->table_group->tables[0];
Benjamin Herrenschmidtb8c49de2010-12-09 15:24:01 +11001146 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001147 tbl->it_ops = &iommu_table_lpar_multi_ops;
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001148 iommu_init_table(tbl, pci->phb->node);
1149 iommu_register_group(pci->table_group,
1150 pci_domain_nr(pci->phb->bus), 0);
1151 pr_debug(" created table: %p\n", pci->table_group);
Michael Neulingde113212007-05-10 15:16:27 +10001152 } else {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001153 pr_debug(" found DMA window, table: %p\n", pci->table_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001156 set_iommu_table_base(&dev->dev, pci->table_group->tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001157 iommu_add_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001159
1160static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask)
1161{
1162 bool ddw_enabled = false;
1163 struct device_node *pdn, *dn;
1164 struct pci_dev *pdev;
Anton Blanchard2083f682013-08-07 02:01:36 +10001165 const __be32 *dma_window = NULL;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001166 u64 dma_offset;
1167
Milton Miller64ac8222011-05-11 12:24:57 +00001168 if (!dev->dma_mask)
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001169 return -EIO;
1170
Milton Miller64ac8222011-05-11 12:24:57 +00001171 if (!dev_is_pci(dev))
1172 goto check_mask;
1173
Nishanth Aravamudaneb0dd412011-05-09 12:58:03 +00001174 pdev = to_pci_dev(dev);
1175
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001176 /* only attempt to use a new window if 64-bit DMA is requested */
1177 if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) {
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001178 dn = pci_device_to_OF_node(pdev);
1179 dev_dbg(dev, "node is %s\n", dn->full_name);
1180
1181 /*
1182 * the device tree might contain the dma-window properties
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001183 * per-device and not necessarily for the bus. So we need to
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001184 * search upwards in the tree until we either hit a dma-window
1185 * property, OR find a parent with a table already allocated.
1186 */
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001187 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001188 pdn = pdn->parent) {
1189 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1190 if (dma_window)
1191 break;
1192 }
1193 if (pdn && PCI_DN(pdn)) {
1194 dma_offset = enable_ddw(pdev, pdn);
1195 if (dma_offset != 0) {
1196 dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset);
1197 set_dma_offset(dev, dma_offset);
1198 set_dma_ops(dev, &dma_direct_ops);
1199 ddw_enabled = true;
1200 }
1201 }
1202 }
1203
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001204 /* fall back on iommu ops */
Milton Miller64ac8222011-05-11 12:24:57 +00001205 if (!ddw_enabled && get_dma_ops(dev) != &dma_iommu_ops) {
1206 dev_info(dev, "Restoring 32-bit DMA via iommu\n");
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001207 set_dma_ops(dev, &dma_iommu_ops);
1208 }
1209
Milton Miller64ac8222011-05-11 12:24:57 +00001210check_mask:
1211 if (!dma_supported(dev, dma_mask))
1212 return -EIO;
1213
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001214 *dev->dma_mask = dma_mask;
1215 return 0;
1216}
1217
Milton Miller6a5c7be2011-06-24 09:05:22 +00001218static u64 dma_get_required_mask_pSeriesLP(struct device *dev)
1219{
1220 if (!dev->dma_mask)
1221 return 0;
1222
1223 if (!disable_ddw && dev_is_pci(dev)) {
1224 struct pci_dev *pdev = to_pci_dev(dev);
1225 struct device_node *dn;
1226
1227 dn = pci_device_to_OF_node(pdev);
1228
1229 /* search upwards for ibm,dma-window */
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001230 for (; dn && PCI_DN(dn) && !PCI_DN(dn)->table_group;
Milton Miller6a5c7be2011-06-24 09:05:22 +00001231 dn = dn->parent)
1232 if (of_get_property(dn, "ibm,dma-window", NULL))
1233 break;
1234 /* if there is a ibm,ddw-applicable property require 64 bits */
1235 if (dn && PCI_DN(dn) &&
1236 of_get_property(dn, "ibm,ddw-applicable", NULL))
1237 return DMA_BIT_MASK(64);
1238 }
1239
Milton Millerd24f9c62011-06-24 09:05:24 +00001240 return dma_iommu_ops.get_required_mask(dev);
Milton Miller6a5c7be2011-06-24 09:05:22 +00001241}
1242
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001243static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
1244 void *data)
1245{
1246 struct direct_window *window;
1247 struct memory_notify *arg = data;
1248 int ret = 0;
1249
1250 switch (action) {
1251 case MEM_GOING_ONLINE:
1252 spin_lock(&direct_window_list_lock);
1253 list_for_each_entry(window, &direct_window_list, list) {
1254 ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
1255 arg->nr_pages, window->prop);
1256 /* XXX log error */
1257 }
1258 spin_unlock(&direct_window_list_lock);
1259 break;
1260 case MEM_CANCEL_ONLINE:
1261 case MEM_OFFLINE:
1262 spin_lock(&direct_window_list_lock);
1263 list_for_each_entry(window, &direct_window_list, list) {
1264 ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
1265 arg->nr_pages, window->prop);
1266 /* XXX log error */
1267 }
1268 spin_unlock(&direct_window_list_lock);
1269 break;
1270 default:
1271 break;
1272 }
1273 if (ret && action != MEM_CANCEL_ONLINE)
1274 return NOTIFY_BAD;
1275
1276 return NOTIFY_OK;
1277}
1278
1279static struct notifier_block iommu_mem_nb = {
1280 .notifier_call = iommu_mem_notifier,
1281};
1282
Grant Likelyf5242e52014-11-24 17:58:01 +00001283static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *data)
Stephen Rothwellbed59272007-03-04 17:04:44 +11001284{
1285 int err = NOTIFY_OK;
Grant Likelyf5242e52014-11-24 17:58:01 +00001286 struct of_reconfig_data *rd = data;
1287 struct device_node *np = rd->dn;
Stephen Rothwellbed59272007-03-04 17:04:44 +11001288 struct pci_dn *pci = PCI_DN(np);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001289 struct direct_window *window;
Stephen Rothwellbed59272007-03-04 17:04:44 +11001290
1291 switch (action) {
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001292 case OF_RECONFIG_DETACH_NODE:
Gavin Shan5efbabe2014-08-11 19:16:20 +10001293 /*
1294 * Removing the property will invoke the reconfig
1295 * notifier again, which causes dead-lock on the
1296 * read-write semaphore of the notifier chain. So
1297 * we have to remove the property when releasing
1298 * the device node.
1299 */
1300 remove_ddw(np, false);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001301 if (pci && pci->table_group)
1302 iommu_pseries_free_group(pci->table_group,
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001303 np->full_name);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001304
1305 spin_lock(&direct_window_list_lock);
1306 list_for_each_entry(window, &direct_window_list, list) {
1307 if (window->device == np) {
1308 list_del(&window->list);
1309 kfree(window);
1310 break;
1311 }
1312 }
1313 spin_unlock(&direct_window_list_lock);
Stephen Rothwellbed59272007-03-04 17:04:44 +11001314 break;
1315 default:
1316 err = NOTIFY_DONE;
1317 break;
1318 }
1319 return err;
1320}
1321
1322static struct notifier_block iommu_reconfig_nb = {
1323 .notifier_call = iommu_reconfig_notifier,
1324};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326/* These are called very early. */
1327void iommu_init_early_pSeries(void)
1328{
Nishanth Aravamudana8daac82010-10-18 07:27:03 +00001329 if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Michael Ellerman57cfb812006-03-21 20:45:59 +11001332 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Daniel Axtens38ae9ec2015-03-31 16:00:50 +11001333 pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
1334 pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001335 ppc_md.dma_set_mask = dma_set_mask_pSeriesLP;
Milton Miller6a5c7be2011-06-24 09:05:22 +00001336 ppc_md.dma_get_required_mask = dma_get_required_mask_pSeriesLP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 } else {
Daniel Axtens38ae9ec2015-03-31 16:00:50 +11001338 pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeries;
1339 pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341
1342
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001343 of_reconfig_notifier_register(&iommu_reconfig_nb);
Nishanth Aravamudan4e8b0cf2011-02-10 09:10:47 +00001344 register_memory_notifier(&iommu_mem_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Stephen Rothwell98747772007-03-04 16:58:39 +11001346 set_pci_dma_ops(&dma_iommu_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Will Schmidt4e89a2d2010-09-28 15:33:12 +00001349static int __init disable_multitce(char *str)
1350{
1351 if (strcmp(str, "off") == 0 &&
1352 firmware_has_feature(FW_FEATURE_LPAR) &&
1353 firmware_has_feature(FW_FEATURE_MULTITCE)) {
1354 printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
Will Schmidt4e89a2d2010-09-28 15:33:12 +00001355 powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
1356 }
1357 return 1;
1358}
1359
1360__setup("multitce=", disable_multitce);
Nishanth Aravamudan4ad04e52015-02-21 11:00:50 -08001361
1362machine_subsys_initcall_sync(pseries, tce_iommu_bus_notifier_init);