pci.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef DRIVERS_PCI_H
  3. #define DRIVERS_PCI_H
  4. #include <linux/align.h>
  5. #include <linux/bitfield.h>
  6. #include <linux/pci.h>
  7. #include <trace/events/pci.h>
  8. struct pcie_tlp_log;
  9. /* Number of possible devfns: 0.0 to 1f.7 inclusive */
  10. #define MAX_NR_DEVFNS 256
  11. #define PCI_MAX_NR_DEVS 32
  12. #define MAX_NR_LANES 16
  13. #define PCI_FIND_CAP_TTL 48
  14. #define PCI_VSEC_ID_INTEL_TBT 0x1234 /* Thunderbolt */
  15. #define PCIE_LINK_RETRAIN_TIMEOUT_MS 1000
  16. /*
  17. * Power stable to PERST# inactive.
  18. *
  19. * See the "Power Sequencing and Reset Signal Timings" table of the PCI Express
  20. * Card Electromechanical Specification, Revision 5.1, Section 2.9.2, Symbol
  21. * "T_PVPERL".
  22. */
  23. #define PCIE_T_PVPERL_MS 100
  24. /*
  25. * REFCLK stable before PERST# inactive.
  26. *
  27. * See the "Power Sequencing and Reset Signal Timings" table of the PCI Express
  28. * Card Electromechanical Specification, Revision 5.1, Section 2.9.2, Symbol
  29. * "T_PERST-CLK".
  30. */
  31. #define PCIE_T_PERST_CLK_US 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. /* Parameters for the waiting for link up routine */
  52. #define PCIE_LINK_WAIT_MAX_RETRIES 10
  53. #define PCIE_LINK_WAIT_SLEEP_MS 90
  54. /* Format of TLP; PCIe r7.0, sec 2.2.1 */
  55. #define PCIE_TLP_FMT_3DW_NO_DATA 0x00 /* 3DW header, no data */
  56. #define PCIE_TLP_FMT_4DW_NO_DATA 0x01 /* 4DW header, no data */
  57. #define PCIE_TLP_FMT_3DW_DATA 0x02 /* 3DW header, with data */
  58. #define PCIE_TLP_FMT_4DW_DATA 0x03 /* 4DW header, with data */
  59. /* Type of TLP; PCIe r7.0, sec 2.2.1 */
  60. #define PCIE_TLP_TYPE_CFG0_RD 0x04 /* Config Type 0 Read Request */
  61. #define PCIE_TLP_TYPE_CFG0_WR 0x04 /* Config Type 0 Write Request */
  62. #define PCIE_TLP_TYPE_CFG1_RD 0x05 /* Config Type 1 Read Request */
  63. #define PCIE_TLP_TYPE_CFG1_WR 0x05 /* Config Type 1 Write Request */
  64. /* Message Routing (r[2:0]); PCIe r6.0, sec 2.2.8 */
  65. #define PCIE_MSG_TYPE_R_RC 0
  66. #define PCIE_MSG_TYPE_R_ADDR 1
  67. #define PCIE_MSG_TYPE_R_ID 2
  68. #define PCIE_MSG_TYPE_R_BC 3
  69. #define PCIE_MSG_TYPE_R_LOCAL 4
  70. #define PCIE_MSG_TYPE_R_GATHER 5
  71. /* Power Management Messages; PCIe r6.0, sec 2.2.8.2 */
  72. #define PCIE_MSG_CODE_PME_TURN_OFF 0x19
  73. /* INTx Mechanism Messages; PCIe r6.0, sec 2.2.8.1 */
  74. #define PCIE_MSG_CODE_ASSERT_INTA 0x20
  75. #define PCIE_MSG_CODE_ASSERT_INTB 0x21
  76. #define PCIE_MSG_CODE_ASSERT_INTC 0x22
  77. #define PCIE_MSG_CODE_ASSERT_INTD 0x23
  78. #define PCIE_MSG_CODE_DEASSERT_INTA 0x24
  79. #define PCIE_MSG_CODE_DEASSERT_INTB 0x25
  80. #define PCIE_MSG_CODE_DEASSERT_INTC 0x26
  81. #define PCIE_MSG_CODE_DEASSERT_INTD 0x27
  82. /* Cpl. status of Complete; PCIe r7.0, sec 2.2.9.1 */
  83. #define PCIE_CPL_STS_SUCCESS 0x00 /* Successful Completion */
  84. #define PCI_BUS_BRIDGE_IO_WINDOW 0
  85. #define PCI_BUS_BRIDGE_MEM_WINDOW 1
  86. #define PCI_BUS_BRIDGE_PREF_MEM_WINDOW 2
  87. #define PCI_EXP_AER_FLAGS (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
  88. PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
  89. extern const unsigned char pcie_link_speed[];
  90. extern bool pci_early_dump;
  91. extern struct mutex pci_rescan_remove_lock;
  92. bool pcie_cap_has_lnkctl(const struct pci_dev *dev);
  93. bool pcie_cap_has_lnkctl2(const struct pci_dev *dev);
  94. bool pcie_cap_has_rtctl(const struct pci_dev *dev);
  95. /* Standard Capability finder */
  96. /**
  97. * PCI_FIND_NEXT_CAP - Find a PCI standard capability
  98. * @read_cfg: Function pointer for reading PCI config space
  99. * @start: Starting position to begin search
  100. * @cap: Capability ID to find
  101. * @prev_ptr: Pointer to store position of preceding capability (optional)
  102. * @args: Arguments to pass to read_cfg function
  103. *
  104. * Search the capability list in PCI config space to find @cap. If
  105. * found, update *prev_ptr with the position of the preceding capability
  106. * (if prev_ptr != NULL)
  107. * Implements TTL (time-to-live) protection against infinite loops.
  108. *
  109. * Return: Position of the capability if found, 0 otherwise.
  110. */
  111. #define PCI_FIND_NEXT_CAP(read_cfg, start, cap, prev_ptr, args...) \
  112. ({ \
  113. int __ttl = PCI_FIND_CAP_TTL; \
  114. u8 __id, __found_pos = 0; \
  115. u8 __prev_pos = (start); \
  116. u8 __pos = (start); \
  117. u16 __ent; \
  118. \
  119. read_cfg##_byte(args, __pos, &__pos); \
  120. \
  121. while (__ttl--) { \
  122. if (__pos < PCI_STD_HEADER_SIZEOF) \
  123. break; \
  124. \
  125. __pos = ALIGN_DOWN(__pos, 4); \
  126. read_cfg##_word(args, __pos, &__ent); \
  127. \
  128. __id = FIELD_GET(PCI_CAP_ID_MASK, __ent); \
  129. if (__id == 0xff) \
  130. break; \
  131. \
  132. if (__id == (cap)) { \
  133. __found_pos = __pos; \
  134. if (prev_ptr != NULL) \
  135. *(u8 *)prev_ptr = __prev_pos; \
  136. break; \
  137. } \
  138. \
  139. __prev_pos = __pos; \
  140. __pos = FIELD_GET(PCI_CAP_LIST_NEXT_MASK, __ent); \
  141. } \
  142. __found_pos; \
  143. })
  144. /* Extended Capability finder */
  145. /**
  146. * PCI_FIND_NEXT_EXT_CAP - Find a PCI extended capability
  147. * @read_cfg: Function pointer for reading PCI config space
  148. * @start: Starting position to begin search (0 for initial search)
  149. * @cap: Extended capability ID to find
  150. * @prev_ptr: Pointer to store position of preceding capability (optional)
  151. * @args: Arguments to pass to read_cfg function
  152. *
  153. * Search the extended capability list in PCI config space to find @cap.
  154. * If found, update *prev_ptr with the position of the preceding capability
  155. * (if prev_ptr != NULL)
  156. * Implements TTL protection against infinite loops using a calculated
  157. * maximum search count.
  158. *
  159. * Return: Position of the capability if found, 0 otherwise.
  160. */
  161. #define PCI_FIND_NEXT_EXT_CAP(read_cfg, start, cap, prev_ptr, args...) \
  162. ({ \
  163. u16 __pos = (start) ?: PCI_CFG_SPACE_SIZE; \
  164. u16 __found_pos = 0; \
  165. u16 __prev_pos; \
  166. int __ttl, __ret; \
  167. u32 __header; \
  168. \
  169. __prev_pos = __pos; \
  170. __ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8; \
  171. while (__ttl-- > 0 && __pos >= PCI_CFG_SPACE_SIZE) { \
  172. __ret = read_cfg##_dword(args, __pos, &__header); \
  173. if (__ret != PCIBIOS_SUCCESSFUL) \
  174. break; \
  175. \
  176. if (__header == 0) \
  177. break; \
  178. \
  179. if (PCI_EXT_CAP_ID(__header) == (cap) && __pos != start) {\
  180. __found_pos = __pos; \
  181. if (prev_ptr != NULL) \
  182. *(u16 *)prev_ptr = __prev_pos; \
  183. break; \
  184. } \
  185. \
  186. __prev_pos = __pos; \
  187. __pos = PCI_EXT_CAP_NEXT(__header); \
  188. } \
  189. __found_pos; \
  190. })
  191. /* Functions internal to the PCI core code */
  192. #ifdef CONFIG_DMI
  193. extern const struct attribute_group pci_dev_smbios_attr_group;
  194. #endif
  195. enum pci_mmap_api {
  196. PCI_MMAP_SYSFS, /* mmap on /sys/bus/pci/devices/<BDF>/resource<N> */
  197. PCI_MMAP_PROCFS /* mmap on /proc/bus/pci/<BDF> */
  198. };
  199. int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vmai,
  200. enum pci_mmap_api mmap_api);
  201. bool pci_reset_supported(struct pci_dev *dev);
  202. void pci_init_reset_methods(struct pci_dev *dev);
  203. int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
  204. int pci_bus_error_reset(struct pci_dev *dev);
  205. int __pci_reset_bus(struct pci_bus *bus);
  206. struct pci_cap_saved_data {
  207. u16 cap_nr;
  208. bool cap_extended;
  209. unsigned int size;
  210. u32 data[];
  211. };
  212. struct pci_cap_saved_state {
  213. struct hlist_node next;
  214. struct pci_cap_saved_data cap;
  215. };
  216. void pci_allocate_cap_save_buffers(struct pci_dev *dev);
  217. void pci_free_cap_save_buffers(struct pci_dev *dev);
  218. int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size);
  219. int pci_add_ext_cap_save_buffer(struct pci_dev *dev,
  220. u16 cap, unsigned int size);
  221. struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap);
  222. struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev,
  223. u16 cap);
  224. #define PCI_PM_D2_DELAY 200 /* usec; see PCIe r4.0, sec 5.9.1 */
  225. #define PCI_PM_D3HOT_WAIT 10 /* msec */
  226. #define PCI_PM_D3COLD_WAIT 100 /* msec */
  227. void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
  228. void pci_refresh_power_state(struct pci_dev *dev);
  229. int pci_power_up(struct pci_dev *dev);
  230. void pci_disable_enabled_device(struct pci_dev *dev);
  231. int pci_finish_runtime_suspend(struct pci_dev *dev);
  232. void pcie_clear_device_status(struct pci_dev *dev);
  233. void pcie_clear_root_pme_status(struct pci_dev *dev);
  234. bool pci_check_pme_status(struct pci_dev *dev);
  235. void pci_pme_wakeup_bus(struct pci_bus *bus);
  236. void pci_pme_restore(struct pci_dev *dev);
  237. bool pci_dev_need_resume(struct pci_dev *dev);
  238. void pci_dev_adjust_pme(struct pci_dev *dev);
  239. void pci_dev_complete_resume(struct pci_dev *pci_dev);
  240. void pci_config_pm_runtime_get(struct pci_dev *dev);
  241. void pci_config_pm_runtime_put(struct pci_dev *dev);
  242. void pci_pm_power_up_and_verify_state(struct pci_dev *pci_dev);
  243. void pci_pm_init(struct pci_dev *dev);
  244. void pci_ea_init(struct pci_dev *dev);
  245. bool pci_ea_fixed_busnrs(struct pci_dev *dev, u8 *sec, u8 *sub);
  246. void pci_msi_init(struct pci_dev *dev);
  247. void pci_msix_init(struct pci_dev *dev);
  248. bool pci_bridge_d3_possible(struct pci_dev *dev);
  249. void pci_bridge_d3_update(struct pci_dev *dev);
  250. int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type);
  251. static inline bool pci_bus_rrs_vendor_id(u32 l)
  252. {
  253. return (l & 0xffff) == PCI_VENDOR_ID_PCI_SIG;
  254. }
  255. static inline void pci_wakeup_event(struct pci_dev *dev)
  256. {
  257. /* Wait 100 ms before the system can be put into a sleep state. */
  258. pm_wakeup_event(&dev->dev, 100);
  259. }
  260. /**
  261. * pci_bar_index_is_valid - Check whether a BAR index is within valid range
  262. * @bar: BAR index
  263. *
  264. * Protects against overflowing &struct pci_dev.resource array.
  265. *
  266. * Return: true for valid index, false otherwise.
  267. */
  268. static inline bool pci_bar_index_is_valid(int bar)
  269. {
  270. if (bar >= 0 && bar < PCI_NUM_RESOURCES)
  271. return true;
  272. return false;
  273. }
  274. static inline bool pci_has_subordinate(struct pci_dev *pci_dev)
  275. {
  276. return !!(pci_dev->subordinate);
  277. }
  278. static inline bool pci_power_manageable(struct pci_dev *pci_dev)
  279. {
  280. /*
  281. * Currently we allow normal PCI devices and PCI bridges transition
  282. * into D3 if their bridge_d3 is set.
  283. */
  284. return !pci_has_subordinate(pci_dev) || pci_dev->bridge_d3;
  285. }
  286. static inline bool pcie_downstream_port(const struct pci_dev *dev)
  287. {
  288. int type = pci_pcie_type(dev);
  289. return type == PCI_EXP_TYPE_ROOT_PORT ||
  290. type == PCI_EXP_TYPE_DOWNSTREAM ||
  291. type == PCI_EXP_TYPE_PCIE_BRIDGE;
  292. }
  293. void pci_vpd_init(struct pci_dev *dev);
  294. extern const struct attribute_group pci_dev_vpd_attr_group;
  295. /* PCI Virtual Channel */
  296. int pci_save_vc_state(struct pci_dev *dev);
  297. void pci_restore_vc_state(struct pci_dev *dev);
  298. void pci_allocate_vc_save_buffers(struct pci_dev *dev);
  299. /* PCI /proc functions */
  300. #ifdef CONFIG_PROC_FS
  301. int pci_proc_attach_device(struct pci_dev *dev);
  302. int pci_proc_detach_device(struct pci_dev *dev);
  303. int pci_proc_detach_bus(struct pci_bus *bus);
  304. #else
  305. static inline int pci_proc_attach_device(struct pci_dev *dev) { return 0; }
  306. static inline int pci_proc_detach_device(struct pci_dev *dev) { return 0; }
  307. static inline int pci_proc_detach_bus(struct pci_bus *bus) { return 0; }
  308. #endif
  309. /* Functions for PCI Hotplug drivers to use */
  310. int pci_hp_add_bridge(struct pci_dev *dev);
  311. bool pci_hp_spurious_link_change(struct pci_dev *pdev);
  312. #if defined(CONFIG_SYSFS) && defined(HAVE_PCI_LEGACY)
  313. void pci_create_legacy_files(struct pci_bus *bus);
  314. void pci_remove_legacy_files(struct pci_bus *bus);
  315. #else
  316. static inline void pci_create_legacy_files(struct pci_bus *bus) { }
  317. static inline void pci_remove_legacy_files(struct pci_bus *bus) { }
  318. #endif
  319. /* Lock for read/write access to pci device and bus lists */
  320. extern struct rw_semaphore pci_bus_sem;
  321. extern struct mutex pci_slot_mutex;
  322. extern raw_spinlock_t pci_lock;
  323. extern unsigned int pci_pm_d3hot_delay;
  324. #ifdef CONFIG_PCI_MSI
  325. void pci_no_msi(void);
  326. #else
  327. static inline void pci_no_msi(void) { }
  328. #endif
  329. void pci_realloc_get_opt(char *);
  330. static inline int pci_no_d1d2(struct pci_dev *dev)
  331. {
  332. unsigned int parent_dstates = 0;
  333. if (dev->bus->self)
  334. parent_dstates = dev->bus->self->no_d1d2;
  335. return (dev->no_d1d2 || parent_dstates);
  336. }
  337. #ifdef CONFIG_SYSFS
  338. int pci_create_sysfs_dev_files(struct pci_dev *pdev);
  339. void pci_remove_sysfs_dev_files(struct pci_dev *pdev);
  340. extern const struct attribute_group *pci_dev_groups[];
  341. extern const struct attribute_group *pci_dev_attr_groups[];
  342. extern const struct attribute_group *pcibus_groups[];
  343. extern const struct attribute_group *pci_bus_groups[];
  344. extern const struct attribute_group pci_doe_sysfs_group;
  345. #else
  346. static inline int pci_create_sysfs_dev_files(struct pci_dev *pdev) { return 0; }
  347. static inline void pci_remove_sysfs_dev_files(struct pci_dev *pdev) { }
  348. #define pci_dev_groups NULL
  349. #define pci_dev_attr_groups NULL
  350. #define pcibus_groups NULL
  351. #define pci_bus_groups NULL
  352. #endif
  353. extern unsigned long pci_hotplug_io_size;
  354. extern unsigned long pci_hotplug_mmio_size;
  355. extern unsigned long pci_hotplug_mmio_pref_size;
  356. extern unsigned long pci_hotplug_bus_size;
  357. static inline bool pci_is_cardbus_bridge(struct pci_dev *dev)
  358. {
  359. return dev->hdr_type == PCI_HEADER_TYPE_CARDBUS;
  360. }
  361. #ifdef CONFIG_CARDBUS
  362. unsigned long pci_cardbus_resource_alignment(struct resource *res);
  363. int pci_bus_size_cardbus_bridge(struct pci_bus *bus,
  364. struct list_head *realloc_head);
  365. int pci_cardbus_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
  366. u32 buses, int max,
  367. unsigned int available_buses, int pass);
  368. int pci_setup_cardbus(char *str);
  369. #else
  370. static inline unsigned long pci_cardbus_resource_alignment(struct resource *res)
  371. {
  372. return 0;
  373. }
  374. static inline int pci_bus_size_cardbus_bridge(struct pci_bus *bus,
  375. struct list_head *realloc_head)
  376. {
  377. return -EOPNOTSUPP;
  378. }
  379. static inline int pci_cardbus_scan_bridge_extend(struct pci_bus *bus,
  380. struct pci_dev *dev,
  381. u32 buses, int max,
  382. unsigned int available_buses,
  383. int pass)
  384. {
  385. return max;
  386. }
  387. static inline int pci_setup_cardbus(char *str) { return -ENOENT; }
  388. #endif /* CONFIG_CARDBUS */
  389. /**
  390. * pci_match_one_device - Tell if a PCI device structure has a matching
  391. * PCI device id structure
  392. * @id: single PCI device id structure to match
  393. * @dev: the PCI device structure to match against
  394. *
  395. * Returns the matching pci_device_id structure or %NULL if there is no match.
  396. */
  397. static inline const struct pci_device_id *
  398. pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
  399. {
  400. if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) &&
  401. (id->device == PCI_ANY_ID || id->device == dev->device) &&
  402. (id->subvendor == PCI_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
  403. (id->subdevice == PCI_ANY_ID || id->subdevice == dev->subsystem_device) &&
  404. !((id->class ^ dev->class) & id->class_mask))
  405. return id;
  406. return NULL;
  407. }
  408. /* PCI slot sysfs helper code */
  409. #define to_pci_slot(s) container_of(s, struct pci_slot, kobj)
  410. extern struct kset *pci_slots_kset;
  411. struct pci_slot_attribute {
  412. struct attribute attr;
  413. ssize_t (*show)(struct pci_slot *, char *);
  414. ssize_t (*store)(struct pci_slot *, const char *, size_t);
  415. };
  416. #define to_pci_slot_attr(s) container_of(s, struct pci_slot_attribute, attr)
  417. enum pci_bar_type {
  418. pci_bar_unknown, /* Standard PCI BAR probe */
  419. pci_bar_io, /* An I/O port BAR */
  420. pci_bar_mem32, /* A 32-bit memory BAR */
  421. pci_bar_mem64, /* A 64-bit memory BAR */
  422. };
  423. struct device *pci_get_host_bridge_device(struct pci_dev *dev);
  424. void pci_put_host_bridge_device(struct device *dev);
  425. void pci_resize_resource_set_size(struct pci_dev *dev, int resno, int size);
  426. int pci_do_resource_release_and_resize(struct pci_dev *dev, int resno, int size,
  427. int exclude_bars);
  428. unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge);
  429. int __must_check pci_reassign_resource(struct pci_dev *dev, int i, resource_size_t add_size, resource_size_t align);
  430. int pci_configure_extended_tags(struct pci_dev *dev, void *ign);
  431. bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *pl,
  432. int rrs_timeout);
  433. bool pci_bus_generic_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *pl,
  434. int rrs_timeout);
  435. int pci_setup_device(struct pci_dev *dev);
  436. void __pci_size_stdbars(struct pci_dev *dev, int count,
  437. unsigned int pos, u32 *sizes);
  438. int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
  439. struct resource *res, unsigned int reg, u32 *sizes);
  440. void pci_configure_ari(struct pci_dev *dev);
  441. int pci_dev_res_add_to_list(struct list_head *head, struct pci_dev *dev,
  442. struct resource *res, resource_size_t add_size,
  443. resource_size_t min_align);
  444. void __pci_bus_size_bridges(struct pci_bus *bus,
  445. struct list_head *realloc_head);
  446. void __pci_bus_assign_resources(const struct pci_bus *bus,
  447. struct list_head *realloc_head,
  448. struct list_head *fail_head);
  449. bool pci_bus_clip_resource(struct pci_dev *dev, int idx);
  450. void pci_walk_bus_locked(struct pci_bus *top,
  451. int (*cb)(struct pci_dev *, void *),
  452. void *userdata);
  453. const char *pci_resource_name(struct pci_dev *dev, unsigned int i);
  454. bool pci_resource_is_optional(const struct pci_dev *dev, int resno);
  455. static inline bool pci_resource_is_bridge_win(int resno)
  456. {
  457. return resno >= PCI_BRIDGE_RESOURCES &&
  458. resno <= PCI_BRIDGE_RESOURCE_END;
  459. }
  460. /**
  461. * pci_resource_num - Reverse lookup resource number from device resources
  462. * @dev: PCI device
  463. * @res: Resource to lookup index for (MUST be a @dev's resource)
  464. *
  465. * Perform reverse lookup to determine the resource number for @res within
  466. * @dev resource array. NOTE: The caller is responsible for ensuring @res is
  467. * among @dev's resources!
  468. *
  469. * Returns: resource number.
  470. */
  471. static inline int pci_resource_num(const struct pci_dev *dev,
  472. const struct resource *res)
  473. {
  474. int resno = res - &dev->resource[0];
  475. /* Passing a resource that is not among dev's resources? */
  476. WARN_ON_ONCE(resno >= PCI_NUM_RESOURCES);
  477. return resno;
  478. }
  479. void pbus_validate_busn(struct pci_bus *bus);
  480. struct resource *pbus_select_window(struct pci_bus *bus,
  481. const struct resource *res);
  482. void pci_reassigndev_resource_alignment(struct pci_dev *dev);
  483. void pci_disable_bridge_window(struct pci_dev *dev);
  484. struct pci_bus *pci_bus_get(struct pci_bus *bus);
  485. void pci_bus_put(struct pci_bus *bus);
  486. #define PCIE_LNKCAP_SLS2SPEED(lnkcap) \
  487. ({ \
  488. u32 lnkcap_sls = (lnkcap) & PCI_EXP_LNKCAP_SLS; \
  489. \
  490. (lnkcap_sls == PCI_EXP_LNKCAP_SLS_64_0GB ? PCIE_SPEED_64_0GT : \
  491. lnkcap_sls == PCI_EXP_LNKCAP_SLS_32_0GB ? PCIE_SPEED_32_0GT : \
  492. lnkcap_sls == PCI_EXP_LNKCAP_SLS_16_0GB ? PCIE_SPEED_16_0GT : \
  493. lnkcap_sls == PCI_EXP_LNKCAP_SLS_8_0GB ? PCIE_SPEED_8_0GT : \
  494. lnkcap_sls == PCI_EXP_LNKCAP_SLS_5_0GB ? PCIE_SPEED_5_0GT : \
  495. lnkcap_sls == PCI_EXP_LNKCAP_SLS_2_5GB ? PCIE_SPEED_2_5GT : \
  496. PCI_SPEED_UNKNOWN); \
  497. })
  498. /* PCIe link information from Link Capabilities 2 */
  499. #define PCIE_LNKCAP2_SLS2SPEED(lnkcap2) \
  500. ((lnkcap2) & PCI_EXP_LNKCAP2_SLS_64_0GB ? PCIE_SPEED_64_0GT : \
  501. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_32_0GB ? PCIE_SPEED_32_0GT : \
  502. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_16_0GB ? PCIE_SPEED_16_0GT : \
  503. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_8_0GB ? PCIE_SPEED_8_0GT : \
  504. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_5_0GB ? PCIE_SPEED_5_0GT : \
  505. (lnkcap2) & PCI_EXP_LNKCAP2_SLS_2_5GB ? PCIE_SPEED_2_5GT : \
  506. PCI_SPEED_UNKNOWN)
  507. #define PCIE_LNKCTL2_TLS2SPEED(lnkctl2) \
  508. ({ \
  509. u16 lnkctl2_tls = (lnkctl2) & PCI_EXP_LNKCTL2_TLS; \
  510. \
  511. (lnkctl2_tls == PCI_EXP_LNKCTL2_TLS_64_0GT ? PCIE_SPEED_64_0GT : \
  512. lnkctl2_tls == PCI_EXP_LNKCTL2_TLS_32_0GT ? PCIE_SPEED_32_0GT : \
  513. lnkctl2_tls == PCI_EXP_LNKCTL2_TLS_16_0GT ? PCIE_SPEED_16_0GT : \
  514. lnkctl2_tls == PCI_EXP_LNKCTL2_TLS_8_0GT ? PCIE_SPEED_8_0GT : \
  515. lnkctl2_tls == PCI_EXP_LNKCTL2_TLS_5_0GT ? PCIE_SPEED_5_0GT : \
  516. lnkctl2_tls == PCI_EXP_LNKCTL2_TLS_2_5GT ? PCIE_SPEED_2_5GT : \
  517. PCI_SPEED_UNKNOWN); \
  518. })
  519. /* PCIe speed to Mb/s reduced by encoding overhead */
  520. #define PCIE_SPEED2MBS_ENC(speed) \
  521. ((speed) == PCIE_SPEED_64_0GT ? 64000*1/1 : \
  522. (speed) == PCIE_SPEED_32_0GT ? 32000*128/130 : \
  523. (speed) == PCIE_SPEED_16_0GT ? 16000*128/130 : \
  524. (speed) == PCIE_SPEED_8_0GT ? 8000*128/130 : \
  525. (speed) == PCIE_SPEED_5_0GT ? 5000*8/10 : \
  526. (speed) == PCIE_SPEED_2_5GT ? 2500*8/10 : \
  527. 0)
  528. static inline int pcie_dev_speed_mbps(enum pci_bus_speed speed)
  529. {
  530. switch (speed) {
  531. case PCIE_SPEED_2_5GT:
  532. return 2500;
  533. case PCIE_SPEED_5_0GT:
  534. return 5000;
  535. case PCIE_SPEED_8_0GT:
  536. return 8000;
  537. case PCIE_SPEED_16_0GT:
  538. return 16000;
  539. case PCIE_SPEED_32_0GT:
  540. return 32000;
  541. case PCIE_SPEED_64_0GT:
  542. return 64000;
  543. default:
  544. break;
  545. }
  546. return -EINVAL;
  547. }
  548. u8 pcie_get_supported_speeds(struct pci_dev *dev);
  549. const char *pci_speed_string(enum pci_bus_speed speed);
  550. void __pcie_print_link_status(struct pci_dev *dev, bool verbose);
  551. void pcie_report_downtraining(struct pci_dev *dev);
  552. enum pcie_link_change_reason {
  553. PCIE_LINK_RETRAIN,
  554. PCIE_ADD_BUS,
  555. PCIE_BWCTRL_ENABLE,
  556. PCIE_BWCTRL_IRQ,
  557. PCIE_HOTPLUG,
  558. };
  559. static inline void __pcie_update_link_speed(struct pci_bus *bus,
  560. enum pcie_link_change_reason reason,
  561. u16 linksta, u16 linksta2)
  562. {
  563. bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
  564. bus->flit_mode = (linksta2 & PCI_EXP_LNKSTA2_FLIT) ? 1 : 0;
  565. trace_pcie_link_event(bus,
  566. reason,
  567. FIELD_GET(PCI_EXP_LNKSTA_NLW, linksta),
  568. linksta & PCI_EXP_LNKSTA_LINK_STATUS_MASK);
  569. }
  570. void pcie_update_link_speed(struct pci_bus *bus, enum pcie_link_change_reason reason);
  571. /* Single Root I/O Virtualization */
  572. struct pci_sriov {
  573. int pos; /* Capability position */
  574. int nres; /* Number of resources */
  575. u32 cap; /* SR-IOV Capabilities */
  576. u16 ctrl; /* SR-IOV Control */
  577. u16 total_VFs; /* Total VFs associated with the PF */
  578. u16 initial_VFs; /* Initial VFs associated with the PF */
  579. u16 num_VFs; /* Number of VFs available */
  580. u16 offset; /* First VF Routing ID offset */
  581. u16 stride; /* Following VF stride */
  582. u16 vf_device; /* VF device ID */
  583. u32 pgsz; /* Page size for BAR alignment */
  584. u8 link; /* Function Dependency Link */
  585. u8 max_VF_buses; /* Max buses consumed by VFs */
  586. u16 driver_max_VFs; /* Max num VFs driver supports */
  587. struct pci_dev *dev; /* Lowest numbered PF */
  588. struct pci_dev *self; /* This PF */
  589. u32 class; /* VF device */
  590. u8 hdr_type; /* VF header type */
  591. u16 subsystem_vendor; /* VF subsystem vendor */
  592. u16 subsystem_device; /* VF subsystem device */
  593. resource_size_t barsz[PCI_SRIOV_NUM_BARS]; /* VF BAR size */
  594. u16 vf_rebar_cap; /* VF Resizable BAR capability offset */
  595. bool drivers_autoprobe; /* Auto probing of VFs by driver */
  596. };
  597. #ifdef CONFIG_PCI_DOE
  598. void pci_doe_init(struct pci_dev *pdev);
  599. void pci_doe_destroy(struct pci_dev *pdev);
  600. void pci_doe_disconnected(struct pci_dev *pdev);
  601. #else
  602. static inline void pci_doe_init(struct pci_dev *pdev) { }
  603. static inline void pci_doe_destroy(struct pci_dev *pdev) { }
  604. static inline void pci_doe_disconnected(struct pci_dev *pdev) { }
  605. #endif
  606. #ifdef CONFIG_PCI_NPEM
  607. void pci_npem_create(struct pci_dev *dev);
  608. void pci_npem_remove(struct pci_dev *dev);
  609. #else
  610. static inline void pci_npem_create(struct pci_dev *dev) { }
  611. static inline void pci_npem_remove(struct pci_dev *dev) { }
  612. #endif
  613. #if defined(CONFIG_PCI_DOE) && defined(CONFIG_SYSFS)
  614. void pci_doe_sysfs_init(struct pci_dev *pci_dev);
  615. void pci_doe_sysfs_teardown(struct pci_dev *pdev);
  616. #else
  617. static inline void pci_doe_sysfs_init(struct pci_dev *pdev) { }
  618. static inline void pci_doe_sysfs_teardown(struct pci_dev *pdev) { }
  619. #endif
  620. #ifdef CONFIG_PCI_IDE
  621. void pci_ide_init(struct pci_dev *dev);
  622. void pci_ide_init_host_bridge(struct pci_host_bridge *hb);
  623. void pci_ide_destroy(struct pci_dev *dev);
  624. extern const struct attribute_group pci_ide_attr_group;
  625. #else
  626. static inline void pci_ide_init(struct pci_dev *dev) { }
  627. static inline void pci_ide_init_host_bridge(struct pci_host_bridge *hb) { }
  628. static inline void pci_ide_destroy(struct pci_dev *dev) { }
  629. #endif
  630. #ifdef CONFIG_PCI_TSM
  631. void pci_tsm_init(struct pci_dev *pdev);
  632. void pci_tsm_destroy(struct pci_dev *pdev);
  633. extern const struct attribute_group pci_tsm_attr_group;
  634. extern const struct attribute_group pci_tsm_auth_attr_group;
  635. #else
  636. static inline void pci_tsm_init(struct pci_dev *pdev) { }
  637. static inline void pci_tsm_destroy(struct pci_dev *pdev) { }
  638. #endif
  639. /**
  640. * pci_dev_set_io_state - Set the new error state if possible.
  641. *
  642. * @dev: PCI device to set new error_state
  643. * @new: the state we want dev to be in
  644. *
  645. * If the device is experiencing perm_failure, it has to remain in that state.
  646. * Any other transition is allowed.
  647. *
  648. * Returns true if state has been changed to the requested state.
  649. */
  650. static inline bool pci_dev_set_io_state(struct pci_dev *dev,
  651. pci_channel_state_t new)
  652. {
  653. pci_channel_state_t old;
  654. switch (new) {
  655. case pci_channel_io_perm_failure:
  656. xchg(&dev->error_state, pci_channel_io_perm_failure);
  657. return true;
  658. case pci_channel_io_frozen:
  659. old = cmpxchg(&dev->error_state, pci_channel_io_normal,
  660. pci_channel_io_frozen);
  661. return old != pci_channel_io_perm_failure;
  662. case pci_channel_io_normal:
  663. old = cmpxchg(&dev->error_state, pci_channel_io_frozen,
  664. pci_channel_io_normal);
  665. return old != pci_channel_io_perm_failure;
  666. default:
  667. return false;
  668. }
  669. }
  670. static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused)
  671. {
  672. pci_dev_set_io_state(dev, pci_channel_io_perm_failure);
  673. pci_doe_disconnected(dev);
  674. return 0;
  675. }
  676. /* pci_dev priv_flags */
  677. #define PCI_DEV_ADDED 0
  678. #define PCI_DPC_RECOVERED 1
  679. #define PCI_DPC_RECOVERING 2
  680. #define PCI_DEV_REMOVED 3
  681. #define PCI_LINK_CHANGED 4
  682. #define PCI_LINK_CHANGING 5
  683. #define PCI_LINK_LBMS_SEEN 6
  684. #define PCI_DEV_ALLOW_BINDING 7
  685. static inline void pci_dev_assign_added(struct pci_dev *dev)
  686. {
  687. smp_mb__before_atomic();
  688. set_bit(PCI_DEV_ADDED, &dev->priv_flags);
  689. smp_mb__after_atomic();
  690. }
  691. static inline bool pci_dev_test_and_clear_added(struct pci_dev *dev)
  692. {
  693. return test_and_clear_bit(PCI_DEV_ADDED, &dev->priv_flags);
  694. }
  695. static inline bool pci_dev_is_added(const struct pci_dev *dev)
  696. {
  697. return test_bit(PCI_DEV_ADDED, &dev->priv_flags);
  698. }
  699. static inline bool pci_dev_test_and_set_removed(struct pci_dev *dev)
  700. {
  701. return test_and_set_bit(PCI_DEV_REMOVED, &dev->priv_flags);
  702. }
  703. static inline void pci_dev_allow_binding(struct pci_dev *dev)
  704. {
  705. set_bit(PCI_DEV_ALLOW_BINDING, &dev->priv_flags);
  706. }
  707. static inline bool pci_dev_binding_disallowed(struct pci_dev *dev)
  708. {
  709. return !test_bit(PCI_DEV_ALLOW_BINDING, &dev->priv_flags);
  710. }
  711. #ifdef CONFIG_PCIEAER
  712. #include <linux/aer.h>
  713. #define AER_MAX_MULTI_ERR_DEVICES 5 /* Not likely to have more */
  714. /**
  715. * struct aer_err_info - AER Error Information
  716. * @dev: Devices reporting error
  717. * @ratelimit_print: Flag to log or not log the devices' error. 0=NotLog/1=Log
  718. * @__pad1: Padding for alignment
  719. * @error_dev_num: Number of devices reporting an error
  720. * @level: printk level to use in logging
  721. * @id: Value from register PCI_ERR_ROOT_ERR_SRC
  722. * @severity: AER severity, 0-UNCOR Non-fatal, 1-UNCOR fatal, 2-COR
  723. * @root_ratelimit_print: Flag to log or not log the root's error. 0=NotLog/1=Log
  724. * @multi_error_valid: If multiple errors are reported
  725. * @first_error: First reported error
  726. * @__pad2: Padding for alignment
  727. * @is_cxl: Bus type error: 0-PCI Bus error, 1-CXL Bus error
  728. * @tlp_header_valid: Indicates if TLP field contains error information
  729. * @status: COR/UNCOR error status
  730. * @mask: COR/UNCOR mask
  731. * @tlp: Transaction packet information
  732. */
  733. struct aer_err_info {
  734. struct pci_dev *dev[AER_MAX_MULTI_ERR_DEVICES];
  735. int ratelimit_print[AER_MAX_MULTI_ERR_DEVICES];
  736. int error_dev_num;
  737. const char *level;
  738. unsigned int id:16;
  739. unsigned int severity:2;
  740. unsigned int root_ratelimit_print:1;
  741. unsigned int __pad1:4;
  742. unsigned int multi_error_valid:1;
  743. unsigned int first_error:5;
  744. unsigned int __pad2:1;
  745. unsigned int is_cxl:1;
  746. unsigned int tlp_header_valid:1;
  747. unsigned int status;
  748. unsigned int mask;
  749. struct pcie_tlp_log tlp;
  750. };
  751. int aer_get_device_error_info(struct aer_err_info *info, int i);
  752. void aer_print_error(struct aer_err_info *info, int i);
  753. static inline const char *aer_err_bus(struct aer_err_info *info)
  754. {
  755. return info->is_cxl ? "CXL" : "PCIe";
  756. }
  757. int pcie_read_tlp_log(struct pci_dev *dev, int where, int where2,
  758. unsigned int tlp_len, bool flit,
  759. struct pcie_tlp_log *log);
  760. unsigned int aer_tlp_log_len(struct pci_dev *dev, u32 aercc);
  761. void pcie_print_tlp_log(const struct pci_dev *dev,
  762. const struct pcie_tlp_log *log, const char *level,
  763. const char *pfx);
  764. #endif /* CONFIG_PCIEAER */
  765. #ifdef CONFIG_PCIEPORTBUS
  766. /* Cached RCEC Endpoint Association */
  767. struct rcec_ea {
  768. u8 nextbusn;
  769. u8 lastbusn;
  770. u32 bitmap;
  771. };
  772. #endif
  773. #ifdef CONFIG_PCIE_DPC
  774. void pci_save_dpc_state(struct pci_dev *dev);
  775. void pci_restore_dpc_state(struct pci_dev *dev);
  776. void pci_dpc_init(struct pci_dev *pdev);
  777. void dpc_process_error(struct pci_dev *pdev);
  778. pci_ers_result_t dpc_reset_link(struct pci_dev *pdev);
  779. bool pci_dpc_recovered(struct pci_dev *pdev);
  780. unsigned int dpc_tlp_log_len(struct pci_dev *dev);
  781. #else
  782. static inline void pci_save_dpc_state(struct pci_dev *dev) { }
  783. static inline void pci_restore_dpc_state(struct pci_dev *dev) { }
  784. static inline void pci_dpc_init(struct pci_dev *pdev) { }
  785. static inline bool pci_dpc_recovered(struct pci_dev *pdev) { return false; }
  786. #endif
  787. #ifdef CONFIG_PCIEPORTBUS
  788. void pci_rcec_init(struct pci_dev *dev);
  789. void pci_rcec_exit(struct pci_dev *dev);
  790. void pcie_link_rcec(struct pci_dev *rcec);
  791. void pcie_walk_rcec(struct pci_dev *rcec,
  792. int (*cb)(struct pci_dev *, void *),
  793. void *userdata);
  794. #else
  795. static inline void pci_rcec_init(struct pci_dev *dev) { }
  796. static inline void pci_rcec_exit(struct pci_dev *dev) { }
  797. static inline void pcie_link_rcec(struct pci_dev *rcec) { }
  798. static inline void pcie_walk_rcec(struct pci_dev *rcec,
  799. int (*cb)(struct pci_dev *, void *),
  800. void *userdata) { }
  801. #endif
  802. #ifdef CONFIG_PCI_ATS
  803. /* Address Translation Service */
  804. void pci_ats_init(struct pci_dev *dev);
  805. void pci_restore_ats_state(struct pci_dev *dev);
  806. #else
  807. static inline void pci_ats_init(struct pci_dev *d) { }
  808. static inline void pci_restore_ats_state(struct pci_dev *dev) { }
  809. #endif /* CONFIG_PCI_ATS */
  810. #ifdef CONFIG_PCI_PRI
  811. void pci_pri_init(struct pci_dev *dev);
  812. void pci_restore_pri_state(struct pci_dev *pdev);
  813. #else
  814. static inline void pci_pri_init(struct pci_dev *dev) { }
  815. static inline void pci_restore_pri_state(struct pci_dev *pdev) { }
  816. #endif
  817. #ifdef CONFIG_PCI_PASID
  818. void pci_pasid_init(struct pci_dev *dev);
  819. void pci_restore_pasid_state(struct pci_dev *pdev);
  820. #else
  821. static inline void pci_pasid_init(struct pci_dev *dev) { }
  822. static inline void pci_restore_pasid_state(struct pci_dev *pdev) { }
  823. #endif
  824. #ifdef CONFIG_PCI_IOV
  825. int pci_iov_init(struct pci_dev *dev);
  826. void pci_iov_release(struct pci_dev *dev);
  827. void pci_iov_remove(struct pci_dev *dev);
  828. void pci_iov_update_resource(struct pci_dev *dev, int resno);
  829. resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno);
  830. void pci_restore_iov_state(struct pci_dev *dev);
  831. int pci_iov_bus_range(struct pci_bus *bus);
  832. void pci_iov_resource_set_size(struct pci_dev *dev, int resno, int size);
  833. bool pci_iov_is_memory_decoding_enabled(struct pci_dev *dev);
  834. static inline u16 pci_iov_vf_rebar_cap(struct pci_dev *dev)
  835. {
  836. if (!dev->is_physfn)
  837. return 0;
  838. return dev->sriov->vf_rebar_cap;
  839. }
  840. static inline bool pci_resource_is_iov(int resno)
  841. {
  842. return resno >= PCI_IOV_RESOURCES && resno <= PCI_IOV_RESOURCE_END;
  843. }
  844. static inline int pci_resource_num_from_vf_bar(int resno)
  845. {
  846. return resno + PCI_IOV_RESOURCES;
  847. }
  848. static inline int pci_resource_num_to_vf_bar(int resno)
  849. {
  850. return resno - PCI_IOV_RESOURCES;
  851. }
  852. extern const struct attribute_group sriov_pf_dev_attr_group;
  853. extern const struct attribute_group sriov_vf_dev_attr_group;
  854. #else
  855. static inline int pci_iov_init(struct pci_dev *dev)
  856. {
  857. return -ENODEV;
  858. }
  859. static inline void pci_iov_release(struct pci_dev *dev) { }
  860. static inline void pci_iov_remove(struct pci_dev *dev) { }
  861. static inline void pci_iov_update_resource(struct pci_dev *dev, int resno) { }
  862. static inline resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev,
  863. int resno)
  864. {
  865. return 0;
  866. }
  867. static inline void pci_restore_iov_state(struct pci_dev *dev) { }
  868. static inline int pci_iov_bus_range(struct pci_bus *bus)
  869. {
  870. return 0;
  871. }
  872. static inline void pci_iov_resource_set_size(struct pci_dev *dev, int resno,
  873. int size) { }
  874. static inline bool pci_iov_is_memory_decoding_enabled(struct pci_dev *dev)
  875. {
  876. return false;
  877. }
  878. static inline u16 pci_iov_vf_rebar_cap(struct pci_dev *dev)
  879. {
  880. return 0;
  881. }
  882. static inline bool pci_resource_is_iov(int resno)
  883. {
  884. return false;
  885. }
  886. static inline int pci_resource_num_from_vf_bar(int resno)
  887. {
  888. WARN_ON_ONCE(1);
  889. return -ENODEV;
  890. }
  891. static inline int pci_resource_num_to_vf_bar(int resno)
  892. {
  893. WARN_ON_ONCE(1);
  894. return -ENODEV;
  895. }
  896. #endif /* CONFIG_PCI_IOV */
  897. #ifdef CONFIG_PCIE_TPH
  898. void pci_restore_tph_state(struct pci_dev *dev);
  899. void pci_save_tph_state(struct pci_dev *dev);
  900. void pci_no_tph(void);
  901. void pci_tph_init(struct pci_dev *dev);
  902. #else
  903. static inline void pci_restore_tph_state(struct pci_dev *dev) { }
  904. static inline void pci_save_tph_state(struct pci_dev *dev) { }
  905. static inline void pci_no_tph(void) { }
  906. static inline void pci_tph_init(struct pci_dev *dev) { }
  907. #endif
  908. #ifdef CONFIG_PCIE_PTM
  909. void pci_ptm_init(struct pci_dev *dev);
  910. void pci_save_ptm_state(struct pci_dev *dev);
  911. void pci_restore_ptm_state(struct pci_dev *dev);
  912. void pci_suspend_ptm(struct pci_dev *dev);
  913. void pci_resume_ptm(struct pci_dev *dev);
  914. #else
  915. static inline void pci_ptm_init(struct pci_dev *dev) { }
  916. static inline void pci_save_ptm_state(struct pci_dev *dev) { }
  917. static inline void pci_restore_ptm_state(struct pci_dev *dev) { }
  918. static inline void pci_suspend_ptm(struct pci_dev *dev) { }
  919. static inline void pci_resume_ptm(struct pci_dev *dev) { }
  920. #endif
  921. static inline resource_size_t pci_resource_alignment(struct pci_dev *dev,
  922. struct resource *res)
  923. {
  924. int resno = pci_resource_num(dev, res);
  925. if (pci_resource_is_iov(resno))
  926. return pci_sriov_resource_alignment(dev, resno);
  927. if (dev->class >> 8 == PCI_CLASS_BRIDGE_CARDBUS)
  928. return pci_cardbus_resource_alignment(res);
  929. return resource_alignment(res);
  930. }
  931. void pci_acs_init(struct pci_dev *dev);
  932. void pci_enable_acs(struct pci_dev *dev);
  933. #ifdef CONFIG_PCI_QUIRKS
  934. int pci_dev_specific_acs_enabled(struct pci_dev *dev, u16 acs_flags);
  935. int pci_dev_specific_enable_acs(struct pci_dev *dev);
  936. int pci_dev_specific_disable_acs_redir(struct pci_dev *dev);
  937. void pci_disable_broken_acs_cap(struct pci_dev *pdev);
  938. int pcie_failed_link_retrain(struct pci_dev *dev);
  939. #else
  940. static inline int pci_dev_specific_acs_enabled(struct pci_dev *dev,
  941. u16 acs_flags)
  942. {
  943. return -ENOTTY;
  944. }
  945. static inline int pci_dev_specific_enable_acs(struct pci_dev *dev)
  946. {
  947. return -ENOTTY;
  948. }
  949. static inline int pci_dev_specific_disable_acs_redir(struct pci_dev *dev)
  950. {
  951. return -ENOTTY;
  952. }
  953. static inline void pci_disable_broken_acs_cap(struct pci_dev *dev) { }
  954. static inline int pcie_failed_link_retrain(struct pci_dev *dev)
  955. {
  956. return -ENOTTY;
  957. }
  958. #endif
  959. /* PCI error reporting and recovery */
  960. pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
  961. pci_channel_state_t state,
  962. pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev));
  963. bool pcie_wait_for_link(struct pci_dev *pdev, bool active);
  964. int pcie_retrain_link(struct pci_dev *pdev, bool use_lt);
  965. /* ASPM-related functionality we need even without CONFIG_PCIEASPM */
  966. void pci_save_ltr_state(struct pci_dev *dev);
  967. void pci_restore_ltr_state(struct pci_dev *dev);
  968. void pci_configure_aspm_l1ss(struct pci_dev *dev);
  969. void pci_save_aspm_l1ss_state(struct pci_dev *dev);
  970. void pci_restore_aspm_l1ss_state(struct pci_dev *dev);
  971. #ifdef CONFIG_PCIEASPM
  972. void pcie_aspm_remove_cap(struct pci_dev *pdev, u32 lnkcap);
  973. void pcie_aspm_init_link_state(struct pci_dev *pdev);
  974. void pcie_aspm_exit_link_state(struct pci_dev *pdev);
  975. void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked);
  976. void pcie_aspm_powersave_config_link(struct pci_dev *pdev);
  977. void pci_configure_ltr(struct pci_dev *pdev);
  978. void pci_bridge_reconfigure_ltr(struct pci_dev *pdev);
  979. #else
  980. static inline void pcie_aspm_remove_cap(struct pci_dev *pdev, u32 lnkcap) { }
  981. static inline void pcie_aspm_init_link_state(struct pci_dev *pdev) { }
  982. static inline void pcie_aspm_exit_link_state(struct pci_dev *pdev) { }
  983. static inline void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked) { }
  984. static inline void pcie_aspm_powersave_config_link(struct pci_dev *pdev) { }
  985. static inline void pci_configure_ltr(struct pci_dev *pdev) { }
  986. static inline void pci_bridge_reconfigure_ltr(struct pci_dev *pdev) { }
  987. #endif
  988. #ifdef CONFIG_PCIE_ECRC
  989. void pcie_set_ecrc_checking(struct pci_dev *dev);
  990. void pcie_ecrc_get_policy(char *str);
  991. #else
  992. static inline void pcie_set_ecrc_checking(struct pci_dev *dev) { }
  993. static inline void pcie_ecrc_get_policy(char *str) { }
  994. #endif
  995. #ifdef CONFIG_PCIEPORTBUS
  996. void pcie_reset_lbms(struct pci_dev *port);
  997. #else
  998. static inline void pcie_reset_lbms(struct pci_dev *port) {}
  999. #endif
  1000. struct pci_dev_reset_methods {
  1001. u16 vendor;
  1002. u16 device;
  1003. int (*reset)(struct pci_dev *dev, bool probe);
  1004. };
  1005. struct pci_reset_fn_method {
  1006. int (*reset_fn)(struct pci_dev *pdev, bool probe);
  1007. char *name;
  1008. };
  1009. extern const struct pci_reset_fn_method pci_reset_fn_methods[];
  1010. #ifdef CONFIG_PCI_QUIRKS
  1011. int pci_dev_specific_reset(struct pci_dev *dev, bool probe);
  1012. #else
  1013. static inline int pci_dev_specific_reset(struct pci_dev *dev, bool probe)
  1014. {
  1015. return -ENOTTY;
  1016. }
  1017. #endif
  1018. #if defined(CONFIG_PCI_QUIRKS) && defined(CONFIG_ARM64)
  1019. int acpi_get_rc_resources(struct device *dev, const char *hid, u16 segment,
  1020. struct resource *res);
  1021. #else
  1022. static inline int acpi_get_rc_resources(struct device *dev, const char *hid,
  1023. u16 segment, struct resource *res)
  1024. {
  1025. return -ENODEV;
  1026. }
  1027. #endif
  1028. void pci_rebar_init(struct pci_dev *pdev);
  1029. void pci_restore_rebar_state(struct pci_dev *pdev);
  1030. int pci_rebar_get_current_size(struct pci_dev *pdev, int bar);
  1031. int pci_rebar_set_size(struct pci_dev *pdev, int bar, int size);
  1032. struct device_node;
  1033. #define PCI_EQ_RESV 0xff
  1034. enum equalization_preset_type {
  1035. EQ_PRESET_TYPE_8GTS,
  1036. EQ_PRESET_TYPE_16GTS,
  1037. EQ_PRESET_TYPE_32GTS,
  1038. EQ_PRESET_TYPE_64GTS,
  1039. EQ_PRESET_TYPE_MAX
  1040. };
  1041. struct pci_eq_presets {
  1042. u16 eq_presets_8gts[MAX_NR_LANES];
  1043. u8 eq_presets_Ngts[EQ_PRESET_TYPE_MAX - 1][MAX_NR_LANES];
  1044. };
  1045. #ifdef CONFIG_OF
  1046. int of_get_pci_domain_nr(struct device_node *node);
  1047. int of_pci_get_max_link_speed(struct device_node *node);
  1048. u32 of_pci_get_slot_power_limit(struct device_node *node,
  1049. u8 *slot_power_limit_value,
  1050. u8 *slot_power_limit_scale);
  1051. bool of_pci_preserve_config(struct device_node *node);
  1052. int pci_set_of_node(struct pci_dev *dev);
  1053. void pci_release_of_node(struct pci_dev *dev);
  1054. void pci_set_bus_of_node(struct pci_bus *bus);
  1055. void pci_release_bus_of_node(struct pci_bus *bus);
  1056. int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge);
  1057. bool of_pci_supply_present(struct device_node *np);
  1058. int of_pci_get_equalization_presets(struct device *dev,
  1059. struct pci_eq_presets *presets,
  1060. int num_lanes);
  1061. #else
  1062. static inline int
  1063. of_get_pci_domain_nr(struct device_node *node)
  1064. {
  1065. return -1;
  1066. }
  1067. static inline int
  1068. of_pci_get_max_link_speed(struct device_node *node)
  1069. {
  1070. return -EINVAL;
  1071. }
  1072. static inline u32
  1073. of_pci_get_slot_power_limit(struct device_node *node,
  1074. u8 *slot_power_limit_value,
  1075. u8 *slot_power_limit_scale)
  1076. {
  1077. if (slot_power_limit_value)
  1078. *slot_power_limit_value = 0;
  1079. if (slot_power_limit_scale)
  1080. *slot_power_limit_scale = 0;
  1081. return 0;
  1082. }
  1083. static inline bool of_pci_preserve_config(struct device_node *node)
  1084. {
  1085. return false;
  1086. }
  1087. static inline int pci_set_of_node(struct pci_dev *dev) { return 0; }
  1088. static inline void pci_release_of_node(struct pci_dev *dev) { }
  1089. static inline void pci_set_bus_of_node(struct pci_bus *bus) { }
  1090. static inline void pci_release_bus_of_node(struct pci_bus *bus) { }
  1091. static inline int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge)
  1092. {
  1093. return 0;
  1094. }
  1095. static inline bool of_pci_supply_present(struct device_node *np)
  1096. {
  1097. return false;
  1098. }
  1099. static inline int of_pci_get_equalization_presets(struct device *dev,
  1100. struct pci_eq_presets *presets,
  1101. int num_lanes)
  1102. {
  1103. presets->eq_presets_8gts[0] = PCI_EQ_RESV;
  1104. for (int i = 0; i < EQ_PRESET_TYPE_MAX - 1; i++)
  1105. presets->eq_presets_Ngts[i][0] = PCI_EQ_RESV;
  1106. return 0;
  1107. }
  1108. #endif /* CONFIG_OF */
  1109. struct of_changeset;
  1110. #ifdef CONFIG_PCI_DYNAMIC_OF_NODES
  1111. void of_pci_make_dev_node(struct pci_dev *pdev);
  1112. void of_pci_remove_node(struct pci_dev *pdev);
  1113. int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs,
  1114. struct device_node *np);
  1115. void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge);
  1116. void of_pci_remove_host_bridge_node(struct pci_host_bridge *bridge);
  1117. int of_pci_add_host_bridge_properties(struct pci_host_bridge *bridge,
  1118. struct of_changeset *ocs,
  1119. struct device_node *np);
  1120. #else
  1121. static inline void of_pci_make_dev_node(struct pci_dev *pdev) { }
  1122. static inline void of_pci_remove_node(struct pci_dev *pdev) { }
  1123. static inline void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge) { }
  1124. static inline void of_pci_remove_host_bridge_node(struct pci_host_bridge *bridge) { }
  1125. #endif
  1126. #ifdef CONFIG_PCIEAER
  1127. void pci_no_aer(void);
  1128. void pci_aer_init(struct pci_dev *dev);
  1129. void pci_aer_exit(struct pci_dev *dev);
  1130. extern const struct attribute_group aer_stats_attr_group;
  1131. extern const struct attribute_group aer_attr_group;
  1132. void pci_aer_clear_fatal_status(struct pci_dev *dev);
  1133. int pci_aer_clear_status(struct pci_dev *dev);
  1134. int pci_aer_raw_clear_status(struct pci_dev *dev);
  1135. void pci_save_aer_state(struct pci_dev *dev);
  1136. void pci_restore_aer_state(struct pci_dev *dev);
  1137. #else
  1138. static inline void pci_no_aer(void) { }
  1139. static inline void pci_aer_init(struct pci_dev *d) { }
  1140. static inline void pci_aer_exit(struct pci_dev *d) { }
  1141. static inline void pci_aer_clear_fatal_status(struct pci_dev *dev) { }
  1142. static inline int pci_aer_clear_status(struct pci_dev *dev) { return -EINVAL; }
  1143. static inline int pci_aer_raw_clear_status(struct pci_dev *dev) { return -EINVAL; }
  1144. static inline void pci_save_aer_state(struct pci_dev *dev) { }
  1145. static inline void pci_restore_aer_state(struct pci_dev *dev) { }
  1146. #endif
  1147. #ifdef CONFIG_ACPI
  1148. bool pci_acpi_preserve_config(struct pci_host_bridge *bridge);
  1149. int pci_acpi_program_hp_params(struct pci_dev *dev);
  1150. extern const struct attribute_group pci_dev_acpi_attr_group;
  1151. void pci_set_acpi_fwnode(struct pci_dev *dev);
  1152. int pci_dev_acpi_reset(struct pci_dev *dev, bool probe);
  1153. bool acpi_pci_power_manageable(struct pci_dev *dev);
  1154. bool acpi_pci_bridge_d3(struct pci_dev *dev);
  1155. int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state);
  1156. pci_power_t acpi_pci_get_power_state(struct pci_dev *dev);
  1157. void acpi_pci_refresh_power_state(struct pci_dev *dev);
  1158. int acpi_pci_wakeup(struct pci_dev *dev, bool enable);
  1159. bool acpi_pci_need_resume(struct pci_dev *dev);
  1160. pci_power_t acpi_pci_choose_state(struct pci_dev *pdev);
  1161. #else
  1162. static inline bool pci_acpi_preserve_config(struct pci_host_bridge *bridge)
  1163. {
  1164. return false;
  1165. }
  1166. static inline int pci_dev_acpi_reset(struct pci_dev *dev, bool probe)
  1167. {
  1168. return -ENOTTY;
  1169. }
  1170. static inline void pci_set_acpi_fwnode(struct pci_dev *dev) { }
  1171. static inline int pci_acpi_program_hp_params(struct pci_dev *dev)
  1172. {
  1173. return -ENODEV;
  1174. }
  1175. static inline bool acpi_pci_power_manageable(struct pci_dev *dev)
  1176. {
  1177. return false;
  1178. }
  1179. static inline bool acpi_pci_bridge_d3(struct pci_dev *dev)
  1180. {
  1181. return false;
  1182. }
  1183. static inline int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  1184. {
  1185. return -ENODEV;
  1186. }
  1187. static inline pci_power_t acpi_pci_get_power_state(struct pci_dev *dev)
  1188. {
  1189. return PCI_UNKNOWN;
  1190. }
  1191. static inline void acpi_pci_refresh_power_state(struct pci_dev *dev) { }
  1192. static inline int acpi_pci_wakeup(struct pci_dev *dev, bool enable)
  1193. {
  1194. return -ENODEV;
  1195. }
  1196. static inline bool acpi_pci_need_resume(struct pci_dev *dev)
  1197. {
  1198. return false;
  1199. }
  1200. static inline pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
  1201. {
  1202. return PCI_POWER_ERROR;
  1203. }
  1204. #endif
  1205. #ifdef CONFIG_PCIEASPM
  1206. extern const struct attribute_group aspm_ctrl_attr_group;
  1207. #endif
  1208. #ifdef CONFIG_X86_INTEL_MID
  1209. bool pci_use_mid_pm(void);
  1210. int mid_pci_set_power_state(struct pci_dev *pdev, pci_power_t state);
  1211. pci_power_t mid_pci_get_power_state(struct pci_dev *pdev);
  1212. #else
  1213. static inline bool pci_use_mid_pm(void)
  1214. {
  1215. return false;
  1216. }
  1217. static inline int mid_pci_set_power_state(struct pci_dev *pdev, pci_power_t state)
  1218. {
  1219. return -ENODEV;
  1220. }
  1221. static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev)
  1222. {
  1223. return PCI_UNKNOWN;
  1224. }
  1225. #endif
  1226. #ifdef CONFIG_PCI_MSI
  1227. int pci_msix_write_tph_tag(struct pci_dev *pdev, unsigned int index, u16 tag);
  1228. #else
  1229. static inline int pci_msix_write_tph_tag(struct pci_dev *pdev, unsigned int index, u16 tag)
  1230. {
  1231. return -ENODEV;
  1232. }
  1233. #endif
  1234. /*
  1235. * Config Address for PCI Configuration Mechanism #1
  1236. *
  1237. * See PCI Local Bus Specification, Revision 3.0,
  1238. * Section 3.2.2.3.2, Figure 3-2, p. 50.
  1239. */
  1240. #define PCI_CONF1_BUS_SHIFT 16 /* Bus number */
  1241. #define PCI_CONF1_DEV_SHIFT 11 /* Device number */
  1242. #define PCI_CONF1_FUNC_SHIFT 8 /* Function number */
  1243. #define PCI_CONF1_BUS_MASK 0xff
  1244. #define PCI_CONF1_DEV_MASK 0x1f
  1245. #define PCI_CONF1_FUNC_MASK 0x7
  1246. #define PCI_CONF1_REG_MASK 0xfc /* Limit aligned offset to a maximum of 256B */
  1247. #define PCI_CONF1_ENABLE BIT(31)
  1248. #define PCI_CONF1_BUS(x) (((x) & PCI_CONF1_BUS_MASK) << PCI_CONF1_BUS_SHIFT)
  1249. #define PCI_CONF1_DEV(x) (((x) & PCI_CONF1_DEV_MASK) << PCI_CONF1_DEV_SHIFT)
  1250. #define PCI_CONF1_FUNC(x) (((x) & PCI_CONF1_FUNC_MASK) << PCI_CONF1_FUNC_SHIFT)
  1251. #define PCI_CONF1_REG(x) ((x) & PCI_CONF1_REG_MASK)
  1252. #define PCI_CONF1_ADDRESS(bus, dev, func, reg) \
  1253. (PCI_CONF1_ENABLE | \
  1254. PCI_CONF1_BUS(bus) | \
  1255. PCI_CONF1_DEV(dev) | \
  1256. PCI_CONF1_FUNC(func) | \
  1257. PCI_CONF1_REG(reg))
  1258. /*
  1259. * Extension of PCI Config Address for accessing extended PCIe registers
  1260. *
  1261. * No standardized specification, but used on lot of non-ECAM-compliant ARM SoCs
  1262. * or on AMD Barcelona and new CPUs. Reserved bits [27:24] of PCI Config Address
  1263. * are used for specifying additional 4 high bits of PCI Express register.
  1264. */
  1265. #define PCI_CONF1_EXT_REG_SHIFT 16
  1266. #define PCI_CONF1_EXT_REG_MASK 0xf00
  1267. #define PCI_CONF1_EXT_REG(x) (((x) & PCI_CONF1_EXT_REG_MASK) << PCI_CONF1_EXT_REG_SHIFT)
  1268. #define PCI_CONF1_EXT_ADDRESS(bus, dev, func, reg) \
  1269. (PCI_CONF1_ADDRESS(bus, dev, func, reg) | \
  1270. PCI_CONF1_EXT_REG(reg))
  1271. #endif /* DRIVERS_PCI_H */