1 /* mdesc.c: Sun4V machine description handling.
3 * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
5 #include <linux/kernel.h>
6 #include <linux/types.h>
7 #include <linux/bootmem.h>
8 #include <linux/log2.h>
9 #include <linux/list.h>
10 #include <linux/slab.h>
13 #include <asm/hypervisor.h>
14 #include <asm/mdesc.h>
16 #include <asm/oplib.h>
19 /* Unlike the OBP device tree, the machine description is a full-on
20 * DAG. An arbitrary number of ARCs are possible from one
21 * node to other nodes and thus we can't use the OBP device_node
22 * data structure to represent these nodes inside of the kernel.
24 * Actually, it isn't even a DAG, because there are back pointers
25 * which create cycles in the graph.
27 * mdesc_hdr and mdesc_elem describe the layout of the data structure
28 * we get from the Hypervisor.
31 u32 version; /* Transport version */
32 u32 node_sz; /* node block size */
33 u32 name_sz; /* name block size */
34 u32 data_sz; /* data block size */
35 } __attribute__((aligned(16)));
39 #define MD_LIST_END 0x00
41 #define MD_NODE_END 0x45
43 #define MD_PROP_ARC 0x61
44 #define MD_PROP_VAL 0x76
45 #define MD_PROP_STR 0x73
46 #define MD_PROP_DATA 0x64
59 struct mdesc_mem_ops {
60 struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
61 void (*free)(struct mdesc_handle *handle);
65 struct list_head list;
66 struct mdesc_mem_ops *mops;
69 unsigned int handle_size;
70 struct mdesc_hdr mdesc;
73 static void mdesc_handle_init(struct mdesc_handle *hp,
74 unsigned int handle_size,
77 BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));
79 memset(hp, 0, handle_size);
80 INIT_LIST_HEAD(&hp->list);
82 atomic_set(&hp->refcnt, 1);
83 hp->handle_size = handle_size;
86 static struct mdesc_handle *mdesc_bootmem_alloc(unsigned int mdesc_size)
88 struct mdesc_handle *hp;
89 unsigned int handle_size, alloc_size;
91 handle_size = (sizeof(struct mdesc_handle) -
92 sizeof(struct mdesc_hdr) +
94 alloc_size = PAGE_ALIGN(handle_size);
96 hp = __alloc_bootmem(alloc_size, PAGE_SIZE, 0UL);
98 mdesc_handle_init(hp, handle_size, hp);
103 static void mdesc_bootmem_free(struct mdesc_handle *hp)
105 unsigned int alloc_size, handle_size = hp->handle_size;
106 unsigned long start, end;
108 BUG_ON(atomic_read(&hp->refcnt) != 0);
109 BUG_ON(!list_empty(&hp->list));
111 alloc_size = PAGE_ALIGN(handle_size);
113 start = (unsigned long) hp;
114 end = start + alloc_size;
116 while (start < end) {
119 p = virt_to_page(start);
120 ClearPageReserved(p);
126 static struct mdesc_mem_ops bootmem_mdesc_memops = {
127 .alloc = mdesc_bootmem_alloc,
128 .free = mdesc_bootmem_free,
131 static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
133 unsigned int handle_size;
136 handle_size = (sizeof(struct mdesc_handle) -
137 sizeof(struct mdesc_hdr) +
140 base = kmalloc(handle_size + 15, GFP_KERNEL);
142 struct mdesc_handle *hp;
145 addr = (unsigned long)base;
146 addr = (addr + 15UL) & ~15UL;
147 hp = (struct mdesc_handle *) addr;
149 mdesc_handle_init(hp, handle_size, base);
156 static void mdesc_kfree(struct mdesc_handle *hp)
158 BUG_ON(atomic_read(&hp->refcnt) != 0);
159 BUG_ON(!list_empty(&hp->list));
161 kfree(hp->self_base);
164 static struct mdesc_mem_ops kmalloc_mdesc_memops = {
165 .alloc = mdesc_kmalloc,
169 static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
170 struct mdesc_mem_ops *mops)
172 struct mdesc_handle *hp = mops->alloc(mdesc_size);
180 static void mdesc_free(struct mdesc_handle *hp)
185 static struct mdesc_handle *cur_mdesc;
186 static LIST_HEAD(mdesc_zombie_list);
187 static DEFINE_SPINLOCK(mdesc_lock);
189 struct mdesc_handle *mdesc_grab(void)
191 struct mdesc_handle *hp;
194 spin_lock_irqsave(&mdesc_lock, flags);
197 atomic_inc(&hp->refcnt);
198 spin_unlock_irqrestore(&mdesc_lock, flags);
202 EXPORT_SYMBOL(mdesc_grab);
204 void mdesc_release(struct mdesc_handle *hp)
208 spin_lock_irqsave(&mdesc_lock, flags);
209 if (atomic_dec_and_test(&hp->refcnt)) {
210 list_del_init(&hp->list);
213 spin_unlock_irqrestore(&mdesc_lock, flags);
215 EXPORT_SYMBOL(mdesc_release);
217 static void do_mdesc_update(struct work_struct *work)
219 unsigned long len, real_len, status;
220 struct mdesc_handle *hp, *orig_hp;
223 (void) sun4v_mach_desc(0UL, 0UL, &len);
225 hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
227 printk(KERN_ERR "MD: mdesc alloc fails\n");
231 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
232 if (status != HV_EOK || real_len > len) {
233 printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
235 atomic_dec(&hp->refcnt);
240 spin_lock_irqsave(&mdesc_lock, flags);
244 if (atomic_dec_and_test(&orig_hp->refcnt))
247 list_add(&orig_hp->list, &mdesc_zombie_list);
248 spin_unlock_irqrestore(&mdesc_lock, flags);
251 static DECLARE_WORK(mdesc_update_work, do_mdesc_update);
253 void mdesc_update(void)
255 schedule_work(&mdesc_update_work);
258 static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
260 return (struct mdesc_elem *) (mdesc + 1);
263 static void *name_block(struct mdesc_hdr *mdesc)
265 return ((void *) node_block(mdesc)) + mdesc->node_sz;
268 static void *data_block(struct mdesc_hdr *mdesc)
270 return ((void *) name_block(mdesc)) + mdesc->name_sz;
273 u64 mdesc_node_by_name(struct mdesc_handle *hp,
274 u64 from_node, const char *name)
276 struct mdesc_elem *ep = node_block(&hp->mdesc);
277 const char *names = name_block(&hp->mdesc);
278 u64 last_node = hp->mdesc.node_sz / 16;
281 if (from_node == MDESC_NODE_NULL)
284 if (from_node >= last_node)
285 return MDESC_NODE_NULL;
287 ret = ep[from_node].d.val;
288 while (ret < last_node) {
289 if (ep[ret].tag != MD_NODE)
290 return MDESC_NODE_NULL;
291 if (!strcmp(names + ep[ret].name_offset, name))
295 if (ret >= last_node)
296 ret = MDESC_NODE_NULL;
299 EXPORT_SYMBOL(mdesc_node_by_name);
301 const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
302 const char *name, int *lenp)
304 const char *names = name_block(&hp->mdesc);
305 u64 last_node = hp->mdesc.node_sz / 16;
306 void *data = data_block(&hp->mdesc);
307 struct mdesc_elem *ep;
309 if (node == MDESC_NODE_NULL || node >= last_node)
312 ep = node_block(&hp->mdesc) + node;
314 for (; ep->tag != MD_NODE_END; ep++) {
326 val = data + ep->d.data.data_offset;
327 len = ep->d.data.data_len;
336 if (!strcmp(names + ep->name_offset, name)) {
345 EXPORT_SYMBOL(mdesc_get_property);
347 u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
349 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
350 const char *names = name_block(&hp->mdesc);
351 u64 last_node = hp->mdesc.node_sz / 16;
353 if (from == MDESC_NODE_NULL || from >= last_node)
354 return MDESC_NODE_NULL;
359 for (; ep->tag != MD_NODE_END; ep++) {
360 if (ep->tag != MD_PROP_ARC)
363 if (strcmp(names + ep->name_offset, arc_type))
369 return MDESC_NODE_NULL;
371 EXPORT_SYMBOL(mdesc_next_arc);
373 u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
375 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
381 EXPORT_SYMBOL(mdesc_arc_target);
383 const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
385 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
386 const char *names = name_block(&hp->mdesc);
387 u64 last_node = hp->mdesc.node_sz / 16;
389 if (node == MDESC_NODE_NULL || node >= last_node)
393 if (ep->tag != MD_NODE)
396 return names + ep->name_offset;
398 EXPORT_SYMBOL(mdesc_node_name);
400 static void __init report_platform_properties(void)
402 struct mdesc_handle *hp = mdesc_grab();
403 u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
407 if (pn == MDESC_NODE_NULL) {
408 prom_printf("No platform node in machine-description.\n");
412 s = mdesc_get_property(hp, pn, "banner-name", NULL);
413 printk("PLATFORM: banner-name [%s]\n", s);
414 s = mdesc_get_property(hp, pn, "name", NULL);
415 printk("PLATFORM: name [%s]\n", s);
417 v = mdesc_get_property(hp, pn, "hostid", NULL);
419 printk("PLATFORM: hostid [%08lx]\n", *v);
420 v = mdesc_get_property(hp, pn, "serial#", NULL);
422 printk("PLATFORM: serial# [%08lx]\n", *v);
423 v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
424 printk("PLATFORM: stick-frequency [%08lx]\n", *v);
425 v = mdesc_get_property(hp, pn, "mac-address", NULL);
427 printk("PLATFORM: mac-address [%lx]\n", *v);
428 v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
430 printk("PLATFORM: watchdog-resolution [%lu ms]\n", *v);
431 v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
433 printk("PLATFORM: watchdog-max-timeout [%lu ms]\n", *v);
434 v = mdesc_get_property(hp, pn, "max-cpus", NULL);
436 printk("PLATFORM: max-cpus [%lu]\n", *v);
444 if (max_cpu > NR_CPUS)
449 for (i = 0; i < max_cpu; i++)
450 cpu_set(i, cpu_possible_map);
457 static int inline find_in_proplist(const char *list, const char *match, int len)
462 if (!strcmp(list, match))
464 l = strlen(list) + 1;
471 static void __devinit fill_in_one_cache(cpuinfo_sparc *c,
472 struct mdesc_handle *hp,
475 const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
476 const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
477 const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
481 type = mdesc_get_property(hp, mp, "type", &type_len);
485 if (find_in_proplist(type, "instn", type_len)) {
486 c->icache_size = *size;
487 c->icache_line_size = *line_size;
488 } else if (find_in_proplist(type, "data", type_len)) {
489 c->dcache_size = *size;
490 c->dcache_line_size = *line_size;
495 c->ecache_size = *size;
496 c->ecache_line_size = *line_size;
506 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
507 u64 target = mdesc_arc_target(hp, a);
508 const char *name = mdesc_node_name(hp, target);
510 if (!strcmp(name, "cache"))
511 fill_in_one_cache(c, hp, target);
516 static void __devinit mark_core_ids(struct mdesc_handle *hp, u64 mp,
521 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
522 u64 t = mdesc_arc_target(hp, a);
526 name = mdesc_node_name(hp, t);
527 if (!strcmp(name, "cpu")) {
528 id = mdesc_get_property(hp, t, "id", NULL);
530 cpu_data(*id).core_id = core_id;
534 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_BACK) {
535 u64 n = mdesc_arc_target(hp, j);
538 n_name = mdesc_node_name(hp, n);
539 if (strcmp(n_name, "cpu"))
542 id = mdesc_get_property(hp, n, "id", NULL);
544 cpu_data(*id).core_id = core_id;
550 static void __devinit set_core_ids(struct mdesc_handle *hp)
556 mdesc_for_each_node_by_name(hp, mp, "cache") {
561 level = mdesc_get_property(hp, mp, "level", NULL);
565 type = mdesc_get_property(hp, mp, "type", &len);
566 if (!find_in_proplist(type, "instn", len))
569 mark_core_ids(hp, mp, idx);
575 static void __devinit mark_proc_ids(struct mdesc_handle *hp, u64 mp,
580 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
581 u64 t = mdesc_arc_target(hp, a);
585 name = mdesc_node_name(hp, t);
586 if (strcmp(name, "cpu"))
589 id = mdesc_get_property(hp, t, "id", NULL);
591 cpu_data(*id).proc_id = proc_id;
595 static void __devinit __set_proc_ids(struct mdesc_handle *hp,
596 const char *exec_unit_name)
602 mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
606 type = mdesc_get_property(hp, mp, "type", &len);
607 if (!find_in_proplist(type, "int", len) &&
608 !find_in_proplist(type, "integer", len))
611 mark_proc_ids(hp, mp, idx);
617 static void __devinit set_proc_ids(struct mdesc_handle *hp)
619 __set_proc_ids(hp, "exec_unit");
620 __set_proc_ids(hp, "exec-unit");
623 static void __devinit get_one_mondo_bits(const u64 *p, unsigned int *mask,
632 if (!val || val >= 64)
635 *mask = ((1U << val) * 64U) - 1U;
639 *mask = ((1U << def) * 64U) - 1U;
642 static void __devinit get_mondo_data(struct mdesc_handle *hp, u64 mp,
643 struct trap_per_cpu *tb)
647 val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
648 get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7);
650 val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
651 get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7);
653 val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
654 get_one_mondo_bits(val, &tb->resum_qmask, 6);
656 val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
657 get_one_mondo_bits(val, &tb->nonresum_qmask, 2);
660 void __devinit mdesc_fill_in_cpu_data(cpumask_t mask)
662 struct mdesc_handle *hp = mdesc_grab();
666 mdesc_for_each_node_by_name(hp, mp, "cpu") {
667 const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
668 const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
669 struct trap_per_cpu *tb;
679 if (cpuid >= NR_CPUS)
681 if (!cpu_isset(cpuid, mask))
684 /* On uniprocessor we only want the values for the
685 * real physical cpu the kernel booted onto, however
686 * cpu_data() only has one entry at index 0.
688 if (cpuid != real_hard_smp_processor_id())
693 c = &cpu_data(cpuid);
694 c->clock_tick = *cfreq;
696 tb = &trap_block[cpuid];
697 get_mondo_data(hp, mp, tb);
699 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
700 u64 j, t = mdesc_arc_target(hp, a);
703 t_name = mdesc_node_name(hp, t);
704 if (!strcmp(t_name, "cache")) {
705 fill_in_one_cache(c, hp, t);
709 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
710 u64 n = mdesc_arc_target(hp, j);
713 n_name = mdesc_node_name(hp, n);
714 if (!strcmp(n_name, "cache"))
715 fill_in_one_cache(c, hp, n);
720 cpu_set(cpuid, cpu_present_map);
728 sparc64_multi_core = 1;
734 smp_fill_in_sib_core_maps();
739 void __init sun4v_mdesc_init(void)
741 struct mdesc_handle *hp;
742 unsigned long len, real_len, status;
745 (void) sun4v_mach_desc(0UL, 0UL, &len);
747 printk("MDESC: Size is %lu bytes.\n", len);
749 hp = mdesc_alloc(len, &bootmem_mdesc_memops);
751 prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
755 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
756 if (status != HV_EOK || real_len > len) {
757 prom_printf("sun4v_mach_desc fails, err(%lu), "
758 "len(%lu), real_len(%lu)\n",
759 status, len, real_len);
766 report_platform_properties();
769 mdesc_fill_in_cpu_data(mask);