perf_event.c 59 KB

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