Rev 1104 | Rev 1297 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | jermar | 1 | /* |
| 2 | * Copyright (C) 2001-2004 Jakub Jermar |
||
| 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 | |||
| 1248 | jermar | 29 | /** |
| 30 | * @file tlb.c |
||
| 31 | * @brief Generic TLB shootdown algorithm. |
||
| 32 | */ |
||
| 33 | |||
| 1 | jermar | 34 | #include <mm/tlb.h> |
| 1002 | jermar | 35 | #include <mm/asid.h> |
| 389 | jermar | 36 | #include <arch/mm/tlb.h> |
| 7 | jermar | 37 | #include <smp/ipi.h> |
| 5 | jermar | 38 | #include <synch/spinlock.h> |
| 39 | #include <typedefs.h> |
||
| 1104 | jermar | 40 | #include <atomic.h> |
| 7 | jermar | 41 | #include <arch/interrupt.h> |
| 5 | jermar | 42 | #include <config.h> |
| 31 | jermar | 43 | #include <arch.h> |
| 594 | jermar | 44 | #include <panic.h> |
| 1002 | jermar | 45 | #include <debug.h> |
| 1 | jermar | 46 | |
| 1002 | jermar | 47 | /** |
| 48 | * This lock is used for synchronisation between sender and |
||
| 49 | * recipients of TLB shootdown message. It must be acquired |
||
| 50 | * before CPU structure lock. |
||
| 51 | */ |
||
| 623 | jermar | 52 | SPINLOCK_INITIALIZE(tlblock); |
| 5 | jermar | 53 | |
| 54 | void tlb_init(void) |
||
| 1 | jermar | 55 | { |
| 569 | jermar | 56 | tlb_arch_init(); |
| 1 | jermar | 57 | } |
| 5 | jermar | 58 | |
| 458 | decky | 59 | #ifdef CONFIG_SMP |
| 1002 | jermar | 60 | |
| 61 | /** Send TLB shootdown message. |
||
| 62 | * |
||
| 63 | * This function attempts to deliver TLB shootdown message |
||
| 64 | * to all other processors. |
||
| 65 | * |
||
| 66 | * This function must be called with interrupts disabled. |
||
| 67 | * |
||
| 68 | * @param type Type describing scope of shootdown. |
||
| 69 | * @param asid Address space, if required by type. |
||
| 70 | * @param page Virtual page address, if required by type. |
||
| 71 | * @param count Number of pages, if required by type. |
||
| 72 | */ |
||
| 73 | void tlb_shootdown_start(tlb_invalidate_type_t type, asid_t asid, __address page, count_t count) |
||
| 5 | jermar | 74 | { |
| 31 | jermar | 75 | int i; |
| 76 | |||
| 77 | CPU->tlb_active = 0; |
||
| 5 | jermar | 78 | spinlock_lock(&tlblock); |
| 727 | jermar | 79 | |
| 1002 | jermar | 80 | for (i = 0; i < config.cpu_count; i++) { |
| 81 | cpu_t *cpu; |
||
| 82 | |||
| 83 | if (i == CPU->id) |
||
| 84 | continue; |
||
| 85 | |||
| 86 | cpu = &cpus[i]; |
||
| 87 | spinlock_lock(&cpu->lock); |
||
| 88 | if (cpu->tlb_messages_count == TLB_MESSAGE_QUEUE_LEN) { |
||
| 89 | /* |
||
| 90 | * The message queue is full. |
||
| 91 | * Erase the queue and store one TLB_INVL_ALL message. |
||
| 92 | */ |
||
| 93 | cpu->tlb_messages_count = 1; |
||
| 94 | cpu->tlb_messages[0].type = TLB_INVL_ALL; |
||
| 95 | cpu->tlb_messages[0].asid = ASID_INVALID; |
||
| 96 | cpu->tlb_messages[0].page = 0; |
||
| 97 | cpu->tlb_messages[0].count = 0; |
||
| 98 | } else { |
||
| 99 | /* |
||
| 100 | * Enqueue the message. |
||
| 101 | */ |
||
| 102 | cpu->tlb_messages[cpu->tlb_messages_count].type = type; |
||
| 103 | cpu->tlb_messages[cpu->tlb_messages_count].asid = asid; |
||
| 104 | cpu->tlb_messages[cpu->tlb_messages_count].page = page; |
||
| 105 | cpu->tlb_messages[cpu->tlb_messages_count].count = count; |
||
| 106 | cpu->tlb_messages_count++; |
||
| 107 | } |
||
| 108 | spinlock_unlock(&cpu->lock); |
||
| 109 | } |
||
| 740 | jermar | 110 | |
| 6 | jermar | 111 | tlb_shootdown_ipi_send(); |
| 727 | jermar | 112 | |
| 31 | jermar | 113 | busy_wait: |
| 1002 | jermar | 114 | for (i = 0; i < config.cpu_count; i++) |
| 31 | jermar | 115 | if (cpus[i].tlb_active) |
| 116 | goto busy_wait; |
||
| 5 | jermar | 117 | } |
| 118 | |||
| 1002 | jermar | 119 | /** Finish TLB shootdown sequence. */ |
| 6 | jermar | 120 | void tlb_shootdown_finalize(void) |
| 5 | jermar | 121 | { |
| 122 | spinlock_unlock(&tlblock); |
||
| 31 | jermar | 123 | CPU->tlb_active = 1; |
| 5 | jermar | 124 | } |
| 125 | |||
| 7 | jermar | 126 | void tlb_shootdown_ipi_send(void) |
| 127 | { |
||
| 128 | ipi_broadcast(VECTOR_TLB_SHOOTDOWN_IPI); |
||
| 129 | } |
||
| 130 | |||
| 1002 | jermar | 131 | /** Receive TLB shootdown message. */ |
| 6 | jermar | 132 | void tlb_shootdown_ipi_recv(void) |
| 5 | jermar | 133 | { |
| 1002 | jermar | 134 | tlb_invalidate_type_t type; |
| 135 | asid_t asid; |
||
| 136 | __address page; |
||
| 137 | count_t count; |
||
| 138 | int i; |
||
| 139 | |||
| 1008 | jermar | 140 | ASSERT(CPU); |
| 141 | |||
| 31 | jermar | 142 | CPU->tlb_active = 0; |
| 5 | jermar | 143 | spinlock_lock(&tlblock); |
| 144 | spinlock_unlock(&tlblock); |
||
| 1002 | jermar | 145 | |
| 146 | spinlock_lock(&CPU->lock); |
||
| 147 | ASSERT(CPU->tlb_messages_count <= TLB_MESSAGE_QUEUE_LEN); |
||
| 148 | |||
| 149 | for (i = 0; i < CPU->tlb_messages_count; CPU->tlb_messages_count--) { |
||
| 150 | type = CPU->tlb_messages[i].type; |
||
| 151 | asid = CPU->tlb_messages[i].asid; |
||
| 152 | page = CPU->tlb_messages[i].page; |
||
| 153 | count = CPU->tlb_messages[i].count; |
||
| 154 | |||
| 155 | switch (type) { |
||
| 156 | case TLB_INVL_ALL: |
||
| 157 | tlb_invalidate_all(); |
||
| 158 | break; |
||
| 159 | case TLB_INVL_ASID: |
||
| 160 | tlb_invalidate_asid(asid); |
||
| 161 | break; |
||
| 162 | case TLB_INVL_PAGES: |
||
| 163 | ASSERT(count); |
||
| 164 | tlb_invalidate_pages(asid, page, count); |
||
| 165 | break; |
||
| 166 | default: |
||
| 167 | panic("unknown type (%d)\n", type); |
||
| 168 | break; |
||
| 169 | } |
||
| 170 | if (type == TLB_INVL_ALL) |
||
| 171 | break; |
||
| 172 | } |
||
| 173 | |||
| 174 | spinlock_unlock(&CPU->lock); |
||
| 31 | jermar | 175 | CPU->tlb_active = 1; |
| 5 | jermar | 176 | } |
| 1002 | jermar | 177 | |
| 458 | decky | 178 | #endif /* CONFIG_SMP */ |