mdesc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 __cpuinit 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 __cpuinit mark_core_ids(struct mdesc_handle *hp, u64 mp, 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 __cpuinit 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 __cpuinit mark_proc_ids(struct mdesc_handle *hp, u64 mp, int proc_id)
  560. {
  561. u64 a;
  562. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
  563. u64 t = mdesc_arc_target(hp, a);
  564. const char *name;
  565. const u64 *id;
  566. name = mdesc_node_name(hp, t);
  567. if (strcmp(name, "cpu"))
  568. continue;
  569. id = mdesc_get_property(hp, t, "id", NULL);
  570. if (*id < NR_CPUS)
  571. cpu_data(*id).proc_id = proc_id;
  572. }
  573. }
  574. static void __cpuinit __set_proc_ids(struct mdesc_handle *hp, const char *exec_unit_name)
  575. {
  576. int idx;
  577. u64 mp;
  578. idx = 0;
  579. mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
  580. const char *type;
  581. int len;
  582. type = mdesc_get_property(hp, mp, "type", &len);
  583. if (!of_find_in_proplist(type, "int", len) &&
  584. !of_find_in_proplist(type, "integer", len))
  585. continue;
  586. mark_proc_ids(hp, mp, idx);
  587. idx++;
  588. }
  589. }
  590. static void __cpuinit set_proc_ids(struct mdesc_handle *hp)
  591. {
  592. __set_proc_ids(hp, "exec_unit");
  593. __set_proc_ids(hp, "exec-unit");
  594. }
  595. static void __cpuinit get_one_mondo_bits(const u64 *p, unsigned int *mask,
  596. unsigned char def)
  597. {
  598. u64 val;
  599. if (!p)
  600. goto use_default;
  601. val = *p;
  602. if (!val || val >= 64)
  603. goto use_default;
  604. *mask = ((1U << val) * 64U) - 1U;
  605. return;
  606. use_default:
  607. *mask = ((1U << def) * 64U) - 1U;
  608. }
  609. static void __cpuinit get_mondo_data(struct mdesc_handle *hp, u64 mp,
  610. struct trap_per_cpu *tb)
  611. {
  612. const u64 *val;
  613. val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
  614. get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7);
  615. val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
  616. get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7);
  617. val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
  618. get_one_mondo_bits(val, &tb->resum_qmask, 6);
  619. val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
  620. get_one_mondo_bits(val, &tb->nonresum_qmask, 2);
  621. }
  622. static void * __cpuinit mdesc_iterate_over_cpus(void *(*func)(struct mdesc_handle *, u64, int, void *), void *arg, cpumask_t *mask)
  623. {
  624. struct mdesc_handle *hp = mdesc_grab();
  625. void *ret = NULL;
  626. u64 mp;
  627. mdesc_for_each_node_by_name(hp, mp, "cpu") {
  628. const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
  629. int cpuid = *id;
  630. #ifdef CONFIG_SMP
  631. if (cpuid >= NR_CPUS) {
  632. printk(KERN_WARNING "Ignoring CPU %d which is "
  633. ">= NR_CPUS (%d)\n",
  634. cpuid, NR_CPUS);
  635. continue;
  636. }
  637. if (!cpu_isset(cpuid, *mask))
  638. continue;
  639. #endif
  640. ret = func(hp, mp, cpuid, arg);
  641. if (ret)
  642. goto out;
  643. }
  644. out:
  645. mdesc_release(hp);
  646. return ret;
  647. }
  648. static void * __cpuinit record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
  649. {
  650. ncpus_probed++;
  651. #ifdef CONFIG_SMP
  652. set_cpu_present(cpuid, true);
  653. #endif
  654. return NULL;
  655. }
  656. void __cpuinit mdesc_populate_present_mask(cpumask_t *mask)
  657. {
  658. if (tlb_type != hypervisor)
  659. return;
  660. ncpus_probed = 0;
  661. mdesc_iterate_over_cpus(record_one_cpu, NULL, mask);
  662. }
  663. static void * __cpuinit fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
  664. {
  665. const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
  666. struct trap_per_cpu *tb;
  667. cpuinfo_sparc *c;
  668. u64 a;
  669. #ifndef CONFIG_SMP
  670. /* On uniprocessor we only want the values for the
  671. * real physical cpu the kernel booted onto, however
  672. * cpu_data() only has one entry at index 0.
  673. */
  674. if (cpuid != real_hard_smp_processor_id())
  675. return NULL;
  676. cpuid = 0;
  677. #endif
  678. c = &cpu_data(cpuid);
  679. c->clock_tick = *cfreq;
  680. tb = &trap_block[cpuid];
  681. get_mondo_data(hp, mp, tb);
  682. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  683. u64 j, t = mdesc_arc_target(hp, a);
  684. const char *t_name;
  685. t_name = mdesc_node_name(hp, t);
  686. if (!strcmp(t_name, "cache")) {
  687. fill_in_one_cache(c, hp, t);
  688. continue;
  689. }
  690. mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
  691. u64 n = mdesc_arc_target(hp, j);
  692. const char *n_name;
  693. n_name = mdesc_node_name(hp, n);
  694. if (!strcmp(n_name, "cache"))
  695. fill_in_one_cache(c, hp, n);
  696. }
  697. }
  698. c->core_id = 0;
  699. c->proc_id = -1;
  700. return NULL;
  701. }
  702. void __cpuinit mdesc_fill_in_cpu_data(cpumask_t *mask)
  703. {
  704. struct mdesc_handle *hp;
  705. mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask);
  706. #ifdef CONFIG_SMP
  707. sparc64_multi_core = 1;
  708. #endif
  709. hp = mdesc_grab();
  710. set_core_ids(hp);
  711. set_proc_ids(hp);
  712. mdesc_release(hp);
  713. smp_fill_in_sib_core_maps();
  714. }
  715. static ssize_t mdesc_read(struct file *file, char __user *buf,
  716. size_t len, loff_t *offp)
  717. {
  718. struct mdesc_handle *hp = mdesc_grab();
  719. int err;
  720. if (!hp)
  721. return -ENODEV;
  722. err = hp->handle_size;
  723. if (len < hp->handle_size)
  724. err = -EMSGSIZE;
  725. else if (copy_to_user(buf, &hp->mdesc, hp->handle_size))
  726. err = -EFAULT;
  727. mdesc_release(hp);
  728. return err;
  729. }
  730. static const struct file_operations mdesc_fops = {
  731. .read = mdesc_read,
  732. .owner = THIS_MODULE,
  733. };
  734. static struct miscdevice mdesc_misc = {
  735. .minor = MISC_DYNAMIC_MINOR,
  736. .name = "mdesc",
  737. .fops = &mdesc_fops,
  738. };
  739. static int __init mdesc_misc_init(void)
  740. {
  741. return misc_register(&mdesc_misc);
  742. }
  743. __initcall(mdesc_misc_init);
  744. void __init sun4v_mdesc_init(void)
  745. {
  746. struct mdesc_handle *hp;
  747. unsigned long len, real_len, status;
  748. (void) sun4v_mach_desc(0UL, 0UL, &len);
  749. printk("MDESC: Size is %lu bytes.\n", len);
  750. hp = mdesc_alloc(len, &lmb_mdesc_ops);
  751. if (hp == NULL) {
  752. prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
  753. prom_halt();
  754. }
  755. status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
  756. if (status != HV_EOK || real_len > len) {
  757. prom_printf("sun4v_mach_desc fails, err(%lu), "
  758. "len(%lu), real_len(%lu)\n",
  759. status, len, real_len);
  760. mdesc_free(hp);
  761. prom_halt();
  762. }
  763. cur_mdesc = hp;
  764. report_platform_properties();
  765. }