[SPARC64]: SMP build fix.
[deliverable/linux.git] / arch / sparc64 / kernel / mdesc.c
1 /* 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>
9 #include <linux/list.h>
10 #include <linux/slab.h>
11 #include <linux/mm.h>
12
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 */
30 struct 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 */
35 } __attribute__((aligned(16)));
36
37 struct 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
59 struct mdesc_mem_ops {
60 struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
61 void (*free)(struct mdesc_handle *handle);
62 };
63
64 struct 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 };
72
73 static void mdesc_handle_init(struct mdesc_handle *hp,
74 unsigned int handle_size,
75 void *base)
76 {
77 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;
84 }
85
86 static struct mdesc_handle *mdesc_bootmem_alloc(unsigned int mdesc_size)
87 {
88 struct mdesc_handle *hp;
89 unsigned int handle_size, alloc_size;
90
91 handle_size = (sizeof(struct mdesc_handle) -
92 sizeof(struct mdesc_hdr) +
93 mdesc_size);
94 alloc_size = PAGE_ALIGN(handle_size);
95
96 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
103 static 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;
123 }
124 }
125
126 static struct mdesc_mem_ops bootmem_mdesc_memops = {
127 .alloc = mdesc_bootmem_alloc,
128 .free = mdesc_bootmem_free,
129 };
130
131 static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
132 {
133 unsigned int handle_size;
134 void *base;
135
136 handle_size = (sizeof(struct mdesc_handle) -
137 sizeof(struct mdesc_hdr) +
138 mdesc_size);
139
140 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;
151 }
152
153 return NULL;
154 }
155
156 static void mdesc_kfree(struct mdesc_handle *hp)
157 {
158 BUG_ON(atomic_read(&hp->refcnt) != 0);
159 BUG_ON(!list_empty(&hp->list));
160
161 kfree(hp->self_base);
162 }
163
164 static struct mdesc_mem_ops kmalloc_mdesc_memops = {
165 .alloc = mdesc_kmalloc,
166 .free = mdesc_kfree,
167 };
168
169 static 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
180 static void mdesc_free(struct mdesc_handle *hp)
181 {
182 hp->mops->free(hp);
183 }
184
185 static struct mdesc_handle *cur_mdesc;
186 static LIST_HEAD(mdesc_zombie_list);
187 static DEFINE_SPINLOCK(mdesc_lock);
188
189 struct 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 }
202 EXPORT_SYMBOL(mdesc_grab);
203
204 void 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);
212 }
213 spin_unlock_irqrestore(&mdesc_lock, flags);
214 }
215 EXPORT_SYMBOL(mdesc_release);
216
217 static void do_mdesc_update(struct work_struct *work)
218 {
219 unsigned long len, real_len, status;
220 struct mdesc_handle *hp, *orig_hp;
221 unsigned long flags;
222
223 (void) sun4v_mach_desc(0UL, 0UL, &len);
224
225 hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
226 if (!hp) {
227 printk(KERN_ERR "MD: mdesc alloc fails\n");
228 return;
229 }
230
231 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;
238 }
239
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);
249 }
250
251 static DECLARE_WORK(mdesc_update_work, do_mdesc_update);
252
253 void mdesc_update(void)
254 {
255 schedule_work(&mdesc_update_work);
256 }
257
258 static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
259 {
260 return (struct mdesc_elem *) (mdesc + 1);
261 }
262
263 static void *name_block(struct mdesc_hdr *mdesc)
264 {
265 return ((void *) node_block(mdesc)) + mdesc->node_sz;
266 }
267
268 static void *data_block(struct mdesc_hdr *mdesc)
269 {
270 return ((void *) name_block(mdesc)) + mdesc->name_sz;
271 }
272
273 u64 mdesc_node_by_name(struct mdesc_handle *hp,
274 u64 from_node, const char *name)
275 {
276 struct mdesc_elem *ep = node_block(&hp->mdesc);
277 const char *names = name_block(&hp->mdesc);
278 u64 last_node = hp->mdesc.node_sz / 16;
279 u64 ret;
280
281 if (from_node == MDESC_NODE_NULL)
282 from_node = 0;
283
284 if (from_node >= last_node)
285 return MDESC_NODE_NULL;
286
287 ret = ep[from_node].d.val;
288 while (ret < last_node) {
289 if (ep[ret].tag != MD_NODE)
290 return MDESC_NODE_NULL;
291 if (!strcmp(names + ep[ret].name_offset, name))
292 break;
293 ret = ep[ret].d.val;
294 }
295 if (ret >= last_node)
296 ret = MDESC_NODE_NULL;
297 return ret;
298 }
299 EXPORT_SYMBOL(mdesc_node_by_name);
300
301 const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
302 const char *name, int *lenp)
303 {
304 const char *names = name_block(&hp->mdesc);
305 u64 last_node = hp->mdesc.node_sz / 16;
306 void *data = data_block(&hp->mdesc);
307 struct mdesc_elem *ep;
308
309 if (node == MDESC_NODE_NULL || node >= last_node)
310 return NULL;
311
312 ep = node_block(&hp->mdesc) + node;
313 ep++;
314 for (; ep->tag != MD_NODE_END; ep++) {
315 void *val = NULL;
316 int len = 0;
317
318 switch (ep->tag) {
319 case MD_PROP_VAL:
320 val = &ep->d.val;
321 len = 8;
322 break;
323
324 case MD_PROP_STR:
325 case MD_PROP_DATA:
326 val = data + ep->d.data.data_offset;
327 len = ep->d.data.data_len;
328 break;
329
330 default:
331 break;
332 }
333 if (!val)
334 continue;
335
336 if (!strcmp(names + ep->name_offset, name)) {
337 if (lenp)
338 *lenp = len;
339 return val;
340 }
341 }
342
343 return NULL;
344 }
345 EXPORT_SYMBOL(mdesc_get_property);
346
347 u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
348 {
349 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
350 const char *names = name_block(&hp->mdesc);
351 u64 last_node = hp->mdesc.node_sz / 16;
352
353 if (from == MDESC_NODE_NULL || from >= last_node)
354 return MDESC_NODE_NULL;
355
356 ep = base + from;
357
358 ep++;
359 for (; ep->tag != MD_NODE_END; ep++) {
360 if (ep->tag != MD_PROP_ARC)
361 continue;
362
363 if (strcmp(names + ep->name_offset, arc_type))
364 continue;
365
366 return ep - base;
367 }
368
369 return MDESC_NODE_NULL;
370 }
371 EXPORT_SYMBOL(mdesc_next_arc);
372
373 u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
374 {
375 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
376
377 ep = base + arc;
378
379 return ep->d.val;
380 }
381 EXPORT_SYMBOL(mdesc_arc_target);
382
383 const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
384 {
385 struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
386 const char *names = name_block(&hp->mdesc);
387 u64 last_node = hp->mdesc.node_sz / 16;
388
389 if (node == MDESC_NODE_NULL || node >= last_node)
390 return NULL;
391
392 ep = base + node;
393 if (ep->tag != MD_NODE)
394 return NULL;
395
396 return names + ep->name_offset;
397 }
398 EXPORT_SYMBOL(mdesc_node_name);
399
400 static void __init report_platform_properties(void)
401 {
402 struct mdesc_handle *hp = mdesc_grab();
403 u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
404 const char *s;
405 const u64 *v;
406
407 if (pn == MDESC_NODE_NULL) {
408 prom_printf("No platform node in machine-description.\n");
409 prom_halt();
410 }
411
412 s = mdesc_get_property(hp, pn, "banner-name", NULL);
413 printk("PLATFORM: banner-name [%s]\n", s);
414 s = mdesc_get_property(hp, pn, "name", NULL);
415 printk("PLATFORM: name [%s]\n", s);
416
417 v = mdesc_get_property(hp, pn, "hostid", NULL);
418 if (v)
419 printk("PLATFORM: hostid [%08lx]\n", *v);
420 v = mdesc_get_property(hp, pn, "serial#", NULL);
421 if (v)
422 printk("PLATFORM: serial# [%08lx]\n", *v);
423 v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
424 printk("PLATFORM: stick-frequency [%08lx]\n", *v);
425 v = mdesc_get_property(hp, pn, "mac-address", NULL);
426 if (v)
427 printk("PLATFORM: mac-address [%lx]\n", *v);
428 v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
429 if (v)
430 printk("PLATFORM: watchdog-resolution [%lu ms]\n", *v);
431 v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
432 if (v)
433 printk("PLATFORM: watchdog-max-timeout [%lu ms]\n", *v);
434 v = mdesc_get_property(hp, pn, "max-cpus", NULL);
435 if (v)
436 printk("PLATFORM: max-cpus [%lu]\n", *v);
437
438 #ifdef CONFIG_SMP
439 {
440 int max_cpu, i;
441
442 if (v) {
443 max_cpu = *v;
444 if (max_cpu > NR_CPUS)
445 max_cpu = NR_CPUS;
446 } else {
447 max_cpu = NR_CPUS;
448 }
449 for (i = 0; i < max_cpu; i++)
450 cpu_set(i, cpu_possible_map);
451 }
452 #endif
453
454 mdesc_release(hp);
455 }
456
457 static int inline find_in_proplist(const char *list, const char *match, int len)
458 {
459 while (len > 0) {
460 int l;
461
462 if (!strcmp(list, match))
463 return 1;
464 l = strlen(list) + 1;
465 list += l;
466 len -= l;
467 }
468 return 0;
469 }
470
471 static void __devinit fill_in_one_cache(cpuinfo_sparc *c,
472 struct mdesc_handle *hp,
473 u64 mp)
474 {
475 const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
476 const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
477 const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
478 const char *type;
479 int type_len;
480
481 type = mdesc_get_property(hp, mp, "type", &type_len);
482
483 switch (*level) {
484 case 1:
485 if (find_in_proplist(type, "instn", type_len)) {
486 c->icache_size = *size;
487 c->icache_line_size = *line_size;
488 } else if (find_in_proplist(type, "data", type_len)) {
489 c->dcache_size = *size;
490 c->dcache_line_size = *line_size;
491 }
492 break;
493
494 case 2:
495 c->ecache_size = *size;
496 c->ecache_line_size = *line_size;
497 break;
498
499 default:
500 break;
501 }
502
503 if (*level == 1) {
504 u64 a;
505
506 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
507 u64 target = mdesc_arc_target(hp, a);
508 const char *name = mdesc_node_name(hp, target);
509
510 if (!strcmp(name, "cache"))
511 fill_in_one_cache(c, hp, target);
512 }
513 }
514 }
515
516 static void __devinit mark_core_ids(struct mdesc_handle *hp, u64 mp,
517 int core_id)
518 {
519 u64 a;
520
521 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
522 u64 t = mdesc_arc_target(hp, a);
523 const char *name;
524 const u64 *id;
525
526 name = mdesc_node_name(hp, t);
527 if (!strcmp(name, "cpu")) {
528 id = mdesc_get_property(hp, t, "id", NULL);
529 if (*id < NR_CPUS)
530 cpu_data(*id).core_id = core_id;
531 } else {
532 u64 j;
533
534 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_BACK) {
535 u64 n = mdesc_arc_target(hp, j);
536 const char *n_name;
537
538 n_name = mdesc_node_name(hp, n);
539 if (strcmp(n_name, "cpu"))
540 continue;
541
542 id = mdesc_get_property(hp, n, "id", NULL);
543 if (*id < NR_CPUS)
544 cpu_data(*id).core_id = core_id;
545 }
546 }
547 }
548 }
549
550 static void __devinit set_core_ids(struct mdesc_handle *hp)
551 {
552 int idx;
553 u64 mp;
554
555 idx = 1;
556 mdesc_for_each_node_by_name(hp, mp, "cache") {
557 const u64 *level;
558 const char *type;
559 int len;
560
561 level = mdesc_get_property(hp, mp, "level", NULL);
562 if (*level != 1)
563 continue;
564
565 type = mdesc_get_property(hp, mp, "type", &len);
566 if (!find_in_proplist(type, "instn", len))
567 continue;
568
569 mark_core_ids(hp, mp, idx);
570
571 idx++;
572 }
573 }
574
575 static void __devinit mark_proc_ids(struct mdesc_handle *hp, u64 mp,
576 int proc_id)
577 {
578 u64 a;
579
580 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
581 u64 t = mdesc_arc_target(hp, a);
582 const char *name;
583 const u64 *id;
584
585 name = mdesc_node_name(hp, t);
586 if (strcmp(name, "cpu"))
587 continue;
588
589 id = mdesc_get_property(hp, t, "id", NULL);
590 if (*id < NR_CPUS)
591 cpu_data(*id).proc_id = proc_id;
592 }
593 }
594
595 static void __devinit __set_proc_ids(struct mdesc_handle *hp,
596 const char *exec_unit_name)
597 {
598 int idx;
599 u64 mp;
600
601 idx = 0;
602 mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
603 const char *type;
604 int len;
605
606 type = mdesc_get_property(hp, mp, "type", &len);
607 if (!find_in_proplist(type, "int", len) &&
608 !find_in_proplist(type, "integer", len))
609 continue;
610
611 mark_proc_ids(hp, mp, idx);
612
613 idx++;
614 }
615 }
616
617 static void __devinit set_proc_ids(struct mdesc_handle *hp)
618 {
619 __set_proc_ids(hp, "exec_unit");
620 __set_proc_ids(hp, "exec-unit");
621 }
622
623 static void __devinit get_one_mondo_bits(const u64 *p, unsigned int *mask,
624 unsigned char def)
625 {
626 u64 val;
627
628 if (!p)
629 goto use_default;
630 val = *p;
631
632 if (!val || val >= 64)
633 goto use_default;
634
635 *mask = ((1U << val) * 64U) - 1U;
636 return;
637
638 use_default:
639 *mask = ((1U << def) * 64U) - 1U;
640 }
641
642 static void __devinit get_mondo_data(struct mdesc_handle *hp, u64 mp,
643 struct trap_per_cpu *tb)
644 {
645 const u64 *val;
646
647 val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
648 get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7);
649
650 val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
651 get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7);
652
653 val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
654 get_one_mondo_bits(val, &tb->resum_qmask, 6);
655
656 val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
657 get_one_mondo_bits(val, &tb->nonresum_qmask, 2);
658 }
659
660 void __devinit mdesc_fill_in_cpu_data(cpumask_t mask)
661 {
662 struct mdesc_handle *hp = mdesc_grab();
663 u64 mp;
664
665 ncpus_probed = 0;
666 mdesc_for_each_node_by_name(hp, mp, "cpu") {
667 const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
668 const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
669 struct trap_per_cpu *tb;
670 cpuinfo_sparc *c;
671 int cpuid;
672 u64 a;
673
674 ncpus_probed++;
675
676 cpuid = *id;
677
678 #ifdef CONFIG_SMP
679 if (cpuid >= NR_CPUS)
680 continue;
681 if (!cpu_isset(cpuid, mask))
682 continue;
683 #else
684 /* On uniprocessor we only want the values for the
685 * real physical cpu the kernel booted onto, however
686 * cpu_data() only has one entry at index 0.
687 */
688 if (cpuid != real_hard_smp_processor_id())
689 continue;
690 cpuid = 0;
691 #endif
692
693 c = &cpu_data(cpuid);
694 c->clock_tick = *cfreq;
695
696 tb = &trap_block[cpuid];
697 get_mondo_data(hp, mp, tb);
698
699 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
700 u64 j, t = mdesc_arc_target(hp, a);
701 const char *t_name;
702
703 t_name = mdesc_node_name(hp, t);
704 if (!strcmp(t_name, "cache")) {
705 fill_in_one_cache(c, hp, t);
706 continue;
707 }
708
709 mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
710 u64 n = mdesc_arc_target(hp, j);
711 const char *n_name;
712
713 n_name = mdesc_node_name(hp, n);
714 if (!strcmp(n_name, "cache"))
715 fill_in_one_cache(c, hp, n);
716 }
717 }
718
719 #ifdef CONFIG_SMP
720 cpu_set(cpuid, cpu_present_map);
721 #endif
722
723 c->core_id = 0;
724 c->proc_id = -1;
725 }
726
727 #ifdef CONFIG_SMP
728 sparc64_multi_core = 1;
729 #endif
730
731 set_core_ids(hp);
732 set_proc_ids(hp);
733
734 smp_fill_in_sib_core_maps();
735
736 mdesc_release(hp);
737 }
738
739 void __init sun4v_mdesc_init(void)
740 {
741 struct mdesc_handle *hp;
742 unsigned long len, real_len, status;
743 cpumask_t mask;
744
745 (void) sun4v_mach_desc(0UL, 0UL, &len);
746
747 printk("MDESC: Size is %lu bytes.\n", len);
748
749 hp = mdesc_alloc(len, &bootmem_mdesc_memops);
750 if (hp == NULL) {
751 prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
752 prom_halt();
753 }
754
755 status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
756 if (status != HV_EOK || real_len > len) {
757 prom_printf("sun4v_mach_desc fails, err(%lu), "
758 "len(%lu), real_len(%lu)\n",
759 status, len, real_len);
760 mdesc_free(hp);
761 prom_halt();
762 }
763
764 cur_mdesc = hp;
765
766 report_platform_properties();
767
768 cpus_setall(mask);
769 mdesc_fill_in_cpu_data(mask);
770 }
This page took 0.04623 seconds and 6 git commands to generate.