blob: cfc28e93c825cb997054ce514f3abbd1d10783ce [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 void spu_restart_dma(struct spu *spu)
136{
137 struct spu_priv2 __iomem *priv2 = spu->priv2;
Mark Nutter5473af02005-11-15 15:53:49 -0500138
Arnd Bergmann8837d922006-01-04 20:31:28 +0100139 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
Mark Nutter5473af02005-11-15 15:53:49 -0500140 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500141}
142
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100143static inline void spu_load_slb(struct spu *spu, int slbe, struct spu_slb *slb)
144{
145 struct spu_priv2 __iomem *priv2 = spu->priv2;
146
147 pr_debug("%s: adding SLB[%d] 0x%016lx 0x%016lx\n",
148 __func__, slbe, slb->vsid, slb->esid);
149
150 out_be64(&priv2->slb_index_W, slbe);
Arnd Bergmanncc4b7c12008-02-26 07:01:56 +0100151 /* set invalid before writing vsid */
152 out_be64(&priv2->slb_esid_RW, 0);
153 /* now it's safe to write the vsid */
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100154 out_be64(&priv2->slb_vsid_RW, slb->vsid);
Arnd Bergmanncc4b7c12008-02-26 07:01:56 +0100155 /* setting the new esid makes the entry valid again */
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100156 out_be64(&priv2->slb_esid_RW, slb->esid);
157}
158
Arnd Bergmann67207b92005-11-15 15:53:48 -0500159static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
160{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500161 struct mm_struct *mm = spu->mm;
Jeremy Kerr4d434662007-12-05 13:49:31 +1100162 struct spu_slb slb;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100163 int psize;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500164
165 pr_debug("%s\n", __FUNCTION__);
166
Jeremy Kerr4d434662007-12-05 13:49:31 +1100167 slb.esid = (ea & ESID_MASK) | SLB_ESID_V;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200168
169 switch(REGION_ID(ea)) {
170 case USER_REGION_ID:
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000171#ifdef CONFIG_PPC_MM_SLICES
172 psize = get_slice_psize(mm, ea);
173#else
174 psize = mm->context.user_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200175#endif
Jeremy Kerr4d434662007-12-05 13:49:31 +1100176 slb.vsid = (get_vsid(mm->context.id, ea, MMU_SEGSIZE_256M)
177 << SLB_VSID_SHIFT) | SLB_VSID_USER;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200178 break;
179 case VMALLOC_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100180 if (ea < VMALLOC_END)
181 psize = mmu_vmalloc_psize;
182 else
183 psize = mmu_io_psize;
Jeremy Kerr4d434662007-12-05 13:49:31 +1100184 slb.vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M)
185 << SLB_VSID_SHIFT) | SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200186 break;
187 case KERNEL_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100188 psize = mmu_linear_psize;
Jeremy Kerr4d434662007-12-05 13:49:31 +1100189 slb.vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M)
190 << SLB_VSID_SHIFT) | SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200191 break;
192 default:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500193 /* Future: support kernel segments so that drivers
194 * can use SPUs.
195 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500196 pr_debug("invalid region access at %016lx\n", ea);
197 return 1;
198 }
Jeremy Kerr4d434662007-12-05 13:49:31 +1100199 slb.vsid |= mmu_psize_defs[psize].sllp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500200
Jeremy Kerr4d434662007-12-05 13:49:31 +1100201 spu_load_slb(spu, spu->slb_replace, &slb);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500202
203 spu->slb_replace++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500204 if (spu->slb_replace >= 8)
205 spu->slb_replace = 0;
206
Arnd Bergmann67207b92005-11-15 15:53:48 -0500207 spu_restart_dma(spu);
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000208 spu->stats.slb_flt++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500209 return 0;
210}
211
Mark Nutter5473af02005-11-15 15:53:49 -0500212extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500213static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500214{
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100215 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500216
Mark Nutter5473af02005-11-15 15:53:49 -0500217 /* Handle kernel space hash faults immediately.
218 User hash faults need to be deferred to process context. */
219 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
220 && REGION_ID(ea) != USER_REGION_ID
221 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
222 spu_restart_dma(spu);
223 return 0;
224 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500225
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +0900226 spu->class_0_pending = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500227 spu->dar = ea;
228 spu->dsisr = dsisr;
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +0900229
Masato Noguchiba723fe22006-06-19 20:33:33 +0200230 spu->stop_callback(spu);
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +0900231
Arnd Bergmann67207b92005-11-15 15:53:48 -0500232 return 0;
233}
234
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100235static void __spu_kernel_slb(void *addr, struct spu_slb *slb)
236{
237 unsigned long ea = (unsigned long)addr;
238 u64 llp;
239
240 if (REGION_ID(ea) == KERNEL_REGION_ID)
241 llp = mmu_psize_defs[mmu_linear_psize].sllp;
242 else
243 llp = mmu_psize_defs[mmu_virtual_psize].sllp;
244
245 slb->vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M) << SLB_VSID_SHIFT) |
246 SLB_VSID_KERNEL | llp;
247 slb->esid = (ea & ESID_MASK) | SLB_ESID_V;
248}
249
250/**
Jeremy Kerr684bd612007-12-05 13:49:31 +1100251 * Given an array of @nr_slbs SLB entries, @slbs, return non-zero if the
252 * address @new_addr is present.
253 */
254static inline int __slb_present(struct spu_slb *slbs, int nr_slbs,
255 void *new_addr)
256{
257 unsigned long ea = (unsigned long)new_addr;
258 int i;
259
260 for (i = 0; i < nr_slbs; i++)
261 if (!((slbs[i].esid ^ ea) & ESID_MASK))
262 return 1;
263
264 return 0;
265}
266
267/**
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100268 * Setup the SPU kernel SLBs, in preparation for a context save/restore. We
269 * need to map both the context save area, and the save/restore code.
Jeremy Kerr684bd612007-12-05 13:49:31 +1100270 *
271 * Because the lscsa and code may cross segment boundaires, we check to see
272 * if mappings are required for the start and end of each range. We currently
273 * assume that the mappings are smaller that one segment - if not, something
274 * is seriously wrong.
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100275 */
Jeremy Kerr684bd612007-12-05 13:49:31 +1100276void spu_setup_kernel_slbs(struct spu *spu, struct spu_lscsa *lscsa,
277 void *code, int code_size)
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100278{
Jeremy Kerr684bd612007-12-05 13:49:31 +1100279 struct spu_slb slbs[4];
280 int i, nr_slbs = 0;
281 /* start and end addresses of both mappings */
282 void *addrs[] = {
283 lscsa, (void *)lscsa + sizeof(*lscsa) - 1,
284 code, code + code_size - 1
285 };
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100286
Jeremy Kerr684bd612007-12-05 13:49:31 +1100287 /* check the set of addresses, and create a new entry in the slbs array
288 * if there isn't already a SLB for that address */
289 for (i = 0; i < ARRAY_SIZE(addrs); i++) {
290 if (__slb_present(slbs, nr_slbs, addrs[i]))
291 continue;
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100292
Jeremy Kerr684bd612007-12-05 13:49:31 +1100293 __spu_kernel_slb(addrs[i], &slbs[nr_slbs]);
294 nr_slbs++;
295 }
296
297 /* Add the set of SLBs */
298 for (i = 0; i < nr_slbs; i++)
299 spu_load_slb(spu, i, &slbs[i]);
Jeremy Kerr58bd4032007-12-05 13:49:31 +1100300}
301EXPORT_SYMBOL_GPL(spu_setup_kernel_slbs);
302
Arnd Bergmann67207b92005-11-15 15:53:48 -0500303static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200304spu_irq_class_0(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500305{
306 struct spu *spu;
Masato Noguchib7f90a42007-09-07 18:28:27 +1000307 unsigned long stat, mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500308
309 spu = data;
Masato Noguchib7f90a42007-09-07 18:28:27 +1000310
Masato Noguchib7f90a42007-09-07 18:28:27 +1000311 spin_lock(&spu->register_lock);
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +0900312 mask = spu_int_mask_get(spu, 0);
313 stat = spu_int_stat_get(spu, 0) & mask;
314
Masato Noguchib7f90a42007-09-07 18:28:27 +1000315 spu->class_0_pending |= stat;
Jeremy Kerrd6ad39b2007-12-20 16:39:59 +0900316 spu->dsisr = spu_mfc_dsisr_get(spu);
317 spu->dar = spu_mfc_dar_get(spu);
Masato Noguchib7f90a42007-09-07 18:28:27 +1000318 spin_unlock(&spu->register_lock);
319
Masato Noguchiba723fe22006-06-19 20:33:33 +0200320 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500321
Masato Noguchib7f90a42007-09-07 18:28:27 +1000322 spu_int_stat_clear(spu, 0, stat);
323
Arnd Bergmann67207b92005-11-15 15:53:48 -0500324 return IRQ_HANDLED;
325}
326
Arnd Bergmann67207b92005-11-15 15:53:48 -0500327static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200328spu_irq_class_1(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500329{
330 struct spu *spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500331 unsigned long stat, mask, dar, dsisr;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500332
333 spu = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500334
335 /* atomically read & clear class1 status. */
336 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100337 mask = spu_int_mask_get(spu, 1);
338 stat = spu_int_stat_get(spu, 1) & mask;
339 dar = spu_mfc_dar_get(spu);
340 dsisr = spu_mfc_dsisr_get(spu);
Jeremy Kerr8af30672007-12-20 16:39:59 +0900341 if (stat & CLASS1_STORAGE_FAULT_INTR)
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100342 spu_mfc_dsisr_set(spu, 0ul);
343 spu_int_stat_clear(spu, 1, stat);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500344 spin_unlock(&spu->register_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100345 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
346 dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500347
Jeremy Kerr8af30672007-12-20 16:39:59 +0900348 if (stat & CLASS1_SEGMENT_FAULT_INTR)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500349 __spu_trap_data_seg(spu, dar);
350
Jeremy Kerr8af30672007-12-20 16:39:59 +0900351 if (stat & CLASS1_STORAGE_FAULT_INTR)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500352 __spu_trap_data_map(spu, dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500353
Jeremy Kerr8af30672007-12-20 16:39:59 +0900354 if (stat & CLASS1_LS_COMPARE_SUSPEND_ON_GET_INTR)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500355 ;
356
Jeremy Kerr8af30672007-12-20 16:39:59 +0900357 if (stat & CLASS1_LS_COMPARE_SUSPEND_ON_PUT_INTR)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500358 ;
359
Arnd Bergmann67207b92005-11-15 15:53:48 -0500360 return stat ? IRQ_HANDLED : IRQ_NONE;
361}
362
363static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200364spu_irq_class_2(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500365{
366 struct spu *spu;
367 unsigned long stat;
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500368 unsigned long mask;
Jeremy Kerr8af30672007-12-20 16:39:59 +0900369 const int mailbox_intrs =
370 CLASS2_MAILBOX_THRESHOLD_INTR | CLASS2_MAILBOX_INTR;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500371
372 spu = data;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200373 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100374 stat = spu_int_stat_get(spu, 2);
375 mask = spu_int_mask_get(spu, 2);
Masato Noguchiba723fe22006-06-19 20:33:33 +0200376 /* ignore interrupts we're not waiting for */
377 stat &= mask;
Jeremy Kerr8af30672007-12-20 16:39:59 +0900378
379 /* mailbox interrupts are level triggered. mask them now before
380 * acknowledging */
381 if (stat & mailbox_intrs)
382 spu_int_mask_and(spu, 2, ~(stat & mailbox_intrs));
Masato Noguchiba723fe22006-06-19 20:33:33 +0200383 /* acknowledge all interrupts before the callbacks */
384 spu_int_stat_clear(spu, 2, stat);
385 spin_unlock(&spu->register_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500386
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500387 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500388
Jeremy Kerr8af30672007-12-20 16:39:59 +0900389 if (stat & CLASS2_MAILBOX_INTR)
Masato Noguchiba723fe22006-06-19 20:33:33 +0200390 spu->ibox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500391
Jeremy Kerr8af30672007-12-20 16:39:59 +0900392 if (stat & CLASS2_SPU_STOP_INTR)
Masato Noguchiba723fe22006-06-19 20:33:33 +0200393 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500394
Jeremy Kerr8af30672007-12-20 16:39:59 +0900395 if (stat & CLASS2_SPU_HALT_INTR)
Masato Noguchiba723fe22006-06-19 20:33:33 +0200396 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500397
Jeremy Kerr8af30672007-12-20 16:39:59 +0900398 if (stat & CLASS2_SPU_DMA_TAG_GROUP_COMPLETE_INTR)
Masato Noguchiba723fe22006-06-19 20:33:33 +0200399 spu->mfc_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500400
Jeremy Kerr8af30672007-12-20 16:39:59 +0900401 if (stat & CLASS2_MAILBOX_THRESHOLD_INTR)
Masato Noguchiba723fe22006-06-19 20:33:33 +0200402 spu->wbox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500403
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000404 spu->stats.class2_intr++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500405 return stat ? IRQ_HANDLED : IRQ_NONE;
406}
407
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000408static int spu_request_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500409{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000410 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500411
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000412 if (spu->irqs[0] != NO_IRQ) {
413 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
414 spu->number);
415 ret = request_irq(spu->irqs[0], spu_irq_class_0,
416 IRQF_DISABLED,
417 spu->irq_c0, spu);
418 if (ret)
419 goto bail0;
420 }
421 if (spu->irqs[1] != NO_IRQ) {
422 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
423 spu->number);
424 ret = request_irq(spu->irqs[1], spu_irq_class_1,
425 IRQF_DISABLED,
426 spu->irq_c1, spu);
427 if (ret)
428 goto bail1;
429 }
430 if (spu->irqs[2] != NO_IRQ) {
431 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
432 spu->number);
433 ret = request_irq(spu->irqs[2], spu_irq_class_2,
434 IRQF_DISABLED,
435 spu->irq_c2, spu);
436 if (ret)
437 goto bail2;
438 }
439 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500440
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000441bail2:
442 if (spu->irqs[1] != NO_IRQ)
443 free_irq(spu->irqs[1], spu);
444bail1:
445 if (spu->irqs[0] != NO_IRQ)
446 free_irq(spu->irqs[0], spu);
447bail0:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500448 return ret;
449}
450
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000451static void spu_free_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500452{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000453 if (spu->irqs[0] != NO_IRQ)
454 free_irq(spu->irqs[0], spu);
455 if (spu->irqs[1] != NO_IRQ)
456 free_irq(spu->irqs[1], spu);
457 if (spu->irqs[2] != NO_IRQ)
458 free_irq(spu->irqs[2], spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500459}
460
Christoph Hellwig486acd42007-07-20 21:39:54 +0200461void spu_init_channels(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500462{
463 static const struct {
464 unsigned channel;
465 unsigned count;
466 } zero_list[] = {
467 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
468 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
469 }, count_list[] = {
470 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
471 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
472 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
473 };
Arnd Bergmann6ff730c2006-01-04 20:31:31 +0100474 struct spu_priv2 __iomem *priv2;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500475 int i;
476
477 priv2 = spu->priv2;
478
479 /* initialize all channel data to zero */
480 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
481 int count;
482
483 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
484 for (count = 0; count < zero_list[i].count; count++)
485 out_be64(&priv2->spu_chnldata_RW, 0);
486 }
487
488 /* initialize channel counts to meaningful values */
489 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
490 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
491 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
492 }
493}
Christoph Hellwig486acd42007-07-20 21:39:54 +0200494EXPORT_SYMBOL_GPL(spu_init_channels);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500495
Geoff Levand6deac062007-06-16 07:17:32 +1000496static int spu_shutdown(struct sys_device *sysdev)
497{
498 struct spu *spu = container_of(sysdev, struct spu, sysdev);
499
500 spu_free_irqs(spu);
501 spu_destroy_spu(spu);
502 return 0;
503}
504
Sebastian Siewior12388192007-09-19 14:38:12 +1000505static struct sysdev_class spu_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100506 .name = "spu",
Geoff Levand6deac062007-06-16 07:17:32 +1000507 .shutdown = spu_shutdown,
Jeremy Kerr1d640932006-06-19 20:33:19 +0200508};
509
Christian Kraffte570beb2006-10-24 18:31:23 +0200510int spu_add_sysdev_attr(struct sysdev_attribute *attr)
511{
512 struct spu *spu;
Christian Kraffte570beb2006-10-24 18:31:23 +0200513
Christoph Hellwig24140592007-07-20 21:39:51 +0200514 mutex_lock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200515 list_for_each_entry(spu, &spu_full_list, full_list)
516 sysdev_create_file(&spu->sysdev, attr);
Christoph Hellwig24140592007-07-20 21:39:51 +0200517 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200518
Christian Kraffte570beb2006-10-24 18:31:23 +0200519 return 0;
520}
521EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
522
523int spu_add_sysdev_attr_group(struct attribute_group *attrs)
524{
525 struct spu *spu;
Jeremy Kerr1e771032007-12-05 13:49:31 +1100526 int rc = 0;
Christian Kraffte570beb2006-10-24 18:31:23 +0200527
Christoph Hellwig24140592007-07-20 21:39:51 +0200528 mutex_lock(&spu_full_list_mutex);
Jeremy Kerr1e771032007-12-05 13:49:31 +1100529 list_for_each_entry(spu, &spu_full_list, full_list) {
530 rc = sysfs_create_group(&spu->sysdev.kobj, attrs);
531
532 /* we're in trouble here, but try unwinding anyway */
533 if (rc) {
534 printk(KERN_ERR "%s: can't create sysfs group '%s'\n",
535 __func__, attrs->name);
536
537 list_for_each_entry_continue_reverse(spu,
538 &spu_full_list, full_list)
539 sysfs_remove_group(&spu->sysdev.kobj, attrs);
540 break;
541 }
542 }
543
Christoph Hellwig24140592007-07-20 21:39:51 +0200544 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200545
Jeremy Kerr1e771032007-12-05 13:49:31 +1100546 return rc;
Christian Kraffte570beb2006-10-24 18:31:23 +0200547}
548EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
549
550
551void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
552{
553 struct spu *spu;
Christian Kraffte570beb2006-10-24 18:31:23 +0200554
Christoph Hellwig24140592007-07-20 21:39:51 +0200555 mutex_lock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200556 list_for_each_entry(spu, &spu_full_list, full_list)
557 sysdev_remove_file(&spu->sysdev, attr);
Christoph Hellwig24140592007-07-20 21:39:51 +0200558 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200559}
560EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
561
562void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
563{
564 struct spu *spu;
Christian Kraffte570beb2006-10-24 18:31:23 +0200565
Christoph Hellwig24140592007-07-20 21:39:51 +0200566 mutex_lock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200567 list_for_each_entry(spu, &spu_full_list, full_list)
568 sysfs_remove_group(&spu->sysdev.kobj, attrs);
Christoph Hellwig24140592007-07-20 21:39:51 +0200569 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200570}
571EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
572
Jeremy Kerr1d640932006-06-19 20:33:19 +0200573static int spu_create_sysdev(struct spu *spu)
574{
575 int ret;
576
577 spu->sysdev.id = spu->number;
578 spu->sysdev.cls = &spu_sysdev_class;
579 ret = sysdev_register(&spu->sysdev);
580 if (ret) {
581 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
582 spu->number);
583 return ret;
584 }
585
Geoff Levand00215502006-11-20 18:45:02 +0100586 sysfs_add_device_to_node(&spu->sysdev, spu->node);
Jeremy Kerr1d640932006-06-19 20:33:19 +0200587
588 return 0;
589}
590
Geoff Levande28b0032006-11-23 00:46:49 +0100591static int __init create_spu(void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500592{
593 struct spu *spu;
594 int ret;
595 static int number;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100596 unsigned long flags;
Andre Detsch27ec41d2007-07-20 21:39:33 +0200597 struct timespec ts;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500598
599 ret = -ENOMEM;
Jeremy Kerrecec2172006-06-19 20:33:26 +0200600 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500601 if (!spu)
602 goto out;
603
Christoph Hellwig486acd42007-07-20 21:39:54 +0200604 spu->alloc_state = SPU_FREE;
605
Geoff Levande28b0032006-11-23 00:46:49 +0100606 spin_lock_init(&spu->register_lock);
Christoph Hellwig24140592007-07-20 21:39:51 +0200607 spin_lock(&spu_lock);
Geoff Levande28b0032006-11-23 00:46:49 +0100608 spu->number = number++;
Christoph Hellwig24140592007-07-20 21:39:51 +0200609 spin_unlock(&spu_lock);
Benjamin Herrenschmidte5267b42006-10-10 15:14:12 +1000610
Geoff Levande28b0032006-11-23 00:46:49 +0100611 ret = spu_create_spu(spu, data);
612
Arnd Bergmann67207b92005-11-15 15:53:48 -0500613 if (ret)
614 goto out_free;
615
Masato Noguchi24f43b32006-10-24 18:31:14 +0200616 spu_mfc_sdr_setup(spu);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100617 spu_mfc_sr1_set(spu, 0x33);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500618 ret = spu_request_irqs(spu);
619 if (ret)
Geoff Levande28b0032006-11-23 00:46:49 +0100620 goto out_destroy;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500621
Jeremy Kerr1d640932006-06-19 20:33:19 +0200622 ret = spu_create_sysdev(spu);
623 if (ret)
624 goto out_free_irqs;
625
Christoph Hellwig486acd42007-07-20 21:39:54 +0200626 mutex_lock(&cbe_spu_info[spu->node].list_mutex);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200627 list_add(&spu->cbe_list, &cbe_spu_info[spu->node].spus);
628 cbe_spu_info[spu->node].n_spus++;
Christoph Hellwig486acd42007-07-20 21:39:54 +0200629 mutex_unlock(&cbe_spu_info[spu->node].list_mutex);
Christoph Hellwig24140592007-07-20 21:39:51 +0200630
631 mutex_lock(&spu_full_list_mutex);
632 spin_lock_irqsave(&spu_full_list_lock, flags);
Christian Kraffte570beb2006-10-24 18:31:23 +0200633 list_add(&spu->full_list, &spu_full_list);
Christoph Hellwig24140592007-07-20 21:39:51 +0200634 spin_unlock_irqrestore(&spu_full_list_lock, flags);
635 mutex_unlock(&spu_full_list_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500636
Andre Detsch27ec41d2007-07-20 21:39:33 +0200637 spu->stats.util_state = SPU_UTIL_IDLE_LOADED;
638 ktime_get_ts(&ts);
639 spu->stats.tstamp = timespec_to_ns(&ts);
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000640
Arnd Bergmann9d92af62007-07-20 21:39:45 +0200641 INIT_LIST_HEAD(&spu->aff_list);
642
Arnd Bergmann67207b92005-11-15 15:53:48 -0500643 goto out;
644
Jeremy Kerr1d640932006-06-19 20:33:19 +0200645out_free_irqs:
646 spu_free_irqs(spu);
Geoff Levande28b0032006-11-23 00:46:49 +0100647out_destroy:
648 spu_destroy_spu(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500649out_free:
650 kfree(spu);
651out:
652 return ret;
653}
654
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000655static const char *spu_state_names[] = {
656 "user", "system", "iowait", "idle"
657};
658
659static unsigned long long spu_acct_time(struct spu *spu,
660 enum spu_utilization_state state)
661{
Andre Detsch27ec41d2007-07-20 21:39:33 +0200662 struct timespec ts;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000663 unsigned long long time = spu->stats.times[state];
664
Andre Detsch27ec41d2007-07-20 21:39:33 +0200665 /*
666 * If the spu is idle or the context is stopped, utilization
667 * statistics are not updated. Apply the time delta from the
668 * last recorded state of the spu.
669 */
670 if (spu->stats.util_state == state) {
671 ktime_get_ts(&ts);
672 time += timespec_to_ns(&ts) - spu->stats.tstamp;
673 }
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000674
Andre Detsch27ec41d2007-07-20 21:39:33 +0200675 return time / NSEC_PER_MSEC;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000676}
677
678
679static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf)
680{
681 struct spu *spu = container_of(sysdev, struct spu, sysdev);
682
683 return sprintf(buf, "%s %llu %llu %llu %llu "
684 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +0200685 spu_state_names[spu->stats.util_state],
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000686 spu_acct_time(spu, SPU_UTIL_USER),
687 spu_acct_time(spu, SPU_UTIL_SYSTEM),
688 spu_acct_time(spu, SPU_UTIL_IOWAIT),
Andre Detsch27ec41d2007-07-20 21:39:33 +0200689 spu_acct_time(spu, SPU_UTIL_IDLE_LOADED),
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000690 spu->stats.vol_ctx_switch,
691 spu->stats.invol_ctx_switch,
692 spu->stats.slb_flt,
693 spu->stats.hash_flt,
694 spu->stats.min_flt,
695 spu->stats.maj_flt,
696 spu->stats.class2_intr,
697 spu->stats.libassist);
698}
699
700static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL);
701
Arnd Bergmann67207b92005-11-15 15:53:48 -0500702static int __init init_spu_base(void)
703{
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200704 int i, ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500705
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200706 for (i = 0; i < MAX_NUMNODES; i++) {
Christoph Hellwig486acd42007-07-20 21:39:54 +0200707 mutex_init(&cbe_spu_info[i].list_mutex);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200708 INIT_LIST_HEAD(&cbe_spu_info[i].spus);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200709 }
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200710
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100711 if (!spu_management_ops)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200712 goto out;
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100713
Jeremy Kerr1d640932006-06-19 20:33:19 +0200714 /* create sysdev class for spus */
715 ret = sysdev_class_register(&spu_sysdev_class);
716 if (ret)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200717 goto out;
Jeremy Kerr1d640932006-06-19 20:33:19 +0200718
Geoff Levande28b0032006-11-23 00:46:49 +0100719 ret = spu_enumerate_spus(create_spu);
720
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700721 if (ret < 0) {
Geoff Levande28b0032006-11-23 00:46:49 +0100722 printk(KERN_WARNING "%s: Error initializing spus\n",
723 __FUNCTION__);
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200724 goto out_unregister_sysdev_class;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500725 }
Michael Ellermanff8a8f22006-10-24 18:31:27 +0200726
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700727 if (ret > 0) {
728 /*
729 * We cannot put the forward declaration in
730 * <linux/linux_logo.h> because of conflicting session type
731 * conflicts for const and __initdata with different compiler
732 * versions
733 */
734 extern const struct linux_logo logo_spe_clut224;
735
736 fb_append_extra_logo(&logo_spe_clut224, ret);
737 }
738
Christoph Hellwig24140592007-07-20 21:39:51 +0200739 mutex_lock(&spu_full_list_mutex);
Michael Ellermanff8a8f22006-10-24 18:31:27 +0200740 xmon_register_spus(&spu_full_list);
Andre Detsch8d2655e2007-07-20 21:39:27 +0200741 crash_register_spus(&spu_full_list);
Christoph Hellwig24140592007-07-20 21:39:51 +0200742 mutex_unlock(&spu_full_list_mutex);
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000743 spu_add_sysdev_attr(&attr_stat);
744
Andre Detschf5996442007-08-03 18:53:46 -0700745 spu_init_affinity();
Arnd Bergmann3ad216c2007-07-20 21:39:46 +0200746
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200747 return 0;
748
749 out_unregister_sysdev_class:
750 sysdev_class_unregister(&spu_sysdev_class);
751 out:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500752 return ret;
753}
754module_init(init_spu_base);
755
756MODULE_LICENSE("GPL");
757MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");