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