// File: /workspace/github_c_makefile_star2_O3/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 0x72e7
cmpb $0x0, 0x29192(%rip)     # 0x301e8
je 0x706c
cmpl $0x0, 0x29185(%rip)     # 0x301e4
js 0x7076
incl 0x29191(%rip)           # 0x301f8
jmp 0x72e7
movl $0xffffffff, 0x2916e(%rip) # imm = 0xFFFFFFFF
leaq 0x26381(%rip), %rdi     # 0x2d3fe
movl $0x2, %esi
xorl %eax, %eax
callq 0x2130
movl %eax, 0x29155(%rip)     # 0x301e4
testl %eax, %eax
js 0x724a
movb $0x1, 0x2914a(%rip)     # 0x301e8
leaq 0x291bb(%rip), %rdi     # 0x30260
movl $0x2800, %edx           # imm = 0x2800
xorl %esi, %esi
callq 0x20e0
xorl %edi, %edi
callq 0x2120
testq %rax, %rax
je 0x7264
movq 0x29128(%rip), %rax     # 0x301f0
cmpb $0x0, 0x20024(%rax)
jne 0x70f7
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 0x70d7
movb $0x1, 0x20024(%rax)
movl $0x0, 0x29103(%rip)     # 0x30204
movl 0x290dd(%rip), %edi     # 0x301e4
leaq 0x290f2(%rip), %rdx     # 0x30200
movl $0x4020560c, %esi       # imm = 0x4020560C
xorl %eax, %eax
callq 0x2230
testl %eax, %eax
js 0x726d
movl 0x290d8(%rip), %esi     # 0x30200
movl 0x290b5(%rip), %r8d     # 0x301e4
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r9d, %r9d
callq 0x2320
movq %rax, 0x290ce(%rip)     # 0x30218
cmpq $-0x1, %rax
je 0x7276
xorl %edi, %edi
movl $0x1, %esi
callq 0x2330
movl 0x2907e(%rip), %edi     # 0x301e4
testl %edi, %edi
js 0x72c3
cmpb $0x0, 0x29073(%rip)     # 0x301e8
je 0x72c3
xorps %xmm0, %xmm0
movq %rsp, %rbx
movups %xmm0, 0x8(%rbx)
movq $0x0, 0x18(%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 0x7293
movl 0x29030(%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 0x72a3
movq 0x29010(%rip), %rcx     # 0x301f0
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, 0x2903b(%rip)    # 0x30250
movups 0x20000(%rcx), %xmm0
movaps %xmm0, 0x2901d(%rip)    # 0x30240
movl $0xa0, %eax
leaq 0x28fb1(%rip), %rcx     # 0x301e0
cmpl $0x0, (%rax,%rcx)
je 0x72ef
addq $0x28, %rax
cmpq $0x28a0, %rax           # imm = 0x28A0
jne 0x722f
jmp 0x730f
leaq 0x261ba(%rip), %rdi     # 0x2d40b
xorl %eax, %eax
callq 0x2050
movl $0xa, %edi
callq 0x2220
jmp 0x72e2
leaq 0x261bc(%rip), %rdi     # 0x2d427
jmp 0x72ca
leaq 0x261e7(%rip), %rdi     # 0x2d45b
jmp 0x72ca
movq %rax, %rbx
callq 0x2040
movl (%rax), %edx
leaq 0x261fe(%rip), %rdi     # 0x2d485
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x72d1
leaq 0x26352(%rip), %rdi     # 0x2d5ec
xorl %eax, %eax
callq 0x2050
jmp 0x72b9
movq 0x8(%rsp), %rsi
movl (%rsp), %edx
leaq 0x264e1(%rip), %rdi     # 0x2d793
xorl %eax, %eax
callq 0x2050
movl $0xa, %edi
callq 0x2220
leaq 0x261f3(%rip), %rdi     # 0x2d4bd
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
movups (%rsp), %xmm0
movups 0x10(%rsp), %xmm1
movups %xmm1, -0x10(%rax,%rcx)
movups %xmm0, -0x20(%rax,%rcx)
incl 0x2b758(%rip)           # 0x32a60
movl $0x1, (%rax,%rcx)
movq 0x28f32(%rip), %rsi     # 0x30248
movq 0x28f3b(%rip), %rdx     # 0x30258
movl 0x28f1d(%rip), %ecx     # 0x30240
leaq 0x264a4(%rip), %rdi     # 0x2d7ce
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2050
movl $0xa, %edi
callq 0x2220
movl $0x0, 0x28e99(%rip)     # 0x301e0
jmp 0x7061