mdesc.c 19 KB

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