memalloc.c 14 KB

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