Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * cbe_regs.c
0004  *
0005  * Accessor routines for the various MMIO register blocks of the CBE
0006  *
0007  * (c) 2006 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
0008  */
0009 
0010 #include <linux/percpu.h>
0011 #include <linux/types.h>
0012 #include <linux/export.h>
0013 #include <linux/of_address.h>
0014 #include <linux/of_device.h>
0015 #include <linux/of_platform.h>
0016 #include <linux/pgtable.h>
0017 
0018 #include <asm/io.h>
0019 #include <asm/ptrace.h>
0020 #include <asm/cell-regs.h>
0021 
0022 /*
0023  * Current implementation uses "cpu" nodes. We build our own mapping
0024  * array of cpu numbers to cpu nodes locally for now to allow interrupt
0025  * time code to have a fast path rather than call of_get_cpu_node(). If
0026  * we implement cpu hotplug, we'll have to install an appropriate notifier
0027  * in order to release references to the cpu going away
0028  */
0029 static struct cbe_regs_map
0030 {
0031     struct device_node *cpu_node;
0032     struct device_node *be_node;
0033     struct cbe_pmd_regs __iomem *pmd_regs;
0034     struct cbe_iic_regs __iomem *iic_regs;
0035     struct cbe_mic_tm_regs __iomem *mic_tm_regs;
0036     struct cbe_pmd_shadow_regs pmd_shadow_regs;
0037 } cbe_regs_maps[MAX_CBE];
0038 static int cbe_regs_map_count;
0039 
0040 static struct cbe_thread_map
0041 {
0042     struct device_node *cpu_node;
0043     struct device_node *be_node;
0044     struct cbe_regs_map *regs;
0045     unsigned int thread_id;
0046     unsigned int cbe_id;
0047 } cbe_thread_map[NR_CPUS];
0048 
0049 static cpumask_t cbe_local_mask[MAX_CBE] = { [0 ... MAX_CBE-1] = {CPU_BITS_NONE} };
0050 static cpumask_t cbe_first_online_cpu = { CPU_BITS_NONE };
0051 
0052 static struct cbe_regs_map *cbe_find_map(struct device_node *np)
0053 {
0054     int i;
0055     struct device_node *tmp_np;
0056 
0057     if (!of_node_is_type(np, "spe")) {
0058         for (i = 0; i < cbe_regs_map_count; i++)
0059             if (cbe_regs_maps[i].cpu_node == np ||
0060                 cbe_regs_maps[i].be_node == np)
0061                 return &cbe_regs_maps[i];
0062         return NULL;
0063     }
0064 
0065     if (np->data)
0066         return np->data;
0067 
0068     /* walk up path until cpu or be node was found */
0069     tmp_np = np;
0070     do {
0071         tmp_np = tmp_np->parent;
0072         /* on a correct devicetree we wont get up to root */
0073         BUG_ON(!tmp_np);
0074     } while (!of_node_is_type(tmp_np, "cpu") ||
0075          !of_node_is_type(tmp_np, "be"));
0076 
0077     np->data = cbe_find_map(tmp_np);
0078 
0079     return np->data;
0080 }
0081 
0082 struct cbe_pmd_regs __iomem *cbe_get_pmd_regs(struct device_node *np)
0083 {
0084     struct cbe_regs_map *map = cbe_find_map(np);
0085     if (map == NULL)
0086         return NULL;
0087     return map->pmd_regs;
0088 }
0089 EXPORT_SYMBOL_GPL(cbe_get_pmd_regs);
0090 
0091 struct cbe_pmd_regs __iomem *cbe_get_cpu_pmd_regs(int cpu)
0092 {
0093     struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
0094     if (map == NULL)
0095         return NULL;
0096     return map->pmd_regs;
0097 }
0098 EXPORT_SYMBOL_GPL(cbe_get_cpu_pmd_regs);
0099 
0100 struct cbe_pmd_shadow_regs *cbe_get_pmd_shadow_regs(struct device_node *np)
0101 {
0102     struct cbe_regs_map *map = cbe_find_map(np);
0103     if (map == NULL)
0104         return NULL;
0105     return &map->pmd_shadow_regs;
0106 }
0107 
0108 struct cbe_pmd_shadow_regs *cbe_get_cpu_pmd_shadow_regs(int cpu)
0109 {
0110     struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
0111     if (map == NULL)
0112         return NULL;
0113     return &map->pmd_shadow_regs;
0114 }
0115 
0116 struct cbe_iic_regs __iomem *cbe_get_iic_regs(struct device_node *np)
0117 {
0118     struct cbe_regs_map *map = cbe_find_map(np);
0119     if (map == NULL)
0120         return NULL;
0121     return map->iic_regs;
0122 }
0123 
0124 struct cbe_iic_regs __iomem *cbe_get_cpu_iic_regs(int cpu)
0125 {
0126     struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
0127     if (map == NULL)
0128         return NULL;
0129     return map->iic_regs;
0130 }
0131 
0132 struct cbe_mic_tm_regs __iomem *cbe_get_mic_tm_regs(struct device_node *np)
0133 {
0134     struct cbe_regs_map *map = cbe_find_map(np);
0135     if (map == NULL)
0136         return NULL;
0137     return map->mic_tm_regs;
0138 }
0139 
0140 struct cbe_mic_tm_regs __iomem *cbe_get_cpu_mic_tm_regs(int cpu)
0141 {
0142     struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
0143     if (map == NULL)
0144         return NULL;
0145     return map->mic_tm_regs;
0146 }
0147 EXPORT_SYMBOL_GPL(cbe_get_cpu_mic_tm_regs);
0148 
0149 u32 cbe_get_hw_thread_id(int cpu)
0150 {
0151     return cbe_thread_map[cpu].thread_id;
0152 }
0153 EXPORT_SYMBOL_GPL(cbe_get_hw_thread_id);
0154 
0155 u32 cbe_cpu_to_node(int cpu)
0156 {
0157     return cbe_thread_map[cpu].cbe_id;
0158 }
0159 EXPORT_SYMBOL_GPL(cbe_cpu_to_node);
0160 
0161 u32 cbe_node_to_cpu(int node)
0162 {
0163     return cpumask_first(&cbe_local_mask[node]);
0164 
0165 }
0166 EXPORT_SYMBOL_GPL(cbe_node_to_cpu);
0167 
0168 static struct device_node *__init cbe_get_be_node(int cpu_id)
0169 {
0170     struct device_node *np;
0171 
0172     for_each_node_by_type (np, "be") {
0173         int len,i;
0174         const phandle *cpu_handle;
0175 
0176         cpu_handle = of_get_property(np, "cpus", &len);
0177 
0178         /*
0179          * the CAB SLOF tree is non compliant, so we just assume
0180          * there is only one node
0181          */
0182         if (WARN_ON_ONCE(!cpu_handle))
0183             return np;
0184 
0185         for (i=0; i<len; i++)
0186             if (of_find_node_by_phandle(cpu_handle[i]) == of_get_cpu_node(cpu_id, NULL))
0187                 return np;
0188     }
0189 
0190     return NULL;
0191 }
0192 
0193 static void __init cbe_fill_regs_map(struct cbe_regs_map *map)
0194 {
0195     if(map->be_node) {
0196         struct device_node *be, *np;
0197 
0198         be = map->be_node;
0199 
0200         for_each_node_by_type(np, "pervasive")
0201             if (of_get_parent(np) == be)
0202                 map->pmd_regs = of_iomap(np, 0);
0203 
0204         for_each_node_by_type(np, "CBEA-Internal-Interrupt-Controller")
0205             if (of_get_parent(np) == be)
0206                 map->iic_regs = of_iomap(np, 2);
0207 
0208         for_each_node_by_type(np, "mic-tm")
0209             if (of_get_parent(np) == be)
0210                 map->mic_tm_regs = of_iomap(np, 0);
0211     } else {
0212         struct device_node *cpu;
0213         /* That hack must die die die ! */
0214         const struct address_prop {
0215             unsigned long address;
0216             unsigned int len;
0217         } __attribute__((packed)) *prop;
0218 
0219         cpu = map->cpu_node;
0220 
0221         prop = of_get_property(cpu, "pervasive", NULL);
0222         if (prop != NULL)
0223             map->pmd_regs = ioremap(prop->address, prop->len);
0224 
0225         prop = of_get_property(cpu, "iic", NULL);
0226         if (prop != NULL)
0227             map->iic_regs = ioremap(prop->address, prop->len);
0228 
0229         prop = of_get_property(cpu, "mic-tm", NULL);
0230         if (prop != NULL)
0231             map->mic_tm_regs = ioremap(prop->address, prop->len);
0232     }
0233 }
0234 
0235 
0236 void __init cbe_regs_init(void)
0237 {
0238     int i;
0239     unsigned int thread_id;
0240     struct device_node *cpu;
0241 
0242     /* Build local fast map of CPUs */
0243     for_each_possible_cpu(i) {
0244         cbe_thread_map[i].cpu_node = of_get_cpu_node(i, &thread_id);
0245         cbe_thread_map[i].be_node = cbe_get_be_node(i);
0246         cbe_thread_map[i].thread_id = thread_id;
0247     }
0248 
0249     /* Find maps for each device tree CPU */
0250     for_each_node_by_type(cpu, "cpu") {
0251         struct cbe_regs_map *map;
0252         unsigned int cbe_id;
0253 
0254         cbe_id = cbe_regs_map_count++;
0255         map = &cbe_regs_maps[cbe_id];
0256 
0257         if (cbe_regs_map_count > MAX_CBE) {
0258             printk(KERN_ERR "cbe_regs: More BE chips than supported"
0259                    "!\n");
0260             cbe_regs_map_count--;
0261             of_node_put(cpu);
0262             return;
0263         }
0264         map->cpu_node = cpu;
0265 
0266         for_each_possible_cpu(i) {
0267             struct cbe_thread_map *thread = &cbe_thread_map[i];
0268 
0269             if (thread->cpu_node == cpu) {
0270                 thread->regs = map;
0271                 thread->cbe_id = cbe_id;
0272                 map->be_node = thread->be_node;
0273                 cpumask_set_cpu(i, &cbe_local_mask[cbe_id]);
0274                 if(thread->thread_id == 0)
0275                     cpumask_set_cpu(i, &cbe_first_online_cpu);
0276             }
0277         }
0278 
0279         cbe_fill_regs_map(map);
0280     }
0281 }
0282