// File: /workspace/github_c_makefile_star2_O1/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 0x7366
cmpb $0x0, 0x2911a(%rip)     # 0x301e8
jne 0x70da
movl $0xffffffff, 0x2910a(%rip) # imm = 0xFFFFFFFF
cmpl $0x0, 0x29103(%rip)     # 0x301e4
js 0x70ee
incl 0x2910f(%rip)           # 0x301f8
jmp 0x7366
leaq 0x262f9(%rip), %rdi     # 0x2d3ee
movl $0x2, %esi
xorl %eax, %eax
callq 0x2130
movl %eax, 0x290dd(%rip)     # 0x301e4
testl %eax, %eax
js 0x72c9
movb $0x1, 0x290d2(%rip)     # 0x301e8
leaq 0x29143(%rip), %rdi     # 0x30260
movl $0x2800, %edx           # imm = 0x2800
xorl %esi, %esi
callq 0x20e0
xorl %edi, %edi
callq 0x2120
testq %rax, %rax
je 0x72e3
movq 0x290b0(%rip), %rax     # 0x301f0
cmpb $0x0, 0x20024(%rax)
jne 0x716f
leaq 0x4(%rax), %rcx
xorl %edx, %edx
movl %edx, (%rcx)
movl $0x0, -0x4(%rcx)
incq %rdx
addq $0x8000, %rcx           # imm = 0x8000
cmpq $0x4, %rdx
jne 0x714f
movb $0x1, 0x20024(%rax)
movl $0x0, 0x2908b(%rip)     # 0x30204
movl 0x29065(%rip), %edi     # 0x301e4
leaq 0x2907a(%rip), %rdx     # 0x30200
movl $0x4020560c, %esi       # imm = 0x4020560C
xorl %eax, %eax
callq 0x2230
testl %eax, %eax
js 0x72ec
movl 0x29060(%rip), %esi     # 0x30200
movl 0x2903d(%rip), %r8d     # 0x301e4
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r9d, %r9d
callq 0x2320
movq %rax, 0x29056(%rip)     # 0x30218
cmpq $-0x1, %rax
je 0x72f5
xorl %edi, %edi
movl $0x1, %esi
callq 0x2330
movl 0x29006(%rip), %edi     # 0x301e4
testl %edi, %edi
js 0x7342
cmpb $0x0, 0x28ffb(%rip)     # 0x301e8
je 0x7342
xorps %xmm0, %xmm0
movq %rsp, %rbx
movaps %xmm0, (%rbx)
movaps %xmm0, 0x10(%rbx)
movq $0x80000, (%rbx)        # imm = 0x80000
movl $0x40205606, %esi       # imm = 0x40205606
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2230
movl (%rbx), %esi
testl %eax, %eax
js 0x7312
movl 0x28fbd(%rip), %r8d     # 0x301e4
movq 0x8(%rsp), %r9
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x2320
movq %rax, 0x18(%rsp)
cmpq $-0x1, %rax
je 0x7322
movq 0x28f9d(%rip), %rcx     # 0x301f0
movl $0x80000, 0x20000(%rcx) # imm = 0x80000
movl 0x4(%rsp), %edx
movl %edx, 0x20004(%rcx)
movq 0x8(%rsp), %rdx
movq %rdx, 0x20008(%rcx)
movq 0x10(%rsp), %rdx
movq %rdx, 0x20010(%rcx)
movq %rax, 0x20018(%rcx)
movups 0x20000(%rcx), %xmm0
movaps %xmm0, 0x28fac(%rip)    # 0x30240
movups 0x20010(%rcx), %xmm0
movaps %xmm0, 0x28fae(%rip)    # 0x30250
movl $0xa0, %eax
leaq 0x28f32(%rip), %rcx     # 0x301e0
cmpl $0x0, (%rax,%rcx)
je 0x736e
addq $0x28, %rax
cmpq $0x28a0, %rax           # imm = 0x28A0
jne 0x72ae
jmp 0x738e
leaq 0x2612b(%rip), %rdi     # 0x2d3fb
xorl %eax, %eax
callq 0x2050
movl $0xa, %edi
callq 0x2220
jmp 0x7361
leaq 0x2612d(%rip), %rdi     # 0x2d417
jmp 0x7349
leaq 0x26158(%rip), %rdi     # 0x2d44b
jmp 0x7349
movq %rax, %rbx
callq 0x2040
movl (%rax), %edx
leaq 0x2616f(%rip), %rdi     # 0x2d475
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x7350
leaq 0x262c3(%rip), %rdi     # 0x2d5dc
xorl %eax, %eax
callq 0x2050
jmp 0x7338
movq 0x8(%rsp), %rsi
movl (%rsp), %edx
leaq 0x26452(%rip), %rdi     # 0x2d783
xorl %eax, %eax
callq 0x2050
movl $0xa, %edi
callq 0x2220
leaq 0x26164(%rip), %rdi     # 0x2d4ad
xorl %eax, %eax
callq 0x2050
movl $0xa, %edi
callq 0x2220
xorl %edi, %edi
callq 0x2190
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
retq
movaps (%rsp), %xmm0
movaps 0x10(%rsp), %xmm1
movups %xmm1, -0x10(%rax,%rcx)
movups %xmm0, -0x20(%rax,%rcx)
incl 0x2b6d9(%rip)           # 0x32a60
movl $0x1, (%rax,%rcx)
movq 0x28eb3(%rip), %rsi     # 0x30248
movq 0x28ebc(%rip), %rdx     # 0x30258
movl 0x28e9e(%rip), %ecx     # 0x30240
leaq 0x26415(%rip), %rdi     # 0x2d7be
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2050
movl $0xa, %edi
callq 0x2220
movl $0x0, 0x28e1a(%rip)     # 0x301e0
jmp 0x70e3