blob: 95059c2ec414804e50d63681b60ddd2ccfaa4be4 [file] [log] [blame]
David S. Miller5cbc3072007-05-25 15:49:59 -07001/* mdesc.c: Sun4V machine description handling.
2 *
3 * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
4 */
5#include <linux/kernel.h>
6#include <linux/types.h>
7#include <linux/bootmem.h>
8#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. Miller5cbc3072007-05-25 15:49:59 -070012
13#include <asm/hypervisor.h>
14#include <asm/mdesc.h>
15#include <asm/prom.h>
16#include <asm/oplib.h>
17#include <asm/smp.h>
18
19/* Unlike the OBP device tree, the machine description is a full-on
20 * DAG. An arbitrary number of ARCs are possible from one
21 * node to other nodes and thus we can't use the OBP device_node
22 * data structure to represent these nodes inside of the kernel.
23 *
24 * Actually, it isn't even a DAG, because there are back pointers
25 * which create cycles in the graph.
26 *
27 * mdesc_hdr and mdesc_elem describe the layout of the data structure
28 * we get from the Hypervisor.
29 */
30struct mdesc_hdr {
31 u32 version; /* Transport version */
32 u32 node_sz; /* node block size */
33 u32 name_sz; /* name block size */
34 u32 data_sz; /* data block size */
David S. Miller43fdf272007-07-12 13:47:50 -070035} __attribute__((aligned(16)));
David S. Miller5cbc3072007-05-25 15:49:59 -070036
37struct mdesc_elem {
38 u8 tag;
39#define MD_LIST_END 0x00
40#define MD_NODE 0x4e
41#define MD_NODE_END 0x45
42#define MD_NOOP 0x20
43#define MD_PROP_ARC 0x61
44#define MD_PROP_VAL 0x76
45#define MD_PROP_STR 0x73
46#define MD_PROP_DATA 0x64
47 u8 name_len;
48 u16 resv;
49 u32 name_offset;
50 union {
51 struct {
52 u32 data_len;
53 u32 data_offset;
54 } data;
55 u64 val;
56 } d;
57};
58
David S. Miller43fdf272007-07-12 13:47:50 -070059struct mdesc_mem_ops {
60 struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
61 void (*free)(struct mdesc_handle *handle);
62};
David S. Miller5cbc3072007-05-25 15:49:59 -070063
David S. Miller43fdf272007-07-12 13:47:50 -070064struct mdesc_handle {
65 struct list_head list;
66 struct mdesc_mem_ops *mops;
67 void *self_base;
68 atomic_t refcnt;
69 unsigned int handle_size;
70 struct mdesc_hdr mdesc;
71};
David S. Miller5cbc3072007-05-25 15:49:59 -070072
David S. Miller43fdf272007-07-12 13:47:50 -070073static void mdesc_handle_init(struct mdesc_handle *hp,
74 unsigned int handle_size,
75 void *base)
David S. Miller5cbc3072007-05-25 15:49:59 -070076{
David S. Miller43fdf272007-07-12 13:47:50 -070077 BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));
78
79 memset(hp, 0, handle_size);
80 INIT_LIST_HEAD(&hp->list);
81 hp->self_base = base;
82 atomic_set(&hp->refcnt, 1);
83 hp->handle_size = handle_size;
David S. Miller5cbc3072007-05-25 15:49:59 -070084}
85
Sam Ravnborg1256efd2007-07-20 17:20:56 -070086static struct mdesc_handle * __init mdesc_bootmem_alloc(unsigned int mdesc_size)
David S. Miller5cbc3072007-05-25 15:49:59 -070087{
David S. Miller43fdf272007-07-12 13:47:50 -070088 struct mdesc_handle *hp;
89 unsigned int handle_size, alloc_size;
David S. Miller5cbc3072007-05-25 15:49:59 -070090
David S. Miller43fdf272007-07-12 13:47:50 -070091 handle_size = (sizeof(struct mdesc_handle) -
92 sizeof(struct mdesc_hdr) +
93 mdesc_size);
94 alloc_size = PAGE_ALIGN(handle_size);
David S. Miller5cbc3072007-05-25 15:49:59 -070095
David S. Miller43fdf272007-07-12 13:47:50 -070096 hp = __alloc_bootmem(alloc_size, PAGE_SIZE, 0UL);
97 if (hp)
98 mdesc_handle_init(hp, handle_size, hp);
99
100 return hp;
101}
102
103static void mdesc_bootmem_free(struct mdesc_handle *hp)
104{
105 unsigned int alloc_size, handle_size = hp->handle_size;
106 unsigned long start, end;
107
108 BUG_ON(atomic_read(&hp->refcnt) != 0);
109 BUG_ON(!list_empty(&hp->list));
110
111 alloc_size = PAGE_ALIGN(handle_size);
112
113 start = (unsigned long) hp;
114 end = start + alloc_size;
115
116 while (start < end) {
117 struct page *p;
118
119 p = virt_to_page(start);
120 ClearPageReserved(p);
121 __free_page(p);
122 start += PAGE_SIZE;
David S. Miller5cbc3072007-05-25 15:49:59 -0700123 }
124}
125
Sam Ravnborg1256efd2007-07-20 17:20:56 -0700126static struct mdesc_mem_ops bootmem_mdesc_ops = {
David S. Miller43fdf272007-07-12 13:47:50 -0700127 .alloc = mdesc_bootmem_alloc,
128 .free = mdesc_bootmem_free,
129};
130
131static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
David S. Miller5cbc3072007-05-25 15:49:59 -0700132{
David S. Miller43fdf272007-07-12 13:47:50 -0700133 unsigned int handle_size;
134 void *base;
David S. Miller5cbc3072007-05-25 15:49:59 -0700135
David S. Miller43fdf272007-07-12 13:47:50 -0700136 handle_size = (sizeof(struct mdesc_handle) -
137 sizeof(struct mdesc_hdr) +
138 mdesc_size);
David S. Miller5cbc3072007-05-25 15:49:59 -0700139
David S. Miller920c3ed2007-07-17 21:37:35 -0700140 base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_NOFAIL);
David S. Miller43fdf272007-07-12 13:47:50 -0700141 if (base) {
142 struct mdesc_handle *hp;
143 unsigned long addr;
144
145 addr = (unsigned long)base;
146 addr = (addr + 15UL) & ~15UL;
147 hp = (struct mdesc_handle *) addr;
148
149 mdesc_handle_init(hp, handle_size, base);
150 return hp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700151 }
David S. Miller43fdf272007-07-12 13:47:50 -0700152
David S. Miller5cbc3072007-05-25 15:49:59 -0700153 return NULL;
154}
155
David S. Miller43fdf272007-07-12 13:47:50 -0700156static void mdesc_kfree(struct mdesc_handle *hp)
David S. Miller5cbc3072007-05-25 15:49:59 -0700157{
David S. Miller43fdf272007-07-12 13:47:50 -0700158 BUG_ON(atomic_read(&hp->refcnt) != 0);
159 BUG_ON(!list_empty(&hp->list));
David S. Miller5cbc3072007-05-25 15:49:59 -0700160
David S. Miller43fdf272007-07-12 13:47:50 -0700161 kfree(hp->self_base);
162}
163
164static struct mdesc_mem_ops kmalloc_mdesc_memops = {
165 .alloc = mdesc_kmalloc,
166 .free = mdesc_kfree,
167};
168
169static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
170 struct mdesc_mem_ops *mops)
171{
172 struct mdesc_handle *hp = mops->alloc(mdesc_size);
173
174 if (hp)
175 hp->mops = mops;
176
177 return hp;
178}
179
180static void mdesc_free(struct mdesc_handle *hp)
181{
182 hp->mops->free(hp);
183}
184
185static struct mdesc_handle *cur_mdesc;
186static LIST_HEAD(mdesc_zombie_list);
187static DEFINE_SPINLOCK(mdesc_lock);
188
189struct mdesc_handle *mdesc_grab(void)
190{
191 struct mdesc_handle *hp;
192 unsigned long flags;
193
194 spin_lock_irqsave(&mdesc_lock, flags);
195 hp = cur_mdesc;
196 if (hp)
197 atomic_inc(&hp->refcnt);
198 spin_unlock_irqrestore(&mdesc_lock, flags);
199
200 return hp;
201}
202EXPORT_SYMBOL(mdesc_grab);
203
204void mdesc_release(struct mdesc_handle *hp)
205{
206 unsigned long flags;
207
208 spin_lock_irqsave(&mdesc_lock, flags);
209 if (atomic_dec_and_test(&hp->refcnt)) {
210 list_del_init(&hp->list);
211 hp->mops->free(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700212 }
David S. Miller43fdf272007-07-12 13:47:50 -0700213 spin_unlock_irqrestore(&mdesc_lock, flags);
David S. Miller5cbc3072007-05-25 15:49:59 -0700214}
David S. Miller43fdf272007-07-12 13:47:50 -0700215EXPORT_SYMBOL(mdesc_release);
David S. Miller5cbc3072007-05-25 15:49:59 -0700216
David S. Miller920c3ed2007-07-17 21:37:35 -0700217static DEFINE_MUTEX(mdesc_mutex);
218static struct mdesc_notifier_client *client_list;
219
220void mdesc_register_notifier(struct mdesc_notifier_client *client)
221{
222 u64 node;
223
224 mutex_lock(&mdesc_mutex);
225 client->next = client_list;
226 client_list = client;
227
228 mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
229 client->add(cur_mdesc, node);
230
231 mutex_unlock(&mdesc_mutex);
232}
233
David S. Miller91ba3c22007-07-18 15:15:45 -0700234static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
235{
236 const u64 *id;
237 u64 a;
238
239 id = NULL;
240 mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
241 u64 target;
242
243 target = mdesc_arc_target(hp, a);
244 id = mdesc_get_property(hp, target,
245 "cfg-handle", NULL);
246 if (id)
247 break;
248 }
249
250 return id;
251}
252
David S. Miller920c3ed2007-07-17 21:37:35 -0700253/* Run 'func' on nodes which are in A but not in B. */
254static void invoke_on_missing(const char *name,
255 struct mdesc_handle *a,
256 struct mdesc_handle *b,
257 void (*func)(struct mdesc_handle *, u64))
258{
259 u64 node;
260
261 mdesc_for_each_node_by_name(a, node, name) {
David S. Miller91ba3c22007-07-18 15:15:45 -0700262 int found = 0, is_vdc_port = 0;
263 const char *name_prop;
264 const u64 *id;
David S. Miller920c3ed2007-07-17 21:37:35 -0700265 u64 fnode;
266
David S. Miller91ba3c22007-07-18 15:15:45 -0700267 name_prop = mdesc_get_property(a, node, "name", NULL);
268 if (name_prop && !strcmp(name_prop, "vdc-port")) {
269 is_vdc_port = 1;
270 id = parent_cfg_handle(a, node);
271 } else
272 id = mdesc_get_property(a, node, "id", NULL);
273
274 if (!id) {
275 printk(KERN_ERR "MD: Cannot find ID for %s node.\n",
276 (name_prop ? name_prop : name));
277 continue;
278 }
279
David S. Miller920c3ed2007-07-17 21:37:35 -0700280 mdesc_for_each_node_by_name(b, fnode, name) {
David S. Miller91ba3c22007-07-18 15:15:45 -0700281 const u64 *fid;
282
283 if (is_vdc_port) {
284 name_prop = mdesc_get_property(b, fnode,
285 "name", NULL);
286 if (!name_prop ||
287 strcmp(name_prop, "vdc-port"))
288 continue;
289 fid = parent_cfg_handle(b, fnode);
290 if (!fid) {
291 printk(KERN_ERR "MD: Cannot find ID "
292 "for vdc-port node.\n");
293 continue;
294 }
295 } else
296 fid = mdesc_get_property(b, fnode,
297 "id", NULL);
David S. Miller920c3ed2007-07-17 21:37:35 -0700298
299 if (*id == *fid) {
300 found = 1;
301 break;
302 }
303 }
304 if (!found)
305 func(a, node);
306 }
307}
308
309static void notify_one(struct mdesc_notifier_client *p,
310 struct mdesc_handle *old_hp,
311 struct mdesc_handle *new_hp)
312{
313 invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
314 invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
315}
316
317static void mdesc_notify_clients(struct mdesc_handle *old_hp,
318 struct mdesc_handle *new_hp)
319{
320 struct mdesc_notifier_client *p = client_list;
321
322 while (p) {
323 notify_one(p, old_hp, new_hp);
324 p = p->next;
325 }
326}
327
David S. Miller778feeb2007-07-16 16:50:36 -0700328void mdesc_update(void)
David S. Miller5cbc3072007-05-25 15:49:59 -0700329{
David S. Miller43fdf272007-07-12 13:47:50 -0700330 unsigned long len, real_len, status;
331 struct mdesc_handle *hp, *orig_hp;
332 unsigned long flags;
David S. Miller5cbc3072007-05-25 15:49:59 -0700333
David S. Miller920c3ed2007-07-17 21:37:35 -0700334 mutex_lock(&mdesc_mutex);
335
David S. Miller43fdf272007-07-12 13:47:50 -0700336 (void) sun4v_mach_desc(0UL, 0UL, &len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700337
David S. Miller43fdf272007-07-12 13:47:50 -0700338 hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
339 if (!hp) {
340 printk(KERN_ERR "MD: mdesc alloc fails\n");
David S. Miller920c3ed2007-07-17 21:37:35 -0700341 goto out;
David S. Miller5cbc3072007-05-25 15:49:59 -0700342 }
343
David S. Miller43fdf272007-07-12 13:47:50 -0700344 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
345 if (status != HV_EOK || real_len > len) {
346 printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
347 status);
348 atomic_dec(&hp->refcnt);
349 mdesc_free(hp);
David S. Miller920c3ed2007-07-17 21:37:35 -0700350 goto out;
David S. Miller5cbc3072007-05-25 15:49:59 -0700351 }
David S. Miller43fdf272007-07-12 13:47:50 -0700352
353 spin_lock_irqsave(&mdesc_lock, flags);
354 orig_hp = cur_mdesc;
355 cur_mdesc = hp;
David S. Miller920c3ed2007-07-17 21:37:35 -0700356 spin_unlock_irqrestore(&mdesc_lock, flags);
David S. Miller43fdf272007-07-12 13:47:50 -0700357
David S. Miller920c3ed2007-07-17 21:37:35 -0700358 mdesc_notify_clients(orig_hp, hp);
359
360 spin_lock_irqsave(&mdesc_lock, flags);
David S. Miller43fdf272007-07-12 13:47:50 -0700361 if (atomic_dec_and_test(&orig_hp->refcnt))
362 mdesc_free(orig_hp);
363 else
364 list_add(&orig_hp->list, &mdesc_zombie_list);
365 spin_unlock_irqrestore(&mdesc_lock, flags);
David S. Miller920c3ed2007-07-17 21:37:35 -0700366
367out:
368 mutex_unlock(&mdesc_mutex);
David S. Miller5cbc3072007-05-25 15:49:59 -0700369}
370
David S. Miller43fdf272007-07-12 13:47:50 -0700371static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700372{
373 return (struct mdesc_elem *) (mdesc + 1);
374}
375
David S. Miller43fdf272007-07-12 13:47:50 -0700376static void *name_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700377{
378 return ((void *) node_block(mdesc)) + mdesc->node_sz;
379}
380
David S. Miller43fdf272007-07-12 13:47:50 -0700381static void *data_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700382{
383 return ((void *) name_block(mdesc)) + mdesc->name_sz;
384}
385
David S. Miller43fdf272007-07-12 13:47:50 -0700386u64 mdesc_node_by_name(struct mdesc_handle *hp,
387 u64 from_node, const char *name)
David S. Miller5cbc3072007-05-25 15:49:59 -0700388{
David S. Miller43fdf272007-07-12 13:47:50 -0700389 struct mdesc_elem *ep = node_block(&hp->mdesc);
390 const char *names = name_block(&hp->mdesc);
391 u64 last_node = hp->mdesc.node_sz / 16;
392 u64 ret;
David S. Miller5cbc3072007-05-25 15:49:59 -0700393
David S. Miller778feeb2007-07-16 16:50:36 -0700394 if (from_node == MDESC_NODE_NULL) {
395 ret = from_node = 0;
396 } else if (from_node >= last_node) {
David S. Miller43fdf272007-07-12 13:47:50 -0700397 return MDESC_NODE_NULL;
David S. Miller778feeb2007-07-16 16:50:36 -0700398 } else {
399 ret = ep[from_node].d.val;
400 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700401
David S. Miller43fdf272007-07-12 13:47:50 -0700402 while (ret < last_node) {
403 if (ep[ret].tag != MD_NODE)
404 return MDESC_NODE_NULL;
405 if (!strcmp(names + ep[ret].name_offset, name))
406 break;
407 ret = ep[ret].d.val;
408 }
409 if (ret >= last_node)
410 ret = MDESC_NODE_NULL;
411 return ret;
412}
413EXPORT_SYMBOL(mdesc_node_by_name);
David S. Miller5cbc3072007-05-25 15:49:59 -0700414
David S. Miller43fdf272007-07-12 13:47:50 -0700415const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
416 const char *name, int *lenp)
417{
418 const char *names = name_block(&hp->mdesc);
419 u64 last_node = hp->mdesc.node_sz / 16;
420 void *data = data_block(&hp->mdesc);
421 struct mdesc_elem *ep;
422
423 if (node == MDESC_NODE_NULL || node >= last_node)
424 return NULL;
425
426 ep = node_block(&hp->mdesc) + node;
427 ep++;
428 for (; ep->tag != MD_NODE_END; ep++) {
429 void *val = NULL;
430 int len = 0;
431
432 switch (ep->tag) {
433 case MD_PROP_VAL:
434 val = &ep->d.val;
435 len = 8;
David S. Miller5cbc3072007-05-25 15:49:59 -0700436 break;
437
David S. Miller43fdf272007-07-12 13:47:50 -0700438 case MD_PROP_STR:
439 case MD_PROP_DATA:
440 val = data + ep->d.data.data_offset;
441 len = ep->d.data.data_len;
442 break;
David S. Miller5cbc3072007-05-25 15:49:59 -0700443
David S. Miller43fdf272007-07-12 13:47:50 -0700444 default:
David S. Miller5cbc3072007-05-25 15:49:59 -0700445 break;
446 }
David S. Miller43fdf272007-07-12 13:47:50 -0700447 if (!val)
448 continue;
David S. Miller5cbc3072007-05-25 15:49:59 -0700449
David S. Miller43fdf272007-07-12 13:47:50 -0700450 if (!strcmp(names + ep->name_offset, name)) {
451 if (lenp)
452 *lenp = len;
453 return val;
David S. Miller5cbc3072007-05-25 15:49:59 -0700454 }
455 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700456
David S. Miller43fdf272007-07-12 13:47:50 -0700457 return NULL;
458}
459EXPORT_SYMBOL(mdesc_get_property);
460
461u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
David S. Miller5cbc3072007-05-25 15:49:59 -0700462{
David S. Miller43fdf272007-07-12 13:47:50 -0700463 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
464 const char *names = name_block(&hp->mdesc);
465 u64 last_node = hp->mdesc.node_sz / 16;
David S. Miller5cbc3072007-05-25 15:49:59 -0700466
David S. Miller43fdf272007-07-12 13:47:50 -0700467 if (from == MDESC_NODE_NULL || from >= last_node)
468 return MDESC_NODE_NULL;
469
470 ep = base + from;
471
472 ep++;
473 for (; ep->tag != MD_NODE_END; ep++) {
474 if (ep->tag != MD_PROP_ARC)
475 continue;
476
477 if (strcmp(names + ep->name_offset, arc_type))
478 continue;
479
480 return ep - base;
David S. Miller5cbc3072007-05-25 15:49:59 -0700481 }
David S. Miller43fdf272007-07-12 13:47:50 -0700482
483 return MDESC_NODE_NULL;
David S. Miller5cbc3072007-05-25 15:49:59 -0700484}
David S. Miller43fdf272007-07-12 13:47:50 -0700485EXPORT_SYMBOL(mdesc_next_arc);
486
487u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
488{
489 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
490
491 ep = base + arc;
492
493 return ep->d.val;
494}
495EXPORT_SYMBOL(mdesc_arc_target);
496
497const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
498{
499 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
500 const char *names = name_block(&hp->mdesc);
501 u64 last_node = hp->mdesc.node_sz / 16;
502
503 if (node == MDESC_NODE_NULL || node >= last_node)
504 return NULL;
505
506 ep = base + node;
507 if (ep->tag != MD_NODE)
508 return NULL;
509
510 return names + ep->name_offset;
511}
512EXPORT_SYMBOL(mdesc_node_name);
David S. Miller5cbc3072007-05-25 15:49:59 -0700513
514static void __init report_platform_properties(void)
515{
David S. Miller43fdf272007-07-12 13:47:50 -0700516 struct mdesc_handle *hp = mdesc_grab();
517 u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
David S. Miller5cbc3072007-05-25 15:49:59 -0700518 const char *s;
519 const u64 *v;
520
David S. Miller43fdf272007-07-12 13:47:50 -0700521 if (pn == MDESC_NODE_NULL) {
David S. Miller5cbc3072007-05-25 15:49:59 -0700522 prom_printf("No platform node in machine-description.\n");
523 prom_halt();
524 }
525
David S. Miller43fdf272007-07-12 13:47:50 -0700526 s = mdesc_get_property(hp, pn, "banner-name", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700527 printk("PLATFORM: banner-name [%s]\n", s);
David S. Miller43fdf272007-07-12 13:47:50 -0700528 s = mdesc_get_property(hp, pn, "name", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700529 printk("PLATFORM: name [%s]\n", s);
530
David S. Miller43fdf272007-07-12 13:47:50 -0700531 v = mdesc_get_property(hp, pn, "hostid", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700532 if (v)
533 printk("PLATFORM: hostid [%08lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700534 v = mdesc_get_property(hp, pn, "serial#", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700535 if (v)
536 printk("PLATFORM: serial# [%08lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700537 v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700538 printk("PLATFORM: stick-frequency [%08lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700539 v = mdesc_get_property(hp, pn, "mac-address", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700540 if (v)
541 printk("PLATFORM: mac-address [%lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700542 v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700543 if (v)
544 printk("PLATFORM: watchdog-resolution [%lu ms]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700545 v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700546 if (v)
547 printk("PLATFORM: watchdog-max-timeout [%lu ms]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700548 v = mdesc_get_property(hp, pn, "max-cpus", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700549 if (v)
550 printk("PLATFORM: max-cpus [%lu]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700551
David S. Miller4f0234f2007-07-13 16:03:42 -0700552#ifdef CONFIG_SMP
553 {
554 int max_cpu, i;
555
556 if (v) {
557 max_cpu = *v;
558 if (max_cpu > NR_CPUS)
559 max_cpu = NR_CPUS;
560 } else {
561 max_cpu = NR_CPUS;
562 }
563 for (i = 0; i < max_cpu; i++)
564 cpu_set(i, cpu_possible_map);
565 }
566#endif
567
David S. Miller43fdf272007-07-12 13:47:50 -0700568 mdesc_release(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700569}
570
David S. Miller4f0234f2007-07-13 16:03:42 -0700571static void __devinit fill_in_one_cache(cpuinfo_sparc *c,
572 struct mdesc_handle *hp,
573 u64 mp)
David S. Miller5cbc3072007-05-25 15:49:59 -0700574{
David S. Miller43fdf272007-07-12 13:47:50 -0700575 const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
576 const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
577 const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700578 const char *type;
579 int type_len;
580
David S. Miller43fdf272007-07-12 13:47:50 -0700581 type = mdesc_get_property(hp, mp, "type", &type_len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700582
583 switch (*level) {
584 case 1:
David S. Miller46bcea72007-08-07 18:46:36 -0700585 if (of_find_in_proplist(type, "instn", type_len)) {
David S. Miller5cbc3072007-05-25 15:49:59 -0700586 c->icache_size = *size;
587 c->icache_line_size = *line_size;
David S. Miller46bcea72007-08-07 18:46:36 -0700588 } else if (of_find_in_proplist(type, "data", type_len)) {
David S. Miller5cbc3072007-05-25 15:49:59 -0700589 c->dcache_size = *size;
590 c->dcache_line_size = *line_size;
591 }
592 break;
593
594 case 2:
595 c->ecache_size = *size;
596 c->ecache_line_size = *line_size;
597 break;
598
599 default:
600 break;
601 }
602
603 if (*level == 1) {
David S. Miller43fdf272007-07-12 13:47:50 -0700604 u64 a;
David S. Miller5cbc3072007-05-25 15:49:59 -0700605
David S. Miller43fdf272007-07-12 13:47:50 -0700606 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
607 u64 target = mdesc_arc_target(hp, a);
608 const char *name = mdesc_node_name(hp, target);
David S. Miller5cbc3072007-05-25 15:49:59 -0700609
David S. Miller43fdf272007-07-12 13:47:50 -0700610 if (!strcmp(name, "cache"))
611 fill_in_one_cache(c, hp, target);
David S. Miller5cbc3072007-05-25 15:49:59 -0700612 }
613 }
614}
615
David S. Miller4f0234f2007-07-13 16:03:42 -0700616static void __devinit mark_core_ids(struct mdesc_handle *hp, u64 mp,
617 int core_id)
David S. Miller5cbc3072007-05-25 15:49:59 -0700618{
David S. Miller43fdf272007-07-12 13:47:50 -0700619 u64 a;
David S. Miller5cbc3072007-05-25 15:49:59 -0700620
David S. Miller43fdf272007-07-12 13:47:50 -0700621 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
622 u64 t = mdesc_arc_target(hp, a);
623 const char *name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700624 const u64 *id;
625
David S. Miller43fdf272007-07-12 13:47:50 -0700626 name = mdesc_node_name(hp, t);
627 if (!strcmp(name, "cpu")) {
628 id = mdesc_get_property(hp, t, "id", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700629 if (*id < NR_CPUS)
630 cpu_data(*id).core_id = core_id;
631 } else {
David S. Miller43fdf272007-07-12 13:47:50 -0700632 u64 j;
David S. Miller5cbc3072007-05-25 15:49:59 -0700633
David S. Miller43fdf272007-07-12 13:47:50 -0700634 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_BACK) {
635 u64 n = mdesc_arc_target(hp, j);
636 const char *n_name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700637
David S. Miller43fdf272007-07-12 13:47:50 -0700638 n_name = mdesc_node_name(hp, n);
639 if (strcmp(n_name, "cpu"))
David S. Miller5cbc3072007-05-25 15:49:59 -0700640 continue;
641
David S. Miller43fdf272007-07-12 13:47:50 -0700642 id = mdesc_get_property(hp, n, "id", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700643 if (*id < NR_CPUS)
644 cpu_data(*id).core_id = core_id;
645 }
646 }
647 }
648}
649
David S. Miller4f0234f2007-07-13 16:03:42 -0700650static void __devinit set_core_ids(struct mdesc_handle *hp)
David S. Miller5cbc3072007-05-25 15:49:59 -0700651{
David S. Miller5cbc3072007-05-25 15:49:59 -0700652 int idx;
David S. Miller43fdf272007-07-12 13:47:50 -0700653 u64 mp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700654
655 idx = 1;
David S. Miller43fdf272007-07-12 13:47:50 -0700656 mdesc_for_each_node_by_name(hp, mp, "cache") {
657 const u64 *level;
David S. Miller5cbc3072007-05-25 15:49:59 -0700658 const char *type;
659 int len;
660
David S. Miller43fdf272007-07-12 13:47:50 -0700661 level = mdesc_get_property(hp, mp, "level", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700662 if (*level != 1)
663 continue;
664
David S. Miller43fdf272007-07-12 13:47:50 -0700665 type = mdesc_get_property(hp, mp, "type", &len);
David S. Miller46bcea72007-08-07 18:46:36 -0700666 if (!of_find_in_proplist(type, "instn", len))
David S. Miller5cbc3072007-05-25 15:49:59 -0700667 continue;
668
David S. Miller43fdf272007-07-12 13:47:50 -0700669 mark_core_ids(hp, mp, idx);
David S. Miller5cbc3072007-05-25 15:49:59 -0700670
671 idx++;
672 }
673}
674
David S. Miller4f0234f2007-07-13 16:03:42 -0700675static void __devinit mark_proc_ids(struct mdesc_handle *hp, u64 mp,
676 int proc_id)
David S. Millerf78eae22007-06-04 17:01:39 -0700677{
David S. Miller43fdf272007-07-12 13:47:50 -0700678 u64 a;
David S. Millerf78eae22007-06-04 17:01:39 -0700679
David S. Miller43fdf272007-07-12 13:47:50 -0700680 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
681 u64 t = mdesc_arc_target(hp, a);
682 const char *name;
David S. Millerf78eae22007-06-04 17:01:39 -0700683 const u64 *id;
684
David S. Miller43fdf272007-07-12 13:47:50 -0700685 name = mdesc_node_name(hp, t);
686 if (strcmp(name, "cpu"))
David S. Millerf78eae22007-06-04 17:01:39 -0700687 continue;
688
David S. Miller43fdf272007-07-12 13:47:50 -0700689 id = mdesc_get_property(hp, t, "id", NULL);
David S. Millerf78eae22007-06-04 17:01:39 -0700690 if (*id < NR_CPUS)
691 cpu_data(*id).proc_id = proc_id;
692 }
693}
694
David S. Miller4f0234f2007-07-13 16:03:42 -0700695static void __devinit __set_proc_ids(struct mdesc_handle *hp,
696 const char *exec_unit_name)
David S. Millerf78eae22007-06-04 17:01:39 -0700697{
David S. Millerf78eae22007-06-04 17:01:39 -0700698 int idx;
David S. Miller43fdf272007-07-12 13:47:50 -0700699 u64 mp;
David S. Millerf78eae22007-06-04 17:01:39 -0700700
701 idx = 0;
David S. Miller43fdf272007-07-12 13:47:50 -0700702 mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
David S. Millerf78eae22007-06-04 17:01:39 -0700703 const char *type;
704 int len;
705
David S. Miller43fdf272007-07-12 13:47:50 -0700706 type = mdesc_get_property(hp, mp, "type", &len);
David S. Miller46bcea72007-08-07 18:46:36 -0700707 if (!of_find_in_proplist(type, "int", len) &&
708 !of_find_in_proplist(type, "integer", len))
David S. Millerf78eae22007-06-04 17:01:39 -0700709 continue;
710
David S. Miller43fdf272007-07-12 13:47:50 -0700711 mark_proc_ids(hp, mp, idx);
David S. Millerf78eae22007-06-04 17:01:39 -0700712
713 idx++;
714 }
715}
716
David S. Miller4f0234f2007-07-13 16:03:42 -0700717static void __devinit set_proc_ids(struct mdesc_handle *hp)
David S. Millerf78eae22007-06-04 17:01:39 -0700718{
David S. Miller43fdf272007-07-12 13:47:50 -0700719 __set_proc_ids(hp, "exec_unit");
720 __set_proc_ids(hp, "exec-unit");
David S. Millerf78eae22007-06-04 17:01:39 -0700721}
722
David S. Miller4f0234f2007-07-13 16:03:42 -0700723static void __devinit get_one_mondo_bits(const u64 *p, unsigned int *mask,
724 unsigned char def)
David S. Miller5cbc3072007-05-25 15:49:59 -0700725{
726 u64 val;
727
728 if (!p)
729 goto use_default;
730 val = *p;
731
732 if (!val || val >= 64)
733 goto use_default;
734
735 *mask = ((1U << val) * 64U) - 1U;
736 return;
737
738use_default:
739 *mask = ((1U << def) * 64U) - 1U;
740}
741
David S. Miller4f0234f2007-07-13 16:03:42 -0700742static void __devinit get_mondo_data(struct mdesc_handle *hp, u64 mp,
743 struct trap_per_cpu *tb)
David S. Miller5cbc3072007-05-25 15:49:59 -0700744{
745 const u64 *val;
746
David S. Miller43fdf272007-07-12 13:47:50 -0700747 val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700748 get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7);
749
David S. Miller43fdf272007-07-12 13:47:50 -0700750 val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700751 get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7);
752
David S. Miller43fdf272007-07-12 13:47:50 -0700753 val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700754 get_one_mondo_bits(val, &tb->resum_qmask, 6);
755
David S. Miller43fdf272007-07-12 13:47:50 -0700756 val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700757 get_one_mondo_bits(val, &tb->nonresum_qmask, 2);
758}
759
David S. Miller4f0234f2007-07-13 16:03:42 -0700760void __devinit mdesc_fill_in_cpu_data(cpumask_t mask)
David S. Miller5cbc3072007-05-25 15:49:59 -0700761{
David S. Miller43fdf272007-07-12 13:47:50 -0700762 struct mdesc_handle *hp = mdesc_grab();
763 u64 mp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700764
765 ncpus_probed = 0;
David S. Miller43fdf272007-07-12 13:47:50 -0700766 mdesc_for_each_node_by_name(hp, mp, "cpu") {
767 const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
768 const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700769 struct trap_per_cpu *tb;
770 cpuinfo_sparc *c;
David S. Miller5cbc3072007-05-25 15:49:59 -0700771 int cpuid;
David S. Miller43fdf272007-07-12 13:47:50 -0700772 u64 a;
David S. Miller5cbc3072007-05-25 15:49:59 -0700773
774 ncpus_probed++;
775
776 cpuid = *id;
777
778#ifdef CONFIG_SMP
779 if (cpuid >= NR_CPUS)
780 continue;
David S. Miller4f0234f2007-07-13 16:03:42 -0700781 if (!cpu_isset(cpuid, mask))
782 continue;
David S. Miller5cbc3072007-05-25 15:49:59 -0700783#else
784 /* On uniprocessor we only want the values for the
785 * real physical cpu the kernel booted onto, however
786 * cpu_data() only has one entry at index 0.
787 */
788 if (cpuid != real_hard_smp_processor_id())
789 continue;
790 cpuid = 0;
791#endif
792
793 c = &cpu_data(cpuid);
794 c->clock_tick = *cfreq;
795
796 tb = &trap_block[cpuid];
David S. Miller43fdf272007-07-12 13:47:50 -0700797 get_mondo_data(hp, mp, tb);
David S. Miller5cbc3072007-05-25 15:49:59 -0700798
David S. Miller43fdf272007-07-12 13:47:50 -0700799 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
800 u64 j, t = mdesc_arc_target(hp, a);
801 const char *t_name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700802
David S. Miller43fdf272007-07-12 13:47:50 -0700803 t_name = mdesc_node_name(hp, t);
804 if (!strcmp(t_name, "cache")) {
805 fill_in_one_cache(c, hp, t);
David S. Miller5cbc3072007-05-25 15:49:59 -0700806 continue;
807 }
808
David S. Miller43fdf272007-07-12 13:47:50 -0700809 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
810 u64 n = mdesc_arc_target(hp, j);
811 const char *n_name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700812
David S. Miller43fdf272007-07-12 13:47:50 -0700813 n_name = mdesc_node_name(hp, n);
814 if (!strcmp(n_name, "cache"))
815 fill_in_one_cache(c, hp, n);
David S. Miller5cbc3072007-05-25 15:49:59 -0700816 }
817 }
818
819#ifdef CONFIG_SMP
820 cpu_set(cpuid, cpu_present_map);
David S. Miller5cbc3072007-05-25 15:49:59 -0700821#endif
822
823 c->core_id = 0;
David S. Millerf78eae22007-06-04 17:01:39 -0700824 c->proc_id = -1;
David S. Miller5cbc3072007-05-25 15:49:59 -0700825 }
826
David S. Millera2f9f6b2007-06-04 21:48:33 -0700827#ifdef CONFIG_SMP
828 sparc64_multi_core = 1;
829#endif
830
David S. Miller43fdf272007-07-12 13:47:50 -0700831 set_core_ids(hp);
832 set_proc_ids(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700833
834 smp_fill_in_sib_core_maps();
David S. Miller43fdf272007-07-12 13:47:50 -0700835
836 mdesc_release(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700837}
838
839void __init sun4v_mdesc_init(void)
840{
David S. Miller43fdf272007-07-12 13:47:50 -0700841 struct mdesc_handle *hp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700842 unsigned long len, real_len, status;
David S. Miller4f0234f2007-07-13 16:03:42 -0700843 cpumask_t mask;
David S. Miller5cbc3072007-05-25 15:49:59 -0700844
845 (void) sun4v_mach_desc(0UL, 0UL, &len);
846
847 printk("MDESC: Size is %lu bytes.\n", len);
848
Sam Ravnborg1256efd2007-07-20 17:20:56 -0700849 hp = mdesc_alloc(len, &bootmem_mdesc_ops);
David S. Miller43fdf272007-07-12 13:47:50 -0700850 if (hp == NULL) {
851 prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
852 prom_halt();
853 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700854
David S. Miller43fdf272007-07-12 13:47:50 -0700855 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700856 if (status != HV_EOK || real_len > len) {
857 prom_printf("sun4v_mach_desc fails, err(%lu), "
858 "len(%lu), real_len(%lu)\n",
859 status, len, real_len);
David S. Miller43fdf272007-07-12 13:47:50 -0700860 mdesc_free(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700861 prom_halt();
862 }
863
David S. Miller43fdf272007-07-12 13:47:50 -0700864 cur_mdesc = hp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700865
866 report_platform_properties();
David S. Miller4f0234f2007-07-13 16:03:42 -0700867
868 cpus_setall(mask);
869 mdesc_fill_in_cpu_data(mask);
David S. Miller5cbc3072007-05-25 15:49:59 -0700870}