dart_iommu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 void iommu_table_dart_setup(void)
  237. {
  238. iommu_table_dart.it_busno = 0;
  239. iommu_table_dart.it_offset = 0;
  240. /* it_size is in number of entries */
  241. iommu_table_dart.it_size = dart_tablesize / sizeof(u32);
  242. /* Initialize the common IOMMU code */
  243. iommu_table_dart.it_base = (unsigned long)dart_vbase;
  244. iommu_table_dart.it_index = 0;
  245. iommu_table_dart.it_blocksize = 1;
  246. iommu_init_table(&iommu_table_dart, -1);
  247. /* Reserve the last page of the DART to avoid possible prefetch
  248. * past the DART mapped area
  249. */
  250. set_bit(iommu_table_dart.it_size - 1, iommu_table_dart.it_map);
  251. }
  252. static void dma_dev_setup_dart(struct device *dev)
  253. {
  254. /* We only have one iommu table on the mac for now, which makes
  255. * things simple. Setup all PCI devices to point to this table
  256. */
  257. if (get_dma_ops(dev) == &dma_direct_ops)
  258. set_dma_offset(dev, DART_U4_BYPASS_BASE);
  259. else
  260. set_iommu_table_base(dev, &iommu_table_dart);
  261. }
  262. static void pci_dma_dev_setup_dart(struct pci_dev *dev)
  263. {
  264. dma_dev_setup_dart(&dev->dev);
  265. }
  266. static void pci_dma_bus_setup_dart(struct pci_bus *bus)
  267. {
  268. if (!iommu_table_dart_inited) {
  269. iommu_table_dart_inited = 1;
  270. iommu_table_dart_setup();
  271. }
  272. }
  273. static bool dart_device_on_pcie(struct device *dev)
  274. {
  275. struct device_node *np = of_node_get(dev->of_node);
  276. while(np) {
  277. if (of_device_is_compatible(np, "U4-pcie") ||
  278. of_device_is_compatible(np, "u4-pcie")) {
  279. of_node_put(np);
  280. return true;
  281. }
  282. np = of_get_next_parent(np);
  283. }
  284. return false;
  285. }
  286. static int dart_dma_set_mask(struct device *dev, u64 dma_mask)
  287. {
  288. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  289. return -EIO;
  290. /* U4 supports a DART bypass, we use it for 64-bit capable
  291. * devices to improve performances. However, that only works
  292. * for devices connected to U4 own PCIe interface, not bridged
  293. * through hypertransport. We need the device to support at
  294. * least 40 bits of addresses.
  295. */
  296. if (dart_device_on_pcie(dev) && dma_mask >= DMA_BIT_MASK(40)) {
  297. dev_info(dev, "Using 64-bit DMA iommu bypass\n");
  298. set_dma_ops(dev, &dma_direct_ops);
  299. } else {
  300. dev_info(dev, "Using 32-bit DMA via iommu\n");
  301. set_dma_ops(dev, &dma_iommu_ops);
  302. }
  303. dma_dev_setup_dart(dev);
  304. *dev->dma_mask = dma_mask;
  305. return 0;
  306. }
  307. void __init iommu_init_early_dart(void)
  308. {
  309. struct device_node *dn;
  310. /* Find the DART in the device-tree */
  311. dn = of_find_compatible_node(NULL, "dart", "u3-dart");
  312. if (dn == NULL) {
  313. dn = of_find_compatible_node(NULL, "dart", "u4-dart");
  314. if (dn == NULL)
  315. return; /* use default direct_dma_ops */
  316. dart_is_u4 = 1;
  317. }
  318. /* Initialize the DART HW */
  319. if (dart_init(dn) != 0)
  320. goto bail;
  321. /* Setup low level TCE operations for the core IOMMU code */
  322. ppc_md.tce_build = dart_build;
  323. ppc_md.tce_free = dart_free;
  324. ppc_md.tce_flush = dart_flush;
  325. /* Setup bypass if supported */
  326. if (dart_is_u4)
  327. ppc_md.dma_set_mask = dart_dma_set_mask;
  328. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_dart;
  329. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_dart;
  330. /* Setup pci_dma ops */
  331. set_pci_dma_ops(&dma_iommu_ops);
  332. return;
  333. bail:
  334. /* If init failed, use direct iommu and null setup functions */
  335. ppc_md.pci_dma_dev_setup = NULL;
  336. ppc_md.pci_dma_bus_setup = NULL;
  337. /* Setup pci_dma ops */
  338. set_pci_dma_ops(&dma_direct_ops);
  339. }
  340. #ifdef CONFIG_PM
  341. static void iommu_dart_save(void)
  342. {
  343. memcpy(dart_copy, dart_vbase, 2*1024*1024);
  344. }
  345. static void iommu_dart_restore(void)
  346. {
  347. memcpy(dart_vbase, dart_copy, 2*1024*1024);
  348. dart_tlb_invalidate_all();
  349. }
  350. static int __init iommu_init_late_dart(void)
  351. {
  352. unsigned long tbasepfn;
  353. struct page *p;
  354. /* if no dart table exists then we won't need to save it
  355. * and the area has also not been reserved */
  356. if (!dart_tablebase)
  357. return 0;
  358. tbasepfn = __pa(dart_tablebase) >> PAGE_SHIFT;
  359. register_nosave_region_late(tbasepfn,
  360. tbasepfn + ((1<<24) >> PAGE_SHIFT));
  361. /* For suspend we need to copy the dart contents because
  362. * it is not part of the regular mapping (see above) and
  363. * thus not saved automatically. The memory for this copy
  364. * must be allocated early because we need 2 MB. */
  365. p = alloc_pages(GFP_KERNEL, 21 - PAGE_SHIFT);
  366. BUG_ON(!p);
  367. dart_copy = page_address(p);
  368. ppc_md.iommu_save = iommu_dart_save;
  369. ppc_md.iommu_restore = iommu_dart_restore;
  370. return 0;
  371. }
  372. late_initcall(iommu_init_late_dart);
  373. #endif
  374. void __init alloc_dart_table(void)
  375. {
  376. /* Only reserve DART space if machine has more than 1GB of RAM
  377. * or if requested with iommu=on on cmdline.
  378. *
  379. * 1GB of RAM is picked as limit because some default devices
  380. * (i.e. Airport Extreme) have 30 bit address range limits.
  381. */
  382. if (iommu_is_off)
  383. return;
  384. if (!iommu_force_on && memblock_end_of_DRAM() <= 0x40000000ull)
  385. return;
  386. /* 512 pages (2MB) is max DART tablesize. */
  387. dart_tablesize = 1UL << 21;
  388. /* 16MB (1 << 24) alignment. We allocate a full 16Mb chuck since we
  389. * will blow up an entire large page anyway in the kernel mapping
  390. */
  391. dart_tablebase = (unsigned long)
  392. __va(memblock_alloc_base(1UL<<24, 1UL<<24, 0x80000000L));
  393. printk(KERN_INFO "DART table allocated at: %lx\n", dart_tablebase);
  394. }