summaryrefslogtreecommitdiff
path: root/arch/csky/mm/tcm.c
blob: ddeb36328819960ac275ca1febf13fc4ad628677 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// SPDX-License-Identifier: GPL-2.0

#include <linux/highmem.h>
#include <linux/genalloc.h>
#include <asm/tlbflush.h>
#include <asm/fixmap.h>

#if (CONFIG_ITCM_RAM_BASE == 0xffffffff)
#error "You should define ITCM_RAM_BASE"
#endif

#ifdef CONFIG_HAVE_DTCM
#if (CONFIG_DTCM_RAM_BASE == 0xffffffff)
#error "You should define DTCM_RAM_BASE"
#endif

#if (CONFIG_DTCM_RAM_BASE == CONFIG_ITCM_RAM_BASE)
#error "You should define correct DTCM_RAM_BASE"
#endif
#endif

extern char __tcm_start, __tcm_end, __dtcm_start;

static struct gen_pool *tcm_pool;

static void __init tcm_mapping_init(void)
{
	pte_t *tcm_pte;
	unsigned long vaddr, paddr;
	int i;

	paddr = CONFIG_ITCM_RAM_BASE;

	if (pfn_valid(PFN_DOWN(CONFIG_ITCM_RAM_BASE)))
		goto panic;

#ifndef CONFIG_HAVE_DTCM
	for (i = 0; i < TCM_NR_PAGES; i++) {
#else
	for (i = 0; i < CONFIG_ITCM_NR_PAGES; i++) {
#endif
		vaddr = __fix_to_virt(FIX_TCM - i);

		tcm_pte =
			pte_offset_kernel((pmd_t *)pgd_offset_k(vaddr), vaddr);

		set_pte(tcm_pte, pfn_pte(__phys_to_pfn(paddr), PAGE_KERNEL));

		flush_tlb_one(vaddr);

		paddr = paddr + PAGE_SIZE;
	}

#ifdef CONFIG_HAVE_DTCM
	if (pfn_valid(PFN_DOWN(CONFIG_DTCM_RAM_BASE)))
		goto panic;

	paddr = CONFIG_DTCM_RAM_BASE;

	for (i = 0; i < CONFIG_DTCM_NR_PAGES; i++) {
		vaddr = __fix_to_virt(FIX_TCM - CONFIG_ITCM_NR_PAGES - i);

		tcm_pte =
			pte_offset_kernel((pmd_t *) pgd_offset_k(vaddr), vaddr);

		set_pte(tcm_pte, pfn_pte(__phys_to_pfn(paddr), PAGE_KERNEL));

		flush_tlb_one(vaddr);

		paddr = paddr + PAGE_SIZE;
	}
#endif

#ifndef CONFIG_HAVE_DTCM
	memcpy((void *)__fix_to_virt(FIX_TCM),
				&__tcm_start, &__tcm_end - &__tcm_start);

	pr_info("%s: mapping tcm va:0x%08lx to pa:0x%08x\n",
			__func__, __fix_to_virt(FIX_TCM), CONFIG_ITCM_RAM_BASE);

	pr_info("%s: __tcm_start va:0x%08lx size:%d\n",
			__func__, (unsigned long)&__tcm_start, &__tcm_end - &__tcm_start);
#else
	memcpy((void *)__fix_to_virt(FIX_TCM),
				&__tcm_start, &__dtcm_start - &__tcm_start);

	pr_info("%s: mapping itcm va:0x%08lx to pa:0x%08x\n",
			__func__, __fix_to_virt(FIX_TCM), CONFIG_ITCM_RAM_BASE);

	pr_info("%s: __itcm_start va:0x%08lx size:%d\n",
			__func__, (unsigned long)&__tcm_start, &__dtcm_start - &__tcm_start);

	memcpy((void *)__fix_to_virt(FIX_TCM - CONFIG_ITCM_NR_PAGES),
				&__dtcm_start, &__tcm_end - &__dtcm_start);

	pr_info("%s: mapping dtcm va:0x%08lx to pa:0x%08x\n",
			__func__, __fix_to_virt(FIX_TCM - CONFIG_ITCM_NR_PAGES),
						CONFIG_DTCM_RAM_BASE);

	pr_info("%s: __dtcm_start va:0x%08lx size:%d\n",
			__func__, (unsigned long)&__dtcm_start, &__tcm_end - &__dtcm_start);

#endif
	return;
panic:
	panic("TCM init error");
}

void *tcm_alloc(size_t len)
{
	unsigned long vaddr;

	if (!tcm_pool)
		return NULL;

	vaddr = gen_pool_alloc(tcm_pool, len);
	if (!vaddr)
		return NULL;

	return (void *) vaddr;
}
EXPORT_SYMBOL(tcm_alloc);

void tcm_free(void *addr, size_t len)
{
	gen_pool_free(tcm_pool, (unsigned long) addr, len);
}
EXPORT_SYMBOL(tcm_free);

static int __init tcm_setup_pool(void)
{
#ifndef CONFIG_HAVE_DTCM
	u32 pool_size = (u32) (TCM_NR_PAGES * PAGE_SIZE)
				- (u32) (&__tcm_end - &__tcm_start);

	u32 tcm_pool_start = __fix_to_virt(FIX_TCM)
				+ (u32) (&__tcm_end - &__tcm_start);
#else
	u32 pool_size = (u32) (CONFIG_DTCM_NR_PAGES * PAGE_SIZE)
				- (u32) (&__tcm_end - &__dtcm_start);

	u32 tcm_pool_start = __fix_to_virt(FIX_TCM - CONFIG_ITCM_NR_PAGES)
				+ (u32) (&__tcm_end - &__dtcm_start);
#endif
	int ret;

	tcm_pool = gen_pool_create(2, -1);

	ret = gen_pool_add(tcm_pool, tcm_pool_start, pool_size, -1);
	if (ret) {
		pr_err("%s: gen_pool add failed!\n", __func__);
		return ret;
	}

	pr_info("%s: Added %d bytes @ 0x%08x to memory pool\n",
		__func__, pool_size, tcm_pool_start);

	return 0;
}

static int __init tcm_init(void)
{
	tcm_mapping_init();

	tcm_setup_pool();

	return 0;
}
arch_initcall(tcm_init);