1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2013 Imagination Technologies
4 * Author: Paul Burton <paul.burton@mips.com>
7 #include <linux/bitfield.h>
8 #include <linux/errno.h>
9 #include <linux/percpu.h>
11 #include <linux/of_address.h>
12 #include <linux/spinlock.h>
14 #include <asm/mips-cps.h>
16 void __iomem *mips_cpc_base;
18 static DEFINE_PER_CPU_ALIGNED(spinlock_t, cpc_core_lock);
20 static DEFINE_PER_CPU_ALIGNED(unsigned long, cpc_core_lock_flags);
22 phys_addr_t __weak mips_cpc_default_phys_base(void)
24 struct device_node *cpc_node;
28 cpc_node = of_find_compatible_node(of_root, NULL, "mti,mips-cpc");
30 err = of_address_to_resource(cpc_node, 0, &res);
39 * mips_cpc_phys_base - retrieve the physical base address of the CPC
41 * This function returns the physical base address of the Cluster Power
42 * Controller memory mapped registers, or 0 if no Cluster Power Controller
45 static phys_addr_t mips_cpc_phys_base(void)
47 unsigned long cpc_base;
49 if (!mips_cm_present())
52 if (!(read_gcr_cpc_status() & CM_GCR_CPC_STATUS_EX))
55 /* If the CPC is already enabled, leave it so */
56 cpc_base = read_gcr_cpc_base();
57 if (cpc_base & CM_GCR_CPC_BASE_CPCEN)
58 return cpc_base & CM_GCR_CPC_BASE_CPCBASE;
60 /* Otherwise, use the default address */
61 cpc_base = mips_cpc_default_phys_base();
65 /* Enable the CPC, mapped at the default address */
66 write_gcr_cpc_base(cpc_base | CM_GCR_CPC_BASE_CPCEN);
70 int mips_cpc_probe(void)
75 for_each_possible_cpu(cpu)
76 spin_lock_init(&per_cpu(cpc_core_lock, cpu));
78 addr = mips_cpc_phys_base();
82 mips_cpc_base = ioremap(addr, 0x8000);
89 void mips_cpc_lock_other(unsigned int core)
91 unsigned int curr_core;
93 if (mips_cm_revision() >= CM_REV_CM3)
94 /* Systems with CM >= 3 lock the CPC via mips_cm_lock_other */
98 curr_core = cpu_core(¤t_cpu_data);
99 spin_lock_irqsave(&per_cpu(cpc_core_lock, curr_core),
100 per_cpu(cpc_core_lock_flags, curr_core));
101 write_cpc_cl_other(FIELD_PREP(CPC_Cx_OTHER_CORENUM, core));
104 * Ensure the core-other region reflects the appropriate core &
105 * VP before any accesses to it occur.
110 void mips_cpc_unlock_other(void)
112 unsigned int curr_core;
114 if (mips_cm_revision() >= CM_REV_CM3)
115 /* Systems with CM >= 3 lock the CPC via mips_cm_lock_other */
118 curr_core = cpu_core(¤t_cpu_data);
119 spin_unlock_irqrestore(&per_cpu(cpc_core_lock, curr_core),
120 per_cpu(cpc_core_lock_flags, curr_core));