2 * OPAL IMC interface detection driver
3 * Supported on POWERNV platform
5 * Copyright (C) 2017 Madhavan Srinivasan, IBM Corporation.
6 * (C) 2017 Anju T Sudhakar, IBM Corporation.
7 * (C) 2017 Hemant K Shaw, IBM Corporation.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or later version.
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
17 #include <linux/of_address.h>
18 #include <linux/of_platform.h>
19 #include <linux/crash_dump.h>
22 #include <asm/imc-pmu.h>
23 #include <asm/cputhreads.h>
24 #include <asm/debugfs.h>
26 static struct dentry
*imc_debugfs_parent
;
28 /* Helpers to export imc command and mode via debugfs */
29 static int imc_mem_get(void *data
, u64
*val
)
31 *val
= cpu_to_be64(*(u64
*)data
);
35 static int imc_mem_set(void *data
, u64 val
)
37 *(u64
*)data
= cpu_to_be64(val
);
40 DEFINE_DEBUGFS_ATTRIBUTE(fops_imc_x64
, imc_mem_get
, imc_mem_set
, "0x%016llx\n");
42 static struct dentry
*imc_debugfs_create_x64(const char *name
, umode_t mode
,
43 struct dentry
*parent
, u64
*value
)
45 return debugfs_create_file_unsafe(name
, mode
, parent
,
46 value
, &fops_imc_x64
);
50 * export_imc_mode_and_cmd: Create a debugfs interface
51 * for imc_cmd and imc_mode
52 * for each node in the system.
53 * imc_mode and imc_cmd can be changed by echo into
56 static void export_imc_mode_and_cmd(struct device_node
*node
,
57 struct imc_pmu
*pmu_ptr
)
59 static u64 loc
, *imc_mode_addr
, *imc_cmd_addr
;
61 char mode
[16], cmd
[16];
64 imc_debugfs_parent
= debugfs_create_dir("imc", powerpc_debugfs_root
);
67 * Return here, either because 'imc' directory already exists,
68 * Or failed to create a new one.
70 if (!imc_debugfs_parent
)
73 if (of_property_read_u32(node
, "cb_offset", &cb_offset
))
74 cb_offset
= IMC_CNTL_BLK_OFFSET
;
77 loc
= (u64
)(pmu_ptr
->mem_info
[chip
].vbase
) + cb_offset
;
78 imc_mode_addr
= (u64
*)(loc
+ IMC_CNTL_BLK_MODE_OFFSET
);
79 sprintf(mode
, "imc_mode_%d", nid
);
80 if (!imc_debugfs_create_x64(mode
, 0600, imc_debugfs_parent
,
84 imc_cmd_addr
= (u64
*)(loc
+ IMC_CNTL_BLK_CMD_OFFSET
);
85 sprintf(cmd
, "imc_cmd_%d", nid
);
86 if (!imc_debugfs_create_x64(cmd
, 0600, imc_debugfs_parent
,
94 debugfs_remove_recursive(imc_debugfs_parent
);
98 * imc_get_mem_addr_nest: Function to get nest counter memory region
101 static int imc_get_mem_addr_nest(struct device_node
*node
,
102 struct imc_pmu
*pmu_ptr
,
106 u64
*base_addr_arr
, baddr
;
109 nr_chips
= of_property_count_u32_elems(node
, "chip-id");
113 base_addr_arr
= kcalloc(nr_chips
, sizeof(u64
), GFP_KERNEL
);
117 chipid_arr
= kcalloc(nr_chips
, sizeof(u32
), GFP_KERNEL
);
121 if (of_property_read_u32_array(node
, "chip-id", chipid_arr
, nr_chips
))
124 if (of_property_read_u64_array(node
, "base-addr", base_addr_arr
,
128 pmu_ptr
->mem_info
= kcalloc(nr_chips
, sizeof(struct imc_mem_info
),
130 if (!pmu_ptr
->mem_info
)
133 for (i
= 0; i
< nr_chips
; i
++) {
134 pmu_ptr
->mem_info
[i
].id
= chipid_arr
[i
];
135 baddr
= base_addr_arr
[i
] + offset
;
136 pmu_ptr
->mem_info
[i
].vbase
= phys_to_virt(baddr
);
139 pmu_ptr
->imc_counter_mmaped
= true;
140 export_imc_mode_and_cmd(node
, pmu_ptr
);
141 kfree(base_addr_arr
);
146 kfree(pmu_ptr
->mem_info
);
147 kfree(base_addr_arr
);
153 * imc_pmu_create : Takes the parent device which is the pmu unit, pmu_index
154 * and domain as the inputs.
155 * Allocates memory for the struct imc_pmu, sets up its domain, size and offsets
157 static int imc_pmu_create(struct device_node
*parent
, int pmu_index
, int domain
)
160 struct imc_pmu
*pmu_ptr
;
164 pmu_ptr
= kzalloc(sizeof(struct imc_pmu
), GFP_KERNEL
);
169 pmu_ptr
->domain
= domain
;
171 ret
= of_property_read_u32(parent
, "size", &pmu_ptr
->counter_mem_size
);
177 if (!of_property_read_u32(parent
, "offset", &offset
)) {
178 if (imc_get_mem_addr_nest(parent
, pmu_ptr
, offset
)) {
184 /* Function to register IMC pmu */
185 ret
= init_imc_pmu(parent
, pmu_ptr
, pmu_index
);
187 pr_err("IMC PMU %s Register failed\n", pmu_ptr
->pmu
.name
);
196 static void disable_nest_pmu_counters(void)
199 const struct cpumask
*l_cpumask
;
202 for_each_node_with_cpus(nid
) {
203 l_cpumask
= cpumask_of_node(nid
);
204 cpu
= cpumask_first_and(l_cpumask
, cpu_online_mask
);
205 if (cpu
>= nr_cpu_ids
)
207 opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST
,
208 get_hard_smp_processor_id(cpu
));
213 static void disable_core_pmu_counters(void)
219 /* Disable the IMC Core functions */
220 cores_map
= cpu_online_cores_map();
221 for_each_cpu(cpu
, &cores_map
) {
222 rc
= opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE
,
223 get_hard_smp_processor_id(cpu
));
225 pr_err("%s: Failed to stop Core (cpu = %d)\n",
231 int get_max_nest_dev(void)
233 struct device_node
*node
;
234 u32 pmu_units
= 0, type
;
236 for_each_compatible_node(node
, NULL
, IMC_DTB_UNIT_COMPAT
) {
237 if (of_property_read_u32(node
, "type", &type
))
240 if (type
== IMC_TYPE_CHIP
)
247 static int opal_imc_counters_probe(struct platform_device
*pdev
)
249 struct device_node
*imc_dev
= pdev
->dev
.of_node
;
250 int pmu_count
= 0, domain
;
254 * Check whether this is kdump kernel. If yes, force the engines to
257 if (is_kdump_kernel()) {
258 disable_nest_pmu_counters();
259 disable_core_pmu_counters();
263 for_each_compatible_node(imc_dev
, NULL
, IMC_DTB_UNIT_COMPAT
) {
264 if (of_property_read_u32(imc_dev
, "type", &type
)) {
265 pr_warn("IMC Device without type property\n");
271 domain
= IMC_DOMAIN_NEST
;
274 domain
=IMC_DOMAIN_CORE
;
276 case IMC_TYPE_THREAD
:
277 domain
= IMC_DOMAIN_THREAD
;
280 pr_warn("IMC Unknown Device type \n");
285 if (!imc_pmu_create(imc_dev
, pmu_count
, domain
)) {
286 if (domain
== IMC_DOMAIN_NEST
)
291 /* If none of the nest units are registered, remove debugfs interface */
293 debugfs_remove_recursive(imc_debugfs_parent
);
298 static void opal_imc_counters_shutdown(struct platform_device
*pdev
)
301 * Function only stops the engines which is bare minimum.
302 * TODO: Need to handle proper memory cleanup and pmu
305 disable_nest_pmu_counters();
306 disable_core_pmu_counters();
309 static const struct of_device_id opal_imc_match
[] = {
310 { .compatible
= IMC_DTB_COMPAT
},
314 static struct platform_driver opal_imc_driver
= {
316 .name
= "opal-imc-counters",
317 .of_match_table
= opal_imc_match
,
319 .probe
= opal_imc_counters_probe
,
320 .shutdown
= opal_imc_counters_shutdown
,
323 builtin_platform_driver(opal_imc_driver
);