falcon.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #include <engine/falcon.h>
  23. #include <subdev/timer.h>
  24. void
  25. nouveau_falcon_intr(struct nouveau_subdev *subdev)
  26. {
  27. struct nouveau_falcon *falcon = (void *)subdev;
  28. u32 dispatch = nv_ro32(falcon, 0x01c);
  29. u32 intr = nv_ro32(falcon, 0x008) & dispatch & ~(dispatch >> 16);
  30. if (intr & 0x00000010) {
  31. nv_debug(falcon, "ucode halted\n");
  32. nv_wo32(falcon, 0x004, 0x00000010);
  33. intr &= ~0x00000010;
  34. }
  35. if (intr) {
  36. nv_error(falcon, "unhandled intr 0x%08x\n", intr);
  37. nv_wo32(falcon, 0x004, intr);
  38. }
  39. }
  40. u32
  41. _nouveau_falcon_rd32(struct nouveau_object *object, u64 addr)
  42. {
  43. struct nouveau_falcon *falcon = (void *)object;
  44. return nv_rd32(falcon, falcon->addr + addr);
  45. }
  46. void
  47. _nouveau_falcon_wr32(struct nouveau_object *object, u64 addr, u32 data)
  48. {
  49. struct nouveau_falcon *falcon = (void *)object;
  50. nv_wr32(falcon, falcon->addr + addr, data);
  51. }
  52. static void *
  53. vmemdup(const void *src, size_t len)
  54. {
  55. void *p = vmalloc(len);
  56. if (p)
  57. memcpy(p, src, len);
  58. return p;
  59. }
  60. int
  61. _nouveau_falcon_init(struct nouveau_object *object)
  62. {
  63. struct nouveau_device *device = nv_device(object);
  64. struct nouveau_falcon *falcon = (void *)object;
  65. const struct firmware *fw;
  66. char name[32] = "internal";
  67. int ret, i;
  68. u32 caps;
  69. /* enable engine, and determine its capabilities */
  70. ret = nouveau_engine_init(&falcon->base);
  71. if (ret)
  72. return ret;
  73. if (device->chipset < 0xa3 ||
  74. device->chipset == 0xaa || device->chipset == 0xac) {
  75. falcon->version = 0;
  76. falcon->secret = (falcon->addr == 0x087000) ? 1 : 0;
  77. } else {
  78. caps = nv_ro32(falcon, 0x12c);
  79. falcon->version = (caps & 0x0000000f);
  80. falcon->secret = (caps & 0x00000030) >> 4;
  81. }
  82. caps = nv_ro32(falcon, 0x108);
  83. falcon->code.limit = (caps & 0x000001ff) << 8;
  84. falcon->data.limit = (caps & 0x0003fe00) >> 1;
  85. nv_debug(falcon, "falcon version: %d\n", falcon->version);
  86. nv_debug(falcon, "secret level: %d\n", falcon->secret);
  87. nv_debug(falcon, "code limit: %d\n", falcon->code.limit);
  88. nv_debug(falcon, "data limit: %d\n", falcon->data.limit);
  89. /* wait for 'uc halted' to be signalled before continuing */
  90. if (falcon->secret && falcon->version < 4) {
  91. if (!falcon->version)
  92. nv_wait(falcon, 0x008, 0x00000010, 0x00000010);
  93. else
  94. nv_wait(falcon, 0x180, 0x80000000, 0);
  95. nv_wo32(falcon, 0x004, 0x00000010);
  96. }
  97. /* disable all interrupts */
  98. nv_wo32(falcon, 0x014, 0xffffffff);
  99. /* no default ucode provided by the engine implementation, try and
  100. * locate a "self-bootstrapping" firmware image for the engine
  101. */
  102. if (!falcon->code.data) {
  103. snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03x",
  104. device->chipset, falcon->addr >> 12);
  105. ret = request_firmware(&fw, name, nv_device_base(device));
  106. if (ret == 0) {
  107. falcon->code.data = vmemdup(fw->data, fw->size);
  108. falcon->code.size = fw->size;
  109. falcon->data.data = NULL;
  110. falcon->data.size = 0;
  111. release_firmware(fw);
  112. }
  113. falcon->external = true;
  114. }
  115. /* next step is to try and load "static code/data segment" firmware
  116. * images for the engine
  117. */
  118. if (!falcon->code.data) {
  119. snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03xd",
  120. device->chipset, falcon->addr >> 12);
  121. ret = request_firmware(&fw, name, nv_device_base(device));
  122. if (ret) {
  123. nv_error(falcon, "unable to load firmware data\n");
  124. return ret;
  125. }
  126. falcon->data.data = vmemdup(fw->data, fw->size);
  127. falcon->data.size = fw->size;
  128. release_firmware(fw);
  129. if (!falcon->data.data)
  130. return -ENOMEM;
  131. snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03xc",
  132. device->chipset, falcon->addr >> 12);
  133. ret = request_firmware(&fw, name, nv_device_base(device));
  134. if (ret) {
  135. nv_error(falcon, "unable to load firmware code\n");
  136. return ret;
  137. }
  138. falcon->code.data = vmemdup(fw->data, fw->size);
  139. falcon->code.size = fw->size;
  140. release_firmware(fw);
  141. if (!falcon->code.data)
  142. return -ENOMEM;
  143. }
  144. nv_debug(falcon, "firmware: %s (%s)\n", name, falcon->data.data ?
  145. "static code/data segments" : "self-bootstrapping");
  146. /* ensure any "self-bootstrapping" firmware image is in vram */
  147. if (!falcon->data.data && !falcon->core) {
  148. ret = nouveau_gpuobj_new(object->parent, NULL,
  149. falcon->code.size, 256, 0,
  150. &falcon->core);
  151. if (ret) {
  152. nv_error(falcon, "core allocation failed, %d\n", ret);
  153. return ret;
  154. }
  155. for (i = 0; i < falcon->code.size; i += 4)
  156. nv_wo32(falcon->core, i, falcon->code.data[i / 4]);
  157. }
  158. /* upload firmware bootloader (or the full code segments) */
  159. if (falcon->core) {
  160. if (device->card_type < NV_C0)
  161. nv_wo32(falcon, 0x618, 0x04000000);
  162. else
  163. nv_wo32(falcon, 0x618, 0x00000114);
  164. nv_wo32(falcon, 0x11c, 0);
  165. nv_wo32(falcon, 0x110, falcon->core->addr >> 8);
  166. nv_wo32(falcon, 0x114, 0);
  167. nv_wo32(falcon, 0x118, 0x00006610);
  168. } else {
  169. if (falcon->code.size > falcon->code.limit ||
  170. falcon->data.size > falcon->data.limit) {
  171. nv_error(falcon, "ucode exceeds falcon limit(s)\n");
  172. return -EINVAL;
  173. }
  174. if (falcon->version < 3) {
  175. nv_wo32(falcon, 0xff8, 0x00100000);
  176. for (i = 0; i < falcon->code.size / 4; i++)
  177. nv_wo32(falcon, 0xff4, falcon->code.data[i]);
  178. } else {
  179. nv_wo32(falcon, 0x180, 0x01000000);
  180. for (i = 0; i < falcon->code.size / 4; i++) {
  181. if ((i & 0x3f) == 0)
  182. nv_wo32(falcon, 0x188, i >> 6);
  183. nv_wo32(falcon, 0x184, falcon->code.data[i]);
  184. }
  185. }
  186. }
  187. /* upload data segment (if necessary), zeroing the remainder */
  188. if (falcon->version < 3) {
  189. nv_wo32(falcon, 0xff8, 0x00000000);
  190. for (i = 0; !falcon->core && i < falcon->data.size / 4; i++)
  191. nv_wo32(falcon, 0xff4, falcon->data.data[i]);
  192. for (; i < falcon->data.limit; i += 4)
  193. nv_wo32(falcon, 0xff4, 0x00000000);
  194. } else {
  195. nv_wo32(falcon, 0x1c0, 0x01000000);
  196. for (i = 0; !falcon->core && i < falcon->data.size / 4; i++)
  197. nv_wo32(falcon, 0x1c4, falcon->data.data[i]);
  198. for (; i < falcon->data.limit / 4; i++)
  199. nv_wo32(falcon, 0x1c4, 0x00000000);
  200. }
  201. /* start it running */
  202. nv_wo32(falcon, 0x10c, 0x00000001); /* BLOCK_ON_FIFO */
  203. nv_wo32(falcon, 0x104, 0x00000000); /* ENTRY */
  204. nv_wo32(falcon, 0x100, 0x00000002); /* TRIGGER */
  205. nv_wo32(falcon, 0x048, 0x00000003); /* FIFO | CHSW */
  206. return 0;
  207. }
  208. int
  209. _nouveau_falcon_fini(struct nouveau_object *object, bool suspend)
  210. {
  211. struct nouveau_falcon *falcon = (void *)object;
  212. if (!suspend) {
  213. nouveau_gpuobj_ref(NULL, &falcon->core);
  214. if (falcon->external) {
  215. vfree(falcon->data.data);
  216. vfree(falcon->code.data);
  217. falcon->code.data = NULL;
  218. }
  219. }
  220. nv_mo32(falcon, 0x048, 0x00000003, 0x00000000);
  221. nv_wo32(falcon, 0x014, 0xffffffff);
  222. return nouveau_engine_fini(&falcon->base, suspend);
  223. }
  224. int
  225. nouveau_falcon_create_(struct nouveau_object *parent,
  226. struct nouveau_object *engine,
  227. struct nouveau_oclass *oclass, u32 addr, bool enable,
  228. const char *iname, const char *fname,
  229. int length, void **pobject)
  230. {
  231. struct nouveau_falcon *falcon;
  232. int ret;
  233. ret = nouveau_engine_create_(parent, engine, oclass, enable, iname,
  234. fname, length, pobject);
  235. falcon = *pobject;
  236. if (ret)
  237. return ret;
  238. falcon->addr = addr;
  239. return 0;
  240. }