pmc.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * linux/arch/ppc64/kernel/pmc.c
  3. *
  4. * Copyright (C) 2004 David Gibson, IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/config.h>
  12. #include <linux/errno.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/module.h>
  15. #include <asm/processor.h>
  16. #include <asm/pmc.h>
  17. /* Ensure exceptions are disabled */
  18. static void dummy_perf(struct pt_regs *regs)
  19. {
  20. unsigned int mmcr0 = mfspr(SPRN_MMCR0);
  21. mmcr0 &= ~(MMCR0_PMXE|MMCR0_PMAO);
  22. mtspr(SPRN_MMCR0, mmcr0);
  23. }
  24. static spinlock_t pmc_owner_lock = SPIN_LOCK_UNLOCKED;
  25. static void *pmc_owner_caller; /* mostly for debugging */
  26. perf_irq_t perf_irq = dummy_perf;
  27. int reserve_pmc_hardware(perf_irq_t new_perf_irq)
  28. {
  29. int err = 0;
  30. spin_lock(&pmc_owner_lock);
  31. if (pmc_owner_caller) {
  32. printk(KERN_WARNING "reserve_pmc_hardware: "
  33. "PMC hardware busy (reserved by caller %p)\n",
  34. pmc_owner_caller);
  35. err = -EBUSY;
  36. goto out;
  37. }
  38. pmc_owner_caller = __builtin_return_address(0);
  39. perf_irq = new_perf_irq ? : dummy_perf;
  40. out:
  41. spin_unlock(&pmc_owner_lock);
  42. return err;
  43. }
  44. EXPORT_SYMBOL_GPL(reserve_pmc_hardware);
  45. void release_pmc_hardware(void)
  46. {
  47. spin_lock(&pmc_owner_lock);
  48. WARN_ON(! pmc_owner_caller);
  49. pmc_owner_caller = NULL;
  50. perf_irq = dummy_perf;
  51. spin_unlock(&pmc_owner_lock);
  52. }
  53. EXPORT_SYMBOL_GPL(release_pmc_hardware);
  54. void power4_enable_pmcs(void)
  55. {
  56. unsigned long hid0;
  57. hid0 = mfspr(HID0);
  58. hid0 |= 1UL << (63 - 20);
  59. /* POWER4 requires the following sequence */
  60. asm volatile(
  61. "sync\n"
  62. "mtspr %1, %0\n"
  63. "mfspr %0, %1\n"
  64. "mfspr %0, %1\n"
  65. "mfspr %0, %1\n"
  66. "mfspr %0, %1\n"
  67. "mfspr %0, %1\n"
  68. "mfspr %0, %1\n"
  69. "isync" : "=&r" (hid0) : "i" (HID0), "0" (hid0):
  70. "memory");
  71. }