power_budget.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2016 Karol Herbst
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Karol Herbst
  23. */
  24. #include <subdev/bios.h>
  25. #include <subdev/bios/bit.h>
  26. #include <subdev/bios/power_budget.h>
  27. static u32
  28. nvbios_power_budget_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt,
  29. u8 *len)
  30. {
  31. struct bit_entry bit_P;
  32. u32 power_budget;
  33. if (bit_entry(bios, 'P', &bit_P) || bit_P.version != 2 ||
  34. bit_P.length < 0x2c)
  35. return 0;
  36. power_budget = nvbios_rd32(bios, bit_P.offset + 0x2c);
  37. if (!power_budget)
  38. return 0;
  39. *ver = nvbios_rd08(bios, power_budget);
  40. switch (*ver) {
  41. case 0x20:
  42. case 0x30:
  43. *hdr = nvbios_rd08(bios, power_budget + 0x1);
  44. *len = nvbios_rd08(bios, power_budget + 0x2);
  45. *cnt = nvbios_rd08(bios, power_budget + 0x3);
  46. return power_budget;
  47. default:
  48. break;
  49. }
  50. return 0;
  51. }
  52. int
  53. nvbios_power_budget_header(struct nvkm_bios *bios,
  54. struct nvbios_power_budget *budget)
  55. {
  56. struct nvkm_subdev *subdev = &bios->subdev;
  57. u8 ver, hdr, cnt, len, cap_entry;
  58. u32 header;
  59. if (!bios || !budget)
  60. return -EINVAL;
  61. header = nvbios_power_budget_table(bios, &ver, &hdr, &cnt, &len);
  62. if (!header || !cnt)
  63. return -ENODEV;
  64. switch (ver) {
  65. case 0x20:
  66. cap_entry = nvbios_rd08(bios, header + 0x9);
  67. break;
  68. case 0x30:
  69. cap_entry = nvbios_rd08(bios, header + 0xa);
  70. break;
  71. default:
  72. cap_entry = 0xff;
  73. }
  74. if (cap_entry >= cnt && cap_entry != 0xff) {
  75. nvkm_warn(subdev,
  76. "invalid cap_entry in power budget table found\n");
  77. budget->cap_entry = 0xff;
  78. return -EINVAL;
  79. }
  80. budget->offset = header;
  81. budget->ver = ver;
  82. budget->hlen = hdr;
  83. budget->elen = len;
  84. budget->ecount = cnt;
  85. budget->cap_entry = cap_entry;
  86. return 0;
  87. }
  88. int
  89. nvbios_power_budget_entry(struct nvkm_bios *bios,
  90. struct nvbios_power_budget *budget,
  91. u8 idx, struct nvbios_power_budget_entry *entry)
  92. {
  93. u32 entry_offset;
  94. if (!bios || !budget || !budget->offset || idx >= budget->ecount
  95. || !entry)
  96. return -EINVAL;
  97. entry_offset = budget->offset + budget->hlen + idx * budget->elen;
  98. if (budget->ver >= 0x20) {
  99. entry->min_w = nvbios_rd32(bios, entry_offset + 0x2);
  100. entry->avg_w = nvbios_rd32(bios, entry_offset + 0x6);
  101. entry->max_w = nvbios_rd32(bios, entry_offset + 0xa);
  102. } else {
  103. entry->min_w = 0;
  104. entry->max_w = nvbios_rd32(bios, entry_offset + 0x2);
  105. entry->avg_w = entry->max_w;
  106. }
  107. return 0;
  108. }