summaryrefslogtreecommitdiff
path: root/Platform/Hisilicon/D03/Library/FdtUpdateLib/FdtUpdateLib.c
blob: d00cb9b2abc1081a21db0e68e4b3f2dba3651489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/** @file
*
*  Copyright (c) 2016, Hisilicon Limited. All rights reserved.
*  Copyright (c) 2016, Linaro Limited. All rights reserved.
*
*  This program and the accompanying materials
*  are licensed and made available under the terms and conditions of the BSD License
*  which accompanies this distribution.  The full text of the license may be found at
*  http://opensource.org/licenses/bsd-license.php
*
*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*
**/

#include <Uefi.h>
#include <Library/ArmArchTimer.h>
#include <Library/BaseLib.h>
#include <libfdt.h>
#include <Library/IoLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/FdtUpdateLib.h>
#include <PlatformArch.h>
#include <Library/PcdLib.h>
#include <Library/PlatformSysCtrlLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Protocol/HisiBoardNicProtocol.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/OemMiscLib.h>

typedef union AA_DAW
{
    /* Define the struct bits */
    struct
    {
        unsigned int    sysdaw_id           : 7  ; /* [6:0] */
        unsigned int    interleave_en       : 1  ; /* [7] */
        unsigned int    sysdaw_size         : 4  ; /* [11:8] */
        unsigned int    reserved            : 4  ; /* [15:12] */
        unsigned int    sysdaw_addr         : 16 ; /* [31:16] */
    } bits;

    /* Define an unsigned member */
    unsigned int    u32;

} AA_DAW_U;



MAC_ADDRESS gMacAddress[1];


CHAR8  *EthName[8]=
{
 "ethernet@0","ethernet@1",
 "ethernet@2","ethernet@3",
 "ethernet@4","ethernet@5",
 "ethernet@6","ethernet@7"
};

UINT8 DawNum[4] = {0, 0, 0, 0};
PHY_MEM_REGION    *NodemRegion[4] = {NULL, NULL, NULL, NULL};
UINTN  NumaPages[4] = {0, 0, 0, 0};

CHAR8  *NumaNodeName[4]=
{
 "p0-ta","p0-tc",
 "p1-ta","p1-tc",
};

STATIC
BOOLEAN
IsMemMapRegion (
  IN EFI_MEMORY_TYPE MemoryType
  )
{
  switch(MemoryType)
    {
        case EfiRuntimeServicesCode:
        case EfiRuntimeServicesData:
        case EfiConventionalMemory:
        case EfiACPIReclaimMemory:
        case EfiACPIMemoryNVS:
        case EfiLoaderCode:
        case EfiLoaderData:
        case EfiBootServicesCode:
        case EfiBootServicesData:
        case EfiPalCode:
                return TRUE;
        default:
                return FALSE;
  }
}


EFI_STATUS
GetMacAddress (UINT32 Port)
{
    EFI_MAC_ADDRESS Mac;
    EFI_STATUS Status;
    HISI_BOARD_NIC_PROTOCOL *OemNic = NULL;

    Status = gBS->LocateProtocol(&gHisiBoardNicProtocolGuid, NULL, (VOID **)&OemNic);
    if(EFI_ERROR(Status))
    {
        DEBUG((EFI_D_ERROR, "[%a]:[%dL] LocateProtocol failed %r\n", __FUNCTION__, __LINE__, Status));
        return Status;
    }

    Status = OemNic->GetMac(&Mac, Port);
    if(EFI_ERROR(Status))
    {
        DEBUG((EFI_D_ERROR, "[%a]:[%dL] GetMac failed %r\n", __FUNCTION__, __LINE__, Status));
        return Status;
    }

    gMacAddress[0].data0=Mac.Addr[0];
    gMacAddress[0].data1=Mac.Addr[1];
    gMacAddress[0].data2=Mac.Addr[2];
    gMacAddress[0].data3=Mac.Addr[3];
    gMacAddress[0].data4=Mac.Addr[4];
    gMacAddress[0].data5=Mac.Addr[5];
    DEBUG((EFI_D_INFO, "Port%d:0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
        Port,gMacAddress[0].data0,gMacAddress[0].data1,gMacAddress[0].data2,
        gMacAddress[0].data3,gMacAddress[0].data4,gMacAddress[0].data5));

    return EFI_SUCCESS;
}

STATIC
EFI_STATUS
DelPhyhandleUpdateMacAddress(IN VOID* Fdt)
{
    UINT8               port;
    INTN                ethernetnode;
    INTN                node;
    INTN                Error;
    struct              fdt_property *m_prop;
    int                 m_oldlen;
    EFI_STATUS          Status = EFI_SUCCESS;
    EFI_STATUS          GetMacStatus = EFI_SUCCESS;

    node = fdt_subnode_offset(Fdt, 0, "soc");
    if (node < 0)
    {
        DEBUG ((EFI_D_ERROR, "can not find soc root node\n"));
        return EFI_INVALID_PARAMETER;
    }
    else
    {
        for( port=0; port<8; port++ )
        {
            GetMacStatus= GetMacAddress(port);
            ethernetnode = fdt_subnode_offset(Fdt, node,EthName[port]);
            if(!EFI_ERROR(GetMacStatus))
            {

                if (ethernetnode < 0)
                {
                    DEBUG ((EFI_D_WARN, "Can not find ethernet@%d node\n",port));
                    DEBUG ((EFI_D_WARN, "Suppose port %d is not enabled.\n", port));
                    continue;
                }
                m_prop = fdt_get_property_w(Fdt, ethernetnode, "local-mac-address", &m_oldlen);
                if(m_prop)
                {
                    Error = fdt_delprop(Fdt, ethernetnode, "local-mac-address");
                    if (Error)
                    {
                        DEBUG ((EFI_D_ERROR, "ERROR:fdt_delprop() Local-mac-address: %a\n", fdt_strerror (Error)));
                        Status = EFI_INVALID_PARAMETER;
                    }
                    Error = fdt_setprop(Fdt, ethernetnode, "local-mac-address",gMacAddress,sizeof(MAC_ADDRESS));
                    if (Error)
                    {
                        DEBUG ((EFI_D_ERROR, "ERROR:fdt_setprop():local-mac-address %a\n", fdt_strerror (Error)));
                        Status = EFI_INVALID_PARAMETER;
                    }
                }
            }
        }
    }
    return Status;
}

STATIC
EFI_STATUS
UpdateRefClk (IN VOID* Fdt)
{
  INTN                node;
  INTN                Error;
  struct              fdt_property *m_prop;
  int                 m_oldlen;
  UINTN               ArchTimerFreq = 0;
  UINT32              Data;
  CONST CHAR8         *Property = "clock-frequency";

  ArmArchTimerReadReg (CntFrq, &ArchTimerFreq);
  if (!ArchTimerFreq) {
    DEBUG ((DEBUG_ERROR, "[%a]:[%dL] Get timer frequency failed!\n", __FUNCTION__, __LINE__));
    return EFI_INVALID_PARAMETER;
  }

  node = fdt_subnode_offset(Fdt, 0, "soc");
  if (node < 0) {
    DEBUG ((DEBUG_ERROR, "can not find soc node\n"));
    return EFI_INVALID_PARAMETER;
  }

  node = fdt_subnode_offset(Fdt, node, "refclk");
  if (node < 0) {
    DEBUG ((DEBUG_ERROR, "can not find refclk node\n"));
    return EFI_INVALID_PARAMETER;
  }

  m_prop = fdt_get_property_w(Fdt, node, Property, &m_oldlen);
  if(!m_prop) {
    DEBUG ((DEBUG_ERROR, "[%a]:[%dL] Can't find property %a\n", __FUNCTION__, __LINE__, Property));
    return EFI_INVALID_PARAMETER;
  }

  Error = fdt_delprop(Fdt, node, Property);
  if (Error) {
    DEBUG ((DEBUG_ERROR, "ERROR: fdt_delprop() %a: %a\n", Property, fdt_strerror (Error)));
    return EFI_INVALID_PARAMETER;
  }

  // UINT32 is enough for refclk data length
  Data = (UINT32) ArchTimerFreq;
  Data = cpu_to_fdt32 (Data);
  Error = fdt_setprop(Fdt, node, Property, &Data, sizeof(Data));
  if (Error) {
    DEBUG ((DEBUG_ERROR, "ERROR:fdt_setprop() %a: %a\n", Property, fdt_strerror (Error)));
    return EFI_INVALID_PARAMETER;
  }

  DEBUG ((DEBUG_INFO, "Update refclk successfully.\n"));
  return EFI_SUCCESS;
}

INTN
GetMemoryNode(VOID* Fdt)
{
    INTN                node;
    int                 m_oldlen;
    struct              fdt_property *m_prop;
    INTN                Error = 0;


    node = fdt_subnode_offset(Fdt, 0, "memory");
    if (node < 0)
    {
        // Create the memory node
        node = fdt_add_subnode(Fdt, 0, "memory");

        if(node < 0)
        {
          DEBUG((EFI_D_ERROR, "[%a]:[%dL] fdt add subnode error\n", __FUNCTION__, __LINE__));

          return node;
        }

    }
    //find the memory node property
    m_prop = fdt_get_property_w(Fdt, node, "memory", &m_oldlen);
    if(m_prop)
    {
        Error = fdt_delprop(Fdt, node, "reg");

        if (Error)
        {
            DEBUG ((EFI_D_ERROR, "ERROR:fdt_delprop(): %a\n", fdt_strerror (Error)));
            node = -1;
            return node;
        }
    }

    return node;
}


EFI_STATUS UpdateMemoryNode(VOID* Fdt)
{
    INTN                Error = 0;
    EFI_STATUS          Status = EFI_SUCCESS;
    UINT32              Index = 0;
    UINT32              MemIndex;
    INTN                node;
    EFI_MEMORY_DESCRIPTOR *MemoryMap;
    EFI_MEMORY_DESCRIPTOR *MemoryMapPtr;
    EFI_MEMORY_DESCRIPTOR *MemoryMapPtrCurrent;
    UINTN                 MemoryMapSize;
    UINTN                 Pages0 = 0;
    UINTN                 Pages1 = 0;
    UINTN                 MapKey;
    UINTN                 DescriptorSize;
    UINT32                DescriptorVersion;
    PHY_MEM_REGION        *mRegion;
    UINTN                 MemoryMapLastEndAddress ;
    UINTN                 MemoryMapcontinuousStartAddress ;
    UINTN                 MemoryMapCurrentStartAddress;
    BOOLEAN               FindMemoryRegionFlag = FALSE;

    node = GetMemoryNode(Fdt);
    if (node < 0)
    {
        DEBUG((EFI_D_ERROR, "Can not find memory node\n"));
        return EFI_NOT_FOUND;
    }
    MemoryMap = NULL;
    MemoryMapSize = 0;
    MemIndex = 0;

    Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);
    if (Status == EFI_BUFFER_TOO_SMALL)
    {
        // The UEFI specification advises to allocate more memory for the MemoryMap buffer between successive
        // calls to GetMemoryMap(), since allocation of the new buffer may potentially increase memory map size.
        Pages0 = EFI_SIZE_TO_PAGES (MemoryMapSize) + 1;
        MemoryMap = AllocatePages (Pages0);
        if (MemoryMap == NULL)
        {
            Status = EFI_OUT_OF_RESOURCES;
            return Status;
        }
        Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);

        if (EFI_ERROR(Status))
        {
            DEBUG ((EFI_D_ERROR, "FdtUpdateLib GetMemoryMap Error\n"));
            FreePages (MemoryMap, Pages0);
            return Status;
        }
    }
    else
    {
        DEBUG ((EFI_D_ERROR, "FdtUpdateLib GetmemoryMap Status: %r\n",Status));
        return EFI_ABORTED;
    }

    mRegion = NULL;
    Pages1 = EFI_SIZE_TO_PAGES (sizeof(PHY_MEM_REGION) *( MemoryMapSize / DescriptorSize));

    mRegion = (PHY_MEM_REGION*)AllocatePool(Pages1);
    if (mRegion == NULL)
    {
      Status = EFI_OUT_OF_RESOURCES;
      FreePages (MemoryMap, Pages0);
      return Status;
    }


    MemoryMapPtr = MemoryMap;
    MemoryMapPtrCurrent = MemoryMapPtr;
    MemoryMapLastEndAddress = 0;
    MemoryMapcontinuousStartAddress = 0;
    MemoryMapCurrentStartAddress = 0;
    for (Index = 0; Index < (MemoryMapSize / DescriptorSize); Index++)
    {
        MemoryMapPtrCurrent = (EFI_MEMORY_DESCRIPTOR*)((UINTN)MemoryMapPtr + Index*DescriptorSize);
        MemoryMapCurrentStartAddress = (UINTN)MemoryMapPtrCurrent->PhysicalStart;

        if (!IsMemMapRegion ((EFI_MEMORY_TYPE)MemoryMapPtrCurrent->Type))
        {
            continue;
        }
        else
        {
            FindMemoryRegionFlag = TRUE;
            if(MemoryMapCurrentStartAddress != MemoryMapLastEndAddress)
            {
                mRegion[MemIndex].BaseHigh= cpu_to_fdt32(MemoryMapcontinuousStartAddress>>32);
                mRegion[MemIndex].BaseLow=cpu_to_fdt32(MemoryMapcontinuousStartAddress);
                mRegion[MemIndex].LengthHigh= cpu_to_fdt32((MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress)>>32);
                mRegion[MemIndex].LengthLow=cpu_to_fdt32(MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress);
                MemIndex+=1;
                MemoryMapcontinuousStartAddress=MemoryMapCurrentStartAddress;
            }
        }
        MemoryMapLastEndAddress = (UINTN)(MemoryMapPtrCurrent->PhysicalStart + MemoryMapPtrCurrent->NumberOfPages * EFI_PAGE_SIZE);
    }
    if (FindMemoryRegionFlag)
    {
        mRegion[MemIndex].BaseHigh = cpu_to_fdt32(MemoryMapcontinuousStartAddress>>32);
        mRegion[MemIndex].BaseLow = cpu_to_fdt32(MemoryMapcontinuousStartAddress);
        mRegion[MemIndex].LengthHigh = cpu_to_fdt32((MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress)>>32);
        mRegion[MemIndex].LengthLow = cpu_to_fdt32(MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress);
    }

    Error = fdt_setprop(Fdt, node, "reg",mRegion,sizeof(PHY_MEM_REGION) *(MemIndex+1));

    FreePool (mRegion);
    FreePages (MemoryMap, Pages0);
    if (Error)
    {
        DEBUG ((EFI_D_ERROR, "ERROR:fdt_setprop(): %a\n", fdt_strerror (Error)));
        Status = EFI_INVALID_PARAMETER;
        return Status;
    }

  return Status;
}


EFI_STATUS
UpdateNumaNode(VOID* Fdt)
{
    //TODO: Need to update numa node
    return EFI_SUCCESS;
}
/*
 * Entry point for fdtupdate lib.
 */

EFI_STATUS EFIFdtUpdate(UINTN FdtFileAddr)
{
    INTN                Error;
    VOID*               Fdt;
    UINT32              Size;
    UINTN               NewFdtBlobSize;
    UINTN               NewFdtBlobBase;
    EFI_STATUS          Status = EFI_SUCCESS;
    EFI_STATUS          UpdateNumaStatus = EFI_SUCCESS;


    Error = fdt_check_header ((VOID*)(FdtFileAddr));
    if (0 != Error)
    {
        DEBUG ((EFI_D_ERROR,"ERROR: Device Tree header not valid (%a)\n", fdt_strerror(Error)));
        return EFI_INVALID_PARAMETER;
    }

    Size = (UINTN)fdt_totalsize ((VOID*)(UINTN)(FdtFileAddr));
    NewFdtBlobSize = Size + ADD_FILE_LENGTH;
    Fdt = (VOID*)(UINTN)FdtFileAddr;

    Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, EFI_SIZE_TO_PAGES(NewFdtBlobSize), &NewFdtBlobBase);
    if (EFI_ERROR (Status))
    {
        return EFI_OUT_OF_RESOURCES;
    }


    Error = fdt_open_into(Fdt,(VOID*)(UINTN)(NewFdtBlobBase), (NewFdtBlobSize));
    if (Error) {
        DEBUG ((EFI_D_ERROR, "ERROR:fdt_open_into(): %a\n", fdt_strerror (Error)));
        Status = EFI_INVALID_PARAMETER;
        goto EXIT;
    }

    Fdt = (VOID*)(UINTN)NewFdtBlobBase;
    Status = DelPhyhandleUpdateMacAddress(Fdt);
    if (EFI_ERROR (Status))
    {
        DEBUG ((EFI_D_ERROR, "DelPhyhandleUpdateMacAddress fail:\n"));
        Status = EFI_SUCCESS;
    }

    Status =  UpdateRefClk (Fdt);
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "UpdateiRefClk fail.\n"));
    }

    Status = UpdateMemoryNode(Fdt);
    if (EFI_ERROR (Status))
    {
        DEBUG ((EFI_D_ERROR, "UpdateMemoryNode Error\n"));
        goto EXIT;
    }

    UpdateNumaStatus = UpdateNumaNode(Fdt);
    if (EFI_ERROR (UpdateNumaStatus))
    {
        DEBUG ((EFI_D_ERROR, "Update NumaNode fail\n"));
    }

    gBS->CopyMem(((VOID*)(UINTN)(FdtFileAddr)),((VOID*)(UINTN)(NewFdtBlobBase)),NewFdtBlobSize);

EXIT:
    gBS->FreePages(NewFdtBlobBase,EFI_SIZE_TO_PAGES(NewFdtBlobSize));

    return Status;



}