perf_event.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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 <asm/atomic.h>
  24. #include <asm/nmi.h>
  25. #include <asm/pcr.h>
  26. #include "kernel.h"
  27. #include "kstack.h"
  28. /* Sparc64 chips have two performance counters, 32-bits each, with
  29. * overflow interrupts generated on transition from 0xffffffff to 0.
  30. * The counters are accessed in one go using a 64-bit register.
  31. *
  32. * Both counters are controlled using a single control register. The
  33. * only way to stop all sampling is to clear all of the context (user,
  34. * supervisor, hypervisor) sampling enable bits. But these bits apply
  35. * to both counters, thus the two counters can't be enabled/disabled
  36. * individually.
  37. *
  38. * The control register has two event fields, one for each of the two
  39. * counters. It's thus nearly impossible to have one counter going
  40. * while keeping the other one stopped. Therefore it is possible to
  41. * get overflow interrupts for counters not currently "in use" and
  42. * that condition must be checked in the overflow interrupt handler.
  43. *
  44. * So we use a hack, in that we program inactive counters with the
  45. * "sw_count0" and "sw_count1" events. These count how many times
  46. * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
  47. * unusual way to encode a NOP and therefore will not trigger in
  48. * normal code.
  49. */
  50. #define MAX_HWEVENTS 2
  51. #define MAX_PERIOD ((1UL << 32) - 1)
  52. #define PIC_UPPER_INDEX 0
  53. #define PIC_LOWER_INDEX 1
  54. #define PIC_NO_INDEX -1
  55. struct cpu_hw_events {
  56. /* Number of events currently scheduled onto this cpu.
  57. * This tells how many entries in the arrays below
  58. * are valid.
  59. */
  60. int n_events;
  61. /* Number of new events added since the last hw_perf_disable().
  62. * This works because the perf event layer always adds new
  63. * events inside of a perf_{disable,enable}() sequence.
  64. */
  65. int n_added;
  66. /* Array of events current scheduled on this cpu. */
  67. struct perf_event *event[MAX_HWEVENTS];
  68. /* Array of encoded longs, specifying the %pcr register
  69. * encoding and the mask of PIC counters this even can
  70. * be scheduled on. See perf_event_encode() et al.
  71. */
  72. unsigned long events[MAX_HWEVENTS];
  73. /* The current counter index assigned to an event. When the
  74. * event hasn't been programmed into the cpu yet, this will
  75. * hold PIC_NO_INDEX. The event->hw.idx value tells us where
  76. * we ought to schedule the event.
  77. */
  78. int current_idx[MAX_HWEVENTS];
  79. /* Software copy of %pcr register on this cpu. */
  80. u64 pcr;
  81. /* Enabled/disable state. */
  82. int enabled;
  83. unsigned int group_flag;
  84. };
  85. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
  86. /* An event map describes the characteristics of a performance
  87. * counter event. In particular it gives the encoding as well as
  88. * a mask telling which counters the event can be measured on.
  89. */
  90. struct perf_event_map {
  91. u16 encoding;
  92. u8 pic_mask;
  93. #define PIC_NONE 0x00
  94. #define PIC_UPPER 0x01
  95. #define PIC_LOWER 0x02
  96. };
  97. /* Encode a perf_event_map entry into a long. */
  98. static unsigned long perf_event_encode(const struct perf_event_map *pmap)
  99. {
  100. return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
  101. }
  102. static u8 perf_event_get_msk(unsigned long val)
  103. {
  104. return val & 0xff;
  105. }
  106. static u64 perf_event_get_enc(unsigned long val)
  107. {
  108. return val >> 16;
  109. }
  110. #define C(x) PERF_COUNT_HW_CACHE_##x
  111. #define CACHE_OP_UNSUPPORTED 0xfffe
  112. #define CACHE_OP_NONSENSE 0xffff
  113. typedef struct perf_event_map cache_map_t
  114. [PERF_COUNT_HW_CACHE_MAX]
  115. [PERF_COUNT_HW_CACHE_OP_MAX]
  116. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  117. struct sparc_pmu {
  118. const struct perf_event_map *(*event_map)(int);
  119. const cache_map_t *cache_map;
  120. int max_events;
  121. int upper_shift;
  122. int lower_shift;
  123. int event_mask;
  124. int hv_bit;
  125. int irq_bit;
  126. int upper_nop;
  127. int lower_nop;
  128. };
  129. static const struct perf_event_map ultra3_perfmon_event_map[] = {
  130. [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
  131. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
  132. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
  133. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
  134. };
  135. static const struct perf_event_map *ultra3_event_map(int event_id)
  136. {
  137. return &ultra3_perfmon_event_map[event_id];
  138. }
  139. static const cache_map_t ultra3_cache_map = {
  140. [C(L1D)] = {
  141. [C(OP_READ)] = {
  142. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  143. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  144. },
  145. [C(OP_WRITE)] = {
  146. [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
  147. [C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
  148. },
  149. [C(OP_PREFETCH)] = {
  150. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  151. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  152. },
  153. },
  154. [C(L1I)] = {
  155. [C(OP_READ)] = {
  156. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  157. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  158. },
  159. [ C(OP_WRITE) ] = {
  160. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  161. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  162. },
  163. [ C(OP_PREFETCH) ] = {
  164. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  165. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  166. },
  167. },
  168. [C(LL)] = {
  169. [C(OP_READ)] = {
  170. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
  171. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
  172. },
  173. [C(OP_WRITE)] = {
  174. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
  175. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
  176. },
  177. [C(OP_PREFETCH)] = {
  178. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  179. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  180. },
  181. },
  182. [C(DTLB)] = {
  183. [C(OP_READ)] = {
  184. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  185. [C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
  186. },
  187. [ C(OP_WRITE) ] = {
  188. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  189. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  190. },
  191. [ C(OP_PREFETCH) ] = {
  192. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  193. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  194. },
  195. },
  196. [C(ITLB)] = {
  197. [C(OP_READ)] = {
  198. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  199. [C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
  200. },
  201. [ C(OP_WRITE) ] = {
  202. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  203. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  204. },
  205. [ C(OP_PREFETCH) ] = {
  206. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  207. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  208. },
  209. },
  210. [C(BPU)] = {
  211. [C(OP_READ)] = {
  212. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  213. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  214. },
  215. [ C(OP_WRITE) ] = {
  216. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  217. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  218. },
  219. [ C(OP_PREFETCH) ] = {
  220. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  221. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  222. },
  223. },
  224. };
  225. static const struct sparc_pmu ultra3_pmu = {
  226. .event_map = ultra3_event_map,
  227. .cache_map = &ultra3_cache_map,
  228. .max_events = ARRAY_SIZE(ultra3_perfmon_event_map),
  229. .upper_shift = 11,
  230. .lower_shift = 4,
  231. .event_mask = 0x3f,
  232. .upper_nop = 0x1c,
  233. .lower_nop = 0x14,
  234. };
  235. /* Niagara1 is very limited. The upper PIC is hard-locked to count
  236. * only instructions, so it is free running which creates all kinds of
  237. * problems. Some hardware designs make one wonder if the creator
  238. * even looked at how this stuff gets used by software.
  239. */
  240. static const struct perf_event_map niagara1_perfmon_event_map[] = {
  241. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER },
  242. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER },
  243. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE },
  244. [PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER },
  245. };
  246. static const struct perf_event_map *niagara1_event_map(int event_id)
  247. {
  248. return &niagara1_perfmon_event_map[event_id];
  249. }
  250. static const cache_map_t niagara1_cache_map = {
  251. [C(L1D)] = {
  252. [C(OP_READ)] = {
  253. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  254. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  255. },
  256. [C(OP_WRITE)] = {
  257. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  258. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  259. },
  260. [C(OP_PREFETCH)] = {
  261. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  262. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  263. },
  264. },
  265. [C(L1I)] = {
  266. [C(OP_READ)] = {
  267. [C(RESULT_ACCESS)] = { 0x00, PIC_UPPER },
  268. [C(RESULT_MISS)] = { 0x02, PIC_LOWER, },
  269. },
  270. [ C(OP_WRITE) ] = {
  271. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  272. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  273. },
  274. [ C(OP_PREFETCH) ] = {
  275. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  276. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  277. },
  278. },
  279. [C(LL)] = {
  280. [C(OP_READ)] = {
  281. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  282. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  283. },
  284. [C(OP_WRITE)] = {
  285. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  286. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  287. },
  288. [C(OP_PREFETCH)] = {
  289. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  290. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  291. },
  292. },
  293. [C(DTLB)] = {
  294. [C(OP_READ)] = {
  295. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  296. [C(RESULT_MISS)] = { 0x05, PIC_LOWER, },
  297. },
  298. [ C(OP_WRITE) ] = {
  299. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  300. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  301. },
  302. [ C(OP_PREFETCH) ] = {
  303. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  304. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  305. },
  306. },
  307. [C(ITLB)] = {
  308. [C(OP_READ)] = {
  309. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  310. [C(RESULT_MISS)] = { 0x04, PIC_LOWER, },
  311. },
  312. [ C(OP_WRITE) ] = {
  313. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  314. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  315. },
  316. [ C(OP_PREFETCH) ] = {
  317. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  318. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  319. },
  320. },
  321. [C(BPU)] = {
  322. [C(OP_READ)] = {
  323. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  324. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  325. },
  326. [ C(OP_WRITE) ] = {
  327. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  328. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  329. },
  330. [ C(OP_PREFETCH) ] = {
  331. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  332. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  333. },
  334. },
  335. };
  336. static const struct sparc_pmu niagara1_pmu = {
  337. .event_map = niagara1_event_map,
  338. .cache_map = &niagara1_cache_map,
  339. .max_events = ARRAY_SIZE(niagara1_perfmon_event_map),
  340. .upper_shift = 0,
  341. .lower_shift = 4,
  342. .event_mask = 0x7,
  343. .upper_nop = 0x0,
  344. .lower_nop = 0x0,
  345. };
  346. static const struct perf_event_map niagara2_perfmon_event_map[] = {
  347. [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  348. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  349. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
  350. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
  351. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
  352. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
  353. };
  354. static const struct perf_event_map *niagara2_event_map(int event_id)
  355. {
  356. return &niagara2_perfmon_event_map[event_id];
  357. }
  358. static const cache_map_t niagara2_cache_map = {
  359. [C(L1D)] = {
  360. [C(OP_READ)] = {
  361. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  362. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  363. },
  364. [C(OP_WRITE)] = {
  365. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  366. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  367. },
  368. [C(OP_PREFETCH)] = {
  369. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  370. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  371. },
  372. },
  373. [C(L1I)] = {
  374. [C(OP_READ)] = {
  375. [C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, },
  376. [C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, },
  377. },
  378. [ C(OP_WRITE) ] = {
  379. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  380. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  381. },
  382. [ C(OP_PREFETCH) ] = {
  383. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  384. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  385. },
  386. },
  387. [C(LL)] = {
  388. [C(OP_READ)] = {
  389. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  390. [C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, },
  391. },
  392. [C(OP_WRITE)] = {
  393. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  394. [C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, },
  395. },
  396. [C(OP_PREFETCH)] = {
  397. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  398. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  399. },
  400. },
  401. [C(DTLB)] = {
  402. [C(OP_READ)] = {
  403. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  404. [C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, },
  405. },
  406. [ C(OP_WRITE) ] = {
  407. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  408. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  409. },
  410. [ C(OP_PREFETCH) ] = {
  411. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  412. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  413. },
  414. },
  415. [C(ITLB)] = {
  416. [C(OP_READ)] = {
  417. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  418. [C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, },
  419. },
  420. [ C(OP_WRITE) ] = {
  421. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  422. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  423. },
  424. [ C(OP_PREFETCH) ] = {
  425. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  426. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  427. },
  428. },
  429. [C(BPU)] = {
  430. [C(OP_READ)] = {
  431. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  432. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  433. },
  434. [ C(OP_WRITE) ] = {
  435. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  436. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  437. },
  438. [ C(OP_PREFETCH) ] = {
  439. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  440. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  441. },
  442. },
  443. };
  444. static const struct sparc_pmu niagara2_pmu = {
  445. .event_map = niagara2_event_map,
  446. .cache_map = &niagara2_cache_map,
  447. .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
  448. .upper_shift = 19,
  449. .lower_shift = 6,
  450. .event_mask = 0xfff,
  451. .hv_bit = 0x8,
  452. .irq_bit = 0x30,
  453. .upper_nop = 0x220,
  454. .lower_nop = 0x220,
  455. };
  456. static const struct sparc_pmu *sparc_pmu __read_mostly;
  457. static u64 event_encoding(u64 event_id, int idx)
  458. {
  459. if (idx == PIC_UPPER_INDEX)
  460. event_id <<= sparc_pmu->upper_shift;
  461. else
  462. event_id <<= sparc_pmu->lower_shift;
  463. return event_id;
  464. }
  465. static u64 mask_for_index(int idx)
  466. {
  467. return event_encoding(sparc_pmu->event_mask, idx);
  468. }
  469. static u64 nop_for_index(int idx)
  470. {
  471. return event_encoding(idx == PIC_UPPER_INDEX ?
  472. sparc_pmu->upper_nop :
  473. sparc_pmu->lower_nop, idx);
  474. }
  475. static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  476. {
  477. u64 val, mask = mask_for_index(idx);
  478. val = cpuc->pcr;
  479. val &= ~mask;
  480. val |= hwc->config;
  481. cpuc->pcr = val;
  482. pcr_ops->write(cpuc->pcr);
  483. }
  484. static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  485. {
  486. u64 mask = mask_for_index(idx);
  487. u64 nop = nop_for_index(idx);
  488. u64 val;
  489. val = cpuc->pcr;
  490. val &= ~mask;
  491. val |= nop;
  492. cpuc->pcr = val;
  493. pcr_ops->write(cpuc->pcr);
  494. }
  495. static u32 read_pmc(int idx)
  496. {
  497. u64 val;
  498. read_pic(val);
  499. if (idx == PIC_UPPER_INDEX)
  500. val >>= 32;
  501. return val & 0xffffffff;
  502. }
  503. static void write_pmc(int idx, u64 val)
  504. {
  505. u64 shift, mask, pic;
  506. shift = 0;
  507. if (idx == PIC_UPPER_INDEX)
  508. shift = 32;
  509. mask = ((u64) 0xffffffff) << shift;
  510. val <<= shift;
  511. read_pic(pic);
  512. pic &= ~mask;
  513. pic |= val;
  514. write_pic(pic);
  515. }
  516. static u64 sparc_perf_event_update(struct perf_event *event,
  517. struct hw_perf_event *hwc, int idx)
  518. {
  519. int shift = 64 - 32;
  520. u64 prev_raw_count, new_raw_count;
  521. s64 delta;
  522. again:
  523. prev_raw_count = local64_read(&hwc->prev_count);
  524. new_raw_count = read_pmc(idx);
  525. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  526. new_raw_count) != prev_raw_count)
  527. goto again;
  528. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  529. delta >>= shift;
  530. local64_add(delta, &event->count);
  531. local64_sub(delta, &hwc->period_left);
  532. return new_raw_count;
  533. }
  534. static int sparc_perf_event_set_period(struct perf_event *event,
  535. struct hw_perf_event *hwc, int idx)
  536. {
  537. s64 left = local64_read(&hwc->period_left);
  538. s64 period = hwc->sample_period;
  539. int ret = 0;
  540. if (unlikely(left <= -period)) {
  541. left = period;
  542. local64_set(&hwc->period_left, left);
  543. hwc->last_period = period;
  544. ret = 1;
  545. }
  546. if (unlikely(left <= 0)) {
  547. left += period;
  548. local64_set(&hwc->period_left, left);
  549. hwc->last_period = period;
  550. ret = 1;
  551. }
  552. if (left > MAX_PERIOD)
  553. left = MAX_PERIOD;
  554. local64_set(&hwc->prev_count, (u64)-left);
  555. write_pmc(idx, (u64)(-left) & 0xffffffff);
  556. perf_event_update_userpage(event);
  557. return ret;
  558. }
  559. /* If performance event entries have been added, move existing
  560. * events around (if necessary) and then assign new entries to
  561. * counters.
  562. */
  563. static u64 maybe_change_configuration(struct cpu_hw_events *cpuc, u64 pcr)
  564. {
  565. int i;
  566. if (!cpuc->n_added)
  567. goto out;
  568. /* Read in the counters which are moving. */
  569. for (i = 0; i < cpuc->n_events; i++) {
  570. struct perf_event *cp = cpuc->event[i];
  571. if (cpuc->current_idx[i] != PIC_NO_INDEX &&
  572. cpuc->current_idx[i] != cp->hw.idx) {
  573. sparc_perf_event_update(cp, &cp->hw,
  574. cpuc->current_idx[i]);
  575. cpuc->current_idx[i] = PIC_NO_INDEX;
  576. }
  577. }
  578. /* Assign to counters all unassigned events. */
  579. for (i = 0; i < cpuc->n_events; i++) {
  580. struct perf_event *cp = cpuc->event[i];
  581. struct hw_perf_event *hwc = &cp->hw;
  582. int idx = hwc->idx;
  583. u64 enc;
  584. if (cpuc->current_idx[i] != PIC_NO_INDEX)
  585. continue;
  586. sparc_perf_event_set_period(cp, hwc, idx);
  587. cpuc->current_idx[i] = idx;
  588. enc = perf_event_get_enc(cpuc->events[i]);
  589. pcr &= ~mask_for_index(idx);
  590. if (hwc->state & PERF_HES_STOPPED)
  591. pcr |= nop_for_index(idx);
  592. else
  593. pcr |= event_encoding(enc, idx);
  594. }
  595. out:
  596. return pcr;
  597. }
  598. static void sparc_pmu_enable(struct pmu *pmu)
  599. {
  600. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  601. u64 pcr;
  602. if (cpuc->enabled)
  603. return;
  604. cpuc->enabled = 1;
  605. barrier();
  606. pcr = cpuc->pcr;
  607. if (!cpuc->n_events) {
  608. pcr = 0;
  609. } else {
  610. pcr = maybe_change_configuration(cpuc, pcr);
  611. /* We require that all of the events have the same
  612. * configuration, so just fetch the settings from the
  613. * first entry.
  614. */
  615. cpuc->pcr = pcr | cpuc->event[0]->hw.config_base;
  616. }
  617. pcr_ops->write(cpuc->pcr);
  618. }
  619. static void sparc_pmu_disable(struct pmu *pmu)
  620. {
  621. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  622. u64 val;
  623. if (!cpuc->enabled)
  624. return;
  625. cpuc->enabled = 0;
  626. cpuc->n_added = 0;
  627. val = cpuc->pcr;
  628. val &= ~(PCR_UTRACE | PCR_STRACE |
  629. sparc_pmu->hv_bit | sparc_pmu->irq_bit);
  630. cpuc->pcr = val;
  631. pcr_ops->write(cpuc->pcr);
  632. }
  633. static int active_event_index(struct cpu_hw_events *cpuc,
  634. struct perf_event *event)
  635. {
  636. int i;
  637. for (i = 0; i < cpuc->n_events; i++) {
  638. if (cpuc->event[i] == event)
  639. break;
  640. }
  641. BUG_ON(i == cpuc->n_events);
  642. return cpuc->current_idx[i];
  643. }
  644. static void sparc_pmu_start(struct perf_event *event, int flags)
  645. {
  646. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  647. int idx = active_event_index(cpuc, event);
  648. if (flags & PERF_EF_RELOAD) {
  649. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  650. sparc_perf_event_set_period(event, &event->hw, idx);
  651. }
  652. event->hw.state = 0;
  653. sparc_pmu_enable_event(cpuc, &event->hw, idx);
  654. }
  655. static void sparc_pmu_stop(struct perf_event *event, int flags)
  656. {
  657. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  658. int idx = active_event_index(cpuc, event);
  659. if (!(event->hw.state & PERF_HES_STOPPED)) {
  660. sparc_pmu_disable_event(cpuc, &event->hw, idx);
  661. event->hw.state |= PERF_HES_STOPPED;
  662. }
  663. if (!(event->hw.state & PERF_HES_UPTODATE) && (flags & PERF_EF_UPDATE)) {
  664. sparc_perf_event_update(event, &event->hw, idx);
  665. event->hw.state |= PERF_HES_UPTODATE;
  666. }
  667. }
  668. static void sparc_pmu_del(struct perf_event *event, int _flags)
  669. {
  670. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  671. unsigned long flags;
  672. int i;
  673. local_irq_save(flags);
  674. perf_pmu_disable(event->pmu);
  675. for (i = 0; i < cpuc->n_events; i++) {
  676. if (event == cpuc->event[i]) {
  677. /* Absorb the final count and turn off the
  678. * event.
  679. */
  680. sparc_pmu_stop(event, PERF_EF_UPDATE);
  681. /* Shift remaining entries down into
  682. * the existing slot.
  683. */
  684. while (++i < cpuc->n_events) {
  685. cpuc->event[i - 1] = cpuc->event[i];
  686. cpuc->events[i - 1] = cpuc->events[i];
  687. cpuc->current_idx[i - 1] =
  688. cpuc->current_idx[i];
  689. }
  690. perf_event_update_userpage(event);
  691. cpuc->n_events--;
  692. break;
  693. }
  694. }
  695. perf_pmu_enable(event->pmu);
  696. local_irq_restore(flags);
  697. }
  698. static void sparc_pmu_read(struct perf_event *event)
  699. {
  700. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  701. int idx = active_event_index(cpuc, event);
  702. struct hw_perf_event *hwc = &event->hw;
  703. sparc_perf_event_update(event, hwc, idx);
  704. }
  705. static atomic_t active_events = ATOMIC_INIT(0);
  706. static DEFINE_MUTEX(pmc_grab_mutex);
  707. static void perf_stop_nmi_watchdog(void *unused)
  708. {
  709. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  710. stop_nmi_watchdog(NULL);
  711. cpuc->pcr = pcr_ops->read();
  712. }
  713. void perf_event_grab_pmc(void)
  714. {
  715. if (atomic_inc_not_zero(&active_events))
  716. return;
  717. mutex_lock(&pmc_grab_mutex);
  718. if (atomic_read(&active_events) == 0) {
  719. if (atomic_read(&nmi_active) > 0) {
  720. on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
  721. BUG_ON(atomic_read(&nmi_active) != 0);
  722. }
  723. atomic_inc(&active_events);
  724. }
  725. mutex_unlock(&pmc_grab_mutex);
  726. }
  727. void perf_event_release_pmc(void)
  728. {
  729. if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
  730. if (atomic_read(&nmi_active) == 0)
  731. on_each_cpu(start_nmi_watchdog, NULL, 1);
  732. mutex_unlock(&pmc_grab_mutex);
  733. }
  734. }
  735. static const struct perf_event_map *sparc_map_cache_event(u64 config)
  736. {
  737. unsigned int cache_type, cache_op, cache_result;
  738. const struct perf_event_map *pmap;
  739. if (!sparc_pmu->cache_map)
  740. return ERR_PTR(-ENOENT);
  741. cache_type = (config >> 0) & 0xff;
  742. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  743. return ERR_PTR(-EINVAL);
  744. cache_op = (config >> 8) & 0xff;
  745. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  746. return ERR_PTR(-EINVAL);
  747. cache_result = (config >> 16) & 0xff;
  748. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  749. return ERR_PTR(-EINVAL);
  750. pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
  751. if (pmap->encoding == CACHE_OP_UNSUPPORTED)
  752. return ERR_PTR(-ENOENT);
  753. if (pmap->encoding == CACHE_OP_NONSENSE)
  754. return ERR_PTR(-EINVAL);
  755. return pmap;
  756. }
  757. static void hw_perf_event_destroy(struct perf_event *event)
  758. {
  759. perf_event_release_pmc();
  760. }
  761. /* Make sure all events can be scheduled into the hardware at
  762. * the same time. This is simplified by the fact that we only
  763. * need to support 2 simultaneous HW events.
  764. *
  765. * As a side effect, the evts[]->hw.idx values will be assigned
  766. * on success. These are pending indexes. When the events are
  767. * actually programmed into the chip, these values will propagate
  768. * to the per-cpu cpuc->current_idx[] slots, see the code in
  769. * maybe_change_configuration() for details.
  770. */
  771. static int sparc_check_constraints(struct perf_event **evts,
  772. unsigned long *events, int n_ev)
  773. {
  774. u8 msk0 = 0, msk1 = 0;
  775. int idx0 = 0;
  776. /* This case is possible when we are invoked from
  777. * hw_perf_group_sched_in().
  778. */
  779. if (!n_ev)
  780. return 0;
  781. if (n_ev > MAX_HWEVENTS)
  782. return -1;
  783. msk0 = perf_event_get_msk(events[0]);
  784. if (n_ev == 1) {
  785. if (msk0 & PIC_LOWER)
  786. idx0 = 1;
  787. goto success;
  788. }
  789. BUG_ON(n_ev != 2);
  790. msk1 = perf_event_get_msk(events[1]);
  791. /* If both events can go on any counter, OK. */
  792. if (msk0 == (PIC_UPPER | PIC_LOWER) &&
  793. msk1 == (PIC_UPPER | PIC_LOWER))
  794. goto success;
  795. /* If one event is limited to a specific counter,
  796. * and the other can go on both, OK.
  797. */
  798. if ((msk0 == PIC_UPPER || msk0 == PIC_LOWER) &&
  799. msk1 == (PIC_UPPER | PIC_LOWER)) {
  800. if (msk0 & PIC_LOWER)
  801. idx0 = 1;
  802. goto success;
  803. }
  804. if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
  805. msk0 == (PIC_UPPER | PIC_LOWER)) {
  806. if (msk1 & PIC_UPPER)
  807. idx0 = 1;
  808. goto success;
  809. }
  810. /* If the events are fixed to different counters, OK. */
  811. if ((msk0 == PIC_UPPER && msk1 == PIC_LOWER) ||
  812. (msk0 == PIC_LOWER && msk1 == PIC_UPPER)) {
  813. if (msk0 & PIC_LOWER)
  814. idx0 = 1;
  815. goto success;
  816. }
  817. /* Otherwise, there is a conflict. */
  818. return -1;
  819. success:
  820. evts[0]->hw.idx = idx0;
  821. if (n_ev == 2)
  822. evts[1]->hw.idx = idx0 ^ 1;
  823. return 0;
  824. }
  825. static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
  826. {
  827. int eu = 0, ek = 0, eh = 0;
  828. struct perf_event *event;
  829. int i, n, first;
  830. n = n_prev + n_new;
  831. if (n <= 1)
  832. return 0;
  833. first = 1;
  834. for (i = 0; i < n; i++) {
  835. event = evts[i];
  836. if (first) {
  837. eu = event->attr.exclude_user;
  838. ek = event->attr.exclude_kernel;
  839. eh = event->attr.exclude_hv;
  840. first = 0;
  841. } else if (event->attr.exclude_user != eu ||
  842. event->attr.exclude_kernel != ek ||
  843. event->attr.exclude_hv != eh) {
  844. return -EAGAIN;
  845. }
  846. }
  847. return 0;
  848. }
  849. static int collect_events(struct perf_event *group, int max_count,
  850. struct perf_event *evts[], unsigned long *events,
  851. int *current_idx)
  852. {
  853. struct perf_event *event;
  854. int n = 0;
  855. if (!is_software_event(group)) {
  856. if (n >= max_count)
  857. return -1;
  858. evts[n] = group;
  859. events[n] = group->hw.event_base;
  860. current_idx[n++] = PIC_NO_INDEX;
  861. }
  862. list_for_each_entry(event, &group->sibling_list, group_entry) {
  863. if (!is_software_event(event) &&
  864. event->state != PERF_EVENT_STATE_OFF) {
  865. if (n >= max_count)
  866. return -1;
  867. evts[n] = event;
  868. events[n] = event->hw.event_base;
  869. current_idx[n++] = PIC_NO_INDEX;
  870. }
  871. }
  872. return n;
  873. }
  874. static int sparc_pmu_add(struct perf_event *event, int ef_flags)
  875. {
  876. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  877. int n0, ret = -EAGAIN;
  878. unsigned long flags;
  879. local_irq_save(flags);
  880. perf_pmu_disable(event->pmu);
  881. n0 = cpuc->n_events;
  882. if (n0 >= MAX_HWEVENTS)
  883. goto out;
  884. cpuc->event[n0] = event;
  885. cpuc->events[n0] = event->hw.event_base;
  886. cpuc->current_idx[n0] = PIC_NO_INDEX;
  887. event->hw.state = PERF_HES_UPTODATE;
  888. if (!(ef_flags & PERF_EF_START))
  889. event->hw.state |= PERF_HES_STOPPED;
  890. /*
  891. * If group events scheduling transaction was started,
  892. * skip the schedulability test here, it will be performed
  893. * at commit time(->commit_txn) as a whole
  894. */
  895. if (cpuc->group_flag & PERF_EVENT_TXN)
  896. goto nocheck;
  897. if (check_excludes(cpuc->event, n0, 1))
  898. goto out;
  899. if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1))
  900. goto out;
  901. nocheck:
  902. cpuc->n_events++;
  903. cpuc->n_added++;
  904. ret = 0;
  905. out:
  906. perf_pmu_enable(event->pmu);
  907. local_irq_restore(flags);
  908. return ret;
  909. }
  910. static int sparc_pmu_event_init(struct perf_event *event)
  911. {
  912. struct perf_event_attr *attr = &event->attr;
  913. struct perf_event *evts[MAX_HWEVENTS];
  914. struct hw_perf_event *hwc = &event->hw;
  915. unsigned long events[MAX_HWEVENTS];
  916. int current_idx_dmy[MAX_HWEVENTS];
  917. const struct perf_event_map *pmap;
  918. int n;
  919. if (atomic_read(&nmi_active) < 0)
  920. return -ENODEV;
  921. switch (attr->type) {
  922. case PERF_TYPE_HARDWARE:
  923. if (attr->config >= sparc_pmu->max_events)
  924. return -EINVAL;
  925. pmap = sparc_pmu->event_map(attr->config);
  926. break;
  927. case PERF_TYPE_HW_CACHE:
  928. pmap = sparc_map_cache_event(attr->config);
  929. if (IS_ERR(pmap))
  930. return PTR_ERR(pmap);
  931. break;
  932. case PERF_TYPE_RAW:
  933. pmap = NULL;
  934. break;
  935. default:
  936. return -ENOENT;
  937. }
  938. if (pmap) {
  939. hwc->event_base = perf_event_encode(pmap);
  940. } else {
  941. /*
  942. * User gives us "(encoding << 16) | pic_mask" for
  943. * PERF_TYPE_RAW events.
  944. */
  945. hwc->event_base = attr->config;
  946. }
  947. /* We save the enable bits in the config_base. */
  948. hwc->config_base = sparc_pmu->irq_bit;
  949. if (!attr->exclude_user)
  950. hwc->config_base |= PCR_UTRACE;
  951. if (!attr->exclude_kernel)
  952. hwc->config_base |= PCR_STRACE;
  953. if (!attr->exclude_hv)
  954. hwc->config_base |= sparc_pmu->hv_bit;
  955. n = 0;
  956. if (event->group_leader != event) {
  957. n = collect_events(event->group_leader,
  958. MAX_HWEVENTS - 1,
  959. evts, events, current_idx_dmy);
  960. if (n < 0)
  961. return -EINVAL;
  962. }
  963. events[n] = hwc->event_base;
  964. evts[n] = event;
  965. if (check_excludes(evts, n, 1))
  966. return -EINVAL;
  967. if (sparc_check_constraints(evts, events, n + 1))
  968. return -EINVAL;
  969. hwc->idx = PIC_NO_INDEX;
  970. /* Try to do all error checking before this point, as unwinding
  971. * state after grabbing the PMC is difficult.
  972. */
  973. perf_event_grab_pmc();
  974. event->destroy = hw_perf_event_destroy;
  975. if (!hwc->sample_period) {
  976. hwc->sample_period = MAX_PERIOD;
  977. hwc->last_period = hwc->sample_period;
  978. local64_set(&hwc->period_left, hwc->sample_period);
  979. }
  980. return 0;
  981. }
  982. /*
  983. * Start group events scheduling transaction
  984. * Set the flag to make pmu::enable() not perform the
  985. * schedulability test, it will be performed at commit time
  986. */
  987. static void sparc_pmu_start_txn(struct pmu *pmu)
  988. {
  989. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  990. perf_pmu_disable(pmu);
  991. cpuhw->group_flag |= PERF_EVENT_TXN;
  992. }
  993. /*
  994. * Stop group events scheduling transaction
  995. * Clear the flag and pmu::enable() will perform the
  996. * schedulability test.
  997. */
  998. static void sparc_pmu_cancel_txn(struct pmu *pmu)
  999. {
  1000. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  1001. cpuhw->group_flag &= ~PERF_EVENT_TXN;
  1002. perf_pmu_enable(pmu);
  1003. }
  1004. /*
  1005. * Commit group events scheduling transaction
  1006. * Perform the group schedulability test as a whole
  1007. * Return 0 if success
  1008. */
  1009. static int sparc_pmu_commit_txn(struct pmu *pmu)
  1010. {
  1011. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1012. int n;
  1013. if (!sparc_pmu)
  1014. return -EINVAL;
  1015. cpuc = &__get_cpu_var(cpu_hw_events);
  1016. n = cpuc->n_events;
  1017. if (check_excludes(cpuc->event, 0, n))
  1018. return -EINVAL;
  1019. if (sparc_check_constraints(cpuc->event, cpuc->events, n))
  1020. return -EAGAIN;
  1021. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1022. perf_pmu_enable(pmu);
  1023. return 0;
  1024. }
  1025. static struct pmu pmu = {
  1026. .pmu_enable = sparc_pmu_enable,
  1027. .pmu_disable = sparc_pmu_disable,
  1028. .event_init = sparc_pmu_event_init,
  1029. .add = sparc_pmu_add,
  1030. .del = sparc_pmu_del,
  1031. .start = sparc_pmu_start,
  1032. .stop = sparc_pmu_stop,
  1033. .read = sparc_pmu_read,
  1034. .start_txn = sparc_pmu_start_txn,
  1035. .cancel_txn = sparc_pmu_cancel_txn,
  1036. .commit_txn = sparc_pmu_commit_txn,
  1037. };
  1038. void perf_event_print_debug(void)
  1039. {
  1040. unsigned long flags;
  1041. u64 pcr, pic;
  1042. int cpu;
  1043. if (!sparc_pmu)
  1044. return;
  1045. local_irq_save(flags);
  1046. cpu = smp_processor_id();
  1047. pcr = pcr_ops->read();
  1048. read_pic(pic);
  1049. pr_info("\n");
  1050. pr_info("CPU#%d: PCR[%016llx] PIC[%016llx]\n",
  1051. cpu, pcr, pic);
  1052. local_irq_restore(flags);
  1053. }
  1054. static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
  1055. unsigned long cmd, void *__args)
  1056. {
  1057. struct die_args *args = __args;
  1058. struct perf_sample_data data;
  1059. struct cpu_hw_events *cpuc;
  1060. struct pt_regs *regs;
  1061. int i;
  1062. if (!atomic_read(&active_events))
  1063. return NOTIFY_DONE;
  1064. switch (cmd) {
  1065. case DIE_NMI:
  1066. break;
  1067. default:
  1068. return NOTIFY_DONE;
  1069. }
  1070. regs = args->regs;
  1071. perf_sample_data_init(&data, 0);
  1072. cpuc = &__get_cpu_var(cpu_hw_events);
  1073. /* If the PMU has the TOE IRQ enable bits, we need to do a
  1074. * dummy write to the %pcr to clear the overflow bits and thus
  1075. * the interrupt.
  1076. *
  1077. * Do this before we peek at the counters to determine
  1078. * overflow so we don't lose any events.
  1079. */
  1080. if (sparc_pmu->irq_bit)
  1081. pcr_ops->write(cpuc->pcr);
  1082. for (i = 0; i < cpuc->n_events; i++) {
  1083. struct perf_event *event = cpuc->event[i];
  1084. int idx = cpuc->current_idx[i];
  1085. struct hw_perf_event *hwc;
  1086. u64 val;
  1087. hwc = &event->hw;
  1088. val = sparc_perf_event_update(event, hwc, idx);
  1089. if (val & (1ULL << 31))
  1090. continue;
  1091. data.period = event->hw.last_period;
  1092. if (!sparc_perf_event_set_period(event, hwc, idx))
  1093. continue;
  1094. if (perf_event_overflow(event, 1, &data, regs))
  1095. sparc_pmu_stop(event, 0);
  1096. }
  1097. return NOTIFY_STOP;
  1098. }
  1099. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1100. .notifier_call = perf_event_nmi_handler,
  1101. };
  1102. static bool __init supported_pmu(void)
  1103. {
  1104. if (!strcmp(sparc_pmu_type, "ultra3") ||
  1105. !strcmp(sparc_pmu_type, "ultra3+") ||
  1106. !strcmp(sparc_pmu_type, "ultra3i") ||
  1107. !strcmp(sparc_pmu_type, "ultra4+")) {
  1108. sparc_pmu = &ultra3_pmu;
  1109. return true;
  1110. }
  1111. if (!strcmp(sparc_pmu_type, "niagara")) {
  1112. sparc_pmu = &niagara1_pmu;
  1113. return true;
  1114. }
  1115. if (!strcmp(sparc_pmu_type, "niagara2") ||
  1116. !strcmp(sparc_pmu_type, "niagara3")) {
  1117. sparc_pmu = &niagara2_pmu;
  1118. return true;
  1119. }
  1120. return false;
  1121. }
  1122. int __init init_hw_perf_events(void)
  1123. {
  1124. pr_info("Performance events: ");
  1125. if (!supported_pmu()) {
  1126. pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
  1127. return 0;
  1128. }
  1129. pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
  1130. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1131. register_die_notifier(&perf_event_nmi_notifier);
  1132. return 0;
  1133. }
  1134. early_initcall(init_hw_perf_events);
  1135. void perf_callchain_kernel(struct perf_callchain_entry *entry,
  1136. struct pt_regs *regs)
  1137. {
  1138. unsigned long ksp, fp;
  1139. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1140. int graph = 0;
  1141. #endif
  1142. stack_trace_flush();
  1143. perf_callchain_store(entry, regs->tpc);
  1144. ksp = regs->u_regs[UREG_I6];
  1145. fp = ksp + STACK_BIAS;
  1146. do {
  1147. struct sparc_stackf *sf;
  1148. struct pt_regs *regs;
  1149. unsigned long pc;
  1150. if (!kstack_valid(current_thread_info(), fp))
  1151. break;
  1152. sf = (struct sparc_stackf *) fp;
  1153. regs = (struct pt_regs *) (sf + 1);
  1154. if (kstack_is_trap_frame(current_thread_info(), regs)) {
  1155. if (user_mode(regs))
  1156. break;
  1157. pc = regs->tpc;
  1158. fp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1159. } else {
  1160. pc = sf->callers_pc;
  1161. fp = (unsigned long)sf->fp + STACK_BIAS;
  1162. }
  1163. perf_callchain_store(entry, pc);
  1164. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1165. if ((pc + 8UL) == (unsigned long) &return_to_handler) {
  1166. int index = current->curr_ret_stack;
  1167. if (current->ret_stack && index >= graph) {
  1168. pc = current->ret_stack[index - graph].ret;
  1169. perf_callchain_store(entry, pc);
  1170. graph++;
  1171. }
  1172. }
  1173. #endif
  1174. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1175. }
  1176. static void perf_callchain_user_64(struct perf_callchain_entry *entry,
  1177. struct pt_regs *regs)
  1178. {
  1179. unsigned long ufp;
  1180. perf_callchain_store(entry, regs->tpc);
  1181. ufp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1182. do {
  1183. struct sparc_stackf *usf, sf;
  1184. unsigned long pc;
  1185. usf = (struct sparc_stackf *) ufp;
  1186. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1187. break;
  1188. pc = sf.callers_pc;
  1189. ufp = (unsigned long)sf.fp + STACK_BIAS;
  1190. perf_callchain_store(entry, pc);
  1191. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1192. }
  1193. static void perf_callchain_user_32(struct perf_callchain_entry *entry,
  1194. struct pt_regs *regs)
  1195. {
  1196. unsigned long ufp;
  1197. perf_callchain_store(entry, regs->tpc);
  1198. ufp = regs->u_regs[UREG_I6] & 0xffffffffUL;
  1199. do {
  1200. struct sparc_stackf32 *usf, sf;
  1201. unsigned long pc;
  1202. usf = (struct sparc_stackf32 *) ufp;
  1203. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1204. break;
  1205. pc = sf.callers_pc;
  1206. ufp = (unsigned long)sf.fp;
  1207. perf_callchain_store(entry, pc);
  1208. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1209. }
  1210. void
  1211. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1212. {
  1213. flushw_user();
  1214. if (test_thread_flag(TIF_32BIT))
  1215. perf_callchain_user_32(entry, regs);
  1216. else
  1217. perf_callchain_user_64(entry, regs);
  1218. }