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