aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/gcx/gccore/gcmem.c
blob: b3b01d430d256e2681abca97d740d17b119d2554 (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
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
/*
 * gcmain.c
 *
 * Copyright (C) 2010-2011 Vivante Corporation.
 *
 * This package is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <linux/pagemap.h>
#include <linux/dma-mapping.h>
#include "gcmain.h"

#define GCZONE_NONE		0
#define GCZONE_ALL		(~0U)
#define GCZONE_CACHED		(1 << 0)
#define GCZONE_NONCACHED	(1 << 1)
#define GCZONE_FLUSH		(1 << 2)

GCDBG_FILTERDEF(mem, GCZONE_NONE,
		"cached",
		"noncached",
		"flush")

enum gcerror gc_alloc_noncached(struct gcpage *p, unsigned int size)
{
	enum gcerror gcerror;

	GCENTERARG(GCZONE_NONCACHED, "p = 0x%08X\n", (unsigned int) p);

	p->pages = NULL;
	p->order = 0;
	p->size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
	p->logical = NULL;
	p->physical = ~0UL;

	GCDBG(GCZONE_NONCACHED, "requested size=%d\n", size);
	GCDBG(GCZONE_NONCACHED, "aligned size=%d\n", p->size);

	p->logical = dma_alloc_coherent(NULL, p->size, &p->physical,
						GFP_KERNEL);
	if (!p->logical) {
		GCERR("failed to allocate memory\n");
		gcerror = GCERR_OOPM;
		goto exit;
	}

	GCDBG(GCZONE_NONCACHED, "logical=0x%08X\n",
		(unsigned int) p->logical);
	GCDBG(GCZONE_NONCACHED, "physical=0x%08X\n",
		(unsigned int) p->physical);

	GCEXIT(GCZONE_NONCACHED);
	return GCERR_NONE;

exit:
	gc_free_noncached(p);

	GCEXITARG(GCZONE_NONCACHED, "gcerror = 0x%08X\n", gcerror);
	return gcerror;
}

void gc_free_noncached(struct gcpage *p)
{
	GCENTERARG(GCZONE_NONCACHED, "p = 0x%08X\n", (unsigned int) p);

	if (p->logical != NULL) {
		dma_free_coherent(NULL, p->size, p->logical, p->physical);
		p->logical = NULL;
	}

	p->physical = ~0UL;
	p->size = 0;

	GCEXIT(GCZONE_NONCACHED);
}

enum gcerror gc_alloc_cached(struct gcpage *p, unsigned int size)
{
	enum gcerror gcerror;
	struct page *pages;
	int count;

	GCENTERARG(GCZONE_CACHED, "p = 0x%08X\n", (unsigned int) p);

	p->order = get_order(size);
	p->pages = NULL;
	p->size = (1 << p->order) * PAGE_SIZE;
	p->logical = NULL;
	p->physical = ~0UL;

	GCDBG(GCZONE_CACHED, "requested size=%d\n", size);
	GCDBG(GCZONE_CACHED, "aligned size=%d\n", p->size);

	p->pages = alloc_pages(GFP_KERNEL, p->order);
	if (p->pages == NULL) {
		GCERR("failed to allocate memory\n");
		gcerror = GCERR_OOPM;
		goto exit;
	}

	p->physical = page_to_phys(p->pages);
	p->logical = (unsigned int *) page_address(p->pages);

	if (p->logical == NULL) {
		GCERR("failed to retrieve page virtual address\n");
		gcerror = GCERR_PMMAP;
		goto exit;
	}

	/* Reserve pages. */
	pages = p->pages;
	count = p->size / PAGE_SIZE;

	while (count--)
		SetPageReserved(pages++);

	GCDBG(GCZONE_CACHED, "page array=0x%08X\n", (unsigned int) p->pages);
	GCDBG(GCZONE_CACHED, "logical=0x%08X\n", (unsigned int) p->logical);
	GCDBG(GCZONE_CACHED, "physical=0x%08X\n", (unsigned int) p->physical);

	GCEXIT(GCZONE_CACHED);
	return GCERR_NONE;

exit:
	gc_free_cached(p);

	GCEXITARG(GCZONE_CACHED, "gcerror = 0x%08X\n", gcerror);
	return gcerror;
}

void gc_free_cached(struct gcpage *p)
{
	GCENTERARG(GCZONE_CACHED, "p = 0x%08X\n", (unsigned int) p);

	GCDBG(GCZONE_CACHED, "page array=0x%08X\n", (unsigned int) p->pages);
	GCDBG(GCZONE_CACHED, "logical=0x%08X\n", (unsigned int) p->logical);
	GCDBG(GCZONE_CACHED, "physical=0x%08X\n", (unsigned int) p->physical);
	GCDBG(GCZONE_CACHED, "size=%d\n", p->size);

	if (p->logical != NULL) {
		struct page *pages;
		int count;

		pages = p->pages;
		count = p->size / PAGE_SIZE;

		while (count--)
			ClearPageReserved(pages++);

		p->logical = NULL;
	}

	if (p->pages != NULL) {
		__free_pages(p->pages, p->order);
		p->pages = NULL;
	}

	p->physical = ~0UL;
	p->order = 0;
	p->size = 0;

	GCEXIT(GCZONE_CACHED);
}

void gc_flush_cached(struct gcpage *p)
{
	GCENTERARG(GCZONE_FLUSH, "p = 0x%08X\n", (unsigned int) p);

	dmac_flush_range(p->logical, (unsigned char *) p->logical + p->size);
	outer_flush_range(p->physical, p->physical + p->size);

	GCEXIT(GCZONE_FLUSH);
}

void gc_flush_region(unsigned int physical, void *logical,
			unsigned int offset, unsigned int size)
{
	unsigned char *startlog;
	unsigned int startphys;

	GCENTER(GCZONE_FLUSH);

	GCDBG(GCZONE_FLUSH, "logical=0x%08X\n", (unsigned int) logical);
	GCDBG(GCZONE_FLUSH, "physical=0x%08X\n", physical);
	GCDBG(GCZONE_FLUSH, "offset=%d\n", offset);
	GCDBG(GCZONE_FLUSH, "size=%d\n", size);

	startlog = (unsigned char *) logical + offset;
	startphys = physical + offset;

	dmac_flush_range(startlog, startlog + size);
	outer_flush_range(startphys, startphys + size);

	GCEXIT(GCZONE_FLUSH);
}