You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpuid_arm64.c 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**************************************************************************
  2. Copyright (c) 2013, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE
  20. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  25. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *****************************************************************************/
  27. #include <string.h>
  28. #define CPU_UNKNOWN 0
  29. #define CPU_ARMV8 1
  30. #define CPU_CORTEXA57 2
  31. #define CPU_VULCAN 3
  32. #define CPU_THUNDERX 4
  33. #define CPU_THUNDERX2T99 5
  34. static char *cpuname[] = {
  35. "UNKNOWN",
  36. "ARMV8" ,
  37. "CORTEXA57",
  38. "VULCAN",
  39. "THUNDERX",
  40. "THUNDERX2T99"
  41. };
  42. static char *cpuname_lower[] = {
  43. "unknown",
  44. "armv8" ,
  45. "cortexa57",
  46. "vulcan",
  47. "thunderx",
  48. "thunderx2t99"
  49. };
  50. int get_feature(char *search)
  51. {
  52. #ifdef linux
  53. FILE *infile;
  54. char buffer[2048], *p,*t;
  55. p = (char *) NULL ;
  56. infile = fopen("/proc/cpuinfo", "r");
  57. while (fgets(buffer, sizeof(buffer), infile))
  58. {
  59. if (!strncmp("Features", buffer, 8))
  60. {
  61. p = strchr(buffer, ':') + 2;
  62. break;
  63. }
  64. }
  65. fclose(infile);
  66. if( p == NULL ) return 0;
  67. t = strtok(p," ");
  68. while( t = strtok(NULL," "))
  69. {
  70. if (!strcmp(t, search)) { return(1); }
  71. }
  72. #endif
  73. return(0);
  74. }
  75. int detect(void)
  76. {
  77. #ifdef linux
  78. FILE *infile;
  79. char buffer[512], *p, *cpu_part = NULL, *cpu_implementer = NULL;
  80. p = (char *) NULL ;
  81. infile = fopen("/proc/cpuinfo", "r");
  82. while (fgets(buffer, sizeof(buffer), infile)) {
  83. if ((cpu_part != NULL) && (cpu_implementer != NULL)) {
  84. break;
  85. }
  86. if ((cpu_part == NULL) && !strncmp("CPU part", buffer, 8)) {
  87. cpu_part = strchr(buffer, ':') + 2;
  88. cpu_part = strdup(cpu_part);
  89. } else if ((cpu_implementer == NULL) && !strncmp("CPU implementer", buffer, 15)) {
  90. cpu_implementer = strchr(buffer, ':') + 2;
  91. cpu_implementer = strdup(cpu_implementer);
  92. }
  93. }
  94. fclose(infile);
  95. if(cpu_part != NULL && cpu_implementer != NULL) {
  96. if (strstr(cpu_implementer, "0x41") &&
  97. (strstr(cpu_part, "0xd07") || strstr(cpu_part,"0xd08") || strstr(cpu_part,"0xd03") ))
  98. return CPU_CORTEXA57; //or compatible A53, A72
  99. else if (strstr(cpu_part, "0x516") && strstr(cpu_implementer, "0x42"))
  100. return CPU_VULCAN;
  101. else if (strstr(cpu_part, "0x0a1") && strstr(cpu_implementer, "0x43"))
  102. return CPU_THUNDERX;
  103. else if (strstr(cpu_part, "0x0af") && strstr(cpu_implementer, "0x43"))
  104. return CPU_THUNDERX2T99;
  105. }
  106. p = (char *) NULL ;
  107. infile = fopen("/proc/cpuinfo", "r");
  108. while (fgets(buffer, sizeof(buffer), infile))
  109. {
  110. if ((!strncmp("model name", buffer, 10)) || (!strncmp("Processor", buffer, 9)) ||
  111. (!strncmp("CPU architecture", buffer, 16)))
  112. {
  113. p = strchr(buffer, ':') + 2;
  114. break;
  115. }
  116. }
  117. fclose(infile);
  118. if(p != NULL)
  119. {
  120. if ((strstr(p, "AArch64")) || (strstr(p, "8")))
  121. {
  122. return CPU_ARMV8;
  123. }
  124. }
  125. #endif
  126. return CPU_UNKNOWN;
  127. }
  128. char *get_corename(void)
  129. {
  130. return cpuname[detect()];
  131. }
  132. void get_architecture(void)
  133. {
  134. printf("ARM64");
  135. }
  136. void get_subarchitecture(void)
  137. {
  138. int d = detect();
  139. printf("%s", cpuname[d]);
  140. }
  141. void get_subdirname(void)
  142. {
  143. printf("arm64");
  144. }
  145. void get_cpuconfig(void)
  146. {
  147. int d = detect();
  148. switch (d)
  149. {
  150. case CPU_ARMV8:
  151. printf("#define ARMV8\n");
  152. printf("#define L1_DATA_SIZE 32768\n");
  153. printf("#define L1_DATA_LINESIZE 64\n");
  154. printf("#define L2_SIZE 262144\n");
  155. printf("#define L2_LINESIZE 64\n");
  156. printf("#define DTB_DEFAULT_ENTRIES 64\n");
  157. printf("#define DTB_SIZE 4096\n");
  158. printf("#define L2_ASSOCIATIVE 4\n");
  159. break;
  160. case CPU_VULCAN:
  161. printf("#define VULCAN \n");
  162. printf("#define HAVE_VFP \n");
  163. printf("#define HAVE_VFPV3 \n");
  164. printf("#define HAVE_NEON \n");
  165. printf("#define HAVE_VFPV4 \n");
  166. printf("#define L1_CODE_SIZE 32768 \n");
  167. printf("#define L1_CODE_LINESIZE 64 \n");
  168. printf("#define L1_CODE_ASSOCIATIVE 8 \n");
  169. printf("#define L1_DATA_SIZE 32768 \n");
  170. printf("#define L1_DATA_LINESIZE 64 \n");
  171. printf("#define L1_DATA_ASSOCIATIVE 8 \n");
  172. printf("#define L2_SIZE 262144 \n");
  173. printf("#define L2_LINESIZE 64 \n");
  174. printf("#define L2_ASSOCIATIVE 8 \n");
  175. printf("#define L3_SIZE 33554432 \n");
  176. printf("#define L3_LINESIZE 64 \n");
  177. printf("#define L3_ASSOCIATIVE 32 \n");
  178. printf("#define DTB_DEFAULT_ENTRIES 64 \n");
  179. printf("#define DTB_SIZE 4096 \n");
  180. break;
  181. case CPU_CORTEXA57:
  182. printf("#define CORTEXA57\n");
  183. printf("#define HAVE_VFP\n");
  184. printf("#define HAVE_VFPV3\n");
  185. printf("#define HAVE_NEON\n");
  186. printf("#define HAVE_VFPV4\n");
  187. printf("#define L1_CODE_SIZE 49152\n");
  188. printf("#define L1_CODE_LINESIZE 64\n");
  189. printf("#define L1_CODE_ASSOCIATIVE 3\n");
  190. printf("#define L1_DATA_SIZE 32768\n");
  191. printf("#define L1_DATA_LINESIZE 64\n");
  192. printf("#define L1_DATA_ASSOCIATIVE 2\n");
  193. printf("#define L2_SIZE 2097152\n");
  194. printf("#define L2_LINESIZE 64\n");
  195. printf("#define L2_ASSOCIATIVE 16\n");
  196. printf("#define DTB_DEFAULT_ENTRIES 64\n");
  197. printf("#define DTB_SIZE 4096\n");
  198. break;
  199. case CPU_THUNDERX:
  200. printf("#define ARMV8\n");
  201. printf("#define THUNDERX\n");
  202. printf("#define L1_DATA_SIZE 32768\n");
  203. printf("#define L1_DATA_LINESIZE 128\n");
  204. printf("#define L2_SIZE 16777216\n");
  205. printf("#define L2_LINESIZE 128\n");
  206. printf("#define DTB_DEFAULT_ENTRIES 64\n");
  207. printf("#define DTB_SIZE 4096\n");
  208. printf("#define L2_ASSOCIATIVE 16\n");
  209. break;
  210. case CPU_THUNDERX2T99:
  211. printf("#define VULCAN \n");
  212. printf("#define HAVE_VFP \n");
  213. printf("#define HAVE_VFPV3 \n");
  214. printf("#define HAVE_NEON \n");
  215. printf("#define HAVE_VFPV4 \n");
  216. printf("#define L1_CODE_SIZE 32768 \n");
  217. printf("#define L1_CODE_LINESIZE 64 \n");
  218. printf("#define L1_CODE_ASSOCIATIVE 8 \n");
  219. printf("#define L1_DATA_SIZE 32768 \n");
  220. printf("#define L1_DATA_LINESIZE 64 \n");
  221. printf("#define L1_DATA_ASSOCIATIVE 8 \n");
  222. printf("#define L2_SIZE 262144 \n");
  223. printf("#define L2_LINESIZE 64 \n");
  224. printf("#define L2_ASSOCIATIVE 8 \n");
  225. printf("#define L3_SIZE 33554432 \n");
  226. printf("#define L3_LINESIZE 64 \n");
  227. printf("#define L3_ASSOCIATIVE 32 \n");
  228. printf("#define DTB_DEFAULT_ENTRIES 64 \n");
  229. printf("#define DTB_SIZE 4096 \n");
  230. break;
  231. }
  232. }
  233. void get_libname(void)
  234. {
  235. int d = detect();
  236. printf("%s", cpuname_lower[d]);
  237. }
  238. void get_features(void)
  239. {
  240. #ifdef linux
  241. FILE *infile;
  242. char buffer[2048], *p,*t;
  243. p = (char *) NULL ;
  244. infile = fopen("/proc/cpuinfo", "r");
  245. while (fgets(buffer, sizeof(buffer), infile))
  246. {
  247. if (!strncmp("Features", buffer, 8))
  248. {
  249. p = strchr(buffer, ':') + 2;
  250. break;
  251. }
  252. }
  253. fclose(infile);
  254. if( p == NULL ) return;
  255. t = strtok(p," ");
  256. while( t = strtok(NULL," "))
  257. {
  258. }
  259. #endif
  260. return;
  261. }