Memory management---common functions for allocating memory (allocation method when using DMA mechanism: return virtual address and physical address)

makefile

  1 LINUX_SRC :=/home/liuye/tiny4412/FriendlyARM_kernel/linux- 3.5 
  2 #obj-m += module.o #Generate module.ko but we can write module.o
   3 #module-objs= param.o #here Write the file to be compiled
   4 obj-m += mmc_v_p.o #This is a single-file compilation: the above two lines are multi-file compilation, which is more convenient
   5  
  6 all:
   7          make -C $(LINUX_SRC) M= `pwd` modules
   8 clean:
   9          make -C $(LINUX_SRC) M= `pwd` modules clean
  10         

mmc_v_p.c

  1 #include <linux/init.h>
   2 #include <linux/sched.h>
   3 #include <linux/module.h>
   4 #include <linux/mm.h>
   5 #include <linux/highmem.h>
   6 #include <linux/vmalloc.h>
   7 #include <linux/dma-mapping.h>
   8 #include <linux/slab.h>
   9  // find 
 10  // The kernel code works in two environments: 
 11  / /       1/Interrupt context does not allow sleep 
 12  //       2/Process context allows sleep 
 13  
 14  /*
 15 Many page structures stored in the kernel are consecutive: page+1 is the address of the next page
 16 page
 17 page
 18 page
 19 page
 20 page
 21 page
 22 */ 
 23  // In the kernel program, the requested memory must not have memory leaks. If it is used up, the kernel will crash. 
 24  // In the application program, if the requested memory is not used, more and more If more, the kernel will kill the process, but no one can kill the kernel due to memory leaks in the kernel. 
 25  static __init int test_init( void )
  26 {
  27          char *v; // virtual address 
 28          dma_addr_t paddr; / / physical address 
 29          // curent: pointer: pointing to the task_struct structure of this process: used to describe process information: comm process name 
 30          printk( " process:%s\n " ,current-> comm);
  31          // Apply for dma Memory to be accessed: NULL: The device type should be filled in here, which will be explained later
 32          // General sound card drivers, framebuffer and other drivers that require dma hardware support need to use this function 
 33          v = dma_alloc_coherent(NULL, 480 * 800 * 4 ,& paddr,GFP_KERNEL);
  34  
 35  
 36          // Release: 
 37          dma_free_coherent( NULL, 480 * 800 * 4 ,v,paddr);
  38  
 39  
 40  
 41          return  0 ;
  42 }
  43  static __exit void test_exit( void )
  44 {
  45          printk("mmc exit!\n");
 46 }
 47 
 48 module_init(test_init);
 49 module_exit(test_exit);
 50 MODULE_LICENSE("GPL");
 51 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325088797&siteId=291194637
Recommended