Skip to contentSkip to conversation

utils: Add helper to read arm MIDR_EL1 register

PATCH Peter Maydell 2 participants 7 messages Oct 27, 2016 – Nov 4, 2016 3 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 69 lines · On 25 October 2016 at 13:12, <[email protected]> wrote:

From: Vijaya Kumar K <[email protected]>

Add helper API to read MIDR_EL1 registers to fetch cpu identification information. This helps in adding errata's and architecture specific features.

This is implemented only for arm architecture.

---
 include/qemu/aarch64-cpuid.h | 29 +++++++++++++++++++++
 util/Makefile.objs           |  1 +
 util/aarch64-cpuid.c         | 61 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+)
include/qemu/aarch64-cpuid.h new file +29 -0
new file mode 100644
index 0000000..8f776e8
@@ -0,0 +1,29 @@
+#ifndef QEMU_AARCH64_CPUID_H
+#define QEMU_AARCH64_CPUID_H
+
+#if defined(__aarch64__)
+#define MIDR_IMPLEMENTER_SHIFT  24
+#define MIDR_IMPLEMENTER_MASK   (0xffULL << MIDR_IMPLEMENTER_SHIFT)
+#define MIDR_ARCHITECTURE_SHIFT 16
+#define MIDR_ARCHITECTURE_MASK  (0xf << MIDR_ARCHITECTURE_SHIFT)
+#define MIDR_PARTNUM_SHIFT      4
+#define MIDR_PARTNUM_MASK       (0xfff << MIDR_PARTNUM_SHIFT)
+
+#define MIDR_CPU_PART(imp, partnum) \
+        (((imp)                 << MIDR_IMPLEMENTER_SHIFT)  | \
+        (0xf                    << MIDR_ARCHITECTURE_SHIFT) | \
+        ((partnum)              << MIDR_PARTNUM_SHIFT))
+
+#define ARM_CPU_IMP_CAVIUM        0x43
+#define CAVIUM_CPU_PART_THUNDERX  0x0A1
+
+#define MIDR_THUNDERX_PASS2  \
+               MIDR_CPU_PART(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_THUNDERX)
+#define CPU_MODEL_MASK  (MIDR_IMPLEMENTER_MASK | MIDR_ARCHITECTURE_MASK | \
+                         MIDR_PARTNUM_MASK)
+
+uint64_t get_aarch64_cpu_id(void);
+bool is_thunderx_pass2_cpu(void);
+#endif
+
+#endif
util/Makefile.objs +1 -0
index 36c7dcc..d14a455 100644
@@ -37,3 +37,4 @@ util-obj-y += log.o
 util-obj-y += qdist.o
 util-obj-y += qht.o
 util-obj-y += range.o
+util-obj-y += aarch64-cpuid.o
util/aarch64-cpuid.c new file +4 -0
new file mode 100644
index 0000000..536ece1
@@ -0,0 +1,61 @@
+/*
+ * Dealing with arm cpu identification information.
+ *
+ * Copyright (C) 2016 Cavium, Inc.

util/aarch64-cpuid.c line 14

Quoted 10 lines
util/aarch64-cpuid.c new file +10 -0
+ *
+ * Authors:
+ *  Vijaya Kumar K <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1
+ * or later.  See the COPYING.LIB file in the top-level directory.
+ */
+
+#include <math.h>
+#include "qemu/osdep.h"

osdep.h must always be the first #include, before anything else.

What do we need math.h for anyway?

util/aarch64-cpuid.c line 15

Quoted 1 line
util/aarch64-cpuid.c new file +1 -0
+#include "qemu-common.h"

What do we need qemu-common.h for ?

util/aarch64-cpuid.c line 22

Quoted 7 lines
util/aarch64-cpuid.c new file +7 -0
+#include "qemu/cutils.h"
+#include "qemu/aarch64-cpuid.h"
+
+#if defined(__aarch64__)
+static uint64_t qemu_read_aarch64_midr_el1(void)
+{
+#ifdef CONFIG_LINUX

When will CONFIG_LINUX not be defined but __aarch64__ is?

util/aarch64-cpuid.c line 35

Quoted 13 lines
util/aarch64-cpuid.c new file +13 -0
+    const char *file = "/sys/devices/system/cpu/cpu0/regs/identification/midr_el1";
+    char *buf;
+    uint64_t midr = 0;
+
+    if (!g_file_get_contents(file, &buf, 0, NULL)) {
+        goto out;
+    }
+
+    if (qemu_strtoull(buf, NULL, 0, &midr) < 0) {
+        goto out;
+    }
+
+out:

Why do we do a goto to the immediately following statement?

You have an inconsistency in that for one error condition (if g_file_get_contents() fails) this function will return 0, but for another error condition (qemu_strtoull() fails) you'll return -1.

Quoted 18 lines
util/aarch64-cpuid.c new file +18 -0
+    g_free(buf);
+
+    return midr;
+#else
+    return 0;
+#endif
+}
+
+static uint64_t aarch64_midr_val;
+uint64_t get_aarch64_cpu_id(void)
+{
+#ifdef CONFIG_LINUX
+    aarch64_midr_val = qemu_read_aarch64_midr_el1();
+    aarch64_midr_val &= CPU_MODEL_MASK;
+
+    return aarch64_midr_val;
+#else
+    return 0;

util/aarch64-cpuid.c

Quoted 10 lines
util/aarch64-cpuid.c new file +8 -1
+#endif
+}
+
+bool is_thunderx_pass2_cpu(void)
+{
+    return aarch64_midr_val == MIDR_THUNDERX_PASS2;
+}
+#endif
 
--

thanks
-- PMM

#3 lore raw
Quoted 89 lines · Peter Maydell
Quoted 69 lines · On 25 October 2016 at 13:12, <[email protected]> wrote:

From: Vijaya Kumar K <[email protected]>

Add helper API to read MIDR_EL1 registers to fetch cpu identification information. This helps in adding errata's and architecture specific features.

This is implemented only for arm architecture.

---
 include/qemu/aarch64-cpuid.h | 29 +++++++++++++++++++++
 util/Makefile.objs           |  1 +
 util/aarch64-cpuid.c         | 61 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+)
include/qemu/aarch64-cpuid.h new file +29 -0
new file mode 100644
index 0000000..8f776e8
@@ -0,0 +1,29 @@
+#ifndef QEMU_AARCH64_CPUID_H
+#define QEMU_AARCH64_CPUID_H
+
+#if defined(__aarch64__)
+#define MIDR_IMPLEMENTER_SHIFT  24
+#define MIDR_IMPLEMENTER_MASK   (0xffULL << MIDR_IMPLEMENTER_SHIFT)
+#define MIDR_ARCHITECTURE_SHIFT 16
+#define MIDR_ARCHITECTURE_MASK  (0xf << MIDR_ARCHITECTURE_SHIFT)
+#define MIDR_PARTNUM_SHIFT      4
+#define MIDR_PARTNUM_MASK       (0xfff << MIDR_PARTNUM_SHIFT)
+
+#define MIDR_CPU_PART(imp, partnum) \
+        (((imp)                 << MIDR_IMPLEMENTER_SHIFT)  | \
+        (0xf                    << MIDR_ARCHITECTURE_SHIFT) | \
+        ((partnum)              << MIDR_PARTNUM_SHIFT))
+
+#define ARM_CPU_IMP_CAVIUM        0x43
+#define CAVIUM_CPU_PART_THUNDERX  0x0A1
+
+#define MIDR_THUNDERX_PASS2  \
+               MIDR_CPU_PART(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_THUNDERX)
+#define CPU_MODEL_MASK  (MIDR_IMPLEMENTER_MASK | MIDR_ARCHITECTURE_MASK | \
+                         MIDR_PARTNUM_MASK)
+
+uint64_t get_aarch64_cpu_id(void);
+bool is_thunderx_pass2_cpu(void);
+#endif
+
+#endif
util/Makefile.objs +1 -0
index 36c7dcc..d14a455 100644
@@ -37,3 +37,4 @@ util-obj-y += log.o
 util-obj-y += qdist.o
 util-obj-y += qht.o
 util-obj-y += range.o
+util-obj-y += aarch64-cpuid.o
util/aarch64-cpuid.c new file +4 -0
new file mode 100644
index 0000000..536ece1
@@ -0,0 +1,61 @@
+/*
+ * Dealing with arm cpu identification information.
+ *
+ * Copyright (C) 2016 Cavium, Inc.

util/aarch64-cpuid.c line 14

Quoted 10 lines
util/aarch64-cpuid.c new file +10 -0
+ *
+ * Authors:
+ *  Vijaya Kumar K <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1
+ * or later.  See the COPYING.LIB file in the top-level directory.
+ */
+
+#include <math.h>
+#include "qemu/osdep.h"

osdep.h must always be the first #include, before anything else.

What do we need math.h for anyway?

util/aarch64-cpuid.c line 15

Quoted 1 line
util/aarch64-cpuid.c new file +1 -0
+#include "qemu-common.h"

What do we need qemu-common.h for ?

Quoted 9 lines

util/aarch64-cpuid.c line 22

Quoted 7 lines
util/aarch64-cpuid.c new file +7 -0
+#include "qemu/cutils.h"
+#include "qemu/aarch64-cpuid.h"
+
+#if defined(__aarch64__)
+static uint64_t qemu_read_aarch64_midr_el1(void)
+{
+#ifdef CONFIG_LINUX

When will CONFIG_LINUX not be defined but __aarch64__ is?

The contents of this file is compiled only for aarch64 and hence
all the contents are under this __aarch64__.
Also the code is only for linux, have added CONFIG_LINUX.

#4 lore raw
Quoted 32 lines · Vijay Kilari
Quoted 22 lines · Peter Maydell
Quoted 19 lines · On 25 October 2016 at 13:12, <[email protected]> wrote:

From: Vijaya Kumar K <[email protected]>

Add helper API to read MIDR_EL1 registers to fetch cpu identification information. This helps in adding errata's and architecture specific features.

This is implemented only for arm architecture.

util/Makefile.objs +1 -0
index 36c7dcc..d14a455 100644
@@ -37,3 +37,4 @@ util-obj-y += log.o
 util-obj-y += qdist.o
 util-obj-y += qht.o
 util-obj-y += range.o
+util-obj-y += aarch64-cpuid.o
Quoted 9 lines

util/Makefile.objs

Quoted 7 lines
util/Makefile.objs +7 -0
+#include "qemu/cutils.h"
+#include "qemu/aarch64-cpuid.h"
+
+#if defined(__aarch64__)
+static uint64_t qemu_read_aarch64_midr_el1(void)
+{
+#ifdef CONFIG_LINUX

When will CONFIG_LINUX not be defined but __aarch64__ is?

Quoted 1 line

The contents of this file is compiled only for aarch64

Your makefile change compiles it for everything.

Quoted 3 lines

and hence
all the contents are under this __aarch64__. Also the code is only for linux, have added CONFIG_LINUX.

...and you haven't answered the question: in what
circumstances could __aarch64__ be defined but
CONFIG_LINUX is not, ie why is there any point in
checking both defines?

thanks
-- PMM

#5 lore raw
Quoted 37 lines · Peter Maydell
Quoted 32 lines · Vijay Kilari
Quoted 22 lines · Peter Maydell
Quoted 19 lines · On 25 October 2016 at 13:12, <[email protected]> wrote:

From: Vijaya Kumar K <[email protected]>

Add helper API to read MIDR_EL1 registers to fetch cpu identification information. This helps in adding errata's and architecture specific features.

This is implemented only for arm architecture.

util/Makefile.objs +1 -0
index 36c7dcc..d14a455 100644
@@ -37,3 +37,4 @@ util-obj-y += log.o
 util-obj-y += qdist.o
 util-obj-y += qht.o
 util-obj-y += range.o
+util-obj-y += aarch64-cpuid.o
Quoted 9 lines

util/Makefile.objs

Quoted 7 lines
util/Makefile.objs +7 -0
+#include "qemu/cutils.h"
+#include "qemu/aarch64-cpuid.h"
+
+#if defined(__aarch64__)
+static uint64_t qemu_read_aarch64_midr_el1(void)
+{
+#ifdef CONFIG_LINUX

When will CONFIG_LINUX not be defined but __aarch64__ is?

Quoted 1 line

The contents of this file is compiled only for aarch64

Your makefile change compiles it for everything.

Quoted 8 lines
Quoted 3 lines

and hence
all the contents are under this __aarch64__. Also the code is only for linux, have added CONFIG_LINUX.

...and you haven't answered the question: in what
circumstances could __aarch64__ be defined but
CONFIG_LINUX is not, ie why is there any point in
checking both defines?

Ok. You mean __aarch64__ and __linux__ both are defined by gcc. we can rely on __aarch64__ define here?.

AFAIK, the caller of this function bufferiszero.c is compiled for everything, I case of bufferiszero.c is compiled for other than linux for aarch64, compilation might fail. In such case, the header file needs to have dummy/empty functions.

#6 lore raw

Hi Peter

Quoted 47 lines · Vijay Kilari
Quoted 37 lines · Peter Maydell
Quoted 32 lines · Vijay Kilari
Quoted 22 lines · Peter Maydell
Quoted 19 lines · On 25 October 2016 at 13:12, <[email protected]> wrote:

From: Vijaya Kumar K <[email protected]>

Add helper API to read MIDR_EL1 registers to fetch cpu identification information. This helps in adding errata's and architecture specific features.

This is implemented only for arm architecture.

util/Makefile.objs +1 -0
index 36c7dcc..d14a455 100644
@@ -37,3 +37,4 @@ util-obj-y += log.o
 util-obj-y += qdist.o
 util-obj-y += qht.o
 util-obj-y += range.o
+util-obj-y += aarch64-cpuid.o
Quoted 9 lines

util/Makefile.objs

Quoted 7 lines
util/Makefile.objs +7 -0
+#include "qemu/cutils.h"
+#include "qemu/aarch64-cpuid.h"
+
+#if defined(__aarch64__)
+static uint64_t qemu_read_aarch64_midr_el1(void)
+{
+#ifdef CONFIG_LINUX

When will CONFIG_LINUX not be defined but __aarch64__ is?

Quoted 1 line

The contents of this file is compiled only for aarch64

Your makefile change compiles it for everything.

Quoted 8 lines
Quoted 3 lines

and hence
all the contents are under this __aarch64__. Also the code is only for linux, have added CONFIG_LINUX.

...and you haven't answered the question: in what
circumstances could __aarch64__ be defined but
CONFIG_LINUX is not, ie why is there any point in
checking both defines?

Quoted 7 lines

Ok. You mean __aarch64__ and __linux__ both are defined by gcc. we can rely on __aarch64__ define here?.

AFAIK, the caller of this function bufferiszero.c is compiled for everything, I case of bufferiszero.c is compiled for other than linux for aarch64, compilation might fail. In such case, the header file needs to have dummy/empty functions.

What do you suggest for this?.

Regards
Vijay

#7 lore raw
Quoted 1 line · Vijay Kilari

What do you suggest for this?.

Some general principles:
* be consistent
* don't check things you don't need to
* prefer functions that exist in all configs (which may be dummy functions in some cases) rather than guarding callsites and #include lines with #ifdefs

thanks
-- PMM

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw

Re: [Qemu-devel] [PATCH v4 3/3] utils: Add prefetch for Thunderx platform

Quoted 14 lines · On 25 October 2016 at 13:12, <[email protected]> wrote:

From: Vijaya Kumar K <[email protected]>

Thunderx pass2 chip requires explicit prefetch
instruction to give prefetch hint.

To speed up live migration on Thunderx platform, prefetch instruction is added in zero buffer check function.The below results show live migration time improvement with prefetch instruction. VM with 4 VCPUs, 8GB RAM is migrated.

Code for decoding cache size is taken from Richard's patch.

(quoted diff) +2 -0
+#if defined(__aarch64__)
+#include "qemu/aarch64-cpuid.h"

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+static void __attribute__((constructor)) aarch64_init_cache_size(void)
+{
+    uint64_t t;
+
+    /* Use the DZP block size as a proxy for the cacheline size,
+       since the later is not available to userspace.  This seems
+       to work in practice for existing implementations.  */
+    asm("mrs %0, dczid_el0" : "=r"(t));
+    if (pow(2, (t & 0xf)) * 4 >= 128) {

Converting to double and calling a function in the math library just to determine 2^x seems a bit silly when that is (1 << x).

   dzp_blocksize_bytes = 1 << ((t & 0xf) + 2);

   if (dzp_blocksize_bytes >= 128) {

Review

Quoted 13 lines
(quoted diff) +13 -0
+        cache_line_size = 128;
+    } else {
+        cache_line_size = 64;
+    }
+
+    get_aarch64_cpu_id();
+    if (is_thunderx_pass2_cpu()) {
+        prefetch_line_dist = 3;
+        prefetch_distance = (prefetch_line_dist * cache_line_size) /
+                             sizeof(uint64_t);
+    }
+}
+#endif

thanks
-- PMM