pci.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef DRIVERS_PCI_H
  3. #define DRIVERS_PCI_H
  4. #include <linux/pci.h>
  5. /* Number of possible devfns: 0.0 to 1f.7 inclusive */
  6. #define MAX_NR_DEVFNS 256
  7. #define PCI_FIND_CAP_TTL 48
  8. #define PCI_VSEC_ID_INTEL_TBT 0x1234 /* Thunderbolt */
  9. #define PCIE_LINK_RETRAIN_TIMEOUT_MS 1000
  10. /*
  11. * Power stable to PERST# inactive.
  12. *
  13. * See the "Power Sequencing and Reset Signal Timings" table of the PCI Express
  14. * Card Electromechanical Specification, Revision 5.1, Section 2.9.2, Symbol
  15. * "T_PVPERL".
  16. */
  17. #define PCIE_T_PVPERL_MS 100
  18. /*
  19. * REFCLK stable before PERST# inactive.
  20. *
  21. * See the "Power Sequencing and Reset Signal Timings" table of the PCI Express
  22. * Card Electromechanical Specification, Revision 5.1, Section 2.9.2, Symbol
  23. * "T_PERST-CLK".
  24. */
  25. #define PCIE_T_PERST_CLK_US 100
  26. /*
  27. * End of conventional reset (PERST# de-asserted) to first configuration
  28. * request (device able to respond with a "Request Retry Status" completion),
  29. * from PCIe r6.0, sec 6.6.1.
  30. */
  31. #define PCIE_T_RRS_READY_MS 100
  32. /*
  33. * PCIe r6.0, sec 5.3.3.2.1 <PME Synchronization>
  34. * Recommends 1ms to 10ms timeout to check L2 ready.
  35. */
  36. #define PCIE_PME_TO_L2_TIMEOUT_US 10000
  37. /*
  38. * PCIe r6.0, sec 6.6.1 <Conventional Reset>
  39. *
  40. * - "With a Downstream Port that does not support Link speeds greater
  41. * than 5.0 GT/s, software must wait a minimum of 100 ms following exit
  42. * from a Conventional Reset before sending a Configuration Request to
  43. * the device immediately below that Port."
  44. *
  45. * - "With a Downstream Port that supports Link speeds greater than
  46. * 5.0 GT/s, software must wait a minimum of 100 ms after Link training
  47. * completes before sending a Configuration Request to the device
  48. * immediately below that Port."
  49. */
  50. #define PCIE_RESET_CONFIG_WAIT_MS 100
  51. /* Message Routing (r[2:0]); PCIe r6.0, sec 2.2.8 */
  52. #define PCIE_MSG_TYPE_R_RC 0
  53. #define PCIE_MSG_TYPE_R_ADDR 1
  54. #define PCIE_MSG_TYPE_R_ID 2
  55. #define PCIE_MSG_TYPE_R_BC 3
  56. #define PCIE_MSG_TYPE_R_LOCAL 4
  57. #define PCIE_MSG_TYPE_R_GATHER 5
  58. /* Power Management Messages; PCIe r6.0, sec 2.2.8.2 */
  59. #define PCIE_MSG_CODE_PME_TURN_OFF 0x19
  60. /* INTx Mechanism Messages; PCIe r6.0, sec 2.2.8.1 */
  61. #define PCIE_MSG_CODE_ASSERT_INTA 0x20
  62. #define PCIE_MSG_CODE_ASSERT_INTB 0x21
  63. #define PCIE_MSG_CODE_ASSERT_INTC 0x22
  64. #define PCIE_MSG_CODE_ASSERT_INTD 0x23
  65. #define PCIE_MSG_CODE_DEASSERT_INTA 0x24
  66. #define PCIE_MSG_CODE_DEASSERT_INTB 0x25
  67. #define PCIE_MSG_CODE_DEASSERT_INTC 0x26
  68. #define PCIE_MSG_CODE_DEASSERT_INTD 0x27
  69. extern const unsigned char pcie_link_speed[];
  70. extern bool pci_early_dump;
  71. bool pcie_cap_has_lnkctl(const struct pci_dev *dev);
  72. bool pcie_cap_has_lnkctl2(const struct pci_dev *dev);
  73. bool pcie_cap_has_rtctl(const struct pci_dev *dev);
  74. /* Functions internal to the PCI core code */
  75. #ifdef CONFIG_DMI
  76. extern const struct attribute_group pci_dev_smbios_attr_group;
  77. #endif
  78. enum pci_mmap_api {
  79. PCI_MMAP_SYSFS, /* mmap on /sys/bus/pci/devices/<BDF>/resource<N> */
  80. PCI_MMAP_PROCFS /* mmap on /proc/bus/pci/<BDF> */
  81. };
  82. int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vmai,
  83. enum pci_mmap_api mmap_api);
  84. bool pci_reset_supported(struct pci_dev *dev);
  85. void pci_init_reset_methods(struct pci_dev *dev);
  86. int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
  87. int pci_bus_error_reset(struct pci_dev *dev);
  88. int __pci_reset_bus(struct pci_bus *bus);
  89. struct pci_cap_saved_data {
  90. u16 cap_nr;
  91. bool cap_extended;
  92. unsigned int size;
  93. u32 data[];
  94. };
  95. struct pci_cap_saved_state {
  96. struct hlist_node next;
  97. struct pci_cap_saved_data cap;
  98. };
  99. void pci_allocate_cap_save_buffers(struct pci_dev *dev);
  100. void pci_free_cap_save_buffers(struct pci_dev *dev);
  101. int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size);
  102. int pci_add_ext_cap_save_buffer(struct pci_dev *dev,
  103. u16 cap, unsigned int size);
  104. struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap);
  105. struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev,
  106. u16 cap);
  107. #define PCI_PM_D2_DELAY 200 /* usec; see PCIe r4.0, sec 5.9.1 */
  108. #define PCI_PM_D3HOT_WAIT 10 /* msec */
  109. #define PCI_PM_D3COLD_WAIT 100 /* msec */
  110. void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
  111. void pci_refresh_power_state(struct pci_dev *dev);
  112. int pci_power_up(struct pci_dev *dev);
  113. void pci_disable_enabled_device(struct pci_dev *dev);
  114. int pci_finish_runtime_suspend(struct pci_dev *dev);
  115. void pcie_clear_device_status(struct pci_dev *dev);
  116. void pcie_clear_root_pme_status(struct pci_dev *dev);
  117. bool pci_check_pme_status(struct pci_dev *dev);
  118. void pci_pme_wakeup_bus(struct pci_bus *bus);
  119. void pci_pme_restore(struct pci_dev *dev);
  120. bool pci_dev_need_resume(struct pci_dev *dev);
  121. void pci_dev_adjust_pme(struct pci_dev *dev);
  122. void pci_dev_complete_resume(struct pci_dev *pci_dev);
  123. void pci_config_pm_runtime_get(struct pci_dev *dev);
  124. void pci_config_pm_runtime_put(struct pci_dev *dev);
  125. void pci_pm_init(struct pci_dev *dev);
  126. void pci_ea_init(struct pci_dev *dev);
  127. void pci_msi_init(struct pci_dev *dev);
  128. void pci_msix_init(struct pci_dev *dev);
  129. bool pci_bridge_d3_possible(struct pci_dev *dev);
  130. void pci_bridge_d3_update(struct pci_dev *dev);
  131. int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type);
  132. static inline bool pci_bus_rrs_vendor_id(u32 l)
  133. {
  134. return (l & 0xffff) == PCI_VENDOR_ID_PCI_SIG;
  135. }
  136. static inline void pci_wakeup_event(struct pci_dev *dev)
  137. {
  138. /* Wait 100 ms before the system can be put into a sleep state. */
  139. pm_wakeup_event(&dev->dev, 100);
  140. }
  141. /**
  142. * pci_bar_index_is_valid - Check whether a BAR index is within valid range
  143. * @bar: BAR index
  144. *
  145. * Protects against overflowing &struct pci_dev.resource array.
  146. *
  147. * Return: true for valid index, false otherwise.
  148. */
  149. static inline bool pci_bar_index_is_valid(int bar)
  150. {
  151. if (bar >= 0 && bar < PCI_NUM_RESOURCES)
  152. return true;
  153. return false;
  154. }
  155. static inline bool pci_has_subordinate(struct pci_dev *pci_dev)
  156. {
  157. return !!(pci_dev->subordinate);
  158. }
  159. static inline bool pci_power_manageable(struct pci_dev *pci_dev)
  160. {
  161. /*
  162. * Currently we allow normal PCI devices and PCI bridges transition
  163. * into D3 if their bridge_d3 is set.
  164. */
  165. return !pci_has_subordinate(pci_dev) || pci_dev->bridge_d3;
  166. }
  167. static inline bool pcie_downstream_port(const struct pci_dev *dev)
  168. {
  169. int type = pci_pcie_type(dev);
  170. return type == PCI_EXP_TYPE_ROOT_PORT ||
  171. type == PCI_EXP_TYPE_DOWNSTREAM ||
  172. type == PCI_EXP_TYPE_PCIE_BRIDGE;
  173. }
  174. void pci_vpd_init(struct pci_dev *dev);
  175. extern const struct attribute_group pci_dev_vpd_attr_group;
  176. /* PCI Virtual Channel */
  177. int pci_save_vc_state(struct pci_dev *dev);
  178. void pci_restore_vc_state(struct pci_dev *dev);
  179. void pci_allocate_vc_save_buffers(struct pci_dev *dev);
  180. /* PCI /proc functions */
  181. #ifdef CONFIG_PROC_FS
  182. int pci_proc_attach_device(struct pci_dev *dev);
  183. int pci_proc_detach_device(struct pci_dev *dev);
  184. int pci_proc_detach_bus(struct pci_bus *bus);
  185. #else
  186. static inline int pci_proc_attach_device(struct pci_dev *dev) { return 0; }
  187. static inline int pci_proc_detach_device(struct pci_dev *dev) { return 0; }
  188. static inline int pci_proc_detach_bus(struct pci_bus *bus) { return 0; }
  189. #endif
  190. /* Functions for PCI Hotplug drivers to use */
  191. int pci_hp_add_bridge(struct pci_dev *dev);
  192. #if defined(CONFIG_SYSFS) && defined(HAVE_PCI_LEGACY)
  193. void pci_create_legacy_files(struct pci_bus *bus);
  194. void pci_remove_legacy_files(struct pci_bus *bus);
  195. #else
  196. static inline void pci_create_legacy_files(struct pci_bus *bus) { }
  197. static inline void pci_remove_legacy_files(struct pci_bus *bus) { }
  198. #endif
  199. /* Lock for read/write access to pci device and bus lists */
  200. extern struct rw_semaphore pci_bus_sem;
  201. extern struct mutex pci_slot_mutex;
  202. extern raw_spinlock_t pci_lock;
  203. extern unsigned int pci_pm_d3hot_delay;
  204. #ifdef CONFIG_PCI_MSI
  205. void pci_no_msi(void);
  206. #else
  207. static inline void pci_no_msi(void) { }
  208. #endif
  209. void pci_realloc_get_opt(char *);
  210. static inline int pci_no_d1d2(struct pci_dev *dev)
  211. {
  212. unsigned int parent_dstates = 0;
  213. if (dev->bus->self)
  214. parent_dstates = dev->bus->self->no_d1d2;
  215. return (dev->no_d1d2 || parent_dstates);
  216. }
  217. #ifdef CONFIG_SYSFS
  218. int pci_create_sysfs_dev_files(struct pci_dev *pdev);
  219. void pci_remove_sysfs_dev_files(struct pci_dev *pdev);
  220. extern const struct attribute_group *pci_dev_groups[];
  221. extern const struct attribute_group *pci_dev_attr_groups[];
  222. extern const struct attribute_group *pcibus_groups[];
  223. extern const struct attribute_group *pci_bus_groups[];
  224. #else
  225. static inline int pci_create_sysfs_dev_files(struct pci_dev *pdev) { return 0; }
  226. static inline void pci_remove_sysfs_dev_files(struct pci_dev *pdev) { }
  227. #define pci_dev_groups NULL
  228. #define pci_dev_attr_groups NULL
  229. #define pcibus_groups NULL
  230. #define pci_bus_groups NULL
  231. #endif
  232. extern unsigned long pci_hotplug_io_size;
  233. extern unsigned long pci_hotplug_mmio_size;
  234. extern unsigned long pci_hotplug_mmio_pref_size;
  235. extern unsigned long pci_hotplug_bus_size;
  236. /**
  237. * pci_match_one_device - Tell if a PCI device structure has a matching
  238. * PCI device id structure
  239. * @id: single PCI device id structure to match
  240. * @dev: the PCI device structure to match against
  241. *
  242. * Returns the matching pci_device_id structure or %NULL if there is no match.
  243. */
  244. static inline const struct pci_device_id *
  245. pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
  246. {
  247. if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) &&
  248. (id->device == PCI_ANY_ID || id->device == dev->device) &&
  249. (id->subvendor == PCI_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
  250. (id->subdevice == PCI_ANY_ID || id->subdevice == dev->subsystem_device) &&
  251. !((id->class ^ dev->class) & id->class_mask))
  252. return id;
  253. return NULL;
  254. }
  255. /* PCI slot sysfs helper code */
  256. #define to_pci_slot(s) container_of(s, struct pci_slot, kobj)
  257. extern struct kset *pci_slots_kset;
  258. struct pci_slot_attribute {
  259. struct attribute attr;
  260. ssize_t (*show)(struct pci_slot *, char *);
  261. ssize_t (*store)(struct pci_slot *, const char *, size_t);
  262. };
  263. #define to_pci_slot_attr(s) container_of(s, struct pci_slot_attribute, attr)
  264. enum pci_bar_type {
  265. pci_bar_unknown, /* Standard PCI BAR probe */
  266. pci_bar_io, /* An I/O port BAR */
  267. pci_bar_mem32, /* A 32-bit memory BAR */
  268. pci_bar_mem64, /* A 64-bit memory BAR */
  269. };
  270. struct device *pci_get_host_bridge_device(struct pci_dev *dev);
  271. void pci_put_host_bridge_device(struct device *dev);
  272. int pci_configure_extended_tags(struct pci_dev *dev, void *ign);
  273. bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *pl,
  274. int rrs_timeout);
  275. bool pci_bus_generic_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *pl,
  276. int rrs_timeout);
  277. int pci_idt_bus_quirk(struct pci_bus *bus, int devfn, u32 *pl, int rrs_timeout);
  278. int pci_setup_device(struct pci_dev *dev);
  279. int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
  280. struct resource *res, unsigned int reg);
  281. void pci_configure_ari(struct pci_dev *dev);
  282. void __pci_bus_size_bridges(struct pci_bus *bus,
  283. struct list_head *realloc_head);
  284. void __pci_bus_assign_resources(const struct pci_bus *bus,
  285. struct list_head *realloc_head,
  286. struct list_head *fail_head);
  287. bool pci_bus_clip_resource(struct pci_dev *dev, int idx);
  288. const char *pci_resource_name(struct pci_dev *dev, unsigned int i);
  289. void pci_reassigndev_resource_alignment(struct pci_dev *dev);
  290. void pci_disable_bridge_window(struct pci_dev *dev);
  291. struct pci_bus *pci_bus_get(struct pci_bus *bus);
  292. void pci_bus_put(struct pci_bus *bus);
  293. /* PCIe link information from Link Capabilities 2 */
  294. #define PCIE_LNKCAP2_SLS2SPEED(lnkcap2) \
  295. ((lnkcap2) & PCI_EXP_LNKCAP2_SLS_64_0GB ? PCIE_SPEED_64_0GT : \
  296. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_32_0GB ? PCIE_SPEED_32_0GT : \
  297. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_16_0GB ? PCIE_SPEED_16_0GT : \
  298. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_8_0GB ? PCIE_SPEED_8_0GT : \
  299. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_5_0GB ? PCIE_SPEED_5_0GT : \
  300. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_2_5GB ? PCIE_SPEED_2_5GT : \
  301. PCI_SPEED_UNKNOWN)
  302. /* PCIe speed to Mb/s reduced by encoding overhead */
  303. #define PCIE_SPEED2MBS_ENC(speed) \
  304. ((speed) == PCIE_SPEED_64_0GT ? 64000*1/1 : \
  305. (speed) == PCIE_SPEED_32_0GT ? 32000*128/130 : \
  306. (speed) == PCIE_SPEED_16_0GT ? 16000*128/130 : \
  307. (speed) == PCIE_SPEED_8_0GT ? 8000*128/130 : \
  308. (speed) == PCIE_SPEED_5_0GT ? 5000*8/10 : \
  309. (speed) == PCIE_SPEED_2_5GT ? 2500*8/10 : \
  310. 0)
  311. static inline int pcie_dev_speed_mbps(enum pci_bus_speed speed)
  312. {
  313. switch (speed) {
  314. case PCIE_SPEED_2_5GT:
  315. return 2500;
  316. case PCIE_SPEED_5_0GT:
  317. return 5000;
  318. case PCIE_SPEED_8_0GT:
  319. return 8000;
  320. case PCIE_SPEED_16_0GT:
  321. return 16000;
  322. case PCIE_SPEED_32_0GT:
  323. return 32000;
  324. case PCIE_SPEED_64_0GT:
  325. return 64000;
  326. default:
  327. break;
  328. }
  329. return -EINVAL;
  330. }
  331. u8 pcie_get_supported_speeds(struct pci_dev *dev);
  332. const char *pci_speed_string(enum pci_bus_speed speed);
  333. enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev);
  334. enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev);
  335. void __pcie_print_link_status(struct pci_dev *dev, bool verbose);
  336. void pcie_report_downtraining(struct pci_dev *dev);
  337. void pcie_update_link_speed(struct pci_bus *bus, u16 link_status);
  338. /* Single Root I/O Virtualization */
  339. struct pci_sriov {
  340. int pos; /* Capability position */
  341. int nres; /* Number of resources */
  342. u32 cap; /* SR-IOV Capabilities */
  343. u16 ctrl; /* SR-IOV Control */
  344. u16 total_VFs; /* Total VFs associated with the PF */
  345. u16 initial_VFs; /* Initial VFs associated with the PF */
  346. u16 num_VFs; /* Number of VFs available */
  347. u16 offset; /* First VF Routing ID offset */
  348. u16 stride; /* Following VF stride */
  349. u16 vf_device; /* VF device ID */
  350. u32 pgsz; /* Page size for BAR alignment */
  351. u8 link; /* Function Dependency Link */
  352. u8 max_VF_buses; /* Max buses consumed by VFs */
  353. u16 driver_max_VFs; /* Max num VFs driver supports */
  354. struct pci_dev *dev; /* Lowest numbered PF */
  355. struct pci_dev *self; /* This PF */
  356. u32 class; /* VF device */
  357. u8 hdr_type; /* VF header type */
  358. u16 subsystem_vendor; /* VF subsystem vendor */
  359. u16 subsystem_device; /* VF subsystem device */
  360. resource_size_t barsz[PCI_SRIOV_NUM_BARS]; /* VF BAR size */
  361. bool drivers_autoprobe; /* Auto probing of VFs by driver */
  362. };
  363. #ifdef CONFIG_PCI_DOE
  364. void pci_doe_init(struct pci_dev *pdev);
  365. void pci_doe_destroy(struct pci_dev *pdev);
  366. void pci_doe_disconnected(struct pci_dev *pdev);
  367. #else
  368. static inline void pci_doe_init(struct pci_dev *pdev) { }
  369. static inline void pci_doe_destroy(struct pci_dev *pdev) { }
  370. static inline void pci_doe_disconnected(struct pci_dev *pdev) { }
  371. #endif
  372. #ifdef CONFIG_PCI_NPEM
  373. void pci_npem_create(struct pci_dev *dev);
  374. void pci_npem_remove(struct pci_dev *dev);
  375. #else
  376. static inline void pci_npem_create(struct pci_dev *dev) { }
  377. static inline void pci_npem_remove(struct pci_dev *dev) { }
  378. #endif
  379. /**
  380. * pci_dev_set_io_state - Set the new error state if possible.
  381. *
  382. * @dev: PCI device to set new error_state
  383. * @new: the state we want dev to be in
  384. *
  385. * If the device is experiencing perm_failure, it has to remain in that state.
  386. * Any other transition is allowed.
  387. *
  388. * Returns true if state has been changed to the requested state.
  389. */
  390. static inline bool pci_dev_set_io_state(struct pci_dev *dev,
  391. pci_channel_state_t new)
  392. {
  393. pci_channel_state_t old;
  394. switch (new) {
  395. case pci_channel_io_perm_failure:
  396. xchg(&dev->error_state, pci_channel_io_perm_failure);
  397. return true;
  398. case pci_channel_io_frozen:
  399. old = cmpxchg(&dev->error_state, pci_channel_io_normal,
  400. pci_channel_io_frozen);
  401. return old != pci_channel_io_perm_failure;
  402. case pci_channel_io_normal:
  403. old = cmpxchg(&dev->error_state, pci_channel_io_frozen,
  404. pci_channel_io_normal);
  405. return old != pci_channel_io_perm_failure;
  406. default:
  407. return false;
  408. }
  409. }
  410. static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused)
  411. {
  412. pci_dev_set_io_state(dev, pci_channel_io_perm_failure);
  413. pci_doe_disconnected(dev);
  414. return 0;
  415. }
  416. /* pci_dev priv_flags */
  417. #define PCI_DEV_ADDED 0
  418. #define PCI_DPC_RECOVERED 1
  419. #define PCI_DPC_RECOVERING 2
  420. static inline void pci_dev_assign_added(struct pci_dev *dev, bool added)
  421. {
  422. assign_bit(PCI_DEV_ADDED, &dev->priv_flags, added);
  423. }
  424. static inline bool pci_dev_is_added(const struct pci_dev *dev)
  425. {
  426. return test_bit(PCI_DEV_ADDED, &dev->priv_flags);
  427. }
  428. #ifdef CONFIG_PCIEAER
  429. #include <linux/aer.h>
  430. #define AER_MAX_MULTI_ERR_DEVICES 5 /* Not likely to have more */
  431. struct aer_err_info {
  432. struct pci_dev *dev[AER_MAX_MULTI_ERR_DEVICES];
  433. int error_dev_num;
  434. unsigned int id:16;
  435. unsigned int severity:2; /* 0:NONFATAL | 1:FATAL | 2:COR */
  436. unsigned int __pad1:5;
  437. unsigned int multi_error_valid:1;
  438. unsigned int first_error:5;
  439. unsigned int __pad2:2;
  440. unsigned int tlp_header_valid:1;
  441. unsigned int status; /* COR/UNCOR Error Status */
  442. unsigned int mask; /* COR/UNCOR Error Mask */
  443. struct pcie_tlp_log tlp; /* TLP Header */
  444. };
  445. int aer_get_device_error_info(struct pci_dev *dev, struct aer_err_info *info);
  446. void aer_print_error(struct pci_dev *dev, struct aer_err_info *info);
  447. #endif /* CONFIG_PCIEAER */
  448. #ifdef CONFIG_PCIEPORTBUS
  449. /* Cached RCEC Endpoint Association */
  450. struct rcec_ea {
  451. u8 nextbusn;
  452. u8 lastbusn;
  453. u32 bitmap;
  454. };
  455. #endif
  456. #ifdef CONFIG_PCIE_DPC
  457. void pci_save_dpc_state(struct pci_dev *dev);
  458. void pci_restore_dpc_state(struct pci_dev *dev);
  459. void pci_dpc_init(struct pci_dev *pdev);
  460. void dpc_process_error(struct pci_dev *pdev);
  461. pci_ers_result_t dpc_reset_link(struct pci_dev *pdev);
  462. bool pci_dpc_recovered(struct pci_dev *pdev);
  463. #else
  464. static inline void pci_save_dpc_state(struct pci_dev *dev) { }
  465. static inline void pci_restore_dpc_state(struct pci_dev *dev) { }
  466. static inline void pci_dpc_init(struct pci_dev *pdev) { }
  467. static inline bool pci_dpc_recovered(struct pci_dev *pdev) { return false; }
  468. #endif
  469. #ifdef CONFIG_PCIEPORTBUS
  470. void pci_rcec_init(struct pci_dev *dev);
  471. void pci_rcec_exit(struct pci_dev *dev);
  472. void pcie_link_rcec(struct pci_dev *rcec);
  473. void pcie_walk_rcec(struct pci_dev *rcec,
  474. int (*cb)(struct pci_dev *, void *),
  475. void *userdata);
  476. #else
  477. static inline void pci_rcec_init(struct pci_dev *dev) { }
  478. static inline void pci_rcec_exit(struct pci_dev *dev) { }
  479. static inline void pcie_link_rcec(struct pci_dev *rcec) { }
  480. static inline void pcie_walk_rcec(struct pci_dev *rcec,
  481. int (*cb)(struct pci_dev *, void *),
  482. void *userdata) { }
  483. #endif
  484. #ifdef CONFIG_PCI_ATS
  485. /* Address Translation Service */
  486. void pci_ats_init(struct pci_dev *dev);
  487. void pci_restore_ats_state(struct pci_dev *dev);
  488. #else
  489. static inline void pci_ats_init(struct pci_dev *d) { }
  490. static inline void pci_restore_ats_state(struct pci_dev *dev) { }
  491. #endif /* CONFIG_PCI_ATS */
  492. #ifdef CONFIG_PCI_PRI
  493. void pci_pri_init(struct pci_dev *dev);
  494. void pci_restore_pri_state(struct pci_dev *pdev);
  495. #else
  496. static inline void pci_pri_init(struct pci_dev *dev) { }
  497. static inline void pci_restore_pri_state(struct pci_dev *pdev) { }
  498. #endif
  499. #ifdef CONFIG_PCI_PASID
  500. void pci_pasid_init(struct pci_dev *dev);
  501. void pci_restore_pasid_state(struct pci_dev *pdev);
  502. #else
  503. static inline void pci_pasid_init(struct pci_dev *dev) { }
  504. static inline void pci_restore_pasid_state(struct pci_dev *pdev) { }
  505. #endif
  506. #ifdef CONFIG_PCI_IOV
  507. int pci_iov_init(struct pci_dev *dev);
  508. void pci_iov_release(struct pci_dev *dev);
  509. void pci_iov_remove(struct pci_dev *dev);
  510. void pci_iov_update_resource(struct pci_dev *dev, int resno);
  511. resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno);
  512. void pci_restore_iov_state(struct pci_dev *dev);
  513. int pci_iov_bus_range(struct pci_bus *bus);
  514. extern const struct attribute_group sriov_pf_dev_attr_group;
  515. extern const struct attribute_group sriov_vf_dev_attr_group;
  516. #else
  517. static inline int pci_iov_init(struct pci_dev *dev)
  518. {
  519. return -ENODEV;
  520. }
  521. static inline void pci_iov_release(struct pci_dev *dev) { }
  522. static inline void pci_iov_remove(struct pci_dev *dev) { }
  523. static inline void pci_restore_iov_state(struct pci_dev *dev) { }
  524. static inline int pci_iov_bus_range(struct pci_bus *bus)
  525. {
  526. return 0;
  527. }
  528. #endif /* CONFIG_PCI_IOV */
  529. #ifdef CONFIG_PCIE_PTM
  530. void pci_ptm_init(struct pci_dev *dev);
  531. void pci_save_ptm_state(struct pci_dev *dev);
  532. void pci_restore_ptm_state(struct pci_dev *dev);
  533. void pci_suspend_ptm(struct pci_dev *dev);
  534. void pci_resume_ptm(struct pci_dev *dev);
  535. #else
  536. static inline void pci_ptm_init(struct pci_dev *dev) { }
  537. static inline void pci_save_ptm_state(struct pci_dev *dev) { }
  538. static inline void pci_restore_ptm_state(struct pci_dev *dev) { }
  539. static inline void pci_suspend_ptm(struct pci_dev *dev) { }
  540. static inline void pci_resume_ptm(struct pci_dev *dev) { }
  541. #endif
  542. unsigned long pci_cardbus_resource_alignment(struct resource *);
  543. static inline resource_size_t pci_resource_alignment(struct pci_dev *dev,
  544. struct resource *res)
  545. {
  546. #ifdef CONFIG_PCI_IOV
  547. int resno = res - dev->resource;
  548. if (resno >= PCI_IOV_RESOURCES && resno <= PCI_IOV_RESOURCE_END)
  549. return pci_sriov_resource_alignment(dev, resno);
  550. #endif
  551. if (dev->class >> 8 == PCI_CLASS_BRIDGE_CARDBUS)
  552. return pci_cardbus_resource_alignment(res);
  553. return resource_alignment(res);
  554. }
  555. void pci_acs_init(struct pci_dev *dev);
  556. #ifdef CONFIG_PCI_QUIRKS
  557. int pci_dev_specific_acs_enabled(struct pci_dev *dev, u16 acs_flags);
  558. int pci_dev_specific_enable_acs(struct pci_dev *dev);
  559. int pci_dev_specific_disable_acs_redir(struct pci_dev *dev);
  560. int pcie_failed_link_retrain(struct pci_dev *dev);
  561. #else
  562. static inline int pci_dev_specific_acs_enabled(struct pci_dev *dev,
  563. u16 acs_flags)
  564. {
  565. return -ENOTTY;
  566. }
  567. static inline int pci_dev_specific_enable_acs(struct pci_dev *dev)
  568. {
  569. return -ENOTTY;
  570. }
  571. static inline int pci_dev_specific_disable_acs_redir(struct pci_dev *dev)
  572. {
  573. return -ENOTTY;
  574. }
  575. static inline int pcie_failed_link_retrain(struct pci_dev *dev)
  576. {
  577. return -ENOTTY;
  578. }
  579. #endif
  580. /* PCI error reporting and recovery */
  581. pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
  582. pci_channel_state_t state,
  583. pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev));
  584. bool pcie_wait_for_link(struct pci_dev *pdev, bool active);
  585. int pcie_retrain_link(struct pci_dev *pdev, bool use_lt);
  586. /* ASPM-related functionality we need even without CONFIG_PCIEASPM */
  587. void pci_save_ltr_state(struct pci_dev *dev);
  588. void pci_restore_ltr_state(struct pci_dev *dev);
  589. void pci_configure_aspm_l1ss(struct pci_dev *dev);
  590. void pci_save_aspm_l1ss_state(struct pci_dev *dev);
  591. void pci_restore_aspm_l1ss_state(struct pci_dev *dev);
  592. #ifdef CONFIG_PCIEASPM
  593. void pcie_aspm_init_link_state(struct pci_dev *pdev);
  594. void pcie_aspm_exit_link_state(struct pci_dev *pdev);
  595. void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked);
  596. void pcie_aspm_powersave_config_link(struct pci_dev *pdev);
  597. void pci_configure_ltr(struct pci_dev *pdev);
  598. void pci_bridge_reconfigure_ltr(struct pci_dev *pdev);
  599. #else
  600. static inline void pcie_aspm_init_link_state(struct pci_dev *pdev) { }
  601. static inline void pcie_aspm_exit_link_state(struct pci_dev *pdev) { }
  602. static inline void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked) { }
  603. static inline void pcie_aspm_powersave_config_link(struct pci_dev *pdev) { }
  604. static inline void pci_configure_ltr(struct pci_dev *pdev) { }
  605. static inline void pci_bridge_reconfigure_ltr(struct pci_dev *pdev) { }
  606. #endif
  607. #ifdef CONFIG_PCIE_ECRC
  608. void pcie_set_ecrc_checking(struct pci_dev *dev);
  609. void pcie_ecrc_get_policy(char *str);
  610. #else
  611. static inline void pcie_set_ecrc_checking(struct pci_dev *dev) { }
  612. static inline void pcie_ecrc_get_policy(char *str) { }
  613. #endif
  614. struct pci_dev_reset_methods {
  615. u16 vendor;
  616. u16 device;
  617. int (*reset)(struct pci_dev *dev, bool probe);
  618. };
  619. struct pci_reset_fn_method {
  620. int (*reset_fn)(struct pci_dev *pdev, bool probe);
  621. char *name;
  622. };
  623. #ifdef CONFIG_PCI_QUIRKS
  624. int pci_dev_specific_reset(struct pci_dev *dev, bool probe);
  625. #else
  626. static inline int pci_dev_specific_reset(struct pci_dev *dev, bool probe)
  627. {
  628. return -ENOTTY;
  629. }
  630. #endif
  631. #if defined(CONFIG_PCI_QUIRKS) && defined(CONFIG_ARM64)
  632. int acpi_get_rc_resources(struct device *dev, const char *hid, u16 segment,
  633. struct resource *res);
  634. #else
  635. static inline int acpi_get_rc_resources(struct device *dev, const char *hid,
  636. u16 segment, struct resource *res)
  637. {
  638. return -ENODEV;
  639. }
  640. #endif
  641. int pci_rebar_get_current_size(struct pci_dev *pdev, int bar);
  642. int pci_rebar_set_size(struct pci_dev *pdev, int bar, int size);
  643. static inline u64 pci_rebar_size_to_bytes(int size)
  644. {
  645. return 1ULL << (size + 20);
  646. }
  647. struct device_node;
  648. #ifdef CONFIG_OF
  649. int of_pci_parse_bus_range(struct device_node *node, struct resource *res);
  650. int of_get_pci_domain_nr(struct device_node *node);
  651. int of_pci_get_max_link_speed(struct device_node *node);
  652. u32 of_pci_get_slot_power_limit(struct device_node *node,
  653. u8 *slot_power_limit_value,
  654. u8 *slot_power_limit_scale);
  655. bool of_pci_preserve_config(struct device_node *node);
  656. int pci_set_of_node(struct pci_dev *dev);
  657. void pci_release_of_node(struct pci_dev *dev);
  658. void pci_set_bus_of_node(struct pci_bus *bus);
  659. void pci_release_bus_of_node(struct pci_bus *bus);
  660. int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge);
  661. #else
  662. static inline int
  663. of_pci_parse_bus_range(struct device_node *node, struct resource *res)
  664. {
  665. return -EINVAL;
  666. }
  667. static inline int
  668. of_get_pci_domain_nr(struct device_node *node)
  669. {
  670. return -1;
  671. }
  672. static inline int
  673. of_pci_get_max_link_speed(struct device_node *node)
  674. {
  675. return -EINVAL;
  676. }
  677. static inline u32
  678. of_pci_get_slot_power_limit(struct device_node *node,
  679. u8 *slot_power_limit_value,
  680. u8 *slot_power_limit_scale)
  681. {
  682. if (slot_power_limit_value)
  683. *slot_power_limit_value = 0;
  684. if (slot_power_limit_scale)
  685. *slot_power_limit_scale = 0;
  686. return 0;
  687. }
  688. static inline bool of_pci_preserve_config(struct device_node *node)
  689. {
  690. return false;
  691. }
  692. static inline int pci_set_of_node(struct pci_dev *dev) { return 0; }
  693. static inline void pci_release_of_node(struct pci_dev *dev) { }
  694. static inline void pci_set_bus_of_node(struct pci_bus *bus) { }
  695. static inline void pci_release_bus_of_node(struct pci_bus *bus) { }
  696. static inline int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge)
  697. {
  698. return 0;
  699. }
  700. #endif /* CONFIG_OF */
  701. struct of_changeset;
  702. #ifdef CONFIG_PCI_DYNAMIC_OF_NODES
  703. void of_pci_make_dev_node(struct pci_dev *pdev);
  704. void of_pci_remove_node(struct pci_dev *pdev);
  705. int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs,
  706. struct device_node *np);
  707. #else
  708. static inline void of_pci_make_dev_node(struct pci_dev *pdev) { }
  709. static inline void of_pci_remove_node(struct pci_dev *pdev) { }
  710. #endif
  711. #ifdef CONFIG_PCIEAER
  712. void pci_no_aer(void);
  713. void pci_aer_init(struct pci_dev *dev);
  714. void pci_aer_exit(struct pci_dev *dev);
  715. extern const struct attribute_group aer_stats_attr_group;
  716. void pci_aer_clear_fatal_status(struct pci_dev *dev);
  717. int pci_aer_clear_status(struct pci_dev *dev);
  718. int pci_aer_raw_clear_status(struct pci_dev *dev);
  719. void pci_save_aer_state(struct pci_dev *dev);
  720. void pci_restore_aer_state(struct pci_dev *dev);
  721. #else
  722. static inline void pci_no_aer(void) { }
  723. static inline void pci_aer_init(struct pci_dev *d) { }
  724. static inline void pci_aer_exit(struct pci_dev *d) { }
  725. static inline void pci_aer_clear_fatal_status(struct pci_dev *dev) { }
  726. static inline int pci_aer_clear_status(struct pci_dev *dev) { return -EINVAL; }
  727. static inline int pci_aer_raw_clear_status(struct pci_dev *dev) { return -EINVAL; }
  728. static inline void pci_save_aer_state(struct pci_dev *dev) { }
  729. static inline void pci_restore_aer_state(struct pci_dev *dev) { }
  730. #endif
  731. #ifdef CONFIG_ACPI
  732. bool pci_acpi_preserve_config(struct pci_host_bridge *bridge);
  733. int pci_acpi_program_hp_params(struct pci_dev *dev);
  734. extern const struct attribute_group pci_dev_acpi_attr_group;
  735. void pci_set_acpi_fwnode(struct pci_dev *dev);
  736. int pci_dev_acpi_reset(struct pci_dev *dev, bool probe);
  737. bool acpi_pci_power_manageable(struct pci_dev *dev);
  738. bool acpi_pci_bridge_d3(struct pci_dev *dev);
  739. int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state);
  740. pci_power_t acpi_pci_get_power_state(struct pci_dev *dev);
  741. void acpi_pci_refresh_power_state(struct pci_dev *dev);
  742. int acpi_pci_wakeup(struct pci_dev *dev, bool enable);
  743. bool acpi_pci_need_resume(struct pci_dev *dev);
  744. pci_power_t acpi_pci_choose_state(struct pci_dev *pdev);
  745. #else
  746. static inline bool pci_acpi_preserve_config(struct pci_host_bridge *bridge)
  747. {
  748. return false;
  749. }
  750. static inline int pci_dev_acpi_reset(struct pci_dev *dev, bool probe)
  751. {
  752. return -ENOTTY;
  753. }
  754. static inline void pci_set_acpi_fwnode(struct pci_dev *dev) { }
  755. static inline int pci_acpi_program_hp_params(struct pci_dev *dev)
  756. {
  757. return -ENODEV;
  758. }
  759. static inline bool acpi_pci_power_manageable(struct pci_dev *dev)
  760. {
  761. return false;
  762. }
  763. static inline bool acpi_pci_bridge_d3(struct pci_dev *dev)
  764. {
  765. return false;
  766. }
  767. static inline int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  768. {
  769. return -ENODEV;
  770. }
  771. static inline pci_power_t acpi_pci_get_power_state(struct pci_dev *dev)
  772. {
  773. return PCI_UNKNOWN;
  774. }
  775. static inline void acpi_pci_refresh_power_state(struct pci_dev *dev) { }
  776. static inline int acpi_pci_wakeup(struct pci_dev *dev, bool enable)
  777. {
  778. return -ENODEV;
  779. }
  780. static inline bool acpi_pci_need_resume(struct pci_dev *dev)
  781. {
  782. return false;
  783. }
  784. static inline pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
  785. {
  786. return PCI_POWER_ERROR;
  787. }
  788. #endif
  789. #ifdef CONFIG_PCIEASPM
  790. extern const struct attribute_group aspm_ctrl_attr_group;
  791. #endif
  792. extern const struct attribute_group pci_dev_reset_method_attr_group;
  793. #ifdef CONFIG_X86_INTEL_MID
  794. bool pci_use_mid_pm(void);
  795. int mid_pci_set_power_state(struct pci_dev *pdev, pci_power_t state);
  796. pci_power_t mid_pci_get_power_state(struct pci_dev *pdev);
  797. #else
  798. static inline bool pci_use_mid_pm(void)
  799. {
  800. return false;
  801. }
  802. static inline int mid_pci_set_power_state(struct pci_dev *pdev, pci_power_t state)
  803. {
  804. return -ENODEV;
  805. }
  806. static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev)
  807. {
  808. return PCI_UNKNOWN;
  809. }
  810. #endif
  811. int pcim_intx(struct pci_dev *dev, int enable);
  812. int pcim_request_region_exclusive(struct pci_dev *pdev, int bar,
  813. const char *name);
  814. void pcim_release_region(struct pci_dev *pdev, int bar);
  815. /*
  816. * Config Address for PCI Configuration Mechanism #1
  817. *
  818. * See PCI Local Bus Specification, Revision 3.0,
  819. * Section 3.2.2.3.2, Figure 3-2, p. 50.
  820. */
  821. #define PCI_CONF1_BUS_SHIFT 16 /* Bus number */
  822. #define PCI_CONF1_DEV_SHIFT 11 /* Device number */
  823. #define PCI_CONF1_FUNC_SHIFT 8 /* Function number */
  824. #define PCI_CONF1_BUS_MASK 0xff
  825. #define PCI_CONF1_DEV_MASK 0x1f
  826. #define PCI_CONF1_FUNC_MASK 0x7
  827. #define PCI_CONF1_REG_MASK 0xfc /* Limit aligned offset to a maximum of 256B */
  828. #define PCI_CONF1_ENABLE BIT(31)
  829. #define PCI_CONF1_BUS(x) (((x) & PCI_CONF1_BUS_MASK) << PCI_CONF1_BUS_SHIFT)
  830. #define PCI_CONF1_DEV(x) (((x) & PCI_CONF1_DEV_MASK) << PCI_CONF1_DEV_SHIFT)
  831. #define PCI_CONF1_FUNC(x) (((x) & PCI_CONF1_FUNC_MASK) << PCI_CONF1_FUNC_SHIFT)
  832. #define PCI_CONF1_REG(x) ((x) & PCI_CONF1_REG_MASK)
  833. #define PCI_CONF1_ADDRESS(bus, dev, func, reg) \
  834. (PCI_CONF1_ENABLE | \
  835. PCI_CONF1_BUS(bus) | \
  836. PCI_CONF1_DEV(dev) | \
  837. PCI_CONF1_FUNC(func) | \
  838. PCI_CONF1_REG(reg))
  839. /*
  840. * Extension of PCI Config Address for accessing extended PCIe registers
  841. *
  842. * No standardized specification, but used on lot of non-ECAM-compliant ARM SoCs
  843. * or on AMD Barcelona and new CPUs. Reserved bits [27:24] of PCI Config Address
  844. * are used for specifying additional 4 high bits of PCI Express register.
  845. */
  846. #define PCI_CONF1_EXT_REG_SHIFT 16
  847. #define PCI_CONF1_EXT_REG_MASK 0xf00
  848. #define PCI_CONF1_EXT_REG(x) (((x) & PCI_CONF1_EXT_REG_MASK) << PCI_CONF1_EXT_REG_SHIFT)
  849. #define PCI_CONF1_EXT_ADDRESS(bus, dev, func, reg) \
  850. (PCI_CONF1_ADDRESS(bus, dev, func, reg) | \
  851. PCI_CONF1_EXT_REG(reg))
  852. #endif /* DRIVERS_PCI_H */