op_model_mpcore.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * @file op_model_mpcore.c
  3. * MPCORE Event Monitor Driver
  4. * @remark Copyright 2004 ARM SMP Development Team
  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. * Counters:
  17. * 0: PMN0 on CPU0, per-cpu configurable event counter
  18. * 1: PMN1 on CPU0, per-cpu configurable event counter
  19. * 2: CCNT on CPU0
  20. * 3: PMN0 on CPU1
  21. * 4: PMN1 on CPU1
  22. * 5: CCNT on CPU1
  23. * 6: PMN0 on CPU1
  24. * 7: PMN1 on CPU1
  25. * 8: CCNT on CPU1
  26. * 9: PMN0 on CPU1
  27. * 10: PMN1 on CPU1
  28. * 11: CCNT on CPU1
  29. * 12-19: configurable SCU event counters
  30. */
  31. /* #define DEBUG */
  32. #include <linux/types.h>
  33. #include <linux/errno.h>
  34. #include <linux/sched.h>
  35. #include <linux/oprofile.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/smp.h>
  38. #include <linux/io.h>
  39. #include <asm/irq.h>
  40. #include <asm/mach/irq.h>
  41. #include <mach/hardware.h>
  42. #include <mach/board-eb.h>
  43. #include <asm/system.h>
  44. #include "op_counter.h"
  45. #include "op_arm_model.h"
  46. #include "op_model_arm11_core.h"
  47. #include "op_model_mpcore.h"
  48. /*
  49. * MPCore SCU event monitor support
  50. */
  51. #define SCU_EVENTMONITORS_VA_BASE __io_address(REALVIEW_EB11MP_SCU_BASE + 0x10)
  52. /*
  53. * Bitmask of used SCU counters
  54. */
  55. static unsigned int scu_em_used;
  56. /*
  57. * 2 helper fns take a counter number from 0-7 (not the userspace-visible counter number)
  58. */
  59. static inline void scu_reset_counter(struct eventmonitor __iomem *emc, unsigned int n)
  60. {
  61. writel(-(u32)counter_config[SCU_COUNTER(n)].count, &emc->MC[n]);
  62. }
  63. static inline void scu_set_event(struct eventmonitor __iomem *emc, unsigned int n, u32 event)
  64. {
  65. event &= 0xff;
  66. writeb(event, &emc->MCEB[n]);
  67. }
  68. /*
  69. * SCU counters' IRQ handler (one IRQ per counter => 2 IRQs per CPU)
  70. */
  71. static irqreturn_t scu_em_interrupt(int irq, void *arg)
  72. {
  73. struct eventmonitor __iomem *emc = SCU_EVENTMONITORS_VA_BASE;
  74. unsigned int cnt;
  75. cnt = irq - IRQ_EB11MP_PMU_SCU0;
  76. oprofile_add_sample(get_irq_regs(), SCU_COUNTER(cnt));
  77. scu_reset_counter(emc, cnt);
  78. /* Clear overflow flag for this counter */
  79. writel(1 << (cnt + 16), &emc->PMCR);
  80. return IRQ_HANDLED;
  81. }
  82. /* Configure just the SCU counters that the user has requested */
  83. static void scu_setup(void)
  84. {
  85. struct eventmonitor __iomem *emc = SCU_EVENTMONITORS_VA_BASE;
  86. unsigned int i;
  87. scu_em_used = 0;
  88. for (i = 0; i < NUM_SCU_COUNTERS; i++) {
  89. if (counter_config[SCU_COUNTER(i)].enabled &&
  90. counter_config[SCU_COUNTER(i)].event) {
  91. scu_set_event(emc, i, 0); /* disable counter for now */
  92. scu_em_used |= 1 << i;
  93. }
  94. }
  95. }
  96. static int scu_start(void)
  97. {
  98. struct eventmonitor __iomem *emc = SCU_EVENTMONITORS_VA_BASE;
  99. unsigned int temp, i;
  100. unsigned long event;
  101. int ret = 0;
  102. /*
  103. * request the SCU counter interrupts that we need
  104. */
  105. for (i = 0; i < NUM_SCU_COUNTERS; i++) {
  106. if (scu_em_used & (1 << i)) {
  107. ret = request_irq(IRQ_EB11MP_PMU_SCU0 + i, scu_em_interrupt, IRQF_DISABLED, "SCU PMU", NULL);
  108. if (ret) {
  109. printk(KERN_ERR "oprofile: unable to request IRQ%u for SCU Event Monitor\n",
  110. IRQ_EB11MP_PMU_SCU0 + i);
  111. goto err_free_scu;
  112. }
  113. }
  114. }
  115. /*
  116. * clear overflow and enable interrupt for all used counters
  117. */
  118. temp = readl(&emc->PMCR);
  119. for (i = 0; i < NUM_SCU_COUNTERS; i++) {
  120. if (scu_em_used & (1 << i)) {
  121. scu_reset_counter(emc, i);
  122. event = counter_config[SCU_COUNTER(i)].event;
  123. scu_set_event(emc, i, event);
  124. /* clear overflow/interrupt */
  125. temp |= 1 << (i + 16);
  126. /* enable interrupt*/
  127. temp |= 1 << (i + 8);
  128. }
  129. }
  130. /* Enable all 8 counters */
  131. temp |= PMCR_E;
  132. writel(temp, &emc->PMCR);
  133. return 0;
  134. err_free_scu:
  135. while (i--)
  136. free_irq(IRQ_EB11MP_PMU_SCU0 + i, NULL);
  137. return ret;
  138. }
  139. static void scu_stop(void)
  140. {
  141. struct eventmonitor __iomem *emc = SCU_EVENTMONITORS_VA_BASE;
  142. unsigned int temp, i;
  143. /* Disable counter interrupts */
  144. /* Don't disable all 8 counters (with the E bit) as they may be in use */
  145. temp = readl(&emc->PMCR);
  146. for (i = 0; i < NUM_SCU_COUNTERS; i++) {
  147. if (scu_em_used & (1 << i))
  148. temp &= ~(1 << (i + 8));
  149. }
  150. writel(temp, &emc->PMCR);
  151. /* Free counter interrupts and reset counters */
  152. for (i = 0; i < NUM_SCU_COUNTERS; i++) {
  153. if (scu_em_used & (1 << i)) {
  154. scu_reset_counter(emc, i);
  155. free_irq(IRQ_EB11MP_PMU_SCU0 + i, NULL);
  156. }
  157. }
  158. }
  159. struct em_function_data {
  160. int (*fn)(void);
  161. int ret;
  162. };
  163. static void em_func(void *data)
  164. {
  165. struct em_function_data *d = data;
  166. int ret = d->fn();
  167. if (ret)
  168. d->ret = ret;
  169. }
  170. static int em_call_function(int (*fn)(void))
  171. {
  172. struct em_function_data data;
  173. data.fn = fn;
  174. data.ret = 0;
  175. preempt_disable();
  176. smp_call_function(em_func, &data, 1);
  177. em_func(&data);
  178. preempt_enable();
  179. return data.ret;
  180. }
  181. /*
  182. * Glue to stick the individual ARM11 PMUs and the SCU
  183. * into the oprofile framework.
  184. */
  185. static int em_setup_ctrs(void)
  186. {
  187. int ret;
  188. /* Configure CPU counters by cross-calling to the other CPUs */
  189. ret = em_call_function(arm11_setup_pmu);
  190. if (ret == 0)
  191. scu_setup();
  192. return 0;
  193. }
  194. static int arm11_irqs[] = {
  195. [0] = IRQ_EB11MP_PMU_CPU0,
  196. [1] = IRQ_EB11MP_PMU_CPU1,
  197. [2] = IRQ_EB11MP_PMU_CPU2,
  198. [3] = IRQ_EB11MP_PMU_CPU3
  199. };
  200. static int em_start(void)
  201. {
  202. int ret;
  203. ret = arm11_request_interrupts(arm11_irqs, ARRAY_SIZE(arm11_irqs));
  204. if (ret == 0) {
  205. em_call_function(arm11_start_pmu);
  206. ret = scu_start();
  207. if (ret)
  208. arm11_release_interrupts(arm11_irqs, ARRAY_SIZE(arm11_irqs));
  209. }
  210. return ret;
  211. }
  212. static void em_stop(void)
  213. {
  214. em_call_function(arm11_stop_pmu);
  215. arm11_release_interrupts(arm11_irqs, ARRAY_SIZE(arm11_irqs));
  216. scu_stop();
  217. }
  218. /*
  219. * Why isn't there a function to route an IRQ to a specific CPU in
  220. * genirq?
  221. */
  222. static void em_route_irq(int irq, unsigned int cpu)
  223. {
  224. struct irq_desc *desc = irq_desc + irq;
  225. const struct cpumask *mask = cpumask_of(cpu);
  226. spin_lock_irq(&desc->lock);
  227. cpumask_copy(desc->affinity, mask);
  228. desc->chip->set_affinity(irq, mask);
  229. spin_unlock_irq(&desc->lock);
  230. }
  231. static int em_setup(void)
  232. {
  233. /*
  234. * Send SCU PMU interrupts to the "owner" CPU.
  235. */
  236. em_route_irq(IRQ_EB11MP_PMU_SCU0, 0);
  237. em_route_irq(IRQ_EB11MP_PMU_SCU1, 0);
  238. em_route_irq(IRQ_EB11MP_PMU_SCU2, 1);
  239. em_route_irq(IRQ_EB11MP_PMU_SCU3, 1);
  240. em_route_irq(IRQ_EB11MP_PMU_SCU4, 2);
  241. em_route_irq(IRQ_EB11MP_PMU_SCU5, 2);
  242. em_route_irq(IRQ_EB11MP_PMU_SCU6, 3);
  243. em_route_irq(IRQ_EB11MP_PMU_SCU7, 3);
  244. /*
  245. * Send CP15 PMU interrupts to the owner CPU.
  246. */
  247. em_route_irq(IRQ_EB11MP_PMU_CPU0, 0);
  248. em_route_irq(IRQ_EB11MP_PMU_CPU1, 1);
  249. em_route_irq(IRQ_EB11MP_PMU_CPU2, 2);
  250. em_route_irq(IRQ_EB11MP_PMU_CPU3, 3);
  251. return 0;
  252. }
  253. struct op_arm_model_spec op_mpcore_spec = {
  254. .init = em_setup,
  255. .num_counters = MPCORE_NUM_COUNTERS,
  256. .setup_ctrs = em_setup_ctrs,
  257. .start = em_start,
  258. .stop = em_stop,
  259. .name = "arm/mpcore",
  260. };