perf_event.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Performance event support for sparc64.
  3. *
  4. * Copyright (C) 2009, 2010 David S. Miller <davem@davemloft.net>
  5. *
  6. * This code is based almost entirely upon the x86 perf event
  7. * code, which is:
  8. *
  9. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  10. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  11. * Copyright (C) 2009 Jaswinder Singh Rajput
  12. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  13. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra
  14. */
  15. #include <linux/perf_event.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/ftrace.h>
  18. #include <linux/kernel.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/mutex.h>
  21. #include <asm/stacktrace.h>
  22. #include <asm/cpudata.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/atomic.h>
  25. #include <asm/nmi.h>
  26. #include <asm/pcr.h>
  27. #include <asm/cacheflush.h>
  28. #include "kernel.h"
  29. #include "kstack.h"
  30. /* Two classes of sparc64 chips currently exist. All of which have
  31. * 32-bit counters which can generate overflow interrupts on the
  32. * transition from 0xffffffff to 0.
  33. *
  34. * All chips upto and including SPARC-T3 have two performance
  35. * counters. The two 32-bit counters are accessed in one go using a
  36. * single 64-bit register.
  37. *
  38. * On these older chips both counters are controlled using a single
  39. * control register. The only way to stop all sampling is to clear
  40. * all of the context (user, supervisor, hypervisor) sampling enable
  41. * bits. But these bits apply to both counters, thus the two counters
  42. * can't be enabled/disabled individually.
  43. *
  44. * Furthermore, the control register on these older chips have two
  45. * event fields, one for each of the two counters. It's thus nearly
  46. * impossible to have one counter going while keeping the other one
  47. * stopped. Therefore it is possible to get overflow interrupts for
  48. * counters not currently "in use" and that condition must be checked
  49. * in the overflow interrupt handler.
  50. *
  51. * So we use a hack, in that we program inactive counters with the
  52. * "sw_count0" and "sw_count1" events. These count how many times
  53. * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
  54. * unusual way to encode a NOP and therefore will not trigger in
  55. * normal code.
  56. *
  57. * Starting with SPARC-T4 we have one control register per counter.
  58. * And the counters are stored in individual registers. The registers
  59. * for the counters are 64-bit but only a 32-bit counter is
  60. * implemented. The event selections on SPARC-T4 lack any
  61. * restrictions, therefore we can elide all of the complicated
  62. * conflict resolution code we have for SPARC-T3 and earlier chips.
  63. */
  64. #define MAX_HWEVENTS 4
  65. #define MAX_PCRS 4
  66. #define MAX_PERIOD ((1UL << 32) - 1)
  67. #define PIC_UPPER_INDEX 0
  68. #define PIC_LOWER_INDEX 1
  69. #define PIC_NO_INDEX -1
  70. struct cpu_hw_events {
  71. /* Number of events currently scheduled onto this cpu.
  72. * This tells how many entries in the arrays below
  73. * are valid.
  74. */
  75. int n_events;
  76. /* Number of new events added since the last hw_perf_disable().
  77. * This works because the perf event layer always adds new
  78. * events inside of a perf_{disable,enable}() sequence.
  79. */
  80. int n_added;
  81. /* Array of events current scheduled on this cpu. */
  82. struct perf_event *event[MAX_HWEVENTS];
  83. /* Array of encoded longs, specifying the %pcr register
  84. * encoding and the mask of PIC counters this even can
  85. * be scheduled on. See perf_event_encode() et al.
  86. */
  87. unsigned long events[MAX_HWEVENTS];
  88. /* The current counter index assigned to an event. When the
  89. * event hasn't been programmed into the cpu yet, this will
  90. * hold PIC_NO_INDEX. The event->hw.idx value tells us where
  91. * we ought to schedule the event.
  92. */
  93. int current_idx[MAX_HWEVENTS];
  94. /* Software copy of %pcr register(s) on this cpu. */
  95. u64 pcr[MAX_HWEVENTS];
  96. /* Enabled/disable state. */
  97. int enabled;
  98. unsigned int txn_flags;
  99. };
  100. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
  101. /* An event map describes the characteristics of a performance
  102. * counter event. In particular it gives the encoding as well as
  103. * a mask telling which counters the event can be measured on.
  104. *
  105. * The mask is unused on SPARC-T4 and later.
  106. */
  107. struct perf_event_map {
  108. u16 encoding;
  109. u8 pic_mask;
  110. #define PIC_NONE 0x00
  111. #define PIC_UPPER 0x01
  112. #define PIC_LOWER 0x02
  113. };
  114. /* Encode a perf_event_map entry into a long. */
  115. static unsigned long perf_event_encode(const struct perf_event_map *pmap)
  116. {
  117. return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
  118. }
  119. static u8 perf_event_get_msk(unsigned long val)
  120. {
  121. return val & 0xff;
  122. }
  123. static u64 perf_event_get_enc(unsigned long val)
  124. {
  125. return val >> 16;
  126. }
  127. #define C(x) PERF_COUNT_HW_CACHE_##x
  128. #define CACHE_OP_UNSUPPORTED 0xfffe
  129. #define CACHE_OP_NONSENSE 0xffff
  130. typedef struct perf_event_map cache_map_t
  131. [PERF_COUNT_HW_CACHE_MAX]
  132. [PERF_COUNT_HW_CACHE_OP_MAX]
  133. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  134. struct sparc_pmu {
  135. const struct perf_event_map *(*event_map)(int);
  136. const cache_map_t *cache_map;
  137. int max_events;
  138. u32 (*read_pmc)(int);
  139. void (*write_pmc)(int, u64);
  140. int upper_shift;
  141. int lower_shift;
  142. int event_mask;
  143. int user_bit;
  144. int priv_bit;
  145. int hv_bit;
  146. int irq_bit;
  147. int upper_nop;
  148. int lower_nop;
  149. unsigned int flags;
  150. #define SPARC_PMU_ALL_EXCLUDES_SAME 0x00000001
  151. #define SPARC_PMU_HAS_CONFLICTS 0x00000002
  152. int max_hw_events;
  153. int num_pcrs;
  154. int num_pic_regs;
  155. };
  156. static u32 sparc_default_read_pmc(int idx)
  157. {
  158. u64 val;
  159. val = pcr_ops->read_pic(0);
  160. if (idx == PIC_UPPER_INDEX)
  161. val >>= 32;
  162. return val & 0xffffffff;
  163. }
  164. static void sparc_default_write_pmc(int idx, u64 val)
  165. {
  166. u64 shift, mask, pic;
  167. shift = 0;
  168. if (idx == PIC_UPPER_INDEX)
  169. shift = 32;
  170. mask = ((u64) 0xffffffff) << shift;
  171. val <<= shift;
  172. pic = pcr_ops->read_pic(0);
  173. pic &= ~mask;
  174. pic |= val;
  175. pcr_ops->write_pic(0, pic);
  176. }
  177. static const struct perf_event_map ultra3_perfmon_event_map[] = {
  178. [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
  179. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
  180. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
  181. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
  182. };
  183. static const struct perf_event_map *ultra3_event_map(int event_id)
  184. {
  185. return &ultra3_perfmon_event_map[event_id];
  186. }
  187. static const cache_map_t ultra3_cache_map = {
  188. [C(L1D)] = {
  189. [C(OP_READ)] = {
  190. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  191. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  192. },
  193. [C(OP_WRITE)] = {
  194. [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
  195. [C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
  196. },
  197. [C(OP_PREFETCH)] = {
  198. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  199. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  200. },
  201. },
  202. [C(L1I)] = {
  203. [C(OP_READ)] = {
  204. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  205. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  206. },
  207. [ C(OP_WRITE) ] = {
  208. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  209. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  210. },
  211. [ C(OP_PREFETCH) ] = {
  212. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  213. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  214. },
  215. },
  216. [C(LL)] = {
  217. [C(OP_READ)] = {
  218. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
  219. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
  220. },
  221. [C(OP_WRITE)] = {
  222. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
  223. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
  224. },
  225. [C(OP_PREFETCH)] = {
  226. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  227. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  228. },
  229. },
  230. [C(DTLB)] = {
  231. [C(OP_READ)] = {
  232. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  233. [C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
  234. },
  235. [ C(OP_WRITE) ] = {
  236. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  237. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  238. },
  239. [ C(OP_PREFETCH) ] = {
  240. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  241. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  242. },
  243. },
  244. [C(ITLB)] = {
  245. [C(OP_READ)] = {
  246. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  247. [C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
  248. },
  249. [ C(OP_WRITE) ] = {
  250. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  251. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  252. },
  253. [ C(OP_PREFETCH) ] = {
  254. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  255. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  256. },
  257. },
  258. [C(BPU)] = {
  259. [C(OP_READ)] = {
  260. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  261. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  262. },
  263. [ C(OP_WRITE) ] = {
  264. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  265. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  266. },
  267. [ C(OP_PREFETCH) ] = {
  268. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  269. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  270. },
  271. },
  272. [C(NODE)] = {
  273. [C(OP_READ)] = {
  274. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  275. [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  276. },
  277. [ C(OP_WRITE) ] = {
  278. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  279. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  280. },
  281. [ C(OP_PREFETCH) ] = {
  282. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  283. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  284. },
  285. },
  286. };
  287. static const struct sparc_pmu ultra3_pmu = {
  288. .event_map = ultra3_event_map,
  289. .cache_map = &ultra3_cache_map,
  290. .max_events = ARRAY_SIZE(ultra3_perfmon_event_map),
  291. .read_pmc = sparc_default_read_pmc,
  292. .write_pmc = sparc_default_write_pmc,
  293. .upper_shift = 11,
  294. .lower_shift = 4,
  295. .event_mask = 0x3f,
  296. .user_bit = PCR_UTRACE,
  297. .priv_bit = PCR_STRACE,
  298. .upper_nop = 0x1c,
  299. .lower_nop = 0x14,
  300. .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
  301. SPARC_PMU_HAS_CONFLICTS),
  302. .max_hw_events = 2,
  303. .num_pcrs = 1,
  304. .num_pic_regs = 1,
  305. };
  306. /* Niagara1 is very limited. The upper PIC is hard-locked to count
  307. * only instructions, so it is free running which creates all kinds of
  308. * problems. Some hardware designs make one wonder if the creator
  309. * even looked at how this stuff gets used by software.
  310. */
  311. static const struct perf_event_map niagara1_perfmon_event_map[] = {
  312. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER },
  313. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER },
  314. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE },
  315. [PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER },
  316. };
  317. static const struct perf_event_map *niagara1_event_map(int event_id)
  318. {
  319. return &niagara1_perfmon_event_map[event_id];
  320. }
  321. static const cache_map_t niagara1_cache_map = {
  322. [C(L1D)] = {
  323. [C(OP_READ)] = {
  324. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  325. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  326. },
  327. [C(OP_WRITE)] = {
  328. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  329. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  330. },
  331. [C(OP_PREFETCH)] = {
  332. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  333. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  334. },
  335. },
  336. [C(L1I)] = {
  337. [C(OP_READ)] = {
  338. [C(RESULT_ACCESS)] = { 0x00, PIC_UPPER },
  339. [C(RESULT_MISS)] = { 0x02, PIC_LOWER, },
  340. },
  341. [ C(OP_WRITE) ] = {
  342. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  343. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  344. },
  345. [ C(OP_PREFETCH) ] = {
  346. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  347. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  348. },
  349. },
  350. [C(LL)] = {
  351. [C(OP_READ)] = {
  352. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  353. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  354. },
  355. [C(OP_WRITE)] = {
  356. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  357. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  358. },
  359. [C(OP_PREFETCH)] = {
  360. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  361. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  362. },
  363. },
  364. [C(DTLB)] = {
  365. [C(OP_READ)] = {
  366. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  367. [C(RESULT_MISS)] = { 0x05, PIC_LOWER, },
  368. },
  369. [ C(OP_WRITE) ] = {
  370. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  371. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  372. },
  373. [ C(OP_PREFETCH) ] = {
  374. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  375. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  376. },
  377. },
  378. [C(ITLB)] = {
  379. [C(OP_READ)] = {
  380. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  381. [C(RESULT_MISS)] = { 0x04, PIC_LOWER, },
  382. },
  383. [ C(OP_WRITE) ] = {
  384. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  385. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  386. },
  387. [ C(OP_PREFETCH) ] = {
  388. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  389. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  390. },
  391. },
  392. [C(BPU)] = {
  393. [C(OP_READ)] = {
  394. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  395. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  396. },
  397. [ C(OP_WRITE) ] = {
  398. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  399. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  400. },
  401. [ C(OP_PREFETCH) ] = {
  402. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  403. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  404. },
  405. },
  406. [C(NODE)] = {
  407. [C(OP_READ)] = {
  408. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  409. [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  410. },
  411. [ C(OP_WRITE) ] = {
  412. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  413. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  414. },
  415. [ C(OP_PREFETCH) ] = {
  416. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  417. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  418. },
  419. },
  420. };
  421. static const struct sparc_pmu niagara1_pmu = {
  422. .event_map = niagara1_event_map,
  423. .cache_map = &niagara1_cache_map,
  424. .max_events = ARRAY_SIZE(niagara1_perfmon_event_map),
  425. .read_pmc = sparc_default_read_pmc,
  426. .write_pmc = sparc_default_write_pmc,
  427. .upper_shift = 0,
  428. .lower_shift = 4,
  429. .event_mask = 0x7,
  430. .user_bit = PCR_UTRACE,
  431. .priv_bit = PCR_STRACE,
  432. .upper_nop = 0x0,
  433. .lower_nop = 0x0,
  434. .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
  435. SPARC_PMU_HAS_CONFLICTS),
  436. .max_hw_events = 2,
  437. .num_pcrs = 1,
  438. .num_pic_regs = 1,
  439. };
  440. static const struct perf_event_map niagara2_perfmon_event_map[] = {
  441. [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  442. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  443. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
  444. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
  445. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
  446. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
  447. };
  448. static const struct perf_event_map *niagara2_event_map(int event_id)
  449. {
  450. return &niagara2_perfmon_event_map[event_id];
  451. }
  452. static const cache_map_t niagara2_cache_map = {
  453. [C(L1D)] = {
  454. [C(OP_READ)] = {
  455. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  456. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  457. },
  458. [C(OP_WRITE)] = {
  459. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  460. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  461. },
  462. [C(OP_PREFETCH)] = {
  463. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  464. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  465. },
  466. },
  467. [C(L1I)] = {
  468. [C(OP_READ)] = {
  469. [C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, },
  470. [C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, },
  471. },
  472. [ C(OP_WRITE) ] = {
  473. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  474. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  475. },
  476. [ C(OP_PREFETCH) ] = {
  477. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  478. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  479. },
  480. },
  481. [C(LL)] = {
  482. [C(OP_READ)] = {
  483. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  484. [C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, },
  485. },
  486. [C(OP_WRITE)] = {
  487. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  488. [C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, },
  489. },
  490. [C(OP_PREFETCH)] = {
  491. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  492. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  493. },
  494. },
  495. [C(DTLB)] = {
  496. [C(OP_READ)] = {
  497. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  498. [C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, },
  499. },
  500. [ C(OP_WRITE) ] = {
  501. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  502. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  503. },
  504. [ C(OP_PREFETCH) ] = {
  505. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  506. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  507. },
  508. },
  509. [C(ITLB)] = {
  510. [C(OP_READ)] = {
  511. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  512. [C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, },
  513. },
  514. [ C(OP_WRITE) ] = {
  515. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  516. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  517. },
  518. [ C(OP_PREFETCH) ] = {
  519. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  520. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  521. },
  522. },
  523. [C(BPU)] = {
  524. [C(OP_READ)] = {
  525. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  526. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  527. },
  528. [ C(OP_WRITE) ] = {
  529. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  530. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  531. },
  532. [ C(OP_PREFETCH) ] = {
  533. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  534. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  535. },
  536. },
  537. [C(NODE)] = {
  538. [C(OP_READ)] = {
  539. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  540. [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  541. },
  542. [ C(OP_WRITE) ] = {
  543. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  544. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  545. },
  546. [ C(OP_PREFETCH) ] = {
  547. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  548. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  549. },
  550. },
  551. };
  552. static const struct sparc_pmu niagara2_pmu = {
  553. .event_map = niagara2_event_map,
  554. .cache_map = &niagara2_cache_map,
  555. .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
  556. .read_pmc = sparc_default_read_pmc,
  557. .write_pmc = sparc_default_write_pmc,
  558. .upper_shift = 19,
  559. .lower_shift = 6,
  560. .event_mask = 0xfff,
  561. .user_bit = PCR_UTRACE,
  562. .priv_bit = PCR_STRACE,
  563. .hv_bit = PCR_N2_HTRACE,
  564. .irq_bit = 0x30,
  565. .upper_nop = 0x220,
  566. .lower_nop = 0x220,
  567. .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
  568. SPARC_PMU_HAS_CONFLICTS),
  569. .max_hw_events = 2,
  570. .num_pcrs = 1,
  571. .num_pic_regs = 1,
  572. };
  573. static const struct perf_event_map niagara4_perfmon_event_map[] = {
  574. [PERF_COUNT_HW_CPU_CYCLES] = { (26 << 6) },
  575. [PERF_COUNT_HW_INSTRUCTIONS] = { (3 << 6) | 0x3f },
  576. [PERF_COUNT_HW_CACHE_REFERENCES] = { (3 << 6) | 0x04 },
  577. [PERF_COUNT_HW_CACHE_MISSES] = { (16 << 6) | 0x07 },
  578. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { (4 << 6) | 0x01 },
  579. [PERF_COUNT_HW_BRANCH_MISSES] = { (25 << 6) | 0x0f },
  580. };
  581. static const struct perf_event_map *niagara4_event_map(int event_id)
  582. {
  583. return &niagara4_perfmon_event_map[event_id];
  584. }
  585. static const cache_map_t niagara4_cache_map = {
  586. [C(L1D)] = {
  587. [C(OP_READ)] = {
  588. [C(RESULT_ACCESS)] = { (3 << 6) | 0x04 },
  589. [C(RESULT_MISS)] = { (16 << 6) | 0x07 },
  590. },
  591. [C(OP_WRITE)] = {
  592. [C(RESULT_ACCESS)] = { (3 << 6) | 0x08 },
  593. [C(RESULT_MISS)] = { (16 << 6) | 0x07 },
  594. },
  595. [C(OP_PREFETCH)] = {
  596. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  597. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  598. },
  599. },
  600. [C(L1I)] = {
  601. [C(OP_READ)] = {
  602. [C(RESULT_ACCESS)] = { (3 << 6) | 0x3f },
  603. [C(RESULT_MISS)] = { (11 << 6) | 0x03 },
  604. },
  605. [ C(OP_WRITE) ] = {
  606. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  607. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  608. },
  609. [ C(OP_PREFETCH) ] = {
  610. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  611. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  612. },
  613. },
  614. [C(LL)] = {
  615. [C(OP_READ)] = {
  616. [C(RESULT_ACCESS)] = { (3 << 6) | 0x04 },
  617. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  618. },
  619. [C(OP_WRITE)] = {
  620. [C(RESULT_ACCESS)] = { (3 << 6) | 0x08 },
  621. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  622. },
  623. [C(OP_PREFETCH)] = {
  624. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  625. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  626. },
  627. },
  628. [C(DTLB)] = {
  629. [C(OP_READ)] = {
  630. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  631. [C(RESULT_MISS)] = { (17 << 6) | 0x3f },
  632. },
  633. [ C(OP_WRITE) ] = {
  634. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  635. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  636. },
  637. [ C(OP_PREFETCH) ] = {
  638. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  639. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  640. },
  641. },
  642. [C(ITLB)] = {
  643. [C(OP_READ)] = {
  644. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  645. [C(RESULT_MISS)] = { (6 << 6) | 0x3f },
  646. },
  647. [ C(OP_WRITE) ] = {
  648. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  649. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  650. },
  651. [ C(OP_PREFETCH) ] = {
  652. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  653. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  654. },
  655. },
  656. [C(BPU)] = {
  657. [C(OP_READ)] = {
  658. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  659. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  660. },
  661. [ C(OP_WRITE) ] = {
  662. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  663. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  664. },
  665. [ C(OP_PREFETCH) ] = {
  666. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  667. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  668. },
  669. },
  670. [C(NODE)] = {
  671. [C(OP_READ)] = {
  672. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  673. [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  674. },
  675. [ C(OP_WRITE) ] = {
  676. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  677. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  678. },
  679. [ C(OP_PREFETCH) ] = {
  680. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  681. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  682. },
  683. },
  684. };
  685. static u32 sparc_vt_read_pmc(int idx)
  686. {
  687. u64 val = pcr_ops->read_pic(idx);
  688. return val & 0xffffffff;
  689. }
  690. static void sparc_vt_write_pmc(int idx, u64 val)
  691. {
  692. u64 pcr;
  693. pcr = pcr_ops->read_pcr(idx);
  694. /* ensure ov and ntc are reset */
  695. pcr &= ~(PCR_N4_OV | PCR_N4_NTC);
  696. pcr_ops->write_pic(idx, val & 0xffffffff);
  697. pcr_ops->write_pcr(idx, pcr);
  698. }
  699. static const struct sparc_pmu niagara4_pmu = {
  700. .event_map = niagara4_event_map,
  701. .cache_map = &niagara4_cache_map,
  702. .max_events = ARRAY_SIZE(niagara4_perfmon_event_map),
  703. .read_pmc = sparc_vt_read_pmc,
  704. .write_pmc = sparc_vt_write_pmc,
  705. .upper_shift = 5,
  706. .lower_shift = 5,
  707. .event_mask = 0x7ff,
  708. .user_bit = PCR_N4_UTRACE,
  709. .priv_bit = PCR_N4_STRACE,
  710. /* We explicitly don't support hypervisor tracing. The T4
  711. * generates the overflow event for precise events via a trap
  712. * which will not be generated (ie. it's completely lost) if
  713. * we happen to be in the hypervisor when the event triggers.
  714. * Essentially, the overflow event reporting is completely
  715. * unusable when you have hypervisor mode tracing enabled.
  716. */
  717. .hv_bit = 0,
  718. .irq_bit = PCR_N4_TOE,
  719. .upper_nop = 0,
  720. .lower_nop = 0,
  721. .flags = 0,
  722. .max_hw_events = 4,
  723. .num_pcrs = 4,
  724. .num_pic_regs = 4,
  725. };
  726. static const struct sparc_pmu sparc_m7_pmu = {
  727. .event_map = niagara4_event_map,
  728. .cache_map = &niagara4_cache_map,
  729. .max_events = ARRAY_SIZE(niagara4_perfmon_event_map),
  730. .read_pmc = sparc_vt_read_pmc,
  731. .write_pmc = sparc_vt_write_pmc,
  732. .upper_shift = 5,
  733. .lower_shift = 5,
  734. .event_mask = 0x7ff,
  735. .user_bit = PCR_N4_UTRACE,
  736. .priv_bit = PCR_N4_STRACE,
  737. /* We explicitly don't support hypervisor tracing. */
  738. .hv_bit = 0,
  739. .irq_bit = PCR_N4_TOE,
  740. .upper_nop = 0,
  741. .lower_nop = 0,
  742. .flags = 0,
  743. .max_hw_events = 4,
  744. .num_pcrs = 4,
  745. .num_pic_regs = 4,
  746. };
  747. static const struct sparc_pmu *sparc_pmu __read_mostly;
  748. static u64 event_encoding(u64 event_id, int idx)
  749. {
  750. if (idx == PIC_UPPER_INDEX)
  751. event_id <<= sparc_pmu->upper_shift;
  752. else
  753. event_id <<= sparc_pmu->lower_shift;
  754. return event_id;
  755. }
  756. static u64 mask_for_index(int idx)
  757. {
  758. return event_encoding(sparc_pmu->event_mask, idx);
  759. }
  760. static u64 nop_for_index(int idx)
  761. {
  762. return event_encoding(idx == PIC_UPPER_INDEX ?
  763. sparc_pmu->upper_nop :
  764. sparc_pmu->lower_nop, idx);
  765. }
  766. static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  767. {
  768. u64 enc, val, mask = mask_for_index(idx);
  769. int pcr_index = 0;
  770. if (sparc_pmu->num_pcrs > 1)
  771. pcr_index = idx;
  772. enc = perf_event_get_enc(cpuc->events[idx]);
  773. val = cpuc->pcr[pcr_index];
  774. val &= ~mask;
  775. val |= event_encoding(enc, idx);
  776. cpuc->pcr[pcr_index] = val;
  777. pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]);
  778. }
  779. static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  780. {
  781. u64 mask = mask_for_index(idx);
  782. u64 nop = nop_for_index(idx);
  783. int pcr_index = 0;
  784. u64 val;
  785. if (sparc_pmu->num_pcrs > 1)
  786. pcr_index = idx;
  787. val = cpuc->pcr[pcr_index];
  788. val &= ~mask;
  789. val |= nop;
  790. cpuc->pcr[pcr_index] = val;
  791. pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]);
  792. }
  793. static u64 sparc_perf_event_update(struct perf_event *event,
  794. struct hw_perf_event *hwc, int idx)
  795. {
  796. int shift = 64 - 32;
  797. u64 prev_raw_count, new_raw_count;
  798. s64 delta;
  799. again:
  800. prev_raw_count = local64_read(&hwc->prev_count);
  801. new_raw_count = sparc_pmu->read_pmc(idx);
  802. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  803. new_raw_count) != prev_raw_count)
  804. goto again;
  805. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  806. delta >>= shift;
  807. local64_add(delta, &event->count);
  808. local64_sub(delta, &hwc->period_left);
  809. return new_raw_count;
  810. }
  811. static int sparc_perf_event_set_period(struct perf_event *event,
  812. struct hw_perf_event *hwc, int idx)
  813. {
  814. s64 left = local64_read(&hwc->period_left);
  815. s64 period = hwc->sample_period;
  816. int ret = 0;
  817. if (unlikely(left <= -period)) {
  818. left = period;
  819. local64_set(&hwc->period_left, left);
  820. hwc->last_period = period;
  821. ret = 1;
  822. }
  823. if (unlikely(left <= 0)) {
  824. left += period;
  825. local64_set(&hwc->period_left, left);
  826. hwc->last_period = period;
  827. ret = 1;
  828. }
  829. if (left > MAX_PERIOD)
  830. left = MAX_PERIOD;
  831. local64_set(&hwc->prev_count, (u64)-left);
  832. sparc_pmu->write_pmc(idx, (u64)(-left) & 0xffffffff);
  833. perf_event_update_userpage(event);
  834. return ret;
  835. }
  836. static void read_in_all_counters(struct cpu_hw_events *cpuc)
  837. {
  838. int i;
  839. for (i = 0; i < cpuc->n_events; i++) {
  840. struct perf_event *cp = cpuc->event[i];
  841. if (cpuc->current_idx[i] != PIC_NO_INDEX &&
  842. cpuc->current_idx[i] != cp->hw.idx) {
  843. sparc_perf_event_update(cp, &cp->hw,
  844. cpuc->current_idx[i]);
  845. cpuc->current_idx[i] = PIC_NO_INDEX;
  846. }
  847. }
  848. }
  849. /* On this PMU all PICs are programmed using a single PCR. Calculate
  850. * the combined control register value.
  851. *
  852. * For such chips we require that all of the events have the same
  853. * configuration, so just fetch the settings from the first entry.
  854. */
  855. static void calculate_single_pcr(struct cpu_hw_events *cpuc)
  856. {
  857. int i;
  858. if (!cpuc->n_added)
  859. goto out;
  860. /* Assign to counters all unassigned events. */
  861. for (i = 0; i < cpuc->n_events; i++) {
  862. struct perf_event *cp = cpuc->event[i];
  863. struct hw_perf_event *hwc = &cp->hw;
  864. int idx = hwc->idx;
  865. u64 enc;
  866. if (cpuc->current_idx[i] != PIC_NO_INDEX)
  867. continue;
  868. sparc_perf_event_set_period(cp, hwc, idx);
  869. cpuc->current_idx[i] = idx;
  870. enc = perf_event_get_enc(cpuc->events[i]);
  871. cpuc->pcr[0] &= ~mask_for_index(idx);
  872. if (hwc->state & PERF_HES_STOPPED)
  873. cpuc->pcr[0] |= nop_for_index(idx);
  874. else
  875. cpuc->pcr[0] |= event_encoding(enc, idx);
  876. }
  877. out:
  878. cpuc->pcr[0] |= cpuc->event[0]->hw.config_base;
  879. }
  880. static void sparc_pmu_start(struct perf_event *event, int flags);
  881. /* On this PMU each PIC has it's own PCR control register. */
  882. static void calculate_multiple_pcrs(struct cpu_hw_events *cpuc)
  883. {
  884. int i;
  885. if (!cpuc->n_added)
  886. goto out;
  887. for (i = 0; i < cpuc->n_events; i++) {
  888. struct perf_event *cp = cpuc->event[i];
  889. struct hw_perf_event *hwc = &cp->hw;
  890. int idx = hwc->idx;
  891. if (cpuc->current_idx[i] != PIC_NO_INDEX)
  892. continue;
  893. cpuc->current_idx[i] = idx;
  894. sparc_pmu_start(cp, PERF_EF_RELOAD);
  895. }
  896. out:
  897. for (i = 0; i < cpuc->n_events; i++) {
  898. struct perf_event *cp = cpuc->event[i];
  899. int idx = cp->hw.idx;
  900. cpuc->pcr[idx] |= cp->hw.config_base;
  901. }
  902. }
  903. /* If performance event entries have been added, move existing events
  904. * around (if necessary) and then assign new entries to counters.
  905. */
  906. static void update_pcrs_for_enable(struct cpu_hw_events *cpuc)
  907. {
  908. if (cpuc->n_added)
  909. read_in_all_counters(cpuc);
  910. if (sparc_pmu->num_pcrs == 1) {
  911. calculate_single_pcr(cpuc);
  912. } else {
  913. calculate_multiple_pcrs(cpuc);
  914. }
  915. }
  916. static void sparc_pmu_enable(struct pmu *pmu)
  917. {
  918. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  919. int i;
  920. if (cpuc->enabled)
  921. return;
  922. cpuc->enabled = 1;
  923. barrier();
  924. if (cpuc->n_events)
  925. update_pcrs_for_enable(cpuc);
  926. for (i = 0; i < sparc_pmu->num_pcrs; i++)
  927. pcr_ops->write_pcr(i, cpuc->pcr[i]);
  928. }
  929. static void sparc_pmu_disable(struct pmu *pmu)
  930. {
  931. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  932. int i;
  933. if (!cpuc->enabled)
  934. return;
  935. cpuc->enabled = 0;
  936. cpuc->n_added = 0;
  937. for (i = 0; i < sparc_pmu->num_pcrs; i++) {
  938. u64 val = cpuc->pcr[i];
  939. val &= ~(sparc_pmu->user_bit | sparc_pmu->priv_bit |
  940. sparc_pmu->hv_bit | sparc_pmu->irq_bit);
  941. cpuc->pcr[i] = val;
  942. pcr_ops->write_pcr(i, cpuc->pcr[i]);
  943. }
  944. }
  945. static int active_event_index(struct cpu_hw_events *cpuc,
  946. struct perf_event *event)
  947. {
  948. int i;
  949. for (i = 0; i < cpuc->n_events; i++) {
  950. if (cpuc->event[i] == event)
  951. break;
  952. }
  953. BUG_ON(i == cpuc->n_events);
  954. return cpuc->current_idx[i];
  955. }
  956. static void sparc_pmu_start(struct perf_event *event, int flags)
  957. {
  958. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  959. int idx = active_event_index(cpuc, event);
  960. if (flags & PERF_EF_RELOAD) {
  961. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  962. sparc_perf_event_set_period(event, &event->hw, idx);
  963. }
  964. event->hw.state = 0;
  965. sparc_pmu_enable_event(cpuc, &event->hw, idx);
  966. }
  967. static void sparc_pmu_stop(struct perf_event *event, int flags)
  968. {
  969. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  970. int idx = active_event_index(cpuc, event);
  971. if (!(event->hw.state & PERF_HES_STOPPED)) {
  972. sparc_pmu_disable_event(cpuc, &event->hw, idx);
  973. event->hw.state |= PERF_HES_STOPPED;
  974. }
  975. if (!(event->hw.state & PERF_HES_UPTODATE) && (flags & PERF_EF_UPDATE)) {
  976. sparc_perf_event_update(event, &event->hw, idx);
  977. event->hw.state |= PERF_HES_UPTODATE;
  978. }
  979. }
  980. static void sparc_pmu_del(struct perf_event *event, int _flags)
  981. {
  982. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  983. unsigned long flags;
  984. int i;
  985. local_irq_save(flags);
  986. for (i = 0; i < cpuc->n_events; i++) {
  987. if (event == cpuc->event[i]) {
  988. /* Absorb the final count and turn off the
  989. * event.
  990. */
  991. sparc_pmu_stop(event, PERF_EF_UPDATE);
  992. /* Shift remaining entries down into
  993. * the existing slot.
  994. */
  995. while (++i < cpuc->n_events) {
  996. cpuc->event[i - 1] = cpuc->event[i];
  997. cpuc->events[i - 1] = cpuc->events[i];
  998. cpuc->current_idx[i - 1] =
  999. cpuc->current_idx[i];
  1000. }
  1001. perf_event_update_userpage(event);
  1002. cpuc->n_events--;
  1003. break;
  1004. }
  1005. }
  1006. local_irq_restore(flags);
  1007. }
  1008. static void sparc_pmu_read(struct perf_event *event)
  1009. {
  1010. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1011. int idx = active_event_index(cpuc, event);
  1012. struct hw_perf_event *hwc = &event->hw;
  1013. sparc_perf_event_update(event, hwc, idx);
  1014. }
  1015. static atomic_t active_events = ATOMIC_INIT(0);
  1016. static DEFINE_MUTEX(pmc_grab_mutex);
  1017. static void perf_stop_nmi_watchdog(void *unused)
  1018. {
  1019. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1020. int i;
  1021. stop_nmi_watchdog(NULL);
  1022. for (i = 0; i < sparc_pmu->num_pcrs; i++)
  1023. cpuc->pcr[i] = pcr_ops->read_pcr(i);
  1024. }
  1025. static void perf_event_grab_pmc(void)
  1026. {
  1027. if (atomic_inc_not_zero(&active_events))
  1028. return;
  1029. mutex_lock(&pmc_grab_mutex);
  1030. if (atomic_read(&active_events) == 0) {
  1031. if (atomic_read(&nmi_active) > 0) {
  1032. on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
  1033. BUG_ON(atomic_read(&nmi_active) != 0);
  1034. }
  1035. atomic_inc(&active_events);
  1036. }
  1037. mutex_unlock(&pmc_grab_mutex);
  1038. }
  1039. static void perf_event_release_pmc(void)
  1040. {
  1041. if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
  1042. if (atomic_read(&nmi_active) == 0)
  1043. on_each_cpu(start_nmi_watchdog, NULL, 1);
  1044. mutex_unlock(&pmc_grab_mutex);
  1045. }
  1046. }
  1047. static const struct perf_event_map *sparc_map_cache_event(u64 config)
  1048. {
  1049. unsigned int cache_type, cache_op, cache_result;
  1050. const struct perf_event_map *pmap;
  1051. if (!sparc_pmu->cache_map)
  1052. return ERR_PTR(-ENOENT);
  1053. cache_type = (config >> 0) & 0xff;
  1054. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  1055. return ERR_PTR(-EINVAL);
  1056. cache_op = (config >> 8) & 0xff;
  1057. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  1058. return ERR_PTR(-EINVAL);
  1059. cache_result = (config >> 16) & 0xff;
  1060. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  1061. return ERR_PTR(-EINVAL);
  1062. pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
  1063. if (pmap->encoding == CACHE_OP_UNSUPPORTED)
  1064. return ERR_PTR(-ENOENT);
  1065. if (pmap->encoding == CACHE_OP_NONSENSE)
  1066. return ERR_PTR(-EINVAL);
  1067. return pmap;
  1068. }
  1069. static void hw_perf_event_destroy(struct perf_event *event)
  1070. {
  1071. perf_event_release_pmc();
  1072. }
  1073. /* Make sure all events can be scheduled into the hardware at
  1074. * the same time. This is simplified by the fact that we only
  1075. * need to support 2 simultaneous HW events.
  1076. *
  1077. * As a side effect, the evts[]->hw.idx values will be assigned
  1078. * on success. These are pending indexes. When the events are
  1079. * actually programmed into the chip, these values will propagate
  1080. * to the per-cpu cpuc->current_idx[] slots, see the code in
  1081. * maybe_change_configuration() for details.
  1082. */
  1083. static int sparc_check_constraints(struct perf_event **evts,
  1084. unsigned long *events, int n_ev)
  1085. {
  1086. u8 msk0 = 0, msk1 = 0;
  1087. int idx0 = 0;
  1088. /* This case is possible when we are invoked from
  1089. * hw_perf_group_sched_in().
  1090. */
  1091. if (!n_ev)
  1092. return 0;
  1093. if (n_ev > sparc_pmu->max_hw_events)
  1094. return -1;
  1095. if (!(sparc_pmu->flags & SPARC_PMU_HAS_CONFLICTS)) {
  1096. int i;
  1097. for (i = 0; i < n_ev; i++)
  1098. evts[i]->hw.idx = i;
  1099. return 0;
  1100. }
  1101. msk0 = perf_event_get_msk(events[0]);
  1102. if (n_ev == 1) {
  1103. if (msk0 & PIC_LOWER)
  1104. idx0 = 1;
  1105. goto success;
  1106. }
  1107. BUG_ON(n_ev != 2);
  1108. msk1 = perf_event_get_msk(events[1]);
  1109. /* If both events can go on any counter, OK. */
  1110. if (msk0 == (PIC_UPPER | PIC_LOWER) &&
  1111. msk1 == (PIC_UPPER | PIC_LOWER))
  1112. goto success;
  1113. /* If one event is limited to a specific counter,
  1114. * and the other can go on both, OK.
  1115. */
  1116. if ((msk0 == PIC_UPPER || msk0 == PIC_LOWER) &&
  1117. msk1 == (PIC_UPPER | PIC_LOWER)) {
  1118. if (msk0 & PIC_LOWER)
  1119. idx0 = 1;
  1120. goto success;
  1121. }
  1122. if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
  1123. msk0 == (PIC_UPPER | PIC_LOWER)) {
  1124. if (msk1 & PIC_UPPER)
  1125. idx0 = 1;
  1126. goto success;
  1127. }
  1128. /* If the events are fixed to different counters, OK. */
  1129. if ((msk0 == PIC_UPPER && msk1 == PIC_LOWER) ||
  1130. (msk0 == PIC_LOWER && msk1 == PIC_UPPER)) {
  1131. if (msk0 & PIC_LOWER)
  1132. idx0 = 1;
  1133. goto success;
  1134. }
  1135. /* Otherwise, there is a conflict. */
  1136. return -1;
  1137. success:
  1138. evts[0]->hw.idx = idx0;
  1139. if (n_ev == 2)
  1140. evts[1]->hw.idx = idx0 ^ 1;
  1141. return 0;
  1142. }
  1143. static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
  1144. {
  1145. int eu = 0, ek = 0, eh = 0;
  1146. struct perf_event *event;
  1147. int i, n, first;
  1148. if (!(sparc_pmu->flags & SPARC_PMU_ALL_EXCLUDES_SAME))
  1149. return 0;
  1150. n = n_prev + n_new;
  1151. if (n <= 1)
  1152. return 0;
  1153. first = 1;
  1154. for (i = 0; i < n; i++) {
  1155. event = evts[i];
  1156. if (first) {
  1157. eu = event->attr.exclude_user;
  1158. ek = event->attr.exclude_kernel;
  1159. eh = event->attr.exclude_hv;
  1160. first = 0;
  1161. } else if (event->attr.exclude_user != eu ||
  1162. event->attr.exclude_kernel != ek ||
  1163. event->attr.exclude_hv != eh) {
  1164. return -EAGAIN;
  1165. }
  1166. }
  1167. return 0;
  1168. }
  1169. static int collect_events(struct perf_event *group, int max_count,
  1170. struct perf_event *evts[], unsigned long *events,
  1171. int *current_idx)
  1172. {
  1173. struct perf_event *event;
  1174. int n = 0;
  1175. if (!is_software_event(group)) {
  1176. if (n >= max_count)
  1177. return -1;
  1178. evts[n] = group;
  1179. events[n] = group->hw.event_base;
  1180. current_idx[n++] = PIC_NO_INDEX;
  1181. }
  1182. list_for_each_entry(event, &group->sibling_list, group_entry) {
  1183. if (!is_software_event(event) &&
  1184. event->state != PERF_EVENT_STATE_OFF) {
  1185. if (n >= max_count)
  1186. return -1;
  1187. evts[n] = event;
  1188. events[n] = event->hw.event_base;
  1189. current_idx[n++] = PIC_NO_INDEX;
  1190. }
  1191. }
  1192. return n;
  1193. }
  1194. static int sparc_pmu_add(struct perf_event *event, int ef_flags)
  1195. {
  1196. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1197. int n0, ret = -EAGAIN;
  1198. unsigned long flags;
  1199. local_irq_save(flags);
  1200. n0 = cpuc->n_events;
  1201. if (n0 >= sparc_pmu->max_hw_events)
  1202. goto out;
  1203. cpuc->event[n0] = event;
  1204. cpuc->events[n0] = event->hw.event_base;
  1205. cpuc->current_idx[n0] = PIC_NO_INDEX;
  1206. event->hw.state = PERF_HES_UPTODATE;
  1207. if (!(ef_flags & PERF_EF_START))
  1208. event->hw.state |= PERF_HES_STOPPED;
  1209. /*
  1210. * If group events scheduling transaction was started,
  1211. * skip the schedulability test here, it will be performed
  1212. * at commit time(->commit_txn) as a whole
  1213. */
  1214. if (cpuc->txn_flags & PERF_PMU_TXN_ADD)
  1215. goto nocheck;
  1216. if (check_excludes(cpuc->event, n0, 1))
  1217. goto out;
  1218. if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1))
  1219. goto out;
  1220. nocheck:
  1221. cpuc->n_events++;
  1222. cpuc->n_added++;
  1223. ret = 0;
  1224. out:
  1225. local_irq_restore(flags);
  1226. return ret;
  1227. }
  1228. static int sparc_pmu_event_init(struct perf_event *event)
  1229. {
  1230. struct perf_event_attr *attr = &event->attr;
  1231. struct perf_event *evts[MAX_HWEVENTS];
  1232. struct hw_perf_event *hwc = &event->hw;
  1233. unsigned long events[MAX_HWEVENTS];
  1234. int current_idx_dmy[MAX_HWEVENTS];
  1235. const struct perf_event_map *pmap;
  1236. int n;
  1237. if (atomic_read(&nmi_active) < 0)
  1238. return -ENODEV;
  1239. /* does not support taken branch sampling */
  1240. if (has_branch_stack(event))
  1241. return -EOPNOTSUPP;
  1242. switch (attr->type) {
  1243. case PERF_TYPE_HARDWARE:
  1244. if (attr->config >= sparc_pmu->max_events)
  1245. return -EINVAL;
  1246. pmap = sparc_pmu->event_map(attr->config);
  1247. break;
  1248. case PERF_TYPE_HW_CACHE:
  1249. pmap = sparc_map_cache_event(attr->config);
  1250. if (IS_ERR(pmap))
  1251. return PTR_ERR(pmap);
  1252. break;
  1253. case PERF_TYPE_RAW:
  1254. pmap = NULL;
  1255. break;
  1256. default:
  1257. return -ENOENT;
  1258. }
  1259. if (pmap) {
  1260. hwc->event_base = perf_event_encode(pmap);
  1261. } else {
  1262. /*
  1263. * User gives us "(encoding << 16) | pic_mask" for
  1264. * PERF_TYPE_RAW events.
  1265. */
  1266. hwc->event_base = attr->config;
  1267. }
  1268. /* We save the enable bits in the config_base. */
  1269. hwc->config_base = sparc_pmu->irq_bit;
  1270. if (!attr->exclude_user)
  1271. hwc->config_base |= sparc_pmu->user_bit;
  1272. if (!attr->exclude_kernel)
  1273. hwc->config_base |= sparc_pmu->priv_bit;
  1274. if (!attr->exclude_hv)
  1275. hwc->config_base |= sparc_pmu->hv_bit;
  1276. n = 0;
  1277. if (event->group_leader != event) {
  1278. n = collect_events(event->group_leader,
  1279. sparc_pmu->max_hw_events - 1,
  1280. evts, events, current_idx_dmy);
  1281. if (n < 0)
  1282. return -EINVAL;
  1283. }
  1284. events[n] = hwc->event_base;
  1285. evts[n] = event;
  1286. if (check_excludes(evts, n, 1))
  1287. return -EINVAL;
  1288. if (sparc_check_constraints(evts, events, n + 1))
  1289. return -EINVAL;
  1290. hwc->idx = PIC_NO_INDEX;
  1291. /* Try to do all error checking before this point, as unwinding
  1292. * state after grabbing the PMC is difficult.
  1293. */
  1294. perf_event_grab_pmc();
  1295. event->destroy = hw_perf_event_destroy;
  1296. if (!hwc->sample_period) {
  1297. hwc->sample_period = MAX_PERIOD;
  1298. hwc->last_period = hwc->sample_period;
  1299. local64_set(&hwc->period_left, hwc->sample_period);
  1300. }
  1301. return 0;
  1302. }
  1303. /*
  1304. * Start group events scheduling transaction
  1305. * Set the flag to make pmu::enable() not perform the
  1306. * schedulability test, it will be performed at commit time
  1307. */
  1308. static void sparc_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
  1309. {
  1310. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  1311. WARN_ON_ONCE(cpuhw->txn_flags); /* txn already in flight */
  1312. cpuhw->txn_flags = txn_flags;
  1313. if (txn_flags & ~PERF_PMU_TXN_ADD)
  1314. return;
  1315. perf_pmu_disable(pmu);
  1316. }
  1317. /*
  1318. * Stop group events scheduling transaction
  1319. * Clear the flag and pmu::enable() will perform the
  1320. * schedulability test.
  1321. */
  1322. static void sparc_pmu_cancel_txn(struct pmu *pmu)
  1323. {
  1324. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  1325. unsigned int txn_flags;
  1326. WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
  1327. txn_flags = cpuhw->txn_flags;
  1328. cpuhw->txn_flags = 0;
  1329. if (txn_flags & ~PERF_PMU_TXN_ADD)
  1330. return;
  1331. perf_pmu_enable(pmu);
  1332. }
  1333. /*
  1334. * Commit group events scheduling transaction
  1335. * Perform the group schedulability test as a whole
  1336. * Return 0 if success
  1337. */
  1338. static int sparc_pmu_commit_txn(struct pmu *pmu)
  1339. {
  1340. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1341. int n;
  1342. if (!sparc_pmu)
  1343. return -EINVAL;
  1344. WARN_ON_ONCE(!cpuc->txn_flags); /* no txn in flight */
  1345. if (cpuc->txn_flags & ~PERF_PMU_TXN_ADD) {
  1346. cpuc->txn_flags = 0;
  1347. return 0;
  1348. }
  1349. n = cpuc->n_events;
  1350. if (check_excludes(cpuc->event, 0, n))
  1351. return -EINVAL;
  1352. if (sparc_check_constraints(cpuc->event, cpuc->events, n))
  1353. return -EAGAIN;
  1354. cpuc->txn_flags = 0;
  1355. perf_pmu_enable(pmu);
  1356. return 0;
  1357. }
  1358. static struct pmu pmu = {
  1359. .pmu_enable = sparc_pmu_enable,
  1360. .pmu_disable = sparc_pmu_disable,
  1361. .event_init = sparc_pmu_event_init,
  1362. .add = sparc_pmu_add,
  1363. .del = sparc_pmu_del,
  1364. .start = sparc_pmu_start,
  1365. .stop = sparc_pmu_stop,
  1366. .read = sparc_pmu_read,
  1367. .start_txn = sparc_pmu_start_txn,
  1368. .cancel_txn = sparc_pmu_cancel_txn,
  1369. .commit_txn = sparc_pmu_commit_txn,
  1370. };
  1371. void perf_event_print_debug(void)
  1372. {
  1373. unsigned long flags;
  1374. int cpu, i;
  1375. if (!sparc_pmu)
  1376. return;
  1377. local_irq_save(flags);
  1378. cpu = smp_processor_id();
  1379. pr_info("\n");
  1380. for (i = 0; i < sparc_pmu->num_pcrs; i++)
  1381. pr_info("CPU#%d: PCR%d[%016llx]\n",
  1382. cpu, i, pcr_ops->read_pcr(i));
  1383. for (i = 0; i < sparc_pmu->num_pic_regs; i++)
  1384. pr_info("CPU#%d: PIC%d[%016llx]\n",
  1385. cpu, i, pcr_ops->read_pic(i));
  1386. local_irq_restore(flags);
  1387. }
  1388. static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
  1389. unsigned long cmd, void *__args)
  1390. {
  1391. struct die_args *args = __args;
  1392. struct perf_sample_data data;
  1393. struct cpu_hw_events *cpuc;
  1394. struct pt_regs *regs;
  1395. int i;
  1396. if (!atomic_read(&active_events))
  1397. return NOTIFY_DONE;
  1398. switch (cmd) {
  1399. case DIE_NMI:
  1400. break;
  1401. default:
  1402. return NOTIFY_DONE;
  1403. }
  1404. regs = args->regs;
  1405. cpuc = this_cpu_ptr(&cpu_hw_events);
  1406. /* If the PMU has the TOE IRQ enable bits, we need to do a
  1407. * dummy write to the %pcr to clear the overflow bits and thus
  1408. * the interrupt.
  1409. *
  1410. * Do this before we peek at the counters to determine
  1411. * overflow so we don't lose any events.
  1412. */
  1413. if (sparc_pmu->irq_bit &&
  1414. sparc_pmu->num_pcrs == 1)
  1415. pcr_ops->write_pcr(0, cpuc->pcr[0]);
  1416. for (i = 0; i < cpuc->n_events; i++) {
  1417. struct perf_event *event = cpuc->event[i];
  1418. int idx = cpuc->current_idx[i];
  1419. struct hw_perf_event *hwc;
  1420. u64 val;
  1421. if (sparc_pmu->irq_bit &&
  1422. sparc_pmu->num_pcrs > 1)
  1423. pcr_ops->write_pcr(idx, cpuc->pcr[idx]);
  1424. hwc = &event->hw;
  1425. val = sparc_perf_event_update(event, hwc, idx);
  1426. if (val & (1ULL << 31))
  1427. continue;
  1428. perf_sample_data_init(&data, 0, hwc->last_period);
  1429. if (!sparc_perf_event_set_period(event, hwc, idx))
  1430. continue;
  1431. if (perf_event_overflow(event, &data, regs))
  1432. sparc_pmu_stop(event, 0);
  1433. }
  1434. return NOTIFY_STOP;
  1435. }
  1436. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1437. .notifier_call = perf_event_nmi_handler,
  1438. };
  1439. static bool __init supported_pmu(void)
  1440. {
  1441. if (!strcmp(sparc_pmu_type, "ultra3") ||
  1442. !strcmp(sparc_pmu_type, "ultra3+") ||
  1443. !strcmp(sparc_pmu_type, "ultra3i") ||
  1444. !strcmp(sparc_pmu_type, "ultra4+")) {
  1445. sparc_pmu = &ultra3_pmu;
  1446. return true;
  1447. }
  1448. if (!strcmp(sparc_pmu_type, "niagara")) {
  1449. sparc_pmu = &niagara1_pmu;
  1450. return true;
  1451. }
  1452. if (!strcmp(sparc_pmu_type, "niagara2") ||
  1453. !strcmp(sparc_pmu_type, "niagara3")) {
  1454. sparc_pmu = &niagara2_pmu;
  1455. return true;
  1456. }
  1457. if (!strcmp(sparc_pmu_type, "niagara4") ||
  1458. !strcmp(sparc_pmu_type, "niagara5")) {
  1459. sparc_pmu = &niagara4_pmu;
  1460. return true;
  1461. }
  1462. if (!strcmp(sparc_pmu_type, "sparc-m7")) {
  1463. sparc_pmu = &sparc_m7_pmu;
  1464. return true;
  1465. }
  1466. return false;
  1467. }
  1468. static int __init init_hw_perf_events(void)
  1469. {
  1470. int err;
  1471. pr_info("Performance events: ");
  1472. err = pcr_arch_init();
  1473. if (err || !supported_pmu()) {
  1474. pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
  1475. return 0;
  1476. }
  1477. pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
  1478. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1479. register_die_notifier(&perf_event_nmi_notifier);
  1480. return 0;
  1481. }
  1482. pure_initcall(init_hw_perf_events);
  1483. void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
  1484. struct pt_regs *regs)
  1485. {
  1486. unsigned long ksp, fp;
  1487. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1488. int graph = 0;
  1489. #endif
  1490. stack_trace_flush();
  1491. perf_callchain_store(entry, regs->tpc);
  1492. ksp = regs->u_regs[UREG_I6];
  1493. fp = ksp + STACK_BIAS;
  1494. do {
  1495. struct sparc_stackf *sf;
  1496. struct pt_regs *regs;
  1497. unsigned long pc;
  1498. if (!kstack_valid(current_thread_info(), fp))
  1499. break;
  1500. sf = (struct sparc_stackf *) fp;
  1501. regs = (struct pt_regs *) (sf + 1);
  1502. if (kstack_is_trap_frame(current_thread_info(), regs)) {
  1503. if (user_mode(regs))
  1504. break;
  1505. pc = regs->tpc;
  1506. fp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1507. } else {
  1508. pc = sf->callers_pc;
  1509. fp = (unsigned long)sf->fp + STACK_BIAS;
  1510. }
  1511. perf_callchain_store(entry, pc);
  1512. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1513. if ((pc + 8UL) == (unsigned long) &return_to_handler) {
  1514. int index = current->curr_ret_stack;
  1515. if (current->ret_stack && index >= graph) {
  1516. pc = current->ret_stack[index - graph].ret;
  1517. perf_callchain_store(entry, pc);
  1518. graph++;
  1519. }
  1520. }
  1521. #endif
  1522. } while (entry->nr < entry->max_stack);
  1523. }
  1524. static inline int
  1525. valid_user_frame(const void __user *fp, unsigned long size)
  1526. {
  1527. /* addresses should be at least 4-byte aligned */
  1528. if (((unsigned long) fp) & 3)
  1529. return 0;
  1530. return (__range_not_ok(fp, size, TASK_SIZE) == 0);
  1531. }
  1532. static void perf_callchain_user_64(struct perf_callchain_entry_ctx *entry,
  1533. struct pt_regs *regs)
  1534. {
  1535. unsigned long ufp;
  1536. ufp = regs->u_regs[UREG_FP] + STACK_BIAS;
  1537. do {
  1538. struct sparc_stackf __user *usf;
  1539. struct sparc_stackf sf;
  1540. unsigned long pc;
  1541. usf = (struct sparc_stackf __user *)ufp;
  1542. if (!valid_user_frame(usf, sizeof(sf)))
  1543. break;
  1544. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1545. break;
  1546. pc = sf.callers_pc;
  1547. ufp = (unsigned long)sf.fp + STACK_BIAS;
  1548. perf_callchain_store(entry, pc);
  1549. } while (entry->nr < entry->max_stack);
  1550. }
  1551. static void perf_callchain_user_32(struct perf_callchain_entry_ctx *entry,
  1552. struct pt_regs *regs)
  1553. {
  1554. unsigned long ufp;
  1555. ufp = regs->u_regs[UREG_FP] & 0xffffffffUL;
  1556. do {
  1557. unsigned long pc;
  1558. if (thread32_stack_is_64bit(ufp)) {
  1559. struct sparc_stackf __user *usf;
  1560. struct sparc_stackf sf;
  1561. ufp += STACK_BIAS;
  1562. usf = (struct sparc_stackf __user *)ufp;
  1563. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1564. break;
  1565. pc = sf.callers_pc & 0xffffffff;
  1566. ufp = ((unsigned long) sf.fp) & 0xffffffff;
  1567. } else {
  1568. struct sparc_stackf32 __user *usf;
  1569. struct sparc_stackf32 sf;
  1570. usf = (struct sparc_stackf32 __user *)ufp;
  1571. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1572. break;
  1573. pc = sf.callers_pc;
  1574. ufp = (unsigned long)sf.fp;
  1575. }
  1576. perf_callchain_store(entry, pc);
  1577. } while (entry->nr < entry->max_stack);
  1578. }
  1579. void
  1580. perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
  1581. {
  1582. u64 saved_fault_address = current_thread_info()->fault_address;
  1583. u8 saved_fault_code = get_thread_fault_code();
  1584. mm_segment_t old_fs;
  1585. perf_callchain_store(entry, regs->tpc);
  1586. if (!current->mm)
  1587. return;
  1588. old_fs = get_fs();
  1589. set_fs(USER_DS);
  1590. flushw_user();
  1591. pagefault_disable();
  1592. if (test_thread_flag(TIF_32BIT))
  1593. perf_callchain_user_32(entry, regs);
  1594. else
  1595. perf_callchain_user_64(entry, regs);
  1596. pagefault_enable();
  1597. set_fs(old_fs);
  1598. set_thread_fault_code(saved_fault_code);
  1599. current_thread_info()->fault_address = saved_fault_address;
  1600. }