linux reserved memory用法(ioremap)

主要做项目时候,需要分配一块连续的大的物理内存。

1.kmalloc 不能分配那么大。

2.alloc_pages MAX_ORDER为11,也就是最大分配2的11次方的页。

reserverd memory在最开始时候分配好,这块内存不会再分配给kernel用。主要更改devicetree。

  1.  
    memory {
  2.  
    device_type = "memory";
  3.  
    reg = <0x0 0x40000000>; //512m DDR
  4.  
    };
  5.  
    reserved-memory {
  6.  
    #address-cells = <1>;
  7.  
    #size-cells = <1>;
  8.  
    ranges;
  9.  
    test_reserved: test@1c000000 {
  10.  
    compatible = "test,test-memory";
  11.  
    reg = <0x1c000000 0x4000000>; //起始内存,内存大小
  12.  
    no-map; //禁止操作系统做逻辑地址映射,开始我没加这句话,ioremap出错了。
  13.  
    };
  14.  
    };

验证代码:

  1.  
    p_malloc = (char *)ioremap(0x1c000000,0x4000000); //做映射
  2.  
    char *p_tmp = p_malloc;
  3.  
    if(p_malloc == NULL) {
  4.  
    printk("virtual add is error \n");
  5.  
    }else {
  6.  
    for(i = 0; i < 1024; i++) {
  7.  
    *p_tmp++ = i;
  8.  
    }
  9.  
    p_tmp = p_malloc;
  10.  
    for(i = 0; i < 1024; i++) {
  11.  
    printk("%x \n",*p_tmp++);
  12.  
    }
  13.  
posted @ 2020-11-15 14:37  aspirs  阅读(1579)  评论(0编辑  收藏  举报