Merge branch 'loongson3b' into release-0.1.0
[platform/upstream/openblas.git] / cpuid_mips.c
1 /*****************************************************************************
2 Copyright (c) 2011, Lab of Parallel Software and Computational Science,ICSAS
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 ISCAS nor the names of its contributors may 
17       be used to endorse or promote products derived from this software 
18       without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
29 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 **********************************************************************************/
32
33
34 /*********************************************************************/
35 /* Copyright 2009, 2010 The University of Texas at Austin.           */
36 /* All rights reserved.                                              */
37 /*                                                                   */
38 /* Redistribution and use in source and binary forms, with or        */
39 /* without modification, are permitted provided that the following   */
40 /* conditions are met:                                               */
41 /*                                                                   */
42 /*   1. Redistributions of source code must retain the above         */
43 /*      copyright notice, this list of conditions and the following  */
44 /*      disclaimer.                                                  */
45 /*                                                                   */
46 /*   2. Redistributions in binary form must reproduce the above      */
47 /*      copyright notice, this list of conditions and the following  */
48 /*      disclaimer in the documentation and/or other materials       */
49 /*      provided with the distribution.                              */
50 /*                                                                   */
51 /*    THIS  SOFTWARE IS PROVIDED  BY THE  UNIVERSITY OF  TEXAS AT    */
52 /*    AUSTIN  ``AS IS''  AND ANY  EXPRESS OR  IMPLIED WARRANTIES,    */
53 /*    INCLUDING, BUT  NOT LIMITED  TO, THE IMPLIED  WARRANTIES OF    */
54 /*    MERCHANTABILITY  AND FITNESS FOR  A PARTICULAR  PURPOSE ARE    */
55 /*    DISCLAIMED.  IN  NO EVENT SHALL THE UNIVERSITY  OF TEXAS AT    */
56 /*    AUSTIN OR CONTRIBUTORS BE  LIABLE FOR ANY DIRECT, INDIRECT,    */
57 /*    INCIDENTAL,  SPECIAL, EXEMPLARY,  OR  CONSEQUENTIAL DAMAGES    */
58 /*    (INCLUDING, BUT  NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE    */
59 /*    GOODS  OR  SERVICES; LOSS  OF  USE,  DATA,  OR PROFITS;  OR    */
60 /*    BUSINESS INTERRUPTION) HOWEVER CAUSED  AND ON ANY THEORY OF    */
61 /*    LIABILITY, WHETHER  IN CONTRACT, STRICT  LIABILITY, OR TORT    */
62 /*    (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY WAY OUT    */
63 /*    OF  THE  USE OF  THIS  SOFTWARE,  EVEN  IF ADVISED  OF  THE    */
64 /*    POSSIBILITY OF SUCH DAMAGE.                                    */
65 /*                                                                   */
66 /* The views and conclusions contained in the software and           */
67 /* documentation are those of the authors and should not be          */
68 /* interpreted as representing official policies, either expressed   */
69 /* or implied, of The University of Texas at Austin.                 */
70 /*********************************************************************/
71
72 #define CPU_UNKNOWN     0
73 #define CPU_SICORTEX    1
74 #define CPU_LOONGSON3A  2
75 #define CPU_LOONGSON3B  3
76
77 static char *cpuname[] = {
78   "UNKOWN",
79   "SICORTEX",
80   "LOONGSON3A",
81   "LOONGSON3B"
82 };
83
84 int detect(void){
85
86 #ifdef linux
87   FILE *infile;
88   char buffer[512], *p;
89
90   p = (char *)NULL;
91   infile = fopen("/proc/cpuinfo", "r");
92   while (fgets(buffer, sizeof(buffer), infile)){
93     if (!strncmp("cpu", buffer, 3)){
94         p = strchr(buffer, ':') + 2;
95 #if 0
96         fprintf(stderr, "%s\n", p);
97 #endif
98         break;
99       }
100   }
101
102   fclose(infile);
103
104   if (strstr(p, "Loongson-3A")){
105     return CPU_LOONGSON3A;
106   }else if(strstr(p, "Loongson-3B")){
107     return CPU_LOONGSON3B;
108   }else if (strstr(p, "Loongson-3")){
109     infile = fopen("/proc/cpuinfo", "r");
110     while (fgets(buffer, sizeof(buffer), infile)){
111       if (!strncmp("system type", buffer, 11)){
112         p = strchr(buffer, ':') + 2;
113         break;
114       }
115     }
116     fclose(infile);
117     if (strstr(p, "loongson3a"))
118       return CPU_LOONGSON3A;
119   }else{
120     return CPU_SICORTEX;
121   }
122 #endif
123     return CPU_UNKNOWN;
124 }
125
126 char *get_corename(void){
127   return cpuname[detect()];
128 }
129
130 void get_architecture(void){
131   printf("MIPS64");
132 }
133
134 void get_subarchitecture(void){
135   if(detect()==CPU_LOONGSON3A) {
136     printf("LOONGSON3A");
137   }else if(detect()==CPU_LOONGSON3B){
138     printf("LOONGSON3B");
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_LOONGSON3A) {
150     printf("#define LOONGSON3A\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_LOONGSON3B){
159     printf("#define LOONGSON3B\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{
168     printf("#define SICORTEX\n");
169     printf("#define L1_DATA_SIZE 32768\n");
170     printf("#define L1_DATA_LINESIZE 32\n");
171     printf("#define L2_SIZE 512488\n");
172     printf("#define L2_LINESIZE 32\n");
173     printf("#define DTB_DEFAULT_ENTRIES 32\n");
174     printf("#define DTB_SIZE 4096\n");
175     printf("#define L2_ASSOCIATIVE 8\n");
176   }
177 }
178
179 void get_libname(void){
180   if(detect()==CPU_LOONGSON3A) {
181     printf("loongson3a\n");
182   }else if(detect()==CPU_LOONGSON3B) {
183     printf("loongson3b\n");
184   }else{
185 #ifdef __mips64
186   printf("mips64\n");
187 #else
188   printf("mips32\n");
189 #endif
190   }
191 }