dma.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
  3. *
  4. * Provide default implementations of the DMA mapping callbacks for
  5. * directly mapped busses.
  6. */
  7. #include <linux/device.h>
  8. #include <linux/dma-mapping.h>
  9. #include <asm/bug.h>
  10. #include <asm/abs_addr.h>
  11. /*
  12. * Generic direct DMA implementation
  13. *
  14. * This implementation supports a per-device offset that can be applied if
  15. * the address at which memory is visible to devices is not 0. Platform code
  16. * can set archdata.dma_data to an unsigned long holding the offset. By
  17. * default the offset is zero.
  18. */
  19. static unsigned long get_dma_direct_offset(struct device *dev)
  20. {
  21. return (unsigned long)dev->archdata.dma_data;
  22. }
  23. static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
  24. dma_addr_t *dma_handle, gfp_t flag)
  25. {
  26. struct page *page;
  27. void *ret;
  28. int node = dev_to_node(dev);
  29. page = alloc_pages_node(node, flag, get_order(size));
  30. if (page == NULL)
  31. return NULL;
  32. ret = page_address(page);
  33. memset(ret, 0, size);
  34. *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
  35. return ret;
  36. }
  37. static void dma_direct_free_coherent(struct device *dev, size_t size,
  38. void *vaddr, dma_addr_t dma_handle)
  39. {
  40. free_pages((unsigned long)vaddr, get_order(size));
  41. }
  42. static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
  43. size_t size,
  44. enum dma_data_direction direction,
  45. struct dma_attrs *attrs)
  46. {
  47. return virt_to_abs(ptr) + get_dma_direct_offset(dev);
  48. }
  49. static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
  50. size_t size,
  51. enum dma_data_direction direction,
  52. struct dma_attrs *attrs)
  53. {
  54. }
  55. static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
  56. int nents, enum dma_data_direction direction,
  57. struct dma_attrs *attrs)
  58. {
  59. struct scatterlist *sg;
  60. int i;
  61. for_each_sg(sgl, sg, nents, i) {
  62. sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
  63. sg->dma_length = sg->length;
  64. }
  65. return nents;
  66. }
  67. static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
  68. int nents, enum dma_data_direction direction,
  69. struct dma_attrs *attrs)
  70. {
  71. }
  72. static int dma_direct_dma_supported(struct device *dev, u64 mask)
  73. {
  74. /* Could be improved to check for memory though it better be
  75. * done via some global so platforms can set the limit in case
  76. * they have limited DMA windows
  77. */
  78. return mask >= DMA_32BIT_MASK;
  79. }
  80. struct dma_mapping_ops dma_direct_ops = {
  81. .alloc_coherent = dma_direct_alloc_coherent,
  82. .free_coherent = dma_direct_free_coherent,
  83. .map_single = dma_direct_map_single,
  84. .unmap_single = dma_direct_unmap_single,
  85. .map_sg = dma_direct_map_sg,
  86. .unmap_sg = dma_direct_unmap_sg,
  87. .dma_supported = dma_direct_dma_supported,
  88. };
  89. EXPORT_SYMBOL(dma_direct_ops);