xref: /netbsd-src/sys/arch/xen/x86/xen_bus_dma.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: xen_bus_dma.c,v 1.6 2006/01/15 22:09:52 bouyer 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.6 2006/01/15 22:09:52 bouyer 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 	struct pglist freelist;
79 	int s, error;
80 #ifdef XEN3
81 	struct xen_memory_reservation res;
82 #endif
83 
84 	TAILQ_INIT(&freelist);
85 
86 	/*
87 	 * When requesting a contigous memory region, the hypervisor will
88 	 * return a memory range aligned on size. This will automagically
89 	 * handle "boundary", but the only way to enforce alignment
90 	 * is to request a memory region of size max(alignment, size).
91 	 */
92 	order = max(get_order(size), get_order(alignment));
93 	npages = (1 << order);
94 	npagesreq = (size >> PAGE_SHIFT);
95 	KASSERT(npages >= npagesreq);
96 
97 	/* get npages from UWM, and give them back to the hypervisor */
98 	error = uvm_pglistalloc(npages << PAGE_SHIFT, 0, avail_end, 0, 0,
99 	    mlistp, npages, (flags & BUS_DMA_NOWAIT) == 0);
100 	if (error)
101 		return (error);
102 
103 	for (pg = mlistp->tqh_first; pg != NULL; pg = pg->pageq.tqe_next) {
104 		pa = VM_PAGE_TO_PHYS(pg);
105 		mfn = xpmap_ptom(pa) >> PAGE_SHIFT;
106 		xpmap_phys_to_machine_mapping[
107 		    (pa - XPMAP_OFFSET) >> PAGE_SHIFT] = INVALID_P2M_ENTRY;
108 #ifdef XEN3
109 		res.extent_start = &mfn;
110 		res.nr_extents = 1;
111 		res.extent_order = 0;
112 		res.domid = DOMID_SELF;
113 		if (HYPERVISOR_memory_op(XENMEM_decrease_reservation, &res)
114 		    < 0) {
115 			printf("xen_alloc_contig: XENMEM_decrease_reservation "
116 			    "failed!\n");
117 			return ENOMEM;
118 		}
119 #else
120 		if (HYPERVISOR_dom_mem_op(MEMOP_decrease_reservation,
121 		    &mfn, 1, 0) != 1) {
122 			printf("xen_alloc_contig: MEMOP_decrease_reservation "
123 			    "failed!\n");
124 			return ENOMEM;
125 		}
126 #endif
127 	}
128 	/* Get the new contiguous memory extent */
129 #ifdef XEN3
130 	res.extent_start = &mfn;
131 	res.nr_extents = 1;
132 	res.extent_order = order;
133 	res.address_bits = 31;
134 	res.domid = DOMID_SELF;
135 	if (HYPERVISOR_memory_op(XENMEM_increase_reservation, &res) < 0) {
136 		printf("xen_alloc_contig: XENMEM_increase_reservation "
137 		    "failed!\n");
138 		return ENOMEM;
139 	}
140 #else
141 	if (HYPERVISOR_dom_mem_op(MEMOP_increase_reservation,
142 	    &mfn, 1, order) != 1) {
143 		printf("xen_alloc_contig: MEMOP_increase_reservation "
144 		    "failed!\n");
145 		return ENOMEM;
146 	}
147 #endif
148 	s = splvm();
149 	/* Map the new extent in place of the old pages */
150 	for (pg = mlistp->tqh_first, i = 0; pg != NULL; pg = pgnext, i++) {
151 		pgnext = pg->pageq.tqe_next;
152 		pa = VM_PAGE_TO_PHYS(pg);
153 		xpmap_phys_to_machine_mapping[
154 		    (pa - XPMAP_OFFSET) >> PAGE_SHIFT] = mfn+i;
155 		xpq_queue_machphys_update((mfn+i) << PAGE_SHIFT, pa);
156 		/* while here, give extra pages back to UVM */
157 		if (i >= npagesreq) {
158 			TAILQ_REMOVE(mlistp, pg, pageq);
159 			uvm_pagefree(pg);
160 		}
161 
162 	}
163 	/* Flush updates through and flush the TLB */
164 	xpq_queue_tlb_flush();
165 	xpq_flush_queue();
166 	splx(s);
167 	return 0;
168 }
169 
170 
171 /*
172  * Allocate physical memory from the given physical address range.
173  * Called by DMA-safe memory allocation methods.
174  * We need our own version to deal with physical vs machine addresses.
175  */
176 int
177 _xen_bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size,
178     bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs,
179     int nsegs, int *rsegs, int flags, bus_addr_t low, bus_addr_t high)
180 {
181 	paddr_t curaddr, lastaddr;
182 	bus_addr_t bus_curaddr, bus_lastaddr;
183 	struct vm_page *m;
184 	struct pglist mlist;
185 	int curseg, error;
186 	int doingrealloc = 0;
187 
188 	/* Always round the size. */
189 	size = round_page(size);
190 
191 	KASSERT((alignment & (alignment - 1)) == 0);
192 	KASSERT((boundary & (boundary - 1)) == 0);
193 	if (alignment < PAGE_SIZE)
194 		alignment = PAGE_SIZE;
195 	if (boundary != 0 && boundary < size)
196 		return (EINVAL);
197 
198 	/*
199 	 * Allocate pages from the VM system.
200 	 */
201 	error = uvm_pglistalloc(size, 0, avail_end, alignment, boundary,
202 	    &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
203 	if (error)
204 		return (error);
205 again:
206 
207 	/*
208 	 * Compute the location, size, and number of segments actually
209 	 * returned by the VM code.
210 	 */
211 	m = mlist.tqh_first;
212 	curseg = 0;
213 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
214 	segs[curseg].ds_len = PAGE_SIZE;
215 	m = m->pageq.tqe_next;
216 	if ((_BUS_PHYS_TO_BUS(segs[curseg].ds_addr) & (alignment - 1)) != 0)
217 		goto dorealloc;
218 
219 	for (; m != NULL; m = m->pageq.tqe_next) {
220 		curaddr = VM_PAGE_TO_PHYS(m);
221 		bus_curaddr = _BUS_PHYS_TO_BUS(curaddr);
222 		bus_lastaddr = _BUS_PHYS_TO_BUS(lastaddr);
223 		if ((bus_lastaddr < low || bus_lastaddr >= high) ||
224 		    (bus_curaddr < low || bus_curaddr >= high)) {
225 			/*
226 			 * If machine addresses are outside the allowed
227 			 * range we have to bail. Xen2 doesn't offer an
228 			 * interface to get memory in a specific address
229 			 * range.
230 			 */
231 			printf("_xen_bus_dmamem_alloc_range: no way to "
232 			    "enforce address range\n");
233 			uvm_pglistfree(&mlist);
234 			return EINVAL;
235 		}
236 		if (bus_curaddr == (bus_lastaddr + PAGE_SIZE)) {
237 			segs[curseg].ds_len += PAGE_SIZE;
238 			if ((bus_lastaddr & boundary) !=
239 			    (bus_curaddr & boundary))
240 				goto dorealloc;
241 		}
242 		else {
243 			curseg++;
244 			if (curseg >= nsegs ||
245 			    (bus_curaddr & (alignment - 1)) != 0) {
246 dorealloc:
247 				if (doingrealloc == 1)
248 					panic("_xen_bus_dmamem_alloc_range: "
249 					   "xen_alloc_contig returned "
250 					   "too much segments");
251 				doingrealloc = 1;
252 				/*
253 				 * Too much segments. Free this memory and
254 				 * get a contigous segment from the hypervisor.
255 				 */
256 				uvm_pglistfree(&mlist);
257 				for (curseg = 0; curseg < nsegs; curseg++) {
258 					segs[curseg].ds_addr = 0;
259 					segs[curseg].ds_len = 0;
260 				}
261 				error = _xen_alloc_contig(size, alignment,
262 				    boundary, &mlist, flags);
263 				if (error)
264 					return error;
265 				goto again;
266 			}
267 			segs[curseg].ds_addr = curaddr;
268 			segs[curseg].ds_len = PAGE_SIZE;
269 		}
270 		lastaddr = curaddr;
271 	}
272 
273 	*rsegs = curseg + 1;
274 
275 	return (0);
276 }
277