ipa_uc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2018-2024 Linaro Ltd.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/io.h>
  7. #include <linux/pm_runtime.h>
  8. #include <linux/types.h>
  9. #include "ipa.h"
  10. #include "ipa_interrupt.h"
  11. #include "ipa_power.h"
  12. #include "ipa_reg.h"
  13. #include "ipa_uc.h"
  14. /**
  15. * DOC: The IPA embedded microcontroller
  16. *
  17. * The IPA incorporates a microcontroller that is able to do some additional
  18. * handling/offloading of network activity. The current code makes
  19. * essentially no use of the microcontroller, but it still requires some
  20. * initialization. It needs to be notified in the event the AP crashes.
  21. *
  22. * The microcontroller can generate two interrupts to the AP. One interrupt
  23. * is used to indicate that a response to a request from the AP is available.
  24. * The other is used to notify the AP of the occurrence of an event. In
  25. * addition, the AP can interrupt the microcontroller by writing a register.
  26. *
  27. * A 128 byte block of structured memory within the IPA SRAM is used together
  28. * with these interrupts to implement the communication interface between the
  29. * AP and the IPA microcontroller. Each side writes data to the shared area
  30. * before interrupting its peer, which will read the written data in response
  31. * to the interrupt. Some information found in the shared area is currently
  32. * unused. All remaining space in the shared area is reserved, and must not
  33. * be read or written by the AP.
  34. */
  35. /* Supports hardware interface version 0x2000 */
  36. /* Delay to allow a the microcontroller to save state when crashing */
  37. #define IPA_SEND_DELAY 100 /* microseconds */
  38. /**
  39. * struct ipa_uc_mem_area - AP/microcontroller shared memory area
  40. * @command: command code (AP->microcontroller)
  41. * @reserved0: reserved bytes; avoid reading or writing
  42. * @command_param: low 32 bits of command parameter (AP->microcontroller)
  43. * @command_param_hi: high 32 bits of command parameter (AP->microcontroller)
  44. *
  45. * @response: response code (microcontroller->AP)
  46. * @reserved1: reserved bytes; avoid reading or writing
  47. * @response_param: response parameter (microcontroller->AP)
  48. *
  49. * @event: event code (microcontroller->AP)
  50. * @reserved2: reserved bytes; avoid reading or writing
  51. * @event_param: event parameter (microcontroller->AP)
  52. *
  53. * @first_error_address: address of first error-source on SNOC
  54. * @hw_state: state of hardware (including error type information)
  55. * @warning_counter: counter of non-fatal hardware errors
  56. * @reserved3: reserved bytes; avoid reading or writing
  57. * @interface_version: hardware-reported interface version
  58. * @reserved4: reserved bytes; avoid reading or writing
  59. *
  60. * A shared memory area at the base of IPA resident memory is used for
  61. * communication with the microcontroller. The region is 128 bytes in
  62. * size, but only the first 40 bytes (structured this way) are used.
  63. */
  64. struct ipa_uc_mem_area {
  65. u8 command; /* enum ipa_uc_command */
  66. u8 reserved0[3];
  67. __le32 command_param;
  68. __le32 command_param_hi;
  69. u8 response; /* enum ipa_uc_response */
  70. u8 reserved1[3];
  71. __le32 response_param;
  72. u8 event; /* enum ipa_uc_event */
  73. u8 reserved2[3];
  74. __le32 event_param;
  75. __le32 first_error_address;
  76. u8 hw_state;
  77. u8 warning_counter;
  78. __le16 reserved3;
  79. __le16 interface_version;
  80. __le16 reserved4;
  81. };
  82. /** enum ipa_uc_command - commands from the AP to the microcontroller */
  83. enum ipa_uc_command {
  84. IPA_UC_COMMAND_NO_OP = 0x0,
  85. IPA_UC_COMMAND_UPDATE_FLAGS = 0x1,
  86. IPA_UC_COMMAND_DEBUG_RUN_TEST = 0x2,
  87. IPA_UC_COMMAND_DEBUG_GET_INFO = 0x3,
  88. IPA_UC_COMMAND_ERR_FATAL = 0x4,
  89. IPA_UC_COMMAND_CLK_GATE = 0x5,
  90. IPA_UC_COMMAND_CLK_UNGATE = 0x6,
  91. IPA_UC_COMMAND_MEMCPY = 0x7,
  92. IPA_UC_COMMAND_RESET_PIPE = 0x8,
  93. IPA_UC_COMMAND_REG_WRITE = 0x9,
  94. IPA_UC_COMMAND_GSI_CH_EMPTY = 0xa,
  95. };
  96. /** enum ipa_uc_response - microcontroller response codes */
  97. enum ipa_uc_response {
  98. IPA_UC_RESPONSE_NO_OP = 0x0,
  99. IPA_UC_RESPONSE_INIT_COMPLETED = 0x1,
  100. IPA_UC_RESPONSE_CMD_COMPLETED = 0x2,
  101. IPA_UC_RESPONSE_DEBUG_GET_INFO = 0x3,
  102. };
  103. /** enum ipa_uc_event - common cpu events reported by the microcontroller */
  104. enum ipa_uc_event {
  105. IPA_UC_EVENT_NO_OP = 0x0,
  106. IPA_UC_EVENT_ERROR = 0x1,
  107. IPA_UC_EVENT_LOG_INFO = 0x2,
  108. };
  109. static struct ipa_uc_mem_area *ipa_uc_shared(struct ipa *ipa)
  110. {
  111. const struct ipa_mem *mem = ipa_mem_find(ipa, IPA_MEM_UC_SHARED);
  112. u32 offset = ipa->mem_offset + mem->offset;
  113. return ipa->mem_virt + offset;
  114. }
  115. /* Microcontroller event IPA interrupt handler */
  116. static void ipa_uc_event_handler(struct ipa *ipa)
  117. {
  118. struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa);
  119. struct device *dev = ipa->dev;
  120. if (shared->event == IPA_UC_EVENT_ERROR)
  121. dev_err(dev, "microcontroller error event\n");
  122. else if (shared->event != IPA_UC_EVENT_LOG_INFO)
  123. dev_err(dev, "unsupported microcontroller event %u\n",
  124. shared->event);
  125. /* The LOG_INFO event can be safely ignored */
  126. }
  127. /* Microcontroller response IPA interrupt handler */
  128. static void ipa_uc_response_hdlr(struct ipa *ipa)
  129. {
  130. struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa);
  131. struct device *dev = ipa->dev;
  132. /* An INIT_COMPLETED response message is sent to the AP by the
  133. * microcontroller when it is operational. Other than this, the AP
  134. * should only receive responses from the microcontroller when it has
  135. * sent it a request message.
  136. *
  137. * We can drop the power reference taken in ipa_uc_power() once we
  138. * know the microcontroller has finished its initialization.
  139. */
  140. switch (shared->response) {
  141. case IPA_UC_RESPONSE_INIT_COMPLETED:
  142. if (ipa->uc_powered) {
  143. ipa->uc_loaded = true;
  144. ipa_power_retention(ipa, true);
  145. (void)pm_runtime_put_autosuspend(dev);
  146. ipa->uc_powered = false;
  147. } else {
  148. dev_warn(dev, "unexpected init_completed response\n");
  149. }
  150. break;
  151. default:
  152. dev_warn(dev, "unsupported microcontroller response %u\n",
  153. shared->response);
  154. break;
  155. }
  156. }
  157. void ipa_uc_interrupt_handler(struct ipa *ipa, enum ipa_irq_id irq_id)
  158. {
  159. /* Silently ignore anything unrecognized */
  160. if (irq_id == IPA_IRQ_UC_0)
  161. ipa_uc_event_handler(ipa);
  162. else if (irq_id == IPA_IRQ_UC_1)
  163. ipa_uc_response_hdlr(ipa);
  164. }
  165. /* Configure the IPA microcontroller subsystem */
  166. void ipa_uc_config(struct ipa *ipa)
  167. {
  168. ipa->uc_powered = false;
  169. ipa->uc_loaded = false;
  170. ipa_interrupt_enable(ipa, IPA_IRQ_UC_0);
  171. ipa_interrupt_enable(ipa, IPA_IRQ_UC_1);
  172. }
  173. /* Inverse of ipa_uc_config() */
  174. void ipa_uc_deconfig(struct ipa *ipa)
  175. {
  176. struct device *dev = ipa->dev;
  177. ipa_interrupt_disable(ipa, IPA_IRQ_UC_1);
  178. ipa_interrupt_disable(ipa, IPA_IRQ_UC_0);
  179. if (ipa->uc_loaded)
  180. ipa_power_retention(ipa, false);
  181. if (!ipa->uc_powered)
  182. return;
  183. (void)pm_runtime_put_autosuspend(dev);
  184. }
  185. /* Take a proxy power reference for the microcontroller */
  186. void ipa_uc_power(struct ipa *ipa)
  187. {
  188. struct device *dev = ipa->dev;
  189. static bool already;
  190. int ret;
  191. if (already)
  192. return;
  193. already = true; /* Only do this on first boot */
  194. /* This power reference dropped in ipa_uc_response_hdlr() above */
  195. ret = pm_runtime_get_sync(dev);
  196. if (ret < 0) {
  197. pm_runtime_put_noidle(dev);
  198. dev_err(dev, "error %d getting proxy power\n", ret);
  199. } else {
  200. ipa->uc_powered = true;
  201. }
  202. }
  203. /* Send a command to the microcontroller */
  204. static void send_uc_command(struct ipa *ipa, u32 command, u32 command_param)
  205. {
  206. struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa);
  207. const struct reg *reg;
  208. u32 val;
  209. /* Fill in the command data */
  210. shared->command = command;
  211. shared->command_param = cpu_to_le32(command_param);
  212. shared->command_param_hi = 0;
  213. shared->response = 0;
  214. shared->response_param = 0;
  215. /* Use an interrupt to tell the microcontroller the command is ready */
  216. reg = ipa_reg(ipa, IPA_IRQ_UC);
  217. val = reg_bit(reg, UC_INTR);
  218. iowrite32(val, ipa->reg_virt + reg_offset(reg));
  219. }
  220. /* Tell the microcontroller the AP is shutting down */
  221. void ipa_uc_panic_notifier(struct ipa *ipa)
  222. {
  223. if (!ipa->uc_loaded)
  224. return;
  225. send_uc_command(ipa, IPA_UC_COMMAND_ERR_FATAL, 0);
  226. /* give uc enough time to save state */
  227. udelay(IPA_SEND_DELAY);
  228. }