如何读取设备树中保留内存的地址

How to read address of reserved memory in device tree

我正在 Linux 中为小型设备编写设备驱动程序。该设备有一些特殊的内存限制,迫使我开出一块内存,在我的驱动程序中,我需要知道保留内存的地址(和大小)

/ {
    reserved-memory {
        my_reserve: my_reserve@a0000000 {
            compatible = "shared-dma-pool";
            reg = <0 0xa0000000 0 0x20000>;
            no-map;
        };
    };

    my_device {
        compatible = "my_device";
        memory-region = <&my_reserve>;
    };
};

如何在设备驱动程序中读取保留内存的物理地址(即如何读取值 0xa000'0000)?似乎有一个 API 用于保留内存,但我看不到 returns 一个 struct reserved_mem *

如果 struct device *hwdev 指向您的硬件 struct device(例如,如果 hwdev 指向 struct platform_devicedev 成员),那么这片段说明了如何访问保留内存区域的设备树节点并将其转换为 struct resource.

        struct device_node *memnp;
        struct resource mem_res;
        int rc;

        /* Get pointer to memory region device node from "memory-region" phandle. */
        memnp = of_parse_phandle(hwdev->of_node, "memory-region", 0);
        if (!memnp) {
                dev_err(hwdev, "no memory-region node\n");
                rc = -ENXIO;
                goto err1;
        }
        /* Convert memory region to a struct resource */
        rc = of_address_to_resource(memnp, 0, &mem_res);
        /* finished with memnp */
        of_node_put(memnp);
        if (rc) {
                dev_err(hwdev, "failed to translate memory-region to a resource\n");
                goto err1;
        }

起始地址以mem_res.start结束,长度由resource_size(&mem_res);给出。