Rev 1104 | Rev 1297 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1104 | Rev 1248 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
- | 29 | /** |
|
- | 30 | * @file tlb.c |
|
- | 31 | * @brief Generic TLB shootdown algorithm. |
|
- | 32 | */ |
|
- | 33 | ||
29 | #include <mm/tlb.h> |
34 | #include <mm/tlb.h> |
30 | #include <mm/asid.h> |
35 | #include <mm/asid.h> |
31 | #include <arch/mm/tlb.h> |
36 | #include <arch/mm/tlb.h> |
32 | #include <smp/ipi.h> |
37 | #include <smp/ipi.h> |
33 | #include <synch/spinlock.h> |
38 | #include <synch/spinlock.h> |
34 | #include <typedefs.h> |
39 | #include <typedefs.h> |
35 | #include <atomic.h> |
40 | #include <atomic.h> |
36 | #include <arch/interrupt.h> |
41 | #include <arch/interrupt.h> |
37 | #include <config.h> |
42 | #include <config.h> |
38 | #include <arch.h> |
43 | #include <arch.h> |
39 | #include <panic.h> |
44 | #include <panic.h> |
40 | #include <debug.h> |
45 | #include <debug.h> |
41 | 46 | ||
42 | /** |
47 | /** |
43 | * This lock is used for synchronisation between sender and |
48 | * This lock is used for synchronisation between sender and |
44 | * recipients of TLB shootdown message. It must be acquired |
49 | * recipients of TLB shootdown message. It must be acquired |
45 | * before CPU structure lock. |
50 | * before CPU structure lock. |
46 | */ |
51 | */ |
47 | SPINLOCK_INITIALIZE(tlblock); |
52 | SPINLOCK_INITIALIZE(tlblock); |
48 | 53 | ||
49 | void tlb_init(void) |
54 | void tlb_init(void) |
50 | { |
55 | { |
51 | tlb_arch_init(); |
56 | tlb_arch_init(); |
52 | } |
57 | } |
53 | 58 | ||
54 | #ifdef CONFIG_SMP |
59 | #ifdef CONFIG_SMP |
55 | 60 | ||
56 | /** Send TLB shootdown message. |
61 | /** Send TLB shootdown message. |
57 | * |
62 | * |
58 | * This function attempts to deliver TLB shootdown message |
63 | * This function attempts to deliver TLB shootdown message |
59 | * to all other processors. |
64 | * to all other processors. |
60 | * |
65 | * |
61 | * This function must be called with interrupts disabled. |
66 | * This function must be called with interrupts disabled. |
62 | * |
67 | * |
63 | * @param type Type describing scope of shootdown. |
68 | * @param type Type describing scope of shootdown. |
64 | * @param asid Address space, if required by type. |
69 | * @param asid Address space, if required by type. |
65 | * @param page Virtual page address, if required by type. |
70 | * @param page Virtual page address, if required by type. |
66 | * @param count Number of pages, if required by type. |
71 | * @param count Number of pages, if required by type. |
67 | */ |
72 | */ |
68 | void tlb_shootdown_start(tlb_invalidate_type_t type, asid_t asid, __address page, count_t count) |
73 | void tlb_shootdown_start(tlb_invalidate_type_t type, asid_t asid, __address page, count_t count) |
69 | { |
74 | { |
70 | int i; |
75 | int i; |
71 | 76 | ||
72 | CPU->tlb_active = 0; |
77 | CPU->tlb_active = 0; |
73 | spinlock_lock(&tlblock); |
78 | spinlock_lock(&tlblock); |
74 | 79 | ||
75 | for (i = 0; i < config.cpu_count; i++) { |
80 | for (i = 0; i < config.cpu_count; i++) { |
76 | cpu_t *cpu; |
81 | cpu_t *cpu; |
77 | 82 | ||
78 | if (i == CPU->id) |
83 | if (i == CPU->id) |
79 | continue; |
84 | continue; |
80 | 85 | ||
81 | cpu = &cpus[i]; |
86 | cpu = &cpus[i]; |
82 | spinlock_lock(&cpu->lock); |
87 | spinlock_lock(&cpu->lock); |
83 | if (cpu->tlb_messages_count == TLB_MESSAGE_QUEUE_LEN) { |
88 | if (cpu->tlb_messages_count == TLB_MESSAGE_QUEUE_LEN) { |
84 | /* |
89 | /* |
85 | * The message queue is full. |
90 | * The message queue is full. |
86 | * Erase the queue and store one TLB_INVL_ALL message. |
91 | * Erase the queue and store one TLB_INVL_ALL message. |
87 | */ |
92 | */ |
88 | cpu->tlb_messages_count = 1; |
93 | cpu->tlb_messages_count = 1; |
89 | cpu->tlb_messages[0].type = TLB_INVL_ALL; |
94 | cpu->tlb_messages[0].type = TLB_INVL_ALL; |
90 | cpu->tlb_messages[0].asid = ASID_INVALID; |
95 | cpu->tlb_messages[0].asid = ASID_INVALID; |
91 | cpu->tlb_messages[0].page = 0; |
96 | cpu->tlb_messages[0].page = 0; |
92 | cpu->tlb_messages[0].count = 0; |
97 | cpu->tlb_messages[0].count = 0; |
93 | } else { |
98 | } else { |
94 | /* |
99 | /* |
95 | * Enqueue the message. |
100 | * Enqueue the message. |
96 | */ |
101 | */ |
97 | cpu->tlb_messages[cpu->tlb_messages_count].type = type; |
102 | cpu->tlb_messages[cpu->tlb_messages_count].type = type; |
98 | cpu->tlb_messages[cpu->tlb_messages_count].asid = asid; |
103 | cpu->tlb_messages[cpu->tlb_messages_count].asid = asid; |
99 | cpu->tlb_messages[cpu->tlb_messages_count].page = page; |
104 | cpu->tlb_messages[cpu->tlb_messages_count].page = page; |
100 | cpu->tlb_messages[cpu->tlb_messages_count].count = count; |
105 | cpu->tlb_messages[cpu->tlb_messages_count].count = count; |
101 | cpu->tlb_messages_count++; |
106 | cpu->tlb_messages_count++; |
102 | } |
107 | } |
103 | spinlock_unlock(&cpu->lock); |
108 | spinlock_unlock(&cpu->lock); |
104 | } |
109 | } |
105 | 110 | ||
106 | tlb_shootdown_ipi_send(); |
111 | tlb_shootdown_ipi_send(); |
107 | 112 | ||
108 | busy_wait: |
113 | busy_wait: |
109 | for (i = 0; i < config.cpu_count; i++) |
114 | for (i = 0; i < config.cpu_count; i++) |
110 | if (cpus[i].tlb_active) |
115 | if (cpus[i].tlb_active) |
111 | goto busy_wait; |
116 | goto busy_wait; |
112 | } |
117 | } |
113 | 118 | ||
114 | /** Finish TLB shootdown sequence. */ |
119 | /** Finish TLB shootdown sequence. */ |
115 | void tlb_shootdown_finalize(void) |
120 | void tlb_shootdown_finalize(void) |
116 | { |
121 | { |
117 | spinlock_unlock(&tlblock); |
122 | spinlock_unlock(&tlblock); |
118 | CPU->tlb_active = 1; |
123 | CPU->tlb_active = 1; |
119 | } |
124 | } |
120 | 125 | ||
121 | void tlb_shootdown_ipi_send(void) |
126 | void tlb_shootdown_ipi_send(void) |
122 | { |
127 | { |
123 | ipi_broadcast(VECTOR_TLB_SHOOTDOWN_IPI); |
128 | ipi_broadcast(VECTOR_TLB_SHOOTDOWN_IPI); |
124 | } |
129 | } |
125 | 130 | ||
126 | /** Receive TLB shootdown message. */ |
131 | /** Receive TLB shootdown message. */ |
127 | void tlb_shootdown_ipi_recv(void) |
132 | void tlb_shootdown_ipi_recv(void) |
128 | { |
133 | { |
129 | tlb_invalidate_type_t type; |
134 | tlb_invalidate_type_t type; |
130 | asid_t asid; |
135 | asid_t asid; |
131 | __address page; |
136 | __address page; |
132 | count_t count; |
137 | count_t count; |
133 | int i; |
138 | int i; |
134 | 139 | ||
135 | ASSERT(CPU); |
140 | ASSERT(CPU); |
136 | 141 | ||
137 | CPU->tlb_active = 0; |
142 | CPU->tlb_active = 0; |
138 | spinlock_lock(&tlblock); |
143 | spinlock_lock(&tlblock); |
139 | spinlock_unlock(&tlblock); |
144 | spinlock_unlock(&tlblock); |
140 | 145 | ||
141 | spinlock_lock(&CPU->lock); |
146 | spinlock_lock(&CPU->lock); |
142 | ASSERT(CPU->tlb_messages_count <= TLB_MESSAGE_QUEUE_LEN); |
147 | ASSERT(CPU->tlb_messages_count <= TLB_MESSAGE_QUEUE_LEN); |
143 | 148 | ||
144 | for (i = 0; i < CPU->tlb_messages_count; CPU->tlb_messages_count--) { |
149 | for (i = 0; i < CPU->tlb_messages_count; CPU->tlb_messages_count--) { |
145 | type = CPU->tlb_messages[i].type; |
150 | type = CPU->tlb_messages[i].type; |
146 | asid = CPU->tlb_messages[i].asid; |
151 | asid = CPU->tlb_messages[i].asid; |
147 | page = CPU->tlb_messages[i].page; |
152 | page = CPU->tlb_messages[i].page; |
148 | count = CPU->tlb_messages[i].count; |
153 | count = CPU->tlb_messages[i].count; |
149 | 154 | ||
150 | switch (type) { |
155 | switch (type) { |
151 | case TLB_INVL_ALL: |
156 | case TLB_INVL_ALL: |
152 | tlb_invalidate_all(); |
157 | tlb_invalidate_all(); |
153 | break; |
158 | break; |
154 | case TLB_INVL_ASID: |
159 | case TLB_INVL_ASID: |
155 | tlb_invalidate_asid(asid); |
160 | tlb_invalidate_asid(asid); |
156 | break; |
161 | break; |
157 | case TLB_INVL_PAGES: |
162 | case TLB_INVL_PAGES: |
158 | ASSERT(count); |
163 | ASSERT(count); |
159 | tlb_invalidate_pages(asid, page, count); |
164 | tlb_invalidate_pages(asid, page, count); |
160 | break; |
165 | break; |
161 | default: |
166 | default: |
162 | panic("unknown type (%d)\n", type); |
167 | panic("unknown type (%d)\n", type); |
163 | break; |
168 | break; |
164 | } |
169 | } |
165 | if (type == TLB_INVL_ALL) |
170 | if (type == TLB_INVL_ALL) |
166 | break; |
171 | break; |
167 | } |
172 | } |
168 | 173 | ||
169 | spinlock_unlock(&CPU->lock); |
174 | spinlock_unlock(&CPU->lock); |
170 | CPU->tlb_active = 1; |
175 | CPU->tlb_active = 1; |
171 | } |
176 | } |
172 | 177 | ||
173 | #endif /* CONFIG_SMP */ |
178 | #endif /* CONFIG_SMP */ |
174 | 179 |