blob: caaf2bf78cad908a64d867ffb0dc6e2a58ffd20e [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 Bergmann67207b92005-11-15 15:53:48 -050038
Geoff Levande28b0032006-11-23 00:46:49 +010039const struct spu_management_ops *spu_management_ops;
Jeremy Kerrccf17e92007-04-23 21:08:29 +020040EXPORT_SYMBOL_GPL(spu_management_ops);
41
Geoff Levand540270d2006-06-19 20:33:29 +020042const struct spu_priv1_ops *spu_priv1_ops;
43
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010044static struct list_head spu_list[MAX_NUMNODES];
45static LIST_HEAD(spu_full_list);
46static DEFINE_MUTEX(spu_mutex);
Thomas Gleixner057b1842007-04-29 16:10:39 +000047static DEFINE_SPINLOCK(spu_list_lock);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010048
Geoff Levand540270d2006-06-19 20:33:29 +020049EXPORT_SYMBOL_GPL(spu_priv1_ops);
50
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010051void spu_invalidate_slbs(struct spu *spu)
52{
53 struct spu_priv2 __iomem *priv2 = spu->priv2;
54
55 if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
56 out_be64(&priv2->slb_invalidate_all_W, 0UL);
57}
58EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
59
60/* This is called by the MM core when a segment size is changed, to
61 * request a flush of all the SPEs using a given mm
62 */
63void spu_flush_all_slbs(struct mm_struct *mm)
64{
65 struct spu *spu;
66 unsigned long flags;
67
68 spin_lock_irqsave(&spu_list_lock, flags);
69 list_for_each_entry(spu, &spu_full_list, full_list) {
70 if (spu->mm == mm)
71 spu_invalidate_slbs(spu);
72 }
73 spin_unlock_irqrestore(&spu_list_lock, flags);
74}
75
76/* The hack below stinks... try to do something better one of
77 * these days... Does it even work properly with NR_CPUS == 1 ?
78 */
79static inline void mm_needs_global_tlbie(struct mm_struct *mm)
80{
81 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
82
83 /* Global TLBIE broadcast required with SPEs. */
84 __cpus_setall(&mm->cpu_vm_mask, nr);
85}
86
87void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
88{
89 unsigned long flags;
90
91 spin_lock_irqsave(&spu_list_lock, flags);
92 spu->mm = mm;
93 spin_unlock_irqrestore(&spu_list_lock, flags);
94 if (mm)
95 mm_needs_global_tlbie(mm);
96}
97EXPORT_SYMBOL_GPL(spu_associate_mm);
98
Arnd Bergmann67207b92005-11-15 15:53:48 -050099static int __spu_trap_invalid_dma(struct spu *spu)
100{
101 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200102 spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500103 return 0;
104}
105
106static int __spu_trap_dma_align(struct spu *spu)
107{
108 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200109 spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500110 return 0;
111}
112
113static int __spu_trap_error(struct spu *spu)
114{
115 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200116 spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500117 return 0;
118}
119
120static void spu_restart_dma(struct spu *spu)
121{
122 struct spu_priv2 __iomem *priv2 = spu->priv2;
Mark Nutter5473af02005-11-15 15:53:49 -0500123
Arnd Bergmann8837d922006-01-04 20:31:28 +0100124 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
Mark Nutter5473af02005-11-15 15:53:49 -0500125 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500126}
127
128static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
129{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500130 struct spu_priv2 __iomem *priv2 = spu->priv2;
131 struct mm_struct *mm = spu->mm;
arnd@arndb.de724bd802006-06-19 20:33:23 +0200132 u64 esid, vsid, llp;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100133 int psize;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500134
135 pr_debug("%s\n", __FUNCTION__);
136
Arnd Bergmann8837d922006-01-04 20:31:28 +0100137 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500138 /* SLBs are pre-loaded for context switch, so
139 * we should never get here!
140 */
Mark Nutter5473af02005-11-15 15:53:49 -0500141 printk("%s: invalid access during switch!\n", __func__);
142 return 1;
143 }
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200144 esid = (ea & ESID_MASK) | SLB_ESID_V;
145
146 switch(REGION_ID(ea)) {
147 case USER_REGION_ID:
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000148#ifdef CONFIG_PPC_MM_SLICES
149 psize = get_slice_psize(mm, ea);
150#else
151 psize = mm->context.user_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200152#endif
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200153 vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100154 SLB_VSID_USER;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200155 break;
156 case VMALLOC_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100157 if (ea < VMALLOC_END)
158 psize = mmu_vmalloc_psize;
159 else
160 psize = mmu_io_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200161 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100162 SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200163 break;
164 case KERNEL_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100165 psize = mmu_linear_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200166 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100167 SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200168 break;
169 default:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500170 /* Future: support kernel segments so that drivers
171 * can use SPUs.
172 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500173 pr_debug("invalid region access at %016lx\n", ea);
174 return 1;
175 }
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100176 llp = mmu_psize_defs[psize].sllp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500177
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500178 out_be64(&priv2->slb_index_W, spu->slb_replace);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100179 out_be64(&priv2->slb_vsid_RW, vsid | llp);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500180 out_be64(&priv2->slb_esid_RW, esid);
181
182 spu->slb_replace++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500183 if (spu->slb_replace >= 8)
184 spu->slb_replace = 0;
185
Arnd Bergmann67207b92005-11-15 15:53:48 -0500186 spu_restart_dma(spu);
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000187 spu->stats.slb_flt++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500188 return 0;
189}
190
Mark Nutter5473af02005-11-15 15:53:49 -0500191extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500192static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500193{
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100194 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500195
Mark Nutter5473af02005-11-15 15:53:49 -0500196 /* Handle kernel space hash faults immediately.
197 User hash faults need to be deferred to process context. */
198 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
199 && REGION_ID(ea) != USER_REGION_ID
200 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
201 spu_restart_dma(spu);
202 return 0;
203 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500204
Arnd Bergmann8837d922006-01-04 20:31:28 +0100205 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Mark Nutter5473af02005-11-15 15:53:49 -0500206 printk("%s: invalid access during switch!\n", __func__);
207 return 1;
208 }
209
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500210 spu->dar = ea;
211 spu->dsisr = dsisr;
212 mb();
Masato Noguchiba723fe22006-06-19 20:33:33 +0200213 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500214 return 0;
215}
216
217static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200218spu_irq_class_0(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500219{
220 struct spu *spu;
221
222 spu = data;
223 spu->class_0_pending = 1;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200224 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500225
226 return IRQ_HANDLED;
227}
228
Arnd Bergmann51104592005-12-05 22:52:25 -0500229int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500230spu_irq_class_0_bottom(struct spu *spu)
231{
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500232 unsigned long stat, mask;
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900233 unsigned long flags;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500234
235 spu->class_0_pending = 0;
236
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900237 spin_lock_irqsave(&spu->register_lock, flags);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100238 mask = spu_int_mask_get(spu, 0);
239 stat = spu_int_stat_get(spu, 0);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500240
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500241 stat &= mask;
242
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200243 if (stat & 1) /* invalid DMA alignment */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500244 __spu_trap_dma_align(spu);
245
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200246 if (stat & 2) /* invalid MFC DMA */
247 __spu_trap_invalid_dma(spu);
248
Arnd Bergmann67207b92005-11-15 15:53:48 -0500249 if (stat & 4) /* error on SPU */
250 __spu_trap_error(spu);
251
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100252 spu_int_stat_clear(spu, 0, stat);
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900253 spin_unlock_irqrestore(&spu->register_lock, flags);
Arnd Bergmann51104592005-12-05 22:52:25 -0500254
255 return (stat & 0x7) ? -EIO : 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500256}
Arnd Bergmann51104592005-12-05 22:52:25 -0500257EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500258
259static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200260spu_irq_class_1(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500261{
262 struct spu *spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500263 unsigned long stat, mask, dar, dsisr;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500264
265 spu = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500266
267 /* atomically read & clear class1 status. */
268 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100269 mask = spu_int_mask_get(spu, 1);
270 stat = spu_int_stat_get(spu, 1) & mask;
271 dar = spu_mfc_dar_get(spu);
272 dsisr = spu_mfc_dsisr_get(spu);
Arnd Bergmann38307342005-12-09 19:04:18 +0100273 if (stat & 2) /* mapping fault */
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100274 spu_mfc_dsisr_set(spu, 0ul);
275 spu_int_stat_clear(spu, 1, stat);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500276 spin_unlock(&spu->register_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100277 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
278 dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500279
280 if (stat & 1) /* segment fault */
281 __spu_trap_data_seg(spu, dar);
282
283 if (stat & 2) { /* mapping fault */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500284 __spu_trap_data_map(spu, dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500285 }
286
287 if (stat & 4) /* ls compare & suspend on get */
288 ;
289
290 if (stat & 8) /* ls compare & suspend on put */
291 ;
292
Arnd Bergmann67207b92005-11-15 15:53:48 -0500293 return stat ? IRQ_HANDLED : IRQ_NONE;
294}
295
296static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200297spu_irq_class_2(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500298{
299 struct spu *spu;
300 unsigned long stat;
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500301 unsigned long mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500302
303 spu = data;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200304 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100305 stat = spu_int_stat_get(spu, 2);
306 mask = spu_int_mask_get(spu, 2);
Masato Noguchiba723fe22006-06-19 20:33:33 +0200307 /* ignore interrupts we're not waiting for */
308 stat &= mask;
309 /*
310 * mailbox interrupts (0x1 and 0x10) are level triggered.
311 * mask them now before acknowledging.
312 */
313 if (stat & 0x11)
314 spu_int_mask_and(spu, 2, ~(stat & 0x11));
315 /* acknowledge all interrupts before the callbacks */
316 spu_int_stat_clear(spu, 2, stat);
317 spin_unlock(&spu->register_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500318
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500319 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500320
Arnd Bergmann67207b92005-11-15 15:53:48 -0500321 if (stat & 1) /* PPC core mailbox */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200322 spu->ibox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500323
324 if (stat & 2) /* SPU stop-and-signal */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200325 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500326
327 if (stat & 4) /* SPU halted */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200328 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500329
330 if (stat & 8) /* DMA tag group complete */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200331 spu->mfc_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500332
333 if (stat & 0x10) /* SPU mailbox threshold */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200334 spu->wbox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500335
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000336 spu->stats.class2_intr++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500337 return stat ? IRQ_HANDLED : IRQ_NONE;
338}
339
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000340static int spu_request_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500341{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000342 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500343
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000344 if (spu->irqs[0] != NO_IRQ) {
345 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
346 spu->number);
347 ret = request_irq(spu->irqs[0], spu_irq_class_0,
348 IRQF_DISABLED,
349 spu->irq_c0, spu);
350 if (ret)
351 goto bail0;
352 }
353 if (spu->irqs[1] != NO_IRQ) {
354 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
355 spu->number);
356 ret = request_irq(spu->irqs[1], spu_irq_class_1,
357 IRQF_DISABLED,
358 spu->irq_c1, spu);
359 if (ret)
360 goto bail1;
361 }
362 if (spu->irqs[2] != NO_IRQ) {
363 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
364 spu->number);
365 ret = request_irq(spu->irqs[2], spu_irq_class_2,
366 IRQF_DISABLED,
367 spu->irq_c2, spu);
368 if (ret)
369 goto bail2;
370 }
371 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500372
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000373bail2:
374 if (spu->irqs[1] != NO_IRQ)
375 free_irq(spu->irqs[1], spu);
376bail1:
377 if (spu->irqs[0] != NO_IRQ)
378 free_irq(spu->irqs[0], spu);
379bail0:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500380 return ret;
381}
382
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000383static void spu_free_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500384{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000385 if (spu->irqs[0] != NO_IRQ)
386 free_irq(spu->irqs[0], spu);
387 if (spu->irqs[1] != NO_IRQ)
388 free_irq(spu->irqs[1], spu);
389 if (spu->irqs[2] != NO_IRQ)
390 free_irq(spu->irqs[2], spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500391}
392
Arnd Bergmann67207b92005-11-15 15:53:48 -0500393static void spu_init_channels(struct spu *spu)
394{
395 static const struct {
396 unsigned channel;
397 unsigned count;
398 } zero_list[] = {
399 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
400 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
401 }, count_list[] = {
402 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
403 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
404 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
405 };
Arnd Bergmann6ff730c2006-01-04 20:31:31 +0100406 struct spu_priv2 __iomem *priv2;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500407 int i;
408
409 priv2 = spu->priv2;
410
411 /* initialize all channel data to zero */
412 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
413 int count;
414
415 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
416 for (count = 0; count < zero_list[i].count; count++)
417 out_be64(&priv2->spu_chnldata_RW, 0);
418 }
419
420 /* initialize channel counts to meaningful values */
421 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
422 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
423 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
424 }
425}
426
Mark Nuttera68cf982006-10-04 17:26:12 +0200427struct spu *spu_alloc_node(int node)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500428{
Mark Nuttera68cf982006-10-04 17:26:12 +0200429 struct spu *spu = NULL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500430
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800431 mutex_lock(&spu_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200432 if (!list_empty(&spu_list[node])) {
433 spu = list_entry(spu_list[node].next, struct spu, list);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500434 list_del_init(&spu->list);
Geoff Levandcc21a662006-10-24 18:31:15 +0200435 pr_debug("Got SPU %d %d\n", spu->number, spu->node);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500436 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800437 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500438
Christoph Hellwig62c05d52007-04-23 21:08:14 +0200439 if (spu)
440 spu_init_channels(spu);
Mark Nuttera68cf982006-10-04 17:26:12 +0200441 return spu;
442}
443EXPORT_SYMBOL_GPL(spu_alloc_node);
444
445struct spu *spu_alloc(void)
446{
447 struct spu *spu = NULL;
448 int node;
449
450 for (node = 0; node < MAX_NUMNODES; node++) {
451 spu = spu_alloc_node(node);
452 if (spu)
453 break;
454 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500455
456 return spu;
457}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500458
459void spu_free(struct spu *spu)
460{
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800461 mutex_lock(&spu_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200462 list_add_tail(&spu->list, &spu_list[spu->node]);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800463 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500464}
Arnd Bergmann39c73c32005-12-05 22:52:21 -0500465EXPORT_SYMBOL_GPL(spu_free);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500466
Geoff Levand6deac062007-06-16 07:17:32 +1000467static int spu_shutdown(struct sys_device *sysdev)
468{
469 struct spu *spu = container_of(sysdev, struct spu, sysdev);
470
471 spu_free_irqs(spu);
472 spu_destroy_spu(spu);
473 return 0;
474}
475
Jeremy Kerr1d640932006-06-19 20:33:19 +0200476struct sysdev_class spu_sysdev_class = {
Geoff Levand6deac062007-06-16 07:17:32 +1000477 set_kset_name("spu"),
478 .shutdown = spu_shutdown,
Jeremy Kerr1d640932006-06-19 20:33:19 +0200479};
480
Christian Kraffte570beb2006-10-24 18:31:23 +0200481int spu_add_sysdev_attr(struct sysdev_attribute *attr)
482{
483 struct spu *spu;
484 mutex_lock(&spu_mutex);
485
486 list_for_each_entry(spu, &spu_full_list, full_list)
487 sysdev_create_file(&spu->sysdev, attr);
488
489 mutex_unlock(&spu_mutex);
490 return 0;
491}
492EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
493
494int spu_add_sysdev_attr_group(struct attribute_group *attrs)
495{
496 struct spu *spu;
497 mutex_lock(&spu_mutex);
498
499 list_for_each_entry(spu, &spu_full_list, full_list)
500 sysfs_create_group(&spu->sysdev.kobj, attrs);
501
502 mutex_unlock(&spu_mutex);
503 return 0;
504}
505EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
506
507
508void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
509{
510 struct spu *spu;
511 mutex_lock(&spu_mutex);
512
513 list_for_each_entry(spu, &spu_full_list, full_list)
514 sysdev_remove_file(&spu->sysdev, attr);
515
516 mutex_unlock(&spu_mutex);
517}
518EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
519
520void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
521{
522 struct spu *spu;
523 mutex_lock(&spu_mutex);
524
525 list_for_each_entry(spu, &spu_full_list, full_list)
526 sysfs_remove_group(&spu->sysdev.kobj, attrs);
527
528 mutex_unlock(&spu_mutex);
529}
530EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
531
Jeremy Kerr1d640932006-06-19 20:33:19 +0200532static int spu_create_sysdev(struct spu *spu)
533{
534 int ret;
535
536 spu->sysdev.id = spu->number;
537 spu->sysdev.cls = &spu_sysdev_class;
538 ret = sysdev_register(&spu->sysdev);
539 if (ret) {
540 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
541 spu->number);
542 return ret;
543 }
544
Geoff Levand00215502006-11-20 18:45:02 +0100545 sysfs_add_device_to_node(&spu->sysdev, spu->node);
Jeremy Kerr1d640932006-06-19 20:33:19 +0200546
547 return 0;
548}
549
Geoff Levande28b0032006-11-23 00:46:49 +0100550static int __init create_spu(void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500551{
552 struct spu *spu;
553 int ret;
554 static int number;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100555 unsigned long flags;
Andre Detsch27ec41d2007-07-20 21:39:33 +0200556 struct timespec ts;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500557
558 ret = -ENOMEM;
Jeremy Kerrecec2172006-06-19 20:33:26 +0200559 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500560 if (!spu)
561 goto out;
562
Geoff Levande28b0032006-11-23 00:46:49 +0100563 spin_lock_init(&spu->register_lock);
564 mutex_lock(&spu_mutex);
565 spu->number = number++;
566 mutex_unlock(&spu_mutex);
Benjamin Herrenschmidte5267b42006-10-10 15:14:12 +1000567
Geoff Levande28b0032006-11-23 00:46:49 +0100568 ret = spu_create_spu(spu, data);
569
Arnd Bergmann67207b92005-11-15 15:53:48 -0500570 if (ret)
571 goto out_free;
572
Masato Noguchi24f43b32006-10-24 18:31:14 +0200573 spu_mfc_sdr_setup(spu);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100574 spu_mfc_sr1_set(spu, 0x33);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500575 ret = spu_request_irqs(spu);
576 if (ret)
Geoff Levande28b0032006-11-23 00:46:49 +0100577 goto out_destroy;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500578
Jeremy Kerr1d640932006-06-19 20:33:19 +0200579 ret = spu_create_sysdev(spu);
580 if (ret)
581 goto out_free_irqs;
582
Geoff Levande28b0032006-11-23 00:46:49 +0100583 mutex_lock(&spu_mutex);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100584 spin_lock_irqsave(&spu_list_lock, flags);
Mark Nuttera68cf982006-10-04 17:26:12 +0200585 list_add(&spu->list, &spu_list[spu->node]);
Christian Kraffte570beb2006-10-24 18:31:23 +0200586 list_add(&spu->full_list, &spu_full_list);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100587 spin_unlock_irqrestore(&spu_list_lock, flags);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800588 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500589
Andre Detsch27ec41d2007-07-20 21:39:33 +0200590 spu->stats.util_state = SPU_UTIL_IDLE_LOADED;
591 ktime_get_ts(&ts);
592 spu->stats.tstamp = timespec_to_ns(&ts);
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000593
Arnd Bergmann67207b92005-11-15 15:53:48 -0500594 goto out;
595
Jeremy Kerr1d640932006-06-19 20:33:19 +0200596out_free_irqs:
597 spu_free_irqs(spu);
Geoff Levande28b0032006-11-23 00:46:49 +0100598out_destroy:
599 spu_destroy_spu(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500600out_free:
601 kfree(spu);
602out:
603 return ret;
604}
605
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000606static const char *spu_state_names[] = {
607 "user", "system", "iowait", "idle"
608};
609
610static unsigned long long spu_acct_time(struct spu *spu,
611 enum spu_utilization_state state)
612{
Andre Detsch27ec41d2007-07-20 21:39:33 +0200613 struct timespec ts;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000614 unsigned long long time = spu->stats.times[state];
615
Andre Detsch27ec41d2007-07-20 21:39:33 +0200616 /*
617 * If the spu is idle or the context is stopped, utilization
618 * statistics are not updated. Apply the time delta from the
619 * last recorded state of the spu.
620 */
621 if (spu->stats.util_state == state) {
622 ktime_get_ts(&ts);
623 time += timespec_to_ns(&ts) - spu->stats.tstamp;
624 }
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000625
Andre Detsch27ec41d2007-07-20 21:39:33 +0200626 return time / NSEC_PER_MSEC;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000627}
628
629
630static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf)
631{
632 struct spu *spu = container_of(sysdev, struct spu, sysdev);
633
634 return sprintf(buf, "%s %llu %llu %llu %llu "
635 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +0200636 spu_state_names[spu->stats.util_state],
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000637 spu_acct_time(spu, SPU_UTIL_USER),
638 spu_acct_time(spu, SPU_UTIL_SYSTEM),
639 spu_acct_time(spu, SPU_UTIL_IOWAIT),
Andre Detsch27ec41d2007-07-20 21:39:33 +0200640 spu_acct_time(spu, SPU_UTIL_IDLE_LOADED),
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000641 spu->stats.vol_ctx_switch,
642 spu->stats.invol_ctx_switch,
643 spu->stats.slb_flt,
644 spu->stats.hash_flt,
645 spu->stats.min_flt,
646 spu->stats.maj_flt,
647 spu->stats.class2_intr,
648 spu->stats.libassist);
649}
650
651static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL);
652
Arnd Bergmann67207b92005-11-15 15:53:48 -0500653static int __init init_spu_base(void)
654{
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200655 int i, ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500656
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200657 for (i = 0; i < MAX_NUMNODES; i++)
658 INIT_LIST_HEAD(&spu_list[i]);
659
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100660 if (!spu_management_ops)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200661 goto out;
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100662
Jeremy Kerr1d640932006-06-19 20:33:19 +0200663 /* create sysdev class for spus */
664 ret = sysdev_class_register(&spu_sysdev_class);
665 if (ret)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200666 goto out;
Jeremy Kerr1d640932006-06-19 20:33:19 +0200667
Geoff Levande28b0032006-11-23 00:46:49 +0100668 ret = spu_enumerate_spus(create_spu);
669
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700670 if (ret < 0) {
Geoff Levande28b0032006-11-23 00:46:49 +0100671 printk(KERN_WARNING "%s: Error initializing spus\n",
672 __FUNCTION__);
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200673 goto out_unregister_sysdev_class;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500674 }
Michael Ellermanff8a8f22006-10-24 18:31:27 +0200675
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700676 if (ret > 0) {
677 /*
678 * We cannot put the forward declaration in
679 * <linux/linux_logo.h> because of conflicting session type
680 * conflicts for const and __initdata with different compiler
681 * versions
682 */
683 extern const struct linux_logo logo_spe_clut224;
684
685 fb_append_extra_logo(&logo_spe_clut224, ret);
686 }
687
Michael Ellermanff8a8f22006-10-24 18:31:27 +0200688 xmon_register_spus(&spu_full_list);
Andre Detsch8d2655e2007-07-20 21:39:27 +0200689 crash_register_spus(&spu_full_list);
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000690 spu_add_sysdev_attr(&attr_stat);
691
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200692 return 0;
693
694 out_unregister_sysdev_class:
695 sysdev_class_unregister(&spu_sysdev_class);
696 out:
697
Arnd Bergmann67207b92005-11-15 15:53:48 -0500698 return ret;
699}
700module_init(init_spu_base);
701
702MODULE_LICENSE("GPL");
703MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");