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