spu_base.c 20 KB

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