From nobody Tue May 21 22:53:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1655776242; cv=none; d=zohomail.com; s=zohoarc; b=GqU54R+sJys+QWuC1NWBBB5NHQ1acSG2U7oAoNNviVahOExlarGMqedVJDsCA+zgmGpY8ma6V4D36EDKDYPa85Eka1lIlHpMN/om/1rm8ZuPViExqf0uUarCk86ehokCTE//6+5pjn+Nbr1DmbH2B3ZKJ+w9qfIhJ+amh8/ii0Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655776242; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=hdUixK1qVAGQKnIWY1i2054HTiVYBkI7oMMXQEZ8rSg=; b=CUP7PWqduKkvDVwxvJhkP5PUixRdbV5jkHenUJ69utBS1p/Sy0GnG3UtSqtDn1edzUqCuOnlkFq0ltl2adNazRffJ6oMREu4lj6UdUPRN0Onn1aiyy+ihuF8AhJwRErD9NMVKr2LreUec5lnpVsqwMrDYPCzmFD9prSsjLH+1+A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1655776242589140.7552081331745; Mon, 20 Jun 2022 18:50:42 -0700 (PDT) Received: from localhost ([::1]:33008 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o3T2O-0006J9-Vr for importer@patchew.org; Mon, 20 Jun 2022 21:50:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34902) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3T0V-0003Rd-OW for qemu-devel@nongnu.org; Mon, 20 Jun 2022 21:48:43 -0400 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]:45969) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o3T0T-0001tS-0P for qemu-devel@nongnu.org; Mon, 20 Jun 2022 21:48:43 -0400 Received: by mail-pf1-x42b.google.com with SMTP id 128so4724768pfv.12 for ; Mon, 20 Jun 2022 18:48:40 -0700 (PDT) Received: from stoup.. ([2602:47:d49e:3c01:a3e0:8a80:7b85:aea6]) by smtp.gmail.com with ESMTPSA id q11-20020a056a00084b00b0051bc3a2355csm9907875pfk.64.2022.06.20.18.48.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jun 2022 18:48:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hdUixK1qVAGQKnIWY1i2054HTiVYBkI7oMMXQEZ8rSg=; b=Zw4s6uwFQ1aNI0Ew9FV5K9zY9/jyEMJ35DXBp/tfRgF8DrmIWh7cHWt7nilZWttQKz CWZ4sAXgEnggOtoFInNx9u5avkUtx7bvUSb8TvOuTbxDpJn4L31uMSPtwEaD/rtcweNc uahRKkibVlryO6CCZGQr+oZ7RXZOftpP39s/CTUjua+gZ0fWXku5PWY824CH+fOOGvlQ j/algNnsh34OoELjjjJKybPME3M149CdRr0m1YBG+iCnQ62xZ2ODg6PCCFLjRXNsjJuU FhveUDhHy3F/1ZkLLT54ZWuBf8asHLtRzFpPkAKvBA9L9mFRQswNgFGMBIH0kXThsP5T VInw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hdUixK1qVAGQKnIWY1i2054HTiVYBkI7oMMXQEZ8rSg=; b=Ok9bkHIybTyWL/rhhIzzXElWYcurMhBZRHZQx68JMpoBtPryItVEA9/d641wIhWVxo wVPoj1y5wmPeV/KgvdxOaf9HmLy1tjQcy2SqwViJ/aPFC5vWKhr/XWLyYbtETL40GSgB bbu/OxaN/C1YraFuKEeYM+J7HSnK6J3UQaTkjonNw/d33XovEsaC9X6ZBNq5vhLGKN0U FRLd+QL6xFIJaOY7DLY9u2dpbtcaCEg+vTVl7js7QyKGrYBLcjKvLp/bcEAeUTiJlS+1 8BMLcosJSN45Emhv1vL0Nvq02IGgQMGpfm8PajM9QJbcrTB1d1+BySFmRL5pD3+rkuzh 8Yhg== X-Gm-Message-State: AJIora+VVTSxFLfmtBzj+yc7r5IuZeFbVmUzNlde+OYhVU5cZBj1Urk9 8HD1QYQ/3UxSwYSXSrWkTqSoTqOQkhDDuQ== X-Google-Smtp-Source: AGRyM1v4G4zWJ2t0b7Z1NJR3GGMG2zuR0gB+f91Jy3E0q0vIjIACVKf/SrTua/VKvCqnURsmgtfUfQ== X-Received: by 2002:aa7:97b6:0:b0:525:20b4:be8 with SMTP id d22-20020aa797b6000000b0052520b40be8mr9290999pfq.7.1655776119406; Mon, 20 Jun 2022 18:48:39 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: npiggin@gmail.com, qemu-ppc@nongnu.org Subject: [PATCH v2 1/3] util: Merge cacheflush.c and cacheinfo.c Date: Mon, 20 Jun 2022 18:48:35 -0700 Message-Id: <20220621014837.189139-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220621014837.189139-1-richard.henderson@linaro.org> References: <20220621014837.189139-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42b; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1655776244201100005 Content-Type: text/plain; charset="utf-8" Combine the two files into cacheflush.c. There's a couple of bits that would be helpful to share between the two, and combining them seems better than exporting the bits. Signed-off-by: Richard Henderson Reviewed-by: Peter Maydell --- util/cacheflush.c | 202 +++++++++++++++++++++++++++++++++++++++++++++- util/cacheinfo.c | 200 --------------------------------------------- util/meson.build | 2 +- 3 files changed, 202 insertions(+), 202 deletions(-) delete mode 100644 util/cacheinfo.c diff --git a/util/cacheflush.c b/util/cacheflush.c index 4b57186d89..8096afd33c 100644 --- a/util/cacheflush.c +++ b/util/cacheflush.c @@ -1,5 +1,5 @@ /* - * Flush the host cpu caches. + * Info about, and flushing the host cpu caches. * * This work is licensed under the terms of the GNU GPL, version 2 or late= r. * See the COPYING file in the top-level directory. @@ -9,8 +9,208 @@ #include "qemu/cacheflush.h" #include "qemu/cacheinfo.h" #include "qemu/bitops.h" +#include "qemu/host-utils.h" +#include "qemu/atomic.h" =20 =20 +int qemu_icache_linesize =3D 0; +int qemu_icache_linesize_log; +int qemu_dcache_linesize =3D 0; +int qemu_dcache_linesize_log; + +/* + * Operating system specific cache detection mechanisms. + */ + +#if defined(_WIN32) + +static void sys_cache_info(int *isize, int *dsize) +{ + SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buf; + DWORD size =3D 0; + BOOL success; + size_t i, n; + + /* + * Check for the required buffer size first. Note that if the zero + * size we use for the probe results in success, then there is no + * data available; fail in that case. + */ + success =3D GetLogicalProcessorInformation(0, &size); + if (success || GetLastError() !=3D ERROR_INSUFFICIENT_BUFFER) { + return; + } + + n =3D size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); + size =3D n * sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); + buf =3D g_new0(SYSTEM_LOGICAL_PROCESSOR_INFORMATION, n); + if (!GetLogicalProcessorInformation(buf, &size)) { + goto fail; + } + + for (i =3D 0; i < n; i++) { + if (buf[i].Relationship =3D=3D RelationCache + && buf[i].Cache.Level =3D=3D 1) { + switch (buf[i].Cache.Type) { + case CacheUnified: + *isize =3D *dsize =3D buf[i].Cache.LineSize; + break; + case CacheInstruction: + *isize =3D buf[i].Cache.LineSize; + break; + case CacheData: + *dsize =3D buf[i].Cache.LineSize; + break; + default: + break; + } + } + } + fail: + g_free(buf); +} + +#elif defined(__APPLE__) +# include +static void sys_cache_info(int *isize, int *dsize) +{ + /* There's only a single sysctl for both I/D cache line sizes. */ + long size; + size_t len =3D sizeof(size); + if (!sysctlbyname("hw.cachelinesize", &size, &len, NULL, 0)) { + *isize =3D *dsize =3D size; + } +} +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +# include +static void sys_cache_info(int *isize, int *dsize) +{ + /* There's only a single sysctl for both I/D cache line sizes. */ + int size; + size_t len =3D sizeof(size); + if (!sysctlbyname("machdep.cacheline_size", &size, &len, NULL, 0)) { + *isize =3D *dsize =3D size; + } +} +#else +/* POSIX */ + +static void sys_cache_info(int *isize, int *dsize) +{ +# ifdef _SC_LEVEL1_ICACHE_LINESIZE + int tmp_isize =3D (int) sysconf(_SC_LEVEL1_ICACHE_LINESIZE); + if (tmp_isize > 0) { + *isize =3D tmp_isize; + } +# endif +# ifdef _SC_LEVEL1_DCACHE_LINESIZE + int tmp_dsize =3D (int) sysconf(_SC_LEVEL1_DCACHE_LINESIZE); + if (tmp_dsize > 0) { + *dsize =3D tmp_dsize; + } +# endif +} +#endif /* sys_cache_info */ + + +/* + * Architecture (+ OS) specific cache detection mechanisms. + */ + +#if defined(__aarch64__) + +static void arch_cache_info(int *isize, int *dsize) +{ + if (*isize =3D=3D 0 || *dsize =3D=3D 0) { + uint64_t ctr; + + /* + * The real cache geometry is in CCSIDR_EL1/CLIDR_EL1/CSSELR_EL1, + * but (at least under Linux) these are marked protected by the + * kernel. However, CTR_EL0 contains the minimum linesize in the + * entire hierarchy, and is used by userspace cache flushing. + */ + asm volatile("mrs\t%0, ctr_el0" : "=3Dr"(ctr)); + if (*isize =3D=3D 0) { + *isize =3D 4 << (ctr & 0xf); + } + if (*dsize =3D=3D 0) { + *dsize =3D 4 << ((ctr >> 16) & 0xf); + } + } +} + +#elif defined(_ARCH_PPC) && defined(__linux__) +# include "elf.h" + +static void arch_cache_info(int *isize, int *dsize) +{ + if (*isize =3D=3D 0) { + *isize =3D qemu_getauxval(AT_ICACHEBSIZE); + } + if (*dsize =3D=3D 0) { + *dsize =3D qemu_getauxval(AT_DCACHEBSIZE); + } +} + +#else +static void arch_cache_info(int *isize, int *dsize) { } +#endif /* arch_cache_info */ + +/* + * ... and if all else fails ... + */ + +static void fallback_cache_info(int *isize, int *dsize) +{ + /* If we can only find one of the two, assume they're the same. */ + if (*isize) { + if (*dsize) { + /* Success! */ + } else { + *dsize =3D *isize; + } + } else if (*dsize) { + *isize =3D *dsize; + } else { +#if defined(_ARCH_PPC) + /* + * For PPC, we're going to use the cache sizes computed for + * flush_idcache_range. Which means that we must use the + * architecture minimum. + */ + *isize =3D *dsize =3D 16; +#else + /* Otherwise, 64 bytes is not uncommon. */ + *isize =3D *dsize =3D 64; +#endif + } +} + +static void __attribute__((constructor)) init_cache_info(void) +{ + int isize =3D 0, dsize =3D 0; + + sys_cache_info(&isize, &dsize); + arch_cache_info(&isize, &dsize); + fallback_cache_info(&isize, &dsize); + + assert((isize & (isize - 1)) =3D=3D 0); + assert((dsize & (dsize - 1)) =3D=3D 0); + + qemu_icache_linesize =3D isize; + qemu_icache_linesize_log =3D ctz32(isize); + qemu_dcache_linesize =3D dsize; + qemu_dcache_linesize_log =3D ctz32(dsize); + + qatomic64_init(); +} + + +/* + * Architecture (+ OS) specific cache flushing mechanisms. + */ + #if defined(__i386__) || defined(__x86_64__) || defined(__s390__) =20 /* Caches are coherent and do not require flushing; symbol inline. */ diff --git a/util/cacheinfo.c b/util/cacheinfo.c deleted file mode 100644 index ab1644d490..0000000000 --- a/util/cacheinfo.c +++ /dev/null @@ -1,200 +0,0 @@ -/* - * cacheinfo.c - helpers to query the host about its caches - * - * Copyright (C) 2017, Emilio G. Cota - * License: GNU GPL, version 2 or later. - * See the COPYING file in the top-level directory. - */ - -#include "qemu/osdep.h" -#include "qemu/host-utils.h" -#include "qemu/atomic.h" -#include "qemu/cacheinfo.h" - -int qemu_icache_linesize =3D 0; -int qemu_icache_linesize_log; -int qemu_dcache_linesize =3D 0; -int qemu_dcache_linesize_log; - -/* - * Operating system specific detection mechanisms. - */ - -#if defined(_WIN32) - -static void sys_cache_info(int *isize, int *dsize) -{ - SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buf; - DWORD size =3D 0; - BOOL success; - size_t i, n; - - /* Check for the required buffer size first. Note that if the zero - size we use for the probe results in success, then there is no - data available; fail in that case. */ - success =3D GetLogicalProcessorInformation(0, &size); - if (success || GetLastError() !=3D ERROR_INSUFFICIENT_BUFFER) { - return; - } - - n =3D size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); - size =3D n * sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); - buf =3D g_new0(SYSTEM_LOGICAL_PROCESSOR_INFORMATION, n); - if (!GetLogicalProcessorInformation(buf, &size)) { - goto fail; - } - - for (i =3D 0; i < n; i++) { - if (buf[i].Relationship =3D=3D RelationCache - && buf[i].Cache.Level =3D=3D 1) { - switch (buf[i].Cache.Type) { - case CacheUnified: - *isize =3D *dsize =3D buf[i].Cache.LineSize; - break; - case CacheInstruction: - *isize =3D buf[i].Cache.LineSize; - break; - case CacheData: - *dsize =3D buf[i].Cache.LineSize; - break; - default: - break; - } - } - } - fail: - g_free(buf); -} - -#elif defined(__APPLE__) -# include -static void sys_cache_info(int *isize, int *dsize) -{ - /* There's only a single sysctl for both I/D cache line sizes. */ - long size; - size_t len =3D sizeof(size); - if (!sysctlbyname("hw.cachelinesize", &size, &len, NULL, 0)) { - *isize =3D *dsize =3D size; - } -} -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -# include -static void sys_cache_info(int *isize, int *dsize) -{ - /* There's only a single sysctl for both I/D cache line sizes. */ - int size; - size_t len =3D sizeof(size); - if (!sysctlbyname("machdep.cacheline_size", &size, &len, NULL, 0)) { - *isize =3D *dsize =3D size; - } -} -#else -/* POSIX */ - -static void sys_cache_info(int *isize, int *dsize) -{ -# ifdef _SC_LEVEL1_ICACHE_LINESIZE - int tmp_isize =3D (int) sysconf(_SC_LEVEL1_ICACHE_LINESIZE); - if (tmp_isize > 0) { - *isize =3D tmp_isize; - } -# endif -# ifdef _SC_LEVEL1_DCACHE_LINESIZE - int tmp_dsize =3D (int) sysconf(_SC_LEVEL1_DCACHE_LINESIZE); - if (tmp_dsize > 0) { - *dsize =3D tmp_dsize; - } -# endif -} -#endif /* sys_cache_info */ - -/* - * Architecture (+ OS) specific detection mechanisms. - */ - -#if defined(__aarch64__) - -static void arch_cache_info(int *isize, int *dsize) -{ - if (*isize =3D=3D 0 || *dsize =3D=3D 0) { - uint64_t ctr; - - /* The real cache geometry is in CCSIDR_EL1/CLIDR_EL1/CSSELR_EL1, - but (at least under Linux) these are marked protected by the - kernel. However, CTR_EL0 contains the minimum linesize in the - entire hierarchy, and is used by userspace cache flushing. */ - asm volatile("mrs\t%0, ctr_el0" : "=3Dr"(ctr)); - if (*isize =3D=3D 0) { - *isize =3D 4 << (ctr & 0xf); - } - if (*dsize =3D=3D 0) { - *dsize =3D 4 << ((ctr >> 16) & 0xf); - } - } -} - -#elif defined(_ARCH_PPC) && defined(__linux__) -# include "elf.h" - -static void arch_cache_info(int *isize, int *dsize) -{ - if (*isize =3D=3D 0) { - *isize =3D qemu_getauxval(AT_ICACHEBSIZE); - } - if (*dsize =3D=3D 0) { - *dsize =3D qemu_getauxval(AT_DCACHEBSIZE); - } -} - -#else -static void arch_cache_info(int *isize, int *dsize) { } -#endif /* arch_cache_info */ - -/* - * ... and if all else fails ... - */ - -static void fallback_cache_info(int *isize, int *dsize) -{ - /* If we can only find one of the two, assume they're the same. */ - if (*isize) { - if (*dsize) { - /* Success! */ - } else { - *dsize =3D *isize; - } - } else if (*dsize) { - *isize =3D *dsize; - } else { -#if defined(_ARCH_PPC) - /* - * For PPC, we're going to use the cache sizes computed for - * flush_idcache_range. Which means that we must use the - * architecture minimum. - */ - *isize =3D *dsize =3D 16; -#else - /* Otherwise, 64 bytes is not uncommon. */ - *isize =3D *dsize =3D 64; -#endif - } -} - -static void __attribute__((constructor)) init_cache_info(void) -{ - int isize =3D 0, dsize =3D 0; - - sys_cache_info(&isize, &dsize); - arch_cache_info(&isize, &dsize); - fallback_cache_info(&isize, &dsize); - - assert((isize & (isize - 1)) =3D=3D 0); - assert((dsize & (dsize - 1)) =3D=3D 0); - - qemu_icache_linesize =3D isize; - qemu_icache_linesize_log =3D ctz32(isize); - qemu_dcache_linesize =3D dsize; - qemu_dcache_linesize_log =3D ctz32(dsize); - - qatomic64_init(); -} diff --git a/util/meson.build b/util/meson.build index 8f16018cd4..4939b0b91c 100644 --- a/util/meson.build +++ b/util/meson.build @@ -27,7 +27,7 @@ util_ss.add(files('envlist.c', 'path.c', 'module.c')) util_ss.add(files('host-utils.c')) util_ss.add(files('bitmap.c', 'bitops.c')) util_ss.add(files('fifo8.c')) -util_ss.add(files('cacheinfo.c', 'cacheflush.c')) +util_ss.add(files('cacheflush.c')) util_ss.add(files('error.c', 'error-report.c')) util_ss.add(files('qemu-print.c')) util_ss.add(files('id.c')) --=20 2.34.1 From nobody Tue May 21 22:53:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1655776240; cv=none; d=zohomail.com; s=zohoarc; b=bLw5rEhwCr1BhQ7x2um/yoC2H/uiLaehcZny+rP+I6q6WhUk71iCxNK1jJqn1lKBNWWussdh1H4kCf7NpKpVeIdtuJMNwKz9LlRDM8cQos79h2JASnSHrg3mBNKqb/tx6ZADLv1voGU1OvNBTiiQwMH83LdWbW8saXJGYznCbZU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655776240; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=L5ak+zhha2NejqhbdG/upUYuAV+/wGYDkUJ8k4RZozs=; b=kzg+FMnEn1Erba8nYQIKv6s+Lmo0LzkmNAU9a08dzzl0eyqB1mYRiV7vSi/uv/omWOCoDrFAhuCb6n1qVOX/KA1ImbSDCkQuEyOY4UTad1XvJhQUizMDA53kyw16DGWgcyT+Yj+xMZ4f/L3ZpHEM+imPDMKhoDYgP3lZyGZYKs4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 165577624096225.12705205603038; Mon, 20 Jun 2022 18:50:40 -0700 (PDT) Received: from localhost ([::1]:32814 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o3T2M-0006B6-A7 for importer@patchew.org; Mon, 20 Jun 2022 21:50:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34896) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3T0V-0003RM-G6 for qemu-devel@nongnu.org; Mon, 20 Jun 2022 21:48:43 -0400 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]:55246) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o3T0T-0001ta-Ol for qemu-devel@nongnu.org; Mon, 20 Jun 2022 21:48:43 -0400 Received: by mail-pj1-x102b.google.com with SMTP id cv13so8984332pjb.4 for ; Mon, 20 Jun 2022 18:48:41 -0700 (PDT) Received: from stoup.. ([2602:47:d49e:3c01:a3e0:8a80:7b85:aea6]) by smtp.gmail.com with ESMTPSA id q11-20020a056a00084b00b0051bc3a2355csm9907875pfk.64.2022.06.20.18.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jun 2022 18:48:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=L5ak+zhha2NejqhbdG/upUYuAV+/wGYDkUJ8k4RZozs=; b=JvVQs7i60aH4yf2xGlaVdht5d12XserxVPEhwGxZ1N/LTj/dkhDokaiP4gZ395NR64 ZOkYKuYIYf2U1cQRa3LcwFdzjWvfT01XtZodCHHmGyfsBrxkcGl7TJMO+jLAD4EoLdQ2 ytX+SEO808o/S7r6EQN9u/qscAWWGSeEjy3TGQEyhbqIn5sXgB20KLwZmbCLLEE/Nk7d PVlxZJokSR12EJZSsxkYjrCwdt6dKnemwCWZNbBjZr3LKkEo1M/8x+V3prNeUSXkz8wL P9pZHiOTNka9zqLbNaGBsbKCrdmQx8QuizF0w7ytN8OIUfqafyxwBoZNSAgV3uqHZVEH JoLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=L5ak+zhha2NejqhbdG/upUYuAV+/wGYDkUJ8k4RZozs=; b=0iXsaZWp+lsGsNJ0d0e5ZVhg6bjZe7ZaHyy/rZihhYtkoPHj093nGZ6i7NgTJ4bGM3 KlqoD5sYf0asmb05CqewRlE0zvoeOwYPhPT3piDHNwD97Z8tn0pBLlmtpxu81iBc5miv lnYLkHPXGRlakn6dBfqxlu+UDwfIS55swiQtS50ppXH1MDZxXCpf7Q9GE8CVvVRSkEHA wPOZ6vBEueJIHy4fpicvsBX2j6SEq9reG2zfZlZKiMCm25kA8Rv1cYhzbkJwFOCNPn4Z MzczNcMSiypMz59Gqhta/s3GI0uMv0aC4P5RDDQcJqmmxQKCtoiNEBBa9IrUGCczS5/A eS1A== X-Gm-Message-State: AJIora+NJSb8zD4iAk3xjCA8+AlWsDj3LuyO7ZS+NgtyTBqbDgd/X82Z 0ALWFD6kEHHF04eiiPUqbQ4ktvwX1wMtow== X-Google-Smtp-Source: AGRyM1vQzPZ8Mv1bJKeNAvo2bWpAX8Go5ULA7Yz/IDkW+Nt7F7L4AgA93UUkMl/fRuS6a4ZUeiflVQ== X-Received: by 2002:a17:902:d652:b0:168:bffe:e5fe with SMTP id y18-20020a170902d65200b00168bffee5femr27193143plh.81.1655776120396; Mon, 20 Jun 2022 18:48:40 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: npiggin@gmail.com, qemu-ppc@nongnu.org Subject: [PATCH v2 2/3] util/cacheflush: Merge aarch64 ctr_el0 usage Date: Mon, 20 Jun 2022 18:48:36 -0700 Message-Id: <20220621014837.189139-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220621014837.189139-1-richard.henderson@linaro.org> References: <20220621014837.189139-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102b; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1655776242199100003 Content-Type: text/plain; charset="utf-8" Merge init_ctr_el0 into arch_cache_info. In flush_idcache_range, use the pre-computed line sizes from the global variables. Use CONFIG_DARWIN in preference to __APPLE__. Signed-off-by: Richard Henderson Reviewed-by: Peter Maydell --- util/cacheflush.c | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/util/cacheflush.c b/util/cacheflush.c index 8096afd33c..01b6cb7583 100644 --- a/util/cacheflush.c +++ b/util/cacheflush.c @@ -70,7 +70,7 @@ static void sys_cache_info(int *isize, int *dsize) g_free(buf); } =20 -#elif defined(__APPLE__) +#elif defined(CONFIG_DARWIN) # include static void sys_cache_info(int *isize, int *dsize) { @@ -117,20 +117,25 @@ static void sys_cache_info(int *isize, int *dsize) * Architecture (+ OS) specific cache detection mechanisms. */ =20 -#if defined(__aarch64__) - +#if defined(__aarch64__) && !defined(CONFIG_DARWIN) +/* Apple does not expose CTR_EL0, so we must use system interfaces. */ +static uint64_t save_ctr_el0; static void arch_cache_info(int *isize, int *dsize) { - if (*isize =3D=3D 0 || *dsize =3D=3D 0) { - uint64_t ctr; + uint64_t ctr; =20 - /* - * The real cache geometry is in CCSIDR_EL1/CLIDR_EL1/CSSELR_EL1, - * but (at least under Linux) these are marked protected by the - * kernel. However, CTR_EL0 contains the minimum linesize in the - * entire hierarchy, and is used by userspace cache flushing. - */ - asm volatile("mrs\t%0, ctr_el0" : "=3Dr"(ctr)); + /* + * The real cache geometry is in CCSIDR_EL1/CLIDR_EL1/CSSELR_EL1, + * but (at least under Linux) these are marked protected by the + * kernel. However, CTR_EL0 contains the minimum linesize in the + * entire hierarchy, and is used by userspace cache flushing. + * + * We will also use this value in flush_idcache_range. + */ + asm volatile("mrs\t%0, ctr_el0" : "=3Dr"(ctr)); + save_ctr_el0 =3D ctr; + + if (*isize =3D=3D 0 || *dsize =3D=3D 0) { if (*isize =3D=3D 0) { *isize =3D 4 << (ctr & 0xf); } @@ -228,17 +233,6 @@ void flush_idcache_range(uintptr_t rx, uintptr_t rw, s= ize_t len) } #else =20 -/* - * TODO: unify this with cacheinfo.c. - * We want to save the whole contents of CTR_EL0, so that we - * have more than the linesize, but also IDC and DIC. - */ -static uint64_t save_ctr_el0; -static void __attribute__((constructor)) init_ctr_el0(void) -{ - asm volatile("mrs\t%0, ctr_el0" : "=3Dr"(save_ctr_el0)); -} - /* * This is a copy of gcc's __aarch64_sync_cache_range, modified * to fit this three-operand interface. @@ -248,8 +242,8 @@ void flush_idcache_range(uintptr_t rx, uintptr_t rw, si= ze_t len) const unsigned CTR_IDC =3D 1u << 28; const unsigned CTR_DIC =3D 1u << 29; const uint64_t ctr_el0 =3D save_ctr_el0; - const uintptr_t icache_lsize =3D 4 << extract64(ctr_el0, 0, 4); - const uintptr_t dcache_lsize =3D 4 << extract64(ctr_el0, 16, 4); + const uintptr_t icache_lsize =3D qemu_icache_linesize; + const uintptr_t dcache_lsize =3D qemu_dcache_linesize; uintptr_t p; =20 /* --=20 2.34.1 From nobody Tue May 21 22:53:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1655776382; cv=none; d=zohomail.com; s=zohoarc; b=Uczf5ek1yT1mrPDX0VIVCvVEPsWKjwLo3WTqnWDY2GZeVqpTosmofvFYe1+OMks+vqhNw7Gs5AAdCMFftTVt2fKEmdptfeCTxBcTzYpgLQqyh78y+Z/B8rPGBKUM0O5NrZFMjerJCgt1pPSmAtYOIivXZrblfQKfNloJkj2NP9A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655776382; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=YBQ+kqYkpbLgtv+WWFdZqAHxgkdKeAvGA4OIxoJYS3s=; b=an8MhuAhvcmMpj5lWwMCrJitWaZ13AUtpgzOlQuNLQZkLGzEn7eYaSH17IVNfx2gLVj9bAPbaJaAuZUd0z6RT7FWOGFDaRjZIQFymJGQxBzg5hQMX84z+7PTU+lQy/XZHra7EAAYnAz/fdCfoN+I0US007vUM7xl3Oh+pCK/EmE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1655776382194109.42954194328968; Mon, 20 Jun 2022 18:53:02 -0700 (PDT) Received: from localhost ([::1]:37984 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o3T4f-0001LX-0F for importer@patchew.org; Mon, 20 Jun 2022 21:53:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34920) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3T0Y-0003VQ-1T for qemu-devel@nongnu.org; Mon, 20 Jun 2022 21:48:46 -0400 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]:39901) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o3T0U-0001tu-LT for qemu-devel@nongnu.org; Mon, 20 Jun 2022 21:48:45 -0400 Received: by mail-pg1-x535.google.com with SMTP id q140so11747792pgq.6 for ; Mon, 20 Jun 2022 18:48:42 -0700 (PDT) Received: from stoup.. ([2602:47:d49e:3c01:a3e0:8a80:7b85:aea6]) by smtp.gmail.com with ESMTPSA id q11-20020a056a00084b00b0051bc3a2355csm9907875pfk.64.2022.06.20.18.48.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jun 2022 18:48:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YBQ+kqYkpbLgtv+WWFdZqAHxgkdKeAvGA4OIxoJYS3s=; b=FrjhT/hcROvAqbDtmVHCj5H/y+MsD9/vID6h/78dVv9qKhqjgNxer/h2y/vhdGKmME ehTjWhPPueqfzIP+ydcEuLX/XQFHKKFaA0rozMBKFo4CDBMAt+MLdv1n6jy+E9y3Y1/o yc8CI/TWOJWQ4qAtaOsUdrdKaQDZNXOh3gC8pq/2hdlXxHJOYlinQH3FFmjqVZAJ4szG MUEFzjiQdIQknYStqaMQbJzDXfMdSuwrWEGEmBlhs7lf/QAZWc57vkLcNTdjO+wUvsgv qtsQFVG4tLMMcLKUP2FD3kSWE4rbB43gUeEL1uOKQybQrkoD2Oju0kjdw6HrK6ex2m47 XGiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YBQ+kqYkpbLgtv+WWFdZqAHxgkdKeAvGA4OIxoJYS3s=; b=CqC1kmOVc5sU8gzsBc2DaB8HLmOgqDPfr0e/2av4HHIAnkItNHQwTTkK9MZOoUAZK7 tqTYq1m+o461IPEAqlnJ5Jm+o65j1iks0KF2uvbSAjQR/yH2Q8M2JL3HuqlBu816KLns Rc2w11WfmXt2SxJhOn0Sn4Oc+3xH1jrmN5c8mivvBWjtQPqDSCaLEK4Iw5MRtWBqIB7P cEQOUtkP69zB7tD7q1246RV4VRvpDgZGhPaaFdU2Yr+VtOc2LYEkj01qoCG8sHDMblg+ vI0eENT1rvCaV+fmKI7/pWMGAFxVzJRTajAgCFpztCTdO5CNQdZ/vh4hcJWe/u2zPtoq hNXQ== X-Gm-Message-State: AJIora8R2nMiImOFJjkJ+l2vztBscnM38tVbIhrsCbkho/cIH6EXRlMv LFADM3B07I51g86PPKVfatSTXlGSQ9Emqg== X-Google-Smtp-Source: AGRyM1vhVHQ4lpRwllfEGyihaeV3XlOcqjc6RLTiFBweQhBA3uSOdPcNRjarjT9W43eBMCLxtYccaw== X-Received: by 2002:a05:6a00:16c7:b0:520:6ede:2539 with SMTP id l7-20020a056a0016c700b005206ede2539mr27321525pfc.46.1655776121220; Mon, 20 Jun 2022 18:48:41 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: npiggin@gmail.com, qemu-ppc@nongnu.org Subject: [PATCH v2 3/3] util/cacheflush: Optimize flushing when ppc host has coherent icache Date: Mon, 20 Jun 2022 18:48:37 -0700 Message-Id: <20220621014837.189139-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220621014837.189139-1-richard.henderson@linaro.org> References: <20220621014837.189139-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::535; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x535.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1655776382834100001 Content-Type: text/plain; charset="utf-8" From: Nicholas Piggin On linux, the AT_HWCAP bit PPC_FEATURE_ICACHE_SNOOP indicates that we can use a simplified 3 instruction flush sequence. Signed-off-by: Nicholas Piggin Message-Id: <20220519141131.29839-1-npiggin@gmail.com> [rth: update after merging cacheflush.c and cacheinfo.c] Signed-off-by: Richard Henderson Reviewed-by: Peter Maydell --- util/cacheflush.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/util/cacheflush.c b/util/cacheflush.c index 01b6cb7583..2c2c73e085 100644 --- a/util/cacheflush.c +++ b/util/cacheflush.c @@ -117,6 +117,10 @@ static void sys_cache_info(int *isize, int *dsize) * Architecture (+ OS) specific cache detection mechanisms. */ =20 +#if defined(__powerpc__) +static bool have_coherent_icache; +#endif + #if defined(__aarch64__) && !defined(CONFIG_DARWIN) /* Apple does not expose CTR_EL0, so we must use system interfaces. */ static uint64_t save_ctr_el0; @@ -156,6 +160,7 @@ static void arch_cache_info(int *isize, int *dsize) if (*dsize =3D=3D 0) { *dsize =3D qemu_getauxval(AT_DCACHEBSIZE); } + have_coherent_icache =3D qemu_getauxval(AT_HWCAP) & PPC_FEATURE_ICACHE= _SNOOP; } =20 #else @@ -298,8 +303,24 @@ void flush_idcache_range(uintptr_t rx, uintptr_t rw, s= ize_t len) void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len) { uintptr_t p, b, e; - size_t dsize =3D qemu_dcache_linesize; - size_t isize =3D qemu_icache_linesize; + size_t dsize, isize; + + /* + * Some processors have coherent caches and support a simplified + * flushing procedure. See + * POWER9 UM, 4.6.2.2 Instruction Cache Block Invalidate (icbi)=20 + * https://ibm.ent.box.com/s/tmklq90ze7aj8f4n32er1mu3sy9u8k3k + */ + if (have_coherent_icache) { + asm volatile ("sync\n\t" + "icbi 0,%0\n\t" + "isync" + : : "r"(rx) : "memory"); + return; + } + + dsize =3D qemu_dcache_linesize; + isize =3D qemu_icache_linesize; =20 b =3D rw & ~(dsize - 1); e =3D (rw + len + dsize - 1) & ~(dsize - 1); --=20 2.34.1