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_arm.c 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_ARMV6 1
  30. #define CPU_ARMV7 2
  31. #define CPU_CORTEXA9 3
  32. #define CPU_CORTEXA15 4
  33. static char *cpuname[] = {
  34. "UNKNOWN",
  35. "ARMV6",
  36. "ARMV7",
  37. "CORTEXA9",
  38. "CORTEXA15"
  39. };
  40. static char *cpuname_lower[] = {
  41. "unknown",
  42. "armv6",
  43. "armv7",
  44. "cortexa9",
  45. "cortexa15"
  46. };
  47. int get_feature(char *search)
  48. {
  49. #ifdef __linux
  50. FILE *infile;
  51. char buffer[2048], *p,*t;
  52. p = (char *) NULL ;
  53. infile = fopen("/proc/cpuinfo", "r");
  54. while (fgets(buffer, sizeof(buffer), infile))
  55. {
  56. if (!strncmp("Features", buffer, 8))
  57. {
  58. p = strchr(buffer, ':') + 2;
  59. break;
  60. }
  61. }
  62. fclose(infile);
  63. if( p == NULL ) return 0;
  64. t = strtok(p," ");
  65. while( t = strtok(NULL," "))
  66. {
  67. if (!strcmp(t, search)) { return(1); }
  68. }
  69. #endif
  70. return(0);
  71. }
  72. int detect(void)
  73. {
  74. #ifdef __linux
  75. FILE *infile;
  76. char buffer[512], *p;
  77. p = (char *) NULL ;
  78. infile = fopen("/proc/cpuinfo", "r");
  79. while (fgets(buffer, sizeof(buffer), infile))
  80. {
  81. if (!strncmp("CPU part", buffer, 8))
  82. {
  83. p = strchr(buffer, ':') + 2;
  84. break;
  85. }
  86. }
  87. fclose(infile);
  88. if(p != NULL) {
  89. if (strstr(p, "0xc09")) {
  90. return CPU_CORTEXA9;
  91. }
  92. if (strstr(p, "0xc0f")) {
  93. return CPU_CORTEXA15;
  94. }
  95. if (strstr(p, "0xd07")) {
  96. return CPU_ARMV7; //ARMV8 on 32-bit
  97. }
  98. }
  99. p = (char *) NULL ;
  100. infile = fopen("/proc/cpuinfo", "r");
  101. while (fgets(buffer, sizeof(buffer), infile))
  102. {
  103. if ((!strncmp("model name", buffer, 10)) || (!strncmp("Processor", buffer, 9)))
  104. {
  105. p = strchr(buffer, ':') + 2;
  106. break;
  107. }
  108. }
  109. fclose(infile);
  110. if(p != NULL)
  111. {
  112. if (strstr(p, "ARMv7"))
  113. {
  114. if ( get_feature("vfpv4"))
  115. return CPU_ARMV7;
  116. if ( get_feature("vfpv3"))
  117. return CPU_ARMV7;
  118. if ( get_feature("vfp"))
  119. return CPU_ARMV6;
  120. }
  121. if (strstr(p, "ARMv6"))
  122. {
  123. if ( get_feature("vfp"))
  124. return CPU_ARMV6;
  125. }
  126. }
  127. p = (char *) NULL ;
  128. infile = fopen("/proc/cpuinfo", "r");
  129. while (fgets(buffer, sizeof(buffer), infile))
  130. {
  131. if ((!strncmp("CPU architecture", buffer, 16)))
  132. {
  133. p = strchr(buffer, ':') + 2;
  134. break;
  135. }
  136. }
  137. fclose(infile);
  138. if(p != NULL) {
  139. if (strstr(p, "8")) {
  140. return CPU_ARMV7; //ARMV8 on 32-bit
  141. }
  142. }
  143. #endif
  144. return CPU_UNKNOWN;
  145. }
  146. char *get_corename(void)
  147. {
  148. return cpuname[detect()];
  149. }
  150. void get_architecture(void)
  151. {
  152. printf("ARM");
  153. }
  154. void get_subarchitecture(void)
  155. {
  156. int d = detect();
  157. printf("%s", cpuname[d]);
  158. }
  159. void get_subdirname(void)
  160. {
  161. printf("arm");
  162. }
  163. void get_cpuconfig(void)
  164. {
  165. int d = detect();
  166. switch (d)
  167. {
  168. case CPU_CORTEXA9:
  169. printf("#define CORTEXA9\n");
  170. printf("#define ARMV7\n");
  171. printf("#define HAVE_VFP\n");
  172. printf("#define HAVE_VFPV3\n");
  173. if ( get_feature("neon")) printf("#define HAVE_NEON\n");
  174. if ( get_feature("vfpv4")) printf("#define HAVE_VFPV4\n");
  175. printf("#define L1_DATA_SIZE 32768\n");
  176. printf("#define L1_DATA_LINESIZE 32\n");
  177. printf("#define L2_SIZE 1048576\n");
  178. printf("#define L2_LINESIZE 32\n");
  179. printf("#define DTB_DEFAULT_ENTRIES 128\n");
  180. printf("#define DTB_SIZE 4096\n");
  181. printf("#define L2_ASSOCIATIVE 4\n");
  182. break;
  183. case CPU_CORTEXA15:
  184. printf("#define CORTEXA15\n");
  185. printf("#define ARMV7\n");
  186. printf("#define HAVE_VFP\n");
  187. printf("#define HAVE_VFPV3\n");
  188. if ( get_feature("neon")) printf("#define HAVE_NEON\n");
  189. if ( get_feature("vfpv4")) printf("#define HAVE_VFPV4\n");
  190. printf("#define L1_DATA_SIZE 32768\n");
  191. printf("#define L1_DATA_LINESIZE 32\n");
  192. printf("#define L2_SIZE 1048576\n");
  193. printf("#define L2_LINESIZE 32\n");
  194. printf("#define DTB_DEFAULT_ENTRIES 128\n");
  195. printf("#define DTB_SIZE 4096\n");
  196. printf("#define L2_ASSOCIATIVE 4\n");
  197. break;
  198. case CPU_ARMV7:
  199. printf("#define ARMV7\n");
  200. printf("#define HAVE_VFP\n");
  201. printf("#define HAVE_VFPV3\n");
  202. if ( get_feature("neon")) printf("#define HAVE_NEON\n");
  203. if ( get_feature("vfpv4")) printf("#define HAVE_VFPV4\n");
  204. printf("#define L1_DATA_SIZE 65536\n");
  205. printf("#define L1_DATA_LINESIZE 32\n");
  206. printf("#define L2_SIZE 512488\n");
  207. printf("#define L2_LINESIZE 32\n");
  208. printf("#define DTB_DEFAULT_ENTRIES 64\n");
  209. printf("#define DTB_SIZE 4096\n");
  210. printf("#define L2_ASSOCIATIVE 4\n");
  211. break;
  212. case CPU_ARMV6:
  213. printf("#define ARMV6\n");
  214. printf("#define HAVE_VFP\n");
  215. printf("#define L1_DATA_SIZE 65536\n");
  216. printf("#define L1_DATA_LINESIZE 32\n");
  217. printf("#define L2_SIZE 512488\n");
  218. printf("#define L2_LINESIZE 32\n");
  219. printf("#define DTB_DEFAULT_ENTRIES 64\n");
  220. printf("#define DTB_SIZE 4096\n");
  221. printf("#define L2_ASSOCIATIVE 4\n");
  222. break;
  223. }
  224. }
  225. void get_libname(void)
  226. {
  227. int d = detect();
  228. printf("%s", cpuname_lower[d]);
  229. }
  230. void get_features(void)
  231. {
  232. #ifdef __linux
  233. FILE *infile;
  234. char buffer[2048], *p,*t;
  235. p = (char *) NULL ;
  236. infile = fopen("/proc/cpuinfo", "r");
  237. while (fgets(buffer, sizeof(buffer), infile))
  238. {
  239. if (!strncmp("Features", buffer, 8))
  240. {
  241. p = strchr(buffer, ':') + 2;
  242. break;
  243. }
  244. }
  245. fclose(infile);
  246. if( p == NULL ) return;
  247. t = strtok(p," ");
  248. while( t = strtok(NULL," "))
  249. {
  250. if (!strcmp(t, "vfp")) { printf("HAVE_VFP=1\n"); continue; }
  251. if (!strcmp(t, "vfpv3")) { printf("HAVE_VFPV3=1\n"); continue; }
  252. if (!strcmp(t, "vfpv4")) { printf("HAVE_VFPV4=1\n"); continue; }
  253. if (!strcmp(t, "neon")) { printf("HAVE_NEON=1\n"); continue; }
  254. }
  255. #endif
  256. return;
  257. }