1 // SPDX-License-Identifier: GPL-2.0-or-later 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 2 /* 3 * Copyright (c) by Jaroslav Kysela <perex@pe 3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 4 * Copyright (c) by Takashi Iwai <tiwai@suse. 4 * Copyright (c) by Takashi Iwai <tiwai@suse.de> 5 * 5 * 6 * EMU10K1 memory page allocation (PTB area) 6 * EMU10K1 memory page allocation (PTB area) 7 */ 7 */ 8 8 9 #include <linux/pci.h> 9 #include <linux/pci.h> 10 #include <linux/gfp.h> 10 #include <linux/gfp.h> 11 #include <linux/time.h> 11 #include <linux/time.h> 12 #include <linux/mutex.h> 12 #include <linux/mutex.h> 13 #include <linux/export.h> 13 #include <linux/export.h> 14 14 15 #include <sound/core.h> 15 #include <sound/core.h> 16 #include <sound/emu10k1.h> 16 #include <sound/emu10k1.h> 17 17 18 /* page arguments of these two macros are Emu 18 /* page arguments of these two macros are Emu page (4096 bytes), not like 19 * aligned pages in others 19 * aligned pages in others 20 */ 20 */ 21 #define __set_ptb_entry(emu,page,addr) \ 21 #define __set_ptb_entry(emu,page,addr) \ 22 (((__le32 *)(emu)->ptb_pages.area)[pag 22 (((__le32 *)(emu)->ptb_pages.area)[page] = \ 23 cpu_to_le32(((addr) << (emu->address_ 23 cpu_to_le32(((addr) << (emu->address_mode)) | (page))) 24 #define __get_ptb_entry(emu, page) \ 24 #define __get_ptb_entry(emu, page) \ 25 (le32_to_cpu(((__le32 *)(emu)->ptb_pag 25 (le32_to_cpu(((__le32 *)(emu)->ptb_pages.area)[page])) 26 26 27 #define UNIT_PAGES (PAGE_SIZE / E 27 #define UNIT_PAGES (PAGE_SIZE / EMUPAGESIZE) 28 #define MAX_ALIGN_PAGES0 (MAXPA 28 #define MAX_ALIGN_PAGES0 (MAXPAGES0 / UNIT_PAGES) 29 #define MAX_ALIGN_PAGES1 (MAXPA 29 #define MAX_ALIGN_PAGES1 (MAXPAGES1 / UNIT_PAGES) 30 /* get aligned page from offset address */ 30 /* get aligned page from offset address */ 31 #define get_aligned_page(offset) ((offs 31 #define get_aligned_page(offset) ((offset) >> PAGE_SHIFT) 32 /* get offset address from aligned page */ 32 /* get offset address from aligned page */ 33 #define aligned_page_offset(page) ((page 33 #define aligned_page_offset(page) ((page) << PAGE_SHIFT) 34 34 35 #if PAGE_SIZE == EMUPAGESIZE && !IS_ENABLED(CO 35 #if PAGE_SIZE == EMUPAGESIZE && !IS_ENABLED(CONFIG_DYNAMIC_DEBUG) 36 /* fill PTB entrie(s) corresponding to page wi 36 /* fill PTB entrie(s) corresponding to page with addr */ 37 #define set_ptb_entry(emu,page,addr) __set_ 37 #define set_ptb_entry(emu,page,addr) __set_ptb_entry(emu,page,addr) 38 /* fill PTB entrie(s) corresponding to page wi 38 /* fill PTB entrie(s) corresponding to page with silence pointer */ 39 #define set_silent_ptb(emu,page) __set_ 39 #define set_silent_ptb(emu,page) __set_ptb_entry(emu,page,emu->silent_page.addr) 40 #else 40 #else 41 /* fill PTB entries -- we need to fill UNIT_PA 41 /* fill PTB entries -- we need to fill UNIT_PAGES entries */ 42 static inline void set_ptb_entry(struct snd_em 42 static inline void set_ptb_entry(struct snd_emu10k1 *emu, int page, dma_addr_t addr) 43 { 43 { 44 int i; 44 int i; 45 page *= UNIT_PAGES; 45 page *= UNIT_PAGES; 46 for (i = 0; i < UNIT_PAGES; i++, page+ 46 for (i = 0; i < UNIT_PAGES; i++, page++) { 47 __set_ptb_entry(emu, page, add 47 __set_ptb_entry(emu, page, addr); 48 dev_dbg(emu->card->dev, "mappe 48 dev_dbg(emu->card->dev, "mapped page %d to entry %.8x\n", page, 49 (unsigned int)__get_pt 49 (unsigned int)__get_ptb_entry(emu, page)); 50 addr += EMUPAGESIZE; 50 addr += EMUPAGESIZE; 51 } 51 } 52 } 52 } 53 static inline void set_silent_ptb(struct snd_e 53 static inline void set_silent_ptb(struct snd_emu10k1 *emu, int page) 54 { 54 { 55 int i; 55 int i; 56 page *= UNIT_PAGES; 56 page *= UNIT_PAGES; 57 for (i = 0; i < UNIT_PAGES; i++, page+ 57 for (i = 0; i < UNIT_PAGES; i++, page++) { 58 /* do not increment ptr */ 58 /* do not increment ptr */ 59 __set_ptb_entry(emu, page, emu 59 __set_ptb_entry(emu, page, emu->silent_page.addr); 60 dev_dbg(emu->card->dev, "mappe 60 dev_dbg(emu->card->dev, "mapped silent page %d to entry %.8x\n", 61 page, (unsigned int)__ 61 page, (unsigned int)__get_ptb_entry(emu, page)); 62 } 62 } 63 } 63 } 64 #endif /* PAGE_SIZE */ 64 #endif /* PAGE_SIZE */ 65 65 66 66 67 /* 67 /* 68 */ 68 */ 69 static int synth_alloc_pages(struct snd_emu10k 69 static int synth_alloc_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk); 70 static int synth_free_pages(struct snd_emu10k1 70 static int synth_free_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk); 71 71 72 #define get_emu10k1_memblk(l,member) list_e 72 #define get_emu10k1_memblk(l,member) list_entry(l, struct snd_emu10k1_memblk, member) 73 73 74 74 75 /* initialize emu10k1 part */ 75 /* initialize emu10k1 part */ 76 static void emu10k1_memblk_init(struct snd_emu 76 static void emu10k1_memblk_init(struct snd_emu10k1_memblk *blk) 77 { 77 { 78 blk->mapped_page = -1; 78 blk->mapped_page = -1; 79 INIT_LIST_HEAD(&blk->mapped_link); 79 INIT_LIST_HEAD(&blk->mapped_link); 80 INIT_LIST_HEAD(&blk->mapped_order_link 80 INIT_LIST_HEAD(&blk->mapped_order_link); 81 blk->map_locked = 0; 81 blk->map_locked = 0; 82 82 83 blk->first_page = get_aligned_page(blk 83 blk->first_page = get_aligned_page(blk->mem.offset); 84 blk->last_page = get_aligned_page(blk- 84 blk->last_page = get_aligned_page(blk->mem.offset + blk->mem.size - 1); 85 blk->pages = blk->last_page - blk->fir 85 blk->pages = blk->last_page - blk->first_page + 1; 86 } 86 } 87 87 88 /* 88 /* 89 * search empty region on PTB with the given s 89 * search empty region on PTB with the given size 90 * 90 * 91 * if an empty region is found, return the pag 91 * if an empty region is found, return the page and store the next mapped block 92 * in nextp 92 * in nextp 93 * if not found, return a negative error code. 93 * if not found, return a negative error code. 94 */ 94 */ 95 static int search_empty_map_area(struct snd_em 95 static int search_empty_map_area(struct snd_emu10k1 *emu, int npages, struct list_head **nextp) 96 { 96 { 97 int page = 1, found_page = -ENOMEM; 97 int page = 1, found_page = -ENOMEM; 98 int max_size = npages; 98 int max_size = npages; 99 int size; 99 int size; 100 struct list_head *candidate = &emu->ma 100 struct list_head *candidate = &emu->mapped_link_head; 101 struct list_head *pos; 101 struct list_head *pos; 102 102 103 list_for_each (pos, &emu->mapped_link_ 103 list_for_each (pos, &emu->mapped_link_head) { 104 struct snd_emu10k1_memblk *blk 104 struct snd_emu10k1_memblk *blk = get_emu10k1_memblk(pos, mapped_link); 105 if (blk->mapped_page < 0) 105 if (blk->mapped_page < 0) 106 continue; 106 continue; 107 size = blk->mapped_page - page 107 size = blk->mapped_page - page; 108 if (size == npages) { 108 if (size == npages) { 109 *nextp = pos; 109 *nextp = pos; 110 return page; 110 return page; 111 } 111 } 112 else if (size > max_size) { 112 else if (size > max_size) { 113 /* we look for the max 113 /* we look for the maximum empty hole */ 114 max_size = size; 114 max_size = size; 115 candidate = pos; 115 candidate = pos; 116 found_page = page; 116 found_page = page; 117 } 117 } 118 page = blk->mapped_page + blk- 118 page = blk->mapped_page + blk->pages; 119 } 119 } 120 size = (emu->address_mode ? MAX_ALIGN_ 120 size = (emu->address_mode ? MAX_ALIGN_PAGES1 : MAX_ALIGN_PAGES0) - page; 121 if (size >= max_size) { 121 if (size >= max_size) { 122 *nextp = pos; 122 *nextp = pos; 123 return page; 123 return page; 124 } 124 } 125 *nextp = candidate; 125 *nextp = candidate; 126 return found_page; 126 return found_page; 127 } 127 } 128 128 129 /* 129 /* 130 * map a memory block onto emu10k1's PTB 130 * map a memory block onto emu10k1's PTB 131 * 131 * 132 * call with memblk_lock held 132 * call with memblk_lock held 133 */ 133 */ 134 static int map_memblk(struct snd_emu10k1 *emu, 134 static int map_memblk(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk) 135 { 135 { 136 int page, pg; 136 int page, pg; 137 struct list_head *next; 137 struct list_head *next; 138 138 139 page = search_empty_map_area(emu, blk- 139 page = search_empty_map_area(emu, blk->pages, &next); 140 if (page < 0) /* not found */ 140 if (page < 0) /* not found */ 141 return page; 141 return page; 142 if (page == 0) { 142 if (page == 0) { 143 dev_err(emu->card->dev, "tryin 143 dev_err(emu->card->dev, "trying to map zero (reserved) page\n"); 144 return -EINVAL; 144 return -EINVAL; 145 } 145 } 146 /* insert this block in the proper pos 146 /* insert this block in the proper position of mapped list */ 147 list_add_tail(&blk->mapped_link, next) 147 list_add_tail(&blk->mapped_link, next); 148 /* append this as a newest block in or 148 /* append this as a newest block in order list */ 149 list_add_tail(&blk->mapped_order_link, 149 list_add_tail(&blk->mapped_order_link, &emu->mapped_order_link_head); 150 blk->mapped_page = page; 150 blk->mapped_page = page; 151 /* fill PTB */ 151 /* fill PTB */ 152 for (pg = blk->first_page; pg <= blk-> 152 for (pg = blk->first_page; pg <= blk->last_page; pg++) { 153 set_ptb_entry(emu, page, emu-> 153 set_ptb_entry(emu, page, emu->page_addr_table[pg]); 154 page++; 154 page++; 155 } 155 } 156 return 0; 156 return 0; 157 } 157 } 158 158 159 /* 159 /* 160 * unmap the block 160 * unmap the block 161 * return the size of resultant empty pages 161 * return the size of resultant empty pages 162 * 162 * 163 * call with memblk_lock held 163 * call with memblk_lock held 164 */ 164 */ 165 static int unmap_memblk(struct snd_emu10k1 *em 165 static int unmap_memblk(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk) 166 { 166 { 167 int start_page, end_page, mpage, pg; 167 int start_page, end_page, mpage, pg; 168 struct list_head *p; 168 struct list_head *p; 169 struct snd_emu10k1_memblk *q; 169 struct snd_emu10k1_memblk *q; 170 170 171 /* calculate the expected size of empt 171 /* calculate the expected size of empty region */ 172 p = blk->mapped_link.prev; 172 p = blk->mapped_link.prev; 173 if (p != &emu->mapped_link_head) { 173 if (p != &emu->mapped_link_head) { 174 q = get_emu10k1_memblk(p, mapp 174 q = get_emu10k1_memblk(p, mapped_link); 175 start_page = q->mapped_page + 175 start_page = q->mapped_page + q->pages; 176 } else { 176 } else { 177 start_page = 1; 177 start_page = 1; 178 } 178 } 179 p = blk->mapped_link.next; 179 p = blk->mapped_link.next; 180 if (p != &emu->mapped_link_head) { 180 if (p != &emu->mapped_link_head) { 181 q = get_emu10k1_memblk(p, mapp 181 q = get_emu10k1_memblk(p, mapped_link); 182 end_page = q->mapped_page; 182 end_page = q->mapped_page; 183 } else { 183 } else { 184 end_page = (emu->address_mode 184 end_page = (emu->address_mode ? MAX_ALIGN_PAGES1 : MAX_ALIGN_PAGES0); 185 } 185 } 186 186 187 /* remove links */ 187 /* remove links */ 188 list_del(&blk->mapped_link); 188 list_del(&blk->mapped_link); 189 list_del(&blk->mapped_order_link); 189 list_del(&blk->mapped_order_link); 190 /* clear PTB */ 190 /* clear PTB */ 191 mpage = blk->mapped_page; 191 mpage = blk->mapped_page; 192 for (pg = blk->first_page; pg <= blk-> 192 for (pg = blk->first_page; pg <= blk->last_page; pg++) { 193 set_silent_ptb(emu, mpage); 193 set_silent_ptb(emu, mpage); 194 mpage++; 194 mpage++; 195 } 195 } 196 blk->mapped_page = -1; 196 blk->mapped_page = -1; 197 return end_page - start_page; /* retur 197 return end_page - start_page; /* return the new empty size */ 198 } 198 } 199 199 200 /* 200 /* 201 * search empty pages with the given size, and 201 * search empty pages with the given size, and create a memory block 202 * 202 * 203 * unlike synth_alloc the memory block is alig 203 * unlike synth_alloc the memory block is aligned to the page start 204 */ 204 */ 205 static struct snd_emu10k1_memblk * 205 static struct snd_emu10k1_memblk * 206 search_empty(struct snd_emu10k1 *emu, int size 206 search_empty(struct snd_emu10k1 *emu, int size) 207 { 207 { 208 struct list_head *p; 208 struct list_head *p; 209 struct snd_emu10k1_memblk *blk; 209 struct snd_emu10k1_memblk *blk; 210 int page, psize; 210 int page, psize; 211 211 212 psize = get_aligned_page(size + PAGE_S 212 psize = get_aligned_page(size + PAGE_SIZE -1); 213 page = 0; 213 page = 0; 214 list_for_each(p, &emu->memhdr->block) 214 list_for_each(p, &emu->memhdr->block) { 215 blk = get_emu10k1_memblk(p, me 215 blk = get_emu10k1_memblk(p, mem.list); 216 if (page + psize <= blk->first 216 if (page + psize <= blk->first_page) 217 goto __found_pages; 217 goto __found_pages; 218 page = blk->last_page + 1; 218 page = blk->last_page + 1; 219 } 219 } 220 if (page + psize > emu->max_cache_page 220 if (page + psize > emu->max_cache_pages) 221 return NULL; 221 return NULL; 222 222 223 __found_pages: 223 __found_pages: 224 /* create a new memory block */ 224 /* create a new memory block */ 225 blk = (struct snd_emu10k1_memblk *)__s 225 blk = (struct snd_emu10k1_memblk *)__snd_util_memblk_new(emu->memhdr, psize << PAGE_SHIFT, p->prev); 226 if (blk == NULL) 226 if (blk == NULL) 227 return NULL; 227 return NULL; 228 blk->mem.offset = aligned_page_offset( 228 blk->mem.offset = aligned_page_offset(page); /* set aligned offset */ 229 emu10k1_memblk_init(blk); 229 emu10k1_memblk_init(blk); 230 return blk; 230 return blk; 231 } 231 } 232 232 233 233 234 /* 234 /* 235 * check if the given pointer is valid for pag 235 * check if the given pointer is valid for pages 236 */ 236 */ 237 static int is_valid_page(struct snd_emu10k1 *e 237 static int is_valid_page(struct snd_emu10k1 *emu, dma_addr_t addr) 238 { 238 { 239 if (addr & ~emu->dma_mask) { 239 if (addr & ~emu->dma_mask) { 240 dev_err_ratelimited(emu->card- 240 dev_err_ratelimited(emu->card->dev, 241 "max memory size is 0x 241 "max memory size is 0x%lx (addr = 0x%lx)!!\n", 242 emu->dma_mask, (unsign 242 emu->dma_mask, (unsigned long)addr); 243 return 0; 243 return 0; 244 } 244 } 245 if (addr & (EMUPAGESIZE-1)) { 245 if (addr & (EMUPAGESIZE-1)) { 246 dev_err_ratelimited(emu->card- 246 dev_err_ratelimited(emu->card->dev, "page is not aligned\n"); 247 return 0; 247 return 0; 248 } 248 } 249 return 1; 249 return 1; 250 } 250 } 251 251 252 /* 252 /* 253 * map the given memory block on PTB. 253 * map the given memory block on PTB. 254 * if the block is already mapped, update the 254 * if the block is already mapped, update the link order. 255 * if no empty pages are found, tries to relea 255 * if no empty pages are found, tries to release unused memory blocks 256 * and retry the mapping. 256 * and retry the mapping. 257 */ 257 */ 258 int snd_emu10k1_memblk_map(struct snd_emu10k1 258 int snd_emu10k1_memblk_map(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk) 259 { 259 { 260 int err; 260 int err; 261 int size; 261 int size; 262 struct list_head *p, *nextp; 262 struct list_head *p, *nextp; 263 struct snd_emu10k1_memblk *deleted; 263 struct snd_emu10k1_memblk *deleted; 264 unsigned long flags; 264 unsigned long flags; 265 265 266 spin_lock_irqsave(&emu->memblk_lock, f 266 spin_lock_irqsave(&emu->memblk_lock, flags); 267 if (blk->mapped_page >= 0) { 267 if (blk->mapped_page >= 0) { 268 /* update order link */ 268 /* update order link */ 269 list_move_tail(&blk->mapped_or 269 list_move_tail(&blk->mapped_order_link, 270 &emu->mapped_or 270 &emu->mapped_order_link_head); 271 spin_unlock_irqrestore(&emu->m 271 spin_unlock_irqrestore(&emu->memblk_lock, flags); 272 return 0; 272 return 0; 273 } 273 } 274 err = map_memblk(emu, blk); 274 err = map_memblk(emu, blk); 275 if (err < 0) { 275 if (err < 0) { 276 /* no enough page - try to unm 276 /* no enough page - try to unmap some blocks */ 277 /* starting from the oldest bl 277 /* starting from the oldest block */ 278 p = emu->mapped_order_link_hea 278 p = emu->mapped_order_link_head.next; 279 for (; p != &emu->mapped_order 279 for (; p != &emu->mapped_order_link_head; p = nextp) { 280 nextp = p->next; 280 nextp = p->next; 281 deleted = get_emu10k1_ 281 deleted = get_emu10k1_memblk(p, mapped_order_link); 282 if (deleted->map_locke 282 if (deleted->map_locked) 283 continue; 283 continue; 284 size = unmap_memblk(em 284 size = unmap_memblk(emu, deleted); 285 if (size >= blk->pages 285 if (size >= blk->pages) { 286 /* ok the empt 286 /* ok the empty region is enough large */ 287 err = map_memb 287 err = map_memblk(emu, blk); 288 break; 288 break; 289 } 289 } 290 } 290 } 291 } 291 } 292 spin_unlock_irqrestore(&emu->memblk_lo 292 spin_unlock_irqrestore(&emu->memblk_lock, flags); 293 return err; 293 return err; 294 } 294 } 295 295 296 EXPORT_SYMBOL(snd_emu10k1_memblk_map); 296 EXPORT_SYMBOL(snd_emu10k1_memblk_map); 297 297 298 /* 298 /* 299 * page allocation for DMA 299 * page allocation for DMA 300 */ 300 */ 301 struct snd_util_memblk * 301 struct snd_util_memblk * 302 snd_emu10k1_alloc_pages(struct snd_emu10k1 *em 302 snd_emu10k1_alloc_pages(struct snd_emu10k1 *emu, struct snd_pcm_substream *substream) 303 { 303 { 304 struct snd_pcm_runtime *runtime = subs 304 struct snd_pcm_runtime *runtime = substream->runtime; 305 struct snd_util_memhdr *hdr; 305 struct snd_util_memhdr *hdr; 306 struct snd_emu10k1_memblk *blk; 306 struct snd_emu10k1_memblk *blk; 307 int page, err, idx; 307 int page, err, idx; 308 308 309 if (snd_BUG_ON(!emu)) 309 if (snd_BUG_ON(!emu)) 310 return NULL; 310 return NULL; 311 if (snd_BUG_ON(runtime->dma_bytes <= 0 311 if (snd_BUG_ON(runtime->dma_bytes <= 0 || 312 runtime->dma_bytes >= ( 312 runtime->dma_bytes >= (emu->address_mode ? MAXPAGES1 : MAXPAGES0) * EMUPAGESIZE)) 313 return NULL; 313 return NULL; 314 hdr = emu->memhdr; 314 hdr = emu->memhdr; 315 if (snd_BUG_ON(!hdr)) 315 if (snd_BUG_ON(!hdr)) 316 return NULL; 316 return NULL; 317 317 318 mutex_lock(&hdr->block_mutex); 318 mutex_lock(&hdr->block_mutex); 319 blk = search_empty(emu, runtime->dma_b 319 blk = search_empty(emu, runtime->dma_bytes); 320 if (blk == NULL) { 320 if (blk == NULL) { 321 mutex_unlock(&hdr->block_mutex 321 mutex_unlock(&hdr->block_mutex); 322 return NULL; 322 return NULL; 323 } 323 } 324 /* fill buffer addresses but pointers 324 /* fill buffer addresses but pointers are not stored so that 325 * snd_free_pci_page() is not called i 325 * snd_free_pci_page() is not called in synth_free() 326 */ 326 */ 327 idx = 0; 327 idx = 0; 328 for (page = blk->first_page; page <= b 328 for (page = blk->first_page; page <= blk->last_page; page++, idx++) { 329 unsigned long ofs = idx << PAG 329 unsigned long ofs = idx << PAGE_SHIFT; 330 dma_addr_t addr; 330 dma_addr_t addr; 331 if (ofs >= runtime->dma_bytes) 331 if (ofs >= runtime->dma_bytes) 332 addr = emu->silent_pag 332 addr = emu->silent_page.addr; 333 else 333 else 334 addr = snd_pcm_sgbuf_g 334 addr = snd_pcm_sgbuf_get_addr(substream, ofs); 335 if (! is_valid_page(emu, addr) 335 if (! is_valid_page(emu, addr)) { 336 dev_err_ratelimited(em 336 dev_err_ratelimited(emu->card->dev, 337 "emu: failure 337 "emu: failure page = %d\n", idx); 338 mutex_unlock(&hdr->blo 338 mutex_unlock(&hdr->block_mutex); 339 return NULL; 339 return NULL; 340 } 340 } 341 emu->page_addr_table[page] = a 341 emu->page_addr_table[page] = addr; 342 emu->page_ptr_table[page] = NU 342 emu->page_ptr_table[page] = NULL; 343 } 343 } 344 344 345 /* set PTB entries */ 345 /* set PTB entries */ 346 blk->map_locked = 1; /* do not unmap t 346 blk->map_locked = 1; /* do not unmap this block! */ 347 err = snd_emu10k1_memblk_map(emu, blk) 347 err = snd_emu10k1_memblk_map(emu, blk); 348 if (err < 0) { 348 if (err < 0) { 349 __snd_util_mem_free(hdr, (stru 349 __snd_util_mem_free(hdr, (struct snd_util_memblk *)blk); 350 mutex_unlock(&hdr->block_mutex 350 mutex_unlock(&hdr->block_mutex); 351 return NULL; 351 return NULL; 352 } 352 } 353 mutex_unlock(&hdr->block_mutex); 353 mutex_unlock(&hdr->block_mutex); 354 return (struct snd_util_memblk *)blk; 354 return (struct snd_util_memblk *)blk; 355 } 355 } 356 356 357 357 358 /* 358 /* 359 * release DMA buffer from page table 359 * release DMA buffer from page table 360 */ 360 */ 361 int snd_emu10k1_free_pages(struct snd_emu10k1 361 int snd_emu10k1_free_pages(struct snd_emu10k1 *emu, struct snd_util_memblk *blk) 362 { 362 { 363 if (snd_BUG_ON(!emu || !blk)) 363 if (snd_BUG_ON(!emu || !blk)) 364 return -EINVAL; 364 return -EINVAL; 365 return snd_emu10k1_synth_free(emu, blk 365 return snd_emu10k1_synth_free(emu, blk); 366 } 366 } 367 367 368 /* 368 /* 369 * allocate DMA pages, widening the allocation 369 * allocate DMA pages, widening the allocation if necessary 370 * 370 * 371 * See the comment above snd_emu10k1_detect_io 371 * See the comment above snd_emu10k1_detect_iommu() in emu10k1_main.c why 372 * this might be needed. 372 * this might be needed. 373 * 373 * 374 * If you modify this function check whether _ 374 * If you modify this function check whether __synth_free_pages() also needs 375 * changes. 375 * changes. 376 */ 376 */ 377 int snd_emu10k1_alloc_pages_maybe_wider(struct 377 int snd_emu10k1_alloc_pages_maybe_wider(struct snd_emu10k1 *emu, size_t size, 378 struct 378 struct snd_dma_buffer *dmab) 379 { 379 { 380 if (emu->iommu_workaround) { 380 if (emu->iommu_workaround) { 381 size_t npages = DIV_ROUND_UP(s 381 size_t npages = DIV_ROUND_UP(size, PAGE_SIZE); 382 size_t size_real = npages * PA 382 size_t size_real = npages * PAGE_SIZE; 383 383 384 /* 384 /* 385 * The device has been observe 385 * The device has been observed to accesses up to 256 extra 386 * bytes, but use 1k to be saf 386 * bytes, but use 1k to be safe. 387 */ 387 */ 388 if (size_real < size + 1024) 388 if (size_real < size + 1024) 389 size += PAGE_SIZE; 389 size += PAGE_SIZE; 390 } 390 } 391 391 392 return snd_dma_alloc_pages(SNDRV_DMA_T 392 return snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, 393 &emu->pci-> 393 &emu->pci->dev, size, dmab); 394 } 394 } 395 395 396 /* 396 /* 397 * memory allocation using multiple pages (for 397 * memory allocation using multiple pages (for synth) 398 * Unlike the DMA allocation above, non-contig 398 * Unlike the DMA allocation above, non-contiguous pages are assined. 399 */ 399 */ 400 400 401 /* 401 /* 402 * allocate a synth sample area 402 * allocate a synth sample area 403 */ 403 */ 404 struct snd_util_memblk * 404 struct snd_util_memblk * 405 snd_emu10k1_synth_alloc(struct snd_emu10k1 *hw 405 snd_emu10k1_synth_alloc(struct snd_emu10k1 *hw, unsigned int size) 406 { 406 { 407 struct snd_emu10k1_memblk *blk; 407 struct snd_emu10k1_memblk *blk; 408 struct snd_util_memhdr *hdr = hw->memh 408 struct snd_util_memhdr *hdr = hw->memhdr; 409 409 410 mutex_lock(&hdr->block_mutex); 410 mutex_lock(&hdr->block_mutex); 411 blk = (struct snd_emu10k1_memblk *)__s 411 blk = (struct snd_emu10k1_memblk *)__snd_util_mem_alloc(hdr, size); 412 if (blk == NULL) { 412 if (blk == NULL) { 413 mutex_unlock(&hdr->block_mutex 413 mutex_unlock(&hdr->block_mutex); 414 return NULL; 414 return NULL; 415 } 415 } 416 if (synth_alloc_pages(hw, blk)) { 416 if (synth_alloc_pages(hw, blk)) { 417 __snd_util_mem_free(hdr, (stru 417 __snd_util_mem_free(hdr, (struct snd_util_memblk *)blk); 418 mutex_unlock(&hdr->block_mutex 418 mutex_unlock(&hdr->block_mutex); 419 return NULL; 419 return NULL; 420 } 420 } 421 snd_emu10k1_memblk_map(hw, blk); 421 snd_emu10k1_memblk_map(hw, blk); 422 mutex_unlock(&hdr->block_mutex); 422 mutex_unlock(&hdr->block_mutex); 423 return (struct snd_util_memblk *)blk; 423 return (struct snd_util_memblk *)blk; 424 } 424 } 425 425 426 EXPORT_SYMBOL(snd_emu10k1_synth_alloc); 426 EXPORT_SYMBOL(snd_emu10k1_synth_alloc); 427 427 428 /* 428 /* 429 * free a synth sample area 429 * free a synth sample area 430 */ 430 */ 431 int 431 int 432 snd_emu10k1_synth_free(struct snd_emu10k1 *emu 432 snd_emu10k1_synth_free(struct snd_emu10k1 *emu, struct snd_util_memblk *memblk) 433 { 433 { 434 struct snd_util_memhdr *hdr = emu->mem 434 struct snd_util_memhdr *hdr = emu->memhdr; 435 struct snd_emu10k1_memblk *blk = (stru 435 struct snd_emu10k1_memblk *blk = (struct snd_emu10k1_memblk *)memblk; 436 unsigned long flags; 436 unsigned long flags; 437 437 438 mutex_lock(&hdr->block_mutex); 438 mutex_lock(&hdr->block_mutex); 439 spin_lock_irqsave(&emu->memblk_lock, f 439 spin_lock_irqsave(&emu->memblk_lock, flags); 440 if (blk->mapped_page >= 0) 440 if (blk->mapped_page >= 0) 441 unmap_memblk(emu, blk); 441 unmap_memblk(emu, blk); 442 spin_unlock_irqrestore(&emu->memblk_lo 442 spin_unlock_irqrestore(&emu->memblk_lock, flags); 443 synth_free_pages(emu, blk); 443 synth_free_pages(emu, blk); 444 __snd_util_mem_free(hdr, memblk); 444 __snd_util_mem_free(hdr, memblk); 445 mutex_unlock(&hdr->block_mutex); 445 mutex_unlock(&hdr->block_mutex); 446 return 0; 446 return 0; 447 } 447 } 448 448 449 EXPORT_SYMBOL(snd_emu10k1_synth_free); 449 EXPORT_SYMBOL(snd_emu10k1_synth_free); 450 450 451 /* check new allocation range */ 451 /* check new allocation range */ 452 static void get_single_page_range(struct snd_u 452 static void get_single_page_range(struct snd_util_memhdr *hdr, 453 struct snd_e 453 struct snd_emu10k1_memblk *blk, 454 int *first_p 454 int *first_page_ret, int *last_page_ret) 455 { 455 { 456 struct list_head *p; 456 struct list_head *p; 457 struct snd_emu10k1_memblk *q; 457 struct snd_emu10k1_memblk *q; 458 int first_page, last_page; 458 int first_page, last_page; 459 first_page = blk->first_page; 459 first_page = blk->first_page; 460 p = blk->mem.list.prev; 460 p = blk->mem.list.prev; 461 if (p != &hdr->block) { 461 if (p != &hdr->block) { 462 q = get_emu10k1_memblk(p, mem. 462 q = get_emu10k1_memblk(p, mem.list); 463 if (q->last_page == first_page 463 if (q->last_page == first_page) 464 first_page++; /* firs 464 first_page++; /* first page was already allocated */ 465 } 465 } 466 last_page = blk->last_page; 466 last_page = blk->last_page; 467 p = blk->mem.list.next; 467 p = blk->mem.list.next; 468 if (p != &hdr->block) { 468 if (p != &hdr->block) { 469 q = get_emu10k1_memblk(p, mem. 469 q = get_emu10k1_memblk(p, mem.list); 470 if (q->first_page == last_page 470 if (q->first_page == last_page) 471 last_page--; /* last p 471 last_page--; /* last page was already allocated */ 472 } 472 } 473 *first_page_ret = first_page; 473 *first_page_ret = first_page; 474 *last_page_ret = last_page; 474 *last_page_ret = last_page; 475 } 475 } 476 476 477 /* release allocated pages */ 477 /* release allocated pages */ 478 static void __synth_free_pages(struct snd_emu1 478 static void __synth_free_pages(struct snd_emu10k1 *emu, int first_page, 479 int last_page) 479 int last_page) 480 { 480 { 481 struct snd_dma_buffer dmab; 481 struct snd_dma_buffer dmab; 482 int page; 482 int page; 483 483 484 dmab.dev.type = SNDRV_DMA_TYPE_DEV; 484 dmab.dev.type = SNDRV_DMA_TYPE_DEV; 485 dmab.dev.dev = &emu->pci->dev; 485 dmab.dev.dev = &emu->pci->dev; 486 486 487 for (page = first_page; page <= last_p 487 for (page = first_page; page <= last_page; page++) { 488 if (emu->page_ptr_table[page] 488 if (emu->page_ptr_table[page] == NULL) 489 continue; 489 continue; 490 dmab.area = emu->page_ptr_tabl 490 dmab.area = emu->page_ptr_table[page]; 491 dmab.addr = emu->page_addr_tab 491 dmab.addr = emu->page_addr_table[page]; 492 492 493 /* 493 /* 494 * please keep me in sync with 494 * please keep me in sync with logic in 495 * snd_emu10k1_alloc_pages_may 495 * snd_emu10k1_alloc_pages_maybe_wider() 496 */ 496 */ 497 dmab.bytes = PAGE_SIZE; 497 dmab.bytes = PAGE_SIZE; 498 if (emu->iommu_workaround) 498 if (emu->iommu_workaround) 499 dmab.bytes *= 2; 499 dmab.bytes *= 2; 500 500 501 snd_dma_free_pages(&dmab); 501 snd_dma_free_pages(&dmab); 502 emu->page_addr_table[page] = 0 502 emu->page_addr_table[page] = 0; 503 emu->page_ptr_table[page] = NU 503 emu->page_ptr_table[page] = NULL; 504 } 504 } 505 } 505 } 506 506 507 /* 507 /* 508 * allocate kernel pages 508 * allocate kernel pages 509 */ 509 */ 510 static int synth_alloc_pages(struct snd_emu10k 510 static int synth_alloc_pages(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk) 511 { 511 { 512 int page, first_page, last_page; 512 int page, first_page, last_page; 513 struct snd_dma_buffer dmab; 513 struct snd_dma_buffer dmab; 514 514 515 emu10k1_memblk_init(blk); 515 emu10k1_memblk_init(blk); 516 get_single_page_range(emu->memhdr, blk 516 get_single_page_range(emu->memhdr, blk, &first_page, &last_page); 517 /* allocate kernel pages */ 517 /* allocate kernel pages */ 518 for (page = first_page; page <= last_p 518 for (page = first_page; page <= last_page; page++) { 519 if (snd_emu10k1_alloc_pages_ma 519 if (snd_emu10k1_alloc_pages_maybe_wider(emu, PAGE_SIZE, 520 520 &dmab) < 0) 521 goto __fail; 521 goto __fail; 522 if (!is_valid_page(emu, dmab.a 522 if (!is_valid_page(emu, dmab.addr)) { 523 snd_dma_free_pages(&dm 523 snd_dma_free_pages(&dmab); 524 goto __fail; 524 goto __fail; 525 } 525 } 526 emu->page_addr_table[page] = d 526 emu->page_addr_table[page] = dmab.addr; 527 emu->page_ptr_table[page] = dm 527 emu->page_ptr_table[page] = dmab.area; 528 } 528 } 529 return 0; 529 return 0; 530 530 531 __fail: 531 __fail: 532 /* release allocated pages */ 532 /* release allocated pages */ 533 last_page = page - 1; 533 last_page = page - 1; 534 __synth_free_pages(emu, first_page, la 534 __synth_free_pages(emu, first_page, last_page); 535 535 536 return -ENOMEM; 536 return -ENOMEM; 537 } 537 } 538 538 539 /* 539 /* 540 * free pages 540 * free pages 541 */ 541 */ 542 static int synth_free_pages(struct snd_emu10k1 542 static int synth_free_pages(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk) 543 { 543 { 544 int first_page, last_page; 544 int first_page, last_page; 545 545 546 get_single_page_range(emu->memhdr, blk 546 get_single_page_range(emu->memhdr, blk, &first_page, &last_page); 547 __synth_free_pages(emu, first_page, la 547 __synth_free_pages(emu, first_page, last_page); 548 return 0; 548 return 0; 549 } 549 } 550 550 551 /* calculate buffer pointer from offset addres 551 /* calculate buffer pointer from offset address */ 552 static inline void *offset_ptr(struct snd_emu1 552 static inline void *offset_ptr(struct snd_emu10k1 *emu, int page, int offset) 553 { 553 { 554 char *ptr; 554 char *ptr; 555 if (snd_BUG_ON(page < 0 || page >= emu 555 if (snd_BUG_ON(page < 0 || page >= emu->max_cache_pages)) 556 return NULL; 556 return NULL; 557 ptr = emu->page_ptr_table[page]; 557 ptr = emu->page_ptr_table[page]; 558 if (! ptr) { 558 if (! ptr) { 559 dev_err(emu->card->dev, 559 dev_err(emu->card->dev, 560 "access to NULL ptr: p 560 "access to NULL ptr: page = %d\n", page); 561 return NULL; 561 return NULL; 562 } 562 } 563 ptr += offset & (PAGE_SIZE - 1); 563 ptr += offset & (PAGE_SIZE - 1); 564 return (void*)ptr; 564 return (void*)ptr; 565 } 565 } 566 566 567 /* 567 /* 568 * memset(blk + offset, value, size) 568 * memset(blk + offset, value, size) 569 */ 569 */ 570 int snd_emu10k1_synth_memset(struct snd_emu10k 570 int snd_emu10k1_synth_memset(struct snd_emu10k1 *emu, struct snd_util_memblk *blk, 571 int offset, int s 571 int offset, int size, u8 value) 572 { 572 { 573 int page, nextofs, end_offset, temp, t 573 int page, nextofs, end_offset, temp, temp1; 574 void *ptr; 574 void *ptr; 575 struct snd_emu10k1_memblk *p = (struct 575 struct snd_emu10k1_memblk *p = (struct snd_emu10k1_memblk *)blk; 576 576 577 if (snd_BUG_ON(offset + size > p->mem. 577 if (snd_BUG_ON(offset + size > p->mem.size)) 578 return -EFAULT; 578 return -EFAULT; 579 579 580 offset += blk->offset & (PAGE_SIZE - 1 580 offset += blk->offset & (PAGE_SIZE - 1); 581 end_offset = offset + size; 581 end_offset = offset + size; 582 page = get_aligned_page(offset); 582 page = get_aligned_page(offset); 583 do { 583 do { 584 nextofs = aligned_page_offset( 584 nextofs = aligned_page_offset(page + 1); 585 temp = nextofs - offset; 585 temp = nextofs - offset; 586 temp1 = end_offset - offset; 586 temp1 = end_offset - offset; 587 if (temp1 < temp) 587 if (temp1 < temp) 588 temp = temp1; 588 temp = temp1; 589 ptr = offset_ptr(emu, page + p 589 ptr = offset_ptr(emu, page + p->first_page, offset); 590 if (ptr) 590 if (ptr) 591 memset(ptr, value, tem 591 memset(ptr, value, temp); 592 offset = nextofs; 592 offset = nextofs; 593 page++; 593 page++; 594 } while (offset < end_offset); 594 } while (offset < end_offset); 595 return 0; 595 return 0; 596 } 596 } 597 597 598 EXPORT_SYMBOL(snd_emu10k1_synth_memset); 598 EXPORT_SYMBOL(snd_emu10k1_synth_memset); 599 599 600 // Note that the value is assumed to be suitab 600 // Note that the value is assumed to be suitably repetitive. 601 static void xor_range(void *ptr, int size, u32 601 static void xor_range(void *ptr, int size, u32 value) 602 { 602 { 603 if ((long)ptr & 1) { 603 if ((long)ptr & 1) { 604 *(u8 *)ptr ^= (u8)value; 604 *(u8 *)ptr ^= (u8)value; 605 ptr++; 605 ptr++; 606 size--; 606 size--; 607 } 607 } 608 if (size > 1 && ((long)ptr & 2)) { 608 if (size > 1 && ((long)ptr & 2)) { 609 *(u16 *)ptr ^= (u16)value; 609 *(u16 *)ptr ^= (u16)value; 610 ptr += 2; 610 ptr += 2; 611 size -= 2; 611 size -= 2; 612 } 612 } 613 while (size > 3) { 613 while (size > 3) { 614 *(u32 *)ptr ^= value; 614 *(u32 *)ptr ^= value; 615 ptr += 4; 615 ptr += 4; 616 size -= 4; 616 size -= 4; 617 } 617 } 618 if (size > 1) { 618 if (size > 1) { 619 *(u16 *)ptr ^= (u16)value; 619 *(u16 *)ptr ^= (u16)value; 620 ptr += 2; 620 ptr += 2; 621 size -= 2; 621 size -= 2; 622 } 622 } 623 if (size > 0) 623 if (size > 0) 624 *(u8 *)ptr ^= (u8)value; 624 *(u8 *)ptr ^= (u8)value; 625 } 625 } 626 626 627 /* 627 /* 628 * copy_from_user(blk + offset, data, size) ^ 628 * copy_from_user(blk + offset, data, size) ^ xor 629 */ 629 */ 630 int snd_emu10k1_synth_copy_from_user(struct sn 630 int snd_emu10k1_synth_copy_from_user(struct snd_emu10k1 *emu, struct snd_util_memblk *blk, 631 int offse 631 int offset, const char __user *data, int size, u32 xor) 632 { 632 { 633 int page, nextofs, end_offset, temp, t 633 int page, nextofs, end_offset, temp, temp1; 634 void *ptr; 634 void *ptr; 635 struct snd_emu10k1_memblk *p = (struct 635 struct snd_emu10k1_memblk *p = (struct snd_emu10k1_memblk *)blk; 636 636 637 if (snd_BUG_ON(offset + size > p->mem. 637 if (snd_BUG_ON(offset + size > p->mem.size)) 638 return -EFAULT; 638 return -EFAULT; 639 639 640 offset += blk->offset & (PAGE_SIZE - 1 640 offset += blk->offset & (PAGE_SIZE - 1); 641 end_offset = offset + size; 641 end_offset = offset + size; 642 page = get_aligned_page(offset); 642 page = get_aligned_page(offset); 643 do { 643 do { 644 nextofs = aligned_page_offset( 644 nextofs = aligned_page_offset(page + 1); 645 temp = nextofs - offset; 645 temp = nextofs - offset; 646 temp1 = end_offset - offset; 646 temp1 = end_offset - offset; 647 if (temp1 < temp) 647 if (temp1 < temp) 648 temp = temp1; 648 temp = temp1; 649 ptr = offset_ptr(emu, page + p 649 ptr = offset_ptr(emu, page + p->first_page, offset); 650 if (ptr) { 650 if (ptr) { 651 if (copy_from_user(ptr 651 if (copy_from_user(ptr, data, temp)) 652 return -EFAULT 652 return -EFAULT; 653 if (xor) 653 if (xor) 654 xor_range(ptr, 654 xor_range(ptr, temp, xor); 655 } 655 } 656 offset = nextofs; 656 offset = nextofs; 657 data += temp; 657 data += temp; 658 page++; 658 page++; 659 } while (offset < end_offset); 659 } while (offset < end_offset); 660 return 0; 660 return 0; 661 } 661 } 662 662 663 EXPORT_SYMBOL(snd_emu10k1_synth_copy_from_user 663 EXPORT_SYMBOL(snd_emu10k1_synth_copy_from_user); 664 664
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.