memalloc.c 14 KB

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