mdesc.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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_sock_id(struct mdesc_handle *hp, u64 node,
  536. int sock_id)
  537. {
  538. const u64 *id = mdesc_get_property(hp, node, "id", NULL);
  539. if (*id < num_possible_cpus())
  540. cpu_data(*id).sock_id = sock_id;
  541. }
  542. static void mark_core_ids(struct mdesc_handle *hp, u64 mp,
  543. int core_id)
  544. {
  545. find_back_node_value(hp, mp, "cpu", __mark_core_id, core_id, 10);
  546. }
  547. static void mark_sock_ids(struct mdesc_handle *hp, u64 mp,
  548. int sock_id)
  549. {
  550. find_back_node_value(hp, mp, "cpu", __mark_sock_id, sock_id, 10);
  551. }
  552. static void set_core_ids(struct mdesc_handle *hp)
  553. {
  554. int idx;
  555. u64 mp;
  556. idx = 1;
  557. /* Identify unique cores by looking for cpus backpointed to by
  558. * level 1 instruction caches.
  559. */
  560. mdesc_for_each_node_by_name(hp, mp, "cache") {
  561. const u64 *level;
  562. const char *type;
  563. int len;
  564. level = mdesc_get_property(hp, mp, "level", NULL);
  565. if (*level != 1)
  566. continue;
  567. type = mdesc_get_property(hp, mp, "type", &len);
  568. if (!of_find_in_proplist(type, "instn", len))
  569. continue;
  570. mark_core_ids(hp, mp, idx);
  571. idx++;
  572. }
  573. }
  574. static int set_sock_ids_by_cache(struct mdesc_handle *hp, int level)
  575. {
  576. u64 mp;
  577. int idx = 1;
  578. int fnd = 0;
  579. /* Identify unique sockets by looking for cpus backpointed to by
  580. * shared level n caches.
  581. */
  582. mdesc_for_each_node_by_name(hp, mp, "cache") {
  583. const u64 *cur_lvl;
  584. cur_lvl = mdesc_get_property(hp, mp, "level", NULL);
  585. if (*cur_lvl != level)
  586. continue;
  587. mark_sock_ids(hp, mp, idx);
  588. idx++;
  589. fnd = 1;
  590. }
  591. return fnd;
  592. }
  593. static void set_sock_ids_by_socket(struct mdesc_handle *hp, u64 mp)
  594. {
  595. int idx = 1;
  596. mdesc_for_each_node_by_name(hp, mp, "socket") {
  597. u64 a;
  598. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  599. u64 t = mdesc_arc_target(hp, a);
  600. const char *name;
  601. const u64 *id;
  602. name = mdesc_node_name(hp, t);
  603. if (strcmp(name, "cpu"))
  604. continue;
  605. id = mdesc_get_property(hp, t, "id", NULL);
  606. if (*id < num_possible_cpus())
  607. cpu_data(*id).sock_id = idx;
  608. }
  609. idx++;
  610. }
  611. }
  612. static void set_sock_ids(struct mdesc_handle *hp)
  613. {
  614. u64 mp;
  615. /* If machine description exposes sockets data use it.
  616. * Otherwise fallback to use shared L3 or L2 caches.
  617. */
  618. mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "sockets");
  619. if (mp != MDESC_NODE_NULL)
  620. return set_sock_ids_by_socket(hp, mp);
  621. if (!set_sock_ids_by_cache(hp, 3))
  622. set_sock_ids_by_cache(hp, 2);
  623. }
  624. static void mark_proc_ids(struct mdesc_handle *hp, u64 mp, int proc_id)
  625. {
  626. u64 a;
  627. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
  628. u64 t = mdesc_arc_target(hp, a);
  629. const char *name;
  630. const u64 *id;
  631. name = mdesc_node_name(hp, t);
  632. if (strcmp(name, "cpu"))
  633. continue;
  634. id = mdesc_get_property(hp, t, "id", NULL);
  635. if (*id < NR_CPUS)
  636. cpu_data(*id).proc_id = proc_id;
  637. }
  638. }
  639. static void __set_proc_ids(struct mdesc_handle *hp, const char *exec_unit_name)
  640. {
  641. int idx;
  642. u64 mp;
  643. idx = 0;
  644. mdesc_for_each_node_by_name(hp, mp, exec_unit_name) {
  645. const char *type;
  646. int len;
  647. type = mdesc_get_property(hp, mp, "type", &len);
  648. if (!of_find_in_proplist(type, "int", len) &&
  649. !of_find_in_proplist(type, "integer", len))
  650. continue;
  651. mark_proc_ids(hp, mp, idx);
  652. idx++;
  653. }
  654. }
  655. static void set_proc_ids(struct mdesc_handle *hp)
  656. {
  657. __set_proc_ids(hp, "exec_unit");
  658. __set_proc_ids(hp, "exec-unit");
  659. }
  660. static void get_one_mondo_bits(const u64 *p, unsigned int *mask,
  661. unsigned long def, unsigned long max)
  662. {
  663. u64 val;
  664. if (!p)
  665. goto use_default;
  666. val = *p;
  667. if (!val || val >= 64)
  668. goto use_default;
  669. if (val > max)
  670. val = max;
  671. *mask = ((1U << val) * 64U) - 1U;
  672. return;
  673. use_default:
  674. *mask = ((1U << def) * 64U) - 1U;
  675. }
  676. static void get_mondo_data(struct mdesc_handle *hp, u64 mp,
  677. struct trap_per_cpu *tb)
  678. {
  679. static int printed;
  680. const u64 *val;
  681. val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL);
  682. get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7, ilog2(max_cpus * 2));
  683. val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL);
  684. get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7, 8);
  685. val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL);
  686. get_one_mondo_bits(val, &tb->resum_qmask, 6, 7);
  687. val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL);
  688. get_one_mondo_bits(val, &tb->nonresum_qmask, 2, 2);
  689. if (!printed++) {
  690. pr_info("SUN4V: Mondo queue sizes "
  691. "[cpu(%u) dev(%u) r(%u) nr(%u)]\n",
  692. tb->cpu_mondo_qmask + 1,
  693. tb->dev_mondo_qmask + 1,
  694. tb->resum_qmask + 1,
  695. tb->nonresum_qmask + 1);
  696. }
  697. }
  698. static void *mdesc_iterate_over_cpus(void *(*func)(struct mdesc_handle *, u64, int, void *), void *arg, cpumask_t *mask)
  699. {
  700. struct mdesc_handle *hp = mdesc_grab();
  701. void *ret = NULL;
  702. u64 mp;
  703. mdesc_for_each_node_by_name(hp, mp, "cpu") {
  704. const u64 *id = mdesc_get_property(hp, mp, "id", NULL);
  705. int cpuid = *id;
  706. #ifdef CONFIG_SMP
  707. if (cpuid >= NR_CPUS) {
  708. printk(KERN_WARNING "Ignoring CPU %d which is "
  709. ">= NR_CPUS (%d)\n",
  710. cpuid, NR_CPUS);
  711. continue;
  712. }
  713. if (!cpumask_test_cpu(cpuid, mask))
  714. continue;
  715. #endif
  716. ret = func(hp, mp, cpuid, arg);
  717. if (ret)
  718. goto out;
  719. }
  720. out:
  721. mdesc_release(hp);
  722. return ret;
  723. }
  724. static void *record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
  725. void *arg)
  726. {
  727. ncpus_probed++;
  728. #ifdef CONFIG_SMP
  729. set_cpu_present(cpuid, true);
  730. #endif
  731. return NULL;
  732. }
  733. void mdesc_populate_present_mask(cpumask_t *mask)
  734. {
  735. if (tlb_type != hypervisor)
  736. return;
  737. ncpus_probed = 0;
  738. mdesc_iterate_over_cpus(record_one_cpu, NULL, mask);
  739. }
  740. static void * __init check_one_pgsz(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg)
  741. {
  742. const u64 *pgsz_prop = mdesc_get_property(hp, mp, "mmu-page-size-list", NULL);
  743. unsigned long *pgsz_mask = arg;
  744. u64 val;
  745. val = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
  746. HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
  747. if (pgsz_prop)
  748. val = *pgsz_prop;
  749. if (!*pgsz_mask)
  750. *pgsz_mask = val;
  751. else
  752. *pgsz_mask &= val;
  753. return NULL;
  754. }
  755. void __init mdesc_get_page_sizes(cpumask_t *mask, unsigned long *pgsz_mask)
  756. {
  757. *pgsz_mask = 0;
  758. mdesc_iterate_over_cpus(check_one_pgsz, pgsz_mask, mask);
  759. }
  760. static void *fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid,
  761. void *arg)
  762. {
  763. const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL);
  764. struct trap_per_cpu *tb;
  765. cpuinfo_sparc *c;
  766. u64 a;
  767. #ifndef CONFIG_SMP
  768. /* On uniprocessor we only want the values for the
  769. * real physical cpu the kernel booted onto, however
  770. * cpu_data() only has one entry at index 0.
  771. */
  772. if (cpuid != real_hard_smp_processor_id())
  773. return NULL;
  774. cpuid = 0;
  775. #endif
  776. c = &cpu_data(cpuid);
  777. c->clock_tick = *cfreq;
  778. tb = &trap_block[cpuid];
  779. get_mondo_data(hp, mp, tb);
  780. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  781. u64 j, t = mdesc_arc_target(hp, a);
  782. const char *t_name;
  783. t_name = mdesc_node_name(hp, t);
  784. if (!strcmp(t_name, "cache")) {
  785. fill_in_one_cache(c, hp, t);
  786. continue;
  787. }
  788. mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) {
  789. u64 n = mdesc_arc_target(hp, j);
  790. const char *n_name;
  791. n_name = mdesc_node_name(hp, n);
  792. if (!strcmp(n_name, "cache"))
  793. fill_in_one_cache(c, hp, n);
  794. }
  795. }
  796. c->core_id = 0;
  797. c->proc_id = -1;
  798. return NULL;
  799. }
  800. void mdesc_fill_in_cpu_data(cpumask_t *mask)
  801. {
  802. struct mdesc_handle *hp;
  803. mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask);
  804. hp = mdesc_grab();
  805. set_core_ids(hp);
  806. set_proc_ids(hp);
  807. set_sock_ids(hp);
  808. mdesc_release(hp);
  809. smp_fill_in_sib_core_maps();
  810. }
  811. /* mdesc_open() - Grab a reference to mdesc_handle when /dev/mdesc is
  812. * opened. Hold this reference until /dev/mdesc is closed to ensure
  813. * mdesc data structure is not released underneath us. Store the
  814. * pointer to mdesc structure in private_data for read and seek to use
  815. */
  816. static int mdesc_open(struct inode *inode, struct file *file)
  817. {
  818. struct mdesc_handle *hp = mdesc_grab();
  819. if (!hp)
  820. return -ENODEV;
  821. file->private_data = hp;
  822. return 0;
  823. }
  824. static ssize_t mdesc_read(struct file *file, char __user *buf,
  825. size_t len, loff_t *offp)
  826. {
  827. struct mdesc_handle *hp = file->private_data;
  828. unsigned char *mdesc;
  829. int bytes_left, count = len;
  830. if (*offp >= hp->handle_size)
  831. return 0;
  832. bytes_left = hp->handle_size - *offp;
  833. if (count > bytes_left)
  834. count = bytes_left;
  835. mdesc = (unsigned char *)&hp->mdesc;
  836. mdesc += *offp;
  837. if (!copy_to_user(buf, mdesc, count)) {
  838. *offp += count;
  839. return count;
  840. } else {
  841. return -EFAULT;
  842. }
  843. }
  844. static loff_t mdesc_llseek(struct file *file, loff_t offset, int whence)
  845. {
  846. struct mdesc_handle *hp;
  847. switch (whence) {
  848. case SEEK_CUR:
  849. offset += file->f_pos;
  850. break;
  851. case SEEK_SET:
  852. break;
  853. default:
  854. return -EINVAL;
  855. }
  856. hp = file->private_data;
  857. if (offset > hp->handle_size)
  858. return -EINVAL;
  859. else
  860. file->f_pos = offset;
  861. return offset;
  862. }
  863. /* mdesc_close() - /dev/mdesc is being closed, release the reference to
  864. * mdesc structure.
  865. */
  866. static int mdesc_close(struct inode *inode, struct file *file)
  867. {
  868. mdesc_release(file->private_data);
  869. return 0;
  870. }
  871. static const struct file_operations mdesc_fops = {
  872. .open = mdesc_open,
  873. .read = mdesc_read,
  874. .llseek = mdesc_llseek,
  875. .release = mdesc_close,
  876. .owner = THIS_MODULE,
  877. };
  878. static struct miscdevice mdesc_misc = {
  879. .minor = MISC_DYNAMIC_MINOR,
  880. .name = "mdesc",
  881. .fops = &mdesc_fops,
  882. };
  883. static int __init mdesc_misc_init(void)
  884. {
  885. return misc_register(&mdesc_misc);
  886. }
  887. __initcall(mdesc_misc_init);
  888. void __init sun4v_mdesc_init(void)
  889. {
  890. struct mdesc_handle *hp;
  891. unsigned long len, real_len, status;
  892. (void) sun4v_mach_desc(0UL, 0UL, &len);
  893. printk("MDESC: Size is %lu bytes.\n", len);
  894. hp = mdesc_alloc(len, &memblock_mdesc_ops);
  895. if (hp == NULL) {
  896. prom_printf("MDESC: alloc of %lu bytes failed.\n", len);
  897. prom_halt();
  898. }
  899. status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len);
  900. if (status != HV_EOK || real_len > len) {
  901. prom_printf("sun4v_mach_desc fails, err(%lu), "
  902. "len(%lu), real_len(%lu)\n",
  903. status, len, real_len);
  904. mdesc_free(hp);
  905. prom_halt();
  906. }
  907. cur_mdesc = hp;
  908. report_platform_properties();
  909. }