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
|
#include <stdlib.h>
#include <error.h>
#include <string.h>
#include <pciaccess.h>
#include "ddekit/assert.h"
#include "ddekit/printf.h"
#include "ddekit/pci.h"
#include "config.h"
#define dbg_this 0
/** PCI descriptor */
typedef struct ddekit_pci_dev {
struct pci_device *dev;
} ddekit_pci_dev_t;
typedef struct ddekit_pci_bus {
struct {
ddekit_pci_dev_t funs[8];
} devs[32];
} ddekit_pci_bus_t;
#define MAX_NUM_BUSES 256
static int num_pci_devs;
static ddekit_pci_dev_t *ddekit_pci_devs;
static ddekit_pci_bus_t *ddekit_pci_buses[MAX_NUM_BUSES];
static inline int invalid_device(ddekit_pci_dev_t *d)
{
return d->dev == NULL;
}
/** Initialize DDEKit PCI module.
*
* \ingroup DDEKit_pci
*
* This function builds a list of devices by querying L4IO.
*/
void ddekit_pci_init(void)
{
int slots_found = 0;
int err;
struct pci_device *pci_dev;
struct pci_device_iterator *dev_iter;
err = pci_system_init ();
if (err)
error (2, err, "pci_system_init");
num_pci_devs = 32;
ddekit_pci_devs = malloc (sizeof (ddekit_pci_devs[0]) * num_pci_devs);
memset (ddekit_pci_devs, 0,
sizeof (ddekit_pci_devs[0]) * num_pci_devs);
dev_iter = pci_slot_match_iterator_create (NULL);
while ((pci_dev = pci_device_next (dev_iter)) != NULL) {
/* store the device in the structured array. */
if (ddekit_pci_buses[pci_dev->bus] == NULL) {
ddekit_pci_buses[pci_dev->bus] = calloc (1, sizeof (**ddekit_pci_buses));
assert(ddekit_pci_buses[pci_dev->bus] != NULL);
}
ddekit_pci_buses[pci_dev->bus]->devs[pci_dev->dev].funs[pci_dev->func].dev = pci_dev;
/* We also put the device in the flat array.
* The array is increased dynamically. */
if (slots_found >= num_pci_devs) {
num_pci_devs *= 2;
ddekit_pci_devs = realloc (ddekit_pci_devs,
sizeof (ddekit_pci_devs[0])
* num_pci_devs);
assert (ddekit_pci_devs != NULL);
memset (&ddekit_pci_devs[slots_found], 0,
(num_pci_devs - slots_found)
* sizeof (ddekit_pci_devs[0]));
}
ddekit_pci_devs[slots_found].dev = pci_dev;
++slots_found;
}
num_pci_devs = slots_found;
pci_iterator_destroy (dev_iter);
}
/**
* Get PCI device (bus, slot, func) for handle.
*
* \ingroup DDEKit_pci
*/
int ddekit_pci_get_device(int nr, int *bus, int *slot, int *func)
{
ddekit_pci_dev_t *dev;
ddekit_printf ("searching for dev #%d", nr);
if (nr >= 0 && nr < num_pci_devs
&& !invalid_device(&ddekit_pci_devs[nr])) {
dev = &ddekit_pci_devs[nr];
*bus = dev->dev->bus;
*slot = dev->dev->dev;
*func = dev->dev->func;
return 0;
}
return -1;
}
ddekit_pci_dev_t *ddekit_pci_find_device(int *bus, int *slot, int *func,
ddekit_pci_dev_t *start)
{
Assert(bus);
Assert(slot);
Assert(func);
int idx = start ? start->dev->dev + 1 : 0;
for ( ; idx < num_pci_devs; ++idx) {
ddekit_pci_dev_t *dev = &ddekit_pci_devs[idx];
if (!invalid_device(dev) &&
(*bus == DDEKIT_PCI_ANY_ID || dev->dev->bus == *bus) &&
(*slot == DDEKIT_PCI_ANY_ID || dev->dev->dev == *slot) &&
(*func == DDEKIT_PCI_ANY_ID || dev->dev->func == *func)) {
*bus = dev->dev->bus;
*slot = dev->dev->dev;
*func = dev->dev->func;
return dev;
}
}
return NULL;
}
int ddekit_pci_read(int bus, int slot, int func, int pos, int len, ddekit_uint32_t *val)
{
switch(len)
{
case 1:
return ddekit_pci_readb(bus, slot, func, pos, (ddekit_uint8_t *)val);
case 2:
return ddekit_pci_readw(bus, slot, func, pos, (ddekit_uint16_t *)val);
case 4:
return ddekit_pci_readl(bus, slot, func, pos, val);
}
return -1;
}
int ddekit_pci_write(int bus, int slot, int func, int pos, int len, ddekit_uint32_t val)
{
switch(len)
{
case 1:
return ddekit_pci_writeb(bus, slot, func, pos, val);
case 2:
return ddekit_pci_writew(bus, slot, func, pos, val);
case 4:
return ddekit_pci_writel(bus, slot, func, pos, val);
}
return -1;
}
/** Find PCI device for a given (bus, slot, func) tuple.
*
* \ingroup DDEKit_pci
*
* This function is only used internally by the pci_{read,write}{b,l,w} functions.
* It was once called pci_find_device, but this caused confusion with the "real"
* find_device() functionality provided by L4IO and the Linux PCI subsystem.
*/
static inline ddekit_pci_dev_t *ddekit_pci_find_device_fixed(int bus, int slot, int func)
{
ddekit_pci_dev_t *dev;
/* If the bus doesn't exist. */
if (ddekit_pci_buses[bus] == NULL)
return NULL;
dev = &ddekit_pci_buses[bus]->devs[slot].funs[func];
if (dev && dev->dev)
return dev;
return NULL;
}
int ddekit_pci_readb (int bus, int slot, int func, int pos, ddekit_uint8_t *val)
{
ddekit_pci_dev_t *dev = ddekit_pci_find_device_fixed(bus, slot, func);
if (dev)
return pci_device_cfg_read_u8 (dev->dev, val, pos);
else
return -1;
}
int ddekit_pci_readw (int bus, int slot, int func, int pos, ddekit_uint16_t *val)
{
ddekit_pci_dev_t *dev = ddekit_pci_find_device_fixed(bus, slot, func);
if (dev)
return pci_device_cfg_read_u16 (dev->dev, val, pos);
else
return -1;
}
int ddekit_pci_readl (int bus, int slot, int func, int pos, ddekit_uint32_t *val)
{
ddekit_pci_dev_t *dev = ddekit_pci_find_device_fixed(bus, slot, func);
if (dev) {
assert (dev->dev != NULL);
return pci_device_cfg_read_u32 (dev->dev, val, pos);
}
else
return -1;
}
int ddekit_pci_writeb(int bus, int slot, int func, int pos, ddekit_uint8_t val)
{
ddekit_pci_dev_t *dev = ddekit_pci_find_device_fixed(bus, slot, func);
if (dev)
return pci_device_cfg_write_u8 (dev->dev, val, pos);
else
return -1;
}
int ddekit_pci_writew(int bus, int slot, int func, int pos, ddekit_uint16_t val)
{
ddekit_pci_dev_t *dev = ddekit_pci_find_device_fixed(bus, slot, func);
if (dev)
return pci_device_cfg_write_u16 (dev->dev, val, pos);
else
return -1;
}
int ddekit_pci_writel(int bus, int slot, int func, int pos, ddekit_uint32_t val)
{
ddekit_pci_dev_t *dev = ddekit_pci_find_device_fixed(bus, slot, func);
if (dev)
return pci_device_cfg_write_u32 (dev->dev, val, pos);
else
return -1;
}
int ddekit_pci_enable_device(struct ddekit_pci_dev *dev)
{
pci_device_enable (dev->dev);
return 0;
}
int ddekit_pci_disable_device(struct ddekit_pci_dev *dev)
{
// TODO
return -1;
}
/********************************************************************************
** Accessor functions for the various kinds of information that are available **
** from struct l4io_pci_dev (which is not handed through to the DDEs, because **
** it still contains L4 types). **
********************************************************************************/
/**
* Get PCI device vendor ID.
*
* \param dev device
*
* \ingroup DDEKit_pci
*/
unsigned short ddekit_pci_get_vendor(struct ddekit_pci_dev *dev)
{
return dev->dev->vendor_id;
}
/**
* Get PCI device ID.
*
* \param dev device
*
* \ingroup DDEKit_pci
*/
unsigned short ddekit_pci_get_device_id(struct ddekit_pci_dev *dev)
{
return dev->dev->device_id;
}
/**
* Get PCI device subvendor ID.
*
* \param dev device
*
* \ingroup DDEKit_pci
*/
unsigned short ddekit_pci_get_sub_vendor(struct ddekit_pci_dev *dev)
{
return dev->dev->subvendor_id;
}
/**
* Get PCI device subdevice ID.
*
* \param dev device
*
* \ingroup DDEKit_pci
*/
unsigned short ddekit_pci_get_sub_device(struct ddekit_pci_dev *dev)
{
return dev->dev->subdevice_id;
}
/**
* Get PCI device class ID.
*
* \param dev device
*
* \ingroup DDEKit_pci
*/
unsigned ddekit_pci_get_dev_class(struct ddekit_pci_dev *dev)
{
return dev->dev->device_class;
}
/**
* Get PCI device IRQ number.
*
* \param dev device
*
* \ingroup DDEKit_pci
*/
unsigned long ddekit_pci_get_irq(struct ddekit_pci_dev *dev)
{
return dev->dev->irq;
}
/**
* Get PCI device name.
*
* \param dev device
*
* \ingroup DDEKit_pci
*/
char *ddekit_pci_get_name(struct ddekit_pci_dev *dev)
{
// TODO
return NULL;
}
/**
* Get PCI device slot name.
*
* \param dev device
*
* \ingroup DDEKit_pci
*/
char *ddekit_pci_get_slot_name(struct ddekit_pci_dev *dev)
{
// TODO
return NULL;
}
/**
* Get PCI device resource at index.
*
* \param dev device
* \param idx resource index
*
* \ingroup DDEKit_pci
*/
ddekit_pci_res_t *ddekit_pci_get_resource(struct ddekit_pci_dev *dev, unsigned int idx)
{
// TODO
// if (idx > L4IO_PCIDEV_RES)
return NULL;
// return (ddekit_pci_res_t *)(&(dev->l4dev.res[idx]));
}
/**
* Set PCI bus mastering for device.
*
* \ingroup DDEKit_pci
*/
void ddekit_pci_set_master(struct ddekit_pci_dev *dev)
{
//TODO l4io_pci_set_master(dev->l4dev.handle);
}
int ddekit_pci_irq_enable(int bus, int slot, int func, int pin, int *irq)
{
return 0;
}
|