perf_event.c 36 KB

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