perf_event.c 38 KB

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