dts展开为platform_device结构过程分析

dts展开为platform_device结构过程分析dts节点展开为platform_device结构过程分析1.概述本文主要是记录学习Linux解析dts的代码分析,以便进行后续回顾。平台:ARMVexpress内核版本:linux-4.92.dts节点展开为platform_device结构过程分析自从ARM引入的dts之后,bsp驱动代码产生了非常之大的变化,像在linux-2.6.32这些版本的platform驱动中,会存在大…

大家好,又见面了,我是你们的朋友全栈君。

dts节点展开为platform_device结构过程分析

1.概述

本文主要是记录学习Linux解析dts的代码分析,以便进行后续回顾。

平台:ARM Vexpress

内核版本:linux-4.9

2.dts节点展开为platform_device结构过程分析

自从ARM引入的dts之后,bsp驱动代码产生了非常之大的变化,像在linux-2.6.32这些版本的platform驱动中,会存在大量类似一下的代码:

	static struct resource xxx_resources[] = { 
   
	        [0] = { 
   
	                .start  =,
	                .end    =,
	                .flags  = IORESOURCE_MEM,
	        },
	        [1] = { 
   
	                .start  =,
	                .end    =,
	                .flags  = IORESOURCE_IRQ,
	         },
	 };

	 static struct platform_device xxx_device = { 
   
	         .name           = "xxx",
	         .id             = -1,
	         .dev            = { 
   
	                                 .platform_data          = &xxx_data,
	         },
	         .resource       = xxx_resources,
	         .num_resources  = ARRAY_SIZE(xxx_resources),
	};

但是通过dts,像上述的代码不再需要我们程序员进行手动配置,只需在dts相应的节点通过reg、interrupt等属性的配置,就可以通过内核提供的解析dts的接口把dts中的节点信息展开为platform_device,然后把reg、interrupt等属性的信息填充到struct resource资源结构体中,有效的减少了我们驱动代码的冗余,做到配置简单,易读。以下就是通过分析代码,了解linux是如何把dts节点信息展开为struct platform_device结构体的过程。

将dts节点展开为struct platform_device结构体的过程主要是交给of_platform_populate()函数完成,通过对该函数使用dump_stack()回溯其调用过程可以得到以下log:

	futex hash table entries: 1024 (order: 4, 65536 bytes)
	NET: Registered protocol family 16
	DMA: preallocated 256 KiB pool for atomic coherent allocations
	CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.9.56+ #7
	Hardware name: ARM-Versatile Express
	[<80111158>] (unwind_backtrace) from [<8010ca80>] (show_stack+0x20/0x24)
	[<8010ca80>] (show_stack) from [<803dd1b4>] (dump_stack+0xac/0xd8)
	[<803dd1b4>] (dump_stack) from [<80586318>] (of_platform_populate+0x30/0xbc)
	[<80586318>] (of_platform_populate) from [<80928410>] (vexpress_config_init+0xb8/0xec)
	[<80928410>] (vexpress_config_init) from [<80101c9c>] (do_one_initcall+0x54/0x184)
	[<80101c9c>] (do_one_initcall) from [<80900f48>] (kernel_init_freeable+0x214/0x2a8)
	[<80900f48>] (kernel_init_freeable) from [<806a4a94>] (kernel_init+0x18/0x124)
	[<806a4a94>] (kernel_init) from [<80108190>] (ret_from_fork+0x14/0x24)
	CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.9.56+ #7
	Hardware name: ARM-Versatile Express
	[<80111158>] (unwind_backtrace) from [<8010ca80>] (show_stack+0x20/0x24)
	[<8010ca80>] (show_stack) from [<803dd1b4>] (dump_stack+0xac/0xd8)
	[<803dd1b4>] (dump_stack) from [<80586318>] (of_platform_populate+0x30/0xbc)
	[<80586318>] (of_platform_populate) from [<80928410>] (vexpress_config_init+0xb8/0xec)
	[<80928410>] (vexpress_config_init) from [<80101c9c>] (do_one_initcall+0x54/0x184)
	[<80101c9c>] (do_one_initcall) from [<80900f48>] (kernel_init_freeable+0x214/0x2a8)
	[<80900f48>] (kernel_init_freeable) from [<806a4a94>] (kernel_init+0x18/0x124)
	[<806a4a94>] (kernel_init) from [<80108190>] (ret_from_fork+0x14/0x24)
	cpuidle: using governor ladder

通过栈回溯信息,我们可以知道of_platform_populate()函数的最早调用入口是vexpress_config_init()函数,除此之外,在of_platform_default_populate()函数中也会调用该函数,分别如下所示

vexpress_config_init()函数是定义在drivers/bus/vexpress-config.c文件中,该函数实现如下:

	static int __init vexpress_config_init(void)
	{ 
   
		int err = 0;
		struct device_node *node;

		/* Need the config devices early, before the "normal" devices... */
		for_each_compatible_node(node, NULL, "arm,vexpress,config-bus") { 
   
			err = vexpress_config_populate(node);
			if (err) { 
   
				of_node_put(node);
				break;
			}
		}

		return err;
	}
	postcore_initcall(vexpress_config_init);

  //另外一个函数入口
  int of_platform_default_populate(struct device_node *root,
  				 const struct of_dev_auxdata *lookup,
  				 struct device *parent)
  { 
   
  	return of_platform_populate(root, of_default_bus_match_table, lookup,
  				    parent);
  }
  EXPORT_SYMBOL_GPL(of_platform_default_populate);

  #ifndef CONFIG_PPC
  static int __init of_platform_default_populate_init(void)
  { 
   
  	struct device_node *node;

  	if (!of_have_populated_dt())
  		return -ENODEV;

  	/* * Handle ramoops explicitly, since it is inside /reserved-memory, * which lacks a "compatible" property. */
  	node = of_find_node_by_path("/reserved-memory");
  	if (node) { 
   
  		node = of_find_compatible_node(node, NULL, "ramoops");
  		if (node)
  			of_platform_device_create(node, NULL, NULL);
  	}

  	/* Populate everything else. */
  	of_platform_default_populate(NULL, NULL, NULL);

  	return 0;
  }
  arch_initcall_sync(of_platform_default_populate_init);
  #endif

首先,通过postcore_initcall和arch_initcall_sync这些宏声明该platform driver的调用等级,当内核执行到以下调用时,会调用到该函数:

	start_kernel(void)
		->rest_init()
			->kernel_init()
				->kernel_init_freeable()
					->do_basic_setup()
						->do_initcalls()
							->do_initcall_level()
								->do_one_initcall()

在do_initcalls()函数中,会从0遍历到ARRAY_SIZE(initcall_levels) – 1,代码如下:

	static void __init do_initcalls(void)
	{ 
   
		int level;

		for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
			do_initcall_level(level);
	}

其中initcall_levels是一个指针数组,里面存放着各个level下的函数指针的首地址,该数组定义在init/main.c中,如下所示:

	static initcall_t *initcall_levels[] __initdata = { 
   
		__initcall0_start,
		__initcall1_start,
		__initcall2_start,
		__initcall3_start,
		__initcall4_start,
		__initcall5_start,
		__initcall6_start,
		__initcall7_start,
		__initcall_end,
	};

因此在do_initcalls()函数中,会从level=0遍历到level=8,每个driver的调用顺序可以通过以下宏来声明:

	#define pure_initcall(fn) __define_initcall(fn, 0)
	#define core_initcall(fn) __define_initcall(fn, 1)
	#define core_initcall_sync(fn) __define_initcall(fn, 1s)
	#define postcore_initcall(fn) __define_initcall(fn, 2)
	#define postcore_initcall_sync(fn) __define_initcall(fn, 2s)
	#define arch_initcall(fn) __define_initcall(fn, 3)
	#define arch_initcall_sync(fn) __define_initcall(fn, 3s)
	#define subsys_initcall(fn) __define_initcall(fn, 4)
	#define subsys_initcall_sync(fn) __define_initcall(fn, 4s)
	#define fs_initcall(fn) __define_initcall(fn, 5)
	#define fs_initcall_sync(fn) __define_initcall(fn, 5s)
	#define rootfs_initcall(fn) __define_initcall(fn, rootfs)
	#define device_initcall(fn) __define_initcall(fn, 6)
	#define device_initcall_sync(fn) __define_initcall(fn, 6s)
	#define late_initcall(fn) __define_initcall(fn, 7)
	#define late_initcall_sync(fn) __define_initcall(fn, 7s)

	#define __initcall(fn) device_initcall(fn)

以上这些宏都定义在include/linux/init.h文件中。从这里可以看出postcore_initcall宏声明的driver初始化函数的调用level为2。

了解到如何调用到of_platform_default_populate()函数的过程,我们再来看看of_platform_populate()函数如何把一个dts节点展开struct platform_device结构的,分析如下:

/** * of_platform_populate() - Populate platform_devices from device tree data * @root: parent of the first level to probe or NULL for the root of the tree * @matches: match table, NULL to use the default * @lookup: auxdata table for matching id and platform_data with device nodes * @parent: parent to hook devices from, NULL for toplevel * * Similar to of_platform_bus_probe(), this function walks the device tree * and creates devices from nodes. It differs in that it follows the modern * convention of requiring all device nodes to have a 'compatible' property, * and it is suitable for creating devices which are children of the root * node (of_platform_bus_probe will only create children of the root which * are selected by the @matches argument). * * New board support should be using this function instead of * of_platform_bus_probe(). * * Returns 0 on success, < 0 on failure. */
int of_platform_populate(struct device_node *root,
			const struct of_device_id *matches,
			const struct of_dev_auxdata *lookup,
			struct device *parent)
{ 
   
	struct device_node *child;
	int rc = 0;

	root = root ? of_node_get(root) : of_find_node_by_path("/");
	if (!root)
		return -EINVAL;

	pr_debug("%s()\n", __func__);
	pr_debug(" starting at: %s\n", root->full_name);

	for_each_child_of_node(root, child) { 
   
    /* 1.遍历root节点下的所有子节点,调用of_platform_bus_create() */
		rc = of_platform_bus_create(child, matches, lookup, parent, true);
		if (rc) { 
   
			of_node_put(child);
			break;
		}
	}
	of_node_set_flag(root, OF_POPULATED_BUS);

	of_node_put(root);
	return rc;
}
EXPORT_SYMBOL_GPL(of_platform_populate);

通常root节点都是一些特定的总线节点,如platform device的simple-bus,如下所示:

const struct of_device_id of_default_bus_match_table[] = { 
   
	{ 
    .compatible = "simple-bus", },
	{ 
    .compatible = "simple-mfd", },
	{ 
    .compatible = "isa", },
#ifdef CONFIG_ARM_AMBA
	{ 
    .compatible = "arm,amba-bus", },
#endif /* CONFIG_ARM_AMBA */
	{ 
   } /* Empty terminated list */
};

dts对应节点如下:

smb@04000000 { 
   
  compatible = "simple-bus";

  #address-cells = <2>;
  #size-cells = <1>;
  ranges = <0 0 0x40000000 0x04000000>,
     <1 0 0x44000000 0x04000000>,
     <2 0 0x48000000 0x04000000>,
     <3 0 0x4c000000 0x04000000>,
     <7 0 0x10000000 0x00020000>;

  ...
}

还有arm vexpress的config-bus,dts节点如下:

dcc { 
   
  compatible = "arm,vexpress,config-bus";
  arm,vexpress,config-bridge = <&v2m_sysreg>;

  oscclk0: extsaxiclk { 
   
    /* ACLK clock to the AXI master port on the test chip */
    compatible = "arm,vexpress-osc";
    arm,vexpress-sysreg,func = <1 0>;
    freq-range = <30000000 50000000>;
    #clock-cells = <0>;
    clock-output-names = "extsaxiclk";
  };
  ...
}

在of_platform_bus_create()函数中,会根据节点的compatible属性进行相应的设备创建,代码分析如下:

/** * of_platform_bus_create() - Create a device for a node and its children. * @bus: device node of the bus to instantiate * @matches: match table for bus nodes * @lookup: auxdata table for matching id and platform_data with device nodes * @parent: parent for new device, or NULL for top level. * @strict: require compatible property * * Creates a platform_device for the provided device_node, and optionally * recursively create devices for all the child nodes. */
static int of_platform_bus_create(struct device_node *bus,
				  const struct of_device_id *matches,
				  const struct of_dev_auxdata *lookup,
				  struct device *parent, bool strict)
{ 
   
	const struct of_dev_auxdata *auxdata;
	struct device_node *child;
	struct platform_device *dev;
	const char *bus_id = NULL;
	void *platform_data = NULL;
	int rc = 0;

	/* 1.检查节点是否具备compatible属性,没有,则返回0 */
	if (strict && (!of_get_property(bus, "compatible", NULL))) { 
   
		pr_debug("%s() - skipping %s, no compatible prop\n",
			 __func__, bus->full_name);
		return 0;
	}

  /* 2.检查节点是否已经解析过,是,则返回0 */
	if (of_node_check_flag(bus, OF_POPULATED_BUS)) { 
   
		pr_debug("%s() - skipping %s, already populated\n",
			__func__, bus->full_name);
		return 0;
	}

	auxdata = of_dev_lookup(lookup, bus);
	if (auxdata) { 
   
		bus_id = auxdata->name;
		platform_data = auxdata->platform_data;
	}

  /* 3.判断是否是amba device,如果是,则创建该类型设备 */
	if (of_device_is_compatible(bus, "arm,primecell")) { 
   
		/* * Don't return an error here to keep compatibility with older * device tree files. */
		of_amba_device_create(bus, bus_id, platform_data, parent);
		return 0;
	}

  /* 4.创建platform device */
	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
	if (!dev || !of_match_node(matches, bus))
		return 0;

  /* 5.如果该节点为总线节点,则遍历该节点下的所有子节点,并创建相应的设备 */
	for_each_child_of_node(bus, child) { 
   
		pr_debug(" create child: %s\n", child->full_name);
		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
		if (rc) { 
   
			of_node_put(child);
			break;
		}
	}
	of_node_set_flag(bus, OF_POPULATED_BUS);
	return rc;
}

创建设备的函数of_platform_device_create_pdata()分析如下:

/** * of_platform_device_create_pdata - Alloc, initialize and register an of_device * @np: pointer to node to create device for * @bus_id: name to assign device * @platform_data: pointer to populate platform_data pointer with * @parent: Linux device model parent device. * * Returns pointer to created platform device, or NULL if a device was not * registered. Unavailable devices will not get registered. */
static struct platform_device *of_platform_device_create_pdata(
					struct device_node *np,
					const char *bus_id,
					void *platform_data,
					struct device *parent)
{ 
   
	struct platform_device *dev;

  /* 1.检查节点status属性值是否为ok或okay,不等,则返回false, * 2.status属性不存在,返回true */
	if (!of_device_is_available(np) ||
	    of_node_test_and_set_flag(np, OF_POPULATED))
		return NULL;

  /* 2.分配struct platform_device结构体 * 3.解析dts mem/irq等资源信息,填充到设备结构中 */
	dev = of_device_alloc(np, bus_id, parent);
	if (!dev)
		goto err_clear_flag;

  /* 4.初始化设备bus类型,用于驱动probe */
	dev->dev.bus = &platform_bus_type;
	dev->dev.platform_data = platform_data;
	of_dma_configure(&dev->dev, dev->dev.of_node);
	of_msi_configure(&dev->dev, dev->dev.of_node);

  /* 5.添加到设备链表,至此,设备创建完成 */
	if (of_device_add(dev) != 0) { 
   
		of_dma_deconfigure(&dev->dev);
		platform_device_put(dev);
		goto err_clear_flag;
	}

	return dev;

err_clear_flag:
	of_node_clear_flag(np, OF_POPULATED);
	return NULL;
}

至此,设备创建完成,可以等待驱动的probe

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163821.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js 判断字符串中是否包含某个字符include的坑「建议收藏」

    js 判断字符串中是否包含某个字符include的坑「建议收藏」方法一indexOf()(推荐)varstr=”123″;console.log(str.indexOf(“3″)!=-1);//trueindexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有出现,则该方法返回-1。方法二test()varstr=”123”;varreg=RegExp(/3/);console.log(reg.test(str));//truetest()方法用于检索字

    2022年10月6日
    0
  • C# WinForm界面美化插件简介

    C# WinForm界面美化插件简介 美化C#的WIN程序界面可以考虑用第三方控件,比如DotNetBar或DevExpress。但是它们都是收费的,虽然有破解版。。但是使用时还是需要权衡的。另外,用第三方控件会让运行速度下降。 1.      DevExpress收费软件 2.      Rad 3.      Irisskin2在项目中添加DLL文件,在程序中控制显示即可。使

    2022年5月8日
    119
  • mac在变化mysql-rootpassword-各种解决问题的能力

    mac在变化mysql-rootpassword-各种解决问题的能力

    2022年1月15日
    158
  • MATLAB分段函数及应用实例

    MATLAB分段函数及应用实例简单实例:用matlab画分段函数        当x&lt;0时 y=5*sin(x);        当x&gt;=0且x&lt;=5时y=x^2;        当x&gt;5时y=(8-x)^2+16.x=-5:0.001:10;y=5*sin(x).*(x&lt;0)+x.^2.*(x&gt;=0&amp;x&lt;=5)+((8-x).^2+16).*(x&gt;5);p…

    2022年6月7日
    42
  • object finalized_finalize()方法

    object finalized_finalize()方法一、一次标记首先finalize方法是在垃圾回收时,用于确认该对象是否确认被回收的一个标记过程。确认一个对象真正被回收需要经历两次标记过程:可达性分析没有引用,这是第一次标记是否有必要执行finalize方法,如果对象没有重写finalize方法或者finalize方法已经被调用过了,那么finalize方法就是没有必要执行的,没有必要执行finalize方法的对象就会被直接回收。如果对象被判定为有必要执行finalize()方法,那么这个对象将会放置在一个叫做F-Queue的队列之中,并在稍后

    2022年9月18日
    0
  • 走进webpack(3)– 小结「建议收藏」

    写这一系列的文章,本意是想要梳理一下自己凌乱的webpack知识,只是使用过vue-cli,修改过其中的一部分代码,但是对于一个简单项目从0开始搭建webpack的流程和其中的依赖并不是十分清楚。所以

    2022年3月25日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号