Pull button into test branch
[linux-drm-fsl-dcu.git] / arch / powerpc / platforms / cell / spu_priv1_mmio.c
1 /*
2  * spu hypervisor abstraction for direct hardware access.
3  *
4  *  (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5  *  Copyright 2006 Sony Corp.
6  *
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.
10  *
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.
15  *
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
19  */
20
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>
31
32 #include <asm/spu.h>
33 #include <asm/spu_priv1.h>
34 #include <asm/firmware.h>
35 #include <asm/prom.h>
36
37 #include "interrupt.h"
38 #include "spu_priv1_mmio.h"
39
40 struct spu_pdata {
41         int nid;
42         struct device_node *devnode;
43         struct spu_priv1 __iomem *priv1;
44 };
45
46 static struct spu_pdata *spu_get_pdata(struct spu *spu)
47 {
48         BUG_ON(!spu->pdata);
49         return spu->pdata;
50 }
51
52 struct device_node *spu_devnode(struct spu *spu)
53 {
54         return spu_get_pdata(spu)->devnode;
55 }
56
57 EXPORT_SYMBOL_GPL(spu_devnode);
58
59 static int __init find_spu_node_id(struct device_node *spe)
60 {
61         const unsigned int *id;
62         struct device_node *cpu;
63         cpu = spe->parent->parent;
64         id = get_property(cpu, "node-id", NULL);
65         return id ? *id : 0;
66 }
67
68 static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
69                 const char *prop)
70 {
71         static DEFINE_MUTEX(add_spumem_mutex);
72
73         const struct address_prop {
74                 unsigned long address;
75                 unsigned int len;
76         } __attribute__((packed)) *p;
77         int proplen;
78
79         unsigned long start_pfn, nr_pages;
80         struct pglist_data *pgdata;
81         struct zone *zone;
82         int ret;
83
84         p = get_property(spe, prop, &proplen);
85         WARN_ON(proplen != sizeof (*p));
86
87         start_pfn = p->address >> PAGE_SHIFT;
88         nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
89
90         pgdata = NODE_DATA(spu_get_pdata(spu)->nid);
91         zone = pgdata->node_zones;
92
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);
97
98         return ret;
99 }
100
101 static void __iomem * __init map_spe_prop(struct spu *spu,
102                 struct device_node *n, const char *name)
103 {
104         const struct address_prop {
105                 unsigned long address;
106                 unsigned int len;
107         } __attribute__((packed)) *prop;
108
109         const void *p;
110         int proplen;
111         void __iomem *ret = NULL;
112         int err = 0;
113
114         p = get_property(n, name, &proplen);
115         if (proplen != sizeof (struct address_prop))
116                 return NULL;
117
118         prop = p;
119
120         err = cell_spuprop_present(spu, n, name);
121         if (err && (err != -EEXIST))
122                 goto out;
123
124         ret = ioremap(prop->address, prop->len);
125
126  out:
127         return ret;
128 }
129
130 static void spu_unmap(struct spu *spu)
131 {
132         iounmap(spu->priv2);
133         iounmap(spu_get_pdata(spu)->priv1);
134         iounmap(spu->problem);
135         iounmap((__force u8 __iomem *)spu->local_store);
136 }
137
138 static int __init spu_map_interrupts_old(struct spu *spu,
139         struct device_node *np)
140 {
141         unsigned int isrc;
142         const u32 *tmp;
143
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];
149
150         /* Add the node number */
151         isrc |= spu->node << IIC_IRQ_NODE_SHIFT;
152
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);
157
158         /* Right now, we only fail if class 2 failed */
159         return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
160 }
161
162 static int __init spu_map_device_old(struct spu *spu, struct device_node *node)
163 {
164         const char *prop;
165         int ret;
166
167         ret = -ENODEV;
168         spu->name = get_property(node, "name", NULL);
169         if (!spu->name)
170                 goto out;
171
172         prop = get_property(node, "local-store", NULL);
173         if (!prop)
174                 goto out;
175         spu->local_store_phys = *(unsigned long *)prop;
176
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;
182
183         prop = get_property(node, "problem", NULL);
184         if (!prop)
185                 goto out_unmap;
186         spu->problem_phys = *(unsigned long *)prop;
187
188         spu->problem= map_spe_prop(spu, node, "problem");
189         if (!spu->problem)
190                 goto out_unmap;
191
192         spu_get_pdata(spu)->priv1= map_spe_prop(spu, node, "priv1");
193
194         spu->priv2= map_spe_prop(spu, node, "priv2");
195         if (!spu->priv2)
196                 goto out_unmap;
197         ret = 0;
198         goto out;
199
200 out_unmap:
201         spu_unmap(spu);
202 out:
203         return ret;
204 }
205
206 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
207 {
208         struct of_irq oirq;
209         int ret;
210         int i;
211
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;
217                 }
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;
226                 }
227         }
228         return 0;
229
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]);
236         }
237         return ret;
238 }
239
240 static int spu_map_resource(struct device_node *node, int nr,
241                 void __iomem** virt, unsigned long *phys)
242 {
243         struct resource resource = { };
244         int ret;
245
246         ret = of_address_to_resource(node, nr, &resource);
247         if (ret)
248                 goto out;
249
250         if (phys)
251                 *phys = resource.start;
252         *virt = ioremap(resource.start, resource.end - resource.start);
253         if (!*virt)
254                 ret = -EINVAL;
255
256 out:
257         return ret;
258 }
259
260 static int __init spu_map_device(struct spu *spu, struct device_node *node)
261 {
262         int ret = -ENODEV;
263         spu->name = get_property(node, "name", NULL);
264         if (!spu->name)
265                 goto out;
266
267         ret = spu_map_resource(node, 0, (void __iomem**)&spu->local_store,
268                                         &spu->local_store_phys);
269         if (ret) {
270                 pr_debug("spu_new: failed to map %s resource 0\n",
271                          node->full_name);
272                 goto out;
273         }
274         ret = spu_map_resource(node, 1, (void __iomem**)&spu->problem,
275                                         &spu->problem_phys);
276         if (ret) {
277                 pr_debug("spu_new: failed to map %s resource 1\n",
278                          node->full_name);
279                 goto out_unmap;
280         }
281         ret = spu_map_resource(node, 2, (void __iomem**)&spu->priv2,
282                                         NULL);
283         if (ret) {
284                 pr_debug("spu_new: failed to map %s resource 2\n",
285                          node->full_name);
286                 goto out_unmap;
287         }
288         if (!firmware_has_feature(FW_FEATURE_LPAR))
289                 ret = spu_map_resource(node, 3,
290                         (void __iomem**)&spu_get_pdata(spu)->priv1, NULL);
291         if (ret) {
292                 pr_debug("spu_new: failed to map %s resource 3\n",
293                          node->full_name);
294                 goto out_unmap;
295         }
296         pr_debug("spu_new: %s maps:\n", node->full_name);
297         pr_debug("  local store   : 0x%016lx -> 0x%p\n",
298                  spu->local_store_phys, spu->local_store);
299         pr_debug("  problem state : 0x%016lx -> 0x%p\n",
300                  spu->problem_phys, spu->problem);
301         pr_debug("  priv2         :                       0x%p\n", spu->priv2);
302         pr_debug("  priv1         :                       0x%p\n",
303                                                 spu_get_pdata(spu)->priv1);
304
305         return 0;
306
307 out_unmap:
308         spu_unmap(spu);
309 out:
310         pr_debug("failed to map spe %s: %d\n", spu->name, ret);
311         return ret;
312 }
313
314 static int __init of_enumerate_spus(int (*fn)(void *data))
315 {
316         int ret;
317         struct device_node *node;
318
319         ret = -ENODEV;
320         for (node = of_find_node_by_type(NULL, "spe");
321                         node; node = of_find_node_by_type(node, "spe")) {
322                 ret = fn(node);
323                 if (ret) {
324                         printk(KERN_WARNING "%s: Error initializing %s\n",
325                                 __FUNCTION__, node->name);
326                         break;
327                 }
328         }
329         return ret;
330 }
331
332 static int __init of_create_spu(struct spu *spu, void *data)
333 {
334         int ret;
335         struct device_node *spe = (struct device_node *)data;
336
337         spu->pdata = kzalloc(sizeof(struct spu_pdata),
338                 GFP_KERNEL);
339         if (!spu->pdata) {
340                 ret = -ENOMEM;
341                 goto out;
342         }
343
344         spu->node = find_spu_node_id(spe);
345         if (spu->node >= MAX_NUMNODES) {
346                 printk(KERN_WARNING "SPE %s on node %d ignored,"
347                        " node number too big\n", spe->full_name, spu->node);
348                 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
349                 ret = -ENODEV;
350                 goto out_free;
351         }
352
353         spu_get_pdata(spu)->nid = of_node_to_nid(spe);
354         if (spu_get_pdata(spu)->nid == -1)
355                 spu_get_pdata(spu)->nid = 0;
356
357         ret = spu_map_device(spu, spe);
358         /* try old method */
359         if (ret)
360                 ret = spu_map_device_old(spu, spe);
361         if (ret)
362                 goto out_free;
363
364         ret = spu_map_interrupts(spu, spe);
365         if (ret)
366                 ret = spu_map_interrupts_old(spu, spe);
367         if (ret)
368                 goto out_unmap;
369
370         spu_get_pdata(spu)->devnode = of_node_get(spe);
371
372         pr_debug(KERN_DEBUG "Using SPE %s %p %p %p %p %d\n", spu->name,
373                 spu->local_store, spu->problem, spu_get_pdata(spu)->priv1,
374                 spu->priv2, spu->number);
375         goto out;
376
377 out_unmap:
378         spu_unmap(spu);
379 out_free:
380         kfree(spu->pdata);
381         spu->pdata = NULL;
382 out:
383         return ret;
384 }
385
386 static int of_destroy_spu(struct spu *spu)
387 {
388         spu_unmap(spu);
389         of_node_put(spu_get_pdata(spu)->devnode);
390         kfree(spu->pdata);
391         spu->pdata = NULL;
392         return 0;
393 }
394
395 const struct spu_management_ops spu_management_of_ops = {
396         .enumerate_spus = of_enumerate_spus,
397         .create_spu = of_create_spu,
398         .destroy_spu = of_destroy_spu,
399 };
400
401 static void int_mask_and(struct spu *spu, int class, u64 mask)
402 {
403         u64 old_mask;
404
405         old_mask = in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
406         out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class],
407                 old_mask & mask);
408 }
409
410 static void int_mask_or(struct spu *spu, int class, u64 mask)
411 {
412         u64 old_mask;
413
414         old_mask = in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
415         out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class],
416                 old_mask | mask);
417 }
418
419 static void int_mask_set(struct spu *spu, int class, u64 mask)
420 {
421         out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class], mask);
422 }
423
424 static u64 int_mask_get(struct spu *spu, int class)
425 {
426         return in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
427 }
428
429 static void int_stat_clear(struct spu *spu, int class, u64 stat)
430 {
431         out_be64(&spu_get_pdata(spu)->priv1->int_stat_RW[class], stat);
432 }
433
434 static u64 int_stat_get(struct spu *spu, int class)
435 {
436         return in_be64(&spu_get_pdata(spu)->priv1->int_stat_RW[class]);
437 }
438
439 static void cpu_affinity_set(struct spu *spu, int cpu)
440 {
441         u64 target = iic_get_target_id(cpu);
442         u64 route = target << 48 | target << 32 | target << 16;
443         out_be64(&spu_get_pdata(spu)->priv1->int_route_RW, route);
444 }
445
446 static u64 mfc_dar_get(struct spu *spu)
447 {
448         return in_be64(&spu_get_pdata(spu)->priv1->mfc_dar_RW);
449 }
450
451 static u64 mfc_dsisr_get(struct spu *spu)
452 {
453         return in_be64(&spu_get_pdata(spu)->priv1->mfc_dsisr_RW);
454 }
455
456 static void mfc_dsisr_set(struct spu *spu, u64 dsisr)
457 {
458         out_be64(&spu_get_pdata(spu)->priv1->mfc_dsisr_RW, dsisr);
459 }
460
461 static void mfc_sdr_setup(struct spu *spu)
462 {
463         out_be64(&spu_get_pdata(spu)->priv1->mfc_sdr_RW, mfspr(SPRN_SDR1));
464 }
465
466 static void mfc_sr1_set(struct spu *spu, u64 sr1)
467 {
468         out_be64(&spu_get_pdata(spu)->priv1->mfc_sr1_RW, sr1);
469 }
470
471 static u64 mfc_sr1_get(struct spu *spu)
472 {
473         return in_be64(&spu_get_pdata(spu)->priv1->mfc_sr1_RW);
474 }
475
476 static void mfc_tclass_id_set(struct spu *spu, u64 tclass_id)
477 {
478         out_be64(&spu_get_pdata(spu)->priv1->mfc_tclass_id_RW, tclass_id);
479 }
480
481 static u64 mfc_tclass_id_get(struct spu *spu)
482 {
483         return in_be64(&spu_get_pdata(spu)->priv1->mfc_tclass_id_RW);
484 }
485
486 static void tlb_invalidate(struct spu *spu)
487 {
488         out_be64(&spu_get_pdata(spu)->priv1->tlb_invalidate_entry_W, 0ul);
489 }
490
491 static void resource_allocation_groupID_set(struct spu *spu, u64 id)
492 {
493         out_be64(&spu_get_pdata(spu)->priv1->resource_allocation_groupID_RW,
494                 id);
495 }
496
497 static u64 resource_allocation_groupID_get(struct spu *spu)
498 {
499         return in_be64(
500                 &spu_get_pdata(spu)->priv1->resource_allocation_groupID_RW);
501 }
502
503 static void resource_allocation_enable_set(struct spu *spu, u64 enable)
504 {
505         out_be64(&spu_get_pdata(spu)->priv1->resource_allocation_enable_RW,
506                 enable);
507 }
508
509 static u64 resource_allocation_enable_get(struct spu *spu)
510 {
511         return in_be64(
512                 &spu_get_pdata(spu)->priv1->resource_allocation_enable_RW);
513 }
514
515 const struct spu_priv1_ops spu_priv1_mmio_ops =
516 {
517         .int_mask_and = int_mask_and,
518         .int_mask_or = int_mask_or,
519         .int_mask_set = int_mask_set,
520         .int_mask_get = int_mask_get,
521         .int_stat_clear = int_stat_clear,
522         .int_stat_get = int_stat_get,
523         .cpu_affinity_set = cpu_affinity_set,
524         .mfc_dar_get = mfc_dar_get,
525         .mfc_dsisr_get = mfc_dsisr_get,
526         .mfc_dsisr_set = mfc_dsisr_set,
527         .mfc_sdr_setup = mfc_sdr_setup,
528         .mfc_sr1_set = mfc_sr1_set,
529         .mfc_sr1_get = mfc_sr1_get,
530         .mfc_tclass_id_set = mfc_tclass_id_set,
531         .mfc_tclass_id_get = mfc_tclass_id_get,
532         .tlb_invalidate = tlb_invalidate,
533         .resource_allocation_groupID_set = resource_allocation_groupID_set,
534         .resource_allocation_groupID_get = resource_allocation_groupID_get,
535         .resource_allocation_enable_set = resource_allocation_enable_set,
536         .resource_allocation_enable_get = resource_allocation_enable_get,
537 };