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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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, "Cell", 4)) return CPUTYPE_CELL;
  125. if (!strncasecmp(p, "7447", 4)) return CPUTYPE_PPCG4;
  126. return CPUTYPE_UNKNOWN;
  127. #endif
  128. #ifdef _AIX
  129. // Cast from int to unsigned to ensure comparisons work for all bits in
  130. // the bit mask, even the top bit
  131. unsigned implementation = (unsigned) _system_configuration.implementation;
  132. if (implementation >= 0x40000u) return CPUTYPE_POWER10;
  133. else if (implementation & 0x20000) return CPUTYPE_POWER9;
  134. else if (implementation & 0x10000) return CPUTYPE_POWER8;
  135. else if (implementation & 0x08000) return CPUTYPE_POWER6; // POWER 7
  136. else if (implementation & 0x04000) return CPUTYPE_POWER6;
  137. else if (implementation & 0x02000) return CPUTYPE_POWER5;
  138. else if (implementation & 0x01000) return CPUTYPE_POWER4; // MPC7450
  139. else if (implementation & 0x00800) return CPUTYPE_POWER4;
  140. else return CPUTYPE_POWER3;
  141. #endif
  142. #ifdef __APPLE__
  143. host_basic_info_data_t hostInfo;
  144. mach_msg_type_number_t infoCount;
  145. infoCount = HOST_BASIC_INFO_COUNT;
  146. host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);
  147. if (hostInfo.cpu_subtype == CPU_SUBTYPE_POWERPC_7400) return CPUTYPE_PPCG4;
  148. if (hostInfo.cpu_subtype == CPU_SUBTYPE_POWERPC_7450) return CPUTYPE_PPCG4;
  149. if (hostInfo.cpu_subtype == CPU_SUBTYPE_POWERPC_970) return CPUTYPE_PPC970;
  150. return CPUTYPE_PPC970;
  151. #endif
  152. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
  153. int id;
  154. __asm __volatile("mfpvr %0" : "=r"(id));
  155. switch ( id >> 16 ) {
  156. case 0x80: // POWER10
  157. return CPUTYPE_POWER10;
  158. break;
  159. case 0x4e: // POWER9
  160. return CPUTYPE_POWER9;
  161. break;
  162. case 0x4d:
  163. case 0x4b: // POWER8/8E
  164. return CPUTYPE_POWER8;
  165. break;
  166. case 0x4a:
  167. case 0x3f: // POWER7/7E
  168. return CPUTYPE_POWER6;
  169. break;
  170. case 0x3e:
  171. return CPUTYPE_POWER6;
  172. break;
  173. case 0x3a:
  174. return CPUTYPE_POWER5;
  175. break;
  176. case 0x35:
  177. case 0x38: // POWER4 /4+
  178. return CPUTYPE_POWER4;
  179. break;
  180. case 0x40:
  181. case 0x41: // POWER3 /3+
  182. return CPUTYPE_POWER3;
  183. break;
  184. case 0x39:
  185. case 0x3c:
  186. case 0x44:
  187. case 0x45:
  188. return CPUTYPE_PPC970;
  189. break;
  190. case 0x70:
  191. return CPUTYPE_CELL;
  192. break;
  193. case 0x8003:
  194. return CPUTYPE_PPCG4;
  195. break;
  196. default:
  197. return CPUTYPE_UNKNOWN;
  198. }
  199. #endif
  200. return CPUTYPE_UNKNOWN;
  201. }
  202. void get_architecture(void){
  203. printf("POWER");
  204. }
  205. void get_subdirname(void){
  206. printf("power");
  207. }
  208. void get_subarchitecture(void){
  209. printf("%s", cpuname[detect()]);
  210. }
  211. void get_cpuconfig(void){
  212. #if 0
  213. #ifdef _AIX
  214. struct vminfo info;
  215. #endif
  216. #endif
  217. printf("#define %s\n", cpuname[detect()]);
  218. printf("#define CORE_%s\n", corename[detect()]);
  219. printf("#define L1_DATA_SIZE 32768\n");
  220. printf("#define L1_DATA_LINESIZE 128\n");
  221. printf("#define L2_SIZE 524288\n");
  222. printf("#define L2_LINESIZE 128 \n");
  223. printf("#define DTB_DEFAULT_ENTRIES 128\n");
  224. printf("#define DTB_SIZE 4096\n");
  225. printf("#define L2_ASSOCIATIVE 8\n");
  226. #if 0
  227. #ifdef _AIX
  228. if (vmgetinfo(&info, VMINFO, 0) == 0) {
  229. if ((info.lgpg_size >> 20) >= 1024) {
  230. printf("#define ALLOC_HUGETLB\n");
  231. }
  232. }
  233. #endif
  234. #endif
  235. }
  236. void get_libname(void){
  237. printf("%s", lowercpuname[detect()]);
  238. }
  239. char *get_corename(void){
  240. return cpuname[detect()];
  241. }