Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/dev/virtio_gpu.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / dev / virtio_gpu.c` 167 lines ยท 6497 bytes Raw Blame History 1 /* virtio_gpu.c -- virtio-gpu 2D screen (virtio_gpu.h): pure command 2 * builders, and (kernel build) the PCI binding behind ck_vgpu_open. */ 3 #include "virtio_gpu.h" 4 #include 5 6 static void put32(uint8_t *p, uint32_t v) { memcpy(p, &v, 4); } 7 static void put64(uint8_t *p, uint64_t v) { memcpy(p, &v, 8); } 8 static uint32_t get32(const uint8_t *p) 9 { 10 uint32_t v; 11 memcpy(&v, p, 4); 12 return v; 13 } 14 15 /* struct virtio_gpu_ctrl_hdr: type, flags, fence_id, ctx_id, ring_idx, padding[3] */ 16 static size_t hdr(uint8_t *out, size_t cap, size_t len, uint32_t type) 17 { 18 if (!out || cap < len) 19 return 0; 20 memset(out, 0, len); 21 put32(out, type); 22 return len; 23 } 24 25 /* struct virtio_gpu_rect: x, y, width, height */ 26 static void rect(uint8_t *p, uint32_t x, uint32_t y, uint32_t w, uint32_t h) 27 { 28 put32(p, x), put32(p + 4, y), put32(p + 8, w), put32(p + 12, h); 29 } 30 31 size_t vgpu_cmd_display_info(uint8_t *out, size_t cap) { return hdr(out, cap, VGPU_HDR_BYTES, VGPU_CMD_GET_DISPLAY_INFO); } 32 33 /* struct virtio_gpu_resource_create_2d: hdr, resource_id, format, width, height */ 34 size_t vgpu_cmd_create_2d(uint8_t *out, size_t cap, uint32_t res, uint32_t fmt, uint32_t w, uint32_t h) 35 { 36 size_t n = hdr(out, cap, VGPU_HDR_BYTES + 16u, VGPU_CMD_RESOURCE_CREATE_2D); 37 if (n) 38 put32(out + 24, res), put32(out + 28, fmt), put32(out + 32, w), put32(out + 36, h); 39 return n; 40 } 41 42 /* struct virtio_gpu_resource_attach_backing: hdr, resource_id, nr_entries; 43 * then one struct virtio_gpu_mem_entry: addr, length, padding */ 44 size_t vgpu_cmd_attach_backing(uint8_t *out, size_t cap, uint32_t res, uint64_t addr, uint32_t len) 45 { 46 size_t n = hdr(out, cap, VGPU_HDR_BYTES + 8u + 16u, VGPU_CMD_RESOURCE_ATTACH_BACKING); 47 if (n) 48 put32(out + 24, res), put32(out + 28, 1), put64(out + 32, addr), put32(out + 40, len); 49 return n; 50 } 51 52 /* struct virtio_gpu_set_scanout: hdr, rect, scanout_id, resource_id */ 53 size_t vgpu_cmd_set_scanout(uint8_t *out, size_t cap, uint32_t scanout, uint32_t res, uint32_t w, uint32_t h) 54 { 55 size_t n = hdr(out, cap, VGPU_HDR_BYTES + 16u + 8u, VGPU_CMD_SET_SCANOUT); 56 if (n) 57 rect(out + 24, 0, 0, w, h), put32(out + 40, scanout), put32(out + 44, res); 58 return n; 59 } 60 61 /* struct virtio_gpu_transfer_to_host_2d: hdr, rect, offset (64), resource_id, padding */ 62 size_t vgpu_cmd_transfer_2d(uint8_t *out, size_t cap, uint32_t res, uint32_t x, uint32_t y, uint32_t w, uint32_t h, 63 uint64_t offset) 64 { 65 size_t n = hdr(out, cap, VGPU_HDR_BYTES + 16u + 16u, VGPU_CMD_TRANSFER_TO_HOST_2D); 66 if (n) 67 rect(out + 24, x, y, w, h), put64(out + 40, offset), put32(out + 48, res); 68 return n; 69 } 70 71 /* struct virtio_gpu_resource_flush: hdr, rect, resource_id, padding */ 72 size_t vgpu_cmd_flush(uint8_t *out, size_t cap, uint32_t res, uint32_t x, uint32_t y, uint32_t w, uint32_t h) 73 { 74 size_t n = hdr(out, cap, VGPU_HDR_BYTES + 16u + 8u, VGPU_CMD_RESOURCE_FLUSH); 75 if (n) 76 rect(out + 24, x, y, w, h), put32(out + 40, res); 77 return n; 78 } 79 80 uint32_t vgpu_resp_type(const uint8_t *resp, size_t len) { return resp && len >= VGPU_HDR_BYTES ? get32(resp) : 0; } 81 82 /* struct virtio_gpu_resp_display_info: hdr, then pmodes[16] of {rect, enabled, flags} */ 83 int vgpu_display0(const uint8_t *resp, size_t len, uint32_t *w, uint32_t *h) 84 { 85 if (len < VGPU_DISPLAY_INFO_BYTES || vgpu_resp_type(resp, len) != VGPU_RESP_OK_DISPLAY_INFO) 86 return 0; 87 const uint8_t *m = resp + VGPU_HDR_BYTES; 88 uint32_t mw = get32(m + 8), mh = get32(m + 12), en = get32(m + 16); 89 if (!en || !mw || !mh) 90 return 0; 91 *w = mw, *h = mh; 92 return 1; 93 } 94 95 #ifndef CK_HOSTED 96 #include "ck.h" 97 #include "virtio_bind.h" 98 99 #define VGPU_SPINS 200000000u 100 101 static struct { 102 vq_bind b; 103 uint8_t *cmd, *resp; /* one page each after the rings */ 104 uint64_t cmd_bus, resp_bus; 105 uint32_t w, h; 106 int on; 107 } g; 108 109 /* One command, one reply: 0 when the reply's type is want. */ 110 static int command(size_t len, uint32_t want, uint32_t *got) 111 { 112 vq_buf b[2] = {{g.cmd_bus, (uint32_t)len, 0}, {g.resp_bus, 4096u, 1}}; 113 uint32_t wrote = 0; 114 memset(g.resp, 0, VGPU_HDR_BYTES); 115 int e = vq_call(&g.b.dev, 0, b, 2, VGPU_SPINS, &wrote); 116 *got = e ? 0 : vgpu_resp_type(g.resp, wrote); 117 return e ? e : (*got == want ? 0 : -1); 118 } 119 120 int ck_vgpu_open(const pci_system *pci, uint32_t max_w, uint32_t max_h, volatile uint32_t **fb, uint32_t *w, 121 uint32_t *h) 122 { 123 /* 0x1050 = 0x1040 + device id 16 (virtio 1.x 4.1.2.1): virtio-gpu-pci and virtio-vga. 124 * DMA window: rings, a command page, a reply page, then the picture. */ 125 if (vq_bind_open(&g.b, "virtio_gpu", pci, 0x1050u, 1, 2u * 4096u + (size_t)max_w * max_h * 4u)) 126 return -1; 127 vq_ready(&g.b.dev); 128 g.cmd = g.b.mem + g.b.rings, g.cmd_bus = g.b.phys + g.b.rings; 129 g.resp = g.cmd + 4096u, g.resp_bus = g.cmd_bus + 4096u; 130 uint8_t *px = g.resp + 4096u; 131 uint64_t px_bus = g.resp_bus + 4096u; 132 133 uint32_t got = 0, dw = 0, dh = 0; 134 if (command(vgpu_cmd_display_info(g.cmd, 4096), VGPU_RESP_OK_DISPLAY_INFO, &got) || 135 !vgpu_display0(g.resp, VGPU_DISPLAY_INFO_BYTES, &dw, &dh)) { 136 ck_printf("virtio_gpu: no display (reply 0x%x)\n", got); 137 return -7; 138 } 139 g.w = dw < max_w ? dw : max_w, g.h = dh < max_h ? dh : max_h; 140 uint32_t bytes = g.w * g.h * 4u; 141 if (command(vgpu_cmd_create_2d(g.cmd, 4096, VGPU_RESOURCE, VGPU_FORMAT_B8G8R8X8_UNORM, g.w, g.h), 142 VGPU_RESP_OK_NODATA, &got) || 143 command(vgpu_cmd_attach_backing(g.cmd, 4096, VGPU_RESOURCE, px_bus, bytes), VGPU_RESP_OK_NODATA, &got) || 144 command(vgpu_cmd_set_scanout(g.cmd, 4096, 0, VGPU_RESOURCE, g.w, g.h), VGPU_RESP_OK_NODATA, &got)) { 145 ck_printf("virtio_gpu: screen setup refused (reply 0x%x)\n", got); 146 return -8; 147 } 148 g.on = 1; 149 ck_printf("virtio_gpu: screen %ux%u (display %ux%u) bgrx, picture %u KiB in the confined window\n", g.w, g.h, 150 dw, dh, bytes >> 10); 151 *fb = (volatile uint32_t *)px, *w = g.w, *h = g.h; 152 return 0; 153 } 154 155 void ck_vgpu_present(void *ctx) 156 { 157 (void)ctx; 158 uint32_t got; 159 if (!g.on) 160 return; 161 if (command(vgpu_cmd_transfer_2d(g.cmd, 4096, VGPU_RESOURCE, 0, 0, g.w, g.h, 0), VGPU_RESP_OK_NODATA, &got) || 162 command(vgpu_cmd_flush(g.cmd, 4096, VGPU_RESOURCE, 0, 0, g.w, g.h), VGPU_RESP_OK_NODATA, &got)) { 163 ck_printf("virtio_gpu: present refused (reply 0x%x); screen off\n", got); 164 g.on = 0; 165 } 166 } 167 #endif