FdtUpdateLib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /** @file
  2. *
  3. * Copyright (c) 2016, Hisilicon Limited. All rights reserved.
  4. * Copyright (c) 2016, Linaro Limited. All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause-Patent
  7. *
  8. **/
  9. #include <Uefi.h>
  10. #include <Library/ArmArchTimer.h>
  11. #include <Library/BaseLib.h>
  12. #include <libfdt.h>
  13. #include <Library/IoLib.h>
  14. #include <Library/DebugLib.h>
  15. #include <Library/UefiBootServicesTableLib.h>
  16. #include <Library/FdtUpdateLib.h>
  17. #include <PlatformArch.h>
  18. #include <Library/PcdLib.h>
  19. #include <Library/PlatformSysCtrlLib.h>
  20. #include <Library/MemoryAllocationLib.h>
  21. #include <Protocol/HisiBoardNicProtocol.h>
  22. #include <Library/UefiRuntimeServicesTableLib.h>
  23. #include <Library/OemMiscLib.h>
  24. typedef union AA_DAW
  25. {
  26. /* Define the struct bits */
  27. struct
  28. {
  29. unsigned int sysdaw_id : 7 ; /* [6:0] */
  30. unsigned int interleave_en : 1 ; /* [7] */
  31. unsigned int sysdaw_size : 4 ; /* [11:8] */
  32. unsigned int reserved : 4 ; /* [15:12] */
  33. unsigned int sysdaw_addr : 16 ; /* [31:16] */
  34. } bits;
  35. /* Define an unsigned member */
  36. unsigned int u32;
  37. } AA_DAW_U;
  38. MAC_ADDRESS gMacAddress[1];
  39. CHAR8 *EthName[8]=
  40. {
  41. "ethernet@0","ethernet@1",
  42. "ethernet@2","ethernet@3",
  43. "ethernet@4","ethernet@5",
  44. "ethernet@6","ethernet@7"
  45. };
  46. UINT8 DawNum[4] = {0, 0, 0, 0};
  47. PHY_MEM_REGION *NodemRegion[4] = {NULL, NULL, NULL, NULL};
  48. UINTN NumaPages[4] = {0, 0, 0, 0};
  49. CHAR8 *NumaNodeName[4]=
  50. {
  51. "p0-ta","p0-tc",
  52. "p1-ta","p1-tc",
  53. };
  54. STATIC
  55. BOOLEAN
  56. IsMemMapRegion (
  57. IN EFI_MEMORY_TYPE MemoryType
  58. )
  59. {
  60. switch(MemoryType)
  61. {
  62. case EfiRuntimeServicesCode:
  63. case EfiRuntimeServicesData:
  64. case EfiConventionalMemory:
  65. case EfiACPIReclaimMemory:
  66. case EfiACPIMemoryNVS:
  67. case EfiLoaderCode:
  68. case EfiLoaderData:
  69. case EfiBootServicesCode:
  70. case EfiBootServicesData:
  71. case EfiPalCode:
  72. return TRUE;
  73. default:
  74. return FALSE;
  75. }
  76. }
  77. EFI_STATUS
  78. GetMacAddress (UINT32 Port)
  79. {
  80. EFI_MAC_ADDRESS Mac;
  81. EFI_STATUS Status;
  82. HISI_BOARD_NIC_PROTOCOL *OemNic = NULL;
  83. Status = gBS->LocateProtocol(&gHisiBoardNicProtocolGuid, NULL, (VOID **)&OemNic);
  84. if(EFI_ERROR(Status))
  85. {
  86. DEBUG((EFI_D_ERROR, "[%a]:[%dL] LocateProtocol failed %r\n", __FUNCTION__, __LINE__, Status));
  87. return Status;
  88. }
  89. Status = OemNic->GetMac(&Mac, Port);
  90. if(EFI_ERROR(Status))
  91. {
  92. DEBUG((EFI_D_ERROR, "[%a]:[%dL] GetMac failed %r\n", __FUNCTION__, __LINE__, Status));
  93. return Status;
  94. }
  95. gMacAddress[0].data0=Mac.Addr[0];
  96. gMacAddress[0].data1=Mac.Addr[1];
  97. gMacAddress[0].data2=Mac.Addr[2];
  98. gMacAddress[0].data3=Mac.Addr[3];
  99. gMacAddress[0].data4=Mac.Addr[4];
  100. gMacAddress[0].data5=Mac.Addr[5];
  101. DEBUG((EFI_D_INFO, "Port%d:0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  102. Port,gMacAddress[0].data0,gMacAddress[0].data1,gMacAddress[0].data2,
  103. gMacAddress[0].data3,gMacAddress[0].data4,gMacAddress[0].data5));
  104. return EFI_SUCCESS;
  105. }
  106. STATIC
  107. EFI_STATUS
  108. DelPhyhandleUpdateMacAddress(IN VOID* Fdt)
  109. {
  110. UINT8 port;
  111. INTN ethernetnode;
  112. INTN node;
  113. INTN Error;
  114. struct fdt_property *m_prop;
  115. int m_oldlen;
  116. EFI_STATUS Status = EFI_SUCCESS;
  117. EFI_STATUS GetMacStatus = EFI_SUCCESS;
  118. node = fdt_subnode_offset(Fdt, 0, "soc");
  119. if (node < 0)
  120. {
  121. DEBUG ((EFI_D_ERROR, "can not find soc root node\n"));
  122. return EFI_INVALID_PARAMETER;
  123. }
  124. else
  125. {
  126. for( port=0; port<8; port++ )
  127. {
  128. GetMacStatus= GetMacAddress(port);
  129. ethernetnode = fdt_subnode_offset(Fdt, node,EthName[port]);
  130. if(!EFI_ERROR(GetMacStatus))
  131. {
  132. if (ethernetnode < 0)
  133. {
  134. DEBUG ((EFI_D_WARN, "Can not find ethernet@%d node\n",port));
  135. DEBUG ((EFI_D_WARN, "Suppose port %d is not enabled.\n", port));
  136. continue;
  137. }
  138. m_prop = fdt_get_property_w(Fdt, ethernetnode, "local-mac-address", &m_oldlen);
  139. if(m_prop)
  140. {
  141. Error = fdt_delprop(Fdt, ethernetnode, "local-mac-address");
  142. if (Error)
  143. {
  144. DEBUG ((EFI_D_ERROR, "ERROR:fdt_delprop() Local-mac-address: %a\n", fdt_strerror (Error)));
  145. Status = EFI_INVALID_PARAMETER;
  146. }
  147. Error = fdt_setprop(Fdt, ethernetnode, "local-mac-address",gMacAddress,sizeof(MAC_ADDRESS));
  148. if (Error)
  149. {
  150. DEBUG ((EFI_D_ERROR, "ERROR:fdt_setprop():local-mac-address %a\n", fdt_strerror (Error)));
  151. Status = EFI_INVALID_PARAMETER;
  152. }
  153. }
  154. }
  155. }
  156. }
  157. return Status;
  158. }
  159. STATIC
  160. EFI_STATUS
  161. UpdateRefClk (IN VOID* Fdt)
  162. {
  163. INTN node;
  164. INTN Error;
  165. struct fdt_property *m_prop;
  166. int m_oldlen;
  167. UINTN ArchTimerFreq = 0;
  168. UINT32 Data;
  169. CONST CHAR8 *Property = "clock-frequency";
  170. ArmArchTimerReadReg (CntFrq, &ArchTimerFreq);
  171. if (!ArchTimerFreq) {
  172. DEBUG ((DEBUG_ERROR, "[%a]:[%dL] Get timer frequency failed!\n", __FUNCTION__, __LINE__));
  173. return EFI_INVALID_PARAMETER;
  174. }
  175. node = fdt_subnode_offset(Fdt, 0, "soc");
  176. if (node < 0) {
  177. DEBUG ((DEBUG_ERROR, "can not find soc node\n"));
  178. return EFI_INVALID_PARAMETER;
  179. }
  180. node = fdt_subnode_offset(Fdt, node, "refclk");
  181. if (node < 0) {
  182. DEBUG ((DEBUG_ERROR, "can not find refclk node\n"));
  183. return EFI_INVALID_PARAMETER;
  184. }
  185. m_prop = fdt_get_property_w(Fdt, node, Property, &m_oldlen);
  186. if(!m_prop) {
  187. DEBUG ((DEBUG_ERROR, "[%a]:[%dL] Can't find property %a\n", __FUNCTION__, __LINE__, Property));
  188. return EFI_INVALID_PARAMETER;
  189. }
  190. Error = fdt_delprop(Fdt, node, Property);
  191. if (Error) {
  192. DEBUG ((DEBUG_ERROR, "ERROR: fdt_delprop() %a: %a\n", Property, fdt_strerror (Error)));
  193. return EFI_INVALID_PARAMETER;
  194. }
  195. // UINT32 is enough for refclk data length
  196. Data = (UINT32) ArchTimerFreq;
  197. Data = cpu_to_fdt32 (Data);
  198. Error = fdt_setprop(Fdt, node, Property, &Data, sizeof(Data));
  199. if (Error) {
  200. DEBUG ((DEBUG_ERROR, "ERROR:fdt_setprop() %a: %a\n", Property, fdt_strerror (Error)));
  201. return EFI_INVALID_PARAMETER;
  202. }
  203. DEBUG ((DEBUG_INFO, "Update refclk successfully.\n"));
  204. return EFI_SUCCESS;
  205. }
  206. INTN
  207. GetMemoryNode(VOID* Fdt)
  208. {
  209. INTN node;
  210. int m_oldlen;
  211. struct fdt_property *m_prop;
  212. INTN Error = 0;
  213. node = fdt_subnode_offset(Fdt, 0, "memory");
  214. if (node < 0)
  215. {
  216. // Create the memory node
  217. node = fdt_add_subnode(Fdt, 0, "memory");
  218. if(node < 0)
  219. {
  220. DEBUG((EFI_D_ERROR, "[%a]:[%dL] fdt add subnode error\n", __FUNCTION__, __LINE__));
  221. return node;
  222. }
  223. }
  224. //find the memory node property
  225. m_prop = fdt_get_property_w(Fdt, node, "memory", &m_oldlen);
  226. if(m_prop)
  227. {
  228. Error = fdt_delprop(Fdt, node, "reg");
  229. if (Error)
  230. {
  231. DEBUG ((EFI_D_ERROR, "ERROR:fdt_delprop(): %a\n", fdt_strerror (Error)));
  232. node = -1;
  233. return node;
  234. }
  235. }
  236. return node;
  237. }
  238. EFI_STATUS UpdateMemoryNode(VOID* Fdt)
  239. {
  240. INTN Error = 0;
  241. EFI_STATUS Status = EFI_SUCCESS;
  242. UINT32 Index = 0;
  243. UINT32 MemIndex;
  244. INTN node;
  245. EFI_MEMORY_DESCRIPTOR *MemoryMap;
  246. EFI_MEMORY_DESCRIPTOR *MemoryMapPtr;
  247. EFI_MEMORY_DESCRIPTOR *MemoryMapPtrCurrent;
  248. UINTN MemoryMapSize;
  249. UINTN Pages0 = 0;
  250. UINTN Pages1 = 0;
  251. UINTN MapKey;
  252. UINTN DescriptorSize;
  253. UINT32 DescriptorVersion;
  254. PHY_MEM_REGION *mRegion;
  255. UINTN MemoryMapLastEndAddress ;
  256. UINTN MemoryMapcontinuousStartAddress ;
  257. UINTN MemoryMapCurrentStartAddress;
  258. BOOLEAN FindMemoryRegionFlag = FALSE;
  259. node = GetMemoryNode(Fdt);
  260. if (node < 0)
  261. {
  262. DEBUG((EFI_D_ERROR, "Can not find memory node\n"));
  263. return EFI_NOT_FOUND;
  264. }
  265. MemoryMap = NULL;
  266. MemoryMapSize = 0;
  267. MemIndex = 0;
  268. Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);
  269. if (Status == EFI_BUFFER_TOO_SMALL)
  270. {
  271. // The UEFI specification advises to allocate more memory for the MemoryMap buffer between successive
  272. // calls to GetMemoryMap(), since allocation of the new buffer may potentially increase memory map size.
  273. Pages0 = EFI_SIZE_TO_PAGES (MemoryMapSize) + 1;
  274. MemoryMap = AllocatePages (Pages0);
  275. if (MemoryMap == NULL)
  276. {
  277. Status = EFI_OUT_OF_RESOURCES;
  278. return Status;
  279. }
  280. Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);
  281. if (EFI_ERROR(Status))
  282. {
  283. DEBUG ((EFI_D_ERROR, "FdtUpdateLib GetMemoryMap Error\n"));
  284. FreePages (MemoryMap, Pages0);
  285. return Status;
  286. }
  287. }
  288. else
  289. {
  290. DEBUG ((EFI_D_ERROR, "FdtUpdateLib GetmemoryMap Status: %r\n",Status));
  291. return EFI_ABORTED;
  292. }
  293. mRegion = NULL;
  294. Pages1 = EFI_SIZE_TO_PAGES (sizeof(PHY_MEM_REGION) *( MemoryMapSize / DescriptorSize));
  295. mRegion = (PHY_MEM_REGION*)AllocatePool(Pages1);
  296. if (mRegion == NULL)
  297. {
  298. Status = EFI_OUT_OF_RESOURCES;
  299. FreePages (MemoryMap, Pages0);
  300. return Status;
  301. }
  302. MemoryMapPtr = MemoryMap;
  303. MemoryMapPtrCurrent = MemoryMapPtr;
  304. MemoryMapLastEndAddress = 0;
  305. MemoryMapcontinuousStartAddress = 0;
  306. MemoryMapCurrentStartAddress = 0;
  307. for (Index = 0; Index < (MemoryMapSize / DescriptorSize); Index++)
  308. {
  309. MemoryMapPtrCurrent = (EFI_MEMORY_DESCRIPTOR*)((UINTN)MemoryMapPtr + Index*DescriptorSize);
  310. MemoryMapCurrentStartAddress = (UINTN)MemoryMapPtrCurrent->PhysicalStart;
  311. if (!IsMemMapRegion ((EFI_MEMORY_TYPE)MemoryMapPtrCurrent->Type))
  312. {
  313. continue;
  314. }
  315. else
  316. {
  317. FindMemoryRegionFlag = TRUE;
  318. if(MemoryMapCurrentStartAddress != MemoryMapLastEndAddress)
  319. {
  320. mRegion[MemIndex].BaseHigh= cpu_to_fdt32(MemoryMapcontinuousStartAddress>>32);
  321. mRegion[MemIndex].BaseLow=cpu_to_fdt32(MemoryMapcontinuousStartAddress);
  322. mRegion[MemIndex].LengthHigh= cpu_to_fdt32((MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress)>>32);
  323. mRegion[MemIndex].LengthLow=cpu_to_fdt32(MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress);
  324. MemIndex+=1;
  325. MemoryMapcontinuousStartAddress=MemoryMapCurrentStartAddress;
  326. }
  327. }
  328. MemoryMapLastEndAddress = (UINTN)(MemoryMapPtrCurrent->PhysicalStart + MemoryMapPtrCurrent->NumberOfPages * EFI_PAGE_SIZE);
  329. }
  330. if (FindMemoryRegionFlag)
  331. {
  332. mRegion[MemIndex].BaseHigh = cpu_to_fdt32(MemoryMapcontinuousStartAddress>>32);
  333. mRegion[MemIndex].BaseLow = cpu_to_fdt32(MemoryMapcontinuousStartAddress);
  334. mRegion[MemIndex].LengthHigh = cpu_to_fdt32((MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress)>>32);
  335. mRegion[MemIndex].LengthLow = cpu_to_fdt32(MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress);
  336. }
  337. Error = fdt_setprop(Fdt, node, "reg",mRegion,sizeof(PHY_MEM_REGION) *(MemIndex+1));
  338. FreePool (mRegion);
  339. FreePages (MemoryMap, Pages0);
  340. if (Error)
  341. {
  342. DEBUG ((EFI_D_ERROR, "ERROR:fdt_setprop(): %a\n", fdt_strerror (Error)));
  343. Status = EFI_INVALID_PARAMETER;
  344. return Status;
  345. }
  346. return Status;
  347. }
  348. EFI_STATUS
  349. UpdateNumaNode(VOID* Fdt)
  350. {
  351. //TODO: Need to update numa node
  352. return EFI_SUCCESS;
  353. }
  354. /*
  355. * Entry point for fdtupdate lib.
  356. */
  357. EFI_STATUS EFIFdtUpdate(UINTN FdtFileAddr)
  358. {
  359. INTN Error;
  360. VOID* Fdt;
  361. UINT32 Size;
  362. UINTN NewFdtBlobSize;
  363. UINTN NewFdtBlobBase;
  364. EFI_STATUS Status = EFI_SUCCESS;
  365. EFI_STATUS UpdateNumaStatus = EFI_SUCCESS;
  366. Error = fdt_check_header ((VOID*)(FdtFileAddr));
  367. if (0 != Error)
  368. {
  369. DEBUG ((EFI_D_ERROR,"ERROR: Device Tree header not valid (%a)\n", fdt_strerror(Error)));
  370. return EFI_INVALID_PARAMETER;
  371. }
  372. Size = (UINTN)fdt_totalsize ((VOID*)(UINTN)(FdtFileAddr));
  373. NewFdtBlobSize = Size + ADD_FILE_LENGTH;
  374. Fdt = (VOID*)(UINTN)FdtFileAddr;
  375. Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, EFI_SIZE_TO_PAGES(NewFdtBlobSize), &NewFdtBlobBase);
  376. if (EFI_ERROR (Status))
  377. {
  378. return EFI_OUT_OF_RESOURCES;
  379. }
  380. Error = fdt_open_into(Fdt,(VOID*)(UINTN)(NewFdtBlobBase), (NewFdtBlobSize));
  381. if (Error) {
  382. DEBUG ((EFI_D_ERROR, "ERROR:fdt_open_into(): %a\n", fdt_strerror (Error)));
  383. Status = EFI_INVALID_PARAMETER;
  384. goto EXIT;
  385. }
  386. Fdt = (VOID*)(UINTN)NewFdtBlobBase;
  387. Status = DelPhyhandleUpdateMacAddress(Fdt);
  388. if (EFI_ERROR (Status))
  389. {
  390. DEBUG ((EFI_D_ERROR, "DelPhyhandleUpdateMacAddress fail:\n"));
  391. Status = EFI_SUCCESS;
  392. }
  393. Status = UpdateRefClk (Fdt);
  394. if (EFI_ERROR (Status)) {
  395. DEBUG ((DEBUG_ERROR, "UpdateiRefClk fail.\n"));
  396. }
  397. Status = UpdateMemoryNode(Fdt);
  398. if (EFI_ERROR (Status))
  399. {
  400. DEBUG ((EFI_D_ERROR, "UpdateMemoryNode Error\n"));
  401. goto EXIT;
  402. }
  403. UpdateNumaStatus = UpdateNumaNode(Fdt);
  404. if (EFI_ERROR (UpdateNumaStatus))
  405. {
  406. DEBUG ((EFI_D_ERROR, "Update NumaNode fail\n"));
  407. }
  408. gBS->CopyMem(((VOID*)(UINTN)(FdtFileAddr)),((VOID*)(UINTN)(NewFdtBlobBase)),NewFdtBlobSize);
  409. EXIT:
  410. gBS->FreePages(NewFdtBlobBase,EFI_SIZE_TO_PAGES(NewFdtBlobSize));
  411. return Status;
  412. }