[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[f761f1eb] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[cc73a8a1] | 29 | /** @addtogroup genericmm
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[9179d0a] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[da1bafb] | 35 | * @brief Generic TLB shootdown algorithm.
|
---|
[2bb8648] | 36 | *
|
---|
| 37 | * The algorithm implemented here is based on the CMU TLB shootdown
|
---|
| 38 | * algorithm and is further simplified (e.g. all CPUs receive all TLB
|
---|
| 39 | * shootdown messages).
|
---|
[9179d0a] | 40 | */
|
---|
| 41 |
|
---|
[f761f1eb] | 42 | #include <mm/tlb.h>
|
---|
[d1e414c] | 43 | #include <mm/asid.h>
|
---|
[ce031f0] | 44 | #include <arch/mm/tlb.h>
|
---|
[4ffa9e0] | 45 | #include <smp/ipi.h>
|
---|
[169587a] | 46 | #include <synch/spinlock.h>
|
---|
[23684b7] | 47 | #include <atomic.h>
|
---|
[4ffa9e0] | 48 | #include <arch/interrupt.h>
|
---|
[169587a] | 49 | #include <config.h>
|
---|
[434f700] | 50 | #include <arch.h>
|
---|
[02055415] | 51 | #include <panic.h>
|
---|
[d1e414c] | 52 | #include <debug.h>
|
---|
[b3f8fb7] | 53 | #include <cpu.h>
|
---|
[f761f1eb] | 54 |
|
---|
[169587a] | 55 | void tlb_init(void)
|
---|
| 56 | {
|
---|
[b00fdde] | 57 | tlb_arch_init();
|
---|
[169587a] | 58 | }
|
---|
| 59 |
|
---|
[5f85c91] | 60 | #ifdef CONFIG_SMP
|
---|
[d1e414c] | 61 |
|
---|
[da1bafb] | 62 | /**
|
---|
| 63 | * This lock is used for synchronisation between sender and
|
---|
| 64 | * recipients of TLB shootdown message. It must be acquired
|
---|
| 65 | * before CPU structure lock.
|
---|
| 66 | *
|
---|
| 67 | */
|
---|
| 68 | IRQ_SPINLOCK_STATIC_INITIALIZE(tlblock);
|
---|
| 69 |
|
---|
[d1e414c] | 70 | /** Send TLB shootdown message.
|
---|
| 71 | *
|
---|
| 72 | * This function attempts to deliver TLB shootdown message
|
---|
| 73 | * to all other processors.
|
---|
| 74 | *
|
---|
| 75 | * This function must be called with interrupts disabled.
|
---|
| 76 | *
|
---|
| 77 | * @param type Type describing scope of shootdown.
|
---|
| 78 | * @param asid Address space, if required by type.
|
---|
| 79 | * @param page Virtual page address, if required by type.
|
---|
| 80 | * @param count Number of pages, if required by type.
|
---|
[da1bafb] | 81 | *
|
---|
[d1e414c] | 82 | */
|
---|
[4638401] | 83 | void tlb_shootdown_start(tlb_invalidate_type_t type, asid_t asid,
|
---|
[98000fb] | 84 | uintptr_t page, size_t count)
|
---|
[169587a] | 85 | {
|
---|
[da1bafb] | 86 | CPU->tlb_active = false;
|
---|
| 87 | irq_spinlock_lock(&tlblock, false);
|
---|
[4512d7e] | 88 |
|
---|
[da1bafb] | 89 | size_t i;
|
---|
[d1e414c] | 90 | for (i = 0; i < config.cpu_count; i++) {
|
---|
| 91 | cpu_t *cpu;
|
---|
| 92 |
|
---|
| 93 | if (i == CPU->id)
|
---|
| 94 | continue;
|
---|
[da1bafb] | 95 |
|
---|
[d1e414c] | 96 | cpu = &cpus[i];
|
---|
[da1bafb] | 97 | irq_spinlock_lock(&cpu->lock, false);
|
---|
[d1e414c] | 98 | if (cpu->tlb_messages_count == TLB_MESSAGE_QUEUE_LEN) {
|
---|
| 99 | /*
|
---|
| 100 | * The message queue is full.
|
---|
| 101 | * Erase the queue and store one TLB_INVL_ALL message.
|
---|
| 102 | */
|
---|
| 103 | cpu->tlb_messages_count = 1;
|
---|
| 104 | cpu->tlb_messages[0].type = TLB_INVL_ALL;
|
---|
| 105 | cpu->tlb_messages[0].asid = ASID_INVALID;
|
---|
| 106 | cpu->tlb_messages[0].page = 0;
|
---|
| 107 | cpu->tlb_messages[0].count = 0;
|
---|
| 108 | } else {
|
---|
| 109 | /*
|
---|
| 110 | * Enqueue the message.
|
---|
| 111 | */
|
---|
[98000fb] | 112 | size_t idx = cpu->tlb_messages_count++;
|
---|
[4638401] | 113 | cpu->tlb_messages[idx].type = type;
|
---|
| 114 | cpu->tlb_messages[idx].asid = asid;
|
---|
| 115 | cpu->tlb_messages[idx].page = page;
|
---|
| 116 | cpu->tlb_messages[idx].count = count;
|
---|
[d1e414c] | 117 | }
|
---|
[da1bafb] | 118 | irq_spinlock_unlock(&cpu->lock, false);
|
---|
[d1e414c] | 119 | }
|
---|
[36b01bb2] | 120 |
|
---|
[b109ebb] | 121 | tlb_shootdown_ipi_send();
|
---|
[da1bafb] | 122 |
|
---|
| 123 | busy_wait:
|
---|
[d1e414c] | 124 | for (i = 0; i < config.cpu_count; i++)
|
---|
[434f700] | 125 | if (cpus[i].tlb_active)
|
---|
| 126 | goto busy_wait;
|
---|
[169587a] | 127 | }
|
---|
| 128 |
|
---|
[da1bafb] | 129 | /** Finish TLB shootdown sequence.
|
---|
| 130 | *
|
---|
| 131 | */
|
---|
[b109ebb] | 132 | void tlb_shootdown_finalize(void)
|
---|
[169587a] | 133 | {
|
---|
[da1bafb] | 134 | irq_spinlock_unlock(&tlblock, false);
|
---|
| 135 | CPU->tlb_active = true;
|
---|
[169587a] | 136 | }
|
---|
| 137 |
|
---|
[4ffa9e0] | 138 | void tlb_shootdown_ipi_send(void)
|
---|
| 139 | {
|
---|
| 140 | ipi_broadcast(VECTOR_TLB_SHOOTDOWN_IPI);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[da1bafb] | 143 | /** Receive TLB shootdown message.
|
---|
| 144 | *
|
---|
| 145 | */
|
---|
[b109ebb] | 146 | void tlb_shootdown_ipi_recv(void)
|
---|
[f761f1eb] | 147 | {
|
---|
[97b64c9] | 148 | ASSERT(CPU);
|
---|
| 149 |
|
---|
[da1bafb] | 150 | CPU->tlb_active = false;
|
---|
| 151 | irq_spinlock_lock(&tlblock, false);
|
---|
| 152 | irq_spinlock_unlock(&tlblock, false);
|
---|
[d1e414c] | 153 |
|
---|
[da1bafb] | 154 | irq_spinlock_lock(&CPU->lock, false);
|
---|
[d1e414c] | 155 | ASSERT(CPU->tlb_messages_count <= TLB_MESSAGE_QUEUE_LEN);
|
---|
[da1bafb] | 156 |
|
---|
| 157 | size_t i;
|
---|
[d1e414c] | 158 | for (i = 0; i < CPU->tlb_messages_count; CPU->tlb_messages_count--) {
|
---|
[da1bafb] | 159 | tlb_invalidate_type_t type = CPU->tlb_messages[i].type;
|
---|
| 160 | asid_t asid = CPU->tlb_messages[i].asid;
|
---|
| 161 | uintptr_t page = CPU->tlb_messages[i].page;
|
---|
| 162 | size_t count = CPU->tlb_messages[i].count;
|
---|
| 163 |
|
---|
[d1e414c] | 164 | switch (type) {
|
---|
[00b38a3] | 165 | case TLB_INVL_ALL:
|
---|
[d1e414c] | 166 | tlb_invalidate_all();
|
---|
| 167 | break;
|
---|
[00b38a3] | 168 | case TLB_INVL_ASID:
|
---|
[d1e414c] | 169 | tlb_invalidate_asid(asid);
|
---|
| 170 | break;
|
---|
[00b38a3] | 171 | case TLB_INVL_PAGES:
|
---|
[da1bafb] | 172 | ASSERT(count);
|
---|
[d1e414c] | 173 | tlb_invalidate_pages(asid, page, count);
|
---|
| 174 | break;
|
---|
[00b38a3] | 175 | default:
|
---|
[f651e80] | 176 | panic("Unknown type (%d).", type);
|
---|
[d1e414c] | 177 | break;
|
---|
| 178 | }
|
---|
[da1bafb] | 179 |
|
---|
[d1e414c] | 180 | if (type == TLB_INVL_ALL)
|
---|
| 181 | break;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[da1bafb] | 184 | irq_spinlock_unlock(&CPU->lock, false);
|
---|
| 185 | CPU->tlb_active = true;
|
---|
[f761f1eb] | 186 | }
|
---|
[d1e414c] | 187 |
|
---|
[5f85c91] | 188 | #endif /* CONFIG_SMP */
|
---|
[b45c443] | 189 |
|
---|
[cc73a8a1] | 190 | /** @}
|
---|
[b45c443] | 191 | */
|
---|