mdesc.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* mdesc.c: Sun4V machine description handling.
  3. *
  4. * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/types.h>
  8. #include <linux/memblock.h>
  9. #include <linux/log2.h>
  10. #include <linux/list.h>
  11. #include <linux/slab.h>
  12. #include <linux/mm.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/export.h>
  16. #include <asm/cpudata.h>
  17. #include <asm/hypervisor.h>
  18. #include <asm/mdesc.h>
  19. #include <asm/prom.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/oplib.h>
  22. #include <asm/smp.h>
  23. /* Unlike the OBP device tree, the machine description is a full-on
  24. * DAG. An arbitrary number of ARCs are possible from one
  25. * node to other nodes and thus we can't use the OBP device_node
  26. * data structure to represent these nodes inside of the kernel.
  27. *
  28. * Actually, it isn't even a DAG, because there are back pointers
  29. * which create cycles in the graph.
  30. *
  31. * mdesc_hdr and mdesc_elem describe the layout of the data structure
  32. * we get from the Hypervisor.
  33. */
  34. struct mdesc_hdr {
  35. u32 version; /* Transport version */
  36. u32 node_sz; /* node block size */
  37. u32 name_sz; /* name block size */
  38. u32 data_sz; /* data block size */
  39. } __attribute__((aligned(16)));
  40. struct mdesc_elem {
  41. u8 tag;
  42. #define MD_LIST_END 0x00
  43. #define MD_NODE 0x4e
  44. #define MD_NODE_END 0x45
  45. #define MD_NOOP 0x20
  46. #define MD_PROP_ARC 0x61
  47. #define MD_PROP_VAL 0x76
  48. #define MD_PROP_STR 0x73
  49. #define MD_PROP_DATA 0x64
  50. u8 name_len;
  51. u16 resv;
  52. u32 name_offset;
  53. union {
  54. struct {
  55. u32 data_len;
  56. u32 data_offset;
  57. } data;
  58. u64 val;
  59. } d;
  60. };
  61. struct mdesc_mem_ops {
  62. struct mdesc_handle *(*alloc)(unsigned int mdesc_size);
  63. void (*free)(struct mdesc_handle *handle);
  64. };
  65. struct mdesc_handle {
  66. struct list_head list;
  67. struct mdesc_mem_ops *mops;
  68. void *self_base;
  69. atomic_t refcnt;
  70. unsigned int handle_size;
  71. struct mdesc_hdr mdesc;
  72. };
  73. typedef int (*mdesc_node_info_get_f)(struct mdesc_handle *, u64,
  74. union md_node_info *);
  75. typedef void (*mdesc_node_info_rel_f)(union md_node_info *);
  76. typedef bool (*mdesc_node_match_f)(union md_node_info *, union md_node_info *);
  77. struct md_node_ops {
  78. char *name;
  79. mdesc_node_info_get_f get_info;
  80. mdesc_node_info_rel_f rel_info;
  81. mdesc_node_match_f node_match;
  82. };
  83. static int get_vdev_port_node_info(struct mdesc_handle *md, u64 node,
  84. union md_node_info *node_info);
  85. static void rel_vdev_port_node_info(union md_node_info *node_info);
  86. static bool vdev_port_node_match(union md_node_info *a_node_info,
  87. union md_node_info *b_node_info);
  88. static int get_ds_port_node_info(struct mdesc_handle *md, u64 node,
  89. union md_node_info *node_info);
  90. static void rel_ds_port_node_info(union md_node_info *node_info);
  91. static bool ds_port_node_match(union md_node_info *a_node_info,
  92. union md_node_info *b_node_info);
  93. /* supported node types which can be registered */
  94. static struct md_node_ops md_node_ops_table[] = {
  95. {"virtual-device-port", get_vdev_port_node_info,
  96. rel_vdev_port_node_info, vdev_port_node_match},
  97. {"domain-services-port", get_ds_port_node_info,
  98. rel_ds_port_node_info, ds_port_node_match},
  99. {NULL, NULL, NULL, NULL}
  100. };
  101. static void mdesc_get_node_ops(const char *node_name,
  102. mdesc_node_info_get_f *get_info_f,
  103. mdesc_node_info_rel_f *rel_info_f,
  104. mdesc_node_match_f *match_f)
  105. {
  106. int i;
  107. if (get_info_f)
  108. *get_info_f = NULL;
  109. if (rel_info_f)
  110. *rel_info_f = NULL;
  111. if (match_f)
  112. *match_f = NULL;
  113. if (!node_name)
  114. return;
  115. for (i = 0; md_node_ops_table[i].name != NULL; i++) {
  116. if (strcmp(md_node_ops_table[i].name, node_name) == 0) {
  117. if (get_info_f)
  118. *get_info_f = md_node_ops_table[i].get_info;
  119. if (rel_info_f)
  120. *rel_info_f = md_node_ops_table[i].rel_info;
  121. if (match_f)
  122. *match_f = md_node_ops_table[i].node_match;
  123. break;
  124. }
  125. }
  126. }
  127. static void mdesc_handle_init(struct mdesc_handle *hp,
  128. unsigned int handle_size,
  129. void *base)
  130. {
  131. BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1));
  132. memset(hp, 0, handle_size);
  133. INIT_LIST_HEAD(&hp->list);
  134. hp->self_base = base;
  135. atomic_set(&hp->refcnt, 1);
  136. hp->handle_size = handle_size;
  137. }
  138. static struct mdesc_handle * __init mdesc_memblock_alloc(unsigned int mdesc_size)
  139. {
  140. unsigned int handle_size, alloc_size;
  141. struct mdesc_handle *hp;
  142. unsigned long paddr;
  143. handle_size = (sizeof(struct mdesc_handle) -
  144. sizeof(struct mdesc_hdr) +
  145. mdesc_size);
  146. alloc_size = PAGE_ALIGN(handle_size);
  147. paddr = memblock_alloc(alloc_size, PAGE_SIZE);
  148. hp = NULL;
  149. if (paddr) {
  150. hp = __va(paddr);
  151. mdesc_handle_init(hp, handle_size, hp);
  152. }
  153. return hp;
  154. }
  155. static void __init mdesc_memblock_free(struct mdesc_handle *hp)
  156. {
  157. unsigned int alloc_size;
  158. unsigned long start;
  159. BUG_ON(atomic_read(&hp->refcnt) != 0);
  160. BUG_ON(!list_empty(&hp->list));
  161. alloc_size = PAGE_ALIGN(hp->handle_size);
  162. start = __pa(hp);
  163. free_bootmem_late(start, alloc_size);
  164. }
  165. static struct mdesc_mem_ops memblock_mdesc_ops = {
  166. .alloc = mdesc_memblock_alloc,
  167. .free = mdesc_memblock_free,
  168. };
  169. static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size)
  170. {
  171. unsigned int handle_size;
  172. struct mdesc_handle *hp;
  173. unsigned long addr;
  174. void *base;
  175. handle_size = (sizeof(struct mdesc_handle) -
  176. sizeof(struct mdesc_hdr) +
  177. mdesc_size);
  178. base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  179. if (!base)
  180. return NULL;
  181. addr = (unsigned long)base;
  182. addr = (addr + 15UL) & ~15UL;
  183. hp = (struct mdesc_handle *) addr;
  184. mdesc_handle_init(hp, handle_size, base);
  185. return hp;
  186. }
  187. static void mdesc_kfree(struct mdesc_handle *hp)
  188. {
  189. BUG_ON(atomic_read(&hp->refcnt) != 0);
  190. BUG_ON(!list_empty(&hp->list));
  191. kfree(hp->self_base);
  192. }
  193. static struct mdesc_mem_ops kmalloc_mdesc_memops = {
  194. .alloc = mdesc_kmalloc,
  195. .free = mdesc_kfree,
  196. };
  197. static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size,
  198. struct mdesc_mem_ops *mops)
  199. {
  200. struct mdesc_handle *hp = mops->alloc(mdesc_size);
  201. if (hp)
  202. hp->mops = mops;
  203. return hp;
  204. }
  205. static void mdesc_free(struct mdesc_handle *hp)
  206. {
  207. hp->mops->free(hp);
  208. }
  209. static struct mdesc_handle *cur_mdesc;
  210. static LIST_HEAD(mdesc_zombie_list);
  211. static DEFINE_SPINLOCK(mdesc_lock);
  212. struct mdesc_handle *mdesc_grab(void)
  213. {
  214. struct mdesc_handle *hp;
  215. unsigned long flags;
  216. spin_lock_irqsave(&mdesc_lock, flags);
  217. hp = cur_mdesc;
  218. if (hp)
  219. atomic_inc(&hp->refcnt);
  220. spin_unlock_irqrestore(&mdesc_lock, flags);
  221. return hp;
  222. }
  223. EXPORT_SYMBOL(mdesc_grab);
  224. void mdesc_release(struct mdesc_handle *hp)
  225. {
  226. unsigned long flags;
  227. spin_lock_irqsave(&mdesc_lock, flags);
  228. if (atomic_dec_and_test(&hp->refcnt)) {
  229. list_del_init(&hp->list);
  230. hp->mops->free(hp);
  231. }
  232. spin_unlock_irqrestore(&mdesc_lock, flags);
  233. }
  234. EXPORT_SYMBOL(mdesc_release);
  235. static DEFINE_MUTEX(mdesc_mutex);
  236. static struct mdesc_notifier_client *client_list;
  237. void mdesc_register_notifier(struct mdesc_notifier_client *client)
  238. {
  239. bool supported = false;
  240. u64 node;
  241. int i;
  242. mutex_lock(&mdesc_mutex);
  243. /* check to see if the node is supported for registration */
  244. for (i = 0; md_node_ops_table[i].name != NULL; i++) {
  245. if (strcmp(md_node_ops_table[i].name, client->node_name) == 0) {
  246. supported = true;
  247. break;
  248. }
  249. }
  250. if (!supported) {
  251. pr_err("MD: %s node not supported\n", client->node_name);
  252. mutex_unlock(&mdesc_mutex);
  253. return;
  254. }
  255. client->next = client_list;
  256. client_list = client;
  257. mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name)
  258. client->add(cur_mdesc, node, client->node_name);
  259. mutex_unlock(&mdesc_mutex);
  260. }
  261. static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node)
  262. {
  263. const u64 *id;
  264. u64 a;
  265. id = NULL;
  266. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  267. u64 target;
  268. target = mdesc_arc_target(hp, a);
  269. id = mdesc_get_property(hp, target,
  270. "cfg-handle", NULL);
  271. if (id)
  272. break;
  273. }
  274. return id;
  275. }
  276. static int get_vdev_port_node_info(struct mdesc_handle *md, u64 node,
  277. union md_node_info *node_info)
  278. {
  279. const u64 *parent_cfg_hdlp;
  280. const char *name;
  281. const u64 *idp;
  282. /*
  283. * Virtual device nodes are distinguished by:
  284. * 1. "id" property
  285. * 2. "name" property
  286. * 3. parent node "cfg-handle" property
  287. */
  288. idp = mdesc_get_property(md, node, "id", NULL);
  289. name = mdesc_get_property(md, node, "name", NULL);
  290. parent_cfg_hdlp = parent_cfg_handle(md, node);
  291. if (!idp || !name || !parent_cfg_hdlp)
  292. return -1;
  293. node_info->vdev_port.id = *idp;
  294. node_info->vdev_port.name = kstrdup_const(name, GFP_KERNEL);
  295. if (!node_info->vdev_port.name)
  296. return -1;
  297. node_info->vdev_port.parent_cfg_hdl = *parent_cfg_hdlp;
  298. return 0;
  299. }
  300. static void rel_vdev_port_node_info(union md_node_info *node_info)
  301. {
  302. if (node_info && node_info->vdev_port.name) {
  303. kfree_const(node_info->vdev_port.name);
  304. node_info->vdev_port.name = NULL;
  305. }
  306. }
  307. static bool vdev_port_node_match(union md_node_info *a_node_info,
  308. union md_node_info *b_node_info)
  309. {
  310. if (a_node_info->vdev_port.id != b_node_info->vdev_port.id)
  311. return false;
  312. if (a_node_info->vdev_port.parent_cfg_hdl !=
  313. b_node_info->vdev_port.parent_cfg_hdl)
  314. return false;
  315. if (strncmp(a_node_info->vdev_port.name,
  316. b_node_info->vdev_port.name, MDESC_MAX_STR_LEN) != 0)
  317. return false;
  318. return true;
  319. }
  320. static int get_ds_port_node_info(struct mdesc_handle *md, u64 node,
  321. union md_node_info *node_info)
  322. {
  323. const u64 *idp;
  324. /* DS port nodes use the "id" property to distinguish them */
  325. idp = mdesc_get_property(md, node, "id", NULL);
  326. if (!idp)
  327. return -1;
  328. node_info->ds_port.id = *idp;
  329. return 0;
  330. }
  331. static void rel_ds_port_node_info(union md_node_info *node_info)
  332. {
  333. }
  334. static bool ds_port_node_match(union md_node_info *a_node_info,
  335. union md_node_info *b_node_info)
  336. {
  337. if (a_node_info->ds_port.id != b_node_info->ds_port.id)
  338. return false;
  339. return true;
  340. }
  341. /* Run 'func' on nodes which are in A but not in B. */
  342. static void invoke_on_missing(const char *name,
  343. struct mdesc_handle *a,
  344. struct mdesc_handle *b,
  345. void (*func)(struct mdesc_handle *, u64,
  346. const char *node_name))
  347. {
  348. mdesc_node_info_get_f get_info_func;
  349. mdesc_node_info_rel_f rel_info_func;
  350. mdesc_node_match_f node_match_func;
  351. union md_node_info a_node_info;
  352. union md_node_info b_node_info;
  353. bool found;
  354. u64 a_node;
  355. u64 b_node;
  356. int rv;
  357. /*
  358. * Find the get_info, rel_info and node_match ops for the given
  359. * node name
  360. */
  361. mdesc_get_node_ops(name, &get_info_func, &rel_info_func,
  362. &node_match_func);
  363. /* If we didn't find a match, the node type is not supported */
  364. if (!get_info_func || !rel_info_func || !node_match_func) {
  365. pr_err("MD: %s node type is not supported\n", name);
  366. return;
  367. }
  368. mdesc_for_each_node_by_name(a, a_node, name) {
  369. found = false;
  370. rv = get_info_func(a, a_node, &a_node_info);
  371. if (rv != 0) {
  372. pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
  373. name);
  374. continue;
  375. }
  376. /* Check each node in B for node matching a_node */
  377. mdesc_for_each_node_by_name(b, b_node, name) {
  378. rv = get_info_func(b, b_node, &b_node_info);
  379. if (rv != 0)
  380. continue;
  381. if (node_match_func(&a_node_info, &b_node_info)) {
  382. found = true;
  383. rel_info_func(&b_node_info);
  384. break;
  385. }
  386. rel_info_func(&b_node_info);
  387. }
  388. rel_info_func(&a_node_info);
  389. if (!found)
  390. func(a, a_node, name);
  391. }
  392. }
  393. static void notify_one(struct mdesc_notifier_client *p,
  394. struct mdesc_handle *old_hp,
  395. struct mdesc_handle *new_hp)
  396. {
  397. invoke_on_missing(p->node_name, old_hp, new_hp, p->remove);
  398. invoke_on_missing(p->node_name, new_hp, old_hp, p->add);
  399. }
  400. static void mdesc_notify_clients(struct mdesc_handle *old_hp,
  401. struct mdesc_handle *new_hp)
  402. {
  403. struct mdesc_notifier_client *p = client_list;
  404. while (p) {
  405. notify_one(p, old_hp, new_hp);
  406. p = p->next;
  407. }
  408. }
  409. void mdesc_update(void)
  410. {
  411. unsigned long len, real_len, status;
  412. struct mdesc_handle *hp, *orig_hp;
  413. unsigned long flags;
  414. mutex_lock(&mdesc_mutex);
  415. (void) sun4v_mach_desc(0UL, 0UL, &len);
  416. hp = mdesc_alloc(len, &kmalloc_mdesc_memops);
  417. if (!hp) {
  418. printk(KERN_ERR "MD: mdesc alloc fails\n");
  419. goto out;
  420. }
  421. status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
  422. if (status != HV_EOK || real_len > len) {
  423. printk(KERN_ERR "MD: mdesc reread fails with %lu\n",
  424. status);
  425. atomic_dec(&hp->refcnt);
  426. mdesc_free(hp);
  427. goto out;
  428. }
  429. spin_lock_irqsave(&mdesc_lock, flags);
  430. orig_hp = cur_mdesc;
  431. cur_mdesc = hp;
  432. spin_unlock_irqrestore(&mdesc_lock, flags);
  433. mdesc_notify_clients(orig_hp, hp);
  434. spin_lock_irqsave(&mdesc_lock, flags);
  435. if (atomic_dec_and_test(&orig_hp->refcnt))
  436. mdesc_free(orig_hp);
  437. else
  438. list_add(&orig_hp->list, &mdesc_zombie_list);
  439. spin_unlock_irqrestore(&mdesc_lock, flags);
  440. out:
  441. mutex_unlock(&mdesc_mutex);
  442. }
  443. u64 mdesc_get_node(struct mdesc_handle *hp, const char *node_name,
  444. union md_node_info *node_info)
  445. {
  446. mdesc_node_info_get_f get_info_func;
  447. mdesc_node_info_rel_f rel_info_func;
  448. mdesc_node_match_f node_match_func;
  449. union md_node_info hp_node_info;
  450. u64 hp_node;
  451. int rv;
  452. if (hp == NULL || node_name == NULL || node_info == NULL)
  453. return MDESC_NODE_NULL;
  454. /* Find the ops for the given node name */
  455. mdesc_get_node_ops(node_name, &get_info_func, &rel_info_func,
  456. &node_match_func);
  457. /* If we didn't find ops for the given node name, it is not supported */
  458. if (!get_info_func || !rel_info_func || !node_match_func) {
  459. pr_err("MD: %s node is not supported\n", node_name);
  460. return -EINVAL;
  461. }
  462. mdesc_for_each_node_by_name(hp, hp_node, node_name) {
  463. rv = get_info_func(hp, hp_node, &hp_node_info);
  464. if (rv != 0)
  465. continue;
  466. if (node_match_func(node_info, &hp_node_info))
  467. break;
  468. rel_info_func(&hp_node_info);
  469. }
  470. rel_info_func(&hp_node_info);
  471. return hp_node;
  472. }
  473. EXPORT_SYMBOL(mdesc_get_node);
  474. int mdesc_get_node_info(struct mdesc_handle *hp, u64 node,
  475. const char *node_name, union md_node_info *node_info)
  476. {
  477. mdesc_node_info_get_f get_info_func;
  478. int rv;
  479. if (hp == NULL || node == MDESC_NODE_NULL ||
  480. node_name == NULL || node_info == NULL)
  481. return -EINVAL;
  482. /* Find the get_info op for the given node name */
  483. mdesc_get_node_ops(node_name, &get_info_func, NULL, NULL);
  484. /* If we didn't find a get_info_func, the node name is not supported */
  485. if (get_info_func == NULL) {
  486. pr_err("MD: %s node is not supported\n", node_name);
  487. return -EINVAL;
  488. }
  489. rv = get_info_func(hp, node, node_info);
  490. if (rv != 0) {
  491. pr_err("MD: Cannot find 1 or more required match properties for %s node.\n",
  492. node_name);
  493. return -1;
  494. }
  495. return 0;
  496. }
  497. EXPORT_SYMBOL(mdesc_get_node_info);
  498. static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
  499. {
  500. return (struct mdesc_elem *) (mdesc + 1);
  501. }
  502. static void *name_block(struct mdesc_hdr *mdesc)
  503. {
  504. return ((void *) node_block(mdesc)) + mdesc->node_sz;
  505. }
  506. static void *data_block(struct mdesc_hdr *mdesc)
  507. {
  508. return ((void *) name_block(mdesc)) + mdesc->name_sz;
  509. }
  510. u64 mdesc_node_by_name(struct mdesc_handle *hp,
  511. u64 from_node, const char *name)
  512. {
  513. struct mdesc_elem *ep = node_block(&hp->mdesc);
  514. const char *names = name_block(&hp->mdesc);
  515. u64 last_node = hp->mdesc.node_sz / 16;
  516. u64 ret;
  517. if (from_node == MDESC_NODE_NULL) {
  518. ret = from_node = 0;
  519. } else if (from_node >= last_node) {
  520. return MDESC_NODE_NULL;
  521. } else {
  522. ret = ep[from_node].d.val;
  523. }
  524. while (ret < last_node) {
  525. if (ep[ret].tag != MD_NODE)
  526. return MDESC_NODE_NULL;
  527. if (!strcmp(names + ep[ret].name_offset, name))
  528. break;
  529. ret = ep[ret].d.val;
  530. }
  531. if (ret >= last_node)
  532. ret = MDESC_NODE_NULL;
  533. return ret;
  534. }
  535. EXPORT_SYMBOL(mdesc_node_by_name);
  536. const void *mdesc_get_property(struct mdesc_handle *hp, u64 node,
  537. const char *name, int *lenp)
  538. {
  539. const char *names = name_block(&hp->mdesc);
  540. u64 last_node = hp->mdesc.node_sz / 16;
  541. void *data = data_block(&hp->mdesc);
  542. struct mdesc_elem *ep;
  543. if (node == MDESC_NODE_NULL || node >= last_node)
  544. return NULL;
  545. ep = node_block(&hp->mdesc) + node;
  546. ep++;
  547. for (; ep->tag != MD_NODE_END; ep++) {
  548. void *val = NULL;
  549. int len = 0;
  550. switch (ep->tag) {
  551. case MD_PROP_VAL:
  552. val = &ep->d.val;
  553. len = 8;
  554. break;
  555. case MD_PROP_STR:
  556. case MD_PROP_DATA:
  557. val = data + ep->d.data.data_offset;
  558. len = ep->d.data.data_len;
  559. break;
  560. default:
  561. break;
  562. }
  563. if (!val)
  564. continue;
  565. if (!strcmp(names + ep->name_offset, name)) {
  566. if (lenp)
  567. *lenp = len;
  568. return val;
  569. }
  570. }
  571. return NULL;
  572. }
  573. EXPORT_SYMBOL(mdesc_get_property);
  574. u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type)
  575. {
  576. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  577. const char *names = name_block(&hp->mdesc);
  578. u64 last_node = hp->mdesc.node_sz / 16;
  579. if (from == MDESC_NODE_NULL || from >= last_node)
  580. return MDESC_NODE_NULL;
  581. ep = base + from;
  582. ep++;
  583. for (; ep->tag != MD_NODE_END; ep++) {
  584. if (ep->tag != MD_PROP_ARC)
  585. continue;
  586. if (strcmp(names + ep->name_offset, arc_type))
  587. continue;
  588. return ep - base;
  589. }
  590. return MDESC_NODE_NULL;
  591. }
  592. EXPORT_SYMBOL(mdesc_next_arc);
  593. u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc)
  594. {
  595. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  596. ep = base + arc;
  597. return ep->d.val;
  598. }
  599. EXPORT_SYMBOL(mdesc_arc_target);
  600. const char *mdesc_node_name(struct mdesc_handle *hp, u64 node)
  601. {
  602. struct mdesc_elem *ep, *base = node_block(&hp->mdesc);
  603. const char *names = name_block(&hp->mdesc);
  604. u64 last_node = hp->mdesc.node_sz / 16;
  605. if (node == MDESC_NODE_NULL || node >= last_node)
  606. return NULL;
  607. ep = base + node;
  608. if (ep->tag != MD_NODE)
  609. return NULL;
  610. return names + ep->name_offset;
  611. }
  612. EXPORT_SYMBOL(mdesc_node_name);
  613. static u64 max_cpus = 64;
  614. static void __init report_platform_properties(void)
  615. {
  616. struct mdesc_handle *hp = mdesc_grab();
  617. u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
  618. const char *s;
  619. const u64 *v;
  620. if (pn == MDESC_NODE_NULL) {
  621. prom_printf("No platform node in machine-description.\n");
  622. prom_halt();
  623. }
  624. s = mdesc_get_property(hp, pn, "banner-name", NULL);
  625. printk("PLATFORM: banner-name [%s]\n", s);
  626. s = mdesc_get_property(hp, pn, "name", NULL);
  627. printk("PLATFORM: name [%s]\n", s);
  628. v = mdesc_get_property(hp, pn, "hostid", NULL);
  629. if (v)
  630. printk("PLATFORM: hostid [%08llx]\n", *v);
  631. v = mdesc_get_property(hp, pn, "serial#", NULL);
  632. if (v)
  633. printk("PLATFORM: serial# [%08llx]\n", *v);
  634. v = mdesc_get_property(hp, pn, "stick-frequency", NULL);
  635. printk("PLATFORM: stick-frequency [%08llx]\n", *v);
  636. v = mdesc_get_property(hp, pn, "mac-address", NULL);
  637. if (v)
  638. printk("PLATFORM: mac-address [%llx]\n", *v);
  639. v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL);
  640. if (v)
  641. printk("PLATFORM: watchdog-resolution [%llu ms]\n", *v);
  642. v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL);
  643. if (v)
  644. printk("PLATFORM: watchdog-max-timeout [%llu ms]\n", *v);
  645. v = mdesc_get_property(hp, pn, "max-cpus", NULL);
  646. if (v) {
  647. max_cpus = *v;
  648. printk("PLATFORM: max-cpus [%llu]\n", max_cpus);
  649. }
  650. #ifdef CONFIG_SMP
  651. {
  652. int max_cpu, i;
  653. if (v) {
  654. max_cpu = *v;
  655. if (max_cpu > NR_CPUS)
  656. max_cpu = NR_CPUS;
  657. } else {
  658. max_cpu = NR_CPUS;
  659. }
  660. for (i = 0; i < max_cpu; i++)
  661. set_cpu_possible(i, true);
  662. }
  663. #endif
  664. mdesc_release(hp);
  665. }
  666. static void fill_in_one_cache(cpuinfo_sparc *c, struct mdesc_handle *hp, u64 mp)
  667. {
  668. const u64 *level = mdesc_get_property(hp, mp, "level", NULL);
  669. const u64 *size = mdesc_get_property(hp, mp, "size", NULL);
  670. const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL);
  671. const char *type;
  672. int type_len;
  673. type = mdesc_get_property(hp, mp, "type", &type_len);
  674. switch (*level) {
  675. case 1:
  676. if (of_find_in_proplist(type, "instn", type_len)) {
  677. c->icache_size = *size;
  678. c->icache_line_size = *line_size;
  679. } else if (of_find_in_proplist(type, "data", type_len)) {
  680. c->dcache_size = *size;
  681. c->dcache_line_size = *line_size;
  682. }
  683. break;
  684. case 2:
  685. c->ecache_size = *size;
  686. c->ecache_line_size = *line_size;
  687. break;
  688. default:
  689. break;
  690. }
  691. if (*level == 1) {
  692. u64 a;
  693. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  694. u64 target = mdesc_arc_target(hp, a);
  695. const char *name = mdesc_node_name(hp, target);
  696. if (!strcmp(name, "cache"))
  697. fill_in_one_cache(c, hp, target);
  698. }
  699. }
  700. }
  701. static void find_back_node_value(struct mdesc_handle *hp, u64 node,
  702. char *srch_val,
  703. void (*func)(struct mdesc_handle *, u64, int),
  704. u64 val, int depth)
  705. {
  706. u64 arc;
  707. /* Since we have an estimate of recursion depth, do a sanity check. */
  708. if (depth == 0)
  709. return;
  710. mdesc_for_each_arc(arc, hp, node, MDESC_ARC_TYPE_BACK) {
  711. u64 n = mdesc_arc_target(hp, arc);
  712. const char *name = mdesc_node_name(hp, n);
  713. if (!strcmp(srch_val, name))
  714. (*func)(hp, n, val);
  715. find_back_node_value(hp, n, srch_val, func, val, depth-1);
  716. }
  717. }
  718. static void __mark_core_id(struct mdesc_handle *hp, u64 node,
  719. int core_id)
  720. {
  721. const u64 *id = mdesc_get_property(hp, node, "id", NULL);
  722. if (*id < num_possible_cpus())
  723. cpu_data(*id).core_id = core_id;
  724. }
  725. static void __mark_max_cache_id(struct mdesc_handle *hp, u64 node,
  726. int max_cache_id)
  727. {
  728. const u64 *id = mdesc_get_property(hp, node, "id", NULL);
  729. if (*id < num_possible_cpus()) {
  730. cpu_data(*id).max_cache_id = max_cache_id;
  731. /**
  732. * On systems without explicit socket descriptions socket
  733. * is max_cache_id
  734. */
  735. cpu_data(*id).sock_id = max_cache_id;
  736. }
  737. }
  738. static void mark_core_ids(struct mdesc_handle *hp, u64 mp,
  739. int core_id)
  740. {
  741. find_back_node_value(hp, mp, "cpu", __mark_core_id, core_id, 10);
  742. }
  743. static void mark_max_cache_ids(struct mdesc_handle *hp, u64 mp,
  744. int max_cache_id)
  745. {
  746. find_back_node_value(hp, mp, "cpu", __mark_max_cache_id,
  747. max_cache_id, 10);
  748. }
  749. static void set_core_ids(struct mdesc_handle *hp)
  750. {
  751. int idx;
  752. u64 mp;
  753. idx = 1;
  754. /* Identify unique cores by looking for cpus backpointed to by
  755. * level 1 instruction caches.
  756. */
  757. mdesc_for_each_node_by_name(hp, mp, "cache") {
  758. const u64 *level;
  759. const char *type;
  760. int len;
  761. level = mdesc_get_property(hp, mp, "level", NULL);
  762. if (*level != 1)
  763. continue;
  764. type = mdesc_get_property(hp, mp, "type", &len);
  765. if (!of_find_in_proplist(type, "instn", len))
  766. continue;
  767. mark_core_ids(hp, mp, idx);
  768. idx++;
  769. }
  770. }
  771. static int set_max_cache_ids_by_cache(struct mdesc_handle *hp, int level)
  772. {
  773. u64 mp;
  774. int idx = 1;
  775. int fnd = 0;
  776. /**
  777. * Identify unique highest level of shared cache by looking for cpus
  778. * backpointed to by shared level N caches.
  779. */
  780. mdesc_for_each_node_by_name(hp, mp, "cache") {
  781. const u64 *cur_lvl;
  782. cur_lvl = mdesc_get_property(hp, mp, "level", NULL);
  783. if (*cur_lvl != level)
  784. continue;
  785. mark_max_cache_ids(hp, mp, idx);
  786. idx++;
  787. fnd = 1;
  788. }
  789. return fnd;
  790. }
  791. static void set_sock_ids_by_socket(struct mdesc_handle *hp, u64 mp)
  792. {
  793. int idx = 1;
  794. mdesc_for_each_node_by_name(hp, mp, "socket") {
  795. u64 a;
  796. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  797. u64 t = mdesc_arc_target(hp, a);
  798. const char *name;
  799. const u64 *id;
  800. name = mdesc_node_name(hp, t);
  801. if (strcmp(name, "cpu"))
  802. continue;
  803. id = mdesc_get_property(hp, t, "id", NULL);
  804. if (*id < num_possible_cpus())
  805. cpu_data(*id).sock_id = idx;
  806. }
  807. idx++;
  808. }
  809. }
  810. static void set_sock_ids(struct mdesc_handle *hp)
  811. {
  812. u64 mp;
  813. /**
  814. * Find the highest level of shared cache which pre-T7 is also
  815. * the socket.
  816. */
  817. if (!set_max_cache_ids_by_cache(hp, 3))
  818. set_max_cache_ids_by_cache(hp, 2);
  819. /* If machine description exposes sockets data use it.*/
  820. mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "sockets");
  821. if (mp != MDESC_NODE_NULL)
  822. set_sock_ids_by_socket(hp, mp);
  823. }
  824. static void mark_proc_ids(struct mdesc_handle *hp, u64 mp, int proc_id)
  825. {
  826. u64 a;
  827. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
  828. u64 t = mdesc_arc_target(hp, a);
  829. const char *name;
  830. const u64 *id;
  831. name = mdesc_node_name(hp, t);
  832. if (strcmp(name, "cpu"))
  833. continue;
  834. id = mdesc_get_property(hp, t, "id", NULL);
  835. if (*id < NR_CPUS)
  836. cpu_data(*id).proc_id = proc_id;
  837. }
  838. }
  839. static void __set_proc_ids(struct mdesc_handle *hp, const char *exec_unit_name)
  840. {
  841. int idx;
  842. u64 mp;
  843. idx = 0;
  844. mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
  845. const char *type;
  846. int len;
  847. type = mdesc_get_property(hp, mp, "type", &len);
  848. if (!of_find_in_proplist(type, "int", len) &&
  849. !of_find_in_proplist(type, "integer", len))
  850. continue;
  851. mark_proc_ids(hp, mp, idx);
  852. idx++;
  853. }
  854. }
  855. static void set_proc_ids(struct mdesc_handle *hp)
  856. {
  857. __set_proc_ids(hp, "exec_unit");
  858. __set_proc_ids(hp, "exec-unit");
  859. }
  860. static void get_one_mondo_bits(const u64 *p, unsigned int *mask,
  861. unsigned long def, unsigned long max)
  862. {
  863. u64 val;
  864. if (!p)
  865. goto use_default;
  866. val = *p;
  867. if (!val || val >= 64)
  868. goto use_default;
  869. if (val > max)
  870. val = max;
  871. *mask = ((1U << val) * 64U) - 1U;
  872. return;
  873. use_default:
  874. *mask = ((1U << def) * 64U) - 1U;
  875. }
  876. static void get_mondo_data(struct mdesc_handle *hp, u64 mp,
  877. struct trap_per_cpu *tb)
  878. {
  879. static int printed;
  880. const u64 *val;
  881. val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
  882. get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7, ilog2(max_cpus * 2));
  883. val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
  884. get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7, 8);
  885. val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
  886. get_one_mondo_bits(val, &tb->resum_qmask, 6, 7);
  887. val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
  888. get_one_mondo_bits(val, &tb->nonresum_qmask, 2, 2);
  889. if (!printed++) {
  890. pr_info("SUN4V: Mondo queue sizes "
  891. "[cpu(%u) dev(%u) r(%u) nr(%u)]\n",
  892. tb->cpu_mondo_qmask + 1,
  893. tb->dev_mondo_qmask + 1,
  894. tb->resum_qmask + 1,
  895. tb->nonresum_qmask + 1);
  896. }
  897. }
  898. static void *mdesc_iterate_over_cpus(void *(*func)(struct mdesc_handle *, u64, int, void *), void *arg, cpumask_t *mask)
  899. {
  900. struct mdesc_handle *hp = mdesc_grab();
  901. void *ret = NULL;
  902. u64 mp;
  903. mdesc_for_each_node_by_name(hp, mp, "cpu") {
  904. const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
  905. int cpuid = *id;
  906. #ifdef CONFIG_SMP
  907. if (cpuid >= NR_CPUS) {
  908. printk(KERN_WARNING "Ignoring CPU %d which is "
  909. ">= NR_CPUS (%d)\n",
  910. cpuid, NR_CPUS);
  911. continue;
  912. }
  913. if (!cpumask_test_cpu(cpuid, mask))
  914. continue;
  915. #endif
  916. ret = func(hp, mp, cpuid, arg);
  917. if (ret)
  918. goto out;
  919. }
  920. out:
  921. mdesc_release(hp);
  922. return ret;
  923. }
  924. static void *record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
  925. void *arg)
  926. {
  927. ncpus_probed++;
  928. #ifdef CONFIG_SMP
  929. set_cpu_present(cpuid, true);
  930. #endif
  931. return NULL;
  932. }
  933. void mdesc_populate_present_mask(cpumask_t *mask)
  934. {
  935. if (tlb_type != hypervisor)
  936. return;
  937. ncpus_probed = 0;
  938. mdesc_iterate_over_cpus(record_one_cpu, NULL, mask);
  939. }
  940. static void * __init check_one_pgsz(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
  941. {
  942. const u64 *pgsz_prop = mdesc_get_property(hp, mp, "mmu-page-size-list", NULL);
  943. unsigned long *pgsz_mask = arg;
  944. u64 val;
  945. val = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
  946. HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
  947. if (pgsz_prop)
  948. val = *pgsz_prop;
  949. if (!*pgsz_mask)
  950. *pgsz_mask = val;
  951. else
  952. *pgsz_mask &= val;
  953. return NULL;
  954. }
  955. void __init mdesc_get_page_sizes(cpumask_t *mask, unsigned long *pgsz_mask)
  956. {
  957. *pgsz_mask = 0;
  958. mdesc_iterate_over_cpus(check_one_pgsz, pgsz_mask, mask);
  959. }
  960. static void *fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
  961. void *arg)
  962. {
  963. const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
  964. struct trap_per_cpu *tb;
  965. cpuinfo_sparc *c;
  966. u64 a;
  967. #ifndef CONFIG_SMP
  968. /* On uniprocessor we only want the values for the
  969. * real physical cpu the kernel booted onto, however
  970. * cpu_data() only has one entry at index 0.
  971. */
  972. if (cpuid != real_hard_smp_processor_id())
  973. return NULL;
  974. cpuid = 0;
  975. #endif
  976. c = &cpu_data(cpuid);
  977. c->clock_tick = *cfreq;
  978. tb = &trap_block[cpuid];
  979. get_mondo_data(hp, mp, tb);
  980. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  981. u64 j, t = mdesc_arc_target(hp, a);
  982. const char *t_name;
  983. t_name = mdesc_node_name(hp, t);
  984. if (!strcmp(t_name, "cache")) {
  985. fill_in_one_cache(c, hp, t);
  986. continue;
  987. }
  988. mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
  989. u64 n = mdesc_arc_target(hp, j);
  990. const char *n_name;
  991. n_name = mdesc_node_name(hp, n);
  992. if (!strcmp(n_name, "cache"))
  993. fill_in_one_cache(c, hp, n);
  994. }
  995. }
  996. c->core_id = 0;
  997. c->proc_id = -1;
  998. return NULL;
  999. }
  1000. void mdesc_fill_in_cpu_data(cpumask_t *mask)
  1001. {
  1002. struct mdesc_handle *hp;
  1003. mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask);
  1004. hp = mdesc_grab();
  1005. set_core_ids(hp);
  1006. set_proc_ids(hp);
  1007. set_sock_ids(hp);
  1008. mdesc_release(hp);
  1009. smp_fill_in_sib_core_maps();
  1010. }
  1011. /* mdesc_open() - Grab a reference to mdesc_handle when /dev/mdesc is
  1012. * opened. Hold this reference until /dev/mdesc is closed to ensure
  1013. * mdesc data structure is not released underneath us. Store the
  1014. * pointer to mdesc structure in private_data for read and seek to use
  1015. */
  1016. static int mdesc_open(struct inode *inode, struct file *file)
  1017. {
  1018. struct mdesc_handle *hp = mdesc_grab();
  1019. if (!hp)
  1020. return -ENODEV;
  1021. file->private_data = hp;
  1022. return 0;
  1023. }
  1024. static ssize_t mdesc_read(struct file *file, char __user *buf,
  1025. size_t len, loff_t *offp)
  1026. {
  1027. struct mdesc_handle *hp = file->private_data;
  1028. unsigned char *mdesc;
  1029. int bytes_left, count = len;
  1030. if (*offp >= hp->handle_size)
  1031. return 0;
  1032. bytes_left = hp->handle_size - *offp;
  1033. if (count > bytes_left)
  1034. count = bytes_left;
  1035. mdesc = (unsigned char *)&hp->mdesc;
  1036. mdesc += *offp;
  1037. if (!copy_to_user(buf, mdesc, count)) {
  1038. *offp += count;
  1039. return count;
  1040. } else {
  1041. return -EFAULT;
  1042. }
  1043. }
  1044. static loff_t mdesc_llseek(struct file *file, loff_t offset, int whence)
  1045. {
  1046. struct mdesc_handle *hp = file->private_data;
  1047. return no_seek_end_llseek_size(file, offset, whence, hp->handle_size);
  1048. }
  1049. /* mdesc_close() - /dev/mdesc is being closed, release the reference to
  1050. * mdesc structure.
  1051. */
  1052. static int mdesc_close(struct inode *inode, struct file *file)
  1053. {
  1054. mdesc_release(file->private_data);
  1055. return 0;
  1056. }
  1057. static const struct file_operations mdesc_fops = {
  1058. .open = mdesc_open,
  1059. .read = mdesc_read,
  1060. .llseek = mdesc_llseek,
  1061. .release = mdesc_close,
  1062. .owner = THIS_MODULE,
  1063. };
  1064. static struct miscdevice mdesc_misc = {
  1065. .minor = MISC_DYNAMIC_MINOR,
  1066. .name = "mdesc",
  1067. .fops = &mdesc_fops,
  1068. };
  1069. static int __init mdesc_misc_init(void)
  1070. {
  1071. return misc_register(&mdesc_misc);
  1072. }
  1073. __initcall(mdesc_misc_init);
  1074. void __init sun4v_mdesc_init(void)
  1075. {
  1076. struct mdesc_handle *hp;
  1077. unsigned long len, real_len, status;
  1078. (void) sun4v_mach_desc(0UL, 0UL, &len);
  1079. printk("MDESC: Size is %lu bytes.\n", len);
  1080. hp = mdesc_alloc(len, &memblock_mdesc_ops);
  1081. if (hp == NULL) {
  1082. prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
  1083. prom_halt();
  1084. }
  1085. status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
  1086. if (status != HV_EOK || real_len > len) {
  1087. prom_printf("sun4v_mach_desc fails, err(%lu), "
  1088. "len(%lu), real_len(%lu)\n",
  1089. status, len, real_len);
  1090. mdesc_free(hp);
  1091. prom_halt();
  1092. }
  1093. cur_mdesc = hp;
  1094. report_platform_properties();
  1095. }