这两天都源码时,总是看到ioremap_nocacheioremap这两个函数,本文就介绍总结一下吧。

1. 背景知识

1.1 物理地址空间

内容源于《系统虚拟化:原理与实现》2.2.1章节。


1.2 CPU cache

如果对CPU Cache概念有遗忘,可以参考此文。
本文中,读者只需明白一点:内核可以让部分物理地址空间的内存不使用cpu cache。
ps:读者若想了解细节,可以参考SDM11.3METHODS OF CACHING AVAILABLE章节。

2. 函数说明

有了背景知识后,理解这些函数就变得简单了。
首先看下内核中ioremap_nocache函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* ioremap_nocache - map bus memory into CPU space
* @phys_addr: 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 uncachable
* on the CPU.
*
* Must be freed with iounmap.
*/
void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size)

就比如图2-1,当我们需要映射512MB的I/O地址时,调用ioremap_nocache(3GB, 512MB)即可。

ioremapioremap_nocache的区别从名称中就可以看出区别:ioremap映射的物理地址空间使用CPU cache,而ioremap_nocache映射的物理地址空间不使用CPU cache。

知道函数的作用后,就可以去慢慢啃内核了。


参考资料:

  1. 《系统虚拟化:原理与实现》
  2. LINUX kernel