[POWERPC] cell: Fix spufs with "new style" device-tree
[linux-2.6.22.y-op.git] / arch / powerpc / platforms / cell / spu_priv1_mmio.c
blobc805e63b7d5cf0b4dff628f828286366c8f1fa3e
1 /*
2 * spu hypervisor abstraction for direct hardware access.
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 * Copyright 2006 Sony Corp.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/interrupt.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/ptrace.h>
25 #include <linux/slab.h>
26 #include <linux/wait.h>
27 #include <linux/mm.h>
28 #include <linux/io.h>
29 #include <linux/mutex.h>
30 #include <linux/device.h>
32 #include <asm/spu.h>
33 #include <asm/spu_priv1.h>
34 #include <asm/firmware.h>
35 #include <asm/prom.h>
37 #include "interrupt.h"
38 #include "spu_priv1_mmio.h"
40 static DEFINE_MUTEX(add_spumem_mutex);
42 struct spu_pdata {
43 int nid;
44 struct device_node *devnode;
45 struct spu_priv1 __iomem *priv1;
48 static struct spu_pdata *spu_get_pdata(struct spu *spu)
50 BUG_ON(!spu->pdata);
51 return spu->pdata;
54 struct device_node *spu_devnode(struct spu *spu)
56 return spu_get_pdata(spu)->devnode;
59 EXPORT_SYMBOL_GPL(spu_devnode);
61 static int __init find_spu_node_id(struct device_node *spe)
63 const unsigned int *id;
64 struct device_node *cpu;
65 cpu = spe->parent->parent;
66 id = get_property(cpu, "node-id", NULL);
67 return id ? *id : 0;
70 static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
71 const char *prop)
73 const struct address_prop {
74 unsigned long address;
75 unsigned int len;
76 } __attribute__((packed)) *p;
77 int proplen;
79 unsigned long start_pfn, nr_pages;
80 struct pglist_data *pgdata;
81 struct zone *zone;
82 int ret;
84 p = get_property(spe, prop, &proplen);
85 WARN_ON(proplen != sizeof (*p));
87 start_pfn = p->address >> PAGE_SHIFT;
88 nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
90 pgdata = NODE_DATA(spu_get_pdata(spu)->nid);
91 zone = pgdata->node_zones;
93 /* XXX rethink locking here */
94 mutex_lock(&add_spumem_mutex);
95 ret = __add_pages(zone, start_pfn, nr_pages);
96 mutex_unlock(&add_spumem_mutex);
98 return ret;
101 static void __iomem * __init map_spe_prop(struct spu *spu,
102 struct device_node *n, const char *name)
104 const struct address_prop {
105 unsigned long address;
106 unsigned int len;
107 } __attribute__((packed)) *prop;
109 const void *p;
110 int proplen;
111 void __iomem *ret = NULL;
112 int err = 0;
114 p = get_property(n, name, &proplen);
115 if (proplen != sizeof (struct address_prop))
116 return NULL;
118 prop = p;
120 err = cell_spuprop_present(spu, n, name);
121 if (err && (err != -EEXIST))
122 goto out;
124 ret = ioremap(prop->address, prop->len);
126 out:
127 return ret;
130 static void spu_unmap(struct spu *spu)
132 iounmap(spu->priv2);
133 iounmap(spu_get_pdata(spu)->priv1);
134 iounmap(spu->problem);
135 iounmap((__force u8 __iomem *)spu->local_store);
138 static int __init spu_map_interrupts_old(struct spu *spu,
139 struct device_node *np)
141 unsigned int isrc;
142 const u32 *tmp;
144 /* Get the interrupt source unit from the device-tree */
145 tmp = get_property(np, "isrc", NULL);
146 if (!tmp)
147 return -ENODEV;
148 isrc = tmp[0];
150 /* Add the node number */
151 isrc |= spu->node << IIC_IRQ_NODE_SHIFT;
153 /* Now map interrupts of all 3 classes */
154 spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
155 spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
156 spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
158 /* Right now, we only fail if class 2 failed */
159 return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
162 static int __init spu_map_device_old(struct spu *spu, struct device_node *node)
164 const char *prop;
165 int ret;
167 ret = -ENODEV;
168 spu->name = get_property(node, "name", NULL);
169 if (!spu->name)
170 goto out;
172 prop = get_property(node, "local-store", NULL);
173 if (!prop)
174 goto out;
175 spu->local_store_phys = *(unsigned long *)prop;
177 /* we use local store as ram, not io memory */
178 spu->local_store = (void __force *)
179 map_spe_prop(spu, node, "local-store");
180 if (!spu->local_store)
181 goto out;
183 prop = get_property(node, "problem", NULL);
184 if (!prop)
185 goto out_unmap;
186 spu->problem_phys = *(unsigned long *)prop;
188 spu->problem= map_spe_prop(spu, node, "problem");
189 if (!spu->problem)
190 goto out_unmap;
192 spu_get_pdata(spu)->priv1= map_spe_prop(spu, node, "priv1");
194 spu->priv2= map_spe_prop(spu, node, "priv2");
195 if (!spu->priv2)
196 goto out_unmap;
197 ret = 0;
198 goto out;
200 out_unmap:
201 spu_unmap(spu);
202 out:
203 return ret;
206 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
208 struct of_irq oirq;
209 int ret;
210 int i;
212 for (i=0; i < 3; i++) {
213 ret = of_irq_map_one(np, i, &oirq);
214 if (ret) {
215 pr_debug("spu_new: failed to get irq %d\n", i);
216 goto err;
218 ret = -EINVAL;
219 pr_debug(" irq %d no 0x%x on %s\n", i, oirq.specifier[0],
220 oirq.controller->full_name);
221 spu->irqs[i] = irq_create_of_mapping(oirq.controller,
222 oirq.specifier, oirq.size);
223 if (spu->irqs[i] == NO_IRQ) {
224 pr_debug("spu_new: failed to map it !\n");
225 goto err;
228 return 0;
230 err:
231 pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
232 spu->name);
233 for (; i >= 0; i--) {
234 if (spu->irqs[i] != NO_IRQ)
235 irq_dispose_mapping(spu->irqs[i]);
237 return ret;
240 static int spu_map_resource(struct spu *spu, int nr,
241 void __iomem** virt, unsigned long *phys)
243 struct device_node *np = spu_get_pdata(spu)->devnode;
244 unsigned long start_pfn, nr_pages;
245 struct pglist_data *pgdata;
246 struct zone *zone;
247 struct resource resource = { };
248 unsigned long len;
249 int ret;
251 ret = of_address_to_resource(np, nr, &resource);
252 if (ret)
253 goto out;
255 if (phys)
256 *phys = resource.start;
257 len = resource.end - resource.start + 1;
258 *virt = ioremap(resource.start, len);
259 if (!*virt)
260 ret = -EINVAL;
262 start_pfn = resource.start >> PAGE_SHIFT;
263 nr_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
265 pgdata = NODE_DATA(spu_get_pdata(spu)->nid);
266 zone = pgdata->node_zones;
268 /* XXX rethink locking here */
269 mutex_lock(&add_spumem_mutex);
270 ret = __add_pages(zone, start_pfn, nr_pages);
271 mutex_unlock(&add_spumem_mutex);
273 out:
274 return ret;
277 static int __init spu_map_device(struct spu *spu)
279 struct device_node *np = spu_get_pdata(spu)->devnode;
280 int ret = -ENODEV;
282 spu->name = get_property(np, "name", NULL);
283 if (!spu->name)
284 goto out;
286 ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
287 &spu->local_store_phys);
288 if (ret) {
289 pr_debug("spu_new: failed to map %s resource 0\n",
290 np->full_name);
291 goto out;
293 ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
294 &spu->problem_phys);
295 if (ret) {
296 pr_debug("spu_new: failed to map %s resource 1\n",
297 np->full_name);
298 goto out_unmap;
300 ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
301 if (ret) {
302 pr_debug("spu_new: failed to map %s resource 2\n",
303 np->full_name);
304 goto out_unmap;
306 if (!firmware_has_feature(FW_FEATURE_LPAR))
307 ret = spu_map_resource(spu, 3,
308 (void __iomem**)&spu_get_pdata(spu)->priv1, NULL);
309 if (ret) {
310 pr_debug("spu_new: failed to map %s resource 3\n",
311 np->full_name);
312 goto out_unmap;
314 pr_debug("spu_new: %s maps:\n", np->full_name);
315 pr_debug(" local store : 0x%016lx -> 0x%p\n",
316 spu->local_store_phys, spu->local_store);
317 pr_debug(" problem state : 0x%016lx -> 0x%p\n",
318 spu->problem_phys, spu->problem);
319 pr_debug(" priv2 : 0x%p\n", spu->priv2);
320 pr_debug(" priv1 : 0x%p\n",
321 spu_get_pdata(spu)->priv1);
323 return 0;
325 out_unmap:
326 spu_unmap(spu);
327 out:
328 pr_debug("failed to map spe %s: %d\n", spu->name, ret);
329 return ret;
332 static int __init of_enumerate_spus(int (*fn)(void *data))
334 int ret;
335 struct device_node *node;
337 ret = -ENODEV;
338 for (node = of_find_node_by_type(NULL, "spe");
339 node; node = of_find_node_by_type(node, "spe")) {
340 ret = fn(node);
341 if (ret) {
342 printk(KERN_WARNING "%s: Error initializing %s\n",
343 __FUNCTION__, node->name);
344 break;
347 return ret;
350 static int __init of_create_spu(struct spu *spu, void *data)
352 int ret;
353 struct device_node *spe = (struct device_node *)data;
355 spu->pdata = kzalloc(sizeof(struct spu_pdata),
356 GFP_KERNEL);
357 if (!spu->pdata) {
358 ret = -ENOMEM;
359 goto out;
361 spu_get_pdata(spu)->devnode = of_node_get(spe);
363 spu->node = find_spu_node_id(spe);
364 if (spu->node >= MAX_NUMNODES) {
365 printk(KERN_WARNING "SPE %s on node %d ignored,"
366 " node number too big\n", spe->full_name, spu->node);
367 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
368 ret = -ENODEV;
369 goto out_free;
372 spu_get_pdata(spu)->nid = of_node_to_nid(spe);
373 if (spu_get_pdata(spu)->nid == -1)
374 spu_get_pdata(spu)->nid = 0;
376 ret = spu_map_device(spu);
377 /* try old method */
378 if (ret)
379 ret = spu_map_device_old(spu, spe);
380 if (ret)
381 goto out_free;
383 ret = spu_map_interrupts(spu, spe);
384 if (ret)
385 ret = spu_map_interrupts_old(spu, spe);
386 if (ret)
387 goto out_unmap;
389 pr_debug(KERN_DEBUG "Using SPE %s %p %p %p %p %d\n", spu->name,
390 spu->local_store, spu->problem, spu_get_pdata(spu)->priv1,
391 spu->priv2, spu->number);
392 goto out;
394 out_unmap:
395 spu_unmap(spu);
396 out_free:
397 kfree(spu->pdata);
398 spu->pdata = NULL;
399 out:
400 return ret;
403 static int of_destroy_spu(struct spu *spu)
405 spu_unmap(spu);
406 of_node_put(spu_get_pdata(spu)->devnode);
407 kfree(spu->pdata);
408 spu->pdata = NULL;
409 return 0;
412 const struct spu_management_ops spu_management_of_ops = {
413 .enumerate_spus = of_enumerate_spus,
414 .create_spu = of_create_spu,
415 .destroy_spu = of_destroy_spu,
418 static void int_mask_and(struct spu *spu, int class, u64 mask)
420 u64 old_mask;
422 old_mask = in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
423 out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class],
424 old_mask & mask);
427 static void int_mask_or(struct spu *spu, int class, u64 mask)
429 u64 old_mask;
431 old_mask = in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
432 out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class],
433 old_mask | mask);
436 static void int_mask_set(struct spu *spu, int class, u64 mask)
438 out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class], mask);
441 static u64 int_mask_get(struct spu *spu, int class)
443 return in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
446 static void int_stat_clear(struct spu *spu, int class, u64 stat)
448 out_be64(&spu_get_pdata(spu)->priv1->int_stat_RW[class], stat);
451 static u64 int_stat_get(struct spu *spu, int class)
453 return in_be64(&spu_get_pdata(spu)->priv1->int_stat_RW[class]);
456 static void cpu_affinity_set(struct spu *spu, int cpu)
458 u64 target = iic_get_target_id(cpu);
459 u64 route = target << 48 | target << 32 | target << 16;
460 out_be64(&spu_get_pdata(spu)->priv1->int_route_RW, route);
463 static u64 mfc_dar_get(struct spu *spu)
465 return in_be64(&spu_get_pdata(spu)->priv1->mfc_dar_RW);
468 static u64 mfc_dsisr_get(struct spu *spu)
470 return in_be64(&spu_get_pdata(spu)->priv1->mfc_dsisr_RW);
473 static void mfc_dsisr_set(struct spu *spu, u64 dsisr)
475 out_be64(&spu_get_pdata(spu)->priv1->mfc_dsisr_RW, dsisr);
478 static void mfc_sdr_setup(struct spu *spu)
480 out_be64(&spu_get_pdata(spu)->priv1->mfc_sdr_RW, mfspr(SPRN_SDR1));
483 static void mfc_sr1_set(struct spu *spu, u64 sr1)
485 out_be64(&spu_get_pdata(spu)->priv1->mfc_sr1_RW, sr1);
488 static u64 mfc_sr1_get(struct spu *spu)
490 return in_be64(&spu_get_pdata(spu)->priv1->mfc_sr1_RW);
493 static void mfc_tclass_id_set(struct spu *spu, u64 tclass_id)
495 out_be64(&spu_get_pdata(spu)->priv1->mfc_tclass_id_RW, tclass_id);
498 static u64 mfc_tclass_id_get(struct spu *spu)
500 return in_be64(&spu_get_pdata(spu)->priv1->mfc_tclass_id_RW);
503 static void tlb_invalidate(struct spu *spu)
505 out_be64(&spu_get_pdata(spu)->priv1->tlb_invalidate_entry_W, 0ul);
508 static void resource_allocation_groupID_set(struct spu *spu, u64 id)
510 out_be64(&spu_get_pdata(spu)->priv1->resource_allocation_groupID_RW,
511 id);
514 static u64 resource_allocation_groupID_get(struct spu *spu)
516 return in_be64(
517 &spu_get_pdata(spu)->priv1->resource_allocation_groupID_RW);
520 static void resource_allocation_enable_set(struct spu *spu, u64 enable)
522 out_be64(&spu_get_pdata(spu)->priv1->resource_allocation_enable_RW,
523 enable);
526 static u64 resource_allocation_enable_get(struct spu *spu)
528 return in_be64(
529 &spu_get_pdata(spu)->priv1->resource_allocation_enable_RW);
532 const struct spu_priv1_ops spu_priv1_mmio_ops =
534 .int_mask_and = int_mask_and,
535 .int_mask_or = int_mask_or,
536 .int_mask_set = int_mask_set,
537 .int_mask_get = int_mask_get,
538 .int_stat_clear = int_stat_clear,
539 .int_stat_get = int_stat_get,
540 .cpu_affinity_set = cpu_affinity_set,
541 .mfc_dar_get = mfc_dar_get,
542 .mfc_dsisr_get = mfc_dsisr_get,
543 .mfc_dsisr_set = mfc_dsisr_set,
544 .mfc_sdr_setup = mfc_sdr_setup,
545 .mfc_sr1_set = mfc_sr1_set,
546 .mfc_sr1_get = mfc_sr1_get,
547 .mfc_tclass_id_set = mfc_tclass_id_set,
548 .mfc_tclass_id_get = mfc_tclass_id_get,
549 .tlb_invalidate = tlb_invalidate,
550 .resource_allocation_groupID_set = resource_allocation_groupID_set,
551 .resource_allocation_groupID_get = resource_allocation_groupID_get,
552 .resource_allocation_enable_set = resource_allocation_enable_set,
553 .resource_allocation_enable_get = resource_allocation_enable_get,