amd.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0
  2. #if defined(__i386__) || defined(__x86_64__)
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <pci/pci.h>
  8. #include "helpers/helpers.h"
  9. #define MSR_AMD_PSTATE_STATUS 0xc0010063
  10. #define MSR_AMD_PSTATE 0xc0010064
  11. #define MSR_AMD_PSTATE_LIMIT 0xc0010061
  12. union msr_pstate {
  13. struct {
  14. unsigned fid:6;
  15. unsigned did:3;
  16. unsigned vid:7;
  17. unsigned res1:6;
  18. unsigned nbdid:1;
  19. unsigned res2:2;
  20. unsigned nbvid:7;
  21. unsigned iddval:8;
  22. unsigned idddiv:2;
  23. unsigned res3:21;
  24. unsigned en:1;
  25. } bits;
  26. struct {
  27. unsigned fid:8;
  28. unsigned did:6;
  29. unsigned vid:8;
  30. unsigned iddval:8;
  31. unsigned idddiv:2;
  32. unsigned res1:31;
  33. unsigned en:1;
  34. } fam17h_bits;
  35. unsigned long long val;
  36. };
  37. static int get_did(int family, union msr_pstate pstate)
  38. {
  39. int t;
  40. if (family == 0x12)
  41. t = pstate.val & 0xf;
  42. else if (family == 0x17)
  43. t = pstate.fam17h_bits.did;
  44. else
  45. t = pstate.bits.did;
  46. return t;
  47. }
  48. static int get_cof(int family, union msr_pstate pstate)
  49. {
  50. int t;
  51. int fid, did, cof;
  52. did = get_did(family, pstate);
  53. if (family == 0x17) {
  54. fid = pstate.fam17h_bits.fid;
  55. cof = 200 * fid / did;
  56. } else {
  57. t = 0x10;
  58. fid = pstate.bits.fid;
  59. if (family == 0x11)
  60. t = 0x8;
  61. cof = (100 * (fid + t)) >> did;
  62. }
  63. return cof;
  64. }
  65. /* Needs:
  66. * cpu -> the cpu that gets evaluated
  67. * cpu_family -> The cpu's family (0x10, 0x12,...)
  68. * boots_states -> how much boost states the machines support
  69. *
  70. * Fills up:
  71. * pstates -> a pointer to an array of size MAX_HW_PSTATES
  72. * must be initialized with zeros.
  73. * All available HW pstates (including boost states)
  74. * no -> amount of pstates above array got filled up with
  75. *
  76. * returns zero on success, -1 on failure
  77. */
  78. int decode_pstates(unsigned int cpu, unsigned int cpu_family,
  79. int boost_states, unsigned long *pstates, int *no)
  80. {
  81. int i, psmax, pscur;
  82. union msr_pstate pstate;
  83. unsigned long long val;
  84. /* Only read out frequencies from HW when CPU might be boostable
  85. to keep the code as short and clean as possible.
  86. Otherwise frequencies are exported via ACPI tables.
  87. */
  88. if (cpu_family < 0x10 || cpu_family == 0x14)
  89. return -1;
  90. if (read_msr(cpu, MSR_AMD_PSTATE_LIMIT, &val))
  91. return -1;
  92. psmax = (val >> 4) & 0x7;
  93. if (read_msr(cpu, MSR_AMD_PSTATE_STATUS, &val))
  94. return -1;
  95. pscur = val & 0x7;
  96. pscur += boost_states;
  97. psmax += boost_states;
  98. for (i = 0; i <= psmax; i++) {
  99. if (i >= MAX_HW_PSTATES) {
  100. fprintf(stderr, "HW pstates [%d] exceeding max [%d]\n",
  101. psmax, MAX_HW_PSTATES);
  102. return -1;
  103. }
  104. if (read_msr(cpu, MSR_AMD_PSTATE + i, &pstate.val))
  105. return -1;
  106. if ((cpu_family == 0x17) && (!pstate.fam17h_bits.en))
  107. continue;
  108. else if (!pstate.bits.en)
  109. continue;
  110. pstates[i] = get_cof(cpu_family, pstate);
  111. }
  112. *no = i;
  113. return 0;
  114. }
  115. int amd_pci_get_num_boost_states(int *active, int *states)
  116. {
  117. struct pci_access *pci_acc;
  118. struct pci_dev *device;
  119. uint8_t val = 0;
  120. *active = *states = 0;
  121. device = pci_slot_func_init(&pci_acc, 0x18, 4);
  122. if (device == NULL)
  123. return -ENODEV;
  124. val = pci_read_byte(device, 0x15c);
  125. if (val & 3)
  126. *active = 1;
  127. else
  128. *active = 0;
  129. *states = (val >> 2) & 7;
  130. pci_cleanup(pci_acc);
  131. return 0;
  132. }
  133. #endif /* defined(__i386__) || defined(__x86_64__) */