Home | History | Annotate | Line # | Download | only in bus_dma
      1  1.5     skrll /*	$NetBSD: bus_dmamem_common.c,v 1.5 2022/11/12 07:48:34 skrll Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 1997, 1998, 2009 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  christos  * NASA Ames Research Center.
     10  1.1  christos  *
     11  1.1  christos  * Redistribution and use in source and binary forms, with or without
     12  1.1  christos  * modification, are permitted provided that the following conditions
     13  1.1  christos  * are met:
     14  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     16  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  christos  *    documentation and/or other materials provided with the distribution.
     19  1.1  christos  *
     20  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1  christos  */
     32  1.1  christos 
     33  1.1  christos #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     34  1.1  christos 
     35  1.5     skrll __KERNEL_RCSID(0, "$NetBSD: bus_dmamem_common.c,v 1.5 2022/11/12 07:48:34 skrll Exp $");
     36  1.1  christos 
     37  1.1  christos #include <sys/param.h>
     38  1.1  christos #include <sys/systm.h>
     39  1.1  christos #include <sys/proc.h>
     40  1.1  christos #include <sys/bus.h>
     41  1.1  christos 
     42  1.3  riastrad #include <uvm/uvm_extern.h>
     43  1.4  riastrad #include <uvm/uvm_page.h>
     44  1.1  christos 
     45  1.2  christos #include <dev/bus_dma/bus_dmamem_common.h>
     46  1.1  christos 
     47  1.1  christos /*
     48  1.1  christos  * _bus_dmamem_alloc_range_common --
     49  1.1  christos  *	Allocate physical memory from the specified physical address range.
     50  1.1  christos  */
     51  1.1  christos int
     52  1.1  christos _bus_dmamem_alloc_range_common(bus_dma_tag_t t,
     53  1.1  christos 			       bus_size_t size,
     54  1.1  christos 			       bus_size_t alignment,
     55  1.1  christos 			       bus_size_t boundary,
     56  1.1  christos 			       bus_dma_segment_t *segs,
     57  1.1  christos 			       int nsegs,
     58  1.1  christos 			       int *rsegs,
     59  1.1  christos 			       int flags,
     60  1.1  christos 			       paddr_t low,
     61  1.1  christos 			       paddr_t high)
     62  1.1  christos {
     63  1.1  christos 	paddr_t curaddr, lastaddr;
     64  1.1  christos 	struct vm_page *m;
     65  1.1  christos 	struct pglist mlist;
     66  1.1  christos 	int curseg, error;
     67  1.1  christos 
     68  1.1  christos 	/* Always round the size. */
     69  1.1  christos 	size = round_page(size);
     70  1.1  christos 
     71  1.1  christos 	/* Allocate pages from the VM system. */
     72  1.1  christos 	error = uvm_pglistalloc(size, low, high, alignment, boundary,
     73  1.1  christos 				&mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
     74  1.1  christos 	if (__predict_false(error != 0))
     75  1.1  christos 		return (error);
     76  1.5     skrll 
     77  1.1  christos 	/*
     78  1.1  christos 	 * Compute the location, size, and number of segments actually
     79  1.1  christos 	 * returned by the VM system.
     80  1.1  christos 	 */
     81  1.1  christos 	m = TAILQ_FIRST(&mlist);
     82  1.1  christos 	curseg = 0;
     83  1.1  christos 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
     84  1.1  christos 	segs[curseg].ds_len = PAGE_SIZE;
     85  1.1  christos 	m = TAILQ_NEXT(m, pageq.queue);
     86  1.1  christos 
     87  1.1  christos 	for (; m != NULL; m = TAILQ_NEXT(m, pageq.queue)) {
     88  1.1  christos 		curaddr = VM_PAGE_TO_PHYS(m);
     89  1.1  christos 		KASSERT(curaddr >= low);
     90  1.1  christos 		KASSERT(curaddr < high);
     91  1.1  christos 		if (curaddr == (lastaddr + PAGE_SIZE))
     92  1.1  christos 			segs[curseg].ds_len += PAGE_SIZE;
     93  1.1  christos 		else {
     94  1.1  christos 			curseg++;
     95  1.1  christos 			segs[curseg].ds_addr = curaddr;
     96  1.1  christos 			segs[curseg].ds_len = PAGE_SIZE;
     97  1.1  christos 		}
     98  1.1  christos 		lastaddr = curaddr;
     99  1.1  christos 	}
    100  1.1  christos 
    101  1.1  christos 	*rsegs = curseg + 1;
    102  1.1  christos 
    103  1.1  christos 	return (0);
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos /*
    107  1.1  christos  * _bus_dmamem_free_common --
    108  1.1  christos  *	Free memory allocated with _bus_dmamem_alloc_range_common()
    109  1.1  christos  *	back to the VM system.
    110  1.1  christos  */
    111  1.1  christos void
    112  1.1  christos _bus_dmamem_free_common(bus_dma_tag_t t,
    113  1.1  christos 			bus_dma_segment_t *segs,
    114  1.1  christos 			int nsegs)
    115  1.1  christos {
    116  1.1  christos 	struct vm_page *m;
    117  1.1  christos 	bus_addr_t addr;
    118  1.1  christos 	struct pglist mlist;
    119  1.1  christos 	int curseg;
    120  1.1  christos 
    121  1.1  christos 	TAILQ_INIT(&mlist);
    122  1.1  christos 	for (curseg = 0; curseg < nsegs; curseg++) {
    123  1.1  christos 		for (addr = segs[curseg].ds_addr;
    124  1.1  christos 		     addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    125  1.1  christos 		     addr += PAGE_SIZE) {
    126  1.1  christos 			m = PHYS_TO_VM_PAGE(addr);
    127  1.1  christos 			TAILQ_INSERT_TAIL(&mlist, m, pageq.queue);
    128  1.1  christos 		}
    129  1.1  christos 	}
    130  1.1  christos 
    131  1.1  christos 	uvm_pglistfree(&mlist);
    132  1.1  christos }
    133  1.1  christos 
    134  1.1  christos /*
    135  1.1  christos  * _bus_dmamem_map_common --
    136  1.1  christos  *	Map memory allocated with _bus_dmamem_alloc_range_common() into
    137  1.1  christos  *	the kernel virtual address space.
    138  1.1  christos  */
    139  1.1  christos int
    140  1.1  christos _bus_dmamem_map_common(bus_dma_tag_t t,
    141  1.1  christos 		       bus_dma_segment_t *segs,
    142  1.1  christos 		       int nsegs,
    143  1.1  christos 		       size_t size,
    144  1.1  christos 		       void **kvap,
    145  1.1  christos 		       int flags,
    146  1.1  christos 		       int pmapflags)
    147  1.1  christos {
    148  1.1  christos 	vaddr_t va;
    149  1.1  christos 	bus_addr_t addr;
    150  1.1  christos 	int curseg;
    151  1.1  christos 	const uvm_flag_t kmflags =
    152  1.1  christos 	    (flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0;
    153  1.1  christos 
    154  1.1  christos 	size = round_page(size);
    155  1.1  christos 
    156  1.1  christos 	va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY | kmflags);
    157  1.1  christos 	if (__predict_false(va == 0))
    158  1.1  christos 		return (ENOMEM);
    159  1.5     skrll 
    160  1.1  christos 	*kvap = (void *)va;
    161  1.1  christos 
    162  1.1  christos 	for (curseg = 0; curseg < nsegs; curseg++) {
    163  1.1  christos 		for (addr = segs[curseg].ds_addr;
    164  1.1  christos 		     addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    165  1.1  christos 		     addr += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
    166  1.1  christos 			KASSERT(size != 0);
    167  1.1  christos 			/* XXX pmap_kenter_pa()? */
    168  1.1  christos 			pmap_enter(pmap_kernel(), va, addr,
    169  1.1  christos 			    VM_PROT_READ | VM_PROT_WRITE,
    170  1.1  christos 			    pmapflags | PMAP_WIRED |
    171  1.1  christos 			    	VM_PROT_READ | VM_PROT_WRITE);
    172  1.1  christos 		}
    173  1.1  christos 	}
    174  1.1  christos 	pmap_update(pmap_kernel());
    175  1.1  christos 
    176  1.1  christos 	return (0);
    177  1.1  christos }
    178  1.1  christos 
    179  1.1  christos /*
    180  1.1  christos  * _bus_dmamem_unmap_common --
    181  1.1  christos  *	Remove a mapping created with _bus_dmamem_map_common().
    182  1.1  christos  */
    183  1.1  christos void
    184  1.1  christos _bus_dmamem_unmap_common(bus_dma_tag_t t,
    185  1.1  christos 			 void *kva,
    186  1.1  christos 			 size_t size)
    187  1.1  christos {
    188  1.1  christos 
    189  1.1  christos 	KASSERT(((vaddr_t)kva & PAGE_MASK) == 0);
    190  1.1  christos 
    191  1.1  christos 	size = round_page(size);
    192  1.1  christos 	/* XXX pmap_kremove()?  See above... */
    193  1.1  christos 	pmap_remove(pmap_kernel(), (vaddr_t)kva, (vaddr_t)kva + size);
    194  1.1  christos 	pmap_update(pmap_kernel());
    195  1.1  christos 	uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
    196  1.1  christos }
    197  1.1  christos 
    198  1.1  christos /*
    199  1.1  christos  * _bus_dmamem_mmap_common --
    200  1.1  christos  *	Mmap support for memory allocated with _bus_dmamem_alloc_range_common().
    201  1.1  christos  */
    202  1.1  christos bus_addr_t
    203  1.1  christos _bus_dmamem_mmap_common(bus_dma_tag_t t,
    204  1.1  christos 			bus_dma_segment_t *segs,
    205  1.1  christos 			int nsegs,
    206  1.1  christos 			off_t off,
    207  1.1  christos 			int prot,
    208  1.1  christos 			int flags)
    209  1.1  christos {
    210  1.1  christos 	int i;
    211  1.1  christos 
    212  1.1  christos 	for (i = 0; i < nsegs; i++) {
    213  1.1  christos 		KASSERT((off & PAGE_MASK) == 0);
    214  1.1  christos 		KASSERT((segs[i].ds_addr & PAGE_MASK) == 0);
    215  1.1  christos 		KASSERT((segs[i].ds_len & PAGE_MASK) == 0);
    216  1.1  christos 		if (off >= segs[i].ds_len) {
    217  1.1  christos 			off -= segs[i].ds_len;
    218  1.1  christos 			continue;
    219  1.1  christos 		}
    220  1.1  christos 
    221  1.1  christos 		/* XXX BUS_DMA_COHERENT */
    222  1.1  christos 
    223  1.1  christos 		return (segs[i].ds_addr + off);
    224  1.1  christos 	}
    225  1.1  christos 
    226  1.1  christos 	/* Page not found. */
    227  1.1  christos 	return ((bus_addr_t)-1);
    228  1.1  christos }
    229