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 | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 44 | #define RSC_DRV_IPC_LOG_SIZE 2 |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 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 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 50 | #define RSC_DRV_TCS_OFFSET 672 |
| 51 | #define RSC_DRV_CMD_OFFSET 20 |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 61 | #define RSC_DRV_IRQ_ENABLE 0x00 |
| 62 | #define RSC_DRV_IRQ_STATUS 0x04 |
| 63 | #define RSC_DRV_IRQ_CLEAR 0x08 |
| 64 | #define RSC_DRV_CMD_WAIT_FOR_CMPL 0x10 |
| 65 | #define RSC_DRV_CONTROL 0x14 |
| 66 | #define RSC_DRV_STATUS 0x18 |
| 67 | #define RSC_DRV_CMD_ENABLE 0x1C |
| 68 | #define RSC_DRV_CMD_MSGID 0x30 |
| 69 | #define RSC_DRV_CMD_ADDR 0x34 |
| 70 | #define RSC_DRV_CMD_DATA 0x38 |
| 71 | #define RSC_DRV_CMD_STATUS 0x3C |
| 72 | #define RSC_DRV_CMD_RESP_DATA 0x40 |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 93 | struct rsc_drv; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 94 | |
| 95 | struct tcs_response { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 96 | struct rsc_drv *drv; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 114 | struct rsc_drv *drv; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 126 | struct rsc_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 | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 175 | static int tcs_response_pool_init(struct rsc_drv *drv) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 197 | static struct tcs_response *setup_response(struct rsc_drv *drv, |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 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 | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 236 | static inline struct tcs_response *get_response(struct rsc_drv *drv, u32 m, |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 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 | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 262 | static void print_response(struct rsc_drv *drv, int m) |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 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 + |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 291 | RSC_DRV_TCS_OFFSET * m + RSC_DRV_CMD_OFFSET * n)); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 + |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 298 | RSC_DRV_TCS_OFFSET * m + RSC_DRV_CMD_OFFSET * n); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 312 | static inline bool tcs_is_free(struct rsc_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 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 316 | return read_tcs_reg(base, RSC_DRV_STATUS, m, 0) && |
Lina Iyer | b68814f | 2017-04-14 12:49:07 -0600 | [diff] [blame] | 317 | !atomic_read(&drv->tcs_in_use[m]); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 318 | } |
| 319 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 320 | static inline struct tcs_mbox *get_tcs_from_index(struct rsc_drv *drv, int m) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 339 | static inline struct tcs_mbox *get_tcs_of_type(struct rsc_drv *drv, int type) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 358 | static inline struct tcs_mbox *get_tcs_for_msg(struct rsc_drv *drv, |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 397 | struct rsc_drv *drv = resp->drv; |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 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 | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 408 | static inline void enable_tcs_irq(struct rsc_drv *drv, int m, bool enable) |
Lina Iyer | e0ef521 | 2017-04-28 09:55:32 -0600 | [diff] [blame] | 409 | { |
| 410 | void __iomem *base = drv->reg_base; |
| 411 | u32 data; |
| 412 | |
| 413 | /* Enable interrupts for non-ACTIVE TCS */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 414 | data = read_tcs_reg(base, RSC_DRV_IRQ_ENABLE, 0, 0); |
Lina Iyer | e0ef521 | 2017-04-28 09:55:32 -0600 | [diff] [blame] | 415 | if (enable) |
| 416 | data |= BIT(m); |
| 417 | else |
| 418 | data &= ~BIT(m); |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 419 | write_tcs_reg(base, RSC_DRV_IRQ_ENABLE, 0, 0, data); |
Lina Iyer | e0ef521 | 2017-04-28 09:55:32 -0600 | [diff] [blame] | 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 | { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 427 | struct rsc_drv *drv = p; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 437 | irq_status = read_tcs_reg(base, RSC_DRV_IRQ_STATUS, 0, 0); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 454 | sts = read_tcs_reg(base, RSC_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 */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 466 | data = read_tcs_reg(base, RSC_DRV_CMD_RESP_DATA, m, 0); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 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 | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 477 | data = read_tcs_reg(base, RSC_DRV_CONTROL, m, 0); |
Lina Iyer | d3435fa | 2017-08-08 15:42:10 -0600 | [diff] [blame] | 478 | data &= ~TCS_AMC_MODE_TRIGGER; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 479 | write_tcs_reg_sync(base, RSC_DRV_CONTROL, m, 0, data); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 480 | data &= ~TCS_AMC_MODE_ENABLE; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 481 | write_tcs_reg(base, RSC_DRV_CONTROL, m, 0, data); |
Lina Iyer | e0ef521 | 2017-04-28 09:55:32 -0600 | [diff] [blame] | 482 | /* |
| 483 | * Disable interrupt for this TCS to avoid being |
| 484 | * spammed with interrupts coming when the solver |
| 485 | * sends its wake votes. |
| 486 | */ |
| 487 | enable_tcs_irq(drv, m, false); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 488 | } else { |
| 489 | /* Clear the enable bit for the commands */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 490 | write_tcs_reg(base, RSC_DRV_CMD_ENABLE, m, 0, 0); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 491 | } |
| 492 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 493 | no_resp: |
| 494 | /* Record the recvd time stamp */ |
| 495 | drv->tcs_last_recv_ts[m] = arch_counter_get_cntvct(); |
| 496 | |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 497 | /* Clear the TCS IRQ status */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 498 | write_tcs_reg(base, RSC_DRV_IRQ_CLEAR, 0, 0, BIT(m)); |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 499 | |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 500 | /* Notify the client that this request is completed. */ |
| 501 | atomic_set(&drv->tcs_in_use[m], 0); |
| 502 | |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 503 | /* Clean up response object and notify mbox in tasklet */ |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 504 | if (resp) |
| 505 | send_tcs_response(resp); |
Lina Iyer | b68814f | 2017-04-14 12:49:07 -0600 | [diff] [blame] | 506 | } |
| 507 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 508 | return IRQ_HANDLED; |
| 509 | } |
| 510 | |
| 511 | static inline void mbox_notify_tx_done(struct mbox_chan *chan, |
| 512 | struct tcs_mbox_msg *msg, int m, int err) |
| 513 | { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 514 | struct rsc_drv *drv = container_of(chan->mbox, struct rsc_drv, mbox); |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 515 | |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 516 | log_rpmh_notify(drv, m, msg->payload[0].addr, err); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 517 | mbox_chan_txdone(chan, err); |
| 518 | } |
| 519 | |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 520 | static void respond_tx_done(struct tcs_response *resp) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 521 | { |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 522 | struct mbox_chan *chan = resp->chan; |
| 523 | struct tcs_mbox_msg *msg = resp->msg; |
| 524 | int err = resp->err; |
| 525 | int m = resp->m; |
| 526 | |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 527 | free_response(resp); |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 528 | mbox_notify_tx_done(chan, msg, m, err); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * tcs_notify_tx_done: TX Done for requests that do not trigger TCS |
| 533 | */ |
| 534 | static void tcs_notify_tx_done(unsigned long data) |
| 535 | { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 536 | struct rsc_drv *drv = (struct rsc_drv *)data; |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 537 | struct tcs_response *resp; |
| 538 | unsigned long flags; |
| 539 | |
| 540 | do { |
| 541 | spin_lock_irqsave(&drv->drv_lock, flags); |
| 542 | if (list_empty(&drv->response_pending)) { |
| 543 | spin_unlock_irqrestore(&drv->drv_lock, flags); |
| 544 | break; |
| 545 | } |
| 546 | resp = list_first_entry(&drv->response_pending, |
| 547 | struct tcs_response, list); |
| 548 | list_del(&resp->list); |
| 549 | spin_unlock_irqrestore(&drv->drv_lock, flags); |
| 550 | respond_tx_done(resp); |
| 551 | } while (1); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 552 | } |
| 553 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 554 | static void __tcs_buffer_write(struct rsc_drv *drv, int d, int m, int n, |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 555 | struct tcs_mbox_msg *msg, bool trigger) |
| 556 | { |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 557 | u32 msgid, cmd_msgid = 0; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 558 | u32 cmd_enable = 0; |
| 559 | u32 cmd_complete; |
Lina Iyer | d3435fa | 2017-08-08 15:42:10 -0600 | [diff] [blame] | 560 | u32 enable; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 561 | struct tcs_cmd *cmd; |
| 562 | int i; |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 563 | void __iomem *base = drv->reg_base; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 564 | |
| 565 | /* We have homologous command set i.e pure read or write, not a mix */ |
| 566 | cmd_msgid = CMD_MSGID_LEN; |
| 567 | cmd_msgid |= (msg->is_complete) ? CMD_MSGID_RESP_REQ : 0; |
| 568 | cmd_msgid |= (!msg->is_read) ? CMD_MSGID_WRITE : 0; |
| 569 | |
| 570 | /* Read the send-after-prev complete flag for those already in TCS */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 571 | cmd_complete = read_tcs_reg(base, RSC_DRV_CMD_WAIT_FOR_CMPL, m, 0); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 572 | |
| 573 | for (i = 0; i < msg->num_payload; i++) { |
| 574 | cmd = &msg->payload[i]; |
| 575 | cmd_enable |= BIT(n + i); |
| 576 | cmd_complete |= cmd->complete << (n + i); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 577 | msgid = cmd_msgid; |
| 578 | msgid |= (cmd->complete) ? CMD_MSGID_RESP_REQ : 0; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 579 | write_tcs_reg(base, RSC_DRV_CMD_MSGID, m, n + i, msgid); |
| 580 | write_tcs_reg(base, RSC_DRV_CMD_ADDR, m, n + i, cmd->addr); |
| 581 | write_tcs_reg(base, RSC_DRV_CMD_DATA, m, n + i, cmd->data); |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 582 | log_send_msg(drv, m, n + i, msgid, cmd->addr, |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 583 | cmd->data, cmd->complete, trigger); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* Write the send-after-prev completion bits for the batch */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 587 | write_tcs_reg(base, RSC_DRV_CMD_WAIT_FOR_CMPL, m, 0, cmd_complete); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 588 | |
| 589 | /* Enable the new commands in TCS */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 590 | cmd_enable |= read_tcs_reg(base, RSC_DRV_CMD_ENABLE, m, 0); |
| 591 | write_tcs_reg(base, RSC_DRV_CMD_ENABLE, m, 0, cmd_enable); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 592 | |
| 593 | if (trigger) { |
Lina Iyer | d3435fa | 2017-08-08 15:42:10 -0600 | [diff] [blame] | 594 | /* |
| 595 | * HW req: Clear the DRV_CONTROL and enable TCS again |
| 596 | * While clearing ensure that the AMC mode trigger is cleared |
| 597 | * and then the mode enable is cleared. |
| 598 | */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 599 | enable = read_tcs_reg(base, RSC_DRV_CONTROL, m, 0); |
Lina Iyer | d3435fa | 2017-08-08 15:42:10 -0600 | [diff] [blame] | 600 | enable &= ~TCS_AMC_MODE_TRIGGER; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 601 | write_tcs_reg_sync(base, RSC_DRV_CONTROL, m, 0, enable); |
Lina Iyer | d3435fa | 2017-08-08 15:42:10 -0600 | [diff] [blame] | 602 | enable &= ~TCS_AMC_MODE_ENABLE; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 603 | write_tcs_reg_sync(base, RSC_DRV_CONTROL, m, 0, enable); |
Lina Iyer | d3435fa | 2017-08-08 15:42:10 -0600 | [diff] [blame] | 604 | |
| 605 | /* Enable the AMC mode on the TCS and then trigger the TCS */ |
| 606 | enable = TCS_AMC_MODE_ENABLE; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 607 | write_tcs_reg_sync(base, RSC_DRV_CONTROL, m, 0, enable); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 608 | enable |= TCS_AMC_MODE_TRIGGER; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 609 | write_tcs_reg(base, RSC_DRV_CONTROL, m, 0, enable); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 613 | /** |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 614 | * rsc_drv_is_idle: Check if any of the AMCs are busy. |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 615 | * |
| 616 | * @mbox: The mailbox controller. |
| 617 | * |
| 618 | * Returns true if the AMCs are not engaged or absent. |
| 619 | */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 620 | static bool rsc_drv_is_idle(struct mbox_controller *mbox) |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 621 | { |
| 622 | int m; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 623 | struct rsc_drv *drv = container_of(mbox, struct rsc_drv, mbox); |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 624 | struct tcs_mbox *tcs = get_tcs_of_type(drv, ACTIVE_TCS); |
| 625 | |
Lina Iyer | 21c1788 | 2016-09-22 11:05:51 -0600 | [diff] [blame] | 626 | /* Check for WAKE TCS if there are no ACTIVE TCS */ |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 627 | if (IS_ERR(tcs)) |
Lina Iyer | 21c1788 | 2016-09-22 11:05:51 -0600 | [diff] [blame] | 628 | tcs = get_tcs_of_type(drv, WAKE_TCS); |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 629 | |
| 630 | 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] | 631 | if (!tcs_is_free(drv, m)) |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 632 | return false; |
| 633 | |
| 634 | return true; |
| 635 | } |
| 636 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 637 | static int check_for_req_inflight(struct rsc_drv *drv, struct tcs_mbox *tcs, |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 638 | struct tcs_mbox_msg *msg) |
| 639 | { |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 640 | u32 curr_enabled, addr; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 641 | int i, j, k; |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 642 | void __iomem *base = drv->reg_base; |
| 643 | int m = tcs->tcs_offset; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 644 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 645 | for (i = 0; i < tcs->num_tcs; i++, m++) { |
| 646 | if (tcs_is_free(drv, m)) |
| 647 | continue; |
| 648 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 649 | curr_enabled = read_tcs_reg(base, RSC_DRV_CMD_ENABLE, m, 0); |
Lina Iyer | b465ea6 | 2017-05-03 15:27:22 -0600 | [diff] [blame] | 650 | |
| 651 | for (j = 0; j < MAX_CMDS_PER_TCS; j++) { |
| 652 | if (!(curr_enabled & (u32)BIT(j))) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 653 | continue; |
Lina Iyer | b465ea6 | 2017-05-03 15:27:22 -0600 | [diff] [blame] | 654 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 655 | addr = read_tcs_reg(base, RSC_DRV_CMD_ADDR, m, j); |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 656 | for (k = 0; k < msg->num_payload; k++) { |
| 657 | if (addr == msg->payload[k].addr) |
| 658 | return -EBUSY; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 659 | } |
| 660 | } |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | return 0; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | static int find_free_tcs(struct tcs_mbox *tcs) |
| 667 | { |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 668 | int slot = -EBUSY; |
| 669 | int m = 0; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 670 | |
| 671 | /* Loop until we find a free AMC */ |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 672 | for (m = 0; m < tcs->num_tcs; m++) { |
Lina Iyer | b68814f | 2017-04-14 12:49:07 -0600 | [diff] [blame] | 673 | if (tcs_is_free(tcs->drv, tcs->tcs_offset + m)) { |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 674 | slot = m * tcs->ncpt; |
| 675 | break; |
| 676 | } |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 677 | } |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 678 | |
| 679 | return slot; |
| 680 | } |
| 681 | |
| 682 | static int find_match(struct tcs_mbox *tcs, struct tcs_cmd *cmd, int len) |
| 683 | { |
| 684 | bool found = false; |
| 685 | int i = 0, j; |
| 686 | |
| 687 | /* Check for already cached commands */ |
| 688 | while ((i = find_next_bit(tcs->slots, MAX_TCS_SLOTS, i)) < |
| 689 | MAX_TCS_SLOTS) { |
| 690 | if (tcs->cmd_addr[i] != cmd[0].addr) { |
| 691 | i++; |
| 692 | continue; |
| 693 | } |
| 694 | /* sanity check to ensure the seq is same */ |
| 695 | for (j = 1; j < len; j++) { |
| 696 | WARN((tcs->cmd_addr[i + j] != cmd[j].addr), |
| 697 | "Message does not match previous sequence.\n"); |
| 698 | return -EINVAL; |
| 699 | } |
| 700 | found = true; |
| 701 | break; |
| 702 | } |
| 703 | |
| 704 | return found ? i : -1; |
| 705 | } |
| 706 | |
| 707 | static int find_slots(struct tcs_mbox *tcs, struct tcs_mbox_msg *msg) |
| 708 | { |
| 709 | int slot; |
| 710 | int n = 0; |
| 711 | |
| 712 | /* For active requests find the first free AMC. */ |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 713 | if (msg->state == RPMH_ACTIVE_ONLY_STATE || |
| 714 | msg->state == RPMH_AWAKE_STATE) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 715 | return find_free_tcs(tcs); |
| 716 | |
| 717 | /* Find if we already have the msg in our TCS */ |
| 718 | slot = find_match(tcs, msg->payload, msg->num_payload); |
| 719 | if (slot >= 0) |
| 720 | return slot; |
| 721 | |
| 722 | /* Do over, until we can fit the full payload in a TCS */ |
| 723 | do { |
| 724 | slot = bitmap_find_next_zero_area(tcs->slots, MAX_TCS_SLOTS, |
| 725 | n, msg->num_payload, 0); |
| 726 | if (slot == MAX_TCS_SLOTS) |
| 727 | break; |
| 728 | n += tcs->ncpt; |
| 729 | } while (slot + msg->num_payload - 1 >= n); |
| 730 | |
| 731 | return (slot != MAX_TCS_SLOTS) ? slot : -ENOMEM; |
| 732 | } |
| 733 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 734 | static int tcs_mbox_write(struct mbox_chan *chan, struct tcs_mbox_msg *msg, |
| 735 | bool trigger) |
| 736 | { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 737 | struct rsc_drv *drv = container_of(chan->mbox, struct rsc_drv, mbox); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 738 | int d = drv->drv_id; |
| 739 | struct tcs_mbox *tcs; |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 740 | int i, slot, offset, m, n, ret; |
Channagoud Kadabi | 075db3b | 2017-03-16 14:26:17 -0700 | [diff] [blame] | 741 | struct tcs_response *resp = NULL; |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 742 | unsigned long flags; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 743 | |
| 744 | tcs = get_tcs_for_msg(drv, msg); |
| 745 | if (IS_ERR(tcs)) |
| 746 | return PTR_ERR(tcs); |
| 747 | |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 748 | if (trigger) { |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 749 | resp = setup_response(drv, msg, chan, TCS_M_INIT, 0); |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 750 | if (IS_ERR_OR_NULL(resp)) |
| 751 | return -EBUSY; |
| 752 | } |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 753 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 754 | /* Identify the sequential slots that we can write to */ |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 755 | spin_lock_irqsave(&tcs->tcs_lock, flags); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 756 | slot = find_slots(tcs, msg); |
| 757 | if (slot < 0) { |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 758 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 759 | if (resp) |
| 760 | free_response(resp); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 761 | return slot; |
| 762 | } |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 763 | |
Lina Iyer | 1442465 | 2017-06-03 15:55:25 -0600 | [diff] [blame] | 764 | /* Figure out the TCS-m and CMD-n to write to */ |
| 765 | offset = slot / tcs->ncpt; |
| 766 | m = offset + tcs->tcs_offset; |
| 767 | n = slot % tcs->ncpt; |
| 768 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 769 | if (trigger) { |
Lina Iyer | 1442465 | 2017-06-03 15:55:25 -0600 | [diff] [blame] | 770 | /* Block, if we have an address from the msg in flight */ |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 771 | ret = check_for_req_inflight(drv, tcs, msg); |
| 772 | if (ret) { |
| 773 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 774 | if (resp) |
| 775 | free_response(resp); |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 776 | return ret; |
| 777 | } |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 778 | |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 779 | resp->m = m; |
| 780 | /* Mark the TCS as busy */ |
| 781 | atomic_set(&drv->tcs_in_use[m], 1); |
Lina Iyer | 088ccec | 2017-04-24 20:18:48 -0600 | [diff] [blame] | 782 | atomic_inc(&drv->tcs_send_count[m]); |
Lina Iyer | e0ef521 | 2017-04-28 09:55:32 -0600 | [diff] [blame] | 783 | /* Enable interrupt for active votes through wake TCS */ |
| 784 | if (tcs->type != ACTIVE_TCS) |
| 785 | enable_tcs_irq(drv, m, true); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 786 | drv->tcs_last_sent_ts[m] = arch_counter_get_cntvct(); |
Lina Iyer | 1442465 | 2017-06-03 15:55:25 -0600 | [diff] [blame] | 787 | } else { |
| 788 | /* Mark the slots as in-use, before we unlock */ |
| 789 | if (tcs->type == SLEEP_TCS || tcs->type == WAKE_TCS) |
| 790 | bitmap_set(tcs->slots, slot, msg->num_payload); |
| 791 | |
| 792 | /* Copy the addresses of the resources over to the slots */ |
| 793 | for (i = 0; tcs->cmd_addr && i < msg->num_payload; i++) |
| 794 | tcs->cmd_addr[slot + i] = msg->payload[i].addr; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /* Write to the TCS or AMC */ |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 798 | __tcs_buffer_write(drv, d, m, n, msg, trigger); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 799 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 800 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 805 | static void __tcs_buffer_invalidate(void __iomem *base, int m) |
| 806 | { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 807 | write_tcs_reg(base, RSC_DRV_CMD_ENABLE, m, 0, 0); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | static int tcs_mbox_invalidate(struct mbox_chan *chan) |
| 811 | { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 812 | struct rsc_drv *drv = container_of(chan->mbox, struct rsc_drv, mbox); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 813 | struct tcs_mbox *tcs; |
| 814 | int m, i; |
| 815 | int inv_types[] = { WAKE_TCS, SLEEP_TCS }; |
| 816 | int type = 0; |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 817 | unsigned long flags; |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 818 | |
| 819 | do { |
| 820 | tcs = get_tcs_of_type(drv, inv_types[type]); |
| 821 | if (IS_ERR(tcs)) |
| 822 | return PTR_ERR(tcs); |
| 823 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 824 | spin_lock_irqsave(&tcs->tcs_lock, flags); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 825 | for (i = 0; i < tcs->num_tcs; i++) { |
| 826 | m = i + tcs->tcs_offset; |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 827 | if (!tcs_is_free(drv, m)) { |
| 828 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
| 829 | return -EBUSY; |
| 830 | } |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 831 | __tcs_buffer_invalidate(drv->reg_base, m); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 832 | } |
| 833 | /* Mark the TCS as free */ |
| 834 | bitmap_zero(tcs->slots, MAX_TCS_SLOTS); |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 835 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 836 | } while (++type < ARRAY_SIZE(inv_types)); |
| 837 | |
| 838 | return 0; |
| 839 | } |
| 840 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 841 | static void print_tcs_regs(struct rsc_drv *drv, int m) |
Lina Iyer | 035ddd3 | 2017-05-30 22:11:53 -0600 | [diff] [blame] | 842 | { |
| 843 | int n; |
| 844 | struct tcs_mbox *tcs = get_tcs_from_index(drv, m); |
| 845 | void __iomem *base = drv->reg_base; |
Lina Iyer | d9abfa6 | 2017-07-27 17:23:05 -0600 | [diff] [blame] | 846 | u32 enable, addr, data, msgid, sts, irq_sts; |
Lina Iyer | 035ddd3 | 2017-05-30 22:11:53 -0600 | [diff] [blame] | 847 | |
| 848 | if (!tcs || tcs_is_free(drv, m)) |
| 849 | return; |
| 850 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 851 | enable = read_tcs_reg(base, RSC_DRV_CMD_ENABLE, m, 0); |
Lina Iyer | 035ddd3 | 2017-05-30 22:11:53 -0600 | [diff] [blame] | 852 | if (!enable) |
| 853 | return; |
| 854 | |
Lina Iyer | d9abfa6 | 2017-07-27 17:23:05 -0600 | [diff] [blame] | 855 | pr_warn("RSC:%s\n", drv->name); |
| 856 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 857 | sts = read_tcs_reg(base, RSC_DRV_STATUS, m, 0); |
| 858 | data = read_tcs_reg(base, RSC_DRV_CONTROL, m, 0); |
| 859 | irq_sts = read_tcs_reg(base, RSC_DRV_IRQ_STATUS, 0, 0); |
Lina Iyer | d9abfa6 | 2017-07-27 17:23:05 -0600 | [diff] [blame] | 860 | pr_warn("TCS=%d [ctrlr-sts:%s amc-mode:0x%x irq-sts:%s]\n", |
| 861 | m, sts ? "IDLE" : "BUSY", data, |
| 862 | (irq_sts & BIT(m)) ? "COMPLETED" : "PENDING"); |
| 863 | |
Lina Iyer | 035ddd3 | 2017-05-30 22:11:53 -0600 | [diff] [blame] | 864 | for (n = 0; n < tcs->ncpt; n++) { |
| 865 | if (!(enable & BIT(n))) |
| 866 | continue; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 867 | addr = read_tcs_reg(base, RSC_DRV_CMD_ADDR, m, n); |
| 868 | data = read_tcs_reg(base, RSC_DRV_CMD_DATA, m, n); |
| 869 | msgid = read_tcs_reg(base, RSC_DRV_CMD_MSGID, m, n); |
| 870 | sts = read_tcs_reg(base, RSC_DRV_CMD_STATUS, m, n); |
Lina Iyer | d9abfa6 | 2017-07-27 17:23:05 -0600 | [diff] [blame] | 871 | pr_warn("\tCMD=%d [addr=0x%x data=0x%x hdr=0x%x sts=0x%x]\n", |
| 872 | n, addr, data, msgid, sts); |
Lina Iyer | 035ddd3 | 2017-05-30 22:11:53 -0600 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 876 | static void dump_tcs_stats(struct rsc_drv *drv) |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 877 | { |
| 878 | int i; |
| 879 | unsigned long long curr = arch_counter_get_cntvct(); |
| 880 | |
| 881 | for (i = 0; i < drv->num_tcs; i++) { |
| 882 | if (!atomic_read(&drv->tcs_in_use[i])) |
| 883 | continue; |
Lina Iyer | e2f4272 | 2017-07-13 12:37:50 -0600 | [diff] [blame] | 884 | 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] | 885 | curr, i, |
| 886 | atomic_read(&drv->tcs_send_count[i]), |
| 887 | drv->tcs_last_sent_ts[i], |
| 888 | atomic_read(&drv->tcs_irq_count[i]), |
| 889 | drv->tcs_last_recv_ts[i]); |
Lina Iyer | 035ddd3 | 2017-05-30 22:11:53 -0600 | [diff] [blame] | 890 | print_tcs_regs(drv, i); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 891 | print_response(drv, i); |
| 892 | } |
| 893 | } |
| 894 | |
Lina Iyer | e2f4272 | 2017-07-13 12:37:50 -0600 | [diff] [blame] | 895 | static void chan_debug(struct mbox_chan *chan) |
| 896 | { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 897 | struct rsc_drv *drv = container_of(chan->mbox, struct rsc_drv, mbox); |
Lina Iyer | e2f4272 | 2017-07-13 12:37:50 -0600 | [diff] [blame] | 898 | |
| 899 | dump_tcs_stats(drv); |
| 900 | } |
| 901 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 902 | /** |
| 903 | * chan_tcs_write: Validate the incoming message and write to the |
| 904 | * appropriate TCS block. |
| 905 | * |
| 906 | * @chan: the MBOX channel |
| 907 | * @data: the tcs_mbox_msg* |
| 908 | * |
| 909 | * Returns a negative error for invalid message structure and invalid |
| 910 | * message combination, -EBUSY if there is an other active request for |
| 911 | * the channel in process, otherwise bubbles up internal error. |
| 912 | */ |
| 913 | static int chan_tcs_write(struct mbox_chan *chan, void *data) |
| 914 | { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 915 | struct rsc_drv *drv = container_of(chan->mbox, struct rsc_drv, mbox); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 916 | struct tcs_mbox_msg *msg = data; |
| 917 | const struct device *dev = chan->cl->dev; |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 918 | int ret = 0; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 919 | |
| 920 | if (!msg) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 921 | dev_err(dev, "Payload error\n"); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 922 | ret = -EINVAL; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 923 | goto tx_fail; |
| 924 | } |
| 925 | |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 926 | if (!msg->payload || !msg->num_payload || |
| 927 | msg->num_payload > MAX_RPMH_PAYLOAD) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 928 | dev_err(dev, "Payload error\n"); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 929 | ret = -EINVAL; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 930 | goto tx_fail; |
| 931 | } |
| 932 | |
| 933 | if (msg->invalidate || msg->is_control) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 934 | dev_err(dev, "Incorrect API\n"); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 935 | ret = -EINVAL; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 936 | goto tx_fail; |
| 937 | } |
| 938 | |
Lina Iyer | 21c1788 | 2016-09-22 11:05:51 -0600 | [diff] [blame] | 939 | if (msg->state != RPMH_ACTIVE_ONLY_STATE && |
| 940 | msg->state != RPMH_AWAKE_STATE) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 941 | dev_err(dev, "Incorrect API\n"); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 942 | ret = -EINVAL; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 943 | goto tx_fail; |
| 944 | } |
| 945 | |
| 946 | /* Read requests should always be single */ |
| 947 | if (msg->is_read && msg->num_payload > 1) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 948 | dev_err(dev, "Incorrect read request\n"); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 949 | ret = -EINVAL; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 950 | goto tx_fail; |
| 951 | } |
| 952 | |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 953 | /* |
| 954 | * Since we are re-purposing the wake TCS, invalidate previous |
| 955 | * contents to avoid confusion. |
| 956 | */ |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 957 | if (msg->state == RPMH_AWAKE_STATE) { |
| 958 | ret = tcs_mbox_invalidate(chan); |
| 959 | if (ret) |
| 960 | goto tx_fail; |
| 961 | } |
Lina Iyer | 7846e21 | 2017-03-22 10:35:53 -0600 | [diff] [blame] | 962 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 963 | /* Post the message to the TCS and trigger */ |
Lina Iyer | 64834df | 2017-06-09 11:36:46 -0600 | [diff] [blame] | 964 | ret = tcs_mbox_write(chan, msg, true); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 965 | |
| 966 | tx_fail: |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 967 | /* If there was an error in the request, schedule a response */ |
| 968 | if (ret < 0 && ret != -EBUSY) { |
Lina Iyer | c8712ca | 2017-04-20 00:18:49 -0600 | [diff] [blame] | 969 | struct tcs_response *resp = setup_response( |
| 970 | drv, msg, chan, TCS_M_INIT, ret); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 971 | |
| 972 | dev_err(dev, "Error sending RPMH message %d\n", ret); |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 973 | if (!IS_ERR(resp)) |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 974 | send_tcs_response(resp); |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 975 | else |
| 976 | dev_err(dev, "No response object %ld\n", PTR_ERR(resp)); |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 977 | ret = 0; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 978 | } |
| 979 | |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 980 | /* If we were just busy waiting for TCS, dump the state and return */ |
| 981 | if (ret == -EBUSY) { |
Lina Iyer | 9497220 | 2017-06-28 15:37:43 -0600 | [diff] [blame] | 982 | pr_info_ratelimited("TCS Busy, retrying RPMH message send\n"); |
Lina Iyer | 348b271 | 2017-05-30 22:13:59 -0600 | [diff] [blame] | 983 | ret = -EAGAIN; |
Lina Iyer | 51bc14d | 2017-04-28 13:56:12 -0600 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | return ret; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 987 | } |
| 988 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 989 | static void __tcs_write_hidden(struct rsc_drv *drv, int d, |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 990 | struct tcs_mbox_msg *msg) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 991 | { |
| 992 | int i; |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 993 | void __iomem *addr = drv->base + TCS_HIDDEN_CMD0_DRV_DATA; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 994 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 995 | for (i = 0; i < msg->num_payload; i++) { |
| 996 | /* Only data is write capable */ |
Lina Iyer | d93fbce | 2017-03-01 09:07:20 -0700 | [diff] [blame] | 997 | writel_relaxed(cpu_to_le32(msg->payload[i].data), addr); |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 998 | log_rpmh_control_msg(drv, msg->payload[i].data); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 999 | addr += TCS_HIDDEN_CMD_SHIFT; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | static int tcs_control_write(struct mbox_chan *chan, struct tcs_mbox_msg *msg) |
| 1004 | { |
| 1005 | const struct device *dev = chan->cl->dev; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1006 | struct rsc_drv *drv = container_of(chan->mbox, struct rsc_drv, mbox); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1007 | struct tcs_mbox *tcs; |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 1008 | unsigned long flags; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1009 | |
| 1010 | tcs = get_tcs_of_type(drv, CONTROL_TCS); |
| 1011 | if (IS_ERR(tcs)) |
| 1012 | return PTR_ERR(tcs); |
| 1013 | |
| 1014 | if (msg->num_payload != tcs->ncpt) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 1015 | dev_err(dev, "Request must fit the control TCS size\n"); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1016 | return -EINVAL; |
| 1017 | } |
| 1018 | |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 1019 | spin_lock_irqsave(&tcs->tcs_lock, flags); |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 1020 | __tcs_write_hidden(tcs->drv, drv->drv_id, msg); |
Lina Iyer | cc114b2 | 2017-04-26 14:40:44 -0600 | [diff] [blame] | 1021 | spin_unlock_irqrestore(&tcs->tcs_lock, flags); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1022 | |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * chan_tcs_ctrl_write: Write message to the controller, no ACK sent. |
| 1028 | * |
| 1029 | * @chan: the MBOX channel |
| 1030 | * @data: the tcs_mbox_msg* |
| 1031 | */ |
| 1032 | static int chan_tcs_ctrl_write(struct mbox_chan *chan, void *data) |
| 1033 | { |
| 1034 | struct tcs_mbox_msg *msg = data; |
| 1035 | const struct device *dev = chan->cl->dev; |
| 1036 | int ret = -EINVAL; |
| 1037 | |
| 1038 | if (!msg) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 1039 | dev_err(dev, "Payload error\n"); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1040 | goto tx_done; |
| 1041 | } |
| 1042 | |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 1043 | if (!msg->payload || (!msg->num_payload && !msg->invalidate) || |
| 1044 | msg->num_payload > MAX_RPMH_PAYLOAD) { |
Lina Iyer | 5ddea73 | 2017-05-09 14:25:33 -0600 | [diff] [blame] | 1045 | dev_err(dev, "Payload error\n"); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1046 | goto tx_done; |
| 1047 | } |
| 1048 | |
| 1049 | /* Invalidate sleep/wake TCS */ |
| 1050 | if (msg->invalidate) { |
| 1051 | ret = tcs_mbox_invalidate(chan); |
| 1052 | goto tx_done; |
| 1053 | } |
| 1054 | |
| 1055 | /* Control slots are unique. They carry specific data. */ |
| 1056 | if (msg->is_control) { |
| 1057 | ret = tcs_control_write(chan, msg); |
| 1058 | goto tx_done; |
| 1059 | } |
| 1060 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1061 | /* Post the message to the TCS without trigger */ |
| 1062 | ret = tcs_mbox_write(chan, msg, false); |
| 1063 | |
| 1064 | tx_done: |
| 1065 | return ret; |
| 1066 | } |
| 1067 | |
| 1068 | static int chan_init(struct mbox_chan *chan) |
| 1069 | { |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | static void chan_shutdown(struct mbox_chan *chan) |
| 1074 | { } |
| 1075 | |
| 1076 | static const struct mbox_chan_ops mbox_ops = { |
| 1077 | .send_data = chan_tcs_write, |
| 1078 | .send_controller_data = chan_tcs_ctrl_write, |
| 1079 | .startup = chan_init, |
| 1080 | .shutdown = chan_shutdown, |
| 1081 | }; |
| 1082 | |
| 1083 | static struct mbox_chan *of_tcs_mbox_xlate(struct mbox_controller *mbox, |
| 1084 | const struct of_phandle_args *sp) |
| 1085 | { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1086 | struct rsc_drv *drv = container_of(mbox, struct rsc_drv, mbox); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1087 | struct mbox_chan *chan; |
| 1088 | |
| 1089 | if (drv->num_assigned >= mbox->num_chans) { |
| 1090 | pr_err("TCS-Mbox out of channel memory\n"); |
| 1091 | return ERR_PTR(-ENOMEM); |
| 1092 | } |
| 1093 | |
| 1094 | chan = &mbox->chans[drv->num_assigned++]; |
Lina Iyer | 1442465 | 2017-06-03 15:55:25 -0600 | [diff] [blame] | 1095 | chan->con_priv = drv; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1096 | |
| 1097 | return chan; |
| 1098 | } |
| 1099 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1100 | static int rsc_drv_probe(struct platform_device *pdev) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1101 | { |
| 1102 | struct device_node *dn = pdev->dev.of_node; |
| 1103 | struct device_node *np; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1104 | struct rsc_drv *drv; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1105 | struct mbox_chan *chans; |
| 1106 | struct tcs_mbox *tcs; |
| 1107 | struct of_phandle_args p; |
| 1108 | int irq; |
| 1109 | u32 val[8] = { 0 }; |
| 1110 | int num_chans = 0; |
| 1111 | int st = 0; |
| 1112 | int i, j, ret, nelem; |
| 1113 | u32 config, max_tcs, ncpt; |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1114 | int tcs_type_count[TCS_TYPE_NR] = { 0 }; |
| 1115 | struct resource *res; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1116 | |
| 1117 | drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL); |
| 1118 | if (!drv) |
| 1119 | return -ENOMEM; |
| 1120 | |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1121 | ret = of_property_read_u32(dn, "qcom,drv-id", &drv->drv_id); |
| 1122 | if (ret) |
| 1123 | return ret; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1124 | |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1125 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1126 | if (!res) |
| 1127 | return -EINVAL; |
| 1128 | drv->base = devm_ioremap_resource(&pdev->dev, res); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1129 | if (IS_ERR(drv->base)) |
| 1130 | return PTR_ERR(drv->base); |
| 1131 | |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1132 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1133 | if (!res) |
| 1134 | return -EINVAL; |
| 1135 | drv->reg_base = devm_ioremap_resource(&pdev->dev, res); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1136 | if (IS_ERR(drv->reg_base)) |
| 1137 | return PTR_ERR(drv->reg_base); |
| 1138 | |
| 1139 | config = read_drv_config(drv->base); |
| 1140 | max_tcs = config & (DRV_NUM_TCS_MASK << |
| 1141 | (DRV_NUM_TCS_SHIFT * drv->drv_id)); |
| 1142 | max_tcs = max_tcs >> (DRV_NUM_TCS_SHIFT * drv->drv_id); |
| 1143 | ncpt = config & (DRV_NCPT_MASK << DRV_NCPT_SHIFT); |
| 1144 | ncpt = ncpt >> DRV_NCPT_SHIFT; |
| 1145 | |
| 1146 | nelem = of_property_count_elems_of_size(dn, "qcom,tcs-config", |
| 1147 | sizeof(u32)); |
| 1148 | if (!nelem || (nelem % 2) || (nelem > 2 * TCS_TYPE_NR)) |
| 1149 | return -EINVAL; |
| 1150 | |
| 1151 | ret = of_property_read_u32_array(dn, "qcom,tcs-config", val, nelem); |
| 1152 | if (ret) |
| 1153 | return ret; |
| 1154 | |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1155 | /* 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] | 1156 | for (i = 0; i < (nelem / 2); i++) { |
Lina Iyer | 7310142 | 2017-02-16 14:09:25 -0700 | [diff] [blame] | 1157 | if (val[2 * i] >= TCS_TYPE_NR) |
| 1158 | return -EINVAL; |
| 1159 | tcs_type_count[val[2 * i]]++; |
| 1160 | if (tcs_type_count[val[2 * i]] > 1) |
| 1161 | return -EINVAL; |
| 1162 | } |
| 1163 | |
| 1164 | /* Ensure we have each type specified in DT */ |
| 1165 | for (i = 0; i < ARRAY_SIZE(tcs_type_count); i++) |
| 1166 | if (!tcs_type_count[i]) |
| 1167 | return -EINVAL; |
| 1168 | |
| 1169 | for (i = 0; i < (nelem / 2); i++) { |
| 1170 | tcs = &drv->tcs[val[2 * i]]; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1171 | tcs->drv = drv; |
| 1172 | tcs->type = val[2 * i]; |
| 1173 | tcs->num_tcs = val[2 * i + 1]; |
| 1174 | tcs->ncpt = (tcs->type == CONTROL_TCS) ? TCS_HIDDEN_MAX_SLOTS |
| 1175 | : ncpt; |
| 1176 | spin_lock_init(&tcs->tcs_lock); |
| 1177 | |
| 1178 | if (tcs->num_tcs <= 0 || tcs->type == CONTROL_TCS) |
| 1179 | continue; |
| 1180 | |
| 1181 | if (tcs->num_tcs > MAX_TCS_PER_TYPE) |
| 1182 | return -EINVAL; |
| 1183 | |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 1184 | if (st + tcs->num_tcs > max_tcs && |
| 1185 | st + tcs->num_tcs >= sizeof(tcs->tcs_mask)) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1186 | return -EINVAL; |
| 1187 | |
| 1188 | tcs->tcs_mask = ((1 << tcs->num_tcs) - 1) << st; |
| 1189 | tcs->tcs_offset = st; |
| 1190 | st += tcs->num_tcs; |
| 1191 | |
| 1192 | tcs->cmd_addr = devm_kzalloc(&pdev->dev, sizeof(u32) * |
| 1193 | tcs->num_tcs * tcs->ncpt, GFP_KERNEL); |
| 1194 | if (!tcs->cmd_addr) |
| 1195 | return -ENOMEM; |
| 1196 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /* Allocate only that many channels specified in DT for our MBOX */ |
| 1200 | for_each_node_with_property(np, "mboxes") { |
| 1201 | if (!of_device_is_available(np)) |
| 1202 | continue; |
| 1203 | i = of_count_phandle_with_args(np, "mboxes", "#mbox-cells"); |
| 1204 | for (j = 0; j < i; j++) { |
| 1205 | ret = of_parse_phandle_with_args(np, "mboxes", |
| 1206 | "#mbox-cells", j, &p); |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 1207 | of_node_put(p.np); |
| 1208 | if (!ret && p.np == pdev->dev.of_node) { |
| 1209 | num_chans++; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1210 | break; |
Lina Iyer | fd7e0c2 | 2017-07-14 18:08:46 -0600 | [diff] [blame] | 1211 | } |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1212 | } |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | if (!num_chans) { |
| 1216 | pr_err("%s: No clients for controller (%s)\n", __func__, |
| 1217 | dn->full_name); |
| 1218 | return -ENODEV; |
| 1219 | } |
| 1220 | |
| 1221 | chans = devm_kzalloc(&pdev->dev, num_chans * sizeof(*chans), |
| 1222 | GFP_KERNEL); |
| 1223 | if (!chans) |
| 1224 | return -ENOMEM; |
| 1225 | |
| 1226 | for (i = 0; i < num_chans; i++) { |
| 1227 | chans[i].mbox = &drv->mbox; |
| 1228 | chans[i].txdone_method = TXDONE_BY_IRQ; |
| 1229 | } |
| 1230 | |
| 1231 | drv->mbox.dev = &pdev->dev; |
| 1232 | drv->mbox.ops = &mbox_ops; |
| 1233 | drv->mbox.chans = chans; |
| 1234 | drv->mbox.num_chans = num_chans; |
| 1235 | drv->mbox.txdone_irq = true; |
| 1236 | drv->mbox.of_xlate = of_tcs_mbox_xlate; |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1237 | drv->mbox.is_idle = rsc_drv_is_idle; |
Lina Iyer | e2f4272 | 2017-07-13 12:37:50 -0600 | [diff] [blame] | 1238 | drv->mbox.debug = chan_debug; |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1239 | drv->num_tcs = st; |
| 1240 | drv->pdev = pdev; |
Lina Iyer | 00328f1 | 2017-05-30 14:08:49 -0600 | [diff] [blame] | 1241 | INIT_LIST_HEAD(&drv->response_pending); |
| 1242 | spin_lock_init(&drv->drv_lock); |
| 1243 | tasklet_init(&drv->tasklet, tcs_notify_tx_done, (unsigned long)drv); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1244 | |
Lina Iyer | 884981e | 2017-03-21 13:43:05 -0600 | [diff] [blame] | 1245 | drv->name = of_get_property(pdev->dev.of_node, "label", NULL); |
| 1246 | if (!drv->name) |
| 1247 | drv->name = dev_name(&pdev->dev); |
| 1248 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1249 | ret = tcs_response_pool_init(drv); |
| 1250 | if (ret) |
| 1251 | return ret; |
| 1252 | |
| 1253 | irq = of_irq_get(dn, 0); |
| 1254 | if (irq < 0) |
| 1255 | return irq; |
| 1256 | |
Lina Iyer | afcdc18 | 2017-04-19 18:31:04 -0600 | [diff] [blame] | 1257 | ret = devm_request_irq(&pdev->dev, irq, tcs_irq_handler, |
Lina Iyer | 9837a42 | 2017-04-26 10:06:21 -0600 | [diff] [blame] | 1258 | IRQF_TRIGGER_HIGH | IRQF_NO_SUSPEND, |
Lina Iyer | 81981ec | 2017-06-26 17:08:45 -0600 | [diff] [blame] | 1259 | drv->name, drv); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1260 | if (ret) |
| 1261 | return ret; |
| 1262 | |
Lina Iyer | e0ef521 | 2017-04-28 09:55:32 -0600 | [diff] [blame] | 1263 | /* Enable interrupts for AMC TCS */ |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1264 | write_tcs_reg(drv->reg_base, RSC_DRV_IRQ_ENABLE, 0, 0, |
Lina Iyer | e0ef521 | 2017-04-28 09:55:32 -0600 | [diff] [blame] | 1265 | drv->tcs[ACTIVE_TCS].tcs_mask); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1266 | |
Lina Iyer | b68814f | 2017-04-14 12:49:07 -0600 | [diff] [blame] | 1267 | for (i = 0; i < ARRAY_SIZE(drv->tcs_in_use); i++) |
| 1268 | atomic_set(&drv->tcs_in_use[i], 0); |
| 1269 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1270 | drv->ipc_log_ctx = ipc_log_context_create(RSC_DRV_IPC_LOG_SIZE, |
Lina Iyer | 47b708a | 2017-07-21 18:49:00 -0600 | [diff] [blame] | 1271 | drv->name, 0); |
| 1272 | |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1273 | ret = mbox_controller_register(&drv->mbox); |
| 1274 | if (ret) |
| 1275 | return ret; |
| 1276 | |
| 1277 | pr_debug("Mailbox controller (%s, drv=%d) registered\n", |
| 1278 | dn->full_name, drv->drv_id); |
| 1279 | |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1283 | static const struct of_device_id rsc_drv_match[] = { |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1284 | { .compatible = "qcom,tcs-drv", }, |
| 1285 | { } |
| 1286 | }; |
| 1287 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1288 | static struct platform_driver rpmh_mbox_driver = { |
| 1289 | .probe = rsc_drv_probe, |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1290 | .driver = { |
| 1291 | .name = KBUILD_MODNAME, |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1292 | .of_match_table = rsc_drv_match, |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1293 | }, |
| 1294 | }; |
| 1295 | |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1296 | static int __init rpmh_mbox_driver_init(void) |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1297 | { |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1298 | return platform_driver_register(&rpmh_mbox_driver); |
Lina Iyer | 88a8fda | 2016-04-01 08:23:31 -0600 | [diff] [blame] | 1299 | } |
Lina Iyer | ecb04eb | 2017-08-29 15:35:43 -0600 | [diff] [blame] | 1300 | arch_initcall(rpmh_mbox_driver_init); |