bus_dmamem_common.c revision 1.2 1 1.2 christos /* $NetBSD: bus_dmamem_common.c,v 1.2 2012/10/02 23:49:19 christos 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.2 christos __KERNEL_RCSID(0, "$NetBSD: bus_dmamem_common.c,v 1.2 2012/10/02 23:49:19 christos 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.1 christos #include <uvm/uvm.h>
43 1.1 christos
44 1.2 christos #include <dev/bus_dma/bus_dmamem_common.h>
45 1.1 christos
46 1.1 christos /*
47 1.1 christos * _bus_dmamem_alloc_range_common --
48 1.1 christos * Allocate physical memory from the specified physical address range.
49 1.1 christos */
50 1.1 christos int
51 1.1 christos _bus_dmamem_alloc_range_common(bus_dma_tag_t t,
52 1.1 christos bus_size_t size,
53 1.1 christos bus_size_t alignment,
54 1.1 christos bus_size_t boundary,
55 1.1 christos bus_dma_segment_t *segs,
56 1.1 christos int nsegs,
57 1.1 christos int *rsegs,
58 1.1 christos int flags,
59 1.1 christos paddr_t low,
60 1.1 christos paddr_t high)
61 1.1 christos {
62 1.1 christos paddr_t curaddr, lastaddr;
63 1.1 christos struct vm_page *m;
64 1.1 christos struct pglist mlist;
65 1.1 christos int curseg, error;
66 1.1 christos
67 1.1 christos /* Always round the size. */
68 1.1 christos size = round_page(size);
69 1.1 christos
70 1.1 christos /* Allocate pages from the VM system. */
71 1.1 christos error = uvm_pglistalloc(size, low, high, alignment, boundary,
72 1.1 christos &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
73 1.1 christos if (__predict_false(error != 0))
74 1.1 christos return (error);
75 1.1 christos
76 1.1 christos /*
77 1.1 christos * Compute the location, size, and number of segments actually
78 1.1 christos * returned by the VM system.
79 1.1 christos */
80 1.1 christos m = TAILQ_FIRST(&mlist);
81 1.1 christos curseg = 0;
82 1.1 christos lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
83 1.1 christos segs[curseg].ds_len = PAGE_SIZE;
84 1.1 christos m = TAILQ_NEXT(m, pageq.queue);
85 1.1 christos
86 1.1 christos for (; m != NULL; m = TAILQ_NEXT(m, pageq.queue)) {
87 1.1 christos curaddr = VM_PAGE_TO_PHYS(m);
88 1.1 christos KASSERT(curaddr >= low);
89 1.1 christos KASSERT(curaddr < high);
90 1.1 christos if (curaddr == (lastaddr + PAGE_SIZE))
91 1.1 christos segs[curseg].ds_len += PAGE_SIZE;
92 1.1 christos else {
93 1.1 christos curseg++;
94 1.1 christos segs[curseg].ds_addr = curaddr;
95 1.1 christos segs[curseg].ds_len = PAGE_SIZE;
96 1.1 christos }
97 1.1 christos lastaddr = curaddr;
98 1.1 christos }
99 1.1 christos
100 1.1 christos *rsegs = curseg + 1;
101 1.1 christos
102 1.1 christos return (0);
103 1.1 christos }
104 1.1 christos
105 1.1 christos /*
106 1.1 christos * _bus_dmamem_free_common --
107 1.1 christos * Free memory allocated with _bus_dmamem_alloc_range_common()
108 1.1 christos * back to the VM system.
109 1.1 christos */
110 1.1 christos void
111 1.1 christos _bus_dmamem_free_common(bus_dma_tag_t t,
112 1.1 christos bus_dma_segment_t *segs,
113 1.1 christos int nsegs)
114 1.1 christos {
115 1.1 christos struct vm_page *m;
116 1.1 christos bus_addr_t addr;
117 1.1 christos struct pglist mlist;
118 1.1 christos int curseg;
119 1.1 christos
120 1.1 christos TAILQ_INIT(&mlist);
121 1.1 christos for (curseg = 0; curseg < nsegs; curseg++) {
122 1.1 christos for (addr = segs[curseg].ds_addr;
123 1.1 christos addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
124 1.1 christos addr += PAGE_SIZE) {
125 1.1 christos m = PHYS_TO_VM_PAGE(addr);
126 1.1 christos TAILQ_INSERT_TAIL(&mlist, m, pageq.queue);
127 1.1 christos }
128 1.1 christos }
129 1.1 christos
130 1.1 christos uvm_pglistfree(&mlist);
131 1.1 christos }
132 1.1 christos
133 1.1 christos /*
134 1.1 christos * _bus_dmamem_map_common --
135 1.1 christos * Map memory allocated with _bus_dmamem_alloc_range_common() into
136 1.1 christos * the kernel virtual address space.
137 1.1 christos */
138 1.1 christos int
139 1.1 christos _bus_dmamem_map_common(bus_dma_tag_t t,
140 1.1 christos bus_dma_segment_t *segs,
141 1.1 christos int nsegs,
142 1.1 christos size_t size,
143 1.1 christos void **kvap,
144 1.1 christos int flags,
145 1.1 christos int pmapflags)
146 1.1 christos {
147 1.1 christos vaddr_t va;
148 1.1 christos bus_addr_t addr;
149 1.1 christos int curseg;
150 1.1 christos const uvm_flag_t kmflags =
151 1.1 christos (flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0;
152 1.1 christos
153 1.1 christos size = round_page(size);
154 1.1 christos
155 1.1 christos va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY | kmflags);
156 1.1 christos if (__predict_false(va == 0))
157 1.1 christos return (ENOMEM);
158 1.1 christos
159 1.1 christos *kvap = (void *)va;
160 1.1 christos
161 1.1 christos for (curseg = 0; curseg < nsegs; curseg++) {
162 1.1 christos for (addr = segs[curseg].ds_addr;
163 1.1 christos addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
164 1.1 christos addr += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
165 1.1 christos KASSERT(size != 0);
166 1.1 christos /* XXX pmap_kenter_pa()? */
167 1.1 christos pmap_enter(pmap_kernel(), va, addr,
168 1.1 christos VM_PROT_READ | VM_PROT_WRITE,
169 1.1 christos pmapflags | PMAP_WIRED |
170 1.1 christos VM_PROT_READ | VM_PROT_WRITE);
171 1.1 christos }
172 1.1 christos }
173 1.1 christos pmap_update(pmap_kernel());
174 1.1 christos
175 1.1 christos return (0);
176 1.1 christos }
177 1.1 christos
178 1.1 christos /*
179 1.1 christos * _bus_dmamem_unmap_common --
180 1.1 christos * Remove a mapping created with _bus_dmamem_map_common().
181 1.1 christos */
182 1.1 christos void
183 1.1 christos _bus_dmamem_unmap_common(bus_dma_tag_t t,
184 1.1 christos void *kva,
185 1.1 christos size_t size)
186 1.1 christos {
187 1.1 christos
188 1.1 christos KASSERT(((vaddr_t)kva & PAGE_MASK) == 0);
189 1.1 christos
190 1.1 christos size = round_page(size);
191 1.1 christos /* XXX pmap_kremove()? See above... */
192 1.1 christos pmap_remove(pmap_kernel(), (vaddr_t)kva, (vaddr_t)kva + size);
193 1.1 christos pmap_update(pmap_kernel());
194 1.1 christos uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
195 1.1 christos }
196 1.1 christos
197 1.1 christos /*
198 1.1 christos * _bus_dmamem_mmap_common --
199 1.1 christos * Mmap support for memory allocated with _bus_dmamem_alloc_range_common().
200 1.1 christos */
201 1.1 christos bus_addr_t
202 1.1 christos _bus_dmamem_mmap_common(bus_dma_tag_t t,
203 1.1 christos bus_dma_segment_t *segs,
204 1.1 christos int nsegs,
205 1.1 christos off_t off,
206 1.1 christos int prot,
207 1.1 christos int flags)
208 1.1 christos {
209 1.1 christos int i;
210 1.1 christos
211 1.1 christos for (i = 0; i < nsegs; i++) {
212 1.1 christos KASSERT((off & PAGE_MASK) == 0);
213 1.1 christos KASSERT((segs[i].ds_addr & PAGE_MASK) == 0);
214 1.1 christos KASSERT((segs[i].ds_len & PAGE_MASK) == 0);
215 1.1 christos if (off >= segs[i].ds_len) {
216 1.1 christos off -= segs[i].ds_len;
217 1.1 christos continue;
218 1.1 christos }
219 1.1 christos
220 1.1 christos /* XXX BUS_DMA_COHERENT */
221 1.1 christos
222 1.1 christos return (segs[i].ds_addr + off);
223 1.1 christos }
224 1.1 christos
225 1.1 christos /* Page not found. */
226 1.1 christos return ((bus_addr_t)-1);
227 1.1 christos }
228