blob: 2e8aa9433b3b8659b94ec4f0ca3b5fa1b456588d [file] [log] [blame]
Arnd Bergmann67207b92005-11-15 15:53:48 -05001/*
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
Arnd Bergmann3b3d22c2005-12-05 22:52:24 -050023#undef DEBUG
Arnd Bergmann67207b92005-11-15 15:53:48 -050024
25#include <linux/interrupt.h>
26#include <linux/list.h>
27#include <linux/module.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050028#include <linux/ptrace.h>
29#include <linux/slab.h>
30#include <linux/wait.h>
Geoff Levande28b0032006-11-23 00:46:49 +010031#include <linux/mm.h>
32#include <linux/io.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080033#include <linux/mutex.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050034#include <asm/spu.h>
Geoff Levand540270d2006-06-19 20:33:29 +020035#include <asm/spu_priv1.h>
Michael Ellermanff8a8f22006-10-24 18:31:27 +020036#include <asm/xmon.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050037
Geoff Levande28b0032006-11-23 00:46:49 +010038const struct spu_management_ops *spu_management_ops;
Geoff Levand540270d2006-06-19 20:33:29 +020039const struct spu_priv1_ops *spu_priv1_ops;
40
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010041static struct list_head spu_list[MAX_NUMNODES];
42static LIST_HEAD(spu_full_list);
43static DEFINE_MUTEX(spu_mutex);
44static spinlock_t spu_list_lock = SPIN_LOCK_UNLOCKED;
45
Geoff Levand540270d2006-06-19 20:33:29 +020046EXPORT_SYMBOL_GPL(spu_priv1_ops);
47
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010048void spu_invalidate_slbs(struct spu *spu)
49{
50 struct spu_priv2 __iomem *priv2 = spu->priv2;
51
52 if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
53 out_be64(&priv2->slb_invalidate_all_W, 0UL);
54}
55EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
56
57/* This is called by the MM core when a segment size is changed, to
58 * request a flush of all the SPEs using a given mm
59 */
60void spu_flush_all_slbs(struct mm_struct *mm)
61{
62 struct spu *spu;
63 unsigned long flags;
64
65 spin_lock_irqsave(&spu_list_lock, flags);
66 list_for_each_entry(spu, &spu_full_list, full_list) {
67 if (spu->mm == mm)
68 spu_invalidate_slbs(spu);
69 }
70 spin_unlock_irqrestore(&spu_list_lock, flags);
71}
72
73/* The hack below stinks... try to do something better one of
74 * these days... Does it even work properly with NR_CPUS == 1 ?
75 */
76static inline void mm_needs_global_tlbie(struct mm_struct *mm)
77{
78 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
79
80 /* Global TLBIE broadcast required with SPEs. */
81 __cpus_setall(&mm->cpu_vm_mask, nr);
82}
83
84void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
85{
86 unsigned long flags;
87
88 spin_lock_irqsave(&spu_list_lock, flags);
89 spu->mm = mm;
90 spin_unlock_irqrestore(&spu_list_lock, flags);
91 if (mm)
92 mm_needs_global_tlbie(mm);
93}
94EXPORT_SYMBOL_GPL(spu_associate_mm);
95
Arnd Bergmann67207b92005-11-15 15:53:48 -050096static int __spu_trap_invalid_dma(struct spu *spu)
97{
98 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +020099 spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500100 return 0;
101}
102
103static int __spu_trap_dma_align(struct spu *spu)
104{
105 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200106 spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500107 return 0;
108}
109
110static int __spu_trap_error(struct spu *spu)
111{
112 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200113 spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500114 return 0;
115}
116
117static void spu_restart_dma(struct spu *spu)
118{
119 struct spu_priv2 __iomem *priv2 = spu->priv2;
Mark Nutter5473af02005-11-15 15:53:49 -0500120
Arnd Bergmann8837d922006-01-04 20:31:28 +0100121 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
Mark Nutter5473af02005-11-15 15:53:49 -0500122 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500123}
124
125static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
126{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500127 struct spu_priv2 __iomem *priv2 = spu->priv2;
128 struct mm_struct *mm = spu->mm;
arnd@arndb.de724bd802006-06-19 20:33:23 +0200129 u64 esid, vsid, llp;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100130 int psize;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500131
132 pr_debug("%s\n", __FUNCTION__);
133
Arnd Bergmann8837d922006-01-04 20:31:28 +0100134 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500135 /* SLBs are pre-loaded for context switch, so
136 * we should never get here!
137 */
Mark Nutter5473af02005-11-15 15:53:49 -0500138 printk("%s: invalid access during switch!\n", __func__);
139 return 1;
140 }
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200141 esid = (ea & ESID_MASK) | SLB_ESID_V;
142
143 switch(REGION_ID(ea)) {
144 case USER_REGION_ID:
145#ifdef CONFIG_HUGETLB_PAGE
146 if (in_hugepage_area(mm->context, ea))
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100147 psize = mmu_huge_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200148 else
149#endif
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100150 psize = mm->context.user_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200151 vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100152 SLB_VSID_USER;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200153 break;
154 case VMALLOC_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100155 if (ea < VMALLOC_END)
156 psize = mmu_vmalloc_psize;
157 else
158 psize = mmu_io_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200159 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100160 SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200161 break;
162 case KERNEL_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100163 psize = mmu_linear_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200164 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100165 SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200166 break;
167 default:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500168 /* Future: support kernel segments so that drivers
169 * can use SPUs.
170 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500171 pr_debug("invalid region access at %016lx\n", ea);
172 return 1;
173 }
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100174 llp = mmu_psize_defs[psize].sllp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500175
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500176 out_be64(&priv2->slb_index_W, spu->slb_replace);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100177 out_be64(&priv2->slb_vsid_RW, vsid | llp);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500178 out_be64(&priv2->slb_esid_RW, esid);
179
180 spu->slb_replace++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500181 if (spu->slb_replace >= 8)
182 spu->slb_replace = 0;
183
Arnd Bergmann67207b92005-11-15 15:53:48 -0500184 spu_restart_dma(spu);
185
Arnd Bergmann67207b92005-11-15 15:53:48 -0500186 return 0;
187}
188
Mark Nutter5473af02005-11-15 15:53:49 -0500189extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500190static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500191{
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100192 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500193
Mark Nutter5473af02005-11-15 15:53:49 -0500194 /* Handle kernel space hash faults immediately.
195 User hash faults need to be deferred to process context. */
196 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
197 && REGION_ID(ea) != USER_REGION_ID
198 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
199 spu_restart_dma(spu);
200 return 0;
201 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500202
Arnd Bergmann8837d922006-01-04 20:31:28 +0100203 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Mark Nutter5473af02005-11-15 15:53:49 -0500204 printk("%s: invalid access during switch!\n", __func__);
205 return 1;
206 }
207
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500208 spu->dar = ea;
209 spu->dsisr = dsisr;
210 mb();
Masato Noguchiba723fe22006-06-19 20:33:33 +0200211 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500212 return 0;
213}
214
215static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200216spu_irq_class_0(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500217{
218 struct spu *spu;
219
220 spu = data;
221 spu->class_0_pending = 1;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200222 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500223
224 return IRQ_HANDLED;
225}
226
Arnd Bergmann51104592005-12-05 22:52:25 -0500227int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500228spu_irq_class_0_bottom(struct spu *spu)
229{
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500230 unsigned long stat, mask;
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900231 unsigned long flags;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500232
233 spu->class_0_pending = 0;
234
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900235 spin_lock_irqsave(&spu->register_lock, flags);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100236 mask = spu_int_mask_get(spu, 0);
237 stat = spu_int_stat_get(spu, 0);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500238
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500239 stat &= mask;
240
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200241 if (stat & 1) /* invalid DMA alignment */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500242 __spu_trap_dma_align(spu);
243
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200244 if (stat & 2) /* invalid MFC DMA */
245 __spu_trap_invalid_dma(spu);
246
Arnd Bergmann67207b92005-11-15 15:53:48 -0500247 if (stat & 4) /* error on SPU */
248 __spu_trap_error(spu);
249
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100250 spu_int_stat_clear(spu, 0, stat);
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900251 spin_unlock_irqrestore(&spu->register_lock, flags);
Arnd Bergmann51104592005-12-05 22:52:25 -0500252
253 return (stat & 0x7) ? -EIO : 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500254}
Arnd Bergmann51104592005-12-05 22:52:25 -0500255EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500256
257static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200258spu_irq_class_1(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500259{
260 struct spu *spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500261 unsigned long stat, mask, dar, dsisr;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500262
263 spu = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500264
265 /* atomically read & clear class1 status. */
266 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100267 mask = spu_int_mask_get(spu, 1);
268 stat = spu_int_stat_get(spu, 1) & mask;
269 dar = spu_mfc_dar_get(spu);
270 dsisr = spu_mfc_dsisr_get(spu);
Arnd Bergmann38307342005-12-09 19:04:18 +0100271 if (stat & 2) /* mapping fault */
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100272 spu_mfc_dsisr_set(spu, 0ul);
273 spu_int_stat_clear(spu, 1, stat);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500274 spin_unlock(&spu->register_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100275 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
276 dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500277
278 if (stat & 1) /* segment fault */
279 __spu_trap_data_seg(spu, dar);
280
281 if (stat & 2) { /* mapping fault */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500282 __spu_trap_data_map(spu, dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500283 }
284
285 if (stat & 4) /* ls compare & suspend on get */
286 ;
287
288 if (stat & 8) /* ls compare & suspend on put */
289 ;
290
Arnd Bergmann67207b92005-11-15 15:53:48 -0500291 return stat ? IRQ_HANDLED : IRQ_NONE;
292}
293
294static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200295spu_irq_class_2(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500296{
297 struct spu *spu;
298 unsigned long stat;
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500299 unsigned long mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500300
301 spu = data;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200302 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100303 stat = spu_int_stat_get(spu, 2);
304 mask = spu_int_mask_get(spu, 2);
Masato Noguchiba723fe22006-06-19 20:33:33 +0200305 /* ignore interrupts we're not waiting for */
306 stat &= mask;
307 /*
308 * mailbox interrupts (0x1 and 0x10) are level triggered.
309 * mask them now before acknowledging.
310 */
311 if (stat & 0x11)
312 spu_int_mask_and(spu, 2, ~(stat & 0x11));
313 /* acknowledge all interrupts before the callbacks */
314 spu_int_stat_clear(spu, 2, stat);
315 spin_unlock(&spu->register_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500316
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500317 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500318
Arnd Bergmann67207b92005-11-15 15:53:48 -0500319 if (stat & 1) /* PPC core mailbox */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200320 spu->ibox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500321
322 if (stat & 2) /* SPU stop-and-signal */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200323 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500324
325 if (stat & 4) /* SPU halted */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200326 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500327
328 if (stat & 8) /* DMA tag group complete */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200329 spu->mfc_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500330
331 if (stat & 0x10) /* SPU mailbox threshold */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200332 spu->wbox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500333
Arnd Bergmann67207b92005-11-15 15:53:48 -0500334 return stat ? IRQ_HANDLED : IRQ_NONE;
335}
336
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000337static int spu_request_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500338{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000339 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500340
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000341 if (spu->irqs[0] != NO_IRQ) {
342 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
343 spu->number);
344 ret = request_irq(spu->irqs[0], spu_irq_class_0,
345 IRQF_DISABLED,
346 spu->irq_c0, spu);
347 if (ret)
348 goto bail0;
349 }
350 if (spu->irqs[1] != NO_IRQ) {
351 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
352 spu->number);
353 ret = request_irq(spu->irqs[1], spu_irq_class_1,
354 IRQF_DISABLED,
355 spu->irq_c1, spu);
356 if (ret)
357 goto bail1;
358 }
359 if (spu->irqs[2] != NO_IRQ) {
360 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
361 spu->number);
362 ret = request_irq(spu->irqs[2], spu_irq_class_2,
363 IRQF_DISABLED,
364 spu->irq_c2, spu);
365 if (ret)
366 goto bail2;
367 }
368 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500369
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000370bail2:
371 if (spu->irqs[1] != NO_IRQ)
372 free_irq(spu->irqs[1], spu);
373bail1:
374 if (spu->irqs[0] != NO_IRQ)
375 free_irq(spu->irqs[0], spu);
376bail0:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500377 return ret;
378}
379
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000380static void spu_free_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500381{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000382 if (spu->irqs[0] != NO_IRQ)
383 free_irq(spu->irqs[0], spu);
384 if (spu->irqs[1] != NO_IRQ)
385 free_irq(spu->irqs[1], spu);
386 if (spu->irqs[2] != NO_IRQ)
387 free_irq(spu->irqs[2], spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500388}
389
Arnd Bergmann67207b92005-11-15 15:53:48 -0500390static void spu_init_channels(struct spu *spu)
391{
392 static const struct {
393 unsigned channel;
394 unsigned count;
395 } zero_list[] = {
396 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
397 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
398 }, count_list[] = {
399 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
400 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
401 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
402 };
Arnd Bergmann6ff730c2006-01-04 20:31:31 +0100403 struct spu_priv2 __iomem *priv2;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500404 int i;
405
406 priv2 = spu->priv2;
407
408 /* initialize all channel data to zero */
409 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
410 int count;
411
412 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
413 for (count = 0; count < zero_list[i].count; count++)
414 out_be64(&priv2->spu_chnldata_RW, 0);
415 }
416
417 /* initialize channel counts to meaningful values */
418 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
419 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
420 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
421 }
422}
423
Mark Nuttera68cf982006-10-04 17:26:12 +0200424struct spu *spu_alloc_node(int node)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500425{
Mark Nuttera68cf982006-10-04 17:26:12 +0200426 struct spu *spu = NULL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500427
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800428 mutex_lock(&spu_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200429 if (!list_empty(&spu_list[node])) {
430 spu = list_entry(spu_list[node].next, struct spu, list);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500431 list_del_init(&spu->list);
Geoff Levandcc21a662006-10-24 18:31:15 +0200432 pr_debug("Got SPU %d %d\n", spu->number, spu->node);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500433 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800434 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500435
Christoph Hellwig62c05d52007-04-23 21:08:14 +0200436 if (spu)
437 spu_init_channels(spu);
Mark Nuttera68cf982006-10-04 17:26:12 +0200438 return spu;
439}
440EXPORT_SYMBOL_GPL(spu_alloc_node);
441
442struct spu *spu_alloc(void)
443{
444 struct spu *spu = NULL;
445 int node;
446
447 for (node = 0; node < MAX_NUMNODES; node++) {
448 spu = spu_alloc_node(node);
449 if (spu)
450 break;
451 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500452
453 return spu;
454}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500455
456void spu_free(struct spu *spu)
457{
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800458 mutex_lock(&spu_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200459 list_add_tail(&spu->list, &spu_list[spu->node]);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800460 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500461}
Arnd Bergmann39c73c32005-12-05 22:52:21 -0500462EXPORT_SYMBOL_GPL(spu_free);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500463
Jeremy Kerr1d640932006-06-19 20:33:19 +0200464struct sysdev_class spu_sysdev_class = {
465 set_kset_name("spu")
466};
467
Christian Kraffte570beb2006-10-24 18:31:23 +0200468int spu_add_sysdev_attr(struct sysdev_attribute *attr)
469{
470 struct spu *spu;
471 mutex_lock(&spu_mutex);
472
473 list_for_each_entry(spu, &spu_full_list, full_list)
474 sysdev_create_file(&spu->sysdev, attr);
475
476 mutex_unlock(&spu_mutex);
477 return 0;
478}
479EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
480
481int spu_add_sysdev_attr_group(struct attribute_group *attrs)
482{
483 struct spu *spu;
484 mutex_lock(&spu_mutex);
485
486 list_for_each_entry(spu, &spu_full_list, full_list)
487 sysfs_create_group(&spu->sysdev.kobj, attrs);
488
489 mutex_unlock(&spu_mutex);
490 return 0;
491}
492EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
493
494
495void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
496{
497 struct spu *spu;
498 mutex_lock(&spu_mutex);
499
500 list_for_each_entry(spu, &spu_full_list, full_list)
501 sysdev_remove_file(&spu->sysdev, attr);
502
503 mutex_unlock(&spu_mutex);
504}
505EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
506
507void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
508{
509 struct spu *spu;
510 mutex_lock(&spu_mutex);
511
512 list_for_each_entry(spu, &spu_full_list, full_list)
513 sysfs_remove_group(&spu->sysdev.kobj, attrs);
514
515 mutex_unlock(&spu_mutex);
516}
517EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
518
Jeremy Kerr1d640932006-06-19 20:33:19 +0200519static int spu_create_sysdev(struct spu *spu)
520{
521 int ret;
522
523 spu->sysdev.id = spu->number;
524 spu->sysdev.cls = &spu_sysdev_class;
525 ret = sysdev_register(&spu->sysdev);
526 if (ret) {
527 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
528 spu->number);
529 return ret;
530 }
531
Geoff Levand00215502006-11-20 18:45:02 +0100532 sysfs_add_device_to_node(&spu->sysdev, spu->node);
Jeremy Kerr1d640932006-06-19 20:33:19 +0200533
534 return 0;
535}
536
Geoff Levande28b0032006-11-23 00:46:49 +0100537static int __init create_spu(void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500538{
539 struct spu *spu;
540 int ret;
541 static int number;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100542 unsigned long flags;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500543
544 ret = -ENOMEM;
Jeremy Kerrecec2172006-06-19 20:33:26 +0200545 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500546 if (!spu)
547 goto out;
548
Geoff Levande28b0032006-11-23 00:46:49 +0100549 spin_lock_init(&spu->register_lock);
550 mutex_lock(&spu_mutex);
551 spu->number = number++;
552 mutex_unlock(&spu_mutex);
Benjamin Herrenschmidte5267b42006-10-10 15:14:12 +1000553
Geoff Levande28b0032006-11-23 00:46:49 +0100554 ret = spu_create_spu(spu, data);
555
Arnd Bergmann67207b92005-11-15 15:53:48 -0500556 if (ret)
557 goto out_free;
558
Masato Noguchi24f43b32006-10-24 18:31:14 +0200559 spu_mfc_sdr_setup(spu);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100560 spu_mfc_sr1_set(spu, 0x33);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500561 ret = spu_request_irqs(spu);
562 if (ret)
Geoff Levande28b0032006-11-23 00:46:49 +0100563 goto out_destroy;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500564
Jeremy Kerr1d640932006-06-19 20:33:19 +0200565 ret = spu_create_sysdev(spu);
566 if (ret)
567 goto out_free_irqs;
568
Geoff Levande28b0032006-11-23 00:46:49 +0100569 mutex_lock(&spu_mutex);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100570 spin_lock_irqsave(&spu_list_lock, flags);
Mark Nuttera68cf982006-10-04 17:26:12 +0200571 list_add(&spu->list, &spu_list[spu->node]);
Christian Kraffte570beb2006-10-24 18:31:23 +0200572 list_add(&spu->full_list, &spu_full_list);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100573 spin_unlock_irqrestore(&spu_list_lock, flags);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800574 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500575
Arnd Bergmann67207b92005-11-15 15:53:48 -0500576 goto out;
577
Jeremy Kerr1d640932006-06-19 20:33:19 +0200578out_free_irqs:
579 spu_free_irqs(spu);
Geoff Levande28b0032006-11-23 00:46:49 +0100580out_destroy:
581 spu_destroy_spu(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500582out_free:
583 kfree(spu);
584out:
585 return ret;
586}
587
Arnd Bergmann67207b92005-11-15 15:53:48 -0500588static int __init init_spu_base(void)
589{
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200590 int i, ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500591
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100592 if (!spu_management_ops)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200593 goto out;
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100594
Jeremy Kerr1d640932006-06-19 20:33:19 +0200595 /* create sysdev class for spus */
596 ret = sysdev_class_register(&spu_sysdev_class);
597 if (ret)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200598 goto out;
Jeremy Kerr1d640932006-06-19 20:33:19 +0200599
Mark Nuttera68cf982006-10-04 17:26:12 +0200600 for (i = 0; i < MAX_NUMNODES; i++)
601 INIT_LIST_HEAD(&spu_list[i]);
602
Geoff Levande28b0032006-11-23 00:46:49 +0100603 ret = spu_enumerate_spus(create_spu);
604
605 if (ret) {
606 printk(KERN_WARNING "%s: Error initializing spus\n",
607 __FUNCTION__);
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200608 goto out_unregister_sysdev_class;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500609 }
Michael Ellermanff8a8f22006-10-24 18:31:27 +0200610
611 xmon_register_spus(&spu_full_list);
612
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200613 return 0;
614
615 out_unregister_sysdev_class:
616 sysdev_class_unregister(&spu_sysdev_class);
617 out:
618
Arnd Bergmann67207b92005-11-15 15:53:48 -0500619 return ret;
620}
621module_init(init_spu_base);
622
623MODULE_LICENSE("GPL");
624MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");