dart_iommu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * arch/powerpc/sysdev/dart_iommu.c
  3. *
  4. * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
  5. * Copyright (C) 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>,
  6. * IBM Corporation
  7. *
  8. * Based on pSeries_iommu.c:
  9. * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
  10. * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
  11. *
  12. * Dynamic DMA mapping support, Apple U3, U4 & IBM CPC925 "DART" iommu.
  13. *
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/mm.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/string.h>
  34. #include <linux/pci.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/suspend.h>
  38. #include <linux/memblock.h>
  39. #include <linux/gfp.h>
  40. #include <asm/io.h>
  41. #include <asm/prom.h>
  42. #include <asm/iommu.h>
  43. #include <asm/pci-bridge.h>
  44. #include <asm/machdep.h>
  45. #include <asm/cacheflush.h>
  46. #include <asm/ppc-pci.h>
  47. #include "dart.h"
  48. /* Physical base address and size of the DART table */
  49. unsigned long dart_tablebase; /* exported to htab_initialize */
  50. static unsigned long dart_tablesize;
  51. /* Virtual base address of the DART table */
  52. static u32 *dart_vbase;
  53. #ifdef CONFIG_PM
  54. static u32 *dart_copy;
  55. #endif
  56. /* Mapped base address for the dart */
  57. static unsigned int __iomem *dart;
  58. /* Dummy val that entries are set to when unused */
  59. static unsigned int dart_emptyval;
  60. static struct iommu_table iommu_table_dart;
  61. static int iommu_table_dart_inited;
  62. static int dart_dirty;
  63. static int dart_is_u4;
  64. #define DART_U4_BYPASS_BASE 0x8000000000ull
  65. #define DBG(...)
  66. static DEFINE_SPINLOCK(invalidate_lock);
  67. static inline void dart_tlb_invalidate_all(void)
  68. {
  69. unsigned long l = 0;
  70. unsigned int reg, inv_bit;
  71. unsigned long limit;
  72. unsigned long flags;
  73. spin_lock_irqsave(&invalidate_lock, flags);
  74. DBG("dart: flush\n");
  75. /* To invalidate the DART, set the DARTCNTL_FLUSHTLB bit in the
  76. * control register and wait for it to clear.
  77. *
  78. * Gotcha: Sometimes, the DART won't detect that the bit gets
  79. * set. If so, clear it and set it again.
  80. */
  81. limit = 0;
  82. inv_bit = dart_is_u4 ? DART_CNTL_U4_FLUSHTLB : DART_CNTL_U3_FLUSHTLB;
  83. retry:
  84. l = 0;
  85. reg = DART_IN(DART_CNTL);
  86. reg |= inv_bit;
  87. DART_OUT(DART_CNTL, reg);
  88. while ((DART_IN(DART_CNTL) & inv_bit) && l < (1L << limit))
  89. l++;
  90. if (l == (1L << limit)) {
  91. if (limit < 4) {
  92. limit++;
  93. reg = DART_IN(DART_CNTL);
  94. reg &= ~inv_bit;
  95. DART_OUT(DART_CNTL, reg);
  96. goto retry;
  97. } else
  98. panic("DART: TLB did not flush after waiting a long "
  99. "time. Buggy U3 ?");
  100. }
  101. spin_unlock_irqrestore(&invalidate_lock, flags);
  102. }
  103. static inline void dart_tlb_invalidate_one(unsigned long bus_rpn)
  104. {
  105. unsigned int reg;
  106. unsigned int l, limit;
  107. unsigned long flags;
  108. spin_lock_irqsave(&invalidate_lock, flags);
  109. reg = DART_CNTL_U4_ENABLE | DART_CNTL_U4_IONE |
  110. (bus_rpn & DART_CNTL_U4_IONE_MASK);
  111. DART_OUT(DART_CNTL, reg);
  112. limit = 0;
  113. wait_more:
  114. l = 0;
  115. while ((DART_IN(DART_CNTL) & DART_CNTL_U4_IONE) && l < (1L << limit)) {
  116. rmb();
  117. l++;
  118. }
  119. if (l == (1L << limit)) {
  120. if (limit < 4) {
  121. limit++;
  122. goto wait_more;
  123. } else
  124. panic("DART: TLB did not flush after waiting a long "
  125. "time. Buggy U4 ?");
  126. }
  127. spin_unlock_irqrestore(&invalidate_lock, flags);
  128. }
  129. static void dart_flush(struct iommu_table *tbl)
  130. {
  131. mb();
  132. if (dart_dirty) {
  133. dart_tlb_invalidate_all();
  134. dart_dirty = 0;
  135. }
  136. }
  137. static int dart_build(struct iommu_table *tbl, long index,
  138. long npages, unsigned long uaddr,
  139. enum dma_data_direction direction,
  140. struct dma_attrs *attrs)
  141. {
  142. unsigned int *dp;
  143. unsigned int rpn;
  144. long l;
  145. DBG("dart: build at: %lx, %lx, addr: %x\n", index, npages, uaddr);
  146. dp = ((unsigned int*)tbl->it_base) + index;
  147. /* On U3, all memory is contiguous, so we can move this
  148. * out of the loop.
  149. */
  150. l = npages;
  151. while (l--) {
  152. rpn = __pa(uaddr) >> DART_PAGE_SHIFT;
  153. *(dp++) = DARTMAP_VALID | (rpn & DARTMAP_RPNMASK);
  154. uaddr += DART_PAGE_SIZE;
  155. }
  156. /* make sure all updates have reached memory */
  157. mb();
  158. in_be32((unsigned __iomem *)dp);
  159. mb();
  160. if (dart_is_u4) {
  161. rpn = index;
  162. while (npages--)
  163. dart_tlb_invalidate_one(rpn++);
  164. } else {
  165. dart_dirty = 1;
  166. }
  167. return 0;
  168. }
  169. static void dart_free(struct iommu_table *tbl, long index, long npages)
  170. {
  171. unsigned int *dp;
  172. /* We don't worry about flushing the TLB cache. The only drawback of
  173. * not doing it is that we won't catch buggy device drivers doing
  174. * bad DMAs, but then no 32-bit architecture ever does either.
  175. */
  176. DBG("dart: free at: %lx, %lx\n", index, npages);
  177. dp = ((unsigned int *)tbl->it_base) + index;
  178. while (npages--)
  179. *(dp++) = dart_emptyval;
  180. }
  181. static int __init dart_init(struct device_node *dart_node)
  182. {
  183. unsigned int i;
  184. unsigned long tmp, base, size;
  185. struct resource r;
  186. if (dart_tablebase == 0 || dart_tablesize == 0) {
  187. printk(KERN_INFO "DART: table not allocated, using "
  188. "direct DMA\n");
  189. return -ENODEV;
  190. }
  191. if (of_address_to_resource(dart_node, 0, &r))
  192. panic("DART: can't get register base ! ");
  193. /* Make sure nothing from the DART range remains in the CPU cache
  194. * from a previous mapping that existed before the kernel took
  195. * over
  196. */
  197. flush_dcache_phys_range(dart_tablebase,
  198. dart_tablebase + dart_tablesize);
  199. /* Allocate a spare page to map all invalid DART pages. We need to do
  200. * that to work around what looks like a problem with the HT bridge
  201. * prefetching into invalid pages and corrupting data
  202. */
  203. tmp = memblock_alloc(DART_PAGE_SIZE, DART_PAGE_SIZE);
  204. dart_emptyval = DARTMAP_VALID | ((tmp >> DART_PAGE_SHIFT) &
  205. DARTMAP_RPNMASK);
  206. /* Map in DART registers */
  207. dart = ioremap(r.start, resource_size(&r));
  208. if (dart == NULL)
  209. panic("DART: Cannot map registers!");
  210. /* Map in DART table */
  211. dart_vbase = ioremap(__pa(dart_tablebase), dart_tablesize);
  212. /* Fill initial table */
  213. for (i = 0; i < dart_tablesize/4; i++)
  214. dart_vbase[i] = dart_emptyval;
  215. /* Initialize DART with table base and enable it. */
  216. base = dart_tablebase >> DART_PAGE_SHIFT;
  217. size = dart_tablesize >> DART_PAGE_SHIFT;
  218. if (dart_is_u4) {
  219. size &= DART_SIZE_U4_SIZE_MASK;
  220. DART_OUT(DART_BASE_U4, base);
  221. DART_OUT(DART_SIZE_U4, size);
  222. DART_OUT(DART_CNTL, DART_CNTL_U4_ENABLE);
  223. } else {
  224. size &= DART_CNTL_U3_SIZE_MASK;
  225. DART_OUT(DART_CNTL,
  226. DART_CNTL_U3_ENABLE |
  227. (base << DART_CNTL_U3_BASE_SHIFT) |
  228. (size << DART_CNTL_U3_SIZE_SHIFT));
  229. }
  230. /* Invalidate DART to get rid of possible stale TLBs */
  231. dart_tlb_invalidate_all();
  232. printk(KERN_INFO "DART IOMMU initialized for %s type chipset\n",
  233. dart_is_u4 ? "U4" : "U3");
  234. return 0;
  235. }
  236. static struct iommu_table_ops iommu_dart_ops = {
  237. .set = dart_build,
  238. .clear = dart_free,
  239. .flush = dart_flush,
  240. };
  241. static void iommu_table_dart_setup(void)
  242. {
  243. iommu_table_dart.it_busno = 0;
  244. iommu_table_dart.it_offset = 0;
  245. /* it_size is in number of entries */
  246. iommu_table_dart.it_size = dart_tablesize / sizeof(u32);
  247. iommu_table_dart.it_page_shift = IOMMU_PAGE_SHIFT_4K;
  248. /* Initialize the common IOMMU code */
  249. iommu_table_dart.it_base = (unsigned long)dart_vbase;
  250. iommu_table_dart.it_index = 0;
  251. iommu_table_dart.it_blocksize = 1;
  252. iommu_table_dart.it_ops = &iommu_dart_ops;
  253. iommu_init_table(&iommu_table_dart, -1);
  254. /* Reserve the last page of the DART to avoid possible prefetch
  255. * past the DART mapped area
  256. */
  257. set_bit(iommu_table_dart.it_size - 1, iommu_table_dart.it_map);
  258. }
  259. static void pci_dma_dev_setup_dart(struct pci_dev *dev)
  260. {
  261. if (dart_is_u4)
  262. set_dma_offset(&dev->dev, DART_U4_BYPASS_BASE);
  263. set_iommu_table_base(&dev->dev, &iommu_table_dart);
  264. }
  265. static void pci_dma_bus_setup_dart(struct pci_bus *bus)
  266. {
  267. if (!iommu_table_dart_inited) {
  268. iommu_table_dart_inited = 1;
  269. iommu_table_dart_setup();
  270. }
  271. }
  272. static bool dart_device_on_pcie(struct device *dev)
  273. {
  274. struct device_node *np = of_node_get(dev->of_node);
  275. while(np) {
  276. if (of_device_is_compatible(np, "U4-pcie") ||
  277. of_device_is_compatible(np, "u4-pcie")) {
  278. of_node_put(np);
  279. return true;
  280. }
  281. np = of_get_next_parent(np);
  282. }
  283. return false;
  284. }
  285. static int dart_dma_set_mask(struct device *dev, u64 dma_mask)
  286. {
  287. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  288. return -EIO;
  289. /* U4 supports a DART bypass, we use it for 64-bit capable
  290. * devices to improve performances. However, that only works
  291. * for devices connected to U4 own PCIe interface, not bridged
  292. * through hypertransport. We need the device to support at
  293. * least 40 bits of addresses.
  294. */
  295. if (dart_device_on_pcie(dev) && dma_mask >= DMA_BIT_MASK(40)) {
  296. dev_info(dev, "Using 64-bit DMA iommu bypass\n");
  297. set_dma_ops(dev, &dma_direct_ops);
  298. } else {
  299. dev_info(dev, "Using 32-bit DMA via iommu\n");
  300. set_dma_ops(dev, &dma_iommu_ops);
  301. }
  302. *dev->dma_mask = dma_mask;
  303. return 0;
  304. }
  305. void __init iommu_init_early_dart(struct pci_controller_ops *controller_ops)
  306. {
  307. struct device_node *dn;
  308. /* Find the DART in the device-tree */
  309. dn = of_find_compatible_node(NULL, "dart", "u3-dart");
  310. if (dn == NULL) {
  311. dn = of_find_compatible_node(NULL, "dart", "u4-dart");
  312. if (dn == NULL)
  313. return; /* use default direct_dma_ops */
  314. dart_is_u4 = 1;
  315. }
  316. /* Initialize the DART HW */
  317. if (dart_init(dn) != 0)
  318. goto bail;
  319. /* Setup bypass if supported */
  320. if (dart_is_u4)
  321. ppc_md.dma_set_mask = dart_dma_set_mask;
  322. controller_ops->dma_dev_setup = pci_dma_dev_setup_dart;
  323. controller_ops->dma_bus_setup = pci_dma_bus_setup_dart;
  324. /* Setup pci_dma ops */
  325. set_pci_dma_ops(&dma_iommu_ops);
  326. return;
  327. bail:
  328. /* If init failed, use direct iommu and null setup functions */
  329. controller_ops->dma_dev_setup = NULL;
  330. controller_ops->dma_bus_setup = NULL;
  331. /* Setup pci_dma ops */
  332. set_pci_dma_ops(&dma_direct_ops);
  333. }
  334. #ifdef CONFIG_PM
  335. static void iommu_dart_save(void)
  336. {
  337. memcpy(dart_copy, dart_vbase, 2*1024*1024);
  338. }
  339. static void iommu_dart_restore(void)
  340. {
  341. memcpy(dart_vbase, dart_copy, 2*1024*1024);
  342. dart_tlb_invalidate_all();
  343. }
  344. static int __init iommu_init_late_dart(void)
  345. {
  346. unsigned long tbasepfn;
  347. struct page *p;
  348. /* if no dart table exists then we won't need to save it
  349. * and the area has also not been reserved */
  350. if (!dart_tablebase)
  351. return 0;
  352. tbasepfn = __pa(dart_tablebase) >> PAGE_SHIFT;
  353. register_nosave_region_late(tbasepfn,
  354. tbasepfn + ((1<<24) >> PAGE_SHIFT));
  355. /* For suspend we need to copy the dart contents because
  356. * it is not part of the regular mapping (see above) and
  357. * thus not saved automatically. The memory for this copy
  358. * must be allocated early because we need 2 MB. */
  359. p = alloc_pages(GFP_KERNEL, 21 - PAGE_SHIFT);
  360. BUG_ON(!p);
  361. dart_copy = page_address(p);
  362. ppc_md.iommu_save = iommu_dart_save;
  363. ppc_md.iommu_restore = iommu_dart_restore;
  364. return 0;
  365. }
  366. late_initcall(iommu_init_late_dart);
  367. #endif
  368. void __init alloc_dart_table(void)
  369. {
  370. /* Only reserve DART space if machine has more than 1GB of RAM
  371. * or if requested with iommu=on on cmdline.
  372. *
  373. * 1GB of RAM is picked as limit because some default devices
  374. * (i.e. Airport Extreme) have 30 bit address range limits.
  375. */
  376. if (iommu_is_off)
  377. return;
  378. if (!iommu_force_on && memblock_end_of_DRAM() <= 0x40000000ull)
  379. return;
  380. /* 512 pages (2MB) is max DART tablesize. */
  381. dart_tablesize = 1UL << 21;
  382. /* 16MB (1 << 24) alignment. We allocate a full 16Mb chuck since we
  383. * will blow up an entire large page anyway in the kernel mapping
  384. */
  385. dart_tablebase = (unsigned long)
  386. __va(memblock_alloc_base(1UL<<24, 1UL<<24, 0x80000000L));
  387. /*
  388. * The DART space is later unmapped from the kernel linear mapping and
  389. * accessing dart_tablebase during kmemleak scanning will fault.
  390. */
  391. kmemleak_no_scan((void *)dart_tablebase);
  392. printk(KERN_INFO "DART table allocated at: %lx\n", dart_tablebase);
  393. }