sgbuf.c 4.2 KB

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