memory.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) by Jaroslav Kysela <[email protected]>
  4. * Copyright (c) by Takashi Iwai <[email protected]>
  5. *
  6. * EMU10K1 memory page allocation (PTB area)
  7. */
  8. #include <linux/pci.h>
  9. #include <linux/gfp.h>
  10. #include <linux/time.h>
  11. #include <linux/mutex.h>
  12. #include <linux/export.h>
  13. #include <sound/core.h>
  14. #include <sound/emu10k1.h>
  15. /* page arguments of these two macros are Emu page (4096 bytes), not like
  16. * aligned pages in others
  17. */
  18. #define __set_ptb_entry(emu,page,addr) \
  19. (((__le32 *)(emu)->ptb_pages.area)[page] = \
  20. cpu_to_le32(((addr) << (emu->address_mode)) | (page)))
  21. #define __get_ptb_entry(emu, page) \
  22. (le32_to_cpu(((__le32 *)(emu)->ptb_pages.area)[page]))
  23. #define UNIT_PAGES (PAGE_SIZE / EMUPAGESIZE)
  24. #define MAX_ALIGN_PAGES0 (MAXPAGES0 / UNIT_PAGES)
  25. #define MAX_ALIGN_PAGES1 (MAXPAGES1 / UNIT_PAGES)
  26. /* get aligned page from offset address */
  27. #define get_aligned_page(offset) ((offset) >> PAGE_SHIFT)
  28. /* get offset address from aligned page */
  29. #define aligned_page_offset(page) ((page) << PAGE_SHIFT)
  30. #if PAGE_SIZE == EMUPAGESIZE && !IS_ENABLED(CONFIG_DYNAMIC_DEBUG)
  31. /* fill PTB entrie(s) corresponding to page with addr */
  32. #define set_ptb_entry(emu,page,addr) __set_ptb_entry(emu,page,addr)
  33. /* fill PTB entrie(s) corresponding to page with silence pointer */
  34. #define set_silent_ptb(emu,page) __set_ptb_entry(emu,page,emu->silent_page.addr)
  35. #else
  36. /* fill PTB entries -- we need to fill UNIT_PAGES entries */
  37. static inline void set_ptb_entry(struct snd_emu10k1 *emu, int page, dma_addr_t addr)
  38. {
  39. int i;
  40. page *= UNIT_PAGES;
  41. for (i = 0; i < UNIT_PAGES; i++, page++) {
  42. __set_ptb_entry(emu, page, addr);
  43. dev_dbg(emu->card->dev, "mapped page %d to entry %.8x\n", page,
  44. (unsigned int)__get_ptb_entry(emu, page));
  45. addr += EMUPAGESIZE;
  46. }
  47. }
  48. static inline void set_silent_ptb(struct snd_emu10k1 *emu, int page)
  49. {
  50. int i;
  51. page *= UNIT_PAGES;
  52. for (i = 0; i < UNIT_PAGES; i++, page++) {
  53. /* do not increment ptr */
  54. __set_ptb_entry(emu, page, emu->silent_page.addr);
  55. dev_dbg(emu->card->dev, "mapped silent page %d to entry %.8x\n",
  56. page, (unsigned int)__get_ptb_entry(emu, page));
  57. }
  58. }
  59. #endif /* PAGE_SIZE */
  60. /*
  61. */
  62. static int synth_alloc_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk);
  63. static int synth_free_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk);
  64. #define get_emu10k1_memblk(l,member) list_entry(l, struct snd_emu10k1_memblk, member)
  65. /* initialize emu10k1 part */
  66. static void emu10k1_memblk_init(struct snd_emu10k1_memblk *blk)
  67. {
  68. blk->mapped_page = -1;
  69. INIT_LIST_HEAD(&blk->mapped_link);
  70. INIT_LIST_HEAD(&blk->mapped_order_link);
  71. blk->map_locked = 0;
  72. blk->first_page = get_aligned_page(blk->mem.offset);
  73. blk->last_page = get_aligned_page(blk->mem.offset + blk->mem.size - 1);
  74. blk->pages = blk->last_page - blk->first_page + 1;
  75. }
  76. /*
  77. * search empty region on PTB with the given size
  78. *
  79. * if an empty region is found, return the page and store the next mapped block
  80. * in nextp
  81. * if not found, return a negative error code.
  82. */
  83. static int search_empty_map_area(struct snd_emu10k1 *emu, int npages, struct list_head **nextp)
  84. {
  85. int page = 1, found_page = -ENOMEM;
  86. int max_size = npages;
  87. int size;
  88. struct list_head *candidate = &emu->mapped_link_head;
  89. struct list_head *pos;
  90. list_for_each (pos, &emu->mapped_link_head) {
  91. struct snd_emu10k1_memblk *blk = get_emu10k1_memblk(pos, mapped_link);
  92. if (blk->mapped_page < 0)
  93. continue;
  94. size = blk->mapped_page - page;
  95. if (size == npages) {
  96. *nextp = pos;
  97. return page;
  98. }
  99. else if (size > max_size) {
  100. /* we look for the maximum empty hole */
  101. max_size = size;
  102. candidate = pos;
  103. found_page = page;
  104. }
  105. page = blk->mapped_page + blk->pages;
  106. }
  107. size = (emu->address_mode ? MAX_ALIGN_PAGES1 : MAX_ALIGN_PAGES0) - page;
  108. if (size >= max_size) {
  109. *nextp = pos;
  110. return page;
  111. }
  112. *nextp = candidate;
  113. return found_page;
  114. }
  115. /*
  116. * map a memory block onto emu10k1's PTB
  117. *
  118. * call with memblk_lock held
  119. */
  120. static int map_memblk(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
  121. {
  122. int page, pg;
  123. struct list_head *next;
  124. page = search_empty_map_area(emu, blk->pages, &next);
  125. if (page < 0) /* not found */
  126. return page;
  127. if (page == 0) {
  128. dev_err(emu->card->dev, "trying to map zero (reserved) page\n");
  129. return -EINVAL;
  130. }
  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. p = blk->mapped_link.prev;
  156. if (p != &emu->mapped_link_head) {
  157. q = get_emu10k1_memblk(p, mapped_link);
  158. start_page = q->mapped_page + q->pages;
  159. } else {
  160. start_page = 1;
  161. }
  162. p = blk->mapped_link.next;
  163. if (p != &emu->mapped_link_head) {
  164. q = get_emu10k1_memblk(p, mapped_link);
  165. end_page = q->mapped_page;
  166. } else {
  167. end_page = (emu->address_mode ? MAX_ALIGN_PAGES1 : MAX_ALIGN_PAGES0);
  168. }
  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. err = map_memblk(emu, blk);
  250. if (err < 0) {
  251. /* no enough page - try to unmap some blocks */
  252. /* starting from the oldest block */
  253. p = emu->mapped_order_link_head.next;
  254. for (; p != &emu->mapped_order_link_head; p = nextp) {
  255. nextp = p->next;
  256. deleted = get_emu10k1_memblk(p, mapped_order_link);
  257. if (deleted->map_locked)
  258. continue;
  259. size = unmap_memblk(emu, deleted);
  260. if (size >= blk->pages) {
  261. /* ok the empty region is enough large */
  262. err = map_memblk(emu, blk);
  263. break;
  264. }
  265. }
  266. }
  267. spin_unlock_irqrestore(&emu->memblk_lock, flags);
  268. return err;
  269. }
  270. EXPORT_SYMBOL(snd_emu10k1_memblk_map);
  271. /*
  272. * page allocation for DMA
  273. */
  274. struct snd_util_memblk *
  275. snd_emu10k1_alloc_pages(struct snd_emu10k1 *emu, struct snd_pcm_substream *substream)
  276. {
  277. struct snd_pcm_runtime *runtime = substream->runtime;
  278. struct snd_util_memhdr *hdr;
  279. struct snd_emu10k1_memblk *blk;
  280. int page, err, idx;
  281. if (snd_BUG_ON(!emu))
  282. return NULL;
  283. if (snd_BUG_ON(runtime->dma_bytes <= 0 ||
  284. runtime->dma_bytes >= (emu->address_mode ? MAXPAGES1 : MAXPAGES0) * EMUPAGESIZE))
  285. return NULL;
  286. hdr = emu->memhdr;
  287. if (snd_BUG_ON(!hdr))
  288. return NULL;
  289. idx = runtime->period_size >= runtime->buffer_size ?
  290. (emu->delay_pcm_irq * 2) : 0;
  291. mutex_lock(&hdr->block_mutex);
  292. blk = search_empty(emu, runtime->dma_bytes + idx);
  293. if (blk == NULL) {
  294. mutex_unlock(&hdr->block_mutex);
  295. return NULL;
  296. }
  297. /* fill buffer addresses but pointers are not stored so that
  298. * snd_free_pci_page() is not called in synth_free()
  299. */
  300. idx = 0;
  301. for (page = blk->first_page; page <= blk->last_page; page++, idx++) {
  302. unsigned long ofs = idx << PAGE_SHIFT;
  303. dma_addr_t addr;
  304. if (ofs >= runtime->dma_bytes)
  305. addr = emu->silent_page.addr;
  306. else
  307. addr = snd_pcm_sgbuf_get_addr(substream, ofs);
  308. if (! is_valid_page(emu, addr)) {
  309. dev_err_ratelimited(emu->card->dev,
  310. "emu: failure page = %d\n", idx);
  311. mutex_unlock(&hdr->block_mutex);
  312. return NULL;
  313. }
  314. emu->page_addr_table[page] = addr;
  315. emu->page_ptr_table[page] = NULL;
  316. }
  317. /* set PTB entries */
  318. blk->map_locked = 1; /* do not unmap this block! */
  319. err = snd_emu10k1_memblk_map(emu, blk);
  320. if (err < 0) {
  321. __snd_util_mem_free(hdr, (struct snd_util_memblk *)blk);
  322. mutex_unlock(&hdr->block_mutex);
  323. return NULL;
  324. }
  325. mutex_unlock(&hdr->block_mutex);
  326. return (struct snd_util_memblk *)blk;
  327. }
  328. /*
  329. * release DMA buffer from page table
  330. */
  331. int snd_emu10k1_free_pages(struct snd_emu10k1 *emu, struct snd_util_memblk *blk)
  332. {
  333. if (snd_BUG_ON(!emu || !blk))
  334. return -EINVAL;
  335. return snd_emu10k1_synth_free(emu, blk);
  336. }
  337. /*
  338. * allocate DMA pages, widening the allocation if necessary
  339. *
  340. * See the comment above snd_emu10k1_detect_iommu() in emu10k1_main.c why
  341. * this might be needed.
  342. *
  343. * If you modify this function check whether __synth_free_pages() also needs
  344. * changes.
  345. */
  346. int snd_emu10k1_alloc_pages_maybe_wider(struct snd_emu10k1 *emu, size_t size,
  347. struct snd_dma_buffer *dmab)
  348. {
  349. if (emu->iommu_workaround) {
  350. size_t npages = DIV_ROUND_UP(size, PAGE_SIZE);
  351. size_t size_real = npages * PAGE_SIZE;
  352. /*
  353. * The device has been observed to accesses up to 256 extra
  354. * bytes, but use 1k to be safe.
  355. */
  356. if (size_real < size + 1024)
  357. size += PAGE_SIZE;
  358. }
  359. return snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  360. &emu->pci->dev, size, dmab);
  361. }
  362. /*
  363. * memory allocation using multiple pages (for synth)
  364. * Unlike the DMA allocation above, non-contiguous pages are assined.
  365. */
  366. /*
  367. * allocate a synth sample area
  368. */
  369. struct snd_util_memblk *
  370. snd_emu10k1_synth_alloc(struct snd_emu10k1 *hw, unsigned int size)
  371. {
  372. struct snd_emu10k1_memblk *blk;
  373. struct snd_util_memhdr *hdr = hw->memhdr;
  374. mutex_lock(&hdr->block_mutex);
  375. blk = (struct snd_emu10k1_memblk *)__snd_util_mem_alloc(hdr, size);
  376. if (blk == NULL) {
  377. mutex_unlock(&hdr->block_mutex);
  378. return NULL;
  379. }
  380. if (synth_alloc_pages(hw, blk)) {
  381. __snd_util_mem_free(hdr, (struct snd_util_memblk *)blk);
  382. mutex_unlock(&hdr->block_mutex);
  383. return NULL;
  384. }
  385. snd_emu10k1_memblk_map(hw, blk);
  386. mutex_unlock(&hdr->block_mutex);
  387. return (struct snd_util_memblk *)blk;
  388. }
  389. EXPORT_SYMBOL(snd_emu10k1_synth_alloc);
  390. /*
  391. * free a synth sample area
  392. */
  393. int
  394. snd_emu10k1_synth_free(struct snd_emu10k1 *emu, struct snd_util_memblk *memblk)
  395. {
  396. struct snd_util_memhdr *hdr = emu->memhdr;
  397. struct snd_emu10k1_memblk *blk = (struct snd_emu10k1_memblk *)memblk;
  398. unsigned long flags;
  399. mutex_lock(&hdr->block_mutex);
  400. spin_lock_irqsave(&emu->memblk_lock, flags);
  401. if (blk->mapped_page >= 0)
  402. unmap_memblk(emu, blk);
  403. spin_unlock_irqrestore(&emu->memblk_lock, flags);
  404. synth_free_pages(emu, blk);
  405. __snd_util_mem_free(hdr, memblk);
  406. mutex_unlock(&hdr->block_mutex);
  407. return 0;
  408. }
  409. EXPORT_SYMBOL(snd_emu10k1_synth_free);
  410. /* check new allocation range */
  411. static void get_single_page_range(struct snd_util_memhdr *hdr,
  412. struct snd_emu10k1_memblk *blk,
  413. int *first_page_ret, int *last_page_ret)
  414. {
  415. struct list_head *p;
  416. struct snd_emu10k1_memblk *q;
  417. int first_page, last_page;
  418. first_page = blk->first_page;
  419. p = blk->mem.list.prev;
  420. if (p != &hdr->block) {
  421. q = get_emu10k1_memblk(p, mem.list);
  422. if (q->last_page == first_page)
  423. first_page++; /* first page was already allocated */
  424. }
  425. last_page = blk->last_page;
  426. p = blk->mem.list.next;
  427. if (p != &hdr->block) {
  428. q = get_emu10k1_memblk(p, mem.list);
  429. if (q->first_page == last_page)
  430. last_page--; /* last page was already allocated */
  431. }
  432. *first_page_ret = first_page;
  433. *last_page_ret = last_page;
  434. }
  435. /* release allocated pages */
  436. static void __synth_free_pages(struct snd_emu10k1 *emu, int first_page,
  437. int last_page)
  438. {
  439. struct snd_dma_buffer dmab;
  440. int page;
  441. dmab.dev.type = SNDRV_DMA_TYPE_DEV;
  442. dmab.dev.dev = &emu->pci->dev;
  443. for (page = first_page; page <= last_page; page++) {
  444. if (emu->page_ptr_table[page] == NULL)
  445. continue;
  446. dmab.area = emu->page_ptr_table[page];
  447. dmab.addr = emu->page_addr_table[page];
  448. /*
  449. * please keep me in sync with logic in
  450. * snd_emu10k1_alloc_pages_maybe_wider()
  451. */
  452. dmab.bytes = PAGE_SIZE;
  453. if (emu->iommu_workaround)
  454. dmab.bytes *= 2;
  455. snd_dma_free_pages(&dmab);
  456. emu->page_addr_table[page] = 0;
  457. emu->page_ptr_table[page] = NULL;
  458. }
  459. }
  460. /*
  461. * allocate kernel pages
  462. */
  463. static int synth_alloc_pages(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
  464. {
  465. int page, first_page, last_page;
  466. struct snd_dma_buffer dmab;
  467. emu10k1_memblk_init(blk);
  468. get_single_page_range(emu->memhdr, blk, &first_page, &last_page);
  469. /* allocate kernel pages */
  470. for (page = first_page; page <= last_page; page++) {
  471. if (snd_emu10k1_alloc_pages_maybe_wider(emu, PAGE_SIZE,
  472. &dmab) < 0)
  473. goto __fail;
  474. if (!is_valid_page(emu, dmab.addr)) {
  475. snd_dma_free_pages(&dmab);
  476. goto __fail;
  477. }
  478. emu->page_addr_table[page] = dmab.addr;
  479. emu->page_ptr_table[page] = dmab.area;
  480. }
  481. return 0;
  482. __fail:
  483. /* release allocated pages */
  484. last_page = page - 1;
  485. __synth_free_pages(emu, first_page, last_page);
  486. return -ENOMEM;
  487. }
  488. /*
  489. * free pages
  490. */
  491. static int synth_free_pages(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
  492. {
  493. int first_page, last_page;
  494. get_single_page_range(emu->memhdr, blk, &first_page, &last_page);
  495. __synth_free_pages(emu, first_page, last_page);
  496. return 0;
  497. }
  498. /* calculate buffer pointer from offset address */
  499. static inline void *offset_ptr(struct snd_emu10k1 *emu, int page, int offset)
  500. {
  501. char *ptr;
  502. if (snd_BUG_ON(page < 0 || page >= emu->max_cache_pages))
  503. return NULL;
  504. ptr = emu->page_ptr_table[page];
  505. if (! ptr) {
  506. dev_err(emu->card->dev,
  507. "access to NULL ptr: page = %d\n", page);
  508. return NULL;
  509. }
  510. ptr += offset & (PAGE_SIZE - 1);
  511. return (void*)ptr;
  512. }
  513. /*
  514. * bzero(blk + offset, size)
  515. */
  516. int snd_emu10k1_synth_bzero(struct snd_emu10k1 *emu, struct snd_util_memblk *blk,
  517. int offset, int size)
  518. {
  519. int page, nextofs, end_offset, temp, temp1;
  520. void *ptr;
  521. struct snd_emu10k1_memblk *p = (struct snd_emu10k1_memblk *)blk;
  522. offset += blk->offset & (PAGE_SIZE - 1);
  523. end_offset = offset + size;
  524. page = get_aligned_page(offset);
  525. do {
  526. nextofs = aligned_page_offset(page + 1);
  527. temp = nextofs - offset;
  528. temp1 = end_offset - offset;
  529. if (temp1 < temp)
  530. temp = temp1;
  531. ptr = offset_ptr(emu, page + p->first_page, offset);
  532. if (ptr)
  533. memset(ptr, 0, temp);
  534. offset = nextofs;
  535. page++;
  536. } while (offset < end_offset);
  537. return 0;
  538. }
  539. EXPORT_SYMBOL(snd_emu10k1_synth_bzero);
  540. /*
  541. * copy_from_user(blk + offset, data, size)
  542. */
  543. int snd_emu10k1_synth_copy_from_user(struct snd_emu10k1 *emu, struct snd_util_memblk *blk,
  544. int offset, const char __user *data, int size)
  545. {
  546. int page, nextofs, end_offset, temp, temp1;
  547. void *ptr;
  548. struct snd_emu10k1_memblk *p = (struct snd_emu10k1_memblk *)blk;
  549. offset += blk->offset & (PAGE_SIZE - 1);
  550. end_offset = offset + size;
  551. page = get_aligned_page(offset);
  552. do {
  553. nextofs = aligned_page_offset(page + 1);
  554. temp = nextofs - offset;
  555. temp1 = end_offset - offset;
  556. if (temp1 < temp)
  557. temp = temp1;
  558. ptr = offset_ptr(emu, page + p->first_page, offset);
  559. if (ptr && copy_from_user(ptr, data, temp))
  560. return -EFAULT;
  561. offset = nextofs;
  562. data += temp;
  563. page++;
  564. } while (offset < end_offset);
  565. return 0;
  566. }
  567. EXPORT_SYMBOL(snd_emu10k1_synth_copy_from_user);