Skip to content

Commit

Permalink
WIP virtio-pci
Browse files Browse the repository at this point in the history
  • Loading branch information
travisg committed Mar 23, 2024
1 parent 3288b15 commit 0ec65f7
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 10 deletions.
12 changes: 12 additions & 0 deletions dev/bus/pci/bus_mgr/bus_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,18 @@ status_t pci_bus_mgr_allocate_irq(const pci_location_t loc, uint *irqbase) {
return d->allocate_irq(irqbase);
}

ssize_t pci_read_vendor_capability(const pci_location_t loc, size_t index, void *buf, size_t buflen) {
char str[14];
LTRACEF("%s\n", pci_loc_string(loc, str));

device *d = lookup_device_by_loc(loc);
if (!d) {
return ERR_NOT_FOUND;
}

return d->read_vendor_capability(index, buf, buflen);
}

void pci_dump_bar(const pci_bar_t *bar, int index) {
if (bar->addr >= UINT32_MAX || bar->size >= UINT32_MAX) {
printf("BAR %d: addr %-#16llx size %-#16zx io %d 64b %d pref %d\n",
Expand Down
29 changes: 29 additions & 0 deletions dev/bus/pci/bus_mgr/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ void device::dump(size_t indent) {
pci_dump_bar(bars_ + b, b);
}
}

capability *cap;
list_for_every_entry(&capability_list_, cap, capability, node) {
for (size_t i = 0; i < indent + 2; i++) {
printf(" ");
}
printf("capability: offset %#x id %#x\n", cap->config_offset, cap->id);
}
}

status_t device::enable() {
Expand Down Expand Up @@ -220,6 +228,27 @@ status_t device::probe_capabilities() {
return NO_ERROR;
}

ssize_t device::read_vendor_capability(size_t index, void *buf, size_t buflen) {
const capability *cap;
list_for_every_entry(&capability_list_, cap, capability, node) {
if (cap->id == 0x9) { // vendor specific
if (index == 0) {
uint8_t len;
pci_read_config_byte(loc(), cap->config_offset + 2, &len);

const size_t readlen = MIN(len, buflen);
for (size_t i = 0; i < readlen; i++) {
pci_read_config_byte(loc(), cap->config_offset + i, static_cast<uint8_t *>(buf) + i);
}
return len;
}
index--;
}
}

return ERR_NOT_FOUND;
}

status_t device::init_msi_capability(capability *cap) {
LTRACE_ENTRY;

Expand Down
1 change: 1 addition & 0 deletions dev/bus/pci/bus_mgr/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class device {
uint8_t header_type() const { return config_.header_type & PCI_HEADER_TYPE_MASK; }

status_t read_bars(pci_bar_t bar[6]);
ssize_t read_vendor_capability(size_t index, void *buf, size_t buflen);

bool has_msi() const { return msi_cap_; }
bool has_msix() const { return msix_cap_; }
Expand Down
4 changes: 3 additions & 1 deletion dev/bus/pci/drivers/rules.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Fake module that just declares deps on all the PCI drivers in the system.
#
MODULES += dev/bus/pci

MODULES += dev/net/e1000
MODULES += dev/virtio/block
MODULES += dev/virtio/net
MODULES += dev/virtio/gpu
3 changes: 3 additions & 0 deletions dev/bus/pci/include/dev/bus/pci.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ status_t pci_bus_mgr_allocate_msi(const pci_location_t loc, size_t num_requested
// allocate a regular irq for this device and return it in irqbase
status_t pci_bus_mgr_allocate_irq(const pci_location_t loc, uint *irqbase);

// XXX sort this nicely
ssize_t pci_read_vendor_capability(const pci_location_t loc, size_t index, void *buf, size_t buflen);

// return a pointer to a formatted string
const char *pci_loc_string(pci_location_t loc, char out_str[14]);

Expand Down
4 changes: 2 additions & 2 deletions dev/virtio/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ LOCAL_DIR := $(GET_LOCAL_DIR)

MODULE := $(LOCAL_DIR)

MODULE_SRCS += \
$(LOCAL_DIR)/virtio.c
MODULE_SRCS += $(LOCAL_DIR)/virtio.c
MODULE_SRCS += $(LOCAL_DIR)/virtio_pci.c

include make/module.mk
32 changes: 25 additions & 7 deletions scripts/do-qemuarm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

function HELP {
echo "help:"
echo "-p <project> : override LK project"
echo "-6 : 64bit arm"
echo "-3 : cortex-m3 based platform"
echo "-v : boot kernel at EL2"
Expand All @@ -17,6 +18,7 @@ function HELP {
echo "-n : a virtio network device"
echo "-t : a virtio tap network device"
echo "-g : a virtio display"
echo "-P : PCI based virtio devices"
echo
echo "-h for help"
echo "all arguments after -- are passed to qemu directly"
Expand All @@ -33,12 +35,13 @@ DO_CORTEX_M3=0
DO_DISPLAY=0
DO_CMPCTMALLOC=0
DO_MINIHEAP=0
DO_PCI_VIRTIO=0
SMP=1
MEMSIZE=512
SUDO=""
PROJECT=""

while getopts cd:ghm:Mnt36vp:s: FLAG; do
while getopts cd:ghm:Mnt36vp:Ps: FLAG; do
case $FLAG in
c) DO_CMPCTMALLOC=1;;
d) DO_DISK=1; DISK_IMAGE=$OPTARG;;
Expand All @@ -52,6 +55,7 @@ while getopts cd:ghm:Mnt36vp:s: FLAG; do
m) MEMSIZE=$OPTARG;;
s) SMP=$OPTARG;;
p) PROJECT=$OPTARG;;
P) DO_PCI_VIRTIO=1;;
h) HELP;;
\?)
echo unrecognized option
Expand Down Expand Up @@ -90,14 +94,28 @@ fi

ARGS=" -cpu $CPU -m $MEMSIZE -smp $SMP -machine $MACHINE -kernel build-${PROJECT}/lk.elf"

if (( $DO_PCI_VIRTIO )); then
VIRTIO_BLOCK_DEVICE="virtio-blk-pci"
VIRTIO_NET_DEVICE="virtio-net-pci"
VIRTIO_GPU_DEVICE="virtio-gpu-pci"
VIRTIO_KEYBOARD_DEVICE="virtio-keyboard-pci"
VIRTIO_MOUSE_DEVICE="virtio-mouse-pci"
else
VIRTIO_BLOCK_DEVICE="virtio-blk-device"
VIRTIO_NET_DEVICE="virtio-net-device"
VIRTIO_GPU_DEVICE="virtio-gpu-device"
VIRTIO_KEYBOARD_DEVICE="virtio-keyboard-device"
VIRTIO_MOUSE_DEVICE="virtio-mouse-device"
fi

if (( $DO_DISK )); then
ARGS+=" -drive if=none,file=${DISK_IMAGE},id=blk,format=raw"
ARGS+=" -device virtio-blk-device,drive=blk"
ARGS+=" -device ${VIRTIO_BLOCK_DEVICE},drive=blk"
fi

if (( $DO_NET )); then
ARGS+=" -netdev user,id=vmnic,hostname=qemu "
ARGS+=" -device virtio-net-device,netdev=vmnic"
ARGS+=" -device ${VIRTIO_NET_DEVICE},netdev=vmnic"
elif (( $DO_NET_TAP )); then
# quick note to enable tap interface
# IFNAME=qemu0
Expand All @@ -106,17 +124,17 @@ elif (( $DO_NET_TAP )); then
# sudo ifconfig ${IFNAME} up
# sudo ip link set ${IFNAME} master ${BRIDGE}
ARGS+=" -netdev tap,id=vmnic,ifname=qemu0,script=no,downscript=no"
ARGS+=" -device virtio-net-device,netdev=vmnic"
ARGS+=" -device ${VIRTIO_NET_DEVICE},netdev=vmnic"
#SUDO="sudo "
else
NO_NET_ARGS=" -net none"
ARGS+=$NO_NET_ARGS
fi

if (( $DO_DISPLAY )); then
ARGS+=" -device virtio-gpu-device -serial stdio"
ARGS+=" -device virtio-keyboard-device"
ARGS+=" -device virtio-mouse-device"
ARGS+=" -device ${VIRTIO_GPU_DEVICE} -serial stdio"
ARGS+=" -device ${VIRTIO_KEYBOARD_DEVICE}"
ARGS+=" -device ${VIRTIO_MOUSE_DEVICE}"
else
ARGS+=" -nographic"
fi
Expand Down

0 comments on commit 0ec65f7

Please sign in to comment.