memalloc.c 15 KB

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