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>
7 #include <linux/memblock.h>
8 #include <linux/log2.h>
9 #include <linux/list.h>
10 #include <linux/slab.h>
12 #include <linux/miscdevice.h>
13 #include <linux/bootmem.h>
14 #include <linux/export.h>
16 #include <asm/cpudata.h>
17 #include <asm/hypervisor.h>
18 #include <asm/mdesc.h>
20 #include <linux/uaccess.h>
21 #include <asm/oplib.h>
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.
29 * Actually, it isn't even a DAG, because there are back pointers
30 * which create cycles in the graph.
32 * mdesc_hdr and mdesc_elem describe the layout of the data structure
33 * we get from the Hypervisor.
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)));
44 #define MD_LIST_END 0x00
46 #define MD_NODE_END 0x45
48 #define MD_PROP_ARC 0x61
49 #define MD_PROP_VAL 0x76
50 #define MD_PROP_STR 0x73
51 #define MD_PROP_DATA 0x64
64 struct mdesc_mem_ops
{
65 struct mdesc_handle
*(*alloc
)(unsigned int mdesc_size
);
66 void (*free
)(struct mdesc_handle
*handle
);
70 struct list_head list
;
71 struct mdesc_mem_ops
*mops
;
74 unsigned int handle_size
;
75 struct mdesc_hdr mdesc
;
78 typedef int (*mdesc_node_info_get_f
)(struct mdesc_handle
*, u64
,
79 union md_node_info
*);
80 typedef void (*mdesc_node_info_rel_f
)(union md_node_info
*);
81 typedef bool (*mdesc_node_match_f
)(union md_node_info
*, union md_node_info
*);
85 mdesc_node_info_get_f get_info
;
86 mdesc_node_info_rel_f rel_info
;
87 mdesc_node_match_f node_match
;
90 static int get_vdev_port_node_info(struct mdesc_handle
*md
, u64 node
,
91 union md_node_info
*node_info
);
92 static void rel_vdev_port_node_info(union md_node_info
*node_info
);
93 static bool vdev_port_node_match(union md_node_info
*a_node_info
,
94 union md_node_info
*b_node_info
);
96 static int get_ds_port_node_info(struct mdesc_handle
*md
, u64 node
,
97 union md_node_info
*node_info
);
98 static void rel_ds_port_node_info(union md_node_info
*node_info
);
99 static bool ds_port_node_match(union md_node_info
*a_node_info
,
100 union md_node_info
*b_node_info
);
102 /* supported node types which can be registered */
103 static struct md_node_ops md_node_ops_table
[] = {
104 {"virtual-device-port", get_vdev_port_node_info
,
105 rel_vdev_port_node_info
, vdev_port_node_match
},
106 {"domain-services-port", get_ds_port_node_info
,
107 rel_ds_port_node_info
, ds_port_node_match
},
108 {NULL
, NULL
, NULL
, NULL
}
111 static void mdesc_get_node_ops(const char *node_name
,
112 mdesc_node_info_get_f
*get_info_f
,
113 mdesc_node_info_rel_f
*rel_info_f
,
114 mdesc_node_match_f
*match_f
)
130 for (i
= 0; md_node_ops_table
[i
].name
!= NULL
; i
++) {
131 if (strcmp(md_node_ops_table
[i
].name
, node_name
) == 0) {
133 *get_info_f
= md_node_ops_table
[i
].get_info
;
136 *rel_info_f
= md_node_ops_table
[i
].rel_info
;
139 *match_f
= md_node_ops_table
[i
].node_match
;
146 static void mdesc_handle_init(struct mdesc_handle
*hp
,
147 unsigned int handle_size
,
150 BUG_ON(((unsigned long)&hp
->mdesc
) & (16UL - 1));
152 memset(hp
, 0, handle_size
);
153 INIT_LIST_HEAD(&hp
->list
);
154 hp
->self_base
= base
;
155 atomic_set(&hp
->refcnt
, 1);
156 hp
->handle_size
= handle_size
;
159 static struct mdesc_handle
* __init
mdesc_memblock_alloc(unsigned int mdesc_size
)
161 unsigned int handle_size
, alloc_size
;
162 struct mdesc_handle
*hp
;
165 handle_size
= (sizeof(struct mdesc_handle
) -
166 sizeof(struct mdesc_hdr
) +
168 alloc_size
= PAGE_ALIGN(handle_size
);
170 paddr
= memblock_alloc(alloc_size
, PAGE_SIZE
);
175 mdesc_handle_init(hp
, handle_size
, hp
);
180 static void __init
mdesc_memblock_free(struct mdesc_handle
*hp
)
182 unsigned int alloc_size
;
185 BUG_ON(atomic_read(&hp
->refcnt
) != 0);
186 BUG_ON(!list_empty(&hp
->list
));
188 alloc_size
= PAGE_ALIGN(hp
->handle_size
);
190 free_bootmem_late(start
, alloc_size
);
193 static struct mdesc_mem_ops memblock_mdesc_ops
= {
194 .alloc
= mdesc_memblock_alloc
,
195 .free
= mdesc_memblock_free
,
198 static struct mdesc_handle
*mdesc_kmalloc(unsigned int mdesc_size
)
200 unsigned int handle_size
;
201 struct mdesc_handle
*hp
;
205 handle_size
= (sizeof(struct mdesc_handle
) -
206 sizeof(struct mdesc_hdr
) +
208 base
= kmalloc(handle_size
+ 15, GFP_KERNEL
| __GFP_RETRY_MAYFAIL
);
212 addr
= (unsigned long)base
;
213 addr
= (addr
+ 15UL) & ~15UL;
214 hp
= (struct mdesc_handle
*) addr
;
216 mdesc_handle_init(hp
, handle_size
, base
);
221 static void mdesc_kfree(struct mdesc_handle
*hp
)
223 BUG_ON(atomic_read(&hp
->refcnt
) != 0);
224 BUG_ON(!list_empty(&hp
->list
));
226 kfree(hp
->self_base
);
229 static struct mdesc_mem_ops kmalloc_mdesc_memops
= {
230 .alloc
= mdesc_kmalloc
,
234 static struct mdesc_handle
*mdesc_alloc(unsigned int mdesc_size
,
235 struct mdesc_mem_ops
*mops
)
237 struct mdesc_handle
*hp
= mops
->alloc(mdesc_size
);
245 static void mdesc_free(struct mdesc_handle
*hp
)
250 static struct mdesc_handle
*cur_mdesc
;
251 static LIST_HEAD(mdesc_zombie_list
);
252 static DEFINE_SPINLOCK(mdesc_lock
);
254 struct mdesc_handle
*mdesc_grab(void)
256 struct mdesc_handle
*hp
;
259 spin_lock_irqsave(&mdesc_lock
, flags
);
262 atomic_inc(&hp
->refcnt
);
263 spin_unlock_irqrestore(&mdesc_lock
, flags
);
267 EXPORT_SYMBOL(mdesc_grab
);
269 void mdesc_release(struct mdesc_handle
*hp
)
273 spin_lock_irqsave(&mdesc_lock
, flags
);
274 if (atomic_dec_and_test(&hp
->refcnt
)) {
275 list_del_init(&hp
->list
);
278 spin_unlock_irqrestore(&mdesc_lock
, flags
);
280 EXPORT_SYMBOL(mdesc_release
);
282 static DEFINE_MUTEX(mdesc_mutex
);
283 static struct mdesc_notifier_client
*client_list
;
285 void mdesc_register_notifier(struct mdesc_notifier_client
*client
)
287 bool supported
= false;
291 mutex_lock(&mdesc_mutex
);
293 /* check to see if the node is supported for registration */
294 for (i
= 0; md_node_ops_table
[i
].name
!= NULL
; i
++) {
295 if (strcmp(md_node_ops_table
[i
].name
, client
->node_name
) == 0) {
302 pr_err("MD: %s node not supported\n", client
->node_name
);
303 mutex_unlock(&mdesc_mutex
);
307 client
->next
= client_list
;
308 client_list
= client
;
310 mdesc_for_each_node_by_name(cur_mdesc
, node
, client
->node_name
)
311 client
->add(cur_mdesc
, node
, client
->node_name
);
313 mutex_unlock(&mdesc_mutex
);
316 static const u64
*parent_cfg_handle(struct mdesc_handle
*hp
, u64 node
)
322 mdesc_for_each_arc(a
, hp
, node
, MDESC_ARC_TYPE_BACK
) {
325 target
= mdesc_arc_target(hp
, a
);
326 id
= mdesc_get_property(hp
, target
,
335 static int get_vdev_port_node_info(struct mdesc_handle
*md
, u64 node
,
336 union md_node_info
*node_info
)
338 const u64
*parent_cfg_hdlp
;
343 * Virtual device nodes are distinguished by:
346 * 3. parent node "cfg-handle" property
348 idp
= mdesc_get_property(md
, node
, "id", NULL
);
349 name
= mdesc_get_property(md
, node
, "name", NULL
);
350 parent_cfg_hdlp
= parent_cfg_handle(md
, node
);
352 if (!idp
|| !name
|| !parent_cfg_hdlp
)
355 node_info
->vdev_port
.id
= *idp
;
356 node_info
->vdev_port
.name
= kstrdup_const(name
, GFP_KERNEL
);
357 node_info
->vdev_port
.parent_cfg_hdl
= *parent_cfg_hdlp
;
362 static void rel_vdev_port_node_info(union md_node_info
*node_info
)
364 if (node_info
&& node_info
->vdev_port
.name
) {
365 kfree_const(node_info
->vdev_port
.name
);
366 node_info
->vdev_port
.name
= NULL
;
370 static bool vdev_port_node_match(union md_node_info
*a_node_info
,
371 union md_node_info
*b_node_info
)
373 if (a_node_info
->vdev_port
.id
!= b_node_info
->vdev_port
.id
)
376 if (a_node_info
->vdev_port
.parent_cfg_hdl
!=
377 b_node_info
->vdev_port
.parent_cfg_hdl
)
380 if (strncmp(a_node_info
->vdev_port
.name
,
381 b_node_info
->vdev_port
.name
, MDESC_MAX_STR_LEN
) != 0)
387 static int get_ds_port_node_info(struct mdesc_handle
*md
, u64 node
,
388 union md_node_info
*node_info
)
392 /* DS port nodes use the "id" property to distinguish them */
393 idp
= mdesc_get_property(md
, node
, "id", NULL
);
397 node_info
->ds_port
.id
= *idp
;
402 static void rel_ds_port_node_info(union md_node_info
*node_info
)
406 static bool ds_port_node_match(union md_node_info
*a_node_info
,
407 union md_node_info
*b_node_info
)
409 if (a_node_info
->ds_port
.id
!= b_node_info
->ds_port
.id
)
415 /* Run 'func' on nodes which are in A but not in B. */
416 static void invoke_on_missing(const char *name
,
417 struct mdesc_handle
*a
,
418 struct mdesc_handle
*b
,
419 void (*func
)(struct mdesc_handle
*, u64
,
420 const char *node_name
))
422 mdesc_node_info_get_f get_info_func
;
423 mdesc_node_info_rel_f rel_info_func
;
424 mdesc_node_match_f node_match_func
;
425 union md_node_info a_node_info
;
426 union md_node_info b_node_info
;
433 * Find the get_info, rel_info and node_match ops for the given
436 mdesc_get_node_ops(name
, &get_info_func
, &rel_info_func
,
439 /* If we didn't find a match, the node type is not supported */
440 if (!get_info_func
|| !rel_info_func
|| !node_match_func
) {
441 pr_err("MD: %s node type is not supported\n", name
);
445 mdesc_for_each_node_by_name(a
, a_node
, name
) {
448 rv
= get_info_func(a
, a_node
, &a_node_info
);
450 pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
455 /* Check each node in B for node matching a_node */
456 mdesc_for_each_node_by_name(b
, b_node
, name
) {
457 rv
= get_info_func(b
, b_node
, &b_node_info
);
461 if (node_match_func(&a_node_info
, &b_node_info
)) {
463 rel_info_func(&b_node_info
);
467 rel_info_func(&b_node_info
);
470 rel_info_func(&a_node_info
);
473 func(a
, a_node
, name
);
477 static void notify_one(struct mdesc_notifier_client
*p
,
478 struct mdesc_handle
*old_hp
,
479 struct mdesc_handle
*new_hp
)
481 invoke_on_missing(p
->node_name
, old_hp
, new_hp
, p
->remove
);
482 invoke_on_missing(p
->node_name
, new_hp
, old_hp
, p
->add
);
485 static void mdesc_notify_clients(struct mdesc_handle
*old_hp
,
486 struct mdesc_handle
*new_hp
)
488 struct mdesc_notifier_client
*p
= client_list
;
491 notify_one(p
, old_hp
, new_hp
);
496 void mdesc_update(void)
498 unsigned long len
, real_len
, status
;
499 struct mdesc_handle
*hp
, *orig_hp
;
502 mutex_lock(&mdesc_mutex
);
504 (void) sun4v_mach_desc(0UL, 0UL, &len
);
506 hp
= mdesc_alloc(len
, &kmalloc_mdesc_memops
);
508 printk(KERN_ERR
"MD: mdesc alloc fails\n");
512 status
= sun4v_mach_desc(__pa(&hp
->mdesc
), len
, &real_len
);
513 if (status
!= HV_EOK
|| real_len
> len
) {
514 printk(KERN_ERR
"MD: mdesc reread fails with %lu\n",
516 atomic_dec(&hp
->refcnt
);
521 spin_lock_irqsave(&mdesc_lock
, flags
);
524 spin_unlock_irqrestore(&mdesc_lock
, flags
);
526 mdesc_notify_clients(orig_hp
, hp
);
528 spin_lock_irqsave(&mdesc_lock
, flags
);
529 if (atomic_dec_and_test(&orig_hp
->refcnt
))
532 list_add(&orig_hp
->list
, &mdesc_zombie_list
);
533 spin_unlock_irqrestore(&mdesc_lock
, flags
);
536 mutex_unlock(&mdesc_mutex
);
539 u64
mdesc_get_node(struct mdesc_handle
*hp
, const char *node_name
,
540 union md_node_info
*node_info
)
542 mdesc_node_info_get_f get_info_func
;
543 mdesc_node_info_rel_f rel_info_func
;
544 mdesc_node_match_f node_match_func
;
545 union md_node_info hp_node_info
;
549 if (hp
== NULL
|| node_name
== NULL
|| node_info
== NULL
)
550 return MDESC_NODE_NULL
;
552 /* Find the ops for the given node name */
553 mdesc_get_node_ops(node_name
, &get_info_func
, &rel_info_func
,
556 /* If we didn't find ops for the given node name, it is not supported */
557 if (!get_info_func
|| !rel_info_func
|| !node_match_func
) {
558 pr_err("MD: %s node is not supported\n", node_name
);
562 mdesc_for_each_node_by_name(hp
, hp_node
, node_name
) {
563 rv
= get_info_func(hp
, hp_node
, &hp_node_info
);
567 if (node_match_func(node_info
, &hp_node_info
))
570 rel_info_func(&hp_node_info
);
573 rel_info_func(&hp_node_info
);
577 EXPORT_SYMBOL(mdesc_get_node
);
579 int mdesc_get_node_info(struct mdesc_handle
*hp
, u64 node
,
580 const char *node_name
, union md_node_info
*node_info
)
582 mdesc_node_info_get_f get_info_func
;
585 if (hp
== NULL
|| node
== MDESC_NODE_NULL
||
586 node_name
== NULL
|| node_info
== NULL
)
589 /* Find the get_info op for the given node name */
590 mdesc_get_node_ops(node_name
, &get_info_func
, NULL
, NULL
);
592 /* If we didn't find a get_info_func, the node name is not supported */
593 if (get_info_func
== NULL
) {
594 pr_err("MD: %s node is not supported\n", node_name
);
598 rv
= get_info_func(hp
, node
, node_info
);
600 pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
607 EXPORT_SYMBOL(mdesc_get_node_info
);
609 static struct mdesc_elem
*node_block(struct mdesc_hdr
*mdesc
)
611 return (struct mdesc_elem
*) (mdesc
+ 1);
614 static void *name_block(struct mdesc_hdr
*mdesc
)
616 return ((void *) node_block(mdesc
)) + mdesc
->node_sz
;
619 static void *data_block(struct mdesc_hdr
*mdesc
)
621 return ((void *) name_block(mdesc
)) + mdesc
->name_sz
;
624 u64
mdesc_node_by_name(struct mdesc_handle
*hp
,
625 u64 from_node
, const char *name
)
627 struct mdesc_elem
*ep
= node_block(&hp
->mdesc
);
628 const char *names
= name_block(&hp
->mdesc
);
629 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
632 if (from_node
== MDESC_NODE_NULL
) {
634 } else if (from_node
>= last_node
) {
635 return MDESC_NODE_NULL
;
637 ret
= ep
[from_node
].d
.val
;
640 while (ret
< last_node
) {
641 if (ep
[ret
].tag
!= MD_NODE
)
642 return MDESC_NODE_NULL
;
643 if (!strcmp(names
+ ep
[ret
].name_offset
, name
))
647 if (ret
>= last_node
)
648 ret
= MDESC_NODE_NULL
;
651 EXPORT_SYMBOL(mdesc_node_by_name
);
653 const void *mdesc_get_property(struct mdesc_handle
*hp
, u64 node
,
654 const char *name
, int *lenp
)
656 const char *names
= name_block(&hp
->mdesc
);
657 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
658 void *data
= data_block(&hp
->mdesc
);
659 struct mdesc_elem
*ep
;
661 if (node
== MDESC_NODE_NULL
|| node
>= last_node
)
664 ep
= node_block(&hp
->mdesc
) + node
;
666 for (; ep
->tag
!= MD_NODE_END
; ep
++) {
678 val
= data
+ ep
->d
.data
.data_offset
;
679 len
= ep
->d
.data
.data_len
;
688 if (!strcmp(names
+ ep
->name_offset
, name
)) {
697 EXPORT_SYMBOL(mdesc_get_property
);
699 u64
mdesc_next_arc(struct mdesc_handle
*hp
, u64 from
, const char *arc_type
)
701 struct mdesc_elem
*ep
, *base
= node_block(&hp
->mdesc
);
702 const char *names
= name_block(&hp
->mdesc
);
703 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
705 if (from
== MDESC_NODE_NULL
|| from
>= last_node
)
706 return MDESC_NODE_NULL
;
711 for (; ep
->tag
!= MD_NODE_END
; ep
++) {
712 if (ep
->tag
!= MD_PROP_ARC
)
715 if (strcmp(names
+ ep
->name_offset
, arc_type
))
721 return MDESC_NODE_NULL
;
723 EXPORT_SYMBOL(mdesc_next_arc
);
725 u64
mdesc_arc_target(struct mdesc_handle
*hp
, u64 arc
)
727 struct mdesc_elem
*ep
, *base
= node_block(&hp
->mdesc
);
733 EXPORT_SYMBOL(mdesc_arc_target
);
735 const char *mdesc_node_name(struct mdesc_handle
*hp
, u64 node
)
737 struct mdesc_elem
*ep
, *base
= node_block(&hp
->mdesc
);
738 const char *names
= name_block(&hp
->mdesc
);
739 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
741 if (node
== MDESC_NODE_NULL
|| node
>= last_node
)
745 if (ep
->tag
!= MD_NODE
)
748 return names
+ ep
->name_offset
;
750 EXPORT_SYMBOL(mdesc_node_name
);
752 static u64 max_cpus
= 64;
754 static void __init
report_platform_properties(void)
756 struct mdesc_handle
*hp
= mdesc_grab();
757 u64 pn
= mdesc_node_by_name(hp
, MDESC_NODE_NULL
, "platform");
761 if (pn
== MDESC_NODE_NULL
) {
762 prom_printf("No platform node in machine-description.\n");
766 s
= mdesc_get_property(hp
, pn
, "banner-name", NULL
);
767 printk("PLATFORM: banner-name [%s]\n", s
);
768 s
= mdesc_get_property(hp
, pn
, "name", NULL
);
769 printk("PLATFORM: name [%s]\n", s
);
771 v
= mdesc_get_property(hp
, pn
, "hostid", NULL
);
773 printk("PLATFORM: hostid [%08llx]\n", *v
);
774 v
= mdesc_get_property(hp
, pn
, "serial#", NULL
);
776 printk("PLATFORM: serial# [%08llx]\n", *v
);
777 v
= mdesc_get_property(hp
, pn
, "stick-frequency", NULL
);
778 printk("PLATFORM: stick-frequency [%08llx]\n", *v
);
779 v
= mdesc_get_property(hp
, pn
, "mac-address", NULL
);
781 printk("PLATFORM: mac-address [%llx]\n", *v
);
782 v
= mdesc_get_property(hp
, pn
, "watchdog-resolution", NULL
);
784 printk("PLATFORM: watchdog-resolution [%llu ms]\n", *v
);
785 v
= mdesc_get_property(hp
, pn
, "watchdog-max-timeout", NULL
);
787 printk("PLATFORM: watchdog-max-timeout [%llu ms]\n", *v
);
788 v
= mdesc_get_property(hp
, pn
, "max-cpus", NULL
);
791 printk("PLATFORM: max-cpus [%llu]\n", max_cpus
);
800 if (max_cpu
> NR_CPUS
)
805 for (i
= 0; i
< max_cpu
; i
++)
806 set_cpu_possible(i
, true);
813 static void fill_in_one_cache(cpuinfo_sparc
*c
, struct mdesc_handle
*hp
, u64 mp
)
815 const u64
*level
= mdesc_get_property(hp
, mp
, "level", NULL
);
816 const u64
*size
= mdesc_get_property(hp
, mp
, "size", NULL
);
817 const u64
*line_size
= mdesc_get_property(hp
, mp
, "line-size", NULL
);
821 type
= mdesc_get_property(hp
, mp
, "type", &type_len
);
825 if (of_find_in_proplist(type
, "instn", type_len
)) {
826 c
->icache_size
= *size
;
827 c
->icache_line_size
= *line_size
;
828 } else if (of_find_in_proplist(type
, "data", type_len
)) {
829 c
->dcache_size
= *size
;
830 c
->dcache_line_size
= *line_size
;
835 c
->ecache_size
= *size
;
836 c
->ecache_line_size
= *line_size
;
846 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_FWD
) {
847 u64 target
= mdesc_arc_target(hp
, a
);
848 const char *name
= mdesc_node_name(hp
, target
);
850 if (!strcmp(name
, "cache"))
851 fill_in_one_cache(c
, hp
, target
);
856 static void find_back_node_value(struct mdesc_handle
*hp
, u64 node
,
858 void (*func
)(struct mdesc_handle
*, u64
, int),
863 /* Since we have an estimate of recursion depth, do a sanity check. */
867 mdesc_for_each_arc(arc
, hp
, node
, MDESC_ARC_TYPE_BACK
) {
868 u64 n
= mdesc_arc_target(hp
, arc
);
869 const char *name
= mdesc_node_name(hp
, n
);
871 if (!strcmp(srch_val
, name
))
874 find_back_node_value(hp
, n
, srch_val
, func
, val
, depth
-1);
878 static void __mark_core_id(struct mdesc_handle
*hp
, u64 node
,
881 const u64
*id
= mdesc_get_property(hp
, node
, "id", NULL
);
883 if (*id
< num_possible_cpus())
884 cpu_data(*id
).core_id
= core_id
;
887 static void __mark_max_cache_id(struct mdesc_handle
*hp
, u64 node
,
890 const u64
*id
= mdesc_get_property(hp
, node
, "id", NULL
);
892 if (*id
< num_possible_cpus()) {
893 cpu_data(*id
).max_cache_id
= max_cache_id
;
896 * On systems without explicit socket descriptions socket
899 cpu_data(*id
).sock_id
= max_cache_id
;
903 static void mark_core_ids(struct mdesc_handle
*hp
, u64 mp
,
906 find_back_node_value(hp
, mp
, "cpu", __mark_core_id
, core_id
, 10);
909 static void mark_max_cache_ids(struct mdesc_handle
*hp
, u64 mp
,
912 find_back_node_value(hp
, mp
, "cpu", __mark_max_cache_id
,
916 static void set_core_ids(struct mdesc_handle
*hp
)
923 /* Identify unique cores by looking for cpus backpointed to by
924 * level 1 instruction caches.
926 mdesc_for_each_node_by_name(hp
, mp
, "cache") {
931 level
= mdesc_get_property(hp
, mp
, "level", NULL
);
935 type
= mdesc_get_property(hp
, mp
, "type", &len
);
936 if (!of_find_in_proplist(type
, "instn", len
))
939 mark_core_ids(hp
, mp
, idx
);
944 static int set_max_cache_ids_by_cache(struct mdesc_handle
*hp
, int level
)
951 * Identify unique highest level of shared cache by looking for cpus
952 * backpointed to by shared level N caches.
954 mdesc_for_each_node_by_name(hp
, mp
, "cache") {
957 cur_lvl
= mdesc_get_property(hp
, mp
, "level", NULL
);
958 if (*cur_lvl
!= level
)
960 mark_max_cache_ids(hp
, mp
, idx
);
967 static void set_sock_ids_by_socket(struct mdesc_handle
*hp
, u64 mp
)
971 mdesc_for_each_node_by_name(hp
, mp
, "socket") {
974 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_FWD
) {
975 u64 t
= mdesc_arc_target(hp
, a
);
979 name
= mdesc_node_name(hp
, t
);
980 if (strcmp(name
, "cpu"))
983 id
= mdesc_get_property(hp
, t
, "id", NULL
);
984 if (*id
< num_possible_cpus())
985 cpu_data(*id
).sock_id
= idx
;
991 static void set_sock_ids(struct mdesc_handle
*hp
)
996 * Find the highest level of shared cache which pre-T7 is also
999 if (!set_max_cache_ids_by_cache(hp
, 3))
1000 set_max_cache_ids_by_cache(hp
, 2);
1002 /* If machine description exposes sockets data use it.*/
1003 mp
= mdesc_node_by_name(hp
, MDESC_NODE_NULL
, "sockets");
1004 if (mp
!= MDESC_NODE_NULL
)
1005 set_sock_ids_by_socket(hp
, mp
);
1008 static void mark_proc_ids(struct mdesc_handle
*hp
, u64 mp
, int proc_id
)
1012 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_BACK
) {
1013 u64 t
= mdesc_arc_target(hp
, a
);
1017 name
= mdesc_node_name(hp
, t
);
1018 if (strcmp(name
, "cpu"))
1021 id
= mdesc_get_property(hp
, t
, "id", NULL
);
1023 cpu_data(*id
).proc_id
= proc_id
;
1027 static void __set_proc_ids(struct mdesc_handle
*hp
, const char *exec_unit_name
)
1033 mdesc_for_each_node_by_name(hp
, mp
, exec_unit_name
) {
1037 type
= mdesc_get_property(hp
, mp
, "type", &len
);
1038 if (!of_find_in_proplist(type
, "int", len
) &&
1039 !of_find_in_proplist(type
, "integer", len
))
1042 mark_proc_ids(hp
, mp
, idx
);
1047 static void set_proc_ids(struct mdesc_handle
*hp
)
1049 __set_proc_ids(hp
, "exec_unit");
1050 __set_proc_ids(hp
, "exec-unit");
1053 static void get_one_mondo_bits(const u64
*p
, unsigned int *mask
,
1054 unsigned long def
, unsigned long max
)
1062 if (!val
|| val
>= 64)
1068 *mask
= ((1U << val
) * 64U) - 1U;
1072 *mask
= ((1U << def
) * 64U) - 1U;
1075 static void get_mondo_data(struct mdesc_handle
*hp
, u64 mp
,
1076 struct trap_per_cpu
*tb
)
1081 val
= mdesc_get_property(hp
, mp
, "q-cpu-mondo-#bits", NULL
);
1082 get_one_mondo_bits(val
, &tb
->cpu_mondo_qmask
, 7, ilog2(max_cpus
* 2));
1084 val
= mdesc_get_property(hp
, mp
, "q-dev-mondo-#bits", NULL
);
1085 get_one_mondo_bits(val
, &tb
->dev_mondo_qmask
, 7, 8);
1087 val
= mdesc_get_property(hp
, mp
, "q-resumable-#bits", NULL
);
1088 get_one_mondo_bits(val
, &tb
->resum_qmask
, 6, 7);
1090 val
= mdesc_get_property(hp
, mp
, "q-nonresumable-#bits", NULL
);
1091 get_one_mondo_bits(val
, &tb
->nonresum_qmask
, 2, 2);
1093 pr_info("SUN4V: Mondo queue sizes "
1094 "[cpu(%u) dev(%u) r(%u) nr(%u)]\n",
1095 tb
->cpu_mondo_qmask
+ 1,
1096 tb
->dev_mondo_qmask
+ 1,
1097 tb
->resum_qmask
+ 1,
1098 tb
->nonresum_qmask
+ 1);
1102 static void *mdesc_iterate_over_cpus(void *(*func
)(struct mdesc_handle
*, u64
, int, void *), void *arg
, cpumask_t
*mask
)
1104 struct mdesc_handle
*hp
= mdesc_grab();
1108 mdesc_for_each_node_by_name(hp
, mp
, "cpu") {
1109 const u64
*id
= mdesc_get_property(hp
, mp
, "id", NULL
);
1113 if (cpuid
>= NR_CPUS
) {
1114 printk(KERN_WARNING
"Ignoring CPU %d which is "
1115 ">= NR_CPUS (%d)\n",
1119 if (!cpumask_test_cpu(cpuid
, mask
))
1123 ret
= func(hp
, mp
, cpuid
, arg
);
1132 static void *record_one_cpu(struct mdesc_handle
*hp
, u64 mp
, int cpuid
,
1137 set_cpu_present(cpuid
, true);
1142 void mdesc_populate_present_mask(cpumask_t
*mask
)
1144 if (tlb_type
!= hypervisor
)
1148 mdesc_iterate_over_cpus(record_one_cpu
, NULL
, mask
);
1151 static void * __init
check_one_pgsz(struct mdesc_handle
*hp
, u64 mp
, int cpuid
, void *arg
)
1153 const u64
*pgsz_prop
= mdesc_get_property(hp
, mp
, "mmu-page-size-list", NULL
);
1154 unsigned long *pgsz_mask
= arg
;
1157 val
= (HV_PGSZ_MASK_8K
| HV_PGSZ_MASK_64K
|
1158 HV_PGSZ_MASK_512K
| HV_PGSZ_MASK_4MB
);
1169 void __init
mdesc_get_page_sizes(cpumask_t
*mask
, unsigned long *pgsz_mask
)
1172 mdesc_iterate_over_cpus(check_one_pgsz
, pgsz_mask
, mask
);
1175 static void *fill_in_one_cpu(struct mdesc_handle
*hp
, u64 mp
, int cpuid
,
1178 const u64
*cfreq
= mdesc_get_property(hp
, mp
, "clock-frequency", NULL
);
1179 struct trap_per_cpu
*tb
;
1184 /* On uniprocessor we only want the values for the
1185 * real physical cpu the kernel booted onto, however
1186 * cpu_data() only has one entry at index 0.
1188 if (cpuid
!= real_hard_smp_processor_id())
1193 c
= &cpu_data(cpuid
);
1194 c
->clock_tick
= *cfreq
;
1196 tb
= &trap_block
[cpuid
];
1197 get_mondo_data(hp
, mp
, tb
);
1199 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_FWD
) {
1200 u64 j
, t
= mdesc_arc_target(hp
, a
);
1203 t_name
= mdesc_node_name(hp
, t
);
1204 if (!strcmp(t_name
, "cache")) {
1205 fill_in_one_cache(c
, hp
, t
);
1209 mdesc_for_each_arc(j
, hp
, t
, MDESC_ARC_TYPE_FWD
) {
1210 u64 n
= mdesc_arc_target(hp
, j
);
1213 n_name
= mdesc_node_name(hp
, n
);
1214 if (!strcmp(n_name
, "cache"))
1215 fill_in_one_cache(c
, hp
, n
);
1225 void mdesc_fill_in_cpu_data(cpumask_t
*mask
)
1227 struct mdesc_handle
*hp
;
1229 mdesc_iterate_over_cpus(fill_in_one_cpu
, NULL
, mask
);
1239 smp_fill_in_sib_core_maps();
1242 /* mdesc_open() - Grab a reference to mdesc_handle when /dev/mdesc is
1243 * opened. Hold this reference until /dev/mdesc is closed to ensure
1244 * mdesc data structure is not released underneath us. Store the
1245 * pointer to mdesc structure in private_data for read and seek to use
1247 static int mdesc_open(struct inode
*inode
, struct file
*file
)
1249 struct mdesc_handle
*hp
= mdesc_grab();
1254 file
->private_data
= hp
;
1259 static ssize_t
mdesc_read(struct file
*file
, char __user
*buf
,
1260 size_t len
, loff_t
*offp
)
1262 struct mdesc_handle
*hp
= file
->private_data
;
1263 unsigned char *mdesc
;
1264 int bytes_left
, count
= len
;
1266 if (*offp
>= hp
->handle_size
)
1269 bytes_left
= hp
->handle_size
- *offp
;
1270 if (count
> bytes_left
)
1273 mdesc
= (unsigned char *)&hp
->mdesc
;
1275 if (!copy_to_user(buf
, mdesc
, count
)) {
1283 static loff_t
mdesc_llseek(struct file
*file
, loff_t offset
, int whence
)
1285 struct mdesc_handle
*hp
= file
->private_data
;
1287 return no_seek_end_llseek_size(file
, offset
, whence
, hp
->handle_size
);
1290 /* mdesc_close() - /dev/mdesc is being closed, release the reference to
1293 static int mdesc_close(struct inode
*inode
, struct file
*file
)
1295 mdesc_release(file
->private_data
);
1299 static const struct file_operations mdesc_fops
= {
1302 .llseek
= mdesc_llseek
,
1303 .release
= mdesc_close
,
1304 .owner
= THIS_MODULE
,
1307 static struct miscdevice mdesc_misc
= {
1308 .minor
= MISC_DYNAMIC_MINOR
,
1310 .fops
= &mdesc_fops
,
1313 static int __init
mdesc_misc_init(void)
1315 return misc_register(&mdesc_misc
);
1318 __initcall(mdesc_misc_init
);
1320 void __init
sun4v_mdesc_init(void)
1322 struct mdesc_handle
*hp
;
1323 unsigned long len
, real_len
, status
;
1325 (void) sun4v_mach_desc(0UL, 0UL, &len
);
1327 printk("MDESC: Size is %lu bytes.\n", len
);
1329 hp
= mdesc_alloc(len
, &memblock_mdesc_ops
);
1331 prom_printf("MDESC: alloc of %lu bytes failed.\n", len
);
1335 status
= sun4v_mach_desc(__pa(&hp
->mdesc
), len
, &real_len
);
1336 if (status
!= HV_EOK
|| real_len
> len
) {
1337 prom_printf("sun4v_mach_desc fails, err(%lu), "
1338 "len(%lu), real_len(%lu)\n",
1339 status
, len
, real_len
);
1346 report_platform_properties();