2023-09-08 11:35:18 +02:00
|
|
|
/* AXI Stream interconnect related helper functions
|
2017-12-21 21:23:54 +01:00
|
|
|
*
|
|
|
|
* These functions present a simpler interface to Xilinx' AXI Stream switch driver (XAxis_Switch_*)
|
|
|
|
*
|
2023-01-07 17:20:15 +01:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* Author: Daniel Krebs <github@daniel-krebs.net>
|
2023-01-07 17:32:48 +01:00
|
|
|
* SPDX-FileCopyrightText: 2017 Steffen Vogel <post@steffenvogel.de>
|
2023-01-07 17:20:15 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-09-08 11:35:18 +02:00
|
|
|
*/
|
2017-12-21 21:23:54 +01:00
|
|
|
|
2018-01-23 14:47:44 +01:00
|
|
|
#include <jansson.h>
|
2017-12-21 21:23:54 +01:00
|
|
|
#include <xilinx/xaxis_switch.h>
|
|
|
|
|
2022-11-11 06:36:20 -05:00
|
|
|
#include <villas/exceptions.hpp>
|
2018-06-04 12:17:23 +02:00
|
|
|
#include <villas/fpga/ips/switch.hpp>
|
2017-12-21 21:23:54 +01:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace fpga {
|
|
|
|
namespace ip {
|
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
bool AxiStreamSwitch::init() {
|
|
|
|
if (XAxisScr_CfgInitialize(&xSwitch, &xConfig, getBaseAddr(registerMemory)) !=
|
|
|
|
XST_SUCCESS) {
|
|
|
|
logger->error("Cannot initialize switch");
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-21 21:23:54 +01:00
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
// Disable all masters
|
|
|
|
XAxisScr_RegUpdateDisable(&xSwitch);
|
|
|
|
XAxisScr_MiPortDisableAll(&xSwitch);
|
|
|
|
XAxisScr_RegUpdateEnable(&xSwitch);
|
2017-12-21 21:23:54 +01:00
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
for (auto &[masterName, masterPort] : portsMaster) {
|
2018-06-04 14:00:15 +02:00
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
// Initialize internal mapping
|
|
|
|
portMapping[masterName] = PORT_DISABLED;
|
2018-06-04 14:00:15 +02:00
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
// Each slave port may be internally routed to a master port
|
|
|
|
for (auto &[slaveName, slavePort] : portsSlave) {
|
|
|
|
(void)slaveName;
|
2018-06-04 14:00:15 +02:00
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
streamGraph.addDefaultEdge(slavePort->getIdentifier(),
|
|
|
|
masterPort->getIdentifier());
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 21:23:54 +01:00
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
return true;
|
2017-12-21 21:23:54 +01:00
|
|
|
}
|
|
|
|
|
2024-02-07 15:08:08 +01:00
|
|
|
void AxiStreamSwitch::printConfig() const {
|
|
|
|
u32 MiPortAddr;
|
|
|
|
u32 RegValue;
|
|
|
|
u8 Enable;
|
|
|
|
logger->info(
|
|
|
|
"Switch configuration: {} Master Interfaces, {} Slave Interfaces",
|
|
|
|
xConfig.MaxNumMI, xConfig.MaxNumSI);
|
|
|
|
|
|
|
|
for (int i = 0; i < xConfig.MaxNumMI; i++) {
|
|
|
|
/* Calculate MI port address to be enabled */
|
|
|
|
MiPortAddr = XAXIS_SCR_MI_MUX_START_OFFSET + 4 * i;
|
|
|
|
|
|
|
|
/* Read MI port data */
|
|
|
|
RegValue = XAxisScr_ReadReg(xSwitch.Config.BaseAddress, MiPortAddr);
|
|
|
|
|
|
|
|
/* Fetch enable bit */
|
|
|
|
Enable = RegValue >> XAXIS_SCR_MI_X_DISABLE_SHIFT;
|
|
|
|
|
|
|
|
/* Fetch SI value */
|
|
|
|
RegValue &= XAXIS_SCR_MI_X_MUX_MASK;
|
|
|
|
|
|
|
|
logger->info("Master Interface {}: {} (enabled: {})", i, RegValue, Enable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 06:38:00 -05:00
|
|
|
bool AxiStreamSwitch::connectInternal(const std::string &portSlave,
|
2024-02-29 19:34:27 +01:00
|
|
|
const std::string &portMaster) {
|
|
|
|
// Check if slave port exists
|
|
|
|
try {
|
|
|
|
getSlavePort(portSlave);
|
|
|
|
} catch (const std::out_of_range &) {
|
|
|
|
logger->error("Switch doesn't have a slave port named '{}'", portSlave);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if master port exists
|
|
|
|
try {
|
|
|
|
getMasterPort(portMaster);
|
|
|
|
} catch (const std::out_of_range &) {
|
|
|
|
logger->error("Switch doesn't have a master port named '{}'", portMaster);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (portSlave.substr(0, 1) != "S" or portMaster.substr(0, 1) != "M") {
|
|
|
|
logger->error("sanity check failed: master {} slave {}", portMaster,
|
|
|
|
portSlave);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (portMapping[portMaster] == portSlave) {
|
|
|
|
logger->debug("Ports already connected (slave {} to master {}", portSlave,
|
|
|
|
portMaster);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto [master, slave] : portMapping) {
|
|
|
|
if (slave == portSlave) {
|
|
|
|
logger->warn("Slave {} has already been connected to master {}. "
|
|
|
|
"Disabling master {}.",
|
|
|
|
slave, master, master);
|
|
|
|
|
|
|
|
XAxisScr_RegUpdateDisable(&xSwitch);
|
|
|
|
XAxisScr_MiPortDisable(&xSwitch, portNameToNum(master));
|
|
|
|
XAxisScr_RegUpdateEnable(&xSwitch);
|
|
|
|
|
|
|
|
portMapping[master] = PORT_DISABLED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconfigure switch
|
|
|
|
XAxisScr_RegUpdateDisable(&xSwitch);
|
|
|
|
XAxisScr_MiPortEnable(&xSwitch, portNameToNum(portMaster),
|
|
|
|
portNameToNum(portSlave));
|
|
|
|
XAxisScr_RegUpdateEnable(&xSwitch);
|
|
|
|
|
|
|
|
portMapping[portMaster] = portSlave;
|
|
|
|
|
|
|
|
logger->debug("Connect slave {} to master {}", portSlave, portMaster);
|
|
|
|
|
|
|
|
return true;
|
2017-12-21 21:23:54 +01:00
|
|
|
}
|
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
int AxiStreamSwitch::portNameToNum(const std::string &portName) {
|
|
|
|
const std::string number = portName.substr(1, 2);
|
|
|
|
return std::stoi(number);
|
2017-12-21 21:23:54 +01:00
|
|
|
}
|
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
void AxiStreamSwitchFactory::parse(Core &ip, json_t *cfg) {
|
|
|
|
NodeFactory::parse(ip, cfg);
|
2018-01-23 14:47:44 +01:00
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
auto logger = getLogger();
|
2018-01-23 14:47:44 +01:00
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
auto &axiSwitch = dynamic_cast<AxiStreamSwitch &>(ip);
|
2018-01-23 14:47:44 +01:00
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
int num_si, num_mi;
|
|
|
|
json_error_t err;
|
|
|
|
auto ret = json_unpack_ex(cfg, &err, 0, "{ s: { s: i, s: i } }", "parameters",
|
|
|
|
"num_si", &num_si, "num_mi", &num_mi);
|
|
|
|
if (ret != 0)
|
|
|
|
throw ConfigError(cfg, err, "", "Cannot parse switch config");
|
2022-11-11 06:36:20 -05:00
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
axiSwitch.xConfig.MaxNumMI = num_mi;
|
|
|
|
axiSwitch.xConfig.MaxNumSI = num_si;
|
2018-01-23 14:47:44 +01:00
|
|
|
}
|
|
|
|
|
2022-12-02 18:46:00 +01:00
|
|
|
static AxiStreamSwitchFactory f;
|
|
|
|
|
2023-09-08 11:35:18 +02:00
|
|
|
} // namespace ip
|
|
|
|
} // namespace fpga
|
|
|
|
} // namespace villas
|