1*4568490cSriastradh /* $NetBSD: cpufeature.h,v 1.9 2021/12/19 11:52:47 riastradh Exp $ */
2d865091bSriastradh
3d865091bSriastradh /*-
4d865091bSriastradh * Copyright (c) 2018 The NetBSD Foundation, Inc.
5d865091bSriastradh * All rights reserved.
6d865091bSriastradh *
7d865091bSriastradh * This code is derived from software contributed to The NetBSD Foundation
8d865091bSriastradh * by Taylor R. Campbell.
9d865091bSriastradh *
10d865091bSriastradh * Redistribution and use in source and binary forms, with or without
11d865091bSriastradh * modification, are permitted provided that the following conditions
12d865091bSriastradh * are met:
13d865091bSriastradh * 1. Redistributions of source code must retain the above copyright
14d865091bSriastradh * notice, this list of conditions and the following disclaimer.
15d865091bSriastradh * 2. Redistributions in binary form must reproduce the above copyright
16d865091bSriastradh * notice, this list of conditions and the following disclaimer in the
17d865091bSriastradh * documentation and/or other materials provided with the distribution.
18d865091bSriastradh *
19d865091bSriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d865091bSriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d865091bSriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d865091bSriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d865091bSriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d865091bSriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d865091bSriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d865091bSriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d865091bSriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d865091bSriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d865091bSriastradh * POSSIBILITY OF SUCH DAMAGE.
30d865091bSriastradh */
31d865091bSriastradh
32d865091bSriastradh #ifndef _LINUX_ASM_CPUFEATURE_H_
33d865091bSriastradh #define _LINUX_ASM_CPUFEATURE_H_
34d865091bSriastradh
35*4568490cSriastradh #include <sys/cpu.h>
36d865091bSriastradh
37d865091bSriastradh #if defined(__i386__) || defined(__x86_64__)
387c0adb77Sriastradh
399c351cd5Sriastradh #include <x86/specialreg.h>
409c351cd5Sriastradh
4160236c8cSmaxv #define cpu_has_clflush ((cpu_info_primary.ci_feat_val[0] & CPUID_CLFSH) != 0)
42362f7ddeSriastradh #define cpu_has_pat ((cpu_info_primary.ci_feat_val[0] & CPUID_PAT) != 0)
437c0adb77Sriastradh
440dbebf08Sriastradh #define X86_FEATURE_CLFLUSH 0
45637492a7Sriastradh #define X86_FEATURE_PAT 1
460dbebf08Sriastradh
470dbebf08Sriastradh static inline bool
static_cpu_has(int feature)480dbebf08Sriastradh static_cpu_has(int feature)
490dbebf08Sriastradh {
500dbebf08Sriastradh switch (feature) {
510dbebf08Sriastradh case X86_FEATURE_CLFLUSH:
520dbebf08Sriastradh return cpu_has_clflush;
53637492a7Sriastradh case X86_FEATURE_PAT:
54637492a7Sriastradh return cpu_has_pat;
550dbebf08Sriastradh default:
560dbebf08Sriastradh return false;
570dbebf08Sriastradh }
580dbebf08Sriastradh }
590dbebf08Sriastradh
60637492a7Sriastradh #define boot_cpu_has static_cpu_has
61637492a7Sriastradh
624f0a6669Sriastradh static inline size_t
cache_line_size(void)634f0a6669Sriastradh cache_line_size(void)
644f0a6669Sriastradh {
654f0a6669Sriastradh return cpu_info_primary.ci_cflush_lsize;
664f0a6669Sriastradh }
677c0adb77Sriastradh
680dbebf08Sriastradh static inline void
clflush(const void * p)690dbebf08Sriastradh clflush(const void *p)
700dbebf08Sriastradh {
710dbebf08Sriastradh asm volatile ("clflush %0" : : "m" (*(const char *)p));
720dbebf08Sriastradh }
730dbebf08Sriastradh
747c0adb77Sriastradh #endif /* x86 */
75d865091bSriastradh
76d865091bSriastradh #endif /* _LINUX_ASM_CPUFEATURE_H_ */
77