summaryrefslogtreecommitdiff
path: root/src/pirq.c
blob: 6568c2459b2c12013a110e04941beb5e49ddecc3 (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
/*
 * PCI IRQ Routing Table dumper
 * Copyright (c) 2006 Dell, Inc.
 *  by Matt Domsch <Matt_Domsch@dell.com>
 *
 * Based on the description of said table found at
 * http://www.microsoft.com/hwdev/busbios/pciirq.htm
 * Licensed under the GNU General Public license, version 2.
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <sys/mman.h>
#include "pirq.h"

extern int nopirq;

/* If unknown, use INT_MAX so they get sorted last */
int pirq_pci_dev_to_slot(struct routing_table *table, int domain, int bus, int dev)
{
	int i, num_slots;
	struct slot_entry *slot;

	if (!table)
		return INT_MAX;
	if (domain != 0) /* can't represent non-zero domains in PIRQ */
		return INT_MAX;

	num_slots = (table->size - 32) / sizeof(*slot);
	for (i=0; i<num_slots; i++) {
		slot = &table->slot[i];
		if (slot->bus == bus &&
		    PCI_DEVICE(slot->device) == dev) {
		  	if (slot->slot >= '1' && slot->slot <= '9')
				return slot->slot - '0';
			return slot->slot;
		}
	}
	return INT_MAX;
}



struct routing_table * pirq_alloc_read_table()
{
	struct routing_table *table = NULL;
	uint16_t size = 0;
	uint8_t checksum = 0;
	int i;
	void *mem;
	off_t offset=0L;
	int fd;

	/* Skip PIRQ table parsing */
	if (nopirq) {
		return NULL;
	}
	fd = open("/dev/mem", O_RDONLY);
	if(fd==-1)
		return NULL;

	mem = mmap(0, 64*1024, PROT_READ, MAP_SHARED, fd, 0xF0000L);
	if (mem == (void *)-1LL)
		goto out;

	while( offset < 0xFFFF)
	{
		if(memcmp(mem+offset, "$PIR", 4)==0)
		{
			table = (struct routing_table *)(mem+offset);
			size = table->size;
			/* quick sanity checks */
			if (size == 0) {
				table = NULL;
				break;
			}
			/* Version must be 1.0 */
			if (!((table->version >> 8) == 1 &&
			      (table->version & 0xFF) == 0)) {
				table = NULL;
				break;
			}

			table = malloc(size);
			if (!table) break;

			memcpy(table, mem+offset, size);
			for (i=0; i<size; i++)
				checksum +=*(((uint8_t *)table)+i);
			if (checksum) {
				free (table);
				table = NULL;
			}
			break;
		}
		offset += 16;
	}
	munmap(mem, 64*1024);
out:
	close(fd);
	return table;
}

void pirq_free_table(struct routing_table *table)
{
	if (table)
		free(table);
}


#ifdef UNIT_TEST_PIRQ

static void
pirq_unparse_slot(struct slot_entry *slot)
{
	printf("Slot %d: PCI %x:%x. ",
	       slot->slot, slot->bus, PCI_DEVICE(slot->device));
	printf("INTA link %x irq %x ", slot->inta_link, slot->inta_irq);
	printf("B link %x irq %x ", slot->intb_link, slot->intb_irq);
	printf("C link %x irq %x ", slot->intc_link, slot->intc_irq);
	printf("D link %x irq %x ", slot->intd_link, slot->intd_irq);
	printf("\n");
}

static void
pirq_unparse_routing_table(struct routing_table *table)
{
	int i, num_slots;
	struct slot_entry *slot;
	char buf[5]; buf[4] = 0;
	memcpy(buf, &table->signature, 4);

	printf("PCI IRQ Routing Table\n");
	printf("Signature: %s\n", buf);
	printf("Version  : %x\n", table->version);
	printf("Size     : %xh\n", table->size);
	printf("Bus      : %x\n", table->router_bus);
	printf("DevFn    : %x\n", table->router_devfn);
	printf("Exclusive IRQs : %x\n", table->exclusive_irqs);
	printf("Compatible Router: %x\n", table->compatable_router);

	num_slots = (table->size - 32) / sizeof(*slot);
	slot = &table->slot[0];
	for (i=0; i<num_slots; i++) {
		pirq_unparse_slot(&slot[i]);
	}
}

int main(int argc, char *argv[])
{
	struct routing_table *table;
	table = pirq_alloc_read_table();
	if (!table)
		return 1;
	pirq_unparse_routing_table(table);
	pirq_free_table(table);
	return 0;
}

#endif