Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com> |
| 4 | * |
| 5 | * Part of this file is ported from coreboot src/arch/x86/boot/pirq_routing.c |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 10 | #include <pci.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 12 | #include <asm/pci.h> |
| 13 | #include <asm/pirq_routing.h> |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 14 | |
Simon Glass | 25271ae | 2019-12-06 21:42:15 -0700 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Simon Glass | 27a6eaa | 2017-01-16 07:04:18 -0700 | [diff] [blame] | 17 | static u8 pirq_get_next_free_irq(struct udevice *dev, u8 *pirq, u16 bitmap, |
| 18 | bool irq_already_routed[]) |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 19 | { |
| 20 | int i, link; |
| 21 | u8 irq = 0; |
| 22 | |
| 23 | /* IRQ sharing starts from IRQ#3 */ |
| 24 | for (i = 3; i < 16; i++) { |
| 25 | /* Can we assign this IRQ? */ |
| 26 | if (!((bitmap >> i) & 1)) |
| 27 | continue; |
| 28 | |
| 29 | /* We can, now let's assume we can use this IRQ */ |
| 30 | irq = i; |
| 31 | |
| 32 | /* Have we already routed it? */ |
| 33 | if (irq_already_routed[irq]) |
| 34 | continue; |
| 35 | |
| 36 | for (link = 0; link < CONFIG_MAX_PIRQ_LINKS; link++) { |
Bin Meng | a5a2003 | 2016-02-01 01:40:51 -0800 | [diff] [blame] | 37 | if (pirq_check_irq_routed(dev, link, irq)) { |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 38 | irq_already_routed[irq] = true; |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /* If it's not yet routed, use it */ |
| 44 | if (!irq_already_routed[irq]) { |
| 45 | irq_already_routed[irq] = true; |
| 46 | break; |
| 47 | } |
| 48 | |
| 49 | /* But if it was already routed, try the next one */ |
| 50 | } |
| 51 | |
| 52 | /* Now we get our IRQ */ |
| 53 | return irq; |
| 54 | } |
| 55 | |
Bin Meng | a5a2003 | 2016-02-01 01:40:51 -0800 | [diff] [blame] | 56 | void pirq_route_irqs(struct udevice *dev, struct irq_info *irq, int num) |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 57 | { |
| 58 | unsigned char irq_slot[MAX_INTX_ENTRIES]; |
| 59 | unsigned char pirq[CONFIG_MAX_PIRQ_LINKS]; |
Simon Glass | 27a6eaa | 2017-01-16 07:04:18 -0700 | [diff] [blame] | 60 | bool irq_already_routed[16]; |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 61 | int i, intx; |
| 62 | |
| 63 | memset(pirq, 0, CONFIG_MAX_PIRQ_LINKS); |
Simon Glass | 27a6eaa | 2017-01-16 07:04:18 -0700 | [diff] [blame] | 64 | memset(irq_already_routed, '\0', sizeof(irq_already_routed)); |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 65 | |
| 66 | /* Set PCI IRQs */ |
| 67 | for (i = 0; i < num; i++) { |
| 68 | debug("PIRQ Entry %d Dev: %d.%x.%d\n", i, |
| 69 | irq->bus, irq->devfn >> 3, irq->devfn & 7); |
| 70 | |
| 71 | for (intx = 0; intx < MAX_INTX_ENTRIES; intx++) { |
| 72 | int link = irq->irq[intx].link; |
| 73 | int bitmap = irq->irq[intx].bitmap; |
| 74 | int irq = 0; |
| 75 | |
| 76 | debug("INT%c link: %x bitmap: %x ", |
| 77 | 'A' + intx, link, bitmap); |
| 78 | |
| 79 | if (!bitmap || !link) { |
| 80 | debug("not routed\n"); |
| 81 | irq_slot[intx] = irq; |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | /* translate link value to link number */ |
Bin Meng | a5a2003 | 2016-02-01 01:40:51 -0800 | [diff] [blame] | 86 | link = pirq_translate_link(dev, link); |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 87 | |
| 88 | /* yet not routed */ |
| 89 | if (!pirq[link]) { |
Simon Glass | 27a6eaa | 2017-01-16 07:04:18 -0700 | [diff] [blame] | 90 | irq = pirq_get_next_free_irq(dev, pirq, bitmap, |
| 91 | irq_already_routed); |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 92 | pirq[link] = irq; |
| 93 | } else { |
| 94 | irq = pirq[link]; |
| 95 | } |
| 96 | |
| 97 | debug("IRQ: %d\n", irq); |
| 98 | irq_slot[intx] = irq; |
| 99 | |
| 100 | /* Assign IRQ in the interrupt router */ |
Bin Meng | a5a2003 | 2016-02-01 01:40:51 -0800 | [diff] [blame] | 101 | pirq_assign_irq(dev, link, irq); |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* Bus, device, slots IRQs for {A,B,C,D} */ |
Bin Meng | da5d463 | 2015-07-15 16:23:40 +0800 | [diff] [blame] | 105 | pci_assign_irqs(irq->bus, irq->devfn >> 3, irq_slot); |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 106 | |
| 107 | irq++; |
| 108 | } |
| 109 | |
| 110 | for (i = 0; i < CONFIG_MAX_PIRQ_LINKS; i++) |
| 111 | debug("PIRQ%c: %d\n", 'A' + i, pirq[i]); |
| 112 | } |
| 113 | |
| 114 | u32 copy_pirq_routing_table(u32 addr, struct irq_routing_table *rt) |
| 115 | { |
Bin Meng | 7a01b87 | 2015-04-27 14:16:01 +0800 | [diff] [blame] | 116 | struct irq_routing_table *rom_rt; |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 117 | |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 118 | /* Align the table to be 16 byte aligned */ |
| 119 | addr = ALIGN(addr, 16); |
| 120 | |
| 121 | debug("Copying Interrupt Routing Table to 0x%x\n", addr); |
Simon Glass | aa0ed9d | 2017-01-16 07:03:42 -0700 | [diff] [blame] | 122 | memcpy((void *)(uintptr_t)addr, rt, rt->size); |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 123 | |
Bin Meng | 7a01b87 | 2015-04-27 14:16:01 +0800 | [diff] [blame] | 124 | /* |
| 125 | * We do the sanity check here against the copied table after memcpy, |
| 126 | * as something might go wrong after the memcpy, which is normally |
| 127 | * due to the F segment decode is not turned on to systeam RAM. |
| 128 | */ |
Simon Glass | aa0ed9d | 2017-01-16 07:03:42 -0700 | [diff] [blame] | 129 | rom_rt = (struct irq_routing_table *)(uintptr_t)addr; |
Bin Meng | 7a01b87 | 2015-04-27 14:16:01 +0800 | [diff] [blame] | 130 | if (rom_rt->signature != PIRQ_SIGNATURE || |
| 131 | rom_rt->version != PIRQ_VERSION || rom_rt->size % 16) { |
| 132 | printf("Interrupt Routing Table not valid\n"); |
| 133 | return addr; |
| 134 | } |
| 135 | |
Bin Meng | 45236ad | 2015-04-24 18:10:05 +0800 | [diff] [blame] | 136 | return addr + rt->size; |
| 137 | } |
Simon Glass | 25271ae | 2019-12-06 21:42:15 -0700 | [diff] [blame] | 138 | |
| 139 | ulong write_pirq_routing_table(ulong addr) |
| 140 | { |
| 141 | if (!gd->arch.pirq_routing_table) |
| 142 | return addr; |
| 143 | |
| 144 | return copy_pirq_routing_table(addr, gd->arch.pirq_routing_table); |
| 145 | } |