blob: 8158ddf227a7755c2f032fd26a2246a9b172a921 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) 2018 MediaTek Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/hrtimer.h>
17#include <linux/cpu.h>
18#include <linux/mm.h>
19#include <linux/slab.h>
20#include <linux/profile.h>
21#include <linux/dcache.h>
22#include <linux/types.h>
23#include <linux/dcookies.h>
24#include <linux/sched.h>
25#include <linux/fs.h>
26
27#include <asm/irq_regs.h>
28
29#include "met_struct.h"
30#include "met_drv.h"
31#include "met_kernel_symbol.h"
32#include "interface.h"
33#include <linux/of.h>
34
35
36extern struct device_node *of_root;
37static const char *platform_name;
38
39struct cpu_type_name {
40 char full_name[32];
41 char abbr_name[8];
42};
43
44static struct cpu_type_name met_known_cpu_type[] = {
45 {"arm,cortex-a53", "CA53"},
46 {"arm,cortex-a55", "CA55"},
47 {"arm,cortex-a73", "CA73"},
48 {"arm,cortex-a75", "CA75"},
49};
50#define MET_KNOWN_CPU_TYPE_COUNT \
51 (sizeof(met_known_cpu_type)/sizeof(struct cpu_type_name))
52
53static char met_cpu_topology[64];
54
55#if defined(CONFIG_MET_ARM_32BIT)
56void (*met_get_cpuinfo_symbol)(int cpu, struct cpuinfo_arm **cpuinfo);
57#else
58void (*met_get_cpuinfo_symbol)(int cpu, struct cpuinfo_arm64 **cpuinfo);
59#endif
60
61void (*tracing_record_cmdline_symbol)(struct task_struct *tsk);
62void (*met_cpu_frequency_symbol)(unsigned int frequency, unsigned int cpu_id);
63int (*met_reg_switch_symbol)(void);
64void (*met_unreg_switch_symbol)(void);
65
66void (*met_arch_setup_dma_ops_symbol)(struct device *dev);
67u64 (*met_perf_event_read_local_symbol)(struct perf_event *ev);
68struct task_struct *(*met_kthread_create_on_cpu_symbol)(int (*threadfn)(void *data),
69 void *data, unsigned int cpu,
70 const char *namefmt);
71int (*met_smp_call_function_single_symbol)(int cpu, smp_call_func_t func, void *info, int wait);
72
73
74const char *met_get_platform_name(void)
75{
76 return platform_name;
77}
78EXPORT_SYMBOL(met_get_platform_name);
79
80static void get_cpu_type_name(const char *compatible, char *cpu_type)
81{
82 int i;
83
84 for (i = 0; i < MET_KNOWN_CPU_TYPE_COUNT; i++) {
85 if (!strncmp(compatible, met_known_cpu_type[i].full_name,
86 strlen(met_known_cpu_type[i].full_name)))
87 strncpy(cpu_type, met_known_cpu_type[i].abbr_name,
88 strlen(met_known_cpu_type[i].abbr_name) + 1);
89 }
90}
91
92static void met_set_cpu_topology(int core_id, int cluster_core_num)
93{
94 int i, buf_len = strlen(met_cpu_topology);
95 struct device_node *node = NULL;
96 const char *prev_cptb = NULL;
97 const char *cptb;
98 char cpu_type[16];
99
100 for (i = 0; i < cluster_core_num; i++) {
101 node = of_get_cpu_node(core_id + i, NULL);
102 if (node) {
103 cptb = of_get_property(node, "compatible", NULL);
104 if (cptb) {
105 get_cpu_type_name(cptb, cpu_type);
106 if (prev_cptb == NULL)
107 /* first write: write core_type & core_number */
108 buf_len += snprintf(met_cpu_topology + buf_len,
109 sizeof(met_cpu_topology) - buf_len,
110 "%s:%d", cpu_type, core_id + i);
111 else if (!strncmp(prev_cptb, cptb, strlen(prev_cptb)))
112 /* cpu type is the same with before */
113 /* write core_number */
114 buf_len += snprintf(met_cpu_topology + buf_len,
115 sizeof(met_cpu_topology) - buf_len,
116 ",%d", core_id + i);
117 else
118 /* cpu type is different with before */
119 /* write core_type & core_number */
120 buf_len += snprintf(met_cpu_topology + buf_len,
121 sizeof(met_cpu_topology) - buf_len,
122 "|%s:%d", cpu_type, core_id + i);
123
124 prev_cptb = cptb;
125 }
126 }
127 }
128}
129
130static int met_create_cpu_topology(void)
131{
132 int i, len;
133 struct device_node *node = NULL;
134 int start_core_id = 0;
135 int cluster_num = 0, cluster_core_num = 0;
136 char cluster_name[16];
137
138 node = of_find_node_by_name(NULL, "cpu-map");
139 if (!node)
140 node = of_find_node_by_name(NULL, "virtual-cpu-map");
141
142 if (node) {
143 cluster_num = of_get_child_count(node);
144
145 for (i = 0; i < cluster_num; i++) {
146 snprintf(cluster_name, sizeof(cluster_name), "cluster%d", i);
147 node = of_find_node_by_name(NULL, cluster_name);
148 if (node) {
149 cluster_core_num = of_get_child_count(node);
150
151 /* "|" use to separate different cluster */
152 if (i > 0) {
153 len = strlen(met_cpu_topology);
154 snprintf(met_cpu_topology + len, sizeof(met_cpu_topology) - len, "|");
155 }
156
157 met_set_cpu_topology(start_core_id, cluster_core_num);
158 start_core_id = cluster_core_num;
159 }
160 }
161 }
162
163 return strlen(met_cpu_topology);
164}
165
166static int met_kernel_symbol_get(void)
167{
168 int ret = 0;
169
170 if (met_get_cpuinfo_symbol == NULL)
171 met_get_cpuinfo_symbol = (void *)symbol_get(met_get_cpuinfo);
172 if (met_get_cpuinfo_symbol == NULL)
173 return -2;
174
175 if (tracing_record_cmdline_symbol == NULL)
176 tracing_record_cmdline_symbol = (void *)symbol_get(met_tracing_record_cmdline);
177 if (tracing_record_cmdline_symbol == NULL)
178 ret = -3;
179
180 if (met_cpu_frequency_symbol == NULL)
181 met_cpu_frequency_symbol = (void *)symbol_get(met_cpu_frequency);
182 if (met_cpu_frequency_symbol == NULL)
183 ret = -4;
184
185 if (met_reg_switch_symbol == NULL)
186 met_reg_switch_symbol = (void *)symbol_get(met_reg_switch);
187 if (met_reg_switch_symbol == NULL)
188 ret = -5;
189
190 if (met_unreg_switch_symbol == NULL)
191 met_unreg_switch_symbol = (void *)symbol_get(met_unreg_switch);
192 if (met_unreg_switch_symbol == NULL)
193 ret = -6;
194
195 if (met_arch_setup_dma_ops_symbol == NULL)
196 met_arch_setup_dma_ops_symbol = (void *)symbol_get(met_arch_setup_dma_ops);
197 if (met_arch_setup_dma_ops_symbol == NULL)
198 ret = -7;
199
200 if (met_perf_event_read_local_symbol == NULL)
201 met_perf_event_read_local_symbol = (void *)symbol_get(met_perf_event_read_local);
202 if (met_perf_event_read_local_symbol == NULL)
203 ret = -8;
204
205 if (met_kthread_create_on_cpu_symbol == NULL)
206 met_kthread_create_on_cpu_symbol = (void *)symbol_get(met_kthread_create_on_cpu);
207 if (met_kthread_create_on_cpu_symbol == NULL)
208 ret = -9;
209
210 if (met_smp_call_function_single_symbol == NULL)
211 met_smp_call_function_single_symbol = (void *)symbol_get(met_smp_call_function_single);
212 if (met_smp_call_function_single_symbol == NULL)
213 ret = -10;
214 return ret;
215}
216
217DEFINE_PER_CPU(struct met_cpu_struct, met_cpu);
218
219static int __init met_drv_init(void)
220{
221 int cpu;
222 int ret;
223 int cpu_topology_len;
224 struct met_cpu_struct *met_cpu_ptr;
225
226 for_each_possible_cpu(cpu) {
227 met_cpu_ptr = &per_cpu(met_cpu, cpu);
228 /* snprintf(&(met_cpu_ptr->name[0]), sizeof(met_cpu_ptr->name), "met%02d", cpu); */
229 met_cpu_ptr->cpu = cpu;
230 }
231
232 ret = met_kernel_symbol_get();
233 if (ret) {
234 pr_notice("[MET] met_kernel_symbol_get fail, ret = %d\n", ret);
235 return ret;
236 }
237 fs_reg();
238
239 if (of_root){
240 /*
241 mt6765.dts
242 model = "MT6765";
243 compatible = "mediatek,MT6765";
244 interrupt-parent = <&sysirq>;
245 */
246 if (of_root->properties) {
247 of_property_read_string(of_root, of_root->properties->name, &platform_name);
248 PR_BOOTMSG("platform_name=%s\n", platform_name);
249 }
250 }
251 if (platform_name) {
252 char buf[7];
253 int len = strlen(platform_name);
254 int i;
255 int found = 0;
256
257 if (len >= 7) {
258 for (i=0; i<len-6; i++) {
259 if ((platform_name[i] == 'm' && platform_name[i+1] == 't')
260 || (platform_name[i] == 'M' && platform_name[i+1] == 'T')
261 || (platform_name[i] == 'M' && platform_name[i+1] == 't')
262 || (platform_name[i] == 'm' && platform_name[i+1] == 'T')) {
263 found = 1;
264 break;
265 }
266 }
267 }
268
269 if (found == 1) {
270 memset(buf, 0x0, 7);
271 buf[0] = 'm';
272 buf[1] = 't';
273 strncpy(&buf[2], &platform_name[i+2], 4);
274 met_set_platform(buf, 1);
275 PR_BOOTMSG("set_platform=%s\n", buf);
276 } else {
277 met_set_platform("mtxxxx", 1);
278 }
279 }
280
281 cpu_topology_len = met_create_cpu_topology();
282 if (cpu_topology_len)
283 met_set_topology(met_cpu_topology, 1);
284
285#ifdef MET_PLF_USE
286 core_plf_init();
287#endif
288 return 0;
289}
290
291static void __exit met_drv_exit(void)
292{
293 if (met_cpu_frequency_symbol)
294 symbol_put(met_cpu_frequency);
295 if (met_reg_switch_symbol)
296 symbol_put(met_reg_switch);
297 if (met_unreg_switch_symbol)
298 symbol_put(met_unreg_switch);
299 if (tracing_record_cmdline_symbol)
300 symbol_put(met_tracing_record_cmdline);
301 if (met_get_cpuinfo_symbol)
302 symbol_put(met_get_cpuinfo);
303
304#ifdef MET_PLF_USE
305 core_plf_exit();
306#endif
307 fs_unreg();
308
309}
310module_init(met_drv_init);
311module_exit(met_drv_exit);
312
313MODULE_AUTHOR("DT_DM5");
314MODULE_DESCRIPTION("MET_CORE");
315MODULE_LICENSE("GPL");