GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / sparc / kernel / mdesc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* mdesc.c: Sun4V machine description handling.
3  *
4  * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
5  */
6 #include <linux/kernel.h>
7 #include <linux/types.h>
8 #include <linux/memblock.h>
9 #include <linux/log2.h>
10 #include <linux/list.h>
11 #include <linux/slab.h>
12 #include <linux/mm.h>
13 #include <linux/miscdevice.h>
14 #include <linux/bootmem.h>
15 #include <linux/export.h>
16 #include <linux/refcount.h>
17
18 #include <asm/cpudata.h>
19 #include <asm/hypervisor.h>
20 #include <asm/mdesc.h>
21 #include <asm/prom.h>
22 #include <linux/uaccess.h>
23 #include <asm/oplib.h>
24 #include <asm/smp.h>
25 #include <asm/adi.h>
26
27 /* Unlike the OBP device tree, the machine description is a full-on
28  * DAG.  An arbitrary number of ARCs are possible from one
29  * node to other nodes and thus we can't use the OBP device_node
30  * data structure to represent these nodes inside of the kernel.
31  *
32  * Actually, it isn't even a DAG, because there are back pointers
33  * which create cycles in the graph.
34  *
35  * mdesc_hdr and mdesc_elem describe the layout of the data structure
36  * we get from the Hypervisor.
37  */
38 struct mdesc_hdr {
39         u32     version; /* Transport version */
40         u32     node_sz; /* node block size */
41         u32     name_sz; /* name block size */
42         u32     data_sz; /* data block size */
43         char    data[];
44 } __attribute__((aligned(16)));
45
46 struct mdesc_elem {
47         u8      tag;
48 #define MD_LIST_END     0x00
49 #define MD_NODE         0x4e
50 #define MD_NODE_END     0x45
51 #define MD_NOOP         0x20
52 #define MD_PROP_ARC     0x61
53 #define MD_PROP_VAL     0x76
54 #define MD_PROP_STR     0x73
55 #define MD_PROP_DATA    0x64
56         u8      name_len;
57         u16     resv;
58         u32     name_offset;
59         union {
60                 struct {
61                         u32     data_len;
62                         u32     data_offset;
63                 } data;
64                 u64     val;
65         } d;
66 };
67
68 struct mdesc_mem_ops {
69         struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
70         void (*free)(struct mdesc_handle *handle);
71 };
72
73 struct mdesc_handle {
74         struct list_head        list;
75         struct mdesc_mem_ops    *mops;
76         void                    *self_base;
77         refcount_t              refcnt;
78         unsigned int            handle_size;
79         struct mdesc_hdr        mdesc;
80 };
81
82 typedef int (*mdesc_node_info_get_f)(struct mdesc_handle *, u64,
83                                      union md_node_info *);
84 typedef void (*mdesc_node_info_rel_f)(union md_node_info *);
85 typedef bool (*mdesc_node_match_f)(union md_node_info *, union md_node_info *);
86
87 struct md_node_ops {
88         char                    *name;
89         mdesc_node_info_get_f   get_info;
90         mdesc_node_info_rel_f   rel_info;
91         mdesc_node_match_f      node_match;
92 };
93
94 static int get_vdev_port_node_info(struct mdesc_handle *md, u64 node,
95                                    union md_node_info *node_info);
96 static void rel_vdev_port_node_info(union md_node_info *node_info);
97 static bool vdev_port_node_match(union md_node_info *a_node_info,
98                                  union md_node_info *b_node_info);
99
100 static int get_ds_port_node_info(struct mdesc_handle *md, u64 node,
101                                  union md_node_info *node_info);
102 static void rel_ds_port_node_info(union md_node_info *node_info);
103 static bool ds_port_node_match(union md_node_info *a_node_info,
104                                union md_node_info *b_node_info);
105
106 /* supported node types which can be registered */
107 static struct md_node_ops md_node_ops_table[] = {
108         {"virtual-device-port", get_vdev_port_node_info,
109          rel_vdev_port_node_info, vdev_port_node_match},
110         {"domain-services-port", get_ds_port_node_info,
111          rel_ds_port_node_info, ds_port_node_match},
112         {NULL, NULL, NULL, NULL}
113 };
114
115 static void mdesc_get_node_ops(const char *node_name,
116                                mdesc_node_info_get_f *get_info_f,
117                                mdesc_node_info_rel_f *rel_info_f,
118                                mdesc_node_match_f *match_f)
119 {
120         int i;
121
122         if (get_info_f)
123                 *get_info_f = NULL;
124
125         if (rel_info_f)
126                 *rel_info_f = NULL;
127
128         if (match_f)
129                 *match_f = NULL;
130
131         if (!node_name)
132                 return;
133
134         for (i = 0; md_node_ops_table[i].name != NULL; i++) {
135                 if (strcmp(md_node_ops_table[i].name, node_name) == 0) {
136                         if (get_info_f)
137                                 *get_info_f = md_node_ops_table[i].get_info;
138
139                         if (rel_info_f)
140                                 *rel_info_f = md_node_ops_table[i].rel_info;
141
142                         if (match_f)
143                                 *match_f = md_node_ops_table[i].node_match;
144
145                         break;
146                 }
147         }
148 }
149
150 static void mdesc_handle_init(struct mdesc_handle *hp,
151                               unsigned int handle_size,
152                               void *base)
153 {
154         BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));
155
156         memset(hp, 0, handle_size);
157         INIT_LIST_HEAD(&hp->list);
158         hp->self_base = base;
159         refcount_set(&hp->refcnt, 1);
160         hp->handle_size = handle_size;
161 }
162
163 static struct mdesc_handle * __init mdesc_memblock_alloc(unsigned int mdesc_size)
164 {
165         unsigned int handle_size, alloc_size;
166         struct mdesc_handle *hp;
167         unsigned long paddr;
168
169         handle_size = (sizeof(struct mdesc_handle) -
170                        sizeof(struct mdesc_hdr) +
171                        mdesc_size);
172         alloc_size = PAGE_ALIGN(handle_size);
173
174         paddr = memblock_alloc(alloc_size, PAGE_SIZE);
175
176         hp = NULL;
177         if (paddr) {
178                 hp = __va(paddr);
179                 mdesc_handle_init(hp, handle_size, hp);
180         }
181         return hp;
182 }
183
184 static void __init mdesc_memblock_free(struct mdesc_handle *hp)
185 {
186         unsigned int alloc_size;
187         unsigned long start;
188
189         BUG_ON(refcount_read(&hp->refcnt) != 0);
190         BUG_ON(!list_empty(&hp->list));
191
192         alloc_size = PAGE_ALIGN(hp->handle_size);
193         start = __pa(hp);
194         free_bootmem_late(start, alloc_size);
195 }
196
197 static struct mdesc_mem_ops memblock_mdesc_ops = {
198         .alloc = mdesc_memblock_alloc,
199         .free  = mdesc_memblock_free,
200 };
201
202 static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
203 {
204         unsigned int handle_size;
205         struct mdesc_handle *hp;
206         unsigned long addr;
207         void *base;
208
209         handle_size = (sizeof(struct mdesc_handle) -
210                        sizeof(struct mdesc_hdr) +
211                        mdesc_size);
212         base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
213         if (!base)
214                 return NULL;
215
216         addr = (unsigned long)base;
217         addr = (addr + 15UL) & ~15UL;
218         hp = (struct mdesc_handle *) addr;
219
220         mdesc_handle_init(hp, handle_size, base);
221
222         return hp;
223 }
224
225 static void mdesc_kfree(struct mdesc_handle *hp)
226 {
227         BUG_ON(refcount_read(&hp->refcnt) != 0);
228         BUG_ON(!list_empty(&hp->list));
229
230         kfree(hp->self_base);
231 }
232
233 static struct mdesc_mem_ops kmalloc_mdesc_memops = {
234         .alloc = mdesc_kmalloc,
235         .free  = mdesc_kfree,
236 };
237
238 static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
239                                         struct mdesc_mem_ops *mops)
240 {
241         struct mdesc_handle *hp = mops->alloc(mdesc_size);
242
243         if (hp)
244                 hp->mops = mops;
245
246         return hp;
247 }
248
249 static void mdesc_free(struct mdesc_handle *hp)
250 {
251         hp->mops->free(hp);
252 }
253
254 static struct mdesc_handle *cur_mdesc;
255 static LIST_HEAD(mdesc_zombie_list);
256 static DEFINE_SPINLOCK(mdesc_lock);
257
258 struct mdesc_handle *mdesc_grab(void)
259 {
260         struct mdesc_handle *hp;
261         unsigned long flags;
262
263         spin_lock_irqsave(&mdesc_lock, flags);
264         hp = cur_mdesc;
265         if (hp)
266                 refcount_inc(&hp->refcnt);
267         spin_unlock_irqrestore(&mdesc_lock, flags);
268
269         return hp;
270 }
271 EXPORT_SYMBOL(mdesc_grab);
272
273 void mdesc_release(struct mdesc_handle *hp)
274 {
275         unsigned long flags;
276
277         spin_lock_irqsave(&mdesc_lock, flags);
278         if (refcount_dec_and_test(&hp->refcnt)) {
279                 list_del_init(&hp->list);
280                 hp->mops->free(hp);
281         }
282         spin_unlock_irqrestore(&mdesc_lock, flags);
283 }
284 EXPORT_SYMBOL(mdesc_release);
285
286 static DEFINE_MUTEX(mdesc_mutex);
287 static struct mdesc_notifier_client *client_list;
288
289 void mdesc_register_notifier(struct mdesc_notifier_client *client)
290 {
291         bool supported = false;
292         u64 node;
293         int i;
294
295         mutex_lock(&mdesc_mutex);
296
297         /* check to see if the node is supported for registration */
298         for (i = 0; md_node_ops_table[i].name != NULL; i++) {
299                 if (strcmp(md_node_ops_table[i].name, client->node_name) == 0) {
300                         supported = true;
301                         break;
302                 }
303         }
304
305         if (!supported) {
306                 pr_err("MD: %s node not supported\n", client->node_name);
307                 mutex_unlock(&mdesc_mutex);
308                 return;
309         }
310
311         client->next = client_list;
312         client_list = client;
313
314         mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
315                 client->add(cur_mdesc, node, client->node_name);
316
317         mutex_unlock(&mdesc_mutex);
318 }
319
320 static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
321 {
322         const u64 *id;
323         u64 a;
324
325         id = NULL;
326         mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
327                 u64 target;
328
329                 target = mdesc_arc_target(hp, a);
330                 id = mdesc_get_property(hp, target,
331                                         "cfg-handle", NULL);
332                 if (id)
333                         break;
334         }
335
336         return id;
337 }
338
339 static int get_vdev_port_node_info(struct mdesc_handle *md, u64 node,
340                                    union md_node_info *node_info)
341 {
342         const u64 *parent_cfg_hdlp;
343         const char *name;
344         const u64 *idp;
345
346         /*
347          * Virtual device nodes are distinguished by:
348          * 1. "id" property
349          * 2. "name" property
350          * 3. parent node "cfg-handle" property
351          */
352         idp = mdesc_get_property(md, node, "id", NULL);
353         name = mdesc_get_property(md, node, "name", NULL);
354         parent_cfg_hdlp = parent_cfg_handle(md, node);
355
356         if (!idp || !name || !parent_cfg_hdlp)
357                 return -1;
358
359         node_info->vdev_port.id = *idp;
360         node_info->vdev_port.name = kstrdup_const(name, GFP_KERNEL);
361         if (!node_info->vdev_port.name)
362                 return -1;
363         node_info->vdev_port.parent_cfg_hdl = *parent_cfg_hdlp;
364
365         return 0;
366 }
367
368 static void rel_vdev_port_node_info(union md_node_info *node_info)
369 {
370         if (node_info && node_info->vdev_port.name) {
371                 kfree_const(node_info->vdev_port.name);
372                 node_info->vdev_port.name = NULL;
373         }
374 }
375
376 static bool vdev_port_node_match(union md_node_info *a_node_info,
377                                  union md_node_info *b_node_info)
378 {
379         if (a_node_info->vdev_port.id != b_node_info->vdev_port.id)
380                 return false;
381
382         if (a_node_info->vdev_port.parent_cfg_hdl !=
383             b_node_info->vdev_port.parent_cfg_hdl)
384                 return false;
385
386         if (strncmp(a_node_info->vdev_port.name,
387                     b_node_info->vdev_port.name, MDESC_MAX_STR_LEN) != 0)
388                 return false;
389
390         return true;
391 }
392
393 static int get_ds_port_node_info(struct mdesc_handle *md, u64 node,
394                                  union md_node_info *node_info)
395 {
396         const u64 *idp;
397
398         /* DS port nodes use the "id" property to distinguish them */
399         idp = mdesc_get_property(md, node, "id", NULL);
400         if (!idp)
401                 return -1;
402
403         node_info->ds_port.id = *idp;
404
405         return 0;
406 }
407
408 static void rel_ds_port_node_info(union md_node_info *node_info)
409 {
410 }
411
412 static bool ds_port_node_match(union md_node_info *a_node_info,
413                                union md_node_info *b_node_info)
414 {
415         if (a_node_info->ds_port.id != b_node_info->ds_port.id)
416                 return false;
417
418         return true;
419 }
420
421 /* Run 'func' on nodes which are in A but not in B.  */
422 static void invoke_on_missing(const char *name,
423                               struct mdesc_handle *a,
424                               struct mdesc_handle *b,
425                               void (*func)(struct mdesc_handle *, u64,
426                                            const char *node_name))
427 {
428         mdesc_node_info_get_f get_info_func;
429         mdesc_node_info_rel_f rel_info_func;
430         mdesc_node_match_f node_match_func;
431         union md_node_info a_node_info;
432         union md_node_info b_node_info;
433         bool found;
434         u64 a_node;
435         u64 b_node;
436         int rv;
437
438         /*
439          * Find the get_info, rel_info and node_match ops for the given
440          * node name
441          */
442         mdesc_get_node_ops(name, &get_info_func, &rel_info_func,
443                            &node_match_func);
444
445         /* If we didn't find a match, the node type is not supported */
446         if (!get_info_func || !rel_info_func || !node_match_func) {
447                 pr_err("MD: %s node type is not supported\n", name);
448                 return;
449         }
450
451         mdesc_for_each_node_by_name(a, a_node, name) {
452                 found = false;
453
454                 rv = get_info_func(a, a_node, &a_node_info);
455                 if (rv != 0) {
456                         pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
457                                name);
458                         continue;
459                 }
460
461                 /* Check each node in B for node matching a_node */
462                 mdesc_for_each_node_by_name(b, b_node, name) {
463                         rv = get_info_func(b, b_node, &b_node_info);
464                         if (rv != 0)
465                                 continue;
466
467                         if (node_match_func(&a_node_info, &b_node_info)) {
468                                 found = true;
469                                 rel_info_func(&b_node_info);
470                                 break;
471                         }
472
473                         rel_info_func(&b_node_info);
474                 }
475
476                 rel_info_func(&a_node_info);
477
478                 if (!found)
479                         func(a, a_node, name);
480         }
481 }
482
483 static void notify_one(struct mdesc_notifier_client *p,
484                        struct mdesc_handle *old_hp,
485                        struct mdesc_handle *new_hp)
486 {
487         invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
488         invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
489 }
490
491 static void mdesc_notify_clients(struct mdesc_handle *old_hp,
492                                  struct mdesc_handle *new_hp)
493 {
494         struct mdesc_notifier_client *p = client_list;
495
496         while (p) {
497                 notify_one(p, old_hp, new_hp);
498                 p = p->next;
499         }
500 }
501
502 void mdesc_update(void)
503 {
504         unsigned long len, real_len, status;
505         struct mdesc_handle *hp, *orig_hp;
506         unsigned long flags;
507
508         mutex_lock(&mdesc_mutex);
509
510         (void) sun4v_mach_desc(0UL, 0UL, &len);
511
512         hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
513         if (!hp) {
514                 printk(KERN_ERR "MD: mdesc alloc fails\n");
515                 goto out;
516         }
517
518         status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
519         if (status != HV_EOK || real_len > len) {
520                 printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
521                        status);
522                 refcount_dec(&hp->refcnt);
523                 mdesc_free(hp);
524                 goto out;
525         }
526
527         spin_lock_irqsave(&mdesc_lock, flags);
528         orig_hp = cur_mdesc;
529         cur_mdesc = hp;
530         spin_unlock_irqrestore(&mdesc_lock, flags);
531
532         mdesc_notify_clients(orig_hp, hp);
533
534         spin_lock_irqsave(&mdesc_lock, flags);
535         if (refcount_dec_and_test(&orig_hp->refcnt))
536                 mdesc_free(orig_hp);
537         else
538                 list_add(&orig_hp->list, &mdesc_zombie_list);
539         spin_unlock_irqrestore(&mdesc_lock, flags);
540
541 out:
542         mutex_unlock(&mdesc_mutex);
543 }
544
545 u64 mdesc_get_node(struct mdesc_handle *hp, const char *node_name,
546                    union md_node_info *node_info)
547 {
548         mdesc_node_info_get_f get_info_func;
549         mdesc_node_info_rel_f rel_info_func;
550         mdesc_node_match_f node_match_func;
551         union md_node_info hp_node_info;
552         u64 hp_node;
553         int rv;
554
555         if (hp == NULL || node_name == NULL || node_info == NULL)
556                 return MDESC_NODE_NULL;
557
558         /* Find the ops for the given node name */
559         mdesc_get_node_ops(node_name, &get_info_func, &rel_info_func,
560                            &node_match_func);
561
562         /* If we didn't find ops for the given node name, it is not supported */
563         if (!get_info_func || !rel_info_func || !node_match_func) {
564                 pr_err("MD: %s node is not supported\n", node_name);
565                 return -EINVAL;
566         }
567
568         mdesc_for_each_node_by_name(hp, hp_node, node_name) {
569                 rv = get_info_func(hp, hp_node, &hp_node_info);
570                 if (rv != 0)
571                         continue;
572
573                 if (node_match_func(node_info, &hp_node_info))
574                         break;
575
576                 rel_info_func(&hp_node_info);
577         }
578
579         rel_info_func(&hp_node_info);
580
581         return hp_node;
582 }
583 EXPORT_SYMBOL(mdesc_get_node);
584
585 int mdesc_get_node_info(struct mdesc_handle *hp, u64 node,
586                         const char *node_name, union md_node_info *node_info)
587 {
588         mdesc_node_info_get_f get_info_func;
589         int rv;
590
591         if (hp == NULL || node == MDESC_NODE_NULL ||
592             node_name == NULL || node_info == NULL)
593                 return -EINVAL;
594
595         /* Find the get_info op for the given node name */
596         mdesc_get_node_ops(node_name, &get_info_func, NULL, NULL);
597
598         /* If we didn't find a get_info_func, the node name is not supported */
599         if (get_info_func == NULL) {
600                 pr_err("MD: %s node is not supported\n", node_name);
601                 return -EINVAL;
602         }
603
604         rv = get_info_func(hp, node, node_info);
605         if (rv != 0) {
606                 pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
607                        node_name);
608                 return -1;
609         }
610
611         return 0;
612 }
613 EXPORT_SYMBOL(mdesc_get_node_info);
614
615 static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
616 {
617         return (struct mdesc_elem *) mdesc->data;
618 }
619
620 static void *name_block(struct mdesc_hdr *mdesc)
621 {
622         return ((void *) node_block(mdesc)) + mdesc->node_sz;
623 }
624
625 static void *data_block(struct mdesc_hdr *mdesc)
626 {
627         return ((void *) name_block(mdesc)) + mdesc->name_sz;
628 }
629
630 u64 mdesc_node_by_name(struct mdesc_handle *hp,
631                        u64 from_node, const char *name)
632 {
633         struct mdesc_elem *ep = node_block(&hp->mdesc);
634         const char *names = name_block(&hp->mdesc);
635         u64 last_node = hp->mdesc.node_sz / 16;
636         u64 ret;
637
638         if (from_node == MDESC_NODE_NULL) {
639                 ret = from_node = 0;
640         } else if (from_node >= last_node) {
641                 return MDESC_NODE_NULL;
642         } else {
643                 ret = ep[from_node].d.val;
644         }
645
646         while (ret < last_node) {
647                 if (ep[ret].tag != MD_NODE)
648                         return MDESC_NODE_NULL;
649                 if (!strcmp(names + ep[ret].name_offset, name))
650                         break;
651                 ret = ep[ret].d.val;
652         }
653         if (ret >= last_node)
654                 ret = MDESC_NODE_NULL;
655         return ret;
656 }
657 EXPORT_SYMBOL(mdesc_node_by_name);
658
659 const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
660                                const char *name, int *lenp)
661 {
662         const char *names = name_block(&hp->mdesc);
663         u64 last_node = hp->mdesc.node_sz / 16;
664         void *data = data_block(&hp->mdesc);
665         struct mdesc_elem *ep;
666
667         if (node == MDESC_NODE_NULL || node >= last_node)
668                 return NULL;
669
670         ep = node_block(&hp->mdesc) + node;
671         ep++;
672         for (; ep->tag != MD_NODE_END; ep++) {
673                 void *val = NULL;
674                 int len = 0;
675
676                 switch (ep->tag) {
677                 case MD_PROP_VAL:
678                         val = &ep->d.val;
679                         len = 8;
680                         break;
681
682                 case MD_PROP_STR:
683                 case MD_PROP_DATA:
684                         val = data + ep->d.data.data_offset;
685                         len = ep->d.data.data_len;
686                         break;
687
688                 default:
689                         break;
690                 }
691                 if (!val)
692                         continue;
693
694                 if (!strcmp(names + ep->name_offset, name)) {
695                         if (lenp)
696                                 *lenp = len;
697                         return val;
698                 }
699         }
700
701         return NULL;
702 }
703 EXPORT_SYMBOL(mdesc_get_property);
704
705 u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
706 {
707         struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
708         const char *names = name_block(&hp->mdesc);
709         u64 last_node = hp->mdesc.node_sz / 16;
710
711         if (from == MDESC_NODE_NULL || from >= last_node)
712                 return MDESC_NODE_NULL;
713
714         ep = base + from;
715
716         ep++;
717         for (; ep->tag != MD_NODE_END; ep++) {
718                 if (ep->tag != MD_PROP_ARC)
719                         continue;
720
721                 if (strcmp(names + ep->name_offset, arc_type))
722                         continue;
723
724                 return ep - base;
725         }
726
727         return MDESC_NODE_NULL;
728 }
729 EXPORT_SYMBOL(mdesc_next_arc);
730
731 u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
732 {
733         struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
734
735         ep = base + arc;
736
737         return ep->d.val;
738 }
739 EXPORT_SYMBOL(mdesc_arc_target);
740
741 const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
742 {
743         struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
744         const char *names = name_block(&hp->mdesc);
745         u64 last_node = hp->mdesc.node_sz / 16;
746
747         if (node == MDESC_NODE_NULL || node >= last_node)
748                 return NULL;
749
750         ep = base + node;
751         if (ep->tag != MD_NODE)
752                 return NULL;
753
754         return names + ep->name_offset;
755 }
756 EXPORT_SYMBOL(mdesc_node_name);
757
758 static u64 max_cpus = 64;
759
760 static void __init report_platform_properties(void)
761 {
762         struct mdesc_handle *hp = mdesc_grab();
763         u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
764         const char *s;
765         const u64 *v;
766
767         if (pn == MDESC_NODE_NULL) {
768                 prom_printf("No platform node in machine-description.\n");
769                 prom_halt();
770         }
771
772         s = mdesc_get_property(hp, pn, "banner-name", NULL);
773         printk("PLATFORM: banner-name [%s]\n", s);
774         s = mdesc_get_property(hp, pn, "name", NULL);
775         printk("PLATFORM: name [%s]\n", s);
776
777         v = mdesc_get_property(hp, pn, "hostid", NULL);
778         if (v)
779                 printk("PLATFORM: hostid [%08llx]\n", *v);
780         v = mdesc_get_property(hp, pn, "serial#", NULL);
781         if (v)
782                 printk("PLATFORM: serial# [%08llx]\n", *v);
783         v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
784         printk("PLATFORM: stick-frequency [%08llx]\n", *v);
785         v = mdesc_get_property(hp, pn, "mac-address", NULL);
786         if (v)
787                 printk("PLATFORM: mac-address [%llx]\n", *v);
788         v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
789         if (v)
790                 printk("PLATFORM: watchdog-resolution [%llu ms]\n", *v);
791         v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
792         if (v)
793                 printk("PLATFORM: watchdog-max-timeout [%llu ms]\n", *v);
794         v = mdesc_get_property(hp, pn, "max-cpus", NULL);
795         if (v) {
796                 max_cpus = *v;
797                 printk("PLATFORM: max-cpus [%llu]\n", max_cpus);
798         }
799
800 #ifdef CONFIG_SMP
801         {
802                 int max_cpu, i;
803
804                 if (v) {
805                         max_cpu = *v;
806                         if (max_cpu > NR_CPUS)
807                                 max_cpu = NR_CPUS;
808                 } else {
809                         max_cpu = NR_CPUS;
810                 }
811                 for (i = 0; i < max_cpu; i++)
812                         set_cpu_possible(i, true);
813         }
814 #endif
815
816         mdesc_release(hp);
817 }
818
819 static void fill_in_one_cache(cpuinfo_sparc *c, struct mdesc_handle *hp, u64 mp)
820 {
821         const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
822         const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
823         const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
824         const char *type;
825         int type_len;
826
827         type = mdesc_get_property(hp, mp, "type", &type_len);
828
829         switch (*level) {
830         case 1:
831                 if (of_find_in_proplist(type, "instn", type_len)) {
832                         c->icache_size = *size;
833                         c->icache_line_size = *line_size;
834                 } else if (of_find_in_proplist(type, "data", type_len)) {
835                         c->dcache_size = *size;
836                         c->dcache_line_size = *line_size;
837                 }
838                 break;
839
840         case 2:
841                 c->ecache_size = *size;
842                 c->ecache_line_size = *line_size;
843                 break;
844
845         default:
846                 break;
847         }
848
849         if (*level == 1) {
850                 u64 a;
851
852                 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
853                         u64 target = mdesc_arc_target(hp, a);
854                         const char *name = mdesc_node_name(hp, target);
855
856                         if (!strcmp(name, "cache"))
857                                 fill_in_one_cache(c, hp, target);
858                 }
859         }
860 }
861
862 static void find_back_node_value(struct mdesc_handle *hp, u64 node,
863                                  char *srch_val,
864                                  void (*func)(struct mdesc_handle *, u64, int),
865                                  u64 val, int depth)
866 {
867         u64 arc;
868
869         /* Since we have an estimate of recursion depth, do a sanity check. */
870         if (depth == 0)
871                 return;
872
873         mdesc_for_each_arc(arc, hp, node, MDESC_ARC_TYPE_BACK) {
874                 u64 n = mdesc_arc_target(hp, arc);
875                 const char *name = mdesc_node_name(hp, n);
876
877                 if (!strcmp(srch_val, name))
878                         (*func)(hp, n, val);
879
880                 find_back_node_value(hp, n, srch_val, func, val, depth-1);
881         }
882 }
883
884 static void __mark_core_id(struct mdesc_handle *hp, u64 node,
885                            int core_id)
886 {
887         const u64 *id = mdesc_get_property(hp, node, "id", NULL);
888
889         if (*id < num_possible_cpus())
890                 cpu_data(*id).core_id = core_id;
891 }
892
893 static void __mark_max_cache_id(struct mdesc_handle *hp, u64 node,
894                                 int max_cache_id)
895 {
896         const u64 *id = mdesc_get_property(hp, node, "id", NULL);
897
898         if (*id < num_possible_cpus()) {
899                 cpu_data(*id).max_cache_id = max_cache_id;
900
901                 /**
902                  * On systems without explicit socket descriptions socket
903                  * is max_cache_id
904                  */
905                 cpu_data(*id).sock_id = max_cache_id;
906         }
907 }
908
909 static void mark_core_ids(struct mdesc_handle *hp, u64 mp,
910                           int core_id)
911 {
912         find_back_node_value(hp, mp, "cpu", __mark_core_id, core_id, 10);
913 }
914
915 static void mark_max_cache_ids(struct mdesc_handle *hp, u64 mp,
916                                int max_cache_id)
917 {
918         find_back_node_value(hp, mp, "cpu", __mark_max_cache_id,
919                              max_cache_id, 10);
920 }
921
922 static void set_core_ids(struct mdesc_handle *hp)
923 {
924         int idx;
925         u64 mp;
926
927         idx = 1;
928
929         /* Identify unique cores by looking for cpus backpointed to by
930          * level 1 instruction caches.
931          */
932         mdesc_for_each_node_by_name(hp, mp, "cache") {
933                 const u64 *level;
934                 const char *type;
935                 int len;
936
937                 level = mdesc_get_property(hp, mp, "level", NULL);
938                 if (*level != 1)
939                         continue;
940
941                 type = mdesc_get_property(hp, mp, "type", &len);
942                 if (!of_find_in_proplist(type, "instn", len))
943                         continue;
944
945                 mark_core_ids(hp, mp, idx);
946                 idx++;
947         }
948 }
949
950 static int set_max_cache_ids_by_cache(struct mdesc_handle *hp, int level)
951 {
952         u64 mp;
953         int idx = 1;
954         int fnd = 0;
955
956         /**
957          * Identify unique highest level of shared cache by looking for cpus
958          * backpointed to by shared level N caches.
959          */
960         mdesc_for_each_node_by_name(hp, mp, "cache") {
961                 const u64 *cur_lvl;
962
963                 cur_lvl = mdesc_get_property(hp, mp, "level", NULL);
964                 if (*cur_lvl != level)
965                         continue;
966                 mark_max_cache_ids(hp, mp, idx);
967                 idx++;
968                 fnd = 1;
969         }
970         return fnd;
971 }
972
973 static void set_sock_ids_by_socket(struct mdesc_handle *hp, u64 mp)
974 {
975         int idx = 1;
976
977         mdesc_for_each_node_by_name(hp, mp, "socket") {
978                 u64 a;
979
980                 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
981                         u64 t = mdesc_arc_target(hp, a);
982                         const char *name;
983                         const u64 *id;
984
985                         name = mdesc_node_name(hp, t);
986                         if (strcmp(name, "cpu"))
987                                 continue;
988
989                         id = mdesc_get_property(hp, t, "id", NULL);
990                         if (*id < num_possible_cpus())
991                                 cpu_data(*id).sock_id = idx;
992                 }
993                 idx++;
994         }
995 }
996
997 static void set_sock_ids(struct mdesc_handle *hp)
998 {
999         u64 mp;
1000
1001         /**
1002          * Find the highest level of shared cache which pre-T7 is also
1003          * the socket.
1004          */
1005         if (!set_max_cache_ids_by_cache(hp, 3))
1006                 set_max_cache_ids_by_cache(hp, 2);
1007
1008         /* If machine description exposes sockets data use it.*/
1009         mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "sockets");
1010         if (mp != MDESC_NODE_NULL)
1011                 set_sock_ids_by_socket(hp, mp);
1012 }
1013
1014 static void mark_proc_ids(struct mdesc_handle *hp, u64 mp, int proc_id)
1015 {
1016         u64 a;
1017
1018         mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
1019                 u64 t = mdesc_arc_target(hp, a);
1020                 const char *name;
1021                 const u64 *id;
1022
1023                 name = mdesc_node_name(hp, t);
1024                 if (strcmp(name, "cpu"))
1025                         continue;
1026
1027                 id = mdesc_get_property(hp, t, "id", NULL);
1028                 if (*id < NR_CPUS)
1029                         cpu_data(*id).proc_id = proc_id;
1030         }
1031 }
1032
1033 static void __set_proc_ids(struct mdesc_handle *hp, const char *exec_unit_name)
1034 {
1035         int idx;
1036         u64 mp;
1037
1038         idx = 0;
1039         mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
1040                 const char *type;
1041                 int len;
1042
1043                 type = mdesc_get_property(hp, mp, "type", &len);
1044                 if (!of_find_in_proplist(type, "int", len) &&
1045                     !of_find_in_proplist(type, "integer", len))
1046                         continue;
1047
1048                 mark_proc_ids(hp, mp, idx);
1049                 idx++;
1050         }
1051 }
1052
1053 static void set_proc_ids(struct mdesc_handle *hp)
1054 {
1055         __set_proc_ids(hp, "exec_unit");
1056         __set_proc_ids(hp, "exec-unit");
1057 }
1058
1059 static void get_one_mondo_bits(const u64 *p, unsigned int *mask,
1060                                unsigned long def, unsigned long max)
1061 {
1062         u64 val;
1063
1064         if (!p)
1065                 goto use_default;
1066         val = *p;
1067
1068         if (!val || val >= 64)
1069                 goto use_default;
1070
1071         if (val > max)
1072                 val = max;
1073
1074         *mask = ((1U << val) * 64U) - 1U;
1075         return;
1076
1077 use_default:
1078         *mask = ((1U << def) * 64U) - 1U;
1079 }
1080
1081 static void get_mondo_data(struct mdesc_handle *hp, u64 mp,
1082                            struct trap_per_cpu *tb)
1083 {
1084         static int printed;
1085         const u64 *val;
1086
1087         val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
1088         get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7, ilog2(max_cpus * 2));
1089
1090         val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
1091         get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7, 8);
1092
1093         val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
1094         get_one_mondo_bits(val, &tb->resum_qmask, 6, 7);
1095
1096         val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
1097         get_one_mondo_bits(val, &tb->nonresum_qmask, 2, 2);
1098         if (!printed++) {
1099                 pr_info("SUN4V: Mondo queue sizes "
1100                         "[cpu(%u) dev(%u) r(%u) nr(%u)]\n",
1101                         tb->cpu_mondo_qmask + 1,
1102                         tb->dev_mondo_qmask + 1,
1103                         tb->resum_qmask + 1,
1104                         tb->nonresum_qmask + 1);
1105         }
1106 }
1107
1108 static void *mdesc_iterate_over_cpus(void *(*func)(struct mdesc_handle *, u64, int, void *), void *arg, cpumask_t *mask)
1109 {
1110         struct mdesc_handle *hp = mdesc_grab();
1111         void *ret = NULL;
1112         u64 mp;
1113
1114         mdesc_for_each_node_by_name(hp, mp, "cpu") {
1115                 const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
1116                 int cpuid = *id;
1117
1118 #ifdef CONFIG_SMP
1119                 if (cpuid >= NR_CPUS) {
1120                         printk(KERN_WARNING "Ignoring CPU %d which is "
1121                                ">= NR_CPUS (%d)\n",
1122                                cpuid, NR_CPUS);
1123                         continue;
1124                 }
1125                 if (!cpumask_test_cpu(cpuid, mask))
1126                         continue;
1127 #endif
1128
1129                 ret = func(hp, mp, cpuid, arg);
1130                 if (ret)
1131                         goto out;
1132         }
1133 out:
1134         mdesc_release(hp);
1135         return ret;
1136 }
1137
1138 static void *record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
1139                             void *arg)
1140 {
1141         ncpus_probed++;
1142 #ifdef CONFIG_SMP
1143         set_cpu_present(cpuid, true);
1144 #endif
1145         return NULL;
1146 }
1147
1148 void mdesc_populate_present_mask(cpumask_t *mask)
1149 {
1150         if (tlb_type != hypervisor)
1151                 return;
1152
1153         ncpus_probed = 0;
1154         mdesc_iterate_over_cpus(record_one_cpu, NULL, mask);
1155 }
1156
1157 static void * __init check_one_pgsz(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
1158 {
1159         const u64 *pgsz_prop = mdesc_get_property(hp, mp, "mmu-page-size-list", NULL);
1160         unsigned long *pgsz_mask = arg;
1161         u64 val;
1162
1163         val = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
1164                HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
1165         if (pgsz_prop)
1166                 val = *pgsz_prop;
1167
1168         if (!*pgsz_mask)
1169                 *pgsz_mask = val;
1170         else
1171                 *pgsz_mask &= val;
1172         return NULL;
1173 }
1174
1175 void __init mdesc_get_page_sizes(cpumask_t *mask, unsigned long *pgsz_mask)
1176 {
1177         *pgsz_mask = 0;
1178         mdesc_iterate_over_cpus(check_one_pgsz, pgsz_mask, mask);
1179 }
1180
1181 static void *fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
1182                              void *arg)
1183 {
1184         const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
1185         struct trap_per_cpu *tb;
1186         cpuinfo_sparc *c;
1187         u64 a;
1188
1189 #ifndef CONFIG_SMP
1190         /* On uniprocessor we only want the values for the
1191          * real physical cpu the kernel booted onto, however
1192          * cpu_data() only has one entry at index 0.
1193          */
1194         if (cpuid != real_hard_smp_processor_id())
1195                 return NULL;
1196         cpuid = 0;
1197 #endif
1198
1199         c = &cpu_data(cpuid);
1200         c->clock_tick = *cfreq;
1201
1202         tb = &trap_block[cpuid];
1203         get_mondo_data(hp, mp, tb);
1204
1205         mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
1206                 u64 j, t = mdesc_arc_target(hp, a);
1207                 const char *t_name;
1208
1209                 t_name = mdesc_node_name(hp, t);
1210                 if (!strcmp(t_name, "cache")) {
1211                         fill_in_one_cache(c, hp, t);
1212                         continue;
1213                 }
1214
1215                 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
1216                         u64 n = mdesc_arc_target(hp, j);
1217                         const char *n_name;
1218
1219                         n_name = mdesc_node_name(hp, n);
1220                         if (!strcmp(n_name, "cache"))
1221                                 fill_in_one_cache(c, hp, n);
1222                 }
1223         }
1224
1225         c->core_id = 0;
1226         c->proc_id = -1;
1227
1228         return NULL;
1229 }
1230
1231 void mdesc_fill_in_cpu_data(cpumask_t *mask)
1232 {
1233         struct mdesc_handle *hp;
1234
1235         mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask);
1236
1237         hp = mdesc_grab();
1238
1239         set_core_ids(hp);
1240         set_proc_ids(hp);
1241         set_sock_ids(hp);
1242
1243         mdesc_release(hp);
1244
1245         smp_fill_in_sib_core_maps();
1246 }
1247
1248 /* mdesc_open() - Grab a reference to mdesc_handle when /dev/mdesc is
1249  * opened. Hold this reference until /dev/mdesc is closed to ensure
1250  * mdesc data structure is not released underneath us. Store the
1251  * pointer to mdesc structure in private_data for read and seek to use
1252  */
1253 static int mdesc_open(struct inode *inode, struct file *file)
1254 {
1255         struct mdesc_handle *hp = mdesc_grab();
1256
1257         if (!hp)
1258                 return -ENODEV;
1259
1260         file->private_data = hp;
1261
1262         return 0;
1263 }
1264
1265 static ssize_t mdesc_read(struct file *file, char __user *buf,
1266                           size_t len, loff_t *offp)
1267 {
1268         struct mdesc_handle *hp = file->private_data;
1269         unsigned char *mdesc;
1270         int bytes_left, count = len;
1271
1272         if (*offp >= hp->handle_size)
1273                 return 0;
1274
1275         bytes_left = hp->handle_size - *offp;
1276         if (count > bytes_left)
1277                 count = bytes_left;
1278
1279         mdesc = (unsigned char *)&hp->mdesc;
1280         mdesc += *offp;
1281         if (!copy_to_user(buf, mdesc, count)) {
1282                 *offp += count;
1283                 return count;
1284         } else {
1285                 return -EFAULT;
1286         }
1287 }
1288
1289 static loff_t mdesc_llseek(struct file *file, loff_t offset, int whence)
1290 {
1291         struct mdesc_handle *hp = file->private_data;
1292
1293         return no_seek_end_llseek_size(file, offset, whence, hp->handle_size);
1294 }
1295
1296 /* mdesc_close() - /dev/mdesc is being closed, release the reference to
1297  * mdesc structure.
1298  */
1299 static int mdesc_close(struct inode *inode, struct file *file)
1300 {
1301         mdesc_release(file->private_data);
1302         return 0;
1303 }
1304
1305 static const struct file_operations mdesc_fops = {
1306         .open    = mdesc_open,
1307         .read    = mdesc_read,
1308         .llseek  = mdesc_llseek,
1309         .release = mdesc_close,
1310         .owner   = THIS_MODULE,
1311 };
1312
1313 static struct miscdevice mdesc_misc = {
1314         .minor  = MISC_DYNAMIC_MINOR,
1315         .name   = "mdesc",
1316         .fops   = &mdesc_fops,
1317 };
1318
1319 static int __init mdesc_misc_init(void)
1320 {
1321         return misc_register(&mdesc_misc);
1322 }
1323
1324 __initcall(mdesc_misc_init);
1325
1326 void __init sun4v_mdesc_init(void)
1327 {
1328         struct mdesc_handle *hp;
1329         unsigned long len, real_len, status;
1330
1331         (void) sun4v_mach_desc(0UL, 0UL, &len);
1332
1333         printk("MDESC: Size is %lu bytes.\n", len);
1334
1335         hp = mdesc_alloc(len, &memblock_mdesc_ops);
1336         if (hp == NULL) {
1337                 prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
1338                 prom_halt();
1339         }
1340
1341         status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
1342         if (status != HV_EOK || real_len > len) {
1343                 prom_printf("sun4v_mach_desc fails, err(%lu), "
1344                             "len(%lu), real_len(%lu)\n",
1345                             status, len, real_len);
1346                 mdesc_free(hp);
1347                 prom_halt();
1348         }
1349
1350         cur_mdesc = hp;
1351
1352         mdesc_adi_init();
1353         report_platform_properties();
1354 }