spu_base.c 20 KB

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