mdesc.c 20 KB

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