spu_base.c 16 KB

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