cbe_regs.c 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * cbe_regs.c
 *
 * Accessor routines for the various MMIO register blocks of the CBE
 *
 * (c) 2006 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
 */

#include <linux/percpu.h>
#include <linux/types.h>
11
#include <linux/module.h>
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/prom.h>
#include <asm/ptrace.h>

#include "cbe_regs.h"

/*
 * Current implementation uses "cpu" nodes. We build our own mapping
 * array of cpu numbers to cpu nodes locally for now to allow interrupt
 * time code to have a fast path rather than call of_get_cpu_node(). If
 * we implement cpu hotplug, we'll have to install an appropriate norifier
 * in order to release references to the cpu going away
 */
static struct cbe_regs_map
{
	struct device_node *cpu_node;
	struct cbe_pmd_regs __iomem *pmd_regs;
	struct cbe_iic_regs __iomem *iic_regs;
32
	struct cbe_mic_tm_regs __iomem *mic_tm_regs;
33
	struct cbe_pmd_shadow_regs pmd_shadow_regs;
34 35 36 37 38 39 40
} cbe_regs_maps[MAX_CBE];
static int cbe_regs_map_count;

static struct cbe_thread_map
{
	struct device_node *cpu_node;
	struct cbe_regs_map *regs;
41 42
	unsigned int thread_id;
	unsigned int cbe_id;
43 44
} cbe_thread_map[NR_CPUS];

45 46 47
static cpumask_t cbe_local_mask[MAX_CBE] = { [0 ... MAX_CBE-1] = CPU_MASK_NONE };
static cpumask_t cbe_first_online_cpu = CPU_MASK_NONE;

48 49 50
static struct cbe_regs_map *cbe_find_map(struct device_node *np)
{
	int i;
51 52 53 54 55 56 57 58 59 60 61 62 63
	struct device_node *tmp_np;

	if (strcasecmp(np->type, "spe") == 0) {
		if (np->data == NULL) {
			/* walk up path until cpu node was found */
			tmp_np = np->parent;
			while (tmp_np != NULL && strcasecmp(tmp_np->type, "cpu") != 0)
				tmp_np = tmp_np->parent;

			np->data = cbe_find_map(tmp_np);
		}
		return np->data;
	}
64 65 66 67 68 69 70 71 72 73 74 75 76 77

	for (i = 0; i < cbe_regs_map_count; i++)
		if (cbe_regs_maps[i].cpu_node == np)
			return &cbe_regs_maps[i];
	return NULL;
}

struct cbe_pmd_regs __iomem *cbe_get_pmd_regs(struct device_node *np)
{
	struct cbe_regs_map *map = cbe_find_map(np);
	if (map == NULL)
		return NULL;
	return map->pmd_regs;
}
78
EXPORT_SYMBOL_GPL(cbe_get_pmd_regs);
79 80 81 82 83 84 85 86

struct cbe_pmd_regs __iomem *cbe_get_cpu_pmd_regs(int cpu)
{
	struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
	if (map == NULL)
		return NULL;
	return map->pmd_regs;
}
87
EXPORT_SYMBOL_GPL(cbe_get_cpu_pmd_regs);
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
struct cbe_pmd_shadow_regs *cbe_get_pmd_shadow_regs(struct device_node *np)
{
	struct cbe_regs_map *map = cbe_find_map(np);
	if (map == NULL)
		return NULL;
	return &map->pmd_shadow_regs;
}

struct cbe_pmd_shadow_regs *cbe_get_cpu_pmd_shadow_regs(int cpu)
{
	struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
	if (map == NULL)
		return NULL;
	return &map->pmd_shadow_regs;
}

105 106 107 108 109 110 111
struct cbe_iic_regs __iomem *cbe_get_iic_regs(struct device_node *np)
{
	struct cbe_regs_map *map = cbe_find_map(np);
	if (map == NULL)
		return NULL;
	return map->iic_regs;
}
112

113 114 115 116 117 118 119 120
struct cbe_iic_regs __iomem *cbe_get_cpu_iic_regs(int cpu)
{
	struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
	if (map == NULL)
		return NULL;
	return map->iic_regs;
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
struct cbe_mic_tm_regs __iomem *cbe_get_mic_tm_regs(struct device_node *np)
{
	struct cbe_regs_map *map = cbe_find_map(np);
	if (map == NULL)
		return NULL;
	return map->mic_tm_regs;
}

struct cbe_mic_tm_regs __iomem *cbe_get_cpu_mic_tm_regs(int cpu)
{
	struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
	if (map == NULL)
		return NULL;
	return map->mic_tm_regs;
}
EXPORT_SYMBOL_GPL(cbe_get_cpu_mic_tm_regs);

138 139
u32 cbe_get_hw_thread_id(int cpu)
{
140
	return cbe_thread_map[cpu].thread_id;
141 142
}
EXPORT_SYMBOL_GPL(cbe_get_hw_thread_id);
143

144 145 146 147 148 149 150 151 152 153 154 155
u32 cbe_cpu_to_node(int cpu)
{
	return cbe_thread_map[cpu].cbe_id;
}
EXPORT_SYMBOL_GPL(cbe_cpu_to_node);

u32 cbe_node_to_cpu(int node)
{
	return find_first_bit( (unsigned long *) &cbe_local_mask[node], sizeof(cpumask_t));
}
EXPORT_SYMBOL_GPL(cbe_node_to_cpu);

156 157 158
void __init cbe_regs_init(void)
{
	int i;
159
	unsigned int thread_id;
160 161 162
	struct device_node *cpu;

	/* Build local fast map of CPUs */
163 164 165 166
	for_each_possible_cpu(i) {
		cbe_thread_map[i].cpu_node = of_get_cpu_node(i, &thread_id);
		cbe_thread_map[i].thread_id = thread_id;
	}
167 168 169

	/* Find maps for each device tree CPU */
	for_each_node_by_type(cpu, "cpu") {
170 171
		struct cbe_regs_map *map;
		unsigned int cbe_id;
172 173

		/* That hack must die die die ! */
174
		const struct address_prop {
175 176 177 178
			unsigned long address;
			unsigned int len;
		} __attribute__((packed)) *prop;

179 180
		cbe_id = cbe_regs_map_count++;
		map = &cbe_regs_maps[cbe_id];
181 182 183 184 185 186 187 188

		if (cbe_regs_map_count > MAX_CBE) {
			printk(KERN_ERR "cbe_regs: More BE chips than supported"
			       "!\n");
			cbe_regs_map_count--;
			return;
		}
		map->cpu_node = cpu;
189 190 191 192 193 194 195 196 197 198 199 200

		for_each_possible_cpu(i) {
			struct cbe_thread_map *thread = &cbe_thread_map[i];

			if (thread->cpu_node == cpu) {
				thread->regs = map;
				thread->cbe_id = cbe_id;
				cpu_set(i, cbe_local_mask[cbe_id]);
				if(thread->thread_id == 0)
					cpu_set(i, cbe_first_online_cpu);
			}
		}
201

202
		prop = of_get_property(cpu, "pervasive", NULL);
203 204 205
		if (prop != NULL)
			map->pmd_regs = ioremap(prop->address, prop->len);

206
		prop = of_get_property(cpu, "iic", NULL);
207 208
		if (prop != NULL)
			map->iic_regs = ioremap(prop->address, prop->len);
209

210
		prop = of_get_property(cpu, "mic-tm", NULL);
211 212
		if (prop != NULL)
			map->mic_tm_regs = ioremap(prop->address, prop->len);
213 214 215
	}
}