|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2021, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2021-05-20 bigmagic first version |
| 9 | + * 2022-09-16 WangXiaoyao Porting to rv64 |
| 10 | + */ |
| 11 | +#include <rtdef.h> |
| 12 | +#include "plic.h" |
| 13 | +#include <riscv_io.h> |
| 14 | + |
| 15 | +/* |
| 16 | + * Each interrupt source has a priority register associated with it. |
| 17 | + * We always hardwire it to one in Linux. |
| 18 | + */ |
| 19 | +#define PRIORITY_BASE 0 |
| 20 | +#define PRIORITY_PER_ID 4 |
| 21 | + |
| 22 | +/* |
| 23 | + * Each hart context has a vector of interrupt enable bits associated with it. |
| 24 | + * There's one bit for each interrupt source. |
| 25 | + */ |
| 26 | +#define CONTEXT_ENABLE_BASE 0x2000 |
| 27 | +#define CONTEXT_ENABLE_SIZE 0x80 |
| 28 | + |
| 29 | +/* |
| 30 | + * Each hart context has a set of control registers associated with it. Right |
| 31 | + * now there's only two: a source priority threshold over which the hart will |
| 32 | + * take an interrupt, and a register to claim interrupts. |
| 33 | + */ |
| 34 | +#define CONTEXT_BASE 0x200000 |
| 35 | +#define CONTEXT_SIZE 0x1000 |
| 36 | +#define CONTEXT_THRESHOLD 0x00 |
| 37 | +#define CONTEXT_CLAIM 0x04 |
| 38 | + |
| 39 | +static void plic_toggle(struct plic_handler *handler, unsigned int irq, int enable) |
| 40 | +{ |
| 41 | + void *reg = handler->enable_base + (irq / 32) * sizeof(unsigned int); |
| 42 | + unsigned int hwirq_mask = 1 << (irq % 32); |
| 43 | + |
| 44 | + if (enable) |
| 45 | + writel(readl(reg) | hwirq_mask, reg); |
| 46 | + else |
| 47 | + writel(readl(reg) & ~hwirq_mask, reg); |
| 48 | +} |
| 49 | + |
| 50 | +/* |
| 51 | + * Each PLIC interrupt source can be assigned a priority by writing |
| 52 | + * to its 32-bit memory-mapped priority register. |
| 53 | + * The QEMU-virt (the same as FU540-C000) supports 7 levels of priority. |
| 54 | + * A priority value of 0 is reserved to mean "never interrupt" and |
| 55 | + * effectively disables the interrupt. |
| 56 | + * Priority 1 is the lowest active priority, and priority 7 is the highest. |
| 57 | + * Ties between global interrupts of the same priority are broken by |
| 58 | + * the Interrupt ID; interrupts with the lowest ID have the highest |
| 59 | + * effective priority. |
| 60 | + */ |
| 61 | +void plic_set_priority(struct plic_handler *handler, int irq, int priority) |
| 62 | +{ |
| 63 | + writel(priority, handler->base + PRIORITY_BASE + irq * PRIORITY_PER_ID); |
| 64 | +} |
| 65 | + |
| 66 | +/* |
| 67 | + * Each global interrupt can be enabled by setting the corresponding |
| 68 | + * bit in the enables registers. |
| 69 | + */ |
| 70 | +void plic_irq_enable(struct plic_handler *handler, int irq) |
| 71 | +{ |
| 72 | + plic_toggle(handler, irq, 1); |
| 73 | +} |
| 74 | + |
| 75 | +void plic_irq_disable(struct plic_handler *handler, int irq) |
| 76 | +{ |
| 77 | + plic_toggle(handler, irq, 1); |
| 78 | +} |
| 79 | + |
| 80 | +/* |
| 81 | + * PLIC will mask all interrupts of a priority less than or equal to threshold. |
| 82 | + * Maximum threshold is 7. |
| 83 | + * For example, a threshold value of zero permits all interrupts with |
| 84 | + * non-zero priority, whereas a value of 7 masks all interrupts. |
| 85 | + * Notice, the threshold is global for PLIC, not for each interrupt source. |
| 86 | + */ |
| 87 | +void plic_set_threshold(struct plic_handler *handler, int threshold) |
| 88 | +{ |
| 89 | + writel(threshold, handler->hart_base + CONTEXT_THRESHOLD); |
| 90 | +} |
| 91 | + |
| 92 | +/* |
| 93 | + * DESCRIPTION: |
| 94 | + * Query the PLIC what interrupt we should serve. |
| 95 | + * Perform an interrupt claim by reading the claim register, which |
| 96 | + * returns the ID of the highest-priority pending interrupt or zero if there |
| 97 | + * is no pending interrupt. |
| 98 | + * A successful claim also atomically clears the corresponding pending bit |
| 99 | + * on the interrupt source. |
| 100 | + * RETURN VALUE: |
| 101 | + * the ID of the highest-priority pending interrupt or zero if there |
| 102 | + * is no pending interrupt. |
| 103 | + */ |
| 104 | +int plic_claim(struct plic_handler *handler) |
| 105 | +{ |
| 106 | + void *claim = handler->hart_base + CONTEXT_CLAIM; |
| 107 | + |
| 108 | + return readl(claim); |
| 109 | +} |
| 110 | + |
| 111 | +/* |
| 112 | + * DESCRIPTION: |
| 113 | + * Writing the interrupt ID it received from the claim (irq) to the |
| 114 | + * complete register would signal the PLIC we've served this IRQ. |
| 115 | + * The PLIC does not check whether the completion ID is the same as the |
| 116 | + * last claim ID for that target. If the completion ID does not match an |
| 117 | + * interrupt source that is currently enabled for the target, the completion |
| 118 | + * is silently ignored. |
| 119 | + * RETURN VALUE: none |
| 120 | + */ |
| 121 | +void plic_complete(struct plic_handler *handler, int irq) |
| 122 | +{ |
| 123 | + void *claim = handler->hart_base + CONTEXT_CLAIM; |
| 124 | + |
| 125 | + writel(irq, claim); |
| 126 | +} |
| 127 | + |
| 128 | +void plic_handler_init(struct plic_handler *handler, void *base, unsigned int context_id) |
| 129 | +{ |
| 130 | + handler->base = base; |
| 131 | + handler->hart_base = base + CONTEXT_BASE + context_id * CONTEXT_SIZE; |
| 132 | + handler->enable_base = base + CONTEXT_ENABLE_BASE + context_id * CONTEXT_ENABLE_SIZE; |
| 133 | +} |
0 commit comments