ipa_uc.c 7.8 KB

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