imc-pmu.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. /* Return, if chip_id is not valid */
  421. if (chip_id < 0)
  422. return -ENODEV;
  423. pcni = pmu->mem_info;
  424. do {
  425. if (pcni->id == chip_id) {
  426. flag = true;
  427. break;
  428. }
  429. pcni++;
  430. } while (pcni->vbase != 0);
  431. if (!flag)
  432. return -ENODEV;
  433. /*
  434. * Add the event offset to the base address.
  435. */
  436. l_config = config & IMC_EVENT_OFFSET_MASK;
  437. event->hw.event_base = (u64)pcni->vbase + l_config;
  438. node_id = cpu_to_node(event->cpu);
  439. /*
  440. * Get the imc_pmu_ref struct for this node.
  441. * Take the mutex lock and then increment the count of nest pmu events
  442. * inited.
  443. */
  444. ref = get_nest_pmu_ref(event->cpu);
  445. if (!ref)
  446. return -EINVAL;
  447. mutex_lock(&ref->lock);
  448. if (ref->refc == 0) {
  449. rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_NEST,
  450. get_hard_smp_processor_id(event->cpu));
  451. if (rc) {
  452. mutex_unlock(&ref->lock);
  453. pr_err("nest-imc: Unable to start the counters for node %d\n",
  454. node_id);
  455. return rc;
  456. }
  457. }
  458. ++ref->refc;
  459. mutex_unlock(&ref->lock);
  460. event->destroy = nest_imc_counters_release;
  461. return 0;
  462. }
  463. /*
  464. * core_imc_mem_init : Initializes memory for the current core.
  465. *
  466. * Uses alloc_pages_node() and uses the returned address as an argument to
  467. * an opal call to configure the pdbar. The address sent as an argument is
  468. * converted to physical address before the opal call is made. This is the
  469. * base address at which the core imc counters are populated.
  470. */
  471. static int core_imc_mem_init(int cpu, int size)
  472. {
  473. int nid, rc = 0, core_id = (cpu / threads_per_core);
  474. struct imc_mem_info *mem_info;
  475. /*
  476. * alloc_pages_node() will allocate memory for core in the
  477. * local node only.
  478. */
  479. nid = cpu_to_node(cpu);
  480. mem_info = &core_imc_pmu->mem_info[core_id];
  481. mem_info->id = core_id;
  482. /* We need only vbase for core counters */
  483. mem_info->vbase = page_address(alloc_pages_node(nid,
  484. GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
  485. __GFP_NOWARN, get_order(size)));
  486. if (!mem_info->vbase)
  487. return -ENOMEM;
  488. /* Init the mutex */
  489. core_imc_refc[core_id].id = core_id;
  490. mutex_init(&core_imc_refc[core_id].lock);
  491. rc = opal_imc_counters_init(OPAL_IMC_COUNTERS_CORE,
  492. __pa((void *)mem_info->vbase),
  493. get_hard_smp_processor_id(cpu));
  494. if (rc) {
  495. free_pages((u64)mem_info->vbase, get_order(size));
  496. mem_info->vbase = NULL;
  497. }
  498. return rc;
  499. }
  500. static bool is_core_imc_mem_inited(int cpu)
  501. {
  502. struct imc_mem_info *mem_info;
  503. int core_id = (cpu / threads_per_core);
  504. mem_info = &core_imc_pmu->mem_info[core_id];
  505. if (!mem_info->vbase)
  506. return false;
  507. return true;
  508. }
  509. static int ppc_core_imc_cpu_online(unsigned int cpu)
  510. {
  511. const struct cpumask *l_cpumask;
  512. static struct cpumask tmp_mask;
  513. int ret = 0;
  514. /* Get the cpumask for this core */
  515. l_cpumask = cpu_sibling_mask(cpu);
  516. /* If a cpu for this core is already set, then, don't do anything */
  517. if (cpumask_and(&tmp_mask, l_cpumask, &core_imc_cpumask))
  518. return 0;
  519. if (!is_core_imc_mem_inited(cpu)) {
  520. ret = core_imc_mem_init(cpu, core_imc_pmu->counter_mem_size);
  521. if (ret) {
  522. pr_info("core_imc memory allocation for cpu %d failed\n", cpu);
  523. return ret;
  524. }
  525. }
  526. /* set the cpu in the mask */
  527. cpumask_set_cpu(cpu, &core_imc_cpumask);
  528. return 0;
  529. }
  530. static int ppc_core_imc_cpu_offline(unsigned int cpu)
  531. {
  532. unsigned int core_id;
  533. int ncpu;
  534. struct imc_pmu_ref *ref;
  535. /*
  536. * clear this cpu out of the mask, if not present in the mask,
  537. * don't bother doing anything.
  538. */
  539. if (!cpumask_test_and_clear_cpu(cpu, &core_imc_cpumask))
  540. return 0;
  541. /*
  542. * Check whether core_imc is registered. We could end up here
  543. * if the cpuhotplug callback registration fails. i.e, callback
  544. * invokes the offline path for all sucessfully registered cpus.
  545. * At this stage, core_imc pmu will not be registered and we
  546. * should return here.
  547. *
  548. * We return with a zero since this is not an offline failure.
  549. * And cpuhp_setup_state() returns the actual failure reason
  550. * to the caller, which inturn will call the cleanup routine.
  551. */
  552. if (!core_imc_pmu->pmu.event_init)
  553. return 0;
  554. /* Find any online cpu in that core except the current "cpu" */
  555. ncpu = cpumask_any_but(cpu_sibling_mask(cpu), cpu);
  556. if (ncpu >= 0 && ncpu < nr_cpu_ids) {
  557. cpumask_set_cpu(ncpu, &core_imc_cpumask);
  558. perf_pmu_migrate_context(&core_imc_pmu->pmu, cpu, ncpu);
  559. } else {
  560. /*
  561. * If this is the last cpu in this core then, skip taking refernce
  562. * count mutex lock for this core and directly zero "refc" for
  563. * this core.
  564. */
  565. opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
  566. get_hard_smp_processor_id(cpu));
  567. core_id = cpu / threads_per_core;
  568. ref = &core_imc_refc[core_id];
  569. if (!ref)
  570. return -EINVAL;
  571. ref->refc = 0;
  572. }
  573. return 0;
  574. }
  575. static int core_imc_pmu_cpumask_init(void)
  576. {
  577. return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE,
  578. "perf/powerpc/imc_core:online",
  579. ppc_core_imc_cpu_online,
  580. ppc_core_imc_cpu_offline);
  581. }
  582. static void core_imc_counters_release(struct perf_event *event)
  583. {
  584. int rc, core_id;
  585. struct imc_pmu_ref *ref;
  586. if (event->cpu < 0)
  587. return;
  588. /*
  589. * See if we need to disable the IMC PMU.
  590. * If no events are currently in use, then we have to take a
  591. * mutex to ensure that we don't race with another task doing
  592. * enable or disable the core counters.
  593. */
  594. core_id = event->cpu / threads_per_core;
  595. /* Take the mutex lock and decrement the refernce count for this core */
  596. ref = &core_imc_refc[core_id];
  597. if (!ref)
  598. return;
  599. mutex_lock(&ref->lock);
  600. if (ref->refc == 0) {
  601. /*
  602. * The scenario where this is true is, when perf session is
  603. * started, followed by offlining of all cpus in a given core.
  604. *
  605. * In the cpuhotplug offline path, ppc_core_imc_cpu_offline()
  606. * function set the ref->count to zero, if the cpu which is
  607. * about to offline is the last cpu in a given core and make
  608. * an OPAL call to disable the engine in that core.
  609. *
  610. */
  611. mutex_unlock(&ref->lock);
  612. return;
  613. }
  614. ref->refc--;
  615. if (ref->refc == 0) {
  616. rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
  617. get_hard_smp_processor_id(event->cpu));
  618. if (rc) {
  619. mutex_unlock(&ref->lock);
  620. pr_err("IMC: Unable to stop the counters for core %d\n", core_id);
  621. return;
  622. }
  623. } else if (ref->refc < 0) {
  624. WARN(1, "core-imc: Invalid event reference count\n");
  625. ref->refc = 0;
  626. }
  627. mutex_unlock(&ref->lock);
  628. }
  629. static int core_imc_event_init(struct perf_event *event)
  630. {
  631. int core_id, rc;
  632. u64 config = event->attr.config;
  633. struct imc_mem_info *pcmi;
  634. struct imc_pmu *pmu;
  635. struct imc_pmu_ref *ref;
  636. if (event->attr.type != event->pmu->type)
  637. return -ENOENT;
  638. /* Sampling not supported */
  639. if (event->hw.sample_period)
  640. return -EINVAL;
  641. /* unsupported modes and filters */
  642. if (event->attr.exclude_user ||
  643. event->attr.exclude_kernel ||
  644. event->attr.exclude_hv ||
  645. event->attr.exclude_idle ||
  646. event->attr.exclude_host ||
  647. event->attr.exclude_guest)
  648. return -EINVAL;
  649. if (event->cpu < 0)
  650. return -EINVAL;
  651. event->hw.idx = -1;
  652. pmu = imc_event_to_pmu(event);
  653. /* Sanity check for config (event offset) */
  654. if (((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size))
  655. return -EINVAL;
  656. if (!is_core_imc_mem_inited(event->cpu))
  657. return -ENODEV;
  658. core_id = event->cpu / threads_per_core;
  659. pcmi = &core_imc_pmu->mem_info[core_id];
  660. if ((!pcmi->vbase))
  661. return -ENODEV;
  662. /* Get the core_imc mutex for this core */
  663. ref = &core_imc_refc[core_id];
  664. if (!ref)
  665. return -EINVAL;
  666. /*
  667. * Core pmu units are enabled only when it is used.
  668. * See if this is triggered for the first time.
  669. * If yes, take the mutex lock and enable the core counters.
  670. * If not, just increment the count in core_imc_refc struct.
  671. */
  672. mutex_lock(&ref->lock);
  673. if (ref->refc == 0) {
  674. rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
  675. get_hard_smp_processor_id(event->cpu));
  676. if (rc) {
  677. mutex_unlock(&ref->lock);
  678. pr_err("core-imc: Unable to start the counters for core %d\n",
  679. core_id);
  680. return rc;
  681. }
  682. }
  683. ++ref->refc;
  684. mutex_unlock(&ref->lock);
  685. event->hw.event_base = (u64)pcmi->vbase + (config & IMC_EVENT_OFFSET_MASK);
  686. event->destroy = core_imc_counters_release;
  687. return 0;
  688. }
  689. /*
  690. * Allocates a page of memory for each of the online cpus, and write the
  691. * physical base address of that page to the LDBAR for that cpu.
  692. *
  693. * LDBAR Register Layout:
  694. *
  695. * 0 4 8 12 16 20 24 28
  696. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  697. * | | [ ] [ Counter Address [8:50]
  698. * | * Mode |
  699. * | * PB Scope
  700. * * Enable/Disable
  701. *
  702. * 32 36 40 44 48 52 56 60
  703. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  704. * Counter Address [8:50] ]
  705. *
  706. */
  707. static int thread_imc_mem_alloc(int cpu_id, int size)
  708. {
  709. u64 ldbar_value, *local_mem = per_cpu(thread_imc_mem, cpu_id);
  710. int nid = cpu_to_node(cpu_id);
  711. if (!local_mem) {
  712. /*
  713. * This case could happen only once at start, since we dont
  714. * free the memory in cpu offline path.
  715. */
  716. local_mem = page_address(alloc_pages_node(nid,
  717. GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
  718. __GFP_NOWARN, get_order(size)));
  719. if (!local_mem)
  720. return -ENOMEM;
  721. per_cpu(thread_imc_mem, cpu_id) = local_mem;
  722. }
  723. ldbar_value = ((u64)local_mem & THREAD_IMC_LDBAR_MASK) | THREAD_IMC_ENABLE;
  724. mtspr(SPRN_LDBAR, ldbar_value);
  725. return 0;
  726. }
  727. static int ppc_thread_imc_cpu_online(unsigned int cpu)
  728. {
  729. return thread_imc_mem_alloc(cpu, thread_imc_mem_size);
  730. }
  731. static int ppc_thread_imc_cpu_offline(unsigned int cpu)
  732. {
  733. mtspr(SPRN_LDBAR, 0);
  734. return 0;
  735. }
  736. static int thread_imc_cpu_init(void)
  737. {
  738. return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE,
  739. "perf/powerpc/imc_thread:online",
  740. ppc_thread_imc_cpu_online,
  741. ppc_thread_imc_cpu_offline);
  742. }
  743. static int thread_imc_event_init(struct perf_event *event)
  744. {
  745. u32 config = event->attr.config;
  746. struct task_struct *target;
  747. struct imc_pmu *pmu;
  748. if (event->attr.type != event->pmu->type)
  749. return -ENOENT;
  750. /* Sampling not supported */
  751. if (event->hw.sample_period)
  752. return -EINVAL;
  753. event->hw.idx = -1;
  754. pmu = imc_event_to_pmu(event);
  755. /* Sanity check for config offset */
  756. if (((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size))
  757. return -EINVAL;
  758. target = event->hw.target;
  759. if (!target)
  760. return -EINVAL;
  761. event->pmu->task_ctx_nr = perf_sw_context;
  762. return 0;
  763. }
  764. static bool is_thread_imc_pmu(struct perf_event *event)
  765. {
  766. if (!strncmp(event->pmu->name, "thread_imc", strlen("thread_imc")))
  767. return true;
  768. return false;
  769. }
  770. static u64 * get_event_base_addr(struct perf_event *event)
  771. {
  772. u64 addr;
  773. if (is_thread_imc_pmu(event)) {
  774. addr = (u64)per_cpu(thread_imc_mem, smp_processor_id());
  775. return (u64 *)(addr + (event->attr.config & IMC_EVENT_OFFSET_MASK));
  776. }
  777. return (u64 *)event->hw.event_base;
  778. }
  779. static void thread_imc_pmu_start_txn(struct pmu *pmu,
  780. unsigned int txn_flags)
  781. {
  782. if (txn_flags & ~PERF_PMU_TXN_ADD)
  783. return;
  784. perf_pmu_disable(pmu);
  785. }
  786. static void thread_imc_pmu_cancel_txn(struct pmu *pmu)
  787. {
  788. perf_pmu_enable(pmu);
  789. }
  790. static int thread_imc_pmu_commit_txn(struct pmu *pmu)
  791. {
  792. perf_pmu_enable(pmu);
  793. return 0;
  794. }
  795. static u64 imc_read_counter(struct perf_event *event)
  796. {
  797. u64 *addr, data;
  798. /*
  799. * In-Memory Collection (IMC) counters are free flowing counters.
  800. * So we take a snapshot of the counter value on enable and save it
  801. * to calculate the delta at later stage to present the event counter
  802. * value.
  803. */
  804. addr = get_event_base_addr(event);
  805. data = be64_to_cpu(READ_ONCE(*addr));
  806. local64_set(&event->hw.prev_count, data);
  807. return data;
  808. }
  809. static void imc_event_update(struct perf_event *event)
  810. {
  811. u64 counter_prev, counter_new, final_count;
  812. counter_prev = local64_read(&event->hw.prev_count);
  813. counter_new = imc_read_counter(event);
  814. final_count = counter_new - counter_prev;
  815. /* Update the delta to the event count */
  816. local64_add(final_count, &event->count);
  817. }
  818. static void imc_event_start(struct perf_event *event, int flags)
  819. {
  820. /*
  821. * In Memory Counters are free flowing counters. HW or the microcode
  822. * keeps adding to the counter offset in memory. To get event
  823. * counter value, we snapshot the value here and we calculate
  824. * delta at later point.
  825. */
  826. imc_read_counter(event);
  827. }
  828. static void imc_event_stop(struct perf_event *event, int flags)
  829. {
  830. /*
  831. * Take a snapshot and calculate the delta and update
  832. * the event counter values.
  833. */
  834. imc_event_update(event);
  835. }
  836. static int imc_event_add(struct perf_event *event, int flags)
  837. {
  838. if (flags & PERF_EF_START)
  839. imc_event_start(event, flags);
  840. return 0;
  841. }
  842. static int thread_imc_event_add(struct perf_event *event, int flags)
  843. {
  844. int core_id;
  845. struct imc_pmu_ref *ref;
  846. if (flags & PERF_EF_START)
  847. imc_event_start(event, flags);
  848. if (!is_core_imc_mem_inited(smp_processor_id()))
  849. return -EINVAL;
  850. core_id = smp_processor_id() / threads_per_core;
  851. /*
  852. * imc pmus are enabled only when it is used.
  853. * See if this is triggered for the first time.
  854. * If yes, take the mutex lock and enable the counters.
  855. * If not, just increment the count in ref count struct.
  856. */
  857. ref = &core_imc_refc[core_id];
  858. if (!ref)
  859. return -EINVAL;
  860. mutex_lock(&ref->lock);
  861. if (ref->refc == 0) {
  862. if (opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
  863. get_hard_smp_processor_id(smp_processor_id()))) {
  864. mutex_unlock(&ref->lock);
  865. pr_err("thread-imc: Unable to start the counter\
  866. for core %d\n", core_id);
  867. return -EINVAL;
  868. }
  869. }
  870. ++ref->refc;
  871. mutex_unlock(&ref->lock);
  872. return 0;
  873. }
  874. static void thread_imc_event_del(struct perf_event *event, int flags)
  875. {
  876. int core_id;
  877. struct imc_pmu_ref *ref;
  878. /*
  879. * Take a snapshot and calculate the delta and update
  880. * the event counter values.
  881. */
  882. imc_event_update(event);
  883. core_id = smp_processor_id() / threads_per_core;
  884. ref = &core_imc_refc[core_id];
  885. mutex_lock(&ref->lock);
  886. ref->refc--;
  887. if (ref->refc == 0) {
  888. if (opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
  889. get_hard_smp_processor_id(smp_processor_id()))) {
  890. mutex_unlock(&ref->lock);
  891. pr_err("thread-imc: Unable to stop the counters\
  892. for core %d\n", core_id);
  893. return;
  894. }
  895. } else if (ref->refc < 0) {
  896. ref->refc = 0;
  897. }
  898. mutex_unlock(&ref->lock);
  899. }
  900. /* update_pmu_ops : Populate the appropriate operations for "pmu" */
  901. static int update_pmu_ops(struct imc_pmu *pmu)
  902. {
  903. pmu->pmu.task_ctx_nr = perf_invalid_context;
  904. pmu->pmu.add = imc_event_add;
  905. pmu->pmu.del = imc_event_stop;
  906. pmu->pmu.start = imc_event_start;
  907. pmu->pmu.stop = imc_event_stop;
  908. pmu->pmu.read = imc_event_update;
  909. pmu->pmu.attr_groups = pmu->attr_groups;
  910. pmu->attr_groups[IMC_FORMAT_ATTR] = &imc_format_group;
  911. switch (pmu->domain) {
  912. case IMC_DOMAIN_NEST:
  913. pmu->pmu.event_init = nest_imc_event_init;
  914. pmu->attr_groups[IMC_CPUMASK_ATTR] = &imc_pmu_cpumask_attr_group;
  915. break;
  916. case IMC_DOMAIN_CORE:
  917. pmu->pmu.event_init = core_imc_event_init;
  918. pmu->attr_groups[IMC_CPUMASK_ATTR] = &imc_pmu_cpumask_attr_group;
  919. break;
  920. case IMC_DOMAIN_THREAD:
  921. pmu->pmu.event_init = thread_imc_event_init;
  922. pmu->pmu.add = thread_imc_event_add;
  923. pmu->pmu.del = thread_imc_event_del;
  924. pmu->pmu.start_txn = thread_imc_pmu_start_txn;
  925. pmu->pmu.cancel_txn = thread_imc_pmu_cancel_txn;
  926. pmu->pmu.commit_txn = thread_imc_pmu_commit_txn;
  927. break;
  928. default:
  929. break;
  930. }
  931. return 0;
  932. }
  933. /* init_nest_pmu_ref: Initialize the imc_pmu_ref struct for all the nodes */
  934. static int init_nest_pmu_ref(void)
  935. {
  936. int nid, i, cpu;
  937. nest_imc_refc = kcalloc(num_possible_nodes(), sizeof(*nest_imc_refc),
  938. GFP_KERNEL);
  939. if (!nest_imc_refc)
  940. return -ENOMEM;
  941. i = 0;
  942. for_each_node(nid) {
  943. /*
  944. * Mutex lock to avoid races while tracking the number of
  945. * sessions using the chip's nest pmu units.
  946. */
  947. mutex_init(&nest_imc_refc[i].lock);
  948. /*
  949. * Loop to init the "id" with the node_id. Variable "i" initialized to
  950. * 0 and will be used as index to the array. "i" will not go off the
  951. * end of the array since the "for_each_node" loops for "N_POSSIBLE"
  952. * nodes only.
  953. */
  954. nest_imc_refc[i++].id = nid;
  955. }
  956. /*
  957. * Loop to init the per_cpu "local_nest_imc_refc" with the proper
  958. * "nest_imc_refc" index. This makes get_nest_pmu_ref() alot simple.
  959. */
  960. for_each_possible_cpu(cpu) {
  961. nid = cpu_to_node(cpu);
  962. for (i = 0; i < num_possible_nodes(); i++) {
  963. if (nest_imc_refc[i].id == nid) {
  964. per_cpu(local_nest_imc_refc, cpu) = &nest_imc_refc[i];
  965. break;
  966. }
  967. }
  968. }
  969. return 0;
  970. }
  971. static void cleanup_all_core_imc_memory(void)
  972. {
  973. int i, nr_cores = DIV_ROUND_UP(num_possible_cpus(), threads_per_core);
  974. struct imc_mem_info *ptr = core_imc_pmu->mem_info;
  975. int size = core_imc_pmu->counter_mem_size;
  976. /* mem_info will never be NULL */
  977. for (i = 0; i < nr_cores; i++) {
  978. if (ptr[i].vbase)
  979. free_pages((u64)ptr[i].vbase, get_order(size));
  980. }
  981. kfree(ptr);
  982. kfree(core_imc_refc);
  983. }
  984. static void thread_imc_ldbar_disable(void *dummy)
  985. {
  986. /*
  987. * By Zeroing LDBAR, we disable thread-imc
  988. * updates.
  989. */
  990. mtspr(SPRN_LDBAR, 0);
  991. }
  992. void thread_imc_disable(void)
  993. {
  994. on_each_cpu(thread_imc_ldbar_disable, NULL, 1);
  995. }
  996. static void cleanup_all_thread_imc_memory(void)
  997. {
  998. int i, order = get_order(thread_imc_mem_size);
  999. for_each_online_cpu(i) {
  1000. if (per_cpu(thread_imc_mem, i))
  1001. free_pages((u64)per_cpu(thread_imc_mem, i), order);
  1002. }
  1003. }
  1004. /* Function to free the attr_groups which are dynamically allocated */
  1005. static void imc_common_mem_free(struct imc_pmu *pmu_ptr)
  1006. {
  1007. if (pmu_ptr->attr_groups[IMC_EVENT_ATTR])
  1008. kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]->attrs);
  1009. kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]);
  1010. }
  1011. /*
  1012. * Common function to unregister cpu hotplug callback and
  1013. * free the memory.
  1014. * TODO: Need to handle pmu unregistering, which will be
  1015. * done in followup series.
  1016. */
  1017. static void imc_common_cpuhp_mem_free(struct imc_pmu *pmu_ptr)
  1018. {
  1019. if (pmu_ptr->domain == IMC_DOMAIN_NEST) {
  1020. mutex_lock(&nest_init_lock);
  1021. if (nest_pmus == 1) {
  1022. cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE);
  1023. kfree(nest_imc_refc);
  1024. kfree(per_nest_pmu_arr);
  1025. per_nest_pmu_arr = NULL;
  1026. }
  1027. if (nest_pmus > 0)
  1028. nest_pmus--;
  1029. mutex_unlock(&nest_init_lock);
  1030. }
  1031. /* Free core_imc memory */
  1032. if (pmu_ptr->domain == IMC_DOMAIN_CORE) {
  1033. cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE);
  1034. cleanup_all_core_imc_memory();
  1035. }
  1036. /* Free thread_imc memory */
  1037. if (pmu_ptr->domain == IMC_DOMAIN_THREAD) {
  1038. cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE);
  1039. cleanup_all_thread_imc_memory();
  1040. }
  1041. }
  1042. /*
  1043. * Function to unregister thread-imc if core-imc
  1044. * is not registered.
  1045. */
  1046. void unregister_thread_imc(void)
  1047. {
  1048. imc_common_cpuhp_mem_free(thread_imc_pmu);
  1049. imc_common_mem_free(thread_imc_pmu);
  1050. perf_pmu_unregister(&thread_imc_pmu->pmu);
  1051. }
  1052. /*
  1053. * imc_mem_init : Function to support memory allocation for core imc.
  1054. */
  1055. static int imc_mem_init(struct imc_pmu *pmu_ptr, struct device_node *parent,
  1056. int pmu_index)
  1057. {
  1058. const char *s;
  1059. int nr_cores, cpu, res = -ENOMEM;
  1060. if (of_property_read_string(parent, "name", &s))
  1061. return -ENODEV;
  1062. switch (pmu_ptr->domain) {
  1063. case IMC_DOMAIN_NEST:
  1064. /* Update the pmu name */
  1065. pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s_imc", "nest_", s);
  1066. if (!pmu_ptr->pmu.name)
  1067. goto err;
  1068. /* Needed for hotplug/migration */
  1069. if (!per_nest_pmu_arr) {
  1070. per_nest_pmu_arr = kcalloc(get_max_nest_dev() + 1,
  1071. sizeof(struct imc_pmu *),
  1072. GFP_KERNEL);
  1073. if (!per_nest_pmu_arr)
  1074. goto err;
  1075. }
  1076. per_nest_pmu_arr[pmu_index] = pmu_ptr;
  1077. break;
  1078. case IMC_DOMAIN_CORE:
  1079. /* Update the pmu name */
  1080. pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
  1081. if (!pmu_ptr->pmu.name)
  1082. goto err;
  1083. nr_cores = DIV_ROUND_UP(num_possible_cpus(), threads_per_core);
  1084. pmu_ptr->mem_info = kcalloc(nr_cores, sizeof(struct imc_mem_info),
  1085. GFP_KERNEL);
  1086. if (!pmu_ptr->mem_info)
  1087. goto err;
  1088. core_imc_refc = kcalloc(nr_cores, sizeof(struct imc_pmu_ref),
  1089. GFP_KERNEL);
  1090. if (!core_imc_refc) {
  1091. kfree(pmu_ptr->mem_info);
  1092. goto err;
  1093. }
  1094. core_imc_pmu = pmu_ptr;
  1095. break;
  1096. case IMC_DOMAIN_THREAD:
  1097. /* Update the pmu name */
  1098. pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
  1099. if (!pmu_ptr->pmu.name)
  1100. goto err;
  1101. thread_imc_mem_size = pmu_ptr->counter_mem_size;
  1102. for_each_online_cpu(cpu) {
  1103. res = thread_imc_mem_alloc(cpu, pmu_ptr->counter_mem_size);
  1104. if (res) {
  1105. cleanup_all_thread_imc_memory();
  1106. goto err;
  1107. }
  1108. }
  1109. thread_imc_pmu = pmu_ptr;
  1110. break;
  1111. default:
  1112. return -EINVAL;
  1113. }
  1114. return 0;
  1115. err:
  1116. return res;
  1117. }
  1118. /*
  1119. * init_imc_pmu : Setup and register the IMC pmu device.
  1120. *
  1121. * @parent: Device tree unit node
  1122. * @pmu_ptr: memory allocated for this pmu
  1123. * @pmu_idx: Count of nest pmc registered
  1124. *
  1125. * init_imc_pmu() setup pmu cpumask and registers for a cpu hotplug callback.
  1126. * Handles failure cases and accordingly frees memory.
  1127. */
  1128. int init_imc_pmu(struct device_node *parent, struct imc_pmu *pmu_ptr, int pmu_idx)
  1129. {
  1130. int ret;
  1131. ret = imc_mem_init(pmu_ptr, parent, pmu_idx);
  1132. if (ret)
  1133. goto err_free_mem;
  1134. switch (pmu_ptr->domain) {
  1135. case IMC_DOMAIN_NEST:
  1136. /*
  1137. * Nest imc pmu need only one cpu per chip, we initialize the
  1138. * cpumask for the first nest imc pmu and use the same for the
  1139. * rest. To handle the cpuhotplug callback unregister, we track
  1140. * the number of nest pmus in "nest_pmus".
  1141. */
  1142. mutex_lock(&nest_init_lock);
  1143. if (nest_pmus == 0) {
  1144. ret = init_nest_pmu_ref();
  1145. if (ret) {
  1146. mutex_unlock(&nest_init_lock);
  1147. kfree(per_nest_pmu_arr);
  1148. per_nest_pmu_arr = NULL;
  1149. goto err_free_mem;
  1150. }
  1151. /* Register for cpu hotplug notification. */
  1152. ret = nest_pmu_cpumask_init();
  1153. if (ret) {
  1154. mutex_unlock(&nest_init_lock);
  1155. kfree(nest_imc_refc);
  1156. kfree(per_nest_pmu_arr);
  1157. per_nest_pmu_arr = NULL;
  1158. goto err_free_mem;
  1159. }
  1160. }
  1161. nest_pmus++;
  1162. mutex_unlock(&nest_init_lock);
  1163. break;
  1164. case IMC_DOMAIN_CORE:
  1165. ret = core_imc_pmu_cpumask_init();
  1166. if (ret) {
  1167. cleanup_all_core_imc_memory();
  1168. goto err_free_mem;
  1169. }
  1170. break;
  1171. case IMC_DOMAIN_THREAD:
  1172. ret = thread_imc_cpu_init();
  1173. if (ret) {
  1174. cleanup_all_thread_imc_memory();
  1175. goto err_free_mem;
  1176. }
  1177. break;
  1178. default:
  1179. return -EINVAL; /* Unknown domain */
  1180. }
  1181. ret = update_events_in_group(parent, pmu_ptr);
  1182. if (ret)
  1183. goto err_free_cpuhp_mem;
  1184. ret = update_pmu_ops(pmu_ptr);
  1185. if (ret)
  1186. goto err_free_cpuhp_mem;
  1187. ret = perf_pmu_register(&pmu_ptr->pmu, pmu_ptr->pmu.name, -1);
  1188. if (ret)
  1189. goto err_free_cpuhp_mem;
  1190. pr_info("%s performance monitor hardware support registered\n",
  1191. pmu_ptr->pmu.name);
  1192. return 0;
  1193. err_free_cpuhp_mem:
  1194. imc_common_cpuhp_mem_free(pmu_ptr);
  1195. err_free_mem:
  1196. imc_common_mem_free(pmu_ptr);
  1197. return ret;
  1198. }