mdesc.c 20 KB

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