blob: de5310ffdb480239903feec457fab1618e14520d [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
David S. Miller43fdf272007-07-12 13:47:50 -070086static struct mdesc_handle *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
David S. Miller43fdf272007-07-12 13:47:50 -0700126static struct mdesc_mem_ops bootmem_mdesc_memops = {
127 .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. Miller43fdf272007-07-12 13:47:50 -0700140 base = kmalloc(handle_size + 15, GFP_KERNEL);
141 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. Miller778feeb2007-07-16 16:50:36 -0700217void mdesc_update(void)
David S. Miller5cbc3072007-05-25 15:49:59 -0700218{
David S. Miller43fdf272007-07-12 13:47:50 -0700219 unsigned long len, real_len, status;
220 struct mdesc_handle *hp, *orig_hp;
221 unsigned long flags;
David S. Miller5cbc3072007-05-25 15:49:59 -0700222
David S. Miller43fdf272007-07-12 13:47:50 -0700223 (void) sun4v_mach_desc(0UL, 0UL, &len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700224
David S. Miller43fdf272007-07-12 13:47:50 -0700225 hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
226 if (!hp) {
227 printk(KERN_ERR "MD: mdesc alloc fails\n");
228 return;
David S. Miller5cbc3072007-05-25 15:49:59 -0700229 }
230
David S. Miller43fdf272007-07-12 13:47:50 -0700231 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
232 if (status != HV_EOK || real_len > len) {
233 printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
234 status);
235 atomic_dec(&hp->refcnt);
236 mdesc_free(hp);
237 return;
David S. Miller5cbc3072007-05-25 15:49:59 -0700238 }
David S. Miller43fdf272007-07-12 13:47:50 -0700239
240 spin_lock_irqsave(&mdesc_lock, flags);
241 orig_hp = cur_mdesc;
242 cur_mdesc = hp;
243
244 if (atomic_dec_and_test(&orig_hp->refcnt))
245 mdesc_free(orig_hp);
246 else
247 list_add(&orig_hp->list, &mdesc_zombie_list);
248 spin_unlock_irqrestore(&mdesc_lock, flags);
David S. Miller5cbc3072007-05-25 15:49:59 -0700249}
250
David S. Miller43fdf272007-07-12 13:47:50 -0700251static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700252{
253 return (struct mdesc_elem *) (mdesc + 1);
254}
255
David S. Miller43fdf272007-07-12 13:47:50 -0700256static void *name_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700257{
258 return ((void *) node_block(mdesc)) + mdesc->node_sz;
259}
260
David S. Miller43fdf272007-07-12 13:47:50 -0700261static void *data_block(struct mdesc_hdr *mdesc)
David S. Miller5cbc3072007-05-25 15:49:59 -0700262{
263 return ((void *) name_block(mdesc)) + mdesc->name_sz;
264}
265
David S. Miller43fdf272007-07-12 13:47:50 -0700266u64 mdesc_node_by_name(struct mdesc_handle *hp,
267 u64 from_node, const char *name)
David S. Miller5cbc3072007-05-25 15:49:59 -0700268{
David S. Miller43fdf272007-07-12 13:47:50 -0700269 struct mdesc_elem *ep = node_block(&hp->mdesc);
270 const char *names = name_block(&hp->mdesc);
271 u64 last_node = hp->mdesc.node_sz / 16;
272 u64 ret;
David S. Miller5cbc3072007-05-25 15:49:59 -0700273
David S. Miller778feeb2007-07-16 16:50:36 -0700274 if (from_node == MDESC_NODE_NULL) {
275 ret = from_node = 0;
276 } else if (from_node >= last_node) {
David S. Miller43fdf272007-07-12 13:47:50 -0700277 return MDESC_NODE_NULL;
David S. Miller778feeb2007-07-16 16:50:36 -0700278 } else {
279 ret = ep[from_node].d.val;
280 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700281
David S. Miller43fdf272007-07-12 13:47:50 -0700282 while (ret < last_node) {
283 if (ep[ret].tag != MD_NODE)
284 return MDESC_NODE_NULL;
285 if (!strcmp(names + ep[ret].name_offset, name))
286 break;
287 ret = ep[ret].d.val;
288 }
289 if (ret >= last_node)
290 ret = MDESC_NODE_NULL;
291 return ret;
292}
293EXPORT_SYMBOL(mdesc_node_by_name);
David S. Miller5cbc3072007-05-25 15:49:59 -0700294
David S. Miller43fdf272007-07-12 13:47:50 -0700295const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
296 const char *name, int *lenp)
297{
298 const char *names = name_block(&hp->mdesc);
299 u64 last_node = hp->mdesc.node_sz / 16;
300 void *data = data_block(&hp->mdesc);
301 struct mdesc_elem *ep;
302
303 if (node == MDESC_NODE_NULL || node >= last_node)
304 return NULL;
305
306 ep = node_block(&hp->mdesc) + node;
307 ep++;
308 for (; ep->tag != MD_NODE_END; ep++) {
309 void *val = NULL;
310 int len = 0;
311
312 switch (ep->tag) {
313 case MD_PROP_VAL:
314 val = &ep->d.val;
315 len = 8;
David S. Miller5cbc3072007-05-25 15:49:59 -0700316 break;
317
David S. Miller43fdf272007-07-12 13:47:50 -0700318 case MD_PROP_STR:
319 case MD_PROP_DATA:
320 val = data + ep->d.data.data_offset;
321 len = ep->d.data.data_len;
322 break;
David S. Miller5cbc3072007-05-25 15:49:59 -0700323
David S. Miller43fdf272007-07-12 13:47:50 -0700324 default:
David S. Miller5cbc3072007-05-25 15:49:59 -0700325 break;
326 }
David S. Miller43fdf272007-07-12 13:47:50 -0700327 if (!val)
328 continue;
David S. Miller5cbc3072007-05-25 15:49:59 -0700329
David S. Miller43fdf272007-07-12 13:47:50 -0700330 if (!strcmp(names + ep->name_offset, name)) {
331 if (lenp)
332 *lenp = len;
333 return val;
David S. Miller5cbc3072007-05-25 15:49:59 -0700334 }
335 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700336
David S. Miller43fdf272007-07-12 13:47:50 -0700337 return NULL;
338}
339EXPORT_SYMBOL(mdesc_get_property);
340
341u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
David S. Miller5cbc3072007-05-25 15:49:59 -0700342{
David S. Miller43fdf272007-07-12 13:47:50 -0700343 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
344 const char *names = name_block(&hp->mdesc);
345 u64 last_node = hp->mdesc.node_sz / 16;
David S. Miller5cbc3072007-05-25 15:49:59 -0700346
David S. Miller43fdf272007-07-12 13:47:50 -0700347 if (from == MDESC_NODE_NULL || from >= last_node)
348 return MDESC_NODE_NULL;
349
350 ep = base + from;
351
352 ep++;
353 for (; ep->tag != MD_NODE_END; ep++) {
354 if (ep->tag != MD_PROP_ARC)
355 continue;
356
357 if (strcmp(names + ep->name_offset, arc_type))
358 continue;
359
360 return ep - base;
David S. Miller5cbc3072007-05-25 15:49:59 -0700361 }
David S. Miller43fdf272007-07-12 13:47:50 -0700362
363 return MDESC_NODE_NULL;
David S. Miller5cbc3072007-05-25 15:49:59 -0700364}
David S. Miller43fdf272007-07-12 13:47:50 -0700365EXPORT_SYMBOL(mdesc_next_arc);
366
367u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
368{
369 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
370
371 ep = base + arc;
372
373 return ep->d.val;
374}
375EXPORT_SYMBOL(mdesc_arc_target);
376
377const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
378{
379 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
380 const char *names = name_block(&hp->mdesc);
381 u64 last_node = hp->mdesc.node_sz / 16;
382
383 if (node == MDESC_NODE_NULL || node >= last_node)
384 return NULL;
385
386 ep = base + node;
387 if (ep->tag != MD_NODE)
388 return NULL;
389
390 return names + ep->name_offset;
391}
392EXPORT_SYMBOL(mdesc_node_name);
David S. Miller5cbc3072007-05-25 15:49:59 -0700393
394static void __init report_platform_properties(void)
395{
David S. Miller43fdf272007-07-12 13:47:50 -0700396 struct mdesc_handle *hp = mdesc_grab();
397 u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
David S. Miller5cbc3072007-05-25 15:49:59 -0700398 const char *s;
399 const u64 *v;
400
David S. Miller43fdf272007-07-12 13:47:50 -0700401 if (pn == MDESC_NODE_NULL) {
David S. Miller5cbc3072007-05-25 15:49:59 -0700402 prom_printf("No platform node in machine-description.\n");
403 prom_halt();
404 }
405
David S. Miller43fdf272007-07-12 13:47:50 -0700406 s = mdesc_get_property(hp, pn, "banner-name", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700407 printk("PLATFORM: banner-name [%s]\n", s);
David S. Miller43fdf272007-07-12 13:47:50 -0700408 s = mdesc_get_property(hp, pn, "name", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700409 printk("PLATFORM: name [%s]\n", s);
410
David S. Miller43fdf272007-07-12 13:47:50 -0700411 v = mdesc_get_property(hp, pn, "hostid", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700412 if (v)
413 printk("PLATFORM: hostid [%08lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700414 v = mdesc_get_property(hp, pn, "serial#", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700415 if (v)
416 printk("PLATFORM: serial# [%08lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700417 v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700418 printk("PLATFORM: stick-frequency [%08lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700419 v = mdesc_get_property(hp, pn, "mac-address", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700420 if (v)
421 printk("PLATFORM: mac-address [%lx]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700422 v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700423 if (v)
424 printk("PLATFORM: watchdog-resolution [%lu ms]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700425 v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700426 if (v)
427 printk("PLATFORM: watchdog-max-timeout [%lu ms]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700428 v = mdesc_get_property(hp, pn, "max-cpus", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700429 if (v)
430 printk("PLATFORM: max-cpus [%lu]\n", *v);
David S. Miller43fdf272007-07-12 13:47:50 -0700431
David S. Miller4f0234f2007-07-13 16:03:42 -0700432#ifdef CONFIG_SMP
433 {
434 int max_cpu, i;
435
436 if (v) {
437 max_cpu = *v;
438 if (max_cpu > NR_CPUS)
439 max_cpu = NR_CPUS;
440 } else {
441 max_cpu = NR_CPUS;
442 }
443 for (i = 0; i < max_cpu; i++)
444 cpu_set(i, cpu_possible_map);
445 }
446#endif
447
David S. Miller43fdf272007-07-12 13:47:50 -0700448 mdesc_release(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700449}
450
451static int inline find_in_proplist(const char *list, const char *match, int len)
452{
453 while (len > 0) {
454 int l;
455
456 if (!strcmp(list, match))
457 return 1;
458 l = strlen(list) + 1;
459 list += l;
460 len -= l;
461 }
462 return 0;
463}
464
David S. Miller4f0234f2007-07-13 16:03:42 -0700465static void __devinit fill_in_one_cache(cpuinfo_sparc *c,
466 struct mdesc_handle *hp,
467 u64 mp)
David S. Miller5cbc3072007-05-25 15:49:59 -0700468{
David S. Miller43fdf272007-07-12 13:47:50 -0700469 const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
470 const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
471 const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700472 const char *type;
473 int type_len;
474
David S. Miller43fdf272007-07-12 13:47:50 -0700475 type = mdesc_get_property(hp, mp, "type", &type_len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700476
477 switch (*level) {
478 case 1:
479 if (find_in_proplist(type, "instn", type_len)) {
480 c->icache_size = *size;
481 c->icache_line_size = *line_size;
482 } else if (find_in_proplist(type, "data", type_len)) {
483 c->dcache_size = *size;
484 c->dcache_line_size = *line_size;
485 }
486 break;
487
488 case 2:
489 c->ecache_size = *size;
490 c->ecache_line_size = *line_size;
491 break;
492
493 default:
494 break;
495 }
496
497 if (*level == 1) {
David S. Miller43fdf272007-07-12 13:47:50 -0700498 u64 a;
David S. Miller5cbc3072007-05-25 15:49:59 -0700499
David S. Miller43fdf272007-07-12 13:47:50 -0700500 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
501 u64 target = mdesc_arc_target(hp, a);
502 const char *name = mdesc_node_name(hp, target);
David S. Miller5cbc3072007-05-25 15:49:59 -0700503
David S. Miller43fdf272007-07-12 13:47:50 -0700504 if (!strcmp(name, "cache"))
505 fill_in_one_cache(c, hp, target);
David S. Miller5cbc3072007-05-25 15:49:59 -0700506 }
507 }
508}
509
David S. Miller4f0234f2007-07-13 16:03:42 -0700510static void __devinit mark_core_ids(struct mdesc_handle *hp, u64 mp,
511 int core_id)
David S. Miller5cbc3072007-05-25 15:49:59 -0700512{
David S. Miller43fdf272007-07-12 13:47:50 -0700513 u64 a;
David S. Miller5cbc3072007-05-25 15:49:59 -0700514
David S. Miller43fdf272007-07-12 13:47:50 -0700515 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
516 u64 t = mdesc_arc_target(hp, a);
517 const char *name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700518 const u64 *id;
519
David S. Miller43fdf272007-07-12 13:47:50 -0700520 name = mdesc_node_name(hp, t);
521 if (!strcmp(name, "cpu")) {
522 id = mdesc_get_property(hp, t, "id", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700523 if (*id < NR_CPUS)
524 cpu_data(*id).core_id = core_id;
525 } else {
David S. Miller43fdf272007-07-12 13:47:50 -0700526 u64 j;
David S. Miller5cbc3072007-05-25 15:49:59 -0700527
David S. Miller43fdf272007-07-12 13:47:50 -0700528 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_BACK) {
529 u64 n = mdesc_arc_target(hp, j);
530 const char *n_name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700531
David S. Miller43fdf272007-07-12 13:47:50 -0700532 n_name = mdesc_node_name(hp, n);
533 if (strcmp(n_name, "cpu"))
David S. Miller5cbc3072007-05-25 15:49:59 -0700534 continue;
535
David S. Miller43fdf272007-07-12 13:47:50 -0700536 id = mdesc_get_property(hp, n, "id", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700537 if (*id < NR_CPUS)
538 cpu_data(*id).core_id = core_id;
539 }
540 }
541 }
542}
543
David S. Miller4f0234f2007-07-13 16:03:42 -0700544static void __devinit set_core_ids(struct mdesc_handle *hp)
David S. Miller5cbc3072007-05-25 15:49:59 -0700545{
David S. Miller5cbc3072007-05-25 15:49:59 -0700546 int idx;
David S. Miller43fdf272007-07-12 13:47:50 -0700547 u64 mp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700548
549 idx = 1;
David S. Miller43fdf272007-07-12 13:47:50 -0700550 mdesc_for_each_node_by_name(hp, mp, "cache") {
551 const u64 *level;
David S. Miller5cbc3072007-05-25 15:49:59 -0700552 const char *type;
553 int len;
554
David S. Miller43fdf272007-07-12 13:47:50 -0700555 level = mdesc_get_property(hp, mp, "level", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700556 if (*level != 1)
557 continue;
558
David S. Miller43fdf272007-07-12 13:47:50 -0700559 type = mdesc_get_property(hp, mp, "type", &len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700560 if (!find_in_proplist(type, "instn", len))
561 continue;
562
David S. Miller43fdf272007-07-12 13:47:50 -0700563 mark_core_ids(hp, mp, idx);
David S. Miller5cbc3072007-05-25 15:49:59 -0700564
565 idx++;
566 }
567}
568
David S. Miller4f0234f2007-07-13 16:03:42 -0700569static void __devinit mark_proc_ids(struct mdesc_handle *hp, u64 mp,
570 int proc_id)
David S. Millerf78eae22007-06-04 17:01:39 -0700571{
David S. Miller43fdf272007-07-12 13:47:50 -0700572 u64 a;
David S. Millerf78eae22007-06-04 17:01:39 -0700573
David S. Miller43fdf272007-07-12 13:47:50 -0700574 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
575 u64 t = mdesc_arc_target(hp, a);
576 const char *name;
David S. Millerf78eae22007-06-04 17:01:39 -0700577 const u64 *id;
578
David S. Miller43fdf272007-07-12 13:47:50 -0700579 name = mdesc_node_name(hp, t);
580 if (strcmp(name, "cpu"))
David S. Millerf78eae22007-06-04 17:01:39 -0700581 continue;
582
David S. Miller43fdf272007-07-12 13:47:50 -0700583 id = mdesc_get_property(hp, t, "id", NULL);
David S. Millerf78eae22007-06-04 17:01:39 -0700584 if (*id < NR_CPUS)
585 cpu_data(*id).proc_id = proc_id;
586 }
587}
588
David S. Miller4f0234f2007-07-13 16:03:42 -0700589static void __devinit __set_proc_ids(struct mdesc_handle *hp,
590 const char *exec_unit_name)
David S. Millerf78eae22007-06-04 17:01:39 -0700591{
David S. Millerf78eae22007-06-04 17:01:39 -0700592 int idx;
David S. Miller43fdf272007-07-12 13:47:50 -0700593 u64 mp;
David S. Millerf78eae22007-06-04 17:01:39 -0700594
595 idx = 0;
David S. Miller43fdf272007-07-12 13:47:50 -0700596 mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
David S. Millerf78eae22007-06-04 17:01:39 -0700597 const char *type;
598 int len;
599
David S. Miller43fdf272007-07-12 13:47:50 -0700600 type = mdesc_get_property(hp, mp, "type", &len);
David S. Millerf78eae22007-06-04 17:01:39 -0700601 if (!find_in_proplist(type, "int", len) &&
602 !find_in_proplist(type, "integer", len))
603 continue;
604
David S. Miller43fdf272007-07-12 13:47:50 -0700605 mark_proc_ids(hp, mp, idx);
David S. Millerf78eae22007-06-04 17:01:39 -0700606
607 idx++;
608 }
609}
610
David S. Miller4f0234f2007-07-13 16:03:42 -0700611static void __devinit set_proc_ids(struct mdesc_handle *hp)
David S. Millerf78eae22007-06-04 17:01:39 -0700612{
David S. Miller43fdf272007-07-12 13:47:50 -0700613 __set_proc_ids(hp, "exec_unit");
614 __set_proc_ids(hp, "exec-unit");
David S. Millerf78eae22007-06-04 17:01:39 -0700615}
616
David S. Miller4f0234f2007-07-13 16:03:42 -0700617static void __devinit get_one_mondo_bits(const u64 *p, unsigned int *mask,
618 unsigned char def)
David S. Miller5cbc3072007-05-25 15:49:59 -0700619{
620 u64 val;
621
622 if (!p)
623 goto use_default;
624 val = *p;
625
626 if (!val || val >= 64)
627 goto use_default;
628
629 *mask = ((1U << val) * 64U) - 1U;
630 return;
631
632use_default:
633 *mask = ((1U << def) * 64U) - 1U;
634}
635
David S. Miller4f0234f2007-07-13 16:03:42 -0700636static void __devinit get_mondo_data(struct mdesc_handle *hp, u64 mp,
637 struct trap_per_cpu *tb)
David S. Miller5cbc3072007-05-25 15:49:59 -0700638{
639 const u64 *val;
640
David S. Miller43fdf272007-07-12 13:47:50 -0700641 val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700642 get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7);
643
David S. Miller43fdf272007-07-12 13:47:50 -0700644 val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700645 get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7);
646
David S. Miller43fdf272007-07-12 13:47:50 -0700647 val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700648 get_one_mondo_bits(val, &tb->resum_qmask, 6);
649
David S. Miller43fdf272007-07-12 13:47:50 -0700650 val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700651 get_one_mondo_bits(val, &tb->nonresum_qmask, 2);
652}
653
David S. Miller4f0234f2007-07-13 16:03:42 -0700654void __devinit mdesc_fill_in_cpu_data(cpumask_t mask)
David S. Miller5cbc3072007-05-25 15:49:59 -0700655{
David S. Miller43fdf272007-07-12 13:47:50 -0700656 struct mdesc_handle *hp = mdesc_grab();
657 u64 mp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700658
659 ncpus_probed = 0;
David S. Miller43fdf272007-07-12 13:47:50 -0700660 mdesc_for_each_node_by_name(hp, mp, "cpu") {
661 const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
662 const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
David S. Miller5cbc3072007-05-25 15:49:59 -0700663 struct trap_per_cpu *tb;
664 cpuinfo_sparc *c;
David S. Miller5cbc3072007-05-25 15:49:59 -0700665 int cpuid;
David S. Miller43fdf272007-07-12 13:47:50 -0700666 u64 a;
David S. Miller5cbc3072007-05-25 15:49:59 -0700667
668 ncpus_probed++;
669
670 cpuid = *id;
671
672#ifdef CONFIG_SMP
673 if (cpuid >= NR_CPUS)
674 continue;
David S. Miller4f0234f2007-07-13 16:03:42 -0700675 if (!cpu_isset(cpuid, mask))
676 continue;
David S. Miller5cbc3072007-05-25 15:49:59 -0700677#else
678 /* On uniprocessor we only want the values for the
679 * real physical cpu the kernel booted onto, however
680 * cpu_data() only has one entry at index 0.
681 */
682 if (cpuid != real_hard_smp_processor_id())
683 continue;
684 cpuid = 0;
685#endif
686
687 c = &cpu_data(cpuid);
688 c->clock_tick = *cfreq;
689
690 tb = &trap_block[cpuid];
David S. Miller43fdf272007-07-12 13:47:50 -0700691 get_mondo_data(hp, mp, tb);
David S. Miller5cbc3072007-05-25 15:49:59 -0700692
David S. Miller43fdf272007-07-12 13:47:50 -0700693 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
694 u64 j, t = mdesc_arc_target(hp, a);
695 const char *t_name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700696
David S. Miller43fdf272007-07-12 13:47:50 -0700697 t_name = mdesc_node_name(hp, t);
698 if (!strcmp(t_name, "cache")) {
699 fill_in_one_cache(c, hp, t);
David S. Miller5cbc3072007-05-25 15:49:59 -0700700 continue;
701 }
702
David S. Miller43fdf272007-07-12 13:47:50 -0700703 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
704 u64 n = mdesc_arc_target(hp, j);
705 const char *n_name;
David S. Miller5cbc3072007-05-25 15:49:59 -0700706
David S. Miller43fdf272007-07-12 13:47:50 -0700707 n_name = mdesc_node_name(hp, n);
708 if (!strcmp(n_name, "cache"))
709 fill_in_one_cache(c, hp, n);
David S. Miller5cbc3072007-05-25 15:49:59 -0700710 }
711 }
712
713#ifdef CONFIG_SMP
714 cpu_set(cpuid, cpu_present_map);
David S. Miller5cbc3072007-05-25 15:49:59 -0700715#endif
716
717 c->core_id = 0;
David S. Millerf78eae22007-06-04 17:01:39 -0700718 c->proc_id = -1;
David S. Miller5cbc3072007-05-25 15:49:59 -0700719 }
720
David S. Millera2f9f6b2007-06-04 21:48:33 -0700721#ifdef CONFIG_SMP
722 sparc64_multi_core = 1;
723#endif
724
David S. Miller43fdf272007-07-12 13:47:50 -0700725 set_core_ids(hp);
726 set_proc_ids(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700727
728 smp_fill_in_sib_core_maps();
David S. Miller43fdf272007-07-12 13:47:50 -0700729
730 mdesc_release(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700731}
732
733void __init sun4v_mdesc_init(void)
734{
David S. Miller43fdf272007-07-12 13:47:50 -0700735 struct mdesc_handle *hp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700736 unsigned long len, real_len, status;
David S. Miller4f0234f2007-07-13 16:03:42 -0700737 cpumask_t mask;
David S. Miller5cbc3072007-05-25 15:49:59 -0700738
739 (void) sun4v_mach_desc(0UL, 0UL, &len);
740
741 printk("MDESC: Size is %lu bytes.\n", len);
742
David S. Miller43fdf272007-07-12 13:47:50 -0700743 hp = mdesc_alloc(len, &bootmem_mdesc_memops);
744 if (hp == NULL) {
745 prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
746 prom_halt();
747 }
David S. Miller5cbc3072007-05-25 15:49:59 -0700748
David S. Miller43fdf272007-07-12 13:47:50 -0700749 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
David S. Miller5cbc3072007-05-25 15:49:59 -0700750 if (status != HV_EOK || real_len > len) {
751 prom_printf("sun4v_mach_desc fails, err(%lu), "
752 "len(%lu), real_len(%lu)\n",
753 status, len, real_len);
David S. Miller43fdf272007-07-12 13:47:50 -0700754 mdesc_free(hp);
David S. Miller5cbc3072007-05-25 15:49:59 -0700755 prom_halt();
756 }
757
David S. Miller43fdf272007-07-12 13:47:50 -0700758 cur_mdesc = hp;
David S. Miller5cbc3072007-05-25 15:49:59 -0700759
760 report_platform_properties();
David S. Miller4f0234f2007-07-13 16:03:42 -0700761
762 cpus_setall(mask);
763 mdesc_fill_in_cpu_data(mask);
David S. Miller5cbc3072007-05-25 15:49:59 -0700764}