blob: 14d00ce45bfa954daea1a297c00c2c08d98636d3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Zhao, Yu557848c2008-10-13 19:18:07 +08002#ifndef DRIVERS_PCI_H
3#define DRIVERS_PCI_H
4
Alexandru Gagniuc2209e062018-11-28 16:28:04 -06005#include <linux/pci.h>
6
James Sewartf8bf2ae2019-12-10 15:51:33 -06007/* Number of possible devfns: 0.0 to 1f.7 inclusive */
8#define MAX_NR_DEVFNS 256
9
Wei Yangfff905f2015-06-30 09:16:41 +080010#define PCI_FIND_CAP_TTL 48
11
Lukas Wunner8531e282017-03-10 21:23:45 +010012#define PCI_VSEC_ID_INTEL_TBT 0x1234 /* Thunderbolt */
13
Maciej W. Rozycki33a176a2023-06-11 18:19:19 +010014#define PCIE_LINK_RETRAIN_TIMEOUT_MS 1000
15
Thomas Richard6aa9c092024-06-19 12:15:13 +020016/*
17 * Power stable to PERST# inactive.
18 *
19 * See the "Power Sequencing and Reset Signal Timings" table of the PCI Express
20 * Card Electromechanical Specification, Revision 5.1, Section 2.9.2, Symbol
21 * "T_PVPERL".
22 */
Yoshihiro Shimoda164f66b2023-10-18 17:56:17 +090023#define PCIE_T_PVPERL_MS 100
24
Frank Lie78bd502023-08-21 14:48:13 -040025/*
Thomas Richard6aa9c092024-06-19 12:15:13 +020026 * REFCLK stable before PERST# inactive.
27 *
28 * See the "Power Sequencing and Reset Signal Timings" table of the PCI Express
29 * Card Electromechanical Specification, Revision 5.1, Section 2.9.2, Symbol
30 * "T_PERST-CLK".
31 */
32#define PCIE_T_PERST_CLK_US 100
33
34/*
Damien Le Moal70a7bfb2024-04-13 09:41:20 +090035 * End of conventional reset (PERST# de-asserted) to first configuration
36 * request (device able to respond with a "Request Retry Status" completion),
37 * from PCIe r6.0, sec 6.6.1.
38 */
39#define PCIE_T_RRS_READY_MS 100
40
41/*
Frank Lie78bd502023-08-21 14:48:13 -040042 * PCIe r6.0, sec 5.3.3.2.1 <PME Synchronization>
43 * Recommends 1ms to 10ms timeout to check L2 ready.
44 */
45#define PCIE_PME_TO_L2_TIMEOUT_US 10000
46
Kevin Xied5ceb942024-03-28 17:18:33 +080047/*
48 * PCIe r6.0, sec 6.6.1 <Conventional Reset>
49 *
50 * - "With a Downstream Port that does not support Link speeds greater
51 * than 5.0 GT/s, software must wait a minimum of 100 ms following exit
52 * from a Conventional Reset before sending a Configuration Request to
53 * the device immediately below that Port."
54 *
55 * - "With a Downstream Port that supports Link speeds greater than
56 * 5.0 GT/s, software must wait a minimum of 100 ms after Link training
57 * completes before sending a Configuration Request to the device
58 * immediately below that Port."
59 */
60#define PCIE_RESET_CONFIG_DEVICE_WAIT_MS 100
61
Yoshihiro Shimoda95cb8ff2024-04-18 12:04:24 -040062/* Message Routing (r[2:0]); PCIe r6.0, sec 2.2.8 */
63#define PCIE_MSG_TYPE_R_RC 0
64#define PCIE_MSG_TYPE_R_ADDR 1
65#define PCIE_MSG_TYPE_R_ID 2
66#define PCIE_MSG_TYPE_R_BC 3
67#define PCIE_MSG_TYPE_R_LOCAL 4
68#define PCIE_MSG_TYPE_R_GATHER 5
69
Frank Li9972b172024-04-18 12:04:27 -040070/* Power Management Messages; PCIe r6.0, sec 2.2.8.2 */
71#define PCIE_MSG_CODE_PME_TURN_OFF 0x19
72
Yoshihiro Shimoda95cb8ff2024-04-18 12:04:24 -040073/* INTx Mechanism Messages; PCIe r6.0, sec 2.2.8.1 */
74#define PCIE_MSG_CODE_ASSERT_INTA 0x20
75#define PCIE_MSG_CODE_ASSERT_INTB 0x21
76#define PCIE_MSG_CODE_ASSERT_INTC 0x22
77#define PCIE_MSG_CODE_ASSERT_INTD 0x23
78#define PCIE_MSG_CODE_DEASSERT_INTA 0x24
79#define PCIE_MSG_CODE_DEASSERT_INTB 0x25
80#define PCIE_MSG_CODE_DEASSERT_INTC 0x26
81#define PCIE_MSG_CODE_DEASSERT_INTD 0x27
82
Jacob Keller343e51a2013-07-31 06:53:16 +000083extern const unsigned char pcie_link_speed[];
Sinan Kaya11eb0e0e2018-06-04 22:16:09 -040084extern bool pci_early_dump;
Jacob Keller343e51a2013-07-31 06:53:16 +000085
Yinghai Lu7a1562d2014-11-11 12:09:46 -080086bool pcie_cap_has_lnkctl(const struct pci_dev *dev);
Maciej W. Rozycki503fa232022-09-17 13:03:09 +010087bool pcie_cap_has_lnkctl2(const struct pci_dev *dev);
Patel, Mayurkumaraf65d1a2019-10-18 16:52:21 +000088bool pcie_cap_has_rtctl(const struct pci_dev *dev);
Yinghai Lu7a1562d2014-11-11 12:09:46 -080089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Functions internal to the PCI core code */
91
Krzysztof Wilczyński506140f2021-04-27 10:49:16 -050092#ifdef CONFIG_DMI
93extern const struct attribute_group pci_dev_smbios_attr_group;
94#endif
David Woodhousef7195822017-04-12 13:25:59 +010095
Martin Wilck3b519e42010-11-10 11:03:21 +010096enum pci_mmap_api {
97 PCI_MMAP_SYSFS, /* mmap on /sys/bus/pci/devices/<BDF>/resource<N> */
98 PCI_MMAP_PROCFS /* mmap on /proc/bus/pci/<BDF> */
99};
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600100int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vmai,
101 enum pci_mmap_api mmap_api);
David Woodhousef7195822017-04-12 13:25:59 +0100102
Amey Narkhedee20afa02021-08-17 23:34:54 +0530103bool pci_reset_supported(struct pci_dev *dev);
104void pci_init_reset_methods(struct pci_dev *dev);
Sinan Kaya381634c2018-07-19 18:04:11 -0500105int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
Keith Buschc4eed622018-09-20 10:27:11 -0600106int pci_bus_error_reset(struct pci_dev *dev);
Kumar Galace5ccde2007-07-16 23:27:10 -0500107
Bjorn Helgaasf0ab0012021-08-02 17:17:28 -0500108struct pci_cap_saved_data {
109 u16 cap_nr;
110 bool cap_extended;
111 unsigned int size;
112 u32 data[];
113};
114
115struct pci_cap_saved_state {
116 struct hlist_node next;
117 struct pci_cap_saved_data cap;
118};
119
120void pci_allocate_cap_save_buffers(struct pci_dev *dev);
121void pci_free_cap_save_buffers(struct pci_dev *dev);
122int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size);
123int pci_add_ext_cap_save_buffer(struct pci_dev *dev,
124 u16 cap, unsigned int size);
125struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap);
126struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev,
127 u16 cap);
128
Bjorn Helgaas638c133e2020-09-29 14:24:11 -0500129#define PCI_PM_D2_DELAY 200 /* usec; see PCIe r4.0, sec 5.9.1 */
130#define PCI_PM_D3HOT_WAIT 10 /* msec */
131#define PCI_PM_D3COLD_WAIT 100 /* msec */
Kelsey Skunbergc776dd52019-07-24 17:38:38 -0600132
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600133void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
Rafael J. Wysockib51033e2019-06-25 14:09:12 +0200134void pci_refresh_power_state(struct pci_dev *dev);
Rafael J. Wysockiadfac8f2019-11-05 11:27:49 +0100135int pci_power_up(struct pci_dev *dev);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600136void pci_disable_enabled_device(struct pci_dev *dev);
137int pci_finish_runtime_suspend(struct pci_dev *dev);
Bjorn Helgaas600a5b42020-07-16 17:34:30 -0500138void pcie_clear_device_status(struct pci_dev *dev);
Bjorn Helgaasdcb04532018-03-09 11:06:53 -0600139void pcie_clear_root_pme_status(struct pci_dev *dev);
Kelsey Skunberg669696e2019-07-24 17:38:39 -0600140bool pci_check_pme_status(struct pci_dev *dev);
141void pci_pme_wakeup_bus(struct pci_bus *bus);
Rafael J. Wysocki0ce3fca2017-07-12 03:05:39 +0200142void pci_pme_restore(struct pci_dev *dev);
Rafael J. Wysocki0c7376a2019-06-07 00:32:31 +0200143bool pci_dev_need_resume(struct pci_dev *dev);
144void pci_dev_adjust_pme(struct pci_dev *dev);
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +0200145void pci_dev_complete_resume(struct pci_dev *pci_dev);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600146void pci_config_pm_runtime_get(struct pci_dev *dev);
147void pci_config_pm_runtime_put(struct pci_dev *dev);
148void pci_pm_init(struct pci_dev *dev);
Sean O. Stalley938174e2015-10-29 17:35:39 -0500149void pci_ea_init(struct pci_dev *dev);
Bjorn Helgaascbc40d52020-12-03 12:51:08 -0600150void pci_msi_init(struct pci_dev *dev);
151void pci_msix_init(struct pci_dev *dev);
Lukas Wunnerc6a63302016-10-28 10:52:06 +0200152bool pci_bridge_d3_possible(struct pci_dev *dev);
Lukas Wunner1ed276a2016-10-28 10:52:06 +0200153void pci_bridge_d3_update(struct pci_dev *dev);
Mika Westerberge74b2b52023-04-04 15:32:55 -0500154int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type);
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100155
Bjorn Helgaas87f10fa2024-08-27 18:48:48 -0500156static inline bool pci_bus_rrs_vendor_id(u32 l)
Bjorn Helgaasd591f682024-08-27 18:48:46 -0500157{
158 return (l & 0xffff) == PCI_VENDOR_ID_PCI_SIG;
159}
160
Rafael J. Wysockib6e335a2010-12-29 13:21:23 +0100161static inline void pci_wakeup_event(struct pci_dev *dev)
162{
163 /* Wait 100 ms before the system can be put into a sleep state. */
164 pm_wakeup_event(&dev->dev, 100);
165}
166
Yijing Wang326c1cd2014-05-04 12:23:36 +0800167static inline bool pci_has_subordinate(struct pci_dev *pci_dev)
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100168{
169 return !!(pci_dev->subordinate);
170}
David Shaohua Li0f644742005-03-19 00:15:48 -0500171
Mika Westerberg9d26d3a2016-06-02 11:17:12 +0300172static inline bool pci_power_manageable(struct pci_dev *pci_dev)
173{
174 /*
175 * Currently we allow normal PCI devices and PCI bridges transition
176 * into D3 if their bridge_d3 is set.
177 */
178 return !pci_has_subordinate(pci_dev) || pci_dev->bridge_d3;
179}
180
Mika Westerberg984998e2019-08-22 11:55:52 +0300181static inline bool pcie_downstream_port(const struct pci_dev *dev)
182{
183 int type = pci_pcie_type(dev);
184
185 return type == PCI_EXP_TYPE_ROOT_PORT ||
186 type == PCI_EXP_TYPE_DOWNSTREAM ||
187 type == PCI_EXP_TYPE_PCIE_BRIDGE;
188}
189
Heiner Kallweite947e7b2021-04-01 18:37:47 +0200190void pci_vpd_init(struct pci_dev *dev);
Krzysztof Wilczyńskid93f8392021-04-16 20:58:40 +0000191extern const struct attribute_group pci_dev_vpd_attr_group;
Ben Hutchings94e61082008-03-05 16:52:39 +0000192
Kelsey Skunberg440589d2019-07-24 17:38:41 -0600193/* PCI Virtual Channel */
194int pci_save_vc_state(struct pci_dev *dev);
195void pci_restore_vc_state(struct pci_dev *dev);
196void pci_allocate_vc_save_buffers(struct pci_dev *dev);
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/* PCI /proc functions */
199#ifdef CONFIG_PROC_FS
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600200int pci_proc_attach_device(struct pci_dev *dev);
201int pci_proc_detach_device(struct pci_dev *dev);
202int pci_proc_detach_bus(struct pci_bus *bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#else
204static inline int pci_proc_attach_device(struct pci_dev *dev) { return 0; }
205static inline int pci_proc_detach_device(struct pci_dev *dev) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static inline int pci_proc_detach_bus(struct pci_bus *bus) { return 0; }
207#endif
208
209/* Functions for PCI Hotplug drivers to use */
Yinghai Lua8e4b9c2012-05-18 13:46:34 -0600210int pci_hp_add_bridge(struct pci_dev *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Lukas Wunnerbe9c3a42023-10-30 13:32:12 +0100212#if defined(CONFIG_SYSFS) && defined(HAVE_PCI_LEGACY)
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600213void pci_create_legacy_files(struct pci_bus *bus);
214void pci_remove_legacy_files(struct pci_bus *bus);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000215#else
Bjorn Helgaaseb6723b2023-08-24 13:33:05 -0500216static inline void pci_create_legacy_files(struct pci_bus *bus) { }
217static inline void pci_remove_legacy_files(struct pci_bus *bus) { }
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220/* Lock for read/write access to pci device and bus lists */
Zhang Yanmind71374d2006-06-02 12:35:43 +0800221extern struct rw_semaphore pci_bus_sem;
Keith Buschc4eed622018-09-20 10:27:11 -0600222extern struct mutex pci_slot_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jan Kiszkaa2e27782011-11-04 09:46:00 +0100224extern raw_spinlock_t pci_lock;
225
Krzysztof Wilczyński3789af92020-07-30 21:08:48 +0000226extern unsigned int pci_pm_d3hot_delay;
Michael Ellerman88187df2007-01-25 19:34:07 +1100227
Andrew Morton4b47b0e2005-08-16 15:16:05 -0700228#ifdef CONFIG_PCI_MSI
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700229void pci_no_msi(void);
Andrew Morton4b47b0e2005-08-16 15:16:05 -0700230#else
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700231static inline void pci_no_msi(void) { }
Andrew Morton4b47b0e2005-08-16 15:16:05 -0700232#endif
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100233
Yinghai Lub55438f2012-02-23 19:23:30 -0800234void pci_realloc_get_opt(char *);
Ram Paif483d392011-07-07 11:19:10 -0700235
Kristen Carlson Accardiffadcc22006-07-12 08:59:00 -0700236static inline int pci_no_d1d2(struct pci_dev *dev)
237{
238 unsigned int parent_dstates = 0;
Andrew Morton4b47b0e2005-08-16 15:16:05 -0700239
Kristen Carlson Accardiffadcc22006-07-12 08:59:00 -0700240 if (dev->bus->self)
241 parent_dstates = dev->bus->self->no_d1d2;
242 return (dev->no_d1d2 || parent_dstates);
243
244}
Lukas Wunnerbe9c3a42023-10-30 13:32:12 +0100245
246#ifdef CONFIG_SYSFS
247int pci_create_sysfs_dev_files(struct pci_dev *pdev);
248void pci_remove_sysfs_dev_files(struct pci_dev *pdev);
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700249extern const struct attribute_group *pci_dev_groups[];
Lukas Wunnerbe9c3a42023-10-30 13:32:12 +0100250extern const struct attribute_group *pci_dev_attr_groups[];
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700251extern const struct attribute_group *pcibus_groups[];
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600252extern const struct attribute_group *pci_bus_groups[];
Lukas Wunnerbe9c3a42023-10-30 13:32:12 +0100253#else
254static inline int pci_create_sysfs_dev_files(struct pci_dev *pdev) { return 0; }
255static inline void pci_remove_sysfs_dev_files(struct pci_dev *pdev) { }
256#define pci_dev_groups NULL
257#define pci_dev_attr_groups NULL
258#define pcibus_groups NULL
259#define pci_bus_groups NULL
260#endif
Alex Chiang705b1aa2009-03-20 14:56:31 -0600261
Kelsey Skunberg003d3b22019-07-24 17:38:42 -0600262extern unsigned long pci_hotplug_io_size;
Nicholas Johnsond7b8a212019-10-23 12:12:29 +0000263extern unsigned long pci_hotplug_mmio_size;
264extern unsigned long pci_hotplug_mmio_pref_size;
Kelsey Skunberg003d3b22019-07-24 17:38:42 -0600265extern unsigned long pci_hotplug_bus_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267/**
268 * pci_match_one_device - Tell if a PCI device structure has a matching
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -0600269 * PCI device id structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * @id: single PCI device id structure to match
271 * @dev: the PCI device structure to match against
Greg Kroah-Hartman367b09f2008-01-30 15:21:33 -0800272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * Returns the matching pci_device_id structure or %NULL if there is no match.
274 */
275static inline const struct pci_device_id *
276pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
277{
278 if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) &&
279 (id->device == PCI_ANY_ID || id->device == dev->device) &&
280 (id->subvendor == PCI_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
281 (id->subdevice == PCI_ANY_ID || id->subdevice == dev->subsystem_device) &&
282 !((id->class ^ dev->class) & id->class_mask))
283 return id;
284 return NULL;
285}
286
Alex Chiangf46753c2008-06-10 15:28:50 -0600287/* PCI slot sysfs helper code */
288#define to_pci_slot(s) container_of(s, struct pci_slot, kobj)
289
290extern struct kset *pci_slots_kset;
291
292struct pci_slot_attribute {
293 struct attribute attr;
294 ssize_t (*show)(struct pci_slot *, char *);
295 ssize_t (*store)(struct pci_slot *, const char *, size_t);
296};
297#define to_pci_slot_attr(s) container_of(s, struct pci_slot_attribute, attr)
298
Yu Zhao0b400c72008-11-22 02:40:40 +0800299enum pci_bar_type {
300 pci_bar_unknown, /* Standard PCI BAR probe */
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -0600301 pci_bar_io, /* An I/O port BAR */
Yu Zhao0b400c72008-11-22 02:40:40 +0800302 pci_bar_mem32, /* A 32-bit memory BAR */
303 pci_bar_mem64, /* A 64-bit memory BAR */
304};
305
Kelsey Skunberg975e1ac2019-07-24 17:38:40 -0600306struct device *pci_get_host_bridge_device(struct pci_dev *dev);
307void pci_put_host_bridge_device(struct device *dev);
308
Sinan Kaya62ce94a2017-07-12 00:04:14 -0400309int pci_configure_extended_tags(struct pci_dev *dev, void *ign);
Yinghai Luefdc87d2012-01-27 10:55:10 -0800310bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *pl,
Bjorn Helgaas87f10fa2024-08-27 18:48:48 -0500311 int rrs_timeout);
James Puthukattukaranaa667c62018-07-09 11:31:25 -0400312bool pci_bus_generic_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *pl,
Bjorn Helgaas87f10fa2024-08-27 18:48:48 -0500313 int rrs_timeout);
314int pci_idt_bus_quirk(struct pci_bus *bus, int devfn, u32 *pl, int rrs_timeout);
James Puthukattukaranaa667c62018-07-09 11:31:25 -0400315
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600316int pci_setup_device(struct pci_dev *dev);
317int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
318 struct resource *res, unsigned int reg);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600319void pci_configure_ari(struct pci_dev *dev);
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -0600320void __pci_bus_size_bridges(struct pci_bus *bus,
Jiang Liud66ecb72013-06-23 01:01:35 +0200321 struct list_head *realloc_head);
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -0600322void __pci_bus_assign_resources(const struct pci_bus *bus,
323 struct list_head *realloc_head,
324 struct list_head *fail_head);
Yinghai Lu0f7e7ae2015-01-15 16:21:49 -0600325bool pci_bus_clip_resource(struct pci_dev *dev, int idx);
Bjorn Helgaas939de1d2013-01-26 17:35:58 -0700326
Puranjay Mohan65f8e0b2021-11-06 16:56:05 +0530327const char *pci_resource_name(struct pci_dev *dev, unsigned int i);
328
Yinghai Lu2069ecf2012-02-15 21:40:31 -0800329void pci_reassigndev_resource_alignment(struct pci_dev *dev);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600330void pci_disable_bridge_window(struct pci_dev *dev);
Kelsey Skunbergecd29c12019-07-24 17:38:43 -0600331struct pci_bus *pci_bus_get(struct pci_bus *bus);
332void pci_bus_put(struct pci_bus *bus);
Yuji Shimada32a9a6822009-03-16 17:13:39 +0900333
Yicong Yang757bfaa2020-02-17 19:13:03 +0800334/* PCIe link information from Link Capabilities 2 */
335#define PCIE_LNKCAP2_SLS2SPEED(lnkcap2) \
Gustavo Pimentel34191742020-11-18 23:49:20 +0100336 ((lnkcap2) & PCI_EXP_LNKCAP2_SLS_64_0GB ? PCIE_SPEED_64_0GT : \
337 (lnkcap2) & PCI_EXP_LNKCAP2_SLS_32_0GB ? PCIE_SPEED_32_0GT : \
Yicong Yang757bfaa2020-02-17 19:13:03 +0800338 (lnkcap2) & PCI_EXP_LNKCAP2_SLS_16_0GB ? PCIE_SPEED_16_0GT : \
339 (lnkcap2) & PCI_EXP_LNKCAP2_SLS_8_0GB ? PCIE_SPEED_8_0GT : \
340 (lnkcap2) & PCI_EXP_LNKCAP2_SLS_5_0GB ? PCIE_SPEED_5_0GT : \
341 (lnkcap2) & PCI_EXP_LNKCAP2_SLS_2_5GB ? PCIE_SPEED_2_5GT : \
342 PCI_SPEED_UNKNOWN)
Tal Gilboa6cf57be2018-03-30 07:44:05 -0500343
Tal Gilboab852f632018-03-30 08:32:03 -0500344/* PCIe speed to Mb/s reduced by encoding overhead */
345#define PCIE_SPEED2MBS_ENC(speed) \
Ilpo Järvinenac4f1892024-01-02 19:27:00 +0200346 ((speed) == PCIE_SPEED_64_0GT ? 64000*1/1 : \
Gustavo Pimentel34191742020-11-18 23:49:20 +0100347 (speed) == PCIE_SPEED_32_0GT ? 32000*128/130 : \
Yicong Yang9cb39852020-02-17 19:12:55 +0800348 (speed) == PCIE_SPEED_16_0GT ? 16000*128/130 : \
Tal Gilboab852f632018-03-30 08:32:03 -0500349 (speed) == PCIE_SPEED_8_0GT ? 8000*128/130 : \
350 (speed) == PCIE_SPEED_5_0GT ? 5000*8/10 : \
351 (speed) == PCIE_SPEED_2_5GT ? 2500*8/10 : \
352 0)
353
Krishna chaitanya chundru100ae5d2024-06-19 20:41:12 +0530354static inline int pcie_dev_speed_mbps(enum pci_bus_speed speed)
355{
356 switch (speed) {
357 case PCIE_SPEED_2_5GT:
358 return 2500;
359 case PCIE_SPEED_5_0GT:
360 return 5000;
361 case PCIE_SPEED_8_0GT:
362 return 8000;
363 case PCIE_SPEED_16_0GT:
364 return 16000;
365 case PCIE_SPEED_32_0GT:
366 return 32000;
367 case PCIE_SPEED_64_0GT:
368 return 64000;
369 default:
370 break;
371 }
372
373 return -EINVAL;
374}
375
Bjorn Helgaase56faff2020-02-28 15:02:03 -0600376const char *pci_speed_string(enum pci_bus_speed speed);
Tal Gilboa6cf57be2018-03-30 07:44:05 -0500377enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev);
Tal Gilboac70b65f2018-03-30 08:24:36 -0500378enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev);
Alexandru Gagniuc2d1ce5e2018-08-06 18:25:35 -0500379void __pcie_print_link_status(struct pci_dev *dev, bool verbose);
Lukas Wunner0fa635a2019-03-20 12:05:30 +0100380void pcie_report_downtraining(struct pci_dev *dev);
Kelsey Skunberg5da78d92019-07-24 17:38:44 -0600381void pcie_update_link_speed(struct pci_bus *bus, u16 link_status);
Tal Gilboa6cf57be2018-03-30 07:44:05 -0500382
Yu Zhaod1b054d2009-03-20 11:25:11 +0800383/* Single Root I/O Virtualization */
384struct pci_sriov {
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -0600385 int pos; /* Capability position */
386 int nres; /* Number of resources */
387 u32 cap; /* SR-IOV Capabilities */
388 u16 ctrl; /* SR-IOV Control */
389 u16 total_VFs; /* Total VFs associated with the PF */
390 u16 initial_VFs; /* Initial VFs associated with the PF */
391 u16 num_VFs; /* Number of VFs available */
392 u16 offset; /* First VF Routing ID offset */
393 u16 stride; /* Following VF stride */
394 u16 vf_device; /* VF device ID */
395 u32 pgsz; /* Page size for BAR alignment */
396 u8 link; /* Function Dependency Link */
397 u8 max_VF_buses; /* Max buses consumed by VFs */
398 u16 driver_max_VFs; /* Max num VFs driver supports */
399 struct pci_dev *dev; /* Lowest numbered PF */
400 struct pci_dev *self; /* This PF */
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100401 u32 class; /* VF device */
402 u8 hdr_type; /* VF header type */
403 u16 subsystem_vendor; /* VF subsystem vendor */
404 u16 subsystem_device; /* VF subsystem device */
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -0600405 resource_size_t barsz[PCI_SRIOV_NUM_BARS]; /* VF BAR size */
406 bool drivers_autoprobe; /* Auto probing of VFs by driver */
Yu Zhaod1b054d2009-03-20 11:25:11 +0800407};
408
Lukas Wunnerac048402023-03-11 15:40:12 +0100409#ifdef CONFIG_PCI_DOE
410void pci_doe_init(struct pci_dev *pdev);
411void pci_doe_destroy(struct pci_dev *pdev);
412void pci_doe_disconnected(struct pci_dev *pdev);
413#else
414static inline void pci_doe_init(struct pci_dev *pdev) { }
415static inline void pci_doe_destroy(struct pci_dev *pdev) { }
416static inline void pci_doe_disconnected(struct pci_dev *pdev) { }
417#endif
418
Mariusz Tkaczyk4e893542024-09-04 12:48:47 +0200419#ifdef CONFIG_PCI_NPEM
420void pci_npem_create(struct pci_dev *dev);
421void pci_npem_remove(struct pci_dev *dev);
422#else
423static inline void pci_npem_create(struct pci_dev *dev) { }
424static inline void pci_npem_remove(struct pci_dev *dev) { }
425#endif
426
Keith Buscha6bd1012018-09-20 10:27:16 -0600427/**
428 * pci_dev_set_io_state - Set the new error state if possible.
429 *
Krzysztof Wilczyński347269c2021-07-03 15:13:02 +0000430 * @dev: PCI device to set new error_state
431 * @new: the state we want dev to be in
Keith Buscha6bd1012018-09-20 10:27:16 -0600432 *
Lukas Wunner74ff8862023-01-20 10:19:02 +0100433 * If the device is experiencing perm_failure, it has to remain in that state.
434 * Any other transition is allowed.
Keith Buscha6bd1012018-09-20 10:27:16 -0600435 *
436 * Returns true if state has been changed to the requested state.
437 */
438static inline bool pci_dev_set_io_state(struct pci_dev *dev,
439 pci_channel_state_t new)
440{
Lukas Wunner74ff8862023-01-20 10:19:02 +0100441 pci_channel_state_t old;
Keith Buscha6bd1012018-09-20 10:27:16 -0600442
Keith Buscha6bd1012018-09-20 10:27:16 -0600443 switch (new) {
444 case pci_channel_io_perm_failure:
Lukas Wunner74ff8862023-01-20 10:19:02 +0100445 xchg(&dev->error_state, pci_channel_io_perm_failure);
446 return true;
Keith Buscha6bd1012018-09-20 10:27:16 -0600447 case pci_channel_io_frozen:
Lukas Wunner74ff8862023-01-20 10:19:02 +0100448 old = cmpxchg(&dev->error_state, pci_channel_io_normal,
449 pci_channel_io_frozen);
450 return old != pci_channel_io_perm_failure;
Keith Buscha6bd1012018-09-20 10:27:16 -0600451 case pci_channel_io_normal:
Lukas Wunner74ff8862023-01-20 10:19:02 +0100452 old = cmpxchg(&dev->error_state, pci_channel_io_frozen,
453 pci_channel_io_normal);
454 return old != pci_channel_io_perm_failure;
455 default:
456 return false;
Keith Buscha6bd1012018-09-20 10:27:16 -0600457 }
Keith Buscha6bd1012018-09-20 10:27:16 -0600458}
Keith Busch89ee9f72017-03-29 22:48:59 -0500459
460static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused)
461{
Keith Buscha6bd1012018-09-20 10:27:16 -0600462 pci_dev_set_io_state(dev, pci_channel_io_perm_failure);
Lukas Wunnerac048402023-03-11 15:40:12 +0100463 pci_doe_disconnected(dev);
Keith Buscha6bd1012018-09-20 10:27:16 -0600464
Keith Busch89ee9f72017-03-29 22:48:59 -0500465 return 0;
466}
467
Keith Buscha6bd1012018-09-20 10:27:16 -0600468/* pci_dev priv_flags */
469#define PCI_DEV_ADDED 0
Lukas Wunnera97396c2021-05-01 10:29:00 +0200470#define PCI_DPC_RECOVERED 1
471#define PCI_DPC_RECOVERING 2
Keith Buscha6bd1012018-09-20 10:27:16 -0600472
Hari Vyas44bda4b2018-07-03 14:35:41 +0530473static inline void pci_dev_assign_added(struct pci_dev *dev, bool added)
474{
475 assign_bit(PCI_DEV_ADDED, &dev->priv_flags, added);
476}
477
478static inline bool pci_dev_is_added(const struct pci_dev *dev)
479{
480 return test_bit(PCI_DEV_ADDED, &dev->priv_flags);
481}
482
Keith Busch1e451162018-07-19 16:16:55 -0500483#ifdef CONFIG_PCIEAER
484#include <linux/aer.h>
485
486#define AER_MAX_MULTI_ERR_DEVICES 5 /* Not likely to have more */
487
488struct aer_err_info {
489 struct pci_dev *dev[AER_MAX_MULTI_ERR_DEVICES];
490 int error_dev_num;
491
492 unsigned int id:16;
493
494 unsigned int severity:2; /* 0:NONFATAL | 1:FATAL | 2:COR */
495 unsigned int __pad1:5;
496 unsigned int multi_error_valid:1;
497
498 unsigned int first_error:5;
499 unsigned int __pad2:2;
500 unsigned int tlp_header_valid:1;
501
502 unsigned int status; /* COR/UNCOR Error Status */
503 unsigned int mask; /* COR/UNCOR Error Mask */
Ilpo Järvinen0a5a46a2024-02-06 15:57:15 +0200504 struct pcie_tlp_log tlp; /* TLP Header */
Keith Busch1e451162018-07-19 16:16:55 -0500505};
506
507int aer_get_device_error_info(struct pci_dev *dev, struct aer_err_info *info);
508void aer_print_error(struct pci_dev *dev, struct aer_err_info *info);
509#endif /* CONFIG_PCIEAER */
510
Sean V Kelley90655632020-11-20 16:10:24 -0800511#ifdef CONFIG_PCIEPORTBUS
512/* Cached RCEC Endpoint Association */
513struct rcec_ea {
514 u8 nextbusn;
515 u8 lastbusn;
516 u32 bitmap;
517};
518#endif
519
Keith Busch4f802172018-09-20 10:27:08 -0600520#ifdef CONFIG_PCIE_DPC
521void pci_save_dpc_state(struct pci_dev *dev);
522void pci_restore_dpc_state(struct pci_dev *dev);
Kuppuswamy Sathyanarayanan27005612020-03-23 17:26:04 -0700523void pci_dpc_init(struct pci_dev *pdev);
Kuppuswamy Sathyanarayananaea47412020-03-23 17:26:06 -0700524void dpc_process_error(struct pci_dev *pdev);
525pci_ers_result_t dpc_reset_link(struct pci_dev *pdev);
Lukas Wunnera97396c2021-05-01 10:29:00 +0200526bool pci_dpc_recovered(struct pci_dev *pdev);
Keith Busch4f802172018-09-20 10:27:08 -0600527#else
Bjorn Helgaaseb6723b2023-08-24 13:33:05 -0500528static inline void pci_save_dpc_state(struct pci_dev *dev) { }
529static inline void pci_restore_dpc_state(struct pci_dev *dev) { }
530static inline void pci_dpc_init(struct pci_dev *pdev) { }
Lukas Wunnera97396c2021-05-01 10:29:00 +0200531static inline bool pci_dpc_recovered(struct pci_dev *pdev) { return false; }
Keith Busch4f802172018-09-20 10:27:08 -0600532#endif
533
Sean V Kelley90655632020-11-20 16:10:24 -0800534#ifdef CONFIG_PCIEPORTBUS
535void pci_rcec_init(struct pci_dev *dev);
536void pci_rcec_exit(struct pci_dev *dev);
Sean V Kelley507b4602020-11-20 16:10:32 -0800537void pcie_link_rcec(struct pci_dev *rcec);
Sean V Kelleyaf113552020-11-20 16:10:34 -0800538void pcie_walk_rcec(struct pci_dev *rcec,
539 int (*cb)(struct pci_dev *, void *),
540 void *userdata);
Sean V Kelley90655632020-11-20 16:10:24 -0800541#else
Bjorn Helgaaseb6723b2023-08-24 13:33:05 -0500542static inline void pci_rcec_init(struct pci_dev *dev) { }
543static inline void pci_rcec_exit(struct pci_dev *dev) { }
544static inline void pcie_link_rcec(struct pci_dev *rcec) { }
Sean V Kelleyaf113552020-11-20 16:10:34 -0800545static inline void pcie_walk_rcec(struct pci_dev *rcec,
546 int (*cb)(struct pci_dev *, void *),
Bjorn Helgaaseb6723b2023-08-24 13:33:05 -0500547 void *userdata) { }
Sean V Kelley90655632020-11-20 16:10:24 -0800548#endif
549
Hao, Xudong1900ca12011-12-17 21:24:40 +0800550#ifdef CONFIG_PCI_ATS
Kelsey Skunbergb92b5122019-07-24 17:38:45 -0600551/* Address Translation Service */
552void pci_ats_init(struct pci_dev *dev);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600553void pci_restore_ats_state(struct pci_dev *dev);
Hao, Xudong1900ca12011-12-17 21:24:40 +0800554#else
Kelsey Skunbergb92b5122019-07-24 17:38:45 -0600555static inline void pci_ats_init(struct pci_dev *d) { }
556static inline void pci_restore_ats_state(struct pci_dev *dev) { }
Hao, Xudong1900ca12011-12-17 21:24:40 +0800557#endif /* CONFIG_PCI_ATS */
558
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500559#ifdef CONFIG_PCI_PRI
560void pci_pri_init(struct pci_dev *dev);
Bjorn Helgaasfef2dd82019-10-09 16:47:15 -0500561void pci_restore_pri_state(struct pci_dev *pdev);
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500562#else
563static inline void pci_pri_init(struct pci_dev *dev) { }
Bjorn Helgaasfef2dd82019-10-09 16:47:15 -0500564static inline void pci_restore_pri_state(struct pci_dev *pdev) { }
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500565#endif
566
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500567#ifdef CONFIG_PCI_PASID
568void pci_pasid_init(struct pci_dev *dev);
Bjorn Helgaasfef2dd82019-10-09 16:47:15 -0500569void pci_restore_pasid_state(struct pci_dev *pdev);
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500570#else
571static inline void pci_pasid_init(struct pci_dev *dev) { }
Bjorn Helgaasfef2dd82019-10-09 16:47:15 -0500572static inline void pci_restore_pasid_state(struct pci_dev *pdev) { }
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500573#endif
574
Yu Zhaod1b054d2009-03-20 11:25:11 +0800575#ifdef CONFIG_PCI_IOV
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600576int pci_iov_init(struct pci_dev *dev);
577void pci_iov_release(struct pci_dev *dev);
Jakub Kicinski38972372018-06-29 15:08:52 -0500578void pci_iov_remove(struct pci_dev *dev);
Bjorn Helgaas6ffa2482016-11-28 09:15:52 -0600579void pci_iov_update_resource(struct pci_dev *dev, int resno);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600580resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno);
581void pci_restore_iov_state(struct pci_dev *dev);
582int pci_iov_bus_range(struct pci_bus *bus);
Leon Romanovskyc3d5c2d2021-04-04 10:22:18 +0300583extern const struct attribute_group sriov_pf_dev_attr_group;
584extern const struct attribute_group sriov_vf_dev_attr_group;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800585#else
586static inline int pci_iov_init(struct pci_dev *dev)
587{
588 return -ENODEV;
589}
Bjorn Helgaaseb6723b2023-08-24 13:33:05 -0500590static inline void pci_iov_release(struct pci_dev *dev) { }
591static inline void pci_iov_remove(struct pci_dev *dev) { }
592static inline void pci_restore_iov_state(struct pci_dev *dev) { }
Yu Zhaoa28724b2009-03-20 11:25:13 +0800593static inline int pci_iov_bus_range(struct pci_bus *bus)
594{
595 return 0;
596}
Yu Zhao302b4212009-05-18 13:51:32 +0800597
Yu Zhaod1b054d2009-03-20 11:25:11 +0800598#endif /* CONFIG_PCI_IOV */
599
David E. Box39850ed52020-12-07 14:39:50 -0800600#ifdef CONFIG_PCIE_PTM
Bjorn Helgaasd736d292022-09-09 15:25:04 -0500601void pci_ptm_init(struct pci_dev *dev);
David E. Box39850ed52020-12-07 14:39:50 -0800602void pci_save_ptm_state(struct pci_dev *dev);
603void pci_restore_ptm_state(struct pci_dev *dev);
Bjorn Helgaase8bdc5e2022-09-09 15:25:00 -0500604void pci_suspend_ptm(struct pci_dev *dev);
605void pci_resume_ptm(struct pci_dev *dev);
David E. Box39850ed52020-12-07 14:39:50 -0800606#else
Bjorn Helgaasd736d292022-09-09 15:25:04 -0500607static inline void pci_ptm_init(struct pci_dev *dev) { }
David E. Box39850ed52020-12-07 14:39:50 -0800608static inline void pci_save_ptm_state(struct pci_dev *dev) { }
609static inline void pci_restore_ptm_state(struct pci_dev *dev) { }
Bjorn Helgaase8bdc5e2022-09-09 15:25:00 -0500610static inline void pci_suspend_ptm(struct pci_dev *dev) { }
611static inline void pci_resume_ptm(struct pci_dev *dev) { }
David E. Box39850ed52020-12-07 14:39:50 -0800612#endif
613
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600614unsigned long pci_cardbus_resource_alignment(struct resource *);
Ram Pai0a2daa12011-07-25 13:08:41 -0700615
Cam Macdonell0e522472010-09-07 17:25:20 -0700616static inline resource_size_t pci_resource_alignment(struct pci_dev *dev,
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600617 struct resource *res)
Chris Wright6faf17f2009-08-28 13:00:06 -0700618{
619#ifdef CONFIG_PCI_IOV
620 int resno = res - dev->resource;
621
622 if (resno >= PCI_IOV_RESOURCES && resno <= PCI_IOV_RESOURCE_END)
623 return pci_sriov_resource_alignment(dev, resno);
624#endif
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -0600625 if (dev->class >> 8 == PCI_CLASS_BRIDGE_CARDBUS)
Ram Pai0a2daa12011-07-25 13:08:41 -0700626 return pci_cardbus_resource_alignment(res);
Chris Wright6faf17f2009-08-28 13:00:06 -0700627 return resource_alignment(res);
628}
629
Rajat Jain52fbf5b2020-07-07 15:46:02 -0700630void pci_acs_init(struct pci_dev *dev);
Bjorn Helgaasbd2e9562018-08-09 16:19:52 -0500631#ifdef CONFIG_PCI_QUIRKS
632int pci_dev_specific_acs_enabled(struct pci_dev *dev, u16 acs_flags);
633int pci_dev_specific_enable_acs(struct pci_dev *dev);
Logan Gunthorpe73c47dde2018-08-09 16:51:43 -0500634int pci_dev_specific_disable_acs_redir(struct pci_dev *dev);
Maciej W. Rozycki59100eb2024-08-09 14:25:02 +0100635int pcie_failed_link_retrain(struct pci_dev *dev);
Bjorn Helgaasbd2e9562018-08-09 16:19:52 -0500636#else
637static inline int pci_dev_specific_acs_enabled(struct pci_dev *dev,
638 u16 acs_flags)
639{
640 return -ENOTTY;
641}
642static inline int pci_dev_specific_enable_acs(struct pci_dev *dev)
643{
644 return -ENOTTY;
645}
Logan Gunthorpe73c47dde2018-08-09 16:51:43 -0500646static inline int pci_dev_specific_disable_acs_redir(struct pci_dev *dev)
647{
648 return -ENOTTY;
649}
Maciej W. Rozycki59100eb2024-08-09 14:25:02 +0100650static inline int pcie_failed_link_retrain(struct pci_dev *dev)
Maciej W. Rozyckia89c82242023-06-11 18:20:10 +0100651{
Maciej W. Rozycki59100eb2024-08-09 14:25:02 +0100652 return -ENOTTY;
Maciej W. Rozyckia89c82242023-06-11 18:20:10 +0100653}
Bjorn Helgaasbd2e9562018-08-09 16:19:52 -0500654#endif
Allen Kayae21ee62009-10-07 10:27:17 -0700655
Oza Pawandeep2e28bc82018-05-17 16:44:15 -0500656/* PCI error reporting and recovery */
Kuppuswamy Sathyanarayanane8e5ff22020-03-23 17:26:03 -0700657pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
Sean V Kelley8f1bbfb2020-11-20 16:10:25 -0800658 pci_channel_state_t state,
659 pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev));
Oza Pawandeep2e28bc82018-05-17 16:44:15 -0500660
Oza Pawandeep9f5a70f12018-05-17 16:44:11 -0500661bool pcie_wait_for_link(struct pci_dev *pdev, bool active);
Bjorn Helgaas1abb4732023-06-26 12:59:56 -0500662int pcie_retrain_link(struct pci_dev *pdev, bool use_lt);
David E. Box1e11b542024-02-23 14:58:49 -0600663
664/* ASPM-related functionality we need even without CONFIG_PCIEASPM */
665void pci_save_ltr_state(struct pci_dev *dev);
666void pci_restore_ltr_state(struct pci_dev *dev);
David E. Box17423362024-02-23 14:58:50 -0600667void pci_configure_aspm_l1ss(struct pci_dev *dev);
668void pci_save_aspm_l1ss_state(struct pci_dev *dev);
669void pci_restore_aspm_l1ss_state(struct pci_dev *dev);
David E. Box1e11b542024-02-23 14:58:49 -0600670
Bjorn Helgaas7d8e7d12017-12-15 08:57:28 -0600671#ifdef CONFIG_PCIEASPM
672void pcie_aspm_init_link_state(struct pci_dev *pdev);
673void pcie_aspm_exit_link_state(struct pci_dev *pdev);
Johan Hovold1e560862024-01-30 11:02:43 +0100674void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked);
Bjorn Helgaas7d8e7d12017-12-15 08:57:28 -0600675void pcie_aspm_powersave_config_link(struct pci_dev *pdev);
David E. Boxfa84f442024-02-23 14:58:47 -0600676void pci_configure_ltr(struct pci_dev *pdev);
677void pci_bridge_reconfigure_ltr(struct pci_dev *pdev);
Bjorn Helgaas7d8e7d12017-12-15 08:57:28 -0600678#else
679static inline void pcie_aspm_init_link_state(struct pci_dev *pdev) { }
680static inline void pcie_aspm_exit_link_state(struct pci_dev *pdev) { }
Johan Hovold1e560862024-01-30 11:02:43 +0100681static inline void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked) { }
Bjorn Helgaas7d8e7d12017-12-15 08:57:28 -0600682static inline void pcie_aspm_powersave_config_link(struct pci_dev *pdev) { }
David E. Boxfa84f442024-02-23 14:58:47 -0600683static inline void pci_configure_ltr(struct pci_dev *pdev) { }
684static inline void pci_bridge_reconfigure_ltr(struct pci_dev *pdev) { }
Bjorn Helgaas7d8e7d12017-12-15 08:57:28 -0600685#endif
686
Kelsey Skunberg72bde9c2019-07-24 17:38:46 -0600687#ifdef CONFIG_PCIE_ECRC
688void pcie_set_ecrc_checking(struct pci_dev *dev);
689void pcie_ecrc_get_policy(char *str);
690#else
691static inline void pcie_set_ecrc_checking(struct pci_dev *dev) { }
692static inline void pcie_ecrc_get_policy(char *str) { }
693#endif
694
Dexuan Cuib9c3b262009-12-07 13:03:21 +0800695struct pci_dev_reset_methods {
696 u16 vendor;
697 u16 device;
Amey Narkhede9bdc81c2021-08-17 23:35:00 +0530698 int (*reset)(struct pci_dev *dev, bool probe);
Dexuan Cuib9c3b262009-12-07 13:03:21 +0800699};
700
Amey Narkhedee20afa02021-08-17 23:34:54 +0530701struct pci_reset_fn_method {
Amey Narkhede9bdc81c2021-08-17 23:35:00 +0530702 int (*reset_fn)(struct pci_dev *pdev, bool probe);
Amey Narkhedee20afa02021-08-17 23:34:54 +0530703 char *name;
Dexuan Cuib9c3b262009-12-07 13:03:21 +0800704};
705
Rafael J. Wysocki93177a72010-01-02 22:57:24 +0100706#ifdef CONFIG_PCI_QUIRKS
Amey Narkhede9bdc81c2021-08-17 23:35:00 +0530707int pci_dev_specific_reset(struct pci_dev *dev, bool probe);
Rafael J. Wysocki93177a72010-01-02 22:57:24 +0100708#else
Amey Narkhede9bdc81c2021-08-17 23:35:00 +0530709static inline int pci_dev_specific_reset(struct pci_dev *dev, bool probe)
Rafael J. Wysocki93177a72010-01-02 22:57:24 +0100710{
711 return -ENOTTY;
712}
713#endif
Dexuan Cuib9c3b262009-12-07 13:03:21 +0800714
Dongdong Liu169de962016-12-01 00:33:42 -0600715#if defined(CONFIG_PCI_QUIRKS) && defined(CONFIG_ARM64)
716int acpi_get_rc_resources(struct device *dev, const char *hid, u16 segment,
717 struct resource *res);
Arnd Bergmann16f7ae592021-03-08 16:24:47 +0100718#else
719static inline int acpi_get_rc_resources(struct device *dev, const char *hid,
720 u16 segment, struct resource *res)
721{
722 return -ENODEV;
723}
Dongdong Liu169de962016-12-01 00:33:42 -0600724#endif
725
Christian König276b7382017-10-24 14:40:20 -0500726int pci_rebar_get_current_size(struct pci_dev *pdev, int bar);
727int pci_rebar_set_size(struct pci_dev *pdev, int bar, int size);
728static inline u64 pci_rebar_size_to_bytes(int size)
729{
730 return 1ULL << (size + 20);
731}
732
Rob Herring9e2aee82018-05-11 12:15:30 -0500733struct device_node;
734
735#ifdef CONFIG_OF
736int of_pci_parse_bus_range(struct device_node *node, struct resource *res);
737int of_get_pci_domain_nr(struct device_node *node);
738int of_pci_get_max_link_speed(struct device_node *node);
Pali Rohár35662422022-04-12 11:49:45 +0200739u32 of_pci_get_slot_power_limit(struct device_node *node,
740 u8 *slot_power_limit_value,
741 u8 *slot_power_limit_scale);
Vidya Sagar407abde2024-05-08 23:11:36 +0530742bool of_pci_preserve_config(struct device_node *node);
Rob Herring0d21e712023-04-19 14:35:13 -0500743int pci_set_of_node(struct pci_dev *dev);
Kelsey Skunberg621f7e32019-07-24 17:38:48 -0600744void pci_release_of_node(struct pci_dev *dev);
745void pci_set_bus_of_node(struct pci_bus *bus);
746void pci_release_bus_of_node(struct pci_bus *bus);
Rob Herring9e2aee82018-05-11 12:15:30 -0500747
Rob Herring669cbc72020-07-21 20:25:13 -0600748int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge);
749
Rob Herring9e2aee82018-05-11 12:15:30 -0500750#else
751static inline int
752of_pci_parse_bus_range(struct device_node *node, struct resource *res)
753{
754 return -EINVAL;
755}
756
757static inline int
758of_get_pci_domain_nr(struct device_node *node)
759{
760 return -1;
761}
762
763static inline int
764of_pci_get_max_link_speed(struct device_node *node)
765{
766 return -EINVAL;
767}
Kelsey Skunberg621f7e32019-07-24 17:38:48 -0600768
Pali Rohár35662422022-04-12 11:49:45 +0200769static inline u32
770of_pci_get_slot_power_limit(struct device_node *node,
771 u8 *slot_power_limit_value,
772 u8 *slot_power_limit_scale)
773{
774 if (slot_power_limit_value)
775 *slot_power_limit_value = 0;
776 if (slot_power_limit_scale)
777 *slot_power_limit_scale = 0;
778 return 0;
779}
780
Vidya Sagar407abde2024-05-08 23:11:36 +0530781static inline bool of_pci_preserve_config(struct device_node *node)
782{
783 return false;
784}
785
Rob Herring0d21e712023-04-19 14:35:13 -0500786static inline int pci_set_of_node(struct pci_dev *dev) { return 0; }
Kelsey Skunberg621f7e32019-07-24 17:38:48 -0600787static inline void pci_release_of_node(struct pci_dev *dev) { }
788static inline void pci_set_bus_of_node(struct pci_bus *bus) { }
789static inline void pci_release_bus_of_node(struct pci_bus *bus) { }
Rob Herring669cbc72020-07-21 20:25:13 -0600790
791static inline int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge)
792{
793 return 0;
794}
795
Rob Herring9e2aee82018-05-11 12:15:30 -0500796#endif /* CONFIG_OF */
797
Lizhi Hou407d1a52023-08-15 10:19:57 -0700798struct of_changeset;
799
800#ifdef CONFIG_PCI_DYNAMIC_OF_NODES
801void of_pci_make_dev_node(struct pci_dev *pdev);
802void of_pci_remove_node(struct pci_dev *pdev);
803int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs,
804 struct device_node *np);
805#else
806static inline void of_pci_make_dev_node(struct pci_dev *pdev) { }
807static inline void of_pci_remove_node(struct pci_dev *pdev) { }
808#endif
809
Rajat Jain60ed9822018-06-21 16:48:26 -0700810#ifdef CONFIG_PCIEAER
811void pci_no_aer(void);
812void pci_aer_init(struct pci_dev *dev);
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500813void pci_aer_exit(struct pci_dev *dev);
Rajat Jain81aa5202018-06-21 16:48:28 -0700814extern const struct attribute_group aer_stats_attr_group;
Bjorn Helgaas7ab92e82018-07-19 17:55:58 -0500815void pci_aer_clear_fatal_status(struct pci_dev *dev);
Kuppuswamy Sathyanarayanan894020f2020-03-23 17:26:08 -0700816int pci_aer_clear_status(struct pci_dev *dev);
Kuppuswamy Sathyanarayanan20e15e62020-03-23 17:26:05 -0700817int pci_aer_raw_clear_status(struct pci_dev *dev);
Bjorn Helgaasba3da662023-06-09 17:24:57 -0500818void pci_save_aer_state(struct pci_dev *dev);
819void pci_restore_aer_state(struct pci_dev *dev);
Rajat Jain60ed9822018-06-21 16:48:26 -0700820#else
821static inline void pci_no_aer(void) { }
Jisheng Zhang31f996e2019-04-12 06:43:06 +0000822static inline void pci_aer_init(struct pci_dev *d) { }
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500823static inline void pci_aer_exit(struct pci_dev *d) { }
Bjorn Helgaas7ab92e82018-07-19 17:55:58 -0500824static inline void pci_aer_clear_fatal_status(struct pci_dev *dev) { }
Kuppuswamy Sathyanarayanan894020f2020-03-23 17:26:08 -0700825static inline int pci_aer_clear_status(struct pci_dev *dev) { return -EINVAL; }
Kuppuswamy Sathyanarayanan20e15e62020-03-23 17:26:05 -0700826static inline int pci_aer_raw_clear_status(struct pci_dev *dev) { return -EINVAL; }
Bjorn Helgaasba3da662023-06-09 17:24:57 -0500827static inline void pci_save_aer_state(struct pci_dev *dev) { }
828static inline void pci_restore_aer_state(struct pci_dev *dev) { }
Rajat Jain60ed9822018-06-21 16:48:26 -0700829#endif
830
Krzysztof Wilczynski8c3aac62019-08-27 11:49:50 +0200831#ifdef CONFIG_ACPI
Vidya Sagar9d7d5db2024-05-08 23:11:35 +0530832bool pci_acpi_preserve_config(struct pci_host_bridge *bridge);
Krzysztof Wilczynski4a2dbed2019-08-27 11:49:51 +0200833int pci_acpi_program_hp_params(struct pci_dev *dev);
Krzysztof Wilczyński506140f2021-04-27 10:49:16 -0500834extern const struct attribute_group pci_dev_acpi_attr_group;
Shanker Donthineni3a159552021-08-17 23:34:57 +0530835void pci_set_acpi_fwnode(struct pci_dev *dev);
Amey Narkhede9bdc81c2021-08-17 23:35:00 +0530836int pci_dev_acpi_reset(struct pci_dev *dev, bool probe);
Rafael J. Wysockid97c5d42021-09-20 21:17:08 +0200837bool acpi_pci_power_manageable(struct pci_dev *dev);
838bool acpi_pci_bridge_d3(struct pci_dev *dev);
839int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state);
840pci_power_t acpi_pci_get_power_state(struct pci_dev *dev);
841void acpi_pci_refresh_power_state(struct pci_dev *dev);
842int acpi_pci_wakeup(struct pci_dev *dev, bool enable);
843bool acpi_pci_need_resume(struct pci_dev *dev);
844pci_power_t acpi_pci_choose_state(struct pci_dev *pdev);
Krzysztof Wilczynski8c3aac62019-08-27 11:49:50 +0200845#else
Vidya Sagar9d7d5db2024-05-08 23:11:35 +0530846static inline bool pci_acpi_preserve_config(struct pci_host_bridge *bridge)
847{
848 return false;
849}
Amey Narkhede9bdc81c2021-08-17 23:35:00 +0530850static inline int pci_dev_acpi_reset(struct pci_dev *dev, bool probe)
Shanker Donthineni6937b7d2021-08-17 23:34:59 +0530851{
852 return -ENOTTY;
853}
Bjorn Helgaaseb6723b2023-08-24 13:33:05 -0500854static inline void pci_set_acpi_fwnode(struct pci_dev *dev) { }
Krzysztof Wilczynski4a2dbed2019-08-27 11:49:51 +0200855static inline int pci_acpi_program_hp_params(struct pci_dev *dev)
Krzysztof Wilczynski8c3aac62019-08-27 11:49:50 +0200856{
857 return -ENODEV;
858}
Rafael J. Wysockid97c5d42021-09-20 21:17:08 +0200859static inline bool acpi_pci_power_manageable(struct pci_dev *dev)
860{
861 return false;
862}
863static inline bool acpi_pci_bridge_d3(struct pci_dev *dev)
864{
865 return false;
866}
867static inline int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
868{
869 return -ENODEV;
870}
871static inline pci_power_t acpi_pci_get_power_state(struct pci_dev *dev)
872{
873 return PCI_UNKNOWN;
874}
Bjorn Helgaaseb6723b2023-08-24 13:33:05 -0500875static inline void acpi_pci_refresh_power_state(struct pci_dev *dev) { }
Rafael J. Wysockid97c5d42021-09-20 21:17:08 +0200876static inline int acpi_pci_wakeup(struct pci_dev *dev, bool enable)
877{
878 return -ENODEV;
879}
880static inline bool acpi_pci_need_resume(struct pci_dev *dev)
881{
882 return false;
883}
884static inline pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
885{
886 return PCI_POWER_ERROR;
887}
Krzysztof Wilczynski8c3aac62019-08-27 11:49:50 +0200888#endif
889
Heiner Kallweit72ea91a2019-10-05 14:07:56 +0200890#ifdef CONFIG_PCIEASPM
891extern const struct attribute_group aspm_ctrl_attr_group;
892#endif
893
Amey Narkheded88f5212021-08-17 23:34:56 +0530894extern const struct attribute_group pci_dev_reset_method_attr_group;
895
Rafael J. Wysockid5b0d882021-09-20 21:16:59 +0200896#ifdef CONFIG_X86_INTEL_MID
897bool pci_use_mid_pm(void);
898int mid_pci_set_power_state(struct pci_dev *pdev, pci_power_t state);
899pci_power_t mid_pci_get_power_state(struct pci_dev *pdev);
900#else
901static inline bool pci_use_mid_pm(void)
902{
903 return false;
904}
905static inline int mid_pci_set_power_state(struct pci_dev *pdev, pci_power_t state)
906{
907 return -ENODEV;
908}
909static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev)
910{
911 return PCI_UNKNOWN;
912}
913#endif
914
Philipp Stanner25216af2024-06-13 13:50:23 +0200915int pcim_intx(struct pci_dev *dev, int enable);
Philipp Stannerd47bde72024-06-13 13:50:18 +0200916int pcim_request_region_exclusive(struct pci_dev *pdev, int bar,
917 const char *name);
918void pcim_release_region(struct pci_dev *pdev, int bar);
919
Philipp Stanner815a3902024-01-31 10:00:23 +0100920/*
Pali Rohár8a9b7ef2022-09-24 11:24:02 +0200921 * Config Address for PCI Configuration Mechanism #1
922 *
923 * See PCI Local Bus Specification, Revision 3.0,
924 * Section 3.2.2.3.2, Figure 3-2, p. 50.
925 */
926
927#define PCI_CONF1_BUS_SHIFT 16 /* Bus number */
928#define PCI_CONF1_DEV_SHIFT 11 /* Device number */
929#define PCI_CONF1_FUNC_SHIFT 8 /* Function number */
930
931#define PCI_CONF1_BUS_MASK 0xff
932#define PCI_CONF1_DEV_MASK 0x1f
933#define PCI_CONF1_FUNC_MASK 0x7
934#define PCI_CONF1_REG_MASK 0xfc /* Limit aligned offset to a maximum of 256B */
935
936#define PCI_CONF1_ENABLE BIT(31)
937#define PCI_CONF1_BUS(x) (((x) & PCI_CONF1_BUS_MASK) << PCI_CONF1_BUS_SHIFT)
938#define PCI_CONF1_DEV(x) (((x) & PCI_CONF1_DEV_MASK) << PCI_CONF1_DEV_SHIFT)
939#define PCI_CONF1_FUNC(x) (((x) & PCI_CONF1_FUNC_MASK) << PCI_CONF1_FUNC_SHIFT)
940#define PCI_CONF1_REG(x) ((x) & PCI_CONF1_REG_MASK)
941
942#define PCI_CONF1_ADDRESS(bus, dev, func, reg) \
943 (PCI_CONF1_ENABLE | \
944 PCI_CONF1_BUS(bus) | \
945 PCI_CONF1_DEV(dev) | \
946 PCI_CONF1_FUNC(func) | \
947 PCI_CONF1_REG(reg))
948
949/*
950 * Extension of PCI Config Address for accessing extended PCIe registers
951 *
952 * No standardized specification, but used on lot of non-ECAM-compliant ARM SoCs
953 * or on AMD Barcelona and new CPUs. Reserved bits [27:24] of PCI Config Address
954 * are used for specifying additional 4 high bits of PCI Express register.
955 */
956
957#define PCI_CONF1_EXT_REG_SHIFT 16
958#define PCI_CONF1_EXT_REG_MASK 0xf00
959#define PCI_CONF1_EXT_REG(x) (((x) & PCI_CONF1_EXT_REG_MASK) << PCI_CONF1_EXT_REG_SHIFT)
960
961#define PCI_CONF1_EXT_ADDRESS(bus, dev, func, reg) \
962 (PCI_CONF1_ADDRESS(bus, dev, func, reg) | \
963 PCI_CONF1_EXT_REG(reg))
964
Zhao, Yu557848c2008-10-13 19:18:07 +0800965#endif /* DRIVERS_PCI_H */