memalloc.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (c) by Jaroslav Kysela <[email protected]>
  4. * Takashi Iwai <[email protected]>
  5. *
  6. * Generic memory allocators
  7. */
  8. #ifndef __SOUND_MEMALLOC_H
  9. #define __SOUND_MEMALLOC_H
  10. #include <linux/dma-direction.h>
  11. #include <asm/page.h>
  12. struct device;
  13. struct vm_area_struct;
  14. struct sg_table;
  15. /*
  16. * buffer device info
  17. */
  18. struct snd_dma_device {
  19. int type; /* SNDRV_DMA_TYPE_XXX */
  20. enum dma_data_direction dir; /* DMA direction */
  21. bool need_sync; /* explicit sync needed? */
  22. struct device *dev; /* generic device */
  23. };
  24. /*
  25. * buffer types
  26. */
  27. #define SNDRV_DMA_TYPE_UNKNOWN 0 /* not defined */
  28. #define SNDRV_DMA_TYPE_CONTINUOUS 1 /* continuous no-DMA memory */
  29. #define SNDRV_DMA_TYPE_DEV 2 /* generic device continuous */
  30. #define SNDRV_DMA_TYPE_DEV_WC 5 /* continuous write-combined */
  31. #ifdef CONFIG_GENERIC_ALLOCATOR
  32. #define SNDRV_DMA_TYPE_DEV_IRAM 4 /* generic device iram-buffer */
  33. #else
  34. #define SNDRV_DMA_TYPE_DEV_IRAM SNDRV_DMA_TYPE_DEV
  35. #endif
  36. #define SNDRV_DMA_TYPE_VMALLOC 7 /* vmalloc'ed buffer */
  37. #define SNDRV_DMA_TYPE_NONCONTIG 8 /* non-coherent SG buffer */
  38. #define SNDRV_DMA_TYPE_NONCOHERENT 9 /* non-coherent buffer */
  39. #ifdef CONFIG_SND_DMA_SGBUF
  40. #define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_NONCONTIG
  41. #define SNDRV_DMA_TYPE_DEV_WC_SG 6 /* SG write-combined */
  42. #else
  43. #define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_DEV /* no SG-buf support */
  44. #define SNDRV_DMA_TYPE_DEV_WC_SG SNDRV_DMA_TYPE_DEV_WC
  45. #endif
  46. /* fallback types, don't use those directly */
  47. #ifdef CONFIG_SND_DMA_SGBUF
  48. #define SNDRV_DMA_TYPE_DEV_SG_FALLBACK 10
  49. #define SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK 11
  50. #endif
  51. /*
  52. * info for buffer allocation
  53. */
  54. struct snd_dma_buffer {
  55. struct snd_dma_device dev; /* device type */
  56. unsigned char *area; /* virtual pointer */
  57. dma_addr_t addr; /* physical address */
  58. size_t bytes; /* buffer size in bytes */
  59. void *private_data; /* private for allocator; don't touch */
  60. };
  61. /*
  62. * return the pages matching with the given byte size
  63. */
  64. static inline unsigned int snd_sgbuf_aligned_pages(size_t size)
  65. {
  66. return (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  67. }
  68. /* allocate/release a buffer */
  69. int snd_dma_alloc_dir_pages(int type, struct device *dev,
  70. enum dma_data_direction dir, size_t size,
  71. struct snd_dma_buffer *dmab);
  72. static inline int snd_dma_alloc_pages(int type, struct device *dev,
  73. size_t size, struct snd_dma_buffer *dmab)
  74. {
  75. return snd_dma_alloc_dir_pages(type, dev, DMA_BIDIRECTIONAL, size, dmab);
  76. }
  77. int snd_dma_alloc_pages_fallback(int type, struct device *dev, size_t size,
  78. struct snd_dma_buffer *dmab);
  79. void snd_dma_free_pages(struct snd_dma_buffer *dmab);
  80. int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
  81. struct vm_area_struct *area);
  82. enum snd_dma_sync_mode { SNDRV_DMA_SYNC_CPU, SNDRV_DMA_SYNC_DEVICE };
  83. #ifdef CONFIG_HAS_DMA
  84. void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
  85. enum snd_dma_sync_mode mode);
  86. #else
  87. static inline void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
  88. enum snd_dma_sync_mode mode) {}
  89. #endif
  90. dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset);
  91. struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset);
  92. unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
  93. unsigned int ofs, unsigned int size);
  94. /* device-managed memory allocator */
  95. struct snd_dma_buffer *snd_devm_alloc_dir_pages(struct device *dev, int type,
  96. enum dma_data_direction dir,
  97. size_t size);
  98. static inline struct snd_dma_buffer *
  99. snd_devm_alloc_pages(struct device *dev, int type, size_t size)
  100. {
  101. return snd_devm_alloc_dir_pages(dev, type, DMA_BIDIRECTIONAL, size);
  102. }
  103. static inline struct sg_table *
  104. snd_dma_noncontig_sg_table(struct snd_dma_buffer *dmab)
  105. {
  106. return dmab->private_data;
  107. }
  108. #endif /* __SOUND_MEMALLOC_H */