mdesc.c 19 KB

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