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