hisi_uncore_pmu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * HiSilicon SoC Hardware event counters support
  3. *
  4. * Copyright (C) 2017 Hisilicon Limited
  5. * Author: Anurup M <anurup.m@huawei.com>
  6. * Shaokun Zhang <zhangshaokun@hisilicon.com>
  7. *
  8. * This code is based on the uncore PMUs like arm-cci and arm-ccn.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/bitmap.h>
  15. #include <linux/bitops.h>
  16. #include <linux/bug.h>
  17. #include <linux/err.h>
  18. #include <linux/errno.h>
  19. #include <linux/interrupt.h>
  20. #include <asm/local64.h>
  21. #include "hisi_uncore_pmu.h"
  22. #define HISI_GET_EVENTID(ev) (ev->hw.config_base & 0xff)
  23. #define HISI_MAX_PERIOD(nr) (BIT_ULL(nr) - 1)
  24. /*
  25. * PMU format attributes
  26. */
  27. ssize_t hisi_format_sysfs_show(struct device *dev,
  28. struct device_attribute *attr, char *buf)
  29. {
  30. struct dev_ext_attribute *eattr;
  31. eattr = container_of(attr, struct dev_ext_attribute, attr);
  32. return sprintf(buf, "%s\n", (char *)eattr->var);
  33. }
  34. /*
  35. * PMU event attributes
  36. */
  37. ssize_t hisi_event_sysfs_show(struct device *dev,
  38. struct device_attribute *attr, char *page)
  39. {
  40. struct dev_ext_attribute *eattr;
  41. eattr = container_of(attr, struct dev_ext_attribute, attr);
  42. return sprintf(page, "config=0x%lx\n", (unsigned long)eattr->var);
  43. }
  44. /*
  45. * sysfs cpumask attributes. For uncore PMU, we only have a single CPU to show
  46. */
  47. ssize_t hisi_cpumask_sysfs_show(struct device *dev,
  48. struct device_attribute *attr, char *buf)
  49. {
  50. struct hisi_pmu *hisi_pmu = to_hisi_pmu(dev_get_drvdata(dev));
  51. return sprintf(buf, "%d\n", hisi_pmu->on_cpu);
  52. }
  53. static bool hisi_validate_event_group(struct perf_event *event)
  54. {
  55. struct perf_event *sibling, *leader = event->group_leader;
  56. struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
  57. /* Include count for the event */
  58. int counters = 1;
  59. if (!is_software_event(leader)) {
  60. /*
  61. * We must NOT create groups containing mixed PMUs, although
  62. * software events are acceptable
  63. */
  64. if (leader->pmu != event->pmu)
  65. return false;
  66. /* Increment counter for the leader */
  67. if (leader != event)
  68. counters++;
  69. }
  70. for_each_sibling_event(sibling, event->group_leader) {
  71. if (is_software_event(sibling))
  72. continue;
  73. if (sibling->pmu != event->pmu)
  74. return false;
  75. /* Increment counter for each sibling */
  76. counters++;
  77. }
  78. /* The group can not count events more than the counters in the HW */
  79. return counters <= hisi_pmu->num_counters;
  80. }
  81. int hisi_uncore_pmu_counter_valid(struct hisi_pmu *hisi_pmu, int idx)
  82. {
  83. return idx >= 0 && idx < hisi_pmu->num_counters;
  84. }
  85. int hisi_uncore_pmu_get_event_idx(struct perf_event *event)
  86. {
  87. struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
  88. unsigned long *used_mask = hisi_pmu->pmu_events.used_mask;
  89. u32 num_counters = hisi_pmu->num_counters;
  90. int idx;
  91. idx = find_first_zero_bit(used_mask, num_counters);
  92. if (idx == num_counters)
  93. return -EAGAIN;
  94. set_bit(idx, used_mask);
  95. return idx;
  96. }
  97. static void hisi_uncore_pmu_clear_event_idx(struct hisi_pmu *hisi_pmu, int idx)
  98. {
  99. if (!hisi_uncore_pmu_counter_valid(hisi_pmu, idx)) {
  100. dev_err(hisi_pmu->dev, "Unsupported event index:%d!\n", idx);
  101. return;
  102. }
  103. clear_bit(idx, hisi_pmu->pmu_events.used_mask);
  104. }
  105. int hisi_uncore_pmu_event_init(struct perf_event *event)
  106. {
  107. struct hw_perf_event *hwc = &event->hw;
  108. struct hisi_pmu *hisi_pmu;
  109. if (event->attr.type != event->pmu->type)
  110. return -ENOENT;
  111. /*
  112. * We do not support sampling as the counters are all
  113. * shared by all CPU cores in a CPU die(SCCL). Also we
  114. * do not support attach to a task(per-process mode)
  115. */
  116. if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
  117. return -EOPNOTSUPP;
  118. /* counters do not have these bits */
  119. if (event->attr.exclude_user ||
  120. event->attr.exclude_kernel ||
  121. event->attr.exclude_host ||
  122. event->attr.exclude_guest ||
  123. event->attr.exclude_hv ||
  124. event->attr.exclude_idle)
  125. return -EINVAL;
  126. /*
  127. * The uncore counters not specific to any CPU, so cannot
  128. * support per-task
  129. */
  130. if (event->cpu < 0)
  131. return -EINVAL;
  132. /*
  133. * Validate if the events in group does not exceed the
  134. * available counters in hardware.
  135. */
  136. if (!hisi_validate_event_group(event))
  137. return -EINVAL;
  138. hisi_pmu = to_hisi_pmu(event->pmu);
  139. if (event->attr.config > hisi_pmu->check_event)
  140. return -EINVAL;
  141. if (hisi_pmu->on_cpu == -1)
  142. return -EINVAL;
  143. /*
  144. * We don't assign an index until we actually place the event onto
  145. * hardware. Use -1 to signify that we haven't decided where to put it
  146. * yet.
  147. */
  148. hwc->idx = -1;
  149. hwc->config_base = event->attr.config;
  150. /* Enforce to use the same CPU for all events in this PMU */
  151. event->cpu = hisi_pmu->on_cpu;
  152. return 0;
  153. }
  154. /*
  155. * Set the counter to count the event that we're interested in,
  156. * and enable interrupt and counter.
  157. */
  158. static void hisi_uncore_pmu_enable_event(struct perf_event *event)
  159. {
  160. struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
  161. struct hw_perf_event *hwc = &event->hw;
  162. hisi_pmu->ops->write_evtype(hisi_pmu, hwc->idx,
  163. HISI_GET_EVENTID(event));
  164. hisi_pmu->ops->enable_counter_int(hisi_pmu, hwc);
  165. hisi_pmu->ops->enable_counter(hisi_pmu, hwc);
  166. }
  167. /*
  168. * Disable counter and interrupt.
  169. */
  170. static void hisi_uncore_pmu_disable_event(struct perf_event *event)
  171. {
  172. struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
  173. struct hw_perf_event *hwc = &event->hw;
  174. hisi_pmu->ops->disable_counter(hisi_pmu, hwc);
  175. hisi_pmu->ops->disable_counter_int(hisi_pmu, hwc);
  176. }
  177. void hisi_uncore_pmu_set_event_period(struct perf_event *event)
  178. {
  179. struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
  180. struct hw_perf_event *hwc = &event->hw;
  181. /*
  182. * The HiSilicon PMU counters support 32 bits or 48 bits, depending on
  183. * the PMU. We reduce it to 2^(counter_bits - 1) to account for the
  184. * extreme interrupt latency. So we could hopefully handle the overflow
  185. * interrupt before another 2^(counter_bits - 1) events occur and the
  186. * counter overtakes its previous value.
  187. */
  188. u64 val = BIT_ULL(hisi_pmu->counter_bits - 1);
  189. local64_set(&hwc->prev_count, val);
  190. /* Write start value to the hardware event counter */
  191. hisi_pmu->ops->write_counter(hisi_pmu, hwc, val);
  192. }
  193. void hisi_uncore_pmu_event_update(struct perf_event *event)
  194. {
  195. struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
  196. struct hw_perf_event *hwc = &event->hw;
  197. u64 delta, prev_raw_count, new_raw_count;
  198. do {
  199. /* Read the count from the counter register */
  200. new_raw_count = hisi_pmu->ops->read_counter(hisi_pmu, hwc);
  201. prev_raw_count = local64_read(&hwc->prev_count);
  202. } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  203. new_raw_count) != prev_raw_count);
  204. /*
  205. * compute the delta
  206. */
  207. delta = (new_raw_count - prev_raw_count) &
  208. HISI_MAX_PERIOD(hisi_pmu->counter_bits);
  209. local64_add(delta, &event->count);
  210. }
  211. void hisi_uncore_pmu_start(struct perf_event *event, int flags)
  212. {
  213. struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
  214. struct hw_perf_event *hwc = &event->hw;
  215. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  216. return;
  217. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  218. hwc->state = 0;
  219. hisi_uncore_pmu_set_event_period(event);
  220. if (flags & PERF_EF_RELOAD) {
  221. u64 prev_raw_count = local64_read(&hwc->prev_count);
  222. hisi_pmu->ops->write_counter(hisi_pmu, hwc, prev_raw_count);
  223. }
  224. hisi_uncore_pmu_enable_event(event);
  225. perf_event_update_userpage(event);
  226. }
  227. void hisi_uncore_pmu_stop(struct perf_event *event, int flags)
  228. {
  229. struct hw_perf_event *hwc = &event->hw;
  230. hisi_uncore_pmu_disable_event(event);
  231. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  232. hwc->state |= PERF_HES_STOPPED;
  233. if (hwc->state & PERF_HES_UPTODATE)
  234. return;
  235. /* Read hardware counter and update the perf counter statistics */
  236. hisi_uncore_pmu_event_update(event);
  237. hwc->state |= PERF_HES_UPTODATE;
  238. }
  239. int hisi_uncore_pmu_add(struct perf_event *event, int flags)
  240. {
  241. struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
  242. struct hw_perf_event *hwc = &event->hw;
  243. int idx;
  244. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  245. /* Get an available counter index for counting */
  246. idx = hisi_pmu->ops->get_event_idx(event);
  247. if (idx < 0)
  248. return idx;
  249. event->hw.idx = idx;
  250. hisi_pmu->pmu_events.hw_events[idx] = event;
  251. if (flags & PERF_EF_START)
  252. hisi_uncore_pmu_start(event, PERF_EF_RELOAD);
  253. return 0;
  254. }
  255. void hisi_uncore_pmu_del(struct perf_event *event, int flags)
  256. {
  257. struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
  258. struct hw_perf_event *hwc = &event->hw;
  259. hisi_uncore_pmu_stop(event, PERF_EF_UPDATE);
  260. hisi_uncore_pmu_clear_event_idx(hisi_pmu, hwc->idx);
  261. perf_event_update_userpage(event);
  262. hisi_pmu->pmu_events.hw_events[hwc->idx] = NULL;
  263. }
  264. void hisi_uncore_pmu_read(struct perf_event *event)
  265. {
  266. /* Read hardware counter and update the perf counter statistics */
  267. hisi_uncore_pmu_event_update(event);
  268. }
  269. void hisi_uncore_pmu_enable(struct pmu *pmu)
  270. {
  271. struct hisi_pmu *hisi_pmu = to_hisi_pmu(pmu);
  272. int enabled = bitmap_weight(hisi_pmu->pmu_events.used_mask,
  273. hisi_pmu->num_counters);
  274. if (!enabled)
  275. return;
  276. hisi_pmu->ops->start_counters(hisi_pmu);
  277. }
  278. void hisi_uncore_pmu_disable(struct pmu *pmu)
  279. {
  280. struct hisi_pmu *hisi_pmu = to_hisi_pmu(pmu);
  281. hisi_pmu->ops->stop_counters(hisi_pmu);
  282. }
  283. /*
  284. * Read Super CPU cluster and CPU cluster ID from MPIDR_EL1.
  285. * If multi-threading is supported, CCL_ID is the low 3-bits in MPIDR[Aff2]
  286. * and SCCL_ID is the upper 5-bits of Aff2 field; if not, SCCL_ID
  287. * is in MPIDR[Aff2] and CCL_ID is in MPIDR[Aff1].
  288. */
  289. static void hisi_read_sccl_and_ccl_id(int *sccl_id, int *ccl_id)
  290. {
  291. u64 mpidr = read_cpuid_mpidr();
  292. if (mpidr & MPIDR_MT_BITMASK) {
  293. int aff2 = MPIDR_AFFINITY_LEVEL(mpidr, 2);
  294. if (sccl_id)
  295. *sccl_id = aff2 >> 3;
  296. if (ccl_id)
  297. *ccl_id = aff2 & 0x7;
  298. } else {
  299. if (sccl_id)
  300. *sccl_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
  301. if (ccl_id)
  302. *ccl_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  303. }
  304. }
  305. /*
  306. * Check whether the CPU is associated with this uncore PMU
  307. */
  308. static bool hisi_pmu_cpu_is_associated_pmu(struct hisi_pmu *hisi_pmu)
  309. {
  310. int sccl_id, ccl_id;
  311. if (hisi_pmu->ccl_id == -1) {
  312. /* If CCL_ID is -1, the PMU only shares the same SCCL */
  313. hisi_read_sccl_and_ccl_id(&sccl_id, NULL);
  314. return sccl_id == hisi_pmu->sccl_id;
  315. }
  316. hisi_read_sccl_and_ccl_id(&sccl_id, &ccl_id);
  317. return sccl_id == hisi_pmu->sccl_id && ccl_id == hisi_pmu->ccl_id;
  318. }
  319. int hisi_uncore_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
  320. {
  321. struct hisi_pmu *hisi_pmu = hlist_entry_safe(node, struct hisi_pmu,
  322. node);
  323. if (!hisi_pmu_cpu_is_associated_pmu(hisi_pmu))
  324. return 0;
  325. cpumask_set_cpu(cpu, &hisi_pmu->associated_cpus);
  326. /* If another CPU is already managing this PMU, simply return. */
  327. if (hisi_pmu->on_cpu != -1)
  328. return 0;
  329. /* Use this CPU in cpumask for event counting */
  330. hisi_pmu->on_cpu = cpu;
  331. /* Overflow interrupt also should use the same CPU */
  332. WARN_ON(irq_set_affinity(hisi_pmu->irq, cpumask_of(cpu)));
  333. return 0;
  334. }
  335. int hisi_uncore_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
  336. {
  337. struct hisi_pmu *hisi_pmu = hlist_entry_safe(node, struct hisi_pmu,
  338. node);
  339. cpumask_t pmu_online_cpus;
  340. unsigned int target;
  341. if (!cpumask_test_and_clear_cpu(cpu, &hisi_pmu->associated_cpus))
  342. return 0;
  343. /* Nothing to do if this CPU doesn't own the PMU */
  344. if (hisi_pmu->on_cpu != cpu)
  345. return 0;
  346. /* Give up ownership of the PMU */
  347. hisi_pmu->on_cpu = -1;
  348. /* Choose a new CPU to migrate ownership of the PMU to */
  349. cpumask_and(&pmu_online_cpus, &hisi_pmu->associated_cpus,
  350. cpu_online_mask);
  351. target = cpumask_any_but(&pmu_online_cpus, cpu);
  352. if (target >= nr_cpu_ids)
  353. return 0;
  354. perf_pmu_migrate_context(&hisi_pmu->pmu, cpu, target);
  355. /* Use this CPU for event counting */
  356. hisi_pmu->on_cpu = target;
  357. WARN_ON(irq_set_affinity(hisi_pmu->irq, cpumask_of(target)));
  358. return 0;
  359. }