prom.c 46 KB

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