base.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. falcon->func->load_dmem(falcon, data, start, size, port);
  41. }
  42. void
  43. nvkm_falcon_read_dmem(struct nvkm_falcon *falcon, u32 start, u32 size, u8 port,
  44. void *data)
  45. {
  46. falcon->func->read_dmem(falcon, start, size, port, data);
  47. }
  48. void
  49. nvkm_falcon_bind_context(struct nvkm_falcon *falcon, struct nvkm_gpuobj *inst)
  50. {
  51. if (!falcon->func->bind_context) {
  52. nvkm_error(falcon->user,
  53. "Context binding not supported on this falcon!\n");
  54. return;
  55. }
  56. falcon->func->bind_context(falcon, inst);
  57. }
  58. void
  59. nvkm_falcon_set_start_addr(struct nvkm_falcon *falcon, u32 start_addr)
  60. {
  61. falcon->func->set_start_addr(falcon, start_addr);
  62. }
  63. void
  64. nvkm_falcon_start(struct nvkm_falcon *falcon)
  65. {
  66. falcon->func->start(falcon);
  67. }
  68. int
  69. nvkm_falcon_enable(struct nvkm_falcon *falcon)
  70. {
  71. struct nvkm_device *device = falcon->owner->device;
  72. enum nvkm_devidx id = falcon->owner->index;
  73. int ret;
  74. nvkm_mc_enable(device, id);
  75. ret = falcon->func->enable(falcon);
  76. if (ret) {
  77. nvkm_mc_disable(device, id);
  78. return ret;
  79. }
  80. return 0;
  81. }
  82. void
  83. nvkm_falcon_disable(struct nvkm_falcon *falcon)
  84. {
  85. struct nvkm_device *device = falcon->owner->device;
  86. enum nvkm_devidx id = falcon->owner->index;
  87. /* already disabled, return or wait_idle will timeout */
  88. if (!nvkm_mc_enabled(device, id))
  89. return;
  90. falcon->func->disable(falcon);
  91. nvkm_mc_disable(device, id);
  92. }
  93. int
  94. nvkm_falcon_reset(struct nvkm_falcon *falcon)
  95. {
  96. nvkm_falcon_disable(falcon);
  97. return nvkm_falcon_enable(falcon);
  98. }
  99. int
  100. nvkm_falcon_wait_for_halt(struct nvkm_falcon *falcon, u32 ms)
  101. {
  102. return falcon->func->wait_for_halt(falcon, ms);
  103. }
  104. int
  105. nvkm_falcon_clear_interrupt(struct nvkm_falcon *falcon, u32 mask)
  106. {
  107. return falcon->func->clear_interrupt(falcon, mask);
  108. }
  109. void
  110. nvkm_falcon_put(struct nvkm_falcon *falcon, const struct nvkm_subdev *user)
  111. {
  112. mutex_lock(&falcon->mutex);
  113. if (falcon->user == user) {
  114. nvkm_debug(falcon->user, "released %s falcon\n", falcon->name);
  115. falcon->user = NULL;
  116. }
  117. mutex_unlock(&falcon->mutex);
  118. }
  119. int
  120. nvkm_falcon_get(struct nvkm_falcon *falcon, const struct nvkm_subdev *user)
  121. {
  122. mutex_lock(&falcon->mutex);
  123. if (falcon->user) {
  124. nvkm_error(user, "%s falcon already acquired by %s!\n",
  125. falcon->name, nvkm_subdev_name[falcon->user->index]);
  126. mutex_unlock(&falcon->mutex);
  127. return -EBUSY;
  128. }
  129. nvkm_debug(user, "acquired %s falcon\n", falcon->name);
  130. falcon->user = user;
  131. mutex_unlock(&falcon->mutex);
  132. return 0;
  133. }
  134. void
  135. nvkm_falcon_ctor(const struct nvkm_falcon_func *func,
  136. struct nvkm_subdev *subdev, const char *name, u32 addr,
  137. struct nvkm_falcon *falcon)
  138. {
  139. u32 reg;
  140. falcon->func = func;
  141. falcon->owner = subdev;
  142. falcon->name = name;
  143. falcon->addr = addr;
  144. mutex_init(&falcon->mutex);
  145. reg = nvkm_falcon_rd32(falcon, 0x12c);
  146. falcon->version = reg & 0xf;
  147. falcon->secret = (reg >> 4) & 0x3;
  148. falcon->code.ports = (reg >> 8) & 0xf;
  149. falcon->data.ports = (reg >> 12) & 0xf;
  150. reg = nvkm_falcon_rd32(falcon, 0x108);
  151. falcon->code.limit = (reg & 0x1ff) << 8;
  152. falcon->data.limit = (reg & 0x3fe00) >> 1;
  153. reg = nvkm_falcon_rd32(falcon, 0xc08);
  154. falcon->debug = (reg >> 20) & 0x1;
  155. }
  156. void
  157. nvkm_falcon_del(struct nvkm_falcon **pfalcon)
  158. {
  159. if (*pfalcon) {
  160. kfree(*pfalcon);
  161. *pfalcon = NULL;
  162. }
  163. }