iommu.c 19.3 KB
Newer Older
1
/* iommu.c: Generic sparc64 IOMMU support.
L
Linus Torvalds 已提交
2
 *
3
 * Copyright (C) 1999, 2007 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 13 14
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/errno.h>

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

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

#include "iommu_common.h"

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

29
#define iommu_read(__reg) \
L
Linus Torvalds 已提交
30 31 32 33 34 35 36
({	u64 __ret; \
	__asm__ __volatile__("ldxa [%1] %2, %0" \
			     : "=r" (__ret) \
			     : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
			     : "memory"); \
	__ret; \
})
37
#define iommu_write(__reg, __val) \
L
Linus Torvalds 已提交
38 39 40 41 42 43
	__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. */
44
static void __iommu_flushall(struct iommu *iommu)
L
Linus Torvalds 已提交
45
{
46
	if (iommu->iommu_flushinv) {
47
		iommu_write(iommu->iommu_flushinv, ~(u64)0);
48 49 50
	} else {
		unsigned long tag;
		int entry;
L
Linus Torvalds 已提交
51

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

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

#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)

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

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

	iopte_val(*iopte) = val;
}

86
/* Based largely upon the ppc64 iommu allocator.  */
87
static long arena_alloc(struct iommu *iommu, unsigned long npages)
88
{
89
	struct iommu_arena *arena = &iommu->arena;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	unsigned long n, i, start, end, limit;
	int pass;

	limit = arena->limit;
	start = arena->hint;
	pass = 0;

again:
	n = find_next_zero_bit(arena->map, limit, start);
	end = n + npages;
	if (unlikely(end >= limit)) {
		if (likely(pass < 1)) {
			limit = start;
			start = 0;
			__iommu_flushall(iommu);
			pass++;
			goto again;
		} else {
			/* Scanned the whole thing, give up. */
			return -1;
		}
	}

	for (i = n; i < end; i++) {
		if (test_bit(i, arena->map)) {
			start = i + 1;
			goto again;
		}
	}

	for (i = n; i < end; i++)
		__set_bit(i, arena->map);

	arena->hint = end;

	return n;
}

128
static void arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages)
129 130 131 132 133 134 135
{
	unsigned long i;

	for (i = base; i < (base + npages); i++)
		__clear_bit(i, arena->map);
}

136 137
int iommu_table_init(struct iommu *iommu, int tsbsize,
		     u32 dma_offset, u32 dma_addr_mask)
L
Linus Torvalds 已提交
138
{
139 140 141
	unsigned long i, tsbbase, order, sz, num_tsb_entries;

	num_tsb_entries = tsbsize / sizeof(iopte_t);
142 143 144 145 146 147 148

	/* 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;

149 150 151
	/* Allocate and initialize the free area map.  */
	sz = num_tsb_entries / 8;
	sz = (sz + 7UL) & ~7UL;
152
	iommu->arena.map = kzalloc(sz, GFP_KERNEL);
153
	if (!iommu->arena.map) {
154 155
		printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
		return -ENOMEM;
156
	}
157
	iommu->arena.limit = num_tsb_entries;
L
Linus Torvalds 已提交
158

159 160 161 162 163
	/* Allocate and initialize the dummy page which we
	 * set inactive IO PTEs to point to.
	 */
	iommu->dummy_page = __get_free_pages(GFP_KERNEL, 0);
	if (!iommu->dummy_page) {
164 165
		printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
		goto out_free_map;
166 167 168 169 170 171 172 173
	}
	memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
	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) {
174 175
		printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n");
		goto out_free_dummy_page;
176 177
	}
	iommu->page_table = (iopte_t *)tsbbase;
L
Linus Torvalds 已提交
178

179
	for (i = 0; i < num_tsb_entries; i++)
L
Linus Torvalds 已提交
180
		iopte_make_dummy(iommu, &iommu->page_table[i]);
181 182 183 184 185 186 187 188 189 190 191 192

	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 已提交
193 194
}

195
static inline iopte_t *alloc_npages(struct iommu *iommu, unsigned long npages)
L
Linus Torvalds 已提交
196
{
197
	long entry;
L
Linus Torvalds 已提交
198

199
	entry = arena_alloc(iommu, npages);
200 201
	if (unlikely(entry < 0))
		return NULL;
L
Linus Torvalds 已提交
202

203
	return iommu->page_table + entry;
L
Linus Torvalds 已提交
204 205
}

206
static inline void free_npages(struct iommu *iommu, dma_addr_t base, unsigned long npages)
L
Linus Torvalds 已提交
207
{
208
	arena_free(&iommu->arena, base >> IO_PAGE_SHIFT, npages);
L
Linus Torvalds 已提交
209 210
}

211
static int iommu_alloc_ctx(struct iommu *iommu)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
{
	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;
}

230
static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
231 232 233 234 235 236 237 238
{
	if (likely(ctx)) {
		__clear_bit(ctx, iommu->ctx_bitmap);
		if (ctx < iommu->ctx_lowest_free)
			iommu->ctx_lowest_free = ctx;
	}
}

239 240
static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
				   dma_addr_t *dma_addrp, gfp_t gfp)
L
Linus Torvalds 已提交
241
{
242
	struct iommu *iommu;
L
Linus Torvalds 已提交
243
	iopte_t *iopte;
244
	unsigned long flags, order, first_page;
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252
	void *ret;
	int npages;

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

253
	first_page = __get_free_pages(gfp, order);
L
Linus Torvalds 已提交
254 255 256 257
	if (first_page == 0UL)
		return NULL;
	memset((char *)first_page, 0, PAGE_SIZE << order);

258
	iommu = dev->archdata.iommu;
L
Linus Torvalds 已提交
259 260

	spin_lock_irqsave(&iommu->lock, flags);
261 262 263 264
	iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT);
	spin_unlock_irqrestore(&iommu->lock, flags);

	if (unlikely(iopte == NULL)) {
L
Linus Torvalds 已提交
265 266 267 268 269 270 271 272 273 274
		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--) {
275
		iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
L
Linus Torvalds 已提交
276 277 278 279 280 281 282 283 284
				     IOPTE_WRITE |
				     (first_page & IOPTE_PAGE));
		iopte++;
		first_page += IO_PAGE_SIZE;
	}

	return ret;
}

285 286
static void dma_4u_free_coherent(struct device *dev, size_t size,
				 void *cpu, dma_addr_t dvma)
L
Linus Torvalds 已提交
287
{
288
	struct iommu *iommu;
L
Linus Torvalds 已提交
289
	iopte_t *iopte;
290
	unsigned long flags, order, npages;
L
Linus Torvalds 已提交
291 292

	npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
293
	iommu = dev->archdata.iommu;
L
Linus Torvalds 已提交
294 295 296 297 298
	iopte = iommu->page_table +
		((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);

	spin_lock_irqsave(&iommu->lock, flags);

299
	free_npages(iommu, dvma - iommu->page_table_map_base, npages);
300

L
Linus Torvalds 已提交
301 302 303 304 305 306 307
	spin_unlock_irqrestore(&iommu->lock, flags);

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

308 309
static dma_addr_t dma_4u_map_single(struct device *dev, void *ptr, size_t sz,
				    enum dma_data_direction direction)
L
Linus Torvalds 已提交
310
{
311 312
	struct iommu *iommu;
	struct strbuf *strbuf;
L
Linus Torvalds 已提交
313 314 315 316 317 318
	iopte_t *base;
	unsigned long flags, npages, oaddr;
	unsigned long i, base_paddr, ctx;
	u32 bus_addr, ret;
	unsigned long iopte_protection;

319 320
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;
L
Linus Torvalds 已提交
321

322
	if (unlikely(direction == DMA_NONE))
323
		goto bad_no_ctx;
L
Linus Torvalds 已提交
324 325 326 327 328 329

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

	spin_lock_irqsave(&iommu->lock, flags);
330 331 332 333 334
	base = alloc_npages(iommu, npages);
	ctx = 0;
	if (iommu->iommu_ctxflush)
		ctx = iommu_alloc_ctx(iommu);
	spin_unlock_irqrestore(&iommu->lock, flags);
L
Linus Torvalds 已提交
335

336
	if (unlikely(!base))
L
Linus Torvalds 已提交
337
		goto bad;
338

L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346
	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);
347
	if (direction != DMA_TO_DEVICE)
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355
		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:
356 357 358 359
	iommu_free_ctx(iommu, ctx);
bad_no_ctx:
	if (printk_ratelimit())
		WARN_ON(1);
360
	return DMA_ERROR_CODE;
L
Linus Torvalds 已提交
361 362
}

363 364 365
static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
			 u32 vaddr, unsigned long ctx, unsigned long npages,
			 enum dma_data_direction direction)
366 367 368 369 370 371
{
	int limit;

	if (strbuf->strbuf_ctxflush &&
	    iommu->iommu_ctxflush) {
		unsigned long matchreg, flushreg;
372
		u64 val;
373 374

		flushreg = strbuf->strbuf_ctxflush;
375
		matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
376

377 378
		iommu_write(flushreg, ctx);
		val = iommu_read(matchreg);
379 380
		val &= 0xffff;
		if (!val)
381 382 383 384
			goto do_flush_sync;

		while (val) {
			if (val & 0x1)
385
				iommu_write(flushreg, ctx);
386
			val >>= 1;
387
		}
388
		val = iommu_read(matchreg);
389
		if (unlikely(val)) {
390
			printk(KERN_WARNING "strbuf_flush: ctx flush "
391 392 393 394
			       "timeout matchreg[%lx] ctx[%lx]\n",
			       val, ctx);
			goto do_page_flush;
		}
395 396 397
	} else {
		unsigned long i;

398
	do_page_flush:
399
		for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
400
			iommu_write(strbuf->strbuf_pflush, vaddr);
401 402
	}

403 404 405 406 407
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.
	 */
408
	if (direction == DMA_TO_DEVICE)
409 410
		return;

411 412 413
	STC_FLUSHFLAG_INIT(strbuf);
	iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
	(void) iommu_read(iommu->write_complete_reg);
414

415
	limit = 100000;
416
	while (!STC_FLUSHFLAG_SET(strbuf)) {
417 418 419
		limit--;
		if (!limit)
			break;
420
		udelay(1);
421
		rmb();
422 423
	}
	if (!limit)
424
		printk(KERN_WARNING "strbuf_flush: flushflag timeout "
425 426 427 428
		       "vaddr[%08x] ctx[%lx] npages[%ld]\n",
		       vaddr, ctx, npages);
}

429 430
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 已提交
431
{
432 433
	struct iommu *iommu;
	struct strbuf *strbuf;
L
Linus Torvalds 已提交
434
	iopte_t *base;
435
	unsigned long flags, npages, ctx, i;
L
Linus Torvalds 已提交
436

437
	if (unlikely(direction == DMA_NONE)) {
438 439 440 441
		if (printk_ratelimit())
			WARN_ON(1);
		return;
	}
L
Linus Torvalds 已提交
442

443 444
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;
L
Linus Torvalds 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

	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. */
460
	if (strbuf->strbuf_enabled)
461 462
		strbuf_flush(strbuf, iommu, bus_addr, ctx,
			     npages, direction);
L
Linus Torvalds 已提交
463

464 465 466
	/* Step 2: Clear out TSB entries. */
	for (i = 0; i < npages; i++)
		iopte_make_dummy(iommu, base + i);
L
Linus Torvalds 已提交
467

468
	free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
L
Linus Torvalds 已提交
469

470 471
	iommu_free_ctx(iommu, ctx);

L
Linus Torvalds 已提交
472 473 474
	spin_unlock_irqrestore(&iommu->lock, flags);
}

J
Jens Axboe 已提交
475
#define SG_ENT_PHYS_ADDRESS(SG)	(__pa(sg_virt((SG))))
L
Linus Torvalds 已提交
476

477 478 479
static void fill_sg(iopte_t *iopte, struct scatterlist *sg,
		    int nused, int nelems,
		    unsigned long iopte_protection)
L
Linus Torvalds 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
{
	struct scatterlist *dma_sg = sg;
	int i;

	for (i = 0; i < nused; i++) {
		unsigned long pteval = ~0UL;
		u32 dma_npages;

		dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
			      dma_sg->dma_length +
			      ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
		do {
			unsigned long offset;
			signed int len;

			/* If we are here, we know we have at least one
			 * more page to map.  So walk forward until we
			 * hit a page crossing, and begin creating new
			 * mappings from that spot.
			 */
			for (;;) {
				unsigned long tmp;

				tmp = SG_ENT_PHYS_ADDRESS(sg);
				len = sg->length;
				if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
					pteval = tmp & IO_PAGE_MASK;
					offset = tmp & (IO_PAGE_SIZE - 1UL);
					break;
				}
				if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
					pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
					offset = 0UL;
					len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
					break;
				}
J
Jens Axboe 已提交
516
				sg = sg_next(sg);
517
				nelems--;
L
Linus Torvalds 已提交
518 519 520 521 522 523 524 525 526 527 528 529
			}

			pteval = iopte_protection | (pteval & IOPTE_PAGE);
			while (len > 0) {
				*iopte++ = __iopte(pteval);
				pteval += IO_PAGE_SIZE;
				len -= (IO_PAGE_SIZE - offset);
				offset = 0;
				dma_npages--;
			}

			pteval = (pteval & IOPTE_PAGE) + len;
J
Jens Axboe 已提交
530
			sg = sg_next(sg);
531
			nelems--;
L
Linus Torvalds 已提交
532 533 534 535 536

			/* Skip over any tail mappings we've fully mapped,
			 * adjusting pteval along the way.  Stop when we
			 * detect a page crossing event.
			 */
537
			while (nelems &&
L
Linus Torvalds 已提交
538 539 540 541 542
			       (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
			       (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
			       ((pteval ^
				 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
				pteval += sg->length;
J
Jens Axboe 已提交
543
				sg = sg_next(sg);
544
				nelems--;
L
Linus Torvalds 已提交
545 546 547 548
			}
			if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
				pteval = ~0UL;
		} while (dma_npages != 0);
J
Jens Axboe 已提交
549
		dma_sg = sg_next(dma_sg);
L
Linus Torvalds 已提交
550 551 552
	}
}

553 554
static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
			 int nelems, enum dma_data_direction direction)
L
Linus Torvalds 已提交
555
{
556 557
	struct iommu *iommu;
	struct strbuf *strbuf;
L
Linus Torvalds 已提交
558 559 560 561 562 563 564 565 566
	unsigned long flags, ctx, npages, iopte_protection;
	iopte_t *base;
	u32 dma_base;
	struct scatterlist *sgtmp;
	int used;

	/* Fast path single entry scatterlists. */
	if (nelems == 1) {
		sglist->dma_address =
J
Jens Axboe 已提交
567
			dma_4u_map_single(dev, sg_virt(sglist),
568
					  sglist->length, direction);
569
		if (unlikely(sglist->dma_address == DMA_ERROR_CODE))
570
			return 0;
L
Linus Torvalds 已提交
571 572 573 574
		sglist->dma_length = sglist->length;
		return 1;
	}

575 576 577 578
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;

	if (unlikely(direction == DMA_NONE))
579
		goto bad_no_ctx;
L
Linus Torvalds 已提交
580 581 582

	/* Step 1: Prepare scatter list. */

583
	npages = prepare_sg(dev, sglist, nelems);
L
Linus Torvalds 已提交
584

585
	/* Step 2: Allocate a cluster and context, if necessary. */
L
Linus Torvalds 已提交
586 587 588

	spin_lock_irqsave(&iommu->lock, flags);

589 590 591 592 593 594 595
	base = alloc_npages(iommu, npages);
	ctx = 0;
	if (iommu->iommu_ctxflush)
		ctx = iommu_alloc_ctx(iommu);

	spin_unlock_irqrestore(&iommu->lock, flags);

L
Linus Torvalds 已提交
596 597
	if (base == NULL)
		goto bad;
598 599 600

	dma_base = iommu->page_table_map_base +
		((base - iommu->page_table) << IO_PAGE_SHIFT);
L
Linus Torvalds 已提交
601 602 603 604 605 606 607

	/* Step 3: Normalize DMA addresses. */
	used = nelems;

	sgtmp = sglist;
	while (used && sgtmp->dma_length) {
		sgtmp->dma_address += dma_base;
J
Jens Axboe 已提交
608
		sgtmp = sg_next(sgtmp);
L
Linus Torvalds 已提交
609 610 611 612
		used--;
	}
	used = nelems - used;

613
	/* Step 4: Create the mappings. */
L
Linus Torvalds 已提交
614 615 616 617
	if (strbuf->strbuf_enabled)
		iopte_protection = IOPTE_STREAMING(ctx);
	else
		iopte_protection = IOPTE_CONSISTENT(ctx);
618
	if (direction != DMA_TO_DEVICE)
L
Linus Torvalds 已提交
619
		iopte_protection |= IOPTE_WRITE;
620 621 622

	fill_sg(base, sglist, used, nelems, iopte_protection);

L
Linus Torvalds 已提交
623 624 625 626 627 628 629
#ifdef VERIFY_SG
	verify_sglist(sglist, nelems, base, npages);
#endif

	return used;

bad:
630 631 632 633 634
	iommu_free_ctx(iommu, ctx);
bad_no_ctx:
	if (printk_ratelimit())
		WARN_ON(1);
	return 0;
L
Linus Torvalds 已提交
635 636
}

637 638
static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
			    int nelems, enum dma_data_direction direction)
L
Linus Torvalds 已提交
639
{
640 641
	struct iommu *iommu;
	struct strbuf *strbuf;
L
Linus Torvalds 已提交
642 643
	iopte_t *base;
	unsigned long flags, ctx, i, npages;
J
Jens Axboe 已提交
644
	struct scatterlist *sg, *sgprv;
L
Linus Torvalds 已提交
645 646
	u32 bus_addr;

647
	if (unlikely(direction == DMA_NONE)) {
648 649 650
		if (printk_ratelimit())
			WARN_ON(1);
	}
L
Linus Torvalds 已提交
651

652 653 654
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;

L
Linus Torvalds 已提交
655 656
	bus_addr = sglist->dma_address & IO_PAGE_MASK;

J
Jens Axboe 已提交
657 658 659
	sgprv = NULL;
	for_each_sg(sglist, sg, nelems, i) {
		if (sg->dma_length == 0)
L
Linus Torvalds 已提交
660
			break;
J
Jens Axboe 已提交
661 662 663 664
		sgprv = sg;
	}

	npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length) -
665
		  bus_addr) >> IO_PAGE_SHIFT;
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674 675 676 677

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

	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. */
678
	if (strbuf->strbuf_enabled)
679
		strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
L
Linus Torvalds 已提交
680

681 682 683
	/* Step 2: Clear out the TSB entries. */
	for (i = 0; i < npages; i++)
		iopte_make_dummy(iommu, base + i);
L
Linus Torvalds 已提交
684

685
	free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
L
Linus Torvalds 已提交
686

687 688
	iommu_free_ctx(iommu, ctx);

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

692 693 694
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 已提交
695
{
696 697
	struct iommu *iommu;
	struct strbuf *strbuf;
L
Linus Torvalds 已提交
698 699
	unsigned long flags, ctx, npages;

700 701
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;
L
Linus Torvalds 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723

	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. */
724
	strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
L
Linus Torvalds 已提交
725 726 727 728

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

729 730 731
static void dma_4u_sync_sg_for_cpu(struct device *dev,
				   struct scatterlist *sglist, int nelems,
				   enum dma_data_direction direction)
L
Linus Torvalds 已提交
732
{
733 734
	struct iommu *iommu;
	struct strbuf *strbuf;
735
	unsigned long flags, ctx, npages, i;
J
Jens Axboe 已提交
736
	struct scatterlist *sg, *sgprv;
737
	u32 bus_addr;
L
Linus Torvalds 已提交
738

739 740
	iommu = dev->archdata.iommu;
	strbuf = dev->archdata.stc;
L
Linus Torvalds 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758

	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. */
759
	bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
J
Jens Axboe 已提交
760 761 762
	sgprv = NULL;
	for_each_sg(sglist, sg, nelems, i) {
		if (sg->dma_length == 0)
763
			break;
J
Jens Axboe 已提交
764 765 766 767
		sgprv = sg;
	}

	npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
768
		  - bus_addr) >> IO_PAGE_SHIFT;
769
	strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
L
Linus Torvalds 已提交
770 771 772 773

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

774 775 776 777 778 779 780 781 782
const struct dma_ops sun4u_dma_ops = {
	.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,
783 784
};

785 786
const struct dma_ops *dma_ops = &sun4u_dma_ops;
EXPORT_SYMBOL(dma_ops);
L
Linus Torvalds 已提交
787

788
int dma_supported(struct device *dev, u64 device_mask)
L
Linus Torvalds 已提交
789
{
790 791
	struct iommu *iommu = dev->archdata.iommu;
	u64 dma_addr_mask = iommu->dma_addr_mask;
L
Linus Torvalds 已提交
792

793 794
	if (device_mask >= (1UL << 32UL))
		return 0;
L
Linus Torvalds 已提交
795

796 797
	if ((device_mask & dma_addr_mask) == dma_addr_mask)
		return 1;
L
Linus Torvalds 已提交
798

799 800 801 802
#ifdef CONFIG_PCI
	if (dev->bus == &pci_bus_type)
		return pci_dma_supported(to_pci_dev(dev), device_mask);
#endif
L
Linus Torvalds 已提交
803

804 805 806
	return 0;
}
EXPORT_SYMBOL(dma_supported);
L
Linus Torvalds 已提交
807

808 809 810 811 812 813 814
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 已提交
815
}
816
EXPORT_SYMBOL(dma_set_mask);