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. 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->archdata.dma_data, size, dma_handle,
  30. 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->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->archdata.dma_data, 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. /* TODO: Maybe use the numa node here too ? */
  115. page = alloc_pages_node(node, flag, get_order(size));
  116. if (page == NULL)
  117. return NULL;
  118. ret = page_address(page);
  119. memset(ret, 0, size);
  120. *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
  121. return ret;
  122. }
  123. static void dma_direct_free_coherent(struct device *dev, size_t size,
  124. void *vaddr, dma_addr_t dma_handle)
  125. {
  126. free_pages((unsigned long)vaddr, get_order(size));
  127. }
  128. static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
  129. size_t size,
  130. enum dma_data_direction direction)
  131. {
  132. return virt_to_abs(ptr) + get_dma_direct_offset(dev);
  133. }
  134. static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
  135. size_t size,
  136. enum dma_data_direction direction)
  137. {
  138. }
  139. static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
  140. int nents, enum dma_data_direction direction)
  141. {
  142. struct scatterlist *sg;
  143. int i;
  144. for_each_sg(sgl, sg, nents, i) {
  145. sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
  146. sg->dma_length = sg->length;
  147. }
  148. return nents;
  149. }
  150. static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
  151. int nents, enum dma_data_direction direction)
  152. {
  153. }
  154. static int dma_direct_dma_supported(struct device *dev, u64 mask)
  155. {
  156. /* Could be improved to check for memory though it better be
  157. * done via some global so platforms can set the limit in case
  158. * they have limited DMA windows
  159. */
  160. return mask >= DMA_32BIT_MASK;
  161. }
  162. struct dma_mapping_ops dma_direct_ops = {
  163. .alloc_coherent = dma_direct_alloc_coherent,
  164. .free_coherent = dma_direct_free_coherent,
  165. .map_single = dma_direct_map_single,
  166. .unmap_single = dma_direct_unmap_single,
  167. .map_sg = dma_direct_map_sg,
  168. .unmap_sg = dma_direct_unmap_sg,
  169. .dma_supported = dma_direct_dma_supported,
  170. };
  171. EXPORT_SYMBOL(dma_direct_ops);