| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <stdint.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <string.h> |
| 14 |
#include <sys/auxv.h> |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#define _IFC_REG(n) \ |
| 39 |
".ifc \\r, $" #n "\n\t" \ |
| 40 |
"\\var = " #n "\n\t" \ |
| 41 |
".endif\n\t" |
| 42 |
|
| 43 |
__asm__(".macro parse_r var r\n\t" |
| 44 |
"\\var = -1\n\t" |
| 45 |
_IFC_REG(0) _IFC_REG(1) _IFC_REG(2) _IFC_REG(3) |
| 46 |
_IFC_REG(4) _IFC_REG(5) _IFC_REG(6) _IFC_REG(7) |
| 47 |
_IFC_REG(8) _IFC_REG(9) _IFC_REG(10) _IFC_REG(11) |
| 48 |
_IFC_REG(12) _IFC_REG(13) _IFC_REG(14) _IFC_REG(15) |
| 49 |
_IFC_REG(16) _IFC_REG(17) _IFC_REG(18) _IFC_REG(19) |
| 50 |
_IFC_REG(20) _IFC_REG(21) _IFC_REG(22) _IFC_REG(23) |
| 51 |
_IFC_REG(24) _IFC_REG(25) _IFC_REG(26) _IFC_REG(27) |
| 52 |
_IFC_REG(28) _IFC_REG(29) _IFC_REG(30) _IFC_REG(31) |
| 53 |
".iflt \\var\n\t" |
| 54 |
".error \"Unable to parse register name \\r\"\n\t" |
| 55 |
".endif\n\t" |
| 56 |
".endm"); |
| 57 |
|
| 58 |
#define HWCAP_LOONGSON_CPUCFG (1 << 14) |
| 59 |
|
| 60 |
static int |
| 61 |
cpucfg_available(void) |
| 62 |
{ |
| 63 |
return getauxval(AT_HWCAP) & HWCAP_LOONGSON_CPUCFG; |
| 64 |
} |
| 65 |
|
| 66 |
static int |
| 67 |
strstart(const char *str, const char *pfx, const char **ptr) |
| 68 |
{ |
| 69 |
while (*pfx && *pfx == *str) { |
| 70 |
pfx++; |
| 71 |
str++; |
| 72 |
} |
| 73 |
if (!*pfx && ptr) |
| 74 |
*ptr = str; |
| 75 |
return !*pfx; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
static uint32_t |
| 80 |
read_cpucfg(uint32_t reg) |
| 81 |
{ |
| 82 |
uint32_t __res; |
| 83 |
|
| 84 |
__asm__ __volatile__( |
| 85 |
"parse_r __res,%0\n\t" |
| 86 |
"parse_r reg,%1\n\t" |
| 87 |
".insn \n\t" |
| 88 |
".word (0xc8080118 | (reg << 21) | (__res << 11))\n\t" |
| 89 |
:"=r"(__res) |
| 90 |
:"r"(reg) |
| 91 |
: |
| 92 |
); |
| 93 |
return __res; |
| 94 |
} |
| 95 |
|
| 96 |
#define LOONGSON_CFG1 0x1 |
| 97 |
|
| 98 |
#define LOONGSON_CFG1_MMI (1 << 4) |
| 99 |
|
| 100 |
static int |
| 101 |
cpu_flags_cpucfg(void) |
| 102 |
{ |
| 103 |
int flags = 0; |
| 104 |
uint32_t cfg1 = read_cpucfg(LOONGSON_CFG1); |
| 105 |
|
| 106 |
if (cfg1 & LOONGSON_CFG1_MMI) |
| 107 |
flags = 1; |
| 108 |
|
| 109 |
return flags; |
| 110 |
} |
| 111 |
|
| 112 |
static int |
| 113 |
cpu_flags_cpuinfo(void) |
| 114 |
{ |
| 115 |
FILE *f = fopen("/proc/cpuinfo", "r"); |
| 116 |
char buf[200]; |
| 117 |
int flags = 0; |
| 118 |
|
| 119 |
if (!f) |
| 120 |
return flags; |
| 121 |
|
| 122 |
while (fgets(buf, sizeof(buf), f)) { |
| 123 |
|
| 124 |
if (strstart(buf, "cpu model", NULL)) { |
| 125 |
if (strstr(buf, "Loongson-3 ")) |
| 126 |
flags = 1; |
| 127 |
break; |
| 128 |
} |
| 129 |
if (strstart(buf, "ASEs implemented", NULL)) { |
| 130 |
if (strstr(buf, " loongson-mmi")) |
| 131 |
flags = 1; |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
fclose(f); |
| 136 |
return flags; |
| 137 |
} |
| 138 |
|
| 139 |
static int |
| 140 |
png_have_mmi() |
| 141 |
{ |
| 142 |
if (cpucfg_available()) |
| 143 |
return cpu_flags_cpucfg(); |
| 144 |
else |
| 145 |
return cpu_flags_cpuinfo(); |
| 146 |
return 0; |
| 147 |
} |