blob: f73263ba98412e97ad73f2e679001354940a22f2 [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>
Jeremy Kerr58bd4032007-12-05 13:49:31 +110037#include <asm/spu_csa.h>
Michael Ellermanff8a8f22006-10-24 18:31:27 +020038#include <asm/xmon.h>
Arnd Bergmann3ad216c2007-07-20 21:39:46 +020039#include <asm/prom.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;
Geoff Levand540270d2006-06-19 20:33:29 +020045EXPORT_SYMBOL_GPL(spu_priv1_ops);
46
Christoph Hellwig24140592007-07-20 21:39:51 +020047struct cbe_spu_info cbe_spu_info[MAX_NUMNODES];
48EXPORT_SYMBOL_GPL(cbe_spu_info);
49
50/*
Jeremy Kerr3ce2f622007-12-05 13:49:31 +110051 * The spufs fault-handling code needs to call force_sig_info to raise signals
52 * on DMA errors. Export it here to avoid general kernel-wide access to this
53 * function
54 */
55EXPORT_SYMBOL_GPL(force_sig_info);
56
57/*
Christoph Hellwig24140592007-07-20 21:39:51 +020058 * Protects cbe_spu_info and spu->number.
59 */
60static DEFINE_SPINLOCK(spu_lock);
61
62/*
63 * List of all spus in the system.
64 *
65 * This list is iterated by callers from irq context and callers that
66 * want to sleep. Thus modifications need to be done with both
67 * spu_full_list_lock and spu_full_list_mutex held, while iterating
68 * through it requires either of these locks.
69 *
70 * In addition spu_full_list_lock protects all assignmens to
71 * spu->mm.
72 */
73static LIST_HEAD(spu_full_list);
74static DEFINE_SPINLOCK(spu_full_list_lock);
75static DEFINE_MUTEX(spu_full_list_mutex);
76
Jeremy Kerr58bd4032007-12-05 13:49:31 +110077struct spu_slb {
78 u64 esid, vsid;
79};
80
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010081void spu_invalidate_slbs(struct spu *spu)
82{
83 struct spu_priv2 __iomem *priv2 = spu->priv2;
84
85 if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
86 out_be64(&priv2->slb_invalidate_all_W, 0UL);
87}
88EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
89
90/* This is called by the MM core when a segment size is changed, to
91 * request a flush of all the SPEs using a given mm
92 */
93void spu_flush_all_slbs(struct mm_struct *mm)
94{
95 struct spu *spu;
96 unsigned long flags;
97
Christoph Hellwig24140592007-07-20 21:39:51 +020098 spin_lock_irqsave(&spu_full_list_lock, flags);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010099 list_for_each_entry(spu, &spu_full_list, full_list) {
100 if (spu->mm == mm)
101 spu_invalidate_slbs(spu);
102 }
Christoph Hellwig24140592007-07-20 21:39:51 +0200103 spin_unlock_irqrestore(&spu_full_list_lock, flags);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100104}
105
106/* The hack below stinks... try to do something better one of
107 * these days... Does it even work properly with NR_CPUS == 1 ?
108 */
109static inline void mm_needs_global_tlbie(struct mm_struct *mm)
110{
111 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
112
113 /* Global TLBIE broadcast required with SPEs. */
114 __cpus_setall(&mm->cpu_vm_mask, nr);
115}
116
117void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
118{
119 unsigned long flags;
120
Christoph Hellwig24140592007-07-20 21:39:51 +0200121 spin_lock_irqsave(&spu_full_list_lock, flags);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100122 spu->mm = mm;
Christoph Hellwig24140592007-07-20 21:39:51 +0200123 spin_unlock_irqrestore(&spu_full_list_lock, flags);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100124 if (mm)
125 mm_needs_global_tlbie(mm);
126}
127EXPORT_SYMBOL_GPL(spu_associate_mm);
128
Jeremy Kerrf6eb7d7f2007-12-05 13:49:31 +1100129int spu_64k_pages_available(void)
130{
131 return mmu_psize_defs[MMU_PAGE_64K].shift != 0;
132}
133EXPORT_SYMBOL_GPL(spu_64k_pages_available);
134
Arnd Bergmann67207b92005-11-15 15:53:48 -0500135static int __spu_trap_invalid_dma(struct spu *spu)
136{
137 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200138 spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500139 return 0;
140}
141
142static int __spu_trap_dma_align(struct spu *spu)
143{
144 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200145 spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500146 return 0;
147}
148
149static int __spu_trap_error(struct spu *spu)
150{
151 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200152 spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500153 return 0;
154}
155
156static void spu_restart_dma(struct spu *spu)
157{
158 struct spu_priv2 __iomem *priv2 = spu->priv2;
Mark Nutter5473af02005-11-15 15:53:49 -0500159
Arnd Bergmann8837d922006-01-04 20:31:28 +0100160 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
Mark Nutter5473af02005-11-15 15:53:49 -0500161 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500162}
163
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100164static inline void spu_load_slb(struct spu *spu, int slbe, struct spu_slb *slb)
165{
166 struct spu_priv2 __iomem *priv2 = spu->priv2;
167
168 pr_debug("%s: adding SLB[%d] 0x%016lx 0x%016lx\n",
169 __func__, slbe, slb->vsid, slb->esid);
170
171 out_be64(&priv2->slb_index_W, slbe);
172 out_be64(&priv2->slb_vsid_RW, slb->vsid);
173 out_be64(&priv2->slb_esid_RW, slb->esid);
174}
175
Arnd Bergmann67207b92005-11-15 15:53:48 -0500176static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
177{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500178 struct mm_struct *mm = spu->mm;
Jeremy Kerr4d434662007-12-05 13:49:31 +1100179 struct spu_slb slb;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100180 int psize;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500181
182 pr_debug("%s\n", __FUNCTION__);
183
Arnd Bergmann8837d922006-01-04 20:31:28 +0100184 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500185 /* SLBs are pre-loaded for context switch, so
186 * we should never get here!
187 */
Mark Nutter5473af02005-11-15 15:53:49 -0500188 printk("%s: invalid access during switch!\n", __func__);
189 return 1;
190 }
Jeremy Kerr4d434662007-12-05 13:49:31 +1100191 slb.esid = (ea & ESID_MASK) | SLB_ESID_V;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200192
193 switch(REGION_ID(ea)) {
194 case USER_REGION_ID:
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000195#ifdef CONFIG_PPC_MM_SLICES
196 psize = get_slice_psize(mm, ea);
197#else
198 psize = mm->context.user_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200199#endif
Jeremy Kerr4d434662007-12-05 13:49:31 +1100200 slb.vsid = (get_vsid(mm->context.id, ea, MMU_SEGSIZE_256M)
201 << SLB_VSID_SHIFT) | SLB_VSID_USER;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200202 break;
203 case VMALLOC_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100204 if (ea < VMALLOC_END)
205 psize = mmu_vmalloc_psize;
206 else
207 psize = mmu_io_psize;
Jeremy Kerr4d434662007-12-05 13:49:31 +1100208 slb.vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M)
209 << SLB_VSID_SHIFT) | SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200210 break;
211 case KERNEL_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100212 psize = mmu_linear_psize;
Jeremy Kerr4d434662007-12-05 13:49:31 +1100213 slb.vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M)
214 << SLB_VSID_SHIFT) | SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200215 break;
216 default:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500217 /* Future: support kernel segments so that drivers
218 * can use SPUs.
219 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500220 pr_debug("invalid region access at %016lx\n", ea);
221 return 1;
222 }
Jeremy Kerr4d434662007-12-05 13:49:31 +1100223 slb.vsid |= mmu_psize_defs[psize].sllp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500224
Jeremy Kerr4d434662007-12-05 13:49:31 +1100225 spu_load_slb(spu, spu->slb_replace, &slb);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500226
227 spu->slb_replace++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500228 if (spu->slb_replace >= 8)
229 spu->slb_replace = 0;
230
Arnd Bergmann67207b92005-11-15 15:53:48 -0500231 spu_restart_dma(spu);
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000232 spu->stats.slb_flt++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500233 return 0;
234}
235
Mark Nutter5473af02005-11-15 15:53:49 -0500236extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500237static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500238{
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100239 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500240
Mark Nutter5473af02005-11-15 15:53:49 -0500241 /* Handle kernel space hash faults immediately.
242 User hash faults need to be deferred to process context. */
243 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
244 && REGION_ID(ea) != USER_REGION_ID
245 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
246 spu_restart_dma(spu);
247 return 0;
248 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500249
Arnd Bergmann8837d922006-01-04 20:31:28 +0100250 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Mark Nutter5473af02005-11-15 15:53:49 -0500251 printk("%s: invalid access during switch!\n", __func__);
252 return 1;
253 }
254
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500255 spu->dar = ea;
256 spu->dsisr = dsisr;
257 mb();
Masato Noguchiba723fe22006-06-19 20:33:33 +0200258 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500259 return 0;
260}
261
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100262static void __spu_kernel_slb(void *addr, struct spu_slb *slb)
263{
264 unsigned long ea = (unsigned long)addr;
265 u64 llp;
266
267 if (REGION_ID(ea) == KERNEL_REGION_ID)
268 llp = mmu_psize_defs[mmu_linear_psize].sllp;
269 else
270 llp = mmu_psize_defs[mmu_virtual_psize].sllp;
271
272 slb->vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M) << SLB_VSID_SHIFT) |
273 SLB_VSID_KERNEL | llp;
274 slb->esid = (ea & ESID_MASK) | SLB_ESID_V;
275}
276
277/**
Jeremy Kerr684bd612007-12-05 13:49:31 +1100278 * Given an array of @nr_slbs SLB entries, @slbs, return non-zero if the
279 * address @new_addr is present.
280 */
281static inline int __slb_present(struct spu_slb *slbs, int nr_slbs,
282 void *new_addr)
283{
284 unsigned long ea = (unsigned long)new_addr;
285 int i;
286
287 for (i = 0; i < nr_slbs; i++)
288 if (!((slbs[i].esid ^ ea) & ESID_MASK))
289 return 1;
290
291 return 0;
292}
293
294/**
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100295 * Setup the SPU kernel SLBs, in preparation for a context save/restore. We
296 * need to map both the context save area, and the save/restore code.
Jeremy Kerr684bd612007-12-05 13:49:31 +1100297 *
298 * Because the lscsa and code may cross segment boundaires, we check to see
299 * if mappings are required for the start and end of each range. We currently
300 * assume that the mappings are smaller that one segment - if not, something
301 * is seriously wrong.
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100302 */
Jeremy Kerr684bd612007-12-05 13:49:31 +1100303void spu_setup_kernel_slbs(struct spu *spu, struct spu_lscsa *lscsa,
304 void *code, int code_size)
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100305{
Jeremy Kerr684bd612007-12-05 13:49:31 +1100306 struct spu_slb slbs[4];
307 int i, nr_slbs = 0;
308 /* start and end addresses of both mappings */
309 void *addrs[] = {
310 lscsa, (void *)lscsa + sizeof(*lscsa) - 1,
311 code, code + code_size - 1
312 };
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100313
Jeremy Kerr684bd612007-12-05 13:49:31 +1100314 /* check the set of addresses, and create a new entry in the slbs array
315 * if there isn't already a SLB for that address */
316 for (i = 0; i < ARRAY_SIZE(addrs); i++) {
317 if (__slb_present(slbs, nr_slbs, addrs[i]))
318 continue;
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100319
Jeremy Kerr684bd612007-12-05 13:49:31 +1100320 __spu_kernel_slb(addrs[i], &slbs[nr_slbs]);
321 nr_slbs++;
322 }
323
324 /* Add the set of SLBs */
325 for (i = 0; i < nr_slbs; i++)
326 spu_load_slb(spu, i, &slbs[i]);
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100327}
328EXPORT_SYMBOL_GPL(spu_setup_kernel_slbs);
329
Arnd Bergmann67207b92005-11-15 15:53:48 -0500330static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200331spu_irq_class_0(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500332{
333 struct spu *spu;
Masato Noguchib7f90a42007-09-07 18:28:27 +1000334 unsigned long stat, mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500335
336 spu = data;
Masato Noguchib7f90a42007-09-07 18:28:27 +1000337
338 mask = spu_int_mask_get(spu, 0);
339 stat = spu_int_stat_get(spu, 0);
340 stat &= mask;
341
342 spin_lock(&spu->register_lock);
343 spu->class_0_pending |= stat;
344 spin_unlock(&spu->register_lock);
345
Masato Noguchiba723fe22006-06-19 20:33:33 +0200346 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500347
Masato Noguchib7f90a42007-09-07 18:28:27 +1000348 spu_int_stat_clear(spu, 0, stat);
349
Arnd Bergmann67207b92005-11-15 15:53:48 -0500350 return IRQ_HANDLED;
351}
352
Arnd Bergmann51104592005-12-05 22:52:25 -0500353int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500354spu_irq_class_0_bottom(struct spu *spu)
355{
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900356 unsigned long flags;
Masato Noguchib7f90a42007-09-07 18:28:27 +1000357 unsigned long stat;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500358
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900359 spin_lock_irqsave(&spu->register_lock, flags);
Masato Noguchib7f90a42007-09-07 18:28:27 +1000360 stat = spu->class_0_pending;
361 spu->class_0_pending = 0;
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500362
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200363 if (stat & 1) /* invalid DMA alignment */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500364 __spu_trap_dma_align(spu);
365
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200366 if (stat & 2) /* invalid MFC DMA */
367 __spu_trap_invalid_dma(spu);
368
Arnd Bergmann67207b92005-11-15 15:53:48 -0500369 if (stat & 4) /* error on SPU */
370 __spu_trap_error(spu);
371
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900372 spin_unlock_irqrestore(&spu->register_lock, flags);
Arnd Bergmann51104592005-12-05 22:52:25 -0500373
374 return (stat & 0x7) ? -EIO : 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500375}
Arnd Bergmann51104592005-12-05 22:52:25 -0500376EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500377
378static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200379spu_irq_class_1(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500380{
381 struct spu *spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500382 unsigned long stat, mask, dar, dsisr;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500383
384 spu = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500385
386 /* atomically read & clear class1 status. */
387 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100388 mask = spu_int_mask_get(spu, 1);
389 stat = spu_int_stat_get(spu, 1) & mask;
390 dar = spu_mfc_dar_get(spu);
391 dsisr = spu_mfc_dsisr_get(spu);
Arnd Bergmann38307342005-12-09 19:04:18 +0100392 if (stat & 2) /* mapping fault */
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100393 spu_mfc_dsisr_set(spu, 0ul);
394 spu_int_stat_clear(spu, 1, stat);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500395 spin_unlock(&spu->register_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100396 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
397 dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500398
399 if (stat & 1) /* segment fault */
400 __spu_trap_data_seg(spu, dar);
401
402 if (stat & 2) { /* mapping fault */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500403 __spu_trap_data_map(spu, dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500404 }
405
406 if (stat & 4) /* ls compare & suspend on get */
407 ;
408
409 if (stat & 8) /* ls compare & suspend on put */
410 ;
411
Arnd Bergmann67207b92005-11-15 15:53:48 -0500412 return stat ? IRQ_HANDLED : IRQ_NONE;
413}
414
415static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200416spu_irq_class_2(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500417{
418 struct spu *spu;
419 unsigned long stat;
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500420 unsigned long mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500421
422 spu = data;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200423 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100424 stat = spu_int_stat_get(spu, 2);
425 mask = spu_int_mask_get(spu, 2);
Masato Noguchiba723fe22006-06-19 20:33:33 +0200426 /* ignore interrupts we're not waiting for */
427 stat &= mask;
428 /*
429 * mailbox interrupts (0x1 and 0x10) are level triggered.
430 * mask them now before acknowledging.
431 */
432 if (stat & 0x11)
433 spu_int_mask_and(spu, 2, ~(stat & 0x11));
434 /* acknowledge all interrupts before the callbacks */
435 spu_int_stat_clear(spu, 2, stat);
436 spin_unlock(&spu->register_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500437
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500438 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500439
Arnd Bergmann67207b92005-11-15 15:53:48 -0500440 if (stat & 1) /* PPC core mailbox */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200441 spu->ibox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500442
443 if (stat & 2) /* SPU stop-and-signal */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200444 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500445
446 if (stat & 4) /* SPU halted */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200447 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500448
449 if (stat & 8) /* DMA tag group complete */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200450 spu->mfc_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500451
452 if (stat & 0x10) /* SPU mailbox threshold */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200453 spu->wbox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500454
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000455 spu->stats.class2_intr++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500456 return stat ? IRQ_HANDLED : IRQ_NONE;
457}
458
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000459static int spu_request_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500460{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000461 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500462
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000463 if (spu->irqs[0] != NO_IRQ) {
464 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
465 spu->number);
466 ret = request_irq(spu->irqs[0], spu_irq_class_0,
467 IRQF_DISABLED,
468 spu->irq_c0, spu);
469 if (ret)
470 goto bail0;
471 }
472 if (spu->irqs[1] != NO_IRQ) {
473 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
474 spu->number);
475 ret = request_irq(spu->irqs[1], spu_irq_class_1,
476 IRQF_DISABLED,
477 spu->irq_c1, spu);
478 if (ret)
479 goto bail1;
480 }
481 if (spu->irqs[2] != NO_IRQ) {
482 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
483 spu->number);
484 ret = request_irq(spu->irqs[2], spu_irq_class_2,
485 IRQF_DISABLED,
486 spu->irq_c2, spu);
487 if (ret)
488 goto bail2;
489 }
490 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500491
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000492bail2:
493 if (spu->irqs[1] != NO_IRQ)
494 free_irq(spu->irqs[1], spu);
495bail1:
496 if (spu->irqs[0] != NO_IRQ)
497 free_irq(spu->irqs[0], spu);
498bail0:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500499 return ret;
500}
501
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000502static void spu_free_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500503{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000504 if (spu->irqs[0] != NO_IRQ)
505 free_irq(spu->irqs[0], spu);
506 if (spu->irqs[1] != NO_IRQ)
507 free_irq(spu->irqs[1], spu);
508 if (spu->irqs[2] != NO_IRQ)
509 free_irq(spu->irqs[2], spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500510}
511
Christoph Hellwig486acd42007-07-20 21:39:54 +0200512void spu_init_channels(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500513{
514 static const struct {
515 unsigned channel;
516 unsigned count;
517 } zero_list[] = {
518 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
519 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
520 }, count_list[] = {
521 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
522 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
523 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
524 };
Arnd Bergmann6ff730c2006-01-04 20:31:31 +0100525 struct spu_priv2 __iomem *priv2;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500526 int i;
527
528 priv2 = spu->priv2;
529
530 /* initialize all channel data to zero */
531 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
532 int count;
533
534 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
535 for (count = 0; count < zero_list[i].count; count++)
536 out_be64(&priv2->spu_chnldata_RW, 0);
537 }
538
539 /* initialize channel counts to meaningful values */
540 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
541 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
542 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
543 }
544}
Christoph Hellwig486acd42007-07-20 21:39:54 +0200545EXPORT_SYMBOL_GPL(spu_init_channels);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500546
Geoff Levand6deac062007-06-16 07:17:32 +1000547static int spu_shutdown(struct sys_device *sysdev)
548{
549 struct spu *spu = container_of(sysdev, struct spu, sysdev);
550
551 spu_free_irqs(spu);
552 spu_destroy_spu(spu);
553 return 0;
554}
555
Sebastian Siewior12388192007-09-19 14:38:12 +1000556static struct sysdev_class spu_sysdev_class = {
Geoff Levand6deac062007-06-16 07:17:32 +1000557 set_kset_name("spu"),
558 .shutdown = spu_shutdown,
Jeremy Kerr1d640932006-06-19 20:33:19 +0200559};
560
Christian Kraffte570beb2006-10-24 18:31:23 +0200561int spu_add_sysdev_attr(struct sysdev_attribute *attr)
562{
563 struct spu *spu;
Christian Kraffte570beb2006-10-24 18:31:23 +0200564
Christoph Hellwig24140592007-07-20 21:39:51 +0200565 mutex_lock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200566 list_for_each_entry(spu, &spu_full_list, full_list)
567 sysdev_create_file(&spu->sysdev, attr);
Christoph Hellwig24140592007-07-20 21:39:51 +0200568 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200569
Christian Kraffte570beb2006-10-24 18:31:23 +0200570 return 0;
571}
572EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
573
574int spu_add_sysdev_attr_group(struct attribute_group *attrs)
575{
576 struct spu *spu;
Jeremy Kerr1e771032007-12-05 13:49:31 +1100577 int rc = 0;
Christian Kraffte570beb2006-10-24 18:31:23 +0200578
Christoph Hellwig24140592007-07-20 21:39:51 +0200579 mutex_lock(&spu_full_list_mutex);
Jeremy Kerr1e771032007-12-05 13:49:31 +1100580 list_for_each_entry(spu, &spu_full_list, full_list) {
581 rc = sysfs_create_group(&spu->sysdev.kobj, attrs);
582
583 /* we're in trouble here, but try unwinding anyway */
584 if (rc) {
585 printk(KERN_ERR "%s: can't create sysfs group '%s'\n",
586 __func__, attrs->name);
587
588 list_for_each_entry_continue_reverse(spu,
589 &spu_full_list, full_list)
590 sysfs_remove_group(&spu->sysdev.kobj, attrs);
591 break;
592 }
593 }
594
Christoph Hellwig24140592007-07-20 21:39:51 +0200595 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200596
Jeremy Kerr1e771032007-12-05 13:49:31 +1100597 return rc;
Christian Kraffte570beb2006-10-24 18:31:23 +0200598}
599EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
600
601
602void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
603{
604 struct spu *spu;
Christian Kraffte570beb2006-10-24 18:31:23 +0200605
Christoph Hellwig24140592007-07-20 21:39:51 +0200606 mutex_lock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200607 list_for_each_entry(spu, &spu_full_list, full_list)
608 sysdev_remove_file(&spu->sysdev, attr);
Christoph Hellwig24140592007-07-20 21:39:51 +0200609 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200610}
611EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
612
613void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
614{
615 struct spu *spu;
Christian Kraffte570beb2006-10-24 18:31:23 +0200616
Christoph Hellwig24140592007-07-20 21:39:51 +0200617 mutex_lock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200618 list_for_each_entry(spu, &spu_full_list, full_list)
619 sysfs_remove_group(&spu->sysdev.kobj, attrs);
Christoph Hellwig24140592007-07-20 21:39:51 +0200620 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200621}
622EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
623
Jeremy Kerr1d640932006-06-19 20:33:19 +0200624static int spu_create_sysdev(struct spu *spu)
625{
626 int ret;
627
628 spu->sysdev.id = spu->number;
629 spu->sysdev.cls = &spu_sysdev_class;
630 ret = sysdev_register(&spu->sysdev);
631 if (ret) {
632 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
633 spu->number);
634 return ret;
635 }
636
Geoff Levand00215502006-11-20 18:45:02 +0100637 sysfs_add_device_to_node(&spu->sysdev, spu->node);
Jeremy Kerr1d640932006-06-19 20:33:19 +0200638
639 return 0;
640}
641
Geoff Levande28b0032006-11-23 00:46:49 +0100642static int __init create_spu(void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500643{
644 struct spu *spu;
645 int ret;
646 static int number;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100647 unsigned long flags;
Andre Detsch27ec41d2007-07-20 21:39:33 +0200648 struct timespec ts;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500649
650 ret = -ENOMEM;
Jeremy Kerrecec2172006-06-19 20:33:26 +0200651 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500652 if (!spu)
653 goto out;
654
Christoph Hellwig486acd42007-07-20 21:39:54 +0200655 spu->alloc_state = SPU_FREE;
656
Geoff Levande28b0032006-11-23 00:46:49 +0100657 spin_lock_init(&spu->register_lock);
Christoph Hellwig24140592007-07-20 21:39:51 +0200658 spin_lock(&spu_lock);
Geoff Levande28b0032006-11-23 00:46:49 +0100659 spu->number = number++;
Christoph Hellwig24140592007-07-20 21:39:51 +0200660 spin_unlock(&spu_lock);
Benjamin Herrenschmidte5267b42006-10-10 15:14:12 +1000661
Geoff Levande28b0032006-11-23 00:46:49 +0100662 ret = spu_create_spu(spu, data);
663
Arnd Bergmann67207b92005-11-15 15:53:48 -0500664 if (ret)
665 goto out_free;
666
Masato Noguchi24f43b32006-10-24 18:31:14 +0200667 spu_mfc_sdr_setup(spu);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100668 spu_mfc_sr1_set(spu, 0x33);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500669 ret = spu_request_irqs(spu);
670 if (ret)
Geoff Levande28b0032006-11-23 00:46:49 +0100671 goto out_destroy;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500672
Jeremy Kerr1d640932006-06-19 20:33:19 +0200673 ret = spu_create_sysdev(spu);
674 if (ret)
675 goto out_free_irqs;
676
Christoph Hellwig486acd42007-07-20 21:39:54 +0200677 mutex_lock(&cbe_spu_info[spu->node].list_mutex);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200678 list_add(&spu->cbe_list, &cbe_spu_info[spu->node].spus);
679 cbe_spu_info[spu->node].n_spus++;
Christoph Hellwig486acd42007-07-20 21:39:54 +0200680 mutex_unlock(&cbe_spu_info[spu->node].list_mutex);
Christoph Hellwig24140592007-07-20 21:39:51 +0200681
682 mutex_lock(&spu_full_list_mutex);
683 spin_lock_irqsave(&spu_full_list_lock, flags);
Christian Kraffte570beb2006-10-24 18:31:23 +0200684 list_add(&spu->full_list, &spu_full_list);
Christoph Hellwig24140592007-07-20 21:39:51 +0200685 spin_unlock_irqrestore(&spu_full_list_lock, flags);
686 mutex_unlock(&spu_full_list_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500687
Andre Detsch27ec41d2007-07-20 21:39:33 +0200688 spu->stats.util_state = SPU_UTIL_IDLE_LOADED;
689 ktime_get_ts(&ts);
690 spu->stats.tstamp = timespec_to_ns(&ts);
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000691
Arnd Bergmann9d92af62007-07-20 21:39:45 +0200692 INIT_LIST_HEAD(&spu->aff_list);
693
Arnd Bergmann67207b92005-11-15 15:53:48 -0500694 goto out;
695
Jeremy Kerr1d640932006-06-19 20:33:19 +0200696out_free_irqs:
697 spu_free_irqs(spu);
Geoff Levande28b0032006-11-23 00:46:49 +0100698out_destroy:
699 spu_destroy_spu(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500700out_free:
701 kfree(spu);
702out:
703 return ret;
704}
705
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000706static const char *spu_state_names[] = {
707 "user", "system", "iowait", "idle"
708};
709
710static unsigned long long spu_acct_time(struct spu *spu,
711 enum spu_utilization_state state)
712{
Andre Detsch27ec41d2007-07-20 21:39:33 +0200713 struct timespec ts;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000714 unsigned long long time = spu->stats.times[state];
715
Andre Detsch27ec41d2007-07-20 21:39:33 +0200716 /*
717 * If the spu is idle or the context is stopped, utilization
718 * statistics are not updated. Apply the time delta from the
719 * last recorded state of the spu.
720 */
721 if (spu->stats.util_state == state) {
722 ktime_get_ts(&ts);
723 time += timespec_to_ns(&ts) - spu->stats.tstamp;
724 }
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000725
Andre Detsch27ec41d2007-07-20 21:39:33 +0200726 return time / NSEC_PER_MSEC;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000727}
728
729
730static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf)
731{
732 struct spu *spu = container_of(sysdev, struct spu, sysdev);
733
734 return sprintf(buf, "%s %llu %llu %llu %llu "
735 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +0200736 spu_state_names[spu->stats.util_state],
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000737 spu_acct_time(spu, SPU_UTIL_USER),
738 spu_acct_time(spu, SPU_UTIL_SYSTEM),
739 spu_acct_time(spu, SPU_UTIL_IOWAIT),
Andre Detsch27ec41d2007-07-20 21:39:33 +0200740 spu_acct_time(spu, SPU_UTIL_IDLE_LOADED),
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000741 spu->stats.vol_ctx_switch,
742 spu->stats.invol_ctx_switch,
743 spu->stats.slb_flt,
744 spu->stats.hash_flt,
745 spu->stats.min_flt,
746 spu->stats.maj_flt,
747 spu->stats.class2_intr,
748 spu->stats.libassist);
749}
750
751static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL);
752
Arnd Bergmann67207b92005-11-15 15:53:48 -0500753static int __init init_spu_base(void)
754{
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200755 int i, ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500756
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200757 for (i = 0; i < MAX_NUMNODES; i++) {
Christoph Hellwig486acd42007-07-20 21:39:54 +0200758 mutex_init(&cbe_spu_info[i].list_mutex);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200759 INIT_LIST_HEAD(&cbe_spu_info[i].spus);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200760 }
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200761
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100762 if (!spu_management_ops)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200763 goto out;
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100764
Jeremy Kerr1d640932006-06-19 20:33:19 +0200765 /* create sysdev class for spus */
766 ret = sysdev_class_register(&spu_sysdev_class);
767 if (ret)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200768 goto out;
Jeremy Kerr1d640932006-06-19 20:33:19 +0200769
Geoff Levande28b0032006-11-23 00:46:49 +0100770 ret = spu_enumerate_spus(create_spu);
771
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700772 if (ret < 0) {
Geoff Levande28b0032006-11-23 00:46:49 +0100773 printk(KERN_WARNING "%s: Error initializing spus\n",
774 __FUNCTION__);
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200775 goto out_unregister_sysdev_class;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500776 }
Michael Ellermanff8a8f22006-10-24 18:31:27 +0200777
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700778 if (ret > 0) {
779 /*
780 * We cannot put the forward declaration in
781 * <linux/linux_logo.h> because of conflicting session type
782 * conflicts for const and __initdata with different compiler
783 * versions
784 */
785 extern const struct linux_logo logo_spe_clut224;
786
787 fb_append_extra_logo(&logo_spe_clut224, ret);
788 }
789
Christoph Hellwig24140592007-07-20 21:39:51 +0200790 mutex_lock(&spu_full_list_mutex);
Michael Ellermanff8a8f22006-10-24 18:31:27 +0200791 xmon_register_spus(&spu_full_list);
Andre Detsch8d2655e2007-07-20 21:39:27 +0200792 crash_register_spus(&spu_full_list);
Christoph Hellwig24140592007-07-20 21:39:51 +0200793 mutex_unlock(&spu_full_list_mutex);
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000794 spu_add_sysdev_attr(&attr_stat);
795
Andre Detschf5996442007-08-03 18:53:46 -0700796 spu_init_affinity();
Arnd Bergmann3ad216c2007-07-20 21:39:46 +0200797
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200798 return 0;
799
800 out_unregister_sysdev_class:
801 sysdev_class_unregister(&spu_sysdev_class);
802 out:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500803 return ret;
804}
805module_init(init_spu_base);
806
807MODULE_LICENSE("GPL");
808MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");