Path: blob/master/arch/powerpc/platforms/powernv/opal-imc.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* OPAL IMC interface detection driver3* Supported on POWERNV platform4*5* Copyright (C) 2017 Madhavan Srinivasan, IBM Corporation.6* (C) 2017 Anju T Sudhakar, IBM Corporation.7* (C) 2017 Hemant K Shaw, IBM Corporation.8*/9#include <linux/kernel.h>10#include <linux/platform_device.h>11#include <linux/of.h>12#include <linux/of_address.h>13#include <linux/crash_dump.h>14#include <linux/debugfs.h>15#include <asm/opal.h>16#include <asm/io.h>17#include <asm/imc-pmu.h>18#include <asm/cputhreads.h>1920static struct dentry *imc_debugfs_parent;2122/* Helpers to export imc command and mode via debugfs */23static int imc_mem_get(void *data, u64 *val)24{25*val = cpu_to_be64(*(u64 *)data);26return 0;27}2829static int imc_mem_set(void *data, u64 val)30{31*(u64 *)data = cpu_to_be64(val);32return 0;33}34DEFINE_DEBUGFS_ATTRIBUTE(fops_imc_x64, imc_mem_get, imc_mem_set, "0x%016llx\n");3536static void imc_debugfs_create_x64(const char *name, umode_t mode,37struct dentry *parent, u64 *value)38{39debugfs_create_file_unsafe(name, mode, parent, value, &fops_imc_x64);40}4142/*43* export_imc_mode_and_cmd: Create a debugfs interface44* for imc_cmd and imc_mode45* for each node in the system.46* imc_mode and imc_cmd can be changed by echo into47* this interface.48*/49static void export_imc_mode_and_cmd(struct device_node *node,50struct imc_pmu *pmu_ptr)51{52static u64 loc, *imc_mode_addr, *imc_cmd_addr;53char mode[16], cmd[16];54u32 cb_offset;55struct imc_mem_info *ptr = pmu_ptr->mem_info;5657imc_debugfs_parent = debugfs_create_dir("imc", arch_debugfs_dir);5859if (of_property_read_u32(node, "cb_offset", &cb_offset))60cb_offset = IMC_CNTL_BLK_OFFSET;6162while (ptr->vbase != NULL) {63loc = (u64)(ptr->vbase) + cb_offset;64imc_mode_addr = (u64 *)(loc + IMC_CNTL_BLK_MODE_OFFSET);65sprintf(mode, "imc_mode_%d", (u32)(ptr->id));66imc_debugfs_create_x64(mode, 0600, imc_debugfs_parent,67imc_mode_addr);6869imc_cmd_addr = (u64 *)(loc + IMC_CNTL_BLK_CMD_OFFSET);70sprintf(cmd, "imc_cmd_%d", (u32)(ptr->id));71imc_debugfs_create_x64(cmd, 0600, imc_debugfs_parent,72imc_cmd_addr);73ptr++;74}75}7677/*78* imc_get_mem_addr_nest: Function to get nest counter memory region79* for each chip80*/81static int imc_get_mem_addr_nest(struct device_node *node,82struct imc_pmu *pmu_ptr,83u32 offset)84{85int nr_chips = 0, i;86u64 *base_addr_arr, baddr;87u32 *chipid_arr;8889nr_chips = of_property_count_u32_elems(node, "chip-id");90if (nr_chips <= 0)91return -ENODEV;9293base_addr_arr = kcalloc(nr_chips, sizeof(*base_addr_arr), GFP_KERNEL);94if (!base_addr_arr)95return -ENOMEM;9697chipid_arr = kcalloc(nr_chips, sizeof(*chipid_arr), GFP_KERNEL);98if (!chipid_arr) {99kfree(base_addr_arr);100return -ENOMEM;101}102103if (of_property_read_u32_array(node, "chip-id", chipid_arr, nr_chips))104goto error;105106if (of_property_read_u64_array(node, "base-addr", base_addr_arr,107nr_chips))108goto error;109110pmu_ptr->mem_info = kcalloc(nr_chips + 1, sizeof(*pmu_ptr->mem_info),111GFP_KERNEL);112if (!pmu_ptr->mem_info)113goto error;114115for (i = 0; i < nr_chips; i++) {116pmu_ptr->mem_info[i].id = chipid_arr[i];117baddr = base_addr_arr[i] + offset;118pmu_ptr->mem_info[i].vbase = phys_to_virt(baddr);119}120121pmu_ptr->imc_counter_mmaped = true;122kfree(base_addr_arr);123kfree(chipid_arr);124return 0;125126error:127kfree(base_addr_arr);128kfree(chipid_arr);129return -1;130}131132/*133* imc_pmu_create : Takes the parent device which is the pmu unit, pmu_index134* and domain as the inputs.135* Allocates memory for the struct imc_pmu, sets up its domain, size and offsets136*/137static struct imc_pmu *imc_pmu_create(struct device_node *parent, int pmu_index, int domain)138{139int ret = 0;140struct imc_pmu *pmu_ptr;141u32 offset;142143/* Return for unknown domain */144if (domain < 0)145return NULL;146147/* memory for pmu */148pmu_ptr = kzalloc(sizeof(*pmu_ptr), GFP_KERNEL);149if (!pmu_ptr)150return NULL;151152/* Set the domain */153pmu_ptr->domain = domain;154155ret = of_property_read_u32(parent, "size", &pmu_ptr->counter_mem_size);156if (ret)157goto free_pmu;158159if (!of_property_read_u32(parent, "offset", &offset)) {160if (imc_get_mem_addr_nest(parent, pmu_ptr, offset))161goto free_pmu;162}163164/* Function to register IMC pmu */165ret = init_imc_pmu(parent, pmu_ptr, pmu_index);166if (ret) {167pr_err("IMC PMU %s Register failed\n", pmu_ptr->pmu.name);168kfree(pmu_ptr->pmu.name);169if (pmu_ptr->domain == IMC_DOMAIN_NEST)170kfree(pmu_ptr->mem_info);171kfree(pmu_ptr);172return NULL;173}174175return pmu_ptr;176177free_pmu:178kfree(pmu_ptr);179return NULL;180}181182static void disable_nest_pmu_counters(void)183{184int nid, cpu;185const struct cpumask *l_cpumask;186187cpus_read_lock();188for_each_node_with_cpus(nid) {189l_cpumask = cpumask_of_node(nid);190cpu = cpumask_first_and(l_cpumask, cpu_online_mask);191if (cpu >= nr_cpu_ids)192continue;193opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,194get_hard_smp_processor_id(cpu));195}196cpus_read_unlock();197}198199static void disable_core_pmu_counters(void)200{201int cpu, rc;202203cpus_read_lock();204/* Disable the IMC Core functions */205for_each_online_cpu(cpu) {206if (cpu_first_thread_sibling(cpu) != cpu)207continue;208rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,209get_hard_smp_processor_id(cpu));210if (rc)211pr_err("%s: Failed to stop Core (cpu = %d)\n",212__func__, cpu);213}214cpus_read_unlock();215}216217int get_max_nest_dev(void)218{219struct device_node *node;220u32 pmu_units = 0, type;221222for_each_compatible_node(node, NULL, IMC_DTB_UNIT_COMPAT) {223if (of_property_read_u32(node, "type", &type))224continue;225226if (type == IMC_TYPE_CHIP)227pmu_units++;228}229230return pmu_units;231}232233static int opal_imc_counters_probe(struct platform_device *pdev)234{235struct device_node *imc_dev = pdev->dev.of_node;236struct imc_pmu *pmu;237int pmu_count = 0, domain;238bool core_imc_reg = false, thread_imc_reg = false;239u32 type;240241/*242* Check whether this is kdump kernel. If yes, force the engines to243* stop and return.244*/245if (is_kdump_kernel()) {246disable_nest_pmu_counters();247disable_core_pmu_counters();248return -ENODEV;249}250251for_each_compatible_node(imc_dev, NULL, IMC_DTB_UNIT_COMPAT) {252pmu = NULL;253if (of_property_read_u32(imc_dev, "type", &type)) {254pr_warn("IMC Device without type property\n");255continue;256}257258switch (type) {259case IMC_TYPE_CHIP:260domain = IMC_DOMAIN_NEST;261break;262case IMC_TYPE_CORE:263domain =IMC_DOMAIN_CORE;264break;265case IMC_TYPE_THREAD:266domain = IMC_DOMAIN_THREAD;267break;268case IMC_TYPE_TRACE:269domain = IMC_DOMAIN_TRACE;270break;271default:272pr_warn("IMC Unknown Device type \n");273domain = -1;274break;275}276277pmu = imc_pmu_create(imc_dev, pmu_count, domain);278if (pmu != NULL) {279if (domain == IMC_DOMAIN_NEST) {280if (!imc_debugfs_parent)281export_imc_mode_and_cmd(imc_dev, pmu);282pmu_count++;283}284if (domain == IMC_DOMAIN_CORE)285core_imc_reg = true;286if (domain == IMC_DOMAIN_THREAD)287thread_imc_reg = true;288}289}290291/* If core imc is not registered, unregister thread-imc */292if (!core_imc_reg && thread_imc_reg)293unregister_thread_imc();294295return 0;296}297298static void opal_imc_counters_shutdown(struct platform_device *pdev)299{300/*301* Function only stops the engines which is bare minimum.302* TODO: Need to handle proper memory cleanup and pmu303* unregister.304*/305disable_nest_pmu_counters();306disable_core_pmu_counters();307}308309static const struct of_device_id opal_imc_match[] = {310{ .compatible = IMC_DTB_COMPAT },311{},312};313314static struct platform_driver opal_imc_driver = {315.driver = {316.name = "opal-imc-counters",317.of_match_table = opal_imc_match,318},319.probe = opal_imc_counters_probe,320.shutdown = opal_imc_counters_shutdown,321};322323builtin_platform_driver(opal_imc_driver);324325326