PCI: Index saved capability state by configuration space offset
Subsystems (6)
v2 cover PCI: Index saved capability state by configuration space offset
This series can be found on GitHub:
https://github.com/dmatlack/linux/tree/liveupdate/pci/savedstate/v2
This series replaces the per-capability save buffers with a single per-device store indexed by configuration space offset, lays out struct pci_saved_state the same way, and adds helpers for drivers to preserve struct pci_saved_state across a Live Update.
The main motivation here is Live Update. VFIO snapshots a device's state with pci_store_saved_state() when the device is first opened and restores it with pci_load_and_free_saved_state() when it is last closed. A Live Update can happen in between, so VFIO needs to pass this state to the next kernel.
This is not safe today because the layout of struct pci_saved_state is defined by the kernel that produced it: which capabilities get a record, how large each record is, and what each word in it means. Nothing ties a saved value back to the register it came from, so preserving it as is would turn the private layout of every capability save buffer into Live Update ABI [1]. Indexing saved values by configuration space offset makes the layout a property of the hardware rather than of the kernel version, so it can be preserved as a bitmap of saved DWORDs plus their values, and the next kernel can decide per register what to restore.
[1] https://lore.kernel.org/linux-pci/[email protected]/
The same change also makes saving and restoring capabilities simpler and more robust:
- Capability save buffers were allocated at ten sites (plus one hand-built by a quirk) across three phases of a device's life, and four of them (AER, DPC, PTM, TPH) ignored allocation failure, so the capability silently went unsaved (patch 1 fixes DPC). Now each device has exactly one allocation, and its failure is reported.
- Each capability's save and restore routines had to walk the same registers in the same order through a buffer cursor, and code elsewhere had to know which slot held which register. Now saving is a single pass over a bitmap of reserved registers, each restore routine names its registers by offset, and the common logic lives in one small helper library (drivers/pci/saved-caps.c).
Patches 1-16 are roughly net-neutral in size. Outside of the new helper library (half of which is comments), this series removes about 250 lines from the PCI core.
Series Overview
---------------
This series implements the following:
1. Allocate the DPC save buffer during device setup instead of when the portdrv DPC service binds, so that every capability is set up in the same phase, and a reset before the service binds no longer loses the DPC Control register (patch 1).
2. Add the per-device store. Capabilities reserve registers by offset during device setup, and the values are allocated in one array once every capability has been initialized (patch 2).
3. Lay out struct pci_saved_state as a bitmap of saved DWORDs plus their values (patch 3).
4. Convert PCIe, PCI-X, LTR, L1SS, AER, PTM, TPH, DPC, ACS and VC to the store, one capability at a time (patches 4-14).
5. Replace the per-capability save functions with a single pass over the reservation bitmap (patch 15).
6. Remove struct pci_cap_saved_state and the code that managed it
(patch 16).
7. Add helpers that preserve a struct pci_saved_state in KHO memory for the next kernel, which callers reference by its physical address (patch 17).
Design Notes
------------
- The store only covers capability registers. The configuration space header is still saved in pci_dev.saved_config_space.
- Registers are saved a DWORD at a time, since configuration space reads have no side effects, but restored at their own width, since a neighbouring register in the same DWORD may be RW1C (e.g. PCI_EXP_DEVCTL and PCI_EXP_DEVSTA).
- struct pci_saved_state describes a device's complete saved state at every commit, so a bisect landing in the middle of the series does not silently lose state in pci_load_saved_state().
- PCIe capability registers are now saved with raw DWORD reads instead of pcie_capability_read_word(), which returns 0 for registers the device does not implement. Restore still goes through pcie_capability_write_word(), which skips them.
- The reservation bitmap is embedded in struct pci_dev so that reserving cannot fail. This grows struct pci_dev by 136 bytes on x86_64 (2448 to 2584), which stays in the same kmalloc-4k bucket. Removing the per-capability allocations, each with a 24-byte header, saves 32 to 160 bytes of slab per device. The blob from pci_store_saved_state() grows (e.g. from 192 to 256 bytes for a typical endpoint), since it now carries a 128-byte bitmap.
- Along the way, quirk_intel_qat_vf_cap() no longer builds a PCIe save buffer by hand, and pci_load_saved_state() no longer performs potentially unaligned 32-bit reads of packed records.
Merge Plan
----------
This series applies on top of v10 of the PCI core support for Live Update series [2], which is targeting Linux 7.5. This series is targeting Linux 7.5 as well.
Both series will be merged through liveupdate.git, matching the agreed-upon tree in the MAINTAINERS entry for PCI LIVE UPDATE.
[2] https://lore.kernel.org/linux-pci/[email protected]/
Testing
-------
All patches were build-tested individually. The series was tested using the VFIO selftests, which reset devices many times and so exercise the PCI core's save and restore paths. The tests were run in a QEMU VM using an emulated igb device, and on bare metal using an Intel DSA device on an Intel server.
To check that the series does not change what gets restored, every PCI config write made by the kernel with and without this series was traced in QEMU (pci_cfg_write trace event) while performing FLR, PM and VF resets, runtime suspend/resume under vfio-pci, and the VFIO selftests on an igb device below a PCIe Root Port, and while resetting a PCIe switch Downstream Port. The writes to all of these devices were identical. Of the capabilities this series touches, that setup covers PCIe and AER.
The series was also build-tested on x86_64, i386, arm64 and ppc64le, with gcc and clang at W=1, with sparse, with CONFIG_PCI_LIVEUPDATE enabled and disabled, and with each of CONFIG_PCIEAER, CONFIG_PCIE_DPC, CONFIG_PCIE_PTM, CONFIG_PCIEASPM and CONFIG_PCI_IOV disabled.
The helpers in patch 17 were tested across a Live Update selftests (not yet posted), extended so that the test driver preserves each device's struct pci_saved_state along with the device. On x86_64 and arm64 in QEMU, for every tested topology, the next kernel got back the state that was preserved, dropped a saved DWORD that it does not reserve, and restoring the state wrote back a register that had been changed before the Live Update. The full set of patches used for testing can be found on GitHub:
https://github.com/dmatlack/linux/tree/liveupdate/pci/savedstate/v2-test
Future Work
-----------
The next version of the VFIO PCI Live Update series will use the new helpers to preserve the state that VFIO captures with pci_store_saved_state(), in place of [PATCH v6 05/27] [3]. Please see the PCI core Live Update Roadmap for the rest of the planned work:
https://lore.kernel.org/linux-pci/[email protected]/
[3] https://lore.kernel.org/linux-pci/[email protected]/
Separately, the one remaining saved state allocation could be made fatal to device setup. pci_device_add() returns void today, so a device whose saved state could not be allocated is still added.
AI Assistance
-------------
This series was developed with the help of an LLM-based coding assistant, which was used to draft code and commit messages and to review the series. I reviewed every change and take full responsibility for all of it.
Changelog
---------
v2:
- Rebase onto v10 of the PCI core support for Live Update series, and target liveupdate.git.
- Add patch 12 to convert the ACS save buffer added by the PCI core support for Live Update series to the store.
- Add patch 17 with helpers to preserve struct pci_saved_state across Live Update, replacing [PATCH v6 05/27] of the VFIO PCI Live Update series [3] (Vipin, Bjorn).
- Drop the Assisted-by tags, which upstream no longer asks for, and describe the use of AI tools in the cover letter instead.
- Add pci_saved_cap_shift() so the code reading and writing 16-bit saved registers shares one shift calculation (Josh).
- Move the rename of pci_allocate_cap_save_buffers() to pci_reserve_saved_caps() from patch 16 to patch 14, where it stops allocating, and mention ACS and VC in the comment at its call site (Josh).
- Fix inaccuracies in the commit messages of patches 2, 7 and 15 (Josh).
v1: https://lore.kernel.org/linux-pci/[email protected]/
David Matlack (17):
PCI/DPC: Allocate the DPC save buffer during device setup
PCI: Add an offset-indexed store for saved capability registers
PCI: Lay out struct pci_saved_state by configuration space offset
PCI: Save PCIe state in the saved capability store
PCI: Save PCI-X state in the saved capability store PCI/ASPM: Save LTR state in the saved capability store PCI/ASPM: Save L1SS state in the saved capability store PCI/AER: Save AER state in the saved capability store PCI/PTM: Save PTM state in the saved capability store PCI/TPH: Save TPH state in the saved capability store PCI/DPC: Save DPC state in the saved capability store
PCI: Save ACS state in the saved capability store PCI/VC: Split the VC Resource Control restore into a helper PCI/VC: Save VC state in the saved capability store
PCI: Save reserved capability registers in a single pass
PCI: Remove the per-capability save buffers
PCI: liveupdate: Add helpers to preserve struct pci_saved_state
Documentation/PCI/liveupdate.rst | 3 + drivers/pci/Makefile | 3 +- drivers/pci/liveupdate.c | 133 ++++++++++- drivers/pci/pci.c | 383 +++++++++++-------------------- drivers/pci/pci.h | 59 ++--- drivers/pci/pcie/aer.c | 80 +++---- drivers/pci/pcie/aspm.c | 111 ++++----- drivers/pci/pcie/dpc.c | 41 ++-- drivers/pci/pcie/ptm.c | 42 ++-- drivers/pci/probe.c | 9 +- drivers/pci/quirks.c | 30 +-- drivers/pci/saved-caps.c | 308 +++++++++++++++++++++++++ drivers/pci/tph.c | 68 ++---- drivers/pci/vc.c | 272 +++++++++++----------- include/linux/kho/abi/pci.h | 32 +++ include/linux/pci.h | 31 ++- include/linux/pci_liveupdate.h | 23 ++ 17 files changed, 959 insertions(+), 669 deletions(-) create mode 100644 drivers/pci/saved-caps.c
base-commit: 312200e9211ede3f9a03cc17d094805e06e4674e
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 1/17 PCI/DPC: Allocate the DPC save buffer during device setup
Allocate the DPC capability's save buffer in pci_dpc_init() rather than in dpc_probe(), and report an allocation failure, which dpc_probe() ignored.
dpc_probe() runs when the portdrv DPC service binds to a port, which is long after the device has been added, and does not happen at all if the service is not enabled. Until then pci_save_dpc_state() silently saves nothing, so a reset in that window loses the DPC Control register.
Allocating during device setup makes buffer existence depend only on the capability being present, which is what the save and restore paths already assume. It also puts every capability save buffer in one phase of a device's life, which a later patch relies on.
--- drivers/pci/pcie/dpc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pcie/dpc.c +3 -1
@@ -408,6 +408,9 @@ void pci_dpc_init(struct pci_dev *pdev)if (!pdev->dpc_cap)return ; +if (pci_add_ext_cap_save_buffer (pdev,PCI_EXT_CAP_ID_DPC ,sizeof (u16))) +pci_err (pdev,"unable to allocate suspend buffer for DPC\n" ); +pci_read_config_word (pdev, pdev->dpc_cap +PCI_EXP_DPC_CAP , &cap);if (!(cap &PCI_EXP_DPC_CAP_RP_EXT ))return ;
@@ -499,7 +502,6 @@ static int dpc_probe(struct pcie_device *dev)FLAG (cap,PCI_EXP_DPC_CAP_SW_TRIGGER ), pdev->dpc_rp_log_size, FLAG(cap, PCI_EXP_DPC_CAP_DL_ACTIVE)); -pci_add_ext_cap_save_buffer (pdev,PCI_EXT_CAP_ID_DPC ,sizeof (u16));return status; }
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 2/17 PCI: Add an offset-indexed store for saved capability registers
In reply to David MatlackAdd a per-device store for saved capability registers that is indexed by the offset of the register in configuration space, and whose values are allocated in one piece during device setup.
The store holds capability registers only. The configuration space header continues to be saved in pci_dev.saved_config_space, which is embedded in struct pci_dev so that saving it cannot fail.
Today each capability that has state to preserve across a reset owns a separate save buffer, allocated by pci_add_cap_save_buffer() and found by capability ID. The layout of each buffer is private to the code that saves and restores that capability: a dense array of whichever registers that code happens to save, in whichever order it saves them.
That has a few consequences:
- Each capability allocates its own buffer, at ten call sites (plus one hand-built by a quirk) that can each fail independently. Three of them (AER, PTM, TPH) ignore the failure, and the save paths for those capabilities return quietly when the buffer is missing, so a failed allocation leaves the capability unsaved without any message.
- The layout is defined by the kernel rather than by the device, so it changes whenever the core starts or stops saving a register.
- Nothing ties a saved value back to the register it came from, which makes the buffers unusable outside the kernel version that produced them.
Reserve registers by configuration space offset during device setup instead, and allocate the values in a single array once every capability has been initialized. Reserving a register sets a bit in a per-device bitmap indexed by DWORD, so a register's position in the store is derived from the device's own layout, and "was this register saved?" is a property of the store rather than of which buffers happened to be allocated.
Embed the bitmap in struct pci_dev rather than allocating it. Every device needs one during enumeration, so allocating it separately saves nothing and only adds a failure path that every caller has to tolerate. The values remain the one allocation, since their number is not known until enumeration is complete.
Save registers a DWORD at a time, since configuration space reads have no side effects, but restore them at their own width: neighbouring registers within a DWORD may be RW1C or may not tolerate being written, e.g. PCI_EXP_DEVCTL and PCI_EXP_DEVSTA share a DWORD.
Offsets and sizes can come from the device, so refuse a reservation that runs past the end of configuration space without a WARN and let the caller report it. WARN only on misuse that indicates a kernel bug, such as restoring a register that was never reserved.
No capability uses this yet. Subsequent patches convert them one at a time, after which the per-capability buffers are removed.
--- drivers/pci/Makefile | 3 +- drivers/pci/pci.h | 12 ++ drivers/pci/probe.c | 4 + drivers/pci/saved-caps.c | 273 +++++++++++++++++++++++++++++++++++++++ include/linux/pci.h | 30 +++++ 5 files changed, 321 insertions(+), 1 deletion(-) create mode 100644 drivers/pci/saved-caps.c
drivers/pci/Makefile +2 -1
@@ -5,7 +5,8 @@ obj-$(CONFIG_PCI) += access.o bus.o probe.o host-bridge.o \ remove.o pci.o pci-driver.o search.o \ rebar.o rom.o setup-res.o irq.o vpd.o \ - setup-bus.o vc.o mmap.o devres.o + setup-bus.o vc.o mmap.o devres.o \ + saved-caps.o obj-$(CONFIG_PCI) += msi/ obj-$(CONFIG_PCI) += pcie/
drivers/pci/pci.h +12 -0
@@ -258,6 +258,18 @@ struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap);struct pci_cap_saved_state *pci_find_saved_ext_cap (struct pci_dev *dev,u16 cap); +void pci_saved_caps_finalize (struct pci_dev *dev); +void pci_saved_caps_release (struct pci_dev *dev); +int pci_reserve_saved_cap (struct pci_dev *dev,unsigned int off,unsigned int len); +bool pci_saved_cap_reserved (struct pci_dev *dev,unsigned int off); +void pci_save_cap_word (struct pci_dev *dev,unsigned int off); +void pci_save_cap_dword (struct pci_dev *dev,unsigned int off); +void pci_restore_cap_word (struct pci_dev *dev,unsigned int off); +void pci_restore_cap_dword (struct pci_dev *dev,unsigned int off); +bool pci_read_saved_cap_word (struct pci_dev *dev,unsigned int off,u16 *val); +bool pci_read_saved_cap_dword (struct pci_dev *dev,unsigned int off,u32 *val); +void pci_write_saved_cap_word (struct pci_dev *dev,unsigned int off,u16 val); +#define PCI_PM_D2_DELAY 200/* usec; see PCIe r4.0, sec 5.9.1 */ #define PCI_PM_D3HOT_WAIT 10/* msec */ #define PCI_PM_D3COLD_WAIT 100/* msec */
drivers/pci/probe.c +4 -0
@@ -2476,6 +2476,7 @@ static void pci_release_capabilities(struct pci_dev *dev)pci_rcec_exit (dev);pci_iov_release (dev);pci_free_cap_save_buffers (dev); +pci_saved_caps_release (dev); }/**
@@ -2685,6 +2686,9 @@ static void pci_init_capabilities(struct pci_dev *dev)pcie_report_downtraining (dev);pci_init_reset_methods (dev); + +/* No capability may reserve saved registers after this */ +pci_saved_caps_finalize (dev); }/*
drivers/pci/saved-caps.c new file +273 -0
273 lines not on this page · raw message
include/linux/pci.h +30 -0
@@ -334,6 +334,35 @@ struct pci_vpd {u8 cap; }; +/* +* Reservations are tracked for the largest configuration space a device can +* have, since quirks can still change pci_dev.cfg_size after the device has +* been enumerated. +*/ +#define PCI_CFG_SPACE_EXP_DWORDS (PCI_CFG_SPACE_EXP_SIZE / sizeof(u32)) + +/** +* struct pci_saved_caps - a device's saved capability registers +* @nr_dwords: number of DWORDs reserved, i.e. entries in @dword_val +* @finalized: set once @dword_val has been allocated, after which nothing +* may be reserved +* @incomplete: set if the device described a register that does not fit +* in its configuration space, so the reservation was refused +* @dword_val: saved values, in ascending configuration space offset order +* @dword_map: one bit per DWORD of configuration space, set if that DWORD +* has a slot in @dword_val +* +* Private to the PCI core, defined here only so that it can be embedded in +* struct pci_dev. Use the pci_{save,restore}_cap_*() accessors. +*/ +struct pci_saved_caps { +u16 nr_dwords; +bool finalized; +bool incomplete; +u32 *dword_val; +unsigned long dword_map[BITS_TO_LONGS (PCI_CFG_SPACE_EXP_DWORDS )]; +}; +struct irq_affinity ;struct pcie_bwctrl_data ;struct pcie_link_state ;
@@ -521,6 +550,7 @@ struct pci_dev {spinlock_t pcie_cap_lock;/* Protects RMW ops in capability accessors */ u32 saved_config_space[16 ];/* Config space saved at suspend time */ struct hlist_head saved_cap_space; +struct pci_saved_caps saved_caps;/* Saved capability registers */ #ifdef CONFIG_HOTPLUG_PCI_PCIE unsigned int broken_cmd_compl:1; /* No compl for some cmds */
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 3/17 PCI: Lay out struct pci_saved_state by configuration space offset
In reply to David MatlackStore a device's saved state as a bitmap of the configuration space DWORDs that were saved plus their values, instead of a copy of the configuration space header followed by a list of pci_cap_saved_data records.
The old layout is described entirely by the kernel: which capabilities have a record, how large each record is, and what each word within a record means are all properties of the code that happens to be saving them. That ties the blob to the kernel that produced it, so VFIO cannot carry a device's saved state across a Live Update. The new layout is described by the device: a value's position is the offset of its register in the device's own configuration space.
Keep refusing state that the device has nowhere to put in pci_load_saved_state(), the equivalent of the old check that the capability existed and its record was the expected size. Use pci_saved_cap_reserved() for the check, since state saved from another device or kernel can legitimately name an offset that this device did not reserve.
No capability uses the store yet, and the patches that follow move them into it one at a time, so keep appending the records of the capabilities that still have their own buffer after the saved DWORDs. The blob therefore continues to describe all of a device's saved state at every step; a later patch drops the records once there are none left.
struct pci_saved_state is opaque to everything outside drivers/pci, so no caller needs to change.
--- drivers/pci/pci.c | 89 +++++++++++++++++++++++++++++++++++----- drivers/pci/pci.h | 4 ++ drivers/pci/saved-caps.c | 14 +++++++ 3 files changed, 96 insertions(+), 11 deletions(-)
drivers/pci/pci.c +78 -11
@@ -2032,11 +2032,40 @@ void pci_restore_state(struct pci_dev *dev) }EXPORT_SYMBOL (pci_restore_state); +/** +* struct pci_saved_state - a device's saved configuration space +* @dword_map: one bit per DWORD of configuration space, set if that DWORD +* was saved +* @dword_val: saved values, in ascending configuration space offset order +* +* Laid out so that the position of every value is described by the device's +* configuration space rather than by the kernel. +* +* Capabilities that have not moved to the saved capability store yet keep +* their own buffer, and their records trail @dword_val until the last of +* those buffers goes away. Both areas vary in length and only one of them +* can be a flexible array member, so the records are reached with +* pci_saved_state_records() rather than declared here. +*/ struct pci_saved_state { -u32 config_space[16 ]; -struct pci_cap_saved_data cap[]; +unsigned long dword_map[BITS_TO_LONGS (PCI_CFG_SPACE_EXP_DWORDS )]; +u32 dword_val[]; +/* struct pci_cap_saved_data cap[] follows dword_val */ }; +/* +* The records that follow the saved DWORDs, one per capability that still has +* its own save buffer, terminated by an empty record. +*/ +static struct pci_cap_saved_data * +pci_saved_state_records (struct pci_saved_state *state) +{ +unsigned int nr_dwords =bitmap_weight (state->dword_map, +PCI_CFG_SPACE_EXP_DWORDS ); + +return (struct pci_cap_saved_data *)&state->dword_val[nr_dwords]; +} +/** * pci_store_saved_state - Allocate and return an opaque struct containing * the device saved state.
@@ -2046,16 +2075,28 @@ struct pci_saved_state {*/ struct pci_saved_state *pci_store_saved_state (struct pci_dev *dev) { -struct pci_saved_state *state; +DECLARE_BITMAP (map,PCI_CFG_SPACE_EXP_DWORDS ); +struct pci_saved_caps *caps = &dev->saved_caps;struct pci_cap_saved_state *tmp;struct pci_cap_saved_data *cap; +struct pci_saved_state *state; +unsigned int dword, nr_dwords, i =0 ;size_t size;if (!dev->state_saved)return NULL ; - size =sizeof (*state) +sizeof (struct pci_cap_saved_data ); +bitmap_zero (map,PCI_CFG_SPACE_EXP_DWORDS ); +bitmap_set (map,0 ,PCI_STD_HEADER_DWORDS ); +if (caps->dword_val) +bitmap_or (map, map, caps->dword_map,PCI_CFG_SPACE_EXP_DWORDS ); + + nr_dwords =bitmap_weight (map,PCI_CFG_SPACE_EXP_DWORDS ); + + size =struct_size (state, dword_val, nr_dwords); +/* Room for the buffers that are left, and for the terminator */ + size +=sizeof (struct pci_cap_saved_data );hlist_for_each_entry (tmp, &dev->saved_cap_space, next) size +=sizeof (struct pci_cap_saved_data ) + tmp->cap.size;
@@ -2063,16 +2104,25 @@ struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)if (!state)return NULL ; -memcpy (state->config_space, dev->saved_config_space, -sizeof (state->config_space)); +bitmap_copy (state->dword_map, map,PCI_CFG_SPACE_EXP_DWORDS ); - cap = state->cap; +for_each_set_bit (dword, state->dword_map,PCI_CFG_SPACE_EXP_DWORDS ) { +u32 *val = &state->dword_val[i++]; + +if (dword <PCI_STD_HEADER_DWORDS ) + *val = dev->saved_config_space[dword]; +else +pci_read_saved_cap_dword (dev, dword *sizeof (u32), val); + } + + cap =pci_saved_state_records (state);hlist_for_each_entry (tmp, &dev->saved_cap_space, next) {size_t len =sizeof (struct pci_cap_saved_data ) + tmp->cap.size; +memcpy (cap, &tmp->cap, len); cap = (struct pci_cap_saved_data *)((u8 *)cap + len); } -/* Empty cap_save terminates list */ +/* Empty record terminates the list */ return state; }
@@ -2087,16 +2137,33 @@ int pci_load_saved_state(struct pci_dev *dev, struct pci_saved_state *state) {struct pci_cap_saved_data *cap; +unsigned int dword, i =0 ; dev->state_saved = false;if (!state)return 0 ; -memcpy (dev->saved_config_space, state->config_space, -sizeof (state->config_space)); +for_each_set_bit (dword, state->dword_map,PCI_CFG_SPACE_EXP_DWORDS ) { +unsigned int off = dword *sizeof (u32); +u32 val = state->dword_val[i++]; + +if (dword <PCI_STD_HEADER_DWORDS ) { + dev->saved_config_space[dword] = val; +continue ; + } + +/* +* Refuse state that the device has nowhere to put, e.g. +* because it was saved from a different device. +*/ +if (!pci_saved_cap_reserved (dev, off)) +return -EINVAL ; + +pci_write_saved_cap_dword (dev, off, val); + } - cap = state->cap; + cap =pci_saved_state_records (state);while (cap->size) {struct pci_cap_saved_state *tmp;
drivers/pci/pci.h +4 -0
@@ -258,6 +258,9 @@ struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap);struct pci_cap_saved_state *pci_find_saved_ext_cap (struct pci_dev *dev,u16 cap); +/* DWORDs of the configuration space header, i.e. pci_dev.saved_config_space */ +#define PCI_STD_HEADER_DWORDS (PCI_STD_HEADER_SIZEOF / sizeof(u32)) +void pci_saved_caps_finalize (struct pci_dev *dev);void pci_saved_caps_release (struct pci_dev *dev);int pci_reserve_saved_cap (struct pci_dev *dev,unsigned int off,unsigned int len);
@@ -269,6 +272,7 @@ void pci_restore_cap_dword(struct pci_dev *dev, unsigned int off);bool pci_read_saved_cap_word (struct pci_dev *dev,unsigned int off,u16 *val);bool pci_read_saved_cap_dword (struct pci_dev *dev,unsigned int off,u32 *val);void pci_write_saved_cap_word (struct pci_dev *dev,unsigned int off,u16 val); +void pci_write_saved_cap_dword (struct pci_dev *dev,unsigned int off,u32 val);#define PCI_PM_D2_DELAY 200/* usec; see PCIe r4.0, sec 5.9.1 */ #define PCI_PM_D3HOT_WAIT 10/* msec */
drivers/pci/saved-caps.c +14 -0
@@ -211,6 +211,20 @@ void pci_write_saved_cap_word(struct pci_dev *dev, unsigned int off, u16 val) *slot |= (u32 )val << shift; } +/** +* pci_write_saved_cap_dword - change the saved value of a 32-bit register +* @dev: the PCI device +* @off: offset of the register in configuration space +* @val: value to save +*/ +void pci_write_saved_cap_dword (struct pci_dev *dev,unsigned int off,u32 val) +{ +u32 *slot =pci_saved_cap_slot (dev, off); + +if (slot) + *slot = val; +} +/** * pci_restore_cap_dword - restore a 32-bit capability register * @dev: the PCI device
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 4/17 PCI: Save PCIe state in the saved capability store
In reply to David MatlackSave and restore the PCIe capability control registers through the per-device saved capability store instead of a private pci_cap_saved_state buffer.
The private buffer is an opaque array of u16s whose layout is a contract between pci_save_pcie_state() and pci_restore_pcie_state(). Anything else that needs to touch the saved state has to know that contract, e.g. pci_update_aspm_saved_state() patches the saved Link Control register with a comment reminding the reader that cap[1] is LNKCTL. Indexing the saved state by configuration space offset removes the coupling.
Describe the registers to save in pcie_saved_regs[] and drive the reservation, save, and restore loops from it so that the three can no longer disagree about which register is where. Reserve the registers in pci_reserve_pcie_state(), named to match pci_save_pcie_state() and pci_restore_pcie_state().
Reserve the registers based on pci_is_pcie() rather than pci_find_capability(PCI_CAP_ID_EXP), so that a device whose capability list is broken but whose pcie_cap a quirk has fixed up gets saved state like any other PCIe device. Drop the save buffer that quirk_intel_qat_vf_cap() builds by hand, since it is no longer needed.
Keep using pcie_capability_read_word() and pcie_capability_write_word() to access the hardware so that registers the device does not implement are skipped. Reserve space for all of the registers regardless; that is harmless, since an unimplemented register reads back as zero and is never written.
Make pci_save_pcie_state() void, so pci_save_state() no longer fails with -ENOMEM when the PCIe save buffer is missing. A failure to allocate the store is reported once during device setup instead.
No other functional change intended.
--- drivers/pci/pci.c | 92 +++++++++++++++++++++++------------------ drivers/pci/pcie/aspm.c | 16 +++---- drivers/pci/quirks.c | 30 +------------- 3 files changed, 59 insertions(+), 79 deletions(-)
drivers/pci/pci.c +52 -40
@@ -1795,7 +1795,20 @@ int pci_set_power_state_locked(struct pci_dev *dev, pci_power_t state) }EXPORT_SYMBOL (pci_set_power_state_locked); -#define PCI_EXP_SAVE_REGS 7 +/* +* PCIe capability registers saved and restored by the PCI core. Registers a +* device does not implement are skipped by the pcie_capability_*() accessors, +* so space is reserved for all of them regardless of the device. +*/ +static const u16 pcie_saved_regs[] = { +PCI_EXP_DEVCTL , +PCI_EXP_LNKCTL , +PCI_EXP_SLTCTL , +PCI_EXP_RTCTL , +PCI_EXP_DEVCTL2 , +PCI_EXP_LNKCTL2 , +PCI_EXP_SLTCTL2 , +};static struct pci_cap_saved_state *_pci_find_saved_cap (struct pci_dev *pci_dev,u16 cap,bool extended)
@@ -1819,41 +1832,47 @@ struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)return _pci_find_saved_cap (dev, cap, true); } -static int pci_save_pcie_state (struct pci_dev *dev) +static void pci_save_pcie_state (struct pci_dev *dev) { -int i =0 ; -struct pci_cap_saved_state *save_state; -u16 *cap; +unsigned int i; +u16 val;if (!pci_is_pcie (dev)) -return 0 ; +return ; - save_state =pci_find_saved_cap (dev,PCI_CAP_ID_EXP ); -if (!save_state) { -pci_err (dev,"buffer not found in %s\n" , __func__); -return -ENOMEM ; +for (i =0 ; i <ARRAY_SIZE (pcie_saved_regs); i++) { +pcie_capability_read_word (dev, pcie_saved_regs[i], &val); +pci_write_saved_cap_word (dev, dev->pcie_cap + pcie_saved_regs[i], + val); } - cap = (u16 *)&save_state->cap.data[0 ]; -pcie_capability_read_word (dev,PCI_EXP_DEVCTL , &cap[i++]); -pcie_capability_read_word (dev,PCI_EXP_LNKCTL , &cap[i++]); -pcie_capability_read_word (dev,PCI_EXP_SLTCTL , &cap[i++]); -pcie_capability_read_word (dev,PCI_EXP_RTCTL , &cap[i++]); -pcie_capability_read_word (dev,PCI_EXP_DEVCTL2 , &cap[i++]); -pcie_capability_read_word (dev,PCI_EXP_LNKCTL2 , &cap[i++]); -pcie_capability_read_word (dev,PCI_EXP_SLTCTL2 , &cap[i++]); -pci_save_aspm_l1ss_state (dev);pci_save_ltr_state (dev); +} -return 0 ; +static void pci_reserve_pcie_state (struct pci_dev *dev) +{ +unsigned int i; +int rc; + +if (!pci_is_pcie (dev)) +return ; + +for (i =0 ; i <ARRAY_SIZE (pcie_saved_regs); i++) { +unsigned int off = dev->pcie_cap + pcie_saved_regs[i]; + + rc =pci_reserve_saved_cap (dev, off,sizeof (u16)); +if (rc) { +pci_err (dev,"unable to reserve PCI Express save state (%pe)\n" , +ERR_PTR (rc)); +return ; + } + } }static void pci_restore_pcie_state (struct pci_dev *dev) { -int i =0 ; -struct pci_cap_saved_state *save_state; -u16 *cap; +unsigned int i;/* * Restore max latencies (in the LTR capability) before enabling
@@ -1862,8 +1881,7 @@ static void pci_restore_pcie_state(struct pci_dev *dev)pci_restore_ltr_state (dev);pci_restore_aspm_l1ss_state (dev); - save_state =pci_find_saved_cap (dev,PCI_CAP_ID_EXP ); -if (!save_state) +if (!pci_is_pcie (dev))return ;/*
@@ -1873,14 +1891,13 @@ static void pci_restore_pcie_state(struct pci_dev *dev)*/ pci_bridge_reconfigure_ltr (dev); - cap = (u16 *)&save_state->cap.data[0 ]; -pcie_capability_write_word (dev,PCI_EXP_DEVCTL , cap[i++]); -pcie_capability_write_word (dev,PCI_EXP_LNKCTL , cap[i++]); -pcie_capability_write_word (dev,PCI_EXP_SLTCTL , cap[i++]); -pcie_capability_write_word (dev,PCI_EXP_RTCTL , cap[i++]); -pcie_capability_write_word (dev,PCI_EXP_DEVCTL2 , cap[i++]); -pcie_capability_write_word (dev,PCI_EXP_LNKCTL2 , cap[i++]); -pcie_capability_write_word (dev,PCI_EXP_SLTCTL2 , cap[i++]); +for (i =0 ; i <ARRAY_SIZE (pcie_saved_regs); i++) { +u16 reg = pcie_saved_regs[i]; +u16 val; + +if (pci_read_saved_cap_word (dev, dev->pcie_cap + reg, &val)) +pcie_capability_write_word (dev, reg, val); + } }static int pci_save_pcix_state (struct pci_dev *dev)
@@ -1935,9 +1952,7 @@ int pci_save_state(struct pci_dev *dev) } dev->state_saved = true; - i =pci_save_pcie_state (dev); -if (i !=0 ) -return i; +pci_save_pcie_state (dev); i =pci_save_pcix_state (dev);if (i !=0 )
@@ -3733,10 +3748,7 @@ void pci_allocate_cap_save_buffers(struct pci_dev *dev) {int error; - error =pci_add_cap_save_buffer (dev,PCI_CAP_ID_EXP , -PCI_EXP_SAVE_REGS *sizeof (u16)); -if (error) -pci_err (dev,"unable to preallocate PCI Express save buffer\n" ); +pci_reserve_pcie_state (dev); error =pci_add_cap_save_buffer (dev,PCI_CAP_ID_PCIX ,sizeof (u16));if (error)
drivers/pci/pcie/aspm.c +5 -11
@@ -324,26 +324,20 @@ static int policy_to_clkpm_state(struct pcie_link_state *link)static void pci_update_aspm_saved_state (struct pci_dev *dev) { -struct pci_cap_saved_state *save_state; -u16 *cap, lnkctl, aspm_ctl; - - save_state =pci_find_saved_cap (dev,PCI_CAP_ID_EXP ); -if (!save_state) -return ; +u16 lnkctl, aspm_ctl;pcie_capability_read_word (dev,PCI_EXP_LNKCTL , &lnkctl);/* -* Update ASPM and CLKREQ bits of LNKCTL in save_state. We only +* Update ASPM and CLKREQ bits of LNKCTL in the saved state. We only * write PCI_EXP_LNKCTL_CCC during enumeration, so it shouldn't -* change after being captured in save_state. +* change after being captured in the saved state. */ aspm_ctl = lnkctl & (PCI_EXP_LNKCTL_ASPMC |PCI_EXP_LNKCTL_CLKREQ_EN ); lnkctl &= ~(PCI_EXP_LNKCTL_ASPMC |PCI_EXP_LNKCTL_CLKREQ_EN ); -/* Depends on pci_save_pcie_state(): cap[1] is LNKCTL */ - cap = (u16 *)&save_state->cap.data[0 ]; - cap[1 ] = lnkctl | aspm_ctl; +pci_write_saved_cap_word (dev, dev->pcie_cap +PCI_EXP_LNKCTL , + lnkctl | aspm_ctl); }static void pcie_set_clkpm_nocheck (struct pcie_link_state *link,int enable)
drivers/pci/quirks.c +2 -28
@@ -5512,10 +5512,9 @@ int pci_dev_specific_disable_acs_redir(struct pci_dev *dev)*/ static void quirk_intel_qat_vf_cap (struct pci_dev *pdev) { -int pos, i =0 , ret; +int pos, ret;u8 next_cap; -u16 reg16, *cap; -struct pci_cap_saved_state *state; +u16 reg16;/* Bail if the hardware bug is fixed */ if (pdev->pcie_cap ||pci_find_capability (pdev,PCI_CAP_ID_EXP ))
@@ -5546,10 +5545,6 @@ static void quirk_intel_qat_vf_cap(struct pci_dev *pdev)pci_read_config_word (pdev, pos, ®16);if (reg16 == (0x0000 |PCI_CAP_ID_EXP )) {u32 status; -#ifndef PCI_EXP_SAVE_REGS -#define PCI_EXP_SAVE_REGS 7 -#endif -int size =PCI_EXP_SAVE_REGS *sizeof (u16); pdev->pcie_cap = pos;pci_read_config_word (pdev, pos +PCI_EXP_FLAGS , ®16);
@@ -5561,27 +5556,6 @@ static void quirk_intel_qat_vf_cap(struct pci_dev *pdev) ret =pci_read_config_dword (pdev,PCI_CFG_SPACE_SIZE , &status);if ((ret !=PCIBIOS_SUCCESSFUL ) || (PCI_POSSIBLE_ERROR (status))) pdev->cfg_size =PCI_CFG_SPACE_SIZE ; - -if (pci_find_saved_cap (pdev,PCI_CAP_ID_EXP )) -return ; - -/* Save PCIe cap */ - state =kzalloc (sizeof (*state) + size,GFP_KERNEL ); -if (!state) -return ; - - state->cap.cap_nr =PCI_CAP_ID_EXP ; - state->cap.cap_extended =0 ; - state->cap.size = size; - cap = (u16 *)&state->cap.data[0 ]; -pcie_capability_read_word (pdev,PCI_EXP_DEVCTL , &cap[i++]); -pcie_capability_read_word (pdev,PCI_EXP_LNKCTL , &cap[i++]); -pcie_capability_read_word (pdev,PCI_EXP_SLTCTL , &cap[i++]); -pcie_capability_read_word (pdev,PCI_EXP_RTCTL , &cap[i++]); -pcie_capability_read_word (pdev,PCI_EXP_DEVCTL2 , &cap[i++]); -pcie_capability_read_word (pdev,PCI_EXP_LNKCTL2 , &cap[i++]); -pcie_capability_read_word (pdev,PCI_EXP_SLTCTL2 , &cap[i++]); -hlist_add_head (&state->next, &pdev->saved_cap_space); } }DECLARE_PCI_FIXUP_EARLY (PCI_VENDOR_ID_INTEL ,0x443 , quirk_intel_qat_vf_cap);
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 5/17 PCI: Save PCI-X state in the saved capability store
In reply to David MatlackSave and restore the PCI-X Command register through the per-device saved capability store instead of a private pci_cap_saved_state buffer.
Reserve the register by its configuration space offset during device setup, in pci_reserve_pcix_state(), so that the save and restore paths no longer have to locate a buffer.
Make pci_save_pcix_state() void, so pci_save_state() no longer fails with -ENOMEM when the PCI-X save buffer is missing. A failure to allocate the store is reported once during device setup instead.
No other functional change intended.
--- drivers/pci/pci.c | 47 ++++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 27 deletions(-)
drivers/pci/pci.c +20 -27
@@ -1900,40 +1900,40 @@ static void pci_restore_pcie_state(struct pci_dev *dev) } } -static int pci_save_pcix_state (struct pci_dev *dev) +static void pci_save_pcix_state (struct pci_dev *dev) {int pos; -struct pci_cap_saved_state *save_state; pos =pci_find_capability (dev,PCI_CAP_ID_PCIX );if (!pos) -return 0 ; +return ; - save_state =pci_find_saved_cap (dev,PCI_CAP_ID_PCIX ); -if (!save_state) { -pci_err (dev,"buffer not found in %s\n" , __func__); -return -ENOMEM ; - } +pci_save_cap_word (dev, pos +PCI_X_CMD ); +} -pci_read_config_word (dev, pos +PCI_X_CMD , - (u16 *)save_state->cap.data); +static void pci_reserve_pcix_state (struct pci_dev *dev) +{ +int pos, rc; -return 0 ; + pos =pci_find_capability (dev,PCI_CAP_ID_PCIX ); +if (!pos) +return ; + + rc =pci_reserve_saved_cap (dev, pos +PCI_X_CMD ,sizeof (u16)); +if (rc) +pci_err (dev,"unable to reserve PCI-X save state (%pe)\n" , +ERR_PTR (rc)); }static void pci_restore_pcix_state (struct pci_dev *dev) { -int i =0 , pos; -struct pci_cap_saved_state *save_state; -u16 *cap; +int pos; - save_state =pci_find_saved_cap (dev,PCI_CAP_ID_PCIX ); pos =pci_find_capability (dev,PCI_CAP_ID_PCIX ); -if (!save_state || !pos) +if (!pos)return ; - cap = (u16 *)&save_state->cap.data[0 ]; -pci_write_config_word (dev, pos +PCI_X_CMD , cap[i++]); +pci_restore_cap_word (dev, pos +PCI_X_CMD ); }/**
@@ -1953,11 +1953,7 @@ int pci_save_state(struct pci_dev *dev) dev->state_saved = true;pci_save_pcie_state (dev); - - i =pci_save_pcix_state (dev); -if (i !=0 ) -return i; - +pci_save_pcix_state (dev);pci_save_dpc_state (dev);pci_save_aer_state (dev);pci_save_ptm_state (dev);
@@ -3749,10 +3745,7 @@ void pci_allocate_cap_save_buffers(struct pci_dev *dev)int error;pci_reserve_pcie_state (dev); - - error =pci_add_cap_save_buffer (dev,PCI_CAP_ID_PCIX ,sizeof (u16)); -if (error) -pci_err (dev,"unable to preallocate PCI-X save buffer\n" ); +pci_reserve_pcix_state (dev); error =pci_add_ext_cap_save_buffer (dev,PCI_EXT_CAP_ID_LTR ,2 *sizeof (u16));
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 6/17 PCI/ASPM: Save LTR state in the saved capability store
In reply to David MatlackSave and restore the LTR maximum latency registers through the per-device saved capability store instead of a private pci_cap_saved_state buffer.
Reserve the DWORD by its configuration space offset during device setup, in pci_reserve_ltr_state(), and keep accessing the registers a DWORD at a time because some devices only support DWORD access to them.
No functional change intended.
--- drivers/pci/pci.c | 8 +------- drivers/pci/pci.h | 1 + drivers/pci/pcie/aspm.c | 31 ++++++++++++++++--------------- 3 files changed, 18 insertions(+), 22 deletions(-)
drivers/pci/pci.c +1 -7
@@ -3742,15 +3742,9 @@ int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)*/ void pci_allocate_cap_save_buffers (struct pci_dev *dev) { -int error; -pci_reserve_pcie_state (dev);pci_reserve_pcix_state (dev); - - error =pci_add_ext_cap_save_buffer (dev,PCI_EXT_CAP_ID_LTR , -2 *sizeof (u16)); -if (error) -pci_err (dev,"unable to allocate suspend buffer for LTR\n" ); +pci_reserve_ltr_state (dev);pci_allocate_acs_save_buffer (dev);pci_allocate_vc_save_buffers (dev);
drivers/pci/pci.h +1 -0
@@ -1151,6 +1151,7 @@ int pcie_retrain_link(struct pci_dev *pdev, bool use_lt);/* ASPM-related functionality we need even without CONFIG_PCIEASPM */ void pci_save_ltr_state (struct pci_dev *dev); +void pci_reserve_ltr_state (struct pci_dev *dev);void pci_restore_ltr_state (struct pci_dev *dev);void pci_configure_aspm_l1ss (struct pci_dev *dev);void pci_save_aspm_l1ss_state (struct pci_dev *dev);
drivers/pci/pcie/aspm.c +16 -15
@@ -30,8 +30,6 @@void pci_save_ltr_state (struct pci_dev *dev) {int ltr; -struct pci_cap_saved_state *save_state; -u32 *cap;if (!pci_is_pcie (dev))return ;
@@ -40,31 +38,34 @@ void pci_save_ltr_state(struct pci_dev *dev)if (!ltr)return ; - save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_LTR ); -if (!save_state) { -pci_err (dev,"no suspend buffer for LTR; ASPM issues possible after resume\n" ); +/* Some broken devices only support dword access to LTR */ +pci_save_cap_dword (dev, ltr +PCI_LTR_MAX_SNOOP_LAT ); +} + +void pci_reserve_ltr_state (struct pci_dev *dev) +{ +int ltr, rc; + + ltr =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_LTR ); +if (!ltr)return ; - } -/* Some broken devices only support dword access to LTR */ - cap = &save_state->cap.data[0 ]; -pci_read_config_dword (dev, ltr +PCI_LTR_MAX_SNOOP_LAT , cap); + rc =pci_reserve_saved_cap (dev, ltr +PCI_LTR_MAX_SNOOP_LAT ,sizeof (u32)); +if (rc) +pci_err (dev,"unable to reserve LTR save state (%pe)\n" , +ERR_PTR (rc)); }void pci_restore_ltr_state (struct pci_dev *dev) { -struct pci_cap_saved_state *save_state;int ltr; -u32 *cap; - save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_LTR ); ltr =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_LTR ); -if (!save_state || !ltr) +if (!ltr)return ;/* Some broken devices only support dword access to LTR */ - cap = &save_state->cap.data[0 ]; -pci_write_config_dword (dev, ltr +PCI_LTR_MAX_SNOOP_LAT , *cap); +pci_restore_cap_dword (dev, ltr +PCI_LTR_MAX_SNOOP_LAT ); }void pci_configure_aspm_l1ss (struct pci_dev *pdev)
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 7/17 PCI/ASPM: Save L1SS state in the saved capability store
In reply to David MatlackSave and restore the L1 PM Substates control registers through the per-device saved capability store instead of a private pci_cap_saved_state buffer.
pci_restore_aspm_l1ss_state() needs both the Upstream and Downstream Port's saved L1SS configuration, and had to fetch two buffers and decode each by index. Read the saved values by configuration space offset instead, which names the register at every use. Add pci_read_saved_l1ss_ctl() to read one device's pair, so that the Port and the device below it are read the same way.
Reserve PCI_L1SS_CTL1 and PCI_L1SS_CTL2 with a single request in pci_reserve_aspm_l1ss_state(), since they are adjacent DWORDs. Skip the reservation when the device has no L1SS capability, which pci_add_ext_cap_save_buffer() did implicitly.
No functional change intended.
--- drivers/pci/pcie/aspm.c | 63 ++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 33 deletions(-)
drivers/pci/pcie/aspm.c +30 -33
@@ -68,24 +68,29 @@ void pci_restore_ltr_state(struct pci_dev *dev)pci_restore_cap_dword (dev, ltr +PCI_LTR_MAX_SNOOP_LAT ); } -void pci_configure_aspm_l1ss (struct pci_dev *pdev) +static void pci_reserve_aspm_l1ss_state (struct pci_dev *pdev) {int rc; - pdev->l1ss =pci_find_ext_capability (pdev,PCI_EXT_CAP_ID_L1SS ); - - rc =pci_add_ext_cap_save_buffer (pdev,PCI_EXT_CAP_ID_L1SS , -2 *sizeof (u32)); +/* PCI_L1SS_CTL1 and PCI_L1SS_CTL2 are adjacent DWORDs */ + rc =pci_reserve_saved_cap (pdev, pdev->l1ss +PCI_L1SS_CTL1 ,2 *sizeof (u32));if (rc) -pci_err (pdev,"unable to allocate ASPM L1SS save buffer (%pe)\n" , +pci_err (pdev,"unable to reserve ASPM L1SS save state (%pe)\n" ,ERR_PTR (rc)); } +void pci_configure_aspm_l1ss (struct pci_dev *pdev) +{ + pdev->l1ss =pci_find_ext_capability (pdev,PCI_EXT_CAP_ID_L1SS ); +if (!pdev->l1ss) +return ; + +pci_reserve_aspm_l1ss_state (pdev); +} +void pci_save_aspm_l1ss_state (struct pci_dev *pdev) {struct pci_dev *parent = pdev->bus->self; -struct pci_cap_saved_state *save_state; -u32 *cap;/* * If this is a Downstream Port, we never restore the L1SS state
@@ -102,32 +107,32 @@ void pci_save_aspm_l1ss_state(struct pci_dev *pdev)* Save L1 substate configuration. The ASPM L0s/L1 configuration * in PCI_EXP_LNKCTL_ASPMC is saved by pci_save_pcie_state(). */ - save_state =pci_find_saved_ext_cap (pdev,PCI_EXT_CAP_ID_L1SS ); -if (!save_state) -return ; - - cap = &save_state->cap.data[0 ]; -pci_read_config_dword (pdev, pdev->l1ss +PCI_L1SS_CTL2 , cap++); -pci_read_config_dword (pdev, pdev->l1ss +PCI_L1SS_CTL1 , cap++); +pci_save_cap_dword (pdev, pdev->l1ss +PCI_L1SS_CTL2 ); +pci_save_cap_dword (pdev, pdev->l1ss +PCI_L1SS_CTL1 );/* * Save parent's L1 substate configuration so we have it for * pci_restore_aspm_l1ss_state(pdev) to restore. */ - save_state =pci_find_saved_ext_cap (parent,PCI_EXT_CAP_ID_L1SS ); -if (!save_state) -return ; +pci_save_cap_dword (parent, parent->l1ss +PCI_L1SS_CTL2 ); +pci_save_cap_dword (parent, parent->l1ss +PCI_L1SS_CTL1 ); +} - cap = &save_state->cap.data[0 ]; -pci_read_config_dword (parent, parent->l1ss +PCI_L1SS_CTL2 , cap++); -pci_read_config_dword (parent, parent->l1ss +PCI_L1SS_CTL1 , cap++); +/* +* Read a device's saved L1 substate configuration. +* +* Return: true if both registers had a saved value, false otherwise. +*/ +static bool pci_read_saved_l1ss_ctl (struct pci_dev *dev,u32 *ctl1,u32 *ctl2) +{ +return pci_read_saved_cap_dword (dev, dev->l1ss +PCI_L1SS_CTL1 , ctl1) && +pci_read_saved_cap_dword (dev, dev->l1ss +PCI_L1SS_CTL2 , ctl2); }void pci_restore_aspm_l1ss_state (struct pci_dev *pdev) { -struct pci_cap_saved_state *pl_save_state, *cl_save_state;struct pci_dev *parent = pdev->bus->self; -u32 *cap, pl_ctl1, pl_ctl2, pl_l1_2_enable; +u32 pl_ctl1, pl_ctl2, pl_l1_2_enable;u32 cl_ctl1, cl_ctl2, cl_l1_2_enable;u16 clnkctl, plnkctl;
@@ -142,18 +147,10 @@ void pci_restore_aspm_l1ss_state(struct pci_dev *pdev)if (!pdev->l1ss || !parent->l1ss)return ; - cl_save_state =pci_find_saved_ext_cap (pdev,PCI_EXT_CAP_ID_L1SS ); - pl_save_state =pci_find_saved_ext_cap (parent,PCI_EXT_CAP_ID_L1SS ); -if (!cl_save_state || !pl_save_state) +if (!pci_read_saved_l1ss_ctl (pdev, &cl_ctl1, &cl_ctl2) || + !pci_read_saved_l1ss_ctl (parent, &pl_ctl1, &pl_ctl2))return ; - cap = &cl_save_state->cap.data[0 ]; - cl_ctl2 = *cap++; - cl_ctl1 = *cap; - cap = &pl_save_state->cap.data[0 ]; - pl_ctl2 = *cap++; - pl_ctl1 = *cap; -/* Make sure L0s/L1 are disabled before updating L1SS config */ pcie_capability_read_word (pdev,PCI_EXP_LNKCTL , &clnkctl);pcie_capability_read_word (parent,PCI_EXP_LNKCTL , &plnkctl);
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 8/17 PCI/AER: Save AER state in the saved capability store
In reply to David MatlackSave and restore the AER mask, severity, capability and root command registers through the per-device saved capability store instead of a private pci_cap_saved_state buffer.
Describe the registers in aer_saved_regs[] and drive the reservation, save, and restore loops from it. Previously the three had to be kept in the same order by hand, and pci_aer_init() encoded the register count as a bare 4 or 5.
Reserve the registers in pci_reserve_aer_state(), called from pci_aer_init(), and report a refused reservation. pci_aer_init() ignored the return value of pci_add_ext_cap_save_buffer(), so a failed allocation left AER state unsaved without any message. An allocation failure is now reported once by pci_saved_caps_finalize().
No other functional change intended.
--- drivers/pci/pcie/aer.c | 85 +++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 39 deletions(-)
drivers/pci/pcie/aer.c +46 -39
@@ -352,54 +352,68 @@ int pci_aer_clear_status(struct pci_dev *dev)return pci_aer_raw_clear_status (dev); } -void pci_save_aer_state (struct pci_dev *dev) +/* +* AER registers saved and restored by the PCI core. PCI_ERR_ROOT_COMMAND is +* only implemented by Root Ports and Root Complex Event Collectors (PCIe +* r6.0, sec 7.8.4.9) and so must come last. +*/ +static const u16 aer_saved_regs[] = { +PCI_ERR_UNCOR_MASK , +PCI_ERR_UNCOR_SEVER , +PCI_ERR_COR_MASK , +PCI_ERR_CAP , +PCI_ERR_ROOT_COMMAND , +}; + +static unsigned int aer_nr_saved_regs (struct pci_dev *dev) { -int aer = dev->aer_cap; -struct pci_cap_saved_state *save_state; -u32 *cap; +if (pcie_cap_has_rtctl (dev)) +return ARRAY_SIZE (aer_saved_regs); -if (!aer) -return ; +return ARRAY_SIZE (aer_saved_regs) -1 ; +} - save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_ERR ); -if (!save_state) +void pci_save_aer_state (struct pci_dev *dev) +{ +unsigned int i; + +if (!dev->aer_cap)return ; - cap = &save_state->cap.data[0 ]; -pci_read_config_dword (dev, aer +PCI_ERR_UNCOR_MASK , cap++); -pci_read_config_dword (dev, aer +PCI_ERR_UNCOR_SEVER , cap++); -pci_read_config_dword (dev, aer +PCI_ERR_COR_MASK , cap++); -pci_read_config_dword (dev, aer +PCI_ERR_CAP , cap++); -if (pcie_cap_has_rtctl (dev)) -pci_read_config_dword (dev, aer +PCI_ERR_ROOT_COMMAND , cap++); +for (i =0 ; i <aer_nr_saved_regs (dev); i++) +pci_save_cap_dword (dev, dev->aer_cap + aer_saved_regs[i]); }void pci_restore_aer_state (struct pci_dev *dev) { -int aer = dev->aer_cap; -struct pci_cap_saved_state *save_state; -u32 *cap; +unsigned int i; -if (!aer) +if (!dev->aer_cap)return ; - save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_ERR ); -if (!save_state) -return ; +for (i =0 ; i <aer_nr_saved_regs (dev); i++) +pci_restore_cap_dword (dev, dev->aer_cap + aer_saved_regs[i]); +} - cap = &save_state->cap.data[0 ]; -pci_write_config_dword (dev, aer +PCI_ERR_UNCOR_MASK , *cap++); -pci_write_config_dword (dev, aer +PCI_ERR_UNCOR_SEVER , *cap++); -pci_write_config_dword (dev, aer +PCI_ERR_COR_MASK , *cap++); -pci_write_config_dword (dev, aer +PCI_ERR_CAP , *cap++); -if (pcie_cap_has_rtctl (dev)) -pci_write_config_dword (dev, aer +PCI_ERR_ROOT_COMMAND , *cap++); +static void pci_reserve_aer_state (struct pci_dev *dev) +{ +unsigned int i; +int rc; + +for (i =0 ; i <aer_nr_saved_regs (dev); i++) { +unsigned int off = dev->aer_cap + aer_saved_regs[i]; + + rc =pci_reserve_saved_cap (dev, off,sizeof (u32)); +if (rc) { +pci_err (dev,"unable to reserve AER save state (%pe)\n" , +ERR_PTR (rc)); +return ; + } + } }void pci_aer_init (struct pci_dev *dev) { -int n; - dev->aer_cap =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_ERR );if (!dev->aer_cap)return ;
@@ -415,14 +429,7 @@ void pci_aer_init(struct pci_dev *dev)ratelimit_state_init (&dev->aer_info->nonfatal_ratelimit,DEFAULT_RATELIMIT_INTERVAL ,DEFAULT_RATELIMIT_BURST ); -/* -* We save/restore PCI_ERR_UNCOR_MASK, PCI_ERR_UNCOR_SEVER, -* PCI_ERR_COR_MASK, and PCI_ERR_CAP. Root and Root Complex Event -* Collectors also implement PCI_ERR_ROOT_COMMAND (PCIe r6.0, sec -* 7.8.4.9). -*/ - n =pcie_cap_has_rtctl (dev) ?5 :4 ; -pci_add_ext_cap_save_buffer (dev,PCI_EXT_CAP_ID_ERR ,sizeof (u32) * n); +pci_reserve_aer_state (dev);/* * Advisory Non-Fatal Errors are masked by default (PCIe r7.0, sec
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 9/17 PCI/PTM: Save PTM state in the saved capability store
In reply to David MatlackSave and restore the PTM Control register through the per-device saved capability store instead of a private pci_cap_saved_state buffer.
Reserve the register in pci_reserve_ptm_state(), called from pci_ptm_init(), and report a refused reservation. pci_ptm_init() ignored the return value of pci_add_ext_cap_save_buffer(), so a failed allocation left PTM state unsaved without any message. An allocation failure is now reported once by pci_saved_caps_finalize().
No other functional change intended.
--- drivers/pci/pcie/ptm.c | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-)
drivers/pci/pcie/ptm.c +15 -23
@@ -34,6 +34,16 @@ static struct pci_dev *pci_upstream_ptm(struct pci_dev *dev)return NULL ; } +static void pci_reserve_ptm_state (struct pci_dev *dev) +{ +int rc; + + rc =pci_reserve_saved_cap (dev, dev->ptm_cap +PCI_PTM_CTRL ,sizeof (u32)); +if (rc) +pci_err (dev,"unable to reserve PTM save state (%pe)\n" , +ERR_PTR (rc)); +} +/* * Find the PTM Capability (if present) and extract the information we need * to use it.
@@ -53,7 +63,7 @@ void pci_ptm_init(struct pci_dev *dev) dev->ptm_cap = ptm;atomic_set (&dev->ptm_enable_cnt,0 ); -pci_add_ext_cap_save_buffer (dev,PCI_EXT_CAP_ID_PTM ,sizeof (u32)); +pci_reserve_ptm_state (dev);pci_read_config_dword (dev, ptm +PCI_PTM_CAP , &cap); dev->ptm_granularity =FIELD_GET (PCI_PTM_GRANULARITY_MASK , cap);
@@ -90,36 +100,18 @@ void pci_ptm_init(struct pci_dev *dev)void pci_save_ptm_state (struct pci_dev *dev) { -u16 ptm = dev->ptm_cap; -struct pci_cap_saved_state *save_state; -u32 *cap; - -if (!ptm) +if (!dev->ptm_cap)return ; - save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_PTM ); -if (!save_state) -return ; - - cap = (u32 *)&save_state->cap.data[0 ]; -pci_read_config_dword (dev, ptm +PCI_PTM_CTRL , cap); +pci_save_cap_dword (dev, dev->ptm_cap +PCI_PTM_CTRL ); }void pci_restore_ptm_state (struct pci_dev *dev) { -u16 ptm = dev->ptm_cap; -struct pci_cap_saved_state *save_state; -u32 *cap; - -if (!ptm) -return ; - - save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_PTM ); -if (!save_state) +if (!dev->ptm_cap)return ; - cap = (u32 *)&save_state->cap.data[0 ]; -pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , *cap); +pci_restore_cap_dword (dev, dev->ptm_cap +PCI_PTM_CTRL ); }/* Enable PTM in the Control register if possible */
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 10/17 PCI/TPH: Save TPH state in the saved capability store
In reply to David MatlackSave and restore the TPH Control register and Steering Tag table through the per-device saved capability store instead of a private pci_cap_saved_state buffer.
The Steering Tag table is variable length, so pci_tph_init() had to compute a buffer size and both the save and restore loops had to walk that buffer in step with configuration space. Reserve the table by offset instead so that the loops can address the entries directly.
Reserve the registers in pci_reserve_tph_state(), called from pci_tph_init(), and report a refused reservation. pci_tph_init() ignored the return value of pci_add_ext_cap_save_buffer(), so a failed allocation left TPH state unsaved without any message. An allocation failure is now reported once by pci_saved_caps_finalize().
No other functional change intended.
--- drivers/pci/tph.c | 54 +++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 30 deletions(-)
drivers/pci/tph.c +24 -30
@@ -439,10 +439,7 @@ EXPORT_SYMBOL(pcie_enable_tph);void pci_restore_tph_state (struct pci_dev *pdev) { -struct pci_cap_saved_state *save_state;int num_entries, i, offset; -u16 *st_entry; -u32 *cap;if (!pdev->tph_cap)return ;
@@ -450,29 +447,19 @@ void pci_restore_tph_state(struct pci_dev *pdev)if (!pdev->tph_enabled)return ; - save_state =pci_find_saved_ext_cap (pdev,PCI_EXT_CAP_ID_TPH ); -if (!save_state) -return ; -/* Restore control register and all ST entries */ - cap = &save_state->cap.data[0 ]; -pci_write_config_dword (pdev, pdev->tph_cap +PCI_TPH_CTRL , *cap++); - st_entry = (u16 *)cap; +pci_restore_cap_dword (pdev, pdev->tph_cap +PCI_TPH_CTRL ); offset =PCI_TPH_BASE_SIZEOF ; num_entries =pcie_tph_get_st_table_size (pdev);for (i =0 ; i < num_entries; i++) { -pci_write_config_word (pdev, pdev->tph_cap + offset, - *st_entry++); +pci_restore_cap_word (pdev, pdev->tph_cap + offset); offset +=sizeof (u16); } }void pci_save_tph_state (struct pci_dev *pdev) { -struct pci_cap_saved_state *save_state;int num_entries, i, offset; -u16 *st_entry; -u32 *cap;if (!pdev->tph_cap)return ;
@@ -480,21 +467,14 @@ void pci_save_tph_state(struct pci_dev *pdev)if (!pdev->tph_enabled)return ; - save_state =pci_find_saved_ext_cap (pdev,PCI_EXT_CAP_ID_TPH ); -if (!save_state) -return ; -/* Save control register */ - cap = &save_state->cap.data[0 ]; -pci_read_config_dword (pdev, pdev->tph_cap +PCI_TPH_CTRL , cap++); +pci_save_cap_dword (pdev, pdev->tph_cap +PCI_TPH_CTRL );/* Save all ST entries in extended capability structure */ - st_entry = (u16 *)cap; offset =PCI_TPH_BASE_SIZEOF ; num_entries =pcie_tph_get_st_table_size (pdev);for (i =0 ; i < num_entries; i++) { -pci_read_config_word (pdev, pdev->tph_cap + offset, - st_entry++); +pci_save_cap_word (pdev, pdev->tph_cap + offset); offset +=sizeof (u16); } }
@@ -506,16 +486,30 @@ void pci_no_tph(void)pr_info ("PCIe TPH is disabled\n" ); } -void pci_tph_init (struct pci_dev *pdev) +static void pci_reserve_tph_state (struct pci_dev *pdev) { -int num_entries; -u32 save_size; +unsigned int off, len; +int num_entries, rc; + + off = pdev->tph_cap +PCI_TPH_CTRL ; + rc =pci_reserve_saved_cap (pdev, off,sizeof (u32)); + num_entries =pcie_tph_get_st_table_size (pdev); +if (!rc && num_entries) { + off = pdev->tph_cap +PCI_TPH_BASE_SIZEOF ; + len = num_entries *sizeof (u16); + rc =pci_reserve_saved_cap (pdev, off, len); + } +if (rc) +pci_err (pdev,"unable to reserve TPH save state (%pe)\n" , +ERR_PTR (rc)); +} + +void pci_tph_init (struct pci_dev *pdev) +{ pdev->tph_cap =pci_find_ext_capability (pdev,PCI_EXT_CAP_ID_TPH );if (!pdev->tph_cap)return ; - num_entries =pcie_tph_get_st_table_size (pdev); - save_size =sizeof (u32) + num_entries *sizeof (u16); -pci_add_ext_cap_save_buffer (pdev,PCI_EXT_CAP_ID_TPH , save_size); +pci_reserve_tph_state (pdev); }
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 11/17 PCI/DPC: Save DPC state in the saved capability store
In reply to David MatlackSave and restore the DPC Control register through the per-device saved capability store instead of a private pci_cap_saved_state buffer.
Key the save and restore paths off pdev->dpc_cap rather than pci_is_pcie(). The old code relied on the save buffer existing only when the capability did, and the store has no per-capability entry to check.
Reserve the register in pci_reserve_dpc_state(), called from pci_dpc_init().
No functional change intended.
--- drivers/pci/pcie/dpc.c | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-)
drivers/pci/pcie/dpc.c +15 -22
@@ -45,34 +45,18 @@ static const char * const rp_pio_error_string[] = {void pci_save_dpc_state (struct pci_dev *dev) { -struct pci_cap_saved_state *save_state; -u16 *cap; - -if (!pci_is_pcie (dev)) -return ; - - save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_DPC ); -if (!save_state) +if (!dev->dpc_cap)return ; - cap = (u16 *)&save_state->cap.data[0 ]; -pci_read_config_word (dev, dev->dpc_cap +PCI_EXP_DPC_CTL , cap); +pci_save_cap_word (dev, dev->dpc_cap +PCI_EXP_DPC_CTL ); }void pci_restore_dpc_state (struct pci_dev *dev) { -struct pci_cap_saved_state *save_state; -u16 *cap; - -if (!pci_is_pcie (dev)) -return ; - - save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_DPC ); -if (!save_state) +if (!dev->dpc_cap)return ; - cap = (u16 *)&save_state->cap.data[0 ]; -pci_write_config_word (dev, dev->dpc_cap +PCI_EXP_DPC_CTL , *cap); +pci_restore_cap_word (dev, dev->dpc_cap +PCI_EXP_DPC_CTL ); }static DECLARE_WAIT_QUEUE_HEAD (dpc_completed_waitqueue);
@@ -400,6 +384,16 @@ static irqreturn_t dpc_irq(int irq, void *context)return IRQ_HANDLED ; } +static void pci_reserve_dpc_state (struct pci_dev *pdev) +{ +int rc; + + rc =pci_reserve_saved_cap (pdev, pdev->dpc_cap +PCI_EXP_DPC_CTL ,sizeof (u16)); +if (rc) +pci_err (pdev,"unable to reserve DPC save state (%pe)\n" , +ERR_PTR (rc)); +} +void pci_dpc_init (struct pci_dev *pdev) {u16 cap;
@@ -408,8 +402,7 @@ void pci_dpc_init(struct pci_dev *pdev)if (!pdev->dpc_cap)return ; -if (pci_add_ext_cap_save_buffer (pdev,PCI_EXT_CAP_ID_DPC ,sizeof (u16))) -pci_err (pdev,"unable to allocate suspend buffer for DPC\n" ); +pci_reserve_dpc_state (pdev);pci_read_config_word (pdev, pdev->dpc_cap +PCI_EXP_DPC_CAP , &cap);if (!(cap &PCI_EXP_DPC_CAP_RP_EXT ))
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 12/17 PCI: Save ACS state in the saved capability store
In reply to David MatlackSave and restore the ACS Control register and the ACS Egress Control Vector through the per-device saved capability store instead of a private pci_cap_saved_state buffer.
Rename pci_allocate_acs_save_buffer() to pci_reserve_acs_state(), to match pci_save_acs_state() and pci_restore_acs_state().
Reserve the Egress Control Vector using the size the device reports in its HwInit ACS Capability register during setup, as the save buffer was sized before. Save or restore the vector only if all of it is reserved, since its size is read from the device again each time.
pci_liveupdate_adopt_acs() checked for the ACS save buffer to make sure adopted controls would survive a later reset. Check that the ACS Control register has room in the store instead, which also fails if the store could not be allocated.
No functional change intended for devices whose Egress Control Vector size does not change after setup.
--- drivers/pci/liveupdate.c | 15 +++---- drivers/pci/pci.c | 87 ++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 56 deletions(-)
drivers/pci/liveupdate.c +8 -7
@@ -932,14 +932,15 @@ int pci_liveupdate_adopt_acs(struct pci_dev *dev)/* * Adopting the previous kernel's controls depends on them being -* captured in the ACS save buffer, so that they are reapplied if the -* device is later reset. Without that buffer, e.g. because it could -* not be allocated under memory pressure, the adopted controls would -* be silently lost by the first reset. Program ACS from scratch -* instead, which is a better outcome than leaving ACS disabled. +* captured in the saved capability store, so that they are reapplied +* if the device is later reset. Without room in the store, e.g. +* because it could not be allocated under memory pressure, the adopted +* controls would be silently lost by the first reset. Program ACS from +* scratch instead, which is a better outcome than leaving ACS disabled. */ -if (dev->acs_cap && !pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_ACS )) { -pci_err (dev,"No ACS save buffer, not adopting ACS controls\n" ); +if (dev->acs_cap && + !pci_saved_cap_reserved (dev, dev->acs_cap +PCI_ACS_CTRL )) { +pci_err (dev,"No ACS save state, not adopting ACS controls\n" );return -EINVAL ; }
drivers/pci/pci.c +38 -49
157 lines not on this page · raw message
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 13/17 PCI/VC: Split the VC Resource Control restore into a helper
In reply to David MatlackMove the restore of the VC Resource Control register out of pci_vc_do_save_buffer() and into pci_vc_restore_res_ctrl().
Restoring this register is more involved than the rest of the walk: the enable bit has to be preserved across the write, the port arbitration table may have to be reloaded, and the VC may have to be re-enabled afterwards. Inlined in the middle of a loop that is already sizing, saving and restoring three kinds of register, it is hard to see where that sequence begins and ends.
No functional change intended.
--- drivers/pci/vc.c | 51 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 17 deletions(-)
drivers/pci/vc.c +34 -17
@@ -168,6 +168,37 @@ static void pci_vc_enable(struct pci_dev *dev, int pos, int res)pci_err (link,"VC%d negotiation stuck pending\n" , id); } +/** +* pci_vc_restore_res_ctrl - Restore a VC Resource Control Register +* @dev: device +* @pos: starting position of VC capability (VC/VC9/MFVC) +* @res: VC resource number, ie. VCn (0-7) +* @ctrl: saved VC Resource Control Register +*/ +static void pci_vc_restore_res_ctrl (struct pci_dev *dev,int pos,int res, +u32 ctrl) +{ +int ctrl_pos = pos +PCI_VC_RES_CTRL + (res *PCI_CAP_VC_PER_VC_SIZEOF ); +u32 tmp; + +/* +* For an FLR case, the VC config may remain. Preserve enable bit, +* restore the rest. +*/ +pci_read_config_dword (dev, ctrl_pos, &tmp); + tmp &=PCI_VC_RES_CTRL_ENABLE ; + tmp |= ctrl & ~PCI_VC_RES_CTRL_ENABLE ; +pci_write_config_dword (dev, ctrl_pos, tmp); + +/* Load port arbitration table if used */ +if (ctrl &PCI_VC_RES_CTRL_ARB_SELECT ) +pci_vc_load_port_arb_table (dev, pos, res); + +/* Re-enable if needed */ +if ((ctrl ^ tmp) &PCI_VC_RES_CTRL_ENABLE ) +pci_vc_enable (dev, pos, res); +} +/** * pci_vc_do_save_buffer - Size, save, or restore VC state * @dev: device
@@ -311,23 +342,9 @@ static int pci_vc_do_save_buffer(struct pci_dev *dev, int pos,if (save)pci_read_config_dword (dev, ctrl_pos, (u32 *)buf); -else { -u32 tmp, ctrl = *(u32 *)buf; -/* -* For an FLR case, the VC config may remain. -* Preserve enable bit, restore the rest. -*/ -pci_read_config_dword (dev, ctrl_pos, &tmp); - tmp &=PCI_VC_RES_CTRL_ENABLE ; - tmp |= ctrl & ~PCI_VC_RES_CTRL_ENABLE ; -pci_write_config_dword (dev, ctrl_pos, tmp); -/* Load port arbitration table if used */ -if (ctrl &PCI_VC_RES_CTRL_ARB_SELECT ) -pci_vc_load_port_arb_table (dev, pos, i); -/* Re-enable if needed */ -if ((ctrl ^ tmp) &PCI_VC_RES_CTRL_ENABLE ) -pci_vc_enable (dev, pos, i); - } +else +pci_vc_restore_res_ctrl (dev, pos, i, + *(u32 *)buf); buf +=4 ; } len +=4 ;
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 14/17 PCI/VC: Save VC state in the saved capability store
In reply to David MatlackSave and restore the Virtual Channel registers, including the VC and Port Arbitration Tables, through the per-device saved capability store instead of a private pci_cap_saved_state buffer.
Walking VC configuration space is complicated, so sizing, saving and restoring it were all folded into pci_vc_do_save_buffer() to guarantee they agreed on the buffer layout. Keep the walk shared: because the store is addressed by configuration space offset, the three passes now only have to agree on which registers exist, not on where each one lands in a buffer. Drop pci_vc_do_save_buffer()'s dual size/save/restore return value, its running length, its buffer cursor, and the check that the length matched the buffer size, and rename it to pci_vc_do_saved_caps().
Rename pci_allocate_vc_save_buffers() to pci_reserve_vc_state(), and pci_allocate_cap_save_buffers() to pci_reserve_saved_caps(), and make pci_save_vc_state() void, since none of them allocates anything now.
pci_save_state() therefore no longer fails with -ENOMEM when the VC state cannot be saved, and the messages for a missing or mis-sized save buffer go away. A failure to allocate the store is reported once during device setup instead.
The arbitration table offsets and sizes come from the device, so what it reports later need not match what was reserved during setup. Save or restore a table only if all of it was reserved, so that hardware is only asked to reload the VC Arbitration Table if it was actually restored, as before.
No other functional change intended.
--- drivers/pci/pci.c | 10 +- drivers/pci/pci.h | 6 +- drivers/pci/probe.c | 4 +- drivers/pci/vc.c | 230 +++++++++++++++++++++++--------------------- 4 files changed, 130 insertions(+), 120 deletions(-)
drivers/pci/pci.c +6 -4
@@ -1949,7 +1949,9 @@ int pci_save_state(struct pci_dev *dev)pci_save_ptm_state (dev);pci_save_tph_state (dev);pci_save_acs_state (dev); -return pci_save_vc_state (dev); +pci_save_vc_state (dev); + +return 0 ; }EXPORT_SYMBOL (pci_save_state);
@@ -3727,16 +3729,16 @@ int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size) }/** -* pci_allocate_cap_save_buffers - allocate buffers for saving capabilities +* pci_reserve_saved_caps - reserve room to save capability registers * @dev: the PCI device */ -void pci_allocate_cap_save_buffers (struct pci_dev *dev) +void pci_reserve_saved_caps (struct pci_dev *dev) {pci_reserve_pcie_state (dev);pci_reserve_pcix_state (dev);pci_reserve_ltr_state (dev);pci_reserve_acs_state (dev); -pci_allocate_vc_save_buffers (dev); +pci_reserve_vc_state (dev); }void pci_free_cap_save_buffers (struct pci_dev *dev)
drivers/pci/pci.h +3 -3
@@ -249,7 +249,7 @@ struct pci_cap_saved_state {struct pci_cap_saved_data cap; }; -void pci_allocate_cap_save_buffers (struct pci_dev *dev); +void pci_reserve_saved_caps (struct pci_dev *dev);void pci_free_cap_save_buffers (struct pci_dev *dev);int pci_add_cap_save_buffer (struct pci_dev *dev,char cap,unsigned int size); int pci_add_ext_cap_save_buffer(struct pci_dev *dev,
@@ -359,9 +359,9 @@ void pci_vpd_init(struct pci_dev *dev);extern const struct attribute_group pci_dev_vpd_attr_group;/* PCI Virtual Channel */ -int pci_save_vc_state (struct pci_dev *dev); +void pci_save_vc_state (struct pci_dev *dev);void pci_restore_vc_state (struct pci_dev *dev); -void pci_allocate_vc_save_buffers (struct pci_dev *dev); +void pci_reserve_vc_state (struct pci_dev *dev);/* PCI /proc functions */ #ifdef CONFIG_PROC_FS
drivers/pci/probe.c +2 -2
@@ -2662,8 +2662,8 @@ static void pci_init_capabilities(struct pci_dev *dev)pci_msi_init (dev);/* Disable MSI */ pci_msix_init (dev);/* Disable MSI-X */ -/* Buffers for saving PCIe and PCI-X capabilities */ -pci_allocate_cap_save_buffers (dev); +/* Room to save PCIe, PCI-X, LTR, ACS and VC state */ +pci_reserve_saved_caps (dev);pci_imm_ready_init (dev);/* Immediate Readiness */ pci_pm_init (dev);/* Power Management */
drivers/pci/vc.c +119 -111
346 lines not on this page · raw message
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 15/17 PCI: Save reserved capability registers in a single pass
In reply to David MatlackSave every reserved capability register in pci_save_state() with one walk of the reservation bitmap instead of a save function per capability. Every capability now reserves its registers during device setup, so the bitmap already records exactly which configuration space DWORDs need to be read.
Delete pci_save_pcie_state(), pci_save_pcix_state(), pci_save_ltr_state(), pci_save_aer_state(), pci_save_ptm_state(), pci_save_tph_state(), pci_save_dpc_state() and pci_save_vc_state(), along with the PCI_VC_SAVE arm of the Virtual Channel walker.
Keep pci_save_aspm_l1ss_state(), which also saves the parent's L1SS registers into the parent's store, and call it from pci_save_state() instead of the deleted pci_save_pcie_state(). Keep pci_save_acs_state() too, but only for pci_enable_acs(), which refreshes the saved ACS registers after a driver binds.
This is not a pure refactor in two places:
- PCIe: pci_save_pcie_state() read each register with pcie_capability_read_word(), which returns 0 for registers the device does not implement. The walk reads the DWORD raw, so those slots now hold whatever the device returns. Restore still goes through pcie_capability_write_word(), which skips unimplemented registers.
- L1SS on Downstream Ports: until now only the child saved a Downstream Port's L1SS registers, through pci_save_aspm_l1ss_state(). The walk now also saves them when the port itself is saved. Both read the same registers, and only the child's restore writes them back.
Every other capability already saved whole DWORDs, so its saved values are unchanged. Capabilities whose save function had a runtime guard, e.g. TPH only saved its registers while TPH was enabled, now get a few extra configuration space reads; restore keeps those guards.
--- drivers/pci/pci.c | 54 +++++++++++----------------------------- drivers/pci/pci.h | 11 +------- drivers/pci/pcie/aer.c | 13 +--------- drivers/pci/pcie/aspm.c | 17 +------------ drivers/pci/pcie/dpc.c | 8 ------ drivers/pci/pcie/ptm.c | 8 ------ drivers/pci/saved-caps.c | 27 +++++++++++++++++--- drivers/pci/tph.c | 22 ---------------- drivers/pci/vc.c | 45 ++++++--------------------------- 9 files changed, 49 insertions(+), 156 deletions(-)
drivers/pci/pci.c +14 -40
@@ -1091,7 +1091,9 @@ static void pci_reserve_acs_state(struct pci_dev *dev)* @dev: the PCI device * * Record the ACS configuration currently programmed in hardware so that -* pci_restore_acs_state() can reapply it after a reset. +* pci_restore_acs_state() can reapply it after a reset. pci_save_state() +* saves these registers along with every other reserved register, so this is +* only needed to refresh them outside of pci_save_state(). */ static void pci_save_acs_state (struct pci_dev *dev) {
@@ -1786,8 +1788,8 @@ int pci_set_power_state_locked(struct pci_dev *dev, pci_power_t state)EXPORT_SYMBOL (pci_set_power_state_locked);/* -* PCIe capability registers saved and restored by the PCI core. Registers a -* device does not implement are skipped by the pcie_capability_*() accessors, +* PCIe capability registers reserved and restored by the PCI core. +* pcie_capability_write_word() skips registers the device does not implement, * so space is reserved for all of them regardless of the device. */ static const u16 pcie_saved_regs[] = {
@@ -1822,24 +1824,6 @@ struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)return _pci_find_saved_cap (dev, cap, true); } -static void pci_save_pcie_state (struct pci_dev *dev) -{ -unsigned int i; -u16 val; - -if (!pci_is_pcie (dev)) -return ; - -for (i =0 ; i <ARRAY_SIZE (pcie_saved_regs); i++) { -pcie_capability_read_word (dev, pcie_saved_regs[i], &val); -pci_write_saved_cap_word (dev, dev->pcie_cap + pcie_saved_regs[i], - val); - } - -pci_save_aspm_l1ss_state (dev); -pci_save_ltr_state (dev); -} -static void pci_reserve_pcie_state (struct pci_dev *dev) {unsigned int i;
@@ -1890,17 +1874,6 @@ static void pci_restore_pcie_state(struct pci_dev *dev) } } -static void pci_save_pcix_state (struct pci_dev *dev) -{ -int pos; - - pos =pci_find_capability (dev,PCI_CAP_ID_PCIX ); -if (!pos) -return ; - -pci_save_cap_word (dev, pos +PCI_X_CMD ); -} -static void pci_reserve_pcix_state (struct pci_dev *dev) {int pos, rc;
@@ -1942,14 +1915,15 @@ int pci_save_state(struct pci_dev *dev) } dev->state_saved = true; -pci_save_pcie_state (dev); -pci_save_pcix_state (dev); -pci_save_dpc_state (dev); -pci_save_aer_state (dev); -pci_save_ptm_state (dev); -pci_save_tph_state (dev); -pci_save_acs_state (dev); -pci_save_vc_state (dev); +pci_save_caps (dev); + +/* +* L1SS is the one capability whose state does not live entirely in +* this device's store: an endpoint's L1 substates only take effect if +* its upstream port is programmed to match, so the two are saved and +* restored together. +*/ +pci_save_aspm_l1ss_state (dev);return 0 ; }
drivers/pci/pci.h +1 -10
@@ -265,6 +265,7 @@ void pci_saved_caps_finalize(struct pci_dev *dev);void pci_saved_caps_release (struct pci_dev *dev);int pci_reserve_saved_cap (struct pci_dev *dev,unsigned int off,unsigned int len);bool pci_saved_cap_reserved (struct pci_dev *dev,unsigned int off); +void pci_save_caps (struct pci_dev *dev);void pci_save_cap_word (struct pci_dev *dev,unsigned int off);void pci_save_cap_dword (struct pci_dev *dev,unsigned int off);void pci_restore_cap_word (struct pci_dev *dev,unsigned int off);
@@ -359,7 +360,6 @@ void pci_vpd_init(struct pci_dev *dev);extern const struct attribute_group pci_dev_vpd_attr_group;/* PCI Virtual Channel */ -void pci_save_vc_state (struct pci_dev *dev);void pci_restore_vc_state (struct pci_dev *dev);void pci_reserve_vc_state (struct pci_dev *dev);
@@ -944,7 +944,6 @@ struct rcec_ea {#endif #ifdef CONFIG_PCIE_DPC -void pci_save_dpc_state (struct pci_dev *dev);void pci_restore_dpc_state (struct pci_dev *dev);void pci_dpc_init (struct pci_dev *pdev);void dpc_process_error (struct pci_dev *pdev);
@@ -952,7 +951,6 @@ pci_ers_result_t dpc_reset_link(struct pci_dev *pdev);bool pci_dpc_recovered (struct pci_dev *pdev);unsigned int dpc_tlp_log_len (struct pci_dev *dev);#else -static inline void pci_save_dpc_state (struct pci_dev *dev) { }static inline void pci_restore_dpc_state (struct pci_dev *dev) { }static inline void pci_dpc_init (struct pci_dev *pdev) { }static inline bool pci_dpc_recovered (struct pci_dev *pdev) {return false; }
@@ -1077,25 +1075,21 @@ static inline int pci_resource_num_to_vf_bar(int resno)#ifdef CONFIG_PCIE_TPH void pci_restore_tph_state (struct pci_dev *dev); -void pci_save_tph_state (struct pci_dev *dev);void pci_no_tph (void );void pci_tph_init (struct pci_dev *dev);#else static inline void pci_restore_tph_state (struct pci_dev *dev) { } -static inline void pci_save_tph_state (struct pci_dev *dev) { }static inline void pci_no_tph (void ) { }static inline void pci_tph_init (struct pci_dev *dev) { }#endif #ifdef CONFIG_PCIE_PTM void pci_ptm_init (struct pci_dev *dev); -void pci_save_ptm_state (struct pci_dev *dev);void pci_restore_ptm_state (struct pci_dev *dev);void pci_suspend_ptm (struct pci_dev *dev);void pci_resume_ptm (struct pci_dev *dev);#else static inline void pci_ptm_init (struct pci_dev *dev) { } -static inline void pci_save_ptm_state (struct pci_dev *dev) { }static inline void pci_restore_ptm_state (struct pci_dev *dev) { }static inline void pci_suspend_ptm (struct pci_dev *dev) { }static inline void pci_resume_ptm (struct pci_dev *dev) { }
@@ -1150,7 +1144,6 @@ bool pcie_wait_for_link(struct pci_dev *pdev, bool active);int pcie_retrain_link (struct pci_dev *pdev,bool use_lt);/* ASPM-related functionality we need even without CONFIG_PCIEASPM */ -void pci_save_ltr_state (struct pci_dev *dev);void pci_reserve_ltr_state (struct pci_dev *dev);void pci_restore_ltr_state (struct pci_dev *dev);void pci_configure_aspm_l1ss (struct pci_dev *dev);
@@ -1360,7 +1353,6 @@ extern const struct attribute_group aer_attr_group;void pci_aer_clear_fatal_status (struct pci_dev *dev);int pci_aer_clear_status (struct pci_dev *dev);int pci_aer_raw_clear_status (struct pci_dev *dev); -void pci_save_aer_state (struct pci_dev *dev);void pci_restore_aer_state (struct pci_dev *dev);#else static inline void pci_no_aer (void ) { }
@@ -1369,7 +1361,6 @@ static inline void pci_aer_exit(struct pci_dev *d) { }static inline void pci_aer_clear_fatal_status (struct pci_dev *dev) { }static inline int pci_aer_clear_status (struct pci_dev *dev) {return -EINVAL ; }static inline int pci_aer_raw_clear_status (struct pci_dev *dev) {return -EINVAL ; } -static inline void pci_save_aer_state (struct pci_dev *dev) { }static inline void pci_restore_aer_state (struct pci_dev *dev) { }#endif
drivers/pci/pcie/aer.c +1 -12
@@ -353,7 +353,7 @@ int pci_aer_clear_status(struct pci_dev *dev) }/* -* AER registers saved and restored by the PCI core. PCI_ERR_ROOT_COMMAND is +* AER registers reserved and restored by the PCI core. PCI_ERR_ROOT_COMMAND is * only implemented by Root Ports and Root Complex Event Collectors (PCIe * r6.0, sec 7.8.4.9) and so must come last. */
@@ -373,17 +373,6 @@ static unsigned int aer_nr_saved_regs(struct pci_dev *dev)return ARRAY_SIZE (aer_saved_regs) -1 ; } -void pci_save_aer_state (struct pci_dev *dev) -{ -unsigned int i; - -if (!dev->aer_cap) -return ; - -for (i =0 ; i <aer_nr_saved_regs (dev); i++) -pci_save_cap_dword (dev, dev->aer_cap + aer_saved_regs[i]); -} -void pci_restore_aer_state (struct pci_dev *dev) {unsigned int i;
drivers/pci/pcie/aspm.c +1 -16
@@ -27,21 +27,6 @@#include "../pci.h" -void pci_save_ltr_state (struct pci_dev *dev) -{ -int ltr; - -if (!pci_is_pcie (dev)) -return ; - - ltr =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_LTR ); -if (!ltr) -return ; - -/* Some broken devices only support dword access to LTR */ -pci_save_cap_dword (dev, ltr +PCI_LTR_MAX_SNOOP_LAT ); -} -void pci_reserve_ltr_state (struct pci_dev *dev) {int ltr, rc;
@@ -105,7 +90,7 @@ void pci_save_aspm_l1ss_state(struct pci_dev *pdev)/* * Save L1 substate configuration. The ASPM L0s/L1 configuration -* in PCI_EXP_LNKCTL_ASPMC is saved by pci_save_pcie_state(). +* in PCI_EXP_LNKCTL_ASPMC is saved by pci_save_caps(). */ pci_save_cap_dword (pdev, pdev->l1ss +PCI_L1SS_CTL2 );pci_save_cap_dword (pdev, pdev->l1ss +PCI_L1SS_CTL1 );
drivers/pci/pcie/dpc.c +0 -8
@@ -43,14 +43,6 @@ static const char * const rp_pio_error_string[] = { "Memory Request Completion Timeout", /* Bit Position 18 */ }; -void pci_save_dpc_state (struct pci_dev *dev) -{ -if (!dev->dpc_cap) -return ; - -pci_save_cap_word (dev, dev->dpc_cap +PCI_EXP_DPC_CTL ); -} -void pci_restore_dpc_state (struct pci_dev *dev) {if (!dev->dpc_cap)
drivers/pci/pcie/ptm.c +0 -8
@@ -98,14 +98,6 @@ void pci_ptm_init(struct pci_dev *dev) dev->ptm_requester =1 ; } -void pci_save_ptm_state (struct pci_dev *dev) -{ -if (!dev->ptm_cap) -return ; - -pci_save_cap_dword (dev, dev->ptm_cap +PCI_PTM_CTRL ); -} -void pci_restore_ptm_state (struct pci_dev *dev) {if (!dev->ptm_cap)
drivers/pci/saved-caps.c +24 -3
@@ -81,9 +81,9 @@ bool pci_saved_cap_reserved(struct pci_dev *dev, unsigned int off)* @off: offset of the register in configuration space * @len: size of the register in bytes * -* Reserve room to save the register at @off, which may then be saved with -* pci_save_cap_word() and friends. Must be called during device setup, before -* the store is finalized. +* Reserve room to save the register at @off, which pci_save_caps() then reads +* into the store. Must be called during device setup, before the store is +* finalized. * * Return: 0 on success, negative errno otherwise. */
@@ -122,6 +122,27 @@ int pci_reserve_saved_cap(struct pci_dev *dev, unsigned int off, unsigned int lereturn 0 ; } +/** +* pci_save_caps - save every reserved capability register +* @dev: the PCI device +* +* Read all of the configuration space DWORDs that capabilities reserved during +* device setup into the store. Values are kept in ascending offset order, the +* same order that for_each_set_bit() walks the reservation bitmap, so the +* destination slot advances in lockstep with the bitmap and needs no lookup. +*/ +void pci_save_caps (struct pci_dev *dev) +{ +struct pci_saved_caps *caps = &dev->saved_caps; +unsigned int dword, i =0 ; + +if (!caps->dword_val) +return ; + +for_each_set_bit (dword, caps->dword_map,PCI_CFG_SPACE_EXP_DWORDS ) +pci_read_config_dword (dev, dword *sizeof (u32), &caps->dword_val[i++]); +} +/** * pci_save_cap_dword - save a 32-bit capability register * @dev: the PCI device
drivers/pci/tph.c +0 -22
@@ -457,28 +457,6 @@ void pci_restore_tph_state(struct pci_dev *pdev) } } -void pci_save_tph_state (struct pci_dev *pdev) -{ -int num_entries, i, offset; - -if (!pdev->tph_cap) -return ; - -if (!pdev->tph_enabled) -return ; - -/* Save control register */ -pci_save_cap_dword (pdev, pdev->tph_cap +PCI_TPH_CTRL ); - -/* Save all ST entries in extended capability structure */ - offset =PCI_TPH_BASE_SIZEOF ; - num_entries =pcie_tph_get_st_table_size (pdev); -for (i =0 ; i < num_entries; i++) { -pci_save_cap_word (pdev, pdev->tph_cap + offset); - offset +=sizeof (u16); - } -} -void pci_no_tph (void ) { pci_tph_disabled = true;
drivers/pci/vc.c +8 -37
94 lines not on this page · raw message
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 16/17 PCI: Remove the per-capability save buffers
In reply to David MatlackDelete struct pci_cap_saved_state, struct pci_cap_saved_data, pci_dev.saved_cap_space and the helpers that managed them. Every capability now saves its registers in the offset-indexed capability store, so nothing is left to find, allocate or free.
Drop pci_free_cap_save_buffers() from pci_release_capabilities(); pci_saved_caps_release() already frees the one remaining allocation.
pci_store_saved_state() and pci_load_saved_state() no longer have to append and consume the records of the capabilities that still had a buffer, so the blob they exchange is now just the saved DWORDs.
--- drivers/pci/pci.c | 136 +------------------------------------------- drivers/pci/pci.h | 19 ------- drivers/pci/probe.c | 1 - include/linux/pci.h | 1 - 4 files changed, 1 insertion(+), 156 deletions(-)
drivers/pci/pci.c +1 -135
@@ -1802,28 +1802,6 @@ static const u16 pcie_saved_regs[] = { PCI_EXP_SLTCTL2, }; -static struct pci_cap_saved_state *_pci_find_saved_cap (struct pci_dev *pci_dev, -u16 cap,bool extended) -{ -struct pci_cap_saved_state *tmp; - -hlist_for_each_entry (tmp, &pci_dev->saved_cap_space, next) { -if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap) -return tmp; - } -return NULL ; -} - -struct pci_cap_saved_state *pci_find_saved_cap (struct pci_dev *dev,char cap) -{ -return _pci_find_saved_cap (dev, cap, false); -} - -struct pci_cap_saved_state *pci_find_saved_ext_cap (struct pci_dev *dev,u16 cap) -{ -return _pci_find_saved_cap (dev, cap, true); -} -static void pci_reserve_pcie_state (struct pci_dev *dev) {unsigned int i;
@@ -2017,32 +1995,12 @@ EXPORT_SYMBOL(pci_restore_state);* * Laid out so that the position of every value is described by the device's * configuration space rather than by the kernel. -* -* Capabilities that have not moved to the saved capability store yet keep -* their own buffer, and their records trail @dword_val until the last of -* those buffers goes away. Both areas vary in length and only one of them -* can be a flexible array member, so the records are reached with -* pci_saved_state_records() rather than declared here. */ struct pci_saved_state {unsigned long dword_map[BITS_TO_LONGS (PCI_CFG_SPACE_EXP_DWORDS )];u32 dword_val[]; -/* struct pci_cap_saved_data cap[] follows dword_val */ }; -/* -* The records that follow the saved DWORDs, one per capability that still has -* its own save buffer, terminated by an empty record. -*/ -static struct pci_cap_saved_data * -pci_saved_state_records (struct pci_saved_state *state) -{ -unsigned int nr_dwords =bitmap_weight (state->dword_map, -PCI_CFG_SPACE_EXP_DWORDS ); - -return (struct pci_cap_saved_data *)&state->dword_val[nr_dwords]; -} -/** * pci_store_saved_state - Allocate and return an opaque struct containing * the device saved state.
@@ -2054,11 +2012,8 @@ struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev) {DECLARE_BITMAP (map,PCI_CFG_SPACE_EXP_DWORDS );struct pci_saved_caps *caps = &dev->saved_caps; -struct pci_cap_saved_state *tmp; -struct pci_cap_saved_data *cap;struct pci_saved_state *state;unsigned int dword, nr_dwords, i =0 ; -size_t size;if (!dev->state_saved)return NULL ;
@@ -2070,14 +2025,7 @@ struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev) nr_dwords =bitmap_weight (map,PCI_CFG_SPACE_EXP_DWORDS ); - size =struct_size (state, dword_val, nr_dwords); - -/* Room for the buffers that are left, and for the terminator */ - size +=sizeof (struct pci_cap_saved_data ); -hlist_for_each_entry (tmp, &dev->saved_cap_space, next) - size +=sizeof (struct pci_cap_saved_data ) + tmp->cap.size; - - state =kzalloc (size,GFP_KERNEL ); + state =kzalloc (struct_size (state, dword_val, nr_dwords),GFP_KERNEL );if (!state)return NULL ;
@@ -2092,15 +2040,6 @@ struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)pci_read_saved_cap_dword (dev, dword *sizeof (u32), val); } - cap =pci_saved_state_records (state); -hlist_for_each_entry (tmp, &dev->saved_cap_space, next) { -size_t len =sizeof (struct pci_cap_saved_data ) + tmp->cap.size; - -memcpy (cap, &tmp->cap, len); - cap = (struct pci_cap_saved_data *)((u8 *)cap + len); - } -/* Empty record terminates the list */ -return state; }EXPORT_SYMBOL_GPL (pci_store_saved_state);
@@ -2113,7 +2052,6 @@ EXPORT_SYMBOL_GPL(pci_store_saved_state);int pci_load_saved_state (struct pci_dev *dev,struct pci_saved_state *state) { -struct pci_cap_saved_data *cap;unsigned int dword, i =0 ; dev->state_saved = false;
@@ -2140,19 +2078,6 @@ int pci_load_saved_state(struct pci_dev *dev,pci_write_saved_cap_dword (dev, off, val); } - cap =pci_saved_state_records (state); -while (cap->size) { -struct pci_cap_saved_state *tmp; - - tmp =_pci_find_saved_cap (dev, cap->cap_nr, cap->cap_extended); -if (!tmp || tmp->cap.size != cap->size) -return -EINVAL ; - -memcpy (tmp->cap.data, cap->data, tmp->cap.size); - cap = (struct pci_cap_saved_data *)((u8 *)cap + -sizeof (struct pci_cap_saved_data ) + cap->size); - } - dev->state_saved = true;return 0 ; }
@@ -3652,56 +3577,6 @@ void pci_ea_init(struct pci_dev *dev) offset =pci_ea_read (dev, offset); } -static void pci_add_saved_cap (struct pci_dev *pci_dev, -struct pci_cap_saved_state *new_cap) -{ -hlist_add_head (&new_cap->next, &pci_dev->saved_cap_space); -} - -/** -* _pci_add_cap_save_buffer - allocate buffer for saving given -* capability registers -* @dev: the PCI device -* @cap: the capability to allocate the buffer for -* @extended: Standard or Extended capability ID -* @size: requested size of the buffer -*/ -static int _pci_add_cap_save_buffer (struct pci_dev *dev,u16 cap, -bool extended,unsigned int size) -{ -int pos; -struct pci_cap_saved_state *save_state; - -if (extended) - pos =pci_find_ext_capability (dev, cap); -else - pos =pci_find_capability (dev, cap); - -if (!pos) -return 0 ; - - save_state =kzalloc (sizeof (*save_state) + size,GFP_KERNEL ); -if (!save_state) -return -ENOMEM ; - - save_state->cap.cap_nr = cap; - save_state->cap.cap_extended = extended; - save_state->cap.size = size; -pci_add_saved_cap (dev, save_state); - -return 0 ; -} - -int pci_add_cap_save_buffer (struct pci_dev *dev,char cap,unsigned int size) -{ -return _pci_add_cap_save_buffer (dev, cap, false, size); -} - -int pci_add_ext_cap_save_buffer (struct pci_dev *dev,u16 cap,unsigned int size) -{ -return _pci_add_cap_save_buffer (dev, cap, true, size); -} -/** * pci_reserve_saved_caps - reserve room to save capability registers * @dev: the PCI device
@@ -3715,15 +3590,6 @@ void pci_reserve_saved_caps(struct pci_dev *dev)pci_reserve_vc_state (dev); } -void pci_free_cap_save_buffers (struct pci_dev *dev) -{ -struct pci_cap_saved_state *tmp; -struct hlist_node *n; - -hlist_for_each_entry_safe (tmp, n, &dev->saved_cap_space, next) -kfree (tmp); -} -/** * pci_configure_ari - enable or disable ARI forwarding * @dev: the PCI device
drivers/pci/pci.h +0 -19
@@ -237,26 +237,7 @@ int pci_bridge_secondary_bus_reset(struct pci_dev *dev);int pci_bus_error_reset (struct pci_dev *dev);int pci_try_reset_bridge (struct pci_dev *bridge); -struct pci_cap_saved_data { -u16 cap_nr; -bool cap_extended; -unsigned int size; -u32 data[]; -}; - -struct pci_cap_saved_state { -struct hlist_node next; -struct pci_cap_saved_data cap; -}; -void pci_reserve_saved_caps (struct pci_dev *dev); -void pci_free_cap_save_buffers (struct pci_dev *dev); -int pci_add_cap_save_buffer (struct pci_dev *dev,char cap,unsigned int size); -int pci_add_ext_cap_save_buffer (struct pci_dev *dev, -u16 cap,unsigned int size); -struct pci_cap_saved_state *pci_find_saved_cap (struct pci_dev *dev,char cap); -struct pci_cap_saved_state *pci_find_saved_ext_cap (struct pci_dev *dev, -u16 cap);/* DWORDs of the configuration space header, i.e. pci_dev.saved_config_space */ #define PCI_STD_HEADER_DWORDS (PCI_STD_HEADER_SIZEOF / sizeof(u32))
drivers/pci/probe.c +0 -1
@@ -2475,7 +2475,6 @@ static void pci_release_capabilities(struct pci_dev *dev)pci_aer_exit (dev);pci_rcec_exit (dev);pci_iov_release (dev); -pci_free_cap_save_buffers (dev);pci_saved_caps_release (dev); }
include/linux/pci.h +0 -1
@@ -549,7 +549,6 @@ struct pci_dev {spinlock_t pcie_cap_lock;/* Protects RMW ops in capability accessors */ u32 saved_config_space[16 ];/* Config space saved at suspend time */ -struct hlist_head saved_cap_space;struct pci_saved_caps saved_caps;/* Saved capability registers */ #ifdef CONFIG_HOTPLUG_PCI_PCIE
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH v2 17/17 PCI: liveupdate: Add helpers to preserve struct pci_saved_state
In reply to David MatlackAdd pci_liveupdate_preserve_saved_state(), which copies a struct pci_saved_state into memory preserved with KHO and returns its physical address, and pci_liveupdate_retrieve_saved_state(), which rebuilds the struct pci_saved_state from that address in the next kernel and frees the preserved memory. Add pci_liveupdate_unpreserve_saved_state() to free the memory if the device ends up not being preserved. If pci_liveupdate_retrieve_saved_state() fails, it leaves the memory in place so that the caller can try again.
Drivers that keep a device running across Live Update need to hand the state they captured with pci_store_saved_state() to the next kernel, since by the time the next kernel boots the device is no longer in that state. VFIO, for example, captures it at first open and uses it to restore the device when the last user closes it. Callers record only the physical address in their own preserved state, so the layout of the saved state stays private to the PCI core.
Define the layout as struct pci_saved_state_ser in include/linux/kho/abi/pci.h: a bitmap with one bit per DWORD of configuration space, followed by the saved DWORDs in ascending offset order, mirroring struct pci_saved_state. Each value is identified only by its offset, so the format does not change when a kernel starts or stops saving a register.
Drop saved capability registers that the next kernel does not reserve, since it would never restore them and pci_load_saved_state() would reject the whole state because of them. This lets the PCI core stop saving a register without breaking Live Update from older kernels. A register that the next kernel restores but the previous kernel did not save keeps its current saved value in the next kernel.
--- Documentation/PCI/liveupdate.rst | 3 + drivers/pci/liveupdate.c | 118 +++++++++++++++++++++++++++++++ drivers/pci/pci.c | 14 ---- drivers/pci/pci.h | 14 ++++ include/linux/kho/abi/pci.h | 32 +++++++++ include/linux/pci_liveupdate.h | 23 ++++++ 6 files changed, 190 insertions(+), 14 deletions(-)
Documentation/PCI/liveupdate.rst +3 -0
@@ -25,6 +25,9 @@ Live Update ABI .. kernel-doc:: include/linux/kho/abi/pci.h :doc: PCI File-Lifecycle Bound (FLB) Live Update ABI +.. kernel-doc:: include/linux/kho/abi/pci.h + :doc: PCI Saved State Live Update ABI + .. kernel-doc:: include/linux/kho/abi/pci.h :internal:
drivers/pci/liveupdate.c +118 -0
121 lines not on this page · raw message
drivers/pci/pci.c +0 -14
@@ -1987,20 +1987,6 @@ void pci_restore_state(struct pci_dev *dev) }EXPORT_SYMBOL (pci_restore_state); -/** -* struct pci_saved_state - a device's saved configuration space -* @dword_map: one bit per DWORD of configuration space, set if that DWORD -* was saved -* @dword_val: saved values, in ascending configuration space offset order -* -* Laid out so that the position of every value is described by the device's -* configuration space rather than by the kernel. -*/ -struct pci_saved_state { -unsigned long dword_map[BITS_TO_LONGS (PCI_CFG_SPACE_EXP_DWORDS )]; -u32 dword_val[]; -}; -/** * pci_store_saved_state - Allocate and return an opaque struct containing * the device saved state.
drivers/pci/pci.h +14 -0
@@ -242,6 +242,20 @@ void pci_reserve_saved_caps(struct pci_dev *dev);/* DWORDs of the configuration space header, i.e. pci_dev.saved_config_space */ #define PCI_STD_HEADER_DWORDS (PCI_STD_HEADER_SIZEOF / sizeof(u32)) +/** +* struct pci_saved_state - a device's saved configuration space +* @dword_map: one bit per DWORD of configuration space, set if that DWORD +* was saved +* @dword_val: saved values, in ascending configuration space offset order +* +* Laid out so that the position of every value is described by the device's +* configuration space rather than by the kernel. +*/ +struct pci_saved_state { +unsigned long dword_map[BITS_TO_LONGS (PCI_CFG_SPACE_EXP_DWORDS )]; +u32 dword_val[]; +}; +void pci_saved_caps_finalize (struct pci_dev *dev);void pci_saved_caps_release (struct pci_dev *dev);int pci_reserve_saved_cap (struct pci_dev *dev,unsigned int off,unsigned int len);
include/linux/kho/abi/pci.h +32 -0
@@ -93,4 +93,36 @@ struct pci_ser { u64 devices; } __packed; +/** +* DOC: PCI Saved State Live Update ABI +* +* A driver that keeps a PCI device running across a Live Update can hand the +* state it captured with pci_store_saved_state() to the next kernel. The PCI +* core copies the state into memory preserved with KHO, laid out as struct +* pci_saved_state_ser, and returns the physical address of that memory, which +* the driver records in its own preserved state. +* +* Every saved value is identified only by the configuration space offset of +* the register it was read from. The layout therefore does not depend on which +* registers the kernel that saved them chose to save, and the next kernel can +* drop values for registers it does not restore. +*/ + +/* Number of DWORDs in a PCI Express configuration space (4 KiB). */ +#define PCI_SAVED_STATE_SER_DWORDS 1024 + +/** +* struct pci_saved_state_ser - Serialized struct pci_saved_state +* +* @dword_map: One bit per DWORD of configuration space. Bit N, i.e. bit +* (N % 64) of @dword_map[N / 64], is set if the DWORD at +* configuration space offset N * 4 was saved. +* @dword_val: The saved DWORDs, one per bit set in @dword_map, in ascending +* configuration space offset order. +*/ +struct pci_saved_state_ser { +u64 dword_map[PCI_SAVED_STATE_SER_DWORDS /64 ]; +u32 dword_val[]; +} __packed; +#endif /* _LINUX_KHO_ABI_PCI_H */
include/linux/pci_liveupdate.h +23 -0
@@ -32,6 +32,7 @@ struct pci_liveupdate { };struct pci_dev ; +struct pci_saved_state ;#ifdef CONFIG_PCI_LIVEUPDATE int pci_liveupdate_register_flb (struct liveupdate_file_handler *fh);
@@ -40,6 +41,11 @@ int pci_liveupdate_preserve(struct pci_dev *dev);void pci_liveupdate_unpreserve (struct pci_dev *dev);void pci_liveupdate_finish (struct pci_dev *dev);bool pci_liveupdate_is_incoming (struct pci_dev *dev); +int pci_liveupdate_preserve_saved_state (const struct pci_saved_state *state, +u64 *phys); +void pci_liveupdate_unpreserve_saved_state (u64 phys); +struct pci_saved_state * +pci_liveupdate_retrieve_saved_state (struct pci_dev *dev,u64 phys);#else static inline int pci_liveupdate_register_flb (struct liveupdate_file_handler *fh) {
@@ -67,6 +73,23 @@ static inline bool pci_liveupdate_is_incoming(struct pci_dev *dev) {return false; } + +static inline int +pci_liveupdate_preserve_saved_state (const struct pci_saved_state *state, +u64 *phys) +{ +return -EOPNOTSUPP ; +} + +static inline void pci_liveupdate_unpreserve_saved_state (u64 phys) +{ +} + +static inline struct pci_saved_state * +pci_liveupdate_retrieve_saved_state (struct pci_dev *dev,u64 phys) +{ +return ERR_PTR (-EOPNOTSUPP ); +}#endif #endif /* LINUX_PCI_LIVEUPDATE_H */
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
Quoted 9 lines · David Matlack
Merge Plan
----------This series applies on top of v10 of the PCI core support for Live Update series [2], which is targeting Linux 7.5. This series is targeting Linux 7.5 as well.
Both series will be merged through liveupdate.git, matching the agreed-upon tree in the
MAINTAINERSentry for PCI LIVE UPDATE.
Apologies, an update I made here got lost. The PCI core support for Live Update is targeting 7.5, via liveupdate.git. _This_ series should probably target 7.6. I doubt we can get reviews in time for 7.5. And as for which tree, I am not sure. This series is mostly PCI core changes, so I lean towards merging through the PCI tree. Only the last commit touches drivers/pci/liveupdate.c.
Pasha Tatashin
maintainerQuoted 12 lines · David Matlack
Quoted 9 lines · David Matlack
Merge Plan
----------This series applies on top of v10 of the PCI core support for Live Update series [2], which is targeting Linux 7.5. This series is targeting Linux 7.5 as well.
Both series will be merged through liveupdate.git, matching the agreed-upon tree in the
MAINTAINERSentry for PCI LIVE UPDATE.
Quoted 6 lines
Apologies, an update I made here got lost. The PCI core support for Live Update is targeting 7.5, via liveupdate.git. _This_ series should probably target 7.6. I doubt we can get reviews in time for 7.5. And as for which tree, I am not sure. This series is mostly PCI core changes, so I lean towards merging through the PCI tree. Only the last commit touches
drivers/pci/liveupdate.c.
I looked through this series. Even though it is motivated by Live Update, it is primarily a refactoring of how the PCI core saves and restores capability state (to make struct pci_saved_state compatible with Live Update), so it should go through the PCI tree rather than liveupdate.git. Only two patches (12 and 17) touch drivers/pci/liveupdate.c, and we can Ack those.
Let's get the current v10 PCI core LU series landed in first; after that, this series can apply against 7.X-rc1 so the PCI tree can cleanly pick it up.
Signature · 1 line
Pasha