hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
/*
 *
 * (C) COPYRIGHT 2022 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 */
 
#include <linux/atomic.h>
#include <linux/coresight.h>
#include <linux/dma-mapping.h>
#include <linux/types.h>
#include <linux/version.h>
#include <linux/vmalloc.h>
#include <linux/of_platform.h>
 
#include <linux/mali_kbase_debug_coresight_csf.h>
#include <coresight-priv.h>
#include "sources/coresight_mali_sources.h"
 
static int coresight_mali_source_trace_id(struct coresight_device *csdev)
{
   struct coresight_mali_source_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
 
   return drvdata->trcid;
}
 
static int coresight_mali_enable_source(struct coresight_device *csdev, struct perf_event *event,
                   u32 mode)
{
   return coresight_mali_enable_component(csdev, mode);
}
 
static void coresight_mali_disable_source(struct coresight_device *csdev, struct perf_event *event)
{
   coresight_mali_disable_component(csdev);
}
 
static const struct coresight_ops_source coresight_mali_source_ops = {
   .trace_id = coresight_mali_source_trace_id,
   .enable = coresight_mali_enable_source,
   .disable = coresight_mali_disable_source
};
 
static const struct coresight_ops mali_cs_ops = {
   .source_ops = &coresight_mali_source_ops,
};
 
int coresight_mali_sources_probe(struct platform_device *pdev)
{
   int ret = 0;
   struct coresight_platform_data *pdata = NULL;
   struct coresight_mali_source_drvdata *drvdata = NULL;
   struct coresight_desc desc = { 0 };
   struct device *dev = &pdev->dev;
   struct device_node *np = dev->of_node;
   struct platform_device *gpu_pdev = NULL;
   struct device_node *gpu_node = NULL;
 
   drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
   if (!drvdata)
       return -ENOMEM;
 
   dev_set_drvdata(dev, drvdata);
   drvdata->base.dev = dev;
 
#if KERNEL_VERSION(5, 3, 0) <= LINUX_VERSION_CODE
   pdata = coresight_get_platform_data(dev);
#else
   if (np)
       pdata = of_get_coresight_platform_data(dev, np);
#endif
   if (IS_ERR(pdata)) {
       dev_err(drvdata->base.dev, "Failed to get platform data\n");
       ret = PTR_ERR(pdata);
       goto devm_kfree_drvdata;
   }
 
   dev->platform_data = pdata;
 
   gpu_node = of_parse_phandle(np, "gpu", 0);
   if (!gpu_node) {
       dev_err(drvdata->base.dev, "GPU node not available\n");
       goto devm_kfree_drvdata;
   }
   gpu_pdev = of_find_device_by_node(gpu_node);
   if (gpu_pdev == NULL) {
       dev_err(drvdata->base.dev, "Couldn't find GPU device from node\n");
       goto devm_kfree_drvdata;
   }
 
   drvdata->base.gpu_dev = platform_get_drvdata(gpu_pdev);
   if (!drvdata->base.gpu_dev) {
       dev_err(drvdata->base.dev, "GPU dev not available\n");
       goto devm_kfree_drvdata;
   }
 
   ret = coresight_mali_sources_init_drvdata(drvdata);
   if (ret) {
       dev_err(drvdata->base.dev, "Failed to init source driver data\n");
       goto kbase_client_unregister;
   }
 
   desc.type = CORESIGHT_DEV_TYPE_SOURCE;
   desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE;
   desc.ops = &mali_cs_ops;
   desc.pdata = pdata;
   desc.dev = dev;
   desc.groups = coresight_mali_source_groups_get();
 
#if KERNEL_VERSION(5, 3, 0) <= LINUX_VERSION_CODE
   desc.name = devm_kasprintf(dev, GFP_KERNEL, "%s", drvdata->type_name);
   if (!desc.name) {
       ret = -ENOMEM;
       goto devm_kfree_drvdata;
   }
#endif
   drvdata->base.csdev = coresight_register(&desc);
   if (IS_ERR(drvdata->base.csdev)) {
       dev_err(drvdata->base.dev, "Failed to register coresight device\n");
       ret = PTR_ERR(drvdata->base.csdev);
       goto devm_kfree_drvdata;
   }
 
   return ret;
 
kbase_client_unregister:
   if (drvdata->base.csdev != NULL)
       coresight_unregister(drvdata->base.csdev);
 
   coresight_mali_sources_deinit_drvdata(drvdata);
 
devm_kfree_drvdata:
   devm_kfree(dev, drvdata);
 
   return ret;
}
 
int coresight_mali_sources_remove(struct platform_device *pdev)
{
   struct coresight_mali_source_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
 
   if (drvdata->base.csdev != NULL)
       coresight_unregister(drvdata->base.csdev);
 
   coresight_mali_sources_deinit_drvdata(drvdata);
 
   devm_kfree(&pdev->dev, drvdata);
 
   return 0;
}
 
MODULE_AUTHOR("ARM Ltd.");
MODULE_DESCRIPTION("Arm Coresight Mali source");
MODULE_LICENSE("GPL");