perf_event.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /* Performance event support for sparc64.
  2. *
  3. * Copyright (C) 2009 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/kernel.h>
  17. #include <linux/kdebug.h>
  18. #include <linux/mutex.h>
  19. #include <asm/cpudata.h>
  20. #include <asm/atomic.h>
  21. #include <asm/nmi.h>
  22. #include <asm/pcr.h>
  23. /* Sparc64 chips have two performance counters, 32-bits each, with
  24. * overflow interrupts generated on transition from 0xffffffff to 0.
  25. * The counters are accessed in one go using a 64-bit register.
  26. *
  27. * Both counters are controlled using a single control register. The
  28. * only way to stop all sampling is to clear all of the context (user,
  29. * supervisor, hypervisor) sampling enable bits. But these bits apply
  30. * to both counters, thus the two counters can't be enabled/disabled
  31. * individually.
  32. *
  33. * The control register has two event fields, one for each of the two
  34. * counters. It's thus nearly impossible to have one counter going
  35. * while keeping the other one stopped. Therefore it is possible to
  36. * get overflow interrupts for counters not currently "in use" and
  37. * that condition must be checked in the overflow interrupt handler.
  38. *
  39. * So we use a hack, in that we program inactive counters with the
  40. * "sw_count0" and "sw_count1" events. These count how many times
  41. * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
  42. * unusual way to encode a NOP and therefore will not trigger in
  43. * normal code.
  44. */
  45. #define MAX_HWEVENTS 2
  46. #define MAX_PERIOD ((1UL << 32) - 1)
  47. #define PIC_UPPER_INDEX 0
  48. #define PIC_LOWER_INDEX 1
  49. struct cpu_hw_events {
  50. struct perf_event *events[MAX_HWEVENTS];
  51. unsigned long used_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
  52. unsigned long active_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
  53. u64 pcr;
  54. int enabled;
  55. };
  56. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
  57. struct perf_event_map {
  58. u16 encoding;
  59. u8 pic_mask;
  60. #define PIC_NONE 0x00
  61. #define PIC_UPPER 0x01
  62. #define PIC_LOWER 0x02
  63. };
  64. static unsigned long perf_event_encode(const struct perf_event_map *pmap)
  65. {
  66. return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
  67. }
  68. static void perf_event_decode(unsigned long val, u16 *enc, u8 *msk)
  69. {
  70. *msk = val & 0xff;
  71. *enc = val >> 16;
  72. }
  73. #define C(x) PERF_COUNT_HW_CACHE_##x
  74. #define CACHE_OP_UNSUPPORTED 0xfffe
  75. #define CACHE_OP_NONSENSE 0xffff
  76. typedef struct perf_event_map cache_map_t
  77. [PERF_COUNT_HW_CACHE_MAX]
  78. [PERF_COUNT_HW_CACHE_OP_MAX]
  79. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  80. struct sparc_pmu {
  81. const struct perf_event_map *(*event_map)(int);
  82. const cache_map_t *cache_map;
  83. int max_events;
  84. int upper_shift;
  85. int lower_shift;
  86. int event_mask;
  87. int hv_bit;
  88. int irq_bit;
  89. int upper_nop;
  90. int lower_nop;
  91. };
  92. static const struct perf_event_map ultra3_perfmon_event_map[] = {
  93. [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
  94. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
  95. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
  96. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
  97. };
  98. static const struct perf_event_map *ultra3_event_map(int event_id)
  99. {
  100. return &ultra3_perfmon_event_map[event_id];
  101. }
  102. static const cache_map_t ultra3_cache_map = {
  103. [C(L1D)] = {
  104. [C(OP_READ)] = {
  105. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  106. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  107. },
  108. [C(OP_WRITE)] = {
  109. [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
  110. [C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
  111. },
  112. [C(OP_PREFETCH)] = {
  113. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  114. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  115. },
  116. },
  117. [C(L1I)] = {
  118. [C(OP_READ)] = {
  119. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  120. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  121. },
  122. [ C(OP_WRITE) ] = {
  123. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  124. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  125. },
  126. [ C(OP_PREFETCH) ] = {
  127. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  128. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  129. },
  130. },
  131. [C(LL)] = {
  132. [C(OP_READ)] = {
  133. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
  134. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
  135. },
  136. [C(OP_WRITE)] = {
  137. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
  138. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
  139. },
  140. [C(OP_PREFETCH)] = {
  141. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  142. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  143. },
  144. },
  145. [C(DTLB)] = {
  146. [C(OP_READ)] = {
  147. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  148. [C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
  149. },
  150. [ C(OP_WRITE) ] = {
  151. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  152. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  153. },
  154. [ C(OP_PREFETCH) ] = {
  155. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  156. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  157. },
  158. },
  159. [C(ITLB)] = {
  160. [C(OP_READ)] = {
  161. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  162. [C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
  163. },
  164. [ C(OP_WRITE) ] = {
  165. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  166. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  167. },
  168. [ C(OP_PREFETCH) ] = {
  169. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  170. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  171. },
  172. },
  173. [C(BPU)] = {
  174. [C(OP_READ)] = {
  175. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  176. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  177. },
  178. [ C(OP_WRITE) ] = {
  179. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  180. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  181. },
  182. [ C(OP_PREFETCH) ] = {
  183. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  184. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  185. },
  186. },
  187. };
  188. static const struct sparc_pmu ultra3_pmu = {
  189. .event_map = ultra3_event_map,
  190. .cache_map = &ultra3_cache_map,
  191. .max_events = ARRAY_SIZE(ultra3_perfmon_event_map),
  192. .upper_shift = 11,
  193. .lower_shift = 4,
  194. .event_mask = 0x3f,
  195. .upper_nop = 0x1c,
  196. .lower_nop = 0x14,
  197. };
  198. /* Niagara1 is very limited. The upper PIC is hard-locked to count
  199. * only instructions, so it is free running which creates all kinds of
  200. * problems. Some hardware designs make one wonder if the creator
  201. * even looked at how this stuff gets used by software.
  202. */
  203. static const struct perf_event_map niagara1_perfmon_event_map[] = {
  204. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER },
  205. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER },
  206. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE },
  207. [PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER },
  208. };
  209. static const struct perf_event_map *niagara1_event_map(int event_id)
  210. {
  211. return &niagara1_perfmon_event_map[event_id];
  212. }
  213. static const cache_map_t niagara1_cache_map = {
  214. [C(L1D)] = {
  215. [C(OP_READ)] = {
  216. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  217. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  218. },
  219. [C(OP_WRITE)] = {
  220. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  221. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  222. },
  223. [C(OP_PREFETCH)] = {
  224. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  225. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  226. },
  227. },
  228. [C(L1I)] = {
  229. [C(OP_READ)] = {
  230. [C(RESULT_ACCESS)] = { 0x00, PIC_UPPER },
  231. [C(RESULT_MISS)] = { 0x02, PIC_LOWER, },
  232. },
  233. [ C(OP_WRITE) ] = {
  234. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  235. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  236. },
  237. [ C(OP_PREFETCH) ] = {
  238. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  239. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  240. },
  241. },
  242. [C(LL)] = {
  243. [C(OP_READ)] = {
  244. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  245. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  246. },
  247. [C(OP_WRITE)] = {
  248. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  249. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  250. },
  251. [C(OP_PREFETCH)] = {
  252. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  253. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  254. },
  255. },
  256. [C(DTLB)] = {
  257. [C(OP_READ)] = {
  258. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  259. [C(RESULT_MISS)] = { 0x05, PIC_LOWER, },
  260. },
  261. [ C(OP_WRITE) ] = {
  262. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  263. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  264. },
  265. [ C(OP_PREFETCH) ] = {
  266. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  267. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  268. },
  269. },
  270. [C(ITLB)] = {
  271. [C(OP_READ)] = {
  272. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  273. [C(RESULT_MISS)] = { 0x04, PIC_LOWER, },
  274. },
  275. [ C(OP_WRITE) ] = {
  276. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  277. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  278. },
  279. [ C(OP_PREFETCH) ] = {
  280. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  281. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  282. },
  283. },
  284. [C(BPU)] = {
  285. [C(OP_READ)] = {
  286. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  287. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  288. },
  289. [ C(OP_WRITE) ] = {
  290. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  291. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  292. },
  293. [ C(OP_PREFETCH) ] = {
  294. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  295. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  296. },
  297. },
  298. };
  299. static const struct sparc_pmu niagara1_pmu = {
  300. .event_map = niagara1_event_map,
  301. .cache_map = &niagara1_cache_map,
  302. .max_events = ARRAY_SIZE(niagara1_perfmon_event_map),
  303. .upper_shift = 0,
  304. .lower_shift = 4,
  305. .event_mask = 0x7,
  306. .upper_nop = 0x0,
  307. .lower_nop = 0x0,
  308. };
  309. static const struct perf_event_map niagara2_perfmon_event_map[] = {
  310. [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  311. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  312. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
  313. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
  314. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
  315. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
  316. };
  317. static const struct perf_event_map *niagara2_event_map(int event_id)
  318. {
  319. return &niagara2_perfmon_event_map[event_id];
  320. }
  321. static const cache_map_t niagara2_cache_map = {
  322. [C(L1D)] = {
  323. [C(OP_READ)] = {
  324. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  325. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  326. },
  327. [C(OP_WRITE)] = {
  328. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  329. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  330. },
  331. [C(OP_PREFETCH)] = {
  332. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  333. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  334. },
  335. },
  336. [C(L1I)] = {
  337. [C(OP_READ)] = {
  338. [C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, },
  339. [C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, },
  340. },
  341. [ C(OP_WRITE) ] = {
  342. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  343. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  344. },
  345. [ C(OP_PREFETCH) ] = {
  346. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  347. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  348. },
  349. },
  350. [C(LL)] = {
  351. [C(OP_READ)] = {
  352. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  353. [C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, },
  354. },
  355. [C(OP_WRITE)] = {
  356. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  357. [C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, },
  358. },
  359. [C(OP_PREFETCH)] = {
  360. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  361. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  362. },
  363. },
  364. [C(DTLB)] = {
  365. [C(OP_READ)] = {
  366. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  367. [C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, },
  368. },
  369. [ C(OP_WRITE) ] = {
  370. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  371. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  372. },
  373. [ C(OP_PREFETCH) ] = {
  374. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  375. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  376. },
  377. },
  378. [C(ITLB)] = {
  379. [C(OP_READ)] = {
  380. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  381. [C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, },
  382. },
  383. [ C(OP_WRITE) ] = {
  384. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  385. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  386. },
  387. [ C(OP_PREFETCH) ] = {
  388. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  389. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  390. },
  391. },
  392. [C(BPU)] = {
  393. [C(OP_READ)] = {
  394. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  395. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  396. },
  397. [ C(OP_WRITE) ] = {
  398. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  399. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  400. },
  401. [ C(OP_PREFETCH) ] = {
  402. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  403. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  404. },
  405. },
  406. };
  407. static const struct sparc_pmu niagara2_pmu = {
  408. .event_map = niagara2_event_map,
  409. .cache_map = &niagara2_cache_map,
  410. .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
  411. .upper_shift = 19,
  412. .lower_shift = 6,
  413. .event_mask = 0xfff,
  414. .hv_bit = 0x8,
  415. .irq_bit = 0x30,
  416. .upper_nop = 0x220,
  417. .lower_nop = 0x220,
  418. };
  419. static const struct sparc_pmu *sparc_pmu __read_mostly;
  420. static u64 event_encoding(u64 event_id, int idx)
  421. {
  422. if (idx == PIC_UPPER_INDEX)
  423. event_id <<= sparc_pmu->upper_shift;
  424. else
  425. event_id <<= sparc_pmu->lower_shift;
  426. return event_id;
  427. }
  428. static u64 mask_for_index(int idx)
  429. {
  430. return event_encoding(sparc_pmu->event_mask, idx);
  431. }
  432. static u64 nop_for_index(int idx)
  433. {
  434. return event_encoding(idx == PIC_UPPER_INDEX ?
  435. sparc_pmu->upper_nop :
  436. sparc_pmu->lower_nop, idx);
  437. }
  438. static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  439. {
  440. u64 val, mask = mask_for_index(idx);
  441. val = cpuc->pcr;
  442. val &= ~mask;
  443. val |= hwc->config;
  444. cpuc->pcr = val;
  445. pcr_ops->write(cpuc->pcr);
  446. }
  447. static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  448. {
  449. u64 mask = mask_for_index(idx);
  450. u64 nop = nop_for_index(idx);
  451. u64 val;
  452. val = cpuc->pcr;
  453. val &= ~mask;
  454. val |= nop;
  455. cpuc->pcr = val;
  456. pcr_ops->write(cpuc->pcr);
  457. }
  458. void hw_perf_enable(void)
  459. {
  460. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  461. u64 val;
  462. int i;
  463. if (cpuc->enabled)
  464. return;
  465. cpuc->enabled = 1;
  466. barrier();
  467. val = cpuc->pcr;
  468. for (i = 0; i < MAX_HWEVENTS; i++) {
  469. struct perf_event *cp = cpuc->events[i];
  470. struct hw_perf_event *hwc;
  471. if (!cp)
  472. continue;
  473. hwc = &cp->hw;
  474. val |= hwc->config_base;
  475. }
  476. cpuc->pcr = val;
  477. pcr_ops->write(cpuc->pcr);
  478. }
  479. void hw_perf_disable(void)
  480. {
  481. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  482. u64 val;
  483. if (!cpuc->enabled)
  484. return;
  485. cpuc->enabled = 0;
  486. val = cpuc->pcr;
  487. val &= ~(PCR_UTRACE | PCR_STRACE |
  488. sparc_pmu->hv_bit | sparc_pmu->irq_bit);
  489. cpuc->pcr = val;
  490. pcr_ops->write(cpuc->pcr);
  491. }
  492. static u32 read_pmc(int idx)
  493. {
  494. u64 val;
  495. read_pic(val);
  496. if (idx == PIC_UPPER_INDEX)
  497. val >>= 32;
  498. return val & 0xffffffff;
  499. }
  500. static void write_pmc(int idx, u64 val)
  501. {
  502. u64 shift, mask, pic;
  503. shift = 0;
  504. if (idx == PIC_UPPER_INDEX)
  505. shift = 32;
  506. mask = ((u64) 0xffffffff) << shift;
  507. val <<= shift;
  508. read_pic(pic);
  509. pic &= ~mask;
  510. pic |= val;
  511. write_pic(pic);
  512. }
  513. static int sparc_perf_event_set_period(struct perf_event *event,
  514. struct hw_perf_event *hwc, int idx)
  515. {
  516. s64 left = atomic64_read(&hwc->period_left);
  517. s64 period = hwc->sample_period;
  518. int ret = 0;
  519. if (unlikely(left <= -period)) {
  520. left = period;
  521. atomic64_set(&hwc->period_left, left);
  522. hwc->last_period = period;
  523. ret = 1;
  524. }
  525. if (unlikely(left <= 0)) {
  526. left += period;
  527. atomic64_set(&hwc->period_left, left);
  528. hwc->last_period = period;
  529. ret = 1;
  530. }
  531. if (left > MAX_PERIOD)
  532. left = MAX_PERIOD;
  533. atomic64_set(&hwc->prev_count, (u64)-left);
  534. write_pmc(idx, (u64)(-left) & 0xffffffff);
  535. perf_event_update_userpage(event);
  536. return ret;
  537. }
  538. static int sparc_pmu_enable(struct perf_event *event)
  539. {
  540. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  541. struct hw_perf_event *hwc = &event->hw;
  542. int idx = hwc->idx;
  543. if (test_and_set_bit(idx, cpuc->used_mask))
  544. return -EAGAIN;
  545. sparc_pmu_disable_event(cpuc, hwc, idx);
  546. cpuc->events[idx] = event;
  547. set_bit(idx, cpuc->active_mask);
  548. sparc_perf_event_set_period(event, hwc, idx);
  549. sparc_pmu_enable_event(cpuc, hwc, idx);
  550. perf_event_update_userpage(event);
  551. return 0;
  552. }
  553. static u64 sparc_perf_event_update(struct perf_event *event,
  554. struct hw_perf_event *hwc, int idx)
  555. {
  556. int shift = 64 - 32;
  557. u64 prev_raw_count, new_raw_count;
  558. s64 delta;
  559. again:
  560. prev_raw_count = atomic64_read(&hwc->prev_count);
  561. new_raw_count = read_pmc(idx);
  562. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  563. new_raw_count) != prev_raw_count)
  564. goto again;
  565. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  566. delta >>= shift;
  567. atomic64_add(delta, &event->count);
  568. atomic64_sub(delta, &hwc->period_left);
  569. return new_raw_count;
  570. }
  571. static void sparc_pmu_disable(struct perf_event *event)
  572. {
  573. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  574. struct hw_perf_event *hwc = &event->hw;
  575. int idx = hwc->idx;
  576. clear_bit(idx, cpuc->active_mask);
  577. sparc_pmu_disable_event(cpuc, hwc, idx);
  578. barrier();
  579. sparc_perf_event_update(event, hwc, idx);
  580. cpuc->events[idx] = NULL;
  581. clear_bit(idx, cpuc->used_mask);
  582. perf_event_update_userpage(event);
  583. }
  584. static void sparc_pmu_read(struct perf_event *event)
  585. {
  586. struct hw_perf_event *hwc = &event->hw;
  587. sparc_perf_event_update(event, hwc, hwc->idx);
  588. }
  589. static void sparc_pmu_unthrottle(struct perf_event *event)
  590. {
  591. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  592. struct hw_perf_event *hwc = &event->hw;
  593. sparc_pmu_enable_event(cpuc, hwc, hwc->idx);
  594. }
  595. static atomic_t active_events = ATOMIC_INIT(0);
  596. static DEFINE_MUTEX(pmc_grab_mutex);
  597. static void perf_stop_nmi_watchdog(void *unused)
  598. {
  599. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  600. stop_nmi_watchdog(NULL);
  601. cpuc->pcr = pcr_ops->read();
  602. }
  603. void perf_event_grab_pmc(void)
  604. {
  605. if (atomic_inc_not_zero(&active_events))
  606. return;
  607. mutex_lock(&pmc_grab_mutex);
  608. if (atomic_read(&active_events) == 0) {
  609. if (atomic_read(&nmi_active) > 0) {
  610. on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
  611. BUG_ON(atomic_read(&nmi_active) != 0);
  612. }
  613. atomic_inc(&active_events);
  614. }
  615. mutex_unlock(&pmc_grab_mutex);
  616. }
  617. void perf_event_release_pmc(void)
  618. {
  619. if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
  620. if (atomic_read(&nmi_active) == 0)
  621. on_each_cpu(start_nmi_watchdog, NULL, 1);
  622. mutex_unlock(&pmc_grab_mutex);
  623. }
  624. }
  625. static const struct perf_event_map *sparc_map_cache_event(u64 config)
  626. {
  627. unsigned int cache_type, cache_op, cache_result;
  628. const struct perf_event_map *pmap;
  629. if (!sparc_pmu->cache_map)
  630. return ERR_PTR(-ENOENT);
  631. cache_type = (config >> 0) & 0xff;
  632. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  633. return ERR_PTR(-EINVAL);
  634. cache_op = (config >> 8) & 0xff;
  635. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  636. return ERR_PTR(-EINVAL);
  637. cache_result = (config >> 16) & 0xff;
  638. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  639. return ERR_PTR(-EINVAL);
  640. pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
  641. if (pmap->encoding == CACHE_OP_UNSUPPORTED)
  642. return ERR_PTR(-ENOENT);
  643. if (pmap->encoding == CACHE_OP_NONSENSE)
  644. return ERR_PTR(-EINVAL);
  645. return pmap;
  646. }
  647. static void hw_perf_event_destroy(struct perf_event *event)
  648. {
  649. perf_event_release_pmc();
  650. }
  651. /* Make sure all events can be scheduled into the hardware at
  652. * the same time. This is simplified by the fact that we only
  653. * need to support 2 simultaneous HW events.
  654. */
  655. static int sparc_check_constraints(unsigned long *events, int n_ev)
  656. {
  657. if (n_ev <= perf_max_events) {
  658. u8 msk1, msk2;
  659. u16 dummy;
  660. if (n_ev == 1)
  661. return 0;
  662. BUG_ON(n_ev != 2);
  663. perf_event_decode(events[0], &dummy, &msk1);
  664. perf_event_decode(events[1], &dummy, &msk2);
  665. /* If both events can go on any counter, OK. */
  666. if (msk1 == (PIC_UPPER | PIC_LOWER) &&
  667. msk2 == (PIC_UPPER | PIC_LOWER))
  668. return 0;
  669. /* If one event is limited to a specific counter,
  670. * and the other can go on both, OK.
  671. */
  672. if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
  673. msk2 == (PIC_UPPER | PIC_LOWER))
  674. return 0;
  675. if ((msk2 == PIC_UPPER || msk2 == PIC_LOWER) &&
  676. msk1 == (PIC_UPPER | PIC_LOWER))
  677. return 0;
  678. /* If the events are fixed to different counters, OK. */
  679. if ((msk1 == PIC_UPPER && msk2 == PIC_LOWER) ||
  680. (msk1 == PIC_LOWER && msk2 == PIC_UPPER))
  681. return 0;
  682. /* Otherwise, there is a conflict. */
  683. }
  684. return -1;
  685. }
  686. static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
  687. {
  688. int eu = 0, ek = 0, eh = 0;
  689. struct perf_event *event;
  690. int i, n, first;
  691. n = n_prev + n_new;
  692. if (n <= 1)
  693. return 0;
  694. first = 1;
  695. for (i = 0; i < n; i++) {
  696. event = evts[i];
  697. if (first) {
  698. eu = event->attr.exclude_user;
  699. ek = event->attr.exclude_kernel;
  700. eh = event->attr.exclude_hv;
  701. first = 0;
  702. } else if (event->attr.exclude_user != eu ||
  703. event->attr.exclude_kernel != ek ||
  704. event->attr.exclude_hv != eh) {
  705. return -EAGAIN;
  706. }
  707. }
  708. return 0;
  709. }
  710. static int collect_events(struct perf_event *group, int max_count,
  711. struct perf_event *evts[], unsigned long *events)
  712. {
  713. struct perf_event *event;
  714. int n = 0;
  715. if (!is_software_event(group)) {
  716. if (n >= max_count)
  717. return -1;
  718. evts[n] = group;
  719. events[n++] = group->hw.event_base;
  720. }
  721. list_for_each_entry(event, &group->sibling_list, group_entry) {
  722. if (!is_software_event(event) &&
  723. event->state != PERF_EVENT_STATE_OFF) {
  724. if (n >= max_count)
  725. return -1;
  726. evts[n] = event;
  727. events[n++] = event->hw.event_base;
  728. }
  729. }
  730. return n;
  731. }
  732. static int __hw_perf_event_init(struct perf_event *event)
  733. {
  734. struct perf_event_attr *attr = &event->attr;
  735. struct perf_event *evts[MAX_HWEVENTS];
  736. struct hw_perf_event *hwc = &event->hw;
  737. unsigned long events[MAX_HWEVENTS];
  738. const struct perf_event_map *pmap;
  739. u64 enc;
  740. int n;
  741. if (atomic_read(&nmi_active) < 0)
  742. return -ENODEV;
  743. if (attr->type == PERF_TYPE_HARDWARE) {
  744. if (attr->config >= sparc_pmu->max_events)
  745. return -EINVAL;
  746. pmap = sparc_pmu->event_map(attr->config);
  747. } else if (attr->type == PERF_TYPE_HW_CACHE) {
  748. pmap = sparc_map_cache_event(attr->config);
  749. if (IS_ERR(pmap))
  750. return PTR_ERR(pmap);
  751. } else
  752. return -EOPNOTSUPP;
  753. /* We save the enable bits in the config_base. So to
  754. * turn off sampling just write 'config', and to enable
  755. * things write 'config | config_base'.
  756. */
  757. hwc->config_base = sparc_pmu->irq_bit;
  758. if (!attr->exclude_user)
  759. hwc->config_base |= PCR_UTRACE;
  760. if (!attr->exclude_kernel)
  761. hwc->config_base |= PCR_STRACE;
  762. if (!attr->exclude_hv)
  763. hwc->config_base |= sparc_pmu->hv_bit;
  764. hwc->event_base = perf_event_encode(pmap);
  765. enc = pmap->encoding;
  766. n = 0;
  767. if (event->group_leader != event) {
  768. n = collect_events(event->group_leader,
  769. perf_max_events - 1,
  770. evts, events);
  771. if (n < 0)
  772. return -EINVAL;
  773. }
  774. events[n] = hwc->event_base;
  775. evts[n] = event;
  776. if (check_excludes(evts, n, 1))
  777. return -EINVAL;
  778. if (sparc_check_constraints(events, n + 1))
  779. return -EINVAL;
  780. /* Try to do all error checking before this point, as unwinding
  781. * state after grabbing the PMC is difficult.
  782. */
  783. perf_event_grab_pmc();
  784. event->destroy = hw_perf_event_destroy;
  785. if (!hwc->sample_period) {
  786. hwc->sample_period = MAX_PERIOD;
  787. hwc->last_period = hwc->sample_period;
  788. atomic64_set(&hwc->period_left, hwc->sample_period);
  789. }
  790. if (pmap->pic_mask & PIC_UPPER) {
  791. hwc->idx = PIC_UPPER_INDEX;
  792. enc <<= sparc_pmu->upper_shift;
  793. } else {
  794. hwc->idx = PIC_LOWER_INDEX;
  795. enc <<= sparc_pmu->lower_shift;
  796. }
  797. hwc->config |= enc;
  798. return 0;
  799. }
  800. static const struct pmu pmu = {
  801. .enable = sparc_pmu_enable,
  802. .disable = sparc_pmu_disable,
  803. .read = sparc_pmu_read,
  804. .unthrottle = sparc_pmu_unthrottle,
  805. };
  806. const struct pmu *hw_perf_event_init(struct perf_event *event)
  807. {
  808. int err = __hw_perf_event_init(event);
  809. if (err)
  810. return ERR_PTR(err);
  811. return &pmu;
  812. }
  813. void perf_event_print_debug(void)
  814. {
  815. unsigned long flags;
  816. u64 pcr, pic;
  817. int cpu;
  818. if (!sparc_pmu)
  819. return;
  820. local_irq_save(flags);
  821. cpu = smp_processor_id();
  822. pcr = pcr_ops->read();
  823. read_pic(pic);
  824. pr_info("\n");
  825. pr_info("CPU#%d: PCR[%016llx] PIC[%016llx]\n",
  826. cpu, pcr, pic);
  827. local_irq_restore(flags);
  828. }
  829. static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
  830. unsigned long cmd, void *__args)
  831. {
  832. struct die_args *args = __args;
  833. struct perf_sample_data data;
  834. struct cpu_hw_events *cpuc;
  835. struct pt_regs *regs;
  836. int idx;
  837. if (!atomic_read(&active_events))
  838. return NOTIFY_DONE;
  839. switch (cmd) {
  840. case DIE_NMI:
  841. break;
  842. default:
  843. return NOTIFY_DONE;
  844. }
  845. regs = args->regs;
  846. data.addr = 0;
  847. cpuc = &__get_cpu_var(cpu_hw_events);
  848. for (idx = 0; idx < MAX_HWEVENTS; idx++) {
  849. struct perf_event *event = cpuc->events[idx];
  850. struct hw_perf_event *hwc;
  851. u64 val;
  852. if (!test_bit(idx, cpuc->active_mask))
  853. continue;
  854. hwc = &event->hw;
  855. val = sparc_perf_event_update(event, hwc, idx);
  856. if (val & (1ULL << 31))
  857. continue;
  858. data.period = event->hw.last_period;
  859. if (!sparc_perf_event_set_period(event, hwc, idx))
  860. continue;
  861. if (perf_event_overflow(event, 1, &data, regs))
  862. sparc_pmu_disable_event(cpuc, hwc, idx);
  863. }
  864. return NOTIFY_STOP;
  865. }
  866. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  867. .notifier_call = perf_event_nmi_handler,
  868. };
  869. static bool __init supported_pmu(void)
  870. {
  871. if (!strcmp(sparc_pmu_type, "ultra3") ||
  872. !strcmp(sparc_pmu_type, "ultra3+") ||
  873. !strcmp(sparc_pmu_type, "ultra3i") ||
  874. !strcmp(sparc_pmu_type, "ultra4+")) {
  875. sparc_pmu = &ultra3_pmu;
  876. return true;
  877. }
  878. if (!strcmp(sparc_pmu_type, "niagara")) {
  879. sparc_pmu = &niagara1_pmu;
  880. return true;
  881. }
  882. if (!strcmp(sparc_pmu_type, "niagara2")) {
  883. sparc_pmu = &niagara2_pmu;
  884. return true;
  885. }
  886. return false;
  887. }
  888. void __init init_hw_perf_events(void)
  889. {
  890. pr_info("Performance events: ");
  891. if (!supported_pmu()) {
  892. pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
  893. return;
  894. }
  895. pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
  896. /* All sparc64 PMUs currently have 2 events. But this simple
  897. * driver only supports one active event at a time.
  898. */
  899. perf_max_events = 1;
  900. register_die_notifier(&perf_event_nmi_notifier);
  901. }