Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved. |
| 3 | * |
| 4 | * This source file is released under GPL v2 license (no other versions). |
| 5 | * See the COPYING file included in the main directory of this source |
| 6 | * distribution for the license terms and conditions. |
| 7 | * |
| 8 | * @File ctvmem.c |
| 9 | * |
| 10 | * @Brief |
| 11 | * This file contains the implementation of virtual memory management object |
| 12 | * for card device. |
| 13 | * |
| 14 | * @Author Liu Chun |
| 15 | * @Date Apr 1 2008 |
| 16 | */ |
| 17 | |
| 18 | #include "ctvmem.h" |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/mm.h> |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 21 | #include <linux/io.h> |
Takashi Iwai | c76157d | 2009-06-02 15:26:19 +0200 | [diff] [blame] | 22 | #include <sound/pcm.h> |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 23 | |
Takashi Iwai | cd391e2 | 2009-06-02 15:04:29 +0200 | [diff] [blame] | 24 | #define CT_PTES_PER_PAGE (CT_PAGE_SIZE / sizeof(void *)) |
| 25 | #define CT_ADDRS_PER_PAGE (CT_PTES_PER_PAGE * CT_PAGE_SIZE) |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 26 | |
| 27 | /* * |
| 28 | * Find or create vm block based on requested @size. |
| 29 | * @size must be page aligned. |
| 30 | * */ |
| 31 | static struct ct_vm_block * |
| 32 | get_vm_block(struct ct_vm *vm, unsigned int size) |
| 33 | { |
Takashi Iwai | 514eef9 | 2009-06-08 14:57:57 +0200 | [diff] [blame] | 34 | struct ct_vm_block *block = NULL, *entry; |
| 35 | struct list_head *pos; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 36 | |
Takashi Iwai | c76157d | 2009-06-02 15:26:19 +0200 | [diff] [blame] | 37 | size = CT_PAGE_ALIGN(size); |
| 38 | if (size > vm->size) { |
Masanari Iida | 8a236f3 | 2012-02-23 23:23:06 +0900 | [diff] [blame] | 39 | printk(KERN_ERR "ctxfi: Fail! No sufficient device virtual " |
Takashi Iwai | c76157d | 2009-06-02 15:26:19 +0200 | [diff] [blame] | 40 | "memory space available!\n"); |
| 41 | return NULL; |
| 42 | } |
| 43 | |
Takashi Iwai | 8a4259b | 2009-06-02 08:40:51 +0200 | [diff] [blame] | 44 | mutex_lock(&vm->lock); |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 45 | list_for_each(pos, &vm->unused) { |
| 46 | entry = list_entry(pos, struct ct_vm_block, list); |
| 47 | if (entry->size >= size) |
| 48 | break; /* found a block that is big enough */ |
| 49 | } |
| 50 | if (pos == &vm->unused) |
Takashi Iwai | 8a4259b | 2009-06-02 08:40:51 +0200 | [diff] [blame] | 51 | goto out; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 52 | |
| 53 | if (entry->size == size) { |
| 54 | /* Move the vm node from unused list to used list directly */ |
Kirill A. Shutemov | 9d4ed9e | 2011-03-16 00:53:24 +0200 | [diff] [blame] | 55 | list_move(&entry->list, &vm->used); |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 56 | vm->size -= size; |
Takashi Iwai | 8a4259b | 2009-06-02 08:40:51 +0200 | [diff] [blame] | 57 | block = entry; |
| 58 | goto out; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | block = kzalloc(sizeof(*block), GFP_KERNEL); |
Takashi Iwai | 35ebf6e | 2009-07-22 17:12:34 +0200 | [diff] [blame] | 62 | if (!block) |
Takashi Iwai | 8a4259b | 2009-06-02 08:40:51 +0200 | [diff] [blame] | 63 | goto out; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 64 | |
| 65 | block->addr = entry->addr; |
| 66 | block->size = size; |
| 67 | list_add(&block->list, &vm->used); |
| 68 | entry->addr += size; |
| 69 | entry->size -= size; |
| 70 | vm->size -= size; |
| 71 | |
Takashi Iwai | 8a4259b | 2009-06-02 08:40:51 +0200 | [diff] [blame] | 72 | out: |
| 73 | mutex_unlock(&vm->lock); |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 74 | return block; |
| 75 | } |
| 76 | |
| 77 | static void put_vm_block(struct ct_vm *vm, struct ct_vm_block *block) |
| 78 | { |
Takashi Iwai | 514eef9 | 2009-06-08 14:57:57 +0200 | [diff] [blame] | 79 | struct ct_vm_block *entry, *pre_ent; |
| 80 | struct list_head *pos, *pre; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 81 | |
Takashi Iwai | c76157d | 2009-06-02 15:26:19 +0200 | [diff] [blame] | 82 | block->size = CT_PAGE_ALIGN(block->size); |
| 83 | |
Takashi Iwai | 8a4259b | 2009-06-02 08:40:51 +0200 | [diff] [blame] | 84 | mutex_lock(&vm->lock); |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 85 | list_del(&block->list); |
| 86 | vm->size += block->size; |
| 87 | |
| 88 | list_for_each(pos, &vm->unused) { |
| 89 | entry = list_entry(pos, struct ct_vm_block, list); |
| 90 | if (entry->addr >= (block->addr + block->size)) |
| 91 | break; /* found a position */ |
| 92 | } |
| 93 | if (pos == &vm->unused) { |
| 94 | list_add_tail(&block->list, &vm->unused); |
| 95 | entry = block; |
| 96 | } else { |
| 97 | if ((block->addr + block->size) == entry->addr) { |
| 98 | entry->addr = block->addr; |
| 99 | entry->size += block->size; |
| 100 | kfree(block); |
| 101 | } else { |
| 102 | __list_add(&block->list, pos->prev, pos); |
| 103 | entry = block; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | pos = &entry->list; |
| 108 | pre = pos->prev; |
| 109 | while (pre != &vm->unused) { |
| 110 | entry = list_entry(pos, struct ct_vm_block, list); |
| 111 | pre_ent = list_entry(pre, struct ct_vm_block, list); |
| 112 | if ((pre_ent->addr + pre_ent->size) > entry->addr) |
| 113 | break; |
| 114 | |
| 115 | pre_ent->size += entry->size; |
| 116 | list_del(pos); |
| 117 | kfree(entry); |
| 118 | pos = pre; |
| 119 | pre = pos->prev; |
| 120 | } |
Takashi Iwai | 8a4259b | 2009-06-02 08:40:51 +0200 | [diff] [blame] | 121 | mutex_unlock(&vm->lock); |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* Map host addr (kmalloced/vmalloced) to device logical addr. */ |
| 125 | static struct ct_vm_block * |
Takashi Iwai | c76157d | 2009-06-02 15:26:19 +0200 | [diff] [blame] | 126 | ct_vm_map(struct ct_vm *vm, struct snd_pcm_substream *substream, int size) |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 127 | { |
Takashi Iwai | c76157d | 2009-06-02 15:26:19 +0200 | [diff] [blame] | 128 | struct ct_vm_block *block; |
| 129 | unsigned int pte_start; |
| 130 | unsigned i, pages; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 131 | unsigned long *ptp; |
| 132 | |
Takashi Iwai | c76157d | 2009-06-02 15:26:19 +0200 | [diff] [blame] | 133 | block = get_vm_block(vm, size); |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 134 | if (block == NULL) { |
Takashi Iwai | b3e0afe | 2009-05-14 15:19:30 +0200 | [diff] [blame] | 135 | printk(KERN_ERR "ctxfi: No virtual memory block that is big " |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 136 | "enough to allocate!\n"); |
| 137 | return NULL; |
| 138 | } |
| 139 | |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 140 | ptp = (unsigned long *)vm->ptp[0].area; |
Takashi Iwai | cd391e2 | 2009-06-02 15:04:29 +0200 | [diff] [blame] | 141 | pte_start = (block->addr >> CT_PAGE_SHIFT); |
Takashi Iwai | c76157d | 2009-06-02 15:26:19 +0200 | [diff] [blame] | 142 | pages = block->size >> CT_PAGE_SHIFT; |
| 143 | for (i = 0; i < pages; i++) { |
| 144 | unsigned long addr; |
| 145 | addr = snd_pcm_sgbuf_get_addr(substream, i << CT_PAGE_SHIFT); |
| 146 | ptp[pte_start + i] = addr; |
| 147 | } |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 148 | |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 149 | block->size = size; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 150 | return block; |
| 151 | } |
| 152 | |
| 153 | static void ct_vm_unmap(struct ct_vm *vm, struct ct_vm_block *block) |
| 154 | { |
| 155 | /* do unmapping */ |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 156 | put_vm_block(vm, block); |
| 157 | } |
| 158 | |
| 159 | /* * |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 160 | * return the host physical addr of the @index-th device |
| 161 | * page table page on success, or ~0UL on failure. |
| 162 | * The first returned ~0UL indicates the termination. |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 163 | * */ |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 164 | static dma_addr_t |
| 165 | ct_get_ptp_phys(struct ct_vm *vm, int index) |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 166 | { |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 167 | dma_addr_t addr; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 168 | |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 169 | addr = (index >= CT_PTP_NUM) ? ~0UL : vm->ptp[index].addr; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 170 | |
| 171 | return addr; |
| 172 | } |
| 173 | |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 174 | int ct_vm_create(struct ct_vm **rvm, struct pci_dev *pci) |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 175 | { |
| 176 | struct ct_vm *vm; |
| 177 | struct ct_vm_block *block; |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 178 | int i, err = 0; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 179 | |
| 180 | *rvm = NULL; |
| 181 | |
| 182 | vm = kzalloc(sizeof(*vm), GFP_KERNEL); |
Takashi Iwai | 35ebf6e | 2009-07-22 17:12:34 +0200 | [diff] [blame] | 183 | if (!vm) |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 184 | return -ENOMEM; |
| 185 | |
Takashi Iwai | 8a4259b | 2009-06-02 08:40:51 +0200 | [diff] [blame] | 186 | mutex_init(&vm->lock); |
| 187 | |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 188 | /* Allocate page table pages */ |
| 189 | for (i = 0; i < CT_PTP_NUM; i++) { |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 190 | err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, |
| 191 | snd_dma_pci_data(pci), |
| 192 | PAGE_SIZE, &vm->ptp[i]); |
| 193 | if (err < 0) |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 194 | break; |
| 195 | } |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 196 | if (err < 0) { |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 197 | /* no page table pages are allocated */ |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 198 | ct_vm_destroy(vm); |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 199 | return -ENOMEM; |
| 200 | } |
| 201 | vm->size = CT_ADDRS_PER_PAGE * i; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 202 | vm->map = ct_vm_map; |
| 203 | vm->unmap = ct_vm_unmap; |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 204 | vm->get_ptp_phys = ct_get_ptp_phys; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 205 | INIT_LIST_HEAD(&vm->unused); |
| 206 | INIT_LIST_HEAD(&vm->used); |
| 207 | block = kzalloc(sizeof(*block), GFP_KERNEL); |
| 208 | if (NULL != block) { |
| 209 | block->addr = 0; |
| 210 | block->size = vm->size; |
| 211 | list_add(&block->list, &vm->unused); |
| 212 | } |
| 213 | |
| 214 | *rvm = vm; |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /* The caller must ensure no mapping pages are being used |
| 219 | * by hardware before calling this function */ |
| 220 | void ct_vm_destroy(struct ct_vm *vm) |
| 221 | { |
| 222 | int i; |
Takashi Iwai | 514eef9 | 2009-06-08 14:57:57 +0200 | [diff] [blame] | 223 | struct list_head *pos; |
| 224 | struct ct_vm_block *entry; |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 225 | |
| 226 | /* free used and unused list nodes */ |
| 227 | while (!list_empty(&vm->used)) { |
| 228 | pos = vm->used.next; |
| 229 | list_del(pos); |
| 230 | entry = list_entry(pos, struct ct_vm_block, list); |
| 231 | kfree(entry); |
| 232 | } |
| 233 | while (!list_empty(&vm->unused)) { |
| 234 | pos = vm->unused.next; |
| 235 | list_del(pos); |
| 236 | entry = list_entry(pos, struct ct_vm_block, list); |
| 237 | kfree(entry); |
| 238 | } |
| 239 | |
| 240 | /* free allocated page table pages */ |
| 241 | for (i = 0; i < CT_PTP_NUM; i++) |
Jaroslav Kysela | 21956b6 | 2010-02-02 19:58:25 +0100 | [diff] [blame] | 242 | snd_dma_free_pages(&vm->ptp[i]); |
Wai Yew CHAY | 8cc7236 | 2009-05-14 08:05:58 +0200 | [diff] [blame] | 243 | |
| 244 | vm->size = 0; |
| 245 | |
| 246 | kfree(vm); |
| 247 | } |