1 1.16 skrll /* $NetBSD: isa_dma.c,v 1.16 2022/01/22 15:10:30 skrll Exp $ */ 2 1.1 leo 3 1.1 leo #define ISA_DMA_STATS 4 1.1 leo 5 1.1 leo /*- 6 1.1 leo * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc. 7 1.1 leo * All rights reserved. 8 1.1 leo * 9 1.1 leo * This code is derived from software contributed to The NetBSD Foundation 10 1.1 leo * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace 11 1.1 leo * Simulation Facility, NASA Ames Research Center. 12 1.1 leo * 13 1.1 leo * Redistribution and use in source and binary forms, with or without 14 1.1 leo * modification, are permitted provided that the following conditions 15 1.1 leo * are met: 16 1.1 leo * 1. Redistributions of source code must retain the above copyright 17 1.1 leo * notice, this list of conditions and the following disclaimer. 18 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright 19 1.1 leo * notice, this list of conditions and the following disclaimer in the 20 1.1 leo * documentation and/or other materials provided with the distribution. 21 1.1 leo * 22 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 1.1 leo * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 1.1 leo * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 1.1 leo * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 1.1 leo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 1.1 leo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 1.1 leo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 1.1 leo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 1.1 leo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 1.1 leo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 1.1 leo * POSSIBILITY OF SUCH DAMAGE. 33 1.1 leo */ 34 1.3 lukem 35 1.3 lukem #include <sys/cdefs.h> 36 1.16 skrll __KERNEL_RCSID(0, "$NetBSD: isa_dma.c,v 1.16 2022/01/22 15:10:30 skrll Exp $"); 37 1.3 lukem 38 1.1 leo #include <sys/param.h> 39 1.1 leo #include <sys/systm.h> 40 1.1 leo #include <sys/kernel.h> 41 1.1 leo #include <sys/syslog.h> 42 1.1 leo #include <sys/device.h> 43 1.13 thorpej #include <sys/kmem.h> 44 1.1 leo #include <sys/proc.h> 45 1.1 leo #include <sys/mbuf.h> 46 1.1 leo 47 1.1 leo #define _ATARI_BUS_DMA_PRIVATE 48 1.10 dyoung #include <sys/bus.h> 49 1.1 leo 50 1.1 leo #include <dev/isa/isareg.h> 51 1.1 leo #include <dev/isa/isavar.h> 52 1.1 leo 53 1.1 leo #include <uvm/uvm_extern.h> 54 1.1 leo 55 1.1 leo extern paddr_t avail_end; 56 1.1 leo 57 1.1 leo /* 58 1.1 leo * Cookie used by ISA dma. A pointer to one of these it stashed in 59 1.1 leo * the DMA map. 60 1.1 leo */ 61 1.1 leo struct atari_isa_dma_cookie { 62 1.1 leo int id_flags; /* flags; see below */ 63 1.1 leo 64 1.1 leo /* 65 1.1 leo * Information about the original buffer used during 66 1.1 leo * DMA map syncs. Note that origibuflen is only used 67 1.1 leo * for ID_BUFTYPE_LINEAR. 68 1.1 leo */ 69 1.1 leo void *id_origbuf; /* pointer to orig buffer if 70 1.1 leo bouncing */ 71 1.1 leo bus_size_t id_origbuflen; /* ...and size */ 72 1.1 leo int id_buftype; /* type of buffer */ 73 1.1 leo 74 1.1 leo void *id_bouncebuf; /* pointer to the bounce buffer */ 75 1.1 leo bus_size_t id_bouncebuflen; /* ...and size */ 76 1.1 leo int id_nbouncesegs; /* number of valid bounce segs */ 77 1.1 leo bus_dma_segment_t id_bouncesegs[0]; /* array of bounce buffer 78 1.1 leo physical memory segments */ 79 1.1 leo }; 80 1.1 leo 81 1.1 leo /* id_flags */ 82 1.1 leo #define ID_MIGHT_NEED_BOUNCE 0x01 /* map could need bounce buffers */ 83 1.1 leo #define ID_HAS_BOUNCE 0x02 /* map currently has bounce buffers */ 84 1.1 leo #define ID_IS_BOUNCING 0x04 /* map is bouncing current xfer */ 85 1.1 leo 86 1.1 leo /* id_buftype */ 87 1.1 leo #define ID_BUFTYPE_INVALID 0 88 1.1 leo #define ID_BUFTYPE_LINEAR 1 89 1.1 leo #define ID_BUFTYPE_MBUF 2 90 1.1 leo #define ID_BUFTYPE_UIO 3 91 1.1 leo #define ID_BUFTYPE_RAW 4 92 1.1 leo 93 1.8 dsl int _isa_bus_dmamap_create(bus_dma_tag_t, bus_size_t, int, 94 1.8 dsl bus_size_t, bus_size_t, int, bus_dmamap_t *); 95 1.8 dsl void _isa_bus_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t); 96 1.8 dsl int _isa_bus_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, 97 1.8 dsl bus_size_t, struct proc *, int); 98 1.8 dsl int _isa_bus_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t, 99 1.8 dsl struct mbuf *, int); 100 1.8 dsl int _isa_bus_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t, 101 1.8 dsl struct uio *, int); 102 1.8 dsl int _isa_bus_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t, 103 1.8 dsl bus_dma_segment_t *, int, bus_size_t, int); 104 1.8 dsl void _isa_bus_dmamap_unload(bus_dma_tag_t, bus_dmamap_t); 105 1.8 dsl void _isa_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, 106 1.8 dsl bus_addr_t, bus_size_t, int); 107 1.8 dsl 108 1.8 dsl int _isa_bus_dmamem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t, 109 1.8 dsl bus_size_t, bus_dma_segment_t *, int, int *, int); 110 1.8 dsl 111 1.8 dsl int _isa_dma_alloc_bouncebuf(bus_dma_tag_t, bus_dmamap_t, 112 1.8 dsl bus_size_t, int); 113 1.8 dsl void _isa_dma_free_bouncebuf(bus_dma_tag_t, bus_dmamap_t); 114 1.1 leo 115 1.1 leo /* 116 1.1 leo * Entry points for ISA DMA. These are mostly wrappers around 117 1.1 leo * the generic functions that understand how to deal with bounce 118 1.1 leo * buffers, if necessary. 119 1.1 leo */ 120 1.1 leo struct atari_bus_dma_tag isa_bus_dma_tag = { 121 1.1 leo ISA_DMA_BOUNCE_THRESHOLD, 122 1.1 leo 0, 123 1.1 leo _isa_bus_dmamap_create, 124 1.1 leo _isa_bus_dmamap_destroy, 125 1.1 leo _isa_bus_dmamap_load, 126 1.1 leo _isa_bus_dmamap_load_mbuf, 127 1.1 leo _isa_bus_dmamap_load_uio, 128 1.1 leo _isa_bus_dmamap_load_raw, 129 1.1 leo _isa_bus_dmamap_unload, 130 1.1 leo _isa_bus_dmamap_sync, 131 1.1 leo }; 132 1.1 leo 133 1.1 leo /********************************************************************** 134 1.1 leo * bus.h dma interface entry points 135 1.1 leo **********************************************************************/ 136 1.1 leo 137 1.1 leo #ifdef ISA_DMA_STATS 138 1.1 leo #define STAT_INCR(v) (v)++ 139 1.1 leo #define STAT_DECR(v) do { \ 140 1.1 leo if ((v) == 0) \ 141 1.1 leo printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \ 142 1.1 leo else \ 143 1.1 leo (v)--; \ 144 1.1 leo } while (0) 145 1.1 leo u_long isa_dma_stats_loads; 146 1.1 leo u_long isa_dma_stats_bounces; 147 1.1 leo u_long isa_dma_stats_nbouncebufs; 148 1.1 leo #else 149 1.1 leo #define STAT_INCR(v) 150 1.1 leo #define STAT_DECR(v) 151 1.1 leo #endif 152 1.1 leo 153 1.13 thorpej static int 154 1.13 thorpej isadma_bounce_cookieflags(bus_dma_tag_t const t, bus_dmamap_t const map, 155 1.13 thorpej int const flags) 156 1.1 leo { 157 1.13 thorpej int cookieflags = 0; 158 1.1 leo 159 1.1 leo /* 160 1.1 leo * ISA only has 24-bits of address space. This means 161 1.1 leo * we can't DMA to pages over 16M. In order to DMA to 162 1.1 leo * arbitrary buffers, we use "bounce buffers" - pages 163 1.1 leo * in memory below the 16M boundary. On DMA reads, 164 1.1 leo * DMA happens to the bounce buffers, and is copied into 165 1.1 leo * the caller's buffer. On writes, data is copied into 166 1.15 skrll * the bounce buffer, and the DMA happens from those 167 1.1 leo * pages. To software using the DMA mapping interface, 168 1.1 leo * this looks simply like a data cache. 169 1.1 leo * 170 1.1 leo * If we have more than 16M of RAM in the system, we may 171 1.1 leo * need bounce buffers. We check and remember that here. 172 1.1 leo * 173 1.1 leo * There are exceptions, however. VLB devices can do 174 1.1 leo * 32-bit DMA, and indicate that here. 175 1.1 leo * 176 1.1 leo * ...or, there is an opposite case. The most segments 177 1.1 leo * a transfer will require is (maxxfer / PAGE_SIZE) + 1. If 178 1.1 leo * the caller can't handle that many segments (e.g. the 179 1.1 leo * ISA DMA controller), we may have to bounce it as well. 180 1.1 leo */ 181 1.1 leo if (avail_end <= t->_bounce_thresh || 182 1.1 leo (flags & ISABUS_DMA_32BIT) != 0) { 183 1.14 skrll /* Bouncing not necessary due to memory size. */ 184 1.1 leo map->_dm_bounce_thresh = 0; 185 1.1 leo } 186 1.1 leo if (map->_dm_bounce_thresh != 0 || 187 1.1 leo ((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt) { 188 1.1 leo cookieflags |= ID_MIGHT_NEED_BOUNCE; 189 1.13 thorpej } 190 1.13 thorpej return cookieflags; 191 1.13 thorpej } 192 1.13 thorpej 193 1.13 thorpej static size_t 194 1.13 thorpej isadma_bounce_cookiesize(bus_dmamap_t const map, int cookieflags) 195 1.13 thorpej { 196 1.13 thorpej size_t cookiesize = sizeof(struct atari_isa_dma_cookie); 197 1.13 thorpej 198 1.13 thorpej if (cookieflags & ID_MIGHT_NEED_BOUNCE) { 199 1.1 leo cookiesize += (sizeof(bus_dma_segment_t) * map->_dm_segcnt); 200 1.1 leo } 201 1.13 thorpej return cookiesize; 202 1.13 thorpej } 203 1.13 thorpej 204 1.13 thorpej static int 205 1.13 thorpej isadma_bounce_cookie_alloc(bus_dma_tag_t const t, bus_dmamap_t const map, 206 1.13 thorpej int const flags) 207 1.13 thorpej { 208 1.13 thorpej struct atari_isa_dma_cookie *cookie; 209 1.13 thorpej int cookieflags = isadma_bounce_cookieflags(t, map, flags); 210 1.13 thorpej 211 1.13 thorpej if ((cookie = kmem_zalloc(isadma_bounce_cookiesize(map, cookieflags), 212 1.13 thorpej (flags & BUS_DMA_NOWAIT) ? KM_NOSLEEP : KM_SLEEP)) == NULL) { 213 1.13 thorpej return ENOMEM; 214 1.13 thorpej } 215 1.13 thorpej 216 1.13 thorpej cookie->id_flags = cookieflags; 217 1.13 thorpej map->_dm_cookie = cookie; 218 1.13 thorpej 219 1.13 thorpej return 0; 220 1.13 thorpej } 221 1.13 thorpej 222 1.13 thorpej static void 223 1.13 thorpej isadma_bounce_cookie_free(bus_dmamap_t const map) 224 1.13 thorpej { 225 1.13 thorpej struct atari_isa_dma_cookie *cookie = map->_dm_cookie; 226 1.13 thorpej 227 1.13 thorpej if (cookie != NULL) { 228 1.13 thorpej kmem_free(map->_dm_cookie, 229 1.13 thorpej isadma_bounce_cookiesize(map, cookie->id_flags)); 230 1.13 thorpej map->_dm_cookie = NULL; 231 1.13 thorpej } 232 1.13 thorpej } 233 1.13 thorpej 234 1.13 thorpej /* 235 1.13 thorpej * Create an ISA DMA map. 236 1.13 thorpej */ 237 1.13 thorpej int 238 1.13 thorpej _isa_bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp) 239 1.13 thorpej { 240 1.13 thorpej struct atari_isa_dma_cookie *cookie; 241 1.13 thorpej bus_dmamap_t map; 242 1.13 thorpej int error; 243 1.13 thorpej 244 1.13 thorpej /* Call common function to create the basic map. */ 245 1.13 thorpej error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, 246 1.13 thorpej flags, dmamp); 247 1.13 thorpej if (error) 248 1.13 thorpej return (error); 249 1.13 thorpej 250 1.13 thorpej map = *dmamp; 251 1.13 thorpej map->_dm_cookie = NULL; 252 1.1 leo 253 1.1 leo /* 254 1.1 leo * Allocate our cookie. 255 1.1 leo */ 256 1.13 thorpej if ((error = isadma_bounce_cookie_alloc(t, map, flags)) != 0) { 257 1.1 leo goto out; 258 1.1 leo } 259 1.13 thorpej cookie = map->_dm_cookie; 260 1.1 leo 261 1.13 thorpej if (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) { 262 1.1 leo /* 263 1.1 leo * Allocate the bounce pages now if the caller 264 1.1 leo * wishes us to do so. 265 1.1 leo */ 266 1.13 thorpej if (flags & BUS_DMA_ALLOCNOW) { 267 1.13 thorpej error = _isa_dma_alloc_bouncebuf(t, map, size, flags); 268 1.13 thorpej } 269 1.1 leo } 270 1.1 leo 271 1.1 leo out: 272 1.1 leo if (error) { 273 1.13 thorpej isadma_bounce_cookie_free(map); 274 1.1 leo _bus_dmamap_destroy(t, map); 275 1.1 leo } 276 1.1 leo return (error); 277 1.1 leo } 278 1.1 leo 279 1.1 leo /* 280 1.1 leo * Destroy an ISA DMA map. 281 1.1 leo */ 282 1.1 leo void 283 1.9 dsl _isa_bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map) 284 1.1 leo { 285 1.1 leo struct atari_isa_dma_cookie *cookie = map->_dm_cookie; 286 1.1 leo 287 1.1 leo /* 288 1.1 leo * Free any bounce pages this map might hold. 289 1.1 leo */ 290 1.1 leo if (cookie->id_flags & ID_HAS_BOUNCE) 291 1.1 leo _isa_dma_free_bouncebuf(t, map); 292 1.1 leo 293 1.13 thorpej isadma_bounce_cookie_free(map); 294 1.1 leo _bus_dmamap_destroy(t, map); 295 1.1 leo } 296 1.1 leo 297 1.1 leo /* 298 1.1 leo * Load an ISA DMA map with a linear buffer. 299 1.1 leo */ 300 1.1 leo int 301 1.11 jdc _isa_bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 302 1.11 jdc bus_size_t buflen, struct proc *p, int flags) 303 1.1 leo { 304 1.1 leo struct atari_isa_dma_cookie *cookie = map->_dm_cookie; 305 1.1 leo int error; 306 1.1 leo 307 1.1 leo STAT_INCR(isa_dma_stats_loads); 308 1.1 leo 309 1.1 leo /* 310 1.1 leo * Make sure that on error condition we return "no valid mappings." 311 1.1 leo */ 312 1.1 leo map->dm_mapsize = 0; 313 1.1 leo map->dm_nsegs = 0; 314 1.1 leo 315 1.1 leo /* 316 1.1 leo * Try to load the map the normal way. If this errors out, 317 1.1 leo * and we can bounce, we will. 318 1.1 leo */ 319 1.1 leo error = _bus_dmamap_load(t, map, buf, buflen, p, flags); 320 1.12 christos if (error == 0 || (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0) 321 1.1 leo return (error); 322 1.1 leo 323 1.1 leo /* 324 1.1 leo * First attempt failed; bounce it. 325 1.1 leo */ 326 1.1 leo 327 1.1 leo STAT_INCR(isa_dma_stats_bounces); 328 1.1 leo 329 1.1 leo /* 330 1.1 leo * Allocate bounce pages, if necessary. 331 1.1 leo */ 332 1.1 leo if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) { 333 1.1 leo error = _isa_dma_alloc_bouncebuf(t, map, buflen, flags); 334 1.1 leo if (error) 335 1.1 leo return (error); 336 1.1 leo } 337 1.1 leo 338 1.1 leo /* 339 1.1 leo * Cache a pointer to the caller's buffer and load the DMA map 340 1.1 leo * with the bounce buffer. 341 1.1 leo */ 342 1.1 leo cookie->id_origbuf = buf; 343 1.1 leo cookie->id_origbuflen = buflen; 344 1.1 leo cookie->id_buftype = ID_BUFTYPE_LINEAR; 345 1.1 leo error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen, 346 1.1 leo p, flags); 347 1.1 leo if (error) { 348 1.1 leo /* 349 1.1 leo * Free the bounce pages, unless our resources 350 1.1 leo * are reserved for our exclusive use. 351 1.1 leo */ 352 1.1 leo if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) 353 1.1 leo _isa_dma_free_bouncebuf(t, map); 354 1.1 leo return (error); 355 1.1 leo } 356 1.1 leo 357 1.1 leo /* ...so _isa_bus_dmamap_sync() knows we're bouncing */ 358 1.1 leo cookie->id_flags |= ID_IS_BOUNCING; 359 1.1 leo return (0); 360 1.1 leo } 361 1.1 leo 362 1.1 leo /* 363 1.1 leo * Like _isa_bus_dmamap_load(), but for mbufs. 364 1.1 leo */ 365 1.1 leo int 366 1.11 jdc _isa_bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0, 367 1.14 skrll int flags) 368 1.1 leo { 369 1.1 leo struct atari_isa_dma_cookie *cookie = map->_dm_cookie; 370 1.1 leo int error; 371 1.1 leo 372 1.1 leo /* 373 1.1 leo * Make sure on error condition we return "no valid mappings." 374 1.1 leo */ 375 1.1 leo map->dm_mapsize = 0; 376 1.1 leo map->dm_nsegs = 0; 377 1.1 leo 378 1.1 leo #ifdef DIAGNOSTIC 379 1.1 leo if ((m0->m_flags & M_PKTHDR) == 0) 380 1.1 leo panic("_isa_bus_dmamap_load_mbuf: no packet header"); 381 1.1 leo #endif 382 1.1 leo 383 1.1 leo if (m0->m_pkthdr.len > map->_dm_size) 384 1.1 leo return (EINVAL); 385 1.1 leo 386 1.1 leo /* 387 1.1 leo * Try to load the map the normal way. If this errors out, 388 1.1 leo * and we can bounce, we will. 389 1.1 leo */ 390 1.1 leo error = _bus_dmamap_load_mbuf(t, map, m0, flags); 391 1.12 christos if (error == 0 || (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0) 392 1.1 leo return (error); 393 1.1 leo 394 1.1 leo /* 395 1.1 leo * First attempt failed; bounce it. 396 1.1 leo */ 397 1.1 leo 398 1.1 leo STAT_INCR(isa_dma_stats_bounces); 399 1.1 leo 400 1.1 leo /* 401 1.1 leo * Allocate bounce pages, if necessary. 402 1.1 leo */ 403 1.1 leo if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) { 404 1.1 leo error = _isa_dma_alloc_bouncebuf(t, map, m0->m_pkthdr.len, 405 1.1 leo flags); 406 1.1 leo if (error) 407 1.1 leo return (error); 408 1.1 leo } 409 1.1 leo 410 1.1 leo /* 411 1.1 leo * Cache a pointer to the caller's buffer and load the DMA map 412 1.1 leo * with the bounce buffer. 413 1.1 leo */ 414 1.1 leo cookie->id_origbuf = m0; 415 1.1 leo cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */ 416 1.1 leo cookie->id_buftype = ID_BUFTYPE_MBUF; 417 1.1 leo error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, 418 1.1 leo m0->m_pkthdr.len, NULL, flags); 419 1.1 leo if (error) { 420 1.1 leo /* 421 1.1 leo * Free the bounce pages, unless our resources 422 1.1 leo * are reserved for our exclusive use. 423 1.1 leo */ 424 1.1 leo if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) 425 1.1 leo _isa_dma_free_bouncebuf(t, map); 426 1.1 leo return (error); 427 1.1 leo } 428 1.1 leo 429 1.1 leo /* ...so _isa_bus_dmamap_sync() knows we're bouncing */ 430 1.1 leo cookie->id_flags |= ID_IS_BOUNCING; 431 1.1 leo return (0); 432 1.1 leo } 433 1.1 leo 434 1.1 leo /* 435 1.1 leo * Like _isa_bus_dmamap_load(), but for uios. 436 1.1 leo */ 437 1.1 leo int 438 1.9 dsl _isa_bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags) 439 1.1 leo { 440 1.1 leo 441 1.1 leo panic("_isa_bus_dmamap_load_uio: not implemented"); 442 1.1 leo } 443 1.1 leo 444 1.1 leo /* 445 1.1 leo * Like _isa_bus_dmamap_load(), but for raw memory allocated with 446 1.1 leo * bus_dmamem_alloc(). 447 1.1 leo */ 448 1.1 leo int 449 1.9 dsl _isa_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) 450 1.1 leo { 451 1.1 leo 452 1.1 leo panic("_isa_bus_dmamap_load_raw: not implemented"); 453 1.1 leo } 454 1.1 leo 455 1.1 leo /* 456 1.1 leo * Unload an ISA DMA map. 457 1.1 leo */ 458 1.1 leo void 459 1.9 dsl _isa_bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map) 460 1.1 leo { 461 1.1 leo struct atari_isa_dma_cookie *cookie = map->_dm_cookie; 462 1.1 leo 463 1.1 leo /* 464 1.1 leo * If we have bounce pages, free them, unless they're 465 1.1 leo * reserved for our exclusive use. 466 1.1 leo */ 467 1.1 leo if ((cookie->id_flags & ID_HAS_BOUNCE) && 468 1.1 leo (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) 469 1.1 leo _isa_dma_free_bouncebuf(t, map); 470 1.1 leo 471 1.1 leo cookie->id_flags &= ~ID_IS_BOUNCING; 472 1.1 leo cookie->id_buftype = ID_BUFTYPE_INVALID; 473 1.1 leo 474 1.1 leo /* 475 1.1 leo * Do the generic bits of the unload. 476 1.1 leo */ 477 1.1 leo _bus_dmamap_unload(t, map); 478 1.1 leo } 479 1.1 leo 480 1.1 leo /* 481 1.1 leo * Synchronize an ISA DMA map. 482 1.1 leo */ 483 1.1 leo void 484 1.9 dsl _isa_bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, bus_size_t len, int ops) 485 1.1 leo { 486 1.1 leo struct atari_isa_dma_cookie *cookie = map->_dm_cookie; 487 1.1 leo 488 1.1 leo /* 489 1.1 leo * Mixing PRE and POST operations is not allowed. 490 1.1 leo */ 491 1.1 leo if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 && 492 1.1 leo (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0) 493 1.1 leo panic("_isa_bus_dmamap_sync: mix PRE and POST"); 494 1.1 leo 495 1.1 leo #ifdef DIAGNOSTIC 496 1.1 leo if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) { 497 1.1 leo if (offset >= map->dm_mapsize) 498 1.1 leo panic("_isa_bus_dmamap_sync: bad offset"); 499 1.1 leo if (len == 0 || (offset + len) > map->dm_mapsize) 500 1.1 leo panic("_isa_bus_dmamap_sync: bad length"); 501 1.1 leo } 502 1.1 leo #endif 503 1.1 leo 504 1.1 leo /* 505 1.1 leo * If we're not bouncing, just return; nothing to do. 506 1.1 leo */ 507 1.1 leo if ((cookie->id_flags & ID_IS_BOUNCING) == 0) 508 1.1 leo return; 509 1.1 leo 510 1.1 leo switch (cookie->id_buftype) { 511 1.1 leo case ID_BUFTYPE_LINEAR: 512 1.1 leo /* 513 1.1 leo * Nothing to do for pre-read. 514 1.1 leo */ 515 1.1 leo 516 1.1 leo if (ops & BUS_DMASYNC_PREWRITE) { 517 1.1 leo /* 518 1.1 leo * Copy the caller's buffer to the bounce buffer. 519 1.1 leo */ 520 1.1 leo memcpy((char *)cookie->id_bouncebuf + offset, 521 1.1 leo (char *)cookie->id_origbuf + offset, len); 522 1.1 leo } 523 1.1 leo 524 1.1 leo if (ops & BUS_DMASYNC_POSTREAD) { 525 1.1 leo /* 526 1.1 leo * Copy the bounce buffer to the caller's buffer. 527 1.1 leo */ 528 1.1 leo memcpy((char *)cookie->id_origbuf + offset, 529 1.1 leo (char *)cookie->id_bouncebuf + offset, len); 530 1.1 leo } 531 1.1 leo 532 1.1 leo /* 533 1.1 leo * Nothing to do for post-write. 534 1.1 leo */ 535 1.1 leo break; 536 1.1 leo 537 1.1 leo case ID_BUFTYPE_MBUF: 538 1.1 leo { 539 1.1 leo struct mbuf *m, *m0 = cookie->id_origbuf; 540 1.1 leo bus_size_t minlen, moff; 541 1.1 leo 542 1.1 leo /* 543 1.1 leo * Nothing to do for pre-read. 544 1.1 leo */ 545 1.1 leo 546 1.1 leo if (ops & BUS_DMASYNC_PREWRITE) { 547 1.1 leo /* 548 1.1 leo * Copy the caller's buffer to the bounce buffer. 549 1.1 leo */ 550 1.1 leo m_copydata(m0, offset, len, 551 1.1 leo (char *)cookie->id_bouncebuf + offset); 552 1.1 leo } 553 1.1 leo 554 1.1 leo if (ops & BUS_DMASYNC_POSTREAD) { 555 1.1 leo /* 556 1.1 leo * Copy the bounce buffer to the caller's buffer. 557 1.1 leo */ 558 1.1 leo for (moff = offset, m = m0; m != NULL && len != 0; 559 1.1 leo m = m->m_next) { 560 1.1 leo /* Find the beginning mbuf. */ 561 1.1 leo if (moff >= m->m_len) { 562 1.1 leo moff -= m->m_len; 563 1.1 leo continue; 564 1.1 leo } 565 1.1 leo 566 1.1 leo /* 567 1.1 leo * Now at the first mbuf to sync; nail 568 1.1 leo * each one until we have exhausted the 569 1.1 leo * length. 570 1.1 leo */ 571 1.1 leo minlen = len < m->m_len - moff ? 572 1.1 leo len : m->m_len - moff; 573 1.1 leo 574 1.6 tsutsui memcpy(mtod(m, char *) + moff, 575 1.1 leo (char *)cookie->id_bouncebuf + offset, 576 1.1 leo minlen); 577 1.1 leo 578 1.1 leo moff = 0; 579 1.1 leo len -= minlen; 580 1.1 leo offset += minlen; 581 1.1 leo } 582 1.1 leo } 583 1.1 leo 584 1.1 leo /* 585 1.1 leo * Nothing to do for post-write. 586 1.1 leo */ 587 1.1 leo break; 588 1.1 leo } 589 1.1 leo 590 1.1 leo case ID_BUFTYPE_UIO: 591 1.1 leo panic("_isa_bus_dmamap_sync: ID_BUFTYPE_UIO"); 592 1.1 leo break; 593 1.1 leo 594 1.1 leo case ID_BUFTYPE_RAW: 595 1.1 leo panic("_isa_bus_dmamap_sync: ID_BUFTYPE_RAW"); 596 1.1 leo break; 597 1.1 leo 598 1.1 leo case ID_BUFTYPE_INVALID: 599 1.1 leo panic("_isa_bus_dmamap_sync: ID_BUFTYPE_INVALID"); 600 1.1 leo break; 601 1.1 leo 602 1.1 leo default: 603 1.1 leo printf("unknown buffer type %d\n", cookie->id_buftype); 604 1.1 leo panic("_isa_bus_dmamap_sync"); 605 1.1 leo } 606 1.1 leo } 607 1.1 leo 608 1.1 leo /* 609 1.1 leo * Allocate memory safe for ISA DMA. 610 1.1 leo */ 611 1.1 leo int 612 1.9 dsl _isa_bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags) 613 1.1 leo { 614 1.1 leo paddr_t high; 615 1.1 leo 616 1.1 leo if (avail_end > ISA_DMA_BOUNCE_THRESHOLD) 617 1.16 skrll high = ISA_DMA_BOUNCE_THRESHOLD - 1; 618 1.1 leo else 619 1.16 skrll high = avail_end - 1; 620 1.1 leo 621 1.1 leo return (bus_dmamem_alloc_range(t, size, alignment, boundary, 622 1.1 leo segs, nsegs, rsegs, flags, 0, high)); 623 1.1 leo } 624 1.1 leo 625 1.1 leo /********************************************************************** 626 1.1 leo * ISA DMA utility functions 627 1.1 leo **********************************************************************/ 628 1.1 leo 629 1.1 leo int 630 1.9 dsl _isa_dma_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map, bus_size_t size, int flags) 631 1.1 leo { 632 1.1 leo struct atari_isa_dma_cookie *cookie = map->_dm_cookie; 633 1.1 leo int error = 0; 634 1.1 leo 635 1.1 leo cookie->id_bouncebuflen = round_page(size); 636 1.1 leo error = _isa_bus_dmamem_alloc(t, cookie->id_bouncebuflen, 637 1.1 leo PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs, 638 1.1 leo map->_dm_segcnt, &cookie->id_nbouncesegs, flags); 639 1.1 leo if (error) 640 1.1 leo goto out; 641 1.1 leo error = bus_dmamem_map(t, cookie->id_bouncesegs, 642 1.1 leo cookie->id_nbouncesegs, cookie->id_bouncebuflen, 643 1.5 christos (void **)&cookie->id_bouncebuf, flags); 644 1.1 leo 645 1.1 leo out: 646 1.1 leo if (error) { 647 1.1 leo bus_dmamem_free(t, cookie->id_bouncesegs, 648 1.1 leo cookie->id_nbouncesegs); 649 1.1 leo cookie->id_bouncebuflen = 0; 650 1.1 leo cookie->id_nbouncesegs = 0; 651 1.1 leo } else { 652 1.1 leo cookie->id_flags |= ID_HAS_BOUNCE; 653 1.1 leo STAT_INCR(isa_dma_stats_nbouncebufs); 654 1.1 leo } 655 1.1 leo 656 1.1 leo return (error); 657 1.1 leo } 658 1.1 leo 659 1.1 leo void 660 1.9 dsl _isa_dma_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map) 661 1.1 leo { 662 1.1 leo struct atari_isa_dma_cookie *cookie = map->_dm_cookie; 663 1.1 leo 664 1.1 leo STAT_DECR(isa_dma_stats_nbouncebufs); 665 1.1 leo 666 1.1 leo bus_dmamem_unmap(t, cookie->id_bouncebuf, 667 1.1 leo cookie->id_bouncebuflen); 668 1.1 leo bus_dmamem_free(t, cookie->id_bouncesegs, 669 1.1 leo cookie->id_nbouncesegs); 670 1.1 leo cookie->id_bouncebuflen = 0; 671 1.1 leo cookie->id_nbouncesegs = 0; 672 1.1 leo cookie->id_flags &= ~ID_HAS_BOUNCE; 673 1.1 leo } 674