op_model_xscale.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /**
  2. * @file op_model_xscale.c
  3. * XScale Performance Monitor Driver
  4. *
  5. * @remark Copyright 2000-2004 Deepak Saxena <dsaxena@mvista.com>
  6. * @remark Copyright 2000-2004 MontaVista Software Inc
  7. * @remark Copyright 2004 Dave Jiang <dave.jiang@intel.com>
  8. * @remark Copyright 2004 Intel Corporation
  9. * @remark Copyright 2004 Zwane Mwaikambo <zwane@arm.linux.org.uk>
  10. * @remark Copyright 2004 OProfile Authors
  11. *
  12. * @remark Read the file COPYING
  13. *
  14. * @author Zwane Mwaikambo
  15. */
  16. /* #define DEBUG */
  17. #include <linux/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/sched.h>
  21. #include <linux/oprofile.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <asm/cputype.h>
  25. #include <asm/pmu.h>
  26. #include "op_counter.h"
  27. #include "op_arm_model.h"
  28. #define PMU_ENABLE 0x001 /* Enable counters */
  29. #define PMN_RESET 0x002 /* Reset event counters */
  30. #define CCNT_RESET 0x004 /* Reset clock counter */
  31. #define PMU_RESET (CCNT_RESET | PMN_RESET)
  32. #define PMU_CNT64 0x008 /* Make CCNT count every 64th cycle */
  33. /*
  34. * Different types of events that can be counted by the XScale PMU
  35. * as used by Oprofile userspace. Here primarily for documentation
  36. * purposes.
  37. */
  38. #define EVT_ICACHE_MISS 0x00
  39. #define EVT_ICACHE_NO_DELIVER 0x01
  40. #define EVT_DATA_STALL 0x02
  41. #define EVT_ITLB_MISS 0x03
  42. #define EVT_DTLB_MISS 0x04
  43. #define EVT_BRANCH 0x05
  44. #define EVT_BRANCH_MISS 0x06
  45. #define EVT_INSTRUCTION 0x07
  46. #define EVT_DCACHE_FULL_STALL 0x08
  47. #define EVT_DCACHE_FULL_STALL_CONTIG 0x09
  48. #define EVT_DCACHE_ACCESS 0x0A
  49. #define EVT_DCACHE_MISS 0x0B
  50. #define EVT_DCACE_WRITE_BACK 0x0C
  51. #define EVT_PC_CHANGED 0x0D
  52. #define EVT_BCU_REQUEST 0x10
  53. #define EVT_BCU_FULL 0x11
  54. #define EVT_BCU_DRAIN 0x12
  55. #define EVT_BCU_ECC_NO_ELOG 0x14
  56. #define EVT_BCU_1_BIT_ERR 0x15
  57. #define EVT_RMW 0x16
  58. /* EVT_CCNT is not hardware defined */
  59. #define EVT_CCNT 0xFE
  60. #define EVT_UNUSED 0xFF
  61. struct pmu_counter {
  62. volatile unsigned long ovf;
  63. unsigned long reset_counter;
  64. };
  65. enum { CCNT, PMN0, PMN1, PMN2, PMN3, MAX_COUNTERS };
  66. static struct pmu_counter results[MAX_COUNTERS];
  67. /*
  68. * There are two versions of the PMU in current XScale processors
  69. * with differing register layouts and number of performance counters.
  70. * e.g. IOP32x is xsc1 whilst IOP33x is xsc2.
  71. * We detect which register layout to use in xscale_detect_pmu()
  72. */
  73. enum { PMU_XSC1, PMU_XSC2 };
  74. struct pmu_type {
  75. int id;
  76. char *name;
  77. int num_counters;
  78. unsigned int int_enable;
  79. unsigned int cnt_ovf[MAX_COUNTERS];
  80. unsigned int int_mask[MAX_COUNTERS];
  81. };
  82. static struct pmu_type pmu_parms[] = {
  83. {
  84. .id = PMU_XSC1,
  85. .name = "arm/xscale1",
  86. .num_counters = 3,
  87. .int_mask = { [PMN0] = 0x10, [PMN1] = 0x20,
  88. [CCNT] = 0x40 },
  89. .cnt_ovf = { [CCNT] = 0x400, [PMN0] = 0x100,
  90. [PMN1] = 0x200},
  91. },
  92. {
  93. .id = PMU_XSC2,
  94. .name = "arm/xscale2",
  95. .num_counters = 5,
  96. .int_mask = { [CCNT] = 0x01, [PMN0] = 0x02,
  97. [PMN1] = 0x04, [PMN2] = 0x08,
  98. [PMN3] = 0x10 },
  99. .cnt_ovf = { [CCNT] = 0x01, [PMN0] = 0x02,
  100. [PMN1] = 0x04, [PMN2] = 0x08,
  101. [PMN3] = 0x10 },
  102. },
  103. };
  104. static struct pmu_type *pmu;
  105. static void write_pmnc(u32 val)
  106. {
  107. if (pmu->id == PMU_XSC1) {
  108. /* upper 4bits and 7, 11 are write-as-0 */
  109. val &= 0xffff77f;
  110. __asm__ __volatile__ ("mcr p14, 0, %0, c0, c0, 0" : : "r" (val));
  111. } else {
  112. /* bits 4-23 are write-as-0, 24-31 are write ignored */
  113. val &= 0xf;
  114. __asm__ __volatile__ ("mcr p14, 0, %0, c0, c1, 0" : : "r" (val));
  115. }
  116. }
  117. static u32 read_pmnc(void)
  118. {
  119. u32 val;
  120. if (pmu->id == PMU_XSC1)
  121. __asm__ __volatile__ ("mrc p14, 0, %0, c0, c0, 0" : "=r" (val));
  122. else {
  123. __asm__ __volatile__ ("mrc p14, 0, %0, c0, c1, 0" : "=r" (val));
  124. /* bits 1-2 and 4-23 are read-unpredictable */
  125. val &= 0xff000009;
  126. }
  127. return val;
  128. }
  129. static u32 __xsc1_read_counter(int counter)
  130. {
  131. u32 val = 0;
  132. switch (counter) {
  133. case CCNT:
  134. __asm__ __volatile__ ("mrc p14, 0, %0, c1, c0, 0" : "=r" (val));
  135. break;
  136. case PMN0:
  137. __asm__ __volatile__ ("mrc p14, 0, %0, c2, c0, 0" : "=r" (val));
  138. break;
  139. case PMN1:
  140. __asm__ __volatile__ ("mrc p14, 0, %0, c3, c0, 0" : "=r" (val));
  141. break;
  142. }
  143. return val;
  144. }
  145. static u32 __xsc2_read_counter(int counter)
  146. {
  147. u32 val = 0;
  148. switch (counter) {
  149. case CCNT:
  150. __asm__ __volatile__ ("mrc p14, 0, %0, c1, c1, 0" : "=r" (val));
  151. break;
  152. case PMN0:
  153. __asm__ __volatile__ ("mrc p14, 0, %0, c0, c2, 0" : "=r" (val));
  154. break;
  155. case PMN1:
  156. __asm__ __volatile__ ("mrc p14, 0, %0, c1, c2, 0" : "=r" (val));
  157. break;
  158. case PMN2:
  159. __asm__ __volatile__ ("mrc p14, 0, %0, c2, c2, 0" : "=r" (val));
  160. break;
  161. case PMN3:
  162. __asm__ __volatile__ ("mrc p14, 0, %0, c3, c2, 0" : "=r" (val));
  163. break;
  164. }
  165. return val;
  166. }
  167. static u32 read_counter(int counter)
  168. {
  169. u32 val;
  170. if (pmu->id == PMU_XSC1)
  171. val = __xsc1_read_counter(counter);
  172. else
  173. val = __xsc2_read_counter(counter);
  174. return val;
  175. }
  176. static void __xsc1_write_counter(int counter, u32 val)
  177. {
  178. switch (counter) {
  179. case CCNT:
  180. __asm__ __volatile__ ("mcr p14, 0, %0, c1, c0, 0" : : "r" (val));
  181. break;
  182. case PMN0:
  183. __asm__ __volatile__ ("mcr p14, 0, %0, c2, c0, 0" : : "r" (val));
  184. break;
  185. case PMN1:
  186. __asm__ __volatile__ ("mcr p14, 0, %0, c3, c0, 0" : : "r" (val));
  187. break;
  188. }
  189. }
  190. static void __xsc2_write_counter(int counter, u32 val)
  191. {
  192. switch (counter) {
  193. case CCNT:
  194. __asm__ __volatile__ ("mcr p14, 0, %0, c1, c1, 0" : : "r" (val));
  195. break;
  196. case PMN0:
  197. __asm__ __volatile__ ("mcr p14, 0, %0, c0, c2, 0" : : "r" (val));
  198. break;
  199. case PMN1:
  200. __asm__ __volatile__ ("mcr p14, 0, %0, c1, c2, 0" : : "r" (val));
  201. break;
  202. case PMN2:
  203. __asm__ __volatile__ ("mcr p14, 0, %0, c2, c2, 0" : : "r" (val));
  204. break;
  205. case PMN3:
  206. __asm__ __volatile__ ("mcr p14, 0, %0, c3, c2, 0" : : "r" (val));
  207. break;
  208. }
  209. }
  210. static void write_counter(int counter, u32 val)
  211. {
  212. if (pmu->id == PMU_XSC1)
  213. __xsc1_write_counter(counter, val);
  214. else
  215. __xsc2_write_counter(counter, val);
  216. }
  217. static int xscale_setup_ctrs(void)
  218. {
  219. u32 evtsel, pmnc;
  220. int i;
  221. for (i = CCNT; i < MAX_COUNTERS; i++) {
  222. if (counter_config[i].enabled)
  223. continue;
  224. counter_config[i].event = EVT_UNUSED;
  225. }
  226. switch (pmu->id) {
  227. case PMU_XSC1:
  228. pmnc = (counter_config[PMN1].event << 20) | (counter_config[PMN0].event << 12);
  229. pr_debug("xscale_setup_ctrs: pmnc: %#08x\n", pmnc);
  230. write_pmnc(pmnc);
  231. break;
  232. case PMU_XSC2:
  233. evtsel = counter_config[PMN0].event | (counter_config[PMN1].event << 8) |
  234. (counter_config[PMN2].event << 16) | (counter_config[PMN3].event << 24);
  235. pr_debug("xscale_setup_ctrs: evtsel %#08x\n", evtsel);
  236. __asm__ __volatile__ ("mcr p14, 0, %0, c8, c1, 0" : : "r" (evtsel));
  237. break;
  238. }
  239. for (i = CCNT; i < MAX_COUNTERS; i++) {
  240. if (counter_config[i].event == EVT_UNUSED) {
  241. counter_config[i].event = 0;
  242. pmu->int_enable &= ~pmu->int_mask[i];
  243. continue;
  244. }
  245. results[i].reset_counter = counter_config[i].count;
  246. write_counter(i, -(u32)counter_config[i].count);
  247. pmu->int_enable |= pmu->int_mask[i];
  248. pr_debug("xscale_setup_ctrs: counter%d %#08x from %#08lx\n", i,
  249. read_counter(i), counter_config[i].count);
  250. }
  251. return 0;
  252. }
  253. static void inline __xsc1_check_ctrs(void)
  254. {
  255. int i;
  256. u32 pmnc = read_pmnc();
  257. /* NOTE: there's an A stepping errata that states if an overflow */
  258. /* bit already exists and another occurs, the previous */
  259. /* Overflow bit gets cleared. There's no workaround. */
  260. /* Fixed in B stepping or later */
  261. /* Write the value back to clear the overflow flags. Overflow */
  262. /* flags remain in pmnc for use below */
  263. write_pmnc(pmnc & ~PMU_ENABLE);
  264. for (i = CCNT; i <= PMN1; i++) {
  265. if (!(pmu->int_mask[i] & pmu->int_enable))
  266. continue;
  267. if (pmnc & pmu->cnt_ovf[i])
  268. results[i].ovf++;
  269. }
  270. }
  271. static void inline __xsc2_check_ctrs(void)
  272. {
  273. int i;
  274. u32 flag = 0, pmnc = read_pmnc();
  275. pmnc &= ~PMU_ENABLE;
  276. write_pmnc(pmnc);
  277. /* read overflow flag register */
  278. __asm__ __volatile__ ("mrc p14, 0, %0, c5, c1, 0" : "=r" (flag));
  279. for (i = CCNT; i <= PMN3; i++) {
  280. if (!(pmu->int_mask[i] & pmu->int_enable))
  281. continue;
  282. if (flag & pmu->cnt_ovf[i])
  283. results[i].ovf++;
  284. }
  285. /* writeback clears overflow bits */
  286. __asm__ __volatile__ ("mcr p14, 0, %0, c5, c1, 0" : : "r" (flag));
  287. }
  288. static irqreturn_t xscale_pmu_interrupt(int irq, void *arg)
  289. {
  290. int i;
  291. u32 pmnc;
  292. if (pmu->id == PMU_XSC1)
  293. __xsc1_check_ctrs();
  294. else
  295. __xsc2_check_ctrs();
  296. for (i = CCNT; i < MAX_COUNTERS; i++) {
  297. if (!results[i].ovf)
  298. continue;
  299. write_counter(i, -(u32)results[i].reset_counter);
  300. oprofile_add_sample(get_irq_regs(), i);
  301. results[i].ovf--;
  302. }
  303. pmnc = read_pmnc() | PMU_ENABLE;
  304. write_pmnc(pmnc);
  305. return IRQ_HANDLED;
  306. }
  307. static const struct pmu_irqs *pmu_irqs;
  308. static void xscale_pmu_stop(void)
  309. {
  310. u32 pmnc = read_pmnc();
  311. pmnc &= ~PMU_ENABLE;
  312. write_pmnc(pmnc);
  313. free_irq(pmu_irqs->irqs[0], results);
  314. release_pmu(pmu_irqs);
  315. pmu_irqs = NULL;
  316. }
  317. static int xscale_pmu_start(void)
  318. {
  319. int ret;
  320. u32 pmnc;
  321. pmu_irqs = reserve_pmu();
  322. if (IS_ERR(pmu_irqs))
  323. return PTR_ERR(pmu_irqs);
  324. pmnc = read_pmnc();
  325. ret = request_irq(pmu_irqs->irqs[0], xscale_pmu_interrupt,
  326. IRQF_DISABLED, "XScale PMU", (void *)results);
  327. if (ret < 0) {
  328. printk(KERN_ERR "oprofile: unable to request IRQ%d for XScale PMU\n",
  329. pmu_irqs->irqs[0]);
  330. release_pmu(pmu_irqs);
  331. pmu_irqs = NULL;
  332. return ret;
  333. }
  334. if (pmu->id == PMU_XSC1)
  335. pmnc |= pmu->int_enable;
  336. else {
  337. __asm__ __volatile__ ("mcr p14, 0, %0, c4, c1, 0" : : "r" (pmu->int_enable));
  338. pmnc &= ~PMU_CNT64;
  339. }
  340. pmnc |= PMU_ENABLE;
  341. write_pmnc(pmnc);
  342. pr_debug("xscale_pmu_start: pmnc: %#08x mask: %08x\n", pmnc, pmu->int_enable);
  343. return 0;
  344. }
  345. static int xscale_detect_pmu(void)
  346. {
  347. int ret = 0;
  348. u32 id;
  349. id = (read_cpuid(CPUID_ID) >> 13) & 0x7;
  350. switch (id) {
  351. case 1:
  352. pmu = &pmu_parms[PMU_XSC1];
  353. break;
  354. case 2:
  355. pmu = &pmu_parms[PMU_XSC2];
  356. break;
  357. default:
  358. ret = -ENODEV;
  359. break;
  360. }
  361. if (!ret) {
  362. op_xscale_spec.name = pmu->name;
  363. op_xscale_spec.num_counters = pmu->num_counters;
  364. pr_debug("xscale_detect_pmu: detected %s PMU\n", pmu->name);
  365. }
  366. return ret;
  367. }
  368. struct op_arm_model_spec op_xscale_spec = {
  369. .init = xscale_detect_pmu,
  370. .setup_ctrs = xscale_setup_ctrs,
  371. .start = xscale_pmu_start,
  372. .stop = xscale_pmu_stop,
  373. };