blob: d06a8df9fa22f4b081f6bc80b279e286e43c3eec [file] [log] [blame]
Marc Jones24484842017-05-04 21:17:45 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 Sage Electronic Engineering, LLC.
Richard Spiegel376dc822017-12-01 08:24:26 -07005 * Copyright (C) 2017 Advanced Micro Devices, Inc.
Marc Jones24484842017-05-04 21:17:45 -06006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <console/console.h>
18#include <device/pci.h>
19#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020020#include <device/pci_ops.h>
Marc Jones24484842017-05-04 21:17:45 -060021#include <string.h>
Richard Spiegel2bbc3dc2017-12-06 16:14:58 -070022#include <amdblocks/amd_pci_util.h>
Marc Jones24484842017-05-04 21:17:45 -060023#include <pc80/i8259.h>
Richard Spiegela800bdb2017-11-13 16:59:38 -070024#include <soc/amd_pci_int_defs.h>
Marc Jones24484842017-05-04 21:17:45 -060025
Aaron Durbin8dd40062017-11-03 11:50:14 -060026const struct pirq_struct *pirq_data_ptr;
27u32 pirq_data_size;
28const u8 *intr_data_ptr;
29const u8 *picr_data_ptr;
Marc Jones24484842017-05-04 21:17:45 -060030
31/*
32 * Read the FCH PCI_INTR registers 0xC00/0xC01 at a
33 * given index and a given PIC (0) or IOAPIC (1) mode
34 */
35u8 read_pci_int_idx(u8 index, int mode)
36{
37 outb((mode << 7) | index, PCI_INTR_INDEX);
38 return inb(PCI_INTR_DATA);
39}
40
41/*
42 * Write a value to the FCH PCI_INTR registers 0xC00/0xC01
43 * at a given index and PIC (0) or IOAPIC (1) mode
44 */
45void write_pci_int_idx(u8 index, int mode, u8 data)
46{
47 outb((mode << 7) | index, PCI_INTR_INDEX);
48 outb(data, PCI_INTR_DATA);
49}
50
51/*
52 * Write the FCH PCI_INTR registers 0xC00/0xC01 with values
53 * given in global variables intr_data and picr_data.
54 * These variables are defined in mainboard.c
55 */
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -060056void write_pci_int_table(void)
Marc Jones24484842017-05-04 21:17:45 -060057{
Richard Spiegel376dc822017-12-01 08:24:26 -070058 uint8_t byte;
59 size_t i, limit;
60 const struct irq_idx_name *idx_name;
Marc Jones24484842017-05-04 21:17:45 -060061
Richard Spiegel376dc822017-12-01 08:24:26 -070062 idx_name = sb_get_apic_reg_association(&limit);
Richard Spiegel6c2ab062017-12-18 09:52:42 -070063 if (picr_data_ptr == NULL || intr_data_ptr == NULL ||
64 idx_name == NULL) {
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -060065 printk(BIOS_ERR, "Warning: Can't write PCI_INTR 0xC00/0xC01"
66 " registers because\n"
Richard Spiegel6c2ab062017-12-18 09:52:42 -070067 "'mainboard_picr_data' or 'mainboard_intr_data'"
68 " or 'irq_association'\ntables are NULL\n");
Marc Jones24484842017-05-04 21:17:45 -060069 return;
70 }
71
72 /* PIC IRQ routine */
Richard Spiegel376dc822017-12-01 08:24:26 -070073 printk(BIOS_DEBUG, "PCI_INTR tables: Writing registers C00/C01 for"
74 " PCI IRQ routing:\n"
Richard Spiegele89d4442017-12-08 07:52:42 -070075 "PCI_INTR_INDEX\tname\t\t PIC mode"
Richard Spiegel376dc822017-12-01 08:24:26 -070076 "\tAPIC mode\n");
77 /*
78 * Iterate table idx_name, indexes outside the table are ignored
79 * (assumed not connected within the chip). For each iteration,
80 * get the register index "byte" and the name of the associated
81 * IRQ source for printing.
82 */
83 for (i = 0 ; i < limit; i++) {
84 byte = idx_name[i].index;
85 write_pci_int_idx(byte, 0, (u8) picr_data_ptr[byte]);
Richard Spiegele89d4442017-12-08 07:52:42 -070086 printk(BIOS_DEBUG, "0x%02X\t\t%-20s 0x%02X\t",
Richard Spiegel376dc822017-12-01 08:24:26 -070087 byte, idx_name[i].name,
88 read_pci_int_idx(byte, 0));
89 write_pci_int_idx(byte, 1, (u8) intr_data_ptr[byte]);
90 printk(BIOS_DEBUG, "0x%02X\n", read_pci_int_idx(byte, 1));
Marc Jones24484842017-05-04 21:17:45 -060091 }
92}
93
94/*
95 * Function to write the PCI config space Interrupt
96 * registers based on the values given in PCI_INTR
97 * table at I/O port 0xC00/0xC01
98 */
99void write_pci_cfg_irqs(void)
100{
Elyes HAOUAS0f5957a2018-05-22 10:58:50 +0200101 struct device *dev = NULL; /* Our current device to route IRQs */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700102 struct device *target_dev = NULL; /* the bridge a device may be
103 * connected to */
104 u16 int_pin = 0;
105 u16 target_pin = 0;
106 u16 int_line = 0;
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600107 u16 pci_intr_idx = 0; /* Index into PCI_INTR table, 0xC00/0xC01 */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700108 u16 devfn = 0;
Marc Jones24484842017-05-04 21:17:45 -0600109 u32 i = 0;
Richard Spiegel376dc822017-12-01 08:24:26 -0700110 size_t limit;
111 const struct irq_idx_name *idx_name;
Marc Jones24484842017-05-04 21:17:45 -0600112
Richard Spiegel376dc822017-12-01 08:24:26 -0700113 idx_name = sb_get_apic_reg_association(&limit);
Marc Jones24484842017-05-04 21:17:45 -0600114 if (pirq_data_ptr == NULL) {
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600115 printk(BIOS_WARNING, "Warning: Can't write PCI IRQ assignments"
116 " because 'mainboard_pirq_data' structure does"
117 " not exist\n");
Marc Jones24484842017-05-04 21:17:45 -0600118 return;
119 }
120
121 /* Populate the PCI cfg space header with the IRQ assignment */
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600122 printk(BIOS_DEBUG, "PCI_CFG IRQ: Write PCI config space IRQ"
123 " assignments\n");
Marc Jones24484842017-05-04 21:17:45 -0600124
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600125 for (dev = all_devices ; dev ; dev = dev->next) {
Marc Jones24484842017-05-04 21:17:45 -0600126 /*
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600127 * Step 1: Get INT_PIN and device structure to look for in the
Marc Jones24484842017-05-04 21:17:45 -0600128 * PCI_INTR table pirq_data
129 */
130 target_dev = NULL;
131 target_pin = get_pci_irq_pins(dev, &target_dev);
132 if (target_dev == NULL)
133 continue;
134
135 if (target_pin < 1)
136 continue;
137
138 /* Get the original INT_PIN for record keeping */
139 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
140 if (int_pin < 1 || int_pin > 4)
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600141 continue; /* Device has invalid INT_PIN - skip */
Marc Jones24484842017-05-04 21:17:45 -0600142
Marc Jones24484842017-05-04 21:17:45 -0600143 devfn = target_dev->path.pci.devfn;
144
145 /*
146 * Step 2: Use the INT_PIN and DevFn number to find the PCI_INTR
147 * register (0xC00) index for this device
148 */
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600149 pci_intr_idx = 0xbad; /* Will check to make sure it changed */
150 for (i = 0 ; i < pirq_data_size ; i++) {
Marc Jones24484842017-05-04 21:17:45 -0600151 if (pirq_data_ptr[i].devfn != devfn)
152 continue;
153
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600154 /* PIN_A is idx0 in pirq_data array but 1 in PCI reg */
Marc Jones24484842017-05-04 21:17:45 -0600155 pci_intr_idx = pirq_data_ptr[i].PIN[target_pin - 1];
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600156 printk(BIOS_SPEW, "\tFound this device in pirq_data"
157 " table entry %d\n", i);
Marc Jones24484842017-05-04 21:17:45 -0600158 break;
159 }
160
161 /*
162 * Step 3: Make sure we got a valid index and use it to get
163 * the IRQ number from the PCI_INTR register table
164 */
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600165 if (pci_intr_idx == 0xbad) {
166 /* Not on a bridge or in pirq_data table, skip it */
167 printk(BIOS_SPEW, "PCI Devfn (0x%x) not found in"
168 " pirq_data table\n", devfn);
Marc Jones24484842017-05-04 21:17:45 -0600169 continue;
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600170 } else if (pci_intr_idx == 0x1f) {
171 /* Index found is not defined */
172 printk(BIOS_SPEW, "Got index 0x1F (Not Connected),"
173 " perhaps this device was"
174 " defined wrong?\n");
Marc Jones24484842017-05-04 21:17:45 -0600175 continue;
Richard Spiegel376dc822017-12-01 08:24:26 -0700176 }
177 /*
178 * Find the name associated with register [pci_intr_idx]
179 * and print information.
180 */
181 for (i = 0; i < limit; i++) {
182 if (idx_name[i].index == pci_intr_idx)
183 break;
184 }
185 if (i == limit) {
186 printk(BIOS_SPEW, "Got register index 0x%02x"
187 " undefined in table irq_idx_name,\n"
188 " perhaps this device was"
189 " defined wrong?\n", pci_intr_idx);
Marc Jones24484842017-05-04 21:17:45 -0600190 continue;
191 }
192
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600193 /* Find the value to program into the INT_LINE register from
194 * the PCI_INTR registers
195 */
Marc Jones24484842017-05-04 21:17:45 -0600196 int_line = read_pci_int_idx(pci_intr_idx, 0);
197 if (int_line == PIRQ_NC) { /* The IRQ found is disabled */
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600198 printk(BIOS_SPEW, "Got IRQ 0x1F (disabled), perhaps"
199 " this device was defined wrong?\n");
Marc Jones24484842017-05-04 21:17:45 -0600200 continue;
201 }
202
203 /*
204 * Step 4: Program the INT_LINE register in this device's
205 * PCI config space with the IRQ number we found in step 3
206 * and make it Level Triggered
207 */
208 pci_write_config8(dev, PCI_INTERRUPT_LINE, int_line);
209
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600210 /* Set IRQ to level triggered since used by a PCI device */
Marc Jones24484842017-05-04 21:17:45 -0600211 i8259_configure_irq_trigger(int_line, IRQ_LEVEL_TRIGGERED);
212
213 /*
214 * Step 5: Print out debug info and move on to next device
215 */
216 printk(BIOS_SPEW, "\tOrig INT_PIN\t: %d (%s)\n",
217 int_pin, pin_to_str(int_pin));
Marc Jones24484842017-05-04 21:17:45 -0600218 printk(BIOS_SPEW, "\tPCI_INTR idx\t: 0x%02x (%s)\n"
Richard Spiegel376dc822017-12-01 08:24:26 -0700219 "\tINT_LINE\t: 0x%X (IRQ %d)\n",
220 pci_intr_idx, idx_name[i].name,
221 int_line, int_line);
Marc Jones24484842017-05-04 21:17:45 -0600222 } /* for (dev = all_devices) */
Marshall Dawsonf3dc71e2017-06-14 16:22:07 -0600223 printk(BIOS_DEBUG, "PCI_CFG IRQ: Finished writing PCI config space"
224 " IRQ assignments\n");
Marc Jones24484842017-05-04 21:17:45 -0600225}