perf_event.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125
  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/module.h>
  18. #include <linux/perf_event.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/cputype.h>
  23. #include <asm/irq.h>
  24. #include <asm/irq_regs.h>
  25. #include <asm/pmu.h>
  26. #include <asm/stacktrace.h>
  27. static struct platform_device *pmu_device;
  28. /*
  29. * Hardware lock to serialize accesses to PMU registers. Needed for the
  30. * read/modify/write sequences.
  31. */
  32. DEFINE_SPINLOCK(pmu_lock);
  33. /*
  34. * ARMv6 supports a maximum of 3 events, starting from index 1. If we add
  35. * another platform that supports more, we need to increase this to be the
  36. * largest of all platforms.
  37. *
  38. * ARMv7 supports up to 32 events:
  39. * cycle counter CCNT + 31 events counters CNT0..30.
  40. * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
  41. */
  42. #define ARMPMU_MAX_HWEVENTS 33
  43. /* The events for a given CPU. */
  44. struct cpu_hw_events {
  45. /*
  46. * The events that are active on the CPU for the given index. Index 0
  47. * is reserved.
  48. */
  49. struct perf_event *events[ARMPMU_MAX_HWEVENTS];
  50. /*
  51. * A 1 bit for an index indicates that the counter is being used for
  52. * an event. A 0 means that the counter can be used.
  53. */
  54. unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
  55. /*
  56. * A 1 bit for an index indicates that the counter is actively being
  57. * used.
  58. */
  59. unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
  60. };
  61. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  62. /* PMU names. */
  63. static const char *arm_pmu_names[] = {
  64. [ARM_PERF_PMU_ID_XSCALE1] = "xscale1",
  65. [ARM_PERF_PMU_ID_XSCALE2] = "xscale2",
  66. [ARM_PERF_PMU_ID_V6] = "v6",
  67. [ARM_PERF_PMU_ID_V6MP] = "v6mpcore",
  68. [ARM_PERF_PMU_ID_CA8] = "ARMv7 Cortex-A8",
  69. [ARM_PERF_PMU_ID_CA9] = "ARMv7 Cortex-A9",
  70. };
  71. struct arm_pmu {
  72. enum arm_perf_pmu_ids id;
  73. irqreturn_t (*handle_irq)(int irq_num, void *dev);
  74. void (*enable)(struct hw_perf_event *evt, int idx);
  75. void (*disable)(struct hw_perf_event *evt, int idx);
  76. int (*event_map)(int evt);
  77. u64 (*raw_event)(u64);
  78. int (*get_event_idx)(struct cpu_hw_events *cpuc,
  79. struct hw_perf_event *hwc);
  80. u32 (*read_counter)(int idx);
  81. void (*write_counter)(int idx, u32 val);
  82. void (*start)(void);
  83. void (*stop)(void);
  84. int num_events;
  85. u64 max_period;
  86. };
  87. /* Set at runtime when we know what CPU type we are. */
  88. static const struct arm_pmu *armpmu;
  89. enum arm_perf_pmu_ids
  90. armpmu_get_pmu_id(void)
  91. {
  92. int id = -ENODEV;
  93. if (armpmu != NULL)
  94. id = armpmu->id;
  95. return id;
  96. }
  97. EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
  98. int
  99. armpmu_get_max_events(void)
  100. {
  101. int max_events = 0;
  102. if (armpmu != NULL)
  103. max_events = armpmu->num_events;
  104. return max_events;
  105. }
  106. EXPORT_SYMBOL_GPL(armpmu_get_max_events);
  107. int perf_num_counters(void)
  108. {
  109. return armpmu_get_max_events();
  110. }
  111. EXPORT_SYMBOL_GPL(perf_num_counters);
  112. #define HW_OP_UNSUPPORTED 0xFFFF
  113. #define C(_x) \
  114. PERF_COUNT_HW_CACHE_##_x
  115. #define CACHE_OP_UNSUPPORTED 0xFFFF
  116. static unsigned armpmu_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  117. [PERF_COUNT_HW_CACHE_OP_MAX]
  118. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  119. static int
  120. armpmu_map_cache_event(u64 config)
  121. {
  122. unsigned int cache_type, cache_op, cache_result, ret;
  123. cache_type = (config >> 0) & 0xff;
  124. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  125. return -EINVAL;
  126. cache_op = (config >> 8) & 0xff;
  127. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  128. return -EINVAL;
  129. cache_result = (config >> 16) & 0xff;
  130. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  131. return -EINVAL;
  132. ret = (int)armpmu_perf_cache_map[cache_type][cache_op][cache_result];
  133. if (ret == CACHE_OP_UNSUPPORTED)
  134. return -ENOENT;
  135. return ret;
  136. }
  137. static int
  138. armpmu_event_set_period(struct perf_event *event,
  139. struct hw_perf_event *hwc,
  140. int idx)
  141. {
  142. s64 left = local64_read(&hwc->period_left);
  143. s64 period = hwc->sample_period;
  144. int ret = 0;
  145. if (unlikely(left <= -period)) {
  146. left = period;
  147. local64_set(&hwc->period_left, left);
  148. hwc->last_period = period;
  149. ret = 1;
  150. }
  151. if (unlikely(left <= 0)) {
  152. left += period;
  153. local64_set(&hwc->period_left, left);
  154. hwc->last_period = period;
  155. ret = 1;
  156. }
  157. if (left > (s64)armpmu->max_period)
  158. left = armpmu->max_period;
  159. local64_set(&hwc->prev_count, (u64)-left);
  160. armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
  161. perf_event_update_userpage(event);
  162. return ret;
  163. }
  164. static u64
  165. armpmu_event_update(struct perf_event *event,
  166. struct hw_perf_event *hwc,
  167. int idx)
  168. {
  169. int shift = 64 - 32;
  170. s64 prev_raw_count, new_raw_count;
  171. u64 delta;
  172. again:
  173. prev_raw_count = local64_read(&hwc->prev_count);
  174. new_raw_count = armpmu->read_counter(idx);
  175. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  176. new_raw_count) != prev_raw_count)
  177. goto again;
  178. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  179. delta >>= shift;
  180. local64_add(delta, &event->count);
  181. local64_sub(delta, &hwc->period_left);
  182. return new_raw_count;
  183. }
  184. static void
  185. armpmu_read(struct perf_event *event)
  186. {
  187. struct hw_perf_event *hwc = &event->hw;
  188. /* Don't read disabled counters! */
  189. if (hwc->idx < 0)
  190. return;
  191. armpmu_event_update(event, hwc, hwc->idx);
  192. }
  193. static void
  194. armpmu_stop(struct perf_event *event, int flags)
  195. {
  196. struct hw_perf_event *hwc = &event->hw;
  197. if (!armpmu)
  198. return;
  199. /*
  200. * ARM pmu always has to update the counter, so ignore
  201. * PERF_EF_UPDATE, see comments in armpmu_start().
  202. */
  203. if (!(hwc->state & PERF_HES_STOPPED)) {
  204. armpmu->disable(hwc, hwc->idx);
  205. barrier(); /* why? */
  206. armpmu_event_update(event, hwc, hwc->idx);
  207. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  208. }
  209. }
  210. static void
  211. armpmu_start(struct perf_event *event, int flags)
  212. {
  213. struct hw_perf_event *hwc = &event->hw;
  214. if (!armpmu)
  215. return;
  216. /*
  217. * ARM pmu always has to reprogram the period, so ignore
  218. * PERF_EF_RELOAD, see the comment below.
  219. */
  220. if (flags & PERF_EF_RELOAD)
  221. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  222. hwc->state = 0;
  223. /*
  224. * Set the period again. Some counters can't be stopped, so when we
  225. * were stopped we simply disabled the IRQ source and the counter
  226. * may have been left counting. If we don't do this step then we may
  227. * get an interrupt too soon or *way* too late if the overflow has
  228. * happened since disabling.
  229. */
  230. armpmu_event_set_period(event, hwc, hwc->idx);
  231. armpmu->enable(hwc, hwc->idx);
  232. }
  233. static void
  234. armpmu_del(struct perf_event *event, int flags)
  235. {
  236. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  237. struct hw_perf_event *hwc = &event->hw;
  238. int idx = hwc->idx;
  239. WARN_ON(idx < 0);
  240. clear_bit(idx, cpuc->active_mask);
  241. armpmu_stop(event, PERF_EF_UPDATE);
  242. cpuc->events[idx] = NULL;
  243. clear_bit(idx, cpuc->used_mask);
  244. perf_event_update_userpage(event);
  245. }
  246. static int
  247. armpmu_add(struct perf_event *event, int flags)
  248. {
  249. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  250. struct hw_perf_event *hwc = &event->hw;
  251. int idx;
  252. int err = 0;
  253. perf_pmu_disable(event->pmu);
  254. /* If we don't have a space for the counter then finish early. */
  255. idx = armpmu->get_event_idx(cpuc, hwc);
  256. if (idx < 0) {
  257. err = idx;
  258. goto out;
  259. }
  260. /*
  261. * If there is an event in the counter we are going to use then make
  262. * sure it is disabled.
  263. */
  264. event->hw.idx = idx;
  265. armpmu->disable(hwc, idx);
  266. cpuc->events[idx] = event;
  267. set_bit(idx, cpuc->active_mask);
  268. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  269. if (flags & PERF_EF_START)
  270. armpmu_start(event, PERF_EF_RELOAD);
  271. /* Propagate our changes to the userspace mapping. */
  272. perf_event_update_userpage(event);
  273. out:
  274. perf_pmu_enable(event->pmu);
  275. return err;
  276. }
  277. static struct pmu pmu;
  278. static int
  279. validate_event(struct cpu_hw_events *cpuc,
  280. struct perf_event *event)
  281. {
  282. struct hw_perf_event fake_event = event->hw;
  283. if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
  284. return 1;
  285. return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
  286. }
  287. static int
  288. validate_group(struct perf_event *event)
  289. {
  290. struct perf_event *sibling, *leader = event->group_leader;
  291. struct cpu_hw_events fake_pmu;
  292. memset(&fake_pmu, 0, sizeof(fake_pmu));
  293. if (!validate_event(&fake_pmu, leader))
  294. return -ENOSPC;
  295. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  296. if (!validate_event(&fake_pmu, sibling))
  297. return -ENOSPC;
  298. }
  299. if (!validate_event(&fake_pmu, event))
  300. return -ENOSPC;
  301. return 0;
  302. }
  303. static int
  304. armpmu_reserve_hardware(void)
  305. {
  306. int i, err = -ENODEV, irq;
  307. pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU);
  308. if (IS_ERR(pmu_device)) {
  309. pr_warning("unable to reserve pmu\n");
  310. return PTR_ERR(pmu_device);
  311. }
  312. init_pmu(ARM_PMU_DEVICE_CPU);
  313. if (pmu_device->num_resources < 1) {
  314. pr_err("no irqs for PMUs defined\n");
  315. return -ENODEV;
  316. }
  317. for (i = 0; i < pmu_device->num_resources; ++i) {
  318. irq = platform_get_irq(pmu_device, i);
  319. if (irq < 0)
  320. continue;
  321. err = request_irq(irq, armpmu->handle_irq,
  322. IRQF_DISABLED | IRQF_NOBALANCING,
  323. "armpmu", NULL);
  324. if (err) {
  325. pr_warning("unable to request IRQ%d for ARM perf "
  326. "counters\n", irq);
  327. break;
  328. }
  329. }
  330. if (err) {
  331. for (i = i - 1; i >= 0; --i) {
  332. irq = platform_get_irq(pmu_device, i);
  333. if (irq >= 0)
  334. free_irq(irq, NULL);
  335. }
  336. release_pmu(pmu_device);
  337. pmu_device = NULL;
  338. }
  339. return err;
  340. }
  341. static void
  342. armpmu_release_hardware(void)
  343. {
  344. int i, irq;
  345. for (i = pmu_device->num_resources - 1; i >= 0; --i) {
  346. irq = platform_get_irq(pmu_device, i);
  347. if (irq >= 0)
  348. free_irq(irq, NULL);
  349. }
  350. armpmu->stop();
  351. release_pmu(pmu_device);
  352. pmu_device = NULL;
  353. }
  354. static atomic_t active_events = ATOMIC_INIT(0);
  355. static DEFINE_MUTEX(pmu_reserve_mutex);
  356. static void
  357. hw_perf_event_destroy(struct perf_event *event)
  358. {
  359. if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
  360. armpmu_release_hardware();
  361. mutex_unlock(&pmu_reserve_mutex);
  362. }
  363. }
  364. static int
  365. __hw_perf_event_init(struct perf_event *event)
  366. {
  367. struct hw_perf_event *hwc = &event->hw;
  368. int mapping, err;
  369. /* Decode the generic type into an ARM event identifier. */
  370. if (PERF_TYPE_HARDWARE == event->attr.type) {
  371. mapping = armpmu->event_map(event->attr.config);
  372. } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
  373. mapping = armpmu_map_cache_event(event->attr.config);
  374. } else if (PERF_TYPE_RAW == event->attr.type) {
  375. mapping = armpmu->raw_event(event->attr.config);
  376. } else {
  377. pr_debug("event type %x not supported\n", event->attr.type);
  378. return -EOPNOTSUPP;
  379. }
  380. if (mapping < 0) {
  381. pr_debug("event %x:%llx not supported\n", event->attr.type,
  382. event->attr.config);
  383. return mapping;
  384. }
  385. /*
  386. * Check whether we need to exclude the counter from certain modes.
  387. * The ARM performance counters are on all of the time so if someone
  388. * has asked us for some excludes then we have to fail.
  389. */
  390. if (event->attr.exclude_kernel || event->attr.exclude_user ||
  391. event->attr.exclude_hv || event->attr.exclude_idle) {
  392. pr_debug("ARM performance counters do not support "
  393. "mode exclusion\n");
  394. return -EPERM;
  395. }
  396. /*
  397. * We don't assign an index until we actually place the event onto
  398. * hardware. Use -1 to signify that we haven't decided where to put it
  399. * yet. For SMP systems, each core has it's own PMU so we can't do any
  400. * clever allocation or constraints checking at this point.
  401. */
  402. hwc->idx = -1;
  403. /*
  404. * Store the event encoding into the config_base field. config and
  405. * event_base are unused as the only 2 things we need to know are
  406. * the event mapping and the counter to use. The counter to use is
  407. * also the indx and the config_base is the event type.
  408. */
  409. hwc->config_base = (unsigned long)mapping;
  410. hwc->config = 0;
  411. hwc->event_base = 0;
  412. if (!hwc->sample_period) {
  413. hwc->sample_period = armpmu->max_period;
  414. hwc->last_period = hwc->sample_period;
  415. local64_set(&hwc->period_left, hwc->sample_period);
  416. }
  417. err = 0;
  418. if (event->group_leader != event) {
  419. err = validate_group(event);
  420. if (err)
  421. return -EINVAL;
  422. }
  423. return err;
  424. }
  425. static int armpmu_event_init(struct perf_event *event)
  426. {
  427. int err = 0;
  428. switch (event->attr.type) {
  429. case PERF_TYPE_RAW:
  430. case PERF_TYPE_HARDWARE:
  431. case PERF_TYPE_HW_CACHE:
  432. break;
  433. default:
  434. return -ENOENT;
  435. }
  436. if (!armpmu)
  437. return -ENODEV;
  438. event->destroy = hw_perf_event_destroy;
  439. if (!atomic_inc_not_zero(&active_events)) {
  440. if (atomic_read(&active_events) > armpmu->num_events) {
  441. atomic_dec(&active_events);
  442. return -ENOSPC;
  443. }
  444. mutex_lock(&pmu_reserve_mutex);
  445. if (atomic_read(&active_events) == 0) {
  446. err = armpmu_reserve_hardware();
  447. }
  448. if (!err)
  449. atomic_inc(&active_events);
  450. mutex_unlock(&pmu_reserve_mutex);
  451. }
  452. if (err)
  453. return err;
  454. err = __hw_perf_event_init(event);
  455. if (err)
  456. hw_perf_event_destroy(event);
  457. return err;
  458. }
  459. static void armpmu_enable(struct pmu *pmu)
  460. {
  461. /* Enable all of the perf events on hardware. */
  462. int idx;
  463. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  464. if (!armpmu)
  465. return;
  466. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  467. struct perf_event *event = cpuc->events[idx];
  468. if (!event)
  469. continue;
  470. armpmu->enable(&event->hw, idx);
  471. }
  472. armpmu->start();
  473. }
  474. static void armpmu_disable(struct pmu *pmu)
  475. {
  476. if (armpmu)
  477. armpmu->stop();
  478. }
  479. static struct pmu pmu = {
  480. .pmu_enable = armpmu_enable,
  481. .pmu_disable = armpmu_disable,
  482. .event_init = armpmu_event_init,
  483. .add = armpmu_add,
  484. .del = armpmu_del,
  485. .start = armpmu_start,
  486. .stop = armpmu_stop,
  487. .read = armpmu_read,
  488. };
  489. /*
  490. * ARMv6 Performance counter handling code.
  491. *
  492. * ARMv6 has 2 configurable performance counters and a single cycle counter.
  493. * They all share a single reset bit but can be written to zero so we can use
  494. * that for a reset.
  495. *
  496. * The counters can't be individually enabled or disabled so when we remove
  497. * one event and replace it with another we could get spurious counts from the
  498. * wrong event. However, we can take advantage of the fact that the
  499. * performance counters can export events to the event bus, and the event bus
  500. * itself can be monitored. This requires that we *don't* export the events to
  501. * the event bus. The procedure for disabling a configurable counter is:
  502. * - change the counter to count the ETMEXTOUT[0] signal (0x20). This
  503. * effectively stops the counter from counting.
  504. * - disable the counter's interrupt generation (each counter has it's
  505. * own interrupt enable bit).
  506. * Once stopped, the counter value can be written as 0 to reset.
  507. *
  508. * To enable a counter:
  509. * - enable the counter's interrupt generation.
  510. * - set the new event type.
  511. *
  512. * Note: the dedicated cycle counter only counts cycles and can't be
  513. * enabled/disabled independently of the others. When we want to disable the
  514. * cycle counter, we have to just disable the interrupt reporting and start
  515. * ignoring that counter. When re-enabling, we have to reset the value and
  516. * enable the interrupt.
  517. */
  518. enum armv6_perf_types {
  519. ARMV6_PERFCTR_ICACHE_MISS = 0x0,
  520. ARMV6_PERFCTR_IBUF_STALL = 0x1,
  521. ARMV6_PERFCTR_DDEP_STALL = 0x2,
  522. ARMV6_PERFCTR_ITLB_MISS = 0x3,
  523. ARMV6_PERFCTR_DTLB_MISS = 0x4,
  524. ARMV6_PERFCTR_BR_EXEC = 0x5,
  525. ARMV6_PERFCTR_BR_MISPREDICT = 0x6,
  526. ARMV6_PERFCTR_INSTR_EXEC = 0x7,
  527. ARMV6_PERFCTR_DCACHE_HIT = 0x9,
  528. ARMV6_PERFCTR_DCACHE_ACCESS = 0xA,
  529. ARMV6_PERFCTR_DCACHE_MISS = 0xB,
  530. ARMV6_PERFCTR_DCACHE_WBACK = 0xC,
  531. ARMV6_PERFCTR_SW_PC_CHANGE = 0xD,
  532. ARMV6_PERFCTR_MAIN_TLB_MISS = 0xF,
  533. ARMV6_PERFCTR_EXPL_D_ACCESS = 0x10,
  534. ARMV6_PERFCTR_LSU_FULL_STALL = 0x11,
  535. ARMV6_PERFCTR_WBUF_DRAINED = 0x12,
  536. ARMV6_PERFCTR_CPU_CYCLES = 0xFF,
  537. ARMV6_PERFCTR_NOP = 0x20,
  538. };
  539. enum armv6_counters {
  540. ARMV6_CYCLE_COUNTER = 1,
  541. ARMV6_COUNTER0,
  542. ARMV6_COUNTER1,
  543. };
  544. /*
  545. * The hardware events that we support. We do support cache operations but
  546. * we have harvard caches and no way to combine instruction and data
  547. * accesses/misses in hardware.
  548. */
  549. static const unsigned armv6_perf_map[PERF_COUNT_HW_MAX] = {
  550. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6_PERFCTR_CPU_CYCLES,
  551. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6_PERFCTR_INSTR_EXEC,
  552. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  553. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  554. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6_PERFCTR_BR_EXEC,
  555. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6_PERFCTR_BR_MISPREDICT,
  556. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  557. };
  558. static const unsigned armv6_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  559. [PERF_COUNT_HW_CACHE_OP_MAX]
  560. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  561. [C(L1D)] = {
  562. /*
  563. * The performance counters don't differentiate between read
  564. * and write accesses/misses so this isn't strictly correct,
  565. * but it's the best we can do. Writes and reads get
  566. * combined.
  567. */
  568. [C(OP_READ)] = {
  569. [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  570. [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  571. },
  572. [C(OP_WRITE)] = {
  573. [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  574. [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  575. },
  576. [C(OP_PREFETCH)] = {
  577. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  578. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  579. },
  580. },
  581. [C(L1I)] = {
  582. [C(OP_READ)] = {
  583. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  584. [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
  585. },
  586. [C(OP_WRITE)] = {
  587. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  588. [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
  589. },
  590. [C(OP_PREFETCH)] = {
  591. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  592. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  593. },
  594. },
  595. [C(LL)] = {
  596. [C(OP_READ)] = {
  597. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  598. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  599. },
  600. [C(OP_WRITE)] = {
  601. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  602. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  603. },
  604. [C(OP_PREFETCH)] = {
  605. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  606. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  607. },
  608. },
  609. [C(DTLB)] = {
  610. /*
  611. * The ARM performance counters can count micro DTLB misses,
  612. * micro ITLB misses and main TLB misses. There isn't an event
  613. * for TLB misses, so use the micro misses here and if users
  614. * want the main TLB misses they can use a raw counter.
  615. */
  616. [C(OP_READ)] = {
  617. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  618. [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  619. },
  620. [C(OP_WRITE)] = {
  621. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  622. [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  623. },
  624. [C(OP_PREFETCH)] = {
  625. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  626. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  627. },
  628. },
  629. [C(ITLB)] = {
  630. [C(OP_READ)] = {
  631. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  632. [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  633. },
  634. [C(OP_WRITE)] = {
  635. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  636. [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  637. },
  638. [C(OP_PREFETCH)] = {
  639. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  640. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  641. },
  642. },
  643. [C(BPU)] = {
  644. [C(OP_READ)] = {
  645. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  646. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  647. },
  648. [C(OP_WRITE)] = {
  649. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  650. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  651. },
  652. [C(OP_PREFETCH)] = {
  653. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  654. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  655. },
  656. },
  657. };
  658. enum armv6mpcore_perf_types {
  659. ARMV6MPCORE_PERFCTR_ICACHE_MISS = 0x0,
  660. ARMV6MPCORE_PERFCTR_IBUF_STALL = 0x1,
  661. ARMV6MPCORE_PERFCTR_DDEP_STALL = 0x2,
  662. ARMV6MPCORE_PERFCTR_ITLB_MISS = 0x3,
  663. ARMV6MPCORE_PERFCTR_DTLB_MISS = 0x4,
  664. ARMV6MPCORE_PERFCTR_BR_EXEC = 0x5,
  665. ARMV6MPCORE_PERFCTR_BR_NOTPREDICT = 0x6,
  666. ARMV6MPCORE_PERFCTR_BR_MISPREDICT = 0x7,
  667. ARMV6MPCORE_PERFCTR_INSTR_EXEC = 0x8,
  668. ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS = 0xA,
  669. ARMV6MPCORE_PERFCTR_DCACHE_RDMISS = 0xB,
  670. ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS = 0xC,
  671. ARMV6MPCORE_PERFCTR_DCACHE_WRMISS = 0xD,
  672. ARMV6MPCORE_PERFCTR_DCACHE_EVICTION = 0xE,
  673. ARMV6MPCORE_PERFCTR_SW_PC_CHANGE = 0xF,
  674. ARMV6MPCORE_PERFCTR_MAIN_TLB_MISS = 0x10,
  675. ARMV6MPCORE_PERFCTR_EXPL_MEM_ACCESS = 0x11,
  676. ARMV6MPCORE_PERFCTR_LSU_FULL_STALL = 0x12,
  677. ARMV6MPCORE_PERFCTR_WBUF_DRAINED = 0x13,
  678. ARMV6MPCORE_PERFCTR_CPU_CYCLES = 0xFF,
  679. };
  680. /*
  681. * The hardware events that we support. We do support cache operations but
  682. * we have harvard caches and no way to combine instruction and data
  683. * accesses/misses in hardware.
  684. */
  685. static const unsigned armv6mpcore_perf_map[PERF_COUNT_HW_MAX] = {
  686. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6MPCORE_PERFCTR_CPU_CYCLES,
  687. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_INSTR_EXEC,
  688. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  689. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  690. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_BR_EXEC,
  691. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6MPCORE_PERFCTR_BR_MISPREDICT,
  692. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  693. };
  694. static const unsigned armv6mpcore_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  695. [PERF_COUNT_HW_CACHE_OP_MAX]
  696. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  697. [C(L1D)] = {
  698. [C(OP_READ)] = {
  699. [C(RESULT_ACCESS)] =
  700. ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS,
  701. [C(RESULT_MISS)] =
  702. ARMV6MPCORE_PERFCTR_DCACHE_RDMISS,
  703. },
  704. [C(OP_WRITE)] = {
  705. [C(RESULT_ACCESS)] =
  706. ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS,
  707. [C(RESULT_MISS)] =
  708. ARMV6MPCORE_PERFCTR_DCACHE_WRMISS,
  709. },
  710. [C(OP_PREFETCH)] = {
  711. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  712. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  713. },
  714. },
  715. [C(L1I)] = {
  716. [C(OP_READ)] = {
  717. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  718. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
  719. },
  720. [C(OP_WRITE)] = {
  721. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  722. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
  723. },
  724. [C(OP_PREFETCH)] = {
  725. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  726. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  727. },
  728. },
  729. [C(LL)] = {
  730. [C(OP_READ)] = {
  731. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  732. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  733. },
  734. [C(OP_WRITE)] = {
  735. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  736. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  737. },
  738. [C(OP_PREFETCH)] = {
  739. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  740. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  741. },
  742. },
  743. [C(DTLB)] = {
  744. /*
  745. * The ARM performance counters can count micro DTLB misses,
  746. * micro ITLB misses and main TLB misses. There isn't an event
  747. * for TLB misses, so use the micro misses here and if users
  748. * want the main TLB misses they can use a raw counter.
  749. */
  750. [C(OP_READ)] = {
  751. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  752. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  753. },
  754. [C(OP_WRITE)] = {
  755. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  756. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  757. },
  758. [C(OP_PREFETCH)] = {
  759. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  760. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  761. },
  762. },
  763. [C(ITLB)] = {
  764. [C(OP_READ)] = {
  765. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  766. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  767. },
  768. [C(OP_WRITE)] = {
  769. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  770. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  771. },
  772. [C(OP_PREFETCH)] = {
  773. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  774. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  775. },
  776. },
  777. [C(BPU)] = {
  778. [C(OP_READ)] = {
  779. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  780. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  781. },
  782. [C(OP_WRITE)] = {
  783. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  784. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  785. },
  786. [C(OP_PREFETCH)] = {
  787. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  788. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  789. },
  790. },
  791. };
  792. static inline unsigned long
  793. armv6_pmcr_read(void)
  794. {
  795. u32 val;
  796. asm volatile("mrc p15, 0, %0, c15, c12, 0" : "=r"(val));
  797. return val;
  798. }
  799. static inline void
  800. armv6_pmcr_write(unsigned long val)
  801. {
  802. asm volatile("mcr p15, 0, %0, c15, c12, 0" : : "r"(val));
  803. }
  804. #define ARMV6_PMCR_ENABLE (1 << 0)
  805. #define ARMV6_PMCR_CTR01_RESET (1 << 1)
  806. #define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
  807. #define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
  808. #define ARMV6_PMCR_COUNT0_IEN (1 << 4)
  809. #define ARMV6_PMCR_COUNT1_IEN (1 << 5)
  810. #define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
  811. #define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
  812. #define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
  813. #define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
  814. #define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
  815. #define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
  816. #define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
  817. #define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
  818. #define ARMV6_PMCR_OVERFLOWED_MASK \
  819. (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
  820. ARMV6_PMCR_CCOUNT_OVERFLOW)
  821. static inline int
  822. armv6_pmcr_has_overflowed(unsigned long pmcr)
  823. {
  824. return (pmcr & ARMV6_PMCR_OVERFLOWED_MASK);
  825. }
  826. static inline int
  827. armv6_pmcr_counter_has_overflowed(unsigned long pmcr,
  828. enum armv6_counters counter)
  829. {
  830. int ret = 0;
  831. if (ARMV6_CYCLE_COUNTER == counter)
  832. ret = pmcr & ARMV6_PMCR_CCOUNT_OVERFLOW;
  833. else if (ARMV6_COUNTER0 == counter)
  834. ret = pmcr & ARMV6_PMCR_COUNT0_OVERFLOW;
  835. else if (ARMV6_COUNTER1 == counter)
  836. ret = pmcr & ARMV6_PMCR_COUNT1_OVERFLOW;
  837. else
  838. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  839. return ret;
  840. }
  841. static inline u32
  842. armv6pmu_read_counter(int counter)
  843. {
  844. unsigned long value = 0;
  845. if (ARMV6_CYCLE_COUNTER == counter)
  846. asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value));
  847. else if (ARMV6_COUNTER0 == counter)
  848. asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value));
  849. else if (ARMV6_COUNTER1 == counter)
  850. asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value));
  851. else
  852. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  853. return value;
  854. }
  855. static inline void
  856. armv6pmu_write_counter(int counter,
  857. u32 value)
  858. {
  859. if (ARMV6_CYCLE_COUNTER == counter)
  860. asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value));
  861. else if (ARMV6_COUNTER0 == counter)
  862. asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value));
  863. else if (ARMV6_COUNTER1 == counter)
  864. asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value));
  865. else
  866. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  867. }
  868. void
  869. armv6pmu_enable_event(struct hw_perf_event *hwc,
  870. int idx)
  871. {
  872. unsigned long val, mask, evt, flags;
  873. if (ARMV6_CYCLE_COUNTER == idx) {
  874. mask = 0;
  875. evt = ARMV6_PMCR_CCOUNT_IEN;
  876. } else if (ARMV6_COUNTER0 == idx) {
  877. mask = ARMV6_PMCR_EVT_COUNT0_MASK;
  878. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT0_SHIFT) |
  879. ARMV6_PMCR_COUNT0_IEN;
  880. } else if (ARMV6_COUNTER1 == idx) {
  881. mask = ARMV6_PMCR_EVT_COUNT1_MASK;
  882. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT1_SHIFT) |
  883. ARMV6_PMCR_COUNT1_IEN;
  884. } else {
  885. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  886. return;
  887. }
  888. /*
  889. * Mask out the current event and set the counter to count the event
  890. * that we're interested in.
  891. */
  892. spin_lock_irqsave(&pmu_lock, flags);
  893. val = armv6_pmcr_read();
  894. val &= ~mask;
  895. val |= evt;
  896. armv6_pmcr_write(val);
  897. spin_unlock_irqrestore(&pmu_lock, flags);
  898. }
  899. static irqreturn_t
  900. armv6pmu_handle_irq(int irq_num,
  901. void *dev)
  902. {
  903. unsigned long pmcr = armv6_pmcr_read();
  904. struct perf_sample_data data;
  905. struct cpu_hw_events *cpuc;
  906. struct pt_regs *regs;
  907. int idx;
  908. if (!armv6_pmcr_has_overflowed(pmcr))
  909. return IRQ_NONE;
  910. regs = get_irq_regs();
  911. /*
  912. * The interrupts are cleared by writing the overflow flags back to
  913. * the control register. All of the other bits don't have any effect
  914. * if they are rewritten, so write the whole value back.
  915. */
  916. armv6_pmcr_write(pmcr);
  917. perf_sample_data_init(&data, 0);
  918. cpuc = &__get_cpu_var(cpu_hw_events);
  919. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  920. struct perf_event *event = cpuc->events[idx];
  921. struct hw_perf_event *hwc;
  922. if (!test_bit(idx, cpuc->active_mask))
  923. continue;
  924. /*
  925. * We have a single interrupt for all counters. Check that
  926. * each counter has overflowed before we process it.
  927. */
  928. if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
  929. continue;
  930. hwc = &event->hw;
  931. armpmu_event_update(event, hwc, idx);
  932. data.period = event->hw.last_period;
  933. if (!armpmu_event_set_period(event, hwc, idx))
  934. continue;
  935. if (perf_event_overflow(event, 0, &data, regs))
  936. armpmu->disable(hwc, idx);
  937. }
  938. /*
  939. * Handle the pending perf events.
  940. *
  941. * Note: this call *must* be run with interrupts disabled. For
  942. * platforms that can have the PMU interrupts raised as an NMI, this
  943. * will not work.
  944. */
  945. irq_work_run();
  946. return IRQ_HANDLED;
  947. }
  948. static void
  949. armv6pmu_start(void)
  950. {
  951. unsigned long flags, val;
  952. spin_lock_irqsave(&pmu_lock, flags);
  953. val = armv6_pmcr_read();
  954. val |= ARMV6_PMCR_ENABLE;
  955. armv6_pmcr_write(val);
  956. spin_unlock_irqrestore(&pmu_lock, flags);
  957. }
  958. void
  959. armv6pmu_stop(void)
  960. {
  961. unsigned long flags, val;
  962. spin_lock_irqsave(&pmu_lock, flags);
  963. val = armv6_pmcr_read();
  964. val &= ~ARMV6_PMCR_ENABLE;
  965. armv6_pmcr_write(val);
  966. spin_unlock_irqrestore(&pmu_lock, flags);
  967. }
  968. static inline int
  969. armv6pmu_event_map(int config)
  970. {
  971. int mapping = armv6_perf_map[config];
  972. if (HW_OP_UNSUPPORTED == mapping)
  973. mapping = -EOPNOTSUPP;
  974. return mapping;
  975. }
  976. static inline int
  977. armv6mpcore_pmu_event_map(int config)
  978. {
  979. int mapping = armv6mpcore_perf_map[config];
  980. if (HW_OP_UNSUPPORTED == mapping)
  981. mapping = -EOPNOTSUPP;
  982. return mapping;
  983. }
  984. static u64
  985. armv6pmu_raw_event(u64 config)
  986. {
  987. return config & 0xff;
  988. }
  989. static int
  990. armv6pmu_get_event_idx(struct cpu_hw_events *cpuc,
  991. struct hw_perf_event *event)
  992. {
  993. /* Always place a cycle counter into the cycle counter. */
  994. if (ARMV6_PERFCTR_CPU_CYCLES == event->config_base) {
  995. if (test_and_set_bit(ARMV6_CYCLE_COUNTER, cpuc->used_mask))
  996. return -EAGAIN;
  997. return ARMV6_CYCLE_COUNTER;
  998. } else {
  999. /*
  1000. * For anything other than a cycle counter, try and use
  1001. * counter0 and counter1.
  1002. */
  1003. if (!test_and_set_bit(ARMV6_COUNTER1, cpuc->used_mask)) {
  1004. return ARMV6_COUNTER1;
  1005. }
  1006. if (!test_and_set_bit(ARMV6_COUNTER0, cpuc->used_mask)) {
  1007. return ARMV6_COUNTER0;
  1008. }
  1009. /* The counters are all in use. */
  1010. return -EAGAIN;
  1011. }
  1012. }
  1013. static void
  1014. armv6pmu_disable_event(struct hw_perf_event *hwc,
  1015. int idx)
  1016. {
  1017. unsigned long val, mask, evt, flags;
  1018. if (ARMV6_CYCLE_COUNTER == idx) {
  1019. mask = ARMV6_PMCR_CCOUNT_IEN;
  1020. evt = 0;
  1021. } else if (ARMV6_COUNTER0 == idx) {
  1022. mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
  1023. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
  1024. } else if (ARMV6_COUNTER1 == idx) {
  1025. mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
  1026. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
  1027. } else {
  1028. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  1029. return;
  1030. }
  1031. /*
  1032. * Mask out the current event and set the counter to count the number
  1033. * of ETM bus signal assertion cycles. The external reporting should
  1034. * be disabled and so this should never increment.
  1035. */
  1036. spin_lock_irqsave(&pmu_lock, flags);
  1037. val = armv6_pmcr_read();
  1038. val &= ~mask;
  1039. val |= evt;
  1040. armv6_pmcr_write(val);
  1041. spin_unlock_irqrestore(&pmu_lock, flags);
  1042. }
  1043. static void
  1044. armv6mpcore_pmu_disable_event(struct hw_perf_event *hwc,
  1045. int idx)
  1046. {
  1047. unsigned long val, mask, flags, evt = 0;
  1048. if (ARMV6_CYCLE_COUNTER == idx) {
  1049. mask = ARMV6_PMCR_CCOUNT_IEN;
  1050. } else if (ARMV6_COUNTER0 == idx) {
  1051. mask = ARMV6_PMCR_COUNT0_IEN;
  1052. } else if (ARMV6_COUNTER1 == idx) {
  1053. mask = ARMV6_PMCR_COUNT1_IEN;
  1054. } else {
  1055. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  1056. return;
  1057. }
  1058. /*
  1059. * Unlike UP ARMv6, we don't have a way of stopping the counters. We
  1060. * simply disable the interrupt reporting.
  1061. */
  1062. spin_lock_irqsave(&pmu_lock, flags);
  1063. val = armv6_pmcr_read();
  1064. val &= ~mask;
  1065. val |= evt;
  1066. armv6_pmcr_write(val);
  1067. spin_unlock_irqrestore(&pmu_lock, flags);
  1068. }
  1069. static const struct arm_pmu armv6pmu = {
  1070. .id = ARM_PERF_PMU_ID_V6,
  1071. .handle_irq = armv6pmu_handle_irq,
  1072. .enable = armv6pmu_enable_event,
  1073. .disable = armv6pmu_disable_event,
  1074. .event_map = armv6pmu_event_map,
  1075. .raw_event = armv6pmu_raw_event,
  1076. .read_counter = armv6pmu_read_counter,
  1077. .write_counter = armv6pmu_write_counter,
  1078. .get_event_idx = armv6pmu_get_event_idx,
  1079. .start = armv6pmu_start,
  1080. .stop = armv6pmu_stop,
  1081. .num_events = 3,
  1082. .max_period = (1LLU << 32) - 1,
  1083. };
  1084. /*
  1085. * ARMv6mpcore is almost identical to single core ARMv6 with the exception
  1086. * that some of the events have different enumerations and that there is no
  1087. * *hack* to stop the programmable counters. To stop the counters we simply
  1088. * disable the interrupt reporting and update the event. When unthrottling we
  1089. * reset the period and enable the interrupt reporting.
  1090. */
  1091. static const struct arm_pmu armv6mpcore_pmu = {
  1092. .id = ARM_PERF_PMU_ID_V6MP,
  1093. .handle_irq = armv6pmu_handle_irq,
  1094. .enable = armv6pmu_enable_event,
  1095. .disable = armv6mpcore_pmu_disable_event,
  1096. .event_map = armv6mpcore_pmu_event_map,
  1097. .raw_event = armv6pmu_raw_event,
  1098. .read_counter = armv6pmu_read_counter,
  1099. .write_counter = armv6pmu_write_counter,
  1100. .get_event_idx = armv6pmu_get_event_idx,
  1101. .start = armv6pmu_start,
  1102. .stop = armv6pmu_stop,
  1103. .num_events = 3,
  1104. .max_period = (1LLU << 32) - 1,
  1105. };
  1106. /*
  1107. * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
  1108. *
  1109. * Copied from ARMv6 code, with the low level code inspired
  1110. * by the ARMv7 Oprofile code.
  1111. *
  1112. * Cortex-A8 has up to 4 configurable performance counters and
  1113. * a single cycle counter.
  1114. * Cortex-A9 has up to 31 configurable performance counters and
  1115. * a single cycle counter.
  1116. *
  1117. * All counters can be enabled/disabled and IRQ masked separately. The cycle
  1118. * counter and all 4 performance counters together can be reset separately.
  1119. */
  1120. /* Common ARMv7 event types */
  1121. enum armv7_perf_types {
  1122. ARMV7_PERFCTR_PMNC_SW_INCR = 0x00,
  1123. ARMV7_PERFCTR_IFETCH_MISS = 0x01,
  1124. ARMV7_PERFCTR_ITLB_MISS = 0x02,
  1125. ARMV7_PERFCTR_DCACHE_REFILL = 0x03,
  1126. ARMV7_PERFCTR_DCACHE_ACCESS = 0x04,
  1127. ARMV7_PERFCTR_DTLB_REFILL = 0x05,
  1128. ARMV7_PERFCTR_DREAD = 0x06,
  1129. ARMV7_PERFCTR_DWRITE = 0x07,
  1130. ARMV7_PERFCTR_EXC_TAKEN = 0x09,
  1131. ARMV7_PERFCTR_EXC_EXECUTED = 0x0A,
  1132. ARMV7_PERFCTR_CID_WRITE = 0x0B,
  1133. /* ARMV7_PERFCTR_PC_WRITE is equivalent to HW_BRANCH_INSTRUCTIONS.
  1134. * It counts:
  1135. * - all branch instructions,
  1136. * - instructions that explicitly write the PC,
  1137. * - exception generating instructions.
  1138. */
  1139. ARMV7_PERFCTR_PC_WRITE = 0x0C,
  1140. ARMV7_PERFCTR_PC_IMM_BRANCH = 0x0D,
  1141. ARMV7_PERFCTR_UNALIGNED_ACCESS = 0x0F,
  1142. ARMV7_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
  1143. ARMV7_PERFCTR_CLOCK_CYCLES = 0x11,
  1144. ARMV7_PERFCTR_PC_BRANCH_MIS_USED = 0x12,
  1145. ARMV7_PERFCTR_CPU_CYCLES = 0xFF
  1146. };
  1147. /* ARMv7 Cortex-A8 specific event types */
  1148. enum armv7_a8_perf_types {
  1149. ARMV7_PERFCTR_INSTR_EXECUTED = 0x08,
  1150. ARMV7_PERFCTR_PC_PROC_RETURN = 0x0E,
  1151. ARMV7_PERFCTR_WRITE_BUFFER_FULL = 0x40,
  1152. ARMV7_PERFCTR_L2_STORE_MERGED = 0x41,
  1153. ARMV7_PERFCTR_L2_STORE_BUFF = 0x42,
  1154. ARMV7_PERFCTR_L2_ACCESS = 0x43,
  1155. ARMV7_PERFCTR_L2_CACH_MISS = 0x44,
  1156. ARMV7_PERFCTR_AXI_READ_CYCLES = 0x45,
  1157. ARMV7_PERFCTR_AXI_WRITE_CYCLES = 0x46,
  1158. ARMV7_PERFCTR_MEMORY_REPLAY = 0x47,
  1159. ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY = 0x48,
  1160. ARMV7_PERFCTR_L1_DATA_MISS = 0x49,
  1161. ARMV7_PERFCTR_L1_INST_MISS = 0x4A,
  1162. ARMV7_PERFCTR_L1_DATA_COLORING = 0x4B,
  1163. ARMV7_PERFCTR_L1_NEON_DATA = 0x4C,
  1164. ARMV7_PERFCTR_L1_NEON_CACH_DATA = 0x4D,
  1165. ARMV7_PERFCTR_L2_NEON = 0x4E,
  1166. ARMV7_PERFCTR_L2_NEON_HIT = 0x4F,
  1167. ARMV7_PERFCTR_L1_INST = 0x50,
  1168. ARMV7_PERFCTR_PC_RETURN_MIS_PRED = 0x51,
  1169. ARMV7_PERFCTR_PC_BRANCH_FAILED = 0x52,
  1170. ARMV7_PERFCTR_PC_BRANCH_TAKEN = 0x53,
  1171. ARMV7_PERFCTR_PC_BRANCH_EXECUTED = 0x54,
  1172. ARMV7_PERFCTR_OP_EXECUTED = 0x55,
  1173. ARMV7_PERFCTR_CYCLES_INST_STALL = 0x56,
  1174. ARMV7_PERFCTR_CYCLES_INST = 0x57,
  1175. ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL = 0x58,
  1176. ARMV7_PERFCTR_CYCLES_NEON_INST_STALL = 0x59,
  1177. ARMV7_PERFCTR_NEON_CYCLES = 0x5A,
  1178. ARMV7_PERFCTR_PMU0_EVENTS = 0x70,
  1179. ARMV7_PERFCTR_PMU1_EVENTS = 0x71,
  1180. ARMV7_PERFCTR_PMU_EVENTS = 0x72,
  1181. };
  1182. /* ARMv7 Cortex-A9 specific event types */
  1183. enum armv7_a9_perf_types {
  1184. ARMV7_PERFCTR_JAVA_HW_BYTECODE_EXEC = 0x40,
  1185. ARMV7_PERFCTR_JAVA_SW_BYTECODE_EXEC = 0x41,
  1186. ARMV7_PERFCTR_JAZELLE_BRANCH_EXEC = 0x42,
  1187. ARMV7_PERFCTR_COHERENT_LINE_MISS = 0x50,
  1188. ARMV7_PERFCTR_COHERENT_LINE_HIT = 0x51,
  1189. ARMV7_PERFCTR_ICACHE_DEP_STALL_CYCLES = 0x60,
  1190. ARMV7_PERFCTR_DCACHE_DEP_STALL_CYCLES = 0x61,
  1191. ARMV7_PERFCTR_TLB_MISS_DEP_STALL_CYCLES = 0x62,
  1192. ARMV7_PERFCTR_STREX_EXECUTED_PASSED = 0x63,
  1193. ARMV7_PERFCTR_STREX_EXECUTED_FAILED = 0x64,
  1194. ARMV7_PERFCTR_DATA_EVICTION = 0x65,
  1195. ARMV7_PERFCTR_ISSUE_STAGE_NO_INST = 0x66,
  1196. ARMV7_PERFCTR_ISSUE_STAGE_EMPTY = 0x67,
  1197. ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE = 0x68,
  1198. ARMV7_PERFCTR_PREDICTABLE_FUNCT_RETURNS = 0x6E,
  1199. ARMV7_PERFCTR_MAIN_UNIT_EXECUTED_INST = 0x70,
  1200. ARMV7_PERFCTR_SECOND_UNIT_EXECUTED_INST = 0x71,
  1201. ARMV7_PERFCTR_LD_ST_UNIT_EXECUTED_INST = 0x72,
  1202. ARMV7_PERFCTR_FP_EXECUTED_INST = 0x73,
  1203. ARMV7_PERFCTR_NEON_EXECUTED_INST = 0x74,
  1204. ARMV7_PERFCTR_PLD_FULL_DEP_STALL_CYCLES = 0x80,
  1205. ARMV7_PERFCTR_DATA_WR_DEP_STALL_CYCLES = 0x81,
  1206. ARMV7_PERFCTR_ITLB_MISS_DEP_STALL_CYCLES = 0x82,
  1207. ARMV7_PERFCTR_DTLB_MISS_DEP_STALL_CYCLES = 0x83,
  1208. ARMV7_PERFCTR_MICRO_ITLB_MISS_DEP_STALL_CYCLES = 0x84,
  1209. ARMV7_PERFCTR_MICRO_DTLB_MISS_DEP_STALL_CYCLES = 0x85,
  1210. ARMV7_PERFCTR_DMB_DEP_STALL_CYCLES = 0x86,
  1211. ARMV7_PERFCTR_INTGR_CLK_ENABLED_CYCLES = 0x8A,
  1212. ARMV7_PERFCTR_DATA_ENGINE_CLK_EN_CYCLES = 0x8B,
  1213. ARMV7_PERFCTR_ISB_INST = 0x90,
  1214. ARMV7_PERFCTR_DSB_INST = 0x91,
  1215. ARMV7_PERFCTR_DMB_INST = 0x92,
  1216. ARMV7_PERFCTR_EXT_INTERRUPTS = 0x93,
  1217. ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_COMPLETED = 0xA0,
  1218. ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_SKIPPED = 0xA1,
  1219. ARMV7_PERFCTR_PLE_FIFO_FLUSH = 0xA2,
  1220. ARMV7_PERFCTR_PLE_RQST_COMPLETED = 0xA3,
  1221. ARMV7_PERFCTR_PLE_FIFO_OVERFLOW = 0xA4,
  1222. ARMV7_PERFCTR_PLE_RQST_PROG = 0xA5
  1223. };
  1224. /*
  1225. * Cortex-A8 HW events mapping
  1226. *
  1227. * The hardware events that we support. We do support cache operations but
  1228. * we have harvard caches and no way to combine instruction and data
  1229. * accesses/misses in hardware.
  1230. */
  1231. static const unsigned armv7_a8_perf_map[PERF_COUNT_HW_MAX] = {
  1232. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  1233. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  1234. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  1235. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  1236. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  1237. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1238. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
  1239. };
  1240. static const unsigned armv7_a8_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  1241. [PERF_COUNT_HW_CACHE_OP_MAX]
  1242. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1243. [C(L1D)] = {
  1244. /*
  1245. * The performance counters don't differentiate between read
  1246. * and write accesses/misses so this isn't strictly correct,
  1247. * but it's the best we can do. Writes and reads get
  1248. * combined.
  1249. */
  1250. [C(OP_READ)] = {
  1251. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1252. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1253. },
  1254. [C(OP_WRITE)] = {
  1255. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1256. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1257. },
  1258. [C(OP_PREFETCH)] = {
  1259. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1260. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1261. },
  1262. },
  1263. [C(L1I)] = {
  1264. [C(OP_READ)] = {
  1265. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
  1266. [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
  1267. },
  1268. [C(OP_WRITE)] = {
  1269. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
  1270. [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
  1271. },
  1272. [C(OP_PREFETCH)] = {
  1273. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1274. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1275. },
  1276. },
  1277. [C(LL)] = {
  1278. [C(OP_READ)] = {
  1279. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
  1280. [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
  1281. },
  1282. [C(OP_WRITE)] = {
  1283. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
  1284. [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
  1285. },
  1286. [C(OP_PREFETCH)] = {
  1287. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1288. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1289. },
  1290. },
  1291. [C(DTLB)] = {
  1292. /*
  1293. * Only ITLB misses and DTLB refills are supported.
  1294. * If users want the DTLB refills misses a raw counter
  1295. * must be used.
  1296. */
  1297. [C(OP_READ)] = {
  1298. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1299. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1300. },
  1301. [C(OP_WRITE)] = {
  1302. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1303. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1304. },
  1305. [C(OP_PREFETCH)] = {
  1306. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1307. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1308. },
  1309. },
  1310. [C(ITLB)] = {
  1311. [C(OP_READ)] = {
  1312. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1313. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1314. },
  1315. [C(OP_WRITE)] = {
  1316. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1317. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1318. },
  1319. [C(OP_PREFETCH)] = {
  1320. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1321. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1322. },
  1323. },
  1324. [C(BPU)] = {
  1325. [C(OP_READ)] = {
  1326. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1327. [C(RESULT_MISS)]
  1328. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1329. },
  1330. [C(OP_WRITE)] = {
  1331. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1332. [C(RESULT_MISS)]
  1333. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1334. },
  1335. [C(OP_PREFETCH)] = {
  1336. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1337. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1338. },
  1339. },
  1340. };
  1341. /*
  1342. * Cortex-A9 HW events mapping
  1343. */
  1344. static const unsigned armv7_a9_perf_map[PERF_COUNT_HW_MAX] = {
  1345. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  1346. [PERF_COUNT_HW_INSTRUCTIONS] =
  1347. ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE,
  1348. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_COHERENT_LINE_HIT,
  1349. [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_COHERENT_LINE_MISS,
  1350. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  1351. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1352. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
  1353. };
  1354. static const unsigned armv7_a9_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  1355. [PERF_COUNT_HW_CACHE_OP_MAX]
  1356. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1357. [C(L1D)] = {
  1358. /*
  1359. * The performance counters don't differentiate between read
  1360. * and write accesses/misses so this isn't strictly correct,
  1361. * but it's the best we can do. Writes and reads get
  1362. * combined.
  1363. */
  1364. [C(OP_READ)] = {
  1365. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1366. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1367. },
  1368. [C(OP_WRITE)] = {
  1369. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1370. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1371. },
  1372. [C(OP_PREFETCH)] = {
  1373. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1374. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1375. },
  1376. },
  1377. [C(L1I)] = {
  1378. [C(OP_READ)] = {
  1379. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1380. [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
  1381. },
  1382. [C(OP_WRITE)] = {
  1383. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1384. [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
  1385. },
  1386. [C(OP_PREFETCH)] = {
  1387. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1388. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1389. },
  1390. },
  1391. [C(LL)] = {
  1392. [C(OP_READ)] = {
  1393. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1394. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1395. },
  1396. [C(OP_WRITE)] = {
  1397. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1398. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1399. },
  1400. [C(OP_PREFETCH)] = {
  1401. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1402. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1403. },
  1404. },
  1405. [C(DTLB)] = {
  1406. /*
  1407. * Only ITLB misses and DTLB refills are supported.
  1408. * If users want the DTLB refills misses a raw counter
  1409. * must be used.
  1410. */
  1411. [C(OP_READ)] = {
  1412. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1413. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1414. },
  1415. [C(OP_WRITE)] = {
  1416. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1417. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1418. },
  1419. [C(OP_PREFETCH)] = {
  1420. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1421. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1422. },
  1423. },
  1424. [C(ITLB)] = {
  1425. [C(OP_READ)] = {
  1426. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1427. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1428. },
  1429. [C(OP_WRITE)] = {
  1430. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1431. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1432. },
  1433. [C(OP_PREFETCH)] = {
  1434. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1435. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1436. },
  1437. },
  1438. [C(BPU)] = {
  1439. [C(OP_READ)] = {
  1440. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1441. [C(RESULT_MISS)]
  1442. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1443. },
  1444. [C(OP_WRITE)] = {
  1445. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1446. [C(RESULT_MISS)]
  1447. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1448. },
  1449. [C(OP_PREFETCH)] = {
  1450. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1451. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1452. },
  1453. },
  1454. };
  1455. /*
  1456. * Perf Events counters
  1457. */
  1458. enum armv7_counters {
  1459. ARMV7_CYCLE_COUNTER = 1, /* Cycle counter */
  1460. ARMV7_COUNTER0 = 2, /* First event counter */
  1461. };
  1462. /*
  1463. * The cycle counter is ARMV7_CYCLE_COUNTER.
  1464. * The first event counter is ARMV7_COUNTER0.
  1465. * The last event counter is (ARMV7_COUNTER0 + armpmu->num_events - 1).
  1466. */
  1467. #define ARMV7_COUNTER_LAST (ARMV7_COUNTER0 + armpmu->num_events - 1)
  1468. /*
  1469. * ARMv7 low level PMNC access
  1470. */
  1471. /*
  1472. * Per-CPU PMNC: config reg
  1473. */
  1474. #define ARMV7_PMNC_E (1 << 0) /* Enable all counters */
  1475. #define ARMV7_PMNC_P (1 << 1) /* Reset all counters */
  1476. #define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */
  1477. #define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */
  1478. #define ARMV7_PMNC_X (1 << 4) /* Export to ETM */
  1479. #define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
  1480. #define ARMV7_PMNC_N_SHIFT 11 /* Number of counters supported */
  1481. #define ARMV7_PMNC_N_MASK 0x1f
  1482. #define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */
  1483. /*
  1484. * Available counters
  1485. */
  1486. #define ARMV7_CNT0 0 /* First event counter */
  1487. #define ARMV7_CCNT 31 /* Cycle counter */
  1488. /* Perf Event to low level counters mapping */
  1489. #define ARMV7_EVENT_CNT_TO_CNTx (ARMV7_COUNTER0 - ARMV7_CNT0)
  1490. /*
  1491. * CNTENS: counters enable reg
  1492. */
  1493. #define ARMV7_CNTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1494. #define ARMV7_CNTENS_C (1 << ARMV7_CCNT)
  1495. /*
  1496. * CNTENC: counters disable reg
  1497. */
  1498. #define ARMV7_CNTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1499. #define ARMV7_CNTENC_C (1 << ARMV7_CCNT)
  1500. /*
  1501. * INTENS: counters overflow interrupt enable reg
  1502. */
  1503. #define ARMV7_INTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1504. #define ARMV7_INTENS_C (1 << ARMV7_CCNT)
  1505. /*
  1506. * INTENC: counters overflow interrupt disable reg
  1507. */
  1508. #define ARMV7_INTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1509. #define ARMV7_INTENC_C (1 << ARMV7_CCNT)
  1510. /*
  1511. * EVTSEL: Event selection reg
  1512. */
  1513. #define ARMV7_EVTSEL_MASK 0xff /* Mask for writable bits */
  1514. /*
  1515. * SELECT: Counter selection reg
  1516. */
  1517. #define ARMV7_SELECT_MASK 0x1f /* Mask for writable bits */
  1518. /*
  1519. * FLAG: counters overflow flag status reg
  1520. */
  1521. #define ARMV7_FLAG_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1522. #define ARMV7_FLAG_C (1 << ARMV7_CCNT)
  1523. #define ARMV7_FLAG_MASK 0xffffffff /* Mask for writable bits */
  1524. #define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK
  1525. static inline unsigned long armv7_pmnc_read(void)
  1526. {
  1527. u32 val;
  1528. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val));
  1529. return val;
  1530. }
  1531. static inline void armv7_pmnc_write(unsigned long val)
  1532. {
  1533. val &= ARMV7_PMNC_MASK;
  1534. asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val));
  1535. }
  1536. static inline int armv7_pmnc_has_overflowed(unsigned long pmnc)
  1537. {
  1538. return pmnc & ARMV7_OVERFLOWED_MASK;
  1539. }
  1540. static inline int armv7_pmnc_counter_has_overflowed(unsigned long pmnc,
  1541. enum armv7_counters counter)
  1542. {
  1543. int ret = 0;
  1544. if (counter == ARMV7_CYCLE_COUNTER)
  1545. ret = pmnc & ARMV7_FLAG_C;
  1546. else if ((counter >= ARMV7_COUNTER0) && (counter <= ARMV7_COUNTER_LAST))
  1547. ret = pmnc & ARMV7_FLAG_P(counter);
  1548. else
  1549. pr_err("CPU%u checking wrong counter %d overflow status\n",
  1550. smp_processor_id(), counter);
  1551. return ret;
  1552. }
  1553. static inline int armv7_pmnc_select_counter(unsigned int idx)
  1554. {
  1555. u32 val;
  1556. if ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST)) {
  1557. pr_err("CPU%u selecting wrong PMNC counter"
  1558. " %d\n", smp_processor_id(), idx);
  1559. return -1;
  1560. }
  1561. val = (idx - ARMV7_EVENT_CNT_TO_CNTx) & ARMV7_SELECT_MASK;
  1562. asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (val));
  1563. return idx;
  1564. }
  1565. static inline u32 armv7pmu_read_counter(int idx)
  1566. {
  1567. unsigned long value = 0;
  1568. if (idx == ARMV7_CYCLE_COUNTER)
  1569. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value));
  1570. else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
  1571. if (armv7_pmnc_select_counter(idx) == idx)
  1572. asm volatile("mrc p15, 0, %0, c9, c13, 2"
  1573. : "=r" (value));
  1574. } else
  1575. pr_err("CPU%u reading wrong counter %d\n",
  1576. smp_processor_id(), idx);
  1577. return value;
  1578. }
  1579. static inline void armv7pmu_write_counter(int idx, u32 value)
  1580. {
  1581. if (idx == ARMV7_CYCLE_COUNTER)
  1582. asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value));
  1583. else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
  1584. if (armv7_pmnc_select_counter(idx) == idx)
  1585. asm volatile("mcr p15, 0, %0, c9, c13, 2"
  1586. : : "r" (value));
  1587. } else
  1588. pr_err("CPU%u writing wrong counter %d\n",
  1589. smp_processor_id(), idx);
  1590. }
  1591. static inline void armv7_pmnc_write_evtsel(unsigned int idx, u32 val)
  1592. {
  1593. if (armv7_pmnc_select_counter(idx) == idx) {
  1594. val &= ARMV7_EVTSEL_MASK;
  1595. asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val));
  1596. }
  1597. }
  1598. static inline u32 armv7_pmnc_enable_counter(unsigned int idx)
  1599. {
  1600. u32 val;
  1601. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1602. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1603. pr_err("CPU%u enabling wrong PMNC counter"
  1604. " %d\n", smp_processor_id(), idx);
  1605. return -1;
  1606. }
  1607. if (idx == ARMV7_CYCLE_COUNTER)
  1608. val = ARMV7_CNTENS_C;
  1609. else
  1610. val = ARMV7_CNTENS_P(idx);
  1611. asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (val));
  1612. return idx;
  1613. }
  1614. static inline u32 armv7_pmnc_disable_counter(unsigned int idx)
  1615. {
  1616. u32 val;
  1617. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1618. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1619. pr_err("CPU%u disabling wrong PMNC counter"
  1620. " %d\n", smp_processor_id(), idx);
  1621. return -1;
  1622. }
  1623. if (idx == ARMV7_CYCLE_COUNTER)
  1624. val = ARMV7_CNTENC_C;
  1625. else
  1626. val = ARMV7_CNTENC_P(idx);
  1627. asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (val));
  1628. return idx;
  1629. }
  1630. static inline u32 armv7_pmnc_enable_intens(unsigned int idx)
  1631. {
  1632. u32 val;
  1633. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1634. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1635. pr_err("CPU%u enabling wrong PMNC counter"
  1636. " interrupt enable %d\n", smp_processor_id(), idx);
  1637. return -1;
  1638. }
  1639. if (idx == ARMV7_CYCLE_COUNTER)
  1640. val = ARMV7_INTENS_C;
  1641. else
  1642. val = ARMV7_INTENS_P(idx);
  1643. asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (val));
  1644. return idx;
  1645. }
  1646. static inline u32 armv7_pmnc_disable_intens(unsigned int idx)
  1647. {
  1648. u32 val;
  1649. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1650. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1651. pr_err("CPU%u disabling wrong PMNC counter"
  1652. " interrupt enable %d\n", smp_processor_id(), idx);
  1653. return -1;
  1654. }
  1655. if (idx == ARMV7_CYCLE_COUNTER)
  1656. val = ARMV7_INTENC_C;
  1657. else
  1658. val = ARMV7_INTENC_P(idx);
  1659. asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (val));
  1660. return idx;
  1661. }
  1662. static inline u32 armv7_pmnc_getreset_flags(void)
  1663. {
  1664. u32 val;
  1665. /* Read */
  1666. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  1667. /* Write to clear flags */
  1668. val &= ARMV7_FLAG_MASK;
  1669. asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (val));
  1670. return val;
  1671. }
  1672. #ifdef DEBUG
  1673. static void armv7_pmnc_dump_regs(void)
  1674. {
  1675. u32 val;
  1676. unsigned int cnt;
  1677. printk(KERN_INFO "PMNC registers dump:\n");
  1678. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r" (val));
  1679. printk(KERN_INFO "PMNC =0x%08x\n", val);
  1680. asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r" (val));
  1681. printk(KERN_INFO "CNTENS=0x%08x\n", val);
  1682. asm volatile("mrc p15, 0, %0, c9, c14, 1" : "=r" (val));
  1683. printk(KERN_INFO "INTENS=0x%08x\n", val);
  1684. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  1685. printk(KERN_INFO "FLAGS =0x%08x\n", val);
  1686. asm volatile("mrc p15, 0, %0, c9, c12, 5" : "=r" (val));
  1687. printk(KERN_INFO "SELECT=0x%08x\n", val);
  1688. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (val));
  1689. printk(KERN_INFO "CCNT =0x%08x\n", val);
  1690. for (cnt = ARMV7_COUNTER0; cnt < ARMV7_COUNTER_LAST; cnt++) {
  1691. armv7_pmnc_select_counter(cnt);
  1692. asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (val));
  1693. printk(KERN_INFO "CNT[%d] count =0x%08x\n",
  1694. cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
  1695. asm volatile("mrc p15, 0, %0, c9, c13, 1" : "=r" (val));
  1696. printk(KERN_INFO "CNT[%d] evtsel=0x%08x\n",
  1697. cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
  1698. }
  1699. }
  1700. #endif
  1701. void armv7pmu_enable_event(struct hw_perf_event *hwc, int idx)
  1702. {
  1703. unsigned long flags;
  1704. /*
  1705. * Enable counter and interrupt, and set the counter to count
  1706. * the event that we're interested in.
  1707. */
  1708. spin_lock_irqsave(&pmu_lock, flags);
  1709. /*
  1710. * Disable counter
  1711. */
  1712. armv7_pmnc_disable_counter(idx);
  1713. /*
  1714. * Set event (if destined for PMNx counters)
  1715. * We don't need to set the event if it's a cycle count
  1716. */
  1717. if (idx != ARMV7_CYCLE_COUNTER)
  1718. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  1719. /*
  1720. * Enable interrupt for this counter
  1721. */
  1722. armv7_pmnc_enable_intens(idx);
  1723. /*
  1724. * Enable counter
  1725. */
  1726. armv7_pmnc_enable_counter(idx);
  1727. spin_unlock_irqrestore(&pmu_lock, flags);
  1728. }
  1729. static void armv7pmu_disable_event(struct hw_perf_event *hwc, int idx)
  1730. {
  1731. unsigned long flags;
  1732. /*
  1733. * Disable counter and interrupt
  1734. */
  1735. spin_lock_irqsave(&pmu_lock, flags);
  1736. /*
  1737. * Disable counter
  1738. */
  1739. armv7_pmnc_disable_counter(idx);
  1740. /*
  1741. * Disable interrupt for this counter
  1742. */
  1743. armv7_pmnc_disable_intens(idx);
  1744. spin_unlock_irqrestore(&pmu_lock, flags);
  1745. }
  1746. static irqreturn_t armv7pmu_handle_irq(int irq_num, void *dev)
  1747. {
  1748. unsigned long pmnc;
  1749. struct perf_sample_data data;
  1750. struct cpu_hw_events *cpuc;
  1751. struct pt_regs *regs;
  1752. int idx;
  1753. /*
  1754. * Get and reset the IRQ flags
  1755. */
  1756. pmnc = armv7_pmnc_getreset_flags();
  1757. /*
  1758. * Did an overflow occur?
  1759. */
  1760. if (!armv7_pmnc_has_overflowed(pmnc))
  1761. return IRQ_NONE;
  1762. /*
  1763. * Handle the counter(s) overflow(s)
  1764. */
  1765. regs = get_irq_regs();
  1766. perf_sample_data_init(&data, 0);
  1767. cpuc = &__get_cpu_var(cpu_hw_events);
  1768. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  1769. struct perf_event *event = cpuc->events[idx];
  1770. struct hw_perf_event *hwc;
  1771. if (!test_bit(idx, cpuc->active_mask))
  1772. continue;
  1773. /*
  1774. * We have a single interrupt for all counters. Check that
  1775. * each counter has overflowed before we process it.
  1776. */
  1777. if (!armv7_pmnc_counter_has_overflowed(pmnc, idx))
  1778. continue;
  1779. hwc = &event->hw;
  1780. armpmu_event_update(event, hwc, idx);
  1781. data.period = event->hw.last_period;
  1782. if (!armpmu_event_set_period(event, hwc, idx))
  1783. continue;
  1784. if (perf_event_overflow(event, 0, &data, regs))
  1785. armpmu->disable(hwc, idx);
  1786. }
  1787. /*
  1788. * Handle the pending perf events.
  1789. *
  1790. * Note: this call *must* be run with interrupts disabled. For
  1791. * platforms that can have the PMU interrupts raised as an NMI, this
  1792. * will not work.
  1793. */
  1794. irq_work_run();
  1795. return IRQ_HANDLED;
  1796. }
  1797. static void armv7pmu_start(void)
  1798. {
  1799. unsigned long flags;
  1800. spin_lock_irqsave(&pmu_lock, flags);
  1801. /* Enable all counters */
  1802. armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E);
  1803. spin_unlock_irqrestore(&pmu_lock, flags);
  1804. }
  1805. static void armv7pmu_stop(void)
  1806. {
  1807. unsigned long flags;
  1808. spin_lock_irqsave(&pmu_lock, flags);
  1809. /* Disable all counters */
  1810. armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E);
  1811. spin_unlock_irqrestore(&pmu_lock, flags);
  1812. }
  1813. static inline int armv7_a8_pmu_event_map(int config)
  1814. {
  1815. int mapping = armv7_a8_perf_map[config];
  1816. if (HW_OP_UNSUPPORTED == mapping)
  1817. mapping = -EOPNOTSUPP;
  1818. return mapping;
  1819. }
  1820. static inline int armv7_a9_pmu_event_map(int config)
  1821. {
  1822. int mapping = armv7_a9_perf_map[config];
  1823. if (HW_OP_UNSUPPORTED == mapping)
  1824. mapping = -EOPNOTSUPP;
  1825. return mapping;
  1826. }
  1827. static u64 armv7pmu_raw_event(u64 config)
  1828. {
  1829. return config & 0xff;
  1830. }
  1831. static int armv7pmu_get_event_idx(struct cpu_hw_events *cpuc,
  1832. struct hw_perf_event *event)
  1833. {
  1834. int idx;
  1835. /* Always place a cycle counter into the cycle counter. */
  1836. if (event->config_base == ARMV7_PERFCTR_CPU_CYCLES) {
  1837. if (test_and_set_bit(ARMV7_CYCLE_COUNTER, cpuc->used_mask))
  1838. return -EAGAIN;
  1839. return ARMV7_CYCLE_COUNTER;
  1840. } else {
  1841. /*
  1842. * For anything other than a cycle counter, try and use
  1843. * the events counters
  1844. */
  1845. for (idx = ARMV7_COUNTER0; idx <= armpmu->num_events; ++idx) {
  1846. if (!test_and_set_bit(idx, cpuc->used_mask))
  1847. return idx;
  1848. }
  1849. /* The counters are all in use. */
  1850. return -EAGAIN;
  1851. }
  1852. }
  1853. static struct arm_pmu armv7pmu = {
  1854. .handle_irq = armv7pmu_handle_irq,
  1855. .enable = armv7pmu_enable_event,
  1856. .disable = armv7pmu_disable_event,
  1857. .raw_event = armv7pmu_raw_event,
  1858. .read_counter = armv7pmu_read_counter,
  1859. .write_counter = armv7pmu_write_counter,
  1860. .get_event_idx = armv7pmu_get_event_idx,
  1861. .start = armv7pmu_start,
  1862. .stop = armv7pmu_stop,
  1863. .max_period = (1LLU << 32) - 1,
  1864. };
  1865. static u32 __init armv7_reset_read_pmnc(void)
  1866. {
  1867. u32 nb_cnt;
  1868. /* Initialize & Reset PMNC: C and P bits */
  1869. armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
  1870. /* Read the nb of CNTx counters supported from PMNC */
  1871. nb_cnt = (armv7_pmnc_read() >> ARMV7_PMNC_N_SHIFT) & ARMV7_PMNC_N_MASK;
  1872. /* Add the CPU cycles counter and return */
  1873. return nb_cnt + 1;
  1874. }
  1875. /*
  1876. * ARMv5 [xscale] Performance counter handling code.
  1877. *
  1878. * Based on xscale OProfile code.
  1879. *
  1880. * There are two variants of the xscale PMU that we support:
  1881. * - xscale1pmu: 2 event counters and a cycle counter
  1882. * - xscale2pmu: 4 event counters and a cycle counter
  1883. * The two variants share event definitions, but have different
  1884. * PMU structures.
  1885. */
  1886. enum xscale_perf_types {
  1887. XSCALE_PERFCTR_ICACHE_MISS = 0x00,
  1888. XSCALE_PERFCTR_ICACHE_NO_DELIVER = 0x01,
  1889. XSCALE_PERFCTR_DATA_STALL = 0x02,
  1890. XSCALE_PERFCTR_ITLB_MISS = 0x03,
  1891. XSCALE_PERFCTR_DTLB_MISS = 0x04,
  1892. XSCALE_PERFCTR_BRANCH = 0x05,
  1893. XSCALE_PERFCTR_BRANCH_MISS = 0x06,
  1894. XSCALE_PERFCTR_INSTRUCTION = 0x07,
  1895. XSCALE_PERFCTR_DCACHE_FULL_STALL = 0x08,
  1896. XSCALE_PERFCTR_DCACHE_FULL_STALL_CONTIG = 0x09,
  1897. XSCALE_PERFCTR_DCACHE_ACCESS = 0x0A,
  1898. XSCALE_PERFCTR_DCACHE_MISS = 0x0B,
  1899. XSCALE_PERFCTR_DCACHE_WRITE_BACK = 0x0C,
  1900. XSCALE_PERFCTR_PC_CHANGED = 0x0D,
  1901. XSCALE_PERFCTR_BCU_REQUEST = 0x10,
  1902. XSCALE_PERFCTR_BCU_FULL = 0x11,
  1903. XSCALE_PERFCTR_BCU_DRAIN = 0x12,
  1904. XSCALE_PERFCTR_BCU_ECC_NO_ELOG = 0x14,
  1905. XSCALE_PERFCTR_BCU_1_BIT_ERR = 0x15,
  1906. XSCALE_PERFCTR_RMW = 0x16,
  1907. /* XSCALE_PERFCTR_CCNT is not hardware defined */
  1908. XSCALE_PERFCTR_CCNT = 0xFE,
  1909. XSCALE_PERFCTR_UNUSED = 0xFF,
  1910. };
  1911. enum xscale_counters {
  1912. XSCALE_CYCLE_COUNTER = 1,
  1913. XSCALE_COUNTER0,
  1914. XSCALE_COUNTER1,
  1915. XSCALE_COUNTER2,
  1916. XSCALE_COUNTER3,
  1917. };
  1918. static const unsigned xscale_perf_map[PERF_COUNT_HW_MAX] = {
  1919. [PERF_COUNT_HW_CPU_CYCLES] = XSCALE_PERFCTR_CCNT,
  1920. [PERF_COUNT_HW_INSTRUCTIONS] = XSCALE_PERFCTR_INSTRUCTION,
  1921. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  1922. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  1923. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = XSCALE_PERFCTR_BRANCH,
  1924. [PERF_COUNT_HW_BRANCH_MISSES] = XSCALE_PERFCTR_BRANCH_MISS,
  1925. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  1926. };
  1927. static const unsigned xscale_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  1928. [PERF_COUNT_HW_CACHE_OP_MAX]
  1929. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1930. [C(L1D)] = {
  1931. [C(OP_READ)] = {
  1932. [C(RESULT_ACCESS)] = XSCALE_PERFCTR_DCACHE_ACCESS,
  1933. [C(RESULT_MISS)] = XSCALE_PERFCTR_DCACHE_MISS,
  1934. },
  1935. [C(OP_WRITE)] = {
  1936. [C(RESULT_ACCESS)] = XSCALE_PERFCTR_DCACHE_ACCESS,
  1937. [C(RESULT_MISS)] = XSCALE_PERFCTR_DCACHE_MISS,
  1938. },
  1939. [C(OP_PREFETCH)] = {
  1940. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1941. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1942. },
  1943. },
  1944. [C(L1I)] = {
  1945. [C(OP_READ)] = {
  1946. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1947. [C(RESULT_MISS)] = XSCALE_PERFCTR_ICACHE_MISS,
  1948. },
  1949. [C(OP_WRITE)] = {
  1950. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1951. [C(RESULT_MISS)] = XSCALE_PERFCTR_ICACHE_MISS,
  1952. },
  1953. [C(OP_PREFETCH)] = {
  1954. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1955. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1956. },
  1957. },
  1958. [C(LL)] = {
  1959. [C(OP_READ)] = {
  1960. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1961. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1962. },
  1963. [C(OP_WRITE)] = {
  1964. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1965. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1966. },
  1967. [C(OP_PREFETCH)] = {
  1968. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1969. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1970. },
  1971. },
  1972. [C(DTLB)] = {
  1973. [C(OP_READ)] = {
  1974. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1975. [C(RESULT_MISS)] = XSCALE_PERFCTR_DTLB_MISS,
  1976. },
  1977. [C(OP_WRITE)] = {
  1978. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1979. [C(RESULT_MISS)] = XSCALE_PERFCTR_DTLB_MISS,
  1980. },
  1981. [C(OP_PREFETCH)] = {
  1982. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1983. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1984. },
  1985. },
  1986. [C(ITLB)] = {
  1987. [C(OP_READ)] = {
  1988. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1989. [C(RESULT_MISS)] = XSCALE_PERFCTR_ITLB_MISS,
  1990. },
  1991. [C(OP_WRITE)] = {
  1992. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1993. [C(RESULT_MISS)] = XSCALE_PERFCTR_ITLB_MISS,
  1994. },
  1995. [C(OP_PREFETCH)] = {
  1996. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1997. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1998. },
  1999. },
  2000. [C(BPU)] = {
  2001. [C(OP_READ)] = {
  2002. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  2003. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  2004. },
  2005. [C(OP_WRITE)] = {
  2006. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  2007. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  2008. },
  2009. [C(OP_PREFETCH)] = {
  2010. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  2011. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  2012. },
  2013. },
  2014. };
  2015. #define XSCALE_PMU_ENABLE 0x001
  2016. #define XSCALE_PMN_RESET 0x002
  2017. #define XSCALE_CCNT_RESET 0x004
  2018. #define XSCALE_PMU_RESET (CCNT_RESET | PMN_RESET)
  2019. #define XSCALE_PMU_CNT64 0x008
  2020. static inline int
  2021. xscalepmu_event_map(int config)
  2022. {
  2023. int mapping = xscale_perf_map[config];
  2024. if (HW_OP_UNSUPPORTED == mapping)
  2025. mapping = -EOPNOTSUPP;
  2026. return mapping;
  2027. }
  2028. static u64
  2029. xscalepmu_raw_event(u64 config)
  2030. {
  2031. return config & 0xff;
  2032. }
  2033. #define XSCALE1_OVERFLOWED_MASK 0x700
  2034. #define XSCALE1_CCOUNT_OVERFLOW 0x400
  2035. #define XSCALE1_COUNT0_OVERFLOW 0x100
  2036. #define XSCALE1_COUNT1_OVERFLOW 0x200
  2037. #define XSCALE1_CCOUNT_INT_EN 0x040
  2038. #define XSCALE1_COUNT0_INT_EN 0x010
  2039. #define XSCALE1_COUNT1_INT_EN 0x020
  2040. #define XSCALE1_COUNT0_EVT_SHFT 12
  2041. #define XSCALE1_COUNT0_EVT_MASK (0xff << XSCALE1_COUNT0_EVT_SHFT)
  2042. #define XSCALE1_COUNT1_EVT_SHFT 20
  2043. #define XSCALE1_COUNT1_EVT_MASK (0xff << XSCALE1_COUNT1_EVT_SHFT)
  2044. static inline u32
  2045. xscale1pmu_read_pmnc(void)
  2046. {
  2047. u32 val;
  2048. asm volatile("mrc p14, 0, %0, c0, c0, 0" : "=r" (val));
  2049. return val;
  2050. }
  2051. static inline void
  2052. xscale1pmu_write_pmnc(u32 val)
  2053. {
  2054. /* upper 4bits and 7, 11 are write-as-0 */
  2055. val &= 0xffff77f;
  2056. asm volatile("mcr p14, 0, %0, c0, c0, 0" : : "r" (val));
  2057. }
  2058. static inline int
  2059. xscale1_pmnc_counter_has_overflowed(unsigned long pmnc,
  2060. enum xscale_counters counter)
  2061. {
  2062. int ret = 0;
  2063. switch (counter) {
  2064. case XSCALE_CYCLE_COUNTER:
  2065. ret = pmnc & XSCALE1_CCOUNT_OVERFLOW;
  2066. break;
  2067. case XSCALE_COUNTER0:
  2068. ret = pmnc & XSCALE1_COUNT0_OVERFLOW;
  2069. break;
  2070. case XSCALE_COUNTER1:
  2071. ret = pmnc & XSCALE1_COUNT1_OVERFLOW;
  2072. break;
  2073. default:
  2074. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  2075. }
  2076. return ret;
  2077. }
  2078. static irqreturn_t
  2079. xscale1pmu_handle_irq(int irq_num, void *dev)
  2080. {
  2081. unsigned long pmnc;
  2082. struct perf_sample_data data;
  2083. struct cpu_hw_events *cpuc;
  2084. struct pt_regs *regs;
  2085. int idx;
  2086. /*
  2087. * NOTE: there's an A stepping erratum that states if an overflow
  2088. * bit already exists and another occurs, the previous
  2089. * Overflow bit gets cleared. There's no workaround.
  2090. * Fixed in B stepping or later.
  2091. */
  2092. pmnc = xscale1pmu_read_pmnc();
  2093. /*
  2094. * Write the value back to clear the overflow flags. Overflow
  2095. * flags remain in pmnc for use below. We also disable the PMU
  2096. * while we process the interrupt.
  2097. */
  2098. xscale1pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
  2099. if (!(pmnc & XSCALE1_OVERFLOWED_MASK))
  2100. return IRQ_NONE;
  2101. regs = get_irq_regs();
  2102. perf_sample_data_init(&data, 0);
  2103. cpuc = &__get_cpu_var(cpu_hw_events);
  2104. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  2105. struct perf_event *event = cpuc->events[idx];
  2106. struct hw_perf_event *hwc;
  2107. if (!test_bit(idx, cpuc->active_mask))
  2108. continue;
  2109. if (!xscale1_pmnc_counter_has_overflowed(pmnc, idx))
  2110. continue;
  2111. hwc = &event->hw;
  2112. armpmu_event_update(event, hwc, idx);
  2113. data.period = event->hw.last_period;
  2114. if (!armpmu_event_set_period(event, hwc, idx))
  2115. continue;
  2116. if (perf_event_overflow(event, 0, &data, regs))
  2117. armpmu->disable(hwc, idx);
  2118. }
  2119. irq_work_run();
  2120. /*
  2121. * Re-enable the PMU.
  2122. */
  2123. pmnc = xscale1pmu_read_pmnc() | XSCALE_PMU_ENABLE;
  2124. xscale1pmu_write_pmnc(pmnc);
  2125. return IRQ_HANDLED;
  2126. }
  2127. static void
  2128. xscale1pmu_enable_event(struct hw_perf_event *hwc, int idx)
  2129. {
  2130. unsigned long val, mask, evt, flags;
  2131. switch (idx) {
  2132. case XSCALE_CYCLE_COUNTER:
  2133. mask = 0;
  2134. evt = XSCALE1_CCOUNT_INT_EN;
  2135. break;
  2136. case XSCALE_COUNTER0:
  2137. mask = XSCALE1_COUNT0_EVT_MASK;
  2138. evt = (hwc->config_base << XSCALE1_COUNT0_EVT_SHFT) |
  2139. XSCALE1_COUNT0_INT_EN;
  2140. break;
  2141. case XSCALE_COUNTER1:
  2142. mask = XSCALE1_COUNT1_EVT_MASK;
  2143. evt = (hwc->config_base << XSCALE1_COUNT1_EVT_SHFT) |
  2144. XSCALE1_COUNT1_INT_EN;
  2145. break;
  2146. default:
  2147. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  2148. return;
  2149. }
  2150. spin_lock_irqsave(&pmu_lock, flags);
  2151. val = xscale1pmu_read_pmnc();
  2152. val &= ~mask;
  2153. val |= evt;
  2154. xscale1pmu_write_pmnc(val);
  2155. spin_unlock_irqrestore(&pmu_lock, flags);
  2156. }
  2157. static void
  2158. xscale1pmu_disable_event(struct hw_perf_event *hwc, int idx)
  2159. {
  2160. unsigned long val, mask, evt, flags;
  2161. switch (idx) {
  2162. case XSCALE_CYCLE_COUNTER:
  2163. mask = XSCALE1_CCOUNT_INT_EN;
  2164. evt = 0;
  2165. break;
  2166. case XSCALE_COUNTER0:
  2167. mask = XSCALE1_COUNT0_INT_EN | XSCALE1_COUNT0_EVT_MASK;
  2168. evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT0_EVT_SHFT;
  2169. break;
  2170. case XSCALE_COUNTER1:
  2171. mask = XSCALE1_COUNT1_INT_EN | XSCALE1_COUNT1_EVT_MASK;
  2172. evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT1_EVT_SHFT;
  2173. break;
  2174. default:
  2175. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  2176. return;
  2177. }
  2178. spin_lock_irqsave(&pmu_lock, flags);
  2179. val = xscale1pmu_read_pmnc();
  2180. val &= ~mask;
  2181. val |= evt;
  2182. xscale1pmu_write_pmnc(val);
  2183. spin_unlock_irqrestore(&pmu_lock, flags);
  2184. }
  2185. static int
  2186. xscale1pmu_get_event_idx(struct cpu_hw_events *cpuc,
  2187. struct hw_perf_event *event)
  2188. {
  2189. if (XSCALE_PERFCTR_CCNT == event->config_base) {
  2190. if (test_and_set_bit(XSCALE_CYCLE_COUNTER, cpuc->used_mask))
  2191. return -EAGAIN;
  2192. return XSCALE_CYCLE_COUNTER;
  2193. } else {
  2194. if (!test_and_set_bit(XSCALE_COUNTER1, cpuc->used_mask)) {
  2195. return XSCALE_COUNTER1;
  2196. }
  2197. if (!test_and_set_bit(XSCALE_COUNTER0, cpuc->used_mask)) {
  2198. return XSCALE_COUNTER0;
  2199. }
  2200. return -EAGAIN;
  2201. }
  2202. }
  2203. static void
  2204. xscale1pmu_start(void)
  2205. {
  2206. unsigned long flags, val;
  2207. spin_lock_irqsave(&pmu_lock, flags);
  2208. val = xscale1pmu_read_pmnc();
  2209. val |= XSCALE_PMU_ENABLE;
  2210. xscale1pmu_write_pmnc(val);
  2211. spin_unlock_irqrestore(&pmu_lock, flags);
  2212. }
  2213. static void
  2214. xscale1pmu_stop(void)
  2215. {
  2216. unsigned long flags, val;
  2217. spin_lock_irqsave(&pmu_lock, flags);
  2218. val = xscale1pmu_read_pmnc();
  2219. val &= ~XSCALE_PMU_ENABLE;
  2220. xscale1pmu_write_pmnc(val);
  2221. spin_unlock_irqrestore(&pmu_lock, flags);
  2222. }
  2223. static inline u32
  2224. xscale1pmu_read_counter(int counter)
  2225. {
  2226. u32 val = 0;
  2227. switch (counter) {
  2228. case XSCALE_CYCLE_COUNTER:
  2229. asm volatile("mrc p14, 0, %0, c1, c0, 0" : "=r" (val));
  2230. break;
  2231. case XSCALE_COUNTER0:
  2232. asm volatile("mrc p14, 0, %0, c2, c0, 0" : "=r" (val));
  2233. break;
  2234. case XSCALE_COUNTER1:
  2235. asm volatile("mrc p14, 0, %0, c3, c0, 0" : "=r" (val));
  2236. break;
  2237. }
  2238. return val;
  2239. }
  2240. static inline void
  2241. xscale1pmu_write_counter(int counter, u32 val)
  2242. {
  2243. switch (counter) {
  2244. case XSCALE_CYCLE_COUNTER:
  2245. asm volatile("mcr p14, 0, %0, c1, c0, 0" : : "r" (val));
  2246. break;
  2247. case XSCALE_COUNTER0:
  2248. asm volatile("mcr p14, 0, %0, c2, c0, 0" : : "r" (val));
  2249. break;
  2250. case XSCALE_COUNTER1:
  2251. asm volatile("mcr p14, 0, %0, c3, c0, 0" : : "r" (val));
  2252. break;
  2253. }
  2254. }
  2255. static const struct arm_pmu xscale1pmu = {
  2256. .id = ARM_PERF_PMU_ID_XSCALE1,
  2257. .handle_irq = xscale1pmu_handle_irq,
  2258. .enable = xscale1pmu_enable_event,
  2259. .disable = xscale1pmu_disable_event,
  2260. .event_map = xscalepmu_event_map,
  2261. .raw_event = xscalepmu_raw_event,
  2262. .read_counter = xscale1pmu_read_counter,
  2263. .write_counter = xscale1pmu_write_counter,
  2264. .get_event_idx = xscale1pmu_get_event_idx,
  2265. .start = xscale1pmu_start,
  2266. .stop = xscale1pmu_stop,
  2267. .num_events = 3,
  2268. .max_period = (1LLU << 32) - 1,
  2269. };
  2270. #define XSCALE2_OVERFLOWED_MASK 0x01f
  2271. #define XSCALE2_CCOUNT_OVERFLOW 0x001
  2272. #define XSCALE2_COUNT0_OVERFLOW 0x002
  2273. #define XSCALE2_COUNT1_OVERFLOW 0x004
  2274. #define XSCALE2_COUNT2_OVERFLOW 0x008
  2275. #define XSCALE2_COUNT3_OVERFLOW 0x010
  2276. #define XSCALE2_CCOUNT_INT_EN 0x001
  2277. #define XSCALE2_COUNT0_INT_EN 0x002
  2278. #define XSCALE2_COUNT1_INT_EN 0x004
  2279. #define XSCALE2_COUNT2_INT_EN 0x008
  2280. #define XSCALE2_COUNT3_INT_EN 0x010
  2281. #define XSCALE2_COUNT0_EVT_SHFT 0
  2282. #define XSCALE2_COUNT0_EVT_MASK (0xff << XSCALE2_COUNT0_EVT_SHFT)
  2283. #define XSCALE2_COUNT1_EVT_SHFT 8
  2284. #define XSCALE2_COUNT1_EVT_MASK (0xff << XSCALE2_COUNT1_EVT_SHFT)
  2285. #define XSCALE2_COUNT2_EVT_SHFT 16
  2286. #define XSCALE2_COUNT2_EVT_MASK (0xff << XSCALE2_COUNT2_EVT_SHFT)
  2287. #define XSCALE2_COUNT3_EVT_SHFT 24
  2288. #define XSCALE2_COUNT3_EVT_MASK (0xff << XSCALE2_COUNT3_EVT_SHFT)
  2289. static inline u32
  2290. xscale2pmu_read_pmnc(void)
  2291. {
  2292. u32 val;
  2293. asm volatile("mrc p14, 0, %0, c0, c1, 0" : "=r" (val));
  2294. /* bits 1-2 and 4-23 are read-unpredictable */
  2295. return val & 0xff000009;
  2296. }
  2297. static inline void
  2298. xscale2pmu_write_pmnc(u32 val)
  2299. {
  2300. /* bits 4-23 are write-as-0, 24-31 are write ignored */
  2301. val &= 0xf;
  2302. asm volatile("mcr p14, 0, %0, c0, c1, 0" : : "r" (val));
  2303. }
  2304. static inline u32
  2305. xscale2pmu_read_overflow_flags(void)
  2306. {
  2307. u32 val;
  2308. asm volatile("mrc p14, 0, %0, c5, c1, 0" : "=r" (val));
  2309. return val;
  2310. }
  2311. static inline void
  2312. xscale2pmu_write_overflow_flags(u32 val)
  2313. {
  2314. asm volatile("mcr p14, 0, %0, c5, c1, 0" : : "r" (val));
  2315. }
  2316. static inline u32
  2317. xscale2pmu_read_event_select(void)
  2318. {
  2319. u32 val;
  2320. asm volatile("mrc p14, 0, %0, c8, c1, 0" : "=r" (val));
  2321. return val;
  2322. }
  2323. static inline void
  2324. xscale2pmu_write_event_select(u32 val)
  2325. {
  2326. asm volatile("mcr p14, 0, %0, c8, c1, 0" : : "r"(val));
  2327. }
  2328. static inline u32
  2329. xscale2pmu_read_int_enable(void)
  2330. {
  2331. u32 val;
  2332. asm volatile("mrc p14, 0, %0, c4, c1, 0" : "=r" (val));
  2333. return val;
  2334. }
  2335. static void
  2336. xscale2pmu_write_int_enable(u32 val)
  2337. {
  2338. asm volatile("mcr p14, 0, %0, c4, c1, 0" : : "r" (val));
  2339. }
  2340. static inline int
  2341. xscale2_pmnc_counter_has_overflowed(unsigned long of_flags,
  2342. enum xscale_counters counter)
  2343. {
  2344. int ret = 0;
  2345. switch (counter) {
  2346. case XSCALE_CYCLE_COUNTER:
  2347. ret = of_flags & XSCALE2_CCOUNT_OVERFLOW;
  2348. break;
  2349. case XSCALE_COUNTER0:
  2350. ret = of_flags & XSCALE2_COUNT0_OVERFLOW;
  2351. break;
  2352. case XSCALE_COUNTER1:
  2353. ret = of_flags & XSCALE2_COUNT1_OVERFLOW;
  2354. break;
  2355. case XSCALE_COUNTER2:
  2356. ret = of_flags & XSCALE2_COUNT2_OVERFLOW;
  2357. break;
  2358. case XSCALE_COUNTER3:
  2359. ret = of_flags & XSCALE2_COUNT3_OVERFLOW;
  2360. break;
  2361. default:
  2362. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  2363. }
  2364. return ret;
  2365. }
  2366. static irqreturn_t
  2367. xscale2pmu_handle_irq(int irq_num, void *dev)
  2368. {
  2369. unsigned long pmnc, of_flags;
  2370. struct perf_sample_data data;
  2371. struct cpu_hw_events *cpuc;
  2372. struct pt_regs *regs;
  2373. int idx;
  2374. /* Disable the PMU. */
  2375. pmnc = xscale2pmu_read_pmnc();
  2376. xscale2pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
  2377. /* Check the overflow flag register. */
  2378. of_flags = xscale2pmu_read_overflow_flags();
  2379. if (!(of_flags & XSCALE2_OVERFLOWED_MASK))
  2380. return IRQ_NONE;
  2381. /* Clear the overflow bits. */
  2382. xscale2pmu_write_overflow_flags(of_flags);
  2383. regs = get_irq_regs();
  2384. perf_sample_data_init(&data, 0);
  2385. cpuc = &__get_cpu_var(cpu_hw_events);
  2386. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  2387. struct perf_event *event = cpuc->events[idx];
  2388. struct hw_perf_event *hwc;
  2389. if (!test_bit(idx, cpuc->active_mask))
  2390. continue;
  2391. if (!xscale2_pmnc_counter_has_overflowed(pmnc, idx))
  2392. continue;
  2393. hwc = &event->hw;
  2394. armpmu_event_update(event, hwc, idx);
  2395. data.period = event->hw.last_period;
  2396. if (!armpmu_event_set_period(event, hwc, idx))
  2397. continue;
  2398. if (perf_event_overflow(event, 0, &data, regs))
  2399. armpmu->disable(hwc, idx);
  2400. }
  2401. irq_work_run();
  2402. /*
  2403. * Re-enable the PMU.
  2404. */
  2405. pmnc = xscale2pmu_read_pmnc() | XSCALE_PMU_ENABLE;
  2406. xscale2pmu_write_pmnc(pmnc);
  2407. return IRQ_HANDLED;
  2408. }
  2409. static void
  2410. xscale2pmu_enable_event(struct hw_perf_event *hwc, int idx)
  2411. {
  2412. unsigned long flags, ien, evtsel;
  2413. ien = xscale2pmu_read_int_enable();
  2414. evtsel = xscale2pmu_read_event_select();
  2415. switch (idx) {
  2416. case XSCALE_CYCLE_COUNTER:
  2417. ien |= XSCALE2_CCOUNT_INT_EN;
  2418. break;
  2419. case XSCALE_COUNTER0:
  2420. ien |= XSCALE2_COUNT0_INT_EN;
  2421. evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
  2422. evtsel |= hwc->config_base << XSCALE2_COUNT0_EVT_SHFT;
  2423. break;
  2424. case XSCALE_COUNTER1:
  2425. ien |= XSCALE2_COUNT1_INT_EN;
  2426. evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
  2427. evtsel |= hwc->config_base << XSCALE2_COUNT1_EVT_SHFT;
  2428. break;
  2429. case XSCALE_COUNTER2:
  2430. ien |= XSCALE2_COUNT2_INT_EN;
  2431. evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
  2432. evtsel |= hwc->config_base << XSCALE2_COUNT2_EVT_SHFT;
  2433. break;
  2434. case XSCALE_COUNTER3:
  2435. ien |= XSCALE2_COUNT3_INT_EN;
  2436. evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
  2437. evtsel |= hwc->config_base << XSCALE2_COUNT3_EVT_SHFT;
  2438. break;
  2439. default:
  2440. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  2441. return;
  2442. }
  2443. spin_lock_irqsave(&pmu_lock, flags);
  2444. xscale2pmu_write_event_select(evtsel);
  2445. xscale2pmu_write_int_enable(ien);
  2446. spin_unlock_irqrestore(&pmu_lock, flags);
  2447. }
  2448. static void
  2449. xscale2pmu_disable_event(struct hw_perf_event *hwc, int idx)
  2450. {
  2451. unsigned long flags, ien, evtsel;
  2452. ien = xscale2pmu_read_int_enable();
  2453. evtsel = xscale2pmu_read_event_select();
  2454. switch (idx) {
  2455. case XSCALE_CYCLE_COUNTER:
  2456. ien &= ~XSCALE2_CCOUNT_INT_EN;
  2457. break;
  2458. case XSCALE_COUNTER0:
  2459. ien &= ~XSCALE2_COUNT0_INT_EN;
  2460. evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
  2461. evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT0_EVT_SHFT;
  2462. break;
  2463. case XSCALE_COUNTER1:
  2464. ien &= ~XSCALE2_COUNT1_INT_EN;
  2465. evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
  2466. evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT1_EVT_SHFT;
  2467. break;
  2468. case XSCALE_COUNTER2:
  2469. ien &= ~XSCALE2_COUNT2_INT_EN;
  2470. evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
  2471. evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT2_EVT_SHFT;
  2472. break;
  2473. case XSCALE_COUNTER3:
  2474. ien &= ~XSCALE2_COUNT3_INT_EN;
  2475. evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
  2476. evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT3_EVT_SHFT;
  2477. break;
  2478. default:
  2479. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  2480. return;
  2481. }
  2482. spin_lock_irqsave(&pmu_lock, flags);
  2483. xscale2pmu_write_event_select(evtsel);
  2484. xscale2pmu_write_int_enable(ien);
  2485. spin_unlock_irqrestore(&pmu_lock, flags);
  2486. }
  2487. static int
  2488. xscale2pmu_get_event_idx(struct cpu_hw_events *cpuc,
  2489. struct hw_perf_event *event)
  2490. {
  2491. int idx = xscale1pmu_get_event_idx(cpuc, event);
  2492. if (idx >= 0)
  2493. goto out;
  2494. if (!test_and_set_bit(XSCALE_COUNTER3, cpuc->used_mask))
  2495. idx = XSCALE_COUNTER3;
  2496. else if (!test_and_set_bit(XSCALE_COUNTER2, cpuc->used_mask))
  2497. idx = XSCALE_COUNTER2;
  2498. out:
  2499. return idx;
  2500. }
  2501. static void
  2502. xscale2pmu_start(void)
  2503. {
  2504. unsigned long flags, val;
  2505. spin_lock_irqsave(&pmu_lock, flags);
  2506. val = xscale2pmu_read_pmnc() & ~XSCALE_PMU_CNT64;
  2507. val |= XSCALE_PMU_ENABLE;
  2508. xscale2pmu_write_pmnc(val);
  2509. spin_unlock_irqrestore(&pmu_lock, flags);
  2510. }
  2511. static void
  2512. xscale2pmu_stop(void)
  2513. {
  2514. unsigned long flags, val;
  2515. spin_lock_irqsave(&pmu_lock, flags);
  2516. val = xscale2pmu_read_pmnc();
  2517. val &= ~XSCALE_PMU_ENABLE;
  2518. xscale2pmu_write_pmnc(val);
  2519. spin_unlock_irqrestore(&pmu_lock, flags);
  2520. }
  2521. static inline u32
  2522. xscale2pmu_read_counter(int counter)
  2523. {
  2524. u32 val = 0;
  2525. switch (counter) {
  2526. case XSCALE_CYCLE_COUNTER:
  2527. asm volatile("mrc p14, 0, %0, c1, c1, 0" : "=r" (val));
  2528. break;
  2529. case XSCALE_COUNTER0:
  2530. asm volatile("mrc p14, 0, %0, c0, c2, 0" : "=r" (val));
  2531. break;
  2532. case XSCALE_COUNTER1:
  2533. asm volatile("mrc p14, 0, %0, c1, c2, 0" : "=r" (val));
  2534. break;
  2535. case XSCALE_COUNTER2:
  2536. asm volatile("mrc p14, 0, %0, c2, c2, 0" : "=r" (val));
  2537. break;
  2538. case XSCALE_COUNTER3:
  2539. asm volatile("mrc p14, 0, %0, c3, c2, 0" : "=r" (val));
  2540. break;
  2541. }
  2542. return val;
  2543. }
  2544. static inline void
  2545. xscale2pmu_write_counter(int counter, u32 val)
  2546. {
  2547. switch (counter) {
  2548. case XSCALE_CYCLE_COUNTER:
  2549. asm volatile("mcr p14, 0, %0, c1, c1, 0" : : "r" (val));
  2550. break;
  2551. case XSCALE_COUNTER0:
  2552. asm volatile("mcr p14, 0, %0, c0, c2, 0" : : "r" (val));
  2553. break;
  2554. case XSCALE_COUNTER1:
  2555. asm volatile("mcr p14, 0, %0, c1, c2, 0" : : "r" (val));
  2556. break;
  2557. case XSCALE_COUNTER2:
  2558. asm volatile("mcr p14, 0, %0, c2, c2, 0" : : "r" (val));
  2559. break;
  2560. case XSCALE_COUNTER3:
  2561. asm volatile("mcr p14, 0, %0, c3, c2, 0" : : "r" (val));
  2562. break;
  2563. }
  2564. }
  2565. static const struct arm_pmu xscale2pmu = {
  2566. .id = ARM_PERF_PMU_ID_XSCALE2,
  2567. .handle_irq = xscale2pmu_handle_irq,
  2568. .enable = xscale2pmu_enable_event,
  2569. .disable = xscale2pmu_disable_event,
  2570. .event_map = xscalepmu_event_map,
  2571. .raw_event = xscalepmu_raw_event,
  2572. .read_counter = xscale2pmu_read_counter,
  2573. .write_counter = xscale2pmu_write_counter,
  2574. .get_event_idx = xscale2pmu_get_event_idx,
  2575. .start = xscale2pmu_start,
  2576. .stop = xscale2pmu_stop,
  2577. .num_events = 5,
  2578. .max_period = (1LLU << 32) - 1,
  2579. };
  2580. static int __init
  2581. init_hw_perf_events(void)
  2582. {
  2583. unsigned long cpuid = read_cpuid_id();
  2584. unsigned long implementor = (cpuid & 0xFF000000) >> 24;
  2585. unsigned long part_number = (cpuid & 0xFFF0);
  2586. /* ARM Ltd CPUs. */
  2587. if (0x41 == implementor) {
  2588. switch (part_number) {
  2589. case 0xB360: /* ARM1136 */
  2590. case 0xB560: /* ARM1156 */
  2591. case 0xB760: /* ARM1176 */
  2592. armpmu = &armv6pmu;
  2593. memcpy(armpmu_perf_cache_map, armv6_perf_cache_map,
  2594. sizeof(armv6_perf_cache_map));
  2595. break;
  2596. case 0xB020: /* ARM11mpcore */
  2597. armpmu = &armv6mpcore_pmu;
  2598. memcpy(armpmu_perf_cache_map,
  2599. armv6mpcore_perf_cache_map,
  2600. sizeof(armv6mpcore_perf_cache_map));
  2601. break;
  2602. case 0xC080: /* Cortex-A8 */
  2603. armv7pmu.id = ARM_PERF_PMU_ID_CA8;
  2604. memcpy(armpmu_perf_cache_map, armv7_a8_perf_cache_map,
  2605. sizeof(armv7_a8_perf_cache_map));
  2606. armv7pmu.event_map = armv7_a8_pmu_event_map;
  2607. armpmu = &armv7pmu;
  2608. /* Reset PMNC and read the nb of CNTx counters
  2609. supported */
  2610. armv7pmu.num_events = armv7_reset_read_pmnc();
  2611. break;
  2612. case 0xC090: /* Cortex-A9 */
  2613. armv7pmu.id = ARM_PERF_PMU_ID_CA9;
  2614. memcpy(armpmu_perf_cache_map, armv7_a9_perf_cache_map,
  2615. sizeof(armv7_a9_perf_cache_map));
  2616. armv7pmu.event_map = armv7_a9_pmu_event_map;
  2617. armpmu = &armv7pmu;
  2618. /* Reset PMNC and read the nb of CNTx counters
  2619. supported */
  2620. armv7pmu.num_events = armv7_reset_read_pmnc();
  2621. break;
  2622. }
  2623. /* Intel CPUs [xscale]. */
  2624. } else if (0x69 == implementor) {
  2625. part_number = (cpuid >> 13) & 0x7;
  2626. switch (part_number) {
  2627. case 1:
  2628. armpmu = &xscale1pmu;
  2629. memcpy(armpmu_perf_cache_map, xscale_perf_cache_map,
  2630. sizeof(xscale_perf_cache_map));
  2631. break;
  2632. case 2:
  2633. armpmu = &xscale2pmu;
  2634. memcpy(armpmu_perf_cache_map, xscale_perf_cache_map,
  2635. sizeof(xscale_perf_cache_map));
  2636. break;
  2637. }
  2638. }
  2639. if (armpmu) {
  2640. pr_info("enabled with %s PMU driver, %d counters available\n",
  2641. arm_pmu_names[armpmu->id], armpmu->num_events);
  2642. } else {
  2643. pr_info("no hardware support available\n");
  2644. }
  2645. perf_pmu_register(&pmu);
  2646. return 0;
  2647. }
  2648. arch_initcall(init_hw_perf_events);
  2649. /*
  2650. * Callchain handling code.
  2651. */
  2652. /*
  2653. * The registers we're interested in are at the end of the variable
  2654. * length saved register structure. The fp points at the end of this
  2655. * structure so the address of this struct is:
  2656. * (struct frame_tail *)(xxx->fp)-1
  2657. *
  2658. * This code has been adapted from the ARM OProfile support.
  2659. */
  2660. struct frame_tail {
  2661. struct frame_tail *fp;
  2662. unsigned long sp;
  2663. unsigned long lr;
  2664. } __attribute__((packed));
  2665. /*
  2666. * Get the return address for a single stackframe and return a pointer to the
  2667. * next frame tail.
  2668. */
  2669. static struct frame_tail *
  2670. user_backtrace(struct frame_tail *tail,
  2671. struct perf_callchain_entry *entry)
  2672. {
  2673. struct frame_tail buftail;
  2674. /* Also check accessibility of one struct frame_tail beyond */
  2675. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  2676. return NULL;
  2677. if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
  2678. return NULL;
  2679. perf_callchain_store(entry, buftail.lr);
  2680. /*
  2681. * Frame pointers should strictly progress back up the stack
  2682. * (towards higher addresses).
  2683. */
  2684. if (tail >= buftail.fp)
  2685. return NULL;
  2686. return buftail.fp - 1;
  2687. }
  2688. void
  2689. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  2690. {
  2691. struct frame_tail *tail;
  2692. tail = (struct frame_tail *)regs->ARM_fp - 1;
  2693. while (tail && !((unsigned long)tail & 0x3))
  2694. tail = user_backtrace(tail, entry);
  2695. }
  2696. /*
  2697. * Gets called by walk_stackframe() for every stackframe. This will be called
  2698. * whist unwinding the stackframe and is like a subroutine return so we use
  2699. * the PC.
  2700. */
  2701. static int
  2702. callchain_trace(struct stackframe *fr,
  2703. void *data)
  2704. {
  2705. struct perf_callchain_entry *entry = data;
  2706. perf_callchain_store(entry, fr->pc);
  2707. return 0;
  2708. }
  2709. void
  2710. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  2711. {
  2712. struct stackframe fr;
  2713. fr.fp = regs->ARM_fp;
  2714. fr.sp = regs->ARM_sp;
  2715. fr.lr = regs->ARM_lr;
  2716. fr.pc = regs->ARM_pc;
  2717. walk_stackframe(&fr, callchain_trace, entry);
  2718. }