msi: Small cleanups and fixes
cover msi: Small cleanups and fixes
A few patches to the MSI and MSI-X layer that clean up the interfaces and fix reset issues. They are from my MSI rework to prepare it for KVM's requirements (in-kernel irqchip).
Jan Kiszka (7):
msi: Fix copy&paste mistake in msi_uninit
msi: Guard msi/msix_write_config with msi_present
msi: Guard msi_reset with msi_present
msi: Use msi/msix_present more consistently ahci/intel-hda: Properly reset MSI state
msix: Align MSI-X constants to libpci definitions and extend them
msi: Move PCI_MSI_PENDING_32/64 into pci_regs.h
hw/ide/ahci.c | 2 ++ hw/intel-hda.c | 8 +++----- hw/msi.c | 15 ++++++++------- hw/msix.c | 39 ++++++++++++++++++++------------------- hw/pci_regs.h | 16 ++++++++++------ 5 files changed, 43 insertions(+), 37 deletions(-)
PATCH 2/7 msi: Guard msi/msix_write_config with msi_present
Terminate msi/msix_write_config early if support is not enabled. This allows to remove checks at the caller site if MSI is optional.
--- hw/intel-hda.c | 6 +----- hw/msi.c | 3 ++- hw/msix.c | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-)
hw/intel-hda.c +1 -5
@@ -1173,12 +1173,8 @@ static int intel_hda_exit(PCIDevice *pci)static void intel_hda_write_config (PCIDevice *pci,uint32_t addr,uint32_t val,int len) { -IntelHDAState *d =DO_UPCAST (IntelHDAState, pci, pci); -pci_default_write_config (pci, addr, val, len); -if (d->msi) { -msi_write_config (pci, addr, val, len); - } +msi_write_config (pci, addr, val, len); }static int intel_hda_post_load (void *opaque,int version)
hw/msi.c +2 -1
@@ -264,7 +264,8 @@ void msi_write_config(PCIDevice *dev, uint32_t addr, uint32_t val, int len)unsigned int vector;uint32_t pending; -if (!ranges_overlap (addr, len, dev->msi_cap,msi_cap_sizeof (flags))) { +if (!msi_present (dev) || + !ranges_overlap (addr, len, dev->msi_cap,msi_cap_sizeof (flags))) {return ; }
hw/msix.c +1 -1
@@ -160,7 +160,7 @@ void msix_write_config(PCIDevice *dev, uint32_t addr,unsigned enable_pos = dev->msix_cap +MSIX_CONTROL_OFFSET ;int vector; -if (!range_covers_byte (addr, len, enable_pos)) { +if (!msix_present (dev) || !range_covers_byte (addr, len, enable_pos)) {return ; }
Signature · 1 line
1.7.1
PATCH 5/7 ahci/intel-hda: Properly reset MSI state
In reply to Jan KiszkaAlso invoke msi_reset on device reset to unsure proper config space state.
--- hw/ide/ahci.c | 2 ++ hw/intel-hda.c | 2 ++ 2 files changed, 4 insertions(+), 0 deletions(-)
hw/ide/ahci.c +2 -0
@@ -1159,6 +1159,8 @@ void ahci_reset(void *opaque)struct AHCIPCIState *d = opaque;int i; +msi_reset (&d->card); + d->ahci.control_regs.irqstatus =0 ; d->ahci.control_regs.ghc =0 ;
hw/intel-hda.c +2 -0
@@ -1117,6 +1117,8 @@ static void intel_hda_reset(DeviceState *dev)DeviceState *qdev;HDACodecDevice *cdev; +msi_reset (&d->pci); +intel_hda_regs_reset (d); d->wall_base_ns =qemu_get_clock_ns (vm_clock);
Signature · 1 line
1.7.1
PATCH 6/7 msix: Align MSI-X constants to libpci definitions and extend them
In reply to Jan KiszkaAdd PCI_MSIX_TABLE and PCI_MSIX_PBA, align other MSIX related constant names to libpci style. Will be used for device assignment code in qemu-kvm.
--- hw/msix.c | 24 +++++++++++------------- hw/pci_regs.h | 14 ++++++++------ 2 files changed, 19 insertions(+), 19 deletions(-)
hw/msix.c +11 -13
@@ -16,15 +16,12 @@#include "pci.h" #include "range.h" -/* MSI-X capability structure */ -#define MSIX_TABLE_OFFSET 4 -#define MSIX_PBA_OFFSET 8#define MSIX_CAP_LENGTH 12 -/* MSI enable bit and maskall bit are in byte 1 in FLAGS register */ -#define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1) -#define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8) -#define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8) +/* MSI enable bit and maskall bit are in byte 1 in control register */ +#define MSIX_CONTROL_OFFSET (PCI_MSIX_CTRL + 1) +#define MSIX_ENABLE_MASK (PCI_MSIX_ENABLE >> 8) +#define MSIX_MASKALL_MASK (PCI_MSIX_MASK >> 8)/* MSI-X table format */ #define MSIX_MSG_ADDR 0
@@ -58,8 +55,9 @@ static int msix_add_config(struct PCIDevice *pdev, unsigned short nentries,uint8_t *config;uint32_t new_size; -if (nentries <1 || nentries >PCI_MSIX_FLAGS_QSIZE +1 ) +if (nentries <1 || nentries >PCI_MSIX_TABSIZE +1 ) {return -EINVAL ; + }if (bar_size >0x80000000 )return -ENOSPC ;
@@ -80,11 +78,11 @@ static int msix_add_config(struct PCIDevice *pdev, unsigned short nentries,return config_offset; config = pdev->config + config_offset; -pci_set_word (config +PCI_MSIX_FLAGS , nentries -1 ); +pci_set_word (config +PCI_MSIX_CTRL , nentries -1 );/* Table on top of BAR */ -pci_set_long (config +MSIX_TABLE_OFFSET , bar_size | bar_nr); +pci_set_long (config +PCI_MSIX_TABLE , bar_size | bar_nr);/* Pending bits on top of that */ -pci_set_long (config +MSIX_PBA_OFFSET , (bar_size +MSIX_PAGE_PENDING ) | +pci_set_long (config +PCI_MSIX_PBA , (bar_size +MSIX_PAGE_PENDING ) | bar_nr); pdev->msix_cap = config_offset;/* Make flags bit writable. */
@@ -208,11 +206,11 @@ void msix_mmio_map(PCIDevice *d, int region_num, pcibus_t addr, pcibus_t size, int type) { uint8_t *config = d->config + d->msix_cap; -uint32_t table =pci_get_long (config +MSIX_TABLE_OFFSET ); +uint32_t table =pci_get_long (config +PCI_MSIX_TABLE );uint32_t offset = table & ~(MSIX_PAGE_SIZE -1 );/* TODO: for assigned devices, we'll want to make it possible to map * pending bits separately in case they are in a separate bar. */ -int table_bir = table &PCI_MSIX_FLAGS_BIRMASK ; +int table_bir = table &PCI_MSIX_BIR ;if (table_bir != region_num)return ;
hw/pci_regs.h +8 -6
@@ -300,12 +300,14 @@#define PCI_MSI_DATA_64 12/* 16 bits of data for 64-bit devices */ #define PCI_MSI_MASK_64 16/* Mask bits register for 64-bit devices */ -/* MSI-X registers (these are at offset PCI_MSIX_FLAGS) */ -#define PCI_MSIX_FLAGS 2 -#define PCI_MSIX_FLAGS_QSIZE 0x7FF -#define PCI_MSIX_FLAGS_ENABLE (1 << 15) -#define PCI_MSIX_FLAGS_MASKALL (1 << 14) -#define PCI_MSIX_FLAGS_BIRMASK (7 << 0) +/* MSI-X registers */ +#define PCI_MSIX_CTRL 2/* Message control */ +#define PCI_MSIX_TABSIZE 0x7FF/* Table size - 1 */ +#define PCI_MSIX_MASK 0x4000/* Mask all vectors */ +#define PCI_MSIX_ENABLE 0x8000/* Enable MSI-X */ +#define PCI_MSIX_TABLE 4/* MSI-X table */ +#define PCI_MSIX_PBA 8/* Pending bit array */ +#define PCI_MSIX_BIR 0x7/* BAR indication register */ /* CompactPCI Hotswap Register */
Signature · 1 line
1.7.1
PATCH 7/7 msi: Move PCI_MSI_PENDING_32/64 into pci_regs.h
In reply to Jan Kiszka--- hw/msi.c | 4 ---- hw/pci_regs.h | 2 ++ 2 files changed, 2 insertions(+), 4 deletions(-)
hw/msi.c +0 -4
@@ -21,10 +21,6 @@#include "msi.h" #include "range.h" -/* Eventually those constants should go to Linux pci_regs.h */ -#define PCI_MSI_PENDING_32 0x10 -#define PCI_MSI_PENDING_64 0x14 -/* PCI_MSI_ADDRESS_LO */ #define PCI_MSI_ADDRESS_LO_MASK (~0x3)
hw/pci_regs.h +2 -0
@@ -297,8 +297,10 @@#define PCI_MSI_ADDRESS_HI 8/* Upper 32 bits (if PCI_MSI_FLAGS_64BIT set) */ #define PCI_MSI_DATA_32 8/* 16 bits of data for 32-bit devices */ #define PCI_MSI_MASK_32 12/* Mask bits register for 32-bit devices */ +#define PCI_MSI_PENDING_32 16/* Pending bits register for 32-bit devices */ #define PCI_MSI_DATA_64 12/* 16 bits of data for 64-bit devices */ #define PCI_MSI_MASK_64 16/* Mask bits register for 64-bit devices */ +#define PCI_MSI_PENDING_64 20/* Pending bits register for 64-bit devices */ /* MSI-X registers */ #define PCI_MSIX_CTRL 2/* Message control */
Signature · 1 line
1.7.1
PATCH 1/7 msi: Fix copy&paste mistake in msi_uninit
In reply to Jan Kiszka--- hw/msi.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
hw/msi.c +1 -1
@@ -172,7 +172,7 @@ void msi_uninit(struct PCIDevice *dev) } flags =pci_get_word (dev->config +msi_flags_off (dev)); cap_size =msi_cap_sizeof (flags); -pci_del_capability (dev,PCI_CAP_ID_MSIX , cap_size); +pci_del_capability (dev,PCI_CAP_ID_MSI , cap_size); dev->cap_present &= ~QEMU_PCI_CAP_MSI ;MSI_DEV_PRINTF (dev,"uninit\n" );
Signature · 1 line
1.7.1
PATCH 4/7 msi: Use msi/msix_present more consistently
In reply to Jan KiszkaReplace some open-coded msi/msix_present checks and drop redundant msix_supported tests (present implies supported).
--- hw/msi.c | 2 +- hw/msix.c | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-)
hw/msi.c +1 -1
@@ -167,7 +167,7 @@ void msi_uninit(struct PCIDevice *dev)uint16_t flags;uint8_t cap_size; -if (!(dev->cap_present &QEMU_PCI_CAP_MSI )) { +if (!msi_present (dev)) {return ; } flags =pci_get_word (dev->config +msi_flags_off (dev));
hw/msix.c +8 -5
@@ -290,8 +290,9 @@ static void msix_free_irq_entries(PCIDevice *dev)/* Clean up resources for the device. */ int msix_uninit (PCIDevice *dev) { -if (!(dev->cap_present &QEMU_PCI_CAP_MSIX )) +if (!msix_present (dev)) {return 0 ; + }pci_del_capability (dev,PCI_CAP_ID_MSIX ,MSIX_CAP_LENGTH ); dev->msix_cap =0 ;msix_free_irq_entries (dev);
@@ -309,7 +310,7 @@ void msix_save(PCIDevice *dev, QEMUFile *f) {unsigned n = dev->msix_entries_nr; -if (!(dev->cap_present &QEMU_PCI_CAP_MSIX )) { +if (!msix_present (dev)) {return ; }
@@ -322,7 +323,7 @@ void msix_load(PCIDevice *dev, QEMUFile *f) {unsigned n = dev->msix_entries_nr; -if (!(dev->cap_present &QEMU_PCI_CAP_MSIX )) { +if (!msix_present (dev)) {return ; }
@@ -374,8 +375,9 @@ void msix_notify(PCIDevice *dev, unsigned vector)void msix_reset (PCIDevice *dev) { -if (!(dev->cap_present &QEMU_PCI_CAP_MSIX )) +if (!msix_present (dev)) {return ; + }msix_free_irq_entries (dev); dev->config[dev->msix_cap +MSIX_CONTROL_OFFSET ] &= ~dev->wmask[dev->msix_cap +MSIX_CONTROL_OFFSET ];
@@ -414,7 +416,8 @@ void msix_vector_unuse(PCIDevice *dev, unsigned vector)void msix_unuse_all_vectors (PCIDevice *dev) { -if (!(dev->cap_present &QEMU_PCI_CAP_MSIX )) +if (!msix_present (dev)) {return ; + }msix_free_irq_entries (dev); }
Signature · 1 line
1.7.1
PATCH 3/7 msi: Guard msi_reset with msi_present
In reply to Jan Kiszka--- hw/msi.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
hw/msi.c +4 -0
@@ -183,6 +183,10 @@ void msi_reset(PCIDevice *dev)uint16_t flags;bool msi64bit; +if (!msi_present (dev)) { +return ; + } + flags =pci_get_word (dev->config +msi_flags_off (dev)); flags &= ~(PCI_MSI_FLAGS_QSIZE |PCI_MSI_FLAGS_ENABLE ); msi64bit = flags &PCI_MSI_FLAGS_64BIT ;
Signature · 1 line
1.7.1
[Qemu-devel] [PATCH 8/7] ivshmem: Reset MSI-X state on device reset
In reply to Jan Kiszka--- hw/ivshmem.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
hw/ivshmem.c +1 -0
@@ -545,6 +545,7 @@ static void ivshmem_reset(DeviceState *d) {IVShmemState *s =DO_UPCAST (IVShmemState, dev.qdev, d); +msix_reset (&s->dev); s->intrstatus =0 ;return ; }
Re: [Qemu-devel] [PATCH 5/7] ahci/intel-hda: Properly reset MSI state
In reply to Jan KiszkaQuoted 1 line · Jan Kiszka
Also invoke
msi_reseton device reset to unsure proper config space
ensure.
Otherwise looks sane :)
Alex
Re: [Qemu-devel] [PATCH 5/7] ahci/intel-hda: Properly reset MSI state
Quoted 6 lines · Alexander Graf
Quoted 1 line · Jan Kiszka
Also invoke
msi_reseton device reset to unsure proper config spaceensure.
Sure. :)
Michael, please fix up on commit unless I'll have to repost anyway.
Quoted 6 lines
Otherwise looks sane :)
Alex
Thanks,
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1 Corporate Competence Center Embedded Linux
Re: [Qemu-devel] [PATCH 2/7] msi: Guard msi/msix_write_config with msi_present
In reply to Jan Kiszkahw/intel-hda.c static int intel_hda_exit(PCIDevice *pci) · line 1178
Quoted 12 lines
hw/intel-hda.c +1
-5@@ -1173,12 +1173,8 @@ static int intel_hda_exit(PCIDevice *pci)static void intel_hda_write_config (PCIDevice *pci,uint32_t addr,uint32_t val,int len) { -IntelHDAState *d =DO_UPCAST (IntelHDAState, pci, pci); -pci_default_write_config (pci, addr, val, len); -if (d->msi) { -msi_write_config (pci, addr, val, len); - } +msi_write_config (pci, addr, val, len); }
Nothing device specific left in there now. If msi_write_config() checks itself whenever msi is enabled or not we could call it from pci_default_write_config(), then zap this function altogether, no?
cheers,
Gerd
Re: [Qemu-devel] [PATCH 2/7] msi: Guard msi/msix_write_config with msi_present
Quoted 8 lines · Gerd Hoffmann
Quoted 8 lines
hw/intel-hda.c +0
-3@@ -1173,12 +1173,8 @@ static int intel_hda_exit(PCIDevice *pci)static void intel_hda_write_config (PCIDevice *pci,uint32_t addr,uint32_t val,int len) { -IntelHDAState *d =DO_UPCAST (IntelHDAState, pci, pci); -pci_default_write_config (pci, addr, val, len); -if (d->msi) {
Quoted 8 lines
hw/intel-hda.c static int intel_hda_exit(PCIDevice *pci) · line 1178
Quoted 4 lines
hw/intel-hda.c +1
-2-msi_write_config (pci, addr, val, len); - } +msi_write_config (pci, addr, val, len); }Nothing device specific left in there now. If
msi_write_config()checks itself whenever msi is enabled or not we could call it frompci_default_write_config(), then zap this function altogether, no?
Well, we could put those helpers (msi_write_config, msi_reset, maybe more) into generic PCI code. Then device will only have to overload the handlers if they do something in addition. Haven't checked the details yet, though.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1 Corporate Competence Center Embedded Linux
Re: [Qemu-devel] [PATCH 2/7] msi: Guard msi/msix_write_config with msi_present
Quoted 18 lines · Jan Kiszka
Quoted 8 lines · Gerd Hoffmann
Quoted 8 lines
hw/intel-hda.c +0
-3@@ -1173,12 +1173,8 @@ static int intel_hda_exit(PCIDevice *pci)static void intel_hda_write_config (PCIDevice *pci,uint32_t addr,uint32_t val,int len) { -IntelHDAState *d =DO_UPCAST (IntelHDAState, pci, pci); -pci_default_write_config (pci, addr, val, len); -if (d->msi) {Quoted 8 lines
hw/intel-hda.c static int intel_hda_exit(PCIDevice *pci) · line 1178
Quoted 4 lines
hw/intel-hda.c +1
-2-msi_write_config (pci, addr, val, len); - } +msi_write_config (pci, addr, val, len); }Nothing device specific left in there now. If
msi_write_config()checks itself whenever msi is enabled or not we could call it frompci_default_write_config(), then zap this function altogether, no?
Quoted 4 lines
Well, we could put those helpers (
msi_write_config,msi_reset, maybe more) into generic PCI code. Then device will only have to overload the handlers if they do something in addition. Haven't checked the details yet, though.
Seems to fly.
I'll repost the series to make msi_reset, msi_write_config, and msi_uninit implicit. Less boilerplate, less potential bugs.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1 Corporate Competence Center Embedded Linux
Re: [Qemu-devel] [PATCH 6/7] msix: Align MSI-X constants to libpci definitions and extend them
In reply to Jan KiszkaQuoted 3 lines · Jan Kiszka
Add
PCI_MSIX_TABLEandPCI_MSIX_PBA, align other MSIX related constant names to libpci style. Will be used for device assignment code in qemu-kvm.
So since libpci would be used by qemu, you are claiming that it should be switched to libpci style from Linux pci_regs.h?
Signature · 1 line
yamahata
Re: [Qemu-devel] [PATCH 6/7] msix: Align MSI-X constants to libpci definitions and extend them
Quoted 7 lines · Isaku Yamahata
Quoted 3 lines · Jan Kiszka
Add
PCI_MSIX_TABLEandPCI_MSIX_PBA, align other MSIX related constant names to libpci style. Will be used for device assignment code in qemu-kvm.So since libpci would be used by qemu, you are claiming that it should be switched to libpci style from Linux
pci_regs.h?
No, libpci won't be used (I'm about to remove that dependency from qemu-kvm). We are just copying its definitions, so I think we should align to its style.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1 Corporate Competence Center Embedded Linux
Re: [Qemu-devel] [PATCH 6/7] msix: Align MSI-X constants to libpci definitions and extend them
Quoted 6 lines · Jan Kiszka
Quoted 5 lines · Isaku Yamahata
Quoted 3 lines · Jan Kiszka
Add
PCI_MSIX_TABLEandPCI_MSIX_PBA, align other MSIX related constant names to libpci style. Will be used for device assignment code in qemu-kvm.
Quoted 8 lines
Quoted 2 lines
So since libpci would be used by qemu, you are claiming that it should be switched to libpci style from Linux
pci_regs.h?No, libpci won't be used (I'm about to remove that dependency from qemu-kvm). We are just copying its definitions, so I think we should align to its style.
Jan
But why?
We are currently aligned with pci_regs.h from linux. What is the benefit of switching styles?
It adds support overhead e.g. as backport becomes harder.
BTW, libpci doesn't seem to have pci_regs.h - it has pci/header.h
If some register definitions are missing in linux, what we did in the past is:
- stick them in a C file that needs them meanwhile
- long term add them to linux and backport to our header
We could also add pci_ext_regs.h or something to this end as an alternative, although this removes the pressure to upstream definitions ...
Quoted 3 lines
Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1 Corporate Competence Center Embedded Linux
Quoted 2 lines · Jan Kiszka
A few patches to the MSI and MSI-X layer that clean up the interfaces and fix reset issues.
Are there any more fixes besides the typo in uninit?
Quoted 21 lines
They are from my MSI rework to prepare it for
KVM's requirements (in-kernel irqchip).CC Alexander Graf <[email protected]>CC Gerd Hoffmann <[email protected]>Jan Kiszka (7):
msi: Fix copy&paste mistake inmsi_uninit
msi: Guard msi/msix_write_config withmsi_present
msi: Guardmsi_resetwithmsi_present
msi: Use msi/msix_present more consistently ahci/intel-hda: Properly reset MSI state
msix: Align MSI-X constants to libpci definitions and extend them
msi: Move PCI_MSI_PENDING_32/64 intopci_regs.hhw/ide/ahci.c | 2 ++ hw/intel-hda.c | 8 +++----- hw/msi.c | 15 ++++++++------- hw/msix.c | 39 ++++++++++++++++++++------------------- hw/pci_regs.h | 16 ++++++++++------ 5 files changed, 43 insertions(+), 37 deletions(-)