Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* mdesc.c: Sun4V machine description handling.
0003  *
0004  * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
0005  */
0006 #include <linux/kernel.h>
0007 #include <linux/types.h>
0008 #include <linux/log2.h>
0009 #include <linux/list.h>
0010 #include <linux/slab.h>
0011 #include <linux/mm.h>
0012 #include <linux/miscdevice.h>
0013 #include <linux/memblock.h>
0014 #include <linux/export.h>
0015 #include <linux/refcount.h>
0016 
0017 #include <asm/cpudata.h>
0018 #include <asm/hypervisor.h>
0019 #include <asm/mdesc.h>
0020 #include <asm/prom.h>
0021 #include <linux/uaccess.h>
0022 #include <asm/oplib.h>
0023 #include <asm/smp.h>
0024 #include <asm/adi.h>
0025 
0026 /* Unlike the OBP device tree, the machine description is a full-on
0027  * DAG.  An arbitrary number of ARCs are possible from one
0028  * node to other nodes and thus we can't use the OBP device_node
0029  * data structure to represent these nodes inside of the kernel.
0030  *
0031  * Actually, it isn't even a DAG, because there are back pointers
0032  * which create cycles in the graph.
0033  *
0034  * mdesc_hdr and mdesc_elem describe the layout of the data structure
0035  * we get from the Hypervisor.
0036  */
0037 struct mdesc_hdr {
0038     u32 version; /* Transport version */
0039     u32 node_sz; /* node block size */
0040     u32 name_sz; /* name block size */
0041     u32 data_sz; /* data block size */
0042     char    data[];
0043 } __attribute__((aligned(16)));
0044 
0045 struct mdesc_elem {
0046     u8  tag;
0047 #define MD_LIST_END 0x00
0048 #define MD_NODE     0x4e
0049 #define MD_NODE_END 0x45
0050 #define MD_NOOP     0x20
0051 #define MD_PROP_ARC 0x61
0052 #define MD_PROP_VAL 0x76
0053 #define MD_PROP_STR 0x73
0054 #define MD_PROP_DATA    0x64
0055     u8  name_len;
0056     u16 resv;
0057     u32 name_offset;
0058     union {
0059         struct {
0060             u32 data_len;
0061             u32 data_offset;
0062         } data;
0063         u64 val;
0064     } d;
0065 };
0066 
0067 struct mdesc_mem_ops {
0068     struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
0069     void (*free)(struct mdesc_handle *handle);
0070 };
0071 
0072 struct mdesc_handle {
0073     struct list_head    list;
0074     struct mdesc_mem_ops    *mops;
0075     void            *self_base;
0076     refcount_t      refcnt;
0077     unsigned int        handle_size;
0078     struct mdesc_hdr    mdesc;
0079 };
0080 
0081 typedef int (*mdesc_node_info_get_f)(struct mdesc_handle *, u64,
0082                      union md_node_info *);
0083 typedef void (*mdesc_node_info_rel_f)(union md_node_info *);
0084 typedef bool (*mdesc_node_match_f)(union md_node_info *, union md_node_info *);
0085 
0086 struct md_node_ops {
0087     char            *name;
0088     mdesc_node_info_get_f   get_info;
0089     mdesc_node_info_rel_f   rel_info;
0090     mdesc_node_match_f  node_match;
0091 };
0092 
0093 static int get_vdev_port_node_info(struct mdesc_handle *md, u64 node,
0094                    union md_node_info *node_info);
0095 static void rel_vdev_port_node_info(union md_node_info *node_info);
0096 static bool vdev_port_node_match(union md_node_info *a_node_info,
0097                  union md_node_info *b_node_info);
0098 
0099 static int get_ds_port_node_info(struct mdesc_handle *md, u64 node,
0100                  union md_node_info *node_info);
0101 static void rel_ds_port_node_info(union md_node_info *node_info);
0102 static bool ds_port_node_match(union md_node_info *a_node_info,
0103                    union md_node_info *b_node_info);
0104 
0105 /* supported node types which can be registered */
0106 static struct md_node_ops md_node_ops_table[] = {
0107     {"virtual-device-port", get_vdev_port_node_info,
0108      rel_vdev_port_node_info, vdev_port_node_match},
0109     {"domain-services-port", get_ds_port_node_info,
0110      rel_ds_port_node_info, ds_port_node_match},
0111     {NULL, NULL, NULL, NULL}
0112 };
0113 
0114 static void mdesc_get_node_ops(const char *node_name,
0115                    mdesc_node_info_get_f *get_info_f,
0116                    mdesc_node_info_rel_f *rel_info_f,
0117                    mdesc_node_match_f *match_f)
0118 {
0119     int i;
0120 
0121     if (get_info_f)
0122         *get_info_f = NULL;
0123 
0124     if (rel_info_f)
0125         *rel_info_f = NULL;
0126 
0127     if (match_f)
0128         *match_f = NULL;
0129 
0130     if (!node_name)
0131         return;
0132 
0133     for (i = 0; md_node_ops_table[i].name != NULL; i++) {
0134         if (strcmp(md_node_ops_table[i].name, node_name) == 0) {
0135             if (get_info_f)
0136                 *get_info_f = md_node_ops_table[i].get_info;
0137 
0138             if (rel_info_f)
0139                 *rel_info_f = md_node_ops_table[i].rel_info;
0140 
0141             if (match_f)
0142                 *match_f = md_node_ops_table[i].node_match;
0143 
0144             break;
0145         }
0146     }
0147 }
0148 
0149 static void mdesc_handle_init(struct mdesc_handle *hp,
0150                   unsigned int handle_size,
0151                   void *base)
0152 {
0153     BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));
0154 
0155     memset(hp, 0, handle_size);
0156     INIT_LIST_HEAD(&hp->list);
0157     hp->self_base = base;
0158     refcount_set(&hp->refcnt, 1);
0159     hp->handle_size = handle_size;
0160 }
0161 
0162 static struct mdesc_handle * __init mdesc_memblock_alloc(unsigned int mdesc_size)
0163 {
0164     unsigned int handle_size, alloc_size;
0165     struct mdesc_handle *hp;
0166     unsigned long paddr;
0167 
0168     handle_size = (sizeof(struct mdesc_handle) -
0169                sizeof(struct mdesc_hdr) +
0170                mdesc_size);
0171     alloc_size = PAGE_ALIGN(handle_size);
0172 
0173     paddr = memblock_phys_alloc(alloc_size, PAGE_SIZE);
0174 
0175     hp = NULL;
0176     if (paddr) {
0177         hp = __va(paddr);
0178         mdesc_handle_init(hp, handle_size, hp);
0179     }
0180     return hp;
0181 }
0182 
0183 static void __init mdesc_memblock_free(struct mdesc_handle *hp)
0184 {
0185     unsigned int alloc_size;
0186     unsigned long start;
0187 
0188     BUG_ON(refcount_read(&hp->refcnt) != 0);
0189     BUG_ON(!list_empty(&hp->list));
0190 
0191     alloc_size = PAGE_ALIGN(hp->handle_size);
0192     start = __pa(hp);
0193     memblock_free_late(start, alloc_size);
0194 }
0195 
0196 static struct mdesc_mem_ops memblock_mdesc_ops = {
0197     .alloc = mdesc_memblock_alloc,
0198     .free  = mdesc_memblock_free,
0199 };
0200 
0201 static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
0202 {
0203     unsigned int handle_size;
0204     struct mdesc_handle *hp;
0205     unsigned long addr;
0206     void *base;
0207 
0208     handle_size = (sizeof(struct mdesc_handle) -
0209                sizeof(struct mdesc_hdr) +
0210                mdesc_size);
0211     base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
0212     if (!base)
0213         return NULL;
0214 
0215     addr = (unsigned long)base;
0216     addr = (addr + 15UL) & ~15UL;
0217     hp = (struct mdesc_handle *) addr;
0218 
0219     mdesc_handle_init(hp, handle_size, base);
0220 
0221     return hp;
0222 }
0223 
0224 static void mdesc_kfree(struct mdesc_handle *hp)
0225 {
0226     BUG_ON(refcount_read(&hp->refcnt) != 0);
0227     BUG_ON(!list_empty(&hp->list));
0228 
0229     kfree(hp->self_base);
0230 }
0231 
0232 static struct mdesc_mem_ops kmalloc_mdesc_memops = {
0233     .alloc = mdesc_kmalloc,
0234     .free  = mdesc_kfree,
0235 };
0236 
0237 static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
0238                     struct mdesc_mem_ops *mops)
0239 {
0240     struct mdesc_handle *hp = mops->alloc(mdesc_size);
0241 
0242     if (hp)
0243         hp->mops = mops;
0244 
0245     return hp;
0246 }
0247 
0248 static void mdesc_free(struct mdesc_handle *hp)
0249 {
0250     hp->mops->free(hp);
0251 }
0252 
0253 static struct mdesc_handle *cur_mdesc;
0254 static LIST_HEAD(mdesc_zombie_list);
0255 static DEFINE_SPINLOCK(mdesc_lock);
0256 
0257 struct mdesc_handle *mdesc_grab(void)
0258 {
0259     struct mdesc_handle *hp;
0260     unsigned long flags;
0261 
0262     spin_lock_irqsave(&mdesc_lock, flags);
0263     hp = cur_mdesc;
0264     if (hp)
0265         refcount_inc(&hp->refcnt);
0266     spin_unlock_irqrestore(&mdesc_lock, flags);
0267 
0268     return hp;
0269 }
0270 EXPORT_SYMBOL(mdesc_grab);
0271 
0272 void mdesc_release(struct mdesc_handle *hp)
0273 {
0274     unsigned long flags;
0275 
0276     spin_lock_irqsave(&mdesc_lock, flags);
0277     if (refcount_dec_and_test(&hp->refcnt)) {
0278         list_del_init(&hp->list);
0279         hp->mops->free(hp);
0280     }
0281     spin_unlock_irqrestore(&mdesc_lock, flags);
0282 }
0283 EXPORT_SYMBOL(mdesc_release);
0284 
0285 static DEFINE_MUTEX(mdesc_mutex);
0286 static struct mdesc_notifier_client *client_list;
0287 
0288 void mdesc_register_notifier(struct mdesc_notifier_client *client)
0289 {
0290     bool supported = false;
0291     u64 node;
0292     int i;
0293 
0294     mutex_lock(&mdesc_mutex);
0295 
0296     /* check to see if the node is supported for registration */
0297     for (i = 0; md_node_ops_table[i].name != NULL; i++) {
0298         if (strcmp(md_node_ops_table[i].name, client->node_name) == 0) {
0299             supported = true;
0300             break;
0301         }
0302     }
0303 
0304     if (!supported) {
0305         pr_err("MD: %s node not supported\n", client->node_name);
0306         mutex_unlock(&mdesc_mutex);
0307         return;
0308     }
0309 
0310     client->next = client_list;
0311     client_list = client;
0312 
0313     mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
0314         client->add(cur_mdesc, node, client->node_name);
0315 
0316     mutex_unlock(&mdesc_mutex);
0317 }
0318 
0319 static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
0320 {
0321     const u64 *id;
0322     u64 a;
0323 
0324     id = NULL;
0325     mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
0326         u64 target;
0327 
0328         target = mdesc_arc_target(hp, a);
0329         id = mdesc_get_property(hp, target,
0330                     "cfg-handle", NULL);
0331         if (id)
0332             break;
0333     }
0334 
0335     return id;
0336 }
0337 
0338 static int get_vdev_port_node_info(struct mdesc_handle *md, u64 node,
0339                    union md_node_info *node_info)
0340 {
0341     const u64 *parent_cfg_hdlp;
0342     const char *name;
0343     const u64 *idp;
0344 
0345     /*
0346      * Virtual device nodes are distinguished by:
0347      * 1. "id" property
0348      * 2. "name" property
0349      * 3. parent node "cfg-handle" property
0350      */
0351     idp = mdesc_get_property(md, node, "id", NULL);
0352     name = mdesc_get_property(md, node, "name", NULL);
0353     parent_cfg_hdlp = parent_cfg_handle(md, node);
0354 
0355     if (!idp || !name || !parent_cfg_hdlp)
0356         return -1;
0357 
0358     node_info->vdev_port.id = *idp;
0359     node_info->vdev_port.name = kstrdup_const(name, GFP_KERNEL);
0360     if (!node_info->vdev_port.name)
0361         return -1;
0362     node_info->vdev_port.parent_cfg_hdl = *parent_cfg_hdlp;
0363 
0364     return 0;
0365 }
0366 
0367 static void rel_vdev_port_node_info(union md_node_info *node_info)
0368 {
0369     if (node_info && node_info->vdev_port.name) {
0370         kfree_const(node_info->vdev_port.name);
0371         node_info->vdev_port.name = NULL;
0372     }
0373 }
0374 
0375 static bool vdev_port_node_match(union md_node_info *a_node_info,
0376                  union md_node_info *b_node_info)
0377 {
0378     if (a_node_info->vdev_port.id != b_node_info->vdev_port.id)
0379         return false;
0380 
0381     if (a_node_info->vdev_port.parent_cfg_hdl !=
0382         b_node_info->vdev_port.parent_cfg_hdl)
0383         return false;
0384 
0385     if (strncmp(a_node_info->vdev_port.name,
0386             b_node_info->vdev_port.name, MDESC_MAX_STR_LEN) != 0)
0387         return false;
0388 
0389     return true;
0390 }
0391 
0392 static int get_ds_port_node_info(struct mdesc_handle *md, u64 node,
0393                  union md_node_info *node_info)
0394 {
0395     const u64 *idp;
0396 
0397     /* DS port nodes use the "id" property to distinguish them */
0398     idp = mdesc_get_property(md, node, "id", NULL);
0399     if (!idp)
0400         return -1;
0401 
0402     node_info->ds_port.id = *idp;
0403 
0404     return 0;
0405 }
0406 
0407 static void rel_ds_port_node_info(union md_node_info *node_info)
0408 {
0409 }
0410 
0411 static bool ds_port_node_match(union md_node_info *a_node_info,
0412                    union md_node_info *b_node_info)
0413 {
0414     if (a_node_info->ds_port.id != b_node_info->ds_port.id)
0415         return false;
0416 
0417     return true;
0418 }
0419 
0420 /* Run 'func' on nodes which are in A but not in B.  */
0421 static void invoke_on_missing(const char *name,
0422                   struct mdesc_handle *a,
0423                   struct mdesc_handle *b,
0424                   void (*func)(struct mdesc_handle *, u64,
0425                        const char *node_name))
0426 {
0427     mdesc_node_info_get_f get_info_func;
0428     mdesc_node_info_rel_f rel_info_func;
0429     mdesc_node_match_f node_match_func;
0430     union md_node_info a_node_info;
0431     union md_node_info b_node_info;
0432     bool found;
0433     u64 a_node;
0434     u64 b_node;
0435     int rv;
0436 
0437     /*
0438      * Find the get_info, rel_info and node_match ops for the given
0439      * node name
0440      */
0441     mdesc_get_node_ops(name, &get_info_func, &rel_info_func,
0442                &node_match_func);
0443 
0444     /* If we didn't find a match, the node type is not supported */
0445     if (!get_info_func || !rel_info_func || !node_match_func) {
0446         pr_err("MD: %s node type is not supported\n", name);
0447         return;
0448     }
0449 
0450     mdesc_for_each_node_by_name(a, a_node, name) {
0451         found = false;
0452 
0453         rv = get_info_func(a, a_node, &a_node_info);
0454         if (rv != 0) {
0455             pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
0456                    name);
0457             continue;
0458         }
0459 
0460         /* Check each node in B for node matching a_node */
0461         mdesc_for_each_node_by_name(b, b_node, name) {
0462             rv = get_info_func(b, b_node, &b_node_info);
0463             if (rv != 0)
0464                 continue;
0465 
0466             if (node_match_func(&a_node_info, &b_node_info)) {
0467                 found = true;
0468                 rel_info_func(&b_node_info);
0469                 break;
0470             }
0471 
0472             rel_info_func(&b_node_info);
0473         }
0474 
0475         rel_info_func(&a_node_info);
0476 
0477         if (!found)
0478             func(a, a_node, name);
0479     }
0480 }
0481 
0482 static void notify_one(struct mdesc_notifier_client *p,
0483                struct mdesc_handle *old_hp,
0484                struct mdesc_handle *new_hp)
0485 {
0486     invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
0487     invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
0488 }
0489 
0490 static void mdesc_notify_clients(struct mdesc_handle *old_hp,
0491                  struct mdesc_handle *new_hp)
0492 {
0493     struct mdesc_notifier_client *p = client_list;
0494 
0495     while (p) {
0496         notify_one(p, old_hp, new_hp);
0497         p = p->next;
0498     }
0499 }
0500 
0501 void mdesc_update(void)
0502 {
0503     unsigned long len, real_len, status;
0504     struct mdesc_handle *hp, *orig_hp;
0505     unsigned long flags;
0506 
0507     mutex_lock(&mdesc_mutex);
0508 
0509     (void) sun4v_mach_desc(0UL, 0UL, &len);
0510 
0511     hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
0512     if (!hp) {
0513         printk(KERN_ERR "MD: mdesc alloc fails\n");
0514         goto out;
0515     }
0516 
0517     status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
0518     if (status != HV_EOK || real_len > len) {
0519         printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
0520                status);
0521         refcount_dec(&hp->refcnt);
0522         mdesc_free(hp);
0523         goto out;
0524     }
0525 
0526     spin_lock_irqsave(&mdesc_lock, flags);
0527     orig_hp = cur_mdesc;
0528     cur_mdesc = hp;
0529     spin_unlock_irqrestore(&mdesc_lock, flags);
0530 
0531     mdesc_notify_clients(orig_hp, hp);
0532 
0533     spin_lock_irqsave(&mdesc_lock, flags);
0534     if (refcount_dec_and_test(&orig_hp->refcnt))
0535         mdesc_free(orig_hp);
0536     else
0537         list_add(&orig_hp->list, &mdesc_zombie_list);
0538     spin_unlock_irqrestore(&mdesc_lock, flags);
0539 
0540 out:
0541     mutex_unlock(&mdesc_mutex);
0542 }
0543 
0544 u64 mdesc_get_node(struct mdesc_handle *hp, const char *node_name,
0545            union md_node_info *node_info)
0546 {
0547     mdesc_node_info_get_f get_info_func;
0548     mdesc_node_info_rel_f rel_info_func;
0549     mdesc_node_match_f node_match_func;
0550     union md_node_info hp_node_info;
0551     u64 hp_node;
0552     int rv;
0553 
0554     if (hp == NULL || node_name == NULL || node_info == NULL)
0555         return MDESC_NODE_NULL;
0556 
0557     /* Find the ops for the given node name */
0558     mdesc_get_node_ops(node_name, &get_info_func, &rel_info_func,
0559                &node_match_func);
0560 
0561     /* If we didn't find ops for the given node name, it is not supported */
0562     if (!get_info_func || !rel_info_func || !node_match_func) {
0563         pr_err("MD: %s node is not supported\n", node_name);
0564         return -EINVAL;
0565     }
0566 
0567     mdesc_for_each_node_by_name(hp, hp_node, node_name) {
0568         rv = get_info_func(hp, hp_node, &hp_node_info);
0569         if (rv != 0)
0570             continue;
0571 
0572         if (node_match_func(node_info, &hp_node_info))
0573             break;
0574 
0575         rel_info_func(&hp_node_info);
0576     }
0577 
0578     rel_info_func(&hp_node_info);
0579 
0580     return hp_node;
0581 }
0582 EXPORT_SYMBOL(mdesc_get_node);
0583 
0584 int mdesc_get_node_info(struct mdesc_handle *hp, u64 node,
0585             const char *node_name, union md_node_info *node_info)
0586 {
0587     mdesc_node_info_get_f get_info_func;
0588     int rv;
0589 
0590     if (hp == NULL || node == MDESC_NODE_NULL ||
0591         node_name == NULL || node_info == NULL)
0592         return -EINVAL;
0593 
0594     /* Find the get_info op for the given node name */
0595     mdesc_get_node_ops(node_name, &get_info_func, NULL, NULL);
0596 
0597     /* If we didn't find a get_info_func, the node name is not supported */
0598     if (get_info_func == NULL) {
0599         pr_err("MD: %s node is not supported\n", node_name);
0600         return -EINVAL;
0601     }
0602 
0603     rv = get_info_func(hp, node, node_info);
0604     if (rv != 0) {
0605         pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
0606                node_name);
0607         return -1;
0608     }
0609 
0610     return 0;
0611 }
0612 EXPORT_SYMBOL(mdesc_get_node_info);
0613 
0614 static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
0615 {
0616     return (struct mdesc_elem *) mdesc->data;
0617 }
0618 
0619 static void *name_block(struct mdesc_hdr *mdesc)
0620 {
0621     return ((void *) node_block(mdesc)) + mdesc->node_sz;
0622 }
0623 
0624 static void *data_block(struct mdesc_hdr *mdesc)
0625 {
0626     return ((void *) name_block(mdesc)) + mdesc->name_sz;
0627 }
0628 
0629 u64 mdesc_node_by_name(struct mdesc_handle *hp,
0630                u64 from_node, const char *name)
0631 {
0632     struct mdesc_elem *ep = node_block(&hp->mdesc);
0633     const char *names = name_block(&hp->mdesc);
0634     u64 last_node = hp->mdesc.node_sz / 16;
0635     u64 ret;
0636 
0637     if (from_node == MDESC_NODE_NULL) {
0638         ret = from_node = 0;
0639     } else if (from_node >= last_node) {
0640         return MDESC_NODE_NULL;
0641     } else {
0642         ret = ep[from_node].d.val;
0643     }
0644 
0645     while (ret < last_node) {
0646         if (ep[ret].tag != MD_NODE)
0647             return MDESC_NODE_NULL;
0648         if (!strcmp(names + ep[ret].name_offset, name))
0649             break;
0650         ret = ep[ret].d.val;
0651     }
0652     if (ret >= last_node)
0653         ret = MDESC_NODE_NULL;
0654     return ret;
0655 }
0656 EXPORT_SYMBOL(mdesc_node_by_name);
0657 
0658 const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
0659                    const char *name, int *lenp)
0660 {
0661     const char *names = name_block(&hp->mdesc);
0662     u64 last_node = hp->mdesc.node_sz / 16;
0663     void *data = data_block(&hp->mdesc);
0664     struct mdesc_elem *ep;
0665 
0666     if (node == MDESC_NODE_NULL || node >= last_node)
0667         return NULL;
0668 
0669     ep = node_block(&hp->mdesc) + node;
0670     ep++;
0671     for (; ep->tag != MD_NODE_END; ep++) {
0672         void *val = NULL;
0673         int len = 0;
0674 
0675         switch (ep->tag) {
0676         case MD_PROP_VAL:
0677             val = &ep->d.val;
0678             len = 8;
0679             break;
0680 
0681         case MD_PROP_STR:
0682         case MD_PROP_DATA:
0683             val = data + ep->d.data.data_offset;
0684             len = ep->d.data.data_len;
0685             break;
0686 
0687         default:
0688             break;
0689         }
0690         if (!val)
0691             continue;
0692 
0693         if (!strcmp(names + ep->name_offset, name)) {
0694             if (lenp)
0695                 *lenp = len;
0696             return val;
0697         }
0698     }
0699 
0700     return NULL;
0701 }
0702 EXPORT_SYMBOL(mdesc_get_property);
0703 
0704 u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
0705 {
0706     struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
0707     const char *names = name_block(&hp->mdesc);
0708     u64 last_node = hp->mdesc.node_sz / 16;
0709 
0710     if (from == MDESC_NODE_NULL || from >= last_node)
0711         return MDESC_NODE_NULL;
0712 
0713     ep = base + from;
0714 
0715     ep++;
0716     for (; ep->tag != MD_NODE_END; ep++) {
0717         if (ep->tag != MD_PROP_ARC)
0718             continue;
0719 
0720         if (strcmp(names + ep->name_offset, arc_type))
0721             continue;
0722 
0723         return ep - base;
0724     }
0725 
0726     return MDESC_NODE_NULL;
0727 }
0728 EXPORT_SYMBOL(mdesc_next_arc);
0729 
0730 u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
0731 {
0732     struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
0733 
0734     ep = base + arc;
0735 
0736     return ep->d.val;
0737 }
0738 EXPORT_SYMBOL(mdesc_arc_target);
0739 
0740 const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
0741 {
0742     struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
0743     const char *names = name_block(&hp->mdesc);
0744     u64 last_node = hp->mdesc.node_sz / 16;
0745 
0746     if (node == MDESC_NODE_NULL || node >= last_node)
0747         return NULL;
0748 
0749     ep = base + node;
0750     if (ep->tag != MD_NODE)
0751         return NULL;
0752 
0753     return names + ep->name_offset;
0754 }
0755 EXPORT_SYMBOL(mdesc_node_name);
0756 
0757 static u64 max_cpus = 64;
0758 
0759 static void __init report_platform_properties(void)
0760 {
0761     struct mdesc_handle *hp = mdesc_grab();
0762     u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
0763     const char *s;
0764     const u64 *v;
0765 
0766     if (pn == MDESC_NODE_NULL) {
0767         prom_printf("No platform node in machine-description.\n");
0768         prom_halt();
0769     }
0770 
0771     s = mdesc_get_property(hp, pn, "banner-name", NULL);
0772     printk("PLATFORM: banner-name [%s]\n", s);
0773     s = mdesc_get_property(hp, pn, "name", NULL);
0774     printk("PLATFORM: name [%s]\n", s);
0775 
0776     v = mdesc_get_property(hp, pn, "hostid", NULL);
0777     if (v)
0778         printk("PLATFORM: hostid [%08llx]\n", *v);
0779     v = mdesc_get_property(hp, pn, "serial#", NULL);
0780     if (v)
0781         printk("PLATFORM: serial# [%08llx]\n", *v);
0782     v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
0783     printk("PLATFORM: stick-frequency [%08llx]\n", *v);
0784     v = mdesc_get_property(hp, pn, "mac-address", NULL);
0785     if (v)
0786         printk("PLATFORM: mac-address [%llx]\n", *v);
0787     v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
0788     if (v)
0789         printk("PLATFORM: watchdog-resolution [%llu ms]\n", *v);
0790     v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
0791     if (v)
0792         printk("PLATFORM: watchdog-max-timeout [%llu ms]\n", *v);
0793     v = mdesc_get_property(hp, pn, "max-cpus", NULL);
0794     if (v) {
0795         max_cpus = *v;
0796         printk("PLATFORM: max-cpus [%llu]\n", max_cpus);
0797     }
0798 
0799 #ifdef CONFIG_SMP
0800     {
0801         int max_cpu, i;
0802 
0803         if (v) {
0804             max_cpu = *v;
0805             if (max_cpu > NR_CPUS)
0806                 max_cpu = NR_CPUS;
0807         } else {
0808             max_cpu = NR_CPUS;
0809         }
0810         for (i = 0; i < max_cpu; i++)
0811             set_cpu_possible(i, true);
0812     }
0813 #endif
0814 
0815     mdesc_release(hp);
0816 }
0817 
0818 static void fill_in_one_cache(cpuinfo_sparc *c, struct mdesc_handle *hp, u64 mp)
0819 {
0820     const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
0821     const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
0822     const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
0823     const char *type;
0824     int type_len;
0825 
0826     type = mdesc_get_property(hp, mp, "type", &type_len);
0827 
0828     switch (*level) {
0829     case 1:
0830         if (of_find_in_proplist(type, "instn", type_len)) {
0831             c->icache_size = *size;
0832             c->icache_line_size = *line_size;
0833         } else if (of_find_in_proplist(type, "data", type_len)) {
0834             c->dcache_size = *size;
0835             c->dcache_line_size = *line_size;
0836         }
0837         break;
0838 
0839     case 2:
0840         c->ecache_size = *size;
0841         c->ecache_line_size = *line_size;
0842         break;
0843 
0844     default:
0845         break;
0846     }
0847 
0848     if (*level == 1) {
0849         u64 a;
0850 
0851         mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
0852             u64 target = mdesc_arc_target(hp, a);
0853             const char *name = mdesc_node_name(hp, target);
0854 
0855             if (!strcmp(name, "cache"))
0856                 fill_in_one_cache(c, hp, target);
0857         }
0858     }
0859 }
0860 
0861 static void find_back_node_value(struct mdesc_handle *hp, u64 node,
0862                  char *srch_val,
0863                  void (*func)(struct mdesc_handle *, u64, int),
0864                  u64 val, int depth)
0865 {
0866     u64 arc;
0867 
0868     /* Since we have an estimate of recursion depth, do a sanity check. */
0869     if (depth == 0)
0870         return;
0871 
0872     mdesc_for_each_arc(arc, hp, node, MDESC_ARC_TYPE_BACK) {
0873         u64 n = mdesc_arc_target(hp, arc);
0874         const char *name = mdesc_node_name(hp, n);
0875 
0876         if (!strcmp(srch_val, name))
0877             (*func)(hp, n, val);
0878 
0879         find_back_node_value(hp, n, srch_val, func, val, depth-1);
0880     }
0881 }
0882 
0883 static void __mark_core_id(struct mdesc_handle *hp, u64 node,
0884                int core_id)
0885 {
0886     const u64 *id = mdesc_get_property(hp, node, "id", NULL);
0887 
0888     if (*id < num_possible_cpus())
0889         cpu_data(*id).core_id = core_id;
0890 }
0891 
0892 static void __mark_max_cache_id(struct mdesc_handle *hp, u64 node,
0893                 int max_cache_id)
0894 {
0895     const u64 *id = mdesc_get_property(hp, node, "id", NULL);
0896 
0897     if (*id < num_possible_cpus()) {
0898         cpu_data(*id).max_cache_id = max_cache_id;
0899 
0900         /**
0901          * On systems without explicit socket descriptions socket
0902          * is max_cache_id
0903          */
0904         cpu_data(*id).sock_id = max_cache_id;
0905     }
0906 }
0907 
0908 static void mark_core_ids(struct mdesc_handle *hp, u64 mp,
0909               int core_id)
0910 {
0911     find_back_node_value(hp, mp, "cpu", __mark_core_id, core_id, 10);
0912 }
0913 
0914 static void mark_max_cache_ids(struct mdesc_handle *hp, u64 mp,
0915                    int max_cache_id)
0916 {
0917     find_back_node_value(hp, mp, "cpu", __mark_max_cache_id,
0918                  max_cache_id, 10);
0919 }
0920 
0921 static void set_core_ids(struct mdesc_handle *hp)
0922 {
0923     int idx;
0924     u64 mp;
0925 
0926     idx = 1;
0927 
0928     /* Identify unique cores by looking for cpus backpointed to by
0929      * level 1 instruction caches.
0930      */
0931     mdesc_for_each_node_by_name(hp, mp, "cache") {
0932         const u64 *level;
0933         const char *type;
0934         int len;
0935 
0936         level = mdesc_get_property(hp, mp, "level", NULL);
0937         if (*level != 1)
0938             continue;
0939 
0940         type = mdesc_get_property(hp, mp, "type", &len);
0941         if (!of_find_in_proplist(type, "instn", len))
0942             continue;
0943 
0944         mark_core_ids(hp, mp, idx);
0945         idx++;
0946     }
0947 }
0948 
0949 static int set_max_cache_ids_by_cache(struct mdesc_handle *hp, int level)
0950 {
0951     u64 mp;
0952     int idx = 1;
0953     int fnd = 0;
0954 
0955     /**
0956      * Identify unique highest level of shared cache by looking for cpus
0957      * backpointed to by shared level N caches.
0958      */
0959     mdesc_for_each_node_by_name(hp, mp, "cache") {
0960         const u64 *cur_lvl;
0961 
0962         cur_lvl = mdesc_get_property(hp, mp, "level", NULL);
0963         if (*cur_lvl != level)
0964             continue;
0965         mark_max_cache_ids(hp, mp, idx);
0966         idx++;
0967         fnd = 1;
0968     }
0969     return fnd;
0970 }
0971 
0972 static void set_sock_ids_by_socket(struct mdesc_handle *hp, u64 mp)
0973 {
0974     int idx = 1;
0975 
0976     mdesc_for_each_node_by_name(hp, mp, "socket") {
0977         u64 a;
0978 
0979         mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
0980             u64 t = mdesc_arc_target(hp, a);
0981             const char *name;
0982             const u64 *id;
0983 
0984             name = mdesc_node_name(hp, t);
0985             if (strcmp(name, "cpu"))
0986                 continue;
0987 
0988             id = mdesc_get_property(hp, t, "id", NULL);
0989             if (*id < num_possible_cpus())
0990                 cpu_data(*id).sock_id = idx;
0991         }
0992         idx++;
0993     }
0994 }
0995 
0996 static void set_sock_ids(struct mdesc_handle *hp)
0997 {
0998     u64 mp;
0999 
1000     /**
1001      * Find the highest level of shared cache which pre-T7 is also
1002      * the socket.
1003      */
1004     if (!set_max_cache_ids_by_cache(hp, 3))
1005         set_max_cache_ids_by_cache(hp, 2);
1006 
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);
1011 }
1012 
1013 static void mark_proc_ids(struct mdesc_handle *hp, u64 mp, int proc_id)
1014 {
1015     u64 a;
1016 
1017     mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
1018         u64 t = mdesc_arc_target(hp, a);
1019         const char *name;
1020         const u64 *id;
1021 
1022         name = mdesc_node_name(hp, t);
1023         if (strcmp(name, "cpu"))
1024             continue;
1025 
1026         id = mdesc_get_property(hp, t, "id", NULL);
1027         if (*id < NR_CPUS)
1028             cpu_data(*id).proc_id = proc_id;
1029     }
1030 }
1031 
1032 static void __set_proc_ids(struct mdesc_handle *hp, const char *exec_unit_name)
1033 {
1034     int idx;
1035     u64 mp;
1036 
1037     idx = 0;
1038     mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
1039         const char *type;
1040         int len;
1041 
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))
1045             continue;
1046 
1047         mark_proc_ids(hp, mp, idx);
1048         idx++;
1049     }
1050 }
1051 
1052 static void set_proc_ids(struct mdesc_handle *hp)
1053 {
1054     __set_proc_ids(hp, "exec_unit");
1055     __set_proc_ids(hp, "exec-unit");
1056 }
1057 
1058 static void get_one_mondo_bits(const u64 *p, unsigned int *mask,
1059                    unsigned long def, unsigned long max)
1060 {
1061     u64 val;
1062 
1063     if (!p)
1064         goto use_default;
1065     val = *p;
1066 
1067     if (!val || val >= 64)
1068         goto use_default;
1069 
1070     if (val > max)
1071         val = max;
1072 
1073     *mask = ((1U << val) * 64U) - 1U;
1074     return;
1075 
1076 use_default:
1077     *mask = ((1U << def) * 64U) - 1U;
1078 }
1079 
1080 static void get_mondo_data(struct mdesc_handle *hp, u64 mp,
1081                struct trap_per_cpu *tb)
1082 {
1083     static int printed;
1084     const u64 *val;
1085 
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));
1088 
1089     val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
1090     get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7, 8);
1091 
1092     val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
1093     get_one_mondo_bits(val, &tb->resum_qmask, 6, 7);
1094 
1095     val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
1096     get_one_mondo_bits(val, &tb->nonresum_qmask, 2, 2);
1097     if (!printed++) {
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);
1104     }
1105 }
1106 
1107 static void *mdesc_iterate_over_cpus(void *(*func)(struct mdesc_handle *, u64, int, void *), void *arg, cpumask_t *mask)
1108 {
1109     struct mdesc_handle *hp = mdesc_grab();
1110     void *ret = NULL;
1111     u64 mp;
1112 
1113     mdesc_for_each_node_by_name(hp, mp, "cpu") {
1114         const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
1115         int cpuid = *id;
1116 
1117 #ifdef CONFIG_SMP
1118         if (cpuid >= NR_CPUS) {
1119             printk(KERN_WARNING "Ignoring CPU %d which is "
1120                    ">= NR_CPUS (%d)\n",
1121                    cpuid, NR_CPUS);
1122             continue;
1123         }
1124         if (!cpumask_test_cpu(cpuid, mask))
1125             continue;
1126 #endif
1127 
1128         ret = func(hp, mp, cpuid, arg);
1129         if (ret)
1130             goto out;
1131     }
1132 out:
1133     mdesc_release(hp);
1134     return ret;
1135 }
1136 
1137 static void *record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
1138                 void *arg)
1139 {
1140     ncpus_probed++;
1141 #ifdef CONFIG_SMP
1142     set_cpu_present(cpuid, true);
1143 #endif
1144     return NULL;
1145 }
1146 
1147 void mdesc_populate_present_mask(cpumask_t *mask)
1148 {
1149     if (tlb_type != hypervisor)
1150         return;
1151 
1152     ncpus_probed = 0;
1153     mdesc_iterate_over_cpus(record_one_cpu, NULL, mask);
1154 }
1155 
1156 static void * __init check_one_pgsz(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
1157 {
1158     const u64 *pgsz_prop = mdesc_get_property(hp, mp, "mmu-page-size-list", NULL);
1159     unsigned long *pgsz_mask = arg;
1160     u64 val;
1161 
1162     val = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
1163            HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
1164     if (pgsz_prop)
1165         val = *pgsz_prop;
1166 
1167     if (!*pgsz_mask)
1168         *pgsz_mask = val;
1169     else
1170         *pgsz_mask &= val;
1171     return NULL;
1172 }
1173 
1174 void __init mdesc_get_page_sizes(cpumask_t *mask, unsigned long *pgsz_mask)
1175 {
1176     *pgsz_mask = 0;
1177     mdesc_iterate_over_cpus(check_one_pgsz, pgsz_mask, mask);
1178 }
1179 
1180 static void *fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
1181                  void *arg)
1182 {
1183     const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
1184     struct trap_per_cpu *tb;
1185     cpuinfo_sparc *c;
1186     u64 a;
1187 
1188 #ifndef CONFIG_SMP
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.
1192      */
1193     if (cpuid != real_hard_smp_processor_id())
1194         return NULL;
1195     cpuid = 0;
1196 #endif
1197 
1198     c = &cpu_data(cpuid);
1199     c->clock_tick = *cfreq;
1200 
1201     tb = &trap_block[cpuid];
1202     get_mondo_data(hp, mp, tb);
1203 
1204     mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
1205         u64 j, t = mdesc_arc_target(hp, a);
1206         const char *t_name;
1207 
1208         t_name = mdesc_node_name(hp, t);
1209         if (!strcmp(t_name, "cache")) {
1210             fill_in_one_cache(c, hp, t);
1211             continue;
1212         }
1213 
1214         mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
1215             u64 n = mdesc_arc_target(hp, j);
1216             const char *n_name;
1217 
1218             n_name = mdesc_node_name(hp, n);
1219             if (!strcmp(n_name, "cache"))
1220                 fill_in_one_cache(c, hp, n);
1221         }
1222     }
1223 
1224     c->core_id = 0;
1225     c->proc_id = -1;
1226 
1227     return NULL;
1228 }
1229 
1230 void mdesc_fill_in_cpu_data(cpumask_t *mask)
1231 {
1232     struct mdesc_handle *hp;
1233 
1234     mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask);
1235 
1236     hp = mdesc_grab();
1237 
1238     set_core_ids(hp);
1239     set_proc_ids(hp);
1240     set_sock_ids(hp);
1241 
1242     mdesc_release(hp);
1243 
1244     smp_fill_in_sib_core_maps();
1245 }
1246 
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
1251  */
1252 static int mdesc_open(struct inode *inode, struct file *file)
1253 {
1254     struct mdesc_handle *hp = mdesc_grab();
1255 
1256     if (!hp)
1257         return -ENODEV;
1258 
1259     file->private_data = hp;
1260 
1261     return 0;
1262 }
1263 
1264 static ssize_t mdesc_read(struct file *file, char __user *buf,
1265               size_t len, loff_t *offp)
1266 {
1267     struct mdesc_handle *hp = file->private_data;
1268     unsigned char *mdesc;
1269     int bytes_left, count = len;
1270 
1271     if (*offp >= hp->handle_size)
1272         return 0;
1273 
1274     bytes_left = hp->handle_size - *offp;
1275     if (count > bytes_left)
1276         count = bytes_left;
1277 
1278     mdesc = (unsigned char *)&hp->mdesc;
1279     mdesc += *offp;
1280     if (!copy_to_user(buf, mdesc, count)) {
1281         *offp += count;
1282         return count;
1283     } else {
1284         return -EFAULT;
1285     }
1286 }
1287 
1288 static loff_t mdesc_llseek(struct file *file, loff_t offset, int whence)
1289 {
1290     struct mdesc_handle *hp = file->private_data;
1291 
1292     return no_seek_end_llseek_size(file, offset, whence, hp->handle_size);
1293 }
1294 
1295 /* mdesc_close() - /dev/mdesc is being closed, release the reference to
1296  * mdesc structure.
1297  */
1298 static int mdesc_close(struct inode *inode, struct file *file)
1299 {
1300     mdesc_release(file->private_data);
1301     return 0;
1302 }
1303 
1304 static const struct file_operations mdesc_fops = {
1305     .open    = mdesc_open,
1306     .read    = mdesc_read,
1307     .llseek  = mdesc_llseek,
1308     .release = mdesc_close,
1309     .owner   = THIS_MODULE,
1310 };
1311 
1312 static struct miscdevice mdesc_misc = {
1313     .minor  = MISC_DYNAMIC_MINOR,
1314     .name   = "mdesc",
1315     .fops   = &mdesc_fops,
1316 };
1317 
1318 static int __init mdesc_misc_init(void)
1319 {
1320     return misc_register(&mdesc_misc);
1321 }
1322 
1323 __initcall(mdesc_misc_init);
1324 
1325 void __init sun4v_mdesc_init(void)
1326 {
1327     struct mdesc_handle *hp;
1328     unsigned long len, real_len, status;
1329 
1330     (void) sun4v_mach_desc(0UL, 0UL, &len);
1331 
1332     printk("MDESC: Size is %lu bytes.\n", len);
1333 
1334     hp = mdesc_alloc(len, &memblock_mdesc_ops);
1335     if (hp == NULL) {
1336         prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
1337         prom_halt();
1338     }
1339 
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);
1345         mdesc_free(hp);
1346         prom_halt();
1347     }
1348 
1349     cur_mdesc = hp;
1350 
1351     mdesc_adi_init();
1352     report_platform_properties();
1353 }