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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* arch/arm/mach-tegra/nvos/nvos_page.c
*
* Implementation of NvOsPage* APIs using the Linux page allocator
*
* Copyright (c) 2010, NVIDIA Corporation.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <linux/vmalloc.h>
#include <linux/highmem.h>
#include <asm/cacheflush.h>
#include <asm/tlbflush.h>
#include <asm/pgtable.h>
#include <asm/pgalloc.h>
#include "nvcommon.h"
#include "nvos.h"
#define nv_gfp_pool (GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN)
struct nvos_pagemap {
void *addr;
unsigned int nr_pages;
struct page *pages[1];
};
static void pagemap_flush_page(struct page *page)
{
#ifdef CONFIG_HIGHMEM
void *km = NULL;
if (!page_address(page)) {
km = kmap(page);
if (!km) {
pr_err("unable to map high page\n");
return;
}
}
#endif
__cpuc_flush_dcache_page(page_address(page));
outer_flush_range(page_to_phys(page), page_to_phys(page)+PAGE_SIZE);
dsb();
#ifdef CONFIG_HIGHMEM
if (km) kunmap(page);
#endif
}
static void nv_free_pages(struct nvos_pagemap *pm)
{
unsigned int i;
if (pm->addr) vm_unmap_ram(pm->addr, pm->nr_pages);
for (i=0; i<pm->nr_pages; i++) {
ClearPageReserved(pm->pages[i]);
__free_page(pm->pages[i]);
}
kfree(pm);
}
static struct nvos_pagemap *nv_alloc_pages(unsigned int count,
pgprot_t prot, bool contiguous, int create_mapping)
{
struct nvos_pagemap *pm;
size_t size;
unsigned int i = 0;
size = sizeof(struct nvos_pagemap) + sizeof(struct page *)*(count-1);
pm = kzalloc(size, GFP_KERNEL);
if (!pm)
return NULL;
if (count==1) contiguous = true;
if (contiguous) {
size_t order = get_order(count << PAGE_SHIFT);
struct page *compound_page;
compound_page = alloc_pages(nv_gfp_pool, order);
if (!compound_page) goto fail;
split_page(compound_page, order);
for (i=0; i<count; i++)
pm->pages[i] = nth_page(compound_page, i);
for ( ; i < (1<<order); i++)
__free_page(nth_page(compound_page, i));
} else {
for (i=0; i<count; i++) {
pm->pages[i] = alloc_page(nv_gfp_pool);
if (!pm->pages[i]) goto fail;
}
}
if (create_mapping) {
/* since the linear kernel mapping uses sections and super-
* sections rather than PTEs, it's not possible to overwrite
* it with the correct caching attributes, so use a local
* mapping */
pm->addr = vm_map_ram(pm->pages, count, -1, prot);
if (!pm->addr) {
pr_err("nv_alloc_pages fail to vmap contiguous area\n");
goto fail;
}
}
pm->nr_pages = count;
for (i=0; i<count; i++) {
SetPageReserved(pm->pages[i]);
pagemap_flush_page(pm->pages[i]);
}
return pm;
fail:
while (i) __free_page(pm->pages[--i]);
if (pm) kfree(pm);
return NULL;
}
NvError NvOsPageMap(NvOsPageAllocHandle desc, size_t offs,
size_t size, void **ptr)
{
struct nvos_pagemap *pm = (struct nvos_pagemap *)desc;
if (!desc || !ptr || !size)
return NvError_BadParameter;
if (pm->addr) *ptr = (void*)((unsigned long)pm->addr + offs);
else *ptr = NULL;
return (*ptr) ? NvSuccess : NvError_MemoryMapFailed;
}
struct page *NvOsPageGetPage(NvOsPageAllocHandle desc, size_t offs)
{
struct nvos_pagemap *pm = (struct nvos_pagemap *)desc;
if (!pm) return NULL;
offs >>= PAGE_SHIFT;
return (likely(offs<pm->nr_pages)) ? pm->pages[offs] : NULL;
}
NvOsPhysAddr NvOsPageAddress(NvOsPageAllocHandle desc, size_t offs)
{
struct nvos_pagemap *pm = (struct nvos_pagemap *)desc;
size_t index;
if (unlikely(!pm)) return (NvOsPhysAddr)0;
index = offs >> PAGE_SHIFT;
offs &= (PAGE_SIZE - 1);
return (NvOsPhysAddr)(page_to_phys(pm->pages[index]) + offs);
}
void NvOsPageUnmap(NvOsPageAllocHandle desc, void *ptr, size_t size)
{
return;
}
NvError NvOsPageAlloc(size_t size, NvOsMemAttribute attrib,
NvOsPageFlags flags, NvU32 protect, NvOsPageAllocHandle *desc)
{
struct nvos_pagemap *pm;
pgprot_t prot = pgprot_kernel;
size += PAGE_SIZE-1;
size >>= PAGE_SHIFT;
/* writeback is implemented as inner-cacheable only, since these
* allocators are only used to allocate buffers for DMA-driven
* clients, and the cost of L2 maintenance makes outer cacheability
* a net performance loss more often than not */
if (attrib == NvOsMemAttribute_WriteBack)
prot = pgprot_inner_writeback(prot);
else
prot = pgprot_writecombine(prot);
pm = nv_alloc_pages(size, prot, (flags==NvOsPageFlags_Contiguous), 1);
if (!pm) return NvError_InsufficientMemory;
*desc = (NvOsPageAllocHandle)pm;
return NvSuccess;
}
void NvOsPageFree(NvOsPageAllocHandle desc)
{
struct nvos_pagemap *pm = (struct nvos_pagemap *)desc;
if (pm) nv_free_pages(pm);
}
NvError NvOsPageLock(void *ptr, size_t size, NvU32 protect,
NvOsPageAllocHandle *descriptor)
{
return NvError_NotImplemented;
}
NvError NvOsPageMapIntoPtr(NvOsPageAllocHandle desc, void *ptr,
size_t offset, size_t size)
{
return NvError_NotImplemented;
}
|