eal: move CPU flag functions out of headers
[dpdk.git] / lib / librte_eal / common / arch / ppc_64 / rte_cpuflags.c
index 7f4e6dd..b7e0b72 100644 (file)
 
 #include "rte_cpuflags.h"
 
+#include <elf.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <unistd.h>
+
+/* Symbolic values for the entries in the auxiliary table */
+#define AT_HWCAP  16
+#define AT_HWCAP2 26
+
+/* software based registers */
+enum cpu_register_t {
+       REG_HWCAP = 0,
+       REG_HWCAP2,
+};
+
+typedef uint32_t cpuid_registers_t[4];
+
+/**
+ * Struct to hold a processor feature entry
+ */
+struct feature_entry {
+       uint32_t leaf;                          /**< cpuid leaf */
+       uint32_t subleaf;                       /**< cpuid subleaf */
+       uint32_t reg;                           /**< cpuid register */
+       uint32_t bit;                           /**< cpuid register bit */
+#define CPU_FLAG_NAME_MAX_LEN 64
+       char name[CPU_FLAG_NAME_MAX_LEN];       /**< String for printing */
+};
+
+#define FEAT_DEF(name, leaf, subleaf, reg, bit) \
+       [RTE_CPUFLAG_##name] = {leaf, subleaf, reg, bit, #name },
+
 const struct feature_entry rte_cpu_feature_table[] = {
        FEAT_DEF(PPC_LE, 0x00000001, 0, REG_HWCAP,  0)
        FEAT_DEF(TRUE_LE, 0x00000001, 0, REG_HWCAP,  1)
@@ -68,3 +100,58 @@ const struct feature_entry rte_cpu_feature_table[] = {
        FEAT_DEF(HTM, 0x00000001, 0, REG_HWCAP2,  30)
        FEAT_DEF(ARCH_2_07, 0x00000001, 0, REG_HWCAP2,  31)
 };
+
+/*
+ * Read AUXV software register and get cpu features for Power
+ */
+static void
+rte_cpu_get_features(__attribute__((unused)) uint32_t leaf,
+       __attribute__((unused)) uint32_t subleaf, cpuid_registers_t out)
+{
+       int auxv_fd;
+       Elf64_auxv_t auxv;
+
+       auxv_fd = open("/proc/self/auxv", O_RDONLY);
+       assert(auxv_fd);
+       while (read(auxv_fd, &auxv,
+               sizeof(Elf64_auxv_t)) == sizeof(Elf64_auxv_t)) {
+               if (auxv.a_type == AT_HWCAP)
+                       out[REG_HWCAP] = auxv.a_un.a_val;
+               else if (auxv.a_type == AT_HWCAP2)
+                       out[REG_HWCAP2] = auxv.a_un.a_val;
+       }
+}
+
+/*
+ * Checks if a particular flag is available on current machine.
+ */
+int
+rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
+{
+       const struct feature_entry *feat;
+       cpuid_registers_t regs = {0};
+
+       if (feature >= RTE_CPUFLAG_NUMFLAGS)
+               /* Flag does not match anything in the feature tables */
+               return -ENOENT;
+
+       feat = &rte_cpu_feature_table[feature];
+
+       if (!feat->leaf)
+               /* This entry in the table wasn't filled out! */
+               return -EFAULT;
+
+       /* get the cpuid leaf containing the desired feature */
+       rte_cpu_get_features(feat->leaf, feat->subleaf, regs);
+
+       /* check if the feature is enabled */
+       return (regs[feat->reg] >> feat->bit) & 1;
+}
+
+const char *
+rte_cpu_get_flag_name(enum rte_cpu_flag_t feature)
+{
+       if (feature >= RTE_CPUFLAG_NUMFLAGS)
+               return NULL;
+       return rte_cpu_feature_table[feature].name;
+}