// File: /workspace/github_c_makefile_star2_O2/xiane[P]libvphevcodec/vdi.cpp
// Function: vdi_init(unsigned int)
s32 vdi_init(u32 core_idx) {
    vdi_info_t *vdi;
    s32 i;

    if (core_idx >= MAX_NUM_VPU_CORE)
        return 0;

    vdi = &s_vdi_info[core_idx];
    if (vdi->opened == 0)
        vdi->vpu_fd = -1;

    if (vdi->vpu_fd >= 0) {
        vdi->task_num++;
        return 0;
    }

    vdi->vpu_fd = open(VPU_DEVICE_NAME, O_RDWR);
    if (vdi->vpu_fd < 0) {
        VLOG(ERR, "[VDI] Can't open vpu driver");
        return -1;
    }

    vdi->opened = 1;
    memset(&vdi->vpu_buffer_pool, 0x00,
        sizeof(vpudrv_buffer_pool_t) * MAX_VPU_BUFFER_POOL);

    if (!vdi_get_instance_pool(core_idx)) {
        VLOG(ERR, "[VDI] fail to create shared info for saving context");
        goto ERR_VDI_INIT;
    }

    if (vdi->pvip->instance_pool_inited == 0) {
        u32* pCodecInst;
        for (i = 0; i < MAX_NUM_INSTANCE; i++) {
            pCodecInst = (u32 *) vdi->pvip->codecInstPool[i];
            pCodecInst[1] = i; // indicate instIndex of CodecInst
            pCodecInst[0] = 0; // indicate inUse of CodecInst
        }
        vdi->pvip->instance_pool_inited = 1;
    }
    vdi->vdb_register.cached = 0;
    if (ioctl(vdi->vpu_fd, VDI_IOCTL_GET_REGISTER_INFO, &vdi->vdb_register) < 0) {
        VLOG(ERR, "[VDI] fail to get host interface register");
        goto ERR_VDI_INIT;
    }

    VLOG(DEBUG, "vdb_register.size:%u vdi->vdb_register.phys_addr:%lu errno:%d",
                                vdi->vdb_register.size, vdi->vdb_register.phys_addr, errno);
    vdi->vdb_register.virt_addr = (ulong) mmap(NULL,
        vdi->vdb_register.size,
        PROT_READ | PROT_WRITE,
        MAP_SHARED,
        vdi->vpu_fd,
        0/*vdi->vdb_register.phys_addr*/);

    if ((void *) vdi->vdb_register.virt_addr == MAP_FAILED) {
        VLOG(ERR, "[VDI] fail to map vpu registers. virt_addr:%lx errno:%d",
                                            vdi->vdb_register.virt_addr, errno);
        goto ERR_VDI_INIT;
    }

    VLOG(DEBUG, "[VDI] map vdb_register core_idx=%d, virtaddr=0x%lx, size=0x%x",
                            core_idx, vdi->vdb_register.virt_addr, vdi->vdb_register.size);

    vdi_set_clock_gate(core_idx, 1);

    if (allocate_common_memory(core_idx) < 0) {
        VLOG(ERR, "[VDI] fail to get vpu common buffer from driver");
        goto ERR_VDI_INIT;
    }

    vdi->core_idx = core_idx;
    vdi->task_num++;
    VLOG(DEBUG, "[VDI] success to init driver");
    return 0;

    ERR_VDI_INIT: vdi_release(core_idx);
    return -1;
}

// Assembly:
pushq %rbx
subq $0x20, %rsp
xorl %ebx, %ebx
testl %edi, %edi
jne 0x6f3f
cmpb $0x0, 0x2858e(%rip)     # 0x2f208
je 0x6c90
cmpl $0x0, 0x28581(%rip)     # 0x2f204
js 0x6c97
incl 0x2858d(%rip)           # 0x2f218
jmp 0x6f3f
orl $-0x1, 0x2856d(%rip)    # 0x2f204
leaq 0x25760(%rip), %rdi     # 0x2c3fe
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x2140
movl %eax, 0x28556(%rip)     # 0x2f204
testl %eax, %eax
js 0x6d06
movb $0x1, 0x2854f(%rip)     # 0x2f208
leaq 0x285c0(%rip), %rdi     # 0x2f280
movl $0x2800, %edx           # imm = 0x2800
xorl %esi, %esi
callq 0x20f0
xorl %edi, %edi
callq 0x2130
testq %rax, %rax
je 0x6d21
movq 0x28531(%rip), %rax     # 0x2f210
cmpb $0x0, 0x20024(%rax)
jne 0x6d34
leaq 0x4(%rax), %rcx
xorl %edx, %edx
cmpq $0x4, %rdx
je 0x6d2d
movl %edx, (%rcx)
andl $0x0, -0x4(%rcx)
incq %rdx
addq $0x8000, %rcx           # imm = 0x8000
jmp 0x6cee
leaq 0x256fe(%rip), %rdi     # 0x2c40b
xorl %eax, %eax
callq 0x2050
pushq $0xa
popq %rdi
callq 0x2230
jmp 0x6f3c
leaq 0x256ff(%rip), %rdi     # 0x2c427
jmp 0x6f26
movb $0x1, 0x20024(%rax)
andl $0x0, 0x284e9(%rip)     # 0x2f224
movl 0x284c3(%rip), %edi     # 0x2f204
leaq 0x284d8(%rip), %rdx     # 0x2f220
movl $0x4020560c, %esi       # imm = 0x4020560C
xorl %eax, %eax
callq 0x2240
testl %eax, %eax
js 0x6ecb
movl 0x284be(%rip), %esi     # 0x2f220
movl 0x2849b(%rip), %r8d     # 0x2f204
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
xorl %r9d, %r9d
callq 0x2340
movq %rax, 0x284b8(%rip)     # 0x2f238
cmpq $-0x1, %rax
je 0x6ed4
pushq $0x1
popq %rsi
xorl %edi, %edi
callq 0x2350
movl 0x2846a(%rip), %edi     # 0x2f204
testl %edi, %edi
js 0x6f1f
cmpb $0x0, 0x2845f(%rip)     # 0x2f208
je 0x6f1f
xorps %xmm0, %xmm0
movq %rsp, %rbx
movups %xmm0, 0x8(%rbx)
andq $0x0, 0x18(%rbx)
movq $0x80000, (%rbx)        # imm = 0x80000
movl $0x40205606, %esi       # imm = 0x40205606
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2240
movl (%rbx), %esi
testl %eax, %eax
js 0x6ef1
movl 0x2841f(%rip), %r8d     # 0x2f204
movq 0x8(%rsp), %r9
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
callq 0x2340
movq %rax, 0x18(%rsp)
cmpq $-0x1, %rax
je 0x6f01
movq 0x28403(%rip), %rcx     # 0x2f210
movl $0x80000, 0x20000(%rcx) # imm = 0x80000
movl 0x4(%rsp), %edx
movl %edx, 0x20004(%rcx)
movups 0x8(%rsp), %xmm0
movups %xmm0, 0x20008(%rcx)
movq %rax, 0x20018(%rcx)
movups 0x20010(%rcx), %xmm0
movaps %xmm0, 0x2842e(%rip)    # 0x2f270
movups 0x20000(%rcx), %xmm0
movaps %xmm0, 0x28410(%rip)    # 0x2f260
movl $0xa0, %edx
leaq 0x283a4(%rip), %rax     # 0x2f200
leaq 0x28(%rdx), %rcx
cmpq $0x28c8, %rcx           # imm = 0x28C8
je 0x6e93
cmpl $0x0, (%rdx,%rax)
movq %rcx, %rdx
jne 0x6e5c
movups (%rsp), %xmm0
movups 0x10(%rsp), %xmm1
movups %xmm1, -0x38(%rcx,%rax)
movups %xmm0, -0x48(%rcx,%rax)
incl 0x2abf5(%rip)           # 0x31a80
movl $0x1, -0x28(%rcx,%rax)
movq 0x283ce(%rip), %rsi     # 0x2f268
movq 0x283d7(%rip), %rdx     # 0x2f278
movl 0x283b9(%rip), %ecx     # 0x2f260
leaq 0x25920(%rip), %rdi     # 0x2c7ce
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2050
pushq $0xa
popq %rdi
callq 0x2230
andl $0x0, 0x2833a(%rip)     # 0x2f200
jmp 0x6c85
leaq 0x25589(%rip), %rdi     # 0x2c45b
jmp 0x6f26
movq %rax, %rbx
callq 0x2040
movl (%rax), %edx
leaq 0x255a0(%rip), %rdi     # 0x2c485
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x6f2d
leaq 0x256f4(%rip), %rdi     # 0x2c5ec
xorl %eax, %eax
callq 0x2050
jmp 0x6f17
movq 0x8(%rsp), %rsi
movl (%rsp), %edx
leaq 0x25883(%rip), %rdi     # 0x2c793
xorl %eax, %eax
callq 0x2050
pushq $0xa
popq %rdi
callq 0x2230
leaq 0x25597(%rip), %rdi     # 0x2c4bd
xorl %eax, %eax
callq 0x2050
pushq $0xa
popq %rdi
callq 0x2230
xorl %edi, %edi
callq 0x21a0
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
retq