summaryrefslogtreecommitdiffstats
path: root/bltsville/gcbv/mirror/gcmap.c
blob: 3f2b3f3b1e28f1fbf03705ebdebcaec70fc65802 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Copyright(c) 2012,
 * Texas Instruments, Inc. and Vivante Corporation.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Vivante Corporation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "gcbv.h"

#define GCZONE_NONE		0
#define GCZONE_ALL		(~0U)
#define GCZONE_MAPPING		(1 << 0)

GCDBG_FILTERDEF(map, GCZONE_NONE,
		"mapping")


/*******************************************************************************
 * Memory management.
 */

enum bverror do_map(struct bvbuffdesc *bvbuffdesc,
		    struct gcbatch *batch,
		    struct bvbuffmap **map)
{
	static const int mapsize
		= sizeof(struct bvbuffmap)
		+ sizeof(struct bvbuffmapinfo);

	enum bverror bverror;
	struct gccontext *gccontext = get_context();
	struct bvbuffmap *bvbuffmap;
	struct bvbuffmapinfo *bvbuffmapinfo;
	struct bvphysdesc *bvphysdesc;
	bool mappedbyothers;
	struct gcimap gcimap;
	struct gcschedunmap *gcschedunmap;

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

	/* Lock access to the mapping list. */
	GCLOCK(&gccontext->maplock);

	/* Try to find existing mapping. */
	bvbuffmap = bvbuffdesc->map;
	while (bvbuffmap != NULL) {
		if (bvbuffmap->bv_unmap == bv_unmap)
			break;
		bvbuffmap = bvbuffmap->nextmap;
	}

	/* Not mapped yet? */
	if (bvbuffmap == NULL) {
		/* New mapping, allocate a record. */
		if (gccontext->buffmapvac == NULL) {
			bvbuffmap = gcalloc(struct bvbuffmap, mapsize);
			if (bvbuffmap == NULL) {
				BVSETERROR(BVERR_OOM,
					   "failed to allocate mapping record");
				goto fail;
			}

			bvbuffmap->structsize = sizeof(struct bvbuffmap);
			bvbuffmap->bv_unmap = bv_unmap;
			bvbuffmap->handle = (unsigned long) (bvbuffmap + 1);
		} else {
			bvbuffmap = gccontext->buffmapvac;
			gccontext->buffmapvac = bvbuffmap->nextmap;
		}

		/* Setup buffer mapping. Here we need to check and make sure
		 * that the buffer starts at a location that is supported by
		 * the hw. If it is not, offset is computed and the buffer is
		 * extended by the value of the offset. */
		gcimap.gcerror = GCERR_NONE;
		gcimap.handle = 0;

		if (bvbuffdesc->auxtype == BVAT_PHYSDESC) {
			bvphysdesc = (struct bvphysdesc *) bvbuffdesc->auxptr;

			if (bvphysdesc->structsize <
			    STRUCTSIZE(bvphysdesc, pageoffset)) {
				BVSETERROR(BVERR_BUFFERDESC_VERS,
					   "unsupported bvphysdesc version");
				goto fail;
			}

			gcimap.buf.offset = bvphysdesc->pageoffset;
			gcimap.pagesize = bvphysdesc->pagesize;
			gcimap.pagearray = bvphysdesc->pagearray;
			gcimap.size = bvbuffdesc->length;

			GCDBG(GCZONE_MAPPING, "new mapping (%s):\n",
			      (batch == NULL) ? "explicit" : "implicit");
			GCDBG(GCZONE_MAPPING, "pagesize = %lu\n",
			      bvphysdesc->pagesize);
			GCDBG(GCZONE_MAPPING, "pagearray = 0x%08X\n",
			      (unsigned int) bvphysdesc->pagearray);
			GCDBG(GCZONE_MAPPING, "pageoffset = %lu\n",
			      bvphysdesc->pageoffset);
			GCDBG(GCZONE_MAPPING, "mapping size = %d\n",
			      gcimap.size);
		} else {
			gcimap.buf.logical = bvbuffdesc->virtaddr;
			gcimap.pagesize = 0;
			gcimap.pagearray = NULL;
			gcimap.size = bvbuffdesc->length;

			GCDBG(GCZONE_MAPPING, "new mapping (%s):\n",
			      (batch == NULL) ? "explicit" : "implicit");
			GCDBG(GCZONE_MAPPING, "specified virtaddr = 0x%08X\n",
			      (unsigned int) bvbuffdesc->virtaddr);
			GCDBG(GCZONE_MAPPING, "aligned virtaddr = 0x%08X\n",
			      (unsigned int) gcimap.buf.logical);
			GCDBG(GCZONE_MAPPING, "mapping size = %d\n",
			      gcimap.size);
		}

		gc_map_wrapper(&gcimap);
		if (gcimap.gcerror != GCERR_NONE) {
			BVSETERROR(BVERR_OOM,
				   "unable to allocate gccore memory");
			goto fail;
		}

		/* Set map handle. */
		bvbuffmapinfo = (struct bvbuffmapinfo *) bvbuffmap->handle;
		bvbuffmapinfo->handle = gcimap.handle;

		/* Initialize reference counters. */
		if (batch == NULL) {
			/* Explicit mapping. */
			bvbuffmapinfo->usermap = 1;
			bvbuffmapinfo->automap = 0;
		} else {
			/* Implicit mapping; if there are existing mappings
			 * from other implementations, mark this an explicit
			 * mapping as well. */
			mappedbyothers = (bvbuffdesc->map != NULL);
			GCDBG(GCZONE_MAPPING, "%smapped by others.\n",
			      mappedbyothers ? "" : "not ");

			bvbuffmapinfo->usermap = mappedbyothers ? 1 : 0;
			bvbuffmapinfo->automap = 1;
		}

		/* Add the record to the list of mappings. */
		bvbuffmap->nextmap = bvbuffdesc->map;
		bvbuffdesc->map = bvbuffmap;
	} else {
		/* Mapping already exists. */
		bvbuffmapinfo = (struct bvbuffmapinfo *) bvbuffmap->handle;

		/* Advance reference counters. */
		if (batch == NULL) {
			/* Explicit mapping. */
			GCDBG(GCZONE_MAPPING, "explicit map.\n");
			bvbuffmapinfo->usermap += 1;
		} else {
			/* Implicit mapping. */
			GCDBG(GCZONE_MAPPING, "implicit map.\n");
			bvbuffmapinfo->automap += 1;
		}

		GCDBG(GCZONE_MAPPING, "mapping exists.\n");
	}

	GCDBG(GCZONE_MAPPING, "bvbuffmap = 0x%08X\n",
		(unsigned int) bvbuffmap);
	GCDBG(GCZONE_MAPPING, "explicit count = %d\n",
		bvbuffmapinfo->usermap);
	GCDBG(GCZONE_MAPPING, "implicit count = %d\n",
		bvbuffmapinfo->automap);

	/* Schedule for unmapping. */
	if (batch != NULL) {
		if (list_empty(&gccontext->unmapvac)) {
			gcschedunmap = gcalloc(struct gcschedunmap,
					       sizeof(struct gcschedunmap));
			if (gcschedunmap == NULL) {
				BVSETERROR(BVERR_OOM,
					   "failed to schedule unmapping");
				goto fail;
			}
			list_add(&gcschedunmap->link, &batch->unmap);
		} else {
			struct list_head *head;
			head = gccontext->unmapvac.next;
			gcschedunmap = list_entry(head,
						  struct gcschedunmap,
						  link);
			list_move(&gcschedunmap->link, &batch->unmap);
		}

		gcschedunmap->handle = (unsigned long) bvbuffdesc;

		GCDBG(GCZONE_MAPPING, "scheduled for unmapping.\n");
	}

	/* Set the map pointer. */
	*map = bvbuffmap;

	/* Unlock access to the mapping list. */
	GCUNLOCK(&gccontext->maplock);

	GCEXITARG(GCZONE_MAPPING, "handle = 0x%08X\n",
		  bvbuffmapinfo->handle);
	return BVERR_NONE;

fail:
	if (bvbuffmap != NULL) {
		bvbuffmap->nextmap = gccontext->buffmapvac;
		gccontext->buffmapvac = bvbuffmap;
	}

	/* Unlock access to the mapping list. */
	GCUNLOCK(&gccontext->maplock);

	GCEXITARG(GCZONE_MAPPING, "bverror = %d\n", bverror);
	return bverror;
}

void do_unmap_implicit(struct gcbatch *batch)
{
	struct gccontext *gccontext = get_context();
	struct list_head *head, *temphead;
	struct gcschedunmap *gcschedunmap;
	struct bvbuffdesc *bvbuffdesc;
	struct bvbuffmap *prev, *bvbuffmap;
	struct bvbuffmapinfo *bvbuffmapinfo;

	GCENTER(GCZONE_MAPPING);

	/* Lock access to the mapping list. */
	GCLOCK(&gccontext->maplock);

	/* Scan scheduled unmappings and remove the ones that are still
	 * in use. */
	list_for_each_safe(head, temphead, &batch->unmap) {
		gcschedunmap = list_entry(head, struct gcschedunmap, link);

		/* Cast the handle. */
		bvbuffdesc = (struct bvbuffdesc *) gcschedunmap->handle;

		/* Find our mapping. */
		prev = NULL;
		bvbuffmap = bvbuffdesc->map;
		while (bvbuffmap != NULL) {
			if (bvbuffmap->bv_unmap == bv_unmap)
				break;
			prev = bvbuffmap;
			bvbuffmap = bvbuffmap->nextmap;
		}

		/* Not found? */
		if (bvbuffmap == NULL) {
			GCERR("mapping not found for bvbuffdesc 0x%08X.\n",
			      (unsigned int) bvbuffdesc);

			/* Remove scheduled unmapping. */
			list_move(head, &gccontext->unmapvac);
			continue;
		}

		/* Get the info structure. */
		bvbuffmapinfo = (struct bvbuffmapinfo *) bvbuffmap->handle;

		GCDBG(GCZONE_MAPPING, "head = 0x%08X\n",
		      (unsigned int) gcschedunmap);
		GCDBG(GCZONE_MAPPING, "  bvbuffmap = 0x%08X\n",
		      (unsigned int) bvbuffmap);
		GCDBG(GCZONE_MAPPING, "  handle = 0x%08X\n",
		      bvbuffmapinfo->handle);

		/* Implicit unmapping. */
		bvbuffmapinfo->automap -= 1;
		if (bvbuffmapinfo->automap < 0) {
			GCERR("implicit count negative.\n");
			bvbuffmapinfo->automap = 0;
		}

		GCDBG(GCZONE_MAPPING, "  explicit count = %d\n",
		      bvbuffmapinfo->usermap);
		GCDBG(GCZONE_MAPPING, "  implicit count = %d\n",
		      bvbuffmapinfo->automap);

		/* Still referenced? */
		if (bvbuffmapinfo->usermap || bvbuffmapinfo->automap) {
			GCDBG(GCZONE_MAPPING, "  still referenced.\n");

			/* Remove scheduled unmapping. */
			list_move(head, &gccontext->unmapvac);
			continue;
		}

		GCDBG(GCZONE_MAPPING, "  ready for unmapping.\n");

		/* Set the handle. */
		gcschedunmap->handle = bvbuffmapinfo->handle;

		/* Remove from the buffer descriptor. */
		if (prev == NULL)
			bvbuffdesc->map = bvbuffmap->nextmap;
		else
			prev->nextmap = bvbuffmap->nextmap;

		/* Add to the vacant list. */
		bvbuffmap->nextmap = gccontext->buffmapvac;
		gccontext->buffmapvac = bvbuffmap;
	}

	/* Unlock access to the mapping list. */
	GCUNLOCK(&gccontext->maplock);

	GCEXIT(GCZONE_MAPPING);
}