summaryrefslogtreecommitdiffstats
path: root/OvmfPkg/CpuHotplugSmm/QemuCpuhp.c
blob: ec3dc379f309ea8f8cc7dbff9632d3532137f036 (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
/** @file
  Simple wrapper functions and utility functions that access QEMU's modern CPU
  hotplug register block.

  These functions manipulate some of the registers described in
  "docs/specs/acpi_cpu_hotplug.txt" in the QEMU source. IO Ports are accessed
  via EFI_MM_CPU_IO_PROTOCOL. If a protocol call fails, these functions don't
  return.

  Copyright (c) 2020, Red Hat, Inc.

  SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <IndustryStandard/Q35MchIch9.h>     // ICH9_CPU_HOTPLUG_BASE
#include <IndustryStandard/QemuCpuHotplug.h> // QEMU_CPUHP_R_CMD_DATA2
#include <Library/BaseLib.h>                 // CpuDeadLoop()
#include <Library/DebugLib.h>                // DEBUG()

#include "QemuCpuhp.h"

UINT32
QemuCpuhpReadCommandData2 (
  IN CONST EFI_MM_CPU_IO_PROTOCOL  *MmCpuIo
  )
{
  UINT32      CommandData2;
  EFI_STATUS  Status;

  CommandData2 = 0;
  Status       = MmCpuIo->Io.Read (
                               MmCpuIo,
                               MM_IO_UINT32,
                               ICH9_CPU_HOTPLUG_BASE + QEMU_CPUHP_R_CMD_DATA2,
                               1,
                               &CommandData2
                               );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, Status));
    ASSERT (FALSE);
    CpuDeadLoop ();
  }

  return CommandData2;
}

UINT8
QemuCpuhpReadCpuStatus (
  IN CONST EFI_MM_CPU_IO_PROTOCOL  *MmCpuIo
  )
{
  UINT8       CpuStatus;
  EFI_STATUS  Status;

  CpuStatus = 0;
  Status    = MmCpuIo->Io.Read (
                            MmCpuIo,
                            MM_IO_UINT8,
                            ICH9_CPU_HOTPLUG_BASE + QEMU_CPUHP_R_CPU_STAT,
                            1,
                            &CpuStatus
                            );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, Status));
    ASSERT (FALSE);
    CpuDeadLoop ();
  }

  return CpuStatus;
}

UINT32
QemuCpuhpReadCommandData (
  IN CONST EFI_MM_CPU_IO_PROTOCOL  *MmCpuIo
  )
{
  UINT32      CommandData;
  EFI_STATUS  Status;

  CommandData = 0;
  Status      = MmCpuIo->Io.Read (
                              MmCpuIo,
                              MM_IO_UINT32,
                              ICH9_CPU_HOTPLUG_BASE + QEMU_CPUHP_RW_CMD_DATA,
                              1,
                              &CommandData
                              );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, Status));
    ASSERT (FALSE);
    CpuDeadLoop ();
  }

  return CommandData;
}

VOID
QemuCpuhpWriteCpuSelector (
  IN CONST EFI_MM_CPU_IO_PROTOCOL  *MmCpuIo,
  IN UINT32                        Selector
  )
{
  EFI_STATUS  Status;

  Status = MmCpuIo->Io.Write (
                         MmCpuIo,
                         MM_IO_UINT32,
                         ICH9_CPU_HOTPLUG_BASE + QEMU_CPUHP_W_CPU_SEL,
                         1,
                         &Selector
                         );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, Status));
    ASSERT (FALSE);
    CpuDeadLoop ();
  }
}

VOID
QemuCpuhpWriteCpuStatus (
  IN CONST EFI_MM_CPU_IO_PROTOCOL  *MmCpuIo,
  IN UINT8                         CpuStatus
  )
{
  EFI_STATUS  Status;

  Status = MmCpuIo->Io.Write (
                         MmCpuIo,
                         MM_IO_UINT8,
                         ICH9_CPU_HOTPLUG_BASE + QEMU_CPUHP_R_CPU_STAT,
                         1,
                         &CpuStatus
                         );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, Status));
    ASSERT (FALSE);
    CpuDeadLoop ();
  }
}

VOID
QemuCpuhpWriteCommand (
  IN CONST EFI_MM_CPU_IO_PROTOCOL  *MmCpuIo,
  IN UINT8                         Command
  )
{
  EFI_STATUS  Status;

  Status = MmCpuIo->Io.Write (
                         MmCpuIo,
                         MM_IO_UINT8,
                         ICH9_CPU_HOTPLUG_BASE + QEMU_CPUHP_W_CMD,
                         1,
                         &Command
                         );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, Status));
    ASSERT (FALSE);
    CpuDeadLoop ();
  }
}

/**
  Collect the APIC IDs of
  - the CPUs that have been hot-plugged,
  - the CPUs that are about to be hot-unplugged.

  This function only scans for events -- it does not modify them -- in the
  hotplug registers.

  On error, the contents of the output parameters are undefined.

  @param[in] MmCpuIo             The EFI_MM_CPU_IO_PROTOCOL instance for
                                 accessing IO Ports.

  @param[in] PossibleCpuCount    The number of possible CPUs in the system. Must
                                 be positive.

  @param[in] ApicIdCount         The number of elements each one of the
                                 PluggedApicIds and ToUnplugApicIds arrays can
                                 accommodate. Must be positive.

  @param[out] PluggedApicIds     The APIC IDs of the CPUs that have been
                                 hot-plugged.

  @param[out] PluggedCount       The number of filled-in APIC IDs in
                                 PluggedApicIds.

  @param[out] ToUnplugApicIds    The APIC IDs of the CPUs that are about to be
                                 hot-unplugged.

  @param[out] ToUnplugSelectors  The QEMU Selectors of the CPUs that are about
                                 to be hot-unplugged.

  @param[out] ToUnplugCount      The number of filled-in APIC IDs in
                                 ToUnplugApicIds.

  @retval EFI_INVALID_PARAMETER  PossibleCpuCount is zero, or ApicIdCount is
                                 zero.

  @retval EFI_PROTOCOL_ERROR     Invalid bitmap detected in the
                                 QEMU_CPUHP_R_CPU_STAT register.

  @retval EFI_BUFFER_TOO_SMALL   There was an attempt to place more than
                                 ApicIdCount APIC IDs into one of the
                                 PluggedApicIds and ToUnplugApicIds arrays.

  @retval EFI_SUCCESS            Output parameters have been set successfully.
**/
EFI_STATUS
QemuCpuhpCollectApicIds (
  IN  CONST EFI_MM_CPU_IO_PROTOCOL  *MmCpuIo,
  IN  UINT32                        PossibleCpuCount,
  IN  UINT32                        ApicIdCount,
  OUT APIC_ID                       *PluggedApicIds,
  OUT UINT32                        *PluggedCount,
  OUT APIC_ID                       *ToUnplugApicIds,
  OUT UINT32                        *ToUnplugSelectors,
  OUT UINT32                        *ToUnplugCount
  )
{
  UINT32  CurrentSelector;

  if ((PossibleCpuCount == 0) || (ApicIdCount == 0)) {
    return EFI_INVALID_PARAMETER;
  }

  *PluggedCount  = 0;
  *ToUnplugCount = 0;

  CurrentSelector = 0;
  do {
    UINT32   PendingSelector;
    UINT8    CpuStatus;
    APIC_ID  *ExtendIds;
    UINT32   *ExtendSels;
    UINT32   *ExtendCount;
    APIC_ID  NewApicId;

    //
    // Write CurrentSelector (which is valid) to the CPU selector register.
    // Consequences:
    //
    // - Other register accesses will be permitted.
    //
    // - The QEMU_CPUHP_CMD_GET_PENDING command will start scanning for a CPU
    //   with pending events at CurrentSelector (inclusive).
    //
    QemuCpuhpWriteCpuSelector (MmCpuIo, CurrentSelector);
    //
    // Write the QEMU_CPUHP_CMD_GET_PENDING command. Consequences
    // (independently of each other):
    //
    // - If there is a CPU with pending events, starting at CurrentSelector
    //   (inclusive), the CPU selector will be updated to that CPU. Note that
    //   the scanning in QEMU may wrap around, because we must never clear the
    //   event bits.
    //
    // - The QEMU_CPUHP_RW_CMD_DATA register will return the (possibly updated)
    //   CPU selector value.
    //
    QemuCpuhpWriteCommand (MmCpuIo, QEMU_CPUHP_CMD_GET_PENDING);
    PendingSelector = QemuCpuhpReadCommandData (MmCpuIo);
    if (PendingSelector < CurrentSelector) {
      DEBUG ((
        DEBUG_VERBOSE,
        "%a: CurrentSelector=%u PendingSelector=%u: "
        "wrap-around\n",
        __FUNCTION__,
        CurrentSelector,
        PendingSelector
        ));
      break;
    }

    CurrentSelector = PendingSelector;

    //
    // Check the known status / event bits for the currently selected CPU.
    //
    CpuStatus = QemuCpuhpReadCpuStatus (MmCpuIo);
    if ((CpuStatus & QEMU_CPUHP_STAT_INSERT) != 0) {
      //
      // The "insert" event guarantees the "enabled" status; plus it excludes
      // the "fw_remove" event.
      //
      if (((CpuStatus & QEMU_CPUHP_STAT_ENABLED) == 0) ||
          ((CpuStatus & QEMU_CPUHP_STAT_FW_REMOVE) != 0))
      {
        DEBUG ((
          DEBUG_ERROR,
          "%a: CurrentSelector=%u CpuStatus=0x%x: "
          "inconsistent CPU status\n",
          __FUNCTION__,
          CurrentSelector,
          CpuStatus
          ));
        return EFI_PROTOCOL_ERROR;
      }

      DEBUG ((
        DEBUG_VERBOSE,
        "%a: CurrentSelector=%u: insert\n",
        __FUNCTION__,
        CurrentSelector
        ));

      ExtendIds   = PluggedApicIds;
      ExtendSels  = NULL;
      ExtendCount = PluggedCount;
    } else if ((CpuStatus & QEMU_CPUHP_STAT_FW_REMOVE) != 0) {
      //
      // "fw_remove" event guarantees "enabled".
      //
      if ((CpuStatus & QEMU_CPUHP_STAT_ENABLED) == 0) {
        DEBUG ((
          DEBUG_ERROR,
          "%a: CurrentSelector=%u CpuStatus=0x%x: "
          "inconsistent CPU status\n",
          __FUNCTION__,
          CurrentSelector,
          CpuStatus
          ));
        return EFI_PROTOCOL_ERROR;
      }

      DEBUG ((
        DEBUG_VERBOSE,
        "%a: CurrentSelector=%u: fw_remove\n",
        __FUNCTION__,
        CurrentSelector
        ));

      ExtendIds   = ToUnplugApicIds;
      ExtendSels  = ToUnplugSelectors;
      ExtendCount = ToUnplugCount;
    } else if ((CpuStatus & QEMU_CPUHP_STAT_REMOVE) != 0) {
      //
      // Let the OSPM deal with the "remove" event.
      //
      DEBUG ((
        DEBUG_VERBOSE,
        "%a: CurrentSelector=%u: remove (ignored)\n",
        __FUNCTION__,
        CurrentSelector
        ));

      ExtendIds   = NULL;
      ExtendSels  = NULL;
      ExtendCount = NULL;
    } else {
      DEBUG ((
        DEBUG_VERBOSE,
        "%a: CurrentSelector=%u: no event\n",
        __FUNCTION__,
        CurrentSelector
        ));
      break;
    }

    ASSERT ((ExtendIds == NULL) == (ExtendCount == NULL));
    ASSERT ((ExtendSels == NULL) || (ExtendIds != NULL));

    if (ExtendIds != NULL) {
      //
      // Save the APIC ID of the CPU with the pending event, to the
      // corresponding APIC ID array.
      // For unplug events, also save the CurrentSelector.
      //
      if (*ExtendCount == ApicIdCount) {
        DEBUG ((DEBUG_ERROR, "%a: APIC ID array too small\n", __FUNCTION__));
        return EFI_BUFFER_TOO_SMALL;
      }

      QemuCpuhpWriteCommand (MmCpuIo, QEMU_CPUHP_CMD_GET_ARCH_ID);
      NewApicId = QemuCpuhpReadCommandData (MmCpuIo);
      DEBUG ((
        DEBUG_VERBOSE,
        "%a: ApicId=" FMT_APIC_ID "\n",
        __FUNCTION__,
        NewApicId
        ));
      if (ExtendSels != NULL) {
        ExtendSels[(*ExtendCount)] = CurrentSelector;
      }

      ExtendIds[(*ExtendCount)++] = NewApicId;
    }

    //
    // We've processed the CPU with (known) pending events, but we must never
    // clear events. Therefore we need to advance past this CPU manually;
    // otherwise, QEMU_CPUHP_CMD_GET_PENDING would stick to the currently
    // selected CPU.
    //
    CurrentSelector++;
  } while (CurrentSelector < PossibleCpuCount);

  DEBUG ((
    DEBUG_VERBOSE,
    "%a: PluggedCount=%u ToUnplugCount=%u\n",
    __FUNCTION__,
    *PluggedCount,
    *ToUnplugCount
    ));
  return EFI_SUCCESS;
}