hsw_ext_idle.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2.
  5. *
  6. * Based on SandyBridge monitor. Implements the new package C-states
  7. * (PC8, PC9, PC10) coming with a specific Haswell (family 0x45) CPU.
  8. */
  9. #if defined(__i386__) || defined(__x86_64__)
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "helpers/helpers.h"
  15. #include "idle_monitor/cpupower-monitor.h"
  16. #define MSR_PKG_C8_RESIDENCY 0x00000630
  17. #define MSR_PKG_C9_RESIDENCY 0x00000631
  18. #define MSR_PKG_C10_RESIDENCY 0x00000632
  19. #define MSR_TSC 0x10
  20. enum intel_hsw_ext_id { PC8 = 0, PC9, PC10, HSW_EXT_CSTATE_COUNT,
  21. TSC = 0xFFFF };
  22. static int hsw_ext_get_count_percent(unsigned int self_id, double *percent,
  23. unsigned int cpu);
  24. static cstate_t hsw_ext_cstates[HSW_EXT_CSTATE_COUNT] = {
  25. {
  26. .name = "PC8",
  27. .desc = N_("Processor Package C8"),
  28. .id = PC8,
  29. .range = RANGE_PACKAGE,
  30. .get_count_percent = hsw_ext_get_count_percent,
  31. },
  32. {
  33. .name = "PC9",
  34. .desc = N_("Processor Package C9"),
  35. .desc = N_("Processor Package C2"),
  36. .id = PC9,
  37. .range = RANGE_PACKAGE,
  38. .get_count_percent = hsw_ext_get_count_percent,
  39. },
  40. {
  41. .name = "PC10",
  42. .desc = N_("Processor Package C10"),
  43. .id = PC10,
  44. .range = RANGE_PACKAGE,
  45. .get_count_percent = hsw_ext_get_count_percent,
  46. },
  47. };
  48. static unsigned long long tsc_at_measure_start;
  49. static unsigned long long tsc_at_measure_end;
  50. static unsigned long long *previous_count[HSW_EXT_CSTATE_COUNT];
  51. static unsigned long long *current_count[HSW_EXT_CSTATE_COUNT];
  52. /* valid flag for all CPUs. If a MSR read failed it will be zero */
  53. static int *is_valid;
  54. static int hsw_ext_get_count(enum intel_hsw_ext_id id, unsigned long long *val,
  55. unsigned int cpu)
  56. {
  57. int msr;
  58. switch (id) {
  59. case PC8:
  60. msr = MSR_PKG_C8_RESIDENCY;
  61. break;
  62. case PC9:
  63. msr = MSR_PKG_C9_RESIDENCY;
  64. break;
  65. case PC10:
  66. msr = MSR_PKG_C10_RESIDENCY;
  67. break;
  68. case TSC:
  69. msr = MSR_TSC;
  70. break;
  71. default:
  72. return -1;
  73. };
  74. if (read_msr(cpu, msr, val))
  75. return -1;
  76. return 0;
  77. }
  78. static int hsw_ext_get_count_percent(unsigned int id, double *percent,
  79. unsigned int cpu)
  80. {
  81. *percent = 0.0;
  82. if (!is_valid[cpu])
  83. return -1;
  84. *percent = (100.0 *
  85. (current_count[id][cpu] - previous_count[id][cpu])) /
  86. (tsc_at_measure_end - tsc_at_measure_start);
  87. dprint("%s: previous: %llu - current: %llu - (%u)\n",
  88. hsw_ext_cstates[id].name, previous_count[id][cpu],
  89. current_count[id][cpu], cpu);
  90. dprint("%s: tsc_diff: %llu - count_diff: %llu - percent: %2.f (%u)\n",
  91. hsw_ext_cstates[id].name,
  92. (unsigned long long) tsc_at_measure_end - tsc_at_measure_start,
  93. current_count[id][cpu] - previous_count[id][cpu],
  94. *percent, cpu);
  95. return 0;
  96. }
  97. static int hsw_ext_start(void)
  98. {
  99. int num, cpu;
  100. unsigned long long val;
  101. for (num = 0; num < HSW_EXT_CSTATE_COUNT; num++) {
  102. for (cpu = 0; cpu < cpu_count; cpu++) {
  103. hsw_ext_get_count(num, &val, cpu);
  104. previous_count[num][cpu] = val;
  105. }
  106. }
  107. hsw_ext_get_count(TSC, &tsc_at_measure_start, 0);
  108. return 0;
  109. }
  110. static int hsw_ext_stop(void)
  111. {
  112. unsigned long long val;
  113. int num, cpu;
  114. hsw_ext_get_count(TSC, &tsc_at_measure_end, 0);
  115. for (num = 0; num < HSW_EXT_CSTATE_COUNT; num++) {
  116. for (cpu = 0; cpu < cpu_count; cpu++) {
  117. is_valid[cpu] = !hsw_ext_get_count(num, &val, cpu);
  118. current_count[num][cpu] = val;
  119. }
  120. }
  121. return 0;
  122. }
  123. struct cpuidle_monitor intel_hsw_ext_monitor;
  124. static struct cpuidle_monitor *hsw_ext_register(void)
  125. {
  126. int num;
  127. if (cpupower_cpu_info.vendor != X86_VENDOR_INTEL
  128. || cpupower_cpu_info.family != 6)
  129. return NULL;
  130. switch (cpupower_cpu_info.model) {
  131. case 0x45: /* HSW */
  132. break;
  133. default:
  134. return NULL;
  135. }
  136. is_valid = calloc(cpu_count, sizeof(int));
  137. for (num = 0; num < HSW_EXT_CSTATE_COUNT; num++) {
  138. previous_count[num] = calloc(cpu_count,
  139. sizeof(unsigned long long));
  140. current_count[num] = calloc(cpu_count,
  141. sizeof(unsigned long long));
  142. }
  143. intel_hsw_ext_monitor.name_len = strlen(intel_hsw_ext_monitor.name);
  144. return &intel_hsw_ext_monitor;
  145. }
  146. void hsw_ext_unregister(void)
  147. {
  148. int num;
  149. free(is_valid);
  150. for (num = 0; num < HSW_EXT_CSTATE_COUNT; num++) {
  151. free(previous_count[num]);
  152. free(current_count[num]);
  153. }
  154. }
  155. struct cpuidle_monitor intel_hsw_ext_monitor = {
  156. .name = "HaswellExtended",
  157. .hw_states = hsw_ext_cstates,
  158. .hw_states_num = HSW_EXT_CSTATE_COUNT,
  159. .start = hsw_ext_start,
  160. .stop = hsw_ext_stop,
  161. .do_register = hsw_ext_register,
  162. .unregister = hsw_ext_unregister,
  163. .needs_root = 1,
  164. .overflow_s = 922000000 /* 922337203 seconds TSC overflow
  165. at 20GHz */
  166. };
  167. #endif /* defined(__i386__) || defined(__x86_64__) */