1 /* mdesc.c: Sun4V machine description handling.
3 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
5 #include <linux/kernel.h>
6 #include <linux/types.h>
8 #include <linux/log2.h>
9 #include <linux/list.h>
10 #include <linux/slab.h>
12 #include <linux/miscdevice.h>
14 #include <asm/hypervisor.h>
15 #include <asm/mdesc.h>
17 #include <asm/oplib.h>
20 /* Unlike the OBP device tree, the machine description is a full-on
21 * DAG. An arbitrary number of ARCs are possible from one
22 * node to other nodes and thus we can't use the OBP device_node
23 * data structure to represent these nodes inside of the kernel.
25 * Actually, it isn't even a DAG, because there are back pointers
26 * which create cycles in the graph.
28 * mdesc_hdr and mdesc_elem describe the layout of the data structure
29 * we get from the Hypervisor.
32 u32 version; /* Transport version */
33 u32 node_sz; /* node block size */
34 u32 name_sz; /* name block size */
35 u32 data_sz; /* data block size */
36 } __attribute__((aligned(16)));
40 #define MD_LIST_END 0x00
42 #define MD_NODE_END 0x45
44 #define MD_PROP_ARC 0x61
45 #define MD_PROP_VAL 0x76
46 #define MD_PROP_STR 0x73
47 #define MD_PROP_DATA 0x64
60 struct mdesc_mem_ops {
61 struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
62 void (*free)(struct mdesc_handle *handle);
66 struct list_head list;
67 struct mdesc_mem_ops *mops;
70 unsigned int handle_size;
71 struct mdesc_hdr mdesc;
74 static void mdesc_handle_init(struct mdesc_handle *hp,
75 unsigned int handle_size,
78 BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));
80 memset(hp, 0, handle_size);
81 INIT_LIST_HEAD(&hp->list);
83 atomic_set(&hp->refcnt, 1);
84 hp->handle_size = handle_size;
87 static struct mdesc_handle * __init mdesc_lmb_alloc(unsigned int mdesc_size)
89 unsigned int handle_size, alloc_size;
90 struct mdesc_handle *hp;
93 handle_size = (sizeof(struct mdesc_handle) -
94 sizeof(struct mdesc_hdr) +
96 alloc_size = PAGE_ALIGN(handle_size);
98 paddr = lmb_alloc(alloc_size, PAGE_SIZE);
103 mdesc_handle_init(hp, handle_size, hp);
108 static void mdesc_lmb_free(struct mdesc_handle *hp)
110 unsigned int alloc_size, handle_size = hp->handle_size;
111 unsigned long start, end;
113 BUG_ON(atomic_read(&hp->refcnt) != 0);
114 BUG_ON(!list_empty(&hp->list));
116 alloc_size = PAGE_ALIGN(handle_size);
118 start = (unsigned long) hp;
119 end = start + alloc_size;
121 while (start < end) {
124 p = virt_to_page(start);
125 ClearPageReserved(p);
131 static struct mdesc_mem_ops lmb_mdesc_ops = {
132 .alloc = mdesc_lmb_alloc,
133 .free = mdesc_lmb_free,
136 static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
138 unsigned int handle_size;
141 handle_size = (sizeof(struct mdesc_handle) -
142 sizeof(struct mdesc_hdr) +
145 base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_NOFAIL);
147 struct mdesc_handle *hp;
150 addr = (unsigned long)base;
151 addr = (addr + 15UL) & ~15UL;
152 hp = (struct mdesc_handle *) addr;
154 mdesc_handle_init(hp, handle_size, base);
161 static void mdesc_kfree(struct mdesc_handle *hp)
163 BUG_ON(atomic_read(&hp->refcnt) != 0);
164 BUG_ON(!list_empty(&hp->list));
166 kfree(hp->self_base);
169 static struct mdesc_mem_ops kmalloc_mdesc_memops = {
170 .alloc = mdesc_kmalloc,
174 static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
175 struct mdesc_mem_ops *mops)
177 struct mdesc_handle *hp = mops->alloc(mdesc_size);
185 static void mdesc_free(struct mdesc_handle *hp)
190 static struct mdesc_handle *cur_mdesc;
191 static LIST_HEAD(mdesc_zombie_list);
192 static DEFINE_SPINLOCK(mdesc_lock);
194 struct mdesc_handle *mdesc_grab(void)
196 struct mdesc_handle *hp;
199 spin_lock_irqsave(&mdesc_lock, flags);
202 atomic_inc(&hp->refcnt);
203 spin_unlock_irqrestore(&mdesc_lock, flags);
207 EXPORT_SYMBOL(mdesc_grab);
209 void mdesc_release(struct mdesc_handle *hp)
213 spin_lock_irqsave(&mdesc_lock, flags);
214 if (atomic_dec_and_test(&hp->refcnt)) {
215 list_del_init(&hp->list);
218 spin_unlock_irqrestore(&mdesc_lock, flags);
220 EXPORT_SYMBOL(mdesc_release);
222 static DEFINE_MUTEX(mdesc_mutex);
223 static struct mdesc_notifier_client *client_list;
225 void mdesc_register_notifier(struct mdesc_notifier_client *client)
229 mutex_lock(&mdesc_mutex);
230 client->next = client_list;
231 client_list = client;
233 mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
234 client->add(cur_mdesc, node);
236 mutex_unlock(&mdesc_mutex);
239 static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
245 mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
248 target = mdesc_arc_target(hp, a);
249 id = mdesc_get_property(hp, target,
258 /* Run 'func' on nodes which are in A but not in B. */
259 static void invoke_on_missing(const char *name,
260 struct mdesc_handle *a,
261 struct mdesc_handle *b,
262 void (*func)(struct mdesc_handle *, u64))
266 mdesc_for_each_node_by_name(a, node, name) {
267 int found = 0, is_vdc_port = 0;
268 const char *name_prop;
272 name_prop = mdesc_get_property(a, node, "name", NULL);
273 if (name_prop && !strcmp(name_prop, "vdc-port")) {
275 id = parent_cfg_handle(a, node);
277 id = mdesc_get_property(a, node, "id", NULL);
280 printk(KERN_ERR "MD: Cannot find ID for %s node.\n",
281 (name_prop ? name_prop : name));
285 mdesc_for_each_node_by_name(b, fnode, name) {
289 name_prop = mdesc_get_property(b, fnode,
292 strcmp(name_prop, "vdc-port"))
294 fid = parent_cfg_handle(b, fnode);
296 printk(KERN_ERR "MD: Cannot find ID "
297 "for vdc-port node.\n");
301 fid = mdesc_get_property(b, fnode,
314 static void notify_one(struct mdesc_notifier_client *p,
315 struct mdesc_handle *old_hp,
316 struct mdesc_handle *new_hp)
318 invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
319 invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
322 static void mdesc_notify_clients(struct mdesc_handle *old_hp,
323 struct mdesc_handle *new_hp)
325 struct mdesc_notifier_client *p = client_list;
328 notify_one(p, old_hp, new_hp);
333 void mdesc_update(void)
335 unsigned long len, real_len, status;
336 struct mdesc_handle *hp, *orig_hp;
339 mutex_lock(&mdesc_mutex);
341 (void) sun4v_mach_desc(0UL, 0UL, &len);
343 hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
345 printk(KERN_ERR "MD: mdesc alloc fails\n");
349 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
350 if (status != HV_EOK || real_len > len) {
351 printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
353 atomic_dec(&hp->refcnt);
358 spin_lock_irqsave(&mdesc_lock, flags);
361 spin_unlock_irqrestore(&mdesc_lock, flags);
363 mdesc_notify_clients(orig_hp, hp);
365 spin_lock_irqsave(&mdesc_lock, flags);
366 if (atomic_dec_and_test(&orig_hp->refcnt))
369 list_add(&orig_hp->list, &mdesc_zombie_list);
370 spin_unlock_irqrestore(&mdesc_lock, flags);
373 mutex_unlock(&mdesc_mutex);
376 static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
378 return (struct mdesc_elem *) (mdesc + 1);
381 static void *name_block(struct mdesc_hdr *mdesc)
383 return ((void *) node_block(mdesc)) + mdesc->node_sz;
386 static void *data_block(struct mdesc_hdr *mdesc)
388 return ((void *) name_block(mdesc)) + mdesc->name_sz;
391 u64 mdesc_node_by_name(struct mdesc_handle *hp,
392 u64 from_node, const char *name)
394 struct mdesc_elem *ep = node_block(&hp->mdesc);
395 const char *names = name_block(&hp->mdesc);
396 u64 last_node = hp->mdesc.node_sz / 16;
399 if (from_node == MDESC_NODE_NULL) {
401 } else if (from_node >= last_node) {
402 return MDESC_NODE_NULL;
404 ret = ep[from_node].d.val;
407 while (ret < last_node) {
408 if (ep[ret].tag != MD_NODE)
409 return MDESC_NODE_NULL;
410 if (!strcmp(names + ep[ret].name_offset, name))
414 if (ret >= last_node)
415 ret = MDESC_NODE_NULL;
418 EXPORT_SYMBOL(mdesc_node_by_name);
420 const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
421 const char *name, int *lenp)
423 const char *names = name_block(&hp->mdesc);
424 u64 last_node = hp->mdesc.node_sz / 16;
425 void *data = data_block(&hp->mdesc);
426 struct mdesc_elem *ep;
428 if (node == MDESC_NODE_NULL || node >= last_node)
431 ep = node_block(&hp->mdesc) + node;
433 for (; ep->tag != MD_NODE_END; ep++) {
445 val = data + ep->d.data.data_offset;
446 len = ep->d.data.data_len;
455 if (!strcmp(names + ep->name_offset, name)) {
464 EXPORT_SYMBOL(mdesc_get_property);
466 u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
468 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
469 const char *names = name_block(&hp->mdesc);
470 u64 last_node = hp->mdesc.node_sz / 16;
472 if (from == MDESC_NODE_NULL || from >= last_node)
473 return MDESC_NODE_NULL;
478 for (; ep->tag != MD_NODE_END; ep++) {
479 if (ep->tag != MD_PROP_ARC)
482 if (strcmp(names + ep->name_offset, arc_type))
488 return MDESC_NODE_NULL;
490 EXPORT_SYMBOL(mdesc_next_arc);
492 u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
494 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
500 EXPORT_SYMBOL(mdesc_arc_target);
502 const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
504 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
505 const char *names = name_block(&hp->mdesc);
506 u64 last_node = hp->mdesc.node_sz / 16;
508 if (node == MDESC_NODE_NULL || node >= last_node)
512 if (ep->tag != MD_NODE)
515 return names + ep->name_offset;
517 EXPORT_SYMBOL(mdesc_node_name);
519 static void __init report_platform_properties(void)
521 struct mdesc_handle *hp = mdesc_grab();
522 u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
526 if (pn == MDESC_NODE_NULL) {
527 prom_printf("No platform node in machine-description.\n");
531 s = mdesc_get_property(hp, pn, "banner-name", NULL);
532 printk("PLATFORM: banner-name [%s]\n", s);
533 s = mdesc_get_property(hp, pn, "name", NULL);
534 printk("PLATFORM: name [%s]\n", s);
536 v = mdesc_get_property(hp, pn, "hostid", NULL);
538 printk("PLATFORM: hostid [%08lx]\n", *v);
539 v = mdesc_get_property(hp, pn, "serial#", NULL);
541 printk("PLATFORM: serial# [%08lx]\n", *v);
542 v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
543 printk("PLATFORM: stick-frequency [%08lx]\n", *v);
544 v = mdesc_get_property(hp, pn, "mac-address", NULL);
546 printk("PLATFORM: mac-address [%lx]\n", *v);
547 v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
549 printk("PLATFORM: watchdog-resolution [%lu ms]\n", *v);
550 v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
552 printk("PLATFORM: watchdog-max-timeout [%lu ms]\n", *v);
553 v = mdesc_get_property(hp, pn, "max-cpus", NULL);
555 printk("PLATFORM: max-cpus [%lu]\n", *v);
563 if (max_cpu > NR_CPUS)
568 for (i = 0; i < max_cpu; i++)
569 cpu_set(i, cpu_possible_map);
576 static void __devinit fill_in_one_cache(cpuinfo_sparc *c,
577 struct mdesc_handle *hp,
580 const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
581 const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
582 const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
586 type = mdesc_get_property(hp, mp, "type", &type_len);
590 if (of_find_in_proplist(type, "instn", type_len)) {
591 c->icache_size = *size;
592 c->icache_line_size = *line_size;
593 } else if (of_find_in_proplist(type, "data", type_len)) {
594 c->dcache_size = *size;
595 c->dcache_line_size = *line_size;
600 c->ecache_size = *size;
601 c->ecache_line_size = *line_size;
611 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
612 u64 target = mdesc_arc_target(hp, a);
613 const char *name = mdesc_node_name(hp, target);
615 if (!strcmp(name, "cache"))
616 fill_in_one_cache(c, hp, target);
621 static void __devinit mark_core_ids(struct mdesc_handle *hp, u64 mp,
626 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
627 u64 t = mdesc_arc_target(hp, a);
631 name = mdesc_node_name(hp, t);
632 if (!strcmp(name, "cpu")) {
633 id = mdesc_get_property(hp, t, "id", NULL);
635 cpu_data(*id).core_id = core_id;
639 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_BACK) {
640 u64 n = mdesc_arc_target(hp, j);
643 n_name = mdesc_node_name(hp, n);
644 if (strcmp(n_name, "cpu"))
647 id = mdesc_get_property(hp, n, "id", NULL);
649 cpu_data(*id).core_id = core_id;
655 static void __devinit set_core_ids(struct mdesc_handle *hp)
661 mdesc_for_each_node_by_name(hp, mp, "cache") {
666 level = mdesc_get_property(hp, mp, "level", NULL);
670 type = mdesc_get_property(hp, mp, "type", &len);
671 if (!of_find_in_proplist(type, "instn", len))
674 mark_core_ids(hp, mp, idx);
680 static void __devinit mark_proc_ids(struct mdesc_handle *hp, u64 mp,
685 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
686 u64 t = mdesc_arc_target(hp, a);
690 name = mdesc_node_name(hp, t);
691 if (strcmp(name, "cpu"))
694 id = mdesc_get_property(hp, t, "id", NULL);
696 cpu_data(*id).proc_id = proc_id;
700 static void __devinit __set_proc_ids(struct mdesc_handle *hp,
701 const char *exec_unit_name)
707 mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
711 type = mdesc_get_property(hp, mp, "type", &len);
712 if (!of_find_in_proplist(type, "int", len) &&
713 !of_find_in_proplist(type, "integer", len))
716 mark_proc_ids(hp, mp, idx);
722 static void __devinit set_proc_ids(struct mdesc_handle *hp)
724 __set_proc_ids(hp, "exec_unit");
725 __set_proc_ids(hp, "exec-unit");
728 static void __devinit get_one_mondo_bits(const u64 *p, unsigned int *mask,
737 if (!val || val >= 64)
740 *mask = ((1U << val) * 64U) - 1U;
744 *mask = ((1U << def) * 64U) - 1U;
747 static void __devinit get_mondo_data(struct mdesc_handle *hp, u64 mp,
748 struct trap_per_cpu *tb)
752 val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
753 get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7);
755 val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
756 get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7);
758 val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
759 get_one_mondo_bits(val, &tb->resum_qmask, 6);
761 val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
762 get_one_mondo_bits(val, &tb->nonresum_qmask, 2);
765 void __cpuinit mdesc_fill_in_cpu_data(cpumask_t mask)
767 struct mdesc_handle *hp = mdesc_grab();
771 mdesc_for_each_node_by_name(hp, mp, "cpu") {
772 const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
773 const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
774 struct trap_per_cpu *tb;
784 if (cpuid >= NR_CPUS) {
785 printk(KERN_WARNING "Ignoring CPU %d which is "
790 if (!cpu_isset(cpuid, mask))
793 /* On uniprocessor we only want the values for the
794 * real physical cpu the kernel booted onto, however
795 * cpu_data() only has one entry at index 0.
797 if (cpuid != real_hard_smp_processor_id())
802 c = &cpu_data(cpuid);
803 c->clock_tick = *cfreq;
805 tb = &trap_block[cpuid];
806 get_mondo_data(hp, mp, tb);
808 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
809 u64 j, t = mdesc_arc_target(hp, a);
812 t_name = mdesc_node_name(hp, t);
813 if (!strcmp(t_name, "cache")) {
814 fill_in_one_cache(c, hp, t);
818 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
819 u64 n = mdesc_arc_target(hp, j);
822 n_name = mdesc_node_name(hp, n);
823 if (!strcmp(n_name, "cache"))
824 fill_in_one_cache(c, hp, n);
829 cpu_set(cpuid, cpu_present_map);
837 sparc64_multi_core = 1;
843 smp_fill_in_sib_core_maps();
848 static ssize_t mdesc_read(struct file *file, char __user *buf,
849 size_t len, loff_t *offp)
851 struct mdesc_handle *hp = mdesc_grab();
857 err = hp->handle_size;
858 if (len < hp->handle_size)
860 else if (copy_to_user(buf, &hp->mdesc, hp->handle_size))
867 static const struct file_operations mdesc_fops = {
869 .owner = THIS_MODULE,
872 static struct miscdevice mdesc_misc = {
873 .minor = MISC_DYNAMIC_MINOR,
878 static int __init mdesc_misc_init(void)
880 return misc_register(&mdesc_misc);
883 __initcall(mdesc_misc_init);
885 void __init sun4v_mdesc_init(void)
887 struct mdesc_handle *hp;
888 unsigned long len, real_len, status;
891 (void) sun4v_mach_desc(0UL, 0UL, &len);
893 printk("MDESC: Size is %lu bytes.\n", len);
895 hp = mdesc_alloc(len, &lmb_mdesc_ops);
897 prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
901 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
902 if (status != HV_EOK || real_len > len) {
903 prom_printf("sun4v_mach_desc fails, err(%lu), "
904 "len(%lu), real_len(%lu)\n",
905 status, len, real_len);
912 report_platform_properties();
915 mdesc_fill_in_cpu_data(mask);