summaryrefslogtreecommitdiff
path: root/arch/sh/cchips/voyagergx/consistent.c
blob: 07e8b9c5a5314ed437036ca54369b9c6ef61276e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * arch/sh/cchips/voyagergx/consistent.c
 *
 * Copyright (C) 2004  Paul Mundt
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#include <linux/mm.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/device.h>
#include <asm/io.h>


struct voya_alloc_entry {
	struct list_head list;
	unsigned long ofs;
	unsigned long len;
};

static DEFINE_SPINLOCK(voya_list_lock);
static LIST_HEAD(voya_alloc_list);

#define OHCI_SRAM_START	0xb0000000
#define OHCI_HCCA_SIZE	0x100
#define OHCI_SRAM_SIZE	0x10000

#define VOYAGER_OHCI_NAME	"voyager-ohci"

void *voyagergx_consistent_alloc(struct device *dev, size_t size,
				 dma_addr_t *handle, gfp_t flag)
{
	struct list_head *list = &voya_alloc_list;
	struct voya_alloc_entry *entry;
	unsigned long start, end;
	unsigned long flags;

	/*
	 * The SM501 contains an integrated 8051 with its own SRAM.
	 * Devices within the cchip can all hook into the 8051 SRAM.
	 * We presently use this for the OHCI.
	 *
	 * Everything else goes through consistent_alloc().
	 */
	if (!dev || strcmp(dev->driver->name, VOYAGER_OHCI_NAME))
		return NULL;

	start = OHCI_SRAM_START + OHCI_HCCA_SIZE;

	entry = kmalloc(sizeof(struct voya_alloc_entry), GFP_ATOMIC);
	if (!entry)
		return ERR_PTR(-ENOMEM);

	entry->len = (size + 15) & ~15;

	/*
	 * The basis for this allocator is dwmw2's malloc.. the
	 * Matrox allocator :-)
	 */
	spin_lock_irqsave(&voya_list_lock, flags);
	list_for_each(list, &voya_alloc_list) {
		struct voya_alloc_entry *p;

		p = list_entry(list, struct voya_alloc_entry, list);

		if (p->ofs - start >= size)
			goto out;

		start = p->ofs + p->len;
	}

	end  = start + (OHCI_SRAM_SIZE  - OHCI_HCCA_SIZE);
	list = &voya_alloc_list;

	if (end - start >= size) {
out:
		entry->ofs = start;
		list_add_tail(&entry->list, list);
		spin_unlock_irqrestore(&voya_list_lock, flags);

		*handle = start;
		return (void *)start;
	}

	kfree(entry);
	spin_unlock_irqrestore(&voya_list_lock, flags);

	return ERR_PTR(-EINVAL);
}

int voyagergx_consistent_free(struct device *dev, size_t size,
			      void *vaddr, dma_addr_t handle)
{
	struct voya_alloc_entry *entry;
	unsigned long flags;

	if (!dev || strcmp(dev->driver->name, VOYAGER_OHCI_NAME))
		return -EINVAL;

	spin_lock_irqsave(&voya_list_lock, flags);
	list_for_each_entry(entry, &voya_alloc_list, list) {
		if (entry->ofs != handle)
			continue;

		list_del(&entry->list);
		kfree(entry);

		break;
	}
	spin_unlock_irqrestore(&voya_list_lock, flags);

	return 0;
}

EXPORT_SYMBOL(voyagergx_consistent_alloc);
EXPORT_SYMBOL(voyagergx_consistent_free);