perf_event.c 79 KB

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