perf_event.c 79 KB

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