helpers.h 5.5 KB

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