// File: /workspace/github_c_makefile_star2_O3/xiane[P]libvphevcodec/vdi.cpp
// Function: vdi_allocate_dma_memory(unsigned int, vpu_buffer_t*)
s32 vdi_allocate_dma_memory(u32 core_idx, vpu_buffer_t *vb) {
    vdi_info_t *vdi;
    s32 i;
    vpu_buffer_t vdb;

    if (core_idx >= MAX_NUM_VPU_CORE)
        return -1;

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

    memset(&vdb, 0x00, sizeof(vpu_buffer_t));

    vdb.size = vb->size;
    vdb.cached = vb->cached;
    if (ioctl(vdi->vpu_fd, VDI_IOCTL_ALLOCATE_PHYSICAL_MEMORY, &vdb) < 0) {
        VLOG(ERR, "[VDI] fail to vdi_allocate_dma_memory size=0x%x", vb->size);
        return -1;
    }

    vb->phys_addr = (ulong) vdb.phys_addr;
    vb->base = (ulong) vdb.base;

    //map to virtual address
    vdb.virt_addr = (ulong) mmap(NULL, vdb.size, PROT_READ | PROT_WRITE, MAP_SHARED, vdi->vpu_fd, vdb.phys_addr);
    if ((void *) vdb.virt_addr == MAP_FAILED) {
        memset(vb, 0x00, sizeof(vpu_buffer_t));
        return -1;
    }

    vb->virt_addr = vdb.virt_addr;
    for (i = 0; i < MAX_VPU_BUFFER_POOL; i++) {
        if (vdi->vpu_buffer_pool[i].inuse == 0) {
            vdi->vpu_buffer_pool[i].vdb = vdb;
            vdi->vpu_buffer_pool_count++;
            vdi->vpu_buffer_pool[i].inuse = 1;
            VLOG(DEBUG, "[VDI] vdi_allocate_dma_memory->update pool %d, physaddr=0x%lx, virtaddr=0x%lx~0x%lx, size=0x%x",
                i,vb->phys_addr, vb->virt_addr, vb->virt_addr + vb->size, vb->size);
            break;
        }
    }
    VLOG(INFO, "[VDI] vdi_allocate_dma_memory, physaddr=0x%lx, virtaddr=0x%lx~0x%lx, size=0x%x",
                                                  vb->phys_addr, vb->virt_addr, vb->virt_addr + vb->size, vb->size);
    return 0;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x28, %rsp
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %edi, %edi
je 0x7bfe
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
retq
movl 0x285e0(%rip), %edi     # 0x301e4
testl %edi, %edi
js 0x7bf4
cmpb $0x0, 0x285d9(%rip)     # 0x301e8
je 0x7bf4
movq %rsi, %r14
xorps %xmm0, %xmm0
leaq 0x8(%rsp), %rdx
movups %xmm0, 0x8(%rdx)
movq $0x0, 0x18(%rdx)
movq (%rsi), %rax
movq %rax, (%rdx)
movl $0x40205600, %esi       # imm = 0x40205600
xorl %eax, %eax
callq 0x2230
testl %eax, %eax
js 0x7c99
movq 0x10(%rsp), %r9
movups 0x10(%rsp), %xmm0
movups %xmm0, 0x8(%r14)
movl 0x8(%rsp), %esi
movl 0x2858c(%rip), %r8d     # 0x301e4
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x2320
movq %rax, 0x20(%rsp)
cmpq $-0x1, %rax
je 0x7cac
movq %rax, 0x18(%r14)
movl $0xa0, %ecx
leaq 0x2855c(%rip), %rdx     # 0x301e0
cmpl $0x0, (%rcx,%rdx)
je 0x7cbd
addq $0x28, %rcx
cmpq $0x28a0, %rcx           # imm = 0x28A0
jne 0x7c84
jmp 0x7ce2
movl (%r14), %esi
leaq 0x25949(%rip), %rdi     # 0x2d5ec
xorl %eax, %eax
callq 0x2050
jmp 0x7d02
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
movups %xmm0, (%r14)
jmp 0x7bf4
movups 0x8(%rsp), %xmm0
movups 0x18(%rsp), %xmm1
movups %xmm1, -0x10(%rcx,%rdx)
movups %xmm0, -0x20(%rcx,%rdx)
incl 0x2ad89(%rip)           # 0x32a60
movl $0x1, (%rcx,%rdx)
movq 0x18(%r14), %rax
movq 0x8(%r14), %rsi
movl (%r14), %r8d
movq %r8, %rcx
addq %rax, %rcx
leaq 0x25926(%rip), %rdi     # 0x2d61c
xorl %ebx, %ebx
movq %rax, %rdx
xorl %eax, %eax
callq 0x2050
movl $0xa, %edi
callq 0x2220
jmp 0x7bf4