dma_64.c 5.9 KB

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