s390x/migration: Storage attributes device
[qemu/ar7.git] / util / cacheinfo.c
blob6253049533e8557e7592252126e135299035216b
1 /*
2 * cacheinfo.c - helpers to query the host about its caches
4 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
5 * License: GNU GPL, version 2 or later.
6 * See the COPYING file in the top-level directory.
7 */
9 #include "qemu/osdep.h"
11 int qemu_icache_linesize = 0;
12 int qemu_dcache_linesize = 0;
15 * Operating system specific detection mechanisms.
18 #if defined(_AIX)
19 # include <sys/systemcfg.h>
21 static void sys_cache_info(int *isize, int *dsize)
23 *isize = _system_configuration.icache_line;
24 *dsize = _system_configuration.dcache_line;
27 #elif defined(_WIN32)
29 static void sys_cache_info(int *isize, int *dsize)
31 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buf;
32 DWORD size = 0;
33 BOOL success;
34 size_t i, n;
36 /* Check for the required buffer size first. Note that if the zero
37 size we use for the probe results in success, then there is no
38 data available; fail in that case. */
39 success = GetLogicalProcessorInformation(0, &size);
40 if (success || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
41 return;
44 n = size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
45 size = n * sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
46 buf = g_new0(SYSTEM_LOGICAL_PROCESSOR_INFORMATION, n);
47 if (!GetLogicalProcessorInformation(buf, &size)) {
48 goto fail;
51 for (i = 0; i < n; i++) {
52 if (buf[i].Relationship == RelationCache
53 && buf[i].Cache.Level == 1) {
54 switch (buf[i].Cache.Type) {
55 case CacheUnified:
56 *isize = *dsize = buf[i].Cache.LineSize;
57 break;
58 case CacheInstruction:
59 *isize = buf[i].Cache.LineSize;
60 break;
61 case CacheData:
62 *dsize = buf[i].Cache.LineSize;
63 break;
64 default:
65 break;
69 fail:
70 g_free(buf);
73 #elif defined(__APPLE__) \
74 || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
75 # include <sys/sysctl.h>
76 # if defined(__APPLE__)
77 # define SYSCTL_CACHELINE_NAME "hw.cachelinesize"
78 # else
79 # define SYSCTL_CACHELINE_NAME "machdep.cacheline_size"
80 # endif
82 static void sys_cache_info(int *isize, int *dsize)
84 /* There's only a single sysctl for both I/D cache line sizes. */
85 long size;
86 size_t len = sizeof(size);
87 if (!sysctlbyname(SYSCTL_CACHELINE_NAME, &size, &len, NULL, 0)) {
88 *isize = *dsize = size;
92 #else
93 /* POSIX */
95 static void sys_cache_info(int *isize, int *dsize)
97 # ifdef _SC_LEVEL1_ICACHE_LINESIZE
98 *isize = sysconf(_SC_LEVEL1_ICACHE_LINESIZE);
99 # endif
100 # ifdef _SC_LEVEL1_DCACHE_LINESIZE
101 *dsize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
102 # endif
104 #endif /* sys_cache_info */
107 * Architecture (+ OS) specific detection mechanisms.
110 #if defined(__aarch64__)
112 static void arch_cache_info(int *isize, int *dsize)
114 if (*isize == 0 || *dsize == 0) {
115 unsigned long ctr;
117 /* The real cache geometry is in CCSIDR_EL1/CLIDR_EL1/CSSELR_EL1,
118 but (at least under Linux) these are marked protected by the
119 kernel. However, CTR_EL0 contains the minimum linesize in the
120 entire hierarchy, and is used by userspace cache flushing. */
121 asm volatile("mrs\t%0, ctr_el0" : "=r"(ctr));
122 if (*isize == 0) {
123 *isize = 4 << (ctr & 0xf);
125 if (*dsize == 0) {
126 *dsize = 4 << ((ctr >> 16) & 0xf);
131 #elif defined(_ARCH_PPC) && defined(__linux__)
133 static void arch_cache_info(int *isize, int *dsize)
135 if (*isize == 0) {
136 *isize = qemu_getauxval(AT_ICACHEBSIZE);
138 if (*dsize == 0) {
139 *dsize = qemu_getauxval(AT_DCACHEBSIZE);
143 #else
144 static void arch_cache_info(int *isize, int *dsize) { }
145 #endif /* arch_cache_info */
148 * ... and if all else fails ...
151 static void fallback_cache_info(int *isize, int *dsize)
153 /* If we can only find one of the two, assume they're the same. */
154 if (*isize) {
155 if (*dsize) {
156 /* Success! */
157 } else {
158 *dsize = *isize;
160 } else if (*dsize) {
161 *isize = *dsize;
162 } else {
163 #if defined(_ARCH_PPC)
164 /* For PPC, we're going to use the icache size computed for
165 flush_icache_range. Which means that we must use the
166 architecture minimum. */
167 *isize = *dsize = 16;
168 #else
169 /* Otherwise, 64 bytes is not uncommon. */
170 *isize = *dsize = 64;
171 #endif
175 static void __attribute__((constructor)) init_cache_info(void)
177 int isize = 0, dsize = 0;
179 sys_cache_info(&isize, &dsize);
180 arch_cache_info(&isize, &dsize);
181 fallback_cache_info(&isize, &dsize);
183 qemu_icache_linesize = isize;
184 qemu_dcache_linesize = dsize;