Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #ifndef ARCH_CPUID_H |
| 4 | #define ARCH_CPUID_H |
| 5 | |
| 6 | #include <types.h> |
| 7 | |
| 8 | struct cpuid_result { |
| 9 | uint32_t eax; |
| 10 | uint32_t ebx; |
| 11 | uint32_t ecx; |
| 12 | uint32_t edx; |
| 13 | }; |
| 14 | |
| 15 | /* |
| 16 | * Generic CPUID function |
| 17 | */ |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 18 | static inline struct cpuid_result cpuid(const uint32_t eax) |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 19 | { |
| 20 | struct cpuid_result result; |
| 21 | asm volatile( |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 22 | "cpuid;" |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 23 | : "=a" (result.eax), |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 24 | "=b" (result.ebx), |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 25 | "=c" (result.ecx), |
| 26 | "=d" (result.edx) |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 27 | : "a" (eax)); |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 28 | return result; |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * Generic Extended CPUID function |
| 33 | */ |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 34 | static inline struct cpuid_result cpuid_ext(const uint32_t eax, const uint32_t ecx) |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 35 | { |
| 36 | struct cpuid_result result; |
| 37 | asm volatile( |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 38 | "cpuid;" |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 39 | : "=a" (result.eax), |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 40 | "=b" (result.ebx), |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 41 | "=c" (result.ecx), |
| 42 | "=d" (result.edx) |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 43 | : "a" (eax), "c" (ecx)); |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 44 | return result; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * CPUID functions returning a single datum |
| 49 | */ |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 50 | static inline uint32_t cpuid_eax(uint32_t eax) |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 51 | { |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 52 | asm volatile( |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 53 | "cpuid;" |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 54 | : "+a" (eax) |
| 55 | :: "ebx", "ecx", "edx"); |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 56 | return eax; |
| 57 | } |
| 58 | |
Patrick Rudolph | 699b1c4 | 2023-09-30 15:12:17 +0200 | [diff] [blame^] | 59 | static inline uint32_t cpuid_ebx(uint32_t eax) |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 60 | { |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 61 | uint32_t ebx; |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 62 | |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 63 | asm volatile( |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 64 | "cpuid;" |
Patrick Rudolph | 699b1c4 | 2023-09-30 15:12:17 +0200 | [diff] [blame^] | 65 | : "=b" (ebx), "+a" (eax) |
| 66 | :: "ecx", "edx"); |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 67 | return ebx; |
| 68 | } |
| 69 | |
Patrick Rudolph | 699b1c4 | 2023-09-30 15:12:17 +0200 | [diff] [blame^] | 70 | static inline uint32_t cpuid_ecx(uint32_t eax) |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 71 | { |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 72 | uint32_t ecx; |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 73 | |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 74 | asm volatile( |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 75 | "cpuid;" |
Patrick Rudolph | 699b1c4 | 2023-09-30 15:12:17 +0200 | [diff] [blame^] | 76 | : "=c" (ecx), "+a" (eax) |
| 77 | :: "ebx", "edx"); |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 78 | return ecx; |
| 79 | } |
| 80 | |
Patrick Rudolph | 699b1c4 | 2023-09-30 15:12:17 +0200 | [diff] [blame^] | 81 | static inline uint32_t cpuid_edx(uint32_t eax) |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 82 | { |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 83 | uint32_t edx; |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 84 | |
Matei Dibu | 516eff0 | 2022-08-02 00:48:59 +0300 | [diff] [blame] | 85 | asm volatile( |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 86 | "cpuid;" |
Patrick Rudolph | 699b1c4 | 2023-09-30 15:12:17 +0200 | [diff] [blame^] | 87 | : "=d" (edx), "+a" (eax) |
| 88 | :: "ebx", "ecx"); |
Martin Roth | 568670f9 | 2022-09-17 15:52:36 -0600 | [diff] [blame] | 89 | return edx; |
| 90 | } |
| 91 | |
| 92 | #endif /* ARCH_CPUID_H */ |