2 * Copyright (c) 2015 Nahanni Systems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
35 #include <machine/vmm.h>
54 * bhyve Framebuffer device emulation.
55 * BAR0 points to the current mode information.
56 * BAR1 is the 32-bit framebuffer address.
58 * -s <b>,fbuf,wait,vga=on|io|off,rfb=<ip>:port,w=width,h=height
61 static int fbuf_debug = 1;
63 #define DEBUG_VERBOSE 4
64 #define DPRINTF(level, params) if (level <= fbuf_debug) printf params
68 #define MB (1024 * 1024UL)
72 #define FB_SIZE (16*MB)
77 #define COLS_DEFAULT 1024
78 #define ROWS_DEFAULT 768
83 struct pci_fbuf_softc {
84 struct pci_devinst *fsc_pi;
91 uint8_t reserved[116];
107 struct bhyvegc_image *gc_image;
110 static struct pci_fbuf_softc *fbuf_sc;
112 #define PCI_FBUF_MSI_MSGS 4
115 pci_fbuf_usage(char *opt)
118 fprintf(stderr, "Invalid fbuf emulation \"%s\"\r\n", opt);
119 fprintf(stderr, "fbuf: {wait,}{vga=on|io|off,}rfb=<ip>:port\r\n");
123 pci_fbuf_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
124 int baridx, uint64_t offset, int size, uint64_t value)
126 struct pci_fbuf_softc *sc;
133 DPRINTF(DEBUG_VERBOSE,
134 ("fbuf wr: offset 0x%lx, size: %d, value: 0x%lx\n",
135 offset, size, value));
137 if (offset + size > DMEMSZ) {
138 printf("fbuf: write too large, offset %ld size %d\n",
143 p = (uint8_t *)&sc->memregs + offset;
150 *(uint16_t *)p = value;
153 *(uint32_t *)p = value;
156 *(uint64_t *)p = value;
159 printf("fbuf: write unknown size %d\n", size);
163 if (!sc->gc_image->vgamode && sc->memregs.width == 0 &&
164 sc->memregs.height == 0) {
165 DPRINTF(DEBUG_INFO, ("switching to VGA mode\r\n"));
166 sc->gc_image->vgamode = 1;
169 } else if (sc->gc_image->vgamode && sc->memregs.width != 0 &&
170 sc->memregs.height != 0) {
171 DPRINTF(DEBUG_INFO, ("switching to VESA mode\r\n"));
172 sc->gc_image->vgamode = 0;
177 pci_fbuf_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
178 int baridx, uint64_t offset, int size)
180 struct pci_fbuf_softc *sc;
189 if (offset + size > DMEMSZ) {
190 printf("fbuf: read too large, offset %ld size %d\n",
195 p = (uint8_t *)&sc->memregs + offset;
202 value = *(uint16_t *)p;
205 value = *(uint32_t *)p;
208 value = *(uint64_t *)p;
211 printf("fbuf: read unknown size %d\n", size);
215 DPRINTF(DEBUG_VERBOSE,
216 ("fbuf rd: offset 0x%lx, size: %d, value: 0x%lx\n",
217 offset, size, value));
223 pci_fbuf_parse_opts(struct pci_fbuf_softc *sc, char *opts)
225 char *uopts, *xopts, *config;
230 uopts = strdup(opts);
231 for (xopts = strtok(uopts, ",");
233 xopts = strtok(NULL, ",")) {
234 if (strcmp(xopts, "wait") == 0) {
239 if ((config = strchr(xopts, '=')) == NULL) {
240 pci_fbuf_usage(xopts);
247 DPRINTF(DEBUG_VERBOSE, ("pci_fbuf option %s = %s\r\n",
250 if (!strcmp(xopts, "tcp") || !strcmp(xopts, "rfb")) {
251 /* parse host-ip:port */
252 tmpstr = strsep(&config, ":");
254 sc->rfb_port = atoi(tmpstr);
256 sc->rfb_port = atoi(config);
257 sc->rfb_host = tmpstr;
259 } else if (!strcmp(xopts, "vga")) {
260 if (!strcmp(config, "off")) {
262 } else if (!strcmp(config, "io")) {
265 } else if (!strcmp(config, "on")) {
269 pci_fbuf_usage(opts);
273 } else if (!strcmp(xopts, "w")) {
274 sc->memregs.width = atoi(config);
275 if (sc->memregs.width > COLS_MAX) {
276 pci_fbuf_usage(xopts);
279 } else if (sc->memregs.width == 0)
280 sc->memregs.width = 1920;
281 } else if (!strcmp(xopts, "h")) {
282 sc->memregs.height = atoi(config);
283 if (sc->memregs.height > ROWS_MAX) {
284 pci_fbuf_usage(xopts);
287 } else if (sc->memregs.height == 0)
288 sc->memregs.height = 1080;
289 } else if (!strcmp(xopts, "password")) {
290 sc->rfb_password = config;
292 pci_fbuf_usage(xopts);
303 extern void vga_render(struct bhyvegc *gc, void *arg);
306 pci_fbuf_render(struct bhyvegc *gc, void *arg)
308 struct pci_fbuf_softc *sc;
312 if (sc->vga_full && sc->gc_image->vgamode) {
313 /* TODO: mode switching to vga and vesa should use the special
314 * EFI-bhyve protocol port.
316 vga_render(gc, sc->vgasc);
319 if (sc->gc_width != sc->memregs.width ||
320 sc->gc_height != sc->memregs.height) {
321 bhyvegc_resize(gc, sc->memregs.width, sc->memregs.height);
322 sc->gc_width = sc->memregs.width;
323 sc->gc_height = sc->memregs.height;
330 pci_fbuf_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
333 struct pci_fbuf_softc *sc;
335 if (fbuf_sc != NULL) {
336 fprintf(stderr, "Only one frame buffer device is allowed.\n");
340 sc = calloc(1, sizeof(struct pci_fbuf_softc));
344 /* initialize config space */
345 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x40FB);
346 pci_set_cfgdata16(pi, PCIR_VENDOR, 0xFB5D);
347 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_DISPLAY);
348 pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_DISPLAY_VGA);
350 error = pci_emul_alloc_bar(pi, 0, PCIBAR_MEM32, DMEMSZ);
353 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, FB_SIZE);
356 error = pci_emul_add_msicap(pi, PCI_FBUF_MSI_MSGS);
359 sc->fbaddr = pi->pi_bar[1].addr;
360 sc->memregs.fbsize = FB_SIZE;
361 sc->memregs.width = COLS_DEFAULT;
362 sc->memregs.height = ROWS_DEFAULT;
363 sc->memregs.depth = 32;
370 error = pci_fbuf_parse_opts(sc, opts);
374 /* XXX until VGA rendering is enabled */
375 if (sc->vga_full != 0) {
376 fprintf(stderr, "pci_fbuf: VGA rendering not enabled");
380 sc->fb_base = vm_create_devmem(ctx, VM_FRAMEBUFFER, "framebuffer", FB_SIZE);
381 if (sc->fb_base == MAP_FAILED) {
385 DPRINTF(DEBUG_INFO, ("fbuf frame buffer base: %p [sz %lu]\r\n",
386 sc->fb_base, FB_SIZE));
389 * Map the framebuffer into the guest address space.
390 * XXX This may fail if the BAR is different than a prior
391 * run. In this case flag the error. This will be fixed
392 * when a change_memseg api is available.
394 prot = PROT_READ | PROT_WRITE;
395 if (vm_mmap_memseg(ctx, sc->fbaddr, VM_FRAMEBUFFER, 0, FB_SIZE, prot) != 0) {
396 fprintf(stderr, "pci_fbuf: mapseg failed - try deleting VM and restarting\n");
401 console_init(sc->memregs.width, sc->memregs.height, sc->fb_base);
402 console_fb_register(pci_fbuf_render, sc);
405 sc->vgasc = vga_init(!sc->vga_full);
406 sc->gc_image = console_get_image();
410 memset((void *)sc->fb_base, 0, FB_SIZE);
412 error = rfb_init(sc->rfb_host, sc->rfb_port, sc->rfb_wait, sc->rfb_password);
420 struct pci_devemu pci_fbuf = {
422 .pe_init = pci_fbuf_init,
423 .pe_barwrite = pci_fbuf_write,
424 .pe_barread = pci_fbuf_read
426 PCI_EMUL_SET(pci_fbuf);