memalloc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr);
  168. if (res != NULL)
  169. inc_snd_pages(pg);
  170. return res;
  171. }
  172. static void snd_free_sbus_pages(struct device *dev, size_t size,
  173. void *ptr, dma_addr_t dma_addr)
  174. {
  175. struct sbus_dev *sdev = (struct sbus_dev *)dev;
  176. int pg;
  177. if (ptr == NULL)
  178. return;
  179. pg = get_order(size);
  180. dec_snd_pages(pg);
  181. sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
  182. }
  183. #endif /* CONFIG_SBUS */
  184. /*
  185. *
  186. * ALSA generic memory management
  187. *
  188. */
  189. /**
  190. * snd_dma_alloc_pages - allocate the buffer area according to the given type
  191. * @type: the DMA buffer type
  192. * @device: the device pointer
  193. * @size: the buffer size to allocate
  194. * @dmab: buffer allocation record to store the allocated data
  195. *
  196. * Calls the memory-allocator function for the corresponding
  197. * buffer type.
  198. *
  199. * Returns zero if the buffer with the given size is allocated successfuly,
  200. * other a negative value at error.
  201. */
  202. int snd_dma_alloc_pages(int type, struct device *device, size_t size,
  203. struct snd_dma_buffer *dmab)
  204. {
  205. snd_assert(size > 0, return -ENXIO);
  206. snd_assert(dmab != NULL, return -ENXIO);
  207. dmab->dev.type = type;
  208. dmab->dev.dev = device;
  209. dmab->bytes = 0;
  210. switch (type) {
  211. case SNDRV_DMA_TYPE_CONTINUOUS:
  212. dmab->area = snd_malloc_pages(size, (unsigned long)device);
  213. dmab->addr = 0;
  214. break;
  215. #ifdef CONFIG_SBUS
  216. case SNDRV_DMA_TYPE_SBUS:
  217. dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr);
  218. break;
  219. #endif
  220. #ifdef CONFIG_HAS_DMA
  221. case SNDRV_DMA_TYPE_DEV:
  222. dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
  223. break;
  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. * Returns zero if the buffer with the given size is allocated successfuly,
  252. * other a negative value at 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. snd_assert(size > 0, return -ENXIO);
  259. snd_assert(dmab != NULL, return -ENXIO);
  260. while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
  261. if (err != -ENOMEM)
  262. return err;
  263. size >>= 1;
  264. if (size <= PAGE_SIZE)
  265. return -ENOMEM;
  266. }
  267. if (! dmab->area)
  268. return -ENOMEM;
  269. return 0;
  270. }
  271. /**
  272. * snd_dma_free_pages - release the allocated buffer
  273. * @dmab: the buffer allocation record to release
  274. *
  275. * Releases the allocated buffer via snd_dma_alloc_pages().
  276. */
  277. void snd_dma_free_pages(struct snd_dma_buffer *dmab)
  278. {
  279. switch (dmab->dev.type) {
  280. case SNDRV_DMA_TYPE_CONTINUOUS:
  281. snd_free_pages(dmab->area, dmab->bytes);
  282. break;
  283. #ifdef CONFIG_SBUS
  284. case SNDRV_DMA_TYPE_SBUS:
  285. snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
  286. break;
  287. #endif
  288. #ifdef CONFIG_HAS_DMA
  289. case SNDRV_DMA_TYPE_DEV:
  290. snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
  291. break;
  292. case SNDRV_DMA_TYPE_DEV_SG:
  293. snd_free_sgbuf_pages(dmab);
  294. break;
  295. #endif
  296. default:
  297. printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
  298. }
  299. }
  300. /**
  301. * snd_dma_get_reserved - get the reserved buffer for the given device
  302. * @dmab: the buffer allocation record to store
  303. * @id: the buffer id
  304. *
  305. * Looks for the reserved-buffer list and re-uses if the same buffer
  306. * is found in the list. When the buffer is found, it's removed from the free list.
  307. *
  308. * Returns the size of buffer if the buffer is found, or zero if not found.
  309. */
  310. size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
  311. {
  312. struct snd_mem_list *mem;
  313. snd_assert(dmab, return 0);
  314. mutex_lock(&list_mutex);
  315. list_for_each_entry(mem, &mem_list_head, list) {
  316. if (mem->id == id &&
  317. (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
  318. ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
  319. struct device *dev = dmab->dev.dev;
  320. list_del(&mem->list);
  321. *dmab = mem->buffer;
  322. if (dmab->dev.dev == NULL)
  323. dmab->dev.dev = dev;
  324. kfree(mem);
  325. mutex_unlock(&list_mutex);
  326. return dmab->bytes;
  327. }
  328. }
  329. mutex_unlock(&list_mutex);
  330. return 0;
  331. }
  332. /**
  333. * snd_dma_reserve_buf - reserve the buffer
  334. * @dmab: the buffer to reserve
  335. * @id: the buffer id
  336. *
  337. * Reserves the given buffer as a reserved buffer.
  338. *
  339. * Returns zero if successful, or a negative code at error.
  340. */
  341. int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
  342. {
  343. struct snd_mem_list *mem;
  344. snd_assert(dmab, return -EINVAL);
  345. mem = kmalloc(sizeof(*mem), GFP_KERNEL);
  346. if (! mem)
  347. return -ENOMEM;
  348. mutex_lock(&list_mutex);
  349. mem->buffer = *dmab;
  350. mem->id = id;
  351. list_add_tail(&mem->list, &mem_list_head);
  352. mutex_unlock(&list_mutex);
  353. return 0;
  354. }
  355. /*
  356. * purge all reserved buffers
  357. */
  358. static void free_all_reserved_pages(void)
  359. {
  360. struct list_head *p;
  361. struct snd_mem_list *mem;
  362. mutex_lock(&list_mutex);
  363. while (! list_empty(&mem_list_head)) {
  364. p = mem_list_head.next;
  365. mem = list_entry(p, struct snd_mem_list, list);
  366. list_del(p);
  367. snd_dma_free_pages(&mem->buffer);
  368. kfree(mem);
  369. }
  370. mutex_unlock(&list_mutex);
  371. }
  372. #ifdef CONFIG_PROC_FS
  373. /*
  374. * proc file interface
  375. */
  376. #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
  377. static struct proc_dir_entry *snd_mem_proc;
  378. static int snd_mem_proc_read(struct seq_file *seq, void *offset)
  379. {
  380. long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
  381. struct snd_mem_list *mem;
  382. int devno;
  383. static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
  384. mutex_lock(&list_mutex);
  385. seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
  386. pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
  387. devno = 0;
  388. list_for_each_entry(mem, &mem_list_head, list) {
  389. devno++;
  390. seq_printf(seq, "buffer %d : ID %08x : type %s\n",
  391. devno, mem->id, types[mem->buffer.dev.type]);
  392. seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
  393. (unsigned long)mem->buffer.addr,
  394. (int)mem->buffer.bytes);
  395. }
  396. mutex_unlock(&list_mutex);
  397. return 0;
  398. }
  399. static int snd_mem_proc_open(struct inode *inode, struct file *file)
  400. {
  401. return single_open(file, snd_mem_proc_read, NULL);
  402. }
  403. /* FIXME: for pci only - other bus? */
  404. #ifdef CONFIG_PCI
  405. #define gettoken(bufp) strsep(bufp, " \t\n")
  406. static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
  407. size_t count, loff_t * ppos)
  408. {
  409. char buf[128];
  410. char *token, *p;
  411. if (count > sizeof(buf) - 1)
  412. return -EINVAL;
  413. if (copy_from_user(buf, buffer, count))
  414. return -EFAULT;
  415. buf[count] = '\0';
  416. p = buf;
  417. token = gettoken(&p);
  418. if (! token || *token == '#')
  419. return count;
  420. if (strcmp(token, "add") == 0) {
  421. char *endp;
  422. int vendor, device, size, buffers;
  423. long mask;
  424. int i, alloced;
  425. struct pci_dev *pci;
  426. if ((token = gettoken(&p)) == NULL ||
  427. (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
  428. (token = gettoken(&p)) == NULL ||
  429. (device = simple_strtol(token, NULL, 0)) <= 0 ||
  430. (token = gettoken(&p)) == NULL ||
  431. (mask = simple_strtol(token, NULL, 0)) < 0 ||
  432. (token = gettoken(&p)) == NULL ||
  433. (size = memparse(token, &endp)) < 64*1024 ||
  434. size > 16*1024*1024 /* too big */ ||
  435. (token = gettoken(&p)) == NULL ||
  436. (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
  437. buffers > 4) {
  438. printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
  439. return count;
  440. }
  441. vendor &= 0xffff;
  442. device &= 0xffff;
  443. alloced = 0;
  444. pci = NULL;
  445. while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
  446. if (mask > 0 && mask < 0xffffffff) {
  447. if (pci_set_dma_mask(pci, mask) < 0 ||
  448. pci_set_consistent_dma_mask(pci, mask) < 0) {
  449. printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
  450. pci_dev_put(pci);
  451. return count;
  452. }
  453. }
  454. for (i = 0; i < buffers; i++) {
  455. struct snd_dma_buffer dmab;
  456. memset(&dmab, 0, sizeof(dmab));
  457. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  458. size, &dmab) < 0) {
  459. printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
  460. pci_dev_put(pci);
  461. return count;
  462. }
  463. snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
  464. }
  465. alloced++;
  466. }
  467. if (! alloced) {
  468. for (i = 0; i < buffers; i++) {
  469. struct snd_dma_buffer dmab;
  470. memset(&dmab, 0, sizeof(dmab));
  471. /* FIXME: We can allocate only in ZONE_DMA
  472. * without a device pointer!
  473. */
  474. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
  475. size, &dmab) < 0) {
  476. printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
  477. break;
  478. }
  479. snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
  480. }
  481. }
  482. } else if (strcmp(token, "erase") == 0)
  483. /* FIXME: need for releasing each buffer chunk? */
  484. free_all_reserved_pages();
  485. else
  486. printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
  487. return count;
  488. }
  489. #endif /* CONFIG_PCI */
  490. static const struct file_operations snd_mem_proc_fops = {
  491. .owner = THIS_MODULE,
  492. .open = snd_mem_proc_open,
  493. .read = seq_read,
  494. #ifdef CONFIG_PCI
  495. .write = snd_mem_proc_write,
  496. #endif
  497. .llseek = seq_lseek,
  498. .release = single_release,
  499. };
  500. #endif /* CONFIG_PROC_FS */
  501. /*
  502. * module entry
  503. */
  504. static int __init snd_mem_init(void)
  505. {
  506. #ifdef CONFIG_PROC_FS
  507. snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
  508. &snd_mem_proc_fops);
  509. #endif
  510. return 0;
  511. }
  512. static void __exit snd_mem_exit(void)
  513. {
  514. remove_proc_entry(SND_MEM_PROC_FILE, NULL);
  515. free_all_reserved_pages();
  516. if (snd_allocated_pages > 0)
  517. printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
  518. }
  519. module_init(snd_mem_init)
  520. module_exit(snd_mem_exit)
  521. /*
  522. * exports
  523. */
  524. EXPORT_SYMBOL(snd_dma_alloc_pages);
  525. EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
  526. EXPORT_SYMBOL(snd_dma_free_pages);
  527. EXPORT_SYMBOL(snd_dma_get_reserved_buf);
  528. EXPORT_SYMBOL(snd_dma_reserve_buf);
  529. EXPORT_SYMBOL(snd_malloc_pages);
  530. EXPORT_SYMBOL(snd_free_pages);