falcon.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015, NVIDIA Corporation.
  4. */
  5. #include <linux/platform_device.h>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/firmware.h>
  8. #include <linux/pci_ids.h>
  9. #include <linux/iopoll.h>
  10. #include "falcon.h"
  11. #include "drm.h"
  12. enum falcon_memory {
  13. FALCON_MEMORY_IMEM,
  14. FALCON_MEMORY_DATA,
  15. };
  16. static void falcon_writel(struct falcon *falcon, u32 value, u32 offset)
  17. {
  18. writel(value, falcon->regs + offset);
  19. }
  20. int falcon_wait_idle(struct falcon *falcon)
  21. {
  22. u32 value;
  23. return readl_poll_timeout(falcon->regs + FALCON_IDLESTATE, value,
  24. (value == 0), 10, 100000);
  25. }
  26. static int falcon_dma_wait_not_full(struct falcon *falcon)
  27. {
  28. u32 value;
  29. return readl_poll_timeout(falcon->regs + FALCON_DMATRFCMD, value,
  30. !(value & FALCON_DMATRFCMD_FULL), 10, 100000);
  31. }
  32. static int falcon_dma_wait_idle(struct falcon *falcon)
  33. {
  34. u32 value;
  35. return readl_poll_timeout(falcon->regs + FALCON_DMATRFCMD, value,
  36. (value & FALCON_DMATRFCMD_IDLE), 10, 100000);
  37. }
  38. static int falcon_copy_chunk(struct falcon *falcon,
  39. phys_addr_t base,
  40. unsigned long offset,
  41. enum falcon_memory target)
  42. {
  43. u32 cmd = FALCON_DMATRFCMD_SIZE_256B;
  44. int err;
  45. if (target == FALCON_MEMORY_IMEM)
  46. cmd |= FALCON_DMATRFCMD_IMEM;
  47. /*
  48. * Use second DMA context (i.e. the one for firmware). Strictly
  49. * speaking, at this point both DMA contexts point to the firmware
  50. * stream ID, but this register's value will be reused by the firmware
  51. * for later DMA transactions, so we need to use the correct value.
  52. */
  53. cmd |= FALCON_DMATRFCMD_DMACTX(1);
  54. err = falcon_dma_wait_not_full(falcon);
  55. if (err < 0)
  56. return err;
  57. falcon_writel(falcon, offset, FALCON_DMATRFMOFFS);
  58. falcon_writel(falcon, base, FALCON_DMATRFFBOFFS);
  59. falcon_writel(falcon, cmd, FALCON_DMATRFCMD);
  60. return 0;
  61. }
  62. static void falcon_copy_firmware_image(struct falcon *falcon,
  63. const struct firmware *firmware)
  64. {
  65. u32 *virt = falcon->firmware.virt;
  66. size_t i;
  67. /* copy the whole thing taking into account endianness */
  68. for (i = 0; i < firmware->size / sizeof(u32); i++)
  69. virt[i] = le32_to_cpu(((__le32 *)firmware->data)[i]);
  70. }
  71. static int falcon_parse_firmware_image(struct falcon *falcon)
  72. {
  73. struct falcon_fw_bin_header_v1 *bin = (void *)falcon->firmware.virt;
  74. struct falcon_fw_os_header_v1 *os;
  75. /* endian problems would show up right here */
  76. if (bin->magic != PCI_VENDOR_ID_NVIDIA && bin->magic != 0x10fe) {
  77. dev_err(falcon->dev, "incorrect firmware magic\n");
  78. return -EINVAL;
  79. }
  80. /* currently only version 1 is supported */
  81. if (bin->version != 1) {
  82. dev_err(falcon->dev, "unsupported firmware version\n");
  83. return -EINVAL;
  84. }
  85. /* check that the firmware size is consistent */
  86. if (bin->size > falcon->firmware.size) {
  87. dev_err(falcon->dev, "firmware image size inconsistency\n");
  88. return -EINVAL;
  89. }
  90. os = falcon->firmware.virt + bin->os_header_offset;
  91. falcon->firmware.bin_data.size = bin->os_size;
  92. falcon->firmware.bin_data.offset = bin->os_data_offset;
  93. falcon->firmware.code.offset = os->code_offset;
  94. falcon->firmware.code.size = os->code_size;
  95. falcon->firmware.data.offset = os->data_offset;
  96. falcon->firmware.data.size = os->data_size;
  97. return 0;
  98. }
  99. int falcon_read_firmware(struct falcon *falcon, const char *name)
  100. {
  101. int err;
  102. /* request_firmware prints error if it fails */
  103. err = request_firmware(&falcon->firmware.firmware, name, falcon->dev);
  104. if (err < 0)
  105. return err;
  106. falcon->firmware.size = falcon->firmware.firmware->size;
  107. return 0;
  108. }
  109. int falcon_load_firmware(struct falcon *falcon)
  110. {
  111. const struct firmware *firmware = falcon->firmware.firmware;
  112. int err;
  113. /* copy firmware image into local area. this also ensures endianness */
  114. falcon_copy_firmware_image(falcon, firmware);
  115. /* parse the image data */
  116. err = falcon_parse_firmware_image(falcon);
  117. if (err < 0) {
  118. dev_err(falcon->dev, "failed to parse firmware image\n");
  119. return err;
  120. }
  121. release_firmware(firmware);
  122. falcon->firmware.firmware = NULL;
  123. return 0;
  124. }
  125. int falcon_init(struct falcon *falcon)
  126. {
  127. falcon->firmware.virt = NULL;
  128. return 0;
  129. }
  130. void falcon_exit(struct falcon *falcon)
  131. {
  132. if (falcon->firmware.firmware)
  133. release_firmware(falcon->firmware.firmware);
  134. }
  135. int falcon_boot(struct falcon *falcon)
  136. {
  137. unsigned long offset;
  138. u32 value;
  139. int err;
  140. if (!falcon->firmware.virt)
  141. return -EINVAL;
  142. err = readl_poll_timeout(falcon->regs + FALCON_DMACTL, value,
  143. (value & (FALCON_DMACTL_IMEM_SCRUBBING |
  144. FALCON_DMACTL_DMEM_SCRUBBING)) == 0,
  145. 10, 10000);
  146. if (err < 0)
  147. return err;
  148. falcon_writel(falcon, 0, FALCON_DMACTL);
  149. /* setup the address of the binary data so Falcon can access it later */
  150. falcon_writel(falcon, (falcon->firmware.iova +
  151. falcon->firmware.bin_data.offset) >> 8,
  152. FALCON_DMATRFBASE);
  153. /* copy the data segment into Falcon internal memory */
  154. for (offset = 0; offset < falcon->firmware.data.size; offset += 256)
  155. falcon_copy_chunk(falcon,
  156. falcon->firmware.data.offset + offset,
  157. offset, FALCON_MEMORY_DATA);
  158. /* copy the code segment into Falcon internal memory */
  159. for (offset = 0; offset < falcon->firmware.code.size; offset += 256)
  160. falcon_copy_chunk(falcon, falcon->firmware.code.offset + offset,
  161. offset, FALCON_MEMORY_IMEM);
  162. /* wait for DMA to complete */
  163. err = falcon_dma_wait_idle(falcon);
  164. if (err < 0)
  165. return err;
  166. /* setup falcon interrupts */
  167. falcon_writel(falcon, FALCON_IRQMSET_EXT(0xff) |
  168. FALCON_IRQMSET_SWGEN1 |
  169. FALCON_IRQMSET_SWGEN0 |
  170. FALCON_IRQMSET_EXTERR |
  171. FALCON_IRQMSET_HALT |
  172. FALCON_IRQMSET_WDTMR,
  173. FALCON_IRQMSET);
  174. falcon_writel(falcon, FALCON_IRQDEST_EXT(0xff) |
  175. FALCON_IRQDEST_SWGEN1 |
  176. FALCON_IRQDEST_SWGEN0 |
  177. FALCON_IRQDEST_EXTERR |
  178. FALCON_IRQDEST_HALT,
  179. FALCON_IRQDEST);
  180. /* enable interface */
  181. falcon_writel(falcon, FALCON_ITFEN_MTHDEN |
  182. FALCON_ITFEN_CTXEN,
  183. FALCON_ITFEN);
  184. /* boot falcon */
  185. falcon_writel(falcon, 0x00000000, FALCON_BOOTVEC);
  186. falcon_writel(falcon, FALCON_CPUCTL_STARTCPU, FALCON_CPUCTL);
  187. err = falcon_wait_idle(falcon);
  188. if (err < 0) {
  189. dev_err(falcon->dev, "Falcon boot failed due to timeout\n");
  190. return err;
  191. }
  192. return 0;
  193. }
  194. void falcon_execute_method(struct falcon *falcon, u32 method, u32 data)
  195. {
  196. falcon_writel(falcon, method >> 2, FALCON_UCLASS_METHOD_OFFSET);
  197. falcon_writel(falcon, data, FALCON_UCLASS_METHOD_DATA);
  198. }