iommu.c 20.2 KB
Newer Older
1
/* iommu.c: Generic sparc64 IOMMU support.
L
Linus Torvalds 已提交
2
 *
3
 * Copyright (C) 1999, 2007, 2008 David S. Miller (davem@davemloft.net)
L
Linus Torvalds 已提交
4 5 6 7
 * Copyright (C) 1999, 2000 Jakub Jelinek (jakub@redhat.com)
 */

#include <linux/kernel.h>
8
#include <linux/module.h>
9
#include <linux/delay.h>
10 11 12
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/errno.h>
13
#include <linux/iommu-helper.h>
14 15

#ifdef CONFIG_PCI
16
#include <linux/pci.h>
17
#endif
L
Linus Torvalds 已提交
18

19
#include <asm/iommu.h>
L
Linus Torvalds 已提交
20 21 22

#include "iommu_common.h"

23
#define STC_CTXMATCH_ADDR(STC, CTX)	\
L
Linus Torvalds 已提交
24
	((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
25 26 27 28
#define STC_FLUSHFLAG_INIT(STC) \
	(*((STC)->strbuf_flushflag) = 0UL)
#define STC_FLUSHFLAG_SET(STC) \
	(*((STC)->strbuf_flushflag) != 0UL)
L
Linus Torvalds 已提交
29

30
#define iommu_read(__reg) \
L
Linus Torvalds 已提交
31 32 33 34 35 36 37
({	u64 __ret; \
	__asm__ __volatile__("ldxa [%1] %2, %0" \
			     : "=r" (__ret) \
			     : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
			     : "memory"); \
	__ret; \
})
38
#define iommu_write(__reg, __val) \
L
Linus Torvalds 已提交
39 40 41 42 43 44
	__asm__ __volatile__("stxa %0, [%1] %2" \
			     : /* no outputs */ \
			     : "r" (__val), "r" (__reg), \
			       "i" (ASI_PHYS_BYPASS_EC_E))

/* Must be invoked under the IOMMU lock. */
45
static void iommu_flushall(struct iommu *iommu)
L
Linus Torvalds 已提交
46
{
47
	if (iommu->iommu_flushinv) {
48
		iommu_write(iommu->iommu_flushinv, ~(u64)0);
49 50 51
	} else {
		unsigned long tag;
		int entry;
L
Linus Torvalds 已提交
52

53
		tag = iommu->iommu_tags;
54
		for (entry = 0; entry < 16; entry++) {
55
			iommu_write(tag, 0);
56 57
			tag += 8;
		}
L
Linus Torvalds 已提交
58

59
		/* Ensure completion of previous PIO writes. */
60
		(void) iommu_read(iommu->write_complete_reg);
61
	}
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
}

#define IOPTE_CONSISTENT(CTX) \
	(IOPTE_VALID | IOPTE_CACHE | \
	 (((CTX) << 47) & IOPTE_CONTEXT))

#define IOPTE_STREAMING(CTX) \
	(IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)

/* Existing mappings are never marked invalid, instead they
 * are pointed to a dummy page.
 */
#define IOPTE_IS_DUMMY(iommu, iopte)	\
	((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)

77
static inline void iopte_make_dummy(struct iommu *iommu, iopte_t *iopte)
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86
{
	unsigned long val = iopte_val(*iopte);

	val &= ~IOPTE_PAGE;
	val |= iommu->dummy_page_pa;

	iopte_val(*iopte) = val;
}

87 88 89 90 91 92 93 94 95 96 97
/* Based almost entirely upon the ppc64 iommu allocator.  If you use the 'handle'
 * facility it must all be done in one pass while under the iommu lock.
 *
 * On sun4u platforms, we only flush the IOMMU once every time we've passed
 * over the entire page table doing allocations.  Therefore we only ever advance
 * the hint and cannot backtrack it.
 */
unsigned long iommu_range_alloc(struct device *dev,
				struct iommu *iommu,
				unsigned long npages,
				unsigned long *handle)
98
{
99
	unsigned long n, end, start, limit, boundary_size;
100
	struct iommu_arena *arena = &iommu->arena;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	int pass = 0;

	/* This allocator was derived from x86_64's bit string search */

	/* Sanity check */
	if (unlikely(npages == 0)) {
		if (printk_ratelimit())
			WARN_ON(1);
		return DMA_ERROR_CODE;
	}

	if (handle && *handle)
		start = *handle;
	else
		start = arena->hint;
116 117 118

	limit = arena->limit;

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	/* The case below can happen if we have a small segment appended
	 * to a large, or when the previous alloc was at the very end of
	 * the available space. If so, go back to the beginning and flush.
	 */
	if (start >= limit) {
		start = 0;
		if (iommu->flush_all)
			iommu->flush_all(iommu);
	}

 again:

	if (dev)
		boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
				      1 << IO_PAGE_SHIFT);
	else
		boundary_size = ALIGN(1UL << 32, 1 << IO_PAGE_SHIFT);

137 138
	n = iommu_area_alloc(arena->map, limit, start, npages,
			     iommu->page_table_map_base >> IO_PAGE_SHIFT,
139 140
			     boundary_size >> IO_PAGE_SHIFT, 0);
	if (n == -1) {
141
		if (likely(pass < 1)) {
142
			/* First failure, rescan from the beginning.  */
143
			start = 0;
144 145
			if (iommu->flush_all)
				iommu->flush_all(iommu);
146 147 148
			pass++;
			goto again;
		} else {
149 150
			/* Second failure, give up */
			return DMA_ERROR_CODE;
151 152 153
		}
	}

154
	end = n + npages;
155 156 157

	arena->hint = end;

158 159 160 161
	/* Update handle for SG allocations */
	if (handle)
		*handle = end;

162 163 164
	return n;
}

165
void iommu_range_free(struct iommu *iommu, dma_addr_t dma_addr, unsigned long npages)
166
{
167 168
	struct iommu_arena *arena = &iommu->arena;
	unsigned long entry;
169

170 171 172
	entry = (dma_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;

	iommu_area_free(arena->map, entry, npages);
173 174
}

175 176
int iommu_table_init(struct iommu *iommu, int tsbsize,
		     u32 dma_offset, u32 dma_addr_mask)
L
Linus Torvalds 已提交
177
{
178 179 180
	unsigned long i, tsbbase, order, sz, num_tsb_entries;

	num_tsb_entries = tsbsize / sizeof(iopte_t);
181 182 183 184 185 186 187

	/* Setup initial software IOMMU state. */
	spin_lock_init(&iommu->lock);
	iommu->ctx_lowest_free = 1;
	iommu->page_table_map_base = dma_offset;
	iommu->dma_addr_mask = dma_addr_mask;

188 189 190
	/* Allocate and initialize the free area map.  */
	sz = num_tsb_entries / 8;
	sz = (sz + 7UL) & ~7UL;
191
	iommu->arena.map = kzalloc(sz, GFP_KERNEL);
192
	if (!iommu->arena.map) {
193 194
		printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
		return -ENOMEM;
195
	}
196
	iommu->arena.limit = num_tsb_entries;
L
Linus Torvalds 已提交
197

198 199 200
	if (tlb_type != hypervisor)
		iommu->flush_all = iommu_flushall;

201 202 203
	/* Allocate and initialize the dummy page which we
	 * set inactive IO PTEs to point to.
	 */
204
	iommu->dummy_page = get_zeroed_page(GFP_KERNEL);
205
	if (!iommu->dummy_page) {
206 207
		printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
		goto out_free_map;
208 209 210 211 212 213 214
	}
	iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);

	/* Now allocate and setup the IOMMU page table itself.  */
	order = get_order(tsbsize);
	tsbbase = __get_free_pages(GFP_KERNEL, order);
	if (!tsbbase) {
215 216
		printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n");
		goto out_free_dummy_page;
217 218
	}
	iommu->page_table = (iopte_t *)tsbbase;
L
Linus Torvalds 已提交
219

220
	for (i = 0; i < num_tsb_entries; i++)
L
Linus Torvalds 已提交
221
		iopte_make_dummy(iommu, &iommu->page_table[i]);
222 223 224 225 226 227 228 229 230 231 232 233

	return 0;

out_free_dummy_page:
	free_page(iommu->dummy_page);
	iommu->dummy_page = 0UL;

out_free_map:
	kfree(iommu->arena.map);
	iommu->arena.map = NULL;

	return -ENOMEM;
L
Linus Torvalds 已提交
234 235
}

236 237
static inline iopte_t *alloc_npages(struct device *dev, struct iommu *iommu,
				    unsigned long npages)
L
Linus Torvalds 已提交
238
{
239
	unsigned long entry;
L
Linus Torvalds 已提交
240

241 242
	entry = iommu_range_alloc(dev, iommu, npages, NULL);
	if (unlikely(entry == DMA_ERROR_CODE))
243
		return NULL;
L
Linus Torvalds 已提交
244

245
	return iommu->page_table + entry;
L
Linus Torvalds 已提交
246 247
}

248
static int iommu_alloc_ctx(struct iommu *iommu)
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
{
	int lowest = iommu->ctx_lowest_free;
	int sz = IOMMU_NUM_CTXS - lowest;
	int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);

	if (unlikely(n == sz)) {
		n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
		if (unlikely(n == lowest)) {
			printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
			n = 0;
		}
	}
	if (n)
		__set_bit(n, iommu->ctx_bitmap);

	return n;
}

267
static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
268 269 270 271 272 273 274 275
{
	if (likely(ctx)) {
		__clear_bit(ctx, iommu->ctx_bitmap);
		if (ctx < iommu->ctx_lowest_free)
			iommu->ctx_lowest_free = ctx;
	}
}

276 277
static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
				   dma_addr_t *dma_addrp, gfp_t gfp)
L
Linus Torvalds 已提交
278
{
279
	struct iommu *iommu;
L
Linus Torvalds 已提交
280
	iopte_t *iopte;
281
	unsigned long flags, order, first_page;
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289
	void *ret;
	int npages;

	size = IO_PAGE_ALIGN(size);
	order = get_order(size);
	if (order >= 10)
		return NULL;

290
	first_page = __get_free_pages(gfp, order);
L
Linus Torvalds 已提交
291 292 293 294
	if (first_page == 0UL)
		return NULL;
	memset((char *)first_page, 0, PAGE_SIZE << order);

295
	iommu = dev->archdata.iommu;
L
Linus Torvalds 已提交
296 297

	spin_lock_irqsave(&iommu->lock, flags);
298
	iopte = alloc_npages(dev, iommu, size >> IO_PAGE_SHIFT);
299 300 301
	spin_unlock_irqrestore(&iommu->lock, flags);

	if (unlikely(iopte == NULL)) {
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310 311
		free_pages(first_page, order);
		return NULL;
	}

	*dma_addrp = (iommu->page_table_map_base +
		      ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
	ret = (void *) first_page;
	npages = size >> IO_PAGE_SHIFT;
	first_page = __pa(first_page);
	while (npages--) {
312
		iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
L
Linus Torvalds 已提交
313 314 315 316 317 318 319 320 321
				     IOPTE_WRITE |
				     (first_page & IOPTE_PAGE));
		iopte++;
		first_page += IO_PAGE_SIZE;
	}

	return ret;
}

322 323
static void dma_4u_free_coherent(struct device *dev, size_t size,
				 void *cpu, dma_addr_t dvma)
L
Linus Torvalds 已提交
324
{
325
	struct iommu *iommu;
L
Linus Torvalds 已提交
326
	iopte_t *iopte;
327
	unsigned long flags, order, npages;
L
Linus Torvalds 已提交
328 329

	npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
330
	iommu = dev->archdata.iommu;
L
Linus Torvalds 已提交
331 332 333 334 335
	iopte = iommu->page_table +
		((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);

	spin_lock_irqsave(&iommu->lock, flags);

336
	iommu_range_free(iommu, dvma, npages);
337

L
Linus Torvalds 已提交
338 339 340 341 342 343 344
	spin_unlock_irqrestore(&iommu->lock, flags);

	order = get_order(size);
	if (order < 10)
		free_pages((unsigned long)cpu, order);
}

345 346
static dma_addr_t dma_4u_map_single(struct device *dev, void *ptr, size_t sz,
				    enum dma_data_direction direction)
L
Linus Torvalds 已提交
347
{
348 349
	struct iommu *iommu;
	struct strbuf *strbuf;
L
Linus Torvalds 已提交
350 351 352 353 354 355
	iopte_t *base;
	unsigned long flags, npages, oaddr;
	unsigned long i, base_paddr, ctx;
	u32 bus_addr, ret;
	unsigned long iopte_protection;

356 357
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;
L
Linus Torvalds 已提交
358

359
	if (unlikely(direction == DMA_NONE))
360
		goto bad_no_ctx;
L
Linus Torvalds 已提交
361 362 363 364 365 366

	oaddr = (unsigned long)ptr;
	npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
	npages >>= IO_PAGE_SHIFT;

	spin_lock_irqsave(&iommu->lock, flags);
367
	base = alloc_npages(dev, iommu, npages);
368 369 370 371
	ctx = 0;
	if (iommu->iommu_ctxflush)
		ctx = iommu_alloc_ctx(iommu);
	spin_unlock_irqrestore(&iommu->lock, flags);
L
Linus Torvalds 已提交
372

373
	if (unlikely(!base))
L
Linus Torvalds 已提交
374
		goto bad;
375

L
Linus Torvalds 已提交
376 377 378 379 380 381 382 383
	bus_addr = (iommu->page_table_map_base +
		    ((base - iommu->page_table) << IO_PAGE_SHIFT));
	ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
	base_paddr = __pa(oaddr & IO_PAGE_MASK);
	if (strbuf->strbuf_enabled)
		iopte_protection = IOPTE_STREAMING(ctx);
	else
		iopte_protection = IOPTE_CONSISTENT(ctx);
384
	if (direction != DMA_TO_DEVICE)
L
Linus Torvalds 已提交
385 386 387 388 389 390 391 392
		iopte_protection |= IOPTE_WRITE;

	for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
		iopte_val(*base) = iopte_protection | base_paddr;

	return ret;

bad:
393 394 395 396
	iommu_free_ctx(iommu, ctx);
bad_no_ctx:
	if (printk_ratelimit())
		WARN_ON(1);
397
	return DMA_ERROR_CODE;
L
Linus Torvalds 已提交
398 399
}

400 401 402
static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
			 u32 vaddr, unsigned long ctx, unsigned long npages,
			 enum dma_data_direction direction)
403 404 405 406 407 408
{
	int limit;

	if (strbuf->strbuf_ctxflush &&
	    iommu->iommu_ctxflush) {
		unsigned long matchreg, flushreg;
409
		u64 val;
410 411

		flushreg = strbuf->strbuf_ctxflush;
412
		matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
413

414 415
		iommu_write(flushreg, ctx);
		val = iommu_read(matchreg);
416 417
		val &= 0xffff;
		if (!val)
418 419 420 421
			goto do_flush_sync;

		while (val) {
			if (val & 0x1)
422
				iommu_write(flushreg, ctx);
423
			val >>= 1;
424
		}
425
		val = iommu_read(matchreg);
426
		if (unlikely(val)) {
427
			printk(KERN_WARNING "strbuf_flush: ctx flush "
428 429 430 431
			       "timeout matchreg[%lx] ctx[%lx]\n",
			       val, ctx);
			goto do_page_flush;
		}
432 433 434
	} else {
		unsigned long i;

435
	do_page_flush:
436
		for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
437
			iommu_write(strbuf->strbuf_pflush, vaddr);
438 439
	}

440 441 442 443 444
do_flush_sync:
	/* If the device could not have possibly put dirty data into
	 * the streaming cache, no flush-flag synchronization needs
	 * to be performed.
	 */
445
	if (direction == DMA_TO_DEVICE)
446 447
		return;

448 449 450
	STC_FLUSHFLAG_INIT(strbuf);
	iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
	(void) iommu_read(iommu->write_complete_reg);
451

452
	limit = 100000;
453
	while (!STC_FLUSHFLAG_SET(strbuf)) {
454 455 456
		limit--;
		if (!limit)
			break;
457
		udelay(1);
458
		rmb();
459 460
	}
	if (!limit)
461
		printk(KERN_WARNING "strbuf_flush: flushflag timeout "
462 463 464 465
		       "vaddr[%08x] ctx[%lx] npages[%ld]\n",
		       vaddr, ctx, npages);
}

466 467
static void dma_4u_unmap_single(struct device *dev, dma_addr_t bus_addr,
				size_t sz, enum dma_data_direction direction)
L
Linus Torvalds 已提交
468
{
469 470
	struct iommu *iommu;
	struct strbuf *strbuf;
L
Linus Torvalds 已提交
471
	iopte_t *base;
472
	unsigned long flags, npages, ctx, i;
L
Linus Torvalds 已提交
473

474
	if (unlikely(direction == DMA_NONE)) {
475 476 477 478
		if (printk_ratelimit())
			WARN_ON(1);
		return;
	}
L
Linus Torvalds 已提交
479

480 481
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;
L
Linus Torvalds 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496

	npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
	npages >>= IO_PAGE_SHIFT;
	base = iommu->page_table +
		((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
	bus_addr &= IO_PAGE_MASK;

	spin_lock_irqsave(&iommu->lock, flags);

	/* Record the context, if any. */
	ctx = 0;
	if (iommu->iommu_ctxflush)
		ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;

	/* Step 1: Kick data out of streaming buffers if necessary. */
497
	if (strbuf->strbuf_enabled)
498 499
		strbuf_flush(strbuf, iommu, bus_addr, ctx,
			     npages, direction);
L
Linus Torvalds 已提交
500

501 502 503
	/* Step 2: Clear out TSB entries. */
	for (i = 0; i < npages; i++)
		iopte_make_dummy(iommu, base + i);
L
Linus Torvalds 已提交
504

505
	iommu_range_free(iommu, bus_addr, npages);
L
Linus Torvalds 已提交
506

507 508
	iommu_free_ctx(iommu, ctx);

L
Linus Torvalds 已提交
509 510 511
	spin_unlock_irqrestore(&iommu->lock, flags);
}

512 513
static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
			 int nelems, enum dma_data_direction direction)
L
Linus Torvalds 已提交
514
{
515 516 517 518 519
	struct scatterlist *s, *outs, *segstart;
	unsigned long flags, handle, prot, ctx;
	dma_addr_t dma_next = 0, dma_addr;
	unsigned int max_seg_size;
	int outcount, incount, i;
520
	struct strbuf *strbuf;
521
	struct iommu *iommu;
522 523

	BUG_ON(direction == DMA_NONE);
L
Linus Torvalds 已提交
524

525 526
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;
527 528
	if (nelems == 0 || !iommu)
		return 0;
L
Linus Torvalds 已提交
529 530 531

	spin_lock_irqsave(&iommu->lock, flags);

532 533 534 535
	ctx = 0;
	if (iommu->iommu_ctxflush)
		ctx = iommu_alloc_ctx(iommu);

L
Linus Torvalds 已提交
536
	if (strbuf->strbuf_enabled)
537
		prot = IOPTE_STREAMING(ctx);
L
Linus Torvalds 已提交
538
	else
539
		prot = IOPTE_CONSISTENT(ctx);
540
	if (direction != DMA_TO_DEVICE)
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
		prot |= IOPTE_WRITE;

	outs = s = segstart = &sglist[0];
	outcount = 1;
	incount = nelems;
	handle = 0;

	/* Init first segment length for backout at failure */
	outs->dma_length = 0;

	max_seg_size = dma_get_max_seg_size(dev);
	for_each_sg(sglist, s, nelems, i) {
		unsigned long paddr, npages, entry, slen;
		iopte_t *base;

		slen = s->length;
		/* Sanity check */
		if (slen == 0) {
			dma_next = 0;
			continue;
		}
		/* Allocate iommu entries for that segment */
		paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
		npages = iommu_num_pages(paddr, slen);
		entry = iommu_range_alloc(dev, iommu, npages, &handle);

		/* Handle failure */
		if (unlikely(entry == DMA_ERROR_CODE)) {
			if (printk_ratelimit())
				printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
				       " npages %lx\n", iommu, paddr, npages);
			goto iommu_map_failed;
		}
574

575
		base = iommu->page_table + entry;
L
Linus Torvalds 已提交
576

577 578 579 580
		/* Convert entry to a dma_addr_t */
		dma_addr = iommu->page_table_map_base +
			(entry << IO_PAGE_SHIFT);
		dma_addr |= (s->offset & ~IO_PAGE_MASK);
581

582
		/* Insert into HW table */
583
		paddr &= IO_PAGE_MASK;
584 585
		while (npages--) {
			iopte_val(*base) = prot | paddr;
586 587 588
			base++;
			paddr += IO_PAGE_SIZE;
		}
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

		/* If we are in an open segment, try merging */
		if (segstart != s) {
			/* We cannot merge if:
			 * - allocated dma_addr isn't contiguous to previous allocation
			 */
			if ((dma_addr != dma_next) ||
			    (outs->dma_length + s->length > max_seg_size)) {
				/* Can't merge: create a new segment */
				segstart = s;
				outcount++;
				outs = sg_next(outs);
			} else {
				outs->dma_length += s->length;
			}
		}

		if (segstart == s) {
			/* This is a new segment, fill entries */
			outs->dma_address = dma_addr;
			outs->dma_length = slen;
		}

		/* Calculate next page pointer for contiguous check */
		dma_next = dma_addr + slen;
614 615
	}

616 617 618 619 620 621 622 623 624 625 626 627 628
	spin_unlock_irqrestore(&iommu->lock, flags);

	if (outcount < incount) {
		outs = sg_next(outs);
		outs->dma_address = DMA_ERROR_CODE;
		outs->dma_length = 0;
	}

	return outcount;

iommu_map_failed:
	for_each_sg(sglist, s, nelems, i) {
		if (s->dma_length != 0) {
629
			unsigned long vaddr, npages, entry, j;
630 631 632 633 634 635 636 637 638 639
			iopte_t *base;

			vaddr = s->dma_address & IO_PAGE_MASK;
			npages = iommu_num_pages(s->dma_address, s->dma_length);
			iommu_range_free(iommu, vaddr, npages);

			entry = (vaddr - iommu->page_table_map_base)
				>> IO_PAGE_SHIFT;
			base = iommu->page_table + entry;

640 641
			for (j = 0; j < npages; j++)
				iopte_make_dummy(iommu, base + j);
642 643 644 645 646 647 648 649

			s->dma_address = DMA_ERROR_CODE;
			s->dma_length = 0;
		}
		if (s == outs)
			break;
	}
	spin_unlock_irqrestore(&iommu->lock, flags);
L
Linus Torvalds 已提交
650

651
	return 0;
L
Linus Torvalds 已提交
652 653
}

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
/* If contexts are being used, they are the same in all of the mappings
 * we make for a particular SG.
 */
static unsigned long fetch_sg_ctx(struct iommu *iommu, struct scatterlist *sg)
{
	unsigned long ctx = 0;

	if (iommu->iommu_ctxflush) {
		iopte_t *base;
		u32 bus_addr;

		bus_addr = sg->dma_address & IO_PAGE_MASK;
		base = iommu->page_table +
			((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);

		ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
	}
	return ctx;
}

674 675
static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
			    int nelems, enum dma_data_direction direction)
L
Linus Torvalds 已提交
676
{
677 678
	unsigned long flags, ctx;
	struct scatterlist *sg;
679
	struct strbuf *strbuf;
680
	struct iommu *iommu;
L
Linus Torvalds 已提交
681

682
	BUG_ON(direction == DMA_NONE);
L
Linus Torvalds 已提交
683

684 685 686
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;

687
	ctx = fetch_sg_ctx(iommu, sglist);
L
Linus Torvalds 已提交
688

689
	spin_lock_irqsave(&iommu->lock, flags);
L
Linus Torvalds 已提交
690

691 692 693 694 695 696 697
	sg = sglist;
	while (nelems--) {
		dma_addr_t dma_handle = sg->dma_address;
		unsigned int len = sg->dma_length;
		unsigned long npages, entry;
		iopte_t *base;
		int i;
L
Linus Torvalds 已提交
698

699 700 701 702
		if (!len)
			break;
		npages = iommu_num_pages(dma_handle, len);
		iommu_range_free(iommu, dma_handle, npages);
L
Linus Torvalds 已提交
703

704 705 706
		entry = ((dma_handle - iommu->page_table_map_base)
			 >> IO_PAGE_SHIFT);
		base = iommu->page_table + entry;
L
Linus Torvalds 已提交
707

708 709 710 711
		dma_handle &= IO_PAGE_MASK;
		if (strbuf->strbuf_enabled)
			strbuf_flush(strbuf, iommu, dma_handle, ctx,
				     npages, direction);
L
Linus Torvalds 已提交
712

713 714
		for (i = 0; i < npages; i++)
			iopte_make_dummy(iommu, base + i);
L
Linus Torvalds 已提交
715

716 717
		sg = sg_next(sg);
	}
L
Linus Torvalds 已提交
718

719 720
	iommu_free_ctx(iommu, ctx);

L
Linus Torvalds 已提交
721 722 723
	spin_unlock_irqrestore(&iommu->lock, flags);
}

724 725 726
static void dma_4u_sync_single_for_cpu(struct device *dev,
				       dma_addr_t bus_addr, size_t sz,
				       enum dma_data_direction direction)
L
Linus Torvalds 已提交
727
{
728 729
	struct iommu *iommu;
	struct strbuf *strbuf;
L
Linus Torvalds 已提交
730 731
	unsigned long flags, ctx, npages;

732 733
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;
L
Linus Torvalds 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

	if (!strbuf->strbuf_enabled)
		return;

	spin_lock_irqsave(&iommu->lock, flags);

	npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
	npages >>= IO_PAGE_SHIFT;
	bus_addr &= IO_PAGE_MASK;

	/* Step 1: Record the context, if any. */
	ctx = 0;
	if (iommu->iommu_ctxflush &&
	    strbuf->strbuf_ctxflush) {
		iopte_t *iopte;

		iopte = iommu->page_table +
			((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
		ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
	}

	/* Step 2: Kick data out of streaming buffers. */
756
	strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
L
Linus Torvalds 已提交
757 758 759 760

	spin_unlock_irqrestore(&iommu->lock, flags);
}

761 762 763
static void dma_4u_sync_sg_for_cpu(struct device *dev,
				   struct scatterlist *sglist, int nelems,
				   enum dma_data_direction direction)
L
Linus Torvalds 已提交
764
{
765 766
	struct iommu *iommu;
	struct strbuf *strbuf;
767
	unsigned long flags, ctx, npages, i;
J
Jens Axboe 已提交
768
	struct scatterlist *sg, *sgprv;
769
	u32 bus_addr;
L
Linus Torvalds 已提交
770

771 772
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;
L
Linus Torvalds 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790

	if (!strbuf->strbuf_enabled)
		return;

	spin_lock_irqsave(&iommu->lock, flags);

	/* Step 1: Record the context, if any. */
	ctx = 0;
	if (iommu->iommu_ctxflush &&
	    strbuf->strbuf_ctxflush) {
		iopte_t *iopte;

		iopte = iommu->page_table +
			((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
		ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
	}

	/* Step 2: Kick data out of streaming buffers. */
791
	bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
J
Jens Axboe 已提交
792 793 794
	sgprv = NULL;
	for_each_sg(sglist, sg, nelems, i) {
		if (sg->dma_length == 0)
795
			break;
J
Jens Axboe 已提交
796 797 798 799
		sgprv = sg;
	}

	npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
800
		  - bus_addr) >> IO_PAGE_SHIFT;
801
	strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
L
Linus Torvalds 已提交
802 803 804 805

	spin_unlock_irqrestore(&iommu->lock, flags);
}

806
static const struct dma_ops sun4u_dma_ops = {
807 808 809 810 811 812 813 814
	.alloc_coherent		= dma_4u_alloc_coherent,
	.free_coherent		= dma_4u_free_coherent,
	.map_single		= dma_4u_map_single,
	.unmap_single		= dma_4u_unmap_single,
	.map_sg			= dma_4u_map_sg,
	.unmap_sg		= dma_4u_unmap_sg,
	.sync_single_for_cpu	= dma_4u_sync_single_for_cpu,
	.sync_sg_for_cpu	= dma_4u_sync_sg_for_cpu,
815 816
};

817 818
const struct dma_ops *dma_ops = &sun4u_dma_ops;
EXPORT_SYMBOL(dma_ops);
L
Linus Torvalds 已提交
819

820
int dma_supported(struct device *dev, u64 device_mask)
L
Linus Torvalds 已提交
821
{
822 823
	struct iommu *iommu = dev->archdata.iommu;
	u64 dma_addr_mask = iommu->dma_addr_mask;
L
Linus Torvalds 已提交
824

825 826
	if (device_mask >= (1UL << 32UL))
		return 0;
L
Linus Torvalds 已提交
827

828 829
	if ((device_mask & dma_addr_mask) == dma_addr_mask)
		return 1;
L
Linus Torvalds 已提交
830

831 832 833 834
#ifdef CONFIG_PCI
	if (dev->bus == &pci_bus_type)
		return pci_dma_supported(to_pci_dev(dev), device_mask);
#endif
L
Linus Torvalds 已提交
835

836 837 838
	return 0;
}
EXPORT_SYMBOL(dma_supported);
L
Linus Torvalds 已提交
839

840 841 842 843 844 845 846
int dma_set_mask(struct device *dev, u64 dma_mask)
{
#ifdef CONFIG_PCI
	if (dev->bus == &pci_bus_type)
		return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
#endif
	return -EINVAL;
L
Linus Torvalds 已提交
847
}
848
EXPORT_SYMBOL(dma_set_mask);