1de9707f9SLawrence Tang /**
2*efe17e2cSLawrence Tang  * Functions for generating pseudo-random CPER PCIe error sections.
3de9707f9SLawrence Tang  *
4de9707f9SLawrence Tang  * Author: Lawrence.Tang@arm.com
5de9707f9SLawrence Tang  **/
6de9707f9SLawrence Tang 
7de9707f9SLawrence Tang #include <stdlib.h>
8de9707f9SLawrence Tang #include "../../edk/BaseTypes.h"
9de9707f9SLawrence Tang #include "../gen-utils.h"
10de9707f9SLawrence Tang #include "gen-sections.h"
11de9707f9SLawrence Tang 
12de9707f9SLawrence Tang #define PCIE_PORT_TYPES (int []){0, 1, 4, 5, 6, 7, 8, 9, 10}
13de9707f9SLawrence Tang 
14*efe17e2cSLawrence Tang //Generates a single pseudo-random PCIe error section, saving the resulting address to the given
15de9707f9SLawrence Tang //location. Returns the size of the newly created section.
16de9707f9SLawrence Tang size_t generate_section_pcie(void** location)
17de9707f9SLawrence Tang {
18de9707f9SLawrence Tang     //Create random bytes.
19de9707f9SLawrence Tang     int size = 208;
20de9707f9SLawrence Tang     UINT8* bytes = generate_random_bytes(size);
21de9707f9SLawrence Tang 
22de9707f9SLawrence Tang     //Set reserved areas to zero.
23de9707f9SLawrence Tang     UINT64* validation = (UINT64*)bytes;
24de9707f9SLawrence Tang     *validation &= 0xFF; //Validation 8-63
25de9707f9SLawrence Tang     UINT32* version = (UINT32*)(bytes + 12);
26de9707f9SLawrence Tang     *version &= 0xFFFF; //Version bytes 2-3
27de9707f9SLawrence Tang     UINT32* reserved = (UINT32*)(bytes + 20);
28de9707f9SLawrence Tang     *reserved = 0; //Reserved bytes 20-24
293ab351feSLawrence Tang     *(bytes + 37) &= ~0b111; //Device ID byte 13 bits 0-3
30de9707f9SLawrence Tang     *(bytes + 39) = 0; //Device ID byte 15
31de9707f9SLawrence Tang 
323ab351feSLawrence Tang     //Set expected values.
333ab351feSLawrence Tang     int minor = rand() % 128;
343ab351feSLawrence Tang     int major = rand() % 128;
353ab351feSLawrence Tang     *version = int_to_bcd(minor);
363ab351feSLawrence Tang     *version |= int_to_bcd(major) << 8;
373ab351feSLawrence Tang 
38de9707f9SLawrence Tang     //Fix values that could be above range.
39de9707f9SLawrence Tang     UINT32* port_type = (UINT32*)(bytes + 8);
40de9707f9SLawrence Tang     *port_type = PCIE_PORT_TYPES[rand() % (sizeof(PCIE_PORT_TYPES) / sizeof(int))];
41de9707f9SLawrence Tang 
42de9707f9SLawrence Tang     //Set return values, exit.
43de9707f9SLawrence Tang     *location = bytes;
44de9707f9SLawrence Tang     return size;
45de9707f9SLawrence Tang }