op_model_avr32.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * AVR32 Performance Counter Driver
  3. *
  4. * Copyright (C) 2005-2007 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Author: Ronny Pedersen
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/oprofile.h>
  16. #include <linux/sched.h>
  17. #include <linux/types.h>
  18. #include <asm/sysreg.h>
  19. #define AVR32_PERFCTR_IRQ_GROUP 0
  20. #define AVR32_PERFCTR_IRQ_LINE 1
  21. void avr32_backtrace(struct pt_regs * const regs, unsigned int depth);
  22. enum { PCCNT, PCNT0, PCNT1, NR_counter };
  23. struct avr32_perf_counter {
  24. unsigned long enabled;
  25. unsigned long event;
  26. unsigned long count;
  27. unsigned long unit_mask;
  28. unsigned long kernel;
  29. unsigned long user;
  30. u32 ie_mask;
  31. u32 flag_mask;
  32. };
  33. static struct avr32_perf_counter counter[NR_counter] = {
  34. {
  35. .ie_mask = SYSREG_BIT(IEC),
  36. .flag_mask = SYSREG_BIT(FC),
  37. }, {
  38. .ie_mask = SYSREG_BIT(IE0),
  39. .flag_mask = SYSREG_BIT(F0),
  40. }, {
  41. .ie_mask = SYSREG_BIT(IE1),
  42. .flag_mask = SYSREG_BIT(F1),
  43. },
  44. };
  45. static void avr32_perf_counter_reset(void)
  46. {
  47. /* Reset all counter and disable/clear all interrupts */
  48. sysreg_write(PCCR, (SYSREG_BIT(PCCR_R)
  49. | SYSREG_BIT(PCCR_C)
  50. | SYSREG_BIT(FC)
  51. | SYSREG_BIT(F0)
  52. | SYSREG_BIT(F1)));
  53. }
  54. static irqreturn_t avr32_perf_counter_interrupt(int irq, void *dev_id)
  55. {
  56. struct avr32_perf_counter *ctr = dev_id;
  57. struct pt_regs *regs;
  58. u32 pccr;
  59. if (likely(!(intc_get_pending(AVR32_PERFCTR_IRQ_GROUP)
  60. & (1 << AVR32_PERFCTR_IRQ_LINE))))
  61. return IRQ_NONE;
  62. regs = get_irq_regs();
  63. pccr = sysreg_read(PCCR);
  64. /* Clear the interrupt flags we're about to handle */
  65. sysreg_write(PCCR, pccr);
  66. /* PCCNT */
  67. if (ctr->enabled && (pccr & ctr->flag_mask)) {
  68. sysreg_write(PCCNT, -ctr->count);
  69. oprofile_add_sample(regs, PCCNT);
  70. }
  71. ctr++;
  72. /* PCNT0 */
  73. if (ctr->enabled && (pccr & ctr->flag_mask)) {
  74. sysreg_write(PCNT0, -ctr->count);
  75. oprofile_add_sample(regs, PCNT0);
  76. }
  77. ctr++;
  78. /* PCNT1 */
  79. if (ctr->enabled && (pccr & ctr->flag_mask)) {
  80. sysreg_write(PCNT1, -ctr->count);
  81. oprofile_add_sample(regs, PCNT1);
  82. }
  83. return IRQ_HANDLED;
  84. }
  85. static int avr32_perf_counter_create_files(struct super_block *sb,
  86. struct dentry *root)
  87. {
  88. struct dentry *dir;
  89. unsigned int i;
  90. char filename[4];
  91. for (i = 0; i < NR_counter; i++) {
  92. snprintf(filename, sizeof(filename), "%u", i);
  93. dir = oprofilefs_mkdir(sb, root, filename);
  94. oprofilefs_create_ulong(sb, dir, "enabled",
  95. &counter[i].enabled);
  96. oprofilefs_create_ulong(sb, dir, "event",
  97. &counter[i].event);
  98. oprofilefs_create_ulong(sb, dir, "count",
  99. &counter[i].count);
  100. /* Dummy entries */
  101. oprofilefs_create_ulong(sb, dir, "kernel",
  102. &counter[i].kernel);
  103. oprofilefs_create_ulong(sb, dir, "user",
  104. &counter[i].user);
  105. oprofilefs_create_ulong(sb, dir, "unit_mask",
  106. &counter[i].unit_mask);
  107. }
  108. return 0;
  109. }
  110. static int avr32_perf_counter_setup(void)
  111. {
  112. struct avr32_perf_counter *ctr;
  113. u32 pccr;
  114. int ret;
  115. int i;
  116. pr_debug("avr32_perf_counter_setup\n");
  117. if (sysreg_read(PCCR) & SYSREG_BIT(PCCR_E)) {
  118. printk(KERN_ERR
  119. "oprofile: setup: perf counter already enabled\n");
  120. return -EBUSY;
  121. }
  122. ret = request_irq(AVR32_PERFCTR_IRQ_GROUP,
  123. avr32_perf_counter_interrupt, IRQF_SHARED,
  124. "oprofile", counter);
  125. if (ret)
  126. return ret;
  127. avr32_perf_counter_reset();
  128. pccr = 0;
  129. for (i = PCCNT; i < NR_counter; i++) {
  130. ctr = &counter[i];
  131. if (!ctr->enabled)
  132. continue;
  133. pr_debug("enabling counter %d...\n", i);
  134. pccr |= ctr->ie_mask;
  135. switch (i) {
  136. case PCCNT:
  137. /* PCCNT always counts cycles, so no events */
  138. sysreg_write(PCCNT, -ctr->count);
  139. break;
  140. case PCNT0:
  141. pccr |= SYSREG_BF(CONF0, ctr->event);
  142. sysreg_write(PCNT0, -ctr->count);
  143. break;
  144. case PCNT1:
  145. pccr |= SYSREG_BF(CONF1, ctr->event);
  146. sysreg_write(PCNT1, -ctr->count);
  147. break;
  148. }
  149. }
  150. pr_debug("oprofile: writing 0x%x to PCCR...\n", pccr);
  151. sysreg_write(PCCR, pccr);
  152. return 0;
  153. }
  154. static void avr32_perf_counter_shutdown(void)
  155. {
  156. pr_debug("avr32_perf_counter_shutdown\n");
  157. avr32_perf_counter_reset();
  158. free_irq(AVR32_PERFCTR_IRQ_GROUP, counter);
  159. }
  160. static int avr32_perf_counter_start(void)
  161. {
  162. pr_debug("avr32_perf_counter_start\n");
  163. sysreg_write(PCCR, sysreg_read(PCCR) | SYSREG_BIT(PCCR_E));
  164. return 0;
  165. }
  166. static void avr32_perf_counter_stop(void)
  167. {
  168. pr_debug("avr32_perf_counter_stop\n");
  169. sysreg_write(PCCR, sysreg_read(PCCR) & ~SYSREG_BIT(PCCR_E));
  170. }
  171. static struct oprofile_operations avr32_perf_counter_ops __initdata = {
  172. .create_files = avr32_perf_counter_create_files,
  173. .setup = avr32_perf_counter_setup,
  174. .shutdown = avr32_perf_counter_shutdown,
  175. .start = avr32_perf_counter_start,
  176. .stop = avr32_perf_counter_stop,
  177. .cpu_type = "avr32",
  178. };
  179. int __init oprofile_arch_init(struct oprofile_operations *ops)
  180. {
  181. if (!(current_cpu_data.features & AVR32_FEATURE_PCTR))
  182. return -ENODEV;
  183. memcpy(ops, &avr32_perf_counter_ops,
  184. sizeof(struct oprofile_operations));
  185. ops->backtrace = avr32_backtrace;
  186. printk(KERN_INFO "oprofile: using AVR32 performance monitoring.\n");
  187. return 0;
  188. }
  189. void oprofile_arch_exit(void)
  190. {
  191. }