Debian Codegraph @

src/soc/amd/common/block/pci/amd_pci_util.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <device/pci.h>
5 #include <arch/io.h>
6 #include <device/pci_ops.h>
7 #include <amdblocks/amd_pci_util.h>
8 #include <pc80/i8259.h>
9 #include <soc/amd_pci_int_defs.h>
10 #include <string.h>
11
12 /*
13 * These arrays set up the FCH PCI_INTR registers 0xC00/0xC01.
14 * This table is responsible for physically routing the PIC and
15 * IOAPIC IRQs to the different PCI devices on the system. It
16 * is read and written via registers 0xC00/0xC01 as an
17 * Index/Data pair. These values are chipset and mainboard
18 * dependent and should be updated accordingly.
19 */
20 static uint8_t fch_pic_routing[FCH_IRQ_ROUTING_ENTRIES];
21 static uint8_t fch_apic_routing[FCH_IRQ_ROUTING_ENTRIES];
22
23 const struct pirq_struct *pirq_data_ptr;
24 u32 pirq_data_size;
25
26 /*
27 * Read the FCH PCI_INTR registers 0xC00/0xC01 at a
28 * given index and a given PIC (0) or IOAPIC (1) mode
29 */
30 u8 read_pci_int_idx(u8 index, int mode)
31 {
32 outb((mode << 7) | index, PCI_INTR_INDEX);
33 return inb(PCI_INTR_DATA);
34 }
35
36 /*
37 * Write a value to the FCH PCI_INTR registers 0xC00/0xC01
38 * at a given index and PIC (0) or IOAPIC (1) mode
39 */
40 void write_pci_int_idx(u8 index, int mode, u8 data)
41 {
42 outb((mode << 7) | index, PCI_INTR_INDEX);
43 outb(data, PCI_INTR_DATA);
44 }
45
46 static void init_fch_irq_map_tables(void)
47 {
48 const struct fch_irq_routing *mb_irq_map;
49 size_t mb_fch_irq_mapping_table_size;
50 size_t i;
51
52 mb_irq_map = mb_get_fch_irq_mapping(&mb_fch_irq_mapping_table_size);
53
54 memset(fch_pic_routing, PIRQ_NC, sizeof(fch_pic_routing));
55 memset(fch_apic_routing, PIRQ_NC, sizeof(fch_apic_routing));
56
57 for (i = 0; i < mb_fch_irq_mapping_table_size; i++) {
58 if (mb_irq_map[i].intr_index >= FCH_IRQ_ROUTING_ENTRIES) {
59 printk(BIOS_WARNING,
60 "Invalid IRQ index %u in FCH IRQ routing table entry %zu\n",
61 mb_irq_map[i].intr_index, i);
62 continue;
63 }
64 fch_pic_routing[mb_irq_map[i].intr_index] = mb_irq_map[i].pic_irq_num;
65 fch_apic_routing[mb_irq_map[i].intr_index] = mb_irq_map[i].apic_irq_num;
66 }
67 }
68
69 /*
70 * Write the FCH PCI_INTR registers 0xC00/0xC01 with values
71 * given in global variables intr_data and picr_data.
72 * These variables are defined in mainboard.c
73 */
74 void write_pci_int_table(void)
75 {
76 uint8_t byte;
77 size_t i, limit;
78 const struct irq_idx_name *idx_name;
79
80 init_fch_irq_map_tables();
81
82 idx_name = sb_get_apic_reg_association(&limit);
83 if (idx_name == NULL) {
84 printk(BIOS_ERR, "Warning: Can't write PCI_INTR 0xC00/0xC01"
85 " registers because\n"
86 " 'irq_association'\ntables is NULL\n");
87 return;
88 }
89
90 /* PIC IRQ routine */
91 printk(BIOS_DEBUG, "PCI_INTR tables: Writing registers C00/C01 for"
92 " PCI IRQ routing:\n"
93 "PCI_INTR_INDEX\tname\t\t PIC mode"
94 "\tAPIC mode\n");
95 /*
96 * Iterate table idx_name, indexes outside the table are ignored
97 * (assumed not connected within the chip). For each iteration,
98 * get the register index "byte" and the name of the associated
99 * IRQ source for printing.
100 */
101 for (i = 0 ; i < limit; i++) {
102 byte = idx_name[i].index;
103 write_pci_int_idx(byte, 0, fch_pic_routing[byte]);
104 printk(BIOS_DEBUG, "0x%02X\t\t%-20s 0x%02X\t",
105 byte, idx_name[i].name,
106 read_pci_int_idx(byte, 0));
107 write_pci_int_idx(byte, 1, fch_apic_routing[byte]);
108 printk(BIOS_DEBUG, "0x%02X\n", read_pci_int_idx(byte, 1));
109 }
110 }
111
112 /*
113 * Function to write the PCI config space Interrupt
114 * registers based on the values given in PCI_INTR
115 * table at I/O port 0xC00/0xC01
116 */
117 void write_pci_cfg_irqs(void)
118 {
119 struct device *dev = NULL; /* Our current device to route IRQs */
120 struct device *target_dev = NULL; /* the bridge a device may be
121 * connected to */
122 u16 int_pin = 0;
123 u16 target_pin = 0;
124 u16 int_line = 0;
125 u16 pci_intr_idx = 0; /* Index into PCI_INTR table, 0xC00/0xC01 */
126 u16 devfn = 0;
127 u32 i = 0;
128 size_t limit;
129 const struct irq_idx_name *idx_name;
130
131 idx_name = sb_get_apic_reg_association(&limit);
132 if (pirq_data_ptr == NULL) {
133 printk(BIOS_WARNING, "Can't write PCI IRQ assignments"
134 " because 'mainboard_pirq_data' structure does"
135 " not exist\n");
136 return;
137 }
138
139 /* Populate the PCI cfg space header with the IRQ assignment */
140 printk(BIOS_DEBUG, "PCI_CFG IRQ: Write PCI config space IRQ"
141 " assignments\n");
142
143 for (dev = all_devices ; dev ; dev = dev->next) {
144 /*
145 * Step 1: Get INT_PIN and device structure to look for in the
146 * PCI_INTR table pirq_data
147 */
148 target_dev = NULL;
149 target_pin = get_pci_irq_pins(dev, &target_dev);
150 if (target_dev == NULL)
151 continue;
152
153 if (target_pin < 1)
154 continue;
155
156 /* Get the original INT_PIN for record keeping */
157 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
158 if (int_pin < 1 || int_pin > 4)
159 continue; /* Device has invalid INT_PIN - skip */
160
161 devfn = target_dev->path.pci.devfn;
162
163 /*
164 * Step 2: Use the INT_PIN and DevFn number to find the PCI_INTR
165 * register (0xC00) index for this device
166 */
167 pci_intr_idx = 0xbad; /* Will check to make sure it changed */
168 for (i = 0 ; i < pirq_data_size ; i++) {
169 if (pirq_data_ptr[i].devfn != devfn)
170 continue;
171
172 /* PIN_A is idx0 in pirq_data array but 1 in PCI reg */
173 pci_intr_idx = pirq_data_ptr[i].PIN[target_pin - 1];
174 printk(BIOS_SPEW, "\tFound this device in pirq_data"
175 " table entry %d\n", i);
176 break;
177 }
178
179 /*
180 * Step 3: Make sure we got a valid index and use it to get
181 * the IRQ number from the PCI_INTR register table
182 */
183 if (pci_intr_idx == 0xbad) {
184 /* Not on a bridge or in pirq_data table, skip it */
185 printk(BIOS_SPEW, "PCI Devfn (0x%x) not found in"
186 " pirq_data table\n", devfn);
187 continue;
188 } else if (pci_intr_idx == 0x1f) {
189 /* Index found is not defined */
190 printk(BIOS_SPEW, "Got index 0x1F (Not Connected),"
191 " perhaps this device was"
192 " defined wrong?\n");
193 continue;
194 }
195 /*
196 * Find the name associated with register [pci_intr_idx]
197 * and print information.
198 */
199 for (i = 0; i < limit; i++) {
200 if (idx_name[i].index == pci_intr_idx)
201 break;
202 }
203 if (i == limit) {
204 printk(BIOS_SPEW, "Got register index 0x%02x"
205 " undefined in table irq_idx_name,\n"
206 " perhaps this device was"
207 " defined wrong?\n", pci_intr_idx);
208 continue;
209 }
210
211 /* Find the value to program into the INT_LINE register from
212 * the PCI_INTR registers
213 */
214 int_line = read_pci_int_idx(pci_intr_idx, 0);
215 if (int_line == PIRQ_NC) { /* The IRQ found is disabled */
216 printk(BIOS_SPEW, "Got IRQ 0x1F (disabled), perhaps"
217 " this device was defined wrong?\n");
218 continue;
219 }
220
221 /*
222 * Step 4: Program the INT_LINE register in this device's
223 * PCI config space with the IRQ number we found in step 3
224 * and make it Level Triggered
225 */
226 pci_write_config8(dev, PCI_INTERRUPT_LINE, int_line);
227
228 /* Set IRQ to level triggered since used by a PCI device */
229 i8259_configure_irq_trigger(int_line, IRQ_LEVEL_TRIGGERED);
230
231 /*
232 * Step 5: Print out debug info and move on to next device
233 */
234 printk(BIOS_SPEW, "\tOrig INT_PIN\t: %d (%s)\n",
235 int_pin, pin_to_str(int_pin));
236 printk(BIOS_SPEW, "\tPCI_INTR idx\t: 0x%02x (%s)\n"
237 "\tINT_LINE\t: 0x%X (IRQ %d)\n",
238 pci_intr_idx, idx_name[i].name,
239 int_line, int_line);
240 } /* for (dev = all_devices) */
241 printk(BIOS_DEBUG, "PCI_CFG IRQ: Finished writing PCI config space"
242 " IRQ assignments\n");
243 }