dma_64.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. {
  48. return iommu_map_single(dev, dev->archdata.dma_data, vaddr, size,
  49. device_to_mask(dev), direction);
  50. }
  51. static void dma_iommu_unmap_single(struct device *dev, dma_addr_t dma_handle,
  52. size_t size,
  53. enum dma_data_direction direction)
  54. {
  55. iommu_unmap_single(dev->archdata.dma_data, dma_handle, size, direction);
  56. }
  57. static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
  58. int nelems, enum dma_data_direction direction)
  59. {
  60. return iommu_map_sg(dev, sglist, nelems,
  61. device_to_mask(dev), direction);
  62. }
  63. static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
  64. int nelems, enum dma_data_direction direction)
  65. {
  66. iommu_unmap_sg(dev->archdata.dma_data, sglist, nelems, direction);
  67. }
  68. /* We support DMA to/from any memory page via the iommu */
  69. static int dma_iommu_dma_supported(struct device *dev, u64 mask)
  70. {
  71. struct iommu_table *tbl = dev->archdata.dma_data;
  72. if (!tbl || tbl->it_offset > mask) {
  73. printk(KERN_INFO
  74. "Warning: IOMMU offset too big for device mask\n");
  75. if (tbl)
  76. printk(KERN_INFO
  77. "mask: 0x%08lx, table offset: 0x%08lx\n",
  78. mask, tbl->it_offset);
  79. else
  80. printk(KERN_INFO "mask: 0x%08lx, table unavailable\n",
  81. mask);
  82. return 0;
  83. } else
  84. return 1;
  85. }
  86. struct dma_mapping_ops dma_iommu_ops = {
  87. .alloc_coherent = dma_iommu_alloc_coherent,
  88. .free_coherent = dma_iommu_free_coherent,
  89. .map_single = dma_iommu_map_single,
  90. .unmap_single = dma_iommu_unmap_single,
  91. .map_sg = dma_iommu_map_sg,
  92. .unmap_sg = dma_iommu_unmap_sg,
  93. .dma_supported = dma_iommu_dma_supported,
  94. };
  95. EXPORT_SYMBOL(dma_iommu_ops);
  96. /*
  97. * Generic direct DMA implementation
  98. *
  99. * This implementation supports a per-device offset that can be applied if
  100. * the address at which memory is visible to devices is not 0. Platform code
  101. * can set archdata.dma_data to an unsigned long holding the offset. By
  102. * default the offset is zero.
  103. */
  104. static unsigned long get_dma_direct_offset(struct device *dev)
  105. {
  106. return (unsigned long)dev->archdata.dma_data;
  107. }
  108. static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
  109. dma_addr_t *dma_handle, gfp_t flag)
  110. {
  111. struct page *page;
  112. void *ret;
  113. int node = dev->archdata.numa_node;
  114. page = alloc_pages_node(node, flag, get_order(size));
  115. if (page == NULL)
  116. return NULL;
  117. ret = page_address(page);
  118. memset(ret, 0, size);
  119. *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
  120. return ret;
  121. }
  122. static void dma_direct_free_coherent(struct device *dev, size_t size,
  123. void *vaddr, dma_addr_t dma_handle)
  124. {
  125. free_pages((unsigned long)vaddr, get_order(size));
  126. }
  127. static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
  128. size_t size,
  129. enum dma_data_direction direction)
  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. {
  137. }
  138. static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
  139. int nents, enum dma_data_direction direction)
  140. {
  141. struct scatterlist *sg;
  142. int i;
  143. for_each_sg(sgl, sg, nents, i) {
  144. sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
  145. sg->dma_length = sg->length;
  146. }
  147. return nents;
  148. }
  149. static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
  150. int nents, enum dma_data_direction direction)
  151. {
  152. }
  153. static int dma_direct_dma_supported(struct device *dev, u64 mask)
  154. {
  155. /* Could be improved to check for memory though it better be
  156. * done via some global so platforms can set the limit in case
  157. * they have limited DMA windows
  158. */
  159. return mask >= DMA_32BIT_MASK;
  160. }
  161. struct dma_mapping_ops dma_direct_ops = {
  162. .alloc_coherent = dma_direct_alloc_coherent,
  163. .free_coherent = dma_direct_free_coherent,
  164. .map_single = dma_direct_map_single,
  165. .unmap_single = dma_direct_unmap_single,
  166. .map_sg = dma_direct_map_sg,
  167. .unmap_sg = dma_direct_unmap_sg,
  168. .dma_supported = dma_direct_dma_supported,
  169. };
  170. EXPORT_SYMBOL(dma_direct_ops);