blob: ce9f4d3ca075168d18a9d3856addb860e2bf4489 [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
Wei Yangfff905f2015-06-30 09:16:41 +08007#define PCI_FIND_CAP_TTL 48
8
Lukas Wunner8531e282017-03-10 21:23:45 +01009#define PCI_VSEC_ID_INTEL_TBT 0x1234 /* Thunderbolt */
10
Jacob Keller343e51a2013-07-31 06:53:16 +000011extern const unsigned char pcie_link_speed[];
Sinan Kaya11eb0e02018-06-04 22:16:09 -040012extern bool pci_early_dump;
Jacob Keller343e51a2013-07-31 06:53:16 +000013
Yinghai Lu7a1562d2014-11-11 12:09:46 -080014bool pcie_cap_has_lnkctl(const struct pci_dev *dev);
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016/* Functions internal to the PCI core code */
17
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -060018int pci_create_sysfs_dev_files(struct pci_dev *pdev);
19void pci_remove_sysfs_dev_files(struct pci_dev *pdev);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +053020#if !defined(CONFIG_DMI) && !defined(CONFIG_ACPI)
Narendra K911e1c92010-07-26 05:56:50 -050021static inline void pci_create_firmware_label_files(struct pci_dev *pdev)
Narendra Kb879743f2010-08-02 07:44:29 -050022{ return; }
Narendra K911e1c92010-07-26 05:56:50 -050023static inline void pci_remove_firmware_label_files(struct pci_dev *pdev)
Narendra Kb879743f2010-08-02 07:44:29 -050024{ return; }
Narendra K911e1c92010-07-26 05:56:50 -050025#else
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -060026void pci_create_firmware_label_files(struct pci_dev *pdev);
27void pci_remove_firmware_label_files(struct pci_dev *pdev);
Narendra K911e1c92010-07-26 05:56:50 -050028#endif
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -060029void pci_cleanup_rom(struct pci_dev *dev);
David Woodhousef7195822017-04-12 13:25:59 +010030
Martin Wilck3b519e42010-11-10 11:03:21 +010031enum pci_mmap_api {
32 PCI_MMAP_SYSFS, /* mmap on /sys/bus/pci/devices/<BDF>/resource<N> */
33 PCI_MMAP_PROCFS /* mmap on /proc/bus/pci/<BDF> */
34};
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -060035int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vmai,
36 enum pci_mmap_api mmap_api);
David Woodhousef7195822017-04-12 13:25:59 +010037
Michael S. Tsirkin711d5772009-07-27 23:37:48 +030038int pci_probe_reset_function(struct pci_dev *dev);
Sinan Kaya381634c2018-07-19 18:04:11 -050039int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
Keith Buschc4eed622018-09-20 10:27:11 -060040int pci_bus_error_reset(struct pci_dev *dev);
Kumar Galace5ccde2007-07-16 23:27:10 -050041
Rafael J. Wysocki961d9122008-07-07 03:32:02 +020042/**
Randy Dunlapb33bfde2009-01-09 17:04:26 -080043 * struct pci_platform_pm_ops - Firmware PM callbacks
Rafael J. Wysocki961d9122008-07-07 03:32:02 +020044 *
Mika Westerberg26ad34d2018-09-27 16:57:14 -050045 * @bridge_d3: Does the bridge allow entering into D3
46 *
Randy Dunlapb33bfde2009-01-09 17:04:26 -080047 * @is_manageable: returns 'true' if given device is power manageable by the
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -060048 * platform firmware
Rafael J. Wysocki961d9122008-07-07 03:32:02 +020049 *
Randy Dunlapb33bfde2009-01-09 17:04:26 -080050 * @set_state: invokes the platform firmware to set the device's power state
Rafael J. Wysocki961d9122008-07-07 03:32:02 +020051 *
Lukas Wunnercc7cc022016-09-18 05:39:20 +020052 * @get_state: queries the platform firmware for a device's current power state
53 *
Rafael J. Wysockib51033e2019-06-25 14:09:12 +020054 * @refresh_state: asks the platform to refresh the device's power state data
55 *
Randy Dunlapb33bfde2009-01-09 17:04:26 -080056 * @choose_state: returns PCI power state of given device preferred by the
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -060057 * platform; to be used during system-wide transitions from a
58 * sleeping state to the working state and vice versa
Rafael J. Wysocki961d9122008-07-07 03:32:02 +020059 *
Rafael J. Wysocki08476842017-06-24 01:57:35 +020060 * @set_wakeup: enables/disables wakeup capability for the device
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010061 *
Rafael J. Wysockibac2a902015-01-21 02:17:42 +010062 * @need_resume: returns 'true' if the given device (which is currently
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -060063 * suspended) needs to be resumed to be configured for system
64 * wakeup.
Rafael J. Wysockibac2a902015-01-21 02:17:42 +010065 *
Rafael J. Wysocki961d9122008-07-07 03:32:02 +020066 * If given platform is generally capable of power managing PCI devices, all of
67 * these callbacks are mandatory.
68 */
69struct pci_platform_pm_ops {
Mika Westerberg26ad34d2018-09-27 16:57:14 -050070 bool (*bridge_d3)(struct pci_dev *dev);
Rafael J. Wysocki961d9122008-07-07 03:32:02 +020071 bool (*is_manageable)(struct pci_dev *dev);
72 int (*set_state)(struct pci_dev *dev, pci_power_t state);
Lukas Wunnercc7cc022016-09-18 05:39:20 +020073 pci_power_t (*get_state)(struct pci_dev *dev);
Rafael J. Wysockib51033e2019-06-25 14:09:12 +020074 void (*refresh_state)(struct pci_dev *dev);
Rafael J. Wysocki961d9122008-07-07 03:32:02 +020075 pci_power_t (*choose_state)(struct pci_dev *dev);
Rafael J. Wysocki08476842017-06-24 01:57:35 +020076 int (*set_wakeup)(struct pci_dev *dev, bool enable);
Rafael J. Wysockibac2a902015-01-21 02:17:42 +010077 bool (*need_resume)(struct pci_dev *dev);
Rafael J. Wysocki961d9122008-07-07 03:32:02 +020078};
79
Julia Lawall299f2ff2015-12-06 17:33:45 +010080int pci_set_platform_pm(const struct pci_platform_pm_ops *ops);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -060081void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
Rafael J. Wysockib51033e2019-06-25 14:09:12 +020082void pci_refresh_power_state(struct pci_dev *dev);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -060083void pci_power_up(struct pci_dev *dev);
84void pci_disable_enabled_device(struct pci_dev *dev);
85int pci_finish_runtime_suspend(struct pci_dev *dev);
Bjorn Helgaasdcb04532018-03-09 11:06:53 -060086void pcie_clear_root_pme_status(struct pci_dev *dev);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -060087int __pci_pme_wakeup(struct pci_dev *dev, void *ign);
Rafael J. Wysocki0ce3fca2017-07-12 03:05:39 +020088void pci_pme_restore(struct pci_dev *dev);
Rafael J. Wysocki0c7376a2019-06-07 00:32:31 +020089bool pci_dev_need_resume(struct pci_dev *dev);
90void pci_dev_adjust_pme(struct pci_dev *dev);
Rafael J. Wysocki2cef5482015-09-30 01:10:24 +020091void pci_dev_complete_resume(struct pci_dev *pci_dev);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -060092void pci_config_pm_runtime_get(struct pci_dev *dev);
93void pci_config_pm_runtime_put(struct pci_dev *dev);
94void pci_pm_init(struct pci_dev *dev);
Sean O. Stalley938174e2015-10-29 17:35:39 -050095void pci_ea_init(struct pci_dev *dev);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -060096void pci_allocate_cap_save_buffers(struct pci_dev *dev);
Yinghai Luf7968412012-02-11 00:18:30 -080097void pci_free_cap_save_buffers(struct pci_dev *dev);
Lukas Wunnerc6a63302016-10-28 10:52:06 +020098bool pci_bridge_d3_possible(struct pci_dev *dev);
Lukas Wunner1ed276a2016-10-28 10:52:06 +020099void pci_bridge_d3_update(struct pci_dev *dev);
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100100
Rafael J. Wysockib6e335a2010-12-29 13:21:23 +0100101static inline void pci_wakeup_event(struct pci_dev *dev)
102{
103 /* Wait 100 ms before the system can be put into a sleep state. */
104 pm_wakeup_event(&dev->dev, 100);
105}
106
Yijing Wang326c1cd2014-05-04 12:23:36 +0800107static inline bool pci_has_subordinate(struct pci_dev *pci_dev)
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100108{
109 return !!(pci_dev->subordinate);
110}
David Shaohua Li0f644742005-03-19 00:15:48 -0500111
Mika Westerberg9d26d3a2016-06-02 11:17:12 +0300112static inline bool pci_power_manageable(struct pci_dev *pci_dev)
113{
114 /*
115 * Currently we allow normal PCI devices and PCI bridges transition
116 * into D3 if their bridge_d3 is set.
117 */
118 return !pci_has_subordinate(pci_dev) || pci_dev->bridge_d3;
119}
120
Mika Westerberg984998e2019-08-22 11:55:52 +0300121static inline bool pcie_downstream_port(const struct pci_dev *dev)
122{
123 int type = pci_pcie_type(dev);
124
125 return type == PCI_EXP_TYPE_ROOT_PORT ||
126 type == PCI_EXP_TYPE_DOWNSTREAM ||
127 type == PCI_EXP_TYPE_PCIE_BRIDGE;
128}
129
Bjorn Helgaasf1cd93f2016-02-22 13:58:37 -0600130int pci_vpd_init(struct pci_dev *dev);
Bjorn Helgaas64379072016-02-22 13:58:06 -0600131void pci_vpd_release(struct pci_dev *dev);
Bjorn Helgaasb1c615c2018-03-19 13:06:17 -0500132void pcie_vpd_create_sysfs_dev_files(struct pci_dev *dev);
133void pcie_vpd_remove_sysfs_dev_files(struct pci_dev *dev);
Ben Hutchings94e61082008-03-05 16:52:39 +0000134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* PCI /proc functions */
136#ifdef CONFIG_PROC_FS
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600137int pci_proc_attach_device(struct pci_dev *dev);
138int pci_proc_detach_device(struct pci_dev *dev);
139int pci_proc_detach_bus(struct pci_bus *bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#else
141static inline int pci_proc_attach_device(struct pci_dev *dev) { return 0; }
142static inline int pci_proc_detach_device(struct pci_dev *dev) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static inline int pci_proc_detach_bus(struct pci_bus *bus) { return 0; }
144#endif
145
146/* Functions for PCI Hotplug drivers to use */
Yinghai Lua8e4b9c2012-05-18 13:46:34 -0600147int pci_hp_add_bridge(struct pci_dev *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000149#ifdef HAVE_PCI_LEGACY
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600150void pci_create_legacy_files(struct pci_bus *bus);
151void pci_remove_legacy_files(struct pci_bus *bus);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000152#else
153static inline void pci_create_legacy_files(struct pci_bus *bus) { return; }
154static inline void pci_remove_legacy_files(struct pci_bus *bus) { return; }
155#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157/* Lock for read/write access to pci device and bus lists */
Zhang Yanmind71374d2006-06-02 12:35:43 +0800158extern struct rw_semaphore pci_bus_sem;
Keith Buschc4eed622018-09-20 10:27:11 -0600159extern struct mutex pci_slot_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Jan Kiszkaa2e27782011-11-04 09:46:00 +0100161extern raw_spinlock_t pci_lock;
162
Kristen Carlson Accardiffadcc22006-07-12 08:59:00 -0700163extern unsigned int pci_pm_d3_delay;
Michael Ellerman88187df2007-01-25 19:34:07 +1100164
Andrew Morton4b47b0e2005-08-16 15:16:05 -0700165#ifdef CONFIG_PCI_MSI
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700166void pci_no_msi(void);
Andrew Morton4b47b0e2005-08-16 15:16:05 -0700167#else
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700168static inline void pci_no_msi(void) { }
Andrew Morton4b47b0e2005-08-16 15:16:05 -0700169#endif
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100170
Michael S. Tsirkin6a25f5e2015-05-07 09:52:21 -0500171static inline void pci_msi_set_enable(struct pci_dev *dev, int enable)
172{
173 u16 control;
174
175 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
176 control &= ~PCI_MSI_FLAGS_ENABLE;
177 if (enable)
178 control |= PCI_MSI_FLAGS_ENABLE;
179 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
180}
181
182static inline void pci_msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
183{
184 u16 ctrl;
185
186 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
187 ctrl &= ~clear;
188 ctrl |= set;
189 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
190}
191
Yinghai Lub55438f2012-02-23 19:23:30 -0800192void pci_realloc_get_opt(char *);
Ram Paif483d392011-07-07 11:19:10 -0700193
Kristen Carlson Accardiffadcc22006-07-12 08:59:00 -0700194static inline int pci_no_d1d2(struct pci_dev *dev)
195{
196 unsigned int parent_dstates = 0;
Andrew Morton4b47b0e2005-08-16 15:16:05 -0700197
Kristen Carlson Accardiffadcc22006-07-12 08:59:00 -0700198 if (dev->bus->self)
199 parent_dstates = dev->bus->self->no_d1d2;
200 return (dev->no_d1d2 || parent_dstates);
201
202}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700203extern const struct attribute_group *pci_dev_groups[];
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700204extern const struct attribute_group *pcibus_groups[];
Bhumika Goyal69f2dc22017-09-12 16:43:33 +0530205extern const struct device_type pci_dev_type;
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600206extern const struct attribute_group *pci_bus_groups[];
Alex Chiang705b1aa2009-03-20 14:56:31 -0600207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/**
210 * pci_match_one_device - Tell if a PCI device structure has a matching
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -0600211 * PCI device id structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * @id: single PCI device id structure to match
213 * @dev: the PCI device structure to match against
Greg Kroah-Hartman367b09f2008-01-30 15:21:33 -0800214 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Returns the matching pci_device_id structure or %NULL if there is no match.
216 */
217static inline const struct pci_device_id *
218pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
219{
220 if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) &&
221 (id->device == PCI_ANY_ID || id->device == dev->device) &&
222 (id->subvendor == PCI_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
223 (id->subdevice == PCI_ANY_ID || id->subdevice == dev->subsystem_device) &&
224 !((id->class ^ dev->class) & id->class_mask))
225 return id;
226 return NULL;
227}
228
Alex Chiangf46753c2008-06-10 15:28:50 -0600229/* PCI slot sysfs helper code */
230#define to_pci_slot(s) container_of(s, struct pci_slot, kobj)
231
232extern struct kset *pci_slots_kset;
233
234struct pci_slot_attribute {
235 struct attribute attr;
236 ssize_t (*show)(struct pci_slot *, char *);
237 ssize_t (*store)(struct pci_slot *, const char *, size_t);
238};
239#define to_pci_slot_attr(s) container_of(s, struct pci_slot_attribute, attr)
240
Yu Zhao0b400c72008-11-22 02:40:40 +0800241enum pci_bar_type {
242 pci_bar_unknown, /* Standard PCI BAR probe */
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -0600243 pci_bar_io, /* An I/O port BAR */
Yu Zhao0b400c72008-11-22 02:40:40 +0800244 pci_bar_mem32, /* A 32-bit memory BAR */
245 pci_bar_mem64, /* A 64-bit memory BAR */
246};
247
Sinan Kaya62ce94a2017-07-12 00:04:14 -0400248int pci_configure_extended_tags(struct pci_dev *dev, void *ign);
Yinghai Luefdc87d2012-01-27 10:55:10 -0800249bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *pl,
250 int crs_timeout);
James Puthukattukaranaa667c62018-07-09 11:31:25 -0400251bool pci_bus_generic_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *pl,
252 int crs_timeout);
253int pci_idt_bus_quirk(struct pci_bus *bus, int devfn, u32 *pl, int crs_timeout);
254
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600255int pci_setup_device(struct pci_dev *dev);
256int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
257 struct resource *res, unsigned int reg);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600258void pci_configure_ari(struct pci_dev *dev);
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -0600259void __pci_bus_size_bridges(struct pci_bus *bus,
Jiang Liud66ecb72013-06-23 01:01:35 +0200260 struct list_head *realloc_head);
Bjorn Helgaas10874f5a2014-04-14 16:11:40 -0600261void __pci_bus_assign_resources(const struct pci_bus *bus,
262 struct list_head *realloc_head,
263 struct list_head *fail_head);
Yinghai Lu0f7e7ae2015-01-15 16:21:49 -0600264bool pci_bus_clip_resource(struct pci_dev *dev, int idx);
Bjorn Helgaas939de1d2013-01-26 17:35:58 -0700265
Yinghai Lu2069ecf2012-02-15 21:40:31 -0800266void pci_reassigndev_resource_alignment(struct pci_dev *dev);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600267void pci_disable_bridge_window(struct pci_dev *dev);
Yuji Shimada32a9a6822009-03-16 17:13:39 +0900268
Tal Gilboa6cf57be2018-03-30 07:44:05 -0500269/* PCIe link information */
270#define PCIE_SPEED2STR(speed) \
271 ((speed) == PCIE_SPEED_16_0GT ? "16 GT/s" : \
272 (speed) == PCIE_SPEED_8_0GT ? "8 GT/s" : \
273 (speed) == PCIE_SPEED_5_0GT ? "5 GT/s" : \
274 (speed) == PCIE_SPEED_2_5GT ? "2.5 GT/s" : \
275 "Unknown speed")
276
Tal Gilboab852f632018-03-30 08:32:03 -0500277/* PCIe speed to Mb/s reduced by encoding overhead */
278#define PCIE_SPEED2MBS_ENC(speed) \
279 ((speed) == PCIE_SPEED_16_0GT ? 16000*128/130 : \
280 (speed) == PCIE_SPEED_8_0GT ? 8000*128/130 : \
281 (speed) == PCIE_SPEED_5_0GT ? 5000*8/10 : \
282 (speed) == PCIE_SPEED_2_5GT ? 2500*8/10 : \
283 0)
284
Tal Gilboa6cf57be2018-03-30 07:44:05 -0500285enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev);
Tal Gilboac70b65f2018-03-30 08:24:36 -0500286enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev);
Tal Gilboab852f632018-03-30 08:32:03 -0500287u32 pcie_bandwidth_capable(struct pci_dev *dev, enum pci_bus_speed *speed,
288 enum pcie_link_width *width);
Alexandru Gagniuc2d1ce5e2018-08-06 18:25:35 -0500289void __pcie_print_link_status(struct pci_dev *dev, bool verbose);
Lukas Wunner0fa635a2019-03-20 12:05:30 +0100290void pcie_report_downtraining(struct pci_dev *dev);
Tal Gilboa6cf57be2018-03-30 07:44:05 -0500291
Yu Zhaod1b054d2009-03-20 11:25:11 +0800292/* Single Root I/O Virtualization */
293struct pci_sriov {
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -0600294 int pos; /* Capability position */
295 int nres; /* Number of resources */
296 u32 cap; /* SR-IOV Capabilities */
297 u16 ctrl; /* SR-IOV Control */
298 u16 total_VFs; /* Total VFs associated with the PF */
299 u16 initial_VFs; /* Initial VFs associated with the PF */
300 u16 num_VFs; /* Number of VFs available */
301 u16 offset; /* First VF Routing ID offset */
302 u16 stride; /* Following VF stride */
303 u16 vf_device; /* VF device ID */
304 u32 pgsz; /* Page size for BAR alignment */
305 u8 link; /* Function Dependency Link */
306 u8 max_VF_buses; /* Max buses consumed by VFs */
307 u16 driver_max_VFs; /* Max num VFs driver supports */
308 struct pci_dev *dev; /* Lowest numbered PF */
309 struct pci_dev *self; /* This PF */
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100310 u32 class; /* VF device */
311 u8 hdr_type; /* VF header type */
312 u16 subsystem_vendor; /* VF subsystem vendor */
313 u16 subsystem_device; /* VF subsystem device */
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -0600314 resource_size_t barsz[PCI_SRIOV_NUM_BARS]; /* VF BAR size */
315 bool drivers_autoprobe; /* Auto probing of VFs by driver */
Yu Zhaod1b054d2009-03-20 11:25:11 +0800316};
317
Keith Buscha6bd1012018-09-20 10:27:16 -0600318/**
319 * pci_dev_set_io_state - Set the new error state if possible.
320 *
321 * @dev - pci device to set new error_state
322 * @new - the state we want dev to be in
323 *
324 * Must be called with device_lock held.
325 *
326 * Returns true if state has been changed to the requested state.
327 */
328static inline bool pci_dev_set_io_state(struct pci_dev *dev,
329 pci_channel_state_t new)
330{
331 bool changed = false;
332
333 device_lock_assert(&dev->dev);
334 switch (new) {
335 case pci_channel_io_perm_failure:
336 switch (dev->error_state) {
337 case pci_channel_io_frozen:
338 case pci_channel_io_normal:
339 case pci_channel_io_perm_failure:
340 changed = true;
341 break;
342 }
343 break;
344 case pci_channel_io_frozen:
345 switch (dev->error_state) {
346 case pci_channel_io_frozen:
347 case pci_channel_io_normal:
348 changed = true;
349 break;
350 }
351 break;
352 case pci_channel_io_normal:
353 switch (dev->error_state) {
354 case pci_channel_io_frozen:
355 case pci_channel_io_normal:
356 changed = true;
357 break;
358 }
359 break;
360 }
361 if (changed)
362 dev->error_state = new;
363 return changed;
364}
Keith Busch89ee9f72017-03-29 22:48:59 -0500365
366static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused)
367{
Keith Buscha6bd1012018-09-20 10:27:16 -0600368 device_lock(&dev->dev);
369 pci_dev_set_io_state(dev, pci_channel_io_perm_failure);
370 device_unlock(&dev->dev);
371
Keith Busch89ee9f72017-03-29 22:48:59 -0500372 return 0;
373}
374
375static inline bool pci_dev_is_disconnected(const struct pci_dev *dev)
376{
Keith Buscha6bd1012018-09-20 10:27:16 -0600377 return dev->error_state == pci_channel_io_perm_failure;
Keith Busch89ee9f72017-03-29 22:48:59 -0500378}
379
Keith Buscha6bd1012018-09-20 10:27:16 -0600380/* pci_dev priv_flags */
381#define PCI_DEV_ADDED 0
382
Hari Vyas44bda4b2018-07-03 14:35:41 +0530383static inline void pci_dev_assign_added(struct pci_dev *dev, bool added)
384{
385 assign_bit(PCI_DEV_ADDED, &dev->priv_flags, added);
386}
387
388static inline bool pci_dev_is_added(const struct pci_dev *dev)
389{
390 return test_bit(PCI_DEV_ADDED, &dev->priv_flags);
391}
392
Keith Busch1e451162018-07-19 16:16:55 -0500393#ifdef CONFIG_PCIEAER
394#include <linux/aer.h>
395
396#define AER_MAX_MULTI_ERR_DEVICES 5 /* Not likely to have more */
397
398struct aer_err_info {
399 struct pci_dev *dev[AER_MAX_MULTI_ERR_DEVICES];
400 int error_dev_num;
401
402 unsigned int id:16;
403
404 unsigned int severity:2; /* 0:NONFATAL | 1:FATAL | 2:COR */
405 unsigned int __pad1:5;
406 unsigned int multi_error_valid:1;
407
408 unsigned int first_error:5;
409 unsigned int __pad2:2;
410 unsigned int tlp_header_valid:1;
411
412 unsigned int status; /* COR/UNCOR Error Status */
413 unsigned int mask; /* COR/UNCOR Error Mask */
414 struct aer_header_log_regs tlp; /* TLP Header */
415};
416
417int aer_get_device_error_info(struct pci_dev *dev, struct aer_err_info *info);
418void aer_print_error(struct pci_dev *dev, struct aer_err_info *info);
419#endif /* CONFIG_PCIEAER */
420
Keith Busch4f802172018-09-20 10:27:08 -0600421#ifdef CONFIG_PCIE_DPC
422void pci_save_dpc_state(struct pci_dev *dev);
423void pci_restore_dpc_state(struct pci_dev *dev);
424#else
425static inline void pci_save_dpc_state(struct pci_dev *dev) {}
426static inline void pci_restore_dpc_state(struct pci_dev *dev) {}
427#endif
428
Hao, Xudong1900ca12011-12-17 21:24:40 +0800429#ifdef CONFIG_PCI_ATS
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600430void pci_restore_ats_state(struct pci_dev *dev);
Hao, Xudong1900ca12011-12-17 21:24:40 +0800431#else
432static inline void pci_restore_ats_state(struct pci_dev *dev)
433{
434}
435#endif /* CONFIG_PCI_ATS */
436
Yu Zhaod1b054d2009-03-20 11:25:11 +0800437#ifdef CONFIG_PCI_IOV
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600438int pci_iov_init(struct pci_dev *dev);
439void pci_iov_release(struct pci_dev *dev);
Jakub Kicinski38972372018-06-29 15:08:52 -0500440void pci_iov_remove(struct pci_dev *dev);
Bjorn Helgaas6ffa2482016-11-28 09:15:52 -0600441void pci_iov_update_resource(struct pci_dev *dev, int resno);
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600442resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno);
443void pci_restore_iov_state(struct pci_dev *dev);
444int pci_iov_bus_range(struct pci_bus *bus);
Yu Zhao302b4212009-05-18 13:51:32 +0800445
Yu Zhaod1b054d2009-03-20 11:25:11 +0800446#else
447static inline int pci_iov_init(struct pci_dev *dev)
448{
449 return -ENODEV;
450}
451static inline void pci_iov_release(struct pci_dev *dev)
452
453{
454}
Jakub Kicinski38972372018-06-29 15:08:52 -0500455static inline void pci_iov_remove(struct pci_dev *dev)
456{
457}
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800458static inline void pci_restore_iov_state(struct pci_dev *dev)
459{
460}
Yu Zhaoa28724b2009-03-20 11:25:13 +0800461static inline int pci_iov_bus_range(struct pci_bus *bus)
462{
463 return 0;
464}
Yu Zhao302b4212009-05-18 13:51:32 +0800465
Yu Zhaod1b054d2009-03-20 11:25:11 +0800466#endif /* CONFIG_PCI_IOV */
467
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600468unsigned long pci_cardbus_resource_alignment(struct resource *);
Ram Pai0a2daa12011-07-25 13:08:41 -0700469
Cam Macdonell0e522472010-09-07 17:25:20 -0700470static inline resource_size_t pci_resource_alignment(struct pci_dev *dev,
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600471 struct resource *res)
Chris Wright6faf17f2009-08-28 13:00:06 -0700472{
473#ifdef CONFIG_PCI_IOV
474 int resno = res - dev->resource;
475
476 if (resno >= PCI_IOV_RESOURCES && resno <= PCI_IOV_RESOURCE_END)
477 return pci_sriov_resource_alignment(dev, resno);
478#endif
Bjorn Helgaas0aa0f5d2017-12-02 13:21:37 -0600479 if (dev->class >> 8 == PCI_CLASS_BRIDGE_CARDBUS)
Ram Pai0a2daa12011-07-25 13:08:41 -0700480 return pci_cardbus_resource_alignment(res);
Chris Wright6faf17f2009-08-28 13:00:06 -0700481 return resource_alignment(res);
482}
483
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600484void pci_enable_acs(struct pci_dev *dev);
Bjorn Helgaasbd2e9562018-08-09 16:19:52 -0500485#ifdef CONFIG_PCI_QUIRKS
486int pci_dev_specific_acs_enabled(struct pci_dev *dev, u16 acs_flags);
487int pci_dev_specific_enable_acs(struct pci_dev *dev);
Logan Gunthorpe73c47dde2018-08-09 16:51:43 -0500488int pci_dev_specific_disable_acs_redir(struct pci_dev *dev);
Bjorn Helgaasbd2e9562018-08-09 16:19:52 -0500489#else
490static inline int pci_dev_specific_acs_enabled(struct pci_dev *dev,
491 u16 acs_flags)
492{
493 return -ENOTTY;
494}
495static inline int pci_dev_specific_enable_acs(struct pci_dev *dev)
496{
497 return -ENOTTY;
498}
Logan Gunthorpe73c47dde2018-08-09 16:51:43 -0500499static inline int pci_dev_specific_disable_acs_redir(struct pci_dev *dev)
500{
501 return -ENOTTY;
502}
Bjorn Helgaasbd2e9562018-08-09 16:19:52 -0500503#endif
Allen Kayae21ee62009-10-07 10:27:17 -0700504
Oza Pawandeep2e28bc82018-05-17 16:44:15 -0500505/* PCI error reporting and recovery */
Keith Buschbdb5ac852018-09-20 10:27:12 -0600506void pcie_do_recovery(struct pci_dev *dev, enum pci_channel_state state,
507 u32 service);
Oza Pawandeep2e28bc82018-05-17 16:44:15 -0500508
Mika Westerbergc2bf1fc2019-06-12 13:57:38 +0300509bool pcie_wait_for_link_delay(struct pci_dev *pdev, bool active, int delay);
Oza Pawandeep9f5a70f12018-05-17 16:44:11 -0500510bool pcie_wait_for_link(struct pci_dev *pdev, bool active);
Bjorn Helgaas7d8e7d12017-12-15 08:57:28 -0600511#ifdef CONFIG_PCIEASPM
512void pcie_aspm_init_link_state(struct pci_dev *pdev);
513void pcie_aspm_exit_link_state(struct pci_dev *pdev);
514void pcie_aspm_pm_state_change(struct pci_dev *pdev);
515void pcie_aspm_powersave_config_link(struct pci_dev *pdev);
516#else
517static inline void pcie_aspm_init_link_state(struct pci_dev *pdev) { }
518static inline void pcie_aspm_exit_link_state(struct pci_dev *pdev) { }
519static inline void pcie_aspm_pm_state_change(struct pci_dev *pdev) { }
520static inline void pcie_aspm_powersave_config_link(struct pci_dev *pdev) { }
521#endif
522
523#ifdef CONFIG_PCIEASPM_DEBUG
524void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev);
525void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev);
526#else
527static inline void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev) { }
528static inline void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev) { }
529#endif
530
Jonathan Yong9bb04a02016-06-11 14:13:38 -0500531#ifdef CONFIG_PCIE_PTM
532void pci_ptm_init(struct pci_dev *dev);
533#else
534static inline void pci_ptm_init(struct pci_dev *dev) { }
535#endif
536
Dexuan Cuib9c3b262009-12-07 13:03:21 +0800537struct pci_dev_reset_methods {
538 u16 vendor;
539 u16 device;
540 int (*reset)(struct pci_dev *dev, int probe);
541};
542
Rafael J. Wysocki93177a72010-01-02 22:57:24 +0100543#ifdef CONFIG_PCI_QUIRKS
Bjorn Helgaasf39d5b72013-04-12 12:02:59 -0600544int pci_dev_specific_reset(struct pci_dev *dev, int probe);
Rafael J. Wysocki93177a72010-01-02 22:57:24 +0100545#else
546static inline int pci_dev_specific_reset(struct pci_dev *dev, int probe)
547{
548 return -ENOTTY;
549}
550#endif
Dexuan Cuib9c3b262009-12-07 13:03:21 +0800551
Dongdong Liu169de962016-12-01 00:33:42 -0600552#if defined(CONFIG_PCI_QUIRKS) && defined(CONFIG_ARM64)
553int acpi_get_rc_resources(struct device *dev, const char *hid, u16 segment,
554 struct resource *res);
555#endif
556
Christian König276b7382017-10-24 14:40:20 -0500557u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar);
558int pci_rebar_get_current_size(struct pci_dev *pdev, int bar);
559int pci_rebar_set_size(struct pci_dev *pdev, int bar, int size);
560static inline u64 pci_rebar_size_to_bytes(int size)
561{
562 return 1ULL << (size + 20);
563}
564
Rob Herring9e2aee82018-05-11 12:15:30 -0500565struct device_node;
566
567#ifdef CONFIG_OF
568int of_pci_parse_bus_range(struct device_node *node, struct resource *res);
569int of_get_pci_domain_nr(struct device_node *node);
570int of_pci_get_max_link_speed(struct device_node *node);
571
572#else
573static inline int
574of_pci_parse_bus_range(struct device_node *node, struct resource *res)
575{
576 return -EINVAL;
577}
578
579static inline int
580of_get_pci_domain_nr(struct device_node *node)
581{
582 return -1;
583}
584
585static inline int
586of_pci_get_max_link_speed(struct device_node *node)
587{
588 return -EINVAL;
589}
590#endif /* CONFIG_OF */
591
592#if defined(CONFIG_OF_ADDRESS)
Bjorn Helgaasf03c7aa2018-06-06 16:10:27 -0500593int devm_of_pci_get_host_bridge_resources(struct device *dev,
Rob Herring9e2aee82018-05-11 12:15:30 -0500594 unsigned char busno, unsigned char bus_max,
595 struct list_head *resources, resource_size_t *io_base);
596#else
Bjorn Helgaasf03c7aa2018-06-06 16:10:27 -0500597static inline int devm_of_pci_get_host_bridge_resources(struct device *dev,
Rob Herring9e2aee82018-05-11 12:15:30 -0500598 unsigned char busno, unsigned char bus_max,
599 struct list_head *resources, resource_size_t *io_base)
600{
601 return -EINVAL;
602}
603#endif
604
Rajat Jain60ed9822018-06-21 16:48:26 -0700605#ifdef CONFIG_PCIEAER
606void pci_no_aer(void);
607void pci_aer_init(struct pci_dev *dev);
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500608void pci_aer_exit(struct pci_dev *dev);
Rajat Jain81aa5202018-06-21 16:48:28 -0700609extern const struct attribute_group aer_stats_attr_group;
Bjorn Helgaas7ab92e82018-07-19 17:55:58 -0500610void pci_aer_clear_fatal_status(struct pci_dev *dev);
Oza Pawandeepec752f52018-07-19 17:58:09 -0500611void pci_aer_clear_device_status(struct pci_dev *dev);
Rajat Jain60ed9822018-06-21 16:48:26 -0700612#else
613static inline void pci_no_aer(void) { }
Jisheng Zhang31f996e2019-04-12 06:43:06 +0000614static inline void pci_aer_init(struct pci_dev *d) { }
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500615static inline void pci_aer_exit(struct pci_dev *d) { }
Bjorn Helgaas7ab92e82018-07-19 17:55:58 -0500616static inline void pci_aer_clear_fatal_status(struct pci_dev *dev) { }
Oza Pawandeepec752f52018-07-19 17:58:09 -0500617static inline void pci_aer_clear_device_status(struct pci_dev *dev) { }
Rajat Jain60ed9822018-06-21 16:48:26 -0700618#endif
619
Krzysztof Wilczynski8c3aac62019-08-27 11:49:50 +0200620#ifdef CONFIG_ACPI
Krzysztof Wilczynski4a2dbed2019-08-27 11:49:51 +0200621int pci_acpi_program_hp_params(struct pci_dev *dev);
Krzysztof Wilczynski8c3aac62019-08-27 11:49:50 +0200622#else
Krzysztof Wilczynski4a2dbed2019-08-27 11:49:51 +0200623static inline int pci_acpi_program_hp_params(struct pci_dev *dev)
Krzysztof Wilczynski8c3aac62019-08-27 11:49:50 +0200624{
625 return -ENODEV;
626}
627#endif
628
Zhao, Yu557848c2008-10-13 19:18:07 +0800629#endif /* DRIVERS_PCI_H */