nv04_pm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <drm/drmP.h>
  25. #include "nouveau_drm.h"
  26. #include "nouveau_reg.h"
  27. #include "nouveau_hw.h"
  28. #include "nouveau_pm.h"
  29. #include <subdev/bios/pll.h>
  30. #include <subdev/clock.h>
  31. #include <subdev/timer.h>
  32. int
  33. nv04_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  34. {
  35. int ret;
  36. ret = nouveau_hw_get_clock(dev, PLL_CORE);
  37. if (ret < 0)
  38. return ret;
  39. perflvl->core = ret;
  40. ret = nouveau_hw_get_clock(dev, PLL_MEMORY);
  41. if (ret < 0)
  42. return ret;
  43. perflvl->memory = ret;
  44. return 0;
  45. }
  46. struct nv04_pm_clock {
  47. struct nvbios_pll pll;
  48. struct nouveau_pll_vals calc;
  49. };
  50. struct nv04_pm_state {
  51. struct nv04_pm_clock core;
  52. struct nv04_pm_clock memory;
  53. };
  54. static int
  55. calc_pll(struct drm_device *dev, u32 id, int khz, struct nv04_pm_clock *clk)
  56. {
  57. struct nouveau_device *device = nouveau_dev(dev);
  58. struct nouveau_bios *bios = nouveau_bios(device);
  59. struct nouveau_clock *pclk = nouveau_clock(device);
  60. int ret;
  61. ret = nvbios_pll_parse(bios, id, &clk->pll);
  62. if (ret)
  63. return ret;
  64. ret = pclk->pll_calc(pclk, &clk->pll, khz, &clk->calc);
  65. if (!ret)
  66. return -EINVAL;
  67. return 0;
  68. }
  69. void *
  70. nv04_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  71. {
  72. struct nv04_pm_state *info;
  73. int ret;
  74. info = kzalloc(sizeof(*info), GFP_KERNEL);
  75. if (!info)
  76. return ERR_PTR(-ENOMEM);
  77. ret = calc_pll(dev, PLL_CORE, perflvl->core, &info->core);
  78. if (ret)
  79. goto error;
  80. if (perflvl->memory) {
  81. ret = calc_pll(dev, PLL_MEMORY, perflvl->memory, &info->memory);
  82. if (ret)
  83. goto error;
  84. }
  85. return info;
  86. error:
  87. kfree(info);
  88. return ERR_PTR(ret);
  89. }
  90. static void
  91. prog_pll(struct drm_device *dev, struct nv04_pm_clock *clk)
  92. {
  93. struct nouveau_device *device = nouveau_dev(dev);
  94. struct nouveau_clock *pclk = nouveau_clock(device);
  95. u32 reg = clk->pll.reg;
  96. /* thank the insane nouveau_hw_setpll() interface for this */
  97. if (device->card_type >= NV_40)
  98. reg += 4;
  99. pclk->pll_prog(pclk, reg, &clk->calc);
  100. }
  101. int
  102. nv04_pm_clocks_set(struct drm_device *dev, void *pre_state)
  103. {
  104. struct nouveau_device *device = nouveau_dev(dev);
  105. struct nouveau_timer *ptimer = nouveau_timer(device);
  106. struct nv04_pm_state *state = pre_state;
  107. prog_pll(dev, &state->core);
  108. if (state->memory.pll.reg) {
  109. prog_pll(dev, &state->memory);
  110. if (device->card_type < NV_30) {
  111. if (device->card_type == NV_20)
  112. nv_mask(device, 0x1002c4, 0, 1 << 20);
  113. /* Reset the DLLs */
  114. nv_mask(device, 0x1002c0, 0, 1 << 8);
  115. }
  116. }
  117. nv_ofuncs(ptimer)->init(nv_object(ptimer));
  118. kfree(state);
  119. return 0;
  120. }