@@ -38,22 +38,48 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3838#include <sys/resource.h>
3939#include "common.h"
4040
41+ #if (defined OS_LINUX || defined OS_ANDROID )
42+ #include <asm/hwcap.h>
43+ #include <sys/auxv.h>
44+
45+ #ifndef HWCAP_LOONGSON_CPUCFG
46+ #define HWCAP_LOONGSON_CPUCFG (1 << 14)
47+ #endif
48+ #endif
49+
50+ #ifdef DYNAMIC_LIST
51+ extern gotoblas_t gotoblas_MIPS64_GENERIC ;
52+ #ifdef DYN_LOONGSON3R3
4153extern gotoblas_t gotoblas_LOONGSON3R3 ;
54+ #else
55+ #define gotoblas_LOONGSON3R3 gotoblas_MIPS64_GENERIC
56+ #endif
57+ #ifdef DYN_LOONGSON3R4
4258extern gotoblas_t gotoblas_LOONGSON3R4 ;
59+ #else
60+ #define gotoblas_LOONGSON3R4 gotoblas_MIPS64_GENERIC
61+ #endif
62+ #else
63+ extern gotoblas_t gotoblas_LOONGSON3R3 ;
64+ extern gotoblas_t gotoblas_LOONGSON3R4 ;
65+ extern gotoblas_t gotoblas_MIPS64_GENERIC ;
66+ #endif
4367
4468extern void openblas_warning (int verbose , const char * msg );
4569
46- #define NUM_CORETYPES 2
70+ #define NUM_CORETYPES 3
4771
4872static char * corename [] = {
73+ "MIPS64_GENERIC"
4974 "loongson3r3" ,
5075 "loongson3r4" ,
5176 "UNKNOWN"
5277};
5378
5479char * gotoblas_corename (void ) {
55- if (gotoblas == & gotoblas_LOONGSON3R3 ) return corename [0 ];
56- if (gotoblas == & gotoblas_LOONGSON3R4 ) return corename [1 ];
80+ if (gotoblas == & gotoblas_MIPS64_GENERIC ) return corename [0 ];
81+ if (gotoblas == & gotoblas_LOONGSON3R3 ) return corename [1 ];
82+ if (gotoblas == & gotoblas_LOONGSON3R4 ) return corename [2 ];
5783 return corename [NUM_CORETYPES ];
5884}
5985
@@ -73,77 +99,32 @@ static gotoblas_t *force_coretype(char *coretype) {
7399
74100 switch (found )
75101 {
76- case 0 : return (& gotoblas_LOONGSON3R3 );
77- case 1 : return (& gotoblas_LOONGSON3R4 );
102+ case 0 : return (& gotoblas_MIPS64_GENERIC );
103+ case 1 : return (& gotoblas_LOONGSON3R3 );
104+ case 2 : return (& gotoblas_LOONGSON3R4 );
78105 }
79106 snprintf (message , 128 , "Core not found: %s\n" , coretype );
80107 openblas_warning (1 , message );
81108 return NULL ;
82109}
83110
111+ #if (defined OS_LINUX || defined OS_ANDROID )
84112#define MMI_MASK 0x00000010
85113#define MSA_MASK 0x00000020
86114
87- int fd [2 ];
88- int support_cpucfg ;
89-
90- static void handler (int signum )
91- {
92- close (fd [1 ]);
93- exit (1 );
94- }
95-
96- /* Brief : Function to check if cpucfg supported on loongson
97- * Return: 1 supported
98- * 0 not supported
99- */
100- static int cpucfg_test (void ) {
101- pid_t pid ;
102- int status = 0 ;
103-
104- support_cpucfg = 0 ;
105- pipe (fd );
106- pid = fork ();
107- if (pid == 0 ) { /* Subprocess */
108- struct sigaction act ;
109- close (fd [0 ]);
110- /* Set signal action for SIGILL. */
111- act .sa_handler = handler ;
112- sigaction (SIGILL ,& act ,NULL );
113-
114- /* Execute cpucfg in subprocess. */
115- __asm__ volatile (
116- ".insn \n\t"
117- ".word (0xc8080118) \n\t"
118- :::
119- );
120- support_cpucfg = 1 ;
121- write (fd [1 ],& support_cpucfg ,sizeof (support_cpucfg ));
122- close (fd [1 ]);
123- exit (0 );
124- } else if (pid > 0 ){ /* Parent process*/
125- close (fd [1 ]);
126- if ((waitpid (pid ,& status ,0 ) <= 0 ) ||
127- (read (fd [0 ],& support_cpucfg ,sizeof (support_cpucfg )) <= 0 ))
128- support_cpucfg = 0 ;
129- close (fd [0 ]);
130- } else {
131- support_cpucfg = 0 ;
132- }
133-
134- return support_cpucfg ;
135- }
136-
137115static gotoblas_t * get_coretype_from_cpucfg (void ) {
138116 int flag = 0 ;
139117 __asm__ volatile (
118+ ".set push \n\t"
119+ ".set noat \n\t"
140120 ".insn \n\t"
141- "dli $8, 0x01 \n\t"
142- ".word (0xc9084918) \n\t"
143- "usw $9, 0x00(%0) \n\t"
121+ "dli $1, 0x01 \n\t"
122+ ".word (0xc8080118) \n\t"
123+ "move %0, $1 \n\t"
124+ ".set pop \n\t"
125+ : "=r" (flag )
126+ :
144127 :
145- : "r" (& flag )
146- : "memory"
147128 );
148129 if (flag & MSA_MASK )
149130 return (& gotoblas_LOONGSON3R4 );
@@ -153,7 +134,7 @@ static gotoblas_t *get_coretype_from_cpucfg(void) {
153134}
154135
155136static gotoblas_t * get_coretype_from_cpuinfo (void ) {
156- #ifdef linux
137+ #ifdef __linux
157138 FILE * infile ;
158139 char buffer [512 ], * p ;
159140
@@ -176,17 +157,19 @@ static gotoblas_t *get_coretype_from_cpuinfo(void) {
176157 return NULL ;
177158 }
178159#endif
179- return NULL ;
160+ return NULL ;
180161}
162+ #endif
181163
182164static gotoblas_t * get_coretype (void ) {
183- int ret = 0 ;
184-
185- ret = cpucfg_test ();
186- if (ret == 1 )
187- return get_coretype_from_cpucfg ();
188- else
189- return get_coretype_from_cpuinfo ();
165+ #if (!defined OS_LINUX && !defined OS_ANDROID )
166+ return NULL ;
167+ #else
168+ if (!(getauxval (AT_HWCAP ) & HWCAP_LOONGSON_CPUCFG ))
169+ return get_coretype_from_cpucfg ();
170+ else
171+ return get_coretype_from_cpuinfo ();
172+ #endif
190173}
191174
192175void gotoblas_dynamic_init (void ) {
@@ -208,9 +191,9 @@ void gotoblas_dynamic_init(void) {
208191
209192 if (gotoblas == NULL )
210193 {
211- snprintf (coremsg , 128 , "Falling back to loongson3r3 core \n" );
194+ snprintf (coremsg , 128 , "Falling back to MIPS64_GENEIRC \n" );
212195 openblas_warning (1 , coremsg );
213- gotoblas = & gotoblas_LOONGSON3R3 ;
196+ gotoblas = & gotoblas_MIPS64_GENERIC ;
214197 }
215198
216199 if (gotoblas && gotoblas -> init ) {
0 commit comments