summaryrefslogtreecommitdiff
path: root/bmm_lib.c
blob: fa6c350deb7ba5f6047be94a4a53af25cfc0533e (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
343
344
345
346
347
348
349
/*
 *  bmm_lib.c
 *
 *  Buffer Management Module
 *
 *  User level BMM Defines/Globals/Functions
 *
 *  Li Li (lea.li@marvell.com)

 *(C) Copyright 2007 Marvell International Ltd.
 * All Rights Reserved
 */

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>

#include "bmm_lib_priv.h"
#include "bmm_lib.h"

#undef DEBUG

#ifdef DEBUG
#include <stdio.h>
#define pr_debug(fmt, arg...) printf(fmt, ##arg)
#else
#define pr_debug(fmt, arg...) do { } while(0)
#endif

static int bmm_fd = -1;

int bmm_init()
{
        /* attempt to open the BMM driver */
	if(bmm_fd < 0)
		bmm_fd = open(BMM_DEVICE_FILE, O_RDWR);

	/* if the open failed, try to mount the driver */
	if(bmm_fd < 0 ) {
		system("mknod /dev/bmm c 10 94\n");
		bmm_fd = open(BMM_DEVICE_FILE, O_RDWR);
	}

	pr_debug("BMM device fd: %d", bmm_fd);

	return bmm_fd;
}

void bmm_exit()
{
	if(bmm_fd >= 0)
		close(bmm_fd);
	bmm_fd = -1;
}

void *bmm_malloc(unsigned long size, int attr)
{
	int ret;
	void *vaddr;
	ioctl_arg_t io;

	if(size == 0)
		return NULL;

	if(bmm_init() < 0)
		return NULL;

	/* obsolete, only for back-compatible */
	if ((attr & BMM_ATTR_NONBUFFERABLE)&&(attr & BMM_ATTR_NONCACHEABLE))
		attr = BMM_ATTR_NONCACHED;

	if ((!(attr & BMM_ATTR_NONBUFFERABLE))&&(attr & BMM_ATTR_NONCACHEABLE))
		attr = BMM_ATTR_WRITECOMBINE;

	io.input = size;
	io.output = 0;
	io.arg = attr;
	ret = ioctl(bmm_fd, BMM_MALLOC, &io);
	if(ret < 0 || io.output == 0)
		return NULL;

	pr_debug("bmm_malloc return paddr = 0x%08lx\n", io.output);

	vaddr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, bmm_fd, io.output);
	return ((int)vaddr == -1) ? NULL : vaddr;
}

void bmm_free(void *vaddr)
{
	unsigned long size;
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return;

	size = bmm_get_mem_size(vaddr);
	munmap(vaddr, size);

	io.input = (unsigned long)vaddr;
	io.output = 0;
	io.arg = 0;
	ioctl(bmm_fd, BMM_FREE, &io);
}

void *bmm_attach(unsigned long paddr, unsigned long len)
{
	void *vaddr;

	if(len == 0)
		return NULL;

	if(bmm_init() < 0)
		return NULL;

	vaddr = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, bmm_fd, paddr);

	return ((int)vaddr == -1) ? NULL : vaddr;
}

void bmm_detach(void *vaddr, unsigned long len)
{
	if(bmm_init() < 0)
		return;

	munmap(vaddr, len);
}

void *bmm_get_vaddr(unsigned long paddr)
{
	int ret;
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return NULL;

	io.input = paddr;
	io.output = 0;
	io.arg = 0;
	ret = ioctl(bmm_fd, BMM_GET_VIRT_ADDR, &io);
	if(ret < 0)
		return NULL;

	return (void *)io.output;
}

unsigned long bmm_get_kern_paddr(void *vaddr, unsigned long size)
{
	int ret;
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return 0;

	io.input = (unsigned long)vaddr;
	io.length = (unsigned long)size;
	io.output = 0;
	io.arg = 0;
	ret = ioctl(bmm_fd, BMM_GET_KERN_PHYS_ADDR, &io);
	if(ret < 0)
		return 0;

	return io.output;
}

unsigned long bmm_get_paddr(void *vaddr)
{
	int ret;
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return 0;

	io.input = (unsigned long)vaddr;
	io.output = 0;
	io.arg = 0;
	ret = ioctl(bmm_fd,BMM_GET_PHYS_ADDR, &io);
	if(ret < 0)
		return 0;

	return io.output;
}

unsigned long bmm_get_mem_size(void *vaddr)
{
	int ret;
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return 0;

	io.input = (unsigned long)vaddr;
	io.output = 0;
	io.arg = 0;
	ret = ioctl(bmm_fd,BMM_GET_MEM_SIZE, &io);
	if(ret < 0)
		return 0;

	return io.output;
}

int bmm_get_mem_attr(void *vaddr)
{
	int ret;
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return 0;

	io.input = (unsigned long)vaddr;
	io.output = 0;
	io.arg = 0;
	ret = ioctl(bmm_fd, BMM_GET_MEM_ATTR, &io);
	if(ret < 0)
		return 0;

	return (int)io.output;
}

int bmm_set_mem_attr(void *vaddr, int attr)
{
	int ret;
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return 0;

	io.input = (unsigned long)vaddr;
	io.output = 0;
	io.arg = attr;
	ret = ioctl(bmm_fd, BMM_SET_MEM_ATTR, &io);
	if(ret < 0)
		return 0;

	return (int)io.output;
}

unsigned long bmm_get_total_space()
{
	int ret;
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return 0;

	io.input = 0;
	io.output = 0;
	io.arg = 0;
	ret = ioctl(bmm_fd, BMM_GET_TOTAL_SPACE, &io);
	if(ret < 0)
		return 0;

	return io.output;
}

unsigned long bmm_get_free_space()
{
	int ret;
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return 0;

	io.input = 0;
	io.output = 0;
	io.arg = 0;
	ret = ioctl(bmm_fd, BMM_GET_FREE_SPACE, &io);
	if(ret < 0)
		return 0;

	return io.output;
}

unsigned long bmm_get_allocated_space()
{
	int ret;
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return 0;

	io.input = 0;
	io.output = 0;
	io.arg = 0;
	ret = ioctl(bmm_fd, BMM_GET_ALLOCATED_SPACE, &io);
	if(ret < 0)
		return 0;

	return io.output;
}

void bmm_flush_cache(void *vaddr, int dir)
{
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return;

	io.input = (unsigned long)vaddr;
	io.output = 0;
	io.arg = dir;
	ioctl(bmm_fd, BMM_FLUSH_CACHE, &io);
}

void bmm_flush_cache_range(void *start, size_t size, int direction)
{
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return;

	io.input = (unsigned long)start;
	io.length = size;
	io.output = 0;
	io.arg = direction;
	ioctl(bmm_fd, BMM_CONSISTENT_SYNC, &io);
}

void bmm_flush_user(void *start, long size, int direction)
{
	ioctl_arg_t io;

	if (bmm_init() < 0)
		return;
	if (size <= 0)
		return;

	io.input = (unsigned long)start;
	io.length = size;
	io.output = 0;
	io.arg = direction;
	ioctl(bmm_fd, BMM_SYNC_USER, &io);
}

void bmm_dump()
{
	ioctl_arg_t io;

	if(bmm_init() < 0)
		return;

	io.input = 0;
	io.output = 0;
	io.arg = 0;
	ioctl(bmm_fd, BMM_DUMP, &io);
}