blob: 856e55a080a7f07ee2024b3d1932ba73893a8400 [file] [log] [blame]
Alex Eldera646d6e2020-03-05 22:28:27 -06001// 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 Elder799c5c22021-08-19 17:19:25 -050010#include <linux/pm_runtime.h>
Alex Eldera646d6e2020-03-05 22:28:27 -060011
12#include "ipa.h"
Alex Eldera646d6e2020-03-05 22:28:27 -060013#include "ipa_uc.h"
14
15/**
16 * DOC: The IPA embedded microcontroller
17 *
18 * The IPA incorporates a microcontroller that is able to do some additional
19 * handling/offloading of network activity. The current code makes
20 * essentially no use of the microcontroller, but it still requires some
21 * initialization. It needs to be notified in the event the AP crashes.
22 *
23 * The microcontroller can generate two interrupts to the AP. One interrupt
24 * is used to indicate that a response to a request from the AP is available.
25 * The other is used to notify the AP of the occurrence of an event. In
26 * addition, the AP can interrupt the microcontroller by writing a register.
27 *
28 * A 128 byte block of structured memory within the IPA SRAM is used together
29 * with these interrupts to implement the communication interface between the
30 * AP and the IPA microcontroller. Each side writes data to the shared area
31 * before interrupting its peer, which will read the written data in response
32 * to the interrupt. Some information found in the shared area is currently
33 * unused. All remaining space in the shared area is reserved, and must not
34 * be read or written by the AP.
35 */
36/* Supports hardware interface version 0x2000 */
37
Alex Eldera646d6e2020-03-05 22:28:27 -060038/* Delay to allow a the microcontroller to save state when crashing */
39#define IPA_SEND_DELAY 100 /* microseconds */
40
41/**
42 * struct ipa_uc_mem_area - AP/microcontroller shared memory area
43 * @command: command code (AP->microcontroller)
Alex Eldere3eea082020-07-13 07:24:18 -050044 * @reserved0: reserved bytes; avoid reading or writing
Alex Eldera646d6e2020-03-05 22:28:27 -060045 * @command_param: low 32 bits of command parameter (AP->microcontroller)
46 * @command_param_hi: high 32 bits of command parameter (AP->microcontroller)
47 *
48 * @response: response code (microcontroller->AP)
Alex Eldere3eea082020-07-13 07:24:18 -050049 * @reserved1: reserved bytes; avoid reading or writing
Alex Eldera646d6e2020-03-05 22:28:27 -060050 * @response_param: response parameter (microcontroller->AP)
51 *
52 * @event: event code (microcontroller->AP)
Alex Eldere3eea082020-07-13 07:24:18 -050053 * @reserved2: reserved bytes; avoid reading or writing
Alex Eldera646d6e2020-03-05 22:28:27 -060054 * @event_param: event parameter (microcontroller->AP)
55 *
56 * @first_error_address: address of first error-source on SNOC
57 * @hw_state: state of hardware (including error type information)
58 * @warning_counter: counter of non-fatal hardware errors
Alex Eldere3eea082020-07-13 07:24:18 -050059 * @reserved3: reserved bytes; avoid reading or writing
Alex Eldera646d6e2020-03-05 22:28:27 -060060 * @interface_version: hardware-reported interface version
Alex Eldere3eea082020-07-13 07:24:18 -050061 * @reserved4: reserved bytes; avoid reading or writing
Alex Elder722208e2020-06-30 07:58:46 -050062 *
63 * A shared memory area at the base of IPA resident memory is used for
64 * communication with the microcontroller. The region is 128 bytes in
65 * size, but only the first 40 bytes (structured this way) are used.
Alex Eldera646d6e2020-03-05 22:28:27 -060066 */
67struct ipa_uc_mem_area {
68 u8 command; /* enum ipa_uc_command */
69 u8 reserved0[3];
70 __le32 command_param;
71 __le32 command_param_hi;
72 u8 response; /* enum ipa_uc_response */
73 u8 reserved1[3];
74 __le32 response_param;
75 u8 event; /* enum ipa_uc_event */
76 u8 reserved2[3];
77
78 __le32 event_param;
79 __le32 first_error_address;
80 u8 hw_state;
81 u8 warning_counter;
82 __le16 reserved3;
83 __le16 interface_version;
84 __le16 reserved4;
85};
86
87/** enum ipa_uc_command - commands from the AP to the microcontroller */
88enum ipa_uc_command {
Alex Elder8701cb02020-11-16 17:38:01 -060089 IPA_UC_COMMAND_NO_OP = 0x0,
90 IPA_UC_COMMAND_UPDATE_FLAGS = 0x1,
91 IPA_UC_COMMAND_DEBUG_RUN_TEST = 0x2,
92 IPA_UC_COMMAND_DEBUG_GET_INFO = 0x3,
93 IPA_UC_COMMAND_ERR_FATAL = 0x4,
94 IPA_UC_COMMAND_CLK_GATE = 0x5,
95 IPA_UC_COMMAND_CLK_UNGATE = 0x6,
96 IPA_UC_COMMAND_MEMCPY = 0x7,
97 IPA_UC_COMMAND_RESET_PIPE = 0x8,
98 IPA_UC_COMMAND_REG_WRITE = 0x9,
99 IPA_UC_COMMAND_GSI_CH_EMPTY = 0xa,
Alex Eldera646d6e2020-03-05 22:28:27 -0600100};
101
102/** enum ipa_uc_response - microcontroller response codes */
103enum ipa_uc_response {
Alex Elder8701cb02020-11-16 17:38:01 -0600104 IPA_UC_RESPONSE_NO_OP = 0x0,
105 IPA_UC_RESPONSE_INIT_COMPLETED = 0x1,
106 IPA_UC_RESPONSE_CMD_COMPLETED = 0x2,
107 IPA_UC_RESPONSE_DEBUG_GET_INFO = 0x3,
Alex Eldera646d6e2020-03-05 22:28:27 -0600108};
109
110/** enum ipa_uc_event - common cpu events reported by the microcontroller */
111enum ipa_uc_event {
Alex Elder8701cb02020-11-16 17:38:01 -0600112 IPA_UC_EVENT_NO_OP = 0x0,
113 IPA_UC_EVENT_ERROR = 0x1,
114 IPA_UC_EVENT_LOG_INFO = 0x2,
Alex Eldera646d6e2020-03-05 22:28:27 -0600115};
116
117static struct ipa_uc_mem_area *ipa_uc_shared(struct ipa *ipa)
118{
Alex Elder5e3bc1e2021-06-10 14:23:07 -0500119 const struct ipa_mem *mem = ipa_mem_find(ipa, IPA_MEM_UC_SHARED);
120 u32 offset = ipa->mem_offset + mem->offset;
Alex Eldera646d6e2020-03-05 22:28:27 -0600121
122 return ipa->mem_virt + offset;
123}
124
125/* Microcontroller event IPA interrupt handler */
126static void ipa_uc_event_handler(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
131 if (shared->event == IPA_UC_EVENT_ERROR)
132 dev_err(dev, "microcontroller error event\n");
Alex Elder0a5096e2020-11-12 06:20:00 -0600133 else if (shared->event != IPA_UC_EVENT_LOG_INFO)
Alex Eldere2f154e2021-07-26 15:11:36 -0500134 dev_err(dev, "unsupported microcontroller event %u\n",
Alex Eldera646d6e2020-03-05 22:28:27 -0600135 shared->event);
Alex Elder0a5096e2020-11-12 06:20:00 -0600136 /* The LOG_INFO event can be safely ignored */
Alex Eldera646d6e2020-03-05 22:28:27 -0600137}
138
139/* Microcontroller response IPA interrupt handler */
140static void ipa_uc_response_hdlr(struct ipa *ipa, enum ipa_irq_id irq_id)
141{
142 struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa);
Alex Eldere2f154e2021-07-26 15:11:36 -0500143 struct device *dev = &ipa->pdev->dev;
Alex Eldera646d6e2020-03-05 22:28:27 -0600144
145 /* An INIT_COMPLETED response message is sent to the AP by the
146 * microcontroller when it is operational. Other than this, the AP
147 * should only receive responses from the microcontroller when it has
148 * sent it a request message.
149 *
Alex Elder7aa0e8b2021-08-20 11:01:28 -0500150 * We can drop the power reference taken in ipa_uc_power() once we
Alex Eldera646d6e2020-03-05 22:28:27 -0600151 * know the microcontroller has finished its initialization.
152 */
153 switch (shared->response) {
154 case IPA_UC_RESPONSE_INIT_COMPLETED:
Alex Elder7aa0e8b2021-08-20 11:01:28 -0500155 if (ipa->uc_powered) {
Alex Eldere2f154e2021-07-26 15:11:36 -0500156 ipa->uc_loaded = true;
Alex Elder1aac3092021-08-20 11:01:27 -0500157 pm_runtime_mark_last_busy(dev);
158 (void)pm_runtime_put_autosuspend(dev);
Alex Elder7aa0e8b2021-08-20 11:01:28 -0500159 ipa->uc_powered = false;
Alex Eldere2f154e2021-07-26 15:11:36 -0500160 } else {
161 dev_warn(dev, "unexpected init_completed response\n");
162 }
Alex Eldera646d6e2020-03-05 22:28:27 -0600163 break;
164 default:
Alex Eldere2f154e2021-07-26 15:11:36 -0500165 dev_warn(dev, "unsupported microcontroller response %u\n",
Alex Eldera646d6e2020-03-05 22:28:27 -0600166 shared->response);
167 break;
168 }
169}
170
Alex Elderdc8f7e32021-07-26 15:11:35 -0500171/* Configure the IPA microcontroller subsystem */
172void ipa_uc_config(struct ipa *ipa)
Alex Eldera646d6e2020-03-05 22:28:27 -0600173{
Alex Elder7aa0e8b2021-08-20 11:01:28 -0500174 ipa->uc_powered = false;
Alex Eldera646d6e2020-03-05 22:28:27 -0600175 ipa->uc_loaded = false;
176 ipa_interrupt_add(ipa->interrupt, IPA_IRQ_UC_0, ipa_uc_event_handler);
177 ipa_interrupt_add(ipa->interrupt, IPA_IRQ_UC_1, ipa_uc_response_hdlr);
178}
179
Alex Elderdc8f7e32021-07-26 15:11:35 -0500180/* Inverse of ipa_uc_config() */
181void ipa_uc_deconfig(struct ipa *ipa)
Alex Eldera646d6e2020-03-05 22:28:27 -0600182{
Alex Elder1aac3092021-08-20 11:01:27 -0500183 struct device *dev = &ipa->pdev->dev;
184
Alex Eldera646d6e2020-03-05 22:28:27 -0600185 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_UC_1);
186 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_UC_0);
Alex Elder7aa0e8b2021-08-20 11:01:28 -0500187 if (!ipa->uc_powered)
Alex Elder1aac3092021-08-20 11:01:27 -0500188 return;
189
190 pm_runtime_mark_last_busy(dev);
191 (void)pm_runtime_put_autosuspend(dev);
Alex Eldera646d6e2020-03-05 22:28:27 -0600192}
193
Alex Elder7aa0e8b2021-08-20 11:01:28 -0500194/* Take a proxy power reference for the microcontroller */
195void ipa_uc_power(struct ipa *ipa)
Alex Eldere2f154e2021-07-26 15:11:36 -0500196{
197 static bool already;
Alex Elder799c5c22021-08-19 17:19:25 -0500198 struct device *dev;
Alex Elder7ebd1682021-08-10 14:26:58 -0500199 int ret;
Alex Eldere2f154e2021-07-26 15:11:36 -0500200
201 if (already)
202 return;
203 already = true; /* Only do this on first boot */
204
Alex Elder799c5c22021-08-19 17:19:25 -0500205 /* This power reference dropped in ipa_uc_response_hdlr() above */
206 dev = &ipa->pdev->dev;
207 ret = pm_runtime_get_sync(dev);
208 if (ret < 0) {
209 pm_runtime_put_noidle(dev);
210 dev_err(dev, "error %d getting proxy power\n", ret);
211 } else {
Alex Elder7aa0e8b2021-08-20 11:01:28 -0500212 ipa->uc_powered = true;
Alex Elder799c5c22021-08-19 17:19:25 -0500213 }
Alex Eldere2f154e2021-07-26 15:11:36 -0500214}
215
Alex Eldera646d6e2020-03-05 22:28:27 -0600216/* Send a command to the microcontroller */
217static void send_uc_command(struct ipa *ipa, u32 command, u32 command_param)
218{
219 struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa);
Alex Eldere666aa92021-03-25 09:44:34 -0500220 u32 offset;
Alex Elder716a1152020-11-16 17:38:05 -0600221 u32 val;
Alex Eldera646d6e2020-03-05 22:28:27 -0600222
Alex Elder716a1152020-11-16 17:38:05 -0600223 /* Fill in the command data */
Alex Eldera646d6e2020-03-05 22:28:27 -0600224 shared->command = command;
225 shared->command_param = cpu_to_le32(command_param);
226 shared->command_param_hi = 0;
227 shared->response = 0;
228 shared->response_param = 0;
229
Alex Elder716a1152020-11-16 17:38:05 -0600230 /* Use an interrupt to tell the microcontroller the command is ready */
231 val = u32_encode_bits(1, UC_INTR_FMASK);
Alex Eldere666aa92021-03-25 09:44:34 -0500232 offset = ipa_reg_irq_uc_offset(ipa->version);
233 iowrite32(val, ipa->reg_virt + offset);
Alex Eldera646d6e2020-03-05 22:28:27 -0600234}
235
236/* Tell the microcontroller the AP is shutting down */
237void ipa_uc_panic_notifier(struct ipa *ipa)
238{
239 if (!ipa->uc_loaded)
240 return;
241
242 send_uc_command(ipa, IPA_UC_COMMAND_ERR_FATAL, 0);
243
244 /* give uc enough time to save state */
245 udelay(IPA_SEND_DELAY);
246}