Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 15 | #ifndef PCI_OPS_H |
| 16 | #define PCI_OPS_H |
| 17 | |
| 18 | #include <stdint.h> |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 19 | #include <device/device.h> |
Kyösti Mälkki | 3e6913b | 2019-03-02 16:26:10 +0200 | [diff] [blame] | 20 | #include <device/pci_type.h> |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 21 | #include <arch/pci_ops.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 22 | |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 23 | void __noreturn pcidev_die(void); |
| 24 | |
Kyösti Mälkki | 34cf561 | 2019-03-11 20:34:26 +0200 | [diff] [blame] | 25 | static __always_inline pci_devfn_t pcidev_bdf(const struct device *dev) |
| 26 | { |
| 27 | return (dev->path.pci.devfn << 12) | (dev->bus->secondary << 20); |
| 28 | } |
| 29 | |
| 30 | static __always_inline pci_devfn_t pcidev_assert(const struct device *dev) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 31 | { |
| 32 | if (!dev) |
| 33 | pcidev_die(); |
Kyösti Mälkki | 34cf561 | 2019-03-11 20:34:26 +0200 | [diff] [blame] | 34 | return pcidev_bdf(dev); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 35 | } |
Kyösti Mälkki | 3f98d41 | 2019-07-29 16:38:14 +0300 | [diff] [blame] | 36 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 37 | #if defined(__SIMPLE_DEVICE__) |
| 38 | #define ENV_PCI_SIMPLE_DEVICE 1 |
| 39 | #else |
| 40 | #define ENV_PCI_SIMPLE_DEVICE 0 |
| 41 | #endif |
| 42 | |
| 43 | #if ENV_PCI_SIMPLE_DEVICE |
Kyösti Mälkki | 3f98d41 | 2019-07-29 16:38:14 +0300 | [diff] [blame] | 44 | |
| 45 | /* Avoid name collisions as different stages have different signature |
| 46 | * for these functions. The _s_ stands for simple, fundamental IO or |
| 47 | * MMIO variant. |
| 48 | */ |
| 49 | #define pci_read_config8 pci_s_read_config8 |
| 50 | #define pci_read_config16 pci_s_read_config16 |
| 51 | #define pci_read_config32 pci_s_read_config32 |
| 52 | #define pci_write_config8 pci_s_write_config8 |
| 53 | #define pci_write_config16 pci_s_write_config16 |
| 54 | #define pci_write_config32 pci_s_write_config32 |
| 55 | #else |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 56 | |
| 57 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 58 | u8 pci_read_config8(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 59 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 60 | return pci_s_read_config8(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 64 | u16 pci_read_config16(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 65 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 66 | return pci_s_read_config16(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 70 | u32 pci_read_config32(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 71 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 72 | return pci_s_read_config32(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 76 | void pci_write_config8(const struct device *dev, u16 reg, u8 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 77 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 78 | pci_s_write_config8(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 82 | void pci_write_config16(const struct device *dev, u16 reg, u16 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 83 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 84 | pci_s_write_config16(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 88 | void pci_write_config32(const struct device *dev, u16 reg, u32 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 89 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 90 | pci_s_write_config32(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Stefan Reinauer | 24d1d4b | 2013-03-21 11:51:41 -0700 | [diff] [blame] | 93 | #endif |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 94 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 95 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 96 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 97 | void pci_or_config8(pci_devfn_t dev, u16 reg, u8 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 98 | #else |
| 99 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 100 | void pci_or_config8(const struct device *dev, u16 reg, u8 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 101 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 102 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 103 | u8 value = pci_read_config8(dev, reg); |
| 104 | pci_write_config8(dev, reg, value | ormask); |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 107 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 108 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 109 | void pci_or_config16(pci_devfn_t dev, u16 reg, u16 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 110 | #else |
| 111 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 112 | void pci_or_config16(const struct device *dev, u16 reg, u16 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 113 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 114 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 115 | u16 value = pci_read_config16(dev, reg); |
| 116 | pci_write_config16(dev, reg, value | ormask); |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 119 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 120 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 121 | void pci_or_config32(pci_devfn_t dev, u16 reg, u32 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 122 | #else |
| 123 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 124 | void pci_or_config32(const struct device *dev, u16 reg, u32 ormask) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 125 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 126 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 127 | u32 value = pci_read_config32(dev, reg); |
| 128 | pci_write_config32(dev, reg, value | ormask); |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 131 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 132 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 133 | void pci_update_config8(pci_devfn_t dev, u16 reg, u8 mask, u8 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 134 | #else |
| 135 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 136 | void pci_update_config8(const struct device *dev, u16 reg, u8 mask, u8 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 137 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 138 | { |
| 139 | u8 reg8; |
| 140 | |
| 141 | reg8 = pci_read_config8(dev, reg); |
| 142 | reg8 &= mask; |
| 143 | reg8 |= or; |
| 144 | pci_write_config8(dev, reg, reg8); |
| 145 | } |
| 146 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 147 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 148 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 149 | void pci_update_config16(pci_devfn_t dev, u16 reg, u16 mask, u16 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 150 | #else |
| 151 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 152 | void pci_update_config16(const struct device *dev, u16 reg, u16 mask, u16 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 153 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 154 | { |
| 155 | u16 reg16; |
| 156 | |
| 157 | reg16 = pci_read_config16(dev, reg); |
| 158 | reg16 &= mask; |
| 159 | reg16 |= or; |
| 160 | pci_write_config16(dev, reg, reg16); |
| 161 | } |
| 162 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 163 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 164 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 165 | void pci_update_config32(pci_devfn_t dev, u16 reg, u32 mask, u32 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 166 | #else |
| 167 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 168 | void pci_update_config32(const struct device *dev, u16 reg, u32 mask, u32 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 169 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 170 | { |
| 171 | u32 reg32; |
| 172 | |
| 173 | reg32 = pci_read_config32(dev, reg); |
| 174 | reg32 &= mask; |
| 175 | reg32 |= or; |
| 176 | pci_write_config32(dev, reg, reg32); |
| 177 | } |
| 178 | |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 179 | u16 pci_s_find_next_capability(pci_devfn_t dev, u16 cap, u16 last); |
| 180 | u16 pci_s_find_capability(pci_devfn_t dev, u16 cap); |
| 181 | |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 182 | static __always_inline |
| 183 | u16 pci_find_next_capability(const struct device *dev, u16 cap, u16 last) |
| 184 | { |
| 185 | return pci_s_find_next_capability(PCI_BDF(dev), cap, last); |
| 186 | } |
| 187 | |
| 188 | static __always_inline |
| 189 | u16 pci_find_capability(const struct device *dev, u16 cap) |
| 190 | { |
| 191 | return pci_s_find_capability(PCI_BDF(dev), cap); |
| 192 | } |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 193 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 194 | #endif /* PCI_OPS_H */ |