spu.c 14 KB

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