1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#define pr_fmt(fmt) "ACPI PPTT: " fmt
19
20#include <linux/acpi.h>
21#include <linux/cacheinfo.h>
22#include <acpi/processor.h>
23
24static struct acpi_subtable_header *fetch_pptt_subtable(struct acpi_table_header *table_hdr,
25 u32 pptt_ref)
26{
27 struct acpi_subtable_header *entry;
28
29
30 if (pptt_ref < sizeof(struct acpi_subtable_header))
31 return NULL;
32
33 if (pptt_ref + sizeof(struct acpi_subtable_header) > table_hdr->length)
34 return NULL;
35
36 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr, pptt_ref);
37
38 if (entry->length == 0)
39 return NULL;
40
41 if (pptt_ref + entry->length > table_hdr->length)
42 return NULL;
43
44 return entry;
45}
46
47static struct acpi_pptt_processor *fetch_pptt_node(struct acpi_table_header *table_hdr,
48 u32 pptt_ref)
49{
50 return (struct acpi_pptt_processor *)fetch_pptt_subtable(table_hdr, pptt_ref);
51}
52
53static struct acpi_pptt_cache *fetch_pptt_cache(struct acpi_table_header *table_hdr,
54 u32 pptt_ref)
55{
56 return (struct acpi_pptt_cache *)fetch_pptt_subtable(table_hdr, pptt_ref);
57}
58
59static struct acpi_subtable_header *acpi_get_pptt_resource(struct acpi_table_header *table_hdr,
60 struct acpi_pptt_processor *node,
61 int resource)
62{
63 u32 *ref;
64
65 if (resource >= node->number_of_priv_resources)
66 return NULL;
67
68 ref = ACPI_ADD_PTR(u32, node, sizeof(struct acpi_pptt_processor));
69 ref += resource;
70
71 return fetch_pptt_subtable(table_hdr, *ref);
72}
73
74static inline bool acpi_pptt_match_type(int table_type, int type)
75{
76 return ((table_type & ACPI_PPTT_MASK_CACHE_TYPE) == type ||
77 table_type & ACPI_PPTT_CACHE_TYPE_UNIFIED & type);
78}
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101static int acpi_pptt_walk_cache(struct acpi_table_header *table_hdr,
102 int local_level,
103 struct acpi_subtable_header *res,
104 struct acpi_pptt_cache **found,
105 int level, int type)
106{
107 struct acpi_pptt_cache *cache;
108
109 if (res->type != ACPI_PPTT_TYPE_CACHE)
110 return 0;
111
112 cache = (struct acpi_pptt_cache *) res;
113 while (cache) {
114 local_level++;
115
116 if (local_level == level &&
117 cache->flags & ACPI_PPTT_CACHE_TYPE_VALID &&
118 acpi_pptt_match_type(cache->attributes, type)) {
119 if (*found != NULL && cache != *found)
120 pr_warn("Found duplicate cache level/type unable to determine uniqueness\n");
121
122 pr_debug("Found cache @ level %d\n", level);
123 *found = cache;
124
125
126
127
128
129 }
130 cache = fetch_pptt_cache(table_hdr, cache->next_level_of_cache);
131 }
132 return local_level;
133}
134
135static struct acpi_pptt_cache *acpi_find_cache_level(struct acpi_table_header *table_hdr,
136 struct acpi_pptt_processor *cpu_node,
137 int *starting_level, int level,
138 int type)
139{
140 struct acpi_subtable_header *res;
141 int number_of_levels = *starting_level;
142 int resource = 0;
143 struct acpi_pptt_cache *ret = NULL;
144 int local_level;
145
146
147 while ((res = acpi_get_pptt_resource(table_hdr, cpu_node, resource))) {
148 resource++;
149
150 local_level = acpi_pptt_walk_cache(table_hdr, *starting_level,
151 res, &ret, level, type);
152
153
154
155
156
157 if (number_of_levels < local_level)
158 number_of_levels = local_level;
159 }
160 if (number_of_levels > *starting_level)
161 *starting_level = number_of_levels;
162
163 return ret;
164}
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179static int acpi_count_levels(struct acpi_table_header *table_hdr,
180 struct acpi_pptt_processor *cpu_node)
181{
182 int total_levels = 0;
183
184 do {
185 acpi_find_cache_level(table_hdr, cpu_node, &total_levels, 0, 0);
186 cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
187 } while (cpu_node);
188
189 return total_levels;
190}
191
192
193
194
195
196
197
198
199
200
201
202
203static int acpi_pptt_leaf_node(struct acpi_table_header *table_hdr,
204 struct acpi_pptt_processor *node)
205{
206 struct acpi_subtable_header *entry;
207 unsigned long table_end;
208 u32 node_entry;
209 struct acpi_pptt_processor *cpu_node;
210 u32 proc_sz;
211
212 table_end = (unsigned long)table_hdr + table_hdr->length;
213 node_entry = ACPI_PTR_DIFF(node, table_hdr);
214 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
215 sizeof(struct acpi_table_pptt));
216 proc_sz = sizeof(struct acpi_pptt_processor *);
217
218 while ((unsigned long)entry + proc_sz < table_end) {
219 cpu_node = (struct acpi_pptt_processor *)entry;
220 if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
221 cpu_node->parent == node_entry)
222 return 0;
223 if (entry->length == 0)
224 return 0;
225 entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
226 entry->length);
227
228 }
229 return 1;
230}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246static struct acpi_pptt_processor *acpi_find_processor_node(struct acpi_table_header *table_hdr,
247 u32 acpi_cpu_id)
248{
249 struct acpi_subtable_header *entry;
250 unsigned long table_end;
251 struct acpi_pptt_processor *cpu_node;
252 u32 proc_sz;
253
254 table_end = (unsigned long)table_hdr + table_hdr->length;
255 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
256 sizeof(struct acpi_table_pptt));
257 proc_sz = sizeof(struct acpi_pptt_processor *);
258
259
260 while ((unsigned long)entry + proc_sz < table_end) {
261 cpu_node = (struct acpi_pptt_processor *)entry;
262
263 if (entry->length == 0) {
264 pr_warn("Invalid zero length subtable\n");
265 break;
266 }
267 if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
268 acpi_cpu_id == cpu_node->acpi_processor_id &&
269 acpi_pptt_leaf_node(table_hdr, cpu_node)) {
270 return (struct acpi_pptt_processor *)entry;
271 }
272
273 entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
274 entry->length);
275 }
276
277 return NULL;
278}
279
280static int acpi_find_cache_levels(struct acpi_table_header *table_hdr,
281 u32 acpi_cpu_id)
282{
283 int number_of_levels = 0;
284 struct acpi_pptt_processor *cpu;
285
286 cpu = acpi_find_processor_node(table_hdr, acpi_cpu_id);
287 if (cpu)
288 number_of_levels = acpi_count_levels(table_hdr, cpu);
289
290 return number_of_levels;
291}
292
293static u8 acpi_cache_type(enum cache_type type)
294{
295 switch (type) {
296 case CACHE_TYPE_DATA:
297 pr_debug("Looking for data cache\n");
298 return ACPI_PPTT_CACHE_TYPE_DATA;
299 case CACHE_TYPE_INST:
300 pr_debug("Looking for instruction cache\n");
301 return ACPI_PPTT_CACHE_TYPE_INSTR;
302 default:
303 case CACHE_TYPE_UNIFIED:
304 pr_debug("Looking for unified cache\n");
305
306
307
308
309
310
311 return ACPI_PPTT_CACHE_TYPE_UNIFIED;
312 }
313}
314
315static struct acpi_pptt_cache *acpi_find_cache_node(struct acpi_table_header *table_hdr,
316 u32 acpi_cpu_id,
317 enum cache_type type,
318 unsigned int level,
319 struct acpi_pptt_processor **node)
320{
321 int total_levels = 0;
322 struct acpi_pptt_cache *found = NULL;
323 struct acpi_pptt_processor *cpu_node;
324 u8 acpi_type = acpi_cache_type(type);
325
326 pr_debug("Looking for CPU %d's level %d cache type %d\n",
327 acpi_cpu_id, level, acpi_type);
328
329 cpu_node = acpi_find_processor_node(table_hdr, acpi_cpu_id);
330
331 while (cpu_node && !found) {
332 found = acpi_find_cache_level(table_hdr, cpu_node,
333 &total_levels, level, acpi_type);
334 *node = cpu_node;
335 cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
336 }
337
338 return found;
339}
340
341
342#define PPTT_CHECKED_ATTRIBUTES 4
343
344
345
346
347
348
349
350
351
352
353
354
355
356static void update_cache_properties(struct cacheinfo *this_leaf,
357 struct acpi_pptt_cache *found_cache,
358 struct acpi_pptt_processor *cpu_node)
359{
360 int valid_flags = 0;
361
362 this_leaf->fw_token = cpu_node;
363 if (found_cache->flags & ACPI_PPTT_SIZE_PROPERTY_VALID) {
364 this_leaf->size = found_cache->size;
365 valid_flags++;
366 }
367 if (found_cache->flags & ACPI_PPTT_LINE_SIZE_VALID) {
368 this_leaf->coherency_line_size = found_cache->line_size;
369 valid_flags++;
370 }
371 if (found_cache->flags & ACPI_PPTT_NUMBER_OF_SETS_VALID) {
372 this_leaf->number_of_sets = found_cache->number_of_sets;
373 valid_flags++;
374 }
375 if (found_cache->flags & ACPI_PPTT_ASSOCIATIVITY_VALID) {
376 this_leaf->ways_of_associativity = found_cache->associativity;
377 valid_flags++;
378 }
379 if (found_cache->flags & ACPI_PPTT_WRITE_POLICY_VALID) {
380 switch (found_cache->attributes & ACPI_PPTT_MASK_WRITE_POLICY) {
381 case ACPI_PPTT_CACHE_POLICY_WT:
382 this_leaf->attributes = CACHE_WRITE_THROUGH;
383 break;
384 case ACPI_PPTT_CACHE_POLICY_WB:
385 this_leaf->attributes = CACHE_WRITE_BACK;
386 break;
387 }
388 }
389 if (found_cache->flags & ACPI_PPTT_ALLOCATION_TYPE_VALID) {
390 switch (found_cache->attributes & ACPI_PPTT_MASK_ALLOCATION_TYPE) {
391 case ACPI_PPTT_CACHE_READ_ALLOCATE:
392 this_leaf->attributes |= CACHE_READ_ALLOCATE;
393 break;
394 case ACPI_PPTT_CACHE_WRITE_ALLOCATE:
395 this_leaf->attributes |= CACHE_WRITE_ALLOCATE;
396 break;
397 case ACPI_PPTT_CACHE_RW_ALLOCATE:
398 case ACPI_PPTT_CACHE_RW_ALLOCATE_ALT:
399 this_leaf->attributes |=
400 CACHE_READ_ALLOCATE | CACHE_WRITE_ALLOCATE;
401 break;
402 }
403 }
404
405
406
407
408 if (this_leaf->type == CACHE_TYPE_NOCACHE &&
409 valid_flags == PPTT_CHECKED_ATTRIBUTES)
410 this_leaf->type = CACHE_TYPE_UNIFIED;
411}
412
413static void cache_setup_acpi_cpu(struct acpi_table_header *table,
414 unsigned int cpu)
415{
416 struct acpi_pptt_cache *found_cache;
417 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
418 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
419 struct cacheinfo *this_leaf;
420 unsigned int index = 0;
421 struct acpi_pptt_processor *cpu_node = NULL;
422
423 while (index < get_cpu_cacheinfo(cpu)->num_leaves) {
424 this_leaf = this_cpu_ci->info_list + index;
425 found_cache = acpi_find_cache_node(table, acpi_cpu_id,
426 this_leaf->type,
427 this_leaf->level,
428 &cpu_node);
429 pr_debug("found = %p %p\n", found_cache, cpu_node);
430 if (found_cache)
431 update_cache_properties(this_leaf,
432 found_cache,
433 cpu_node);
434
435 index++;
436 }
437}
438
439
440#define PPTT_ABORT_PACKAGE 0xFF
441
442static struct acpi_pptt_processor *acpi_find_processor_package_id(struct acpi_table_header *table_hdr,
443 struct acpi_pptt_processor *cpu,
444 int level, int flag)
445{
446 struct acpi_pptt_processor *prev_node;
447
448 while (cpu && level) {
449 if (cpu->flags & flag)
450 break;
451 pr_debug("level %d\n", level);
452 prev_node = fetch_pptt_node(table_hdr, cpu->parent);
453 if (prev_node == NULL)
454 break;
455 cpu = prev_node;
456 level--;
457 }
458 return cpu;
459}
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474static int topology_get_acpi_cpu_tag(struct acpi_table_header *table,
475 unsigned int cpu, int level, int flag)
476{
477 struct acpi_pptt_processor *cpu_node;
478 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
479
480 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
481 if (cpu_node) {
482 cpu_node = acpi_find_processor_package_id(table, cpu_node,
483 level, flag);
484
485
486
487
488
489
490 if (level == 0 ||
491 cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID)
492 return cpu_node->acpi_processor_id;
493 return ACPI_PTR_DIFF(cpu_node, table);
494 }
495 pr_warn_once("PPTT table found, but unable to locate core %d (%d)\n",
496 cpu, acpi_cpu_id);
497 return -ENOENT;
498}
499
500static int find_acpi_cpu_topology_tag(unsigned int cpu, int level, int flag)
501{
502 struct acpi_table_header *table;
503 acpi_status status;
504 int retval;
505
506 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
507 if (ACPI_FAILURE(status)) {
508 pr_warn_once("No PPTT table found, cpu topology may be inaccurate\n");
509 return -ENOENT;
510 }
511 retval = topology_get_acpi_cpu_tag(table, cpu, level, flag);
512 pr_debug("Topology Setup ACPI cpu %d, level %d ret = %d\n",
513 cpu, level, retval);
514 acpi_put_table(table);
515
516 return retval;
517}
518
519
520
521
522
523
524
525
526
527
528
529int acpi_find_last_cache_level(unsigned int cpu)
530{
531 u32 acpi_cpu_id;
532 struct acpi_table_header *table;
533 int number_of_levels = 0;
534 acpi_status status;
535
536 pr_debug("Cache Setup find last level cpu=%d\n", cpu);
537
538 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
539 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
540 if (ACPI_FAILURE(status)) {
541 pr_warn_once("No PPTT table found, cache topology may be inaccurate\n");
542 } else {
543 number_of_levels = acpi_find_cache_levels(table, acpi_cpu_id);
544 acpi_put_table(table);
545 }
546 pr_debug("Cache Setup find last level level=%d\n", number_of_levels);
547
548 return number_of_levels;
549}
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564int cache_setup_acpi(unsigned int cpu)
565{
566 struct acpi_table_header *table;
567 acpi_status status;
568
569 pr_debug("Cache Setup ACPI cpu %d\n", cpu);
570
571 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
572 if (ACPI_FAILURE(status)) {
573 pr_warn_once("No PPTT table found, cache topology may be inaccurate\n");
574 return -ENOENT;
575 }
576
577 cache_setup_acpi_cpu(table, cpu);
578 acpi_put_table(table);
579
580 return status;
581}
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601int find_acpi_cpu_topology(unsigned int cpu, int level)
602{
603 return find_acpi_cpu_topology_tag(cpu, level, 0);
604}
605
606
607
608
609
610
611
612
613
614
615
616int find_acpi_cpu_cache_topology(unsigned int cpu, int level)
617{
618 struct acpi_table_header *table;
619 struct acpi_pptt_cache *found_cache;
620 acpi_status status;
621 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
622 struct acpi_pptt_processor *cpu_node = NULL;
623 int ret = -1;
624
625 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
626 if (ACPI_FAILURE(status)) {
627 pr_warn_once("No PPTT table found, topology may be inaccurate\n");
628 return -ENOENT;
629 }
630
631 found_cache = acpi_find_cache_node(table, acpi_cpu_id,
632 CACHE_TYPE_UNIFIED,
633 level,
634 &cpu_node);
635 if (found_cache)
636 ret = ACPI_PTR_DIFF(cpu_node, table);
637
638 acpi_put_table(table);
639
640 return ret;
641}
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657int find_acpi_cpu_topology_package(unsigned int cpu)
658{
659 return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
660 ACPI_PPTT_PHYSICAL_PACKAGE);
661}
662