Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild
[deliverable/linux.git] / arch / powerpc / platforms / cell / spu_base.c
CommitLineData
67207b96
AB
1/*
2 * Low-level SPU handling
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 *
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
3b3d22cb 23#undef DEBUG
67207b96
AB
24
25#include <linux/interrupt.h>
26#include <linux/list.h>
27#include <linux/module.h>
28#include <linux/poll.h>
29#include <linux/ptrace.h>
30#include <linux/slab.h>
31#include <linux/wait.h>
32
33#include <asm/io.h>
34#include <asm/prom.h>
14cc3e2b 35#include <linux/mutex.h>
67207b96 36#include <asm/spu.h>
540270d8 37#include <asm/spu_priv1.h>
67207b96
AB
38#include <asm/mmu_context.h>
39
40#include "interrupt.h"
41
540270d8
GL
42const struct spu_priv1_ops *spu_priv1_ops;
43
44EXPORT_SYMBOL_GPL(spu_priv1_ops);
45
67207b96
AB
46static int __spu_trap_invalid_dma(struct spu *spu)
47{
48 pr_debug("%s\n", __FUNCTION__);
49 force_sig(SIGBUS, /* info, */ current);
50 return 0;
51}
52
53static int __spu_trap_dma_align(struct spu *spu)
54{
55 pr_debug("%s\n", __FUNCTION__);
56 force_sig(SIGBUS, /* info, */ current);
57 return 0;
58}
59
60static int __spu_trap_error(struct spu *spu)
61{
62 pr_debug("%s\n", __FUNCTION__);
63 force_sig(SIGILL, /* info, */ current);
64 return 0;
65}
66
67static void spu_restart_dma(struct spu *spu)
68{
69 struct spu_priv2 __iomem *priv2 = spu->priv2;
5473af04 70
8837d921 71 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
5473af04 72 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
67207b96
AB
73}
74
75static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
76{
8b3d6663
AB
77 struct spu_priv2 __iomem *priv2 = spu->priv2;
78 struct mm_struct *mm = spu->mm;
724bd80e 79 u64 esid, vsid, llp;
67207b96
AB
80
81 pr_debug("%s\n", __FUNCTION__);
82
8837d921 83 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
8b3d6663
AB
84 /* SLBs are pre-loaded for context switch, so
85 * we should never get here!
86 */
5473af04
MN
87 printk("%s: invalid access during switch!\n", __func__);
88 return 1;
89 }
8b3d6663
AB
90 if (!mm || (REGION_ID(ea) != USER_REGION_ID)) {
91 /* Future: support kernel segments so that drivers
92 * can use SPUs.
93 */
67207b96
AB
94 pr_debug("invalid region access at %016lx\n", ea);
95 return 1;
96 }
97
8b3d6663 98 esid = (ea & ESID_MASK) | SLB_ESID_V;
724bd80e 99#ifdef CONFIG_HUGETLB_PAGE
8b3d6663 100 if (in_hugepage_area(mm->context, ea))
724bd80e 101 llp = mmu_psize_defs[mmu_huge_psize].sllp;
102 else
103#endif
104 llp = mmu_psize_defs[mmu_virtual_psize].sllp;
105 vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
106 SLB_VSID_USER | llp;
67207b96 107
8b3d6663
AB
108 out_be64(&priv2->slb_index_W, spu->slb_replace);
109 out_be64(&priv2->slb_vsid_RW, vsid);
110 out_be64(&priv2->slb_esid_RW, esid);
111
112 spu->slb_replace++;
67207b96
AB
113 if (spu->slb_replace >= 8)
114 spu->slb_replace = 0;
115
67207b96
AB
116 spu_restart_dma(spu);
117
67207b96
AB
118 return 0;
119}
120
5473af04 121extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
8b3d6663 122static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
67207b96 123{
a33a7d73 124 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
67207b96 125
5473af04
MN
126 /* Handle kernel space hash faults immediately.
127 User hash faults need to be deferred to process context. */
128 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
129 && REGION_ID(ea) != USER_REGION_ID
130 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
131 spu_restart_dma(spu);
132 return 0;
133 }
134
8837d921 135 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
5473af04
MN
136 printk("%s: invalid access during switch!\n", __func__);
137 return 1;
138 }
67207b96 139
8b3d6663
AB
140 spu->dar = ea;
141 spu->dsisr = dsisr;
142 mb();
ba723fe2 143 spu->stop_callback(spu);
67207b96
AB
144 return 0;
145}
146
147static irqreturn_t
148spu_irq_class_0(int irq, void *data, struct pt_regs *regs)
149{
150 struct spu *spu;
151
152 spu = data;
153 spu->class_0_pending = 1;
ba723fe2 154 spu->stop_callback(spu);
67207b96
AB
155
156 return IRQ_HANDLED;
157}
158
5110459f 159int
67207b96
AB
160spu_irq_class_0_bottom(struct spu *spu)
161{
3a843d7c 162 unsigned long stat, mask;
67207b96
AB
163
164 spu->class_0_pending = 0;
165
f0831acc
AB
166 mask = spu_int_mask_get(spu, 0);
167 stat = spu_int_stat_get(spu, 0);
67207b96 168
3a843d7c
AB
169 stat &= mask;
170
2cd90bc8 171 if (stat & 1) /* invalid DMA alignment */
67207b96
AB
172 __spu_trap_dma_align(spu);
173
2cd90bc8
AB
174 if (stat & 2) /* invalid MFC DMA */
175 __spu_trap_invalid_dma(spu);
176
67207b96
AB
177 if (stat & 4) /* error on SPU */
178 __spu_trap_error(spu);
179
f0831acc 180 spu_int_stat_clear(spu, 0, stat);
5110459f
AB
181
182 return (stat & 0x7) ? -EIO : 0;
67207b96 183}
5110459f 184EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
67207b96
AB
185
186static irqreturn_t
187spu_irq_class_1(int irq, void *data, struct pt_regs *regs)
188{
189 struct spu *spu;
8b3d6663 190 unsigned long stat, mask, dar, dsisr;
67207b96
AB
191
192 spu = data;
8b3d6663
AB
193
194 /* atomically read & clear class1 status. */
195 spin_lock(&spu->register_lock);
f0831acc
AB
196 mask = spu_int_mask_get(spu, 1);
197 stat = spu_int_stat_get(spu, 1) & mask;
198 dar = spu_mfc_dar_get(spu);
199 dsisr = spu_mfc_dsisr_get(spu);
38307341 200 if (stat & 2) /* mapping fault */
f0831acc
AB
201 spu_mfc_dsisr_set(spu, 0ul);
202 spu_int_stat_clear(spu, 1, stat);
8b3d6663 203 spin_unlock(&spu->register_lock);
a33a7d73
AB
204 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
205 dar, dsisr);
67207b96
AB
206
207 if (stat & 1) /* segment fault */
208 __spu_trap_data_seg(spu, dar);
209
210 if (stat & 2) { /* mapping fault */
8b3d6663 211 __spu_trap_data_map(spu, dar, dsisr);
67207b96
AB
212 }
213
214 if (stat & 4) /* ls compare & suspend on get */
215 ;
216
217 if (stat & 8) /* ls compare & suspend on put */
218 ;
219
67207b96
AB
220 return stat ? IRQ_HANDLED : IRQ_NONE;
221}
5110459f 222EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom);
67207b96
AB
223
224static irqreturn_t
225spu_irq_class_2(int irq, void *data, struct pt_regs *regs)
226{
227 struct spu *spu;
228 unsigned long stat;
3a843d7c 229 unsigned long mask;
67207b96
AB
230
231 spu = data;
ba723fe2 232 spin_lock(&spu->register_lock);
f0831acc
AB
233 stat = spu_int_stat_get(spu, 2);
234 mask = spu_int_mask_get(spu, 2);
ba723fe2
MN
235 /* ignore interrupts we're not waiting for */
236 stat &= mask;
237 /*
238 * mailbox interrupts (0x1 and 0x10) are level triggered.
239 * mask them now before acknowledging.
240 */
241 if (stat & 0x11)
242 spu_int_mask_and(spu, 2, ~(stat & 0x11));
243 /* acknowledge all interrupts before the callbacks */
244 spu_int_stat_clear(spu, 2, stat);
245 spin_unlock(&spu->register_lock);
67207b96 246
3a843d7c 247 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
67207b96 248
67207b96 249 if (stat & 1) /* PPC core mailbox */
ba723fe2 250 spu->ibox_callback(spu);
67207b96
AB
251
252 if (stat & 2) /* SPU stop-and-signal */
ba723fe2 253 spu->stop_callback(spu);
67207b96
AB
254
255 if (stat & 4) /* SPU halted */
ba723fe2 256 spu->stop_callback(spu);
67207b96
AB
257
258 if (stat & 8) /* DMA tag group complete */
ba723fe2 259 spu->mfc_callback(spu);
67207b96
AB
260
261 if (stat & 0x10) /* SPU mailbox threshold */
ba723fe2 262 spu->wbox_callback(spu);
67207b96 263
67207b96
AB
264 return stat ? IRQ_HANDLED : IRQ_NONE;
265}
266
0ebfff14 267static int spu_request_irqs(struct spu *spu)
67207b96 268{
0ebfff14 269 int ret = 0;
67207b96 270
0ebfff14
BH
271 if (spu->irqs[0] != NO_IRQ) {
272 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
273 spu->number);
274 ret = request_irq(spu->irqs[0], spu_irq_class_0,
275 IRQF_DISABLED,
276 spu->irq_c0, spu);
277 if (ret)
278 goto bail0;
279 }
280 if (spu->irqs[1] != NO_IRQ) {
281 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
282 spu->number);
283 ret = request_irq(spu->irqs[1], spu_irq_class_1,
284 IRQF_DISABLED,
285 spu->irq_c1, spu);
286 if (ret)
287 goto bail1;
288 }
289 if (spu->irqs[2] != NO_IRQ) {
290 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
291 spu->number);
292 ret = request_irq(spu->irqs[2], spu_irq_class_2,
293 IRQF_DISABLED,
294 spu->irq_c2, spu);
295 if (ret)
296 goto bail2;
297 }
298 return 0;
67207b96 299
0ebfff14
BH
300bail2:
301 if (spu->irqs[1] != NO_IRQ)
302 free_irq(spu->irqs[1], spu);
303bail1:
304 if (spu->irqs[0] != NO_IRQ)
305 free_irq(spu->irqs[0], spu);
306bail0:
67207b96
AB
307 return ret;
308}
309
0ebfff14 310static void spu_free_irqs(struct spu *spu)
67207b96 311{
0ebfff14
BH
312 if (spu->irqs[0] != NO_IRQ)
313 free_irq(spu->irqs[0], spu);
314 if (spu->irqs[1] != NO_IRQ)
315 free_irq(spu->irqs[1], spu);
316 if (spu->irqs[2] != NO_IRQ)
317 free_irq(spu->irqs[2], spu);
67207b96
AB
318}
319
320static LIST_HEAD(spu_list);
14cc3e2b 321static DEFINE_MUTEX(spu_mutex);
67207b96
AB
322
323static void spu_init_channels(struct spu *spu)
324{
325 static const struct {
326 unsigned channel;
327 unsigned count;
328 } zero_list[] = {
329 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
330 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
331 }, count_list[] = {
332 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
333 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
334 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
335 };
6ff730c3 336 struct spu_priv2 __iomem *priv2;
67207b96
AB
337 int i;
338
339 priv2 = spu->priv2;
340
341 /* initialize all channel data to zero */
342 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
343 int count;
344
345 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
346 for (count = 0; count < zero_list[i].count; count++)
347 out_be64(&priv2->spu_chnldata_RW, 0);
348 }
349
350 /* initialize channel counts to meaningful values */
351 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
352 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
353 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
354 }
355}
356
67207b96
AB
357struct spu *spu_alloc(void)
358{
359 struct spu *spu;
360
14cc3e2b 361 mutex_lock(&spu_mutex);
67207b96
AB
362 if (!list_empty(&spu_list)) {
363 spu = list_entry(spu_list.next, struct spu, list);
364 list_del_init(&spu->list);
365 pr_debug("Got SPU %x %d\n", spu->isrc, spu->number);
366 } else {
367 pr_debug("No SPU left\n");
368 spu = NULL;
369 }
14cc3e2b 370 mutex_unlock(&spu_mutex);
67207b96 371
f0831acc 372 if (spu)
67207b96 373 spu_init_channels(spu);
67207b96
AB
374
375 return spu;
376}
39c73c33 377EXPORT_SYMBOL_GPL(spu_alloc);
67207b96
AB
378
379void spu_free(struct spu *spu)
380{
14cc3e2b 381 mutex_lock(&spu_mutex);
67207b96 382 list_add_tail(&spu->list, &spu_list);
14cc3e2b 383 mutex_unlock(&spu_mutex);
67207b96 384}
39c73c33 385EXPORT_SYMBOL_GPL(spu_free);
67207b96 386
67207b96
AB
387static int spu_handle_mm_fault(struct spu *spu)
388{
67207b96
AB
389 struct mm_struct *mm = spu->mm;
390 struct vm_area_struct *vma;
391 u64 ea, dsisr, is_write;
392 int ret;
393
8b3d6663
AB
394 ea = spu->dar;
395 dsisr = spu->dsisr;
67207b96
AB
396#if 0
397 if (!IS_VALID_EA(ea)) {
398 return -EFAULT;
399 }
400#endif /* XXX */
401 if (mm == NULL) {
402 return -EFAULT;
403 }
404 if (mm->pgd == NULL) {
405 return -EFAULT;
406 }
407
408 down_read(&mm->mmap_sem);
409 vma = find_vma(mm, ea);
410 if (!vma)
411 goto bad_area;
412 if (vma->vm_start <= ea)
413 goto good_area;
414 if (!(vma->vm_flags & VM_GROWSDOWN))
415 goto bad_area;
416#if 0
417 if (expand_stack(vma, ea))
418 goto bad_area;
419#endif /* XXX */
420good_area:
421 is_write = dsisr & MFC_DSISR_ACCESS_PUT;
422 if (is_write) {
423 if (!(vma->vm_flags & VM_WRITE))
424 goto bad_area;
425 } else {
426 if (dsisr & MFC_DSISR_ACCESS_DENIED)
427 goto bad_area;
428 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
429 goto bad_area;
430 }
431 ret = 0;
432 switch (handle_mm_fault(mm, vma, ea, is_write)) {
433 case VM_FAULT_MINOR:
434 current->min_flt++;
435 break;
436 case VM_FAULT_MAJOR:
437 current->maj_flt++;
438 break;
439 case VM_FAULT_SIGBUS:
440 ret = -EFAULT;
441 goto bad_area;
442 case VM_FAULT_OOM:
443 ret = -ENOMEM;
444 goto bad_area;
445 default:
446 BUG();
447 }
448 up_read(&mm->mmap_sem);
449 return ret;
450
451bad_area:
452 up_read(&mm->mmap_sem);
453 return -EFAULT;
454}
455
5110459f 456int spu_irq_class_1_bottom(struct spu *spu)
67207b96 457{
67207b96
AB
458 u64 ea, dsisr, access, error = 0UL;
459 int ret = 0;
460
8b3d6663
AB
461 ea = spu->dar;
462 dsisr = spu->dsisr;
79c227a9 463 if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) {
f807221d
AB
464 u64 flags;
465
8b3d6663
AB
466 access = (_PAGE_PRESENT | _PAGE_USER);
467 access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
f807221d 468 local_irq_save(flags);
67207b96
AB
469 if (hash_page(ea, access, 0x300) != 0)
470 error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
f807221d 471 local_irq_restore(flags);
67207b96 472 }
79c227a9 473 if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) {
67207b96
AB
474 if ((ret = spu_handle_mm_fault(spu)) != 0)
475 error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
476 else
477 error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
478 }
8b3d6663
AB
479 spu->dar = 0UL;
480 spu->dsisr = 0UL;
481 if (!error) {
67207b96 482 spu_restart_dma(spu);
8b3d6663
AB
483 } else {
484 __spu_trap_invalid_dma(spu);
485 }
67207b96
AB
486 return ret;
487}
488
bed120c6
JS
489static int __init find_spu_node_id(struct device_node *spe)
490{
491 unsigned int *id;
492 struct device_node *cpu;
493 cpu = spe->parent->parent;
494 id = (unsigned int *)get_property(cpu, "node-id", NULL);
495 return id ? *id : 0;
496}
497
8261aa60
JK
498static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
499 const char *prop)
bed120c6
JS
500{
501 static DEFINE_MUTEX(add_spumem_mutex);
502
503 struct address_prop {
504 unsigned long address;
505 unsigned int len;
506 } __attribute__((packed)) *p;
507 int proplen;
508
509 unsigned long start_pfn, nr_pages;
bed120c6
JS
510 struct pglist_data *pgdata;
511 struct zone *zone;
512 int ret;
513
514 p = (void*)get_property(spe, prop, &proplen);
515 WARN_ON(proplen != sizeof (*p));
516
517 start_pfn = p->address >> PAGE_SHIFT;
518 nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
519
8261aa60 520 pgdata = NODE_DATA(spu->nid);
bed120c6
JS
521 zone = pgdata->node_zones;
522
523 /* XXX rethink locking here */
524 mutex_lock(&add_spumem_mutex);
525 ret = __add_pages(zone, start_pfn, nr_pages);
526 mutex_unlock(&add_spumem_mutex);
527
528 return ret;
529}
530
8261aa60
JK
531static void __iomem * __init map_spe_prop(struct spu *spu,
532 struct device_node *n, const char *name)
67207b96
AB
533{
534 struct address_prop {
535 unsigned long address;
536 unsigned int len;
537 } __attribute__((packed)) *prop;
538
539 void *p;
540 int proplen;
bed120c6
JS
541 void* ret = NULL;
542 int err = 0;
67207b96
AB
543
544 p = get_property(n, name, &proplen);
545 if (proplen != sizeof (struct address_prop))
546 return NULL;
547
548 prop = p;
549
8261aa60 550 err = cell_spuprop_present(spu, n, name);
bed120c6
JS
551 if (err && (err != -EEXIST))
552 goto out;
553
554 ret = ioremap(prop->address, prop->len);
555
556 out:
557 return ret;
67207b96
AB
558}
559
560static void spu_unmap(struct spu *spu)
561{
562 iounmap(spu->priv2);
563 iounmap(spu->priv1);
564 iounmap(spu->problem);
565 iounmap((u8 __iomem *)spu->local_store);
566}
567
0ebfff14
BH
568/* This function shall be abstracted for HV platforms */
569static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
570{
571 struct irq_host *host;
572 unsigned int isrc;
573 u32 *tmp;
574
575 host = iic_get_irq_host(spu->node);
576 if (host == NULL)
577 return -ENODEV;
578
579 /* Get the interrupt source from the device-tree */
580 tmp = (u32 *)get_property(np, "isrc", NULL);
581 if (!tmp)
582 return -ENODEV;
583 spu->isrc = isrc = tmp[0];
584
585 /* Now map interrupts of all 3 classes */
586 spu->irqs[0] = irq_create_mapping(host, 0x00 | isrc, 0);
587 spu->irqs[1] = irq_create_mapping(host, 0x10 | isrc, 0);
588 spu->irqs[2] = irq_create_mapping(host, 0x20 | isrc, 0);
589
590 /* Right now, we only fail if class 2 failed */
591 return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
592}
593
8261aa60 594static int __init spu_map_device(struct spu *spu, struct device_node *node)
67207b96
AB
595{
596 char *prop;
597 int ret;
598
599 ret = -ENODEV;
8261aa60 600 spu->name = get_property(node, "name", NULL);
67207b96
AB
601 if (!spu->name)
602 goto out;
603
8261aa60 604 prop = get_property(node, "local-store", NULL);
67207b96
AB
605 if (!prop)
606 goto out;
607 spu->local_store_phys = *(unsigned long *)prop;
608
609 /* we use local store as ram, not io memory */
8261aa60
JK
610 spu->local_store = (void __force *)
611 map_spe_prop(spu, node, "local-store");
67207b96
AB
612 if (!spu->local_store)
613 goto out;
614
8261aa60 615 prop = get_property(node, "problem", NULL);
6df10a82
MN
616 if (!prop)
617 goto out_unmap;
618 spu->problem_phys = *(unsigned long *)prop;
619
8261aa60 620 spu->problem= map_spe_prop(spu, node, "problem");
67207b96
AB
621 if (!spu->problem)
622 goto out_unmap;
623
8261aa60 624 spu->priv1= map_spe_prop(spu, node, "priv1");
f0831acc 625 /* priv1 is not available on a hypervisor */
67207b96 626
8261aa60 627 spu->priv2= map_spe_prop(spu, node, "priv2");
67207b96
AB
628 if (!spu->priv2)
629 goto out_unmap;
630 ret = 0;
631 goto out;
632
633out_unmap:
634 spu_unmap(spu);
635out:
636 return ret;
637}
638
1d64093f
JK
639struct sysdev_class spu_sysdev_class = {
640 set_kset_name("spu")
641};
642
643static ssize_t spu_show_isrc(struct sys_device *sysdev, char *buf)
644{
645 struct spu *spu = container_of(sysdev, struct spu, sysdev);
646 return sprintf(buf, "%d\n", spu->isrc);
647
648}
649static SYSDEV_ATTR(isrc, 0400, spu_show_isrc, NULL);
650
651extern int attach_sysdev_to_node(struct sys_device *dev, int nid);
652
653static int spu_create_sysdev(struct spu *spu)
654{
655 int ret;
656
657 spu->sysdev.id = spu->number;
658 spu->sysdev.cls = &spu_sysdev_class;
659 ret = sysdev_register(&spu->sysdev);
660 if (ret) {
661 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
662 spu->number);
663 return ret;
664 }
665
0ebfff14
BH
666 if (spu->isrc != 0)
667 sysdev_create_file(&spu->sysdev, &attr_isrc);
1d64093f
JK
668 sysfs_add_device_to_node(&spu->sysdev, spu->nid);
669
670 return 0;
671}
672
673static void spu_destroy_sysdev(struct spu *spu)
674{
675 sysdev_remove_file(&spu->sysdev, &attr_isrc);
676 sysfs_remove_device_from_node(&spu->sysdev, spu->nid);
677 sysdev_unregister(&spu->sysdev);
678}
679
67207b96
AB
680static int __init create_spu(struct device_node *spe)
681{
682 struct spu *spu;
683 int ret;
684 static int number;
685
686 ret = -ENOMEM;
ecec2177 687 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
67207b96
AB
688 if (!spu)
689 goto out;
690
691 ret = spu_map_device(spu, spe);
692 if (ret)
693 goto out_free;
694
695 spu->node = find_spu_node_id(spe);
8261aa60
JK
696 spu->nid = of_node_to_nid(spe);
697 if (spu->nid == -1)
698 spu->nid = 0;
0ebfff14
BH
699 ret = spu_map_interrupts(spu, spe);
700 if (ret)
701 goto out_unmap;
67207b96 702 spin_lock_init(&spu->register_lock);
f0831acc
AB
703 spu_mfc_sdr_set(spu, mfspr(SPRN_SDR1));
704 spu_mfc_sr1_set(spu, 0x33);
14cc3e2b 705 mutex_lock(&spu_mutex);
ecec2177 706
67207b96
AB
707 spu->number = number++;
708 ret = spu_request_irqs(spu);
709 if (ret)
710 goto out_unmap;
711
1d64093f
JK
712 ret = spu_create_sysdev(spu);
713 if (ret)
714 goto out_free_irqs;
715
67207b96 716 list_add(&spu->list, &spu_list);
14cc3e2b 717 mutex_unlock(&spu_mutex);
67207b96
AB
718
719 pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n",
720 spu->name, spu->isrc, spu->local_store,
721 spu->problem, spu->priv1, spu->priv2, spu->number);
722 goto out;
723
1d64093f
JK
724out_free_irqs:
725 spu_free_irqs(spu);
726
67207b96 727out_unmap:
14cc3e2b 728 mutex_unlock(&spu_mutex);
67207b96
AB
729 spu_unmap(spu);
730out_free:
731 kfree(spu);
732out:
733 return ret;
734}
735
736static void destroy_spu(struct spu *spu)
737{
738 list_del_init(&spu->list);
739
1d64093f 740 spu_destroy_sysdev(spu);
67207b96
AB
741 spu_free_irqs(spu);
742 spu_unmap(spu);
743 kfree(spu);
744}
745
746static void cleanup_spu_base(void)
747{
748 struct spu *spu, *tmp;
14cc3e2b 749 mutex_lock(&spu_mutex);
67207b96
AB
750 list_for_each_entry_safe(spu, tmp, &spu_list, list)
751 destroy_spu(spu);
14cc3e2b 752 mutex_unlock(&spu_mutex);
1d64093f 753 sysdev_class_unregister(&spu_sysdev_class);
67207b96
AB
754}
755module_exit(cleanup_spu_base);
756
757static int __init init_spu_base(void)
758{
759 struct device_node *node;
760 int ret;
761
1d64093f
JK
762 /* create sysdev class for spus */
763 ret = sysdev_class_register(&spu_sysdev_class);
764 if (ret)
765 return ret;
766
67207b96
AB
767 ret = -ENODEV;
768 for (node = of_find_node_by_type(NULL, "spe");
769 node; node = of_find_node_by_type(node, "spe")) {
770 ret = create_spu(node);
771 if (ret) {
772 printk(KERN_WARNING "%s: Error initializing %s\n",
773 __FUNCTION__, node->name);
774 cleanup_spu_base();
775 break;
776 }
777 }
778 /* in some old firmware versions, the spe is called 'spc', so we
779 look for that as well */
780 for (node = of_find_node_by_type(NULL, "spc");
781 node; node = of_find_node_by_type(node, "spc")) {
782 ret = create_spu(node);
783 if (ret) {
784 printk(KERN_WARNING "%s: Error initializing %s\n",
785 __FUNCTION__, node->name);
786 cleanup_spu_base();
787 break;
788 }
789 }
790 return ret;
791}
792module_init(init_spu_base);
793
794MODULE_LICENSE("GPL");
795MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
This page took 0.135918 seconds and 5 git commands to generate.