spu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PS3 Platform spu routines.
  4. *
  5. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  6. * Copyright 2006 Sony Corp.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/mmzone.h>
  12. #include <linux/export.h>
  13. #include <linux/io.h>
  14. #include <linux/mm.h>
  15. #include <asm/spu.h>
  16. #include <asm/spu_priv1.h>
  17. #include <asm/lv1call.h>
  18. #include <asm/ps3.h>
  19. #include "../cell/spufs/spufs.h"
  20. #include "platform.h"
  21. /* spu_management_ops */
  22. /**
  23. * enum spe_type - Type of spe to create.
  24. * @spe_type_logical: Standard logical spe.
  25. *
  26. * For use with lv1_construct_logical_spe(). The current HV does not support
  27. * any types other than those listed.
  28. */
  29. enum spe_type {
  30. SPE_TYPE_LOGICAL = 0,
  31. };
  32. /**
  33. * struct spe_shadow - logical spe shadow register area.
  34. *
  35. * Read-only shadow of spe registers.
  36. */
  37. struct spe_shadow {
  38. u8 padding_0140[0x0140];
  39. u64 int_status_class0_RW; /* 0x0140 */
  40. u64 int_status_class1_RW; /* 0x0148 */
  41. u64 int_status_class2_RW; /* 0x0150 */
  42. u8 padding_0158[0x0610-0x0158];
  43. u64 mfc_dsisr_RW; /* 0x0610 */
  44. u8 padding_0618[0x0620-0x0618];
  45. u64 mfc_dar_RW; /* 0x0620 */
  46. u8 padding_0628[0x0800-0x0628];
  47. u64 mfc_dsipr_R; /* 0x0800 */
  48. u8 padding_0808[0x0810-0x0808];
  49. u64 mfc_lscrr_R; /* 0x0810 */
  50. u8 padding_0818[0x0c00-0x0818];
  51. u64 mfc_cer_R; /* 0x0c00 */
  52. u8 padding_0c08[0x0f00-0x0c08];
  53. u64 spe_execution_status; /* 0x0f00 */
  54. u8 padding_0f08[0x1000-0x0f08];
  55. };
  56. /**
  57. * enum spe_ex_state - Logical spe execution state.
  58. * @spe_ex_state_unexecutable: Uninitialized.
  59. * @spe_ex_state_executable: Enabled, not ready.
  60. * @spe_ex_state_executed: Ready for use.
  61. *
  62. * The execution state (status) of the logical spe as reported in
  63. * struct spe_shadow:spe_execution_status.
  64. */
  65. enum spe_ex_state {
  66. SPE_EX_STATE_UNEXECUTABLE = 0,
  67. SPE_EX_STATE_EXECUTABLE = 2,
  68. SPE_EX_STATE_EXECUTED = 3,
  69. };
  70. /**
  71. * struct priv1_cache - Cached values of priv1 registers.
  72. * @masks[]: Array of cached spe interrupt masks, indexed by class.
  73. * @sr1: Cached mfc_sr1 register.
  74. * @tclass_id: Cached mfc_tclass_id register.
  75. */
  76. struct priv1_cache {
  77. u64 masks[3];
  78. u64 sr1;
  79. u64 tclass_id;
  80. };
  81. /**
  82. * struct spu_pdata - Platform state variables.
  83. * @spe_id: HV spe id returned by lv1_construct_logical_spe().
  84. * @resource_id: HV spe resource id returned by
  85. * ps3_repository_read_spe_resource_id().
  86. * @priv2_addr: lpar address of spe priv2 area returned by
  87. * lv1_construct_logical_spe().
  88. * @shadow_addr: lpar address of spe register shadow area returned by
  89. * lv1_construct_logical_spe().
  90. * @shadow: Virtual (ioremap) address of spe register shadow area.
  91. * @cache: Cached values of priv1 registers.
  92. */
  93. struct spu_pdata {
  94. u64 spe_id;
  95. u64 resource_id;
  96. u64 priv2_addr;
  97. u64 shadow_addr;
  98. struct spe_shadow __iomem *shadow;
  99. struct priv1_cache cache;
  100. };
  101. static struct spu_pdata *spu_pdata(struct spu *spu)
  102. {
  103. return spu->pdata;
  104. }
  105. #define dump_areas(_a, _b, _c, _d, _e) \
  106. _dump_areas(_a, _b, _c, _d, _e, __func__, __LINE__)
  107. static void _dump_areas(unsigned int spe_id, unsigned long priv2,
  108. unsigned long problem, unsigned long ls, unsigned long shadow,
  109. const char* func, int line)
  110. {
  111. pr_debug("%s:%d: spe_id: %xh (%u)\n", func, line, spe_id, spe_id);
  112. pr_debug("%s:%d: priv2: %lxh\n", func, line, priv2);
  113. pr_debug("%s:%d: problem: %lxh\n", func, line, problem);
  114. pr_debug("%s:%d: ls: %lxh\n", func, line, ls);
  115. pr_debug("%s:%d: shadow: %lxh\n", func, line, shadow);
  116. }
  117. u64 ps3_get_spe_id(void *arg)
  118. {
  119. return spu_pdata(arg)->spe_id;
  120. }
  121. EXPORT_SYMBOL_GPL(ps3_get_spe_id);
  122. static unsigned long __init get_vas_id(void)
  123. {
  124. u64 id;
  125. lv1_get_logical_ppe_id(&id);
  126. lv1_get_virtual_address_space_id_of_ppe(&id);
  127. return id;
  128. }
  129. static int __init construct_spu(struct spu *spu)
  130. {
  131. int result;
  132. u64 unused;
  133. u64 problem_phys;
  134. u64 local_store_phys;
  135. result = lv1_construct_logical_spe(PAGE_SHIFT, PAGE_SHIFT, PAGE_SHIFT,
  136. PAGE_SHIFT, PAGE_SHIFT, get_vas_id(), SPE_TYPE_LOGICAL,
  137. &spu_pdata(spu)->priv2_addr, &problem_phys,
  138. &local_store_phys, &unused,
  139. &spu_pdata(spu)->shadow_addr,
  140. &spu_pdata(spu)->spe_id);
  141. spu->problem_phys = problem_phys;
  142. spu->local_store_phys = local_store_phys;
  143. if (result) {
  144. pr_debug("%s:%d: lv1_construct_logical_spe failed: %s\n",
  145. __func__, __LINE__, ps3_result(result));
  146. return result;
  147. }
  148. return result;
  149. }
  150. static void spu_unmap(struct spu *spu)
  151. {
  152. iounmap(spu->priv2);
  153. iounmap(spu->problem);
  154. iounmap((__force u8 __iomem *)spu->local_store);
  155. iounmap(spu_pdata(spu)->shadow);
  156. }
  157. /**
  158. * setup_areas - Map the spu regions into the address space.
  159. *
  160. * The current HV requires the spu shadow regs to be mapped with the
  161. * PTE page protection bits set as read-only.
  162. */
  163. static int __init setup_areas(struct spu *spu)
  164. {
  165. struct table {char* name; unsigned long addr; unsigned long size;};
  166. unsigned long shadow_flags = pgprot_val(pgprot_noncached_wc(PAGE_KERNEL_RO));
  167. spu_pdata(spu)->shadow = ioremap_prot(spu_pdata(spu)->shadow_addr,
  168. sizeof(struct spe_shadow), shadow_flags);
  169. if (!spu_pdata(spu)->shadow) {
  170. pr_debug("%s:%d: ioremap shadow failed\n", __func__, __LINE__);
  171. goto fail_ioremap;
  172. }
  173. spu->local_store = (__force void *)ioremap_wc(spu->local_store_phys, LS_SIZE);
  174. if (!spu->local_store) {
  175. pr_debug("%s:%d: ioremap local_store failed\n",
  176. __func__, __LINE__);
  177. goto fail_ioremap;
  178. }
  179. spu->problem = ioremap(spu->problem_phys,
  180. sizeof(struct spu_problem));
  181. if (!spu->problem) {
  182. pr_debug("%s:%d: ioremap problem failed\n", __func__, __LINE__);
  183. goto fail_ioremap;
  184. }
  185. spu->priv2 = ioremap(spu_pdata(spu)->priv2_addr,
  186. sizeof(struct spu_priv2));
  187. if (!spu->priv2) {
  188. pr_debug("%s:%d: ioremap priv2 failed\n", __func__, __LINE__);
  189. goto fail_ioremap;
  190. }
  191. dump_areas(spu_pdata(spu)->spe_id, spu_pdata(spu)->priv2_addr,
  192. spu->problem_phys, spu->local_store_phys,
  193. spu_pdata(spu)->shadow_addr);
  194. dump_areas(spu_pdata(spu)->spe_id, (unsigned long)spu->priv2,
  195. (unsigned long)spu->problem, (unsigned long)spu->local_store,
  196. (unsigned long)spu_pdata(spu)->shadow);
  197. return 0;
  198. fail_ioremap:
  199. spu_unmap(spu);
  200. return -ENOMEM;
  201. }
  202. static int __init setup_interrupts(struct spu *spu)
  203. {
  204. int result;
  205. result = ps3_spe_irq_setup(PS3_BINDING_CPU_ANY, spu_pdata(spu)->spe_id,
  206. 0, &spu->irqs[0]);
  207. if (result)
  208. goto fail_alloc_0;
  209. result = ps3_spe_irq_setup(PS3_BINDING_CPU_ANY, spu_pdata(spu)->spe_id,
  210. 1, &spu->irqs[1]);
  211. if (result)
  212. goto fail_alloc_1;
  213. result = ps3_spe_irq_setup(PS3_BINDING_CPU_ANY, spu_pdata(spu)->spe_id,
  214. 2, &spu->irqs[2]);
  215. if (result)
  216. goto fail_alloc_2;
  217. return result;
  218. fail_alloc_2:
  219. ps3_spe_irq_destroy(spu->irqs[1]);
  220. fail_alloc_1:
  221. ps3_spe_irq_destroy(spu->irqs[0]);
  222. fail_alloc_0:
  223. spu->irqs[0] = spu->irqs[1] = spu->irqs[2] = 0;
  224. return result;
  225. }
  226. static int __init enable_spu(struct spu *spu)
  227. {
  228. int result;
  229. result = lv1_enable_logical_spe(spu_pdata(spu)->spe_id,
  230. spu_pdata(spu)->resource_id);
  231. if (result) {
  232. pr_debug("%s:%d: lv1_enable_logical_spe failed: %s\n",
  233. __func__, __LINE__, ps3_result(result));
  234. goto fail_enable;
  235. }
  236. result = setup_areas(spu);
  237. if (result)
  238. goto fail_areas;
  239. result = setup_interrupts(spu);
  240. if (result)
  241. goto fail_interrupts;
  242. return 0;
  243. fail_interrupts:
  244. spu_unmap(spu);
  245. fail_areas:
  246. lv1_disable_logical_spe(spu_pdata(spu)->spe_id, 0);
  247. fail_enable:
  248. return result;
  249. }
  250. static int ps3_destroy_spu(struct spu *spu)
  251. {
  252. int result;
  253. pr_debug("%s:%d spu_%d\n", __func__, __LINE__, spu->number);
  254. result = lv1_disable_logical_spe(spu_pdata(spu)->spe_id, 0);
  255. BUG_ON(result);
  256. ps3_spe_irq_destroy(spu->irqs[2]);
  257. ps3_spe_irq_destroy(spu->irqs[1]);
  258. ps3_spe_irq_destroy(spu->irqs[0]);
  259. spu->irqs[0] = spu->irqs[1] = spu->irqs[2] = 0;
  260. spu_unmap(spu);
  261. result = lv1_destruct_logical_spe(spu_pdata(spu)->spe_id);
  262. BUG_ON(result);
  263. kfree(spu->pdata);
  264. spu->pdata = NULL;
  265. return 0;
  266. }
  267. static int __init ps3_create_spu(struct spu *spu, void *data)
  268. {
  269. int result;
  270. pr_debug("%s:%d spu_%d\n", __func__, __LINE__, spu->number);
  271. spu->pdata = kzalloc(sizeof(struct spu_pdata),
  272. GFP_KERNEL);
  273. if (!spu->pdata) {
  274. result = -ENOMEM;
  275. goto fail_malloc;
  276. }
  277. spu_pdata(spu)->resource_id = (unsigned long)data;
  278. /* Init cached reg values to HV defaults. */
  279. spu_pdata(spu)->cache.sr1 = 0x33;
  280. result = construct_spu(spu);
  281. if (result)
  282. goto fail_construct;
  283. /* For now, just go ahead and enable it. */
  284. result = enable_spu(spu);
  285. if (result)
  286. goto fail_enable;
  287. /* Make sure the spu is in SPE_EX_STATE_EXECUTED. */
  288. /* need something better here!!! */
  289. while (in_be64(&spu_pdata(spu)->shadow->spe_execution_status)
  290. != SPE_EX_STATE_EXECUTED)
  291. (void)0;
  292. return result;
  293. fail_enable:
  294. fail_construct:
  295. ps3_destroy_spu(spu);
  296. fail_malloc:
  297. return result;
  298. }
  299. static int __init ps3_enumerate_spus(int (*fn)(void *data))
  300. {
  301. int result;
  302. unsigned int num_resource_id;
  303. unsigned int i;
  304. result = ps3_repository_read_num_spu_resource_id(&num_resource_id);
  305. pr_debug("%s:%d: num_resource_id %u\n", __func__, __LINE__,
  306. num_resource_id);
  307. /*
  308. * For now, just create logical spus equal to the number
  309. * of physical spus reserved for the partition.
  310. */
  311. for (i = 0; i < num_resource_id; i++) {
  312. enum ps3_spu_resource_type resource_type;
  313. unsigned int resource_id;
  314. result = ps3_repository_read_spu_resource_id(i,
  315. &resource_type, &resource_id);
  316. if (result)
  317. break;
  318. if (resource_type == PS3_SPU_RESOURCE_TYPE_EXCLUSIVE) {
  319. result = fn((void*)(unsigned long)resource_id);
  320. if (result)
  321. break;
  322. }
  323. }
  324. if (result) {
  325. printk(KERN_WARNING "%s:%d: Error initializing spus\n",
  326. __func__, __LINE__);
  327. return result;
  328. }
  329. return num_resource_id;
  330. }
  331. static int ps3_init_affinity(void)
  332. {
  333. return 0;
  334. }
  335. /**
  336. * ps3_enable_spu - Enable SPU run control.
  337. *
  338. * An outstanding enhancement for the PS3 would be to add a guard to check
  339. * for incorrect access to the spu problem state when the spu context is
  340. * disabled. This check could be implemented with a flag added to the spu
  341. * context that would inhibit mapping problem state pages, and a routine
  342. * to unmap spu problem state pages. When the spu is enabled with
  343. * ps3_enable_spu() the flag would be set allowing pages to be mapped,
  344. * and when the spu is disabled with ps3_disable_spu() the flag would be
  345. * cleared and the mapped problem state pages would be unmapped.
  346. */
  347. static void ps3_enable_spu(struct spu_context *ctx)
  348. {
  349. }
  350. static void ps3_disable_spu(struct spu_context *ctx)
  351. {
  352. ctx->ops->runcntl_stop(ctx);
  353. }
  354. static const struct spu_management_ops spu_management_ps3_ops = {
  355. .enumerate_spus = ps3_enumerate_spus,
  356. .create_spu = ps3_create_spu,
  357. .destroy_spu = ps3_destroy_spu,
  358. .enable_spu = ps3_enable_spu,
  359. .disable_spu = ps3_disable_spu,
  360. .init_affinity = ps3_init_affinity,
  361. };
  362. /* spu_priv1_ops */
  363. static void int_mask_and(struct spu *spu, int class, u64 mask)
  364. {
  365. u64 old_mask;
  366. /* are these serialized by caller??? */
  367. old_mask = spu_int_mask_get(spu, class);
  368. spu_int_mask_set(spu, class, old_mask & mask);
  369. }
  370. static void int_mask_or(struct spu *spu, int class, u64 mask)
  371. {
  372. u64 old_mask;
  373. old_mask = spu_int_mask_get(spu, class);
  374. spu_int_mask_set(spu, class, old_mask | mask);
  375. }
  376. static void int_mask_set(struct spu *spu, int class, u64 mask)
  377. {
  378. spu_pdata(spu)->cache.masks[class] = mask;
  379. lv1_set_spe_interrupt_mask(spu_pdata(spu)->spe_id, class,
  380. spu_pdata(spu)->cache.masks[class]);
  381. }
  382. static u64 int_mask_get(struct spu *spu, int class)
  383. {
  384. return spu_pdata(spu)->cache.masks[class];
  385. }
  386. static void int_stat_clear(struct spu *spu, int class, u64 stat)
  387. {
  388. /* Note that MFC_DSISR will be cleared when class1[MF] is set. */
  389. lv1_clear_spe_interrupt_status(spu_pdata(spu)->spe_id, class,
  390. stat, 0);
  391. }
  392. static u64 int_stat_get(struct spu *spu, int class)
  393. {
  394. u64 stat;
  395. lv1_get_spe_interrupt_status(spu_pdata(spu)->spe_id, class, &stat);
  396. return stat;
  397. }
  398. static void cpu_affinity_set(struct spu *spu, int cpu)
  399. {
  400. /* No support. */
  401. }
  402. static u64 mfc_dar_get(struct spu *spu)
  403. {
  404. return in_be64(&spu_pdata(spu)->shadow->mfc_dar_RW);
  405. }
  406. static void mfc_dsisr_set(struct spu *spu, u64 dsisr)
  407. {
  408. /* Nothing to do, cleared in int_stat_clear(). */
  409. }
  410. static u64 mfc_dsisr_get(struct spu *spu)
  411. {
  412. return in_be64(&spu_pdata(spu)->shadow->mfc_dsisr_RW);
  413. }
  414. static void mfc_sdr_setup(struct spu *spu)
  415. {
  416. /* Nothing to do. */
  417. }
  418. static void mfc_sr1_set(struct spu *spu, u64 sr1)
  419. {
  420. /* Check bits allowed by HV. */
  421. static const u64 allowed = ~(MFC_STATE1_LOCAL_STORAGE_DECODE_MASK
  422. | MFC_STATE1_PROBLEM_STATE_MASK);
  423. BUG_ON((sr1 & allowed) != (spu_pdata(spu)->cache.sr1 & allowed));
  424. spu_pdata(spu)->cache.sr1 = sr1;
  425. lv1_set_spe_privilege_state_area_1_register(
  426. spu_pdata(spu)->spe_id,
  427. offsetof(struct spu_priv1, mfc_sr1_RW),
  428. spu_pdata(spu)->cache.sr1);
  429. }
  430. static u64 mfc_sr1_get(struct spu *spu)
  431. {
  432. return spu_pdata(spu)->cache.sr1;
  433. }
  434. static void mfc_tclass_id_set(struct spu *spu, u64 tclass_id)
  435. {
  436. spu_pdata(spu)->cache.tclass_id = tclass_id;
  437. lv1_set_spe_privilege_state_area_1_register(
  438. spu_pdata(spu)->spe_id,
  439. offsetof(struct spu_priv1, mfc_tclass_id_RW),
  440. spu_pdata(spu)->cache.tclass_id);
  441. }
  442. static u64 mfc_tclass_id_get(struct spu *spu)
  443. {
  444. return spu_pdata(spu)->cache.tclass_id;
  445. }
  446. static void tlb_invalidate(struct spu *spu)
  447. {
  448. /* Nothing to do. */
  449. }
  450. static void resource_allocation_groupID_set(struct spu *spu, u64 id)
  451. {
  452. /* No support. */
  453. }
  454. static u64 resource_allocation_groupID_get(struct spu *spu)
  455. {
  456. return 0; /* No support. */
  457. }
  458. static void resource_allocation_enable_set(struct spu *spu, u64 enable)
  459. {
  460. /* No support. */
  461. }
  462. static u64 resource_allocation_enable_get(struct spu *spu)
  463. {
  464. return 0; /* No support. */
  465. }
  466. static const struct spu_priv1_ops spu_priv1_ps3_ops = {
  467. .int_mask_and = int_mask_and,
  468. .int_mask_or = int_mask_or,
  469. .int_mask_set = int_mask_set,
  470. .int_mask_get = int_mask_get,
  471. .int_stat_clear = int_stat_clear,
  472. .int_stat_get = int_stat_get,
  473. .cpu_affinity_set = cpu_affinity_set,
  474. .mfc_dar_get = mfc_dar_get,
  475. .mfc_dsisr_set = mfc_dsisr_set,
  476. .mfc_dsisr_get = mfc_dsisr_get,
  477. .mfc_sdr_setup = mfc_sdr_setup,
  478. .mfc_sr1_set = mfc_sr1_set,
  479. .mfc_sr1_get = mfc_sr1_get,
  480. .mfc_tclass_id_set = mfc_tclass_id_set,
  481. .mfc_tclass_id_get = mfc_tclass_id_get,
  482. .tlb_invalidate = tlb_invalidate,
  483. .resource_allocation_groupID_set = resource_allocation_groupID_set,
  484. .resource_allocation_groupID_get = resource_allocation_groupID_get,
  485. .resource_allocation_enable_set = resource_allocation_enable_set,
  486. .resource_allocation_enable_get = resource_allocation_enable_get,
  487. };
  488. void ps3_spu_set_platform(void)
  489. {
  490. spu_priv1_ops = &spu_priv1_ps3_ops;
  491. spu_management_ops = &spu_management_ps3_ops;
  492. }