pmc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2014 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/module.h>
  17. #include <linux/atomic.h>
  18. #include <linux/interrupt.h>
  19. #include <asm/processor.h>
  20. #include <asm/pmc.h>
  21. perf_irq_t perf_irq = NULL;
  22. int handle_perf_interrupt(struct pt_regs *regs, int fault)
  23. {
  24. int retval;
  25. if (!perf_irq)
  26. panic("Unexpected PERF_COUNT interrupt %d\n", fault);
  27. nmi_enter();
  28. retval = perf_irq(regs, fault);
  29. nmi_exit();
  30. return retval;
  31. }
  32. /* Reserve PMC hardware if it is available. */
  33. perf_irq_t reserve_pmc_hardware(perf_irq_t new_perf_irq)
  34. {
  35. return cmpxchg(&perf_irq, NULL, new_perf_irq);
  36. }
  37. EXPORT_SYMBOL(reserve_pmc_hardware);
  38. /* Release PMC hardware. */
  39. void release_pmc_hardware(void)
  40. {
  41. perf_irq = NULL;
  42. }
  43. EXPORT_SYMBOL(release_pmc_hardware);
  44. /*
  45. * Get current overflow status of each performance counter,
  46. * and auxiliary performance counter.
  47. */
  48. unsigned long
  49. pmc_get_overflow(void)
  50. {
  51. unsigned long status;
  52. /*
  53. * merge base+aux into a single vector
  54. */
  55. status = __insn_mfspr(SPR_PERF_COUNT_STS);
  56. status |= __insn_mfspr(SPR_AUX_PERF_COUNT_STS) << TILE_BASE_COUNTERS;
  57. return status;
  58. }
  59. /*
  60. * Clear the status bit for the corresponding counter, if written
  61. * with a one.
  62. */
  63. void
  64. pmc_ack_overflow(unsigned long status)
  65. {
  66. /*
  67. * clear overflow status by writing ones
  68. */
  69. __insn_mtspr(SPR_PERF_COUNT_STS, status);
  70. __insn_mtspr(SPR_AUX_PERF_COUNT_STS, status >> TILE_BASE_COUNTERS);
  71. }
  72. /*
  73. * The perf count interrupts are masked and unmasked explicitly,
  74. * and only here. The normal irq_enable() does not enable them,
  75. * and irq_disable() does not disable them. That lets these
  76. * routines drive the perf count interrupts orthogonally.
  77. *
  78. * We also mask the perf count interrupts on entry to the perf count
  79. * interrupt handler in assembly code, and by default unmask them
  80. * again (with interrupt critical section protection) just before
  81. * returning from the interrupt. If the perf count handler returns
  82. * a non-zero error code, then we don't re-enable them before returning.
  83. *
  84. * For Pro, we rely on both interrupts being in the same word to update
  85. * them atomically so we never have one enabled and one disabled.
  86. */
  87. #if CHIP_HAS_SPLIT_INTR_MASK()
  88. # if INT_PERF_COUNT < 32 || INT_AUX_PERF_COUNT < 32
  89. # error Fix assumptions about which word PERF_COUNT interrupts are in
  90. # endif
  91. #endif
  92. static inline unsigned long long pmc_mask(void)
  93. {
  94. unsigned long long mask = 1ULL << INT_PERF_COUNT;
  95. mask |= 1ULL << INT_AUX_PERF_COUNT;
  96. return mask;
  97. }
  98. void unmask_pmc_interrupts(void)
  99. {
  100. interrupt_mask_reset_mask(pmc_mask());
  101. }
  102. void mask_pmc_interrupts(void)
  103. {
  104. interrupt_mask_set_mask(pmc_mask());
  105. }