helpers.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * Miscellaneous helpers which do not fit or are worth
  7. * to put into separate headers
  8. */
  9. #ifndef __CPUPOWERUTILS_HELPERS__
  10. #define __CPUPOWERUTILS_HELPERS__
  11. #include <libintl.h>
  12. #include <locale.h>
  13. #include "helpers/bitmask.h"
  14. /* Internationalization ****************************/
  15. #ifdef NLS
  16. #define _(String) gettext(String)
  17. #ifndef gettext_noop
  18. #define gettext_noop(String) String
  19. #endif
  20. #define N_(String) gettext_noop(String)
  21. #else /* !NLS */
  22. #define _(String) String
  23. #define N_(String) String
  24. #endif
  25. /* Internationalization ****************************/
  26. extern int run_as_root;
  27. extern struct bitmask *cpus_chosen;
  28. /* Global verbose (-d) stuff *********************************/
  29. /*
  30. * define DEBUG via global Makefile variable
  31. * Debug output is sent to stderr, do:
  32. * cpupower monitor 2>/tmp/debug
  33. * to split debug output away from normal output
  34. */
  35. #ifdef DEBUG
  36. extern int be_verbose;
  37. #define dprint(fmt, ...) { \
  38. if (be_verbose) { \
  39. fprintf(stderr, "%s: " fmt, \
  40. __func__, ##__VA_ARGS__); \
  41. } \
  42. }
  43. #else
  44. static inline void dprint(const char *fmt, ...) { }
  45. #endif
  46. extern int be_verbose;
  47. /* Global verbose (-v) stuff *********************************/
  48. /* cpuid and cpuinfo helpers **************************/
  49. enum cpupower_cpu_vendor {X86_VENDOR_UNKNOWN = 0, X86_VENDOR_INTEL,
  50. X86_VENDOR_AMD, X86_VENDOR_MAX};
  51. #define CPUPOWER_CAP_INV_TSC 0x00000001
  52. #define CPUPOWER_CAP_APERF 0x00000002
  53. #define CPUPOWER_CAP_AMD_CBP 0x00000004
  54. #define CPUPOWER_CAP_PERF_BIAS 0x00000008
  55. #define CPUPOWER_CAP_HAS_TURBO_RATIO 0x00000010
  56. #define CPUPOWER_CAP_IS_SNB 0x00000020
  57. #define CPUPOWER_CAP_INTEL_IDA 0x00000040
  58. #define MAX_HW_PSTATES 10
  59. struct cpupower_cpu_info {
  60. enum cpupower_cpu_vendor vendor;
  61. unsigned int family;
  62. unsigned int model;
  63. unsigned int stepping;
  64. /* CPU capabilities read out from cpuid */
  65. unsigned long long caps;
  66. };
  67. /* get_cpu_info
  68. *
  69. * Extract CPU vendor, family, model, stepping info from /proc/cpuinfo
  70. *
  71. * Returns 0 on success or a negativ error code
  72. * Only used on x86, below global's struct values are zero/unknown on
  73. * other archs
  74. */
  75. extern int get_cpu_info(unsigned int cpu, struct cpupower_cpu_info *cpu_info);
  76. extern struct cpupower_cpu_info cpupower_cpu_info;
  77. /* cpuid and cpuinfo helpers **************************/
  78. struct cpuid_core_info {
  79. int pkg;
  80. int core;
  81. int cpu;
  82. /* flags */
  83. unsigned int is_online:1;
  84. };
  85. /* CPU topology/hierarchy parsing ******************/
  86. struct cpupower_topology {
  87. /* Amount of CPU cores, packages and threads per core in the system */
  88. unsigned int cores;
  89. unsigned int pkgs;
  90. unsigned int threads; /* per core */
  91. /* Array gets mallocated with cores entries, holding per core info */
  92. struct cpuid_core_info *core_info;
  93. };
  94. extern int get_cpu_topology(struct cpupower_topology *cpu_top);
  95. extern void cpu_topology_release(struct cpupower_topology cpu_top);
  96. /* CPU topology/hierarchy parsing ******************/
  97. /* X86 ONLY ****************************************/
  98. #if defined(__i386__) || defined(__x86_64__)
  99. #include <pci/pci.h>
  100. /* Read/Write msr ****************************/
  101. extern int read_msr(int cpu, unsigned int idx, unsigned long long *val);
  102. extern int write_msr(int cpu, unsigned int idx, unsigned long long val);
  103. extern int msr_intel_set_perf_bias(unsigned int cpu, unsigned int val);
  104. extern int msr_intel_get_perf_bias(unsigned int cpu);
  105. extern unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu);
  106. /* Read/Write msr ****************************/
  107. /* PCI stuff ****************************/
  108. extern int amd_pci_get_num_boost_states(int *active, int *states);
  109. extern struct pci_dev *pci_acc_init(struct pci_access **pacc, int domain,
  110. int bus, int slot, int func, int vendor,
  111. int dev);
  112. extern struct pci_dev *pci_slot_func_init(struct pci_access **pacc,
  113. int slot, int func);
  114. /* PCI stuff ****************************/
  115. /* AMD HW pstate decoding **************************/
  116. extern int decode_pstates(unsigned int cpu, unsigned int cpu_family,
  117. int boost_states, unsigned long *pstates, int *no);
  118. /* AMD HW pstate decoding **************************/
  119. extern int cpufreq_has_boost_support(unsigned int cpu, int *support,
  120. int *active, int * states);
  121. /*
  122. * CPUID functions returning a single datum
  123. */
  124. unsigned int cpuid_eax(unsigned int op);
  125. unsigned int cpuid_ebx(unsigned int op);
  126. unsigned int cpuid_ecx(unsigned int op);
  127. unsigned int cpuid_edx(unsigned int op);
  128. /* cpuid and cpuinfo helpers **************************/
  129. /* X86 ONLY ********************************************/
  130. #else
  131. static inline int decode_pstates(unsigned int cpu, unsigned int cpu_family,
  132. int boost_states, unsigned long *pstates,
  133. int *no)
  134. { return -1; };
  135. static inline int read_msr(int cpu, unsigned int idx, unsigned long long *val)
  136. { return -1; };
  137. static inline int write_msr(int cpu, unsigned int idx, unsigned long long val)
  138. { return -1; };
  139. static inline int msr_intel_set_perf_bias(unsigned int cpu, unsigned int val)
  140. { return -1; };
  141. static inline int msr_intel_get_perf_bias(unsigned int cpu)
  142. { return -1; };
  143. static inline unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
  144. { return 0; };
  145. /* Read/Write msr ****************************/
  146. static inline int cpufreq_has_boost_support(unsigned int cpu, int *support,
  147. int *active, int * states)
  148. { return -1; }
  149. /* cpuid and cpuinfo helpers **************************/
  150. static inline unsigned int cpuid_eax(unsigned int op) { return 0; };
  151. static inline unsigned int cpuid_ebx(unsigned int op) { return 0; };
  152. static inline unsigned int cpuid_ecx(unsigned int op) { return 0; };
  153. static inline unsigned int cpuid_edx(unsigned int op) { return 0; };
  154. #endif /* defined(__i386__) || defined(__x86_64__) */
  155. #endif /* __CPUPOWERUTILS_HELPERS__ */