spu_base.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. #undef DEBUG
  23. #include <linux/interrupt.h>
  24. #include <linux/list.h>
  25. #include <linux/module.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/slab.h>
  28. #include <linux/wait.h>
  29. #include <linux/mm.h>
  30. #include <linux/io.h>
  31. #include <linux/mutex.h>
  32. #include <asm/spu.h>
  33. #include <asm/spu_priv1.h>
  34. #include <asm/xmon.h>
  35. const struct spu_management_ops *spu_management_ops;
  36. const struct spu_priv1_ops *spu_priv1_ops;
  37. static struct list_head spu_list[MAX_NUMNODES];
  38. static LIST_HEAD(spu_full_list);
  39. static DEFINE_MUTEX(spu_mutex);
  40. static spinlock_t spu_list_lock = SPIN_LOCK_UNLOCKED;
  41. EXPORT_SYMBOL_GPL(spu_priv1_ops);
  42. void spu_invalidate_slbs(struct spu *spu)
  43. {
  44. struct spu_priv2 __iomem *priv2 = spu->priv2;
  45. if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
  46. out_be64(&priv2->slb_invalidate_all_W, 0UL);
  47. }
  48. EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
  49. /* This is called by the MM core when a segment size is changed, to
  50. * request a flush of all the SPEs using a given mm
  51. */
  52. void spu_flush_all_slbs(struct mm_struct *mm)
  53. {
  54. struct spu *spu;
  55. unsigned long flags;
  56. spin_lock_irqsave(&spu_list_lock, flags);
  57. list_for_each_entry(spu, &spu_full_list, full_list) {
  58. if (spu->mm == mm)
  59. spu_invalidate_slbs(spu);
  60. }
  61. spin_unlock_irqrestore(&spu_list_lock, flags);
  62. }
  63. /* The hack below stinks... try to do something better one of
  64. * these days... Does it even work properly with NR_CPUS == 1 ?
  65. */
  66. static inline void mm_needs_global_tlbie(struct mm_struct *mm)
  67. {
  68. int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
  69. /* Global TLBIE broadcast required with SPEs. */
  70. __cpus_setall(&mm->cpu_vm_mask, nr);
  71. }
  72. void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
  73. {
  74. unsigned long flags;
  75. spin_lock_irqsave(&spu_list_lock, flags);
  76. spu->mm = mm;
  77. spin_unlock_irqrestore(&spu_list_lock, flags);
  78. if (mm)
  79. mm_needs_global_tlbie(mm);
  80. }
  81. EXPORT_SYMBOL_GPL(spu_associate_mm);
  82. static int __spu_trap_invalid_dma(struct spu *spu)
  83. {
  84. pr_debug("%s\n", __FUNCTION__);
  85. spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
  86. return 0;
  87. }
  88. static int __spu_trap_dma_align(struct spu *spu)
  89. {
  90. pr_debug("%s\n", __FUNCTION__);
  91. spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
  92. return 0;
  93. }
  94. static int __spu_trap_error(struct spu *spu)
  95. {
  96. pr_debug("%s\n", __FUNCTION__);
  97. spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
  98. return 0;
  99. }
  100. static void spu_restart_dma(struct spu *spu)
  101. {
  102. struct spu_priv2 __iomem *priv2 = spu->priv2;
  103. if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
  104. out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
  105. }
  106. static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
  107. {
  108. struct spu_priv2 __iomem *priv2 = spu->priv2;
  109. struct mm_struct *mm = spu->mm;
  110. u64 esid, vsid, llp;
  111. int psize;
  112. pr_debug("%s\n", __FUNCTION__);
  113. if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
  114. /* SLBs are pre-loaded for context switch, so
  115. * we should never get here!
  116. */
  117. printk("%s: invalid access during switch!\n", __func__);
  118. return 1;
  119. }
  120. esid = (ea & ESID_MASK) | SLB_ESID_V;
  121. switch(REGION_ID(ea)) {
  122. case USER_REGION_ID:
  123. #ifdef CONFIG_HUGETLB_PAGE
  124. if (in_hugepage_area(mm->context, ea))
  125. psize = mmu_huge_psize;
  126. else
  127. #endif
  128. psize = mm->context.user_psize;
  129. vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
  130. SLB_VSID_USER;
  131. break;
  132. case VMALLOC_REGION_ID:
  133. if (ea < VMALLOC_END)
  134. psize = mmu_vmalloc_psize;
  135. else
  136. psize = mmu_io_psize;
  137. vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
  138. SLB_VSID_KERNEL;
  139. break;
  140. case KERNEL_REGION_ID:
  141. psize = mmu_linear_psize;
  142. vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
  143. SLB_VSID_KERNEL;
  144. break;
  145. default:
  146. /* Future: support kernel segments so that drivers
  147. * can use SPUs.
  148. */
  149. pr_debug("invalid region access at %016lx\n", ea);
  150. return 1;
  151. }
  152. llp = mmu_psize_defs[psize].sllp;
  153. out_be64(&priv2->slb_index_W, spu->slb_replace);
  154. out_be64(&priv2->slb_vsid_RW, vsid | llp);
  155. out_be64(&priv2->slb_esid_RW, esid);
  156. spu->slb_replace++;
  157. if (spu->slb_replace >= 8)
  158. spu->slb_replace = 0;
  159. spu_restart_dma(spu);
  160. return 0;
  161. }
  162. extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
  163. static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
  164. {
  165. pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
  166. /* Handle kernel space hash faults immediately.
  167. User hash faults need to be deferred to process context. */
  168. if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
  169. && REGION_ID(ea) != USER_REGION_ID
  170. && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
  171. spu_restart_dma(spu);
  172. return 0;
  173. }
  174. if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
  175. printk("%s: invalid access during switch!\n", __func__);
  176. return 1;
  177. }
  178. spu->dar = ea;
  179. spu->dsisr = dsisr;
  180. mb();
  181. spu->stop_callback(spu);
  182. return 0;
  183. }
  184. static irqreturn_t
  185. spu_irq_class_0(int irq, void *data)
  186. {
  187. struct spu *spu;
  188. spu = data;
  189. spu->class_0_pending = 1;
  190. spu->stop_callback(spu);
  191. return IRQ_HANDLED;
  192. }
  193. int
  194. spu_irq_class_0_bottom(struct spu *spu)
  195. {
  196. unsigned long stat, mask;
  197. unsigned long flags;
  198. spu->class_0_pending = 0;
  199. spin_lock_irqsave(&spu->register_lock, flags);
  200. mask = spu_int_mask_get(spu, 0);
  201. stat = spu_int_stat_get(spu, 0);
  202. stat &= mask;
  203. if (stat & 1) /* invalid DMA alignment */
  204. __spu_trap_dma_align(spu);
  205. if (stat & 2) /* invalid MFC DMA */
  206. __spu_trap_invalid_dma(spu);
  207. if (stat & 4) /* error on SPU */
  208. __spu_trap_error(spu);
  209. spu_int_stat_clear(spu, 0, stat);
  210. spin_unlock_irqrestore(&spu->register_lock, flags);
  211. return (stat & 0x7) ? -EIO : 0;
  212. }
  213. EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
  214. static irqreturn_t
  215. spu_irq_class_1(int irq, void *data)
  216. {
  217. struct spu *spu;
  218. unsigned long stat, mask, dar, dsisr;
  219. spu = data;
  220. /* atomically read & clear class1 status. */
  221. spin_lock(&spu->register_lock);
  222. mask = spu_int_mask_get(spu, 1);
  223. stat = spu_int_stat_get(spu, 1) & mask;
  224. dar = spu_mfc_dar_get(spu);
  225. dsisr = spu_mfc_dsisr_get(spu);
  226. if (stat & 2) /* mapping fault */
  227. spu_mfc_dsisr_set(spu, 0ul);
  228. spu_int_stat_clear(spu, 1, stat);
  229. spin_unlock(&spu->register_lock);
  230. pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
  231. dar, dsisr);
  232. if (stat & 1) /* segment fault */
  233. __spu_trap_data_seg(spu, dar);
  234. if (stat & 2) { /* mapping fault */
  235. __spu_trap_data_map(spu, dar, dsisr);
  236. }
  237. if (stat & 4) /* ls compare & suspend on get */
  238. ;
  239. if (stat & 8) /* ls compare & suspend on put */
  240. ;
  241. return stat ? IRQ_HANDLED : IRQ_NONE;
  242. }
  243. EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom);
  244. static irqreturn_t
  245. spu_irq_class_2(int irq, void *data)
  246. {
  247. struct spu *spu;
  248. unsigned long stat;
  249. unsigned long mask;
  250. spu = data;
  251. spin_lock(&spu->register_lock);
  252. stat = spu_int_stat_get(spu, 2);
  253. mask = spu_int_mask_get(spu, 2);
  254. /* ignore interrupts we're not waiting for */
  255. stat &= mask;
  256. /*
  257. * mailbox interrupts (0x1 and 0x10) are level triggered.
  258. * mask them now before acknowledging.
  259. */
  260. if (stat & 0x11)
  261. spu_int_mask_and(spu, 2, ~(stat & 0x11));
  262. /* acknowledge all interrupts before the callbacks */
  263. spu_int_stat_clear(spu, 2, stat);
  264. spin_unlock(&spu->register_lock);
  265. pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
  266. if (stat & 1) /* PPC core mailbox */
  267. spu->ibox_callback(spu);
  268. if (stat & 2) /* SPU stop-and-signal */
  269. spu->stop_callback(spu);
  270. if (stat & 4) /* SPU halted */
  271. spu->stop_callback(spu);
  272. if (stat & 8) /* DMA tag group complete */
  273. spu->mfc_callback(spu);
  274. if (stat & 0x10) /* SPU mailbox threshold */
  275. spu->wbox_callback(spu);
  276. return stat ? IRQ_HANDLED : IRQ_NONE;
  277. }
  278. static int spu_request_irqs(struct spu *spu)
  279. {
  280. int ret = 0;
  281. if (spu->irqs[0] != NO_IRQ) {
  282. snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
  283. spu->number);
  284. ret = request_irq(spu->irqs[0], spu_irq_class_0,
  285. IRQF_DISABLED,
  286. spu->irq_c0, spu);
  287. if (ret)
  288. goto bail0;
  289. }
  290. if (spu->irqs[1] != NO_IRQ) {
  291. snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
  292. spu->number);
  293. ret = request_irq(spu->irqs[1], spu_irq_class_1,
  294. IRQF_DISABLED,
  295. spu->irq_c1, spu);
  296. if (ret)
  297. goto bail1;
  298. }
  299. if (spu->irqs[2] != NO_IRQ) {
  300. snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
  301. spu->number);
  302. ret = request_irq(spu->irqs[2], spu_irq_class_2,
  303. IRQF_DISABLED,
  304. spu->irq_c2, spu);
  305. if (ret)
  306. goto bail2;
  307. }
  308. return 0;
  309. bail2:
  310. if (spu->irqs[1] != NO_IRQ)
  311. free_irq(spu->irqs[1], spu);
  312. bail1:
  313. if (spu->irqs[0] != NO_IRQ)
  314. free_irq(spu->irqs[0], spu);
  315. bail0:
  316. return ret;
  317. }
  318. static void spu_free_irqs(struct spu *spu)
  319. {
  320. if (spu->irqs[0] != NO_IRQ)
  321. free_irq(spu->irqs[0], spu);
  322. if (spu->irqs[1] != NO_IRQ)
  323. free_irq(spu->irqs[1], spu);
  324. if (spu->irqs[2] != NO_IRQ)
  325. free_irq(spu->irqs[2], spu);
  326. }
  327. static void spu_init_channels(struct spu *spu)
  328. {
  329. static const struct {
  330. unsigned channel;
  331. unsigned count;
  332. } zero_list[] = {
  333. { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
  334. { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
  335. }, count_list[] = {
  336. { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
  337. { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
  338. { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
  339. };
  340. struct spu_priv2 __iomem *priv2;
  341. int i;
  342. priv2 = spu->priv2;
  343. /* initialize all channel data to zero */
  344. for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
  345. int count;
  346. out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
  347. for (count = 0; count < zero_list[i].count; count++)
  348. out_be64(&priv2->spu_chnldata_RW, 0);
  349. }
  350. /* initialize channel counts to meaningful values */
  351. for (i = 0; i < ARRAY_SIZE(count_list); i++) {
  352. out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
  353. out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
  354. }
  355. }
  356. struct spu *spu_alloc_node(int node)
  357. {
  358. struct spu *spu = NULL;
  359. mutex_lock(&spu_mutex);
  360. if (!list_empty(&spu_list[node])) {
  361. spu = list_entry(spu_list[node].next, struct spu, list);
  362. list_del_init(&spu->list);
  363. pr_debug("Got SPU %d %d\n", spu->number, spu->node);
  364. spu_init_channels(spu);
  365. }
  366. mutex_unlock(&spu_mutex);
  367. return spu;
  368. }
  369. EXPORT_SYMBOL_GPL(spu_alloc_node);
  370. struct spu *spu_alloc(void)
  371. {
  372. struct spu *spu = NULL;
  373. int node;
  374. for (node = 0; node < MAX_NUMNODES; node++) {
  375. spu = spu_alloc_node(node);
  376. if (spu)
  377. break;
  378. }
  379. return spu;
  380. }
  381. void spu_free(struct spu *spu)
  382. {
  383. mutex_lock(&spu_mutex);
  384. list_add_tail(&spu->list, &spu_list[spu->node]);
  385. mutex_unlock(&spu_mutex);
  386. }
  387. EXPORT_SYMBOL_GPL(spu_free);
  388. static int spu_handle_mm_fault(struct spu *spu)
  389. {
  390. struct mm_struct *mm = spu->mm;
  391. struct vm_area_struct *vma;
  392. u64 ea, dsisr, is_write;
  393. int ret;
  394. ea = spu->dar;
  395. dsisr = spu->dsisr;
  396. #if 0
  397. if (!IS_VALID_EA(ea)) {
  398. return -EFAULT;
  399. }
  400. #endif /* XXX */
  401. if (mm == NULL) {
  402. return -EFAULT;
  403. }
  404. if (mm->pgd == NULL) {
  405. return -EFAULT;
  406. }
  407. down_read(&mm->mmap_sem);
  408. vma = find_vma(mm, ea);
  409. if (!vma)
  410. goto bad_area;
  411. if (vma->vm_start <= ea)
  412. goto good_area;
  413. if (!(vma->vm_flags & VM_GROWSDOWN))
  414. goto bad_area;
  415. #if 0
  416. if (expand_stack(vma, ea))
  417. goto bad_area;
  418. #endif /* XXX */
  419. good_area:
  420. is_write = dsisr & MFC_DSISR_ACCESS_PUT;
  421. if (is_write) {
  422. if (!(vma->vm_flags & VM_WRITE))
  423. goto bad_area;
  424. } else {
  425. if (dsisr & MFC_DSISR_ACCESS_DENIED)
  426. goto bad_area;
  427. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  428. goto bad_area;
  429. }
  430. ret = 0;
  431. switch (handle_mm_fault(mm, vma, ea, is_write)) {
  432. case VM_FAULT_MINOR:
  433. current->min_flt++;
  434. break;
  435. case VM_FAULT_MAJOR:
  436. current->maj_flt++;
  437. break;
  438. case VM_FAULT_SIGBUS:
  439. ret = -EFAULT;
  440. goto bad_area;
  441. case VM_FAULT_OOM:
  442. ret = -ENOMEM;
  443. goto bad_area;
  444. default:
  445. BUG();
  446. }
  447. up_read(&mm->mmap_sem);
  448. return ret;
  449. bad_area:
  450. up_read(&mm->mmap_sem);
  451. return -EFAULT;
  452. }
  453. int spu_irq_class_1_bottom(struct spu *spu)
  454. {
  455. u64 ea, dsisr, access, error = 0UL;
  456. int ret = 0;
  457. ea = spu->dar;
  458. dsisr = spu->dsisr;
  459. if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) {
  460. u64 flags;
  461. access = (_PAGE_PRESENT | _PAGE_USER);
  462. access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
  463. local_irq_save(flags);
  464. if (hash_page(ea, access, 0x300) != 0)
  465. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  466. local_irq_restore(flags);
  467. }
  468. if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) {
  469. if ((ret = spu_handle_mm_fault(spu)) != 0)
  470. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  471. else
  472. error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
  473. }
  474. spu->dar = 0UL;
  475. spu->dsisr = 0UL;
  476. if (!error) {
  477. spu_restart_dma(spu);
  478. } else {
  479. spu->dma_callback(spu, SPE_EVENT_SPE_DATA_STORAGE);
  480. }
  481. return ret;
  482. }
  483. struct sysdev_class spu_sysdev_class = {
  484. set_kset_name("spu")
  485. };
  486. int spu_add_sysdev_attr(struct sysdev_attribute *attr)
  487. {
  488. struct spu *spu;
  489. mutex_lock(&spu_mutex);
  490. list_for_each_entry(spu, &spu_full_list, full_list)
  491. sysdev_create_file(&spu->sysdev, attr);
  492. mutex_unlock(&spu_mutex);
  493. return 0;
  494. }
  495. EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
  496. int spu_add_sysdev_attr_group(struct attribute_group *attrs)
  497. {
  498. struct spu *spu;
  499. mutex_lock(&spu_mutex);
  500. list_for_each_entry(spu, &spu_full_list, full_list)
  501. sysfs_create_group(&spu->sysdev.kobj, attrs);
  502. mutex_unlock(&spu_mutex);
  503. return 0;
  504. }
  505. EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
  506. void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
  507. {
  508. struct spu *spu;
  509. mutex_lock(&spu_mutex);
  510. list_for_each_entry(spu, &spu_full_list, full_list)
  511. sysdev_remove_file(&spu->sysdev, attr);
  512. mutex_unlock(&spu_mutex);
  513. }
  514. EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
  515. void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
  516. {
  517. struct spu *spu;
  518. mutex_lock(&spu_mutex);
  519. list_for_each_entry(spu, &spu_full_list, full_list)
  520. sysfs_remove_group(&spu->sysdev.kobj, attrs);
  521. mutex_unlock(&spu_mutex);
  522. }
  523. EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
  524. static int spu_create_sysdev(struct spu *spu)
  525. {
  526. int ret;
  527. spu->sysdev.id = spu->number;
  528. spu->sysdev.cls = &spu_sysdev_class;
  529. ret = sysdev_register(&spu->sysdev);
  530. if (ret) {
  531. printk(KERN_ERR "Can't register SPU %d with sysfs\n",
  532. spu->number);
  533. return ret;
  534. }
  535. sysfs_add_device_to_node(&spu->sysdev, spu->node);
  536. return 0;
  537. }
  538. static void spu_destroy_sysdev(struct spu *spu)
  539. {
  540. sysfs_remove_device_from_node(&spu->sysdev, spu->node);
  541. sysdev_unregister(&spu->sysdev);
  542. }
  543. static int __init create_spu(void *data)
  544. {
  545. struct spu *spu;
  546. int ret;
  547. static int number;
  548. unsigned long flags;
  549. ret = -ENOMEM;
  550. spu = kzalloc(sizeof (*spu), GFP_KERNEL);
  551. if (!spu)
  552. goto out;
  553. spin_lock_init(&spu->register_lock);
  554. mutex_lock(&spu_mutex);
  555. spu->number = number++;
  556. mutex_unlock(&spu_mutex);
  557. ret = spu_create_spu(spu, data);
  558. if (ret)
  559. goto out_free;
  560. spu_mfc_sdr_setup(spu);
  561. spu_mfc_sr1_set(spu, 0x33);
  562. ret = spu_request_irqs(spu);
  563. if (ret)
  564. goto out_destroy;
  565. ret = spu_create_sysdev(spu);
  566. if (ret)
  567. goto out_free_irqs;
  568. mutex_lock(&spu_mutex);
  569. spin_lock_irqsave(&spu_list_lock, flags);
  570. list_add(&spu->list, &spu_list[spu->node]);
  571. list_add(&spu->full_list, &spu_full_list);
  572. spin_unlock_irqrestore(&spu_list_lock, flags);
  573. mutex_unlock(&spu_mutex);
  574. goto out;
  575. out_free_irqs:
  576. spu_free_irqs(spu);
  577. out_destroy:
  578. spu_destroy_spu(spu);
  579. out_free:
  580. kfree(spu);
  581. out:
  582. return ret;
  583. }
  584. static void destroy_spu(struct spu *spu)
  585. {
  586. list_del_init(&spu->list);
  587. list_del_init(&spu->full_list);
  588. spu_destroy_sysdev(spu);
  589. spu_free_irqs(spu);
  590. spu_destroy_spu(spu);
  591. kfree(spu);
  592. }
  593. static void cleanup_spu_base(void)
  594. {
  595. struct spu *spu, *tmp;
  596. int node;
  597. mutex_lock(&spu_mutex);
  598. for (node = 0; node < MAX_NUMNODES; node++) {
  599. list_for_each_entry_safe(spu, tmp, &spu_list[node], list)
  600. destroy_spu(spu);
  601. }
  602. mutex_unlock(&spu_mutex);
  603. sysdev_class_unregister(&spu_sysdev_class);
  604. }
  605. module_exit(cleanup_spu_base);
  606. static int __init init_spu_base(void)
  607. {
  608. int i, ret;
  609. if (!spu_management_ops)
  610. return 0;
  611. /* create sysdev class for spus */
  612. ret = sysdev_class_register(&spu_sysdev_class);
  613. if (ret)
  614. return ret;
  615. for (i = 0; i < MAX_NUMNODES; i++)
  616. INIT_LIST_HEAD(&spu_list[i]);
  617. ret = spu_enumerate_spus(create_spu);
  618. if (ret) {
  619. printk(KERN_WARNING "%s: Error initializing spus\n",
  620. __FUNCTION__);
  621. cleanup_spu_base();
  622. return ret;
  623. }
  624. xmon_register_spus(&spu_full_list);
  625. return ret;
  626. }
  627. module_init(init_spu_base);
  628. MODULE_LICENSE("GPL");
  629. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");