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_power.c 7.9 kB

3 months ago
3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #include <sys/utsname.h>
  39. #ifdef _AIX
  40. #include <sys/systemcfg.h>
  41. #include <sys/vminfo.h>
  42. #endif
  43. #ifdef __APPLE__
  44. #include <mach/mach.h>
  45. #include <mach/mach_host.h>
  46. #include <mach/host_info.h>
  47. #include <mach/machine.h>
  48. #endif
  49. #define CPUTYPE_UNKNOWN 0
  50. #define CPUTYPE_POWER3 1
  51. #define CPUTYPE_POWER4 2
  52. #define CPUTYPE_PPC970 3
  53. #define CPUTYPE_POWER5 4
  54. #define CPUTYPE_POWER6 5
  55. #define CPUTYPE_CELL 6
  56. #define CPUTYPE_PPCG4 7
  57. #define CPUTYPE_POWER8 8
  58. #define CPUTYPE_POWER9 9
  59. #define CPUTYPE_POWER10 10
  60. char *cpuname[] = {
  61. "UNKNOWN",
  62. "POWER3",
  63. "POWER4",
  64. "PPC970",
  65. "POWER5",
  66. "POWER6",
  67. "CELL",
  68. "PPCG4",
  69. "POWER8",
  70. "POWER9",
  71. "POWER10"
  72. };
  73. char *lowercpuname[] = {
  74. "unknown",
  75. "power3",
  76. "power4",
  77. "ppc970",
  78. "power5",
  79. "power6",
  80. "cell",
  81. "ppcg4",
  82. "power8",
  83. "power9",
  84. "power10"
  85. };
  86. char *corename[] = {
  87. "UNKNOWN",
  88. "POWER3",
  89. "POWER4",
  90. "POWER4",
  91. "POWER4",
  92. "POWER6",
  93. "CELL",
  94. "PPCG4",
  95. "POWER8",
  96. "POWER9",
  97. "POWER10"
  98. };
  99. int detect(void){
  100. #ifdef __linux
  101. FILE *infile;
  102. char buffer[512], *p;
  103. p = (char *)NULL;
  104. infile = fopen("/proc/cpuinfo", "r");
  105. while (fgets(buffer, sizeof(buffer), infile)){
  106. if (!strncmp("cpu", buffer, 3)){
  107. p = strchr(buffer, ':') + 2;
  108. #if 0
  109. fprintf(stderr, "%s\n", p);
  110. #endif
  111. break;
  112. }
  113. }
  114. fclose(infile);
  115. if (!strncasecmp(p, "POWER3", 6)) return CPUTYPE_POWER3;
  116. if (!strncasecmp(p, "POWER4", 6)) return CPUTYPE_POWER4;
  117. if (!strncasecmp(p, "PPC970", 6)) return CPUTYPE_PPC970;
  118. if (!strncasecmp(p, "POWER5", 6)) return CPUTYPE_POWER5;
  119. if (!strncasecmp(p, "POWER6", 6)) return CPUTYPE_POWER6;
  120. if (!strncasecmp(p, "POWER7", 6)) return CPUTYPE_POWER6;
  121. if (!strncasecmp(p, "POWER8", 6)) return CPUTYPE_POWER8;
  122. if (!strncasecmp(p, "POWER9", 6)) return CPUTYPE_POWER9;
  123. if (!strncasecmp(p, "POWER10", 7)) return CPUTYPE_POWER10;
  124. if (!strncasecmp(p, "POWER11", 7)) return CPUTYPE_POWER10;
  125. if (!strncasecmp(p, "Cell", 4)) return CPUTYPE_CELL;
  126. if (!strncasecmp(p, "7447", 4)) return CPUTYPE_PPCG4;
  127. return CPUTYPE_UNKNOWN;
  128. #endif
  129. #ifdef _AIX
  130. // Cast from int to unsigned to ensure comparisons work for all bits in
  131. // the bit mask, even the top bit
  132. unsigned implementation = (unsigned) _system_configuration.implementation;
  133. if (implementation >= 0x40000u) return CPUTYPE_POWER10;
  134. else if (implementation & 0x20000) return CPUTYPE_POWER9;
  135. else if (implementation & 0x10000) return CPUTYPE_POWER8;
  136. else if (implementation & 0x08000) return CPUTYPE_POWER6; // POWER 7
  137. else if (implementation & 0x04000) return CPUTYPE_POWER6;
  138. else if (implementation & 0x02000) return CPUTYPE_POWER5;
  139. else if (implementation & 0x01000) return CPUTYPE_POWER4; // MPC7450
  140. else if (implementation & 0x00800) return CPUTYPE_POWER4;
  141. else return CPUTYPE_POWER3;
  142. #endif
  143. #ifdef __APPLE__
  144. host_basic_info_data_t hostInfo;
  145. mach_msg_type_number_t infoCount;
  146. infoCount = HOST_BASIC_INFO_COUNT;
  147. host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);
  148. if (hostInfo.cpu_subtype == CPU_SUBTYPE_POWERPC_7400) return CPUTYPE_PPCG4;
  149. if (hostInfo.cpu_subtype == CPU_SUBTYPE_POWERPC_7450) return CPUTYPE_PPCG4;
  150. if (hostInfo.cpu_subtype == CPU_SUBTYPE_POWERPC_970) return CPUTYPE_PPC970;
  151. return CPUTYPE_PPC970;
  152. #endif
  153. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
  154. int id;
  155. __asm __volatile("mfpvr %0" : "=r"(id));
  156. switch ( id >> 16 ) {
  157. case 0x82: // POWER11
  158. return CPUTYPE_POWER10;
  159. break;
  160. case 0x80: // POWER10
  161. return CPUTYPE_POWER10;
  162. break;
  163. case 0x4e: // POWER9
  164. return CPUTYPE_POWER9;
  165. break;
  166. case 0x4d:
  167. case 0x4b: // POWER8/8E
  168. return CPUTYPE_POWER8;
  169. break;
  170. case 0x4a:
  171. case 0x3f: // POWER7/7E
  172. return CPUTYPE_POWER6;
  173. break;
  174. case 0x3e:
  175. return CPUTYPE_POWER6;
  176. break;
  177. case 0x3a:
  178. return CPUTYPE_POWER5;
  179. break;
  180. case 0x35:
  181. case 0x38: // POWER4 /4+
  182. return CPUTYPE_POWER4;
  183. break;
  184. case 0x40:
  185. case 0x41: // POWER3 /3+
  186. return CPUTYPE_POWER3;
  187. break;
  188. case 0x39:
  189. case 0x3c:
  190. case 0x44:
  191. case 0x45:
  192. return CPUTYPE_PPC970;
  193. break;
  194. case 0x70:
  195. return CPUTYPE_CELL;
  196. break;
  197. case 0x8003:
  198. return CPUTYPE_PPCG4;
  199. break;
  200. default:
  201. return CPUTYPE_UNKNOWN;
  202. }
  203. #endif
  204. return CPUTYPE_UNKNOWN;
  205. }
  206. void get_architecture(void){
  207. printf("POWER");
  208. }
  209. void get_subdirname(void){
  210. printf("power");
  211. }
  212. void get_subarchitecture(void){
  213. printf("%s", cpuname[detect()]);
  214. }
  215. void get_cpuconfig(void){
  216. #if 0
  217. #ifdef _AIX
  218. struct vminfo info;
  219. #endif
  220. #endif
  221. printf("#define %s\n", cpuname[detect()]);
  222. printf("#define CORE_%s\n", corename[detect()]);
  223. printf("#define L1_DATA_SIZE 32768\n");
  224. printf("#define L1_DATA_LINESIZE 128\n");
  225. printf("#define L2_SIZE 524288\n");
  226. printf("#define L2_LINESIZE 128 \n");
  227. printf("#define DTB_DEFAULT_ENTRIES 128\n");
  228. printf("#define DTB_SIZE 4096\n");
  229. printf("#define L2_ASSOCIATIVE 8\n");
  230. #if 0
  231. #ifdef _AIX
  232. if (vmgetinfo(&info, VMINFO, 0) == 0) {
  233. if ((info.lgpg_size >> 20) >= 1024) {
  234. printf("#define ALLOC_HUGETLB\n");
  235. }
  236. }
  237. #endif
  238. #endif
  239. }
  240. void get_libname(void){
  241. printf("%s", lowercpuname[detect()]);
  242. }
  243. char *get_corename(void){
  244. return cpuname[detect()];
  245. }