iommu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <asm/sections.h>
  31. #include <asm/iommu.h>
  32. #include <asm/io.h>
  33. #include <asm/prom.h>
  34. #include <asm/pci-bridge.h>
  35. #include <asm/machdep.h>
  36. #include <asm/pmac_feature.h>
  37. #include <asm/abs_addr.h>
  38. #include <asm/system.h>
  39. #include <asm/ppc-pci.h>
  40. #include "iommu.h"
  41. static inline unsigned long
  42. get_iopt_entry(unsigned long real_address, unsigned long ioid,
  43. unsigned long prot)
  44. {
  45. return (prot & IOPT_PROT_MASK)
  46. | (IOPT_COHERENT)
  47. | (IOPT_ORDER_VC)
  48. | (real_address & IOPT_RPN_MASK)
  49. | (ioid & IOPT_IOID_MASK);
  50. }
  51. typedef struct {
  52. unsigned long val;
  53. } ioste;
  54. static inline ioste
  55. mk_ioste(unsigned long val)
  56. {
  57. ioste ioste = { .val = val, };
  58. return ioste;
  59. }
  60. static inline ioste
  61. get_iost_entry(unsigned long iopt_base, unsigned long io_address, unsigned page_size)
  62. {
  63. unsigned long ps;
  64. unsigned long iostep;
  65. unsigned long nnpt;
  66. unsigned long shift;
  67. switch (page_size) {
  68. case 0x1000000:
  69. ps = IOST_PS_16M;
  70. nnpt = 0; /* one page per segment */
  71. shift = 5; /* segment has 16 iopt entries */
  72. break;
  73. case 0x100000:
  74. ps = IOST_PS_1M;
  75. nnpt = 0; /* one page per segment */
  76. shift = 1; /* segment has 256 iopt entries */
  77. break;
  78. case 0x10000:
  79. ps = IOST_PS_64K;
  80. nnpt = 0x07; /* 8 pages per io page table */
  81. shift = 0; /* all entries are used */
  82. break;
  83. case 0x1000:
  84. ps = IOST_PS_4K;
  85. nnpt = 0x7f; /* 128 pages per io page table */
  86. shift = 0; /* all entries are used */
  87. break;
  88. default: /* not a known compile time constant */
  89. {
  90. /* BUILD_BUG_ON() is not usable here */
  91. extern void __get_iost_entry_bad_page_size(void);
  92. __get_iost_entry_bad_page_size();
  93. }
  94. break;
  95. }
  96. iostep = iopt_base +
  97. /* need 8 bytes per iopte */
  98. (((io_address / page_size * 8)
  99. /* align io page tables on 4k page boundaries */
  100. << shift)
  101. /* nnpt+1 pages go into each iopt */
  102. & ~(nnpt << 12));
  103. nnpt++; /* this seems to work, but the documentation is not clear
  104. about wether we put nnpt or nnpt-1 into the ioste bits.
  105. In theory, this can't work for 4k pages. */
  106. return mk_ioste(IOST_VALID_MASK
  107. | (iostep & IOST_PT_BASE_MASK)
  108. | ((nnpt << 5) & IOST_NNPT_MASK)
  109. | (ps & IOST_PS_MASK));
  110. }
  111. /* compute the address of an io pte */
  112. static inline unsigned long
  113. get_ioptep(ioste iost_entry, unsigned long io_address)
  114. {
  115. unsigned long iopt_base;
  116. unsigned long page_size;
  117. unsigned long page_number;
  118. unsigned long iopt_offset;
  119. iopt_base = iost_entry.val & IOST_PT_BASE_MASK;
  120. page_size = iost_entry.val & IOST_PS_MASK;
  121. /* decode page size to compute page number */
  122. page_number = (io_address & 0x0fffffff) >> (10 + 2 * page_size);
  123. /* page number is an offset into the io page table */
  124. iopt_offset = (page_number << 3) & 0x7fff8ul;
  125. return iopt_base + iopt_offset;
  126. }
  127. /* compute the tag field of the iopt cache entry */
  128. static inline unsigned long
  129. get_ioc_tag(ioste iost_entry, unsigned long io_address)
  130. {
  131. unsigned long iopte = get_ioptep(iost_entry, io_address);
  132. return IOPT_VALID_MASK
  133. | ((iopte & 0x00000000000000ff8ul) >> 3)
  134. | ((iopte & 0x0000003fffffc0000ul) >> 9);
  135. }
  136. /* compute the hashed 6 bit index for the 4-way associative pte cache */
  137. static inline unsigned long
  138. get_ioc_hash(ioste iost_entry, unsigned long io_address)
  139. {
  140. unsigned long iopte = get_ioptep(iost_entry, io_address);
  141. return ((iopte & 0x000000000000001f8ul) >> 3)
  142. ^ ((iopte & 0x00000000000020000ul) >> 17)
  143. ^ ((iopte & 0x00000000000010000ul) >> 15)
  144. ^ ((iopte & 0x00000000000008000ul) >> 13)
  145. ^ ((iopte & 0x00000000000004000ul) >> 11)
  146. ^ ((iopte & 0x00000000000002000ul) >> 9)
  147. ^ ((iopte & 0x00000000000001000ul) >> 7);
  148. }
  149. /* same as above, but pretend that we have a simpler 1-way associative
  150. pte cache with an 8 bit index */
  151. static inline unsigned long
  152. get_ioc_hash_1way(ioste iost_entry, unsigned long io_address)
  153. {
  154. unsigned long iopte = get_ioptep(iost_entry, io_address);
  155. return ((iopte & 0x000000000000001f8ul) >> 3)
  156. ^ ((iopte & 0x00000000000020000ul) >> 17)
  157. ^ ((iopte & 0x00000000000010000ul) >> 15)
  158. ^ ((iopte & 0x00000000000008000ul) >> 13)
  159. ^ ((iopte & 0x00000000000004000ul) >> 11)
  160. ^ ((iopte & 0x00000000000002000ul) >> 9)
  161. ^ ((iopte & 0x00000000000001000ul) >> 7)
  162. ^ ((iopte & 0x0000000000000c000ul) >> 8);
  163. }
  164. static inline ioste
  165. get_iost_cache(void __iomem *base, unsigned long index)
  166. {
  167. unsigned long __iomem *p = (base + IOC_ST_CACHE_DIR);
  168. return mk_ioste(in_be64(&p[index]));
  169. }
  170. static inline void
  171. set_iost_cache(void __iomem *base, unsigned long index, ioste ste)
  172. {
  173. unsigned long __iomem *p = (base + IOC_ST_CACHE_DIR);
  174. pr_debug("ioste %02lx was %016lx, store %016lx", index,
  175. get_iost_cache(base, index).val, ste.val);
  176. out_be64(&p[index], ste.val);
  177. pr_debug(" now %016lx\n", get_iost_cache(base, index).val);
  178. }
  179. static inline unsigned long
  180. get_iopt_cache(void __iomem *base, unsigned long index, unsigned long *tag)
  181. {
  182. unsigned long __iomem *tags = (void *)(base + IOC_PT_CACHE_DIR);
  183. unsigned long __iomem *p = (void *)(base + IOC_PT_CACHE_REG);
  184. *tag = tags[index];
  185. rmb();
  186. return *p;
  187. }
  188. static inline void
  189. set_iopt_cache(void __iomem *base, unsigned long index,
  190. unsigned long tag, unsigned long val)
  191. {
  192. unsigned long __iomem *tags = base + IOC_PT_CACHE_DIR;
  193. unsigned long __iomem *p = base + IOC_PT_CACHE_REG;
  194. pr_debug("iopt %02lx was v%016lx/t%016lx, store v%016lx/t%016lx\n",
  195. index, get_iopt_cache(base, index, &oldtag), oldtag, val, tag);
  196. out_be64(p, val);
  197. out_be64(&tags[index], tag);
  198. }
  199. static inline void
  200. set_iost_origin(void __iomem *base)
  201. {
  202. unsigned long __iomem *p = base + IOC_ST_ORIGIN;
  203. unsigned long origin = IOSTO_ENABLE | IOSTO_SW;
  204. pr_debug("iost_origin %016lx, now %016lx\n", in_be64(p), origin);
  205. out_be64(p, origin);
  206. }
  207. static inline void
  208. set_iocmd_config(void __iomem *base)
  209. {
  210. unsigned long __iomem *p = base + 0xc00;
  211. unsigned long conf;
  212. conf = in_be64(p);
  213. pr_debug("iost_conf %016lx, now %016lx\n", conf, conf | IOCMD_CONF_TE);
  214. out_be64(p, conf | IOCMD_CONF_TE);
  215. }
  216. /* FIXME: get these from the device tree */
  217. #define ioc_base 0x20000511000ull
  218. #define ioc_mmio_base 0x20000510000ull
  219. #define ioid 0x48a
  220. #define iopt_phys_offset (- 0x20000000) /* We have a 512MB offset from the SB */
  221. #define io_page_size 0x1000000
  222. static unsigned long map_iopt_entry(unsigned long address)
  223. {
  224. switch (address >> 20) {
  225. case 0x600:
  226. address = 0x24020000000ull; /* spider i/o */
  227. break;
  228. default:
  229. address += iopt_phys_offset;
  230. break;
  231. }
  232. return get_iopt_entry(address, ioid, IOPT_PROT_RW);
  233. }
  234. static void iommu_bus_setup_null(struct pci_bus *b) { }
  235. static void iommu_dev_setup_null(struct pci_dev *d) { }
  236. /* initialize the iommu to support a simple linear mapping
  237. * for each DMA window used by any device. For now, we
  238. * happen to know that there is only one DMA window in use,
  239. * starting at iopt_phys_offset. */
  240. static void cell_map_iommu(void)
  241. {
  242. unsigned long address;
  243. void __iomem *base;
  244. ioste ioste;
  245. unsigned long index;
  246. base = __ioremap(ioc_base, 0x1000, _PAGE_NO_CACHE);
  247. pr_debug("%lx mapped to %p\n", ioc_base, base);
  248. set_iocmd_config(base);
  249. iounmap(base);
  250. base = __ioremap(ioc_mmio_base, 0x1000, _PAGE_NO_CACHE);
  251. pr_debug("%lx mapped to %p\n", ioc_mmio_base, base);
  252. set_iost_origin(base);
  253. for (address = 0; address < 0x100000000ul; address += io_page_size) {
  254. ioste = get_iost_entry(0x10000000000ul, address, io_page_size);
  255. if ((address & 0xfffffff) == 0) /* segment start */
  256. set_iost_cache(base, address >> 28, ioste);
  257. index = get_ioc_hash_1way(ioste, address);
  258. pr_debug("addr %08lx, index %02lx, ioste %016lx\n",
  259. address, index, ioste.val);
  260. set_iopt_cache(base,
  261. get_ioc_hash_1way(ioste, address),
  262. get_ioc_tag(ioste, address),
  263. map_iopt_entry(address));
  264. }
  265. iounmap(base);
  266. }
  267. static void *cell_alloc_coherent(struct device *hwdev, size_t size,
  268. dma_addr_t *dma_handle, gfp_t flag)
  269. {
  270. void *ret;
  271. ret = (void *)__get_free_pages(flag, get_order(size));
  272. if (ret != NULL) {
  273. memset(ret, 0, size);
  274. *dma_handle = virt_to_abs(ret) | CELL_DMA_VALID;
  275. }
  276. return ret;
  277. }
  278. static void cell_free_coherent(struct device *hwdev, size_t size,
  279. void *vaddr, dma_addr_t dma_handle)
  280. {
  281. free_pages((unsigned long)vaddr, get_order(size));
  282. }
  283. static dma_addr_t cell_map_single(struct device *hwdev, void *ptr,
  284. size_t size, enum dma_data_direction direction)
  285. {
  286. return virt_to_abs(ptr) | CELL_DMA_VALID;
  287. }
  288. static void cell_unmap_single(struct device *hwdev, dma_addr_t dma_addr,
  289. size_t size, enum dma_data_direction direction)
  290. {
  291. }
  292. static int cell_map_sg(struct device *hwdev, struct scatterlist *sg,
  293. int nents, enum dma_data_direction direction)
  294. {
  295. int i;
  296. for (i = 0; i < nents; i++, sg++) {
  297. sg->dma_address = (page_to_phys(sg->page) + sg->offset)
  298. | CELL_DMA_VALID;
  299. sg->dma_length = sg->length;
  300. }
  301. return nents;
  302. }
  303. static void cell_unmap_sg(struct device *hwdev, struct scatterlist *sg,
  304. int nents, enum dma_data_direction direction)
  305. {
  306. }
  307. static int cell_dma_supported(struct device *dev, u64 mask)
  308. {
  309. return mask < 0x100000000ull;
  310. }
  311. void cell_init_iommu(void)
  312. {
  313. cell_map_iommu();
  314. /* Direct I/O, IOMMU off */
  315. ppc_md.iommu_dev_setup = iommu_dev_setup_null;
  316. ppc_md.iommu_bus_setup = iommu_bus_setup_null;
  317. pci_dma_ops.alloc_coherent = cell_alloc_coherent;
  318. pci_dma_ops.free_coherent = cell_free_coherent;
  319. pci_dma_ops.map_single = cell_map_single;
  320. pci_dma_ops.unmap_single = cell_unmap_single;
  321. pci_dma_ops.map_sg = cell_map_sg;
  322. pci_dma_ops.unmap_sg = cell_unmap_sg;
  323. pci_dma_ops.dma_supported = cell_dma_supported;
  324. }