summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Bus/Pci/IncompatiblePciDeviceSupportDxe/IncompatiblePciDeviceSupport.c
blob: aae16cd856b7c8ca381db7976de8a5aed4098e02 (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
/** @file
  This module is one template module for Incompatible PCI Device Support protocol.
  It includes one incompatible pci devices list template.

  Incompatible PCI Device Support protocol allows the PCI bus driver to support
  resource allocation for some PCI devices that do not comply with the PCI Specification.

Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <PiDxe.h>
#include <Protocol/IncompatiblePciDeviceSupport.h>

#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DebugLib.h>

#include <IndustryStandard/Pci.h>
#include <IndustryStandard/Acpi.h>

typedef struct {
  UINT64    VendorId;
  UINT64    DeviceId;
  UINT64    RevisionId;
  UINT64    SubsystemVendorId;
  UINT64    SubsystemDeviceId;
} EFI_PCI_DEVICE_HEADER_INFO;

typedef struct {
  UINT64    ResType;
  UINT64    GenFlag;
  UINT64    SpecificFlag;
  UINT64    AddrSpaceGranularity;
  UINT64    AddrRangeMin;
  UINT64    AddrRangeMax;
  UINT64    AddrTranslationOffset;
  UINT64    AddrLen;
} EFI_PCI_RESOUCE_DESCRIPTOR;

#define PCI_DEVICE_ID(VendorId, DeviceId, Revision, SubVendorId, SubDeviceId) \
    VendorId, DeviceId, Revision, SubVendorId, SubDeviceId

#define DEVICE_INF_TAG  0xFFF2
#define DEVICE_RES_TAG  0xFFF1
#define LIST_END_TAG    0x0000

#define EVEN_ALIGN  0xFFFFFFFFFFFFFFFEULL

/**
  Returns a list of ACPI resource descriptors that detail the special
  resource configuration requirements for an incompatible PCI device.

  @param  This                  Pointer to the EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL instance.
  @param  VendorId              A unique ID to identify the manufacturer of the PCI device.
  @param  DeviceId              A unique ID to identify the particular PCI device.
  @param  RevisionId            A PCI device-specific revision identifier.
  @param  SubsystemVendorId     Specifies the subsystem vendor ID.
  @param  SubsystemDeviceId     Specifies the subsystem device ID.
  @param  Configuration         A list of ACPI resource descriptors returned that detail
                                the configuration requirement.

  @retval EFI_SUCCESS           Successfully got ACPI resource for specified PCI device.
  @retval EFI_INVALID_PARAMETER Configuration is NULL.
  @retval EFI_OUT_OF_RESOURCES  No memory available.
  @retval EFI_UNSUPPORTED       The specified PCI device wasn't supported.

**/
EFI_STATUS
EFIAPI
PCheckDevice (
  IN  EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL  *This,
  IN  UINTN                                         VendorId,
  IN  UINTN                                         DeviceId,
  IN  UINTN                                         RevisionId,
  IN  UINTN                                         SubsystemVendorId,
  IN  UINTN                                         SubsystemDeviceId,
  OUT VOID                                          **Configuration
  );

//
// Handle onto which the Incompatible PCI Device List is installed
//
EFI_HANDLE  mIncompatiblePciDeviceSupportHandle = NULL;

//
// The Incompatible PCI Device Support Protocol instance produced by this driver
//
EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL  mIncompatiblePciDeviceSupport = {
  PCheckDevice
};

//
// The incompatible PCI devices list template
//
GLOBAL_REMOVE_IF_UNREFERENCED UINT64  mIncompatiblePciDeviceList[] = {
  //
  // DEVICE_INF_TAG,
  // PCI_DEVICE_ID (VendorID, DeviceID, Revision, SubVendorId, SubDeviceId),
  // DEVICE_RES_TAG,
  // ResType,  GFlag , SFlag,   Granularity,  RangeMin,
  // RangeMax, Offset, AddrLen
  //
  //
  // Device Adaptec 9004
  //
  DEVICE_INF_TAG,
  PCI_DEVICE_ID (0x9004,     MAX_UINT64,  MAX_UINT64, MAX_UINT64, MAX_UINT64),
  DEVICE_RES_TAG,
  ACPI_ADDRESS_SPACE_TYPE_IO,
  0,
  0,
  0,
  0,
  EVEN_ALIGN,
  MAX_UINT64,
  0,
  //
  // Device Adaptec 9005
  //
  DEVICE_INF_TAG,
  PCI_DEVICE_ID (0x9005,     MAX_UINT64,  MAX_UINT64, MAX_UINT64, MAX_UINT64),
  DEVICE_RES_TAG,
  ACPI_ADDRESS_SPACE_TYPE_IO,
  0,
  0,
  0,
  0,
  EVEN_ALIGN,
  MAX_UINT64,
  0,
  //
  // Device QLogic  1007
  //
  DEVICE_INF_TAG,
  PCI_DEVICE_ID (0x1077,     MAX_UINT64,  MAX_UINT64, MAX_UINT64, MAX_UINT64),
  DEVICE_RES_TAG,
  ACPI_ADDRESS_SPACE_TYPE_IO,
  0,
  0,
  0,
  0,
  EVEN_ALIGN,
  MAX_UINT64,
  0,
  //
  // Device Agilent 103C
  //
  DEVICE_INF_TAG,
  PCI_DEVICE_ID (0x103C,     MAX_UINT64,  MAX_UINT64, MAX_UINT64, MAX_UINT64),
  DEVICE_RES_TAG,
  ACPI_ADDRESS_SPACE_TYPE_IO,
  0,
  0,
  0,
  0,
  EVEN_ALIGN,
  MAX_UINT64,
  0,
  //
  // Device Agilent 15BC
  //
  DEVICE_INF_TAG,
  PCI_DEVICE_ID (0x15BC,     MAX_UINT64,  MAX_UINT64, MAX_UINT64, MAX_UINT64),
  DEVICE_RES_TAG,
  ACPI_ADDRESS_SPACE_TYPE_IO,
  0,
  0,
  0,
  0,
  EVEN_ALIGN,
  MAX_UINT64,
  0,
  //
  // The end of the list
  //
  LIST_END_TAG
};

/**
  Entry point of the incompatible pci device support code. Setup an incompatible device list template
  and install EFI Incompatible PCI Device Support protocol.

  @param ImageHandle             A handle for the image that is initializing this driver.
  @param SystemTable             A pointer to the EFI system table.

  @retval EFI_SUCCESS            Installed EFI Incompatible PCI Device Support Protocol successfully.
  @retval others                 Failed to install protocol.

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

  //
  // Install EFI Incompatible PCI Device Support Protocol on a new handle
  //
  Status = gBS->InstallProtocolInterface (
                  &mIncompatiblePciDeviceSupportHandle,
                  &gEfiIncompatiblePciDeviceSupportProtocolGuid,
                  EFI_NATIVE_INTERFACE,
                  &mIncompatiblePciDeviceSupport
                  );
  ASSERT_EFI_ERROR (Status);

  return Status;
}

/**
  Returns a list of ACPI resource descriptors that detail the special
  resource configuration requirements for an incompatible PCI device.

  @param  This                  Pointer to the EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL instance.
  @param  VendorId              A unique ID to identify the manufacturer of the PCI device.
  @param  DeviceId              A unique ID to identify the particular PCI device.
  @param  RevisionId            A PCI device-specific revision identifier.
  @param  SubsystemVendorId     Specifies the subsystem vendor ID.
  @param  SubsystemDeviceId     Specifies the subsystem device ID.
  @param  Configuration         A list of ACPI resource descriptors returned that detail
                                the configuration requirement.

  @retval EFI_SUCCESS           Successfully got ACPI resource for specified PCI device.
  @retval EFI_INVALID_PARAMETER Configuration is NULL.
  @retval EFI_OUT_OF_RESOURCES  No memory available.
  @retval EFI_UNSUPPORTED       The specified PCI device wasn't supported.

**/
EFI_STATUS
EFIAPI
PCheckDevice (
  IN  EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL  *This,
  IN  UINTN                                         VendorId,
  IN  UINTN                                         DeviceId,
  IN  UINTN                                         RevisionId,
  IN  UINTN                                         SubsystemVendorId,
  IN  UINTN                                         SubsystemDeviceId,
  OUT VOID                                          **Configuration
  )
{
  UINT64                             Tag;
  UINT64                             *ListPtr;
  UINT64                             *TempListPtr;
  EFI_PCI_DEVICE_HEADER_INFO         *Header;
  EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR  *AcpiPtr;
  EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR  *OldAcpiPtr;
  EFI_PCI_RESOUCE_DESCRIPTOR         *Dsc;
  EFI_ACPI_END_TAG_DESCRIPTOR        *PtrEnd;
  UINTN                              Index;

  //
  // Validate the parameters
  //
  if (Configuration == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // Initialize the return value to NULL
  //
  *(VOID **)Configuration = NULL;

  ListPtr = mIncompatiblePciDeviceList;
  while (*ListPtr != LIST_END_TAG) {
    Tag = *ListPtr;

    switch (Tag) {
      case DEVICE_INF_TAG:
        Header  = (EFI_PCI_DEVICE_HEADER_INFO *)(ListPtr + 1);
        ListPtr = ListPtr + 1 + sizeof (EFI_PCI_DEVICE_HEADER_INFO) / sizeof (UINT64);
        //
        // See if the Header matches the parameters passed in
        //
        if ((Header->VendorId != MAX_UINT64) && (VendorId != MAX_UINTN)) {
          if (Header->VendorId != VendorId) {
            continue;
          }
        }

        if ((Header->DeviceId != MAX_UINT64) && (DeviceId != MAX_UINTN)) {
          if (DeviceId != Header->DeviceId) {
            continue;
          }
        }

        if ((Header->RevisionId != MAX_UINT64) && (RevisionId != MAX_UINTN)) {
          if (RevisionId != Header->RevisionId) {
            continue;
          }
        }

        if ((Header->SubsystemVendorId != MAX_UINT64) && (SubsystemVendorId != MAX_UINTN)) {
          if (SubsystemVendorId != Header->SubsystemVendorId) {
            continue;
          }
        }

        if ((Header->SubsystemDeviceId != MAX_UINT64) && (SubsystemDeviceId != MAX_UINTN)) {
          if (SubsystemDeviceId != Header->SubsystemDeviceId) {
            continue;
          }
        }

        //
        // Matched an item, so construct the ACPI descriptor for the resource.
        //
        //
        // Count the resource items so that to allocate space
        //
        for (Index = 0, TempListPtr = ListPtr; *TempListPtr == DEVICE_RES_TAG; Index++) {
          TempListPtr = TempListPtr + 1 + ((sizeof (EFI_PCI_RESOUCE_DESCRIPTOR)) / sizeof (UINT64));
        }

        //
        // If there is at least one type of resource request,
        // allocate an acpi resource node
        //
        if (Index == 0) {
          return EFI_UNSUPPORTED;
        }

        AcpiPtr = AllocateZeroPool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) * Index + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR));
        if (AcpiPtr == NULL) {
          return EFI_OUT_OF_RESOURCES;
        }

        OldAcpiPtr = AcpiPtr;
        //
        // Fill the EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR structure
        // according to the EFI_PCI_RESOUCE_DESCRIPTOR structure
        //
        for ( ; *ListPtr == DEVICE_RES_TAG;) {
          Dsc = (EFI_PCI_RESOUCE_DESCRIPTOR *)(ListPtr + 1);

          AcpiPtr->Desc                  = ACPI_ADDRESS_SPACE_DESCRIPTOR;
          AcpiPtr->Len                   = (UINT16)sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3;
          AcpiPtr->ResType               = (UINT8)Dsc->ResType;
          AcpiPtr->GenFlag               = (UINT8)Dsc->GenFlag;
          AcpiPtr->SpecificFlag          = (UINT8)Dsc->SpecificFlag;
          AcpiPtr->AddrSpaceGranularity  = Dsc->AddrSpaceGranularity;
          AcpiPtr->AddrRangeMin          = Dsc->AddrRangeMin;
          AcpiPtr->AddrRangeMax          = Dsc->AddrRangeMax;
          AcpiPtr->AddrTranslationOffset = Dsc->AddrTranslationOffset;
          AcpiPtr->AddrLen               = Dsc->AddrLen;

          ListPtr = ListPtr + 1 + ((sizeof (EFI_PCI_RESOUCE_DESCRIPTOR)) / sizeof (UINT64));
          AcpiPtr++;
        }

        //
        // Put the checksum
        //
        PtrEnd           = (EFI_ACPI_END_TAG_DESCRIPTOR *)(AcpiPtr);
        PtrEnd->Desc     = ACPI_END_TAG_DESCRIPTOR;
        PtrEnd->Checksum = 0;

        *(VOID **)Configuration = OldAcpiPtr;

        return EFI_SUCCESS;

      case DEVICE_RES_TAG:
        //
        // Adjust the pointer to the next PCI resource descriptor item
        //
        ListPtr = ListPtr + 1 + ((sizeof (EFI_PCI_RESOUCE_DESCRIPTOR)) / sizeof (UINT64));
        break;

      default:
        return EFI_UNSUPPORTED;
    }
  }

  return EFI_UNSUPPORTED;
}