mips-cpc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2013 Imagination Technologies
  3. * Author: Paul Burton <paul.burton@mips.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/percpu.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/spinlock.h>
  15. #include <asm/mips-cps.h>
  16. void __iomem *mips_cpc_base;
  17. static DEFINE_PER_CPU_ALIGNED(spinlock_t, cpc_core_lock);
  18. static DEFINE_PER_CPU_ALIGNED(unsigned long, cpc_core_lock_flags);
  19. phys_addr_t __weak mips_cpc_default_phys_base(void)
  20. {
  21. struct device_node *cpc_node;
  22. struct resource res;
  23. int err;
  24. cpc_node = of_find_compatible_node(of_root, NULL, "mti,mips-cpc");
  25. if (cpc_node) {
  26. err = of_address_to_resource(cpc_node, 0, &res);
  27. if (!err)
  28. return res.start;
  29. }
  30. return 0;
  31. }
  32. /**
  33. * mips_cpc_phys_base - retrieve the physical base address of the CPC
  34. *
  35. * This function returns the physical base address of the Cluster Power
  36. * Controller memory mapped registers, or 0 if no Cluster Power Controller
  37. * is present.
  38. */
  39. static phys_addr_t mips_cpc_phys_base(void)
  40. {
  41. unsigned long cpc_base;
  42. if (!mips_cm_present())
  43. return 0;
  44. if (!(read_gcr_cpc_status() & CM_GCR_CPC_STATUS_EX))
  45. return 0;
  46. /* If the CPC is already enabled, leave it so */
  47. cpc_base = read_gcr_cpc_base();
  48. if (cpc_base & CM_GCR_CPC_BASE_CPCEN)
  49. return cpc_base & CM_GCR_CPC_BASE_CPCBASE;
  50. /* Otherwise, use the default address */
  51. cpc_base = mips_cpc_default_phys_base();
  52. if (!cpc_base)
  53. return cpc_base;
  54. /* Enable the CPC, mapped at the default address */
  55. write_gcr_cpc_base(cpc_base | CM_GCR_CPC_BASE_CPCEN);
  56. return cpc_base;
  57. }
  58. int mips_cpc_probe(void)
  59. {
  60. phys_addr_t addr;
  61. unsigned int cpu;
  62. for_each_possible_cpu(cpu)
  63. spin_lock_init(&per_cpu(cpc_core_lock, cpu));
  64. addr = mips_cpc_phys_base();
  65. if (!addr)
  66. return -ENODEV;
  67. mips_cpc_base = ioremap_nocache(addr, 0x8000);
  68. if (!mips_cpc_base)
  69. return -ENXIO;
  70. return 0;
  71. }
  72. void mips_cpc_lock_other(unsigned int core)
  73. {
  74. unsigned int curr_core;
  75. if (mips_cm_revision() >= CM_REV_CM3)
  76. /* Systems with CM >= 3 lock the CPC via mips_cm_lock_other */
  77. return;
  78. preempt_disable();
  79. curr_core = cpu_core(&current_cpu_data);
  80. spin_lock_irqsave(&per_cpu(cpc_core_lock, curr_core),
  81. per_cpu(cpc_core_lock_flags, curr_core));
  82. write_cpc_cl_other(core << __ffs(CPC_Cx_OTHER_CORENUM));
  83. /*
  84. * Ensure the core-other region reflects the appropriate core &
  85. * VP before any accesses to it occur.
  86. */
  87. mb();
  88. }
  89. void mips_cpc_unlock_other(void)
  90. {
  91. unsigned int curr_core;
  92. if (mips_cm_revision() >= CM_REV_CM3)
  93. /* Systems with CM >= 3 lock the CPC via mips_cm_lock_other */
  94. return;
  95. curr_core = cpu_core(&current_cpu_data);
  96. spin_unlock_irqrestore(&per_cpu(cpc_core_lock, curr_core),
  97. per_cpu(cpc_core_lock_flags, curr_core));
  98. preempt_enable();
  99. }