[PATCH] ocfs2: zero_user_page conversion
[wrt350n-kernel.git] / arch / sparc64 / kernel / mdesc.c
blobf0e16045fb1693f0f8ba9e8e507413833aa05b9f
1 /* mdesc.c: Sun4V machine description handling.
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>
10 #include <asm/hypervisor.h>
11 #include <asm/mdesc.h>
12 #include <asm/prom.h>
13 #include <asm/oplib.h>
14 #include <asm/smp.h>
16 /* Unlike the OBP device tree, the machine description is a full-on
17 * DAG. An arbitrary number of ARCs are possible from one
18 * node to other nodes and thus we can't use the OBP device_node
19 * data structure to represent these nodes inside of the kernel.
21 * Actually, it isn't even a DAG, because there are back pointers
22 * which create cycles in the graph.
24 * mdesc_hdr and mdesc_elem describe the layout of the data structure
25 * we get from the Hypervisor.
27 struct mdesc_hdr {
28 u32 version; /* Transport version */
29 u32 node_sz; /* node block size */
30 u32 name_sz; /* name block size */
31 u32 data_sz; /* data block size */
34 struct mdesc_elem {
35 u8 tag;
36 #define MD_LIST_END 0x00
37 #define MD_NODE 0x4e
38 #define MD_NODE_END 0x45
39 #define MD_NOOP 0x20
40 #define MD_PROP_ARC 0x61
41 #define MD_PROP_VAL 0x76
42 #define MD_PROP_STR 0x73
43 #define MD_PROP_DATA 0x64
44 u8 name_len;
45 u16 resv;
46 u32 name_offset;
47 union {
48 struct {
49 u32 data_len;
50 u32 data_offset;
51 } data;
52 u64 val;
53 } d;
56 static struct mdesc_hdr *main_mdesc;
57 static struct mdesc_node *allnodes;
59 static struct mdesc_node *allnodes_tail;
60 static unsigned int unique_id;
62 static struct mdesc_node **mdesc_hash;
63 static unsigned int mdesc_hash_size;
65 static inline unsigned int node_hashfn(u64 node)
67 return ((unsigned int) (node ^ (node >> 8) ^ (node >> 16)))
68 & (mdesc_hash_size - 1);
71 static inline void hash_node(struct mdesc_node *mp)
73 struct mdesc_node **head = &mdesc_hash[node_hashfn(mp->node)];
75 mp->hash_next = *head;
76 *head = mp;
78 if (allnodes_tail) {
79 allnodes_tail->allnodes_next = mp;
80 allnodes_tail = mp;
81 } else {
82 allnodes = allnodes_tail = mp;
86 static struct mdesc_node *find_node(u64 node)
88 struct mdesc_node *mp = mdesc_hash[node_hashfn(node)];
90 while (mp) {
91 if (mp->node == node)
92 return mp;
94 mp = mp->hash_next;
96 return NULL;
99 struct property *md_find_property(const struct mdesc_node *mp,
100 const char *name,
101 int *lenp)
103 struct property *pp;
105 for (pp = mp->properties; pp != 0; pp = pp->next) {
106 if (strcasecmp(pp->name, name) == 0) {
107 if (lenp)
108 *lenp = pp->length;
109 break;
112 return pp;
114 EXPORT_SYMBOL(md_find_property);
117 * Find a property with a given name for a given node
118 * and return the value.
120 const void *md_get_property(const struct mdesc_node *mp, const char *name,
121 int *lenp)
123 struct property *pp = md_find_property(mp, name, lenp);
124 return pp ? pp->value : NULL;
126 EXPORT_SYMBOL(md_get_property);
128 struct mdesc_node *md_find_node_by_name(struct mdesc_node *from,
129 const char *name)
131 struct mdesc_node *mp;
133 mp = from ? from->allnodes_next : allnodes;
134 for (; mp != NULL; mp = mp->allnodes_next) {
135 if (strcmp(mp->name, name) == 0)
136 break;
138 return mp;
140 EXPORT_SYMBOL(md_find_node_by_name);
142 static unsigned int mdesc_early_allocated;
144 static void * __init mdesc_early_alloc(unsigned long size)
146 void *ret;
148 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
149 if (ret == NULL) {
150 prom_printf("MDESC: alloc of %lu bytes failed.\n", size);
151 prom_halt();
154 memset(ret, 0, size);
156 mdesc_early_allocated += size;
158 return ret;
161 static unsigned int __init count_arcs(struct mdesc_elem *ep)
163 unsigned int ret = 0;
165 ep++;
166 while (ep->tag != MD_NODE_END) {
167 if (ep->tag == MD_PROP_ARC)
168 ret++;
169 ep++;
171 return ret;
174 static void __init mdesc_node_alloc(u64 node, struct mdesc_elem *ep, const char *names)
176 unsigned int num_arcs = count_arcs(ep);
177 struct mdesc_node *mp;
179 mp = mdesc_early_alloc(sizeof(*mp) +
180 (num_arcs * sizeof(struct mdesc_arc)));
181 mp->name = names + ep->name_offset;
182 mp->node = node;
183 mp->unique_id = unique_id++;
184 mp->num_arcs = num_arcs;
186 hash_node(mp);
189 static inline struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
191 return (struct mdesc_elem *) (mdesc + 1);
194 static inline void *name_block(struct mdesc_hdr *mdesc)
196 return ((void *) node_block(mdesc)) + mdesc->node_sz;
199 static inline void *data_block(struct mdesc_hdr *mdesc)
201 return ((void *) name_block(mdesc)) + mdesc->name_sz;
204 /* In order to avoid recursion (the graph can be very deep) we use a
205 * two pass algorithm. First we allocate all the nodes and hash them.
206 * Then we iterate over each node, filling in the arcs and properties.
208 static void __init build_all_nodes(struct mdesc_hdr *mdesc)
210 struct mdesc_elem *start, *ep;
211 struct mdesc_node *mp;
212 const char *names;
213 void *data;
214 u64 last_node;
216 start = ep = node_block(mdesc);
217 last_node = mdesc->node_sz / 16;
219 names = name_block(mdesc);
221 while (1) {
222 u64 node = ep - start;
224 if (ep->tag == MD_LIST_END)
225 break;
227 if (ep->tag != MD_NODE) {
228 prom_printf("MDESC: Inconsistent element list.\n");
229 prom_halt();
232 mdesc_node_alloc(node, ep, names);
234 if (ep->d.val >= last_node) {
235 printk("MDESC: Warning, early break out of node scan.\n");
236 printk("MDESC: Next node [%lu] last_node [%lu].\n",
237 node, last_node);
238 break;
241 ep = start + ep->d.val;
244 data = data_block(mdesc);
245 for (mp = allnodes; mp; mp = mp->allnodes_next) {
246 struct mdesc_elem *ep = start + mp->node;
247 struct property **link = &mp->properties;
248 unsigned int this_arc = 0;
250 ep++;
251 while (ep->tag != MD_NODE_END) {
252 switch (ep->tag) {
253 case MD_PROP_ARC: {
254 struct mdesc_node *target;
256 if (this_arc >= mp->num_arcs) {
257 prom_printf("MDESC: ARC overrun [%u:%u]\n",
258 this_arc, mp->num_arcs);
259 prom_halt();
261 target = find_node(ep->d.val);
262 if (!target) {
263 printk("MDESC: Warning, arc points to "
264 "missing node, ignoring.\n");
265 break;
267 mp->arcs[this_arc].name =
268 (names + ep->name_offset);
269 mp->arcs[this_arc].arc = target;
270 this_arc++;
271 break;
274 case MD_PROP_VAL:
275 case MD_PROP_STR:
276 case MD_PROP_DATA: {
277 struct property *p = mdesc_early_alloc(sizeof(*p));
279 p->unique_id = unique_id++;
280 p->name = (char *) names + ep->name_offset;
281 if (ep->tag == MD_PROP_VAL) {
282 p->value = &ep->d.val;
283 p->length = 8;
284 } else {
285 p->value = data + ep->d.data.data_offset;
286 p->length = ep->d.data.data_len;
288 *link = p;
289 link = &p->next;
290 break;
293 case MD_NOOP:
294 break;
296 default:
297 printk("MDESC: Warning, ignoring unknown tag type %02x\n",
298 ep->tag);
300 ep++;
305 static unsigned int __init count_nodes(struct mdesc_hdr *mdesc)
307 struct mdesc_elem *ep = node_block(mdesc);
308 struct mdesc_elem *end;
309 unsigned int cnt = 0;
311 end = ((void *)ep) + mdesc->node_sz;
312 while (ep < end) {
313 if (ep->tag == MD_NODE)
314 cnt++;
315 ep++;
317 return cnt;
320 static void __init report_platform_properties(void)
322 struct mdesc_node *pn = md_find_node_by_name(NULL, "platform");
323 const char *s;
324 const u64 *v;
326 if (!pn) {
327 prom_printf("No platform node in machine-description.\n");
328 prom_halt();
331 s = md_get_property(pn, "banner-name", NULL);
332 printk("PLATFORM: banner-name [%s]\n", s);
333 s = md_get_property(pn, "name", NULL);
334 printk("PLATFORM: name [%s]\n", s);
336 v = md_get_property(pn, "hostid", NULL);
337 if (v)
338 printk("PLATFORM: hostid [%08lx]\n", *v);
339 v = md_get_property(pn, "serial#", NULL);
340 if (v)
341 printk("PLATFORM: serial# [%08lx]\n", *v);
342 v = md_get_property(pn, "stick-frequency", NULL);
343 printk("PLATFORM: stick-frequency [%08lx]\n", *v);
344 v = md_get_property(pn, "mac-address", NULL);
345 if (v)
346 printk("PLATFORM: mac-address [%lx]\n", *v);
347 v = md_get_property(pn, "watchdog-resolution", NULL);
348 if (v)
349 printk("PLATFORM: watchdog-resolution [%lu ms]\n", *v);
350 v = md_get_property(pn, "watchdog-max-timeout", NULL);
351 if (v)
352 printk("PLATFORM: watchdog-max-timeout [%lu ms]\n", *v);
353 v = md_get_property(pn, "max-cpus", NULL);
354 if (v)
355 printk("PLATFORM: max-cpus [%lu]\n", *v);
358 static int inline find_in_proplist(const char *list, const char *match, int len)
360 while (len > 0) {
361 int l;
363 if (!strcmp(list, match))
364 return 1;
365 l = strlen(list) + 1;
366 list += l;
367 len -= l;
369 return 0;
372 static void __init fill_in_one_cache(cpuinfo_sparc *c, struct mdesc_node *mp)
374 const u64 *level = md_get_property(mp, "level", NULL);
375 const u64 *size = md_get_property(mp, "size", NULL);
376 const u64 *line_size = md_get_property(mp, "line-size", NULL);
377 const char *type;
378 int type_len;
380 type = md_get_property(mp, "type", &type_len);
382 switch (*level) {
383 case 1:
384 if (find_in_proplist(type, "instn", type_len)) {
385 c->icache_size = *size;
386 c->icache_line_size = *line_size;
387 } else if (find_in_proplist(type, "data", type_len)) {
388 c->dcache_size = *size;
389 c->dcache_line_size = *line_size;
391 break;
393 case 2:
394 c->ecache_size = *size;
395 c->ecache_line_size = *line_size;
396 break;
398 default:
399 break;
402 if (*level == 1) {
403 unsigned int i;
405 for (i = 0; i < mp->num_arcs; i++) {
406 struct mdesc_node *t = mp->arcs[i].arc;
408 if (strcmp(mp->arcs[i].name, "fwd"))
409 continue;
411 if (!strcmp(t->name, "cache"))
412 fill_in_one_cache(c, t);
417 static void __init mark_core_ids(struct mdesc_node *mp, int core_id)
419 unsigned int i;
421 for (i = 0; i < mp->num_arcs; i++) {
422 struct mdesc_node *t = mp->arcs[i].arc;
423 const u64 *id;
425 if (strcmp(mp->arcs[i].name, "back"))
426 continue;
428 if (!strcmp(t->name, "cpu")) {
429 id = md_get_property(t, "id", NULL);
430 if (*id < NR_CPUS)
431 cpu_data(*id).core_id = core_id;
432 } else {
433 unsigned int j;
435 for (j = 0; j < t->num_arcs; j++) {
436 struct mdesc_node *n = t->arcs[j].arc;
438 if (strcmp(t->arcs[j].name, "back"))
439 continue;
441 if (strcmp(n->name, "cpu"))
442 continue;
444 id = md_get_property(n, "id", NULL);
445 if (*id < NR_CPUS)
446 cpu_data(*id).core_id = core_id;
452 static void __init set_core_ids(void)
454 struct mdesc_node *mp;
455 int idx;
457 idx = 1;
458 md_for_each_node_by_name(mp, "cache") {
459 const u64 *level = md_get_property(mp, "level", NULL);
460 const char *type;
461 int len;
463 if (*level != 1)
464 continue;
466 type = md_get_property(mp, "type", &len);
467 if (!find_in_proplist(type, "instn", len))
468 continue;
470 mark_core_ids(mp, idx);
472 idx++;
476 static void __init mark_proc_ids(struct mdesc_node *mp, int proc_id)
478 int i;
480 for (i = 0; i < mp->num_arcs; i++) {
481 struct mdesc_node *t = mp->arcs[i].arc;
482 const u64 *id;
484 if (strcmp(mp->arcs[i].name, "back"))
485 continue;
487 if (strcmp(t->name, "cpu"))
488 continue;
490 id = md_get_property(t, "id", NULL);
491 if (*id < NR_CPUS)
492 cpu_data(*id).proc_id = proc_id;
496 static void __init __set_proc_ids(const char *exec_unit_name)
498 struct mdesc_node *mp;
499 int idx;
501 idx = 0;
502 md_for_each_node_by_name(mp, exec_unit_name) {
503 const char *type;
504 int len;
506 type = md_get_property(mp, "type", &len);
507 if (!find_in_proplist(type, "int", len) &&
508 !find_in_proplist(type, "integer", len))
509 continue;
511 mark_proc_ids(mp, idx);
513 idx++;
517 static void __init set_proc_ids(void)
519 __set_proc_ids("exec_unit");
520 __set_proc_ids("exec-unit");
523 static void __init get_one_mondo_bits(const u64 *p, unsigned int *mask, unsigned char def)
525 u64 val;
527 if (!p)
528 goto use_default;
529 val = *p;
531 if (!val || val >= 64)
532 goto use_default;
534 *mask = ((1U << val) * 64U) - 1U;
535 return;
537 use_default:
538 *mask = ((1U << def) * 64U) - 1U;
541 static void __init get_mondo_data(struct mdesc_node *mp, struct trap_per_cpu *tb)
543 const u64 *val;
545 val = md_get_property(mp, "q-cpu-mondo-#bits", NULL);
546 get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7);
548 val = md_get_property(mp, "q-dev-mondo-#bits", NULL);
549 get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7);
551 val = md_get_property(mp, "q-resumable-#bits", NULL);
552 get_one_mondo_bits(val, &tb->resum_qmask, 6);
554 val = md_get_property(mp, "q-nonresumable-#bits", NULL);
555 get_one_mondo_bits(val, &tb->nonresum_qmask, 2);
558 static void __init mdesc_fill_in_cpu_data(void)
560 struct mdesc_node *mp;
562 ncpus_probed = 0;
563 md_for_each_node_by_name(mp, "cpu") {
564 const u64 *id = md_get_property(mp, "id", NULL);
565 const u64 *cfreq = md_get_property(mp, "clock-frequency", NULL);
566 struct trap_per_cpu *tb;
567 cpuinfo_sparc *c;
568 unsigned int i;
569 int cpuid;
571 ncpus_probed++;
573 cpuid = *id;
575 #ifdef CONFIG_SMP
576 if (cpuid >= NR_CPUS)
577 continue;
578 #else
579 /* On uniprocessor we only want the values for the
580 * real physical cpu the kernel booted onto, however
581 * cpu_data() only has one entry at index 0.
583 if (cpuid != real_hard_smp_processor_id())
584 continue;
585 cpuid = 0;
586 #endif
588 c = &cpu_data(cpuid);
589 c->clock_tick = *cfreq;
591 tb = &trap_block[cpuid];
592 get_mondo_data(mp, tb);
594 for (i = 0; i < mp->num_arcs; i++) {
595 struct mdesc_node *t = mp->arcs[i].arc;
596 unsigned int j;
598 if (strcmp(mp->arcs[i].name, "fwd"))
599 continue;
601 if (!strcmp(t->name, "cache")) {
602 fill_in_one_cache(c, t);
603 continue;
606 for (j = 0; j < t->num_arcs; j++) {
607 struct mdesc_node *n;
609 n = t->arcs[j].arc;
610 if (strcmp(t->arcs[j].name, "fwd"))
611 continue;
613 if (!strcmp(n->name, "cache"))
614 fill_in_one_cache(c, n);
618 #ifdef CONFIG_SMP
619 cpu_set(cpuid, cpu_present_map);
620 cpu_set(cpuid, phys_cpu_present_map);
621 #endif
623 c->core_id = 0;
624 c->proc_id = -1;
627 #ifdef CONFIG_SMP
628 sparc64_multi_core = 1;
629 #endif
631 set_core_ids();
632 set_proc_ids();
634 smp_fill_in_sib_core_maps();
637 void __init sun4v_mdesc_init(void)
639 unsigned long len, real_len, status;
641 (void) sun4v_mach_desc(0UL, 0UL, &len);
643 printk("MDESC: Size is %lu bytes.\n", len);
645 main_mdesc = mdesc_early_alloc(len);
647 status = sun4v_mach_desc(__pa(main_mdesc), len, &real_len);
648 if (status != HV_EOK || real_len > len) {
649 prom_printf("sun4v_mach_desc fails, err(%lu), "
650 "len(%lu), real_len(%lu)\n",
651 status, len, real_len);
652 prom_halt();
655 len = count_nodes(main_mdesc);
656 printk("MDESC: %lu nodes.\n", len);
658 len = roundup_pow_of_two(len);
660 mdesc_hash = mdesc_early_alloc(len * sizeof(struct mdesc_node *));
661 mdesc_hash_size = len;
663 printk("MDESC: Hash size %lu entries.\n", len);
665 build_all_nodes(main_mdesc);
667 printk("MDESC: Built graph with %u bytes of memory.\n",
668 mdesc_early_allocated);
670 report_platform_properties();
671 mdesc_fill_in_cpu_data();