|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +#include <stdint.h> |
| 19 | +#include "pgcpuid.h" |
| 20 | + |
| 21 | +/* |
| 22 | + * Note: |
| 23 | + * 1) these functions cannot call any other function |
| 24 | + * 2) these functions can only use GPR (not floating point) |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +/** @brief returns false/true if CPUID supports eax function. |
| 29 | + * __pgi_cpuid_getma (uint32_t cpuid_func) |
| 30 | + * @param cpuid_func (I1) function to execute CPUID with |
| 31 | + * |
| 32 | + * Returns false(0)/true(1) |
| 33 | + * |
| 34 | + */ |
| 35 | + |
| 36 | +int |
| 37 | +__pgi_cpuid_getmax(uint32_t f) |
| 38 | +{ |
| 39 | + uint32_t maxcpueax; |
| 40 | + uint32_t fin = f & 0x80000000; |
| 41 | + asm("\tcpuid" |
| 42 | + : "=a"(maxcpueax) |
| 43 | + : "0"(fin) |
| 44 | + : "ebx", "ecx", "edx" |
| 45 | + ); |
| 46 | + return f <= maxcpueax; |
| 47 | +} |
| 48 | + |
| 49 | +/** @brief returns results of executing CPUID with function cpuid_func and |
| 50 | + * sub function ecx. |
| 51 | + * __pgi_cpuid_ecx(uint32_t cpuid_func, uint32_t *res, uint32_t ecx) |
| 52 | + * @param cpuid_func (I1) function to execute CPUID with |
| 53 | + * @param res (I2) pointer to buffer to store eax, ebx, ecx, edx |
| 54 | + * @param ecx (I3) value of %ecx to execute CPUID with |
| 55 | + * |
| 56 | + * Returns false(0): if cpuid_func not supported |
| 57 | + * true(1): CPUID successfully executed with cpuid_func+ecx and: |
| 58 | + * res[0]=%eax, res[1]=%ebx, res[2]=%ecx, res[3]=%edx |
| 59 | + * |
| 60 | + */ |
| 61 | + |
| 62 | +int |
| 63 | +__pgi_cpuid_ecx(uint32_t f, uint32_t *r, uint32_t c) |
| 64 | +{ |
| 65 | + if (__pgi_cpuid_getmax(f) == 0) return 0; |
| 66 | + asm("\tcpuid" |
| 67 | + : "=a"(r[0]), "=b"(r[1]), "=c"(r[2]), "=d"(r[3]) |
| 68 | + : "0"(f), "2"(c) |
| 69 | + : |
| 70 | + ); |
| 71 | + return 1; |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +/** @brief returns results of executing CPUID with function cpuid_func. |
| 76 | + * __pgi_cpuid(uint32_t cpuid_func, uint32_t *res) |
| 77 | + * @param cpuid_func (I1) function to execute CPUID with |
| 78 | + * @param res (I2) pointer to buffer to store eax, ebx, ecx, edx |
| 79 | + * |
| 80 | + * Returns false(0): if cpuid_func not supported |
| 81 | + * true(1): CPUID successfully executed with cpuid_func and: |
| 82 | + * res[0]=%eax, res[1]=%ebx, res[2]=%ecx, res[3]=%edx |
| 83 | + * |
| 84 | + */ |
| 85 | + |
| 86 | +int |
| 87 | +__pgi_cpuid(uint32_t f, uint32_t *r) |
| 88 | +{ |
| 89 | + return __pgi_cpuid_ecx(f, r, 0); |
| 90 | +} |
| 91 | + |
| 92 | +/** @brief returns results of executing CPUID with function cpuid_func. |
| 93 | + * __pgcpuid(uint32_t cpuid_func, uint32_t *res) |
| 94 | + * @param cpuid_func (I1) function to execute CPUID with |
| 95 | + * @param res (I2) pointer to buffer to store eax, ebx, ecx, edx |
| 96 | + * |
| 97 | + * Returns false(0): if cpuid_func not supported |
| 98 | + * true(1): CPUID successfully executed with cpuid_func and: |
| 99 | + * res[0]=%eax, res[1]=%ebx, res[2]=%ecx, res[3]=%edx |
| 100 | + * |
| 101 | + */ |
| 102 | + |
| 103 | +int |
| 104 | +__pgcpuid(uint32_t f, uint32_t *r) |
| 105 | +{ |
| 106 | + return __pgi_cpuid_ecx(f, r, 0); |
| 107 | +} |
| 108 | + |
| 109 | +/** @brief read extended control register. |
| 110 | + * __pgi_getbv(uint32_t xcr_num, uint64_t *xcr_res) |
| 111 | + * @param xcr_num (I1) extended control register number to read |
| 112 | + * @param xcr_res (I2) pointer to buffer to store xcr[xcr_num] |
| 113 | + * |
| 114 | + * Returns true(1) with: |
| 115 | + * xcr_res[31: 0]=%eax |
| 116 | + * xcr_res[63:32]=%edx |
| 117 | + * |
| 118 | + */ |
| 119 | +int |
| 120 | +__pgi_getbv(uint32_t f, uint64_t *r) |
| 121 | +{ |
| 122 | + uint32_t *u32; |
| 123 | + u32 = (uint32_t *)r; |
| 124 | + asm( |
| 125 | +#if defined(__WIN64) |
| 126 | +"\t.byte\t0x0f, 0x01, 0xd0" |
| 127 | +#else |
| 128 | +"\txgetbv" |
| 129 | +#endif |
| 130 | + : "=a"(u32[0]), "=d"(u32[1]) |
| 131 | + : "c"(f) |
| 132 | + : |
| 133 | + ); |
| 134 | + return 1; |
| 135 | +} |
0 commit comments