imc-pmu.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. /*
  2. * In-Memory Collection (IMC) Performance Monitor counter support.
  3. *
  4. * Copyright (C) 2017 Madhavan Srinivasan, IBM Corporation.
  5. * (C) 2017 Anju T Sudhakar, IBM Corporation.
  6. * (C) 2017 Hemant K Shaw, IBM Corporation.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or later version.
  12. */
  13. #include <linux/perf_event.h>
  14. #include <linux/slab.h>
  15. #include <asm/opal.h>
  16. #include <asm/imc-pmu.h>
  17. #include <asm/cputhreads.h>
  18. #include <asm/smp.h>
  19. #include <linux/string.h>
  20. /* Nest IMC data structures and variables */
  21. /*
  22. * Used to avoid races in counting the nest-pmu units during hotplug
  23. * register and unregister
  24. */
  25. static DEFINE_MUTEX(nest_init_lock);
  26. static DEFINE_PER_CPU(struct imc_pmu_ref *, local_nest_imc_refc);
  27. static struct imc_pmu **per_nest_pmu_arr;
  28. static cpumask_t nest_imc_cpumask;
  29. struct imc_pmu_ref *nest_imc_refc;
  30. static int nest_pmus;
  31. /* Core IMC data structures and variables */
  32. static cpumask_t core_imc_cpumask;
  33. struct imc_pmu_ref *core_imc_refc;
  34. static struct imc_pmu *core_imc_pmu;
  35. /* Thread IMC data structures and variables */
  36. static DEFINE_PER_CPU(u64 *, thread_imc_mem);
  37. static struct imc_pmu *thread_imc_pmu;
  38. static int thread_imc_mem_size;
  39. struct imc_pmu *imc_event_to_pmu(struct perf_event *event)
  40. {
  41. return container_of(event->pmu, struct imc_pmu, pmu);
  42. }
  43. PMU_FORMAT_ATTR(event, "config:0-40");
  44. PMU_FORMAT_ATTR(offset, "config:0-31");
  45. PMU_FORMAT_ATTR(rvalue, "config:32");
  46. PMU_FORMAT_ATTR(mode, "config:33-40");
  47. static struct attribute *imc_format_attrs[] = {
  48. &format_attr_event.attr,
  49. &format_attr_offset.attr,
  50. &format_attr_rvalue.attr,
  51. &format_attr_mode.attr,
  52. NULL,
  53. };
  54. static struct attribute_group imc_format_group = {
  55. .name = "format",
  56. .attrs = imc_format_attrs,
  57. };
  58. /* Get the cpumask printed to a buffer "buf" */
  59. static ssize_t imc_pmu_cpumask_get_attr(struct device *dev,
  60. struct device_attribute *attr,
  61. char *buf)
  62. {
  63. struct pmu *pmu = dev_get_drvdata(dev);
  64. struct imc_pmu *imc_pmu = container_of(pmu, struct imc_pmu, pmu);
  65. cpumask_t *active_mask;
  66. switch(imc_pmu->domain){
  67. case IMC_DOMAIN_NEST:
  68. active_mask = &nest_imc_cpumask;
  69. break;
  70. case IMC_DOMAIN_CORE:
  71. active_mask = &core_imc_cpumask;
  72. break;
  73. default:
  74. return 0;
  75. }
  76. return cpumap_print_to_pagebuf(true, buf, active_mask);
  77. }
  78. static DEVICE_ATTR(cpumask, S_IRUGO, imc_pmu_cpumask_get_attr, NULL);
  79. static struct attribute *imc_pmu_cpumask_attrs[] = {
  80. &dev_attr_cpumask.attr,
  81. NULL,
  82. };
  83. static struct attribute_group imc_pmu_cpumask_attr_group = {
  84. .attrs = imc_pmu_cpumask_attrs,
  85. };
  86. /* device_str_attr_create : Populate event "name" and string "str" in attribute */
  87. static struct attribute *device_str_attr_create(const char *name, const char *str)
  88. {
  89. struct perf_pmu_events_attr *attr;
  90. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  91. if (!attr)
  92. return NULL;
  93. sysfs_attr_init(&attr->attr.attr);
  94. attr->event_str = str;
  95. attr->attr.attr.name = name;
  96. attr->attr.attr.mode = 0444;
  97. attr->attr.show = perf_event_sysfs_show;
  98. return &attr->attr.attr;
  99. }
  100. static int imc_parse_event(struct device_node *np, const char *scale,
  101. const char *unit, const char *prefix,
  102. u32 base, struct imc_events *event)
  103. {
  104. const char *s;
  105. u32 reg;
  106. if (of_property_read_u32(np, "reg", &reg))
  107. goto error;
  108. /* Add the base_reg value to the "reg" */
  109. event->value = base + reg;
  110. if (of_property_read_string(np, "event-name", &s))
  111. goto error;
  112. event->name = kasprintf(GFP_KERNEL, "%s%s", prefix, s);
  113. if (!event->name)
  114. goto error;
  115. if (of_property_read_string(np, "scale", &s))
  116. s = scale;
  117. if (s) {
  118. event->scale = kstrdup(s, GFP_KERNEL);
  119. if (!event->scale)
  120. goto error;
  121. }
  122. if (of_property_read_string(np, "unit", &s))
  123. s = unit;
  124. if (s) {
  125. event->unit = kstrdup(s, GFP_KERNEL);
  126. if (!event->unit)
  127. goto error;
  128. }
  129. return 0;
  130. error:
  131. kfree(event->unit);
  132. kfree(event->scale);
  133. kfree(event->name);
  134. return -EINVAL;
  135. }
  136. /*
  137. * imc_free_events: Function to cleanup the events list, having
  138. * "nr_entries".
  139. */
  140. static void imc_free_events(struct imc_events *events, int nr_entries)
  141. {
  142. int i;
  143. /* Nothing to clean, return */
  144. if (!events)
  145. return;
  146. for (i = 0; i < nr_entries; i++) {
  147. kfree(events[i].unit);
  148. kfree(events[i].scale);
  149. kfree(events[i].name);
  150. }
  151. kfree(events);
  152. }
  153. /*
  154. * update_events_in_group: Update the "events" information in an attr_group
  155. * and assign the attr_group to the pmu "pmu".
  156. */
  157. static int update_events_in_group(struct device_node *node, struct imc_pmu *pmu)
  158. {
  159. struct attribute_group *attr_group;
  160. struct attribute **attrs, *dev_str;
  161. struct device_node *np, *pmu_events;
  162. u32 handle, base_reg;
  163. int i = 0, j = 0, ct, ret;
  164. const char *prefix, *g_scale, *g_unit;
  165. const char *ev_val_str, *ev_scale_str, *ev_unit_str;
  166. if (!of_property_read_u32(node, "events", &handle))
  167. pmu_events = of_find_node_by_phandle(handle);
  168. else
  169. return 0;
  170. /* Did not find any node with a given phandle */
  171. if (!pmu_events)
  172. return 0;
  173. /* Get a count of number of child nodes */
  174. ct = of_get_child_count(pmu_events);
  175. /* Get the event prefix */
  176. if (of_property_read_string(node, "events-prefix", &prefix))
  177. return 0;
  178. /* Get a global unit and scale data if available */
  179. if (of_property_read_string(node, "scale", &g_scale))
  180. g_scale = NULL;
  181. if (of_property_read_string(node, "unit", &g_unit))
  182. g_unit = NULL;
  183. /* "reg" property gives out the base offset of the counters data */
  184. of_property_read_u32(node, "reg", &base_reg);
  185. /* Allocate memory for the events */
  186. pmu->events = kcalloc(ct, sizeof(struct imc_events), GFP_KERNEL);
  187. if (!pmu->events)
  188. return -ENOMEM;
  189. ct = 0;
  190. /* Parse the events and update the struct */
  191. for_each_child_of_node(pmu_events, np) {
  192. ret = imc_parse_event(np, g_scale, g_unit, prefix, base_reg, &pmu->events[ct]);
  193. if (!ret)
  194. ct++;
  195. }
  196. /* Allocate memory for attribute group */
  197. attr_group = kzalloc(sizeof(*attr_group), GFP_KERNEL);
  198. if (!attr_group) {
  199. imc_free_events(pmu->events, ct);
  200. return -ENOMEM;
  201. }
  202. /*
  203. * Allocate memory for attributes.
  204. * Since we have count of events for this pmu, we also allocate
  205. * memory for the scale and unit attribute for now.
  206. * "ct" has the total event structs added from the events-parent node.
  207. * So allocate three times the "ct" (this includes event, event_scale and
  208. * event_unit).
  209. */
  210. attrs = kcalloc(((ct * 3) + 1), sizeof(struct attribute *), GFP_KERNEL);
  211. if (!attrs) {
  212. kfree(attr_group);
  213. imc_free_events(pmu->events, ct);
  214. return -ENOMEM;
  215. }
  216. attr_group->name = "events";
  217. attr_group->attrs = attrs;
  218. do {
  219. ev_val_str = kasprintf(GFP_KERNEL, "event=0x%x", pmu->events[i].value);
  220. dev_str = device_str_attr_create(pmu->events[i].name, ev_val_str);
  221. if (!dev_str)
  222. continue;
  223. attrs[j++] = dev_str;
  224. if (pmu->events[i].scale) {
  225. ev_scale_str = kasprintf(GFP_KERNEL, "%s.scale", pmu->events[i].name);
  226. dev_str = device_str_attr_create(ev_scale_str, pmu->events[i].scale);
  227. if (!dev_str)
  228. continue;
  229. attrs[j++] = dev_str;
  230. }
  231. if (pmu->events[i].unit) {
  232. ev_unit_str = kasprintf(GFP_KERNEL, "%s.unit", pmu->events[i].name);
  233. dev_str = device_str_attr_create(ev_unit_str, pmu->events[i].unit);
  234. if (!dev_str)
  235. continue;
  236. attrs[j++] = dev_str;
  237. }
  238. } while (++i < ct);
  239. /* Save the event attribute */
  240. pmu->attr_groups[IMC_EVENT_ATTR] = attr_group;
  241. return 0;
  242. }
  243. /* get_nest_pmu_ref: Return the imc_pmu_ref struct for the given node */
  244. static struct imc_pmu_ref *get_nest_pmu_ref(int cpu)
  245. {
  246. return per_cpu(local_nest_imc_refc, cpu);
  247. }
  248. static void nest_change_cpu_context(int old_cpu, int new_cpu)
  249. {
  250. struct imc_pmu **pn = per_nest_pmu_arr;
  251. if (old_cpu < 0 || new_cpu < 0)
  252. return;
  253. while (*pn) {
  254. perf_pmu_migrate_context(&(*pn)->pmu, old_cpu, new_cpu);
  255. pn++;
  256. }
  257. }
  258. static int ppc_nest_imc_cpu_offline(unsigned int cpu)
  259. {
  260. int nid, target = -1;
  261. const struct cpumask *l_cpumask;
  262. struct imc_pmu_ref *ref;
  263. /*
  264. * Check in the designated list for this cpu. Dont bother
  265. * if not one of them.
  266. */
  267. if (!cpumask_test_and_clear_cpu(cpu, &nest_imc_cpumask))
  268. return 0;
  269. /*
  270. * Check whether nest_imc is registered. We could end up here if the
  271. * cpuhotplug callback registration fails. i.e, callback invokes the
  272. * offline path for all successfully registered nodes. At this stage,
  273. * nest_imc pmu will not be registered and we should return here.
  274. *
  275. * We return with a zero since this is not an offline failure. And
  276. * cpuhp_setup_state() returns the actual failure reason to the caller,
  277. * which in turn will call the cleanup routine.
  278. */
  279. if (!nest_pmus)
  280. return 0;
  281. /*
  282. * Now that this cpu is one of the designated,
  283. * find a next cpu a) which is online and b) in same chip.
  284. */
  285. nid = cpu_to_node(cpu);
  286. l_cpumask = cpumask_of_node(nid);
  287. target = cpumask_any_but(l_cpumask, cpu);
  288. /*
  289. * Update the cpumask with the target cpu and
  290. * migrate the context if needed
  291. */
  292. if (target >= 0 && target < nr_cpu_ids) {
  293. cpumask_set_cpu(target, &nest_imc_cpumask);
  294. nest_change_cpu_context(cpu, target);
  295. } else {
  296. opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
  297. get_hard_smp_processor_id(cpu));
  298. /*
  299. * If this is the last cpu in this chip then, skip the reference
  300. * count mutex lock and make the reference count on this chip zero.
  301. */
  302. ref = get_nest_pmu_ref(cpu);
  303. if (!ref)
  304. return -EINVAL;
  305. ref->refc = 0;
  306. }
  307. return 0;
  308. }
  309. static int ppc_nest_imc_cpu_online(unsigned int cpu)
  310. {
  311. const struct cpumask *l_cpumask;
  312. static struct cpumask tmp_mask;
  313. int res;
  314. /* Get the cpumask of this node */
  315. l_cpumask = cpumask_of_node(cpu_to_node(cpu));
  316. /*
  317. * If this is not the first online CPU on this node, then
  318. * just return.
  319. */
  320. if (cpumask_and(&tmp_mask, l_cpumask, &nest_imc_cpumask))
  321. return 0;
  322. /*
  323. * If this is the first online cpu on this node
  324. * disable the nest counters by making an OPAL call.
  325. */
  326. res = opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
  327. get_hard_smp_processor_id(cpu));
  328. if (res)
  329. return res;
  330. /* Make this CPU the designated target for counter collection */
  331. cpumask_set_cpu(cpu, &nest_imc_cpumask);
  332. return 0;
  333. }
  334. static int nest_pmu_cpumask_init(void)
  335. {
  336. return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE,
  337. "perf/powerpc/imc:online",
  338. ppc_nest_imc_cpu_online,
  339. ppc_nest_imc_cpu_offline);
  340. }
  341. static void nest_imc_counters_release(struct perf_event *event)
  342. {
  343. int rc, node_id;
  344. struct imc_pmu_ref *ref;
  345. if (event->cpu < 0)
  346. return;
  347. node_id = cpu_to_node(event->cpu);
  348. /*
  349. * See if we need to disable the nest PMU.
  350. * If no events are currently in use, then we have to take a
  351. * mutex to ensure that we don't race with another task doing
  352. * enable or disable the nest counters.
  353. */
  354. ref = get_nest_pmu_ref(event->cpu);
  355. if (!ref)
  356. return;
  357. /* Take the mutex lock for this node and then decrement the reference count */
  358. mutex_lock(&ref->lock);
  359. if (ref->refc == 0) {
  360. /*
  361. * The scenario where this is true is, when perf session is
  362. * started, followed by offlining of all cpus in a given node.
  363. *
  364. * In the cpuhotplug offline path, ppc_nest_imc_cpu_offline()
  365. * function set the ref->count to zero, if the cpu which is
  366. * about to offline is the last cpu in a given node and make
  367. * an OPAL call to disable the engine in that node.
  368. *
  369. */
  370. mutex_unlock(&ref->lock);
  371. return;
  372. }
  373. ref->refc--;
  374. if (ref->refc == 0) {
  375. rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
  376. get_hard_smp_processor_id(event->cpu));
  377. if (rc) {
  378. mutex_unlock(&ref->lock);
  379. pr_err("nest-imc: Unable to stop the counters for core %d\n", node_id);
  380. return;
  381. }
  382. } else if (ref->refc < 0) {
  383. WARN(1, "nest-imc: Invalid event reference count\n");
  384. ref->refc = 0;
  385. }
  386. mutex_unlock(&ref->lock);
  387. }
  388. static int nest_imc_event_init(struct perf_event *event)
  389. {
  390. int chip_id, rc, node_id;
  391. u32 l_config, config = event->attr.config;
  392. struct imc_mem_info *pcni;
  393. struct imc_pmu *pmu;
  394. struct imc_pmu_ref *ref;
  395. bool flag = false;
  396. if (event->attr.type != event->pmu->type)
  397. return -ENOENT;
  398. /* Sampling not supported */
  399. if (event->hw.sample_period)
  400. return -EINVAL;
  401. /* unsupported modes and filters */
  402. if (event->attr.exclude_user ||
  403. event->attr.exclude_kernel ||
  404. event->attr.exclude_hv ||
  405. event->attr.exclude_idle ||
  406. event->attr.exclude_host ||
  407. event->attr.exclude_guest)
  408. return -EINVAL;
  409. if (event->cpu < 0)
  410. return -EINVAL;
  411. pmu = imc_event_to_pmu(event);
  412. /* Sanity check for config (event offset) */
  413. if ((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size)
  414. return -EINVAL;
  415. /*
  416. * Nest HW counter memory resides in a per-chip reserve-memory (HOMER).
  417. * Get the base memory addresss for this cpu.
  418. */
  419. chip_id = cpu_to_chip_id(event->cpu);
  420. pcni = pmu->mem_info;
  421. do {
  422. if (pcni->id == chip_id) {
  423. flag = true;
  424. break;
  425. }
  426. pcni++;
  427. } while (pcni);
  428. if (!flag)
  429. return -ENODEV;
  430. /*
  431. * Add the event offset to the base address.
  432. */
  433. l_config = config & IMC_EVENT_OFFSET_MASK;
  434. event->hw.event_base = (u64)pcni->vbase + l_config;
  435. node_id = cpu_to_node(event->cpu);
  436. /*
  437. * Get the imc_pmu_ref struct for this node.
  438. * Take the mutex lock and then increment the count of nest pmu events
  439. * inited.
  440. */
  441. ref = get_nest_pmu_ref(event->cpu);
  442. if (!ref)
  443. return -EINVAL;
  444. mutex_lock(&ref->lock);
  445. if (ref->refc == 0) {
  446. rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_NEST,
  447. get_hard_smp_processor_id(event->cpu));
  448. if (rc) {
  449. mutex_unlock(&ref->lock);
  450. pr_err("nest-imc: Unable to start the counters for node %d\n",
  451. node_id);
  452. return rc;
  453. }
  454. }
  455. ++ref->refc;
  456. mutex_unlock(&ref->lock);
  457. event->destroy = nest_imc_counters_release;
  458. return 0;
  459. }
  460. /*
  461. * core_imc_mem_init : Initializes memory for the current core.
  462. *
  463. * Uses alloc_pages_node() and uses the returned address as an argument to
  464. * an opal call to configure the pdbar. The address sent as an argument is
  465. * converted to physical address before the opal call is made. This is the
  466. * base address at which the core imc counters are populated.
  467. */
  468. static int core_imc_mem_init(int cpu, int size)
  469. {
  470. int nid, rc = 0, core_id = (cpu / threads_per_core);
  471. struct imc_mem_info *mem_info;
  472. /*
  473. * alloc_pages_node() will allocate memory for core in the
  474. * local node only.
  475. */
  476. nid = cpu_to_node(cpu);
  477. mem_info = &core_imc_pmu->mem_info[core_id];
  478. mem_info->id = core_id;
  479. /* We need only vbase for core counters */
  480. mem_info->vbase = page_address(alloc_pages_node(nid,
  481. GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
  482. __GFP_NOWARN, get_order(size)));
  483. if (!mem_info->vbase)
  484. return -ENOMEM;
  485. /* Init the mutex */
  486. core_imc_refc[core_id].id = core_id;
  487. mutex_init(&core_imc_refc[core_id].lock);
  488. rc = opal_imc_counters_init(OPAL_IMC_COUNTERS_CORE,
  489. __pa((void *)mem_info->vbase),
  490. get_hard_smp_processor_id(cpu));
  491. if (rc) {
  492. free_pages((u64)mem_info->vbase, get_order(size));
  493. mem_info->vbase = NULL;
  494. }
  495. return rc;
  496. }
  497. static bool is_core_imc_mem_inited(int cpu)
  498. {
  499. struct imc_mem_info *mem_info;
  500. int core_id = (cpu / threads_per_core);
  501. mem_info = &core_imc_pmu->mem_info[core_id];
  502. if (!mem_info->vbase)
  503. return false;
  504. return true;
  505. }
  506. static int ppc_core_imc_cpu_online(unsigned int cpu)
  507. {
  508. const struct cpumask *l_cpumask;
  509. static struct cpumask tmp_mask;
  510. int ret = 0;
  511. /* Get the cpumask for this core */
  512. l_cpumask = cpu_sibling_mask(cpu);
  513. /* If a cpu for this core is already set, then, don't do anything */
  514. if (cpumask_and(&tmp_mask, l_cpumask, &core_imc_cpumask))
  515. return 0;
  516. if (!is_core_imc_mem_inited(cpu)) {
  517. ret = core_imc_mem_init(cpu, core_imc_pmu->counter_mem_size);
  518. if (ret) {
  519. pr_info("core_imc memory allocation for cpu %d failed\n", cpu);
  520. return ret;
  521. }
  522. }
  523. /* set the cpu in the mask */
  524. cpumask_set_cpu(cpu, &core_imc_cpumask);
  525. return 0;
  526. }
  527. static int ppc_core_imc_cpu_offline(unsigned int cpu)
  528. {
  529. unsigned int core_id;
  530. int ncpu;
  531. struct imc_pmu_ref *ref;
  532. /*
  533. * clear this cpu out of the mask, if not present in the mask,
  534. * don't bother doing anything.
  535. */
  536. if (!cpumask_test_and_clear_cpu(cpu, &core_imc_cpumask))
  537. return 0;
  538. /*
  539. * Check whether core_imc is registered. We could end up here
  540. * if the cpuhotplug callback registration fails. i.e, callback
  541. * invokes the offline path for all sucessfully registered cpus.
  542. * At this stage, core_imc pmu will not be registered and we
  543. * should return here.
  544. *
  545. * We return with a zero since this is not an offline failure.
  546. * And cpuhp_setup_state() returns the actual failure reason
  547. * to the caller, which inturn will call the cleanup routine.
  548. */
  549. if (!core_imc_pmu->pmu.event_init)
  550. return 0;
  551. /* Find any online cpu in that core except the current "cpu" */
  552. ncpu = cpumask_any_but(cpu_sibling_mask(cpu), cpu);
  553. if (ncpu >= 0 && ncpu < nr_cpu_ids) {
  554. cpumask_set_cpu(ncpu, &core_imc_cpumask);
  555. perf_pmu_migrate_context(&core_imc_pmu->pmu, cpu, ncpu);
  556. } else {
  557. /*
  558. * If this is the last cpu in this core then, skip taking refernce
  559. * count mutex lock for this core and directly zero "refc" for
  560. * this core.
  561. */
  562. opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
  563. get_hard_smp_processor_id(cpu));
  564. core_id = cpu / threads_per_core;
  565. ref = &core_imc_refc[core_id];
  566. if (!ref)
  567. return -EINVAL;
  568. ref->refc = 0;
  569. }
  570. return 0;
  571. }
  572. static int core_imc_pmu_cpumask_init(void)
  573. {
  574. return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE,
  575. "perf/powerpc/imc_core:online",
  576. ppc_core_imc_cpu_online,
  577. ppc_core_imc_cpu_offline);
  578. }
  579. static void core_imc_counters_release(struct perf_event *event)
  580. {
  581. int rc, core_id;
  582. struct imc_pmu_ref *ref;
  583. if (event->cpu < 0)
  584. return;
  585. /*
  586. * See if we need to disable the IMC PMU.
  587. * If no events are currently in use, then we have to take a
  588. * mutex to ensure that we don't race with another task doing
  589. * enable or disable the core counters.
  590. */
  591. core_id = event->cpu / threads_per_core;
  592. /* Take the mutex lock and decrement the refernce count for this core */
  593. ref = &core_imc_refc[core_id];
  594. if (!ref)
  595. return;
  596. mutex_lock(&ref->lock);
  597. if (ref->refc == 0) {
  598. /*
  599. * The scenario where this is true is, when perf session is
  600. * started, followed by offlining of all cpus in a given core.
  601. *
  602. * In the cpuhotplug offline path, ppc_core_imc_cpu_offline()
  603. * function set the ref->count to zero, if the cpu which is
  604. * about to offline is the last cpu in a given core and make
  605. * an OPAL call to disable the engine in that core.
  606. *
  607. */
  608. mutex_unlock(&ref->lock);
  609. return;
  610. }
  611. ref->refc--;
  612. if (ref->refc == 0) {
  613. rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
  614. get_hard_smp_processor_id(event->cpu));
  615. if (rc) {
  616. mutex_unlock(&ref->lock);
  617. pr_err("IMC: Unable to stop the counters for core %d\n", core_id);
  618. return;
  619. }
  620. } else if (ref->refc < 0) {
  621. WARN(1, "core-imc: Invalid event reference count\n");
  622. ref->refc = 0;
  623. }
  624. mutex_unlock(&ref->lock);
  625. }
  626. static int core_imc_event_init(struct perf_event *event)
  627. {
  628. int core_id, rc;
  629. u64 config = event->attr.config;
  630. struct imc_mem_info *pcmi;
  631. struct imc_pmu *pmu;
  632. struct imc_pmu_ref *ref;
  633. if (event->attr.type != event->pmu->type)
  634. return -ENOENT;
  635. /* Sampling not supported */
  636. if (event->hw.sample_period)
  637. return -EINVAL;
  638. /* unsupported modes and filters */
  639. if (event->attr.exclude_user ||
  640. event->attr.exclude_kernel ||
  641. event->attr.exclude_hv ||
  642. event->attr.exclude_idle ||
  643. event->attr.exclude_host ||
  644. event->attr.exclude_guest)
  645. return -EINVAL;
  646. if (event->cpu < 0)
  647. return -EINVAL;
  648. event->hw.idx = -1;
  649. pmu = imc_event_to_pmu(event);
  650. /* Sanity check for config (event offset) */
  651. if (((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size))
  652. return -EINVAL;
  653. if (!is_core_imc_mem_inited(event->cpu))
  654. return -ENODEV;
  655. core_id = event->cpu / threads_per_core;
  656. pcmi = &core_imc_pmu->mem_info[core_id];
  657. if ((!pcmi->vbase))
  658. return -ENODEV;
  659. /* Get the core_imc mutex for this core */
  660. ref = &core_imc_refc[core_id];
  661. if (!ref)
  662. return -EINVAL;
  663. /*
  664. * Core pmu units are enabled only when it is used.
  665. * See if this is triggered for the first time.
  666. * If yes, take the mutex lock and enable the core counters.
  667. * If not, just increment the count in core_imc_refc struct.
  668. */
  669. mutex_lock(&ref->lock);
  670. if (ref->refc == 0) {
  671. rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
  672. get_hard_smp_processor_id(event->cpu));
  673. if (rc) {
  674. mutex_unlock(&ref->lock);
  675. pr_err("core-imc: Unable to start the counters for core %d\n",
  676. core_id);
  677. return rc;
  678. }
  679. }
  680. ++ref->refc;
  681. mutex_unlock(&ref->lock);
  682. event->hw.event_base = (u64)pcmi->vbase + (config & IMC_EVENT_OFFSET_MASK);
  683. event->destroy = core_imc_counters_release;
  684. return 0;
  685. }
  686. /*
  687. * Allocates a page of memory for each of the online cpus, and write the
  688. * physical base address of that page to the LDBAR for that cpu.
  689. *
  690. * LDBAR Register Layout:
  691. *
  692. * 0 4 8 12 16 20 24 28
  693. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  694. * | | [ ] [ Counter Address [8:50]
  695. * | * Mode |
  696. * | * PB Scope
  697. * * Enable/Disable
  698. *
  699. * 32 36 40 44 48 52 56 60
  700. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  701. * Counter Address [8:50] ]
  702. *
  703. */
  704. static int thread_imc_mem_alloc(int cpu_id, int size)
  705. {
  706. u64 ldbar_value, *local_mem = per_cpu(thread_imc_mem, cpu_id);
  707. int nid = cpu_to_node(cpu_id);
  708. if (!local_mem) {
  709. /*
  710. * This case could happen only once at start, since we dont
  711. * free the memory in cpu offline path.
  712. */
  713. local_mem = page_address(alloc_pages_node(nid,
  714. GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
  715. __GFP_NOWARN, get_order(size)));
  716. if (!local_mem)
  717. return -ENOMEM;
  718. per_cpu(thread_imc_mem, cpu_id) = local_mem;
  719. }
  720. ldbar_value = ((u64)local_mem & THREAD_IMC_LDBAR_MASK) | THREAD_IMC_ENABLE;
  721. mtspr(SPRN_LDBAR, ldbar_value);
  722. return 0;
  723. }
  724. static int ppc_thread_imc_cpu_online(unsigned int cpu)
  725. {
  726. return thread_imc_mem_alloc(cpu, thread_imc_mem_size);
  727. }
  728. static int ppc_thread_imc_cpu_offline(unsigned int cpu)
  729. {
  730. mtspr(SPRN_LDBAR, 0);
  731. return 0;
  732. }
  733. static int thread_imc_cpu_init(void)
  734. {
  735. return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE,
  736. "perf/powerpc/imc_thread:online",
  737. ppc_thread_imc_cpu_online,
  738. ppc_thread_imc_cpu_offline);
  739. }
  740. static int thread_imc_event_init(struct perf_event *event)
  741. {
  742. u32 config = event->attr.config;
  743. struct task_struct *target;
  744. struct imc_pmu *pmu;
  745. if (event->attr.type != event->pmu->type)
  746. return -ENOENT;
  747. /* Sampling not supported */
  748. if (event->hw.sample_period)
  749. return -EINVAL;
  750. event->hw.idx = -1;
  751. pmu = imc_event_to_pmu(event);
  752. /* Sanity check for config offset */
  753. if (((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size))
  754. return -EINVAL;
  755. target = event->hw.target;
  756. if (!target)
  757. return -EINVAL;
  758. event->pmu->task_ctx_nr = perf_sw_context;
  759. return 0;
  760. }
  761. static bool is_thread_imc_pmu(struct perf_event *event)
  762. {
  763. if (!strncmp(event->pmu->name, "thread_imc", strlen("thread_imc")))
  764. return true;
  765. return false;
  766. }
  767. static u64 * get_event_base_addr(struct perf_event *event)
  768. {
  769. u64 addr;
  770. if (is_thread_imc_pmu(event)) {
  771. addr = (u64)per_cpu(thread_imc_mem, smp_processor_id());
  772. return (u64 *)(addr + (event->attr.config & IMC_EVENT_OFFSET_MASK));
  773. }
  774. return (u64 *)event->hw.event_base;
  775. }
  776. static void thread_imc_pmu_start_txn(struct pmu *pmu,
  777. unsigned int txn_flags)
  778. {
  779. if (txn_flags & ~PERF_PMU_TXN_ADD)
  780. return;
  781. perf_pmu_disable(pmu);
  782. }
  783. static void thread_imc_pmu_cancel_txn(struct pmu *pmu)
  784. {
  785. perf_pmu_enable(pmu);
  786. }
  787. static int thread_imc_pmu_commit_txn(struct pmu *pmu)
  788. {
  789. perf_pmu_enable(pmu);
  790. return 0;
  791. }
  792. static u64 imc_read_counter(struct perf_event *event)
  793. {
  794. u64 *addr, data;
  795. /*
  796. * In-Memory Collection (IMC) counters are free flowing counters.
  797. * So we take a snapshot of the counter value on enable and save it
  798. * to calculate the delta at later stage to present the event counter
  799. * value.
  800. */
  801. addr = get_event_base_addr(event);
  802. data = be64_to_cpu(READ_ONCE(*addr));
  803. local64_set(&event->hw.prev_count, data);
  804. return data;
  805. }
  806. static void imc_event_update(struct perf_event *event)
  807. {
  808. u64 counter_prev, counter_new, final_count;
  809. counter_prev = local64_read(&event->hw.prev_count);
  810. counter_new = imc_read_counter(event);
  811. final_count = counter_new - counter_prev;
  812. /* Update the delta to the event count */
  813. local64_add(final_count, &event->count);
  814. }
  815. static void imc_event_start(struct perf_event *event, int flags)
  816. {
  817. /*
  818. * In Memory Counters are free flowing counters. HW or the microcode
  819. * keeps adding to the counter offset in memory. To get event
  820. * counter value, we snapshot the value here and we calculate
  821. * delta at later point.
  822. */
  823. imc_read_counter(event);
  824. }
  825. static void imc_event_stop(struct perf_event *event, int flags)
  826. {
  827. /*
  828. * Take a snapshot and calculate the delta and update
  829. * the event counter values.
  830. */
  831. imc_event_update(event);
  832. }
  833. static int imc_event_add(struct perf_event *event, int flags)
  834. {
  835. if (flags & PERF_EF_START)
  836. imc_event_start(event, flags);
  837. return 0;
  838. }
  839. static int thread_imc_event_add(struct perf_event *event, int flags)
  840. {
  841. int core_id;
  842. struct imc_pmu_ref *ref;
  843. if (flags & PERF_EF_START)
  844. imc_event_start(event, flags);
  845. if (!is_core_imc_mem_inited(smp_processor_id()))
  846. return -EINVAL;
  847. core_id = smp_processor_id() / threads_per_core;
  848. /*
  849. * imc pmus are enabled only when it is used.
  850. * See if this is triggered for the first time.
  851. * If yes, take the mutex lock and enable the counters.
  852. * If not, just increment the count in ref count struct.
  853. */
  854. ref = &core_imc_refc[core_id];
  855. if (!ref)
  856. return -EINVAL;
  857. mutex_lock(&ref->lock);
  858. if (ref->refc == 0) {
  859. if (opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
  860. get_hard_smp_processor_id(smp_processor_id()))) {
  861. mutex_unlock(&ref->lock);
  862. pr_err("thread-imc: Unable to start the counter\
  863. for core %d\n", core_id);
  864. return -EINVAL;
  865. }
  866. }
  867. ++ref->refc;
  868. mutex_unlock(&ref->lock);
  869. return 0;
  870. }
  871. static void thread_imc_event_del(struct perf_event *event, int flags)
  872. {
  873. int core_id;
  874. struct imc_pmu_ref *ref;
  875. /*
  876. * Take a snapshot and calculate the delta and update
  877. * the event counter values.
  878. */
  879. imc_event_update(event);
  880. core_id = smp_processor_id() / threads_per_core;
  881. ref = &core_imc_refc[core_id];
  882. mutex_lock(&ref->lock);
  883. ref->refc--;
  884. if (ref->refc == 0) {
  885. if (opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
  886. get_hard_smp_processor_id(smp_processor_id()))) {
  887. mutex_unlock(&ref->lock);
  888. pr_err("thread-imc: Unable to stop the counters\
  889. for core %d\n", core_id);
  890. return;
  891. }
  892. } else if (ref->refc < 0) {
  893. ref->refc = 0;
  894. }
  895. mutex_unlock(&ref->lock);
  896. }
  897. /* update_pmu_ops : Populate the appropriate operations for "pmu" */
  898. static int update_pmu_ops(struct imc_pmu *pmu)
  899. {
  900. pmu->pmu.task_ctx_nr = perf_invalid_context;
  901. pmu->pmu.add = imc_event_add;
  902. pmu->pmu.del = imc_event_stop;
  903. pmu->pmu.start = imc_event_start;
  904. pmu->pmu.stop = imc_event_stop;
  905. pmu->pmu.read = imc_event_update;
  906. pmu->pmu.attr_groups = pmu->attr_groups;
  907. pmu->attr_groups[IMC_FORMAT_ATTR] = &imc_format_group;
  908. switch (pmu->domain) {
  909. case IMC_DOMAIN_NEST:
  910. pmu->pmu.event_init = nest_imc_event_init;
  911. pmu->attr_groups[IMC_CPUMASK_ATTR] = &imc_pmu_cpumask_attr_group;
  912. break;
  913. case IMC_DOMAIN_CORE:
  914. pmu->pmu.event_init = core_imc_event_init;
  915. pmu->attr_groups[IMC_CPUMASK_ATTR] = &imc_pmu_cpumask_attr_group;
  916. break;
  917. case IMC_DOMAIN_THREAD:
  918. pmu->pmu.event_init = thread_imc_event_init;
  919. pmu->pmu.add = thread_imc_event_add;
  920. pmu->pmu.del = thread_imc_event_del;
  921. pmu->pmu.start_txn = thread_imc_pmu_start_txn;
  922. pmu->pmu.cancel_txn = thread_imc_pmu_cancel_txn;
  923. pmu->pmu.commit_txn = thread_imc_pmu_commit_txn;
  924. break;
  925. default:
  926. break;
  927. }
  928. return 0;
  929. }
  930. /* init_nest_pmu_ref: Initialize the imc_pmu_ref struct for all the nodes */
  931. static int init_nest_pmu_ref(void)
  932. {
  933. int nid, i, cpu;
  934. nest_imc_refc = kcalloc(num_possible_nodes(), sizeof(*nest_imc_refc),
  935. GFP_KERNEL);
  936. if (!nest_imc_refc)
  937. return -ENOMEM;
  938. i = 0;
  939. for_each_node(nid) {
  940. /*
  941. * Mutex lock to avoid races while tracking the number of
  942. * sessions using the chip's nest pmu units.
  943. */
  944. mutex_init(&nest_imc_refc[i].lock);
  945. /*
  946. * Loop to init the "id" with the node_id. Variable "i" initialized to
  947. * 0 and will be used as index to the array. "i" will not go off the
  948. * end of the array since the "for_each_node" loops for "N_POSSIBLE"
  949. * nodes only.
  950. */
  951. nest_imc_refc[i++].id = nid;
  952. }
  953. /*
  954. * Loop to init the per_cpu "local_nest_imc_refc" with the proper
  955. * "nest_imc_refc" index. This makes get_nest_pmu_ref() alot simple.
  956. */
  957. for_each_possible_cpu(cpu) {
  958. nid = cpu_to_node(cpu);
  959. for (i = 0; i < num_possible_nodes(); i++) {
  960. if (nest_imc_refc[i].id == nid) {
  961. per_cpu(local_nest_imc_refc, cpu) = &nest_imc_refc[i];
  962. break;
  963. }
  964. }
  965. }
  966. return 0;
  967. }
  968. static void cleanup_all_core_imc_memory(void)
  969. {
  970. int i, nr_cores = DIV_ROUND_UP(num_possible_cpus(), threads_per_core);
  971. struct imc_mem_info *ptr = core_imc_pmu->mem_info;
  972. int size = core_imc_pmu->counter_mem_size;
  973. /* mem_info will never be NULL */
  974. for (i = 0; i < nr_cores; i++) {
  975. if (ptr[i].vbase)
  976. free_pages((u64)ptr[i].vbase, get_order(size));
  977. }
  978. kfree(ptr);
  979. kfree(core_imc_refc);
  980. }
  981. static void thread_imc_ldbar_disable(void *dummy)
  982. {
  983. /*
  984. * By Zeroing LDBAR, we disable thread-imc
  985. * updates.
  986. */
  987. mtspr(SPRN_LDBAR, 0);
  988. }
  989. void thread_imc_disable(void)
  990. {
  991. on_each_cpu(thread_imc_ldbar_disable, NULL, 1);
  992. }
  993. static void cleanup_all_thread_imc_memory(void)
  994. {
  995. int i, order = get_order(thread_imc_mem_size);
  996. for_each_online_cpu(i) {
  997. if (per_cpu(thread_imc_mem, i))
  998. free_pages((u64)per_cpu(thread_imc_mem, i), order);
  999. }
  1000. }
  1001. /* Function to free the attr_groups which are dynamically allocated */
  1002. static void imc_common_mem_free(struct imc_pmu *pmu_ptr)
  1003. {
  1004. if (pmu_ptr->attr_groups[IMC_EVENT_ATTR])
  1005. kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]->attrs);
  1006. kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]);
  1007. }
  1008. /*
  1009. * Common function to unregister cpu hotplug callback and
  1010. * free the memory.
  1011. * TODO: Need to handle pmu unregistering, which will be
  1012. * done in followup series.
  1013. */
  1014. static void imc_common_cpuhp_mem_free(struct imc_pmu *pmu_ptr)
  1015. {
  1016. if (pmu_ptr->domain == IMC_DOMAIN_NEST) {
  1017. mutex_lock(&nest_init_lock);
  1018. if (nest_pmus == 1) {
  1019. cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE);
  1020. kfree(nest_imc_refc);
  1021. kfree(per_nest_pmu_arr);
  1022. per_nest_pmu_arr = NULL;
  1023. }
  1024. if (nest_pmus > 0)
  1025. nest_pmus--;
  1026. mutex_unlock(&nest_init_lock);
  1027. }
  1028. /* Free core_imc memory */
  1029. if (pmu_ptr->domain == IMC_DOMAIN_CORE) {
  1030. cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE);
  1031. cleanup_all_core_imc_memory();
  1032. }
  1033. /* Free thread_imc memory */
  1034. if (pmu_ptr->domain == IMC_DOMAIN_THREAD) {
  1035. cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE);
  1036. cleanup_all_thread_imc_memory();
  1037. }
  1038. }
  1039. /*
  1040. * Function to unregister thread-imc if core-imc
  1041. * is not registered.
  1042. */
  1043. void unregister_thread_imc(void)
  1044. {
  1045. imc_common_cpuhp_mem_free(thread_imc_pmu);
  1046. imc_common_mem_free(thread_imc_pmu);
  1047. perf_pmu_unregister(&thread_imc_pmu->pmu);
  1048. }
  1049. /*
  1050. * imc_mem_init : Function to support memory allocation for core imc.
  1051. */
  1052. static int imc_mem_init(struct imc_pmu *pmu_ptr, struct device_node *parent,
  1053. int pmu_index)
  1054. {
  1055. const char *s;
  1056. int nr_cores, cpu, res = -ENOMEM;
  1057. if (of_property_read_string(parent, "name", &s))
  1058. return -ENODEV;
  1059. switch (pmu_ptr->domain) {
  1060. case IMC_DOMAIN_NEST:
  1061. /* Update the pmu name */
  1062. pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s_imc", "nest_", s);
  1063. if (!pmu_ptr->pmu.name)
  1064. goto err;
  1065. /* Needed for hotplug/migration */
  1066. if (!per_nest_pmu_arr) {
  1067. per_nest_pmu_arr = kcalloc(get_max_nest_dev() + 1,
  1068. sizeof(struct imc_pmu *),
  1069. GFP_KERNEL);
  1070. if (!per_nest_pmu_arr)
  1071. goto err;
  1072. }
  1073. per_nest_pmu_arr[pmu_index] = pmu_ptr;
  1074. break;
  1075. case IMC_DOMAIN_CORE:
  1076. /* Update the pmu name */
  1077. pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
  1078. if (!pmu_ptr->pmu.name)
  1079. goto err;
  1080. nr_cores = DIV_ROUND_UP(num_possible_cpus(), threads_per_core);
  1081. pmu_ptr->mem_info = kcalloc(nr_cores, sizeof(struct imc_mem_info),
  1082. GFP_KERNEL);
  1083. if (!pmu_ptr->mem_info)
  1084. goto err;
  1085. core_imc_refc = kcalloc(nr_cores, sizeof(struct imc_pmu_ref),
  1086. GFP_KERNEL);
  1087. if (!core_imc_refc) {
  1088. kfree(pmu_ptr->mem_info);
  1089. goto err;
  1090. }
  1091. core_imc_pmu = pmu_ptr;
  1092. break;
  1093. case IMC_DOMAIN_THREAD:
  1094. /* Update the pmu name */
  1095. pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
  1096. if (!pmu_ptr->pmu.name)
  1097. goto err;
  1098. thread_imc_mem_size = pmu_ptr->counter_mem_size;
  1099. for_each_online_cpu(cpu) {
  1100. res = thread_imc_mem_alloc(cpu, pmu_ptr->counter_mem_size);
  1101. if (res) {
  1102. cleanup_all_thread_imc_memory();
  1103. goto err;
  1104. }
  1105. }
  1106. thread_imc_pmu = pmu_ptr;
  1107. break;
  1108. default:
  1109. return -EINVAL;
  1110. }
  1111. return 0;
  1112. err:
  1113. return res;
  1114. }
  1115. /*
  1116. * init_imc_pmu : Setup and register the IMC pmu device.
  1117. *
  1118. * @parent: Device tree unit node
  1119. * @pmu_ptr: memory allocated for this pmu
  1120. * @pmu_idx: Count of nest pmc registered
  1121. *
  1122. * init_imc_pmu() setup pmu cpumask and registers for a cpu hotplug callback.
  1123. * Handles failure cases and accordingly frees memory.
  1124. */
  1125. int init_imc_pmu(struct device_node *parent, struct imc_pmu *pmu_ptr, int pmu_idx)
  1126. {
  1127. int ret;
  1128. ret = imc_mem_init(pmu_ptr, parent, pmu_idx);
  1129. if (ret)
  1130. goto err_free_mem;
  1131. switch (pmu_ptr->domain) {
  1132. case IMC_DOMAIN_NEST:
  1133. /*
  1134. * Nest imc pmu need only one cpu per chip, we initialize the
  1135. * cpumask for the first nest imc pmu and use the same for the
  1136. * rest. To handle the cpuhotplug callback unregister, we track
  1137. * the number of nest pmus in "nest_pmus".
  1138. */
  1139. mutex_lock(&nest_init_lock);
  1140. if (nest_pmus == 0) {
  1141. ret = init_nest_pmu_ref();
  1142. if (ret) {
  1143. mutex_unlock(&nest_init_lock);
  1144. kfree(per_nest_pmu_arr);
  1145. per_nest_pmu_arr = NULL;
  1146. goto err_free_mem;
  1147. }
  1148. /* Register for cpu hotplug notification. */
  1149. ret = nest_pmu_cpumask_init();
  1150. if (ret) {
  1151. mutex_unlock(&nest_init_lock);
  1152. kfree(nest_imc_refc);
  1153. kfree(per_nest_pmu_arr);
  1154. per_nest_pmu_arr = NULL;
  1155. goto err_free_mem;
  1156. }
  1157. }
  1158. nest_pmus++;
  1159. mutex_unlock(&nest_init_lock);
  1160. break;
  1161. case IMC_DOMAIN_CORE:
  1162. ret = core_imc_pmu_cpumask_init();
  1163. if (ret) {
  1164. cleanup_all_core_imc_memory();
  1165. goto err_free_mem;
  1166. }
  1167. break;
  1168. case IMC_DOMAIN_THREAD:
  1169. ret = thread_imc_cpu_init();
  1170. if (ret) {
  1171. cleanup_all_thread_imc_memory();
  1172. goto err_free_mem;
  1173. }
  1174. break;
  1175. default:
  1176. return -EINVAL; /* Unknown domain */
  1177. }
  1178. ret = update_events_in_group(parent, pmu_ptr);
  1179. if (ret)
  1180. goto err_free_cpuhp_mem;
  1181. ret = update_pmu_ops(pmu_ptr);
  1182. if (ret)
  1183. goto err_free_cpuhp_mem;
  1184. ret = perf_pmu_register(&pmu_ptr->pmu, pmu_ptr->pmu.name, -1);
  1185. if (ret)
  1186. goto err_free_cpuhp_mem;
  1187. pr_debug("%s performance monitor hardware support registered\n",
  1188. pmu_ptr->pmu.name);
  1189. return 0;
  1190. err_free_cpuhp_mem:
  1191. imc_common_cpuhp_mem_free(pmu_ptr);
  1192. err_free_mem:
  1193. imc_common_mem_free(pmu_ptr);
  1194. return ret;
  1195. }