memalloc.c 15 KB

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