// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x8(%rbp)
cmpl $0x1, -0x8(%rbp)
jb 0x94dd
movl $0x0, -0x4(%rbp)
jmp 0x9776
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x29d27(%rip), %rax     # 0x33210
imulq $0x2888, %rcx, %rcx     # imm = 0x2888
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x8(%rax), %eax
cmpl $0x0, %eax
jne 0x950f
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jl 0x9532
movq -0x10(%rbp), %rax
movl 0x18(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x18(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x9776
leaq 0x26ec5(%rip), %rdi     # 0x303fe
movl $0x2, %esi
movb $0x0, %al
callq 0x2140
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jge 0x9582
jmp 0x955a
leaq 0x26eaa(%rip), %rdi     # 0x3040b
movb $0x0, %al
callq 0x2050
leaq 0x26cf7(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9776
movq -0x10(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x10(%rbp), %rdi
addq $0x80, %rdi
xorl %esi, %esi
movl $0x2800, %edx           # imm = 0x2800
callq 0x20f0
movl -0x8(%rbp), %edi
callq 0x2130
cmpq $0x0, %rax
jne 0x95d2
jmp 0x95b1
leaq 0x26e6f(%rip), %rdi     # 0x30427
movb $0x0, %al
callq 0x2050
leaq 0x26ca0(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
jmp 0x9767
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movzbl 0x20024(%rax), %eax
cmpl $0x0, %eax
jne 0x9638
movl $0x0, -0x14(%rbp)
cmpl $0x4, -0x14(%rbp)
jge 0x9629
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0xf, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movl -0x14(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x95ed
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movb $0x1, 0x20024(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x24(%rax)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x10(%rbp), %rdx
addq $0x20, %rdx
movl $0x4020560c, %esi       # imm = 0x4020560C
movb $0x0, %al
callq 0x2230
cmpl $0x0, %eax
jge 0x9686
jmp 0x9665
leaq 0x26def(%rip), %rdi     # 0x3045b
movb $0x0, %al
callq 0x2050
leaq 0x26bec(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
jmp 0x9767
jmp 0x9688
jmp 0x968a
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, %esi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x2320
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x9708
jmp 0x96d3
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x28(%rbp)
callq 0x2040
movq -0x28(%rbp), %rsi
movl (%rax), %edx
leaq 0x26d94(%rip), %rdi     # 0x30485
movb $0x0, %al
callq 0x2050
leaq 0x26b67(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
jmp 0x9767
jmp 0x970a
jmp 0x970c
movl -0x8(%rbp), %edi
movl $0x1, %esi
callq 0x2330
movl -0x8(%rbp), %edi
callq 0x99d0
cmpl $0x0, %eax
jge 0x9746
jmp 0x9728
leaq 0x26d8e(%rip), %rdi     # 0x304bd
movb $0x0, %al
callq 0x2050
leaq 0x26b29(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
jmp 0x9767
movl -0x8(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x18(%rax)
jmp 0x975e
movl $0x0, -0x4(%rbp)
jmp 0x9776
movl -0x8(%rbp), %edi
callq 0x21a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop