falcon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 <core/gpuobj.h>
  24. #include <subdev/timer.h>
  25. #include <engine/fifo.h>
  26. static int
  27. nvkm_falcon_oclass_get(struct nvkm_oclass *oclass, int index)
  28. {
  29. struct nvkm_falcon *falcon = nvkm_falcon(oclass->engine);
  30. int c = 0;
  31. while (falcon->func->sclass[c].oclass) {
  32. if (c++ == index) {
  33. oclass->base = falcon->func->sclass[index];
  34. return index;
  35. }
  36. }
  37. return c;
  38. }
  39. static int
  40. nvkm_falcon_cclass_bind(struct nvkm_object *object, struct nvkm_gpuobj *parent,
  41. int align, struct nvkm_gpuobj **pgpuobj)
  42. {
  43. return nvkm_gpuobj_new(object->engine->subdev.device, 256,
  44. align, true, parent, pgpuobj);
  45. }
  46. static const struct nvkm_object_func
  47. nvkm_falcon_cclass = {
  48. .bind = nvkm_falcon_cclass_bind,
  49. };
  50. static void
  51. nvkm_falcon_intr(struct nvkm_engine *engine)
  52. {
  53. struct nvkm_falcon *falcon = nvkm_falcon(engine);
  54. struct nvkm_subdev *subdev = &falcon->engine.subdev;
  55. struct nvkm_device *device = subdev->device;
  56. const u32 base = falcon->addr;
  57. u32 dest = nvkm_rd32(device, base + 0x01c);
  58. u32 intr = nvkm_rd32(device, base + 0x008) & dest & ~(dest >> 16);
  59. u32 inst = nvkm_rd32(device, base + 0x050) & 0x3fffffff;
  60. struct nvkm_fifo_chan *chan;
  61. unsigned long flags;
  62. chan = nvkm_fifo_chan_inst(device->fifo, (u64)inst << 12, &flags);
  63. if (intr & 0x00000040) {
  64. if (falcon->func->intr) {
  65. falcon->func->intr(falcon, chan);
  66. nvkm_wr32(device, base + 0x004, 0x00000040);
  67. intr &= ~0x00000040;
  68. }
  69. }
  70. if (intr & 0x00000010) {
  71. nvkm_debug(subdev, "ucode halted\n");
  72. nvkm_wr32(device, base + 0x004, 0x00000010);
  73. intr &= ~0x00000010;
  74. }
  75. if (intr) {
  76. nvkm_error(subdev, "intr %08x\n", intr);
  77. nvkm_wr32(device, base + 0x004, intr);
  78. }
  79. nvkm_fifo_chan_put(device->fifo, flags, &chan);
  80. }
  81. static int
  82. nvkm_falcon_fini(struct nvkm_engine *engine, bool suspend)
  83. {
  84. struct nvkm_falcon *falcon = nvkm_falcon(engine);
  85. struct nvkm_device *device = falcon->engine.subdev.device;
  86. const u32 base = falcon->addr;
  87. if (!suspend) {
  88. nvkm_memory_del(&falcon->core);
  89. if (falcon->external) {
  90. vfree(falcon->data.data);
  91. vfree(falcon->code.data);
  92. falcon->code.data = NULL;
  93. }
  94. }
  95. nvkm_mask(device, base + 0x048, 0x00000003, 0x00000000);
  96. nvkm_wr32(device, base + 0x014, 0xffffffff);
  97. return 0;
  98. }
  99. static void *
  100. vmemdup(const void *src, size_t len)
  101. {
  102. void *p = vmalloc(len);
  103. if (p)
  104. memcpy(p, src, len);
  105. return p;
  106. }
  107. static int
  108. nvkm_falcon_oneinit(struct nvkm_engine *engine)
  109. {
  110. struct nvkm_falcon *falcon = nvkm_falcon(engine);
  111. struct nvkm_subdev *subdev = &falcon->engine.subdev;
  112. struct nvkm_device *device = subdev->device;
  113. const u32 base = falcon->addr;
  114. u32 caps;
  115. /* determine falcon capabilities */
  116. if (device->chipset < 0xa3 ||
  117. device->chipset == 0xaa || device->chipset == 0xac) {
  118. falcon->version = 0;
  119. falcon->secret = (falcon->addr == 0x087000) ? 1 : 0;
  120. } else {
  121. caps = nvkm_rd32(device, base + 0x12c);
  122. falcon->version = (caps & 0x0000000f);
  123. falcon->secret = (caps & 0x00000030) >> 4;
  124. }
  125. caps = nvkm_rd32(device, base + 0x108);
  126. falcon->code.limit = (caps & 0x000001ff) << 8;
  127. falcon->data.limit = (caps & 0x0003fe00) >> 1;
  128. nvkm_debug(subdev, "falcon version: %d\n", falcon->version);
  129. nvkm_debug(subdev, "secret level: %d\n", falcon->secret);
  130. nvkm_debug(subdev, "code limit: %d\n", falcon->code.limit);
  131. nvkm_debug(subdev, "data limit: %d\n", falcon->data.limit);
  132. return 0;
  133. }
  134. static int
  135. nvkm_falcon_init(struct nvkm_engine *engine)
  136. {
  137. struct nvkm_falcon *falcon = nvkm_falcon(engine);
  138. struct nvkm_subdev *subdev = &falcon->engine.subdev;
  139. struct nvkm_device *device = subdev->device;
  140. const struct firmware *fw;
  141. char name[32] = "internal";
  142. const u32 base = falcon->addr;
  143. int ret, i;
  144. /* wait for 'uc halted' to be signalled before continuing */
  145. if (falcon->secret && falcon->version < 4) {
  146. if (!falcon->version) {
  147. nvkm_msec(device, 2000,
  148. if (nvkm_rd32(device, base + 0x008) & 0x00000010)
  149. break;
  150. );
  151. } else {
  152. nvkm_msec(device, 2000,
  153. if (!(nvkm_rd32(device, base + 0x180) & 0x80000000))
  154. break;
  155. );
  156. }
  157. nvkm_wr32(device, base + 0x004, 0x00000010);
  158. }
  159. /* disable all interrupts */
  160. nvkm_wr32(device, base + 0x014, 0xffffffff);
  161. /* no default ucode provided by the engine implementation, try and
  162. * locate a "self-bootstrapping" firmware image for the engine
  163. */
  164. if (!falcon->code.data) {
  165. snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03x",
  166. device->chipset, falcon->addr >> 12);
  167. ret = request_firmware(&fw, name, device->dev);
  168. if (ret == 0) {
  169. falcon->code.data = vmemdup(fw->data, fw->size);
  170. falcon->code.size = fw->size;
  171. falcon->data.data = NULL;
  172. falcon->data.size = 0;
  173. release_firmware(fw);
  174. }
  175. falcon->external = true;
  176. }
  177. /* next step is to try and load "static code/data segment" firmware
  178. * images for the engine
  179. */
  180. if (!falcon->code.data) {
  181. snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03xd",
  182. device->chipset, falcon->addr >> 12);
  183. ret = request_firmware(&fw, name, device->dev);
  184. if (ret) {
  185. nvkm_error(subdev, "unable to load firmware data\n");
  186. return -ENODEV;
  187. }
  188. falcon->data.data = vmemdup(fw->data, fw->size);
  189. falcon->data.size = fw->size;
  190. release_firmware(fw);
  191. if (!falcon->data.data)
  192. return -ENOMEM;
  193. snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03xc",
  194. device->chipset, falcon->addr >> 12);
  195. ret = request_firmware(&fw, name, device->dev);
  196. if (ret) {
  197. nvkm_error(subdev, "unable to load firmware code\n");
  198. return -ENODEV;
  199. }
  200. falcon->code.data = vmemdup(fw->data, fw->size);
  201. falcon->code.size = fw->size;
  202. release_firmware(fw);
  203. if (!falcon->code.data)
  204. return -ENOMEM;
  205. }
  206. nvkm_debug(subdev, "firmware: %s (%s)\n", name, falcon->data.data ?
  207. "static code/data segments" : "self-bootstrapping");
  208. /* ensure any "self-bootstrapping" firmware image is in vram */
  209. if (!falcon->data.data && !falcon->core) {
  210. ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST,
  211. falcon->code.size, 256, false,
  212. &falcon->core);
  213. if (ret) {
  214. nvkm_error(subdev, "core allocation failed, %d\n", ret);
  215. return ret;
  216. }
  217. nvkm_kmap(falcon->core);
  218. for (i = 0; i < falcon->code.size; i += 4)
  219. nvkm_wo32(falcon->core, i, falcon->code.data[i / 4]);
  220. nvkm_done(falcon->core);
  221. }
  222. /* upload firmware bootloader (or the full code segments) */
  223. if (falcon->core) {
  224. u64 addr = nvkm_memory_addr(falcon->core);
  225. if (device->card_type < NV_C0)
  226. nvkm_wr32(device, base + 0x618, 0x04000000);
  227. else
  228. nvkm_wr32(device, base + 0x618, 0x00000114);
  229. nvkm_wr32(device, base + 0x11c, 0);
  230. nvkm_wr32(device, base + 0x110, addr >> 8);
  231. nvkm_wr32(device, base + 0x114, 0);
  232. nvkm_wr32(device, base + 0x118, 0x00006610);
  233. } else {
  234. if (falcon->code.size > falcon->code.limit ||
  235. falcon->data.size > falcon->data.limit) {
  236. nvkm_error(subdev, "ucode exceeds falcon limit(s)\n");
  237. return -EINVAL;
  238. }
  239. if (falcon->version < 3) {
  240. nvkm_wr32(device, base + 0xff8, 0x00100000);
  241. for (i = 0; i < falcon->code.size / 4; i++)
  242. nvkm_wr32(device, base + 0xff4, falcon->code.data[i]);
  243. } else {
  244. nvkm_wr32(device, base + 0x180, 0x01000000);
  245. for (i = 0; i < falcon->code.size / 4; i++) {
  246. if ((i & 0x3f) == 0)
  247. nvkm_wr32(device, base + 0x188, i >> 6);
  248. nvkm_wr32(device, base + 0x184, falcon->code.data[i]);
  249. }
  250. }
  251. }
  252. /* upload data segment (if necessary), zeroing the remainder */
  253. if (falcon->version < 3) {
  254. nvkm_wr32(device, base + 0xff8, 0x00000000);
  255. for (i = 0; !falcon->core && i < falcon->data.size / 4; i++)
  256. nvkm_wr32(device, base + 0xff4, falcon->data.data[i]);
  257. for (; i < falcon->data.limit; i += 4)
  258. nvkm_wr32(device, base + 0xff4, 0x00000000);
  259. } else {
  260. nvkm_wr32(device, base + 0x1c0, 0x01000000);
  261. for (i = 0; !falcon->core && i < falcon->data.size / 4; i++)
  262. nvkm_wr32(device, base + 0x1c4, falcon->data.data[i]);
  263. for (; i < falcon->data.limit / 4; i++)
  264. nvkm_wr32(device, base + 0x1c4, 0x00000000);
  265. }
  266. /* start it running */
  267. nvkm_wr32(device, base + 0x10c, 0x00000001); /* BLOCK_ON_FIFO */
  268. nvkm_wr32(device, base + 0x104, 0x00000000); /* ENTRY */
  269. nvkm_wr32(device, base + 0x100, 0x00000002); /* TRIGGER */
  270. nvkm_wr32(device, base + 0x048, 0x00000003); /* FIFO | CHSW */
  271. if (falcon->func->init)
  272. falcon->func->init(falcon);
  273. return 0;
  274. }
  275. static void *
  276. nvkm_falcon_dtor(struct nvkm_engine *engine)
  277. {
  278. return nvkm_falcon(engine);
  279. }
  280. static const struct nvkm_engine_func
  281. nvkm_falcon = {
  282. .dtor = nvkm_falcon_dtor,
  283. .oneinit = nvkm_falcon_oneinit,
  284. .init = nvkm_falcon_init,
  285. .fini = nvkm_falcon_fini,
  286. .intr = nvkm_falcon_intr,
  287. .fifo.sclass = nvkm_falcon_oclass_get,
  288. .cclass = &nvkm_falcon_cclass,
  289. };
  290. int
  291. nvkm_falcon_new_(const struct nvkm_falcon_func *func,
  292. struct nvkm_device *device, int index, bool enable,
  293. u32 addr, struct nvkm_engine **pengine)
  294. {
  295. struct nvkm_falcon *falcon;
  296. if (!(falcon = kzalloc(sizeof(*falcon), GFP_KERNEL)))
  297. return -ENOMEM;
  298. falcon->func = func;
  299. falcon->addr = addr;
  300. falcon->code.data = func->code.data;
  301. falcon->code.size = func->code.size;
  302. falcon->data.data = func->data.data;
  303. falcon->data.size = func->data.size;
  304. *pengine = &falcon->engine;
  305. return nvkm_engine_ctor(&nvkm_falcon, device, index,
  306. enable, &falcon->engine);
  307. }