1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OPAL IMC interface detection driver
4 * Supported on POWERNV platform
6 * Copyright (C) 2017 Madhavan Srinivasan, IBM Corporation.
7 * (C) 2017 Anju T Sudhakar, IBM Corporation.
8 * (C) 2017 Hemant K Shaw, IBM Corporation.
10 #include <linux/kernel.h>
11 #include <linux/platform_device.h>
13 #include <linux/of_address.h>
14 #include <linux/of_platform.h>
15 #include <linux/crash_dump.h>
18 #include <asm/imc-pmu.h>
19 #include <asm/cputhreads.h>
20 #include <asm/debugfs.h>
22 static struct dentry *imc_debugfs_parent;
24 /* Helpers to export imc command and mode via debugfs */
25 static int imc_mem_get(void *data, u64 *val)
27 *val = cpu_to_be64(*(u64 *)data);
31 static int imc_mem_set(void *data, u64 val)
33 *(u64 *)data = cpu_to_be64(val);
36 DEFINE_DEBUGFS_ATTRIBUTE(fops_imc_x64, imc_mem_get, imc_mem_set, "0x%016llx\n");
38 static struct dentry *imc_debugfs_create_x64(const char *name, umode_t mode,
39 struct dentry *parent, u64 *value)
41 return debugfs_create_file_unsafe(name, mode, parent,
42 value, &fops_imc_x64);
46 * export_imc_mode_and_cmd: Create a debugfs interface
47 * for imc_cmd and imc_mode
48 * for each node in the system.
49 * imc_mode and imc_cmd can be changed by echo into
52 static void export_imc_mode_and_cmd(struct device_node *node,
53 struct imc_pmu *pmu_ptr)
55 static u64 loc, *imc_mode_addr, *imc_cmd_addr;
57 char mode[16], cmd[16];
60 imc_debugfs_parent = debugfs_create_dir("imc", powerpc_debugfs_root);
63 * Return here, either because 'imc' directory already exists,
64 * Or failed to create a new one.
66 if (!imc_debugfs_parent)
69 if (of_property_read_u32(node, "cb_offset", &cb_offset))
70 cb_offset = IMC_CNTL_BLK_OFFSET;
73 loc = (u64)(pmu_ptr->mem_info[chip].vbase) + cb_offset;
74 imc_mode_addr = (u64 *)(loc + IMC_CNTL_BLK_MODE_OFFSET);
75 sprintf(mode, "imc_mode_%d", nid);
76 if (!imc_debugfs_create_x64(mode, 0600, imc_debugfs_parent,
80 imc_cmd_addr = (u64 *)(loc + IMC_CNTL_BLK_CMD_OFFSET);
81 sprintf(cmd, "imc_cmd_%d", nid);
82 if (!imc_debugfs_create_x64(cmd, 0600, imc_debugfs_parent,
90 debugfs_remove_recursive(imc_debugfs_parent);
94 * imc_get_mem_addr_nest: Function to get nest counter memory region
97 static int imc_get_mem_addr_nest(struct device_node *node,
98 struct imc_pmu *pmu_ptr,
102 u64 *base_addr_arr, baddr;
105 nr_chips = of_property_count_u32_elems(node, "chip-id");
109 base_addr_arr = kcalloc(nr_chips, sizeof(*base_addr_arr), GFP_KERNEL);
113 chipid_arr = kcalloc(nr_chips, sizeof(*chipid_arr), GFP_KERNEL);
115 kfree(base_addr_arr);
119 if (of_property_read_u32_array(node, "chip-id", chipid_arr, nr_chips))
122 if (of_property_read_u64_array(node, "base-addr", base_addr_arr,
126 pmu_ptr->mem_info = kcalloc(nr_chips + 1, sizeof(*pmu_ptr->mem_info),
128 if (!pmu_ptr->mem_info)
131 for (i = 0; i < nr_chips; i++) {
132 pmu_ptr->mem_info[i].id = chipid_arr[i];
133 baddr = base_addr_arr[i] + offset;
134 pmu_ptr->mem_info[i].vbase = phys_to_virt(baddr);
137 pmu_ptr->imc_counter_mmaped = true;
138 export_imc_mode_and_cmd(node, pmu_ptr);
139 kfree(base_addr_arr);
144 kfree(base_addr_arr);
150 * imc_pmu_create : Takes the parent device which is the pmu unit, pmu_index
151 * and domain as the inputs.
152 * Allocates memory for the struct imc_pmu, sets up its domain, size and offsets
154 static int imc_pmu_create(struct device_node *parent, int pmu_index, int domain)
157 struct imc_pmu *pmu_ptr;
161 pmu_ptr = kzalloc(sizeof(*pmu_ptr), GFP_KERNEL);
166 pmu_ptr->domain = domain;
168 ret = of_property_read_u32(parent, "size", &pmu_ptr->counter_mem_size);
174 if (!of_property_read_u32(parent, "offset", &offset)) {
175 if (imc_get_mem_addr_nest(parent, pmu_ptr, offset)) {
181 /* Function to register IMC pmu */
182 ret = init_imc_pmu(parent, pmu_ptr, pmu_index);
184 pr_err("IMC PMU %s Register failed\n", pmu_ptr->pmu.name);
185 kfree(pmu_ptr->pmu.name);
186 if (pmu_ptr->domain == IMC_DOMAIN_NEST)
187 kfree(pmu_ptr->mem_info);
199 static void disable_nest_pmu_counters(void)
202 const struct cpumask *l_cpumask;
205 for_each_node_with_cpus(nid) {
206 l_cpumask = cpumask_of_node(nid);
207 cpu = cpumask_first_and(l_cpumask, cpu_online_mask);
208 if (cpu >= nr_cpu_ids)
210 opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
211 get_hard_smp_processor_id(cpu));
216 static void disable_core_pmu_counters(void)
222 /* Disable the IMC Core functions */
223 cores_map = cpu_online_cores_map();
224 for_each_cpu(cpu, &cores_map) {
225 rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
226 get_hard_smp_processor_id(cpu));
228 pr_err("%s: Failed to stop Core (cpu = %d)\n",
234 int get_max_nest_dev(void)
236 struct device_node *node;
237 u32 pmu_units = 0, type;
239 for_each_compatible_node(node, NULL, IMC_DTB_UNIT_COMPAT) {
240 if (of_property_read_u32(node, "type", &type))
243 if (type == IMC_TYPE_CHIP)
250 static int opal_imc_counters_probe(struct platform_device *pdev)
252 struct device_node *imc_dev = pdev->dev.of_node;
253 int pmu_count = 0, domain;
254 bool core_imc_reg = false, thread_imc_reg = false;
258 * Check whether this is kdump kernel. If yes, force the engines to
261 if (is_kdump_kernel()) {
262 disable_nest_pmu_counters();
263 disable_core_pmu_counters();
267 for_each_compatible_node(imc_dev, NULL, IMC_DTB_UNIT_COMPAT) {
268 if (of_property_read_u32(imc_dev, "type", &type)) {
269 pr_warn("IMC Device without type property\n");
275 domain = IMC_DOMAIN_NEST;
278 domain =IMC_DOMAIN_CORE;
280 case IMC_TYPE_THREAD:
281 domain = IMC_DOMAIN_THREAD;
284 domain = IMC_DOMAIN_TRACE;
287 pr_warn("IMC Unknown Device type \n");
292 if (!imc_pmu_create(imc_dev, pmu_count, domain)) {
293 if (domain == IMC_DOMAIN_NEST)
295 if (domain == IMC_DOMAIN_CORE)
297 if (domain == IMC_DOMAIN_THREAD)
298 thread_imc_reg = true;
302 /* If none of the nest units are registered, remove debugfs interface */
304 debugfs_remove_recursive(imc_debugfs_parent);
306 /* If core imc is not registered, unregister thread-imc */
307 if (!core_imc_reg && thread_imc_reg)
308 unregister_thread_imc();
313 static void opal_imc_counters_shutdown(struct platform_device *pdev)
316 * Function only stops the engines which is bare minimum.
317 * TODO: Need to handle proper memory cleanup and pmu
320 disable_nest_pmu_counters();
321 disable_core_pmu_counters();
324 static const struct of_device_id opal_imc_match[] = {
325 { .compatible = IMC_DTB_COMPAT },
329 static struct platform_driver opal_imc_driver = {
331 .name = "opal-imc-counters",
332 .of_match_table = opal_imc_match,
334 .probe = opal_imc_counters_probe,
335 .shutdown = opal_imc_counters_shutdown,
338 builtin_platform_driver(opal_imc_driver);