blob: dde52bcf5c64e11daa1cd22558238ebd45eb46b9 [file] [log] [blame]
David S. Miller5cbc3072007-05-25 15:49:59 -07001/* mdesc.c: Sun4V machine description handling.
2 *
David S. Miller4a283332008-02-13 19:22:23 -08003 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
David S. Miller5cbc3072007-05-25 15:49:59 -07004 */
5#include <linux/kernel.h>
6#include <linux/types.h>
David S. Miller4a283332008-02-13 19:22:23 -08007#include <linux/lmb.h>
David S. Miller5cbc3072007-05-25 15:49:59 -07008#include <linux/log2.h>
David S. Miller43fdf272007-07-12 13:47:50 -07009#include <linux/list.h>
10#include <linux/slab.h>
David S. Miller8f3fff22007-07-14 00:54:55 -070011#include <linux/mm.h>
David S. Miller0fdb7f92007-08-15 21:02:23 -070012#include <linux/miscdevice.h>
David S. Miller5cbc3072007-05-25 15:49:59 -070013
14#include <asm/hypervisor.h>
15#include <asm/mdesc.h>
16#include <asm/prom.h>
17#include <asm/oplib.h>
18#include <asm/smp.h>
19
20/* Unlike the OBP device tree, the machine description is a full-on
21 * DAG. An arbitrary number of ARCs are possible from one
22 * node to other nodes and thus we can't use the OBP device_node
23 * data structure to represent these nodes inside of the kernel.
24 *
25 * Actually, it isn't even a DAG, because there are back pointers
26 * which create cycles in the graph.
27 *
28 * mdesc_hdr and mdesc_elem describe the layout of the data structure
29 * we get from the Hypervisor.
30 */
31struct mdesc_hdr {
32 u32 version; /* Transport version */
33 u32 node_sz; /* node block size */
34 u32 name_sz; /* name block size */
35 u32 data_sz; /* data block size */
David S. Miller43fdf272007-07-12 13:47:50 -070036} __attribute__((aligned(16)));
David S. Miller5cbc3072007-05-25 15:49:59 -070037
38struct mdesc_elem {
39 u8 tag;
40#define MD_LIST_END 0x00
41#define MD_NODE 0x4e
42#define MD_NODE_END 0x45
43#define MD_NOOP 0x20
44#define MD_PROP_ARC 0x61
45#define MD_PROP_VAL 0x76
46#define MD_PROP_STR 0x73
47#define MD_PROP_DATA 0x64
48 u8 name_len;
49 u16 resv;
50 u32 name_offset;
51 union {
52 struct {
53 u32 data_len;
54 u32 data_offset;
55 } data;
56 u64 val;
57 } d;
58};
59
David S. Miller43fdf272007-07-12 13:47:50 -070060struct mdesc_mem_ops {
61 struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
62 void (*free)(struct mdesc_handle *handle);
63};
David S. Miller5cbc3072007-05-25 15:49:59 -070064
David S. Miller43fdf272007-07-12 13:47:50 -070065struct mdesc_handle {
66 struct list_head list;
67 struct mdesc_mem_ops *mops;
68 void *self_base;
69 atomic_t refcnt;
70 unsigned int handle_size;
71 struct mdesc_hdr mdesc;
72};
David S. Miller5cbc3072007-05-25 15:49:59 -070073
David S. Miller43fdf272007-07-12 13:47:50 -070074static void mdesc_handle_init(struct mdesc_handle *hp,
75 unsigned int handle_size,
76 void *base)
David S. Miller5cbc3072007-05-25 15:49:59 -070077{
David S. Miller43fdf272007-07-12 13:47:50 -070078 BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));
79
80 memset(hp, 0, handle_size);
81 INIT_LIST_HEAD(&hp->list);
82 hp->self_base = base;
83 atomic_set(&hp->refcnt, 1);
84 hp->handle_size = handle_size;
David S. Miller5cbc3072007-05-25 15:49:59 -070085}
86
David S. Miller4a283332008-02-13 19:22:23 -080087static struct mdesc_handle * __init mdesc_lmb_alloc(unsigned int mdesc_size)
David S. Miller5cbc3072007-05-25 15:49:59 -070088{
David S. Miller43fdf272007-07-12 13:47:50 -070089 unsigned int handle_size, alloc_size;
David S. Miller4a283332008-02-13 19:22:23 -080090 struct mdesc_handle *hp;
91 unsigned long paddr;
David S. Miller5cbc3072007-05-25 15:49:59 -070092
David S. Miller43fdf272007-07-12 13:47:50 -070093 handle_size = (sizeof(struct mdesc_handle) -
94 sizeof(struct mdesc_hdr) +
95 mdesc_size);
96 alloc_size = PAGE_ALIGN(handle_size);
David S. Miller5cbc3072007-05-25 15:49:59 -070097
David S. Miller4a283332008-02-13 19:22:23 -080098 paddr = lmb_alloc(alloc_size, PAGE_SIZE);
David S. Miller43fdf272007-07-12 13:47:50 -070099
David S. Miller4a283332008-02-13 19:22:23 -0800100 hp = NULL;
101 if (paddr) {
102 hp = __va(paddr);
103 mdesc_handle_init(hp, handle_size, hp);
104 }
David S. Miller43fdf272007-07-12 13:47:50 -0700105 return hp;
106}
107
David S. Miller4a283332008-02-13 19:22:23 -0800108static void mdesc_lmb_free(struct mdesc_handle *hp)
David S. Miller43fdf272007-07-12 13:47:50 -0700109{
110 unsigned int alloc_size, handle_size = hp->handle_size;
111 unsigned long start, end;
112
113 BUG_ON(atomic_read(&hp->refcnt) != 0);
114 BUG_ON(!list_empty(&hp->list));
115
116 alloc_size = PAGE_ALIGN(handle_size);
117
118 start = (unsigned long) hp;
119 end = start + alloc_size;
120
121 while (start < end) {
122 struct page *p;
123
124 p = virt_to_page(start);
125 ClearPageReserved(p);
126 __free_page(p);
127 start += PAGE_SIZE;
David S. Miller5cbc3072007-05-25 15:49:59 -0700128 }
129}
130
David S. Miller4a283332008-02-13 19:22:23 -0800131static struct mdesc_mem_ops lmb_mdesc_ops = {
132 .alloc = mdesc_lmb_alloc,
133 .free = mdesc_lmb_free,
David S. Miller43fdf272007-07-12 13:47:50 -0700134};
135
136static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
David S. Miller5cbc3072007-05-25 15:49:59 -0700137{
David S. Miller43fdf272007-07-12 13:47:50 -0700138 unsigned int handle_size;
139 void *base;
David S. Miller5cbc3072007-05-25 15:49:59 -0700140
David S. Miller43fdf272007-07-12 13:47:50 -0700141 handle_size = (sizeof(struct mdesc_handle) -
142 sizeof(struct mdesc_hdr) +
143 mdesc_size);
David S. Miller5cbc3072007-05-25 15:49:59 -0700144
David S. Miller920c3ed2007-07-17 21:37:35 -0700145 base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_NOFAIL);
David S. Miller43fdf272007-07-12 13:47:50 -0700146 if (base) {
147 struct mdesc_handle *hp;
148 unsigned long addr;
149
150 addr = (unsigned long)base;
151 addr = (addr + 15UL) & ~15UL;
152 hp = (struct mdesc_handle *) addr;
153
154 mdesc_handle_init(hp, handle_size, base);
155 return hp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700156 }
David S. Miller43fdf272007-07-12 13:47:50 -0700157
David S. Miller5cbc3072007-05-25 15:49:59 -0700158 return NULL;
159}
160
David S. Miller43fdf272007-07-12 13:47:50 -0700161static void mdesc_kfree(struct mdesc_handle *hp)
David S. Miller5cbc3072007-05-25 15:49:59 -0700162{
David S. Miller43fdf272007-07-12 13:47:50 -0700163 BUG_ON(atomic_read(&hp->refcnt) != 0);
164 BUG_ON(!list_empty(&hp->list));
David S. Miller5cbc3072007-05-25 15:49:59 -0700165
David S. Miller43fdf272007-07-12 13:47:50 -0700166 kfree(hp->self_base);
167}
168
169static struct mdesc_mem_ops kmalloc_mdesc_memops = {
170 .alloc = mdesc_kmalloc,
171 .free = mdesc_kfree,
172};
173
174static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
175 struct mdesc_mem_ops *mops)
176{
177 struct mdesc_handle *hp = mops->alloc(mdesc_size);
178
179 if (hp)
180 hp->mops = mops;
181
182 return hp;
183}
184
185static void mdesc_free(struct mdesc_handle *hp)
186{
187 hp->mops->free(hp);
188}
189
190static struct mdesc_handle *cur_mdesc;
191static LIST_HEAD(mdesc_zombie_list);
192static DEFINE_SPINLOCK(mdesc_lock);
193
194struct mdesc_handle *mdesc_grab(void)
195{
196 struct mdesc_handle *hp;
197 unsigned long flags;
198
199 spin_lock_irqsave(&mdesc_lock, flags);
200 hp = cur_mdesc;
201 if (hp)
202 atomic_inc(&hp->refcnt);
203 spin_unlock_irqrestore(&mdesc_lock, flags);
204
205 return hp;
206}
207EXPORT_SYMBOL(mdesc_grab);
208
209void mdesc_release(struct mdesc_handle *hp)
210{
211 unsigned long flags;
212
213 spin_lock_irqsave(&mdesc_lock, flags);
214 if (atomic_dec_and_test(&hp->refcnt)) {
215 list_del_init(&hp->list);
216 hp->mops->free(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700217 }
David S. Miller43fdf272007-07-12 13:47:50 -0700218 spin_unlock_irqrestore(&mdesc_lock, flags);
David S. Miller5cbc3072007-05-25 15:49:59 -0700219}
David S. Miller43fdf272007-07-12 13:47:50 -0700220EXPORT_SYMBOL(mdesc_release);
David S. Miller5cbc3072007-05-25 15:49:59 -0700221
David S. Miller920c3ed2007-07-17 21:37:35 -0700222static DEFINE_MUTEX(mdesc_mutex);
223static struct mdesc_notifier_client *client_list;
224
225void mdesc_register_notifier(struct mdesc_notifier_client *client)
226{
227 u64 node;
228
229 mutex_lock(&mdesc_mutex);
230 client->next = client_list;
231 client_list = client;
232
233 mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
234 client->add(cur_mdesc, node);
235
236 mutex_unlock(&mdesc_mutex);
237}
238
David S. Miller91ba3c22007-07-18 15:15:45 -0700239static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
240{
241 const u64 *id;
242 u64 a;
243
244 id = NULL;
245 mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
246 u64 target;
247
248 target = mdesc_arc_target(hp, a);
249 id = mdesc_get_property(hp, target,
250 "cfg-handle", NULL);
251 if (id)
252 break;
253 }
254
255 return id;
256}
257
David S. Miller920c3ed2007-07-17 21:37:35 -0700258/* Run 'func' on nodes which are in A but not in B. */
259static void invoke_on_missing(const char *name,
260 struct mdesc_handle *a,
261 struct mdesc_handle *b,
262 void (*func)(struct mdesc_handle *, u64))
263{
264 u64 node;
265
266 mdesc_for_each_node_by_name(a, node, name) {
David S. Miller91ba3c22007-07-18 15:15:45 -0700267 int found = 0, is_vdc_port = 0;
268 const char *name_prop;
269 const u64 *id;
David S. Miller920c3ed2007-07-17 21:37:35 -0700270 u64 fnode;
271
David S. Miller91ba3c22007-07-18 15:15:45 -0700272 name_prop = mdesc_get_property(a, node, "name", NULL);
273 if (name_prop && !strcmp(name_prop, "vdc-port")) {
274 is_vdc_port = 1;
275 id = parent_cfg_handle(a, node);
276 } else
277 id = mdesc_get_property(a, node, "id", NULL);
278
279 if (!id) {
280 printk(KERN_ERR "MD: Cannot find ID for %s node.\n",
281 (name_prop ? name_prop : name));
282 continue;
283 }
284
David S. Miller920c3ed2007-07-17 21:37:35 -0700285 mdesc_for_each_node_by_name(b, fnode, name) {
David S. Miller91ba3c22007-07-18 15:15:45 -0700286 const u64 *fid;
287
288 if (is_vdc_port) {
289 name_prop = mdesc_get_property(b, fnode,
290 "name", NULL);
291 if (!name_prop ||
292 strcmp(name_prop, "vdc-port"))
293 continue;
294 fid = parent_cfg_handle(b, fnode);
295 if (!fid) {
296 printk(KERN_ERR "MD: Cannot find ID "
297 "for vdc-port node.\n");
298 continue;
299 }
300 } else
301 fid = mdesc_get_property(b, fnode,
302 "id", NULL);
David S. Miller920c3ed2007-07-17 21:37:35 -0700303
304 if (*id == *fid) {
305 found = 1;
306 break;
307 }
308 }
309 if (!found)
310 func(a, node);
311 }
312}
313
314static void notify_one(struct mdesc_notifier_client *p,
315 struct mdesc_handle *old_hp,
316 struct mdesc_handle *new_hp)
317{
318 invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
319 invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
320}
321
322static void mdesc_notify_clients(struct mdesc_handle *old_hp,
323 struct mdesc_handle *new_hp)
324{
325 struct mdesc_notifier_client *p = client_list;
326
327 while (p) {
328 notify_one(p, old_hp, new_hp);
329 p = p->next;
330 }
331}
332
David S. Miller778feeb42007-07-16 16:50:36 -0700333void mdesc_update(void)
David S. Miller5cbc3072007-05-25 15:49:59 -0700334{
David S. Miller43fdf272007-07-12 13:47:50 -0700335 unsigned long len, real_len, status;
336 struct mdesc_handle *hp, *orig_hp;
337 unsigned long flags;
David S. Miller5cbc3072007-05-25 15:49:59 -0700338
David S. Miller920c3ed2007-07-17 21:37:35 -0700339 mutex_lock(&mdesc_mutex);
340
David S. Miller43fdf272007-07-12 13:47:50 -0700341 (void) sun4v_mach_desc(0UL, 0UL, &len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700342
David S. Miller43fdf272007-07-12 13:47:50 -0700343 hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
344 if (!hp) {
345 printk(KERN_ERR "MD: mdesc alloc fails\n");
David S. Miller920c3ed2007-07-17 21:37:35 -0700346 goto out;
David S. Miller5cbc3072007-05-25 15:49:59 -0700347 }
348
David S. Miller43fdf272007-07-12 13:47:50 -0700349 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
350 if (status != HV_EOK || real_len > len) {
351 printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
352 status);
353 atomic_dec(&hp->refcnt);
354 mdesc_free(hp);
David S. Miller920c3ed2007-07-17 21:37:35 -0700355 goto out;
David S. Miller5cbc3072007-05-25 15:49:59 -0700356 }
David S. Miller43fdf272007-07-12 13:47:50 -0700357
358 spin_lock_irqsave(&mdesc_lock, flags);
359 orig_hp = cur_mdesc;
360 cur_mdesc = hp;
David S. Miller920c3ed2007-07-17 21:37:35 -0700361 spin_unlock_irqrestore(&mdesc_lock, flags);
David S. Miller43fdf272007-07-12 13:47:50 -0700362
David S. Miller920c3ed2007-07-17 21:37:35 -0700363 mdesc_notify_clients(orig_hp, hp);
364
365 spin_lock_irqsave(&mdesc_lock, flags);
David S. Miller43fdf272007-07-12 13:47:50 -0700366 if (atomic_dec_and_test(&orig_hp->refcnt))
367 mdesc_free(orig_hp);
368 else
369 list_add(&orig_hp->list, &mdesc_zombie_list);
370 spin_unlock_irqrestore(&mdesc_lock, flags);
David S. Miller920c3ed2007-07-17 21:37:35 -0700371
372out:
373 mutex_unlock(&mdesc_mutex);
David S. Miller5cbc3072007-05-25 15:49:59 -0700374}
375
David S. Miller43fdf272007-07-12 13:47:50 -0700376static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700377{
378 return (struct mdesc_elem *) (mdesc + 1);
379}
380
David S. Miller43fdf272007-07-12 13:47:50 -0700381static void *name_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700382{
383 return ((void *) node_block(mdesc)) + mdesc->node_sz;
384}
385
David S. Miller43fdf272007-07-12 13:47:50 -0700386static void *data_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700387{
388 return ((void *) name_block(mdesc)) + mdesc->name_sz;
389}
390
David S. Miller43fdf272007-07-12 13:47:50 -0700391u64 mdesc_node_by_name(struct mdesc_handle *hp,
392 u64 from_node, const char *name)
David S. Miller5cbc3072007-05-25 15:49:59 -0700393{
David S. Miller43fdf272007-07-12 13:47:50 -0700394 struct mdesc_elem *ep = node_block(&hp->mdesc);
395 const char *names = name_block(&hp->mdesc);
396 u64 last_node = hp->mdesc.node_sz / 16;
397 u64 ret;
David S. Miller5cbc3072007-05-25 15:49:59 -0700398
David S. Miller778feeb42007-07-16 16:50:36 -0700399 if (from_node == MDESC_NODE_NULL) {
400 ret = from_node = 0;
401 } else if (from_node >= last_node) {
David S. Miller43fdf272007-07-12 13:47:50 -0700402 return MDESC_NODE_NULL;
David S. Miller778feeb42007-07-16 16:50:36 -0700403 } else {
404 ret = ep[from_node].d.val;
405 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700406
David S. Miller43fdf272007-07-12 13:47:50 -0700407 while (ret < last_node) {
408 if (ep[ret].tag != MD_NODE)
409 return MDESC_NODE_NULL;
410 if (!strcmp(names + ep[ret].name_offset, name))
411 break;
412 ret = ep[ret].d.val;
413 }
414 if (ret >= last_node)
415 ret = MDESC_NODE_NULL;
416 return ret;
417}
418EXPORT_SYMBOL(mdesc_node_by_name);
David S. Miller5cbc3072007-05-25 15:49:59 -0700419
David S. Miller43fdf272007-07-12 13:47:50 -0700420const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
421 const char *name, int *lenp)
422{
423 const char *names = name_block(&hp->mdesc);
424 u64 last_node = hp->mdesc.node_sz / 16;
425 void *data = data_block(&hp->mdesc);
426 struct mdesc_elem *ep;
427
428 if (node == MDESC_NODE_NULL || node >= last_node)
429 return NULL;
430
431 ep = node_block(&hp->mdesc) + node;
432 ep++;
433 for (; ep->tag != MD_NODE_END; ep++) {
434 void *val = NULL;
435 int len = 0;
436
437 switch (ep->tag) {
438 case MD_PROP_VAL:
439 val = &ep->d.val;
440 len = 8;
David S. Miller5cbc3072007-05-25 15:49:59 -0700441 break;
442
David S. Miller43fdf272007-07-12 13:47:50 -0700443 case MD_PROP_STR:
444 case MD_PROP_DATA:
445 val = data + ep->d.data.data_offset;
446 len = ep->d.data.data_len;
447 break;
David S. Miller5cbc3072007-05-25 15:49:59 -0700448
David S. Miller43fdf272007-07-12 13:47:50 -0700449 default:
David S. Miller5cbc3072007-05-25 15:49:59 -0700450 break;
451 }
David S. Miller43fdf272007-07-12 13:47:50 -0700452 if (!val)
453 continue;
David S. Miller5cbc3072007-05-25 15:49:59 -0700454
David S. Miller43fdf272007-07-12 13:47:50 -0700455 if (!strcmp(names + ep->name_offset, name)) {
456 if (lenp)
457 *lenp = len;
458 return val;
David S. Miller5cbc3072007-05-25 15:49:59 -0700459 }
460 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700461
David S. Miller43fdf272007-07-12 13:47:50 -0700462 return NULL;
463}
464EXPORT_SYMBOL(mdesc_get_property);
465
466u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
David S. Miller5cbc3072007-05-25 15:49:59 -0700467{
David S. Miller43fdf272007-07-12 13:47:50 -0700468 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
469 const char *names = name_block(&hp->mdesc);
470 u64 last_node = hp->mdesc.node_sz / 16;
David S. Miller5cbc3072007-05-25 15:49:59 -0700471
David S. Miller43fdf272007-07-12 13:47:50 -0700472 if (from == MDESC_NODE_NULL || from >= last_node)
473 return MDESC_NODE_NULL;
474
475 ep = base + from;
476
477 ep++;
478 for (; ep->tag != MD_NODE_END; ep++) {
479 if (ep->tag != MD_PROP_ARC)
480 continue;
481
482 if (strcmp(names + ep->name_offset, arc_type))
483 continue;
484
485 return ep - base;
David S. Miller5cbc3072007-05-25 15:49:59 -0700486 }
David S. Miller43fdf272007-07-12 13:47:50 -0700487
488 return MDESC_NODE_NULL;
David S. Miller5cbc3072007-05-25 15:49:59 -0700489}
David S. Miller43fdf272007-07-12 13:47:50 -0700490EXPORT_SYMBOL(mdesc_next_arc);
491
492u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
493{
494 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
495
496 ep = base + arc;
497
498 return ep->d.val;
499}
500EXPORT_SYMBOL(mdesc_arc_target);
501
502const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
503{
504 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
505 const char *names = name_block(&hp->mdesc);
506 u64 last_node = hp->mdesc.node_sz / 16;
507
508 if (node == MDESC_NODE_NULL || node >= last_node)
509 return NULL;
510
511 ep = base + node;
512 if (ep->tag != MD_NODE)
513 return NULL;
514
515 return names + ep->name_offset;
516}
517EXPORT_SYMBOL(mdesc_node_name);
David S. Miller5cbc3072007-05-25 15:49:59 -0700518
519static void __init report_platform_properties(void)
520{
David S. Miller43fdf272007-07-12 13:47:50 -0700521 struct mdesc_handle *hp = mdesc_grab();
522 u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
David S. Miller5cbc3072007-05-25 15:49:59 -0700523 const char *s;
524 const u64 *v;
525
David S. Miller43fdf272007-07-12 13:47:50 -0700526 if (pn == MDESC_NODE_NULL) {
David S. Miller5cbc3072007-05-25 15:49:59 -0700527 prom_printf("No platform node in machine-description.\n");
528 prom_halt();
529 }
530
David S. Miller43fdf272007-07-12 13:47:50 -0700531 s = mdesc_get_property(hp, pn, "banner-name", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700532 printk("PLATFORM: banner-name [%s]\n", s);
David S. Miller43fdf272007-07-12 13:47:50 -0700533 s = mdesc_get_property(hp, pn, "name", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700534 printk("PLATFORM: name [%s]\n", s);
535
David S. Miller43fdf272007-07-12 13:47:50 -0700536 v = mdesc_get_property(hp, pn, "hostid", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700537 if (v)
538 printk("PLATFORM: hostid [%08lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700539 v = mdesc_get_property(hp, pn, "serial#", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700540 if (v)
541 printk("PLATFORM: serial# [%08lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700542 v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700543 printk("PLATFORM: stick-frequency [%08lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700544 v = mdesc_get_property(hp, pn, "mac-address", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700545 if (v)
546 printk("PLATFORM: mac-address [%lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700547 v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700548 if (v)
549 printk("PLATFORM: watchdog-resolution [%lu ms]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700550 v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700551 if (v)
552 printk("PLATFORM: watchdog-max-timeout [%lu ms]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700553 v = mdesc_get_property(hp, pn, "max-cpus", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700554 if (v)
555 printk("PLATFORM: max-cpus [%lu]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700556
David S. Miller4f0234f2007-07-13 16:03:42 -0700557#ifdef CONFIG_SMP
558 {
559 int max_cpu, i;
560
561 if (v) {
562 max_cpu = *v;
563 if (max_cpu > NR_CPUS)
564 max_cpu = NR_CPUS;
565 } else {
566 max_cpu = NR_CPUS;
567 }
568 for (i = 0; i < max_cpu; i++)
569 cpu_set(i, cpu_possible_map);
570 }
571#endif
572
David S. Miller43fdf272007-07-12 13:47:50 -0700573 mdesc_release(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700574}
575
David S. Miller4f0234f2007-07-13 16:03:42 -0700576static void __devinit fill_in_one_cache(cpuinfo_sparc *c,
577 struct mdesc_handle *hp,
578 u64 mp)
David S. Miller5cbc3072007-05-25 15:49:59 -0700579{
David S. Miller43fdf272007-07-12 13:47:50 -0700580 const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
581 const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
582 const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700583 const char *type;
584 int type_len;
585
David S. Miller43fdf272007-07-12 13:47:50 -0700586 type = mdesc_get_property(hp, mp, "type", &type_len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700587
588 switch (*level) {
589 case 1:
David S. Miller46bcea72007-08-07 18:46:36 -0700590 if (of_find_in_proplist(type, "instn", type_len)) {
David S. Miller5cbc3072007-05-25 15:49:59 -0700591 c->icache_size = *size;
592 c->icache_line_size = *line_size;
David S. Miller46bcea72007-08-07 18:46:36 -0700593 } else if (of_find_in_proplist(type, "data", type_len)) {
David S. Miller5cbc3072007-05-25 15:49:59 -0700594 c->dcache_size = *size;
595 c->dcache_line_size = *line_size;
596 }
597 break;
598
599 case 2:
600 c->ecache_size = *size;
601 c->ecache_line_size = *line_size;
602 break;
603
604 default:
605 break;
606 }
607
608 if (*level == 1) {
David S. Miller43fdf272007-07-12 13:47:50 -0700609 u64 a;
David S. Miller5cbc3072007-05-25 15:49:59 -0700610
David S. Miller43fdf272007-07-12 13:47:50 -0700611 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
612 u64 target = mdesc_arc_target(hp, a);
613 const char *name = mdesc_node_name(hp, target);
David S. Miller5cbc3072007-05-25 15:49:59 -0700614
David S. Miller43fdf272007-07-12 13:47:50 -0700615 if (!strcmp(name, "cache"))
616 fill_in_one_cache(c, hp, target);
David S. Miller5cbc3072007-05-25 15:49:59 -0700617 }
618 }
619}
620
David S. Miller4f0234f2007-07-13 16:03:42 -0700621static void __devinit mark_core_ids(struct mdesc_handle *hp, u64 mp,
622 int core_id)
David S. Miller5cbc3072007-05-25 15:49:59 -0700623{
David S. Miller43fdf272007-07-12 13:47:50 -0700624 u64 a;
David S. Miller5cbc3072007-05-25 15:49:59 -0700625
David S. Miller43fdf272007-07-12 13:47:50 -0700626 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
627 u64 t = mdesc_arc_target(hp, a);
628 const char *name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700629 const u64 *id;
630
David S. Miller43fdf272007-07-12 13:47:50 -0700631 name = mdesc_node_name(hp, t);
632 if (!strcmp(name, "cpu")) {
633 id = mdesc_get_property(hp, t, "id", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700634 if (*id < NR_CPUS)
635 cpu_data(*id).core_id = core_id;
636 } else {
David S. Miller43fdf272007-07-12 13:47:50 -0700637 u64 j;
David S. Miller5cbc3072007-05-25 15:49:59 -0700638
David S. Miller43fdf272007-07-12 13:47:50 -0700639 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_BACK) {
640 u64 n = mdesc_arc_target(hp, j);
641 const char *n_name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700642
David S. Miller43fdf272007-07-12 13:47:50 -0700643 n_name = mdesc_node_name(hp, n);
644 if (strcmp(n_name, "cpu"))
David S. Miller5cbc3072007-05-25 15:49:59 -0700645 continue;
646
David S. Miller43fdf272007-07-12 13:47:50 -0700647 id = mdesc_get_property(hp, n, "id", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700648 if (*id < NR_CPUS)
649 cpu_data(*id).core_id = core_id;
650 }
651 }
652 }
653}
654
David S. Miller4f0234f2007-07-13 16:03:42 -0700655static void __devinit set_core_ids(struct mdesc_handle *hp)
David S. Miller5cbc3072007-05-25 15:49:59 -0700656{
David S. Miller5cbc3072007-05-25 15:49:59 -0700657 int idx;
David S. Miller43fdf272007-07-12 13:47:50 -0700658 u64 mp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700659
660 idx = 1;
David S. Miller43fdf272007-07-12 13:47:50 -0700661 mdesc_for_each_node_by_name(hp, mp, "cache") {
662 const u64 *level;
David S. Miller5cbc3072007-05-25 15:49:59 -0700663 const char *type;
664 int len;
665
David S. Miller43fdf272007-07-12 13:47:50 -0700666 level = mdesc_get_property(hp, mp, "level", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700667 if (*level != 1)
668 continue;
669
David S. Miller43fdf272007-07-12 13:47:50 -0700670 type = mdesc_get_property(hp, mp, "type", &len);
David S. Miller46bcea72007-08-07 18:46:36 -0700671 if (!of_find_in_proplist(type, "instn", len))
David S. Miller5cbc3072007-05-25 15:49:59 -0700672 continue;
673
David S. Miller43fdf272007-07-12 13:47:50 -0700674 mark_core_ids(hp, mp, idx);
David S. Miller5cbc3072007-05-25 15:49:59 -0700675
676 idx++;
677 }
678}
679
David S. Miller4f0234f2007-07-13 16:03:42 -0700680static void __devinit mark_proc_ids(struct mdesc_handle *hp, u64 mp,
681 int proc_id)
David S. Millerf78eae22007-06-04 17:01:39 -0700682{
David S. Miller43fdf272007-07-12 13:47:50 -0700683 u64 a;
David S. Millerf78eae22007-06-04 17:01:39 -0700684
David S. Miller43fdf272007-07-12 13:47:50 -0700685 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
686 u64 t = mdesc_arc_target(hp, a);
687 const char *name;
David S. Millerf78eae22007-06-04 17:01:39 -0700688 const u64 *id;
689
David S. Miller43fdf272007-07-12 13:47:50 -0700690 name = mdesc_node_name(hp, t);
691 if (strcmp(name, "cpu"))
David S. Millerf78eae22007-06-04 17:01:39 -0700692 continue;
693
David S. Miller43fdf272007-07-12 13:47:50 -0700694 id = mdesc_get_property(hp, t, "id", NULL);
David S. Millerf78eae22007-06-04 17:01:39 -0700695 if (*id < NR_CPUS)
696 cpu_data(*id).proc_id = proc_id;
697 }
698}
699
David S. Miller4f0234f2007-07-13 16:03:42 -0700700static void __devinit __set_proc_ids(struct mdesc_handle *hp,
701 const char *exec_unit_name)
David S. Millerf78eae22007-06-04 17:01:39 -0700702{
David S. Millerf78eae22007-06-04 17:01:39 -0700703 int idx;
David S. Miller43fdf272007-07-12 13:47:50 -0700704 u64 mp;
David S. Millerf78eae22007-06-04 17:01:39 -0700705
706 idx = 0;
David S. Miller43fdf272007-07-12 13:47:50 -0700707 mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
David S. Millerf78eae22007-06-04 17:01:39 -0700708 const char *type;
709 int len;
710
David S. Miller43fdf272007-07-12 13:47:50 -0700711 type = mdesc_get_property(hp, mp, "type", &len);
David S. Miller46bcea72007-08-07 18:46:36 -0700712 if (!of_find_in_proplist(type, "int", len) &&
713 !of_find_in_proplist(type, "integer", len))
David S. Millerf78eae22007-06-04 17:01:39 -0700714 continue;
715
David S. Miller43fdf272007-07-12 13:47:50 -0700716 mark_proc_ids(hp, mp, idx);
David S. Millerf78eae22007-06-04 17:01:39 -0700717
718 idx++;
719 }
720}
721
David S. Miller4f0234f2007-07-13 16:03:42 -0700722static void __devinit set_proc_ids(struct mdesc_handle *hp)
David S. Millerf78eae22007-06-04 17:01:39 -0700723{
David S. Miller43fdf272007-07-12 13:47:50 -0700724 __set_proc_ids(hp, "exec_unit");
725 __set_proc_ids(hp, "exec-unit");
David S. Millerf78eae22007-06-04 17:01:39 -0700726}
727
David S. Miller4f0234f2007-07-13 16:03:42 -0700728static void __devinit get_one_mondo_bits(const u64 *p, unsigned int *mask,
729 unsigned char def)
David S. Miller5cbc3072007-05-25 15:49:59 -0700730{
731 u64 val;
732
733 if (!p)
734 goto use_default;
735 val = *p;
736
737 if (!val || val >= 64)
738 goto use_default;
739
740 *mask = ((1U << val) * 64U) - 1U;
741 return;
742
743use_default:
744 *mask = ((1U << def) * 64U) - 1U;
745}
746
David S. Miller4f0234f2007-07-13 16:03:42 -0700747static void __devinit get_mondo_data(struct mdesc_handle *hp, u64 mp,
748 struct trap_per_cpu *tb)
David S. Miller5cbc3072007-05-25 15:49:59 -0700749{
750 const u64 *val;
751
David S. Miller43fdf272007-07-12 13:47:50 -0700752 val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700753 get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7);
754
David S. Miller43fdf272007-07-12 13:47:50 -0700755 val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700756 get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7);
757
David S. Miller43fdf272007-07-12 13:47:50 -0700758 val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700759 get_one_mondo_bits(val, &tb->resum_qmask, 6);
760
David S. Miller43fdf272007-07-12 13:47:50 -0700761 val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700762 get_one_mondo_bits(val, &tb->nonresum_qmask, 2);
763}
764
Sam Ravnborg7769bd12008-02-24 19:47:51 -0800765void __cpuinit mdesc_fill_in_cpu_data(cpumask_t mask)
David S. Miller5cbc3072007-05-25 15:49:59 -0700766{
David S. Miller43fdf272007-07-12 13:47:50 -0700767 struct mdesc_handle *hp = mdesc_grab();
768 u64 mp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700769
770 ncpus_probed = 0;
David S. Miller43fdf272007-07-12 13:47:50 -0700771 mdesc_for_each_node_by_name(hp, mp, "cpu") {
772 const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
773 const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700774 struct trap_per_cpu *tb;
775 cpuinfo_sparc *c;
David S. Miller5cbc3072007-05-25 15:49:59 -0700776 int cpuid;
David S. Miller43fdf272007-07-12 13:47:50 -0700777 u64 a;
David S. Miller5cbc3072007-05-25 15:49:59 -0700778
779 ncpus_probed++;
780
781 cpuid = *id;
782
783#ifdef CONFIG_SMP
David S. Miller8a177c42007-09-16 14:45:06 -0700784 if (cpuid >= NR_CPUS) {
785 printk(KERN_WARNING "Ignoring CPU %d which is "
786 ">= NR_CPUS (%d)\n",
787 cpuid, NR_CPUS);
David S. Miller5cbc3072007-05-25 15:49:59 -0700788 continue;
David S. Miller8a177c42007-09-16 14:45:06 -0700789 }
David S. Miller4f0234f2007-07-13 16:03:42 -0700790 if (!cpu_isset(cpuid, mask))
791 continue;
David S. Miller5cbc3072007-05-25 15:49:59 -0700792#else
793 /* On uniprocessor we only want the values for the
794 * real physical cpu the kernel booted onto, however
795 * cpu_data() only has one entry at index 0.
796 */
797 if (cpuid != real_hard_smp_processor_id())
798 continue;
799 cpuid = 0;
800#endif
801
802 c = &cpu_data(cpuid);
803 c->clock_tick = *cfreq;
804
805 tb = &trap_block[cpuid];
David S. Miller43fdf272007-07-12 13:47:50 -0700806 get_mondo_data(hp, mp, tb);
David S. Miller5cbc3072007-05-25 15:49:59 -0700807
David S. Miller43fdf272007-07-12 13:47:50 -0700808 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
809 u64 j, t = mdesc_arc_target(hp, a);
810 const char *t_name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700811
David S. Miller43fdf272007-07-12 13:47:50 -0700812 t_name = mdesc_node_name(hp, t);
813 if (!strcmp(t_name, "cache")) {
814 fill_in_one_cache(c, hp, t);
David S. Miller5cbc3072007-05-25 15:49:59 -0700815 continue;
816 }
817
David S. Miller43fdf272007-07-12 13:47:50 -0700818 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
819 u64 n = mdesc_arc_target(hp, j);
820 const char *n_name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700821
David S. Miller43fdf272007-07-12 13:47:50 -0700822 n_name = mdesc_node_name(hp, n);
823 if (!strcmp(n_name, "cache"))
824 fill_in_one_cache(c, hp, n);
David S. Miller5cbc3072007-05-25 15:49:59 -0700825 }
826 }
827
828#ifdef CONFIG_SMP
829 cpu_set(cpuid, cpu_present_map);
David S. Miller5cbc3072007-05-25 15:49:59 -0700830#endif
831
832 c->core_id = 0;
David S. Millerf78eae22007-06-04 17:01:39 -0700833 c->proc_id = -1;
David S. Miller5cbc3072007-05-25 15:49:59 -0700834 }
835
David S. Millera2f9f6b2007-06-04 21:48:33 -0700836#ifdef CONFIG_SMP
837 sparc64_multi_core = 1;
838#endif
839
David S. Miller43fdf272007-07-12 13:47:50 -0700840 set_core_ids(hp);
841 set_proc_ids(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700842
843 smp_fill_in_sib_core_maps();
David S. Miller43fdf272007-07-12 13:47:50 -0700844
845 mdesc_release(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700846}
847
David S. Miller0fdb7f92007-08-15 21:02:23 -0700848static ssize_t mdesc_read(struct file *file, char __user *buf,
849 size_t len, loff_t *offp)
850{
851 struct mdesc_handle *hp = mdesc_grab();
852 int err;
853
854 if (!hp)
855 return -ENODEV;
856
857 err = hp->handle_size;
858 if (len < hp->handle_size)
859 err = -EMSGSIZE;
860 else if (copy_to_user(buf, &hp->mdesc, hp->handle_size))
861 err = -EFAULT;
862 mdesc_release(hp);
863
864 return err;
865}
866
867static const struct file_operations mdesc_fops = {
868 .read = mdesc_read,
869 .owner = THIS_MODULE,
870};
871
872static struct miscdevice mdesc_misc = {
873 .minor = MISC_DYNAMIC_MINOR,
874 .name = "mdesc",
875 .fops = &mdesc_fops,
876};
877
878static int __init mdesc_misc_init(void)
879{
880 return misc_register(&mdesc_misc);
881}
882
883__initcall(mdesc_misc_init);
884
David S. Miller5cbc3072007-05-25 15:49:59 -0700885void __init sun4v_mdesc_init(void)
886{
David S. Miller43fdf272007-07-12 13:47:50 -0700887 struct mdesc_handle *hp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700888 unsigned long len, real_len, status;
David S. Miller4f0234f2007-07-13 16:03:42 -0700889 cpumask_t mask;
David S. Miller5cbc3072007-05-25 15:49:59 -0700890
891 (void) sun4v_mach_desc(0UL, 0UL, &len);
892
893 printk("MDESC: Size is %lu bytes.\n", len);
894
David S. Miller4a283332008-02-13 19:22:23 -0800895 hp = mdesc_alloc(len, &lmb_mdesc_ops);
David S. Miller43fdf272007-07-12 13:47:50 -0700896 if (hp == NULL) {
897 prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
898 prom_halt();
899 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700900
David S. Miller43fdf272007-07-12 13:47:50 -0700901 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700902 if (status != HV_EOK || real_len > len) {
903 prom_printf("sun4v_mach_desc fails, err(%lu), "
904 "len(%lu), real_len(%lu)\n",
905 status, len, real_len);
David S. Miller43fdf272007-07-12 13:47:50 -0700906 mdesc_free(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700907 prom_halt();
908 }
909
David S. Miller43fdf272007-07-12 13:47:50 -0700910 cur_mdesc = hp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700911
912 report_platform_properties();
David S. Miller4f0234f2007-07-13 16:03:42 -0700913
914 cpus_setall(mask);
915 mdesc_fill_in_cpu_data(mask);
David S. Miller5cbc3072007-05-25 15:49:59 -0700916}