blob: 436c63eab4bf5813a4fdbc1d9c37b90f53082b08 [file] [log] [blame]
Patrick Venture22e38752018-11-21 08:52:49 -08001/*
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 Venture1cde5f92018-11-07 08:26:47 -080017#include "pci_handler.hpp"
18
Patrick Venture4289e712019-04-30 17:08:50 -070019#include <fcntl.h>
20#include <linux/aspeed-p2a-ctrl.h>
21
Patrick Venture1cde5f92018-11-07 08:26:47 -080022#include <cstdint>
Patrick Venturea8a99ae2018-11-09 11:14:58 -080023#include <cstring>
Patrick Venturebc40c612019-04-30 16:48:19 -070024#include <string>
Patrick Venture1cde5f92018-11-07 08:26:47 -080025#include <vector>
26
Patrick Venture1d5a31c2019-05-20 11:38:22 -070027namespace ipmi_flash
Patrick Venture1cde5f92018-11-07 08:26:47 -080028{
29
Patrick Venturebc40c612019-04-30 16:48:19 -070030const std::string PciDataHandler::p2aControlPath = "/dev/aspeed-p2a-ctrl";
31
Patrick Venture0d2a8132018-11-09 11:34:21 -080032bool PciDataHandler::open()
33{
Patrick Venture4289e712019-04-30 17:08:50 -070034 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 Venture0fbabf22018-11-09 11:54:12 -080064 */
Patrick Venture4289e712019-04-30 17:08:50 -070065 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;
Patrick Ventureef4993e2019-06-24 09:56:37 -070073 mapped = nullptr;
Patrick Venture4289e712019-04-30 17:08:50 -070074
75 return false;
76 }
77
78 return true;
Patrick Venture0fbabf22018-11-09 11:54:12 -080079}
80
81bool PciDataHandler::close()
82{
83 /* TODO: Turn off the P2A bridge and region to disable host-side access.
Patrick Venture0d2a8132018-11-09 11:34:21 -080084 */
Patrick Venture4289e712019-04-30 17:08:50 -070085 if (mapped)
86 {
87 sys->munmap(mapped, memoryRegionSize);
88 mapped = nullptr;
89 }
90
91 if (mappedFd != -1)
92 {
93 sys->close(mappedFd);
94 mappedFd = -1;
95 }
96
97 return true;
Patrick Venture0d2a8132018-11-09 11:34:21 -080098}
99
Patrick Venture1cde5f92018-11-07 08:26:47 -0800100std::vector<std::uint8_t> PciDataHandler::copyFrom(std::uint32_t length)
101{
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700102 std::vector<std::uint8_t> results(length);
103 std::memcpy(results.data(), mapped, length);
104
105 return results;
Patrick Venture1cde5f92018-11-07 08:26:47 -0800106}
107
Patrick Venture74304642019-01-17 09:31:04 -0800108bool PciDataHandler::writeMeta(const std::vector<std::uint8_t>& configuration)
Patrick Venture8c535332018-11-08 15:58:00 -0800109{
110 /* PCI handler doesn't require configuration write, only read. */
111 return false;
112}
113
Patrick Venture74304642019-01-17 09:31:04 -0800114std::vector<std::uint8_t> PciDataHandler::readMeta()
Patrick Venture8c535332018-11-08 15:58:00 -0800115{
116 /* PCI handler does require returning a configuration from read. */
117 struct PciConfigResponse reply;
Patrick Ventureb0c84d02018-11-09 12:00:31 -0800118 reply.address = regionAddress;
Patrick Venture8c535332018-11-08 15:58:00 -0800119
120 std::vector<std::uint8_t> bytes;
121 bytes.resize(sizeof(reply));
Patrick Venturea8a99ae2018-11-09 11:14:58 -0800122 std::memcpy(bytes.data(), &reply, sizeof(reply));
Patrick Venture8c535332018-11-08 15:58:00 -0800123
124 return bytes;
125}
126
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700127} // namespace ipmi_flash