summaryrefslogtreecommitdiff
path: root/Silicon/AMD/Styx/Drivers/MpBootDxe/MpBootDxe.c
blob: bd7244648ab0f3e7a1f09278a924e6ad7e550aa8 (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
/** @file

  Copyright (c) 2016, AMD Inc. All rights reserved.<BR>

  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 <Library/ArmLib.h>
#include <Library/ArmSmcLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/CacheMaintenanceLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiDriverEntryPoint.h>
#include <Library/UefiBootServicesTableLib.h>

#include <Guid/ArmMpCoreInfo.h>
#include <Protocol/AmdMpBoot.h>


/* These externs are used to relocate our Pen code into pre-allocated memory */
extern VOID  *SecondariesPenStart;
extern VOID  *SecondariesPenEnd;
extern UINTN *AsmParkingBase;
extern UINTN *AsmMailboxBase;


STATIC
EFI_PHYSICAL_ADDRESS
ConfigurePen (
  IN EFI_PHYSICAL_ADDRESS     MpParkingBase,
  IN UINTN                    MpParkingSize,
  IN ARM_CORE_INFO            *ArmCoreInfoTable,
  IN UINTN                    ArmCoreCount
  )
{
  EFI_PHYSICAL_ADDRESS     PenBase;
  UINTN                    PenSize;
  UINTN                    MailboxBase;
  UINTN                    CoreNum;
  UINTN                    CoreMailbox;
  UINTN                    CoreParking;

  //
  // Set Pen at the 2K-offset of the Parking area, skipping an 8-byte slot for the Core#.
  // For details, refer to the "Multi-processor Startup for ARM Platforms" document:
  // https://acpica.org/sites/acpica/files/MP%20Startup%20for%20ARM%20platforms.docx
  //
  PenBase = (EFI_PHYSICAL_ADDRESS)((UINTN)MpParkingBase + SIZE_2KB + sizeof(UINT64));
  PenSize  = (UINTN)&SecondariesPenEnd - (UINTN)&SecondariesPenStart;

  // Relocate the Pen code
  CopyMem ((VOID*)(PenBase), (VOID*)&SecondariesPenStart, PenSize);

  // Put spin-table mailboxes below the pen code so we know where they are relative to code.
  // Make sure this is 8 byte aligned.
  MailboxBase = (UINTN)PenBase + ((UINTN)&SecondariesPenEnd - (UINTN)&SecondariesPenStart);
  if (MailboxBase % sizeof(UINT64) != 0) {
    MailboxBase += sizeof(UINT64) - MailboxBase % sizeof(UINT64);
  }

  // Update variables used in the Pen code
  *(UINTN*)(PenBase + ((UINTN)&AsmMailboxBase - (UINTN)&SecondariesPenStart)) = MailboxBase;
  *(UINTN*)(PenBase + ((UINTN)&AsmParkingBase - (UINTN)&SecondariesPenStart)) = (UINTN)MpParkingBase;

  for (CoreNum = 0; CoreNum < ArmCoreCount; CoreNum++) {
    // Clear the jump address at spin-table slot
    CoreMailbox = MailboxBase + CoreNum * sizeof (UINT64);
    *((UINTN*)(CoreMailbox)) = 0x0;

    // Clear the jump address and set Core# at mp-parking slot
    CoreParking = (UINTN)MpParkingBase + CoreNum * SIZE_4KB;
    *((UINTN*)(CoreParking + sizeof (UINT64))) = 0x0;
    *((UINTN*)(CoreParking + SIZE_2KB)) = CoreNum;

    // Update table entry to be consumed by FDT parser
    ArmCoreInfoTable[CoreNum].MailboxSetAddress = CoreMailbox;
  }

  // flush the cache before launching secondary cores
  WriteBackDataCacheRange ((VOID *)MpParkingBase, MpParkingSize);

  return PenBase;
}


EFI_STATUS
EFIAPI
MpBootDxeEntryPoint (
  IN EFI_HANDLE         ImageHandle,
  IN EFI_SYSTEM_TABLE   *SystemTable
  )
{
  EFI_STATUS               Status;
  AMD_MP_BOOT_PROTOCOL     *MpBootProtocol;
  EFI_PHYSICAL_ADDRESS     MpParkingBase;
  UINTN                    MpParkingSize;
  ARM_CORE_INFO            *ArmCoreInfoTable;
  UINTN                    ArmCoreCount;
  UINTN                    CoreNum;
  EFI_PHYSICAL_ADDRESS     PenBase;

  DEBUG ((EFI_D_ERROR, "MpBootDxe Loaded\n"));

  MpBootProtocol = NULL;
  Status = gBS->LocateProtocol (
               &gAmdMpBootProtocolGuid,
               NULL,
               (VOID **)&MpBootProtocol
               );
  if (EFI_ERROR (Status) || MpBootProtocol == NULL) {
    DEBUG ((EFI_D_ERROR, "Warning: Failed to locate MP-Boot Protocol.\n"));
    return EFI_UNSUPPORTED;
  }

  if ((VOID *)MpBootProtocol->MpBootInfo == NULL) {
    DEBUG ((EFI_D_ERROR, "Warning: MpBootInfo not allocated.\n"));
    return EFI_UNSUPPORTED;
  }

  MpParkingBase = MpBootProtocol->MpBootInfo->MpParkingBase;
  if ((VOID *)MpParkingBase == NULL) {
    DEBUG ((EFI_D_ERROR, "Warning: MpParkingBase not allocated.\n"));
    return EFI_UNSUPPORTED;
  }
  if (((UINTN)MpParkingBase & (SIZE_4KB -1)) != 0) {
    DEBUG ((EFI_D_ERROR, "Warning: MpParkingBase not 4K aligned.\n"));
    return EFI_UNSUPPORTED;
  }

  ArmCoreInfoTable = MpBootProtocol->MpBootInfo->ArmCoreInfoTable;
  if (ArmCoreInfoTable == NULL) {
    DEBUG ((EFI_D_ERROR, "Warning: ArmCoreInfoTable not allocated.\n"));
    return EFI_UNSUPPORTED;
  }

  ArmCoreCount = MpBootProtocol->MpBootInfo->ArmCoreCount;
  if (ArmCoreCount < 2) {
    DEBUG ((EFI_D_ERROR, "Warning: Found %d cores.\n", ArmCoreCount));
    return EFI_UNSUPPORTED;
  }

  MpParkingSize = ArmCoreCount * SIZE_4KB;
  if (MpParkingSize > MpBootProtocol->MpBootInfo->MpParkingSize) {
    DEBUG ((EFI_D_ERROR, "Warning: MpParkingSize = 0x%lX, not large enough for %d cores.\n",
      MpBootProtocol->MpBootInfo->MpParkingSize, ArmCoreCount));
    return EFI_UNSUPPORTED;
  }

  if ((VOID *)MpBootProtocol->ParkSecondaryCore == NULL) {
    DEBUG ((EFI_D_ERROR, "Warning: ParkSecondaryCore() not supported.\n"));
    return EFI_UNSUPPORTED;
  }

  // Move secondary cores to our new Pen
  PenBase = ConfigurePen (MpParkingBase, MpParkingSize, ArmCoreInfoTable, ArmCoreCount);
  for (CoreNum = 0; CoreNum < ArmCoreCount; CoreNum++) {
    MpBootProtocol->ParkSecondaryCore (&ArmCoreInfoTable[CoreNum], PenBase);
  }

  return EFI_SUCCESS;
}