xref: /netbsd-src/sys/arch/alpha/common/bus_dma.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /* $NetBSD: bus_dma.c,v 1.2 1997/06/06 23:58:02 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <machine/options.h>		/* Config options headers */
41 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
42 
43 __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.2 1997/06/06 23:58:02 thorpej Exp $");
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/device.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 
52 #include <vm/vm.h>
53 #include <vm/vm_kern.h>
54 
55 #define _ALPHA_BUS_DMA_PRIVATE
56 #include <machine/bus.h>
57 
58 /*
59  * Common function for DMA map creation.  May be called by bus-specific
60  * DMA map creation functions.
61  */
62 int
63 _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
64 	bus_dma_tag_t t;
65 	bus_size_t size;
66 	int nsegments;
67 	bus_size_t maxsegsz;
68 	bus_size_t boundary;
69 	int flags;
70 	bus_dmamap_t *dmamp;
71 {
72 	struct alpha_bus_dmamap *map;
73 	void *mapstore;
74 	size_t mapsize;
75 
76 	/*
77 	 * Allcoate and initialize the DMA map.  The end of the map
78 	 * is a variable-sized array of segments, so we allocate enough
79 	 * room for them in one shot.
80 	 *
81 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
82 	 * of ALLOCNOW notifes others that we've reserved these resources,
83 	 * and they are not to be freed.
84 	 *
85 	 * The bus_dmamap_t includes one bus_dma_segment_t, hence
86 	 * the (nsegments - 1).
87 	 */
88 	mapsize = sizeof(struct alpha_bus_dmamap) +
89 	    (sizeof(bus_dma_segment_t) * (nsegments - 1));
90 	if ((mapstore = malloc(mapsize, M_DEVBUF,
91 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
92 		return (ENOMEM);
93 
94 	bzero(mapstore, mapsize);
95 	map = (struct alpha_bus_dmamap *)mapstore;
96 	map->_dm_size = size;
97 	map->_dm_segcnt = nsegments;
98 	map->_dm_maxsegsz = maxsegsz;
99 	map->_dm_boundary = boundary;
100 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
101 	map->dm_nsegs = 0;		/* no valid mappings */
102 
103 	*dmamp = map;
104 	return (0);
105 }
106 
107 /*
108  * Common function for DMA map destruction.  May be called by bus-specific
109  * DMA map destruction functions.
110  */
111 void
112 _bus_dmamap_destroy(t, map)
113 	bus_dma_tag_t t;
114 	bus_dmamap_t map;
115 {
116 
117 	free(map, M_DEVBUF);
118 }
119 
120 /*
121  * Common function for loading a direct-mapped DMA map with a linear
122  * buffer.  Called by bus-specific DMA map load functions with the
123  * OR value appropriate for indicating "direct-mapped" for that
124  * chipset.
125  */
126 int
127 _bus_dmamap_load_direct_common(t, map, buf, buflen, p, flags, wbase)
128 	bus_dma_tag_t t;
129 	bus_dmamap_t map;
130 	void *buf;
131 	bus_size_t buflen;
132 	struct proc *p;
133 	int flags;
134 	bus_addr_t wbase;
135 {
136 	bus_size_t sgsize;
137 	vm_offset_t curaddr, lastaddr;
138 	vm_offset_t vaddr = (vm_offset_t)buf;
139 	int first, seg;
140 
141 	/*
142 	 * Make sure that on error condition we return "no valid mappings".
143 	 */
144 	map->dm_nsegs = 0;
145 
146 	if (buflen > map->_dm_size)
147 		return (EINVAL);
148 
149 	/*
150 	 * XXX Need to implement "don't dma across this boundry".
151 	 */
152 
153 	lastaddr = ~0;		/* XXX gcc */
154 	for (first = 1, seg = 0; buflen > 0 && seg < map->_dm_segcnt; ) {
155 		/*
156 		 * Get the physical address for this segment.
157 		 */
158 		if (p != NULL)
159 			curaddr = pmap_extract(&p->p_vmspace->vm_pmap, vaddr);
160 		else
161 			curaddr = vtophys(vaddr);
162 
163 		curaddr |= wbase;
164 
165 		/*
166 		 * Compute the segment size, and adjust counts.
167 		 */
168 		sgsize = NBPG - ((u_long)vaddr & PGOFSET);
169 		if (buflen < sgsize)
170 			sgsize = buflen;
171 
172 		/*
173 		 * Insert chunk into a segment, coalescing with
174 		 * the previous segment if possible.
175 		 */
176 		if (first) {
177 			map->dm_segs[seg].ds_addr = curaddr;
178 			map->dm_segs[seg].ds_len = sgsize;
179 			first = 0;
180 		} else {
181 			if (curaddr == lastaddr &&
182 			    (map->dm_segs[seg].ds_len + sgsize) <=
183 			    map->_dm_maxsegsz)
184 				map->dm_segs[seg].ds_len += sgsize;
185 			else {
186 				seg++;
187 				map->dm_segs[seg].ds_addr = curaddr;
188 				map->dm_segs[seg].ds_len = sgsize;
189 			}
190 		}
191 
192 		lastaddr = curaddr + sgsize;
193 		vaddr += sgsize;
194 		buflen -= sgsize;
195 	}
196 
197 	/*
198 	 * Did we fit?
199 	 */
200 	if (buflen != 0) {
201 		/*
202 		 * XXX Should fall back on SGMAPs.
203 		 */
204 		return (EFBIG);		/* XXX better return value here? */
205 	}
206 
207 	map->dm_nsegs = seg + 1;
208 	return (0);
209 }
210 
211 /*
212  * Like _bus_dmamap_load_direct_common(), but for mbufs.
213  */
214 int
215 _bus_dmamap_load_mbuf_direct_common(t, map, m, flags, wbase)
216 	bus_dma_tag_t t;
217 	bus_dmamap_t map;
218 	struct mbuf *m;
219 	int flags;
220 	bus_addr_t wbase;
221 {
222 
223 	panic("_bus_dmamap_load_mbuf_direct_common: not implemented");
224 }
225 
226 /*
227  * Like _bus_dmamap_load_direct_common(), but for uios.
228  */
229 int
230 _bus_dmamap_load_uio_direct_common(t, map, uio, flags, wbase)
231 	bus_dma_tag_t t;
232 	bus_dmamap_t map;
233 	struct uio *uio;
234 	int flags;
235 	bus_addr_t wbase;
236 {
237 
238 	panic("_bus_dmamap_load_uio_direct_common: not implemented");
239 }
240 
241 /*
242  * Like _bus_dmamap_load_direct_common(), but for raw memory.
243  */
244 int
245 _bus_dmamap_load_raw_direct_common(t, map, segs, nsegs, size, flags, wbase)
246 	bus_dma_tag_t t;
247 	bus_dmamap_t map;
248 	bus_dma_segment_t *segs;
249 	int nsegs;
250 	bus_size_t size;
251 	int flags;
252 	bus_addr_t wbase;
253 {
254 
255 	panic("_bus_dmamap_load_raw_direct_common: not implemented");
256 }
257 
258 /*
259  * Common function for unloading a DMA map.  May be called by
260  * chipset-specific DMA map unload functions.
261  */
262 void
263 _bus_dmamap_unload(t, map)
264 	bus_dma_tag_t t;
265 	bus_dmamap_t map;
266 {
267 
268 	/*
269 	 * No resources to free; just mark the mappings as
270 	 * invalid.
271 	 */
272 	map->dm_nsegs = 0;
273 }
274 
275 /*
276  * Common function for DMA map synchronization.  May be called
277  * by chipset-specific DMA map synchronization functions.
278  */
279 void
280 _bus_dmamap_sync(t, map, op)
281 	bus_dma_tag_t t;
282 	bus_dmamap_t map;
283 	bus_dmasync_op_t op;
284 {
285 
286 	/* Nothing to do. */
287 }
288 
289 /*
290  * Common function for DMA-safe memory allocation.  May be called
291  * by bus-specific DMA memory allocation functions.
292  */
293 int
294 _bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
295 	bus_dma_tag_t t;
296 	bus_size_t size, alignment, boundary;
297 	bus_dma_segment_t *segs;
298 	int nsegs;
299 	int *rsegs;
300 	int flags;
301 {
302 	extern vm_offset_t vm_first_phys, vm_last_phys;
303 	vm_offset_t curaddr, lastaddr, high;
304 	vm_page_t m;
305 	struct pglist mlist;
306 	int curseg, error;
307 
308 	/* Always round the size. */
309 	size = round_page(size);
310 
311 	high = vm_last_phys - PAGE_SIZE;
312 
313 	/*
314 	 * Allocate pages from the VM system.
315 	 */
316 	TAILQ_INIT(&mlist);
317 	error = vm_page_alloc_memory(size, vm_first_phys, high,
318 	    alignment, boundary, &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
319 	if (error)
320 		return (error);
321 
322 	/*
323 	 * Compute the location, size, and number of segments actually
324 	 * returned by the VM code.
325 	 */
326 	m = mlist.tqh_first;
327 	curseg = 0;
328 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
329 	segs[curseg].ds_len = PAGE_SIZE;
330 	m = m->pageq.tqe_next;
331 
332 	for (; m != NULL; m = m->pageq.tqe_next) {
333 		curaddr = VM_PAGE_TO_PHYS(m);
334 #ifdef DIAGNOSTIC
335 		if (curaddr < vm_first_phys || curaddr >= high) {
336 			printf("vm_page_alloc_memory returned non-sensical"
337 			    " address 0x%lx\n", curaddr);
338 			panic("_bus_dmamem_alloc");
339 		}
340 #endif
341 		if (curaddr == (lastaddr + PAGE_SIZE))
342 			segs[curseg].ds_len += PAGE_SIZE;
343 		else {
344 			curseg++;
345 			segs[curseg].ds_addr = curaddr;
346 			segs[curseg].ds_len = PAGE_SIZE;
347 		}
348 		lastaddr = curaddr;
349 	}
350 
351 	*rsegs = curseg + 1;
352 
353 	return (0);
354 }
355 
356 /*
357  * Common function for freeing DMA-safe memory.  May be called by
358  * bus-specific DMA memory free functions.
359  */
360 void
361 _bus_dmamem_free(t, segs, nsegs)
362 	bus_dma_tag_t t;
363 	bus_dma_segment_t *segs;
364 	int nsegs;
365 {
366 	vm_page_t m;
367 	bus_addr_t addr;
368 	struct pglist mlist;
369 	int curseg;
370 
371 	/*
372 	 * Build a list of pages to free back to the VM system.
373 	 */
374 	TAILQ_INIT(&mlist);
375 	for (curseg = 0; curseg < nsegs; curseg++) {
376 		for (addr = segs[curseg].ds_addr;
377 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
378 		    addr += PAGE_SIZE) {
379 			m = PHYS_TO_VM_PAGE(addr);
380 			TAILQ_INSERT_TAIL(&mlist, m, pageq);
381 		}
382 	}
383 
384 	vm_page_free_memory(&mlist);
385 }
386 
387 /*
388  * Common function for mapping DMA-safe memory.  May be called by
389  * bus-specific DMA memory map functions.
390  */
391 int
392 _bus_dmamem_map(t, segs, nsegs, size, kvap, flags)
393 	bus_dma_tag_t t;
394 	bus_dma_segment_t *segs;
395 	int nsegs;
396 	size_t size;
397 	caddr_t *kvap;
398 	int flags;
399 {
400 	vm_offset_t va;
401 	bus_addr_t addr;
402 	int curseg;
403 
404 	size = round_page(size);
405 	va = kmem_alloc_pageable(kmem_map, size);
406 	if (va == 0)
407 		return (ENOMEM);
408 
409 	*kvap = (caddr_t)va;
410 
411 	for (curseg = 0; curseg < nsegs; curseg++) {
412 		for (addr = segs[curseg].ds_addr;
413 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
414 		    addr += NBPG, va += NBPG, size -= NBPG) {
415 			if (size == 0)
416 				panic("_bus_dmamem_map: size botch");
417 			pmap_enter(pmap_kernel(), va, addr,
418 			    VM_PROT_READ | VM_PROT_WRITE, TRUE);
419 #if 0
420 			if (flags & BUS_DMAMEM_NOSYNC)
421 				/* XXX make non-cacheable? */ ;
422 #endif
423 		}
424 	}
425 
426 	return (0);
427 }
428 
429 /*
430  * Common function for unmapping DMA-safe memory.  May be called by
431  * bus-specific DMA memory unmapping functions.
432  */
433 void
434 _bus_dmamem_unmap(t, kva, size)
435 	bus_dma_tag_t t;
436 	caddr_t kva;
437 	size_t size;
438 {
439 
440 #ifdef DIAGNOSTIC
441 	if ((u_long)kva & PGOFSET)
442 		panic("_bus_dmamem_unmap");
443 #endif
444 
445 	size = round_page(size);
446 	kmem_free(kmem_map, (vm_offset_t)kva, size);
447 }
448 
449 /*
450  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
451  * bus-specific DMA mmap(2)'ing functions.
452  */
453 int
454 _bus_dmamem_mmap(t, segs, nsegs, off, prot, flags)
455 	bus_dma_tag_t t;
456 	bus_dma_segment_t *segs;
457 	int nsegs, off, prot, flags;
458 {
459 
460 	panic("_bus_dmamem_mmap: not implemented");
461 }
462