blob: 8f40910b757b2f860a20a2e7dc4c45c558ef3c5b [file] [log] [blame]
Benjamin Faire5aafa52020-06-05 21:04:24 -07001/*
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
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070017#pragma once
18
Benjamin Faire5aafa52020-06-05 21:04:24 -070019#include "data.hpp"
Benjamin Fairc04c2c52020-06-05 09:14:44 -070020#include "internal/sys.hpp"
Benjamin Faire5aafa52020-06-05 21:04:24 -070021#include "pciaccess.hpp"
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070022
Benjamin Fairc04c2c52020-06-05 09:14:44 -070023#include <linux/pci_regs.h>
24
Patrick Williams28c00d62022-04-27 08:37:13 -050025#include <span>
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070026
Benjamin Faire5aafa52020-06-05 21:04:24 -070027// Some versions of the linux/pci_regs.h header don't define this
Benjamin Fairc04c2c52020-06-05 09:14:44 -070028#ifndef PCI_STD_NUM_BARS
29#define PCI_STD_NUM_BARS 6
30#endif // !PCI_STD_NUM_BARS
31
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070032namespace host_tool
33{
34
Benjamin Faire5aafa52020-06-05 21:04:24 -070035class PciBridgeIntf
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070036{
37 public:
Benjamin Faire5aafa52020-06-05 21:04:24 -070038 virtual ~PciBridgeIntf() = default;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070039
Patrick Williams28c00d62022-04-27 08:37:13 -050040 virtual void write(const std::span<const std::uint8_t> data) = 0;
Benjamin Faire5aafa52020-06-05 21:04:24 -070041 virtual void configure(const ipmi_flash::PciConfigResponse& config) = 0;
42
43 virtual std::size_t getDataLength() = 0;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070044};
45
Benjamin Faire5aafa52020-06-05 21:04:24 -070046class PciAccessBridge : public PciBridgeIntf
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070047{
48 public:
Benjamin Faire5aafa52020-06-05 21:04:24 -070049 virtual ~PciAccessBridge();
50
Patrick Williams28c00d62022-04-27 08:37:13 -050051 virtual void write(const std::span<const std::uint8_t> data) override;
Willy Tub487eb42021-09-16 21:44:43 -070052 virtual void configure(const ipmi_flash::PciConfigResponse&) override{};
Benjamin Faire5aafa52020-06-05 21:04:24 -070053
54 std::size_t getDataLength() override
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070055 {
Benjamin Faire5aafa52020-06-05 21:04:24 -070056 return dataLength;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070057 }
58
Benjamin Faire5aafa52020-06-05 21:04:24 -070059 protected:
60 /**
61 * Finds the PCI device matching @a match and saves a reference to it in @a
62 * dev. Also maps the memory region described in BAR number @a bar to
63 * address @a addr,
64 */
65 PciAccessBridge(const struct pci_id_match* match, int bar,
66 std::size_t dataOffset, std::size_t dataLength,
67 const PciAccess* pci);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070068
Benjamin Faire5aafa52020-06-05 21:04:24 -070069 struct pci_device* dev = nullptr;
70 std::uint8_t* addr = nullptr;
71 std::size_t size = 0;
Benjamin Fairc04c2c52020-06-05 09:14:44 -070072
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070073 private:
Benjamin Faire5aafa52020-06-05 21:04:24 -070074 std::size_t dataOffset;
75 std::size_t dataLength;
76
77 protected:
78 const PciAccess* pci;
79};
80
81class NuvotonPciBridge : public PciAccessBridge
82{
83 public:
Willy Tu49dd8ce2020-11-03 19:32:34 -080084 explicit NuvotonPciBridge(const PciAccess* pciAccess,
Willy Tu8a9de242020-10-30 11:00:21 -070085 bool skipBridgeDisable = false) :
Willy Tu49dd8ce2020-11-03 19:32:34 -080086 PciAccessBridge(&match, bar, dataOffset, dataLength, pciAccess),
Willy Tu8a9de242020-10-30 11:00:21 -070087 skipBridgeDisable(skipBridgeDisable)
Benjamin Fairc1a30c02020-06-09 11:46:34 -070088 {
89 enableBridge();
90 }
Benjamin Faire5aafa52020-06-05 21:04:24 -070091
92 ~NuvotonPciBridge()
Benjamin Fairc1a30c02020-06-09 11:46:34 -070093 {
Willy Tu8a9de242020-10-30 11:00:21 -070094 if (!skipBridgeDisable)
95 disableBridge();
Benjamin Fairc1a30c02020-06-09 11:46:34 -070096 }
Benjamin Faire5aafa52020-06-05 21:04:24 -070097
98 private:
99 static constexpr std::uint32_t vid = 0x1050;
100 static constexpr std::uint32_t did = 0x0750;
101 static constexpr int bar = 0;
102 static constexpr struct pci_id_match match
Benjamin Fairc04c2c52020-06-05 09:14:44 -0700103 {
Willy Tub487eb42021-09-16 21:44:43 -0700104 vid, did, PCI_MATCH_ANY, PCI_MATCH_ANY, 0, 0, 0
Benjamin Faire5aafa52020-06-05 21:04:24 -0700105 };
106
107 static constexpr pciaddr_t bridge = 0x04;
108 static constexpr std::uint8_t bridgeEnabled = 0x02;
109
110 static constexpr std::size_t dataOffset = 0x0;
111 static constexpr std::size_t dataLength = 0x4000;
Benjamin Fairc1a30c02020-06-09 11:46:34 -0700112
113 void enableBridge();
114 void disableBridge();
Willy Tu8a9de242020-10-30 11:00:21 -0700115
116 bool skipBridgeDisable;
Benjamin Faire5aafa52020-06-05 21:04:24 -0700117};
118
119class AspeedPciBridge : public PciAccessBridge
120{
121 public:
Willy Tu49dd8ce2020-11-03 19:32:34 -0800122 explicit AspeedPciBridge(const PciAccess* pciAccess,
Willy Tu8a9de242020-10-30 11:00:21 -0700123 bool skipBridgeDisable = false) :
Willy Tu49dd8ce2020-11-03 19:32:34 -0800124 PciAccessBridge(&match, bar, dataOffset, dataLength, pciAccess),
Willy Tu8a9de242020-10-30 11:00:21 -0700125 skipBridgeDisable(skipBridgeDisable)
Benjamin Faire5aafa52020-06-05 21:04:24 -0700126 {
127 enableBridge();
Benjamin Fairc04c2c52020-06-05 09:14:44 -0700128 }
129
Benjamin Faire5aafa52020-06-05 21:04:24 -0700130 ~AspeedPciBridge()
Benjamin Fairc04c2c52020-06-05 09:14:44 -0700131 {
Willy Tu8a9de242020-10-30 11:00:21 -0700132 if (!skipBridgeDisable)
133 disableBridge();
Benjamin Fairc04c2c52020-06-05 09:14:44 -0700134 }
Benjamin Faire5aafa52020-06-05 21:04:24 -0700135
136 void configure(const ipmi_flash::PciConfigResponse& configResp) override;
137
138 private:
139 static constexpr std::uint32_t vid = 0x1a03;
140 static constexpr std::uint32_t did = 0x2000;
141 static constexpr int bar = 1;
142 static constexpr struct pci_id_match match
143 {
Willy Tub487eb42021-09-16 21:44:43 -0700144 vid, did, PCI_MATCH_ANY, PCI_MATCH_ANY, 0, 0, 0
Benjamin Faire5aafa52020-06-05 21:04:24 -0700145 };
146
147 static constexpr std::size_t config = 0x0f000;
148 static constexpr std::size_t bridge = 0x0f004;
149 static constexpr std::uint32_t bridgeEnabled = 0x1;
150
151 static constexpr std::size_t dataOffset = 0x10000;
152 static constexpr std::size_t dataLength = 0x10000;
153
154 void enableBridge();
155 void disableBridge();
Willy Tu8a9de242020-10-30 11:00:21 -0700156
157 bool skipBridgeDisable;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700158};
159
160} // namespace host_tool