1a646d6ecSAlex Elder // SPDX-License-Identifier: GPL-2.0
2a646d6ecSAlex Elder
3a646d6ecSAlex Elder /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4a4388da5SAlex Elder * Copyright (C) 2018-2022 Linaro Ltd.
5a646d6ecSAlex Elder */
6a646d6ecSAlex Elder
7a646d6ecSAlex Elder #include <linux/types.h>
8a646d6ecSAlex Elder #include <linux/io.h>
9a646d6ecSAlex Elder #include <linux/delay.h>
10799c5c24SAlex Elder #include <linux/pm_runtime.h>
11a646d6ecSAlex Elder
12a646d6ecSAlex Elder #include "ipa.h"
13a646d6ecSAlex Elder #include "ipa_uc.h"
1434a08176SAlex Elder #include "ipa_power.h"
15a646d6ecSAlex Elder
16a646d6ecSAlex Elder /**
17a646d6ecSAlex Elder * DOC: The IPA embedded microcontroller
18a646d6ecSAlex Elder *
19a646d6ecSAlex Elder * The IPA incorporates a microcontroller that is able to do some additional
20a646d6ecSAlex Elder * handling/offloading of network activity. The current code makes
21a646d6ecSAlex Elder * essentially no use of the microcontroller, but it still requires some
22a646d6ecSAlex Elder * initialization. It needs to be notified in the event the AP crashes.
23a646d6ecSAlex Elder *
24a646d6ecSAlex Elder * The microcontroller can generate two interrupts to the AP. One interrupt
25a646d6ecSAlex Elder * is used to indicate that a response to a request from the AP is available.
26a646d6ecSAlex Elder * The other is used to notify the AP of the occurrence of an event. In
27a646d6ecSAlex Elder * addition, the AP can interrupt the microcontroller by writing a register.
28a646d6ecSAlex Elder *
29a646d6ecSAlex Elder * A 128 byte block of structured memory within the IPA SRAM is used together
30a646d6ecSAlex Elder * with these interrupts to implement the communication interface between the
31a646d6ecSAlex Elder * AP and the IPA microcontroller. Each side writes data to the shared area
32a646d6ecSAlex Elder * before interrupting its peer, which will read the written data in response
33a646d6ecSAlex Elder * to the interrupt. Some information found in the shared area is currently
34a646d6ecSAlex Elder * unused. All remaining space in the shared area is reserved, and must not
35a646d6ecSAlex Elder * be read or written by the AP.
36a646d6ecSAlex Elder */
37a646d6ecSAlex Elder /* Supports hardware interface version 0x2000 */
38a646d6ecSAlex Elder
39a646d6ecSAlex Elder /* Delay to allow a the microcontroller to save state when crashing */
40a646d6ecSAlex Elder #define IPA_SEND_DELAY 100 /* microseconds */
41a646d6ecSAlex Elder
42a646d6ecSAlex Elder /**
43a646d6ecSAlex Elder * struct ipa_uc_mem_area - AP/microcontroller shared memory area
44a646d6ecSAlex Elder * @command: command code (AP->microcontroller)
45e3eea08eSAlex Elder * @reserved0: reserved bytes; avoid reading or writing
46a646d6ecSAlex Elder * @command_param: low 32 bits of command parameter (AP->microcontroller)
47a646d6ecSAlex Elder * @command_param_hi: high 32 bits of command parameter (AP->microcontroller)
48a646d6ecSAlex Elder *
49a646d6ecSAlex Elder * @response: response code (microcontroller->AP)
50e3eea08eSAlex Elder * @reserved1: reserved bytes; avoid reading or writing
51a646d6ecSAlex Elder * @response_param: response parameter (microcontroller->AP)
52a646d6ecSAlex Elder *
53a646d6ecSAlex Elder * @event: event code (microcontroller->AP)
54e3eea08eSAlex Elder * @reserved2: reserved bytes; avoid reading or writing
55a646d6ecSAlex Elder * @event_param: event parameter (microcontroller->AP)
56a646d6ecSAlex Elder *
57a646d6ecSAlex Elder * @first_error_address: address of first error-source on SNOC
58a646d6ecSAlex Elder * @hw_state: state of hardware (including error type information)
59a646d6ecSAlex Elder * @warning_counter: counter of non-fatal hardware errors
60e3eea08eSAlex Elder * @reserved3: reserved bytes; avoid reading or writing
61a646d6ecSAlex Elder * @interface_version: hardware-reported interface version
62e3eea08eSAlex Elder * @reserved4: reserved bytes; avoid reading or writing
63722208eaSAlex Elder *
64722208eaSAlex Elder * A shared memory area at the base of IPA resident memory is used for
65722208eaSAlex Elder * communication with the microcontroller. The region is 128 bytes in
66722208eaSAlex Elder * size, but only the first 40 bytes (structured this way) are used.
67a646d6ecSAlex Elder */
68a646d6ecSAlex Elder struct ipa_uc_mem_area {
69a646d6ecSAlex Elder u8 command; /* enum ipa_uc_command */
70a646d6ecSAlex Elder u8 reserved0[3];
71a646d6ecSAlex Elder __le32 command_param;
72a646d6ecSAlex Elder __le32 command_param_hi;
73a646d6ecSAlex Elder u8 response; /* enum ipa_uc_response */
74a646d6ecSAlex Elder u8 reserved1[3];
75a646d6ecSAlex Elder __le32 response_param;
76a646d6ecSAlex Elder u8 event; /* enum ipa_uc_event */
77a646d6ecSAlex Elder u8 reserved2[3];
78a646d6ecSAlex Elder
79a646d6ecSAlex Elder __le32 event_param;
80a646d6ecSAlex Elder __le32 first_error_address;
81a646d6ecSAlex Elder u8 hw_state;
82a646d6ecSAlex Elder u8 warning_counter;
83a646d6ecSAlex Elder __le16 reserved3;
84a646d6ecSAlex Elder __le16 interface_version;
85a646d6ecSAlex Elder __le16 reserved4;
86a646d6ecSAlex Elder };
87a646d6ecSAlex Elder
88a646d6ecSAlex Elder /** enum ipa_uc_command - commands from the AP to the microcontroller */
89a646d6ecSAlex Elder enum ipa_uc_command {
908701cb00SAlex Elder IPA_UC_COMMAND_NO_OP = 0x0,
918701cb00SAlex Elder IPA_UC_COMMAND_UPDATE_FLAGS = 0x1,
928701cb00SAlex Elder IPA_UC_COMMAND_DEBUG_RUN_TEST = 0x2,
938701cb00SAlex Elder IPA_UC_COMMAND_DEBUG_GET_INFO = 0x3,
948701cb00SAlex Elder IPA_UC_COMMAND_ERR_FATAL = 0x4,
958701cb00SAlex Elder IPA_UC_COMMAND_CLK_GATE = 0x5,
968701cb00SAlex Elder IPA_UC_COMMAND_CLK_UNGATE = 0x6,
978701cb00SAlex Elder IPA_UC_COMMAND_MEMCPY = 0x7,
988701cb00SAlex Elder IPA_UC_COMMAND_RESET_PIPE = 0x8,
998701cb00SAlex Elder IPA_UC_COMMAND_REG_WRITE = 0x9,
1008701cb00SAlex Elder IPA_UC_COMMAND_GSI_CH_EMPTY = 0xa,
101a646d6ecSAlex Elder };
102a646d6ecSAlex Elder
103a646d6ecSAlex Elder /** enum ipa_uc_response - microcontroller response codes */
104a646d6ecSAlex Elder enum ipa_uc_response {
1058701cb00SAlex Elder IPA_UC_RESPONSE_NO_OP = 0x0,
1068701cb00SAlex Elder IPA_UC_RESPONSE_INIT_COMPLETED = 0x1,
1078701cb00SAlex Elder IPA_UC_RESPONSE_CMD_COMPLETED = 0x2,
1088701cb00SAlex Elder IPA_UC_RESPONSE_DEBUG_GET_INFO = 0x3,
109a646d6ecSAlex Elder };
110a646d6ecSAlex Elder
111a646d6ecSAlex Elder /** enum ipa_uc_event - common cpu events reported by the microcontroller */
112a646d6ecSAlex Elder enum ipa_uc_event {
1138701cb00SAlex Elder IPA_UC_EVENT_NO_OP = 0x0,
1148701cb00SAlex Elder IPA_UC_EVENT_ERROR = 0x1,
1158701cb00SAlex Elder IPA_UC_EVENT_LOG_INFO = 0x2,
116a646d6ecSAlex Elder };
117a646d6ecSAlex Elder
ipa_uc_shared(struct ipa * ipa)118a646d6ecSAlex Elder static struct ipa_uc_mem_area *ipa_uc_shared(struct ipa *ipa)
119a646d6ecSAlex Elder {
1205e3bc1e5SAlex Elder const struct ipa_mem *mem = ipa_mem_find(ipa, IPA_MEM_UC_SHARED);
1215e3bc1e5SAlex Elder u32 offset = ipa->mem_offset + mem->offset;
122a646d6ecSAlex Elder
123a646d6ecSAlex Elder return ipa->mem_virt + offset;
124a646d6ecSAlex Elder }
125a646d6ecSAlex Elder
126a646d6ecSAlex Elder /* Microcontroller event IPA interrupt handler */
ipa_uc_event_handler(struct ipa * ipa)127e5709b7cSAlex Elder static void ipa_uc_event_handler(struct ipa *ipa)
128a646d6ecSAlex Elder {
129a646d6ecSAlex Elder struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa);
130a646d6ecSAlex Elder struct device *dev = &ipa->pdev->dev;
131a646d6ecSAlex Elder
132a646d6ecSAlex Elder if (shared->event == IPA_UC_EVENT_ERROR)
133a646d6ecSAlex Elder dev_err(dev, "microcontroller error event\n");
1340a5096ecSAlex Elder else if (shared->event != IPA_UC_EVENT_LOG_INFO)
135e2f154e6SAlex Elder dev_err(dev, "unsupported microcontroller event %u\n",
136a646d6ecSAlex Elder shared->event);
1370a5096ecSAlex Elder /* The LOG_INFO event can be safely ignored */
138a646d6ecSAlex Elder }
139a646d6ecSAlex Elder
140a646d6ecSAlex Elder /* Microcontroller response IPA interrupt handler */
ipa_uc_response_hdlr(struct ipa * ipa)141e5709b7cSAlex Elder static void ipa_uc_response_hdlr(struct ipa *ipa)
142a646d6ecSAlex Elder {
143a646d6ecSAlex Elder struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa);
144e2f154e6SAlex Elder struct device *dev = &ipa->pdev->dev;
145a646d6ecSAlex Elder
146a646d6ecSAlex Elder /* An INIT_COMPLETED response message is sent to the AP by the
147a646d6ecSAlex Elder * microcontroller when it is operational. Other than this, the AP
148a646d6ecSAlex Elder * should only receive responses from the microcontroller when it has
149a646d6ecSAlex Elder * sent it a request message.
150a646d6ecSAlex Elder *
1517aa0e8b8SAlex Elder * We can drop the power reference taken in ipa_uc_power() once we
152a646d6ecSAlex Elder * know the microcontroller has finished its initialization.
153a646d6ecSAlex Elder */
154a646d6ecSAlex Elder switch (shared->response) {
155a646d6ecSAlex Elder case IPA_UC_RESPONSE_INIT_COMPLETED:
1567aa0e8b8SAlex Elder if (ipa->uc_powered) {
157a646d6ecSAlex Elder ipa->uc_loaded = true;
15834a08176SAlex Elder ipa_power_retention(ipa, true);
1591aac309dSAlex Elder pm_runtime_mark_last_busy(dev);
1601aac309dSAlex Elder (void)pm_runtime_put_autosuspend(dev);
1617aa0e8b8SAlex Elder ipa->uc_powered = false;
162e2f154e6SAlex Elder } else {
163e2f154e6SAlex Elder dev_warn(dev, "unexpected init_completed response\n");
164e2f154e6SAlex Elder }
165a646d6ecSAlex Elder break;
166a646d6ecSAlex Elder default:
167e2f154e6SAlex Elder dev_warn(dev, "unsupported microcontroller response %u\n",
168a646d6ecSAlex Elder shared->response);
169a646d6ecSAlex Elder break;
170a646d6ecSAlex Elder }
171a646d6ecSAlex Elder }
172a646d6ecSAlex Elder
ipa_uc_interrupt_handler(struct ipa * ipa,enum ipa_irq_id irq_id)173482ae3a9SAlex Elder void ipa_uc_interrupt_handler(struct ipa *ipa, enum ipa_irq_id irq_id)
174e5709b7cSAlex Elder {
175e5709b7cSAlex Elder /* Silently ignore anything unrecognized */
176e5709b7cSAlex Elder if (irq_id == IPA_IRQ_UC_0)
177e5709b7cSAlex Elder ipa_uc_event_handler(ipa);
178e5709b7cSAlex Elder else if (irq_id == IPA_IRQ_UC_1)
179e5709b7cSAlex Elder ipa_uc_response_hdlr(ipa);
180e5709b7cSAlex Elder }
181e5709b7cSAlex Elder
182dc8f7e39SAlex Elder /* Configure the IPA microcontroller subsystem */
ipa_uc_config(struct ipa * ipa)183dc8f7e39SAlex Elder void ipa_uc_config(struct ipa *ipa)
184a646d6ecSAlex Elder {
1857aa0e8b8SAlex Elder ipa->uc_powered = false;
186a646d6ecSAlex Elder ipa->uc_loaded = false;
187d50ed355SAlex Elder ipa_interrupt_enable(ipa, IPA_IRQ_UC_0);
188d50ed355SAlex Elder ipa_interrupt_enable(ipa, IPA_IRQ_UC_1);
189a646d6ecSAlex Elder }
190a646d6ecSAlex Elder
191dc8f7e39SAlex Elder /* Inverse of ipa_uc_config() */
ipa_uc_deconfig(struct ipa * ipa)192dc8f7e39SAlex Elder void ipa_uc_deconfig(struct ipa *ipa)
193a646d6ecSAlex Elder {
1941aac309dSAlex Elder struct device *dev = &ipa->pdev->dev;
1951aac309dSAlex Elder
196d50ed355SAlex Elder ipa_interrupt_disable(ipa, IPA_IRQ_UC_1);
197d50ed355SAlex Elder ipa_interrupt_disable(ipa, IPA_IRQ_UC_0);
19834a08176SAlex Elder if (ipa->uc_loaded)
19934a08176SAlex Elder ipa_power_retention(ipa, false);
20034a08176SAlex Elder
2017aa0e8b8SAlex Elder if (!ipa->uc_powered)
2021aac309dSAlex Elder return;
2031aac309dSAlex Elder
2041aac309dSAlex Elder pm_runtime_mark_last_busy(dev);
2051aac309dSAlex Elder (void)pm_runtime_put_autosuspend(dev);
206a646d6ecSAlex Elder }
207a646d6ecSAlex Elder
2087aa0e8b8SAlex Elder /* Take a proxy power reference for the microcontroller */
ipa_uc_power(struct ipa * ipa)2097aa0e8b8SAlex Elder void ipa_uc_power(struct ipa *ipa)
210e2f154e6SAlex Elder {
211e2f154e6SAlex Elder static bool already;
212799c5c24SAlex Elder struct device *dev;
2137ebd168cSAlex Elder int ret;
214e2f154e6SAlex Elder
215e2f154e6SAlex Elder if (already)
216e2f154e6SAlex Elder return;
217e2f154e6SAlex Elder already = true; /* Only do this on first boot */
218e2f154e6SAlex Elder
219799c5c24SAlex Elder /* This power reference dropped in ipa_uc_response_hdlr() above */
220799c5c24SAlex Elder dev = &ipa->pdev->dev;
221799c5c24SAlex Elder ret = pm_runtime_get_sync(dev);
222799c5c24SAlex Elder if (ret < 0) {
223799c5c24SAlex Elder pm_runtime_put_noidle(dev);
224799c5c24SAlex Elder dev_err(dev, "error %d getting proxy power\n", ret);
225799c5c24SAlex Elder } else {
2267aa0e8b8SAlex Elder ipa->uc_powered = true;
227799c5c24SAlex Elder }
228e2f154e6SAlex Elder }
229e2f154e6SAlex Elder
230a646d6ecSAlex Elder /* Send a command to the microcontroller */
send_uc_command(struct ipa * ipa,u32 command,u32 command_param)231a646d6ecSAlex Elder static void send_uc_command(struct ipa *ipa, u32 command, u32 command_param)
232a646d6ecSAlex Elder {
233a646d6ecSAlex Elder struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa);
23481772e44SAlex Elder const struct reg *reg;
235716a115bSAlex Elder u32 val;
236a646d6ecSAlex Elder
237716a115bSAlex Elder /* Fill in the command data */
238a646d6ecSAlex Elder shared->command = command;
239a646d6ecSAlex Elder shared->command_param = cpu_to_le32(command_param);
240a646d6ecSAlex Elder shared->command_param_hi = 0;
241a646d6ecSAlex Elder shared->response = 0;
242a646d6ecSAlex Elder shared->response_param = 0;
243a646d6ecSAlex Elder
244716a115bSAlex Elder /* Use an interrupt to tell the microcontroller the command is ready */
2456a244b75SAlex Elder reg = ipa_reg(ipa, IPA_IRQ_UC);
246*f1470fd7SAlex Elder val = reg_bit(reg, UC_INTR);
2476a244b75SAlex Elder
248fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_offset(reg));
249a646d6ecSAlex Elder }
250a646d6ecSAlex Elder
251a646d6ecSAlex Elder /* Tell the microcontroller the AP is shutting down */
ipa_uc_panic_notifier(struct ipa * ipa)252a646d6ecSAlex Elder void ipa_uc_panic_notifier(struct ipa *ipa)
253a646d6ecSAlex Elder {
254a646d6ecSAlex Elder if (!ipa->uc_loaded)
255a646d6ecSAlex Elder return;
256a646d6ecSAlex Elder
257a646d6ecSAlex Elder send_uc_command(ipa, IPA_UC_COMMAND_ERR_FATAL, 0);
258a646d6ecSAlex Elder
259a646d6ecSAlex Elder /* give uc enough time to save state */
260a646d6ecSAlex Elder udelay(IPA_SEND_DELAY);
261a646d6ecSAlex Elder }
262