xref: /netbsd-src/sys/arch/xen/x86/xen_shm_machdep.c (revision 10ad5ffa714ce1a679dcc9dd8159648df2d67b5a)
1 /*      $NetBSD: xen_shm_machdep.c,v 1.6 2009/07/29 12:02:08 cegger Exp $      */
2 
3 /*
4  * Copyright (c) 2006 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Manuel Bouyer.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: xen_shm_machdep.c,v 1.6 2009/07/29 12:02:08 cegger Exp $");
35 
36 
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/queue.h>
41 #include <sys/vmem.h>
42 #include <sys/kernel.h>
43 #include <uvm/uvm.h>
44 
45 #include <machine/pmap.h>
46 #include <xen/hypervisor.h>
47 #include <xen/xen.h>
48 #include <xen/evtchn.h>
49 #include <xen/xen_shm.h>
50 
51 /*
52  * Helper routines for the backend drivers. This implement the necessary
53  * functions to map a bunch of pages from foreign domains in our kernel VM
54  * space, do I/O to it, and unmap it.
55  *
56  * At boot time, we grap some kernel VM space that we'll use to map the foreign
57  * pages. We also maintain a virtual to machine mapping table to give back
58  * the appropriate address to bus_dma if requested.
59  * If no more VM space is available, we return an error. The caller can then
60  * register a callback which will be called when the required VM space is
61  * available.
62  */
63 
64 /* pointers to our VM space */
65 static vaddr_t xen_shm_base_address;
66 static u_long xen_shm_base_address_pg;
67 static vaddr_t xen_shm_end_address;
68 
69 /* Grab enouth VM space to map an entire vbd ring. */
70 /* Xen3 linux guests seems to eat more pages, gives enough for 10 vbd rings */
71 #define BLKIF_RING_SIZE __RING_SIZE((blkif_sring_t *)0, PAGE_SIZE)
72 #define XENSHM_NPAGES (BLKIF_RING_SIZE * (BLKIF_MAX_SEGMENTS_PER_REQUEST + 1) * 10)
73 
74 static vsize_t xen_shm_size = (XENSHM_NPAGES * PAGE_SIZE);
75 
76 /* vm space management */
77 static vmem_t *xen_shm_arena;
78 
79 /* callbacks are registered in a FIFO list. */
80 
81 static SIMPLEQ_HEAD(xen_shm_callback_head, xen_shm_callback_entry)
82     xen_shm_callbacks;
83 struct xen_shm_callback_entry {
84 	SIMPLEQ_ENTRY(xen_shm_callback_entry) xshmc_entries;
85 	int (*xshmc_callback)(void *); /* our callback */
86 	void *xshmc_arg; /* cookie passed to the callback */
87 };
88 /* a pool of struct xen_shm_callback_entry */
89 static struct pool xen_shm_callback_pool;
90 
91 #ifdef DEBUG
92 /* for ratecheck(9) */
93 static struct timeval xen_shm_errintvl = { 60, 0 };  /* a minute, each */
94 #endif
95 
96 void
97 xen_shm_init(void)
98 {
99 	SIMPLEQ_INIT(&xen_shm_callbacks);
100 	pool_init(&xen_shm_callback_pool, sizeof(struct xen_shm_callback_entry),
101 	    0, 0, 0, "xshmc", NULL, IPL_VM);
102 	/* ensure we'll always get items */
103 	if (pool_prime(&xen_shm_callback_pool,
104 	    PAGE_SIZE / sizeof(struct xen_shm_callback_entry)) != 0) {
105 		panic("xen_shm_init can't prime pool");
106 	}
107 
108 	xen_shm_base_address = uvm_km_alloc(kernel_map, xen_shm_size, 0,
109 	    UVM_KMF_VAONLY);
110 	xen_shm_end_address = xen_shm_base_address + xen_shm_size;
111 	xen_shm_base_address_pg = xen_shm_base_address >> PAGE_SHIFT;
112 	if (xen_shm_base_address == 0) {
113 		panic("xen_shm_init no VM space");
114 	}
115 	xen_shm_arena = vmem_create("xen_shm",
116 	    xen_shm_base_address_pg,
117 	    (xen_shm_end_address >> PAGE_SHIFT) - 1 - xen_shm_base_address_pg,
118 	    1, NULL, NULL, NULL, 1, VM_NOSLEEP, IPL_VM);
119 	if (xen_shm_arena == NULL) {
120 		panic("xen_shm_init no arena");
121 	}
122 }
123 
124 int
125 xen_shm_map(int nentries, int domid, grant_ref_t *grefp, vaddr_t *vap,
126     grant_handle_t *handlep, int flags)
127 {
128 	int s, i;
129 	vaddr_t new_va;
130 	u_long new_va_pg;
131 	int err;
132 	gnttab_map_grant_ref_t op[XENSHM_MAX_PAGES_PER_REQUEST];
133 
134 #ifdef DIAGNOSTIC
135 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
136 		printf("xen_shm_map: %d entries\n", nentries);
137 		panic("xen_shm_map");
138 	}
139 #endif
140 	s = splvm(); /* splvm is the lowest level blocking disk and net IRQ */
141 	/*
142 	 * if a driver is waiting for ressources, don't try to allocate
143 	 * yet. This is to avoid a flood of small requests stalling large
144 	 * ones.
145 	 */
146 	if (__predict_false(SIMPLEQ_FIRST(&xen_shm_callbacks) != NULL) &&
147 	    (flags & XSHM_CALLBACK) == 0) {
148 #ifdef DEBUG
149 		static struct timeval lasttime;
150 #endif
151 		splx(s);
152 #ifdef DEBUG
153 		if (ratecheck(&lasttime, &xen_shm_errintvl))
154 			printf("xen_shm_map: ENOMEM1\n");
155 #endif
156 		return ENOMEM;
157 	}
158 	/* allocate the needed virtual space */
159 	new_va_pg = vmem_alloc(xen_shm_arena, nentries,
160 	    VM_INSTANTFIT | VM_NOSLEEP);
161 	if (new_va_pg == 0) {
162 #ifdef DEBUG
163 		static struct timeval lasttime;
164 #endif
165 		splx(s);
166 #ifdef DEBUG
167 		if (ratecheck(&lasttime, &xen_shm_errintvl))
168 			printf("xen_shm_map: ENOMEM\n");
169 #endif
170 		return ENOMEM;
171 	}
172 	splx(s);
173 
174 	new_va = new_va_pg << PAGE_SHIFT;
175 	for (i = 0; i < nentries; i++) {
176 		op[i].host_addr = new_va + i * PAGE_SIZE;
177 		op[i].dom = domid;
178 		op[i].ref = grefp[i];
179 		op[i].flags = GNTMAP_host_map |
180 		    ((flags & XSHM_RO) ? GNTMAP_readonly : 0);
181 	}
182 	err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, op, nentries);
183 	if (__predict_false(err))
184 		panic("xen_shm_map: HYPERVISOR_grant_table_op failed");
185 	for (i = 0; i < nentries; i++) {
186 		if (__predict_false(op[i].status))
187 			return op[i].status;
188 		handlep[i] = op[i].handle;
189 	}
190 	*vap = new_va;
191 	return 0;
192 }
193 
194 void
195 xen_shm_unmap(vaddr_t va, int nentries, grant_handle_t *handlep)
196 {
197 	gnttab_unmap_grant_ref_t op[XENSHM_MAX_PAGES_PER_REQUEST];
198 	int ret;
199 	int i;
200 	int s;
201 	struct xen_shm_callback_entry *xshmc;
202 
203 #ifdef DIAGNOSTIC
204 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
205 		printf("xen_shm_unmap: %d entries\n", nentries);
206 		panic("xen_shm_unmap");
207 	}
208 #endif
209 
210 	for (i = 0; i < nentries; i++) {
211 		op[i].host_addr = va + i * PAGE_SIZE;
212 		op[i].dev_bus_addr = 0;
213 		op[i].handle = handlep[i];
214 	}
215 	ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
216 	    op, nentries);
217 	if (__predict_false(ret))
218 		panic("xen_shm_unmap: unmap failed");
219 	va = va >> PAGE_SHIFT;
220 	s = splvm(); /* splvm is the lowest level blocking disk and net IRQ */
221 	vmem_free(xen_shm_arena, va, nentries);
222 	while (__predict_false((xshmc = SIMPLEQ_FIRST(&xen_shm_callbacks))
223 	    != NULL)) {
224 		SIMPLEQ_REMOVE_HEAD(&xen_shm_callbacks, xshmc_entries);
225 		splx(s);
226 		if (xshmc->xshmc_callback(xshmc->xshmc_arg) == 0) {
227 			/* callback succeeded */
228 			s = splvm();
229 			pool_put(&xen_shm_callback_pool, xshmc);
230 		} else {
231 			/* callback failed, probably out of ressources */
232 			s = splvm();
233 			SIMPLEQ_INSERT_TAIL(&xen_shm_callbacks, xshmc,
234 					    xshmc_entries);
235 
236 			break;
237 		}
238 	}
239 	splx(s);
240 }
241 
242 int
243 xen_shm_callback(int (*callback)(void *), void *arg)
244 {
245 	struct xen_shm_callback_entry *xshmc;
246 	int s;
247 
248 	s = splvm();
249 	xshmc = pool_get(&xen_shm_callback_pool, PR_NOWAIT);
250 	if (xshmc == NULL) {
251 		splx(s);
252 		return ENOMEM;
253 	}
254 	xshmc->xshmc_arg = arg;
255 	xshmc->xshmc_callback = callback;
256 	SIMPLEQ_INSERT_TAIL(&xen_shm_callbacks, xshmc, xshmc_entries);
257 	splx(s);
258 	return 0;
259 }
260