dart_iommu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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/abs_addr.h>
  46. #include <asm/cacheflush.h>
  47. #include <asm/ppc-pci.h>
  48. #include "dart.h"
  49. /* Physical base address and size of the DART table */
  50. unsigned long dart_tablebase; /* exported to htab_initialize */
  51. static unsigned long dart_tablesize;
  52. /* Virtual base address of the DART table */
  53. static u32 *dart_vbase;
  54. #ifdef CONFIG_PM
  55. static u32 *dart_copy;
  56. #endif
  57. /* Mapped base address for the dart */
  58. static unsigned int __iomem *dart;
  59. /* Dummy val that entries are set to when unused */
  60. static unsigned int dart_emptyval;
  61. static struct iommu_table iommu_table_dart;
  62. static int iommu_table_dart_inited;
  63. static int dart_dirty;
  64. static int dart_is_u4;
  65. #define DART_U4_BYPASS_BASE 0x8000000000ull
  66. #define DBG(...)
  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. DBG("dart: flush\n");
  73. /* To invalidate the DART, set the DARTCNTL_FLUSHTLB bit in the
  74. * control register and wait for it to clear.
  75. *
  76. * Gotcha: Sometimes, the DART won't detect that the bit gets
  77. * set. If so, clear it and set it again.
  78. */
  79. limit = 0;
  80. inv_bit = dart_is_u4 ? DART_CNTL_U4_FLUSHTLB : DART_CNTL_U3_FLUSHTLB;
  81. retry:
  82. l = 0;
  83. reg = DART_IN(DART_CNTL);
  84. reg |= inv_bit;
  85. DART_OUT(DART_CNTL, reg);
  86. while ((DART_IN(DART_CNTL) & inv_bit) && l < (1L << limit))
  87. l++;
  88. if (l == (1L << limit)) {
  89. if (limit < 4) {
  90. limit++;
  91. reg = DART_IN(DART_CNTL);
  92. reg &= ~inv_bit;
  93. DART_OUT(DART_CNTL, reg);
  94. goto retry;
  95. } else
  96. panic("DART: TLB did not flush after waiting a long "
  97. "time. Buggy U3 ?");
  98. }
  99. }
  100. static inline void dart_tlb_invalidate_one(unsigned long bus_rpn)
  101. {
  102. unsigned int reg;
  103. unsigned int l, limit;
  104. reg = DART_CNTL_U4_ENABLE | DART_CNTL_U4_IONE |
  105. (bus_rpn & DART_CNTL_U4_IONE_MASK);
  106. DART_OUT(DART_CNTL, reg);
  107. limit = 0;
  108. wait_more:
  109. l = 0;
  110. while ((DART_IN(DART_CNTL) & DART_CNTL_U4_IONE) && l < (1L << limit)) {
  111. rmb();
  112. l++;
  113. }
  114. if (l == (1L << limit)) {
  115. if (limit < 4) {
  116. limit++;
  117. goto wait_more;
  118. } else
  119. panic("DART: TLB did not flush after waiting a long "
  120. "time. Buggy U4 ?");
  121. }
  122. }
  123. static void dart_flush(struct iommu_table *tbl)
  124. {
  125. mb();
  126. if (dart_dirty) {
  127. dart_tlb_invalidate_all();
  128. dart_dirty = 0;
  129. }
  130. }
  131. static int dart_build(struct iommu_table *tbl, long index,
  132. long npages, unsigned long uaddr,
  133. enum dma_data_direction direction,
  134. struct dma_attrs *attrs)
  135. {
  136. unsigned int *dp;
  137. unsigned int rpn;
  138. long l;
  139. DBG("dart: build at: %lx, %lx, addr: %x\n", index, npages, uaddr);
  140. dp = ((unsigned int*)tbl->it_base) + index;
  141. /* On U3, all memory is contiguous, so we can move this
  142. * out of the loop.
  143. */
  144. l = npages;
  145. while (l--) {
  146. rpn = virt_to_abs(uaddr) >> DART_PAGE_SHIFT;
  147. *(dp++) = DARTMAP_VALID | (rpn & DARTMAP_RPNMASK);
  148. uaddr += DART_PAGE_SIZE;
  149. }
  150. /* make sure all updates have reached memory */
  151. mb();
  152. in_be32((unsigned __iomem *)dp);
  153. mb();
  154. if (dart_is_u4) {
  155. rpn = index;
  156. while (npages--)
  157. dart_tlb_invalidate_one(rpn++);
  158. } else {
  159. dart_dirty = 1;
  160. }
  161. return 0;
  162. }
  163. static void dart_free(struct iommu_table *tbl, long index, long npages)
  164. {
  165. unsigned int *dp;
  166. /* We don't worry about flushing the TLB cache. The only drawback of
  167. * not doing it is that we won't catch buggy device drivers doing
  168. * bad DMAs, but then no 32-bit architecture ever does either.
  169. */
  170. DBG("dart: free at: %lx, %lx\n", index, npages);
  171. dp = ((unsigned int *)tbl->it_base) + index;
  172. while (npages--)
  173. *(dp++) = dart_emptyval;
  174. }
  175. static int __init dart_init(struct device_node *dart_node)
  176. {
  177. unsigned int i;
  178. unsigned long tmp, base, size;
  179. struct resource r;
  180. if (dart_tablebase == 0 || dart_tablesize == 0) {
  181. printk(KERN_INFO "DART: table not allocated, using "
  182. "direct DMA\n");
  183. return -ENODEV;
  184. }
  185. if (of_address_to_resource(dart_node, 0, &r))
  186. panic("DART: can't get register base ! ");
  187. /* Make sure nothing from the DART range remains in the CPU cache
  188. * from a previous mapping that existed before the kernel took
  189. * over
  190. */
  191. flush_dcache_phys_range(dart_tablebase,
  192. dart_tablebase + dart_tablesize);
  193. /* Allocate a spare page to map all invalid DART pages. We need to do
  194. * that to work around what looks like a problem with the HT bridge
  195. * prefetching into invalid pages and corrupting data
  196. */
  197. tmp = memblock_alloc(DART_PAGE_SIZE, DART_PAGE_SIZE);
  198. dart_emptyval = DARTMAP_VALID | ((tmp >> DART_PAGE_SHIFT) &
  199. DARTMAP_RPNMASK);
  200. /* Map in DART registers */
  201. dart = ioremap(r.start, resource_size(&r));
  202. if (dart == NULL)
  203. panic("DART: Cannot map registers!");
  204. /* Map in DART table */
  205. dart_vbase = ioremap(virt_to_abs(dart_tablebase), dart_tablesize);
  206. /* Fill initial table */
  207. for (i = 0; i < dart_tablesize/4; i++)
  208. dart_vbase[i] = dart_emptyval;
  209. /* Initialize DART with table base and enable it. */
  210. base = dart_tablebase >> DART_PAGE_SHIFT;
  211. size = dart_tablesize >> DART_PAGE_SHIFT;
  212. if (dart_is_u4) {
  213. size &= DART_SIZE_U4_SIZE_MASK;
  214. DART_OUT(DART_BASE_U4, base);
  215. DART_OUT(DART_SIZE_U4, size);
  216. DART_OUT(DART_CNTL, DART_CNTL_U4_ENABLE);
  217. } else {
  218. size &= DART_CNTL_U3_SIZE_MASK;
  219. DART_OUT(DART_CNTL,
  220. DART_CNTL_U3_ENABLE |
  221. (base << DART_CNTL_U3_BASE_SHIFT) |
  222. (size << DART_CNTL_U3_SIZE_SHIFT));
  223. }
  224. /* Invalidate DART to get rid of possible stale TLBs */
  225. dart_tlb_invalidate_all();
  226. printk(KERN_INFO "DART IOMMU initialized for %s type chipset\n",
  227. dart_is_u4 ? "U4" : "U3");
  228. return 0;
  229. }
  230. static void iommu_table_dart_setup(void)
  231. {
  232. iommu_table_dart.it_busno = 0;
  233. iommu_table_dart.it_offset = 0;
  234. /* it_size is in number of entries */
  235. iommu_table_dart.it_size = dart_tablesize / sizeof(u32);
  236. /* Initialize the common IOMMU code */
  237. iommu_table_dart.it_base = (unsigned long)dart_vbase;
  238. iommu_table_dart.it_index = 0;
  239. iommu_table_dart.it_blocksize = 1;
  240. iommu_init_table(&iommu_table_dart, -1);
  241. /* Reserve the last page of the DART to avoid possible prefetch
  242. * past the DART mapped area
  243. */
  244. set_bit(iommu_table_dart.it_size - 1, iommu_table_dart.it_map);
  245. }
  246. static void dma_dev_setup_dart(struct device *dev)
  247. {
  248. /* We only have one iommu table on the mac for now, which makes
  249. * things simple. Setup all PCI devices to point to this table
  250. */
  251. if (get_dma_ops(dev) == &dma_direct_ops)
  252. set_dma_offset(dev, DART_U4_BYPASS_BASE);
  253. else
  254. set_iommu_table_base(dev, &iommu_table_dart);
  255. }
  256. static void pci_dma_dev_setup_dart(struct pci_dev *dev)
  257. {
  258. dma_dev_setup_dart(&dev->dev);
  259. }
  260. static void pci_dma_bus_setup_dart(struct pci_bus *bus)
  261. {
  262. if (!iommu_table_dart_inited) {
  263. iommu_table_dart_inited = 1;
  264. iommu_table_dart_setup();
  265. }
  266. }
  267. static bool dart_device_on_pcie(struct device *dev)
  268. {
  269. struct device_node *np = of_node_get(dev->of_node);
  270. while(np) {
  271. if (of_device_is_compatible(np, "U4-pcie") ||
  272. of_device_is_compatible(np, "u4-pcie")) {
  273. of_node_put(np);
  274. return true;
  275. }
  276. np = of_get_next_parent(np);
  277. }
  278. return false;
  279. }
  280. static int dart_dma_set_mask(struct device *dev, u64 dma_mask)
  281. {
  282. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  283. return -EIO;
  284. /* U4 supports a DART bypass, we use it for 64-bit capable
  285. * devices to improve performances. However, that only works
  286. * for devices connected to U4 own PCIe interface, not bridged
  287. * through hypertransport. We need the device to support at
  288. * least 40 bits of addresses.
  289. */
  290. if (dart_device_on_pcie(dev) && dma_mask >= DMA_BIT_MASK(40)) {
  291. dev_info(dev, "Using 64-bit DMA iommu bypass\n");
  292. set_dma_ops(dev, &dma_direct_ops);
  293. } else {
  294. dev_info(dev, "Using 32-bit DMA via iommu\n");
  295. set_dma_ops(dev, &dma_iommu_ops);
  296. }
  297. dma_dev_setup_dart(dev);
  298. *dev->dma_mask = dma_mask;
  299. return 0;
  300. }
  301. void __init iommu_init_early_dart(void)
  302. {
  303. struct device_node *dn;
  304. /* Find the DART in the device-tree */
  305. dn = of_find_compatible_node(NULL, "dart", "u3-dart");
  306. if (dn == NULL) {
  307. dn = of_find_compatible_node(NULL, "dart", "u4-dart");
  308. if (dn == NULL)
  309. return; /* use default direct_dma_ops */
  310. dart_is_u4 = 1;
  311. }
  312. /* Initialize the DART HW */
  313. if (dart_init(dn) != 0)
  314. goto bail;
  315. /* Setup low level TCE operations for the core IOMMU code */
  316. ppc_md.tce_build = dart_build;
  317. ppc_md.tce_free = dart_free;
  318. ppc_md.tce_flush = dart_flush;
  319. /* Setup bypass if supported */
  320. if (dart_is_u4)
  321. ppc_md.dma_set_mask = dart_dma_set_mask;
  322. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_dart;
  323. ppc_md.pci_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. ppc_md.pci_dma_dev_setup = NULL;
  330. ppc_md.pci_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. abs_to_virt(memblock_alloc_base(1UL<<24, 1UL<<24, 0x80000000L));
  387. printk(KERN_INFO "DART table allocated at: %lx\n", dart_tablebase);
  388. }