pci-dma.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Dynamic DMA mapping support.
  3. *
  4. * On i386 there is no hardware dynamic DMA address translation,
  5. * so consistent alloc/free are merely page allocation/freeing.
  6. * The rest of the dynamic DMA mapping interface is implemented
  7. * in asm/pci.h.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/mm.h>
  11. #include <linux/string.h>
  12. #include <linux/pci.h>
  13. #include <linux/module.h>
  14. #include <linux/pci.h>
  15. #include <asm/io.h>
  16. struct dma_coherent_mem {
  17. void *virt_base;
  18. u32 device_base;
  19. int size;
  20. int flags;
  21. unsigned long *bitmap;
  22. };
  23. void *dma_alloc_coherent(struct device *dev, size_t size,
  24. dma_addr_t *dma_handle, gfp_t gfp)
  25. {
  26. void *ret;
  27. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  28. int order = get_order(size);
  29. /* ignore region specifiers */
  30. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
  31. if (mem) {
  32. int page = bitmap_find_free_region(mem->bitmap, mem->size,
  33. order);
  34. if (page >= 0) {
  35. *dma_handle = mem->device_base + (page << PAGE_SHIFT);
  36. ret = mem->virt_base + (page << PAGE_SHIFT);
  37. memset(ret, 0, size);
  38. return ret;
  39. }
  40. if (mem->flags & DMA_MEMORY_EXCLUSIVE)
  41. return NULL;
  42. }
  43. if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
  44. gfp |= GFP_DMA;
  45. ret = (void *)__get_free_pages(gfp, order);
  46. if (ret != NULL) {
  47. memset(ret, 0, size);
  48. *dma_handle = virt_to_phys(ret);
  49. }
  50. return ret;
  51. }
  52. EXPORT_SYMBOL(dma_alloc_coherent);
  53. void dma_free_coherent(struct device *dev, size_t size,
  54. void *vaddr, dma_addr_t dma_handle)
  55. {
  56. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  57. int order = get_order(size);
  58. if (mem && vaddr >= mem->virt_base && vaddr < (mem->virt_base + (mem->size << PAGE_SHIFT))) {
  59. int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  60. bitmap_release_region(mem->bitmap, page, order);
  61. } else
  62. free_pages((unsigned long)vaddr, order);
  63. }
  64. EXPORT_SYMBOL(dma_free_coherent);
  65. int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  66. dma_addr_t device_addr, size_t size, int flags)
  67. {
  68. void __iomem *mem_base = NULL;
  69. int pages = size >> PAGE_SHIFT;
  70. int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
  71. if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
  72. goto out;
  73. if (!size)
  74. goto out;
  75. if (dev->dma_mem)
  76. goto out;
  77. /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
  78. mem_base = ioremap(bus_addr, size);
  79. if (!mem_base)
  80. goto out;
  81. dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
  82. if (!dev->dma_mem)
  83. goto out;
  84. dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  85. if (!dev->dma_mem->bitmap)
  86. goto free1_out;
  87. dev->dma_mem->virt_base = mem_base;
  88. dev->dma_mem->device_base = device_addr;
  89. dev->dma_mem->size = pages;
  90. dev->dma_mem->flags = flags;
  91. if (flags & DMA_MEMORY_MAP)
  92. return DMA_MEMORY_MAP;
  93. return DMA_MEMORY_IO;
  94. free1_out:
  95. kfree(dev->dma_mem);
  96. out:
  97. if (mem_base)
  98. iounmap(mem_base);
  99. return 0;
  100. }
  101. EXPORT_SYMBOL(dma_declare_coherent_memory);
  102. void dma_release_declared_memory(struct device *dev)
  103. {
  104. struct dma_coherent_mem *mem = dev->dma_mem;
  105. if(!mem)
  106. return;
  107. dev->dma_mem = NULL;
  108. iounmap(mem->virt_base);
  109. kfree(mem->bitmap);
  110. kfree(mem);
  111. }
  112. EXPORT_SYMBOL(dma_release_declared_memory);
  113. void *dma_mark_declared_memory_occupied(struct device *dev,
  114. dma_addr_t device_addr, size_t size)
  115. {
  116. struct dma_coherent_mem *mem = dev->dma_mem;
  117. int pages = (size + (device_addr & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  118. int pos, err;
  119. if (!mem)
  120. return ERR_PTR(-EINVAL);
  121. pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
  122. err = bitmap_allocate_region(mem->bitmap, pos, get_order(pages));
  123. if (err != 0)
  124. return ERR_PTR(err);
  125. return mem->virt_base + (pos << PAGE_SHIFT);
  126. }
  127. EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
  128. #ifdef CONFIG_PCI
  129. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  130. int forbid_dac;
  131. EXPORT_SYMBOL(forbid_dac);
  132. static __devinit void via_no_dac(struct pci_dev *dev)
  133. {
  134. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
  135. printk(KERN_INFO "PCI: VIA PCI bridge detected. Disabling DAC.\n");
  136. forbid_dac = 1;
  137. }
  138. }
  139. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
  140. static int check_iommu(char *s)
  141. {
  142. if (!strcmp(s, "usedac")) {
  143. forbid_dac = -1;
  144. return 1;
  145. }
  146. return 0;
  147. }
  148. __setup("iommu=", check_iommu);
  149. #endif