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