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