blob: f0424c6297106b9533c596f43d749f076e77f527 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) 2019 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/moduleparam.h>
17#include <linux/hrtimer.h>
18#include <linux/cpu.h>
19#include <linux/mm.h>
20#include <linux/slab.h>
21#include <linux/profile.h>
22#include <linux/dcache.h>
23#include <linux/types.h>
24#include <linux/dcookies.h>
25#include <linux/sched.h>
26#include <linux/fs.h>
27#include <mt-plat/mtk_chip.h>
28
29#include <asm/irq_regs.h>
30
31#include "met_struct.h"
32#include "met_drv.h"
33#include "met_kernel_symbol.h"
34#include "interface.h"
35#include <linux/of.h>
36
37
38extern struct device_node *of_root;
39static const char *platform_name;
40
41struct cpu_type_name {
42 char full_name[32];
43 char abbr_name[8];
44};
45
46static struct cpu_type_name met_known_cpu_type[] = {
47 {"arm,cortex-a53", "CA53"},
48 {"arm,cortex-a55", "CA55"},
49 {"arm,cortex-a72", "CA72"},
50 {"arm,cortex-a73", "CA73"},
51 {"arm,cortex-a75", "CA75"},
52 {"arm,cortex-a76", "CA76"},
53};
54#define MET_KNOWN_CPU_TYPE_COUNT \
55 (sizeof(met_known_cpu_type)/sizeof(struct cpu_type_name))
56
57static char met_cpu_topology[64];
58
59#if defined(CONFIG_MET_ARM_32BIT)
60void (*met_get_cpuinfo_symbol)(int cpu, struct cpuinfo_arm **cpuinfo);
61#else
62void (*met_get_cpuinfo_symbol)(int cpu, struct cpuinfo_arm64 **cpuinfo);
63#endif
64
65void (*tracing_record_cmdline_symbol)(struct task_struct *tsk);
66void (*met_cpu_frequency_symbol)(unsigned int frequency, unsigned int cpu_id);
67int (*met_reg_switch_symbol)(void);
68void (*met_unreg_switch_symbol)(void);
69
70#ifdef MET_EVENT_POWER_SUPPORT
71int (*met_reg_event_power_symbol)(void);
72void (*met_unreg_event_power_symbol)(void);
73#endif
74
75void (*met_arch_setup_dma_ops_symbol)(struct device *dev);
76int (*met_perf_event_read_local_symbol)(struct perf_event *ev, u64 *value);
77struct task_struct *(*met_kthread_create_on_cpu_symbol)(int (*threadfn)(void *data),
78 void *data, unsigned int cpu,
79 const char *namefmt);
80int (*met_smp_call_function_single_symbol)(int cpu, smp_call_func_t func, void *info, int wait);
81
82#if defined(CONFIG_MTK_TINYSYS_SSPM_SUPPORT) && defined(ONDIEMET_SUPPORT)
83#ifdef SSPM_VERSION_V2
84struct mtk_ipi_device *sspm_ipidev_symbol = NULL;
85#endif
86#endif
87
88static int met_minor = -1;
89module_param(met_minor, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
90
91
92unsigned int (*mt_get_chip_id_symbol)(void);
93
94#ifdef MTK_PLATFORM
95#define _SHOW_MTK_PLATFORM(X) #X
96#define SHOW_MTK_PLATFORM(X) _SHOW_MTK_PLATFORM(X)
97#endif
98
99static int is_platform_name_valid(const char * buf)
100{
101 int len = strlen(buf);
102 int i;
103
104 for (i=0; i<len; i++) {
105 if ((buf[i] == 'm' && buf[i+1] == 't')
106 || (buf[i] == 'M' && buf[i+1] == 'T')
107 || (buf[i] == 'M' && buf[i+1] == 't')
108 || (buf[i] == 'm' && buf[i+1] == 'T')) {
109 return i;
110 }
111 }
112 return -1;
113}
114
115#if 0
116const char *met_get_platform_name(void)
117{
118 const char default_platform_name[7] = "mtxxxx";
119 int found = -1;
120
121 found = is_platform_name_valid(platform_name);
122
123 if(found < 0){
124#ifdef MTK_PLATFORM
125 const char buf[7] = SHOW_MTK_PLATFORM(MTK_PLATFORM);
126 found = is_platform_name_valid(buf);
127 if ( !(found < 0) )
128 platform_name = buf;
129 else
130#endif
131 platform_name = default_platform_name;
132 }
133
134 strncpy(&buf_tmp[2], &platform_name[found+2], 4);
135 return platform_name;
136}
137EXPORT_SYMBOL(met_get_platform_name);
138#endif
139
140static void get_cpu_type_name(const char *compatible, char *cpu_type)
141{
142 int i;
143
144 for (i = 0; i < MET_KNOWN_CPU_TYPE_COUNT; i++) {
145 if (!strncmp(compatible, met_known_cpu_type[i].full_name,
146 strlen(met_known_cpu_type[i].full_name)))
147 strncpy(cpu_type, met_known_cpu_type[i].abbr_name,
148 strlen(met_known_cpu_type[i].abbr_name) + 1);
149 }
150}
151
152static void met_set_cpu_topology(int core_id, int cluster_core_num)
153{
154 int i, buf_len = strlen(met_cpu_topology);
155 struct device_node *node = NULL;
156 const char *prev_cptb = NULL;
157 const char *cptb;
158 char cpu_type[16];
159
160 for (i = 0; i < cluster_core_num; i++) {
161 node = of_get_cpu_node(core_id + i, NULL);
162 if (node) {
163 cptb = of_get_property(node, "compatible", NULL);
164 if (cptb) {
165 get_cpu_type_name(cptb, cpu_type);
166 if (prev_cptb == NULL)
167 /* first write: write core_type & core_number */
168 buf_len += snprintf(met_cpu_topology + buf_len,
169 sizeof(met_cpu_topology) - buf_len,
170 "%s:%d", cpu_type, core_id + i);
171 else if (!strncmp(prev_cptb, cptb, strlen(prev_cptb)))
172 /* cpu type is the same with before */
173 /* write core_number */
174 buf_len += snprintf(met_cpu_topology + buf_len,
175 sizeof(met_cpu_topology) - buf_len,
176 ",%d", core_id + i);
177 else
178 /* cpu type is different with before */
179 /* write core_type & core_number */
180 buf_len += snprintf(met_cpu_topology + buf_len,
181 sizeof(met_cpu_topology) - buf_len,
182 "|%s:%d", cpu_type, core_id + i);
183
184 prev_cptb = cptb;
185 }
186 }
187 }
188}
189
190static int met_create_cpu_topology(void)
191{
192 int i, j, len;
193 struct device_node *node = NULL, *core_node = NULL;
194 int start_core_id = 0;
195 int cluster_num = 0, cluster_core_num = 0;
196 char cluster_name[16], core_name[16];
197
198 node = of_find_node_by_name(NULL, "cpu-map");
199 if (!node)
200 node = of_find_node_by_name(NULL, "virtual-cpu-map");
201
202 if (node) {
203 cluster_num = of_get_child_count(node);
204 of_node_put(node);
205
206 for (i = 0; i < cluster_num; i++) {
207 snprintf(cluster_name, sizeof(cluster_name), "cluster%d", i);
208 node = of_find_node_by_name(NULL, cluster_name);
209 if (node) {
210
211 j = 0;
212 cluster_core_num = 0;
213 do {
214 snprintf(core_name, sizeof(core_name), "core%d", j);
215 core_node = of_get_child_by_name(node, core_name);
216 if (core_node) {
217 cluster_core_num++;
218 of_node_put(core_node);
219 }
220 j++;
221 } while (core_node);
222 of_node_put(node);
223
224 /* "|" use to separate different cluster */
225 if (i > 0) {
226 len = strlen(met_cpu_topology);
227 snprintf(met_cpu_topology + len, sizeof(met_cpu_topology) - len, "|");
228 }
229
230 met_set_cpu_topology(start_core_id, cluster_core_num);
231 start_core_id = cluster_core_num;
232 }
233 }
234 }
235
236 return strlen(met_cpu_topology);
237}
238
239static int met_kernel_symbol_get(void)
240{
241 int ret = 0;
242
243 if (met_get_cpuinfo_symbol == NULL)
244 met_get_cpuinfo_symbol = (void *)symbol_get(met_get_cpuinfo);
245 if (met_get_cpuinfo_symbol == NULL)
246 return -2;
247
248 if (tracing_record_cmdline_symbol == NULL)
249 tracing_record_cmdline_symbol = (void *)symbol_get(met_tracing_record_cmdline);
250 if (tracing_record_cmdline_symbol == NULL)
251 ret = -3;
252
253 if (met_cpu_frequency_symbol == NULL)
254 met_cpu_frequency_symbol = (void *)symbol_get(met_cpu_frequency);
255 if (met_cpu_frequency_symbol == NULL)
256 ret = -4;
257
258 if (met_reg_switch_symbol == NULL)
259 met_reg_switch_symbol = (void *)symbol_get(met_reg_switch);
260 if (met_reg_switch_symbol == NULL)
261 ret = -5;
262
263 if (met_unreg_switch_symbol == NULL)
264 met_unreg_switch_symbol = (void *)symbol_get(met_unreg_switch);
265 if (met_unreg_switch_symbol == NULL)
266 ret = -6;
267
268 if (met_arch_setup_dma_ops_symbol == NULL)
269 met_arch_setup_dma_ops_symbol = (void *)symbol_get(met_arch_setup_dma_ops);
270 if (met_arch_setup_dma_ops_symbol == NULL)
271 ret = -7;
272
273 if (met_perf_event_read_local_symbol == NULL)
274 met_perf_event_read_local_symbol = (void *)symbol_get(met_perf_event_read_local);
275 if (met_perf_event_read_local_symbol == NULL)
276 ret = -8;
277
278 if (met_kthread_create_on_cpu_symbol == NULL)
279 met_kthread_create_on_cpu_symbol = (void *)symbol_get(met_kthread_create_on_cpu);
280 if (met_kthread_create_on_cpu_symbol == NULL)
281 ret = -9;
282
283 if (met_smp_call_function_single_symbol == NULL)
284 met_smp_call_function_single_symbol = (void *)symbol_get(met_smp_call_function_single);
285 if (met_smp_call_function_single_symbol == NULL)
286 ret = -10;
287
288#ifdef MET_EVENT_POWER_SUPPORT
289 if (met_reg_event_power_symbol == NULL)
290 met_reg_event_power_symbol = (void *)symbol_get(met_reg_event_power);
291 if (met_reg_event_power_symbol == NULL)
292 ret = -11;
293
294 if (met_unreg_event_power_symbol == NULL)
295 met_unreg_event_power_symbol = (void *)symbol_get(met_unreg_event_power);
296 if (met_unreg_event_power_symbol == NULL)
297 ret = -12;
298#endif
299
300#if defined(CONFIG_MTK_TINYSYS_SSPM_SUPPORT) && defined(ONDIEMET_SUPPORT)
301#ifdef SSPM_VERSION_V2
302 if (sspm_ipidev_symbol == NULL)
303 sspm_ipidev_symbol = symbol_get(sspm_ipidev);
304#endif
305#endif
306
307 return ret;
308}
309
310DEFINE_PER_CPU(struct met_cpu_struct, met_cpu);
311
312static int __init met_drv_init(void)
313{
314 int cpu;
315 int ret;
316 int cpu_topology_len;
317 struct met_cpu_struct *met_cpu_ptr;
318
319 for_each_possible_cpu(cpu) {
320 met_cpu_ptr = &per_cpu(met_cpu, cpu);
321 /* snprintf(&(met_cpu_ptr->name[0]), sizeof(met_cpu_ptr->name), "met%02d", cpu); */
322 met_cpu_ptr->cpu = cpu;
323 }
324
325 ret = met_kernel_symbol_get();
326 if (ret) {
327 pr_notice("[MET] met_kernel_symbol_get fail, ret = %d\n", ret);
328 return ret;
329 }
330 ret = fs_reg(met_minor);
331 if (ret) {
332 pr_notice("[MET] met fs_reg fail, ret = %d\n", ret);
333 return ret;
334 }
335
336
337 if (of_root){
338 /*
339 mt6765.dts
340 model = "MT6765";
341 compatible = "mediatek,MT6765";
342 interrupt-parent = <&sysirq>;
343 */
344 if (of_root->properties) {
345 of_property_read_string(of_root, "compatible", &platform_name);
346 PR_BOOTMSG("dts property compatible=%s\n", platform_name);
347 }
348 }
349 if (platform_name) {
350 char buf[7];
351 int found = -1;
352
353 found = is_platform_name_valid(platform_name);
354
355 if ( !(found < 0) ) {
356 memset(buf, 0x0, 7);
357 buf[0] = 'm';
358 buf[1] = 't';
359 strncpy(&buf[2], &platform_name[found+2], 4);
360 met_set_platform(buf, 1);
361 PR_BOOTMSG("Get platform info from dts, platform_name=%s\n", buf);
362 } else {
363#ifdef MTK_PLATFORM
364 memset(buf, 0x0, 7);
365 strcpy(buf, SHOW_MTK_PLATFORM(MTK_PLATFORM));
366 found = is_platform_name_valid((const char *)buf);
367 if( !(found < 0) ){
368 PR_BOOTMSG("Get platform info from met_drv Kbuild, platform_name=%s\n", buf);
369 met_set_platform(buf, 1);
370 }
371 else
372#endif
373 {
374 PR_BOOTMSG("Can not get platform info from dts nor met_drv Kbuild, set platform_name=mtxxxx\n");
375 met_set_platform("mtxxxx", 1);
376 }
377 }
378 }
379 /* get the chip id */
380
381 if (mt_get_chip_id_symbol == NULL)
382 mt_get_chip_id_symbol = (void *)symbol_get(mt_get_chip_id);
383
384 if (mt_get_chip_id_symbol != NULL)
385 met_set_chip_id(mt_get_chip_id_symbol());
386 else
387 PR_BOOTMSG("Can not get chip id info by mt_get_chip_id(), set chip_id=0x0\n");
388
389 cpu_topology_len = met_create_cpu_topology();
390 if (cpu_topology_len)
391 met_set_topology(met_cpu_topology, 1);
392
393#ifdef MET_PLF_USE
394 core_plf_init();
395#endif
396
397#ifdef MET_CHIP_USE
398 met_chip_init();
399#endif
400
401 return 0;
402}
403
404static void __exit met_drv_exit(void)
405{
406 if (met_cpu_frequency_symbol)
407 symbol_put(met_cpu_frequency);
408 if (met_reg_switch_symbol)
409 symbol_put(met_reg_switch);
410 if (met_unreg_switch_symbol)
411 symbol_put(met_unreg_switch);
412
413#ifdef MET_EVENT_POWER_SUPPORT
414 if (met_reg_event_power_symbol)
415 symbol_put(met_reg_event_power);
416 if (met_unreg_event_power_symbol)
417 symbol_put(met_unreg_event_power);
418#endif
419
420 if (tracing_record_cmdline_symbol)
421 symbol_put(met_tracing_record_cmdline);
422 if (met_get_cpuinfo_symbol)
423 symbol_put(met_get_cpuinfo);
424
425 if (mt_get_chip_id_symbol)
426 symbol_put(mt_get_chip_id);
427
428#ifdef MET_PLF_USE
429 core_plf_exit();
430#endif
431
432#ifdef MET_CHIP_USE
433 met_chip_exit();
434#endif
435
436 fs_unreg();
437
438}
439module_init(met_drv_init);
440module_exit(met_drv_exit);
441
442MODULE_AUTHOR("DT_DM5");
443MODULE_DESCRIPTION("MET_CORE");
444MODULE_LICENSE("GPL");