interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / dev / virtio_bind.c

98 lines · 3749 bytes Raw Blame History
1/* virtio_bind.c -- a virtio 1.x PCI function behind virtio_q (virtio_bind.h). */
2#include "virtio_bind.h"
3#include "ck.h"
4#include <string.h>
5
6/* The core's register hooks: ctx is the vq_bind. */
7static uint32_t o_read(void *ctx, vnet_window w, uint32_t off, unsigned width)
8{
9 volatile uint8_t *p = ((vq_bind *)ctx)->win[w] + off;
10 if (width == 1)
11 return *p;
12 if (width == 2)
13 return *(volatile uint16_t *)p;
14 return *(volatile uint32_t *)p;
15}
16static void o_write(void *ctx, vnet_window w, uint32_t off, unsigned width, uint32_t v)
17{
18 volatile uint8_t *p = ((vq_bind *)ctx)->win[w] + off;
19 if (width == 1)
20 *p = (uint8_t)v;
21 else if (width == 2)
22 *(volatile uint16_t *)p = (uint16_t)v;
23 else
24 *(volatile uint32_t *)p = v;
25 ck_mb();
26}
27static void o_notify(void *ctx, uint32_t off, uint16_t q)
28{
29 ck_mb(); /* ring writes visible before the doorbell */
30 *(volatile uint16_t *)(((vq_bind *)ctx)->win[VNET_WIN_NOTIFY] + off) = q;
31}
32
33static volatile uint8_t *map_window(vq_bind *b, const virtio_pci_region *r)
34{
35 if (!r->present || r->bar >= PCI_MAX_BARS)
36 return 0;
37 const pci_bar *bar = &b->pf->bar[r->bar];
38 if (bar->io || bar->addr == 0 || bar->size == 0 || (uint64_t)r->offset + r->length > bar->size)
39 return 0;
40 if (!b->bar_va[r->bar])
41 b->bar_va[r->bar] = (volatile uint8_t *)ck_mmio_try_map(bar->addr, (size_t)bar->size);
42 return b->bar_va[r->bar] ? b->bar_va[r->bar] + r->offset : 0;
43}
44
45int vq_bind_open(vq_bind *b, const char *name, const pci_system *pci, uint16_t device, unsigned nq, size_t extra)
46{
47 memset(b, 0, sizeof *b);
48 b->name = name;
49 const pci_func *f = pci_find_id(pci, 0x1af4u, device);
50 if (!f) {
51 ck_printf("%s: none (no 1af4:%04x function)\n", name, device);
52 return -1;
53 }
54 b->pf = f;
55 static uint8_t cfg_image[256];
56 for (uint32_t off = 0; off < sizeof cfg_image; off += 4) {
57 uint32_t v = pci_r32(f->cfg, off);
58 memcpy(cfg_image + off, &v, 4);
59 }
60 virtio_pci_caps caps;
61 virtio_pci_err pe = virtio_pci_parse_caps(cfg_image, sizeof cfg_image, &caps);
62 if (pe != VIRTIO_PCI_OK) {
63 ck_printf("%s: %02x:%02x.%u not used (capabilities: %s)\n", name, f->bus, f->dev, f->fn,
64 virtio_pci_err_name(pe));
65 return -2;
66 }
67 const virtio_pci_region *r[4] = {&caps.common_cfg, &caps.notify_cfg, &caps.isr_cfg, &caps.device_cfg};
68 for (int i = 0; i < 4; i++)
69 if (!(b->win[i] = map_window(b, r[i]))) {
70 ck_printf("%s: not used (capability window %d outside its BAR)\n", name, i);
71 return -3;
72 }
73 b->rings = vq_mem_bytes(nq);
74 b->len = (b->rings + extra + 0xfffu) & ~(size_t)0xfffu;
75 if (!(b->mem = ck_dma_alloc(b->len, 4096, &b->phys))) {
76 ck_printf("%s: not used (DMA allocation of %zu KiB failed)\n", name, b->len >> 10);
77 return -4;
78 }
79 struct ck_dma_confinement cf;
80 uint32_t rid = (uint32_t)f->bus << 8 | (uint32_t)f->dev << 3 | f->fn;
81 int src = ck_dma_confine(f->segment, rid, b->phys, b->len, &cf);
82 if (src) {
83 ck_printf("dma_gate: %s denied (rc=%d), bus master stays off\n", name, src);
84 return -5;
85 }
86 b->stream_id = cf.stream_id;
87 pci_enable(f, 1);
88 vnet_ops ops = {b, o_read, o_write, o_notify};
89 int e = vq_init(&b->dev, &ops, &caps, b->mem, b->phys, b->rings, nq, VQ_INIT_REQUIRE_ACCESS_PLATFORM);
90 if (e) {
91 ck_printf("%s: init FAIL (%s)\n", name, vq_err_name(e));
92 pci_bus_master_off(f);
93 return -6;
94 }
95 ck_printf("dma_gate: %s granted (Confined) %02x:%02x.%u stream 0x%x window %zu KiB\n", name, f->bus, f->dev,
96 f->fn, cf.stream_id, b->len >> 10);
97 return 0;
98}