perf_event.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  1. #undef DEBUG
  2. /*
  3. * ARM performance counter support.
  4. *
  5. * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
  6. *
  7. * ARMv7 support: Jean Pihet <jpihet@mvista.com>
  8. * 2010 (c) MontaVista Software, LLC.
  9. *
  10. * This code is based on the sparc64 perf event code, which is in turn based
  11. * on the x86 code. Callchain code is based on the ARM OProfile backtrace
  12. * code.
  13. */
  14. #define pr_fmt(fmt) "hw perfevents: " fmt
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/perf_event.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/cputype.h>
  21. #include <asm/irq.h>
  22. #include <asm/irq_regs.h>
  23. #include <asm/pmu.h>
  24. #include <asm/stacktrace.h>
  25. static const struct pmu_irqs *pmu_irqs;
  26. /*
  27. * Hardware lock to serialize accesses to PMU registers. Needed for the
  28. * read/modify/write sequences.
  29. */
  30. DEFINE_SPINLOCK(pmu_lock);
  31. /*
  32. * ARMv6 supports a maximum of 3 events, starting from index 1. If we add
  33. * another platform that supports more, we need to increase this to be the
  34. * largest of all platforms.
  35. *
  36. * ARMv7 supports up to 32 events:
  37. * cycle counter CCNT + 31 events counters CNT0..30.
  38. * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
  39. */
  40. #define ARMPMU_MAX_HWEVENTS 33
  41. /* The events for a given CPU. */
  42. struct cpu_hw_events {
  43. /*
  44. * The events that are active on the CPU for the given index. Index 0
  45. * is reserved.
  46. */
  47. struct perf_event *events[ARMPMU_MAX_HWEVENTS];
  48. /*
  49. * A 1 bit for an index indicates that the counter is being used for
  50. * an event. A 0 means that the counter can be used.
  51. */
  52. unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
  53. /*
  54. * A 1 bit for an index indicates that the counter is actively being
  55. * used.
  56. */
  57. unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
  58. };
  59. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  60. struct arm_pmu {
  61. char *name;
  62. irqreturn_t (*handle_irq)(int irq_num, void *dev);
  63. void (*enable)(struct hw_perf_event *evt, int idx);
  64. void (*disable)(struct hw_perf_event *evt, int idx);
  65. int (*event_map)(int evt);
  66. u64 (*raw_event)(u64);
  67. int (*get_event_idx)(struct cpu_hw_events *cpuc,
  68. struct hw_perf_event *hwc);
  69. u32 (*read_counter)(int idx);
  70. void (*write_counter)(int idx, u32 val);
  71. void (*start)(void);
  72. void (*stop)(void);
  73. int num_events;
  74. u64 max_period;
  75. };
  76. /* Set at runtime when we know what CPU type we are. */
  77. static const struct arm_pmu *armpmu;
  78. #define HW_OP_UNSUPPORTED 0xFFFF
  79. #define C(_x) \
  80. PERF_COUNT_HW_CACHE_##_x
  81. #define CACHE_OP_UNSUPPORTED 0xFFFF
  82. static unsigned armpmu_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  83. [PERF_COUNT_HW_CACHE_OP_MAX]
  84. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  85. static int
  86. armpmu_map_cache_event(u64 config)
  87. {
  88. unsigned int cache_type, cache_op, cache_result, ret;
  89. cache_type = (config >> 0) & 0xff;
  90. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  91. return -EINVAL;
  92. cache_op = (config >> 8) & 0xff;
  93. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  94. return -EINVAL;
  95. cache_result = (config >> 16) & 0xff;
  96. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  97. return -EINVAL;
  98. ret = (int)armpmu_perf_cache_map[cache_type][cache_op][cache_result];
  99. if (ret == CACHE_OP_UNSUPPORTED)
  100. return -ENOENT;
  101. return ret;
  102. }
  103. static int
  104. armpmu_event_set_period(struct perf_event *event,
  105. struct hw_perf_event *hwc,
  106. int idx)
  107. {
  108. s64 left = atomic64_read(&hwc->period_left);
  109. s64 period = hwc->sample_period;
  110. int ret = 0;
  111. if (unlikely(left <= -period)) {
  112. left = period;
  113. atomic64_set(&hwc->period_left, left);
  114. hwc->last_period = period;
  115. ret = 1;
  116. }
  117. if (unlikely(left <= 0)) {
  118. left += period;
  119. atomic64_set(&hwc->period_left, left);
  120. hwc->last_period = period;
  121. ret = 1;
  122. }
  123. if (left > (s64)armpmu->max_period)
  124. left = armpmu->max_period;
  125. atomic64_set(&hwc->prev_count, (u64)-left);
  126. armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
  127. perf_event_update_userpage(event);
  128. return ret;
  129. }
  130. static u64
  131. armpmu_event_update(struct perf_event *event,
  132. struct hw_perf_event *hwc,
  133. int idx)
  134. {
  135. int shift = 64 - 32;
  136. s64 prev_raw_count, new_raw_count;
  137. s64 delta;
  138. again:
  139. prev_raw_count = atomic64_read(&hwc->prev_count);
  140. new_raw_count = armpmu->read_counter(idx);
  141. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  142. new_raw_count) != prev_raw_count)
  143. goto again;
  144. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  145. delta >>= shift;
  146. atomic64_add(delta, &event->count);
  147. atomic64_sub(delta, &hwc->period_left);
  148. return new_raw_count;
  149. }
  150. static void
  151. armpmu_disable(struct perf_event *event)
  152. {
  153. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  154. struct hw_perf_event *hwc = &event->hw;
  155. int idx = hwc->idx;
  156. WARN_ON(idx < 0);
  157. clear_bit(idx, cpuc->active_mask);
  158. armpmu->disable(hwc, idx);
  159. barrier();
  160. armpmu_event_update(event, hwc, idx);
  161. cpuc->events[idx] = NULL;
  162. clear_bit(idx, cpuc->used_mask);
  163. perf_event_update_userpage(event);
  164. }
  165. static void
  166. armpmu_read(struct perf_event *event)
  167. {
  168. struct hw_perf_event *hwc = &event->hw;
  169. /* Don't read disabled counters! */
  170. if (hwc->idx < 0)
  171. return;
  172. armpmu_event_update(event, hwc, hwc->idx);
  173. }
  174. static void
  175. armpmu_unthrottle(struct perf_event *event)
  176. {
  177. struct hw_perf_event *hwc = &event->hw;
  178. /*
  179. * Set the period again. Some counters can't be stopped, so when we
  180. * were throttled we simply disabled the IRQ source and the counter
  181. * may have been left counting. If we don't do this step then we may
  182. * get an interrupt too soon or *way* too late if the overflow has
  183. * happened since disabling.
  184. */
  185. armpmu_event_set_period(event, hwc, hwc->idx);
  186. armpmu->enable(hwc, hwc->idx);
  187. }
  188. static int
  189. armpmu_enable(struct perf_event *event)
  190. {
  191. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  192. struct hw_perf_event *hwc = &event->hw;
  193. int idx;
  194. int err = 0;
  195. /* If we don't have a space for the counter then finish early. */
  196. idx = armpmu->get_event_idx(cpuc, hwc);
  197. if (idx < 0) {
  198. err = idx;
  199. goto out;
  200. }
  201. /*
  202. * If there is an event in the counter we are going to use then make
  203. * sure it is disabled.
  204. */
  205. event->hw.idx = idx;
  206. armpmu->disable(hwc, idx);
  207. cpuc->events[idx] = event;
  208. set_bit(idx, cpuc->active_mask);
  209. /* Set the period for the event. */
  210. armpmu_event_set_period(event, hwc, idx);
  211. /* Enable the event. */
  212. armpmu->enable(hwc, idx);
  213. /* Propagate our changes to the userspace mapping. */
  214. perf_event_update_userpage(event);
  215. out:
  216. return err;
  217. }
  218. static struct pmu pmu = {
  219. .enable = armpmu_enable,
  220. .disable = armpmu_disable,
  221. .unthrottle = armpmu_unthrottle,
  222. .read = armpmu_read,
  223. };
  224. static int
  225. validate_event(struct cpu_hw_events *cpuc,
  226. struct perf_event *event)
  227. {
  228. struct hw_perf_event fake_event = event->hw;
  229. if (event->pmu && event->pmu != &pmu)
  230. return 0;
  231. return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
  232. }
  233. static int
  234. validate_group(struct perf_event *event)
  235. {
  236. struct perf_event *sibling, *leader = event->group_leader;
  237. struct cpu_hw_events fake_pmu;
  238. memset(&fake_pmu, 0, sizeof(fake_pmu));
  239. if (!validate_event(&fake_pmu, leader))
  240. return -ENOSPC;
  241. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  242. if (!validate_event(&fake_pmu, sibling))
  243. return -ENOSPC;
  244. }
  245. if (!validate_event(&fake_pmu, event))
  246. return -ENOSPC;
  247. return 0;
  248. }
  249. static int
  250. armpmu_reserve_hardware(void)
  251. {
  252. int i;
  253. int err;
  254. pmu_irqs = reserve_pmu();
  255. if (IS_ERR(pmu_irqs)) {
  256. pr_warning("unable to reserve pmu\n");
  257. return PTR_ERR(pmu_irqs);
  258. }
  259. init_pmu();
  260. if (pmu_irqs->num_irqs < 1) {
  261. pr_err("no irqs for PMUs defined\n");
  262. return -ENODEV;
  263. }
  264. for (i = 0; i < pmu_irqs->num_irqs; ++i) {
  265. err = request_irq(pmu_irqs->irqs[i], armpmu->handle_irq,
  266. IRQF_DISABLED, "armpmu", NULL);
  267. if (err) {
  268. pr_warning("unable to request IRQ%d for ARM "
  269. "perf counters\n", pmu_irqs->irqs[i]);
  270. break;
  271. }
  272. }
  273. if (err) {
  274. for (i = i - 1; i >= 0; --i)
  275. free_irq(pmu_irqs->irqs[i], NULL);
  276. release_pmu(pmu_irqs);
  277. pmu_irqs = NULL;
  278. }
  279. return err;
  280. }
  281. static void
  282. armpmu_release_hardware(void)
  283. {
  284. int i;
  285. for (i = pmu_irqs->num_irqs - 1; i >= 0; --i)
  286. free_irq(pmu_irqs->irqs[i], NULL);
  287. armpmu->stop();
  288. release_pmu(pmu_irqs);
  289. pmu_irqs = NULL;
  290. }
  291. static atomic_t active_events = ATOMIC_INIT(0);
  292. static DEFINE_MUTEX(pmu_reserve_mutex);
  293. static void
  294. hw_perf_event_destroy(struct perf_event *event)
  295. {
  296. if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
  297. armpmu_release_hardware();
  298. mutex_unlock(&pmu_reserve_mutex);
  299. }
  300. }
  301. static int
  302. __hw_perf_event_init(struct perf_event *event)
  303. {
  304. struct hw_perf_event *hwc = &event->hw;
  305. int mapping, err;
  306. /* Decode the generic type into an ARM event identifier. */
  307. if (PERF_TYPE_HARDWARE == event->attr.type) {
  308. mapping = armpmu->event_map(event->attr.config);
  309. } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
  310. mapping = armpmu_map_cache_event(event->attr.config);
  311. } else if (PERF_TYPE_RAW == event->attr.type) {
  312. mapping = armpmu->raw_event(event->attr.config);
  313. } else {
  314. pr_debug("event type %x not supported\n", event->attr.type);
  315. return -EOPNOTSUPP;
  316. }
  317. if (mapping < 0) {
  318. pr_debug("event %x:%llx not supported\n", event->attr.type,
  319. event->attr.config);
  320. return mapping;
  321. }
  322. /*
  323. * Check whether we need to exclude the counter from certain modes.
  324. * The ARM performance counters are on all of the time so if someone
  325. * has asked us for some excludes then we have to fail.
  326. */
  327. if (event->attr.exclude_kernel || event->attr.exclude_user ||
  328. event->attr.exclude_hv || event->attr.exclude_idle) {
  329. pr_debug("ARM performance counters do not support "
  330. "mode exclusion\n");
  331. return -EPERM;
  332. }
  333. /*
  334. * We don't assign an index until we actually place the event onto
  335. * hardware. Use -1 to signify that we haven't decided where to put it
  336. * yet. For SMP systems, each core has it's own PMU so we can't do any
  337. * clever allocation or constraints checking at this point.
  338. */
  339. hwc->idx = -1;
  340. /*
  341. * Store the event encoding into the config_base field. config and
  342. * event_base are unused as the only 2 things we need to know are
  343. * the event mapping and the counter to use. The counter to use is
  344. * also the indx and the config_base is the event type.
  345. */
  346. hwc->config_base = (unsigned long)mapping;
  347. hwc->config = 0;
  348. hwc->event_base = 0;
  349. if (!hwc->sample_period) {
  350. hwc->sample_period = armpmu->max_period;
  351. hwc->last_period = hwc->sample_period;
  352. atomic64_set(&hwc->period_left, hwc->sample_period);
  353. }
  354. err = 0;
  355. if (event->group_leader != event) {
  356. err = validate_group(event);
  357. if (err)
  358. return -EINVAL;
  359. }
  360. return err;
  361. }
  362. const struct pmu *
  363. hw_perf_event_init(struct perf_event *event)
  364. {
  365. int err = 0;
  366. if (!armpmu)
  367. return ERR_PTR(-ENODEV);
  368. event->destroy = hw_perf_event_destroy;
  369. if (!atomic_inc_not_zero(&active_events)) {
  370. if (atomic_read(&active_events) > perf_max_events) {
  371. atomic_dec(&active_events);
  372. return ERR_PTR(-ENOSPC);
  373. }
  374. mutex_lock(&pmu_reserve_mutex);
  375. if (atomic_read(&active_events) == 0) {
  376. err = armpmu_reserve_hardware();
  377. }
  378. if (!err)
  379. atomic_inc(&active_events);
  380. mutex_unlock(&pmu_reserve_mutex);
  381. }
  382. if (err)
  383. return ERR_PTR(err);
  384. err = __hw_perf_event_init(event);
  385. if (err)
  386. hw_perf_event_destroy(event);
  387. return err ? ERR_PTR(err) : &pmu;
  388. }
  389. void
  390. hw_perf_enable(void)
  391. {
  392. /* Enable all of the perf events on hardware. */
  393. int idx;
  394. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  395. if (!armpmu)
  396. return;
  397. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  398. struct perf_event *event = cpuc->events[idx];
  399. if (!event)
  400. continue;
  401. armpmu->enable(&event->hw, idx);
  402. }
  403. armpmu->start();
  404. }
  405. void
  406. hw_perf_disable(void)
  407. {
  408. if (armpmu)
  409. armpmu->stop();
  410. }
  411. /*
  412. * ARMv6 Performance counter handling code.
  413. *
  414. * ARMv6 has 2 configurable performance counters and a single cycle counter.
  415. * They all share a single reset bit but can be written to zero so we can use
  416. * that for a reset.
  417. *
  418. * The counters can't be individually enabled or disabled so when we remove
  419. * one event and replace it with another we could get spurious counts from the
  420. * wrong event. However, we can take advantage of the fact that the
  421. * performance counters can export events to the event bus, and the event bus
  422. * itself can be monitored. This requires that we *don't* export the events to
  423. * the event bus. The procedure for disabling a configurable counter is:
  424. * - change the counter to count the ETMEXTOUT[0] signal (0x20). This
  425. * effectively stops the counter from counting.
  426. * - disable the counter's interrupt generation (each counter has it's
  427. * own interrupt enable bit).
  428. * Once stopped, the counter value can be written as 0 to reset.
  429. *
  430. * To enable a counter:
  431. * - enable the counter's interrupt generation.
  432. * - set the new event type.
  433. *
  434. * Note: the dedicated cycle counter only counts cycles and can't be
  435. * enabled/disabled independently of the others. When we want to disable the
  436. * cycle counter, we have to just disable the interrupt reporting and start
  437. * ignoring that counter. When re-enabling, we have to reset the value and
  438. * enable the interrupt.
  439. */
  440. enum armv6_perf_types {
  441. ARMV6_PERFCTR_ICACHE_MISS = 0x0,
  442. ARMV6_PERFCTR_IBUF_STALL = 0x1,
  443. ARMV6_PERFCTR_DDEP_STALL = 0x2,
  444. ARMV6_PERFCTR_ITLB_MISS = 0x3,
  445. ARMV6_PERFCTR_DTLB_MISS = 0x4,
  446. ARMV6_PERFCTR_BR_EXEC = 0x5,
  447. ARMV6_PERFCTR_BR_MISPREDICT = 0x6,
  448. ARMV6_PERFCTR_INSTR_EXEC = 0x7,
  449. ARMV6_PERFCTR_DCACHE_HIT = 0x9,
  450. ARMV6_PERFCTR_DCACHE_ACCESS = 0xA,
  451. ARMV6_PERFCTR_DCACHE_MISS = 0xB,
  452. ARMV6_PERFCTR_DCACHE_WBACK = 0xC,
  453. ARMV6_PERFCTR_SW_PC_CHANGE = 0xD,
  454. ARMV6_PERFCTR_MAIN_TLB_MISS = 0xF,
  455. ARMV6_PERFCTR_EXPL_D_ACCESS = 0x10,
  456. ARMV6_PERFCTR_LSU_FULL_STALL = 0x11,
  457. ARMV6_PERFCTR_WBUF_DRAINED = 0x12,
  458. ARMV6_PERFCTR_CPU_CYCLES = 0xFF,
  459. ARMV6_PERFCTR_NOP = 0x20,
  460. };
  461. enum armv6_counters {
  462. ARMV6_CYCLE_COUNTER = 1,
  463. ARMV6_COUNTER0,
  464. ARMV6_COUNTER1,
  465. };
  466. /*
  467. * The hardware events that we support. We do support cache operations but
  468. * we have harvard caches and no way to combine instruction and data
  469. * accesses/misses in hardware.
  470. */
  471. static const unsigned armv6_perf_map[PERF_COUNT_HW_MAX] = {
  472. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6_PERFCTR_CPU_CYCLES,
  473. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6_PERFCTR_INSTR_EXEC,
  474. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  475. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  476. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6_PERFCTR_BR_EXEC,
  477. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6_PERFCTR_BR_MISPREDICT,
  478. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  479. };
  480. static const unsigned armv6_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  481. [PERF_COUNT_HW_CACHE_OP_MAX]
  482. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  483. [C(L1D)] = {
  484. /*
  485. * The performance counters don't differentiate between read
  486. * and write accesses/misses so this isn't strictly correct,
  487. * but it's the best we can do. Writes and reads get
  488. * combined.
  489. */
  490. [C(OP_READ)] = {
  491. [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  492. [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  493. },
  494. [C(OP_WRITE)] = {
  495. [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  496. [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  497. },
  498. [C(OP_PREFETCH)] = {
  499. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  500. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  501. },
  502. },
  503. [C(L1I)] = {
  504. [C(OP_READ)] = {
  505. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  506. [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
  507. },
  508. [C(OP_WRITE)] = {
  509. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  510. [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
  511. },
  512. [C(OP_PREFETCH)] = {
  513. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  514. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  515. },
  516. },
  517. [C(LL)] = {
  518. [C(OP_READ)] = {
  519. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  520. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  521. },
  522. [C(OP_WRITE)] = {
  523. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  524. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  525. },
  526. [C(OP_PREFETCH)] = {
  527. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  528. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  529. },
  530. },
  531. [C(DTLB)] = {
  532. /*
  533. * The ARM performance counters can count micro DTLB misses,
  534. * micro ITLB misses and main TLB misses. There isn't an event
  535. * for TLB misses, so use the micro misses here and if users
  536. * want the main TLB misses they can use a raw counter.
  537. */
  538. [C(OP_READ)] = {
  539. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  540. [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  541. },
  542. [C(OP_WRITE)] = {
  543. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  544. [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  545. },
  546. [C(OP_PREFETCH)] = {
  547. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  548. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  549. },
  550. },
  551. [C(ITLB)] = {
  552. [C(OP_READ)] = {
  553. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  554. [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  555. },
  556. [C(OP_WRITE)] = {
  557. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  558. [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  559. },
  560. [C(OP_PREFETCH)] = {
  561. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  562. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  563. },
  564. },
  565. [C(BPU)] = {
  566. [C(OP_READ)] = {
  567. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  568. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  569. },
  570. [C(OP_WRITE)] = {
  571. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  572. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  573. },
  574. [C(OP_PREFETCH)] = {
  575. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  576. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  577. },
  578. },
  579. };
  580. enum armv6mpcore_perf_types {
  581. ARMV6MPCORE_PERFCTR_ICACHE_MISS = 0x0,
  582. ARMV6MPCORE_PERFCTR_IBUF_STALL = 0x1,
  583. ARMV6MPCORE_PERFCTR_DDEP_STALL = 0x2,
  584. ARMV6MPCORE_PERFCTR_ITLB_MISS = 0x3,
  585. ARMV6MPCORE_PERFCTR_DTLB_MISS = 0x4,
  586. ARMV6MPCORE_PERFCTR_BR_EXEC = 0x5,
  587. ARMV6MPCORE_PERFCTR_BR_NOTPREDICT = 0x6,
  588. ARMV6MPCORE_PERFCTR_BR_MISPREDICT = 0x7,
  589. ARMV6MPCORE_PERFCTR_INSTR_EXEC = 0x8,
  590. ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS = 0xA,
  591. ARMV6MPCORE_PERFCTR_DCACHE_RDMISS = 0xB,
  592. ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS = 0xC,
  593. ARMV6MPCORE_PERFCTR_DCACHE_WRMISS = 0xD,
  594. ARMV6MPCORE_PERFCTR_DCACHE_EVICTION = 0xE,
  595. ARMV6MPCORE_PERFCTR_SW_PC_CHANGE = 0xF,
  596. ARMV6MPCORE_PERFCTR_MAIN_TLB_MISS = 0x10,
  597. ARMV6MPCORE_PERFCTR_EXPL_MEM_ACCESS = 0x11,
  598. ARMV6MPCORE_PERFCTR_LSU_FULL_STALL = 0x12,
  599. ARMV6MPCORE_PERFCTR_WBUF_DRAINED = 0x13,
  600. ARMV6MPCORE_PERFCTR_CPU_CYCLES = 0xFF,
  601. };
  602. /*
  603. * The hardware events that we support. We do support cache operations but
  604. * we have harvard caches and no way to combine instruction and data
  605. * accesses/misses in hardware.
  606. */
  607. static const unsigned armv6mpcore_perf_map[PERF_COUNT_HW_MAX] = {
  608. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6MPCORE_PERFCTR_CPU_CYCLES,
  609. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_INSTR_EXEC,
  610. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  611. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  612. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_BR_EXEC,
  613. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6MPCORE_PERFCTR_BR_MISPREDICT,
  614. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  615. };
  616. static const unsigned armv6mpcore_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  617. [PERF_COUNT_HW_CACHE_OP_MAX]
  618. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  619. [C(L1D)] = {
  620. [C(OP_READ)] = {
  621. [C(RESULT_ACCESS)] =
  622. ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS,
  623. [C(RESULT_MISS)] =
  624. ARMV6MPCORE_PERFCTR_DCACHE_RDMISS,
  625. },
  626. [C(OP_WRITE)] = {
  627. [C(RESULT_ACCESS)] =
  628. ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS,
  629. [C(RESULT_MISS)] =
  630. ARMV6MPCORE_PERFCTR_DCACHE_WRMISS,
  631. },
  632. [C(OP_PREFETCH)] = {
  633. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  634. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  635. },
  636. },
  637. [C(L1I)] = {
  638. [C(OP_READ)] = {
  639. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  640. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
  641. },
  642. [C(OP_WRITE)] = {
  643. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  644. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
  645. },
  646. [C(OP_PREFETCH)] = {
  647. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  648. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  649. },
  650. },
  651. [C(LL)] = {
  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(DTLB)] = {
  666. /*
  667. * The ARM performance counters can count micro DTLB misses,
  668. * micro ITLB misses and main TLB misses. There isn't an event
  669. * for TLB misses, so use the micro misses here and if users
  670. * want the main TLB misses they can use a raw counter.
  671. */
  672. [C(OP_READ)] = {
  673. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  674. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  675. },
  676. [C(OP_WRITE)] = {
  677. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  678. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  679. },
  680. [C(OP_PREFETCH)] = {
  681. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  682. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  683. },
  684. },
  685. [C(ITLB)] = {
  686. [C(OP_READ)] = {
  687. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  688. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  689. },
  690. [C(OP_WRITE)] = {
  691. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  692. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  693. },
  694. [C(OP_PREFETCH)] = {
  695. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  696. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  697. },
  698. },
  699. [C(BPU)] = {
  700. [C(OP_READ)] = {
  701. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  702. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  703. },
  704. [C(OP_WRITE)] = {
  705. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  706. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  707. },
  708. [C(OP_PREFETCH)] = {
  709. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  710. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  711. },
  712. },
  713. };
  714. static inline unsigned long
  715. armv6_pmcr_read(void)
  716. {
  717. u32 val;
  718. asm volatile("mrc p15, 0, %0, c15, c12, 0" : "=r"(val));
  719. return val;
  720. }
  721. static inline void
  722. armv6_pmcr_write(unsigned long val)
  723. {
  724. asm volatile("mcr p15, 0, %0, c15, c12, 0" : : "r"(val));
  725. }
  726. #define ARMV6_PMCR_ENABLE (1 << 0)
  727. #define ARMV6_PMCR_CTR01_RESET (1 << 1)
  728. #define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
  729. #define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
  730. #define ARMV6_PMCR_COUNT0_IEN (1 << 4)
  731. #define ARMV6_PMCR_COUNT1_IEN (1 << 5)
  732. #define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
  733. #define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
  734. #define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
  735. #define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
  736. #define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
  737. #define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
  738. #define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
  739. #define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
  740. #define ARMV6_PMCR_OVERFLOWED_MASK \
  741. (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
  742. ARMV6_PMCR_CCOUNT_OVERFLOW)
  743. static inline int
  744. armv6_pmcr_has_overflowed(unsigned long pmcr)
  745. {
  746. return (pmcr & ARMV6_PMCR_OVERFLOWED_MASK);
  747. }
  748. static inline int
  749. armv6_pmcr_counter_has_overflowed(unsigned long pmcr,
  750. enum armv6_counters counter)
  751. {
  752. int ret = 0;
  753. if (ARMV6_CYCLE_COUNTER == counter)
  754. ret = pmcr & ARMV6_PMCR_CCOUNT_OVERFLOW;
  755. else if (ARMV6_COUNTER0 == counter)
  756. ret = pmcr & ARMV6_PMCR_COUNT0_OVERFLOW;
  757. else if (ARMV6_COUNTER1 == counter)
  758. ret = pmcr & ARMV6_PMCR_COUNT1_OVERFLOW;
  759. else
  760. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  761. return ret;
  762. }
  763. static inline u32
  764. armv6pmu_read_counter(int counter)
  765. {
  766. unsigned long value = 0;
  767. if (ARMV6_CYCLE_COUNTER == counter)
  768. asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value));
  769. else if (ARMV6_COUNTER0 == counter)
  770. asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value));
  771. else if (ARMV6_COUNTER1 == counter)
  772. asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value));
  773. else
  774. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  775. return value;
  776. }
  777. static inline void
  778. armv6pmu_write_counter(int counter,
  779. u32 value)
  780. {
  781. if (ARMV6_CYCLE_COUNTER == counter)
  782. asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value));
  783. else if (ARMV6_COUNTER0 == counter)
  784. asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value));
  785. else if (ARMV6_COUNTER1 == counter)
  786. asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value));
  787. else
  788. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  789. }
  790. void
  791. armv6pmu_enable_event(struct hw_perf_event *hwc,
  792. int idx)
  793. {
  794. unsigned long val, mask, evt, flags;
  795. if (ARMV6_CYCLE_COUNTER == idx) {
  796. mask = 0;
  797. evt = ARMV6_PMCR_CCOUNT_IEN;
  798. } else if (ARMV6_COUNTER0 == idx) {
  799. mask = ARMV6_PMCR_EVT_COUNT0_MASK;
  800. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT0_SHIFT) |
  801. ARMV6_PMCR_COUNT0_IEN;
  802. } else if (ARMV6_COUNTER1 == idx) {
  803. mask = ARMV6_PMCR_EVT_COUNT1_MASK;
  804. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT1_SHIFT) |
  805. ARMV6_PMCR_COUNT1_IEN;
  806. } else {
  807. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  808. return;
  809. }
  810. /*
  811. * Mask out the current event and set the counter to count the event
  812. * that we're interested in.
  813. */
  814. spin_lock_irqsave(&pmu_lock, flags);
  815. val = armv6_pmcr_read();
  816. val &= ~mask;
  817. val |= evt;
  818. armv6_pmcr_write(val);
  819. spin_unlock_irqrestore(&pmu_lock, flags);
  820. }
  821. static irqreturn_t
  822. armv6pmu_handle_irq(int irq_num,
  823. void *dev)
  824. {
  825. unsigned long pmcr = armv6_pmcr_read();
  826. struct perf_sample_data data;
  827. struct cpu_hw_events *cpuc;
  828. struct pt_regs *regs;
  829. int idx;
  830. if (!armv6_pmcr_has_overflowed(pmcr))
  831. return IRQ_NONE;
  832. regs = get_irq_regs();
  833. /*
  834. * The interrupts are cleared by writing the overflow flags back to
  835. * the control register. All of the other bits don't have any effect
  836. * if they are rewritten, so write the whole value back.
  837. */
  838. armv6_pmcr_write(pmcr);
  839. data.addr = 0;
  840. cpuc = &__get_cpu_var(cpu_hw_events);
  841. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  842. struct perf_event *event = cpuc->events[idx];
  843. struct hw_perf_event *hwc;
  844. if (!test_bit(idx, cpuc->active_mask))
  845. continue;
  846. /*
  847. * We have a single interrupt for all counters. Check that
  848. * each counter has overflowed before we process it.
  849. */
  850. if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
  851. continue;
  852. hwc = &event->hw;
  853. armpmu_event_update(event, hwc, idx);
  854. data.period = event->hw.last_period;
  855. if (!armpmu_event_set_period(event, hwc, idx))
  856. continue;
  857. if (perf_event_overflow(event, 0, &data, regs))
  858. armpmu->disable(hwc, idx);
  859. }
  860. /*
  861. * Handle the pending perf events.
  862. *
  863. * Note: this call *must* be run with interrupts enabled. For
  864. * platforms that can have the PMU interrupts raised as a PMI, this
  865. * will not work.
  866. */
  867. perf_event_do_pending();
  868. return IRQ_HANDLED;
  869. }
  870. static void
  871. armv6pmu_start(void)
  872. {
  873. unsigned long flags, val;
  874. spin_lock_irqsave(&pmu_lock, flags);
  875. val = armv6_pmcr_read();
  876. val |= ARMV6_PMCR_ENABLE;
  877. armv6_pmcr_write(val);
  878. spin_unlock_irqrestore(&pmu_lock, flags);
  879. }
  880. void
  881. armv6pmu_stop(void)
  882. {
  883. unsigned long flags, val;
  884. spin_lock_irqsave(&pmu_lock, flags);
  885. val = armv6_pmcr_read();
  886. val &= ~ARMV6_PMCR_ENABLE;
  887. armv6_pmcr_write(val);
  888. spin_unlock_irqrestore(&pmu_lock, flags);
  889. }
  890. static inline int
  891. armv6pmu_event_map(int config)
  892. {
  893. int mapping = armv6_perf_map[config];
  894. if (HW_OP_UNSUPPORTED == mapping)
  895. mapping = -EOPNOTSUPP;
  896. return mapping;
  897. }
  898. static inline int
  899. armv6mpcore_pmu_event_map(int config)
  900. {
  901. int mapping = armv6mpcore_perf_map[config];
  902. if (HW_OP_UNSUPPORTED == mapping)
  903. mapping = -EOPNOTSUPP;
  904. return mapping;
  905. }
  906. static u64
  907. armv6pmu_raw_event(u64 config)
  908. {
  909. return config & 0xff;
  910. }
  911. static int
  912. armv6pmu_get_event_idx(struct cpu_hw_events *cpuc,
  913. struct hw_perf_event *event)
  914. {
  915. /* Always place a cycle counter into the cycle counter. */
  916. if (ARMV6_PERFCTR_CPU_CYCLES == event->config_base) {
  917. if (test_and_set_bit(ARMV6_CYCLE_COUNTER, cpuc->used_mask))
  918. return -EAGAIN;
  919. return ARMV6_CYCLE_COUNTER;
  920. } else {
  921. /*
  922. * For anything other than a cycle counter, try and use
  923. * counter0 and counter1.
  924. */
  925. if (!test_and_set_bit(ARMV6_COUNTER1, cpuc->used_mask)) {
  926. return ARMV6_COUNTER1;
  927. }
  928. if (!test_and_set_bit(ARMV6_COUNTER0, cpuc->used_mask)) {
  929. return ARMV6_COUNTER0;
  930. }
  931. /* The counters are all in use. */
  932. return -EAGAIN;
  933. }
  934. }
  935. static void
  936. armv6pmu_disable_event(struct hw_perf_event *hwc,
  937. int idx)
  938. {
  939. unsigned long val, mask, evt, flags;
  940. if (ARMV6_CYCLE_COUNTER == idx) {
  941. mask = ARMV6_PMCR_CCOUNT_IEN;
  942. evt = 0;
  943. } else if (ARMV6_COUNTER0 == idx) {
  944. mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
  945. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
  946. } else if (ARMV6_COUNTER1 == idx) {
  947. mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
  948. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
  949. } else {
  950. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  951. return;
  952. }
  953. /*
  954. * Mask out the current event and set the counter to count the number
  955. * of ETM bus signal assertion cycles. The external reporting should
  956. * be disabled and so this should never increment.
  957. */
  958. spin_lock_irqsave(&pmu_lock, flags);
  959. val = armv6_pmcr_read();
  960. val &= ~mask;
  961. val |= evt;
  962. armv6_pmcr_write(val);
  963. spin_unlock_irqrestore(&pmu_lock, flags);
  964. }
  965. static void
  966. armv6mpcore_pmu_disable_event(struct hw_perf_event *hwc,
  967. int idx)
  968. {
  969. unsigned long val, mask, flags, evt = 0;
  970. if (ARMV6_CYCLE_COUNTER == idx) {
  971. mask = ARMV6_PMCR_CCOUNT_IEN;
  972. } else if (ARMV6_COUNTER0 == idx) {
  973. mask = ARMV6_PMCR_COUNT0_IEN;
  974. } else if (ARMV6_COUNTER1 == idx) {
  975. mask = ARMV6_PMCR_COUNT1_IEN;
  976. } else {
  977. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  978. return;
  979. }
  980. /*
  981. * Unlike UP ARMv6, we don't have a way of stopping the counters. We
  982. * simply disable the interrupt reporting.
  983. */
  984. spin_lock_irqsave(&pmu_lock, flags);
  985. val = armv6_pmcr_read();
  986. val &= ~mask;
  987. val |= evt;
  988. armv6_pmcr_write(val);
  989. spin_unlock_irqrestore(&pmu_lock, flags);
  990. }
  991. static const struct arm_pmu armv6pmu = {
  992. .name = "v6",
  993. .handle_irq = armv6pmu_handle_irq,
  994. .enable = armv6pmu_enable_event,
  995. .disable = armv6pmu_disable_event,
  996. .event_map = armv6pmu_event_map,
  997. .raw_event = armv6pmu_raw_event,
  998. .read_counter = armv6pmu_read_counter,
  999. .write_counter = armv6pmu_write_counter,
  1000. .get_event_idx = armv6pmu_get_event_idx,
  1001. .start = armv6pmu_start,
  1002. .stop = armv6pmu_stop,
  1003. .num_events = 3,
  1004. .max_period = (1LLU << 32) - 1,
  1005. };
  1006. /*
  1007. * ARMv6mpcore is almost identical to single core ARMv6 with the exception
  1008. * that some of the events have different enumerations and that there is no
  1009. * *hack* to stop the programmable counters. To stop the counters we simply
  1010. * disable the interrupt reporting and update the event. When unthrottling we
  1011. * reset the period and enable the interrupt reporting.
  1012. */
  1013. static const struct arm_pmu armv6mpcore_pmu = {
  1014. .name = "v6mpcore",
  1015. .handle_irq = armv6pmu_handle_irq,
  1016. .enable = armv6pmu_enable_event,
  1017. .disable = armv6mpcore_pmu_disable_event,
  1018. .event_map = armv6mpcore_pmu_event_map,
  1019. .raw_event = armv6pmu_raw_event,
  1020. .read_counter = armv6pmu_read_counter,
  1021. .write_counter = armv6pmu_write_counter,
  1022. .get_event_idx = armv6pmu_get_event_idx,
  1023. .start = armv6pmu_start,
  1024. .stop = armv6pmu_stop,
  1025. .num_events = 3,
  1026. .max_period = (1LLU << 32) - 1,
  1027. };
  1028. /*
  1029. * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
  1030. *
  1031. * Copied from ARMv6 code, with the low level code inspired
  1032. * by the ARMv7 Oprofile code.
  1033. *
  1034. * Cortex-A8 has up to 4 configurable performance counters and
  1035. * a single cycle counter.
  1036. * Cortex-A9 has up to 31 configurable performance counters and
  1037. * a single cycle counter.
  1038. *
  1039. * All counters can be enabled/disabled and IRQ masked separately. The cycle
  1040. * counter and all 4 performance counters together can be reset separately.
  1041. */
  1042. #define ARMV7_PMU_CORTEX_A8_NAME "ARMv7 Cortex-A8"
  1043. #define ARMV7_PMU_CORTEX_A9_NAME "ARMv7 Cortex-A9"
  1044. /* Common ARMv7 event types */
  1045. enum armv7_perf_types {
  1046. ARMV7_PERFCTR_PMNC_SW_INCR = 0x00,
  1047. ARMV7_PERFCTR_IFETCH_MISS = 0x01,
  1048. ARMV7_PERFCTR_ITLB_MISS = 0x02,
  1049. ARMV7_PERFCTR_DCACHE_REFILL = 0x03,
  1050. ARMV7_PERFCTR_DCACHE_ACCESS = 0x04,
  1051. ARMV7_PERFCTR_DTLB_REFILL = 0x05,
  1052. ARMV7_PERFCTR_DREAD = 0x06,
  1053. ARMV7_PERFCTR_DWRITE = 0x07,
  1054. ARMV7_PERFCTR_EXC_TAKEN = 0x09,
  1055. ARMV7_PERFCTR_EXC_EXECUTED = 0x0A,
  1056. ARMV7_PERFCTR_CID_WRITE = 0x0B,
  1057. /* ARMV7_PERFCTR_PC_WRITE is equivalent to HW_BRANCH_INSTRUCTIONS.
  1058. * It counts:
  1059. * - all branch instructions,
  1060. * - instructions that explicitly write the PC,
  1061. * - exception generating instructions.
  1062. */
  1063. ARMV7_PERFCTR_PC_WRITE = 0x0C,
  1064. ARMV7_PERFCTR_PC_IMM_BRANCH = 0x0D,
  1065. ARMV7_PERFCTR_UNALIGNED_ACCESS = 0x0F,
  1066. ARMV7_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
  1067. ARMV7_PERFCTR_CLOCK_CYCLES = 0x11,
  1068. ARMV7_PERFCTR_PC_BRANCH_MIS_USED = 0x12,
  1069. ARMV7_PERFCTR_CPU_CYCLES = 0xFF
  1070. };
  1071. /* ARMv7 Cortex-A8 specific event types */
  1072. enum armv7_a8_perf_types {
  1073. ARMV7_PERFCTR_INSTR_EXECUTED = 0x08,
  1074. ARMV7_PERFCTR_PC_PROC_RETURN = 0x0E,
  1075. ARMV7_PERFCTR_WRITE_BUFFER_FULL = 0x40,
  1076. ARMV7_PERFCTR_L2_STORE_MERGED = 0x41,
  1077. ARMV7_PERFCTR_L2_STORE_BUFF = 0x42,
  1078. ARMV7_PERFCTR_L2_ACCESS = 0x43,
  1079. ARMV7_PERFCTR_L2_CACH_MISS = 0x44,
  1080. ARMV7_PERFCTR_AXI_READ_CYCLES = 0x45,
  1081. ARMV7_PERFCTR_AXI_WRITE_CYCLES = 0x46,
  1082. ARMV7_PERFCTR_MEMORY_REPLAY = 0x47,
  1083. ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY = 0x48,
  1084. ARMV7_PERFCTR_L1_DATA_MISS = 0x49,
  1085. ARMV7_PERFCTR_L1_INST_MISS = 0x4A,
  1086. ARMV7_PERFCTR_L1_DATA_COLORING = 0x4B,
  1087. ARMV7_PERFCTR_L1_NEON_DATA = 0x4C,
  1088. ARMV7_PERFCTR_L1_NEON_CACH_DATA = 0x4D,
  1089. ARMV7_PERFCTR_L2_NEON = 0x4E,
  1090. ARMV7_PERFCTR_L2_NEON_HIT = 0x4F,
  1091. ARMV7_PERFCTR_L1_INST = 0x50,
  1092. ARMV7_PERFCTR_PC_RETURN_MIS_PRED = 0x51,
  1093. ARMV7_PERFCTR_PC_BRANCH_FAILED = 0x52,
  1094. ARMV7_PERFCTR_PC_BRANCH_TAKEN = 0x53,
  1095. ARMV7_PERFCTR_PC_BRANCH_EXECUTED = 0x54,
  1096. ARMV7_PERFCTR_OP_EXECUTED = 0x55,
  1097. ARMV7_PERFCTR_CYCLES_INST_STALL = 0x56,
  1098. ARMV7_PERFCTR_CYCLES_INST = 0x57,
  1099. ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL = 0x58,
  1100. ARMV7_PERFCTR_CYCLES_NEON_INST_STALL = 0x59,
  1101. ARMV7_PERFCTR_NEON_CYCLES = 0x5A,
  1102. ARMV7_PERFCTR_PMU0_EVENTS = 0x70,
  1103. ARMV7_PERFCTR_PMU1_EVENTS = 0x71,
  1104. ARMV7_PERFCTR_PMU_EVENTS = 0x72,
  1105. };
  1106. /* ARMv7 Cortex-A9 specific event types */
  1107. enum armv7_a9_perf_types {
  1108. ARMV7_PERFCTR_JAVA_HW_BYTECODE_EXEC = 0x40,
  1109. ARMV7_PERFCTR_JAVA_SW_BYTECODE_EXEC = 0x41,
  1110. ARMV7_PERFCTR_JAZELLE_BRANCH_EXEC = 0x42,
  1111. ARMV7_PERFCTR_COHERENT_LINE_MISS = 0x50,
  1112. ARMV7_PERFCTR_COHERENT_LINE_HIT = 0x51,
  1113. ARMV7_PERFCTR_ICACHE_DEP_STALL_CYCLES = 0x60,
  1114. ARMV7_PERFCTR_DCACHE_DEP_STALL_CYCLES = 0x61,
  1115. ARMV7_PERFCTR_TLB_MISS_DEP_STALL_CYCLES = 0x62,
  1116. ARMV7_PERFCTR_STREX_EXECUTED_PASSED = 0x63,
  1117. ARMV7_PERFCTR_STREX_EXECUTED_FAILED = 0x64,
  1118. ARMV7_PERFCTR_DATA_EVICTION = 0x65,
  1119. ARMV7_PERFCTR_ISSUE_STAGE_NO_INST = 0x66,
  1120. ARMV7_PERFCTR_ISSUE_STAGE_EMPTY = 0x67,
  1121. ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE = 0x68,
  1122. ARMV7_PERFCTR_PREDICTABLE_FUNCT_RETURNS = 0x6E,
  1123. ARMV7_PERFCTR_MAIN_UNIT_EXECUTED_INST = 0x70,
  1124. ARMV7_PERFCTR_SECOND_UNIT_EXECUTED_INST = 0x71,
  1125. ARMV7_PERFCTR_LD_ST_UNIT_EXECUTED_INST = 0x72,
  1126. ARMV7_PERFCTR_FP_EXECUTED_INST = 0x73,
  1127. ARMV7_PERFCTR_NEON_EXECUTED_INST = 0x74,
  1128. ARMV7_PERFCTR_PLD_FULL_DEP_STALL_CYCLES = 0x80,
  1129. ARMV7_PERFCTR_DATA_WR_DEP_STALL_CYCLES = 0x81,
  1130. ARMV7_PERFCTR_ITLB_MISS_DEP_STALL_CYCLES = 0x82,
  1131. ARMV7_PERFCTR_DTLB_MISS_DEP_STALL_CYCLES = 0x83,
  1132. ARMV7_PERFCTR_MICRO_ITLB_MISS_DEP_STALL_CYCLES = 0x84,
  1133. ARMV7_PERFCTR_MICRO_DTLB_MISS_DEP_STALL_CYCLES = 0x85,
  1134. ARMV7_PERFCTR_DMB_DEP_STALL_CYCLES = 0x86,
  1135. ARMV7_PERFCTR_INTGR_CLK_ENABLED_CYCLES = 0x8A,
  1136. ARMV7_PERFCTR_DATA_ENGINE_CLK_EN_CYCLES = 0x8B,
  1137. ARMV7_PERFCTR_ISB_INST = 0x90,
  1138. ARMV7_PERFCTR_DSB_INST = 0x91,
  1139. ARMV7_PERFCTR_DMB_INST = 0x92,
  1140. ARMV7_PERFCTR_EXT_INTERRUPTS = 0x93,
  1141. ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_COMPLETED = 0xA0,
  1142. ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_SKIPPED = 0xA1,
  1143. ARMV7_PERFCTR_PLE_FIFO_FLUSH = 0xA2,
  1144. ARMV7_PERFCTR_PLE_RQST_COMPLETED = 0xA3,
  1145. ARMV7_PERFCTR_PLE_FIFO_OVERFLOW = 0xA4,
  1146. ARMV7_PERFCTR_PLE_RQST_PROG = 0xA5
  1147. };
  1148. /*
  1149. * Cortex-A8 HW events mapping
  1150. *
  1151. * The hardware events that we support. We do support cache operations but
  1152. * we have harvard caches and no way to combine instruction and data
  1153. * accesses/misses in hardware.
  1154. */
  1155. static const unsigned armv7_a8_perf_map[PERF_COUNT_HW_MAX] = {
  1156. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  1157. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  1158. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  1159. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  1160. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  1161. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1162. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
  1163. };
  1164. static const unsigned armv7_a8_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  1165. [PERF_COUNT_HW_CACHE_OP_MAX]
  1166. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1167. [C(L1D)] = {
  1168. /*
  1169. * The performance counters don't differentiate between read
  1170. * and write accesses/misses so this isn't strictly correct,
  1171. * but it's the best we can do. Writes and reads get
  1172. * combined.
  1173. */
  1174. [C(OP_READ)] = {
  1175. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1176. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1177. },
  1178. [C(OP_WRITE)] = {
  1179. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1180. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1181. },
  1182. [C(OP_PREFETCH)] = {
  1183. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1184. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1185. },
  1186. },
  1187. [C(L1I)] = {
  1188. [C(OP_READ)] = {
  1189. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
  1190. [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
  1191. },
  1192. [C(OP_WRITE)] = {
  1193. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
  1194. [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
  1195. },
  1196. [C(OP_PREFETCH)] = {
  1197. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1198. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1199. },
  1200. },
  1201. [C(LL)] = {
  1202. [C(OP_READ)] = {
  1203. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
  1204. [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
  1205. },
  1206. [C(OP_WRITE)] = {
  1207. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
  1208. [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
  1209. },
  1210. [C(OP_PREFETCH)] = {
  1211. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1212. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1213. },
  1214. },
  1215. [C(DTLB)] = {
  1216. /*
  1217. * Only ITLB misses and DTLB refills are supported.
  1218. * If users want the DTLB refills misses a raw counter
  1219. * must be used.
  1220. */
  1221. [C(OP_READ)] = {
  1222. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1223. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1224. },
  1225. [C(OP_WRITE)] = {
  1226. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1227. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1228. },
  1229. [C(OP_PREFETCH)] = {
  1230. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1231. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1232. },
  1233. },
  1234. [C(ITLB)] = {
  1235. [C(OP_READ)] = {
  1236. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1237. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1238. },
  1239. [C(OP_WRITE)] = {
  1240. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1241. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1242. },
  1243. [C(OP_PREFETCH)] = {
  1244. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1245. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1246. },
  1247. },
  1248. [C(BPU)] = {
  1249. [C(OP_READ)] = {
  1250. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1251. [C(RESULT_MISS)]
  1252. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1253. },
  1254. [C(OP_WRITE)] = {
  1255. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1256. [C(RESULT_MISS)]
  1257. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1258. },
  1259. [C(OP_PREFETCH)] = {
  1260. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1261. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1262. },
  1263. },
  1264. };
  1265. /*
  1266. * Cortex-A9 HW events mapping
  1267. */
  1268. static const unsigned armv7_a9_perf_map[PERF_COUNT_HW_MAX] = {
  1269. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  1270. [PERF_COUNT_HW_INSTRUCTIONS] =
  1271. ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE,
  1272. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_COHERENT_LINE_HIT,
  1273. [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_COHERENT_LINE_MISS,
  1274. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  1275. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1276. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
  1277. };
  1278. static const unsigned armv7_a9_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  1279. [PERF_COUNT_HW_CACHE_OP_MAX]
  1280. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1281. [C(L1D)] = {
  1282. /*
  1283. * The performance counters don't differentiate between read
  1284. * and write accesses/misses so this isn't strictly correct,
  1285. * but it's the best we can do. Writes and reads get
  1286. * combined.
  1287. */
  1288. [C(OP_READ)] = {
  1289. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1290. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1291. },
  1292. [C(OP_WRITE)] = {
  1293. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1294. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1295. },
  1296. [C(OP_PREFETCH)] = {
  1297. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1298. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1299. },
  1300. },
  1301. [C(L1I)] = {
  1302. [C(OP_READ)] = {
  1303. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1304. [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
  1305. },
  1306. [C(OP_WRITE)] = {
  1307. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1308. [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
  1309. },
  1310. [C(OP_PREFETCH)] = {
  1311. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1312. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1313. },
  1314. },
  1315. [C(LL)] = {
  1316. [C(OP_READ)] = {
  1317. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1318. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1319. },
  1320. [C(OP_WRITE)] = {
  1321. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1322. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1323. },
  1324. [C(OP_PREFETCH)] = {
  1325. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1326. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1327. },
  1328. },
  1329. [C(DTLB)] = {
  1330. /*
  1331. * Only ITLB misses and DTLB refills are supported.
  1332. * If users want the DTLB refills misses a raw counter
  1333. * must be used.
  1334. */
  1335. [C(OP_READ)] = {
  1336. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1337. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1338. },
  1339. [C(OP_WRITE)] = {
  1340. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1341. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1342. },
  1343. [C(OP_PREFETCH)] = {
  1344. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1345. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1346. },
  1347. },
  1348. [C(ITLB)] = {
  1349. [C(OP_READ)] = {
  1350. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1351. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1352. },
  1353. [C(OP_WRITE)] = {
  1354. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1355. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1356. },
  1357. [C(OP_PREFETCH)] = {
  1358. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1359. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1360. },
  1361. },
  1362. [C(BPU)] = {
  1363. [C(OP_READ)] = {
  1364. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1365. [C(RESULT_MISS)]
  1366. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1367. },
  1368. [C(OP_WRITE)] = {
  1369. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1370. [C(RESULT_MISS)]
  1371. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1372. },
  1373. [C(OP_PREFETCH)] = {
  1374. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1375. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1376. },
  1377. },
  1378. };
  1379. /*
  1380. * Perf Events counters
  1381. */
  1382. enum armv7_counters {
  1383. ARMV7_CYCLE_COUNTER = 1, /* Cycle counter */
  1384. ARMV7_COUNTER0 = 2, /* First event counter */
  1385. };
  1386. /*
  1387. * The cycle counter is ARMV7_CYCLE_COUNTER.
  1388. * The first event counter is ARMV7_COUNTER0.
  1389. * The last event counter is (ARMV7_COUNTER0 + armpmu->num_events - 1).
  1390. */
  1391. #define ARMV7_COUNTER_LAST (ARMV7_COUNTER0 + armpmu->num_events - 1)
  1392. /*
  1393. * ARMv7 low level PMNC access
  1394. */
  1395. /*
  1396. * Per-CPU PMNC: config reg
  1397. */
  1398. #define ARMV7_PMNC_E (1 << 0) /* Enable all counters */
  1399. #define ARMV7_PMNC_P (1 << 1) /* Reset all counters */
  1400. #define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */
  1401. #define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */
  1402. #define ARMV7_PMNC_X (1 << 4) /* Export to ETM */
  1403. #define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
  1404. #define ARMV7_PMNC_N_SHIFT 11 /* Number of counters supported */
  1405. #define ARMV7_PMNC_N_MASK 0x1f
  1406. #define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */
  1407. /*
  1408. * Available counters
  1409. */
  1410. #define ARMV7_CNT0 0 /* First event counter */
  1411. #define ARMV7_CCNT 31 /* Cycle counter */
  1412. /* Perf Event to low level counters mapping */
  1413. #define ARMV7_EVENT_CNT_TO_CNTx (ARMV7_COUNTER0 - ARMV7_CNT0)
  1414. /*
  1415. * CNTENS: counters enable reg
  1416. */
  1417. #define ARMV7_CNTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1418. #define ARMV7_CNTENS_C (1 << ARMV7_CCNT)
  1419. /*
  1420. * CNTENC: counters disable reg
  1421. */
  1422. #define ARMV7_CNTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1423. #define ARMV7_CNTENC_C (1 << ARMV7_CCNT)
  1424. /*
  1425. * INTENS: counters overflow interrupt enable reg
  1426. */
  1427. #define ARMV7_INTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1428. #define ARMV7_INTENS_C (1 << ARMV7_CCNT)
  1429. /*
  1430. * INTENC: counters overflow interrupt disable reg
  1431. */
  1432. #define ARMV7_INTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1433. #define ARMV7_INTENC_C (1 << ARMV7_CCNT)
  1434. /*
  1435. * EVTSEL: Event selection reg
  1436. */
  1437. #define ARMV7_EVTSEL_MASK 0x7f /* Mask for writable bits */
  1438. /*
  1439. * SELECT: Counter selection reg
  1440. */
  1441. #define ARMV7_SELECT_MASK 0x1f /* Mask for writable bits */
  1442. /*
  1443. * FLAG: counters overflow flag status reg
  1444. */
  1445. #define ARMV7_FLAG_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1446. #define ARMV7_FLAG_C (1 << ARMV7_CCNT)
  1447. #define ARMV7_FLAG_MASK 0xffffffff /* Mask for writable bits */
  1448. #define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK
  1449. static inline unsigned long armv7_pmnc_read(void)
  1450. {
  1451. u32 val;
  1452. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val));
  1453. return val;
  1454. }
  1455. static inline void armv7_pmnc_write(unsigned long val)
  1456. {
  1457. val &= ARMV7_PMNC_MASK;
  1458. asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val));
  1459. }
  1460. static inline int armv7_pmnc_has_overflowed(unsigned long pmnc)
  1461. {
  1462. return pmnc & ARMV7_OVERFLOWED_MASK;
  1463. }
  1464. static inline int armv7_pmnc_counter_has_overflowed(unsigned long pmnc,
  1465. enum armv7_counters counter)
  1466. {
  1467. int ret;
  1468. if (counter == ARMV7_CYCLE_COUNTER)
  1469. ret = pmnc & ARMV7_FLAG_C;
  1470. else if ((counter >= ARMV7_COUNTER0) && (counter <= ARMV7_COUNTER_LAST))
  1471. ret = pmnc & ARMV7_FLAG_P(counter);
  1472. else
  1473. pr_err("CPU%u checking wrong counter %d overflow status\n",
  1474. smp_processor_id(), counter);
  1475. return ret;
  1476. }
  1477. static inline int armv7_pmnc_select_counter(unsigned int idx)
  1478. {
  1479. u32 val;
  1480. if ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST)) {
  1481. pr_err("CPU%u selecting wrong PMNC counter"
  1482. " %d\n", smp_processor_id(), idx);
  1483. return -1;
  1484. }
  1485. val = (idx - ARMV7_EVENT_CNT_TO_CNTx) & ARMV7_SELECT_MASK;
  1486. asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (val));
  1487. return idx;
  1488. }
  1489. static inline u32 armv7pmu_read_counter(int idx)
  1490. {
  1491. unsigned long value = 0;
  1492. if (idx == ARMV7_CYCLE_COUNTER)
  1493. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value));
  1494. else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
  1495. if (armv7_pmnc_select_counter(idx) == idx)
  1496. asm volatile("mrc p15, 0, %0, c9, c13, 2"
  1497. : "=r" (value));
  1498. } else
  1499. pr_err("CPU%u reading wrong counter %d\n",
  1500. smp_processor_id(), idx);
  1501. return value;
  1502. }
  1503. static inline void armv7pmu_write_counter(int idx, u32 value)
  1504. {
  1505. if (idx == ARMV7_CYCLE_COUNTER)
  1506. asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value));
  1507. else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
  1508. if (armv7_pmnc_select_counter(idx) == idx)
  1509. asm volatile("mcr p15, 0, %0, c9, c13, 2"
  1510. : : "r" (value));
  1511. } else
  1512. pr_err("CPU%u writing wrong counter %d\n",
  1513. smp_processor_id(), idx);
  1514. }
  1515. static inline void armv7_pmnc_write_evtsel(unsigned int idx, u32 val)
  1516. {
  1517. if (armv7_pmnc_select_counter(idx) == idx) {
  1518. val &= ARMV7_EVTSEL_MASK;
  1519. asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val));
  1520. }
  1521. }
  1522. static inline u32 armv7_pmnc_enable_counter(unsigned int idx)
  1523. {
  1524. u32 val;
  1525. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1526. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1527. pr_err("CPU%u enabling wrong PMNC counter"
  1528. " %d\n", smp_processor_id(), idx);
  1529. return -1;
  1530. }
  1531. if (idx == ARMV7_CYCLE_COUNTER)
  1532. val = ARMV7_CNTENS_C;
  1533. else
  1534. val = ARMV7_CNTENS_P(idx);
  1535. asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (val));
  1536. return idx;
  1537. }
  1538. static inline u32 armv7_pmnc_disable_counter(unsigned int idx)
  1539. {
  1540. u32 val;
  1541. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1542. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1543. pr_err("CPU%u disabling wrong PMNC counter"
  1544. " %d\n", smp_processor_id(), idx);
  1545. return -1;
  1546. }
  1547. if (idx == ARMV7_CYCLE_COUNTER)
  1548. val = ARMV7_CNTENC_C;
  1549. else
  1550. val = ARMV7_CNTENC_P(idx);
  1551. asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (val));
  1552. return idx;
  1553. }
  1554. static inline u32 armv7_pmnc_enable_intens(unsigned int idx)
  1555. {
  1556. u32 val;
  1557. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1558. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1559. pr_err("CPU%u enabling wrong PMNC counter"
  1560. " interrupt enable %d\n", smp_processor_id(), idx);
  1561. return -1;
  1562. }
  1563. if (idx == ARMV7_CYCLE_COUNTER)
  1564. val = ARMV7_INTENS_C;
  1565. else
  1566. val = ARMV7_INTENS_P(idx);
  1567. asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (val));
  1568. return idx;
  1569. }
  1570. static inline u32 armv7_pmnc_disable_intens(unsigned int idx)
  1571. {
  1572. u32 val;
  1573. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1574. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1575. pr_err("CPU%u disabling wrong PMNC counter"
  1576. " interrupt enable %d\n", smp_processor_id(), idx);
  1577. return -1;
  1578. }
  1579. if (idx == ARMV7_CYCLE_COUNTER)
  1580. val = ARMV7_INTENC_C;
  1581. else
  1582. val = ARMV7_INTENC_P(idx);
  1583. asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (val));
  1584. return idx;
  1585. }
  1586. static inline u32 armv7_pmnc_getreset_flags(void)
  1587. {
  1588. u32 val;
  1589. /* Read */
  1590. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  1591. /* Write to clear flags */
  1592. val &= ARMV7_FLAG_MASK;
  1593. asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (val));
  1594. return val;
  1595. }
  1596. #ifdef DEBUG
  1597. static void armv7_pmnc_dump_regs(void)
  1598. {
  1599. u32 val;
  1600. unsigned int cnt;
  1601. printk(KERN_INFO "PMNC registers dump:\n");
  1602. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r" (val));
  1603. printk(KERN_INFO "PMNC =0x%08x\n", val);
  1604. asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r" (val));
  1605. printk(KERN_INFO "CNTENS=0x%08x\n", val);
  1606. asm volatile("mrc p15, 0, %0, c9, c14, 1" : "=r" (val));
  1607. printk(KERN_INFO "INTENS=0x%08x\n", val);
  1608. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  1609. printk(KERN_INFO "FLAGS =0x%08x\n", val);
  1610. asm volatile("mrc p15, 0, %0, c9, c12, 5" : "=r" (val));
  1611. printk(KERN_INFO "SELECT=0x%08x\n", val);
  1612. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (val));
  1613. printk(KERN_INFO "CCNT =0x%08x\n", val);
  1614. for (cnt = ARMV7_COUNTER0; cnt < ARMV7_COUNTER_LAST; cnt++) {
  1615. armv7_pmnc_select_counter(cnt);
  1616. asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (val));
  1617. printk(KERN_INFO "CNT[%d] count =0x%08x\n",
  1618. cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
  1619. asm volatile("mrc p15, 0, %0, c9, c13, 1" : "=r" (val));
  1620. printk(KERN_INFO "CNT[%d] evtsel=0x%08x\n",
  1621. cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
  1622. }
  1623. }
  1624. #endif
  1625. void armv7pmu_enable_event(struct hw_perf_event *hwc, int idx)
  1626. {
  1627. unsigned long flags;
  1628. /*
  1629. * Enable counter and interrupt, and set the counter to count
  1630. * the event that we're interested in.
  1631. */
  1632. spin_lock_irqsave(&pmu_lock, flags);
  1633. /*
  1634. * Disable counter
  1635. */
  1636. armv7_pmnc_disable_counter(idx);
  1637. /*
  1638. * Set event (if destined for PMNx counters)
  1639. * We don't need to set the event if it's a cycle count
  1640. */
  1641. if (idx != ARMV7_CYCLE_COUNTER)
  1642. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  1643. /*
  1644. * Enable interrupt for this counter
  1645. */
  1646. armv7_pmnc_enable_intens(idx);
  1647. /*
  1648. * Enable counter
  1649. */
  1650. armv7_pmnc_enable_counter(idx);
  1651. spin_unlock_irqrestore(&pmu_lock, flags);
  1652. }
  1653. static void armv7pmu_disable_event(struct hw_perf_event *hwc, int idx)
  1654. {
  1655. unsigned long flags;
  1656. /*
  1657. * Disable counter and interrupt
  1658. */
  1659. spin_lock_irqsave(&pmu_lock, flags);
  1660. /*
  1661. * Disable counter
  1662. */
  1663. armv7_pmnc_disable_counter(idx);
  1664. /*
  1665. * Disable interrupt for this counter
  1666. */
  1667. armv7_pmnc_disable_intens(idx);
  1668. spin_unlock_irqrestore(&pmu_lock, flags);
  1669. }
  1670. static irqreturn_t armv7pmu_handle_irq(int irq_num, void *dev)
  1671. {
  1672. unsigned long pmnc;
  1673. struct perf_sample_data data;
  1674. struct cpu_hw_events *cpuc;
  1675. struct pt_regs *regs;
  1676. int idx;
  1677. /*
  1678. * Get and reset the IRQ flags
  1679. */
  1680. pmnc = armv7_pmnc_getreset_flags();
  1681. /*
  1682. * Did an overflow occur?
  1683. */
  1684. if (!armv7_pmnc_has_overflowed(pmnc))
  1685. return IRQ_NONE;
  1686. /*
  1687. * Handle the counter(s) overflow(s)
  1688. */
  1689. regs = get_irq_regs();
  1690. data.addr = 0;
  1691. cpuc = &__get_cpu_var(cpu_hw_events);
  1692. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  1693. struct perf_event *event = cpuc->events[idx];
  1694. struct hw_perf_event *hwc;
  1695. if (!test_bit(idx, cpuc->active_mask))
  1696. continue;
  1697. /*
  1698. * We have a single interrupt for all counters. Check that
  1699. * each counter has overflowed before we process it.
  1700. */
  1701. if (!armv7_pmnc_counter_has_overflowed(pmnc, idx))
  1702. continue;
  1703. hwc = &event->hw;
  1704. armpmu_event_update(event, hwc, idx);
  1705. data.period = event->hw.last_period;
  1706. if (!armpmu_event_set_period(event, hwc, idx))
  1707. continue;
  1708. if (perf_event_overflow(event, 0, &data, regs))
  1709. armpmu->disable(hwc, idx);
  1710. }
  1711. /*
  1712. * Handle the pending perf events.
  1713. *
  1714. * Note: this call *must* be run with interrupts enabled. For
  1715. * platforms that can have the PMU interrupts raised as a PMI, this
  1716. * will not work.
  1717. */
  1718. perf_event_do_pending();
  1719. return IRQ_HANDLED;
  1720. }
  1721. static void armv7pmu_start(void)
  1722. {
  1723. unsigned long flags;
  1724. spin_lock_irqsave(&pmu_lock, flags);
  1725. /* Enable all counters */
  1726. armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E);
  1727. spin_unlock_irqrestore(&pmu_lock, flags);
  1728. }
  1729. static void armv7pmu_stop(void)
  1730. {
  1731. unsigned long flags;
  1732. spin_lock_irqsave(&pmu_lock, flags);
  1733. /* Disable all counters */
  1734. armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E);
  1735. spin_unlock_irqrestore(&pmu_lock, flags);
  1736. }
  1737. static inline int armv7_a8_pmu_event_map(int config)
  1738. {
  1739. int mapping = armv7_a8_perf_map[config];
  1740. if (HW_OP_UNSUPPORTED == mapping)
  1741. mapping = -EOPNOTSUPP;
  1742. return mapping;
  1743. }
  1744. static inline int armv7_a9_pmu_event_map(int config)
  1745. {
  1746. int mapping = armv7_a9_perf_map[config];
  1747. if (HW_OP_UNSUPPORTED == mapping)
  1748. mapping = -EOPNOTSUPP;
  1749. return mapping;
  1750. }
  1751. static u64 armv7pmu_raw_event(u64 config)
  1752. {
  1753. return config & 0xff;
  1754. }
  1755. static int armv7pmu_get_event_idx(struct cpu_hw_events *cpuc,
  1756. struct hw_perf_event *event)
  1757. {
  1758. int idx;
  1759. /* Always place a cycle counter into the cycle counter. */
  1760. if (event->config_base == ARMV7_PERFCTR_CPU_CYCLES) {
  1761. if (test_and_set_bit(ARMV7_CYCLE_COUNTER, cpuc->used_mask))
  1762. return -EAGAIN;
  1763. return ARMV7_CYCLE_COUNTER;
  1764. } else {
  1765. /*
  1766. * For anything other than a cycle counter, try and use
  1767. * the events counters
  1768. */
  1769. for (idx = ARMV7_COUNTER0; idx <= armpmu->num_events; ++idx) {
  1770. if (!test_and_set_bit(idx, cpuc->used_mask))
  1771. return idx;
  1772. }
  1773. /* The counters are all in use. */
  1774. return -EAGAIN;
  1775. }
  1776. }
  1777. static struct arm_pmu armv7pmu = {
  1778. .handle_irq = armv7pmu_handle_irq,
  1779. .enable = armv7pmu_enable_event,
  1780. .disable = armv7pmu_disable_event,
  1781. .raw_event = armv7pmu_raw_event,
  1782. .read_counter = armv7pmu_read_counter,
  1783. .write_counter = armv7pmu_write_counter,
  1784. .get_event_idx = armv7pmu_get_event_idx,
  1785. .start = armv7pmu_start,
  1786. .stop = armv7pmu_stop,
  1787. .max_period = (1LLU << 32) - 1,
  1788. };
  1789. static u32 __init armv7_reset_read_pmnc(void)
  1790. {
  1791. u32 nb_cnt;
  1792. /* Initialize & Reset PMNC: C and P bits */
  1793. armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
  1794. /* Read the nb of CNTx counters supported from PMNC */
  1795. nb_cnt = (armv7_pmnc_read() >> ARMV7_PMNC_N_SHIFT) & ARMV7_PMNC_N_MASK;
  1796. /* Add the CPU cycles counter and return */
  1797. return nb_cnt + 1;
  1798. }
  1799. static int __init
  1800. init_hw_perf_events(void)
  1801. {
  1802. unsigned long cpuid = read_cpuid_id();
  1803. unsigned long implementor = (cpuid & 0xFF000000) >> 24;
  1804. unsigned long part_number = (cpuid & 0xFFF0);
  1805. /* We only support ARM CPUs implemented by ARM at the moment. */
  1806. if (0x41 == implementor) {
  1807. switch (part_number) {
  1808. case 0xB360: /* ARM1136 */
  1809. case 0xB560: /* ARM1156 */
  1810. case 0xB760: /* ARM1176 */
  1811. armpmu = &armv6pmu;
  1812. memcpy(armpmu_perf_cache_map, armv6_perf_cache_map,
  1813. sizeof(armv6_perf_cache_map));
  1814. perf_max_events = armv6pmu.num_events;
  1815. break;
  1816. case 0xB020: /* ARM11mpcore */
  1817. armpmu = &armv6mpcore_pmu;
  1818. memcpy(armpmu_perf_cache_map,
  1819. armv6mpcore_perf_cache_map,
  1820. sizeof(armv6mpcore_perf_cache_map));
  1821. perf_max_events = armv6mpcore_pmu.num_events;
  1822. break;
  1823. case 0xC080: /* Cortex-A8 */
  1824. armv7pmu.name = ARMV7_PMU_CORTEX_A8_NAME;
  1825. memcpy(armpmu_perf_cache_map, armv7_a8_perf_cache_map,
  1826. sizeof(armv7_a8_perf_cache_map));
  1827. armv7pmu.event_map = armv7_a8_pmu_event_map;
  1828. armpmu = &armv7pmu;
  1829. /* Reset PMNC and read the nb of CNTx counters
  1830. supported */
  1831. armv7pmu.num_events = armv7_reset_read_pmnc();
  1832. perf_max_events = armv7pmu.num_events;
  1833. break;
  1834. case 0xC090: /* Cortex-A9 */
  1835. armv7pmu.name = ARMV7_PMU_CORTEX_A9_NAME;
  1836. memcpy(armpmu_perf_cache_map, armv7_a9_perf_cache_map,
  1837. sizeof(armv7_a9_perf_cache_map));
  1838. armv7pmu.event_map = armv7_a9_pmu_event_map;
  1839. armpmu = &armv7pmu;
  1840. /* Reset PMNC and read the nb of CNTx counters
  1841. supported */
  1842. armv7pmu.num_events = armv7_reset_read_pmnc();
  1843. perf_max_events = armv7pmu.num_events;
  1844. break;
  1845. default:
  1846. pr_info("no hardware support available\n");
  1847. perf_max_events = -1;
  1848. }
  1849. }
  1850. if (armpmu)
  1851. pr_info("enabled with %s PMU driver, %d counters available\n",
  1852. armpmu->name, armpmu->num_events);
  1853. return 0;
  1854. }
  1855. arch_initcall(init_hw_perf_events);
  1856. /*
  1857. * Callchain handling code.
  1858. */
  1859. static inline void
  1860. callchain_store(struct perf_callchain_entry *entry,
  1861. u64 ip)
  1862. {
  1863. if (entry->nr < PERF_MAX_STACK_DEPTH)
  1864. entry->ip[entry->nr++] = ip;
  1865. }
  1866. /*
  1867. * The registers we're interested in are at the end of the variable
  1868. * length saved register structure. The fp points at the end of this
  1869. * structure so the address of this struct is:
  1870. * (struct frame_tail *)(xxx->fp)-1
  1871. *
  1872. * This code has been adapted from the ARM OProfile support.
  1873. */
  1874. struct frame_tail {
  1875. struct frame_tail *fp;
  1876. unsigned long sp;
  1877. unsigned long lr;
  1878. } __attribute__((packed));
  1879. /*
  1880. * Get the return address for a single stackframe and return a pointer to the
  1881. * next frame tail.
  1882. */
  1883. static struct frame_tail *
  1884. user_backtrace(struct frame_tail *tail,
  1885. struct perf_callchain_entry *entry)
  1886. {
  1887. struct frame_tail buftail;
  1888. /* Also check accessibility of one struct frame_tail beyond */
  1889. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  1890. return NULL;
  1891. if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
  1892. return NULL;
  1893. callchain_store(entry, buftail.lr);
  1894. /*
  1895. * Frame pointers should strictly progress back up the stack
  1896. * (towards higher addresses).
  1897. */
  1898. if (tail >= buftail.fp)
  1899. return NULL;
  1900. return buftail.fp - 1;
  1901. }
  1902. static void
  1903. perf_callchain_user(struct pt_regs *regs,
  1904. struct perf_callchain_entry *entry)
  1905. {
  1906. struct frame_tail *tail;
  1907. callchain_store(entry, PERF_CONTEXT_USER);
  1908. if (!user_mode(regs))
  1909. regs = task_pt_regs(current);
  1910. tail = (struct frame_tail *)regs->ARM_fp - 1;
  1911. while (tail && !((unsigned long)tail & 0x3))
  1912. tail = user_backtrace(tail, entry);
  1913. }
  1914. /*
  1915. * Gets called by walk_stackframe() for every stackframe. This will be called
  1916. * whist unwinding the stackframe and is like a subroutine return so we use
  1917. * the PC.
  1918. */
  1919. static int
  1920. callchain_trace(struct stackframe *fr,
  1921. void *data)
  1922. {
  1923. struct perf_callchain_entry *entry = data;
  1924. callchain_store(entry, fr->pc);
  1925. return 0;
  1926. }
  1927. static void
  1928. perf_callchain_kernel(struct pt_regs *regs,
  1929. struct perf_callchain_entry *entry)
  1930. {
  1931. struct stackframe fr;
  1932. callchain_store(entry, PERF_CONTEXT_KERNEL);
  1933. fr.fp = regs->ARM_fp;
  1934. fr.sp = regs->ARM_sp;
  1935. fr.lr = regs->ARM_lr;
  1936. fr.pc = regs->ARM_pc;
  1937. walk_stackframe(&fr, callchain_trace, entry);
  1938. }
  1939. static void
  1940. perf_do_callchain(struct pt_regs *regs,
  1941. struct perf_callchain_entry *entry)
  1942. {
  1943. int is_user;
  1944. if (!regs)
  1945. return;
  1946. is_user = user_mode(regs);
  1947. if (!current || !current->pid)
  1948. return;
  1949. if (is_user && current->state != TASK_RUNNING)
  1950. return;
  1951. if (!is_user)
  1952. perf_callchain_kernel(regs, entry);
  1953. if (current->mm)
  1954. perf_callchain_user(regs, entry);
  1955. }
  1956. static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry);
  1957. struct perf_callchain_entry *
  1958. perf_callchain(struct pt_regs *regs)
  1959. {
  1960. struct perf_callchain_entry *entry = &__get_cpu_var(pmc_irq_entry);
  1961. entry->nr = 0;
  1962. perf_do_callchain(regs, entry);
  1963. return entry;
  1964. }