Re: Physical address

From: William Lee Irwin III
Date: Mon Dec 08 2003 - 10:24:33 EST


On Mon, Dec 08, 2003 at 04:07:13PM +0100, moi toi wrote:
> Does that mean that the functions virt_to_phys and
> virt_to_bus don't work on virtual addresses? Does anyone know, how to
> get the real physical address of the buffer.

Linux assumes 1:1 mapping of kernel virtual addresses, with various
exceptions.

virt_to_bus() and virt_to_phys() only work on that statically mapped
area's virtual addreses.

ioremap() dynamically establishes translations for the areas, and
the virtual addresses returned by it are not valid to resolve to
physical addresses with virt_to_bus() and virt_to_phys().

Probably the best way to find the physical address is by a direct
pagetable lookup. Something like the following (untested) may be of use:

static dma_addr_t ioremap_to_phys(unsigned long vaddr)
{
pgd_t *pgd;
pmd_t *pmd;
pte_t *pte;

pgd = pgd_offset_k(vaddr);
if (!pgd_present(*pgd))
return 0;
pmd = pmd_offset(pgd, vaddr);
if (!pmd_present(*pmd))
return 0;
pte = pte_offset_kernel(pmd, vaddr);
if (!pte_present(*pte))
return 0;
return ((dma_addr_t)pte_pfn(*pte) << PAGE_SHIFT) | (addr & ~PAGE_MASK);
}


-- wli
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/