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 * @command_param: low 32 bits of command parameter (AP->microcontroller) 45 * @command_param_hi: high 32 bits of command parameter (AP->microcontroller) 46 * 47 * @response: response code (microcontroller->AP) 48 * @response_param: response parameter (microcontroller->AP) 49 * 50 * @event: event code (microcontroller->AP) 51 * @event_param: event parameter (microcontroller->AP) 52 * 53 * @first_error_address: address of first error-source on SNOC 54 * @hw_state: state of hardware (including error type information) 55 * @warning_counter: counter of non-fatal hardware errors 56 * @interface_version: hardware-reported interface version 57 * 58 * A shared memory area at the base of IPA resident memory is used for 59 * communication with the microcontroller. The region is 128 bytes in 60 * size, but only the first 40 bytes (structured this way) are used. 61 */ 62 struct ipa_uc_mem_area { 63 u8 command; /* enum ipa_uc_command */ 64 u8 reserved0[3]; 65 __le32 command_param; 66 __le32 command_param_hi; 67 u8 response; /* enum ipa_uc_response */ 68 u8 reserved1[3]; 69 __le32 response_param; 70 u8 event; /* enum ipa_uc_event */ 71 u8 reserved2[3]; 72 73 __le32 event_param; 74 __le32 first_error_address; 75 u8 hw_state; 76 u8 warning_counter; 77 __le16 reserved3; 78 __le16 interface_version; 79 __le16 reserved4; 80 }; 81 82 /** enum ipa_uc_command - commands from the AP to the microcontroller */ 83 enum ipa_uc_command { 84 IPA_UC_COMMAND_NO_OP = 0, 85 IPA_UC_COMMAND_UPDATE_FLAGS = 1, 86 IPA_UC_COMMAND_DEBUG_RUN_TEST = 2, 87 IPA_UC_COMMAND_DEBUG_GET_INFO = 3, 88 IPA_UC_COMMAND_ERR_FATAL = 4, 89 IPA_UC_COMMAND_CLK_GATE = 5, 90 IPA_UC_COMMAND_CLK_UNGATE = 6, 91 IPA_UC_COMMAND_MEMCPY = 7, 92 IPA_UC_COMMAND_RESET_PIPE = 8, 93 IPA_UC_COMMAND_REG_WRITE = 9, 94 IPA_UC_COMMAND_GSI_CH_EMPTY = 10, 95 }; 96 97 /** enum ipa_uc_response - microcontroller response codes */ 98 enum ipa_uc_response { 99 IPA_UC_RESPONSE_NO_OP = 0, 100 IPA_UC_RESPONSE_INIT_COMPLETED = 1, 101 IPA_UC_RESPONSE_CMD_COMPLETED = 2, 102 IPA_UC_RESPONSE_DEBUG_GET_INFO = 3, 103 }; 104 105 /** enum ipa_uc_event - common cpu events reported by the microcontroller */ 106 enum ipa_uc_event { 107 IPA_UC_EVENT_NO_OP = 0, 108 IPA_UC_EVENT_ERROR = 1, 109 IPA_UC_EVENT_LOG_INFO = 2, 110 }; 111 112 static struct ipa_uc_mem_area *ipa_uc_shared(struct ipa *ipa) 113 { 114 u32 offset = ipa->mem_offset + ipa->mem[IPA_MEM_UC_SHARED].offset; 115 116 return ipa->mem_virt + offset; 117 } 118 119 /* Microcontroller event IPA interrupt handler */ 120 static void ipa_uc_event_handler(struct ipa *ipa, enum ipa_irq_id irq_id) 121 { 122 struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa); 123 struct device *dev = &ipa->pdev->dev; 124 125 if (shared->event == IPA_UC_EVENT_ERROR) 126 dev_err(dev, "microcontroller error event\n"); 127 else 128 dev_err(dev, "unsupported microcontroller event %hhu\n", 129 shared->event); 130 } 131 132 /* Microcontroller response IPA interrupt handler */ 133 static void ipa_uc_response_hdlr(struct ipa *ipa, enum ipa_irq_id irq_id) 134 { 135 struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa); 136 137 /* An INIT_COMPLETED response message is sent to the AP by the 138 * microcontroller when it is operational. Other than this, the AP 139 * should only receive responses from the microcontroller when it has 140 * sent it a request message. 141 * 142 * We can drop the clock reference taken in ipa_uc_init() once we 143 * know the microcontroller has finished its initialization. 144 */ 145 switch (shared->response) { 146 case IPA_UC_RESPONSE_INIT_COMPLETED: 147 ipa->uc_loaded = true; 148 ipa_clock_put(ipa); 149 break; 150 default: 151 dev_warn(&ipa->pdev->dev, 152 "unsupported microcontroller response %hhu\n", 153 shared->response); 154 break; 155 } 156 } 157 158 /* ipa_uc_setup() - Set up the microcontroller */ 159 void ipa_uc_setup(struct ipa *ipa) 160 { 161 /* The microcontroller needs the IPA clock running until it has 162 * completed its initialization. It signals this by sending an 163 * INIT_COMPLETED response message to the AP. This could occur after 164 * we have finished doing the rest of the IPA initialization, so we 165 * need to take an extra "proxy" reference, and hold it until we've 166 * received that signal. (This reference is dropped in 167 * ipa_uc_response_hdlr(), above.) 168 */ 169 ipa_clock_get(ipa); 170 171 ipa->uc_loaded = false; 172 ipa_interrupt_add(ipa->interrupt, IPA_IRQ_UC_0, ipa_uc_event_handler); 173 ipa_interrupt_add(ipa->interrupt, IPA_IRQ_UC_1, ipa_uc_response_hdlr); 174 } 175 176 /* Inverse of ipa_uc_setup() */ 177 void ipa_uc_teardown(struct ipa *ipa) 178 { 179 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_UC_1); 180 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_UC_0); 181 if (!ipa->uc_loaded) 182 ipa_clock_put(ipa); 183 } 184 185 /* Send a command to the microcontroller */ 186 static void send_uc_command(struct ipa *ipa, u32 command, u32 command_param) 187 { 188 struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa); 189 190 shared->command = command; 191 shared->command_param = cpu_to_le32(command_param); 192 shared->command_param_hi = 0; 193 shared->response = 0; 194 shared->response_param = 0; 195 196 iowrite32(1, ipa->reg_virt + IPA_REG_IRQ_UC_OFFSET); 197 } 198 199 /* Tell the microcontroller the AP is shutting down */ 200 void ipa_uc_panic_notifier(struct ipa *ipa) 201 { 202 if (!ipa->uc_loaded) 203 return; 204 205 send_uc_command(ipa, IPA_UC_COMMAND_ERR_FATAL, 0); 206 207 /* give uc enough time to save state */ 208 udelay(IPA_SEND_DELAY); 209 } 210