dma_64.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
  3. *
  4. * Provide default implementations of the DMA mapping callbacks for
  5. * directly mapped busses and busses using the iommu infrastructure
  6. */
  7. #include <linux/device.h>
  8. #include <linux/dma-mapping.h>
  9. #include <asm/bug.h>
  10. #include <asm/iommu.h>
  11. #include <asm/abs_addr.h>
  12. /*
  13. * Generic iommu implementation
  14. */
  15. /* Allocates a contiguous real buffer and creates mappings over it.
  16. * Returns the virtual address of the buffer and sets dma_handle
  17. * to the dma address (mapping) of the first page.
  18. */
  19. static void *dma_iommu_alloc_coherent(struct device *dev, size_t size,
  20. dma_addr_t *dma_handle, gfp_t flag)
  21. {
  22. return iommu_alloc_coherent(dev, dev->archdata.dma_data, size,
  23. dma_handle, device_to_mask(dev), flag,
  24. dev->archdata.numa_node);
  25. }
  26. static void dma_iommu_free_coherent(struct device *dev, size_t size,
  27. void *vaddr, dma_addr_t dma_handle)
  28. {
  29. iommu_free_coherent(dev->archdata.dma_data, size, vaddr, dma_handle);
  30. }
  31. /* Creates TCEs for a user provided buffer. The user buffer must be
  32. * contiguous real kernel storage (not vmalloc). The address of the buffer
  33. * passed here is the kernel (virtual) address of the buffer. The buffer
  34. * need not be page aligned, the dma_addr_t returned will point to the same
  35. * byte within the page as vaddr.
  36. */
  37. static dma_addr_t dma_iommu_map_single(struct device *dev, void *vaddr,
  38. size_t size,
  39. enum dma_data_direction direction,
  40. struct dma_attrs *attrs)
  41. {
  42. return iommu_map_single(dev, dev->archdata.dma_data, vaddr, size,
  43. device_to_mask(dev), direction, attrs);
  44. }
  45. static void dma_iommu_unmap_single(struct device *dev, dma_addr_t dma_handle,
  46. size_t size,
  47. enum dma_data_direction direction,
  48. struct dma_attrs *attrs)
  49. {
  50. iommu_unmap_single(dev->archdata.dma_data, dma_handle, size, direction,
  51. attrs);
  52. }
  53. static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
  54. int nelems, enum dma_data_direction direction,
  55. struct dma_attrs *attrs)
  56. {
  57. return iommu_map_sg(dev, dev->archdata.dma_data, sglist, nelems,
  58. device_to_mask(dev), direction, attrs);
  59. }
  60. static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
  61. int nelems, enum dma_data_direction direction,
  62. struct dma_attrs *attrs)
  63. {
  64. iommu_unmap_sg(dev->archdata.dma_data, sglist, nelems, direction,
  65. attrs);
  66. }
  67. /* We support DMA to/from any memory page via the iommu */
  68. static int dma_iommu_dma_supported(struct device *dev, u64 mask)
  69. {
  70. struct iommu_table *tbl = dev->archdata.dma_data;
  71. if (!tbl || tbl->it_offset > mask) {
  72. printk(KERN_INFO
  73. "Warning: IOMMU offset too big for device mask\n");
  74. if (tbl)
  75. printk(KERN_INFO
  76. "mask: 0x%08lx, table offset: 0x%08lx\n",
  77. mask, tbl->it_offset);
  78. else
  79. printk(KERN_INFO "mask: 0x%08lx, table unavailable\n",
  80. mask);
  81. return 0;
  82. } else
  83. return 1;
  84. }
  85. struct dma_mapping_ops dma_iommu_ops = {
  86. .alloc_coherent = dma_iommu_alloc_coherent,
  87. .free_coherent = dma_iommu_free_coherent,
  88. .map_single = dma_iommu_map_single,
  89. .unmap_single = dma_iommu_unmap_single,
  90. .map_sg = dma_iommu_map_sg,
  91. .unmap_sg = dma_iommu_unmap_sg,
  92. .dma_supported = dma_iommu_dma_supported,
  93. };
  94. EXPORT_SYMBOL(dma_iommu_ops);
  95. /*
  96. * Generic direct DMA implementation
  97. *
  98. * This implementation supports a per-device offset that can be applied if
  99. * the address at which memory is visible to devices is not 0. Platform code
  100. * can set archdata.dma_data to an unsigned long holding the offset. By
  101. * default the offset is zero.
  102. */
  103. static unsigned long get_dma_direct_offset(struct device *dev)
  104. {
  105. return (unsigned long)dev->archdata.dma_data;
  106. }
  107. static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
  108. dma_addr_t *dma_handle, gfp_t flag)
  109. {
  110. struct page *page;
  111. void *ret;
  112. int node = dev->archdata.numa_node;
  113. page = alloc_pages_node(node, flag, get_order(size));
  114. if (page == NULL)
  115. return NULL;
  116. ret = page_address(page);
  117. memset(ret, 0, size);
  118. *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
  119. return ret;
  120. }
  121. static void dma_direct_free_coherent(struct device *dev, size_t size,
  122. void *vaddr, dma_addr_t dma_handle)
  123. {
  124. free_pages((unsigned long)vaddr, get_order(size));
  125. }
  126. static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
  127. size_t size,
  128. enum dma_data_direction direction,
  129. struct dma_attrs *attrs)
  130. {
  131. return virt_to_abs(ptr) + get_dma_direct_offset(dev);
  132. }
  133. static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
  134. size_t size,
  135. enum dma_data_direction direction,
  136. struct dma_attrs *attrs)
  137. {
  138. }
  139. static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
  140. int nents, enum dma_data_direction direction,
  141. struct dma_attrs *attrs)
  142. {
  143. struct scatterlist *sg;
  144. int i;
  145. for_each_sg(sgl, sg, nents, i) {
  146. sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
  147. sg->dma_length = sg->length;
  148. }
  149. return nents;
  150. }
  151. static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
  152. int nents, enum dma_data_direction direction,
  153. struct dma_attrs *attrs)
  154. {
  155. }
  156. static int dma_direct_dma_supported(struct device *dev, u64 mask)
  157. {
  158. /* Could be improved to check for memory though it better be
  159. * done via some global so platforms can set the limit in case
  160. * they have limited DMA windows
  161. */
  162. return mask >= DMA_32BIT_MASK;
  163. }
  164. struct dma_mapping_ops dma_direct_ops = {
  165. .alloc_coherent = dma_direct_alloc_coherent,
  166. .free_coherent = dma_direct_free_coherent,
  167. .map_single = dma_direct_map_single,
  168. .unmap_single = dma_direct_unmap_single,
  169. .map_sg = dma_direct_map_sg,
  170. .unmap_sg = dma_direct_unmap_sg,
  171. .dma_supported = dma_direct_dma_supported,
  172. };
  173. EXPORT_SYMBOL(dma_direct_ops);