Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
Lina Iyer | d9abfa6 | 2017-07-27 17:23:05 -0600 | [diff] [blame] | 14 | #define pr_fmt(fmt) "%s " fmt, KBUILD_MODNAME |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 15 | |
Lina Iyer | b68814f | 2017-04-14 12:49:07 -0600 | [diff] [blame] | 16 | #include <linux/atomic.h> |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 17 | #include <linux/bitmap.h> |
Lina Iyer | 8bb7d5a | 2017-04-20 09:50:41 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 20 | #include <linux/ipc_logging.h> |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 21 | #include <linux/jiffies.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/mailbox_client.h> /* For dev_err */ |
| 25 | #include <linux/mailbox_controller.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/of.h> |
| 28 | #include <linux/of_address.h> |
| 29 | #include <linux/of_irq.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/spinlock.h> |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 32 | |
| 33 | #include <asm-generic/io.h> |
| 34 | |
| 35 | #include <soc/qcom/tcs.h> |
| 36 | |
| 37 | #include <dt-bindings/soc/qcom,tcs-mbox.h> |
| 38 | |
| 39 | #include "mailbox.h" |
| 40 | |
Lina Iyer | ea92144 | 2016-05-26 15:07:48 -0600 | [diff] [blame] | 41 | #define CREATE_TRACE_POINTS |
| 42 | #include <trace/events/rpmh.h> |
| 43 | |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 44 | #define TCS_DRV_IPC_LOG_SIZE 2 |
| 45 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 46 | #define MAX_CMDS_PER_TCS 16 |
| 47 | #define MAX_TCS_PER_TYPE 3 |
| 48 | #define MAX_TCS_SLOTS (MAX_CMDS_PER_TCS * MAX_TCS_PER_TYPE) |
| 49 | |
| 50 | #define TCS_DRV_TCS_OFFSET 672 |
| 51 | #define TCS_DRV_CMD_OFFSET 20 |
| 52 | |
| 53 | /* DRV Configuration Information Register */ |
| 54 | #define DRV_PRNT_CHLD_CONFIG 0x0C |
| 55 | #define DRV_NUM_TCS_MASK 0x3F |
| 56 | #define DRV_NUM_TCS_SHIFT 6 |
| 57 | #define DRV_NCPT_MASK 0x1F |
| 58 | #define DRV_NCPT_SHIFT 27 |
| 59 | |
| 60 | /* Register offsets */ |
| 61 | #define TCS_DRV_IRQ_ENABLE 0x00 |
| 62 | #define TCS_DRV_IRQ_STATUS 0x04 |
| 63 | #define TCS_DRV_IRQ_CLEAR 0x08 |
| 64 | #define TCS_DRV_CMD_WAIT_FOR_CMPL 0x10 |
| 65 | #define TCS_DRV_CONTROL 0x14 |
| 66 | #define TCS_DRV_STATUS 0x18 |
| 67 | #define TCS_DRV_CMD_ENABLE 0x1C |
| 68 | #define TCS_DRV_CMD_MSGID 0x30 |
| 69 | #define TCS_DRV_CMD_ADDR 0x34 |
| 70 | #define TCS_DRV_CMD_DATA 0x38 |
| 71 | #define TCS_DRV_CMD_STATUS 0x3C |
| 72 | #define TCS_DRV_CMD_RESP_DATA 0x40 |
| 73 | |
| 74 | #define TCS_AMC_MODE_ENABLE BIT(16) |
| 75 | #define TCS_AMC_MODE_TRIGGER BIT(24) |
| 76 | |
| 77 | /* TCS CMD register bit mask */ |
| 78 | #define CMD_MSGID_LEN 8 |
| 79 | #define CMD_MSGID_RESP_REQ BIT(8) |
| 80 | #define CMD_MSGID_WRITE BIT(16) |
| 81 | #define CMD_STATUS_ISSUED BIT(8) |
| 82 | #define CMD_STATUS_COMPL BIT(16) |
| 83 | |
| 84 | /* Control/Hidden TCS */ |
Lina Iyer | d1a6e68 | 2017-06-20 10:05:09 -0600 | [diff] [blame] | 85 | #define TCS_HIDDEN_MAX_SLOTS 2 |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 86 | #define TCS_HIDDEN_CMD0_DRV_DATA 0x38 |
| 87 | #define TCS_HIDDEN_CMD_SHIFT 0x08 |
| 88 | |
| 89 | #define TCS_TYPE_NR 4 |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 90 | #define MAX_POOL_SIZE (MAX_TCS_PER_TYPE * TCS_TYPE_NR) |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 91 | #define TCS_M_INIT 0xFFFF |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 92 | |
| 93 | struct tcs_drv; |
| 94 | |
| 95 | struct tcs_response { |
| 96 | struct tcs_drv *drv; |
| 97 | struct mbox_chan *chan; |
| 98 | struct tcs_mbox_msg *msg; |
| 99 | u32 m; /* m-th TCS */ |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 100 | int err; |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 101 | int idx; |
| 102 | bool in_use; |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 103 | struct list_head list; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | struct tcs_response_pool { |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 107 | struct tcs_response resp[MAX_POOL_SIZE]; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 108 | spinlock_t lock; |
| 109 | DECLARE_BITMAP(avail, MAX_POOL_SIZE); |
| 110 | }; |
| 111 | |
| 112 | /* One per TCS type of a controller */ |
| 113 | struct tcs_mbox { |
| 114 | struct tcs_drv *drv; |
| 115 | u32 *cmd_addr; |
| 116 | int type; |
| 117 | u32 tcs_mask; |
| 118 | u32 tcs_offset; |
| 119 | int num_tcs; |
| 120 | int ncpt; /* num cmds per tcs */ |
| 121 | DECLARE_BITMAP(slots, MAX_TCS_SLOTS); |
| 122 | spinlock_t tcs_lock; /* TCS type lock */ |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* One per MBOX controller */ |
| 126 | struct tcs_drv { |
Lina Iyer | 1442465 | 2017-06-03 15:55:25 -0600 | [diff] [blame] | 127 | struct mbox_controller mbox; |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 128 | const char *name; |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 129 | void __iomem *base; /* start address of the RSC's registers */ |
| 130 | void __iomem *reg_base; /* start address for DRV specific register */ |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 131 | int drv_id; |
| 132 | struct platform_device *pdev; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 133 | struct tcs_mbox tcs[TCS_TYPE_NR]; |
| 134 | int num_assigned; |
| 135 | int num_tcs; |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 136 | struct tasklet_struct tasklet; |
| 137 | struct list_head response_pending; |
| 138 | spinlock_t drv_lock; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 139 | struct tcs_response_pool *resp_pool; |
Lina Iyer | 088ccec | 2017-04-24 20:18:48 -0600 | [diff] [blame] | 140 | atomic_t tcs_in_use[MAX_POOL_SIZE]; |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 141 | /* Debug info */ |
| 142 | u64 tcs_last_sent_ts[MAX_POOL_SIZE]; |
| 143 | u64 tcs_last_recv_ts[MAX_POOL_SIZE]; |
Lina Iyer | 088ccec | 2017-04-24 20:18:48 -0600 | [diff] [blame] | 144 | atomic_t tcs_send_count[MAX_POOL_SIZE]; |
| 145 | atomic_t tcs_irq_count[MAX_POOL_SIZE]; |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 146 | void *ipc_log_ctx; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 147 | }; |
| 148 | |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 149 | /* Log to IPC and Ftrace */ |
| 150 | #define log_send_msg(drv, m, n, i, a, d, c, t) do { \ |
| 151 | trace_rpmh_send_msg(drv->name, m, n, i, a, d, c, t); \ |
| 152 | ipc_log_string(drv->ipc_log_ctx, \ |
| 153 | "send msg: m=%d n=%d msgid=0x%x addr=0x%x data=0x%x cmpl=%d trigger=%d", \ |
| 154 | m, n, i, a, d, c, t); \ |
| 155 | } while (0) |
| 156 | |
| 157 | #define log_rpmh_notify_irq(drv, m, a, e) do { \ |
| 158 | trace_rpmh_notify_irq(drv->name, m, a, e); \ |
| 159 | ipc_log_string(drv->ipc_log_ctx, \ |
| 160 | "irq response: m=%d addr=0x%x err=%d", m, a, e); \ |
| 161 | } while (0) |
| 162 | |
| 163 | #define log_rpmh_control_msg(drv, d) do { \ |
| 164 | trace_rpmh_control_msg(drv->name, d); \ |
| 165 | ipc_log_string(drv->ipc_log_ctx, "ctrlr msg: data=0x%x", d); \ |
| 166 | } while (0) |
| 167 | |
| 168 | #define log_rpmh_notify(drv, m, a, e) do { \ |
| 169 | trace_rpmh_notify(drv->name, m, a, e); \ |
| 170 | ipc_log_string(drv->ipc_log_ctx, \ |
| 171 | "tx done: m=%d addr=0x%x err=%d", m, a, e); \ |
| 172 | } while (0) |
| 173 | |
| 174 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 175 | static int tcs_response_pool_init(struct tcs_drv *drv) |
| 176 | { |
| 177 | struct tcs_response_pool *pool; |
| 178 | int i; |
| 179 | |
| 180 | pool = devm_kzalloc(&drv->pdev->dev, sizeof(*pool), GFP_KERNEL); |
| 181 | if (!pool) |
| 182 | return -ENOMEM; |
| 183 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 184 | for (i = 0; i < MAX_POOL_SIZE; i++) { |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 185 | pool->resp[i].drv = drv; |
| 186 | pool->resp[i].idx = i; |
| 187 | pool->resp[i].m = TCS_M_INIT; |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 188 | INIT_LIST_HEAD(&pool->resp[i].list); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | spin_lock_init(&pool->lock); |
| 192 | drv->resp_pool = pool; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 197 | static struct tcs_response *setup_response(struct tcs_drv *drv, |
| 198 | struct tcs_mbox_msg *msg, struct mbox_chan *chan, |
| 199 | u32 m, int err) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 200 | { |
| 201 | struct tcs_response_pool *pool = drv->resp_pool; |
| 202 | struct tcs_response *resp = ERR_PTR(-ENOMEM); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 203 | int pos; |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 204 | unsigned long flags; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 205 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 206 | spin_lock_irqsave(&pool->lock, flags); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 207 | pos = find_first_zero_bit(pool->avail, MAX_POOL_SIZE); |
| 208 | if (pos != MAX_POOL_SIZE) { |
| 209 | bitmap_set(pool->avail, pos, 1); |
| 210 | resp = &pool->resp[pos]; |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 211 | resp->chan = chan; |
| 212 | resp->msg = msg; |
| 213 | resp->m = m; |
| 214 | resp->err = err; |
| 215 | resp->in_use = false; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 216 | } |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 217 | spin_unlock_irqrestore(&pool->lock, flags); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 218 | |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 219 | if (pos == MAX_POOL_SIZE) |
| 220 | pr_err("response pool is full\n"); |
| 221 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 222 | return resp; |
| 223 | } |
| 224 | |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 225 | static void free_response(struct tcs_response *resp) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 226 | { |
| 227 | struct tcs_response_pool *pool = resp->drv->resp_pool; |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 228 | unsigned long flags; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 229 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 230 | spin_lock_irqsave(&pool->lock, flags); |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 231 | resp->err = -EINVAL; |
| 232 | bitmap_clear(pool->avail, resp->idx, 1); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 233 | spin_unlock_irqrestore(&pool->lock, flags); |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 234 | } |
| 235 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 236 | static inline struct tcs_response *get_response(struct tcs_drv *drv, u32 m, |
| 237 | bool for_use) |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 238 | { |
| 239 | struct tcs_response_pool *pool = drv->resp_pool; |
| 240 | struct tcs_response *resp = NULL; |
| 241 | int pos = 0; |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 242 | unsigned long flags; |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 243 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 244 | spin_lock_irqsave(&pool->lock, flags); |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 245 | do { |
| 246 | pos = find_next_bit(pool->avail, MAX_POOL_SIZE, pos); |
| 247 | if (pos == MAX_POOL_SIZE) |
| 248 | break; |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 249 | |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 250 | resp = &pool->resp[pos]; |
| 251 | if (resp->m == m && !resp->in_use) { |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 252 | resp->in_use = for_use; |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 253 | break; |
| 254 | } |
| 255 | pos++; |
| 256 | } while (1); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 257 | spin_unlock_irqrestore(&pool->lock, flags); |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 258 | |
| 259 | return resp; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 260 | } |
| 261 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 262 | static void print_response(struct tcs_drv *drv, int m) |
| 263 | { |
| 264 | struct tcs_response *resp; |
| 265 | struct tcs_mbox_msg *msg; |
| 266 | int i; |
| 267 | |
| 268 | resp = get_response(drv, m, false); |
| 269 | if (!resp) |
| 270 | return; |
| 271 | |
| 272 | msg = resp->msg; |
Lina Iyer | d9abfa6 | 2017-07-27 17:23:05 -0600 | [diff] [blame] | 273 | pr_warn("Response object [idx=%d for-tcs=%d in-use=%d]\n", |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 274 | resp->idx, resp->m, resp->in_use); |
Lina Iyer | e2f4272 | 2017-07-13 12:37:50 -0600 | [diff] [blame] | 275 | pr_warn("Msg: state=%d\n", msg->state); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 276 | for (i = 0; i < msg->num_payload; i++) |
Lina Iyer | e2f4272 | 2017-07-13 12:37:50 -0600 | [diff] [blame] | 277 | pr_warn("addr=0x%x data=0x%x complete=0x%x\n", |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 278 | msg->payload[i].addr, |
| 279 | msg->payload[i].data, |
| 280 | msg->payload[i].complete); |
| 281 | } |
| 282 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 283 | static inline u32 read_drv_config(void __iomem *base) |
| 284 | { |
| 285 | return le32_to_cpu(readl_relaxed(base + DRV_PRNT_CHLD_CONFIG)); |
| 286 | } |
| 287 | |
| 288 | static inline u32 read_tcs_reg(void __iomem *base, int reg, int m, int n) |
| 289 | { |
| 290 | return le32_to_cpu(readl_relaxed(base + reg + |
| 291 | TCS_DRV_TCS_OFFSET * m + TCS_DRV_CMD_OFFSET * n)); |
| 292 | } |
| 293 | |
| 294 | static inline void write_tcs_reg(void __iomem *base, int reg, int m, int n, |
| 295 | u32 data) |
| 296 | { |
| 297 | writel_relaxed(cpu_to_le32(data), base + reg + |
| 298 | TCS_DRV_TCS_OFFSET * m + TCS_DRV_CMD_OFFSET * n); |
| 299 | } |
| 300 | |
| 301 | static inline void write_tcs_reg_sync(void __iomem *base, int reg, int m, int n, |
| 302 | u32 data) |
| 303 | { |
| 304 | do { |
| 305 | write_tcs_reg(base, reg, m, n, data); |
| 306 | if (data == read_tcs_reg(base, reg, m, n)) |
| 307 | break; |
Lina Iyer | 8bb7d5a | 2017-04-20 09:50:41 -0600 | [diff] [blame] | 308 | udelay(1); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 309 | } while (1); |
| 310 | } |
| 311 | |
Lina Iyer | b68814f | 2017-04-14 12:49:07 -0600 | [diff] [blame] | 312 | static inline bool tcs_is_free(struct tcs_drv *drv, int m) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 313 | { |
Lina Iyer | b68814f | 2017-04-14 12:49:07 -0600 | [diff] [blame] | 314 | void __iomem *base = drv->reg_base; |
| 315 | |
| 316 | return read_tcs_reg(base, TCS_DRV_STATUS, m, 0) && |
| 317 | !atomic_read(&drv->tcs_in_use[m]); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static inline struct tcs_mbox *get_tcs_from_index(struct tcs_drv *drv, int m) |
| 321 | { |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 322 | struct tcs_mbox *tcs = NULL; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 323 | int i; |
| 324 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 325 | for (i = 0; i < drv->num_tcs; i++) { |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 326 | tcs = &drv->tcs[i]; |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 327 | if (tcs->tcs_mask & (u32)BIT(m)) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 328 | break; |
| 329 | } |
| 330 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 331 | if (i == drv->num_tcs) { |
| 332 | WARN(1, "Incorrect TCS index %d", m); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 333 | tcs = NULL; |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 334 | } |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 335 | |
| 336 | return tcs; |
| 337 | } |
| 338 | |
| 339 | static inline struct tcs_mbox *get_tcs_of_type(struct tcs_drv *drv, int type) |
| 340 | { |
| 341 | int i; |
| 342 | struct tcs_mbox *tcs; |
| 343 | |
| 344 | for (i = 0; i < TCS_TYPE_NR; i++) |
| 345 | if (type == drv->tcs[i].type) |
| 346 | break; |
| 347 | |
| 348 | if (i == TCS_TYPE_NR) |
| 349 | return ERR_PTR(-EINVAL); |
| 350 | |
| 351 | tcs = &drv->tcs[i]; |
| 352 | if (!tcs->num_tcs) |
| 353 | return ERR_PTR(-EINVAL); |
| 354 | |
| 355 | return tcs; |
| 356 | } |
| 357 | |
| 358 | static inline struct tcs_mbox *get_tcs_for_msg(struct tcs_drv *drv, |
| 359 | struct tcs_mbox_msg *msg) |
| 360 | { |
| 361 | int type = -1; |
| 362 | |
| 363 | /* Which box are we dropping this in and do we trigger the TCS */ |
| 364 | switch (msg->state) { |
| 365 | case RPMH_SLEEP_STATE: |
| 366 | type = SLEEP_TCS; |
| 367 | break; |
| 368 | case RPMH_WAKE_ONLY_STATE: |
| 369 | type = WAKE_TCS; |
| 370 | break; |
| 371 | case RPMH_ACTIVE_ONLY_STATE: |
| 372 | type = ACTIVE_TCS; |
| 373 | break; |
Lina Iyer | 21c1788 | 2016-09-22 11:05:51 -0600 | [diff] [blame] | 374 | case RPMH_AWAKE_STATE: |
| 375 | /* |
| 376 | * Awake state is only used when the DRV has no separate |
| 377 | * TCS for ACTIVE requests. Switch to WAKE TCS to send |
| 378 | * active votes. Otherwise, the caller should be explicit |
| 379 | * about the state. |
| 380 | */ |
| 381 | if (IS_ERR(get_tcs_of_type(drv, ACTIVE_TCS))) |
| 382 | type = WAKE_TCS; |
| 383 | break; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | if (msg->is_read) |
| 387 | type = ACTIVE_TCS; |
| 388 | |
| 389 | if (type < 0) |
| 390 | return ERR_PTR(-EINVAL); |
| 391 | |
| 392 | return get_tcs_of_type(drv, type); |
| 393 | } |
| 394 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 395 | static inline void send_tcs_response(struct tcs_response *resp) |
| 396 | { |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 397 | struct tcs_drv *drv = resp->drv; |
| 398 | unsigned long flags; |
| 399 | |
| 400 | spin_lock_irqsave(&drv->drv_lock, flags); |
| 401 | INIT_LIST_HEAD(&resp->list); |
| 402 | list_add_tail(&resp->list, &drv->response_pending); |
| 403 | spin_unlock_irqrestore(&drv->drv_lock, flags); |
| 404 | |
| 405 | tasklet_schedule(&drv->tasklet); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 406 | } |
| 407 | |
Lina Iyer | e0ef521 | 2017-04-28 09:55:32 -0600 | [diff] [blame] | 408 | static inline void enable_tcs_irq(struct tcs_drv *drv, int m, bool enable) |
| 409 | { |
| 410 | void __iomem *base = drv->reg_base; |
| 411 | u32 data; |
| 412 | |
| 413 | /* Enable interrupts for non-ACTIVE TCS */ |
| 414 | data = read_tcs_reg(base, TCS_DRV_IRQ_ENABLE, 0, 0); |
| 415 | if (enable) |
| 416 | data |= BIT(m); |
| 417 | else |
| 418 | data &= ~BIT(m); |
| 419 | write_tcs_reg(base, TCS_DRV_IRQ_ENABLE, 0, 0, data); |
| 420 | } |
| 421 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 422 | /** |
| 423 | * tcs_irq_handler: TX Done / Recv data handler |
| 424 | */ |
| 425 | static irqreturn_t tcs_irq_handler(int irq, void *p) |
| 426 | { |
| 427 | struct tcs_drv *drv = p; |
| 428 | void __iomem *base = drv->reg_base; |
| 429 | int m, i; |
| 430 | u32 irq_status, sts; |
Lina Iyer | 21c1788 | 2016-09-22 11:05:51 -0600 | [diff] [blame] | 431 | struct tcs_mbox *tcs; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 432 | struct tcs_response *resp; |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 433 | struct tcs_cmd *cmd; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 434 | u32 data; |
| 435 | |
| 436 | /* Know which TCSes were triggered */ |
| 437 | irq_status = read_tcs_reg(base, TCS_DRV_IRQ_STATUS, 0, 0); |
| 438 | |
Lina Iyer | b465ea6 | 2017-05-03 15:27:22 -0600 | [diff] [blame] | 439 | for (m = 0; m < drv->num_tcs; m++) { |
| 440 | if (!(irq_status & (u32)BIT(m))) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 441 | continue; |
Lina Iyer | 088ccec | 2017-04-24 20:18:48 -0600 | [diff] [blame] | 442 | atomic_inc(&drv->tcs_irq_count[m]); |
| 443 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 444 | resp = get_response(drv, m, true); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 445 | if (!resp) { |
| 446 | pr_err("No resp request for TCS-%d\n", m); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 447 | goto no_resp; |
Lina Iyer | 21c1788 | 2016-09-22 11:05:51 -0600 | [diff] [blame] | 448 | } |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 449 | |
| 450 | /* Check if all commands were completed */ |
| 451 | resp->err = 0; |
| 452 | for (i = 0; i < resp->msg->num_payload; i++) { |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 453 | cmd = &resp->msg->payload[i]; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 454 | sts = read_tcs_reg(base, TCS_DRV_CMD_STATUS, m, i); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 455 | if ((!(sts & CMD_STATUS_ISSUED)) || |
| 456 | ((resp->msg->is_complete || cmd->complete) && |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 457 | (!(sts & CMD_STATUS_COMPL)))) { |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 458 | resp->err = -EIO; |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 459 | break; |
| 460 | } |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* Check for response if this was a read request */ |
| 464 | if (resp->msg->is_read) { |
| 465 | /* Respond the data back in the same req data */ |
| 466 | data = read_tcs_reg(base, TCS_DRV_CMD_RESP_DATA, m, 0); |
| 467 | resp->msg->payload[0].data = data; |
| 468 | mbox_chan_received_data(resp->chan, resp->msg); |
| 469 | } |
| 470 | |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 471 | log_rpmh_notify_irq(drv, m, resp->msg->payload[0].addr, |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 472 | resp->err); |
Lina Iyer | ea92144 | 2016-05-26 15:07:48 -0600 | [diff] [blame] | 473 | |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 474 | /* Clear the AMC mode for non-ACTIVE TCSes */ |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 475 | tcs = get_tcs_from_index(drv, m); |
| 476 | if (tcs && tcs->type != ACTIVE_TCS) { |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 477 | data = read_tcs_reg(base, TCS_DRV_CONTROL, m, 0); |
| 478 | data &= ~TCS_AMC_MODE_ENABLE; |
| 479 | write_tcs_reg(base, TCS_DRV_CONTROL, m, 0, data); |
Lina Iyer | e0ef521 | 2017-04-28 09:55:32 -0600 | [diff] [blame] | 480 | /* |
| 481 | * Disable interrupt for this TCS to avoid being |
| 482 | * spammed with interrupts coming when the solver |
| 483 | * sends its wake votes. |
| 484 | */ |
| 485 | enable_tcs_irq(drv, m, false); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 486 | } else { |
| 487 | /* Clear the enable bit for the commands */ |
| 488 | write_tcs_reg(base, TCS_DRV_CMD_ENABLE, m, 0, 0); |
| 489 | } |
| 490 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 491 | no_resp: |
| 492 | /* Record the recvd time stamp */ |
| 493 | drv->tcs_last_recv_ts[m] = arch_counter_get_cntvct(); |
| 494 | |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 495 | /* Clear the TCS IRQ status */ |
| 496 | write_tcs_reg(base, TCS_DRV_IRQ_CLEAR, 0, 0, BIT(m)); |
| 497 | |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 498 | /* Notify the client that this request is completed. */ |
| 499 | atomic_set(&drv->tcs_in_use[m], 0); |
| 500 | |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 501 | /* Clean up response object and notify mbox in tasklet */ |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 502 | if (resp) |
| 503 | send_tcs_response(resp); |
Lina Iyer | b68814f | 2017-04-14 12:49:07 -0600 | [diff] [blame] | 504 | } |
| 505 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 506 | return IRQ_HANDLED; |
| 507 | } |
| 508 | |
| 509 | static inline void mbox_notify_tx_done(struct mbox_chan *chan, |
| 510 | struct tcs_mbox_msg *msg, int m, int err) |
| 511 | { |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 512 | struct tcs_drv *drv = container_of(chan->mbox, struct tcs_drv, mbox); |
| 513 | |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 514 | log_rpmh_notify(drv, m, msg->payload[0].addr, err); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 515 | mbox_chan_txdone(chan, err); |
| 516 | } |
| 517 | |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 518 | static void respond_tx_done(struct tcs_response *resp) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 519 | { |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 520 | struct mbox_chan *chan = resp->chan; |
| 521 | struct tcs_mbox_msg *msg = resp->msg; |
| 522 | int err = resp->err; |
| 523 | int m = resp->m; |
| 524 | |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 525 | free_response(resp); |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 526 | mbox_notify_tx_done(chan, msg, m, err); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * tcs_notify_tx_done: TX Done for requests that do not trigger TCS |
| 531 | */ |
| 532 | static void tcs_notify_tx_done(unsigned long data) |
| 533 | { |
| 534 | struct tcs_drv *drv = (struct tcs_drv *)data; |
| 535 | struct tcs_response *resp; |
| 536 | unsigned long flags; |
| 537 | |
| 538 | do { |
| 539 | spin_lock_irqsave(&drv->drv_lock, flags); |
| 540 | if (list_empty(&drv->response_pending)) { |
| 541 | spin_unlock_irqrestore(&drv->drv_lock, flags); |
| 542 | break; |
| 543 | } |
| 544 | resp = list_first_entry(&drv->response_pending, |
| 545 | struct tcs_response, list); |
| 546 | list_del(&resp->list); |
| 547 | spin_unlock_irqrestore(&drv->drv_lock, flags); |
| 548 | respond_tx_done(resp); |
| 549 | } while (1); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 550 | } |
| 551 | |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 552 | static void __tcs_buffer_write(struct tcs_drv *drv, int d, int m, int n, |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 553 | struct tcs_mbox_msg *msg, bool trigger) |
| 554 | { |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 555 | u32 msgid, cmd_msgid = 0; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 556 | u32 cmd_enable = 0; |
| 557 | u32 cmd_complete; |
| 558 | u32 enable = TCS_AMC_MODE_ENABLE; |
| 559 | struct tcs_cmd *cmd; |
| 560 | int i; |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 561 | void __iomem *base = drv->reg_base; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 562 | |
| 563 | /* We have homologous command set i.e pure read or write, not a mix */ |
| 564 | cmd_msgid = CMD_MSGID_LEN; |
| 565 | cmd_msgid |= (msg->is_complete) ? CMD_MSGID_RESP_REQ : 0; |
| 566 | cmd_msgid |= (!msg->is_read) ? CMD_MSGID_WRITE : 0; |
| 567 | |
| 568 | /* Read the send-after-prev complete flag for those already in TCS */ |
| 569 | cmd_complete = read_tcs_reg(base, TCS_DRV_CMD_WAIT_FOR_CMPL, m, 0); |
| 570 | |
| 571 | for (i = 0; i < msg->num_payload; i++) { |
| 572 | cmd = &msg->payload[i]; |
| 573 | cmd_enable |= BIT(n + i); |
| 574 | cmd_complete |= cmd->complete << (n + i); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 575 | msgid = cmd_msgid; |
| 576 | msgid |= (cmd->complete) ? CMD_MSGID_RESP_REQ : 0; |
| 577 | write_tcs_reg(base, TCS_DRV_CMD_MSGID, m, n + i, msgid); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 578 | write_tcs_reg(base, TCS_DRV_CMD_ADDR, m, n + i, cmd->addr); |
| 579 | write_tcs_reg(base, TCS_DRV_CMD_DATA, m, n + i, cmd->data); |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 580 | log_send_msg(drv, m, n + i, msgid, cmd->addr, |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 581 | cmd->data, cmd->complete, trigger); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* Write the send-after-prev completion bits for the batch */ |
| 585 | write_tcs_reg(base, TCS_DRV_CMD_WAIT_FOR_CMPL, m, 0, cmd_complete); |
| 586 | |
| 587 | /* Enable the new commands in TCS */ |
| 588 | cmd_enable |= read_tcs_reg(base, TCS_DRV_CMD_ENABLE, m, 0); |
| 589 | write_tcs_reg(base, TCS_DRV_CMD_ENABLE, m, 0, cmd_enable); |
| 590 | |
| 591 | if (trigger) { |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 592 | /* HW req: Clear the DRV_CONTROL and enable TCS again */ |
| 593 | write_tcs_reg_sync(base, TCS_DRV_CONTROL, m, 0, 0); |
| 594 | write_tcs_reg_sync(base, TCS_DRV_CONTROL, m, 0, enable); |
| 595 | /* Enable the AMC mode on the TCS */ |
| 596 | enable |= TCS_AMC_MODE_TRIGGER; |
| 597 | write_tcs_reg_sync(base, TCS_DRV_CONTROL, m, 0, enable); |
| 598 | } |
| 599 | } |
| 600 | |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 601 | /** |
| 602 | * tcs_drv_is_idle: Check if any of the AMCs are busy. |
| 603 | * |
| 604 | * @mbox: The mailbox controller. |
| 605 | * |
| 606 | * Returns true if the AMCs are not engaged or absent. |
| 607 | */ |
| 608 | static bool tcs_drv_is_idle(struct mbox_controller *mbox) |
| 609 | { |
| 610 | int m; |
| 611 | struct tcs_drv *drv = container_of(mbox, struct tcs_drv, mbox); |
| 612 | struct tcs_mbox *tcs = get_tcs_of_type(drv, ACTIVE_TCS); |
| 613 | |
Lina Iyer | 21c1788 | 2016-09-22 11:05:51 -0600 | [diff] [blame] | 614 | /* Check for WAKE TCS if there are no ACTIVE TCS */ |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 615 | if (IS_ERR(tcs)) |
Lina Iyer | 21c1788 | 2016-09-22 11:05:51 -0600 | [diff] [blame] | 616 | tcs = get_tcs_of_type(drv, WAKE_TCS); |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 617 | |
| 618 | for (m = tcs->tcs_offset; m < tcs->tcs_offset + tcs->num_tcs; m++) |
Lina Iyer | b68814f | 2017-04-14 12:49:07 -0600 | [diff] [blame] | 619 | if (!tcs_is_free(drv, m)) |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 620 | return false; |
| 621 | |
| 622 | return true; |
| 623 | } |
| 624 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 625 | static int check_for_req_inflight(struct tcs_drv *drv, struct tcs_mbox *tcs, |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 626 | struct tcs_mbox_msg *msg) |
| 627 | { |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 628 | u32 curr_enabled, addr; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 629 | int i, j, k; |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 630 | void __iomem *base = drv->reg_base; |
| 631 | int m = tcs->tcs_offset; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 632 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 633 | for (i = 0; i < tcs->num_tcs; i++, m++) { |
| 634 | if (tcs_is_free(drv, m)) |
| 635 | continue; |
| 636 | |
| 637 | curr_enabled = read_tcs_reg(base, TCS_DRV_CMD_ENABLE, m, 0); |
Lina Iyer | b465ea6 | 2017-05-03 15:27:22 -0600 | [diff] [blame] | 638 | |
| 639 | for (j = 0; j < MAX_CMDS_PER_TCS; j++) { |
| 640 | if (!(curr_enabled & (u32)BIT(j))) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 641 | continue; |
Lina Iyer | b465ea6 | 2017-05-03 15:27:22 -0600 | [diff] [blame] | 642 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 643 | addr = read_tcs_reg(base, TCS_DRV_CMD_ADDR, m, j); |
| 644 | for (k = 0; k < msg->num_payload; k++) { |
| 645 | if (addr == msg->payload[k].addr) |
| 646 | return -EBUSY; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 647 | } |
| 648 | } |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | return 0; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static int find_free_tcs(struct tcs_mbox *tcs) |
| 655 | { |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 656 | int slot = -EBUSY; |
| 657 | int m = 0; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 658 | |
| 659 | /* Loop until we find a free AMC */ |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 660 | for (m = 0; m < tcs->num_tcs; m++) { |
Lina Iyer | b68814f | 2017-04-14 12:49:07 -0600 | [diff] [blame] | 661 | if (tcs_is_free(tcs->drv, tcs->tcs_offset + m)) { |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 662 | slot = m * tcs->ncpt; |
| 663 | break; |
| 664 | } |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 665 | } |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 666 | |
| 667 | return slot; |
| 668 | } |
| 669 | |
| 670 | static int find_match(struct tcs_mbox *tcs, struct tcs_cmd *cmd, int len) |
| 671 | { |
| 672 | bool found = false; |
| 673 | int i = 0, j; |
| 674 | |
| 675 | /* Check for already cached commands */ |
| 676 | while ((i = find_next_bit(tcs->slots, MAX_TCS_SLOTS, i)) < |
| 677 | MAX_TCS_SLOTS) { |
| 678 | if (tcs->cmd_addr[i] != cmd[0].addr) { |
| 679 | i++; |
| 680 | continue; |
| 681 | } |
| 682 | /* sanity check to ensure the seq is same */ |
| 683 | for (j = 1; j < len; j++) { |
| 684 | WARN((tcs->cmd_addr[i + j] != cmd[j].addr), |
| 685 | "Message does not match previous sequence.\n"); |
| 686 | return -EINVAL; |
| 687 | } |
| 688 | found = true; |
| 689 | break; |
| 690 | } |
| 691 | |
| 692 | return found ? i : -1; |
| 693 | } |
| 694 | |
| 695 | static int find_slots(struct tcs_mbox *tcs, struct tcs_mbox_msg *msg) |
| 696 | { |
| 697 | int slot; |
| 698 | int n = 0; |
| 699 | |
| 700 | /* For active requests find the first free AMC. */ |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 701 | if (msg->state == RPMH_ACTIVE_ONLY_STATE || |
| 702 | msg->state == RPMH_AWAKE_STATE) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 703 | return find_free_tcs(tcs); |
| 704 | |
| 705 | /* Find if we already have the msg in our TCS */ |
| 706 | slot = find_match(tcs, msg->payload, msg->num_payload); |
| 707 | if (slot >= 0) |
| 708 | return slot; |
| 709 | |
| 710 | /* Do over, until we can fit the full payload in a TCS */ |
| 711 | do { |
| 712 | slot = bitmap_find_next_zero_area(tcs->slots, MAX_TCS_SLOTS, |
| 713 | n, msg->num_payload, 0); |
| 714 | if (slot == MAX_TCS_SLOTS) |
| 715 | break; |
| 716 | n += tcs->ncpt; |
| 717 | } while (slot + msg->num_payload - 1 >= n); |
| 718 | |
| 719 | return (slot != MAX_TCS_SLOTS) ? slot : -ENOMEM; |
| 720 | } |
| 721 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 722 | static int tcs_mbox_write(struct mbox_chan *chan, struct tcs_mbox_msg *msg, |
| 723 | bool trigger) |
| 724 | { |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 725 | struct tcs_drv *drv = container_of(chan->mbox, struct tcs_drv, mbox); |
| 726 | int d = drv->drv_id; |
| 727 | struct tcs_mbox *tcs; |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 728 | int i, slot, offset, m, n, ret; |
Channagoud Kadabi | 075db3b | 2017-03-16 14:26:17 -0700 | [diff] [blame] | 729 | struct tcs_response *resp = NULL; |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 730 | unsigned long flags; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 731 | |
| 732 | tcs = get_tcs_for_msg(drv, msg); |
| 733 | if (IS_ERR(tcs)) |
| 734 | return PTR_ERR(tcs); |
| 735 | |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 736 | if (trigger) { |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 737 | resp = setup_response(drv, msg, chan, TCS_M_INIT, 0); |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 738 | if (IS_ERR_OR_NULL(resp)) |
| 739 | return -EBUSY; |
| 740 | } |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 741 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 742 | /* Identify the sequential slots that we can write to */ |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 743 | spin_lock_irqsave(&tcs->tcs_lock, flags); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 744 | slot = find_slots(tcs, msg); |
| 745 | if (slot < 0) { |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 746 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 747 | if (resp) |
| 748 | free_response(resp); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 749 | return slot; |
| 750 | } |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 751 | |
Lina Iyer | 1442465 | 2017-06-03 15:55:25 -0600 | [diff] [blame] | 752 | /* Figure out the TCS-m and CMD-n to write to */ |
| 753 | offset = slot / tcs->ncpt; |
| 754 | m = offset + tcs->tcs_offset; |
| 755 | n = slot % tcs->ncpt; |
| 756 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 757 | if (trigger) { |
Lina Iyer | 1442465 | 2017-06-03 15:55:25 -0600 | [diff] [blame] | 758 | /* Block, if we have an address from the msg in flight */ |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 759 | ret = check_for_req_inflight(drv, tcs, msg); |
| 760 | if (ret) { |
| 761 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 762 | if (resp) |
| 763 | free_response(resp); |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 764 | return ret; |
| 765 | } |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 766 | |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 767 | resp->m = m; |
| 768 | /* Mark the TCS as busy */ |
| 769 | atomic_set(&drv->tcs_in_use[m], 1); |
Lina Iyer | 088ccec | 2017-04-24 20:18:48 -0600 | [diff] [blame] | 770 | atomic_inc(&drv->tcs_send_count[m]); |
Lina Iyer | e0ef521 | 2017-04-28 09:55:32 -0600 | [diff] [blame] | 771 | /* Enable interrupt for active votes through wake TCS */ |
| 772 | if (tcs->type != ACTIVE_TCS) |
| 773 | enable_tcs_irq(drv, m, true); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 774 | drv->tcs_last_sent_ts[m] = arch_counter_get_cntvct(); |
Lina Iyer | 1442465 | 2017-06-03 15:55:25 -0600 | [diff] [blame] | 775 | } else { |
| 776 | /* Mark the slots as in-use, before we unlock */ |
| 777 | if (tcs->type == SLEEP_TCS || tcs->type == WAKE_TCS) |
| 778 | bitmap_set(tcs->slots, slot, msg->num_payload); |
| 779 | |
| 780 | /* Copy the addresses of the resources over to the slots */ |
| 781 | for (i = 0; tcs->cmd_addr && i < msg->num_payload; i++) |
| 782 | tcs->cmd_addr[slot + i] = msg->payload[i].addr; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* Write to the TCS or AMC */ |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 786 | __tcs_buffer_write(drv, d, m, n, msg, trigger); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 787 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 788 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 793 | static void __tcs_buffer_invalidate(void __iomem *base, int m) |
| 794 | { |
| 795 | write_tcs_reg(base, TCS_DRV_CMD_ENABLE, m, 0, 0); |
| 796 | } |
| 797 | |
| 798 | static int tcs_mbox_invalidate(struct mbox_chan *chan) |
| 799 | { |
| 800 | struct tcs_drv *drv = container_of(chan->mbox, struct tcs_drv, mbox); |
| 801 | struct tcs_mbox *tcs; |
| 802 | int m, i; |
| 803 | int inv_types[] = { WAKE_TCS, SLEEP_TCS }; |
| 804 | int type = 0; |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 805 | unsigned long flags; |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 806 | |
| 807 | do { |
| 808 | tcs = get_tcs_of_type(drv, inv_types[type]); |
| 809 | if (IS_ERR(tcs)) |
| 810 | return PTR_ERR(tcs); |
| 811 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 812 | spin_lock_irqsave(&tcs->tcs_lock, flags); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 813 | for (i = 0; i < tcs->num_tcs; i++) { |
| 814 | m = i + tcs->tcs_offset; |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 815 | if (!tcs_is_free(drv, m)) { |
| 816 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
| 817 | return -EBUSY; |
| 818 | } |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 819 | __tcs_buffer_invalidate(drv->reg_base, m); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 820 | } |
| 821 | /* Mark the TCS as free */ |
| 822 | bitmap_zero(tcs->slots, MAX_TCS_SLOTS); |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 823 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 824 | } while (++type < ARRAY_SIZE(inv_types)); |
| 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
Lina Iyer | 035ddd3 | 2017-05-30 22:11:53 -0600 | [diff] [blame] | 829 | static void print_tcs_regs(struct tcs_drv *drv, int m) |
| 830 | { |
| 831 | int n; |
| 832 | struct tcs_mbox *tcs = get_tcs_from_index(drv, m); |
| 833 | void __iomem *base = drv->reg_base; |
Lina Iyer | d9abfa6 | 2017-07-27 17:23:05 -0600 | [diff] [blame] | 834 | u32 enable, addr, data, msgid, sts, irq_sts; |
Lina Iyer | 035ddd3 | 2017-05-30 22:11:53 -0600 | [diff] [blame] | 835 | |
| 836 | if (!tcs || tcs_is_free(drv, m)) |
| 837 | return; |
| 838 | |
| 839 | enable = read_tcs_reg(base, TCS_DRV_CMD_ENABLE, m, 0); |
| 840 | if (!enable) |
| 841 | return; |
| 842 | |
Lina Iyer | d9abfa6 | 2017-07-27 17:23:05 -0600 | [diff] [blame] | 843 | pr_warn("RSC:%s\n", drv->name); |
| 844 | |
| 845 | sts = read_tcs_reg(base, TCS_DRV_STATUS, m, 0); |
| 846 | data = read_tcs_reg(base, TCS_DRV_CONTROL, m, 0); |
| 847 | irq_sts = read_tcs_reg(base, TCS_DRV_IRQ_STATUS, 0, 0); |
| 848 | pr_warn("TCS=%d [ctrlr-sts:%s amc-mode:0x%x irq-sts:%s]\n", |
| 849 | m, sts ? "IDLE" : "BUSY", data, |
| 850 | (irq_sts & BIT(m)) ? "COMPLETED" : "PENDING"); |
| 851 | |
Lina Iyer | 035ddd3 | 2017-05-30 22:11:53 -0600 | [diff] [blame] | 852 | for (n = 0; n < tcs->ncpt; n++) { |
| 853 | if (!(enable & BIT(n))) |
| 854 | continue; |
| 855 | addr = read_tcs_reg(base, TCS_DRV_CMD_ADDR, m, n); |
| 856 | data = read_tcs_reg(base, TCS_DRV_CMD_DATA, m, n); |
| 857 | msgid = read_tcs_reg(base, TCS_DRV_CMD_MSGID, m, n); |
Lina Iyer | d9abfa6 | 2017-07-27 17:23:05 -0600 | [diff] [blame] | 858 | sts = read_tcs_reg(base, TCS_DRV_CMD_STATUS, m, n); |
| 859 | pr_warn("\tCMD=%d [addr=0x%x data=0x%x hdr=0x%x sts=0x%x]\n", |
| 860 | n, addr, data, msgid, sts); |
Lina Iyer | 035ddd3 | 2017-05-30 22:11:53 -0600 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 864 | static void dump_tcs_stats(struct tcs_drv *drv) |
| 865 | { |
| 866 | int i; |
| 867 | unsigned long long curr = arch_counter_get_cntvct(); |
| 868 | |
| 869 | for (i = 0; i < drv->num_tcs; i++) { |
| 870 | if (!atomic_read(&drv->tcs_in_use[i])) |
| 871 | continue; |
Lina Iyer | e2f4272 | 2017-07-13 12:37:50 -0600 | [diff] [blame] | 872 | pr_warn("Time: %llu: TCS-%d:\n\tReq Sent:%d Last Sent:%llu\n\tResp Recv:%d Last Recvd:%llu\n", |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 873 | curr, i, |
| 874 | atomic_read(&drv->tcs_send_count[i]), |
| 875 | drv->tcs_last_sent_ts[i], |
| 876 | atomic_read(&drv->tcs_irq_count[i]), |
| 877 | drv->tcs_last_recv_ts[i]); |
Lina Iyer | 035ddd3 | 2017-05-30 22:11:53 -0600 | [diff] [blame] | 878 | print_tcs_regs(drv, i); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 879 | print_response(drv, i); |
| 880 | } |
| 881 | } |
| 882 | |
Lina Iyer | e2f4272 | 2017-07-13 12:37:50 -0600 | [diff] [blame] | 883 | static void chan_debug(struct mbox_chan *chan) |
| 884 | { |
| 885 | struct tcs_drv *drv = container_of(chan->mbox, struct tcs_drv, mbox); |
| 886 | |
| 887 | dump_tcs_stats(drv); |
| 888 | } |
| 889 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 890 | /** |
| 891 | * chan_tcs_write: Validate the incoming message and write to the |
| 892 | * appropriate TCS block. |
| 893 | * |
| 894 | * @chan: the MBOX channel |
| 895 | * @data: the tcs_mbox_msg* |
| 896 | * |
| 897 | * Returns a negative error for invalid message structure and invalid |
| 898 | * message combination, -EBUSY if there is an other active request for |
| 899 | * the channel in process, otherwise bubbles up internal error. |
| 900 | */ |
| 901 | static int chan_tcs_write(struct mbox_chan *chan, void *data) |
| 902 | { |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 903 | struct tcs_drv *drv = container_of(chan->mbox, struct tcs_drv, mbox); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 904 | struct tcs_mbox_msg *msg = data; |
| 905 | const struct device *dev = chan->cl->dev; |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 906 | int ret = 0; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 907 | |
| 908 | if (!msg) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 909 | dev_err(dev, "Payload error\n"); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 910 | ret = -EINVAL; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 911 | goto tx_fail; |
| 912 | } |
| 913 | |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 914 | if (!msg->payload || !msg->num_payload || |
| 915 | msg->num_payload > MAX_RPMH_PAYLOAD) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 916 | dev_err(dev, "Payload error\n"); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 917 | ret = -EINVAL; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 918 | goto tx_fail; |
| 919 | } |
| 920 | |
| 921 | if (msg->invalidate || msg->is_control) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 922 | dev_err(dev, "Incorrect API\n"); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 923 | ret = -EINVAL; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 924 | goto tx_fail; |
| 925 | } |
| 926 | |
Lina Iyer | 21c1788 | 2016-09-22 11:05:51 -0600 | [diff] [blame] | 927 | if (msg->state != RPMH_ACTIVE_ONLY_STATE && |
| 928 | msg->state != RPMH_AWAKE_STATE) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 929 | dev_err(dev, "Incorrect API\n"); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 930 | ret = -EINVAL; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 931 | goto tx_fail; |
| 932 | } |
| 933 | |
| 934 | /* Read requests should always be single */ |
| 935 | if (msg->is_read && msg->num_payload > 1) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 936 | dev_err(dev, "Incorrect read request\n"); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 937 | ret = -EINVAL; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 938 | goto tx_fail; |
| 939 | } |
| 940 | |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 941 | /* |
| 942 | * Since we are re-purposing the wake TCS, invalidate previous |
| 943 | * contents to avoid confusion. |
| 944 | */ |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 945 | if (msg->state == RPMH_AWAKE_STATE) { |
| 946 | ret = tcs_mbox_invalidate(chan); |
| 947 | if (ret) |
| 948 | goto tx_fail; |
| 949 | } |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 950 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 951 | /* Post the message to the TCS and trigger */ |
Lina Iyer | 64834df | 2017-06-09 11:36:46 -0600 | [diff] [blame] | 952 | ret = tcs_mbox_write(chan, msg, true); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 953 | |
| 954 | tx_fail: |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 955 | /* If there was an error in the request, schedule a response */ |
| 956 | if (ret < 0 && ret != -EBUSY) { |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 957 | struct tcs_response *resp = setup_response( |
| 958 | drv, msg, chan, TCS_M_INIT, ret); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 959 | |
| 960 | dev_err(dev, "Error sending RPMH message %d\n", ret); |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 961 | if (!IS_ERR(resp)) |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 962 | send_tcs_response(resp); |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 963 | else |
| 964 | dev_err(dev, "No response object %ld\n", PTR_ERR(resp)); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 965 | ret = 0; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 966 | } |
| 967 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 968 | /* If we were just busy waiting for TCS, dump the state and return */ |
| 969 | if (ret == -EBUSY) { |
Lina Iyer | 9497220 | 2017-06-28 15:37:43 -0600 | [diff] [blame] | 970 | pr_info_ratelimited("TCS Busy, retrying RPMH message send\n"); |
Lina Iyer | 348b271 | 2017-05-30 22:13:59 -0600 | [diff] [blame] | 971 | ret = -EAGAIN; |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | return ret; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 975 | } |
| 976 | |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 977 | static void __tcs_write_hidden(struct tcs_drv *drv, int d, |
| 978 | struct tcs_mbox_msg *msg) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 979 | { |
| 980 | int i; |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 981 | void __iomem *addr = drv->base + TCS_HIDDEN_CMD0_DRV_DATA; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 982 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 983 | for (i = 0; i < msg->num_payload; i++) { |
| 984 | /* Only data is write capable */ |
Lina Iyer | d93fbce | 2017-03-01 09:07:20 -0700 | [diff] [blame] | 985 | writel_relaxed(cpu_to_le32(msg->payload[i].data), addr); |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 986 | log_rpmh_control_msg(drv, msg->payload[i].data); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 987 | addr += TCS_HIDDEN_CMD_SHIFT; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | static int tcs_control_write(struct mbox_chan *chan, struct tcs_mbox_msg *msg) |
| 992 | { |
| 993 | const struct device *dev = chan->cl->dev; |
| 994 | struct tcs_drv *drv = container_of(chan->mbox, struct tcs_drv, mbox); |
| 995 | struct tcs_mbox *tcs; |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 996 | unsigned long flags; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 997 | |
| 998 | tcs = get_tcs_of_type(drv, CONTROL_TCS); |
| 999 | if (IS_ERR(tcs)) |
| 1000 | return PTR_ERR(tcs); |
| 1001 | |
| 1002 | if (msg->num_payload != tcs->ncpt) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 1003 | dev_err(dev, "Request must fit the control TCS size\n"); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1004 | return -EINVAL; |
| 1005 | } |
| 1006 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 1007 | spin_lock_irqsave(&tcs->tcs_lock, flags); |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 1008 | __tcs_write_hidden(tcs->drv, drv->drv_id, msg); |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 1009 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1010 | |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * chan_tcs_ctrl_write: Write message to the controller, no ACK sent. |
| 1016 | * |
| 1017 | * @chan: the MBOX channel |
| 1018 | * @data: the tcs_mbox_msg* |
| 1019 | */ |
| 1020 | static int chan_tcs_ctrl_write(struct mbox_chan *chan, void *data) |
| 1021 | { |
| 1022 | struct tcs_mbox_msg *msg = data; |
| 1023 | const struct device *dev = chan->cl->dev; |
| 1024 | int ret = -EINVAL; |
| 1025 | |
| 1026 | if (!msg) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 1027 | dev_err(dev, "Payload error\n"); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1028 | goto tx_done; |
| 1029 | } |
| 1030 | |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 1031 | if (!msg->payload || (!msg->num_payload && !msg->invalidate) || |
| 1032 | msg->num_payload > MAX_RPMH_PAYLOAD) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 1033 | dev_err(dev, "Payload error\n"); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1034 | goto tx_done; |
| 1035 | } |
| 1036 | |
| 1037 | /* Invalidate sleep/wake TCS */ |
| 1038 | if (msg->invalidate) { |
| 1039 | ret = tcs_mbox_invalidate(chan); |
| 1040 | goto tx_done; |
| 1041 | } |
| 1042 | |
| 1043 | /* Control slots are unique. They carry specific data. */ |
| 1044 | if (msg->is_control) { |
| 1045 | ret = tcs_control_write(chan, msg); |
| 1046 | goto tx_done; |
| 1047 | } |
| 1048 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1049 | /* Post the message to the TCS without trigger */ |
| 1050 | ret = tcs_mbox_write(chan, msg, false); |
| 1051 | |
| 1052 | tx_done: |
| 1053 | return ret; |
| 1054 | } |
| 1055 | |
| 1056 | static int chan_init(struct mbox_chan *chan) |
| 1057 | { |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | static void chan_shutdown(struct mbox_chan *chan) |
| 1062 | { } |
| 1063 | |
| 1064 | static const struct mbox_chan_ops mbox_ops = { |
| 1065 | .send_data = chan_tcs_write, |
| 1066 | .send_controller_data = chan_tcs_ctrl_write, |
| 1067 | .startup = chan_init, |
| 1068 | .shutdown = chan_shutdown, |
| 1069 | }; |
| 1070 | |
| 1071 | static struct mbox_chan *of_tcs_mbox_xlate(struct mbox_controller *mbox, |
| 1072 | const struct of_phandle_args *sp) |
| 1073 | { |
| 1074 | struct tcs_drv *drv = container_of(mbox, struct tcs_drv, mbox); |
| 1075 | struct mbox_chan *chan; |
| 1076 | |
| 1077 | if (drv->num_assigned >= mbox->num_chans) { |
| 1078 | pr_err("TCS-Mbox out of channel memory\n"); |
| 1079 | return ERR_PTR(-ENOMEM); |
| 1080 | } |
| 1081 | |
| 1082 | chan = &mbox->chans[drv->num_assigned++]; |
Lina Iyer | 1442465 | 2017-06-03 15:55:25 -0600 | [diff] [blame] | 1083 | chan->con_priv = drv; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1084 | |
| 1085 | return chan; |
| 1086 | } |
| 1087 | |
| 1088 | static int tcs_drv_probe(struct platform_device *pdev) |
| 1089 | { |
| 1090 | struct device_node *dn = pdev->dev.of_node; |
| 1091 | struct device_node *np; |
| 1092 | struct tcs_drv *drv; |
| 1093 | struct mbox_chan *chans; |
| 1094 | struct tcs_mbox *tcs; |
| 1095 | struct of_phandle_args p; |
| 1096 | int irq; |
| 1097 | u32 val[8] = { 0 }; |
| 1098 | int num_chans = 0; |
| 1099 | int st = 0; |
| 1100 | int i, j, ret, nelem; |
| 1101 | u32 config, max_tcs, ncpt; |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1102 | int tcs_type_count[TCS_TYPE_NR] = { 0 }; |
| 1103 | struct resource *res; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1104 | |
| 1105 | drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL); |
| 1106 | if (!drv) |
| 1107 | return -ENOMEM; |
| 1108 | |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1109 | ret = of_property_read_u32(dn, "qcom,drv-id", &drv->drv_id); |
| 1110 | if (ret) |
| 1111 | return ret; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1112 | |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1113 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1114 | if (!res) |
| 1115 | return -EINVAL; |
| 1116 | drv->base = devm_ioremap_resource(&pdev->dev, res); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1117 | if (IS_ERR(drv->base)) |
| 1118 | return PTR_ERR(drv->base); |
| 1119 | |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1120 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1121 | if (!res) |
| 1122 | return -EINVAL; |
| 1123 | drv->reg_base = devm_ioremap_resource(&pdev->dev, res); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1124 | if (IS_ERR(drv->reg_base)) |
| 1125 | return PTR_ERR(drv->reg_base); |
| 1126 | |
| 1127 | config = read_drv_config(drv->base); |
| 1128 | max_tcs = config & (DRV_NUM_TCS_MASK << |
| 1129 | (DRV_NUM_TCS_SHIFT * drv->drv_id)); |
| 1130 | max_tcs = max_tcs >> (DRV_NUM_TCS_SHIFT * drv->drv_id); |
| 1131 | ncpt = config & (DRV_NCPT_MASK << DRV_NCPT_SHIFT); |
| 1132 | ncpt = ncpt >> DRV_NCPT_SHIFT; |
| 1133 | |
| 1134 | nelem = of_property_count_elems_of_size(dn, "qcom,tcs-config", |
| 1135 | sizeof(u32)); |
| 1136 | if (!nelem || (nelem % 2) || (nelem > 2 * TCS_TYPE_NR)) |
| 1137 | return -EINVAL; |
| 1138 | |
| 1139 | ret = of_property_read_u32_array(dn, "qcom,tcs-config", val, nelem); |
| 1140 | if (ret) |
| 1141 | return ret; |
| 1142 | |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1143 | /* Ensure we have exactly not more than one of each type in DT */ |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1144 | for (i = 0; i < (nelem / 2); i++) { |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1145 | if (val[2 * i] >= TCS_TYPE_NR) |
| 1146 | return -EINVAL; |
| 1147 | tcs_type_count[val[2 * i]]++; |
| 1148 | if (tcs_type_count[val[2 * i]] > 1) |
| 1149 | return -EINVAL; |
| 1150 | } |
| 1151 | |
| 1152 | /* Ensure we have each type specified in DT */ |
| 1153 | for (i = 0; i < ARRAY_SIZE(tcs_type_count); i++) |
| 1154 | if (!tcs_type_count[i]) |
| 1155 | return -EINVAL; |
| 1156 | |
| 1157 | for (i = 0; i < (nelem / 2); i++) { |
| 1158 | tcs = &drv->tcs[val[2 * i]]; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1159 | tcs->drv = drv; |
| 1160 | tcs->type = val[2 * i]; |
| 1161 | tcs->num_tcs = val[2 * i + 1]; |
| 1162 | tcs->ncpt = (tcs->type == CONTROL_TCS) ? TCS_HIDDEN_MAX_SLOTS |
| 1163 | : ncpt; |
| 1164 | spin_lock_init(&tcs->tcs_lock); |
| 1165 | |
| 1166 | if (tcs->num_tcs <= 0 || tcs->type == CONTROL_TCS) |
| 1167 | continue; |
| 1168 | |
| 1169 | if (tcs->num_tcs > MAX_TCS_PER_TYPE) |
| 1170 | return -EINVAL; |
| 1171 | |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 1172 | if (st + tcs->num_tcs > max_tcs && |
| 1173 | st + tcs->num_tcs >= sizeof(tcs->tcs_mask)) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1174 | return -EINVAL; |
| 1175 | |
| 1176 | tcs->tcs_mask = ((1 << tcs->num_tcs) - 1) << st; |
| 1177 | tcs->tcs_offset = st; |
| 1178 | st += tcs->num_tcs; |
| 1179 | |
| 1180 | tcs->cmd_addr = devm_kzalloc(&pdev->dev, sizeof(u32) * |
| 1181 | tcs->num_tcs * tcs->ncpt, GFP_KERNEL); |
| 1182 | if (!tcs->cmd_addr) |
| 1183 | return -ENOMEM; |
| 1184 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /* Allocate only that many channels specified in DT for our MBOX */ |
| 1188 | for_each_node_with_property(np, "mboxes") { |
| 1189 | if (!of_device_is_available(np)) |
| 1190 | continue; |
| 1191 | i = of_count_phandle_with_args(np, "mboxes", "#mbox-cells"); |
| 1192 | for (j = 0; j < i; j++) { |
| 1193 | ret = of_parse_phandle_with_args(np, "mboxes", |
| 1194 | "#mbox-cells", j, &p); |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 1195 | of_node_put(p.np); |
| 1196 | if (!ret && p.np == pdev->dev.of_node) { |
| 1197 | num_chans++; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1198 | break; |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 1199 | } |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1200 | } |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | if (!num_chans) { |
| 1204 | pr_err("%s: No clients for controller (%s)\n", __func__, |
| 1205 | dn->full_name); |
| 1206 | return -ENODEV; |
| 1207 | } |
| 1208 | |
| 1209 | chans = devm_kzalloc(&pdev->dev, num_chans * sizeof(*chans), |
| 1210 | GFP_KERNEL); |
| 1211 | if (!chans) |
| 1212 | return -ENOMEM; |
| 1213 | |
| 1214 | for (i = 0; i < num_chans; i++) { |
| 1215 | chans[i].mbox = &drv->mbox; |
| 1216 | chans[i].txdone_method = TXDONE_BY_IRQ; |
| 1217 | } |
| 1218 | |
| 1219 | drv->mbox.dev = &pdev->dev; |
| 1220 | drv->mbox.ops = &mbox_ops; |
| 1221 | drv->mbox.chans = chans; |
| 1222 | drv->mbox.num_chans = num_chans; |
| 1223 | drv->mbox.txdone_irq = true; |
| 1224 | drv->mbox.of_xlate = of_tcs_mbox_xlate; |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 1225 | drv->mbox.is_idle = tcs_drv_is_idle; |
Lina Iyer | e2f4272 | 2017-07-13 12:37:50 -0600 | [diff] [blame] | 1226 | drv->mbox.debug = chan_debug; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1227 | drv->num_tcs = st; |
| 1228 | drv->pdev = pdev; |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 1229 | INIT_LIST_HEAD(&drv->response_pending); |
| 1230 | spin_lock_init(&drv->drv_lock); |
| 1231 | tasklet_init(&drv->tasklet, tcs_notify_tx_done, (unsigned long)drv); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1232 | |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 1233 | drv->name = of_get_property(pdev->dev.of_node, "label", NULL); |
| 1234 | if (!drv->name) |
| 1235 | drv->name = dev_name(&pdev->dev); |
| 1236 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1237 | ret = tcs_response_pool_init(drv); |
| 1238 | if (ret) |
| 1239 | return ret; |
| 1240 | |
| 1241 | irq = of_irq_get(dn, 0); |
| 1242 | if (irq < 0) |
| 1243 | return irq; |
| 1244 | |
Lina Iyer | afcdc18 | 2017-04-19 18:31:04 -0600 | [diff] [blame] | 1245 | ret = devm_request_irq(&pdev->dev, irq, tcs_irq_handler, |
Lina Iyer | 9837a42 | 2017-04-26 10:06:21 -0600 | [diff] [blame] | 1246 | IRQF_TRIGGER_HIGH | IRQF_NO_SUSPEND, |
Lina Iyer | 81981ec | 2017-06-26 17:08:45 -0600 | [diff] [blame] | 1247 | drv->name, drv); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1248 | if (ret) |
| 1249 | return ret; |
| 1250 | |
Lina Iyer | e0ef521 | 2017-04-28 09:55:32 -0600 | [diff] [blame] | 1251 | /* Enable interrupts for AMC TCS */ |
| 1252 | write_tcs_reg(drv->reg_base, TCS_DRV_IRQ_ENABLE, 0, 0, |
| 1253 | drv->tcs[ACTIVE_TCS].tcs_mask); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1254 | |
Lina Iyer | b68814f | 2017-04-14 12:49:07 -0600 | [diff] [blame] | 1255 | for (i = 0; i < ARRAY_SIZE(drv->tcs_in_use); i++) |
| 1256 | atomic_set(&drv->tcs_in_use[i], 0); |
| 1257 | |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 1258 | drv->ipc_log_ctx = ipc_log_context_create(TCS_DRV_IPC_LOG_SIZE, |
| 1259 | drv->name, 0); |
| 1260 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1261 | ret = mbox_controller_register(&drv->mbox); |
| 1262 | if (ret) |
| 1263 | return ret; |
| 1264 | |
| 1265 | pr_debug("Mailbox controller (%s, drv=%d) registered\n", |
| 1266 | dn->full_name, drv->drv_id); |
| 1267 | |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | static const struct of_device_id tcs_drv_match[] = { |
| 1272 | { .compatible = "qcom,tcs-drv", }, |
| 1273 | { } |
| 1274 | }; |
| 1275 | |
| 1276 | static struct platform_driver tcs_mbox_driver = { |
| 1277 | .probe = tcs_drv_probe, |
| 1278 | .driver = { |
| 1279 | .name = KBUILD_MODNAME, |
| 1280 | .of_match_table = tcs_drv_match, |
| 1281 | }, |
| 1282 | }; |
| 1283 | |
| 1284 | static int __init tcs_mbox_driver_init(void) |
| 1285 | { |
| 1286 | return platform_driver_register(&tcs_mbox_driver); |
| 1287 | } |
| 1288 | arch_initcall(tcs_mbox_driver_init); |