summaryrefslogtreecommitdiff
path: root/libddekit/pci.c
blob: 35fb7aeaa05b74ea0ac7a3e9ba23d0a09411d63d (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
#include <l4/dde/ddekit/pci.h>
#include <l4/dde/ddekit/printf.h>
#include <l4/dde/ddekit/panic.h>
#include <l4/dde/ddekit/memory.h>
#include <l4/dde/ddekit/assert.h>

#include <l4/generic_io/types.h>
#include <l4/generic_io/libio.h>
#include <l4/env/errno.h>
#include <l4/log/l4log.h>

#include "config.h"

#define dbg_this 0

#define MAX_PCI_DEVS 32

/** PCI descriptor */
typedef struct ddekit_pci_dev {
	int bus;                      /**< bus ID */
	int slot;                     /**< slot ID */
	int func;                     /**< function */
	l4io_pci_dev_t l4dev;         /**< L4IO handle */
	struct ddekit_pci_dev *next;  /**< chaining info */
} ddekit_pci_dev_t;

static ddekit_pci_dev_t ddekit_pci_bus[MAX_PCI_DEVS];

static inline int invalid_device(ddekit_pci_dev_t *d)
{
	return d->slot == -1;
}


/** Initialize DDEKit PCI module.
 *
 * \ingroup DDEKit_pci
 *
 * This function builds a list of devices by querying L4IO.
 */
void ddekit_pci_init(void)
{
	l4io_pdev_t start = 0;

	int slots_found = 0;
	int i;

	/* Init device list */
	for (i = 0; i < MAX_PCI_DEVS; i++)
		ddekit_pci_bus[i].slot = -1;

	while (1) {
		l4io_pci_dev_t l4dev;
		int err;

		/* search next device */
		err = l4io_pci_find_device(~0, ~0, start, &l4dev);
		if (err) {
			if (err == -L4_ENOTFOUND) {
				LOGd(dbg_this, "no more pci devices");
			} else {
				LOGd(dbg_this, "error: scanning pci devices: %s (%d)", l4env_errstr(err), err);
			}
			break;
		}

		/* next search start from here */
		start = l4dev.handle;

		/* print info */

		/* Pretend all our devices are chained to exactly one bus. */
		ddekit_pci_bus[slots_found].bus   = 0; /*l4dev.bus;*/
		ddekit_pci_bus[slots_found].slot  = slots_found;
		ddekit_pci_bus[slots_found].func  = 0;
		ddekit_pci_bus[slots_found].l4dev = l4dev;

		LOGd(dbg_this, "pcib_identify: found device (%x, %x, %x), mapped to (%x, %x, %x)",
		     l4dev.bus, l4dev.devfn >> 3, l4dev.devfn & 0x07,
		     ddekit_pci_bus[slots_found].bus,
		     ddekit_pci_bus[slots_found].slot,
		     ddekit_pci_bus[slots_found].func);

		++slots_found;
	}
}


/**
 * 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;

	LOGd(dbg_this, "searching for dev #%d", nr);

	if (nr >= 0 && nr < MAX_PCI_DEVS && !invalid_device(&ddekit_pci_bus[nr])) {
		dev = &ddekit_pci_bus[nr];
		*bus = dev->bus;
		*slot = dev->slot;
		*func = 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);

	LOGd(dbg_this, "start %p (slot %d)", start, start ? start->slot : -1);
	int idx = start ? start->slot + 1 : 0;
	
	for ( ; idx < MAX_PCI_DEVS; ++idx) {
	    ddekit_pci_dev_t *dev = &ddekit_pci_bus[idx];
		if (!invalid_device(dev) &&
			(*bus == DDEKIT_PCI_ANY_ID || dev->bus == *bus) &&
		    (*slot == DDEKIT_PCI_ANY_ID || dev->slot == *slot) &&
		    (*func == DDEKIT_PCI_ANY_ID || dev->func == *func)) {
			*bus = dev->bus;
			*slot = dev->slot;
			*func = 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)
{
	return &ddekit_pci_bus[slot];
}


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 l4io_pci_readb_cfg(dev->l4dev.handle, pos, val);
	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 l4io_pci_readw_cfg(dev->l4dev.handle, pos, val);
	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)
		return l4io_pci_readl_cfg(dev->l4dev.handle, pos, val);
	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 l4io_pci_writeb_cfg(dev->l4dev.handle, pos, val);
	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 l4io_pci_writew_cfg(dev->l4dev.handle, pos, val);
	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 l4io_pci_writel_cfg(dev->l4dev.handle, pos, val);
	else
		return -1;
}

int ddekit_pci_enable_device(struct ddekit_pci_dev *dev)
{
	return l4io_pci_enable(dev->l4dev.handle);
}

int ddekit_pci_disable_device(struct ddekit_pci_dev *dev)
{
	return l4io_pci_disable(dev->l4dev.handle);
}

/********************************************************************************
 ** 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->l4dev.vendor;
}


/**
 * Get PCI device ID.
 *
 * \param dev     device
 *
 * \ingroup DDEKit_pci
 */
unsigned short ddekit_pci_get_device_id(struct ddekit_pci_dev *dev)
{
	return dev->l4dev.device;
}


/**
 * 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->l4dev.sub_vendor;
}


/**
 * 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->l4dev.sub_device;
}


/**
 * Get PCI device class ID.
 *
 * \param dev     device
 *
 * \ingroup DDEKit_pci
 */
unsigned       ddekit_pci_get_dev_class(struct ddekit_pci_dev *dev)
{
	return dev->l4dev.dev_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->l4dev.irq;
}


/**
 * Get PCI device name.
 *
 * \param dev     device
 *
 * \ingroup DDEKit_pci
 */
char *ddekit_pci_get_name(struct ddekit_pci_dev *dev)
{
	return dev->l4dev.name;
}


/**
 * Get PCI device slot name.
 *
 * \param dev     device
 *
 * \ingroup DDEKit_pci
 */
char *ddekit_pci_get_slot_name(struct ddekit_pci_dev *dev)
{
	return dev->l4dev.slot_name;
}


/**
 * 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)
{
	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)
{
	l4io_pci_set_master(dev->l4dev.handle);
}

int ddekit_pci_irq_enable(int bus, int slot, int func, int pin, int *irq)
{
	return 0;
}