pci.h 26 KB

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