mdesc.c 19 KB

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