blob: 38163f52dd5fd02c4b4a40f354e23733c8c82e99 [file] [log] [blame]
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001/**
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 CHAY8cc72362009-05-14 08:05:58 +020021#include <linux/io.h>
Takashi Iwaic76157d2009-06-02 15:26:19 +020022#include <sound/pcm.h>
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020023
Takashi Iwaicd391e22009-06-02 15:04:29 +020024#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 CHAY8cc72362009-05-14 08:05:58 +020026
27/* *
28 * Find or create vm block based on requested @size.
29 * @size must be page aligned.
30 * */
31static struct ct_vm_block *
32get_vm_block(struct ct_vm *vm, unsigned int size)
33{
Takashi Iwai514eef92009-06-08 14:57:57 +020034 struct ct_vm_block *block = NULL, *entry;
35 struct list_head *pos;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020036
Takashi Iwaic76157d2009-06-02 15:26:19 +020037 size = CT_PAGE_ALIGN(size);
38 if (size > vm->size) {
Sudip Mukherjee62afa852014-08-26 19:01:43 +053039 pr_err("ctxfi: Fail! No sufficient device virtual memory space available!\n");
Takashi Iwaic76157d2009-06-02 15:26:19 +020040 return NULL;
41 }
42
Takashi Iwai8a4259b2009-06-02 08:40:51 +020043 mutex_lock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020044 list_for_each(pos, &vm->unused) {
45 entry = list_entry(pos, struct ct_vm_block, list);
46 if (entry->size >= size)
47 break; /* found a block that is big enough */
48 }
49 if (pos == &vm->unused)
Takashi Iwai8a4259b2009-06-02 08:40:51 +020050 goto out;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020051
52 if (entry->size == size) {
53 /* Move the vm node from unused list to used list directly */
Kirill A. Shutemov9d4ed9e2011-03-16 00:53:24 +020054 list_move(&entry->list, &vm->used);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020055 vm->size -= size;
Takashi Iwai8a4259b2009-06-02 08:40:51 +020056 block = entry;
57 goto out;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020058 }
59
60 block = kzalloc(sizeof(*block), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +020061 if (!block)
Takashi Iwai8a4259b2009-06-02 08:40:51 +020062 goto out;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020063
64 block->addr = entry->addr;
65 block->size = size;
66 list_add(&block->list, &vm->used);
67 entry->addr += size;
68 entry->size -= size;
69 vm->size -= size;
70
Takashi Iwai8a4259b2009-06-02 08:40:51 +020071 out:
72 mutex_unlock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020073 return block;
74}
75
76static void put_vm_block(struct ct_vm *vm, struct ct_vm_block *block)
77{
Takashi Iwai514eef92009-06-08 14:57:57 +020078 struct ct_vm_block *entry, *pre_ent;
79 struct list_head *pos, *pre;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020080
Takashi Iwaic76157d2009-06-02 15:26:19 +020081 block->size = CT_PAGE_ALIGN(block->size);
82
Takashi Iwai8a4259b2009-06-02 08:40:51 +020083 mutex_lock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020084 list_del(&block->list);
85 vm->size += block->size;
86
87 list_for_each(pos, &vm->unused) {
88 entry = list_entry(pos, struct ct_vm_block, list);
89 if (entry->addr >= (block->addr + block->size))
90 break; /* found a position */
91 }
92 if (pos == &vm->unused) {
93 list_add_tail(&block->list, &vm->unused);
94 entry = block;
95 } else {
96 if ((block->addr + block->size) == entry->addr) {
97 entry->addr = block->addr;
98 entry->size += block->size;
99 kfree(block);
100 } else {
101 __list_add(&block->list, pos->prev, pos);
102 entry = block;
103 }
104 }
105
106 pos = &entry->list;
107 pre = pos->prev;
108 while (pre != &vm->unused) {
109 entry = list_entry(pos, struct ct_vm_block, list);
110 pre_ent = list_entry(pre, struct ct_vm_block, list);
111 if ((pre_ent->addr + pre_ent->size) > entry->addr)
112 break;
113
114 pre_ent->size += entry->size;
115 list_del(pos);
116 kfree(entry);
117 pos = pre;
118 pre = pos->prev;
119 }
Takashi Iwai8a4259b2009-06-02 08:40:51 +0200120 mutex_unlock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200121}
122
123/* Map host addr (kmalloced/vmalloced) to device logical addr. */
124static struct ct_vm_block *
Takashi Iwaic76157d2009-06-02 15:26:19 +0200125ct_vm_map(struct ct_vm *vm, struct snd_pcm_substream *substream, int size)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200126{
Takashi Iwaic76157d2009-06-02 15:26:19 +0200127 struct ct_vm_block *block;
128 unsigned int pte_start;
129 unsigned i, pages;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200130 unsigned long *ptp;
131
Takashi Iwaic76157d2009-06-02 15:26:19 +0200132 block = get_vm_block(vm, size);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200133 if (block == NULL) {
Sudip Mukherjee62afa852014-08-26 19:01:43 +0530134 pr_err("ctxfi: No virtual memory block that is big enough to allocate!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200135 return NULL;
136 }
137
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100138 ptp = (unsigned long *)vm->ptp[0].area;
Takashi Iwaicd391e22009-06-02 15:04:29 +0200139 pte_start = (block->addr >> CT_PAGE_SHIFT);
Takashi Iwaic76157d2009-06-02 15:26:19 +0200140 pages = block->size >> CT_PAGE_SHIFT;
141 for (i = 0; i < pages; i++) {
142 unsigned long addr;
143 addr = snd_pcm_sgbuf_get_addr(substream, i << CT_PAGE_SHIFT);
144 ptp[pte_start + i] = addr;
145 }
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200146
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200147 block->size = size;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200148 return block;
149}
150
151static void ct_vm_unmap(struct ct_vm *vm, struct ct_vm_block *block)
152{
153 /* do unmapping */
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200154 put_vm_block(vm, block);
155}
156
157/* *
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100158 * return the host physical addr of the @index-th device
159 * page table page on success, or ~0UL on failure.
160 * The first returned ~0UL indicates the termination.
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200161 * */
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100162static dma_addr_t
163ct_get_ptp_phys(struct ct_vm *vm, int index)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200164{
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100165 dma_addr_t addr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200166
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100167 addr = (index >= CT_PTP_NUM) ? ~0UL : vm->ptp[index].addr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200168
169 return addr;
170}
171
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100172int ct_vm_create(struct ct_vm **rvm, struct pci_dev *pci)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200173{
174 struct ct_vm *vm;
175 struct ct_vm_block *block;
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100176 int i, err = 0;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200177
178 *rvm = NULL;
179
180 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200181 if (!vm)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200182 return -ENOMEM;
183
Takashi Iwai8a4259b2009-06-02 08:40:51 +0200184 mutex_init(&vm->lock);
185
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200186 /* Allocate page table pages */
187 for (i = 0; i < CT_PTP_NUM; i++) {
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100188 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
189 snd_dma_pci_data(pci),
190 PAGE_SIZE, &vm->ptp[i]);
191 if (err < 0)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200192 break;
193 }
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100194 if (err < 0) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200195 /* no page table pages are allocated */
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100196 ct_vm_destroy(vm);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200197 return -ENOMEM;
198 }
199 vm->size = CT_ADDRS_PER_PAGE * i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200200 vm->map = ct_vm_map;
201 vm->unmap = ct_vm_unmap;
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100202 vm->get_ptp_phys = ct_get_ptp_phys;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200203 INIT_LIST_HEAD(&vm->unused);
204 INIT_LIST_HEAD(&vm->used);
205 block = kzalloc(sizeof(*block), GFP_KERNEL);
206 if (NULL != block) {
207 block->addr = 0;
208 block->size = vm->size;
209 list_add(&block->list, &vm->unused);
210 }
211
212 *rvm = vm;
213 return 0;
214}
215
216/* The caller must ensure no mapping pages are being used
217 * by hardware before calling this function */
218void ct_vm_destroy(struct ct_vm *vm)
219{
220 int i;
Takashi Iwai514eef92009-06-08 14:57:57 +0200221 struct list_head *pos;
222 struct ct_vm_block *entry;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200223
224 /* free used and unused list nodes */
225 while (!list_empty(&vm->used)) {
226 pos = vm->used.next;
227 list_del(pos);
228 entry = list_entry(pos, struct ct_vm_block, list);
229 kfree(entry);
230 }
231 while (!list_empty(&vm->unused)) {
232 pos = vm->unused.next;
233 list_del(pos);
234 entry = list_entry(pos, struct ct_vm_block, list);
235 kfree(entry);
236 }
237
238 /* free allocated page table pages */
239 for (i = 0; i < CT_PTP_NUM; i++)
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100240 snd_dma_free_pages(&vm->ptp[i]);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200241
242 vm->size = 0;
243
244 kfree(vm);
245}