spu_priv1_mmio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * spu hypervisor abstraction for direct hardware access.
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  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/interrupt.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/slab.h>
  25. #include <linux/wait.h>
  26. #include <linux/mm.h>
  27. #include <linux/io.h>
  28. #include <linux/mutex.h>
  29. #include <linux/device.h>
  30. #include <asm/spu.h>
  31. #include <asm/spu_priv1.h>
  32. #include <asm/firmware.h>
  33. #include <asm/prom.h>
  34. #include "interrupt.h"
  35. #include "spu_priv1_mmio.h"
  36. struct spu_pdata {
  37. int nid;
  38. struct device_node *devnode;
  39. struct spu_priv1 __iomem *priv1;
  40. };
  41. static struct spu_pdata *spu_get_pdata(struct spu *spu)
  42. {
  43. BUG_ON(!spu->pdata);
  44. return spu->pdata;
  45. }
  46. struct device_node *spu_devnode(struct spu *spu)
  47. {
  48. return spu_get_pdata(spu)->devnode;
  49. }
  50. EXPORT_SYMBOL_GPL(spu_devnode);
  51. static int __init find_spu_node_id(struct device_node *spe)
  52. {
  53. const unsigned int *id;
  54. struct device_node *cpu;
  55. cpu = spe->parent->parent;
  56. id = get_property(cpu, "node-id", NULL);
  57. return id ? *id : 0;
  58. }
  59. static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
  60. const char *prop)
  61. {
  62. static DEFINE_MUTEX(add_spumem_mutex);
  63. const struct address_prop {
  64. unsigned long address;
  65. unsigned int len;
  66. } __attribute__((packed)) *p;
  67. int proplen;
  68. unsigned long start_pfn, nr_pages;
  69. struct pglist_data *pgdata;
  70. struct zone *zone;
  71. int ret;
  72. p = get_property(spe, prop, &proplen);
  73. WARN_ON(proplen != sizeof (*p));
  74. start_pfn = p->address >> PAGE_SHIFT;
  75. nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  76. pgdata = NODE_DATA(spu_get_pdata(spu)->nid);
  77. zone = pgdata->node_zones;
  78. /* XXX rethink locking here */
  79. mutex_lock(&add_spumem_mutex);
  80. ret = __add_pages(zone, start_pfn, nr_pages);
  81. mutex_unlock(&add_spumem_mutex);
  82. return ret;
  83. }
  84. static void __iomem * __init map_spe_prop(struct spu *spu,
  85. struct device_node *n, const char *name)
  86. {
  87. const struct address_prop {
  88. unsigned long address;
  89. unsigned int len;
  90. } __attribute__((packed)) *prop;
  91. const void *p;
  92. int proplen;
  93. void __iomem *ret = NULL;
  94. int err = 0;
  95. p = get_property(n, name, &proplen);
  96. if (proplen != sizeof (struct address_prop))
  97. return NULL;
  98. prop = p;
  99. err = cell_spuprop_present(spu, n, name);
  100. if (err && (err != -EEXIST))
  101. goto out;
  102. ret = ioremap(prop->address, prop->len);
  103. out:
  104. return ret;
  105. }
  106. static void spu_unmap(struct spu *spu)
  107. {
  108. iounmap(spu->priv2);
  109. iounmap(spu_get_pdata(spu)->priv1);
  110. iounmap(spu->problem);
  111. iounmap((__force u8 __iomem *)spu->local_store);
  112. }
  113. static int __init spu_map_interrupts_old(struct spu *spu,
  114. struct device_node *np)
  115. {
  116. unsigned int isrc;
  117. const u32 *tmp;
  118. /* Get the interrupt source unit from the device-tree */
  119. tmp = get_property(np, "isrc", NULL);
  120. if (!tmp)
  121. return -ENODEV;
  122. isrc = tmp[0];
  123. /* Add the node number */
  124. isrc |= spu->node << IIC_IRQ_NODE_SHIFT;
  125. /* Now map interrupts of all 3 classes */
  126. spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
  127. spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
  128. spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
  129. /* Right now, we only fail if class 2 failed */
  130. return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
  131. }
  132. static int __init spu_map_device_old(struct spu *spu, struct device_node *node)
  133. {
  134. const char *prop;
  135. int ret;
  136. ret = -ENODEV;
  137. spu->name = get_property(node, "name", NULL);
  138. if (!spu->name)
  139. goto out;
  140. prop = get_property(node, "local-store", NULL);
  141. if (!prop)
  142. goto out;
  143. spu->local_store_phys = *(unsigned long *)prop;
  144. /* we use local store as ram, not io memory */
  145. spu->local_store = (void __force *)
  146. map_spe_prop(spu, node, "local-store");
  147. if (!spu->local_store)
  148. goto out;
  149. prop = get_property(node, "problem", NULL);
  150. if (!prop)
  151. goto out_unmap;
  152. spu->problem_phys = *(unsigned long *)prop;
  153. spu->problem= map_spe_prop(spu, node, "problem");
  154. if (!spu->problem)
  155. goto out_unmap;
  156. spu_get_pdata(spu)->priv1= map_spe_prop(spu, node, "priv1");
  157. spu->priv2= map_spe_prop(spu, node, "priv2");
  158. if (!spu->priv2)
  159. goto out_unmap;
  160. ret = 0;
  161. goto out;
  162. out_unmap:
  163. spu_unmap(spu);
  164. out:
  165. return ret;
  166. }
  167. static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
  168. {
  169. struct of_irq oirq;
  170. int ret;
  171. int i;
  172. for (i=0; i < 3; i++) {
  173. ret = of_irq_map_one(np, i, &oirq);
  174. if (ret) {
  175. pr_debug("spu_new: failed to get irq %d\n", i);
  176. goto err;
  177. }
  178. ret = -EINVAL;
  179. pr_debug(" irq %d no 0x%x on %s\n", i, oirq.specifier[0],
  180. oirq.controller->full_name);
  181. spu->irqs[i] = irq_create_of_mapping(oirq.controller,
  182. oirq.specifier, oirq.size);
  183. if (spu->irqs[i] == NO_IRQ) {
  184. pr_debug("spu_new: failed to map it !\n");
  185. goto err;
  186. }
  187. }
  188. return 0;
  189. err:
  190. pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
  191. spu->name);
  192. for (; i >= 0; i--) {
  193. if (spu->irqs[i] != NO_IRQ)
  194. irq_dispose_mapping(spu->irqs[i]);
  195. }
  196. return ret;
  197. }
  198. static int spu_map_resource(struct device_node *node, int nr,
  199. void __iomem** virt, unsigned long *phys)
  200. {
  201. struct resource resource = { };
  202. int ret;
  203. ret = of_address_to_resource(node, nr, &resource);
  204. if (ret)
  205. goto out;
  206. if (phys)
  207. *phys = resource.start;
  208. *virt = ioremap(resource.start, resource.end - resource.start);
  209. if (!*virt)
  210. ret = -EINVAL;
  211. out:
  212. return ret;
  213. }
  214. static int __init spu_map_device(struct spu *spu, struct device_node *node)
  215. {
  216. int ret = -ENODEV;
  217. spu->name = get_property(node, "name", NULL);
  218. if (!spu->name)
  219. goto out;
  220. ret = spu_map_resource(node, 0, (void __iomem**)&spu->local_store,
  221. &spu->local_store_phys);
  222. if (ret) {
  223. pr_debug("spu_new: failed to map %s resource 0\n",
  224. node->full_name);
  225. goto out;
  226. }
  227. ret = spu_map_resource(node, 1, (void __iomem**)&spu->problem,
  228. &spu->problem_phys);
  229. if (ret) {
  230. pr_debug("spu_new: failed to map %s resource 1\n",
  231. node->full_name);
  232. goto out_unmap;
  233. }
  234. ret = spu_map_resource(node, 2, (void __iomem**)&spu->priv2,
  235. NULL);
  236. if (ret) {
  237. pr_debug("spu_new: failed to map %s resource 2\n",
  238. node->full_name);
  239. goto out_unmap;
  240. }
  241. if (!firmware_has_feature(FW_FEATURE_LPAR))
  242. ret = spu_map_resource(node, 3,
  243. (void __iomem**)&spu_get_pdata(spu)->priv1, NULL);
  244. if (ret) {
  245. pr_debug("spu_new: failed to map %s resource 3\n",
  246. node->full_name);
  247. goto out_unmap;
  248. }
  249. pr_debug("spu_new: %s maps:\n", node->full_name);
  250. pr_debug(" local store : 0x%016lx -> 0x%p\n",
  251. spu->local_store_phys, spu->local_store);
  252. pr_debug(" problem state : 0x%016lx -> 0x%p\n",
  253. spu->problem_phys, spu->problem);
  254. pr_debug(" priv2 : 0x%p\n", spu->priv2);
  255. pr_debug(" priv1 : 0x%p\n",
  256. spu_get_pdata(spu)->priv1);
  257. return 0;
  258. out_unmap:
  259. spu_unmap(spu);
  260. out:
  261. pr_debug("failed to map spe %s: %d\n", spu->name, ret);
  262. return ret;
  263. }
  264. static int __init of_enumerate_spus(int (*fn)(void *data))
  265. {
  266. int ret;
  267. struct device_node *node;
  268. ret = -ENODEV;
  269. for (node = of_find_node_by_type(NULL, "spe");
  270. node; node = of_find_node_by_type(node, "spe")) {
  271. ret = fn(node);
  272. if (ret) {
  273. printk(KERN_WARNING "%s: Error initializing %s\n",
  274. __FUNCTION__, node->name);
  275. break;
  276. }
  277. }
  278. return ret;
  279. }
  280. static int __init of_create_spu(struct spu *spu, void *data)
  281. {
  282. int ret;
  283. struct device_node *spe = (struct device_node *)data;
  284. spu->pdata = kzalloc(sizeof(struct spu_pdata),
  285. GFP_KERNEL);
  286. if (!spu->pdata) {
  287. ret = -ENOMEM;
  288. goto out;
  289. }
  290. spu->node = find_spu_node_id(spe);
  291. if (spu->node >= MAX_NUMNODES) {
  292. printk(KERN_WARNING "SPE %s on node %d ignored,"
  293. " node number too big\n", spe->full_name, spu->node);
  294. printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
  295. ret = -ENODEV;
  296. goto out_free;
  297. }
  298. spu_get_pdata(spu)->nid = of_node_to_nid(spe);
  299. if (spu_get_pdata(spu)->nid == -1)
  300. spu_get_pdata(spu)->nid = 0;
  301. ret = spu_map_device(spu, spe);
  302. /* try old method */
  303. if (ret)
  304. ret = spu_map_device_old(spu, spe);
  305. if (ret)
  306. goto out_free;
  307. ret = spu_map_interrupts(spu, spe);
  308. if (ret)
  309. ret = spu_map_interrupts_old(spu, spe);
  310. if (ret)
  311. goto out_unmap;
  312. spu_get_pdata(spu)->devnode = of_node_get(spe);
  313. pr_debug(KERN_DEBUG "Using SPE %s %p %p %p %p %d\n", spu->name,
  314. spu->local_store, spu->problem, spu_get_pdata(spu)->priv1,
  315. spu->priv2, spu->number);
  316. goto out;
  317. out_unmap:
  318. spu_unmap(spu);
  319. out_free:
  320. kfree(spu->pdata);
  321. spu->pdata = NULL;
  322. out:
  323. return ret;
  324. }
  325. static int of_destroy_spu(struct spu *spu)
  326. {
  327. spu_unmap(spu);
  328. of_node_put(spu_get_pdata(spu)->devnode);
  329. kfree(spu->pdata);
  330. spu->pdata = NULL;
  331. return 0;
  332. }
  333. const struct spu_management_ops spu_management_of_ops = {
  334. .enumerate_spus = of_enumerate_spus,
  335. .create_spu = of_create_spu,
  336. .destroy_spu = of_destroy_spu,
  337. };
  338. static void int_mask_and(struct spu *spu, int class, u64 mask)
  339. {
  340. u64 old_mask;
  341. old_mask = in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
  342. out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class],
  343. old_mask & mask);
  344. }
  345. static void int_mask_or(struct spu *spu, int class, u64 mask)
  346. {
  347. u64 old_mask;
  348. old_mask = in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
  349. out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class],
  350. old_mask | mask);
  351. }
  352. static void int_mask_set(struct spu *spu, int class, u64 mask)
  353. {
  354. out_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class], mask);
  355. }
  356. static u64 int_mask_get(struct spu *spu, int class)
  357. {
  358. return in_be64(&spu_get_pdata(spu)->priv1->int_mask_RW[class]);
  359. }
  360. static void int_stat_clear(struct spu *spu, int class, u64 stat)
  361. {
  362. out_be64(&spu_get_pdata(spu)->priv1->int_stat_RW[class], stat);
  363. }
  364. static u64 int_stat_get(struct spu *spu, int class)
  365. {
  366. return in_be64(&spu_get_pdata(spu)->priv1->int_stat_RW[class]);
  367. }
  368. static void cpu_affinity_set(struct spu *spu, int cpu)
  369. {
  370. u64 target = iic_get_target_id(cpu);
  371. u64 route = target << 48 | target << 32 | target << 16;
  372. out_be64(&spu_get_pdata(spu)->priv1->int_route_RW, route);
  373. }
  374. static u64 mfc_dar_get(struct spu *spu)
  375. {
  376. return in_be64(&spu_get_pdata(spu)->priv1->mfc_dar_RW);
  377. }
  378. static u64 mfc_dsisr_get(struct spu *spu)
  379. {
  380. return in_be64(&spu_get_pdata(spu)->priv1->mfc_dsisr_RW);
  381. }
  382. static void mfc_dsisr_set(struct spu *spu, u64 dsisr)
  383. {
  384. out_be64(&spu_get_pdata(spu)->priv1->mfc_dsisr_RW, dsisr);
  385. }
  386. static void mfc_sdr_setup(struct spu *spu)
  387. {
  388. out_be64(&spu_get_pdata(spu)->priv1->mfc_sdr_RW, mfspr(SPRN_SDR1));
  389. }
  390. static void mfc_sr1_set(struct spu *spu, u64 sr1)
  391. {
  392. out_be64(&spu_get_pdata(spu)->priv1->mfc_sr1_RW, sr1);
  393. }
  394. static u64 mfc_sr1_get(struct spu *spu)
  395. {
  396. return in_be64(&spu_get_pdata(spu)->priv1->mfc_sr1_RW);
  397. }
  398. static void mfc_tclass_id_set(struct spu *spu, u64 tclass_id)
  399. {
  400. out_be64(&spu_get_pdata(spu)->priv1->mfc_tclass_id_RW, tclass_id);
  401. }
  402. static u64 mfc_tclass_id_get(struct spu *spu)
  403. {
  404. return in_be64(&spu_get_pdata(spu)->priv1->mfc_tclass_id_RW);
  405. }
  406. static void tlb_invalidate(struct spu *spu)
  407. {
  408. out_be64(&spu_get_pdata(spu)->priv1->tlb_invalidate_entry_W, 0ul);
  409. }
  410. static void resource_allocation_groupID_set(struct spu *spu, u64 id)
  411. {
  412. out_be64(&spu_get_pdata(spu)->priv1->resource_allocation_groupID_RW,
  413. id);
  414. }
  415. static u64 resource_allocation_groupID_get(struct spu *spu)
  416. {
  417. return in_be64(
  418. &spu_get_pdata(spu)->priv1->resource_allocation_groupID_RW);
  419. }
  420. static void resource_allocation_enable_set(struct spu *spu, u64 enable)
  421. {
  422. out_be64(&spu_get_pdata(spu)->priv1->resource_allocation_enable_RW,
  423. enable);
  424. }
  425. static u64 resource_allocation_enable_get(struct spu *spu)
  426. {
  427. return in_be64(
  428. &spu_get_pdata(spu)->priv1->resource_allocation_enable_RW);
  429. }
  430. const struct spu_priv1_ops spu_priv1_mmio_ops =
  431. {
  432. .int_mask_and = int_mask_and,
  433. .int_mask_or = int_mask_or,
  434. .int_mask_set = int_mask_set,
  435. .int_mask_get = int_mask_get,
  436. .int_stat_clear = int_stat_clear,
  437. .int_stat_get = int_stat_get,
  438. .cpu_affinity_set = cpu_affinity_set,
  439. .mfc_dar_get = mfc_dar_get,
  440. .mfc_dsisr_get = mfc_dsisr_get,
  441. .mfc_dsisr_set = mfc_dsisr_set,
  442. .mfc_sdr_setup = mfc_sdr_setup,
  443. .mfc_sr1_set = mfc_sr1_set,
  444. .mfc_sr1_get = mfc_sr1_get,
  445. .mfc_tclass_id_set = mfc_tclass_id_set,
  446. .mfc_tclass_id_get = mfc_tclass_id_get,
  447. .tlb_invalidate = tlb_invalidate,
  448. .resource_allocation_groupID_set = resource_allocation_groupID_set,
  449. .resource_allocation_groupID_get = resource_allocation_groupID_get,
  450. .resource_allocation_enable_set = resource_allocation_enable_set,
  451. .resource_allocation_enable_get = resource_allocation_enable_get,
  452. };