blob: ff5a6ce027b88e88df6dbf7aa32519f2d08e6d7c [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 *
4 * Rewrite, cleanup, new allocation schemes, virtual merging:
5 * Copyright (C) 2004 Olof Johansson, IBM Corporation
6 * and Ben. Herrenschmidt, IBM Corporation
7 *
8 * Dynamic DMA mapping support, bus-independent parts.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
30#include <linux/spinlock.h>
31#include <linux/string.h>
32#include <linux/dma-mapping.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080033#include <linux/bitmap.h>
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -080034#include <linux/iommu-helper.h>
Milton Miller62a8bd62008-10-22 15:39:04 -050035#include <linux/crash_dump.h>
Anton Blanchardb4c3a872012-06-07 18:14:48 +000036#include <linux/hash.h>
Anton Blanchardd6b9a812012-06-24 18:26:17 +000037#include <linux/fault-inject.h>
38#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/io.h>
40#include <asm/prom.h>
41#include <asm/iommu.h>
42#include <asm/pci-bridge.h>
43#include <asm/machdep.h>
Haren Myneni5f508672006-06-22 23:35:10 -070044#include <asm/kdump.h>
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +000045#include <asm/fadump.h>
Anton Blanchardd6b9a812012-06-24 18:26:17 +000046#include <asm/vio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define DBG(...)
49
FUJITA Tomonori191aee52010-03-02 14:25:38 +000050static int novmerge;
Jake Moilanen56997552007-03-29 08:44:02 -050051
Robert Jennings6490c492008-07-24 04:31:16 +100052static void __iommu_free(struct iommu_table *, dma_addr_t, unsigned int);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static int __init setup_iommu(char *str)
55{
56 if (!strcmp(str, "novmerge"))
57 novmerge = 1;
58 else if (!strcmp(str, "vmerge"))
59 novmerge = 0;
60 return 1;
61}
62
63__setup("iommu=", setup_iommu);
64
Anton Blanchardb4c3a872012-06-07 18:14:48 +000065static DEFINE_PER_CPU(unsigned int, iommu_pool_hash);
66
67/*
68 * We precalculate the hash to avoid doing it on every allocation.
69 *
70 * The hash is important to spread CPUs across all the pools. For example,
71 * on a POWER7 with 4 way SMT we want interrupts on the primary threads and
72 * with 4 pools all primary threads would map to the same pool.
73 */
74static int __init setup_iommu_pool_hash(void)
75{
76 unsigned int i;
77
78 for_each_possible_cpu(i)
79 per_cpu(iommu_pool_hash, i) = hash_32(i, IOMMU_POOL_HASHBITS);
80
81 return 0;
82}
83subsys_initcall(setup_iommu_pool_hash);
84
Anton Blanchardd6b9a812012-06-24 18:26:17 +000085#ifdef CONFIG_FAIL_IOMMU
86
87static DECLARE_FAULT_ATTR(fail_iommu);
88
89static int __init setup_fail_iommu(char *str)
90{
91 return setup_fault_attr(&fail_iommu, str);
92}
93__setup("fail_iommu=", setup_fail_iommu);
94
95static bool should_fail_iommu(struct device *dev)
96{
97 return dev->archdata.fail_iommu && should_fail(&fail_iommu, 1);
98}
99
100static int __init fail_iommu_debugfs(void)
101{
102 struct dentry *dir = fault_create_debugfs_attr("fail_iommu",
103 NULL, &fail_iommu);
104
105 return IS_ERR(dir) ? PTR_ERR(dir) : 0;
106}
107late_initcall(fail_iommu_debugfs);
108
109static ssize_t fail_iommu_show(struct device *dev,
110 struct device_attribute *attr, char *buf)
111{
112 return sprintf(buf, "%d\n", dev->archdata.fail_iommu);
113}
114
115static ssize_t fail_iommu_store(struct device *dev,
116 struct device_attribute *attr, const char *buf,
117 size_t count)
118{
119 int i;
120
121 if (count > 0 && sscanf(buf, "%d", &i) > 0)
122 dev->archdata.fail_iommu = (i == 0) ? 0 : 1;
123
124 return count;
125}
126
127static DEVICE_ATTR(fail_iommu, S_IRUGO|S_IWUSR, fail_iommu_show,
128 fail_iommu_store);
129
130static int fail_iommu_bus_notify(struct notifier_block *nb,
131 unsigned long action, void *data)
132{
133 struct device *dev = data;
134
135 if (action == BUS_NOTIFY_ADD_DEVICE) {
136 if (device_create_file(dev, &dev_attr_fail_iommu))
137 pr_warn("Unable to create IOMMU fault injection sysfs "
138 "entries\n");
139 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
140 device_remove_file(dev, &dev_attr_fail_iommu);
141 }
142
143 return 0;
144}
145
146static struct notifier_block fail_iommu_bus_notifier = {
147 .notifier_call = fail_iommu_bus_notify
148};
149
150static int __init fail_iommu_setup(void)
151{
152#ifdef CONFIG_PCI
153 bus_register_notifier(&pci_bus_type, &fail_iommu_bus_notifier);
154#endif
155#ifdef CONFIG_IBMVIO
156 bus_register_notifier(&vio_bus_type, &fail_iommu_bus_notifier);
157#endif
158
159 return 0;
160}
161/*
162 * Must execute after PCI and VIO subsystem have initialised but before
163 * devices are probed.
164 */
165arch_initcall(fail_iommu_setup);
166#else
167static inline bool should_fail_iommu(struct device *dev)
168{
169 return false;
170}
171#endif
172
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800173static unsigned long iommu_range_alloc(struct device *dev,
174 struct iommu_table *tbl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 unsigned long npages,
176 unsigned long *handle,
Olof Johansson7daa4112006-04-12 21:05:59 -0500177 unsigned long mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned int align_order)
179{
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800180 unsigned long n, end, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 unsigned long limit;
182 int largealloc = npages > 15;
183 int pass = 0;
184 unsigned long align_mask;
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800185 unsigned long boundary_size;
Anton Blanchardd3622132012-06-03 19:44:25 +0000186 unsigned long flags;
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000187 unsigned int pool_nr;
188 struct iommu_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 align_mask = 0xffffffffffffffffl >> (64 - align_order);
191
192 /* This allocator was derived from x86_64's bit string search */
193
194 /* Sanity check */
Nick Piggin13a2eea2006-10-04 17:25:44 +0200195 if (unlikely(npages == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (printk_ratelimit())
197 WARN_ON(1);
198 return DMA_ERROR_CODE;
199 }
200
Anton Blanchardd6b9a812012-06-24 18:26:17 +0000201 if (should_fail_iommu(dev))
202 return DMA_ERROR_CODE;
203
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000204 /*
205 * We don't need to disable preemption here because any CPU can
206 * safely use any IOMMU pool.
207 */
208 pool_nr = __raw_get_cpu_var(iommu_pool_hash) & (tbl->nr_pools - 1);
Anton Blanchardd3622132012-06-03 19:44:25 +0000209
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000210 if (largealloc)
211 pool = &(tbl->large_pool);
212 else
213 pool = &(tbl->pools[pool_nr]);
214
215 spin_lock_irqsave(&(pool->lock), flags);
216
217again:
218 if ((pass == 0) && handle && *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 start = *handle;
220 else
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000221 start = pool->hint;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000223 limit = pool->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* The case below can happen if we have a small segment appended
226 * to a large, or when the previous alloc was at the very end of
227 * the available space. If so, go back to the initial start.
228 */
229 if (start >= limit)
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000230 start = pool->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Olof Johansson7daa4112006-04-12 21:05:59 -0500232 if (limit + tbl->it_offset > mask) {
233 limit = mask - tbl->it_offset + 1;
234 /* If we're constrained on address range, first try
235 * at the masked hint to avoid O(n) search complexity,
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000236 * but on second pass, start at 0 in pool 0.
Olof Johansson7daa4112006-04-12 21:05:59 -0500237 */
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000238 if ((start & mask) >= limit || pass > 0) {
239 pool = &(tbl->pools[0]);
240 start = pool->start;
241 } else {
Olof Johansson7daa4112006-04-12 21:05:59 -0500242 start &= mask;
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000243 }
Olof Johansson7daa4112006-04-12 21:05:59 -0500244 }
245
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800246 if (dev)
247 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
248 1 << IOMMU_PAGE_SHIFT);
249 else
250 boundary_size = ALIGN(1UL << 32, 1 << IOMMU_PAGE_SHIFT);
251 /* 4GB boundary for iseries_hv_alloc and iseries_hv_map */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800253 n = iommu_area_alloc(tbl->it_map, limit, start, npages,
254 tbl->it_offset, boundary_size >> IOMMU_PAGE_SHIFT,
255 align_mask);
256 if (n == -1) {
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000257 if (likely(pass == 0)) {
258 /* First try the pool from the start */
259 pool->hint = pool->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 pass++;
261 goto again;
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000262
263 } else if (pass <= tbl->nr_pools) {
264 /* Now try scanning all the other pools */
265 spin_unlock(&(pool->lock));
266 pool_nr = (pool_nr + 1) & (tbl->nr_pools - 1);
267 pool = &tbl->pools[pool_nr];
268 spin_lock(&(pool->lock));
269 pool->hint = pool->start;
270 pass++;
271 goto again;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 } else {
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000274 /* Give up */
275 spin_unlock_irqrestore(&(pool->lock), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return DMA_ERROR_CODE;
277 }
278 }
279
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800280 end = n + npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /* Bump the hint to a new block for small allocs. */
283 if (largealloc) {
284 /* Don't bump to new block to avoid fragmentation */
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000285 pool->hint = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 } else {
287 /* Overflow will be taken care of at the next allocation */
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000288 pool->hint = (end + tbl->it_blocksize - 1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 ~(tbl->it_blocksize - 1);
290 }
291
292 /* Update handle for SG allocations */
293 if (handle)
294 *handle = end;
295
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000296 spin_unlock_irqrestore(&(pool->lock), flags);
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return n;
299}
300
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800301static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
302 void *page, unsigned int npages,
303 enum dma_data_direction direction,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000304 unsigned long mask, unsigned int align_order,
305 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Anton Blanchardd3622132012-06-03 19:44:25 +0000307 unsigned long entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 dma_addr_t ret = DMA_ERROR_CODE;
Robert Jennings6490c492008-07-24 04:31:16 +1000309 int build_fail;
Olof Johansson7daa4112006-04-12 21:05:59 -0500310
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800311 entry = iommu_range_alloc(dev, tbl, npages, NULL, mask, align_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Anton Blanchard0e4bc952012-06-03 19:43:02 +0000313 if (unlikely(entry == DMA_ERROR_CODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return DMA_ERROR_CODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 entry += tbl->it_offset; /* Offset into real TCE table */
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100317 ret = entry << IOMMU_PAGE_SHIFT; /* Set the return dma address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /* Put the TCEs in the HW table */
Robert Jennings6490c492008-07-24 04:31:16 +1000320 build_fail = ppc_md.tce_build(tbl, entry, npages,
321 (unsigned long)page & IOMMU_PAGE_MASK,
322 direction, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Robert Jennings6490c492008-07-24 04:31:16 +1000324 /* ppc_md.tce_build() only returns non-zero for transient errors.
325 * Clean up the table bitmap in this case and return
326 * DMA_ERROR_CODE. For all other errors the functionality is
327 * not altered.
328 */
329 if (unlikely(build_fail)) {
330 __iommu_free(tbl, ret, npages);
Robert Jennings6490c492008-07-24 04:31:16 +1000331 return DMA_ERROR_CODE;
332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Flush/invalidate TLB caches if necessary */
335 if (ppc_md.tce_flush)
336 ppc_md.tce_flush(tbl);
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* Make sure updates are seen by hardware */
339 mb();
340
341 return ret;
342}
343
Anton Blanchard67ca1412012-06-03 19:43:44 +0000344static bool iommu_free_check(struct iommu_table *tbl, dma_addr_t dma_addr,
345 unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 unsigned long entry, free_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100349 entry = dma_addr >> IOMMU_PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 free_entry = entry - tbl->it_offset;
351
352 if (((free_entry + npages) > tbl->it_size) ||
353 (entry < tbl->it_offset)) {
354 if (printk_ratelimit()) {
355 printk(KERN_INFO "iommu_free: invalid entry\n");
356 printk(KERN_INFO "\tentry = 0x%lx\n", entry);
Ingo Molnarfe333322009-01-06 14:26:03 +0000357 printk(KERN_INFO "\tdma_addr = 0x%llx\n", (u64)dma_addr);
358 printk(KERN_INFO "\tTable = 0x%llx\n", (u64)tbl);
359 printk(KERN_INFO "\tbus# = 0x%llx\n", (u64)tbl->it_busno);
360 printk(KERN_INFO "\tsize = 0x%llx\n", (u64)tbl->it_size);
361 printk(KERN_INFO "\tstartOff = 0x%llx\n", (u64)tbl->it_offset);
362 printk(KERN_INFO "\tindex = 0x%llx\n", (u64)tbl->it_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 WARN_ON(1);
364 }
Anton Blanchard67ca1412012-06-03 19:43:44 +0000365
366 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
Anton Blanchard67ca1412012-06-03 19:43:44 +0000369 return true;
370}
371
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000372static struct iommu_pool *get_pool(struct iommu_table *tbl,
373 unsigned long entry)
374{
375 struct iommu_pool *p;
376 unsigned long largepool_start = tbl->large_pool.start;
377
378 /* The large pool is the last pool at the top of the table */
379 if (entry >= largepool_start) {
380 p = &tbl->large_pool;
381 } else {
382 unsigned int pool_nr = entry / tbl->poolsize;
383
384 BUG_ON(pool_nr > tbl->nr_pools);
385 p = &tbl->pools[pool_nr];
386 }
387
388 return p;
389}
390
Anton Blanchard67ca1412012-06-03 19:43:44 +0000391static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
392 unsigned int npages)
393{
394 unsigned long entry, free_entry;
395 unsigned long flags;
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000396 struct iommu_pool *pool;
Anton Blanchard67ca1412012-06-03 19:43:44 +0000397
398 entry = dma_addr >> IOMMU_PAGE_SHIFT;
399 free_entry = entry - tbl->it_offset;
400
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000401 pool = get_pool(tbl, free_entry);
402
Anton Blanchard67ca1412012-06-03 19:43:44 +0000403 if (!iommu_free_check(tbl, dma_addr, npages))
404 return;
405
406 ppc_md.tce_free(tbl, entry, npages);
407
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000408 spin_lock_irqsave(&(pool->lock), flags);
Anton Blanchard67ca1412012-06-03 19:43:44 +0000409 bitmap_clear(tbl->it_map, free_entry, npages);
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000410 spin_unlock_irqrestore(&(pool->lock), flags);
Anton Blanchard67ca1412012-06-03 19:43:44 +0000411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
414 unsigned int npages)
415{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 __iommu_free(tbl, dma_addr, npages);
417
418 /* Make sure TLB cache is flushed if the HW needs it. We do
419 * not do an mb() here on purpose, it is not needed on any of
420 * the current platforms.
421 */
422 if (ppc_md.tce_flush)
423 ppc_md.tce_flush(tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Mark Nelsonc8692362008-07-05 05:05:41 +1000426int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
427 struct scatterlist *sglist, int nelems,
Mark Nelson3affedc2008-07-05 05:05:42 +1000428 unsigned long mask, enum dma_data_direction direction,
429 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 dma_addr_t dma_next = 0, dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 struct scatterlist *s, *outs, *segstart;
Robert Jennings6490c492008-07-24 04:31:16 +1000433 int outcount, incount, i, build_fail = 0;
Benjamin Herrenschmidtd262c322008-01-08 10:34:22 +1100434 unsigned int align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 unsigned long handle;
FUJITA Tomonori740c3ce2008-02-04 22:27:57 -0800436 unsigned int max_seg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 BUG_ON(direction == DMA_NONE);
439
440 if ((nelems == 0) || !tbl)
441 return 0;
442
443 outs = s = segstart = &sglist[0];
444 outcount = 1;
Brian Kingac9af7c2005-08-18 07:32:18 +1000445 incount = nelems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 handle = 0;
447
448 /* Init first segment length for backout at failure */
449 outs->dma_length = 0;
450
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100451 DBG("sg mapping %d elements:\n", nelems);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
FUJITA Tomonori740c3ce2008-02-04 22:27:57 -0800453 max_seg_size = dma_get_max_seg_size(dev);
Jens Axboe78bdc312007-10-12 13:44:12 +0200454 for_each_sg(sglist, s, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 unsigned long vaddr, npages, entry, slen;
456
457 slen = s->length;
458 /* Sanity check */
459 if (slen == 0) {
460 dma_next = 0;
461 continue;
462 }
463 /* Allocate iommu entries for that segment */
Jens Axboe58b053e2007-10-22 20:02:46 +0200464 vaddr = (unsigned long) sg_virt(s);
Joerg Roedel2994a3b2008-10-15 22:02:13 -0700465 npages = iommu_num_pages(vaddr, slen, IOMMU_PAGE_SIZE);
Benjamin Herrenschmidtd262c322008-01-08 10:34:22 +1100466 align = 0;
467 if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && slen >= PAGE_SIZE &&
468 (vaddr & ~PAGE_MASK) == 0)
469 align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800470 entry = iommu_range_alloc(dev, tbl, npages, &handle,
Benjamin Herrenschmidtd262c322008-01-08 10:34:22 +1100471 mask >> IOMMU_PAGE_SHIFT, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 DBG(" - vaddr: %lx, size: %lx\n", vaddr, slen);
474
475 /* Handle failure */
476 if (unlikely(entry == DMA_ERROR_CODE)) {
477 if (printk_ratelimit())
Anton Blanchard4dfa9c42010-12-07 14:36:05 +0000478 dev_info(dev, "iommu_alloc failed, tbl %p "
479 "vaddr %lx npages %lu\n", tbl, vaddr,
480 npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto failure;
482 }
483
484 /* Convert entry to a dma_addr_t */
485 entry += tbl->it_offset;
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100486 dma_addr = entry << IOMMU_PAGE_SHIFT;
487 dma_addr |= (s->offset & ~IOMMU_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100489 DBG(" - %lu pages, entry: %lx, dma_addr: %lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 npages, entry, dma_addr);
491
492 /* Insert into HW table */
Robert Jennings6490c492008-07-24 04:31:16 +1000493 build_fail = ppc_md.tce_build(tbl, entry, npages,
494 vaddr & IOMMU_PAGE_MASK,
495 direction, attrs);
496 if(unlikely(build_fail))
497 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* If we are in an open segment, try merging */
500 if (segstart != s) {
501 DBG(" - trying merge...\n");
502 /* We cannot merge if:
503 * - allocated dma_addr isn't contiguous to previous allocation
504 */
FUJITA Tomonori740c3ce2008-02-04 22:27:57 -0800505 if (novmerge || (dma_addr != dma_next) ||
506 (outs->dma_length + s->length > max_seg_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Can't merge: create a new segment */
508 segstart = s;
Jens Axboe78bdc312007-10-12 13:44:12 +0200509 outcount++;
510 outs = sg_next(outs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 DBG(" can't merge, new segment.\n");
512 } else {
513 outs->dma_length += s->length;
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100514 DBG(" merged, new len: %ux\n", outs->dma_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 }
517
518 if (segstart == s) {
519 /* This is a new segment, fill entries */
520 DBG(" - filling new segment.\n");
521 outs->dma_address = dma_addr;
522 outs->dma_length = slen;
523 }
524
525 /* Calculate next page pointer for contiguous check */
526 dma_next = dma_addr + slen;
527
528 DBG(" - dma next is: %lx\n", dma_next);
529 }
530
531 /* Flush/invalidate TLB caches if necessary */
532 if (ppc_md.tce_flush)
533 ppc_md.tce_flush(tbl);
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 DBG("mapped %d elements:\n", outcount);
536
Brian Kingac9af7c2005-08-18 07:32:18 +1000537 /* For the sake of iommu_unmap_sg, we clear out the length in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * next entry of the sglist if we didn't fill the list completely
539 */
Brian Kingac9af7c2005-08-18 07:32:18 +1000540 if (outcount < incount) {
Jens Axboe78bdc312007-10-12 13:44:12 +0200541 outs = sg_next(outs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 outs->dma_address = DMA_ERROR_CODE;
543 outs->dma_length = 0;
544 }
Jake Moilanena958a262006-01-30 21:51:54 -0600545
546 /* Make sure updates are seen by hardware */
547 mb();
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return outcount;
550
551 failure:
Jens Axboe78bdc312007-10-12 13:44:12 +0200552 for_each_sg(sglist, s, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (s->dma_length != 0) {
554 unsigned long vaddr, npages;
555
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100556 vaddr = s->dma_address & IOMMU_PAGE_MASK;
Joerg Roedel2994a3b2008-10-15 22:02:13 -0700557 npages = iommu_num_pages(s->dma_address, s->dma_length,
558 IOMMU_PAGE_SIZE);
Anton Blanchardd3622132012-06-03 19:44:25 +0000559 __iommu_free(tbl, vaddr, npages);
Jake Moilanena958a262006-01-30 21:51:54 -0600560 s->dma_address = DMA_ERROR_CODE;
561 s->dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Jens Axboe78bdc312007-10-12 13:44:12 +0200563 if (s == outs)
564 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return 0;
567}
568
569
570void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
Mark Nelson3affedc2008-07-05 05:05:42 +1000571 int nelems, enum dma_data_direction direction,
572 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Jens Axboe78bdc312007-10-12 13:44:12 +0200574 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 BUG_ON(direction == DMA_NONE);
577
578 if (!tbl)
579 return;
580
Jens Axboe78bdc312007-10-12 13:44:12 +0200581 sg = sglist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 while (nelems--) {
583 unsigned int npages;
Jens Axboe78bdc312007-10-12 13:44:12 +0200584 dma_addr_t dma_handle = sg->dma_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Jens Axboe78bdc312007-10-12 13:44:12 +0200586 if (sg->dma_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 break;
Joerg Roedel2994a3b2008-10-15 22:02:13 -0700588 npages = iommu_num_pages(dma_handle, sg->dma_length,
589 IOMMU_PAGE_SIZE);
Anton Blanchardd3622132012-06-03 19:44:25 +0000590 __iommu_free(tbl, dma_handle, npages);
Jens Axboe78bdc312007-10-12 13:44:12 +0200591 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
594 /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
595 * do not do an mb() here, the affected platforms do not need it
596 * when freeing.
597 */
598 if (ppc_md.tce_flush)
599 ppc_md.tce_flush(tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Mohan Kumar M54622f12008-10-21 17:38:10 +0000602static void iommu_table_clear(struct iommu_table *tbl)
603{
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000604 /*
605 * In case of firmware assisted dump system goes through clean
606 * reboot process at the time of system crash. Hence it's safe to
607 * clear the TCE entries if firmware assisted dump is active.
608 */
609 if (!is_kdump_kernel() || is_fadump_active()) {
Mohan Kumar M54622f12008-10-21 17:38:10 +0000610 /* Clear the table in case firmware left allocations in it */
611 ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);
612 return;
613 }
614
615#ifdef CONFIG_CRASH_DUMP
616 if (ppc_md.tce_get) {
617 unsigned long index, tceval, tcecount = 0;
618
619 /* Reserve the existing mappings left by the first kernel. */
620 for (index = 0; index < tbl->it_size; index++) {
621 tceval = ppc_md.tce_get(tbl, index + tbl->it_offset);
622 /*
623 * Freed TCE entry contains 0x7fffffffffffffff on JS20
624 */
625 if (tceval && (tceval != 0x7fffffffffffffffUL)) {
626 __set_bit(index, tbl->it_map);
627 tcecount++;
628 }
629 }
630
631 if ((tbl->it_size - tcecount) < KDUMP_MIN_TCE_ENTRIES) {
632 printk(KERN_WARNING "TCE table is full; freeing ");
633 printk(KERN_WARNING "%d entries for the kdump boot\n",
634 KDUMP_MIN_TCE_ENTRIES);
635 for (index = tbl->it_size - KDUMP_MIN_TCE_ENTRIES;
636 index < tbl->it_size; index++)
637 __clear_bit(index, tbl->it_map);
638 }
639 }
640#endif
641}
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643/*
644 * Build a iommu_table structure. This contains a bit map which
645 * is used to manage allocation of the tce space.
646 */
Anton Blanchardca1588e2006-06-10 20:58:08 +1000647struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 unsigned long sz;
650 static int welcomed = 0;
Anton Blanchardca1588e2006-06-10 20:58:08 +1000651 struct page *page;
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000652 unsigned int i;
653 struct iommu_pool *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /* number of bytes needed for the bitmap */
656 sz = (tbl->it_size + 7) >> 3;
657
Anton Blanchardca1588e2006-06-10 20:58:08 +1000658 page = alloc_pages_node(nid, GFP_ATOMIC, get_order(sz));
659 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 panic("iommu_init_table: Can't allocate %ld bytes\n", sz);
Anton Blanchardca1588e2006-06-10 20:58:08 +1000661 tbl->it_map = page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 memset(tbl->it_map, 0, sz);
663
Thadeu Lima de Souza Cascardod12b5242011-09-20 03:07:24 +0000664 /*
665 * Reserve page 0 so it will not be used for any mappings.
666 * This avoids buggy drivers that consider page 0 to be invalid
667 * to crash the machine or even lose data.
668 */
669 if (tbl->it_offset == 0)
670 set_bit(0, tbl->it_map);
671
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000672 /* We only split the IOMMU table if we have 1GB or more of space */
673 if ((tbl->it_size << IOMMU_PAGE_SHIFT) >= (1UL * 1024 * 1024 * 1024))
674 tbl->nr_pools = IOMMU_NR_POOLS;
675 else
676 tbl->nr_pools = 1;
677
678 /* We reserve the top 1/4 of the table for large allocations */
Benjamin Herrenschmidtdcd261b2012-07-13 17:45:49 +1000679 tbl->poolsize = (tbl->it_size * 3 / 4) / tbl->nr_pools;
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000680
Benjamin Herrenschmidtdcd261b2012-07-13 17:45:49 +1000681 for (i = 0; i < tbl->nr_pools; i++) {
Anton Blanchardb4c3a872012-06-07 18:14:48 +0000682 p = &tbl->pools[i];
683 spin_lock_init(&(p->lock));
684 p->start = tbl->poolsize * i;
685 p->hint = p->start;
686 p->end = p->start + tbl->poolsize;
687 }
688
689 p = &tbl->large_pool;
690 spin_lock_init(&(p->lock));
691 p->start = tbl->poolsize * i;
692 p->hint = p->start;
693 p->end = tbl->it_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Mohan Kumar M54622f12008-10-21 17:38:10 +0000695 iommu_table_clear(tbl);
John Rosed3588ba2005-06-20 21:43:48 +1000696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (!welcomed) {
698 printk(KERN_INFO "IOMMU table initialized, virtual merging %s\n",
699 novmerge ? "disabled" : "enabled");
700 welcomed = 1;
701 }
702
703 return tbl;
704}
705
Stephen Rothwell68d315f2007-12-06 13:39:19 +1100706void iommu_free_table(struct iommu_table *tbl, const char *node_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 unsigned long bitmap_sz, i;
709 unsigned int order;
710
711 if (!tbl || !tbl->it_map) {
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100712 printk(KERN_ERR "%s: expected TCE map for %s\n", __func__,
Stephen Rothwell68d315f2007-12-06 13:39:19 +1100713 node_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return;
715 }
716
717 /* verify that table contains no entries */
718 /* it_size is in entries, and we're examining 64 at a time */
719 for (i = 0; i < (tbl->it_size/64); i++) {
720 if (tbl->it_map[i] != 0) {
721 printk(KERN_WARNING "%s: Unexpected TCEs for %s\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100722 __func__, node_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 break;
724 }
725 }
726
727 /* calculate bitmap size in bytes */
728 bitmap_sz = (tbl->it_size + 7) / 8;
729
730 /* free bitmap */
731 order = get_order(bitmap_sz);
732 free_pages((unsigned long) tbl->it_map, order);
733
734 /* free table */
735 kfree(tbl);
736}
737
738/* Creates TCEs for a user provided buffer. The user buffer must be
Mark Nelsonf9226d52008-10-27 20:38:08 +0000739 * contiguous real kernel storage (not vmalloc). The address passed here
740 * comprises a page address and offset into that page. The dma_addr_t
741 * returned will point to the same byte within the page as was passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
Mark Nelsonf9226d52008-10-27 20:38:08 +0000743dma_addr_t iommu_map_page(struct device *dev, struct iommu_table *tbl,
744 struct page *page, unsigned long offset, size_t size,
745 unsigned long mask, enum dma_data_direction direction,
746 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 dma_addr_t dma_handle = DMA_ERROR_CODE;
Mark Nelsonf9226d52008-10-27 20:38:08 +0000749 void *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 unsigned long uaddr;
Benjamin Herrenschmidtd262c322008-01-08 10:34:22 +1100751 unsigned int npages, align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 BUG_ON(direction == DMA_NONE);
754
Mark Nelsonf9226d52008-10-27 20:38:08 +0000755 vaddr = page_address(page) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 uaddr = (unsigned long)vaddr;
Joerg Roedel2994a3b2008-10-15 22:02:13 -0700757 npages = iommu_num_pages(uaddr, size, IOMMU_PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (tbl) {
Benjamin Herrenschmidtd262c322008-01-08 10:34:22 +1100760 align = 0;
761 if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && size >= PAGE_SIZE &&
762 ((unsigned long)vaddr & ~PAGE_MASK) == 0)
763 align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
764
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800765 dma_handle = iommu_alloc(dev, tbl, vaddr, npages, direction,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000766 mask >> IOMMU_PAGE_SHIFT, align,
767 attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (dma_handle == DMA_ERROR_CODE) {
769 if (printk_ratelimit()) {
Anton Blanchard4dfa9c42010-12-07 14:36:05 +0000770 dev_info(dev, "iommu_alloc failed, tbl %p "
771 "vaddr %p npages %d\n", tbl, vaddr,
772 npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 } else
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100775 dma_handle |= (uaddr & ~IOMMU_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
778 return dma_handle;
779}
780
Mark Nelsonf9226d52008-10-27 20:38:08 +0000781void iommu_unmap_page(struct iommu_table *tbl, dma_addr_t dma_handle,
782 size_t size, enum dma_data_direction direction,
783 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100785 unsigned int npages;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 BUG_ON(direction == DMA_NONE);
788
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100789 if (tbl) {
Joerg Roedel2994a3b2008-10-15 22:02:13 -0700790 npages = iommu_num_pages(dma_handle, size, IOMMU_PAGE_SIZE);
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100791 iommu_free(tbl, dma_handle, npages);
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795/* Allocates a contiguous real buffer and creates mappings over it.
796 * Returns the virtual address of the buffer and sets dma_handle
797 * to the dma address (mapping) of the first page.
798 */
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800799void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,
800 size_t size, dma_addr_t *dma_handle,
801 unsigned long mask, gfp_t flag, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 void *ret = NULL;
804 dma_addr_t mapping;
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100805 unsigned int order;
806 unsigned int nio_pages, io_order;
Christoph Hellwig8eb6c6e2006-06-06 16:11:35 +0200807 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 size = PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 order = get_order(size);
811
812 /*
813 * Client asked for way too much space. This is checked later
814 * anyway. It is easier to debug here for the drivers than in
815 * the tce tables.
816 */
817 if (order >= IOMAP_MAX_ORDER) {
Anton Blanchard4dfa9c42010-12-07 14:36:05 +0000818 dev_info(dev, "iommu_alloc_consistent size too large: 0x%lx\n",
819 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return NULL;
821 }
822
823 if (!tbl)
824 return NULL;
825
826 /* Alloc enough pages (and possibly more) */
Paul Mackerras05061352006-06-10 18:17:35 +1000827 page = alloc_pages_node(node, flag, order);
Christoph Hellwig8eb6c6e2006-06-06 16:11:35 +0200828 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return NULL;
Christoph Hellwig8eb6c6e2006-06-06 16:11:35 +0200830 ret = page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 memset(ret, 0, size);
832
833 /* Set up tces to cover the allocated range */
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100834 nio_pages = size >> IOMMU_PAGE_SHIFT;
835 io_order = get_iommu_order(size);
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -0800836 mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL,
Mark Nelson4f3dd8a2008-07-16 05:51:47 +1000837 mask >> IOMMU_PAGE_SHIFT, io_order, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (mapping == DMA_ERROR_CODE) {
839 free_pages((unsigned long)ret, order);
Christoph Hellwig8eb6c6e2006-06-06 16:11:35 +0200840 return NULL;
841 }
842 *dma_handle = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return ret;
844}
845
846void iommu_free_coherent(struct iommu_table *tbl, size_t size,
847 void *vaddr, dma_addr_t dma_handle)
848{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (tbl) {
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100850 unsigned int nio_pages;
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 size = PAGE_ALIGN(size);
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100853 nio_pages = size >> IOMMU_PAGE_SHIFT;
854 iommu_free(tbl, dma_handle, nio_pages);
855 size = PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 free_pages((unsigned long)vaddr, get_order(size));
857 }
858}