我如何在 linux 内核中的非常特定的范围内分配 dma 缓冲区?

How can I allocate dma buffers in a very specific range in linux kernel?

我有一个微处理器,它只能通过其数据缓存访问 DDR 中的 0xFFFFFF 字节。我可以给它一个偏移量,它可以开始读取这些 0xFFFFFF DDR

我想为它分配 dma 缓冲区。

我看到我可以给一个掩码来限制这些dma缓冲区可以分配的地方。

我如何知道dma_coherent_alloc分配将从哪里开始以设置微处理器的偏移量?

还有其他解决办法吗? 就像分配一个大缓冲区然后在其中做一些子分配(我不知道内核是否已经有一些设施)

提前致谢。

你需要使用 dma_alloc_coherent 只有在你有多个处理器的情况下。

如果是微处理器,您可以只使用 kmalloc 并禁用缓存并使用 dma_map_sg() 获取 dma 句柄!

如果你想在特定区域分配,你可以在设备树中保留这块内存,并通过DMA使用它-API(例如dma_alloc_coherent)

在您的设备树中:

reserved-memory {
      #address-cells = <2>;
      #size-cells = <2>;
      ranges;
  
      reserved: buffer@0 {
         compatible = "shared-dma-pool"; // to be able to access this memory throught the DMA-API
         no-map;
         reg = <0x0 0x70000000 0x0 0x10000000>;
      };
   };
  
   reserved-driver@0 {
      compatible = "xlnx,reserved-memory";
      memory-region = <&reserved>;
   };

在您的设备驱动程序中:

  /* Initialize reserved memory resources */
  rc = of_reserved_mem_device_init(dev);
  if(rc) {
    dev_err(dev, "Could not get reserved memory\n");
    goto error1;
  }

  /* Allocate memory */
  dma_set_coherent_mask(dev, 0xFFFFFFFF);
  lp->vaddr = dma_alloc_coherent(dev, ALLOC_SIZE, &lp->paddr, GFP_KERNEL);
  dev_info(dev, "Allocated coherent memory, vaddr: 0x%0llX, paddr: 0x%0llX\n", (u64)lp->vaddr, lp->paddr);

很好地解释了这是如何工作的:https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/18841683/Linux+Reserved+Memory