mdesc.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. struct mdesc_handle *hp;
  118. unsigned long addr;
  119. void *base;
  120. handle_size = (sizeof(struct mdesc_handle) -
  121. sizeof(struct mdesc_hdr) +
  122. mdesc_size);
  123. /*
  124. * Allocation has to succeed because mdesc update would be missed
  125. * and such events are not retransmitted.
  126. */
  127. base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_NOFAIL);
  128. addr = (unsigned long)base;
  129. addr = (addr + 15UL) & ~15UL;
  130. hp = (struct mdesc_handle *) addr;
  131. mdesc_handle_init(hp, handle_size, base);
  132. return hp;
  133. }
  134. static void mdesc_kfree(struct mdesc_handle *hp)
  135. {
  136. BUG_ON(atomic_read(&hp->refcnt) != 0);
  137. BUG_ON(!list_empty(&hp->list));
  138. kfree(hp->self_base);
  139. }
  140. static struct mdesc_mem_ops kmalloc_mdesc_memops = {
  141. .alloc = mdesc_kmalloc,
  142. .free = mdesc_kfree,
  143. };
  144. static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
  145. struct mdesc_mem_ops *mops)
  146. {
  147. struct mdesc_handle *hp = mops->alloc(mdesc_size);
  148. if (hp)
  149. hp->mops = mops;
  150. return hp;
  151. }
  152. static void mdesc_free(struct mdesc_handle *hp)
  153. {
  154. hp->mops->free(hp);
  155. }
  156. static struct mdesc_handle *cur_mdesc;
  157. static LIST_HEAD(mdesc_zombie_list);
  158. static DEFINE_SPINLOCK(mdesc_lock);
  159. struct mdesc_handle *mdesc_grab(void)
  160. {
  161. struct mdesc_handle *hp;
  162. unsigned long flags;
  163. spin_lock_irqsave(&mdesc_lock, flags);
  164. hp = cur_mdesc;
  165. if (hp)
  166. atomic_inc(&hp->refcnt);
  167. spin_unlock_irqrestore(&mdesc_lock, flags);
  168. return hp;
  169. }
  170. EXPORT_SYMBOL(mdesc_grab);
  171. void mdesc_release(struct mdesc_handle *hp)
  172. {
  173. unsigned long flags;
  174. spin_lock_irqsave(&mdesc_lock, flags);
  175. if (atomic_dec_and_test(&hp->refcnt)) {
  176. list_del_init(&hp->list);
  177. hp->mops->free(hp);
  178. }
  179. spin_unlock_irqrestore(&mdesc_lock, flags);
  180. }
  181. EXPORT_SYMBOL(mdesc_release);
  182. static DEFINE_MUTEX(mdesc_mutex);
  183. static struct mdesc_notifier_client *client_list;
  184. void mdesc_register_notifier(struct mdesc_notifier_client *client)
  185. {
  186. u64 node;
  187. mutex_lock(&mdesc_mutex);
  188. client->next = client_list;
  189. client_list = client;
  190. mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
  191. client->add(cur_mdesc, node);
  192. mutex_unlock(&mdesc_mutex);
  193. }
  194. static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
  195. {
  196. const u64 *id;
  197. u64 a;
  198. id = NULL;
  199. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  200. u64 target;
  201. target = mdesc_arc_target(hp, a);
  202. id = mdesc_get_property(hp, target,
  203. "cfg-handle", NULL);
  204. if (id)
  205. break;
  206. }
  207. return id;
  208. }
  209. /* Run 'func' on nodes which are in A but not in B. */
  210. static void invoke_on_missing(const char *name,
  211. struct mdesc_handle *a,
  212. struct mdesc_handle *b,
  213. void (*func)(struct mdesc_handle *, u64))
  214. {
  215. u64 node;
  216. mdesc_for_each_node_by_name(a, node, name) {
  217. int found = 0, is_vdc_port = 0;
  218. const char *name_prop;
  219. const u64 *id;
  220. u64 fnode;
  221. name_prop = mdesc_get_property(a, node, "name", NULL);
  222. if (name_prop && !strcmp(name_prop, "vdc-port")) {
  223. is_vdc_port = 1;
  224. id = parent_cfg_handle(a, node);
  225. } else
  226. id = mdesc_get_property(a, node, "id", NULL);
  227. if (!id) {
  228. printk(KERN_ERR "MD: Cannot find ID for %s node.\n",
  229. (name_prop ? name_prop : name));
  230. continue;
  231. }
  232. mdesc_for_each_node_by_name(b, fnode, name) {
  233. const u64 *fid;
  234. if (is_vdc_port) {
  235. name_prop = mdesc_get_property(b, fnode,
  236. "name", NULL);
  237. if (!name_prop ||
  238. strcmp(name_prop, "vdc-port"))
  239. continue;
  240. fid = parent_cfg_handle(b, fnode);
  241. if (!fid) {
  242. printk(KERN_ERR "MD: Cannot find ID "
  243. "for vdc-port node.\n");
  244. continue;
  245. }
  246. } else
  247. fid = mdesc_get_property(b, fnode,
  248. "id", NULL);
  249. if (*id == *fid) {
  250. found = 1;
  251. break;
  252. }
  253. }
  254. if (!found)
  255. func(a, node);
  256. }
  257. }
  258. static void notify_one(struct mdesc_notifier_client *p,
  259. struct mdesc_handle *old_hp,
  260. struct mdesc_handle *new_hp)
  261. {
  262. invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
  263. invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
  264. }
  265. static void mdesc_notify_clients(struct mdesc_handle *old_hp,
  266. struct mdesc_handle *new_hp)
  267. {
  268. struct mdesc_notifier_client *p = client_list;
  269. while (p) {
  270. notify_one(p, old_hp, new_hp);
  271. p = p->next;
  272. }
  273. }
  274. void mdesc_update(void)
  275. {
  276. unsigned long len, real_len, status;
  277. struct mdesc_handle *hp, *orig_hp;
  278. unsigned long flags;
  279. mutex_lock(&mdesc_mutex);
  280. (void) sun4v_mach_desc(0UL, 0UL, &len);
  281. hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
  282. if (!hp) {
  283. printk(KERN_ERR "MD: mdesc alloc fails\n");
  284. goto out;
  285. }
  286. status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
  287. if (status != HV_EOK || real_len > len) {
  288. printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
  289. status);
  290. atomic_dec(&hp->refcnt);
  291. mdesc_free(hp);
  292. goto out;
  293. }
  294. spin_lock_irqsave(&mdesc_lock, flags);
  295. orig_hp = cur_mdesc;
  296. cur_mdesc = hp;
  297. spin_unlock_irqrestore(&mdesc_lock, flags);
  298. mdesc_notify_clients(orig_hp, hp);
  299. spin_lock_irqsave(&mdesc_lock, flags);
  300. if (atomic_dec_and_test(&orig_hp->refcnt))
  301. mdesc_free(orig_hp);
  302. else
  303. list_add(&orig_hp->list, &mdesc_zombie_list);
  304. spin_unlock_irqrestore(&mdesc_lock, flags);
  305. out:
  306. mutex_unlock(&mdesc_mutex);
  307. }
  308. static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
  309. {
  310. return (struct mdesc_elem *) (mdesc + 1);
  311. }
  312. static void *name_block(struct mdesc_hdr *mdesc)
  313. {
  314. return ((void *) node_block(mdesc)) + mdesc->node_sz;
  315. }
  316. static void *data_block(struct mdesc_hdr *mdesc)
  317. {
  318. return ((void *) name_block(mdesc)) + mdesc->name_sz;
  319. }
  320. u64 mdesc_node_by_name(struct mdesc_handle *hp,
  321. u64 from_node, const char *name)
  322. {
  323. struct mdesc_elem *ep = node_block(&hp->mdesc);
  324. const char *names = name_block(&hp->mdesc);
  325. u64 last_node = hp->mdesc.node_sz / 16;
  326. u64 ret;
  327. if (from_node == MDESC_NODE_NULL) {
  328. ret = from_node = 0;
  329. } else if (from_node >= last_node) {
  330. return MDESC_NODE_NULL;
  331. } else {
  332. ret = ep[from_node].d.val;
  333. }
  334. while (ret < last_node) {
  335. if (ep[ret].tag != MD_NODE)
  336. return MDESC_NODE_NULL;
  337. if (!strcmp(names + ep[ret].name_offset, name))
  338. break;
  339. ret = ep[ret].d.val;
  340. }
  341. if (ret >= last_node)
  342. ret = MDESC_NODE_NULL;
  343. return ret;
  344. }
  345. EXPORT_SYMBOL(mdesc_node_by_name);
  346. const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
  347. const char *name, int *lenp)
  348. {
  349. const char *names = name_block(&hp->mdesc);
  350. u64 last_node = hp->mdesc.node_sz / 16;
  351. void *data = data_block(&hp->mdesc);
  352. struct mdesc_elem *ep;
  353. if (node == MDESC_NODE_NULL || node >= last_node)
  354. return NULL;
  355. ep = node_block(&hp->mdesc) + node;
  356. ep++;
  357. for (; ep->tag != MD_NODE_END; ep++) {
  358. void *val = NULL;
  359. int len = 0;
  360. switch (ep->tag) {
  361. case MD_PROP_VAL:
  362. val = &ep->d.val;
  363. len = 8;
  364. break;
  365. case MD_PROP_STR:
  366. case MD_PROP_DATA:
  367. val = data + ep->d.data.data_offset;
  368. len = ep->d.data.data_len;
  369. break;
  370. default:
  371. break;
  372. }
  373. if (!val)
  374. continue;
  375. if (!strcmp(names + ep->name_offset, name)) {
  376. if (lenp)
  377. *lenp = len;
  378. return val;
  379. }
  380. }
  381. return NULL;
  382. }
  383. EXPORT_SYMBOL(mdesc_get_property);
  384. u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
  385. {
  386. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  387. const char *names = name_block(&hp->mdesc);
  388. u64 last_node = hp->mdesc.node_sz / 16;
  389. if (from == MDESC_NODE_NULL || from >= last_node)
  390. return MDESC_NODE_NULL;
  391. ep = base + from;
  392. ep++;
  393. for (; ep->tag != MD_NODE_END; ep++) {
  394. if (ep->tag != MD_PROP_ARC)
  395. continue;
  396. if (strcmp(names + ep->name_offset, arc_type))
  397. continue;
  398. return ep - base;
  399. }
  400. return MDESC_NODE_NULL;
  401. }
  402. EXPORT_SYMBOL(mdesc_next_arc);
  403. u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
  404. {
  405. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  406. ep = base + arc;
  407. return ep->d.val;
  408. }
  409. EXPORT_SYMBOL(mdesc_arc_target);
  410. const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
  411. {
  412. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  413. const char *names = name_block(&hp->mdesc);
  414. u64 last_node = hp->mdesc.node_sz / 16;
  415. if (node == MDESC_NODE_NULL || node >= last_node)
  416. return NULL;
  417. ep = base + node;
  418. if (ep->tag != MD_NODE)
  419. return NULL;
  420. return names + ep->name_offset;
  421. }
  422. EXPORT_SYMBOL(mdesc_node_name);
  423. static u64 max_cpus = 64;
  424. static void __init report_platform_properties(void)
  425. {
  426. struct mdesc_handle *hp = mdesc_grab();
  427. u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
  428. const char *s;
  429. const u64 *v;
  430. if (pn == MDESC_NODE_NULL) {
  431. prom_printf("No platform node in machine-description.\n");
  432. prom_halt();
  433. }
  434. s = mdesc_get_property(hp, pn, "banner-name", NULL);
  435. printk("PLATFORM: banner-name [%s]\n", s);
  436. s = mdesc_get_property(hp, pn, "name", NULL);
  437. printk("PLATFORM: name [%s]\n", s);
  438. v = mdesc_get_property(hp, pn, "hostid", NULL);
  439. if (v)
  440. printk("PLATFORM: hostid [%08llx]\n", *v);
  441. v = mdesc_get_property(hp, pn, "serial#", NULL);
  442. if (v)
  443. printk("PLATFORM: serial# [%08llx]\n", *v);
  444. v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
  445. printk("PLATFORM: stick-frequency [%08llx]\n", *v);
  446. v = mdesc_get_property(hp, pn, "mac-address", NULL);
  447. if (v)
  448. printk("PLATFORM: mac-address [%llx]\n", *v);
  449. v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
  450. if (v)
  451. printk("PLATFORM: watchdog-resolution [%llu ms]\n", *v);
  452. v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
  453. if (v)
  454. printk("PLATFORM: watchdog-max-timeout [%llu ms]\n", *v);
  455. v = mdesc_get_property(hp, pn, "max-cpus", NULL);
  456. if (v) {
  457. max_cpus = *v;
  458. printk("PLATFORM: max-cpus [%llu]\n", max_cpus);
  459. }
  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 fill_in_one_cache(cpuinfo_sparc *c, struct mdesc_handle *hp, u64 mp)
  477. {
  478. const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
  479. const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
  480. const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
  481. const char *type;
  482. int type_len;
  483. type = mdesc_get_property(hp, mp, "type", &type_len);
  484. switch (*level) {
  485. case 1:
  486. if (of_find_in_proplist(type, "instn", type_len)) {
  487. c->icache_size = *size;
  488. c->icache_line_size = *line_size;
  489. } else if (of_find_in_proplist(type, "data", type_len)) {
  490. c->dcache_size = *size;
  491. c->dcache_line_size = *line_size;
  492. }
  493. break;
  494. case 2:
  495. c->ecache_size = *size;
  496. c->ecache_line_size = *line_size;
  497. break;
  498. default:
  499. break;
  500. }
  501. if (*level == 1) {
  502. u64 a;
  503. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  504. u64 target = mdesc_arc_target(hp, a);
  505. const char *name = mdesc_node_name(hp, target);
  506. if (!strcmp(name, "cache"))
  507. fill_in_one_cache(c, hp, target);
  508. }
  509. }
  510. }
  511. static void find_back_node_value(struct mdesc_handle *hp, u64 node,
  512. char *srch_val,
  513. void (*func)(struct mdesc_handle *, u64, int),
  514. u64 val, int depth)
  515. {
  516. u64 arc;
  517. /* Since we have an estimate of recursion depth, do a sanity check. */
  518. if (depth == 0)
  519. return;
  520. mdesc_for_each_arc(arc, hp, node, MDESC_ARC_TYPE_BACK) {
  521. u64 n = mdesc_arc_target(hp, arc);
  522. const char *name = mdesc_node_name(hp, n);
  523. if (!strcmp(srch_val, name))
  524. (*func)(hp, n, val);
  525. find_back_node_value(hp, n, srch_val, func, val, depth-1);
  526. }
  527. }
  528. static void __mark_core_id(struct mdesc_handle *hp, u64 node,
  529. int core_id)
  530. {
  531. const u64 *id = mdesc_get_property(hp, node, "id", NULL);
  532. if (*id < num_possible_cpus())
  533. cpu_data(*id).core_id = core_id;
  534. }
  535. static void __mark_max_cache_id(struct mdesc_handle *hp, u64 node,
  536. int max_cache_id)
  537. {
  538. const u64 *id = mdesc_get_property(hp, node, "id", NULL);
  539. if (*id < num_possible_cpus()) {
  540. cpu_data(*id).max_cache_id = max_cache_id;
  541. /**
  542. * On systems without explicit socket descriptions socket
  543. * is max_cache_id
  544. */
  545. cpu_data(*id).sock_id = max_cache_id;
  546. }
  547. }
  548. static void mark_core_ids(struct mdesc_handle *hp, u64 mp,
  549. int core_id)
  550. {
  551. find_back_node_value(hp, mp, "cpu", __mark_core_id, core_id, 10);
  552. }
  553. static void mark_max_cache_ids(struct mdesc_handle *hp, u64 mp,
  554. int max_cache_id)
  555. {
  556. find_back_node_value(hp, mp, "cpu", __mark_max_cache_id,
  557. max_cache_id, 10);
  558. }
  559. static void set_core_ids(struct mdesc_handle *hp)
  560. {
  561. int idx;
  562. u64 mp;
  563. idx = 1;
  564. /* Identify unique cores by looking for cpus backpointed to by
  565. * level 1 instruction caches.
  566. */
  567. mdesc_for_each_node_by_name(hp, mp, "cache") {
  568. const u64 *level;
  569. const char *type;
  570. int len;
  571. level = mdesc_get_property(hp, mp, "level", NULL);
  572. if (*level != 1)
  573. continue;
  574. type = mdesc_get_property(hp, mp, "type", &len);
  575. if (!of_find_in_proplist(type, "instn", len))
  576. continue;
  577. mark_core_ids(hp, mp, idx);
  578. idx++;
  579. }
  580. }
  581. static int set_max_cache_ids_by_cache(struct mdesc_handle *hp, int level)
  582. {
  583. u64 mp;
  584. int idx = 1;
  585. int fnd = 0;
  586. /**
  587. * Identify unique highest level of shared cache by looking for cpus
  588. * backpointed to by shared level N caches.
  589. */
  590. mdesc_for_each_node_by_name(hp, mp, "cache") {
  591. const u64 *cur_lvl;
  592. cur_lvl = mdesc_get_property(hp, mp, "level", NULL);
  593. if (*cur_lvl != level)
  594. continue;
  595. mark_max_cache_ids(hp, mp, idx);
  596. idx++;
  597. fnd = 1;
  598. }
  599. return fnd;
  600. }
  601. static void set_sock_ids_by_socket(struct mdesc_handle *hp, u64 mp)
  602. {
  603. int idx = 1;
  604. mdesc_for_each_node_by_name(hp, mp, "socket") {
  605. u64 a;
  606. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  607. u64 t = mdesc_arc_target(hp, a);
  608. const char *name;
  609. const u64 *id;
  610. name = mdesc_node_name(hp, t);
  611. if (strcmp(name, "cpu"))
  612. continue;
  613. id = mdesc_get_property(hp, t, "id", NULL);
  614. if (*id < num_possible_cpus())
  615. cpu_data(*id).sock_id = idx;
  616. }
  617. idx++;
  618. }
  619. }
  620. static void set_sock_ids(struct mdesc_handle *hp)
  621. {
  622. u64 mp;
  623. /**
  624. * Find the highest level of shared cache which pre-T7 is also
  625. * the socket.
  626. */
  627. if (!set_max_cache_ids_by_cache(hp, 3))
  628. set_max_cache_ids_by_cache(hp, 2);
  629. /* If machine description exposes sockets data use it.*/
  630. mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "sockets");
  631. if (mp != MDESC_NODE_NULL)
  632. set_sock_ids_by_socket(hp, mp);
  633. }
  634. static void mark_proc_ids(struct mdesc_handle *hp, u64 mp, int proc_id)
  635. {
  636. u64 a;
  637. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
  638. u64 t = mdesc_arc_target(hp, a);
  639. const char *name;
  640. const u64 *id;
  641. name = mdesc_node_name(hp, t);
  642. if (strcmp(name, "cpu"))
  643. continue;
  644. id = mdesc_get_property(hp, t, "id", NULL);
  645. if (*id < NR_CPUS)
  646. cpu_data(*id).proc_id = proc_id;
  647. }
  648. }
  649. static void __set_proc_ids(struct mdesc_handle *hp, const char *exec_unit_name)
  650. {
  651. int idx;
  652. u64 mp;
  653. idx = 0;
  654. mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
  655. const char *type;
  656. int len;
  657. type = mdesc_get_property(hp, mp, "type", &len);
  658. if (!of_find_in_proplist(type, "int", len) &&
  659. !of_find_in_proplist(type, "integer", len))
  660. continue;
  661. mark_proc_ids(hp, mp, idx);
  662. idx++;
  663. }
  664. }
  665. static void set_proc_ids(struct mdesc_handle *hp)
  666. {
  667. __set_proc_ids(hp, "exec_unit");
  668. __set_proc_ids(hp, "exec-unit");
  669. }
  670. static void get_one_mondo_bits(const u64 *p, unsigned int *mask,
  671. unsigned long def, unsigned long max)
  672. {
  673. u64 val;
  674. if (!p)
  675. goto use_default;
  676. val = *p;
  677. if (!val || val >= 64)
  678. goto use_default;
  679. if (val > max)
  680. val = max;
  681. *mask = ((1U << val) * 64U) - 1U;
  682. return;
  683. use_default:
  684. *mask = ((1U << def) * 64U) - 1U;
  685. }
  686. static void get_mondo_data(struct mdesc_handle *hp, u64 mp,
  687. struct trap_per_cpu *tb)
  688. {
  689. static int printed;
  690. const u64 *val;
  691. val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
  692. get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7, ilog2(max_cpus * 2));
  693. val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
  694. get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7, 8);
  695. val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
  696. get_one_mondo_bits(val, &tb->resum_qmask, 6, 7);
  697. val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
  698. get_one_mondo_bits(val, &tb->nonresum_qmask, 2, 2);
  699. if (!printed++) {
  700. pr_info("SUN4V: Mondo queue sizes "
  701. "[cpu(%u) dev(%u) r(%u) nr(%u)]\n",
  702. tb->cpu_mondo_qmask + 1,
  703. tb->dev_mondo_qmask + 1,
  704. tb->resum_qmask + 1,
  705. tb->nonresum_qmask + 1);
  706. }
  707. }
  708. static void *mdesc_iterate_over_cpus(void *(*func)(struct mdesc_handle *, u64, int, void *), void *arg, cpumask_t *mask)
  709. {
  710. struct mdesc_handle *hp = mdesc_grab();
  711. void *ret = NULL;
  712. u64 mp;
  713. mdesc_for_each_node_by_name(hp, mp, "cpu") {
  714. const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
  715. int cpuid = *id;
  716. #ifdef CONFIG_SMP
  717. if (cpuid >= NR_CPUS) {
  718. printk(KERN_WARNING "Ignoring CPU %d which is "
  719. ">= NR_CPUS (%d)\n",
  720. cpuid, NR_CPUS);
  721. continue;
  722. }
  723. if (!cpumask_test_cpu(cpuid, mask))
  724. continue;
  725. #endif
  726. ret = func(hp, mp, cpuid, arg);
  727. if (ret)
  728. goto out;
  729. }
  730. out:
  731. mdesc_release(hp);
  732. return ret;
  733. }
  734. static void *record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
  735. void *arg)
  736. {
  737. ncpus_probed++;
  738. #ifdef CONFIG_SMP
  739. set_cpu_present(cpuid, true);
  740. #endif
  741. return NULL;
  742. }
  743. void mdesc_populate_present_mask(cpumask_t *mask)
  744. {
  745. if (tlb_type != hypervisor)
  746. return;
  747. ncpus_probed = 0;
  748. mdesc_iterate_over_cpus(record_one_cpu, NULL, mask);
  749. }
  750. static void * __init check_one_pgsz(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
  751. {
  752. const u64 *pgsz_prop = mdesc_get_property(hp, mp, "mmu-page-size-list", NULL);
  753. unsigned long *pgsz_mask = arg;
  754. u64 val;
  755. val = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
  756. HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
  757. if (pgsz_prop)
  758. val = *pgsz_prop;
  759. if (!*pgsz_mask)
  760. *pgsz_mask = val;
  761. else
  762. *pgsz_mask &= val;
  763. return NULL;
  764. }
  765. void __init mdesc_get_page_sizes(cpumask_t *mask, unsigned long *pgsz_mask)
  766. {
  767. *pgsz_mask = 0;
  768. mdesc_iterate_over_cpus(check_one_pgsz, pgsz_mask, mask);
  769. }
  770. static void *fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
  771. void *arg)
  772. {
  773. const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
  774. struct trap_per_cpu *tb;
  775. cpuinfo_sparc *c;
  776. u64 a;
  777. #ifndef CONFIG_SMP
  778. /* On uniprocessor we only want the values for the
  779. * real physical cpu the kernel booted onto, however
  780. * cpu_data() only has one entry at index 0.
  781. */
  782. if (cpuid != real_hard_smp_processor_id())
  783. return NULL;
  784. cpuid = 0;
  785. #endif
  786. c = &cpu_data(cpuid);
  787. c->clock_tick = *cfreq;
  788. tb = &trap_block[cpuid];
  789. get_mondo_data(hp, mp, tb);
  790. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  791. u64 j, t = mdesc_arc_target(hp, a);
  792. const char *t_name;
  793. t_name = mdesc_node_name(hp, t);
  794. if (!strcmp(t_name, "cache")) {
  795. fill_in_one_cache(c, hp, t);
  796. continue;
  797. }
  798. mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
  799. u64 n = mdesc_arc_target(hp, j);
  800. const char *n_name;
  801. n_name = mdesc_node_name(hp, n);
  802. if (!strcmp(n_name, "cache"))
  803. fill_in_one_cache(c, hp, n);
  804. }
  805. }
  806. c->core_id = 0;
  807. c->proc_id = -1;
  808. return NULL;
  809. }
  810. void mdesc_fill_in_cpu_data(cpumask_t *mask)
  811. {
  812. struct mdesc_handle *hp;
  813. mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask);
  814. hp = mdesc_grab();
  815. set_core_ids(hp);
  816. set_proc_ids(hp);
  817. set_sock_ids(hp);
  818. mdesc_release(hp);
  819. smp_fill_in_sib_core_maps();
  820. }
  821. /* mdesc_open() - Grab a reference to mdesc_handle when /dev/mdesc is
  822. * opened. Hold this reference until /dev/mdesc is closed to ensure
  823. * mdesc data structure is not released underneath us. Store the
  824. * pointer to mdesc structure in private_data for read and seek to use
  825. */
  826. static int mdesc_open(struct inode *inode, struct file *file)
  827. {
  828. struct mdesc_handle *hp = mdesc_grab();
  829. if (!hp)
  830. return -ENODEV;
  831. file->private_data = hp;
  832. return 0;
  833. }
  834. static ssize_t mdesc_read(struct file *file, char __user *buf,
  835. size_t len, loff_t *offp)
  836. {
  837. struct mdesc_handle *hp = file->private_data;
  838. unsigned char *mdesc;
  839. int bytes_left, count = len;
  840. if (*offp >= hp->handle_size)
  841. return 0;
  842. bytes_left = hp->handle_size - *offp;
  843. if (count > bytes_left)
  844. count = bytes_left;
  845. mdesc = (unsigned char *)&hp->mdesc;
  846. mdesc += *offp;
  847. if (!copy_to_user(buf, mdesc, count)) {
  848. *offp += count;
  849. return count;
  850. } else {
  851. return -EFAULT;
  852. }
  853. }
  854. static loff_t mdesc_llseek(struct file *file, loff_t offset, int whence)
  855. {
  856. struct mdesc_handle *hp = file->private_data;
  857. return no_seek_end_llseek_size(file, offset, whence, hp->handle_size);
  858. }
  859. /* mdesc_close() - /dev/mdesc is being closed, release the reference to
  860. * mdesc structure.
  861. */
  862. static int mdesc_close(struct inode *inode, struct file *file)
  863. {
  864. mdesc_release(file->private_data);
  865. return 0;
  866. }
  867. static const struct file_operations mdesc_fops = {
  868. .open = mdesc_open,
  869. .read = mdesc_read,
  870. .llseek = mdesc_llseek,
  871. .release = mdesc_close,
  872. .owner = THIS_MODULE,
  873. };
  874. static struct miscdevice mdesc_misc = {
  875. .minor = MISC_DYNAMIC_MINOR,
  876. .name = "mdesc",
  877. .fops = &mdesc_fops,
  878. };
  879. static int __init mdesc_misc_init(void)
  880. {
  881. return misc_register(&mdesc_misc);
  882. }
  883. __initcall(mdesc_misc_init);
  884. void __init sun4v_mdesc_init(void)
  885. {
  886. struct mdesc_handle *hp;
  887. unsigned long len, real_len, status;
  888. (void) sun4v_mach_desc(0UL, 0UL, &len);
  889. printk("MDESC: Size is %lu bytes.\n", len);
  890. hp = mdesc_alloc(len, &memblock_mdesc_ops);
  891. if (hp == NULL) {
  892. prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
  893. prom_halt();
  894. }
  895. status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
  896. if (status != HV_EOK || real_len > len) {
  897. prom_printf("sun4v_mach_desc fails, err(%lu), "
  898. "len(%lu), real_len(%lu)\n",
  899. status, len, real_len);
  900. mdesc_free(hp);
  901. prom_halt();
  902. }
  903. cur_mdesc = hp;
  904. report_platform_properties();
  905. }