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