numam-dpdk/lib/eal/riscv/rte_cycles.c
Michal Mazurek f22e705ebf eal/riscv: support RISC-V architecture
Add all necessary elements for DPDK to compile and run EAL on SiFive
Freedom U740 SoC which is based on SiFive U74-MC (ISA: rv64imafdc)
core complex.

This includes:

- EAL library implementation for rv64imafdc ISA.
- meson build structure for 'riscv' architecture. RTE_ARCH_RISCV define
  is added for architecture identification.
- xmm_t structure operation stubs as there is no vector support in the
  U74 core.

Compilation was tested on Ubuntu and Arch Linux using riscv64 toolchain.
Clang compilation currently not supported due to issues with missing
relocation relaxation.

Two rte_rdtsc() schemes are provided: stable low-resolution using rdtime
(default) and unstable high-resolution using rdcycle. User can override
the scheme by defining RTE_RISCV_RDTSC_USE_HPM=1 during compile time of
both DPDK and the application. The reasoning for this is as follows.
The RISC-V ISA mandates that clock read by rdtime has to be of constant
period and synchronized between all hardware threads within 1 tick
(chapter 10.1 in version 20191213 of RISC-V spec).
However this clock may not be of high-enough frequency for dataplane
uses. I.e. on HiFive Unmatched (FU740) it is 1MHz.
There is a high-resolution alternative in form of rdcycle which is
clocked at the core clock frequency. The drawbacks are that it may be
disabled during sleep (WFI), its frequency might change due to DVFS and
it is core-local and therefore cannot be used as a wall-clock. It can
however be used for micro-benchmarking user applications, similarly to
Aarch64's PMCCNTR PMU counter.

The platform is currently marked as linux-only because rte_cycles
implementation uses the timebase-frequency device-tree node read through
the proc file system. Such approach was chosen because Linux kernel
depends on the presence of this device-tree node.

The i40e PMD driver is disabled on RISC-V as the rv64gc ISA has no vector
operations.

The compilation of following modules has been disabled by this commit
and will be re-enabled in later commits as fixes are introduced:
net/ixgbe, net/memif, net/tap, example/l3fwd.

Sponsored-by: Frank Zhao <frank.zhao@starfivetech.com>
Sponsored-by: Sam Grove <sam.grove@sifive.com>
Signed-off-by: Michal Mazurek <maz@semihalf.com>
Signed-off-by: Stanislaw Kardach <kda@semihalf.com>
2022-06-08 11:26:20 +02:00

78 lines
1.7 KiB
C

/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2015 Cavium, Inc
* Copyright(c) 2022 StarFive
* Copyright(c) 2022 SiFive
* Copyright(c) 2022 Semihalf
*/
#include <stdio.h>
#include "eal_private.h"
#include "rte_byteorder.h"
#include "rte_cycles.h"
#include "rte_log.h"
/** Read generic counter frequency */
static uint64_t
__rte_riscv_timefrq(void)
{
#define TIMEBASE_FREQ_SIZE 8
if (RTE_RISCV_TIME_FREQ > 0)
return RTE_RISCV_TIME_FREQ;
uint8_t buf[TIMEBASE_FREQ_SIZE];
ssize_t cnt;
FILE *file;
file = fopen("/proc/device-tree/cpus/timebase-frequency", "rb");
if (!file)
goto fail;
cnt = fread(buf, 1, TIMEBASE_FREQ_SIZE, file);
fclose(file);
switch (cnt) {
case 8:
return rte_be_to_cpu_64(*(uint64_t *)buf);
case 4:
return rte_be_to_cpu_32(*(uint32_t *)buf);
default:
break;
}
fail:
RTE_LOG(WARNING, EAL, "Unable to read timebase-frequency from FDT.\n");
return 0;
}
uint64_t
get_tsc_freq_arch(void)
{
RTE_LOG(NOTICE, EAL, "TSC using RISC-V %s.\n",
RTE_RISCV_RDTSC_USE_HPM ? "rdcycle" : "rdtime");
if (!RTE_RISCV_RDTSC_USE_HPM)
return __rte_riscv_timefrq();
#define CYC_PER_1MHZ 1E6
/*
* Use real time clock to estimate current cycle frequency
*/
uint64_t ticks, frq;
uint64_t start_ticks, cur_ticks;
uint64_t start_cycle, end_cycle;
/* Do not proceed unless clock frequency can be obtained. */
frq = __rte_riscv_timefrq();
if (!frq)
return 0;
/* Number of ticks for 1/10 second */
ticks = frq / 10;
start_ticks = __rte_riscv_rdtime_precise();
start_cycle = rte_rdtsc_precise();
do {
cur_ticks = __rte_riscv_rdtime();
} while ((cur_ticks - start_ticks) < ticks);
end_cycle = rte_rdtsc_precise();
/* Adjust the cycles to next 1Mhz */
return RTE_ALIGN_MUL_CEIL((end_cycle - start_cycle) * 10, CYC_PER_1MHZ);
}