falcon.c 10 KB

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