blob: 8bb2e0e4efba2322b26f225b58c472cc5b6ba685 [file] [log] [blame]
Dave Frodin2093c4f2014-06-13 08:12:48 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 Sage Electronic Engineering, LLC.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Dave Frodin2093c4f2014-06-13 08:12:48 -060014 */
15
16#include <console/console.h>
17#include <device/pci.h>
18#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020019#include <device/pci_ops.h>
Dave Frodin2093c4f2014-06-13 08:12:48 -060020#include <string.h>
21#include "amd_pci_util.h"
22#include <pc80/i8259.h>
23#include "amd_pci_int_defs.h"
24#include "amd_pci_int_types.h"
25
26#ifndef __PRE_RAM__
27
28const struct pirq_struct * pirq_data_ptr = NULL;
29u32 pirq_data_size = 0;
Elyes HAOUAS251279c2018-07-08 12:41:56 +020030const u8 *intr_data_ptr = NULL;
31const u8 *picr_data_ptr = NULL;
Dave Frodin2093c4f2014-06-13 08:12:48 -060032
33/*
34 * Read the FCH PCI_INTR registers 0xC00/0xC01 at a
35 * given index and a given PIC (0) or IOAPIC (1) mode
36 */
37u8 read_pci_int_idx(u8 index, int mode)
38{
39 outb((mode << 7) | index, PCI_INTR_INDEX);
40 return inb(PCI_INTR_DATA);
41}
42
43/*
44 * Write a value to the FCH PCI_INTR registers 0xC00/0xC01
45 * at a given index and PIC (0) or IOAPIC (1) mode
46 */
47void write_pci_int_idx(u8 index, int mode, u8 data)
48{
49 outb((mode << 7) | index, PCI_INTR_INDEX);
50 outb(data, PCI_INTR_DATA);
51}
52
53/*
54 * Write the FCH PCI_INTR registers 0xC00/0xC01 with values
55 * given in global variables intr_data and picr_data.
56 * These variables are defined in mainboard.c
57 */
58void write_pci_int_table (void)
59{
60 u8 byte;
61
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020062 if (picr_data_ptr == NULL || intr_data_ptr == NULL){
Dave Frodin2093c4f2014-06-13 08:12:48 -060063 printk(BIOS_ERR, "Warning: Can't write PCI_INTR 0xC00/0xC01 registers because\n"
64 "'mainboard_picr_data' or 'mainboard_intr_data' tables are NULL\n");
65 return;
66 }
67
68 /* PIC IRQ routine */
69 printk(BIOS_DEBUG, "PCI_INTR tables: Writing registers C00/C01 for PIC mode PCI IRQ routing:\n"
70 "\tPCI_INTR_INDEX\t\tPCI_INTR_DATA\n");
71 for (byte = 0; byte < FCH_INT_TABLE_SIZE; byte++) {
72 if (intr_types[byte]) {
73 write_pci_int_idx(byte, 0, (u8) picr_data_ptr[byte]);
74 printk(BIOS_DEBUG, "\t0x%02X %s\t: 0x%02X\n",
75 byte, intr_types[byte], read_pci_int_idx(byte, 0));
76 }
77 }
78
79 /* APIC IRQ routine */
80 printk(BIOS_DEBUG, "PCI_INTR tables: Writing registers C00/C01 for APIC mode PCI IRQ routing:\n"
81 "\tPCI_INTR_INDEX\t\tPCI_INTR_DATA\n");
82 for (byte = 0; byte < FCH_INT_TABLE_SIZE; byte++) {
83 if (intr_types[byte]) {
84 write_pci_int_idx(byte, 1, (u8) intr_data_ptr[byte]);
85 printk(BIOS_DEBUG, "\t0x%02X %s\t: 0x%02X\n",
86 byte, intr_types[byte], read_pci_int_idx(byte, 1));
87 }
88 }
89}
90
91/*
92 * Function to write the PCI config space Interrupt
93 * registers based on the values given in PCI_INTR
94 * table at I/O port 0xC00/0xC01
95 */
96void write_pci_cfg_irqs(void)
97{
Elyes HAOUASee424e52018-05-19 15:03:17 +020098 struct device *dev = NULL; /* Our current device to route IRQs to */
99 struct device *target_dev = NULL; /* The bridge that a device may be connected to */
Dave Frodin2093c4f2014-06-13 08:12:48 -0600100 u16 int_pin = 0; /* Value of the INT_PIN register 0x3D */
101 u16 target_pin = 0; /* Pin we will search our tables for */
102 u16 int_line = 0; /* IRQ number read from PCI_INTR table and programmed to INT_LINE reg 0x3C */
103 u16 pci_intr_idx = 0; /* Index into PCI_INTR table, 0xC00/0xC01 */
104 u8 bus = 0; /* A PCI Device Bus number */
105 u16 devfn = 0; /* A PCI Device and Function number */
106 u8 bridged_device = 0; /* This device is on a PCI bridge */
107 u32 i = 0;
108
109 if (pirq_data_ptr == NULL) {
110 printk(BIOS_WARNING, "Warning: Can't write PCI IRQ assignments because"
111 " 'mainboard_pirq_data' structure does not exist\n");
112 return;
113 }
114
115 /* Populate the PCI cfg space header with the IRQ assignment */
116 printk(BIOS_DEBUG, "PCI_CFG IRQ: Write PCI config space IRQ assignments\n");
117
118 for (dev = all_devices; dev; dev = dev->next) {
119 /*
120 * Step 1: Get the INT_PIN and device structure to look for in the
121 * PCI_INTR table pirq_data
122 */
123 target_dev = NULL;
124 target_pin = get_pci_irq_pins(dev, &target_dev);
125 if (target_dev == NULL)
126 continue;
127
128 if (target_pin < 1)
129 continue;
130
131 /* Get the original INT_PIN for record keeping */
132 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
133 if (int_pin < 1 || int_pin > 4)
134 continue; /* Device has invalid INT_PIN so skip it */
135
136 bus = target_dev->bus->secondary;
137 devfn = target_dev->path.pci.devfn;
138
139 /*
140 * Step 2: Use the INT_PIN and DevFn number to find the PCI_INTR
141 * register (0xC00) index for this device
142 */
143 pci_intr_idx = 0xBAD; /* Will check to make sure it changed */
144 for (i = 0; i < pirq_data_size; i++) {
145 if (pirq_data_ptr[i].devfn != devfn)
146 continue;
147
148 /* PIN_A is index 0 in pirq_data array but 1 in PCI cfg reg */
149 pci_intr_idx = pirq_data_ptr[i].PIN[target_pin - 1];
150 printk(BIOS_SPEW, "\tFound this device in pirq_data table entry %d\n", i);
151 break;
152 }
153
154 /*
155 * Step 3: Make sure we got a valid index and use it to get
156 * the IRQ number from the PCI_INTR register table
157 */
158 if (pci_intr_idx == 0xBAD) { /* Not on a bridge or in pirq_data table, skip it */
159 printk(BIOS_SPEW, "PCI Devfn (0x%x) not found in pirq_data table\n", devfn);
160 continue;
161 } else if (pci_intr_idx == 0x1F) { /* Index found is not defined */
162 printk(BIOS_SPEW, "Got index 0x1F (Not Connected), perhaps this device was defined wrong?\n");
163 continue;
164 } else if (pci_intr_idx >= FCH_INT_TABLE_SIZE) { /* Index out of bounds */
165 printk(BIOS_ERR, "%s: got 0xC00/0xC01 table index 0x%x, max is 0x%x\n",
166 __func__, pci_intr_idx, FCH_INT_TABLE_SIZE);
167 continue;
168 }
169
170 /* Find the value to program into the INT_LINE register from the PCI_INTR registers */
171 int_line = read_pci_int_idx(pci_intr_idx, 0);
172 if (int_line == PIRQ_NC) { /* The IRQ found is disabled */
173 printk(BIOS_SPEW, "Got IRQ 0x1F (disabled), perhaps this device was defined wrong?\n");
174 continue;
175 }
176
177 /*
178 * Step 4: Program the INT_LINE register in this device's
179 * PCI config space with the IRQ number we found in step 3
180 * and make it Level Triggered
181 */
182 pci_write_config8(dev, PCI_INTERRUPT_LINE, int_line);
183
184 /* Set this IRQ to level triggered since it is used by a PCI device */
185 i8259_configure_irq_trigger(int_line, IRQ_LEVEL_TRIGGERED);
186
187 /*
188 * Step 5: Print out debug info and move on to next device
189 */
190 printk(BIOS_SPEW, "\tOrig INT_PIN\t: %d (%s)\n",
191 int_pin, pin_to_str(int_pin));
192 if (bridged_device)
193 printk(BIOS_SPEW, "\tSwizzled to\t: %d (%s)\n",
194 target_pin, pin_to_str(target_pin));
195 printk(BIOS_SPEW, "\tPCI_INTR idx\t: 0x%02x (%s)\n"
196 "\tINT_LINE\t: 0x%X (IRQ %d)\n",
197 pci_intr_idx, intr_types[pci_intr_idx], int_line, int_line);
198 } /* for (dev = all_devices) */
199 printk(BIOS_DEBUG, "PCI_CFG IRQ: Finished writing PCI config space IRQ assignments\n");
200}
201#endif /* __PRE_RAM__ */