File Index
Files and directories
Sign in
FILE

ctvmem.c

File preview with syntax highlighting.

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