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-a35", "CA35"}, |
| 46 | {"arm,cortex-a53", "CA53"}, |
| 47 | {"arm,cortex-a72", "CA72"}, |
| 48 | {"arm,cortex-a73", "CA73"}, |
| 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 | void (*met_arch_setup_dma_ops_symbol)(struct device *dev); |
| 67 | int (*met_perf_event_read_local_symbol)(struct perf_event *ev, u64 *value, u64 *enable, u64 *running); |
| 68 | struct task_struct *(*met_kthread_create_on_cpu_symbol)(int (*threadfn)(void *data), |
| 69 | void *data, unsigned int cpu, |
| 70 | const char *namefmt); |
| 71 | int (*met_smp_call_function_single_symbol)(int cpu, smp_call_func_t func, void *info, int wait); |
| 72 | |
| 73 | |
| 74 | const char *met_get_platform_name(void) |
| 75 | { |
| 76 | return platform_name; |
| 77 | } |
| 78 | EXPORT_SYMBOL(met_get_platform_name); |
| 79 | |
| 80 | static 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 | |
| 92 | static 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 | |
| 130 | static 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 | cluster_num = of_get_child_count(node); |
| 141 | |
| 142 | for (i = 0; i < cluster_num; i++) { |
| 143 | snprintf(cluster_name, sizeof(cluster_name), "cluster%d", i); |
| 144 | node = of_find_node_by_name(NULL, cluster_name); |
| 145 | if (node) { |
| 146 | cluster_core_num = of_get_child_count(node); |
| 147 | |
| 148 | /* "|" use to separate different cluster */ |
| 149 | if (i > 0) { |
| 150 | len = strlen(met_cpu_topology); |
| 151 | snprintf(met_cpu_topology + len, sizeof(met_cpu_topology) - len, "|"); |
| 152 | } |
| 153 | |
| 154 | met_set_cpu_topology(start_core_id, cluster_core_num); |
| 155 | start_core_id = cluster_core_num; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return strlen(met_cpu_topology); |
| 161 | } |
| 162 | |
| 163 | static int met_kernel_symbol_get(void) |
| 164 | { |
| 165 | int ret = 0; |
| 166 | |
| 167 | if (met_get_cpuinfo_symbol == NULL) |
| 168 | met_get_cpuinfo_symbol = (void *)symbol_get(met_get_cpuinfo); |
| 169 | if (met_get_cpuinfo_symbol == NULL) |
| 170 | return -2; |
| 171 | |
| 172 | if (tracing_record_cmdline_symbol == NULL) |
| 173 | tracing_record_cmdline_symbol = (void *)symbol_get(met_tracing_record_cmdline); |
| 174 | if (tracing_record_cmdline_symbol == NULL) |
| 175 | ret = -3; |
| 176 | |
| 177 | if (met_cpu_frequency_symbol == NULL) |
| 178 | met_cpu_frequency_symbol = (void *)symbol_get(met_cpu_frequency); |
| 179 | if (met_cpu_frequency_symbol == NULL) |
| 180 | ret = -4; |
| 181 | |
| 182 | if (met_reg_switch_symbol == NULL) |
| 183 | met_reg_switch_symbol = (void *)symbol_get(met_reg_switch); |
| 184 | if (met_reg_switch_symbol == NULL) |
| 185 | ret = -5; |
| 186 | |
| 187 | if (met_unreg_switch_symbol == NULL) |
| 188 | met_unreg_switch_symbol = (void *)symbol_get(met_unreg_switch); |
| 189 | if (met_unreg_switch_symbol == NULL) |
| 190 | ret = -6; |
| 191 | |
| 192 | if (met_arch_setup_dma_ops_symbol == NULL) |
| 193 | met_arch_setup_dma_ops_symbol = (void *)symbol_get(met_arch_setup_dma_ops); |
| 194 | if (met_arch_setup_dma_ops_symbol == NULL) |
| 195 | ret = -7; |
| 196 | |
| 197 | if (met_perf_event_read_local_symbol == NULL) |
| 198 | met_perf_event_read_local_symbol = (void *)symbol_get(met_perf_event_read_local); |
| 199 | if (met_perf_event_read_local_symbol == NULL) |
| 200 | ret = -8; |
| 201 | |
| 202 | if (met_kthread_create_on_cpu_symbol == NULL) |
| 203 | met_kthread_create_on_cpu_symbol = (void *)symbol_get(met_kthread_create_on_cpu); |
| 204 | if (met_kthread_create_on_cpu_symbol == NULL) |
| 205 | ret = -9; |
| 206 | |
| 207 | if (met_smp_call_function_single_symbol == NULL) |
| 208 | met_smp_call_function_single_symbol = (void *)symbol_get(met_smp_call_function_single); |
| 209 | if (met_smp_call_function_single_symbol == NULL) |
| 210 | ret = -10; |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | DEFINE_PER_CPU(struct met_cpu_struct, met_cpu); |
| 215 | |
| 216 | static int __init met_drv_init(void) |
| 217 | { |
| 218 | int cpu; |
| 219 | int ret; |
| 220 | int cpu_topology_len; |
| 221 | struct met_cpu_struct *met_cpu_ptr; |
| 222 | |
| 223 | for_each_possible_cpu(cpu) { |
| 224 | met_cpu_ptr = &per_cpu(met_cpu, cpu); |
| 225 | /* snprintf(&(met_cpu_ptr->name[0]), sizeof(met_cpu_ptr->name), "met%02d", cpu); */ |
| 226 | met_cpu_ptr->cpu = cpu; |
| 227 | } |
| 228 | |
| 229 | ret = met_kernel_symbol_get(); |
| 230 | if (ret) { |
| 231 | pr_notice("[MET] met_kernel_symbol_get fail, ret = %d\n", ret); |
| 232 | return ret; |
| 233 | } |
| 234 | fs_reg(); |
| 235 | |
| 236 | if (of_root){ |
| 237 | /* |
| 238 | mt6765.dts |
| 239 | model = "MT6765"; |
| 240 | compatible = "mediatek,MT6765"; |
| 241 | interrupt-parent = <&sysirq>; |
| 242 | */ |
| 243 | if (of_root->properties) { |
| 244 | of_property_read_string(of_root, of_root->properties->name, &platform_name); |
| 245 | PR_BOOTMSG("platform_name=%s\n", platform_name); |
| 246 | } |
| 247 | } |
| 248 | if (platform_name) { |
| 249 | char buf[7]; |
| 250 | |
| 251 | memset(buf, 0x0, 7); |
| 252 | buf[0] = 'm'; |
| 253 | buf[1] = 't'; |
| 254 | strncpy(&buf[2], &platform_name[11], 4); |
| 255 | met_set_platform(buf, 1); |
| 256 | PR_BOOTMSG("buf=%s\n", buf); |
| 257 | } |
| 258 | |
| 259 | cpu_topology_len = met_create_cpu_topology(); |
| 260 | if (cpu_topology_len) |
| 261 | met_set_topology(met_cpu_topology, 1); |
| 262 | |
| 263 | #ifdef MET_PLF_USE |
| 264 | core_plf_init(); |
| 265 | #endif |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static void __exit met_drv_exit(void) |
| 270 | { |
| 271 | if (met_cpu_frequency_symbol) |
| 272 | symbol_put(met_cpu_frequency); |
| 273 | if (met_reg_switch_symbol) |
| 274 | symbol_put(met_reg_switch); |
| 275 | if (met_unreg_switch_symbol) |
| 276 | symbol_put(met_unreg_switch); |
| 277 | if (tracing_record_cmdline_symbol) |
| 278 | symbol_put(met_tracing_record_cmdline); |
| 279 | if (met_get_cpuinfo_symbol) |
| 280 | symbol_put(met_get_cpuinfo); |
| 281 | |
| 282 | #ifdef MET_PLF_USE |
| 283 | core_plf_exit(); |
| 284 | #endif |
| 285 | fs_unreg(); |
| 286 | |
| 287 | } |
| 288 | module_init(met_drv_init); |
| 289 | module_exit(met_drv_exit); |
| 290 | |
| 291 | MODULE_AUTHOR("DT_DM5"); |
| 292 | MODULE_DESCRIPTION("MET_CORE"); |
| 293 | MODULE_LICENSE("GPL"); |