spu_base.c 17 KB

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