1a646d6ecSAlex Elder // SPDX-License-Identifier: GPL-2.0 2a646d6ecSAlex Elder 3a646d6ecSAlex Elder /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved. 4a646d6ecSAlex Elder * Copyright (C) 2018-2020 Linaro Ltd. 5a646d6ecSAlex Elder */ 6a646d6ecSAlex Elder 7a646d6ecSAlex Elder #include <linux/types.h> 8a646d6ecSAlex Elder #include <linux/io.h> 9a646d6ecSAlex Elder #include <linux/delay.h> 10*799c5c24SAlex Elder #include <linux/pm_runtime.h> 11a646d6ecSAlex Elder 12a646d6ecSAlex Elder #include "ipa.h" 13a646d6ecSAlex Elder #include "ipa_uc.h" 14a646d6ecSAlex Elder 15a646d6ecSAlex Elder /** 16a646d6ecSAlex Elder * DOC: The IPA embedded microcontroller 17a646d6ecSAlex Elder * 18a646d6ecSAlex Elder * The IPA incorporates a microcontroller that is able to do some additional 19a646d6ecSAlex Elder * handling/offloading of network activity. The current code makes 20a646d6ecSAlex Elder * essentially no use of the microcontroller, but it still requires some 21a646d6ecSAlex Elder * initialization. It needs to be notified in the event the AP crashes. 22a646d6ecSAlex Elder * 23a646d6ecSAlex Elder * The microcontroller can generate two interrupts to the AP. One interrupt 24a646d6ecSAlex Elder * is used to indicate that a response to a request from the AP is available. 25a646d6ecSAlex Elder * The other is used to notify the AP of the occurrence of an event. In 26a646d6ecSAlex Elder * addition, the AP can interrupt the microcontroller by writing a register. 27a646d6ecSAlex Elder * 28a646d6ecSAlex Elder * A 128 byte block of structured memory within the IPA SRAM is used together 29a646d6ecSAlex Elder * with these interrupts to implement the communication interface between the 30a646d6ecSAlex Elder * AP and the IPA microcontroller. Each side writes data to the shared area 31a646d6ecSAlex Elder * before interrupting its peer, which will read the written data in response 32a646d6ecSAlex Elder * to the interrupt. Some information found in the shared area is currently 33a646d6ecSAlex Elder * unused. All remaining space in the shared area is reserved, and must not 34a646d6ecSAlex Elder * be read or written by the AP. 35a646d6ecSAlex Elder */ 36a646d6ecSAlex Elder /* Supports hardware interface version 0x2000 */ 37a646d6ecSAlex Elder 38a646d6ecSAlex Elder /* Delay to allow a the microcontroller to save state when crashing */ 39a646d6ecSAlex Elder #define IPA_SEND_DELAY 100 /* microseconds */ 40a646d6ecSAlex Elder 41a646d6ecSAlex Elder /** 42a646d6ecSAlex Elder * struct ipa_uc_mem_area - AP/microcontroller shared memory area 43a646d6ecSAlex Elder * @command: command code (AP->microcontroller) 44e3eea08eSAlex Elder * @reserved0: reserved bytes; avoid reading or writing 45a646d6ecSAlex Elder * @command_param: low 32 bits of command parameter (AP->microcontroller) 46a646d6ecSAlex Elder * @command_param_hi: high 32 bits of command parameter (AP->microcontroller) 47a646d6ecSAlex Elder * 48a646d6ecSAlex Elder * @response: response code (microcontroller->AP) 49e3eea08eSAlex Elder * @reserved1: reserved bytes; avoid reading or writing 50a646d6ecSAlex Elder * @response_param: response parameter (microcontroller->AP) 51a646d6ecSAlex Elder * 52a646d6ecSAlex Elder * @event: event code (microcontroller->AP) 53e3eea08eSAlex Elder * @reserved2: reserved bytes; avoid reading or writing 54a646d6ecSAlex Elder * @event_param: event parameter (microcontroller->AP) 55a646d6ecSAlex Elder * 56a646d6ecSAlex Elder * @first_error_address: address of first error-source on SNOC 57a646d6ecSAlex Elder * @hw_state: state of hardware (including error type information) 58a646d6ecSAlex Elder * @warning_counter: counter of non-fatal hardware errors 59e3eea08eSAlex Elder * @reserved3: reserved bytes; avoid reading or writing 60a646d6ecSAlex Elder * @interface_version: hardware-reported interface version 61e3eea08eSAlex Elder * @reserved4: reserved bytes; avoid reading or writing 62722208eaSAlex Elder * 63722208eaSAlex Elder * A shared memory area at the base of IPA resident memory is used for 64722208eaSAlex Elder * communication with the microcontroller. The region is 128 bytes in 65722208eaSAlex Elder * size, but only the first 40 bytes (structured this way) are used. 66a646d6ecSAlex Elder */ 67a646d6ecSAlex Elder struct ipa_uc_mem_area { 68a646d6ecSAlex Elder u8 command; /* enum ipa_uc_command */ 69a646d6ecSAlex Elder u8 reserved0[3]; 70a646d6ecSAlex Elder __le32 command_param; 71a646d6ecSAlex Elder __le32 command_param_hi; 72a646d6ecSAlex Elder u8 response; /* enum ipa_uc_response */ 73a646d6ecSAlex Elder u8 reserved1[3]; 74a646d6ecSAlex Elder __le32 response_param; 75a646d6ecSAlex Elder u8 event; /* enum ipa_uc_event */ 76a646d6ecSAlex Elder u8 reserved2[3]; 77a646d6ecSAlex Elder 78a646d6ecSAlex Elder __le32 event_param; 79a646d6ecSAlex Elder __le32 first_error_address; 80a646d6ecSAlex Elder u8 hw_state; 81a646d6ecSAlex Elder u8 warning_counter; 82a646d6ecSAlex Elder __le16 reserved3; 83a646d6ecSAlex Elder __le16 interface_version; 84a646d6ecSAlex Elder __le16 reserved4; 85a646d6ecSAlex Elder }; 86a646d6ecSAlex Elder 87a646d6ecSAlex Elder /** enum ipa_uc_command - commands from the AP to the microcontroller */ 88a646d6ecSAlex Elder enum ipa_uc_command { 898701cb00SAlex Elder IPA_UC_COMMAND_NO_OP = 0x0, 908701cb00SAlex Elder IPA_UC_COMMAND_UPDATE_FLAGS = 0x1, 918701cb00SAlex Elder IPA_UC_COMMAND_DEBUG_RUN_TEST = 0x2, 928701cb00SAlex Elder IPA_UC_COMMAND_DEBUG_GET_INFO = 0x3, 938701cb00SAlex Elder IPA_UC_COMMAND_ERR_FATAL = 0x4, 948701cb00SAlex Elder IPA_UC_COMMAND_CLK_GATE = 0x5, 958701cb00SAlex Elder IPA_UC_COMMAND_CLK_UNGATE = 0x6, 968701cb00SAlex Elder IPA_UC_COMMAND_MEMCPY = 0x7, 978701cb00SAlex Elder IPA_UC_COMMAND_RESET_PIPE = 0x8, 988701cb00SAlex Elder IPA_UC_COMMAND_REG_WRITE = 0x9, 998701cb00SAlex Elder IPA_UC_COMMAND_GSI_CH_EMPTY = 0xa, 100a646d6ecSAlex Elder }; 101a646d6ecSAlex Elder 102a646d6ecSAlex Elder /** enum ipa_uc_response - microcontroller response codes */ 103a646d6ecSAlex Elder enum ipa_uc_response { 1048701cb00SAlex Elder IPA_UC_RESPONSE_NO_OP = 0x0, 1058701cb00SAlex Elder IPA_UC_RESPONSE_INIT_COMPLETED = 0x1, 1068701cb00SAlex Elder IPA_UC_RESPONSE_CMD_COMPLETED = 0x2, 1078701cb00SAlex Elder IPA_UC_RESPONSE_DEBUG_GET_INFO = 0x3, 108a646d6ecSAlex Elder }; 109a646d6ecSAlex Elder 110a646d6ecSAlex Elder /** enum ipa_uc_event - common cpu events reported by the microcontroller */ 111a646d6ecSAlex Elder enum ipa_uc_event { 1128701cb00SAlex Elder IPA_UC_EVENT_NO_OP = 0x0, 1138701cb00SAlex Elder IPA_UC_EVENT_ERROR = 0x1, 1148701cb00SAlex Elder IPA_UC_EVENT_LOG_INFO = 0x2, 115a646d6ecSAlex Elder }; 116a646d6ecSAlex Elder 117a646d6ecSAlex Elder static struct ipa_uc_mem_area *ipa_uc_shared(struct ipa *ipa) 118a646d6ecSAlex Elder { 1195e3bc1e5SAlex Elder const struct ipa_mem *mem = ipa_mem_find(ipa, IPA_MEM_UC_SHARED); 1205e3bc1e5SAlex Elder u32 offset = ipa->mem_offset + mem->offset; 121a646d6ecSAlex Elder 122a646d6ecSAlex Elder return ipa->mem_virt + offset; 123a646d6ecSAlex Elder } 124a646d6ecSAlex Elder 125a646d6ecSAlex Elder /* Microcontroller event IPA interrupt handler */ 126a646d6ecSAlex Elder static void ipa_uc_event_handler(struct ipa *ipa, enum ipa_irq_id irq_id) 127a646d6ecSAlex Elder { 128a646d6ecSAlex Elder struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa); 129a646d6ecSAlex Elder struct device *dev = &ipa->pdev->dev; 130a646d6ecSAlex Elder 131a646d6ecSAlex Elder if (shared->event == IPA_UC_EVENT_ERROR) 132a646d6ecSAlex Elder dev_err(dev, "microcontroller error event\n"); 1330a5096ecSAlex Elder else if (shared->event != IPA_UC_EVENT_LOG_INFO) 134e2f154e6SAlex Elder dev_err(dev, "unsupported microcontroller event %u\n", 135a646d6ecSAlex Elder shared->event); 1360a5096ecSAlex Elder /* The LOG_INFO event can be safely ignored */ 137a646d6ecSAlex Elder } 138a646d6ecSAlex Elder 139a646d6ecSAlex Elder /* Microcontroller response IPA interrupt handler */ 140a646d6ecSAlex Elder static void ipa_uc_response_hdlr(struct ipa *ipa, enum ipa_irq_id irq_id) 141a646d6ecSAlex Elder { 142a646d6ecSAlex Elder struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa); 143e2f154e6SAlex Elder struct device *dev = &ipa->pdev->dev; 144a646d6ecSAlex Elder 145a646d6ecSAlex Elder /* An INIT_COMPLETED response message is sent to the AP by the 146a646d6ecSAlex Elder * microcontroller when it is operational. Other than this, the AP 147a646d6ecSAlex Elder * should only receive responses from the microcontroller when it has 148a646d6ecSAlex Elder * sent it a request message. 149a646d6ecSAlex Elder * 150e2f154e6SAlex Elder * We can drop the clock reference taken in ipa_uc_clock() once we 151a646d6ecSAlex Elder * know the microcontroller has finished its initialization. 152a646d6ecSAlex Elder */ 153a646d6ecSAlex Elder switch (shared->response) { 154a646d6ecSAlex Elder case IPA_UC_RESPONSE_INIT_COMPLETED: 155e2f154e6SAlex Elder if (ipa->uc_clocked) { 156a646d6ecSAlex Elder ipa->uc_loaded = true; 157*799c5c24SAlex Elder (void)pm_runtime_put(dev); 158e2f154e6SAlex Elder ipa->uc_clocked = false; 159e2f154e6SAlex Elder } else { 160e2f154e6SAlex Elder dev_warn(dev, "unexpected init_completed response\n"); 161e2f154e6SAlex Elder } 162a646d6ecSAlex Elder break; 163a646d6ecSAlex Elder default: 164e2f154e6SAlex Elder dev_warn(dev, "unsupported microcontroller response %u\n", 165a646d6ecSAlex Elder shared->response); 166a646d6ecSAlex Elder break; 167a646d6ecSAlex Elder } 168a646d6ecSAlex Elder } 169a646d6ecSAlex Elder 170dc8f7e39SAlex Elder /* Configure the IPA microcontroller subsystem */ 171dc8f7e39SAlex Elder void ipa_uc_config(struct ipa *ipa) 172a646d6ecSAlex Elder { 173e2f154e6SAlex Elder ipa->uc_clocked = false; 174a646d6ecSAlex Elder ipa->uc_loaded = false; 175a646d6ecSAlex Elder ipa_interrupt_add(ipa->interrupt, IPA_IRQ_UC_0, ipa_uc_event_handler); 176a646d6ecSAlex Elder ipa_interrupt_add(ipa->interrupt, IPA_IRQ_UC_1, ipa_uc_response_hdlr); 177a646d6ecSAlex Elder } 178a646d6ecSAlex Elder 179dc8f7e39SAlex Elder /* Inverse of ipa_uc_config() */ 180dc8f7e39SAlex Elder void ipa_uc_deconfig(struct ipa *ipa) 181a646d6ecSAlex Elder { 182a646d6ecSAlex Elder ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_UC_1); 183a646d6ecSAlex Elder ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_UC_0); 184e2f154e6SAlex Elder if (ipa->uc_clocked) 185*799c5c24SAlex Elder (void)pm_runtime_put(&ipa->pdev->dev); 186a646d6ecSAlex Elder } 187a646d6ecSAlex Elder 188e2f154e6SAlex Elder /* Take a proxy clock reference for the microcontroller */ 189e2f154e6SAlex Elder void ipa_uc_clock(struct ipa *ipa) 190e2f154e6SAlex Elder { 191e2f154e6SAlex Elder static bool already; 192*799c5c24SAlex Elder struct device *dev; 1937ebd168cSAlex Elder int ret; 194e2f154e6SAlex Elder 195e2f154e6SAlex Elder if (already) 196e2f154e6SAlex Elder return; 197e2f154e6SAlex Elder already = true; /* Only do this on first boot */ 198e2f154e6SAlex Elder 199*799c5c24SAlex Elder /* This power reference dropped in ipa_uc_response_hdlr() above */ 200*799c5c24SAlex Elder dev = &ipa->pdev->dev; 201*799c5c24SAlex Elder ret = pm_runtime_get_sync(dev); 202*799c5c24SAlex Elder if (ret < 0) { 203*799c5c24SAlex Elder pm_runtime_put_noidle(dev); 204*799c5c24SAlex Elder dev_err(dev, "error %d getting proxy power\n", ret); 205*799c5c24SAlex Elder } else { 206*799c5c24SAlex Elder ipa->uc_clocked = true; 207*799c5c24SAlex Elder } 208e2f154e6SAlex Elder } 209e2f154e6SAlex Elder 210a646d6ecSAlex Elder /* Send a command to the microcontroller */ 211a646d6ecSAlex Elder static void send_uc_command(struct ipa *ipa, u32 command, u32 command_param) 212a646d6ecSAlex Elder { 213a646d6ecSAlex Elder struct ipa_uc_mem_area *shared = ipa_uc_shared(ipa); 214e666aa97SAlex Elder u32 offset; 215716a115bSAlex Elder u32 val; 216a646d6ecSAlex Elder 217716a115bSAlex Elder /* Fill in the command data */ 218a646d6ecSAlex Elder shared->command = command; 219a646d6ecSAlex Elder shared->command_param = cpu_to_le32(command_param); 220a646d6ecSAlex Elder shared->command_param_hi = 0; 221a646d6ecSAlex Elder shared->response = 0; 222a646d6ecSAlex Elder shared->response_param = 0; 223a646d6ecSAlex Elder 224716a115bSAlex Elder /* Use an interrupt to tell the microcontroller the command is ready */ 225716a115bSAlex Elder val = u32_encode_bits(1, UC_INTR_FMASK); 226e666aa97SAlex Elder offset = ipa_reg_irq_uc_offset(ipa->version); 227e666aa97SAlex Elder iowrite32(val, ipa->reg_virt + offset); 228a646d6ecSAlex Elder } 229a646d6ecSAlex Elder 230a646d6ecSAlex Elder /* Tell the microcontroller the AP is shutting down */ 231a646d6ecSAlex Elder void ipa_uc_panic_notifier(struct ipa *ipa) 232a646d6ecSAlex Elder { 233a646d6ecSAlex Elder if (!ipa->uc_loaded) 234a646d6ecSAlex Elder return; 235a646d6ecSAlex Elder 236a646d6ecSAlex Elder send_uc_command(ipa, IPA_UC_COMMAND_ERR_FATAL, 0); 237a646d6ecSAlex Elder 238a646d6ecSAlex Elder /* give uc enough time to save state */ 239a646d6ecSAlex Elder udelay(IPA_SEND_DELAY); 240a646d6ecSAlex Elder } 241