bmc: implement pci support for Nuvoton

Tested: Verified this works for a Nuvoton BMC.

enable-nuvoton-p2a-mbox is for PCI-MailBox
enable-nuvoton-p2a-vga  is for PCI-VGA

Signed-off-by: Medad CChien <ctcchien@nuvoton.com>
Change-Id: I15bc9cc528c285fbd015f9f2fad0913163d92db8
diff --git a/bmc/Makefile.am b/bmc/Makefile.am
index 52cab6e..0f9b2fb 100644
--- a/bmc/Makefile.am
+++ b/bmc/Makefile.am
@@ -59,6 +59,14 @@
 libfirmwareblob_common_la_SOURCES += pci_handler.cpp
 endif
 
+if ENABLE_NUVOTON_P2A_VGA
+libfirmwareblob_common_la_SOURCES += pci_nuvoton_handler.cpp
+endif
+
+if ENABLE_NUVOTON_P2A_MBOX
+libfirmwareblob_common_la_SOURCES += pci_nuvoton_handler.cpp
+endif
+
 libfirmwareblob_common_la_CXXFLAGS = \
 	-I$(top_srcdir) \
 	$(SDBUSPLUS_CFLAGS) \
diff --git a/bmc/main.cpp b/bmc/main.cpp
index ab7ece8..bc25ff0 100644
--- a/bmc/main.cpp
+++ b/bmc/main.cpp
@@ -43,8 +43,14 @@
 namespace
 {
 
+#ifdef NUVOTON_P2A_MBOX
+static constexpr std::size_t memoryRegionSize = 16 * 1024UL;
+#elif defined NUVOTON_P2A_VGA
+static constexpr std::size_t memoryRegionSize = 4 * 1024 * 1024UL;
+#else
 /* The maximum external buffer size we expect is 64KB. */
 static constexpr std::size_t memoryRegionSize = 64 * 1024UL;
+#endif
 
 static constexpr const char* jsonConfigurationPath =
     "/usr/share/phosphor-ipmi-flash/";
@@ -62,11 +68,7 @@
 #endif
 
 #ifdef ENABLE_PCI_BRIDGE
-#if defined(ASPEED_P2A)
 PciDataHandler pciDataHandler(MAPPED_ADDRESS, memoryRegionSize);
-#else
-#error "You must specify a hardware implementation."
-#endif
 #endif
 
 std::vector<DataHandlerPack> supportedTransports = {
diff --git a/bmc/pci_nuvoton_handler.cpp b/bmc/pci_nuvoton_handler.cpp
new file mode 100644
index 0000000..0a3f62e
--- /dev/null
+++ b/bmc/pci_nuvoton_handler.cpp
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "data.hpp"
+#include "pci_handler.hpp"
+
+#include <fcntl.h>
+
+#include <cstdint>
+#include <cstring>
+#include <string>
+#include <vector>
+
+namespace ipmi_flash
+{
+
+bool PciDataHandler::open()
+{
+    static constexpr auto devmem = "/dev/mem";
+
+    mappedFd = sys->open(devmem, O_RDWR | O_SYNC);
+    if (mappedFd == -1)
+    {
+        std::fprintf(stderr, "PciDataHandler::Unable to open /dev/mem");
+        return false;
+    }
+
+    mapped = reinterpret_cast<uint8_t*>(sys->mmap(
+        0, memoryRegionSize, PROT_READ, MAP_SHARED, mappedFd, regionAddress));
+    if (mapped == MAP_FAILED)
+    {
+        sys->close(mappedFd);
+        mappedFd = -1;
+        mapped = nullptr;
+
+        std::fprintf(stderr, "PciDataHandler::Unable to map region");
+        return false;
+    }
+
+    return true;
+}
+
+bool PciDataHandler::close()
+{
+    /* TODO: Turn off the P2A bridge and region to disable host-side access.
+     */
+    if (mapped)
+    {
+        sys->munmap(mapped, memoryRegionSize);
+        mapped = nullptr;
+    }
+
+    if (mappedFd != -1)
+    {
+        sys->close(mappedFd);
+        mappedFd = -1;
+    }
+
+    return true;
+}
+
+std::vector<std::uint8_t> PciDataHandler::copyFrom(std::uint32_t length)
+{
+    std::vector<std::uint8_t> results(length);
+    std::memcpy(results.data(), mapped, length);
+
+    return results;
+}
+
+bool PciDataHandler::writeMeta(const std::vector<std::uint8_t>& configuration)
+{
+    /* PCI handler doesn't require configuration write, only read. */
+    return false;
+}
+
+std::vector<std::uint8_t> PciDataHandler::readMeta()
+{
+    /* PCI handler does require returning a configuration from read. */
+    struct PciConfigResponse reply;
+    reply.address = regionAddress;
+
+    std::vector<std::uint8_t> bytes;
+    bytes.resize(sizeof(reply));
+    std::memcpy(bytes.data(), &reply, sizeof(reply));
+
+    return bytes;
+}
+
+} // namespace ipmi_flash
diff --git a/configure.ac b/configure.ac
index f38c5f0..bec5108 100644
--- a/configure.ac
+++ b/configure.ac
@@ -251,6 +251,26 @@
     AX_APPEND_COMPILE_FLAGS([-DENABLE_PCI_BRIDGE], [CXXFLAGS])
 ])
 
+AC_ARG_ENABLE([nuvoton-p2a-vga],
+    AS_HELP_STRING([--enable-nuvoton-p2a-vga],
+                   [Enable external transfers using Nuvoton PCI-to-AHB via VGA]))
+
+AM_CONDITIONAL(ENABLE_NUVOTON_P2A_VGA, [test "x$enable_nuvoton_p2a_vga" = "xyes"])
+AS_IF([test "x$enable_nuvoton_p2a_vga" = "xyes"], [
+    AX_APPEND_COMPILE_FLAGS([-DNUVOTON_P2A_VGA], [CXXFLAGS])
+    AX_APPEND_COMPILE_FLAGS([-DENABLE_PCI_BRIDGE], [CXXFLAGS])
+])
+
+AC_ARG_ENABLE([nuvoton-p2a-mbox],
+    AS_HELP_STRING([--enable-nuvoton-p2a-mbox],
+                   [Enable external transfers using Nuvoton PCI-to-AHB via MBOX]))
+
+AM_CONDITIONAL(ENABLE_NUVOTON_P2A_MBOX, [test "x$enable_nuvoton_p2a_mbox" = "xyes"])
+AS_IF([test "x$enable_nuvoton_p2a_mbox" = "xyes"], [
+    AX_APPEND_COMPILE_FLAGS([-DNUVOTON_P2A_MBOX], [CXXFLAGS])
+    AX_APPEND_COMPILE_FLAGS([-DENABLE_PCI_BRIDGE], [CXXFLAGS])
+])
+
 AS_IF([test "x$enable_aspeed_p2a" = "xyes"], [
     AS_IF([test "x$enable_nuvoton_lpc" = "xyes"], [
         AS_IF([test "x$enable_tests" != "xyes"], [
@@ -259,6 +279,30 @@
     ])
 ])
 
+AS_IF([test "x$enable_aspeed_p2a" = "xyes"], [
+    AS_IF([test "x$enable_nuvoton_p2a_vga" = "xyes" -o "x$enable_nuvoton_p2a_mbox" = "xyes"], [
+        AS_IF([test "x$enable_tests" != "xyes"], [
+            AC_MSG_ERROR([Invalid configuration enabling both ASPEED and Nuvoton.])
+        ])
+    ])
+])
+
+AS_IF([test "x$enable_nuvoton_lpc" = "xyes"], [
+    AS_IF([test "x$enable_nuvoton_p2a_vga" = "xyes" -o "x$enable_nuvoton_p2a_mbox" = "xyes"], [
+        AS_IF([test "x$enable_tests" != "xyes"], [
+            AC_MSG_ERROR([Invalid configuration enabling both PCI and LPC of Nuvoton.])
+        ])
+    ])
+])
+
+AS_IF([test "x$enable_nuvoton_p2a_vga" = "xyes"], [
+    AS_IF([test "x$enable_nuvoton_p2a_mbox" = "xyes"], [
+        AS_IF([test "x$enable_tests" != "xyes"], [
+            AC_MSG_ERROR([Invalid configuration enabling both PCI-VGA and PCI-MBOX of Nuvoton.])
+        ])
+    ])
+])
+
 AC_ARG_VAR(
     STATIC_HANDLER_STAGED_NAME,
     [The file to use for staging the firmware update.]