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>
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_bootmem_alloc(unsigned int mdesc_size
)
89 struct mdesc_handle
*hp
;
90 unsigned int handle_size
, alloc_size
;
92 handle_size
= (sizeof(struct mdesc_handle
) -
93 sizeof(struct mdesc_hdr
) +
95 alloc_size
= PAGE_ALIGN(handle_size
);
97 hp
= __alloc_bootmem(alloc_size
, PAGE_SIZE
, 0UL);
99 mdesc_handle_init(hp
, handle_size
, hp
);
104 static void mdesc_bootmem_free(struct mdesc_handle
*hp
)
106 unsigned int alloc_size
, handle_size
= hp
->handle_size
;
107 unsigned long start
, end
;
109 BUG_ON(atomic_read(&hp
->refcnt
) != 0);
110 BUG_ON(!list_empty(&hp
->list
));
112 alloc_size
= PAGE_ALIGN(handle_size
);
114 start
= (unsigned long) hp
;
115 end
= start
+ alloc_size
;
117 while (start
< end
) {
120 p
= virt_to_page(start
);
121 ClearPageReserved(p
);
127 static struct mdesc_mem_ops bootmem_mdesc_ops
= {
128 .alloc
= mdesc_bootmem_alloc
,
129 .free
= mdesc_bootmem_free
,
132 static struct mdesc_handle
*mdesc_kmalloc(unsigned int mdesc_size
)
134 unsigned int handle_size
;
137 handle_size
= (sizeof(struct mdesc_handle
) -
138 sizeof(struct mdesc_hdr
) +
141 base
= kmalloc(handle_size
+ 15, GFP_KERNEL
| __GFP_NOFAIL
);
143 struct mdesc_handle
*hp
;
146 addr
= (unsigned long)base
;
147 addr
= (addr
+ 15UL) & ~15UL;
148 hp
= (struct mdesc_handle
*) addr
;
150 mdesc_handle_init(hp
, handle_size
, base
);
157 static void mdesc_kfree(struct mdesc_handle
*hp
)
159 BUG_ON(atomic_read(&hp
->refcnt
) != 0);
160 BUG_ON(!list_empty(&hp
->list
));
162 kfree(hp
->self_base
);
165 static struct mdesc_mem_ops kmalloc_mdesc_memops
= {
166 .alloc
= mdesc_kmalloc
,
170 static struct mdesc_handle
*mdesc_alloc(unsigned int mdesc_size
,
171 struct mdesc_mem_ops
*mops
)
173 struct mdesc_handle
*hp
= mops
->alloc(mdesc_size
);
181 static void mdesc_free(struct mdesc_handle
*hp
)
186 static struct mdesc_handle
*cur_mdesc
;
187 static LIST_HEAD(mdesc_zombie_list
);
188 static DEFINE_SPINLOCK(mdesc_lock
);
190 struct mdesc_handle
*mdesc_grab(void)
192 struct mdesc_handle
*hp
;
195 spin_lock_irqsave(&mdesc_lock
, flags
);
198 atomic_inc(&hp
->refcnt
);
199 spin_unlock_irqrestore(&mdesc_lock
, flags
);
203 EXPORT_SYMBOL(mdesc_grab
);
205 void mdesc_release(struct mdesc_handle
*hp
)
209 spin_lock_irqsave(&mdesc_lock
, flags
);
210 if (atomic_dec_and_test(&hp
->refcnt
)) {
211 list_del_init(&hp
->list
);
214 spin_unlock_irqrestore(&mdesc_lock
, flags
);
216 EXPORT_SYMBOL(mdesc_release
);
218 static DEFINE_MUTEX(mdesc_mutex
);
219 static struct mdesc_notifier_client
*client_list
;
221 void mdesc_register_notifier(struct mdesc_notifier_client
*client
)
225 mutex_lock(&mdesc_mutex
);
226 client
->next
= client_list
;
227 client_list
= client
;
229 mdesc_for_each_node_by_name(cur_mdesc
, node
, client
->node_name
)
230 client
->add(cur_mdesc
, node
);
232 mutex_unlock(&mdesc_mutex
);
235 static const u64
*parent_cfg_handle(struct mdesc_handle
*hp
, u64 node
)
241 mdesc_for_each_arc(a
, hp
, node
, MDESC_ARC_TYPE_BACK
) {
244 target
= mdesc_arc_target(hp
, a
);
245 id
= mdesc_get_property(hp
, target
,
254 /* Run 'func' on nodes which are in A but not in B. */
255 static void invoke_on_missing(const char *name
,
256 struct mdesc_handle
*a
,
257 struct mdesc_handle
*b
,
258 void (*func
)(struct mdesc_handle
*, u64
))
262 mdesc_for_each_node_by_name(a
, node
, name
) {
263 int found
= 0, is_vdc_port
= 0;
264 const char *name_prop
;
268 name_prop
= mdesc_get_property(a
, node
, "name", NULL
);
269 if (name_prop
&& !strcmp(name_prop
, "vdc-port")) {
271 id
= parent_cfg_handle(a
, node
);
273 id
= mdesc_get_property(a
, node
, "id", NULL
);
276 printk(KERN_ERR
"MD: Cannot find ID for %s node.\n",
277 (name_prop
? name_prop
: name
));
281 mdesc_for_each_node_by_name(b
, fnode
, name
) {
285 name_prop
= mdesc_get_property(b
, fnode
,
288 strcmp(name_prop
, "vdc-port"))
290 fid
= parent_cfg_handle(b
, fnode
);
292 printk(KERN_ERR
"MD: Cannot find ID "
293 "for vdc-port node.\n");
297 fid
= mdesc_get_property(b
, fnode
,
310 static void notify_one(struct mdesc_notifier_client
*p
,
311 struct mdesc_handle
*old_hp
,
312 struct mdesc_handle
*new_hp
)
314 invoke_on_missing(p
->node_name
, old_hp
, new_hp
, p
->remove
);
315 invoke_on_missing(p
->node_name
, new_hp
, old_hp
, p
->add
);
318 static void mdesc_notify_clients(struct mdesc_handle
*old_hp
,
319 struct mdesc_handle
*new_hp
)
321 struct mdesc_notifier_client
*p
= client_list
;
324 notify_one(p
, old_hp
, new_hp
);
329 void mdesc_update(void)
331 unsigned long len
, real_len
, status
;
332 struct mdesc_handle
*hp
, *orig_hp
;
335 mutex_lock(&mdesc_mutex
);
337 (void) sun4v_mach_desc(0UL, 0UL, &len
);
339 hp
= mdesc_alloc(len
, &kmalloc_mdesc_memops
);
341 printk(KERN_ERR
"MD: mdesc alloc fails\n");
345 status
= sun4v_mach_desc(__pa(&hp
->mdesc
), len
, &real_len
);
346 if (status
!= HV_EOK
|| real_len
> len
) {
347 printk(KERN_ERR
"MD: mdesc reread fails with %lu\n",
349 atomic_dec(&hp
->refcnt
);
354 spin_lock_irqsave(&mdesc_lock
, flags
);
357 spin_unlock_irqrestore(&mdesc_lock
, flags
);
359 mdesc_notify_clients(orig_hp
, hp
);
361 spin_lock_irqsave(&mdesc_lock
, flags
);
362 if (atomic_dec_and_test(&orig_hp
->refcnt
))
365 list_add(&orig_hp
->list
, &mdesc_zombie_list
);
366 spin_unlock_irqrestore(&mdesc_lock
, flags
);
369 mutex_unlock(&mdesc_mutex
);
372 static struct mdesc_elem
*node_block(struct mdesc_hdr
*mdesc
)
374 return (struct mdesc_elem
*) (mdesc
+ 1);
377 static void *name_block(struct mdesc_hdr
*mdesc
)
379 return ((void *) node_block(mdesc
)) + mdesc
->node_sz
;
382 static void *data_block(struct mdesc_hdr
*mdesc
)
384 return ((void *) name_block(mdesc
)) + mdesc
->name_sz
;
387 u64
mdesc_node_by_name(struct mdesc_handle
*hp
,
388 u64 from_node
, const char *name
)
390 struct mdesc_elem
*ep
= node_block(&hp
->mdesc
);
391 const char *names
= name_block(&hp
->mdesc
);
392 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
395 if (from_node
== MDESC_NODE_NULL
) {
397 } else if (from_node
>= last_node
) {
398 return MDESC_NODE_NULL
;
400 ret
= ep
[from_node
].d
.val
;
403 while (ret
< last_node
) {
404 if (ep
[ret
].tag
!= MD_NODE
)
405 return MDESC_NODE_NULL
;
406 if (!strcmp(names
+ ep
[ret
].name_offset
, name
))
410 if (ret
>= last_node
)
411 ret
= MDESC_NODE_NULL
;
414 EXPORT_SYMBOL(mdesc_node_by_name
);
416 const void *mdesc_get_property(struct mdesc_handle
*hp
, u64 node
,
417 const char *name
, int *lenp
)
419 const char *names
= name_block(&hp
->mdesc
);
420 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
421 void *data
= data_block(&hp
->mdesc
);
422 struct mdesc_elem
*ep
;
424 if (node
== MDESC_NODE_NULL
|| node
>= last_node
)
427 ep
= node_block(&hp
->mdesc
) + node
;
429 for (; ep
->tag
!= MD_NODE_END
; ep
++) {
441 val
= data
+ ep
->d
.data
.data_offset
;
442 len
= ep
->d
.data
.data_len
;
451 if (!strcmp(names
+ ep
->name_offset
, name
)) {
460 EXPORT_SYMBOL(mdesc_get_property
);
462 u64
mdesc_next_arc(struct mdesc_handle
*hp
, u64 from
, const char *arc_type
)
464 struct mdesc_elem
*ep
, *base
= node_block(&hp
->mdesc
);
465 const char *names
= name_block(&hp
->mdesc
);
466 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
468 if (from
== MDESC_NODE_NULL
|| from
>= last_node
)
469 return MDESC_NODE_NULL
;
474 for (; ep
->tag
!= MD_NODE_END
; ep
++) {
475 if (ep
->tag
!= MD_PROP_ARC
)
478 if (strcmp(names
+ ep
->name_offset
, arc_type
))
484 return MDESC_NODE_NULL
;
486 EXPORT_SYMBOL(mdesc_next_arc
);
488 u64
mdesc_arc_target(struct mdesc_handle
*hp
, u64 arc
)
490 struct mdesc_elem
*ep
, *base
= node_block(&hp
->mdesc
);
496 EXPORT_SYMBOL(mdesc_arc_target
);
498 const char *mdesc_node_name(struct mdesc_handle
*hp
, u64 node
)
500 struct mdesc_elem
*ep
, *base
= node_block(&hp
->mdesc
);
501 const char *names
= name_block(&hp
->mdesc
);
502 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
504 if (node
== MDESC_NODE_NULL
|| node
>= last_node
)
508 if (ep
->tag
!= MD_NODE
)
511 return names
+ ep
->name_offset
;
513 EXPORT_SYMBOL(mdesc_node_name
);
515 static void __init
report_platform_properties(void)
517 struct mdesc_handle
*hp
= mdesc_grab();
518 u64 pn
= mdesc_node_by_name(hp
, MDESC_NODE_NULL
, "platform");
522 if (pn
== MDESC_NODE_NULL
) {
523 prom_printf("No platform node in machine-description.\n");
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
);
532 v
= mdesc_get_property(hp
, pn
, "hostid", NULL
);
534 printk("PLATFORM: hostid [%08lx]\n", *v
);
535 v
= mdesc_get_property(hp
, pn
, "serial#", NULL
);
537 printk("PLATFORM: serial# [%08lx]\n", *v
);
538 v
= mdesc_get_property(hp
, pn
, "stick-frequency", NULL
);
539 printk("PLATFORM: stick-frequency [%08lx]\n", *v
);
540 v
= mdesc_get_property(hp
, pn
, "mac-address", NULL
);
542 printk("PLATFORM: mac-address [%lx]\n", *v
);
543 v
= mdesc_get_property(hp
, pn
, "watchdog-resolution", NULL
);
545 printk("PLATFORM: watchdog-resolution [%lu ms]\n", *v
);
546 v
= mdesc_get_property(hp
, pn
, "watchdog-max-timeout", NULL
);
548 printk("PLATFORM: watchdog-max-timeout [%lu ms]\n", *v
);
549 v
= mdesc_get_property(hp
, pn
, "max-cpus", NULL
);
551 printk("PLATFORM: max-cpus [%lu]\n", *v
);
559 if (max_cpu
> NR_CPUS
)
564 for (i
= 0; i
< max_cpu
; i
++)
565 cpu_set(i
, cpu_possible_map
);
572 static void __devinit
fill_in_one_cache(cpuinfo_sparc
*c
,
573 struct mdesc_handle
*hp
,
576 const u64
*level
= mdesc_get_property(hp
, mp
, "level", NULL
);
577 const u64
*size
= mdesc_get_property(hp
, mp
, "size", NULL
);
578 const u64
*line_size
= mdesc_get_property(hp
, mp
, "line-size", NULL
);
582 type
= mdesc_get_property(hp
, mp
, "type", &type_len
);
586 if (of_find_in_proplist(type
, "instn", type_len
)) {
587 c
->icache_size
= *size
;
588 c
->icache_line_size
= *line_size
;
589 } else if (of_find_in_proplist(type
, "data", type_len
)) {
590 c
->dcache_size
= *size
;
591 c
->dcache_line_size
= *line_size
;
596 c
->ecache_size
= *size
;
597 c
->ecache_line_size
= *line_size
;
607 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_FWD
) {
608 u64 target
= mdesc_arc_target(hp
, a
);
609 const char *name
= mdesc_node_name(hp
, target
);
611 if (!strcmp(name
, "cache"))
612 fill_in_one_cache(c
, hp
, target
);
617 static void __devinit
mark_core_ids(struct mdesc_handle
*hp
, u64 mp
,
622 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_BACK
) {
623 u64 t
= mdesc_arc_target(hp
, a
);
627 name
= mdesc_node_name(hp
, t
);
628 if (!strcmp(name
, "cpu")) {
629 id
= mdesc_get_property(hp
, t
, "id", NULL
);
631 cpu_data(*id
).core_id
= core_id
;
635 mdesc_for_each_arc(j
, hp
, t
, MDESC_ARC_TYPE_BACK
) {
636 u64 n
= mdesc_arc_target(hp
, j
);
639 n_name
= mdesc_node_name(hp
, n
);
640 if (strcmp(n_name
, "cpu"))
643 id
= mdesc_get_property(hp
, n
, "id", NULL
);
645 cpu_data(*id
).core_id
= core_id
;
651 static void __devinit
set_core_ids(struct mdesc_handle
*hp
)
657 mdesc_for_each_node_by_name(hp
, mp
, "cache") {
662 level
= mdesc_get_property(hp
, mp
, "level", NULL
);
666 type
= mdesc_get_property(hp
, mp
, "type", &len
);
667 if (!of_find_in_proplist(type
, "instn", len
))
670 mark_core_ids(hp
, mp
, idx
);
676 static void __devinit
mark_proc_ids(struct mdesc_handle
*hp
, u64 mp
,
681 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_BACK
) {
682 u64 t
= mdesc_arc_target(hp
, a
);
686 name
= mdesc_node_name(hp
, t
);
687 if (strcmp(name
, "cpu"))
690 id
= mdesc_get_property(hp
, t
, "id", NULL
);
692 cpu_data(*id
).proc_id
= proc_id
;
696 static void __devinit
__set_proc_ids(struct mdesc_handle
*hp
,
697 const char *exec_unit_name
)
703 mdesc_for_each_node_by_name(hp
, mp
, exec_unit_name
) {
707 type
= mdesc_get_property(hp
, mp
, "type", &len
);
708 if (!of_find_in_proplist(type
, "int", len
) &&
709 !of_find_in_proplist(type
, "integer", len
))
712 mark_proc_ids(hp
, mp
, idx
);
718 static void __devinit
set_proc_ids(struct mdesc_handle
*hp
)
720 __set_proc_ids(hp
, "exec_unit");
721 __set_proc_ids(hp
, "exec-unit");
724 static void __devinit
get_one_mondo_bits(const u64
*p
, unsigned int *mask
,
733 if (!val
|| val
>= 64)
736 *mask
= ((1U << val
) * 64U) - 1U;
740 *mask
= ((1U << def
) * 64U) - 1U;
743 static void __devinit
get_mondo_data(struct mdesc_handle
*hp
, u64 mp
,
744 struct trap_per_cpu
*tb
)
748 val
= mdesc_get_property(hp
, mp
, "q-cpu-mondo-#bits", NULL
);
749 get_one_mondo_bits(val
, &tb
->cpu_mondo_qmask
, 7);
751 val
= mdesc_get_property(hp
, mp
, "q-dev-mondo-#bits", NULL
);
752 get_one_mondo_bits(val
, &tb
->dev_mondo_qmask
, 7);
754 val
= mdesc_get_property(hp
, mp
, "q-resumable-#bits", NULL
);
755 get_one_mondo_bits(val
, &tb
->resum_qmask
, 6);
757 val
= mdesc_get_property(hp
, mp
, "q-nonresumable-#bits", NULL
);
758 get_one_mondo_bits(val
, &tb
->nonresum_qmask
, 2);
761 void __devinit
mdesc_fill_in_cpu_data(cpumask_t mask
)
763 struct mdesc_handle
*hp
= mdesc_grab();
767 mdesc_for_each_node_by_name(hp
, mp
, "cpu") {
768 const u64
*id
= mdesc_get_property(hp
, mp
, "id", NULL
);
769 const u64
*cfreq
= mdesc_get_property(hp
, mp
, "clock-frequency", NULL
);
770 struct trap_per_cpu
*tb
;
780 if (cpuid
>= NR_CPUS
) {
781 printk(KERN_WARNING
"Ignoring CPU %d which is "
786 if (!cpu_isset(cpuid
, mask
))
789 /* On uniprocessor we only want the values for the
790 * real physical cpu the kernel booted onto, however
791 * cpu_data() only has one entry at index 0.
793 if (cpuid
!= real_hard_smp_processor_id())
798 c
= &cpu_data(cpuid
);
799 c
->clock_tick
= *cfreq
;
801 tb
= &trap_block
[cpuid
];
802 get_mondo_data(hp
, mp
, tb
);
804 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_FWD
) {
805 u64 j
, t
= mdesc_arc_target(hp
, a
);
808 t_name
= mdesc_node_name(hp
, t
);
809 if (!strcmp(t_name
, "cache")) {
810 fill_in_one_cache(c
, hp
, t
);
814 mdesc_for_each_arc(j
, hp
, t
, MDESC_ARC_TYPE_FWD
) {
815 u64 n
= mdesc_arc_target(hp
, j
);
818 n_name
= mdesc_node_name(hp
, n
);
819 if (!strcmp(n_name
, "cache"))
820 fill_in_one_cache(c
, hp
, n
);
825 cpu_set(cpuid
, cpu_present_map
);
833 sparc64_multi_core
= 1;
839 smp_fill_in_sib_core_maps();
844 static ssize_t
mdesc_read(struct file
*file
, char __user
*buf
,
845 size_t len
, loff_t
*offp
)
847 struct mdesc_handle
*hp
= mdesc_grab();
853 err
= hp
->handle_size
;
854 if (len
< hp
->handle_size
)
856 else if (copy_to_user(buf
, &hp
->mdesc
, hp
->handle_size
))
863 static const struct file_operations mdesc_fops
= {
865 .owner
= THIS_MODULE
,
868 static struct miscdevice mdesc_misc
= {
869 .minor
= MISC_DYNAMIC_MINOR
,
874 static int __init
mdesc_misc_init(void)
876 return misc_register(&mdesc_misc
);
879 __initcall(mdesc_misc_init
);
881 void __init
sun4v_mdesc_init(void)
883 struct mdesc_handle
*hp
;
884 unsigned long len
, real_len
, status
;
887 (void) sun4v_mach_desc(0UL, 0UL, &len
);
889 printk("MDESC: Size is %lu bytes.\n", len
);
891 hp
= mdesc_alloc(len
, &bootmem_mdesc_ops
);
893 prom_printf("MDESC: alloc of %lu bytes failed.\n", len
);
897 status
= sun4v_mach_desc(__pa(&hp
->mdesc
), len
, &real_len
);
898 if (status
!= HV_EOK
|| real_len
> len
) {
899 prom_printf("sun4v_mach_desc fails, err(%lu), "
900 "len(%lu), real_len(%lu)\n",
901 status
, len
, real_len
);
908 report_platform_properties();
911 mdesc_fill_in_cpu_data(mask
);