memalloc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Takashi Iwai <tiwai@suse.de>
  4. *
  5. * Generic memory allocators
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/slab.h>
  28. #include <linux/mm.h>
  29. #include <linux/seq_file.h>
  30. #include <asm/uaccess.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/mutex.h>
  34. #include <sound/memalloc.h>
  35. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>");
  36. MODULE_DESCRIPTION("Memory allocator for ALSA system.");
  37. MODULE_LICENSE("GPL");
  38. /*
  39. */
  40. void *snd_malloc_sgbuf_pages(struct device *device,
  41. size_t size, struct snd_dma_buffer *dmab,
  42. size_t *res_size);
  43. int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
  44. /*
  45. */
  46. static DEFINE_MUTEX(list_mutex);
  47. static LIST_HEAD(mem_list_head);
  48. /* buffer preservation list */
  49. struct snd_mem_list {
  50. struct snd_dma_buffer buffer;
  51. unsigned int id;
  52. struct list_head list;
  53. };
  54. /* id for pre-allocated buffers */
  55. #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
  56. #ifdef CONFIG_SND_DEBUG
  57. #define __ASTRING__(x) #x
  58. #define snd_assert(expr, args...) do {\
  59. if (!(expr)) {\
  60. printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
  61. args;\
  62. }\
  63. } while (0)
  64. #else
  65. #define snd_assert(expr, args...) /**/
  66. #endif
  67. /*
  68. *
  69. * Generic memory allocators
  70. *
  71. */
  72. static long snd_allocated_pages; /* holding the number of allocated pages */
  73. static inline void inc_snd_pages(int order)
  74. {
  75. snd_allocated_pages += 1 << order;
  76. }
  77. static inline void dec_snd_pages(int order)
  78. {
  79. snd_allocated_pages -= 1 << order;
  80. }
  81. /**
  82. * snd_malloc_pages - allocate pages with the given size
  83. * @size: the size to allocate in bytes
  84. * @gfp_flags: the allocation conditions, GFP_XXX
  85. *
  86. * Allocates the physically contiguous pages with the given size.
  87. *
  88. * Returns the pointer of the buffer, or NULL if no enoguh memory.
  89. */
  90. void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
  91. {
  92. int pg;
  93. void *res;
  94. snd_assert(size > 0, return NULL);
  95. snd_assert(gfp_flags != 0, return NULL);
  96. gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
  97. pg = get_order(size);
  98. if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
  99. inc_snd_pages(pg);
  100. return res;
  101. }
  102. /**
  103. * snd_free_pages - release the pages
  104. * @ptr: the buffer pointer to release
  105. * @size: the allocated buffer size
  106. *
  107. * Releases the buffer allocated via snd_malloc_pages().
  108. */
  109. void snd_free_pages(void *ptr, size_t size)
  110. {
  111. int pg;
  112. if (ptr == NULL)
  113. return;
  114. pg = get_order(size);
  115. dec_snd_pages(pg);
  116. free_pages((unsigned long) ptr, pg);
  117. }
  118. /*
  119. *
  120. * Bus-specific memory allocators
  121. *
  122. */
  123. #ifdef CONFIG_HAS_DMA
  124. /* allocate the coherent DMA pages */
  125. static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
  126. {
  127. int pg;
  128. void *res;
  129. gfp_t gfp_flags;
  130. snd_assert(size > 0, return NULL);
  131. snd_assert(dma != NULL, return NULL);
  132. pg = get_order(size);
  133. gfp_flags = GFP_KERNEL
  134. | __GFP_COMP /* compound page lets parts be mapped */
  135. | __GFP_NORETRY /* don't trigger OOM-killer */
  136. | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
  137. res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
  138. if (res != NULL)
  139. inc_snd_pages(pg);
  140. return res;
  141. }
  142. /* free the coherent DMA pages */
  143. static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
  144. dma_addr_t dma)
  145. {
  146. int pg;
  147. if (ptr == NULL)
  148. return;
  149. pg = get_order(size);
  150. dec_snd_pages(pg);
  151. dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
  152. }
  153. #endif /* CONFIG_HAS_DMA */
  154. /*
  155. *
  156. * ALSA generic memory management
  157. *
  158. */
  159. /**
  160. * snd_dma_alloc_pages - allocate the buffer area according to the given type
  161. * @type: the DMA buffer type
  162. * @device: the device pointer
  163. * @size: the buffer size to allocate
  164. * @dmab: buffer allocation record to store the allocated data
  165. *
  166. * Calls the memory-allocator function for the corresponding
  167. * buffer type.
  168. *
  169. * Returns zero if the buffer with the given size is allocated successfuly,
  170. * other a negative value at error.
  171. */
  172. int snd_dma_alloc_pages(int type, struct device *device, size_t size,
  173. struct snd_dma_buffer *dmab)
  174. {
  175. snd_assert(size > 0, return -ENXIO);
  176. snd_assert(dmab != NULL, return -ENXIO);
  177. dmab->dev.type = type;
  178. dmab->dev.dev = device;
  179. dmab->bytes = 0;
  180. switch (type) {
  181. case SNDRV_DMA_TYPE_CONTINUOUS:
  182. dmab->area = snd_malloc_pages(size, (unsigned long)device);
  183. dmab->addr = 0;
  184. break;
  185. #ifdef CONFIG_HAS_DMA
  186. case SNDRV_DMA_TYPE_DEV:
  187. dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
  188. break;
  189. case SNDRV_DMA_TYPE_DEV_SG:
  190. snd_malloc_sgbuf_pages(device, size, dmab, NULL);
  191. break;
  192. #endif
  193. default:
  194. printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
  195. dmab->area = NULL;
  196. dmab->addr = 0;
  197. return -ENXIO;
  198. }
  199. if (! dmab->area)
  200. return -ENOMEM;
  201. dmab->bytes = size;
  202. return 0;
  203. }
  204. /**
  205. * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
  206. * @type: the DMA buffer type
  207. * @device: the device pointer
  208. * @size: the buffer size to allocate
  209. * @dmab: buffer allocation record to store the allocated data
  210. *
  211. * Calls the memory-allocator function for the corresponding
  212. * buffer type. When no space is left, this function reduces the size and
  213. * tries to allocate again. The size actually allocated is stored in
  214. * res_size argument.
  215. *
  216. * Returns zero if the buffer with the given size is allocated successfuly,
  217. * other a negative value at error.
  218. */
  219. int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
  220. struct snd_dma_buffer *dmab)
  221. {
  222. int err;
  223. snd_assert(size > 0, return -ENXIO);
  224. snd_assert(dmab != NULL, return -ENXIO);
  225. while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
  226. if (err != -ENOMEM)
  227. return err;
  228. size >>= 1;
  229. if (size <= PAGE_SIZE)
  230. return -ENOMEM;
  231. }
  232. if (! dmab->area)
  233. return -ENOMEM;
  234. return 0;
  235. }
  236. /**
  237. * snd_dma_free_pages - release the allocated buffer
  238. * @dmab: the buffer allocation record to release
  239. *
  240. * Releases the allocated buffer via snd_dma_alloc_pages().
  241. */
  242. void snd_dma_free_pages(struct snd_dma_buffer *dmab)
  243. {
  244. switch (dmab->dev.type) {
  245. case SNDRV_DMA_TYPE_CONTINUOUS:
  246. snd_free_pages(dmab->area, dmab->bytes);
  247. break;
  248. #ifdef CONFIG_HAS_DMA
  249. case SNDRV_DMA_TYPE_DEV:
  250. snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
  251. break;
  252. case SNDRV_DMA_TYPE_DEV_SG:
  253. snd_free_sgbuf_pages(dmab);
  254. break;
  255. #endif
  256. default:
  257. printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
  258. }
  259. }
  260. /**
  261. * snd_dma_get_reserved - get the reserved buffer for the given device
  262. * @dmab: the buffer allocation record to store
  263. * @id: the buffer id
  264. *
  265. * Looks for the reserved-buffer list and re-uses if the same buffer
  266. * is found in the list. When the buffer is found, it's removed from the free list.
  267. *
  268. * Returns the size of buffer if the buffer is found, or zero if not found.
  269. */
  270. size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
  271. {
  272. struct snd_mem_list *mem;
  273. snd_assert(dmab, return 0);
  274. mutex_lock(&list_mutex);
  275. list_for_each_entry(mem, &mem_list_head, list) {
  276. if (mem->id == id &&
  277. (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
  278. ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
  279. struct device *dev = dmab->dev.dev;
  280. list_del(&mem->list);
  281. *dmab = mem->buffer;
  282. if (dmab->dev.dev == NULL)
  283. dmab->dev.dev = dev;
  284. kfree(mem);
  285. mutex_unlock(&list_mutex);
  286. return dmab->bytes;
  287. }
  288. }
  289. mutex_unlock(&list_mutex);
  290. return 0;
  291. }
  292. /**
  293. * snd_dma_reserve_buf - reserve the buffer
  294. * @dmab: the buffer to reserve
  295. * @id: the buffer id
  296. *
  297. * Reserves the given buffer as a reserved buffer.
  298. *
  299. * Returns zero if successful, or a negative code at error.
  300. */
  301. int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
  302. {
  303. struct snd_mem_list *mem;
  304. snd_assert(dmab, return -EINVAL);
  305. mem = kmalloc(sizeof(*mem), GFP_KERNEL);
  306. if (! mem)
  307. return -ENOMEM;
  308. mutex_lock(&list_mutex);
  309. mem->buffer = *dmab;
  310. mem->id = id;
  311. list_add_tail(&mem->list, &mem_list_head);
  312. mutex_unlock(&list_mutex);
  313. return 0;
  314. }
  315. /*
  316. * purge all reserved buffers
  317. */
  318. static void free_all_reserved_pages(void)
  319. {
  320. struct list_head *p;
  321. struct snd_mem_list *mem;
  322. mutex_lock(&list_mutex);
  323. while (! list_empty(&mem_list_head)) {
  324. p = mem_list_head.next;
  325. mem = list_entry(p, struct snd_mem_list, list);
  326. list_del(p);
  327. snd_dma_free_pages(&mem->buffer);
  328. kfree(mem);
  329. }
  330. mutex_unlock(&list_mutex);
  331. }
  332. #ifdef CONFIG_PROC_FS
  333. /*
  334. * proc file interface
  335. */
  336. #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
  337. static struct proc_dir_entry *snd_mem_proc;
  338. static int snd_mem_proc_read(struct seq_file *seq, void *offset)
  339. {
  340. long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
  341. struct snd_mem_list *mem;
  342. int devno;
  343. static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG" };
  344. mutex_lock(&list_mutex);
  345. seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
  346. pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
  347. devno = 0;
  348. list_for_each_entry(mem, &mem_list_head, list) {
  349. devno++;
  350. seq_printf(seq, "buffer %d : ID %08x : type %s\n",
  351. devno, mem->id, types[mem->buffer.dev.type]);
  352. seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
  353. (unsigned long)mem->buffer.addr,
  354. (int)mem->buffer.bytes);
  355. }
  356. mutex_unlock(&list_mutex);
  357. return 0;
  358. }
  359. static int snd_mem_proc_open(struct inode *inode, struct file *file)
  360. {
  361. return single_open(file, snd_mem_proc_read, NULL);
  362. }
  363. /* FIXME: for pci only - other bus? */
  364. #ifdef CONFIG_PCI
  365. #define gettoken(bufp) strsep(bufp, " \t\n")
  366. static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
  367. size_t count, loff_t * ppos)
  368. {
  369. char buf[128];
  370. char *token, *p;
  371. if (count > sizeof(buf) - 1)
  372. return -EINVAL;
  373. if (copy_from_user(buf, buffer, count))
  374. return -EFAULT;
  375. buf[count] = '\0';
  376. p = buf;
  377. token = gettoken(&p);
  378. if (! token || *token == '#')
  379. return count;
  380. if (strcmp(token, "add") == 0) {
  381. char *endp;
  382. int vendor, device, size, buffers;
  383. long mask;
  384. int i, alloced;
  385. struct pci_dev *pci;
  386. if ((token = gettoken(&p)) == NULL ||
  387. (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
  388. (token = gettoken(&p)) == NULL ||
  389. (device = simple_strtol(token, NULL, 0)) <= 0 ||
  390. (token = gettoken(&p)) == NULL ||
  391. (mask = simple_strtol(token, NULL, 0)) < 0 ||
  392. (token = gettoken(&p)) == NULL ||
  393. (size = memparse(token, &endp)) < 64*1024 ||
  394. size > 16*1024*1024 /* too big */ ||
  395. (token = gettoken(&p)) == NULL ||
  396. (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
  397. buffers > 4) {
  398. printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
  399. return count;
  400. }
  401. vendor &= 0xffff;
  402. device &= 0xffff;
  403. alloced = 0;
  404. pci = NULL;
  405. while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
  406. if (mask > 0 && mask < 0xffffffff) {
  407. if (pci_set_dma_mask(pci, mask) < 0 ||
  408. pci_set_consistent_dma_mask(pci, mask) < 0) {
  409. printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
  410. pci_dev_put(pci);
  411. return count;
  412. }
  413. }
  414. for (i = 0; i < buffers; i++) {
  415. struct snd_dma_buffer dmab;
  416. memset(&dmab, 0, sizeof(dmab));
  417. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  418. size, &dmab) < 0) {
  419. printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
  420. pci_dev_put(pci);
  421. return count;
  422. }
  423. snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
  424. }
  425. alloced++;
  426. }
  427. if (! alloced) {
  428. for (i = 0; i < buffers; i++) {
  429. struct snd_dma_buffer dmab;
  430. memset(&dmab, 0, sizeof(dmab));
  431. /* FIXME: We can allocate only in ZONE_DMA
  432. * without a device pointer!
  433. */
  434. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
  435. size, &dmab) < 0) {
  436. printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
  437. break;
  438. }
  439. snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
  440. }
  441. }
  442. } else if (strcmp(token, "erase") == 0)
  443. /* FIXME: need for releasing each buffer chunk? */
  444. free_all_reserved_pages();
  445. else
  446. printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
  447. return count;
  448. }
  449. #endif /* CONFIG_PCI */
  450. static const struct file_operations snd_mem_proc_fops = {
  451. .owner = THIS_MODULE,
  452. .open = snd_mem_proc_open,
  453. .read = seq_read,
  454. #ifdef CONFIG_PCI
  455. .write = snd_mem_proc_write,
  456. #endif
  457. .llseek = seq_lseek,
  458. .release = single_release,
  459. };
  460. #endif /* CONFIG_PROC_FS */
  461. /*
  462. * module entry
  463. */
  464. static int __init snd_mem_init(void)
  465. {
  466. #ifdef CONFIG_PROC_FS
  467. snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
  468. &snd_mem_proc_fops);
  469. #endif
  470. return 0;
  471. }
  472. static void __exit snd_mem_exit(void)
  473. {
  474. remove_proc_entry(SND_MEM_PROC_FILE, NULL);
  475. free_all_reserved_pages();
  476. if (snd_allocated_pages > 0)
  477. printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
  478. }
  479. module_init(snd_mem_init)
  480. module_exit(snd_mem_exit)
  481. /*
  482. * exports
  483. */
  484. EXPORT_SYMBOL(snd_dma_alloc_pages);
  485. EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
  486. EXPORT_SYMBOL(snd_dma_free_pages);
  487. EXPORT_SYMBOL(snd_dma_get_reserved_buf);
  488. EXPORT_SYMBOL(snd_dma_reserve_buf);
  489. EXPORT_SYMBOL(snd_malloc_pages);
  490. EXPORT_SYMBOL(snd_free_pages);