falcon.c 5.9 KB

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