Patrick Venture | 22e3875 | 2018-11-21 08:52:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 17 | #include "pci_handler.hpp" |
| 18 | |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 19 | #include "data.hpp" |
| 20 | |
Patrick Venture | 4289e71 | 2019-04-30 17:08:50 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <linux/aspeed-p2a-ctrl.h> |
| 23 | |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 24 | #include <cstdint> |
Patrick Venture | a8a99ae | 2018-11-09 11:14:58 -0800 | [diff] [blame] | 25 | #include <cstring> |
Patrick Venture | bc40c61 | 2019-04-30 16:48:19 -0700 | [diff] [blame] | 26 | #include <string> |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 29 | namespace ipmi_flash |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 30 | { |
| 31 | |
Patrick Venture | bc40c61 | 2019-04-30 16:48:19 -0700 | [diff] [blame] | 32 | const std::string PciDataHandler::p2aControlPath = "/dev/aspeed-p2a-ctrl"; |
| 33 | |
Patrick Venture | 0d2a813 | 2018-11-09 11:34:21 -0800 | [diff] [blame] | 34 | bool PciDataHandler::open() |
| 35 | { |
Patrick Venture | 4289e71 | 2019-04-30 17:08:50 -0700 | [diff] [blame] | 36 | mappedFd = sys->open(p2aControlPath.c_str(), O_RDWR); |
| 37 | if (mappedFd == -1) |
| 38 | { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | struct aspeed_p2a_ctrl_mapping map; |
| 43 | map.addr = regionAddress; |
| 44 | map.length = memoryRegionSize; |
| 45 | map.flags = ASPEED_P2A_CTRL_READWRITE; |
| 46 | |
| 47 | if (sys->ioctl(mappedFd, ASPEED_P2A_CTRL_IOCTL_SET_WINDOW, &map)) |
| 48 | { |
| 49 | sys->close(mappedFd); |
| 50 | mappedFd = -1; |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | if (sys->ioctl(mappedFd, ASPEED_P2A_CTRL_IOCTL_GET_MEMORY_CONFIG, &map)) |
| 56 | { |
| 57 | sys->close(mappedFd); |
| 58 | mappedFd = -1; |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | /* The length of the region reserved is reported, and it's important |
| 64 | * because the offset + memory region could be beyond that reserved |
| 65 | * region. |
Patrick Venture | 0fbabf2 | 2018-11-09 11:54:12 -0800 | [diff] [blame] | 66 | */ |
Patrick Venture | 4289e71 | 2019-04-30 17:08:50 -0700 | [diff] [blame] | 67 | std::uint64_t offset = regionAddress - map.addr; |
| 68 | |
| 69 | mapped = reinterpret_cast<std::uint8_t*>( |
| 70 | mmap(0, memoryRegionSize, PROT_READ, MAP_SHARED, mappedFd, offset)); |
| 71 | if (mapped == MAP_FAILED) |
| 72 | { |
| 73 | sys->close(mappedFd); |
| 74 | mappedFd = -1; |
Patrick Venture | ef4993e | 2019-06-24 09:56:37 -0700 | [diff] [blame] | 75 | mapped = nullptr; |
Patrick Venture | 4289e71 | 2019-04-30 17:08:50 -0700 | [diff] [blame] | 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | return true; |
Patrick Venture | 0fbabf2 | 2018-11-09 11:54:12 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool PciDataHandler::close() |
| 84 | { |
| 85 | /* TODO: Turn off the P2A bridge and region to disable host-side access. |
Patrick Venture | 0d2a813 | 2018-11-09 11:34:21 -0800 | [diff] [blame] | 86 | */ |
Patrick Venture | 4289e71 | 2019-04-30 17:08:50 -0700 | [diff] [blame] | 87 | if (mapped) |
| 88 | { |
| 89 | sys->munmap(mapped, memoryRegionSize); |
| 90 | mapped = nullptr; |
| 91 | } |
| 92 | |
| 93 | if (mappedFd != -1) |
| 94 | { |
| 95 | sys->close(mappedFd); |
| 96 | mappedFd = -1; |
| 97 | } |
| 98 | |
| 99 | return true; |
Patrick Venture | 0d2a813 | 2018-11-09 11:34:21 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 102 | std::vector<std::uint8_t> PciDataHandler::copyFrom(std::uint32_t length) |
| 103 | { |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 104 | std::vector<std::uint8_t> results(length); |
| 105 | std::memcpy(results.data(), mapped, length); |
| 106 | |
| 107 | return results; |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Patrick Venture | 7430464 | 2019-01-17 09:31:04 -0800 | [diff] [blame] | 110 | bool PciDataHandler::writeMeta(const std::vector<std::uint8_t>& configuration) |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 111 | { |
| 112 | /* PCI handler doesn't require configuration write, only read. */ |
| 113 | return false; |
| 114 | } |
| 115 | |
Patrick Venture | 7430464 | 2019-01-17 09:31:04 -0800 | [diff] [blame] | 116 | std::vector<std::uint8_t> PciDataHandler::readMeta() |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 117 | { |
| 118 | /* PCI handler does require returning a configuration from read. */ |
| 119 | struct PciConfigResponse reply; |
Patrick Venture | b0c84d0 | 2018-11-09 12:00:31 -0800 | [diff] [blame] | 120 | reply.address = regionAddress; |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 121 | |
| 122 | std::vector<std::uint8_t> bytes; |
| 123 | bytes.resize(sizeof(reply)); |
Patrick Venture | a8a99ae | 2018-11-09 11:14:58 -0800 | [diff] [blame] | 124 | std::memcpy(bytes.data(), &reply, sizeof(reply)); |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 125 | |
| 126 | return bytes; |
| 127 | } |
| 128 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 129 | } // namespace ipmi_flash |