base.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2013 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 "priv.h"
  25. #include <subdev/timer.h>
  26. void
  27. nvkm_pmu_pgob(struct nvkm_pmu *pmu, bool enable)
  28. {
  29. if (pmu && pmu->func->pgob)
  30. pmu->func->pgob(pmu, enable);
  31. }
  32. static void
  33. nvkm_pmu_recv(struct work_struct *work)
  34. {
  35. struct nvkm_pmu *pmu = container_of(work, typeof(*pmu), recv.work);
  36. return pmu->func->recv(pmu);
  37. }
  38. int
  39. nvkm_pmu_send(struct nvkm_pmu *pmu, u32 reply[2],
  40. u32 process, u32 message, u32 data0, u32 data1)
  41. {
  42. if (!pmu || !pmu->func->send)
  43. return -ENODEV;
  44. return pmu->func->send(pmu, reply, process, message, data0, data1);
  45. }
  46. static void
  47. nvkm_pmu_intr(struct nvkm_subdev *subdev)
  48. {
  49. struct nvkm_pmu *pmu = nvkm_pmu(subdev);
  50. if (!pmu->func->intr)
  51. return;
  52. pmu->func->intr(pmu);
  53. }
  54. static int
  55. nvkm_pmu_fini(struct nvkm_subdev *subdev, bool suspend)
  56. {
  57. struct nvkm_pmu *pmu = nvkm_pmu(subdev);
  58. if (pmu->func->fini)
  59. pmu->func->fini(pmu);
  60. flush_work(&pmu->recv.work);
  61. return 0;
  62. }
  63. static int
  64. nvkm_pmu_reset(struct nvkm_pmu *pmu)
  65. {
  66. struct nvkm_device *device = pmu->subdev.device;
  67. if (!(nvkm_rd32(device, 0x000200) & 0x00002000))
  68. return 0;
  69. /* Inhibit interrupts, and wait for idle. */
  70. nvkm_wr32(device, 0x10a014, 0x0000ffff);
  71. nvkm_msec(device, 2000,
  72. if (!nvkm_rd32(device, 0x10a04c))
  73. break;
  74. );
  75. /* Reset. */
  76. pmu->func->reset(pmu);
  77. /* Wait for IMEM/DMEM scrubbing to be complete. */
  78. nvkm_msec(device, 2000,
  79. if (!(nvkm_rd32(device, 0x10a10c) & 0x00000006))
  80. break;
  81. );
  82. return 0;
  83. }
  84. static int
  85. nvkm_pmu_preinit(struct nvkm_subdev *subdev)
  86. {
  87. struct nvkm_pmu *pmu = nvkm_pmu(subdev);
  88. return nvkm_pmu_reset(pmu);
  89. }
  90. static int
  91. nvkm_pmu_init(struct nvkm_subdev *subdev)
  92. {
  93. struct nvkm_pmu *pmu = nvkm_pmu(subdev);
  94. int ret = nvkm_pmu_reset(pmu);
  95. if (ret == 0 && pmu->func->init)
  96. ret = pmu->func->init(pmu);
  97. return ret;
  98. }
  99. static void *
  100. nvkm_pmu_dtor(struct nvkm_subdev *subdev)
  101. {
  102. return nvkm_pmu(subdev);
  103. }
  104. static const struct nvkm_subdev_func
  105. nvkm_pmu = {
  106. .dtor = nvkm_pmu_dtor,
  107. .preinit = nvkm_pmu_preinit,
  108. .init = nvkm_pmu_init,
  109. .fini = nvkm_pmu_fini,
  110. .intr = nvkm_pmu_intr,
  111. };
  112. int
  113. nvkm_pmu_new_(const struct nvkm_pmu_func *func, struct nvkm_device *device,
  114. int index, struct nvkm_pmu **ppmu)
  115. {
  116. struct nvkm_pmu *pmu;
  117. if (!(pmu = *ppmu = kzalloc(sizeof(*pmu), GFP_KERNEL)))
  118. return -ENOMEM;
  119. nvkm_subdev_ctor(&nvkm_pmu, device, index, &pmu->subdev);
  120. pmu->func = func;
  121. INIT_WORK(&pmu->recv.work, nvkm_pmu_recv);
  122. init_waitqueue_head(&pmu->recv.wait);
  123. return 0;
  124. }