mdesc.c 30 KB

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