xref: /openbmc/phosphor-ipmi-flash/tools/p2a.cpp (revision c73dce91)
1b5bf0fc2SPatrick Venture /*
2b5bf0fc2SPatrick Venture  * Copyright 2019 Google Inc.
3b5bf0fc2SPatrick Venture  *
4b5bf0fc2SPatrick Venture  * Licensed under the Apache License, Version 2.0 (the "License");
5b5bf0fc2SPatrick Venture  * you may not use this file except in compliance with the License.
6b5bf0fc2SPatrick Venture  * You may obtain a copy of the License at
7b5bf0fc2SPatrick Venture  *
8b5bf0fc2SPatrick Venture  *     http://www.apache.org/licenses/LICENSE-2.0
9b5bf0fc2SPatrick Venture  *
10b5bf0fc2SPatrick Venture  * Unless required by applicable law or agreed to in writing, software
11b5bf0fc2SPatrick Venture  * distributed under the License is distributed on an "AS IS" BASIS,
12b5bf0fc2SPatrick Venture  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b5bf0fc2SPatrick Venture  * See the License for the specific language governing permissions and
14b5bf0fc2SPatrick Venture  * limitations under the License.
15b5bf0fc2SPatrick Venture  */
16b5bf0fc2SPatrick Venture 
17b5bf0fc2SPatrick Venture #include "p2a.hpp"
18b5bf0fc2SPatrick Venture 
19b5bf0fc2SPatrick Venture #include "pci.hpp"
20*c73dce91SPatrick Venture #include "pci_handler.hpp"
21*c73dce91SPatrick Venture 
22*c73dce91SPatrick Venture #include <cstring>
23b5bf0fc2SPatrick Venture 
24b5bf0fc2SPatrick Venture namespace host_tool
25b5bf0fc2SPatrick Venture {
26b5bf0fc2SPatrick Venture 
27b5bf0fc2SPatrick Venture bool P2aDataHandler::sendContents(const std::string& input,
28b5bf0fc2SPatrick Venture                                   std::uint16_t session)
29b5bf0fc2SPatrick Venture {
3024141611SPatrick Venture     PciDevice result;
31b5bf0fc2SPatrick Venture     PciUtilImpl pci;
32b5bf0fc2SPatrick Venture     PciFilter filter;
3336bb4670SPatrick Venture     bool found = false;
34b5bf0fc2SPatrick Venture 
35b5bf0fc2SPatrick Venture     filter.vid = aspeedVendorId;
36b5bf0fc2SPatrick Venture     filter.did = aspeedDeviceId;
37b5bf0fc2SPatrick Venture 
3824141611SPatrick Venture     /* Find the ASPEED PCI device entry we want. */
39b5bf0fc2SPatrick Venture     auto output = pci.getPciDevices(filter);
40b5bf0fc2SPatrick Venture     for (const auto& d : output)
41b5bf0fc2SPatrick Venture     {
4224141611SPatrick Venture         std::fprintf(stderr, "[0x%x 0x%x] ", d.vid, d.did);
4324141611SPatrick Venture 
4424141611SPatrick Venture         /* Verify it's a memory-based bar -- we want bar1. */
4524141611SPatrick Venture         pciaddr_t bar1 = d.bars[1];
4624141611SPatrick Venture         if ((bar1 & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
4724141611SPatrick Venture         {
4824141611SPatrick Venture             /* We want it to not be IO-based access. */
4924141611SPatrick Venture             continue;
50b5bf0fc2SPatrick Venture         }
51b5bf0fc2SPatrick Venture 
5224141611SPatrick Venture         result = d;
5336bb4670SPatrick Venture         found = true;
5436bb4670SPatrick Venture         break;
5524141611SPatrick Venture     }
5636bb4670SPatrick Venture 
5736bb4670SPatrick Venture     if (!found)
5836bb4670SPatrick Venture     {
5936bb4670SPatrick Venture         return false;
6036bb4670SPatrick Venture     }
6136bb4670SPatrick Venture 
6224141611SPatrick Venture     std::fprintf(stderr, "\n");
6324141611SPatrick Venture 
6424141611SPatrick Venture     /* We sent the open command before this, so the window should be open and
6524141611SPatrick Venture      * the bridge enabled.
6624141611SPatrick Venture      */
6724141611SPatrick Venture     std::uint32_t value;
6824141611SPatrick Venture     if (!io->read(result.bars[1] | aspeedP2aConfig, sizeof(value), &value))
6924141611SPatrick Venture     {
7024141611SPatrick Venture         if (0 == (value & p2ABridgeEnabled))
7124141611SPatrick Venture         {
7224141611SPatrick Venture             std::fprintf(stderr, "Bridge not enabled.\n");
7324141611SPatrick Venture             return false;
7424141611SPatrick Venture         }
7524141611SPatrick Venture     }
7624141611SPatrick Venture 
7724141611SPatrick Venture     std::fprintf(stderr, "The bridge is enabled!\n");
7824141611SPatrick Venture 
7924141611SPatrick Venture     /* Read the configuration via blobs metadata (stat). */
80*c73dce91SPatrick Venture     ipmiblob::StatResponse stat = blob->getStat(session);
81*c73dce91SPatrick Venture     if (stat.metadata.size() != sizeof(blobs::PciConfigResponse))
82*c73dce91SPatrick Venture     {
83*c73dce91SPatrick Venture         std::fprintf(stderr, "Didn't receive expected size of metadata for "
84*c73dce91SPatrick Venture                              "PCI Configuration response\n");
85*c73dce91SPatrick Venture         return false;
86*c73dce91SPatrick Venture     }
87*c73dce91SPatrick Venture 
88*c73dce91SPatrick Venture     blobs::PciConfigResponse pciResp;
89*c73dce91SPatrick Venture     std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp));
90*c73dce91SPatrick Venture     std::fprintf(stderr, "Received address: 0x%x\n", pciResp.address);
9124141611SPatrick Venture 
9224141611SPatrick Venture #if 0
9324141611SPatrick Venture     /* Configure the mmio to point there. */
9424141611SPatrick Venture     if (!io->IoWrite(bar | kAspeedP2aBridge, sizeof(phys), &phys)) {
9524141611SPatrick Venture         // Failed to set it up, so fall back.
9624141611SPatrick Venture         std::fprintf(stderr, "Failed to update the bridge address\n");
9724141611SPatrick Venture         return false;
9824141611SPatrick Venture     }
9924141611SPatrick Venture #endif
10024141611SPatrick Venture 
101b5bf0fc2SPatrick Venture     return false;
102b5bf0fc2SPatrick Venture }
103b5bf0fc2SPatrick Venture 
104b5bf0fc2SPatrick Venture } // namespace host_tool
105