blob: ae74e9da635fe5814b2ac9ea379b22049913d8a4 [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
Benjamin Faire5aafa52020-06-05 21:04:24 -070025#include <stdplus/types.hpp>
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
Benjamin Faire5aafa52020-06-05 21:04:24 -070040 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;
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
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
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070056 {
Benjamin Faire5aafa52020-06-05 21:04:24 -070057 return dataLength;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070058 }
59
Benjamin Faire5aafa52020-06-05 21:04:24 -070060 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);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070069
Benjamin Faire5aafa52020-06-05 21:04:24 -070070 struct pci_device* dev = nullptr;
71 std::uint8_t* addr = nullptr;
72 std::size_t size = 0;
Benjamin Fairc04c2c52020-06-05 09:14:44 -070073
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070074 private:
Benjamin Faire5aafa52020-06-05 21:04:24 -070075 std::size_t dataOffset;
76 std::size_t dataLength;
77
78 protected:
79 const PciAccess* pci;
80};
81
82class NuvotonPciBridge : public PciAccessBridge
83{
84 public:
Willy Tu49dd8ce2020-11-03 19:32:34 -080085 explicit NuvotonPciBridge(const PciAccess* pciAccess,
Willy Tu8a9de242020-10-30 11:00:21 -070086 bool skipBridgeDisable = false) :
Willy Tu49dd8ce2020-11-03 19:32:34 -080087 PciAccessBridge(&match, bar, dataOffset, dataLength, pciAccess),
Willy Tu8a9de242020-10-30 11:00:21 -070088 skipBridgeDisable(skipBridgeDisable)
Benjamin Fairc1a30c02020-06-09 11:46:34 -070089 {
90 enableBridge();
91 }
Benjamin Faire5aafa52020-06-05 21:04:24 -070092
93 ~NuvotonPciBridge()
Benjamin Fairc1a30c02020-06-09 11:46:34 -070094 {
Willy Tu8a9de242020-10-30 11:00:21 -070095 if (!skipBridgeDisable)
96 disableBridge();
Benjamin Fairc1a30c02020-06-09 11:46:34 -070097 }
Benjamin Faire5aafa52020-06-05 21:04:24 -070098
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
Benjamin Fairc04c2c52020-06-05 09:14:44 -0700104 {
Benjamin Faire5aafa52020-06-05 21:04:24 -0700105 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;
Benjamin Fairc1a30c02020-06-09 11:46:34 -0700113
114 void enableBridge();
115 void disableBridge();
Willy Tu8a9de242020-10-30 11:00:21 -0700116
117 bool skipBridgeDisable;
Benjamin Faire5aafa52020-06-05 21:04:24 -0700118};
119
120class AspeedPciBridge : public PciAccessBridge
121{
122 public:
Willy Tu49dd8ce2020-11-03 19:32:34 -0800123 explicit AspeedPciBridge(const PciAccess* pciAccess,
Willy Tu8a9de242020-10-30 11:00:21 -0700124 bool skipBridgeDisable = false) :
Willy Tu49dd8ce2020-11-03 19:32:34 -0800125 PciAccessBridge(&match, bar, dataOffset, dataLength, pciAccess),
Willy Tu8a9de242020-10-30 11:00:21 -0700126 skipBridgeDisable(skipBridgeDisable)
Benjamin Faire5aafa52020-06-05 21:04:24 -0700127 {
128 enableBridge();
Benjamin Fairc04c2c52020-06-05 09:14:44 -0700129 }
130
Benjamin Faire5aafa52020-06-05 21:04:24 -0700131 ~AspeedPciBridge()
Benjamin Fairc04c2c52020-06-05 09:14:44 -0700132 {
Willy Tu8a9de242020-10-30 11:00:21 -0700133 if (!skipBridgeDisable)
134 disableBridge();
Benjamin Fairc04c2c52020-06-05 09:14:44 -0700135 }
Benjamin Faire5aafa52020-06-05 21:04:24 -0700136
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();
Willy Tu8a9de242020-10-30 11:00:21 -0700157
158 bool skipBridgeDisable;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700159};
160
161} // namespace host_tool