1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-23 00:00:01 +01:00
VILLASnode/tests/unit/fpga.c

403 lines
11 KiB
C
Raw Normal View History

2016-06-14 01:19:17 +02:00
/** Test procedures for VILLASfpga
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Steffen Vogel
2016-06-14 01:19:17 +02:00
*********************************************************************************/
#include <stdlib.h>
#include <stdbool.h>
2017-03-03 20:21:33 -04:00
#include <unistd.h>
#include <criterion/criterion.h>
#include <criterion/options.h>
#include <xilinx/xtmrctr.h>
2017-03-12 23:16:33 -03:00
#include <villas/super_node.h>
2016-06-14 01:19:17 +02:00
#include <villas/utils.h>
2016-06-19 19:23:19 +02:00
#include <villas/nodes/fpga.h>
2016-06-14 01:19:17 +02:00
#include <villas/fpga/ip.h>
2017-03-03 20:21:33 -04:00
#include <villas/fpga/card.h>
2017-03-07 06:36:23 -04:00
#include <villas/fpga/vlnv.h>
2017-03-03 20:21:33 -04:00
#include <villas/fpga/ips/intc.h>
#include <villas/fpga/ips/timer.h>
2017-03-29 04:25:51 +02:00
#define TEST_CONFIG "/villas/etc/fpga.conf"
#define TEST_LEN 0x1000
2016-06-14 01:19:17 +02:00
2017-03-29 04:25:51 +02:00
#define CPU_HZ 3392389000
#define FPGA_AXI_HZ 125000000
2016-06-14 01:19:17 +02:00
2017-03-29 04:25:51 +02:00
static struct list cards;
2017-03-07 06:36:23 -04:00
static struct fpga_card *card;
2017-03-25 21:23:48 +01:00
static struct super_node sn = { .state = STATE_DESTROYED };
static struct pci pci;
static struct vfio_container vc;
2017-03-03 20:21:33 -04:00
static void init()
{
int ret;
config_setting_t *cfg_root;
2017-03-25 21:23:48 +01:00
ret = super_node_init(&sn);
cr_assert_eq(ret, 0, "Failed to initialize Supernode");
2017-03-25 21:23:48 +01:00
ret = super_node_parse_uri(&sn, TEST_CONFIG);
cr_assert_eq(ret, 0, "Failed to parse configuration");
2017-03-25 21:23:48 +01:00
ret = super_node_check(&sn);
cr_assert_eq(ret, 0, "Failed to check configuration");
2017-03-12 23:16:33 -03:00
cfg_root = config_root_setting(&sn.cfg);
2017-03-25 21:23:48 +01:00
cr_assert_neq(cfg_root, NULL);
ret = pci_init(&pci);
cr_assert_eq(ret, 0, "Failed to initialize PCI sub-system");
2017-03-25 21:23:48 +01:00
ret = vfio_init(&vc);
cr_assert_eq(ret, 0, "Failed to initiliaze VFIO sub-system");
2017-03-25 21:23:48 +01:00
/* Parse FPGA configuration */
2017-03-29 04:25:51 +02:00
list_init(&cards);
ret = fpga_card_parse_list(&cards, cfg_root);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to parse FPGA config");
2017-03-29 04:25:51 +02:00
card = list_lookup(&cards, "vc707");
2017-03-25 21:23:48 +01:00
cr_assert(card, "FPGA card not found");
if (criterion_options.logging_threshold < CRITERION_IMPORTANT)
2017-03-07 06:36:23 -04:00
fpga_card_dump(card);
}
static void fini()
{
int ret;
2017-03-07 06:36:23 -04:00
ret = fpga_card_destroy(card);
cr_assert_eq(ret, 0, "Failed to de-initilize FPGA");
2017-03-12 23:16:33 -03:00
super_node_destroy(&sn);
}
TestSuite(fpga,
.init = init,
.fini = fini,
2017-03-29 04:25:51 +02:00
.description = "VILLASfpga",
.disabled = true);
Test(fpga, intc, .description = "Interrupt Controller")
2016-06-14 01:19:17 +02:00
{
int ret;
2016-06-19 19:23:19 +02:00
uint32_t isr;
2016-06-14 01:19:17 +02:00
2017-03-07 06:36:23 -04:00
cr_assert(card->intc);
2016-06-19 19:23:19 +02:00
2017-03-07 06:36:23 -04:00
ret = intc_enable(card->intc, 0xFF00, 0);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to enable interrupt");
2016-06-14 01:19:17 +02:00
2016-06-19 19:23:19 +02:00
/* Fake IRQs in software by writing to ISR */
2017-03-07 06:36:23 -04:00
XIntc_Out32((uintptr_t) card->map + card->intc->baseaddr + XIN_ISR_OFFSET, 0xFF00);
2016-06-19 19:23:19 +02:00
/* Wait for 8 SW triggered IRQs */
2016-06-14 01:19:17 +02:00
for (int i = 0; i < 8; i++)
2017-03-07 06:36:23 -04:00
intc_wait(card->intc, i+8);
2016-06-14 01:19:17 +02:00
2016-06-19 19:23:19 +02:00
/* Check ISR if all SW IRQs have been deliverd */
2017-03-07 06:36:23 -04:00
isr = XIntc_In32((uintptr_t) card->map + card->intc->baseaddr + XIN_ISR_OFFSET);
2016-06-19 19:23:19 +02:00
2017-03-07 06:36:23 -04:00
ret = intc_disable(card->intc, 0xFF00);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to disable interrupt");
2016-06-14 01:19:17 +02:00
cr_assert_eq(isr & 0xFF00, 0); /* ISR should get cleared by MSI_Grant_signal */
2016-06-14 01:19:17 +02:00
}
Test(fpga, xsg, .description = "XSG: multiply_add")
2016-06-14 01:19:17 +02:00
{
2016-06-19 19:23:19 +02:00
int ret;
double factor, err = 0;
2017-03-25 21:23:48 +01:00
struct fpga_ip *ip, *dma;
2016-07-08 13:31:46 +02:00
struct model_param *p;
struct dma_mem mem;
2017-03-25 21:23:48 +01:00
ip = fpga_vlnv_lookup(&card->ips, &(struct fpga_vlnv) { NULL, "sysgen", "xsg_multiply", NULL });
cr_assert(ip);
2017-03-07 06:36:23 -04:00
dma = fpga_vlnv_lookup(&card->ips, &(struct fpga_vlnv) { "xilinx.com", "ip", "axi_dma", NULL });
2017-03-25 21:23:48 +01:00
cr_assert(dma);
2016-06-14 01:19:17 +02:00
2017-03-29 04:25:51 +02:00
struct model *model = ip->_vd;
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
p = list_lookup(&model->parameters, "factor");
2016-06-19 19:23:19 +02:00
if (!p)
2017-03-25 21:23:48 +01:00
error("Missing parameter 'factor' for model '%s'", ip->name);
2016-06-19 19:23:19 +02:00
ret = model_param_read(p, &factor);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to read parameter 'factor' from model '%s'", ip->name);
2016-06-19 19:23:19 +02:00
info("Model param: factor = %f", factor);
2017-03-25 21:23:48 +01:00
ret = switch_connect(card->sw, dma, ip);
cr_assert_eq(ret, 0, "Failed to configure switch");
ret = switch_connect(card->sw, ip, dma);
cr_assert_eq(ret, 0, "Failed to configure switch");
2016-06-14 01:19:17 +02:00
2016-07-08 13:31:46 +02:00
ret = dma_alloc(dma, &mem, 0x1000, 0);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to allocate DMA memory");
2016-07-08 13:31:46 +02:00
float *src = (float *) mem.base_virt;
float *dst = (float *) mem.base_virt + 0x800;
2016-06-19 19:23:19 +02:00
for (int i = 0; i < 6; i++)
src[i] = 1.1 * (i+1);
2016-06-14 01:19:17 +02:00
2016-06-19 19:23:19 +02:00
ret = dma_ping_pong(dma, (char *) src, (char *) dst, 6 * sizeof(float));
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to to ping pong DMA transfer: %d", ret);
2016-06-14 01:19:17 +02:00
for (int i = 0; i < 6; i++)
2016-06-19 19:23:19 +02:00
err += abs(factor * src[i] - dst[i]);
2016-06-19 19:23:19 +02:00
info("Error after FPGA operation: err = %f", err);
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
ret = switch_disconnect(card->sw, dma, ip);
cr_assert_eq(ret, 0, "Failed to configure switch");
ret = switch_disconnect(card->sw, ip, dma);
cr_assert_eq(ret, 0, "Failed to configure switch");
2016-07-08 13:31:46 +02:00
ret = dma_free(dma, &mem);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to release DMA memory");
2016-07-08 13:31:46 +02:00
cr_assert(err < 1e-3);
2016-06-14 01:19:17 +02:00
}
Test(fpga, hls_dft, .description = "HLS: hls_dft")
{
int ret;
2017-02-18 10:43:58 -05:00
struct fpga_ip *hls, *rtds;
2017-03-07 06:36:23 -04:00
rtds = fpga_vlnv_lookup(&card->ips, &(struct fpga_vlnv) { "acs.eonerc.rwth-aachen.de", "user", "rtds_axis", NULL });
hls = fpga_vlnv_lookup(&card->ips, &(struct fpga_vlnv) { NULL, "hls", "hls_dft", NULL });
/* Check if required IP is available on FPGA */
cr_assert(hls && rtds);
2017-03-07 06:36:23 -04:00
ret = intc_enable(card->intc, (1 << rtds->irq), 0);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to enable interrupt");
2017-03-07 06:36:23 -04:00
ret = switch_connect(card->sw, rtds, hls);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to configure switch");
2017-03-07 06:36:23 -04:00
ret = switch_connect(card->sw, hls, rtds);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to configure switch");
while(1) {
/* Dump RTDS AXI Stream state */
rtds_axis_dump(rtds);
sleep(1);
}
#if 0
int len = 2000;
int NSAMPLES = 400;
float src[len], dst[len];
for (int i = 0; i < len; i++) {
src[i] = 4 + 5.0 * sin(2.0 * M_PI * 1 * i / NSAMPLES) +
2.0 * sin(2.0 * M_PI * 2 * i / NSAMPLES) +
1.0 * sin(2.0 * M_PI * 5 * i / NSAMPLES) +
0.5 * sin(2.0 * M_PI * 9 * i / NSAMPLES) +
0.2 * sin(2.0 * M_PI * 15 * i / NSAMPLES);
fifo_write()
}
2016-06-19 19:23:19 +02:00
#endif
2017-03-07 06:36:23 -04:00
ret = switch_disconnect(card->sw, rtds, hls);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to configure switch");
2017-03-07 06:36:23 -04:00
ret = switch_disconnect(card->sw, hls, rtds);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to configure switch");
}
Test(fpga, fifo, .description = "FIFO")
2016-06-14 01:19:17 +02:00
{
int ret;
ssize_t len;
char src[255], dst[255];
2017-02-18 10:43:58 -05:00
struct fpga_ip *fifo;
2016-06-14 01:19:17 +02:00
2017-03-07 06:36:23 -04:00
fifo = fpga_vlnv_lookup(&card->ips, &(struct fpga_vlnv) { "xilinx.com", "ip", "axi_fifo_mm_s", NULL });
cr_assert(fifo);
2017-03-07 06:36:23 -04:00
ret = intc_enable(card->intc, (1 << fifo->irq), 0);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to enable interrupt");
2017-03-07 06:36:23 -04:00
ret = switch_connect(card->sw, fifo, fifo);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to configure switch");
2016-06-14 01:19:17 +02:00
/* Get some random data to compare */
memset(dst, 0, sizeof(dst));
len = read_random((char *) src, sizeof(src));
if (len != sizeof(src))
2016-06-14 01:19:17 +02:00
error("Failed to get random data");
2016-06-19 19:23:19 +02:00
len = fifo_write(fifo, (char *) src, sizeof(src));
2016-06-14 01:19:17 +02:00
if (len != sizeof(src))
error("Failed to send to FIFO");
2016-06-19 19:23:19 +02:00
len = fifo_read(fifo, (char *) dst, sizeof(dst));
2016-06-14 01:19:17 +02:00
if (len != sizeof(dst))
error("Failed to read from FIFO");
2017-03-07 06:36:23 -04:00
ret = intc_disable(card->intc, (1 << fifo->irq));
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to disable interrupt");
2017-03-07 06:36:23 -04:00
ret = switch_disconnect(card->sw, fifo, fifo);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to configure switch");
2016-06-14 01:19:17 +02:00
/* Compare data */
cr_assert_eq(memcmp(src, dst, sizeof(src)), 0);
2016-06-14 01:19:17 +02:00
}
Test(fpga, dma, .description = "DMA")
2016-06-14 01:19:17 +02:00
{
int ret = -1;
2016-07-08 13:31:46 +02:00
struct dma_mem mem, src, dst;
2017-03-25 21:23:48 +01:00
for (size_t i = 0; i < list_length(&card->ips); i++) { INDENT
struct fpga_ip *dm = list_at(&card->ips, i);
if (fpga_vlnv_cmp(&dm->vlnv, &(struct fpga_vlnv) { "xilinx.com", "ip", "axi_dma", NULL }))
2016-06-19 19:23:19 +02:00
continue; /* skip non DMA IP cores */
2017-03-29 04:25:51 +02:00
struct dma *dma = dm->_vd;
2016-07-08 13:31:46 +02:00
/* Simple DMA can only transfer up to 4 kb due to
* PCIe page size burst limitation */
2017-03-25 21:23:48 +01:00
ssize_t len2, len = dma->inst.HasSg ? 64 << 20 : 1 << 2;
2016-07-08 13:31:46 +02:00
2017-03-25 21:23:48 +01:00
ret = dma_alloc(dm, &mem, 2 * len, 0);
cr_assert_eq(ret, 0);
2016-07-08 13:31:46 +02:00
ret = dma_mem_split(&mem, &src, &dst);
cr_assert_eq(ret, 0);
/* Get new random data */
len2 = read_random(src.base_virt, len);
if (len2 != len)
2016-07-08 13:31:46 +02:00
serror("Failed to get random data");
2017-03-25 21:23:48 +01:00
int irq_mm2s = dm->irq;
int irq_s2mm = dm->irq + 1;
2017-03-07 06:36:23 -04:00
ret = intc_enable(card->intc, (1 << irq_mm2s) | (1 << irq_s2mm), 0);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to enable interrupt");
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
ret = switch_connect(card->sw, dm, dm);
cr_assert_eq(ret, 0, "Failed to configure switch");
2016-06-19 19:23:19 +02:00
/* Start transfer */
2017-03-25 21:23:48 +01:00
ret = dma_ping_pong(dm, src.base_phys, dst.base_phys, dst.len);
cr_assert_eq(ret, 0, "DMA ping pong failed");
2016-06-14 01:19:17 +02:00
2016-07-08 13:31:46 +02:00
ret = memcmp(src.base_virt, dst.base_virt, src.len);
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
info("DMA %s (%s): %s", dm->name, dma->inst.HasSg ? "scatter-gather" : "simple", ret ? RED("failed") : GRN("passed"));
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
ret = switch_disconnect(card->sw, dm, dm);
cr_assert_eq(ret, 0, "Failed to configure switch");
2017-03-07 06:36:23 -04:00
ret = intc_disable(card->intc, (1 << irq_mm2s) | (1 << irq_s2mm));
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0, "Failed to disable interrupt");
2017-03-25 21:23:48 +01:00
ret = dma_free(dm, &mem);
cr_assert_eq(ret, 0, "Failed to release DMA memory");
2016-06-19 19:23:19 +02:00
}
2016-06-14 01:19:17 +02:00
cr_assert_eq(ret, 0);
2016-06-14 01:19:17 +02:00
}
Test(fpga, timer, .description = "Timer Counter")
2016-06-14 01:19:17 +02:00
{
int ret;
2017-03-25 21:23:48 +01:00
struct fpga_ip *ip;
struct timer *tmr;
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
ip = fpga_vlnv_lookup(&card->ips, &(struct fpga_vlnv) { "xilinx.com", "ip", "axi_timer", NULL });
cr_assert(ip);
2017-03-29 04:25:51 +02:00
tmr = ip->_vd;
2017-03-25 21:23:48 +01:00
XTmrCtr *xtmr = &tmr->inst;
2017-03-25 21:23:48 +01:00
ret = intc_enable(card->intc, (1 << ip->irq), 0);
cr_assert_eq(ret, 0, "Failed to enable interrupt");
2016-06-14 01:19:17 +02:00
XTmrCtr_SetOptions(xtmr, 0, XTC_EXT_COMPARE_OPTION | XTC_DOWN_COUNT_OPTION);
2016-07-18 15:09:49 +02:00
XTmrCtr_SetResetValue(xtmr, 0, FPGA_AXI_HZ / 125);
XTmrCtr_Start(xtmr, 0);
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
uint64_t counter = intc_wait(card->intc, ip->irq);
2016-07-08 13:31:46 +02:00
info("Got IRQ: counter = %ju", counter);
2016-06-14 01:19:17 +02:00
2016-07-08 13:31:46 +02:00
if (counter == 1)
return;
2016-07-08 13:31:46 +02:00
else
warn("Counter was not 1");
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
intc_disable(card->intc, (1 << ip->irq));
cr_assert_eq(ret, 0, "Failed to disable interrupt");
2016-06-14 01:19:17 +02:00
return;
2016-06-14 01:19:17 +02:00
}
Test(fpga, rtds_rtt, .description = "RTDS: tight rtt")
2016-06-14 01:19:17 +02:00
{
int ret;
2017-03-25 21:23:48 +01:00
struct fpga_ip *ip, *rtds;
2016-07-08 13:31:46 +02:00
struct dma_mem buf;
size_t recvlen;
2016-06-14 01:19:17 +02:00
2016-07-08 13:31:46 +02:00
/* Get IP cores */
2017-03-07 06:36:23 -04:00
rtds = fpga_vlnv_lookup(&card->ips, &(struct fpga_vlnv) { "acs.eonerc.rwth-aachen.de", "user", "rtds_axis", NULL });
2017-03-25 21:23:48 +01:00
cr_assert(rtds);
ip = fpga_vlnv_lookup(&card->ips, &(struct fpga_vlnv) { "xilinx.com", "ip", "axi_dma", NULL });
2017-03-25 21:23:48 +01:00
cr_assert(ip);
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
ret = switch_connect(card->sw, rtds, ip);
cr_assert_eq(ret, 0, "Failed to configure switch");
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
ret = switch_connect(card->sw, ip, rtds);
cr_assert_eq(ret, 0, "Failed to configure switch");
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
ret = dma_alloc(ip, &buf, 0x100, 0);
cr_assert_eq(ret, 0, "Failed to allocate DMA memory");
2016-06-14 01:19:17 +02:00
while (1) {
2017-03-25 21:23:48 +01:00
ret = dma_read(ip, buf.base_phys, buf.len);
cr_assert_eq(ret, 0, "Failed to start DMA read: %d", ret);
2016-06-14 01:19:17 +02:00
2017-03-25 21:23:48 +01:00
ret = dma_read_complete(ip, NULL, &recvlen);
cr_assert_eq(ret, 0, "Failed to complete DMA read: %d", ret);
2017-03-25 21:23:48 +01:00
ret = dma_write(ip, buf.base_phys, recvlen);
cr_assert_eq(ret, 0, "Failed to start DMA write: %d", ret);
2017-03-25 21:23:48 +01:00
ret = dma_write_complete(ip, NULL, NULL);
cr_assert_eq(ret, 0, "Failed to complete DMA write: %d", ret);
2016-06-14 01:19:17 +02:00
}
2017-03-25 21:23:48 +01:00
ret = switch_disconnect(card->sw, rtds, ip);
cr_assert_eq(ret, 0, "Failed to configure switch");
2017-03-07 06:36:23 -04:00
2017-03-25 21:23:48 +01:00
ret = switch_disconnect(card->sw, ip, rtds);
cr_assert_eq(ret, 0, "Failed to configure switch");
2017-03-25 21:23:48 +01:00
ret = dma_free(ip, &buf);
cr_assert_eq(ret, 0, "Failed to release DMA memory");
}