summaryrefslogtreecommitdiffstats
path: root/simulator/HAL.cpp
blob: 77349bac061bf926ff7aef9da3f9ab837fc6165f (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
#include "../libs/NVRam/bcm5719_NVM.h"
#include "pci_config.h"

#include <bcm5719_DEVICE.h>
#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define DEVICE_CONFIG   "config"
#define BAR_STR         "resource%d"

#define ARRAY_ELEMENTS(__x__) (sizeof(__x__) / sizeof(__x__[0]))

typedef struct
{
    uint16_t vendor_id;
    uint16_t device_id;
} devices_t;

devices_t gSupportedDevices[] = {
    {.vendor_id = 0x14e4, .device_id = 0x1657},
};

bool is_supported(uint16_t vendor_id, uint16_t device_id)
{
    for (unsigned int i = 0; i < ARRAY_ELEMENTS(gSupportedDevices); i++)
    {
        devices_t *pDevice = &gSupportedDevices[i];
        if (vendor_id == pDevice->vendor_id && device_id == pDevice->device_id)
        {
            return true;
        }
    }

    return false;
}

#define BAR_REGION_MASK (0x1)
#define BAR_TYPE_MASK   (0x6)
#define BAR_TYPE_16BIT  (1u << 1)
#define BAR_TYPE_32BIT  (0u << 1)
#define BAR_TYPE_64BIT  (2u << 1)
#define BAR_ADDR_MASK   (0xFFFFFF0)
bool is_bar_64bit(uint32_t bar)
{
    return (BAR_TYPE_64BIT == (bar & BAR_TYPE_MASK));
}

uint32_t get_bar_addr(uint32_t bar)
{
    return (bar & BAR_ADDR_MASK);
}

#define MAX_NUM_BARS 8
uint8_t *bar[MAX_NUM_BARS] = {0};

uint32_t read_device_chipid(uint32_t)
{
    printf("DEIVCE CHIP ID\n");
    return 1123;
}

void initHAL(const char *pci_path)
{
    struct stat st;
    int memfd;
    char *pConfigPath =
        (char *)malloc(strlen("%s/" DEVICE_CONFIG) + strlen(pci_path) + 1);
    sprintf(pConfigPath, "%s/" DEVICE_CONFIG, pci_path);

    FILE *pConfigFile = fopen(pConfigPath, "rb");

    pci_config_t config;

    if (fread(&config, sizeof(config), 1, pConfigFile))
    {
        if (is_supported(config.vendor_id, config.device_id))
        {
            printf("Found supported device %x:%x at %s\n", config.vendor_id,
                   config.device_id, pConfigPath);
            for (unsigned int i = 0; i < ARRAY_ELEMENTS(config.BAR); i++)
            {
                char *pBARPath = (char *)malloc(strlen("%s/" BAR_STR) +
                                                strlen(pci_path) + 1);
                sprintf(pBARPath, "%s/" BAR_STR, pci_path, i);

                if ((memfd = open(pBARPath, O_RDWR | O_SYNC)) < 0)
                {
                    printf("Error opening %s file. \n", pBARPath);
                    close(memfd);
                    exit(-1);
                }
                else
                {
                    printf("mmaping BAR[%d]: %s\n", i, pBARPath);
                }

                if (fstat(memfd, &st) < 0)
                {
                    fprintf(stderr, "error: couldn't stat file\n");
                    exit(-1);
                }

                bar[i] = (uint8_t *)mmap(0, st.st_size, PROT_READ | PROT_WRITE,
                                         MAP_SHARED, memfd, 0); // PROT_WRITE
                if (bar[i] == MAP_FAILED)
                {
                    printf("Unable to mmap %s: %s\n", pBARPath,
                           strerror(errno));
                    exit(-1);
                }

                if (is_bar_64bit(config.BAR[i]))
                {
                    i++;
                }

                free(pBARPath);
            }

            free(pConfigPath);
        }
    }

    uint8_t *DEVICEBase = (uint8_t *)bar[0];

    extern void init_bcm5719_DEVICE(void);
    extern void init_bcm5719_DEVICE_mmap(void *);
    extern void init_bcm5719_APE(void);
    extern void init_bcm5719_NVM(void);
    extern void init_bcm5719_NVM_mmap(void *);
    init_bcm5719_DEVICE();
    init_bcm5719_DEVICE_mmap(DEVICEBase);
    init_bcm5719_APE();
    // init_bcm5719_APE_mmap();
    init_bcm5719_NVM();
    init_bcm5719_NVM_mmap(DEVICEBase + 0x7000);
}
OpenPOWER on IntegriCloud