Skip to content

Commit e1d3dbc

Browse files
committed
[WIP] Implement virtio-pci transport layer
1 parent a7d16c1 commit e1d3dbc

File tree

6 files changed

+777
-2
lines changed

6 files changed

+777
-2
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ OPTS :=
1616

1717
LDFLAGS :=
1818

19+
# virtio-pci
20+
ENABLE_VIRTIOPCI ?= 1
21+
$(call set-feature, VIRTIOPCI)
22+
ifeq ($(call has, VIRTIOPCI), 1)
23+
OBJS_EXTRA += virtio-pci.o
24+
endif
25+
1926
# virtio-blk
2027
ENABLE_VIRTIOBLK ?= 1
2128
$(call set-feature, VIRTIOBLK)

device.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@ void u8250_write(hart_t *core,
7676
void u8250_check_ready(u8250_state_t *uart);
7777
void capture_keyboard_input();
7878

79+
/* virtio-pci */
80+
#if SEMU_HAS(VIRTIOPCI)
81+
typedef struct {
82+
void *priv;
83+
} virtio_pci_state_t;
84+
85+
void virtio_pci_read(hart_t *vm,
86+
virtio_pci_state_t *vpci,
87+
uint32_t addr,
88+
uint8_t width,
89+
uint32_t *value);
90+
91+
void virtio_pci_write(hart_t *vm,
92+
virtio_pci_state_t *vpci,
93+
uint32_t addr,
94+
uint8_t width,
95+
uint32_t value);
96+
97+
void virtio_pci_init(virtio_pci_state_t *vpci);
98+
#endif /* SEMU_HAS(VIRTIOPCI) */
99+
79100
/* virtio-net */
80101

81102
#if SEMU_HAS(VIRTIONET)
@@ -366,6 +387,9 @@ typedef struct {
366387
vm_t vm;
367388
plic_state_t plic;
368389
u8250_state_t uart;
390+
#if SEMU_HAS(VIRTIOPCI)
391+
virtio_pci_state_t vpci;
392+
#endif
369393
#if SEMU_HAS(VIRTIONET)
370394
virtio_net_state_t vnet;
371395
#endif

main.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ static void mem_load(hart_t *hart,
125125
return;
126126
}
127127

128-
if ((addr >> 28) == 0xF) { /* MMIO at 0xF_______ */
128+
if ((addr >> 28) == 0x2) { /* MMIO at 0x2_______ */
129+
virtio_pci_read(hart, &data->vsnd, addr, width, value);
130+
return;
131+
} else if ((addr >> 28) == 0xF) { /* MMIO at 0xF_______ */
129132
/* 256 regions of 1MiB */
130133
switch ((addr >> 20) & MASK(8)) {
131134
case 0x0:
@@ -192,7 +195,15 @@ static void mem_store(hart_t *hart,
192195
return;
193196
}
194197

195-
if ((addr >> 28) == 0xF) { /* MMIO at 0xF_______ */
198+
/* TODO */
199+
if (addr >= 0x100000 && addr <= 0x200000) {
200+
printf("!!!!!!!!!!!!!!!!!\n\n\n\n\n");
201+
}
202+
203+
if ((addr >> 28) == 0x2) { /* MMIO at 0x2_______ */
204+
virtio_pci_write(hart, &data->vsnd, addr, width, value);
205+
return;
206+
} else if ((addr >> 28) == 0xF) { /* MMIO at 0xF_______ */
196207
/* 256 regions of 1MiB */
197208
switch ((addr >> 20) & MASK(8)) {
198209
case 0x0:
@@ -668,6 +679,9 @@ static int semu_init(emu_state_t *emu, int argc, char **argv)
668679
/* Set up peripherals */
669680
emu->uart.in_fd = 0, emu->uart.out_fd = 1;
670681
capture_keyboard_input(); /* set up uart */
682+
#if SEMU_HAS(VIRTIOPCI)
683+
virtio_pci_init(&(emu->vpci));
684+
#endif
671685
#if SEMU_HAS(VIRTIONET)
672686
if (!virtio_net_init(&(emu->vnet), netdev))
673687
fprintf(stderr, "No virtio-net functioned\n");

minimal.dts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@
3333
reg-names = "sram0";
3434
};
3535

36+
#if SEMU_FEATURE_VIRTIOPCI
37+
pci0: pci@21000000 {
38+
compatible = "pci-host-ecam-generic";
39+
device_type = "pci";
40+
#address-cells = <3>;
41+
#size-cells = <2>;
42+
bus-range = <0x0 0x0>; /* Use 1 bus only */
43+
reg = <0x21000000 0x100000>;
44+
45+
/* Reserve memory for BAR mapping */
46+
ranges = <0x02000000 0 0x100000 /* PCI memory space (BAR) */
47+
0x20000000 0 0x100000>; /* CPU address space, 16MB */
48+
};
49+
#endif
50+
3651
soc: soc@F0000000 {
3752
#address-cells = <1>;
3853
#size-cells = <1>;

utils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ static inline void list_del_init(struct list_head *node)
7979
INIT_LIST_HEAD(node);
8080
}
8181

82+
#define LIST_HEAD_INIT(name) \
83+
{ \
84+
.prev = (&name), .next = (&name) \
85+
}
86+
87+
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
88+
8289
#ifndef container_of
8390
#define container_of(ptr, type, member) \
8491
__extension__({ \

0 commit comments

Comments
 (0)