mdesc.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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 fill_in_one_cache(cpuinfo_sparc *c, struct mdesc_handle *hp, 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 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 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 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 __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 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 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 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 *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 *record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
  657. void *arg)
  658. {
  659. ncpus_probed++;
  660. #ifdef CONFIG_SMP
  661. set_cpu_present(cpuid, true);
  662. #endif
  663. return NULL;
  664. }
  665. void mdesc_populate_present_mask(cpumask_t *mask)
  666. {
  667. if (tlb_type != hypervisor)
  668. return;
  669. ncpus_probed = 0;
  670. mdesc_iterate_over_cpus(record_one_cpu, NULL, mask);
  671. }
  672. static void * __init check_one_pgsz(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
  673. {
  674. const u64 *pgsz_prop = mdesc_get_property(hp, mp, "mmu-page-size-list", NULL);
  675. unsigned long *pgsz_mask = arg;
  676. u64 val;
  677. val = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
  678. HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
  679. if (pgsz_prop)
  680. val = *pgsz_prop;
  681. if (!*pgsz_mask)
  682. *pgsz_mask = val;
  683. else
  684. *pgsz_mask &= val;
  685. return NULL;
  686. }
  687. void __init mdesc_get_page_sizes(cpumask_t *mask, unsigned long *pgsz_mask)
  688. {
  689. *pgsz_mask = 0;
  690. mdesc_iterate_over_cpus(check_one_pgsz, pgsz_mask, mask);
  691. }
  692. static void *fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
  693. void *arg)
  694. {
  695. const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
  696. struct trap_per_cpu *tb;
  697. cpuinfo_sparc *c;
  698. u64 a;
  699. #ifndef CONFIG_SMP
  700. /* On uniprocessor we only want the values for the
  701. * real physical cpu the kernel booted onto, however
  702. * cpu_data() only has one entry at index 0.
  703. */
  704. if (cpuid != real_hard_smp_processor_id())
  705. return NULL;
  706. cpuid = 0;
  707. #endif
  708. c = &cpu_data(cpuid);
  709. c->clock_tick = *cfreq;
  710. tb = &trap_block[cpuid];
  711. get_mondo_data(hp, mp, tb);
  712. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  713. u64 j, t = mdesc_arc_target(hp, a);
  714. const char *t_name;
  715. t_name = mdesc_node_name(hp, t);
  716. if (!strcmp(t_name, "cache")) {
  717. fill_in_one_cache(c, hp, t);
  718. continue;
  719. }
  720. mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
  721. u64 n = mdesc_arc_target(hp, j);
  722. const char *n_name;
  723. n_name = mdesc_node_name(hp, n);
  724. if (!strcmp(n_name, "cache"))
  725. fill_in_one_cache(c, hp, n);
  726. }
  727. }
  728. c->core_id = 0;
  729. c->proc_id = -1;
  730. return NULL;
  731. }
  732. void mdesc_fill_in_cpu_data(cpumask_t *mask)
  733. {
  734. struct mdesc_handle *hp;
  735. mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask);
  736. hp = mdesc_grab();
  737. set_core_ids(hp);
  738. set_proc_ids(hp);
  739. mdesc_release(hp);
  740. smp_fill_in_sib_core_maps();
  741. }
  742. /* mdesc_open() - Grab a reference to mdesc_handle when /dev/mdesc is
  743. * opened. Hold this reference until /dev/mdesc is closed to ensure
  744. * mdesc data structure is not released underneath us. Store the
  745. * pointer to mdesc structure in private_data for read and seek to use
  746. */
  747. static int mdesc_open(struct inode *inode, struct file *file)
  748. {
  749. struct mdesc_handle *hp = mdesc_grab();
  750. if (!hp)
  751. return -ENODEV;
  752. file->private_data = hp;
  753. return 0;
  754. }
  755. static ssize_t mdesc_read(struct file *file, char __user *buf,
  756. size_t len, loff_t *offp)
  757. {
  758. struct mdesc_handle *hp = file->private_data;
  759. unsigned char *mdesc;
  760. int bytes_left, count = len;
  761. if (*offp >= hp->handle_size)
  762. return 0;
  763. bytes_left = hp->handle_size - *offp;
  764. if (count > bytes_left)
  765. count = bytes_left;
  766. mdesc = (unsigned char *)&hp->mdesc;
  767. mdesc += *offp;
  768. if (!copy_to_user(buf, mdesc, count)) {
  769. *offp += count;
  770. return count;
  771. } else {
  772. return -EFAULT;
  773. }
  774. }
  775. static loff_t mdesc_llseek(struct file *file, loff_t offset, int whence)
  776. {
  777. struct mdesc_handle *hp;
  778. switch (whence) {
  779. case SEEK_CUR:
  780. offset += file->f_pos;
  781. break;
  782. case SEEK_SET:
  783. break;
  784. default:
  785. return -EINVAL;
  786. }
  787. hp = file->private_data;
  788. if (offset > hp->handle_size)
  789. return -EINVAL;
  790. else
  791. file->f_pos = offset;
  792. return offset;
  793. }
  794. /* mdesc_close() - /dev/mdesc is being closed, release the reference to
  795. * mdesc structure.
  796. */
  797. static int mdesc_close(struct inode *inode, struct file *file)
  798. {
  799. mdesc_release(file->private_data);
  800. return 0;
  801. }
  802. static const struct file_operations mdesc_fops = {
  803. .open = mdesc_open,
  804. .read = mdesc_read,
  805. .llseek = mdesc_llseek,
  806. .release = mdesc_close,
  807. .owner = THIS_MODULE,
  808. };
  809. static struct miscdevice mdesc_misc = {
  810. .minor = MISC_DYNAMIC_MINOR,
  811. .name = "mdesc",
  812. .fops = &mdesc_fops,
  813. };
  814. static int __init mdesc_misc_init(void)
  815. {
  816. return misc_register(&mdesc_misc);
  817. }
  818. __initcall(mdesc_misc_init);
  819. void __init sun4v_mdesc_init(void)
  820. {
  821. struct mdesc_handle *hp;
  822. unsigned long len, real_len, status;
  823. (void) sun4v_mach_desc(0UL, 0UL, &len);
  824. printk("MDESC: Size is %lu bytes.\n", len);
  825. hp = mdesc_alloc(len, &memblock_mdesc_ops);
  826. if (hp == NULL) {
  827. prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
  828. prom_halt();
  829. }
  830. status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
  831. if (status != HV_EOK || real_len > len) {
  832. prom_printf("sun4v_mach_desc fails, err(%lu), "
  833. "len(%lu), real_len(%lu)\n",
  834. status, len, real_len);
  835. mdesc_free(hp);
  836. prom_halt();
  837. }
  838. cur_mdesc = hp;
  839. report_platform_properties();
  840. }