pci-dma_32.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <asm/io.h>
  15. /* For i386, we make it point to the NULL address */
  16. dma_addr_t bad_dma_address __read_mostly = 0x0;
  17. EXPORT_SYMBOL(bad_dma_address);
  18. struct dma_coherent_mem {
  19. void *virt_base;
  20. u32 device_base;
  21. int size;
  22. int flags;
  23. unsigned long *bitmap;
  24. };
  25. void *dma_alloc_coherent(struct device *dev, size_t size,
  26. dma_addr_t *dma_handle, gfp_t gfp)
  27. {
  28. void *ret;
  29. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  30. int order = get_order(size);
  31. /* ignore region specifiers */
  32. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
  33. if (mem) {
  34. int page = bitmap_find_free_region(mem->bitmap, mem->size,
  35. order);
  36. if (page >= 0) {
  37. *dma_handle = mem->device_base + (page << PAGE_SHIFT);
  38. ret = mem->virt_base + (page << PAGE_SHIFT);
  39. memset(ret, 0, size);
  40. return ret;
  41. }
  42. if (mem->flags & DMA_MEMORY_EXCLUSIVE)
  43. return NULL;
  44. }
  45. if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
  46. gfp |= GFP_DMA;
  47. ret = (void *)__get_free_pages(gfp, order);
  48. if (ret != NULL) {
  49. memset(ret, 0, size);
  50. *dma_handle = virt_to_phys(ret);
  51. }
  52. return ret;
  53. }
  54. EXPORT_SYMBOL(dma_alloc_coherent);
  55. void dma_free_coherent(struct device *dev, size_t size,
  56. void *vaddr, dma_addr_t dma_handle)
  57. {
  58. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  59. int order = get_order(size);
  60. WARN_ON(irqs_disabled()); /* for portability */
  61. if (mem && vaddr >= mem->virt_base && vaddr < (mem->virt_base + (mem->size << PAGE_SHIFT))) {
  62. int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  63. bitmap_release_region(mem->bitmap, page, order);
  64. } else
  65. free_pages((unsigned long)vaddr, order);
  66. }
  67. EXPORT_SYMBOL(dma_free_coherent);
  68. int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  69. dma_addr_t device_addr, size_t size, int flags)
  70. {
  71. void __iomem *mem_base = NULL;
  72. int pages = size >> PAGE_SHIFT;
  73. int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
  74. if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
  75. goto out;
  76. if (!size)
  77. goto out;
  78. if (dev->dma_mem)
  79. goto out;
  80. /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
  81. mem_base = ioremap(bus_addr, size);
  82. if (!mem_base)
  83. goto out;
  84. dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
  85. if (!dev->dma_mem)
  86. goto out;
  87. dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  88. if (!dev->dma_mem->bitmap)
  89. goto free1_out;
  90. dev->dma_mem->virt_base = mem_base;
  91. dev->dma_mem->device_base = device_addr;
  92. dev->dma_mem->size = pages;
  93. dev->dma_mem->flags = flags;
  94. if (flags & DMA_MEMORY_MAP)
  95. return DMA_MEMORY_MAP;
  96. return DMA_MEMORY_IO;
  97. free1_out:
  98. kfree(dev->dma_mem);
  99. out:
  100. if (mem_base)
  101. iounmap(mem_base);
  102. return 0;
  103. }
  104. EXPORT_SYMBOL(dma_declare_coherent_memory);
  105. void dma_release_declared_memory(struct device *dev)
  106. {
  107. struct dma_coherent_mem *mem = dev->dma_mem;
  108. if(!mem)
  109. return;
  110. dev->dma_mem = NULL;
  111. iounmap(mem->virt_base);
  112. kfree(mem->bitmap);
  113. kfree(mem);
  114. }
  115. EXPORT_SYMBOL(dma_release_declared_memory);
  116. void *dma_mark_declared_memory_occupied(struct device *dev,
  117. dma_addr_t device_addr, size_t size)
  118. {
  119. struct dma_coherent_mem *mem = dev->dma_mem;
  120. int pages = (size + (device_addr & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  121. int pos, err;
  122. if (!mem)
  123. return ERR_PTR(-EINVAL);
  124. pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
  125. err = bitmap_allocate_region(mem->bitmap, pos, get_order(pages));
  126. if (err != 0)
  127. return ERR_PTR(err);
  128. return mem->virt_base + (pos << PAGE_SHIFT);
  129. }
  130. EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
  131. #ifdef CONFIG_PCI
  132. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  133. int
  134. dma_supported(struct device *dev, u64 mask)
  135. {
  136. /*
  137. * we fall back to GFP_DMA when the mask isn't all 1s,
  138. * so we can't guarantee allocations that must be
  139. * within a tighter range than GFP_DMA..
  140. */
  141. if (mask < 0x00ffffff)
  142. return 0;
  143. /* Work around chipset bugs */
  144. if (forbid_dac > 0 && mask > 0xffffffffULL)
  145. return 0;
  146. if (dma_ops->dma_supported)
  147. return dma_ops->dma_supported(dev, mask);
  148. return 1;
  149. }
  150. EXPORT_SYMBOL(dma_supported);
  151. static int check_iommu(char *s)
  152. {
  153. if (!strcmp(s, "usedac")) {
  154. forbid_dac = -1;
  155. return 1;
  156. }
  157. return 0;
  158. }
  159. __setup("iommu=", check_iommu);
  160. #endif