perf_event.c 36 KB

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