Loading...
1/* mdesc.c: Sun4V machine description handling.
2 *
3 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
4 */
5#include <linux/kernel.h>
6#include <linux/types.h>
7#include <linux/memblock.h>
8#include <linux/log2.h>
9#include <linux/list.h>
10#include <linux/slab.h>
11#include <linux/mm.h>
12#include <linux/miscdevice.h>
13#include <linux/bootmem.h>
14#include <linux/export.h>
15
16#include <asm/cpudata.h>
17#include <asm/hypervisor.h>
18#include <asm/mdesc.h>
19#include <asm/prom.h>
20#include <asm/uaccess.h>
21#include <asm/oplib.h>
22#include <asm/smp.h>
23
24/* Unlike the OBP device tree, the machine description is a full-on
25 * DAG. An arbitrary number of ARCs are possible from one
26 * node to other nodes and thus we can't use the OBP device_node
27 * data structure to represent these nodes inside of the kernel.
28 *
29 * Actually, it isn't even a DAG, because there are back pointers
30 * which create cycles in the graph.
31 *
32 * mdesc_hdr and mdesc_elem describe the layout of the data structure
33 * we get from the Hypervisor.
34 */
35struct mdesc_hdr {
36 u32 version; /* Transport version */
37 u32 node_sz; /* node block size */
38 u32 name_sz; /* name block size */
39 u32 data_sz; /* data block size */
40} __attribute__((aligned(16)));
41
42struct mdesc_elem {
43 u8 tag;
44#define MD_LIST_END 0x00
45#define MD_NODE 0x4e
46#define MD_NODE_END 0x45
47#define MD_NOOP 0x20
48#define MD_PROP_ARC 0x61
49#define MD_PROP_VAL 0x76
50#define MD_PROP_STR 0x73
51#define MD_PROP_DATA 0x64
52 u8 name_len;
53 u16 resv;
54 u32 name_offset;
55 union {
56 struct {
57 u32 data_len;
58 u32 data_offset;
59 } data;
60 u64 val;
61 } d;
62};
63
64struct mdesc_mem_ops {
65 struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
66 void (*free)(struct mdesc_handle *handle);
67};
68
69struct mdesc_handle {
70 struct list_head list;
71 struct mdesc_mem_ops *mops;
72 void *self_base;
73 atomic_t refcnt;
74 unsigned int handle_size;
75 struct mdesc_hdr mdesc;
76};
77
78static void mdesc_handle_init(struct mdesc_handle *hp,
79 unsigned int handle_size,
80 void *base)
81{
82 BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));
83
84 memset(hp, 0, handle_size);
85 INIT_LIST_HEAD(&hp->list);
86 hp->self_base = base;
87 atomic_set(&hp->refcnt, 1);
88 hp->handle_size = handle_size;
89}
90
91static struct mdesc_handle * __init mdesc_memblock_alloc(unsigned int mdesc_size)
92{
93 unsigned int handle_size, alloc_size;
94 struct mdesc_handle *hp;
95 unsigned long paddr;
96
97 handle_size = (sizeof(struct mdesc_handle) -
98 sizeof(struct mdesc_hdr) +
99 mdesc_size);
100 alloc_size = PAGE_ALIGN(handle_size);
101
102 paddr = memblock_alloc(alloc_size, PAGE_SIZE);
103
104 hp = NULL;
105 if (paddr) {
106 hp = __va(paddr);
107 mdesc_handle_init(hp, handle_size, hp);
108 }
109 return hp;
110}
111
112static void __init mdesc_memblock_free(struct mdesc_handle *hp)
113{
114 unsigned int alloc_size;
115 unsigned long start;
116
117 BUG_ON(atomic_read(&hp->refcnt) != 0);
118 BUG_ON(!list_empty(&hp->list));
119
120 alloc_size = PAGE_ALIGN(hp->handle_size);
121 start = __pa(hp);
122 free_bootmem_late(start, alloc_size);
123}
124
125static struct mdesc_mem_ops memblock_mdesc_ops = {
126 .alloc = mdesc_memblock_alloc,
127 .free = mdesc_memblock_free,
128};
129
130static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
131{
132 unsigned int handle_size;
133 void *base;
134
135 handle_size = (sizeof(struct mdesc_handle) -
136 sizeof(struct mdesc_hdr) +
137 mdesc_size);
138
139 base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_NOFAIL);
140 if (base) {
141 struct mdesc_handle *hp;
142 unsigned long addr;
143
144 addr = (unsigned long)base;
145 addr = (addr + 15UL) & ~15UL;
146 hp = (struct mdesc_handle *) addr;
147
148 mdesc_handle_init(hp, handle_size, base);
149 return hp;
150 }
151
152 return NULL;
153}
154
155static void mdesc_kfree(struct mdesc_handle *hp)
156{
157 BUG_ON(atomic_read(&hp->refcnt) != 0);
158 BUG_ON(!list_empty(&hp->list));
159
160 kfree(hp->self_base);
161}
162
163static struct mdesc_mem_ops kmalloc_mdesc_memops = {
164 .alloc = mdesc_kmalloc,
165 .free = mdesc_kfree,
166};
167
168static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
169 struct mdesc_mem_ops *mops)
170{
171 struct mdesc_handle *hp = mops->alloc(mdesc_size);
172
173 if (hp)
174 hp->mops = mops;
175
176 return hp;
177}
178
179static void mdesc_free(struct mdesc_handle *hp)
180{
181 hp->mops->free(hp);
182}
183
184static struct mdesc_handle *cur_mdesc;
185static LIST_HEAD(mdesc_zombie_list);
186static DEFINE_SPINLOCK(mdesc_lock);
187
188struct mdesc_handle *mdesc_grab(void)
189{
190 struct mdesc_handle *hp;
191 unsigned long flags;
192
193 spin_lock_irqsave(&mdesc_lock, flags);
194 hp = cur_mdesc;
195 if (hp)
196 atomic_inc(&hp->refcnt);
197 spin_unlock_irqrestore(&mdesc_lock, flags);
198
199 return hp;
200}
201EXPORT_SYMBOL(mdesc_grab);
202
203void mdesc_release(struct mdesc_handle *hp)
204{
205 unsigned long flags;
206
207 spin_lock_irqsave(&mdesc_lock, flags);
208 if (atomic_dec_and_test(&hp->refcnt)) {
209 list_del_init(&hp->list);
210 hp->mops->free(hp);
211 }
212 spin_unlock_irqrestore(&mdesc_lock, flags);
213}
214EXPORT_SYMBOL(mdesc_release);
215
216static DEFINE_MUTEX(mdesc_mutex);
217static struct mdesc_notifier_client *client_list;
218
219void mdesc_register_notifier(struct mdesc_notifier_client *client)
220{
221 u64 node;
222
223 mutex_lock(&mdesc_mutex);
224 client->next = client_list;
225 client_list = client;
226
227 mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
228 client->add(cur_mdesc, node);
229
230 mutex_unlock(&mdesc_mutex);
231}
232
233static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
234{
235 const u64 *id;
236 u64 a;
237
238 id = NULL;
239 mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
240 u64 target;
241
242 target = mdesc_arc_target(hp, a);
243 id = mdesc_get_property(hp, target,
244 "cfg-handle", NULL);
245 if (id)
246 break;
247 }
248
249 return id;
250}
251
252/* Run 'func' on nodes which are in A but not in B. */
253static void invoke_on_missing(const char *name,
254 struct mdesc_handle *a,
255 struct mdesc_handle *b,
256 void (*func)(struct mdesc_handle *, u64))
257{
258 u64 node;
259
260 mdesc_for_each_node_by_name(a, node, name) {
261 int found = 0, is_vdc_port = 0;
262 const char *name_prop;
263 const u64 *id;
264 u64 fnode;
265
266 name_prop = mdesc_get_property(a, node, "name", NULL);
267 if (name_prop && !strcmp(name_prop, "vdc-port")) {
268 is_vdc_port = 1;
269 id = parent_cfg_handle(a, node);
270 } else
271 id = mdesc_get_property(a, node, "id", NULL);
272
273 if (!id) {
274 printk(KERN_ERR "MD: Cannot find ID for %s node.\n",
275 (name_prop ? name_prop : name));
276 continue;
277 }
278
279 mdesc_for_each_node_by_name(b, fnode, name) {
280 const u64 *fid;
281
282 if (is_vdc_port) {
283 name_prop = mdesc_get_property(b, fnode,
284 "name", NULL);
285 if (!name_prop ||
286 strcmp(name_prop, "vdc-port"))
287 continue;
288 fid = parent_cfg_handle(b, fnode);
289 if (!fid) {
290 printk(KERN_ERR "MD: Cannot find ID "
291 "for vdc-port node.\n");
292 continue;
293 }
294 } else
295 fid = mdesc_get_property(b, fnode,
296 "id", NULL);
297
298 if (*id == *fid) {
299 found = 1;
300 break;
301 }
302 }
303 if (!found)
304 func(a, node);
305 }
306}
307
308static void notify_one(struct mdesc_notifier_client *p,
309 struct mdesc_handle *old_hp,
310 struct mdesc_handle *new_hp)
311{
312 invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
313 invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
314}
315
316static void mdesc_notify_clients(struct mdesc_handle *old_hp,
317 struct mdesc_handle *new_hp)
318{
319 struct mdesc_notifier_client *p = client_list;
320
321 while (p) {
322 notify_one(p, old_hp, new_hp);
323 p = p->next;
324 }
325}
326
327void mdesc_update(void)
328{
329 unsigned long len, real_len, status;
330 struct mdesc_handle *hp, *orig_hp;
331 unsigned long flags;
332
333 mutex_lock(&mdesc_mutex);
334
335 (void) sun4v_mach_desc(0UL, 0UL, &len);
336
337 hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
338 if (!hp) {
339 printk(KERN_ERR "MD: mdesc alloc fails\n");
340 goto out;
341 }
342
343 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
344 if (status != HV_EOK || real_len > len) {
345 printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
346 status);
347 atomic_dec(&hp->refcnt);
348 mdesc_free(hp);
349 goto out;
350 }
351
352 spin_lock_irqsave(&mdesc_lock, flags);
353 orig_hp = cur_mdesc;
354 cur_mdesc = hp;
355 spin_unlock_irqrestore(&mdesc_lock, flags);
356
357 mdesc_notify_clients(orig_hp, hp);
358
359 spin_lock_irqsave(&mdesc_lock, flags);
360 if (atomic_dec_and_test(&orig_hp->refcnt))
361 mdesc_free(orig_hp);
362 else
363 list_add(&orig_hp->list, &mdesc_zombie_list);
364 spin_unlock_irqrestore(&mdesc_lock, flags);
365
366out:
367 mutex_unlock(&mdesc_mutex);
368}
369
370static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
371{
372 return (struct mdesc_elem *) (mdesc + 1);
373}
374
375static void *name_block(struct mdesc_hdr *mdesc)
376{
377 return ((void *) node_block(mdesc)) + mdesc->node_sz;
378}
379
380static void *data_block(struct mdesc_hdr *mdesc)
381{
382 return ((void *) name_block(mdesc)) + mdesc->name_sz;
383}
384
385u64 mdesc_node_by_name(struct mdesc_handle *hp,
386 u64 from_node, const char *name)
387{
388 struct mdesc_elem *ep = node_block(&hp->mdesc);
389 const char *names = name_block(&hp->mdesc);
390 u64 last_node = hp->mdesc.node_sz / 16;
391 u64 ret;
392
393 if (from_node == MDESC_NODE_NULL) {
394 ret = from_node = 0;
395 } else if (from_node >= last_node) {
396 return MDESC_NODE_NULL;
397 } else {
398 ret = ep[from_node].d.val;
399 }
400
401 while (ret < last_node) {
402 if (ep[ret].tag != MD_NODE)
403 return MDESC_NODE_NULL;
404 if (!strcmp(names + ep[ret].name_offset, name))
405 break;
406 ret = ep[ret].d.val;
407 }
408 if (ret >= last_node)
409 ret = MDESC_NODE_NULL;
410 return ret;
411}
412EXPORT_SYMBOL(mdesc_node_by_name);
413
414const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
415 const char *name, int *lenp)
416{
417 const char *names = name_block(&hp->mdesc);
418 u64 last_node = hp->mdesc.node_sz / 16;
419 void *data = data_block(&hp->mdesc);
420 struct mdesc_elem *ep;
421
422 if (node == MDESC_NODE_NULL || node >= last_node)
423 return NULL;
424
425 ep = node_block(&hp->mdesc) + node;
426 ep++;
427 for (; ep->tag != MD_NODE_END; ep++) {
428 void *val = NULL;
429 int len = 0;
430
431 switch (ep->tag) {
432 case MD_PROP_VAL:
433 val = &ep->d.val;
434 len = 8;
435 break;
436
437 case MD_PROP_STR:
438 case MD_PROP_DATA:
439 val = data + ep->d.data.data_offset;
440 len = ep->d.data.data_len;
441 break;
442
443 default:
444 break;
445 }
446 if (!val)
447 continue;
448
449 if (!strcmp(names + ep->name_offset, name)) {
450 if (lenp)
451 *lenp = len;
452 return val;
453 }
454 }
455
456 return NULL;
457}
458EXPORT_SYMBOL(mdesc_get_property);
459
460u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
461{
462 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
463 const char *names = name_block(&hp->mdesc);
464 u64 last_node = hp->mdesc.node_sz / 16;
465
466 if (from == MDESC_NODE_NULL || from >= last_node)
467 return MDESC_NODE_NULL;
468
469 ep = base + from;
470
471 ep++;
472 for (; ep->tag != MD_NODE_END; ep++) {
473 if (ep->tag != MD_PROP_ARC)
474 continue;
475
476 if (strcmp(names + ep->name_offset, arc_type))
477 continue;
478
479 return ep - base;
480 }
481
482 return MDESC_NODE_NULL;
483}
484EXPORT_SYMBOL(mdesc_next_arc);
485
486u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
487{
488 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
489
490 ep = base + arc;
491
492 return ep->d.val;
493}
494EXPORT_SYMBOL(mdesc_arc_target);
495
496const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
497{
498 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
499 const char *names = name_block(&hp->mdesc);
500 u64 last_node = hp->mdesc.node_sz / 16;
501
502 if (node == MDESC_NODE_NULL || node >= last_node)
503 return NULL;
504
505 ep = base + node;
506 if (ep->tag != MD_NODE)
507 return NULL;
508
509 return names + ep->name_offset;
510}
511EXPORT_SYMBOL(mdesc_node_name);
512
513static u64 max_cpus = 64;
514
515static void __init report_platform_properties(void)
516{
517 struct mdesc_handle *hp = mdesc_grab();
518 u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
519 const char *s;
520 const u64 *v;
521
522 if (pn == MDESC_NODE_NULL) {
523 prom_printf("No platform node in machine-description.\n");
524 prom_halt();
525 }
526
527 s = mdesc_get_property(hp, pn, "banner-name", NULL);
528 printk("PLATFORM: banner-name [%s]\n", s);
529 s = mdesc_get_property(hp, pn, "name", NULL);
530 printk("PLATFORM: name [%s]\n", s);
531
532 v = mdesc_get_property(hp, pn, "hostid", NULL);
533 if (v)
534 printk("PLATFORM: hostid [%08llx]\n", *v);
535 v = mdesc_get_property(hp, pn, "serial#", NULL);
536 if (v)
537 printk("PLATFORM: serial# [%08llx]\n", *v);
538 v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
539 printk("PLATFORM: stick-frequency [%08llx]\n", *v);
540 v = mdesc_get_property(hp, pn, "mac-address", NULL);
541 if (v)
542 printk("PLATFORM: mac-address [%llx]\n", *v);
543 v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
544 if (v)
545 printk("PLATFORM: watchdog-resolution [%llu ms]\n", *v);
546 v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
547 if (v)
548 printk("PLATFORM: watchdog-max-timeout [%llu ms]\n", *v);
549 v = mdesc_get_property(hp, pn, "max-cpus", NULL);
550 if (v) {
551 max_cpus = *v;
552 printk("PLATFORM: max-cpus [%llu]\n", max_cpus);
553 }
554
555#ifdef CONFIG_SMP
556 {
557 int max_cpu, i;
558
559 if (v) {
560 max_cpu = *v;
561 if (max_cpu > NR_CPUS)
562 max_cpu = NR_CPUS;
563 } else {
564 max_cpu = NR_CPUS;
565 }
566 for (i = 0; i < max_cpu; i++)
567 set_cpu_possible(i, true);
568 }
569#endif
570
571 mdesc_release(hp);
572}
573
574static void __cpuinit fill_in_one_cache(cpuinfo_sparc *c,
575 struct mdesc_handle *hp,
576 u64 mp)
577{
578 const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
579 const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
580 const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
581 const char *type;
582 int type_len;
583
584 type = mdesc_get_property(hp, mp, "type", &type_len);
585
586 switch (*level) {
587 case 1:
588 if (of_find_in_proplist(type, "instn", type_len)) {
589 c->icache_size = *size;
590 c->icache_line_size = *line_size;
591 } else if (of_find_in_proplist(type, "data", type_len)) {
592 c->dcache_size = *size;
593 c->dcache_line_size = *line_size;
594 }
595 break;
596
597 case 2:
598 c->ecache_size = *size;
599 c->ecache_line_size = *line_size;
600 break;
601
602 default:
603 break;
604 }
605
606 if (*level == 1) {
607 u64 a;
608
609 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
610 u64 target = mdesc_arc_target(hp, a);
611 const char *name = mdesc_node_name(hp, target);
612
613 if (!strcmp(name, "cache"))
614 fill_in_one_cache(c, hp, target);
615 }
616 }
617}
618
619static void __cpuinit mark_core_ids(struct mdesc_handle *hp, u64 mp, int core_id)
620{
621 u64 a;
622
623 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
624 u64 t = mdesc_arc_target(hp, a);
625 const char *name;
626 const u64 *id;
627
628 name = mdesc_node_name(hp, t);
629 if (!strcmp(name, "cpu")) {
630 id = mdesc_get_property(hp, t, "id", NULL);
631 if (*id < NR_CPUS)
632 cpu_data(*id).core_id = core_id;
633 } else {
634 u64 j;
635
636 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_BACK) {
637 u64 n = mdesc_arc_target(hp, j);
638 const char *n_name;
639
640 n_name = mdesc_node_name(hp, n);
641 if (strcmp(n_name, "cpu"))
642 continue;
643
644 id = mdesc_get_property(hp, n, "id", NULL);
645 if (*id < NR_CPUS)
646 cpu_data(*id).core_id = core_id;
647 }
648 }
649 }
650}
651
652static void __cpuinit set_core_ids(struct mdesc_handle *hp)
653{
654 int idx;
655 u64 mp;
656
657 idx = 1;
658 mdesc_for_each_node_by_name(hp, mp, "cache") {
659 const u64 *level;
660 const char *type;
661 int len;
662
663 level = mdesc_get_property(hp, mp, "level", NULL);
664 if (*level != 1)
665 continue;
666
667 type = mdesc_get_property(hp, mp, "type", &len);
668 if (!of_find_in_proplist(type, "instn", len))
669 continue;
670
671 mark_core_ids(hp, mp, idx);
672
673 idx++;
674 }
675}
676
677static void __cpuinit mark_proc_ids(struct mdesc_handle *hp, u64 mp, int proc_id)
678{
679 u64 a;
680
681 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
682 u64 t = mdesc_arc_target(hp, a);
683 const char *name;
684 const u64 *id;
685
686 name = mdesc_node_name(hp, t);
687 if (strcmp(name, "cpu"))
688 continue;
689
690 id = mdesc_get_property(hp, t, "id", NULL);
691 if (*id < NR_CPUS)
692 cpu_data(*id).proc_id = proc_id;
693 }
694}
695
696static void __cpuinit __set_proc_ids(struct mdesc_handle *hp, const char *exec_unit_name)
697{
698 int idx;
699 u64 mp;
700
701 idx = 0;
702 mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
703 const char *type;
704 int len;
705
706 type = mdesc_get_property(hp, mp, "type", &len);
707 if (!of_find_in_proplist(type, "int", len) &&
708 !of_find_in_proplist(type, "integer", len))
709 continue;
710
711 mark_proc_ids(hp, mp, idx);
712
713 idx++;
714 }
715}
716
717static void __cpuinit set_proc_ids(struct mdesc_handle *hp)
718{
719 __set_proc_ids(hp, "exec_unit");
720 __set_proc_ids(hp, "exec-unit");
721}
722
723static void __cpuinit get_one_mondo_bits(const u64 *p, unsigned int *mask,
724 unsigned long def, unsigned long max)
725{
726 u64 val;
727
728 if (!p)
729 goto use_default;
730 val = *p;
731
732 if (!val || val >= 64)
733 goto use_default;
734
735 if (val > max)
736 val = max;
737
738 *mask = ((1U << val) * 64U) - 1U;
739 return;
740
741use_default:
742 *mask = ((1U << def) * 64U) - 1U;
743}
744
745static void __cpuinit get_mondo_data(struct mdesc_handle *hp, u64 mp,
746 struct trap_per_cpu *tb)
747{
748 static int printed;
749 const u64 *val;
750
751 val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
752 get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7, ilog2(max_cpus * 2));
753
754 val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
755 get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7, 8);
756
757 val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
758 get_one_mondo_bits(val, &tb->resum_qmask, 6, 7);
759
760 val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
761 get_one_mondo_bits(val, &tb->nonresum_qmask, 2, 2);
762 if (!printed++) {
763 pr_info("SUN4V: Mondo queue sizes "
764 "[cpu(%u) dev(%u) r(%u) nr(%u)]\n",
765 tb->cpu_mondo_qmask + 1,
766 tb->dev_mondo_qmask + 1,
767 tb->resum_qmask + 1,
768 tb->nonresum_qmask + 1);
769 }
770}
771
772static void * __cpuinit mdesc_iterate_over_cpus(void *(*func)(struct mdesc_handle *, u64, int, void *), void *arg, cpumask_t *mask)
773{
774 struct mdesc_handle *hp = mdesc_grab();
775 void *ret = NULL;
776 u64 mp;
777
778 mdesc_for_each_node_by_name(hp, mp, "cpu") {
779 const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
780 int cpuid = *id;
781
782#ifdef CONFIG_SMP
783 if (cpuid >= NR_CPUS) {
784 printk(KERN_WARNING "Ignoring CPU %d which is "
785 ">= NR_CPUS (%d)\n",
786 cpuid, NR_CPUS);
787 continue;
788 }
789 if (!cpumask_test_cpu(cpuid, mask))
790 continue;
791#endif
792
793 ret = func(hp, mp, cpuid, arg);
794 if (ret)
795 goto out;
796 }
797out:
798 mdesc_release(hp);
799 return ret;
800}
801
802static void * __cpuinit record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
803{
804 ncpus_probed++;
805#ifdef CONFIG_SMP
806 set_cpu_present(cpuid, true);
807#endif
808 return NULL;
809}
810
811void __cpuinit mdesc_populate_present_mask(cpumask_t *mask)
812{
813 if (tlb_type != hypervisor)
814 return;
815
816 ncpus_probed = 0;
817 mdesc_iterate_over_cpus(record_one_cpu, NULL, mask);
818}
819
820static void * __cpuinit fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
821{
822 const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
823 struct trap_per_cpu *tb;
824 cpuinfo_sparc *c;
825 u64 a;
826
827#ifndef CONFIG_SMP
828 /* On uniprocessor we only want the values for the
829 * real physical cpu the kernel booted onto, however
830 * cpu_data() only has one entry at index 0.
831 */
832 if (cpuid != real_hard_smp_processor_id())
833 return NULL;
834 cpuid = 0;
835#endif
836
837 c = &cpu_data(cpuid);
838 c->clock_tick = *cfreq;
839
840 tb = &trap_block[cpuid];
841 get_mondo_data(hp, mp, tb);
842
843 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
844 u64 j, t = mdesc_arc_target(hp, a);
845 const char *t_name;
846
847 t_name = mdesc_node_name(hp, t);
848 if (!strcmp(t_name, "cache")) {
849 fill_in_one_cache(c, hp, t);
850 continue;
851 }
852
853 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
854 u64 n = mdesc_arc_target(hp, j);
855 const char *n_name;
856
857 n_name = mdesc_node_name(hp, n);
858 if (!strcmp(n_name, "cache"))
859 fill_in_one_cache(c, hp, n);
860 }
861 }
862
863 c->core_id = 0;
864 c->proc_id = -1;
865
866 return NULL;
867}
868
869void __cpuinit mdesc_fill_in_cpu_data(cpumask_t *mask)
870{
871 struct mdesc_handle *hp;
872
873 mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask);
874
875#ifdef CONFIG_SMP
876 sparc64_multi_core = 1;
877#endif
878
879 hp = mdesc_grab();
880
881 set_core_ids(hp);
882 set_proc_ids(hp);
883
884 mdesc_release(hp);
885
886 smp_fill_in_sib_core_maps();
887}
888
889static ssize_t mdesc_read(struct file *file, char __user *buf,
890 size_t len, loff_t *offp)
891{
892 struct mdesc_handle *hp = mdesc_grab();
893 int err;
894
895 if (!hp)
896 return -ENODEV;
897
898 err = hp->handle_size;
899 if (len < hp->handle_size)
900 err = -EMSGSIZE;
901 else if (copy_to_user(buf, &hp->mdesc, hp->handle_size))
902 err = -EFAULT;
903 mdesc_release(hp);
904
905 return err;
906}
907
908static const struct file_operations mdesc_fops = {
909 .read = mdesc_read,
910 .owner = THIS_MODULE,
911 .llseek = noop_llseek,
912};
913
914static struct miscdevice mdesc_misc = {
915 .minor = MISC_DYNAMIC_MINOR,
916 .name = "mdesc",
917 .fops = &mdesc_fops,
918};
919
920static int __init mdesc_misc_init(void)
921{
922 return misc_register(&mdesc_misc);
923}
924
925__initcall(mdesc_misc_init);
926
927void __init sun4v_mdesc_init(void)
928{
929 struct mdesc_handle *hp;
930 unsigned long len, real_len, status;
931
932 (void) sun4v_mach_desc(0UL, 0UL, &len);
933
934 printk("MDESC: Size is %lu bytes.\n", len);
935
936 hp = mdesc_alloc(len, &memblock_mdesc_ops);
937 if (hp == NULL) {
938 prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
939 prom_halt();
940 }
941
942 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
943 if (status != HV_EOK || real_len > len) {
944 prom_printf("sun4v_mach_desc fails, err(%lu), "
945 "len(%lu), real_len(%lu)\n",
946 status, len, real_len);
947 mdesc_free(hp);
948 prom_halt();
949 }
950
951 cur_mdesc = hp;
952
953 report_platform_properties();
954}
1// SPDX-License-Identifier: GPL-2.0
2/* mdesc.c: Sun4V machine description handling.
3 *
4 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
5 */
6#include <linux/kernel.h>
7#include <linux/types.h>
8#include <linux/log2.h>
9#include <linux/list.h>
10#include <linux/slab.h>
11#include <linux/mm.h>
12#include <linux/miscdevice.h>
13#include <linux/memblock.h>
14#include <linux/export.h>
15#include <linux/refcount.h>
16
17#include <asm/cpudata.h>
18#include <asm/hypervisor.h>
19#include <asm/mdesc.h>
20#include <asm/prom.h>
21#include <linux/uaccess.h>
22#include <asm/oplib.h>
23#include <asm/smp.h>
24#include <asm/adi.h>
25
26/* Unlike the OBP device tree, the machine description is a full-on
27 * DAG. An arbitrary number of ARCs are possible from one
28 * node to other nodes and thus we can't use the OBP device_node
29 * data structure to represent these nodes inside of the kernel.
30 *
31 * Actually, it isn't even a DAG, because there are back pointers
32 * which create cycles in the graph.
33 *
34 * mdesc_hdr and mdesc_elem describe the layout of the data structure
35 * we get from the Hypervisor.
36 */
37struct mdesc_hdr {
38 u32 version; /* Transport version */
39 u32 node_sz; /* node block size */
40 u32 name_sz; /* name block size */
41 u32 data_sz; /* data block size */
42} __attribute__((aligned(16)));
43
44struct mdesc_elem {
45 u8 tag;
46#define MD_LIST_END 0x00
47#define MD_NODE 0x4e
48#define MD_NODE_END 0x45
49#define MD_NOOP 0x20
50#define MD_PROP_ARC 0x61
51#define MD_PROP_VAL 0x76
52#define MD_PROP_STR 0x73
53#define MD_PROP_DATA 0x64
54 u8 name_len;
55 u16 resv;
56 u32 name_offset;
57 union {
58 struct {
59 u32 data_len;
60 u32 data_offset;
61 } data;
62 u64 val;
63 } d;
64};
65
66struct mdesc_mem_ops {
67 struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
68 void (*free)(struct mdesc_handle *handle);
69};
70
71struct mdesc_handle {
72 struct list_head list;
73 struct mdesc_mem_ops *mops;
74 void *self_base;
75 refcount_t refcnt;
76 unsigned int handle_size;
77 struct mdesc_hdr mdesc;
78};
79
80typedef int (*mdesc_node_info_get_f)(struct mdesc_handle *, u64,
81 union md_node_info *);
82typedef void (*mdesc_node_info_rel_f)(union md_node_info *);
83typedef bool (*mdesc_node_match_f)(union md_node_info *, union md_node_info *);
84
85struct md_node_ops {
86 char *name;
87 mdesc_node_info_get_f get_info;
88 mdesc_node_info_rel_f rel_info;
89 mdesc_node_match_f node_match;
90};
91
92static int get_vdev_port_node_info(struct mdesc_handle *md, u64 node,
93 union md_node_info *node_info);
94static void rel_vdev_port_node_info(union md_node_info *node_info);
95static bool vdev_port_node_match(union md_node_info *a_node_info,
96 union md_node_info *b_node_info);
97
98static int get_ds_port_node_info(struct mdesc_handle *md, u64 node,
99 union md_node_info *node_info);
100static void rel_ds_port_node_info(union md_node_info *node_info);
101static bool ds_port_node_match(union md_node_info *a_node_info,
102 union md_node_info *b_node_info);
103
104/* supported node types which can be registered */
105static struct md_node_ops md_node_ops_table[] = {
106 {"virtual-device-port", get_vdev_port_node_info,
107 rel_vdev_port_node_info, vdev_port_node_match},
108 {"domain-services-port", get_ds_port_node_info,
109 rel_ds_port_node_info, ds_port_node_match},
110 {NULL, NULL, NULL, NULL}
111};
112
113static void mdesc_get_node_ops(const char *node_name,
114 mdesc_node_info_get_f *get_info_f,
115 mdesc_node_info_rel_f *rel_info_f,
116 mdesc_node_match_f *match_f)
117{
118 int i;
119
120 if (get_info_f)
121 *get_info_f = NULL;
122
123 if (rel_info_f)
124 *rel_info_f = NULL;
125
126 if (match_f)
127 *match_f = NULL;
128
129 if (!node_name)
130 return;
131
132 for (i = 0; md_node_ops_table[i].name != NULL; i++) {
133 if (strcmp(md_node_ops_table[i].name, node_name) == 0) {
134 if (get_info_f)
135 *get_info_f = md_node_ops_table[i].get_info;
136
137 if (rel_info_f)
138 *rel_info_f = md_node_ops_table[i].rel_info;
139
140 if (match_f)
141 *match_f = md_node_ops_table[i].node_match;
142
143 break;
144 }
145 }
146}
147
148static void mdesc_handle_init(struct mdesc_handle *hp,
149 unsigned int handle_size,
150 void *base)
151{
152 BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));
153
154 memset(hp, 0, handle_size);
155 INIT_LIST_HEAD(&hp->list);
156 hp->self_base = base;
157 refcount_set(&hp->refcnt, 1);
158 hp->handle_size = handle_size;
159}
160
161static struct mdesc_handle * __init mdesc_memblock_alloc(unsigned int mdesc_size)
162{
163 unsigned int handle_size, alloc_size;
164 struct mdesc_handle *hp;
165 unsigned long paddr;
166
167 handle_size = (sizeof(struct mdesc_handle) -
168 sizeof(struct mdesc_hdr) +
169 mdesc_size);
170 alloc_size = PAGE_ALIGN(handle_size);
171
172 paddr = memblock_phys_alloc(alloc_size, PAGE_SIZE);
173
174 hp = NULL;
175 if (paddr) {
176 hp = __va(paddr);
177 mdesc_handle_init(hp, handle_size, hp);
178 }
179 return hp;
180}
181
182static void __init mdesc_memblock_free(struct mdesc_handle *hp)
183{
184 unsigned int alloc_size;
185 unsigned long start;
186
187 BUG_ON(refcount_read(&hp->refcnt) != 0);
188 BUG_ON(!list_empty(&hp->list));
189
190 alloc_size = PAGE_ALIGN(hp->handle_size);
191 start = __pa(hp);
192 memblock_free_late(start, alloc_size);
193}
194
195static struct mdesc_mem_ops memblock_mdesc_ops = {
196 .alloc = mdesc_memblock_alloc,
197 .free = mdesc_memblock_free,
198};
199
200static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
201{
202 unsigned int handle_size;
203 struct mdesc_handle *hp;
204 unsigned long addr;
205 void *base;
206
207 handle_size = (sizeof(struct mdesc_handle) -
208 sizeof(struct mdesc_hdr) +
209 mdesc_size);
210 base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
211 if (!base)
212 return NULL;
213
214 addr = (unsigned long)base;
215 addr = (addr + 15UL) & ~15UL;
216 hp = (struct mdesc_handle *) addr;
217
218 mdesc_handle_init(hp, handle_size, base);
219
220 return hp;
221}
222
223static void mdesc_kfree(struct mdesc_handle *hp)
224{
225 BUG_ON(refcount_read(&hp->refcnt) != 0);
226 BUG_ON(!list_empty(&hp->list));
227
228 kfree(hp->self_base);
229}
230
231static struct mdesc_mem_ops kmalloc_mdesc_memops = {
232 .alloc = mdesc_kmalloc,
233 .free = mdesc_kfree,
234};
235
236static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
237 struct mdesc_mem_ops *mops)
238{
239 struct mdesc_handle *hp = mops->alloc(mdesc_size);
240
241 if (hp)
242 hp->mops = mops;
243
244 return hp;
245}
246
247static void mdesc_free(struct mdesc_handle *hp)
248{
249 hp->mops->free(hp);
250}
251
252static struct mdesc_handle *cur_mdesc;
253static LIST_HEAD(mdesc_zombie_list);
254static DEFINE_SPINLOCK(mdesc_lock);
255
256struct mdesc_handle *mdesc_grab(void)
257{
258 struct mdesc_handle *hp;
259 unsigned long flags;
260
261 spin_lock_irqsave(&mdesc_lock, flags);
262 hp = cur_mdesc;
263 if (hp)
264 refcount_inc(&hp->refcnt);
265 spin_unlock_irqrestore(&mdesc_lock, flags);
266
267 return hp;
268}
269EXPORT_SYMBOL(mdesc_grab);
270
271void mdesc_release(struct mdesc_handle *hp)
272{
273 unsigned long flags;
274
275 spin_lock_irqsave(&mdesc_lock, flags);
276 if (refcount_dec_and_test(&hp->refcnt)) {
277 list_del_init(&hp->list);
278 hp->mops->free(hp);
279 }
280 spin_unlock_irqrestore(&mdesc_lock, flags);
281}
282EXPORT_SYMBOL(mdesc_release);
283
284static DEFINE_MUTEX(mdesc_mutex);
285static struct mdesc_notifier_client *client_list;
286
287void mdesc_register_notifier(struct mdesc_notifier_client *client)
288{
289 bool supported = false;
290 u64 node;
291 int i;
292
293 mutex_lock(&mdesc_mutex);
294
295 /* check to see if the node is supported for registration */
296 for (i = 0; md_node_ops_table[i].name != NULL; i++) {
297 if (strcmp(md_node_ops_table[i].name, client->node_name) == 0) {
298 supported = true;
299 break;
300 }
301 }
302
303 if (!supported) {
304 pr_err("MD: %s node not supported\n", client->node_name);
305 mutex_unlock(&mdesc_mutex);
306 return;
307 }
308
309 client->next = client_list;
310 client_list = client;
311
312 mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
313 client->add(cur_mdesc, node, client->node_name);
314
315 mutex_unlock(&mdesc_mutex);
316}
317
318static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
319{
320 const u64 *id;
321 u64 a;
322
323 id = NULL;
324 mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
325 u64 target;
326
327 target = mdesc_arc_target(hp, a);
328 id = mdesc_get_property(hp, target,
329 "cfg-handle", NULL);
330 if (id)
331 break;
332 }
333
334 return id;
335}
336
337static int get_vdev_port_node_info(struct mdesc_handle *md, u64 node,
338 union md_node_info *node_info)
339{
340 const u64 *parent_cfg_hdlp;
341 const char *name;
342 const u64 *idp;
343
344 /*
345 * Virtual device nodes are distinguished by:
346 * 1. "id" property
347 * 2. "name" property
348 * 3. parent node "cfg-handle" property
349 */
350 idp = mdesc_get_property(md, node, "id", NULL);
351 name = mdesc_get_property(md, node, "name", NULL);
352 parent_cfg_hdlp = parent_cfg_handle(md, node);
353
354 if (!idp || !name || !parent_cfg_hdlp)
355 return -1;
356
357 node_info->vdev_port.id = *idp;
358 node_info->vdev_port.name = kstrdup_const(name, GFP_KERNEL);
359 if (!node_info->vdev_port.name)
360 return -1;
361 node_info->vdev_port.parent_cfg_hdl = *parent_cfg_hdlp;
362
363 return 0;
364}
365
366static void rel_vdev_port_node_info(union md_node_info *node_info)
367{
368 if (node_info && node_info->vdev_port.name) {
369 kfree_const(node_info->vdev_port.name);
370 node_info->vdev_port.name = NULL;
371 }
372}
373
374static bool vdev_port_node_match(union md_node_info *a_node_info,
375 union md_node_info *b_node_info)
376{
377 if (a_node_info->vdev_port.id != b_node_info->vdev_port.id)
378 return false;
379
380 if (a_node_info->vdev_port.parent_cfg_hdl !=
381 b_node_info->vdev_port.parent_cfg_hdl)
382 return false;
383
384 if (strncmp(a_node_info->vdev_port.name,
385 b_node_info->vdev_port.name, MDESC_MAX_STR_LEN) != 0)
386 return false;
387
388 return true;
389}
390
391static int get_ds_port_node_info(struct mdesc_handle *md, u64 node,
392 union md_node_info *node_info)
393{
394 const u64 *idp;
395
396 /* DS port nodes use the "id" property to distinguish them */
397 idp = mdesc_get_property(md, node, "id", NULL);
398 if (!idp)
399 return -1;
400
401 node_info->ds_port.id = *idp;
402
403 return 0;
404}
405
406static void rel_ds_port_node_info(union md_node_info *node_info)
407{
408}
409
410static bool ds_port_node_match(union md_node_info *a_node_info,
411 union md_node_info *b_node_info)
412{
413 if (a_node_info->ds_port.id != b_node_info->ds_port.id)
414 return false;
415
416 return true;
417}
418
419/* Run 'func' on nodes which are in A but not in B. */
420static void invoke_on_missing(const char *name,
421 struct mdesc_handle *a,
422 struct mdesc_handle *b,
423 void (*func)(struct mdesc_handle *, u64,
424 const char *node_name))
425{
426 mdesc_node_info_get_f get_info_func;
427 mdesc_node_info_rel_f rel_info_func;
428 mdesc_node_match_f node_match_func;
429 union md_node_info a_node_info;
430 union md_node_info b_node_info;
431 bool found;
432 u64 a_node;
433 u64 b_node;
434 int rv;
435
436 /*
437 * Find the get_info, rel_info and node_match ops for the given
438 * node name
439 */
440 mdesc_get_node_ops(name, &get_info_func, &rel_info_func,
441 &node_match_func);
442
443 /* If we didn't find a match, the node type is not supported */
444 if (!get_info_func || !rel_info_func || !node_match_func) {
445 pr_err("MD: %s node type is not supported\n", name);
446 return;
447 }
448
449 mdesc_for_each_node_by_name(a, a_node, name) {
450 found = false;
451
452 rv = get_info_func(a, a_node, &a_node_info);
453 if (rv != 0) {
454 pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
455 name);
456 continue;
457 }
458
459 /* Check each node in B for node matching a_node */
460 mdesc_for_each_node_by_name(b, b_node, name) {
461 rv = get_info_func(b, b_node, &b_node_info);
462 if (rv != 0)
463 continue;
464
465 if (node_match_func(&a_node_info, &b_node_info)) {
466 found = true;
467 rel_info_func(&b_node_info);
468 break;
469 }
470
471 rel_info_func(&b_node_info);
472 }
473
474 rel_info_func(&a_node_info);
475
476 if (!found)
477 func(a, a_node, name);
478 }
479}
480
481static void notify_one(struct mdesc_notifier_client *p,
482 struct mdesc_handle *old_hp,
483 struct mdesc_handle *new_hp)
484{
485 invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
486 invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
487}
488
489static void mdesc_notify_clients(struct mdesc_handle *old_hp,
490 struct mdesc_handle *new_hp)
491{
492 struct mdesc_notifier_client *p = client_list;
493
494 while (p) {
495 notify_one(p, old_hp, new_hp);
496 p = p->next;
497 }
498}
499
500void mdesc_update(void)
501{
502 unsigned long len, real_len, status;
503 struct mdesc_handle *hp, *orig_hp;
504 unsigned long flags;
505
506 mutex_lock(&mdesc_mutex);
507
508 (void) sun4v_mach_desc(0UL, 0UL, &len);
509
510 hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
511 if (!hp) {
512 printk(KERN_ERR "MD: mdesc alloc fails\n");
513 goto out;
514 }
515
516 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
517 if (status != HV_EOK || real_len > len) {
518 printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
519 status);
520 refcount_dec(&hp->refcnt);
521 mdesc_free(hp);
522 goto out;
523 }
524
525 spin_lock_irqsave(&mdesc_lock, flags);
526 orig_hp = cur_mdesc;
527 cur_mdesc = hp;
528 spin_unlock_irqrestore(&mdesc_lock, flags);
529
530 mdesc_notify_clients(orig_hp, hp);
531
532 spin_lock_irqsave(&mdesc_lock, flags);
533 if (refcount_dec_and_test(&orig_hp->refcnt))
534 mdesc_free(orig_hp);
535 else
536 list_add(&orig_hp->list, &mdesc_zombie_list);
537 spin_unlock_irqrestore(&mdesc_lock, flags);
538
539out:
540 mutex_unlock(&mdesc_mutex);
541}
542
543u64 mdesc_get_node(struct mdesc_handle *hp, const char *node_name,
544 union md_node_info *node_info)
545{
546 mdesc_node_info_get_f get_info_func;
547 mdesc_node_info_rel_f rel_info_func;
548 mdesc_node_match_f node_match_func;
549 union md_node_info hp_node_info;
550 u64 hp_node;
551 int rv;
552
553 if (hp == NULL || node_name == NULL || node_info == NULL)
554 return MDESC_NODE_NULL;
555
556 /* Find the ops for the given node name */
557 mdesc_get_node_ops(node_name, &get_info_func, &rel_info_func,
558 &node_match_func);
559
560 /* If we didn't find ops for the given node name, it is not supported */
561 if (!get_info_func || !rel_info_func || !node_match_func) {
562 pr_err("MD: %s node is not supported\n", node_name);
563 return -EINVAL;
564 }
565
566 mdesc_for_each_node_by_name(hp, hp_node, node_name) {
567 rv = get_info_func(hp, hp_node, &hp_node_info);
568 if (rv != 0)
569 continue;
570
571 if (node_match_func(node_info, &hp_node_info))
572 break;
573
574 rel_info_func(&hp_node_info);
575 }
576
577 rel_info_func(&hp_node_info);
578
579 return hp_node;
580}
581EXPORT_SYMBOL(mdesc_get_node);
582
583int mdesc_get_node_info(struct mdesc_handle *hp, u64 node,
584 const char *node_name, union md_node_info *node_info)
585{
586 mdesc_node_info_get_f get_info_func;
587 int rv;
588
589 if (hp == NULL || node == MDESC_NODE_NULL ||
590 node_name == NULL || node_info == NULL)
591 return -EINVAL;
592
593 /* Find the get_info op for the given node name */
594 mdesc_get_node_ops(node_name, &get_info_func, NULL, NULL);
595
596 /* If we didn't find a get_info_func, the node name is not supported */
597 if (get_info_func == NULL) {
598 pr_err("MD: %s node is not supported\n", node_name);
599 return -EINVAL;
600 }
601
602 rv = get_info_func(hp, node, node_info);
603 if (rv != 0) {
604 pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
605 node_name);
606 return -1;
607 }
608
609 return 0;
610}
611EXPORT_SYMBOL(mdesc_get_node_info);
612
613static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
614{
615 return (struct mdesc_elem *) (mdesc + 1);
616}
617
618static void *name_block(struct mdesc_hdr *mdesc)
619{
620 return ((void *) node_block(mdesc)) + mdesc->node_sz;
621}
622
623static void *data_block(struct mdesc_hdr *mdesc)
624{
625 return ((void *) name_block(mdesc)) + mdesc->name_sz;
626}
627
628u64 mdesc_node_by_name(struct mdesc_handle *hp,
629 u64 from_node, const char *name)
630{
631 struct mdesc_elem *ep = node_block(&hp->mdesc);
632 const char *names = name_block(&hp->mdesc);
633 u64 last_node = hp->mdesc.node_sz / 16;
634 u64 ret;
635
636 if (from_node == MDESC_NODE_NULL) {
637 ret = from_node = 0;
638 } else if (from_node >= last_node) {
639 return MDESC_NODE_NULL;
640 } else {
641 ret = ep[from_node].d.val;
642 }
643
644 while (ret < last_node) {
645 if (ep[ret].tag != MD_NODE)
646 return MDESC_NODE_NULL;
647 if (!strcmp(names + ep[ret].name_offset, name))
648 break;
649 ret = ep[ret].d.val;
650 }
651 if (ret >= last_node)
652 ret = MDESC_NODE_NULL;
653 return ret;
654}
655EXPORT_SYMBOL(mdesc_node_by_name);
656
657const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
658 const char *name, int *lenp)
659{
660 const char *names = name_block(&hp->mdesc);
661 u64 last_node = hp->mdesc.node_sz / 16;
662 void *data = data_block(&hp->mdesc);
663 struct mdesc_elem *ep;
664
665 if (node == MDESC_NODE_NULL || node >= last_node)
666 return NULL;
667
668 ep = node_block(&hp->mdesc) + node;
669 ep++;
670 for (; ep->tag != MD_NODE_END; ep++) {
671 void *val = NULL;
672 int len = 0;
673
674 switch (ep->tag) {
675 case MD_PROP_VAL:
676 val = &ep->d.val;
677 len = 8;
678 break;
679
680 case MD_PROP_STR:
681 case MD_PROP_DATA:
682 val = data + ep->d.data.data_offset;
683 len = ep->d.data.data_len;
684 break;
685
686 default:
687 break;
688 }
689 if (!val)
690 continue;
691
692 if (!strcmp(names + ep->name_offset, name)) {
693 if (lenp)
694 *lenp = len;
695 return val;
696 }
697 }
698
699 return NULL;
700}
701EXPORT_SYMBOL(mdesc_get_property);
702
703u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
704{
705 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
706 const char *names = name_block(&hp->mdesc);
707 u64 last_node = hp->mdesc.node_sz / 16;
708
709 if (from == MDESC_NODE_NULL || from >= last_node)
710 return MDESC_NODE_NULL;
711
712 ep = base + from;
713
714 ep++;
715 for (; ep->tag != MD_NODE_END; ep++) {
716 if (ep->tag != MD_PROP_ARC)
717 continue;
718
719 if (strcmp(names + ep->name_offset, arc_type))
720 continue;
721
722 return ep - base;
723 }
724
725 return MDESC_NODE_NULL;
726}
727EXPORT_SYMBOL(mdesc_next_arc);
728
729u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
730{
731 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
732
733 ep = base + arc;
734
735 return ep->d.val;
736}
737EXPORT_SYMBOL(mdesc_arc_target);
738
739const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
740{
741 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
742 const char *names = name_block(&hp->mdesc);
743 u64 last_node = hp->mdesc.node_sz / 16;
744
745 if (node == MDESC_NODE_NULL || node >= last_node)
746 return NULL;
747
748 ep = base + node;
749 if (ep->tag != MD_NODE)
750 return NULL;
751
752 return names + ep->name_offset;
753}
754EXPORT_SYMBOL(mdesc_node_name);
755
756static u64 max_cpus = 64;
757
758static void __init report_platform_properties(void)
759{
760 struct mdesc_handle *hp = mdesc_grab();
761 u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
762 const char *s;
763 const u64 *v;
764
765 if (pn == MDESC_NODE_NULL) {
766 prom_printf("No platform node in machine-description.\n");
767 prom_halt();
768 }
769
770 s = mdesc_get_property(hp, pn, "banner-name", NULL);
771 printk("PLATFORM: banner-name [%s]\n", s);
772 s = mdesc_get_property(hp, pn, "name", NULL);
773 printk("PLATFORM: name [%s]\n", s);
774
775 v = mdesc_get_property(hp, pn, "hostid", NULL);
776 if (v)
777 printk("PLATFORM: hostid [%08llx]\n", *v);
778 v = mdesc_get_property(hp, pn, "serial#", NULL);
779 if (v)
780 printk("PLATFORM: serial# [%08llx]\n", *v);
781 v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
782 printk("PLATFORM: stick-frequency [%08llx]\n", *v);
783 v = mdesc_get_property(hp, pn, "mac-address", NULL);
784 if (v)
785 printk("PLATFORM: mac-address [%llx]\n", *v);
786 v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
787 if (v)
788 printk("PLATFORM: watchdog-resolution [%llu ms]\n", *v);
789 v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
790 if (v)
791 printk("PLATFORM: watchdog-max-timeout [%llu ms]\n", *v);
792 v = mdesc_get_property(hp, pn, "max-cpus", NULL);
793 if (v) {
794 max_cpus = *v;
795 printk("PLATFORM: max-cpus [%llu]\n", max_cpus);
796 }
797
798#ifdef CONFIG_SMP
799 {
800 int max_cpu, i;
801
802 if (v) {
803 max_cpu = *v;
804 if (max_cpu > NR_CPUS)
805 max_cpu = NR_CPUS;
806 } else {
807 max_cpu = NR_CPUS;
808 }
809 for (i = 0; i < max_cpu; i++)
810 set_cpu_possible(i, true);
811 }
812#endif
813
814 mdesc_release(hp);
815}
816
817static void fill_in_one_cache(cpuinfo_sparc *c, struct mdesc_handle *hp, u64 mp)
818{
819 const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
820 const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
821 const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
822 const char *type;
823 int type_len;
824
825 type = mdesc_get_property(hp, mp, "type", &type_len);
826
827 switch (*level) {
828 case 1:
829 if (of_find_in_proplist(type, "instn", type_len)) {
830 c->icache_size = *size;
831 c->icache_line_size = *line_size;
832 } else if (of_find_in_proplist(type, "data", type_len)) {
833 c->dcache_size = *size;
834 c->dcache_line_size = *line_size;
835 }
836 break;
837
838 case 2:
839 c->ecache_size = *size;
840 c->ecache_line_size = *line_size;
841 break;
842
843 default:
844 break;
845 }
846
847 if (*level == 1) {
848 u64 a;
849
850 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
851 u64 target = mdesc_arc_target(hp, a);
852 const char *name = mdesc_node_name(hp, target);
853
854 if (!strcmp(name, "cache"))
855 fill_in_one_cache(c, hp, target);
856 }
857 }
858}
859
860static void find_back_node_value(struct mdesc_handle *hp, u64 node,
861 char *srch_val,
862 void (*func)(struct mdesc_handle *, u64, int),
863 u64 val, int depth)
864{
865 u64 arc;
866
867 /* Since we have an estimate of recursion depth, do a sanity check. */
868 if (depth == 0)
869 return;
870
871 mdesc_for_each_arc(arc, hp, node, MDESC_ARC_TYPE_BACK) {
872 u64 n = mdesc_arc_target(hp, arc);
873 const char *name = mdesc_node_name(hp, n);
874
875 if (!strcmp(srch_val, name))
876 (*func)(hp, n, val);
877
878 find_back_node_value(hp, n, srch_val, func, val, depth-1);
879 }
880}
881
882static void __mark_core_id(struct mdesc_handle *hp, u64 node,
883 int core_id)
884{
885 const u64 *id = mdesc_get_property(hp, node, "id", NULL);
886
887 if (*id < num_possible_cpus())
888 cpu_data(*id).core_id = core_id;
889}
890
891static void __mark_max_cache_id(struct mdesc_handle *hp, u64 node,
892 int max_cache_id)
893{
894 const u64 *id = mdesc_get_property(hp, node, "id", NULL);
895
896 if (*id < num_possible_cpus()) {
897 cpu_data(*id).max_cache_id = max_cache_id;
898
899 /**
900 * On systems without explicit socket descriptions socket
901 * is max_cache_id
902 */
903 cpu_data(*id).sock_id = max_cache_id;
904 }
905}
906
907static void mark_core_ids(struct mdesc_handle *hp, u64 mp,
908 int core_id)
909{
910 find_back_node_value(hp, mp, "cpu", __mark_core_id, core_id, 10);
911}
912
913static void mark_max_cache_ids(struct mdesc_handle *hp, u64 mp,
914 int max_cache_id)
915{
916 find_back_node_value(hp, mp, "cpu", __mark_max_cache_id,
917 max_cache_id, 10);
918}
919
920static void set_core_ids(struct mdesc_handle *hp)
921{
922 int idx;
923 u64 mp;
924
925 idx = 1;
926
927 /* Identify unique cores by looking for cpus backpointed to by
928 * level 1 instruction caches.
929 */
930 mdesc_for_each_node_by_name(hp, mp, "cache") {
931 const u64 *level;
932 const char *type;
933 int len;
934
935 level = mdesc_get_property(hp, mp, "level", NULL);
936 if (*level != 1)
937 continue;
938
939 type = mdesc_get_property(hp, mp, "type", &len);
940 if (!of_find_in_proplist(type, "instn", len))
941 continue;
942
943 mark_core_ids(hp, mp, idx);
944 idx++;
945 }
946}
947
948static int set_max_cache_ids_by_cache(struct mdesc_handle *hp, int level)
949{
950 u64 mp;
951 int idx = 1;
952 int fnd = 0;
953
954 /**
955 * Identify unique highest level of shared cache by looking for cpus
956 * backpointed to by shared level N caches.
957 */
958 mdesc_for_each_node_by_name(hp, mp, "cache") {
959 const u64 *cur_lvl;
960
961 cur_lvl = mdesc_get_property(hp, mp, "level", NULL);
962 if (*cur_lvl != level)
963 continue;
964 mark_max_cache_ids(hp, mp, idx);
965 idx++;
966 fnd = 1;
967 }
968 return fnd;
969}
970
971static void set_sock_ids_by_socket(struct mdesc_handle *hp, u64 mp)
972{
973 int idx = 1;
974
975 mdesc_for_each_node_by_name(hp, mp, "socket") {
976 u64 a;
977
978 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
979 u64 t = mdesc_arc_target(hp, a);
980 const char *name;
981 const u64 *id;
982
983 name = mdesc_node_name(hp, t);
984 if (strcmp(name, "cpu"))
985 continue;
986
987 id = mdesc_get_property(hp, t, "id", NULL);
988 if (*id < num_possible_cpus())
989 cpu_data(*id).sock_id = idx;
990 }
991 idx++;
992 }
993}
994
995static void set_sock_ids(struct mdesc_handle *hp)
996{
997 u64 mp;
998
999 /**
1000 * Find the highest level of shared cache which pre-T7 is also
1001 * the socket.
1002 */
1003 if (!set_max_cache_ids_by_cache(hp, 3))
1004 set_max_cache_ids_by_cache(hp, 2);
1005
1006 /* If machine description exposes sockets data use it.*/
1007 mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "sockets");
1008 if (mp != MDESC_NODE_NULL)
1009 set_sock_ids_by_socket(hp, mp);
1010}
1011
1012static void mark_proc_ids(struct mdesc_handle *hp, u64 mp, int proc_id)
1013{
1014 u64 a;
1015
1016 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
1017 u64 t = mdesc_arc_target(hp, a);
1018 const char *name;
1019 const u64 *id;
1020
1021 name = mdesc_node_name(hp, t);
1022 if (strcmp(name, "cpu"))
1023 continue;
1024
1025 id = mdesc_get_property(hp, t, "id", NULL);
1026 if (*id < NR_CPUS)
1027 cpu_data(*id).proc_id = proc_id;
1028 }
1029}
1030
1031static void __set_proc_ids(struct mdesc_handle *hp, const char *exec_unit_name)
1032{
1033 int idx;
1034 u64 mp;
1035
1036 idx = 0;
1037 mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
1038 const char *type;
1039 int len;
1040
1041 type = mdesc_get_property(hp, mp, "type", &len);
1042 if (!of_find_in_proplist(type, "int", len) &&
1043 !of_find_in_proplist(type, "integer", len))
1044 continue;
1045
1046 mark_proc_ids(hp, mp, idx);
1047 idx++;
1048 }
1049}
1050
1051static void set_proc_ids(struct mdesc_handle *hp)
1052{
1053 __set_proc_ids(hp, "exec_unit");
1054 __set_proc_ids(hp, "exec-unit");
1055}
1056
1057static void get_one_mondo_bits(const u64 *p, unsigned int *mask,
1058 unsigned long def, unsigned long max)
1059{
1060 u64 val;
1061
1062 if (!p)
1063 goto use_default;
1064 val = *p;
1065
1066 if (!val || val >= 64)
1067 goto use_default;
1068
1069 if (val > max)
1070 val = max;
1071
1072 *mask = ((1U << val) * 64U) - 1U;
1073 return;
1074
1075use_default:
1076 *mask = ((1U << def) * 64U) - 1U;
1077}
1078
1079static void get_mondo_data(struct mdesc_handle *hp, u64 mp,
1080 struct trap_per_cpu *tb)
1081{
1082 static int printed;
1083 const u64 *val;
1084
1085 val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
1086 get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7, ilog2(max_cpus * 2));
1087
1088 val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
1089 get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7, 8);
1090
1091 val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
1092 get_one_mondo_bits(val, &tb->resum_qmask, 6, 7);
1093
1094 val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
1095 get_one_mondo_bits(val, &tb->nonresum_qmask, 2, 2);
1096 if (!printed++) {
1097 pr_info("SUN4V: Mondo queue sizes "
1098 "[cpu(%u) dev(%u) r(%u) nr(%u)]\n",
1099 tb->cpu_mondo_qmask + 1,
1100 tb->dev_mondo_qmask + 1,
1101 tb->resum_qmask + 1,
1102 tb->nonresum_qmask + 1);
1103 }
1104}
1105
1106static void *mdesc_iterate_over_cpus(void *(*func)(struct mdesc_handle *, u64, int, void *), void *arg, cpumask_t *mask)
1107{
1108 struct mdesc_handle *hp = mdesc_grab();
1109 void *ret = NULL;
1110 u64 mp;
1111
1112 mdesc_for_each_node_by_name(hp, mp, "cpu") {
1113 const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
1114 int cpuid = *id;
1115
1116#ifdef CONFIG_SMP
1117 if (cpuid >= NR_CPUS) {
1118 printk(KERN_WARNING "Ignoring CPU %d which is "
1119 ">= NR_CPUS (%d)\n",
1120 cpuid, NR_CPUS);
1121 continue;
1122 }
1123 if (!cpumask_test_cpu(cpuid, mask))
1124 continue;
1125#endif
1126
1127 ret = func(hp, mp, cpuid, arg);
1128 if (ret)
1129 goto out;
1130 }
1131out:
1132 mdesc_release(hp);
1133 return ret;
1134}
1135
1136static void *record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
1137 void *arg)
1138{
1139 ncpus_probed++;
1140#ifdef CONFIG_SMP
1141 set_cpu_present(cpuid, true);
1142#endif
1143 return NULL;
1144}
1145
1146void mdesc_populate_present_mask(cpumask_t *mask)
1147{
1148 if (tlb_type != hypervisor)
1149 return;
1150
1151 ncpus_probed = 0;
1152 mdesc_iterate_over_cpus(record_one_cpu, NULL, mask);
1153}
1154
1155static void * __init check_one_pgsz(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
1156{
1157 const u64 *pgsz_prop = mdesc_get_property(hp, mp, "mmu-page-size-list", NULL);
1158 unsigned long *pgsz_mask = arg;
1159 u64 val;
1160
1161 val = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
1162 HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
1163 if (pgsz_prop)
1164 val = *pgsz_prop;
1165
1166 if (!*pgsz_mask)
1167 *pgsz_mask = val;
1168 else
1169 *pgsz_mask &= val;
1170 return NULL;
1171}
1172
1173void __init mdesc_get_page_sizes(cpumask_t *mask, unsigned long *pgsz_mask)
1174{
1175 *pgsz_mask = 0;
1176 mdesc_iterate_over_cpus(check_one_pgsz, pgsz_mask, mask);
1177}
1178
1179static void *fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
1180 void *arg)
1181{
1182 const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
1183 struct trap_per_cpu *tb;
1184 cpuinfo_sparc *c;
1185 u64 a;
1186
1187#ifndef CONFIG_SMP
1188 /* On uniprocessor we only want the values for the
1189 * real physical cpu the kernel booted onto, however
1190 * cpu_data() only has one entry at index 0.
1191 */
1192 if (cpuid != real_hard_smp_processor_id())
1193 return NULL;
1194 cpuid = 0;
1195#endif
1196
1197 c = &cpu_data(cpuid);
1198 c->clock_tick = *cfreq;
1199
1200 tb = &trap_block[cpuid];
1201 get_mondo_data(hp, mp, tb);
1202
1203 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
1204 u64 j, t = mdesc_arc_target(hp, a);
1205 const char *t_name;
1206
1207 t_name = mdesc_node_name(hp, t);
1208 if (!strcmp(t_name, "cache")) {
1209 fill_in_one_cache(c, hp, t);
1210 continue;
1211 }
1212
1213 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
1214 u64 n = mdesc_arc_target(hp, j);
1215 const char *n_name;
1216
1217 n_name = mdesc_node_name(hp, n);
1218 if (!strcmp(n_name, "cache"))
1219 fill_in_one_cache(c, hp, n);
1220 }
1221 }
1222
1223 c->core_id = 0;
1224 c->proc_id = -1;
1225
1226 return NULL;
1227}
1228
1229void mdesc_fill_in_cpu_data(cpumask_t *mask)
1230{
1231 struct mdesc_handle *hp;
1232
1233 mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask);
1234
1235 hp = mdesc_grab();
1236
1237 set_core_ids(hp);
1238 set_proc_ids(hp);
1239 set_sock_ids(hp);
1240
1241 mdesc_release(hp);
1242
1243 smp_fill_in_sib_core_maps();
1244}
1245
1246/* mdesc_open() - Grab a reference to mdesc_handle when /dev/mdesc is
1247 * opened. Hold this reference until /dev/mdesc is closed to ensure
1248 * mdesc data structure is not released underneath us. Store the
1249 * pointer to mdesc structure in private_data for read and seek to use
1250 */
1251static int mdesc_open(struct inode *inode, struct file *file)
1252{
1253 struct mdesc_handle *hp = mdesc_grab();
1254
1255 if (!hp)
1256 return -ENODEV;
1257
1258 file->private_data = hp;
1259
1260 return 0;
1261}
1262
1263static ssize_t mdesc_read(struct file *file, char __user *buf,
1264 size_t len, loff_t *offp)
1265{
1266 struct mdesc_handle *hp = file->private_data;
1267 unsigned char *mdesc;
1268 int bytes_left, count = len;
1269
1270 if (*offp >= hp->handle_size)
1271 return 0;
1272
1273 bytes_left = hp->handle_size - *offp;
1274 if (count > bytes_left)
1275 count = bytes_left;
1276
1277 mdesc = (unsigned char *)&hp->mdesc;
1278 mdesc += *offp;
1279 if (!copy_to_user(buf, mdesc, count)) {
1280 *offp += count;
1281 return count;
1282 } else {
1283 return -EFAULT;
1284 }
1285}
1286
1287static loff_t mdesc_llseek(struct file *file, loff_t offset, int whence)
1288{
1289 struct mdesc_handle *hp = file->private_data;
1290
1291 return no_seek_end_llseek_size(file, offset, whence, hp->handle_size);
1292}
1293
1294/* mdesc_close() - /dev/mdesc is being closed, release the reference to
1295 * mdesc structure.
1296 */
1297static int mdesc_close(struct inode *inode, struct file *file)
1298{
1299 mdesc_release(file->private_data);
1300 return 0;
1301}
1302
1303static const struct file_operations mdesc_fops = {
1304 .open = mdesc_open,
1305 .read = mdesc_read,
1306 .llseek = mdesc_llseek,
1307 .release = mdesc_close,
1308 .owner = THIS_MODULE,
1309};
1310
1311static struct miscdevice mdesc_misc = {
1312 .minor = MISC_DYNAMIC_MINOR,
1313 .name = "mdesc",
1314 .fops = &mdesc_fops,
1315};
1316
1317static int __init mdesc_misc_init(void)
1318{
1319 return misc_register(&mdesc_misc);
1320}
1321
1322__initcall(mdesc_misc_init);
1323
1324void __init sun4v_mdesc_init(void)
1325{
1326 struct mdesc_handle *hp;
1327 unsigned long len, real_len, status;
1328
1329 (void) sun4v_mach_desc(0UL, 0UL, &len);
1330
1331 printk("MDESC: Size is %lu bytes.\n", len);
1332
1333 hp = mdesc_alloc(len, &memblock_mdesc_ops);
1334 if (hp == NULL) {
1335 prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
1336 prom_halt();
1337 }
1338
1339 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
1340 if (status != HV_EOK || real_len > len) {
1341 prom_printf("sun4v_mach_desc fails, err(%lu), "
1342 "len(%lu), real_len(%lu)\n",
1343 status, len, real_len);
1344 mdesc_free(hp);
1345 prom_halt();
1346 }
1347
1348 cur_mdesc = hp;
1349
1350 mdesc_adi_init();
1351 report_platform_properties();
1352}