Martin Roth | 239b5df | 2022-07-26 22:18:26 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 3 | /* |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 4 | * PCI defines and function prototypes |
| 5 | * Copyright 1994, Drew Eckhardt |
| 6 | * Copyright 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz> |
| 7 | * |
| 8 | * For more information, please consult the following manuals (look at |
| 9 | * http://www.pcisig.com/ for how to get them): |
| 10 | * |
| 11 | * PCI BIOS Specification |
| 12 | * PCI Local Bus Specification |
| 13 | * PCI to PCI Bridge Specification |
| 14 | * PCI System Design Guide |
| 15 | */ |
| 16 | |
| 17 | #ifndef PCI_H |
| 18 | #define PCI_H |
| 19 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 20 | #if CONFIG(PCI) |
Kyösti Mälkki | 318066f | 2014-02-12 14:18:50 +0200 | [diff] [blame] | 21 | |
Elyes Haouas | 35c3ae3b | 2022-10-27 12:25:12 +0200 | [diff] [blame] | 22 | /* When <device/pci.h> is needed, it supposed to provide <device/pci_{def,type}.h> */ |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 23 | #include <device/device.h> |
Elyes Haouas | 35c3ae3b | 2022-10-27 12:25:12 +0200 | [diff] [blame] | 24 | #include <device/pci_def.h> /* IWYU pragma: export */ |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 25 | #include <device/pci_ops.h> |
Li-Ta Lo | 883b879 | 2005-01-10 23:16:22 +0000 | [diff] [blame] | 26 | #include <device/pci_rom.h> |
Elyes Haouas | 35c3ae3b | 2022-10-27 12:25:12 +0200 | [diff] [blame] | 27 | #include <device/pci_type.h> /* IWYU pragma: export */ |
Elyes Haouas | 35c3ae3b | 2022-10-27 12:25:12 +0200 | [diff] [blame] | 28 | #include <stddef.h> |
| 29 | #include <stdint.h> |
Kyösti Mälkki | ef84401 | 2013-06-25 23:17:43 +0300 | [diff] [blame] | 30 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 31 | /* Common pci operations without a standard interface */ |
| 32 | struct pci_operations { |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 33 | /* set the Subsystem IDs for the PCI device */ |
Aaron Durbin | c30d913 | 2017-08-07 16:55:43 -0600 | [diff] [blame] | 34 | void (*set_subsystem)(struct device *dev, unsigned int vendor, |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 35 | unsigned int device); |
Nico Huber | 968ef75 | 2021-03-07 01:39:18 +0100 | [diff] [blame] | 36 | void (*get_ltr_max_latencies)(u16 *max_snoop, u16 *max_nosnoop); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 39 | struct pci_driver { |
Uwe Hermann | 312673c | 2009-10-27 21:49:33 +0000 | [diff] [blame] | 40 | const struct device_operations *ops; |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 41 | unsigned short vendor; |
| 42 | unsigned short device; |
Vadim Bendebury | 8049fc9 | 2012-04-24 12:53:19 -0700 | [diff] [blame] | 43 | const unsigned short *devices; |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
Patrick Rudolph | 4e2f95b | 2018-05-16 14:56:22 +0200 | [diff] [blame] | 46 | struct msix_entry { |
| 47 | union { |
| 48 | struct { |
| 49 | u32 lower_addr; |
| 50 | u32 upper_addr; |
| 51 | }; |
| 52 | struct { |
| 53 | u64 addr; |
| 54 | }; |
| 55 | }; |
| 56 | u32 data; |
| 57 | u32 vec_control; |
| 58 | }; |
| 59 | |
Kyösti Mälkki | 44da9e2 | 2019-09-27 15:02:32 +0300 | [diff] [blame] | 60 | #if ENV_RAMSTAGE |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 61 | #define __pci_driver __attribute__((used, __section__(".rodata.pci_driver"))) |
Kyösti Mälkki | 44da9e2 | 2019-09-27 15:02:32 +0300 | [diff] [blame] | 62 | #else |
| 63 | #define __pci_driver __attribute__((unused)) |
Aaron Durbin | c30d913 | 2017-08-07 16:55:43 -0600 | [diff] [blame] | 64 | #endif |
Kyösti Mälkki | 44da9e2 | 2019-09-27 15:02:32 +0300 | [diff] [blame] | 65 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 66 | /** start of compile time generated pci driver array */ |
Aaron Durbin | 0375815 | 2015-09-03 17:23:08 -0500 | [diff] [blame] | 67 | extern struct pci_driver _pci_drivers[]; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 68 | /** end of compile time generated pci driver array */ |
Aaron Durbin | 0375815 | 2015-09-03 17:23:08 -0500 | [diff] [blame] | 69 | extern struct pci_driver _epci_drivers[]; |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 70 | |
Subrata Banik | ffc790b | 2017-12-11 10:29:49 +0530 | [diff] [blame] | 71 | /* Set Subsystem ID operation for PCI devices */ |
| 72 | extern struct pci_operations pci_dev_ops_pci; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 73 | extern struct device_operations default_pci_ops_dev; |
| 74 | extern struct device_operations default_pci_ops_bus; |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 75 | |
Aaron Durbin | c30d913 | 2017-08-07 16:55:43 -0600 | [diff] [blame] | 76 | void pci_dev_read_resources(struct device *dev); |
| 77 | void pci_bus_read_resources(struct device *dev); |
| 78 | void pci_dev_set_resources(struct device *dev); |
| 79 | void pci_dev_enable_resources(struct device *dev); |
| 80 | void pci_bus_enable_resources(struct device *dev); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 81 | void pci_bus_reset(struct bus *bus); |
Aaron Durbin | c30d913 | 2017-08-07 16:55:43 -0600 | [diff] [blame] | 82 | struct device *pci_probe_dev(struct device *dev, struct bus *bus, |
| 83 | unsigned int devfn); |
Patrick Rudolph | aa6971e | 2018-05-03 12:54:47 +0200 | [diff] [blame] | 84 | void do_pci_scan_bridge(struct device *dev, |
Kyösti Mälkki | de271a8 | 2015-03-18 13:09:47 +0200 | [diff] [blame] | 85 | void (*do_scan_bus)(struct bus *bus, |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 86 | unsigned int min_devfn, unsigned int max_devfn)); |
Kyösti Mälkki | de271a8 | 2015-03-18 13:09:47 +0200 | [diff] [blame] | 87 | |
Aaron Durbin | c30d913 | 2017-08-07 16:55:43 -0600 | [diff] [blame] | 88 | void pci_scan_bridge(struct device *bus); |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 89 | void pci_scan_bus(struct bus *bus, unsigned int min_devfn, |
| 90 | unsigned int max_devfn); |
Kyösti Mälkki | de271a8 | 2015-03-18 13:09:47 +0200 | [diff] [blame] | 91 | |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 92 | uint8_t pci_moving_config8(struct device *dev, unsigned int reg); |
| 93 | uint16_t pci_moving_config16(struct device *dev, unsigned int reg); |
| 94 | uint32_t pci_moving_config32(struct device *dev, unsigned int reg); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 95 | struct resource *pci_get_resource(struct device *dev, unsigned long index); |
Aaron Durbin | c30d913 | 2017-08-07 16:55:43 -0600 | [diff] [blame] | 96 | void pci_dev_set_subsystem(struct device *dev, unsigned int vendor, |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 97 | unsigned int device); |
Myles Watson | 43bb9cd | 2008-12-18 18:24:11 +0000 | [diff] [blame] | 98 | void pci_dev_init(struct device *dev); |
Aaron Durbin | c30d913 | 2017-08-07 16:55:43 -0600 | [diff] [blame] | 99 | unsigned int pci_match_simple_dev(struct device *dev, pci_devfn_t sdev); |
Bill XIE | 513d359 | 2022-08-02 22:55:51 +0800 | [diff] [blame] | 100 | uint16_t pci_find_cap_recursive(const struct device *dev, uint16_t cap); |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 101 | |
Lee Leahy | 6d71a43 | 2017-03-07 15:24:16 -0800 | [diff] [blame] | 102 | const char *pin_to_str(int pin); |
Aaron Durbin | c30d913 | 2017-08-07 16:55:43 -0600 | [diff] [blame] | 103 | int get_pci_irq_pins(struct device *dev, struct device **parent_bdg); |
Kyösti Mälkki | c19d6a6 | 2019-07-04 21:39:28 +0300 | [diff] [blame] | 104 | void pci_assign_irqs(struct device *dev, const unsigned char pIntAtoD[4]); |
Aaron Durbin | c30d913 | 2017-08-07 16:55:43 -0600 | [diff] [blame] | 105 | const char *get_pci_class_name(struct device *dev); |
| 106 | const char *get_pci_subclass_name(struct device *dev); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 107 | |
Patrick Rudolph | 4e2f95b | 2018-05-16 14:56:22 +0200 | [diff] [blame] | 108 | size_t pci_msix_table_size(struct device *dev); |
| 109 | int pci_msix_table_bar(struct device *dev, u32 *offset, u8 *idx); |
| 110 | struct msix_entry *pci_msix_get_table(struct device *dev); |
| 111 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 112 | #define PCI_IO_BRIDGE_ALIGN 4096 |
| 113 | #define PCI_MEM_BRIDGE_ALIGN (1024*1024) |
| 114 | |
Kyösti Mälkki | 8a41f4b | 2019-02-08 18:14:34 +0200 | [diff] [blame] | 115 | #define PCI_ID(VENDOR_ID, DEVICE_ID) \ |
| 116 | ((((DEVICE_ID) & 0xFFFF) << 16) | ((VENDOR_ID) & 0xFFFF)) |
| 117 | |
| 118 | pci_devfn_t pci_locate_device(unsigned int pci_id, pci_devfn_t dev); |
| 119 | pci_devfn_t pci_locate_device_on_bus(unsigned int pci_id, unsigned int bus); |
| 120 | |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 121 | void pci_s_assert_secondary_reset(pci_devfn_t p2p_bridge); |
| 122 | void pci_s_deassert_secondary_reset(pci_devfn_t p2p_bridge); |
| 123 | void pci_s_bridge_set_secondary(pci_devfn_t p2p_bridge, u8 secondary); |
| 124 | |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 125 | int pci_early_device_probe(u8 bus, u8 dev, u32 mmio_base); |
Kyösti Mälkki | 2161c1d | 2014-02-11 19:56:57 +0200 | [diff] [blame] | 126 | |
Alexandru Gagniuc | 420caaf | 2016-01-05 17:00:27 -0800 | [diff] [blame] | 127 | static inline int pci_base_address_is_memory_space(unsigned int attr) |
| 128 | { |
| 129 | return (attr & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY; |
| 130 | } |
Alexandru Gagniuc | 420caaf | 2016-01-05 17:00:27 -0800 | [diff] [blame] | 131 | |
John Zhao | 95b4ece0 | 2020-05-04 15:58:48 -0700 | [diff] [blame] | 132 | void pci_dev_disable_bus_master(const struct device *dev); |
Felix Singer | e4a7d9f | 2020-06-15 15:00:56 +0200 | [diff] [blame] | 133 | |
| 134 | static __always_inline |
| 135 | #if ENV_PCI_SIMPLE_DEVICE |
| 136 | void pci_dev_request_bus_master(pci_devfn_t dev) |
| 137 | #else |
Subrata Banik | 86e5326 | 2020-09-05 14:03:31 +0530 | [diff] [blame] | 138 | void pci_dev_request_bus_master(struct device *dev) |
Felix Singer | e4a7d9f | 2020-06-15 15:00:56 +0200 | [diff] [blame] | 139 | #endif /* ENV_PCI_SIMPLE_DEVICE */ |
| 140 | { |
Felix Singer | 3d9fa08 | 2020-09-07 13:57:49 +0200 | [diff] [blame] | 141 | if (CONFIG(PCI_ALLOW_BUS_MASTER_ANY_DEVICE)) |
Felix Singer | e4a7d9f | 2020-06-15 15:00:56 +0200 | [diff] [blame] | 142 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER); |
| 143 | } |
| 144 | |
Kyösti Mälkki | 318066f | 2014-02-12 14:18:50 +0200 | [diff] [blame] | 145 | #endif /* CONFIG_PCI */ |
| 146 | |
Kyösti Mälkki | 5b14116 | 2019-01-23 16:02:28 +0200 | [diff] [blame] | 147 | void pci_early_bridge_init(void); |
| 148 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 149 | #endif /* PCI_H */ |