# 内存映射I/O

**内存映射I/O**（Memory-mapped I/O，MMIO）是一种技术，它将外围设备的寄存器和内存放置在处理器正常地址空间中的特定地址上，使软件能够用与访问RAM相同的加载和存储指令来读写硬件。<sup>[1](https://www.embeddedrelated.com/glossary/memory-mapped-io)</sup> 地址译码硬件将对那些地址的访问重定向到设备而非主存。<sup>[2](https://www.cs.yale.edu/homes/aspnes/pinewiki/InputOutput.html)</sup> 它与端口映射I/O（Port-mapped I/O，PMIO）形成对比，后者也称为隔离I/O，为设备提供一个独立的地址空间，通过专用CPU指令（如x86的`IN`和`OUT`）访问。<sup>[3](https://eng.libretexts.org/Courses/Delta_College/Introduction_to_Operating_Systems/16%3A_I_O_and_Disk_Management/16.02%3A_Types_of_I_O)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 设备寄存器占用处理器正常内存地址空间中的地址，用普通的加载/存储指令访问<sup>[1](https://www.embeddedrelated.com/glossary/memory-mapped-io)</sup> |
| 替代方案 | 端口映射（隔离）I/O使用独立地址空间和专用指令；在x86上，端口空间为16位，而内存寻址可达36位<sup>[2](https://www.cs.yale.edu/homes/aspnes/pinewiki/InputOutput.html)</sup> |
| 典型示例 | PC架构将0xA0000–0xFFFF0（640K–1M）保留给内存映射I/O，包括VGA显示内存<sup>[2](https://www.cs.yale.edu/homes/aspnes/pinewiki/InputOutput.html)</sup> |
| 架构影响 | PDP-11是第一款要求使用内存映射I/O的小型计算机；68000和所有RISC处理器也要求使用它<sup>[4](https://www.sciencedirect.com/topics/engineering/input-output-interface)</sup> |
| 现代状况 | ARM、RISC-V、MIPS、PowerPC和Alpha仅使用内存映射I/O；x86则同时保留端口I/O<sup>[5](https://www.dranirbanchakraborty.com/learn/computer-architecture/io-architecture)</sup> |
| 映射要求 | MMIO区域必须映射为非缓存内存；Linux的`ioremap()`为控制寄存器创建非缓存、非推测、非重排的映射<sup>[6](https://docs.kernel.org/6.2/driver-api/device-io.html)</sup> |
| `volatile` | 将每次访问标记为编译器不能消除、缓存或相对于其他volatile访问重排的可观察的副作用<sup>[7](https://raybench.in/embedded-c/mmio-volatile)</sup> |

## 工作原理

一个外围设备暴露一小组寄存器，如控制寄存器、状态寄存器和数据寄存器，系统设计者为每个寄存器在处理器的物理地址空间中分配一个地址。通常一部分地址空间被保留给I/O端口；例如，一种常见布局将高端的4千字节保留给I/O，但只要没有内存占用该地址，设计者就可以将端口放在任何地址。<sup>[4](https://www.sciencedirect.com/topics/engineering/input-output-interface)</sup> 当CPU执行加载或存储时，地址转换和总线硬件将落在设备范围内的地址路由到设备而非RAM。<sup>[2](https://www.cs.yale.edu/homes/aspnes/pinewiki/InputOutput.html)</sup>

由于设备寄存器就是地址，所有内存引用指令——不仅仅是简单的加载和存储——都可以操作它们，可用端口地址的数量实际上是无限的，且I/O指令不消耗任何操作码或处理器电路。<sup>[4](https://www.sciencedirect.com/topics/engineering/input-output-interface)</sup> 代价是部分内存地址空间被占用，设备接口可能需要更多电路来识别更长的地址。<sup>[4](https://www.sciencedirect.com/topics/engineering/input-output-interface)</sup> 在32位系统上这是一个实际问题：内存映射设备消耗RAM无法占用的物理地址空间，这是32位Windows在某些PC上无法使用全部4 GB已安装RAM的原因之一。<sup>[8](https://stackoverflow.com/questions/15371953/memory-mapped-i-o-vs-port-mapped-i-o)</sup>

## 与端口映射I/O的对比

端口映射I/O为设备提供自己的地址空间，完全独立于内存地址空间。<sup>[9](https://ukma-fin-csa-2026.github.io/materials/lectures/eng/l08-eng.-memory-systems-io-devices-and-low-level-data-structures.html)</sup> Intel架构为此提供了`IN`和`OUT`指令，M/IO#等控制信号在总线上区分I/O周期和内存周期。<sup>[10](https://research.cs.queensu.ca/home/cisc221s/IO.html)</sup><sup> • </sup><sup>[11](https://www.onenoughtone.com/learn/memory-mapped-io)</sup> 独立空间的优点是整个内存范围保持可用于RAM和ROM，I/O端口天然不可缓存，且不同的指令使I/O操作在代码中一目了然。<sup>[11](https://www.onenoughtone.com/learn/memory-mapped-io)</sup> 缺点是需要汇编语言或特殊内建函数，需要与内存保护不同的特权机制，以及端口数量有限：在x86上只有16位端口地址，而内存地址多达36位。<sup>[2](https://www.cs.yale.edu/homes/aspnes/pinewiki/InputOutput.html)</sup> AMD在定义x86-64时没有扩展端口I/O指令以支持64位端口，因此无法使用端口I/O执行64位传输。<sup>[3](https://eng.libretexts.org/Courses/Delta_College/Introduction_to_Operating_Systems/16%3A_I_O_and_Disk_Management/16.02%3A_Types_of_I_O)</sup>

两种方法是互补的，实际系统会混合使用。最初的IBM PC使用I/O端口连接键盘控制器、中断控制器、定时器以及串行和并行端口，而VGA图形帧缓冲则位于0xA0000的内存映射空间中。<sup>[2](https://www.cs.yale.edu/homes/aspnes/pinewiki/InputOutput.html)</sup><sup> • </sup><sup>[12](https://shakenfist.com/components/cloudgood/memory-mapped-devices/)</sup> 混合使用的理由是：I/O指令适合快速变化的小数据，如控制寄存器；而内存映射适合批量读写的大块数据，如显示内存。<sup>[2](https://www.cs.yale.edu/homes/aspnes/pinewiki/InputOutput.html)</sup> 历史上对端口I/O的推动来自小地址空间：[Intel 8080](https://www.edgepedia.org/zh/intel-8080)只提供64 KB的内存寻址，因此将其中任何部分用于设备都代价高昂。<sup>[11](https://www.onenoughtone.com/learn/memory-mapped-io)</sup> 在IBM PC上，这种共存有一个可见的后果：在其20位寻址的640–1024 KiB范围内放置的上位内存区，正是老机器上640 KiB壁垒的起源。<sup>[13](https://ebooks.lpude.in/computer_application/ad/DCAP210_INTRODUCTION_TO_MICROPROCESSORS.pdf)</sup>

## 缓存、顺序与副作用

内存映射寄存器不是变量。读取它可能改变设备状态，写入它可能触发物理动作，其值可能在程序没有任何存储操作的情况下发生变化。<sup>[7](https://raybench.in/embedded-c/mmio-volatile)</sup> 因此将I/O当作内存处理会与缓存产生严重冲突：如果状态寄存器的读取由过期的缓存行提供，软件可能永远看不到操作已完成。<sup>[14](https://lycoristechnologies.com/blog/what-is-memory-mapped-io/)</sup> 在C语言中，`volatile`是确保每次访问都是真实加载或存储的机制，相对于其他volatile访问按程序顺序排列，但这只是一个有限的承诺：它不保证访问的原子性，不提供volatile访问与普通访问之间的顺序保证，也不生成总线或缓存可能需要的任何内存屏障。<sup>[7](https://raybench.in/embedded-c/mmio-volatile)</sup>

架构通过内存类型来解决这个问题。ARM架构为内存映射外设定义了Device内存和Strongly-ordered内存类型，其中访问必须严格按照程序指定的次数发生；对同一位置的两次写入必须作为两次写入执行。<sup>[15](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/memory-access-ordering-part-3---memory-access-ordering-in-the-arm-architecture)</sup> Armv8-A参考材料同样将Device内存描述为可能具有副作用的存储位置的类型，例如推进到下一数据项的FIFO读取。<sup>[16](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Armv8-A%20memory%20model%20guide.pdf?revision=58b1dd0a-3800-4218-b21a-f95a0332034c)</sup>

## 操作系统如何管理MMIO

Linux在所有总线上抽象设备I/O，使驱动程序可以独立于总线类型编写。<sup>[6](https://docs.kernel.org/6.2/driver-api/device-io.html)</sup> 收到设备物理地址的驱动程序调用`ioremap()`获得适合访问器函数的地址，完成后调用`iounmap()`；大多数架构在每次`ioremap()`时分配新的地址空间，如果不释放映射可能会耗尽。<sup>[6](https://docs.kernel.org/6.2/driver-api/device-io.html)</sup> 在大多数架构上，`ioremap()`为指向物理MMIO地址的非缓存虚拟地址创建页表项；在某些架构上需要特殊指令，指针只是由访问器解释的令牌。<sup>[17](https://docs.kernel.org/next/driver-api/device-io.html)</sup>

通用访问器包括`readb()`、`readw()`、`readl()`、`readq()`，以及对应的`write*()`函数，分别用于8、16、32和64位数量，还有`_relaxed`变体，在性能敏感路径上使用更廉价的屏障。<sup>[6](https://docs.kernel.org/6.2/driver-api/device-io.html)</sup><sup> • </sup><sup>[17](https://docs.kernel.org/next/driver-api/device-io.html)</sup> 对于帧缓冲等设备的大块传输，提供了`memcpy_toio()`、`memcpy_fromio()`和`memset_io()`，普通`memcpy`不得用于I/O地址，因为它不保证按序复制。<sup>[6](https://docs.kernel.org/6.2/driver-api/device-io.html)</sup>

不同映射模式在保证上各有取舍。默认的`ioremap()`是非缓存、非推测、非重排、非合并的，适用于控制寄存器。`ioremap_wc()`允许写合并、推测和重排，通常用于视频帧缓冲以提高写入性能。`ioremap_np()`显式请求非发布式写入，即写指令在设备收到数据之前不会显示完成；在许多平台和总线（如PCI）上，普通写入是发布式的，意味着CPU在指令退役前不等待写入到达设备。<sup>[17](https://docs.kernel.org/next/driver-api/device-io.html)</sup> PCI内存空间写入始终是发布式的，因此`ioremap_np()`绝不应在PCI驱动中使用；确保发布式写入已完成的正常方法是随后从同一设备执行一次哑读。<sup>[17](https://docs.kernel.org/next/driver-api/device-io.html)</sup>

在嵌入式系统上，寄存器块是静态描述的。在设备树中，`reg`属性给出设备的内存映射I/O寄存器块在其父总线定义的地址空间中的偏移和长度。<sup>[18](https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter2-devicetree-basics.rst)</sup> 在PCI系统上，软件动态分配地址：PCI设备的地址由系统软件分配并写入设备，仅在设备上电期间保持。<sup>[19](https://www.nxp.com/docs/en/reference-manual/Linux%20Device%20Drivers.pdf)</sup>

## 当今MMIO的使用场景

除x86外，所有主要架构——包括RISC-V、ARM、MIPS、PowerPC和Alpha——都仅使用内存映射I/O，设备寄存器占用正常物理地址范围。<sup>[5](https://www.dranirbanchakraborty.com/learn/computer-architecture/io-architecture)</sup> 地址被映射到内核虚拟地址空间作为非缓存内存，使加载和存储绕过数据缓存并在每次访问时到达设备。<sup>[5](https://www.dranirbanchakraborty.com/learn/computer-architecture/io-architecture)</sup> 对快速设备的研究甚至重新审视了编程I/O，即CPU通过加载和存储直接与设备之间传输数据和控制信息，认为在某些用例中它可能比传统的DMA加中断模型更高效。<sup>[20](https://arxiv.org/abs/2409.08141)</sup>

## References

1. [Memory-Mapped I/O, Embedded Systems Glossary, EmbeddedRelated](https://www.embeddedrelated.com/glossary/memory-mapped-io)
2. [InputOutput, Yale University (James Aspnes)](https://www.cs.yale.edu/homes/aspnes/pinewiki/InputOutput.html)
3. [16.2: Types of I/O, Engineering LibreTexts](https://eng.libretexts.org/Courses/Delta_College/Introduction_to_Operating_Systems/16%3A_I_O_and_Disk_Management/16.02%3A_Types_of_I_O)
4. [Input Output Interface, ScienceDirect Topics](https://www.sciencedirect.com/topics/engineering/input-output-interface)
5. [I/O Architecture, Dr. Anirban Chakraborty](https://www.dranirbanchakraborty.com/learn/computer-architecture/io-architecture)
6. [Bus-Independent Device Accesses, Linux Kernel documentation (v6.2)](https://docs.kernel.org/6.2/driver-api/device-io.html)
7. [MMIO, Volatile & Access Semantics, RayBench Embedded](https://raybench.in/embedded-c/mmio-volatile)
8. [Memory mapped I/O vs Port mapped I/O, Stack Overflow](https://stackoverflow.com/questions/15371953/memory-mapped-i-o-vs-port-mapped-i-o)
9. [Memory Systems, I/O Devices and Low-Level Data Structures, lecture notes](https://ukma-fin-csa-2026.github.io/materials/lectures/eng/l08-eng.-memory-systems-io-devices-and-low-level-data-structures.html)
10. [I/O Devices, Software and Hardware Interrupts, Queen's University](https://research.cs.queensu.ca/home/cisc221s/IO.html)
11. [Memory-Mapped I/O Learning Module, OneNoughtOne](https://www.onenoughtone.com/learn/memory-mapped-io)
12. [Memory mapped devices, Shaken Fist documentation](https://shakenfist.com/components/cloudgood/memory-mapped-devices/)
13. [Introduction to Microprocessors, LPU e-books](https://ebooks.lpude.in/computer_application/ad/DCAP210_INTRODUCTION_TO_MICROPROCESSORS.pdf)
14. [What Is Memory-Mapped I/O? MMIO Explained, Lycoris Technologies](https://lycoristechnologies.com/blog/what-is-memory-mapped-io/)
15. [Memory access ordering part 3: Memory access ordering in the Arm Architecture, Arm Community](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/memory-access-ordering-part-3---memory-access-ordering-in-the-arm-architecture)
16. [Armv8-A memory model guide, Arm Developer](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Armv8-A%20memory%20model%20guide.pdf?revision=58b1dd0a-3800-4218-b21a-f95a0332034c)
17. [Bus-Independent Device Accesses, Linux Kernel documentation (next)](https://docs.kernel.org/next/driver-api/device-io.html)
18. [Devicetree Specification, chapter 2 basics, devicetree-org on GitHub](https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter2-devicetree-basics.rst)
19. [Linux Device Drivers, 2nd Edition, NXP mirror](https://www.nxp.com/docs/en/reference-manual/Linux%20Device%20Drivers.pdf)
20. [Rethinking Programmed I/O for Fast Devices, Cheap Cores, and Coherent Interconnects, arXiv](https://arxiv.org/abs/2409.08141)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 计算机硬件 › 处理器与处理器工程 › 计算机体系结构理论 › 计算机体系结构（概述）

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/memory-mapped-i-o

English original: https://www.edgepedia.org/memory-mapped-i-o
