// File: /workspace/github_c_makefile_star2_O1/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 0x7c92
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
retq
movl 0x2854c(%rip), %edi     # 0x301e4
testl %edi, %edi
js 0x7c88
cmpb $0x0, 0x28545(%rip)     # 0x301e8
je 0x7c88
movq %rsi, %r14
xorps %xmm0, %xmm0
movq %rsp, %rdx
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, (%rdx)
movl (%rsi), %eax
movl %eax, (%rdx)
movl 0x4(%rsi), %eax
movl %eax, 0x4(%rdx)
movl $0x40205600, %esi       # imm = 0x40205600
xorl %eax, %eax
callq 0x2230
testl %eax, %eax
js 0x7d2b
movq 0x8(%rsp), %r9
movq %r9, 0x8(%r14)
movq 0x10(%rsp), %rax
movq %rax, 0x10(%r14)
movl (%rsp), %esi
movl 0x284f9(%rip), %r8d     # 0x301e4
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x2320
movq %rax, 0x18(%rsp)
cmpq $-0x1, %rax
je 0x7d3e
movq %rax, 0x18(%r14)
movl $0xa0, %eax
leaq 0x284c9(%rip), %rcx     # 0x301e0
cmpl $0x0, (%rax,%rcx)
je 0x7d4f
addq $0x28, %rax
cmpq $0x28a0, %rax           # imm = 0x28A0
jne 0x7d17
jmp 0x7d6f
movl (%r14), %esi
leaq 0x258a7(%rip), %rdi     # 0x2d5dc
xorl %eax, %eax
callq 0x2050
jmp 0x7d90
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
movups %xmm0, (%r14)
jmp 0x7c88
movaps (%rsp), %xmm0
movaps 0x10(%rsp), %xmm1
movups %xmm1, -0x10(%rax,%rcx)
movups %xmm0, -0x20(%rax,%rcx)
incl 0x2acf8(%rip)           # 0x32a60
movl $0x1, (%rax,%rcx)
movq 0x8(%r14), %rsi
movq 0x18(%r14), %rdx
movl (%r14), %r8d
movq %r8, %rcx
addq %rdx, %rcx
leaq 0x25885(%rip), %rdi     # 0x2d60c
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2050
movl $0xa, %edi
callq 0x2220
jmp 0x7c88