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>
26 * imc_get_mem_addr_nest: Function to get nest counter memory region
29 static int imc_get_mem_addr_nest(struct device_node *node,
30 struct imc_pmu *pmu_ptr,
34 u64 *base_addr_arr, baddr;
37 nr_chips = of_property_count_u32_elems(node, "chip-id");
41 base_addr_arr = kcalloc(nr_chips, sizeof(u64), GFP_KERNEL);
45 chipid_arr = kcalloc(nr_chips, sizeof(u32), GFP_KERNEL);
49 if (of_property_read_u32_array(node, "chip-id", chipid_arr, nr_chips))
52 if (of_property_read_u64_array(node, "base-addr", base_addr_arr,
56 pmu_ptr->mem_info = kcalloc(nr_chips, sizeof(struct imc_mem_info),
58 if (!pmu_ptr->mem_info)
61 for (i = 0; i < nr_chips; i++) {
62 pmu_ptr->mem_info[i].id = chipid_arr[i];
63 baddr = base_addr_arr[i] + offset;
64 pmu_ptr->mem_info[i].vbase = phys_to_virt(baddr);
67 pmu_ptr->imc_counter_mmaped = true;
73 kfree(pmu_ptr->mem_info);
80 * imc_pmu_create : Takes the parent device which is the pmu unit, pmu_index
81 * and domain as the inputs.
82 * Allocates memory for the struct imc_pmu, sets up its domain, size and offsets
84 static int imc_pmu_create(struct device_node *parent, int pmu_index, int domain)
87 struct imc_pmu *pmu_ptr;
91 pmu_ptr = kzalloc(sizeof(struct imc_pmu), GFP_KERNEL);
96 pmu_ptr->domain = domain;
98 ret = of_property_read_u32(parent, "size", &pmu_ptr->counter_mem_size);
104 if (!of_property_read_u32(parent, "offset", &offset)) {
105 if (imc_get_mem_addr_nest(parent, pmu_ptr, offset)) {
111 /* Function to register IMC pmu */
112 ret = init_imc_pmu(parent, pmu_ptr, pmu_index);
114 pr_err("IMC PMU %s Register failed\n", pmu_ptr->pmu.name);
123 static void disable_nest_pmu_counters(void)
126 const struct cpumask *l_cpumask;
129 for_each_online_node(nid) {
130 l_cpumask = cpumask_of_node(nid);
131 cpu = cpumask_first(l_cpumask);
132 opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
133 get_hard_smp_processor_id(cpu));
138 static void disable_core_pmu_counters(void)
144 /* Disable the IMC Core functions */
145 cores_map = cpu_online_cores_map();
146 for_each_cpu(cpu, &cores_map) {
147 rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
148 get_hard_smp_processor_id(cpu));
150 pr_err("%s: Failed to stop Core (cpu = %d)\n",
156 int get_max_nest_dev(void)
158 struct device_node *node;
159 u32 pmu_units = 0, type;
161 for_each_compatible_node(node, NULL, IMC_DTB_UNIT_COMPAT) {
162 if (of_property_read_u32(node, "type", &type))
165 if (type == IMC_TYPE_CHIP)
172 static int opal_imc_counters_probe(struct platform_device *pdev)
174 struct device_node *imc_dev = pdev->dev.of_node;
175 int pmu_count = 0, domain;
179 * Check whether this is kdump kernel. If yes, force the engines to
182 if (is_kdump_kernel()) {
183 disable_nest_pmu_counters();
184 disable_core_pmu_counters();
188 for_each_compatible_node(imc_dev, NULL, IMC_DTB_UNIT_COMPAT) {
189 if (of_property_read_u32(imc_dev, "type", &type)) {
190 pr_warn("IMC Device without type property\n");
196 domain = IMC_DOMAIN_NEST;
199 domain =IMC_DOMAIN_CORE;
201 case IMC_TYPE_THREAD:
202 domain = IMC_DOMAIN_THREAD;
205 pr_warn("IMC Unknown Device type \n");
210 if (!imc_pmu_create(imc_dev, pmu_count, domain)) {
211 if (domain == IMC_DOMAIN_NEST)
219 static void opal_imc_counters_shutdown(struct platform_device *pdev)
222 * Function only stops the engines which is bare minimum.
223 * TODO: Need to handle proper memory cleanup and pmu
226 disable_nest_pmu_counters();
227 disable_core_pmu_counters();
230 static const struct of_device_id opal_imc_match[] = {
231 { .compatible = IMC_DTB_COMPAT },
235 static struct platform_driver opal_imc_driver = {
237 .name = "opal-imc-counters",
238 .of_match_table = opal_imc_match,
240 .probe = opal_imc_counters_probe,
241 .shutdown = opal_imc_counters_shutdown,
244 builtin_platform_driver(opal_imc_driver);