pci-dma_32.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. WARN_ON(irqs_disabled()); /* for portability */
  59. if (mem && vaddr >= mem->virt_base && vaddr < (mem->virt_base + (mem->size << PAGE_SHIFT))) {
  60. int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  61. bitmap_release_region(mem->bitmap, page, order);
  62. } else
  63. free_pages((unsigned long)vaddr, order);
  64. }
  65. EXPORT_SYMBOL(dma_free_coherent);
  66. int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  67. dma_addr_t device_addr, size_t size, int flags)
  68. {
  69. void __iomem *mem_base = NULL;
  70. int pages = size >> PAGE_SHIFT;
  71. int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
  72. if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
  73. goto out;
  74. if (!size)
  75. goto out;
  76. if (dev->dma_mem)
  77. goto out;
  78. /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
  79. mem_base = ioremap(bus_addr, size);
  80. if (!mem_base)
  81. goto out;
  82. dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
  83. if (!dev->dma_mem)
  84. goto out;
  85. dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  86. if (!dev->dma_mem->bitmap)
  87. goto free1_out;
  88. dev->dma_mem->virt_base = mem_base;
  89. dev->dma_mem->device_base = device_addr;
  90. dev->dma_mem->size = pages;
  91. dev->dma_mem->flags = flags;
  92. if (flags & DMA_MEMORY_MAP)
  93. return DMA_MEMORY_MAP;
  94. return DMA_MEMORY_IO;
  95. free1_out:
  96. kfree(dev->dma_mem);
  97. out:
  98. if (mem_base)
  99. iounmap(mem_base);
  100. return 0;
  101. }
  102. EXPORT_SYMBOL(dma_declare_coherent_memory);
  103. void dma_release_declared_memory(struct device *dev)
  104. {
  105. struct dma_coherent_mem *mem = dev->dma_mem;
  106. if(!mem)
  107. return;
  108. dev->dma_mem = NULL;
  109. iounmap(mem->virt_base);
  110. kfree(mem->bitmap);
  111. kfree(mem);
  112. }
  113. EXPORT_SYMBOL(dma_release_declared_memory);
  114. void *dma_mark_declared_memory_occupied(struct device *dev,
  115. dma_addr_t device_addr, size_t size)
  116. {
  117. struct dma_coherent_mem *mem = dev->dma_mem;
  118. int pages = (size + (device_addr & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  119. int pos, err;
  120. if (!mem)
  121. return ERR_PTR(-EINVAL);
  122. pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
  123. err = bitmap_allocate_region(mem->bitmap, pos, get_order(pages));
  124. if (err != 0)
  125. return ERR_PTR(err);
  126. return mem->virt_base + (pos << PAGE_SHIFT);
  127. }
  128. EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
  129. #ifdef CONFIG_PCI
  130. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  131. int forbid_dac;
  132. EXPORT_SYMBOL(forbid_dac);
  133. static __devinit void via_no_dac(struct pci_dev *dev)
  134. {
  135. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
  136. printk(KERN_INFO "PCI: VIA PCI bridge detected. Disabling DAC.\n");
  137. forbid_dac = 1;
  138. }
  139. }
  140. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
  141. static int check_iommu(char *s)
  142. {
  143. if (!strcmp(s, "usedac")) {
  144. forbid_dac = -1;
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. __setup("iommu=", check_iommu);
  150. #endif