Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2014 Sage Electronic Engineering, LLC. |
Richard Spiegel | 376dc82 | 2017-12-01 08:24:26 -0700 | [diff] [blame] | 5 | * Copyright (C) 2017 Advanced Micro Devices, Inc. |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 6 | * |
| 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> |
| 20 | #include <string.h> |
Richard Spiegel | 2bbc3dc | 2017-12-06 16:14:58 -0700 | [diff] [blame] | 21 | #include <amdblocks/amd_pci_util.h> |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 22 | #include <pc80/i8259.h> |
Richard Spiegel | a800bdb | 2017-11-13 16:59:38 -0700 | [diff] [blame] | 23 | #include <soc/amd_pci_int_defs.h> |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 24 | |
Aaron Durbin | 8dd4006 | 2017-11-03 11:50:14 -0600 | [diff] [blame] | 25 | const struct pirq_struct *pirq_data_ptr; |
| 26 | u32 pirq_data_size; |
| 27 | const u8 *intr_data_ptr; |
| 28 | const u8 *picr_data_ptr; |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * Read the FCH PCI_INTR registers 0xC00/0xC01 at a |
| 32 | * given index and a given PIC (0) or IOAPIC (1) mode |
| 33 | */ |
| 34 | u8 read_pci_int_idx(u8 index, int mode) |
| 35 | { |
| 36 | outb((mode << 7) | index, PCI_INTR_INDEX); |
| 37 | return inb(PCI_INTR_DATA); |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Write a value to the FCH PCI_INTR registers 0xC00/0xC01 |
| 42 | * at a given index and PIC (0) or IOAPIC (1) mode |
| 43 | */ |
| 44 | void write_pci_int_idx(u8 index, int mode, u8 data) |
| 45 | { |
| 46 | outb((mode << 7) | index, PCI_INTR_INDEX); |
| 47 | outb(data, PCI_INTR_DATA); |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Write the FCH PCI_INTR registers 0xC00/0xC01 with values |
| 52 | * given in global variables intr_data and picr_data. |
| 53 | * These variables are defined in mainboard.c |
| 54 | */ |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 55 | void write_pci_int_table(void) |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 56 | { |
Richard Spiegel | 376dc82 | 2017-12-01 08:24:26 -0700 | [diff] [blame] | 57 | uint8_t byte; |
| 58 | size_t i, limit; |
| 59 | const struct irq_idx_name *idx_name; |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 60 | |
Richard Spiegel | 376dc82 | 2017-12-01 08:24:26 -0700 | [diff] [blame] | 61 | idx_name = sb_get_apic_reg_association(&limit); |
Richard Spiegel | 6c2ab06 | 2017-12-18 09:52:42 -0700 | [diff] [blame] | 62 | if (picr_data_ptr == NULL || intr_data_ptr == NULL || |
| 63 | idx_name == NULL) { |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 64 | printk(BIOS_ERR, "Warning: Can't write PCI_INTR 0xC00/0xC01" |
| 65 | " registers because\n" |
Richard Spiegel | 6c2ab06 | 2017-12-18 09:52:42 -0700 | [diff] [blame] | 66 | "'mainboard_picr_data' or 'mainboard_intr_data'" |
| 67 | " or 'irq_association'\ntables are NULL\n"); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | |
| 71 | /* PIC IRQ routine */ |
Richard Spiegel | 376dc82 | 2017-12-01 08:24:26 -0700 | [diff] [blame] | 72 | printk(BIOS_DEBUG, "PCI_INTR tables: Writing registers C00/C01 for" |
| 73 | " PCI IRQ routing:\n" |
Richard Spiegel | e89d444 | 2017-12-08 07:52:42 -0700 | [diff] [blame] | 74 | "PCI_INTR_INDEX\tname\t\t PIC mode" |
Richard Spiegel | 376dc82 | 2017-12-01 08:24:26 -0700 | [diff] [blame] | 75 | "\tAPIC mode\n"); |
| 76 | /* |
| 77 | * Iterate table idx_name, indexes outside the table are ignored |
| 78 | * (assumed not connected within the chip). For each iteration, |
| 79 | * get the register index "byte" and the name of the associated |
| 80 | * IRQ source for printing. |
| 81 | */ |
| 82 | for (i = 0 ; i < limit; i++) { |
| 83 | byte = idx_name[i].index; |
| 84 | write_pci_int_idx(byte, 0, (u8) picr_data_ptr[byte]); |
Richard Spiegel | e89d444 | 2017-12-08 07:52:42 -0700 | [diff] [blame] | 85 | printk(BIOS_DEBUG, "0x%02X\t\t%-20s 0x%02X\t", |
Richard Spiegel | 376dc82 | 2017-12-01 08:24:26 -0700 | [diff] [blame] | 86 | byte, idx_name[i].name, |
| 87 | read_pci_int_idx(byte, 0)); |
| 88 | write_pci_int_idx(byte, 1, (u8) intr_data_ptr[byte]); |
| 89 | printk(BIOS_DEBUG, "0x%02X\n", read_pci_int_idx(byte, 1)); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Function to write the PCI config space Interrupt |
| 95 | * registers based on the values given in PCI_INTR |
| 96 | * table at I/O port 0xC00/0xC01 |
| 97 | */ |
| 98 | void write_pci_cfg_irqs(void) |
| 99 | { |
Elyes HAOUAS | 0f5957a | 2018-05-22 10:58:50 +0200 | [diff] [blame] | 100 | struct device *dev = NULL; /* Our current device to route IRQs */ |
Richard Spiegel | 2b7cd1d | 2018-10-22 14:39:37 -0700 | [diff] [blame^] | 101 | struct device *target_dev = NULL; /* the bridge a device may be |
| 102 | * connected to */ |
| 103 | u16 int_pin = 0; |
| 104 | u16 target_pin = 0; |
| 105 | u16 int_line = 0; |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 106 | u16 pci_intr_idx = 0; /* Index into PCI_INTR table, 0xC00/0xC01 */ |
Richard Spiegel | 2b7cd1d | 2018-10-22 14:39:37 -0700 | [diff] [blame^] | 107 | u8 bus = 0; /* TODO: no longer used, remove it */ |
| 108 | u16 devfn = 0; |
| 109 | u8 bridged_device = 0; /* TODO: Remove this */ |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 110 | u32 i = 0; |
Richard Spiegel | 376dc82 | 2017-12-01 08:24:26 -0700 | [diff] [blame] | 111 | size_t limit; |
| 112 | const struct irq_idx_name *idx_name; |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 113 | |
Richard Spiegel | 376dc82 | 2017-12-01 08:24:26 -0700 | [diff] [blame] | 114 | idx_name = sb_get_apic_reg_association(&limit); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 115 | if (pirq_data_ptr == NULL) { |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 116 | printk(BIOS_WARNING, "Warning: Can't write PCI IRQ assignments" |
| 117 | " because 'mainboard_pirq_data' structure does" |
| 118 | " not exist\n"); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | |
| 122 | /* Populate the PCI cfg space header with the IRQ assignment */ |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 123 | printk(BIOS_DEBUG, "PCI_CFG IRQ: Write PCI config space IRQ" |
| 124 | " assignments\n"); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 125 | |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 126 | for (dev = all_devices ; dev ; dev = dev->next) { |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 127 | /* |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 128 | * Step 1: Get INT_PIN and device structure to look for in the |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 129 | * PCI_INTR table pirq_data |
| 130 | */ |
| 131 | target_dev = NULL; |
| 132 | target_pin = get_pci_irq_pins(dev, &target_dev); |
| 133 | if (target_dev == NULL) |
| 134 | continue; |
| 135 | |
| 136 | if (target_pin < 1) |
| 137 | continue; |
| 138 | |
| 139 | /* Get the original INT_PIN for record keeping */ |
| 140 | int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN); |
| 141 | if (int_pin < 1 || int_pin > 4) |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 142 | continue; /* Device has invalid INT_PIN - skip */ |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 143 | |
| 144 | bus = target_dev->bus->secondary; |
| 145 | devfn = target_dev->path.pci.devfn; |
| 146 | |
| 147 | /* |
| 148 | * Step 2: Use the INT_PIN and DevFn number to find the PCI_INTR |
| 149 | * register (0xC00) index for this device |
| 150 | */ |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 151 | pci_intr_idx = 0xbad; /* Will check to make sure it changed */ |
| 152 | for (i = 0 ; i < pirq_data_size ; i++) { |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 153 | if (pirq_data_ptr[i].devfn != devfn) |
| 154 | continue; |
| 155 | |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 156 | /* PIN_A is idx0 in pirq_data array but 1 in PCI reg */ |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 157 | pci_intr_idx = pirq_data_ptr[i].PIN[target_pin - 1]; |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 158 | printk(BIOS_SPEW, "\tFound this device in pirq_data" |
| 159 | " table entry %d\n", i); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 160 | break; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Step 3: Make sure we got a valid index and use it to get |
| 165 | * the IRQ number from the PCI_INTR register table |
| 166 | */ |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 167 | if (pci_intr_idx == 0xbad) { |
| 168 | /* Not on a bridge or in pirq_data table, skip it */ |
| 169 | printk(BIOS_SPEW, "PCI Devfn (0x%x) not found in" |
| 170 | " pirq_data table\n", devfn); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 171 | continue; |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 172 | } else if (pci_intr_idx == 0x1f) { |
| 173 | /* Index found is not defined */ |
| 174 | printk(BIOS_SPEW, "Got index 0x1F (Not Connected)," |
| 175 | " perhaps this device was" |
| 176 | " defined wrong?\n"); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 177 | continue; |
Richard Spiegel | 376dc82 | 2017-12-01 08:24:26 -0700 | [diff] [blame] | 178 | } |
| 179 | /* |
| 180 | * Find the name associated with register [pci_intr_idx] |
| 181 | * and print information. |
| 182 | */ |
| 183 | for (i = 0; i < limit; i++) { |
| 184 | if (idx_name[i].index == pci_intr_idx) |
| 185 | break; |
| 186 | } |
| 187 | if (i == limit) { |
| 188 | printk(BIOS_SPEW, "Got register index 0x%02x" |
| 189 | " undefined in table irq_idx_name,\n" |
| 190 | " perhaps this device was" |
| 191 | " defined wrong?\n", pci_intr_idx); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 192 | continue; |
| 193 | } |
| 194 | |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 195 | /* Find the value to program into the INT_LINE register from |
| 196 | * the PCI_INTR registers |
| 197 | */ |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 198 | int_line = read_pci_int_idx(pci_intr_idx, 0); |
| 199 | if (int_line == PIRQ_NC) { /* The IRQ found is disabled */ |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 200 | printk(BIOS_SPEW, "Got IRQ 0x1F (disabled), perhaps" |
| 201 | " this device was defined wrong?\n"); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 202 | continue; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Step 4: Program the INT_LINE register in this device's |
| 207 | * PCI config space with the IRQ number we found in step 3 |
| 208 | * and make it Level Triggered |
| 209 | */ |
| 210 | pci_write_config8(dev, PCI_INTERRUPT_LINE, int_line); |
| 211 | |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 212 | /* Set IRQ to level triggered since used by a PCI device */ |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 213 | i8259_configure_irq_trigger(int_line, IRQ_LEVEL_TRIGGERED); |
| 214 | |
| 215 | /* |
| 216 | * Step 5: Print out debug info and move on to next device |
| 217 | */ |
| 218 | printk(BIOS_SPEW, "\tOrig INT_PIN\t: %d (%s)\n", |
| 219 | int_pin, pin_to_str(int_pin)); |
| 220 | if (bridged_device) |
| 221 | printk(BIOS_SPEW, "\tSwizzled to\t: %d (%s)\n", |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 222 | target_pin, pin_to_str(target_pin)); |
Richard Spiegel | 376dc82 | 2017-12-01 08:24:26 -0700 | [diff] [blame] | 223 | |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 224 | printk(BIOS_SPEW, "\tPCI_INTR idx\t: 0x%02x (%s)\n" |
Richard Spiegel | 376dc82 | 2017-12-01 08:24:26 -0700 | [diff] [blame] | 225 | "\tINT_LINE\t: 0x%X (IRQ %d)\n", |
| 226 | pci_intr_idx, idx_name[i].name, |
| 227 | int_line, int_line); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 228 | } /* for (dev = all_devices) */ |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 229 | printk(BIOS_DEBUG, "PCI_CFG IRQ: Finished writing PCI config space" |
| 230 | " IRQ assignments\n"); |
Marc Jones | 2448484 | 2017-05-04 21:17:45 -0600 | [diff] [blame] | 231 | } |