2019-05-31 15:09:25 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2009-05-14 13:05:58 +07:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* @File ctvmem.c
|
|
|
|
*
|
|
|
|
* @Brief
|
|
|
|
* This file contains the implementation of virtual memory management object
|
|
|
|
* for card device.
|
|
|
|
*
|
|
|
|
* @Author Liu Chun
|
|
|
|
* @Date Apr 1 2008
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ctvmem.h"
|
2014-09-29 16:03:26 +07:00
|
|
|
#include "ctatc.h"
|
2009-05-14 13:05:58 +07:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/io.h>
|
2009-06-02 20:26:19 +07:00
|
|
|
#include <sound/pcm.h>
|
2009-05-14 13:05:58 +07:00
|
|
|
|
2009-06-02 20:04:29 +07:00
|
|
|
#define CT_PTES_PER_PAGE (CT_PAGE_SIZE / sizeof(void *))
|
|
|
|
#define CT_ADDRS_PER_PAGE (CT_PTES_PER_PAGE * CT_PAGE_SIZE)
|
2009-05-14 13:05:58 +07:00
|
|
|
|
|
|
|
/* *
|
|
|
|
* Find or create vm block based on requested @size.
|
|
|
|
* @size must be page aligned.
|
|
|
|
* */
|
|
|
|
static struct ct_vm_block *
|
2014-09-29 16:03:26 +07:00
|
|
|
get_vm_block(struct ct_vm *vm, unsigned int size, struct ct_atc *atc)
|
2009-05-14 13:05:58 +07:00
|
|
|
{
|
2009-06-08 19:57:57 +07:00
|
|
|
struct ct_vm_block *block = NULL, *entry;
|
|
|
|
struct list_head *pos;
|
2009-05-14 13:05:58 +07:00
|
|
|
|
2009-06-02 20:26:19 +07:00
|
|
|
size = CT_PAGE_ALIGN(size);
|
|
|
|
if (size > vm->size) {
|
2014-09-29 16:03:26 +07:00
|
|
|
dev_err(atc->card->dev,
|
|
|
|
"Fail! No sufficient device virtual memory space available!\n");
|
2009-06-02 20:26:19 +07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-02 13:40:51 +07:00
|
|
|
mutex_lock(&vm->lock);
|
2009-05-14 13:05:58 +07:00
|
|
|
list_for_each(pos, &vm->unused) {
|
|
|
|
entry = list_entry(pos, struct ct_vm_block, list);
|
|
|
|
if (entry->size >= size)
|
|
|
|
break; /* found a block that is big enough */
|
|
|
|
}
|
|
|
|
if (pos == &vm->unused)
|
2009-06-02 13:40:51 +07:00
|
|
|
goto out;
|
2009-05-14 13:05:58 +07:00
|
|
|
|
|
|
|
if (entry->size == size) {
|
|
|
|
/* Move the vm node from unused list to used list directly */
|
2011-03-16 05:53:24 +07:00
|
|
|
list_move(&entry->list, &vm->used);
|
2009-05-14 13:05:58 +07:00
|
|
|
vm->size -= size;
|
2009-06-02 13:40:51 +07:00
|
|
|
block = entry;
|
|
|
|
goto out;
|
2009-05-14 13:05:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
block = kzalloc(sizeof(*block), GFP_KERNEL);
|
2009-07-22 22:12:34 +07:00
|
|
|
if (!block)
|
2009-06-02 13:40:51 +07:00
|
|
|
goto out;
|
2009-05-14 13:05:58 +07:00
|
|
|
|
|
|
|
block->addr = entry->addr;
|
|
|
|
block->size = size;
|
|
|
|
list_add(&block->list, &vm->used);
|
|
|
|
entry->addr += size;
|
|
|
|
entry->size -= size;
|
|
|
|
vm->size -= size;
|
|
|
|
|
2009-06-02 13:40:51 +07:00
|
|
|
out:
|
|
|
|
mutex_unlock(&vm->lock);
|
2009-05-14 13:05:58 +07:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put_vm_block(struct ct_vm *vm, struct ct_vm_block *block)
|
|
|
|
{
|
2009-06-08 19:57:57 +07:00
|
|
|
struct ct_vm_block *entry, *pre_ent;
|
|
|
|
struct list_head *pos, *pre;
|
2009-05-14 13:05:58 +07:00
|
|
|
|
2009-06-02 20:26:19 +07:00
|
|
|
block->size = CT_PAGE_ALIGN(block->size);
|
|
|
|
|
2009-06-02 13:40:51 +07:00
|
|
|
mutex_lock(&vm->lock);
|
2009-05-14 13:05:58 +07:00
|
|
|
list_del(&block->list);
|
|
|
|
vm->size += block->size;
|
|
|
|
|
|
|
|
list_for_each(pos, &vm->unused) {
|
|
|
|
entry = list_entry(pos, struct ct_vm_block, list);
|
|
|
|
if (entry->addr >= (block->addr + block->size))
|
|
|
|
break; /* found a position */
|
|
|
|
}
|
|
|
|
if (pos == &vm->unused) {
|
|
|
|
list_add_tail(&block->list, &vm->unused);
|
|
|
|
entry = block;
|
|
|
|
} else {
|
|
|
|
if ((block->addr + block->size) == entry->addr) {
|
|
|
|
entry->addr = block->addr;
|
|
|
|
entry->size += block->size;
|
|
|
|
kfree(block);
|
|
|
|
} else {
|
|
|
|
__list_add(&block->list, pos->prev, pos);
|
|
|
|
entry = block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = &entry->list;
|
|
|
|
pre = pos->prev;
|
|
|
|
while (pre != &vm->unused) {
|
|
|
|
entry = list_entry(pos, struct ct_vm_block, list);
|
|
|
|
pre_ent = list_entry(pre, struct ct_vm_block, list);
|
|
|
|
if ((pre_ent->addr + pre_ent->size) > entry->addr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
pre_ent->size += entry->size;
|
|
|
|
list_del(pos);
|
|
|
|
kfree(entry);
|
|
|
|
pos = pre;
|
|
|
|
pre = pos->prev;
|
|
|
|
}
|
2009-06-02 13:40:51 +07:00
|
|
|
mutex_unlock(&vm->lock);
|
2009-05-14 13:05:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Map host addr (kmalloced/vmalloced) to device logical addr. */
|
|
|
|
static struct ct_vm_block *
|
2009-06-02 20:26:19 +07:00
|
|
|
ct_vm_map(struct ct_vm *vm, struct snd_pcm_substream *substream, int size)
|
2009-05-14 13:05:58 +07:00
|
|
|
{
|
2009-06-02 20:26:19 +07:00
|
|
|
struct ct_vm_block *block;
|
|
|
|
unsigned int pte_start;
|
|
|
|
unsigned i, pages;
|
2009-05-14 13:05:58 +07:00
|
|
|
unsigned long *ptp;
|
2014-09-29 16:03:26 +07:00
|
|
|
struct ct_atc *atc = snd_pcm_substream_chip(substream);
|
2009-05-14 13:05:58 +07:00
|
|
|
|
2014-09-29 16:03:26 +07:00
|
|
|
block = get_vm_block(vm, size, atc);
|
2009-05-14 13:05:58 +07:00
|
|
|
if (block == NULL) {
|
2014-09-29 16:03:26 +07:00
|
|
|
dev_err(atc->card->dev,
|
|
|
|
"No virtual memory block that is big enough to allocate!\n");
|
2009-05-14 13:05:58 +07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-03 01:58:25 +07:00
|
|
|
ptp = (unsigned long *)vm->ptp[0].area;
|
2009-06-02 20:04:29 +07:00
|
|
|
pte_start = (block->addr >> CT_PAGE_SHIFT);
|
2009-06-02 20:26:19 +07:00
|
|
|
pages = block->size >> CT_PAGE_SHIFT;
|
|
|
|
for (i = 0; i < pages; i++) {
|
|
|
|
unsigned long addr;
|
|
|
|
addr = snd_pcm_sgbuf_get_addr(substream, i << CT_PAGE_SHIFT);
|
|
|
|
ptp[pte_start + i] = addr;
|
|
|
|
}
|
2009-05-14 13:05:58 +07:00
|
|
|
|
|
|
|
block->size = size;
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ct_vm_unmap(struct ct_vm *vm, struct ct_vm_block *block)
|
|
|
|
{
|
|
|
|
/* do unmapping */
|
|
|
|
put_vm_block(vm, block);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* *
|
2010-02-03 01:58:25 +07:00
|
|
|
* return the host physical addr of the @index-th device
|
|
|
|
* page table page on success, or ~0UL on failure.
|
|
|
|
* The first returned ~0UL indicates the termination.
|
2009-05-14 13:05:58 +07:00
|
|
|
* */
|
2010-02-03 01:58:25 +07:00
|
|
|
static dma_addr_t
|
|
|
|
ct_get_ptp_phys(struct ct_vm *vm, int index)
|
2009-05-14 13:05:58 +07:00
|
|
|
{
|
2016-09-06 18:41:19 +07:00
|
|
|
return (index >= CT_PTP_NUM) ? ~0UL : vm->ptp[index].addr;
|
2009-05-14 13:05:58 +07:00
|
|
|
}
|
|
|
|
|
2010-02-03 01:58:25 +07:00
|
|
|
int ct_vm_create(struct ct_vm **rvm, struct pci_dev *pci)
|
2009-05-14 13:05:58 +07:00
|
|
|
{
|
|
|
|
struct ct_vm *vm;
|
|
|
|
struct ct_vm_block *block;
|
2010-02-03 01:58:25 +07:00
|
|
|
int i, err = 0;
|
2009-05-14 13:05:58 +07:00
|
|
|
|
|
|
|
*rvm = NULL;
|
|
|
|
|
|
|
|
vm = kzalloc(sizeof(*vm), GFP_KERNEL);
|
2009-07-22 22:12:34 +07:00
|
|
|
if (!vm)
|
2009-05-14 13:05:58 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2009-06-02 13:40:51 +07:00
|
|
|
mutex_init(&vm->lock);
|
|
|
|
|
2009-05-14 13:05:58 +07:00
|
|
|
/* Allocate page table pages */
|
|
|
|
for (i = 0; i < CT_PTP_NUM; i++) {
|
2010-02-03 01:58:25 +07:00
|
|
|
err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
|
2019-11-05 22:18:55 +07:00
|
|
|
&pci->dev,
|
2010-02-03 01:58:25 +07:00
|
|
|
PAGE_SIZE, &vm->ptp[i]);
|
|
|
|
if (err < 0)
|
2009-05-14 13:05:58 +07:00
|
|
|
break;
|
|
|
|
}
|
2010-02-03 01:58:25 +07:00
|
|
|
if (err < 0) {
|
2009-05-14 13:05:58 +07:00
|
|
|
/* no page table pages are allocated */
|
2010-02-03 01:58:25 +07:00
|
|
|
ct_vm_destroy(vm);
|
2009-05-14 13:05:58 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
vm->size = CT_ADDRS_PER_PAGE * i;
|
|
|
|
vm->map = ct_vm_map;
|
|
|
|
vm->unmap = ct_vm_unmap;
|
2010-02-03 01:58:25 +07:00
|
|
|
vm->get_ptp_phys = ct_get_ptp_phys;
|
2009-05-14 13:05:58 +07:00
|
|
|
INIT_LIST_HEAD(&vm->unused);
|
|
|
|
INIT_LIST_HEAD(&vm->used);
|
|
|
|
block = kzalloc(sizeof(*block), GFP_KERNEL);
|
|
|
|
if (NULL != block) {
|
|
|
|
block->addr = 0;
|
|
|
|
block->size = vm->size;
|
|
|
|
list_add(&block->list, &vm->unused);
|
|
|
|
}
|
|
|
|
|
|
|
|
*rvm = vm;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The caller must ensure no mapping pages are being used
|
|
|
|
* by hardware before calling this function */
|
|
|
|
void ct_vm_destroy(struct ct_vm *vm)
|
|
|
|
{
|
|
|
|
int i;
|
2009-06-08 19:57:57 +07:00
|
|
|
struct list_head *pos;
|
|
|
|
struct ct_vm_block *entry;
|
2009-05-14 13:05:58 +07:00
|
|
|
|
|
|
|
/* free used and unused list nodes */
|
|
|
|
while (!list_empty(&vm->used)) {
|
|
|
|
pos = vm->used.next;
|
|
|
|
list_del(pos);
|
|
|
|
entry = list_entry(pos, struct ct_vm_block, list);
|
|
|
|
kfree(entry);
|
|
|
|
}
|
|
|
|
while (!list_empty(&vm->unused)) {
|
|
|
|
pos = vm->unused.next;
|
|
|
|
list_del(pos);
|
|
|
|
entry = list_entry(pos, struct ct_vm_block, list);
|
|
|
|
kfree(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free allocated page table pages */
|
|
|
|
for (i = 0; i < CT_PTP_NUM; i++)
|
2010-02-03 01:58:25 +07:00
|
|
|
snd_dma_free_pages(&vm->ptp[i]);
|
2009-05-14 13:05:58 +07:00
|
|
|
|
|
|
|
vm->size = 0;
|
|
|
|
|
|
|
|
kfree(vm);
|
|
|
|
}
|