imc-pmu.c 34 KB

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