Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 1 | /* |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame^] | 2 | * Copyright 2020 Google Inc. |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 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 | #include "pci.hpp" |
| 18 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame^] | 19 | #include "tool_errors.hpp" |
| 20 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 21 | extern "C" |
| 22 | { |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame] | 23 | #include <pciaccess.h> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 24 | } // extern "C" |
| 25 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame^] | 26 | #include <fmt/format.h> |
| 27 | |
| 28 | #include <stdplus/handle/managed.hpp> |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame] | 29 | |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 30 | #include <cstring> |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame^] | 31 | #include <system_error> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 32 | |
| 33 | namespace host_tool |
| 34 | { |
| 35 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame^] | 36 | namespace |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 37 | { |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 38 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame^] | 39 | /** @brief RAII wrapper and its destructor for creating a pci_device_iterator */ |
| 40 | static void closeIt(struct pci_device_iterator*&& it, |
| 41 | const PciAccess* const& pci) |
| 42 | { |
| 43 | pci->pci_iterator_destroy(it); |
| 44 | } |
| 45 | using It = stdplus::Managed<struct pci_device_iterator*, |
| 46 | const PciAccess* const>::Handle<closeIt>; |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 50 | PciAccessBridge::PciAccessBridge(const struct pci_id_match* match, int bar, |
| 51 | std::size_t dataOffset, std::size_t dataLength, |
| 52 | const PciAccess* pci) : |
| 53 | dataOffset(dataOffset), |
| 54 | dataLength(dataLength), pci(pci) |
| 55 | { |
| 56 | It it(pci->pci_id_match_iterator_create(match), pci); |
| 57 | |
| 58 | while ((dev = pci->pci_device_next(*it))) |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 59 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame^] | 60 | int ret = pci->pci_device_probe(dev); |
| 61 | if (ret) |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 62 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame^] | 63 | throw std::system_error(ret, std::generic_category(), |
| 64 | "Error probing PCI device"); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 65 | } |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame] | 66 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame^] | 67 | /* Verify it's a memory-based bar. */ |
| 68 | if (!dev->regions[bar].is_IO) |
| 69 | break; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame^] | 72 | if (!dev) |
| 73 | { |
| 74 | throw NotFoundException(fmt::format( |
| 75 | "PCI device {:#04x}:{:#04x}", match->vendor_id, match->device_id)); |
| 76 | } |
| 77 | |
| 78 | std::fprintf(stderr, "Find [0x%x 0x%x] \n", match->vendor_id, |
| 79 | match->device_id); |
| 80 | std::fprintf(stderr, "bar%d[0x%x] \n", bar, |
| 81 | static_cast<unsigned int>(dev->regions[bar].base_addr)); |
| 82 | |
| 83 | size = dev->regions[bar].size; |
| 84 | int ret = pci->pci_device_map_range( |
| 85 | dev, dev->regions[bar].base_addr, dev->regions[bar].size, |
| 86 | PCI_DEV_MAP_FLAG_WRITABLE, reinterpret_cast<void**>(&addr)); |
| 87 | if (ret) |
| 88 | { |
| 89 | throw std::system_error(ret, std::generic_category(), |
| 90 | "Error mapping PCI device memory"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | PciAccessBridge::~PciAccessBridge() |
| 95 | { |
| 96 | int ret = pci->pci_device_unmap_range(dev, addr, size); |
| 97 | |
| 98 | if (ret) |
| 99 | { |
| 100 | std::fprintf(stderr, "Error while unmapping PCI device memory: %s\n", |
| 101 | std::strerror(ret)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void PciAccessBridge::write(const stdplus::span<const std::uint8_t> data) |
| 106 | { |
| 107 | if (data.size() > dataLength) |
| 108 | { |
| 109 | throw ToolException( |
| 110 | fmt::format("Write of {} bytes exceeds maximum of {}", data.size(), |
| 111 | dataLength)); |
| 112 | } |
| 113 | |
| 114 | std::memcpy(addr + dataOffset, data.data(), data.size()); |
| 115 | } |
| 116 | |
| 117 | void AspeedPciBridge::enableBridge() |
| 118 | { |
| 119 | /* We sent the open command before this, so the window should be open and |
| 120 | * the bridge enabled on the BMC. |
| 121 | */ |
| 122 | std::uint32_t value; |
| 123 | std::memcpy(&value, addr + config, sizeof(value)); |
| 124 | |
| 125 | if (0 == (value & bridgeEnabled)) |
| 126 | { |
| 127 | std::fprintf(stderr, "Bridge not enabled - Enabling from host\n"); |
| 128 | |
| 129 | value |= bridgeEnabled; |
| 130 | std::memcpy(addr + config, &value, sizeof(value)); |
| 131 | } |
| 132 | |
| 133 | std::fprintf(stderr, "The bridge is enabled!\n"); |
| 134 | } |
| 135 | |
| 136 | void AspeedPciBridge::disableBridge() |
| 137 | { |
| 138 | /* addr is valid if the constructor completed */ |
| 139 | |
| 140 | /* Read current value, and just blindly unset the bit. */ |
| 141 | std::uint32_t value; |
| 142 | std::memcpy(&value, addr + config, sizeof(value)); |
| 143 | |
| 144 | value &= ~bridgeEnabled; |
| 145 | std::memcpy(addr + config, &value, sizeof(value)); |
| 146 | } |
| 147 | |
| 148 | void AspeedPciBridge::configure(const ipmi_flash::PciConfigResponse& configResp) |
| 149 | { |
| 150 | std::fprintf(stderr, "Received address: 0x%x\n", configResp.address); |
| 151 | |
| 152 | /* Configure the mmio to point there. */ |
| 153 | std::memcpy(addr + bridge, &configResp.address, sizeof(configResp.address)); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } // namespace host_tool |