dma-mapping.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Implements the generic device dma API for microblaze and the pci
  3. *
  4. * Copyright (C) 2009-2010 Michal Simek <monstr@monstr.eu>
  5. * Copyright (C) 2009-2010 PetaLogix
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. *
  11. * This file is base on powerpc and x86 dma-mapping.h versions
  12. * Copyright (C) 2004 IBM
  13. */
  14. #ifndef _ASM_MICROBLAZE_DMA_MAPPING_H
  15. #define _ASM_MICROBLAZE_DMA_MAPPING_H
  16. /*
  17. * See Documentation/PCI/PCI-DMA-mapping.txt and
  18. * Documentation/DMA-API.txt for documentation.
  19. */
  20. #include <linux/types.h>
  21. #include <linux/cache.h>
  22. #include <linux/mm.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/dma-debug.h>
  25. #include <linux/dma-attrs.h>
  26. #include <asm/io.h>
  27. #include <asm-generic/dma-coherent.h>
  28. #define DMA_ERROR_CODE (~(dma_addr_t)0x0)
  29. #define __dma_alloc_coherent(dev, gfp, size, handle) NULL
  30. #define __dma_free_coherent(size, addr) ((void)0)
  31. #define __dma_sync(addr, size, rw) ((void)0)
  32. static inline unsigned long device_to_mask(struct device *dev)
  33. {
  34. if (dev->dma_mask && *dev->dma_mask)
  35. return *dev->dma_mask;
  36. /* Assume devices without mask can take 32 bit addresses */
  37. return 0xfffffffful;
  38. }
  39. extern struct dma_map_ops *dma_ops;
  40. /*
  41. * Available generic sets of operations
  42. */
  43. extern struct dma_map_ops dma_direct_ops;
  44. static inline struct dma_map_ops *get_dma_ops(struct device *dev)
  45. {
  46. /* We don't handle the NULL dev case for ISA for now. We could
  47. * do it via an out of line call but it is not needed for now. The
  48. * only ISA DMA device we support is the floppy and we have a hack
  49. * in the floppy driver directly to get a device for us.
  50. */
  51. if (unlikely(!dev) || !dev->archdata.dma_ops)
  52. return NULL;
  53. return dev->archdata.dma_ops;
  54. }
  55. static inline void set_dma_ops(struct device *dev, struct dma_map_ops *ops)
  56. {
  57. dev->archdata.dma_ops = ops;
  58. }
  59. static inline int dma_supported(struct device *dev, u64 mask)
  60. {
  61. struct dma_map_ops *ops = get_dma_ops(dev);
  62. if (unlikely(!ops))
  63. return 0;
  64. if (!ops->dma_supported)
  65. return 1;
  66. return ops->dma_supported(dev, mask);
  67. }
  68. #ifdef CONFIG_PCI
  69. /* We have our own implementation of pci_set_dma_mask() */
  70. #define HAVE_ARCH_PCI_SET_DMA_MASK
  71. #endif
  72. static inline int dma_set_mask(struct device *dev, u64 dma_mask)
  73. {
  74. struct dma_map_ops *ops = get_dma_ops(dev);
  75. if (unlikely(ops == NULL))
  76. return -EIO;
  77. if (ops->set_dma_mask)
  78. return ops->set_dma_mask(dev, dma_mask);
  79. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  80. return -EIO;
  81. *dev->dma_mask = dma_mask;
  82. return 0;
  83. }
  84. #include <asm-generic/dma-mapping-common.h>
  85. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  86. {
  87. struct dma_map_ops *ops = get_dma_ops(dev);
  88. if (ops->mapping_error)
  89. return ops->mapping_error(dev, dma_addr);
  90. return (dma_addr == DMA_ERROR_CODE);
  91. }
  92. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  93. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  94. #define dma_is_consistent(d, h) (1)
  95. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  96. dma_addr_t *dma_handle, gfp_t flag)
  97. {
  98. struct dma_map_ops *ops = get_dma_ops(dev);
  99. void *memory;
  100. BUG_ON(!ops);
  101. memory = ops->alloc_coherent(dev, size, dma_handle, flag);
  102. debug_dma_alloc_coherent(dev, size, *dma_handle, memory);
  103. return memory;
  104. }
  105. static inline void dma_free_coherent(struct device *dev, size_t size,
  106. void *cpu_addr, dma_addr_t dma_handle)
  107. {
  108. struct dma_map_ops *ops = get_dma_ops(dev);
  109. BUG_ON(!ops);
  110. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  111. ops->free_coherent(dev, size, cpu_addr, dma_handle);
  112. }
  113. static inline int dma_get_cache_alignment(void)
  114. {
  115. return L1_CACHE_BYTES;
  116. }
  117. static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  118. enum dma_data_direction direction)
  119. {
  120. BUG_ON(direction == DMA_NONE);
  121. __dma_sync(vaddr, size, (int)direction);
  122. }
  123. #endif /* _ASM_MICROBLAZE_DMA_MAPPING_H */