riscv64: Add RISC-V target
[platform/upstream/openblas.git] / cpuid_mips64.c
1 /*****************************************************************************
2 Copyright (c) 2011-2014, The OpenBLAS Project
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9    1. Redistributions of source code must retain the above copyright
10       notice, this list of conditions and the following disclaimer.
11
12    2. Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in
14       the documentation and/or other materials provided with the
15       distribution.
16    3. Neither the name of the OpenBLAS project nor the names of 
17       its contributors may be used to endorse or promote products 
18       derived from this software without specific prior written 
19       permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 **********************************************************************************/
33
34
35 /*********************************************************************/
36 /* Copyright 2009, 2010 The University of Texas at Austin.           */
37 /* All rights reserved.                                              */
38 /*                                                                   */
39 /* Redistribution and use in source and binary forms, with or        */
40 /* without modification, are permitted provided that the following   */
41 /* conditions are met:                                               */
42 /*                                                                   */
43 /*   1. Redistributions of source code must retain the above         */
44 /*      copyright notice, this list of conditions and the following  */
45 /*      disclaimer.                                                  */
46 /*                                                                   */
47 /*   2. Redistributions in binary form must reproduce the above      */
48 /*      copyright notice, this list of conditions and the following  */
49 /*      disclaimer in the documentation and/or other materials       */
50 /*      provided with the distribution.                              */
51 /*                                                                   */
52 /*    THIS  SOFTWARE IS PROVIDED  BY THE  UNIVERSITY OF  TEXAS AT    */
53 /*    AUSTIN  ``AS IS''  AND ANY  EXPRESS OR  IMPLIED WARRANTIES,    */
54 /*    INCLUDING, BUT  NOT LIMITED  TO, THE IMPLIED  WARRANTIES OF    */
55 /*    MERCHANTABILITY  AND FITNESS FOR  A PARTICULAR  PURPOSE ARE    */
56 /*    DISCLAIMED.  IN  NO EVENT SHALL THE UNIVERSITY  OF TEXAS AT    */
57 /*    AUSTIN OR CONTRIBUTORS BE  LIABLE FOR ANY DIRECT, INDIRECT,    */
58 /*    INCIDENTAL,  SPECIAL, EXEMPLARY,  OR  CONSEQUENTIAL DAMAGES    */
59 /*    (INCLUDING, BUT  NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE    */
60 /*    GOODS  OR  SERVICES; LOSS  OF  USE,  DATA,  OR PROFITS;  OR    */
61 /*    BUSINESS INTERRUPTION) HOWEVER CAUSED  AND ON ANY THEORY OF    */
62 /*    LIABILITY, WHETHER  IN CONTRACT, STRICT  LIABILITY, OR TORT    */
63 /*    (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY WAY OUT    */
64 /*    OF  THE  USE OF  THIS  SOFTWARE,  EVEN  IF ADVISED  OF  THE    */
65 /*    POSSIBILITY OF SUCH DAMAGE.                                    */
66 /*                                                                   */
67 /* The views and conclusions contained in the software and           */
68 /* documentation are those of the authors and should not be          */
69 /* interpreted as representing official policies, either expressed   */
70 /* or implied, of The University of Texas at Austin.                 */
71 /*********************************************************************/
72
73 #define CPU_UNKNOWN      0
74 #define CPU_SICORTEX     1
75 #define CPU_LOONGSON3R3  2
76 #define CPU_LOONGSON3R4  3
77 #define CPU_I6400        4
78 #define CPU_P6600        5
79 #define CPU_I6500        6
80
81 static char *cpuname[] = {
82   "UNKNOWN",
83   "SICORTEX",
84   "LOONGSON3R3",
85   "LOONGSON3R4",
86   "I6400",
87   "P6600",
88   "I6500"
89 };
90
91 int detect(void){
92
93 #ifdef linux
94   FILE *infile;
95   char buffer[512], *p;
96
97   p = (char *)NULL;
98   //Check model name for Loongson3
99   infile = fopen("/proc/cpuinfo", "r");
100   while (fgets(buffer, sizeof(buffer), infile)){
101     if (!strncmp("model name", buffer, 10)){
102       p = strchr(buffer, ':') + 2;
103       break;
104     }
105   }
106   fclose(infile);
107   if (p != NULL){
108     if (strstr(p, "Loongson-3A3000") || strstr(p, "Loongson-3B3000")){
109       return CPU_LOONGSON3R3;
110     } else if (strstr(p, "Loongson-3A4000") || strstr(p, "Loongson-3B4000")){
111       return CPU_LOONGSON3R4;
112     } else{
113       return CPU_SICORTEX;
114     }
115   }
116 #endif
117     return CPU_UNKNOWN;
118 }
119
120 char *get_corename(void){
121   return cpuname[detect()];
122 }
123
124 void get_architecture(void){
125   printf("MIPS64");
126 }
127
128 void get_subarchitecture(void){
129   if(detect()==CPU_LOONGSON3R3) {
130     printf("LOONGSON3R3");
131   }else if(detect()==CPU_LOONGSON3R4){
132     printf("LOONGSON3R4");
133   }else if(detect()==CPU_I6400){
134     printf("I6400");
135   }else if(detect()==CPU_P6600){
136     printf("P6600");
137   }else if(detect()==CPU_I6500){
138     printf("I6500");
139   }else{
140     printf("SICORTEX");
141   }
142 }
143
144 void get_subdirname(void){
145   printf("mips64");
146 }
147
148 void get_cpuconfig(void){
149   if(detect()==CPU_LOONGSON3R3) {
150     printf("#define LOONGSON3R3\n");
151     printf("#define L1_DATA_SIZE 65536\n");
152     printf("#define L1_DATA_LINESIZE 32\n");
153     printf("#define L2_SIZE 512488\n");
154     printf("#define L2_LINESIZE 32\n");
155     printf("#define DTB_DEFAULT_ENTRIES 64\n");
156     printf("#define DTB_SIZE 4096\n");
157     printf("#define L2_ASSOCIATIVE 4\n");
158   }else if(detect()==CPU_LOONGSON3R4){
159     printf("#define LOONGSON3R4\n");
160     printf("#define L1_DATA_SIZE 65536\n");
161     printf("#define L1_DATA_LINESIZE 32\n");
162     printf("#define L2_SIZE 512488\n");
163     printf("#define L2_LINESIZE 32\n");
164     printf("#define DTB_DEFAULT_ENTRIES 64\n");
165     printf("#define DTB_SIZE 4096\n");
166     printf("#define L2_ASSOCIATIVE 4\n");
167   }else if(detect()==CPU_I6400){
168     printf("#define I6400\n");
169     printf("#define L1_DATA_SIZE 65536\n");
170     printf("#define L1_DATA_LINESIZE 32\n");
171     printf("#define L2_SIZE 1048576\n");
172     printf("#define L2_LINESIZE 32\n");
173     printf("#define DTB_DEFAULT_ENTRIES 64\n");
174     printf("#define DTB_SIZE 4096\n");
175     printf("#define L2_ASSOCIATIVE 8\n");
176   }else if(detect()==CPU_P6600){
177     printf("#define P6600\n");
178     printf("#define L1_DATA_SIZE 65536\n");
179     printf("#define L1_DATA_LINESIZE 32\n");
180     printf("#define L2_SIZE 1048576\n");
181     printf("#define L2_LINESIZE 32\n");
182     printf("#define DTB_DEFAULT_ENTRIES 64\n");
183     printf("#define DTB_SIZE 4096\n");
184     printf("#define L2_ASSOCIATIVE 8\n");
185   }else if(detect()==CPU_I6500){
186     printf("#define I6500\n");
187     printf("#define L1_DATA_SIZE 65536\n");
188     printf("#define L1_DATA_LINESIZE 32\n");
189     printf("#define L2_SIZE 1048576\n");
190     printf("#define L2_LINESIZE 32\n");
191     printf("#define DTB_DEFAULT_ENTRIES 64\n");
192     printf("#define DTB_SIZE 4096\n");
193     printf("#define L2_ASSOCIATIVE 8\n");
194   }else{
195     printf("#define SICORTEX\n");
196     printf("#define L1_DATA_SIZE 32768\n");
197     printf("#define L1_DATA_LINESIZE 32\n");
198     printf("#define L2_SIZE 512488\n");
199     printf("#define L2_LINESIZE 32\n");
200     printf("#define DTB_DEFAULT_ENTRIES 32\n");
201     printf("#define DTB_SIZE 4096\n");
202     printf("#define L2_ASSOCIATIVE 8\n");
203   }
204   if (!get_feature("msa")) printf("#define NO_MSA\n");
205 }
206
207 void get_libname(void){
208   if(detect()==CPU_LOONGSON3R3) {
209     printf("loongson3r3\n");
210   }else if(detect()==CPU_LOONGSON3R4) {
211     printf("loongson3r4\n");
212   }else if(detect()==CPU_I6400) {
213     printf("i6400\n");
214   }else if(detect()==CPU_P6600) {
215     printf("p6600\n");
216   }else if(detect()==CPU_I6500) {
217     printf("i6500\n");
218   }else{
219     printf("mips64\n");
220   }
221 }
222
223 int get_feature(char *search)
224 {
225
226 #ifdef __linux
227         FILE *infile;
228         char buffer[2048], *p,*t;
229         p = (char *) NULL ;
230
231         infile = fopen("/proc/cpuinfo", "r");
232
233         while (fgets(buffer, sizeof(buffer), infile))
234         {
235
236                 if (!strncmp("Features", buffer, 8) || !strncmp("ASEs implemented", buffer, 16))
237                 {
238                         p = strchr(buffer, ':') + 2;
239                         break;
240                 }
241         }
242
243         fclose(infile);
244
245         if( p == NULL ) return 0;
246
247         t = strtok(p," ");
248         while( t = strtok(NULL," "))
249         {
250                 if (strstr(t, search))   { return(1); }
251         }
252
253 #endif
254         return(0);
255 }
256