mdesc.c 30.1 KB
Newer Older
1 2
/* mdesc.c: Sun4V machine description handling.
 *
3
 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
4 5 6
 */
#include <linux/kernel.h>
#include <linux/types.h>
Y
Yinghai Lu 已提交
7
#include <linux/memblock.h>
8
#include <linux/log2.h>
9 10
#include <linux/list.h>
#include <linux/slab.h>
11
#include <linux/mm.h>
12
#include <linux/miscdevice.h>
13
#include <linux/bootmem.h>
14
#include <linux/export.h>
15

16
#include <asm/cpudata.h>
17 18 19
#include <asm/hypervisor.h>
#include <asm/mdesc.h>
#include <asm/prom.h>
20
#include <linux/uaccess.h>
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <asm/oplib.h>
#include <asm/smp.h>

/* Unlike the OBP device tree, the machine description is a full-on
 * DAG.  An arbitrary number of ARCs are possible from one
 * node to other nodes and thus we can't use the OBP device_node
 * data structure to represent these nodes inside of the kernel.
 *
 * Actually, it isn't even a DAG, because there are back pointers
 * which create cycles in the graph.
 *
 * mdesc_hdr and mdesc_elem describe the layout of the data structure
 * we get from the Hypervisor.
 */
struct mdesc_hdr {
	u32	version; /* Transport version */
	u32	node_sz; /* node block size */
	u32	name_sz; /* name block size */
	u32	data_sz; /* data block size */
40
} __attribute__((aligned(16)));
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

struct mdesc_elem {
	u8	tag;
#define MD_LIST_END	0x00
#define MD_NODE		0x4e
#define MD_NODE_END	0x45
#define MD_NOOP		0x20
#define MD_PROP_ARC	0x61
#define MD_PROP_VAL	0x76
#define MD_PROP_STR	0x73
#define MD_PROP_DATA	0x64
	u8	name_len;
	u16	resv;
	u32	name_offset;
	union {
		struct {
			u32	data_len;
			u32	data_offset;
		} data;
		u64	val;
	} d;
};

64 65 66 67
struct mdesc_mem_ops {
	struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
	void (*free)(struct mdesc_handle *handle);
};
68

69 70 71 72 73 74 75 76
struct mdesc_handle {
	struct list_head	list;
	struct mdesc_mem_ops	*mops;
	void			*self_base;
	atomic_t		refcnt;
	unsigned int		handle_size;
	struct mdesc_hdr	mdesc;
};
77

J
Jag Raman 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
typedef int (*mdesc_node_info_get_f)(struct mdesc_handle *, u64,
				     union md_node_info *);
typedef void (*mdesc_node_info_rel_f)(union md_node_info *);
typedef bool (*mdesc_node_match_f)(union md_node_info *, union md_node_info *);

struct md_node_ops {
	char			*name;
	mdesc_node_info_get_f	get_info;
	mdesc_node_info_rel_f	rel_info;
	mdesc_node_match_f	node_match;
};

static int get_vdev_port_node_info(struct mdesc_handle *md, u64 node,
				   union md_node_info *node_info);
static void rel_vdev_port_node_info(union md_node_info *node_info);
static bool vdev_port_node_match(union md_node_info *a_node_info,
				 union md_node_info *b_node_info);

static int get_ds_port_node_info(struct mdesc_handle *md, u64 node,
				 union md_node_info *node_info);
static void rel_ds_port_node_info(union md_node_info *node_info);
static bool ds_port_node_match(union md_node_info *a_node_info,
			       union md_node_info *b_node_info);

/* supported node types which can be registered */
static struct md_node_ops md_node_ops_table[] = {
	{"virtual-device-port", get_vdev_port_node_info,
	 rel_vdev_port_node_info, vdev_port_node_match},
	{"domain-services-port", get_ds_port_node_info,
	 rel_ds_port_node_info, ds_port_node_match},
	{NULL, NULL, NULL, NULL}
};

static void mdesc_get_node_ops(const char *node_name,
			       mdesc_node_info_get_f *get_info_f,
			       mdesc_node_info_rel_f *rel_info_f,
			       mdesc_node_match_f *match_f)
{
	int i;

	if (get_info_f)
		*get_info_f = NULL;

	if (rel_info_f)
		*rel_info_f = NULL;

	if (match_f)
		*match_f = NULL;

	if (!node_name)
		return;

	for (i = 0; md_node_ops_table[i].name != NULL; i++) {
		if (strcmp(md_node_ops_table[i].name, node_name) == 0) {
			if (get_info_f)
				*get_info_f = md_node_ops_table[i].get_info;

			if (rel_info_f)
				*rel_info_f = md_node_ops_table[i].rel_info;

			if (match_f)
				*match_f = md_node_ops_table[i].node_match;

			break;
		}
	}
}

146 147 148
static void mdesc_handle_init(struct mdesc_handle *hp,
			      unsigned int handle_size,
			      void *base)
149
{
150 151 152 153 154 155 156
	BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));

	memset(hp, 0, handle_size);
	INIT_LIST_HEAD(&hp->list);
	hp->self_base = base;
	atomic_set(&hp->refcnt, 1);
	hp->handle_size = handle_size;
157 158
}

Y
Yinghai Lu 已提交
159
static struct mdesc_handle * __init mdesc_memblock_alloc(unsigned int mdesc_size)
160
{
161
	unsigned int handle_size, alloc_size;
162 163
	struct mdesc_handle *hp;
	unsigned long paddr;
164

165 166 167 168
	handle_size = (sizeof(struct mdesc_handle) -
		       sizeof(struct mdesc_hdr) +
		       mdesc_size);
	alloc_size = PAGE_ALIGN(handle_size);
169

Y
Yinghai Lu 已提交
170
	paddr = memblock_alloc(alloc_size, PAGE_SIZE);
171

172 173 174 175 176
	hp = NULL;
	if (paddr) {
		hp = __va(paddr);
		mdesc_handle_init(hp, handle_size, hp);
	}
177
	return hp;
178 179
}

180
static void __init mdesc_memblock_free(struct mdesc_handle *hp)
181
{
182 183
	unsigned int alloc_size;
	unsigned long start;
184

185 186
	BUG_ON(atomic_read(&hp->refcnt) != 0);
	BUG_ON(!list_empty(&hp->list));
187

188 189 190
	alloc_size = PAGE_ALIGN(hp->handle_size);
	start = __pa(hp);
	free_bootmem_late(start, alloc_size);
191 192
}

Y
Yinghai Lu 已提交
193 194 195
static struct mdesc_mem_ops memblock_mdesc_ops = {
	.alloc = mdesc_memblock_alloc,
	.free  = mdesc_memblock_free,
196 197 198
};

static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
199
{
200
	unsigned int handle_size;
201 202
	struct mdesc_handle *hp;
	unsigned long addr;
203
	void *base;
204

205 206 207
	handle_size = (sizeof(struct mdesc_handle) -
		       sizeof(struct mdesc_hdr) +
		       mdesc_size);
208 209 210
	base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_REPEAT);
	if (!base)
		return NULL;
211

212 213 214
	addr = (unsigned long)base;
	addr = (addr + 15UL) & ~15UL;
	hp = (struct mdesc_handle *) addr;
215

216
	mdesc_handle_init(hp, handle_size, base);
217

218
	return hp;
219 220
}

221
static void mdesc_kfree(struct mdesc_handle *hp)
222
{
223 224 225 226
	BUG_ON(atomic_read(&hp->refcnt) != 0);
	BUG_ON(!list_empty(&hp->list));

	kfree(hp->self_base);
227 228
}

229 230 231 232 233 234 235
static struct mdesc_mem_ops kmalloc_mdesc_memops = {
	.alloc = mdesc_kmalloc,
	.free  = mdesc_kfree,
};

static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
					struct mdesc_mem_ops *mops)
236
{
237
	struct mdesc_handle *hp = mops->alloc(mdesc_size);
238

239 240
	if (hp)
		hp->mops = mops;
241

242 243
	return hp;
}
244

245
static void mdesc_free(struct mdesc_handle *hp)
246
{
247 248
	hp->mops->free(hp);
}
249

250 251 252
static struct mdesc_handle *cur_mdesc;
static LIST_HEAD(mdesc_zombie_list);
static DEFINE_SPINLOCK(mdesc_lock);
253

254 255 256 257
struct mdesc_handle *mdesc_grab(void)
{
	struct mdesc_handle *hp;
	unsigned long flags;
258

259 260 261 262 263
	spin_lock_irqsave(&mdesc_lock, flags);
	hp = cur_mdesc;
	if (hp)
		atomic_inc(&hp->refcnt);
	spin_unlock_irqrestore(&mdesc_lock, flags);
264

265
	return hp;
266
}
267
EXPORT_SYMBOL(mdesc_grab);
268

269
void mdesc_release(struct mdesc_handle *hp)
270
{
271
	unsigned long flags;
272

273 274 275 276
	spin_lock_irqsave(&mdesc_lock, flags);
	if (atomic_dec_and_test(&hp->refcnt)) {
		list_del_init(&hp->list);
		hp->mops->free(hp);
277
	}
278
	spin_unlock_irqrestore(&mdesc_lock, flags);
279
}
280
EXPORT_SYMBOL(mdesc_release);
281

282 283 284 285 286
static DEFINE_MUTEX(mdesc_mutex);
static struct mdesc_notifier_client *client_list;

void mdesc_register_notifier(struct mdesc_notifier_client *client)
{
287
	bool supported = false;
288
	u64 node;
289
	int i;
290 291

	mutex_lock(&mdesc_mutex);
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

	/* check to see if the node is supported for registration */
	for (i = 0; md_node_ops_table[i].name != NULL; i++) {
		if (strcmp(md_node_ops_table[i].name, client->node_name) == 0) {
			supported = true;
			break;
		}
	}

	if (!supported) {
		pr_err("MD: %s node not supported\n", client->node_name);
		mutex_unlock(&mdesc_mutex);
		return;
	}

307 308 309 310
	client->next = client_list;
	client_list = client;

	mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
311
		client->add(cur_mdesc, node, client->node_name);
312 313 314 315

	mutex_unlock(&mdesc_mutex);
}

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
{
	const u64 *id;
	u64 a;

	id = NULL;
	mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
		u64 target;

		target = mdesc_arc_target(hp, a);
		id = mdesc_get_property(hp, target,
					"cfg-handle", NULL);
		if (id)
			break;
	}

	return id;
}

J
Jag Raman 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
static int get_vdev_port_node_info(struct mdesc_handle *md, u64 node,
				   union md_node_info *node_info)
{
	const u64 *parent_cfg_hdlp;
	const char *name;
	const u64 *idp;

	/*
	 * Virtual device nodes are distinguished by:
	 * 1. "id" property
	 * 2. "name" property
	 * 3. parent node "cfg-handle" property
	 */
	idp = mdesc_get_property(md, node, "id", NULL);
	name = mdesc_get_property(md, node, "name", NULL);
	parent_cfg_hdlp = parent_cfg_handle(md, node);

	if (!idp || !name || !parent_cfg_hdlp)
		return -1;

	node_info->vdev_port.id = *idp;
	node_info->vdev_port.name = kstrdup_const(name, GFP_KERNEL);
	node_info->vdev_port.parent_cfg_hdl = *parent_cfg_hdlp;

	return 0;
}

static void rel_vdev_port_node_info(union md_node_info *node_info)
{
	if (node_info && node_info->vdev_port.name) {
		kfree_const(node_info->vdev_port.name);
		node_info->vdev_port.name = NULL;
	}
}

static bool vdev_port_node_match(union md_node_info *a_node_info,
				 union md_node_info *b_node_info)
{
	if (a_node_info->vdev_port.id != b_node_info->vdev_port.id)
		return false;

	if (a_node_info->vdev_port.parent_cfg_hdl !=
	    b_node_info->vdev_port.parent_cfg_hdl)
		return false;

	if (strncmp(a_node_info->vdev_port.name,
		    b_node_info->vdev_port.name, MDESC_MAX_STR_LEN) != 0)
		return false;

	return true;
}

static int get_ds_port_node_info(struct mdesc_handle *md, u64 node,
				 union md_node_info *node_info)
{
	const u64 *idp;

	/* DS port nodes use the "id" property to distinguish them */
	idp = mdesc_get_property(md, node, "id", NULL);
	if (!idp)
		return -1;

	node_info->ds_port.id = *idp;

	return 0;
}

static void rel_ds_port_node_info(union md_node_info *node_info)
{
}

static bool ds_port_node_match(union md_node_info *a_node_info,
			       union md_node_info *b_node_info)
{
	if (a_node_info->ds_port.id != b_node_info->ds_port.id)
		return false;

	return true;
}

415 416 417 418
/* Run 'func' on nodes which are in A but not in B.  */
static void invoke_on_missing(const char *name,
			      struct mdesc_handle *a,
			      struct mdesc_handle *b,
419 420
			      void (*func)(struct mdesc_handle *, u64,
					   const char *node_name))
421
{
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	mdesc_node_info_get_f get_info_func;
	mdesc_node_info_rel_f rel_info_func;
	mdesc_node_match_f node_match_func;
	union md_node_info a_node_info;
	union md_node_info b_node_info;
	bool found;
	u64 a_node;
	u64 b_node;
	int rv;

	/*
	 * Find the get_info, rel_info and node_match ops for the given
	 * node name
	 */
	mdesc_get_node_ops(name, &get_info_func, &rel_info_func,
			   &node_match_func);

	/* If we didn't find a match, the node type is not supported */
	if (!get_info_func || !rel_info_func || !node_match_func) {
		pr_err("MD: %s node type is not supported\n", name);
		return;
	}

	mdesc_for_each_node_by_name(a, a_node, name) {
		found = false;
447

448 449 450 451
		rv = get_info_func(a, a_node, &a_node_info);
		if (rv != 0) {
			pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
			       name);
452 453 454
			continue;
		}

455 456 457 458 459 460 461 462 463
		/* Check each node in B for node matching a_node */
		mdesc_for_each_node_by_name(b, b_node, name) {
			rv = get_info_func(b, b_node, &b_node_info);
			if (rv != 0)
				continue;

			if (node_match_func(&a_node_info, &b_node_info)) {
				found = true;
				rel_info_func(&b_node_info);
464 465
				break;
			}
466 467

			rel_info_func(&b_node_info);
468
		}
469 470 471

		rel_info_func(&a_node_info);

472
		if (!found)
473
			func(a, a_node, name);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
	}
}

static void notify_one(struct mdesc_notifier_client *p,
		       struct mdesc_handle *old_hp,
		       struct mdesc_handle *new_hp)
{
	invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
	invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
}

static void mdesc_notify_clients(struct mdesc_handle *old_hp,
				 struct mdesc_handle *new_hp)
{
	struct mdesc_notifier_client *p = client_list;

	while (p) {
		notify_one(p, old_hp, new_hp);
		p = p->next;
	}
}

496
void mdesc_update(void)
497
{
498 499 500 501
	unsigned long len, real_len, status;
	struct mdesc_handle *hp, *orig_hp;
	unsigned long flags;

502 503
	mutex_lock(&mdesc_mutex);

504 505 506 507 508
	(void) sun4v_mach_desc(0UL, 0UL, &len);

	hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
	if (!hp) {
		printk(KERN_ERR "MD: mdesc alloc fails\n");
509
		goto out;
510 511 512 513 514 515 516 517
	}

	status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
	if (status != HV_EOK || real_len > len) {
		printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
		       status);
		atomic_dec(&hp->refcnt);
		mdesc_free(hp);
518
		goto out;
519
	}
520

521 522 523
	spin_lock_irqsave(&mdesc_lock, flags);
	orig_hp = cur_mdesc;
	cur_mdesc = hp;
524
	spin_unlock_irqrestore(&mdesc_lock, flags);
525

526 527 528
	mdesc_notify_clients(orig_hp, hp);

	spin_lock_irqsave(&mdesc_lock, flags);
529 530 531 532 533
	if (atomic_dec_and_test(&orig_hp->refcnt))
		mdesc_free(orig_hp);
	else
		list_add(&orig_hp->list, &mdesc_zombie_list);
	spin_unlock_irqrestore(&mdesc_lock, flags);
534 535 536

out:
	mutex_unlock(&mdesc_mutex);
537 538
}

J
Jag Raman 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
u64 mdesc_get_node(struct mdesc_handle *hp, const char *node_name,
		   union md_node_info *node_info)
{
	mdesc_node_info_get_f get_info_func;
	mdesc_node_info_rel_f rel_info_func;
	mdesc_node_match_f node_match_func;
	union md_node_info hp_node_info;
	u64 hp_node;
	int rv;

	if (hp == NULL || node_name == NULL || node_info == NULL)
		return MDESC_NODE_NULL;

	/* Find the ops for the given node name */
	mdesc_get_node_ops(node_name, &get_info_func, &rel_info_func,
			   &node_match_func);

	/* If we didn't find ops for the given node name, it is not supported */
	if (!get_info_func || !rel_info_func || !node_match_func) {
		pr_err("MD: %s node is not supported\n", node_name);
		return -EINVAL;
	}

	mdesc_for_each_node_by_name(hp, hp_node, node_name) {
		rv = get_info_func(hp, hp_node, &hp_node_info);
		if (rv != 0)
			continue;

		if (node_match_func(node_info, &hp_node_info))
			break;

		rel_info_func(&hp_node_info);
	}

	rel_info_func(&hp_node_info);

	return hp_node;
}
577
EXPORT_SYMBOL(mdesc_get_node);
J
Jag Raman 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

int mdesc_get_node_info(struct mdesc_handle *hp, u64 node,
			const char *node_name, union md_node_info *node_info)
{
	mdesc_node_info_get_f get_info_func;
	int rv;

	if (hp == NULL || node == MDESC_NODE_NULL ||
	    node_name == NULL || node_info == NULL)
		return -EINVAL;

	/* Find the get_info op for the given node name */
	mdesc_get_node_ops(node_name, &get_info_func, NULL, NULL);

	/* If we didn't find a get_info_func, the node name is not supported */
	if (get_info_func == NULL) {
		pr_err("MD: %s node is not supported\n", node_name);
		return -EINVAL;
	}

	rv = get_info_func(hp, node, node_info);
	if (rv != 0) {
		pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
		       node_name);
		return -1;
	}

	return 0;
}
607
EXPORT_SYMBOL(mdesc_get_node_info);
J
Jag Raman 已提交
608

609
static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
610 611 612 613
{
	return (struct mdesc_elem *) (mdesc + 1);
}

614
static void *name_block(struct mdesc_hdr *mdesc)
615 616 617 618
{
	return ((void *) node_block(mdesc)) + mdesc->node_sz;
}

619
static void *data_block(struct mdesc_hdr *mdesc)
620 621 622 623
{
	return ((void *) name_block(mdesc)) + mdesc->name_sz;
}

624 625
u64 mdesc_node_by_name(struct mdesc_handle *hp,
		       u64 from_node, const char *name)
626
{
627 628 629 630 631
	struct mdesc_elem *ep = node_block(&hp->mdesc);
	const char *names = name_block(&hp->mdesc);
	u64 last_node = hp->mdesc.node_sz / 16;
	u64 ret;

632 633 634
	if (from_node == MDESC_NODE_NULL) {
		ret = from_node = 0;
	} else if (from_node >= last_node) {
635
		return MDESC_NODE_NULL;
636 637 638
	} else {
		ret = ep[from_node].d.val;
	}
639 640 641 642 643 644 645 646 647 648 649 650 651

	while (ret < last_node) {
		if (ep[ret].tag != MD_NODE)
			return MDESC_NODE_NULL;
		if (!strcmp(names + ep[ret].name_offset, name))
			break;
		ret = ep[ret].d.val;
	}
	if (ret >= last_node)
		ret = MDESC_NODE_NULL;
	return ret;
}
EXPORT_SYMBOL(mdesc_node_by_name);
652

653 654 655 656 657 658 659
const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
			       const char *name, int *lenp)
{
	const char *names = name_block(&hp->mdesc);
	u64 last_node = hp->mdesc.node_sz / 16;
	void *data = data_block(&hp->mdesc);
	struct mdesc_elem *ep;
660

661 662
	if (node == MDESC_NODE_NULL || node >= last_node)
		return NULL;
663

664 665 666 667 668 669 670 671 672 673
	ep = node_block(&hp->mdesc) + node;
	ep++;
	for (; ep->tag != MD_NODE_END; ep++) {
		void *val = NULL;
		int len = 0;

		switch (ep->tag) {
		case MD_PROP_VAL:
			val = &ep->d.val;
			len = 8;
674 675
			break;

676 677 678 679 680
		case MD_PROP_STR:
		case MD_PROP_DATA:
			val = data + ep->d.data.data_offset;
			len = ep->d.data.data_len;
			break;
681

682
		default:
683 684
			break;
		}
685 686
		if (!val)
			continue;
687

688 689 690 691 692
		if (!strcmp(names + ep->name_offset, name)) {
			if (lenp)
				*lenp = len;
			return val;
		}
693 694
	}

695 696 697
	return NULL;
}
EXPORT_SYMBOL(mdesc_get_property);
698

699 700 701 702 703
u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
{
	struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
	const char *names = name_block(&hp->mdesc);
	u64 last_node = hp->mdesc.node_sz / 16;
704

705 706
	if (from == MDESC_NODE_NULL || from >= last_node)
		return MDESC_NODE_NULL;
707

708 709 710 711 712 713 714 715 716 717 718
	ep = base + from;

	ep++;
	for (; ep->tag != MD_NODE_END; ep++) {
		if (ep->tag != MD_PROP_ARC)
			continue;

		if (strcmp(names + ep->name_offset, arc_type))
			continue;

		return ep - base;
719
	}
720 721

	return MDESC_NODE_NULL;
722
}
723
EXPORT_SYMBOL(mdesc_next_arc);
724

725
u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
726
{
727 728 729 730 731
	struct mdesc_elem *ep, *base = node_block(&hp->mdesc);

	ep = base + arc;

	return ep->d.val;
732
}
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
EXPORT_SYMBOL(mdesc_arc_target);

const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
{
	struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
	const char *names = name_block(&hp->mdesc);
	u64 last_node = hp->mdesc.node_sz / 16;

	if (node == MDESC_NODE_NULL || node >= last_node)
		return NULL;

	ep = base + node;
	if (ep->tag != MD_NODE)
		return NULL;

	return names + ep->name_offset;
}
EXPORT_SYMBOL(mdesc_node_name);
751

752 753
static u64 max_cpus = 64;

754 755
static void __init report_platform_properties(void)
{
756 757
	struct mdesc_handle *hp = mdesc_grab();
	u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
758 759 760
	const char *s;
	const u64 *v;

761
	if (pn == MDESC_NODE_NULL) {
762 763 764 765
		prom_printf("No platform node in machine-description.\n");
		prom_halt();
	}

766
	s = mdesc_get_property(hp, pn, "banner-name", NULL);
767
	printk("PLATFORM: banner-name [%s]\n", s);
768
	s = mdesc_get_property(hp, pn, "name", NULL);
769 770
	printk("PLATFORM: name [%s]\n", s);

771
	v = mdesc_get_property(hp, pn, "hostid", NULL);
772
	if (v)
773
		printk("PLATFORM: hostid [%08llx]\n", *v);
774
	v = mdesc_get_property(hp, pn, "serial#", NULL);
775
	if (v)
776
		printk("PLATFORM: serial# [%08llx]\n", *v);
777
	v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
778
	printk("PLATFORM: stick-frequency [%08llx]\n", *v);
779
	v = mdesc_get_property(hp, pn, "mac-address", NULL);
780
	if (v)
781
		printk("PLATFORM: mac-address [%llx]\n", *v);
782
	v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
783
	if (v)
784
		printk("PLATFORM: watchdog-resolution [%llu ms]\n", *v);
785
	v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
786
	if (v)
787
		printk("PLATFORM: watchdog-max-timeout [%llu ms]\n", *v);
788
	v = mdesc_get_property(hp, pn, "max-cpus", NULL);
789 790 791 792
	if (v) {
		max_cpus = *v;
		printk("PLATFORM: max-cpus [%llu]\n", max_cpus);
	}
793

794 795 796 797 798 799 800 801 802 803 804 805
#ifdef CONFIG_SMP
	{
		int max_cpu, i;

		if (v) {
			max_cpu = *v;
			if (max_cpu > NR_CPUS)
				max_cpu = NR_CPUS;
		} else {
			max_cpu = NR_CPUS;
		}
		for (i = 0; i < max_cpu; i++)
806
			set_cpu_possible(i, true);
807 808 809
	}
#endif

810
	mdesc_release(hp);
811 812
}

813
static void fill_in_one_cache(cpuinfo_sparc *c, struct mdesc_handle *hp, u64 mp)
814
{
815 816 817
	const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
	const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
	const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
818 819 820
	const char *type;
	int type_len;

821
	type = mdesc_get_property(hp, mp, "type", &type_len);
822 823 824

	switch (*level) {
	case 1:
825
		if (of_find_in_proplist(type, "instn", type_len)) {
826 827
			c->icache_size = *size;
			c->icache_line_size = *line_size;
828
		} else if (of_find_in_proplist(type, "data", type_len)) {
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
			c->dcache_size = *size;
			c->dcache_line_size = *line_size;
		}
		break;

	case 2:
		c->ecache_size = *size;
		c->ecache_line_size = *line_size;
		break;

	default:
		break;
	}

	if (*level == 1) {
844
		u64 a;
845

846 847 848
		mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
			u64 target = mdesc_arc_target(hp, a);
			const char *name = mdesc_node_name(hp, target);
849

850 851
			if (!strcmp(name, "cache"))
				fill_in_one_cache(c, hp, target);
852 853 854 855
		}
	}
}

856 857 858 859
static void find_back_node_value(struct mdesc_handle *hp, u64 node,
				 char *srch_val,
				 void (*func)(struct mdesc_handle *, u64, int),
				 u64 val, int depth)
860
{
861
	u64 arc;
862

863 864 865
	/* Since we have an estimate of recursion depth, do a sanity check. */
	if (depth == 0)
		return;
866

867 868 869
	mdesc_for_each_arc(arc, hp, node, MDESC_ARC_TYPE_BACK) {
		u64 n = mdesc_arc_target(hp, arc);
		const char *name = mdesc_node_name(hp, n);
870

871 872
		if (!strcmp(srch_val, name))
			(*func)(hp, n, val);
873

874
		find_back_node_value(hp, n, srch_val, func, val, depth-1);
875 876 877
	}
}

878 879 880 881 882 883 884 885 886
static void __mark_core_id(struct mdesc_handle *hp, u64 node,
			   int core_id)
{
	const u64 *id = mdesc_get_property(hp, node, "id", NULL);

	if (*id < num_possible_cpus())
		cpu_data(*id).core_id = core_id;
}

887 888
static void __mark_max_cache_id(struct mdesc_handle *hp, u64 node,
				int max_cache_id)
889 890 891
{
	const u64 *id = mdesc_get_property(hp, node, "id", NULL);

892 893 894 895 896 897 898 899 900
	if (*id < num_possible_cpus()) {
		cpu_data(*id).max_cache_id = max_cache_id;

		/**
		 * On systems without explicit socket descriptions socket
		 * is max_cache_id
		 */
		cpu_data(*id).sock_id = max_cache_id;
	}
901 902 903 904 905 906 907 908
}

static void mark_core_ids(struct mdesc_handle *hp, u64 mp,
			  int core_id)
{
	find_back_node_value(hp, mp, "cpu", __mark_core_id, core_id, 10);
}

909 910
static void mark_max_cache_ids(struct mdesc_handle *hp, u64 mp,
			       int max_cache_id)
911
{
912 913
	find_back_node_value(hp, mp, "cpu", __mark_max_cache_id,
			     max_cache_id, 10);
914 915
}

916
static void set_core_ids(struct mdesc_handle *hp)
917 918
{
	int idx;
919
	u64 mp;
920 921

	idx = 1;
922 923 924 925

	/* Identify unique cores by looking for cpus backpointed to by
	 * level 1 instruction caches.
	 */
926 927
	mdesc_for_each_node_by_name(hp, mp, "cache") {
		const u64 *level;
928 929 930
		const char *type;
		int len;

931
		level = mdesc_get_property(hp, mp, "level", NULL);
932 933 934
		if (*level != 1)
			continue;

935
		type = mdesc_get_property(hp, mp, "type", &len);
936
		if (!of_find_in_proplist(type, "instn", len))
937 938
			continue;

939
		mark_core_ids(hp, mp, idx);
940 941 942 943
		idx++;
	}
}

944
static int set_max_cache_ids_by_cache(struct mdesc_handle *hp, int level)
945 946 947 948 949
{
	u64 mp;
	int idx = 1;
	int fnd = 0;

950 951 952
	/**
	 * Identify unique highest level of shared cache by looking for cpus
	 * backpointed to by shared level N caches.
953 954 955 956 957 958 959
	 */
	mdesc_for_each_node_by_name(hp, mp, "cache") {
		const u64 *cur_lvl;

		cur_lvl = mdesc_get_property(hp, mp, "level", NULL);
		if (*cur_lvl != level)
			continue;
960
		mark_max_cache_ids(hp, mp, idx);
961 962 963 964 965 966 967 968 969
		idx++;
		fnd = 1;
	}
	return fnd;
}

static void set_sock_ids_by_socket(struct mdesc_handle *hp, u64 mp)
{
	int idx = 1;
970

971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
	mdesc_for_each_node_by_name(hp, mp, "socket") {
		u64 a;

		mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
			u64 t = mdesc_arc_target(hp, a);
			const char *name;
			const u64 *id;

			name = mdesc_node_name(hp, t);
			if (strcmp(name, "cpu"))
				continue;

			id = mdesc_get_property(hp, t, "id", NULL);
			if (*id < num_possible_cpus())
				cpu_data(*id).sock_id = idx;
		}
987 988 989 990
		idx++;
	}
}

991 992 993 994
static void set_sock_ids(struct mdesc_handle *hp)
{
	u64 mp;

995 996 997
	/**
	 * Find the highest level of shared cache which pre-T7 is also
	 * the socket.
998
	 */
999 1000 1001 1002
	if (!set_max_cache_ids_by_cache(hp, 3))
		set_max_cache_ids_by_cache(hp, 2);

	/* If machine description exposes sockets data use it.*/
1003 1004
	mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "sockets");
	if (mp != MDESC_NODE_NULL)
1005
		set_sock_ids_by_socket(hp, mp);
1006 1007
}

1008
static void mark_proc_ids(struct mdesc_handle *hp, u64 mp, int proc_id)
1009
{
1010
	u64 a;
1011

1012 1013 1014
	mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
		u64 t = mdesc_arc_target(hp, a);
		const char *name;
1015 1016
		const u64 *id;

1017 1018
		name = mdesc_node_name(hp, t);
		if (strcmp(name, "cpu"))
1019 1020
			continue;

1021
		id = mdesc_get_property(hp, t, "id", NULL);
1022 1023 1024 1025 1026
		if (*id < NR_CPUS)
			cpu_data(*id).proc_id = proc_id;
	}
}

1027
static void __set_proc_ids(struct mdesc_handle *hp, const char *exec_unit_name)
1028 1029
{
	int idx;
1030
	u64 mp;
1031 1032

	idx = 0;
1033
	mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
1034 1035 1036
		const char *type;
		int len;

1037
		type = mdesc_get_property(hp, mp, "type", &len);
1038 1039
		if (!of_find_in_proplist(type, "int", len) &&
		    !of_find_in_proplist(type, "integer", len))
1040 1041
			continue;

1042
		mark_proc_ids(hp, mp, idx);
1043 1044 1045 1046
		idx++;
	}
}

1047
static void set_proc_ids(struct mdesc_handle *hp)
1048
{
1049 1050
	__set_proc_ids(hp, "exec_unit");
	__set_proc_ids(hp, "exec-unit");
1051 1052
}

1053 1054
static void get_one_mondo_bits(const u64 *p, unsigned int *mask,
			       unsigned long def, unsigned long max)
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
{
	u64 val;

	if (!p)
		goto use_default;
	val = *p;

	if (!val || val >= 64)
		goto use_default;

1065 1066 1067
	if (val > max)
		val = max;

1068 1069 1070 1071 1072 1073 1074
	*mask = ((1U << val) * 64U) - 1U;
	return;

use_default:
	*mask = ((1U << def) * 64U) - 1U;
}

1075 1076
static void get_mondo_data(struct mdesc_handle *hp, u64 mp,
			   struct trap_per_cpu *tb)
1077
{
1078
	static int printed;
1079 1080
	const u64 *val;

1081
	val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
1082
	get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7, ilog2(max_cpus * 2));
1083

1084
	val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
1085
	get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7, 8);
1086

1087
	val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
1088
	get_one_mondo_bits(val, &tb->resum_qmask, 6, 7);
1089

1090
	val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
1091 1092 1093 1094 1095 1096 1097 1098 1099
	get_one_mondo_bits(val, &tb->nonresum_qmask, 2, 2);
	if (!printed++) {
		pr_info("SUN4V: Mondo queue sizes "
			"[cpu(%u) dev(%u) r(%u) nr(%u)]\n",
			tb->cpu_mondo_qmask + 1,
			tb->dev_mondo_qmask + 1,
			tb->resum_qmask + 1,
			tb->nonresum_qmask + 1);
	}
1100 1101
}

1102
static void *mdesc_iterate_over_cpus(void *(*func)(struct mdesc_handle *, u64, int, void *), void *arg, cpumask_t *mask)
1103
{
1104
	struct mdesc_handle *hp = mdesc_grab();
1105
	void *ret = NULL;
1106
	u64 mp;
1107

1108 1109
	mdesc_for_each_node_by_name(hp, mp, "cpu") {
		const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
1110
		int cpuid = *id;
1111 1112

#ifdef CONFIG_SMP
1113 1114 1115 1116
		if (cpuid >= NR_CPUS) {
			printk(KERN_WARNING "Ignoring CPU %d which is "
			       ">= NR_CPUS (%d)\n",
			       cpuid, NR_CPUS);
1117
			continue;
1118
		}
1119
		if (!cpumask_test_cpu(cpuid, mask))
1120 1121 1122
			continue;
#endif

1123 1124 1125 1126 1127 1128 1129 1130
		ret = func(hp, mp, cpuid, arg);
		if (ret)
			goto out;
	}
out:
	mdesc_release(hp);
	return ret;
}
1131

1132 1133
static void *record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
			    void *arg)
1134 1135 1136 1137 1138 1139 1140
{
	ncpus_probed++;
#ifdef CONFIG_SMP
	set_cpu_present(cpuid, true);
#endif
	return NULL;
}
1141

1142
void mdesc_populate_present_mask(cpumask_t *mask)
1143 1144 1145
{
	if (tlb_type != hypervisor)
		return;
1146

1147 1148 1149
	ncpus_probed = 0;
	mdesc_iterate_over_cpus(record_one_cpu, NULL, mask);
}
1150

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
static void * __init check_one_pgsz(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
{
	const u64 *pgsz_prop = mdesc_get_property(hp, mp, "mmu-page-size-list", NULL);
	unsigned long *pgsz_mask = arg;
	u64 val;

	val = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
	       HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
	if (pgsz_prop)
		val = *pgsz_prop;

	if (!*pgsz_mask)
		*pgsz_mask = val;
	else
		*pgsz_mask &= val;
	return NULL;
}

void __init mdesc_get_page_sizes(cpumask_t *mask, unsigned long *pgsz_mask)
{
	*pgsz_mask = 0;
	mdesc_iterate_over_cpus(check_one_pgsz, pgsz_mask, mask);
}

1175 1176
static void *fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
			     void *arg)
1177 1178 1179 1180 1181
{
	const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
	struct trap_per_cpu *tb;
	cpuinfo_sparc *c;
	u64 a;
1182

1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
#ifndef CONFIG_SMP
	/* On uniprocessor we only want the values for the
	 * real physical cpu the kernel booted onto, however
	 * cpu_data() only has one entry at index 0.
	 */
	if (cpuid != real_hard_smp_processor_id())
		return NULL;
	cpuid = 0;
#endif

	c = &cpu_data(cpuid);
	c->clock_tick = *cfreq;

	tb = &trap_block[cpuid];
	get_mondo_data(hp, mp, tb);

	mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
		u64 j, t = mdesc_arc_target(hp, a);
		const char *t_name;

		t_name = mdesc_node_name(hp, t);
		if (!strcmp(t_name, "cache")) {
			fill_in_one_cache(c, hp, t);
			continue;
1207 1208
		}

1209 1210 1211
		mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
			u64 n = mdesc_arc_target(hp, j);
			const char *n_name;
1212

1213 1214 1215 1216
			n_name = mdesc_node_name(hp, n);
			if (!strcmp(n_name, "cache"))
				fill_in_one_cache(c, hp, n);
		}
1217 1218
	}

1219 1220 1221 1222 1223 1224
	c->core_id = 0;
	c->proc_id = -1;

	return NULL;
}

1225
void mdesc_fill_in_cpu_data(cpumask_t *mask)
1226 1227 1228
{
	struct mdesc_handle *hp;

1229
	mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask);
1230 1231 1232

	hp = mdesc_grab();

1233 1234
	set_core_ids(hp);
	set_proc_ids(hp);
1235
	set_sock_ids(hp);
1236

1237
	mdesc_release(hp);
1238 1239

	smp_fill_in_sib_core_maps();
1240 1241
}

1242 1243 1244 1245 1246 1247
/* mdesc_open() - Grab a reference to mdesc_handle when /dev/mdesc is
 * opened. Hold this reference until /dev/mdesc is closed to ensure
 * mdesc data structure is not released underneath us. Store the
 * pointer to mdesc structure in private_data for read and seek to use
 */
static int mdesc_open(struct inode *inode, struct file *file)
1248 1249 1250 1251 1252 1253
{
	struct mdesc_handle *hp = mdesc_grab();

	if (!hp)
		return -ENODEV;

1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
	file->private_data = hp;

	return 0;
}

static ssize_t mdesc_read(struct file *file, char __user *buf,
			  size_t len, loff_t *offp)
{
	struct mdesc_handle *hp = file->private_data;
	unsigned char *mdesc;
	int bytes_left, count = len;

	if (*offp >= hp->handle_size)
		return 0;

	bytes_left = hp->handle_size - *offp;
	if (count > bytes_left)
		count = bytes_left;

	mdesc = (unsigned char *)&hp->mdesc;
	mdesc += *offp;
	if (!copy_to_user(buf, mdesc, count)) {
		*offp += count;
		return count;
	} else {
		return -EFAULT;
	}
}
1282

1283 1284
static loff_t mdesc_llseek(struct file *file, loff_t offset, int whence)
{
1285
	struct mdesc_handle *hp = file->private_data;
1286

1287
	return no_seek_end_llseek_size(file, offset, whence, hp->handle_size);
1288 1289 1290 1291 1292 1293 1294 1295 1296
}

/* mdesc_close() - /dev/mdesc is being closed, release the reference to
 * mdesc structure.
 */
static int mdesc_close(struct inode *inode, struct file *file)
{
	mdesc_release(file->private_data);
	return 0;
1297 1298 1299
}

static const struct file_operations mdesc_fops = {
1300 1301 1302 1303 1304
	.open    = mdesc_open,
	.read	 = mdesc_read,
	.llseek  = mdesc_llseek,
	.release = mdesc_close,
	.owner	 = THIS_MODULE,
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
};

static struct miscdevice mdesc_misc = {
	.minor	= MISC_DYNAMIC_MINOR,
	.name	= "mdesc",
	.fops	= &mdesc_fops,
};

static int __init mdesc_misc_init(void)
{
	return misc_register(&mdesc_misc);
}

__initcall(mdesc_misc_init);

1320 1321
void __init sun4v_mdesc_init(void)
{
1322
	struct mdesc_handle *hp;
1323 1324 1325 1326 1327 1328
	unsigned long len, real_len, status;

	(void) sun4v_mach_desc(0UL, 0UL, &len);

	printk("MDESC: Size is %lu bytes.\n", len);

Y
Yinghai Lu 已提交
1329
	hp = mdesc_alloc(len, &memblock_mdesc_ops);
1330 1331 1332 1333
	if (hp == NULL) {
		prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
		prom_halt();
	}
1334

1335
	status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
1336 1337 1338 1339
	if (status != HV_EOK || real_len > len) {
		prom_printf("sun4v_mach_desc fails, err(%lu), "
			    "len(%lu), real_len(%lu)\n",
			    status, len, real_len);
1340
		mdesc_free(hp);
1341 1342 1343
		prom_halt();
	}

1344
	cur_mdesc = hp;
1345 1346 1347

	report_platform_properties();
}