base.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 2012 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 <core/option.h>
  26. #include <subdev/top.h>
  27. void
  28. nvkm_mc_unk260(struct nvkm_device *device, u32 data)
  29. {
  30. struct nvkm_mc *mc = device->mc;
  31. if (likely(mc) && mc->func->unk260)
  32. mc->func->unk260(mc, data);
  33. }
  34. void
  35. nvkm_mc_intr_mask(struct nvkm_device *device, enum nvkm_devidx devidx, bool en)
  36. {
  37. struct nvkm_mc *mc = device->mc;
  38. const struct nvkm_mc_map *map;
  39. if (likely(mc) && mc->func->intr_mask) {
  40. u32 mask = nvkm_top_intr_mask(device, devidx);
  41. for (map = mc->func->intr; !mask && map->stat; map++) {
  42. if (map->unit == devidx)
  43. mask = map->stat;
  44. }
  45. mc->func->intr_mask(mc, mask, en ? mask : 0);
  46. }
  47. }
  48. void
  49. nvkm_mc_intr_unarm(struct nvkm_device *device)
  50. {
  51. struct nvkm_mc *mc = device->mc;
  52. if (likely(mc))
  53. mc->func->intr_unarm(mc);
  54. }
  55. void
  56. nvkm_mc_intr_rearm(struct nvkm_device *device)
  57. {
  58. struct nvkm_mc *mc = device->mc;
  59. if (likely(mc))
  60. mc->func->intr_rearm(mc);
  61. }
  62. static u32
  63. nvkm_mc_intr_stat(struct nvkm_mc *mc)
  64. {
  65. u32 intr = mc->func->intr_stat(mc);
  66. if (WARN_ON_ONCE(intr == 0xffffffff))
  67. intr = 0; /* likely fallen off the bus */
  68. return intr;
  69. }
  70. void
  71. nvkm_mc_intr(struct nvkm_device *device, bool *handled)
  72. {
  73. struct nvkm_mc *mc = device->mc;
  74. struct nvkm_subdev *subdev;
  75. const struct nvkm_mc_map *map;
  76. u32 stat, intr;
  77. u64 subdevs;
  78. if (unlikely(!mc))
  79. return;
  80. intr = nvkm_mc_intr_stat(mc);
  81. stat = nvkm_top_intr(device, intr, &subdevs);
  82. while (subdevs) {
  83. enum nvkm_devidx subidx = __ffs64(subdevs);
  84. subdev = nvkm_device_subdev(device, subidx);
  85. if (subdev)
  86. nvkm_subdev_intr(subdev);
  87. subdevs &= ~BIT_ULL(subidx);
  88. }
  89. for (map = mc->func->intr; map->stat; map++) {
  90. if (intr & map->stat) {
  91. subdev = nvkm_device_subdev(device, map->unit);
  92. if (subdev)
  93. nvkm_subdev_intr(subdev);
  94. stat &= ~map->stat;
  95. }
  96. }
  97. if (stat)
  98. nvkm_error(&mc->subdev, "intr %08x\n", stat);
  99. *handled = intr != 0;
  100. }
  101. static u32
  102. nvkm_mc_reset_mask(struct nvkm_device *device, bool isauto,
  103. enum nvkm_devidx devidx)
  104. {
  105. struct nvkm_mc *mc = device->mc;
  106. const struct nvkm_mc_map *map;
  107. u64 pmc_enable = 0;
  108. if (likely(mc)) {
  109. if (!(pmc_enable = nvkm_top_reset(device, devidx))) {
  110. for (map = mc->func->reset; map && map->stat; map++) {
  111. if (!isauto || !map->noauto) {
  112. if (map->unit == devidx) {
  113. pmc_enable = map->stat;
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. return pmc_enable;
  121. }
  122. void
  123. nvkm_mc_reset(struct nvkm_device *device, enum nvkm_devidx devidx)
  124. {
  125. u64 pmc_enable = nvkm_mc_reset_mask(device, true, devidx);
  126. if (pmc_enable) {
  127. nvkm_mask(device, 0x000200, pmc_enable, 0x00000000);
  128. nvkm_mask(device, 0x000200, pmc_enable, pmc_enable);
  129. nvkm_rd32(device, 0x000200);
  130. }
  131. }
  132. void
  133. nvkm_mc_disable(struct nvkm_device *device, enum nvkm_devidx devidx)
  134. {
  135. u64 pmc_enable = nvkm_mc_reset_mask(device, false, devidx);
  136. if (pmc_enable)
  137. nvkm_mask(device, 0x000200, pmc_enable, 0x00000000);
  138. }
  139. void
  140. nvkm_mc_enable(struct nvkm_device *device, enum nvkm_devidx devidx)
  141. {
  142. u64 pmc_enable = nvkm_mc_reset_mask(device, false, devidx);
  143. if (pmc_enable) {
  144. nvkm_mask(device, 0x000200, pmc_enable, pmc_enable);
  145. nvkm_rd32(device, 0x000200);
  146. }
  147. }
  148. static int
  149. nvkm_mc_fini(struct nvkm_subdev *subdev, bool suspend)
  150. {
  151. nvkm_mc_intr_unarm(subdev->device);
  152. return 0;
  153. }
  154. static int
  155. nvkm_mc_init(struct nvkm_subdev *subdev)
  156. {
  157. struct nvkm_mc *mc = nvkm_mc(subdev);
  158. if (mc->func->init)
  159. mc->func->init(mc);
  160. nvkm_mc_intr_rearm(subdev->device);
  161. return 0;
  162. }
  163. static void *
  164. nvkm_mc_dtor(struct nvkm_subdev *subdev)
  165. {
  166. return nvkm_mc(subdev);
  167. }
  168. static const struct nvkm_subdev_func
  169. nvkm_mc = {
  170. .dtor = nvkm_mc_dtor,
  171. .init = nvkm_mc_init,
  172. .fini = nvkm_mc_fini,
  173. };
  174. void
  175. nvkm_mc_ctor(const struct nvkm_mc_func *func, struct nvkm_device *device,
  176. int index, struct nvkm_mc *mc)
  177. {
  178. nvkm_subdev_ctor(&nvkm_mc, device, index, &mc->subdev);
  179. mc->func = func;
  180. }
  181. int
  182. nvkm_mc_new_(const struct nvkm_mc_func *func, struct nvkm_device *device,
  183. int index, struct nvkm_mc **pmc)
  184. {
  185. struct nvkm_mc *mc;
  186. if (!(mc = *pmc = kzalloc(sizeof(*mc), GFP_KERNEL)))
  187. return -ENOMEM;
  188. nvkm_mc_ctor(func, device, index, *pmc);
  189. return 0;
  190. }