memory.c 16 KB

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