sgbuf.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Scatter-Gather buffer
  3. *
  4. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/slab.h>
  22. #include <linux/mm.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/export.h>
  25. #include <asm/pgtable.h>
  26. #include <sound/memalloc.h>
  27. /* table entries are align to 32 */
  28. #define SGBUF_TBL_ALIGN 32
  29. #define sgbuf_align_table(tbl) ALIGN((tbl), SGBUF_TBL_ALIGN)
  30. int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab)
  31. {
  32. struct snd_sg_buf *sgbuf = dmab->private_data;
  33. struct snd_dma_buffer tmpb;
  34. int i;
  35. if (! sgbuf)
  36. return -EINVAL;
  37. vunmap(dmab->area);
  38. dmab->area = NULL;
  39. tmpb.dev.type = SNDRV_DMA_TYPE_DEV;
  40. if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC_SG)
  41. tmpb.dev.type = SNDRV_DMA_TYPE_DEV_UC;
  42. tmpb.dev.dev = sgbuf->dev;
  43. for (i = 0; i < sgbuf->pages; i++) {
  44. if (!(sgbuf->table[i].addr & ~PAGE_MASK))
  45. continue; /* continuous pages */
  46. tmpb.area = sgbuf->table[i].buf;
  47. tmpb.addr = sgbuf->table[i].addr & PAGE_MASK;
  48. tmpb.bytes = (sgbuf->table[i].addr & ~PAGE_MASK) << PAGE_SHIFT;
  49. snd_dma_free_pages(&tmpb);
  50. }
  51. kfree(sgbuf->table);
  52. kfree(sgbuf->page_table);
  53. kfree(sgbuf);
  54. dmab->private_data = NULL;
  55. return 0;
  56. }
  57. #define MAX_ALLOC_PAGES 32
  58. void *snd_malloc_sgbuf_pages(struct device *device,
  59. size_t size, struct snd_dma_buffer *dmab,
  60. size_t *res_size)
  61. {
  62. struct snd_sg_buf *sgbuf;
  63. unsigned int i, pages, chunk, maxpages;
  64. struct snd_dma_buffer tmpb;
  65. struct snd_sg_page *table;
  66. struct page **pgtable;
  67. int type = SNDRV_DMA_TYPE_DEV;
  68. pgprot_t prot = PAGE_KERNEL;
  69. dmab->area = NULL;
  70. dmab->addr = 0;
  71. dmab->private_data = sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
  72. if (! sgbuf)
  73. return NULL;
  74. if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC_SG) {
  75. type = SNDRV_DMA_TYPE_DEV_UC;
  76. #ifdef pgprot_noncached
  77. prot = pgprot_noncached(PAGE_KERNEL);
  78. #endif
  79. }
  80. sgbuf->dev = device;
  81. pages = snd_sgbuf_aligned_pages(size);
  82. sgbuf->tblsize = sgbuf_align_table(pages);
  83. table = kcalloc(sgbuf->tblsize, sizeof(*table), GFP_KERNEL);
  84. if (!table)
  85. goto _failed;
  86. sgbuf->table = table;
  87. pgtable = kcalloc(sgbuf->tblsize, sizeof(*pgtable), GFP_KERNEL);
  88. if (!pgtable)
  89. goto _failed;
  90. sgbuf->page_table = pgtable;
  91. /* allocate pages */
  92. maxpages = MAX_ALLOC_PAGES;
  93. while (pages > 0) {
  94. chunk = pages;
  95. /* don't be too eager to take a huge chunk */
  96. if (chunk > maxpages)
  97. chunk = maxpages;
  98. chunk <<= PAGE_SHIFT;
  99. if (snd_dma_alloc_pages_fallback(type, device,
  100. chunk, &tmpb) < 0) {
  101. if (!sgbuf->pages)
  102. goto _failed;
  103. if (!res_size)
  104. goto _failed;
  105. size = sgbuf->pages * PAGE_SIZE;
  106. break;
  107. }
  108. chunk = tmpb.bytes >> PAGE_SHIFT;
  109. for (i = 0; i < chunk; i++) {
  110. table->buf = tmpb.area;
  111. table->addr = tmpb.addr;
  112. if (!i)
  113. table->addr |= chunk; /* mark head */
  114. table++;
  115. *pgtable++ = virt_to_page(tmpb.area);
  116. tmpb.area += PAGE_SIZE;
  117. tmpb.addr += PAGE_SIZE;
  118. }
  119. sgbuf->pages += chunk;
  120. pages -= chunk;
  121. if (chunk < maxpages)
  122. maxpages = chunk;
  123. }
  124. sgbuf->size = size;
  125. dmab->area = vmap(sgbuf->page_table, sgbuf->pages, VM_MAP, prot);
  126. if (! dmab->area)
  127. goto _failed;
  128. if (res_size)
  129. *res_size = sgbuf->size;
  130. return dmab->area;
  131. _failed:
  132. snd_free_sgbuf_pages(dmab); /* free the table */
  133. return NULL;
  134. }
  135. /*
  136. * compute the max chunk size with continuous pages on sg-buffer
  137. */
  138. unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
  139. unsigned int ofs, unsigned int size)
  140. {
  141. struct snd_sg_buf *sg = dmab->private_data;
  142. unsigned int start, end, pg;
  143. start = ofs >> PAGE_SHIFT;
  144. end = (ofs + size - 1) >> PAGE_SHIFT;
  145. /* check page continuity */
  146. pg = sg->table[start].addr >> PAGE_SHIFT;
  147. for (;;) {
  148. start++;
  149. if (start > end)
  150. break;
  151. pg++;
  152. if ((sg->table[start].addr >> PAGE_SHIFT) != pg)
  153. return (start << PAGE_SHIFT) - ofs;
  154. }
  155. /* ok, all on continuous pages */
  156. return size;
  157. }
  158. EXPORT_SYMBOL(snd_sgbuf_get_chunk_size);