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 17 #pragma once 18 19 #include "data.hpp" 20 #include "internal/sys.hpp" 21 #include "pciaccess.hpp" 22 23 #include <linux/pci_regs.h> 24 25 #include <stdplus/types.hpp> 26 27 // Some versions of the linux/pci_regs.h header don't define this 28 #ifndef PCI_STD_NUM_BARS 29 #define PCI_STD_NUM_BARS 6 30 #endif // !PCI_STD_NUM_BARS 31 32 namespace host_tool 33 { 34 35 class PciBridgeIntf 36 { 37 public: 38 virtual ~PciBridgeIntf() = default; 39 40 virtual void write(const stdplus::span<const std::uint8_t> data) = 0; 41 virtual void configure(const ipmi_flash::PciConfigResponse& config) = 0; 42 43 virtual std::size_t getDataLength() = 0; 44 }; 45 46 class PciAccessBridge : public PciBridgeIntf 47 { 48 public: 49 virtual ~PciAccessBridge(); 50 51 virtual void write(const stdplus::span<const std::uint8_t> data) override; 52 virtual void 53 configure(const ipmi_flash::PciConfigResponse& configResp) override{}; 54 55 std::size_t getDataLength() override 56 { 57 return dataLength; 58 } 59 60 protected: 61 /** 62 * Finds the PCI device matching @a match and saves a reference to it in @a 63 * dev. Also maps the memory region described in BAR number @a bar to 64 * address @a addr, 65 */ 66 PciAccessBridge(const struct pci_id_match* match, int bar, 67 std::size_t dataOffset, std::size_t dataLength, 68 const PciAccess* pci); 69 70 struct pci_device* dev = nullptr; 71 std::uint8_t* addr = nullptr; 72 std::size_t size = 0; 73 74 private: 75 std::size_t dataOffset; 76 std::size_t dataLength; 77 78 protected: 79 const PciAccess* pci; 80 }; 81 82 class NuvotonPciBridge : public PciAccessBridge 83 { 84 public: 85 explicit NuvotonPciBridge(const PciAccess* pciAccess, 86 bool skipBridgeDisable = false) : 87 PciAccessBridge(&match, bar, dataOffset, dataLength, pciAccess), 88 skipBridgeDisable(skipBridgeDisable) 89 { 90 enableBridge(); 91 } 92 93 ~NuvotonPciBridge() 94 { 95 if (!skipBridgeDisable) 96 disableBridge(); 97 } 98 99 private: 100 static constexpr std::uint32_t vid = 0x1050; 101 static constexpr std::uint32_t did = 0x0750; 102 static constexpr int bar = 0; 103 static constexpr struct pci_id_match match 104 { 105 vid, did, PCI_MATCH_ANY, PCI_MATCH_ANY 106 }; 107 108 static constexpr pciaddr_t bridge = 0x04; 109 static constexpr std::uint8_t bridgeEnabled = 0x02; 110 111 static constexpr std::size_t dataOffset = 0x0; 112 static constexpr std::size_t dataLength = 0x4000; 113 114 void enableBridge(); 115 void disableBridge(); 116 117 bool skipBridgeDisable; 118 }; 119 120 class AspeedPciBridge : public PciAccessBridge 121 { 122 public: 123 explicit AspeedPciBridge(const PciAccess* pciAccess, 124 bool skipBridgeDisable = false) : 125 PciAccessBridge(&match, bar, dataOffset, dataLength, pciAccess), 126 skipBridgeDisable(skipBridgeDisable) 127 { 128 enableBridge(); 129 } 130 131 ~AspeedPciBridge() 132 { 133 if (!skipBridgeDisable) 134 disableBridge(); 135 } 136 137 void configure(const ipmi_flash::PciConfigResponse& configResp) override; 138 139 private: 140 static constexpr std::uint32_t vid = 0x1a03; 141 static constexpr std::uint32_t did = 0x2000; 142 static constexpr int bar = 1; 143 static constexpr struct pci_id_match match 144 { 145 vid, did, PCI_MATCH_ANY, PCI_MATCH_ANY 146 }; 147 148 static constexpr std::size_t config = 0x0f000; 149 static constexpr std::size_t bridge = 0x0f004; 150 static constexpr std::uint32_t bridgeEnabled = 0x1; 151 152 static constexpr std::size_t dataOffset = 0x10000; 153 static constexpr std::size_t dataLength = 0x10000; 154 155 void enableBridge(); 156 void disableBridge(); 157 158 bool skipBridgeDisable; 159 }; 160 161 } // namespace host_tool 162