xen_bus_dma.c revision 1.3.2.5 1 /* $NetBSD: xen_bus_dma.c,v 1.3.2.5 2006/09/16 11:18:59 ghen Exp $ */
2 /* NetBSD bus_dma.c,v 1.21 2005/04/16 07:53:35 yamt Exp */
3
4 /*-
5 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
10 * Simulation Facility, NASA Ames Research Center.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: xen_bus_dma.c,v 1.3.2.5 2006/09/16 11:18:59 ghen Exp $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/proc.h>
50
51 #include <machine/bus.h>
52 #include <machine/bus_private.h>
53
54 #include <uvm/uvm_extern.h>
55
56 extern paddr_t avail_end;
57
58 /* Pure 2^n version of get_order */
59 static __inline__ int get_order(unsigned long size)
60 {
61 int order = -1;
62 size = (size - 1) >> (PAGE_SHIFT - 1);
63 do {
64 size >>= 1;
65 order++;
66 } while (size);
67 return order;
68 }
69
70 static int
71 _xen_alloc_contig(bus_size_t size, bus_size_t alignment, bus_size_t boundary,
72 struct pglist *mlistp, int flags)
73 {
74 int order, i;
75 unsigned long npagesreq, npages, mfn;
76 bus_addr_t pa;
77 struct vm_page *pg, *pgnext;
78 int s, error;
79
80 /*
81 * When requesting a contigous memory region, the hypervisor will
82 * return a memory range aligned on size. This will automagically
83 * handle "boundary", but the only way to enforce alignment
84 * is to request a memory region of size max(alignment, size).
85 */
86 order = max(get_order(size), get_order(alignment));
87 npages = (1 << order);
88 npagesreq = (size >> PAGE_SHIFT);
89 KASSERT(npages >= npagesreq);
90
91 /* get npages from UWM, and give them back to the hypervisor */
92 error = uvm_pglistalloc(npages << PAGE_SHIFT, 0, avail_end, 0, 0,
93 mlistp, npages, (flags & BUS_DMA_NOWAIT) == 0);
94 if (error)
95 return (error);
96
97 for (pg = mlistp->tqh_first; pg != NULL; pg = pg->pageq.tqe_next) {
98 pa = VM_PAGE_TO_PHYS(pg);
99 mfn = xpmap_ptom(pa) >> PAGE_SHIFT;
100 xpmap_phys_to_machine_mapping[
101 (pa - XPMAP_OFFSET) >> PAGE_SHIFT] = INVALID_P2M_ENTRY;
102 if (HYPERVISOR_dom_mem_op(MEMOP_decrease_reservation,
103 &mfn, 1, 0) != 1) {
104 #ifdef DEBUG
105 printf("xen_alloc_contig: MEMOP_decrease_reservation "
106 "failed!\n");
107 #endif
108 xpmap_phys_to_machine_mapping[
109 (pa - XPMAP_OFFSET) >> PAGE_SHIFT] = mfn;
110 error = ENOMEM;
111 goto failed;
112 }
113 }
114 /* Get the new contiguous memory extent */
115 if (HYPERVISOR_dom_mem_op(MEMOP_increase_reservation,
116 &mfn, 1, order) != 1) {
117 #ifdef DEBUG
118 printf("xen_alloc_contig: MEMOP_increase_reservation "
119 "failed!\n");
120 #endif
121 error = ENOMEM;
122 pg = NULL;
123 goto failed;
124 }
125 s = splvm();
126 /* Map the new extent in place of the old pages */
127 for (pg = mlistp->tqh_first, i = 0; pg != NULL; pg = pgnext, i++) {
128 pgnext = pg->pageq.tqe_next;
129 pa = VM_PAGE_TO_PHYS(pg);
130 xpmap_phys_to_machine_mapping[
131 (pa - XPMAP_OFFSET) >> PAGE_SHIFT] = mfn+i;
132 xpq_queue_machphys_update((mfn+i) << PAGE_SHIFT, pa);
133 /* while here, give extra pages back to UVM */
134 if (i >= npagesreq) {
135 TAILQ_REMOVE(mlistp, pg, pageq);
136 uvm_pagefree(pg);
137 }
138
139 }
140 /* Flush updates through and flush the TLB */
141 xpq_queue_tlb_flush();
142 xpq_flush_queue();
143 splx(s);
144 return 0;
145
146 failed:
147 /*
148 * Attempt to recover from a failed decrease or increase reservation:
149 * if decrease_reservation failed, we don't have given all pages
150 * back to Xen; give them back to UVM, and get the missing pages
151 * from Xen.
152 * if increase_reservation failed, we expect pg to be NULL and we just
153 * get back the missing pages from Xen one by one.
154 */
155 /* give back remaining pages to UVM */
156 for (; pg != NULL; pg = pgnext) {
157 pgnext = pg->pageq.tqe_next;
158 TAILQ_REMOVE(mlistp, pg, pageq);
159 uvm_pagefree(pg);
160 }
161 /* remplace the pages that we already gave to Xen */
162 s = splvm();
163 for (pg = mlistp->tqh_first; pg != NULL; pg = pgnext) {
164 pgnext = pg->pageq.tqe_next;
165 if (HYPERVISOR_dom_mem_op(MEMOP_increase_reservation,
166 &mfn, 1, 0) != 1) {
167 printf("xen_alloc_contig: recovery "
168 "MEMOP_increase_reservation failed!\n");
169 break;
170 }
171 pa = VM_PAGE_TO_PHYS(pg);
172 xpmap_phys_to_machine_mapping[
173 (pa - XPMAP_OFFSET) >> PAGE_SHIFT] = mfn;
174 xpq_queue_machphys_update((mfn) << PAGE_SHIFT, pa);
175 TAILQ_REMOVE(mlistp, pg, pageq);
176 uvm_pagefree(pg);
177 }
178 /* Flush updates through and flush the TLB */
179 xpq_queue_tlb_flush();
180 xpq_flush_queue();
181 splx(s);
182 return error;
183 }
184
185
186 /*
187 * Allocate physical memory from the given physical address range.
188 * Called by DMA-safe memory allocation methods.
189 * We need our own version to deal with physical vs machine addresses.
190 */
191 int
192 _xen_bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size,
193 bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs,
194 int nsegs, int *rsegs, int flags, bus_addr_t low, bus_addr_t high)
195 {
196 bus_addr_t curaddr, lastaddr;
197 struct vm_page *m;
198 struct pglist mlist;
199 int curseg, error;
200 int doingrealloc = 0;
201
202 /* Always round the size. */
203 size = round_page(size);
204
205 KASSERT((alignment & (alignment - 1)) == 0);
206 KASSERT((boundary & (boundary - 1)) == 0);
207 if (alignment < PAGE_SIZE)
208 alignment = PAGE_SIZE;
209 if (boundary != 0 && boundary < size)
210 return (EINVAL);
211
212 /*
213 * Allocate pages from the VM system.
214 */
215 error = uvm_pglistalloc(size, 0, avail_end, alignment, boundary,
216 &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
217 if (error)
218 return (error);
219 again:
220
221 /*
222 * Compute the location, size, and number of segments actually
223 * returned by the VM code.
224 */
225 m = mlist.tqh_first;
226 curseg = 0;
227 lastaddr = segs[curseg].ds_addr = _BUS_VM_PAGE_TO_BUS(m);
228 segs[curseg].ds_len = PAGE_SIZE;
229 m = m->pageq.tqe_next;
230 if ((segs[curseg].ds_addr & (alignment - 1)) != 0)
231 goto dorealloc;
232
233 for (; m != NULL; m = m->pageq.tqe_next) {
234 curaddr = _BUS_VM_PAGE_TO_BUS(m);
235 if ((lastaddr < low || lastaddr >= high) ||
236 (curaddr < low || curaddr >= high)) {
237 /*
238 * If machine addresses are outside the allowed
239 * range we have to bail. Xen2 doesn't offer an
240 * interface to get memory in a specific address
241 * range.
242 */
243 printf("_xen_bus_dmamem_alloc_range: no way to "
244 "enforce address range\n");
245 uvm_pglistfree(&mlist);
246 return EINVAL;
247 }
248 if (curaddr == (lastaddr + PAGE_SIZE)) {
249 segs[curseg].ds_len += PAGE_SIZE;
250 if ((lastaddr & boundary) !=
251 (curaddr & boundary))
252 goto dorealloc;
253 } else {
254 curseg++;
255 if (curseg >= nsegs ||
256 (curaddr & (alignment - 1)) != 0) {
257 dorealloc:
258 if (doingrealloc == 1)
259 panic("_xen_bus_dmamem_alloc_range: "
260 "xen_alloc_contig returned "
261 "too much segments");
262 doingrealloc = 1;
263 /*
264 * Too much segments. Free this memory and
265 * get a contigous segment from the hypervisor.
266 */
267 uvm_pglistfree(&mlist);
268 for (curseg = 0; curseg < nsegs; curseg++) {
269 segs[curseg].ds_addr = 0;
270 segs[curseg].ds_len = 0;
271 }
272 error = _xen_alloc_contig(size, alignment,
273 boundary, &mlist, flags);
274 if (error)
275 return error;
276 goto again;
277 }
278 segs[curseg].ds_addr = curaddr;
279 segs[curseg].ds_len = PAGE_SIZE;
280 }
281 lastaddr = curaddr;
282 }
283
284 *rsegs = curseg + 1;
285
286 return (0);
287 }
288