summaryrefslogtreecommitdiff
path: root/vmeta_lib.c
blob: 15b3820641801ab2075ffd6df9b4a0cdf7afc99d (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 *  vmeta_lib.c
 *
 *
 * Copyright (C) 2009 Marvell International Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>

#include "vmeta_lib.h"
#include "bmm_lib.h"
#include "sys/poll.h"


#define ALIGN(x,a)		__ALIGN_MASK(x,(typeof(x))(a)-1)
#define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))

// global variable
vdec_os_driver_cb_t *vdec_iface = NULL;
UNSG32 globalDbgLevel = VDEC_DEBUG_NONE;
struct timeval vdec_timer;

//Add for hal mmap
UNSG8 vdec_os_api_rd8(UNSG32 addr)
{
	return *((volatile UNSG8*)addr);
}

UNSG16 vdec_os_api_rd16(UNSG32 addr)
{
	return *((volatile UNSG16*)addr);
}

UNSG32 vdec_os_api_rd32(UNSG32 addr)
{
	return *((volatile UNSG32*)addr);
}

void vdec_os_api_wr8(UNSG32 addr, UNSG8 data)
{
	*((volatile UNSG8*)addr) = data;
}

void vdec_os_api_wr16(UNSG32 addr, UNSG16 data)
{
	*((volatile UNSG16*)addr) = data;
}

void vdec_os_api_wr32(UNSG32 addr, UNSG32 data)
{
	*((volatile UNSG32*)addr) = data;
}

UNSG32 vdec_os_api_get_regbase_addr(void)
{
	vdec_os_driver_cb_t *vdec_iface = vdec_driver_get_cb();
	return vdec_iface->io_mem_virt_addr;
}
//End of hal mmap

//Mem map to bmm_lib
UNSG32 vdec_os_api_get_pa(UNSG32 vaddr)
{
	return ((UNSG32)bmm_get_paddr((void *)vaddr));
}

UNSG32 vdec_os_api_get_va(UNSG32 paddr)
{	
	return ((UNSG32)bmm_get_vaddr((void *)paddr));
}

void vdec_os_api_vfree(void *ptr)
{
    unsigned int   offset = 0;
    unsigned int   *paddr = NULL;

    paddr  = (unsigned int *)(ptr);
    offset = *(paddr - 1);
    paddr = (unsigned int *)((unsigned int)paddr - offset);
    free((void *)paddr);
}

void *vdec_os_api_vmalloc(UNSG32 size, UNSG32 align)
{
	unsigned int   *ptr = NULL;
	unsigned int   tmp = 0;

	align = ALIGN(align, sizeof(int));
	size += align;
	ptr = malloc(size);
	if (!ptr) {
		printf("\tno enough memory\n");
		return NULL;
	} 

	tmp = (unsigned int)((unsigned int)(ptr) & (align - 1));
	tmp = (unsigned int)(align - tmp);
	ptr = (unsigned int *)((unsigned int)ptr + tmp);
	*(ptr - 1) = tmp;

	dbg_printf(VDEC_DEBUG_MEM, "vdec_os_api_vmalloc ptr: 0x%x\n", ptr);
	return ptr;
}

void vdec_os_api_dma_free(void *ptr)
{
	unsigned int   offset = 0;
	unsigned int   *paddr = NULL;

	paddr  = (unsigned int *)(ptr);
	offset = *(paddr - 1);
	paddr = (unsigned int *)((unsigned int)paddr - offset);
	bmm_free((void *)paddr);
}

void * vdec_os_api_dma_alloc(UNSG32 size, UNSG32 align, UNSG32 * pPhysical)
{
	unsigned int   *ptr = NULL;
	unsigned int   tmp = 0;

	if(size <= 0)
		return NULL;

	dbg_printf(VDEC_DEBUG_MEM, "vdec_os_api_dma_alloc -> size: 0x%x\n", size);

	align = ALIGN(align, sizeof(int));
	size += align;
	ptr = bmm_malloc(size, BMM_ATTR_NONCACHED);
	if (!ptr) {
		printf("\tno enough memory\n");
		return NULL;
	} 

	tmp = (unsigned int)((unsigned int)(ptr) & (align - 1));
	tmp = (unsigned int)(align - tmp);
	ptr = (unsigned int *)((unsigned int)ptr + tmp);
	*(ptr - 1) = tmp;

	*pPhysical = (unsigned long)bmm_get_paddr(ptr);

	dbg_printf(VDEC_DEBUG_MEM, "vdec_os_api_dma_alloc ptr: 0x%x\n", ptr);
	//memset(ptr, 0, size);

	return ptr;
}

void * vdec_os_api_dma_alloc_writecombine(UNSG32 size, UNSG32 align, UNSG32 * pPhysical)
{
	unsigned int   *ptr = NULL;
	unsigned int   tmp = 0;

	if(size <= 0)
		return NULL;

	dbg_printf(VDEC_DEBUG_MEM, "vdec_os_api_dma_alloc -> size: 0x%x\n", size);

	align = ALIGN(align, sizeof(int));
	size += align;
	ptr = bmm_malloc(size, BMM_ATTR_WRITECOMBINE);
	if (!ptr) {
		printf("\tno enough memory\n");
		return NULL;
	} 

	tmp = (unsigned int)((unsigned int)(ptr) & (align - 1));
	tmp = (unsigned int)(align - tmp);
	ptr = (unsigned int *)((unsigned int)ptr + tmp);
	*(ptr - 1) = tmp;

	*pPhysical = (unsigned long)bmm_get_paddr(ptr);

	dbg_printf(VDEC_DEBUG_MEM, "vdec_os_api_dma_alloc ptr: 0x%x\n", ptr);
	//memset(ptr, 0, size);

	return ptr;
}

void * vdec_os_api_dma_alloc_cached(UNSG32 size, UNSG32 align, UNSG32 * pPhysical)
{
	unsigned int   *ptr = NULL;
	unsigned int   tmp = 0;

	if(size <= 0)
		return NULL;

	dbg_printf(VDEC_DEBUG_MEM, "vdec_os_api_dma_alloc -> size: 0x%x\n", size);

	align = ALIGN(align, sizeof(int));
	size += align;
	ptr = bmm_malloc(size, BMM_ATTR_DEFAULT);
	if (!ptr) {
		printf("\tno enough memory\n");
		return NULL;
	} 

	tmp = (unsigned int)((unsigned int)(ptr) & (align - 1));
	tmp = (unsigned int)(align - tmp);
	ptr = (unsigned int *)((unsigned int)ptr + tmp);
	*(ptr - 1) = tmp;

	*pPhysical = (unsigned long)bmm_get_paddr(ptr);

	dbg_printf(VDEC_DEBUG_MEM, "vdec_os_api_dma_alloc ptr: 0x%x\n", ptr);
	//memset(ptr, 0, size);

	return ptr;
}

UNSG32 vdec_os_api_flush_cache(UNSG32 vaddr, UNSG32 size, enum dma_data_direction direction)
{
	switch (direction) {
	case DMA_FROM_DEVICE:
		bmm_flush_cache_range((void *)vaddr, size, BMM_DMA_FROM_DEVICE);
		break;
	case DMA_TO_DEVICE:
		bmm_flush_cache_range((void *)vaddr, size, BMM_DMA_TO_DEVICE);
		break;
	case DMA_BIDIRECTIONAL:
		bmm_flush_cache_range((void *)vaddr, size, BMM_DMA_BIDIRECTIONAL);
		break;
	}
	return 0;
}

// enable vmeta interrupt
void vdec_os_api_irq_enable(void)
{
	if(vdec_iface == NULL)
		return;
	
	ioctl(vdec_iface->uiofd, UIO_VPRO_IRQ_ENABLE);
}

// disable vmeta interrupt
void vdec_os_api_irq_disable(void)
{
	if(vdec_iface == NULL)
		return;
	
	ioctl(vdec_iface->uiofd, UIO_VPRO_IRQ_DISABLE);
}

SIGN32 vdec_os_api_set_sync_timeout_isr(UNSG32 timeout)
{
	vdec_timer.tv_sec = 0;
	vdec_timer.tv_usec = timeout * 1000;

	return 0;
}

SIGN32 vdec_os_api_sync_event()
{
	SIGN32 ret = VDEC_OS_DRIVER_OK;
	fd_set rd_fds, tmp_fds;
	struct timeval timeout;

	if(vdec_iface == NULL)
		return -VDEC_OS_DRIVER_SYNC_TIMEOUT_FAIL;

	FD_ZERO(&rd_fds);
	FD_SET(vdec_iface->uiofd, &rd_fds);

	timeout.tv_sec = vdec_timer.tv_sec;
	timeout.tv_usec = vdec_timer.tv_usec;

	while(1) {
		int ret = 0;
		
		tmp_fds = rd_fds;

		// enable interrupt
		vdec_os_api_irq_enable();

		ret = select(vdec_iface->uiofd + 1, &tmp_fds, NULL, NULL, &timeout);
		if(FD_ISSET(vdec_iface->uiofd, &tmp_fds)) {
			int irq_num;
			read(vdec_iface->uiofd, &irq_num, sizeof(int));
			// Or handle userspace ISR here
			break;
		}
		
		// timeout
		ret = -VDEC_OS_DRIVER_SYNC_TIMEOUT_FAIL;

		break;
	}
	
	return ret;
}
//End of mem mmap

UNSG32 get_mem_size(char *msg)
{
	int ret;
	UNSG32 result;
	FILE *file = fopen(msg, "r");

	if(!file) {
		dbg_printf(VDEC_DEBUG_ALL, "Error: get_mem_size -> fopen failed\n");
		return -VDEC_OS_DRIVER_OPEN_FAIL;
	}

	ret = fscanf(file, "0x%x", &result);
	if(ret<0) {
		dbg_printf(VDEC_DEBUG_ALL, "Error: get_mem_size -> fscanf failed\n");
		result = 0;
	}

	fclose(file);
	return result;
}

UNSG32 get_mem_addr(char *msg)
{
	UNSG32 result;
	int ret;
	FILE *file = fopen(msg, "r");

	if(!file) {
		dbg_printf(VDEC_DEBUG_ALL, "Error: get_mem_addr -> fopen failed\n");
		return -VDEC_OS_DRIVER_OPEN_FAIL;
	}

	ret = fscanf(file, "0x%x", &result);
	if(ret<0) {
		dbg_printf(VDEC_DEBUG_ALL, "Error: get_mem_addr -> fscanf failed\n");
		result = 0;
	}

	fclose(file);
	return result;
}

// VMeta power on
void vdec_os_api_power_on(void) {
	if(vdec_iface == NULL)
		return;

	ioctl(vdec_iface->uiofd, UIO_VMETA_POWER_ON);
 }

// VMeta power off
void vdec_os_api_power_off(void) {
	if(vdec_iface == NULL)
		return;

	ioctl(vdec_iface->uiofd, UIO_VMETA_POWER_OFF);
}

// check VMeta is ready to suspend
SIGN32 vdec_os_api_suspend_check(void) {
	SIGN32 suspend_check;

	if(vdec_iface == NULL)
		return 0;

	ioctl(vdec_iface->uiofd, UIO_VMETA_SUSPEND_CHECK, &suspend_check);

	return suspend_check;
}

// VMeta is ready to suspend
void vdec_os_api_suspend_ready(void) {
	if(vdec_iface == NULL)
		return;

	ioctl(vdec_iface->uiofd, UIO_VMETA_SUSPEND_READY);
}

// init vdec os driver
SIGN32 vdec_os_driver_init(void)
{
	int ret = 0;

	if(vdec_iface != NULL) {	// already been initiated
		vdec_iface->refcount++;
		return ret;
	}

	// Prepare the vdec os driver control interface
	vdec_iface = (vdec_os_driver_cb_t*)malloc(sizeof(vdec_os_driver_cb_t));
	memset((void*)vdec_iface, 0, sizeof(vdec_os_driver_cb_t));
	if(vdec_iface == NULL)
		return -VDEC_OS_DRIVER_INIT_FAIL;

	// initialize reference count
	vdec_iface->refcount++;

	// Open the vdec uio driver
	vdec_iface->uiofd = open(UIO_DEV, O_RDWR);
	if(vdec_iface->uiofd < 0) {
		ret = -VDEC_OS_DRIVER_OPEN_FAIL;
		goto err_open_fail;
	}
	dbg_printf(VDEC_DEBUG_ALL, "vdec os driver open: %s\n", UIO_DEV);

	// Get the IO mem size of vPro's register
	vdec_iface->io_mem_size = get_mem_size(UIO_IO_MEM_SIZE);
	if(vdec_iface->io_mem_size <= 0) {
		ret = -VDEC_OS_DRIVER_MMAP_FAIL;
		goto err_mmap_fail;
	}
	dbg_printf(VDEC_DEBUG_MEM, "vdec os driver io mem size: 0x%x\n", vdec_iface->io_mem_size);

	// Get the IO mem phy addr
	vdec_iface->io_mem_phy_addr = get_mem_addr(UIO_IO_MEM_ADDR);
	if(vdec_iface->io_mem_phy_addr <= 0) {
		ret = -VDEC_OS_DRIVER_MMAP_FAIL;
		goto err_mmap_fail;
	}
	dbg_printf(VDEC_DEBUG_MEM, "vdec os driver io mem phy addr: 0x%x\n", vdec_iface->io_mem_phy_addr);

	// mmap the io mem area
	vdec_iface->io_mem_virt_addr = (SIGN32)mmap(NULL, vdec_iface->io_mem_size,
									PROT_READ|PROT_WRITE, MAP_SHARED, vdec_iface->uiofd, 0);

	if(vdec_iface->io_mem_virt_addr == -1) {
		ret = -VDEC_OS_DRIVER_MMAP_FAIL;
		goto err_mmap_fail;
	}
	dbg_printf(VDEC_DEBUG_MEM, "vdec os driver io mem map to: 0x%x\n", vdec_iface->io_mem_virt_addr);

	return ret;

err_mmap_fail:
	if(vdec_iface->io_mem_virt_addr > 0)
		munmap((void*)vdec_iface->io_mem_virt_addr, vdec_iface->io_mem_size);
	close(vdec_iface->uiofd);
err_open_fail:
	free((void*)vdec_iface);
	vdec_iface = NULL;

	return ret;
}

// clean vdec os driver
SIGN32 vdec_os_driver_clean(void)
{
	if(vdec_iface == NULL)
		return -VDEC_OS_DRIVER_CLEAN_FAIL;

	// decrease the refcount
	vdec_iface->refcount--;

	if(vdec_iface->refcount != 0)
		return 0;

	// unmap memory area
	if(vdec_iface->io_mem_virt_addr > 0)
		munmap((void*)vdec_iface->io_mem_virt_addr, vdec_iface->io_mem_size);

	// close fd
	if(vdec_iface->uiofd > 0)
		close(vdec_iface->uiofd);

	// free vdec_iface
	if(vdec_iface != NULL) {
		free((void*)vdec_iface);
		vdec_iface = NULL;
	}

	return 0;
}

/* display debug message */
int dbg_printf(UNSG32 dbglevel, const char* format, ...) {
	char dbgBuf[256] = {'\0'};
	va_list var;
	
	if(VDEC_DEBUG_NONE == globalDbgLevel)
		return 0;
	else {
		va_start(var, format);
		vsprintf(dbgBuf, format, var);
		va_end(var);

		if(VDEC_DEBUG_ALL & globalDbgLevel)
			printf(dbgBuf);
		else if((VDEC_DEBUG_MEM & globalDbgLevel) && (dbglevel == VDEC_DEBUG_MEM))
			printf(dbgBuf);
		else
			return 0;
	}

	return 0;
}

/* vdec driver get cb */
vdec_os_driver_cb_t *vdec_driver_get_cb(void) {
	return vdec_iface;
}