1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2015 Nahanni Systems, Inc. 5 * Copyright 2018 Joyent, Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/types.h> 36 #include <sys/mman.h> 37 38 #include <machine/vmm.h> 39 #include <vmmapi.h> 40 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include <errno.h> 46 #include <unistd.h> 47 48 #include "bhyvegc.h" 49 #include "bhyverun.h" 50 #include "console.h" 51 #include "inout.h" 52 #include "pci_emul.h" 53 #include "rfb.h" 54 #include "vga.h" 55 56 /* 57 * bhyve Framebuffer device emulation. 58 * BAR0 points to the current mode information. 59 * BAR1 is the 32-bit framebuffer address. 60 * 61 * -s <b>,fbuf,wait,vga=on|io|off,rfb=<ip>:port,w=width,h=height 62 */ 63 64 static int fbuf_debug = 1; 65 #define DEBUG_INFO 1 66 #define DEBUG_VERBOSE 4 67 #define DPRINTF(level, params) if (level <= fbuf_debug) printf params 68 69 70 #define KB (1024UL) 71 #define MB (1024 * 1024UL) 72 73 #define DMEMSZ 128 74 75 #define FB_SIZE (16*MB) 76 77 #define COLS_MAX 1920 78 #define ROWS_MAX 1200 79 80 #define COLS_DEFAULT 1024 81 #define ROWS_DEFAULT 768 82 83 #define COLS_MIN 640 84 #define ROWS_MIN 480 85 86 struct pci_fbuf_softc { 87 struct pci_devinst *fsc_pi; 88 struct { 89 uint32_t fbsize; 90 uint16_t width; 91 uint16_t height; 92 uint16_t depth; 93 uint16_t refreshrate; 94 uint8_t reserved[116]; 95 } __packed memregs; 96 97 /* rfb server */ 98 char *rfb_host; 99 char *rfb_password; 100 int rfb_port; 101 #ifndef __FreeBSD__ 102 char *rfb_unix; 103 #endif 104 int rfb_wait; 105 int vga_enabled; 106 int vga_full; 107 108 uint32_t fbaddr; 109 char *fb_base; 110 uint16_t gc_width; 111 uint16_t gc_height; 112 void *vgasc; 113 struct bhyvegc_image *gc_image; 114 }; 115 116 static struct pci_fbuf_softc *fbuf_sc; 117 118 #define PCI_FBUF_MSI_MSGS 4 119 120 static void 121 pci_fbuf_usage(char *opt) 122 { 123 124 fprintf(stderr, "Invalid fbuf emulation option \"%s\"\r\n", opt); 125 fprintf(stderr, "fbuf: {wait,}{vga=on|io|off,}rfb=<ip>:port" 126 "{,w=width}{,h=height}\r\n"); 127 } 128 129 static void 130 pci_fbuf_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, 131 int baridx, uint64_t offset, int size, uint64_t value) 132 { 133 struct pci_fbuf_softc *sc; 134 uint8_t *p; 135 136 assert(baridx == 0); 137 138 sc = pi->pi_arg; 139 140 DPRINTF(DEBUG_VERBOSE, 141 ("fbuf wr: offset 0x%lx, size: %d, value: 0x%lx\n", 142 offset, size, value)); 143 144 if (offset + size > DMEMSZ) { 145 printf("fbuf: write too large, offset %ld size %d\n", 146 offset, size); 147 return; 148 } 149 150 p = (uint8_t *)&sc->memregs + offset; 151 152 switch (size) { 153 case 1: 154 *p = value; 155 break; 156 case 2: 157 *(uint16_t *)p = value; 158 break; 159 case 4: 160 *(uint32_t *)p = value; 161 break; 162 case 8: 163 *(uint64_t *)p = value; 164 break; 165 default: 166 printf("fbuf: write unknown size %d\n", size); 167 break; 168 } 169 170 if (!sc->gc_image->vgamode && sc->memregs.width == 0 && 171 sc->memregs.height == 0) { 172 DPRINTF(DEBUG_INFO, ("switching to VGA mode\r\n")); 173 sc->gc_image->vgamode = 1; 174 sc->gc_width = 0; 175 sc->gc_height = 0; 176 } else if (sc->gc_image->vgamode && sc->memregs.width != 0 && 177 sc->memregs.height != 0) { 178 DPRINTF(DEBUG_INFO, ("switching to VESA mode\r\n")); 179 sc->gc_image->vgamode = 0; 180 } 181 } 182 183 uint64_t 184 pci_fbuf_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, 185 int baridx, uint64_t offset, int size) 186 { 187 struct pci_fbuf_softc *sc; 188 uint8_t *p; 189 uint64_t value; 190 191 assert(baridx == 0); 192 193 sc = pi->pi_arg; 194 195 196 if (offset + size > DMEMSZ) { 197 printf("fbuf: read too large, offset %ld size %d\n", 198 offset, size); 199 return (0); 200 } 201 202 p = (uint8_t *)&sc->memregs + offset; 203 value = 0; 204 switch (size) { 205 case 1: 206 value = *p; 207 break; 208 case 2: 209 value = *(uint16_t *)p; 210 break; 211 case 4: 212 value = *(uint32_t *)p; 213 break; 214 case 8: 215 value = *(uint64_t *)p; 216 break; 217 default: 218 printf("fbuf: read unknown size %d\n", size); 219 break; 220 } 221 222 DPRINTF(DEBUG_VERBOSE, 223 ("fbuf rd: offset 0x%lx, size: %d, value: 0x%lx\n", 224 offset, size, value)); 225 226 return (value); 227 } 228 229 static int 230 pci_fbuf_parse_opts(struct pci_fbuf_softc *sc, char *opts) 231 { 232 char *uopts, *uoptsbak, *xopts, *config; 233 char *tmpstr; 234 int ret; 235 236 ret = 0; 237 uoptsbak = uopts = strdup(opts); 238 while ((xopts = strsep(&uopts, ",")) != NULL) { 239 if (strcmp(xopts, "wait") == 0) { 240 sc->rfb_wait = 1; 241 continue; 242 } 243 244 if ((config = strchr(xopts, '=')) == NULL) { 245 pci_fbuf_usage(xopts); 246 ret = -1; 247 goto done; 248 } 249 250 *config++ = '\0'; 251 252 DPRINTF(DEBUG_VERBOSE, ("pci_fbuf option %s = %s\r\n", 253 xopts, config)); 254 255 if (!strcmp(xopts, "tcp") || !strcmp(xopts, "rfb")) { 256 /* 257 * IPv4 -- host-ip:port 258 * IPv6 -- [host-ip%zone]:port 259 * XXX for now port is mandatory. 260 */ 261 tmpstr = strsep(&config, "]"); 262 if (config) { 263 if (tmpstr[0] == '[') 264 tmpstr++; 265 sc->rfb_host = strdup(tmpstr); 266 if (config[0] == ':') 267 config++; 268 else { 269 pci_fbuf_usage(xopts); 270 ret = -1; 271 goto done; 272 } 273 sc->rfb_port = atoi(config); 274 } else { 275 config = tmpstr; 276 tmpstr = strsep(&config, ":"); 277 if (!config) 278 sc->rfb_port = atoi(tmpstr); 279 else { 280 sc->rfb_port = atoi(config); 281 sc->rfb_host = strdup(tmpstr); 282 } 283 } 284 #ifndef __FreeBSD__ 285 } else if (!strcmp(xopts, "unix")) { 286 sc->rfb_unix = strdup(config); 287 #endif 288 } else if (!strcmp(xopts, "vga")) { 289 if (!strcmp(config, "off")) { 290 sc->vga_enabled = 0; 291 } else if (!strcmp(config, "io")) { 292 sc->vga_enabled = 1; 293 sc->vga_full = 0; 294 } else if (!strcmp(config, "on")) { 295 sc->vga_enabled = 1; 296 sc->vga_full = 1; 297 } else { 298 pci_fbuf_usage(xopts); 299 ret = -1; 300 goto done; 301 } 302 } else if (!strcmp(xopts, "w")) { 303 sc->memregs.width = atoi(config); 304 if (sc->memregs.width > COLS_MAX) { 305 pci_fbuf_usage(xopts); 306 ret = -1; 307 goto done; 308 } else if (sc->memregs.width == 0) 309 sc->memregs.width = 1920; 310 } else if (!strcmp(xopts, "h")) { 311 sc->memregs.height = atoi(config); 312 if (sc->memregs.height > ROWS_MAX) { 313 pci_fbuf_usage(xopts); 314 ret = -1; 315 goto done; 316 } else if (sc->memregs.height == 0) 317 sc->memregs.height = 1080; 318 } else if (!strcmp(xopts, "password")) { 319 sc->rfb_password = strdup(config); 320 } else { 321 pci_fbuf_usage(xopts); 322 ret = -1; 323 goto done; 324 } 325 } 326 327 done: 328 free(uoptsbak); 329 return (ret); 330 } 331 332 333 extern void vga_render(struct bhyvegc *gc, void *arg); 334 335 void 336 pci_fbuf_render(struct bhyvegc *gc, void *arg) 337 { 338 struct pci_fbuf_softc *sc; 339 340 sc = arg; 341 342 if (sc->vga_full && sc->gc_image->vgamode) { 343 /* TODO: mode switching to vga and vesa should use the special 344 * EFI-bhyve protocol port. 345 */ 346 vga_render(gc, sc->vgasc); 347 return; 348 } 349 if (sc->gc_width != sc->memregs.width || 350 sc->gc_height != sc->memregs.height) { 351 bhyvegc_resize(gc, sc->memregs.width, sc->memregs.height); 352 sc->gc_width = sc->memregs.width; 353 sc->gc_height = sc->memregs.height; 354 } 355 356 return; 357 } 358 359 static int 360 pci_fbuf_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) 361 { 362 int error, prot; 363 struct pci_fbuf_softc *sc; 364 365 if (fbuf_sc != NULL) { 366 fprintf(stderr, "Only one frame buffer device is allowed.\n"); 367 return (-1); 368 } 369 370 sc = calloc(1, sizeof(struct pci_fbuf_softc)); 371 372 pi->pi_arg = sc; 373 374 /* initialize config space */ 375 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x40FB); 376 pci_set_cfgdata16(pi, PCIR_VENDOR, 0xFB5D); 377 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_DISPLAY); 378 pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_DISPLAY_VGA); 379 380 error = pci_emul_alloc_bar(pi, 0, PCIBAR_MEM32, DMEMSZ); 381 assert(error == 0); 382 383 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, FB_SIZE); 384 assert(error == 0); 385 386 error = pci_emul_add_msicap(pi, PCI_FBUF_MSI_MSGS); 387 assert(error == 0); 388 389 sc->fbaddr = pi->pi_bar[1].addr; 390 sc->memregs.fbsize = FB_SIZE; 391 sc->memregs.width = COLS_DEFAULT; 392 sc->memregs.height = ROWS_DEFAULT; 393 sc->memregs.depth = 32; 394 395 sc->vga_enabled = 1; 396 sc->vga_full = 0; 397 398 sc->fsc_pi = pi; 399 400 error = pci_fbuf_parse_opts(sc, opts); 401 if (error != 0) 402 goto done; 403 404 /* XXX until VGA rendering is enabled */ 405 if (sc->vga_full != 0) { 406 fprintf(stderr, "pci_fbuf: VGA rendering not enabled"); 407 goto done; 408 } 409 410 sc->fb_base = vm_create_devmem(ctx, VM_FRAMEBUFFER, "framebuffer", FB_SIZE); 411 if (sc->fb_base == MAP_FAILED) { 412 error = -1; 413 goto done; 414 } 415 DPRINTF(DEBUG_INFO, ("fbuf frame buffer base: %p [sz %lu]\r\n", 416 sc->fb_base, FB_SIZE)); 417 418 /* 419 * Map the framebuffer into the guest address space. 420 * XXX This may fail if the BAR is different than a prior 421 * run. In this case flag the error. This will be fixed 422 * when a change_memseg api is available. 423 */ 424 prot = PROT_READ | PROT_WRITE; 425 if (vm_mmap_memseg(ctx, sc->fbaddr, VM_FRAMEBUFFER, 0, FB_SIZE, prot) != 0) { 426 fprintf(stderr, "pci_fbuf: mapseg failed - try deleting VM and restarting\n"); 427 error = -1; 428 goto done; 429 } 430 431 console_init(sc->memregs.width, sc->memregs.height, sc->fb_base); 432 console_fb_register(pci_fbuf_render, sc); 433 434 if (sc->vga_enabled) 435 sc->vgasc = vga_init(!sc->vga_full); 436 sc->gc_image = console_get_image(); 437 438 fbuf_sc = sc; 439 440 memset((void *)sc->fb_base, 0, FB_SIZE); 441 442 #ifdef __FreeBSD__ 443 error = rfb_init(sc->rfb_host, sc->rfb_port, sc->rfb_wait, sc->rfb_password); 444 #else 445 if (sc->rfb_unix != NULL) { 446 error = rfb_init_unix(sc->rfb_unix, sc->rfb_wait, 447 sc->rfb_password); 448 } else { 449 error = rfb_init(sc->rfb_host, sc->rfb_port, sc->rfb_wait, 450 sc->rfb_password); 451 } 452 #endif 453 done: 454 if (error) 455 free(sc); 456 457 return (error); 458 } 459 460 struct pci_devemu pci_fbuf = { 461 .pe_emu = "fbuf", 462 .pe_init = pci_fbuf_init, 463 .pe_barwrite = pci_fbuf_write, 464 .pe_barread = pci_fbuf_read 465 }; 466 PCI_EMUL_SET(pci_fbuf); 467