函数解读:ioremap、ioremap_cached、ioremap_wc-程序员宅基地

技术标签: linux内存子系统  linux  内存管理  嵌入式  

1. 本文关注的问题

在编写linux驱动过程中,不可避免的会涉及操作外设,而外设的地址空间与DDR的地址空间一般不连续,在linux上电时,并不会为外设地址空间建立页表,又因为linux访问内存使用的都是虚拟地址,因此如果想访问外设的寄存器(一般包括数据寄存器、控制寄存器与状态寄存器),需要在驱动初始化中将外设所处的物理地址映射为虚拟地址,linux为应对该问题提供了较多接口,包括ioremap/ioremap_wc/devm_ioremap/devm_ioremap_resource等,以应对不同的场景需求,本文 即阐述这些接口的使用,以及需要注意的区别。

2. 背景介绍

在系统运行时,外设IO资源的物理地址是已知的,由硬件的设计决定(参考SOC的datesheet,一般会有memorymap)。驱动程序不能通过物理地址访问IO资源,必须将其映射到内核态的虚拟地址空间(通过页表)[1],然后根据映射所得到的内核虚拟地址范围,通过线性偏移(virt_addr = virt_base + phy_addr - phy_base)访问这些IO内存资源。

3. IO资源映射与解映射

3.1 映射API(ARM体系架构,linux version:4.9.0)

代码路径:arch/arm/include/asm/io.h

/*                                                                                                  
349  * ioremap() and friends.                                                                           
350  *                                                                                                  
351  * ioremap() takes a resource address, and size.  Due to the ARM memory                             
352  * types, it is important to use the correct ioremap() function as each                             
353  * mapping has specific properties.                                                                 
354  *                                                                                                  
355  * Function             Memory type     Cacheability    Cache hint                                  
356  * ioremap()            Device          n/a             n/a                                         
357  * ioremap_nocache()    Device          n/a             n/a                                         
358  * ioremap_cache()      Normal          Writeback       Read allocate                               
359  * ioremap_wc()         Normal          Non-cacheable   n/a                                         
360  * ioremap_wt()         Normal          Non-cacheable   n/a                                         
361  *                                                                                                  
362  * All device mappings have the following properties:                                               
363  * - no access speculation                                                                          
364  * - no repetition (eg, on return from an exception)                                                
365  * - number, order and size of accesses are maintained                                              
366  * - unaligned accesses are "unpredictable"                                                         
367  * - writes may be delayed before they hit the endpoint device                                      
368  *                                                                                                  
369  * ioremap_nocache() is the same as ioremap() as there are too many device                          
370  * drivers using this for device registers, and documentation which tells                           
371  * people to use it for such for this to be any different.  This is not a  
    
372  * safe fallback for memory-like mappings, or memory regions where the                              
373  * compiler may generate unaligned accesses - eg, via inlining its own                              
374  * memcpy.                                                                                          
375  *                                                                                                  
376  * All normal memory mappings have the following properties:                                        
377  * - reads can be repeated with no side effects                                                     
378  * - repeated reads return the last value written                                                   
379  * - reads can fetch additional locations without side effects                                      
380  * - writes can be repeated (in certain cases) with no side effects                                 
381  * - writes can be merged before accessing the target                                               
382  * - unaligned accesses can be supported                                                            
383  * - ordering is not guaranteed without explicit dependencies or barrier                            
384  *   instructions                                                                                   
385  * - writes may be delayed before they hit the endpoint memory                                      
386  *                                                                                                  
387  * The cache hint is only a performance hint: CPUs may alias these hints.                           
388  * Eg, a CPU not implementing read allocate but implementing write allocate                         
389  * will provide a write allocate mapping instead.                                                   
390  */

ioremap函数组共有五个接口,根据函数实现可以分为三类:

  • ioremap & ioremap_nocache实现相同,使用场景为映射device memory类型内存;
  • ioremap_cached,使用场景为映射normal memory类型内存,且映射后的虚拟内存支持cache;
  • ioremap_wc & ioremap_wt实现相同,使用场景为映射normal memory类型内训,且映射后的虚拟内存不支持cache

3.1.1 何为memory type?

内存类型(memory type)分为设备(device)类型一般(normal)类型

详见文章:ARM体系结构的memory type与 attribute

3.1.2 ioremap & ioremap_nocache

代码路径:arch/arm/include/asm/io.h
void __iomem *ioremap(resource_size_t res_cookie, size_t size);
#define ioremap ioremap                                                                     #define ioremap_nocache ioremap

ioremap用来映射memory type为device memory的设备,同时不使用cache(device memory本身就没有cacheable这个属性),即CPU的读写操作直接操作设备内存。ioremap_nocache的实现与ioremap完全相同,保留该符号是因为向后兼容使用ioremap_nocache接口的驱动程序。

API接口中的res_cookie参数是需要映射的物理地址,size参数是需要映射的内存大小,单位是Byte。不需要考虑物理地址的页对齐问题,底层通过PAGE_ALIGN接口完成了页对齐。

3.1.3 ioremap_cached

/*                                                                                                  
* Do not use ioremap_cached in new code. Provided for the benefit of                               
* the pxa2xx-flash MTD driver only.                                                                
 */                                                                                                 
void __iomem *ioremap_cached(resource_size_t res_cookie, size_t size);

ioremap_cached用来映射memory type为normal memory的设备,同时使用cache,这会提高内存的访问速度,提高系统的性能。

3.1.4 ioremap_wc & ioremap_wt

void __iomem *ioremap_wc(resource_size_t res_cookie, size_t size);                                  
#define ioremap_wc ioremap_wc                                                                       
#define ioremap_wt ioremap_wc

ioremap_wc用来映射memory type为normal memory的设备,同时不使用cache。

3.2 解映射API

代码路径:arch/arm/include/asm/io.h
void iounmap(volatile void __iomem *iomem_cookie);

不论采用以上哪种映射方式,解映射为统一接口,其中iomem_cookie参数为映射后的虚拟地址。

4. IO资源读写过程

第3节介绍了如何映射和解映射虚拟内存,内存映射是为了访问,理论上这时候可以像读写RAM那样直接通过虚拟地址指针读写IO内存资源,但是为了保证驱动程序跨平台的可移植性【这一点还不太理解,直接指针操作怎么影响的移植性,望大家指教】,我们应该采用linux中特定的接口函数来访问IO内存[1]。常用接口如下:

代码路径:arch/arm/include/asm/io.h
299 #define readb(c)                ({
     u8  __v = readb_relaxed(c); __iormb(); __v; })                   
300 #define readw(c)                ({
     u16 __v = readw_relaxed(c); __iormb(); __v; })                   
301 #define readl(c)                ({
     u32 __v = readl_relaxed(c); __iormb(); __v; })                   
                                                                                                     
303 #define writeb(v,c)             ({
     __iowmb(); writeb_relaxed(v,c); })                               
304 #define writew(v,c)             ({
     __iowmb(); writew_relaxed(v,c); })                               
305 #define writel(v,c)             ({
     __iowmb(); writel_relaxed(v,c); })   
    
  
316 static inline void memset_io(volatile void __iomem *dst, unsigned c,                								size_t count);
324 static inline void memcpy_fromio(void *to, const volatile void __iomem *from,           							size_t count);
332 static inline void memcpy_toio(volatile void __iomem *to, const void *from,             							size_t count);

参考文档

[1].Linux内核中ioremap映射的透彻理解

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/student456852/article/details/116868447

智能推荐

JWT(Json Web Token)实现无状态登录_无状态token登录-程序员宅基地

文章浏览阅读685次。1.1.什么是有状态?有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,典型的设计如tomcat中的session。例如登录:用户登录后,我们把登录者的信息保存在服务端session中,并且给用户一个cookie值,记录对应的session。然后下次请求,用户携带cookie值来,我们就能识别到对应session,从而找到用户的信息。缺点是什么?服务端保存大量数据,增加服务端压力 服务端保存用户状态,无法进行水平扩展 客户端请求依赖服务.._无状态token登录

SDUT OJ逆置正整数-程序员宅基地

文章浏览阅读293次。SDUT OnlineJudge#include<iostream>using namespace std;int main(){int a,b,c,d;cin>>a;b=a%10;c=a/10%10;d=a/100%10;int key[3];key[0]=b;key[1]=c;key[2]=d;for(int i = 0;i<3;i++){ if(key[i]!=0) { cout<<key[i.

年终奖盲区_年终奖盲区表-程序员宅基地

文章浏览阅读2.2k次。年终奖采用的平均每月的收入来评定缴税级数的,速算扣除数也按照月份计算出来,但是最终减去的也是一个月的速算扣除数。为什么这么做呢,这样的收的税更多啊,年终也是一个月的收入,凭什么减去12*速算扣除数了?这个霸道(不要脸)的说法,我们只能合理避免的这些跨级的区域了,那具体是那些区域呢?可以参考下面的表格:年终奖一列标红的一对便是盲区的上下线,发放年终奖的数额一定一定要避免这个区域,不然公司多花了钱..._年终奖盲区表

matlab 提取struct结构体中某个字段所有变量的值_matlab读取struct类型数据中的值-程序员宅基地

文章浏览阅读7.5k次,点赞5次,收藏19次。matlab结构体struct字段变量值提取_matlab读取struct类型数据中的值

Android fragment的用法_android reader fragment-程序员宅基地

文章浏览阅读4.8k次。1,什么情况下使用fragment通常用来作为一个activity的用户界面的一部分例如, 一个新闻应用可以在屏幕左侧使用一个fragment来展示一个文章的列表,然后在屏幕右侧使用另一个fragment来展示一篇文章 – 2个fragment并排显示在相同的一个activity中,并且每一个fragment拥有它自己的一套生命周期回调方法,并且处理它们自己的用户输_android reader fragment

FFT of waveIn audio signals-程序员宅基地

文章浏览阅读2.8k次。FFT of waveIn audio signalsBy Aqiruse An article on using the Fast Fourier Transform on audio signals. IntroductionThe Fast Fourier Transform (FFT) allows users to view the spectrum content of _fft of wavein audio signals

随便推点

Awesome Mac:收集的非常全面好用的Mac应用程序、软件以及工具_awesomemac-程序员宅基地

文章浏览阅读5.9k次。https://jaywcjlove.github.io/awesome-mac/ 这个仓库主要是收集非常好用的Mac应用程序、软件以及工具,主要面向开发者和设计师。有这个想法是因为我最近发了一篇较为火爆的涨粉儿微信公众号文章《工具武装的前端开发工程师》,于是建了这么一个仓库,持续更新作为补充,搜集更多好用的软件工具。请Star、Pull Request或者使劲搓它 issu_awesomemac

java前端技术---jquery基础详解_简介java中jquery技术-程序员宅基地

文章浏览阅读616次。一.jquery简介 jQuery是一个快速的,简洁的javaScript库,使用户能更方便地处理HTML documents、events、实现动画效果,并且方便地为网站提供AJAX交互 jQuery 的功能概括1、html 的元素选取2、html的元素操作3、html dom遍历和修改4、js特效和动画效果5、css操作6、html事件操作7、ajax_简介java中jquery技术

Ant Design Table换滚动条的样式_ant design ::-webkit-scrollbar-corner-程序员宅基地

文章浏览阅读1.6w次,点赞5次,收藏19次。我修改的是表格的固定列滚动而产生的滚动条引用Table的组件的css文件中加入下面的样式:.ant-table-body{ &amp;amp;::-webkit-scrollbar { height: 5px; } &amp;amp;::-webkit-scrollbar-thumb { border-radius: 5px; -webkit-box..._ant design ::-webkit-scrollbar-corner

javaWeb毕设分享 健身俱乐部会员管理系统【源码+论文】-程序员宅基地

文章浏览阅读269次。基于JSP的健身俱乐部会员管理系统项目分享:见文末!

论文开题报告怎么写?_开题报告研究难点-程序员宅基地

文章浏览阅读1.8k次,点赞2次,收藏15次。同学们,是不是又到了一年一度写开题报告的时候呀?是不是还在为不知道论文的开题报告怎么写而苦恼?Take it easy!我带着倾尽我所有开题报告写作经验总结出来的最强保姆级开题报告解说来啦,一定让你脱胎换骨,顺利拿下开题报告这个高塔,你确定还不赶快点赞收藏学起来吗?_开题报告研究难点

原生JS 与 VUE获取父级、子级、兄弟节点的方法 及一些DOM对象的获取_获取子节点的路径 vue-程序员宅基地

文章浏览阅读6k次,点赞4次,收藏17次。原生先获取对象var a = document.getElementById("dom");vue先添加ref <div class="" ref="divBox">获取对象let a = this.$refs.divBox获取父、子、兄弟节点方法var b = a.childNodes; 获取a的全部子节点 var c = a.parentNode; 获取a的父节点var d = a.nextSbiling; 获取a的下一个兄弟节点 var e = a.previ_获取子节点的路径 vue

推荐文章

热门文章

相关标签