spu_base.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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 int __spu_trap_invalid_dma(struct spu *spu)
  117. {
  118. pr_debug("%s\n", __FUNCTION__);
  119. spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
  120. return 0;
  121. }
  122. static int __spu_trap_dma_align(struct spu *spu)
  123. {
  124. pr_debug("%s\n", __FUNCTION__);
  125. spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
  126. return 0;
  127. }
  128. static int __spu_trap_error(struct spu *spu)
  129. {
  130. pr_debug("%s\n", __FUNCTION__);
  131. spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
  132. return 0;
  133. }
  134. static void spu_restart_dma(struct spu *spu)
  135. {
  136. struct spu_priv2 __iomem *priv2 = spu->priv2;
  137. if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
  138. out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
  139. }
  140. static inline void spu_load_slb(struct spu *spu, int slbe, struct spu_slb *slb)
  141. {
  142. struct spu_priv2 __iomem *priv2 = spu->priv2;
  143. pr_debug("%s: adding SLB[%d] 0x%016lx 0x%016lx\n",
  144. __func__, slbe, slb->vsid, slb->esid);
  145. out_be64(&priv2->slb_index_W, slbe);
  146. out_be64(&priv2->slb_vsid_RW, slb->vsid);
  147. out_be64(&priv2->slb_esid_RW, slb->esid);
  148. }
  149. static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
  150. {
  151. struct mm_struct *mm = spu->mm;
  152. struct spu_slb slb;
  153. int psize;
  154. pr_debug("%s\n", __FUNCTION__);
  155. if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
  156. /* SLBs are pre-loaded for context switch, so
  157. * we should never get here!
  158. */
  159. printk("%s: invalid access during switch!\n", __func__);
  160. return 1;
  161. }
  162. slb.esid = (ea & ESID_MASK) | SLB_ESID_V;
  163. switch(REGION_ID(ea)) {
  164. case USER_REGION_ID:
  165. #ifdef CONFIG_PPC_MM_SLICES
  166. psize = get_slice_psize(mm, ea);
  167. #else
  168. psize = mm->context.user_psize;
  169. #endif
  170. slb.vsid = (get_vsid(mm->context.id, ea, MMU_SEGSIZE_256M)
  171. << SLB_VSID_SHIFT) | SLB_VSID_USER;
  172. break;
  173. case VMALLOC_REGION_ID:
  174. if (ea < VMALLOC_END)
  175. psize = mmu_vmalloc_psize;
  176. else
  177. psize = mmu_io_psize;
  178. slb.vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M)
  179. << SLB_VSID_SHIFT) | SLB_VSID_KERNEL;
  180. break;
  181. case KERNEL_REGION_ID:
  182. psize = mmu_linear_psize;
  183. slb.vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M)
  184. << SLB_VSID_SHIFT) | SLB_VSID_KERNEL;
  185. break;
  186. default:
  187. /* Future: support kernel segments so that drivers
  188. * can use SPUs.
  189. */
  190. pr_debug("invalid region access at %016lx\n", ea);
  191. return 1;
  192. }
  193. slb.vsid |= mmu_psize_defs[psize].sllp;
  194. spu_load_slb(spu, spu->slb_replace, &slb);
  195. spu->slb_replace++;
  196. if (spu->slb_replace >= 8)
  197. spu->slb_replace = 0;
  198. spu_restart_dma(spu);
  199. spu->stats.slb_flt++;
  200. return 0;
  201. }
  202. extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
  203. static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
  204. {
  205. pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
  206. /* Handle kernel space hash faults immediately.
  207. User hash faults need to be deferred to process context. */
  208. if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
  209. && REGION_ID(ea) != USER_REGION_ID
  210. && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
  211. spu_restart_dma(spu);
  212. return 0;
  213. }
  214. if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
  215. printk("%s: invalid access during switch!\n", __func__);
  216. return 1;
  217. }
  218. spu->dar = ea;
  219. spu->dsisr = dsisr;
  220. mb();
  221. spu->stop_callback(spu);
  222. return 0;
  223. }
  224. static void __spu_kernel_slb(void *addr, struct spu_slb *slb)
  225. {
  226. unsigned long ea = (unsigned long)addr;
  227. u64 llp;
  228. if (REGION_ID(ea) == KERNEL_REGION_ID)
  229. llp = mmu_psize_defs[mmu_linear_psize].sllp;
  230. else
  231. llp = mmu_psize_defs[mmu_virtual_psize].sllp;
  232. slb->vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M) << SLB_VSID_SHIFT) |
  233. SLB_VSID_KERNEL | llp;
  234. slb->esid = (ea & ESID_MASK) | SLB_ESID_V;
  235. }
  236. /**
  237. * Given an array of @nr_slbs SLB entries, @slbs, return non-zero if the
  238. * address @new_addr is present.
  239. */
  240. static inline int __slb_present(struct spu_slb *slbs, int nr_slbs,
  241. void *new_addr)
  242. {
  243. unsigned long ea = (unsigned long)new_addr;
  244. int i;
  245. for (i = 0; i < nr_slbs; i++)
  246. if (!((slbs[i].esid ^ ea) & ESID_MASK))
  247. return 1;
  248. return 0;
  249. }
  250. /**
  251. * Setup the SPU kernel SLBs, in preparation for a context save/restore. We
  252. * need to map both the context save area, and the save/restore code.
  253. *
  254. * Because the lscsa and code may cross segment boundaires, we check to see
  255. * if mappings are required for the start and end of each range. We currently
  256. * assume that the mappings are smaller that one segment - if not, something
  257. * is seriously wrong.
  258. */
  259. void spu_setup_kernel_slbs(struct spu *spu, struct spu_lscsa *lscsa,
  260. void *code, int code_size)
  261. {
  262. struct spu_slb slbs[4];
  263. int i, nr_slbs = 0;
  264. /* start and end addresses of both mappings */
  265. void *addrs[] = {
  266. lscsa, (void *)lscsa + sizeof(*lscsa) - 1,
  267. code, code + code_size - 1
  268. };
  269. /* check the set of addresses, and create a new entry in the slbs array
  270. * if there isn't already a SLB for that address */
  271. for (i = 0; i < ARRAY_SIZE(addrs); i++) {
  272. if (__slb_present(slbs, nr_slbs, addrs[i]))
  273. continue;
  274. __spu_kernel_slb(addrs[i], &slbs[nr_slbs]);
  275. nr_slbs++;
  276. }
  277. /* Add the set of SLBs */
  278. for (i = 0; i < nr_slbs; i++)
  279. spu_load_slb(spu, i, &slbs[i]);
  280. }
  281. EXPORT_SYMBOL_GPL(spu_setup_kernel_slbs);
  282. static irqreturn_t
  283. spu_irq_class_0(int irq, void *data)
  284. {
  285. struct spu *spu;
  286. unsigned long stat, mask;
  287. spu = data;
  288. mask = spu_int_mask_get(spu, 0);
  289. stat = spu_int_stat_get(spu, 0);
  290. stat &= mask;
  291. spin_lock(&spu->register_lock);
  292. spu->class_0_pending |= stat;
  293. spin_unlock(&spu->register_lock);
  294. spu->stop_callback(spu);
  295. spu_int_stat_clear(spu, 0, stat);
  296. return IRQ_HANDLED;
  297. }
  298. int
  299. spu_irq_class_0_bottom(struct spu *spu)
  300. {
  301. unsigned long flags;
  302. unsigned long stat;
  303. spin_lock_irqsave(&spu->register_lock, flags);
  304. stat = spu->class_0_pending;
  305. spu->class_0_pending = 0;
  306. if (stat & 1) /* invalid DMA alignment */
  307. __spu_trap_dma_align(spu);
  308. if (stat & 2) /* invalid MFC DMA */
  309. __spu_trap_invalid_dma(spu);
  310. if (stat & 4) /* error on SPU */
  311. __spu_trap_error(spu);
  312. spin_unlock_irqrestore(&spu->register_lock, flags);
  313. return (stat & 0x7) ? -EIO : 0;
  314. }
  315. EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
  316. static irqreturn_t
  317. spu_irq_class_1(int irq, void *data)
  318. {
  319. struct spu *spu;
  320. unsigned long stat, mask, dar, dsisr;
  321. spu = data;
  322. /* atomically read & clear class1 status. */
  323. spin_lock(&spu->register_lock);
  324. mask = spu_int_mask_get(spu, 1);
  325. stat = spu_int_stat_get(spu, 1) & mask;
  326. dar = spu_mfc_dar_get(spu);
  327. dsisr = spu_mfc_dsisr_get(spu);
  328. if (stat & 2) /* mapping fault */
  329. spu_mfc_dsisr_set(spu, 0ul);
  330. spu_int_stat_clear(spu, 1, stat);
  331. spin_unlock(&spu->register_lock);
  332. pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
  333. dar, dsisr);
  334. if (stat & 1) /* segment fault */
  335. __spu_trap_data_seg(spu, dar);
  336. if (stat & 2) { /* mapping fault */
  337. __spu_trap_data_map(spu, dar, dsisr);
  338. }
  339. if (stat & 4) /* ls compare & suspend on get */
  340. ;
  341. if (stat & 8) /* ls compare & suspend on put */
  342. ;
  343. return stat ? IRQ_HANDLED : IRQ_NONE;
  344. }
  345. static irqreturn_t
  346. spu_irq_class_2(int irq, void *data)
  347. {
  348. struct spu *spu;
  349. unsigned long stat;
  350. unsigned long mask;
  351. spu = data;
  352. spin_lock(&spu->register_lock);
  353. stat = spu_int_stat_get(spu, 2);
  354. mask = spu_int_mask_get(spu, 2);
  355. /* ignore interrupts we're not waiting for */
  356. stat &= mask;
  357. /*
  358. * mailbox interrupts (0x1 and 0x10) are level triggered.
  359. * mask them now before acknowledging.
  360. */
  361. if (stat & 0x11)
  362. spu_int_mask_and(spu, 2, ~(stat & 0x11));
  363. /* acknowledge all interrupts before the callbacks */
  364. spu_int_stat_clear(spu, 2, stat);
  365. spin_unlock(&spu->register_lock);
  366. pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
  367. if (stat & 1) /* PPC core mailbox */
  368. spu->ibox_callback(spu);
  369. if (stat & 2) /* SPU stop-and-signal */
  370. spu->stop_callback(spu);
  371. if (stat & 4) /* SPU halted */
  372. spu->stop_callback(spu);
  373. if (stat & 8) /* DMA tag group complete */
  374. spu->mfc_callback(spu);
  375. if (stat & 0x10) /* SPU mailbox threshold */
  376. spu->wbox_callback(spu);
  377. spu->stats.class2_intr++;
  378. return stat ? IRQ_HANDLED : IRQ_NONE;
  379. }
  380. static int spu_request_irqs(struct spu *spu)
  381. {
  382. int ret = 0;
  383. if (spu->irqs[0] != NO_IRQ) {
  384. snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
  385. spu->number);
  386. ret = request_irq(spu->irqs[0], spu_irq_class_0,
  387. IRQF_DISABLED,
  388. spu->irq_c0, spu);
  389. if (ret)
  390. goto bail0;
  391. }
  392. if (spu->irqs[1] != NO_IRQ) {
  393. snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
  394. spu->number);
  395. ret = request_irq(spu->irqs[1], spu_irq_class_1,
  396. IRQF_DISABLED,
  397. spu->irq_c1, spu);
  398. if (ret)
  399. goto bail1;
  400. }
  401. if (spu->irqs[2] != NO_IRQ) {
  402. snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
  403. spu->number);
  404. ret = request_irq(spu->irqs[2], spu_irq_class_2,
  405. IRQF_DISABLED,
  406. spu->irq_c2, spu);
  407. if (ret)
  408. goto bail2;
  409. }
  410. return 0;
  411. bail2:
  412. if (spu->irqs[1] != NO_IRQ)
  413. free_irq(spu->irqs[1], spu);
  414. bail1:
  415. if (spu->irqs[0] != NO_IRQ)
  416. free_irq(spu->irqs[0], spu);
  417. bail0:
  418. return ret;
  419. }
  420. static void spu_free_irqs(struct spu *spu)
  421. {
  422. if (spu->irqs[0] != NO_IRQ)
  423. free_irq(spu->irqs[0], spu);
  424. if (spu->irqs[1] != NO_IRQ)
  425. free_irq(spu->irqs[1], spu);
  426. if (spu->irqs[2] != NO_IRQ)
  427. free_irq(spu->irqs[2], spu);
  428. }
  429. void spu_init_channels(struct spu *spu)
  430. {
  431. static const struct {
  432. unsigned channel;
  433. unsigned count;
  434. } zero_list[] = {
  435. { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
  436. { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
  437. }, count_list[] = {
  438. { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
  439. { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
  440. { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
  441. };
  442. struct spu_priv2 __iomem *priv2;
  443. int i;
  444. priv2 = spu->priv2;
  445. /* initialize all channel data to zero */
  446. for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
  447. int count;
  448. out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
  449. for (count = 0; count < zero_list[i].count; count++)
  450. out_be64(&priv2->spu_chnldata_RW, 0);
  451. }
  452. /* initialize channel counts to meaningful values */
  453. for (i = 0; i < ARRAY_SIZE(count_list); i++) {
  454. out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
  455. out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
  456. }
  457. }
  458. EXPORT_SYMBOL_GPL(spu_init_channels);
  459. static int spu_shutdown(struct sys_device *sysdev)
  460. {
  461. struct spu *spu = container_of(sysdev, struct spu, sysdev);
  462. spu_free_irqs(spu);
  463. spu_destroy_spu(spu);
  464. return 0;
  465. }
  466. static struct sysdev_class spu_sysdev_class = {
  467. set_kset_name("spu"),
  468. .shutdown = spu_shutdown,
  469. };
  470. int spu_add_sysdev_attr(struct sysdev_attribute *attr)
  471. {
  472. struct spu *spu;
  473. mutex_lock(&spu_full_list_mutex);
  474. list_for_each_entry(spu, &spu_full_list, full_list)
  475. sysdev_create_file(&spu->sysdev, attr);
  476. mutex_unlock(&spu_full_list_mutex);
  477. return 0;
  478. }
  479. EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
  480. int spu_add_sysdev_attr_group(struct attribute_group *attrs)
  481. {
  482. struct spu *spu;
  483. int rc = 0;
  484. mutex_lock(&spu_full_list_mutex);
  485. list_for_each_entry(spu, &spu_full_list, full_list) {
  486. rc = sysfs_create_group(&spu->sysdev.kobj, attrs);
  487. /* we're in trouble here, but try unwinding anyway */
  488. if (rc) {
  489. printk(KERN_ERR "%s: can't create sysfs group '%s'\n",
  490. __func__, attrs->name);
  491. list_for_each_entry_continue_reverse(spu,
  492. &spu_full_list, full_list)
  493. sysfs_remove_group(&spu->sysdev.kobj, attrs);
  494. break;
  495. }
  496. }
  497. mutex_unlock(&spu_full_list_mutex);
  498. return rc;
  499. }
  500. EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
  501. void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
  502. {
  503. struct spu *spu;
  504. mutex_lock(&spu_full_list_mutex);
  505. list_for_each_entry(spu, &spu_full_list, full_list)
  506. sysdev_remove_file(&spu->sysdev, attr);
  507. mutex_unlock(&spu_full_list_mutex);
  508. }
  509. EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
  510. void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
  511. {
  512. struct spu *spu;
  513. mutex_lock(&spu_full_list_mutex);
  514. list_for_each_entry(spu, &spu_full_list, full_list)
  515. sysfs_remove_group(&spu->sysdev.kobj, attrs);
  516. mutex_unlock(&spu_full_list_mutex);
  517. }
  518. EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
  519. static int spu_create_sysdev(struct spu *spu)
  520. {
  521. int ret;
  522. spu->sysdev.id = spu->number;
  523. spu->sysdev.cls = &spu_sysdev_class;
  524. ret = sysdev_register(&spu->sysdev);
  525. if (ret) {
  526. printk(KERN_ERR "Can't register SPU %d with sysfs\n",
  527. spu->number);
  528. return ret;
  529. }
  530. sysfs_add_device_to_node(&spu->sysdev, spu->node);
  531. return 0;
  532. }
  533. static int __init create_spu(void *data)
  534. {
  535. struct spu *spu;
  536. int ret;
  537. static int number;
  538. unsigned long flags;
  539. struct timespec ts;
  540. ret = -ENOMEM;
  541. spu = kzalloc(sizeof (*spu), GFP_KERNEL);
  542. if (!spu)
  543. goto out;
  544. spu->alloc_state = SPU_FREE;
  545. spin_lock_init(&spu->register_lock);
  546. spin_lock(&spu_lock);
  547. spu->number = number++;
  548. spin_unlock(&spu_lock);
  549. ret = spu_create_spu(spu, data);
  550. if (ret)
  551. goto out_free;
  552. spu_mfc_sdr_setup(spu);
  553. spu_mfc_sr1_set(spu, 0x33);
  554. ret = spu_request_irqs(spu);
  555. if (ret)
  556. goto out_destroy;
  557. ret = spu_create_sysdev(spu);
  558. if (ret)
  559. goto out_free_irqs;
  560. mutex_lock(&cbe_spu_info[spu->node].list_mutex);
  561. list_add(&spu->cbe_list, &cbe_spu_info[spu->node].spus);
  562. cbe_spu_info[spu->node].n_spus++;
  563. mutex_unlock(&cbe_spu_info[spu->node].list_mutex);
  564. mutex_lock(&spu_full_list_mutex);
  565. spin_lock_irqsave(&spu_full_list_lock, flags);
  566. list_add(&spu->full_list, &spu_full_list);
  567. spin_unlock_irqrestore(&spu_full_list_lock, flags);
  568. mutex_unlock(&spu_full_list_mutex);
  569. spu->stats.util_state = SPU_UTIL_IDLE_LOADED;
  570. ktime_get_ts(&ts);
  571. spu->stats.tstamp = timespec_to_ns(&ts);
  572. INIT_LIST_HEAD(&spu->aff_list);
  573. goto out;
  574. out_free_irqs:
  575. spu_free_irqs(spu);
  576. out_destroy:
  577. spu_destroy_spu(spu);
  578. out_free:
  579. kfree(spu);
  580. out:
  581. return ret;
  582. }
  583. static const char *spu_state_names[] = {
  584. "user", "system", "iowait", "idle"
  585. };
  586. static unsigned long long spu_acct_time(struct spu *spu,
  587. enum spu_utilization_state state)
  588. {
  589. struct timespec ts;
  590. unsigned long long time = spu->stats.times[state];
  591. /*
  592. * If the spu is idle or the context is stopped, utilization
  593. * statistics are not updated. Apply the time delta from the
  594. * last recorded state of the spu.
  595. */
  596. if (spu->stats.util_state == state) {
  597. ktime_get_ts(&ts);
  598. time += timespec_to_ns(&ts) - spu->stats.tstamp;
  599. }
  600. return time / NSEC_PER_MSEC;
  601. }
  602. static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf)
  603. {
  604. struct spu *spu = container_of(sysdev, struct spu, sysdev);
  605. return sprintf(buf, "%s %llu %llu %llu %llu "
  606. "%llu %llu %llu %llu %llu %llu %llu %llu\n",
  607. spu_state_names[spu->stats.util_state],
  608. spu_acct_time(spu, SPU_UTIL_USER),
  609. spu_acct_time(spu, SPU_UTIL_SYSTEM),
  610. spu_acct_time(spu, SPU_UTIL_IOWAIT),
  611. spu_acct_time(spu, SPU_UTIL_IDLE_LOADED),
  612. spu->stats.vol_ctx_switch,
  613. spu->stats.invol_ctx_switch,
  614. spu->stats.slb_flt,
  615. spu->stats.hash_flt,
  616. spu->stats.min_flt,
  617. spu->stats.maj_flt,
  618. spu->stats.class2_intr,
  619. spu->stats.libassist);
  620. }
  621. static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL);
  622. static int __init init_spu_base(void)
  623. {
  624. int i, ret = 0;
  625. for (i = 0; i < MAX_NUMNODES; i++) {
  626. mutex_init(&cbe_spu_info[i].list_mutex);
  627. INIT_LIST_HEAD(&cbe_spu_info[i].spus);
  628. }
  629. if (!spu_management_ops)
  630. goto out;
  631. /* create sysdev class for spus */
  632. ret = sysdev_class_register(&spu_sysdev_class);
  633. if (ret)
  634. goto out;
  635. ret = spu_enumerate_spus(create_spu);
  636. if (ret < 0) {
  637. printk(KERN_WARNING "%s: Error initializing spus\n",
  638. __FUNCTION__);
  639. goto out_unregister_sysdev_class;
  640. }
  641. if (ret > 0) {
  642. /*
  643. * We cannot put the forward declaration in
  644. * <linux/linux_logo.h> because of conflicting session type
  645. * conflicts for const and __initdata with different compiler
  646. * versions
  647. */
  648. extern const struct linux_logo logo_spe_clut224;
  649. fb_append_extra_logo(&logo_spe_clut224, ret);
  650. }
  651. mutex_lock(&spu_full_list_mutex);
  652. xmon_register_spus(&spu_full_list);
  653. crash_register_spus(&spu_full_list);
  654. mutex_unlock(&spu_full_list_mutex);
  655. spu_add_sysdev_attr(&attr_stat);
  656. spu_init_affinity();
  657. return 0;
  658. out_unregister_sysdev_class:
  659. sysdev_class_unregister(&spu_sysdev_class);
  660. out:
  661. return ret;
  662. }
  663. module_init(init_spu_base);
  664. MODULE_LICENSE("GPL");
  665. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");