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