op_model_mpcore.c 6.8 KB

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