mdesc.c 24 KB

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