[platform]新旧内核的device设备注册对比
轉(zhuǎn)自:http://blog.chinaunix.net/uid-7332782-id-3268801.html
?
1. Version2.6內(nèi)核啟動(dòng)過程
????start_kernel(?) //板子上電啟動(dòng)后進(jìn)入start_kernel( ),相當(dāng)于程序的main入口
????????-->setup_arch(&command_line) ?//command_line由內(nèi)核傳入
????????????-->mdesc?=?setup_machine(machine_arch_type);
????????????????-->list?=?lookup_machine_type(nr);?//匯編實(shí)現(xiàn)查找機(jī)器碼所定義的平臺(tái),找到后返回mdesc結(jié)構(gòu)? ? ? ? ? ? ? ?
????????????-->init_machine?=?mdesc->init_machine; ?//struct machine_desc?*mdesc;machine_desc結(jié)構(gòu)很重要,
????????-->rest_init()
????????????-->kernel_thread(kernel_init,?NULL,?CLONE_FS?|?CLONE_SIGHAND);?//定義進(jìn)程kernel_init,pid=1,在kthreadd進(jìn)程創(chuàng)建好后調(diào)度運(yùn)行? ? ? ? ??
????????????????-->kernel_init()
????????????????????-->do_basic_setup()
????????????????????????-->driver_init()
????????????????????????????-->devices_init()
????????????????????????????-->buses_init()
????????????????????????????-->classes_init()
????????????????????????????-->platform_bus_init()
????????????????????????-->do_initcalls() ?//此函數(shù)很重要,執(zhí)行了initcall表中所有的函數(shù),包含了init_machine(saar_init())函數(shù)? ? ? ? ? ? ? ? ? ? ? ?
????????????????????????????-->saar_init()
????????????????????-->init_post() ???//調(diào)度用戶空間程序,比如bash,在用戶空間死循環(huán)執(zhí)行程序?? ? ? ? ? ? ? ? ??
????????????-->pid?=?kernel_thread(kthreadd,?NULL,?CLONE_FS?|?CLONE_FILES); ?//定義進(jìn)程kthreadd
2. Version3.x內(nèi)核啟動(dòng)過程和2.6版本在init_machine加載設(shè)備資源的差異對(duì)比
- 在2.6內(nèi)核中, 在setup_arch()中,舉個(gè)例子imx5,
-->init_machine=mdesc->init_machine; //init/main.c->start_kernel()->setup_arch(), 將init_machine指向mdesc結(jié)構(gòu)體重的init_machine指針,而mdesc中該指針指向具體芯片對(duì)應(yīng)的設(shè)備資源函數(shù)mxc_board_init.
? -->.init_machine=mxc_board_init; //arch/arm/mach-mx5/mx50_arm2.c->MACHINE_START()?,宏即時(shí)初始化machine_desc結(jié)構(gòu)體 ? ? ? ?
? ? -->mxc_register_device(&mxc_dma_device); //arch/arm/mach-mx5/mx50_arm2.c->mxc_board_init(), 在mxc_board_init完成這些設(shè)備注冊(cè)
? ? -->mxc_register_device(&mxc_wdt_device); //mxc_wdt_device這些設(shè)備資源也都申明在mach-mx50下面
? ? -->mxc_register_device(&mxci2c_devices[0]);
? ? -->..........
當(dāng)然在setup_arch()中對(duì)init_machine進(jìn)行初始化,這個(gè)時(shí)候并沒有調(diào)用init_machine函數(shù),init_machine是在代碼中被定義為arch_initcall屬性(arch/arm/kernel/setup.c), 然后在do_initcalls()中進(jìn)行遍歷調(diào)用,具體見上述的啟動(dòng)過程。
由上述可以看出,在系統(tǒng)啟動(dòng)時(shí),device設(shè)備就已經(jīng)register到總線上了,而3.x以后已經(jīng)不在mach-**中申明設(shè)備資源了,那啟動(dòng)流程如何呢,見下一節(jié)。
- ?在3.x內(nèi)核中,在setup_arch()是這么處理的, 首先解析dtb
-->setup_arch() //init/main.c->start_kernel()->setup_arch()
? -->mdesc=setup_machine_fdt();
? -->unflatten_device_tree() //和2.6內(nèi)核不同,在setup_arch()中并沒有init_machine = mdesc->init_machine.那init_machine如何執(zhí)行呢?
? ? ?-->__unflatten_device_tree()
? ? ? ? -->unflatten_dt_node() //到此基本完成dts中node到鏈表的操作
節(jié)點(diǎn)中的設(shè)備注冊(cè)
-->DT_MACHINE_START //machine_desc結(jié)構(gòu)體賦值,在2.6內(nèi)核中宏偉MACHINE_START,文件位置:arch/arm/mach-***
? -->.init_machine=imx6sx_init_machine
? ? ?-->of_platform_populate();? //imx6sx_init_machine()調(diào)用,在setup_arch()中解析設(shè)備數(shù),構(gòu)造設(shè)備節(jié)點(diǎn)鏈表,然后在這里進(jìn)行設(shè)備的注冊(cè),而init_machine為arch_initcall屬性,當(dāng)在setup_arch()后面代碼調(diào)用到do_initcalls()函數(shù)時(shí)調(diào)用init_machine()函數(shù)完成設(shè)備注冊(cè)。
? ? ? ? -->of_platform_bus_create();? //由for_each_child_of_node()調(diào)用,遍歷device tree中每個(gè)節(jié)點(diǎn)
? ? ? ? ? ?-->of_platform_device_create_pdata()
? ? ? ? ? ? ? -->of_device_alloc() //為每個(gè)device申請(qǐng)空間
? ? ? ? ? ? ? -->platform_device_put()
? ? ? ? ? ?-->of_platform_bus_create()
到此完成設(shè)備的注冊(cè)。
在3.x的setup.c中關(guān)于init_machine的調(diào)用是這么定義的
1 static int __init customize_machine(void) 2 { 3 /* 4 * customizes platform devices, or adds new ones 5 * On DT based machines, we fall back to populating the 6 * machine from the device tree, if no callback is provided, 7 * otherwise we would always need an init_machine callback. 8 */ 9 if (machine_desc->init_machine) 10 machine_desc->init_machine(); 11 #ifdef CONFIG_OF 12 else 13 of_platform_populate(NULL, of_default_bus_match_table, 14 NULL, NULL); 15 #endif 16 return 0; 17 }?
關(guān)于init_machine到底會(huì)不會(huì)被執(zhí)行,在Documentation/Devicetree/usage-model.txt中有這么一段話
The most interesting hook in the DT context is .init_machine() which is primarily responsible for populating the Linux device model with data about the platform. Historically this has been implemented on embedded platforms by defining a set of static clock structures, platform_devices, and other data in the board support .c file, and registering it en-masse in .init_machine(). When DT is used, then instead of hard coding static devices for each platform, the list of devices can be obtained by parsing the DT, and allocating device structures dynamically.The simplest case is when .init_machine() is only responsible for registering a block of platform_devices. A platform_device is a concept used by Linux for memory or I/O mapped devices which cannot be detected by hardware, and for 'composite' or 'virtual' devices (more on those later). While there is no 'platform device' terminology for the DT, platform devices roughly correspond to device nodes at the root of the tree and children of simple memory mapped bus nodes.?
轉(zhuǎn)載于:https://www.cnblogs.com/aaronLinux/p/5559721.html
總結(jié)
以上是生活随笔為你收集整理的[platform]新旧内核的device设备注册对比的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原创:嘉庆死后,皇后为何不选亲儿子继位,
- 下一篇: 2016年度最受欢迎的100个 Java