DMA write fails when dma_alloc_coherent is replaced with dma_map_single












0















I am experimenting the DMA flow with and without SMMU on ARM.
Our custom application requires kernel address, physical address and bus address.



With SMMU enabled, I managed to get the kernel address and bus address using dma_alloc_coherent(), but I could not get the physical address via virt_to_phys().
That made me replace dma_alloc_coherent() with dma_map_single as follows:



// Get kernel address
res->KernelAddress = (u64)kzalloc( size , GFP_KERNEL | GFP_DMA);

// Get bus address
res->BusAddress = (u64)dma_map_single(&DevExt->pdev->dev, &res->KernelAddress, size, PCI_DMA_BIDIRECTIONAL);

// Get physical address
res->PhysicalAddress = (u64)virt_to_phys( (void*)res->KernelAddress );

dma_sync_single_for_cpu(&DevExt->pdev->dev, res->BusAddress, size, PCI_DMA_BIDIRECTIONAL);

// Map physical address to virtual address in user space
if (remap_pfn_range(vma, vma->kern_addr, vma->phys_addr,size,vma->vm_page_prot));


There was no issue with the above flow.
I managed to get 3 addresses but DMA write to the returned location failed.
Are other DMA APIs required for the above flow to work?



It is better if any snippet is shared!










share|improve this question

























  • kzalloc should work with virt_to_phys; something is wrong with your system if not. If you try virt_to_phys on another address range, it may not work. See: Physical address of vector table for alternatives to virt_to_phys.

    – artless noise
    Nov 21 '18 at 14:10













  • Is the flow correct? Should remap_pfn_range be changed to dma_mmap_coherent? But, dma_mmap_coherent expects the bus address not the physical address, doesn't it? Does vma_area_struct maintain bus address (dma_addr_t) details?

    – PBang
    Nov 22 '18 at 10:26











  • Can you please comment on this?

    – PBang
    Nov 26 '18 at 10:05











  • I don’t see how you check for errors of DMA API calls.

    – 0andriy
    Dec 1 '18 at 10:27
















0















I am experimenting the DMA flow with and without SMMU on ARM.
Our custom application requires kernel address, physical address and bus address.



With SMMU enabled, I managed to get the kernel address and bus address using dma_alloc_coherent(), but I could not get the physical address via virt_to_phys().
That made me replace dma_alloc_coherent() with dma_map_single as follows:



// Get kernel address
res->KernelAddress = (u64)kzalloc( size , GFP_KERNEL | GFP_DMA);

// Get bus address
res->BusAddress = (u64)dma_map_single(&DevExt->pdev->dev, &res->KernelAddress, size, PCI_DMA_BIDIRECTIONAL);

// Get physical address
res->PhysicalAddress = (u64)virt_to_phys( (void*)res->KernelAddress );

dma_sync_single_for_cpu(&DevExt->pdev->dev, res->BusAddress, size, PCI_DMA_BIDIRECTIONAL);

// Map physical address to virtual address in user space
if (remap_pfn_range(vma, vma->kern_addr, vma->phys_addr,size,vma->vm_page_prot));


There was no issue with the above flow.
I managed to get 3 addresses but DMA write to the returned location failed.
Are other DMA APIs required for the above flow to work?



It is better if any snippet is shared!










share|improve this question

























  • kzalloc should work with virt_to_phys; something is wrong with your system if not. If you try virt_to_phys on another address range, it may not work. See: Physical address of vector table for alternatives to virt_to_phys.

    – artless noise
    Nov 21 '18 at 14:10













  • Is the flow correct? Should remap_pfn_range be changed to dma_mmap_coherent? But, dma_mmap_coherent expects the bus address not the physical address, doesn't it? Does vma_area_struct maintain bus address (dma_addr_t) details?

    – PBang
    Nov 22 '18 at 10:26











  • Can you please comment on this?

    – PBang
    Nov 26 '18 at 10:05











  • I don’t see how you check for errors of DMA API calls.

    – 0andriy
    Dec 1 '18 at 10:27














0












0








0








I am experimenting the DMA flow with and without SMMU on ARM.
Our custom application requires kernel address, physical address and bus address.



With SMMU enabled, I managed to get the kernel address and bus address using dma_alloc_coherent(), but I could not get the physical address via virt_to_phys().
That made me replace dma_alloc_coherent() with dma_map_single as follows:



// Get kernel address
res->KernelAddress = (u64)kzalloc( size , GFP_KERNEL | GFP_DMA);

// Get bus address
res->BusAddress = (u64)dma_map_single(&DevExt->pdev->dev, &res->KernelAddress, size, PCI_DMA_BIDIRECTIONAL);

// Get physical address
res->PhysicalAddress = (u64)virt_to_phys( (void*)res->KernelAddress );

dma_sync_single_for_cpu(&DevExt->pdev->dev, res->BusAddress, size, PCI_DMA_BIDIRECTIONAL);

// Map physical address to virtual address in user space
if (remap_pfn_range(vma, vma->kern_addr, vma->phys_addr,size,vma->vm_page_prot));


There was no issue with the above flow.
I managed to get 3 addresses but DMA write to the returned location failed.
Are other DMA APIs required for the above flow to work?



It is better if any snippet is shared!










share|improve this question
















I am experimenting the DMA flow with and without SMMU on ARM.
Our custom application requires kernel address, physical address and bus address.



With SMMU enabled, I managed to get the kernel address and bus address using dma_alloc_coherent(), but I could not get the physical address via virt_to_phys().
That made me replace dma_alloc_coherent() with dma_map_single as follows:



// Get kernel address
res->KernelAddress = (u64)kzalloc( size , GFP_KERNEL | GFP_DMA);

// Get bus address
res->BusAddress = (u64)dma_map_single(&DevExt->pdev->dev, &res->KernelAddress, size, PCI_DMA_BIDIRECTIONAL);

// Get physical address
res->PhysicalAddress = (u64)virt_to_phys( (void*)res->KernelAddress );

dma_sync_single_for_cpu(&DevExt->pdev->dev, res->BusAddress, size, PCI_DMA_BIDIRECTIONAL);

// Map physical address to virtual address in user space
if (remap_pfn_range(vma, vma->kern_addr, vma->phys_addr,size,vma->vm_page_prot));


There was no issue with the above flow.
I managed to get 3 addresses but DMA write to the returned location failed.
Are other DMA APIs required for the above flow to work?



It is better if any snippet is shared!







linux-kernel arm dma mmu






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:05









artless noise

15.3k44679




15.3k44679










asked Nov 21 '18 at 10:13









PBangPBang

11




11













  • kzalloc should work with virt_to_phys; something is wrong with your system if not. If you try virt_to_phys on another address range, it may not work. See: Physical address of vector table for alternatives to virt_to_phys.

    – artless noise
    Nov 21 '18 at 14:10













  • Is the flow correct? Should remap_pfn_range be changed to dma_mmap_coherent? But, dma_mmap_coherent expects the bus address not the physical address, doesn't it? Does vma_area_struct maintain bus address (dma_addr_t) details?

    – PBang
    Nov 22 '18 at 10:26











  • Can you please comment on this?

    – PBang
    Nov 26 '18 at 10:05











  • I don’t see how you check for errors of DMA API calls.

    – 0andriy
    Dec 1 '18 at 10:27



















  • kzalloc should work with virt_to_phys; something is wrong with your system if not. If you try virt_to_phys on another address range, it may not work. See: Physical address of vector table for alternatives to virt_to_phys.

    – artless noise
    Nov 21 '18 at 14:10













  • Is the flow correct? Should remap_pfn_range be changed to dma_mmap_coherent? But, dma_mmap_coherent expects the bus address not the physical address, doesn't it? Does vma_area_struct maintain bus address (dma_addr_t) details?

    – PBang
    Nov 22 '18 at 10:26











  • Can you please comment on this?

    – PBang
    Nov 26 '18 at 10:05











  • I don’t see how you check for errors of DMA API calls.

    – 0andriy
    Dec 1 '18 at 10:27

















kzalloc should work with virt_to_phys; something is wrong with your system if not. If you try virt_to_phys on another address range, it may not work. See: Physical address of vector table for alternatives to virt_to_phys.

– artless noise
Nov 21 '18 at 14:10







kzalloc should work with virt_to_phys; something is wrong with your system if not. If you try virt_to_phys on another address range, it may not work. See: Physical address of vector table for alternatives to virt_to_phys.

– artless noise
Nov 21 '18 at 14:10















Is the flow correct? Should remap_pfn_range be changed to dma_mmap_coherent? But, dma_mmap_coherent expects the bus address not the physical address, doesn't it? Does vma_area_struct maintain bus address (dma_addr_t) details?

– PBang
Nov 22 '18 at 10:26





Is the flow correct? Should remap_pfn_range be changed to dma_mmap_coherent? But, dma_mmap_coherent expects the bus address not the physical address, doesn't it? Does vma_area_struct maintain bus address (dma_addr_t) details?

– PBang
Nov 22 '18 at 10:26













Can you please comment on this?

– PBang
Nov 26 '18 at 10:05





Can you please comment on this?

– PBang
Nov 26 '18 at 10:05













I don’t see how you check for errors of DMA API calls.

– 0andriy
Dec 1 '18 at 10:27





I don’t see how you check for errors of DMA API calls.

– 0andriy
Dec 1 '18 at 10:27












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409741%2fdma-write-fails-when-dma-alloc-coherent-is-replaced-with-dma-map-single%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409741%2fdma-write-fails-when-dma-alloc-coherent-is-replaced-with-dma-map-single%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Fotorealismo