memalloc.c 13 KB

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