summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sys/arch/amd64/amd64/bus_dma.c73
1 files changed, 57 insertions, 16 deletions
diff --git a/sys/arch/amd64/amd64/bus_dma.c b/sys/arch/amd64/amd64/bus_dma.c
index 8af1c492adf..d1627286da0 100644
--- a/sys/arch/amd64/amd64/bus_dma.c
+++ b/sys/arch/amd64/amd64/bus_dma.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bus_dma.c,v 1.17 2009/03/30 18:12:24 deraadt Exp $ */
+/* $OpenBSD: bus_dma.c,v 1.18 2009/04/04 13:48:55 dlg Exp $ */
/* $NetBSD: bus_dma.c,v 1.3 2003/05/07 21:33:58 fvdl Exp $ */
/*-
@@ -308,27 +308,68 @@ int
_bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, bus_dma_segment_t *segs,
int nsegs, bus_size_t size, int flags)
{
+ bus_addr_t paddr, baddr, bmask, lastaddr = 0;
+ bus_size_t plen, sgsize;
+ int first = 1;
+ int i, seg = 0;
+
if (nsegs > map->_dm_segcnt || size > map->_dm_size)
return (EINVAL);
- /*
- * Make sure we don't cross any boundaries.
- */
- if (map->_dm_boundary) {
- bus_addr_t bmask = ~(map->_dm_boundary - 1);
- int i;
-
- for (i = 0; i < nsegs; i++) {
- if (segs[i].ds_len > map->_dm_maxsegsz)
- return (EINVAL);
- if ((segs[i].ds_addr & bmask) !=
- ((segs[i].ds_addr + segs[i].ds_len - 1) & bmask))
- return (EINVAL);
+ bmask = ~(map->_dm_boundary - 1);
+
+ for (i = 0; i < nsegs; i++) {
+ paddr = segs[i].ds_addr;
+ plen = segs[i].ds_len;
+
+ while (plen > 0) {
+ /*
+ * Compute the segment size, and adjust counts.
+ */
+ sgsize = PAGE_SIZE - ((u_long)paddr & PGOFSET);
+ if (plen < sgsize)
+ sgsize = plen;
+
+ /*
+ * Make sure we don't cross any boundaries.
+ */
+ if (map->_dm_boundary > 0) {
+ baddr = (paddr + map->_dm_boundary) & bmask;
+ if (sgsize > (baddr - paddr))
+ sgsize = (baddr - paddr);
+ }
+
+ /*
+ * Insert chunk into a segment, coalescing with
+ * previous segment if possible.
+ */
+ if (first) {
+ map->dm_segs[seg].ds_addr = paddr;
+ map->dm_segs[seg].ds_len = sgsize;
+ first = 0;
+ } else {
+ if (paddr == lastaddr &&
+ (map->dm_segs[seg].ds_len + sgsize) <=
+ map->_dm_maxsegsz &&
+ (map->_dm_boundary == 0 ||
+ (map->dm_segs[seg].ds_addr & bmask) ==
+ (paddr & bmask)))
+ map->dm_segs[seg].ds_len += sgsize;
+ else {
+ if (++seg >= map->_dm_segcnt)
+ return (EINVAL);
+ map->dm_segs[seg].ds_addr = paddr;
+ map->dm_segs[seg].ds_len = sgsize;
+ }
+ }
+
+ lastaddr = paddr + sgsize;
+ paddr += sgsize;
+ plen -= sgsize;
}
}
- bcopy(segs, map->dm_segs, nsegs * sizeof(*segs));
- map->dm_nsegs = nsegs;
+ map->dm_nsegs = seg + 1;
return (0);
}