developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2023 MediaTek Inc. All Rights Reserved. |
| 4 | * |
| 5 | * Author: Alvin Kuo <alvin.kuog@mediatek.com> |
| 6 | * Ren-Ting Wang <ren-ting.wang@mediatek.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/debugfs.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/relay.h> |
| 13 | |
developer | fbdb511 | 2023-08-21 15:12:14 +0800 | [diff] [blame] | 14 | #include "trm-debugfs.h" |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 15 | #include "trm-fs.h" |
| 16 | #include "trm-mcu.h" |
| 17 | #include "trm.h" |
| 18 | |
| 19 | #define RLY_RETRY_NUM 3 |
| 20 | |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 21 | static struct rchan *relay; |
| 22 | static bool trm_fs_is_init; |
| 23 | |
| 24 | bool mtk_trm_fs_is_init(void) |
| 25 | { |
| 26 | return trm_fs_is_init; |
| 27 | } |
| 28 | |
| 29 | void *mtk_trm_fs_relay_reserve(u32 size) |
| 30 | { |
| 31 | u32 rty = 0; |
| 32 | void *dst; |
| 33 | |
| 34 | while (rty < RLY_RETRY_NUM) { |
| 35 | dst = relay_reserve(relay, size); |
| 36 | if (likely(dst)) |
| 37 | return dst; |
| 38 | |
developer | 0b3c771 | 2023-08-24 16:23:03 +0800 | [diff] [blame] | 39 | rty++; |
| 40 | msleep(100); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | return ERR_PTR(-ENOMEM); |
| 44 | } |
| 45 | |
| 46 | void mtk_trm_fs_relay_flush(void) |
| 47 | { |
| 48 | relay_flush(relay); |
| 49 | } |
| 50 | |
| 51 | static struct dentry *trm_fs_create_buf_file_cb(const char *filename, |
| 52 | struct dentry *parent, |
| 53 | umode_t mode, |
| 54 | struct rchan_buf *buf, |
| 55 | int *is_global) |
| 56 | { |
| 57 | struct dentry *debugfs_file; |
| 58 | |
developer | fbdb511 | 2023-08-21 15:12:14 +0800 | [diff] [blame] | 59 | debugfs_file = debugfs_create_file("dump-data", mode, |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 60 | parent, buf, |
| 61 | &relay_file_operations); |
| 62 | |
| 63 | *is_global = 1; |
| 64 | |
| 65 | return debugfs_file; |
| 66 | } |
| 67 | |
| 68 | static int trm_fs_remove_buf_file_cb(struct dentry *debugfs_file) |
| 69 | { |
| 70 | debugfs_remove(debugfs_file); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int mtk_trm_fs_init(void) |
| 76 | { |
| 77 | static struct rchan_callbacks relay_cb = { |
| 78 | .create_buf_file = trm_fs_create_buf_file_cb, |
| 79 | .remove_buf_file = trm_fs_remove_buf_file_cb, |
| 80 | }; |
| 81 | int ret = 0; |
| 82 | |
developer | fbdb511 | 2023-08-21 15:12:14 +0800 | [diff] [blame] | 83 | if (!trm_debugfs_root) |
| 84 | return -ENOENT; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 85 | |
| 86 | if (!relay) { |
developer | fbdb511 | 2023-08-21 15:12:14 +0800 | [diff] [blame] | 87 | relay = relay_open("dump-data", trm_debugfs_root, |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 88 | RLY_DUMP_SUBBUF_SZ, |
| 89 | RLY_DUMP_SUBBUF_NUM, |
| 90 | &relay_cb, NULL); |
developer | fbdb511 | 2023-08-21 15:12:14 +0800 | [diff] [blame] | 91 | if (!relay) |
| 92 | return -EINVAL; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | relay_reset(relay); |
| 96 | |
| 97 | trm_fs_is_init = true; |
| 98 | |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | void mtk_trm_fs_deinit(void) |
| 103 | { |
| 104 | trm_fs_is_init = false; |
| 105 | |
| 106 | relay_close(relay); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 107 | } |