perf_event.c 44 KB

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