2011年9月26日 星期一

I/O Addressing and Memory Mapped I/O

Driver的功能是幫助kernel控制硬體, Kernel不了解的裝置就交給driver, 透過drive, user program可以藉由system call來控制硬體

80386提供兩種input/output的控制方式去讀寫裝置上的register
1. 獨立的I/O address space (I/O mapped I/O)
2. Memory mapped I/O (和main memory共同使用memory address space)

I/O address space
- cat /proc/ioports
- 提供2^16 (64K) address space
- 每次可讀寫1, 2, 4btyes
- inb(), outb(), inw(), outw()...
- 每個硬體裝置都有自己的I/O port address space, 必須保證其他硬體不會重複使用
- instructions in and out move data between a register and a port I/O address space

Memory-mapped I/O
- cat /proc/iomem
- 以讀寫記憶體代替I/O port
- Place the I/O devices in the 80386 memory address space.
- Driver只要向預先定義好的位址範圍做讀寫 (4G - 3G 空間可當作address mapping)
- ioremap(), ioremap_nocache(), iounmap()
- 此方法提供較大的彈性, 使用mov, and, or就可值接對裝置的device register做運算
- Memory map定址到3G-4G的physical address space, 但實體記憶體可能小於4G, 所以當軟體讀寫這個區段的時候, 會被硬體當作memory map的讀寫動作, 然後轉換成對裝置的讀寫.
(see ref2, chapter 12 PCI).
(如果所安裝的實體記憶體超過3G, 就需要PAE: physical address extension support)

PCI
- 讀寫PCI裝置時可用I/O address space or Memory mapped I/O
- Linux提供interface來統一處理兩種不同的讀寫方式
- pci_iomap(), ioread8(), iowrite8()
- IA-32的平台式大部份透過I/O mapping來讀寫配置空間 (configuration space).

IOMMU 
- MMIO必須要占掉一塊連續的physical memory讓device做mapping, IOMMU maintain一個table用來做translation, 因此不需要連續的physical memory. Vector I/O (scatter-gather list) can be avoided.
-  In virtual environment, VM會設定DMA map到guest physical, 但guest physical無法直接存取host physical memory. IOMMU 會做remap, 把guest physical address map到host physical上來達成DMA. 所以native device driver就可以直接讓gust VM使用.
- IOMMU概念類似Extended Page Table, 負責做guest virtual -> guest physical -> host physical的後半段translation. (其實EPT就是MMU的延伸版, 多做一次轉換從guest physical to host physical)
- MMU是用來做CPU virtual address space轉換到Main memory, IOMMU則是用來轉換device address space 到 Main memory
- IOMMU is not used when the CPU communicates with devices via I/O ports.
- Connect a DMA-capable I/O bus to the main memory.
- Intel VT-d (Virtualization Technology for Directed I/O)

IOMMU in relation to virtualization:
Guest OS would like to directly access the I/O to improve performance using DMA. If the guest OS can directly instruct the hardware from guest physical memory, it would likely to corrupt the memory. The corruption is avoided because of hypervior's intervention to apply translation. Unfortunately this delays the I/O operation.
An IOMMU can solve this problem by re-mapping the addresses accessed by the hardware according to the same (or a compatible) translation table [from wiki IOMMU]

From wiki: IOMMU

Reference:
1. http://www.cs.sunysb.edu/~porter/courses/cse506/f11/ref/i386/toc.htm
2. Linux Device Driver Programming 驅動程式設計
3. http://en.wikipedia.org/wiki/IOMMU

沒有留言:

張貼留言