xref: /openbmc/linux/drivers/net/ipa/ipa_uc.c (revision c4f7ac64)
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>
10 
11 #include "ipa.h"
12 #include "ipa_clock.h"
13 #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 
38 /* 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)
44  * @reserved0:		reserved bytes; avoid reading or writing
45  * @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)
49  * @reserved1:		reserved bytes; avoid reading or writing
50  * @response_param:	response parameter (microcontroller->AP)
51  *
52  * @event:		event code (microcontroller->AP)
53  * @reserved2:		reserved bytes; avoid reading or writing
54  * @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
59  * @reserved3:		reserved bytes; avoid reading or writing
60  * @interface_version:	hardware-reported interface version
61  * @reserved4:		reserved bytes; avoid reading or writing
62  *
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.
66  */
67 struct 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 */
88 enum ipa_uc_command {
89 	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,
100 };
101 
102 /** enum ipa_uc_response - microcontroller response codes */
103 enum ipa_uc_response {
104 	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,
108 };
109 
110 /** enum ipa_uc_event - common cpu events reported by the microcontroller */
111 enum ipa_uc_event {
112 	IPA_UC_EVENT_NO_OP		= 0x0,
113 	IPA_UC_EVENT_ERROR		= 0x1,
114 	IPA_UC_EVENT_LOG_INFO		= 0x2,
115 };
116 
117 static struct ipa_uc_mem_area *ipa_uc_shared(struct ipa *ipa)
118 {
119 	u32 offset = ipa->mem_offset + ipa->mem[IPA_MEM_UC_SHARED].offset;
120 
121 	return ipa->mem_virt + offset;
122 }
123 
124 /* Microcontroller event IPA interrupt handler */
125 static void ipa_uc_event_handler(struct ipa *ipa, enum ipa_irq_id irq_id)
126 {
127 	struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa);
128 	struct device *dev = &ipa->pdev->dev;
129 
130 	if (shared->event == IPA_UC_EVENT_ERROR)
131 		dev_err(dev, "microcontroller error event\n");
132 	else if (shared->event != IPA_UC_EVENT_LOG_INFO)
133 		dev_err(dev, "unsupported microcontroller event %hhu\n",
134 			shared->event);
135 	/* The LOG_INFO event can be safely ignored */
136 }
137 
138 /* Microcontroller response IPA interrupt handler */
139 static void ipa_uc_response_hdlr(struct ipa *ipa, enum ipa_irq_id irq_id)
140 {
141 	struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa);
142 
143 	/* An INIT_COMPLETED response message is sent to the AP by the
144 	 * microcontroller when it is operational.  Other than this, the AP
145 	 * should only receive responses from the microcontroller when it has
146 	 * sent it a request message.
147 	 *
148 	 * We can drop the clock reference taken in ipa_uc_setup() once we
149 	 * know the microcontroller has finished its initialization.
150 	 */
151 	switch (shared->response) {
152 	case IPA_UC_RESPONSE_INIT_COMPLETED:
153 		ipa->uc_loaded = true;
154 		ipa_clock_put(ipa);
155 		break;
156 	default:
157 		dev_warn(&ipa->pdev->dev,
158 			 "unsupported microcontroller response %hhu\n",
159 			 shared->response);
160 		break;
161 	}
162 }
163 
164 /* ipa_uc_setup() - Set up the microcontroller */
165 void ipa_uc_setup(struct ipa *ipa)
166 {
167 	/* The microcontroller needs the IPA clock running until it has
168 	 * completed its initialization.  It signals this by sending an
169 	 * INIT_COMPLETED response message to the AP.  This could occur after
170 	 * we have finished doing the rest of the IPA initialization, so we
171 	 * need to take an extra "proxy" reference, and hold it until we've
172 	 * received that signal.  (This reference is dropped in
173 	 * ipa_uc_response_hdlr(), above.)
174 	 */
175 	ipa_clock_get(ipa);
176 
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 
182 /* Inverse of ipa_uc_setup() */
183 void ipa_uc_teardown(struct ipa *ipa)
184 {
185 	ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_UC_1);
186 	ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_UC_0);
187 	if (!ipa->uc_loaded)
188 		ipa_clock_put(ipa);
189 }
190 
191 /* Send a command to the microcontroller */
192 static void send_uc_command(struct ipa *ipa, u32 command, u32 command_param)
193 {
194 	struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa);
195 	u32 offset;
196 	u32 val;
197 
198 	/* Fill in the command data */
199 	shared->command = command;
200 	shared->command_param = cpu_to_le32(command_param);
201 	shared->command_param_hi = 0;
202 	shared->response = 0;
203 	shared->response_param = 0;
204 
205 	/* Use an interrupt to tell the microcontroller the command is ready */
206 	val = u32_encode_bits(1, UC_INTR_FMASK);
207 	offset = ipa_reg_irq_uc_offset(ipa->version);
208 	iowrite32(val, ipa->reg_virt + offset);
209 }
210 
211 /* Tell the microcontroller the AP is shutting down */
212 void ipa_uc_panic_notifier(struct ipa *ipa)
213 {
214 	if (!ipa->uc_loaded)
215 		return;
216 
217 	send_uc_command(ipa, IPA_UC_COMMAND_ERR_FATAL, 0);
218 
219 	/* give uc enough time to save state */
220 	udelay(IPA_SEND_DELAY);
221 }
222