viomb.c revision 1.4 1 /* $NetBSD: viomb.c,v 1.4 2015/05/04 14:02:13 ozaki-r Exp $ */
2
3 /*
4 * Copyright (c) 2010 Minoura Makoto.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: viomb.c,v 1.4 2015/05/04 14:02:13 ozaki-r Exp $");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/condvar.h>
36 #include <sys/device.h>
37 #include <sys/kthread.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <uvm/uvm_page.h>
41
42 #include <dev/pci/pcidevs.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
45
46 #include <dev/pci/virtioreg.h>
47 #include <dev/pci/virtiovar.h>
48
49 /* Configuration registers */
50 #define VIRTIO_BALLOON_CONFIG_NUM_PAGES 0 /* 32bit */
51 #define VIRTIO_BALLOON_CONFIG_ACTUAL 4 /* 32bit */
52
53 /* Feature bits */
54 #define VIRTIO_BALLOON_F_MUST_TELL_HOST (1<<0)
55 #define VIRTIO_BALLOON_F_STATS_VQ (1<<1)
56
57 #define PGS_PER_REQ (256) /* 1MB, 4KB/page */
58
59 CTASSERT((PAGE_SIZE) == (VIRTIO_PAGE_SIZE)); /* XXX */
60
61 struct balloon_req {
62 bus_dmamap_t bl_dmamap;
63 struct pglist bl_pglist;
64 int bl_nentries;
65 uint32_t bl_pages[PGS_PER_REQ];
66 };
67
68 struct viomb_softc {
69 device_t sc_dev;
70
71 struct virtio_softc *sc_virtio;
72 struct virtqueue sc_vq[2];
73
74 unsigned int sc_npages;
75 unsigned int sc_actual;
76 int sc_inflight;
77 struct balloon_req sc_req;
78 struct pglist sc_balloon_pages;
79
80 int sc_inflate_done;
81 int sc_deflate_done;
82
83 kcondvar_t sc_wait;
84 kmutex_t sc_waitlock;
85 };
86
87 static int balloon_initialized = 0; /* multiple balloon is not allowed */
88
89 static int viomb_match(device_t, cfdata_t, void *);
90 static void viomb_attach(device_t, device_t, void *);
91 static void viomb_read_config(struct viomb_softc *);
92 static int viomb_config_change(struct virtio_softc *);
93 static int inflate(struct viomb_softc *);
94 static int inflateq_done(struct virtqueue *);
95 static int inflate_done(struct viomb_softc *);
96 static int deflate(struct viomb_softc *);
97 static int deflateq_done(struct virtqueue *);
98 static int deflate_done(struct viomb_softc *);
99 static void viomb_thread(void *);
100
101 CFATTACH_DECL_NEW(viomb, sizeof(struct viomb_softc),
102 viomb_match, viomb_attach, NULL, NULL);
103
104 static int
105 viomb_match(device_t parent, cfdata_t match, void *aux)
106 {
107 struct virtio_softc *vsc = aux;
108
109 if (vsc->sc_childdevid == PCI_PRODUCT_VIRTIO_BALLOON)
110 return 1;
111
112 return 0;
113 }
114
115 static void
116 viomb_attach(device_t parent, device_t self, void *aux)
117 {
118 struct viomb_softc *sc = device_private(self);
119 struct virtio_softc *vsc = device_private(parent);
120 const struct sysctlnode *node;
121
122 if (vsc->sc_child != NULL) {
123 aprint_normal(": child already attached for %s; "
124 "something wrong...\n",
125 device_xname(parent));
126 return;
127 }
128 if (balloon_initialized++) {
129 aprint_normal(": balloon already exists; something wrong...\n");
130 goto err_none;
131 }
132 aprint_normal("\n");
133
134 sc->sc_dev = self;
135 sc->sc_virtio = vsc;
136
137 vsc->sc_child = self;
138 vsc->sc_ipl = IPL_VM;
139 vsc->sc_vqs = &sc->sc_vq[0];
140 vsc->sc_nvqs = 2;
141 vsc->sc_config_change = viomb_config_change;
142 vsc->sc_intrhand = virtio_vq_intr;
143 vsc->sc_flags = 0;
144
145 virtio_negotiate_features(vsc,
146 VIRTIO_CONFIG_DEVICE_FEATURES);
147 if ((virtio_alloc_vq(vsc, &sc->sc_vq[0], 0,
148 sizeof(uint32_t)*PGS_PER_REQ, 1,
149 "inflate") != 0) ||
150 (virtio_alloc_vq(vsc, &sc->sc_vq[1], 1,
151 sizeof(uint32_t)*PGS_PER_REQ, 1,
152 "deflate") != 0)) {
153 goto err_none;
154 }
155 sc->sc_vq[0].vq_done = inflateq_done;
156 sc->sc_vq[1].vq_done = deflateq_done;
157
158 viomb_read_config(sc);
159 sc->sc_inflight = 0;
160 TAILQ_INIT(&sc->sc_balloon_pages);
161
162 if (bus_dmamap_create(vsc->sc_dmat, sizeof(uint32_t)*PGS_PER_REQ,
163 1, sizeof(uint32_t)*PGS_PER_REQ, 0,
164 BUS_DMA_NOWAIT, &sc->sc_req.bl_dmamap)) {
165 aprint_error_dev(sc->sc_dev, "dmamap creation failed.\n");
166 goto err_vq;
167 }
168 if (bus_dmamap_load(vsc->sc_dmat, sc->sc_req.bl_dmamap,
169 &sc->sc_req.bl_pages[0],
170 sizeof(uint32_t) * PGS_PER_REQ,
171 NULL, BUS_DMA_NOWAIT)) {
172 aprint_error_dev(sc->sc_dev, "dmamap load failed.\n");
173 goto err_dmamap;
174 }
175
176 sc->sc_inflate_done = sc->sc_deflate_done = 0;
177 mutex_init(&sc->sc_waitlock, MUTEX_DEFAULT, IPL_VM); /* spin */
178 cv_init(&sc->sc_wait, "balloon");
179
180 if (kthread_create(PRI_IDLE, KTHREAD_MPSAFE, NULL,
181 viomb_thread, sc, NULL, "viomb")) {
182 aprint_error_dev(sc->sc_dev, "cannot create kthread.\n");
183 goto err_mutex;
184 }
185
186 sysctl_createv(NULL, 0, NULL, &node, 0, CTLTYPE_NODE,
187 "viomb", SYSCTL_DESCR("VirtIO Balloon status"),
188 NULL, 0, NULL, 0,
189 CTL_HW, CTL_CREATE, CTL_EOL);
190 sysctl_createv(NULL, 0, NULL, NULL, 0, CTLTYPE_INT,
191 "npages", SYSCTL_DESCR("VirtIO Balloon npages value"),
192 NULL, 0, &sc->sc_npages, 0,
193 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
194 sysctl_createv(NULL, 0, NULL, NULL, 0, CTLTYPE_INT,
195 "actual", SYSCTL_DESCR("VirtIO Balloon actual value"),
196 NULL, 0, &sc->sc_actual, 0,
197 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
198 return;
199
200 err_mutex:
201 cv_destroy(&sc->sc_wait);
202 mutex_destroy(&sc->sc_waitlock);
203 err_dmamap:
204 bus_dmamap_destroy(vsc->sc_dmat, sc->sc_req.bl_dmamap);
205 err_vq:
206 virtio_free_vq(vsc, &sc->sc_vq[1]);
207 virtio_free_vq(vsc, &sc->sc_vq[0]);
208 err_none:
209 vsc->sc_child = (void*)1;
210 return;
211 }
212
213 static void
214 viomb_read_config(struct viomb_softc *sc)
215 {
216 unsigned int reg;
217
218 /* these values are explicitly specified as little-endian */
219 reg = virtio_read_device_config_4(sc->sc_virtio,
220 VIRTIO_BALLOON_CONFIG_NUM_PAGES);
221 sc->sc_npages = le32toh(reg);
222
223 reg = virtio_read_device_config_4(sc->sc_virtio,
224 VIRTIO_BALLOON_CONFIG_ACTUAL);
225 sc->sc_actual = le32toh(reg);
226 }
227
228 /*
229 * Config change callback: wakeup the kthread.
230 */
231 static int
232 viomb_config_change(struct virtio_softc *vsc)
233 {
234 struct viomb_softc *sc = device_private(vsc->sc_child);
235 unsigned int old;
236
237 old = sc->sc_npages;
238 viomb_read_config(sc);
239 mutex_enter(&sc->sc_waitlock);
240 cv_signal(&sc->sc_wait);
241 mutex_exit(&sc->sc_waitlock);
242 if (sc->sc_npages > old)
243 printf("%s: inflating balloon from %u to %u.\n",
244 device_xname(sc->sc_dev), old, sc->sc_npages);
245 else if (sc->sc_npages < old)
246 printf("%s: deflating balloon from %u to %u.\n",
247 device_xname(sc->sc_dev), old, sc->sc_npages);
248
249 return 1;
250 }
251
252 /*
253 * Inflate: consume some amount of physical memory.
254 */
255 static int
256 inflate(struct viomb_softc *sc)
257 {
258 struct virtio_softc *vsc = sc->sc_virtio;
259 int i, slot;
260 uint64_t nvpages, nhpages;
261 struct balloon_req *b;
262 struct vm_page *p;
263 struct virtqueue *vq = &sc->sc_vq[0];
264
265 if (sc->sc_inflight)
266 return 0;
267 nvpages = sc->sc_npages - sc->sc_actual;
268 if (nvpages > PGS_PER_REQ)
269 nvpages = PGS_PER_REQ;
270 nhpages = nvpages * VIRTIO_PAGE_SIZE / PAGE_SIZE;
271
272 b = &sc->sc_req;
273 if (uvm_pglistalloc(nhpages*PAGE_SIZE, 0, UINT32_MAX*PAGE_SIZE,
274 0, 0, &b->bl_pglist, nhpages, 1)) {
275 printf("%s: %" PRIu64 " pages of physical memory "
276 "could not be allocated, retrying...\n",
277 device_xname(sc->sc_dev), nhpages);
278 return 1; /* sleep longer */
279 }
280
281 b->bl_nentries = nvpages;
282 i = 0;
283 TAILQ_FOREACH(p, &b->bl_pglist, pageq.queue) {
284 b->bl_pages[i++] = p->phys_addr / VIRTIO_PAGE_SIZE;
285 }
286 KASSERT(i == nvpages);
287
288 if (virtio_enqueue_prep(vsc, vq, &slot) != 0) {
289 printf("%s: inflate enqueue failed.\n",
290 device_xname(sc->sc_dev));
291 uvm_pglistfree(&b->bl_pglist);
292 return 0;
293 }
294 if (virtio_enqueue_reserve(vsc, vq, slot, 1)) {
295 printf("%s: inflate enqueue failed.\n",
296 device_xname(sc->sc_dev));
297 uvm_pglistfree(&b->bl_pglist);
298 return 0;
299 }
300 bus_dmamap_sync(vsc->sc_dmat, b->bl_dmamap, 0, sizeof(uint32_t)*nvpages,
301 BUS_DMASYNC_PREWRITE);
302 virtio_enqueue(vsc, vq, slot, b->bl_dmamap, true);
303 virtio_enqueue_commit(vsc, vq, slot, true);
304 sc->sc_inflight += nvpages;
305
306 return 0;
307 }
308
309 static int
310 inflateq_done(struct virtqueue *vq)
311 {
312 struct virtio_softc *vsc = vq->vq_owner;
313 struct viomb_softc *sc = device_private(vsc->sc_child);
314
315 mutex_enter(&sc->sc_waitlock);
316 sc->sc_inflate_done = 1;
317 cv_signal(&sc->sc_wait);
318 mutex_exit(&sc->sc_waitlock);
319
320 return 1;
321 }
322
323 static int
324 inflate_done(struct viomb_softc *sc)
325 {
326 struct virtio_softc *vsc = sc->sc_virtio;
327 struct virtqueue *vq = &sc->sc_vq[0];
328 struct balloon_req *b;
329 int r, slot;
330 uint64_t nvpages;
331 struct vm_page *p;
332
333 r = virtio_dequeue(vsc, vq, &slot, NULL);
334 if (r != 0) {
335 printf("%s: inflate dequeue failed, errno %d.\n",
336 device_xname(sc->sc_dev), r);
337 return 1;
338 }
339 virtio_dequeue_commit(vsc, vq, slot);
340
341 b = &sc->sc_req;
342 nvpages = b->bl_nentries;
343 bus_dmamap_sync(vsc->sc_dmat, b->bl_dmamap,
344 offsetof(struct balloon_req, bl_pages),
345 sizeof(uint32_t)*nvpages,
346 BUS_DMASYNC_POSTWRITE);
347 while (!TAILQ_EMPTY(&b->bl_pglist)) {
348 p = TAILQ_FIRST(&b->bl_pglist);
349 TAILQ_REMOVE(&b->bl_pglist, p, pageq.queue);
350 TAILQ_INSERT_TAIL(&sc->sc_balloon_pages, p, pageq.queue);
351 }
352
353 sc->sc_inflight -= nvpages;
354 virtio_write_device_config_4(vsc,
355 VIRTIO_BALLOON_CONFIG_ACTUAL,
356 sc->sc_actual + nvpages);
357 viomb_read_config(sc);
358
359 return 1;
360 }
361
362 /*
363 * Deflate: free previously allocated memory.
364 */
365 static int
366 deflate(struct viomb_softc *sc)
367 {
368 struct virtio_softc *vsc = sc->sc_virtio;
369 int i, slot;
370 uint64_t nvpages, nhpages;
371 struct balloon_req *b;
372 struct vm_page *p;
373 struct virtqueue *vq = &sc->sc_vq[1];
374
375 nvpages = (sc->sc_actual + sc->sc_inflight) - sc->sc_npages;
376 if (nvpages > PGS_PER_REQ)
377 nvpages = PGS_PER_REQ;
378 nhpages = nvpages * VIRTIO_PAGE_SIZE / PAGE_SIZE;
379
380 b = &sc->sc_req;
381
382 b->bl_nentries = nvpages;
383 TAILQ_INIT(&b->bl_pglist);
384 for (i = 0; i < nhpages; i++) {
385 p = TAILQ_FIRST(&sc->sc_balloon_pages);
386 if (p == NULL)
387 break;
388 TAILQ_REMOVE(&sc->sc_balloon_pages, p, pageq.queue);
389 TAILQ_INSERT_TAIL(&b->bl_pglist, p, pageq.queue);
390 b->bl_pages[i] = p->phys_addr / VIRTIO_PAGE_SIZE;
391 }
392
393 if (virtio_enqueue_prep(vsc, vq, &slot) != 0) {
394 printf("%s: deflate enqueue failed.\n",
395 device_xname(sc->sc_dev));
396 TAILQ_FOREACH_REVERSE(p, &b->bl_pglist, pglist, pageq.queue) {
397 TAILQ_REMOVE(&b->bl_pglist, p, pageq.queue);
398 TAILQ_INSERT_HEAD(&sc->sc_balloon_pages, p, pageq.queue);
399 }
400 return 0;
401 }
402 if (virtio_enqueue_reserve(vsc, vq, slot, 1) != 0) {
403 printf("%s: deflate enqueue failed.\n",
404 device_xname(sc->sc_dev));
405 TAILQ_FOREACH_REVERSE(p, &b->bl_pglist, pglist, pageq.queue) {
406 TAILQ_REMOVE(&b->bl_pglist, p, pageq.queue);
407 TAILQ_INSERT_HEAD(&sc->sc_balloon_pages, p, pageq.queue);
408 }
409 return 0;
410 }
411 bus_dmamap_sync(vsc->sc_dmat, b->bl_dmamap, 0, sizeof(uint32_t)*nvpages,
412 BUS_DMASYNC_PREWRITE);
413 virtio_enqueue(vsc, vq, slot, b->bl_dmamap, true);
414 virtio_enqueue_commit(vsc, vq, slot, true);
415 sc->sc_inflight -= nvpages;
416
417 if (!(vsc->sc_features & VIRTIO_BALLOON_F_MUST_TELL_HOST))
418 uvm_pglistfree(&b->bl_pglist);
419
420 return 0;
421 }
422
423 static int
424 deflateq_done(struct virtqueue *vq)
425 {
426 struct virtio_softc *vsc = vq->vq_owner;
427 struct viomb_softc *sc = device_private(vsc->sc_child);
428
429 mutex_enter(&sc->sc_waitlock);
430 sc->sc_deflate_done = 1;
431 cv_signal(&sc->sc_wait);
432 mutex_exit(&sc->sc_waitlock);
433
434 return 1;
435 }
436
437 static int
438 deflate_done(struct viomb_softc *sc)
439 {
440 struct virtio_softc *vsc = sc->sc_virtio;
441 struct virtqueue *vq = &sc->sc_vq[1];
442 struct balloon_req *b;
443 int r, slot;
444 uint64_t nvpages;
445
446 r = virtio_dequeue(vsc, vq, &slot, NULL);
447 if (r != 0) {
448 printf("%s: deflate dequeue failed, errno %d\n",
449 device_xname(sc->sc_dev), r);
450 return 1;
451 }
452 virtio_dequeue_commit(vsc, vq, slot);
453
454 b = &sc->sc_req;
455 nvpages = b->bl_nentries;
456 bus_dmamap_sync(vsc->sc_dmat, b->bl_dmamap,
457 offsetof(struct balloon_req, bl_pages),
458 sizeof(uint32_t)*nvpages,
459 BUS_DMASYNC_POSTWRITE);
460
461 if (vsc->sc_features & VIRTIO_BALLOON_F_MUST_TELL_HOST)
462 uvm_pglistfree(&b->bl_pglist);
463
464 sc->sc_inflight += nvpages;
465 virtio_write_device_config_4(vsc,
466 VIRTIO_BALLOON_CONFIG_ACTUAL,
467 sc->sc_actual - nvpages);
468 viomb_read_config(sc);
469
470 return 1;
471 }
472
473 /*
474 * Kthread: sleeps, eventually inflate and deflate.
475 */
476 static void
477 viomb_thread(void *arg)
478 {
479 struct viomb_softc *sc = arg;
480 int sleeptime, r;
481
482 for ( ; ; ) {
483 sleeptime = 30000;
484 if (sc->sc_npages > sc->sc_actual + sc->sc_inflight) {
485 if (sc->sc_inflight == 0) {
486 r = inflate(sc);
487 if (r != 0)
488 sleeptime = 10000;
489 else
490 sleeptime = 1000;
491 } else
492 sleeptime = 100;
493 } else if (sc->sc_npages < sc->sc_actual + sc->sc_inflight) {
494 if (sc->sc_inflight == 0)
495 r = deflate(sc);
496 sleeptime = 100;
497 }
498
499 again:
500 mutex_enter(&sc->sc_waitlock);
501 if (sc->sc_inflate_done) {
502 sc->sc_inflate_done = 0;
503 mutex_exit(&sc->sc_waitlock);
504 inflate_done(sc);
505 goto again;
506 }
507 if (sc->sc_deflate_done) {
508 sc->sc_deflate_done = 0;
509 mutex_exit(&sc->sc_waitlock);
510 deflate_done(sc);
511 goto again;
512 }
513 cv_timedwait(&sc->sc_wait, &sc->sc_waitlock,
514 mstohz(sleeptime));
515 mutex_exit(&sc->sc_waitlock);
516 }
517 }
518