spu_priv1_mmio.c 13 KB

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