Eric Holmberg | 6275b60 | 2012-11-19 13:05:04 -0700 | [diff] [blame] | 1 | /* arch/arm/mach-msm/smp2p_loopback.c |
| 2 | * |
| 3 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 and |
| 7 | * only version 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | #include <linux/debugfs.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/ctype.h> |
| 17 | #include <linux/jiffies.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/completion.h> |
| 21 | #include <linux/termios.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/remote_spinlock.h> |
Jeff Hugo | 5ba15fe | 2013-05-06 14:24:24 -0600 | [diff] [blame] | 24 | #include "smem_private.h" |
Eric Holmberg | 6275b60 | 2012-11-19 13:05:04 -0700 | [diff] [blame] | 25 | #include "smp2p_private.h" |
| 26 | |
| 27 | /** |
| 28 | * struct smp2p_loopback_ctx - Representation of remote loopback object. |
| 29 | * |
| 30 | * @proc_id: Processor id of the processor that sends the loopback commands. |
| 31 | * @out: Handle to the smem entry structure for providing the response. |
| 32 | * @out_nb: Notifies the opening of local entry. |
| 33 | * @out_is_active: Outbound entry events should be processed. |
| 34 | * @in_nb: Notifies changes in the remote entry. |
| 35 | * @in_is_active: Inbound entry events should be processed. |
| 36 | * @rmt_lpb_work: Work item that handles the incoming loopback commands. |
| 37 | * @rmt_cmd: Structure that holds the current and previous value of the entry. |
| 38 | */ |
| 39 | struct smp2p_loopback_ctx { |
| 40 | int proc_id; |
| 41 | struct msm_smp2p_out *out; |
| 42 | struct notifier_block out_nb; |
| 43 | bool out_is_active; |
| 44 | struct notifier_block in_nb; |
| 45 | bool in_is_active; |
| 46 | struct work_struct rmt_lpb_work; |
| 47 | struct msm_smp2p_update_notif rmt_cmd; |
| 48 | }; |
| 49 | |
| 50 | static struct smp2p_loopback_ctx remote_loopback[SMP2P_NUM_PROCS]; |
| 51 | static struct msm_smp2p_remote_mock remote_mock; |
| 52 | |
| 53 | /** |
| 54 | * remote_spinlock_test - Handles remote spinlock test. |
| 55 | * |
| 56 | * @ctx: Loopback context |
| 57 | */ |
| 58 | static void remote_spinlock_test(struct smp2p_loopback_ctx *ctx) |
| 59 | { |
| 60 | uint32_t test_request; |
| 61 | uint32_t test_response; |
| 62 | unsigned long flags; |
| 63 | int n; |
| 64 | unsigned lock_count = 0; |
| 65 | remote_spinlock_t *smem_spinlock; |
| 66 | |
| 67 | test_request = 0x0; |
| 68 | SMP2P_SET_RMT_CMD_TYPE_REQ(test_request); |
| 69 | smem_spinlock = smem_get_remote_spinlock(); |
| 70 | if (!smem_spinlock) { |
| 71 | pr_err("%s: unable to get remote spinlock\n", __func__); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | for (;;) { |
| 76 | remote_spin_lock_irqsave(smem_spinlock, flags); |
| 77 | ++lock_count; |
| 78 | SMP2P_SET_RMT_CMD(test_request, SMP2P_LB_CMD_RSPIN_LOCKED); |
| 79 | (void)msm_smp2p_out_write(ctx->out, test_request); |
| 80 | |
| 81 | for (n = 0; n < 10000; ++n) { |
| 82 | (void)msm_smp2p_in_read(ctx->proc_id, |
| 83 | "smp2p", &test_response); |
| 84 | test_response = SMP2P_GET_RMT_CMD(test_response); |
| 85 | |
| 86 | if (test_response == SMP2P_LB_CMD_RSPIN_END) |
| 87 | break; |
| 88 | |
| 89 | if (test_response != SMP2P_LB_CMD_RSPIN_UNLOCKED) |
| 90 | SMP2P_ERR("%s: invalid spinlock command %x\n", |
| 91 | __func__, test_response); |
| 92 | } |
| 93 | |
| 94 | if (test_response == SMP2P_LB_CMD_RSPIN_END) { |
| 95 | SMP2P_SET_RMT_CMD_TYPE_RESP(test_request); |
| 96 | SMP2P_SET_RMT_CMD(test_request, |
| 97 | SMP2P_LB_CMD_RSPIN_END); |
| 98 | SMP2P_SET_RMT_DATA(test_request, lock_count); |
| 99 | (void)msm_smp2p_out_write(ctx->out, test_request); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | SMP2P_SET_RMT_CMD(test_request, SMP2P_LB_CMD_RSPIN_UNLOCKED); |
| 104 | (void)msm_smp2p_out_write(ctx->out, test_request); |
| 105 | remote_spin_unlock_irqrestore(smem_spinlock, flags); |
| 106 | } |
| 107 | remote_spin_unlock_irqrestore(smem_spinlock, flags); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * smp2p_rmt_lpb_worker - Handles incoming remote loopback commands. |
| 112 | * |
| 113 | * @work: Work Item scheduled to handle the incoming commands. |
| 114 | */ |
| 115 | static void smp2p_rmt_lpb_worker(struct work_struct *work) |
| 116 | { |
| 117 | struct smp2p_loopback_ctx *ctx; |
| 118 | int lpb_cmd; |
| 119 | int lpb_cmd_type; |
| 120 | int lpb_data; |
| 121 | |
| 122 | ctx = container_of(work, struct smp2p_loopback_ctx, rmt_lpb_work); |
| 123 | |
| 124 | if (!ctx->in_is_active || !ctx->out_is_active) |
| 125 | return; |
| 126 | |
| 127 | if (ctx->rmt_cmd.previous_value == ctx->rmt_cmd.current_value) |
| 128 | return; |
| 129 | |
| 130 | lpb_cmd_type = SMP2P_GET_RMT_CMD_TYPE(ctx->rmt_cmd.current_value); |
| 131 | lpb_cmd = SMP2P_GET_RMT_CMD(ctx->rmt_cmd.current_value); |
| 132 | lpb_data = SMP2P_GET_RMT_DATA(ctx->rmt_cmd.current_value); |
| 133 | |
| 134 | if (lpb_cmd & SMP2P_RLPB_IGNORE) |
| 135 | return; |
| 136 | |
| 137 | switch (lpb_cmd) { |
| 138 | case SMP2P_LB_CMD_NOOP: |
| 139 | /* Do nothing */ |
| 140 | break; |
| 141 | |
| 142 | case SMP2P_LB_CMD_ECHO: |
| 143 | SMP2P_SET_RMT_CMD_TYPE(ctx->rmt_cmd.current_value, 0); |
| 144 | SMP2P_SET_RMT_DATA(ctx->rmt_cmd.current_value, |
| 145 | lpb_data); |
| 146 | (void)msm_smp2p_out_write(ctx->out, |
| 147 | ctx->rmt_cmd.current_value); |
| 148 | break; |
| 149 | |
| 150 | case SMP2P_LB_CMD_CLEARALL: |
| 151 | ctx->rmt_cmd.current_value = 0; |
| 152 | (void)msm_smp2p_out_write(ctx->out, |
| 153 | ctx->rmt_cmd.current_value); |
| 154 | break; |
| 155 | |
| 156 | case SMP2P_LB_CMD_PINGPONG: |
| 157 | SMP2P_SET_RMT_CMD_TYPE(ctx->rmt_cmd.current_value, 0); |
| 158 | if (lpb_data) { |
| 159 | lpb_data--; |
| 160 | SMP2P_SET_RMT_DATA(ctx->rmt_cmd.current_value, |
| 161 | lpb_data); |
| 162 | (void)msm_smp2p_out_write(ctx->out, |
| 163 | ctx->rmt_cmd.current_value); |
| 164 | } |
| 165 | break; |
| 166 | |
| 167 | case SMP2P_LB_CMD_RSPIN_START: |
| 168 | remote_spinlock_test(ctx); |
| 169 | break; |
| 170 | |
| 171 | case SMP2P_LB_CMD_RSPIN_LOCKED: |
| 172 | case SMP2P_LB_CMD_RSPIN_UNLOCKED: |
| 173 | case SMP2P_LB_CMD_RSPIN_END: |
| 174 | /* not used for remote spinlock test */ |
| 175 | break; |
| 176 | |
| 177 | default: |
| 178 | SMP2P_DBG("%s: Unknown loopback command %x\n", |
| 179 | __func__, lpb_cmd); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * smp2p_rmt_in_edge_notify - Schedules a work item to handle the commands. |
| 186 | * |
| 187 | * @nb: Notifier block, this is called when the value in remote entry changes. |
| 188 | * @event: Takes value SMP2P_ENTRY_UPDATE or SMP2P_OPEN based on the event. |
| 189 | * @data: Consists of previous and current value in case of entry update. |
| 190 | * @returns: 0 for success (return value required for notifier chains). |
| 191 | */ |
| 192 | static int smp2p_rmt_in_edge_notify(struct notifier_block *nb, |
| 193 | unsigned long event, void *data) |
| 194 | { |
| 195 | struct smp2p_loopback_ctx *ctx; |
| 196 | |
| 197 | if (!(event == SMP2P_ENTRY_UPDATE || event == SMP2P_OPEN)) |
| 198 | return 0; |
| 199 | |
| 200 | ctx = container_of(nb, struct smp2p_loopback_ctx, in_nb); |
| 201 | if (data && ctx->in_is_active) { |
| 202 | ctx->rmt_cmd = |
| 203 | *(struct msm_smp2p_update_notif *)data; |
| 204 | schedule_work(&ctx->rmt_lpb_work); |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * smp2p_rmt_out_edge_notify - Notifies on the opening of the outbound entry. |
| 212 | * |
| 213 | * @nb: Notifier block, this is called when the local entry is open. |
| 214 | * @event: Takes on value SMP2P_OPEN when the local entry is open. |
| 215 | * @data: Consist of current value of the remote entry, if entry is open. |
| 216 | * @returns: 0 for success (return value required for notifier chains). |
| 217 | */ |
| 218 | static int smp2p_rmt_out_edge_notify(struct notifier_block *nb, |
| 219 | unsigned long event, void *data) |
| 220 | { |
| 221 | struct smp2p_loopback_ctx *ctx; |
| 222 | |
| 223 | ctx = container_of(nb, struct smp2p_loopback_ctx, out_nb); |
| 224 | if (event == SMP2P_OPEN) |
| 225 | SMP2P_DBG("%s: 'smp2p':%d opened\n", __func__, |
| 226 | ctx->proc_id); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * msm_smp2p_init_rmt_lpb - Initializes the remote loopback object. |
| 233 | * |
| 234 | * @ctx: Pointer to remote loopback object that needs to be initialized. |
| 235 | * @pid: Processor id of the processor that is sending the commands. |
| 236 | * @entry: Name of the entry that needs to be opened locally. |
| 237 | * @returns: 0 on success, standard Linux error code otherwise. |
| 238 | */ |
| 239 | static int msm_smp2p_init_rmt_lpb(struct smp2p_loopback_ctx *ctx, |
| 240 | int pid, const char *entry) |
| 241 | { |
| 242 | int ret = 0; |
| 243 | int tmp; |
| 244 | |
| 245 | if (!ctx || !entry || pid > SMP2P_NUM_PROCS) |
| 246 | return -EINVAL; |
| 247 | |
| 248 | ctx->in_nb.notifier_call = smp2p_rmt_in_edge_notify; |
| 249 | ctx->out_nb.notifier_call = smp2p_rmt_out_edge_notify; |
| 250 | ctx->proc_id = pid; |
| 251 | ctx->in_is_active = true; |
| 252 | ctx->out_is_active = true; |
| 253 | tmp = msm_smp2p_out_open(pid, entry, &ctx->out_nb, |
| 254 | &ctx->out); |
| 255 | if (tmp) { |
| 256 | SMP2P_ERR("%s: open failed outbound entry '%s':%d - ret %d\n", |
| 257 | __func__, entry, pid, tmp); |
| 258 | ret = tmp; |
| 259 | } |
| 260 | |
| 261 | tmp = msm_smp2p_in_register(ctx->proc_id, |
| 262 | SMP2P_RLPB_ENTRY_NAME, |
| 263 | &ctx->in_nb); |
| 264 | if (tmp) { |
| 265 | SMP2P_ERR("%s: unable to open inbound entry '%s':%d - ret %d\n", |
| 266 | __func__, entry, pid, tmp); |
| 267 | ret = tmp; |
| 268 | } |
| 269 | |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * msm_smp2p_init_rmt_lpb_proc - Wrapper over msm_smp2p_init_rmt_lpb |
| 275 | * |
| 276 | * @remote_pid: Processor ID of the processor that sends loopback command. |
| 277 | * @returns: Pointer to outbound entry handle. |
| 278 | */ |
| 279 | void *msm_smp2p_init_rmt_lpb_proc(int remote_pid) |
| 280 | { |
| 281 | int tmp; |
| 282 | void *ret = NULL; |
| 283 | |
| 284 | tmp = msm_smp2p_init_rmt_lpb(&remote_loopback[remote_pid], |
| 285 | remote_pid, SMP2P_RLPB_ENTRY_NAME); |
| 286 | if (!tmp) |
| 287 | ret = remote_loopback[remote_pid].out; |
| 288 | |
| 289 | return ret; |
| 290 | } |
| 291 | EXPORT_SYMBOL(msm_smp2p_init_rmt_lpb_proc); |
| 292 | |
| 293 | /** |
| 294 | * msm_smp2p_deinit_rmt_lpb_proc - Unregister support for remote processor. |
| 295 | * |
| 296 | * @remote_pid: Processor ID of the remote system. |
| 297 | * @returns: 0 on success, standard Linux error code otherwise. |
| 298 | * |
| 299 | * Unregister loopback support for remote processor. |
| 300 | */ |
| 301 | int msm_smp2p_deinit_rmt_lpb_proc(int remote_pid) |
| 302 | { |
| 303 | int ret = 0; |
| 304 | int tmp; |
| 305 | struct smp2p_loopback_ctx *ctx; |
| 306 | |
| 307 | if (remote_pid >= SMP2P_NUM_PROCS) |
| 308 | return -EINVAL; |
| 309 | |
| 310 | ctx = &remote_loopback[remote_pid]; |
| 311 | |
| 312 | /* abort any pending notifications */ |
| 313 | remote_loopback[remote_pid].out_is_active = false; |
| 314 | remote_loopback[remote_pid].in_is_active = false; |
| 315 | flush_work(&ctx->rmt_lpb_work); |
| 316 | |
| 317 | /* unregister entries */ |
| 318 | tmp = msm_smp2p_out_close(&remote_loopback[remote_pid].out); |
| 319 | remote_loopback[remote_pid].out = NULL; |
| 320 | if (tmp) { |
| 321 | SMP2P_ERR("%s: outbound 'smp2p':%d close failed %d\n", |
| 322 | __func__, remote_pid, tmp); |
| 323 | ret = tmp; |
| 324 | } |
| 325 | |
| 326 | tmp = msm_smp2p_in_unregister(remote_pid, |
| 327 | SMP2P_RLPB_ENTRY_NAME, &remote_loopback[remote_pid].in_nb); |
| 328 | if (tmp) { |
| 329 | SMP2P_ERR("%s: inbound 'smp2p':%d close failed %d\n", |
| 330 | __func__, remote_pid, tmp); |
| 331 | ret = tmp; |
| 332 | } |
| 333 | |
| 334 | return ret; |
| 335 | } |
| 336 | EXPORT_SYMBOL(msm_smp2p_deinit_rmt_lpb_proc); |
| 337 | |
| 338 | /** |
| 339 | * msm_smp2p_set_remote_mock_exists - Sets the remote mock configuration. |
| 340 | * |
| 341 | * @item_exists: true = Remote mock SMEM item exists |
| 342 | * |
| 343 | * This is used in the testing environment to simulate the existence of the |
| 344 | * remote smem item in order to test the negotiation algorithm. |
| 345 | */ |
| 346 | void msm_smp2p_set_remote_mock_exists(bool item_exists) |
| 347 | { |
| 348 | remote_mock.item_exists = item_exists; |
| 349 | } |
| 350 | EXPORT_SYMBOL(msm_smp2p_set_remote_mock_exists); |
| 351 | |
| 352 | /** |
| 353 | * msm_smp2p_get_remote_mock - Get remote mock object. |
| 354 | * |
| 355 | * @returns: Point to the remote mock object. |
| 356 | */ |
| 357 | void *msm_smp2p_get_remote_mock(void) |
| 358 | { |
| 359 | return &remote_mock; |
| 360 | } |
| 361 | EXPORT_SYMBOL(msm_smp2p_get_remote_mock); |
| 362 | |
| 363 | /** |
| 364 | * msm_smp2p_get_remote_mock_smem_item - Returns a pointer to remote item. |
| 365 | * |
| 366 | * @size: Size of item. |
| 367 | * @returns: Pointer to mock remote smem item. |
| 368 | */ |
| 369 | void *msm_smp2p_get_remote_mock_smem_item(uint32_t *size) |
| 370 | { |
| 371 | void *ptr = NULL; |
| 372 | if (remote_mock.item_exists) { |
| 373 | *size = sizeof(remote_mock.remote_item); |
| 374 | ptr = &(remote_mock.remote_item); |
| 375 | } |
| 376 | |
| 377 | return ptr; |
| 378 | } |
| 379 | EXPORT_SYMBOL(msm_smp2p_get_remote_mock_smem_item); |
| 380 | |
| 381 | /** |
| 382 | * smp2p_remote_mock_rx_interrupt - Triggers receive interrupt for mock proc. |
| 383 | * |
| 384 | * @returns: 0 for success |
| 385 | * |
| 386 | * This function simulates the receiving of interrupt by the mock remote |
| 387 | * processor in a testing environment. |
| 388 | */ |
| 389 | int smp2p_remote_mock_rx_interrupt(void) |
| 390 | { |
| 391 | remote_mock.rx_interrupt_count++; |
| 392 | if (remote_mock.initialized) |
| 393 | complete(&remote_mock.cb_completion); |
| 394 | return 0; |
| 395 | } |
| 396 | EXPORT_SYMBOL(smp2p_remote_mock_rx_interrupt); |
| 397 | |
| 398 | /** |
| 399 | * smp2p_remote_mock_tx_interrupt - Calls the SMP2P interrupt handler. |
| 400 | * |
| 401 | * This function calls the interrupt handler of the Apps processor to simulate |
| 402 | * receiving interrupts from a remote processor. |
| 403 | */ |
| 404 | static void smp2p_remote_mock_tx_interrupt(void) |
| 405 | { |
| 406 | msm_smp2p_interrupt_handler(SMP2P_REMOTE_MOCK_PROC); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * smp2p_remote_mock_init - Initialize the remote mock and loopback objects. |
| 411 | * |
| 412 | * @returns: 0 for success |
| 413 | */ |
| 414 | static int __init smp2p_remote_mock_init(void) |
| 415 | { |
| 416 | int i; |
| 417 | |
| 418 | smp2p_init_header(&remote_mock.remote_item.header, |
| 419 | SMP2P_REMOTE_MOCK_PROC, SMP2P_APPS_PROC, |
| 420 | 0, 0); |
| 421 | remote_mock.rx_interrupt_count = 0; |
| 422 | remote_mock.rx_interrupt = smp2p_remote_mock_rx_interrupt; |
| 423 | remote_mock.tx_interrupt = smp2p_remote_mock_tx_interrupt; |
| 424 | remote_mock.item_exists = false; |
| 425 | init_completion(&remote_mock.cb_completion); |
| 426 | remote_mock.initialized = true; |
| 427 | |
| 428 | for (i = 0; i < SMP2P_NUM_PROCS; i++) { |
| 429 | INIT_WORK(&(remote_loopback[i].rmt_lpb_work), |
| 430 | smp2p_rmt_lpb_worker); |
| 431 | if (i == SMP2P_REMOTE_MOCK_PROC) |
| 432 | /* do not register loopback for remote mock proc */ |
| 433 | continue; |
| 434 | |
| 435 | msm_smp2p_init_rmt_lpb(&remote_loopback[i], |
| 436 | i, SMP2P_RLPB_ENTRY_NAME); |
| 437 | } |
| 438 | return 0; |
| 439 | } |
| 440 | module_init(smp2p_remote_mock_init); |