Linux Audio

Check our new training course

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