iommu_common.c 5.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* $Id: iommu_common.c,v 1.9 2001/12/17 07:05:09 davem Exp $
 * iommu_common.c: UltraSparc SBUS/PCI common iommu code.
 *
 * Copyright (C) 1999 David S. Miller (davem@redhat.com)
 */

#include "iommu_common.h"

/* You are _strongly_ advised to enable the following debugging code
 * any time you make changes to the sg code below, run it for a while
 * with filesystems mounted read-only before buying the farm... -DaveM
 */

#ifdef VERIFY_SG
F
FUJITA Tomonori 已提交
15
static int verify_lengths(struct scatterlist *sglist, int nents, int npages)
L
Linus Torvalds 已提交
16 17 18
{
	int sg_len, dma_len;
	int i, pgcount;
F
FUJITA Tomonori 已提交
19
	struct scatterlist *sg;
L
Linus Torvalds 已提交
20 21

	sg_len = 0;
F
FUJITA Tomonori 已提交
22 23
	for_each_sg(sglist, sg, nents, i)
		sg_len += sg->length;
L
Linus Torvalds 已提交
24 25

	dma_len = 0;
F
FUJITA Tomonori 已提交
26 27 28 29 30
	for_each_sg(sglist, sg, nents, i) {
		if (!sg->dma_length)
			break;
		dma_len += sg->dma_length;
	}
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38

	if (sg_len != dma_len) {
		printk("verify_lengths: Error, different, sg[%d] dma[%d]\n",
		       sg_len, dma_len);
		return -1;
	}

	pgcount = 0;
F
FUJITA Tomonori 已提交
39
	for_each_sg(sglist, sg, nents, i) {
L
Linus Torvalds 已提交
40 41
		unsigned long start, end;

F
FUJITA Tomonori 已提交
42 43 44 45
		if (!sg->dma_length)
			break;

		start = sg->dma_address;
L
Linus Torvalds 已提交
46 47
		start = start & IO_PAGE_MASK;

F
FUJITA Tomonori 已提交
48
		end = sg->dma_address + sg->dma_length;
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
		end = (end + (IO_PAGE_SIZE - 1)) & IO_PAGE_MASK;

		pgcount += ((end - start) >> IO_PAGE_SHIFT);
	}

	if (pgcount != npages) {
		printk("verify_lengths: Error, page count wrong, "
		       "npages[%d] pgcount[%d]\n",
		       npages, pgcount);
		return -1;
	}

	/* This test passes... */
	return 0;
}

static int verify_one_map(struct scatterlist *dma_sg, struct scatterlist **__sg, int nents, iopte_t **__iopte)
{
	struct scatterlist *sg = *__sg;
	iopte_t *iopte = *__iopte;
	u32 dlen = dma_sg->dma_length;
	u32 daddr;
	unsigned int sglen;
	unsigned long sgaddr;

	daddr = dma_sg->dma_address;
	sglen = sg->length;
J
Jens Axboe 已提交
76
	sgaddr = (unsigned long) sg_virt(sg);
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 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
	while (dlen > 0) {
		unsigned long paddr;

		/* SG and DMA_SG must begin at the same sub-page boundary. */
		if ((sgaddr & ~IO_PAGE_MASK) != (daddr & ~IO_PAGE_MASK)) {
			printk("verify_one_map: Wrong start offset "
			       "sg[%08lx] dma[%08x]\n",
			       sgaddr, daddr);
			nents = -1;
			goto out;
		}

		/* Verify the IOPTE points to the right page. */
		paddr = iopte_val(*iopte) & IOPTE_PAGE;
		if ((paddr + PAGE_OFFSET) != (sgaddr & IO_PAGE_MASK)) {
			printk("verify_one_map: IOPTE[%08lx] maps the "
			       "wrong page, should be [%08lx]\n",
			       iopte_val(*iopte), (sgaddr & IO_PAGE_MASK) - PAGE_OFFSET);
			nents = -1;
			goto out;
		}

		/* If this SG crosses a page, adjust to that next page
		 * boundary and loop.
		 */
		if ((sgaddr & IO_PAGE_MASK) ^ ((sgaddr + sglen - 1) & IO_PAGE_MASK)) {
			unsigned long next_page, diff;

			next_page = (sgaddr + IO_PAGE_SIZE) & IO_PAGE_MASK;
			diff = next_page - sgaddr;
			sgaddr += diff;
			daddr += diff;
			sglen -= diff;
			dlen -= diff;
			if (dlen > 0)
				iopte++;
			continue;
		}

		/* SG wholly consumed within this page. */
		daddr += sglen;
		dlen -= sglen;

		if (dlen > 0 && ((daddr & ~IO_PAGE_MASK) == 0))
			iopte++;

F
FUJITA Tomonori 已提交
123
		sg = sg_next(sg);
L
Linus Torvalds 已提交
124 125
		if (--nents <= 0)
			break;
J
Jens Axboe 已提交
126
		sgaddr = (unsigned long) sg_virt(sg);
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		sglen = sg->length;
	}
	if (dlen < 0) {
		/* Transfer overrun, big problems. */
		printk("verify_one_map: Transfer overrun by %d bytes.\n",
		       -dlen);
		nents = -1;
	} else {
		/* Advance to next dma_sg implies that the next iopte will
		 * begin it.
		 */
		iopte++;
	}

out:
	*__sg = sg;
	*__iopte = iopte;
	return nents;
}

static int verify_maps(struct scatterlist *sg, int nents, iopte_t *iopte)
{
	struct scatterlist *dma_sg = sg;
	struct scatterlist *orig_dma_sg = dma_sg;
	int orig_nents = nents;

	for (;;) {
		nents = verify_one_map(dma_sg, &sg, nents, &iopte);
		if (nents <= 0)
			break;
F
FUJITA Tomonori 已提交
157
		dma_sg = sg_next(dma_sg);
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
		if (dma_sg->dma_length == 0)
			break;
	}

	if (nents > 0) {
		printk("verify_maps: dma maps consumed by some sgs remain (%d)\n",
		       nents);
		return -1;
	}

	if (nents < 0) {
		printk("verify_maps: Error, messed up mappings, "
		       "at sg %d dma_sg %d\n",
		       (int) (orig_nents + nents), (int) (dma_sg - orig_dma_sg));
		return -1;
	}

	/* This test passes... */
	return 0;
}

F
FUJITA Tomonori 已提交
179
void verify_sglist(struct scatterlist *sglist, int nents, iopte_t *iopte, int npages)
L
Linus Torvalds 已提交
180
{
F
FUJITA Tomonori 已提交
181 182 183 184
	struct scatterlist *sg;

	if (verify_lengths(sglist, nents, npages) < 0 ||
	    verify_maps(sglist, nents, iopte) < 0) {
L
Linus Torvalds 已提交
185 186 187
		int i;

		printk("verify_sglist: Crap, messed up mappings, dumping, iodma at ");
F
FUJITA Tomonori 已提交
188
		printk("%016lx.\n", sglist->dma_address & IO_PAGE_MASK);
L
Linus Torvalds 已提交
189

F
FUJITA Tomonori 已提交
190
		for_each_sg(sglist, sg, nents, i) {
L
Linus Torvalds 已提交
191
			printk("sg(%d): page_addr(%p) off(%x) length(%x) "
F
FUJITA Tomonori 已提交
192
			       "dma_address[%016x] dma_length[%016x]\n",
L
Linus Torvalds 已提交
193
			       i,
J
Jens Axboe 已提交
194
			       page_address(sg_page(sg)), sg->offset,
F
FUJITA Tomonori 已提交
195 196
			       sg->length,
			       sg->dma_address, sg->dma_length);
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209
		}
	}

	/* Seems to be ok */
}
#endif

unsigned long prepare_sg(struct scatterlist *sg, int nents)
{
	struct scatterlist *dma_sg = sg;
	unsigned long prev;
	u32 dent_addr, dent_len;

J
Jens Axboe 已提交
210
	prev  = (unsigned long) sg_virt(sg);
L
Linus Torvalds 已提交
211
	prev += (unsigned long) (dent_len = sg->length);
J
Jens Axboe 已提交
212
	dent_addr = (u32) ((unsigned long)(sg_virt(sg)) & (IO_PAGE_SIZE - 1UL));
L
Linus Torvalds 已提交
213 214 215
	while (--nents) {
		unsigned long addr;

F
FUJITA Tomonori 已提交
216
		sg = sg_next(sg);
J
Jens Axboe 已提交
217
		addr = (unsigned long) sg_virt(sg);
L
Linus Torvalds 已提交
218 219 220
		if (! VCONTIG(prev, addr)) {
			dma_sg->dma_address = dent_addr;
			dma_sg->dma_length = dent_len;
F
FUJITA Tomonori 已提交
221
			dma_sg = sg_next(dma_sg);
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235

			dent_addr = ((dent_addr +
				      dent_len +
				      (IO_PAGE_SIZE - 1UL)) >> IO_PAGE_SHIFT);
			dent_addr <<= IO_PAGE_SHIFT;
			dent_addr += addr & (IO_PAGE_SIZE - 1UL);
			dent_len = 0;
		}
		dent_len += sg->length;
		prev = addr + sg->length;
	}
	dma_sg->dma_address = dent_addr;
	dma_sg->dma_length = dent_len;

F
FUJITA Tomonori 已提交
236 237 238 239 240
	if (dma_sg != sg) {
		dma_sg = next_sg(dma_sg);
		dma_sg->dma_length = 0;
	}

L
Linus Torvalds 已提交
241 242 243 244
	return ((unsigned long) dent_addr +
		(unsigned long) dent_len +
		(IO_PAGE_SIZE - 1UL)) >> IO_PAGE_SHIFT;
}