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