nouveau_pm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 2010 Red Hat Inc.
  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: Ben Skeggs
  23. */
  24. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_pm.h"
  27. static int
  28. nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  29. {
  30. struct drm_nouveau_private *dev_priv = dev->dev_private;
  31. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  32. int ret;
  33. if (!pm->clock_get)
  34. return -EINVAL;
  35. memset(perflvl, 0, sizeof(*perflvl));
  36. ret = pm->clock_get(dev, PLL_CORE);
  37. if (ret > 0)
  38. perflvl->core = ret;
  39. ret = pm->clock_get(dev, PLL_MEMORY);
  40. if (ret > 0)
  41. perflvl->memory = ret;
  42. ret = pm->clock_get(dev, PLL_SHADER);
  43. if (ret > 0)
  44. perflvl->shader = ret;
  45. ret = pm->clock_get(dev, PLL_UNK05);
  46. if (ret > 0)
  47. perflvl->unk05 = ret;
  48. if (pm->voltage.supported && pm->voltage_get) {
  49. ret = pm->voltage_get(dev);
  50. if (ret > 0)
  51. perflvl->voltage = ret;
  52. }
  53. return 0;
  54. }
  55. static void
  56. nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
  57. {
  58. char s[16], v[16], f[16];
  59. s[0] = '\0';
  60. if (perflvl->shader)
  61. snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
  62. v[0] = '\0';
  63. if (perflvl->voltage)
  64. snprintf(v, sizeof(v), " voltage %dmV", perflvl->voltage * 10);
  65. f[0] = '\0';
  66. if (perflvl->fanspeed)
  67. snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
  68. snprintf(ptr, len, "core %dMHz memory %dMHz%s%s%s\n",
  69. perflvl->core / 1000, perflvl->memory / 1000, s, v, f);
  70. }
  71. static ssize_t
  72. nouveau_pm_get_perflvl_info(struct device *d,
  73. struct device_attribute *a, char *buf)
  74. {
  75. struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
  76. char *ptr = buf;
  77. int len = PAGE_SIZE;
  78. snprintf(ptr, len, "%d: ", perflvl->id);
  79. ptr += strlen(buf);
  80. len -= strlen(buf);
  81. nouveau_pm_perflvl_info(perflvl, ptr, len);
  82. return strlen(buf);
  83. }
  84. static ssize_t
  85. nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
  86. {
  87. struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
  88. struct drm_nouveau_private *dev_priv = dev->dev_private;
  89. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  90. struct nouveau_pm_level cur;
  91. int len = PAGE_SIZE, ret;
  92. char *ptr = buf;
  93. if (!pm->cur)
  94. snprintf(ptr, len, "setting: boot\n");
  95. else if (pm->cur == &pm->boot)
  96. snprintf(ptr, len, "setting: boot\nc: ");
  97. else
  98. snprintf(ptr, len, "setting: static %d\nc: ", pm->cur->id);
  99. ptr += strlen(buf);
  100. len -= strlen(buf);
  101. ret = nouveau_pm_perflvl_get(dev, &cur);
  102. if (ret == 0)
  103. nouveau_pm_perflvl_info(&cur, ptr, len);
  104. return strlen(buf);
  105. }
  106. static ssize_t
  107. nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
  108. const char *buf, size_t count)
  109. {
  110. return -EPERM;
  111. }
  112. DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
  113. nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
  114. int
  115. nouveau_pm_init(struct drm_device *dev)
  116. {
  117. struct drm_nouveau_private *dev_priv = dev->dev_private;
  118. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  119. struct device *d = &dev->pdev->dev;
  120. char info[256];
  121. int ret, i;
  122. nouveau_volt_init(dev);
  123. nouveau_perf_init(dev);
  124. NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
  125. for (i = 0; i < pm->nr_perflvl; i++) {
  126. nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
  127. NV_INFO(dev, "%d: %s", pm->perflvl[i].id, info);
  128. }
  129. /* determine current ("boot") performance level */
  130. ret = nouveau_pm_perflvl_get(dev, &pm->boot);
  131. if (ret == 0) {
  132. pm->cur = &pm->boot;
  133. nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
  134. NV_INFO(dev, "c: %s", info);
  135. }
  136. /* initialise sysfs */
  137. ret = device_create_file(d, &dev_attr_performance_level);
  138. if (ret)
  139. return ret;
  140. for (i = 0; i < pm->nr_perflvl; i++) {
  141. struct nouveau_pm_level *perflvl = &pm->perflvl[i];
  142. perflvl->dev_attr.attr.name = perflvl->name;
  143. perflvl->dev_attr.attr.mode = S_IRUGO;
  144. perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
  145. perflvl->dev_attr.store = NULL;
  146. sysfs_attr_init(&perflvl->dev_attr.attr);
  147. ret = device_create_file(d, &perflvl->dev_attr);
  148. if (ret) {
  149. NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
  150. perflvl->id, i);
  151. perflvl->dev_attr.attr.name = NULL;
  152. nouveau_pm_fini(dev);
  153. return ret;
  154. }
  155. }
  156. return 0;
  157. }
  158. void
  159. nouveau_pm_fini(struct drm_device *dev)
  160. {
  161. struct drm_nouveau_private *dev_priv = dev->dev_private;
  162. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  163. struct device *d = &dev->pdev->dev;
  164. int i;
  165. device_remove_file(d, &dev_attr_performance_level);
  166. for (i = 0; i < pm->nr_perflvl; i++) {
  167. struct nouveau_pm_level *pl = &pm->perflvl[i];
  168. if (!pl->dev_attr.attr.name)
  169. break;
  170. device_remove_file(d, &pl->dev_attr);
  171. }
  172. nouveau_perf_fini(dev);
  173. nouveau_volt_fini(dev);
  174. }