On Thu, 23 Jan 2020 13:47:55 +0000 Alexandru Elisei <alexandru.elisei@xxxxxxx> wrote: Hi, > We're going to be checking the BAR type, the address written to it and > if access to memory or I/O space is enabled quite often when we add > support for reasignable BARs, add helpers for it. I am not a particular fan of these double underscores inside identifiers, but I guess that is too late now, since it's already all over the place. So: > Signed-off-by: Alexandru Elisei <alexandru.elisei@xxxxxxx> Reviewed-by: Andre Przywara <andre.przywara@xxxxxxx> Cheers, Andre. > --- > include/kvm/pci.h | 48 +++++++++++++++++++++++++++++++++++++++++++++++ > pci.c | 2 +- > 2 files changed, 49 insertions(+), 1 deletion(-) > > diff --git a/include/kvm/pci.h b/include/kvm/pci.h > index ccb155e3e8fe..235cd82fff3c 100644 > --- a/include/kvm/pci.h > +++ b/include/kvm/pci.h > @@ -5,6 +5,7 @@ > #include <linux/kvm.h> > #include <linux/pci_regs.h> > #include <endian.h> > +#include <stdbool.h> > > #include "kvm/devices.h" > #include "kvm/msi.h" > @@ -161,4 +162,51 @@ void pci__config_rd(struct kvm *kvm, union pci_config_address addr, void *data, > > void *pci_find_cap(struct pci_device_header *hdr, u8 cap_type); > > +static inline bool __pci__memory_space_enabled(u16 command) > +{ > + return command & PCI_COMMAND_MEMORY; > +} > + > +static inline bool pci__memory_space_enabled(struct pci_device_header *pci_hdr) > +{ > + return __pci__memory_space_enabled(pci_hdr->command); > +} > + > +static inline bool __pci__io_space_enabled(u16 command) > +{ > + return command & PCI_COMMAND_IO; > +} > + > +static inline bool pci__io_space_enabled(struct pci_device_header *pci_hdr) > +{ > + return __pci__io_space_enabled(pci_hdr->command); > +} > + > +static inline bool __pci__bar_is_io(u32 bar) > +{ > + return bar & PCI_BASE_ADDRESS_SPACE_IO; > +} > + > +static inline bool pci__bar_is_io(struct pci_device_header *pci_hdr, int bar_num) > +{ > + return __pci__bar_is_io(pci_hdr->bar[bar_num]); > +} > + > +static inline bool pci__bar_is_memory(struct pci_device_header *pci_hdr, int bar_num) > +{ > + return !pci__bar_is_io(pci_hdr, bar_num); > +} > + > +static inline u32 __pci__bar_address(u32 bar) > +{ > + if (__pci__bar_is_io(bar)) > + return bar & PCI_BASE_ADDRESS_IO_MASK; > + return bar & PCI_BASE_ADDRESS_MEM_MASK; > +} > + > +static inline u32 pci__bar_address(struct pci_device_header *pci_hdr, int bar_num) > +{ > + return __pci__bar_address(pci_hdr->bar[bar_num]); > +} > + > #endif /* KVM__PCI_H */ > diff --git a/pci.c b/pci.c > index b6892d974c08..4f7b863298f6 100644 > --- a/pci.c > +++ b/pci.c > @@ -185,7 +185,7 @@ void pci__config_wr(struct kvm *kvm, union pci_config_address addr, void *data, > * size, it will write the address back. > */ > if (bar < 6) { > - if (pci_hdr->bar[bar] & PCI_BASE_ADDRESS_SPACE_IO) > + if (pci__bar_is_io(pci_hdr, bar)) > mask = (u32)PCI_BASE_ADDRESS_IO_MASK; > else > mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;