ALSA: ctxfi - fix PTP address initialization

After hours of debugging, I finally found the reason why some source
and runtime combination does not work. The PTP (page table pages)
address must be aligned. I am not sure how much, but alignment to
PAGE_SIZE is sufficient. Also, use ALSA's page allocation routines
to ensure proper virtual -> physical address translation.

Cc: <stable@kernel.org>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>

+25 -36
+2 -13
sound/pci/ctxfi/ctatc.c
··· 166 166 167 167 static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index) 168 168 { 169 - struct ct_vm *vm; 170 - void *kvirt_addr; 171 - unsigned long phys_addr; 172 - 173 - vm = atc->vm; 174 - kvirt_addr = vm->get_ptp_virt(vm, index); 175 - if (kvirt_addr == NULL) 176 - phys_addr = (~0UL); 177 - else 178 - phys_addr = virt_to_phys(kvirt_addr); 179 - 180 - return phys_addr; 169 + return atc->vm->get_ptp_phys(atc->vm, index); 181 170 } 182 171 183 172 static unsigned int convert_format(snd_pcm_format_t snd_format) ··· 1658 1669 } 1659 1670 1660 1671 /* Set up device virtual memory management object */ 1661 - err = ct_vm_create(&atc->vm); 1672 + err = ct_vm_create(&atc->vm, pci); 1662 1673 if (err < 0) 1663 1674 goto error1; 1664 1675
+18 -20
sound/pci/ctxfi/ctvmem.c
··· 138 138 return NULL; 139 139 } 140 140 141 - ptp = vm->ptp[0]; 141 + ptp = (unsigned long *)vm->ptp[0].area; 142 142 pte_start = (block->addr >> CT_PAGE_SHIFT); 143 143 pages = block->size >> CT_PAGE_SHIFT; 144 144 for (i = 0; i < pages; i++) { ··· 158 158 } 159 159 160 160 /* * 161 - * return the host (kmalloced) addr of the @index-th device 162 - * page talbe page on success, or NULL on failure. 163 - * The first returned NULL indicates the termination. 161 + * return the host physical addr of the @index-th device 162 + * page table page on success, or ~0UL on failure. 163 + * The first returned ~0UL indicates the termination. 164 164 * */ 165 - static void * 166 - ct_get_ptp_virt(struct ct_vm *vm, int index) 165 + static dma_addr_t 166 + ct_get_ptp_phys(struct ct_vm *vm, int index) 167 167 { 168 - void *addr; 168 + dma_addr_t addr; 169 169 170 - addr = (index >= CT_PTP_NUM) ? NULL : vm->ptp[index]; 170 + addr = (index >= CT_PTP_NUM) ? ~0UL : vm->ptp[index].addr; 171 171 172 172 return addr; 173 173 } 174 174 175 - int ct_vm_create(struct ct_vm **rvm) 175 + int ct_vm_create(struct ct_vm **rvm, struct pci_dev *pci) 176 176 { 177 177 struct ct_vm *vm; 178 178 struct ct_vm_block *block; 179 - int i; 179 + int i, err = 0; 180 180 181 181 *rvm = NULL; 182 182 ··· 188 188 189 189 /* Allocate page table pages */ 190 190 for (i = 0; i < CT_PTP_NUM; i++) { 191 - vm->ptp[i] = kmalloc(PAGE_SIZE, GFP_KERNEL); 192 - if (!vm->ptp[i]) 191 + err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, 192 + snd_dma_pci_data(pci), 193 + PAGE_SIZE, &vm->ptp[i]); 194 + if (err < 0) 193 195 break; 194 196 } 195 - if (!i) { 197 + if (err < 0) { 196 198 /* no page table pages are allocated */ 197 - kfree(vm); 199 + ct_vm_destroy(vm); 198 200 return -ENOMEM; 199 201 } 200 202 vm->size = CT_ADDRS_PER_PAGE * i; 201 - /* Initialise remaining ptps */ 202 - for (; i < CT_PTP_NUM; i++) 203 - vm->ptp[i] = NULL; 204 - 205 203 vm->map = ct_vm_map; 206 204 vm->unmap = ct_vm_unmap; 207 - vm->get_ptp_virt = ct_get_ptp_virt; 205 + vm->get_ptp_phys = ct_get_ptp_phys; 208 206 INIT_LIST_HEAD(&vm->unused); 209 207 INIT_LIST_HEAD(&vm->used); 210 208 block = kzalloc(sizeof(*block), GFP_KERNEL); ··· 240 242 241 243 /* free allocated page table pages */ 242 244 for (i = 0; i < CT_PTP_NUM; i++) 243 - kfree(vm->ptp[i]); 245 + snd_dma_free_pages(&vm->ptp[i]); 244 246 245 247 vm->size = 0; 246 248
+5 -3
sound/pci/ctxfi/ctvmem.h
··· 22 22 23 23 #include <linux/mutex.h> 24 24 #include <linux/list.h> 25 + #include <linux/pci.h> 26 + #include <sound/memalloc.h> 25 27 26 28 /* The chip can handle the page table of 4k pages 27 29 * (emu20k1 can handle even 8k pages, but we don't use it right now) ··· 43 41 44 42 /* Virtual memory management object for card device */ 45 43 struct ct_vm { 46 - void *ptp[CT_PTP_NUM]; /* Device page table pages */ 44 + struct snd_dma_buffer ptp[CT_PTP_NUM]; /* Device page table pages */ 47 45 unsigned int size; /* Available addr space in bytes */ 48 46 struct list_head unused; /* List of unused blocks */ 49 47 struct list_head used; /* List of used blocks */ ··· 54 52 int size); 55 53 /* Unmap device logical addr area. */ 56 54 void (*unmap)(struct ct_vm *, struct ct_vm_block *block); 57 - void *(*get_ptp_virt)(struct ct_vm *vm, int index); 55 + dma_addr_t (*get_ptp_phys)(struct ct_vm *vm, int index); 58 56 }; 59 57 60 - int ct_vm_create(struct ct_vm **rvm); 58 + int ct_vm_create(struct ct_vm **rvm, struct pci_dev *pci); 61 59 void ct_vm_destroy(struct ct_vm *vm); 62 60 63 61 #endif /* CTVMEM_H */