pci.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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_DEVICE_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. static inline bool pci_has_subordinate(struct pci_dev *pci_dev)
  142. {
  143. return !!(pci_dev->subordinate);
  144. }
  145. static inline bool pci_power_manageable(struct pci_dev *pci_dev)
  146. {
  147. /*
  148. * Currently we allow normal PCI devices and PCI bridges transition
  149. * into D3 if their bridge_d3 is set.
  150. */
  151. return !pci_has_subordinate(pci_dev) || pci_dev->bridge_d3;
  152. }
  153. static inline bool pcie_downstream_port(const struct pci_dev *dev)
  154. {
  155. int type = pci_pcie_type(dev);
  156. return type == PCI_EXP_TYPE_ROOT_PORT ||
  157. type == PCI_EXP_TYPE_DOWNSTREAM ||
  158. type == PCI_EXP_TYPE_PCIE_BRIDGE;
  159. }
  160. void pci_vpd_init(struct pci_dev *dev);
  161. extern const struct attribute_group pci_dev_vpd_attr_group;
  162. /* PCI Virtual Channel */
  163. int pci_save_vc_state(struct pci_dev *dev);
  164. void pci_restore_vc_state(struct pci_dev *dev);
  165. void pci_allocate_vc_save_buffers(struct pci_dev *dev);
  166. /* PCI /proc functions */
  167. #ifdef CONFIG_PROC_FS
  168. int pci_proc_attach_device(struct pci_dev *dev);
  169. int pci_proc_detach_device(struct pci_dev *dev);
  170. int pci_proc_detach_bus(struct pci_bus *bus);
  171. #else
  172. static inline int pci_proc_attach_device(struct pci_dev *dev) { return 0; }
  173. static inline int pci_proc_detach_device(struct pci_dev *dev) { return 0; }
  174. static inline int pci_proc_detach_bus(struct pci_bus *bus) { return 0; }
  175. #endif
  176. /* Functions for PCI Hotplug drivers to use */
  177. int pci_hp_add_bridge(struct pci_dev *dev);
  178. #if defined(CONFIG_SYSFS) && defined(HAVE_PCI_LEGACY)
  179. void pci_create_legacy_files(struct pci_bus *bus);
  180. void pci_remove_legacy_files(struct pci_bus *bus);
  181. #else
  182. static inline void pci_create_legacy_files(struct pci_bus *bus) { }
  183. static inline void pci_remove_legacy_files(struct pci_bus *bus) { }
  184. #endif
  185. /* Lock for read/write access to pci device and bus lists */
  186. extern struct rw_semaphore pci_bus_sem;
  187. extern struct mutex pci_slot_mutex;
  188. extern raw_spinlock_t pci_lock;
  189. extern unsigned int pci_pm_d3hot_delay;
  190. #ifdef CONFIG_PCI_MSI
  191. void pci_no_msi(void);
  192. #else
  193. static inline void pci_no_msi(void) { }
  194. #endif
  195. void pci_realloc_get_opt(char *);
  196. static inline int pci_no_d1d2(struct pci_dev *dev)
  197. {
  198. unsigned int parent_dstates = 0;
  199. if (dev->bus->self)
  200. parent_dstates = dev->bus->self->no_d1d2;
  201. return (dev->no_d1d2 || parent_dstates);
  202. }
  203. #ifdef CONFIG_SYSFS
  204. int pci_create_sysfs_dev_files(struct pci_dev *pdev);
  205. void pci_remove_sysfs_dev_files(struct pci_dev *pdev);
  206. extern const struct attribute_group *pci_dev_groups[];
  207. extern const struct attribute_group *pci_dev_attr_groups[];
  208. extern const struct attribute_group *pcibus_groups[];
  209. extern const struct attribute_group *pci_bus_groups[];
  210. #else
  211. static inline int pci_create_sysfs_dev_files(struct pci_dev *pdev) { return 0; }
  212. static inline void pci_remove_sysfs_dev_files(struct pci_dev *pdev) { }
  213. #define pci_dev_groups NULL
  214. #define pci_dev_attr_groups NULL
  215. #define pcibus_groups NULL
  216. #define pci_bus_groups NULL
  217. #endif
  218. extern unsigned long pci_hotplug_io_size;
  219. extern unsigned long pci_hotplug_mmio_size;
  220. extern unsigned long pci_hotplug_mmio_pref_size;
  221. extern unsigned long pci_hotplug_bus_size;
  222. /**
  223. * pci_match_one_device - Tell if a PCI device structure has a matching
  224. * PCI device id structure
  225. * @id: single PCI device id structure to match
  226. * @dev: the PCI device structure to match against
  227. *
  228. * Returns the matching pci_device_id structure or %NULL if there is no match.
  229. */
  230. static inline const struct pci_device_id *
  231. pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
  232. {
  233. if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) &&
  234. (id->device == PCI_ANY_ID || id->device == dev->device) &&
  235. (id->subvendor == PCI_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
  236. (id->subdevice == PCI_ANY_ID || id->subdevice == dev->subsystem_device) &&
  237. !((id->class ^ dev->class) & id->class_mask))
  238. return id;
  239. return NULL;
  240. }
  241. /* PCI slot sysfs helper code */
  242. #define to_pci_slot(s) container_of(s, struct pci_slot, kobj)
  243. extern struct kset *pci_slots_kset;
  244. struct pci_slot_attribute {
  245. struct attribute attr;
  246. ssize_t (*show)(struct pci_slot *, char *);
  247. ssize_t (*store)(struct pci_slot *, const char *, size_t);
  248. };
  249. #define to_pci_slot_attr(s) container_of(s, struct pci_slot_attribute, attr)
  250. enum pci_bar_type {
  251. pci_bar_unknown, /* Standard PCI BAR probe */
  252. pci_bar_io, /* An I/O port BAR */
  253. pci_bar_mem32, /* A 32-bit memory BAR */
  254. pci_bar_mem64, /* A 64-bit memory BAR */
  255. };
  256. struct device *pci_get_host_bridge_device(struct pci_dev *dev);
  257. void pci_put_host_bridge_device(struct device *dev);
  258. int pci_configure_extended_tags(struct pci_dev *dev, void *ign);
  259. bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *pl,
  260. int rrs_timeout);
  261. bool pci_bus_generic_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *pl,
  262. int rrs_timeout);
  263. int pci_idt_bus_quirk(struct pci_bus *bus, int devfn, u32 *pl, int rrs_timeout);
  264. int pci_setup_device(struct pci_dev *dev);
  265. int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
  266. struct resource *res, unsigned int reg);
  267. void pci_configure_ari(struct pci_dev *dev);
  268. void __pci_bus_size_bridges(struct pci_bus *bus,
  269. struct list_head *realloc_head);
  270. void __pci_bus_assign_resources(const struct pci_bus *bus,
  271. struct list_head *realloc_head,
  272. struct list_head *fail_head);
  273. bool pci_bus_clip_resource(struct pci_dev *dev, int idx);
  274. const char *pci_resource_name(struct pci_dev *dev, unsigned int i);
  275. void pci_reassigndev_resource_alignment(struct pci_dev *dev);
  276. void pci_disable_bridge_window(struct pci_dev *dev);
  277. struct pci_bus *pci_bus_get(struct pci_bus *bus);
  278. void pci_bus_put(struct pci_bus *bus);
  279. /* PCIe link information from Link Capabilities 2 */
  280. #define PCIE_LNKCAP2_SLS2SPEED(lnkcap2) \
  281. ((lnkcap2) & PCI_EXP_LNKCAP2_SLS_64_0GB ? PCIE_SPEED_64_0GT : \
  282. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_32_0GB ? PCIE_SPEED_32_0GT : \
  283. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_16_0GB ? PCIE_SPEED_16_0GT : \
  284. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_8_0GB ? PCIE_SPEED_8_0GT : \
  285. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_5_0GB ? PCIE_SPEED_5_0GT : \
  286. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_2_5GB ? PCIE_SPEED_2_5GT : \
  287. PCI_SPEED_UNKNOWN)
  288. /* PCIe speed to Mb/s reduced by encoding overhead */
  289. #define PCIE_SPEED2MBS_ENC(speed) \
  290. ((speed) == PCIE_SPEED_64_0GT ? 64000*1/1 : \
  291. (speed) == PCIE_SPEED_32_0GT ? 32000*128/130 : \
  292. (speed) == PCIE_SPEED_16_0GT ? 16000*128/130 : \
  293. (speed) == PCIE_SPEED_8_0GT ? 8000*128/130 : \
  294. (speed) == PCIE_SPEED_5_0GT ? 5000*8/10 : \
  295. (speed) == PCIE_SPEED_2_5GT ? 2500*8/10 : \
  296. 0)
  297. static inline int pcie_dev_speed_mbps(enum pci_bus_speed speed)
  298. {
  299. switch (speed) {
  300. case PCIE_SPEED_2_5GT:
  301. return 2500;
  302. case PCIE_SPEED_5_0GT:
  303. return 5000;
  304. case PCIE_SPEED_8_0GT:
  305. return 8000;
  306. case PCIE_SPEED_16_0GT:
  307. return 16000;
  308. case PCIE_SPEED_32_0GT:
  309. return 32000;
  310. case PCIE_SPEED_64_0GT:
  311. return 64000;
  312. default:
  313. break;
  314. }
  315. return -EINVAL;
  316. }
  317. const char *pci_speed_string(enum pci_bus_speed speed);
  318. enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev);
  319. enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev);
  320. void __pcie_print_link_status(struct pci_dev *dev, bool verbose);
  321. void pcie_report_downtraining(struct pci_dev *dev);
  322. void pcie_update_link_speed(struct pci_bus *bus, u16 link_status);
  323. /* Single Root I/O Virtualization */
  324. struct pci_sriov {
  325. int pos; /* Capability position */
  326. int nres; /* Number of resources */
  327. u32 cap; /* SR-IOV Capabilities */
  328. u16 ctrl; /* SR-IOV Control */
  329. u16 total_VFs; /* Total VFs associated with the PF */
  330. u16 initial_VFs; /* Initial VFs associated with the PF */
  331. u16 num_VFs; /* Number of VFs available */
  332. u16 offset; /* First VF Routing ID offset */
  333. u16 stride; /* Following VF stride */
  334. u16 vf_device; /* VF device ID */
  335. u32 pgsz; /* Page size for BAR alignment */
  336. u8 link; /* Function Dependency Link */
  337. u8 max_VF_buses; /* Max buses consumed by VFs */
  338. u16 driver_max_VFs; /* Max num VFs driver supports */
  339. struct pci_dev *dev; /* Lowest numbered PF */
  340. struct pci_dev *self; /* This PF */
  341. u32 class; /* VF device */
  342. u8 hdr_type; /* VF header type */
  343. u16 subsystem_vendor; /* VF subsystem vendor */
  344. u16 subsystem_device; /* VF subsystem device */
  345. resource_size_t barsz[PCI_SRIOV_NUM_BARS]; /* VF BAR size */
  346. bool drivers_autoprobe; /* Auto probing of VFs by driver */
  347. };
  348. #ifdef CONFIG_PCI_DOE
  349. void pci_doe_init(struct pci_dev *pdev);
  350. void pci_doe_destroy(struct pci_dev *pdev);
  351. void pci_doe_disconnected(struct pci_dev *pdev);
  352. #else
  353. static inline void pci_doe_init(struct pci_dev *pdev) { }
  354. static inline void pci_doe_destroy(struct pci_dev *pdev) { }
  355. static inline void pci_doe_disconnected(struct pci_dev *pdev) { }
  356. #endif
  357. #ifdef CONFIG_PCI_NPEM
  358. void pci_npem_create(struct pci_dev *dev);
  359. void pci_npem_remove(struct pci_dev *dev);
  360. #else
  361. static inline void pci_npem_create(struct pci_dev *dev) { }
  362. static inline void pci_npem_remove(struct pci_dev *dev) { }
  363. #endif
  364. /**
  365. * pci_dev_set_io_state - Set the new error state if possible.
  366. *
  367. * @dev: PCI device to set new error_state
  368. * @new: the state we want dev to be in
  369. *
  370. * If the device is experiencing perm_failure, it has to remain in that state.
  371. * Any other transition is allowed.
  372. *
  373. * Returns true if state has been changed to the requested state.
  374. */
  375. static inline bool pci_dev_set_io_state(struct pci_dev *dev,
  376. pci_channel_state_t new)
  377. {
  378. pci_channel_state_t old;
  379. switch (new) {
  380. case pci_channel_io_perm_failure:
  381. xchg(&dev->error_state, pci_channel_io_perm_failure);
  382. return true;
  383. case pci_channel_io_frozen:
  384. old = cmpxchg(&dev->error_state, pci_channel_io_normal,
  385. pci_channel_io_frozen);
  386. return old != pci_channel_io_perm_failure;
  387. case pci_channel_io_normal:
  388. old = cmpxchg(&dev->error_state, pci_channel_io_frozen,
  389. pci_channel_io_normal);
  390. return old != pci_channel_io_perm_failure;
  391. default:
  392. return false;
  393. }
  394. }
  395. static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused)
  396. {
  397. pci_dev_set_io_state(dev, pci_channel_io_perm_failure);
  398. pci_doe_disconnected(dev);
  399. return 0;
  400. }
  401. /* pci_dev priv_flags */
  402. #define PCI_DEV_ADDED 0
  403. #define PCI_DPC_RECOVERED 1
  404. #define PCI_DPC_RECOVERING 2
  405. static inline void pci_dev_assign_added(struct pci_dev *dev, bool added)
  406. {
  407. assign_bit(PCI_DEV_ADDED, &dev->priv_flags, added);
  408. }
  409. static inline bool pci_dev_is_added(const struct pci_dev *dev)
  410. {
  411. return test_bit(PCI_DEV_ADDED, &dev->priv_flags);
  412. }
  413. #ifdef CONFIG_PCIEAER
  414. #include <linux/aer.h>
  415. #define AER_MAX_MULTI_ERR_DEVICES 5 /* Not likely to have more */
  416. struct aer_err_info {
  417. struct pci_dev *dev[AER_MAX_MULTI_ERR_DEVICES];
  418. int error_dev_num;
  419. unsigned int id:16;
  420. unsigned int severity:2; /* 0:NONFATAL | 1:FATAL | 2:COR */
  421. unsigned int __pad1:5;
  422. unsigned int multi_error_valid:1;
  423. unsigned int first_error:5;
  424. unsigned int __pad2:2;
  425. unsigned int tlp_header_valid:1;
  426. unsigned int status; /* COR/UNCOR Error Status */
  427. unsigned int mask; /* COR/UNCOR Error Mask */
  428. struct pcie_tlp_log tlp; /* TLP Header */
  429. };
  430. int aer_get_device_error_info(struct pci_dev *dev, struct aer_err_info *info);
  431. void aer_print_error(struct pci_dev *dev, struct aer_err_info *info);
  432. #endif /* CONFIG_PCIEAER */
  433. #ifdef CONFIG_PCIEPORTBUS
  434. /* Cached RCEC Endpoint Association */
  435. struct rcec_ea {
  436. u8 nextbusn;
  437. u8 lastbusn;
  438. u32 bitmap;
  439. };
  440. #endif
  441. #ifdef CONFIG_PCIE_DPC
  442. void pci_save_dpc_state(struct pci_dev *dev);
  443. void pci_restore_dpc_state(struct pci_dev *dev);
  444. void pci_dpc_init(struct pci_dev *pdev);
  445. void dpc_process_error(struct pci_dev *pdev);
  446. pci_ers_result_t dpc_reset_link(struct pci_dev *pdev);
  447. bool pci_dpc_recovered(struct pci_dev *pdev);
  448. #else
  449. static inline void pci_save_dpc_state(struct pci_dev *dev) { }
  450. static inline void pci_restore_dpc_state(struct pci_dev *dev) { }
  451. static inline void pci_dpc_init(struct pci_dev *pdev) { }
  452. static inline bool pci_dpc_recovered(struct pci_dev *pdev) { return false; }
  453. #endif
  454. #ifdef CONFIG_PCIEPORTBUS
  455. void pci_rcec_init(struct pci_dev *dev);
  456. void pci_rcec_exit(struct pci_dev *dev);
  457. void pcie_link_rcec(struct pci_dev *rcec);
  458. void pcie_walk_rcec(struct pci_dev *rcec,
  459. int (*cb)(struct pci_dev *, void *),
  460. void *userdata);
  461. #else
  462. static inline void pci_rcec_init(struct pci_dev *dev) { }
  463. static inline void pci_rcec_exit(struct pci_dev *dev) { }
  464. static inline void pcie_link_rcec(struct pci_dev *rcec) { }
  465. static inline void pcie_walk_rcec(struct pci_dev *rcec,
  466. int (*cb)(struct pci_dev *, void *),
  467. void *userdata) { }
  468. #endif
  469. #ifdef CONFIG_PCI_ATS
  470. /* Address Translation Service */
  471. void pci_ats_init(struct pci_dev *dev);
  472. void pci_restore_ats_state(struct pci_dev *dev);
  473. #else
  474. static inline void pci_ats_init(struct pci_dev *d) { }
  475. static inline void pci_restore_ats_state(struct pci_dev *dev) { }
  476. #endif /* CONFIG_PCI_ATS */
  477. #ifdef CONFIG_PCI_PRI
  478. void pci_pri_init(struct pci_dev *dev);
  479. void pci_restore_pri_state(struct pci_dev *pdev);
  480. #else
  481. static inline void pci_pri_init(struct pci_dev *dev) { }
  482. static inline void pci_restore_pri_state(struct pci_dev *pdev) { }
  483. #endif
  484. #ifdef CONFIG_PCI_PASID
  485. void pci_pasid_init(struct pci_dev *dev);
  486. void pci_restore_pasid_state(struct pci_dev *pdev);
  487. #else
  488. static inline void pci_pasid_init(struct pci_dev *dev) { }
  489. static inline void pci_restore_pasid_state(struct pci_dev *pdev) { }
  490. #endif
  491. #ifdef CONFIG_PCI_IOV
  492. int pci_iov_init(struct pci_dev *dev);
  493. void pci_iov_release(struct pci_dev *dev);
  494. void pci_iov_remove(struct pci_dev *dev);
  495. void pci_iov_update_resource(struct pci_dev *dev, int resno);
  496. resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno);
  497. void pci_restore_iov_state(struct pci_dev *dev);
  498. int pci_iov_bus_range(struct pci_bus *bus);
  499. extern const struct attribute_group sriov_pf_dev_attr_group;
  500. extern const struct attribute_group sriov_vf_dev_attr_group;
  501. #else
  502. static inline int pci_iov_init(struct pci_dev *dev)
  503. {
  504. return -ENODEV;
  505. }
  506. static inline void pci_iov_release(struct pci_dev *dev) { }
  507. static inline void pci_iov_remove(struct pci_dev *dev) { }
  508. static inline void pci_restore_iov_state(struct pci_dev *dev) { }
  509. static inline int pci_iov_bus_range(struct pci_bus *bus)
  510. {
  511. return 0;
  512. }
  513. #endif /* CONFIG_PCI_IOV */
  514. #ifdef CONFIG_PCIE_PTM
  515. void pci_ptm_init(struct pci_dev *dev);
  516. void pci_save_ptm_state(struct pci_dev *dev);
  517. void pci_restore_ptm_state(struct pci_dev *dev);
  518. void pci_suspend_ptm(struct pci_dev *dev);
  519. void pci_resume_ptm(struct pci_dev *dev);
  520. #else
  521. static inline void pci_ptm_init(struct pci_dev *dev) { }
  522. static inline void pci_save_ptm_state(struct pci_dev *dev) { }
  523. static inline void pci_restore_ptm_state(struct pci_dev *dev) { }
  524. static inline void pci_suspend_ptm(struct pci_dev *dev) { }
  525. static inline void pci_resume_ptm(struct pci_dev *dev) { }
  526. #endif
  527. unsigned long pci_cardbus_resource_alignment(struct resource *);
  528. static inline resource_size_t pci_resource_alignment(struct pci_dev *dev,
  529. struct resource *res)
  530. {
  531. #ifdef CONFIG_PCI_IOV
  532. int resno = res - dev->resource;
  533. if (resno >= PCI_IOV_RESOURCES && resno <= PCI_IOV_RESOURCE_END)
  534. return pci_sriov_resource_alignment(dev, resno);
  535. #endif
  536. if (dev->class >> 8 == PCI_CLASS_BRIDGE_CARDBUS)
  537. return pci_cardbus_resource_alignment(res);
  538. return resource_alignment(res);
  539. }
  540. void pci_acs_init(struct pci_dev *dev);
  541. #ifdef CONFIG_PCI_QUIRKS
  542. int pci_dev_specific_acs_enabled(struct pci_dev *dev, u16 acs_flags);
  543. int pci_dev_specific_enable_acs(struct pci_dev *dev);
  544. int pci_dev_specific_disable_acs_redir(struct pci_dev *dev);
  545. int pcie_failed_link_retrain(struct pci_dev *dev);
  546. #else
  547. static inline int pci_dev_specific_acs_enabled(struct pci_dev *dev,
  548. u16 acs_flags)
  549. {
  550. return -ENOTTY;
  551. }
  552. static inline int pci_dev_specific_enable_acs(struct pci_dev *dev)
  553. {
  554. return -ENOTTY;
  555. }
  556. static inline int pci_dev_specific_disable_acs_redir(struct pci_dev *dev)
  557. {
  558. return -ENOTTY;
  559. }
  560. static inline int pcie_failed_link_retrain(struct pci_dev *dev)
  561. {
  562. return -ENOTTY;
  563. }
  564. #endif
  565. /* PCI error reporting and recovery */
  566. pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
  567. pci_channel_state_t state,
  568. pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev));
  569. bool pcie_wait_for_link(struct pci_dev *pdev, bool active);
  570. int pcie_retrain_link(struct pci_dev *pdev, bool use_lt);
  571. /* ASPM-related functionality we need even without CONFIG_PCIEASPM */
  572. void pci_save_ltr_state(struct pci_dev *dev);
  573. void pci_restore_ltr_state(struct pci_dev *dev);
  574. void pci_configure_aspm_l1ss(struct pci_dev *dev);
  575. void pci_save_aspm_l1ss_state(struct pci_dev *dev);
  576. void pci_restore_aspm_l1ss_state(struct pci_dev *dev);
  577. #ifdef CONFIG_PCIEASPM
  578. void pcie_aspm_init_link_state(struct pci_dev *pdev);
  579. void pcie_aspm_exit_link_state(struct pci_dev *pdev);
  580. void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked);
  581. void pcie_aspm_powersave_config_link(struct pci_dev *pdev);
  582. void pci_configure_ltr(struct pci_dev *pdev);
  583. void pci_bridge_reconfigure_ltr(struct pci_dev *pdev);
  584. #else
  585. static inline void pcie_aspm_init_link_state(struct pci_dev *pdev) { }
  586. static inline void pcie_aspm_exit_link_state(struct pci_dev *pdev) { }
  587. static inline void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked) { }
  588. static inline void pcie_aspm_powersave_config_link(struct pci_dev *pdev) { }
  589. static inline void pci_configure_ltr(struct pci_dev *pdev) { }
  590. static inline void pci_bridge_reconfigure_ltr(struct pci_dev *pdev) { }
  591. #endif
  592. #ifdef CONFIG_PCIE_ECRC
  593. void pcie_set_ecrc_checking(struct pci_dev *dev);
  594. void pcie_ecrc_get_policy(char *str);
  595. #else
  596. static inline void pcie_set_ecrc_checking(struct pci_dev *dev) { }
  597. static inline void pcie_ecrc_get_policy(char *str) { }
  598. #endif
  599. struct pci_dev_reset_methods {
  600. u16 vendor;
  601. u16 device;
  602. int (*reset)(struct pci_dev *dev, bool probe);
  603. };
  604. struct pci_reset_fn_method {
  605. int (*reset_fn)(struct pci_dev *pdev, bool probe);
  606. char *name;
  607. };
  608. #ifdef CONFIG_PCI_QUIRKS
  609. int pci_dev_specific_reset(struct pci_dev *dev, bool probe);
  610. #else
  611. static inline int pci_dev_specific_reset(struct pci_dev *dev, bool probe)
  612. {
  613. return -ENOTTY;
  614. }
  615. #endif
  616. #if defined(CONFIG_PCI_QUIRKS) && defined(CONFIG_ARM64)
  617. int acpi_get_rc_resources(struct device *dev, const char *hid, u16 segment,
  618. struct resource *res);
  619. #else
  620. static inline int acpi_get_rc_resources(struct device *dev, const char *hid,
  621. u16 segment, struct resource *res)
  622. {
  623. return -ENODEV;
  624. }
  625. #endif
  626. int pci_rebar_get_current_size(struct pci_dev *pdev, int bar);
  627. int pci_rebar_set_size(struct pci_dev *pdev, int bar, int size);
  628. static inline u64 pci_rebar_size_to_bytes(int size)
  629. {
  630. return 1ULL << (size + 20);
  631. }
  632. struct device_node;
  633. #ifdef CONFIG_OF
  634. int of_pci_parse_bus_range(struct device_node *node, struct resource *res);
  635. int of_get_pci_domain_nr(struct device_node *node);
  636. int of_pci_get_max_link_speed(struct device_node *node);
  637. u32 of_pci_get_slot_power_limit(struct device_node *node,
  638. u8 *slot_power_limit_value,
  639. u8 *slot_power_limit_scale);
  640. bool of_pci_preserve_config(struct device_node *node);
  641. int pci_set_of_node(struct pci_dev *dev);
  642. void pci_release_of_node(struct pci_dev *dev);
  643. void pci_set_bus_of_node(struct pci_bus *bus);
  644. void pci_release_bus_of_node(struct pci_bus *bus);
  645. int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge);
  646. #else
  647. static inline int
  648. of_pci_parse_bus_range(struct device_node *node, struct resource *res)
  649. {
  650. return -EINVAL;
  651. }
  652. static inline int
  653. of_get_pci_domain_nr(struct device_node *node)
  654. {
  655. return -1;
  656. }
  657. static inline int
  658. of_pci_get_max_link_speed(struct device_node *node)
  659. {
  660. return -EINVAL;
  661. }
  662. static inline u32
  663. of_pci_get_slot_power_limit(struct device_node *node,
  664. u8 *slot_power_limit_value,
  665. u8 *slot_power_limit_scale)
  666. {
  667. if (slot_power_limit_value)
  668. *slot_power_limit_value = 0;
  669. if (slot_power_limit_scale)
  670. *slot_power_limit_scale = 0;
  671. return 0;
  672. }
  673. static inline bool of_pci_preserve_config(struct device_node *node)
  674. {
  675. return false;
  676. }
  677. static inline int pci_set_of_node(struct pci_dev *dev) { return 0; }
  678. static inline void pci_release_of_node(struct pci_dev *dev) { }
  679. static inline void pci_set_bus_of_node(struct pci_bus *bus) { }
  680. static inline void pci_release_bus_of_node(struct pci_bus *bus) { }
  681. static inline int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge)
  682. {
  683. return 0;
  684. }
  685. #endif /* CONFIG_OF */
  686. struct of_changeset;
  687. #ifdef CONFIG_PCI_DYNAMIC_OF_NODES
  688. void of_pci_make_dev_node(struct pci_dev *pdev);
  689. void of_pci_remove_node(struct pci_dev *pdev);
  690. int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs,
  691. struct device_node *np);
  692. #else
  693. static inline void of_pci_make_dev_node(struct pci_dev *pdev) { }
  694. static inline void of_pci_remove_node(struct pci_dev *pdev) { }
  695. #endif
  696. #ifdef CONFIG_PCIEAER
  697. void pci_no_aer(void);
  698. void pci_aer_init(struct pci_dev *dev);
  699. void pci_aer_exit(struct pci_dev *dev);
  700. extern const struct attribute_group aer_stats_attr_group;
  701. void pci_aer_clear_fatal_status(struct pci_dev *dev);
  702. int pci_aer_clear_status(struct pci_dev *dev);
  703. int pci_aer_raw_clear_status(struct pci_dev *dev);
  704. void pci_save_aer_state(struct pci_dev *dev);
  705. void pci_restore_aer_state(struct pci_dev *dev);
  706. #else
  707. static inline void pci_no_aer(void) { }
  708. static inline void pci_aer_init(struct pci_dev *d) { }
  709. static inline void pci_aer_exit(struct pci_dev *d) { }
  710. static inline void pci_aer_clear_fatal_status(struct pci_dev *dev) { }
  711. static inline int pci_aer_clear_status(struct pci_dev *dev) { return -EINVAL; }
  712. static inline int pci_aer_raw_clear_status(struct pci_dev *dev) { return -EINVAL; }
  713. static inline void pci_save_aer_state(struct pci_dev *dev) { }
  714. static inline void pci_restore_aer_state(struct pci_dev *dev) { }
  715. #endif
  716. #ifdef CONFIG_ACPI
  717. bool pci_acpi_preserve_config(struct pci_host_bridge *bridge);
  718. int pci_acpi_program_hp_params(struct pci_dev *dev);
  719. extern const struct attribute_group pci_dev_acpi_attr_group;
  720. void pci_set_acpi_fwnode(struct pci_dev *dev);
  721. int pci_dev_acpi_reset(struct pci_dev *dev, bool probe);
  722. bool acpi_pci_power_manageable(struct pci_dev *dev);
  723. bool acpi_pci_bridge_d3(struct pci_dev *dev);
  724. int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state);
  725. pci_power_t acpi_pci_get_power_state(struct pci_dev *dev);
  726. void acpi_pci_refresh_power_state(struct pci_dev *dev);
  727. int acpi_pci_wakeup(struct pci_dev *dev, bool enable);
  728. bool acpi_pci_need_resume(struct pci_dev *dev);
  729. pci_power_t acpi_pci_choose_state(struct pci_dev *pdev);
  730. #else
  731. static inline bool pci_acpi_preserve_config(struct pci_host_bridge *bridge)
  732. {
  733. return false;
  734. }
  735. static inline int pci_dev_acpi_reset(struct pci_dev *dev, bool probe)
  736. {
  737. return -ENOTTY;
  738. }
  739. static inline void pci_set_acpi_fwnode(struct pci_dev *dev) { }
  740. static inline int pci_acpi_program_hp_params(struct pci_dev *dev)
  741. {
  742. return -ENODEV;
  743. }
  744. static inline bool acpi_pci_power_manageable(struct pci_dev *dev)
  745. {
  746. return false;
  747. }
  748. static inline bool acpi_pci_bridge_d3(struct pci_dev *dev)
  749. {
  750. return false;
  751. }
  752. static inline int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  753. {
  754. return -ENODEV;
  755. }
  756. static inline pci_power_t acpi_pci_get_power_state(struct pci_dev *dev)
  757. {
  758. return PCI_UNKNOWN;
  759. }
  760. static inline void acpi_pci_refresh_power_state(struct pci_dev *dev) { }
  761. static inline int acpi_pci_wakeup(struct pci_dev *dev, bool enable)
  762. {
  763. return -ENODEV;
  764. }
  765. static inline bool acpi_pci_need_resume(struct pci_dev *dev)
  766. {
  767. return false;
  768. }
  769. static inline pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
  770. {
  771. return PCI_POWER_ERROR;
  772. }
  773. #endif
  774. #ifdef CONFIG_PCIEASPM
  775. extern const struct attribute_group aspm_ctrl_attr_group;
  776. #endif
  777. extern const struct attribute_group pci_dev_reset_method_attr_group;
  778. #ifdef CONFIG_X86_INTEL_MID
  779. bool pci_use_mid_pm(void);
  780. int mid_pci_set_power_state(struct pci_dev *pdev, pci_power_t state);
  781. pci_power_t mid_pci_get_power_state(struct pci_dev *pdev);
  782. #else
  783. static inline bool pci_use_mid_pm(void)
  784. {
  785. return false;
  786. }
  787. static inline int mid_pci_set_power_state(struct pci_dev *pdev, pci_power_t state)
  788. {
  789. return -ENODEV;
  790. }
  791. static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev)
  792. {
  793. return PCI_UNKNOWN;
  794. }
  795. #endif
  796. int pcim_intx(struct pci_dev *dev, int enable);
  797. int pcim_request_region_exclusive(struct pci_dev *pdev, int bar,
  798. const char *name);
  799. void pcim_release_region(struct pci_dev *pdev, int bar);
  800. /*
  801. * Config Address for PCI Configuration Mechanism #1
  802. *
  803. * See PCI Local Bus Specification, Revision 3.0,
  804. * Section 3.2.2.3.2, Figure 3-2, p. 50.
  805. */
  806. #define PCI_CONF1_BUS_SHIFT 16 /* Bus number */
  807. #define PCI_CONF1_DEV_SHIFT 11 /* Device number */
  808. #define PCI_CONF1_FUNC_SHIFT 8 /* Function number */
  809. #define PCI_CONF1_BUS_MASK 0xff
  810. #define PCI_CONF1_DEV_MASK 0x1f
  811. #define PCI_CONF1_FUNC_MASK 0x7
  812. #define PCI_CONF1_REG_MASK 0xfc /* Limit aligned offset to a maximum of 256B */
  813. #define PCI_CONF1_ENABLE BIT(31)
  814. #define PCI_CONF1_BUS(x) (((x) & PCI_CONF1_BUS_MASK) << PCI_CONF1_BUS_SHIFT)
  815. #define PCI_CONF1_DEV(x) (((x) & PCI_CONF1_DEV_MASK) << PCI_CONF1_DEV_SHIFT)
  816. #define PCI_CONF1_FUNC(x) (((x) & PCI_CONF1_FUNC_MASK) << PCI_CONF1_FUNC_SHIFT)
  817. #define PCI_CONF1_REG(x) ((x) & PCI_CONF1_REG_MASK)
  818. #define PCI_CONF1_ADDRESS(bus, dev, func, reg) \
  819. (PCI_CONF1_ENABLE | \
  820. PCI_CONF1_BUS(bus) | \
  821. PCI_CONF1_DEV(dev) | \
  822. PCI_CONF1_FUNC(func) | \
  823. PCI_CONF1_REG(reg))
  824. /*
  825. * Extension of PCI Config Address for accessing extended PCIe registers
  826. *
  827. * No standardized specification, but used on lot of non-ECAM-compliant ARM SoCs
  828. * or on AMD Barcelona and new CPUs. Reserved bits [27:24] of PCI Config Address
  829. * are used for specifying additional 4 high bits of PCI Express register.
  830. */
  831. #define PCI_CONF1_EXT_REG_SHIFT 16
  832. #define PCI_CONF1_EXT_REG_MASK 0xf00
  833. #define PCI_CONF1_EXT_REG(x) (((x) & PCI_CONF1_EXT_REG_MASK) << PCI_CONF1_EXT_REG_SHIFT)
  834. #define PCI_CONF1_EXT_ADDRESS(bus, dev, func, reg) \
  835. (PCI_CONF1_ADDRESS(bus, dev, func, reg) | \
  836. PCI_CONF1_EXT_REG(reg))
  837. #endif /* DRIVERS_PCI_H */