bpa_iommu.c 10 KB

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