1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2020 Linaro Ltd.
7 #include <linux/types.h>
9 #include <linux/delay.h>
12 #include "ipa_clock.h"
16 * DOC: The IPA embedded microcontroller
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.
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.
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.
36 /* Supports hardware interface version 0x2000 */
38 /* Offset relative to the base of the IPA shared address space of the
39 * shared region used for communication with the microcontroller. The
40 * region is 128 bytes in size, but only the first 40 bytes are used.
42 #define IPA_MEM_UC_OFFSET 0x0000
44 /* Delay to allow a the microcontroller to save state when crashing */
45 #define IPA_SEND_DELAY 100 /* microseconds */
48 * struct ipa_uc_mem_area - AP/microcontroller shared memory area
49 * @command: command code (AP->microcontroller)
50 * @command_param: low 32 bits of command parameter (AP->microcontroller)
51 * @command_param_hi: high 32 bits of command parameter (AP->microcontroller)
53 * @response: response code (microcontroller->AP)
54 * @response_param: response parameter (microcontroller->AP)
56 * @event: event code (microcontroller->AP)
57 * @event_param: event parameter (microcontroller->AP)
59 * @first_error_address: address of first error-source on SNOC
60 * @hw_state: state of hardware (including error type information)
61 * @warning_counter: counter of non-fatal hardware errors
62 * @interface_version: hardware-reported interface version
64 struct ipa_uc_mem_area
{
65 u8 command
; /* enum ipa_uc_command */
68 __le32 command_param_hi
;
69 u8 response
; /* enum ipa_uc_response */
71 __le32 response_param
;
72 u8 event
; /* enum ipa_uc_event */
76 __le32 first_error_address
;
80 __le16 interface_version
;
84 /** enum ipa_uc_command - commands from the AP to the microcontroller */
86 IPA_UC_COMMAND_NO_OP
= 0,
87 IPA_UC_COMMAND_UPDATE_FLAGS
= 1,
88 IPA_UC_COMMAND_DEBUG_RUN_TEST
= 2,
89 IPA_UC_COMMAND_DEBUG_GET_INFO
= 3,
90 IPA_UC_COMMAND_ERR_FATAL
= 4,
91 IPA_UC_COMMAND_CLK_GATE
= 5,
92 IPA_UC_COMMAND_CLK_UNGATE
= 6,
93 IPA_UC_COMMAND_MEMCPY
= 7,
94 IPA_UC_COMMAND_RESET_PIPE
= 8,
95 IPA_UC_COMMAND_REG_WRITE
= 9,
96 IPA_UC_COMMAND_GSI_CH_EMPTY
= 10,
99 /** enum ipa_uc_response - microcontroller response codes */
100 enum ipa_uc_response
{
101 IPA_UC_RESPONSE_NO_OP
= 0,
102 IPA_UC_RESPONSE_INIT_COMPLETED
= 1,
103 IPA_UC_RESPONSE_CMD_COMPLETED
= 2,
104 IPA_UC_RESPONSE_DEBUG_GET_INFO
= 3,
107 /** enum ipa_uc_event - common cpu events reported by the microcontroller */
109 IPA_UC_EVENT_NO_OP
= 0,
110 IPA_UC_EVENT_ERROR
= 1,
111 IPA_UC_EVENT_LOG_INFO
= 2,
114 static struct ipa_uc_mem_area
*ipa_uc_shared(struct ipa
*ipa
)
116 u32 offset
= ipa
->mem_offset
+ ipa
->mem
[IPA_MEM_UC_SHARED
].offset
;
118 return ipa
->mem_virt
+ offset
;
121 /* Microcontroller event IPA interrupt handler */
122 static void ipa_uc_event_handler(struct ipa
*ipa
, enum ipa_irq_id irq_id
)
124 struct ipa_uc_mem_area
*shared
= ipa_uc_shared(ipa
);
125 struct device
*dev
= &ipa
->pdev
->dev
;
127 if (shared
->event
== IPA_UC_EVENT_ERROR
)
128 dev_err(dev
, "microcontroller error event\n");
130 dev_err(dev
, "unsupported microcontroller event %hhu\n",
134 /* Microcontroller response IPA interrupt handler */
135 static void ipa_uc_response_hdlr(struct ipa
*ipa
, enum ipa_irq_id irq_id
)
137 struct ipa_uc_mem_area
*shared
= ipa_uc_shared(ipa
);
139 /* An INIT_COMPLETED response message is sent to the AP by the
140 * microcontroller when it is operational. Other than this, the AP
141 * should only receive responses from the microcontroller when it has
142 * sent it a request message.
144 * We can drop the clock reference taken in ipa_uc_init() once we
145 * know the microcontroller has finished its initialization.
147 switch (shared
->response
) {
148 case IPA_UC_RESPONSE_INIT_COMPLETED
:
149 ipa
->uc_loaded
= true;
153 dev_warn(&ipa
->pdev
->dev
,
154 "unsupported microcontroller response %hhu\n",
160 /* ipa_uc_setup() - Set up the microcontroller */
161 void ipa_uc_setup(struct ipa
*ipa
)
163 /* The microcontroller needs the IPA clock running until it has
164 * completed its initialization. It signals this by sending an
165 * INIT_COMPLETED response message to the AP. This could occur after
166 * we have finished doing the rest of the IPA initialization, so we
167 * need to take an extra "proxy" reference, and hold it until we've
168 * received that signal. (This reference is dropped in
169 * ipa_uc_response_hdlr(), above.)
173 ipa
->uc_loaded
= false;
174 ipa_interrupt_add(ipa
->interrupt
, IPA_IRQ_UC_0
, ipa_uc_event_handler
);
175 ipa_interrupt_add(ipa
->interrupt
, IPA_IRQ_UC_1
, ipa_uc_response_hdlr
);
178 /* Inverse of ipa_uc_setup() */
179 void ipa_uc_teardown(struct ipa
*ipa
)
181 ipa_interrupt_remove(ipa
->interrupt
, IPA_IRQ_UC_1
);
182 ipa_interrupt_remove(ipa
->interrupt
, IPA_IRQ_UC_0
);
187 /* Send a command to the microcontroller */
188 static void send_uc_command(struct ipa
*ipa
, u32 command
, u32 command_param
)
190 struct ipa_uc_mem_area
*shared
= ipa_uc_shared(ipa
);
192 shared
->command
= command
;
193 shared
->command_param
= cpu_to_le32(command_param
);
194 shared
->command_param_hi
= 0;
195 shared
->response
= 0;
196 shared
->response_param
= 0;
198 iowrite32(1, ipa
->reg_virt
+ IPA_REG_IRQ_UC_OFFSET
);
201 /* Tell the microcontroller the AP is shutting down */
202 void ipa_uc_panic_notifier(struct ipa
*ipa
)
207 send_uc_command(ipa
, IPA_UC_COMMAND_ERR_FATAL
, 0);
209 /* give uc enough time to save state */
210 udelay(IPA_SEND_DELAY
);