1 // SPDX-License-Identifier: GPL-2.0
2 /* mdesc.c: Sun4V machine description handling.
4 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
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>
12 #include <linux/miscdevice.h>
13 #include <linux/memblock.h>
14 #include <linux/export.h>
15 #include <linux/refcount.h>
17 #include <asm/cpudata.h>
18 #include <asm/hypervisor.h>
19 #include <asm/mdesc.h>
21 #include <linux/uaccess.h>
22 #include <asm/oplib.h>
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.
31 * Actually, it isn't even a DAG, because there are back pointers
32 * which create cycles in the graph.
34 * mdesc_hdr and mdesc_elem describe the layout of the data structure
35 * we get from the Hypervisor.
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 */
43 } __attribute__((aligned(16)));
47 #define MD_LIST_END 0x00
49 #define MD_NODE_END 0x45
51 #define MD_PROP_ARC 0x61
52 #define MD_PROP_VAL 0x76
53 #define MD_PROP_STR 0x73
54 #define MD_PROP_DATA 0x64
67 struct mdesc_mem_ops
{
68 struct mdesc_handle
*(*alloc
)(unsigned int mdesc_size
);
69 void (*free
)(struct mdesc_handle
*handle
);
73 struct list_head list
;
74 struct mdesc_mem_ops
*mops
;
77 unsigned int handle_size
;
78 struct mdesc_hdr mdesc
;
81 typedef int (*mdesc_node_info_get_f
)(struct mdesc_handle
*, u64
,
82 union md_node_info
*);
83 typedef void (*mdesc_node_info_rel_f
)(union md_node_info
*);
84 typedef bool (*mdesc_node_match_f
)(union md_node_info
*, union md_node_info
*);
88 mdesc_node_info_get_f get_info
;
89 mdesc_node_info_rel_f rel_info
;
90 mdesc_node_match_f node_match
;
93 static int get_vdev_port_node_info(struct mdesc_handle
*md
, u64 node
,
94 union md_node_info
*node_info
);
95 static void rel_vdev_port_node_info(union md_node_info
*node_info
);
96 static bool vdev_port_node_match(union md_node_info
*a_node_info
,
97 union md_node_info
*b_node_info
);
99 static int get_ds_port_node_info(struct mdesc_handle
*md
, u64 node
,
100 union md_node_info
*node_info
);
101 static void rel_ds_port_node_info(union md_node_info
*node_info
);
102 static bool ds_port_node_match(union md_node_info
*a_node_info
,
103 union md_node_info
*b_node_info
);
105 /* supported node types which can be registered */
106 static struct md_node_ops md_node_ops_table
[] = {
107 {"virtual-device-port", get_vdev_port_node_info
,
108 rel_vdev_port_node_info
, vdev_port_node_match
},
109 {"domain-services-port", get_ds_port_node_info
,
110 rel_ds_port_node_info
, ds_port_node_match
},
111 {NULL
, NULL
, NULL
, NULL
}
114 static void mdesc_get_node_ops(const char *node_name
,
115 mdesc_node_info_get_f
*get_info_f
,
116 mdesc_node_info_rel_f
*rel_info_f
,
117 mdesc_node_match_f
*match_f
)
133 for (i
= 0; md_node_ops_table
[i
].name
!= NULL
; i
++) {
134 if (strcmp(md_node_ops_table
[i
].name
, node_name
) == 0) {
136 *get_info_f
= md_node_ops_table
[i
].get_info
;
139 *rel_info_f
= md_node_ops_table
[i
].rel_info
;
142 *match_f
= md_node_ops_table
[i
].node_match
;
149 static void mdesc_handle_init(struct mdesc_handle
*hp
,
150 unsigned int handle_size
,
153 BUG_ON(((unsigned long)&hp
->mdesc
) & (16UL - 1));
155 memset(hp
, 0, handle_size
);
156 INIT_LIST_HEAD(&hp
->list
);
157 hp
->self_base
= base
;
158 refcount_set(&hp
->refcnt
, 1);
159 hp
->handle_size
= handle_size
;
162 static struct mdesc_handle
* __init
mdesc_memblock_alloc(unsigned int mdesc_size
)
164 unsigned int handle_size
, alloc_size
;
165 struct mdesc_handle
*hp
;
168 handle_size
= (sizeof(struct mdesc_handle
) -
169 sizeof(struct mdesc_hdr
) +
171 alloc_size
= PAGE_ALIGN(handle_size
);
173 paddr
= memblock_phys_alloc(alloc_size
, PAGE_SIZE
);
178 mdesc_handle_init(hp
, handle_size
, hp
);
183 static void __init
mdesc_memblock_free(struct mdesc_handle
*hp
)
185 unsigned int alloc_size
;
188 BUG_ON(refcount_read(&hp
->refcnt
) != 0);
189 BUG_ON(!list_empty(&hp
->list
));
191 alloc_size
= PAGE_ALIGN(hp
->handle_size
);
193 memblock_free_late(start
, alloc_size
);
196 static struct mdesc_mem_ops memblock_mdesc_ops
= {
197 .alloc
= mdesc_memblock_alloc
,
198 .free
= mdesc_memblock_free
,
201 static struct mdesc_handle
*mdesc_kmalloc(unsigned int mdesc_size
)
203 unsigned int handle_size
;
204 struct mdesc_handle
*hp
;
208 handle_size
= (sizeof(struct mdesc_handle
) -
209 sizeof(struct mdesc_hdr
) +
211 base
= kmalloc(handle_size
+ 15, GFP_KERNEL
| __GFP_RETRY_MAYFAIL
);
215 addr
= (unsigned long)base
;
216 addr
= (addr
+ 15UL) & ~15UL;
217 hp
= (struct mdesc_handle
*) addr
;
219 mdesc_handle_init(hp
, handle_size
, base
);
224 static void mdesc_kfree(struct mdesc_handle
*hp
)
226 BUG_ON(refcount_read(&hp
->refcnt
) != 0);
227 BUG_ON(!list_empty(&hp
->list
));
229 kfree(hp
->self_base
);
232 static struct mdesc_mem_ops kmalloc_mdesc_memops
= {
233 .alloc
= mdesc_kmalloc
,
237 static struct mdesc_handle
*mdesc_alloc(unsigned int mdesc_size
,
238 struct mdesc_mem_ops
*mops
)
240 struct mdesc_handle
*hp
= mops
->alloc(mdesc_size
);
248 static void mdesc_free(struct mdesc_handle
*hp
)
253 static struct mdesc_handle
*cur_mdesc
;
254 static LIST_HEAD(mdesc_zombie_list
);
255 static DEFINE_SPINLOCK(mdesc_lock
);
257 struct mdesc_handle
*mdesc_grab(void)
259 struct mdesc_handle
*hp
;
262 spin_lock_irqsave(&mdesc_lock
, flags
);
265 refcount_inc(&hp
->refcnt
);
266 spin_unlock_irqrestore(&mdesc_lock
, flags
);
270 EXPORT_SYMBOL(mdesc_grab
);
272 void mdesc_release(struct mdesc_handle
*hp
)
276 spin_lock_irqsave(&mdesc_lock
, flags
);
277 if (refcount_dec_and_test(&hp
->refcnt
)) {
278 list_del_init(&hp
->list
);
281 spin_unlock_irqrestore(&mdesc_lock
, flags
);
283 EXPORT_SYMBOL(mdesc_release
);
285 static DEFINE_MUTEX(mdesc_mutex
);
286 static struct mdesc_notifier_client
*client_list
;
288 void mdesc_register_notifier(struct mdesc_notifier_client
*client
)
290 bool supported
= false;
294 mutex_lock(&mdesc_mutex
);
296 /* check to see if the node is supported for registration */
297 for (i
= 0; md_node_ops_table
[i
].name
!= NULL
; i
++) {
298 if (strcmp(md_node_ops_table
[i
].name
, client
->node_name
) == 0) {
305 pr_err("MD: %s node not supported\n", client
->node_name
);
306 mutex_unlock(&mdesc_mutex
);
310 client
->next
= client_list
;
311 client_list
= client
;
313 mdesc_for_each_node_by_name(cur_mdesc
, node
, client
->node_name
)
314 client
->add(cur_mdesc
, node
, client
->node_name
);
316 mutex_unlock(&mdesc_mutex
);
319 static const u64
*parent_cfg_handle(struct mdesc_handle
*hp
, u64 node
)
325 mdesc_for_each_arc(a
, hp
, node
, MDESC_ARC_TYPE_BACK
) {
328 target
= mdesc_arc_target(hp
, a
);
329 id
= mdesc_get_property(hp
, target
,
338 static int get_vdev_port_node_info(struct mdesc_handle
*md
, u64 node
,
339 union md_node_info
*node_info
)
341 const u64
*parent_cfg_hdlp
;
346 * Virtual device nodes are distinguished by:
349 * 3. parent node "cfg-handle" property
351 idp
= mdesc_get_property(md
, node
, "id", NULL
);
352 name
= mdesc_get_property(md
, node
, "name", NULL
);
353 parent_cfg_hdlp
= parent_cfg_handle(md
, node
);
355 if (!idp
|| !name
|| !parent_cfg_hdlp
)
358 node_info
->vdev_port
.id
= *idp
;
359 node_info
->vdev_port
.name
= kstrdup_const(name
, GFP_KERNEL
);
360 if (!node_info
->vdev_port
.name
)
362 node_info
->vdev_port
.parent_cfg_hdl
= *parent_cfg_hdlp
;
367 static void rel_vdev_port_node_info(union md_node_info
*node_info
)
369 if (node_info
&& node_info
->vdev_port
.name
) {
370 kfree_const(node_info
->vdev_port
.name
);
371 node_info
->vdev_port
.name
= NULL
;
375 static bool vdev_port_node_match(union md_node_info
*a_node_info
,
376 union md_node_info
*b_node_info
)
378 if (a_node_info
->vdev_port
.id
!= b_node_info
->vdev_port
.id
)
381 if (a_node_info
->vdev_port
.parent_cfg_hdl
!=
382 b_node_info
->vdev_port
.parent_cfg_hdl
)
385 if (strncmp(a_node_info
->vdev_port
.name
,
386 b_node_info
->vdev_port
.name
, MDESC_MAX_STR_LEN
) != 0)
392 static int get_ds_port_node_info(struct mdesc_handle
*md
, u64 node
,
393 union md_node_info
*node_info
)
397 /* DS port nodes use the "id" property to distinguish them */
398 idp
= mdesc_get_property(md
, node
, "id", NULL
);
402 node_info
->ds_port
.id
= *idp
;
407 static void rel_ds_port_node_info(union md_node_info
*node_info
)
411 static bool ds_port_node_match(union md_node_info
*a_node_info
,
412 union md_node_info
*b_node_info
)
414 if (a_node_info
->ds_port
.id
!= b_node_info
->ds_port
.id
)
420 /* Run 'func' on nodes which are in A but not in B. */
421 static void invoke_on_missing(const char *name
,
422 struct mdesc_handle
*a
,
423 struct mdesc_handle
*b
,
424 void (*func
)(struct mdesc_handle
*, u64
,
425 const char *node_name
))
427 mdesc_node_info_get_f get_info_func
;
428 mdesc_node_info_rel_f rel_info_func
;
429 mdesc_node_match_f node_match_func
;
430 union md_node_info a_node_info
;
431 union md_node_info b_node_info
;
438 * Find the get_info, rel_info and node_match ops for the given
441 mdesc_get_node_ops(name
, &get_info_func
, &rel_info_func
,
444 /* If we didn't find a match, the node type is not supported */
445 if (!get_info_func
|| !rel_info_func
|| !node_match_func
) {
446 pr_err("MD: %s node type is not supported\n", name
);
450 mdesc_for_each_node_by_name(a
, a_node
, name
) {
453 rv
= get_info_func(a
, a_node
, &a_node_info
);
455 pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
460 /* Check each node in B for node matching a_node */
461 mdesc_for_each_node_by_name(b
, b_node
, name
) {
462 rv
= get_info_func(b
, b_node
, &b_node_info
);
466 if (node_match_func(&a_node_info
, &b_node_info
)) {
468 rel_info_func(&b_node_info
);
472 rel_info_func(&b_node_info
);
475 rel_info_func(&a_node_info
);
478 func(a
, a_node
, name
);
482 static void notify_one(struct mdesc_notifier_client
*p
,
483 struct mdesc_handle
*old_hp
,
484 struct mdesc_handle
*new_hp
)
486 invoke_on_missing(p
->node_name
, old_hp
, new_hp
, p
->remove
);
487 invoke_on_missing(p
->node_name
, new_hp
, old_hp
, p
->add
);
490 static void mdesc_notify_clients(struct mdesc_handle
*old_hp
,
491 struct mdesc_handle
*new_hp
)
493 struct mdesc_notifier_client
*p
= client_list
;
496 notify_one(p
, old_hp
, new_hp
);
501 void mdesc_update(void)
503 unsigned long len
, real_len
, status
;
504 struct mdesc_handle
*hp
, *orig_hp
;
507 mutex_lock(&mdesc_mutex
);
509 (void) sun4v_mach_desc(0UL, 0UL, &len
);
511 hp
= mdesc_alloc(len
, &kmalloc_mdesc_memops
);
513 printk(KERN_ERR
"MD: mdesc alloc fails\n");
517 status
= sun4v_mach_desc(__pa(&hp
->mdesc
), len
, &real_len
);
518 if (status
!= HV_EOK
|| real_len
> len
) {
519 printk(KERN_ERR
"MD: mdesc reread fails with %lu\n",
521 refcount_dec(&hp
->refcnt
);
526 spin_lock_irqsave(&mdesc_lock
, flags
);
529 spin_unlock_irqrestore(&mdesc_lock
, flags
);
531 mdesc_notify_clients(orig_hp
, hp
);
533 spin_lock_irqsave(&mdesc_lock
, flags
);
534 if (refcount_dec_and_test(&orig_hp
->refcnt
))
537 list_add(&orig_hp
->list
, &mdesc_zombie_list
);
538 spin_unlock_irqrestore(&mdesc_lock
, flags
);
541 mutex_unlock(&mdesc_mutex
);
544 u64
mdesc_get_node(struct mdesc_handle
*hp
, const char *node_name
,
545 union md_node_info
*node_info
)
547 mdesc_node_info_get_f get_info_func
;
548 mdesc_node_info_rel_f rel_info_func
;
549 mdesc_node_match_f node_match_func
;
550 union md_node_info hp_node_info
;
554 if (hp
== NULL
|| node_name
== NULL
|| node_info
== NULL
)
555 return MDESC_NODE_NULL
;
557 /* Find the ops for the given node name */
558 mdesc_get_node_ops(node_name
, &get_info_func
, &rel_info_func
,
561 /* If we didn't find ops for the given node name, it is not supported */
562 if (!get_info_func
|| !rel_info_func
|| !node_match_func
) {
563 pr_err("MD: %s node is not supported\n", node_name
);
567 mdesc_for_each_node_by_name(hp
, hp_node
, node_name
) {
568 rv
= get_info_func(hp
, hp_node
, &hp_node_info
);
572 if (node_match_func(node_info
, &hp_node_info
))
575 rel_info_func(&hp_node_info
);
578 rel_info_func(&hp_node_info
);
582 EXPORT_SYMBOL(mdesc_get_node
);
584 int mdesc_get_node_info(struct mdesc_handle
*hp
, u64 node
,
585 const char *node_name
, union md_node_info
*node_info
)
587 mdesc_node_info_get_f get_info_func
;
590 if (hp
== NULL
|| node
== MDESC_NODE_NULL
||
591 node_name
== NULL
|| node_info
== NULL
)
594 /* Find the get_info op for the given node name */
595 mdesc_get_node_ops(node_name
, &get_info_func
, NULL
, NULL
);
597 /* If we didn't find a get_info_func, the node name is not supported */
598 if (get_info_func
== NULL
) {
599 pr_err("MD: %s node is not supported\n", node_name
);
603 rv
= get_info_func(hp
, node
, node_info
);
605 pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
612 EXPORT_SYMBOL(mdesc_get_node_info
);
614 static struct mdesc_elem
*node_block(struct mdesc_hdr
*mdesc
)
616 return (struct mdesc_elem
*) mdesc
->data
;
619 static void *name_block(struct mdesc_hdr
*mdesc
)
621 return ((void *) node_block(mdesc
)) + mdesc
->node_sz
;
624 static void *data_block(struct mdesc_hdr
*mdesc
)
626 return ((void *) name_block(mdesc
)) + mdesc
->name_sz
;
629 u64
mdesc_node_by_name(struct mdesc_handle
*hp
,
630 u64 from_node
, const char *name
)
632 struct mdesc_elem
*ep
= node_block(&hp
->mdesc
);
633 const char *names
= name_block(&hp
->mdesc
);
634 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
637 if (from_node
== MDESC_NODE_NULL
) {
639 } else if (from_node
>= last_node
) {
640 return MDESC_NODE_NULL
;
642 ret
= ep
[from_node
].d
.val
;
645 while (ret
< last_node
) {
646 if (ep
[ret
].tag
!= MD_NODE
)
647 return MDESC_NODE_NULL
;
648 if (!strcmp(names
+ ep
[ret
].name_offset
, name
))
652 if (ret
>= last_node
)
653 ret
= MDESC_NODE_NULL
;
656 EXPORT_SYMBOL(mdesc_node_by_name
);
658 const void *mdesc_get_property(struct mdesc_handle
*hp
, u64 node
,
659 const char *name
, int *lenp
)
661 const char *names
= name_block(&hp
->mdesc
);
662 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
663 void *data
= data_block(&hp
->mdesc
);
664 struct mdesc_elem
*ep
;
666 if (node
== MDESC_NODE_NULL
|| node
>= last_node
)
669 ep
= node_block(&hp
->mdesc
) + node
;
671 for (; ep
->tag
!= MD_NODE_END
; ep
++) {
683 val
= data
+ ep
->d
.data
.data_offset
;
684 len
= ep
->d
.data
.data_len
;
693 if (!strcmp(names
+ ep
->name_offset
, name
)) {
702 EXPORT_SYMBOL(mdesc_get_property
);
704 u64
mdesc_next_arc(struct mdesc_handle
*hp
, u64 from
, const char *arc_type
)
706 struct mdesc_elem
*ep
, *base
= node_block(&hp
->mdesc
);
707 const char *names
= name_block(&hp
->mdesc
);
708 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
710 if (from
== MDESC_NODE_NULL
|| from
>= last_node
)
711 return MDESC_NODE_NULL
;
716 for (; ep
->tag
!= MD_NODE_END
; ep
++) {
717 if (ep
->tag
!= MD_PROP_ARC
)
720 if (strcmp(names
+ ep
->name_offset
, arc_type
))
726 return MDESC_NODE_NULL
;
728 EXPORT_SYMBOL(mdesc_next_arc
);
730 u64
mdesc_arc_target(struct mdesc_handle
*hp
, u64 arc
)
732 struct mdesc_elem
*ep
, *base
= node_block(&hp
->mdesc
);
738 EXPORT_SYMBOL(mdesc_arc_target
);
740 const char *mdesc_node_name(struct mdesc_handle
*hp
, u64 node
)
742 struct mdesc_elem
*ep
, *base
= node_block(&hp
->mdesc
);
743 const char *names
= name_block(&hp
->mdesc
);
744 u64 last_node
= hp
->mdesc
.node_sz
/ 16;
746 if (node
== MDESC_NODE_NULL
|| node
>= last_node
)
750 if (ep
->tag
!= MD_NODE
)
753 return names
+ ep
->name_offset
;
755 EXPORT_SYMBOL(mdesc_node_name
);
757 static u64 max_cpus
= 64;
759 static void __init
report_platform_properties(void)
761 struct mdesc_handle
*hp
= mdesc_grab();
762 u64 pn
= mdesc_node_by_name(hp
, MDESC_NODE_NULL
, "platform");
766 if (pn
== MDESC_NODE_NULL
) {
767 prom_printf("No platform node in machine-description.\n");
771 s
= mdesc_get_property(hp
, pn
, "banner-name", NULL
);
772 printk("PLATFORM: banner-name [%s]\n", s
);
773 s
= mdesc_get_property(hp
, pn
, "name", NULL
);
774 printk("PLATFORM: name [%s]\n", s
);
776 v
= mdesc_get_property(hp
, pn
, "hostid", NULL
);
778 printk("PLATFORM: hostid [%08llx]\n", *v
);
779 v
= mdesc_get_property(hp
, pn
, "serial#", NULL
);
781 printk("PLATFORM: serial# [%08llx]\n", *v
);
782 v
= mdesc_get_property(hp
, pn
, "stick-frequency", NULL
);
783 printk("PLATFORM: stick-frequency [%08llx]\n", *v
);
784 v
= mdesc_get_property(hp
, pn
, "mac-address", NULL
);
786 printk("PLATFORM: mac-address [%llx]\n", *v
);
787 v
= mdesc_get_property(hp
, pn
, "watchdog-resolution", NULL
);
789 printk("PLATFORM: watchdog-resolution [%llu ms]\n", *v
);
790 v
= mdesc_get_property(hp
, pn
, "watchdog-max-timeout", NULL
);
792 printk("PLATFORM: watchdog-max-timeout [%llu ms]\n", *v
);
793 v
= mdesc_get_property(hp
, pn
, "max-cpus", NULL
);
796 printk("PLATFORM: max-cpus [%llu]\n", max_cpus
);
805 if (max_cpu
> NR_CPUS
)
810 for (i
= 0; i
< max_cpu
; i
++)
811 set_cpu_possible(i
, true);
818 static void fill_in_one_cache(cpuinfo_sparc
*c
, struct mdesc_handle
*hp
, u64 mp
)
820 const u64
*level
= mdesc_get_property(hp
, mp
, "level", NULL
);
821 const u64
*size
= mdesc_get_property(hp
, mp
, "size", NULL
);
822 const u64
*line_size
= mdesc_get_property(hp
, mp
, "line-size", NULL
);
826 type
= mdesc_get_property(hp
, mp
, "type", &type_len
);
830 if (of_find_in_proplist(type
, "instn", type_len
)) {
831 c
->icache_size
= *size
;
832 c
->icache_line_size
= *line_size
;
833 } else if (of_find_in_proplist(type
, "data", type_len
)) {
834 c
->dcache_size
= *size
;
835 c
->dcache_line_size
= *line_size
;
840 c
->ecache_size
= *size
;
841 c
->ecache_line_size
= *line_size
;
851 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_FWD
) {
852 u64 target
= mdesc_arc_target(hp
, a
);
853 const char *name
= mdesc_node_name(hp
, target
);
855 if (!strcmp(name
, "cache"))
856 fill_in_one_cache(c
, hp
, target
);
861 static void find_back_node_value(struct mdesc_handle
*hp
, u64 node
,
863 void (*func
)(struct mdesc_handle
*, u64
, int),
868 /* Since we have an estimate of recursion depth, do a sanity check. */
872 mdesc_for_each_arc(arc
, hp
, node
, MDESC_ARC_TYPE_BACK
) {
873 u64 n
= mdesc_arc_target(hp
, arc
);
874 const char *name
= mdesc_node_name(hp
, n
);
876 if (!strcmp(srch_val
, name
))
879 find_back_node_value(hp
, n
, srch_val
, func
, val
, depth
-1);
883 static void __mark_core_id(struct mdesc_handle
*hp
, u64 node
,
886 const u64
*id
= mdesc_get_property(hp
, node
, "id", NULL
);
888 if (*id
< num_possible_cpus())
889 cpu_data(*id
).core_id
= core_id
;
892 static void __mark_max_cache_id(struct mdesc_handle
*hp
, u64 node
,
895 const u64
*id
= mdesc_get_property(hp
, node
, "id", NULL
);
897 if (*id
< num_possible_cpus()) {
898 cpu_data(*id
).max_cache_id
= max_cache_id
;
901 * On systems without explicit socket descriptions socket
904 cpu_data(*id
).sock_id
= max_cache_id
;
908 static void mark_core_ids(struct mdesc_handle
*hp
, u64 mp
,
911 find_back_node_value(hp
, mp
, "cpu", __mark_core_id
, core_id
, 10);
914 static void mark_max_cache_ids(struct mdesc_handle
*hp
, u64 mp
,
917 find_back_node_value(hp
, mp
, "cpu", __mark_max_cache_id
,
921 static void set_core_ids(struct mdesc_handle
*hp
)
928 /* Identify unique cores by looking for cpus backpointed to by
929 * level 1 instruction caches.
931 mdesc_for_each_node_by_name(hp
, mp
, "cache") {
936 level
= mdesc_get_property(hp
, mp
, "level", NULL
);
940 type
= mdesc_get_property(hp
, mp
, "type", &len
);
941 if (!of_find_in_proplist(type
, "instn", len
))
944 mark_core_ids(hp
, mp
, idx
);
949 static int set_max_cache_ids_by_cache(struct mdesc_handle
*hp
, int level
)
956 * Identify unique highest level of shared cache by looking for cpus
957 * backpointed to by shared level N caches.
959 mdesc_for_each_node_by_name(hp
, mp
, "cache") {
962 cur_lvl
= mdesc_get_property(hp
, mp
, "level", NULL
);
963 if (*cur_lvl
!= level
)
965 mark_max_cache_ids(hp
, mp
, idx
);
972 static void set_sock_ids_by_socket(struct mdesc_handle
*hp
, u64 mp
)
976 mdesc_for_each_node_by_name(hp
, mp
, "socket") {
979 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_FWD
) {
980 u64 t
= mdesc_arc_target(hp
, a
);
984 name
= mdesc_node_name(hp
, t
);
985 if (strcmp(name
, "cpu"))
988 id
= mdesc_get_property(hp
, t
, "id", NULL
);
989 if (*id
< num_possible_cpus())
990 cpu_data(*id
).sock_id
= idx
;
996 static void set_sock_ids(struct mdesc_handle
*hp
)
1001 * Find the highest level of shared cache which pre-T7 is also
1004 if (!set_max_cache_ids_by_cache(hp
, 3))
1005 set_max_cache_ids_by_cache(hp
, 2);
1007 /* If machine description exposes sockets data use it.*/
1008 mp
= mdesc_node_by_name(hp
, MDESC_NODE_NULL
, "sockets");
1009 if (mp
!= MDESC_NODE_NULL
)
1010 set_sock_ids_by_socket(hp
, mp
);
1013 static void mark_proc_ids(struct mdesc_handle
*hp
, u64 mp
, int proc_id
)
1017 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_BACK
) {
1018 u64 t
= mdesc_arc_target(hp
, a
);
1022 name
= mdesc_node_name(hp
, t
);
1023 if (strcmp(name
, "cpu"))
1026 id
= mdesc_get_property(hp
, t
, "id", NULL
);
1028 cpu_data(*id
).proc_id
= proc_id
;
1032 static void __set_proc_ids(struct mdesc_handle
*hp
, const char *exec_unit_name
)
1038 mdesc_for_each_node_by_name(hp
, mp
, exec_unit_name
) {
1042 type
= mdesc_get_property(hp
, mp
, "type", &len
);
1043 if (!of_find_in_proplist(type
, "int", len
) &&
1044 !of_find_in_proplist(type
, "integer", len
))
1047 mark_proc_ids(hp
, mp
, idx
);
1052 static void set_proc_ids(struct mdesc_handle
*hp
)
1054 __set_proc_ids(hp
, "exec_unit");
1055 __set_proc_ids(hp
, "exec-unit");
1058 static void get_one_mondo_bits(const u64
*p
, unsigned int *mask
,
1059 unsigned long def
, unsigned long max
)
1067 if (!val
|| val
>= 64)
1073 *mask
= ((1U << val
) * 64U) - 1U;
1077 *mask
= ((1U << def
) * 64U) - 1U;
1080 static void get_mondo_data(struct mdesc_handle
*hp
, u64 mp
,
1081 struct trap_per_cpu
*tb
)
1086 val
= mdesc_get_property(hp
, mp
, "q-cpu-mondo-#bits", NULL
);
1087 get_one_mondo_bits(val
, &tb
->cpu_mondo_qmask
, 7, ilog2(max_cpus
* 2));
1089 val
= mdesc_get_property(hp
, mp
, "q-dev-mondo-#bits", NULL
);
1090 get_one_mondo_bits(val
, &tb
->dev_mondo_qmask
, 7, 8);
1092 val
= mdesc_get_property(hp
, mp
, "q-resumable-#bits", NULL
);
1093 get_one_mondo_bits(val
, &tb
->resum_qmask
, 6, 7);
1095 val
= mdesc_get_property(hp
, mp
, "q-nonresumable-#bits", NULL
);
1096 get_one_mondo_bits(val
, &tb
->nonresum_qmask
, 2, 2);
1098 pr_info("SUN4V: Mondo queue sizes "
1099 "[cpu(%u) dev(%u) r(%u) nr(%u)]\n",
1100 tb
->cpu_mondo_qmask
+ 1,
1101 tb
->dev_mondo_qmask
+ 1,
1102 tb
->resum_qmask
+ 1,
1103 tb
->nonresum_qmask
+ 1);
1107 static void *mdesc_iterate_over_cpus(void *(*func
)(struct mdesc_handle
*, u64
, int, void *), void *arg
, cpumask_t
*mask
)
1109 struct mdesc_handle
*hp
= mdesc_grab();
1113 mdesc_for_each_node_by_name(hp
, mp
, "cpu") {
1114 const u64
*id
= mdesc_get_property(hp
, mp
, "id", NULL
);
1118 if (cpuid
>= NR_CPUS
) {
1119 printk(KERN_WARNING
"Ignoring CPU %d which is "
1120 ">= NR_CPUS (%d)\n",
1124 if (!cpumask_test_cpu(cpuid
, mask
))
1128 ret
= func(hp
, mp
, cpuid
, arg
);
1137 static void *record_one_cpu(struct mdesc_handle
*hp
, u64 mp
, int cpuid
,
1142 set_cpu_present(cpuid
, true);
1147 void mdesc_populate_present_mask(cpumask_t
*mask
)
1149 if (tlb_type
!= hypervisor
)
1153 mdesc_iterate_over_cpus(record_one_cpu
, NULL
, mask
);
1156 static void * __init
check_one_pgsz(struct mdesc_handle
*hp
, u64 mp
, int cpuid
, void *arg
)
1158 const u64
*pgsz_prop
= mdesc_get_property(hp
, mp
, "mmu-page-size-list", NULL
);
1159 unsigned long *pgsz_mask
= arg
;
1162 val
= (HV_PGSZ_MASK_8K
| HV_PGSZ_MASK_64K
|
1163 HV_PGSZ_MASK_512K
| HV_PGSZ_MASK_4MB
);
1174 void __init
mdesc_get_page_sizes(cpumask_t
*mask
, unsigned long *pgsz_mask
)
1177 mdesc_iterate_over_cpus(check_one_pgsz
, pgsz_mask
, mask
);
1180 static void *fill_in_one_cpu(struct mdesc_handle
*hp
, u64 mp
, int cpuid
,
1183 const u64
*cfreq
= mdesc_get_property(hp
, mp
, "clock-frequency", NULL
);
1184 struct trap_per_cpu
*tb
;
1189 /* On uniprocessor we only want the values for the
1190 * real physical cpu the kernel booted onto, however
1191 * cpu_data() only has one entry at index 0.
1193 if (cpuid
!= real_hard_smp_processor_id())
1198 c
= &cpu_data(cpuid
);
1199 c
->clock_tick
= *cfreq
;
1201 tb
= &trap_block
[cpuid
];
1202 get_mondo_data(hp
, mp
, tb
);
1204 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_FWD
) {
1205 u64 j
, t
= mdesc_arc_target(hp
, a
);
1208 t_name
= mdesc_node_name(hp
, t
);
1209 if (!strcmp(t_name
, "cache")) {
1210 fill_in_one_cache(c
, hp
, t
);
1214 mdesc_for_each_arc(j
, hp
, t
, MDESC_ARC_TYPE_FWD
) {
1215 u64 n
= mdesc_arc_target(hp
, j
);
1218 n_name
= mdesc_node_name(hp
, n
);
1219 if (!strcmp(n_name
, "cache"))
1220 fill_in_one_cache(c
, hp
, n
);
1230 void mdesc_fill_in_cpu_data(cpumask_t
*mask
)
1232 struct mdesc_handle
*hp
;
1234 mdesc_iterate_over_cpus(fill_in_one_cpu
, NULL
, mask
);
1244 smp_fill_in_sib_core_maps();
1247 /* mdesc_open() - Grab a reference to mdesc_handle when /dev/mdesc is
1248 * opened. Hold this reference until /dev/mdesc is closed to ensure
1249 * mdesc data structure is not released underneath us. Store the
1250 * pointer to mdesc structure in private_data for read and seek to use
1252 static int mdesc_open(struct inode
*inode
, struct file
*file
)
1254 struct mdesc_handle
*hp
= mdesc_grab();
1259 file
->private_data
= hp
;
1264 static ssize_t
mdesc_read(struct file
*file
, char __user
*buf
,
1265 size_t len
, loff_t
*offp
)
1267 struct mdesc_handle
*hp
= file
->private_data
;
1268 unsigned char *mdesc
;
1269 int bytes_left
, count
= len
;
1271 if (*offp
>= hp
->handle_size
)
1274 bytes_left
= hp
->handle_size
- *offp
;
1275 if (count
> bytes_left
)
1278 mdesc
= (unsigned char *)&hp
->mdesc
;
1280 if (!copy_to_user(buf
, mdesc
, count
)) {
1288 static loff_t
mdesc_llseek(struct file
*file
, loff_t offset
, int whence
)
1290 struct mdesc_handle
*hp
= file
->private_data
;
1292 return no_seek_end_llseek_size(file
, offset
, whence
, hp
->handle_size
);
1295 /* mdesc_close() - /dev/mdesc is being closed, release the reference to
1298 static int mdesc_close(struct inode
*inode
, struct file
*file
)
1300 mdesc_release(file
->private_data
);
1304 static const struct file_operations mdesc_fops
= {
1307 .llseek
= mdesc_llseek
,
1308 .release
= mdesc_close
,
1309 .owner
= THIS_MODULE
,
1312 static struct miscdevice mdesc_misc
= {
1313 .minor
= MISC_DYNAMIC_MINOR
,
1315 .fops
= &mdesc_fops
,
1318 static int __init
mdesc_misc_init(void)
1320 return misc_register(&mdesc_misc
);
1323 __initcall(mdesc_misc_init
);
1325 void __init
sun4v_mdesc_init(void)
1327 struct mdesc_handle
*hp
;
1328 unsigned long len
, real_len
, status
;
1330 (void) sun4v_mach_desc(0UL, 0UL, &len
);
1332 printk("MDESC: Size is %lu bytes.\n", len
);
1334 hp
= mdesc_alloc(len
, &memblock_mdesc_ops
);
1336 prom_printf("MDESC: alloc of %lu bytes failed.\n", len
);
1340 status
= sun4v_mach_desc(__pa(&hp
->mdesc
), len
, &real_len
);
1341 if (status
!= HV_EOK
|| real_len
> len
) {
1342 prom_printf("sun4v_mach_desc fails, err(%lu), "
1343 "len(%lu), real_len(%lu)\n",
1344 status
, len
, real_len
);
1352 report_platform_properties();