imc-pmu.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * In-Memory Collection (IMC) Performance Monitor counter support.
  4. *
  5. * Copyright (C) 2017 Madhavan Srinivasan, IBM Corporation.
  6. * (C) 2017 Anju T Sudhakar, IBM Corporation.
  7. * (C) 2017 Hemant K Shaw, IBM Corporation.
  8. */
  9. #include <linux/of.h>
  10. #include <linux/perf_event.h>
  11. #include <linux/slab.h>
  12. #include <asm/opal.h>
  13. #include <asm/imc-pmu.h>
  14. #include <asm/cputhreads.h>
  15. #include <asm/smp.h>
  16. #include <linux/string.h>
  17. #include <linux/spinlock.h>
  18. /* Nest IMC data structures and variables */
  19. /*
  20. * Used to avoid races in counting the nest-pmu units during hotplug
  21. * register and unregister
  22. */
  23. static DEFINE_MUTEX(nest_init_lock);
  24. static DEFINE_PER_CPU(struct imc_pmu_ref *, local_nest_imc_refc);
  25. static struct imc_pmu **per_nest_pmu_arr;
  26. static cpumask_t nest_imc_cpumask;
  27. static struct imc_pmu_ref *nest_imc_refc;
  28. static int nest_pmus;
  29. /* Core IMC data structures and variables */
  30. static cpumask_t core_imc_cpumask;
  31. static struct imc_pmu_ref *core_imc_refc;
  32. static struct imc_pmu *core_imc_pmu;
  33. /* Thread IMC data structures and variables */
  34. static DEFINE_PER_CPU(u64 *, thread_imc_mem);
  35. static struct imc_pmu *thread_imc_pmu;
  36. static int thread_imc_mem_size;
  37. /* Trace IMC data structures */
  38. static DEFINE_PER_CPU(u64 *, trace_imc_mem);
  39. static struct imc_pmu_ref *trace_imc_refc;
  40. static int trace_imc_mem_size;
  41. /*
  42. * Global data structure used to avoid races between thread,
  43. * core and trace-imc
  44. */
  45. static struct imc_pmu_ref imc_global_refc = {
  46. .lock = __SPIN_LOCK_UNLOCKED(imc_global_refc.lock),
  47. .id = 0,
  48. .refc = 0,
  49. };
  50. static struct imc_pmu *imc_event_to_pmu(struct perf_event *event)
  51. {
  52. return container_of(event->pmu, struct imc_pmu, pmu);
  53. }
  54. PMU_FORMAT_ATTR(event, "config:0-61");
  55. PMU_FORMAT_ATTR(offset, "config:0-31");
  56. PMU_FORMAT_ATTR(rvalue, "config:32");
  57. PMU_FORMAT_ATTR(mode, "config:33-40");
  58. static struct attribute *imc_format_attrs[] = {
  59. &format_attr_event.attr,
  60. &format_attr_offset.attr,
  61. &format_attr_rvalue.attr,
  62. &format_attr_mode.attr,
  63. NULL,
  64. };
  65. static const struct attribute_group imc_format_group = {
  66. .name = "format",
  67. .attrs = imc_format_attrs,
  68. };
  69. /* Format attribute for imc trace-mode */
  70. PMU_FORMAT_ATTR(cpmc_reserved, "config:0-19");
  71. PMU_FORMAT_ATTR(cpmc_event, "config:20-27");
  72. PMU_FORMAT_ATTR(cpmc_samplesel, "config:28-29");
  73. PMU_FORMAT_ATTR(cpmc_load, "config:30-61");
  74. static struct attribute *trace_imc_format_attrs[] = {
  75. &format_attr_event.attr,
  76. &format_attr_cpmc_reserved.attr,
  77. &format_attr_cpmc_event.attr,
  78. &format_attr_cpmc_samplesel.attr,
  79. &format_attr_cpmc_load.attr,
  80. NULL,
  81. };
  82. static const struct attribute_group trace_imc_format_group = {
  83. .name = "format",
  84. .attrs = trace_imc_format_attrs,
  85. };
  86. /* Get the cpumask printed to a buffer "buf" */
  87. static ssize_t imc_pmu_cpumask_get_attr(struct device *dev,
  88. struct device_attribute *attr,
  89. char *buf)
  90. {
  91. struct pmu *pmu = dev_get_drvdata(dev);
  92. struct imc_pmu *imc_pmu = container_of(pmu, struct imc_pmu, pmu);
  93. cpumask_t *active_mask;
  94. switch(imc_pmu->domain){
  95. case IMC_DOMAIN_NEST:
  96. active_mask = &nest_imc_cpumask;
  97. break;
  98. case IMC_DOMAIN_CORE:
  99. active_mask = &core_imc_cpumask;
  100. break;
  101. default:
  102. return 0;
  103. }
  104. return cpumap_print_to_pagebuf(true, buf, active_mask);
  105. }
  106. static DEVICE_ATTR(cpumask, S_IRUGO, imc_pmu_cpumask_get_attr, NULL);
  107. static struct attribute *imc_pmu_cpumask_attrs[] = {
  108. &dev_attr_cpumask.attr,
  109. NULL,
  110. };
  111. static const struct attribute_group imc_pmu_cpumask_attr_group = {
  112. .attrs = imc_pmu_cpumask_attrs,
  113. };
  114. /* device_str_attr_create : Populate event "name" and string "str" in attribute */
  115. static struct attribute *device_str_attr_create(const char *name, const char *str)
  116. {
  117. struct perf_pmu_events_attr *attr;
  118. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  119. if (!attr)
  120. return NULL;
  121. sysfs_attr_init(&attr->attr.attr);
  122. attr->event_str = str;
  123. attr->attr.attr.name = name;
  124. attr->attr.attr.mode = 0444;
  125. attr->attr.show = perf_event_sysfs_show;
  126. return &attr->attr.attr;
  127. }
  128. static int imc_parse_event(struct device_node *np, const char *scale,
  129. const char *unit, const char *prefix,
  130. u32 base, struct imc_events *event)
  131. {
  132. const char *s;
  133. u32 reg;
  134. if (of_property_read_u32(np, "reg", &reg))
  135. goto error;
  136. /* Add the base_reg value to the "reg" */
  137. event->value = base + reg;
  138. if (of_property_read_string(np, "event-name", &s))
  139. goto error;
  140. event->name = kasprintf(GFP_KERNEL, "%s%s", prefix, s);
  141. if (!event->name)
  142. goto error;
  143. if (of_property_read_string(np, "scale", &s))
  144. s = scale;
  145. if (s) {
  146. event->scale = kstrdup(s, GFP_KERNEL);
  147. if (!event->scale)
  148. goto error;
  149. }
  150. if (of_property_read_string(np, "unit", &s))
  151. s = unit;
  152. if (s) {
  153. event->unit = kstrdup(s, GFP_KERNEL);
  154. if (!event->unit)
  155. goto error;
  156. }
  157. return 0;
  158. error:
  159. kfree(event->unit);
  160. kfree(event->scale);
  161. kfree(event->name);
  162. return -EINVAL;
  163. }
  164. /*
  165. * imc_free_events: Function to cleanup the events list, having
  166. * "nr_entries".
  167. */
  168. static void imc_free_events(struct imc_events *events, int nr_entries)
  169. {
  170. int i;
  171. /* Nothing to clean, return */
  172. if (!events)
  173. return;
  174. for (i = 0; i < nr_entries; i++) {
  175. kfree(events[i].unit);
  176. kfree(events[i].scale);
  177. kfree(events[i].name);
  178. }
  179. kfree(events);
  180. }
  181. /*
  182. * update_events_in_group: Update the "events" information in an attr_group
  183. * and assign the attr_group to the pmu "pmu".
  184. */
  185. static int update_events_in_group(struct device_node *node, struct imc_pmu *pmu)
  186. {
  187. struct attribute_group *attr_group;
  188. struct attribute **attrs, *dev_str;
  189. struct device_node *np, *pmu_events;
  190. u32 handle, base_reg;
  191. int i = 0, j = 0, ct, ret;
  192. const char *prefix, *g_scale, *g_unit;
  193. const char *ev_val_str, *ev_scale_str, *ev_unit_str;
  194. if (!of_property_read_u32(node, "events", &handle))
  195. pmu_events = of_find_node_by_phandle(handle);
  196. else
  197. return 0;
  198. /* Did not find any node with a given phandle */
  199. if (!pmu_events)
  200. return 0;
  201. /* Get a count of number of child nodes */
  202. ct = of_get_child_count(pmu_events);
  203. /* Get the event prefix */
  204. if (of_property_read_string(node, "events-prefix", &prefix)) {
  205. of_node_put(pmu_events);
  206. return 0;
  207. }
  208. /* Get a global unit and scale data if available */
  209. if (of_property_read_string(node, "scale", &g_scale))
  210. g_scale = NULL;
  211. if (of_property_read_string(node, "unit", &g_unit))
  212. g_unit = NULL;
  213. /* "reg" property gives out the base offset of the counters data */
  214. of_property_read_u32(node, "reg", &base_reg);
  215. /* Allocate memory for the events */
  216. pmu->events = kcalloc(ct, sizeof(struct imc_events), GFP_KERNEL);
  217. if (!pmu->events) {
  218. of_node_put(pmu_events);
  219. return -ENOMEM;
  220. }
  221. ct = 0;
  222. /* Parse the events and update the struct */
  223. for_each_child_of_node(pmu_events, np) {
  224. ret = imc_parse_event(np, g_scale, g_unit, prefix, base_reg, &pmu->events[ct]);
  225. if (!ret)
  226. ct++;
  227. }
  228. of_node_put(pmu_events);
  229. /* Allocate memory for attribute group */
  230. attr_group = kzalloc(sizeof(*attr_group), GFP_KERNEL);
  231. if (!attr_group) {
  232. imc_free_events(pmu->events, ct);
  233. return -ENOMEM;
  234. }
  235. /*
  236. * Allocate memory for attributes.
  237. * Since we have count of events for this pmu, we also allocate
  238. * memory for the scale and unit attribute for now.
  239. * "ct" has the total event structs added from the events-parent node.
  240. * So allocate three times the "ct" (this includes event, event_scale and
  241. * event_unit).
  242. */
  243. attrs = kcalloc(((ct * 3) + 1), sizeof(struct attribute *), GFP_KERNEL);
  244. if (!attrs) {
  245. kfree(attr_group);
  246. imc_free_events(pmu->events, ct);
  247. return -ENOMEM;
  248. }
  249. attr_group->name = "events";
  250. attr_group->attrs = attrs;
  251. do {
  252. ev_val_str = kasprintf(GFP_KERNEL, "event=0x%x", pmu->events[i].value);
  253. if (!ev_val_str)
  254. continue;
  255. dev_str = device_str_attr_create(pmu->events[i].name, ev_val_str);
  256. if (!dev_str)
  257. continue;
  258. attrs[j++] = dev_str;
  259. if (pmu->events[i].scale) {
  260. ev_scale_str = kasprintf(GFP_KERNEL, "%s.scale", pmu->events[i].name);
  261. if (!ev_scale_str)
  262. continue;
  263. dev_str = device_str_attr_create(ev_scale_str, pmu->events[i].scale);
  264. if (!dev_str)
  265. continue;
  266. attrs[j++] = dev_str;
  267. }
  268. if (pmu->events[i].unit) {
  269. ev_unit_str = kasprintf(GFP_KERNEL, "%s.unit", pmu->events[i].name);
  270. if (!ev_unit_str)
  271. continue;
  272. dev_str = device_str_attr_create(ev_unit_str, pmu->events[i].unit);
  273. if (!dev_str)
  274. continue;
  275. attrs[j++] = dev_str;
  276. }
  277. } while (++i < ct);
  278. /* Save the event attribute */
  279. pmu->attr_groups[IMC_EVENT_ATTR] = attr_group;
  280. return 0;
  281. }
  282. /* get_nest_pmu_ref: Return the imc_pmu_ref struct for the given node */
  283. static struct imc_pmu_ref *get_nest_pmu_ref(int cpu)
  284. {
  285. return per_cpu(local_nest_imc_refc, cpu);
  286. }
  287. static void nest_change_cpu_context(int old_cpu, int new_cpu)
  288. {
  289. struct imc_pmu **pn = per_nest_pmu_arr;
  290. if (old_cpu < 0 || new_cpu < 0)
  291. return;
  292. while (*pn) {
  293. perf_pmu_migrate_context(&(*pn)->pmu, old_cpu, new_cpu);
  294. pn++;
  295. }
  296. }
  297. static int ppc_nest_imc_cpu_offline(unsigned int cpu)
  298. {
  299. int nid, target = -1;
  300. const struct cpumask *l_cpumask;
  301. struct imc_pmu_ref *ref;
  302. /*
  303. * Check in the designated list for this cpu. Dont bother
  304. * if not one of them.
  305. */
  306. if (!cpumask_test_and_clear_cpu(cpu, &nest_imc_cpumask))
  307. return 0;
  308. /*
  309. * Check whether nest_imc is registered. We could end up here if the
  310. * cpuhotplug callback registration fails. i.e, callback invokes the
  311. * offline path for all successfully registered nodes. At this stage,
  312. * nest_imc pmu will not be registered and we should return here.
  313. *
  314. * We return with a zero since this is not an offline failure. And
  315. * cpuhp_setup_state() returns the actual failure reason to the caller,
  316. * which in turn will call the cleanup routine.
  317. */
  318. if (!nest_pmus)
  319. return 0;
  320. /*
  321. * Now that this cpu is one of the designated,
  322. * find a next cpu a) which is online and b) in same chip.
  323. */
  324. nid = cpu_to_node(cpu);
  325. l_cpumask = cpumask_of_node(nid);
  326. target = cpumask_last(l_cpumask);
  327. /*
  328. * If this(target) is the last cpu in the cpumask for this chip,
  329. * check for any possible online cpu in the chip.
  330. */
  331. if (unlikely(target == cpu))
  332. target = cpumask_any_but(l_cpumask, cpu);
  333. /*
  334. * Update the cpumask with the target cpu and
  335. * migrate the context if needed
  336. */
  337. if (target >= 0 && target < nr_cpu_ids) {
  338. cpumask_set_cpu(target, &nest_imc_cpumask);
  339. nest_change_cpu_context(cpu, target);
  340. } else {
  341. opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
  342. get_hard_smp_processor_id(cpu));
  343. /*
  344. * If this is the last cpu in this chip then, skip the reference
  345. * count lock and make the reference count on this chip zero.
  346. */
  347. ref = get_nest_pmu_ref(cpu);
  348. if (!ref)
  349. return -EINVAL;
  350. ref->refc = 0;
  351. }
  352. return 0;
  353. }
  354. static int ppc_nest_imc_cpu_online(unsigned int cpu)
  355. {
  356. const struct cpumask *l_cpumask;
  357. static struct cpumask tmp_mask;
  358. int res;
  359. /* Get the cpumask of this node */
  360. l_cpumask = cpumask_of_node(cpu_to_node(cpu));
  361. /*
  362. * If this is not the first online CPU on this node, then
  363. * just return.
  364. */
  365. if (cpumask_and(&tmp_mask, l_cpumask, &nest_imc_cpumask))
  366. return 0;
  367. /*
  368. * If this is the first online cpu on this node
  369. * disable the nest counters by making an OPAL call.
  370. */
  371. res = opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
  372. get_hard_smp_processor_id(cpu));
  373. if (res)
  374. return res;
  375. /* Make this CPU the designated target for counter collection */
  376. cpumask_set_cpu(cpu, &nest_imc_cpumask);
  377. return 0;
  378. }
  379. static int nest_pmu_cpumask_init(void)
  380. {
  381. return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE,
  382. "perf/powerpc/imc:online",
  383. ppc_nest_imc_cpu_online,
  384. ppc_nest_imc_cpu_offline);
  385. }
  386. static void nest_imc_counters_release(struct perf_event *event)
  387. {
  388. int rc, node_id;
  389. struct imc_pmu_ref *ref;
  390. if (event->cpu < 0)
  391. return;
  392. node_id = cpu_to_node(event->cpu);
  393. /*
  394. * See if we need to disable the nest PMU.
  395. * If no events are currently in use, then we have to take a
  396. * lock to ensure that we don't race with another task doing
  397. * enable or disable the nest counters.
  398. */
  399. ref = get_nest_pmu_ref(event->cpu);
  400. if (!ref)
  401. return;
  402. /* Take the lock for this node and then decrement the reference count */
  403. spin_lock(&ref->lock);
  404. if (ref->refc == 0) {
  405. /*
  406. * The scenario where this is true is, when perf session is
  407. * started, followed by offlining of all cpus in a given node.
  408. *
  409. * In the cpuhotplug offline path, ppc_nest_imc_cpu_offline()
  410. * function set the ref->count to zero, if the cpu which is
  411. * about to offline is the last cpu in a given node and make
  412. * an OPAL call to disable the engine in that node.
  413. *
  414. */
  415. spin_unlock(&ref->lock);
  416. return;
  417. }
  418. ref->refc--;
  419. if (ref->refc == 0) {
  420. rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
  421. get_hard_smp_processor_id(event->cpu));
  422. if (rc) {
  423. spin_unlock(&ref->lock);
  424. pr_err("nest-imc: Unable to stop the counters for core %d\n", node_id);
  425. return;
  426. }
  427. } else if (ref->refc < 0) {
  428. WARN(1, "nest-imc: Invalid event reference count\n");
  429. ref->refc = 0;
  430. }
  431. spin_unlock(&ref->lock);
  432. }
  433. static int nest_imc_event_init(struct perf_event *event)
  434. {
  435. int chip_id, rc, node_id;
  436. u32 l_config, config = event->attr.config;
  437. struct imc_mem_info *pcni;
  438. struct imc_pmu *pmu;
  439. struct imc_pmu_ref *ref;
  440. bool flag = false;
  441. if (event->attr.type != event->pmu->type)
  442. return -ENOENT;
  443. /* Sampling not supported */
  444. if (event->hw.sample_period)
  445. return -EINVAL;
  446. if (event->cpu < 0)
  447. return -EINVAL;
  448. pmu = imc_event_to_pmu(event);
  449. /* Sanity check for config (event offset) */
  450. if ((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size)
  451. return -EINVAL;
  452. /*
  453. * Nest HW counter memory resides in a per-chip reserve-memory (HOMER).
  454. * Get the base memory address for this cpu.
  455. */
  456. chip_id = cpu_to_chip_id(event->cpu);
  457. /* Return, if chip_id is not valid */
  458. if (chip_id < 0)
  459. return -ENODEV;
  460. pcni = pmu->mem_info;
  461. do {
  462. if (pcni->id == chip_id) {
  463. flag = true;
  464. break;
  465. }
  466. pcni++;
  467. } while (pcni->vbase);
  468. if (!flag)
  469. return -ENODEV;
  470. /*
  471. * Add the event offset to the base address.
  472. */
  473. l_config = config & IMC_EVENT_OFFSET_MASK;
  474. event->hw.event_base = (u64)pcni->vbase + l_config;
  475. node_id = cpu_to_node(event->cpu);
  476. /*
  477. * Get the imc_pmu_ref struct for this node.
  478. * Take the lock and then increment the count of nest pmu events inited.
  479. */
  480. ref = get_nest_pmu_ref(event->cpu);
  481. if (!ref)
  482. return -EINVAL;
  483. spin_lock(&ref->lock);
  484. if (ref->refc == 0) {
  485. rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_NEST,
  486. get_hard_smp_processor_id(event->cpu));
  487. if (rc) {
  488. spin_unlock(&ref->lock);
  489. pr_err("nest-imc: Unable to start the counters for node %d\n",
  490. node_id);
  491. return rc;
  492. }
  493. }
  494. ++ref->refc;
  495. spin_unlock(&ref->lock);
  496. event->destroy = nest_imc_counters_release;
  497. return 0;
  498. }
  499. /*
  500. * core_imc_mem_init : Initializes memory for the current core.
  501. *
  502. * Uses alloc_pages_node() and uses the returned address as an argument to
  503. * an opal call to configure the pdbar. The address sent as an argument is
  504. * converted to physical address before the opal call is made. This is the
  505. * base address at which the core imc counters are populated.
  506. */
  507. static int core_imc_mem_init(int cpu, int size)
  508. {
  509. int nid, rc = 0, core_id = (cpu / threads_per_core);
  510. struct imc_mem_info *mem_info;
  511. struct page *page;
  512. /*
  513. * alloc_pages_node() will allocate memory for core in the
  514. * local node only.
  515. */
  516. nid = cpu_to_node(cpu);
  517. mem_info = &core_imc_pmu->mem_info[core_id];
  518. mem_info->id = core_id;
  519. /* We need only vbase for core counters */
  520. page = alloc_pages_node(nid,
  521. GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
  522. __GFP_NOWARN, get_order(size));
  523. if (!page)
  524. return -ENOMEM;
  525. mem_info->vbase = page_address(page);
  526. core_imc_refc[core_id].id = core_id;
  527. spin_lock_init(&core_imc_refc[core_id].lock);
  528. rc = opal_imc_counters_init(OPAL_IMC_COUNTERS_CORE,
  529. __pa((void *)mem_info->vbase),
  530. get_hard_smp_processor_id(cpu));
  531. if (rc) {
  532. free_pages((u64)mem_info->vbase, get_order(size));
  533. mem_info->vbase = NULL;
  534. }
  535. return rc;
  536. }
  537. static bool is_core_imc_mem_inited(int cpu)
  538. {
  539. struct imc_mem_info *mem_info;
  540. int core_id = (cpu / threads_per_core);
  541. mem_info = &core_imc_pmu->mem_info[core_id];
  542. if (!mem_info->vbase)
  543. return false;
  544. return true;
  545. }
  546. static int ppc_core_imc_cpu_online(unsigned int cpu)
  547. {
  548. const struct cpumask *l_cpumask;
  549. static struct cpumask tmp_mask;
  550. int ret = 0;
  551. /* Get the cpumask for this core */
  552. l_cpumask = cpu_sibling_mask(cpu);
  553. /* If a cpu for this core is already set, then, don't do anything */
  554. if (cpumask_and(&tmp_mask, l_cpumask, &core_imc_cpumask))
  555. return 0;
  556. if (!is_core_imc_mem_inited(cpu)) {
  557. ret = core_imc_mem_init(cpu, core_imc_pmu->counter_mem_size);
  558. if (ret) {
  559. pr_info("core_imc memory allocation for cpu %d failed\n", cpu);
  560. return ret;
  561. }
  562. }
  563. /* set the cpu in the mask */
  564. cpumask_set_cpu(cpu, &core_imc_cpumask);
  565. return 0;
  566. }
  567. static int ppc_core_imc_cpu_offline(unsigned int cpu)
  568. {
  569. unsigned int core_id;
  570. int ncpu;
  571. struct imc_pmu_ref *ref;
  572. /*
  573. * clear this cpu out of the mask, if not present in the mask,
  574. * don't bother doing anything.
  575. */
  576. if (!cpumask_test_and_clear_cpu(cpu, &core_imc_cpumask))
  577. return 0;
  578. /*
  579. * Check whether core_imc is registered. We could end up here
  580. * if the cpuhotplug callback registration fails. i.e, callback
  581. * invokes the offline path for all successfully registered cpus.
  582. * At this stage, core_imc pmu will not be registered and we
  583. * should return here.
  584. *
  585. * We return with a zero since this is not an offline failure.
  586. * And cpuhp_setup_state() returns the actual failure reason
  587. * to the caller, which inturn will call the cleanup routine.
  588. */
  589. if (!core_imc_pmu->pmu.event_init)
  590. return 0;
  591. /* Find any online cpu in that core except the current "cpu" */
  592. ncpu = cpumask_last(cpu_sibling_mask(cpu));
  593. if (unlikely(ncpu == cpu))
  594. ncpu = cpumask_any_but(cpu_sibling_mask(cpu), cpu);
  595. if (ncpu >= 0 && ncpu < nr_cpu_ids) {
  596. cpumask_set_cpu(ncpu, &core_imc_cpumask);
  597. perf_pmu_migrate_context(&core_imc_pmu->pmu, cpu, ncpu);
  598. } else {
  599. /*
  600. * If this is the last cpu in this core then skip taking reference
  601. * count lock for this core and directly zero "refc" for this core.
  602. */
  603. opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
  604. get_hard_smp_processor_id(cpu));
  605. core_id = cpu / threads_per_core;
  606. ref = &core_imc_refc[core_id];
  607. if (!ref)
  608. return -EINVAL;
  609. ref->refc = 0;
  610. /*
  611. * Reduce the global reference count, if this is the
  612. * last cpu in this core and core-imc event running
  613. * in this cpu.
  614. */
  615. spin_lock(&imc_global_refc.lock);
  616. if (imc_global_refc.id == IMC_DOMAIN_CORE)
  617. imc_global_refc.refc--;
  618. spin_unlock(&imc_global_refc.lock);
  619. }
  620. return 0;
  621. }
  622. static int core_imc_pmu_cpumask_init(void)
  623. {
  624. return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE,
  625. "perf/powerpc/imc_core:online",
  626. ppc_core_imc_cpu_online,
  627. ppc_core_imc_cpu_offline);
  628. }
  629. static void reset_global_refc(struct perf_event *event)
  630. {
  631. spin_lock(&imc_global_refc.lock);
  632. imc_global_refc.refc--;
  633. /*
  634. * If no other thread is running any
  635. * event for this domain(thread/core/trace),
  636. * set the global id to zero.
  637. */
  638. if (imc_global_refc.refc <= 0) {
  639. imc_global_refc.refc = 0;
  640. imc_global_refc.id = 0;
  641. }
  642. spin_unlock(&imc_global_refc.lock);
  643. }
  644. static void core_imc_counters_release(struct perf_event *event)
  645. {
  646. int rc, core_id;
  647. struct imc_pmu_ref *ref;
  648. if (event->cpu < 0)
  649. return;
  650. /*
  651. * See if we need to disable the IMC PMU.
  652. * If no events are currently in use, then we have to take a
  653. * lock to ensure that we don't race with another task doing
  654. * enable or disable the core counters.
  655. */
  656. core_id = event->cpu / threads_per_core;
  657. /* Take the lock and decrement the refernce count for this core */
  658. ref = &core_imc_refc[core_id];
  659. if (!ref)
  660. return;
  661. spin_lock(&ref->lock);
  662. if (ref->refc == 0) {
  663. /*
  664. * The scenario where this is true is, when perf session is
  665. * started, followed by offlining of all cpus in a given core.
  666. *
  667. * In the cpuhotplug offline path, ppc_core_imc_cpu_offline()
  668. * function set the ref->count to zero, if the cpu which is
  669. * about to offline is the last cpu in a given core and make
  670. * an OPAL call to disable the engine in that core.
  671. *
  672. */
  673. spin_unlock(&ref->lock);
  674. return;
  675. }
  676. ref->refc--;
  677. if (ref->refc == 0) {
  678. rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
  679. get_hard_smp_processor_id(event->cpu));
  680. if (rc) {
  681. spin_unlock(&ref->lock);
  682. pr_err("IMC: Unable to stop the counters for core %d\n", core_id);
  683. return;
  684. }
  685. } else if (ref->refc < 0) {
  686. WARN(1, "core-imc: Invalid event reference count\n");
  687. ref->refc = 0;
  688. }
  689. spin_unlock(&ref->lock);
  690. reset_global_refc(event);
  691. }
  692. static int core_imc_event_init(struct perf_event *event)
  693. {
  694. int core_id, rc;
  695. u64 config = event->attr.config;
  696. struct imc_mem_info *pcmi;
  697. struct imc_pmu *pmu;
  698. struct imc_pmu_ref *ref;
  699. if (event->attr.type != event->pmu->type)
  700. return -ENOENT;
  701. /* Sampling not supported */
  702. if (event->hw.sample_period)
  703. return -EINVAL;
  704. if (event->cpu < 0)
  705. return -EINVAL;
  706. event->hw.idx = -1;
  707. pmu = imc_event_to_pmu(event);
  708. /* Sanity check for config (event offset) */
  709. if (((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size))
  710. return -EINVAL;
  711. if (!is_core_imc_mem_inited(event->cpu))
  712. return -ENODEV;
  713. core_id = event->cpu / threads_per_core;
  714. pcmi = &core_imc_pmu->mem_info[core_id];
  715. if ((!pcmi->vbase))
  716. return -ENODEV;
  717. ref = &core_imc_refc[core_id];
  718. if (!ref)
  719. return -EINVAL;
  720. /*
  721. * Core pmu units are enabled only when it is used.
  722. * See if this is triggered for the first time.
  723. * If yes, take the lock and enable the core counters.
  724. * If not, just increment the count in core_imc_refc struct.
  725. */
  726. spin_lock(&ref->lock);
  727. if (ref->refc == 0) {
  728. rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
  729. get_hard_smp_processor_id(event->cpu));
  730. if (rc) {
  731. spin_unlock(&ref->lock);
  732. pr_err("core-imc: Unable to start the counters for core %d\n",
  733. core_id);
  734. return rc;
  735. }
  736. }
  737. ++ref->refc;
  738. spin_unlock(&ref->lock);
  739. /*
  740. * Since the system can run either in accumulation or trace-mode
  741. * of IMC at a time, core-imc events are allowed only if no other
  742. * trace/thread imc events are enabled/monitored.
  743. *
  744. * Take the global lock, and check the refc.id
  745. * to know whether any other trace/thread imc
  746. * events are running.
  747. */
  748. spin_lock(&imc_global_refc.lock);
  749. if (imc_global_refc.id == 0 || imc_global_refc.id == IMC_DOMAIN_CORE) {
  750. /*
  751. * No other trace/thread imc events are running in
  752. * the system, so set the refc.id to core-imc.
  753. */
  754. imc_global_refc.id = IMC_DOMAIN_CORE;
  755. imc_global_refc.refc++;
  756. } else {
  757. spin_unlock(&imc_global_refc.lock);
  758. return -EBUSY;
  759. }
  760. spin_unlock(&imc_global_refc.lock);
  761. event->hw.event_base = (u64)pcmi->vbase + (config & IMC_EVENT_OFFSET_MASK);
  762. event->destroy = core_imc_counters_release;
  763. return 0;
  764. }
  765. /*
  766. * Allocates a page of memory for each of the online cpus, and load
  767. * LDBAR with 0.
  768. * The physical base address of the page allocated for a cpu will be
  769. * written to the LDBAR for that cpu, when the thread-imc event
  770. * is added.
  771. *
  772. * LDBAR Register Layout:
  773. *
  774. * 0 4 8 12 16 20 24 28
  775. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  776. * | | [ ] [ Counter Address [8:50]
  777. * | * Mode |
  778. * | * PB Scope
  779. * * Enable/Disable
  780. *
  781. * 32 36 40 44 48 52 56 60
  782. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  783. * Counter Address [8:50] ]
  784. *
  785. */
  786. static int thread_imc_mem_alloc(int cpu_id, int size)
  787. {
  788. u64 *local_mem = per_cpu(thread_imc_mem, cpu_id);
  789. int nid = cpu_to_node(cpu_id);
  790. if (!local_mem) {
  791. struct page *page;
  792. /*
  793. * This case could happen only once at start, since we dont
  794. * free the memory in cpu offline path.
  795. */
  796. page = alloc_pages_node(nid,
  797. GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
  798. __GFP_NOWARN, get_order(size));
  799. if (!page)
  800. return -ENOMEM;
  801. local_mem = page_address(page);
  802. per_cpu(thread_imc_mem, cpu_id) = local_mem;
  803. }
  804. mtspr(SPRN_LDBAR, 0);
  805. return 0;
  806. }
  807. static int ppc_thread_imc_cpu_online(unsigned int cpu)
  808. {
  809. return thread_imc_mem_alloc(cpu, thread_imc_mem_size);
  810. }
  811. static int ppc_thread_imc_cpu_offline(unsigned int cpu)
  812. {
  813. /*
  814. * Set the bit 0 of LDBAR to zero.
  815. *
  816. * If bit 0 of LDBAR is unset, it will stop posting
  817. * the counter data to memory.
  818. * For thread-imc, bit 0 of LDBAR will be set to 1 in the
  819. * event_add function. So reset this bit here, to stop the updates
  820. * to memory in the cpu_offline path.
  821. */
  822. mtspr(SPRN_LDBAR, (mfspr(SPRN_LDBAR) & (~(1UL << 63))));
  823. /* Reduce the refc if thread-imc event running on this cpu */
  824. spin_lock(&imc_global_refc.lock);
  825. if (imc_global_refc.id == IMC_DOMAIN_THREAD)
  826. imc_global_refc.refc--;
  827. spin_unlock(&imc_global_refc.lock);
  828. return 0;
  829. }
  830. static int thread_imc_cpu_init(void)
  831. {
  832. return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE,
  833. "perf/powerpc/imc_thread:online",
  834. ppc_thread_imc_cpu_online,
  835. ppc_thread_imc_cpu_offline);
  836. }
  837. static int thread_imc_event_init(struct perf_event *event)
  838. {
  839. u32 config = event->attr.config;
  840. struct task_struct *target;
  841. struct imc_pmu *pmu;
  842. if (event->attr.type != event->pmu->type)
  843. return -ENOENT;
  844. if (!perfmon_capable())
  845. return -EACCES;
  846. /* Sampling not supported */
  847. if (event->hw.sample_period)
  848. return -EINVAL;
  849. event->hw.idx = -1;
  850. pmu = imc_event_to_pmu(event);
  851. /* Sanity check for config offset */
  852. if (((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size))
  853. return -EINVAL;
  854. target = event->hw.target;
  855. if (!target)
  856. return -EINVAL;
  857. spin_lock(&imc_global_refc.lock);
  858. /*
  859. * Check if any other trace/core imc events are running in the
  860. * system, if not set the global id to thread-imc.
  861. */
  862. if (imc_global_refc.id == 0 || imc_global_refc.id == IMC_DOMAIN_THREAD) {
  863. imc_global_refc.id = IMC_DOMAIN_THREAD;
  864. imc_global_refc.refc++;
  865. } else {
  866. spin_unlock(&imc_global_refc.lock);
  867. return -EBUSY;
  868. }
  869. spin_unlock(&imc_global_refc.lock);
  870. event->pmu->task_ctx_nr = perf_sw_context;
  871. event->destroy = reset_global_refc;
  872. return 0;
  873. }
  874. static bool is_thread_imc_pmu(struct perf_event *event)
  875. {
  876. if (!strncmp(event->pmu->name, "thread_imc", strlen("thread_imc")))
  877. return true;
  878. return false;
  879. }
  880. static __be64 *get_event_base_addr(struct perf_event *event)
  881. {
  882. u64 addr;
  883. if (is_thread_imc_pmu(event)) {
  884. addr = (u64)per_cpu(thread_imc_mem, smp_processor_id());
  885. return (__be64 *)(addr + (event->attr.config & IMC_EVENT_OFFSET_MASK));
  886. }
  887. return (__be64 *)event->hw.event_base;
  888. }
  889. static void thread_imc_pmu_start_txn(struct pmu *pmu,
  890. unsigned int txn_flags)
  891. {
  892. if (txn_flags & ~PERF_PMU_TXN_ADD)
  893. return;
  894. perf_pmu_disable(pmu);
  895. }
  896. static void thread_imc_pmu_cancel_txn(struct pmu *pmu)
  897. {
  898. perf_pmu_enable(pmu);
  899. }
  900. static int thread_imc_pmu_commit_txn(struct pmu *pmu)
  901. {
  902. perf_pmu_enable(pmu);
  903. return 0;
  904. }
  905. static u64 imc_read_counter(struct perf_event *event)
  906. {
  907. __be64 *addr;
  908. u64 data;
  909. /*
  910. * In-Memory Collection (IMC) counters are free flowing counters.
  911. * So we take a snapshot of the counter value on enable and save it
  912. * to calculate the delta at later stage to present the event counter
  913. * value.
  914. */
  915. addr = get_event_base_addr(event);
  916. data = be64_to_cpu(READ_ONCE(*addr));
  917. local64_set(&event->hw.prev_count, data);
  918. return data;
  919. }
  920. static void imc_event_update(struct perf_event *event)
  921. {
  922. u64 counter_prev, counter_new, final_count;
  923. counter_prev = local64_read(&event->hw.prev_count);
  924. counter_new = imc_read_counter(event);
  925. final_count = counter_new - counter_prev;
  926. /* Update the delta to the event count */
  927. local64_add(final_count, &event->count);
  928. }
  929. static void imc_event_start(struct perf_event *event, int flags)
  930. {
  931. /*
  932. * In Memory Counters are free flowing counters. HW or the microcode
  933. * keeps adding to the counter offset in memory. To get event
  934. * counter value, we snapshot the value here and we calculate
  935. * delta at later point.
  936. */
  937. imc_read_counter(event);
  938. }
  939. static void imc_event_stop(struct perf_event *event, int flags)
  940. {
  941. /*
  942. * Take a snapshot and calculate the delta and update
  943. * the event counter values.
  944. */
  945. imc_event_update(event);
  946. }
  947. static int imc_event_add(struct perf_event *event, int flags)
  948. {
  949. if (flags & PERF_EF_START)
  950. imc_event_start(event, flags);
  951. return 0;
  952. }
  953. static int thread_imc_event_add(struct perf_event *event, int flags)
  954. {
  955. int core_id;
  956. struct imc_pmu_ref *ref;
  957. u64 ldbar_value, *local_mem = per_cpu(thread_imc_mem, smp_processor_id());
  958. if (flags & PERF_EF_START)
  959. imc_event_start(event, flags);
  960. if (!is_core_imc_mem_inited(smp_processor_id()))
  961. return -EINVAL;
  962. core_id = smp_processor_id() / threads_per_core;
  963. ldbar_value = ((u64)local_mem & THREAD_IMC_LDBAR_MASK) | THREAD_IMC_ENABLE;
  964. mtspr(SPRN_LDBAR, ldbar_value);
  965. /*
  966. * imc pmus are enabled only when it is used.
  967. * See if this is triggered for the first time.
  968. * If yes, take the lock and enable the counters.
  969. * If not, just increment the count in ref count struct.
  970. */
  971. ref = &core_imc_refc[core_id];
  972. if (!ref)
  973. return -EINVAL;
  974. spin_lock(&ref->lock);
  975. if (ref->refc == 0) {
  976. if (opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
  977. get_hard_smp_processor_id(smp_processor_id()))) {
  978. spin_unlock(&ref->lock);
  979. pr_err("thread-imc: Unable to start the counter\
  980. for core %d\n", core_id);
  981. return -EINVAL;
  982. }
  983. }
  984. ++ref->refc;
  985. spin_unlock(&ref->lock);
  986. return 0;
  987. }
  988. static void thread_imc_event_del(struct perf_event *event, int flags)
  989. {
  990. int core_id;
  991. struct imc_pmu_ref *ref;
  992. core_id = smp_processor_id() / threads_per_core;
  993. ref = &core_imc_refc[core_id];
  994. if (!ref) {
  995. pr_debug("imc: Failed to get event reference count\n");
  996. return;
  997. }
  998. spin_lock(&ref->lock);
  999. ref->refc--;
  1000. if (ref->refc == 0) {
  1001. if (opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
  1002. get_hard_smp_processor_id(smp_processor_id()))) {
  1003. spin_unlock(&ref->lock);
  1004. pr_err("thread-imc: Unable to stop the counters\
  1005. for core %d\n", core_id);
  1006. return;
  1007. }
  1008. } else if (ref->refc < 0) {
  1009. ref->refc = 0;
  1010. }
  1011. spin_unlock(&ref->lock);
  1012. /* Set bit 0 of LDBAR to zero, to stop posting updates to memory */
  1013. mtspr(SPRN_LDBAR, (mfspr(SPRN_LDBAR) & (~(1UL << 63))));
  1014. /*
  1015. * Take a snapshot and calculate the delta and update
  1016. * the event counter values.
  1017. */
  1018. imc_event_update(event);
  1019. }
  1020. /*
  1021. * Allocate a page of memory for each cpu, and load LDBAR with 0.
  1022. */
  1023. static int trace_imc_mem_alloc(int cpu_id, int size)
  1024. {
  1025. u64 *local_mem = per_cpu(trace_imc_mem, cpu_id);
  1026. int phys_id = cpu_to_node(cpu_id), rc = 0;
  1027. int core_id = (cpu_id / threads_per_core);
  1028. if (!local_mem) {
  1029. struct page *page;
  1030. page = alloc_pages_node(phys_id,
  1031. GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
  1032. __GFP_NOWARN, get_order(size));
  1033. if (!page)
  1034. return -ENOMEM;
  1035. local_mem = page_address(page);
  1036. per_cpu(trace_imc_mem, cpu_id) = local_mem;
  1037. /* Initialise the counters for trace mode */
  1038. rc = opal_imc_counters_init(OPAL_IMC_COUNTERS_TRACE, __pa((void *)local_mem),
  1039. get_hard_smp_processor_id(cpu_id));
  1040. if (rc) {
  1041. pr_info("IMC:opal init failed for trace imc\n");
  1042. return rc;
  1043. }
  1044. }
  1045. trace_imc_refc[core_id].id = core_id;
  1046. spin_lock_init(&trace_imc_refc[core_id].lock);
  1047. mtspr(SPRN_LDBAR, 0);
  1048. return 0;
  1049. }
  1050. static int ppc_trace_imc_cpu_online(unsigned int cpu)
  1051. {
  1052. return trace_imc_mem_alloc(cpu, trace_imc_mem_size);
  1053. }
  1054. static int ppc_trace_imc_cpu_offline(unsigned int cpu)
  1055. {
  1056. /*
  1057. * No need to set bit 0 of LDBAR to zero, as
  1058. * it is set to zero for imc trace-mode
  1059. *
  1060. * Reduce the refc if any trace-imc event running
  1061. * on this cpu.
  1062. */
  1063. spin_lock(&imc_global_refc.lock);
  1064. if (imc_global_refc.id == IMC_DOMAIN_TRACE)
  1065. imc_global_refc.refc--;
  1066. spin_unlock(&imc_global_refc.lock);
  1067. return 0;
  1068. }
  1069. static int trace_imc_cpu_init(void)
  1070. {
  1071. return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_TRACE_IMC_ONLINE,
  1072. "perf/powerpc/imc_trace:online",
  1073. ppc_trace_imc_cpu_online,
  1074. ppc_trace_imc_cpu_offline);
  1075. }
  1076. static u64 get_trace_imc_event_base_addr(void)
  1077. {
  1078. return (u64)per_cpu(trace_imc_mem, smp_processor_id());
  1079. }
  1080. /*
  1081. * Function to parse trace-imc data obtained
  1082. * and to prepare the perf sample.
  1083. */
  1084. static int trace_imc_prepare_sample(struct trace_imc_data *mem,
  1085. struct perf_sample_data *data,
  1086. u64 *prev_tb,
  1087. struct perf_event_header *header,
  1088. struct perf_event *event)
  1089. {
  1090. /* Sanity checks for a valid record */
  1091. if (be64_to_cpu(READ_ONCE(mem->tb1)) > *prev_tb)
  1092. *prev_tb = be64_to_cpu(READ_ONCE(mem->tb1));
  1093. else
  1094. return -EINVAL;
  1095. if ((be64_to_cpu(READ_ONCE(mem->tb1)) & IMC_TRACE_RECORD_TB1_MASK) !=
  1096. be64_to_cpu(READ_ONCE(mem->tb2)))
  1097. return -EINVAL;
  1098. /* Prepare perf sample */
  1099. data->ip = be64_to_cpu(READ_ONCE(mem->ip));
  1100. data->period = event->hw.last_period;
  1101. header->type = PERF_RECORD_SAMPLE;
  1102. header->size = sizeof(*header) + event->header_size;
  1103. header->misc = 0;
  1104. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  1105. switch (IMC_TRACE_RECORD_VAL_HVPR(be64_to_cpu(READ_ONCE(mem->val)))) {
  1106. case 0:/* when MSR HV and PR not set in the trace-record */
  1107. header->misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1108. break;
  1109. case 1: /* MSR HV is 0 and PR is 1 */
  1110. header->misc |= PERF_RECORD_MISC_GUEST_USER;
  1111. break;
  1112. case 2: /* MSR HV is 1 and PR is 0 */
  1113. header->misc |= PERF_RECORD_MISC_KERNEL;
  1114. break;
  1115. case 3: /* MSR HV is 1 and PR is 1 */
  1116. header->misc |= PERF_RECORD_MISC_USER;
  1117. break;
  1118. default:
  1119. pr_info("IMC: Unable to set the flag based on MSR bits\n");
  1120. break;
  1121. }
  1122. } else {
  1123. if (is_kernel_addr(data->ip))
  1124. header->misc |= PERF_RECORD_MISC_KERNEL;
  1125. else
  1126. header->misc |= PERF_RECORD_MISC_USER;
  1127. }
  1128. perf_event_header__init_id(header, data, event);
  1129. return 0;
  1130. }
  1131. static void dump_trace_imc_data(struct perf_event *event)
  1132. {
  1133. struct trace_imc_data *mem;
  1134. int i, ret;
  1135. u64 prev_tb = 0;
  1136. mem = (struct trace_imc_data *)get_trace_imc_event_base_addr();
  1137. for (i = 0; i < (trace_imc_mem_size / sizeof(struct trace_imc_data));
  1138. i++, mem++) {
  1139. struct perf_sample_data data;
  1140. struct perf_event_header header;
  1141. ret = trace_imc_prepare_sample(mem, &data, &prev_tb, &header, event);
  1142. if (ret) /* Exit, if not a valid record */
  1143. break;
  1144. else {
  1145. /* If this is a valid record, create the sample */
  1146. struct perf_output_handle handle;
  1147. if (perf_output_begin(&handle, &data, event, header.size))
  1148. return;
  1149. perf_output_sample(&handle, &header, &data, event);
  1150. perf_output_end(&handle);
  1151. }
  1152. }
  1153. }
  1154. static int trace_imc_event_add(struct perf_event *event, int flags)
  1155. {
  1156. int core_id = smp_processor_id() / threads_per_core;
  1157. struct imc_pmu_ref *ref = NULL;
  1158. u64 local_mem, ldbar_value;
  1159. /* Set trace-imc bit in ldbar and load ldbar with per-thread memory address */
  1160. local_mem = get_trace_imc_event_base_addr();
  1161. ldbar_value = ((u64)local_mem & THREAD_IMC_LDBAR_MASK) | TRACE_IMC_ENABLE;
  1162. /* trace-imc reference count */
  1163. if (trace_imc_refc)
  1164. ref = &trace_imc_refc[core_id];
  1165. if (!ref) {
  1166. pr_debug("imc: Failed to get the event reference count\n");
  1167. return -EINVAL;
  1168. }
  1169. mtspr(SPRN_LDBAR, ldbar_value);
  1170. spin_lock(&ref->lock);
  1171. if (ref->refc == 0) {
  1172. if (opal_imc_counters_start(OPAL_IMC_COUNTERS_TRACE,
  1173. get_hard_smp_processor_id(smp_processor_id()))) {
  1174. spin_unlock(&ref->lock);
  1175. pr_err("trace-imc: Unable to start the counters for core %d\n", core_id);
  1176. return -EINVAL;
  1177. }
  1178. }
  1179. ++ref->refc;
  1180. spin_unlock(&ref->lock);
  1181. return 0;
  1182. }
  1183. static void trace_imc_event_read(struct perf_event *event)
  1184. {
  1185. return;
  1186. }
  1187. static void trace_imc_event_stop(struct perf_event *event, int flags)
  1188. {
  1189. u64 local_mem = get_trace_imc_event_base_addr();
  1190. dump_trace_imc_data(event);
  1191. memset((void *)local_mem, 0, sizeof(u64));
  1192. }
  1193. static void trace_imc_event_start(struct perf_event *event, int flags)
  1194. {
  1195. return;
  1196. }
  1197. static void trace_imc_event_del(struct perf_event *event, int flags)
  1198. {
  1199. int core_id = smp_processor_id() / threads_per_core;
  1200. struct imc_pmu_ref *ref = NULL;
  1201. if (trace_imc_refc)
  1202. ref = &trace_imc_refc[core_id];
  1203. if (!ref) {
  1204. pr_debug("imc: Failed to get event reference count\n");
  1205. return;
  1206. }
  1207. spin_lock(&ref->lock);
  1208. ref->refc--;
  1209. if (ref->refc == 0) {
  1210. if (opal_imc_counters_stop(OPAL_IMC_COUNTERS_TRACE,
  1211. get_hard_smp_processor_id(smp_processor_id()))) {
  1212. spin_unlock(&ref->lock);
  1213. pr_err("trace-imc: Unable to stop the counters for core %d\n", core_id);
  1214. return;
  1215. }
  1216. } else if (ref->refc < 0) {
  1217. ref->refc = 0;
  1218. }
  1219. spin_unlock(&ref->lock);
  1220. trace_imc_event_stop(event, flags);
  1221. }
  1222. static int trace_imc_event_init(struct perf_event *event)
  1223. {
  1224. if (event->attr.type != event->pmu->type)
  1225. return -ENOENT;
  1226. if (!perfmon_capable())
  1227. return -EACCES;
  1228. /* Return if this is a couting event */
  1229. if (event->attr.sample_period == 0)
  1230. return -ENOENT;
  1231. /*
  1232. * Take the global lock, and make sure
  1233. * no other thread is running any core/thread imc
  1234. * events
  1235. */
  1236. spin_lock(&imc_global_refc.lock);
  1237. if (imc_global_refc.id == 0 || imc_global_refc.id == IMC_DOMAIN_TRACE) {
  1238. /*
  1239. * No core/thread imc events are running in the
  1240. * system, so set the refc.id to trace-imc.
  1241. */
  1242. imc_global_refc.id = IMC_DOMAIN_TRACE;
  1243. imc_global_refc.refc++;
  1244. } else {
  1245. spin_unlock(&imc_global_refc.lock);
  1246. return -EBUSY;
  1247. }
  1248. spin_unlock(&imc_global_refc.lock);
  1249. event->hw.idx = -1;
  1250. /*
  1251. * There can only be a single PMU for perf_hw_context events which is assigned to
  1252. * core PMU. Hence use "perf_sw_context" for trace_imc.
  1253. */
  1254. event->pmu->task_ctx_nr = perf_sw_context;
  1255. event->destroy = reset_global_refc;
  1256. return 0;
  1257. }
  1258. /* update_pmu_ops : Populate the appropriate operations for "pmu" */
  1259. static int update_pmu_ops(struct imc_pmu *pmu)
  1260. {
  1261. pmu->pmu.task_ctx_nr = perf_invalid_context;
  1262. pmu->pmu.add = imc_event_add;
  1263. pmu->pmu.del = imc_event_stop;
  1264. pmu->pmu.start = imc_event_start;
  1265. pmu->pmu.stop = imc_event_stop;
  1266. pmu->pmu.read = imc_event_update;
  1267. pmu->pmu.attr_groups = pmu->attr_groups;
  1268. pmu->pmu.capabilities = PERF_PMU_CAP_NO_EXCLUDE;
  1269. pmu->attr_groups[IMC_FORMAT_ATTR] = &imc_format_group;
  1270. switch (pmu->domain) {
  1271. case IMC_DOMAIN_NEST:
  1272. pmu->pmu.event_init = nest_imc_event_init;
  1273. pmu->attr_groups[IMC_CPUMASK_ATTR] = &imc_pmu_cpumask_attr_group;
  1274. break;
  1275. case IMC_DOMAIN_CORE:
  1276. pmu->pmu.event_init = core_imc_event_init;
  1277. pmu->attr_groups[IMC_CPUMASK_ATTR] = &imc_pmu_cpumask_attr_group;
  1278. break;
  1279. case IMC_DOMAIN_THREAD:
  1280. pmu->pmu.event_init = thread_imc_event_init;
  1281. pmu->pmu.add = thread_imc_event_add;
  1282. pmu->pmu.del = thread_imc_event_del;
  1283. pmu->pmu.start_txn = thread_imc_pmu_start_txn;
  1284. pmu->pmu.cancel_txn = thread_imc_pmu_cancel_txn;
  1285. pmu->pmu.commit_txn = thread_imc_pmu_commit_txn;
  1286. break;
  1287. case IMC_DOMAIN_TRACE:
  1288. pmu->pmu.event_init = trace_imc_event_init;
  1289. pmu->pmu.add = trace_imc_event_add;
  1290. pmu->pmu.del = trace_imc_event_del;
  1291. pmu->pmu.start = trace_imc_event_start;
  1292. pmu->pmu.stop = trace_imc_event_stop;
  1293. pmu->pmu.read = trace_imc_event_read;
  1294. pmu->attr_groups[IMC_FORMAT_ATTR] = &trace_imc_format_group;
  1295. break;
  1296. default:
  1297. break;
  1298. }
  1299. return 0;
  1300. }
  1301. /* init_nest_pmu_ref: Initialize the imc_pmu_ref struct for all the nodes */
  1302. static int init_nest_pmu_ref(void)
  1303. {
  1304. int nid, i, cpu;
  1305. nest_imc_refc = kcalloc(num_possible_nodes(), sizeof(*nest_imc_refc),
  1306. GFP_KERNEL);
  1307. if (!nest_imc_refc)
  1308. return -ENOMEM;
  1309. i = 0;
  1310. for_each_node(nid) {
  1311. /*
  1312. * Take the lock to avoid races while tracking the number of
  1313. * sessions using the chip's nest pmu units.
  1314. */
  1315. spin_lock_init(&nest_imc_refc[i].lock);
  1316. /*
  1317. * Loop to init the "id" with the node_id. Variable "i" initialized to
  1318. * 0 and will be used as index to the array. "i" will not go off the
  1319. * end of the array since the "for_each_node" loops for "N_POSSIBLE"
  1320. * nodes only.
  1321. */
  1322. nest_imc_refc[i++].id = nid;
  1323. }
  1324. /*
  1325. * Loop to init the per_cpu "local_nest_imc_refc" with the proper
  1326. * "nest_imc_refc" index. This makes get_nest_pmu_ref() alot simple.
  1327. */
  1328. for_each_possible_cpu(cpu) {
  1329. nid = cpu_to_node(cpu);
  1330. for (i = 0; i < num_possible_nodes(); i++) {
  1331. if (nest_imc_refc[i].id == nid) {
  1332. per_cpu(local_nest_imc_refc, cpu) = &nest_imc_refc[i];
  1333. break;
  1334. }
  1335. }
  1336. }
  1337. return 0;
  1338. }
  1339. static void cleanup_all_core_imc_memory(void)
  1340. {
  1341. int i, nr_cores = DIV_ROUND_UP(num_possible_cpus(), threads_per_core);
  1342. struct imc_mem_info *ptr = core_imc_pmu->mem_info;
  1343. int size = core_imc_pmu->counter_mem_size;
  1344. /* mem_info will never be NULL */
  1345. for (i = 0; i < nr_cores; i++) {
  1346. if (ptr[i].vbase)
  1347. free_pages((u64)ptr[i].vbase, get_order(size));
  1348. }
  1349. kfree(ptr);
  1350. kfree(core_imc_refc);
  1351. }
  1352. static void thread_imc_ldbar_disable(void *dummy)
  1353. {
  1354. /*
  1355. * By setting 0th bit of LDBAR to zero, we disable thread-imc
  1356. * updates to memory.
  1357. */
  1358. mtspr(SPRN_LDBAR, (mfspr(SPRN_LDBAR) & (~(1UL << 63))));
  1359. }
  1360. void thread_imc_disable(void)
  1361. {
  1362. on_each_cpu(thread_imc_ldbar_disable, NULL, 1);
  1363. }
  1364. static void cleanup_all_thread_imc_memory(void)
  1365. {
  1366. int i, order = get_order(thread_imc_mem_size);
  1367. for_each_online_cpu(i) {
  1368. if (per_cpu(thread_imc_mem, i))
  1369. free_pages((u64)per_cpu(thread_imc_mem, i), order);
  1370. }
  1371. }
  1372. static void cleanup_all_trace_imc_memory(void)
  1373. {
  1374. int i, order = get_order(trace_imc_mem_size);
  1375. for_each_online_cpu(i) {
  1376. if (per_cpu(trace_imc_mem, i))
  1377. free_pages((u64)per_cpu(trace_imc_mem, i), order);
  1378. }
  1379. kfree(trace_imc_refc);
  1380. }
  1381. /* Function to free the attr_groups which are dynamically allocated */
  1382. static void imc_common_mem_free(struct imc_pmu *pmu_ptr)
  1383. {
  1384. if (pmu_ptr->attr_groups[IMC_EVENT_ATTR])
  1385. kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]->attrs);
  1386. kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]);
  1387. }
  1388. /*
  1389. * Common function to unregister cpu hotplug callback and
  1390. * free the memory.
  1391. * TODO: Need to handle pmu unregistering, which will be
  1392. * done in followup series.
  1393. */
  1394. static void imc_common_cpuhp_mem_free(struct imc_pmu *pmu_ptr)
  1395. {
  1396. if (pmu_ptr->domain == IMC_DOMAIN_NEST) {
  1397. mutex_lock(&nest_init_lock);
  1398. if (nest_pmus == 1) {
  1399. cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE);
  1400. kfree(nest_imc_refc);
  1401. kfree(per_nest_pmu_arr);
  1402. per_nest_pmu_arr = NULL;
  1403. }
  1404. if (nest_pmus > 0)
  1405. nest_pmus--;
  1406. mutex_unlock(&nest_init_lock);
  1407. }
  1408. /* Free core_imc memory */
  1409. if (pmu_ptr->domain == IMC_DOMAIN_CORE) {
  1410. cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE);
  1411. cleanup_all_core_imc_memory();
  1412. }
  1413. /* Free thread_imc memory */
  1414. if (pmu_ptr->domain == IMC_DOMAIN_THREAD) {
  1415. cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE);
  1416. cleanup_all_thread_imc_memory();
  1417. }
  1418. if (pmu_ptr->domain == IMC_DOMAIN_TRACE) {
  1419. cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_TRACE_IMC_ONLINE);
  1420. cleanup_all_trace_imc_memory();
  1421. }
  1422. }
  1423. /*
  1424. * Function to unregister thread-imc if core-imc
  1425. * is not registered.
  1426. */
  1427. void unregister_thread_imc(void)
  1428. {
  1429. imc_common_cpuhp_mem_free(thread_imc_pmu);
  1430. imc_common_mem_free(thread_imc_pmu);
  1431. perf_pmu_unregister(&thread_imc_pmu->pmu);
  1432. }
  1433. /*
  1434. * imc_mem_init : Function to support memory allocation for core imc.
  1435. */
  1436. static int imc_mem_init(struct imc_pmu *pmu_ptr, struct device_node *parent,
  1437. int pmu_index)
  1438. {
  1439. const char *s;
  1440. int nr_cores, cpu, res = -ENOMEM;
  1441. if (of_property_read_string(parent, "name", &s))
  1442. return -ENODEV;
  1443. switch (pmu_ptr->domain) {
  1444. case IMC_DOMAIN_NEST:
  1445. /* Update the pmu name */
  1446. pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s_imc", "nest_", s);
  1447. if (!pmu_ptr->pmu.name)
  1448. goto err;
  1449. /* Needed for hotplug/migration */
  1450. if (!per_nest_pmu_arr) {
  1451. per_nest_pmu_arr = kcalloc(get_max_nest_dev() + 1,
  1452. sizeof(struct imc_pmu *),
  1453. GFP_KERNEL);
  1454. if (!per_nest_pmu_arr)
  1455. goto err;
  1456. }
  1457. per_nest_pmu_arr[pmu_index] = pmu_ptr;
  1458. break;
  1459. case IMC_DOMAIN_CORE:
  1460. /* Update the pmu name */
  1461. pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
  1462. if (!pmu_ptr->pmu.name)
  1463. goto err;
  1464. nr_cores = DIV_ROUND_UP(num_possible_cpus(), threads_per_core);
  1465. pmu_ptr->mem_info = kcalloc(nr_cores, sizeof(struct imc_mem_info),
  1466. GFP_KERNEL);
  1467. if (!pmu_ptr->mem_info)
  1468. goto err;
  1469. core_imc_refc = kcalloc(nr_cores, sizeof(struct imc_pmu_ref),
  1470. GFP_KERNEL);
  1471. if (!core_imc_refc) {
  1472. kfree(pmu_ptr->mem_info);
  1473. goto err;
  1474. }
  1475. core_imc_pmu = pmu_ptr;
  1476. break;
  1477. case IMC_DOMAIN_THREAD:
  1478. /* Update the pmu name */
  1479. pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
  1480. if (!pmu_ptr->pmu.name)
  1481. goto err;
  1482. thread_imc_mem_size = pmu_ptr->counter_mem_size;
  1483. for_each_online_cpu(cpu) {
  1484. res = thread_imc_mem_alloc(cpu, pmu_ptr->counter_mem_size);
  1485. if (res) {
  1486. cleanup_all_thread_imc_memory();
  1487. goto err;
  1488. }
  1489. }
  1490. thread_imc_pmu = pmu_ptr;
  1491. break;
  1492. case IMC_DOMAIN_TRACE:
  1493. /* Update the pmu name */
  1494. pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
  1495. if (!pmu_ptr->pmu.name)
  1496. return -ENOMEM;
  1497. nr_cores = DIV_ROUND_UP(num_possible_cpus(), threads_per_core);
  1498. trace_imc_refc = kcalloc(nr_cores, sizeof(struct imc_pmu_ref),
  1499. GFP_KERNEL);
  1500. if (!trace_imc_refc)
  1501. return -ENOMEM;
  1502. trace_imc_mem_size = pmu_ptr->counter_mem_size;
  1503. for_each_online_cpu(cpu) {
  1504. res = trace_imc_mem_alloc(cpu, trace_imc_mem_size);
  1505. if (res) {
  1506. cleanup_all_trace_imc_memory();
  1507. goto err;
  1508. }
  1509. }
  1510. break;
  1511. default:
  1512. return -EINVAL;
  1513. }
  1514. return 0;
  1515. err:
  1516. return res;
  1517. }
  1518. /*
  1519. * init_imc_pmu : Setup and register the IMC pmu device.
  1520. *
  1521. * @parent: Device tree unit node
  1522. * @pmu_ptr: memory allocated for this pmu
  1523. * @pmu_idx: Count of nest pmc registered
  1524. *
  1525. * init_imc_pmu() setup pmu cpumask and registers for a cpu hotplug callback.
  1526. * Handles failure cases and accordingly frees memory.
  1527. */
  1528. int init_imc_pmu(struct device_node *parent, struct imc_pmu *pmu_ptr, int pmu_idx)
  1529. {
  1530. int ret;
  1531. ret = imc_mem_init(pmu_ptr, parent, pmu_idx);
  1532. if (ret)
  1533. goto err_free_mem;
  1534. switch (pmu_ptr->domain) {
  1535. case IMC_DOMAIN_NEST:
  1536. /*
  1537. * Nest imc pmu need only one cpu per chip, we initialize the
  1538. * cpumask for the first nest imc pmu and use the same for the
  1539. * rest. To handle the cpuhotplug callback unregister, we track
  1540. * the number of nest pmus in "nest_pmus".
  1541. */
  1542. mutex_lock(&nest_init_lock);
  1543. if (nest_pmus == 0) {
  1544. ret = init_nest_pmu_ref();
  1545. if (ret) {
  1546. mutex_unlock(&nest_init_lock);
  1547. kfree(per_nest_pmu_arr);
  1548. per_nest_pmu_arr = NULL;
  1549. goto err_free_mem;
  1550. }
  1551. /* Register for cpu hotplug notification. */
  1552. ret = nest_pmu_cpumask_init();
  1553. if (ret) {
  1554. mutex_unlock(&nest_init_lock);
  1555. kfree(nest_imc_refc);
  1556. kfree(per_nest_pmu_arr);
  1557. per_nest_pmu_arr = NULL;
  1558. goto err_free_mem;
  1559. }
  1560. }
  1561. nest_pmus++;
  1562. mutex_unlock(&nest_init_lock);
  1563. break;
  1564. case IMC_DOMAIN_CORE:
  1565. ret = core_imc_pmu_cpumask_init();
  1566. if (ret) {
  1567. cleanup_all_core_imc_memory();
  1568. goto err_free_mem;
  1569. }
  1570. break;
  1571. case IMC_DOMAIN_THREAD:
  1572. ret = thread_imc_cpu_init();
  1573. if (ret) {
  1574. cleanup_all_thread_imc_memory();
  1575. goto err_free_mem;
  1576. }
  1577. break;
  1578. case IMC_DOMAIN_TRACE:
  1579. ret = trace_imc_cpu_init();
  1580. if (ret) {
  1581. cleanup_all_trace_imc_memory();
  1582. goto err_free_mem;
  1583. }
  1584. break;
  1585. default:
  1586. return -EINVAL; /* Unknown domain */
  1587. }
  1588. ret = update_events_in_group(parent, pmu_ptr);
  1589. if (ret)
  1590. goto err_free_cpuhp_mem;
  1591. ret = update_pmu_ops(pmu_ptr);
  1592. if (ret)
  1593. goto err_free_cpuhp_mem;
  1594. ret = perf_pmu_register(&pmu_ptr->pmu, pmu_ptr->pmu.name, -1);
  1595. if (ret)
  1596. goto err_free_cpuhp_mem;
  1597. pr_debug("%s performance monitor hardware support registered\n",
  1598. pmu_ptr->pmu.name);
  1599. return 0;
  1600. err_free_cpuhp_mem:
  1601. imc_common_cpuhp_mem_free(pmu_ptr);
  1602. err_free_mem:
  1603. imc_common_mem_free(pmu_ptr);
  1604. return ret;
  1605. }