/* * imc_free_events: Function to cleanup the events list, having * "nr_entries".
*/ staticvoid imc_free_events(struct imc_events *events, int nr_entries)
{ int i;
/* Nothing to clean, return */ if (!events) return; for (i = 0; i < nr_entries; i++) {
kfree(events[i].unit);
kfree(events[i].scale);
kfree(events[i].name);
}
kfree(events);
}
/* * update_events_in_group: Update the "events" information in an attr_group * and assign the attr_group to the pmu "pmu".
*/ staticint update_events_in_group(struct device_node *node, struct imc_pmu *pmu)
{ struct attribute_group *attr_group; struct attribute **attrs, *dev_str; struct device_node *np, *pmu_events;
u32 handle, base_reg; int i = 0, j = 0, ct, ret; constchar *prefix, *g_scale, *g_unit; constchar *ev_val_str, *ev_scale_str, *ev_unit_str;
if (!of_property_read_u32(node, "events", &handle))
pmu_events = of_find_node_by_phandle(handle); else return 0;
/* Did not find any node with a given phandle */ if (!pmu_events) return 0;
/* Get a count of number of child nodes */
ct = of_get_child_count(pmu_events);
/* Get the event prefix */ if (of_property_read_string(node, "events-prefix", &prefix)) {
of_node_put(pmu_events); return 0;
}
/* Get a global unit and scale data if available */ if (of_property_read_string(node, "scale", &g_scale))
g_scale = NULL;
if (of_property_read_string(node, "unit", &g_unit))
g_unit = NULL;
/* "reg" property gives out the base offset of the counters data */
of_property_read_u32(node, "reg", &base_reg);
/* Allocate memory for the events */
pmu->events = kcalloc(ct, sizeof(struct imc_events), GFP_KERNEL); if (!pmu->events) {
of_node_put(pmu_events); return -ENOMEM;
}
ct = 0; /* Parse the events and update the struct */
for_each_child_of_node(pmu_events, np) {
ret = imc_parse_event(np, g_scale, g_unit, prefix, base_reg, &pmu->events[ct]); if (!ret)
ct++;
}
of_node_put(pmu_events);
/* Allocate memory for attribute group */
attr_group = kzalloc(sizeof(*attr_group), GFP_KERNEL); if (!attr_group) {
imc_free_events(pmu->events, ct); return -ENOMEM;
}
/* * Allocate memory for attributes. * Since we have count of events for this pmu, we also allocate * memory for the scale and unit attribute for now. * "ct" has the total event structs added from the events-parent node. * So allocate three times the "ct" (this includes event, event_scale and * event_unit).
*/
attrs = kcalloc(((ct * 3) + 1), sizeof(struct attribute *), GFP_KERNEL); if (!attrs) {
kfree(attr_group);
imc_free_events(pmu->events, ct); return -ENOMEM;
}
attr_group->name = "events";
attr_group->attrs = attrs; do {
ev_val_str = kasprintf(GFP_KERNEL, "event=0x%x", pmu->events[i].value); if (!ev_val_str) continue;
dev_str = device_str_attr_create(pmu->events[i].name, ev_val_str); if (!dev_str) continue;
attrs[j++] = dev_str; if (pmu->events[i].scale) {
ev_scale_str = kasprintf(GFP_KERNEL, "%s.scale", pmu->events[i].name); if (!ev_scale_str) continue;
dev_str = device_str_attr_create(ev_scale_str, pmu->events[i].scale); if (!dev_str) continue;
attrs[j++] = dev_str;
}
if (pmu->events[i].unit) {
ev_unit_str = kasprintf(GFP_KERNEL, "%s.unit", pmu->events[i].name); if (!ev_unit_str) continue;
dev_str = device_str_attr_create(ev_unit_str, pmu->events[i].unit); if (!dev_str) continue;
attrs[j++] = dev_str;
}
} while (++i < ct);
/* Save the event attribute */
pmu->attr_groups[IMC_EVENT_ATTR] = attr_group;
return 0;
}
/* get_nest_pmu_ref: Return the imc_pmu_ref struct for the given node */ staticstruct imc_pmu_ref *get_nest_pmu_ref(int cpu)
{ return per_cpu(local_nest_imc_refc, cpu);
}
/* * Check in the designated list for this cpu. Dont bother * if not one of them.
*/ if (!cpumask_test_and_clear_cpu(cpu, &nest_imc_cpumask)) return 0;
/* * Check whether nest_imc is registered. We could end up here if the * cpuhotplug callback registration fails. i.e, callback invokes the * offline path for all successfully registered nodes. At this stage, * nest_imc pmu will not be registered and we should return here. * * We return with a zero since this is not an offline failure. And * cpuhp_setup_state() returns the actual failure reason to the caller, * which in turn will call the cleanup routine.
*/ if (!nest_pmus) return 0;
/* * Now that this cpu is one of the designated, * find a next cpu a) which is online and b) in same chip.
*/
nid = cpu_to_node(cpu);
l_cpumask = cpumask_of_node(nid);
target = cpumask_last(l_cpumask);
/* * If this(target) is the last cpu in the cpumask for this chip, * check for any possible online cpu in the chip.
*/ if (unlikely(target == cpu))
target = cpumask_any_but(l_cpumask, cpu);
/* * Update the cpumask with the target cpu and * migrate the context if needed
*/ if (target >= 0 && target < nr_cpu_ids) {
cpumask_set_cpu(target, &nest_imc_cpumask);
nest_change_cpu_context(cpu, target);
} else {
opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
get_hard_smp_processor_id(cpu)); /* * If this is the last cpu in this chip then, skip the reference * count lock and make the reference count on this chip zero.
*/
ref = get_nest_pmu_ref(cpu); if (!ref) return -EINVAL;
/* Get the cpumask of this node */
l_cpumask = cpumask_of_node(cpu_to_node(cpu));
/* * If this is not the first online CPU on this node, then * just return.
*/ if (cpumask_and(&tmp_mask, l_cpumask, &nest_imc_cpumask)) return 0;
/* * If this is the first online cpu on this node * disable the nest counters by making an OPAL call.
*/
res = opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
get_hard_smp_processor_id(cpu)); if (res) return res;
/* Make this CPU the designated target for counter collection */
cpumask_set_cpu(cpu, &nest_imc_cpumask); return 0;
}
/* * See if we need to disable the nest PMU. * If no events are currently in use, then we have to take a * lock to ensure that we don't race with another task doing * enable or disable the nest counters.
*/
ref = get_nest_pmu_ref(event->cpu); if (!ref) return;
/* Take the lock for this node and then decrement the reference count */
spin_lock(&ref->lock); if (ref->refc == 0) { /* * The scenario where this is true is, when perf session is * started, followed by offlining of all cpus in a given node. * * In the cpuhotplug offline path, ppc_nest_imc_cpu_offline() * function set the ref->count to zero, if the cpu which is * about to offline is the last cpu in a given node and make * an OPAL call to disable the engine in that node. *
*/
spin_unlock(&ref->lock); return;
}
ref->refc--; if (ref->refc == 0) {
rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
get_hard_smp_processor_id(event->cpu)); if (rc) {
spin_unlock(&ref->lock);
pr_err("nest-imc: Unable to stop the counters for core %d\n", node_id); return;
}
} elseif (ref->refc < 0) {
WARN(1, "nest-imc: Invalid event reference count\n");
ref->refc = 0;
}
spin_unlock(&ref->lock);
}
if (event->attr.type != event->pmu->type) return -ENOENT;
/* Sampling not supported */ if (event->hw.sample_period) return -EINVAL;
if (event->cpu < 0) return -EINVAL;
pmu = imc_event_to_pmu(event);
/* Sanity check for config (event offset) */ if ((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size) return -EINVAL;
/* * Nest HW counter memory resides in a per-chip reserve-memory (HOMER). * Get the base memory address for this cpu.
*/
chip_id = cpu_to_chip_id(event->cpu);
/* Return, if chip_id is not valid */ if (chip_id < 0) return -ENODEV;
pcni = pmu->mem_info; do { if (pcni->id == chip_id) {
flag = true; break;
}
pcni++;
} while (pcni->vbase);
if (!flag) return -ENODEV;
/* * Add the event offset to the base address.
*/
l_config = config & IMC_EVENT_OFFSET_MASK;
event->hw.event_base = (u64)pcni->vbase + l_config;
node_id = cpu_to_node(event->cpu);
/* * Get the imc_pmu_ref struct for this node. * Take the lock and then increment the count of nest pmu events inited.
*/
ref = get_nest_pmu_ref(event->cpu); if (!ref) return -EINVAL;
spin_lock(&ref->lock); if (ref->refc == 0) {
rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_NEST,
get_hard_smp_processor_id(event->cpu)); if (rc) {
spin_unlock(&ref->lock);
pr_err("nest-imc: Unable to start the counters for node %d\n",
node_id); return rc;
}
}
++ref->refc;
spin_unlock(&ref->lock);
/* * core_imc_mem_init : Initializes memory for the current core. * * Uses alloc_pages_node() and uses the returned address as an argument to * an opal call to configure the pdbar. The address sent as an argument is * converted to physical address before the opal call is made. This is the * base address at which the core imc counters are populated.
*/ staticint core_imc_mem_init(int cpu, int size)
{ int nid, rc = 0, core_id = (cpu / threads_per_core); struct imc_mem_info *mem_info; struct page *page;
/* * alloc_pages_node() will allocate memory for core in the * local node only.
*/
nid = cpu_to_node(cpu);
mem_info = &core_imc_pmu->mem_info[core_id];
mem_info->id = core_id;
/* We need only vbase for core counters */
page = alloc_pages_node(nid,
GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
__GFP_NOWARN, get_order(size)); if (!page) return -ENOMEM;
mem_info->vbase = page_address(page);
mem_info = &core_imc_pmu->mem_info[core_id]; if (!mem_info->vbase) returnfalse;
returntrue;
}
staticint ppc_core_imc_cpu_online(unsignedint cpu)
{ conststruct cpumask *l_cpumask; staticstruct cpumask tmp_mask; int ret = 0;
/* Get the cpumask for this core */
l_cpumask = cpu_sibling_mask(cpu);
/* If a cpu for this core is already set, then, don't do anything */ if (cpumask_and(&tmp_mask, l_cpumask, &core_imc_cpumask)) return 0;
if (!is_core_imc_mem_inited(cpu)) {
ret = core_imc_mem_init(cpu, core_imc_pmu->counter_mem_size); if (ret) {
pr_info("core_imc memory allocation for cpu %d failed\n", cpu); return ret;
}
}
/* set the cpu in the mask */
cpumask_set_cpu(cpu, &core_imc_cpumask); return 0;
}
/* * clear this cpu out of the mask, if not present in the mask, * don't bother doing anything.
*/ if (!cpumask_test_and_clear_cpu(cpu, &core_imc_cpumask)) return 0;
/* * Check whether core_imc is registered. We could end up here * if the cpuhotplug callback registration fails. i.e, callback * invokes the offline path for all successfully registered cpus. * At this stage, core_imc pmu will not be registered and we * should return here. * * We return with a zero since this is not an offline failure. * And cpuhp_setup_state() returns the actual failure reason * to the caller, which inturn will call the cleanup routine.
*/ if (!core_imc_pmu->pmu.event_init) return 0;
/* Find any online cpu in that core except the current "cpu" */
ncpu = cpumask_last(cpu_sibling_mask(cpu));
if (unlikely(ncpu == cpu))
ncpu = cpumask_any_but(cpu_sibling_mask(cpu), cpu);
if (ncpu >= 0 && ncpu < nr_cpu_ids) {
cpumask_set_cpu(ncpu, &core_imc_cpumask);
perf_pmu_migrate_context(&core_imc_pmu->pmu, cpu, ncpu);
} else { /* * If this is the last cpu in this core then skip taking reference * count lock for this core and directly zero "refc" for this core.
*/
opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
get_hard_smp_processor_id(cpu));
core_id = cpu / threads_per_core;
ref = &core_imc_refc[core_id]; if (!ref) return -EINVAL;
ref->refc = 0; /* * Reduce the global reference count, if this is the * last cpu in this core and core-imc event running * in this cpu.
*/
spin_lock(&imc_global_refc.lock); if (imc_global_refc.id == IMC_DOMAIN_CORE)
imc_global_refc.refc--;
/* * If no other thread is running any * event for this domain(thread/core/trace), * set the global id to zero.
*/ if (imc_global_refc.refc <= 0) {
imc_global_refc.refc = 0;
imc_global_refc.id = 0;
}
spin_unlock(&imc_global_refc.lock);
}
if (event->cpu < 0) return; /* * See if we need to disable the IMC PMU. * If no events are currently in use, then we have to take a * lock to ensure that we don't race with another task doing * enable or disable the core counters.
*/
core_id = event->cpu / threads_per_core;
/* Take the lock and decrement the refernce count for this core */
ref = &core_imc_refc[core_id]; if (!ref) return;
spin_lock(&ref->lock); if (ref->refc == 0) { /* * The scenario where this is true is, when perf session is * started, followed by offlining of all cpus in a given core. * * In the cpuhotplug offline path, ppc_core_imc_cpu_offline() * function set the ref->count to zero, if the cpu which is * about to offline is the last cpu in a given core and make * an OPAL call to disable the engine in that core. *
*/
spin_unlock(&ref->lock); return;
}
ref->refc--; if (ref->refc == 0) {
rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
get_hard_smp_processor_id(event->cpu)); if (rc) {
spin_unlock(&ref->lock);
pr_err("IMC: Unable to stop the counters for core %d\n", core_id); return;
}
} elseif (ref->refc < 0) {
WARN(1, "core-imc: Invalid event reference count\n");
ref->refc = 0;
}
spin_unlock(&ref->lock);
ref = &core_imc_refc[core_id]; if (!ref) return -EINVAL;
/* * Core pmu units are enabled only when it is used. * See if this is triggered for the first time. * If yes, take the lock and enable the core counters. * If not, just increment the count in core_imc_refc struct.
*/
spin_lock(&ref->lock); if (ref->refc == 0) {
rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
get_hard_smp_processor_id(event->cpu)); if (rc) {
spin_unlock(&ref->lock);
pr_err("core-imc: Unable to start the counters for core %d\n",
core_id); return rc;
}
}
++ref->refc;
spin_unlock(&ref->lock);
/* * Since the system can run either in accumulation or trace-mode * of IMC at a time, core-imc events are allowed only if no other * trace/thread imc events are enabled/monitored. * * Take the global lock, and check the refc.id * to know whether any other trace/thread imc * events are running.
*/
spin_lock(&imc_global_refc.lock); if (imc_global_refc.id == 0 || imc_global_refc.id == IMC_DOMAIN_CORE) { /* * No other trace/thread imc events are running in * the system, so set the refc.id to core-imc.
*/
imc_global_refc.id = IMC_DOMAIN_CORE;
imc_global_refc.refc++;
} else {
spin_unlock(&imc_global_refc.lock); return -EBUSY;
}
spin_unlock(&imc_global_refc.lock);
/* * Allocates a page of memory for each of the online cpus, and load * LDBAR with 0. * The physical base address of the page allocated for a cpu will be * written to the LDBAR for that cpu, when the thread-imc event * is added. * * LDBAR Register Layout: * * 0 4 8 12 16 20 24 28 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | * | | [ ] [ Counter Address [8:50] * | * Mode | * | * PB Scope * * Enable/Disable * * 32 36 40 44 48 52 56 60 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | * Counter Address [8:50] ] *
*/ staticint thread_imc_mem_alloc(int cpu_id, int size)
{
u64 *local_mem = per_cpu(thread_imc_mem, cpu_id); int nid = cpu_to_node(cpu_id);
if (!local_mem) { struct page *page; /* * This case could happen only once at start, since we dont * free the memory in cpu offline path.
*/
page = alloc_pages_node(nid,
GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
__GFP_NOWARN, get_order(size)); if (!page) return -ENOMEM;
local_mem = page_address(page);
staticint ppc_thread_imc_cpu_offline(unsignedint cpu)
{ /* * Set the bit 0 of LDBAR to zero. * * If bit 0 of LDBAR is unset, it will stop posting * the counter data to memory. * For thread-imc, bit 0 of LDBAR will be set to 1 in the * event_add function. So reset this bit here, to stop the updates * to memory in the cpu_offline path.
*/
mtspr(SPRN_LDBAR, (mfspr(SPRN_LDBAR) & (~(1UL << 63))));
/* Reduce the refc if thread-imc event running on this cpu */
spin_lock(&imc_global_refc.lock); if (imc_global_refc.id == IMC_DOMAIN_THREAD)
imc_global_refc.refc--;
spin_unlock(&imc_global_refc.lock);
/* Sanity check for config offset */ if (((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size)) return -EINVAL;
target = event->hw.target; if (!target) return -EINVAL;
spin_lock(&imc_global_refc.lock); /* * Check if any other trace/core imc events are running in the * system, if not set the global id to thread-imc.
*/ if (imc_global_refc.id == 0 || imc_global_refc.id == IMC_DOMAIN_THREAD) {
imc_global_refc.id = IMC_DOMAIN_THREAD;
imc_global_refc.refc++;
} else {
spin_unlock(&imc_global_refc.lock); return -EBUSY;
}
spin_unlock(&imc_global_refc.lock);
/* * In-Memory Collection (IMC) counters are free flowing counters. * So we take a snapshot of the counter value on enable and save it * to calculate the delta at later stage to present the event counter * value.
*/
addr = get_event_base_addr(event);
data = be64_to_cpu(READ_ONCE(*addr));
local64_set(&event->hw.prev_count, data);
/* Update the delta to the event count */
local64_add(final_count, &event->count);
}
staticvoid imc_event_start(struct perf_event *event, int flags)
{ /* * In Memory Counters are free flowing counters. HW or the microcode * keeps adding to the counter offset in memory. To get event * counter value, we snapshot the value here and we calculate * delta at later point.
*/
imc_read_counter(event);
}
staticvoid imc_event_stop(struct perf_event *event, int flags)
{ /* * Take a snapshot and calculate the delta and update * the event counter values.
*/
imc_event_update(event);
}
staticint imc_event_add(struct perf_event *event, int flags)
{ if (flags & PERF_EF_START)
imc_event_start(event, flags);
return 0;
}
staticint thread_imc_event_add(struct perf_event *event, int flags)
{ int core_id; struct imc_pmu_ref *ref;
u64 ldbar_value, *local_mem = per_cpu(thread_imc_mem, smp_processor_id());
if (flags & PERF_EF_START)
imc_event_start(event, flags);
if (!is_core_imc_mem_inited(smp_processor_id())) return -EINVAL;
/* * imc pmus are enabled only when it is used. * See if this is triggered for the first time. * If yes, take the lock and enable the counters. * If not, just increment the count in ref count struct.
*/
ref = &core_imc_refc[core_id]; if (!ref) return -EINVAL;
spin_lock(&ref->lock); if (ref->refc == 0) { if (opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
get_hard_smp_processor_id(smp_processor_id()))) {
spin_unlock(&ref->lock);
pr_err("thread-imc: Unable to start the counter\ for core %d\n", core_id); return -EINVAL;
}
}
++ref->refc;
spin_unlock(&ref->lock); return 0;
}
staticvoid thread_imc_event_del(struct perf_event *event, int flags)
{
int core_id; struct imc_pmu_ref *ref;
core_id = smp_processor_id() / threads_per_core;
ref = &core_imc_refc[core_id]; if (!ref) {
pr_debug("imc: Failed to get event reference count\n"); return;
}
spin_lock(&ref->lock);
ref->refc--; if (ref->refc == 0) { if (opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
get_hard_smp_processor_id(smp_processor_id()))) {
spin_unlock(&ref->lock);
pr_err("thread-imc: Unable to stop the counters\ for core %d\n", core_id); return;
}
} elseif (ref->refc < 0) {
ref->refc = 0;
}
spin_unlock(&ref->lock);
/* Set bit 0 of LDBAR to zero, to stop posting updates to memory */
mtspr(SPRN_LDBAR, (mfspr(SPRN_LDBAR) & (~(1UL << 63))));
/* * Take a snapshot and calculate the delta and update * the event counter values.
*/
imc_event_update(event);
}
/* * Allocate a page of memory for each cpu, and load LDBAR with 0.
*/ staticint trace_imc_mem_alloc(int cpu_id, int size)
{
u64 *local_mem = per_cpu(trace_imc_mem, cpu_id); int phys_id = cpu_to_node(cpu_id), rc = 0; int core_id = (cpu_id / threads_per_core);
staticint ppc_trace_imc_cpu_offline(unsignedint cpu)
{ /* * No need to set bit 0 of LDBAR to zero, as * it is set to zero for imc trace-mode * * Reduce the refc if any trace-imc event running * on this cpu.
*/
spin_lock(&imc_global_refc.lock); if (imc_global_refc.id == IMC_DOMAIN_TRACE)
imc_global_refc.refc--;
spin_unlock(&imc_global_refc.lock);
/* * Function to parse trace-imc data obtained * and to prepare the perf sample.
*/ staticint trace_imc_prepare_sample(struct trace_imc_data *mem, struct perf_sample_data *data,
u64 *prev_tb, struct perf_event_header *header, struct perf_event *event)
{ /* Sanity checks for a valid record */ if (be64_to_cpu(READ_ONCE(mem->tb1)) > *prev_tb)
*prev_tb = be64_to_cpu(READ_ONCE(mem->tb1)); else return -EINVAL;
if ((be64_to_cpu(READ_ONCE(mem->tb1)) & IMC_TRACE_RECORD_TB1_MASK) !=
be64_to_cpu(READ_ONCE(mem->tb2))) return -EINVAL;
if (cpu_has_feature(CPU_FTR_ARCH_31)) { switch (IMC_TRACE_RECORD_VAL_HVPR(be64_to_cpu(READ_ONCE(mem->val)))) { case 0:/* when MSR HV and PR not set in the trace-record */
header->misc |= PERF_RECORD_MISC_GUEST_KERNEL; break; case 1: /* MSR HV is 0 and PR is 1 */
header->misc |= PERF_RECORD_MISC_GUEST_USER; break; case 2: /* MSR HV is 1 and PR is 0 */
header->misc |= PERF_RECORD_MISC_KERNEL; break; case 3: /* MSR HV is 1 and PR is 1 */
header->misc |= PERF_RECORD_MISC_USER; break; default:
pr_info("IMC: Unable to set the flag based on MSR bits\n"); break;
}
} else { if (is_kernel_addr(data->ip))
header->misc |= PERF_RECORD_MISC_KERNEL; else
header->misc |= PERF_RECORD_MISC_USER;
}
perf_event_header__init_id(header, data, event);
return 0;
}
staticvoid dump_trace_imc_data(struct perf_event *event)
{ struct trace_imc_data *mem; int i, ret;
u64 prev_tb = 0;
mem = (struct trace_imc_data *)get_trace_imc_event_base_addr(); for (i = 0; i < (trace_imc_mem_size / sizeof(struct trace_imc_data));
i++, mem++) { struct perf_sample_data data; struct perf_event_header header;
ret = trace_imc_prepare_sample(mem, &data, &prev_tb, &header, event); if (ret) /* Exit, if not a valid record */ break; else { /* If this is a valid record, create the sample */ struct perf_output_handle handle;
if (perf_output_begin(&handle, &data, event, header.size)) return;
/* Set trace-imc bit in ldbar and load ldbar with per-thread memory address */
local_mem = get_trace_imc_event_base_addr();
ldbar_value = ((u64)local_mem & THREAD_IMC_LDBAR_MASK) | TRACE_IMC_ENABLE;
/* trace-imc reference count */ if (trace_imc_refc)
ref = &trace_imc_refc[core_id]; if (!ref) {
pr_debug("imc: Failed to get the event reference count\n"); return -EINVAL;
}
mtspr(SPRN_LDBAR, ldbar_value);
spin_lock(&ref->lock); if (ref->refc == 0) { if (opal_imc_counters_start(OPAL_IMC_COUNTERS_TRACE,
get_hard_smp_processor_id(smp_processor_id()))) {
spin_unlock(&ref->lock);
pr_err("trace-imc: Unable to start the counters for core %d\n", core_id); return -EINVAL;
}
}
++ref->refc;
spin_unlock(&ref->lock); return 0;
}
/* Return if this is a couting event */ if (event->attr.sample_period == 0) return -ENOENT;
/* * Take the global lock, and make sure * no other thread is running any core/thread imc * events
*/
spin_lock(&imc_global_refc.lock); if (imc_global_refc.id == 0 || imc_global_refc.id == IMC_DOMAIN_TRACE) { /* * No core/thread imc events are running in the * system, so set the refc.id to trace-imc.
*/
imc_global_refc.id = IMC_DOMAIN_TRACE;
imc_global_refc.refc++;
} else {
spin_unlock(&imc_global_refc.lock); return -EBUSY;
}
spin_unlock(&imc_global_refc.lock);
event->hw.idx = -1;
/* * There can only be a single PMU for perf_hw_context events which is assigned to * core PMU. Hence use "perf_sw_context" for trace_imc.
*/
event->pmu->task_ctx_nr = perf_sw_context;
event->destroy = reset_global_refc; return 0;
}
i = 0;
for_each_node(nid) { /* * Take the lock to avoid races while tracking the number of * sessions using the chip's nest pmu units.
*/
spin_lock_init(&nest_imc_refc[i].lock);
/* * Loop to init the "id" with the node_id. Variable "i" initialized to * 0 and will be used as index to the array. "i" will not go off the * end of the array since the "for_each_node" loops for "N_POSSIBLE" * nodes only.
*/
nest_imc_refc[i++].id = nid;
}
/* * Loop to init the per_cpu "local_nest_imc_refc" with the proper * "nest_imc_refc" index. This makes get_nest_pmu_ref() alot simple.
*/
for_each_possible_cpu(cpu) {
nid = cpu_to_node(cpu); for (i = 0; i < num_possible_nodes(); i++) { if (nest_imc_refc[i].id == nid) {
per_cpu(local_nest_imc_refc, cpu) = &nest_imc_refc[i]; break;
}
}
} return 0;
}
staticvoid cleanup_all_core_imc_memory(void)
{ int i, nr_cores = DIV_ROUND_UP(num_possible_cpus(), threads_per_core); struct imc_mem_info *ptr = core_imc_pmu->mem_info; int size = core_imc_pmu->counter_mem_size;
/* mem_info will never be NULL */ for (i = 0; i < nr_cores; i++) { if (ptr[i].vbase)
free_pages((u64)ptr[i].vbase, get_order(size));
}
kfree(ptr);
kfree(core_imc_refc);
}
staticvoid thread_imc_ldbar_disable(void *dummy)
{ /* * By setting 0th bit of LDBAR to zero, we disable thread-imc * updates to memory.
*/
mtspr(SPRN_LDBAR, (mfspr(SPRN_LDBAR) & (~(1UL << 63))));
}
staticvoid cleanup_all_thread_imc_memory(void)
{ int i, order = get_order(thread_imc_mem_size);
for_each_online_cpu(i) { if (per_cpu(thread_imc_mem, i))
free_pages((u64)per_cpu(thread_imc_mem, i), order);
}
}
staticvoid cleanup_all_trace_imc_memory(void)
{ int i, order = get_order(trace_imc_mem_size);
for_each_online_cpu(i) { if (per_cpu(trace_imc_mem, i))
free_pages((u64)per_cpu(trace_imc_mem, i), order);
}
kfree(trace_imc_refc);
}
/* Function to free the attr_groups which are dynamically allocated */ staticvoid imc_common_mem_free(struct imc_pmu *pmu_ptr)
{ if (pmu_ptr->attr_groups[IMC_EVENT_ATTR])
kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]->attrs);
kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]);
}
/* * Common function to unregister cpu hotplug callback and * free the memory. * TODO: Need to handle pmu unregistering, which will be * done in followup series.
*/ staticvoid imc_common_cpuhp_mem_free(struct imc_pmu *pmu_ptr)
{ if (pmu_ptr->domain == IMC_DOMAIN_NEST) {
mutex_lock(&nest_init_lock); if (nest_pmus == 1) {
cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE);
kfree(nest_imc_refc);
kfree(per_nest_pmu_arr);
per_nest_pmu_arr = NULL;
}
if (nest_pmus > 0)
nest_pmus--;
mutex_unlock(&nest_init_lock);
}
if (pmu_ptr->domain == IMC_DOMAIN_TRACE) {
cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_TRACE_IMC_ONLINE);
cleanup_all_trace_imc_memory();
}
}
/* * Function to unregister thread-imc if core-imc * is not registered.
*/ void unregister_thread_imc(void)
{
imc_common_cpuhp_mem_free(thread_imc_pmu);
imc_common_mem_free(thread_imc_pmu);
perf_pmu_unregister(&thread_imc_pmu->pmu);
}
/* * imc_mem_init : Function to support memory allocation for core imc.
*/ staticint imc_mem_init(struct imc_pmu *pmu_ptr, struct device_node *parent, int pmu_index)
{ constchar *s; int nr_cores, cpu, res = -ENOMEM;
if (of_property_read_string(parent, "name", &s)) return -ENODEV;
switch (pmu_ptr->domain) { case IMC_DOMAIN_NEST: /* Update the pmu name */
pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s_imc", "nest_", s); if (!pmu_ptr->pmu.name) goto err;
/* Needed for hotplug/migration */ if (!per_nest_pmu_arr) {
per_nest_pmu_arr = kcalloc(get_max_nest_dev() + 1, sizeof(struct imc_pmu *),
GFP_KERNEL); if (!per_nest_pmu_arr) goto err;
}
per_nest_pmu_arr[pmu_index] = pmu_ptr; break; case IMC_DOMAIN_CORE: /* Update the pmu name */
pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc"); if (!pmu_ptr->pmu.name) goto err;
/* * init_imc_pmu : Setup and register the IMC pmu device. * * @parent: Device tree unit node * @pmu_ptr: memory allocated for this pmu * @pmu_idx: Count of nest pmc registered * * init_imc_pmu() setup pmu cpumask and registers for a cpu hotplug callback. * Handles failure cases and accordingly frees memory.
*/ int init_imc_pmu(struct device_node *parent, struct imc_pmu *pmu_ptr, int pmu_idx)
{ int ret;
ret = imc_mem_init(pmu_ptr, parent, pmu_idx); if (ret) goto err_free_mem;
switch (pmu_ptr->domain) { case IMC_DOMAIN_NEST: /* * Nest imc pmu need only one cpu per chip, we initialize the * cpumask for the first nest imc pmu and use the same for the * rest. To handle the cpuhotplug callback unregister, we track * the number of nest pmus in "nest_pmus".
*/
mutex_lock(&nest_init_lock); if (nest_pmus == 0) {
ret = init_nest_pmu_ref(); if (ret) {
mutex_unlock(&nest_init_lock);
kfree(per_nest_pmu_arr);
per_nest_pmu_arr = NULL; goto err_free_mem;
} /* Register for cpu hotplug notification. */
ret = nest_pmu_cpumask_init(); if (ret) {
mutex_unlock(&nest_init_lock);
kfree(nest_imc_refc);
kfree(per_nest_pmu_arr);
per_nest_pmu_arr = NULL; goto err_free_mem;
}
}
nest_pmus++;
mutex_unlock(&nest_init_lock); break; case IMC_DOMAIN_CORE:
ret = core_imc_pmu_cpumask_init(); if (ret) {
cleanup_all_core_imc_memory(); goto err_free_mem;
}
break; case IMC_DOMAIN_THREAD:
ret = thread_imc_cpu_init(); if (ret) {
cleanup_all_thread_imc_memory(); goto err_free_mem;
}
break; case IMC_DOMAIN_TRACE:
ret = trace_imc_cpu_init(); if (ret) {
cleanup_all_trace_imc_memory(); goto err_free_mem;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.