pci-dma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. #include <linux/dma-mapping.h>
  2. #include <linux/dmar.h>
  3. #include <linux/bootmem.h>
  4. #include <linux/pci.h>
  5. #include <asm/proto.h>
  6. #include <asm/dma.h>
  7. #include <asm/gart.h>
  8. #include <asm/calgary.h>
  9. int forbid_dac __read_mostly;
  10. EXPORT_SYMBOL(forbid_dac);
  11. const struct dma_mapping_ops *dma_ops;
  12. EXPORT_SYMBOL(dma_ops);
  13. int iommu_sac_force __read_mostly = 0;
  14. #ifdef CONFIG_IOMMU_DEBUG
  15. int panic_on_overflow __read_mostly = 1;
  16. int force_iommu __read_mostly = 1;
  17. #else
  18. int panic_on_overflow __read_mostly = 0;
  19. int force_iommu __read_mostly = 0;
  20. #endif
  21. int iommu_merge __read_mostly = 0;
  22. int no_iommu __read_mostly;
  23. /* Set this to 1 if there is a HW IOMMU in the system */
  24. int iommu_detected __read_mostly = 0;
  25. /* This tells the BIO block layer to assume merging. Default to off
  26. because we cannot guarantee merging later. */
  27. int iommu_bio_merge __read_mostly = 0;
  28. EXPORT_SYMBOL(iommu_bio_merge);
  29. dma_addr_t bad_dma_address __read_mostly = 0;
  30. EXPORT_SYMBOL(bad_dma_address);
  31. /* Dummy device used for NULL arguments (normally ISA). Better would
  32. be probably a smaller DMA mask, but this is bug-to-bug compatible
  33. to older i386. */
  34. struct device fallback_dev = {
  35. .bus_id = "fallback device",
  36. .coherent_dma_mask = DMA_32BIT_MASK,
  37. .dma_mask = &fallback_dev.coherent_dma_mask,
  38. };
  39. int dma_set_mask(struct device *dev, u64 mask)
  40. {
  41. if (!dev->dma_mask || !dma_supported(dev, mask))
  42. return -EIO;
  43. *dev->dma_mask = mask;
  44. return 0;
  45. }
  46. EXPORT_SYMBOL(dma_set_mask);
  47. #ifdef CONFIG_X86_64
  48. static __initdata void *dma32_bootmem_ptr;
  49. static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
  50. static int __init parse_dma32_size_opt(char *p)
  51. {
  52. if (!p)
  53. return -EINVAL;
  54. dma32_bootmem_size = memparse(p, &p);
  55. return 0;
  56. }
  57. early_param("dma32_size", parse_dma32_size_opt);
  58. void __init dma32_reserve_bootmem(void)
  59. {
  60. unsigned long size, align;
  61. if (end_pfn <= MAX_DMA32_PFN)
  62. return;
  63. align = 64ULL<<20;
  64. size = round_up(dma32_bootmem_size, align);
  65. dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
  66. __pa(MAX_DMA_ADDRESS));
  67. if (dma32_bootmem_ptr)
  68. dma32_bootmem_size = size;
  69. else
  70. dma32_bootmem_size = 0;
  71. }
  72. static void __init dma32_free_bootmem(void)
  73. {
  74. int node;
  75. if (end_pfn <= MAX_DMA32_PFN)
  76. return;
  77. if (!dma32_bootmem_ptr)
  78. return;
  79. for_each_online_node(node)
  80. free_bootmem_node(NODE_DATA(node), __pa(dma32_bootmem_ptr),
  81. dma32_bootmem_size);
  82. dma32_bootmem_ptr = NULL;
  83. dma32_bootmem_size = 0;
  84. }
  85. void __init pci_iommu_alloc(void)
  86. {
  87. /* free the range so iommu could get some range less than 4G */
  88. dma32_free_bootmem();
  89. /*
  90. * The order of these functions is important for
  91. * fall-back/fail-over reasons
  92. */
  93. #ifdef CONFIG_GART_IOMMU
  94. gart_iommu_hole_init();
  95. #endif
  96. #ifdef CONFIG_CALGARY_IOMMU
  97. detect_calgary();
  98. #endif
  99. detect_intel_iommu();
  100. #ifdef CONFIG_SWIOTLB
  101. pci_swiotlb_init();
  102. #endif
  103. }
  104. #endif
  105. /*
  106. * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
  107. * documentation.
  108. */
  109. static __init int iommu_setup(char *p)
  110. {
  111. iommu_merge = 1;
  112. if (!p)
  113. return -EINVAL;
  114. while (*p) {
  115. if (!strncmp(p, "off", 3))
  116. no_iommu = 1;
  117. /* gart_parse_options has more force support */
  118. if (!strncmp(p, "force", 5))
  119. force_iommu = 1;
  120. if (!strncmp(p, "noforce", 7)) {
  121. iommu_merge = 0;
  122. force_iommu = 0;
  123. }
  124. if (!strncmp(p, "biomerge", 8)) {
  125. iommu_bio_merge = 4096;
  126. iommu_merge = 1;
  127. force_iommu = 1;
  128. }
  129. if (!strncmp(p, "panic", 5))
  130. panic_on_overflow = 1;
  131. if (!strncmp(p, "nopanic", 7))
  132. panic_on_overflow = 0;
  133. if (!strncmp(p, "merge", 5)) {
  134. iommu_merge = 1;
  135. force_iommu = 1;
  136. }
  137. if (!strncmp(p, "nomerge", 7))
  138. iommu_merge = 0;
  139. if (!strncmp(p, "forcesac", 8))
  140. iommu_sac_force = 1;
  141. if (!strncmp(p, "allowdac", 8))
  142. forbid_dac = 0;
  143. if (!strncmp(p, "nodac", 5))
  144. forbid_dac = -1;
  145. if (!strncmp(p, "usedac", 6)) {
  146. forbid_dac = -1;
  147. return 1;
  148. }
  149. #ifdef CONFIG_SWIOTLB
  150. if (!strncmp(p, "soft", 4))
  151. swiotlb = 1;
  152. #endif
  153. #ifdef CONFIG_GART_IOMMU
  154. gart_parse_options(p);
  155. #endif
  156. #ifdef CONFIG_CALGARY_IOMMU
  157. if (!strncmp(p, "calgary", 7))
  158. use_calgary = 1;
  159. #endif /* CONFIG_CALGARY_IOMMU */
  160. p += strcspn(p, ",");
  161. if (*p == ',')
  162. ++p;
  163. }
  164. return 0;
  165. }
  166. early_param("iommu", iommu_setup);
  167. #ifdef CONFIG_X86_32
  168. int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  169. dma_addr_t device_addr, size_t size, int flags)
  170. {
  171. void __iomem *mem_base = NULL;
  172. int pages = size >> PAGE_SHIFT;
  173. int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
  174. if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
  175. goto out;
  176. if (!size)
  177. goto out;
  178. if (dev->dma_mem)
  179. goto out;
  180. /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
  181. mem_base = ioremap(bus_addr, size);
  182. if (!mem_base)
  183. goto out;
  184. dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
  185. if (!dev->dma_mem)
  186. goto out;
  187. dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  188. if (!dev->dma_mem->bitmap)
  189. goto free1_out;
  190. dev->dma_mem->virt_base = mem_base;
  191. dev->dma_mem->device_base = device_addr;
  192. dev->dma_mem->size = pages;
  193. dev->dma_mem->flags = flags;
  194. if (flags & DMA_MEMORY_MAP)
  195. return DMA_MEMORY_MAP;
  196. return DMA_MEMORY_IO;
  197. free1_out:
  198. kfree(dev->dma_mem);
  199. out:
  200. if (mem_base)
  201. iounmap(mem_base);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL(dma_declare_coherent_memory);
  205. void dma_release_declared_memory(struct device *dev)
  206. {
  207. struct dma_coherent_mem *mem = dev->dma_mem;
  208. if (!mem)
  209. return;
  210. dev->dma_mem = NULL;
  211. iounmap(mem->virt_base);
  212. kfree(mem->bitmap);
  213. kfree(mem);
  214. }
  215. EXPORT_SYMBOL(dma_release_declared_memory);
  216. void *dma_mark_declared_memory_occupied(struct device *dev,
  217. dma_addr_t device_addr, size_t size)
  218. {
  219. struct dma_coherent_mem *mem = dev->dma_mem;
  220. int pos, err;
  221. int pages = (size + (device_addr & ~PAGE_MASK) + PAGE_SIZE - 1);
  222. pages >>= PAGE_SHIFT;
  223. if (!mem)
  224. return ERR_PTR(-EINVAL);
  225. pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
  226. err = bitmap_allocate_region(mem->bitmap, pos, get_order(pages));
  227. if (err != 0)
  228. return ERR_PTR(err);
  229. return mem->virt_base + (pos << PAGE_SHIFT);
  230. }
  231. EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
  232. static int dma_alloc_from_coherent_mem(struct device *dev, ssize_t size,
  233. dma_addr_t *dma_handle, void **ret)
  234. {
  235. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  236. int order = get_order(size);
  237. if (mem) {
  238. int page = bitmap_find_free_region(mem->bitmap, mem->size,
  239. order);
  240. if (page >= 0) {
  241. *dma_handle = mem->device_base + (page << PAGE_SHIFT);
  242. *ret = mem->virt_base + (page << PAGE_SHIFT);
  243. memset(*ret, 0, size);
  244. }
  245. if (mem->flags & DMA_MEMORY_EXCLUSIVE)
  246. *ret = NULL;
  247. }
  248. return (mem != NULL);
  249. }
  250. static int dma_release_coherent(struct device *dev, int order, void *vaddr)
  251. {
  252. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  253. if (mem && vaddr >= mem->virt_base && vaddr <
  254. (mem->virt_base + (mem->size << PAGE_SHIFT))) {
  255. int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  256. bitmap_release_region(mem->bitmap, page, order);
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. #else
  262. #define dma_alloc_from_coherent_mem(dev, size, handle, ret) (0)
  263. #define dma_release_coherent(dev, order, vaddr) (0)
  264. #endif /* CONFIG_X86_32 */
  265. int dma_supported(struct device *dev, u64 mask)
  266. {
  267. #ifdef CONFIG_PCI
  268. if (mask > 0xffffffff && forbid_dac > 0) {
  269. printk(KERN_INFO "PCI: Disallowing DAC for device %s\n",
  270. dev->bus_id);
  271. return 0;
  272. }
  273. #endif
  274. if (dma_ops->dma_supported)
  275. return dma_ops->dma_supported(dev, mask);
  276. /* Copied from i386. Doesn't make much sense, because it will
  277. only work for pci_alloc_coherent.
  278. The caller just has to use GFP_DMA in this case. */
  279. if (mask < DMA_24BIT_MASK)
  280. return 0;
  281. /* Tell the device to use SAC when IOMMU force is on. This
  282. allows the driver to use cheaper accesses in some cases.
  283. Problem with this is that if we overflow the IOMMU area and
  284. return DAC as fallback address the device may not handle it
  285. correctly.
  286. As a special case some controllers have a 39bit address
  287. mode that is as efficient as 32bit (aic79xx). Don't force
  288. SAC for these. Assume all masks <= 40 bits are of this
  289. type. Normally this doesn't make any difference, but gives
  290. more gentle handling of IOMMU overflow. */
  291. if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
  292. printk(KERN_INFO "%s: Force SAC with mask %Lx\n",
  293. dev->bus_id, mask);
  294. return 0;
  295. }
  296. return 1;
  297. }
  298. EXPORT_SYMBOL(dma_supported);
  299. /* Allocate DMA memory on node near device */
  300. noinline struct page *
  301. dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
  302. {
  303. int node;
  304. node = dev_to_node(dev);
  305. return alloc_pages_node(node, gfp, order);
  306. }
  307. /*
  308. * Allocate memory for a coherent mapping.
  309. */
  310. void *
  311. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
  312. gfp_t gfp)
  313. {
  314. void *memory = NULL;
  315. struct page *page;
  316. unsigned long dma_mask = 0;
  317. dma_addr_t bus;
  318. /* ignore region specifiers */
  319. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  320. if (dma_alloc_from_coherent_mem(dev, size, dma_handle, &memory))
  321. return memory;
  322. if (!dev)
  323. dev = &fallback_dev;
  324. dma_mask = dev->coherent_dma_mask;
  325. if (dma_mask == 0)
  326. dma_mask = DMA_32BIT_MASK;
  327. /* Device not DMA able */
  328. if (dev->dma_mask == NULL)
  329. return NULL;
  330. /* Don't invoke OOM killer */
  331. gfp |= __GFP_NORETRY;
  332. #ifdef CONFIG_X86_64
  333. /* Why <=? Even when the mask is smaller than 4GB it is often
  334. larger than 16MB and in this case we have a chance of
  335. finding fitting memory in the next higher zone first. If
  336. not retry with true GFP_DMA. -AK */
  337. if (dma_mask <= DMA_32BIT_MASK)
  338. gfp |= GFP_DMA32;
  339. #endif
  340. again:
  341. page = dma_alloc_pages(dev, gfp, get_order(size));
  342. if (page == NULL)
  343. return NULL;
  344. {
  345. int high, mmu;
  346. bus = page_to_phys(page);
  347. memory = page_address(page);
  348. high = (bus + size) >= dma_mask;
  349. mmu = high;
  350. if (force_iommu && !(gfp & GFP_DMA))
  351. mmu = 1;
  352. else if (high) {
  353. free_pages((unsigned long)memory,
  354. get_order(size));
  355. /* Don't use the 16MB ZONE_DMA unless absolutely
  356. needed. It's better to use remapping first. */
  357. if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
  358. gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
  359. goto again;
  360. }
  361. /* Let low level make its own zone decisions */
  362. gfp &= ~(GFP_DMA32|GFP_DMA);
  363. if (dma_ops->alloc_coherent)
  364. return dma_ops->alloc_coherent(dev, size,
  365. dma_handle, gfp);
  366. return NULL;
  367. }
  368. memset(memory, 0, size);
  369. if (!mmu) {
  370. *dma_handle = bus;
  371. return memory;
  372. }
  373. }
  374. if (dma_ops->alloc_coherent) {
  375. free_pages((unsigned long)memory, get_order(size));
  376. gfp &= ~(GFP_DMA|GFP_DMA32);
  377. return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
  378. }
  379. if (dma_ops->map_simple) {
  380. *dma_handle = dma_ops->map_simple(dev, virt_to_phys(memory),
  381. size,
  382. PCI_DMA_BIDIRECTIONAL);
  383. if (*dma_handle != bad_dma_address)
  384. return memory;
  385. }
  386. if (panic_on_overflow)
  387. panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",
  388. (unsigned long)size);
  389. free_pages((unsigned long)memory, get_order(size));
  390. return NULL;
  391. }
  392. EXPORT_SYMBOL(dma_alloc_coherent);
  393. /*
  394. * Unmap coherent memory.
  395. * The caller must ensure that the device has finished accessing the mapping.
  396. */
  397. void dma_free_coherent(struct device *dev, size_t size,
  398. void *vaddr, dma_addr_t bus)
  399. {
  400. int order = get_order(size);
  401. WARN_ON(irqs_disabled()); /* for portability */
  402. if (dma_release_coherent(dev, order, vaddr))
  403. return;
  404. if (dma_ops->unmap_single)
  405. dma_ops->unmap_single(dev, bus, size, 0);
  406. free_pages((unsigned long)vaddr, order);
  407. }
  408. EXPORT_SYMBOL(dma_free_coherent);
  409. static int __init pci_iommu_init(void)
  410. {
  411. #ifdef CONFIG_CALGARY_IOMMU
  412. calgary_iommu_init();
  413. #endif
  414. intel_iommu_init();
  415. #ifdef CONFIG_GART_IOMMU
  416. gart_iommu_init();
  417. #endif
  418. no_iommu_init();
  419. return 0;
  420. }
  421. void pci_iommu_shutdown(void)
  422. {
  423. gart_iommu_shutdown();
  424. }
  425. /* Must execute after PCI subsystem */
  426. fs_initcall(pci_iommu_init);
  427. #ifdef CONFIG_PCI
  428. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  429. static __devinit void via_no_dac(struct pci_dev *dev)
  430. {
  431. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
  432. printk(KERN_INFO "PCI: VIA PCI bridge detected."
  433. "Disabling DAC.\n");
  434. forbid_dac = 1;
  435. }
  436. }
  437. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
  438. #endif