1998-12-11 23:24:59

by Jes Sorensen

[permalink] [raw]
Subject: Re: Access to I/O-mapped / Memory-mapped resources

>>>>> "Ivan" == Linux Lists <[email protected]> writes:

Ivan> Hi, there,

Ivan> I have a question: is there any reference in regards to how /
Ivan> when to use the virt_to_phys, virt_to_bus, ioremap,
Ivan> etc. ... functions, other than
Ivan> /usr/src/linux/Documentation/IO-mapping.txt ?!? I'd like to
Ivan> understand it better, but this text has not been enough (for me,
Ivan> of course).

It's really quite simple:

When you want to map shared memory provided by a device, in most cases
this will PCI shared memory, use ioremap() (or equivalent on other
archs) to map it into kernel shared memory. By doing this the kernel
can directly access this memory, though you really want to use the
read[bwl]()/write[bwl]() macros to access it to ensure portabillity of
your code (ie. Alphas with this wicked sparse memory).

Use virt_to_bus()/bus_to_virt() when you want to convert memory
addresses from/to kernel virtual address and the addresses as seen
from your bus(ses). Ie. on some machines, the physical memory layout
looks different from the CPU as it does from devices on the PCI (or
other) bus(ses). Thus you will normally use these functions to convert
addresses when filling ring descriptors for things like Ethernet
interfaces and other bus master devices (I just use network interfaces
as an example as this is what I hack the most).

virt_to_phys()/phys_to_virt() converts between kernel virtual and
physical addresses, the physical addresses as seen by the main system
that it. These functions are mainly used by the memory management
code. An example is conversion of virtual addresses to physical ones
on architectures that has the cache physically mapped and needs to do
deal with cache coherency in software and the cache management
functions requiring physical addresses (like the m68k).

Conclusion, for most driver related stuff you want to use
virt_to_bus()/bus_to_virt(). However you don't need to deal with any
of this when dealing with non bus mastering devices that only uses
traditional PC ISA I/O (all the inb()/outb() crap).