spu_base.c 18 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 <linux/linux_logo.h>
  33. #include <asm/spu.h>
  34. #include <asm/spu_priv1.h>
  35. #include <asm/spu_csa.h>
  36. #include <asm/xmon.h>
  37. #include <asm/prom.h>
  38. const struct spu_management_ops *spu_management_ops;
  39. EXPORT_SYMBOL_GPL(spu_management_ops);
  40. const struct spu_priv1_ops *spu_priv1_ops;
  41. EXPORT_SYMBOL_GPL(spu_priv1_ops);
  42. struct cbe_spu_info cbe_spu_info[MAX_NUMNODES];
  43. EXPORT_SYMBOL_GPL(cbe_spu_info);
  44. /*
  45. * The spufs fault-handling code needs to call force_sig_info to raise signals
  46. * on DMA errors. Export it here to avoid general kernel-wide access to this
  47. * function
  48. */
  49. EXPORT_SYMBOL_GPL(force_sig_info);
  50. /*
  51. * Protects cbe_spu_info and spu->number.
  52. */
  53. static DEFINE_SPINLOCK(spu_lock);
  54. /*
  55. * List of all spus in the system.
  56. *
  57. * This list is iterated by callers from irq context and callers that
  58. * want to sleep. Thus modifications need to be done with both
  59. * spu_full_list_lock and spu_full_list_mutex held, while iterating
  60. * through it requires either of these locks.
  61. *
  62. * In addition spu_full_list_lock protects all assignmens to
  63. * spu->mm.
  64. */
  65. static LIST_HEAD(spu_full_list);
  66. static DEFINE_SPINLOCK(spu_full_list_lock);
  67. static DEFINE_MUTEX(spu_full_list_mutex);
  68. struct spu_slb {
  69. u64 esid, vsid;
  70. };
  71. void spu_invalidate_slbs(struct spu *spu)
  72. {
  73. struct spu_priv2 __iomem *priv2 = spu->priv2;
  74. if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
  75. out_be64(&priv2->slb_invalidate_all_W, 0UL);
  76. }
  77. EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
  78. /* This is called by the MM core when a segment size is changed, to
  79. * request a flush of all the SPEs using a given mm
  80. */
  81. void spu_flush_all_slbs(struct mm_struct *mm)
  82. {
  83. struct spu *spu;
  84. unsigned long flags;
  85. spin_lock_irqsave(&spu_full_list_lock, flags);
  86. list_for_each_entry(spu, &spu_full_list, full_list) {
  87. if (spu->mm == mm)
  88. spu_invalidate_slbs(spu);
  89. }
  90. spin_unlock_irqrestore(&spu_full_list_lock, flags);
  91. }
  92. /* The hack below stinks... try to do something better one of
  93. * these days... Does it even work properly with NR_CPUS == 1 ?
  94. */
  95. static inline void mm_needs_global_tlbie(struct mm_struct *mm)
  96. {
  97. int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
  98. /* Global TLBIE broadcast required with SPEs. */
  99. __cpus_setall(&mm->cpu_vm_mask, nr);
  100. }
  101. void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
  102. {
  103. unsigned long flags;
  104. spin_lock_irqsave(&spu_full_list_lock, flags);
  105. spu->mm = mm;
  106. spin_unlock_irqrestore(&spu_full_list_lock, flags);
  107. if (mm)
  108. mm_needs_global_tlbie(mm);
  109. }
  110. EXPORT_SYMBOL_GPL(spu_associate_mm);
  111. int spu_64k_pages_available(void)
  112. {
  113. return mmu_psize_defs[MMU_PAGE_64K].shift != 0;
  114. }
  115. EXPORT_SYMBOL_GPL(spu_64k_pages_available);
  116. static void spu_restart_dma(struct spu *spu)
  117. {
  118. struct spu_priv2 __iomem *priv2 = spu->priv2;
  119. if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
  120. out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
  121. }
  122. static inline void spu_load_slb(struct spu *spu, int slbe, struct spu_slb *slb)
  123. {
  124. struct spu_priv2 __iomem *priv2 = spu->priv2;
  125. pr_debug("%s: adding SLB[%d] 0x%016lx 0x%016lx\n",
  126. __func__, slbe, slb->vsid, slb->esid);
  127. out_be64(&priv2->slb_index_W, slbe);
  128. out_be64(&priv2->slb_vsid_RW, slb->vsid);
  129. out_be64(&priv2->slb_esid_RW, slb->esid);
  130. }
  131. static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
  132. {
  133. struct mm_struct *mm = spu->mm;
  134. struct spu_slb slb;
  135. int psize;
  136. pr_debug("%s\n", __FUNCTION__);
  137. slb.esid = (ea & ESID_MASK) | SLB_ESID_V;
  138. switch(REGION_ID(ea)) {
  139. case USER_REGION_ID:
  140. #ifdef CONFIG_PPC_MM_SLICES
  141. psize = get_slice_psize(mm, ea);
  142. #else
  143. psize = mm->context.user_psize;
  144. #endif
  145. slb.vsid = (get_vsid(mm->context.id, ea, MMU_SEGSIZE_256M)
  146. << SLB_VSID_SHIFT) | SLB_VSID_USER;
  147. break;
  148. case VMALLOC_REGION_ID:
  149. if (ea < VMALLOC_END)
  150. psize = mmu_vmalloc_psize;
  151. else
  152. psize = mmu_io_psize;
  153. slb.vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M)
  154. << SLB_VSID_SHIFT) | SLB_VSID_KERNEL;
  155. break;
  156. case KERNEL_REGION_ID:
  157. psize = mmu_linear_psize;
  158. slb.vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M)
  159. << SLB_VSID_SHIFT) | SLB_VSID_KERNEL;
  160. break;
  161. default:
  162. /* Future: support kernel segments so that drivers
  163. * can use SPUs.
  164. */
  165. pr_debug("invalid region access at %016lx\n", ea);
  166. return 1;
  167. }
  168. slb.vsid |= mmu_psize_defs[psize].sllp;
  169. spu_load_slb(spu, spu->slb_replace, &slb);
  170. spu->slb_replace++;
  171. if (spu->slb_replace >= 8)
  172. spu->slb_replace = 0;
  173. spu_restart_dma(spu);
  174. spu->stats.slb_flt++;
  175. return 0;
  176. }
  177. extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
  178. static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
  179. {
  180. pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
  181. /* Handle kernel space hash faults immediately.
  182. User hash faults need to be deferred to process context. */
  183. if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
  184. && REGION_ID(ea) != USER_REGION_ID
  185. && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
  186. spu_restart_dma(spu);
  187. return 0;
  188. }
  189. spu->class_0_pending = 0;
  190. spu->dar = ea;
  191. spu->dsisr = dsisr;
  192. spu->stop_callback(spu);
  193. return 0;
  194. }
  195. static void __spu_kernel_slb(void *addr, struct spu_slb *slb)
  196. {
  197. unsigned long ea = (unsigned long)addr;
  198. u64 llp;
  199. if (REGION_ID(ea) == KERNEL_REGION_ID)
  200. llp = mmu_psize_defs[mmu_linear_psize].sllp;
  201. else
  202. llp = mmu_psize_defs[mmu_virtual_psize].sllp;
  203. slb->vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M) << SLB_VSID_SHIFT) |
  204. SLB_VSID_KERNEL | llp;
  205. slb->esid = (ea & ESID_MASK) | SLB_ESID_V;
  206. }
  207. /**
  208. * Given an array of @nr_slbs SLB entries, @slbs, return non-zero if the
  209. * address @new_addr is present.
  210. */
  211. static inline int __slb_present(struct spu_slb *slbs, int nr_slbs,
  212. void *new_addr)
  213. {
  214. unsigned long ea = (unsigned long)new_addr;
  215. int i;
  216. for (i = 0; i < nr_slbs; i++)
  217. if (!((slbs[i].esid ^ ea) & ESID_MASK))
  218. return 1;
  219. return 0;
  220. }
  221. /**
  222. * Setup the SPU kernel SLBs, in preparation for a context save/restore. We
  223. * need to map both the context save area, and the save/restore code.
  224. *
  225. * Because the lscsa and code may cross segment boundaires, we check to see
  226. * if mappings are required for the start and end of each range. We currently
  227. * assume that the mappings are smaller that one segment - if not, something
  228. * is seriously wrong.
  229. */
  230. void spu_setup_kernel_slbs(struct spu *spu, struct spu_lscsa *lscsa,
  231. void *code, int code_size)
  232. {
  233. struct spu_slb slbs[4];
  234. int i, nr_slbs = 0;
  235. /* start and end addresses of both mappings */
  236. void *addrs[] = {
  237. lscsa, (void *)lscsa + sizeof(*lscsa) - 1,
  238. code, code + code_size - 1
  239. };
  240. /* check the set of addresses, and create a new entry in the slbs array
  241. * if there isn't already a SLB for that address */
  242. for (i = 0; i < ARRAY_SIZE(addrs); i++) {
  243. if (__slb_present(slbs, nr_slbs, addrs[i]))
  244. continue;
  245. __spu_kernel_slb(addrs[i], &slbs[nr_slbs]);
  246. nr_slbs++;
  247. }
  248. /* Add the set of SLBs */
  249. for (i = 0; i < nr_slbs; i++)
  250. spu_load_slb(spu, i, &slbs[i]);
  251. }
  252. EXPORT_SYMBOL_GPL(spu_setup_kernel_slbs);
  253. static irqreturn_t
  254. spu_irq_class_0(int irq, void *data)
  255. {
  256. struct spu *spu;
  257. unsigned long stat, mask;
  258. spu = data;
  259. spin_lock(&spu->register_lock);
  260. mask = spu_int_mask_get(spu, 0);
  261. stat = spu_int_stat_get(spu, 0) & mask;
  262. spu->class_0_pending |= stat;
  263. spu->dsisr = spu_mfc_dsisr_get(spu);
  264. spu->dar = spu_mfc_dar_get(spu);
  265. spin_unlock(&spu->register_lock);
  266. spu->stop_callback(spu);
  267. spu_int_stat_clear(spu, 0, stat);
  268. return IRQ_HANDLED;
  269. }
  270. static irqreturn_t
  271. spu_irq_class_1(int irq, void *data)
  272. {
  273. struct spu *spu;
  274. unsigned long stat, mask, dar, dsisr;
  275. spu = data;
  276. /* atomically read & clear class1 status. */
  277. spin_lock(&spu->register_lock);
  278. mask = spu_int_mask_get(spu, 1);
  279. stat = spu_int_stat_get(spu, 1) & mask;
  280. dar = spu_mfc_dar_get(spu);
  281. dsisr = spu_mfc_dsisr_get(spu);
  282. if (stat & CLASS1_STORAGE_FAULT_INTR)
  283. spu_mfc_dsisr_set(spu, 0ul);
  284. spu_int_stat_clear(spu, 1, stat);
  285. spin_unlock(&spu->register_lock);
  286. pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
  287. dar, dsisr);
  288. if (stat & CLASS1_SEGMENT_FAULT_INTR)
  289. __spu_trap_data_seg(spu, dar);
  290. if (stat & CLASS1_STORAGE_FAULT_INTR)
  291. __spu_trap_data_map(spu, dar, dsisr);
  292. if (stat & CLASS1_LS_COMPARE_SUSPEND_ON_GET_INTR)
  293. ;
  294. if (stat & CLASS1_LS_COMPARE_SUSPEND_ON_PUT_INTR)
  295. ;
  296. return stat ? IRQ_HANDLED : IRQ_NONE;
  297. }
  298. static irqreturn_t
  299. spu_irq_class_2(int irq, void *data)
  300. {
  301. struct spu *spu;
  302. unsigned long stat;
  303. unsigned long mask;
  304. const int mailbox_intrs =
  305. CLASS2_MAILBOX_THRESHOLD_INTR | CLASS2_MAILBOX_INTR;
  306. spu = data;
  307. spin_lock(&spu->register_lock);
  308. stat = spu_int_stat_get(spu, 2);
  309. mask = spu_int_mask_get(spu, 2);
  310. /* ignore interrupts we're not waiting for */
  311. stat &= mask;
  312. /* mailbox interrupts are level triggered. mask them now before
  313. * acknowledging */
  314. if (stat & mailbox_intrs)
  315. spu_int_mask_and(spu, 2, ~(stat & mailbox_intrs));
  316. /* acknowledge all interrupts before the callbacks */
  317. spu_int_stat_clear(spu, 2, stat);
  318. spin_unlock(&spu->register_lock);
  319. pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
  320. if (stat & CLASS2_MAILBOX_INTR)
  321. spu->ibox_callback(spu);
  322. if (stat & CLASS2_SPU_STOP_INTR)
  323. spu->stop_callback(spu);
  324. if (stat & CLASS2_SPU_HALT_INTR)
  325. spu->stop_callback(spu);
  326. if (stat & CLASS2_SPU_DMA_TAG_GROUP_COMPLETE_INTR)
  327. spu->mfc_callback(spu);
  328. if (stat & CLASS2_MAILBOX_THRESHOLD_INTR)
  329. spu->wbox_callback(spu);
  330. spu->stats.class2_intr++;
  331. return stat ? IRQ_HANDLED : IRQ_NONE;
  332. }
  333. static int spu_request_irqs(struct spu *spu)
  334. {
  335. int ret = 0;
  336. if (spu->irqs[0] != NO_IRQ) {
  337. snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
  338. spu->number);
  339. ret = request_irq(spu->irqs[0], spu_irq_class_0,
  340. IRQF_DISABLED,
  341. spu->irq_c0, spu);
  342. if (ret)
  343. goto bail0;
  344. }
  345. if (spu->irqs[1] != NO_IRQ) {
  346. snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
  347. spu->number);
  348. ret = request_irq(spu->irqs[1], spu_irq_class_1,
  349. IRQF_DISABLED,
  350. spu->irq_c1, spu);
  351. if (ret)
  352. goto bail1;
  353. }
  354. if (spu->irqs[2] != NO_IRQ) {
  355. snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
  356. spu->number);
  357. ret = request_irq(spu->irqs[2], spu_irq_class_2,
  358. IRQF_DISABLED,
  359. spu->irq_c2, spu);
  360. if (ret)
  361. goto bail2;
  362. }
  363. return 0;
  364. bail2:
  365. if (spu->irqs[1] != NO_IRQ)
  366. free_irq(spu->irqs[1], spu);
  367. bail1:
  368. if (spu->irqs[0] != NO_IRQ)
  369. free_irq(spu->irqs[0], spu);
  370. bail0:
  371. return ret;
  372. }
  373. static void spu_free_irqs(struct spu *spu)
  374. {
  375. if (spu->irqs[0] != NO_IRQ)
  376. free_irq(spu->irqs[0], spu);
  377. if (spu->irqs[1] != NO_IRQ)
  378. free_irq(spu->irqs[1], spu);
  379. if (spu->irqs[2] != NO_IRQ)
  380. free_irq(spu->irqs[2], spu);
  381. }
  382. void spu_init_channels(struct spu *spu)
  383. {
  384. static const struct {
  385. unsigned channel;
  386. unsigned count;
  387. } zero_list[] = {
  388. { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
  389. { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
  390. }, count_list[] = {
  391. { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
  392. { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
  393. { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
  394. };
  395. struct spu_priv2 __iomem *priv2;
  396. int i;
  397. priv2 = spu->priv2;
  398. /* initialize all channel data to zero */
  399. for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
  400. int count;
  401. out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
  402. for (count = 0; count < zero_list[i].count; count++)
  403. out_be64(&priv2->spu_chnldata_RW, 0);
  404. }
  405. /* initialize channel counts to meaningful values */
  406. for (i = 0; i < ARRAY_SIZE(count_list); i++) {
  407. out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
  408. out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
  409. }
  410. }
  411. EXPORT_SYMBOL_GPL(spu_init_channels);
  412. static int spu_shutdown(struct sys_device *sysdev)
  413. {
  414. struct spu *spu = container_of(sysdev, struct spu, sysdev);
  415. spu_free_irqs(spu);
  416. spu_destroy_spu(spu);
  417. return 0;
  418. }
  419. static struct sysdev_class spu_sysdev_class = {
  420. .name = "spu",
  421. .shutdown = spu_shutdown,
  422. };
  423. int spu_add_sysdev_attr(struct sysdev_attribute *attr)
  424. {
  425. struct spu *spu;
  426. mutex_lock(&spu_full_list_mutex);
  427. list_for_each_entry(spu, &spu_full_list, full_list)
  428. sysdev_create_file(&spu->sysdev, attr);
  429. mutex_unlock(&spu_full_list_mutex);
  430. return 0;
  431. }
  432. EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
  433. int spu_add_sysdev_attr_group(struct attribute_group *attrs)
  434. {
  435. struct spu *spu;
  436. int rc = 0;
  437. mutex_lock(&spu_full_list_mutex);
  438. list_for_each_entry(spu, &spu_full_list, full_list) {
  439. rc = sysfs_create_group(&spu->sysdev.kobj, attrs);
  440. /* we're in trouble here, but try unwinding anyway */
  441. if (rc) {
  442. printk(KERN_ERR "%s: can't create sysfs group '%s'\n",
  443. __func__, attrs->name);
  444. list_for_each_entry_continue_reverse(spu,
  445. &spu_full_list, full_list)
  446. sysfs_remove_group(&spu->sysdev.kobj, attrs);
  447. break;
  448. }
  449. }
  450. mutex_unlock(&spu_full_list_mutex);
  451. return rc;
  452. }
  453. EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
  454. void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
  455. {
  456. struct spu *spu;
  457. mutex_lock(&spu_full_list_mutex);
  458. list_for_each_entry(spu, &spu_full_list, full_list)
  459. sysdev_remove_file(&spu->sysdev, attr);
  460. mutex_unlock(&spu_full_list_mutex);
  461. }
  462. EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
  463. void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
  464. {
  465. struct spu *spu;
  466. mutex_lock(&spu_full_list_mutex);
  467. list_for_each_entry(spu, &spu_full_list, full_list)
  468. sysfs_remove_group(&spu->sysdev.kobj, attrs);
  469. mutex_unlock(&spu_full_list_mutex);
  470. }
  471. EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
  472. static int spu_create_sysdev(struct spu *spu)
  473. {
  474. int ret;
  475. spu->sysdev.id = spu->number;
  476. spu->sysdev.cls = &spu_sysdev_class;
  477. ret = sysdev_register(&spu->sysdev);
  478. if (ret) {
  479. printk(KERN_ERR "Can't register SPU %d with sysfs\n",
  480. spu->number);
  481. return ret;
  482. }
  483. sysfs_add_device_to_node(&spu->sysdev, spu->node);
  484. return 0;
  485. }
  486. static int __init create_spu(void *data)
  487. {
  488. struct spu *spu;
  489. int ret;
  490. static int number;
  491. unsigned long flags;
  492. struct timespec ts;
  493. ret = -ENOMEM;
  494. spu = kzalloc(sizeof (*spu), GFP_KERNEL);
  495. if (!spu)
  496. goto out;
  497. spu->alloc_state = SPU_FREE;
  498. spin_lock_init(&spu->register_lock);
  499. spin_lock(&spu_lock);
  500. spu->number = number++;
  501. spin_unlock(&spu_lock);
  502. ret = spu_create_spu(spu, data);
  503. if (ret)
  504. goto out_free;
  505. spu_mfc_sdr_setup(spu);
  506. spu_mfc_sr1_set(spu, 0x33);
  507. ret = spu_request_irqs(spu);
  508. if (ret)
  509. goto out_destroy;
  510. ret = spu_create_sysdev(spu);
  511. if (ret)
  512. goto out_free_irqs;
  513. mutex_lock(&cbe_spu_info[spu->node].list_mutex);
  514. list_add(&spu->cbe_list, &cbe_spu_info[spu->node].spus);
  515. cbe_spu_info[spu->node].n_spus++;
  516. mutex_unlock(&cbe_spu_info[spu->node].list_mutex);
  517. mutex_lock(&spu_full_list_mutex);
  518. spin_lock_irqsave(&spu_full_list_lock, flags);
  519. list_add(&spu->full_list, &spu_full_list);
  520. spin_unlock_irqrestore(&spu_full_list_lock, flags);
  521. mutex_unlock(&spu_full_list_mutex);
  522. spu->stats.util_state = SPU_UTIL_IDLE_LOADED;
  523. ktime_get_ts(&ts);
  524. spu->stats.tstamp = timespec_to_ns(&ts);
  525. INIT_LIST_HEAD(&spu->aff_list);
  526. goto out;
  527. out_free_irqs:
  528. spu_free_irqs(spu);
  529. out_destroy:
  530. spu_destroy_spu(spu);
  531. out_free:
  532. kfree(spu);
  533. out:
  534. return ret;
  535. }
  536. static const char *spu_state_names[] = {
  537. "user", "system", "iowait", "idle"
  538. };
  539. static unsigned long long spu_acct_time(struct spu *spu,
  540. enum spu_utilization_state state)
  541. {
  542. struct timespec ts;
  543. unsigned long long time = spu->stats.times[state];
  544. /*
  545. * If the spu is idle or the context is stopped, utilization
  546. * statistics are not updated. Apply the time delta from the
  547. * last recorded state of the spu.
  548. */
  549. if (spu->stats.util_state == state) {
  550. ktime_get_ts(&ts);
  551. time += timespec_to_ns(&ts) - spu->stats.tstamp;
  552. }
  553. return time / NSEC_PER_MSEC;
  554. }
  555. static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf)
  556. {
  557. struct spu *spu = container_of(sysdev, struct spu, sysdev);
  558. return sprintf(buf, "%s %llu %llu %llu %llu "
  559. "%llu %llu %llu %llu %llu %llu %llu %llu\n",
  560. spu_state_names[spu->stats.util_state],
  561. spu_acct_time(spu, SPU_UTIL_USER),
  562. spu_acct_time(spu, SPU_UTIL_SYSTEM),
  563. spu_acct_time(spu, SPU_UTIL_IOWAIT),
  564. spu_acct_time(spu, SPU_UTIL_IDLE_LOADED),
  565. spu->stats.vol_ctx_switch,
  566. spu->stats.invol_ctx_switch,
  567. spu->stats.slb_flt,
  568. spu->stats.hash_flt,
  569. spu->stats.min_flt,
  570. spu->stats.maj_flt,
  571. spu->stats.class2_intr,
  572. spu->stats.libassist);
  573. }
  574. static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL);
  575. static int __init init_spu_base(void)
  576. {
  577. int i, ret = 0;
  578. for (i = 0; i < MAX_NUMNODES; i++) {
  579. mutex_init(&cbe_spu_info[i].list_mutex);
  580. INIT_LIST_HEAD(&cbe_spu_info[i].spus);
  581. }
  582. if (!spu_management_ops)
  583. goto out;
  584. /* create sysdev class for spus */
  585. ret = sysdev_class_register(&spu_sysdev_class);
  586. if (ret)
  587. goto out;
  588. ret = spu_enumerate_spus(create_spu);
  589. if (ret < 0) {
  590. printk(KERN_WARNING "%s: Error initializing spus\n",
  591. __FUNCTION__);
  592. goto out_unregister_sysdev_class;
  593. }
  594. if (ret > 0) {
  595. /*
  596. * We cannot put the forward declaration in
  597. * <linux/linux_logo.h> because of conflicting session type
  598. * conflicts for const and __initdata with different compiler
  599. * versions
  600. */
  601. extern const struct linux_logo logo_spe_clut224;
  602. fb_append_extra_logo(&logo_spe_clut224, ret);
  603. }
  604. mutex_lock(&spu_full_list_mutex);
  605. xmon_register_spus(&spu_full_list);
  606. crash_register_spus(&spu_full_list);
  607. mutex_unlock(&spu_full_list_mutex);
  608. spu_add_sysdev_attr(&attr_stat);
  609. spu_init_affinity();
  610. return 0;
  611. out_unregister_sysdev_class:
  612. sysdev_class_unregister(&spu_sysdev_class);
  613. out:
  614. return ret;
  615. }
  616. module_init(init_spu_base);
  617. MODULE_LICENSE("GPL");
  618. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");