linux中ioremap與ioremap_cachable的區別(mips架構)
在arch/mips/include/asm/io.h
/*
* ioremap - map bus memory into CPU space
* @offset: bus address of the memory
* @size: size of the resource to map
*
* ioremap performs a platform specific sequence of operations to
* make bus memory CPU accessible via the readb/readw/readl/writeb/
* writew/writel functions and the other mmio helpers. The returned
* address is not guaranteed to be usable directly as a virtual
* address.
*/
#define ioremap(offset, size) \
__ioremap_mode((offset), (size), _CACHE_UNCACHED)
/*
* ioremap_cachable - map bus memory into CPU space
* @offset: bus address of the memory
* @size: size of the resource to map
*
* ioremap_nocache performs a platform specific sequence of operations to
* make bus memory CPU accessible via the readb/readw/readl/writeb/
* writew/writel functions and the other mmio helpers. The returned
* address is not guaranteed to be usable directly as a virtual
* address.
*
* This version of ioremap ensures that the memory is marked cachable by
* the CPU. Also enables full write-combining. Useful for some
* memory-like regions on I/O busses.
*/
#define ioremap_cachable(offset, size) \
__ioremap_mode((offset), (size), _page_cachable_default)
#define ioremap_cached(offset, size) ioremap_cachable(offset, size)
static inline void __iomem * __ioremap_mode(phys_t offset, unsigned long size,
unsigned long flags)
{
void __iomem *addr = plat_ioremap(offset, size, flags);
if (addr)
return addr;
#define __IS_LOW512(addr) (!((phys_t)(addr) & (phys_t) ~0x1fffffffULL))
if (cpu_has_64bit_addresses) {
u64 base = UNCAC_BASE;
/*
* R10000 supports a 2 bit uncached attribute therefore
* UNCAC_BASE may not equal IO_BASE.
*/
if (flags == _CACHE_UNCACHED)
base = (u64) IO_BASE;
return (void __iomem *) (unsigned long) (base + offset);
} else if (__builtin_constant_p(offset) &&
__builtin_constant_p(size) && __builtin_constant_p(flags)) {
phys_t phys_addr, last_addr;
phys_addr = fixup_bigphys_addr(offset, size);
/* Don't allow wraparound or zero size. */
last_addr = phys_addr + size - 1;
if (!size || last_addr < phys_addr)
return NULL;
/*
* Map uncached objects in the low 512MB of address
* space using KSEG1.
*/
if (__IS_LOW512(phys_addr) && __IS_LOW512(last_addr) &&
flags == _CACHE_UNCACHED)
return (void __iomem *)
(unsigned long)CKSEG1ADDR(phys_addr);
}
return __ioremap(offset, size, flags);
#undef __IS_LOW512
}
其中__builtin_constant_p(x)作用是用來確定一個值在編譯時間是否為常量(use to determine whether a value is a constant at compile-time)。
如果x在編譯的時候就能獲得常值,則為TRUE;如果是變數則為FALSE
在arch/mips/mm/ioremap.c中:
/*
* Remap an arbitrary physical address space into the kernel virtual
* address space. Needed when the kernel wants to access high addresses
* directly.
*
* NOTE! We need to allow non-page-aligned mappings too: we will obviously
* have to convert them into an offset in a page-aligned mapping, but the
* caller shouldn't need to know that small detail.
*/
void __iomem * __ioremap(phys_t phys_addr, phys_t size, unsigned long flags)
{
...
}
下面這段文字轉自http://blog.csdn.net/bing_bing/article/details/5774294
void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
入口: phys_addr:要映射的起始的IO地址;
size:要映射的空間的大小;
flags:要映射的IO空間的和許可權有關的標誌;
功能: 將一個IO地址空間映射到核心的虛擬位址空間上去,便於訪問;
實現:對要映射的IO地址空間進行判斷,低PCI/ISA地址不需要重新對應,也不允許使用者將IO地址空間映射到正在使用的RAM中,最後申請一個vm_area_struct結構,調用remap_area_pages填寫頁表,若填寫過程不成功則釋放申請的vm_area_struct空 間;
意義:
比如isa裝置和pci裝置,或者是fb,硬體的跳線或者是物理串連方式決定了硬體上的記憶體影射到的cpu物理地址。
在核心訪問這些地址必須分配給這段記憶體以虛擬位址,這正是__ioremap的意義所在 ,需要注意的是,實體記憶體已經"存在"了,無需alloc page給這段地址了.
檔案中的注釋也是比較詳盡的,並且只 暴露了__ioremap,iounmap兩個函數供其他模
塊調用,函數remap_area_pte,remap_area_pmd,remap_area_pages只為__ioremap所用.
--------
為了使軟體訪問I/O記憶體,必須為裝置分配虛擬位址.這就是ioremap的工作.這個函數專門用來為I/O記憶體地區分配虛擬位址(空間).對於直接映射的I/O地址ioremap不做任何事情。有了ioremap(和iounmap),裝置就可以訪問任何I/O記憶體空間,不論它是否直接映射到虛擬位址空間.但是,這些地址永遠不能直接使用(指物理地址),而要用readb這種函數.
根據電腦平台和所使用匯流排的不同,I/O 記憶體可能是,也可能不是通過頁表訪問的,通過頁表訪問的是統一編址(PowerPC),否則是獨立編址(Intel)。如果訪問是經由頁表進行的,核心必須首先安排物理地址使其對裝置驅動 程式可見(這通常意味著在進行任何 I/O 之前必須先調用 ioremap)。如果訪問無需頁表,那麼 I/O 記憶體地區就很象 I/O 連接埠,可以使 用適當形式的函數讀寫它們。
不管訪問 I/O 記憶體時是否需要調用 ioremap,都不鼓勵直接使用指向 I/O 記憶體的指標。儘管(在“I/O 連接埠和 I/O 記憶體” 介紹過)I/O 記憶體在硬體一級是象普通 RAM 一樣定址的,但在“I/O 寄存器和常規記憶體”中描述過的那些需要額外小心的情況中已經建議不要使用普 通指標。相反,使用“封裝的”函數訪問 I/O 記憶體,一方面在所有平台上都是安全的,另一方面,在可以直接對指標指向的記憶體地區執行操作的時候,該函數 是經過最佳化的
-------
自己原以為當給顯卡上的儲存空間分配了匯流排地址A以後,它所對應的虛擬空間就隨之確定了.也就是A+3G.可是事實上,在ioremap.c檔案裡面的實現並不是這樣的.所用的函數是 __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)實現的時候是為從phys_addr開始的size大小的物理地址分配一塊虛擬位址.注意這裡是分配,而不是指定.我所認為的分配應該是指定即根據phys_addr得到其所對應的虛擬位址是phys_addr+3G. 本人認為一合理的解釋是這樣的:系統虛擬空間中映射的非IO卡上的地址空間滿足3G差關係,而IO卡上的儲存空間就不滿足了.歡迎討論
在X86體系下的,CPU的物理地址和PCI匯流排地址共用一個空間。linux核心將3G-4G的虛擬位址固定映射到了物理地址的0-1G的地方。但是如果外圍裝置上的地址高於1G,例如某塊PCI卡分配到了一個高於1G的地址,就需要調用ioremap來重建立立該物理地址(匯流排地址)和虛擬位址之間的映射。這個映射過程是這樣的:在ioremap.c檔案的__ioremap函數中首先對將來映射的物理地址進行檢查,也就是不能重新對應640K-1M地址(由於曆史的原因,物理地址640k到1M空間被保留給了顯卡),普通的ram地也不能重新被映射。之後調用get_vm_area獲得可用的虛擬位址,然後根這虛擬位址和欲映射的物理地址修改頁表,之後核心就可以用這個虛擬位址來訪問映射的物理地址了。
http://bbs.chinaunix.net/thread-3554853-1-1.html
|
什麼情況用cacheable,什麼情況下必須non-cacheable。
CPU能幫你維護好 RAM的一致性, 也就是CPU或DMA該RAM時另一個會知道並做出相應動作,那就用cachable,因為效能好。
CPU和DMA各管各的,那就non-cachable,因為省得折騰。 |
for x86應該是cachable的, 因為體繫結構已經保證了一致性
我覺得這裡並不是一致性的問題。ioremap的memory是按指令順序直接寫到device上的。
如果是cacheable的話,假設我們
write reg1
write reg2
如果是cacheable的話,值就會被cache在cpu cache裡,這兩個值寫到memeory的順序就是不定的。
cache一致性只是保證cache和memory裡的值的一致性,x86上由硬體保證。假如cache裡的值是新的,memory裡的是舊的。那麼另外一個cpu,或者device想access這塊memory的值,那麼cache一致性就保證他們會獨到最新的值。