// File: /workspace/github_c_makefile_star2_O2/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
pushq $-0x1
popq %rbx
testl %edi, %edi
je 0x7775
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
retq
movl 0x27a89(%rip), %edi     # 0x2f204
testl %edi, %edi
js 0x776b
cmpb $0x0, 0x27a82(%rip)     # 0x2f208
je 0x776b
movq %rsi, %r14
leaq 0x8(%rsp), %rdx
andq $0x0, 0x18(%rdx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rdx)
movq (%rsi), %rax
movq %rax, (%rdx)
movl $0x40205600, %esi       # imm = 0x40205600
xorl %eax, %eax
callq 0x2240
testl %eax, %eax
js 0x785a
movq 0x10(%rsp), %r9
movups 0x10(%rsp), %xmm0
movups %xmm0, 0x8(%r14)
movl 0x8(%rsp), %esi
movl 0x27a34(%rip), %r8d     # 0x2f204
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
callq 0x2340
movq %rax, 0x20(%rsp)
cmpq $-0x1, %rax
je 0x7878
movq %rax, 0x18(%r14)
movl $0xa0, %esi
leaq 0x27a04(%rip), %rcx     # 0x2f200
leaq 0x28(%rsi), %rdx
cmpq $0x28c8, %rdx           # imm = 0x28C8
je 0x7838
cmpl $0x0, (%rsi,%rcx)
movq %rdx, %rsi
jne 0x77fc
movups 0x8(%rsp), %xmm0
movups 0x18(%rsp), %xmm1
movups %xmm1, -0x38(%rdx,%rcx)
movups %xmm0, -0x48(%rdx,%rcx)
incl 0x2a254(%rip)           # 0x31a80
movl $0x1, -0x28(%rdx,%rcx)
movq 0x18(%r14), %rax
movq 0x8(%r14), %rsi
movl (%r14), %r8d
movq %r8, %rcx
addq %rax, %rcx
leaq 0x24dd0(%rip), %rdi     # 0x2c61c
xorl %ebx, %ebx
movq %rax, %rdx
xorl %eax, %eax
callq 0x2050
jmp 0x786b
movl (%r14), %esi
leaq 0x24d88(%rip), %rdi     # 0x2c5ec
xorl %eax, %eax
callq 0x2050
pushq $0xa
popq %rdi
callq 0x2230
jmp 0x776b
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
movups %xmm0, (%r14)
jmp 0x776b