summaryrefslogtreecommitdiffstats
path: root/cpukit/libpci/pci_bus.c
blob: 5c470a39ed56be0cc56de9ca9880c62c1f66175e (plain) (blame)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* SPDX-License-Identifier: BSD-2-Clause */

/*  PCI bus driver.
 *
 *  COPYRIGHT (c) 2008 Cobham Gaisler AB.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/* General part of PCI Bus driver. The driver is typically
 * initialized from the PCI host driver separating the host
 * driver from the common parts in PCI drivers.
 * The PCI library must be initialized before starting the
 * PCI bus driver. The PCI library have set up BARs and
 * assigned system IRQs for targets.
 * This PCI bus driver rely on the PCI library (pci.c) for
 * interrupt registeration (pci_interrupt_register) and PCI
 * target set up.
 */

/* Use PCI Configuration libarary pci_hb RAM device structure to find devices,
 * undefine to access PCI configuration space directly.
 */
#define USE_PCI_CFG_LIB

/* On small systems undefine PCIBUS_INFO to avoid sprintf get dragged in */
#define PCIBUS_INFO

#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <pci.h>
#ifdef USE_PCI_CFG_LIB
#include <pci/cfg.h>
#endif
#include <pci/irq.h>

#include <drvmgr/drvmgr.h>
#include <drvmgr/pci_bus.h>

#ifdef DEBUG 
#define DBG(args...) printk(args)
#else
#define DBG(args...)
#endif

int pcibus_bus_init1(struct drvmgr_bus *bus);
int pcibus_unite(struct drvmgr_drv *drv, struct drvmgr_dev *dev);
int pcibus_int_register(
	struct drvmgr_dev *dev,
	int index,
	const char *info,
	drvmgr_isr isr,
	void *arg);
int pcibus_int_unregister(
	struct drvmgr_dev *dev,
	int index,
	drvmgr_isr isr,
	void *arg);
int pcibus_int_clear(
	struct drvmgr_dev *dev,
	int index);
static int pcibus_get_freq(
	struct drvmgr_dev *dev,
	int options,
	unsigned int *freq_hz);

int pcibus_get_params(struct drvmgr_dev *dev, struct drvmgr_bus_params *params);

void pcibus_dev_info(
	struct drvmgr_dev *dev,
	void (*print_line)(void *p, char *str),
	void *p);

struct drvmgr_bus_ops pcibus_ops = {
	.init = {
		pcibus_bus_init1,
		NULL,
		NULL,
		NULL
	},
	.remove		= NULL,
	.unite		= pcibus_unite,
	.int_register	= pcibus_int_register,
	.int_unregister	= pcibus_int_unregister,
#if 0
	.int_enable	= pcibus_int_enable,
	.int_disable	= pcibus_int_disable,
#endif
	.int_clear	= pcibus_int_clear,
	.int_mask	= NULL,
	.int_unmask	= NULL,
	.get_params	= pcibus_get_params,
	.get_freq	= pcibus_get_freq,
#ifdef PCIBUS_INFO
	.get_info_dev	= pcibus_dev_info,
#endif
};

struct drvmgr_func pcibus_funcs[] = {
	DRVMGR_FUNC(PCI_FUNC_MREG_R8, NULL),
	DRVMGR_FUNC(PCI_FUNC_MREG_R16, NULL),
	DRVMGR_FUNC(PCI_FUNC_MREG_R32, NULL),
	DRVMGR_FUNC(PCI_FUNC_MREG_W8, NULL),
	DRVMGR_FUNC(PCI_FUNC_MREG_W16, NULL),
	DRVMGR_FUNC(PCI_FUNC_MREG_W32, NULL),
	DRVMGR_FUNC_END
};

/* Driver resources configuration for the PCI bus. It is declared weak so that
 * the user may override it from the project file, if the default settings are
 * not enough.
 */
struct drvmgr_bus_res pcibus_drv_resources __attribute__((weak)) = {
	.next = NULL,
	.resource = {
		DRVMGR_RES_EMPTY,
	},
};

struct pcibus_priv {
	struct drvmgr_dev	*dev;
};

static int compatible(struct pci_dev_id *id, struct pci_dev_id_match *drv)
{
	if (((drv->vendor==PCI_ID_ANY) || (id->vendor==drv->vendor)) &&
	    ((drv->device==PCI_ID_ANY) || (id->device==drv->device)) &&
	    ((drv->subvendor==PCI_ID_ANY) || (id->subvendor==drv->subvendor)) &&
	    ((drv->subdevice==PCI_ID_ANY) || (id->subdevice==drv->subdevice)) &&
	    ((id->class & drv->class_mask) == drv->class))
		return 1;
	else
		return 0;
}

int pcibus_unite(struct drvmgr_drv *drv,
			struct drvmgr_dev *dev)
{
	struct pci_drv_info *pdrv;
	struct pci_dev_id_match *drvid;
	struct pci_dev_info *pci;

	if (!drv || !dev || !dev->parent)
		return 0;

	if ((drv->bus_type != DRVMGR_BUS_TYPE_PCI) ||
	     (dev->parent->bus_type != DRVMGR_BUS_TYPE_PCI))
		return 0;

	pci = (struct pci_dev_info *)dev->businfo;
	if (!pci)
		return 0;

	pdrv = (struct pci_drv_info *)drv;
	drvid = pdrv->ids;
	if (!drvid)
		return 0;
	while (drvid->vendor != 0) {
		if (compatible(&pci->id, drvid)) {
			/* Unite device and driver */
			DBG("DRV %p and DEV %p united\n", drv, dev);
			return 1;
		}
		drvid++;
	}

	return 0;
}

static int pcibus_int_get(struct drvmgr_dev *dev, int index)
{
	int irq;

	/* Relative (positive) or absolute (negative) IRQ number */
	if (index > 0) {
		/* PCI devices only have one IRQ per function */
		return -1;
	} else if (index == 0) {
		/* IRQ Index relative to Cores base IRQ */

		/* Get Base IRQ */
		irq = ((struct pci_dev_info *)dev->businfo)->irq;
		if (irq <= 0)
			return -1;
	} else {
		/* Absolute IRQ number */
		irq = -index;
	}
	return irq;
}

/* Use standard PCI facility to register interrupt handler */
int pcibus_int_register(
	struct drvmgr_dev *dev,
	int index,
	const char *info,
	drvmgr_isr isr,
	void *arg)
{
#ifdef DEBUG
	struct drvmgr_dev *busdev = dev->parent->dev;
#endif
	int irq;

	/* Get IRQ number from index and device information */
	irq = pcibus_int_get(dev, index);
	if (irq < 0)
		return -1;

	DBG("Register PCI interrupt on %p for dev %p (IRQ: %d)\n",
		busdev, dev, irq);

	return pci_interrupt_register(irq, info, isr, arg);
}

/* Use standard PCI facility to unregister interrupt handler */
int pcibus_int_unregister(
	struct drvmgr_dev *dev,
	int index,
	drvmgr_isr isr,
	void *arg)
{
#ifdef DEBUG
	struct drvmgr_dev *busdev = dev->parent->dev;
#endif
	int irq;

	/* Get IRQ number from index and device information */
	irq = pcibus_int_get(dev, index);
	if (irq < 0)
		return -1;

	DBG("Unregister PCI interrupt on %p for dev %p (IRQ: %d)\n",
		busdev, dev, irq);

	return pci_interrupt_unregister(irq, isr, arg);
}

/* Use standard PCI facility to clear interrupt */
int pcibus_int_clear(
	struct drvmgr_dev *dev,
	int index)
{
	int irq;

	/* Get IRQ number from index and device information */
	irq = pcibus_int_get(dev, index);
	if (irq < 0)
		return -1;

	pci_interrupt_clear(irq);

	return 0;
}

static int pcibus_get_freq(
	struct drvmgr_dev *dev,
	int options,
	unsigned int *freq_hz)
{
	/* Standard PCI Bus frequency */
	*freq_hz = 33000000;
	return 0;
}

int pcibus_get_params(struct drvmgr_dev *dev, struct drvmgr_bus_params *params)
{
	/* No device prefix */
	params->dev_prefix = NULL;

	return 0;
}

#ifdef PCIBUS_INFO
void pcibus_dev_info(
	struct drvmgr_dev *dev,
	void (*print_line)(void *p, char *str),
	void *p)
{
	struct pci_dev_info *devinfo;
	struct pcibus_res *pcibusres;
	struct pci_res *res;
	char buf[64];
	int i;
	char *str1, *res_types[3] = {" IO16", "MEMIO", "  MEM"};
	uint32_t pcistart;

	if (!dev)
		return;

	devinfo = (struct pci_dev_info *)dev->businfo;
	if (!devinfo)
		return;

	if ((devinfo->id.class >> 8) == PCID_PCI2PCI_BRIDGE)
		print_line(p, "PCI BRIDGE DEVICE");
	else
		print_line(p, "PCI DEVICE");
	sprintf(buf, "LOCATION:    BUS:SLOT:FUNCTION [%x:%x:%x]",
			PCI_DEV_EXPAND(devinfo->pcidev));
	print_line(p, buf);
	sprintf(buf, "PCIID        0x%" PRIx16 "", devinfo->pcidev);
	print_line(p, buf);
	sprintf(buf, "VENDOR ID:   %04x", devinfo->id.vendor);
	print_line(p, buf);
	sprintf(buf, "DEVICE ID:   %04x", devinfo->id.device);
	print_line(p, buf);
	sprintf(buf, "SUBVEN ID:   %04x", devinfo->id.subvendor);
	print_line(p, buf);
	sprintf(buf, "SUBDEV ID:   %04x", devinfo->id.subdevice);
	print_line(p, buf);
	sprintf(buf, "CLASS:       %" PRIx32, devinfo->id.class);
	print_line(p, buf);
	sprintf(buf, "REVISION:    %x", devinfo->rev);
	print_line(p, buf);
	sprintf(buf, "IRQ:         %d", devinfo->irq);
	print_line(p, buf);
	sprintf(buf, "PCIDEV ptr:  %p", devinfo->pci_device);
	print_line(p, buf);

	/* List Resources */
	print_line(p, "RESOURCES");
	for (i = 0; i < PCIDEV_RES_CNT; i++) {
		pcibusres = &devinfo->resources[i];

		str1 = "  RES";
		pcistart = -1;
		res = pcibusres->res;
		if (res && (res->flags & PCI_RES_TYPE_MASK)) {
			str1 = res_types[(res->flags & PCI_RES_TYPE_MASK) - 1];
			if (res->flags & PCI_RES_IO32)
				str1 = " IO32";
			pcistart = res->start;
		}

		if (res && (res->flags & PCI_RES_FAIL)) {
			sprintf(buf, " %s[%d]:  NOT ASSIGNED", str1, i);
			print_line(p, buf);
			continue;
		}
		if (!pcibusres->size)
			continue;

		sprintf(buf, " %s[%d]:  %08" PRIx32 "-%08" PRIx32
			" [PCIADR %" PRIx32 "]",
			str1, i, pcibusres->address,
			pcibusres->address + pcibusres->size - 1, pcistart);
		print_line(p, buf);
	}
}
#endif

#ifdef USE_PCI_CFG_LIB

static int pcibus_dev_register(struct pci_dev *dev, void *arg)
{
	struct drvmgr_bus *pcibus = arg;
	struct drvmgr_dev *newdev;
	struct pci_dev_info *pciinfo;
	int i, type;
	struct pcibus_res *pcibusres;
	struct pci_res *pcires;

	pci_dev_t pcidev = dev->busdevfun;

	DBG("PCI DEV REGISTER: %x:%x:%x\n", PCI_DEV_EXPAND(pcidev));

	/* Allocate a device */
	drvmgr_alloc_dev(&newdev, 24 + sizeof(struct pci_dev_info));
	newdev->next = NULL;
	newdev->parent = pcibus; /* Ourselfs */
	newdev->minor_drv = 0;
	newdev->minor_bus = 0;
	newdev->priv = NULL;
	newdev->drv = NULL;
	newdev->name = (char *)(newdev + 1);
	newdev->next_in_drv = NULL;
	newdev->bus = NULL;

	/* Init PnP information, Assign Core interfaces with this device */
	pciinfo = (struct pci_dev_info *)((char *)(newdev + 1) + 24);

	/* Read Device and Vendor */
	pciinfo->id.vendor = dev->vendor;
	pciinfo->id.device = dev->device;
	pciinfo->id.subvendor = dev->subvendor;
	pciinfo->id.subdevice = dev->subdevice;
	pciinfo->rev = dev->classrev & 0xff;
	pciinfo->id.class = (dev->classrev >> 8) & 0xffffff;

	/* Read IRQ information set by PCI layer */
	pciinfo->irq = dev->sysirq;

	/* Save Location on PCI bus */
	pciinfo->pcidev = pcidev;

	/* Connect device with PCI data structure */
	pciinfo->pci_device = dev;

	/* Build resources so that PCI device drivers doesn't have to scan
	 * configuration space themselves, also the address is translated
	 * into CPU accessible addresses.
	 */
	for (i = 0; i < PCIDEV_RES_CNT; i++) {
		pcibusres = &pciinfo->resources[i];
		pcires = &dev->resources[i];
		type = pcires->flags & PCI_RES_TYPE_MASK;
		if (type == 0 || (pcires->flags & PCI_RES_FAIL))
			continue; /* size=0 */

		pcibusres->address = pcires->start;
		if (pci_pci2cpu(&pcibusres->address, type))
			continue; /* size=0 */
		pcibusres->res = pcires;
		pcibusres->size = pcires->end - pcires->start;
	}

	/* Connect device with PCI information */
	newdev->businfo = (void *)pciinfo;

	/* Create Device Name */
	sprintf(newdev->name, "PCI_%x:%x:%x_%04x:%04x",
		PCI_DEV_BUS(pcidev), PCI_DEV_SLOT(pcidev), PCI_DEV_FUNC(pcidev),
		pciinfo->id.vendor, pciinfo->id.device);

	/* Register New Device */
	drvmgr_dev_register(newdev);

	return 0;
}

#else

static int pcibus_dev_register(pci_dev_t pcidev, void *arg)
{
	struct drvmgr_bus *pcibus = arg;
	struct drvmgr_dev *newdev;
	struct pci_dev_info *pciinfo;

	DBG("PCI DEV REGISTER: %x:%x:%x\n", PCI_DEV_EXPAND(pcidev));

	/* Allocate a device */
	drvmgr_alloc_dev(&newdev, 24 + sizeof(struct pci_dev_info));
	newdev->next = NULL;
	newdev->parent = pcibus; /* Ourselfs */
	newdev->minor_drv = 0;
	newdev->minor_bus = 0;
	newdev->priv = NULL;
	newdev->drv = NULL;
	newdev->name = (char *)(newdev + 1);
	newdev->next_in_drv = NULL;
	newdev->bus = NULL;

	/* Init PnP information, Assign Core interfaces with this device */
	pciinfo = (struct pci_dev_info *)((char *)(newdev + 1) + 24);

	/* Read Device and Vendor */
	pci_cfg_r16(pcidev, PCIR_VENDOR, &pciinfo->id.vendor);
	pci_cfg_r16(pcidev, PCIR_DEVICE, &pciinfo->id.device);
	pci_cfg_r32(pcidev, PCIR_REVID, &pciinfo->id.class);
	pciinfo->rev = pciinfo->id.class & 0xff;
	pciinfo->id.class = pciinfo->id.class >> 8;

	/* Devices have subsytem device and vendor ID */
	if ((pciinfo->id.class >> 8) != PCID_PCI2PCI_BRIDGE) {
		pci_cfg_r16(pcidev, PCIR_SUBVEND_0,
							&pciinfo->id.subvendor);
		pci_cfg_r16(pcidev, PCIR_SUBDEV_0, &pciinfo->id.subdevice);
	} else {
		pciinfo->id.subvendor = 0;
		pciinfo->id.subdevice = 0;
	}

	/* Read IRQ information set by PCI layer */
	pci_cfg_r8(pcidev, PCIR_INTLINE, &pciinfo->irq);

	/* Save Location */
	pciinfo->pcidev = pcidev;

	/* There is no way we can know this information this way */
	pciinfo->pci_device = NULL;

	/* Connect device with PCI information */
	newdev->businfo = (void *)pciinfo;

	/* Create Device Name */
	sprintf(newdev->name, "PCI_%d:%d:%d_%04x:%04x",
		PCI_DEV_BUS(pcidev), PCI_DEV_SLOT(pcidev), PCI_DEV_FUNC(pcidev),
		pciinfo->id.vendor, pciinfo->id.device);

	/* Register New Device */
	drvmgr_dev_register(newdev);

	return 0;
}

#endif

/* Register all AMBA devices available on the AMBAPP bus */
static int pcibus_devs_register(struct drvmgr_bus *bus)
{
	/* return value 0=DRVMGR_OK works with pci_for_each/pci_for_each_dev */
#ifdef USE_PCI_CFG_LIB
	/* Walk the PCI device tree in RAM */
	return pci_for_each_dev(pcibus_dev_register, bus);
#else
	/* Scan PCI Configuration space */
	return pci_for_each(pcibus_dev_register, bus);
#endif
}

/*** DEVICE FUNCTIONS ***/

int pcibus_register(struct drvmgr_dev *dev, struct pcibus_config *config)
{
	struct pcibus_priv *priv;
	int i, fid, rc;

	DBG("PCI BUS: initializing\n");

	/* Create BUS */
	drvmgr_alloc_bus(&dev->bus, sizeof(struct pcibus_priv));
	dev->bus->bus_type = DRVMGR_BUS_TYPE_PCI;
	dev->bus->next = NULL;
	dev->bus->dev = dev;
	dev->bus->children = NULL;
	dev->bus->ops = &pcibus_ops;
	dev->bus->dev_cnt = 0;
	dev->bus->reslist = NULL;
	dev->bus->maps_up = config->maps_up;
	dev->bus->maps_down = config->maps_down;
	dev->bus->funcs = &pcibus_funcs[0];

	/* Copy function definitions from PCI Layer */
	for (i=0; i<6; i++) {
		fid = pcibus_funcs[i].funcid;
		rc = pci_access_func(RW_DIR(fid), RW_SIZE(fid),
				&pcibus_funcs[i].func, PCI_LITTLE_ENDIAN, 3);
		if (rc != 0)
			DBG("PCI BUS: MEMREG 0x%x function not defined\n", fid);
	}

	/* Add resource configuration if user overrided the default empty cfg */
	if (pcibus_drv_resources.resource[0].drv_id != 0)
		drvmgr_bus_res_add(dev->bus, &pcibus_drv_resources);

	/* Init BUS private structures */
	priv = (struct pcibus_priv *)(dev->bus + 1);
	dev->bus->priv = priv;

	/* Register BUS */
	drvmgr_bus_register(dev->bus);

	return DRVMGR_OK;
}

/*** BUS INITIALIZE FUNCTIONS ***/

int pcibus_bus_init1(struct drvmgr_bus *bus)
{
	return pcibus_devs_register(bus);
}