blob: 75b5af0a7e21f20f3682cb85041573d9aea69ac3 [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>
Geert Uytterhoevenbce94512007-07-17 04:05:52 -070034#include <linux/linux_logo.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050035#include <asm/spu.h>
Geoff Levand540270d2006-06-19 20:33:29 +020036#include <asm/spu_priv1.h>
Michael Ellermanff8a8f22006-10-24 18:31:27 +020037#include <asm/xmon.h>
Arnd Bergmann3ad216c2007-07-20 21:39:46 +020038#include <asm/prom.h>
39#include "spu_priv1_mmio.h"
Arnd Bergmann67207b92005-11-15 15:53:48 -050040
Geoff Levande28b0032006-11-23 00:46:49 +010041const struct spu_management_ops *spu_management_ops;
Jeremy Kerrccf17e92007-04-23 21:08:29 +020042EXPORT_SYMBOL_GPL(spu_management_ops);
43
Geoff Levand540270d2006-06-19 20:33:29 +020044const struct spu_priv1_ops *spu_priv1_ops;
45
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010046static LIST_HEAD(spu_full_list);
47static DEFINE_MUTEX(spu_mutex);
Thomas Gleixner057b1842007-04-29 16:10:39 +000048static DEFINE_SPINLOCK(spu_list_lock);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010049
Geoff Levand540270d2006-06-19 20:33:29 +020050EXPORT_SYMBOL_GPL(spu_priv1_ops);
51
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010052void spu_invalidate_slbs(struct spu *spu)
53{
54 struct spu_priv2 __iomem *priv2 = spu->priv2;
55
56 if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
57 out_be64(&priv2->slb_invalidate_all_W, 0UL);
58}
59EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
60
61/* This is called by the MM core when a segment size is changed, to
62 * request a flush of all the SPEs using a given mm
63 */
64void spu_flush_all_slbs(struct mm_struct *mm)
65{
66 struct spu *spu;
67 unsigned long flags;
68
69 spin_lock_irqsave(&spu_list_lock, flags);
70 list_for_each_entry(spu, &spu_full_list, full_list) {
71 if (spu->mm == mm)
72 spu_invalidate_slbs(spu);
73 }
74 spin_unlock_irqrestore(&spu_list_lock, flags);
75}
76
77/* The hack below stinks... try to do something better one of
78 * these days... Does it even work properly with NR_CPUS == 1 ?
79 */
80static inline void mm_needs_global_tlbie(struct mm_struct *mm)
81{
82 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
83
84 /* Global TLBIE broadcast required with SPEs. */
85 __cpus_setall(&mm->cpu_vm_mask, nr);
86}
87
88void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
89{
90 unsigned long flags;
91
92 spin_lock_irqsave(&spu_list_lock, flags);
93 spu->mm = mm;
94 spin_unlock_irqrestore(&spu_list_lock, flags);
95 if (mm)
96 mm_needs_global_tlbie(mm);
97}
98EXPORT_SYMBOL_GPL(spu_associate_mm);
99
Arnd Bergmann67207b92005-11-15 15:53:48 -0500100static int __spu_trap_invalid_dma(struct spu *spu)
101{
102 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200103 spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500104 return 0;
105}
106
107static int __spu_trap_dma_align(struct spu *spu)
108{
109 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200110 spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500111 return 0;
112}
113
114static int __spu_trap_error(struct spu *spu)
115{
116 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200117 spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500118 return 0;
119}
120
121static void spu_restart_dma(struct spu *spu)
122{
123 struct spu_priv2 __iomem *priv2 = spu->priv2;
Mark Nutter5473af02005-11-15 15:53:49 -0500124
Arnd Bergmann8837d922006-01-04 20:31:28 +0100125 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
Mark Nutter5473af02005-11-15 15:53:49 -0500126 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500127}
128
129static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
130{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500131 struct spu_priv2 __iomem *priv2 = spu->priv2;
132 struct mm_struct *mm = spu->mm;
arnd@arndb.de724bd802006-06-19 20:33:23 +0200133 u64 esid, vsid, llp;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100134 int psize;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500135
136 pr_debug("%s\n", __FUNCTION__);
137
Arnd Bergmann8837d922006-01-04 20:31:28 +0100138 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500139 /* SLBs are pre-loaded for context switch, so
140 * we should never get here!
141 */
Mark Nutter5473af02005-11-15 15:53:49 -0500142 printk("%s: invalid access during switch!\n", __func__);
143 return 1;
144 }
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200145 esid = (ea & ESID_MASK) | SLB_ESID_V;
146
147 switch(REGION_ID(ea)) {
148 case USER_REGION_ID:
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000149#ifdef CONFIG_PPC_MM_SLICES
150 psize = get_slice_psize(mm, ea);
151#else
152 psize = mm->context.user_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200153#endif
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200154 vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100155 SLB_VSID_USER;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200156 break;
157 case VMALLOC_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100158 if (ea < VMALLOC_END)
159 psize = mmu_vmalloc_psize;
160 else
161 psize = mmu_io_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200162 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100163 SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200164 break;
165 case KERNEL_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100166 psize = mmu_linear_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200167 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100168 SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200169 break;
170 default:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500171 /* Future: support kernel segments so that drivers
172 * can use SPUs.
173 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500174 pr_debug("invalid region access at %016lx\n", ea);
175 return 1;
176 }
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100177 llp = mmu_psize_defs[psize].sllp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500178
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500179 out_be64(&priv2->slb_index_W, spu->slb_replace);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100180 out_be64(&priv2->slb_vsid_RW, vsid | llp);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500181 out_be64(&priv2->slb_esid_RW, esid);
182
183 spu->slb_replace++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500184 if (spu->slb_replace >= 8)
185 spu->slb_replace = 0;
186
Arnd Bergmann67207b92005-11-15 15:53:48 -0500187 spu_restart_dma(spu);
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000188 spu->stats.slb_flt++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500189 return 0;
190}
191
Mark Nutter5473af02005-11-15 15:53:49 -0500192extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500193static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500194{
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100195 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500196
Mark Nutter5473af02005-11-15 15:53:49 -0500197 /* Handle kernel space hash faults immediately.
198 User hash faults need to be deferred to process context. */
199 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
200 && REGION_ID(ea) != USER_REGION_ID
201 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
202 spu_restart_dma(spu);
203 return 0;
204 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500205
Arnd Bergmann8837d922006-01-04 20:31:28 +0100206 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Mark Nutter5473af02005-11-15 15:53:49 -0500207 printk("%s: invalid access during switch!\n", __func__);
208 return 1;
209 }
210
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500211 spu->dar = ea;
212 spu->dsisr = dsisr;
213 mb();
Masato Noguchiba723fe22006-06-19 20:33:33 +0200214 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500215 return 0;
216}
217
218static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200219spu_irq_class_0(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500220{
221 struct spu *spu;
222
223 spu = data;
224 spu->class_0_pending = 1;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200225 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500226
227 return IRQ_HANDLED;
228}
229
Arnd Bergmann51104592005-12-05 22:52:25 -0500230int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500231spu_irq_class_0_bottom(struct spu *spu)
232{
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500233 unsigned long stat, mask;
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900234 unsigned long flags;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500235
236 spu->class_0_pending = 0;
237
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900238 spin_lock_irqsave(&spu->register_lock, flags);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100239 mask = spu_int_mask_get(spu, 0);
240 stat = spu_int_stat_get(spu, 0);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500241
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500242 stat &= mask;
243
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200244 if (stat & 1) /* invalid DMA alignment */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500245 __spu_trap_dma_align(spu);
246
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200247 if (stat & 2) /* invalid MFC DMA */
248 __spu_trap_invalid_dma(spu);
249
Arnd Bergmann67207b92005-11-15 15:53:48 -0500250 if (stat & 4) /* error on SPU */
251 __spu_trap_error(spu);
252
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100253 spu_int_stat_clear(spu, 0, stat);
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900254 spin_unlock_irqrestore(&spu->register_lock, flags);
Arnd Bergmann51104592005-12-05 22:52:25 -0500255
256 return (stat & 0x7) ? -EIO : 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500257}
Arnd Bergmann51104592005-12-05 22:52:25 -0500258EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500259
260static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200261spu_irq_class_1(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500262{
263 struct spu *spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500264 unsigned long stat, mask, dar, dsisr;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500265
266 spu = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500267
268 /* atomically read & clear class1 status. */
269 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100270 mask = spu_int_mask_get(spu, 1);
271 stat = spu_int_stat_get(spu, 1) & mask;
272 dar = spu_mfc_dar_get(spu);
273 dsisr = spu_mfc_dsisr_get(spu);
Arnd Bergmann38307342005-12-09 19:04:18 +0100274 if (stat & 2) /* mapping fault */
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100275 spu_mfc_dsisr_set(spu, 0ul);
276 spu_int_stat_clear(spu, 1, stat);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500277 spin_unlock(&spu->register_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100278 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
279 dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500280
281 if (stat & 1) /* segment fault */
282 __spu_trap_data_seg(spu, dar);
283
284 if (stat & 2) { /* mapping fault */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500285 __spu_trap_data_map(spu, dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500286 }
287
288 if (stat & 4) /* ls compare & suspend on get */
289 ;
290
291 if (stat & 8) /* ls compare & suspend on put */
292 ;
293
Arnd Bergmann67207b92005-11-15 15:53:48 -0500294 return stat ? IRQ_HANDLED : IRQ_NONE;
295}
296
297static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200298spu_irq_class_2(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500299{
300 struct spu *spu;
301 unsigned long stat;
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500302 unsigned long mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500303
304 spu = data;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200305 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100306 stat = spu_int_stat_get(spu, 2);
307 mask = spu_int_mask_get(spu, 2);
Masato Noguchiba723fe22006-06-19 20:33:33 +0200308 /* ignore interrupts we're not waiting for */
309 stat &= mask;
310 /*
311 * mailbox interrupts (0x1 and 0x10) are level triggered.
312 * mask them now before acknowledging.
313 */
314 if (stat & 0x11)
315 spu_int_mask_and(spu, 2, ~(stat & 0x11));
316 /* acknowledge all interrupts before the callbacks */
317 spu_int_stat_clear(spu, 2, stat);
318 spin_unlock(&spu->register_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500319
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500320 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500321
Arnd Bergmann67207b92005-11-15 15:53:48 -0500322 if (stat & 1) /* PPC core mailbox */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200323 spu->ibox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500324
325 if (stat & 2) /* SPU stop-and-signal */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200326 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500327
328 if (stat & 4) /* SPU halted */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200329 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500330
331 if (stat & 8) /* DMA tag group complete */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200332 spu->mfc_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500333
334 if (stat & 0x10) /* SPU mailbox threshold */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200335 spu->wbox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500336
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000337 spu->stats.class2_intr++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500338 return stat ? IRQ_HANDLED : IRQ_NONE;
339}
340
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000341static int spu_request_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500342{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000343 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500344
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000345 if (spu->irqs[0] != NO_IRQ) {
346 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
347 spu->number);
348 ret = request_irq(spu->irqs[0], spu_irq_class_0,
349 IRQF_DISABLED,
350 spu->irq_c0, spu);
351 if (ret)
352 goto bail0;
353 }
354 if (spu->irqs[1] != NO_IRQ) {
355 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
356 spu->number);
357 ret = request_irq(spu->irqs[1], spu_irq_class_1,
358 IRQF_DISABLED,
359 spu->irq_c1, spu);
360 if (ret)
361 goto bail1;
362 }
363 if (spu->irqs[2] != NO_IRQ) {
364 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
365 spu->number);
366 ret = request_irq(spu->irqs[2], spu_irq_class_2,
367 IRQF_DISABLED,
368 spu->irq_c2, spu);
369 if (ret)
370 goto bail2;
371 }
372 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500373
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000374bail2:
375 if (spu->irqs[1] != NO_IRQ)
376 free_irq(spu->irqs[1], spu);
377bail1:
378 if (spu->irqs[0] != NO_IRQ)
379 free_irq(spu->irqs[0], spu);
380bail0:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500381 return ret;
382}
383
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000384static void spu_free_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500385{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000386 if (spu->irqs[0] != NO_IRQ)
387 free_irq(spu->irqs[0], spu);
388 if (spu->irqs[1] != NO_IRQ)
389 free_irq(spu->irqs[1], spu);
390 if (spu->irqs[2] != NO_IRQ)
391 free_irq(spu->irqs[2], spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500392}
393
Arnd Bergmann67207b92005-11-15 15:53:48 -0500394static void spu_init_channels(struct spu *spu)
395{
396 static const struct {
397 unsigned channel;
398 unsigned count;
399 } zero_list[] = {
400 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
401 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
402 }, count_list[] = {
403 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
404 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
405 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
406 };
Arnd Bergmann6ff730c2006-01-04 20:31:31 +0100407 struct spu_priv2 __iomem *priv2;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500408 int i;
409
410 priv2 = spu->priv2;
411
412 /* initialize all channel data to zero */
413 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
414 int count;
415
416 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
417 for (count = 0; count < zero_list[i].count; count++)
418 out_be64(&priv2->spu_chnldata_RW, 0);
419 }
420
421 /* initialize channel counts to meaningful values */
422 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
423 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
424 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
425 }
426}
427
Mark Nuttera68cf982006-10-04 17:26:12 +0200428struct spu *spu_alloc_node(int node)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500429{
Mark Nuttera68cf982006-10-04 17:26:12 +0200430 struct spu *spu = NULL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500431
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800432 mutex_lock(&spu_mutex);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200433 if (!list_empty(&cbe_spu_info[node].free_spus)) {
434 spu = list_entry(cbe_spu_info[node].free_spus.next, struct spu,
435 list);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500436 list_del_init(&spu->list);
Geoff Levandcc21a662006-10-24 18:31:15 +0200437 pr_debug("Got SPU %d %d\n", spu->number, spu->node);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500438 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800439 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500440
Christoph Hellwig62c05d52007-04-23 21:08:14 +0200441 if (spu)
442 spu_init_channels(spu);
Mark Nuttera68cf982006-10-04 17:26:12 +0200443 return spu;
444}
445EXPORT_SYMBOL_GPL(spu_alloc_node);
446
447struct spu *spu_alloc(void)
448{
449 struct spu *spu = NULL;
450 int node;
451
452 for (node = 0; node < MAX_NUMNODES; node++) {
453 spu = spu_alloc_node(node);
454 if (spu)
455 break;
456 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500457
458 return spu;
459}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500460
461void spu_free(struct spu *spu)
462{
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800463 mutex_lock(&spu_mutex);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200464 list_add_tail(&spu->list, &cbe_spu_info[spu->node].free_spus);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800465 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500466}
Arnd Bergmann39c73c32005-12-05 22:52:21 -0500467EXPORT_SYMBOL_GPL(spu_free);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500468
Geoff Levand6deac062007-06-16 07:17:32 +1000469static int spu_shutdown(struct sys_device *sysdev)
470{
471 struct spu *spu = container_of(sysdev, struct spu, sysdev);
472
473 spu_free_irqs(spu);
474 spu_destroy_spu(spu);
475 return 0;
476}
477
Jeremy Kerr1d640932006-06-19 20:33:19 +0200478struct sysdev_class spu_sysdev_class = {
Geoff Levand6deac062007-06-16 07:17:32 +1000479 set_kset_name("spu"),
480 .shutdown = spu_shutdown,
Jeremy Kerr1d640932006-06-19 20:33:19 +0200481};
482
Christian Kraffte570beb2006-10-24 18:31:23 +0200483int spu_add_sysdev_attr(struct sysdev_attribute *attr)
484{
485 struct spu *spu;
486 mutex_lock(&spu_mutex);
487
488 list_for_each_entry(spu, &spu_full_list, full_list)
489 sysdev_create_file(&spu->sysdev, attr);
490
491 mutex_unlock(&spu_mutex);
492 return 0;
493}
494EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
495
496int spu_add_sysdev_attr_group(struct attribute_group *attrs)
497{
498 struct spu *spu;
499 mutex_lock(&spu_mutex);
500
501 list_for_each_entry(spu, &spu_full_list, full_list)
502 sysfs_create_group(&spu->sysdev.kobj, attrs);
503
504 mutex_unlock(&spu_mutex);
505 return 0;
506}
507EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
508
509
510void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
511{
512 struct spu *spu;
513 mutex_lock(&spu_mutex);
514
515 list_for_each_entry(spu, &spu_full_list, full_list)
516 sysdev_remove_file(&spu->sysdev, attr);
517
518 mutex_unlock(&spu_mutex);
519}
520EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
521
522void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
523{
524 struct spu *spu;
525 mutex_lock(&spu_mutex);
526
527 list_for_each_entry(spu, &spu_full_list, full_list)
528 sysfs_remove_group(&spu->sysdev.kobj, attrs);
529
530 mutex_unlock(&spu_mutex);
531}
532EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
533
Jeremy Kerr1d640932006-06-19 20:33:19 +0200534static int spu_create_sysdev(struct spu *spu)
535{
536 int ret;
537
538 spu->sysdev.id = spu->number;
539 spu->sysdev.cls = &spu_sysdev_class;
540 ret = sysdev_register(&spu->sysdev);
541 if (ret) {
542 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
543 spu->number);
544 return ret;
545 }
546
Geoff Levand00215502006-11-20 18:45:02 +0100547 sysfs_add_device_to_node(&spu->sysdev, spu->node);
Jeremy Kerr1d640932006-06-19 20:33:19 +0200548
549 return 0;
550}
551
Geoff Levande28b0032006-11-23 00:46:49 +0100552static int __init create_spu(void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500553{
554 struct spu *spu;
555 int ret;
556 static int number;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100557 unsigned long flags;
Andre Detsch27ec41d2007-07-20 21:39:33 +0200558 struct timespec ts;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500559
560 ret = -ENOMEM;
Jeremy Kerrecec2172006-06-19 20:33:26 +0200561 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500562 if (!spu)
563 goto out;
564
Geoff Levande28b0032006-11-23 00:46:49 +0100565 spin_lock_init(&spu->register_lock);
566 mutex_lock(&spu_mutex);
567 spu->number = number++;
568 mutex_unlock(&spu_mutex);
Benjamin Herrenschmidte5267b42006-10-10 15:14:12 +1000569
Geoff Levande28b0032006-11-23 00:46:49 +0100570 ret = spu_create_spu(spu, data);
571
Arnd Bergmann67207b92005-11-15 15:53:48 -0500572 if (ret)
573 goto out_free;
574
Masato Noguchi24f43b32006-10-24 18:31:14 +0200575 spu_mfc_sdr_setup(spu);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100576 spu_mfc_sr1_set(spu, 0x33);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500577 ret = spu_request_irqs(spu);
578 if (ret)
Geoff Levande28b0032006-11-23 00:46:49 +0100579 goto out_destroy;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500580
Jeremy Kerr1d640932006-06-19 20:33:19 +0200581 ret = spu_create_sysdev(spu);
582 if (ret)
583 goto out_free_irqs;
584
Geoff Levande28b0032006-11-23 00:46:49 +0100585 mutex_lock(&spu_mutex);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100586 spin_lock_irqsave(&spu_list_lock, flags);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200587 list_add(&spu->list, &cbe_spu_info[spu->node].free_spus);
588 list_add(&spu->cbe_list, &cbe_spu_info[spu->node].spus);
589 cbe_spu_info[spu->node].n_spus++;
Christian Kraffte570beb2006-10-24 18:31:23 +0200590 list_add(&spu->full_list, &spu_full_list);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100591 spin_unlock_irqrestore(&spu_list_lock, flags);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800592 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500593
Andre Detsch27ec41d2007-07-20 21:39:33 +0200594 spu->stats.util_state = SPU_UTIL_IDLE_LOADED;
595 ktime_get_ts(&ts);
596 spu->stats.tstamp = timespec_to_ns(&ts);
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000597
Arnd Bergmann9d92af62007-07-20 21:39:45 +0200598 INIT_LIST_HEAD(&spu->aff_list);
599
Arnd Bergmann67207b92005-11-15 15:53:48 -0500600 goto out;
601
Jeremy Kerr1d640932006-06-19 20:33:19 +0200602out_free_irqs:
603 spu_free_irqs(spu);
Geoff Levande28b0032006-11-23 00:46:49 +0100604out_destroy:
605 spu_destroy_spu(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500606out_free:
607 kfree(spu);
608out:
609 return ret;
610}
611
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000612static const char *spu_state_names[] = {
613 "user", "system", "iowait", "idle"
614};
615
616static unsigned long long spu_acct_time(struct spu *spu,
617 enum spu_utilization_state state)
618{
Andre Detsch27ec41d2007-07-20 21:39:33 +0200619 struct timespec ts;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000620 unsigned long long time = spu->stats.times[state];
621
Andre Detsch27ec41d2007-07-20 21:39:33 +0200622 /*
623 * If the spu is idle or the context is stopped, utilization
624 * statistics are not updated. Apply the time delta from the
625 * last recorded state of the spu.
626 */
627 if (spu->stats.util_state == state) {
628 ktime_get_ts(&ts);
629 time += timespec_to_ns(&ts) - spu->stats.tstamp;
630 }
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000631
Andre Detsch27ec41d2007-07-20 21:39:33 +0200632 return time / NSEC_PER_MSEC;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000633}
634
635
636static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf)
637{
638 struct spu *spu = container_of(sysdev, struct spu, sysdev);
639
640 return sprintf(buf, "%s %llu %llu %llu %llu "
641 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +0200642 spu_state_names[spu->stats.util_state],
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000643 spu_acct_time(spu, SPU_UTIL_USER),
644 spu_acct_time(spu, SPU_UTIL_SYSTEM),
645 spu_acct_time(spu, SPU_UTIL_IOWAIT),
Andre Detsch27ec41d2007-07-20 21:39:33 +0200646 spu_acct_time(spu, SPU_UTIL_IDLE_LOADED),
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000647 spu->stats.vol_ctx_switch,
648 spu->stats.invol_ctx_switch,
649 spu->stats.slb_flt,
650 spu->stats.hash_flt,
651 spu->stats.min_flt,
652 spu->stats.maj_flt,
653 spu->stats.class2_intr,
654 spu->stats.libassist);
655}
656
657static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL);
658
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200659struct cbe_spu_info cbe_spu_info[MAX_NUMNODES];
660EXPORT_SYMBOL_GPL(cbe_spu_info);
661
Arnd Bergmann3ad216c2007-07-20 21:39:46 +0200662/* Hardcoded affinity idxs for QS20 */
663#define SPES_PER_BE 8
664static int QS20_reg_idxs[SPES_PER_BE] = { 0, 2, 4, 6, 7, 5, 3, 1 };
665static int QS20_reg_memory[SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 };
666
667static struct spu *spu_lookup_reg(int node, u32 reg)
668{
669 struct spu *spu;
670
671 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
672 if (*(u32 *)get_property(spu_devnode(spu), "reg", NULL) == reg)
673 return spu;
674 }
675 return NULL;
676}
677
678static void init_aff_QS20_harcoded(void)
679{
680 int node, i;
681 struct spu *last_spu, *spu;
682 u32 reg;
683
684 for (node = 0; node < MAX_NUMNODES; node++) {
685 last_spu = NULL;
686 for (i = 0; i < SPES_PER_BE; i++) {
687 reg = QS20_reg_idxs[i];
688 spu = spu_lookup_reg(node, reg);
689 if (!spu)
690 continue;
691 spu->has_mem_affinity = QS20_reg_memory[reg];
692 if (last_spu)
693 list_add_tail(&spu->aff_list,
694 &last_spu->aff_list);
695 last_spu = spu;
696 }
697 }
698}
699
700static int of_has_vicinity(void)
701{
702 struct spu* spu;
703
704 spu = list_entry(cbe_spu_info[0].spus.next, struct spu, cbe_list);
705 return of_find_property(spu_devnode(spu), "vicinity", NULL) != NULL;
706}
707
Arnd Bergmann67207b92005-11-15 15:53:48 -0500708static int __init init_spu_base(void)
709{
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200710 int i, ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500711
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200712 for (i = 0; i < MAX_NUMNODES; i++) {
713 INIT_LIST_HEAD(&cbe_spu_info[i].spus);
714 INIT_LIST_HEAD(&cbe_spu_info[i].free_spus);
715 }
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200716
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100717 if (!spu_management_ops)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200718 goto out;
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100719
Jeremy Kerr1d640932006-06-19 20:33:19 +0200720 /* create sysdev class for spus */
721 ret = sysdev_class_register(&spu_sysdev_class);
722 if (ret)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200723 goto out;
Jeremy Kerr1d640932006-06-19 20:33:19 +0200724
Geoff Levande28b0032006-11-23 00:46:49 +0100725 ret = spu_enumerate_spus(create_spu);
726
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700727 if (ret < 0) {
Geoff Levande28b0032006-11-23 00:46:49 +0100728 printk(KERN_WARNING "%s: Error initializing spus\n",
729 __FUNCTION__);
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200730 goto out_unregister_sysdev_class;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500731 }
Michael Ellermanff8a8f22006-10-24 18:31:27 +0200732
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700733 if (ret > 0) {
734 /*
735 * We cannot put the forward declaration in
736 * <linux/linux_logo.h> because of conflicting session type
737 * conflicts for const and __initdata with different compiler
738 * versions
739 */
740 extern const struct linux_logo logo_spe_clut224;
741
742 fb_append_extra_logo(&logo_spe_clut224, ret);
743 }
744
Michael Ellermanff8a8f22006-10-24 18:31:27 +0200745 xmon_register_spus(&spu_full_list);
Andre Detsch8d2655e2007-07-20 21:39:27 +0200746 crash_register_spus(&spu_full_list);
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000747 spu_add_sysdev_attr(&attr_stat);
748
Arnd Bergmann3ad216c2007-07-20 21:39:46 +0200749 if (!of_has_vicinity()) {
750 long root = of_get_flat_dt_root();
751 if (of_flat_dt_is_compatible(root, "IBM,CPBW-1.0"))
752 init_aff_QS20_harcoded();
753 }
754
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200755 return 0;
756
757 out_unregister_sysdev_class:
758 sysdev_class_unregister(&spu_sysdev_class);
759 out:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500760 return ret;
761}
762module_init(init_spu_base);
763
764MODULE_LICENSE("GPL");
765MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");