memory.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  4. *
  5. * EMU10K1 memory page allocation (PTB area)
  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/pci.h>
  24. #include <linux/gfp.h>
  25. #include <linux/time.h>
  26. #include <linux/mutex.h>
  27. #include <linux/export.h>
  28. #include <sound/core.h>
  29. #include <sound/emu10k1.h>
  30. /* page arguments of these two macros are Emu page (4096 bytes), not like
  31. * aligned pages in others
  32. */
  33. #define __set_ptb_entry(emu,page,addr) \
  34. (((u32 *)(emu)->ptb_pages.area)[page] = cpu_to_le32(((addr) << 1) | (page)))
  35. #define UNIT_PAGES (PAGE_SIZE / EMUPAGESIZE)
  36. #define MAX_ALIGN_PAGES (MAXPAGES / UNIT_PAGES)
  37. /* get aligned page from offset address */
  38. #define get_aligned_page(offset) ((offset) >> PAGE_SHIFT)
  39. /* get offset address from aligned page */
  40. #define aligned_page_offset(page) ((page) << PAGE_SHIFT)
  41. #if PAGE_SIZE == 4096
  42. /* page size == EMUPAGESIZE */
  43. /* fill PTB entrie(s) corresponding to page with addr */
  44. #define set_ptb_entry(emu,page,addr) __set_ptb_entry(emu,page,addr)
  45. /* fill PTB entrie(s) corresponding to page with silence pointer */
  46. #define set_silent_ptb(emu,page) __set_ptb_entry(emu,page,emu->silent_page.addr)
  47. #else
  48. /* fill PTB entries -- we need to fill UNIT_PAGES entries */
  49. static inline void set_ptb_entry(struct snd_emu10k1 *emu, int page, dma_addr_t addr)
  50. {
  51. int i;
  52. page *= UNIT_PAGES;
  53. for (i = 0; i < UNIT_PAGES; i++, page++) {
  54. __set_ptb_entry(emu, page, addr);
  55. addr += EMUPAGESIZE;
  56. }
  57. }
  58. static inline void set_silent_ptb(struct snd_emu10k1 *emu, int page)
  59. {
  60. int i;
  61. page *= UNIT_PAGES;
  62. for (i = 0; i < UNIT_PAGES; i++, page++)
  63. /* do not increment ptr */
  64. __set_ptb_entry(emu, page, emu->silent_page.addr);
  65. }
  66. #endif /* PAGE_SIZE */
  67. /*
  68. */
  69. static int synth_alloc_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk);
  70. static int synth_free_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk);
  71. #define get_emu10k1_memblk(l,member) list_entry(l, struct snd_emu10k1_memblk, member)
  72. /* initialize emu10k1 part */
  73. static void emu10k1_memblk_init(struct snd_emu10k1_memblk *blk)
  74. {
  75. blk->mapped_page = -1;
  76. INIT_LIST_HEAD(&blk->mapped_link);
  77. INIT_LIST_HEAD(&blk->mapped_order_link);
  78. blk->map_locked = 0;
  79. blk->first_page = get_aligned_page(blk->mem.offset);
  80. blk->last_page = get_aligned_page(blk->mem.offset + blk->mem.size - 1);
  81. blk->pages = blk->last_page - blk->first_page + 1;
  82. }
  83. /*
  84. * search empty region on PTB with the given size
  85. *
  86. * if an empty region is found, return the page and store the next mapped block
  87. * in nextp
  88. * if not found, return a negative error code.
  89. */
  90. static int search_empty_map_area(struct snd_emu10k1 *emu, int npages, struct list_head **nextp)
  91. {
  92. int page = 0, found_page = -ENOMEM;
  93. int max_size = npages;
  94. int size;
  95. struct list_head *candidate = &emu->mapped_link_head;
  96. struct list_head *pos;
  97. list_for_each (pos, &emu->mapped_link_head) {
  98. struct snd_emu10k1_memblk *blk = get_emu10k1_memblk(pos, mapped_link);
  99. if (blk->mapped_page < 0)
  100. continue;
  101. size = blk->mapped_page - page;
  102. if (size == npages) {
  103. *nextp = pos;
  104. return page;
  105. }
  106. else if (size > max_size) {
  107. /* we look for the maximum empty hole */
  108. max_size = size;
  109. candidate = pos;
  110. found_page = page;
  111. }
  112. page = blk->mapped_page + blk->pages;
  113. }
  114. size = MAX_ALIGN_PAGES - page;
  115. if (size >= max_size) {
  116. *nextp = pos;
  117. return page;
  118. }
  119. *nextp = candidate;
  120. return found_page;
  121. }
  122. /*
  123. * map a memory block onto emu10k1's PTB
  124. *
  125. * call with memblk_lock held
  126. */
  127. static int map_memblk(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
  128. {
  129. int page, pg;
  130. struct list_head *next;
  131. page = search_empty_map_area(emu, blk->pages, &next);
  132. if (page < 0) /* not found */
  133. return page;
  134. /* insert this block in the proper position of mapped list */
  135. list_add_tail(&blk->mapped_link, next);
  136. /* append this as a newest block in order list */
  137. list_add_tail(&blk->mapped_order_link, &emu->mapped_order_link_head);
  138. blk->mapped_page = page;
  139. /* fill PTB */
  140. for (pg = blk->first_page; pg <= blk->last_page; pg++) {
  141. set_ptb_entry(emu, page, emu->page_addr_table[pg]);
  142. page++;
  143. }
  144. return 0;
  145. }
  146. /*
  147. * unmap the block
  148. * return the size of resultant empty pages
  149. *
  150. * call with memblk_lock held
  151. */
  152. static int unmap_memblk(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
  153. {
  154. int start_page, end_page, mpage, pg;
  155. struct list_head *p;
  156. struct snd_emu10k1_memblk *q;
  157. /* calculate the expected size of empty region */
  158. if ((p = blk->mapped_link.prev) != &emu->mapped_link_head) {
  159. q = get_emu10k1_memblk(p, mapped_link);
  160. start_page = q->mapped_page + q->pages;
  161. } else
  162. start_page = 0;
  163. if ((p = blk->mapped_link.next) != &emu->mapped_link_head) {
  164. q = get_emu10k1_memblk(p, mapped_link);
  165. end_page = q->mapped_page;
  166. } else
  167. end_page = MAX_ALIGN_PAGES;
  168. /* remove links */
  169. list_del(&blk->mapped_link);
  170. list_del(&blk->mapped_order_link);
  171. /* clear PTB */
  172. mpage = blk->mapped_page;
  173. for (pg = blk->first_page; pg <= blk->last_page; pg++) {
  174. set_silent_ptb(emu, mpage);
  175. mpage++;
  176. }
  177. blk->mapped_page = -1;
  178. return end_page - start_page; /* return the new empty size */
  179. }
  180. /*
  181. * search empty pages with the given size, and create a memory block
  182. *
  183. * unlike synth_alloc the memory block is aligned to the page start
  184. */
  185. static struct snd_emu10k1_memblk *
  186. search_empty(struct snd_emu10k1 *emu, int size)
  187. {
  188. struct list_head *p;
  189. struct snd_emu10k1_memblk *blk;
  190. int page, psize;
  191. psize = get_aligned_page(size + PAGE_SIZE -1);
  192. page = 0;
  193. list_for_each(p, &emu->memhdr->block) {
  194. blk = get_emu10k1_memblk(p, mem.list);
  195. if (page + psize <= blk->first_page)
  196. goto __found_pages;
  197. page = blk->last_page + 1;
  198. }
  199. if (page + psize > emu->max_cache_pages)
  200. return NULL;
  201. __found_pages:
  202. /* create a new memory block */
  203. blk = (struct snd_emu10k1_memblk *)__snd_util_memblk_new(emu->memhdr, psize << PAGE_SHIFT, p->prev);
  204. if (blk == NULL)
  205. return NULL;
  206. blk->mem.offset = aligned_page_offset(page); /* set aligned offset */
  207. emu10k1_memblk_init(blk);
  208. return blk;
  209. }
  210. /*
  211. * check if the given pointer is valid for pages
  212. */
  213. static int is_valid_page(struct snd_emu10k1 *emu, dma_addr_t addr)
  214. {
  215. if (addr & ~emu->dma_mask) {
  216. dev_err(emu->card->dev,
  217. "max memory size is 0x%lx (addr = 0x%lx)!!\n",
  218. emu->dma_mask, (unsigned long)addr);
  219. return 0;
  220. }
  221. if (addr & (EMUPAGESIZE-1)) {
  222. dev_err(emu->card->dev, "page is not aligned\n");
  223. return 0;
  224. }
  225. return 1;
  226. }
  227. /*
  228. * map the given memory block on PTB.
  229. * if the block is already mapped, update the link order.
  230. * if no empty pages are found, tries to release unused memory blocks
  231. * and retry the mapping.
  232. */
  233. int snd_emu10k1_memblk_map(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
  234. {
  235. int err;
  236. int size;
  237. struct list_head *p, *nextp;
  238. struct snd_emu10k1_memblk *deleted;
  239. unsigned long flags;
  240. spin_lock_irqsave(&emu->memblk_lock, flags);
  241. if (blk->mapped_page >= 0) {
  242. /* update order link */
  243. list_move_tail(&blk->mapped_order_link,
  244. &emu->mapped_order_link_head);
  245. spin_unlock_irqrestore(&emu->memblk_lock, flags);
  246. return 0;
  247. }
  248. if ((err = map_memblk(emu, blk)) < 0) {
  249. /* no enough page - try to unmap some blocks */
  250. /* starting from the oldest block */
  251. p = emu->mapped_order_link_head.next;
  252. for (; p != &emu->mapped_order_link_head; p = nextp) {
  253. nextp = p->next;
  254. deleted = get_emu10k1_memblk(p, mapped_order_link);
  255. if (deleted->map_locked)
  256. continue;
  257. size = unmap_memblk(emu, deleted);
  258. if (size >= blk->pages) {
  259. /* ok the empty region is enough large */
  260. err = map_memblk(emu, blk);
  261. break;
  262. }
  263. }
  264. }
  265. spin_unlock_irqrestore(&emu->memblk_lock, flags);
  266. return err;
  267. }
  268. EXPORT_SYMBOL(snd_emu10k1_memblk_map);
  269. /*
  270. * page allocation for DMA
  271. */
  272. struct snd_util_memblk *
  273. snd_emu10k1_alloc_pages(struct snd_emu10k1 *emu, struct snd_pcm_substream *substream)
  274. {
  275. struct snd_pcm_runtime *runtime = substream->runtime;
  276. struct snd_util_memhdr *hdr;
  277. struct snd_emu10k1_memblk *blk;
  278. int page, err, idx;
  279. if (snd_BUG_ON(!emu))
  280. return NULL;
  281. if (snd_BUG_ON(runtime->dma_bytes <= 0 ||
  282. runtime->dma_bytes >= MAXPAGES * EMUPAGESIZE))
  283. return NULL;
  284. hdr = emu->memhdr;
  285. if (snd_BUG_ON(!hdr))
  286. return NULL;
  287. idx = runtime->period_size >= runtime->buffer_size ?
  288. (emu->delay_pcm_irq * 2) : 0;
  289. mutex_lock(&hdr->block_mutex);
  290. blk = search_empty(emu, runtime->dma_bytes + idx);
  291. if (blk == NULL) {
  292. mutex_unlock(&hdr->block_mutex);
  293. return NULL;
  294. }
  295. /* fill buffer addresses but pointers are not stored so that
  296. * snd_free_pci_page() is not called in in synth_free()
  297. */
  298. idx = 0;
  299. for (page = blk->first_page; page <= blk->last_page; page++, idx++) {
  300. unsigned long ofs = idx << PAGE_SHIFT;
  301. dma_addr_t addr;
  302. if (ofs >= runtime->dma_bytes)
  303. addr = emu->silent_page.addr;
  304. else
  305. addr = snd_pcm_sgbuf_get_addr(substream, ofs);
  306. if (! is_valid_page(emu, addr)) {
  307. dev_err(emu->card->dev,
  308. "emu: failure page = %d\n", idx);
  309. mutex_unlock(&hdr->block_mutex);
  310. return NULL;
  311. }
  312. emu->page_addr_table[page] = addr;
  313. emu->page_ptr_table[page] = NULL;
  314. }
  315. /* set PTB entries */
  316. blk->map_locked = 1; /* do not unmap this block! */
  317. err = snd_emu10k1_memblk_map(emu, blk);
  318. if (err < 0) {
  319. __snd_util_mem_free(hdr, (struct snd_util_memblk *)blk);
  320. mutex_unlock(&hdr->block_mutex);
  321. return NULL;
  322. }
  323. mutex_unlock(&hdr->block_mutex);
  324. return (struct snd_util_memblk *)blk;
  325. }
  326. /*
  327. * release DMA buffer from page table
  328. */
  329. int snd_emu10k1_free_pages(struct snd_emu10k1 *emu, struct snd_util_memblk *blk)
  330. {
  331. if (snd_BUG_ON(!emu || !blk))
  332. return -EINVAL;
  333. return snd_emu10k1_synth_free(emu, blk);
  334. }
  335. /*
  336. * memory allocation using multiple pages (for synth)
  337. * Unlike the DMA allocation above, non-contiguous pages are assined.
  338. */
  339. /*
  340. * allocate a synth sample area
  341. */
  342. struct snd_util_memblk *
  343. snd_emu10k1_synth_alloc(struct snd_emu10k1 *hw, unsigned int size)
  344. {
  345. struct snd_emu10k1_memblk *blk;
  346. struct snd_util_memhdr *hdr = hw->memhdr;
  347. mutex_lock(&hdr->block_mutex);
  348. blk = (struct snd_emu10k1_memblk *)__snd_util_mem_alloc(hdr, size);
  349. if (blk == NULL) {
  350. mutex_unlock(&hdr->block_mutex);
  351. return NULL;
  352. }
  353. if (synth_alloc_pages(hw, blk)) {
  354. __snd_util_mem_free(hdr, (struct snd_util_memblk *)blk);
  355. mutex_unlock(&hdr->block_mutex);
  356. return NULL;
  357. }
  358. snd_emu10k1_memblk_map(hw, blk);
  359. mutex_unlock(&hdr->block_mutex);
  360. return (struct snd_util_memblk *)blk;
  361. }
  362. EXPORT_SYMBOL(snd_emu10k1_synth_alloc);
  363. /*
  364. * free a synth sample area
  365. */
  366. int
  367. snd_emu10k1_synth_free(struct snd_emu10k1 *emu, struct snd_util_memblk *memblk)
  368. {
  369. struct snd_util_memhdr *hdr = emu->memhdr;
  370. struct snd_emu10k1_memblk *blk = (struct snd_emu10k1_memblk *)memblk;
  371. unsigned long flags;
  372. mutex_lock(&hdr->block_mutex);
  373. spin_lock_irqsave(&emu->memblk_lock, flags);
  374. if (blk->mapped_page >= 0)
  375. unmap_memblk(emu, blk);
  376. spin_unlock_irqrestore(&emu->memblk_lock, flags);
  377. synth_free_pages(emu, blk);
  378. __snd_util_mem_free(hdr, memblk);
  379. mutex_unlock(&hdr->block_mutex);
  380. return 0;
  381. }
  382. EXPORT_SYMBOL(snd_emu10k1_synth_free);
  383. /* check new allocation range */
  384. static void get_single_page_range(struct snd_util_memhdr *hdr,
  385. struct snd_emu10k1_memblk *blk,
  386. int *first_page_ret, int *last_page_ret)
  387. {
  388. struct list_head *p;
  389. struct snd_emu10k1_memblk *q;
  390. int first_page, last_page;
  391. first_page = blk->first_page;
  392. if ((p = blk->mem.list.prev) != &hdr->block) {
  393. q = get_emu10k1_memblk(p, mem.list);
  394. if (q->last_page == first_page)
  395. first_page++; /* first page was already allocated */
  396. }
  397. last_page = blk->last_page;
  398. if ((p = blk->mem.list.next) != &hdr->block) {
  399. q = get_emu10k1_memblk(p, mem.list);
  400. if (q->first_page == last_page)
  401. last_page--; /* last page was already allocated */
  402. }
  403. *first_page_ret = first_page;
  404. *last_page_ret = last_page;
  405. }
  406. /* release allocated pages */
  407. static void __synth_free_pages(struct snd_emu10k1 *emu, int first_page,
  408. int last_page)
  409. {
  410. int page;
  411. for (page = first_page; page <= last_page; page++) {
  412. free_page((unsigned long)emu->page_ptr_table[page]);
  413. emu->page_addr_table[page] = 0;
  414. emu->page_ptr_table[page] = NULL;
  415. }
  416. }
  417. /*
  418. * allocate kernel pages
  419. */
  420. static int synth_alloc_pages(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
  421. {
  422. int page, first_page, last_page;
  423. emu10k1_memblk_init(blk);
  424. get_single_page_range(emu->memhdr, blk, &first_page, &last_page);
  425. /* allocate kernel pages */
  426. for (page = first_page; page <= last_page; page++) {
  427. /* first try to allocate from <4GB zone */
  428. struct page *p = alloc_page(GFP_KERNEL | GFP_DMA32 |
  429. __GFP_NOWARN);
  430. if (!p || (page_to_pfn(p) & ~(emu->dma_mask >> PAGE_SHIFT))) {
  431. if (p)
  432. __free_page(p);
  433. /* try to allocate from <16MB zone */
  434. p = alloc_page(GFP_ATOMIC | GFP_DMA |
  435. __GFP_NORETRY | /* no OOM-killer */
  436. __GFP_NOWARN);
  437. }
  438. if (!p) {
  439. __synth_free_pages(emu, first_page, page - 1);
  440. return -ENOMEM;
  441. }
  442. emu->page_addr_table[page] = page_to_phys(p);
  443. emu->page_ptr_table[page] = page_address(p);
  444. }
  445. return 0;
  446. }
  447. /*
  448. * free pages
  449. */
  450. static int synth_free_pages(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
  451. {
  452. int first_page, last_page;
  453. get_single_page_range(emu->memhdr, blk, &first_page, &last_page);
  454. __synth_free_pages(emu, first_page, last_page);
  455. return 0;
  456. }
  457. /* calculate buffer pointer from offset address */
  458. static inline void *offset_ptr(struct snd_emu10k1 *emu, int page, int offset)
  459. {
  460. char *ptr;
  461. if (snd_BUG_ON(page < 0 || page >= emu->max_cache_pages))
  462. return NULL;
  463. ptr = emu->page_ptr_table[page];
  464. if (! ptr) {
  465. dev_err(emu->card->dev,
  466. "access to NULL ptr: page = %d\n", page);
  467. return NULL;
  468. }
  469. ptr += offset & (PAGE_SIZE - 1);
  470. return (void*)ptr;
  471. }
  472. /*
  473. * bzero(blk + offset, size)
  474. */
  475. int snd_emu10k1_synth_bzero(struct snd_emu10k1 *emu, struct snd_util_memblk *blk,
  476. int offset, int size)
  477. {
  478. int page, nextofs, end_offset, temp, temp1;
  479. void *ptr;
  480. struct snd_emu10k1_memblk *p = (struct snd_emu10k1_memblk *)blk;
  481. offset += blk->offset & (PAGE_SIZE - 1);
  482. end_offset = offset + size;
  483. page = get_aligned_page(offset);
  484. do {
  485. nextofs = aligned_page_offset(page + 1);
  486. temp = nextofs - offset;
  487. temp1 = end_offset - offset;
  488. if (temp1 < temp)
  489. temp = temp1;
  490. ptr = offset_ptr(emu, page + p->first_page, offset);
  491. if (ptr)
  492. memset(ptr, 0, temp);
  493. offset = nextofs;
  494. page++;
  495. } while (offset < end_offset);
  496. return 0;
  497. }
  498. EXPORT_SYMBOL(snd_emu10k1_synth_bzero);
  499. /*
  500. * copy_from_user(blk + offset, data, size)
  501. */
  502. int snd_emu10k1_synth_copy_from_user(struct snd_emu10k1 *emu, struct snd_util_memblk *blk,
  503. int offset, const char __user *data, int size)
  504. {
  505. int page, nextofs, end_offset, temp, temp1;
  506. void *ptr;
  507. struct snd_emu10k1_memblk *p = (struct snd_emu10k1_memblk *)blk;
  508. offset += blk->offset & (PAGE_SIZE - 1);
  509. end_offset = offset + size;
  510. page = get_aligned_page(offset);
  511. do {
  512. nextofs = aligned_page_offset(page + 1);
  513. temp = nextofs - offset;
  514. temp1 = end_offset - offset;
  515. if (temp1 < temp)
  516. temp = temp1;
  517. ptr = offset_ptr(emu, page + p->first_page, offset);
  518. if (ptr && copy_from_user(ptr, data, temp))
  519. return -EFAULT;
  520. offset = nextofs;
  521. data += temp;
  522. page++;
  523. } while (offset < end_offset);
  524. return 0;
  525. }
  526. EXPORT_SYMBOL(snd_emu10k1_synth_copy_from_user);