blob: d41ad1d6c04b2be902223915a8a56c87d75283fb [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 Bergmann7650f2f2006-10-04 17:26:20 +020028#include <linux/pci.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050029#include <linux/poll.h>
30#include <linux/ptrace.h>
31#include <linux/slab.h>
32#include <linux/wait.h>
33
Arnd Bergmann7650f2f2006-10-04 17:26:20 +020034#include <asm/firmware.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050035#include <asm/io.h>
36#include <asm/prom.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080037#include <linux/mutex.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050038#include <asm/spu.h>
Geoff Levand540270d2006-06-19 20:33:29 +020039#include <asm/spu_priv1.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050040#include <asm/mmu_context.h>
41
42#include "interrupt.h"
43
Geoff Levand540270d2006-06-19 20:33:29 +020044const struct spu_priv1_ops *spu_priv1_ops;
45
46EXPORT_SYMBOL_GPL(spu_priv1_ops);
47
Arnd Bergmann67207b92005-11-15 15:53:48 -050048static int __spu_trap_invalid_dma(struct spu *spu)
49{
50 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +020051 spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
Arnd Bergmann67207b92005-11-15 15:53:48 -050052 return 0;
53}
54
55static int __spu_trap_dma_align(struct spu *spu)
56{
57 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +020058 spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
Arnd Bergmann67207b92005-11-15 15:53:48 -050059 return 0;
60}
61
62static int __spu_trap_error(struct spu *spu)
63{
64 pr_debug("%s\n", __FUNCTION__);
Arnd Bergmann9add11d2006-10-04 17:26:14 +020065 spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
Arnd Bergmann67207b92005-11-15 15:53:48 -050066 return 0;
67}
68
69static void spu_restart_dma(struct spu *spu)
70{
71 struct spu_priv2 __iomem *priv2 = spu->priv2;
Mark Nutter5473af02005-11-15 15:53:49 -050072
Arnd Bergmann8837d922006-01-04 20:31:28 +010073 if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
Mark Nutter5473af02005-11-15 15:53:49 -050074 out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
Arnd Bergmann67207b92005-11-15 15:53:48 -050075}
76
77static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
78{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050079 struct spu_priv2 __iomem *priv2 = spu->priv2;
80 struct mm_struct *mm = spu->mm;
arnd@arndb.de724bd802006-06-19 20:33:23 +020081 u64 esid, vsid, llp;
Arnd Bergmann67207b92005-11-15 15:53:48 -050082
83 pr_debug("%s\n", __FUNCTION__);
84
Arnd Bergmann8837d922006-01-04 20:31:28 +010085 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050086 /* SLBs are pre-loaded for context switch, so
87 * we should never get here!
88 */
Mark Nutter5473af02005-11-15 15:53:49 -050089 printk("%s: invalid access during switch!\n", __func__);
90 return 1;
91 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050092 if (!mm || (REGION_ID(ea) != USER_REGION_ID)) {
93 /* Future: support kernel segments so that drivers
94 * can use SPUs.
95 */
Arnd Bergmann67207b92005-11-15 15:53:48 -050096 pr_debug("invalid region access at %016lx\n", ea);
97 return 1;
98 }
99
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500100 esid = (ea & ESID_MASK) | SLB_ESID_V;
arnd@arndb.de724bd802006-06-19 20:33:23 +0200101#ifdef CONFIG_HUGETLB_PAGE
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500102 if (in_hugepage_area(mm->context, ea))
arnd@arndb.de724bd802006-06-19 20:33:23 +0200103 llp = mmu_psize_defs[mmu_huge_psize].sllp;
104 else
105#endif
106 llp = mmu_psize_defs[mmu_virtual_psize].sllp;
107 vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
108 SLB_VSID_USER | llp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500109
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500110 out_be64(&priv2->slb_index_W, spu->slb_replace);
111 out_be64(&priv2->slb_vsid_RW, vsid);
112 out_be64(&priv2->slb_esid_RW, esid);
113
114 spu->slb_replace++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500115 if (spu->slb_replace >= 8)
116 spu->slb_replace = 0;
117
Arnd Bergmann67207b92005-11-15 15:53:48 -0500118 spu_restart_dma(spu);
119
Arnd Bergmann67207b92005-11-15 15:53:48 -0500120 return 0;
121}
122
Mark Nutter5473af02005-11-15 15:53:49 -0500123extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500124static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500125{
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100126 pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500127
Mark Nutter5473af02005-11-15 15:53:49 -0500128 /* Handle kernel space hash faults immediately.
129 User hash faults need to be deferred to process context. */
130 if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
131 && REGION_ID(ea) != USER_REGION_ID
132 && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
133 spu_restart_dma(spu);
134 return 0;
135 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500136
Arnd Bergmann8837d922006-01-04 20:31:28 +0100137 if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
Mark Nutter5473af02005-11-15 15:53:49 -0500138 printk("%s: invalid access during switch!\n", __func__);
139 return 1;
140 }
141
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500142 spu->dar = ea;
143 spu->dsisr = dsisr;
144 mb();
Masato Noguchiba723fe22006-06-19 20:33:33 +0200145 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500146 return 0;
147}
148
149static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200150spu_irq_class_0(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500151{
152 struct spu *spu;
153
154 spu = data;
155 spu->class_0_pending = 1;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200156 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500157
158 return IRQ_HANDLED;
159}
160
Arnd Bergmann51104592005-12-05 22:52:25 -0500161int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500162spu_irq_class_0_bottom(struct spu *spu)
163{
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500164 unsigned long stat, mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500165
166 spu->class_0_pending = 0;
167
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100168 mask = spu_int_mask_get(spu, 0);
169 stat = spu_int_stat_get(spu, 0);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500170
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500171 stat &= mask;
172
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200173 if (stat & 1) /* invalid DMA alignment */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500174 __spu_trap_dma_align(spu);
175
Arnd Bergmann2cd90bc2006-06-23 20:57:50 +0200176 if (stat & 2) /* invalid MFC DMA */
177 __spu_trap_invalid_dma(spu);
178
Arnd Bergmann67207b92005-11-15 15:53:48 -0500179 if (stat & 4) /* error on SPU */
180 __spu_trap_error(spu);
181
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100182 spu_int_stat_clear(spu, 0, stat);
Arnd Bergmann51104592005-12-05 22:52:25 -0500183
184 return (stat & 0x7) ? -EIO : 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500185}
Arnd Bergmann51104592005-12-05 22:52:25 -0500186EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500187
188static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200189spu_irq_class_1(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500190{
191 struct spu *spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500192 unsigned long stat, mask, dar, dsisr;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500193
194 spu = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500195
196 /* atomically read & clear class1 status. */
197 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100198 mask = spu_int_mask_get(spu, 1);
199 stat = spu_int_stat_get(spu, 1) & mask;
200 dar = spu_mfc_dar_get(spu);
201 dsisr = spu_mfc_dsisr_get(spu);
Arnd Bergmann38307342005-12-09 19:04:18 +0100202 if (stat & 2) /* mapping fault */
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100203 spu_mfc_dsisr_set(spu, 0ul);
204 spu_int_stat_clear(spu, 1, stat);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500205 spin_unlock(&spu->register_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100206 pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
207 dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500208
209 if (stat & 1) /* segment fault */
210 __spu_trap_data_seg(spu, dar);
211
212 if (stat & 2) { /* mapping fault */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500213 __spu_trap_data_map(spu, dar, dsisr);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500214 }
215
216 if (stat & 4) /* ls compare & suspend on get */
217 ;
218
219 if (stat & 8) /* ls compare & suspend on put */
220 ;
221
Arnd Bergmann67207b92005-11-15 15:53:48 -0500222 return stat ? IRQ_HANDLED : IRQ_NONE;
223}
Arnd Bergmann51104592005-12-05 22:52:25 -0500224EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500225
226static irqreturn_t
Olaf Heringf5a92452006-10-06 22:52:16 +0200227spu_irq_class_2(int irq, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500228{
229 struct spu *spu;
230 unsigned long stat;
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500231 unsigned long mask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500232
233 spu = data;
Masato Noguchiba723fe22006-06-19 20:33:33 +0200234 spin_lock(&spu->register_lock);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100235 stat = spu_int_stat_get(spu, 2);
236 mask = spu_int_mask_get(spu, 2);
Masato Noguchiba723fe22006-06-19 20:33:33 +0200237 /* ignore interrupts we're not waiting for */
238 stat &= mask;
239 /*
240 * mailbox interrupts (0x1 and 0x10) are level triggered.
241 * mask them now before acknowledging.
242 */
243 if (stat & 0x11)
244 spu_int_mask_and(spu, 2, ~(stat & 0x11));
245 /* acknowledge all interrupts before the callbacks */
246 spu_int_stat_clear(spu, 2, stat);
247 spin_unlock(&spu->register_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500248
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500249 pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500250
Arnd Bergmann67207b92005-11-15 15:53:48 -0500251 if (stat & 1) /* PPC core mailbox */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200252 spu->ibox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500253
254 if (stat & 2) /* SPU stop-and-signal */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200255 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500256
257 if (stat & 4) /* SPU halted */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200258 spu->stop_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500259
260 if (stat & 8) /* DMA tag group complete */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200261 spu->mfc_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500262
263 if (stat & 0x10) /* SPU mailbox threshold */
Masato Noguchiba723fe22006-06-19 20:33:33 +0200264 spu->wbox_callback(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500265
Arnd Bergmann67207b92005-11-15 15:53:48 -0500266 return stat ? IRQ_HANDLED : IRQ_NONE;
267}
268
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000269static int spu_request_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500270{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000271 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500272
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000273 if (spu->irqs[0] != NO_IRQ) {
274 snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
275 spu->number);
276 ret = request_irq(spu->irqs[0], spu_irq_class_0,
277 IRQF_DISABLED,
278 spu->irq_c0, spu);
279 if (ret)
280 goto bail0;
281 }
282 if (spu->irqs[1] != NO_IRQ) {
283 snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
284 spu->number);
285 ret = request_irq(spu->irqs[1], spu_irq_class_1,
286 IRQF_DISABLED,
287 spu->irq_c1, spu);
288 if (ret)
289 goto bail1;
290 }
291 if (spu->irqs[2] != NO_IRQ) {
292 snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
293 spu->number);
294 ret = request_irq(spu->irqs[2], spu_irq_class_2,
295 IRQF_DISABLED,
296 spu->irq_c2, spu);
297 if (ret)
298 goto bail2;
299 }
300 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500301
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000302bail2:
303 if (spu->irqs[1] != NO_IRQ)
304 free_irq(spu->irqs[1], spu);
305bail1:
306 if (spu->irqs[0] != NO_IRQ)
307 free_irq(spu->irqs[0], spu);
308bail0:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500309 return ret;
310}
311
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000312static void spu_free_irqs(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500313{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000314 if (spu->irqs[0] != NO_IRQ)
315 free_irq(spu->irqs[0], spu);
316 if (spu->irqs[1] != NO_IRQ)
317 free_irq(spu->irqs[1], spu);
318 if (spu->irqs[2] != NO_IRQ)
319 free_irq(spu->irqs[2], spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500320}
321
Mark Nuttera68cf982006-10-04 17:26:12 +0200322static struct list_head spu_list[MAX_NUMNODES];
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800323static DEFINE_MUTEX(spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500324
325static void spu_init_channels(struct spu *spu)
326{
327 static const struct {
328 unsigned channel;
329 unsigned count;
330 } zero_list[] = {
331 { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
332 { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
333 }, count_list[] = {
334 { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
335 { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
336 { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
337 };
Arnd Bergmann6ff730c2006-01-04 20:31:31 +0100338 struct spu_priv2 __iomem *priv2;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500339 int i;
340
341 priv2 = spu->priv2;
342
343 /* initialize all channel data to zero */
344 for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
345 int count;
346
347 out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
348 for (count = 0; count < zero_list[i].count; count++)
349 out_be64(&priv2->spu_chnldata_RW, 0);
350 }
351
352 /* initialize channel counts to meaningful values */
353 for (i = 0; i < ARRAY_SIZE(count_list); i++) {
354 out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
355 out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
356 }
357}
358
Mark Nuttera68cf982006-10-04 17:26:12 +0200359struct spu *spu_alloc_node(int node)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500360{
Mark Nuttera68cf982006-10-04 17:26:12 +0200361 struct spu *spu = NULL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500362
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800363 mutex_lock(&spu_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200364 if (!list_empty(&spu_list[node])) {
365 spu = list_entry(spu_list[node].next, struct spu, list);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500366 list_del_init(&spu->list);
Mark Nuttera68cf982006-10-04 17:26:12 +0200367 pr_debug("Got SPU %x %d %d\n",
368 spu->isrc, spu->number, spu->node);
369 spu_init_channels(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500370 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800371 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500372
Mark Nuttera68cf982006-10-04 17:26:12 +0200373 return spu;
374}
375EXPORT_SYMBOL_GPL(spu_alloc_node);
376
377struct spu *spu_alloc(void)
378{
379 struct spu *spu = NULL;
380 int node;
381
382 for (node = 0; node < MAX_NUMNODES; node++) {
383 spu = spu_alloc_node(node);
384 if (spu)
385 break;
386 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500387
388 return spu;
389}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500390
391void spu_free(struct spu *spu)
392{
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800393 mutex_lock(&spu_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200394 list_add_tail(&spu->list, &spu_list[spu->node]);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800395 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500396}
Arnd Bergmann39c73c32005-12-05 22:52:21 -0500397EXPORT_SYMBOL_GPL(spu_free);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500398
Arnd Bergmann67207b92005-11-15 15:53:48 -0500399static int spu_handle_mm_fault(struct spu *spu)
400{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500401 struct mm_struct *mm = spu->mm;
402 struct vm_area_struct *vma;
403 u64 ea, dsisr, is_write;
404 int ret;
405
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500406 ea = spu->dar;
407 dsisr = spu->dsisr;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500408#if 0
409 if (!IS_VALID_EA(ea)) {
410 return -EFAULT;
411 }
412#endif /* XXX */
413 if (mm == NULL) {
414 return -EFAULT;
415 }
416 if (mm->pgd == NULL) {
417 return -EFAULT;
418 }
419
420 down_read(&mm->mmap_sem);
421 vma = find_vma(mm, ea);
422 if (!vma)
423 goto bad_area;
424 if (vma->vm_start <= ea)
425 goto good_area;
426 if (!(vma->vm_flags & VM_GROWSDOWN))
427 goto bad_area;
428#if 0
429 if (expand_stack(vma, ea))
430 goto bad_area;
431#endif /* XXX */
432good_area:
433 is_write = dsisr & MFC_DSISR_ACCESS_PUT;
434 if (is_write) {
435 if (!(vma->vm_flags & VM_WRITE))
436 goto bad_area;
437 } else {
438 if (dsisr & MFC_DSISR_ACCESS_DENIED)
439 goto bad_area;
440 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
441 goto bad_area;
442 }
443 ret = 0;
444 switch (handle_mm_fault(mm, vma, ea, is_write)) {
445 case VM_FAULT_MINOR:
446 current->min_flt++;
447 break;
448 case VM_FAULT_MAJOR:
449 current->maj_flt++;
450 break;
451 case VM_FAULT_SIGBUS:
452 ret = -EFAULT;
453 goto bad_area;
454 case VM_FAULT_OOM:
455 ret = -ENOMEM;
456 goto bad_area;
457 default:
458 BUG();
459 }
460 up_read(&mm->mmap_sem);
461 return ret;
462
463bad_area:
464 up_read(&mm->mmap_sem);
465 return -EFAULT;
466}
467
Arnd Bergmann51104592005-12-05 22:52:25 -0500468int spu_irq_class_1_bottom(struct spu *spu)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500469{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500470 u64 ea, dsisr, access, error = 0UL;
471 int ret = 0;
472
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500473 ea = spu->dar;
474 dsisr = spu->dsisr;
Arnd Bergmann79c227a2006-03-24 19:49:27 +0100475 if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) {
Arnd Bergmannf8072212006-04-29 02:40:21 +0200476 u64 flags;
477
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500478 access = (_PAGE_PRESENT | _PAGE_USER);
479 access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
Arnd Bergmannf8072212006-04-29 02:40:21 +0200480 local_irq_save(flags);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500481 if (hash_page(ea, access, 0x300) != 0)
482 error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
Arnd Bergmannf8072212006-04-29 02:40:21 +0200483 local_irq_restore(flags);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500484 }
Arnd Bergmann79c227a2006-03-24 19:49:27 +0100485 if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500486 if ((ret = spu_handle_mm_fault(spu)) != 0)
487 error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
488 else
489 error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
490 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500491 spu->dar = 0UL;
492 spu->dsisr = 0UL;
493 if (!error) {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500494 spu_restart_dma(spu);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500495 } else {
496 __spu_trap_invalid_dma(spu);
497 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500498 return ret;
499}
500
Joel H Schoppbed120c2006-05-01 12:16:11 -0700501static int __init find_spu_node_id(struct device_node *spe)
502{
Jeremy Kerrc61c27d2006-07-12 15:39:54 +1000503 const unsigned int *id;
Joel H Schoppbed120c2006-05-01 12:16:11 -0700504 struct device_node *cpu;
505 cpu = spe->parent->parent;
Jeremy Kerrc61c27d2006-07-12 15:39:54 +1000506 id = get_property(cpu, "node-id", NULL);
Joel H Schoppbed120c2006-05-01 12:16:11 -0700507 return id ? *id : 0;
508}
509
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700510static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
511 const char *prop)
Joel H Schoppbed120c2006-05-01 12:16:11 -0700512{
513 static DEFINE_MUTEX(add_spumem_mutex);
514
Jeremy Kerrc61c27d2006-07-12 15:39:54 +1000515 const struct address_prop {
Joel H Schoppbed120c2006-05-01 12:16:11 -0700516 unsigned long address;
517 unsigned int len;
518 } __attribute__((packed)) *p;
519 int proplen;
520
521 unsigned long start_pfn, nr_pages;
Joel H Schoppbed120c2006-05-01 12:16:11 -0700522 struct pglist_data *pgdata;
523 struct zone *zone;
524 int ret;
525
Jeremy Kerrc61c27d2006-07-12 15:39:54 +1000526 p = get_property(spe, prop, &proplen);
Joel H Schoppbed120c2006-05-01 12:16:11 -0700527 WARN_ON(proplen != sizeof (*p));
528
529 start_pfn = p->address >> PAGE_SHIFT;
530 nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
531
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700532 pgdata = NODE_DATA(spu->nid);
Joel H Schoppbed120c2006-05-01 12:16:11 -0700533 zone = pgdata->node_zones;
534
535 /* XXX rethink locking here */
536 mutex_lock(&add_spumem_mutex);
537 ret = __add_pages(zone, start_pfn, nr_pages);
538 mutex_unlock(&add_spumem_mutex);
539
540 return ret;
541}
542
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700543static void __iomem * __init map_spe_prop(struct spu *spu,
544 struct device_node *n, const char *name)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500545{
Jeremy Kerrc61c27d2006-07-12 15:39:54 +1000546 const struct address_prop {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500547 unsigned long address;
548 unsigned int len;
549 } __attribute__((packed)) *prop;
550
Jeremy Kerrc61c27d2006-07-12 15:39:54 +1000551 const void *p;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500552 int proplen;
Al Viroed2bfcd2006-09-23 01:37:41 +0100553 void __iomem *ret = NULL;
Joel H Schoppbed120c2006-05-01 12:16:11 -0700554 int err = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500555
556 p = get_property(n, name, &proplen);
557 if (proplen != sizeof (struct address_prop))
558 return NULL;
559
560 prop = p;
561
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700562 err = cell_spuprop_present(spu, n, name);
Joel H Schoppbed120c2006-05-01 12:16:11 -0700563 if (err && (err != -EEXIST))
564 goto out;
565
566 ret = ioremap(prop->address, prop->len);
567
568 out:
569 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500570}
571
572static void spu_unmap(struct spu *spu)
573{
574 iounmap(spu->priv2);
575 iounmap(spu->priv1);
576 iounmap(spu->problem);
Al Viroed2bfcd2006-09-23 01:37:41 +0100577 iounmap((__force u8 __iomem *)spu->local_store);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500578}
579
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000580/* This function shall be abstracted for HV platforms */
Arnd Bergmann7650f2f2006-10-04 17:26:20 +0200581static int __init spu_map_interrupts_old(struct spu *spu, struct device_node *np)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000582{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000583 unsigned int isrc;
Jeremy Kerrc61c27d2006-07-12 15:39:54 +1000584 const u32 *tmp;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000585
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000586 /* Get the interrupt source unit from the device-tree */
Jeremy Kerrc61c27d2006-07-12 15:39:54 +1000587 tmp = get_property(np, "isrc", NULL);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000588 if (!tmp)
589 return -ENODEV;
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000590 isrc = tmp[0];
591
592 /* Add the node number */
593 isrc |= spu->node << IIC_IRQ_NODE_SHIFT;
594 spu->isrc = isrc;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000595
596 /* Now map interrupts of all 3 classes */
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000597 spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
598 spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
599 spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000600
601 /* Right now, we only fail if class 2 failed */
602 return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
603}
604
Arnd Bergmann7650f2f2006-10-04 17:26:20 +0200605static int __init spu_map_device_old(struct spu *spu, struct device_node *node)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500606{
Jeremy Kerrc61c27d2006-07-12 15:39:54 +1000607 const char *prop;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500608 int ret;
609
610 ret = -ENODEV;
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700611 spu->name = get_property(node, "name", NULL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500612 if (!spu->name)
613 goto out;
614
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700615 prop = get_property(node, "local-store", NULL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500616 if (!prop)
617 goto out;
618 spu->local_store_phys = *(unsigned long *)prop;
619
620 /* we use local store as ram, not io memory */
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700621 spu->local_store = (void __force *)
622 map_spe_prop(spu, node, "local-store");
Arnd Bergmann67207b92005-11-15 15:53:48 -0500623 if (!spu->local_store)
624 goto out;
625
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700626 prop = get_property(node, "problem", NULL);
Mark Nutter6df10a82006-03-23 00:00:12 +0100627 if (!prop)
628 goto out_unmap;
629 spu->problem_phys = *(unsigned long *)prop;
630
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700631 spu->problem= map_spe_prop(spu, node, "problem");
Arnd Bergmann67207b92005-11-15 15:53:48 -0500632 if (!spu->problem)
633 goto out_unmap;
634
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700635 spu->priv1= map_spe_prop(spu, node, "priv1");
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100636 /* priv1 is not available on a hypervisor */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500637
Jeremy Kerr8261aa62006-05-01 12:16:13 -0700638 spu->priv2= map_spe_prop(spu, node, "priv2");
Arnd Bergmann67207b92005-11-15 15:53:48 -0500639 if (!spu->priv2)
640 goto out_unmap;
641 ret = 0;
642 goto out;
643
644out_unmap:
645 spu_unmap(spu);
646out:
647 return ret;
648}
649
Arnd Bergmann7650f2f2006-10-04 17:26:20 +0200650static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
651{
652 struct of_irq oirq;
653 int ret;
654 int i;
655
656 for (i=0; i < 3; i++) {
657 ret = of_irq_map_one(np, i, &oirq);
658 if (ret)
659 goto err;
660
661 ret = -EINVAL;
662 spu->irqs[i] = irq_create_of_mapping(oirq.controller,
663 oirq.specifier, oirq.size);
664 if (spu->irqs[i] == NO_IRQ)
665 goto err;
666 }
667 return 0;
668
669err:
670 pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier, spu->name);
671 for (; i >= 0; i--) {
672 if (spu->irqs[i] != NO_IRQ)
673 irq_dispose_mapping(spu->irqs[i]);
674 }
675 return ret;
676}
677
678static int spu_map_resource(struct device_node *node, int nr,
679 void __iomem** virt, unsigned long *phys)
680{
681 struct resource resource = { };
682 int ret;
683
684 ret = of_address_to_resource(node, 0, &resource);
685 if (ret)
686 goto out;
687
688 if (phys)
689 *phys = resource.start;
690 *virt = ioremap(resource.start, resource.end - resource.start);
691 if (!*virt)
692 ret = -EINVAL;
693
694out:
695 return ret;
696}
697
698static int __init spu_map_device(struct spu *spu, struct device_node *node)
699{
700 int ret = -ENODEV;
701 spu->name = get_property(node, "name", NULL);
702 if (!spu->name)
703 goto out;
704
705 ret = spu_map_resource(node, 0, (void __iomem**)&spu->local_store,
706 &spu->local_store_phys);
707 if (ret)
708 goto out;
709 ret = spu_map_resource(node, 1, (void __iomem**)&spu->problem,
710 &spu->problem_phys);
711 if (ret)
712 goto out_unmap;
713 ret = spu_map_resource(node, 2, (void __iomem**)&spu->priv2,
714 NULL);
715 if (ret)
716 goto out_unmap;
717
718 if (!firmware_has_feature(FW_FEATURE_LPAR))
719 ret = spu_map_resource(node, 3, (void __iomem**)&spu->priv1,
720 NULL);
721 if (ret)
722 goto out_unmap;
723 return 0;
724
725out_unmap:
726 spu_unmap(spu);
727out:
728 pr_debug("failed to map spe %s: %d\n", spu->name, ret);
729 return ret;
730}
731
Jeremy Kerr1d640932006-06-19 20:33:19 +0200732struct sysdev_class spu_sysdev_class = {
733 set_kset_name("spu")
734};
735
736static ssize_t spu_show_isrc(struct sys_device *sysdev, char *buf)
737{
738 struct spu *spu = container_of(sysdev, struct spu, sysdev);
739 return sprintf(buf, "%d\n", spu->isrc);
740
741}
742static SYSDEV_ATTR(isrc, 0400, spu_show_isrc, NULL);
743
744extern int attach_sysdev_to_node(struct sys_device *dev, int nid);
745
746static int spu_create_sysdev(struct spu *spu)
747{
748 int ret;
749
750 spu->sysdev.id = spu->number;
751 spu->sysdev.cls = &spu_sysdev_class;
752 ret = sysdev_register(&spu->sysdev);
753 if (ret) {
754 printk(KERN_ERR "Can't register SPU %d with sysfs\n",
755 spu->number);
756 return ret;
757 }
758
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000759 if (spu->isrc != 0)
760 sysdev_create_file(&spu->sysdev, &attr_isrc);
Jeremy Kerr1d640932006-06-19 20:33:19 +0200761 sysfs_add_device_to_node(&spu->sysdev, spu->nid);
762
763 return 0;
764}
765
766static void spu_destroy_sysdev(struct spu *spu)
767{
768 sysdev_remove_file(&spu->sysdev, &attr_isrc);
769 sysfs_remove_device_from_node(&spu->sysdev, spu->nid);
770 sysdev_unregister(&spu->sysdev);
771}
772
Arnd Bergmann67207b92005-11-15 15:53:48 -0500773static int __init create_spu(struct device_node *spe)
774{
775 struct spu *spu;
776 int ret;
777 static int number;
778
779 ret = -ENOMEM;
Jeremy Kerrecec2172006-06-19 20:33:26 +0200780 spu = kzalloc(sizeof (*spu), GFP_KERNEL);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500781 if (!spu)
782 goto out;
783
Benjamin Herrenschmidte5267b42006-10-10 15:14:12 +1000784 spu->node = find_spu_node_id(spe);
785 if (spu->node >= MAX_NUMNODES) {
786 printk(KERN_WARNING "SPE %s on node %d ignored,"
787 " node number too big\n", spe->full_name, spu->node);
788 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
789 return -ENODEV;
790 }
791 spu->nid = of_node_to_nid(spe);
792 if (spu->nid == -1)
793 spu->nid = 0;
794
Arnd Bergmann67207b92005-11-15 15:53:48 -0500795 ret = spu_map_device(spu, spe);
Arnd Bergmann7650f2f2006-10-04 17:26:20 +0200796 /* try old method */
797 if (ret)
798 ret = spu_map_device_old(spu, spe);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500799 if (ret)
800 goto out_free;
801
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000802 ret = spu_map_interrupts(spu, spe);
803 if (ret)
Arnd Bergmann7650f2f2006-10-04 17:26:20 +0200804 ret = spu_map_interrupts_old(spu, spe);
805 if (ret)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000806 goto out_unmap;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500807 spin_lock_init(&spu->register_lock);
Masato Noguchi24f43b32006-10-24 18:31:14 +0200808 spu_mfc_sdr_setup(spu);
Arnd Bergmannf0831ac2006-01-04 20:31:30 +0100809 spu_mfc_sr1_set(spu, 0x33);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800810 mutex_lock(&spu_mutex);
Jeremy Kerrecec2172006-06-19 20:33:26 +0200811
Arnd Bergmann67207b92005-11-15 15:53:48 -0500812 spu->number = number++;
813 ret = spu_request_irqs(spu);
814 if (ret)
Arnd Bergmann7650f2f2006-10-04 17:26:20 +0200815 goto out_unlock;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500816
Jeremy Kerr1d640932006-06-19 20:33:19 +0200817 ret = spu_create_sysdev(spu);
818 if (ret)
819 goto out_free_irqs;
820
Mark Nuttera68cf982006-10-04 17:26:12 +0200821 list_add(&spu->list, &spu_list[spu->node]);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800822 mutex_unlock(&spu_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500823
824 pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n",
825 spu->name, spu->isrc, spu->local_store,
826 spu->problem, spu->priv1, spu->priv2, spu->number);
827 goto out;
828
Jeremy Kerr1d640932006-06-19 20:33:19 +0200829out_free_irqs:
830 spu_free_irqs(spu);
Arnd Bergmann7650f2f2006-10-04 17:26:20 +0200831out_unlock:
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800832 mutex_unlock(&spu_mutex);
Arnd Bergmann7650f2f2006-10-04 17:26:20 +0200833out_unmap:
Arnd Bergmann67207b92005-11-15 15:53:48 -0500834 spu_unmap(spu);
835out_free:
836 kfree(spu);
837out:
838 return ret;
839}
840
841static void destroy_spu(struct spu *spu)
842{
843 list_del_init(&spu->list);
844
Jeremy Kerr1d640932006-06-19 20:33:19 +0200845 spu_destroy_sysdev(spu);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500846 spu_free_irqs(spu);
847 spu_unmap(spu);
848 kfree(spu);
849}
850
851static void cleanup_spu_base(void)
852{
853 struct spu *spu, *tmp;
Mark Nuttera68cf982006-10-04 17:26:12 +0200854 int node;
855
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800856 mutex_lock(&spu_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200857 for (node = 0; node < MAX_NUMNODES; node++) {
858 list_for_each_entry_safe(spu, tmp, &spu_list[node], list)
859 destroy_spu(spu);
860 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800861 mutex_unlock(&spu_mutex);
Jeremy Kerr1d640932006-06-19 20:33:19 +0200862 sysdev_class_unregister(&spu_sysdev_class);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500863}
864module_exit(cleanup_spu_base);
865
866static int __init init_spu_base(void)
867{
868 struct device_node *node;
Mark Nuttera68cf982006-10-04 17:26:12 +0200869 int i, ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500870
Jeremy Kerr1d640932006-06-19 20:33:19 +0200871 /* create sysdev class for spus */
872 ret = sysdev_class_register(&spu_sysdev_class);
873 if (ret)
874 return ret;
875
Mark Nuttera68cf982006-10-04 17:26:12 +0200876 for (i = 0; i < MAX_NUMNODES; i++)
877 INIT_LIST_HEAD(&spu_list[i]);
878
Arnd Bergmann67207b92005-11-15 15:53:48 -0500879 ret = -ENODEV;
880 for (node = of_find_node_by_type(NULL, "spe");
881 node; node = of_find_node_by_type(node, "spe")) {
882 ret = create_spu(node);
883 if (ret) {
884 printk(KERN_WARNING "%s: Error initializing %s\n",
885 __FUNCTION__, node->name);
886 cleanup_spu_base();
887 break;
888 }
889 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500890 return ret;
891}
892module_init(init_spu_base);
893
894MODULE_LICENSE("GPL");
895MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");