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/crash_dump.h>
15 #include <linux/debugfs.h>
18 #include <asm/imc-pmu.h>
19 #include <asm/cputhreads.h>
21 static struct dentry
*imc_debugfs_parent
;
23 /* Helpers to export imc command and mode via debugfs */
24 static int imc_mem_get(void *data
, u64
*val
)
26 *val
= cpu_to_be64(*(u64
*)data
);
30 static int imc_mem_set(void *data
, u64 val
)
32 *(u64
*)data
= cpu_to_be64(val
);
35 DEFINE_DEBUGFS_ATTRIBUTE(fops_imc_x64
, imc_mem_get
, imc_mem_set
, "0x%016llx\n");
37 static void imc_debugfs_create_x64(const char *name
, umode_t mode
,
38 struct dentry
*parent
, u64
*value
)
40 debugfs_create_file_unsafe(name
, mode
, parent
, value
, &fops_imc_x64
);
44 * export_imc_mode_and_cmd: Create a debugfs interface
45 * for imc_cmd and imc_mode
46 * for each node in the system.
47 * imc_mode and imc_cmd can be changed by echo into
50 static void export_imc_mode_and_cmd(struct device_node
*node
,
51 struct imc_pmu
*pmu_ptr
)
53 static u64 loc
, *imc_mode_addr
, *imc_cmd_addr
;
54 char mode
[16], cmd
[16];
56 struct imc_mem_info
*ptr
= pmu_ptr
->mem_info
;
58 imc_debugfs_parent
= debugfs_create_dir("imc", arch_debugfs_dir
);
60 if (of_property_read_u32(node
, "cb_offset", &cb_offset
))
61 cb_offset
= IMC_CNTL_BLK_OFFSET
;
63 while (ptr
->vbase
!= NULL
) {
64 loc
= (u64
)(ptr
->vbase
) + cb_offset
;
65 imc_mode_addr
= (u64
*)(loc
+ IMC_CNTL_BLK_MODE_OFFSET
);
66 sprintf(mode
, "imc_mode_%d", (u32
)(ptr
->id
));
67 imc_debugfs_create_x64(mode
, 0600, imc_debugfs_parent
,
70 imc_cmd_addr
= (u64
*)(loc
+ IMC_CNTL_BLK_CMD_OFFSET
);
71 sprintf(cmd
, "imc_cmd_%d", (u32
)(ptr
->id
));
72 imc_debugfs_create_x64(cmd
, 0600, imc_debugfs_parent
,
79 * imc_get_mem_addr_nest: Function to get nest counter memory region
82 static int imc_get_mem_addr_nest(struct device_node
*node
,
83 struct imc_pmu
*pmu_ptr
,
87 u64
*base_addr_arr
, baddr
;
90 nr_chips
= of_property_count_u32_elems(node
, "chip-id");
94 base_addr_arr
= kcalloc(nr_chips
, sizeof(*base_addr_arr
), GFP_KERNEL
);
98 chipid_arr
= kcalloc(nr_chips
, sizeof(*chipid_arr
), GFP_KERNEL
);
100 kfree(base_addr_arr
);
104 if (of_property_read_u32_array(node
, "chip-id", chipid_arr
, nr_chips
))
107 if (of_property_read_u64_array(node
, "base-addr", base_addr_arr
,
111 pmu_ptr
->mem_info
= kcalloc(nr_chips
+ 1, sizeof(*pmu_ptr
->mem_info
),
113 if (!pmu_ptr
->mem_info
)
116 for (i
= 0; i
< nr_chips
; i
++) {
117 pmu_ptr
->mem_info
[i
].id
= chipid_arr
[i
];
118 baddr
= base_addr_arr
[i
] + offset
;
119 pmu_ptr
->mem_info
[i
].vbase
= phys_to_virt(baddr
);
122 pmu_ptr
->imc_counter_mmaped
= true;
123 kfree(base_addr_arr
);
128 kfree(base_addr_arr
);
134 * imc_pmu_create : Takes the parent device which is the pmu unit, pmu_index
135 * and domain as the inputs.
136 * Allocates memory for the struct imc_pmu, sets up its domain, size and offsets
138 static struct imc_pmu
*imc_pmu_create(struct device_node
*parent
, int pmu_index
, int domain
)
141 struct imc_pmu
*pmu_ptr
;
144 /* Return for unknown domain */
149 pmu_ptr
= kzalloc(sizeof(*pmu_ptr
), GFP_KERNEL
);
154 pmu_ptr
->domain
= domain
;
156 ret
= of_property_read_u32(parent
, "size", &pmu_ptr
->counter_mem_size
);
160 if (!of_property_read_u32(parent
, "offset", &offset
)) {
161 if (imc_get_mem_addr_nest(parent
, pmu_ptr
, offset
))
165 /* Function to register IMC pmu */
166 ret
= init_imc_pmu(parent
, pmu_ptr
, pmu_index
);
168 pr_err("IMC PMU %s Register failed\n", pmu_ptr
->pmu
.name
);
169 kfree(pmu_ptr
->pmu
.name
);
170 if (pmu_ptr
->domain
== IMC_DOMAIN_NEST
)
171 kfree(pmu_ptr
->mem_info
);
183 static void disable_nest_pmu_counters(void)
186 const struct cpumask
*l_cpumask
;
189 for_each_node_with_cpus(nid
) {
190 l_cpumask
= cpumask_of_node(nid
);
191 cpu
= cpumask_first_and(l_cpumask
, cpu_online_mask
);
192 if (cpu
>= nr_cpu_ids
)
194 opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST
,
195 get_hard_smp_processor_id(cpu
));
200 static void disable_core_pmu_counters(void)
205 /* Disable the IMC Core functions */
206 for_each_online_cpu(cpu
) {
207 if (cpu_first_thread_sibling(cpu
) != cpu
)
209 rc
= opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE
,
210 get_hard_smp_processor_id(cpu
));
212 pr_err("%s: Failed to stop Core (cpu = %d)\n",
218 int get_max_nest_dev(void)
220 struct device_node
*node
;
221 u32 pmu_units
= 0, type
;
223 for_each_compatible_node(node
, NULL
, IMC_DTB_UNIT_COMPAT
) {
224 if (of_property_read_u32(node
, "type", &type
))
227 if (type
== IMC_TYPE_CHIP
)
234 static int opal_imc_counters_probe(struct platform_device
*pdev
)
236 struct device_node
*imc_dev
= pdev
->dev
.of_node
;
238 int pmu_count
= 0, domain
;
239 bool core_imc_reg
= false, thread_imc_reg
= false;
243 * Check whether this is kdump kernel. If yes, force the engines to
246 if (is_kdump_kernel()) {
247 disable_nest_pmu_counters();
248 disable_core_pmu_counters();
252 for_each_compatible_node(imc_dev
, NULL
, IMC_DTB_UNIT_COMPAT
) {
254 if (of_property_read_u32(imc_dev
, "type", &type
)) {
255 pr_warn("IMC Device without type property\n");
261 domain
= IMC_DOMAIN_NEST
;
264 domain
=IMC_DOMAIN_CORE
;
266 case IMC_TYPE_THREAD
:
267 domain
= IMC_DOMAIN_THREAD
;
270 domain
= IMC_DOMAIN_TRACE
;
273 pr_warn("IMC Unknown Device type \n");
278 pmu
= imc_pmu_create(imc_dev
, pmu_count
, domain
);
280 if (domain
== IMC_DOMAIN_NEST
) {
281 if (!imc_debugfs_parent
)
282 export_imc_mode_and_cmd(imc_dev
, pmu
);
285 if (domain
== IMC_DOMAIN_CORE
)
287 if (domain
== IMC_DOMAIN_THREAD
)
288 thread_imc_reg
= true;
292 /* If core imc is not registered, unregister thread-imc */
293 if (!core_imc_reg
&& thread_imc_reg
)
294 unregister_thread_imc();
299 static void opal_imc_counters_shutdown(struct platform_device
*pdev
)
302 * Function only stops the engines which is bare minimum.
303 * TODO: Need to handle proper memory cleanup and pmu
306 disable_nest_pmu_counters();
307 disable_core_pmu_counters();
310 static const struct of_device_id opal_imc_match
[] = {
311 { .compatible
= IMC_DTB_COMPAT
},
315 static struct platform_driver opal_imc_driver
= {
317 .name
= "opal-imc-counters",
318 .of_match_table
= opal_imc_match
,
320 .probe
= opal_imc_counters_probe
,
321 .shutdown
= opal_imc_counters_shutdown
,
324 builtin_platform_driver(opal_imc_driver
);