summaryrefslogtreecommitdiffstats
path: root/IntelSiliconPkg/Feature/VTd/PlatformVTdSampleDxe/PlatformVTdSampleDxe.c
blob: 3587fa3c83453470c3e3446e7b1010d0ff77fc86 (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
/** @file
  Platform VTd Sample driver.

  Copyright (c) 2017, Intel Corporation. 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 <PiDxe.h>

#include <IndustryStandard/Vtd.h>
#include <Protocol/PlatformVtdPolicy.h>
#include <Protocol/PciIo.h>
#include <Protocol/DevicePath.h>

#include <Library/IoLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DevicePathLib.h>

#include <IndustryStandard/DmaRemappingReportingTable.h>

typedef struct {
  ACPI_EXTENDED_HID_DEVICE_PATH      I2cController;
  UINT8                              HidStr[8];
  UINT8                              UidStr[1];
  UINT8                              CidStr[8];
} PLATFORM_I2C_CONTROLLER_DEVICE_PATH;

typedef struct {
  ACPI_EXTENDED_HID_DEVICE_PATH      I2cDevice;
  UINT8                              HidStr[13];
  UINT8                              UidStr[1];
  UINT8                              CidStr[13];
} PLATFORM_I2C_DEVICE_DEVICE_PATH;

typedef struct {
  PLATFORM_I2C_CONTROLLER_DEVICE_PATH      I2cController;
  PLATFORM_I2C_DEVICE_DEVICE_PATH          I2cDevice;
  EFI_DEVICE_PATH_PROTOCOL                 End;
} PLATFORM_I2C_DEVICE_PATH;

typedef struct {
  ACPI_HID_DEVICE_PATH      PciRootBridge;
  PCI_DEVICE_PATH           PciDevice;
  EFI_DEVICE_PATH_PROTOCOL  EndDevicePath;
} PLATFORM_PCI_DEVICE_PATH;

typedef struct {
  ACPI_HID_DEVICE_PATH      PciRootBridge;
  PCI_DEVICE_PATH           PciBridge;
  PCI_DEVICE_PATH           PciDevice;
  EFI_DEVICE_PATH_PROTOCOL  EndDevicePath;
} PLATFORM_PCI_BRIDGE_DEVICE_PATH;

typedef struct {
  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
  UINT16                    Segment;
  VTD_SOURCE_ID             SourceId;
} PLATFORM_ACPI_DEVICE_MAPPING;

#define PLATFORM_PCI_ROOT_BRIDGE \
  { \
    { \
      ACPI_DEVICE_PATH, \
      ACPI_DP, \
      { \
        (UINT8) (sizeof (ACPI_HID_DEVICE_PATH)), \
        (UINT8) ((sizeof (ACPI_HID_DEVICE_PATH)) >> 8) \
      }, \
    }, \
    EISA_PNP_ID (0x0A03), \
    0 \
  }

#define PLATFORM_END_ENTIRE \
  { \
    END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { END_DEVICE_PATH_LENGTH, 0 } \
  }

#define PLATFORM_PCI(Device, Function) \
  { \
    { \
      HARDWARE_DEVICE_PATH, \
      HW_PCI_DP, \
      { \
        (UINT8) (sizeof (PCI_DEVICE_PATH)), \
        (UINT8) ((sizeof (PCI_DEVICE_PATH)) >> 8) \
      } \
    }, \
    (Function), \
    (Device) \
  }

#define PLATFORM_I2C(Hid, Uid, Cid, HidStr, UidStr, CidStr) \
  { \
    { \
      { \
        ACPI_DEVICE_PATH, \
        ACPI_EXTENDED_DP, \
        {sizeof(ACPI_EXTENDED_HID_DEVICE_PATH) + sizeof(HidStr) + sizeof(UidStr) + sizeof(CidStr), 0} \
      }, \
      Hid, \
      Uid, \
      Cid \
    }, \
    HidStr, \
    UidStr, \
    CidStr \
  }

PLATFORM_I2C_DEVICE_PATH mPlatformI2CDevicePath = {
  PLATFORM_I2C(0, 2, 0, "INT33C3", "", "INT33C3"),
  PLATFORM_I2C(0, 1, 0, "I2C01\\TPANEL", "", "I2C01\\TPANEL"),
  PLATFORM_END_ENTIRE
};

PLATFORM_ACPI_DEVICE_MAPPING  mAcpiDeviceMapping[] = {
  {
    (EFI_DEVICE_PATH_PROTOCOL *)&mPlatformI2CDevicePath,
    0x0,                 // Segment
    {{0x01, 0x15, 0x00}} // Function, Device, Bus
  }
};

PLATFORM_PCI_BRIDGE_DEVICE_PATH mPlatformPciBridgeDevicePath = {
  PLATFORM_PCI_ROOT_BRIDGE,
  PLATFORM_PCI(0x1C, 1),
  PLATFORM_PCI(0, 0),
  PLATFORM_END_ENTIRE
};

#pragma pack(1)

typedef struct {
  EDKII_PLATFORM_VTD_EXCEPTION_DEVICE_INFO     ExceptionDeviceInfo;
  EDKII_PLATFORM_VTD_DEVICE_SCOPE              DeviceScope;
  EFI_ACPI_DMAR_PCI_PATH                       PciBridge;
  EFI_ACPI_DMAR_PCI_PATH                       PciDevice;
} PLATFORM_EXCEPTION_DEVICE_SCOPE_STRUCT;

typedef struct {
  EDKII_PLATFORM_VTD_EXCEPTION_DEVICE_INFO     ExceptionDeviceInfo;
  EDKII_PLATFORM_VTD_PCI_DEVICE_ID             PciDeviceId;
} PLATFORM_EXCEPTION_PCI_DEVICE_ID_STRUCT;

#pragma pack()

PLATFORM_EXCEPTION_DEVICE_SCOPE_STRUCT  mExceptionDeviceScopeList[] = {
  {
    {
      EDKII_PLATFORM_VTD_EXCEPTION_DEVICE_INFO_TYPE_DEVICE_SCOPE,
      sizeof(PLATFORM_EXCEPTION_DEVICE_SCOPE_STRUCT)
    },  // ExceptionDeviceInfo
    {
      0,                                                    // SegmentNumber
      {
        EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_PCI_ENDPOINT,      // Type
        sizeof(EFI_ACPI_DMAR_DEVICE_SCOPE_STRUCTURE_HEADER) +
          2 * sizeof(EFI_ACPI_DMAR_PCI_PATH),               // Length
        0,                                                  // Reserved2
        0,                                                  // EnumerationId
        0,                                                  // StartBusNumber
      },
    },                                                      // DeviceScope
    { 0x1C, 1 },                                            // PciBridge
    { 0x0,  0 },                                            // PciDevice
  },
};

PLATFORM_EXCEPTION_PCI_DEVICE_ID_STRUCT  mExceptionPciDeviceIdList[] = {
  {
    {
      EDKII_PLATFORM_VTD_EXCEPTION_DEVICE_INFO_TYPE_PCI_DEVICE_ID,
      sizeof(PLATFORM_EXCEPTION_PCI_DEVICE_ID_STRUCT)
    },  // ExceptionDeviceInfo
    {
      0x8086,                                               // VendorId
      0x9D2F,                                               // DeviceId
      0x21,                                                 // RevisionId
      0x8086,                                               // SubsystemVendorId
      0x7270,                                               // SubsystemDeviceId
    },
  },
};

/**
  Compares 2 device path.

  @param[in] DevicePath1  A device path with EndDevicePath node.
  @param[in] DevicePath2  A device path with EndDevicePath node.

  @retval TRUE   2 device path are identical.
  @retval FALSE  2 device path are not identical.
**/
BOOLEAN
CompareDevicePath (
  IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath1,
  IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath2
  )
{
  UINTN  Size1;
  UINTN  Size2;

  Size1 = GetDevicePathSize (DevicePath1);
  Size2 = GetDevicePathSize (DevicePath2);
  if (Size1 != Size2) {
    return FALSE;
  }
  if (CompareMem (DevicePath1, DevicePath2, Size1) != 0) {
    return FALSE;
  }
  return TRUE;
}

/**
  Get the VTD SourceId from the device handler.
  This function is required for non PCI device handler.

  Pseudo-algo in Intel VTd driver:
    Status = PlatformGetVTdDeviceId ();
    if (EFI_ERROR(Status)) {
      if (DeviceHandle is PCI) {
        Get SourceId from Bus/Device/Function
      } else {
        return EFI_UNSUPPORTED
      }
    }
    Get VTd engine by Segment/Bus/Device/Function.

  @param[in]  This                  The protocol instance pointer.
  @param[in]  DeviceHandle          Device Identifier in UEFI.
  @param[out] DeviceInfo            DeviceInfo for indentify the VTd engine in ACPI Table
                                    and the VTd page entry.

  @retval EFI_SUCCESS           The VtdIndex and SourceId are returned.
  @retval EFI_INVALID_PARAMETER DeviceHandle is not a valid handler.
  @retval EFI_INVALID_PARAMETER DeviceInfo is NULL.
  @retval EFI_NOT_FOUND         The Segment or SourceId information is NOT found.
  @retval EFI_UNSUPPORTED       This function is not supported.

**/
EFI_STATUS
EFIAPI
PlatformVTdGetDeviceId (
  IN  EDKII_PLATFORM_VTD_POLICY_PROTOCOL       *This,
  IN  EFI_HANDLE                               DeviceHandle,
  OUT EDKII_PLATFORM_VTD_DEVICE_INFO           *DeviceInfo
  )
{
  EFI_PCI_IO_PROTOCOL       *PciIo;
  UINTN                     Seg;
  UINTN                     Bus;
  UINTN                     Dev;
  UINTN                     Func;
  EFI_STATUS                Status;
  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
  UINTN                     Index;

  DEBUG ((DEBUG_VERBOSE, "PlatformVTdGetDeviceId\n"));

  if (DeviceInfo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  if (DeviceHandle == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // Handle PCI device
  //
  Status = gBS->HandleProtocol (DeviceHandle, &gEfiPciIoProtocolGuid, (VOID **)&PciIo);
  if (!EFI_ERROR(Status)) {
    Status = PciIo->GetLocation (PciIo, &Seg, &Bus, &Dev, &Func);
    if (EFI_ERROR(Status)) {
      return EFI_UNSUPPORTED;
    }
    DeviceInfo->Segment = (UINT16)Seg;
    DeviceInfo->SourceId.Bits.Bus = (UINT8)Bus;
    DeviceInfo->SourceId.Bits.Device = (UINT8)Dev;
    DeviceInfo->SourceId.Bits.Function = (UINT8)Func;

    return EFI_SUCCESS;
  }

  //
  // Handle ACPI device
  //
  Status = gBS->HandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePath);
  if (!EFI_ERROR(Status)) {
    for (Index = 0; Index < ARRAY_SIZE(mAcpiDeviceMapping); Index++) {
      if (CompareDevicePath (mAcpiDeviceMapping[Index].DevicePath, DevicePath)) {
        DeviceInfo->Segment = mAcpiDeviceMapping[Index].Segment;
        DeviceInfo->SourceId = mAcpiDeviceMapping[Index].SourceId;
        return EFI_SUCCESS;
      }
    }
  }

  return EFI_NOT_FOUND;
}

/**
  Get a list of the exception devices.

  The VTd driver should always set ALLOW for the device in this list.

  @param[in]  This                  The protocol instance pointer.
  @param[out] DeviceInfoCount       The count of the list of DeviceInfo.
  @param[out] DeviceInfo            A callee allocated buffer to hold a list of DeviceInfo.
                                    Each DeviceInfo pointer points to EDKII_PLATFORM_VTD_EXCEPTION_DEVICE_INFO.

  @retval EFI_SUCCESS           The DeviceInfoCount and DeviceInfo are returned.
  @retval EFI_INVALID_PARAMETER DeviceInfoCount is NULL, or DeviceInfo is NULL.
  @retval EFI_UNSUPPORTED       This function is not supported.

**/
EFI_STATUS
EFIAPI
PlatformVTdGetExceptionDeviceList (
  IN  EDKII_PLATFORM_VTD_POLICY_PROTOCOL       *This,
  OUT UINTN                                    *DeviceInfoCount,
  OUT VOID                                     **DeviceInfo
  )
{
  DEBUG ((DEBUG_VERBOSE, "PlatformVTdGetExceptionDeviceList\n"));

  if (DeviceInfoCount == NULL || DeviceInfo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // Sample codes for device scope based exception list.
  // Uncomment to take affect and comment the sample codes for PCI vendor id
  // based exception list.
  //
  /*
  *DeviceInfo = AllocateZeroPool (sizeof(mExceptionDeviceScopeList));
  if (*DeviceInfo == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
  CopyMem (*DeviceInfo, mExceptionDeviceScopeList, sizeof(mExceptionDeviceScopeList));

  *DeviceInfoCount = ARRAY_SIZE(mExceptionDeviceScopeList);
  */

  //
  // Sample codes for PCI vendor id based exception list.
  // Uncomment to take affect and comment the sample codes for device scope
  // based exception list.
  //
  *DeviceInfo = AllocateZeroPool (sizeof(mExceptionPciDeviceIdList));
  if (*DeviceInfo == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
  CopyMem (*DeviceInfo, mExceptionPciDeviceIdList, sizeof(mExceptionPciDeviceIdList));

  *DeviceInfoCount = ARRAY_SIZE(mExceptionPciDeviceIdList);

  return EFI_SUCCESS;
}

EDKII_PLATFORM_VTD_POLICY_PROTOCOL  mPlatformVTdSample = {
  EDKII_PLATFORM_VTD_POLICY_PROTOCOL_REVISION,
  PlatformVTdGetDeviceId,
  PlatformVTdGetExceptionDeviceList,
};

/**
  Platform VTd sample driver.

  @param[in]  ImageHandle  ImageHandle of the loaded driver
  @param[in]  SystemTable  Pointer to the System Table

  @retval  EFI_SUCCESS           The Protocol is installed.
  @retval  EFI_OUT_OF_RESOURCES  Not enough resources available to initialize driver.
  @retval  EFI_DEVICE_ERROR      A device error occurred attempting to initialize the driver.

**/
EFI_STATUS
EFIAPI
PlatformVTdSampleInitialize (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS  Status;
  EFI_HANDLE  Handle;

  Handle = NULL;
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &Handle,
                  &gEdkiiPlatformVTdPolicyProtocolGuid, &mPlatformVTdSample,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);

  return Status;
}