summaryrefslogtreecommitdiff
path: root/drivers/media/video/dmm/tmm_pat.c
blob: 4b975d33b90f28c5dcb9884f348b287cf1c214c9 (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
/*
 * tmm_pat.c
 *
 * DMM driver support functions for TI OMAP processors.
 *
 * Copyright (C) 2009-2010 Texas Instruments, Inc.
 *
 * 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/init.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/mmzone.h>
#include <asm/cacheflush.h>
#include <linux/mutex.h>
#include <linux/list.h>
#include <linux/slab.h>

#include "tmm.h"

/**
 * Number of pages to allocate when
 * refilling the free page stack.
 */
#define MAX 16
#define DMM_PAGE 0x1000

/* Max pages in free page stack */
#define PAGE_CAP (256 * 128)

/* Number of pages currently allocated */
static unsigned long count;

/**
  * Used to keep track of mem per
  * dmm_get_pages call.
  */
struct fast {
	struct list_head list;
	struct mem **mem;
	u32 *pa;
	u32 num;
};

/**
 * Used to keep track of the page struct ptrs
 * and physical addresses of each page.
 */
struct mem {
	struct list_head list;
	struct page *pg;
	u32 pa;
};

/**
 * TMM PAT private structure
 */
struct dmm_mem {
	struct fast fast_list;
	struct mem free_list;
	struct mem used_list;
	struct mutex mtx;
	struct dmm *dmm;
};

static void dmm_free_fast_list(struct fast *fast)
{
	struct list_head *pos = NULL, *q = NULL;
	struct fast *f = NULL;
	s32 i = 0;

	/* mutex is locked */
	list_for_each_safe(pos, q, &fast->list) {
		f = list_entry(pos, struct fast, list);
		for (i = 0; i < f->num; i++)
			__free_page(f->mem[i]->pg);
		kfree(f->pa);
		kfree(f->mem);
		list_del(pos);
		kfree(f);
	}
}

static u32 fill_page_stack(struct mem *mem, struct mutex *mtx)
{
	s32 i = 0;
	struct mem *m = NULL;

	for (i = 0; i < MAX; i++) {
		m = kmalloc(sizeof(*m), GFP_KERNEL);
		if (!m)
			return -ENOMEM;
		memset(m, 0x0, sizeof(*m));

		m->pg = alloc_page(GFP_KERNEL | GFP_DMA);
		if (!m->pg) {
			kfree(m);
			return -ENOMEM;
		}

		m->pa = page_to_phys(m->pg);

		/**
		 * Note: we need to flush the cache
		 * entry for each page we allocate.
		*/
		dmac_flush_range((void *)page_address(m->pg),
					(void *)page_address(m->pg) + DMM_PAGE);
		outer_flush_range(m->pa, m->pa + DMM_PAGE);

		mutex_lock(mtx);
		count++;
		list_add(&m->list, &mem->list);
		mutex_unlock(mtx);
	}
	return 0x0;
}

static void dmm_free_page_stack(struct mem *mem)
{
	struct list_head *pos = NULL, *q = NULL;
	struct mem *m = NULL;

	/* mutex is locked */
	list_for_each_safe(pos, q, &mem->list) {
		m = list_entry(pos, struct mem, list);
		__free_page(m->pg);
		list_del(pos);
		kfree(m);
	}
}

static void tmm_pat_deinit(struct tmm *tmm)
{
	struct dmm_mem *pvt = (struct dmm_mem *) tmm->pvt;

	mutex_lock(&pvt->mtx);
	dmm_free_fast_list(&pvt->fast_list);
	dmm_free_page_stack(&pvt->free_list);
	dmm_free_page_stack(&pvt->used_list);
	mutex_destroy(&pvt->mtx);
}

static u32 *tmm_pat_get_pages(struct tmm *tmm, s32 n)
{
	s32 i = 0;
	struct list_head *pos = NULL, *q = NULL;
	struct mem *m = NULL;
	struct fast *f = NULL;
	struct dmm_mem *pvt = (struct dmm_mem *) tmm->pvt;

	if (n <= 0 || n > 0x8000)
		return NULL;

	if (list_empty_careful(&pvt->free_list.list))
		if (fill_page_stack(&pvt->free_list, &pvt->mtx))
			return NULL;

	f = kmalloc(sizeof(*f), GFP_KERNEL);
	if (!f)
		return NULL;
	memset(f, 0x0, sizeof(*f));

	/* array of mem struct pointers */
	f->mem = kmalloc(n * sizeof(*f->mem), GFP_KERNEL);
	if (!f->mem) {
		kfree(f); return NULL;
	}
	memset(f->mem, 0x0, n * sizeof(*f->mem));

	/* array of physical addresses */
	f->pa = kmalloc(n * sizeof(*f->pa), GFP_KERNEL);
	if (!f->pa) {
		kfree(f->mem); kfree(f); return NULL;
	}
	memset(f->pa, 0x0, n * sizeof(*f->pa));

	/*
	 * store the number of mem structs so that we
	 * know how many to free later.
	 */
	f->num = n;

	for (i = 0; i < n; i++) {
		if (list_empty_careful(&pvt->free_list.list))
			if (fill_page_stack(&pvt->free_list, &pvt->mtx))
				goto cleanup;

		mutex_lock(&pvt->mtx);
		pos = NULL;
		q = NULL;
		m = NULL;

		/*
		 * remove one mem struct from the free list and
		 * add the address to the fast struct mem array
		 */
		list_for_each_safe(pos, q, &pvt->free_list.list) {
			m = list_entry(pos, struct mem, list);
			list_del(pos);
			break;
		}
		mutex_unlock(&pvt->mtx);

		if (m != NULL) {
			f->mem[i] = m;
			f->pa[i] = m->pa;
		}
		else {
			goto cleanup;
		}
	}

	mutex_lock(&pvt->mtx);
	list_add(&f->list, &pvt->fast_list.list);
	mutex_unlock(&pvt->mtx);

	if (f != NULL)
		return f->pa;
cleanup:
	for (; i > 0; i--) {
		mutex_lock(&pvt->mtx);
		list_add(&f->mem[i - 1]->list, &pvt->free_list.list);
		mutex_unlock(&pvt->mtx);
	}
	kfree(f->pa);
	kfree(f->mem);
	kfree(f);
	return NULL;
}

static void tmm_pat_free_pages(struct tmm *tmm, u32 *list)
{
	struct dmm_mem *pvt = (struct dmm_mem *) tmm->pvt;
	struct list_head *pos = NULL, *q = NULL;
	struct fast *f = NULL;
	s32 i = 0;

	mutex_lock(&pvt->mtx);
	pos = NULL;
	q = NULL;
	list_for_each_safe(pos, q, &pvt->fast_list.list) {
		f = list_entry(pos, struct fast, list);
		if (f->pa[0] == list[0]) {
			for (i = 0; i < f->num; i++) {
				if (count < PAGE_CAP) {
					list_add(
					&((struct mem *)f->mem[i])->list,
					&pvt->free_list.list);
				} else {
					__free_page(
						((struct mem *)f->mem[i])->pg);
					count--;
				}
			}
			list_del(pos);
			kfree(f->pa);
			kfree(f->mem);
			kfree(f);
			break;
		}
	}
	mutex_unlock(&pvt->mtx);
}

static s32 tmm_pat_map(struct tmm *tmm, struct pat_area area, u32 page_pa)
{
	struct dmm_mem *pvt = (struct dmm_mem *) tmm->pvt;
	struct pat pat_desc = {0};

	/* send pat descriptor to dmm driver */
	pat_desc.ctrl.dir = 0;
	pat_desc.ctrl.ini = 0;
	pat_desc.ctrl.lut_id = 0;
	pat_desc.ctrl.start = 1;
	pat_desc.ctrl.sync = 0;
	pat_desc.area = area;
	pat_desc.next = NULL;

	/* must be a 16-byte aligned physical address */
	pat_desc.data = page_pa;
	return dmm_pat_refill(pvt->dmm, &pat_desc, MANUAL);
}

struct tmm *tmm_pat_init(u32 pat_id)
{
	struct tmm *tmm = NULL;
	struct dmm_mem *pvt = NULL;

	struct dmm *dmm = dmm_pat_init(pat_id);
	if (dmm)
		tmm = kmalloc(sizeof(*tmm), GFP_KERNEL);
	if (tmm)
		pvt = kmalloc(sizeof(*pvt), GFP_KERNEL);
	if (pvt) {
		/* private data */
		pvt->dmm = dmm;
		INIT_LIST_HEAD(&pvt->free_list.list);
		INIT_LIST_HEAD(&pvt->used_list.list);
		INIT_LIST_HEAD(&pvt->fast_list.list);
		mutex_init(&pvt->mtx);

		count = 0;
		if (list_empty_careful(&pvt->free_list.list))
			if (fill_page_stack(&pvt->free_list, &pvt->mtx))
				goto error;

		/* public data */
		tmm->pvt = pvt;
		tmm->deinit = tmm_pat_deinit;
		tmm->get = tmm_pat_get_pages;
		tmm->free = tmm_pat_free_pages;
		tmm->map = tmm_pat_map;
		tmm->clear = NULL;   /* not yet supported */

		return tmm;
	}

error:
	kfree(pvt);
	kfree(tmm);
	dmm_pat_release(dmm);
	return NULL;
}
EXPORT_SYMBOL(tmm_pat_init);