spu.c 15 KB

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