memory.c 15 KB

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