1 /* $OpenBSD: viomb.c,v 1.9 2023/05/29 08:13:35 sf Exp $ */ 2 /* $NetBSD: viomb.c,v 1.1 2011/10/30 12:12:21 hannken Exp $ */ 3 4 /* 5 * Copyright (c) 2012 Talypov Dinar <dinar@i-nk.ru> 6 * Copyright (c) 2010 Minoura Makoto. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/malloc.h> 33 #include <sys/device.h> 34 #include <sys/task.h> 35 #include <sys/pool.h> 36 #include <sys/sensors.h> 37 38 #include <uvm/uvm_extern.h> 39 40 #include <dev/pv/virtioreg.h> 41 #include <dev/pv/virtiovar.h> 42 43 #if VIRTIO_PAGE_SIZE!=PAGE_SIZE 44 #error non-4K page sizes are not supported yet 45 #endif 46 47 #define DEVNAME(sc) sc->sc_dev.dv_xname 48 #if VIRTIO_DEBUG 49 #define VIOMBDEBUG(sc, format, args...) \ 50 do { printf("%s: " format, sc->sc_dev.dv_xname, ##args);} \ 51 while (0) 52 #else 53 #define VIOMBDEBUG(...) 54 #endif 55 56 /* flags used to specify kind of operation, 57 * actually should be moved to virtiovar.h 58 */ 59 #define VRING_READ 0 60 #define VRING_WRITE 1 61 62 /* notify or don't notify */ 63 #define VRING_NO_NOTIFY 0 64 #define VRING_NOTIFY 1 65 66 /* Configuration registers */ 67 #define VIRTIO_BALLOON_CONFIG_NUM_PAGES 0 /* 32bit */ 68 #define VIRTIO_BALLOON_CONFIG_ACTUAL 4 /* 32bit */ 69 70 /* Feature bits */ 71 #define VIRTIO_BALLOON_F_MUST_TELL_HOST (1ULL<<0) 72 #define VIRTIO_BALLOON_F_STATS_VQ (1ULL<<1) 73 74 static const struct virtio_feature_name viomb_feature_names[] = { 75 #if VIRTIO_DEBUG 76 {VIRTIO_BALLOON_F_MUST_TELL_HOST, "TellHost"}, 77 {VIRTIO_BALLOON_F_STATS_VQ, "StatVQ"}, 78 #endif 79 {0, NULL} 80 }; 81 #define PGS_PER_REQ 256 /* 1MB, 4KB/page */ 82 #define VQ_INFLATE 0 83 #define VQ_DEFLATE 1 84 85 struct balloon_req { 86 bus_dmamap_t bl_dmamap; 87 struct pglist bl_pglist; 88 int bl_nentries; 89 u_int32_t *bl_pages; 90 }; 91 92 struct viomb_softc { 93 struct device sc_dev; 94 struct virtio_softc *sc_virtio; 95 struct virtqueue sc_vq[2]; 96 u_int32_t sc_npages; /* desired pages */ 97 u_int32_t sc_actual; /* current pages */ 98 struct balloon_req sc_req; 99 struct taskq *sc_taskq; 100 struct task sc_task; 101 struct pglist sc_balloon_pages; 102 struct ksensor sc_sens[2]; 103 struct ksensordev sc_sensdev; 104 }; 105 106 int viomb_match(struct device *, void *, void *); 107 void viomb_attach(struct device *, struct device *, void *); 108 void viomb_worker(void *); 109 void viomb_inflate(struct viomb_softc *); 110 void viomb_deflate(struct viomb_softc *); 111 int viomb_config_change(struct virtio_softc *); 112 void viomb_read_config(struct viomb_softc *); 113 int viomb_vq_dequeue(struct virtqueue *); 114 int viomb_inflate_intr(struct virtqueue *); 115 int viomb_deflate_intr(struct virtqueue *); 116 117 const struct cfattach viomb_ca = { 118 sizeof(struct viomb_softc), viomb_match, viomb_attach 119 }; 120 121 struct cfdriver viomb_cd = { 122 NULL, "viomb", DV_DULL 123 }; 124 125 int 126 viomb_match(struct device *parent, void *match, void *aux) 127 { 128 struct virtio_softc *va = aux; 129 if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_BALLOON) 130 return (1); 131 return (0); 132 } 133 134 void 135 viomb_attach(struct device *parent, struct device *self, void *aux) 136 { 137 struct viomb_softc *sc = (struct viomb_softc *)self; 138 struct virtio_softc *vsc = (struct virtio_softc *)parent; 139 int i; 140 141 if (vsc->sc_child != NULL) { 142 printf("child already attached for %s; something wrong...\n", 143 parent->dv_xname); 144 return; 145 } 146 147 /* fail on non-4K page size archs */ 148 if (VIRTIO_PAGE_SIZE != PAGE_SIZE){ 149 printf("non-4K page size arch found, needs %d, got %d\n", 150 VIRTIO_PAGE_SIZE, PAGE_SIZE); 151 return; 152 } 153 154 sc->sc_virtio = vsc; 155 vsc->sc_vqs = &sc->sc_vq[VQ_INFLATE]; 156 vsc->sc_nvqs = 0; 157 vsc->sc_child = self; 158 vsc->sc_ipl = IPL_BIO; 159 vsc->sc_config_change = viomb_config_change; 160 161 vsc->sc_driver_features = VIRTIO_BALLOON_F_MUST_TELL_HOST; 162 if (virtio_negotiate_features(vsc, viomb_feature_names) != 0) 163 goto err; 164 165 if ((virtio_alloc_vq(vsc, &sc->sc_vq[VQ_INFLATE], VQ_INFLATE, 166 sizeof(u_int32_t) * PGS_PER_REQ, 1, "inflate") != 0)) 167 goto err; 168 vsc->sc_nvqs++; 169 if ((virtio_alloc_vq(vsc, &sc->sc_vq[VQ_DEFLATE], VQ_DEFLATE, 170 sizeof(u_int32_t) * PGS_PER_REQ, 1, "deflate") != 0)) 171 goto err; 172 vsc->sc_nvqs++; 173 174 sc->sc_vq[VQ_INFLATE].vq_done = viomb_inflate_intr; 175 sc->sc_vq[VQ_DEFLATE].vq_done = viomb_deflate_intr; 176 virtio_start_vq_intr(vsc, &sc->sc_vq[VQ_INFLATE]); 177 virtio_start_vq_intr(vsc, &sc->sc_vq[VQ_DEFLATE]); 178 179 viomb_read_config(sc); 180 TAILQ_INIT(&sc->sc_balloon_pages); 181 182 if ((sc->sc_req.bl_pages = dma_alloc(sizeof(u_int32_t) * PGS_PER_REQ, 183 PR_NOWAIT|PR_ZERO)) == NULL) { 184 printf("%s: Can't alloc DMA memory.\n", DEVNAME(sc)); 185 goto err; 186 } 187 if (bus_dmamap_create(vsc->sc_dmat, sizeof(u_int32_t) * PGS_PER_REQ, 188 1, sizeof(u_int32_t) * PGS_PER_REQ, 0, 189 BUS_DMA_NOWAIT, &sc->sc_req.bl_dmamap)) { 190 printf("%s: dmamap creation failed.\n", DEVNAME(sc)); 191 goto err; 192 } 193 if (bus_dmamap_load(vsc->sc_dmat, sc->sc_req.bl_dmamap, 194 &sc->sc_req.bl_pages[0], 195 sizeof(uint32_t) * PGS_PER_REQ, 196 NULL, BUS_DMA_NOWAIT)) { 197 printf("%s: dmamap load failed.\n", DEVNAME(sc)); 198 goto err_dmamap; 199 } 200 201 sc->sc_taskq = taskq_create("viomb", 1, IPL_BIO, 0); 202 if (sc->sc_taskq == NULL) 203 goto err_dmamap; 204 task_set(&sc->sc_task, viomb_worker, sc); 205 206 strlcpy(sc->sc_sensdev.xname, DEVNAME(sc), 207 sizeof(sc->sc_sensdev.xname)); 208 strlcpy(sc->sc_sens[0].desc, "desired", 209 sizeof(sc->sc_sens[0].desc)); 210 sc->sc_sens[0].type = SENSOR_INTEGER; 211 sensor_attach(&sc->sc_sensdev, &sc->sc_sens[0]); 212 sc->sc_sens[0].value = sc->sc_npages << PAGE_SHIFT; 213 214 strlcpy(sc->sc_sens[1].desc, "current", 215 sizeof(sc->sc_sens[1].desc)); 216 sc->sc_sens[1].type = SENSOR_INTEGER; 217 sensor_attach(&sc->sc_sensdev, &sc->sc_sens[1]); 218 sc->sc_sens[1].value = sc->sc_actual << PAGE_SHIFT; 219 220 sensordev_install(&sc->sc_sensdev); 221 222 printf("\n"); 223 virtio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK); 224 return; 225 err_dmamap: 226 bus_dmamap_destroy(vsc->sc_dmat, sc->sc_req.bl_dmamap); 227 err: 228 if (sc->sc_req.bl_pages) 229 dma_free(sc->sc_req.bl_pages, sizeof(u_int32_t) * PGS_PER_REQ); 230 for (i = 0; i < vsc->sc_nvqs; i++) 231 virtio_free_vq(vsc, &sc->sc_vq[i]); 232 vsc->sc_nvqs = 0; 233 vsc->sc_child = VIRTIO_CHILD_ERROR; 234 return; 235 } 236 237 /* 238 * Config change 239 */ 240 int 241 viomb_config_change(struct virtio_softc *vsc) 242 { 243 struct viomb_softc *sc = (struct viomb_softc *)vsc->sc_child; 244 245 task_add(sc->sc_taskq, &sc->sc_task); 246 247 return (1); 248 } 249 250 void 251 viomb_worker(void *arg1) 252 { 253 struct viomb_softc *sc = (struct viomb_softc *)arg1; 254 int s; 255 256 s = splbio(); 257 viomb_read_config(sc); 258 if (sc->sc_npages > sc->sc_actual){ 259 VIOMBDEBUG(sc, "inflating balloon from %u to %u.\n", 260 sc->sc_actual, sc->sc_npages); 261 viomb_inflate(sc); 262 } 263 else if (sc->sc_npages < sc->sc_actual){ 264 VIOMBDEBUG(sc, "deflating balloon from %u to %u.\n", 265 sc->sc_actual, sc->sc_npages); 266 viomb_deflate(sc); 267 } 268 269 sc->sc_sens[0].value = sc->sc_npages << PAGE_SHIFT; 270 sc->sc_sens[1].value = sc->sc_actual << PAGE_SHIFT; 271 272 splx(s); 273 } 274 275 void 276 viomb_inflate(struct viomb_softc *sc) 277 { 278 struct virtio_softc *vsc = (struct virtio_softc *)sc->sc_virtio; 279 struct balloon_req *b; 280 struct vm_page *p; 281 struct virtqueue *vq = &sc->sc_vq[VQ_INFLATE]; 282 u_int32_t nvpages; 283 int slot, error, i = 0; 284 285 nvpages = sc->sc_npages - sc->sc_actual; 286 if (nvpages > PGS_PER_REQ) 287 nvpages = PGS_PER_REQ; 288 b = &sc->sc_req; 289 290 if ((error = uvm_pglistalloc(nvpages * PAGE_SIZE, 0, 291 dma_constraint.ucr_high, 292 0, 0, &b->bl_pglist, nvpages, 293 UVM_PLA_NOWAIT))) { 294 printf("%s unable to allocate %u physmem pages," 295 "error %d\n", DEVNAME(sc), nvpages, error); 296 return; 297 } 298 299 b->bl_nentries = nvpages; 300 TAILQ_FOREACH(p, &b->bl_pglist, pageq) 301 b->bl_pages[i++] = p->phys_addr / VIRTIO_PAGE_SIZE; 302 303 KASSERT(i == nvpages); 304 305 if ((virtio_enqueue_prep(vq, &slot)) > 0) { 306 printf("%s:virtio_enqueue_prep() vq_num %d\n", 307 DEVNAME(sc), vq->vq_num); 308 goto err; 309 } 310 if (virtio_enqueue_reserve(vq, slot, 1)) { 311 printf("%s:virtio_enqueue_reserve vq_num %d\n", 312 DEVNAME(sc), vq->vq_num); 313 goto err; 314 } 315 bus_dmamap_sync(vsc->sc_dmat, b->bl_dmamap, 0, 316 sizeof(u_int32_t) * nvpages, BUS_DMASYNC_PREWRITE); 317 virtio_enqueue_p(vq, slot, b->bl_dmamap, 0, 318 sizeof(u_int32_t) * nvpages, VRING_READ); 319 virtio_enqueue_commit(vsc, vq, slot, VRING_NOTIFY); 320 return; 321 err: 322 uvm_pglistfree(&b->bl_pglist); 323 return; 324 } 325 326 void 327 viomb_deflate(struct viomb_softc *sc) 328 { 329 struct virtio_softc *vsc = (struct virtio_softc *)sc->sc_virtio; 330 struct balloon_req *b; 331 struct vm_page *p; 332 struct virtqueue *vq = &sc->sc_vq[VQ_DEFLATE]; 333 u_int64_t nvpages; 334 int i, slot; 335 336 nvpages = sc->sc_actual - sc->sc_npages; 337 if (nvpages > PGS_PER_REQ) 338 nvpages = PGS_PER_REQ; 339 b = &sc->sc_req; 340 b->bl_nentries = nvpages; 341 342 TAILQ_INIT(&b->bl_pglist); 343 for (i = 0; i < nvpages; i++) { 344 p = TAILQ_FIRST(&sc->sc_balloon_pages); 345 if (p == NULL){ 346 b->bl_nentries = i - 1; 347 break; 348 } 349 TAILQ_REMOVE(&sc->sc_balloon_pages, p, pageq); 350 TAILQ_INSERT_TAIL(&b->bl_pglist, p, pageq); 351 b->bl_pages[i] = p->phys_addr / VIRTIO_PAGE_SIZE; 352 } 353 354 if (virtio_enqueue_prep(vq, &slot)) { 355 printf("%s:virtio_get_slot(def) vq_num %d\n", 356 DEVNAME(sc), vq->vq_num); 357 goto err; 358 } 359 if (virtio_enqueue_reserve(vq, slot, 1)) { 360 printf("%s:virtio_enqueue_reserve() vq_num %d\n", 361 DEVNAME(sc), vq->vq_num); 362 goto err; 363 } 364 bus_dmamap_sync(vsc->sc_dmat, b->bl_dmamap, 0, 365 sizeof(u_int32_t) * nvpages, 366 BUS_DMASYNC_PREWRITE); 367 virtio_enqueue_p(vq, slot, b->bl_dmamap, 0, 368 sizeof(u_int32_t) * nvpages, VRING_READ); 369 370 if (!virtio_has_feature(vsc, VIRTIO_BALLOON_F_MUST_TELL_HOST)) 371 uvm_pglistfree(&b->bl_pglist); 372 virtio_enqueue_commit(vsc, vq, slot, VRING_NOTIFY); 373 return; 374 err: 375 TAILQ_CONCAT(&sc->sc_balloon_pages, &b->bl_pglist, pageq); 376 return; 377 } 378 379 void 380 viomb_read_config(struct viomb_softc *sc) 381 { 382 struct virtio_softc *vsc = (struct virtio_softc *)sc->sc_virtio; 383 u_int32_t reg; 384 385 /* these values are explicitly specified as little-endian */ 386 reg = virtio_read_device_config_4(vsc, VIRTIO_BALLOON_CONFIG_NUM_PAGES); 387 sc->sc_npages = letoh32(reg); 388 reg = virtio_read_device_config_4(vsc, VIRTIO_BALLOON_CONFIG_ACTUAL); 389 sc->sc_actual = letoh32(reg); 390 VIOMBDEBUG(sc, "sc->sc_npages %u, sc->sc_actual %u\n", 391 sc->sc_npages, sc->sc_actual); 392 } 393 394 int 395 viomb_vq_dequeue(struct virtqueue *vq) 396 { 397 struct virtio_softc *vsc = vq->vq_owner; 398 struct viomb_softc *sc = (struct viomb_softc *)vsc->sc_child; 399 int r, slot; 400 401 r = virtio_dequeue(vsc, vq, &slot, NULL); 402 if (r != 0) { 403 printf("%s: dequeue failed, errno %d\n", DEVNAME(sc), r); 404 return(r); 405 } 406 virtio_dequeue_commit(vq, slot); 407 return(0); 408 } 409 410 /* 411 * interrupt handling for vq's 412 */ 413 int 414 viomb_inflate_intr(struct virtqueue *vq) 415 { 416 struct virtio_softc *vsc = vq->vq_owner; 417 struct viomb_softc *sc = (struct viomb_softc *)vsc->sc_child; 418 struct balloon_req *b; 419 u_int64_t nvpages; 420 421 if (viomb_vq_dequeue(vq)) 422 return(1); 423 424 b = &sc->sc_req; 425 nvpages = b->bl_nentries; 426 bus_dmamap_sync(vsc->sc_dmat, b->bl_dmamap, 0, 427 sizeof(u_int32_t) * nvpages, 428 BUS_DMASYNC_POSTWRITE); 429 TAILQ_CONCAT(&sc->sc_balloon_pages, &b->bl_pglist, pageq); 430 VIOMBDEBUG(sc, "updating sc->sc_actual from %u to %llu\n", 431 sc->sc_actual, sc->sc_actual + nvpages); 432 virtio_write_device_config_4(vsc, VIRTIO_BALLOON_CONFIG_ACTUAL, 433 sc->sc_actual + nvpages); 434 viomb_read_config(sc); 435 436 /* if we have more work to do, add it to the task list */ 437 if (sc->sc_npages > sc->sc_actual) 438 task_add(sc->sc_taskq, &sc->sc_task); 439 440 return (1); 441 } 442 443 int 444 viomb_deflate_intr(struct virtqueue *vq) 445 { 446 struct virtio_softc *vsc = vq->vq_owner; 447 struct viomb_softc *sc = (struct viomb_softc *)vsc->sc_child; 448 struct balloon_req *b; 449 u_int64_t nvpages; 450 451 if (viomb_vq_dequeue(vq)) 452 return(1); 453 454 b = &sc->sc_req; 455 nvpages = b->bl_nentries; 456 bus_dmamap_sync(vsc->sc_dmat, b->bl_dmamap, 0, 457 sizeof(u_int32_t) * nvpages, 458 BUS_DMASYNC_POSTWRITE); 459 460 if (virtio_has_feature(vsc, VIRTIO_BALLOON_F_MUST_TELL_HOST)) 461 uvm_pglistfree(&b->bl_pglist); 462 463 VIOMBDEBUG(sc, "updating sc->sc_actual from %u to %llu\n", 464 sc->sc_actual, sc->sc_actual - nvpages); 465 virtio_write_device_config_4(vsc, VIRTIO_BALLOON_CONFIG_ACTUAL, 466 sc->sc_actual - nvpages); 467 viomb_read_config(sc); 468 469 /* if we have more work to do, add it to tasks list */ 470 if (sc->sc_npages < sc->sc_actual) 471 task_add(sc->sc_taskq, &sc->sc_task); 472 473 return(1); 474 } 475