blob: 6d2015e0e5744d78ee01f0927be0d260da63e784 [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
Sam Ravnborg6e6ab2e2008-12-26 15:33:07 -080014#include <asm/cpudata.h>
David S. Miller5cbc3072007-05-25 15:49:59 -070015#include <asm/hypervisor.h>
16#include <asm/mdesc.h>
17#include <asm/prom.h>
18#include <asm/oplib.h>
19#include <asm/smp.h>
20
21/* Unlike the OBP device tree, the machine description is a full-on
22 * DAG. An arbitrary number of ARCs are possible from one
23 * node to other nodes and thus we can't use the OBP device_node
24 * data structure to represent these nodes inside of the kernel.
25 *
26 * Actually, it isn't even a DAG, because there are back pointers
27 * which create cycles in the graph.
28 *
29 * mdesc_hdr and mdesc_elem describe the layout of the data structure
30 * we get from the Hypervisor.
31 */
32struct mdesc_hdr {
33 u32 version; /* Transport version */
34 u32 node_sz; /* node block size */
35 u32 name_sz; /* name block size */
36 u32 data_sz; /* data block size */
David S. Miller43fdf272007-07-12 13:47:50 -070037} __attribute__((aligned(16)));
David S. Miller5cbc3072007-05-25 15:49:59 -070038
39struct mdesc_elem {
40 u8 tag;
41#define MD_LIST_END 0x00
42#define MD_NODE 0x4e
43#define MD_NODE_END 0x45
44#define MD_NOOP 0x20
45#define MD_PROP_ARC 0x61
46#define MD_PROP_VAL 0x76
47#define MD_PROP_STR 0x73
48#define MD_PROP_DATA 0x64
49 u8 name_len;
50 u16 resv;
51 u32 name_offset;
52 union {
53 struct {
54 u32 data_len;
55 u32 data_offset;
56 } data;
57 u64 val;
58 } d;
59};
60
David S. Miller43fdf272007-07-12 13:47:50 -070061struct mdesc_mem_ops {
62 struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
63 void (*free)(struct mdesc_handle *handle);
64};
David S. Miller5cbc3072007-05-25 15:49:59 -070065
David S. Miller43fdf272007-07-12 13:47:50 -070066struct mdesc_handle {
67 struct list_head list;
68 struct mdesc_mem_ops *mops;
69 void *self_base;
70 atomic_t refcnt;
71 unsigned int handle_size;
72 struct mdesc_hdr mdesc;
73};
David S. Miller5cbc3072007-05-25 15:49:59 -070074
David S. Miller43fdf272007-07-12 13:47:50 -070075static void mdesc_handle_init(struct mdesc_handle *hp,
76 unsigned int handle_size,
77 void *base)
David S. Miller5cbc3072007-05-25 15:49:59 -070078{
David S. Miller43fdf272007-07-12 13:47:50 -070079 BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));
80
81 memset(hp, 0, handle_size);
82 INIT_LIST_HEAD(&hp->list);
83 hp->self_base = base;
84 atomic_set(&hp->refcnt, 1);
85 hp->handle_size = handle_size;
David S. Miller5cbc3072007-05-25 15:49:59 -070086}
87
David S. Miller4a283332008-02-13 19:22:23 -080088static struct mdesc_handle * __init mdesc_lmb_alloc(unsigned int mdesc_size)
David S. Miller5cbc3072007-05-25 15:49:59 -070089{
David S. Miller43fdf272007-07-12 13:47:50 -070090 unsigned int handle_size, alloc_size;
David S. Miller4a283332008-02-13 19:22:23 -080091 struct mdesc_handle *hp;
92 unsigned long paddr;
David S. Miller5cbc3072007-05-25 15:49:59 -070093
David S. Miller43fdf272007-07-12 13:47:50 -070094 handle_size = (sizeof(struct mdesc_handle) -
95 sizeof(struct mdesc_hdr) +
96 mdesc_size);
97 alloc_size = PAGE_ALIGN(handle_size);
David S. Miller5cbc3072007-05-25 15:49:59 -070098
David S. Miller4a283332008-02-13 19:22:23 -080099 paddr = lmb_alloc(alloc_size, PAGE_SIZE);
David S. Miller43fdf272007-07-12 13:47:50 -0700100
David S. Miller4a283332008-02-13 19:22:23 -0800101 hp = NULL;
102 if (paddr) {
103 hp = __va(paddr);
104 mdesc_handle_init(hp, handle_size, hp);
105 }
David S. Miller43fdf272007-07-12 13:47:50 -0700106 return hp;
107}
108
David S. Miller4a283332008-02-13 19:22:23 -0800109static void mdesc_lmb_free(struct mdesc_handle *hp)
David S. Miller43fdf272007-07-12 13:47:50 -0700110{
111 unsigned int alloc_size, handle_size = hp->handle_size;
112 unsigned long start, end;
113
114 BUG_ON(atomic_read(&hp->refcnt) != 0);
115 BUG_ON(!list_empty(&hp->list));
116
117 alloc_size = PAGE_ALIGN(handle_size);
118
119 start = (unsigned long) hp;
120 end = start + alloc_size;
121
122 while (start < end) {
123 struct page *p;
124
125 p = virt_to_page(start);
126 ClearPageReserved(p);
127 __free_page(p);
128 start += PAGE_SIZE;
David S. Miller5cbc3072007-05-25 15:49:59 -0700129 }
130}
131
David S. Miller4a283332008-02-13 19:22:23 -0800132static struct mdesc_mem_ops lmb_mdesc_ops = {
133 .alloc = mdesc_lmb_alloc,
134 .free = mdesc_lmb_free,
David S. Miller43fdf272007-07-12 13:47:50 -0700135};
136
137static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
David S. Miller5cbc3072007-05-25 15:49:59 -0700138{
David S. Miller43fdf272007-07-12 13:47:50 -0700139 unsigned int handle_size;
140 void *base;
David S. Miller5cbc3072007-05-25 15:49:59 -0700141
David S. Miller43fdf272007-07-12 13:47:50 -0700142 handle_size = (sizeof(struct mdesc_handle) -
143 sizeof(struct mdesc_hdr) +
144 mdesc_size);
David S. Miller5cbc3072007-05-25 15:49:59 -0700145
David S. Miller920c3ed2007-07-17 21:37:35 -0700146 base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_NOFAIL);
David S. Miller43fdf272007-07-12 13:47:50 -0700147 if (base) {
148 struct mdesc_handle *hp;
149 unsigned long addr;
150
151 addr = (unsigned long)base;
152 addr = (addr + 15UL) & ~15UL;
153 hp = (struct mdesc_handle *) addr;
154
155 mdesc_handle_init(hp, handle_size, base);
156 return hp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700157 }
David S. Miller43fdf272007-07-12 13:47:50 -0700158
David S. Miller5cbc3072007-05-25 15:49:59 -0700159 return NULL;
160}
161
David S. Miller43fdf272007-07-12 13:47:50 -0700162static void mdesc_kfree(struct mdesc_handle *hp)
David S. Miller5cbc3072007-05-25 15:49:59 -0700163{
David S. Miller43fdf272007-07-12 13:47:50 -0700164 BUG_ON(atomic_read(&hp->refcnt) != 0);
165 BUG_ON(!list_empty(&hp->list));
David S. Miller5cbc3072007-05-25 15:49:59 -0700166
David S. Miller43fdf272007-07-12 13:47:50 -0700167 kfree(hp->self_base);
168}
169
170static struct mdesc_mem_ops kmalloc_mdesc_memops = {
171 .alloc = mdesc_kmalloc,
172 .free = mdesc_kfree,
173};
174
175static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
176 struct mdesc_mem_ops *mops)
177{
178 struct mdesc_handle *hp = mops->alloc(mdesc_size);
179
180 if (hp)
181 hp->mops = mops;
182
183 return hp;
184}
185
186static void mdesc_free(struct mdesc_handle *hp)
187{
188 hp->mops->free(hp);
189}
190
191static struct mdesc_handle *cur_mdesc;
192static LIST_HEAD(mdesc_zombie_list);
193static DEFINE_SPINLOCK(mdesc_lock);
194
195struct mdesc_handle *mdesc_grab(void)
196{
197 struct mdesc_handle *hp;
198 unsigned long flags;
199
200 spin_lock_irqsave(&mdesc_lock, flags);
201 hp = cur_mdesc;
202 if (hp)
203 atomic_inc(&hp->refcnt);
204 spin_unlock_irqrestore(&mdesc_lock, flags);
205
206 return hp;
207}
208EXPORT_SYMBOL(mdesc_grab);
209
210void mdesc_release(struct mdesc_handle *hp)
211{
212 unsigned long flags;
213
214 spin_lock_irqsave(&mdesc_lock, flags);
215 if (atomic_dec_and_test(&hp->refcnt)) {
216 list_del_init(&hp->list);
217 hp->mops->free(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700218 }
David S. Miller43fdf272007-07-12 13:47:50 -0700219 spin_unlock_irqrestore(&mdesc_lock, flags);
David S. Miller5cbc3072007-05-25 15:49:59 -0700220}
David S. Miller43fdf272007-07-12 13:47:50 -0700221EXPORT_SYMBOL(mdesc_release);
David S. Miller5cbc3072007-05-25 15:49:59 -0700222
David S. Miller920c3ed2007-07-17 21:37:35 -0700223static DEFINE_MUTEX(mdesc_mutex);
224static struct mdesc_notifier_client *client_list;
225
226void mdesc_register_notifier(struct mdesc_notifier_client *client)
227{
228 u64 node;
229
230 mutex_lock(&mdesc_mutex);
231 client->next = client_list;
232 client_list = client;
233
234 mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
235 client->add(cur_mdesc, node);
236
237 mutex_unlock(&mdesc_mutex);
238}
239
David S. Miller91ba3c22007-07-18 15:15:45 -0700240static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
241{
242 const u64 *id;
243 u64 a;
244
245 id = NULL;
246 mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
247 u64 target;
248
249 target = mdesc_arc_target(hp, a);
250 id = mdesc_get_property(hp, target,
251 "cfg-handle", NULL);
252 if (id)
253 break;
254 }
255
256 return id;
257}
258
David S. Miller920c3ed2007-07-17 21:37:35 -0700259/* Run 'func' on nodes which are in A but not in B. */
260static void invoke_on_missing(const char *name,
261 struct mdesc_handle *a,
262 struct mdesc_handle *b,
263 void (*func)(struct mdesc_handle *, u64))
264{
265 u64 node;
266
267 mdesc_for_each_node_by_name(a, node, name) {
David S. Miller91ba3c22007-07-18 15:15:45 -0700268 int found = 0, is_vdc_port = 0;
269 const char *name_prop;
270 const u64 *id;
David S. Miller920c3ed2007-07-17 21:37:35 -0700271 u64 fnode;
272
David S. Miller91ba3c22007-07-18 15:15:45 -0700273 name_prop = mdesc_get_property(a, node, "name", NULL);
274 if (name_prop && !strcmp(name_prop, "vdc-port")) {
275 is_vdc_port = 1;
276 id = parent_cfg_handle(a, node);
277 } else
278 id = mdesc_get_property(a, node, "id", NULL);
279
280 if (!id) {
281 printk(KERN_ERR "MD: Cannot find ID for %s node.\n",
282 (name_prop ? name_prop : name));
283 continue;
284 }
285
David S. Miller920c3ed2007-07-17 21:37:35 -0700286 mdesc_for_each_node_by_name(b, fnode, name) {
David S. Miller91ba3c22007-07-18 15:15:45 -0700287 const u64 *fid;
288
289 if (is_vdc_port) {
290 name_prop = mdesc_get_property(b, fnode,
291 "name", NULL);
292 if (!name_prop ||
293 strcmp(name_prop, "vdc-port"))
294 continue;
295 fid = parent_cfg_handle(b, fnode);
296 if (!fid) {
297 printk(KERN_ERR "MD: Cannot find ID "
298 "for vdc-port node.\n");
299 continue;
300 }
301 } else
302 fid = mdesc_get_property(b, fnode,
303 "id", NULL);
David S. Miller920c3ed2007-07-17 21:37:35 -0700304
305 if (*id == *fid) {
306 found = 1;
307 break;
308 }
309 }
310 if (!found)
311 func(a, node);
312 }
313}
314
315static void notify_one(struct mdesc_notifier_client *p,
316 struct mdesc_handle *old_hp,
317 struct mdesc_handle *new_hp)
318{
319 invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
320 invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
321}
322
323static void mdesc_notify_clients(struct mdesc_handle *old_hp,
324 struct mdesc_handle *new_hp)
325{
326 struct mdesc_notifier_client *p = client_list;
327
328 while (p) {
329 notify_one(p, old_hp, new_hp);
330 p = p->next;
331 }
332}
333
David S. Miller778feeb2007-07-16 16:50:36 -0700334void mdesc_update(void)
David S. Miller5cbc3072007-05-25 15:49:59 -0700335{
David S. Miller43fdf272007-07-12 13:47:50 -0700336 unsigned long len, real_len, status;
337 struct mdesc_handle *hp, *orig_hp;
338 unsigned long flags;
David S. Miller5cbc3072007-05-25 15:49:59 -0700339
David S. Miller920c3ed2007-07-17 21:37:35 -0700340 mutex_lock(&mdesc_mutex);
341
David S. Miller43fdf272007-07-12 13:47:50 -0700342 (void) sun4v_mach_desc(0UL, 0UL, &len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700343
David S. Miller43fdf272007-07-12 13:47:50 -0700344 hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
345 if (!hp) {
346 printk(KERN_ERR "MD: mdesc alloc fails\n");
David S. Miller920c3ed2007-07-17 21:37:35 -0700347 goto out;
David S. Miller5cbc3072007-05-25 15:49:59 -0700348 }
349
David S. Miller43fdf272007-07-12 13:47:50 -0700350 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
351 if (status != HV_EOK || real_len > len) {
352 printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
353 status);
354 atomic_dec(&hp->refcnt);
355 mdesc_free(hp);
David S. Miller920c3ed2007-07-17 21:37:35 -0700356 goto out;
David S. Miller5cbc3072007-05-25 15:49:59 -0700357 }
David S. Miller43fdf272007-07-12 13:47:50 -0700358
359 spin_lock_irqsave(&mdesc_lock, flags);
360 orig_hp = cur_mdesc;
361 cur_mdesc = hp;
David S. Miller920c3ed2007-07-17 21:37:35 -0700362 spin_unlock_irqrestore(&mdesc_lock, flags);
David S. Miller43fdf272007-07-12 13:47:50 -0700363
David S. Miller920c3ed2007-07-17 21:37:35 -0700364 mdesc_notify_clients(orig_hp, hp);
365
366 spin_lock_irqsave(&mdesc_lock, flags);
David S. Miller43fdf272007-07-12 13:47:50 -0700367 if (atomic_dec_and_test(&orig_hp->refcnt))
368 mdesc_free(orig_hp);
369 else
370 list_add(&orig_hp->list, &mdesc_zombie_list);
371 spin_unlock_irqrestore(&mdesc_lock, flags);
David S. Miller920c3ed2007-07-17 21:37:35 -0700372
373out:
374 mutex_unlock(&mdesc_mutex);
David S. Miller5cbc3072007-05-25 15:49:59 -0700375}
376
David S. Miller43fdf272007-07-12 13:47:50 -0700377static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700378{
379 return (struct mdesc_elem *) (mdesc + 1);
380}
381
David S. Miller43fdf272007-07-12 13:47:50 -0700382static void *name_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700383{
384 return ((void *) node_block(mdesc)) + mdesc->node_sz;
385}
386
David S. Miller43fdf272007-07-12 13:47:50 -0700387static void *data_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700388{
389 return ((void *) name_block(mdesc)) + mdesc->name_sz;
390}
391
David S. Miller43fdf272007-07-12 13:47:50 -0700392u64 mdesc_node_by_name(struct mdesc_handle *hp,
393 u64 from_node, const char *name)
David S. Miller5cbc3072007-05-25 15:49:59 -0700394{
David S. Miller43fdf272007-07-12 13:47:50 -0700395 struct mdesc_elem *ep = node_block(&hp->mdesc);
396 const char *names = name_block(&hp->mdesc);
397 u64 last_node = hp->mdesc.node_sz / 16;
398 u64 ret;
David S. Miller5cbc3072007-05-25 15:49:59 -0700399
David S. Miller778feeb2007-07-16 16:50:36 -0700400 if (from_node == MDESC_NODE_NULL) {
401 ret = from_node = 0;
402 } else if (from_node >= last_node) {
David S. Miller43fdf272007-07-12 13:47:50 -0700403 return MDESC_NODE_NULL;
David S. Miller778feeb2007-07-16 16:50:36 -0700404 } else {
405 ret = ep[from_node].d.val;
406 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700407
David S. Miller43fdf272007-07-12 13:47:50 -0700408 while (ret < last_node) {
409 if (ep[ret].tag != MD_NODE)
410 return MDESC_NODE_NULL;
411 if (!strcmp(names + ep[ret].name_offset, name))
412 break;
413 ret = ep[ret].d.val;
414 }
415 if (ret >= last_node)
416 ret = MDESC_NODE_NULL;
417 return ret;
418}
419EXPORT_SYMBOL(mdesc_node_by_name);
David S. Miller5cbc3072007-05-25 15:49:59 -0700420
David S. Miller43fdf272007-07-12 13:47:50 -0700421const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
422 const char *name, int *lenp)
423{
424 const char *names = name_block(&hp->mdesc);
425 u64 last_node = hp->mdesc.node_sz / 16;
426 void *data = data_block(&hp->mdesc);
427 struct mdesc_elem *ep;
428
429 if (node == MDESC_NODE_NULL || node >= last_node)
430 return NULL;
431
432 ep = node_block(&hp->mdesc) + node;
433 ep++;
434 for (; ep->tag != MD_NODE_END; ep++) {
435 void *val = NULL;
436 int len = 0;
437
438 switch (ep->tag) {
439 case MD_PROP_VAL:
440 val = &ep->d.val;
441 len = 8;
David S. Miller5cbc3072007-05-25 15:49:59 -0700442 break;
443
David S. Miller43fdf272007-07-12 13:47:50 -0700444 case MD_PROP_STR:
445 case MD_PROP_DATA:
446 val = data + ep->d.data.data_offset;
447 len = ep->d.data.data_len;
448 break;
David S. Miller5cbc3072007-05-25 15:49:59 -0700449
David S. Miller43fdf272007-07-12 13:47:50 -0700450 default:
David S. Miller5cbc3072007-05-25 15:49:59 -0700451 break;
452 }
David S. Miller43fdf272007-07-12 13:47:50 -0700453 if (!val)
454 continue;
David S. Miller5cbc3072007-05-25 15:49:59 -0700455
David S. Miller43fdf272007-07-12 13:47:50 -0700456 if (!strcmp(names + ep->name_offset, name)) {
457 if (lenp)
458 *lenp = len;
459 return val;
David S. Miller5cbc3072007-05-25 15:49:59 -0700460 }
461 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700462
David S. Miller43fdf272007-07-12 13:47:50 -0700463 return NULL;
464}
465EXPORT_SYMBOL(mdesc_get_property);
466
467u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
David S. Miller5cbc3072007-05-25 15:49:59 -0700468{
David S. Miller43fdf272007-07-12 13:47:50 -0700469 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
470 const char *names = name_block(&hp->mdesc);
471 u64 last_node = hp->mdesc.node_sz / 16;
David S. Miller5cbc3072007-05-25 15:49:59 -0700472
David S. Miller43fdf272007-07-12 13:47:50 -0700473 if (from == MDESC_NODE_NULL || from >= last_node)
474 return MDESC_NODE_NULL;
475
476 ep = base + from;
477
478 ep++;
479 for (; ep->tag != MD_NODE_END; ep++) {
480 if (ep->tag != MD_PROP_ARC)
481 continue;
482
483 if (strcmp(names + ep->name_offset, arc_type))
484 continue;
485
486 return ep - base;
David S. Miller5cbc3072007-05-25 15:49:59 -0700487 }
David S. Miller43fdf272007-07-12 13:47:50 -0700488
489 return MDESC_NODE_NULL;
David S. Miller5cbc3072007-05-25 15:49:59 -0700490}
David S. Miller43fdf272007-07-12 13:47:50 -0700491EXPORT_SYMBOL(mdesc_next_arc);
492
493u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
494{
495 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
496
497 ep = base + arc;
498
499 return ep->d.val;
500}
501EXPORT_SYMBOL(mdesc_arc_target);
502
503const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
504{
505 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
506 const char *names = name_block(&hp->mdesc);
507 u64 last_node = hp->mdesc.node_sz / 16;
508
509 if (node == MDESC_NODE_NULL || node >= last_node)
510 return NULL;
511
512 ep = base + node;
513 if (ep->tag != MD_NODE)
514 return NULL;
515
516 return names + ep->name_offset;
517}
518EXPORT_SYMBOL(mdesc_node_name);
David S. Miller5cbc3072007-05-25 15:49:59 -0700519
520static void __init report_platform_properties(void)
521{
David S. Miller43fdf272007-07-12 13:47:50 -0700522 struct mdesc_handle *hp = mdesc_grab();
523 u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
David S. Miller5cbc3072007-05-25 15:49:59 -0700524 const char *s;
525 const u64 *v;
526
David S. Miller43fdf272007-07-12 13:47:50 -0700527 if (pn == MDESC_NODE_NULL) {
David S. Miller5cbc3072007-05-25 15:49:59 -0700528 prom_printf("No platform node in machine-description.\n");
529 prom_halt();
530 }
531
David S. Miller43fdf272007-07-12 13:47:50 -0700532 s = mdesc_get_property(hp, pn, "banner-name", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700533 printk("PLATFORM: banner-name [%s]\n", s);
David S. Miller43fdf272007-07-12 13:47:50 -0700534 s = mdesc_get_property(hp, pn, "name", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700535 printk("PLATFORM: name [%s]\n", s);
536
David S. Miller43fdf272007-07-12 13:47:50 -0700537 v = mdesc_get_property(hp, pn, "hostid", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700538 if (v)
Sam Ravnborg90181132009-01-06 13:19:28 -0800539 printk("PLATFORM: hostid [%08llx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700540 v = mdesc_get_property(hp, pn, "serial#", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700541 if (v)
Sam Ravnborg90181132009-01-06 13:19:28 -0800542 printk("PLATFORM: serial# [%08llx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700543 v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
Sam Ravnborg90181132009-01-06 13:19:28 -0800544 printk("PLATFORM: stick-frequency [%08llx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700545 v = mdesc_get_property(hp, pn, "mac-address", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700546 if (v)
Sam Ravnborg90181132009-01-06 13:19:28 -0800547 printk("PLATFORM: mac-address [%llx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700548 v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700549 if (v)
Sam Ravnborg90181132009-01-06 13:19:28 -0800550 printk("PLATFORM: watchdog-resolution [%llu ms]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700551 v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700552 if (v)
Sam Ravnborg90181132009-01-06 13:19:28 -0800553 printk("PLATFORM: watchdog-max-timeout [%llu ms]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700554 v = mdesc_get_property(hp, pn, "max-cpus", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700555 if (v)
Sam Ravnborg90181132009-01-06 13:19:28 -0800556 printk("PLATFORM: max-cpus [%llu]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700557
David S. Miller4f0234f2007-07-13 16:03:42 -0700558#ifdef CONFIG_SMP
559 {
560 int max_cpu, i;
561
562 if (v) {
563 max_cpu = *v;
564 if (max_cpu > NR_CPUS)
565 max_cpu = NR_CPUS;
566 } else {
567 max_cpu = NR_CPUS;
568 }
569 for (i = 0; i < max_cpu; i++)
Rusty Russell89229072009-03-16 14:40:23 +1030570 set_cpu_possible(i, true);
David S. Miller4f0234f2007-07-13 16:03:42 -0700571 }
572#endif
573
David S. Miller43fdf272007-07-12 13:47:50 -0700574 mdesc_release(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700575}
576
David S. Miller5052f522009-04-08 19:55:47 -0700577static void __cpuinit fill_in_one_cache(cpuinfo_sparc *c,
David S. Miller4f0234f2007-07-13 16:03:42 -0700578 struct mdesc_handle *hp,
579 u64 mp)
David S. Miller5cbc3072007-05-25 15:49:59 -0700580{
David S. Miller43fdf272007-07-12 13:47:50 -0700581 const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
582 const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
583 const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700584 const char *type;
585 int type_len;
586
David S. Miller43fdf272007-07-12 13:47:50 -0700587 type = mdesc_get_property(hp, mp, "type", &type_len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700588
589 switch (*level) {
590 case 1:
David S. Miller46bcea72007-08-07 18:46:36 -0700591 if (of_find_in_proplist(type, "instn", type_len)) {
David S. Miller5cbc3072007-05-25 15:49:59 -0700592 c->icache_size = *size;
593 c->icache_line_size = *line_size;
David S. Miller46bcea72007-08-07 18:46:36 -0700594 } else if (of_find_in_proplist(type, "data", type_len)) {
David S. Miller5cbc3072007-05-25 15:49:59 -0700595 c->dcache_size = *size;
596 c->dcache_line_size = *line_size;
597 }
598 break;
599
600 case 2:
601 c->ecache_size = *size;
602 c->ecache_line_size = *line_size;
603 break;
604
605 default:
606 break;
607 }
608
609 if (*level == 1) {
David S. Miller43fdf272007-07-12 13:47:50 -0700610 u64 a;
David S. Miller5cbc3072007-05-25 15:49:59 -0700611
David S. Miller43fdf272007-07-12 13:47:50 -0700612 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
613 u64 target = mdesc_arc_target(hp, a);
614 const char *name = mdesc_node_name(hp, target);
David S. Miller5cbc3072007-05-25 15:49:59 -0700615
David S. Miller43fdf272007-07-12 13:47:50 -0700616 if (!strcmp(name, "cache"))
617 fill_in_one_cache(c, hp, target);
David S. Miller5cbc3072007-05-25 15:49:59 -0700618 }
619 }
620}
621
David S. Miller5052f522009-04-08 19:55:47 -0700622static void __cpuinit mark_core_ids(struct mdesc_handle *hp, u64 mp, 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. Miller5052f522009-04-08 19:55:47 -0700655static void __cpuinit 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. Miller5052f522009-04-08 19:55:47 -0700680static void __cpuinit mark_proc_ids(struct mdesc_handle *hp, u64 mp, int proc_id)
David S. Millerf78eae22007-06-04 17:01:39 -0700681{
David S. Miller43fdf272007-07-12 13:47:50 -0700682 u64 a;
David S. Millerf78eae22007-06-04 17:01:39 -0700683
David S. Miller43fdf272007-07-12 13:47:50 -0700684 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
685 u64 t = mdesc_arc_target(hp, a);
686 const char *name;
David S. Millerf78eae22007-06-04 17:01:39 -0700687 const u64 *id;
688
David S. Miller43fdf272007-07-12 13:47:50 -0700689 name = mdesc_node_name(hp, t);
690 if (strcmp(name, "cpu"))
David S. Millerf78eae22007-06-04 17:01:39 -0700691 continue;
692
David S. Miller43fdf272007-07-12 13:47:50 -0700693 id = mdesc_get_property(hp, t, "id", NULL);
David S. Millerf78eae22007-06-04 17:01:39 -0700694 if (*id < NR_CPUS)
695 cpu_data(*id).proc_id = proc_id;
696 }
697}
698
David S. Miller5052f522009-04-08 19:55:47 -0700699static void __cpuinit __set_proc_ids(struct mdesc_handle *hp, const char *exec_unit_name)
David S. Millerf78eae22007-06-04 17:01:39 -0700700{
David S. Millerf78eae22007-06-04 17:01:39 -0700701 int idx;
David S. Miller43fdf272007-07-12 13:47:50 -0700702 u64 mp;
David S. Millerf78eae22007-06-04 17:01:39 -0700703
704 idx = 0;
David S. Miller43fdf272007-07-12 13:47:50 -0700705 mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
David S. Millerf78eae22007-06-04 17:01:39 -0700706 const char *type;
707 int len;
708
David S. Miller43fdf272007-07-12 13:47:50 -0700709 type = mdesc_get_property(hp, mp, "type", &len);
David S. Miller46bcea72007-08-07 18:46:36 -0700710 if (!of_find_in_proplist(type, "int", len) &&
711 !of_find_in_proplist(type, "integer", len))
David S. Millerf78eae22007-06-04 17:01:39 -0700712 continue;
713
David S. Miller43fdf272007-07-12 13:47:50 -0700714 mark_proc_ids(hp, mp, idx);
David S. Millerf78eae22007-06-04 17:01:39 -0700715
716 idx++;
717 }
718}
719
David S. Miller5052f522009-04-08 19:55:47 -0700720static void __cpuinit set_proc_ids(struct mdesc_handle *hp)
David S. Millerf78eae22007-06-04 17:01:39 -0700721{
David S. Miller43fdf272007-07-12 13:47:50 -0700722 __set_proc_ids(hp, "exec_unit");
723 __set_proc_ids(hp, "exec-unit");
David S. Millerf78eae22007-06-04 17:01:39 -0700724}
725
David S. Miller5052f522009-04-08 19:55:47 -0700726static void __cpuinit get_one_mondo_bits(const u64 *p, unsigned int *mask,
David S. Miller4f0234f2007-07-13 16:03:42 -0700727 unsigned char def)
David S. Miller5cbc3072007-05-25 15:49:59 -0700728{
729 u64 val;
730
731 if (!p)
732 goto use_default;
733 val = *p;
734
735 if (!val || val >= 64)
736 goto use_default;
737
738 *mask = ((1U << val) * 64U) - 1U;
739 return;
740
741use_default:
742 *mask = ((1U << def) * 64U) - 1U;
743}
744
David S. Miller5052f522009-04-08 19:55:47 -0700745static void __cpuinit get_mondo_data(struct mdesc_handle *hp, u64 mp,
David S. Miller4f0234f2007-07-13 16:03:42 -0700746 struct trap_per_cpu *tb)
David S. Miller5cbc3072007-05-25 15:49:59 -0700747{
748 const u64 *val;
749
David S. Miller43fdf272007-07-12 13:47:50 -0700750 val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700751 get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7);
752
David S. Miller43fdf272007-07-12 13:47:50 -0700753 val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700754 get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7);
755
David S. Miller43fdf272007-07-12 13:47:50 -0700756 val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700757 get_one_mondo_bits(val, &tb->resum_qmask, 6);
758
David S. Miller43fdf272007-07-12 13:47:50 -0700759 val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700760 get_one_mondo_bits(val, &tb->nonresum_qmask, 2);
761}
762
David S. Miller5052f522009-04-08 19:55:47 -0700763static void * __cpuinit mdesc_iterate_over_cpus(void *(*func)(struct mdesc_handle *, u64, int, void *), void *arg, cpumask_t *mask)
David S. Miller5cbc3072007-05-25 15:49:59 -0700764{
David S. Miller43fdf272007-07-12 13:47:50 -0700765 struct mdesc_handle *hp = mdesc_grab();
David S. Miller5052f522009-04-08 19:55:47 -0700766 void *ret = NULL;
David S. Miller43fdf272007-07-12 13:47:50 -0700767 u64 mp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700768
David S. Miller43fdf272007-07-12 13:47:50 -0700769 mdesc_for_each_node_by_name(hp, mp, "cpu") {
770 const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
David S. Miller5052f522009-04-08 19:55:47 -0700771 int cpuid = *id;
David S. Miller5cbc3072007-05-25 15:49:59 -0700772
773#ifdef CONFIG_SMP
David S. Miller8a177c42007-09-16 14:45:06 -0700774 if (cpuid >= NR_CPUS) {
775 printk(KERN_WARNING "Ignoring CPU %d which is "
776 ">= NR_CPUS (%d)\n",
777 cpuid, NR_CPUS);
David S. Miller5cbc3072007-05-25 15:49:59 -0700778 continue;
David S. Miller8a177c42007-09-16 14:45:06 -0700779 }
David S. Miller5052f522009-04-08 19:55:47 -0700780 if (!cpu_isset(cpuid, *mask))
David S. Miller4f0234f2007-07-13 16:03:42 -0700781 continue;
David S. Miller5cbc3072007-05-25 15:49:59 -0700782#endif
783
David S. Miller5052f522009-04-08 19:55:47 -0700784 ret = func(hp, mp, cpuid, arg);
785 if (ret)
786 goto out;
787 }
788out:
789 mdesc_release(hp);
790 return ret;
791}
David S. Miller5cbc3072007-05-25 15:49:59 -0700792
David S. Miller5052f522009-04-08 19:55:47 -0700793static void * __cpuinit record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
794{
795 ncpus_probed++;
796#ifdef CONFIG_SMP
797 set_cpu_present(cpuid, true);
798#endif
799 return NULL;
800}
David S. Miller5cbc3072007-05-25 15:49:59 -0700801
David S. Miller5052f522009-04-08 19:55:47 -0700802void __cpuinit mdesc_populate_present_mask(cpumask_t *mask)
803{
804 if (tlb_type != hypervisor)
805 return;
David S. Miller5cbc3072007-05-25 15:49:59 -0700806
David S. Miller5052f522009-04-08 19:55:47 -0700807 ncpus_probed = 0;
808 mdesc_iterate_over_cpus(record_one_cpu, NULL, mask);
809}
David S. Miller5cbc3072007-05-25 15:49:59 -0700810
David S. Miller5052f522009-04-08 19:55:47 -0700811static void * __cpuinit fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
812{
813 const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
814 struct trap_per_cpu *tb;
815 cpuinfo_sparc *c;
816 u64 a;
David S. Miller5cbc3072007-05-25 15:49:59 -0700817
David S. Miller5052f522009-04-08 19:55:47 -0700818#ifndef CONFIG_SMP
819 /* On uniprocessor we only want the values for the
820 * real physical cpu the kernel booted onto, however
821 * cpu_data() only has one entry at index 0.
822 */
823 if (cpuid != real_hard_smp_processor_id())
824 return NULL;
825 cpuid = 0;
826#endif
827
828 c = &cpu_data(cpuid);
829 c->clock_tick = *cfreq;
830
831 tb = &trap_block[cpuid];
832 get_mondo_data(hp, mp, tb);
833
834 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
835 u64 j, t = mdesc_arc_target(hp, a);
836 const char *t_name;
837
838 t_name = mdesc_node_name(hp, t);
839 if (!strcmp(t_name, "cache")) {
840 fill_in_one_cache(c, hp, t);
841 continue;
David S. Miller5cbc3072007-05-25 15:49:59 -0700842 }
843
David S. Miller5052f522009-04-08 19:55:47 -0700844 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
845 u64 n = mdesc_arc_target(hp, j);
846 const char *n_name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700847
David S. Miller5052f522009-04-08 19:55:47 -0700848 n_name = mdesc_node_name(hp, n);
849 if (!strcmp(n_name, "cache"))
850 fill_in_one_cache(c, hp, n);
851 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700852 }
853
David S. Miller5052f522009-04-08 19:55:47 -0700854 c->core_id = 0;
855 c->proc_id = -1;
856
857 return NULL;
858}
859
David S. Millera2094502009-04-01 03:15:11 -0700860void __cpuinit mdesc_fill_in_cpu_data(cpumask_t *mask)
David S. Miller5052f522009-04-08 19:55:47 -0700861{
862 struct mdesc_handle *hp;
863
David S. Millera2094502009-04-01 03:15:11 -0700864 mdesc_populate_present_mask(mask);
865 mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask);
David S. Miller5052f522009-04-08 19:55:47 -0700866
David S. Millera2f9f6b2007-06-04 21:48:33 -0700867#ifdef CONFIG_SMP
868 sparc64_multi_core = 1;
869#endif
870
David S. Miller5052f522009-04-08 19:55:47 -0700871 hp = mdesc_grab();
872
David S. Miller43fdf272007-07-12 13:47:50 -0700873 set_core_ids(hp);
874 set_proc_ids(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700875
David S. Miller43fdf272007-07-12 13:47:50 -0700876 mdesc_release(hp);
David S. Miller5052f522009-04-08 19:55:47 -0700877
878 smp_fill_in_sib_core_maps();
David S. Miller5cbc3072007-05-25 15:49:59 -0700879}
880
David S. Miller0fdb7f92007-08-15 21:02:23 -0700881static ssize_t mdesc_read(struct file *file, char __user *buf,
882 size_t len, loff_t *offp)
883{
884 struct mdesc_handle *hp = mdesc_grab();
885 int err;
886
887 if (!hp)
888 return -ENODEV;
889
890 err = hp->handle_size;
891 if (len < hp->handle_size)
892 err = -EMSGSIZE;
893 else if (copy_to_user(buf, &hp->mdesc, hp->handle_size))
894 err = -EFAULT;
895 mdesc_release(hp);
896
897 return err;
898}
899
900static const struct file_operations mdesc_fops = {
901 .read = mdesc_read,
902 .owner = THIS_MODULE,
903};
904
905static struct miscdevice mdesc_misc = {
906 .minor = MISC_DYNAMIC_MINOR,
907 .name = "mdesc",
908 .fops = &mdesc_fops,
909};
910
911static int __init mdesc_misc_init(void)
912{
913 return misc_register(&mdesc_misc);
914}
915
916__initcall(mdesc_misc_init);
917
David S. Miller5cbc3072007-05-25 15:49:59 -0700918void __init sun4v_mdesc_init(void)
919{
David S. Miller43fdf272007-07-12 13:47:50 -0700920 struct mdesc_handle *hp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700921 unsigned long len, real_len, status;
922
923 (void) sun4v_mach_desc(0UL, 0UL, &len);
924
925 printk("MDESC: Size is %lu bytes.\n", len);
926
David S. Miller4a283332008-02-13 19:22:23 -0800927 hp = mdesc_alloc(len, &lmb_mdesc_ops);
David S. Miller43fdf272007-07-12 13:47:50 -0700928 if (hp == NULL) {
929 prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
930 prom_halt();
931 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700932
David S. Miller43fdf272007-07-12 13:47:50 -0700933 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700934 if (status != HV_EOK || real_len > len) {
935 prom_printf("sun4v_mach_desc fails, err(%lu), "
936 "len(%lu), real_len(%lu)\n",
937 status, len, real_len);
David S. Miller43fdf272007-07-12 13:47:50 -0700938 mdesc_free(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700939 prom_halt();
940 }
941
David S. Miller43fdf272007-07-12 13:47:50 -0700942 cur_mdesc = hp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700943
944 report_platform_properties();
David S. Miller5cbc3072007-05-25 15:49:59 -0700945}