pci-dma.c 12 KB

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