Debian Codegraph @

compat/libpng/contrib/mips-mmi/linux.c

raw symbols SCIP
1 /* contrib/mips-mmi/linux.c
2 *
3 * Copyright (c) 2024 Cosmin Truta
4 * Written by guxiwei, 2023
5 *
6 * This code is released under the libpng license.
7 * For conditions of distribution and use, see the disclaimer
8 * and license in png.h
9 */
10
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/auxv.h>
15
16 /*
17 * parse_r var, r - Helper assembler macro for parsing register names.
18 *
19 * This converts the register name in $n form provided in \r to the
20 * corresponding register number, which is assigned to the variable \var. It is
21 * needed to allow explicit encoding of instructions in inline assembly where
22 * registers are chosen by the compiler in $n form, allowing us to avoid using
23 * fixed register numbers.
24 *
25 * It also allows newer instructions (not implemented by the assembler) to be
26 * transparently implemented using assembler macros, instead of needing separate
27 * cases depending on toolchain support.
28 *
29 * Simple usage example:
30 * __asm__ __volatile__("parse_r __rt, %0\n\t"
31 * ".insn\n\t"
32 * "# di %0\n\t"
33 * ".word (0x41606000 | (__rt << 16))"
34 * : "=r" (status);
35 */
36
37 /* Match an individual register number and assign to \var */
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 /* Most toolchains have no CPUCFG support yet */
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 /* Legacy kernel may not export MMI in ASEs implemented */
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 }