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