1 /* $Id: iommu_common.c,v 1.9 2001/12/17 07:05:09 davem Exp $
2 * iommu_common.c: UltraSparc SBUS/PCI common iommu code.
4 * Copyright (C) 1999 David S. Miller (davem@redhat.com)
7 #include "iommu_common.h"
9 /* You are _strongly_ advised to enable the following debugging code
10 * any time you make changes to the sg code below, run it for a while
11 * with filesystems mounted read-only before buying the farm... -DaveM
15 static int verify_lengths(struct scatterlist
*sg
, int nents
, int npages
)
21 for (i
= 0; i
< nents
; i
++)
22 sg_len
+= sg
[i
].length
;
25 for (i
= 0; i
< nents
&& sg
[i
].dma_length
; i
++)
26 dma_len
+= sg
[i
].dma_length
;
28 if (sg_len
!= dma_len
) {
29 printk("verify_lengths: Error, different, sg[%d] dma[%d]\n",
35 for (i
= 0; i
< nents
&& sg
[i
].dma_length
; i
++) {
36 unsigned long start
, end
;
38 start
= sg
[i
].dma_address
;
39 start
= start
& IO_PAGE_MASK
;
41 end
= sg
[i
].dma_address
+ sg
[i
].dma_length
;
42 end
= (end
+ (IO_PAGE_SIZE
- 1)) & IO_PAGE_MASK
;
44 pgcount
+= ((end
- start
) >> IO_PAGE_SHIFT
);
47 if (pgcount
!= npages
) {
48 printk("verify_lengths: Error, page count wrong, "
49 "npages[%d] pgcount[%d]\n",
54 /* This test passes... */
58 static int verify_one_map(struct scatterlist
*dma_sg
, struct scatterlist
**__sg
, int nents
, iopte_t
**__iopte
)
60 struct scatterlist
*sg
= *__sg
;
61 iopte_t
*iopte
= *__iopte
;
62 u32 dlen
= dma_sg
->dma_length
;
67 daddr
= dma_sg
->dma_address
;
69 sgaddr
= (unsigned long) (page_address(sg
->page
) + sg
->offset
);
73 /* SG and DMA_SG must begin at the same sub-page boundary. */
74 if ((sgaddr
& ~IO_PAGE_MASK
) != (daddr
& ~IO_PAGE_MASK
)) {
75 printk("verify_one_map: Wrong start offset "
76 "sg[%08lx] dma[%08x]\n",
82 /* Verify the IOPTE points to the right page. */
83 paddr
= iopte_val(*iopte
) & IOPTE_PAGE
;
84 if ((paddr
+ PAGE_OFFSET
) != (sgaddr
& IO_PAGE_MASK
)) {
85 printk("verify_one_map: IOPTE[%08lx] maps the "
86 "wrong page, should be [%08lx]\n",
87 iopte_val(*iopte
), (sgaddr
& IO_PAGE_MASK
) - PAGE_OFFSET
);
92 /* If this SG crosses a page, adjust to that next page
95 if ((sgaddr
& IO_PAGE_MASK
) ^ ((sgaddr
+ sglen
- 1) & IO_PAGE_MASK
)) {
96 unsigned long next_page
, diff
;
98 next_page
= (sgaddr
+ IO_PAGE_SIZE
) & IO_PAGE_MASK
;
99 diff
= next_page
- sgaddr
;
109 /* SG wholly consumed within this page. */
113 if (dlen
> 0 && ((daddr
& ~IO_PAGE_MASK
) == 0))
119 sgaddr
= (unsigned long) (page_address(sg
->page
) + sg
->offset
);
123 /* Transfer overrun, big problems. */
124 printk("verify_one_map: Transfer overrun by %d bytes.\n",
128 /* Advance to next dma_sg implies that the next iopte will
140 static int verify_maps(struct scatterlist
*sg
, int nents
, iopte_t
*iopte
)
142 struct scatterlist
*dma_sg
= sg
;
143 struct scatterlist
*orig_dma_sg
= dma_sg
;
144 int orig_nents
= nents
;
147 nents
= verify_one_map(dma_sg
, &sg
, nents
, &iopte
);
151 if (dma_sg
->dma_length
== 0)
156 printk("verify_maps: dma maps consumed by some sgs remain (%d)\n",
162 printk("verify_maps: Error, messed up mappings, "
163 "at sg %d dma_sg %d\n",
164 (int) (orig_nents
+ nents
), (int) (dma_sg
- orig_dma_sg
));
168 /* This test passes... */
172 void verify_sglist(struct scatterlist
*sg
, int nents
, iopte_t
*iopte
, int npages
)
174 if (verify_lengths(sg
, nents
, npages
) < 0 ||
175 verify_maps(sg
, nents
, iopte
) < 0) {
178 printk("verify_sglist: Crap, messed up mappings, dumping, iodma at ");
179 printk("%016lx.\n", sg
->dma_address
& IO_PAGE_MASK
);
181 for (i
= 0; i
< nents
; i
++) {
182 printk("sg(%d): page_addr(%p) off(%x) length(%x) "
183 "dma_address[%016lx] dma_length[%016lx]\n",
185 page_address(sg
[i
].page
), sg
[i
].offset
,
187 sg
[i
].dma_address
, sg
[i
].dma_length
);
195 unsigned long prepare_sg(struct scatterlist
*sg
, int nents
)
197 struct scatterlist
*dma_sg
= sg
;
199 u32 dent_addr
, dent_len
;
201 prev
= (unsigned long) (page_address(sg
->page
) + sg
->offset
);
202 prev
+= (unsigned long) (dent_len
= sg
->length
);
203 dent_addr
= (u32
) ((unsigned long)(page_address(sg
->page
) + sg
->offset
)
204 & (IO_PAGE_SIZE
- 1UL));
209 addr
= (unsigned long) (page_address(sg
->page
) + sg
->offset
);
210 if (! VCONTIG(prev
, addr
)) {
211 dma_sg
->dma_address
= dent_addr
;
212 dma_sg
->dma_length
= dent_len
;
215 dent_addr
= ((dent_addr
+
217 (IO_PAGE_SIZE
- 1UL)) >> IO_PAGE_SHIFT
);
218 dent_addr
<<= IO_PAGE_SHIFT
;
219 dent_addr
+= addr
& (IO_PAGE_SIZE
- 1UL);
222 dent_len
+= sg
->length
;
223 prev
= addr
+ sg
->length
;
225 dma_sg
->dma_address
= dent_addr
;
226 dma_sg
->dma_length
= dent_len
;
228 return ((unsigned long) dent_addr
+
229 (unsigned long) dent_len
+
230 (IO_PAGE_SIZE
- 1UL)) >> IO_PAGE_SHIFT
;