From 3b4dade91fc2e604d071c756e64cf428193b312e Mon Sep 17 00:00:00 2001 From: Pedro Falcato Date: Sat, 31 Dec 2022 03:12:16 +0000 Subject: [PATCH] [bus][pci] Disable IO and mem decoding around BAR enumeration Disable IO and mem decoding around BAR enumation as described in the PCI Local Bus specification. This behavior should be safer when messing around BARs for BAR lengths. --- dev/bus/pci/bus_mgr/device.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/dev/bus/pci/bus_mgr/device.cpp b/dev/bus/pci/bus_mgr/device.cpp index 2b0402fe..c2903ca4 100644 --- a/dev/bus/pci/bus_mgr/device.cpp +++ b/dev/bus/pci/bus_mgr/device.cpp @@ -338,6 +338,14 @@ status_t device::load_bars() { return ERR_NOT_SUPPORTED; } + // Disable IO and MEM decoding around BAR detection, as we fiddle with + // BAR addresses themselves for length detection. + // This behavior is recommended by the PCI Local Bus Specification. + + uint16_t command; + pci_read_config_half(loc(), PCI_CONFIG_COMMAND, &command); + pci_write_config_half(loc(), PCI_CONFIG_COMMAND, command & ~(PCI_COMMAND_IO_EN | PCI_COMMAND_MEM_EN)); + for (size_t i=0; i < num_bars; i++) { bars_[i] = {}; uint64_t bar_addr = config_.type0.base_addresses[i]; @@ -411,6 +419,9 @@ status_t device::load_bars() { } } + // Restore any IO and MEM decoding that was enabled before + pci_write_config_half(loc(), PCI_CONFIG_COMMAND, command); + return NO_ERROR; }