This source file includes following definitions.
- mdesc_adi_init
- find_tag_store
- alloc_tag_store
- del_tag_store
- adi_restore_tags
- adi_save_tags
1
2
3
4
5
6
7
8
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/mm_types.h>
12 #include <asm/mdesc.h>
13 #include <asm/adi_64.h>
14 #include <asm/mmu_64.h>
15 #include <asm/pgtable_64.h>
16
17
18
19
20
21
22
23
24 #define TAG_STORAGE_PAGES 8
25
26 struct adi_config adi_state;
27 EXPORT_SYMBOL(adi_state);
28
29
30
31
32
33
34
35
36
37
38 void __init mdesc_adi_init(void)
39 {
40 struct mdesc_handle *hp = mdesc_grab();
41 const char *prop;
42 u64 pn, *val;
43 int len;
44
45 if (!hp)
46 goto adi_not_found;
47
48 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "cpu");
49 if (pn == MDESC_NODE_NULL)
50 goto adi_not_found;
51
52 prop = mdesc_get_property(hp, pn, "hwcap-list", &len);
53 if (!prop)
54 goto adi_not_found;
55
56
57
58
59
60 adi_state.enabled = false;
61 while (len) {
62 int plen;
63
64 if (!strcmp(prop, "adp")) {
65 adi_state.enabled = true;
66 break;
67 }
68
69 plen = strlen(prop) + 1;
70 prop += plen;
71 len -= plen;
72 }
73
74 if (!adi_state.enabled)
75 goto adi_not_found;
76
77
78
79
80
81 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
82 if (pn == MDESC_NODE_NULL)
83 goto adi_not_found;
84
85 val = (u64 *) mdesc_get_property(hp, pn, "adp-blksz", &len);
86 if (!val)
87 goto adi_not_found;
88 adi_state.caps.blksz = *val;
89
90 val = (u64 *) mdesc_get_property(hp, pn, "adp-nbits", &len);
91 if (!val)
92 goto adi_not_found;
93 adi_state.caps.nbits = *val;
94
95 val = (u64 *) mdesc_get_property(hp, pn, "ue-on-adp", &len);
96 if (!val)
97 goto adi_not_found;
98 adi_state.caps.ue_on_adi = *val;
99
100
101
102
103
104
105
106
107
108 if (adi_state.caps.nbits > 4) {
109 pr_warn("WARNING: ADI tag size >4 on this platform. Disabling AADI support\n");
110 adi_state.enabled = false;
111 }
112
113 mdesc_release(hp);
114 return;
115
116 adi_not_found:
117 adi_state.enabled = false;
118 adi_state.caps.blksz = 0;
119 adi_state.caps.nbits = 0;
120 if (hp)
121 mdesc_release(hp);
122 }
123
124 tag_storage_desc_t *find_tag_store(struct mm_struct *mm,
125 struct vm_area_struct *vma,
126 unsigned long addr)
127 {
128 tag_storage_desc_t *tag_desc = NULL;
129 unsigned long i, max_desc, flags;
130
131
132
133
134 max_desc = PAGE_SIZE/sizeof(tag_storage_desc_t);
135 if (mm->context.tag_store) {
136 tag_desc = mm->context.tag_store;
137 spin_lock_irqsave(&mm->context.tag_lock, flags);
138 for (i = 0; i < max_desc; i++) {
139 if ((addr >= tag_desc->start) &&
140 ((addr + PAGE_SIZE - 1) <= tag_desc->end))
141 break;
142 tag_desc++;
143 }
144 spin_unlock_irqrestore(&mm->context.tag_lock, flags);
145
146
147
148
149 if (i >= max_desc)
150 tag_desc = NULL;
151 }
152
153 return tag_desc;
154 }
155
156 tag_storage_desc_t *alloc_tag_store(struct mm_struct *mm,
157 struct vm_area_struct *vma,
158 unsigned long addr)
159 {
160 unsigned char *tags;
161 unsigned long i, size, max_desc, flags;
162 tag_storage_desc_t *tag_desc, *open_desc;
163 unsigned long end_addr, hole_start, hole_end;
164
165 max_desc = PAGE_SIZE/sizeof(tag_storage_desc_t);
166 open_desc = NULL;
167 hole_start = 0;
168 hole_end = ULONG_MAX;
169 end_addr = addr + PAGE_SIZE - 1;
170
171
172
173
174 spin_lock_irqsave(&mm->context.tag_lock, flags);
175 if (mm->context.tag_store) {
176 tag_desc = mm->context.tag_store;
177
178
179
180
181
182
183 for (i = 0; i < max_desc; i++) {
184 if (tag_desc->tag_users == 0) {
185 if (open_desc == NULL)
186 open_desc = tag_desc;
187 } else {
188 if ((addr >= tag_desc->start) &&
189 (tag_desc->end >= (addr + PAGE_SIZE - 1))) {
190 tag_desc->tag_users++;
191 goto out;
192 }
193 }
194 if ((tag_desc->start > end_addr) &&
195 (tag_desc->start < hole_end))
196 hole_end = tag_desc->start;
197 if ((tag_desc->end < addr) &&
198 (tag_desc->end > hole_start))
199 hole_start = tag_desc->end;
200 tag_desc++;
201 }
202
203 } else {
204 size = sizeof(tag_storage_desc_t)*max_desc;
205 mm->context.tag_store = kzalloc(size, GFP_NOWAIT|__GFP_NOWARN);
206 if (mm->context.tag_store == NULL) {
207 tag_desc = NULL;
208 goto out;
209 }
210 tag_desc = mm->context.tag_store;
211 for (i = 0; i < max_desc; i++, tag_desc++)
212 tag_desc->tag_users = 0;
213 open_desc = mm->context.tag_store;
214 i = 0;
215 }
216
217
218 if (open_desc == NULL) {
219 tag_desc = NULL;
220 goto out;
221 }
222
223
224 tag_desc = open_desc;
225 tag_desc->tag_users = 1;
226
227
228
229
230
231
232
233
234
235
236
237
238 size = TAG_STORAGE_PAGES * PAGE_SIZE;
239 end_addr = addr + (size*2*adi_blksize()) - 1;
240
241 if (end_addr < addr) {
242 size = PAGE_SIZE;
243 end_addr = addr + (size*2*adi_blksize()) - 1;
244
245
246
247
248 if (end_addr < addr)
249 end_addr = ULONG_MAX;
250 }
251 if (hole_end < end_addr) {
252
253
254
255
256 unsigned long tmp_addr;
257
258 end_addr = hole_end - 1;
259 tmp_addr = end_addr - (size*2*adi_blksize()) + 1;
260
261
262
263 if (tmp_addr > addr) {
264 size = PAGE_SIZE;
265 tmp_addr = end_addr - (size*2*adi_blksize()) - 1;
266
267
268
269
270 if (tmp_addr > addr)
271 tmp_addr = 0;
272 }
273 if (tmp_addr < hole_start) {
274
275
276
277 tmp_addr = hole_start + 1;
278 }
279 addr = tmp_addr;
280 size = (end_addr + 1 - addr)/(2*adi_blksize());
281 size = (size + (PAGE_SIZE-adi_blksize()))/PAGE_SIZE;
282 size = size * PAGE_SIZE;
283 }
284 tags = kzalloc(size, GFP_NOWAIT|__GFP_NOWARN);
285 if (tags == NULL) {
286 tag_desc->tag_users = 0;
287 tag_desc = NULL;
288 goto out;
289 }
290 tag_desc->start = addr;
291 tag_desc->tags = tags;
292 tag_desc->end = end_addr;
293
294 out:
295 spin_unlock_irqrestore(&mm->context.tag_lock, flags);
296 return tag_desc;
297 }
298
299 void del_tag_store(tag_storage_desc_t *tag_desc, struct mm_struct *mm)
300 {
301 unsigned long flags;
302 unsigned char *tags = NULL;
303
304 spin_lock_irqsave(&mm->context.tag_lock, flags);
305 tag_desc->tag_users--;
306 if (tag_desc->tag_users == 0) {
307 tag_desc->start = tag_desc->end = 0;
308
309
310
311
312 if (tag_desc != mm->context.tag_store) {
313 tags = tag_desc->tags;
314 tag_desc->tags = NULL;
315 }
316 }
317 spin_unlock_irqrestore(&mm->context.tag_lock, flags);
318 kfree(tags);
319 }
320
321 #define tag_start(addr, tag_desc) \
322 ((tag_desc)->tags + ((addr - (tag_desc)->start)/(2*adi_blksize())))
323
324
325
326
327 void adi_restore_tags(struct mm_struct *mm, struct vm_area_struct *vma,
328 unsigned long addr, pte_t pte)
329 {
330 unsigned char *tag;
331 tag_storage_desc_t *tag_desc;
332 unsigned long paddr, tmp, version1, version2;
333
334
335
336
337
338 tag_desc = find_tag_store(mm, vma, addr);
339 if (tag_desc == NULL)
340 return;
341
342 tag = tag_start(addr, tag_desc);
343 paddr = pte_val(pte) & _PAGE_PADDR_4V;
344 for (tmp = paddr; tmp < (paddr+PAGE_SIZE); tmp += adi_blksize()) {
345 version1 = (*tag) >> 4;
346 version2 = (*tag) & 0x0f;
347 *tag++ = 0;
348 asm volatile("stxa %0, [%1] %2\n\t"
349 :
350 : "r" (version1), "r" (tmp),
351 "i" (ASI_MCD_REAL));
352 tmp += adi_blksize();
353 asm volatile("stxa %0, [%1] %2\n\t"
354 :
355 : "r" (version2), "r" (tmp),
356 "i" (ASI_MCD_REAL));
357 }
358 asm volatile("membar #Sync\n\t");
359
360
361
362
363 del_tag_store(tag_desc, mm);
364 }
365
366
367
368
369
370 int adi_save_tags(struct mm_struct *mm, struct vm_area_struct *vma,
371 unsigned long addr, pte_t oldpte)
372 {
373 unsigned char *tag;
374 tag_storage_desc_t *tag_desc;
375 unsigned long version1, version2, paddr, tmp;
376
377 tag_desc = alloc_tag_store(mm, vma, addr);
378 if (tag_desc == NULL)
379 return -1;
380
381 tag = tag_start(addr, tag_desc);
382 paddr = pte_val(oldpte) & _PAGE_PADDR_4V;
383 for (tmp = paddr; tmp < (paddr+PAGE_SIZE); tmp += adi_blksize()) {
384 asm volatile("ldxa [%1] %2, %0\n\t"
385 : "=r" (version1)
386 : "r" (tmp), "i" (ASI_MCD_REAL));
387 tmp += adi_blksize();
388 asm volatile("ldxa [%1] %2, %0\n\t"
389 : "=r" (version2)
390 : "r" (tmp), "i" (ASI_MCD_REAL));
391 *tag = (version1 << 4) | version2;
392 tag++;
393 }
394
395 return 0;
396 }