mdesc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
  192. {
  193. const u64 *id;
  194. u64 a;
  195. id = NULL;
  196. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  197. u64 target;
  198. target = mdesc_arc_target(hp, a);
  199. id = mdesc_get_property(hp, target,
  200. "cfg-handle", NULL);
  201. if (id)
  202. break;
  203. }
  204. return id;
  205. }
  206. /* Run 'func' on nodes which are in A but not in B. */
  207. static void invoke_on_missing(const char *name,
  208. struct mdesc_handle *a,
  209. struct mdesc_handle *b,
  210. void (*func)(struct mdesc_handle *, u64))
  211. {
  212. u64 node;
  213. mdesc_for_each_node_by_name(a, node, name) {
  214. int found = 0, is_vdc_port = 0;
  215. const char *name_prop;
  216. const u64 *id;
  217. u64 fnode;
  218. name_prop = mdesc_get_property(a, node, "name", NULL);
  219. if (name_prop && !strcmp(name_prop, "vdc-port")) {
  220. is_vdc_port = 1;
  221. id = parent_cfg_handle(a, node);
  222. } else
  223. id = mdesc_get_property(a, node, "id", NULL);
  224. if (!id) {
  225. printk(KERN_ERR "MD: Cannot find ID for %s node.\n",
  226. (name_prop ? name_prop : name));
  227. continue;
  228. }
  229. mdesc_for_each_node_by_name(b, fnode, name) {
  230. const u64 *fid;
  231. if (is_vdc_port) {
  232. name_prop = mdesc_get_property(b, fnode,
  233. "name", NULL);
  234. if (!name_prop ||
  235. strcmp(name_prop, "vdc-port"))
  236. continue;
  237. fid = parent_cfg_handle(b, fnode);
  238. if (!fid) {
  239. printk(KERN_ERR "MD: Cannot find ID "
  240. "for vdc-port node.\n");
  241. continue;
  242. }
  243. } else
  244. fid = mdesc_get_property(b, fnode,
  245. "id", NULL);
  246. if (*id == *fid) {
  247. found = 1;
  248. break;
  249. }
  250. }
  251. if (!found)
  252. func(a, node);
  253. }
  254. }
  255. static void notify_one(struct mdesc_notifier_client *p,
  256. struct mdesc_handle *old_hp,
  257. struct mdesc_handle *new_hp)
  258. {
  259. invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
  260. invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
  261. }
  262. static void mdesc_notify_clients(struct mdesc_handle *old_hp,
  263. struct mdesc_handle *new_hp)
  264. {
  265. struct mdesc_notifier_client *p = client_list;
  266. while (p) {
  267. notify_one(p, old_hp, new_hp);
  268. p = p->next;
  269. }
  270. }
  271. void mdesc_update(void)
  272. {
  273. unsigned long len, real_len, status;
  274. struct mdesc_handle *hp, *orig_hp;
  275. unsigned long flags;
  276. mutex_lock(&mdesc_mutex);
  277. (void) sun4v_mach_desc(0UL, 0UL, &len);
  278. hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
  279. if (!hp) {
  280. printk(KERN_ERR "MD: mdesc alloc fails\n");
  281. goto out;
  282. }
  283. status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
  284. if (status != HV_EOK || real_len > len) {
  285. printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
  286. status);
  287. atomic_dec(&hp->refcnt);
  288. mdesc_free(hp);
  289. goto out;
  290. }
  291. spin_lock_irqsave(&mdesc_lock, flags);
  292. orig_hp = cur_mdesc;
  293. cur_mdesc = hp;
  294. spin_unlock_irqrestore(&mdesc_lock, flags);
  295. mdesc_notify_clients(orig_hp, hp);
  296. spin_lock_irqsave(&mdesc_lock, flags);
  297. if (atomic_dec_and_test(&orig_hp->refcnt))
  298. mdesc_free(orig_hp);
  299. else
  300. list_add(&orig_hp->list, &mdesc_zombie_list);
  301. spin_unlock_irqrestore(&mdesc_lock, flags);
  302. out:
  303. mutex_unlock(&mdesc_mutex);
  304. }
  305. static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
  306. {
  307. return (struct mdesc_elem *) (mdesc + 1);
  308. }
  309. static void *name_block(struct mdesc_hdr *mdesc)
  310. {
  311. return ((void *) node_block(mdesc)) + mdesc->node_sz;
  312. }
  313. static void *data_block(struct mdesc_hdr *mdesc)
  314. {
  315. return ((void *) name_block(mdesc)) + mdesc->name_sz;
  316. }
  317. u64 mdesc_node_by_name(struct mdesc_handle *hp,
  318. u64 from_node, const char *name)
  319. {
  320. struct mdesc_elem *ep = node_block(&hp->mdesc);
  321. const char *names = name_block(&hp->mdesc);
  322. u64 last_node = hp->mdesc.node_sz / 16;
  323. u64 ret;
  324. if (from_node == MDESC_NODE_NULL) {
  325. ret = from_node = 0;
  326. } else if (from_node >= last_node) {
  327. return MDESC_NODE_NULL;
  328. } else {
  329. ret = ep[from_node].d.val;
  330. }
  331. while (ret < last_node) {
  332. if (ep[ret].tag != MD_NODE)
  333. return MDESC_NODE_NULL;
  334. if (!strcmp(names + ep[ret].name_offset, name))
  335. break;
  336. ret = ep[ret].d.val;
  337. }
  338. if (ret >= last_node)
  339. ret = MDESC_NODE_NULL;
  340. return ret;
  341. }
  342. EXPORT_SYMBOL(mdesc_node_by_name);
  343. const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
  344. const char *name, int *lenp)
  345. {
  346. const char *names = name_block(&hp->mdesc);
  347. u64 last_node = hp->mdesc.node_sz / 16;
  348. void *data = data_block(&hp->mdesc);
  349. struct mdesc_elem *ep;
  350. if (node == MDESC_NODE_NULL || node >= last_node)
  351. return NULL;
  352. ep = node_block(&hp->mdesc) + node;
  353. ep++;
  354. for (; ep->tag != MD_NODE_END; ep++) {
  355. void *val = NULL;
  356. int len = 0;
  357. switch (ep->tag) {
  358. case MD_PROP_VAL:
  359. val = &ep->d.val;
  360. len = 8;
  361. break;
  362. case MD_PROP_STR:
  363. case MD_PROP_DATA:
  364. val = data + ep->d.data.data_offset;
  365. len = ep->d.data.data_len;
  366. break;
  367. default:
  368. break;
  369. }
  370. if (!val)
  371. continue;
  372. if (!strcmp(names + ep->name_offset, name)) {
  373. if (lenp)
  374. *lenp = len;
  375. return val;
  376. }
  377. }
  378. return NULL;
  379. }
  380. EXPORT_SYMBOL(mdesc_get_property);
  381. u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
  382. {
  383. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  384. const char *names = name_block(&hp->mdesc);
  385. u64 last_node = hp->mdesc.node_sz / 16;
  386. if (from == MDESC_NODE_NULL || from >= last_node)
  387. return MDESC_NODE_NULL;
  388. ep = base + from;
  389. ep++;
  390. for (; ep->tag != MD_NODE_END; ep++) {
  391. if (ep->tag != MD_PROP_ARC)
  392. continue;
  393. if (strcmp(names + ep->name_offset, arc_type))
  394. continue;
  395. return ep - base;
  396. }
  397. return MDESC_NODE_NULL;
  398. }
  399. EXPORT_SYMBOL(mdesc_next_arc);
  400. u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
  401. {
  402. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  403. ep = base + arc;
  404. return ep->d.val;
  405. }
  406. EXPORT_SYMBOL(mdesc_arc_target);
  407. const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
  408. {
  409. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  410. const char *names = name_block(&hp->mdesc);
  411. u64 last_node = hp->mdesc.node_sz / 16;
  412. if (node == MDESC_NODE_NULL || node >= last_node)
  413. return NULL;
  414. ep = base + node;
  415. if (ep->tag != MD_NODE)
  416. return NULL;
  417. return names + ep->name_offset;
  418. }
  419. EXPORT_SYMBOL(mdesc_node_name);
  420. static void __init report_platform_properties(void)
  421. {
  422. struct mdesc_handle *hp = mdesc_grab();
  423. u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
  424. const char *s;
  425. const u64 *v;
  426. if (pn == MDESC_NODE_NULL) {
  427. prom_printf("No platform node in machine-description.\n");
  428. prom_halt();
  429. }
  430. s = mdesc_get_property(hp, pn, "banner-name", NULL);
  431. printk("PLATFORM: banner-name [%s]\n", s);
  432. s = mdesc_get_property(hp, pn, "name", NULL);
  433. printk("PLATFORM: name [%s]\n", s);
  434. v = mdesc_get_property(hp, pn, "hostid", NULL);
  435. if (v)
  436. printk("PLATFORM: hostid [%08lx]\n", *v);
  437. v = mdesc_get_property(hp, pn, "serial#", NULL);
  438. if (v)
  439. printk("PLATFORM: serial# [%08lx]\n", *v);
  440. v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
  441. printk("PLATFORM: stick-frequency [%08lx]\n", *v);
  442. v = mdesc_get_property(hp, pn, "mac-address", NULL);
  443. if (v)
  444. printk("PLATFORM: mac-address [%lx]\n", *v);
  445. v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
  446. if (v)
  447. printk("PLATFORM: watchdog-resolution [%lu ms]\n", *v);
  448. v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
  449. if (v)
  450. printk("PLATFORM: watchdog-max-timeout [%lu ms]\n", *v);
  451. v = mdesc_get_property(hp, pn, "max-cpus", NULL);
  452. if (v)
  453. printk("PLATFORM: max-cpus [%lu]\n", *v);
  454. #ifdef CONFIG_SMP
  455. {
  456. int max_cpu, i;
  457. if (v) {
  458. max_cpu = *v;
  459. if (max_cpu > NR_CPUS)
  460. max_cpu = NR_CPUS;
  461. } else {
  462. max_cpu = NR_CPUS;
  463. }
  464. for (i = 0; i < max_cpu; i++)
  465. cpu_set(i, cpu_possible_map);
  466. }
  467. #endif
  468. mdesc_release(hp);
  469. }
  470. static int inline find_in_proplist(const char *list, const char *match, int len)
  471. {
  472. while (len > 0) {
  473. int l;
  474. if (!strcmp(list, match))
  475. return 1;
  476. l = strlen(list) + 1;
  477. list += l;
  478. len -= l;
  479. }
  480. return 0;
  481. }
  482. static void __devinit fill_in_one_cache(cpuinfo_sparc *c,
  483. struct mdesc_handle *hp,
  484. u64 mp)
  485. {
  486. const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
  487. const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
  488. const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
  489. const char *type;
  490. int type_len;
  491. type = mdesc_get_property(hp, mp, "type", &type_len);
  492. switch (*level) {
  493. case 1:
  494. if (find_in_proplist(type, "instn", type_len)) {
  495. c->icache_size = *size;
  496. c->icache_line_size = *line_size;
  497. } else if (find_in_proplist(type, "data", type_len)) {
  498. c->dcache_size = *size;
  499. c->dcache_line_size = *line_size;
  500. }
  501. break;
  502. case 2:
  503. c->ecache_size = *size;
  504. c->ecache_line_size = *line_size;
  505. break;
  506. default:
  507. break;
  508. }
  509. if (*level == 1) {
  510. u64 a;
  511. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  512. u64 target = mdesc_arc_target(hp, a);
  513. const char *name = mdesc_node_name(hp, target);
  514. if (!strcmp(name, "cache"))
  515. fill_in_one_cache(c, hp, target);
  516. }
  517. }
  518. }
  519. static void __devinit mark_core_ids(struct mdesc_handle *hp, u64 mp,
  520. int core_id)
  521. {
  522. u64 a;
  523. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
  524. u64 t = mdesc_arc_target(hp, a);
  525. const char *name;
  526. const u64 *id;
  527. name = mdesc_node_name(hp, t);
  528. if (!strcmp(name, "cpu")) {
  529. id = mdesc_get_property(hp, t, "id", NULL);
  530. if (*id < NR_CPUS)
  531. cpu_data(*id).core_id = core_id;
  532. } else {
  533. u64 j;
  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. n_name = mdesc_node_name(hp, n);
  538. if (strcmp(n_name, "cpu"))
  539. continue;
  540. id = mdesc_get_property(hp, n, "id", NULL);
  541. if (*id < NR_CPUS)
  542. cpu_data(*id).core_id = core_id;
  543. }
  544. }
  545. }
  546. }
  547. static void __devinit set_core_ids(struct mdesc_handle *hp)
  548. {
  549. int idx;
  550. u64 mp;
  551. idx = 1;
  552. mdesc_for_each_node_by_name(hp, mp, "cache") {
  553. const u64 *level;
  554. const char *type;
  555. int len;
  556. level = mdesc_get_property(hp, mp, "level", NULL);
  557. if (*level != 1)
  558. continue;
  559. type = mdesc_get_property(hp, mp, "type", &len);
  560. if (!find_in_proplist(type, "instn", len))
  561. continue;
  562. mark_core_ids(hp, mp, idx);
  563. idx++;
  564. }
  565. }
  566. static void __devinit mark_proc_ids(struct mdesc_handle *hp, u64 mp,
  567. int proc_id)
  568. {
  569. u64 a;
  570. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
  571. u64 t = mdesc_arc_target(hp, a);
  572. const char *name;
  573. const u64 *id;
  574. name = mdesc_node_name(hp, t);
  575. if (strcmp(name, "cpu"))
  576. continue;
  577. id = mdesc_get_property(hp, t, "id", NULL);
  578. if (*id < NR_CPUS)
  579. cpu_data(*id).proc_id = proc_id;
  580. }
  581. }
  582. static void __devinit __set_proc_ids(struct mdesc_handle *hp,
  583. const char *exec_unit_name)
  584. {
  585. int idx;
  586. u64 mp;
  587. idx = 0;
  588. mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
  589. const char *type;
  590. int len;
  591. type = mdesc_get_property(hp, mp, "type", &len);
  592. if (!find_in_proplist(type, "int", len) &&
  593. !find_in_proplist(type, "integer", len))
  594. continue;
  595. mark_proc_ids(hp, mp, idx);
  596. idx++;
  597. }
  598. }
  599. static void __devinit set_proc_ids(struct mdesc_handle *hp)
  600. {
  601. __set_proc_ids(hp, "exec_unit");
  602. __set_proc_ids(hp, "exec-unit");
  603. }
  604. static void __devinit get_one_mondo_bits(const u64 *p, unsigned int *mask,
  605. unsigned char def)
  606. {
  607. u64 val;
  608. if (!p)
  609. goto use_default;
  610. val = *p;
  611. if (!val || val >= 64)
  612. goto use_default;
  613. *mask = ((1U << val) * 64U) - 1U;
  614. return;
  615. use_default:
  616. *mask = ((1U << def) * 64U) - 1U;
  617. }
  618. static void __devinit get_mondo_data(struct mdesc_handle *hp, u64 mp,
  619. struct trap_per_cpu *tb)
  620. {
  621. const u64 *val;
  622. val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
  623. get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7);
  624. val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
  625. get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7);
  626. val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
  627. get_one_mondo_bits(val, &tb->resum_qmask, 6);
  628. val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
  629. get_one_mondo_bits(val, &tb->nonresum_qmask, 2);
  630. }
  631. void __devinit mdesc_fill_in_cpu_data(cpumask_t mask)
  632. {
  633. struct mdesc_handle *hp = mdesc_grab();
  634. u64 mp;
  635. ncpus_probed = 0;
  636. mdesc_for_each_node_by_name(hp, mp, "cpu") {
  637. const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
  638. const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
  639. struct trap_per_cpu *tb;
  640. cpuinfo_sparc *c;
  641. int cpuid;
  642. u64 a;
  643. ncpus_probed++;
  644. cpuid = *id;
  645. #ifdef CONFIG_SMP
  646. if (cpuid >= NR_CPUS)
  647. continue;
  648. if (!cpu_isset(cpuid, mask))
  649. continue;
  650. #else
  651. /* On uniprocessor we only want the values for the
  652. * real physical cpu the kernel booted onto, however
  653. * cpu_data() only has one entry at index 0.
  654. */
  655. if (cpuid != real_hard_smp_processor_id())
  656. continue;
  657. cpuid = 0;
  658. #endif
  659. c = &cpu_data(cpuid);
  660. c->clock_tick = *cfreq;
  661. tb = &trap_block[cpuid];
  662. get_mondo_data(hp, mp, tb);
  663. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  664. u64 j, t = mdesc_arc_target(hp, a);
  665. const char *t_name;
  666. t_name = mdesc_node_name(hp, t);
  667. if (!strcmp(t_name, "cache")) {
  668. fill_in_one_cache(c, hp, t);
  669. continue;
  670. }
  671. mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
  672. u64 n = mdesc_arc_target(hp, j);
  673. const char *n_name;
  674. n_name = mdesc_node_name(hp, n);
  675. if (!strcmp(n_name, "cache"))
  676. fill_in_one_cache(c, hp, n);
  677. }
  678. }
  679. #ifdef CONFIG_SMP
  680. cpu_set(cpuid, cpu_present_map);
  681. #endif
  682. c->core_id = 0;
  683. c->proc_id = -1;
  684. }
  685. #ifdef CONFIG_SMP
  686. sparc64_multi_core = 1;
  687. #endif
  688. set_core_ids(hp);
  689. set_proc_ids(hp);
  690. smp_fill_in_sib_core_maps();
  691. mdesc_release(hp);
  692. }
  693. void __init sun4v_mdesc_init(void)
  694. {
  695. struct mdesc_handle *hp;
  696. unsigned long len, real_len, status;
  697. cpumask_t mask;
  698. (void) sun4v_mach_desc(0UL, 0UL, &len);
  699. printk("MDESC: Size is %lu bytes.\n", len);
  700. hp = mdesc_alloc(len, &bootmem_mdesc_memops);
  701. if (hp == NULL) {
  702. prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
  703. prom_halt();
  704. }
  705. status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
  706. if (status != HV_EOK || real_len > len) {
  707. prom_printf("sun4v_mach_desc fails, err(%lu), "
  708. "len(%lu), real_len(%lu)\n",
  709. status, len, real_len);
  710. mdesc_free(hp);
  711. prom_halt();
  712. }
  713. cur_mdesc = hp;
  714. report_platform_properties();
  715. cpus_setall(mask);
  716. mdesc_fill_in_cpu_data(mask);
  717. }