panthor_fw.c 42 KB

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