mdesc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. #include <asm/hypervisor.h>
  13. #include <asm/mdesc.h>
  14. #include <asm/prom.h>
  15. #include <asm/oplib.h>
  16. #include <asm/smp.h>
  17. /* Unlike the OBP device tree, the machine description is a full-on
  18. * DAG. An arbitrary number of ARCs are possible from one
  19. * node to other nodes and thus we can't use the OBP device_node
  20. * data structure to represent these nodes inside of the kernel.
  21. *
  22. * Actually, it isn't even a DAG, because there are back pointers
  23. * which create cycles in the graph.
  24. *
  25. * mdesc_hdr and mdesc_elem describe the layout of the data structure
  26. * we get from the Hypervisor.
  27. */
  28. struct mdesc_hdr {
  29. u32 version; /* Transport version */
  30. u32 node_sz; /* node block size */
  31. u32 name_sz; /* name block size */
  32. u32 data_sz; /* data block size */
  33. } __attribute__((aligned(16)));
  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;
  54. };
  55. struct mdesc_mem_ops {
  56. struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
  57. void (*free)(struct mdesc_handle *handle);
  58. };
  59. struct mdesc_handle {
  60. struct list_head list;
  61. struct mdesc_mem_ops *mops;
  62. void *self_base;
  63. atomic_t refcnt;
  64. unsigned int handle_size;
  65. struct mdesc_hdr mdesc;
  66. };
  67. static void mdesc_handle_init(struct mdesc_handle *hp,
  68. unsigned int handle_size,
  69. void *base)
  70. {
  71. BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));
  72. memset(hp, 0, handle_size);
  73. INIT_LIST_HEAD(&hp->list);
  74. hp->self_base = base;
  75. atomic_set(&hp->refcnt, 1);
  76. hp->handle_size = handle_size;
  77. }
  78. static struct mdesc_handle *mdesc_bootmem_alloc(unsigned int mdesc_size)
  79. {
  80. struct mdesc_handle *hp;
  81. unsigned int handle_size, alloc_size;
  82. handle_size = (sizeof(struct mdesc_handle) -
  83. sizeof(struct mdesc_hdr) +
  84. mdesc_size);
  85. alloc_size = PAGE_ALIGN(handle_size);
  86. hp = __alloc_bootmem(alloc_size, PAGE_SIZE, 0UL);
  87. if (hp)
  88. mdesc_handle_init(hp, handle_size, hp);
  89. return hp;
  90. }
  91. static void mdesc_bootmem_free(struct mdesc_handle *hp)
  92. {
  93. unsigned int alloc_size, handle_size = hp->handle_size;
  94. unsigned long start, end;
  95. BUG_ON(atomic_read(&hp->refcnt) != 0);
  96. BUG_ON(!list_empty(&hp->list));
  97. alloc_size = PAGE_ALIGN(handle_size);
  98. start = (unsigned long) hp;
  99. end = start + alloc_size;
  100. while (start < end) {
  101. struct page *p;
  102. p = virt_to_page(start);
  103. ClearPageReserved(p);
  104. __free_page(p);
  105. start += PAGE_SIZE;
  106. }
  107. }
  108. static struct mdesc_mem_ops bootmem_mdesc_memops = {
  109. .alloc = mdesc_bootmem_alloc,
  110. .free = mdesc_bootmem_free,
  111. };
  112. static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
  113. {
  114. unsigned int handle_size;
  115. void *base;
  116. handle_size = (sizeof(struct mdesc_handle) -
  117. sizeof(struct mdesc_hdr) +
  118. mdesc_size);
  119. base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_NOFAIL);
  120. if (base) {
  121. struct mdesc_handle *hp;
  122. unsigned long addr;
  123. addr = (unsigned long)base;
  124. addr = (addr + 15UL) & ~15UL;
  125. hp = (struct mdesc_handle *) addr;
  126. mdesc_handle_init(hp, handle_size, base);
  127. return hp;
  128. }
  129. return NULL;
  130. }
  131. static void mdesc_kfree(struct mdesc_handle *hp)
  132. {
  133. BUG_ON(atomic_read(&hp->refcnt) != 0);
  134. BUG_ON(!list_empty(&hp->list));
  135. kfree(hp->self_base);
  136. }
  137. static struct mdesc_mem_ops kmalloc_mdesc_memops = {
  138. .alloc = mdesc_kmalloc,
  139. .free = mdesc_kfree,
  140. };
  141. static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
  142. struct mdesc_mem_ops *mops)
  143. {
  144. struct mdesc_handle *hp = mops->alloc(mdesc_size);
  145. if (hp)
  146. hp->mops = mops;
  147. return hp;
  148. }
  149. static void mdesc_free(struct mdesc_handle *hp)
  150. {
  151. hp->mops->free(hp);
  152. }
  153. static struct mdesc_handle *cur_mdesc;
  154. static LIST_HEAD(mdesc_zombie_list);
  155. static DEFINE_SPINLOCK(mdesc_lock);
  156. struct mdesc_handle *mdesc_grab(void)
  157. {
  158. struct mdesc_handle *hp;
  159. unsigned long flags;
  160. spin_lock_irqsave(&mdesc_lock, flags);
  161. hp = cur_mdesc;
  162. if (hp)
  163. atomic_inc(&hp->refcnt);
  164. spin_unlock_irqrestore(&mdesc_lock, flags);
  165. return hp;
  166. }
  167. EXPORT_SYMBOL(mdesc_grab);
  168. void mdesc_release(struct mdesc_handle *hp)
  169. {
  170. unsigned long flags;
  171. spin_lock_irqsave(&mdesc_lock, flags);
  172. if (atomic_dec_and_test(&hp->refcnt)) {
  173. list_del_init(&hp->list);
  174. hp->mops->free(hp);
  175. }
  176. spin_unlock_irqrestore(&mdesc_lock, flags);
  177. }
  178. EXPORT_SYMBOL(mdesc_release);
  179. static DEFINE_MUTEX(mdesc_mutex);
  180. static struct mdesc_notifier_client *client_list;
  181. void mdesc_register_notifier(struct mdesc_notifier_client *client)
  182. {
  183. u64 node;
  184. mutex_lock(&mdesc_mutex);
  185. client->next = client_list;
  186. client_list = client;
  187. mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
  188. client->add(cur_mdesc, node);
  189. mutex_unlock(&mdesc_mutex);
  190. }
  191. /* Run 'func' on nodes which are in A but not in B. */
  192. static void invoke_on_missing(const char *name,
  193. struct mdesc_handle *a,
  194. struct mdesc_handle *b,
  195. void (*func)(struct mdesc_handle *, u64))
  196. {
  197. u64 node;
  198. mdesc_for_each_node_by_name(a, node, name) {
  199. const u64 *id = mdesc_get_property(a, node, "id", NULL);
  200. int found = 0;
  201. u64 fnode;
  202. mdesc_for_each_node_by_name(b, fnode, name) {
  203. const u64 *fid = mdesc_get_property(b, fnode,
  204. "id", NULL);
  205. if (*id == *fid) {
  206. found = 1;
  207. break;
  208. }
  209. }
  210. if (!found)
  211. func(a, node);
  212. }
  213. }
  214. static void notify_one(struct mdesc_notifier_client *p,
  215. struct mdesc_handle *old_hp,
  216. struct mdesc_handle *new_hp)
  217. {
  218. invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
  219. invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
  220. }
  221. static void mdesc_notify_clients(struct mdesc_handle *old_hp,
  222. struct mdesc_handle *new_hp)
  223. {
  224. struct mdesc_notifier_client *p = client_list;
  225. while (p) {
  226. notify_one(p, old_hp, new_hp);
  227. p = p->next;
  228. }
  229. }
  230. void mdesc_update(void)
  231. {
  232. unsigned long len, real_len, status;
  233. struct mdesc_handle *hp, *orig_hp;
  234. unsigned long flags;
  235. mutex_lock(&mdesc_mutex);
  236. (void) sun4v_mach_desc(0UL, 0UL, &len);
  237. hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
  238. if (!hp) {
  239. printk(KERN_ERR "MD: mdesc alloc fails\n");
  240. goto out;
  241. }
  242. status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
  243. if (status != HV_EOK || real_len > len) {
  244. printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
  245. status);
  246. atomic_dec(&hp->refcnt);
  247. mdesc_free(hp);
  248. goto out;
  249. }
  250. spin_lock_irqsave(&mdesc_lock, flags);
  251. orig_hp = cur_mdesc;
  252. cur_mdesc = hp;
  253. spin_unlock_irqrestore(&mdesc_lock, flags);
  254. mdesc_notify_clients(orig_hp, hp);
  255. spin_lock_irqsave(&mdesc_lock, flags);
  256. if (atomic_dec_and_test(&orig_hp->refcnt))
  257. mdesc_free(orig_hp);
  258. else
  259. list_add(&orig_hp->list, &mdesc_zombie_list);
  260. spin_unlock_irqrestore(&mdesc_lock, flags);
  261. out:
  262. mutex_unlock(&mdesc_mutex);
  263. }
  264. static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
  265. {
  266. return (struct mdesc_elem *) (mdesc + 1);
  267. }
  268. static void *name_block(struct mdesc_hdr *mdesc)
  269. {
  270. return ((void *) node_block(mdesc)) + mdesc->node_sz;
  271. }
  272. static void *data_block(struct mdesc_hdr *mdesc)
  273. {
  274. return ((void *) name_block(mdesc)) + mdesc->name_sz;
  275. }
  276. u64 mdesc_node_by_name(struct mdesc_handle *hp,
  277. u64 from_node, const char *name)
  278. {
  279. struct mdesc_elem *ep = node_block(&hp->mdesc);
  280. const char *names = name_block(&hp->mdesc);
  281. u64 last_node = hp->mdesc.node_sz / 16;
  282. u64 ret;
  283. if (from_node == MDESC_NODE_NULL) {
  284. ret = from_node = 0;
  285. } else if (from_node >= last_node) {
  286. return MDESC_NODE_NULL;
  287. } else {
  288. ret = ep[from_node].d.val;
  289. }
  290. while (ret < last_node) {
  291. if (ep[ret].tag != MD_NODE)
  292. return MDESC_NODE_NULL;
  293. if (!strcmp(names + ep[ret].name_offset, name))
  294. break;
  295. ret = ep[ret].d.val;
  296. }
  297. if (ret >= last_node)
  298. ret = MDESC_NODE_NULL;
  299. return ret;
  300. }
  301. EXPORT_SYMBOL(mdesc_node_by_name);
  302. const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
  303. const char *name, int *lenp)
  304. {
  305. const char *names = name_block(&hp->mdesc);
  306. u64 last_node = hp->mdesc.node_sz / 16;
  307. void *data = data_block(&hp->mdesc);
  308. struct mdesc_elem *ep;
  309. if (node == MDESC_NODE_NULL || node >= last_node)
  310. return NULL;
  311. ep = node_block(&hp->mdesc) + node;
  312. ep++;
  313. for (; ep->tag != MD_NODE_END; ep++) {
  314. void *val = NULL;
  315. int len = 0;
  316. switch (ep->tag) {
  317. case MD_PROP_VAL:
  318. val = &ep->d.val;
  319. len = 8;
  320. break;
  321. case MD_PROP_STR:
  322. case MD_PROP_DATA:
  323. val = data + ep->d.data.data_offset;
  324. len = ep->d.data.data_len;
  325. break;
  326. default:
  327. break;
  328. }
  329. if (!val)
  330. continue;
  331. if (!strcmp(names + ep->name_offset, name)) {
  332. if (lenp)
  333. *lenp = len;
  334. return val;
  335. }
  336. }
  337. return NULL;
  338. }
  339. EXPORT_SYMBOL(mdesc_get_property);
  340. u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
  341. {
  342. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  343. const char *names = name_block(&hp->mdesc);
  344. u64 last_node = hp->mdesc.node_sz / 16;
  345. if (from == MDESC_NODE_NULL || from >= last_node)
  346. return MDESC_NODE_NULL;
  347. ep = base + from;
  348. ep++;
  349. for (; ep->tag != MD_NODE_END; ep++) {
  350. if (ep->tag != MD_PROP_ARC)
  351. continue;
  352. if (strcmp(names + ep->name_offset, arc_type))
  353. continue;
  354. return ep - base;
  355. }
  356. return MDESC_NODE_NULL;
  357. }
  358. EXPORT_SYMBOL(mdesc_next_arc);
  359. u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
  360. {
  361. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  362. ep = base + arc;
  363. return ep->d.val;
  364. }
  365. EXPORT_SYMBOL(mdesc_arc_target);
  366. const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
  367. {
  368. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  369. const char *names = name_block(&hp->mdesc);
  370. u64 last_node = hp->mdesc.node_sz / 16;
  371. if (node == MDESC_NODE_NULL || node >= last_node)
  372. return NULL;
  373. ep = base + node;
  374. if (ep->tag != MD_NODE)
  375. return NULL;
  376. return names + ep->name_offset;
  377. }
  378. EXPORT_SYMBOL(mdesc_node_name);
  379. static void __init report_platform_properties(void)
  380. {
  381. struct mdesc_handle *hp = mdesc_grab();
  382. u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
  383. const char *s;
  384. const u64 *v;
  385. if (pn == MDESC_NODE_NULL) {
  386. prom_printf("No platform node in machine-description.\n");
  387. prom_halt();
  388. }
  389. s = mdesc_get_property(hp, pn, "banner-name", NULL);
  390. printk("PLATFORM: banner-name [%s]\n", s);
  391. s = mdesc_get_property(hp, pn, "name", NULL);
  392. printk("PLATFORM: name [%s]\n", s);
  393. v = mdesc_get_property(hp, pn, "hostid", NULL);
  394. if (v)
  395. printk("PLATFORM: hostid [%08lx]\n", *v);
  396. v = mdesc_get_property(hp, pn, "serial#", NULL);
  397. if (v)
  398. printk("PLATFORM: serial# [%08lx]\n", *v);
  399. v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
  400. printk("PLATFORM: stick-frequency [%08lx]\n", *v);
  401. v = mdesc_get_property(hp, pn, "mac-address", NULL);
  402. if (v)
  403. printk("PLATFORM: mac-address [%lx]\n", *v);
  404. v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
  405. if (v)
  406. printk("PLATFORM: watchdog-resolution [%lu ms]\n", *v);
  407. v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
  408. if (v)
  409. printk("PLATFORM: watchdog-max-timeout [%lu ms]\n", *v);
  410. v = mdesc_get_property(hp, pn, "max-cpus", NULL);
  411. if (v)
  412. printk("PLATFORM: max-cpus [%lu]\n", *v);
  413. #ifdef CONFIG_SMP
  414. {
  415. int max_cpu, i;
  416. if (v) {
  417. max_cpu = *v;
  418. if (max_cpu > NR_CPUS)
  419. max_cpu = NR_CPUS;
  420. } else {
  421. max_cpu = NR_CPUS;
  422. }
  423. for (i = 0; i < max_cpu; i++)
  424. cpu_set(i, cpu_possible_map);
  425. }
  426. #endif
  427. mdesc_release(hp);
  428. }
  429. static int inline find_in_proplist(const char *list, const char *match, int len)
  430. {
  431. while (len > 0) {
  432. int l;
  433. if (!strcmp(list, match))
  434. return 1;
  435. l = strlen(list) + 1;
  436. list += l;
  437. len -= l;
  438. }
  439. return 0;
  440. }
  441. static void __devinit fill_in_one_cache(cpuinfo_sparc *c,
  442. struct mdesc_handle *hp,
  443. u64 mp)
  444. {
  445. const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
  446. const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
  447. const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
  448. const char *type;
  449. int type_len;
  450. type = mdesc_get_property(hp, mp, "type", &type_len);
  451. switch (*level) {
  452. case 1:
  453. if (find_in_proplist(type, "instn", type_len)) {
  454. c->icache_size = *size;
  455. c->icache_line_size = *line_size;
  456. } else if (find_in_proplist(type, "data", type_len)) {
  457. c->dcache_size = *size;
  458. c->dcache_line_size = *line_size;
  459. }
  460. break;
  461. case 2:
  462. c->ecache_size = *size;
  463. c->ecache_line_size = *line_size;
  464. break;
  465. default:
  466. break;
  467. }
  468. if (*level == 1) {
  469. u64 a;
  470. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  471. u64 target = mdesc_arc_target(hp, a);
  472. const char *name = mdesc_node_name(hp, target);
  473. if (!strcmp(name, "cache"))
  474. fill_in_one_cache(c, hp, target);
  475. }
  476. }
  477. }
  478. static void __devinit mark_core_ids(struct mdesc_handle *hp, u64 mp,
  479. int core_id)
  480. {
  481. u64 a;
  482. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
  483. u64 t = mdesc_arc_target(hp, a);
  484. const char *name;
  485. const u64 *id;
  486. name = mdesc_node_name(hp, t);
  487. if (!strcmp(name, "cpu")) {
  488. id = mdesc_get_property(hp, t, "id", NULL);
  489. if (*id < NR_CPUS)
  490. cpu_data(*id).core_id = core_id;
  491. } else {
  492. u64 j;
  493. mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_BACK) {
  494. u64 n = mdesc_arc_target(hp, j);
  495. const char *n_name;
  496. n_name = mdesc_node_name(hp, n);
  497. if (strcmp(n_name, "cpu"))
  498. continue;
  499. id = mdesc_get_property(hp, n, "id", NULL);
  500. if (*id < NR_CPUS)
  501. cpu_data(*id).core_id = core_id;
  502. }
  503. }
  504. }
  505. }
  506. static void __devinit set_core_ids(struct mdesc_handle *hp)
  507. {
  508. int idx;
  509. u64 mp;
  510. idx = 1;
  511. mdesc_for_each_node_by_name(hp, mp, "cache") {
  512. const u64 *level;
  513. const char *type;
  514. int len;
  515. level = mdesc_get_property(hp, mp, "level", NULL);
  516. if (*level != 1)
  517. continue;
  518. type = mdesc_get_property(hp, mp, "type", &len);
  519. if (!find_in_proplist(type, "instn", len))
  520. continue;
  521. mark_core_ids(hp, mp, idx);
  522. idx++;
  523. }
  524. }
  525. static void __devinit mark_proc_ids(struct mdesc_handle *hp, u64 mp,
  526. int proc_id)
  527. {
  528. u64 a;
  529. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
  530. u64 t = mdesc_arc_target(hp, a);
  531. const char *name;
  532. const u64 *id;
  533. name = mdesc_node_name(hp, t);
  534. if (strcmp(name, "cpu"))
  535. continue;
  536. id = mdesc_get_property(hp, t, "id", NULL);
  537. if (*id < NR_CPUS)
  538. cpu_data(*id).proc_id = proc_id;
  539. }
  540. }
  541. static void __devinit __set_proc_ids(struct mdesc_handle *hp,
  542. const char *exec_unit_name)
  543. {
  544. int idx;
  545. u64 mp;
  546. idx = 0;
  547. mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
  548. const char *type;
  549. int len;
  550. type = mdesc_get_property(hp, mp, "type", &len);
  551. if (!find_in_proplist(type, "int", len) &&
  552. !find_in_proplist(type, "integer", len))
  553. continue;
  554. mark_proc_ids(hp, mp, idx);
  555. idx++;
  556. }
  557. }
  558. static void __devinit set_proc_ids(struct mdesc_handle *hp)
  559. {
  560. __set_proc_ids(hp, "exec_unit");
  561. __set_proc_ids(hp, "exec-unit");
  562. }
  563. static void __devinit get_one_mondo_bits(const u64 *p, unsigned int *mask,
  564. unsigned char def)
  565. {
  566. u64 val;
  567. if (!p)
  568. goto use_default;
  569. val = *p;
  570. if (!val || val >= 64)
  571. goto use_default;
  572. *mask = ((1U << val) * 64U) - 1U;
  573. return;
  574. use_default:
  575. *mask = ((1U << def) * 64U) - 1U;
  576. }
  577. static void __devinit get_mondo_data(struct mdesc_handle *hp, u64 mp,
  578. struct trap_per_cpu *tb)
  579. {
  580. const u64 *val;
  581. val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
  582. get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7);
  583. val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
  584. get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7);
  585. val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
  586. get_one_mondo_bits(val, &tb->resum_qmask, 6);
  587. val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
  588. get_one_mondo_bits(val, &tb->nonresum_qmask, 2);
  589. }
  590. void __devinit mdesc_fill_in_cpu_data(cpumask_t mask)
  591. {
  592. struct mdesc_handle *hp = mdesc_grab();
  593. u64 mp;
  594. ncpus_probed = 0;
  595. mdesc_for_each_node_by_name(hp, mp, "cpu") {
  596. const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
  597. const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
  598. struct trap_per_cpu *tb;
  599. cpuinfo_sparc *c;
  600. int cpuid;
  601. u64 a;
  602. ncpus_probed++;
  603. cpuid = *id;
  604. #ifdef CONFIG_SMP
  605. if (cpuid >= NR_CPUS)
  606. continue;
  607. if (!cpu_isset(cpuid, mask))
  608. continue;
  609. #else
  610. /* On uniprocessor we only want the values for the
  611. * real physical cpu the kernel booted onto, however
  612. * cpu_data() only has one entry at index 0.
  613. */
  614. if (cpuid != real_hard_smp_processor_id())
  615. continue;
  616. cpuid = 0;
  617. #endif
  618. c = &cpu_data(cpuid);
  619. c->clock_tick = *cfreq;
  620. tb = &trap_block[cpuid];
  621. get_mondo_data(hp, mp, tb);
  622. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  623. u64 j, t = mdesc_arc_target(hp, a);
  624. const char *t_name;
  625. t_name = mdesc_node_name(hp, t);
  626. if (!strcmp(t_name, "cache")) {
  627. fill_in_one_cache(c, hp, t);
  628. continue;
  629. }
  630. mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
  631. u64 n = mdesc_arc_target(hp, j);
  632. const char *n_name;
  633. n_name = mdesc_node_name(hp, n);
  634. if (!strcmp(n_name, "cache"))
  635. fill_in_one_cache(c, hp, n);
  636. }
  637. }
  638. #ifdef CONFIG_SMP
  639. cpu_set(cpuid, cpu_present_map);
  640. #endif
  641. c->core_id = 0;
  642. c->proc_id = -1;
  643. }
  644. #ifdef CONFIG_SMP
  645. sparc64_multi_core = 1;
  646. #endif
  647. set_core_ids(hp);
  648. set_proc_ids(hp);
  649. smp_fill_in_sib_core_maps();
  650. mdesc_release(hp);
  651. }
  652. void __init sun4v_mdesc_init(void)
  653. {
  654. struct mdesc_handle *hp;
  655. unsigned long len, real_len, status;
  656. cpumask_t mask;
  657. (void) sun4v_mach_desc(0UL, 0UL, &len);
  658. printk("MDESC: Size is %lu bytes.\n", len);
  659. hp = mdesc_alloc(len, &bootmem_mdesc_memops);
  660. if (hp == NULL) {
  661. prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
  662. prom_halt();
  663. }
  664. status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
  665. if (status != HV_EOK || real_len > len) {
  666. prom_printf("sun4v_mach_desc fails, err(%lu), "
  667. "len(%lu), real_len(%lu)\n",
  668. status, len, real_len);
  669. mdesc_free(hp);
  670. prom_halt();
  671. }
  672. cur_mdesc = hp;
  673. report_platform_properties();
  674. cpus_setall(mask);
  675. mdesc_fill_in_cpu_data(mask);
  676. }