iommu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * IOMMU implementation for Cell Broadband Processor Architecture
  3. * We just establish a linear mapping at boot by setting all the
  4. * IOPT cache entries in the CPU.
  5. * The mapping functions should be identical to pci_direct_iommu,
  6. * except for the handling of the high order bit that is required
  7. * by the Spider bridge. These should be split into a separate
  8. * file at the point where we get a different bridge chip.
  9. *
  10. * Copyright (C) 2005 IBM Deutschland Entwicklung GmbH,
  11. * Arnd Bergmann <arndb@de.ibm.com>
  12. *
  13. * Based on linear mapping
  14. * Copyright (C) 2003 Benjamin Herrenschmidt (benh@kernel.crashing.org)
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. */
  21. #undef DEBUG
  22. #include <linux/kernel.h>
  23. #include <linux/pci.h>
  24. #include <linux/delay.h>
  25. #include <linux/string.h>
  26. #include <linux/init.h>
  27. #include <linux/bootmem.h>
  28. #include <linux/mm.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/kernel.h>
  31. #include <linux/compiler.h>
  32. #include <asm/sections.h>
  33. #include <asm/iommu.h>
  34. #include <asm/io.h>
  35. #include <asm/prom.h>
  36. #include <asm/pci-bridge.h>
  37. #include <asm/machdep.h>
  38. #include <asm/pmac_feature.h>
  39. #include <asm/abs_addr.h>
  40. #include <asm/system.h>
  41. #include <asm/ppc-pci.h>
  42. #include <asm/udbg.h>
  43. #include "iommu.h"
  44. static inline unsigned long
  45. get_iopt_entry(unsigned long real_address, unsigned long ioid,
  46. unsigned long prot)
  47. {
  48. return (prot & IOPT_PROT_MASK)
  49. | (IOPT_COHERENT)
  50. | (IOPT_ORDER_VC)
  51. | (real_address & IOPT_RPN_MASK)
  52. | (ioid & IOPT_IOID_MASK);
  53. }
  54. typedef struct {
  55. unsigned long val;
  56. } ioste;
  57. static inline ioste
  58. mk_ioste(unsigned long val)
  59. {
  60. ioste ioste = { .val = val, };
  61. return ioste;
  62. }
  63. static inline ioste
  64. get_iost_entry(unsigned long iopt_base, unsigned long io_address, unsigned page_size)
  65. {
  66. unsigned long ps;
  67. unsigned long iostep;
  68. unsigned long nnpt;
  69. unsigned long shift;
  70. switch (page_size) {
  71. case 0x1000000:
  72. ps = IOST_PS_16M;
  73. nnpt = 0; /* one page per segment */
  74. shift = 5; /* segment has 16 iopt entries */
  75. break;
  76. case 0x100000:
  77. ps = IOST_PS_1M;
  78. nnpt = 0; /* one page per segment */
  79. shift = 1; /* segment has 256 iopt entries */
  80. break;
  81. case 0x10000:
  82. ps = IOST_PS_64K;
  83. nnpt = 0x07; /* 8 pages per io page table */
  84. shift = 0; /* all entries are used */
  85. break;
  86. case 0x1000:
  87. ps = IOST_PS_4K;
  88. nnpt = 0x7f; /* 128 pages per io page table */
  89. shift = 0; /* all entries are used */
  90. break;
  91. default: /* not a known compile time constant */
  92. {
  93. /* BUILD_BUG_ON() is not usable here */
  94. extern void __get_iost_entry_bad_page_size(void);
  95. __get_iost_entry_bad_page_size();
  96. }
  97. break;
  98. }
  99. iostep = iopt_base +
  100. /* need 8 bytes per iopte */
  101. (((io_address / page_size * 8)
  102. /* align io page tables on 4k page boundaries */
  103. << shift)
  104. /* nnpt+1 pages go into each iopt */
  105. & ~(nnpt << 12));
  106. nnpt++; /* this seems to work, but the documentation is not clear
  107. about wether we put nnpt or nnpt-1 into the ioste bits.
  108. In theory, this can't work for 4k pages. */
  109. return mk_ioste(IOST_VALID_MASK
  110. | (iostep & IOST_PT_BASE_MASK)
  111. | ((nnpt << 5) & IOST_NNPT_MASK)
  112. | (ps & IOST_PS_MASK));
  113. }
  114. /* compute the address of an io pte */
  115. static inline unsigned long
  116. get_ioptep(ioste iost_entry, unsigned long io_address)
  117. {
  118. unsigned long iopt_base;
  119. unsigned long page_size;
  120. unsigned long page_number;
  121. unsigned long iopt_offset;
  122. iopt_base = iost_entry.val & IOST_PT_BASE_MASK;
  123. page_size = iost_entry.val & IOST_PS_MASK;
  124. /* decode page size to compute page number */
  125. page_number = (io_address & 0x0fffffff) >> (10 + 2 * page_size);
  126. /* page number is an offset into the io page table */
  127. iopt_offset = (page_number << 3) & 0x7fff8ul;
  128. return iopt_base + iopt_offset;
  129. }
  130. /* compute the tag field of the iopt cache entry */
  131. static inline unsigned long
  132. get_ioc_tag(ioste iost_entry, unsigned long io_address)
  133. {
  134. unsigned long iopte = get_ioptep(iost_entry, io_address);
  135. return IOPT_VALID_MASK
  136. | ((iopte & 0x00000000000000ff8ul) >> 3)
  137. | ((iopte & 0x0000003fffffc0000ul) >> 9);
  138. }
  139. /* compute the hashed 6 bit index for the 4-way associative pte cache */
  140. static inline unsigned long
  141. get_ioc_hash(ioste iost_entry, unsigned long io_address)
  142. {
  143. unsigned long iopte = get_ioptep(iost_entry, io_address);
  144. return ((iopte & 0x000000000000001f8ul) >> 3)
  145. ^ ((iopte & 0x00000000000020000ul) >> 17)
  146. ^ ((iopte & 0x00000000000010000ul) >> 15)
  147. ^ ((iopte & 0x00000000000008000ul) >> 13)
  148. ^ ((iopte & 0x00000000000004000ul) >> 11)
  149. ^ ((iopte & 0x00000000000002000ul) >> 9)
  150. ^ ((iopte & 0x00000000000001000ul) >> 7);
  151. }
  152. /* same as above, but pretend that we have a simpler 1-way associative
  153. pte cache with an 8 bit index */
  154. static inline unsigned long
  155. get_ioc_hash_1way(ioste iost_entry, unsigned long io_address)
  156. {
  157. unsigned long iopte = get_ioptep(iost_entry, io_address);
  158. return ((iopte & 0x000000000000001f8ul) >> 3)
  159. ^ ((iopte & 0x00000000000020000ul) >> 17)
  160. ^ ((iopte & 0x00000000000010000ul) >> 15)
  161. ^ ((iopte & 0x00000000000008000ul) >> 13)
  162. ^ ((iopte & 0x00000000000004000ul) >> 11)
  163. ^ ((iopte & 0x00000000000002000ul) >> 9)
  164. ^ ((iopte & 0x00000000000001000ul) >> 7)
  165. ^ ((iopte & 0x0000000000000c000ul) >> 8);
  166. }
  167. static inline ioste
  168. get_iost_cache(void __iomem *base, unsigned long index)
  169. {
  170. unsigned long __iomem *p = (base + IOC_ST_CACHE_DIR);
  171. return mk_ioste(in_be64(&p[index]));
  172. }
  173. static inline void
  174. set_iost_cache(void __iomem *base, unsigned long index, ioste ste)
  175. {
  176. unsigned long __iomem *p = (base + IOC_ST_CACHE_DIR);
  177. pr_debug("ioste %02lx was %016lx, store %016lx", index,
  178. get_iost_cache(base, index).val, ste.val);
  179. out_be64(&p[index], ste.val);
  180. pr_debug(" now %016lx\n", get_iost_cache(base, index).val);
  181. }
  182. static inline unsigned long
  183. get_iopt_cache(void __iomem *base, unsigned long index, unsigned long *tag)
  184. {
  185. unsigned long __iomem *tags = (void *)(base + IOC_PT_CACHE_DIR);
  186. unsigned long __iomem *p = (void *)(base + IOC_PT_CACHE_REG);
  187. *tag = tags[index];
  188. rmb();
  189. return *p;
  190. }
  191. static inline void
  192. set_iopt_cache(void __iomem *base, unsigned long index,
  193. unsigned long tag, unsigned long val)
  194. {
  195. unsigned long __iomem *tags = base + IOC_PT_CACHE_DIR;
  196. unsigned long __iomem *p = base + IOC_PT_CACHE_REG;
  197. out_be64(p, val);
  198. out_be64(&tags[index], tag);
  199. }
  200. static inline void
  201. set_iost_origin(void __iomem *base)
  202. {
  203. unsigned long __iomem *p = base + IOC_ST_ORIGIN;
  204. unsigned long origin = IOSTO_ENABLE | IOSTO_SW;
  205. pr_debug("iost_origin %016lx, now %016lx\n", in_be64(p), origin);
  206. out_be64(p, origin);
  207. }
  208. static inline void
  209. set_iocmd_config(void __iomem *base)
  210. {
  211. unsigned long __iomem *p = base + 0xc00;
  212. unsigned long conf;
  213. conf = in_be64(p);
  214. pr_debug("iost_conf %016lx, now %016lx\n", conf, conf | IOCMD_CONF_TE);
  215. out_be64(p, conf | IOCMD_CONF_TE);
  216. }
  217. static void enable_mapping(void __iomem *base, void __iomem *mmio_base)
  218. {
  219. set_iocmd_config(base);
  220. set_iost_origin(mmio_base);
  221. }
  222. static void iommu_dev_setup_null(struct pci_dev *d) { }
  223. static void iommu_bus_setup_null(struct pci_bus *b) { }
  224. struct cell_iommu {
  225. unsigned long base;
  226. unsigned long mmio_base;
  227. void __iomem *mapped_base;
  228. void __iomem *mapped_mmio_base;
  229. };
  230. static struct cell_iommu cell_iommus[NR_CPUS];
  231. /* initialize the iommu to support a simple linear mapping
  232. * for each DMA window used by any device. For now, we
  233. * happen to know that there is only one DMA window in use,
  234. * starting at iopt_phys_offset. */
  235. static void cell_do_map_iommu(struct cell_iommu *iommu,
  236. unsigned int ioid,
  237. unsigned long map_start,
  238. unsigned long map_size)
  239. {
  240. unsigned long io_address, real_address;
  241. void __iomem *ioc_base, *ioc_mmio_base;
  242. ioste ioste;
  243. unsigned long index;
  244. /* we pretend the io page table was at a very high address */
  245. const unsigned long fake_iopt = 0x10000000000ul;
  246. const unsigned long io_page_size = 0x1000000; /* use 16M pages */
  247. const unsigned long io_segment_size = 0x10000000; /* 256M */
  248. ioc_base = iommu->mapped_base;
  249. ioc_mmio_base = iommu->mapped_mmio_base;
  250. for (real_address = 0, io_address = map_start;
  251. io_address <= map_start + map_size;
  252. real_address += io_page_size, io_address += io_page_size) {
  253. ioste = get_iost_entry(fake_iopt, io_address, io_page_size);
  254. if ((real_address % io_segment_size) == 0) /* segment start */
  255. set_iost_cache(ioc_mmio_base,
  256. io_address >> 28, ioste);
  257. index = get_ioc_hash_1way(ioste, io_address);
  258. pr_debug("addr %08lx, index %02lx, ioste %016lx\n",
  259. io_address, index, ioste.val);
  260. set_iopt_cache(ioc_mmio_base,
  261. get_ioc_hash_1way(ioste, io_address),
  262. get_ioc_tag(ioste, io_address),
  263. get_iopt_entry(real_address, ioid, IOPT_PROT_RW));
  264. }
  265. }
  266. static void iommu_devnode_setup(struct device_node *d)
  267. {
  268. unsigned int *ioid;
  269. unsigned long *dma_window, map_start, map_size, token;
  270. struct cell_iommu *iommu;
  271. ioid = (unsigned int *)get_property(d, "ioid", NULL);
  272. if (!ioid)
  273. pr_debug("No ioid entry found !\n");
  274. dma_window = (unsigned long *)get_property(d, "ibm,dma-window", NULL);
  275. if (!dma_window)
  276. pr_debug("No ibm,dma-window entry found !\n");
  277. map_start = dma_window[1];
  278. map_size = dma_window[2];
  279. token = dma_window[0] >> 32;
  280. iommu = &cell_iommus[token];
  281. cell_do_map_iommu(iommu, *ioid, map_start, map_size);
  282. }
  283. static void iommu_bus_setup(struct pci_bus *b)
  284. {
  285. struct device_node *d = (struct device_node *)b->sysdata;
  286. iommu_devnode_setup(d);
  287. }
  288. static int cell_map_iommu_hardcoded(int num_nodes)
  289. {
  290. struct cell_iommu *iommu = NULL;
  291. pr_debug("%s(%d): Using hardcoded defaults\n", __FUNCTION__, __LINE__);
  292. /* node 0 */
  293. iommu = &cell_iommus[0];
  294. iommu->mapped_base = ioremap(0x20000511000, 0x1000);
  295. iommu->mapped_mmio_base = ioremap(0x20000510000, 0x1000);
  296. enable_mapping(iommu->mapped_base, iommu->mapped_mmio_base);
  297. cell_do_map_iommu(iommu, 0x048a,
  298. 0x20000000ul,0x20000000ul);
  299. if (num_nodes < 2)
  300. return 0;
  301. /* node 1 */
  302. iommu = &cell_iommus[1];
  303. iommu->mapped_base = ioremap(0x30000511000, 0x1000);
  304. iommu->mapped_mmio_base = ioremap(0x30000510000, 0x1000);
  305. enable_mapping(iommu->mapped_base, iommu->mapped_mmio_base);
  306. cell_do_map_iommu(iommu, 0x048a,
  307. 0x20000000,0x20000000ul);
  308. return 0;
  309. }
  310. static int cell_map_iommu(void)
  311. {
  312. unsigned int num_nodes = 0, *node_id;
  313. unsigned long *base, *mmio_base;
  314. struct device_node *dn;
  315. struct cell_iommu *iommu = NULL;
  316. /* determine number of nodes (=iommus) */
  317. pr_debug("%s(%d): determining number of nodes...", __FUNCTION__, __LINE__);
  318. for(dn = of_find_node_by_type(NULL, "cpu");
  319. dn;
  320. dn = of_find_node_by_type(dn, "cpu")) {
  321. node_id = (unsigned int *)get_property(dn, "node-id", NULL);
  322. if (num_nodes < *node_id)
  323. num_nodes = *node_id;
  324. }
  325. num_nodes++;
  326. pr_debug("%i found.\n", num_nodes);
  327. /* map the iommu registers for each node */
  328. pr_debug("%s(%d): Looping through nodes\n", __FUNCTION__, __LINE__);
  329. for(dn = of_find_node_by_type(NULL, "cpu");
  330. dn;
  331. dn = of_find_node_by_type(dn, "cpu")) {
  332. node_id = (unsigned int *)get_property(dn, "node-id", NULL);
  333. base = (unsigned long *)get_property(dn, "ioc-cache", NULL);
  334. mmio_base = (unsigned long *)get_property(dn, "ioc-translation", NULL);
  335. if (!base || !mmio_base || !node_id)
  336. return cell_map_iommu_hardcoded(num_nodes);
  337. iommu = &cell_iommus[*node_id];
  338. iommu->base = *base;
  339. iommu->mmio_base = *mmio_base;
  340. iommu->mapped_base = ioremap(*base, 0x1000);
  341. iommu->mapped_mmio_base = ioremap(*mmio_base, 0x1000);
  342. enable_mapping(iommu->mapped_base,
  343. iommu->mapped_mmio_base);
  344. /* everything else will be done in iommu_bus_setup */
  345. }
  346. return 1;
  347. }
  348. static void *cell_alloc_coherent(struct device *hwdev, size_t size,
  349. dma_addr_t *dma_handle, gfp_t flag)
  350. {
  351. void *ret;
  352. ret = (void *)__get_free_pages(flag, get_order(size));
  353. if (ret != NULL) {
  354. memset(ret, 0, size);
  355. *dma_handle = virt_to_abs(ret) | CELL_DMA_VALID;
  356. }
  357. return ret;
  358. }
  359. static void cell_free_coherent(struct device *hwdev, size_t size,
  360. void *vaddr, dma_addr_t dma_handle)
  361. {
  362. free_pages((unsigned long)vaddr, get_order(size));
  363. }
  364. static dma_addr_t cell_map_single(struct device *hwdev, void *ptr,
  365. size_t size, enum dma_data_direction direction)
  366. {
  367. return virt_to_abs(ptr) | CELL_DMA_VALID;
  368. }
  369. static void cell_unmap_single(struct device *hwdev, dma_addr_t dma_addr,
  370. size_t size, enum dma_data_direction direction)
  371. {
  372. }
  373. static int cell_map_sg(struct device *hwdev, struct scatterlist *sg,
  374. int nents, enum dma_data_direction direction)
  375. {
  376. int i;
  377. for (i = 0; i < nents; i++, sg++) {
  378. sg->dma_address = (page_to_phys(sg->page) + sg->offset)
  379. | CELL_DMA_VALID;
  380. sg->dma_length = sg->length;
  381. }
  382. return nents;
  383. }
  384. static void cell_unmap_sg(struct device *hwdev, struct scatterlist *sg,
  385. int nents, enum dma_data_direction direction)
  386. {
  387. }
  388. static int cell_dma_supported(struct device *dev, u64 mask)
  389. {
  390. return mask < 0x100000000ull;
  391. }
  392. void cell_init_iommu(void)
  393. {
  394. int setup_bus = 0;
  395. if (of_find_node_by_path("/mambo")) {
  396. pr_info("Not using iommu on systemsim\n");
  397. } else {
  398. if (!(of_chosen &&
  399. get_property(of_chosen, "linux,iommu-off", NULL)))
  400. setup_bus = cell_map_iommu();
  401. if (setup_bus) {
  402. pr_debug("%s: IOMMU mapping activated\n", __FUNCTION__);
  403. ppc_md.iommu_dev_setup = iommu_dev_setup_null;
  404. ppc_md.iommu_bus_setup = iommu_bus_setup;
  405. } else {
  406. pr_debug("%s: IOMMU mapping activated, "
  407. "no device action necessary\n", __FUNCTION__);
  408. /* Direct I/O, IOMMU off */
  409. ppc_md.iommu_dev_setup = iommu_dev_setup_null;
  410. ppc_md.iommu_bus_setup = iommu_bus_setup_null;
  411. }
  412. }
  413. pci_dma_ops.alloc_coherent = cell_alloc_coherent;
  414. pci_dma_ops.free_coherent = cell_free_coherent;
  415. pci_dma_ops.map_single = cell_map_single;
  416. pci_dma_ops.unmap_single = cell_unmap_single;
  417. pci_dma_ops.map_sg = cell_map_sg;
  418. pci_dma_ops.unmap_sg = cell_unmap_sg;
  419. pci_dma_ops.dma_supported = cell_dma_supported;
  420. }