prom.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  1. /*
  2. *
  3. *
  4. * Procedures for interfacing to Open Firmware.
  5. *
  6. * Paul Mackerras August 1996.
  7. * Copyright (C) 1996 Paul Mackerras.
  8. *
  9. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  10. * {engebret|bergner}@us.ibm.com
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #undef DEBUG
  18. #include <stdarg.h>
  19. #include <linux/config.h>
  20. #include <linux/kernel.h>
  21. #include <linux/string.h>
  22. #include <linux/init.h>
  23. #include <linux/threads.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/types.h>
  26. #include <linux/pci.h>
  27. #include <linux/stringify.h>
  28. #include <linux/delay.h>
  29. #include <linux/initrd.h>
  30. #include <linux/bitops.h>
  31. #include <linux/module.h>
  32. #include <asm/prom.h>
  33. #include <asm/rtas.h>
  34. #include <asm/lmb.h>
  35. #include <asm/abs_addr.h>
  36. #include <asm/page.h>
  37. #include <asm/processor.h>
  38. #include <asm/irq.h>
  39. #include <asm/io.h>
  40. #include <asm/smp.h>
  41. #include <asm/system.h>
  42. #include <asm/mmu.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/pci.h>
  45. #include <asm/iommu.h>
  46. #include <asm/bootinfo.h>
  47. #include <asm/ppcdebug.h>
  48. #include <asm/btext.h>
  49. #include <asm/sections.h>
  50. #include <asm/machdep.h>
  51. #include <asm/pSeries_reconfig.h>
  52. #ifdef DEBUG
  53. #define DBG(fmt...) udbg_printf(fmt)
  54. #else
  55. #define DBG(fmt...)
  56. #endif
  57. struct pci_reg_property {
  58. struct pci_address addr;
  59. u32 size_hi;
  60. u32 size_lo;
  61. };
  62. struct isa_reg_property {
  63. u32 space;
  64. u32 address;
  65. u32 size;
  66. };
  67. typedef int interpret_func(struct device_node *, unsigned long *,
  68. int, int, int);
  69. extern struct rtas_t rtas;
  70. extern struct lmb lmb;
  71. extern unsigned long klimit;
  72. static int __initdata dt_root_addr_cells;
  73. static int __initdata dt_root_size_cells;
  74. static int __initdata iommu_is_off;
  75. int __initdata iommu_force_on;
  76. typedef u32 cell_t;
  77. #if 0
  78. static struct boot_param_header *initial_boot_params __initdata;
  79. #else
  80. struct boot_param_header *initial_boot_params;
  81. #endif
  82. static struct device_node *allnodes = NULL;
  83. /* use when traversing tree through the allnext, child, sibling,
  84. * or parent members of struct device_node.
  85. */
  86. static DEFINE_RWLOCK(devtree_lock);
  87. /* export that to outside world */
  88. struct device_node *of_chosen;
  89. /*
  90. * Wrapper for allocating memory for various data that needs to be
  91. * attached to device nodes as they are processed at boot or when
  92. * added to the device tree later (e.g. DLPAR). At boot there is
  93. * already a region reserved so we just increment *mem_start by size;
  94. * otherwise we call kmalloc.
  95. */
  96. static void * prom_alloc(unsigned long size, unsigned long *mem_start)
  97. {
  98. unsigned long tmp;
  99. if (!mem_start)
  100. return kmalloc(size, GFP_KERNEL);
  101. tmp = *mem_start;
  102. *mem_start += size;
  103. return (void *)tmp;
  104. }
  105. /*
  106. * Find the device_node with a given phandle.
  107. */
  108. static struct device_node * find_phandle(phandle ph)
  109. {
  110. struct device_node *np;
  111. for (np = allnodes; np != 0; np = np->allnext)
  112. if (np->linux_phandle == ph)
  113. return np;
  114. return NULL;
  115. }
  116. /*
  117. * Find the interrupt parent of a node.
  118. */
  119. static struct device_node * __devinit intr_parent(struct device_node *p)
  120. {
  121. phandle *parp;
  122. parp = (phandle *) get_property(p, "interrupt-parent", NULL);
  123. if (parp == NULL)
  124. return p->parent;
  125. return find_phandle(*parp);
  126. }
  127. /*
  128. * Find out the size of each entry of the interrupts property
  129. * for a node.
  130. */
  131. int __devinit prom_n_intr_cells(struct device_node *np)
  132. {
  133. struct device_node *p;
  134. unsigned int *icp;
  135. for (p = np; (p = intr_parent(p)) != NULL; ) {
  136. icp = (unsigned int *)
  137. get_property(p, "#interrupt-cells", NULL);
  138. if (icp != NULL)
  139. return *icp;
  140. if (get_property(p, "interrupt-controller", NULL) != NULL
  141. || get_property(p, "interrupt-map", NULL) != NULL) {
  142. printk("oops, node %s doesn't have #interrupt-cells\n",
  143. p->full_name);
  144. return 1;
  145. }
  146. }
  147. #ifdef DEBUG_IRQ
  148. printk("prom_n_intr_cells failed for %s\n", np->full_name);
  149. #endif
  150. return 1;
  151. }
  152. /*
  153. * Map an interrupt from a device up to the platform interrupt
  154. * descriptor.
  155. */
  156. static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
  157. struct device_node *np, unsigned int *ints,
  158. int nintrc)
  159. {
  160. struct device_node *p, *ipar;
  161. unsigned int *imap, *imask, *ip;
  162. int i, imaplen, match;
  163. int newintrc = 0, newaddrc = 0;
  164. unsigned int *reg;
  165. int naddrc;
  166. reg = (unsigned int *) get_property(np, "reg", NULL);
  167. naddrc = prom_n_addr_cells(np);
  168. p = intr_parent(np);
  169. while (p != NULL) {
  170. if (get_property(p, "interrupt-controller", NULL) != NULL)
  171. /* this node is an interrupt controller, stop here */
  172. break;
  173. imap = (unsigned int *)
  174. get_property(p, "interrupt-map", &imaplen);
  175. if (imap == NULL) {
  176. p = intr_parent(p);
  177. continue;
  178. }
  179. imask = (unsigned int *)
  180. get_property(p, "interrupt-map-mask", NULL);
  181. if (imask == NULL) {
  182. printk("oops, %s has interrupt-map but no mask\n",
  183. p->full_name);
  184. return 0;
  185. }
  186. imaplen /= sizeof(unsigned int);
  187. match = 0;
  188. ipar = NULL;
  189. while (imaplen > 0 && !match) {
  190. /* check the child-interrupt field */
  191. match = 1;
  192. for (i = 0; i < naddrc && match; ++i)
  193. match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
  194. for (; i < naddrc + nintrc && match; ++i)
  195. match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
  196. imap += naddrc + nintrc;
  197. imaplen -= naddrc + nintrc;
  198. /* grab the interrupt parent */
  199. ipar = find_phandle((phandle) *imap++);
  200. --imaplen;
  201. if (ipar == NULL) {
  202. printk("oops, no int parent %x in map of %s\n",
  203. imap[-1], p->full_name);
  204. return 0;
  205. }
  206. /* find the parent's # addr and intr cells */
  207. ip = (unsigned int *)
  208. get_property(ipar, "#interrupt-cells", NULL);
  209. if (ip == NULL) {
  210. printk("oops, no #interrupt-cells on %s\n",
  211. ipar->full_name);
  212. return 0;
  213. }
  214. newintrc = *ip;
  215. ip = (unsigned int *)
  216. get_property(ipar, "#address-cells", NULL);
  217. newaddrc = (ip == NULL)? 0: *ip;
  218. imap += newaddrc + newintrc;
  219. imaplen -= newaddrc + newintrc;
  220. }
  221. if (imaplen < 0) {
  222. printk("oops, error decoding int-map on %s, len=%d\n",
  223. p->full_name, imaplen);
  224. return 0;
  225. }
  226. if (!match) {
  227. #ifdef DEBUG_IRQ
  228. printk("oops, no match in %s int-map for %s\n",
  229. p->full_name, np->full_name);
  230. #endif
  231. return 0;
  232. }
  233. p = ipar;
  234. naddrc = newaddrc;
  235. nintrc = newintrc;
  236. ints = imap - nintrc;
  237. reg = ints - naddrc;
  238. }
  239. if (p == NULL) {
  240. #ifdef DEBUG_IRQ
  241. printk("hmmm, int tree for %s doesn't have ctrler\n",
  242. np->full_name);
  243. #endif
  244. return 0;
  245. }
  246. *irq = ints;
  247. *ictrler = p;
  248. return nintrc;
  249. }
  250. static int __devinit finish_node_interrupts(struct device_node *np,
  251. unsigned long *mem_start,
  252. int measure_only)
  253. {
  254. unsigned int *ints;
  255. int intlen, intrcells, intrcount;
  256. int i, j, n;
  257. unsigned int *irq, virq;
  258. struct device_node *ic;
  259. ints = (unsigned int *) get_property(np, "interrupts", &intlen);
  260. if (ints == NULL)
  261. return 0;
  262. intrcells = prom_n_intr_cells(np);
  263. intlen /= intrcells * sizeof(unsigned int);
  264. np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
  265. if (!np->intrs)
  266. return -ENOMEM;
  267. if (measure_only)
  268. return 0;
  269. intrcount = 0;
  270. for (i = 0; i < intlen; ++i, ints += intrcells) {
  271. n = map_interrupt(&irq, &ic, np, ints, intrcells);
  272. if (n <= 0)
  273. continue;
  274. /* don't map IRQ numbers under a cascaded 8259 controller */
  275. if (ic && device_is_compatible(ic, "chrp,iic")) {
  276. np->intrs[intrcount].line = irq[0];
  277. } else {
  278. virq = virt_irq_create_mapping(irq[0]);
  279. if (virq == NO_IRQ) {
  280. printk(KERN_CRIT "Could not allocate interrupt"
  281. " number for %s\n", np->full_name);
  282. continue;
  283. }
  284. np->intrs[intrcount].line = irq_offset_up(virq);
  285. }
  286. /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
  287. if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
  288. char *name = get_property(ic->parent, "name", NULL);
  289. if (name && !strcmp(name, "u3"))
  290. np->intrs[intrcount].line += 128;
  291. else if (!(name && !strcmp(name, "mac-io")))
  292. /* ignore other cascaded controllers, such as
  293. the k2-sata-root */
  294. break;
  295. }
  296. np->intrs[intrcount].sense = 1;
  297. if (n > 1)
  298. np->intrs[intrcount].sense = irq[1];
  299. if (n > 2) {
  300. printk("hmmm, got %d intr cells for %s:", n,
  301. np->full_name);
  302. for (j = 0; j < n; ++j)
  303. printk(" %d", irq[j]);
  304. printk("\n");
  305. }
  306. ++intrcount;
  307. }
  308. np->n_intrs = intrcount;
  309. return 0;
  310. }
  311. static int __devinit interpret_pci_props(struct device_node *np,
  312. unsigned long *mem_start,
  313. int naddrc, int nsizec,
  314. int measure_only)
  315. {
  316. struct address_range *adr;
  317. struct pci_reg_property *pci_addrs;
  318. int i, l, n_addrs;
  319. pci_addrs = (struct pci_reg_property *)
  320. get_property(np, "assigned-addresses", &l);
  321. if (!pci_addrs)
  322. return 0;
  323. n_addrs = l / sizeof(*pci_addrs);
  324. adr = prom_alloc(n_addrs * sizeof(*adr), mem_start);
  325. if (!adr)
  326. return -ENOMEM;
  327. if (measure_only)
  328. return 0;
  329. np->addrs = adr;
  330. np->n_addrs = n_addrs;
  331. for (i = 0; i < n_addrs; i++) {
  332. adr[i].space = pci_addrs[i].addr.a_hi;
  333. adr[i].address = pci_addrs[i].addr.a_lo |
  334. ((u64)pci_addrs[i].addr.a_mid << 32);
  335. adr[i].size = pci_addrs[i].size_lo;
  336. }
  337. return 0;
  338. }
  339. static int __init interpret_dbdma_props(struct device_node *np,
  340. unsigned long *mem_start,
  341. int naddrc, int nsizec,
  342. int measure_only)
  343. {
  344. struct reg_property32 *rp;
  345. struct address_range *adr;
  346. unsigned long base_address;
  347. int i, l;
  348. struct device_node *db;
  349. base_address = 0;
  350. if (!measure_only) {
  351. for (db = np->parent; db != NULL; db = db->parent) {
  352. if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
  353. base_address = db->addrs[0].address;
  354. break;
  355. }
  356. }
  357. }
  358. rp = (struct reg_property32 *) get_property(np, "reg", &l);
  359. if (rp != 0 && l >= sizeof(struct reg_property32)) {
  360. i = 0;
  361. adr = (struct address_range *) (*mem_start);
  362. while ((l -= sizeof(struct reg_property32)) >= 0) {
  363. if (!measure_only) {
  364. adr[i].space = 2;
  365. adr[i].address = rp[i].address + base_address;
  366. adr[i].size = rp[i].size;
  367. }
  368. ++i;
  369. }
  370. np->addrs = adr;
  371. np->n_addrs = i;
  372. (*mem_start) += i * sizeof(struct address_range);
  373. }
  374. return 0;
  375. }
  376. static int __init interpret_macio_props(struct device_node *np,
  377. unsigned long *mem_start,
  378. int naddrc, int nsizec,
  379. int measure_only)
  380. {
  381. struct reg_property32 *rp;
  382. struct address_range *adr;
  383. unsigned long base_address;
  384. int i, l;
  385. struct device_node *db;
  386. base_address = 0;
  387. if (!measure_only) {
  388. for (db = np->parent; db != NULL; db = db->parent) {
  389. if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
  390. base_address = db->addrs[0].address;
  391. break;
  392. }
  393. }
  394. }
  395. rp = (struct reg_property32 *) get_property(np, "reg", &l);
  396. if (rp != 0 && l >= sizeof(struct reg_property32)) {
  397. i = 0;
  398. adr = (struct address_range *) (*mem_start);
  399. while ((l -= sizeof(struct reg_property32)) >= 0) {
  400. if (!measure_only) {
  401. adr[i].space = 2;
  402. adr[i].address = rp[i].address + base_address;
  403. adr[i].size = rp[i].size;
  404. }
  405. ++i;
  406. }
  407. np->addrs = adr;
  408. np->n_addrs = i;
  409. (*mem_start) += i * sizeof(struct address_range);
  410. }
  411. return 0;
  412. }
  413. static int __init interpret_isa_props(struct device_node *np,
  414. unsigned long *mem_start,
  415. int naddrc, int nsizec,
  416. int measure_only)
  417. {
  418. struct isa_reg_property *rp;
  419. struct address_range *adr;
  420. int i, l;
  421. rp = (struct isa_reg_property *) get_property(np, "reg", &l);
  422. if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
  423. i = 0;
  424. adr = (struct address_range *) (*mem_start);
  425. while ((l -= sizeof(struct isa_reg_property)) >= 0) {
  426. if (!measure_only) {
  427. adr[i].space = rp[i].space;
  428. adr[i].address = rp[i].address;
  429. adr[i].size = rp[i].size;
  430. }
  431. ++i;
  432. }
  433. np->addrs = adr;
  434. np->n_addrs = i;
  435. (*mem_start) += i * sizeof(struct address_range);
  436. }
  437. return 0;
  438. }
  439. static int __init interpret_root_props(struct device_node *np,
  440. unsigned long *mem_start,
  441. int naddrc, int nsizec,
  442. int measure_only)
  443. {
  444. struct address_range *adr;
  445. int i, l;
  446. unsigned int *rp;
  447. int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
  448. rp = (unsigned int *) get_property(np, "reg", &l);
  449. if (rp != 0 && l >= rpsize) {
  450. i = 0;
  451. adr = (struct address_range *) (*mem_start);
  452. while ((l -= rpsize) >= 0) {
  453. if (!measure_only) {
  454. adr[i].space = 0;
  455. adr[i].address = rp[naddrc - 1];
  456. adr[i].size = rp[naddrc + nsizec - 1];
  457. }
  458. ++i;
  459. rp += naddrc + nsizec;
  460. }
  461. np->addrs = adr;
  462. np->n_addrs = i;
  463. (*mem_start) += i * sizeof(struct address_range);
  464. }
  465. return 0;
  466. }
  467. static int __devinit finish_node(struct device_node *np,
  468. unsigned long *mem_start,
  469. interpret_func *ifunc,
  470. int naddrc, int nsizec,
  471. int measure_only)
  472. {
  473. struct device_node *child;
  474. int *ip, rc = 0;
  475. /* get the device addresses and interrupts */
  476. if (ifunc != NULL)
  477. rc = ifunc(np, mem_start, naddrc, nsizec, measure_only);
  478. if (rc)
  479. goto out;
  480. rc = finish_node_interrupts(np, mem_start, measure_only);
  481. if (rc)
  482. goto out;
  483. /* Look for #address-cells and #size-cells properties. */
  484. ip = (int *) get_property(np, "#address-cells", NULL);
  485. if (ip != NULL)
  486. naddrc = *ip;
  487. ip = (int *) get_property(np, "#size-cells", NULL);
  488. if (ip != NULL)
  489. nsizec = *ip;
  490. if (!strcmp(np->name, "device-tree") || np->parent == NULL)
  491. ifunc = interpret_root_props;
  492. else if (np->type == 0)
  493. ifunc = NULL;
  494. else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
  495. ifunc = interpret_pci_props;
  496. else if (!strcmp(np->type, "dbdma"))
  497. ifunc = interpret_dbdma_props;
  498. else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
  499. ifunc = interpret_macio_props;
  500. else if (!strcmp(np->type, "isa"))
  501. ifunc = interpret_isa_props;
  502. else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
  503. ifunc = interpret_root_props;
  504. else if (!((ifunc == interpret_dbdma_props
  505. || ifunc == interpret_macio_props)
  506. && (!strcmp(np->type, "escc")
  507. || !strcmp(np->type, "media-bay"))))
  508. ifunc = NULL;
  509. for (child = np->child; child != NULL; child = child->sibling) {
  510. rc = finish_node(child, mem_start, ifunc,
  511. naddrc, nsizec, measure_only);
  512. if (rc)
  513. goto out;
  514. }
  515. out:
  516. return rc;
  517. }
  518. /**
  519. * finish_device_tree is called once things are running normally
  520. * (i.e. with text and data mapped to the address they were linked at).
  521. * It traverses the device tree and fills in some of the additional,
  522. * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
  523. * mapping is also initialized at this point.
  524. */
  525. void __init finish_device_tree(void)
  526. {
  527. unsigned long start, end, size = 0;
  528. DBG(" -> finish_device_tree\n");
  529. if (ppc64_interrupt_controller == IC_INVALID) {
  530. DBG("failed to configure interrupt controller type\n");
  531. panic("failed to configure interrupt controller type\n");
  532. }
  533. /* Initialize virtual IRQ map */
  534. virt_irq_init();
  535. /*
  536. * Finish device-tree (pre-parsing some properties etc...)
  537. * We do this in 2 passes. One with "measure_only" set, which
  538. * will only measure the amount of memory needed, then we can
  539. * allocate that memory, and call finish_node again. However,
  540. * we must be careful as most routines will fail nowadays when
  541. * prom_alloc() returns 0, so we must make sure our first pass
  542. * doesn't start at 0. We pre-initialize size to 16 for that
  543. * reason and then remove those additional 16 bytes
  544. */
  545. size = 16;
  546. finish_node(allnodes, &size, NULL, 0, 0, 1);
  547. size -= 16;
  548. end = start = (unsigned long)abs_to_virt(lmb_alloc(size, 128));
  549. finish_node(allnodes, &end, NULL, 0, 0, 0);
  550. BUG_ON(end != start + size);
  551. DBG(" <- finish_device_tree\n");
  552. }
  553. #ifdef DEBUG
  554. #define printk udbg_printf
  555. #endif
  556. static inline char *find_flat_dt_string(u32 offset)
  557. {
  558. return ((char *)initial_boot_params) +
  559. initial_boot_params->off_dt_strings + offset;
  560. }
  561. /**
  562. * This function is used to scan the flattened device-tree, it is
  563. * used to extract the memory informations at boot before we can
  564. * unflatten the tree
  565. */
  566. static int __init scan_flat_dt(int (*it)(unsigned long node,
  567. const char *uname, int depth,
  568. void *data),
  569. void *data)
  570. {
  571. unsigned long p = ((unsigned long)initial_boot_params) +
  572. initial_boot_params->off_dt_struct;
  573. int rc = 0;
  574. int depth = -1;
  575. do {
  576. u32 tag = *((u32 *)p);
  577. char *pathp;
  578. p += 4;
  579. if (tag == OF_DT_END_NODE) {
  580. depth --;
  581. continue;
  582. }
  583. if (tag == OF_DT_NOP)
  584. continue;
  585. if (tag == OF_DT_END)
  586. break;
  587. if (tag == OF_DT_PROP) {
  588. u32 sz = *((u32 *)p);
  589. p += 8;
  590. if (initial_boot_params->version < 0x10)
  591. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  592. p += sz;
  593. p = _ALIGN(p, 4);
  594. continue;
  595. }
  596. if (tag != OF_DT_BEGIN_NODE) {
  597. printk(KERN_WARNING "Invalid tag %x scanning flattened"
  598. " device tree !\n", tag);
  599. return -EINVAL;
  600. }
  601. depth++;
  602. pathp = (char *)p;
  603. p = _ALIGN(p + strlen(pathp) + 1, 4);
  604. if ((*pathp) == '/') {
  605. char *lp, *np;
  606. for (lp = NULL, np = pathp; *np; np++)
  607. if ((*np) == '/')
  608. lp = np+1;
  609. if (lp != NULL)
  610. pathp = lp;
  611. }
  612. rc = it(p, pathp, depth, data);
  613. if (rc != 0)
  614. break;
  615. } while(1);
  616. return rc;
  617. }
  618. /**
  619. * This function can be used within scan_flattened_dt callback to get
  620. * access to properties
  621. */
  622. static void* __init get_flat_dt_prop(unsigned long node, const char *name,
  623. unsigned long *size)
  624. {
  625. unsigned long p = node;
  626. do {
  627. u32 tag = *((u32 *)p);
  628. u32 sz, noff;
  629. const char *nstr;
  630. p += 4;
  631. if (tag == OF_DT_NOP)
  632. continue;
  633. if (tag != OF_DT_PROP)
  634. return NULL;
  635. sz = *((u32 *)p);
  636. noff = *((u32 *)(p + 4));
  637. p += 8;
  638. if (initial_boot_params->version < 0x10)
  639. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  640. nstr = find_flat_dt_string(noff);
  641. if (nstr == NULL) {
  642. printk(KERN_WARNING "Can't find property index"
  643. " name !\n");
  644. return NULL;
  645. }
  646. if (strcmp(name, nstr) == 0) {
  647. if (size)
  648. *size = sz;
  649. return (void *)p;
  650. }
  651. p += sz;
  652. p = _ALIGN(p, 4);
  653. } while(1);
  654. }
  655. static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  656. unsigned long align)
  657. {
  658. void *res;
  659. *mem = _ALIGN(*mem, align);
  660. res = (void *)*mem;
  661. *mem += size;
  662. return res;
  663. }
  664. static unsigned long __init unflatten_dt_node(unsigned long mem,
  665. unsigned long *p,
  666. struct device_node *dad,
  667. struct device_node ***allnextpp,
  668. unsigned long fpsize)
  669. {
  670. struct device_node *np;
  671. struct property *pp, **prev_pp = NULL;
  672. char *pathp;
  673. u32 tag;
  674. unsigned int l, allocl;
  675. int has_name = 0;
  676. int new_format = 0;
  677. tag = *((u32 *)(*p));
  678. if (tag != OF_DT_BEGIN_NODE) {
  679. printk("Weird tag at start of node: %x\n", tag);
  680. return mem;
  681. }
  682. *p += 4;
  683. pathp = (char *)*p;
  684. l = allocl = strlen(pathp) + 1;
  685. *p = _ALIGN(*p + l, 4);
  686. /* version 0x10 has a more compact unit name here instead of the full
  687. * path. we accumulate the full path size using "fpsize", we'll rebuild
  688. * it later. We detect this because the first character of the name is
  689. * not '/'.
  690. */
  691. if ((*pathp) != '/') {
  692. new_format = 1;
  693. if (fpsize == 0) {
  694. /* root node: special case. fpsize accounts for path
  695. * plus terminating zero. root node only has '/', so
  696. * fpsize should be 2, but we want to avoid the first
  697. * level nodes to have two '/' so we use fpsize 1 here
  698. */
  699. fpsize = 1;
  700. allocl = 2;
  701. } else {
  702. /* account for '/' and path size minus terminal 0
  703. * already in 'l'
  704. */
  705. fpsize += l;
  706. allocl = fpsize;
  707. }
  708. }
  709. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  710. __alignof__(struct device_node));
  711. if (allnextpp) {
  712. memset(np, 0, sizeof(*np));
  713. np->full_name = ((char*)np) + sizeof(struct device_node);
  714. if (new_format) {
  715. char *p = np->full_name;
  716. /* rebuild full path for new format */
  717. if (dad && dad->parent) {
  718. strcpy(p, dad->full_name);
  719. #ifdef DEBUG
  720. if ((strlen(p) + l + 1) != allocl) {
  721. DBG("%s: p: %d, l: %d, a: %d\n",
  722. pathp, strlen(p), l, allocl);
  723. }
  724. #endif
  725. p += strlen(p);
  726. }
  727. *(p++) = '/';
  728. memcpy(p, pathp, l);
  729. } else
  730. memcpy(np->full_name, pathp, l);
  731. prev_pp = &np->properties;
  732. **allnextpp = np;
  733. *allnextpp = &np->allnext;
  734. if (dad != NULL) {
  735. np->parent = dad;
  736. /* we temporarily use the next field as `last_child'*/
  737. if (dad->next == 0)
  738. dad->child = np;
  739. else
  740. dad->next->sibling = np;
  741. dad->next = np;
  742. }
  743. kref_init(&np->kref);
  744. }
  745. while(1) {
  746. u32 sz, noff;
  747. char *pname;
  748. tag = *((u32 *)(*p));
  749. if (tag == OF_DT_NOP) {
  750. *p += 4;
  751. continue;
  752. }
  753. if (tag != OF_DT_PROP)
  754. break;
  755. *p += 4;
  756. sz = *((u32 *)(*p));
  757. noff = *((u32 *)((*p) + 4));
  758. *p += 8;
  759. if (initial_boot_params->version < 0x10)
  760. *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
  761. pname = find_flat_dt_string(noff);
  762. if (pname == NULL) {
  763. printk("Can't find property name in list !\n");
  764. break;
  765. }
  766. if (strcmp(pname, "name") == 0)
  767. has_name = 1;
  768. l = strlen(pname) + 1;
  769. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  770. __alignof__(struct property));
  771. if (allnextpp) {
  772. if (strcmp(pname, "linux,phandle") == 0) {
  773. np->node = *((u32 *)*p);
  774. if (np->linux_phandle == 0)
  775. np->linux_phandle = np->node;
  776. }
  777. if (strcmp(pname, "ibm,phandle") == 0)
  778. np->linux_phandle = *((u32 *)*p);
  779. pp->name = pname;
  780. pp->length = sz;
  781. pp->value = (void *)*p;
  782. *prev_pp = pp;
  783. prev_pp = &pp->next;
  784. }
  785. *p = _ALIGN((*p) + sz, 4);
  786. }
  787. /* with version 0x10 we may not have the name property, recreate
  788. * it here from the unit name if absent
  789. */
  790. if (!has_name) {
  791. char *p = pathp, *ps = pathp, *pa = NULL;
  792. int sz;
  793. while (*p) {
  794. if ((*p) == '@')
  795. pa = p;
  796. if ((*p) == '/')
  797. ps = p + 1;
  798. p++;
  799. }
  800. if (pa < ps)
  801. pa = p;
  802. sz = (pa - ps) + 1;
  803. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  804. __alignof__(struct property));
  805. if (allnextpp) {
  806. pp->name = "name";
  807. pp->length = sz;
  808. pp->value = (unsigned char *)(pp + 1);
  809. *prev_pp = pp;
  810. prev_pp = &pp->next;
  811. memcpy(pp->value, ps, sz - 1);
  812. ((char *)pp->value)[sz - 1] = 0;
  813. DBG("fixed up name for %s -> %s\n", pathp, pp->value);
  814. }
  815. }
  816. if (allnextpp) {
  817. *prev_pp = NULL;
  818. np->name = get_property(np, "name", NULL);
  819. np->type = get_property(np, "device_type", NULL);
  820. if (!np->name)
  821. np->name = "<NULL>";
  822. if (!np->type)
  823. np->type = "<NULL>";
  824. }
  825. while (tag == OF_DT_BEGIN_NODE) {
  826. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  827. tag = *((u32 *)(*p));
  828. }
  829. if (tag != OF_DT_END_NODE) {
  830. printk("Weird tag at end of node: %x\n", tag);
  831. return mem;
  832. }
  833. *p += 4;
  834. return mem;
  835. }
  836. /**
  837. * unflattens the device-tree passed by the firmware, creating the
  838. * tree of struct device_node. It also fills the "name" and "type"
  839. * pointers of the nodes so the normal device-tree walking functions
  840. * can be used (this used to be done by finish_device_tree)
  841. */
  842. void __init unflatten_device_tree(void)
  843. {
  844. unsigned long start, mem, size;
  845. struct device_node **allnextp = &allnodes;
  846. char *p = NULL;
  847. int l = 0;
  848. DBG(" -> unflatten_device_tree()\n");
  849. /* First pass, scan for size */
  850. start = ((unsigned long)initial_boot_params) +
  851. initial_boot_params->off_dt_struct;
  852. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  853. size = (size | 3) + 1;
  854. DBG(" size is %lx, allocating...\n", size);
  855. /* Allocate memory for the expanded device tree */
  856. mem = lmb_alloc(size + 4, __alignof__(struct device_node));
  857. if (!mem) {
  858. DBG("Couldn't allocate memory with lmb_alloc()!\n");
  859. panic("Couldn't allocate memory with lmb_alloc()!\n");
  860. }
  861. mem = (unsigned long)abs_to_virt(mem);
  862. ((u32 *)mem)[size / 4] = 0xdeadbeef;
  863. DBG(" unflattening...\n", mem);
  864. /* Second pass, do actual unflattening */
  865. start = ((unsigned long)initial_boot_params) +
  866. initial_boot_params->off_dt_struct;
  867. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  868. if (*((u32 *)start) != OF_DT_END)
  869. printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
  870. if (((u32 *)mem)[size / 4] != 0xdeadbeef)
  871. printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
  872. ((u32 *)mem)[size / 4] );
  873. *allnextp = NULL;
  874. /* Get pointer to OF "/chosen" node for use everywhere */
  875. of_chosen = of_find_node_by_path("/chosen");
  876. /* Retreive command line */
  877. if (of_chosen != NULL) {
  878. p = (char *)get_property(of_chosen, "bootargs", &l);
  879. if (p != NULL && l > 0)
  880. strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
  881. }
  882. #ifdef CONFIG_CMDLINE
  883. if (l == 0 || (l == 1 && (*p) == 0))
  884. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  885. #endif /* CONFIG_CMDLINE */
  886. DBG("Command line is: %s\n", cmd_line);
  887. DBG(" <- unflatten_device_tree()\n");
  888. }
  889. static int __init early_init_dt_scan_cpus(unsigned long node,
  890. const char *uname, int depth, void *data)
  891. {
  892. char *type = get_flat_dt_prop(node, "device_type", NULL);
  893. u32 *prop;
  894. unsigned long size;
  895. /* We are scanning "cpu" nodes only */
  896. if (type == NULL || strcmp(type, "cpu") != 0)
  897. return 0;
  898. /* On LPAR, look for the first ibm,pft-size property for the hash table size
  899. */
  900. if (systemcfg->platform == PLATFORM_PSERIES_LPAR && ppc64_pft_size == 0) {
  901. u32 *pft_size;
  902. pft_size = (u32 *)get_flat_dt_prop(node, "ibm,pft-size", NULL);
  903. if (pft_size != NULL) {
  904. /* pft_size[0] is the NUMA CEC cookie */
  905. ppc64_pft_size = pft_size[1];
  906. }
  907. }
  908. if (initial_boot_params && initial_boot_params->version >= 2) {
  909. /* version 2 of the kexec param format adds the phys cpuid
  910. * of booted proc.
  911. */
  912. boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
  913. boot_cpuid = 0;
  914. } else {
  915. /* Check if it's the boot-cpu, set it's hw index in paca now */
  916. if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
  917. u32 *prop = get_flat_dt_prop(node, "reg", NULL);
  918. set_hard_smp_processor_id(0, prop == NULL ? 0 : *prop);
  919. boot_cpuid_phys = get_hard_smp_processor_id(0);
  920. }
  921. }
  922. #ifdef CONFIG_ALTIVEC
  923. /* Check if we have a VMX and eventually update CPU features */
  924. prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", NULL);
  925. if (prop && (*prop) > 0) {
  926. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  927. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  928. }
  929. /* Same goes for Apple's "altivec" property */
  930. prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL);
  931. if (prop) {
  932. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  933. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  934. }
  935. #endif /* CONFIG_ALTIVEC */
  936. /*
  937. * Check for an SMT capable CPU and set the CPU feature. We do
  938. * this by looking at the size of the ibm,ppc-interrupt-server#s
  939. * property
  940. */
  941. prop = (u32 *)get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
  942. &size);
  943. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  944. if (prop && ((size / sizeof(u32)) > 1))
  945. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  946. return 0;
  947. }
  948. static int __init early_init_dt_scan_chosen(unsigned long node,
  949. const char *uname, int depth, void *data)
  950. {
  951. u32 *prop;
  952. u64 *prop64;
  953. extern unsigned long memory_limit, tce_alloc_start, tce_alloc_end;
  954. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  955. if (depth != 1 || strcmp(uname, "chosen") != 0)
  956. return 0;
  957. /* get platform type */
  958. prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
  959. if (prop == NULL)
  960. return 0;
  961. systemcfg->platform = *prop;
  962. /* check if iommu is forced on or off */
  963. if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  964. iommu_is_off = 1;
  965. if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  966. iommu_force_on = 1;
  967. prop64 = (u64*)get_flat_dt_prop(node, "linux,memory-limit", NULL);
  968. if (prop64)
  969. memory_limit = *prop64;
  970. prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  971. if (prop64)
  972. tce_alloc_start = *prop64;
  973. prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  974. if (prop64)
  975. tce_alloc_end = *prop64;
  976. #ifdef CONFIG_PPC_RTAS
  977. /* To help early debugging via the front panel, we retreive a minimal
  978. * set of RTAS infos now if available
  979. */
  980. {
  981. u64 *basep, *entryp;
  982. basep = (u64*)get_flat_dt_prop(node, "linux,rtas-base", NULL);
  983. entryp = (u64*)get_flat_dt_prop(node, "linux,rtas-entry", NULL);
  984. prop = (u32*)get_flat_dt_prop(node, "linux,rtas-size", NULL);
  985. if (basep && entryp && prop) {
  986. rtas.base = *basep;
  987. rtas.entry = *entryp;
  988. rtas.size = *prop;
  989. }
  990. }
  991. #endif /* CONFIG_PPC_RTAS */
  992. /* break now */
  993. return 1;
  994. }
  995. static int __init early_init_dt_scan_root(unsigned long node,
  996. const char *uname, int depth, void *data)
  997. {
  998. u32 *prop;
  999. if (depth != 0)
  1000. return 0;
  1001. prop = (u32 *)get_flat_dt_prop(node, "#size-cells", NULL);
  1002. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  1003. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  1004. prop = (u32 *)get_flat_dt_prop(node, "#address-cells", NULL);
  1005. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  1006. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  1007. /* break now */
  1008. return 1;
  1009. }
  1010. static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
  1011. {
  1012. cell_t *p = *cellp;
  1013. unsigned long r = 0;
  1014. /* Ignore more than 2 cells */
  1015. while (s > 2) {
  1016. p++;
  1017. s--;
  1018. }
  1019. while (s) {
  1020. r <<= 32;
  1021. r |= *(p++);
  1022. s--;
  1023. }
  1024. *cellp = p;
  1025. return r;
  1026. }
  1027. static int __init early_init_dt_scan_memory(unsigned long node,
  1028. const char *uname, int depth, void *data)
  1029. {
  1030. char *type = get_flat_dt_prop(node, "device_type", NULL);
  1031. cell_t *reg, *endp;
  1032. unsigned long l;
  1033. /* We are scanning "memory" nodes only */
  1034. if (type == NULL || strcmp(type, "memory") != 0)
  1035. return 0;
  1036. reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
  1037. if (reg == NULL)
  1038. return 0;
  1039. endp = reg + (l / sizeof(cell_t));
  1040. DBG("memory scan node %s ..., reg size %ld, data: %x %x %x %x, ...\n",
  1041. uname, l, reg[0], reg[1], reg[2], reg[3]);
  1042. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  1043. unsigned long base, size;
  1044. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  1045. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  1046. if (size == 0)
  1047. continue;
  1048. DBG(" - %lx , %lx\n", base, size);
  1049. if (iommu_is_off) {
  1050. if (base >= 0x80000000ul)
  1051. continue;
  1052. if ((base + size) > 0x80000000ul)
  1053. size = 0x80000000ul - base;
  1054. }
  1055. lmb_add(base, size);
  1056. }
  1057. return 0;
  1058. }
  1059. static void __init early_reserve_mem(void)
  1060. {
  1061. u64 base, size;
  1062. u64 *reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  1063. initial_boot_params->off_mem_rsvmap);
  1064. while (1) {
  1065. base = *(reserve_map++);
  1066. size = *(reserve_map++);
  1067. if (size == 0)
  1068. break;
  1069. DBG("reserving: %lx -> %lx\n", base, size);
  1070. lmb_reserve(base, size);
  1071. }
  1072. #if 0
  1073. DBG("memory reserved, lmbs :\n");
  1074. lmb_dump_all();
  1075. #endif
  1076. }
  1077. void __init early_init_devtree(void *params)
  1078. {
  1079. DBG(" -> early_init_devtree()\n");
  1080. /* Setup flat device-tree pointer */
  1081. initial_boot_params = params;
  1082. /* By default, hash size is not set */
  1083. ppc64_pft_size = 0;
  1084. /* Retreive various informations from the /chosen node of the
  1085. * device-tree, including the platform type, initrd location and
  1086. * size, TCE reserve, and more ...
  1087. */
  1088. scan_flat_dt(early_init_dt_scan_chosen, NULL);
  1089. /* Scan memory nodes and rebuild LMBs */
  1090. lmb_init();
  1091. scan_flat_dt(early_init_dt_scan_root, NULL);
  1092. scan_flat_dt(early_init_dt_scan_memory, NULL);
  1093. lmb_enforce_memory_limit();
  1094. lmb_analyze();
  1095. systemcfg->physicalMemorySize = lmb_phys_mem_size();
  1096. lmb_reserve(0, __pa(klimit));
  1097. DBG("Phys. mem: %lx\n", systemcfg->physicalMemorySize);
  1098. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  1099. early_reserve_mem();
  1100. DBG("Scanning CPUs ...\n");
  1101. /* Retreive hash table size from flattened tree plus other
  1102. * CPU related informations (altivec support, boot CPU ID, ...)
  1103. */
  1104. scan_flat_dt(early_init_dt_scan_cpus, NULL);
  1105. /* If hash size wasn't obtained above, we calculate it now based on
  1106. * the total RAM size
  1107. */
  1108. if (ppc64_pft_size == 0) {
  1109. unsigned long rnd_mem_size, pteg_count;
  1110. /* round mem_size up to next power of 2 */
  1111. rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
  1112. if (rnd_mem_size < systemcfg->physicalMemorySize)
  1113. rnd_mem_size <<= 1;
  1114. /* # pages / 2 */
  1115. pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
  1116. ppc64_pft_size = __ilog2(pteg_count << 7);
  1117. }
  1118. DBG("Hash pftSize: %x\n", (int)ppc64_pft_size);
  1119. DBG(" <- early_init_devtree()\n");
  1120. }
  1121. #undef printk
  1122. int
  1123. prom_n_addr_cells(struct device_node* np)
  1124. {
  1125. int* ip;
  1126. do {
  1127. if (np->parent)
  1128. np = np->parent;
  1129. ip = (int *) get_property(np, "#address-cells", NULL);
  1130. if (ip != NULL)
  1131. return *ip;
  1132. } while (np->parent);
  1133. /* No #address-cells property for the root node, default to 1 */
  1134. return 1;
  1135. }
  1136. int
  1137. prom_n_size_cells(struct device_node* np)
  1138. {
  1139. int* ip;
  1140. do {
  1141. if (np->parent)
  1142. np = np->parent;
  1143. ip = (int *) get_property(np, "#size-cells", NULL);
  1144. if (ip != NULL)
  1145. return *ip;
  1146. } while (np->parent);
  1147. /* No #size-cells property for the root node, default to 1 */
  1148. return 1;
  1149. }
  1150. /**
  1151. * Work out the sense (active-low level / active-high edge)
  1152. * of each interrupt from the device tree.
  1153. */
  1154. void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
  1155. {
  1156. struct device_node *np;
  1157. int i, j;
  1158. /* default to level-triggered */
  1159. memset(senses, 1, max - off);
  1160. for (np = allnodes; np != 0; np = np->allnext) {
  1161. for (j = 0; j < np->n_intrs; j++) {
  1162. i = np->intrs[j].line;
  1163. if (i >= off && i < max)
  1164. senses[i-off] = np->intrs[j].sense ?
  1165. IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE :
  1166. IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE;
  1167. }
  1168. }
  1169. }
  1170. /**
  1171. * Construct and return a list of the device_nodes with a given name.
  1172. */
  1173. struct device_node *
  1174. find_devices(const char *name)
  1175. {
  1176. struct device_node *head, **prevp, *np;
  1177. prevp = &head;
  1178. for (np = allnodes; np != 0; np = np->allnext) {
  1179. if (np->name != 0 && strcasecmp(np->name, name) == 0) {
  1180. *prevp = np;
  1181. prevp = &np->next;
  1182. }
  1183. }
  1184. *prevp = NULL;
  1185. return head;
  1186. }
  1187. EXPORT_SYMBOL(find_devices);
  1188. /**
  1189. * Construct and return a list of the device_nodes with a given type.
  1190. */
  1191. struct device_node *
  1192. find_type_devices(const char *type)
  1193. {
  1194. struct device_node *head, **prevp, *np;
  1195. prevp = &head;
  1196. for (np = allnodes; np != 0; np = np->allnext) {
  1197. if (np->type != 0 && strcasecmp(np->type, type) == 0) {
  1198. *prevp = np;
  1199. prevp = &np->next;
  1200. }
  1201. }
  1202. *prevp = NULL;
  1203. return head;
  1204. }
  1205. EXPORT_SYMBOL(find_type_devices);
  1206. /**
  1207. * Returns all nodes linked together
  1208. */
  1209. struct device_node *
  1210. find_all_nodes(void)
  1211. {
  1212. struct device_node *head, **prevp, *np;
  1213. prevp = &head;
  1214. for (np = allnodes; np != 0; np = np->allnext) {
  1215. *prevp = np;
  1216. prevp = &np->next;
  1217. }
  1218. *prevp = NULL;
  1219. return head;
  1220. }
  1221. EXPORT_SYMBOL(find_all_nodes);
  1222. /** Checks if the given "compat" string matches one of the strings in
  1223. * the device's "compatible" property
  1224. */
  1225. int
  1226. device_is_compatible(struct device_node *device, const char *compat)
  1227. {
  1228. const char* cp;
  1229. int cplen, l;
  1230. cp = (char *) get_property(device, "compatible", &cplen);
  1231. if (cp == NULL)
  1232. return 0;
  1233. while (cplen > 0) {
  1234. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  1235. return 1;
  1236. l = strlen(cp) + 1;
  1237. cp += l;
  1238. cplen -= l;
  1239. }
  1240. return 0;
  1241. }
  1242. EXPORT_SYMBOL(device_is_compatible);
  1243. /**
  1244. * Indicates whether the root node has a given value in its
  1245. * compatible property.
  1246. */
  1247. int
  1248. machine_is_compatible(const char *compat)
  1249. {
  1250. struct device_node *root;
  1251. int rc = 0;
  1252. root = of_find_node_by_path("/");
  1253. if (root) {
  1254. rc = device_is_compatible(root, compat);
  1255. of_node_put(root);
  1256. }
  1257. return rc;
  1258. }
  1259. EXPORT_SYMBOL(machine_is_compatible);
  1260. /**
  1261. * Construct and return a list of the device_nodes with a given type
  1262. * and compatible property.
  1263. */
  1264. struct device_node *
  1265. find_compatible_devices(const char *type, const char *compat)
  1266. {
  1267. struct device_node *head, **prevp, *np;
  1268. prevp = &head;
  1269. for (np = allnodes; np != 0; np = np->allnext) {
  1270. if (type != NULL
  1271. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1272. continue;
  1273. if (device_is_compatible(np, compat)) {
  1274. *prevp = np;
  1275. prevp = &np->next;
  1276. }
  1277. }
  1278. *prevp = NULL;
  1279. return head;
  1280. }
  1281. EXPORT_SYMBOL(find_compatible_devices);
  1282. /**
  1283. * Find the device_node with a given full_name.
  1284. */
  1285. struct device_node *
  1286. find_path_device(const char *path)
  1287. {
  1288. struct device_node *np;
  1289. for (np = allnodes; np != 0; np = np->allnext)
  1290. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
  1291. return np;
  1292. return NULL;
  1293. }
  1294. EXPORT_SYMBOL(find_path_device);
  1295. /*******
  1296. *
  1297. * New implementation of the OF "find" APIs, return a refcounted
  1298. * object, call of_node_put() when done. The device tree and list
  1299. * are protected by a rw_lock.
  1300. *
  1301. * Note that property management will need some locking as well,
  1302. * this isn't dealt with yet.
  1303. *
  1304. *******/
  1305. /**
  1306. * of_find_node_by_name - Find a node by its "name" property
  1307. * @from: The node to start searching from or NULL, the node
  1308. * you pass will not be searched, only the next one
  1309. * will; typically, you pass what the previous call
  1310. * returned. of_node_put() will be called on it
  1311. * @name: The name string to match against
  1312. *
  1313. * Returns a node pointer with refcount incremented, use
  1314. * of_node_put() on it when done.
  1315. */
  1316. struct device_node *of_find_node_by_name(struct device_node *from,
  1317. const char *name)
  1318. {
  1319. struct device_node *np;
  1320. read_lock(&devtree_lock);
  1321. np = from ? from->allnext : allnodes;
  1322. for (; np != 0; np = np->allnext)
  1323. if (np->name != 0 && strcasecmp(np->name, name) == 0
  1324. && of_node_get(np))
  1325. break;
  1326. if (from)
  1327. of_node_put(from);
  1328. read_unlock(&devtree_lock);
  1329. return np;
  1330. }
  1331. EXPORT_SYMBOL(of_find_node_by_name);
  1332. /**
  1333. * of_find_node_by_type - Find a node by its "device_type" property
  1334. * @from: The node to start searching from or NULL, the node
  1335. * you pass will not be searched, only the next one
  1336. * will; typically, you pass what the previous call
  1337. * returned. of_node_put() will be called on it
  1338. * @name: The type string to match against
  1339. *
  1340. * Returns a node pointer with refcount incremented, use
  1341. * of_node_put() on it when done.
  1342. */
  1343. struct device_node *of_find_node_by_type(struct device_node *from,
  1344. const char *type)
  1345. {
  1346. struct device_node *np;
  1347. read_lock(&devtree_lock);
  1348. np = from ? from->allnext : allnodes;
  1349. for (; np != 0; np = np->allnext)
  1350. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1351. && of_node_get(np))
  1352. break;
  1353. if (from)
  1354. of_node_put(from);
  1355. read_unlock(&devtree_lock);
  1356. return np;
  1357. }
  1358. EXPORT_SYMBOL(of_find_node_by_type);
  1359. /**
  1360. * of_find_compatible_node - Find a node based on type and one of the
  1361. * tokens in its "compatible" property
  1362. * @from: The node to start searching from or NULL, the node
  1363. * you pass will not be searched, only the next one
  1364. * will; typically, you pass what the previous call
  1365. * returned. of_node_put() will be called on it
  1366. * @type: The type string to match "device_type" or NULL to ignore
  1367. * @compatible: The string to match to one of the tokens in the device
  1368. * "compatible" list.
  1369. *
  1370. * Returns a node pointer with refcount incremented, use
  1371. * of_node_put() on it when done.
  1372. */
  1373. struct device_node *of_find_compatible_node(struct device_node *from,
  1374. const char *type, const char *compatible)
  1375. {
  1376. struct device_node *np;
  1377. read_lock(&devtree_lock);
  1378. np = from ? from->allnext : allnodes;
  1379. for (; np != 0; np = np->allnext) {
  1380. if (type != NULL
  1381. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1382. continue;
  1383. if (device_is_compatible(np, compatible) && of_node_get(np))
  1384. break;
  1385. }
  1386. if (from)
  1387. of_node_put(from);
  1388. read_unlock(&devtree_lock);
  1389. return np;
  1390. }
  1391. EXPORT_SYMBOL(of_find_compatible_node);
  1392. /**
  1393. * of_find_node_by_path - Find a node matching a full OF path
  1394. * @path: The full path to match
  1395. *
  1396. * Returns a node pointer with refcount incremented, use
  1397. * of_node_put() on it when done.
  1398. */
  1399. struct device_node *of_find_node_by_path(const char *path)
  1400. {
  1401. struct device_node *np = allnodes;
  1402. read_lock(&devtree_lock);
  1403. for (; np != 0; np = np->allnext) {
  1404. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1405. && of_node_get(np))
  1406. break;
  1407. }
  1408. read_unlock(&devtree_lock);
  1409. return np;
  1410. }
  1411. EXPORT_SYMBOL(of_find_node_by_path);
  1412. /**
  1413. * of_find_node_by_phandle - Find a node given a phandle
  1414. * @handle: phandle of the node to find
  1415. *
  1416. * Returns a node pointer with refcount incremented, use
  1417. * of_node_put() on it when done.
  1418. */
  1419. struct device_node *of_find_node_by_phandle(phandle handle)
  1420. {
  1421. struct device_node *np;
  1422. read_lock(&devtree_lock);
  1423. for (np = allnodes; np != 0; np = np->allnext)
  1424. if (np->linux_phandle == handle)
  1425. break;
  1426. if (np)
  1427. of_node_get(np);
  1428. read_unlock(&devtree_lock);
  1429. return np;
  1430. }
  1431. EXPORT_SYMBOL(of_find_node_by_phandle);
  1432. /**
  1433. * of_find_all_nodes - Get next node in global list
  1434. * @prev: Previous node or NULL to start iteration
  1435. * of_node_put() will be called on it
  1436. *
  1437. * Returns a node pointer with refcount incremented, use
  1438. * of_node_put() on it when done.
  1439. */
  1440. struct device_node *of_find_all_nodes(struct device_node *prev)
  1441. {
  1442. struct device_node *np;
  1443. read_lock(&devtree_lock);
  1444. np = prev ? prev->allnext : allnodes;
  1445. for (; np != 0; np = np->allnext)
  1446. if (of_node_get(np))
  1447. break;
  1448. if (prev)
  1449. of_node_put(prev);
  1450. read_unlock(&devtree_lock);
  1451. return np;
  1452. }
  1453. EXPORT_SYMBOL(of_find_all_nodes);
  1454. /**
  1455. * of_get_parent - Get a node's parent if any
  1456. * @node: Node to get parent
  1457. *
  1458. * Returns a node pointer with refcount incremented, use
  1459. * of_node_put() on it when done.
  1460. */
  1461. struct device_node *of_get_parent(const struct device_node *node)
  1462. {
  1463. struct device_node *np;
  1464. if (!node)
  1465. return NULL;
  1466. read_lock(&devtree_lock);
  1467. np = of_node_get(node->parent);
  1468. read_unlock(&devtree_lock);
  1469. return np;
  1470. }
  1471. EXPORT_SYMBOL(of_get_parent);
  1472. /**
  1473. * of_get_next_child - Iterate a node childs
  1474. * @node: parent node
  1475. * @prev: previous child of the parent node, or NULL to get first
  1476. *
  1477. * Returns a node pointer with refcount incremented, use
  1478. * of_node_put() on it when done.
  1479. */
  1480. struct device_node *of_get_next_child(const struct device_node *node,
  1481. struct device_node *prev)
  1482. {
  1483. struct device_node *next;
  1484. read_lock(&devtree_lock);
  1485. next = prev ? prev->sibling : node->child;
  1486. for (; next != 0; next = next->sibling)
  1487. if (of_node_get(next))
  1488. break;
  1489. if (prev)
  1490. of_node_put(prev);
  1491. read_unlock(&devtree_lock);
  1492. return next;
  1493. }
  1494. EXPORT_SYMBOL(of_get_next_child);
  1495. /**
  1496. * of_node_get - Increment refcount of a node
  1497. * @node: Node to inc refcount, NULL is supported to
  1498. * simplify writing of callers
  1499. *
  1500. * Returns node.
  1501. */
  1502. struct device_node *of_node_get(struct device_node *node)
  1503. {
  1504. if (node)
  1505. kref_get(&node->kref);
  1506. return node;
  1507. }
  1508. EXPORT_SYMBOL(of_node_get);
  1509. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1510. {
  1511. return container_of(kref, struct device_node, kref);
  1512. }
  1513. /**
  1514. * of_node_release - release a dynamically allocated node
  1515. * @kref: kref element of the node to be released
  1516. *
  1517. * In of_node_put() this function is passed to kref_put()
  1518. * as the destructor.
  1519. */
  1520. static void of_node_release(struct kref *kref)
  1521. {
  1522. struct device_node *node = kref_to_device_node(kref);
  1523. struct property *prop = node->properties;
  1524. if (!OF_IS_DYNAMIC(node))
  1525. return;
  1526. while (prop) {
  1527. struct property *next = prop->next;
  1528. kfree(prop->name);
  1529. kfree(prop->value);
  1530. kfree(prop);
  1531. prop = next;
  1532. }
  1533. kfree(node->intrs);
  1534. kfree(node->addrs);
  1535. kfree(node->full_name);
  1536. kfree(node);
  1537. }
  1538. /**
  1539. * of_node_put - Decrement refcount of a node
  1540. * @node: Node to dec refcount, NULL is supported to
  1541. * simplify writing of callers
  1542. *
  1543. */
  1544. void of_node_put(struct device_node *node)
  1545. {
  1546. if (node)
  1547. kref_put(&node->kref, of_node_release);
  1548. }
  1549. EXPORT_SYMBOL(of_node_put);
  1550. /*
  1551. * Fix up the uninitialized fields in a new device node:
  1552. * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
  1553. *
  1554. * A lot of boot-time code is duplicated here, because functions such
  1555. * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
  1556. * slab allocator.
  1557. *
  1558. * This should probably be split up into smaller chunks.
  1559. */
  1560. static int of_finish_dynamic_node(struct device_node *node,
  1561. unsigned long *unused1, int unused2,
  1562. int unused3, int unused4)
  1563. {
  1564. struct device_node *parent = of_get_parent(node);
  1565. int err = 0;
  1566. phandle *ibm_phandle;
  1567. node->name = get_property(node, "name", NULL);
  1568. node->type = get_property(node, "device_type", NULL);
  1569. if (!parent) {
  1570. err = -ENODEV;
  1571. goto out;
  1572. }
  1573. /* We don't support that function on PowerMac, at least
  1574. * not yet
  1575. */
  1576. if (systemcfg->platform == PLATFORM_POWERMAC)
  1577. return -ENODEV;
  1578. /* fix up new node's linux_phandle field */
  1579. if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
  1580. node->linux_phandle = *ibm_phandle;
  1581. out:
  1582. of_node_put(parent);
  1583. return err;
  1584. }
  1585. /*
  1586. * Plug a device node into the tree and global list.
  1587. */
  1588. void of_attach_node(struct device_node *np)
  1589. {
  1590. write_lock(&devtree_lock);
  1591. np->sibling = np->parent->child;
  1592. np->allnext = allnodes;
  1593. np->parent->child = np;
  1594. allnodes = np;
  1595. write_unlock(&devtree_lock);
  1596. }
  1597. /*
  1598. * "Unplug" a node from the device tree. The caller must hold
  1599. * a reference to the node. The memory associated with the node
  1600. * is not freed until its refcount goes to zero.
  1601. */
  1602. void of_detach_node(const struct device_node *np)
  1603. {
  1604. struct device_node *parent;
  1605. write_lock(&devtree_lock);
  1606. parent = np->parent;
  1607. if (allnodes == np)
  1608. allnodes = np->allnext;
  1609. else {
  1610. struct device_node *prev;
  1611. for (prev = allnodes;
  1612. prev->allnext != np;
  1613. prev = prev->allnext)
  1614. ;
  1615. prev->allnext = np->allnext;
  1616. }
  1617. if (parent->child == np)
  1618. parent->child = np->sibling;
  1619. else {
  1620. struct device_node *prevsib;
  1621. for (prevsib = np->parent->child;
  1622. prevsib->sibling != np;
  1623. prevsib = prevsib->sibling)
  1624. ;
  1625. prevsib->sibling = np->sibling;
  1626. }
  1627. write_unlock(&devtree_lock);
  1628. }
  1629. static int prom_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
  1630. {
  1631. int err;
  1632. switch (action) {
  1633. case PSERIES_RECONFIG_ADD:
  1634. err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
  1635. if (err < 0) {
  1636. printk(KERN_ERR "finish_node returned %d\n", err);
  1637. err = NOTIFY_BAD;
  1638. }
  1639. break;
  1640. default:
  1641. err = NOTIFY_DONE;
  1642. break;
  1643. }
  1644. return err;
  1645. }
  1646. static struct notifier_block prom_reconfig_nb = {
  1647. .notifier_call = prom_reconfig_notifier,
  1648. .priority = 10, /* This one needs to run first */
  1649. };
  1650. static int __init prom_reconfig_setup(void)
  1651. {
  1652. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1653. }
  1654. __initcall(prom_reconfig_setup);
  1655. /*
  1656. * Find a property with a given name for a given node
  1657. * and return the value.
  1658. */
  1659. unsigned char *
  1660. get_property(struct device_node *np, const char *name, int *lenp)
  1661. {
  1662. struct property *pp;
  1663. for (pp = np->properties; pp != 0; pp = pp->next)
  1664. if (strcmp(pp->name, name) == 0) {
  1665. if (lenp != 0)
  1666. *lenp = pp->length;
  1667. return pp->value;
  1668. }
  1669. return NULL;
  1670. }
  1671. EXPORT_SYMBOL(get_property);
  1672. /*
  1673. * Add a property to a node
  1674. */
  1675. void
  1676. prom_add_property(struct device_node* np, struct property* prop)
  1677. {
  1678. struct property **next = &np->properties;
  1679. prop->next = NULL;
  1680. while (*next)
  1681. next = &(*next)->next;
  1682. *next = prop;
  1683. }
  1684. #if 0
  1685. void
  1686. print_properties(struct device_node *np)
  1687. {
  1688. struct property *pp;
  1689. char *cp;
  1690. int i, n;
  1691. for (pp = np->properties; pp != 0; pp = pp->next) {
  1692. printk(KERN_INFO "%s", pp->name);
  1693. for (i = strlen(pp->name); i < 16; ++i)
  1694. printk(" ");
  1695. cp = (char *) pp->value;
  1696. for (i = pp->length; i > 0; --i, ++cp)
  1697. if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
  1698. || (i == 1 && *cp != 0))
  1699. break;
  1700. if (i == 0 && pp->length > 1) {
  1701. /* looks like a string */
  1702. printk(" %s\n", (char *) pp->value);
  1703. } else {
  1704. /* dump it in hex */
  1705. n = pp->length;
  1706. if (n > 64)
  1707. n = 64;
  1708. if (pp->length % 4 == 0) {
  1709. unsigned int *p = (unsigned int *) pp->value;
  1710. n /= 4;
  1711. for (i = 0; i < n; ++i) {
  1712. if (i != 0 && (i % 4) == 0)
  1713. printk("\n ");
  1714. printk(" %08x", *p++);
  1715. }
  1716. } else {
  1717. unsigned char *bp = pp->value;
  1718. for (i = 0; i < n; ++i) {
  1719. if (i != 0 && (i % 16) == 0)
  1720. printk("\n ");
  1721. printk(" %02x", *bp++);
  1722. }
  1723. }
  1724. printk("\n");
  1725. if (pp->length > 64)
  1726. printk(" ... (length = %d)\n",
  1727. pp->length);
  1728. }
  1729. }
  1730. }
  1731. #endif