base.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "priv.h"
  23. #include <subdev/mc.h>
  24. void
  25. nvkm_falcon_load_imem(struct nvkm_falcon *falcon, void *data, u32 start,
  26. u32 size, u16 tag, u8 port, bool secure)
  27. {
  28. if (secure && !falcon->secret) {
  29. nvkm_warn(falcon->user,
  30. "writing with secure tag on a non-secure falcon!\n");
  31. return;
  32. }
  33. falcon->func->load_imem(falcon, data, start, size, tag, port,
  34. secure);
  35. }
  36. void
  37. nvkm_falcon_load_dmem(struct nvkm_falcon *falcon, void *data, u32 start,
  38. u32 size, u8 port)
  39. {
  40. mutex_lock(&falcon->dmem_mutex);
  41. falcon->func->load_dmem(falcon, data, start, size, port);
  42. mutex_unlock(&falcon->dmem_mutex);
  43. }
  44. void
  45. nvkm_falcon_read_dmem(struct nvkm_falcon *falcon, u32 start, u32 size, u8 port,
  46. void *data)
  47. {
  48. mutex_lock(&falcon->dmem_mutex);
  49. falcon->func->read_dmem(falcon, start, size, port, data);
  50. mutex_unlock(&falcon->dmem_mutex);
  51. }
  52. void
  53. nvkm_falcon_bind_context(struct nvkm_falcon *falcon, struct nvkm_memory *inst)
  54. {
  55. if (!falcon->func->bind_context) {
  56. nvkm_error(falcon->user,
  57. "Context binding not supported on this falcon!\n");
  58. return;
  59. }
  60. falcon->func->bind_context(falcon, inst);
  61. }
  62. void
  63. nvkm_falcon_set_start_addr(struct nvkm_falcon *falcon, u32 start_addr)
  64. {
  65. falcon->func->set_start_addr(falcon, start_addr);
  66. }
  67. void
  68. nvkm_falcon_start(struct nvkm_falcon *falcon)
  69. {
  70. falcon->func->start(falcon);
  71. }
  72. int
  73. nvkm_falcon_enable(struct nvkm_falcon *falcon)
  74. {
  75. struct nvkm_device *device = falcon->owner->device;
  76. enum nvkm_devidx id = falcon->owner->index;
  77. int ret;
  78. nvkm_mc_enable(device, id);
  79. ret = falcon->func->enable(falcon);
  80. if (ret) {
  81. nvkm_mc_disable(device, id);
  82. return ret;
  83. }
  84. return 0;
  85. }
  86. void
  87. nvkm_falcon_disable(struct nvkm_falcon *falcon)
  88. {
  89. struct nvkm_device *device = falcon->owner->device;
  90. enum nvkm_devidx id = falcon->owner->index;
  91. /* already disabled, return or wait_idle will timeout */
  92. if (!nvkm_mc_enabled(device, id))
  93. return;
  94. falcon->func->disable(falcon);
  95. nvkm_mc_disable(device, id);
  96. }
  97. int
  98. nvkm_falcon_reset(struct nvkm_falcon *falcon)
  99. {
  100. nvkm_falcon_disable(falcon);
  101. return nvkm_falcon_enable(falcon);
  102. }
  103. int
  104. nvkm_falcon_wait_for_halt(struct nvkm_falcon *falcon, u32 ms)
  105. {
  106. return falcon->func->wait_for_halt(falcon, ms);
  107. }
  108. int
  109. nvkm_falcon_clear_interrupt(struct nvkm_falcon *falcon, u32 mask)
  110. {
  111. return falcon->func->clear_interrupt(falcon, mask);
  112. }
  113. void
  114. nvkm_falcon_put(struct nvkm_falcon *falcon, const struct nvkm_subdev *user)
  115. {
  116. if (unlikely(!falcon))
  117. return;
  118. mutex_lock(&falcon->mutex);
  119. if (falcon->user == user) {
  120. nvkm_debug(falcon->user, "released %s falcon\n", falcon->name);
  121. falcon->user = NULL;
  122. }
  123. mutex_unlock(&falcon->mutex);
  124. }
  125. int
  126. nvkm_falcon_get(struct nvkm_falcon *falcon, const struct nvkm_subdev *user)
  127. {
  128. mutex_lock(&falcon->mutex);
  129. if (falcon->user) {
  130. nvkm_error(user, "%s falcon already acquired by %s!\n",
  131. falcon->name, nvkm_subdev_name[falcon->user->index]);
  132. mutex_unlock(&falcon->mutex);
  133. return -EBUSY;
  134. }
  135. nvkm_debug(user, "acquired %s falcon\n", falcon->name);
  136. falcon->user = user;
  137. mutex_unlock(&falcon->mutex);
  138. return 0;
  139. }
  140. void
  141. nvkm_falcon_ctor(const struct nvkm_falcon_func *func,
  142. struct nvkm_subdev *subdev, const char *name, u32 addr,
  143. struct nvkm_falcon *falcon)
  144. {
  145. u32 debug_reg;
  146. u32 reg;
  147. falcon->func = func;
  148. falcon->owner = subdev;
  149. falcon->name = name;
  150. falcon->addr = addr;
  151. mutex_init(&falcon->mutex);
  152. mutex_init(&falcon->dmem_mutex);
  153. reg = nvkm_falcon_rd32(falcon, 0x12c);
  154. falcon->version = reg & 0xf;
  155. falcon->secret = (reg >> 4) & 0x3;
  156. falcon->code.ports = (reg >> 8) & 0xf;
  157. falcon->data.ports = (reg >> 12) & 0xf;
  158. reg = nvkm_falcon_rd32(falcon, 0x108);
  159. falcon->code.limit = (reg & 0x1ff) << 8;
  160. falcon->data.limit = (reg & 0x3fe00) >> 1;
  161. switch (subdev->index) {
  162. case NVKM_ENGINE_GR:
  163. debug_reg = 0x0;
  164. break;
  165. case NVKM_SUBDEV_PMU:
  166. debug_reg = 0xc08;
  167. break;
  168. case NVKM_ENGINE_NVDEC:
  169. debug_reg = 0xd00;
  170. break;
  171. case NVKM_ENGINE_SEC2:
  172. debug_reg = 0x408;
  173. falcon->has_emem = true;
  174. break;
  175. default:
  176. nvkm_warn(subdev, "unsupported falcon %s!\n",
  177. nvkm_subdev_name[subdev->index]);
  178. debug_reg = 0;
  179. break;
  180. }
  181. if (debug_reg) {
  182. u32 val = nvkm_falcon_rd32(falcon, debug_reg);
  183. falcon->debug = (val >> 20) & 0x1;
  184. }
  185. }
  186. void
  187. nvkm_falcon_del(struct nvkm_falcon **pfalcon)
  188. {
  189. if (*pfalcon) {
  190. kfree(*pfalcon);
  191. *pfalcon = NULL;
  192. }
  193. }