blob: 81ae77a35f24cbf21500080f6a45954c7bfa6914 [file] [log] [blame]
developere5e687d2023-08-08 16:05:33 +08001// 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
developerfbdb5112023-08-21 15:12:14 +080014#include "trm-debugfs.h"
developere5e687d2023-08-08 16:05:33 +080015#include "trm-fs.h"
16#include "trm-mcu.h"
17#include "trm.h"
18
19#define RLY_RETRY_NUM 3
20
developere5e687d2023-08-08 16:05:33 +080021static struct rchan *relay;
22static bool trm_fs_is_init;
23
24bool mtk_trm_fs_is_init(void)
25{
26 return trm_fs_is_init;
27}
28
29void *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
developer0b3c7712023-08-24 16:23:03 +080039 rty++;
40 msleep(100);
developere5e687d2023-08-08 16:05:33 +080041 }
42
43 return ERR_PTR(-ENOMEM);
44}
45
46void mtk_trm_fs_relay_flush(void)
47{
48 relay_flush(relay);
49}
50
51static 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
developerfbdb5112023-08-21 15:12:14 +080059 debugfs_file = debugfs_create_file("dump-data", mode,
developere5e687d2023-08-08 16:05:33 +080060 parent, buf,
61 &relay_file_operations);
62
63 *is_global = 1;
64
65 return debugfs_file;
66}
67
68static int trm_fs_remove_buf_file_cb(struct dentry *debugfs_file)
69{
70 debugfs_remove(debugfs_file);
71
72 return 0;
73}
74
75int 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
developerfbdb5112023-08-21 15:12:14 +080083 if (!trm_debugfs_root)
84 return -ENOENT;
developere5e687d2023-08-08 16:05:33 +080085
86 if (!relay) {
developerfbdb5112023-08-21 15:12:14 +080087 relay = relay_open("dump-data", trm_debugfs_root,
developere5e687d2023-08-08 16:05:33 +080088 RLY_DUMP_SUBBUF_SZ,
89 RLY_DUMP_SUBBUF_NUM,
90 &relay_cb, NULL);
developerfbdb5112023-08-21 15:12:14 +080091 if (!relay)
92 return -EINVAL;
developere5e687d2023-08-08 16:05:33 +080093 }
94
95 relay_reset(relay);
96
97 trm_fs_is_init = true;
98
developere5e687d2023-08-08 16:05:33 +080099 return ret;
100}
101
102void mtk_trm_fs_deinit(void)
103{
104 trm_fs_is_init = false;
105
106 relay_close(relay);
developere5e687d2023-08-08 16:05:33 +0800107}