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