blob: 90124228b8f43c37bd02f06e927402d29a5c058c [file] [log] [blame]
Arnd Bergmann67207b92005-11-15 15:53:48 -05001/*
2 * Low-level SPU handling
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 *
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
Arnd Bergmann3b3d22c2005-12-05 22:52:24 -050023#undef DEBUG
Arnd Bergmann67207b92005-11-15 15:53:48 -050024
25#include <linux/interrupt.h>
26#include <linux/list.h>
27#include <linux/module.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050028#include <linux/ptrace.h>
29#include <linux/slab.h>
30#include <linux/wait.h>
Geoff Levande28b0032006-11-23 00:46:49 +010031#include <linux/mm.h>
32#include <linux/io.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080033#include <linux/mutex.h>
Geert Uytterhoevenbce94512007-07-17 04:05:52 -070034#include <linux/linux_logo.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050035#include <asm/spu.h>
Geoff Levand540270d2006-06-19 20:33:29 +020036#include <asm/spu_priv1.h>
Michael Ellermanff8a8f22006-10-24 18:31:27 +020037#include <asm/xmon.h>
Arnd Bergmann3ad216c2007-07-20 21:39:46 +020038#include <asm/prom.h>
39#include "spu_priv1_mmio.h"
Arnd Bergmann67207b92005-11-15 15:53:48 -050040
Geoff Levande28b0032006-11-23 00:46:49 +010041const struct spu_management_ops *spu_management_ops;
Jeremy Kerrccf17e92007-04-23 21:08:29 +020042EXPORT_SYMBOL_GPL(spu_management_ops);
43
Geoff Levand540270d2006-06-19 20:33:29 +020044const struct spu_priv1_ops *spu_priv1_ops;
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/*
51 * Protects cbe_spu_info and spu->number.
52 */
53static DEFINE_SPINLOCK(spu_lock);
54
55/*
56 * List of all spus in the system.
57 *
58 * This list is iterated by callers from irq context and callers that
59 * want to sleep. Thus modifications need to be done with both
60 * spu_full_list_lock and spu_full_list_mutex held, while iterating
61 * through it requires either of these locks.
62 *
63 * In addition spu_full_list_lock protects all assignmens to
64 * spu->mm.
65 */
66static LIST_HEAD(spu_full_list);
67static DEFINE_SPINLOCK(spu_full_list_lock);
68static DEFINE_MUTEX(spu_full_list_mutex);
69
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010070void spu_invalidate_slbs(struct spu *spu)
71{
72 struct spu_priv2 __iomem *priv2 = spu->priv2;
73
74 if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
75 out_be64(&priv2->slb_invalidate_all_W, 0UL);
76}
77EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
78
79/* This is called by the MM core when a segment size is changed, to
80 * request a flush of all the SPEs using a given mm
81 */
82void spu_flush_all_slbs(struct mm_struct *mm)
83{
84 struct spu *spu;
85 unsigned long flags;
86
Christoph Hellwig24140592007-07-20 21:39:51 +020087 spin_lock_irqsave(&spu_full_list_lock, flags);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010088 list_for_each_entry(spu, &spu_full_list, full_list) {
89 if (spu->mm == mm)
90 spu_invalidate_slbs(spu);
91 }
Christoph Hellwig24140592007-07-20 21:39:51 +020092 spin_unlock_irqrestore(&spu_full_list_lock, flags);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +010093}
94
95/* The hack below stinks... try to do something better one of
96 * these days... Does it even work properly with NR_CPUS == 1 ?
97 */
98static inline void mm_needs_global_tlbie(struct mm_struct *mm)
99{
100 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
101
102 /* Global TLBIE broadcast required with SPEs. */
103 __cpus_setall(&mm->cpu_vm_mask, nr);
104}
105
106void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
107{
108 unsigned long flags;
109
Christoph Hellwig24140592007-07-20 21:39:51 +0200110 spin_lock_irqsave(&spu_full_list_lock, flags);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100111 spu->mm = mm;
Christoph Hellwig24140592007-07-20 21:39:51 +0200112 spin_unlock_irqrestore(&spu_full_list_lock, flags);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100113 if (mm)
114 mm_needs_global_tlbie(mm);
115}
116EXPORT_SYMBOL_GPL(spu_associate_mm);
117
Arnd Bergmann67207b92005-11-15 15:53:48 -0500118static int __spu_trap_invalid_dma(struct spu *spu)
119{
120 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200121 spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500122 return 0;
123}
124
125static int __spu_trap_dma_align(struct spu *spu)
126{
127 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200128 spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500129 return 0;
130}
131
132static int __spu_trap_error(struct spu *spu)
133{
134 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200135 spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500136 return 0;
137}
138
139static void spu_restart_dma(struct spu *spu)
140{
141 struct spu_priv2 __iomem *priv2 = spu->priv2;
Mark Nutter5473af02005-11-15 15:53:49 -0500142
Arnd Bergmann8837d922006-01-04 20:31:28 +0100143 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
Mark Nutter5473af02005-11-15 15:53:49 -0500144 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500145}
146
147static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
148{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500149 struct spu_priv2 __iomem *priv2 = spu->priv2;
150 struct mm_struct *mm = spu->mm;
arnd@arndb.de724bd802006-06-19 20:33:23 +0200151 u64 esid, vsid, llp;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100152 int psize;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500153
154 pr_debug("%s\n", __FUNCTION__);
155
Arnd Bergmann8837d922006-01-04 20:31:28 +0100156 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500157 /* SLBs are pre-loaded for context switch, so
158 * we should never get here!
159 */
Mark Nutter5473af02005-11-15 15:53:49 -0500160 printk("%s: invalid access during switch!\n", __func__);
161 return 1;
162 }
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200163 esid = (ea & ESID_MASK) | SLB_ESID_V;
164
165 switch(REGION_ID(ea)) {
166 case USER_REGION_ID:
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000167#ifdef CONFIG_PPC_MM_SLICES
168 psize = get_slice_psize(mm, ea);
169#else
170 psize = mm->context.user_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200171#endif
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200172 vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100173 SLB_VSID_USER;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200174 break;
175 case VMALLOC_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100176 if (ea < VMALLOC_END)
177 psize = mmu_vmalloc_psize;
178 else
179 psize = mmu_io_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200180 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100181 SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200182 break;
183 case KERNEL_REGION_ID:
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100184 psize = mmu_linear_psize;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200185 vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100186 SLB_VSID_KERNEL;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200187 break;
188 default:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500189 /* Future: support kernel segments so that drivers
190 * can use SPUs.
191 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500192 pr_debug("invalid region access at %016lx\n", ea);
193 return 1;
194 }
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100195 llp = mmu_psize_defs[psize].sllp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500196
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500197 out_be64(&priv2->slb_index_W, spu->slb_replace);
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100198 out_be64(&priv2->slb_vsid_RW, vsid | llp);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500199 out_be64(&priv2->slb_esid_RW, esid);
200
201 spu->slb_replace++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500202 if (spu->slb_replace >= 8)
203 spu->slb_replace = 0;
204
Arnd Bergmann67207b92005-11-15 15:53:48 -0500205 spu_restart_dma(spu);
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000206 spu->stats.slb_flt++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500207 return 0;
208}
209
Mark Nutter5473af02005-11-15 15:53:49 -0500210extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500211static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500212{
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100213 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500214
Mark Nutter5473af02005-11-15 15:53:49 -0500215 /* Handle kernel space hash faults immediately.
216 User hash faults need to be deferred to process context. */
217 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
218 && REGION_ID(ea) != USER_REGION_ID
219 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
220 spu_restart_dma(spu);
221 return 0;
222 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500223
Arnd Bergmann8837d922006-01-04 20:31:28 +0100224 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Mark Nutter5473af02005-11-15 15:53:49 -0500225 printk("%s: invalid access during switch!\n", __func__);
226 return 1;
227 }
228
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500229 spu->dar = ea;
230 spu->dsisr = dsisr;
231 mb();
Masato Noguchiba723fe22006-06-19 20:33:33 +0200232 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500233 return 0;
234}
235
236static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200237spu_irq_class_0(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500238{
239 struct spu *spu;
240
241 spu = data;
242 spu->class_0_pending = 1;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200243 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500244
245 return IRQ_HANDLED;
246}
247
Arnd Bergmann51104592005-12-05 22:52:25 -0500248int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500249spu_irq_class_0_bottom(struct spu *spu)
250{
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500251 unsigned long stat, mask;
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900252 unsigned long flags;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500253
254 spu->class_0_pending = 0;
255
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900256 spin_lock_irqsave(&spu->register_lock, flags);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100257 mask = spu_int_mask_get(spu, 0);
258 stat = spu_int_stat_get(spu, 0);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500259
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500260 stat &= mask;
261
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200262 if (stat & 1) /* invalid DMA alignment */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500263 __spu_trap_dma_align(spu);
264
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200265 if (stat & 2) /* invalid MFC DMA */
266 __spu_trap_invalid_dma(spu);
267
Arnd Bergmann67207b92005-11-15 15:53:48 -0500268 if (stat & 4) /* error on SPU */
269 __spu_trap_error(spu);
270
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100271 spu_int_stat_clear(spu, 0, stat);
Ishizaki Kou3650cfe2007-01-12 09:52:41 +0900272 spin_unlock_irqrestore(&spu->register_lock, flags);
Arnd Bergmann51104592005-12-05 22:52:25 -0500273
274 return (stat & 0x7) ? -EIO : 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500275}
Arnd Bergmann51104592005-12-05 22:52:25 -0500276EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500277
278static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200279spu_irq_class_1(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500280{
281 struct spu *spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500282 unsigned long stat, mask, dar, dsisr;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500283
284 spu = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500285
286 /* atomically read & clear class1 status. */
287 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100288 mask = spu_int_mask_get(spu, 1);
289 stat = spu_int_stat_get(spu, 1) & mask;
290 dar = spu_mfc_dar_get(spu);
291 dsisr = spu_mfc_dsisr_get(spu);
Arnd Bergmann38307342005-12-09 19:04:18 +0100292 if (stat & 2) /* mapping fault */
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100293 spu_mfc_dsisr_set(spu, 0ul);
294 spu_int_stat_clear(spu, 1, stat);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500295 spin_unlock(&spu->register_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100296 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
297 dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500298
299 if (stat & 1) /* segment fault */
300 __spu_trap_data_seg(spu, dar);
301
302 if (stat & 2) { /* mapping fault */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500303 __spu_trap_data_map(spu, dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500304 }
305
306 if (stat & 4) /* ls compare & suspend on get */
307 ;
308
309 if (stat & 8) /* ls compare & suspend on put */
310 ;
311
Arnd Bergmann67207b92005-11-15 15:53:48 -0500312 return stat ? IRQ_HANDLED : IRQ_NONE;
313}
314
315static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200316spu_irq_class_2(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500317{
318 struct spu *spu;
319 unsigned long stat;
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500320 unsigned long mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500321
322 spu = data;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200323 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100324 stat = spu_int_stat_get(spu, 2);
325 mask = spu_int_mask_get(spu, 2);
Masato Noguchiba723fe22006-06-19 20:33:33 +0200326 /* ignore interrupts we're not waiting for */
327 stat &= mask;
328 /*
329 * mailbox interrupts (0x1 and 0x10) are level triggered.
330 * mask them now before acknowledging.
331 */
332 if (stat & 0x11)
333 spu_int_mask_and(spu, 2, ~(stat & 0x11));
334 /* acknowledge all interrupts before the callbacks */
335 spu_int_stat_clear(spu, 2, stat);
336 spin_unlock(&spu->register_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500337
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500338 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500339
Arnd Bergmann67207b92005-11-15 15:53:48 -0500340 if (stat & 1) /* PPC core mailbox */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200341 spu->ibox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500342
343 if (stat & 2) /* SPU stop-and-signal */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200344 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500345
346 if (stat & 4) /* SPU halted */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200347 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500348
349 if (stat & 8) /* DMA tag group complete */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200350 spu->mfc_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500351
352 if (stat & 0x10) /* SPU mailbox threshold */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200353 spu->wbox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500354
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000355 spu->stats.class2_intr++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500356 return stat ? IRQ_HANDLED : IRQ_NONE;
357}
358
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000359static int spu_request_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500360{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000361 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500362
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000363 if (spu->irqs[0] != NO_IRQ) {
364 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
365 spu->number);
366 ret = request_irq(spu->irqs[0], spu_irq_class_0,
367 IRQF_DISABLED,
368 spu->irq_c0, spu);
369 if (ret)
370 goto bail0;
371 }
372 if (spu->irqs[1] != NO_IRQ) {
373 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
374 spu->number);
375 ret = request_irq(spu->irqs[1], spu_irq_class_1,
376 IRQF_DISABLED,
377 spu->irq_c1, spu);
378 if (ret)
379 goto bail1;
380 }
381 if (spu->irqs[2] != NO_IRQ) {
382 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
383 spu->number);
384 ret = request_irq(spu->irqs[2], spu_irq_class_2,
385 IRQF_DISABLED,
386 spu->irq_c2, spu);
387 if (ret)
388 goto bail2;
389 }
390 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500391
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000392bail2:
393 if (spu->irqs[1] != NO_IRQ)
394 free_irq(spu->irqs[1], spu);
395bail1:
396 if (spu->irqs[0] != NO_IRQ)
397 free_irq(spu->irqs[0], spu);
398bail0:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500399 return ret;
400}
401
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000402static void spu_free_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500403{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000404 if (spu->irqs[0] != NO_IRQ)
405 free_irq(spu->irqs[0], spu);
406 if (spu->irqs[1] != NO_IRQ)
407 free_irq(spu->irqs[1], spu);
408 if (spu->irqs[2] != NO_IRQ)
409 free_irq(spu->irqs[2], spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500410}
411
Christoph Hellwig486acd42007-07-20 21:39:54 +0200412void spu_init_channels(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500413{
414 static const struct {
415 unsigned channel;
416 unsigned count;
417 } zero_list[] = {
418 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
419 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
420 }, count_list[] = {
421 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
422 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
423 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
424 };
Arnd Bergmann6ff730c2006-01-04 20:31:31 +0100425 struct spu_priv2 __iomem *priv2;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500426 int i;
427
428 priv2 = spu->priv2;
429
430 /* initialize all channel data to zero */
431 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
432 int count;
433
434 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
435 for (count = 0; count < zero_list[i].count; count++)
436 out_be64(&priv2->spu_chnldata_RW, 0);
437 }
438
439 /* initialize channel counts to meaningful values */
440 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
441 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
442 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
443 }
444}
Christoph Hellwig486acd42007-07-20 21:39:54 +0200445EXPORT_SYMBOL_GPL(spu_init_channels);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500446
Geoff Levand6deac062007-06-16 07:17:32 +1000447static int spu_shutdown(struct sys_device *sysdev)
448{
449 struct spu *spu = container_of(sysdev, struct spu, sysdev);
450
451 spu_free_irqs(spu);
452 spu_destroy_spu(spu);
453 return 0;
454}
455
Jeremy Kerr1d640932006-06-19 20:33:19 +0200456struct sysdev_class spu_sysdev_class = {
Geoff Levand6deac062007-06-16 07:17:32 +1000457 set_kset_name("spu"),
458 .shutdown = spu_shutdown,
Jeremy Kerr1d640932006-06-19 20:33:19 +0200459};
460
Christian Kraffte570beb2006-10-24 18:31:23 +0200461int spu_add_sysdev_attr(struct sysdev_attribute *attr)
462{
463 struct spu *spu;
Christian Kraffte570beb2006-10-24 18:31:23 +0200464
Christoph Hellwig24140592007-07-20 21:39:51 +0200465 mutex_lock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200466 list_for_each_entry(spu, &spu_full_list, full_list)
467 sysdev_create_file(&spu->sysdev, attr);
Christoph Hellwig24140592007-07-20 21:39:51 +0200468 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200469
Christian Kraffte570beb2006-10-24 18:31:23 +0200470 return 0;
471}
472EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
473
474int spu_add_sysdev_attr_group(struct attribute_group *attrs)
475{
476 struct spu *spu;
Christian Kraffte570beb2006-10-24 18:31:23 +0200477
Christoph Hellwig24140592007-07-20 21:39:51 +0200478 mutex_lock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200479 list_for_each_entry(spu, &spu_full_list, full_list)
480 sysfs_create_group(&spu->sysdev.kobj, attrs);
Christoph Hellwig24140592007-07-20 21:39:51 +0200481 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200482
Christian Kraffte570beb2006-10-24 18:31:23 +0200483 return 0;
484}
485EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
486
487
488void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
489{
490 struct spu *spu;
Christian Kraffte570beb2006-10-24 18:31:23 +0200491
Christoph Hellwig24140592007-07-20 21:39:51 +0200492 mutex_lock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200493 list_for_each_entry(spu, &spu_full_list, full_list)
494 sysdev_remove_file(&spu->sysdev, attr);
Christoph Hellwig24140592007-07-20 21:39:51 +0200495 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200496}
497EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
498
499void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
500{
501 struct spu *spu;
Christian Kraffte570beb2006-10-24 18:31:23 +0200502
Christoph Hellwig24140592007-07-20 21:39:51 +0200503 mutex_lock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200504 list_for_each_entry(spu, &spu_full_list, full_list)
505 sysfs_remove_group(&spu->sysdev.kobj, attrs);
Christoph Hellwig24140592007-07-20 21:39:51 +0200506 mutex_unlock(&spu_full_list_mutex);
Christian Kraffte570beb2006-10-24 18:31:23 +0200507}
508EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
509
Jeremy Kerr1d640932006-06-19 20:33:19 +0200510static int spu_create_sysdev(struct spu *spu)
511{
512 int ret;
513
514 spu->sysdev.id = spu->number;
515 spu->sysdev.cls = &spu_sysdev_class;
516 ret = sysdev_register(&spu->sysdev);
517 if (ret) {
518 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
519 spu->number);
520 return ret;
521 }
522
Geoff Levand00215502006-11-20 18:45:02 +0100523 sysfs_add_device_to_node(&spu->sysdev, spu->node);
Jeremy Kerr1d640932006-06-19 20:33:19 +0200524
525 return 0;
526}
527
Geoff Levande28b0032006-11-23 00:46:49 +0100528static int __init create_spu(void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500529{
530 struct spu *spu;
531 int ret;
532 static int number;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100533 unsigned long flags;
Andre Detsch27ec41d2007-07-20 21:39:33 +0200534 struct timespec ts;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500535
536 ret = -ENOMEM;
Jeremy Kerrecec2172006-06-19 20:33:26 +0200537 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500538 if (!spu)
539 goto out;
540
Christoph Hellwig486acd42007-07-20 21:39:54 +0200541 spu->alloc_state = SPU_FREE;
542
Geoff Levande28b0032006-11-23 00:46:49 +0100543 spin_lock_init(&spu->register_lock);
Christoph Hellwig24140592007-07-20 21:39:51 +0200544 spin_lock(&spu_lock);
Geoff Levande28b0032006-11-23 00:46:49 +0100545 spu->number = number++;
Christoph Hellwig24140592007-07-20 21:39:51 +0200546 spin_unlock(&spu_lock);
Benjamin Herrenschmidte5267b42006-10-10 15:14:12 +1000547
Geoff Levande28b0032006-11-23 00:46:49 +0100548 ret = spu_create_spu(spu, data);
549
Arnd Bergmann67207b92005-11-15 15:53:48 -0500550 if (ret)
551 goto out_free;
552
Masato Noguchi24f43b32006-10-24 18:31:14 +0200553 spu_mfc_sdr_setup(spu);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100554 spu_mfc_sr1_set(spu, 0x33);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500555 ret = spu_request_irqs(spu);
556 if (ret)
Geoff Levande28b0032006-11-23 00:46:49 +0100557 goto out_destroy;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500558
Jeremy Kerr1d640932006-06-19 20:33:19 +0200559 ret = spu_create_sysdev(spu);
560 if (ret)
561 goto out_free_irqs;
562
Christoph Hellwig486acd42007-07-20 21:39:54 +0200563 mutex_lock(&cbe_spu_info[spu->node].list_mutex);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200564 list_add(&spu->cbe_list, &cbe_spu_info[spu->node].spus);
565 cbe_spu_info[spu->node].n_spus++;
Christoph Hellwig486acd42007-07-20 21:39:54 +0200566 mutex_unlock(&cbe_spu_info[spu->node].list_mutex);
Christoph Hellwig24140592007-07-20 21:39:51 +0200567
568 mutex_lock(&spu_full_list_mutex);
569 spin_lock_irqsave(&spu_full_list_lock, flags);
Christian Kraffte570beb2006-10-24 18:31:23 +0200570 list_add(&spu->full_list, &spu_full_list);
Christoph Hellwig24140592007-07-20 21:39:51 +0200571 spin_unlock_irqrestore(&spu_full_list_lock, flags);
572 mutex_unlock(&spu_full_list_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500573
Andre Detsch27ec41d2007-07-20 21:39:33 +0200574 spu->stats.util_state = SPU_UTIL_IDLE_LOADED;
575 ktime_get_ts(&ts);
576 spu->stats.tstamp = timespec_to_ns(&ts);
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000577
Arnd Bergmann9d92af62007-07-20 21:39:45 +0200578 INIT_LIST_HEAD(&spu->aff_list);
579
Arnd Bergmann67207b92005-11-15 15:53:48 -0500580 goto out;
581
Jeremy Kerr1d640932006-06-19 20:33:19 +0200582out_free_irqs:
583 spu_free_irqs(spu);
Geoff Levande28b0032006-11-23 00:46:49 +0100584out_destroy:
585 spu_destroy_spu(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500586out_free:
587 kfree(spu);
588out:
589 return ret;
590}
591
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000592static const char *spu_state_names[] = {
593 "user", "system", "iowait", "idle"
594};
595
596static unsigned long long spu_acct_time(struct spu *spu,
597 enum spu_utilization_state state)
598{
Andre Detsch27ec41d2007-07-20 21:39:33 +0200599 struct timespec ts;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000600 unsigned long long time = spu->stats.times[state];
601
Andre Detsch27ec41d2007-07-20 21:39:33 +0200602 /*
603 * If the spu is idle or the context is stopped, utilization
604 * statistics are not updated. Apply the time delta from the
605 * last recorded state of the spu.
606 */
607 if (spu->stats.util_state == state) {
608 ktime_get_ts(&ts);
609 time += timespec_to_ns(&ts) - spu->stats.tstamp;
610 }
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000611
Andre Detsch27ec41d2007-07-20 21:39:33 +0200612 return time / NSEC_PER_MSEC;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000613}
614
615
616static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf)
617{
618 struct spu *spu = container_of(sysdev, struct spu, sysdev);
619
620 return sprintf(buf, "%s %llu %llu %llu %llu "
621 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +0200622 spu_state_names[spu->stats.util_state],
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000623 spu_acct_time(spu, SPU_UTIL_USER),
624 spu_acct_time(spu, SPU_UTIL_SYSTEM),
625 spu_acct_time(spu, SPU_UTIL_IOWAIT),
Andre Detsch27ec41d2007-07-20 21:39:33 +0200626 spu_acct_time(spu, SPU_UTIL_IDLE_LOADED),
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000627 spu->stats.vol_ctx_switch,
628 spu->stats.invol_ctx_switch,
629 spu->stats.slb_flt,
630 spu->stats.hash_flt,
631 spu->stats.min_flt,
632 spu->stats.maj_flt,
633 spu->stats.class2_intr,
634 spu->stats.libassist);
635}
636
637static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL);
638
Arnd Bergmann3ad216c2007-07-20 21:39:46 +0200639/* Hardcoded affinity idxs for QS20 */
640#define SPES_PER_BE 8
641static int QS20_reg_idxs[SPES_PER_BE] = { 0, 2, 4, 6, 7, 5, 3, 1 };
642static int QS20_reg_memory[SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 };
643
644static struct spu *spu_lookup_reg(int node, u32 reg)
645{
646 struct spu *spu;
647
648 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
649 if (*(u32 *)get_property(spu_devnode(spu), "reg", NULL) == reg)
650 return spu;
651 }
652 return NULL;
653}
654
655static void init_aff_QS20_harcoded(void)
656{
657 int node, i;
658 struct spu *last_spu, *spu;
659 u32 reg;
660
661 for (node = 0; node < MAX_NUMNODES; node++) {
662 last_spu = NULL;
663 for (i = 0; i < SPES_PER_BE; i++) {
664 reg = QS20_reg_idxs[i];
665 spu = spu_lookup_reg(node, reg);
666 if (!spu)
667 continue;
668 spu->has_mem_affinity = QS20_reg_memory[reg];
669 if (last_spu)
670 list_add_tail(&spu->aff_list,
671 &last_spu->aff_list);
672 last_spu = spu;
673 }
674 }
675}
676
677static int of_has_vicinity(void)
678{
679 struct spu* spu;
680
681 spu = list_entry(cbe_spu_info[0].spus.next, struct spu, cbe_list);
682 return of_find_property(spu_devnode(spu), "vicinity", NULL) != NULL;
683}
684
Arnd Bergmann9e7cbcb2007-07-20 21:39:50 +0200685static struct spu *aff_devnode_spu(int cbe, struct device_node *dn)
686{
687 struct spu *spu;
688
689 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list)
690 if (spu_devnode(spu) == dn)
691 return spu;
692 return NULL;
693}
694
695static struct spu *
696aff_node_next_to(int cbe, struct device_node *target, struct device_node *avoid)
697{
698 struct spu *spu;
699 const phandle *vic_handles;
700 int lenp, i;
701
702 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) {
703 if (spu_devnode(spu) == avoid)
704 continue;
705 vic_handles = get_property(spu_devnode(spu), "vicinity", &lenp);
706 for (i=0; i < (lenp / sizeof(phandle)); i++) {
707 if (vic_handles[i] == target->linux_phandle)
708 return spu;
709 }
710 }
711 return NULL;
712}
713
714static void init_aff_fw_vicinity_node(int cbe)
715{
716 struct spu *spu, *last_spu;
717 struct device_node *vic_dn, *last_spu_dn;
718 phandle avoid_ph;
719 const phandle *vic_handles;
720 const char *name;
721 int lenp, i, added, mem_aff;
722
723 last_spu = list_entry(cbe_spu_info[cbe].spus.next, struct spu, cbe_list);
724 avoid_ph = 0;
725 for (added = 1; added < cbe_spu_info[cbe].n_spus; added++) {
726 last_spu_dn = spu_devnode(last_spu);
727 vic_handles = get_property(last_spu_dn, "vicinity", &lenp);
728
729 for (i = 0; i < (lenp / sizeof(phandle)); i++) {
730 if (vic_handles[i] == avoid_ph)
731 continue;
732
733 vic_dn = of_find_node_by_phandle(vic_handles[i]);
734 if (!vic_dn)
735 continue;
736
737 name = get_property(vic_dn, "name", NULL);
738 if (strcmp(name, "spe") == 0) {
739 spu = aff_devnode_spu(cbe, vic_dn);
740 avoid_ph = last_spu_dn->linux_phandle;
741 }
742 else {
743 mem_aff = strcmp(name, "mic-tm") == 0;
744 spu = aff_node_next_to(cbe, vic_dn, last_spu_dn);
745 if (!spu)
746 continue;
747 if (mem_aff) {
748 last_spu->has_mem_affinity = 1;
749 spu->has_mem_affinity = 1;
750 }
751 avoid_ph = vic_dn->linux_phandle;
752 }
753 list_add_tail(&spu->aff_list, &last_spu->aff_list);
754 last_spu = spu;
755 break;
756 }
757 }
758}
759
760static void init_aff_fw_vicinity(void)
761{
762 int cbe;
763
764 /* sets has_mem_affinity for each spu, as long as the
765 * spu->aff_list list, linking each spu to its neighbors
766 */
767 for (cbe = 0; cbe < MAX_NUMNODES; cbe++)
768 init_aff_fw_vicinity_node(cbe);
769}
770
Arnd Bergmann67207b92005-11-15 15:53:48 -0500771static int __init init_spu_base(void)
772{
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200773 int i, ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500774
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200775 for (i = 0; i < MAX_NUMNODES; i++) {
Christoph Hellwig486acd42007-07-20 21:39:54 +0200776 mutex_init(&cbe_spu_info[i].list_mutex);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200777 INIT_LIST_HEAD(&cbe_spu_info[i].spus);
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200778 }
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200779
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100780 if (!spu_management_ops)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200781 goto out;
Stephen Rothwellda06aa02006-11-27 19:18:54 +0100782
Jeremy Kerr1d640932006-06-19 20:33:19 +0200783 /* create sysdev class for spus */
784 ret = sysdev_class_register(&spu_sysdev_class);
785 if (ret)
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200786 goto out;
Jeremy Kerr1d640932006-06-19 20:33:19 +0200787
Geoff Levande28b0032006-11-23 00:46:49 +0100788 ret = spu_enumerate_spus(create_spu);
789
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700790 if (ret < 0) {
Geoff Levande28b0032006-11-23 00:46:49 +0100791 printk(KERN_WARNING "%s: Error initializing spus\n",
792 __FUNCTION__);
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200793 goto out_unregister_sysdev_class;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500794 }
Michael Ellermanff8a8f22006-10-24 18:31:27 +0200795
Geert Uytterhoevenbce94512007-07-17 04:05:52 -0700796 if (ret > 0) {
797 /*
798 * We cannot put the forward declaration in
799 * <linux/linux_logo.h> because of conflicting session type
800 * conflicts for const and __initdata with different compiler
801 * versions
802 */
803 extern const struct linux_logo logo_spe_clut224;
804
805 fb_append_extra_logo(&logo_spe_clut224, ret);
806 }
807
Christoph Hellwig24140592007-07-20 21:39:51 +0200808 mutex_lock(&spu_full_list_mutex);
Michael Ellermanff8a8f22006-10-24 18:31:27 +0200809 xmon_register_spus(&spu_full_list);
Andre Detsch8d2655e2007-07-20 21:39:27 +0200810 crash_register_spus(&spu_full_list);
Christoph Hellwig24140592007-07-20 21:39:51 +0200811 mutex_unlock(&spu_full_list_mutex);
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000812 spu_add_sysdev_attr(&attr_stat);
813
Arnd Bergmann9e7cbcb2007-07-20 21:39:50 +0200814 if (of_has_vicinity()) {
815 init_aff_fw_vicinity();
816 } else {
Arnd Bergmann3ad216c2007-07-20 21:39:46 +0200817 long root = of_get_flat_dt_root();
818 if (of_flat_dt_is_compatible(root, "IBM,CPBW-1.0"))
819 init_aff_QS20_harcoded();
820 }
821
Christoph Hellwigbefdc742007-04-23 21:08:28 +0200822 return 0;
823
824 out_unregister_sysdev_class:
825 sysdev_class_unregister(&spu_sysdev_class);
826 out:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500827 return ret;
828}
829module_init(init_spu_base);
830
831MODULE_LICENSE("GPL");
832MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");