panthor_fw.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. // SPDX-License-Identifier: GPL-2.0 or MIT
  2. /* Copyright 2023 Collabora ltd. */
  3. #ifdef CONFIG_ARM_ARCH_TIMER
  4. #include <asm/arch_timer.h>
  5. #endif
  6. #include <linux/clk.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/firmware.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/iosys-map.h>
  11. #include <linux/mutex.h>
  12. #include <linux/platform_device.h>
  13. #include <drm/drm_drv.h>
  14. #include <drm/drm_managed.h>
  15. #include "panthor_device.h"
  16. #include "panthor_fw.h"
  17. #include "panthor_gem.h"
  18. #include "panthor_gpu.h"
  19. #include "panthor_mmu.h"
  20. #include "panthor_regs.h"
  21. #include "panthor_sched.h"
  22. #define CSF_FW_NAME "mali_csffw.bin"
  23. #define PING_INTERVAL_MS 12000
  24. #define PROGRESS_TIMEOUT_CYCLES (5ull * 500 * 1024 * 1024)
  25. #define PROGRESS_TIMEOUT_SCALE_SHIFT 10
  26. #define IDLE_HYSTERESIS_US 800
  27. #define PWROFF_HYSTERESIS_US 10000
  28. /**
  29. * struct panthor_fw_binary_hdr - Firmware binary header.
  30. */
  31. struct panthor_fw_binary_hdr {
  32. /** @magic: Magic value to check binary validity. */
  33. u32 magic;
  34. #define CSF_FW_BINARY_HEADER_MAGIC 0xc3f13a6e
  35. /** @minor: Minor FW version. */
  36. u8 minor;
  37. /** @major: Major FW version. */
  38. u8 major;
  39. #define CSF_FW_BINARY_HEADER_MAJOR_MAX 0
  40. /** @padding1: MBZ. */
  41. u16 padding1;
  42. /** @version_hash: FW version hash. */
  43. u32 version_hash;
  44. /** @padding2: MBZ. */
  45. u32 padding2;
  46. /** @size: FW binary size. */
  47. u32 size;
  48. };
  49. /**
  50. * enum panthor_fw_binary_entry_type - Firmware binary entry type
  51. */
  52. enum panthor_fw_binary_entry_type {
  53. /** @CSF_FW_BINARY_ENTRY_TYPE_IFACE: Host <-> FW interface. */
  54. CSF_FW_BINARY_ENTRY_TYPE_IFACE = 0,
  55. /** @CSF_FW_BINARY_ENTRY_TYPE_CONFIG: FW config. */
  56. CSF_FW_BINARY_ENTRY_TYPE_CONFIG = 1,
  57. /** @CSF_FW_BINARY_ENTRY_TYPE_FUTF_TEST: Unit-tests. */
  58. CSF_FW_BINARY_ENTRY_TYPE_FUTF_TEST = 2,
  59. /** @CSF_FW_BINARY_ENTRY_TYPE_TRACE_BUFFER: Trace buffer interface. */
  60. CSF_FW_BINARY_ENTRY_TYPE_TRACE_BUFFER = 3,
  61. /** @CSF_FW_BINARY_ENTRY_TYPE_TIMELINE_METADATA: Timeline metadata interface. */
  62. CSF_FW_BINARY_ENTRY_TYPE_TIMELINE_METADATA = 4,
  63. };
  64. #define CSF_FW_BINARY_ENTRY_TYPE(ehdr) ((ehdr) & 0xff)
  65. #define CSF_FW_BINARY_ENTRY_SIZE(ehdr) (((ehdr) >> 8) & 0xff)
  66. #define CSF_FW_BINARY_ENTRY_UPDATE BIT(30)
  67. #define CSF_FW_BINARY_ENTRY_OPTIONAL BIT(31)
  68. #define CSF_FW_BINARY_IFACE_ENTRY_RD_RD BIT(0)
  69. #define CSF_FW_BINARY_IFACE_ENTRY_RD_WR BIT(1)
  70. #define CSF_FW_BINARY_IFACE_ENTRY_RD_EX BIT(2)
  71. #define CSF_FW_BINARY_IFACE_ENTRY_RD_CACHE_MODE_NONE (0 << 3)
  72. #define CSF_FW_BINARY_IFACE_ENTRY_RD_CACHE_MODE_CACHED (1 << 3)
  73. #define CSF_FW_BINARY_IFACE_ENTRY_RD_CACHE_MODE_UNCACHED_COHERENT (2 << 3)
  74. #define CSF_FW_BINARY_IFACE_ENTRY_RD_CACHE_MODE_CACHED_COHERENT (3 << 3)
  75. #define CSF_FW_BINARY_IFACE_ENTRY_RD_CACHE_MODE_MASK GENMASK(4, 3)
  76. #define CSF_FW_BINARY_IFACE_ENTRY_RD_PROT BIT(5)
  77. #define CSF_FW_BINARY_IFACE_ENTRY_RD_SHARED BIT(30)
  78. #define CSF_FW_BINARY_IFACE_ENTRY_RD_ZERO BIT(31)
  79. #define CSF_FW_BINARY_IFACE_ENTRY_RD_SUPPORTED_FLAGS \
  80. (CSF_FW_BINARY_IFACE_ENTRY_RD_RD | \
  81. CSF_FW_BINARY_IFACE_ENTRY_RD_WR | \
  82. CSF_FW_BINARY_IFACE_ENTRY_RD_EX | \
  83. CSF_FW_BINARY_IFACE_ENTRY_RD_CACHE_MODE_MASK | \
  84. CSF_FW_BINARY_IFACE_ENTRY_RD_PROT | \
  85. CSF_FW_BINARY_IFACE_ENTRY_RD_SHARED | \
  86. CSF_FW_BINARY_IFACE_ENTRY_RD_ZERO)
  87. /**
  88. * struct panthor_fw_binary_section_entry_hdr - Describes a section of FW binary
  89. */
  90. struct panthor_fw_binary_section_entry_hdr {
  91. /** @flags: Section flags. */
  92. u32 flags;
  93. /** @va: MCU virtual range to map this binary section to. */
  94. struct {
  95. /** @start: Start address. */
  96. u32 start;
  97. /** @end: End address. */
  98. u32 end;
  99. } va;
  100. /** @data: Data to initialize the FW section with. */
  101. struct {
  102. /** @start: Start offset in the FW binary. */
  103. u32 start;
  104. /** @end: End offset in the FW binary. */
  105. u32 end;
  106. } data;
  107. };
  108. /**
  109. * struct panthor_fw_binary_iter - Firmware binary iterator
  110. *
  111. * Used to parse a firmware binary.
  112. */
  113. struct panthor_fw_binary_iter {
  114. /** @data: FW binary data. */
  115. const void *data;
  116. /** @size: FW binary size. */
  117. size_t size;
  118. /** @offset: Iterator offset. */
  119. size_t offset;
  120. };
  121. /**
  122. * struct panthor_fw_section - FW section
  123. */
  124. struct panthor_fw_section {
  125. /** @node: Used to keep track of FW sections. */
  126. struct list_head node;
  127. /** @flags: Section flags, as encoded in the FW binary. */
  128. u32 flags;
  129. /** @mem: Section memory. */
  130. struct panthor_kernel_bo *mem;
  131. /**
  132. * @name: Name of the section, as specified in the binary.
  133. *
  134. * Can be NULL.
  135. */
  136. const char *name;
  137. /**
  138. * @data: Initial data copied to the FW memory.
  139. *
  140. * We keep data around so we can reload sections after a reset.
  141. */
  142. struct {
  143. /** @buf: Buffed used to store init data. */
  144. const void *buf;
  145. /** @size: Size of @buf in bytes. */
  146. size_t size;
  147. } data;
  148. };
  149. #define CSF_MCU_SHARED_REGION_START 0x04000000ULL
  150. #define CSF_MCU_SHARED_REGION_SIZE 0x04000000ULL
  151. #define MIN_CS_PER_CSG 8
  152. #define MIN_CSGS 3
  153. #define MAX_CSG_PRIO 0xf
  154. #define CSF_IFACE_VERSION(major, minor, patch) \
  155. (((major) << 24) | ((minor) << 16) | (patch))
  156. #define CSF_IFACE_VERSION_MAJOR(v) ((v) >> 24)
  157. #define CSF_IFACE_VERSION_MINOR(v) (((v) >> 16) & 0xff)
  158. #define CSF_IFACE_VERSION_PATCH(v) ((v) & 0xffff)
  159. #define CSF_GROUP_CONTROL_OFFSET 0x1000
  160. #define CSF_STREAM_CONTROL_OFFSET 0x40
  161. #define CSF_UNPRESERVED_REG_COUNT 4
  162. /**
  163. * struct panthor_fw_iface - FW interfaces
  164. */
  165. struct panthor_fw_iface {
  166. /** @global: Global interface. */
  167. struct panthor_fw_global_iface global;
  168. /** @groups: Group slot interfaces. */
  169. struct panthor_fw_csg_iface groups[MAX_CSGS];
  170. /** @streams: Command stream slot interfaces. */
  171. struct panthor_fw_cs_iface streams[MAX_CSGS][MAX_CS_PER_CSG];
  172. };
  173. /**
  174. * struct panthor_fw - Firmware management
  175. */
  176. struct panthor_fw {
  177. /** @vm: MCU VM. */
  178. struct panthor_vm *vm;
  179. /** @sections: List of FW sections. */
  180. struct list_head sections;
  181. /** @shared_section: The section containing the FW interfaces. */
  182. struct panthor_fw_section *shared_section;
  183. /** @iface: FW interfaces. */
  184. struct panthor_fw_iface iface;
  185. /** @watchdog: Collection of fields relating to the FW watchdog. */
  186. struct {
  187. /** @ping_work: Delayed work used to ping the FW. */
  188. struct delayed_work ping_work;
  189. } watchdog;
  190. /**
  191. * @req_waitqueue: FW request waitqueue.
  192. *
  193. * Everytime a request is sent to a command stream group or the global
  194. * interface, the caller will first busy wait for the request to be
  195. * acknowledged, and then fallback to a sleeping wait.
  196. *
  197. * This wait queue is here to support the sleeping wait flavor.
  198. */
  199. wait_queue_head_t req_waitqueue;
  200. /** @booted: True is the FW is booted */
  201. bool booted;
  202. /**
  203. * @fast_reset: True if the post_reset logic can proceed with a fast reset.
  204. *
  205. * A fast reset is just a reset where the driver doesn't reload the FW sections.
  206. *
  207. * Any time the firmware is properly suspended, a fast reset can take place.
  208. * On the other hand, if the halt operation failed, the driver will reload
  209. * all sections to make sure we start from a fresh state.
  210. */
  211. bool fast_reset;
  212. /** @irq: Job irq data. */
  213. struct panthor_irq irq;
  214. };
  215. struct panthor_vm *panthor_fw_vm(struct panthor_device *ptdev)
  216. {
  217. return ptdev->fw->vm;
  218. }
  219. /**
  220. * panthor_fw_get_glb_iface() - Get the global interface
  221. * @ptdev: Device.
  222. *
  223. * Return: The global interface.
  224. */
  225. struct panthor_fw_global_iface *
  226. panthor_fw_get_glb_iface(struct panthor_device *ptdev)
  227. {
  228. return &ptdev->fw->iface.global;
  229. }
  230. /**
  231. * panthor_fw_get_csg_iface() - Get a command stream group slot interface
  232. * @ptdev: Device.
  233. * @csg_slot: Index of the command stream group slot.
  234. *
  235. * Return: The command stream group slot interface.
  236. */
  237. struct panthor_fw_csg_iface *
  238. panthor_fw_get_csg_iface(struct panthor_device *ptdev, u32 csg_slot)
  239. {
  240. if (drm_WARN_ON(&ptdev->base, csg_slot >= MAX_CSGS))
  241. return NULL;
  242. return &ptdev->fw->iface.groups[csg_slot];
  243. }
  244. /**
  245. * panthor_fw_get_cs_iface() - Get a command stream slot interface
  246. * @ptdev: Device.
  247. * @csg_slot: Index of the command stream group slot.
  248. * @cs_slot: Index of the command stream slot.
  249. *
  250. * Return: The command stream slot interface.
  251. */
  252. struct panthor_fw_cs_iface *
  253. panthor_fw_get_cs_iface(struct panthor_device *ptdev, u32 csg_slot, u32 cs_slot)
  254. {
  255. if (drm_WARN_ON(&ptdev->base, csg_slot >= MAX_CSGS || cs_slot >= MAX_CS_PER_CSG))
  256. return NULL;
  257. return &ptdev->fw->iface.streams[csg_slot][cs_slot];
  258. }
  259. /**
  260. * panthor_fw_conv_timeout() - Convert a timeout into a cycle-count
  261. * @ptdev: Device.
  262. * @timeout_us: Timeout expressed in micro-seconds.
  263. *
  264. * The FW has two timer sources: the GPU counter or arch-timer. We need
  265. * to express timeouts in term of number of cycles and specify which
  266. * timer source should be used.
  267. *
  268. * Return: A value suitable for timeout fields in the global interface.
  269. */
  270. static u32 panthor_fw_conv_timeout(struct panthor_device *ptdev, u32 timeout_us)
  271. {
  272. bool use_cycle_counter = false;
  273. u32 timer_rate = 0;
  274. u64 mod_cycles;
  275. #ifdef CONFIG_ARM_ARCH_TIMER
  276. timer_rate = arch_timer_get_cntfrq();
  277. #endif
  278. if (!timer_rate) {
  279. use_cycle_counter = true;
  280. timer_rate = clk_get_rate(ptdev->clks.core);
  281. }
  282. if (drm_WARN_ON(&ptdev->base, !timer_rate)) {
  283. /* We couldn't get a valid clock rate, let's just pick the
  284. * maximum value so the FW still handles the core
  285. * power on/off requests.
  286. */
  287. return GLB_TIMER_VAL(~0) |
  288. GLB_TIMER_SOURCE_GPU_COUNTER;
  289. }
  290. mod_cycles = DIV_ROUND_UP_ULL((u64)timeout_us * timer_rate,
  291. 1000000ull << 10);
  292. if (drm_WARN_ON(&ptdev->base, mod_cycles > GLB_TIMER_VAL(~0)))
  293. mod_cycles = GLB_TIMER_VAL(~0);
  294. return GLB_TIMER_VAL(mod_cycles) |
  295. (use_cycle_counter ? GLB_TIMER_SOURCE_GPU_COUNTER : 0);
  296. }
  297. static int panthor_fw_binary_iter_read(struct panthor_device *ptdev,
  298. struct panthor_fw_binary_iter *iter,
  299. void *out, size_t size)
  300. {
  301. size_t new_offset = iter->offset + size;
  302. if (new_offset > iter->size || new_offset < iter->offset) {
  303. drm_err(&ptdev->base, "Firmware too small\n");
  304. return -EINVAL;
  305. }
  306. memcpy(out, iter->data + iter->offset, size);
  307. iter->offset = new_offset;
  308. return 0;
  309. }
  310. static int panthor_fw_binary_sub_iter_init(struct panthor_device *ptdev,
  311. struct panthor_fw_binary_iter *iter,
  312. struct panthor_fw_binary_iter *sub_iter,
  313. size_t size)
  314. {
  315. size_t new_offset = iter->offset + size;
  316. if (new_offset > iter->size || new_offset < iter->offset) {
  317. drm_err(&ptdev->base, "Firmware entry too long\n");
  318. return -EINVAL;
  319. }
  320. sub_iter->offset = 0;
  321. sub_iter->data = iter->data + iter->offset;
  322. sub_iter->size = size;
  323. iter->offset = new_offset;
  324. return 0;
  325. }
  326. static void panthor_fw_init_section_mem(struct panthor_device *ptdev,
  327. struct panthor_fw_section *section)
  328. {
  329. bool was_mapped = !!section->mem->kmap;
  330. int ret;
  331. if (!section->data.size &&
  332. !(section->flags & CSF_FW_BINARY_IFACE_ENTRY_RD_ZERO))
  333. return;
  334. ret = panthor_kernel_bo_vmap(section->mem);
  335. if (drm_WARN_ON(&ptdev->base, ret))
  336. return;
  337. memcpy(section->mem->kmap, section->data.buf, section->data.size);
  338. if (section->flags & CSF_FW_BINARY_IFACE_ENTRY_RD_ZERO) {
  339. memset(section->mem->kmap + section->data.size, 0,
  340. panthor_kernel_bo_size(section->mem) - section->data.size);
  341. }
  342. if (!was_mapped)
  343. panthor_kernel_bo_vunmap(section->mem);
  344. }
  345. /**
  346. * panthor_fw_alloc_queue_iface_mem() - Allocate a ring-buffer interfaces.
  347. * @ptdev: Device.
  348. * @input: Pointer holding the input interface on success.
  349. * Should be ignored on failure.
  350. * @output: Pointer holding the output interface on success.
  351. * Should be ignored on failure.
  352. * @input_fw_va: Pointer holding the input interface FW VA on success.
  353. * Should be ignored on failure.
  354. * @output_fw_va: Pointer holding the output interface FW VA on success.
  355. * Should be ignored on failure.
  356. *
  357. * Allocates panthor_fw_ringbuf_{input,out}_iface interfaces. The input
  358. * interface is at offset 0, and the output interface at offset 4096.
  359. *
  360. * Return: A valid pointer in case of success, an ERR_PTR() otherwise.
  361. */
  362. struct panthor_kernel_bo *
  363. panthor_fw_alloc_queue_iface_mem(struct panthor_device *ptdev,
  364. struct panthor_fw_ringbuf_input_iface **input,
  365. const struct panthor_fw_ringbuf_output_iface **output,
  366. u32 *input_fw_va, u32 *output_fw_va)
  367. {
  368. struct panthor_kernel_bo *mem;
  369. int ret;
  370. mem = panthor_kernel_bo_create(ptdev, ptdev->fw->vm, SZ_8K,
  371. DRM_PANTHOR_BO_NO_MMAP,
  372. DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC |
  373. DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED,
  374. PANTHOR_VM_KERNEL_AUTO_VA);
  375. if (IS_ERR(mem))
  376. return mem;
  377. ret = panthor_kernel_bo_vmap(mem);
  378. if (ret) {
  379. panthor_kernel_bo_destroy(mem);
  380. return ERR_PTR(ret);
  381. }
  382. memset(mem->kmap, 0, panthor_kernel_bo_size(mem));
  383. *input = mem->kmap;
  384. *output = mem->kmap + SZ_4K;
  385. *input_fw_va = panthor_kernel_bo_gpuva(mem);
  386. *output_fw_va = *input_fw_va + SZ_4K;
  387. return mem;
  388. }
  389. /**
  390. * panthor_fw_alloc_suspend_buf_mem() - Allocate a suspend buffer for a command stream group.
  391. * @ptdev: Device.
  392. * @size: Size of the suspend buffer.
  393. *
  394. * Return: A valid pointer in case of success, an ERR_PTR() otherwise.
  395. */
  396. struct panthor_kernel_bo *
  397. panthor_fw_alloc_suspend_buf_mem(struct panthor_device *ptdev, size_t size)
  398. {
  399. return panthor_kernel_bo_create(ptdev, panthor_fw_vm(ptdev), size,
  400. DRM_PANTHOR_BO_NO_MMAP,
  401. DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC,
  402. PANTHOR_VM_KERNEL_AUTO_VA);
  403. }
  404. static int panthor_fw_load_section_entry(struct panthor_device *ptdev,
  405. const struct firmware *fw,
  406. struct panthor_fw_binary_iter *iter,
  407. u32 ehdr)
  408. {
  409. ssize_t vm_pgsz = panthor_vm_page_size(ptdev->fw->vm);
  410. struct panthor_fw_binary_section_entry_hdr hdr;
  411. struct panthor_fw_section *section;
  412. u32 section_size;
  413. u32 name_len;
  414. int ret;
  415. ret = panthor_fw_binary_iter_read(ptdev, iter, &hdr, sizeof(hdr));
  416. if (ret)
  417. return ret;
  418. if (hdr.data.end < hdr.data.start) {
  419. drm_err(&ptdev->base, "Firmware corrupted, data.end < data.start (0x%x < 0x%x)\n",
  420. hdr.data.end, hdr.data.start);
  421. return -EINVAL;
  422. }
  423. if (hdr.va.end < hdr.va.start) {
  424. drm_err(&ptdev->base, "Firmware corrupted, hdr.va.end < hdr.va.start (0x%x < 0x%x)\n",
  425. hdr.va.end, hdr.va.start);
  426. return -EINVAL;
  427. }
  428. if (hdr.data.end > fw->size) {
  429. drm_err(&ptdev->base, "Firmware corrupted, file truncated? data_end=0x%x > fw size=0x%zx\n",
  430. hdr.data.end, fw->size);
  431. return -EINVAL;
  432. }
  433. if (!IS_ALIGNED(hdr.va.start, vm_pgsz) || !IS_ALIGNED(hdr.va.end, vm_pgsz)) {
  434. drm_err(&ptdev->base, "Firmware corrupted, virtual addresses not page aligned: 0x%x-0x%x\n",
  435. hdr.va.start, hdr.va.end);
  436. return -EINVAL;
  437. }
  438. if (hdr.flags & ~CSF_FW_BINARY_IFACE_ENTRY_RD_SUPPORTED_FLAGS) {
  439. drm_err(&ptdev->base, "Firmware contains interface with unsupported flags (0x%x)\n",
  440. hdr.flags);
  441. return -EINVAL;
  442. }
  443. if (hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_RD_PROT) {
  444. drm_warn(&ptdev->base,
  445. "Firmware protected mode entry not be supported, ignoring");
  446. return 0;
  447. }
  448. if (hdr.va.start == CSF_MCU_SHARED_REGION_START &&
  449. !(hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_RD_SHARED)) {
  450. drm_err(&ptdev->base,
  451. "Interface at 0x%llx must be shared", CSF_MCU_SHARED_REGION_START);
  452. return -EINVAL;
  453. }
  454. name_len = iter->size - iter->offset;
  455. section = drmm_kzalloc(&ptdev->base, sizeof(*section), GFP_KERNEL);
  456. if (!section)
  457. return -ENOMEM;
  458. list_add_tail(&section->node, &ptdev->fw->sections);
  459. section->flags = hdr.flags;
  460. section->data.size = hdr.data.end - hdr.data.start;
  461. if (section->data.size > 0) {
  462. void *data = drmm_kmalloc(&ptdev->base, section->data.size, GFP_KERNEL);
  463. if (!data)
  464. return -ENOMEM;
  465. memcpy(data, fw->data + hdr.data.start, section->data.size);
  466. section->data.buf = data;
  467. }
  468. if (name_len > 0) {
  469. char *name = drmm_kmalloc(&ptdev->base, name_len + 1, GFP_KERNEL);
  470. if (!name)
  471. return -ENOMEM;
  472. memcpy(name, iter->data + iter->offset, name_len);
  473. name[name_len] = '\0';
  474. section->name = name;
  475. }
  476. section_size = hdr.va.end - hdr.va.start;
  477. if (section_size) {
  478. u32 cache_mode = hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_RD_CACHE_MODE_MASK;
  479. struct panthor_gem_object *bo;
  480. u32 vm_map_flags = 0;
  481. struct sg_table *sgt;
  482. u64 va = hdr.va.start;
  483. if (!(hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_RD_WR))
  484. vm_map_flags |= DRM_PANTHOR_VM_BIND_OP_MAP_READONLY;
  485. if (!(hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_RD_EX))
  486. vm_map_flags |= DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC;
  487. /* TODO: CSF_FW_BINARY_IFACE_ENTRY_RD_CACHE_MODE_*_COHERENT are mapped to
  488. * non-cacheable for now. We might want to introduce a new
  489. * IOMMU_xxx flag (or abuse IOMMU_MMIO, which maps to device
  490. * memory and is currently not used by our driver) for
  491. * AS_MEMATTR_AARCH64_SHARED memory, so we can take benefit
  492. * of IO-coherent systems.
  493. */
  494. if (cache_mode != CSF_FW_BINARY_IFACE_ENTRY_RD_CACHE_MODE_CACHED)
  495. vm_map_flags |= DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED;
  496. section->mem = panthor_kernel_bo_create(ptdev, panthor_fw_vm(ptdev),
  497. section_size,
  498. DRM_PANTHOR_BO_NO_MMAP,
  499. vm_map_flags, va);
  500. if (IS_ERR(section->mem))
  501. return PTR_ERR(section->mem);
  502. if (drm_WARN_ON(&ptdev->base, section->mem->va_node.start != hdr.va.start))
  503. return -EINVAL;
  504. if (section->flags & CSF_FW_BINARY_IFACE_ENTRY_RD_SHARED) {
  505. ret = panthor_kernel_bo_vmap(section->mem);
  506. if (ret)
  507. return ret;
  508. }
  509. panthor_fw_init_section_mem(ptdev, section);
  510. bo = to_panthor_bo(section->mem->obj);
  511. sgt = drm_gem_shmem_get_pages_sgt(&bo->base);
  512. if (IS_ERR(sgt))
  513. return PTR_ERR(sgt);
  514. dma_sync_sgtable_for_device(ptdev->base.dev, sgt, DMA_TO_DEVICE);
  515. }
  516. if (hdr.va.start == CSF_MCU_SHARED_REGION_START)
  517. ptdev->fw->shared_section = section;
  518. return 0;
  519. }
  520. static void
  521. panthor_reload_fw_sections(struct panthor_device *ptdev, bool full_reload)
  522. {
  523. struct panthor_fw_section *section;
  524. list_for_each_entry(section, &ptdev->fw->sections, node) {
  525. struct sg_table *sgt;
  526. if (!full_reload && !(section->flags & CSF_FW_BINARY_IFACE_ENTRY_RD_WR))
  527. continue;
  528. panthor_fw_init_section_mem(ptdev, section);
  529. sgt = drm_gem_shmem_get_pages_sgt(&to_panthor_bo(section->mem->obj)->base);
  530. if (!drm_WARN_ON(&ptdev->base, IS_ERR_OR_NULL(sgt)))
  531. dma_sync_sgtable_for_device(ptdev->base.dev, sgt, DMA_TO_DEVICE);
  532. }
  533. }
  534. static int panthor_fw_load_entry(struct panthor_device *ptdev,
  535. const struct firmware *fw,
  536. struct panthor_fw_binary_iter *iter)
  537. {
  538. struct panthor_fw_binary_iter eiter;
  539. u32 ehdr;
  540. int ret;
  541. ret = panthor_fw_binary_iter_read(ptdev, iter, &ehdr, sizeof(ehdr));
  542. if (ret)
  543. return ret;
  544. if ((iter->offset % sizeof(u32)) ||
  545. (CSF_FW_BINARY_ENTRY_SIZE(ehdr) % sizeof(u32))) {
  546. drm_err(&ptdev->base, "Firmware entry isn't 32 bit aligned, offset=0x%x size=0x%x\n",
  547. (u32)(iter->offset - sizeof(u32)), CSF_FW_BINARY_ENTRY_SIZE(ehdr));
  548. return -EINVAL;
  549. }
  550. if (panthor_fw_binary_sub_iter_init(ptdev, iter, &eiter,
  551. CSF_FW_BINARY_ENTRY_SIZE(ehdr) - sizeof(ehdr)))
  552. return -EINVAL;
  553. switch (CSF_FW_BINARY_ENTRY_TYPE(ehdr)) {
  554. case CSF_FW_BINARY_ENTRY_TYPE_IFACE:
  555. return panthor_fw_load_section_entry(ptdev, fw, &eiter, ehdr);
  556. /* FIXME: handle those entry types? */
  557. case CSF_FW_BINARY_ENTRY_TYPE_CONFIG:
  558. case CSF_FW_BINARY_ENTRY_TYPE_FUTF_TEST:
  559. case CSF_FW_BINARY_ENTRY_TYPE_TRACE_BUFFER:
  560. case CSF_FW_BINARY_ENTRY_TYPE_TIMELINE_METADATA:
  561. return 0;
  562. default:
  563. break;
  564. }
  565. if (ehdr & CSF_FW_BINARY_ENTRY_OPTIONAL)
  566. return 0;
  567. drm_err(&ptdev->base,
  568. "Unsupported non-optional entry type %u in firmware\n",
  569. CSF_FW_BINARY_ENTRY_TYPE(ehdr));
  570. return -EINVAL;
  571. }
  572. static int panthor_fw_load(struct panthor_device *ptdev)
  573. {
  574. const struct firmware *fw = NULL;
  575. struct panthor_fw_binary_iter iter = {};
  576. struct panthor_fw_binary_hdr hdr;
  577. char fw_path[128];
  578. int ret;
  579. snprintf(fw_path, sizeof(fw_path), "arm/mali/arch%d.%d/%s",
  580. (u32)GPU_ARCH_MAJOR(ptdev->gpu_info.gpu_id),
  581. (u32)GPU_ARCH_MINOR(ptdev->gpu_info.gpu_id),
  582. CSF_FW_NAME);
  583. ret = request_firmware(&fw, fw_path, ptdev->base.dev);
  584. if (ret) {
  585. drm_err(&ptdev->base, "Failed to load firmware image '%s'\n",
  586. CSF_FW_NAME);
  587. return ret;
  588. }
  589. iter.data = fw->data;
  590. iter.size = fw->size;
  591. ret = panthor_fw_binary_iter_read(ptdev, &iter, &hdr, sizeof(hdr));
  592. if (ret)
  593. goto out;
  594. if (hdr.magic != CSF_FW_BINARY_HEADER_MAGIC) {
  595. ret = -EINVAL;
  596. drm_err(&ptdev->base, "Invalid firmware magic\n");
  597. goto out;
  598. }
  599. if (hdr.major != CSF_FW_BINARY_HEADER_MAJOR_MAX) {
  600. ret = -EINVAL;
  601. drm_err(&ptdev->base, "Unsupported firmware binary header version %d.%d (expected %d.x)\n",
  602. hdr.major, hdr.minor, CSF_FW_BINARY_HEADER_MAJOR_MAX);
  603. goto out;
  604. }
  605. if (hdr.size > iter.size) {
  606. drm_err(&ptdev->base, "Firmware image is truncated\n");
  607. goto out;
  608. }
  609. iter.size = hdr.size;
  610. while (iter.offset < hdr.size) {
  611. ret = panthor_fw_load_entry(ptdev, fw, &iter);
  612. if (ret)
  613. goto out;
  614. }
  615. if (!ptdev->fw->shared_section) {
  616. drm_err(&ptdev->base, "Shared interface region not found\n");
  617. ret = -EINVAL;
  618. goto out;
  619. }
  620. out:
  621. release_firmware(fw);
  622. return ret;
  623. }
  624. /**
  625. * iface_fw_to_cpu_addr() - Turn an MCU address into a CPU address
  626. * @ptdev: Device.
  627. * @mcu_va: MCU address.
  628. *
  629. * Return: NULL if the address is not part of the shared section, non-NULL otherwise.
  630. */
  631. static void *iface_fw_to_cpu_addr(struct panthor_device *ptdev, u32 mcu_va)
  632. {
  633. u64 shared_mem_start = panthor_kernel_bo_gpuva(ptdev->fw->shared_section->mem);
  634. u64 shared_mem_end = shared_mem_start +
  635. panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
  636. if (mcu_va < shared_mem_start || mcu_va >= shared_mem_end)
  637. return NULL;
  638. return ptdev->fw->shared_section->mem->kmap + (mcu_va - shared_mem_start);
  639. }
  640. static int panthor_init_cs_iface(struct panthor_device *ptdev,
  641. unsigned int csg_idx, unsigned int cs_idx)
  642. {
  643. struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
  644. struct panthor_fw_csg_iface *csg_iface = panthor_fw_get_csg_iface(ptdev, csg_idx);
  645. struct panthor_fw_cs_iface *cs_iface = &ptdev->fw->iface.streams[csg_idx][cs_idx];
  646. u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
  647. u32 iface_offset = CSF_GROUP_CONTROL_OFFSET +
  648. (csg_idx * glb_iface->control->group_stride) +
  649. CSF_STREAM_CONTROL_OFFSET +
  650. (cs_idx * csg_iface->control->stream_stride);
  651. struct panthor_fw_cs_iface *first_cs_iface =
  652. panthor_fw_get_cs_iface(ptdev, 0, 0);
  653. if (iface_offset + sizeof(*cs_iface) >= shared_section_sz)
  654. return -EINVAL;
  655. spin_lock_init(&cs_iface->lock);
  656. cs_iface->control = ptdev->fw->shared_section->mem->kmap + iface_offset;
  657. cs_iface->input = iface_fw_to_cpu_addr(ptdev, cs_iface->control->input_va);
  658. cs_iface->output = iface_fw_to_cpu_addr(ptdev, cs_iface->control->output_va);
  659. if (!cs_iface->input || !cs_iface->output) {
  660. drm_err(&ptdev->base, "Invalid stream control interface input/output VA");
  661. return -EINVAL;
  662. }
  663. if (cs_iface != first_cs_iface) {
  664. if (cs_iface->control->features != first_cs_iface->control->features) {
  665. drm_err(&ptdev->base, "Expecting identical CS slots");
  666. return -EINVAL;
  667. }
  668. } else {
  669. u32 reg_count = CS_FEATURES_WORK_REGS(cs_iface->control->features);
  670. ptdev->csif_info.cs_reg_count = reg_count;
  671. ptdev->csif_info.unpreserved_cs_reg_count = CSF_UNPRESERVED_REG_COUNT;
  672. }
  673. return 0;
  674. }
  675. static bool compare_csg(const struct panthor_fw_csg_control_iface *a,
  676. const struct panthor_fw_csg_control_iface *b)
  677. {
  678. if (a->features != b->features)
  679. return false;
  680. if (a->suspend_size != b->suspend_size)
  681. return false;
  682. if (a->protm_suspend_size != b->protm_suspend_size)
  683. return false;
  684. if (a->stream_num != b->stream_num)
  685. return false;
  686. return true;
  687. }
  688. static int panthor_init_csg_iface(struct panthor_device *ptdev,
  689. unsigned int csg_idx)
  690. {
  691. struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
  692. struct panthor_fw_csg_iface *csg_iface = &ptdev->fw->iface.groups[csg_idx];
  693. u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
  694. u32 iface_offset = CSF_GROUP_CONTROL_OFFSET + (csg_idx * glb_iface->control->group_stride);
  695. unsigned int i;
  696. if (iface_offset + sizeof(*csg_iface) >= shared_section_sz)
  697. return -EINVAL;
  698. spin_lock_init(&csg_iface->lock);
  699. csg_iface->control = ptdev->fw->shared_section->mem->kmap + iface_offset;
  700. csg_iface->input = iface_fw_to_cpu_addr(ptdev, csg_iface->control->input_va);
  701. csg_iface->output = iface_fw_to_cpu_addr(ptdev, csg_iface->control->output_va);
  702. if (csg_iface->control->stream_num < MIN_CS_PER_CSG ||
  703. csg_iface->control->stream_num > MAX_CS_PER_CSG)
  704. return -EINVAL;
  705. if (!csg_iface->input || !csg_iface->output) {
  706. drm_err(&ptdev->base, "Invalid group control interface input/output VA");
  707. return -EINVAL;
  708. }
  709. if (csg_idx > 0) {
  710. struct panthor_fw_csg_iface *first_csg_iface =
  711. panthor_fw_get_csg_iface(ptdev, 0);
  712. if (!compare_csg(first_csg_iface->control, csg_iface->control)) {
  713. drm_err(&ptdev->base, "Expecting identical CSG slots");
  714. return -EINVAL;
  715. }
  716. }
  717. for (i = 0; i < csg_iface->control->stream_num; i++) {
  718. int ret = panthor_init_cs_iface(ptdev, csg_idx, i);
  719. if (ret)
  720. return ret;
  721. }
  722. return 0;
  723. }
  724. static u32 panthor_get_instr_features(struct panthor_device *ptdev)
  725. {
  726. struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
  727. if (glb_iface->control->version < CSF_IFACE_VERSION(1, 1, 0))
  728. return 0;
  729. return glb_iface->control->instr_features;
  730. }
  731. static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
  732. {
  733. struct panthor_fw_global_iface *glb_iface = &ptdev->fw->iface.global;
  734. unsigned int i;
  735. if (!ptdev->fw->shared_section->mem->kmap)
  736. return -EINVAL;
  737. spin_lock_init(&glb_iface->lock);
  738. glb_iface->control = ptdev->fw->shared_section->mem->kmap;
  739. if (!glb_iface->control->version) {
  740. drm_err(&ptdev->base, "Firmware version is 0. Firmware may have failed to boot");
  741. return -EINVAL;
  742. }
  743. glb_iface->input = iface_fw_to_cpu_addr(ptdev, glb_iface->control->input_va);
  744. glb_iface->output = iface_fw_to_cpu_addr(ptdev, glb_iface->control->output_va);
  745. if (!glb_iface->input || !glb_iface->output) {
  746. drm_err(&ptdev->base, "Invalid global control interface input/output VA");
  747. return -EINVAL;
  748. }
  749. if (glb_iface->control->group_num > MAX_CSGS ||
  750. glb_iface->control->group_num < MIN_CSGS) {
  751. drm_err(&ptdev->base, "Invalid number of control groups");
  752. return -EINVAL;
  753. }
  754. for (i = 0; i < glb_iface->control->group_num; i++) {
  755. int ret = panthor_init_csg_iface(ptdev, i);
  756. if (ret)
  757. return ret;
  758. }
  759. drm_info(&ptdev->base, "CSF FW v%d.%d.%d, Features %#x Instrumentation features %#x",
  760. CSF_IFACE_VERSION_MAJOR(glb_iface->control->version),
  761. CSF_IFACE_VERSION_MINOR(glb_iface->control->version),
  762. CSF_IFACE_VERSION_PATCH(glb_iface->control->version),
  763. glb_iface->control->features,
  764. panthor_get_instr_features(ptdev));
  765. return 0;
  766. }
  767. static void panthor_fw_init_global_iface(struct panthor_device *ptdev)
  768. {
  769. struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
  770. /* Enable all cores. */
  771. glb_iface->input->core_en_mask = ptdev->gpu_info.shader_present;
  772. /* Setup timers. */
  773. glb_iface->input->poweroff_timer = panthor_fw_conv_timeout(ptdev, PWROFF_HYSTERESIS_US);
  774. glb_iface->input->progress_timer = PROGRESS_TIMEOUT_CYCLES >> PROGRESS_TIMEOUT_SCALE_SHIFT;
  775. glb_iface->input->idle_timer = panthor_fw_conv_timeout(ptdev, IDLE_HYSTERESIS_US);
  776. /* Enable interrupts we care about. */
  777. glb_iface->input->ack_irq_mask = GLB_CFG_ALLOC_EN |
  778. GLB_PING |
  779. GLB_CFG_PROGRESS_TIMER |
  780. GLB_CFG_POWEROFF_TIMER |
  781. GLB_IDLE_EN |
  782. GLB_IDLE;
  783. panthor_fw_update_reqs(glb_iface, req, GLB_IDLE_EN, GLB_IDLE_EN);
  784. panthor_fw_toggle_reqs(glb_iface, req, ack,
  785. GLB_CFG_ALLOC_EN |
  786. GLB_CFG_POWEROFF_TIMER |
  787. GLB_CFG_PROGRESS_TIMER);
  788. gpu_write(ptdev, CSF_DOORBELL(CSF_GLB_DOORBELL_ID), 1);
  789. /* Kick the watchdog. */
  790. mod_delayed_work(ptdev->reset.wq, &ptdev->fw->watchdog.ping_work,
  791. msecs_to_jiffies(PING_INTERVAL_MS));
  792. }
  793. static void panthor_job_irq_handler(struct panthor_device *ptdev, u32 status)
  794. {
  795. if (!ptdev->fw->booted && (status & JOB_INT_GLOBAL_IF))
  796. ptdev->fw->booted = true;
  797. wake_up_all(&ptdev->fw->req_waitqueue);
  798. /* If the FW is not booted, don't process IRQs, just flag the FW as booted. */
  799. if (!ptdev->fw->booted)
  800. return;
  801. panthor_sched_report_fw_events(ptdev, status);
  802. }
  803. PANTHOR_IRQ_HANDLER(job, JOB, panthor_job_irq_handler);
  804. static int panthor_fw_start(struct panthor_device *ptdev)
  805. {
  806. bool timedout = false;
  807. ptdev->fw->booted = false;
  808. panthor_job_irq_resume(&ptdev->fw->irq, ~0);
  809. gpu_write(ptdev, MCU_CONTROL, MCU_CONTROL_AUTO);
  810. if (!wait_event_timeout(ptdev->fw->req_waitqueue,
  811. ptdev->fw->booted,
  812. msecs_to_jiffies(1000))) {
  813. if (!ptdev->fw->booted &&
  814. !(gpu_read(ptdev, JOB_INT_STAT) & JOB_INT_GLOBAL_IF))
  815. timedout = true;
  816. }
  817. if (timedout) {
  818. static const char * const status_str[] = {
  819. [MCU_STATUS_DISABLED] = "disabled",
  820. [MCU_STATUS_ENABLED] = "enabled",
  821. [MCU_STATUS_HALT] = "halt",
  822. [MCU_STATUS_FATAL] = "fatal",
  823. };
  824. u32 status = gpu_read(ptdev, MCU_STATUS);
  825. drm_err(&ptdev->base, "Failed to boot MCU (status=%s)",
  826. status < ARRAY_SIZE(status_str) ? status_str[status] : "unknown");
  827. return -ETIMEDOUT;
  828. }
  829. return 0;
  830. }
  831. static void panthor_fw_stop(struct panthor_device *ptdev)
  832. {
  833. u32 status;
  834. gpu_write(ptdev, MCU_CONTROL, MCU_CONTROL_DISABLE);
  835. if (readl_poll_timeout(ptdev->iomem + MCU_STATUS, status,
  836. status == MCU_STATUS_DISABLED, 10, 100000))
  837. drm_err(&ptdev->base, "Failed to stop MCU");
  838. }
  839. /**
  840. * panthor_fw_pre_reset() - Call before a reset.
  841. * @ptdev: Device.
  842. * @on_hang: true if the reset was triggered on a GPU hang.
  843. *
  844. * If the reset is not triggered on a hang, we try to gracefully halt the
  845. * MCU, so we can do a fast-reset when panthor_fw_post_reset() is called.
  846. */
  847. void panthor_fw_pre_reset(struct panthor_device *ptdev, bool on_hang)
  848. {
  849. /* Make sure we won't be woken up by a ping. */
  850. cancel_delayed_work_sync(&ptdev->fw->watchdog.ping_work);
  851. ptdev->fw->fast_reset = false;
  852. if (!on_hang) {
  853. struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
  854. u32 status;
  855. panthor_fw_update_reqs(glb_iface, req, GLB_HALT, GLB_HALT);
  856. gpu_write(ptdev, CSF_DOORBELL(CSF_GLB_DOORBELL_ID), 1);
  857. if (!readl_poll_timeout(ptdev->iomem + MCU_STATUS, status,
  858. status == MCU_STATUS_HALT, 10, 100000) &&
  859. glb_iface->output->halt_status == PANTHOR_FW_HALT_OK) {
  860. ptdev->fw->fast_reset = true;
  861. } else {
  862. drm_warn(&ptdev->base, "Failed to cleanly suspend MCU");
  863. }
  864. /* The FW detects 0 -> 1 transitions. Make sure we reset
  865. * the HALT bit before the FW is rebooted.
  866. */
  867. panthor_fw_update_reqs(glb_iface, req, 0, GLB_HALT);
  868. }
  869. panthor_job_irq_suspend(&ptdev->fw->irq);
  870. panthor_fw_stop(ptdev);
  871. }
  872. /**
  873. * panthor_fw_post_reset() - Call after a reset.
  874. * @ptdev: Device.
  875. *
  876. * Start the FW. If this is not a fast reset, all FW sections are reloaded to
  877. * make sure we can recover from a memory corruption.
  878. */
  879. int panthor_fw_post_reset(struct panthor_device *ptdev)
  880. {
  881. int ret;
  882. /* Make the MCU VM active. */
  883. ret = panthor_vm_active(ptdev->fw->vm);
  884. if (ret)
  885. return ret;
  886. /* If this is a fast reset, try to start the MCU without reloading
  887. * the FW sections. If it fails, go for a full reset.
  888. */
  889. if (ptdev->fw->fast_reset) {
  890. ret = panthor_fw_start(ptdev);
  891. if (!ret)
  892. goto out;
  893. /* Forcibly reset the MCU and force a slow reset, so we get a
  894. * fresh boot on the next panthor_fw_start() call.
  895. */
  896. panthor_fw_stop(ptdev);
  897. ptdev->fw->fast_reset = false;
  898. drm_err(&ptdev->base, "FW fast reset failed, trying a slow reset");
  899. ret = panthor_vm_flush_all(ptdev->fw->vm);
  900. if (ret) {
  901. drm_err(&ptdev->base, "FW slow reset failed (couldn't flush FW's AS l2cache)");
  902. return ret;
  903. }
  904. }
  905. /* Reload all sections, including RO ones. We're not supposed
  906. * to end up here anyway, let's just assume the overhead of
  907. * reloading everything is acceptable.
  908. */
  909. panthor_reload_fw_sections(ptdev, true);
  910. ret = panthor_fw_start(ptdev);
  911. if (ret) {
  912. drm_err(&ptdev->base, "FW slow reset failed (couldn't start the FW )");
  913. return ret;
  914. }
  915. out:
  916. /* We must re-initialize the global interface even on fast-reset. */
  917. panthor_fw_init_global_iface(ptdev);
  918. return 0;
  919. }
  920. /**
  921. * panthor_fw_unplug() - Called when the device is unplugged.
  922. * @ptdev: Device.
  923. *
  924. * This function must make sure all pending operations are flushed before
  925. * will release device resources, thus preventing any interaction with
  926. * the HW.
  927. *
  928. * If there is still FW-related work running after this function returns,
  929. * they must use drm_dev_{enter,exit}() and skip any HW access when
  930. * drm_dev_enter() returns false.
  931. */
  932. void panthor_fw_unplug(struct panthor_device *ptdev)
  933. {
  934. struct panthor_fw_section *section;
  935. cancel_delayed_work_sync(&ptdev->fw->watchdog.ping_work);
  936. /* Make sure the IRQ handler can be called after that point. */
  937. if (ptdev->fw->irq.irq)
  938. panthor_job_irq_suspend(&ptdev->fw->irq);
  939. panthor_fw_stop(ptdev);
  940. list_for_each_entry(section, &ptdev->fw->sections, node)
  941. panthor_kernel_bo_destroy(section->mem);
  942. /* We intentionally don't call panthor_vm_idle() and let
  943. * panthor_mmu_unplug() release the AS we acquired with
  944. * panthor_vm_active() so we don't have to track the VM active/idle
  945. * state to keep the active_refcnt balanced.
  946. */
  947. panthor_vm_put(ptdev->fw->vm);
  948. ptdev->fw->vm = NULL;
  949. panthor_gpu_power_off(ptdev, L2, ptdev->gpu_info.l2_present, 20000);
  950. }
  951. /**
  952. * panthor_fw_wait_acks() - Wait for requests to be acknowledged by the FW.
  953. * @req_ptr: Pointer to the req register.
  954. * @ack_ptr: Pointer to the ack register.
  955. * @wq: Wait queue to use for the sleeping wait.
  956. * @req_mask: Mask of requests to wait for.
  957. * @acked: Pointer to field that's updated with the acked requests.
  958. * If the function returns 0, *acked == req_mask.
  959. * @timeout_ms: Timeout expressed in milliseconds.
  960. *
  961. * Return: 0 on success, -ETIMEDOUT otherwise.
  962. */
  963. static int panthor_fw_wait_acks(const u32 *req_ptr, const u32 *ack_ptr,
  964. wait_queue_head_t *wq,
  965. u32 req_mask, u32 *acked,
  966. u32 timeout_ms)
  967. {
  968. u32 ack, req = READ_ONCE(*req_ptr) & req_mask;
  969. int ret;
  970. /* Busy wait for a few µsecs before falling back to a sleeping wait. */
  971. *acked = req_mask;
  972. ret = read_poll_timeout_atomic(READ_ONCE, ack,
  973. (ack & req_mask) == req,
  974. 0, 10, 0,
  975. *ack_ptr);
  976. if (!ret)
  977. return 0;
  978. if (wait_event_timeout(*wq, (READ_ONCE(*ack_ptr) & req_mask) == req,
  979. msecs_to_jiffies(timeout_ms)))
  980. return 0;
  981. /* Check one last time, in case we were not woken up for some reason. */
  982. ack = READ_ONCE(*ack_ptr);
  983. if ((ack & req_mask) == req)
  984. return 0;
  985. *acked = ~(req ^ ack) & req_mask;
  986. return -ETIMEDOUT;
  987. }
  988. /**
  989. * panthor_fw_glb_wait_acks() - Wait for global requests to be acknowledged.
  990. * @ptdev: Device.
  991. * @req_mask: Mask of requests to wait for.
  992. * @acked: Pointer to field that's updated with the acked requests.
  993. * If the function returns 0, *acked == req_mask.
  994. * @timeout_ms: Timeout expressed in milliseconds.
  995. *
  996. * Return: 0 on success, -ETIMEDOUT otherwise.
  997. */
  998. int panthor_fw_glb_wait_acks(struct panthor_device *ptdev,
  999. u32 req_mask, u32 *acked,
  1000. u32 timeout_ms)
  1001. {
  1002. struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
  1003. /* GLB_HALT doesn't get acked through the FW interface. */
  1004. if (drm_WARN_ON(&ptdev->base, req_mask & (~GLB_REQ_MASK | GLB_HALT)))
  1005. return -EINVAL;
  1006. return panthor_fw_wait_acks(&glb_iface->input->req,
  1007. &glb_iface->output->ack,
  1008. &ptdev->fw->req_waitqueue,
  1009. req_mask, acked, timeout_ms);
  1010. }
  1011. /**
  1012. * panthor_fw_csg_wait_acks() - Wait for command stream group requests to be acknowledged.
  1013. * @ptdev: Device.
  1014. * @csg_slot: CSG slot ID.
  1015. * @req_mask: Mask of requests to wait for.
  1016. * @acked: Pointer to field that's updated with the acked requests.
  1017. * If the function returns 0, *acked == req_mask.
  1018. * @timeout_ms: Timeout expressed in milliseconds.
  1019. *
  1020. * Return: 0 on success, -ETIMEDOUT otherwise.
  1021. */
  1022. int panthor_fw_csg_wait_acks(struct panthor_device *ptdev, u32 csg_slot,
  1023. u32 req_mask, u32 *acked, u32 timeout_ms)
  1024. {
  1025. struct panthor_fw_csg_iface *csg_iface = panthor_fw_get_csg_iface(ptdev, csg_slot);
  1026. int ret;
  1027. if (drm_WARN_ON(&ptdev->base, req_mask & ~CSG_REQ_MASK))
  1028. return -EINVAL;
  1029. ret = panthor_fw_wait_acks(&csg_iface->input->req,
  1030. &csg_iface->output->ack,
  1031. &ptdev->fw->req_waitqueue,
  1032. req_mask, acked, timeout_ms);
  1033. /*
  1034. * Check that all bits in the state field were updated, if any mismatch
  1035. * then clear all bits in the state field. This allows code to do
  1036. * (acked & CSG_STATE_MASK) and get the right value.
  1037. */
  1038. if ((*acked & CSG_STATE_MASK) != CSG_STATE_MASK)
  1039. *acked &= ~CSG_STATE_MASK;
  1040. return ret;
  1041. }
  1042. /**
  1043. * panthor_fw_ring_csg_doorbells() - Ring command stream group doorbells.
  1044. * @ptdev: Device.
  1045. * @csg_mask: Bitmask encoding the command stream group doorbells to ring.
  1046. *
  1047. * This function is toggling bits in the doorbell_req and ringing the
  1048. * global doorbell. It doesn't require a user doorbell to be attached to
  1049. * the group.
  1050. */
  1051. void panthor_fw_ring_csg_doorbells(struct panthor_device *ptdev, u32 csg_mask)
  1052. {
  1053. struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
  1054. panthor_fw_toggle_reqs(glb_iface, doorbell_req, doorbell_ack, csg_mask);
  1055. gpu_write(ptdev, CSF_DOORBELL(CSF_GLB_DOORBELL_ID), 1);
  1056. }
  1057. static void panthor_fw_ping_work(struct work_struct *work)
  1058. {
  1059. struct panthor_fw *fw = container_of(work, struct panthor_fw, watchdog.ping_work.work);
  1060. struct panthor_device *ptdev = fw->irq.ptdev;
  1061. struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
  1062. u32 acked;
  1063. int ret;
  1064. if (panthor_device_reset_is_pending(ptdev))
  1065. return;
  1066. panthor_fw_toggle_reqs(glb_iface, req, ack, GLB_PING);
  1067. gpu_write(ptdev, CSF_DOORBELL(CSF_GLB_DOORBELL_ID), 1);
  1068. ret = panthor_fw_glb_wait_acks(ptdev, GLB_PING, &acked, 100);
  1069. if (ret) {
  1070. panthor_device_schedule_reset(ptdev);
  1071. drm_err(&ptdev->base, "FW ping timeout, scheduling a reset");
  1072. } else {
  1073. mod_delayed_work(ptdev->reset.wq, &fw->watchdog.ping_work,
  1074. msecs_to_jiffies(PING_INTERVAL_MS));
  1075. }
  1076. }
  1077. /**
  1078. * panthor_fw_init() - Initialize FW related data.
  1079. * @ptdev: Device.
  1080. *
  1081. * Return: 0 on success, a negative error code otherwise.
  1082. */
  1083. int panthor_fw_init(struct panthor_device *ptdev)
  1084. {
  1085. struct panthor_fw *fw;
  1086. int ret, irq;
  1087. fw = drmm_kzalloc(&ptdev->base, sizeof(*fw), GFP_KERNEL);
  1088. if (!fw)
  1089. return -ENOMEM;
  1090. ptdev->fw = fw;
  1091. init_waitqueue_head(&fw->req_waitqueue);
  1092. INIT_LIST_HEAD(&fw->sections);
  1093. INIT_DELAYED_WORK(&fw->watchdog.ping_work, panthor_fw_ping_work);
  1094. irq = platform_get_irq_byname(to_platform_device(ptdev->base.dev), "job");
  1095. if (irq <= 0)
  1096. return -ENODEV;
  1097. ret = panthor_request_job_irq(ptdev, &fw->irq, irq, 0);
  1098. if (ret) {
  1099. drm_err(&ptdev->base, "failed to request job irq");
  1100. return ret;
  1101. }
  1102. ret = panthor_gpu_l2_power_on(ptdev);
  1103. if (ret)
  1104. return ret;
  1105. fw->vm = panthor_vm_create(ptdev, true,
  1106. 0, SZ_4G,
  1107. CSF_MCU_SHARED_REGION_START,
  1108. CSF_MCU_SHARED_REGION_SIZE);
  1109. if (IS_ERR(fw->vm)) {
  1110. ret = PTR_ERR(fw->vm);
  1111. fw->vm = NULL;
  1112. goto err_unplug_fw;
  1113. }
  1114. ret = panthor_fw_load(ptdev);
  1115. if (ret)
  1116. goto err_unplug_fw;
  1117. ret = panthor_vm_active(fw->vm);
  1118. if (ret)
  1119. goto err_unplug_fw;
  1120. ret = panthor_fw_start(ptdev);
  1121. if (ret)
  1122. goto err_unplug_fw;
  1123. ret = panthor_fw_init_ifaces(ptdev);
  1124. if (ret)
  1125. goto err_unplug_fw;
  1126. panthor_fw_init_global_iface(ptdev);
  1127. return 0;
  1128. err_unplug_fw:
  1129. panthor_fw_unplug(ptdev);
  1130. return ret;
  1131. }
  1132. MODULE_FIRMWARE("arm/mali/arch10.8/mali_csffw.bin");