Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tim Lee | 2d57d52 | 2023-09-18 11:47:53 +0800 | [diff] [blame] | 17 | #include "config.h" |
| 18 | |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 19 | #pragma once |
| 20 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 21 | #include "data.hpp" |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame] | 22 | #include "internal/sys.hpp" |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 23 | #include "pciaccess.hpp" |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 24 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame] | 25 | #include <linux/pci_regs.h> |
| 26 | |
Patrick Williams | 28c00d6 | 2022-04-27 08:37:13 -0500 | [diff] [blame] | 27 | #include <span> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 28 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 29 | // Some versions of the linux/pci_regs.h header don't define this |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame] | 30 | #ifndef PCI_STD_NUM_BARS |
| 31 | #define PCI_STD_NUM_BARS 6 |
| 32 | #endif // !PCI_STD_NUM_BARS |
| 33 | |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 34 | namespace host_tool |
| 35 | { |
| 36 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 37 | class PciBridgeIntf |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 38 | { |
| 39 | public: |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 40 | virtual ~PciBridgeIntf() = default; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 41 | |
Patrick Williams | 28c00d6 | 2022-04-27 08:37:13 -0500 | [diff] [blame] | 42 | virtual void write(const std::span<const std::uint8_t> data) = 0; |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 43 | virtual void configure(const ipmi_flash::PciConfigResponse& config) = 0; |
| 44 | |
| 45 | virtual std::size_t getDataLength() = 0; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 48 | class PciAccessBridge : public PciBridgeIntf |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 49 | { |
| 50 | public: |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 51 | virtual ~PciAccessBridge(); |
| 52 | |
Patrick Williams | 28c00d6 | 2022-04-27 08:37:13 -0500 | [diff] [blame] | 53 | virtual void write(const std::span<const std::uint8_t> data) override; |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 54 | virtual void configure(const ipmi_flash::PciConfigResponse&) override {}; |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 55 | |
| 56 | std::size_t getDataLength() override |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 57 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 58 | return dataLength; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 61 | protected: |
| 62 | /** |
| 63 | * Finds the PCI device matching @a match and saves a reference to it in @a |
| 64 | * dev. Also maps the memory region described in BAR number @a bar to |
| 65 | * address @a addr, |
| 66 | */ |
| 67 | PciAccessBridge(const struct pci_id_match* match, int bar, |
| 68 | std::size_t dataOffset, std::size_t dataLength, |
| 69 | const PciAccess* pci); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 70 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 71 | struct pci_device* dev = nullptr; |
| 72 | std::uint8_t* addr = nullptr; |
| 73 | std::size_t size = 0; |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame] | 74 | |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 75 | private: |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 76 | std::size_t dataOffset; |
| 77 | std::size_t dataLength; |
| 78 | |
| 79 | protected: |
| 80 | const PciAccess* pci; |
| 81 | }; |
| 82 | |
| 83 | class NuvotonPciBridge : public PciAccessBridge |
| 84 | { |
| 85 | public: |
Willy Tu | 49dd8ce | 2020-11-03 19:32:34 -0800 | [diff] [blame] | 86 | explicit NuvotonPciBridge(const PciAccess* pciAccess, |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 87 | bool skipBridgeDisable = false) : |
Willy Tu | 49dd8ce | 2020-11-03 19:32:34 -0800 | [diff] [blame] | 88 | PciAccessBridge(&match, bar, dataOffset, dataLength, pciAccess), |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 89 | skipBridgeDisable(skipBridgeDisable) |
Benjamin Fair | c1a30c0 | 2020-06-09 11:46:34 -0700 | [diff] [blame] | 90 | { |
| 91 | enableBridge(); |
| 92 | } |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 93 | |
| 94 | ~NuvotonPciBridge() |
Benjamin Fair | c1a30c0 | 2020-06-09 11:46:34 -0700 | [diff] [blame] | 95 | { |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 96 | if (!skipBridgeDisable) |
| 97 | disableBridge(); |
Benjamin Fair | c1a30c0 | 2020-06-09 11:46:34 -0700 | [diff] [blame] | 98 | } |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 99 | |
| 100 | private: |
| 101 | static constexpr std::uint32_t vid = 0x1050; |
Tim Lee | 2d57d52 | 2023-09-18 11:47:53 +0800 | [diff] [blame] | 102 | static constexpr std::uint32_t did = NUVOTON_PCI_DID; |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 103 | static constexpr int bar = 0; |
| 104 | static constexpr struct pci_id_match match |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame] | 105 | { |
Willy Tu | b487eb4 | 2021-09-16 21:44:43 -0700 | [diff] [blame] | 106 | vid, did, PCI_MATCH_ANY, PCI_MATCH_ANY, 0, 0, 0 |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | static constexpr pciaddr_t bridge = 0x04; |
| 110 | static constexpr std::uint8_t bridgeEnabled = 0x02; |
| 111 | |
| 112 | static constexpr std::size_t dataOffset = 0x0; |
| 113 | static constexpr std::size_t dataLength = 0x4000; |
Benjamin Fair | c1a30c0 | 2020-06-09 11:46:34 -0700 | [diff] [blame] | 114 | |
| 115 | void enableBridge(); |
| 116 | void disableBridge(); |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 117 | |
| 118 | bool skipBridgeDisable; |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | class AspeedPciBridge : public PciAccessBridge |
| 122 | { |
| 123 | public: |
Willy Tu | 49dd8ce | 2020-11-03 19:32:34 -0800 | [diff] [blame] | 124 | explicit AspeedPciBridge(const PciAccess* pciAccess, |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 125 | bool skipBridgeDisable = false) : |
Willy Tu | 49dd8ce | 2020-11-03 19:32:34 -0800 | [diff] [blame] | 126 | PciAccessBridge(&match, bar, dataOffset, dataLength, pciAccess), |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 127 | skipBridgeDisable(skipBridgeDisable) |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 128 | { |
| 129 | enableBridge(); |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 132 | ~AspeedPciBridge() |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame] | 133 | { |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 134 | if (!skipBridgeDisable) |
| 135 | disableBridge(); |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame] | 136 | } |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 137 | |
| 138 | void configure(const ipmi_flash::PciConfigResponse& configResp) override; |
| 139 | |
| 140 | private: |
| 141 | static constexpr std::uint32_t vid = 0x1a03; |
| 142 | static constexpr std::uint32_t did = 0x2000; |
| 143 | static constexpr int bar = 1; |
| 144 | static constexpr struct pci_id_match match |
| 145 | { |
Willy Tu | b487eb4 | 2021-09-16 21:44:43 -0700 | [diff] [blame] | 146 | vid, did, PCI_MATCH_ANY, PCI_MATCH_ANY, 0, 0, 0 |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | static constexpr std::size_t config = 0x0f000; |
| 150 | static constexpr std::size_t bridge = 0x0f004; |
| 151 | static constexpr std::uint32_t bridgeEnabled = 0x1; |
| 152 | |
| 153 | static constexpr std::size_t dataOffset = 0x10000; |
| 154 | static constexpr std::size_t dataLength = 0x10000; |
| 155 | |
| 156 | void enableBridge(); |
| 157 | void disableBridge(); |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 158 | |
| 159 | bool skipBridgeDisable; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | } // namespace host_tool |