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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/vminfo.h>
  41. #endif
  42. #ifdef __APPLE__
  43. #include <mach/mach.h>
  44. #include <mach/mach_host.h>
  45. #include <mach/host_info.h>
  46. #include <mach/machine.h>
  47. #endif
  48. #define CPUTYPE_UNKNOWN 0
  49. #define CPUTYPE_POWER3 1
  50. #define CPUTYPE_POWER4 2
  51. #define CPUTYPE_PPC970 3
  52. #define CPUTYPE_POWER5 4
  53. #define CPUTYPE_POWER6 5
  54. #define CPUTYPE_CELL 6
  55. #define CPUTYPE_PPCG4 7
  56. #define CPUTYPE_POWER8 8
  57. #define CPUTYPE_POWER9 9
  58. char *cpuname[] = {
  59. "UNKNOWN",
  60. "POWER3",
  61. "POWER4",
  62. "PPC970",
  63. "POWER5",
  64. "POWER6",
  65. "CELL",
  66. "PPCG4",
  67. "POWER8",
  68. "POWER9"
  69. };
  70. char *lowercpuname[] = {
  71. "unknown",
  72. "power3",
  73. "power4",
  74. "ppc970",
  75. "power5",
  76. "power6",
  77. "cell",
  78. "ppcg4",
  79. "power8",
  80. "power9"
  81. };
  82. char *corename[] = {
  83. "UNKNOWN",
  84. "POWER3",
  85. "POWER4",
  86. "POWER4",
  87. "POWER4",
  88. "POWER6",
  89. "CELL",
  90. "PPCG4",
  91. "POWER8",
  92. "POWER9"
  93. };
  94. int detect(void){
  95. #ifdef linux
  96. FILE *infile;
  97. char buffer[512], *p;
  98. p = (char *)NULL;
  99. infile = fopen("/proc/cpuinfo", "r");
  100. while (fgets(buffer, sizeof(buffer), infile)){
  101. if (!strncmp("cpu", buffer, 3)){
  102. p = strchr(buffer, ':') + 2;
  103. #if 0
  104. fprintf(stderr, "%s\n", p);
  105. #endif
  106. break;
  107. }
  108. }
  109. fclose(infile);
  110. if (!strncasecmp(p, "POWER3", 6)) return CPUTYPE_POWER3;
  111. if (!strncasecmp(p, "POWER4", 6)) return CPUTYPE_POWER4;
  112. if (!strncasecmp(p, "PPC970", 6)) return CPUTYPE_PPC970;
  113. if (!strncasecmp(p, "POWER5", 6)) return CPUTYPE_POWER5;
  114. if (!strncasecmp(p, "POWER6", 6)) return CPUTYPE_POWER6;
  115. if (!strncasecmp(p, "POWER7", 6)) return CPUTYPE_POWER6;
  116. if (!strncasecmp(p, "POWER8", 6)) return CPUTYPE_POWER8;
  117. if (!strncasecmp(p, "POWER9", 6)) return CPUTYPE_POWER9;
  118. if (!strncasecmp(p, "Cell", 4)) return CPUTYPE_CELL;
  119. if (!strncasecmp(p, "7447", 4)) return CPUTYPE_PPCG4;
  120. return CPUTYPE_UNKNOWN;
  121. #endif
  122. #ifdef _AIX
  123. FILE *infile;
  124. char buffer[512], *p;
  125. p = (char *)NULL;
  126. infile = popen("prtconf|grep 'Processor Type'", "r");
  127. while (fgets(buffer, sizeof(buffer), infile)){
  128. if (!strncmp("Pro", buffer, 3)){
  129. p = strchr(buffer, ':') + 2;
  130. #if 0
  131. fprintf(stderr, "%s\n", p);
  132. #endif
  133. break;
  134. }
  135. }
  136. pclose(infile);
  137. if (!strncasecmp(p, "POWER3", 6)) return CPUTYPE_POWER3;
  138. if (!strncasecmp(p, "POWER4", 6)) return CPUTYPE_POWER4;
  139. if (!strncasecmp(p, "PPC970", 6)) return CPUTYPE_PPC970;
  140. if (!strncasecmp(p, "POWER5", 6)) return CPUTYPE_POWER5;
  141. if (!strncasecmp(p, "POWER6", 6)) return CPUTYPE_POWER6;
  142. if (!strncasecmp(p, "POWER7", 6)) return CPUTYPE_POWER6;
  143. if (!strncasecmp(p, "POWER8", 6)) return CPUTYPE_POWER8;
  144. if (!strncasecmp(p, "POWER9", 6)) return CPUTYPE_POWER9;
  145. if (!strncasecmp(p, "Cell", 4)) return CPUTYPE_CELL;
  146. if (!strncasecmp(p, "7447", 4)) return CPUTYPE_PPCG4;
  147. return CPUTYPE_POWER5;
  148. #endif
  149. #ifdef __APPLE__
  150. host_basic_info_data_t hostInfo;
  151. mach_msg_type_number_t infoCount;
  152. infoCount = HOST_BASIC_INFO_COUNT;
  153. host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);
  154. if (hostInfo.cpu_subtype == CPU_SUBTYPE_POWERPC_7450) return CPUTYPE_PPCG4;
  155. if (hostInfo.cpu_subtype == CPU_SUBTYPE_POWERPC_970) return CPUTYPE_PPC970;
  156. return CPUTYPE_PPC970;
  157. #endif
  158. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
  159. int id;
  160. __asm __volatile("mfpvr %0" : "=r"(id));
  161. switch ( id >> 16 ) {
  162. case 0x4e: // POWER9
  163. return CPUTYPE_POWER9;
  164. break;
  165. case 0x4d:
  166. case 0x4b: // POWER8/8E
  167. return CPUTYPE_POWER8;
  168. break;
  169. case 0x4a:
  170. case 0x3f: // POWER7/7E
  171. return CPUTYPE_POWER6;
  172. break;
  173. case 0x3e:
  174. return CPUTYPE_POWER6;
  175. break;
  176. case 0x3a:
  177. return CPUTYPE_POWER5;
  178. break;
  179. case 0x35:
  180. case 0x38: // POWER4 /4+
  181. return CPUTYPE_POWER4;
  182. break;
  183. case 0x40:
  184. case 0x41: // POWER3 /3+
  185. return CPUTYPE_POWER3;
  186. break;
  187. case 0x39:
  188. case 0x3c:
  189. case 0x44:
  190. case 0x45:
  191. return CPUTYPE_PPC970;
  192. break;
  193. case 0x70:
  194. return CPUTYPE_CELL;
  195. break;
  196. case 0x8003:
  197. return CPUTYPE_PPCG4;
  198. break;
  199. default:
  200. return CPUTYPE_UNKNOWN;
  201. }
  202. #endif
  203. }
  204. void get_architecture(void){
  205. printf("POWER");
  206. }
  207. void get_subdirname(void){
  208. printf("power");
  209. }
  210. void get_subarchitecture(void){
  211. printf("%s", cpuname[detect()]);
  212. }
  213. void get_cpuconfig(void){
  214. #if 0
  215. #ifdef _AIX
  216. struct vminfo info;
  217. #endif
  218. #endif
  219. printf("#define %s\n", cpuname[detect()]);
  220. printf("#define CORE_%s\n", corename[detect()]);
  221. printf("#define L1_DATA_SIZE 32768\n");
  222. printf("#define L1_DATA_LINESIZE 128\n");
  223. printf("#define L2_SIZE 524288\n");
  224. printf("#define L2_LINESIZE 128 \n");
  225. printf("#define DTB_DEFAULT_ENTRIES 128\n");
  226. printf("#define DTB_SIZE 4096\n");
  227. printf("#define L2_ASSOCIATIVE 8\n");
  228. #if 0
  229. #ifdef _AIX
  230. if (vmgetinfo(&info, VMINFO, 0) == 0) {
  231. if ((info.lgpg_size >> 20) >= 1024) {
  232. printf("#define ALLOC_HUGETLB\n");
  233. }
  234. }
  235. #endif
  236. #endif
  237. }
  238. void get_libname(void){
  239. printf("%s", lowercpuname[detect()]);
  240. }
  241. char *get_corename(void){
  242. return cpuname[detect()];
  243. }