sgbuf.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <sound/memalloc.h>
  26. /* table entries are align to 32 */
  27. #define SGBUF_TBL_ALIGN 32
  28. #define sgbuf_align_table(tbl) ALIGN((tbl), SGBUF_TBL_ALIGN)
  29. int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab)
  30. {
  31. struct snd_sg_buf *sgbuf = dmab->private_data;
  32. struct snd_dma_buffer tmpb;
  33. int i;
  34. if (! sgbuf)
  35. return -EINVAL;
  36. vunmap(dmab->area);
  37. dmab->area = NULL;
  38. tmpb.dev.type = SNDRV_DMA_TYPE_DEV;
  39. tmpb.dev.dev = sgbuf->dev;
  40. for (i = 0; i < sgbuf->pages; i++) {
  41. if (!(sgbuf->table[i].addr & ~PAGE_MASK))
  42. continue; /* continuous pages */
  43. tmpb.area = sgbuf->table[i].buf;
  44. tmpb.addr = sgbuf->table[i].addr & PAGE_MASK;
  45. tmpb.bytes = (sgbuf->table[i].addr & ~PAGE_MASK) << PAGE_SHIFT;
  46. snd_dma_free_pages(&tmpb);
  47. }
  48. kfree(sgbuf->table);
  49. kfree(sgbuf->page_table);
  50. kfree(sgbuf);
  51. dmab->private_data = NULL;
  52. return 0;
  53. }
  54. #define MAX_ALLOC_PAGES 32
  55. void *snd_malloc_sgbuf_pages(struct device *device,
  56. size_t size, struct snd_dma_buffer *dmab,
  57. size_t *res_size)
  58. {
  59. struct snd_sg_buf *sgbuf;
  60. unsigned int i, pages, chunk, maxpages;
  61. struct snd_dma_buffer tmpb;
  62. struct snd_sg_page *table;
  63. struct page **pgtable;
  64. dmab->area = NULL;
  65. dmab->addr = 0;
  66. dmab->private_data = sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
  67. if (! sgbuf)
  68. return NULL;
  69. sgbuf->dev = device;
  70. pages = snd_sgbuf_aligned_pages(size);
  71. sgbuf->tblsize = sgbuf_align_table(pages);
  72. table = kcalloc(sgbuf->tblsize, sizeof(*table), GFP_KERNEL);
  73. if (!table)
  74. goto _failed;
  75. sgbuf->table = table;
  76. pgtable = kcalloc(sgbuf->tblsize, sizeof(*pgtable), GFP_KERNEL);
  77. if (!pgtable)
  78. goto _failed;
  79. sgbuf->page_table = pgtable;
  80. /* allocate pages */
  81. maxpages = MAX_ALLOC_PAGES;
  82. while (pages > 0) {
  83. chunk = pages;
  84. /* don't be too eager to take a huge chunk */
  85. if (chunk > maxpages)
  86. chunk = maxpages;
  87. chunk <<= PAGE_SHIFT;
  88. if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV, device,
  89. chunk, &tmpb) < 0) {
  90. if (!sgbuf->pages)
  91. goto _failed;
  92. if (!res_size)
  93. goto _failed;
  94. size = sgbuf->pages * PAGE_SIZE;
  95. break;
  96. }
  97. chunk = tmpb.bytes >> PAGE_SHIFT;
  98. for (i = 0; i < chunk; i++) {
  99. table->buf = tmpb.area;
  100. table->addr = tmpb.addr;
  101. if (!i)
  102. table->addr |= chunk; /* mark head */
  103. table++;
  104. *pgtable++ = virt_to_page(tmpb.area);
  105. tmpb.area += PAGE_SIZE;
  106. tmpb.addr += PAGE_SIZE;
  107. }
  108. sgbuf->pages += chunk;
  109. pages -= chunk;
  110. if (chunk < maxpages)
  111. maxpages = chunk;
  112. }
  113. sgbuf->size = size;
  114. dmab->area = vmap(sgbuf->page_table, sgbuf->pages, VM_MAP, PAGE_KERNEL);
  115. if (! dmab->area)
  116. goto _failed;
  117. if (res_size)
  118. *res_size = sgbuf->size;
  119. return dmab->area;
  120. _failed:
  121. snd_free_sgbuf_pages(dmab); /* free the table */
  122. return NULL;
  123. }
  124. /*
  125. * compute the max chunk size with continuous pages on sg-buffer
  126. */
  127. unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
  128. unsigned int ofs, unsigned int size)
  129. {
  130. struct snd_sg_buf *sg = dmab->private_data;
  131. unsigned int start, end, pg;
  132. start = ofs >> PAGE_SHIFT;
  133. end = (ofs + size - 1) >> PAGE_SHIFT;
  134. /* check page continuity */
  135. pg = sg->table[start].addr >> PAGE_SHIFT;
  136. for (;;) {
  137. start++;
  138. if (start > end)
  139. break;
  140. pg++;
  141. if ((sg->table[start].addr >> PAGE_SHIFT) != pg)
  142. return (start << PAGE_SHIFT) - ofs;
  143. }
  144. /* ok, all on continuous pages */
  145. return size;
  146. }
  147. EXPORT_SYMBOL(snd_sgbuf_get_chunk_size);