perf_event.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. /*
  2. * PMU support
  3. *
  4. * Copyright (C) 2012 ARM Limited
  5. * Author: Will Deacon <will.deacon@arm.com>
  6. *
  7. * This code is based heavily on the ARMv7 perf event code.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #define pr_fmt(fmt) "hw perfevents: " fmt
  22. #include <linux/bitmap.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/kernel.h>
  26. #include <linux/export.h>
  27. #include <linux/perf_event.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/cputype.h>
  32. #include <asm/irq.h>
  33. #include <asm/irq_regs.h>
  34. #include <asm/pmu.h>
  35. #include <asm/stacktrace.h>
  36. /*
  37. * ARMv8 supports a maximum of 32 events.
  38. * The cycle counter is included in this total.
  39. */
  40. #define ARMPMU_MAX_HWEVENTS 32
  41. static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events);
  42. static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask);
  43. static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events);
  44. #define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu))
  45. /* Set at runtime when we know what CPU type we are. */
  46. static struct arm_pmu *cpu_pmu;
  47. int
  48. armpmu_get_max_events(void)
  49. {
  50. int max_events = 0;
  51. if (cpu_pmu != NULL)
  52. max_events = cpu_pmu->num_events;
  53. return max_events;
  54. }
  55. EXPORT_SYMBOL_GPL(armpmu_get_max_events);
  56. int perf_num_counters(void)
  57. {
  58. return armpmu_get_max_events();
  59. }
  60. EXPORT_SYMBOL_GPL(perf_num_counters);
  61. #define HW_OP_UNSUPPORTED 0xFFFF
  62. #define C(_x) \
  63. PERF_COUNT_HW_CACHE_##_x
  64. #define CACHE_OP_UNSUPPORTED 0xFFFF
  65. static int
  66. armpmu_map_cache_event(const unsigned (*cache_map)
  67. [PERF_COUNT_HW_CACHE_MAX]
  68. [PERF_COUNT_HW_CACHE_OP_MAX]
  69. [PERF_COUNT_HW_CACHE_RESULT_MAX],
  70. u64 config)
  71. {
  72. unsigned int cache_type, cache_op, cache_result, ret;
  73. cache_type = (config >> 0) & 0xff;
  74. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  75. return -EINVAL;
  76. cache_op = (config >> 8) & 0xff;
  77. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  78. return -EINVAL;
  79. cache_result = (config >> 16) & 0xff;
  80. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  81. return -EINVAL;
  82. ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
  83. if (ret == CACHE_OP_UNSUPPORTED)
  84. return -ENOENT;
  85. return ret;
  86. }
  87. static int
  88. armpmu_map_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
  89. {
  90. int mapping;
  91. if (config >= PERF_COUNT_HW_MAX)
  92. return -EINVAL;
  93. mapping = (*event_map)[config];
  94. return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
  95. }
  96. static int
  97. armpmu_map_raw_event(u32 raw_event_mask, u64 config)
  98. {
  99. return (int)(config & raw_event_mask);
  100. }
  101. static int map_cpu_event(struct perf_event *event,
  102. const unsigned (*event_map)[PERF_COUNT_HW_MAX],
  103. const unsigned (*cache_map)
  104. [PERF_COUNT_HW_CACHE_MAX]
  105. [PERF_COUNT_HW_CACHE_OP_MAX]
  106. [PERF_COUNT_HW_CACHE_RESULT_MAX],
  107. u32 raw_event_mask)
  108. {
  109. u64 config = event->attr.config;
  110. switch (event->attr.type) {
  111. case PERF_TYPE_HARDWARE:
  112. return armpmu_map_event(event_map, config);
  113. case PERF_TYPE_HW_CACHE:
  114. return armpmu_map_cache_event(cache_map, config);
  115. case PERF_TYPE_RAW:
  116. return armpmu_map_raw_event(raw_event_mask, config);
  117. }
  118. return -ENOENT;
  119. }
  120. int
  121. armpmu_event_set_period(struct perf_event *event,
  122. struct hw_perf_event *hwc,
  123. int idx)
  124. {
  125. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  126. s64 left = local64_read(&hwc->period_left);
  127. s64 period = hwc->sample_period;
  128. int ret = 0;
  129. if (unlikely(left <= -period)) {
  130. left = period;
  131. local64_set(&hwc->period_left, left);
  132. hwc->last_period = period;
  133. ret = 1;
  134. }
  135. if (unlikely(left <= 0)) {
  136. left += period;
  137. local64_set(&hwc->period_left, left);
  138. hwc->last_period = period;
  139. ret = 1;
  140. }
  141. if (left > (s64)armpmu->max_period)
  142. left = armpmu->max_period;
  143. local64_set(&hwc->prev_count, (u64)-left);
  144. armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
  145. perf_event_update_userpage(event);
  146. return ret;
  147. }
  148. u64
  149. armpmu_event_update(struct perf_event *event,
  150. struct hw_perf_event *hwc,
  151. int idx)
  152. {
  153. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  154. u64 delta, prev_raw_count, new_raw_count;
  155. again:
  156. prev_raw_count = local64_read(&hwc->prev_count);
  157. new_raw_count = armpmu->read_counter(idx);
  158. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  159. new_raw_count) != prev_raw_count)
  160. goto again;
  161. delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
  162. local64_add(delta, &event->count);
  163. local64_sub(delta, &hwc->period_left);
  164. return new_raw_count;
  165. }
  166. static void
  167. armpmu_read(struct perf_event *event)
  168. {
  169. struct hw_perf_event *hwc = &event->hw;
  170. /* Don't read disabled counters! */
  171. if (hwc->idx < 0)
  172. return;
  173. armpmu_event_update(event, hwc, hwc->idx);
  174. }
  175. static void
  176. armpmu_stop(struct perf_event *event, int flags)
  177. {
  178. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  179. struct hw_perf_event *hwc = &event->hw;
  180. /*
  181. * ARM pmu always has to update the counter, so ignore
  182. * PERF_EF_UPDATE, see comments in armpmu_start().
  183. */
  184. if (!(hwc->state & PERF_HES_STOPPED)) {
  185. armpmu->disable(hwc, hwc->idx);
  186. barrier(); /* why? */
  187. armpmu_event_update(event, hwc, hwc->idx);
  188. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  189. }
  190. }
  191. static void
  192. armpmu_start(struct perf_event *event, int flags)
  193. {
  194. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  195. struct hw_perf_event *hwc = &event->hw;
  196. /*
  197. * ARM pmu always has to reprogram the period, so ignore
  198. * PERF_EF_RELOAD, see the comment below.
  199. */
  200. if (flags & PERF_EF_RELOAD)
  201. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  202. hwc->state = 0;
  203. /*
  204. * Set the period again. Some counters can't be stopped, so when we
  205. * were stopped we simply disabled the IRQ source and the counter
  206. * may have been left counting. If we don't do this step then we may
  207. * get an interrupt too soon or *way* too late if the overflow has
  208. * happened since disabling.
  209. */
  210. armpmu_event_set_period(event, hwc, hwc->idx);
  211. armpmu->enable(hwc, hwc->idx);
  212. }
  213. static void
  214. armpmu_del(struct perf_event *event, int flags)
  215. {
  216. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  217. struct pmu_hw_events *hw_events = armpmu->get_hw_events();
  218. struct hw_perf_event *hwc = &event->hw;
  219. int idx = hwc->idx;
  220. WARN_ON(idx < 0);
  221. armpmu_stop(event, PERF_EF_UPDATE);
  222. hw_events->events[idx] = NULL;
  223. clear_bit(idx, hw_events->used_mask);
  224. perf_event_update_userpage(event);
  225. }
  226. static int
  227. armpmu_add(struct perf_event *event, int flags)
  228. {
  229. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  230. struct pmu_hw_events *hw_events = armpmu->get_hw_events();
  231. struct hw_perf_event *hwc = &event->hw;
  232. int idx;
  233. int err = 0;
  234. perf_pmu_disable(event->pmu);
  235. /* If we don't have a space for the counter then finish early. */
  236. idx = armpmu->get_event_idx(hw_events, hwc);
  237. if (idx < 0) {
  238. err = idx;
  239. goto out;
  240. }
  241. /*
  242. * If there is an event in the counter we are going to use then make
  243. * sure it is disabled.
  244. */
  245. event->hw.idx = idx;
  246. armpmu->disable(hwc, idx);
  247. hw_events->events[idx] = event;
  248. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  249. if (flags & PERF_EF_START)
  250. armpmu_start(event, PERF_EF_RELOAD);
  251. /* Propagate our changes to the userspace mapping. */
  252. perf_event_update_userpage(event);
  253. out:
  254. perf_pmu_enable(event->pmu);
  255. return err;
  256. }
  257. static int
  258. validate_event(struct pmu *pmu, struct pmu_hw_events *hw_events,
  259. struct perf_event *event)
  260. {
  261. struct arm_pmu *armpmu;
  262. struct hw_perf_event fake_event = event->hw;
  263. struct pmu *leader_pmu = event->group_leader->pmu;
  264. if (is_software_event(event))
  265. return 1;
  266. /*
  267. * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
  268. * core perf code won't check that the pmu->ctx == leader->ctx
  269. * until after pmu->event_init(event).
  270. */
  271. if (event->pmu != pmu)
  272. return 0;
  273. if (event->pmu != leader_pmu || event->state < PERF_EVENT_STATE_OFF)
  274. return 1;
  275. if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
  276. return 1;
  277. armpmu = to_arm_pmu(event->pmu);
  278. return armpmu->get_event_idx(hw_events, &fake_event) >= 0;
  279. }
  280. static int
  281. validate_group(struct perf_event *event)
  282. {
  283. struct perf_event *sibling, *leader = event->group_leader;
  284. struct pmu_hw_events fake_pmu;
  285. DECLARE_BITMAP(fake_used_mask, ARMPMU_MAX_HWEVENTS);
  286. /*
  287. * Initialise the fake PMU. We only need to populate the
  288. * used_mask for the purposes of validation.
  289. */
  290. memset(fake_used_mask, 0, sizeof(fake_used_mask));
  291. fake_pmu.used_mask = fake_used_mask;
  292. if (!validate_event(event->pmu, &fake_pmu, leader))
  293. return -EINVAL;
  294. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  295. if (!validate_event(event->pmu, &fake_pmu, sibling))
  296. return -EINVAL;
  297. }
  298. if (!validate_event(event->pmu, &fake_pmu, event))
  299. return -EINVAL;
  300. return 0;
  301. }
  302. static void
  303. armpmu_disable_percpu_irq(void *data)
  304. {
  305. unsigned int irq = *(unsigned int *)data;
  306. disable_percpu_irq(irq);
  307. }
  308. static void
  309. armpmu_release_hardware(struct arm_pmu *armpmu)
  310. {
  311. int irq;
  312. unsigned int i, irqs;
  313. struct platform_device *pmu_device = armpmu->plat_device;
  314. irqs = min(pmu_device->num_resources, num_possible_cpus());
  315. if (!irqs)
  316. return;
  317. irq = platform_get_irq(pmu_device, 0);
  318. if (irq <= 0)
  319. return;
  320. if (irq_is_percpu(irq)) {
  321. on_each_cpu(armpmu_disable_percpu_irq, &irq, 1);
  322. free_percpu_irq(irq, &cpu_hw_events);
  323. } else {
  324. for (i = 0; i < irqs; ++i) {
  325. if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
  326. continue;
  327. irq = platform_get_irq(pmu_device, i);
  328. if (irq > 0)
  329. free_irq(irq, armpmu);
  330. }
  331. }
  332. }
  333. static void
  334. armpmu_enable_percpu_irq(void *data)
  335. {
  336. unsigned int irq = *(unsigned int *)data;
  337. enable_percpu_irq(irq, IRQ_TYPE_NONE);
  338. }
  339. static int
  340. armpmu_reserve_hardware(struct arm_pmu *armpmu)
  341. {
  342. int err, irq;
  343. unsigned int i, irqs;
  344. struct platform_device *pmu_device = armpmu->plat_device;
  345. if (!pmu_device) {
  346. pr_err("no PMU device registered\n");
  347. return -ENODEV;
  348. }
  349. irqs = min(pmu_device->num_resources, num_possible_cpus());
  350. if (!irqs) {
  351. pr_err("no irqs for PMUs defined\n");
  352. return -ENODEV;
  353. }
  354. irq = platform_get_irq(pmu_device, 0);
  355. if (irq <= 0) {
  356. pr_err("failed to get valid irq for PMU device\n");
  357. return -ENODEV;
  358. }
  359. if (irq_is_percpu(irq)) {
  360. err = request_percpu_irq(irq, armpmu->handle_irq,
  361. "arm-pmu", &cpu_hw_events);
  362. if (err) {
  363. pr_err("unable to request percpu IRQ%d for ARM PMU counters\n",
  364. irq);
  365. armpmu_release_hardware(armpmu);
  366. return err;
  367. }
  368. on_each_cpu(armpmu_enable_percpu_irq, &irq, 1);
  369. } else {
  370. for (i = 0; i < irqs; ++i) {
  371. err = 0;
  372. irq = platform_get_irq(pmu_device, i);
  373. if (irq <= 0)
  374. continue;
  375. /*
  376. * If we have a single PMU interrupt that we can't shift,
  377. * assume that we're running on a uniprocessor machine and
  378. * continue. Otherwise, continue without this interrupt.
  379. */
  380. if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
  381. pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
  382. irq, i);
  383. continue;
  384. }
  385. err = request_irq(irq, armpmu->handle_irq,
  386. IRQF_NOBALANCING,
  387. "arm-pmu", armpmu);
  388. if (err) {
  389. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  390. irq);
  391. armpmu_release_hardware(armpmu);
  392. return err;
  393. }
  394. cpumask_set_cpu(i, &armpmu->active_irqs);
  395. }
  396. }
  397. return 0;
  398. }
  399. static void
  400. hw_perf_event_destroy(struct perf_event *event)
  401. {
  402. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  403. atomic_t *active_events = &armpmu->active_events;
  404. struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
  405. if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
  406. armpmu_release_hardware(armpmu);
  407. mutex_unlock(pmu_reserve_mutex);
  408. }
  409. }
  410. static int
  411. event_requires_mode_exclusion(struct perf_event_attr *attr)
  412. {
  413. return attr->exclude_idle || attr->exclude_user ||
  414. attr->exclude_kernel || attr->exclude_hv;
  415. }
  416. static int
  417. __hw_perf_event_init(struct perf_event *event)
  418. {
  419. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  420. struct hw_perf_event *hwc = &event->hw;
  421. int mapping, err;
  422. mapping = armpmu->map_event(event);
  423. if (mapping < 0) {
  424. pr_debug("event %x:%llx not supported\n", event->attr.type,
  425. event->attr.config);
  426. return mapping;
  427. }
  428. /*
  429. * We don't assign an index until we actually place the event onto
  430. * hardware. Use -1 to signify that we haven't decided where to put it
  431. * yet. For SMP systems, each core has it's own PMU so we can't do any
  432. * clever allocation or constraints checking at this point.
  433. */
  434. hwc->idx = -1;
  435. hwc->config_base = 0;
  436. hwc->config = 0;
  437. hwc->event_base = 0;
  438. /*
  439. * Check whether we need to exclude the counter from certain modes.
  440. */
  441. if ((!armpmu->set_event_filter ||
  442. armpmu->set_event_filter(hwc, &event->attr)) &&
  443. event_requires_mode_exclusion(&event->attr)) {
  444. pr_debug("ARM performance counters do not support mode exclusion\n");
  445. return -EPERM;
  446. }
  447. /*
  448. * Store the event encoding into the config_base field.
  449. */
  450. hwc->config_base |= (unsigned long)mapping;
  451. if (!hwc->sample_period) {
  452. /*
  453. * For non-sampling runs, limit the sample_period to half
  454. * of the counter width. That way, the new counter value
  455. * is far less likely to overtake the previous one unless
  456. * you have some serious IRQ latency issues.
  457. */
  458. hwc->sample_period = armpmu->max_period >> 1;
  459. hwc->last_period = hwc->sample_period;
  460. local64_set(&hwc->period_left, hwc->sample_period);
  461. }
  462. err = 0;
  463. if (event->group_leader != event) {
  464. err = validate_group(event);
  465. if (err)
  466. return -EINVAL;
  467. }
  468. return err;
  469. }
  470. static int armpmu_event_init(struct perf_event *event)
  471. {
  472. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  473. int err = 0;
  474. atomic_t *active_events = &armpmu->active_events;
  475. if (armpmu->map_event(event) == -ENOENT)
  476. return -ENOENT;
  477. event->destroy = hw_perf_event_destroy;
  478. if (!atomic_inc_not_zero(active_events)) {
  479. mutex_lock(&armpmu->reserve_mutex);
  480. if (atomic_read(active_events) == 0)
  481. err = armpmu_reserve_hardware(armpmu);
  482. if (!err)
  483. atomic_inc(active_events);
  484. mutex_unlock(&armpmu->reserve_mutex);
  485. }
  486. if (err)
  487. return err;
  488. err = __hw_perf_event_init(event);
  489. if (err)
  490. hw_perf_event_destroy(event);
  491. return err;
  492. }
  493. static void armpmu_enable(struct pmu *pmu)
  494. {
  495. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  496. struct pmu_hw_events *hw_events = armpmu->get_hw_events();
  497. int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
  498. if (enabled)
  499. armpmu->start();
  500. }
  501. static void armpmu_disable(struct pmu *pmu)
  502. {
  503. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  504. armpmu->stop();
  505. }
  506. static void __init armpmu_init(struct arm_pmu *armpmu)
  507. {
  508. atomic_set(&armpmu->active_events, 0);
  509. mutex_init(&armpmu->reserve_mutex);
  510. armpmu->pmu = (struct pmu) {
  511. .pmu_enable = armpmu_enable,
  512. .pmu_disable = armpmu_disable,
  513. .event_init = armpmu_event_init,
  514. .add = armpmu_add,
  515. .del = armpmu_del,
  516. .start = armpmu_start,
  517. .stop = armpmu_stop,
  518. .read = armpmu_read,
  519. };
  520. }
  521. int __init armpmu_register(struct arm_pmu *armpmu, char *name, int type)
  522. {
  523. armpmu_init(armpmu);
  524. return perf_pmu_register(&armpmu->pmu, name, type);
  525. }
  526. /*
  527. * ARMv8 PMUv3 Performance Events handling code.
  528. * Common event types.
  529. */
  530. enum armv8_pmuv3_perf_types {
  531. /* Required events. */
  532. ARMV8_PMUV3_PERFCTR_PMNC_SW_INCR = 0x00,
  533. ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL = 0x03,
  534. ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS = 0x04,
  535. ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
  536. ARMV8_PMUV3_PERFCTR_CLOCK_CYCLES = 0x11,
  537. ARMV8_PMUV3_PERFCTR_PC_BRANCH_PRED = 0x12,
  538. /* At least one of the following is required. */
  539. ARMV8_PMUV3_PERFCTR_INSTR_EXECUTED = 0x08,
  540. ARMV8_PMUV3_PERFCTR_OP_SPEC = 0x1B,
  541. /* Common architectural events. */
  542. ARMV8_PMUV3_PERFCTR_MEM_READ = 0x06,
  543. ARMV8_PMUV3_PERFCTR_MEM_WRITE = 0x07,
  544. ARMV8_PMUV3_PERFCTR_EXC_TAKEN = 0x09,
  545. ARMV8_PMUV3_PERFCTR_EXC_EXECUTED = 0x0A,
  546. ARMV8_PMUV3_PERFCTR_CID_WRITE = 0x0B,
  547. ARMV8_PMUV3_PERFCTR_PC_WRITE = 0x0C,
  548. ARMV8_PMUV3_PERFCTR_PC_IMM_BRANCH = 0x0D,
  549. ARMV8_PMUV3_PERFCTR_PC_PROC_RETURN = 0x0E,
  550. ARMV8_PMUV3_PERFCTR_MEM_UNALIGNED_ACCESS = 0x0F,
  551. ARMV8_PMUV3_PERFCTR_TTBR_WRITE = 0x1C,
  552. /* Common microarchitectural events. */
  553. ARMV8_PMUV3_PERFCTR_L1_ICACHE_REFILL = 0x01,
  554. ARMV8_PMUV3_PERFCTR_ITLB_REFILL = 0x02,
  555. ARMV8_PMUV3_PERFCTR_DTLB_REFILL = 0x05,
  556. ARMV8_PMUV3_PERFCTR_MEM_ACCESS = 0x13,
  557. ARMV8_PMUV3_PERFCTR_L1_ICACHE_ACCESS = 0x14,
  558. ARMV8_PMUV3_PERFCTR_L1_DCACHE_WB = 0x15,
  559. ARMV8_PMUV3_PERFCTR_L2_CACHE_ACCESS = 0x16,
  560. ARMV8_PMUV3_PERFCTR_L2_CACHE_REFILL = 0x17,
  561. ARMV8_PMUV3_PERFCTR_L2_CACHE_WB = 0x18,
  562. ARMV8_PMUV3_PERFCTR_BUS_ACCESS = 0x19,
  563. ARMV8_PMUV3_PERFCTR_MEM_ERROR = 0x1A,
  564. ARMV8_PMUV3_PERFCTR_BUS_CYCLES = 0x1D,
  565. };
  566. /* PMUv3 HW events mapping. */
  567. static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
  568. [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CLOCK_CYCLES,
  569. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INSTR_EXECUTED,
  570. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS,
  571. [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL,
  572. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = HW_OP_UNSUPPORTED,
  573. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED,
  574. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  575. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = HW_OP_UNSUPPORTED,
  576. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = HW_OP_UNSUPPORTED,
  577. };
  578. static const unsigned armv8_pmuv3_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  579. [PERF_COUNT_HW_CACHE_OP_MAX]
  580. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  581. [C(L1D)] = {
  582. [C(OP_READ)] = {
  583. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS,
  584. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL,
  585. },
  586. [C(OP_WRITE)] = {
  587. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS,
  588. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL,
  589. },
  590. [C(OP_PREFETCH)] = {
  591. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  592. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  593. },
  594. },
  595. [C(L1I)] = {
  596. [C(OP_READ)] = {
  597. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  598. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  599. },
  600. [C(OP_WRITE)] = {
  601. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  602. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  603. },
  604. [C(OP_PREFETCH)] = {
  605. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  606. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  607. },
  608. },
  609. [C(LL)] = {
  610. [C(OP_READ)] = {
  611. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  612. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  613. },
  614. [C(OP_WRITE)] = {
  615. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  616. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  617. },
  618. [C(OP_PREFETCH)] = {
  619. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  620. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  621. },
  622. },
  623. [C(DTLB)] = {
  624. [C(OP_READ)] = {
  625. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  626. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  627. },
  628. [C(OP_WRITE)] = {
  629. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  630. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  631. },
  632. [C(OP_PREFETCH)] = {
  633. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  634. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  635. },
  636. },
  637. [C(ITLB)] = {
  638. [C(OP_READ)] = {
  639. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  640. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  641. },
  642. [C(OP_WRITE)] = {
  643. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  644. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  645. },
  646. [C(OP_PREFETCH)] = {
  647. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  648. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  649. },
  650. },
  651. [C(BPU)] = {
  652. [C(OP_READ)] = {
  653. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_PRED,
  654. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED,
  655. },
  656. [C(OP_WRITE)] = {
  657. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_PRED,
  658. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED,
  659. },
  660. [C(OP_PREFETCH)] = {
  661. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  662. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  663. },
  664. },
  665. [C(NODE)] = {
  666. [C(OP_READ)] = {
  667. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  668. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  669. },
  670. [C(OP_WRITE)] = {
  671. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  672. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  673. },
  674. [C(OP_PREFETCH)] = {
  675. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  676. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  677. },
  678. },
  679. };
  680. /*
  681. * Perf Events' indices
  682. */
  683. #define ARMV8_IDX_CYCLE_COUNTER 0
  684. #define ARMV8_IDX_COUNTER0 1
  685. #define ARMV8_IDX_COUNTER_LAST (ARMV8_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
  686. #define ARMV8_MAX_COUNTERS 32
  687. #define ARMV8_COUNTER_MASK (ARMV8_MAX_COUNTERS - 1)
  688. /*
  689. * ARMv8 low level PMU access
  690. */
  691. /*
  692. * Perf Event to low level counters mapping
  693. */
  694. #define ARMV8_IDX_TO_COUNTER(x) \
  695. (((x) - ARMV8_IDX_COUNTER0) & ARMV8_COUNTER_MASK)
  696. /*
  697. * Per-CPU PMCR: config reg
  698. */
  699. #define ARMV8_PMCR_E (1 << 0) /* Enable all counters */
  700. #define ARMV8_PMCR_P (1 << 1) /* Reset all counters */
  701. #define ARMV8_PMCR_C (1 << 2) /* Cycle counter reset */
  702. #define ARMV8_PMCR_D (1 << 3) /* CCNT counts every 64th cpu cycle */
  703. #define ARMV8_PMCR_X (1 << 4) /* Export to ETM */
  704. #define ARMV8_PMCR_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
  705. #define ARMV8_PMCR_N_SHIFT 11 /* Number of counters supported */
  706. #define ARMV8_PMCR_N_MASK 0x1f
  707. #define ARMV8_PMCR_MASK 0x3f /* Mask for writable bits */
  708. /*
  709. * PMOVSR: counters overflow flag status reg
  710. */
  711. #define ARMV8_OVSR_MASK 0xffffffff /* Mask for writable bits */
  712. #define ARMV8_OVERFLOWED_MASK ARMV8_OVSR_MASK
  713. /*
  714. * PMXEVTYPER: Event selection reg
  715. */
  716. #define ARMV8_EVTYPE_MASK 0xc80003ff /* Mask for writable bits */
  717. #define ARMV8_EVTYPE_EVENT 0x3ff /* Mask for EVENT bits */
  718. /*
  719. * Event filters for PMUv3
  720. */
  721. #define ARMV8_EXCLUDE_EL1 (1 << 31)
  722. #define ARMV8_EXCLUDE_EL0 (1 << 30)
  723. #define ARMV8_INCLUDE_EL2 (1 << 27)
  724. static inline u32 armv8pmu_pmcr_read(void)
  725. {
  726. u32 val;
  727. asm volatile("mrs %0, pmcr_el0" : "=r" (val));
  728. return val;
  729. }
  730. static inline void armv8pmu_pmcr_write(u32 val)
  731. {
  732. val &= ARMV8_PMCR_MASK;
  733. isb();
  734. asm volatile("msr pmcr_el0, %0" :: "r" (val));
  735. }
  736. static inline int armv8pmu_has_overflowed(u32 pmovsr)
  737. {
  738. return pmovsr & ARMV8_OVERFLOWED_MASK;
  739. }
  740. static inline int armv8pmu_counter_valid(int idx)
  741. {
  742. return idx >= ARMV8_IDX_CYCLE_COUNTER && idx <= ARMV8_IDX_COUNTER_LAST;
  743. }
  744. static inline int armv8pmu_counter_has_overflowed(u32 pmnc, int idx)
  745. {
  746. int ret = 0;
  747. u32 counter;
  748. if (!armv8pmu_counter_valid(idx)) {
  749. pr_err("CPU%u checking wrong counter %d overflow status\n",
  750. smp_processor_id(), idx);
  751. } else {
  752. counter = ARMV8_IDX_TO_COUNTER(idx);
  753. ret = pmnc & BIT(counter);
  754. }
  755. return ret;
  756. }
  757. static inline int armv8pmu_select_counter(int idx)
  758. {
  759. u32 counter;
  760. if (!armv8pmu_counter_valid(idx)) {
  761. pr_err("CPU%u selecting wrong PMNC counter %d\n",
  762. smp_processor_id(), idx);
  763. return -EINVAL;
  764. }
  765. counter = ARMV8_IDX_TO_COUNTER(idx);
  766. asm volatile("msr pmselr_el0, %0" :: "r" (counter));
  767. isb();
  768. return idx;
  769. }
  770. static inline u32 armv8pmu_read_counter(int idx)
  771. {
  772. u32 value = 0;
  773. if (!armv8pmu_counter_valid(idx))
  774. pr_err("CPU%u reading wrong counter %d\n",
  775. smp_processor_id(), idx);
  776. else if (idx == ARMV8_IDX_CYCLE_COUNTER)
  777. asm volatile("mrs %0, pmccntr_el0" : "=r" (value));
  778. else if (armv8pmu_select_counter(idx) == idx)
  779. asm volatile("mrs %0, pmxevcntr_el0" : "=r" (value));
  780. return value;
  781. }
  782. static inline void armv8pmu_write_counter(int idx, u32 value)
  783. {
  784. if (!armv8pmu_counter_valid(idx))
  785. pr_err("CPU%u writing wrong counter %d\n",
  786. smp_processor_id(), idx);
  787. else if (idx == ARMV8_IDX_CYCLE_COUNTER)
  788. asm volatile("msr pmccntr_el0, %0" :: "r" (value));
  789. else if (armv8pmu_select_counter(idx) == idx)
  790. asm volatile("msr pmxevcntr_el0, %0" :: "r" (value));
  791. }
  792. static inline void armv8pmu_write_evtype(int idx, u32 val)
  793. {
  794. if (armv8pmu_select_counter(idx) == idx) {
  795. val &= ARMV8_EVTYPE_MASK;
  796. asm volatile("msr pmxevtyper_el0, %0" :: "r" (val));
  797. }
  798. }
  799. static inline int armv8pmu_enable_counter(int idx)
  800. {
  801. u32 counter;
  802. if (!armv8pmu_counter_valid(idx)) {
  803. pr_err("CPU%u enabling wrong PMNC counter %d\n",
  804. smp_processor_id(), idx);
  805. return -EINVAL;
  806. }
  807. counter = ARMV8_IDX_TO_COUNTER(idx);
  808. asm volatile("msr pmcntenset_el0, %0" :: "r" (BIT(counter)));
  809. return idx;
  810. }
  811. static inline int armv8pmu_disable_counter(int idx)
  812. {
  813. u32 counter;
  814. if (!armv8pmu_counter_valid(idx)) {
  815. pr_err("CPU%u disabling wrong PMNC counter %d\n",
  816. smp_processor_id(), idx);
  817. return -EINVAL;
  818. }
  819. counter = ARMV8_IDX_TO_COUNTER(idx);
  820. asm volatile("msr pmcntenclr_el0, %0" :: "r" (BIT(counter)));
  821. return idx;
  822. }
  823. static inline int armv8pmu_enable_intens(int idx)
  824. {
  825. u32 counter;
  826. if (!armv8pmu_counter_valid(idx)) {
  827. pr_err("CPU%u enabling wrong PMNC counter IRQ enable %d\n",
  828. smp_processor_id(), idx);
  829. return -EINVAL;
  830. }
  831. counter = ARMV8_IDX_TO_COUNTER(idx);
  832. asm volatile("msr pmintenset_el1, %0" :: "r" (BIT(counter)));
  833. return idx;
  834. }
  835. static inline int armv8pmu_disable_intens(int idx)
  836. {
  837. u32 counter;
  838. if (!armv8pmu_counter_valid(idx)) {
  839. pr_err("CPU%u disabling wrong PMNC counter IRQ enable %d\n",
  840. smp_processor_id(), idx);
  841. return -EINVAL;
  842. }
  843. counter = ARMV8_IDX_TO_COUNTER(idx);
  844. asm volatile("msr pmintenclr_el1, %0" :: "r" (BIT(counter)));
  845. isb();
  846. /* Clear the overflow flag in case an interrupt is pending. */
  847. asm volatile("msr pmovsclr_el0, %0" :: "r" (BIT(counter)));
  848. isb();
  849. return idx;
  850. }
  851. static inline u32 armv8pmu_getreset_flags(void)
  852. {
  853. u32 value;
  854. /* Read */
  855. asm volatile("mrs %0, pmovsclr_el0" : "=r" (value));
  856. /* Write to clear flags */
  857. value &= ARMV8_OVSR_MASK;
  858. asm volatile("msr pmovsclr_el0, %0" :: "r" (value));
  859. return value;
  860. }
  861. static void armv8pmu_enable_event(struct hw_perf_event *hwc, int idx)
  862. {
  863. unsigned long flags;
  864. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  865. /*
  866. * Enable counter and interrupt, and set the counter to count
  867. * the event that we're interested in.
  868. */
  869. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  870. /*
  871. * Disable counter
  872. */
  873. armv8pmu_disable_counter(idx);
  874. /*
  875. * Set event (if destined for PMNx counters).
  876. */
  877. armv8pmu_write_evtype(idx, hwc->config_base);
  878. /*
  879. * Enable interrupt for this counter
  880. */
  881. armv8pmu_enable_intens(idx);
  882. /*
  883. * Enable counter
  884. */
  885. armv8pmu_enable_counter(idx);
  886. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  887. }
  888. static void armv8pmu_disable_event(struct hw_perf_event *hwc, int idx)
  889. {
  890. unsigned long flags;
  891. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  892. /*
  893. * Disable counter and interrupt
  894. */
  895. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  896. /*
  897. * Disable counter
  898. */
  899. armv8pmu_disable_counter(idx);
  900. /*
  901. * Disable interrupt for this counter
  902. */
  903. armv8pmu_disable_intens(idx);
  904. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  905. }
  906. static irqreturn_t armv8pmu_handle_irq(int irq_num, void *dev)
  907. {
  908. u32 pmovsr;
  909. struct perf_sample_data data;
  910. struct pmu_hw_events *cpuc;
  911. struct pt_regs *regs;
  912. int idx;
  913. /*
  914. * Get and reset the IRQ flags
  915. */
  916. pmovsr = armv8pmu_getreset_flags();
  917. /*
  918. * Did an overflow occur?
  919. */
  920. if (!armv8pmu_has_overflowed(pmovsr))
  921. return IRQ_NONE;
  922. /*
  923. * Handle the counter(s) overflow(s)
  924. */
  925. regs = get_irq_regs();
  926. cpuc = this_cpu_ptr(&cpu_hw_events);
  927. for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
  928. struct perf_event *event = cpuc->events[idx];
  929. struct hw_perf_event *hwc;
  930. /* Ignore if we don't have an event. */
  931. if (!event)
  932. continue;
  933. /*
  934. * We have a single interrupt for all counters. Check that
  935. * each counter has overflowed before we process it.
  936. */
  937. if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
  938. continue;
  939. hwc = &event->hw;
  940. armpmu_event_update(event, hwc, idx);
  941. perf_sample_data_init(&data, 0, hwc->last_period);
  942. if (!armpmu_event_set_period(event, hwc, idx))
  943. continue;
  944. if (perf_event_overflow(event, &data, regs))
  945. cpu_pmu->disable(hwc, idx);
  946. }
  947. /*
  948. * Handle the pending perf events.
  949. *
  950. * Note: this call *must* be run with interrupts disabled. For
  951. * platforms that can have the PMU interrupts raised as an NMI, this
  952. * will not work.
  953. */
  954. irq_work_run();
  955. return IRQ_HANDLED;
  956. }
  957. static void armv8pmu_start(void)
  958. {
  959. unsigned long flags;
  960. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  961. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  962. /* Enable all counters */
  963. armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMCR_E);
  964. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  965. }
  966. static void armv8pmu_stop(void)
  967. {
  968. unsigned long flags;
  969. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  970. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  971. /* Disable all counters */
  972. armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMCR_E);
  973. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  974. }
  975. static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
  976. struct hw_perf_event *event)
  977. {
  978. int idx;
  979. unsigned long evtype = event->config_base & ARMV8_EVTYPE_EVENT;
  980. /* Always place a cycle counter into the cycle counter. */
  981. if (evtype == ARMV8_PMUV3_PERFCTR_CLOCK_CYCLES) {
  982. if (test_and_set_bit(ARMV8_IDX_CYCLE_COUNTER, cpuc->used_mask))
  983. return -EAGAIN;
  984. return ARMV8_IDX_CYCLE_COUNTER;
  985. }
  986. /*
  987. * For anything other than a cycle counter, try and use
  988. * the events counters
  989. */
  990. for (idx = ARMV8_IDX_COUNTER0; idx < cpu_pmu->num_events; ++idx) {
  991. if (!test_and_set_bit(idx, cpuc->used_mask))
  992. return idx;
  993. }
  994. /* The counters are all in use. */
  995. return -EAGAIN;
  996. }
  997. /*
  998. * Add an event filter to a given event. This will only work for PMUv2 PMUs.
  999. */
  1000. static int armv8pmu_set_event_filter(struct hw_perf_event *event,
  1001. struct perf_event_attr *attr)
  1002. {
  1003. unsigned long config_base = 0;
  1004. if (attr->exclude_idle)
  1005. return -EPERM;
  1006. if (attr->exclude_user)
  1007. config_base |= ARMV8_EXCLUDE_EL0;
  1008. if (attr->exclude_kernel)
  1009. config_base |= ARMV8_EXCLUDE_EL1;
  1010. if (!attr->exclude_hv)
  1011. config_base |= ARMV8_INCLUDE_EL2;
  1012. /*
  1013. * Install the filter into config_base as this is used to
  1014. * construct the event type.
  1015. */
  1016. event->config_base = config_base;
  1017. return 0;
  1018. }
  1019. static void armv8pmu_reset(void *info)
  1020. {
  1021. u32 idx, nb_cnt = cpu_pmu->num_events;
  1022. /* The counter and interrupt enable registers are unknown at reset. */
  1023. for (idx = ARMV8_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx)
  1024. armv8pmu_disable_event(NULL, idx);
  1025. /* Initialize & Reset PMNC: C and P bits. */
  1026. armv8pmu_pmcr_write(ARMV8_PMCR_P | ARMV8_PMCR_C);
  1027. /* Disable access from userspace. */
  1028. asm volatile("msr pmuserenr_el0, %0" :: "r" (0));
  1029. }
  1030. static int armv8_pmuv3_map_event(struct perf_event *event)
  1031. {
  1032. return map_cpu_event(event, &armv8_pmuv3_perf_map,
  1033. &armv8_pmuv3_perf_cache_map,
  1034. ARMV8_EVTYPE_EVENT);
  1035. }
  1036. static struct arm_pmu armv8pmu = {
  1037. .handle_irq = armv8pmu_handle_irq,
  1038. .enable = armv8pmu_enable_event,
  1039. .disable = armv8pmu_disable_event,
  1040. .read_counter = armv8pmu_read_counter,
  1041. .write_counter = armv8pmu_write_counter,
  1042. .get_event_idx = armv8pmu_get_event_idx,
  1043. .start = armv8pmu_start,
  1044. .stop = armv8pmu_stop,
  1045. .reset = armv8pmu_reset,
  1046. .max_period = (1LLU << 32) - 1,
  1047. };
  1048. static u32 __init armv8pmu_read_num_pmnc_events(void)
  1049. {
  1050. u32 nb_cnt;
  1051. /* Read the nb of CNTx counters supported from PMNC */
  1052. nb_cnt = (armv8pmu_pmcr_read() >> ARMV8_PMCR_N_SHIFT) & ARMV8_PMCR_N_MASK;
  1053. /* Add the CPU cycles counter and return */
  1054. return nb_cnt + 1;
  1055. }
  1056. static struct arm_pmu *__init armv8_pmuv3_pmu_init(void)
  1057. {
  1058. armv8pmu.name = "arm/armv8-pmuv3";
  1059. armv8pmu.map_event = armv8_pmuv3_map_event;
  1060. armv8pmu.num_events = armv8pmu_read_num_pmnc_events();
  1061. armv8pmu.set_event_filter = armv8pmu_set_event_filter;
  1062. return &armv8pmu;
  1063. }
  1064. /*
  1065. * Ensure the PMU has sane values out of reset.
  1066. * This requires SMP to be available, so exists as a separate initcall.
  1067. */
  1068. static int __init
  1069. cpu_pmu_reset(void)
  1070. {
  1071. if (cpu_pmu && cpu_pmu->reset)
  1072. return on_each_cpu(cpu_pmu->reset, NULL, 1);
  1073. return 0;
  1074. }
  1075. arch_initcall(cpu_pmu_reset);
  1076. /*
  1077. * PMU platform driver and devicetree bindings.
  1078. */
  1079. static const struct of_device_id armpmu_of_device_ids[] = {
  1080. {.compatible = "arm,armv8-pmuv3"},
  1081. {},
  1082. };
  1083. static int armpmu_device_probe(struct platform_device *pdev)
  1084. {
  1085. if (!cpu_pmu)
  1086. return -ENODEV;
  1087. cpu_pmu->plat_device = pdev;
  1088. return 0;
  1089. }
  1090. static struct platform_driver armpmu_driver = {
  1091. .driver = {
  1092. .name = "arm-pmu",
  1093. .of_match_table = armpmu_of_device_ids,
  1094. },
  1095. .probe = armpmu_device_probe,
  1096. };
  1097. static int __init register_pmu_driver(void)
  1098. {
  1099. return platform_driver_register(&armpmu_driver);
  1100. }
  1101. device_initcall(register_pmu_driver);
  1102. static struct pmu_hw_events *armpmu_get_cpu_events(void)
  1103. {
  1104. return this_cpu_ptr(&cpu_hw_events);
  1105. }
  1106. static void __init cpu_pmu_init(struct arm_pmu *armpmu)
  1107. {
  1108. int cpu;
  1109. for_each_possible_cpu(cpu) {
  1110. struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
  1111. events->events = per_cpu(hw_events, cpu);
  1112. events->used_mask = per_cpu(used_mask, cpu);
  1113. raw_spin_lock_init(&events->pmu_lock);
  1114. }
  1115. armpmu->get_hw_events = armpmu_get_cpu_events;
  1116. }
  1117. static int __init init_hw_perf_events(void)
  1118. {
  1119. u64 dfr = read_cpuid(ID_AA64DFR0_EL1);
  1120. switch ((dfr >> 8) & 0xf) {
  1121. case 0x1: /* PMUv3 */
  1122. cpu_pmu = armv8_pmuv3_pmu_init();
  1123. break;
  1124. }
  1125. if (cpu_pmu) {
  1126. pr_info("enabled with %s PMU driver, %d counters available\n",
  1127. cpu_pmu->name, cpu_pmu->num_events);
  1128. cpu_pmu_init(cpu_pmu);
  1129. armpmu_register(cpu_pmu, "cpu", PERF_TYPE_RAW);
  1130. } else {
  1131. pr_info("no hardware support available\n");
  1132. }
  1133. return 0;
  1134. }
  1135. early_initcall(init_hw_perf_events);
  1136. /*
  1137. * Callchain handling code.
  1138. */
  1139. struct frame_tail {
  1140. struct frame_tail __user *fp;
  1141. unsigned long lr;
  1142. } __attribute__((packed));
  1143. /*
  1144. * Get the return address for a single stackframe and return a pointer to the
  1145. * next frame tail.
  1146. */
  1147. static struct frame_tail __user *
  1148. user_backtrace(struct frame_tail __user *tail,
  1149. struct perf_callchain_entry *entry)
  1150. {
  1151. struct frame_tail buftail;
  1152. unsigned long err;
  1153. /* Also check accessibility of one struct frame_tail beyond */
  1154. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  1155. return NULL;
  1156. pagefault_disable();
  1157. err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
  1158. pagefault_enable();
  1159. if (err)
  1160. return NULL;
  1161. perf_callchain_store(entry, buftail.lr);
  1162. /*
  1163. * Frame pointers should strictly progress back up the stack
  1164. * (towards higher addresses).
  1165. */
  1166. if (tail >= buftail.fp)
  1167. return NULL;
  1168. return buftail.fp;
  1169. }
  1170. #ifdef CONFIG_COMPAT
  1171. /*
  1172. * The registers we're interested in are at the end of the variable
  1173. * length saved register structure. The fp points at the end of this
  1174. * structure so the address of this struct is:
  1175. * (struct compat_frame_tail *)(xxx->fp)-1
  1176. *
  1177. * This code has been adapted from the ARM OProfile support.
  1178. */
  1179. struct compat_frame_tail {
  1180. compat_uptr_t fp; /* a (struct compat_frame_tail *) in compat mode */
  1181. u32 sp;
  1182. u32 lr;
  1183. } __attribute__((packed));
  1184. static struct compat_frame_tail __user *
  1185. compat_user_backtrace(struct compat_frame_tail __user *tail,
  1186. struct perf_callchain_entry *entry)
  1187. {
  1188. struct compat_frame_tail buftail;
  1189. unsigned long err;
  1190. /* Also check accessibility of one struct frame_tail beyond */
  1191. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  1192. return NULL;
  1193. pagefault_disable();
  1194. err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
  1195. pagefault_enable();
  1196. if (err)
  1197. return NULL;
  1198. perf_callchain_store(entry, buftail.lr);
  1199. /*
  1200. * Frame pointers should strictly progress back up the stack
  1201. * (towards higher addresses).
  1202. */
  1203. if (tail + 1 >= (struct compat_frame_tail __user *)
  1204. compat_ptr(buftail.fp))
  1205. return NULL;
  1206. return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1;
  1207. }
  1208. #endif /* CONFIG_COMPAT */
  1209. void perf_callchain_user(struct perf_callchain_entry *entry,
  1210. struct pt_regs *regs)
  1211. {
  1212. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1213. /* We don't support guest os callchain now */
  1214. return;
  1215. }
  1216. perf_callchain_store(entry, regs->pc);
  1217. if (!compat_user_mode(regs)) {
  1218. /* AARCH64 mode */
  1219. struct frame_tail __user *tail;
  1220. tail = (struct frame_tail __user *)regs->regs[29];
  1221. while (entry->nr < PERF_MAX_STACK_DEPTH &&
  1222. tail && !((unsigned long)tail & 0xf))
  1223. tail = user_backtrace(tail, entry);
  1224. } else {
  1225. #ifdef CONFIG_COMPAT
  1226. /* AARCH32 compat mode */
  1227. struct compat_frame_tail __user *tail;
  1228. tail = (struct compat_frame_tail __user *)regs->compat_fp - 1;
  1229. while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
  1230. tail && !((unsigned long)tail & 0x3))
  1231. tail = compat_user_backtrace(tail, entry);
  1232. #endif
  1233. }
  1234. }
  1235. /*
  1236. * Gets called by walk_stackframe() for every stackframe. This will be called
  1237. * whist unwinding the stackframe and is like a subroutine return so we use
  1238. * the PC.
  1239. */
  1240. static int callchain_trace(struct stackframe *frame, void *data)
  1241. {
  1242. struct perf_callchain_entry *entry = data;
  1243. perf_callchain_store(entry, frame->pc);
  1244. return 0;
  1245. }
  1246. void perf_callchain_kernel(struct perf_callchain_entry *entry,
  1247. struct pt_regs *regs)
  1248. {
  1249. struct stackframe frame;
  1250. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1251. /* We don't support guest os callchain now */
  1252. return;
  1253. }
  1254. frame.fp = regs->regs[29];
  1255. frame.sp = regs->sp;
  1256. frame.pc = regs->pc;
  1257. walk_stackframe(&frame, callchain_trace, entry);
  1258. }
  1259. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1260. {
  1261. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1262. return perf_guest_cbs->get_guest_ip();
  1263. return instruction_pointer(regs);
  1264. }
  1265. unsigned long perf_misc_flags(struct pt_regs *regs)
  1266. {
  1267. int misc = 0;
  1268. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1269. if (perf_guest_cbs->is_user_mode())
  1270. misc |= PERF_RECORD_MISC_GUEST_USER;
  1271. else
  1272. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1273. } else {
  1274. if (user_mode(regs))
  1275. misc |= PERF_RECORD_MISC_USER;
  1276. else
  1277. misc |= PERF_RECORD_MISC_KERNEL;
  1278. }
  1279. return misc;
  1280. }