spu_base.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  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. #define DEBUG 1
  23. #include <linux/interrupt.h>
  24. #include <linux/list.h>
  25. #include <linux/module.h>
  26. #include <linux/poll.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/slab.h>
  29. #include <linux/wait.h>
  30. #include <asm/io.h>
  31. #include <asm/prom.h>
  32. #include <asm/semaphore.h>
  33. #include <asm/spu.h>
  34. #include <asm/mmu_context.h>
  35. #include "interrupt.h"
  36. static int __spu_trap_invalid_dma(struct spu *spu)
  37. {
  38. pr_debug("%s\n", __FUNCTION__);
  39. force_sig(SIGBUS, /* info, */ current);
  40. return 0;
  41. }
  42. static int __spu_trap_dma_align(struct spu *spu)
  43. {
  44. pr_debug("%s\n", __FUNCTION__);
  45. force_sig(SIGBUS, /* info, */ current);
  46. return 0;
  47. }
  48. static int __spu_trap_error(struct spu *spu)
  49. {
  50. pr_debug("%s\n", __FUNCTION__);
  51. force_sig(SIGILL, /* info, */ current);
  52. return 0;
  53. }
  54. static void spu_restart_dma(struct spu *spu)
  55. {
  56. struct spu_priv2 __iomem *priv2 = spu->priv2;
  57. if (!test_bit(SPU_CONTEXT_SWITCH_PENDING_nr, &spu->flags))
  58. out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
  59. }
  60. static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
  61. {
  62. struct spu_priv2 __iomem *priv2;
  63. struct mm_struct *mm;
  64. pr_debug("%s\n", __FUNCTION__);
  65. if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE_nr, &spu->flags)) {
  66. printk("%s: invalid access during switch!\n", __func__);
  67. return 1;
  68. }
  69. if (REGION_ID(ea) != USER_REGION_ID) {
  70. pr_debug("invalid region access at %016lx\n", ea);
  71. return 1;
  72. }
  73. priv2 = spu->priv2;
  74. mm = spu->mm;
  75. if (spu->slb_replace >= 8)
  76. spu->slb_replace = 0;
  77. out_be64(&priv2->slb_index_W, spu->slb_replace);
  78. out_be64(&priv2->slb_vsid_RW,
  79. (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT)
  80. | SLB_VSID_USER);
  81. out_be64(&priv2->slb_esid_RW, (ea & ESID_MASK) | SLB_ESID_V);
  82. spu_restart_dma(spu);
  83. pr_debug("set slb %d context %lx, ea %016lx, vsid %016lx, esid %016lx\n",
  84. spu->slb_replace, mm->context.id, ea,
  85. (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT)| SLB_VSID_USER,
  86. (ea & ESID_MASK) | SLB_ESID_V);
  87. return 0;
  88. }
  89. extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
  90. static int __spu_trap_data_map(struct spu *spu, unsigned long ea)
  91. {
  92. unsigned long dsisr;
  93. struct spu_priv1 __iomem *priv1;
  94. pr_debug("%s\n", __FUNCTION__);
  95. priv1 = spu->priv1;
  96. dsisr = in_be64(&priv1->mfc_dsisr_RW);
  97. /* Handle kernel space hash faults immediately.
  98. User hash faults need to be deferred to process context. */
  99. if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
  100. && REGION_ID(ea) != USER_REGION_ID
  101. && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
  102. spu_restart_dma(spu);
  103. return 0;
  104. }
  105. if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE_nr, &spu->flags)) {
  106. printk("%s: invalid access during switch!\n", __func__);
  107. return 1;
  108. }
  109. wake_up(&spu->stop_wq);
  110. return 0;
  111. }
  112. static int __spu_trap_mailbox(struct spu *spu)
  113. {
  114. wake_up_all(&spu->ibox_wq);
  115. kill_fasync(&spu->ibox_fasync, SIGIO, POLLIN);
  116. /* atomically disable SPU mailbox interrupts */
  117. spin_lock(&spu->register_lock);
  118. out_be64(&spu->priv1->int_mask_class2_RW,
  119. in_be64(&spu->priv1->int_mask_class2_RW) & ~0x1);
  120. spin_unlock(&spu->register_lock);
  121. return 0;
  122. }
  123. static int __spu_trap_stop(struct spu *spu)
  124. {
  125. pr_debug("%s\n", __FUNCTION__);
  126. spu->stop_code = in_be32(&spu->problem->spu_status_R);
  127. wake_up(&spu->stop_wq);
  128. return 0;
  129. }
  130. static int __spu_trap_halt(struct spu *spu)
  131. {
  132. pr_debug("%s\n", __FUNCTION__);
  133. spu->stop_code = in_be32(&spu->problem->spu_status_R);
  134. wake_up(&spu->stop_wq);
  135. return 0;
  136. }
  137. static int __spu_trap_tag_group(struct spu *spu)
  138. {
  139. pr_debug("%s\n", __FUNCTION__);
  140. /* wake_up(&spu->dma_wq); */
  141. return 0;
  142. }
  143. static int __spu_trap_spubox(struct spu *spu)
  144. {
  145. wake_up_all(&spu->wbox_wq);
  146. kill_fasync(&spu->wbox_fasync, SIGIO, POLLOUT);
  147. /* atomically disable SPU mailbox interrupts */
  148. spin_lock(&spu->register_lock);
  149. out_be64(&spu->priv1->int_mask_class2_RW,
  150. in_be64(&spu->priv1->int_mask_class2_RW) & ~0x10);
  151. spin_unlock(&spu->register_lock);
  152. return 0;
  153. }
  154. static irqreturn_t
  155. spu_irq_class_0(int irq, void *data, struct pt_regs *regs)
  156. {
  157. struct spu *spu;
  158. spu = data;
  159. spu->class_0_pending = 1;
  160. wake_up(&spu->stop_wq);
  161. return IRQ_HANDLED;
  162. }
  163. static int
  164. spu_irq_class_0_bottom(struct spu *spu)
  165. {
  166. unsigned long stat;
  167. spu->class_0_pending = 0;
  168. stat = in_be64(&spu->priv1->int_stat_class0_RW);
  169. if (stat & 1) /* invalid MFC DMA */
  170. __spu_trap_invalid_dma(spu);
  171. if (stat & 2) /* invalid DMA alignment */
  172. __spu_trap_dma_align(spu);
  173. if (stat & 4) /* error on SPU */
  174. __spu_trap_error(spu);
  175. out_be64(&spu->priv1->int_stat_class0_RW, stat);
  176. return 0;
  177. }
  178. static irqreturn_t
  179. spu_irq_class_1(int irq, void *data, struct pt_regs *regs)
  180. {
  181. struct spu *spu;
  182. unsigned long stat, dar;
  183. spu = data;
  184. stat = in_be64(&spu->priv1->int_stat_class1_RW);
  185. dar = in_be64(&spu->priv1->mfc_dar_RW);
  186. if (stat & 1) /* segment fault */
  187. __spu_trap_data_seg(spu, dar);
  188. if (stat & 2) { /* mapping fault */
  189. __spu_trap_data_map(spu, dar);
  190. }
  191. if (stat & 4) /* ls compare & suspend on get */
  192. ;
  193. if (stat & 8) /* ls compare & suspend on put */
  194. ;
  195. out_be64(&spu->priv1->int_stat_class1_RW, stat);
  196. return stat ? IRQ_HANDLED : IRQ_NONE;
  197. }
  198. static irqreturn_t
  199. spu_irq_class_2(int irq, void *data, struct pt_regs *regs)
  200. {
  201. struct spu *spu;
  202. unsigned long stat;
  203. spu = data;
  204. stat = in_be64(&spu->priv1->int_stat_class2_RW);
  205. pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat,
  206. in_be64(&spu->priv1->int_mask_class2_RW));
  207. if (stat & 1) /* PPC core mailbox */
  208. __spu_trap_mailbox(spu);
  209. if (stat & 2) /* SPU stop-and-signal */
  210. __spu_trap_stop(spu);
  211. if (stat & 4) /* SPU halted */
  212. __spu_trap_halt(spu);
  213. if (stat & 8) /* DMA tag group complete */
  214. __spu_trap_tag_group(spu);
  215. if (stat & 0x10) /* SPU mailbox threshold */
  216. __spu_trap_spubox(spu);
  217. out_be64(&spu->priv1->int_stat_class2_RW, stat);
  218. return stat ? IRQ_HANDLED : IRQ_NONE;
  219. }
  220. static int
  221. spu_request_irqs(struct spu *spu)
  222. {
  223. int ret;
  224. int irq_base;
  225. irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
  226. snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0", spu->number);
  227. ret = request_irq(irq_base + spu->isrc,
  228. spu_irq_class_0, 0, spu->irq_c0, spu);
  229. if (ret)
  230. goto out;
  231. out_be64(&spu->priv1->int_mask_class0_RW, 0x7);
  232. snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1", spu->number);
  233. ret = request_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc,
  234. spu_irq_class_1, 0, spu->irq_c1, spu);
  235. if (ret)
  236. goto out1;
  237. out_be64(&spu->priv1->int_mask_class1_RW, 0x3);
  238. snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2", spu->number);
  239. ret = request_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc,
  240. spu_irq_class_2, 0, spu->irq_c2, spu);
  241. if (ret)
  242. goto out2;
  243. out_be64(&spu->priv1->int_mask_class2_RW, 0xe);
  244. goto out;
  245. out2:
  246. free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
  247. out1:
  248. free_irq(irq_base + spu->isrc, spu);
  249. out:
  250. return ret;
  251. }
  252. static void
  253. spu_free_irqs(struct spu *spu)
  254. {
  255. int irq_base;
  256. irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
  257. free_irq(irq_base + spu->isrc, spu);
  258. free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
  259. free_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc, spu);
  260. }
  261. static LIST_HEAD(spu_list);
  262. static DECLARE_MUTEX(spu_mutex);
  263. static void spu_init_channels(struct spu *spu)
  264. {
  265. static const struct {
  266. unsigned channel;
  267. unsigned count;
  268. } zero_list[] = {
  269. { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
  270. { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
  271. }, count_list[] = {
  272. { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
  273. { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
  274. { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
  275. };
  276. struct spu_priv2 *priv2;
  277. int i;
  278. priv2 = spu->priv2;
  279. /* initialize all channel data to zero */
  280. for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
  281. int count;
  282. out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
  283. for (count = 0; count < zero_list[i].count; count++)
  284. out_be64(&priv2->spu_chnldata_RW, 0);
  285. }
  286. /* initialize channel counts to meaningful values */
  287. for (i = 0; i < ARRAY_SIZE(count_list); i++) {
  288. out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
  289. out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
  290. }
  291. }
  292. static void spu_init_regs(struct spu *spu)
  293. {
  294. out_be64(&spu->priv1->int_mask_class0_RW, 0x7);
  295. out_be64(&spu->priv1->int_mask_class1_RW, 0x3);
  296. out_be64(&spu->priv1->int_mask_class2_RW, 0xe);
  297. }
  298. struct spu *spu_alloc(void)
  299. {
  300. struct spu *spu;
  301. down(&spu_mutex);
  302. if (!list_empty(&spu_list)) {
  303. spu = list_entry(spu_list.next, struct spu, list);
  304. list_del_init(&spu->list);
  305. pr_debug("Got SPU %x %d\n", spu->isrc, spu->number);
  306. } else {
  307. pr_debug("No SPU left\n");
  308. spu = NULL;
  309. }
  310. up(&spu_mutex);
  311. if (spu) {
  312. spu_init_channels(spu);
  313. spu_init_regs(spu);
  314. }
  315. return spu;
  316. }
  317. EXPORT_SYMBOL(spu_alloc);
  318. void spu_free(struct spu *spu)
  319. {
  320. down(&spu_mutex);
  321. spu->ibox_fasync = NULL;
  322. spu->wbox_fasync = NULL;
  323. list_add_tail(&spu->list, &spu_list);
  324. up(&spu_mutex);
  325. }
  326. EXPORT_SYMBOL(spu_free);
  327. static int spu_handle_mm_fault(struct spu *spu)
  328. {
  329. struct spu_priv1 __iomem *priv1;
  330. struct mm_struct *mm = spu->mm;
  331. struct vm_area_struct *vma;
  332. u64 ea, dsisr, is_write;
  333. int ret;
  334. priv1 = spu->priv1;
  335. ea = in_be64(&priv1->mfc_dar_RW);
  336. dsisr = in_be64(&priv1->mfc_dsisr_RW);
  337. #if 0
  338. if (!IS_VALID_EA(ea)) {
  339. return -EFAULT;
  340. }
  341. #endif /* XXX */
  342. if (mm == NULL) {
  343. return -EFAULT;
  344. }
  345. if (mm->pgd == NULL) {
  346. return -EFAULT;
  347. }
  348. down_read(&mm->mmap_sem);
  349. vma = find_vma(mm, ea);
  350. if (!vma)
  351. goto bad_area;
  352. if (vma->vm_start <= ea)
  353. goto good_area;
  354. if (!(vma->vm_flags & VM_GROWSDOWN))
  355. goto bad_area;
  356. #if 0
  357. if (expand_stack(vma, ea))
  358. goto bad_area;
  359. #endif /* XXX */
  360. good_area:
  361. is_write = dsisr & MFC_DSISR_ACCESS_PUT;
  362. if (is_write) {
  363. if (!(vma->vm_flags & VM_WRITE))
  364. goto bad_area;
  365. } else {
  366. if (dsisr & MFC_DSISR_ACCESS_DENIED)
  367. goto bad_area;
  368. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  369. goto bad_area;
  370. }
  371. ret = 0;
  372. switch (handle_mm_fault(mm, vma, ea, is_write)) {
  373. case VM_FAULT_MINOR:
  374. current->min_flt++;
  375. break;
  376. case VM_FAULT_MAJOR:
  377. current->maj_flt++;
  378. break;
  379. case VM_FAULT_SIGBUS:
  380. ret = -EFAULT;
  381. goto bad_area;
  382. case VM_FAULT_OOM:
  383. ret = -ENOMEM;
  384. goto bad_area;
  385. default:
  386. BUG();
  387. }
  388. up_read(&mm->mmap_sem);
  389. return ret;
  390. bad_area:
  391. up_read(&mm->mmap_sem);
  392. return -EFAULT;
  393. }
  394. static int spu_handle_pte_fault(struct spu *spu)
  395. {
  396. struct spu_priv1 __iomem *priv1;
  397. u64 ea, dsisr, access, error = 0UL;
  398. int ret = 0;
  399. priv1 = spu->priv1;
  400. ea = in_be64(&priv1->mfc_dar_RW);
  401. dsisr = in_be64(&priv1->mfc_dsisr_RW);
  402. access = (_PAGE_PRESENT | _PAGE_USER);
  403. if (dsisr & MFC_DSISR_PTE_NOT_FOUND) {
  404. if (hash_page(ea, access, 0x300) != 0)
  405. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  406. }
  407. if ((error & CLASS1_ENABLE_STORAGE_FAULT_INTR) ||
  408. (dsisr & MFC_DSISR_ACCESS_DENIED)) {
  409. if ((ret = spu_handle_mm_fault(spu)) != 0)
  410. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  411. else
  412. error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
  413. }
  414. if (!error)
  415. spu_restart_dma(spu);
  416. return ret;
  417. }
  418. int spu_run(struct spu *spu)
  419. {
  420. struct spu_problem __iomem *prob;
  421. struct spu_priv1 __iomem *priv1;
  422. struct spu_priv2 __iomem *priv2;
  423. unsigned long status;
  424. int ret;
  425. prob = spu->problem;
  426. priv1 = spu->priv1;
  427. priv2 = spu->priv2;
  428. /* Let SPU run. */
  429. spu->mm = current->mm;
  430. eieio();
  431. out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
  432. do {
  433. ret = wait_event_interruptible(spu->stop_wq,
  434. (!((status = in_be32(&prob->spu_status_R)) & 0x1))
  435. || (in_be64(&priv1->mfc_dsisr_RW) & MFC_DSISR_PTE_NOT_FOUND)
  436. || spu->class_0_pending);
  437. if (status & SPU_STATUS_STOPPED_BY_STOP)
  438. ret = -EAGAIN;
  439. else if (status & SPU_STATUS_STOPPED_BY_HALT)
  440. ret = -EIO;
  441. else if (in_be64(&priv1->mfc_dsisr_RW) & MFC_DSISR_PTE_NOT_FOUND)
  442. ret = spu_handle_pte_fault(spu);
  443. if (spu->class_0_pending)
  444. spu_irq_class_0_bottom(spu);
  445. if (!ret && signal_pending(current))
  446. ret = -ERESTARTSYS;
  447. } while (!ret);
  448. /* Ensure SPU is stopped. */
  449. out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
  450. eieio();
  451. while (in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING)
  452. cpu_relax();
  453. out_be64(&priv2->slb_invalidate_all_W, 0);
  454. out_be64(&priv1->tlb_invalidate_entry_W, 0UL);
  455. eieio();
  456. spu->mm = NULL;
  457. /* Check for SPU breakpoint. */
  458. if (unlikely(current->ptrace & PT_PTRACED)) {
  459. status = in_be32(&prob->spu_status_R);
  460. if ((status & SPU_STATUS_STOPPED_BY_STOP)
  461. && status >> SPU_STOP_STATUS_SHIFT == 0x3fff) {
  462. force_sig(SIGTRAP, current);
  463. ret = -ERESTARTSYS;
  464. }
  465. }
  466. return ret;
  467. }
  468. EXPORT_SYMBOL(spu_run);
  469. static void __iomem * __init map_spe_prop(struct device_node *n,
  470. const char *name)
  471. {
  472. struct address_prop {
  473. unsigned long address;
  474. unsigned int len;
  475. } __attribute__((packed)) *prop;
  476. void *p;
  477. int proplen;
  478. p = get_property(n, name, &proplen);
  479. if (proplen != sizeof (struct address_prop))
  480. return NULL;
  481. prop = p;
  482. return ioremap(prop->address, prop->len);
  483. }
  484. static void spu_unmap(struct spu *spu)
  485. {
  486. iounmap(spu->priv2);
  487. iounmap(spu->priv1);
  488. iounmap(spu->problem);
  489. iounmap((u8 __iomem *)spu->local_store);
  490. }
  491. static int __init spu_map_device(struct spu *spu, struct device_node *spe)
  492. {
  493. char *prop;
  494. int ret;
  495. ret = -ENODEV;
  496. prop = get_property(spe, "isrc", NULL);
  497. if (!prop)
  498. goto out;
  499. spu->isrc = *(unsigned int *)prop;
  500. spu->name = get_property(spe, "name", NULL);
  501. if (!spu->name)
  502. goto out;
  503. prop = get_property(spe, "local-store", NULL);
  504. if (!prop)
  505. goto out;
  506. spu->local_store_phys = *(unsigned long *)prop;
  507. /* we use local store as ram, not io memory */
  508. spu->local_store = (void __force *)map_spe_prop(spe, "local-store");
  509. if (!spu->local_store)
  510. goto out;
  511. spu->problem= map_spe_prop(spe, "problem");
  512. if (!spu->problem)
  513. goto out_unmap;
  514. spu->priv1= map_spe_prop(spe, "priv1");
  515. if (!spu->priv1)
  516. goto out_unmap;
  517. spu->priv2= map_spe_prop(spe, "priv2");
  518. if (!spu->priv2)
  519. goto out_unmap;
  520. ret = 0;
  521. goto out;
  522. out_unmap:
  523. spu_unmap(spu);
  524. out:
  525. return ret;
  526. }
  527. static int __init find_spu_node_id(struct device_node *spe)
  528. {
  529. unsigned int *id;
  530. struct device_node *cpu;
  531. cpu = spe->parent->parent;
  532. id = (unsigned int *)get_property(cpu, "node-id", NULL);
  533. return id ? *id : 0;
  534. }
  535. static int __init create_spu(struct device_node *spe)
  536. {
  537. struct spu *spu;
  538. int ret;
  539. static int number;
  540. ret = -ENOMEM;
  541. spu = kmalloc(sizeof (*spu), GFP_KERNEL);
  542. if (!spu)
  543. goto out;
  544. ret = spu_map_device(spu, spe);
  545. if (ret)
  546. goto out_free;
  547. spu->node = find_spu_node_id(spe);
  548. spu->stop_code = 0;
  549. spu->slb_replace = 0;
  550. spu->mm = NULL;
  551. spu->class_0_pending = 0;
  552. spu->flags = 0UL;
  553. spin_lock_init(&spu->register_lock);
  554. out_be64(&spu->priv1->mfc_sdr_RW, mfspr(SPRN_SDR1));
  555. out_be64(&spu->priv1->mfc_sr1_RW, 0x33);
  556. init_waitqueue_head(&spu->stop_wq);
  557. init_waitqueue_head(&spu->wbox_wq);
  558. init_waitqueue_head(&spu->ibox_wq);
  559. spu->ibox_fasync = NULL;
  560. spu->wbox_fasync = NULL;
  561. down(&spu_mutex);
  562. spu->number = number++;
  563. ret = spu_request_irqs(spu);
  564. if (ret)
  565. goto out_unmap;
  566. list_add(&spu->list, &spu_list);
  567. up(&spu_mutex);
  568. pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n",
  569. spu->name, spu->isrc, spu->local_store,
  570. spu->problem, spu->priv1, spu->priv2, spu->number);
  571. goto out;
  572. out_unmap:
  573. up(&spu_mutex);
  574. spu_unmap(spu);
  575. out_free:
  576. kfree(spu);
  577. out:
  578. return ret;
  579. }
  580. static void destroy_spu(struct spu *spu)
  581. {
  582. list_del_init(&spu->list);
  583. spu_free_irqs(spu);
  584. spu_unmap(spu);
  585. kfree(spu);
  586. }
  587. static void cleanup_spu_base(void)
  588. {
  589. struct spu *spu, *tmp;
  590. down(&spu_mutex);
  591. list_for_each_entry_safe(spu, tmp, &spu_list, list)
  592. destroy_spu(spu);
  593. up(&spu_mutex);
  594. }
  595. module_exit(cleanup_spu_base);
  596. static int __init init_spu_base(void)
  597. {
  598. struct device_node *node;
  599. int ret;
  600. ret = -ENODEV;
  601. for (node = of_find_node_by_type(NULL, "spe");
  602. node; node = of_find_node_by_type(node, "spe")) {
  603. ret = create_spu(node);
  604. if (ret) {
  605. printk(KERN_WARNING "%s: Error initializing %s\n",
  606. __FUNCTION__, node->name);
  607. cleanup_spu_base();
  608. break;
  609. }
  610. }
  611. /* in some old firmware versions, the spe is called 'spc', so we
  612. look for that as well */
  613. for (node = of_find_node_by_type(NULL, "spc");
  614. node; node = of_find_node_by_type(node, "spc")) {
  615. ret = create_spu(node);
  616. if (ret) {
  617. printk(KERN_WARNING "%s: Error initializing %s\n",
  618. __FUNCTION__, node->name);
  619. cleanup_spu_base();
  620. break;
  621. }
  622. }
  623. return ret;
  624. }
  625. module_init(init_spu_base);
  626. MODULE_LICENSE("GPL");
  627. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");