pci.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  1. /*
  2. * Common pmac/prep/chrp pci routines. -- Cort
  3. */
  4. #include <linux/config.h>
  5. #include <linux/kernel.h>
  6. #include <linux/pci.h>
  7. #include <linux/delay.h>
  8. #include <linux/string.h>
  9. #include <linux/init.h>
  10. #include <linux/capability.h>
  11. #include <linux/sched.h>
  12. #include <linux/errno.h>
  13. #include <linux/bootmem.h>
  14. #include <asm/processor.h>
  15. #include <asm/io.h>
  16. #include <asm/prom.h>
  17. #include <asm/sections.h>
  18. #include <asm/pci-bridge.h>
  19. #include <asm/byteorder.h>
  20. #include <asm/irq.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/machdep.h>
  23. #undef DEBUG
  24. #ifdef DEBUG
  25. #define DBG(x...) printk(x)
  26. #else
  27. #define DBG(x...)
  28. #endif
  29. unsigned long isa_io_base = 0;
  30. unsigned long isa_mem_base = 0;
  31. unsigned long pci_dram_offset = 0;
  32. int pcibios_assign_bus_offset = 1;
  33. void pcibios_make_OF_bus_map(void);
  34. static int pci_relocate_bridge_resource(struct pci_bus *bus, int i);
  35. static int probe_resource(struct pci_bus *parent, struct resource *pr,
  36. struct resource *res, struct resource **conflict);
  37. static void update_bridge_base(struct pci_bus *bus, int i);
  38. static void pcibios_fixup_resources(struct pci_dev* dev);
  39. static void fixup_broken_pcnet32(struct pci_dev* dev);
  40. static int reparent_resources(struct resource *parent, struct resource *res);
  41. static void fixup_rev1_53c810(struct pci_dev* dev);
  42. static void fixup_cpc710_pci64(struct pci_dev* dev);
  43. #ifdef CONFIG_PPC_OF
  44. static u8* pci_to_OF_bus_map;
  45. #endif
  46. /* By default, we don't re-assign bus numbers. We do this only on
  47. * some pmacs
  48. */
  49. int pci_assign_all_buses;
  50. struct pci_controller* hose_head;
  51. struct pci_controller** hose_tail = &hose_head;
  52. static int pci_bus_count;
  53. static void
  54. fixup_broken_pcnet32(struct pci_dev* dev)
  55. {
  56. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  57. dev->vendor = PCI_VENDOR_ID_AMD;
  58. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  59. }
  60. }
  61. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  62. static void
  63. fixup_cpc710_pci64(struct pci_dev* dev)
  64. {
  65. /* Hide the PCI64 BARs from the kernel as their content doesn't
  66. * fit well in the resource management
  67. */
  68. dev->resource[0].start = dev->resource[0].end = 0;
  69. dev->resource[0].flags = 0;
  70. dev->resource[1].start = dev->resource[1].end = 0;
  71. dev->resource[1].flags = 0;
  72. }
  73. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
  74. static void
  75. pcibios_fixup_resources(struct pci_dev *dev)
  76. {
  77. struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
  78. int i;
  79. unsigned long offset;
  80. if (!hose) {
  81. printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
  82. return;
  83. }
  84. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  85. struct resource *res = dev->resource + i;
  86. if (!res->flags)
  87. continue;
  88. if (res->end == 0xffffffff) {
  89. DBG("PCI:%s Resource %d [%08lx-%08lx] is unassigned\n",
  90. pci_name(dev), i, res->start, res->end);
  91. res->end -= res->start;
  92. res->start = 0;
  93. res->flags |= IORESOURCE_UNSET;
  94. continue;
  95. }
  96. offset = 0;
  97. if (res->flags & IORESOURCE_MEM) {
  98. offset = hose->pci_mem_offset;
  99. } else if (res->flags & IORESOURCE_IO) {
  100. offset = (unsigned long) hose->io_base_virt
  101. - isa_io_base;
  102. }
  103. if (offset != 0) {
  104. res->start += offset;
  105. res->end += offset;
  106. #ifdef DEBUG
  107. printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n",
  108. i, res->flags, pci_name(dev),
  109. res->start - offset, res->start);
  110. #endif
  111. }
  112. }
  113. /* Call machine specific resource fixup */
  114. if (ppc_md.pcibios_fixup_resources)
  115. ppc_md.pcibios_fixup_resources(dev);
  116. }
  117. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  118. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  119. struct resource *res)
  120. {
  121. unsigned long offset = 0;
  122. struct pci_controller *hose = dev->sysdata;
  123. if (hose && res->flags & IORESOURCE_IO)
  124. offset = (unsigned long)hose->io_base_virt - isa_io_base;
  125. else if (hose && res->flags & IORESOURCE_MEM)
  126. offset = hose->pci_mem_offset;
  127. region->start = res->start - offset;
  128. region->end = res->end - offset;
  129. }
  130. EXPORT_SYMBOL(pcibios_resource_to_bus);
  131. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  132. struct pci_bus_region *region)
  133. {
  134. unsigned long offset = 0;
  135. struct pci_controller *hose = dev->sysdata;
  136. if (hose && res->flags & IORESOURCE_IO)
  137. offset = (unsigned long)hose->io_base_virt - isa_io_base;
  138. else if (hose && res->flags & IORESOURCE_MEM)
  139. offset = hose->pci_mem_offset;
  140. res->start = region->start + offset;
  141. res->end = region->end + offset;
  142. }
  143. EXPORT_SYMBOL(pcibios_bus_to_resource);
  144. /*
  145. * We need to avoid collisions with `mirrored' VGA ports
  146. * and other strange ISA hardware, so we always want the
  147. * addresses to be allocated in the 0x000-0x0ff region
  148. * modulo 0x400.
  149. *
  150. * Why? Because some silly external IO cards only decode
  151. * the low 10 bits of the IO address. The 0x00-0xff region
  152. * is reserved for motherboard devices that decode all 16
  153. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  154. * but we want to try to avoid allocating at 0x2900-0x2bff
  155. * which might have be mirrored at 0x0100-0x03ff..
  156. */
  157. void pcibios_align_resource(void *data, struct resource *res, unsigned long size,
  158. unsigned long align)
  159. {
  160. struct pci_dev *dev = data;
  161. if (res->flags & IORESOURCE_IO) {
  162. unsigned long start = res->start;
  163. if (size > 0x100) {
  164. printk(KERN_ERR "PCI: I/O Region %s/%d too large"
  165. " (%ld bytes)\n", pci_name(dev),
  166. dev->resource - res, size);
  167. }
  168. if (start & 0x300) {
  169. start = (start + 0x3ff) & ~0x3ff;
  170. res->start = start;
  171. }
  172. }
  173. }
  174. EXPORT_SYMBOL(pcibios_align_resource);
  175. /*
  176. * Handle resources of PCI devices. If the world were perfect, we could
  177. * just allocate all the resource regions and do nothing more. It isn't.
  178. * On the other hand, we cannot just re-allocate all devices, as it would
  179. * require us to know lots of host bridge internals. So we attempt to
  180. * keep as much of the original configuration as possible, but tweak it
  181. * when it's found to be wrong.
  182. *
  183. * Known BIOS problems we have to work around:
  184. * - I/O or memory regions not configured
  185. * - regions configured, but not enabled in the command register
  186. * - bogus I/O addresses above 64K used
  187. * - expansion ROMs left enabled (this may sound harmless, but given
  188. * the fact the PCI specs explicitly allow address decoders to be
  189. * shared between expansion ROMs and other resource regions, it's
  190. * at least dangerous)
  191. *
  192. * Our solution:
  193. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  194. * This gives us fixed barriers on where we can allocate.
  195. * (2) Allocate resources for all enabled devices. If there is
  196. * a collision, just mark the resource as unallocated. Also
  197. * disable expansion ROMs during this step.
  198. * (3) Try to allocate resources for disabled devices. If the
  199. * resources were assigned correctly, everything goes well,
  200. * if they weren't, they won't disturb allocation of other
  201. * resources.
  202. * (4) Assign new addresses to resources which were either
  203. * not configured at all or misconfigured. If explicitly
  204. * requested by the user, configure expansion ROM address
  205. * as well.
  206. */
  207. static void __init
  208. pcibios_allocate_bus_resources(struct list_head *bus_list)
  209. {
  210. struct pci_bus *bus;
  211. int i;
  212. struct resource *res, *pr;
  213. /* Depth-First Search on bus tree */
  214. list_for_each_entry(bus, bus_list, node) {
  215. for (i = 0; i < 4; ++i) {
  216. if ((res = bus->resource[i]) == NULL || !res->flags
  217. || res->start > res->end)
  218. continue;
  219. if (bus->parent == NULL)
  220. pr = (res->flags & IORESOURCE_IO)?
  221. &ioport_resource: &iomem_resource;
  222. else {
  223. pr = pci_find_parent_resource(bus->self, res);
  224. if (pr == res) {
  225. /* this happens when the generic PCI
  226. * code (wrongly) decides that this
  227. * bridge is transparent -- paulus
  228. */
  229. continue;
  230. }
  231. }
  232. DBG("PCI: bridge rsrc %lx..%lx (%lx), parent %p\n",
  233. res->start, res->end, res->flags, pr);
  234. if (pr) {
  235. if (request_resource(pr, res) == 0)
  236. continue;
  237. /*
  238. * Must be a conflict with an existing entry.
  239. * Move that entry (or entries) under the
  240. * bridge resource and try again.
  241. */
  242. if (reparent_resources(pr, res) == 0)
  243. continue;
  244. }
  245. printk(KERN_ERR "PCI: Cannot allocate resource region "
  246. "%d of PCI bridge %d\n", i, bus->number);
  247. if (pci_relocate_bridge_resource(bus, i))
  248. bus->resource[i] = NULL;
  249. }
  250. pcibios_allocate_bus_resources(&bus->children);
  251. }
  252. }
  253. /*
  254. * Reparent resource children of pr that conflict with res
  255. * under res, and make res replace those children.
  256. */
  257. static int __init
  258. reparent_resources(struct resource *parent, struct resource *res)
  259. {
  260. struct resource *p, **pp;
  261. struct resource **firstpp = NULL;
  262. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  263. if (p->end < res->start)
  264. continue;
  265. if (res->end < p->start)
  266. break;
  267. if (p->start < res->start || p->end > res->end)
  268. return -1; /* not completely contained */
  269. if (firstpp == NULL)
  270. firstpp = pp;
  271. }
  272. if (firstpp == NULL)
  273. return -1; /* didn't find any conflicting entries? */
  274. res->parent = parent;
  275. res->child = *firstpp;
  276. res->sibling = *pp;
  277. *firstpp = res;
  278. *pp = NULL;
  279. for (p = res->child; p != NULL; p = p->sibling) {
  280. p->parent = res;
  281. DBG(KERN_INFO "PCI: reparented %s [%lx..%lx] under %s\n",
  282. p->name, p->start, p->end, res->name);
  283. }
  284. return 0;
  285. }
  286. /*
  287. * A bridge has been allocated a range which is outside the range
  288. * of its parent bridge, so it needs to be moved.
  289. */
  290. static int __init
  291. pci_relocate_bridge_resource(struct pci_bus *bus, int i)
  292. {
  293. struct resource *res, *pr, *conflict;
  294. unsigned long try, size;
  295. int j;
  296. struct pci_bus *parent = bus->parent;
  297. if (parent == NULL) {
  298. /* shouldn't ever happen */
  299. printk(KERN_ERR "PCI: can't move host bridge resource\n");
  300. return -1;
  301. }
  302. res = bus->resource[i];
  303. if (res == NULL)
  304. return -1;
  305. pr = NULL;
  306. for (j = 0; j < 4; j++) {
  307. struct resource *r = parent->resource[j];
  308. if (!r)
  309. continue;
  310. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  311. continue;
  312. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
  313. pr = r;
  314. break;
  315. }
  316. if (res->flags & IORESOURCE_PREFETCH)
  317. pr = r;
  318. }
  319. if (pr == NULL)
  320. return -1;
  321. size = res->end - res->start;
  322. if (pr->start > pr->end || size > pr->end - pr->start)
  323. return -1;
  324. try = pr->end;
  325. for (;;) {
  326. res->start = try - size;
  327. res->end = try;
  328. if (probe_resource(bus->parent, pr, res, &conflict) == 0)
  329. break;
  330. if (conflict->start <= pr->start + size)
  331. return -1;
  332. try = conflict->start - 1;
  333. }
  334. if (request_resource(pr, res)) {
  335. DBG(KERN_ERR "PCI: huh? couldn't move to %lx..%lx\n",
  336. res->start, res->end);
  337. return -1; /* "can't happen" */
  338. }
  339. update_bridge_base(bus, i);
  340. printk(KERN_INFO "PCI: bridge %d resource %d moved to %lx..%lx\n",
  341. bus->number, i, res->start, res->end);
  342. return 0;
  343. }
  344. static int __init
  345. probe_resource(struct pci_bus *parent, struct resource *pr,
  346. struct resource *res, struct resource **conflict)
  347. {
  348. struct pci_bus *bus;
  349. struct pci_dev *dev;
  350. struct resource *r;
  351. int i;
  352. for (r = pr->child; r != NULL; r = r->sibling) {
  353. if (r->end >= res->start && res->end >= r->start) {
  354. *conflict = r;
  355. return 1;
  356. }
  357. }
  358. list_for_each_entry(bus, &parent->children, node) {
  359. for (i = 0; i < 4; ++i) {
  360. if ((r = bus->resource[i]) == NULL)
  361. continue;
  362. if (!r->flags || r->start > r->end || r == res)
  363. continue;
  364. if (pci_find_parent_resource(bus->self, r) != pr)
  365. continue;
  366. if (r->end >= res->start && res->end >= r->start) {
  367. *conflict = r;
  368. return 1;
  369. }
  370. }
  371. }
  372. list_for_each_entry(dev, &parent->devices, bus_list) {
  373. for (i = 0; i < 6; ++i) {
  374. r = &dev->resource[i];
  375. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  376. continue;
  377. if (pci_find_parent_resource(dev, r) != pr)
  378. continue;
  379. if (r->end >= res->start && res->end >= r->start) {
  380. *conflict = r;
  381. return 1;
  382. }
  383. }
  384. }
  385. return 0;
  386. }
  387. static void __init
  388. update_bridge_base(struct pci_bus *bus, int i)
  389. {
  390. struct resource *res = bus->resource[i];
  391. u8 io_base_lo, io_limit_lo;
  392. u16 mem_base, mem_limit;
  393. u16 cmd;
  394. unsigned long start, end, off;
  395. struct pci_dev *dev = bus->self;
  396. struct pci_controller *hose = dev->sysdata;
  397. if (!hose) {
  398. printk("update_bridge_base: no hose?\n");
  399. return;
  400. }
  401. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  402. pci_write_config_word(dev, PCI_COMMAND,
  403. cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
  404. if (res->flags & IORESOURCE_IO) {
  405. off = (unsigned long) hose->io_base_virt - isa_io_base;
  406. start = res->start - off;
  407. end = res->end - off;
  408. io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
  409. io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
  410. if (end > 0xffff) {
  411. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  412. start >> 16);
  413. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  414. end >> 16);
  415. io_base_lo |= PCI_IO_RANGE_TYPE_32;
  416. } else
  417. io_base_lo |= PCI_IO_RANGE_TYPE_16;
  418. pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
  419. pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
  420. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  421. == IORESOURCE_MEM) {
  422. off = hose->pci_mem_offset;
  423. mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  424. mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  425. pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
  426. pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
  427. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  428. == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
  429. off = hose->pci_mem_offset;
  430. mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
  431. mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
  432. pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
  433. pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
  434. } else {
  435. DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
  436. pci_name(dev), i, res->flags);
  437. }
  438. pci_write_config_word(dev, PCI_COMMAND, cmd);
  439. }
  440. static inline void alloc_resource(struct pci_dev *dev, int idx)
  441. {
  442. struct resource *pr, *r = &dev->resource[idx];
  443. DBG("PCI:%s: Resource %d: %08lx-%08lx (f=%lx)\n",
  444. pci_name(dev), idx, r->start, r->end, r->flags);
  445. pr = pci_find_parent_resource(dev, r);
  446. if (!pr || request_resource(pr, r) < 0) {
  447. printk(KERN_ERR "PCI: Cannot allocate resource region %d"
  448. " of device %s\n", idx, pci_name(dev));
  449. if (pr)
  450. DBG("PCI: parent is %p: %08lx-%08lx (f=%lx)\n",
  451. pr, pr->start, pr->end, pr->flags);
  452. /* We'll assign a new address later */
  453. r->flags |= IORESOURCE_UNSET;
  454. r->end -= r->start;
  455. r->start = 0;
  456. }
  457. }
  458. static void __init
  459. pcibios_allocate_resources(int pass)
  460. {
  461. struct pci_dev *dev = NULL;
  462. int idx, disabled;
  463. u16 command;
  464. struct resource *r;
  465. while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  466. pci_read_config_word(dev, PCI_COMMAND, &command);
  467. for (idx = 0; idx < 6; idx++) {
  468. r = &dev->resource[idx];
  469. if (r->parent) /* Already allocated */
  470. continue;
  471. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  472. continue; /* Not assigned at all */
  473. if (r->flags & IORESOURCE_IO)
  474. disabled = !(command & PCI_COMMAND_IO);
  475. else
  476. disabled = !(command & PCI_COMMAND_MEMORY);
  477. if (pass == disabled)
  478. alloc_resource(dev, idx);
  479. }
  480. if (pass)
  481. continue;
  482. r = &dev->resource[PCI_ROM_RESOURCE];
  483. if (r->flags & IORESOURCE_ROM_ENABLE) {
  484. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  485. u32 reg;
  486. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  487. r->flags &= ~IORESOURCE_ROM_ENABLE;
  488. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  489. pci_write_config_dword(dev, dev->rom_base_reg,
  490. reg & ~PCI_ROM_ADDRESS_ENABLE);
  491. }
  492. }
  493. }
  494. static void __init
  495. pcibios_assign_resources(void)
  496. {
  497. struct pci_dev *dev = NULL;
  498. int idx;
  499. struct resource *r;
  500. while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  501. int class = dev->class >> 8;
  502. /* Don't touch classless devices and host bridges */
  503. if (!class || class == PCI_CLASS_BRIDGE_HOST)
  504. continue;
  505. for (idx = 0; idx < 6; idx++) {
  506. r = &dev->resource[idx];
  507. /*
  508. * We shall assign a new address to this resource,
  509. * either because the BIOS (sic) forgot to do so
  510. * or because we have decided the old address was
  511. * unusable for some reason.
  512. */
  513. if ((r->flags & IORESOURCE_UNSET) && r->end &&
  514. (!ppc_md.pcibios_enable_device_hook ||
  515. !ppc_md.pcibios_enable_device_hook(dev, 1))) {
  516. r->flags &= ~IORESOURCE_UNSET;
  517. pci_assign_resource(dev, idx);
  518. }
  519. }
  520. #if 0 /* don't assign ROMs */
  521. r = &dev->resource[PCI_ROM_RESOURCE];
  522. r->end -= r->start;
  523. r->start = 0;
  524. if (r->end)
  525. pci_assign_resource(dev, PCI_ROM_RESOURCE);
  526. #endif
  527. }
  528. }
  529. int
  530. pcibios_enable_resources(struct pci_dev *dev, int mask)
  531. {
  532. u16 cmd, old_cmd;
  533. int idx;
  534. struct resource *r;
  535. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  536. old_cmd = cmd;
  537. for (idx=0; idx<6; idx++) {
  538. /* Only set up the requested stuff */
  539. if (!(mask & (1<<idx)))
  540. continue;
  541. r = &dev->resource[idx];
  542. if (r->flags & IORESOURCE_UNSET) {
  543. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  544. return -EINVAL;
  545. }
  546. if (r->flags & IORESOURCE_IO)
  547. cmd |= PCI_COMMAND_IO;
  548. if (r->flags & IORESOURCE_MEM)
  549. cmd |= PCI_COMMAND_MEMORY;
  550. }
  551. if (dev->resource[PCI_ROM_RESOURCE].start)
  552. cmd |= PCI_COMMAND_MEMORY;
  553. if (cmd != old_cmd) {
  554. printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  555. pci_write_config_word(dev, PCI_COMMAND, cmd);
  556. }
  557. return 0;
  558. }
  559. static int next_controller_index;
  560. struct pci_controller * __init
  561. pcibios_alloc_controller(void)
  562. {
  563. struct pci_controller *hose;
  564. hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
  565. memset(hose, 0, sizeof(struct pci_controller));
  566. *hose_tail = hose;
  567. hose_tail = &hose->next;
  568. hose->index = next_controller_index++;
  569. return hose;
  570. }
  571. #ifdef CONFIG_PPC_OF
  572. /*
  573. * Functions below are used on OpenFirmware machines.
  574. */
  575. static void
  576. make_one_node_map(struct device_node* node, u8 pci_bus)
  577. {
  578. int *bus_range;
  579. int len;
  580. if (pci_bus >= pci_bus_count)
  581. return;
  582. bus_range = (int *) get_property(node, "bus-range", &len);
  583. if (bus_range == NULL || len < 2 * sizeof(int)) {
  584. printk(KERN_WARNING "Can't get bus-range for %s, "
  585. "assuming it starts at 0\n", node->full_name);
  586. pci_to_OF_bus_map[pci_bus] = 0;
  587. } else
  588. pci_to_OF_bus_map[pci_bus] = bus_range[0];
  589. for (node=node->child; node != 0;node = node->sibling) {
  590. struct pci_dev* dev;
  591. unsigned int *class_code, *reg;
  592. class_code = (unsigned int *) get_property(node, "class-code", NULL);
  593. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  594. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  595. continue;
  596. reg = (unsigned int *)get_property(node, "reg", NULL);
  597. if (!reg)
  598. continue;
  599. dev = pci_find_slot(pci_bus, ((reg[0] >> 8) & 0xff));
  600. if (!dev || !dev->subordinate)
  601. continue;
  602. make_one_node_map(node, dev->subordinate->number);
  603. }
  604. }
  605. void
  606. pcibios_make_OF_bus_map(void)
  607. {
  608. int i;
  609. struct pci_controller* hose;
  610. u8* of_prop_map;
  611. pci_to_OF_bus_map = (u8*)kmalloc(pci_bus_count, GFP_KERNEL);
  612. if (!pci_to_OF_bus_map) {
  613. printk(KERN_ERR "Can't allocate OF bus map !\n");
  614. return;
  615. }
  616. /* We fill the bus map with invalid values, that helps
  617. * debugging.
  618. */
  619. for (i=0; i<pci_bus_count; i++)
  620. pci_to_OF_bus_map[i] = 0xff;
  621. /* For each hose, we begin searching bridges */
  622. for(hose=hose_head; hose; hose=hose->next) {
  623. struct device_node* node;
  624. node = (struct device_node *)hose->arch_data;
  625. if (!node)
  626. continue;
  627. make_one_node_map(node, hose->first_busno);
  628. }
  629. of_prop_map = get_property(find_path_device("/"), "pci-OF-bus-map", NULL);
  630. if (of_prop_map)
  631. memcpy(of_prop_map, pci_to_OF_bus_map, pci_bus_count);
  632. #ifdef DEBUG
  633. printk("PCI->OF bus map:\n");
  634. for (i=0; i<pci_bus_count; i++) {
  635. if (pci_to_OF_bus_map[i] == 0xff)
  636. continue;
  637. printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
  638. }
  639. #endif
  640. }
  641. typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
  642. static struct device_node*
  643. scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
  644. {
  645. struct device_node* sub_node;
  646. for (; node != 0;node = node->sibling) {
  647. unsigned int *class_code;
  648. if (filter(node, data))
  649. return node;
  650. /* For PCI<->PCI bridges or CardBus bridges, we go down
  651. * Note: some OFs create a parent node "multifunc-device" as
  652. * a fake root for all functions of a multi-function device,
  653. * we go down them as well.
  654. */
  655. class_code = (unsigned int *) get_property(node, "class-code", NULL);
  656. if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  657. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
  658. strcmp(node->name, "multifunc-device"))
  659. continue;
  660. sub_node = scan_OF_pci_childs(node->child, filter, data);
  661. if (sub_node)
  662. return sub_node;
  663. }
  664. return NULL;
  665. }
  666. static int
  667. scan_OF_pci_childs_iterator(struct device_node* node, void* data)
  668. {
  669. unsigned int *reg;
  670. u8* fdata = (u8*)data;
  671. reg = (unsigned int *) get_property(node, "reg", NULL);
  672. if (reg && ((reg[0] >> 8) & 0xff) == fdata[1]
  673. && ((reg[0] >> 16) & 0xff) == fdata[0])
  674. return 1;
  675. return 0;
  676. }
  677. static struct device_node*
  678. scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
  679. {
  680. u8 filter_data[2] = {bus, dev_fn};
  681. return scan_OF_pci_childs(node, scan_OF_pci_childs_iterator, filter_data);
  682. }
  683. /*
  684. * Scans the OF tree for a device node matching a PCI device
  685. */
  686. struct device_node *
  687. pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
  688. {
  689. struct pci_controller *hose;
  690. struct device_node *node;
  691. int busnr;
  692. if (!have_of)
  693. return NULL;
  694. /* Lookup the hose */
  695. busnr = bus->number;
  696. hose = pci_bus_to_hose(busnr);
  697. if (!hose)
  698. return NULL;
  699. /* Check it has an OF node associated */
  700. node = (struct device_node *) hose->arch_data;
  701. if (!node)
  702. return NULL;
  703. /* Fixup bus number according to what OF think it is. */
  704. #ifdef CONFIG_PPC_PMAC
  705. /* The G5 need a special case here. Basically, we don't remap all
  706. * busses on it so we don't create the pci-OF-map. However, we do
  707. * remap the AGP bus and so have to deal with it. A future better
  708. * fix has to be done by making the remapping per-host and always
  709. * filling the pci_to_OF map. --BenH
  710. */
  711. if (_machine == _MACH_Pmac && busnr >= 0xf0)
  712. busnr -= 0xf0;
  713. else
  714. #endif
  715. if (pci_to_OF_bus_map)
  716. busnr = pci_to_OF_bus_map[busnr];
  717. if (busnr == 0xff)
  718. return NULL;
  719. /* Now, lookup childs of the hose */
  720. return scan_OF_childs_for_device(node->child, busnr, devfn);
  721. }
  722. EXPORT_SYMBOL(pci_busdev_to_OF_node);
  723. struct device_node*
  724. pci_device_to_OF_node(struct pci_dev *dev)
  725. {
  726. return pci_busdev_to_OF_node(dev->bus, dev->devfn);
  727. }
  728. EXPORT_SYMBOL(pci_device_to_OF_node);
  729. /* This routine is meant to be used early during boot, when the
  730. * PCI bus numbers have not yet been assigned, and you need to
  731. * issue PCI config cycles to an OF device.
  732. * It could also be used to "fix" RTAS config cycles if you want
  733. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  734. * config cycles.
  735. */
  736. struct pci_controller*
  737. pci_find_hose_for_OF_device(struct device_node* node)
  738. {
  739. if (!have_of)
  740. return NULL;
  741. while(node) {
  742. struct pci_controller* hose;
  743. for (hose=hose_head;hose;hose=hose->next)
  744. if (hose->arch_data == node)
  745. return hose;
  746. node=node->parent;
  747. }
  748. return NULL;
  749. }
  750. static int
  751. find_OF_pci_device_filter(struct device_node* node, void* data)
  752. {
  753. return ((void *)node == data);
  754. }
  755. /*
  756. * Returns the PCI device matching a given OF node
  757. */
  758. int
  759. pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
  760. {
  761. unsigned int *reg;
  762. struct pci_controller* hose;
  763. struct pci_dev* dev = NULL;
  764. if (!have_of)
  765. return -ENODEV;
  766. /* Make sure it's really a PCI device */
  767. hose = pci_find_hose_for_OF_device(node);
  768. if (!hose || !hose->arch_data)
  769. return -ENODEV;
  770. if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
  771. find_OF_pci_device_filter, (void *)node))
  772. return -ENODEV;
  773. reg = (unsigned int *) get_property(node, "reg", NULL);
  774. if (!reg)
  775. return -ENODEV;
  776. *bus = (reg[0] >> 16) & 0xff;
  777. *devfn = ((reg[0] >> 8) & 0xff);
  778. /* Ok, here we need some tweak. If we have already renumbered
  779. * all busses, we can't rely on the OF bus number any more.
  780. * the pci_to_OF_bus_map is not enough as several PCI busses
  781. * may match the same OF bus number.
  782. */
  783. if (!pci_to_OF_bus_map)
  784. return 0;
  785. while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  786. if (pci_to_OF_bus_map[dev->bus->number] != *bus)
  787. continue;
  788. if (dev->devfn != *devfn)
  789. continue;
  790. *bus = dev->bus->number;
  791. return 0;
  792. }
  793. return -ENODEV;
  794. }
  795. EXPORT_SYMBOL(pci_device_from_OF_node);
  796. void __init
  797. pci_process_bridge_OF_ranges(struct pci_controller *hose,
  798. struct device_node *dev, int primary)
  799. {
  800. static unsigned int static_lc_ranges[256] __initdata;
  801. unsigned int *dt_ranges, *lc_ranges, *ranges, *prev;
  802. unsigned int size;
  803. int rlen = 0, orig_rlen;
  804. int memno = 0;
  805. struct resource *res;
  806. int np, na = prom_n_addr_cells(dev);
  807. np = na + 5;
  808. /* First we try to merge ranges to fix a problem with some pmacs
  809. * that can have more than 3 ranges, fortunately using contiguous
  810. * addresses -- BenH
  811. */
  812. dt_ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
  813. if (!dt_ranges)
  814. return;
  815. /* Sanity check, though hopefully that never happens */
  816. if (rlen > sizeof(static_lc_ranges)) {
  817. printk(KERN_WARNING "OF ranges property too large !\n");
  818. rlen = sizeof(static_lc_ranges);
  819. }
  820. lc_ranges = static_lc_ranges;
  821. memcpy(lc_ranges, dt_ranges, rlen);
  822. orig_rlen = rlen;
  823. /* Let's work on a copy of the "ranges" property instead of damaging
  824. * the device-tree image in memory
  825. */
  826. ranges = lc_ranges;
  827. prev = NULL;
  828. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  829. if (prev) {
  830. if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
  831. (prev[2] + prev[na+4]) == ranges[2] &&
  832. (prev[na+2] + prev[na+4]) == ranges[na+2]) {
  833. prev[na+4] += ranges[na+4];
  834. ranges[0] = 0;
  835. ranges += np;
  836. continue;
  837. }
  838. }
  839. prev = ranges;
  840. ranges += np;
  841. }
  842. /*
  843. * The ranges property is laid out as an array of elements,
  844. * each of which comprises:
  845. * cells 0 - 2: a PCI address
  846. * cells 3 or 3+4: a CPU physical address
  847. * (size depending on dev->n_addr_cells)
  848. * cells 4+5 or 5+6: the size of the range
  849. */
  850. ranges = lc_ranges;
  851. rlen = orig_rlen;
  852. while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
  853. res = NULL;
  854. size = ranges[na+4];
  855. switch (ranges[0] >> 24) {
  856. case 1: /* I/O space */
  857. if (ranges[2] != 0)
  858. break;
  859. hose->io_base_phys = ranges[na+2];
  860. /* limit I/O space to 16MB */
  861. if (size > 0x01000000)
  862. size = 0x01000000;
  863. hose->io_base_virt = ioremap(ranges[na+2], size);
  864. if (primary)
  865. isa_io_base = (unsigned long) hose->io_base_virt;
  866. res = &hose->io_resource;
  867. res->flags = IORESOURCE_IO;
  868. res->start = ranges[2];
  869. break;
  870. case 2: /* memory space */
  871. memno = 0;
  872. if (ranges[1] == 0 && ranges[2] == 0
  873. && ranges[na+4] <= (16 << 20)) {
  874. /* 1st 16MB, i.e. ISA memory area */
  875. if (primary)
  876. isa_mem_base = ranges[na+2];
  877. memno = 1;
  878. }
  879. while (memno < 3 && hose->mem_resources[memno].flags)
  880. ++memno;
  881. if (memno == 0)
  882. hose->pci_mem_offset = ranges[na+2] - ranges[2];
  883. if (memno < 3) {
  884. res = &hose->mem_resources[memno];
  885. res->flags = IORESOURCE_MEM;
  886. res->start = ranges[na+2];
  887. }
  888. break;
  889. }
  890. if (res != NULL) {
  891. res->name = dev->full_name;
  892. res->end = res->start + size - 1;
  893. res->parent = NULL;
  894. res->sibling = NULL;
  895. res->child = NULL;
  896. }
  897. ranges += np;
  898. }
  899. }
  900. /* We create the "pci-OF-bus-map" property now so it appears in the
  901. * /proc device tree
  902. */
  903. void __init
  904. pci_create_OF_bus_map(void)
  905. {
  906. struct property* of_prop;
  907. of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
  908. if (of_prop && find_path_device("/")) {
  909. memset(of_prop, -1, sizeof(struct property) + 256);
  910. of_prop->name = "pci-OF-bus-map";
  911. of_prop->length = 256;
  912. of_prop->value = (unsigned char *)&of_prop[1];
  913. prom_add_property(find_path_device("/"), of_prop);
  914. }
  915. }
  916. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  917. {
  918. struct pci_dev *pdev;
  919. struct device_node *np;
  920. pdev = to_pci_dev (dev);
  921. np = pci_device_to_OF_node(pdev);
  922. if (np == NULL || np->full_name == NULL)
  923. return 0;
  924. return sprintf(buf, "%s", np->full_name);
  925. }
  926. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  927. #else /* CONFIG_PPC_OF */
  928. void pcibios_make_OF_bus_map(void)
  929. {
  930. }
  931. #endif /* CONFIG_PPC_OF */
  932. /* Add sysfs properties */
  933. void pcibios_add_platform_entries(struct pci_dev *pdev)
  934. {
  935. #ifdef CONFIG_PPC_OF
  936. device_create_file(&pdev->dev, &dev_attr_devspec);
  937. #endif /* CONFIG_PPC_OF */
  938. }
  939. #ifdef CONFIG_PPC_PMAC
  940. /*
  941. * This set of routines checks for PCI<->PCI bridges that have closed
  942. * IO resources and have child devices. It tries to re-open an IO
  943. * window on them.
  944. *
  945. * This is a _temporary_ fix to workaround a problem with Apple's OF
  946. * closing IO windows on P2P bridges when the OF drivers of cards
  947. * below this bridge don't claim any IO range (typically ATI or
  948. * Adaptec).
  949. *
  950. * A more complete fix would be to use drivers/pci/setup-bus.c, which
  951. * involves a working pcibios_fixup_pbus_ranges(), some more care about
  952. * ordering when creating the host bus resources, and maybe a few more
  953. * minor tweaks
  954. */
  955. /* Initialize bridges with base/limit values we have collected */
  956. static void __init
  957. do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
  958. {
  959. struct pci_dev *bridge = bus->self;
  960. struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
  961. u32 l;
  962. u16 w;
  963. struct resource res;
  964. if (bus->resource[0] == NULL)
  965. return;
  966. res = *(bus->resource[0]);
  967. DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
  968. res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
  969. res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
  970. DBG(" IO window: %08lx-%08lx\n", res.start, res.end);
  971. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  972. pci_read_config_dword(bridge, PCI_IO_BASE, &l);
  973. l &= 0xffff000f;
  974. l |= (res.start >> 8) & 0x00f0;
  975. l |= res.end & 0xf000;
  976. pci_write_config_dword(bridge, PCI_IO_BASE, l);
  977. if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  978. l = (res.start >> 16) | (res.end & 0xffff0000);
  979. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
  980. }
  981. pci_read_config_word(bridge, PCI_COMMAND, &w);
  982. w |= PCI_COMMAND_IO;
  983. pci_write_config_word(bridge, PCI_COMMAND, w);
  984. #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
  985. if (enable_vga) {
  986. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
  987. w |= PCI_BRIDGE_CTL_VGA;
  988. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
  989. }
  990. #endif
  991. }
  992. /* This function is pretty basic and actually quite broken for the
  993. * general case, it's enough for us right now though. It's supposed
  994. * to tell us if we need to open an IO range at all or not and what
  995. * size.
  996. */
  997. static int __init
  998. check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
  999. {
  1000. struct pci_dev *dev;
  1001. int i;
  1002. int rc = 0;
  1003. #define push_end(res, size) do { unsigned long __sz = (size) ; \
  1004. res->end = ((res->end + __sz) / (__sz + 1)) * (__sz + 1) + __sz; \
  1005. } while (0)
  1006. list_for_each_entry(dev, &bus->devices, bus_list) {
  1007. u16 class = dev->class >> 8;
  1008. if (class == PCI_CLASS_DISPLAY_VGA ||
  1009. class == PCI_CLASS_NOT_DEFINED_VGA)
  1010. *found_vga = 1;
  1011. if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
  1012. rc |= check_for_io_childs(dev->subordinate, res, found_vga);
  1013. if (class == PCI_CLASS_BRIDGE_CARDBUS)
  1014. push_end(res, 0xfff);
  1015. for (i=0; i<PCI_NUM_RESOURCES; i++) {
  1016. struct resource *r;
  1017. unsigned long r_size;
  1018. if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
  1019. && i >= PCI_BRIDGE_RESOURCES)
  1020. continue;
  1021. r = &dev->resource[i];
  1022. r_size = r->end - r->start;
  1023. if (r_size < 0xfff)
  1024. r_size = 0xfff;
  1025. if (r->flags & IORESOURCE_IO && (r_size) != 0) {
  1026. rc = 1;
  1027. push_end(res, r_size);
  1028. }
  1029. }
  1030. }
  1031. return rc;
  1032. }
  1033. /* Here we scan all P2P bridges of a given level that have a closed
  1034. * IO window. Note that the test for the presence of a VGA card should
  1035. * be improved to take into account already configured P2P bridges,
  1036. * currently, we don't see them and might end up configuring 2 bridges
  1037. * with VGA pass through enabled
  1038. */
  1039. static void __init
  1040. do_fixup_p2p_level(struct pci_bus *bus)
  1041. {
  1042. struct pci_bus *b;
  1043. int i, parent_io;
  1044. int has_vga = 0;
  1045. for (parent_io=0; parent_io<4; parent_io++)
  1046. if (bus->resource[parent_io]
  1047. && bus->resource[parent_io]->flags & IORESOURCE_IO)
  1048. break;
  1049. if (parent_io >= 4)
  1050. return;
  1051. list_for_each_entry(b, &bus->children, node) {
  1052. struct pci_dev *d = b->self;
  1053. struct pci_controller* hose = (struct pci_controller *)d->sysdata;
  1054. struct resource *res = b->resource[0];
  1055. struct resource tmp_res;
  1056. unsigned long max;
  1057. int found_vga = 0;
  1058. memset(&tmp_res, 0, sizeof(tmp_res));
  1059. tmp_res.start = bus->resource[parent_io]->start;
  1060. /* We don't let low addresses go through that closed P2P bridge, well,
  1061. * that may not be necessary but I feel safer that way
  1062. */
  1063. if (tmp_res.start == 0)
  1064. tmp_res.start = 0x1000;
  1065. if (!list_empty(&b->devices) && res && res->flags == 0 &&
  1066. res != bus->resource[parent_io] &&
  1067. (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
  1068. check_for_io_childs(b, &tmp_res, &found_vga)) {
  1069. u8 io_base_lo;
  1070. printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
  1071. if (found_vga) {
  1072. if (has_vga) {
  1073. printk(KERN_WARNING "Skipping VGA, already active"
  1074. " on bus segment\n");
  1075. found_vga = 0;
  1076. } else
  1077. has_vga = 1;
  1078. }
  1079. pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
  1080. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
  1081. max = ((unsigned long) hose->io_base_virt
  1082. - isa_io_base) + 0xffffffff;
  1083. else
  1084. max = ((unsigned long) hose->io_base_virt
  1085. - isa_io_base) + 0xffff;
  1086. *res = tmp_res;
  1087. res->flags = IORESOURCE_IO;
  1088. res->name = b->name;
  1089. /* Find a resource in the parent where we can allocate */
  1090. for (i = 0 ; i < 4; i++) {
  1091. struct resource *r = bus->resource[i];
  1092. if (!r)
  1093. continue;
  1094. if ((r->flags & IORESOURCE_IO) == 0)
  1095. continue;
  1096. DBG("Trying to allocate from %08lx, size %08lx from parent"
  1097. " res %d: %08lx -> %08lx\n",
  1098. res->start, res->end, i, r->start, r->end);
  1099. if (allocate_resource(r, res, res->end + 1, res->start, max,
  1100. res->end + 1, NULL, NULL) < 0) {
  1101. DBG("Failed !\n");
  1102. continue;
  1103. }
  1104. do_update_p2p_io_resource(b, found_vga);
  1105. break;
  1106. }
  1107. }
  1108. do_fixup_p2p_level(b);
  1109. }
  1110. }
  1111. static void
  1112. pcibios_fixup_p2p_bridges(void)
  1113. {
  1114. struct pci_bus *b;
  1115. list_for_each_entry(b, &pci_root_buses, node)
  1116. do_fixup_p2p_level(b);
  1117. }
  1118. #endif /* CONFIG_PPC_PMAC */
  1119. static int __init
  1120. pcibios_init(void)
  1121. {
  1122. struct pci_controller *hose;
  1123. struct pci_bus *bus;
  1124. int next_busno;
  1125. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  1126. /* Scan all of the recorded PCI controllers. */
  1127. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  1128. if (pci_assign_all_buses)
  1129. hose->first_busno = next_busno;
  1130. hose->last_busno = 0xff;
  1131. bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
  1132. hose->last_busno = bus->subordinate;
  1133. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  1134. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  1135. }
  1136. pci_bus_count = next_busno;
  1137. /* OpenFirmware based machines need a map of OF bus
  1138. * numbers vs. kernel bus numbers since we may have to
  1139. * remap them.
  1140. */
  1141. if (pci_assign_all_buses && have_of)
  1142. pcibios_make_OF_bus_map();
  1143. /* Do machine dependent PCI interrupt routing */
  1144. if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
  1145. pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
  1146. /* Call machine dependent fixup */
  1147. if (ppc_md.pcibios_fixup)
  1148. ppc_md.pcibios_fixup();
  1149. /* Allocate and assign resources */
  1150. pcibios_allocate_bus_resources(&pci_root_buses);
  1151. pcibios_allocate_resources(0);
  1152. pcibios_allocate_resources(1);
  1153. #ifdef CONFIG_PPC_PMAC
  1154. pcibios_fixup_p2p_bridges();
  1155. #endif /* CONFIG_PPC_PMAC */
  1156. pcibios_assign_resources();
  1157. /* Call machine dependent post-init code */
  1158. if (ppc_md.pcibios_after_init)
  1159. ppc_md.pcibios_after_init();
  1160. return 0;
  1161. }
  1162. subsys_initcall(pcibios_init);
  1163. unsigned char __init
  1164. common_swizzle(struct pci_dev *dev, unsigned char *pinp)
  1165. {
  1166. struct pci_controller *hose = dev->sysdata;
  1167. if (dev->bus->number != hose->first_busno) {
  1168. u8 pin = *pinp;
  1169. do {
  1170. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  1171. /* Move up the chain of bridges. */
  1172. dev = dev->bus->self;
  1173. } while (dev->bus->self);
  1174. *pinp = pin;
  1175. /* The slot is the idsel of the last bridge. */
  1176. }
  1177. return PCI_SLOT(dev->devfn);
  1178. }
  1179. unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
  1180. unsigned long start, unsigned long size)
  1181. {
  1182. return start;
  1183. }
  1184. void __init pcibios_fixup_bus(struct pci_bus *bus)
  1185. {
  1186. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  1187. unsigned long io_offset;
  1188. struct resource *res;
  1189. int i;
  1190. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  1191. if (bus->parent == NULL) {
  1192. /* This is a host bridge - fill in its resources */
  1193. hose->bus = bus;
  1194. bus->resource[0] = res = &hose->io_resource;
  1195. if (!res->flags) {
  1196. if (io_offset)
  1197. printk(KERN_ERR "I/O resource not set for host"
  1198. " bridge %d\n", hose->index);
  1199. res->start = 0;
  1200. res->end = IO_SPACE_LIMIT;
  1201. res->flags = IORESOURCE_IO;
  1202. }
  1203. res->start += io_offset;
  1204. res->end += io_offset;
  1205. for (i = 0; i < 3; ++i) {
  1206. res = &hose->mem_resources[i];
  1207. if (!res->flags) {
  1208. if (i > 0)
  1209. continue;
  1210. printk(KERN_ERR "Memory resource not set for "
  1211. "host bridge %d\n", hose->index);
  1212. res->start = hose->pci_mem_offset;
  1213. res->end = ~0U;
  1214. res->flags = IORESOURCE_MEM;
  1215. }
  1216. bus->resource[i+1] = res;
  1217. }
  1218. } else {
  1219. /* This is a subordinate bridge */
  1220. pci_read_bridge_bases(bus);
  1221. for (i = 0; i < 4; ++i) {
  1222. if ((res = bus->resource[i]) == NULL)
  1223. continue;
  1224. if (!res->flags)
  1225. continue;
  1226. if (io_offset && (res->flags & IORESOURCE_IO)) {
  1227. res->start += io_offset;
  1228. res->end += io_offset;
  1229. } else if (hose->pci_mem_offset
  1230. && (res->flags & IORESOURCE_MEM)) {
  1231. res->start += hose->pci_mem_offset;
  1232. res->end += hose->pci_mem_offset;
  1233. }
  1234. }
  1235. }
  1236. if (ppc_md.pcibios_fixup_bus)
  1237. ppc_md.pcibios_fixup_bus(bus);
  1238. }
  1239. char __init *pcibios_setup(char *str)
  1240. {
  1241. return str;
  1242. }
  1243. /* the next one is stolen from the alpha port... */
  1244. void __init
  1245. pcibios_update_irq(struct pci_dev *dev, int irq)
  1246. {
  1247. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  1248. /* XXX FIXME - update OF device tree node interrupt property */
  1249. }
  1250. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1251. {
  1252. u16 cmd, old_cmd;
  1253. int idx;
  1254. struct resource *r;
  1255. if (ppc_md.pcibios_enable_device_hook)
  1256. if (ppc_md.pcibios_enable_device_hook(dev, 0))
  1257. return -EINVAL;
  1258. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1259. old_cmd = cmd;
  1260. for (idx=0; idx<6; idx++) {
  1261. r = &dev->resource[idx];
  1262. if (r->flags & IORESOURCE_UNSET) {
  1263. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  1264. return -EINVAL;
  1265. }
  1266. if (r->flags & IORESOURCE_IO)
  1267. cmd |= PCI_COMMAND_IO;
  1268. if (r->flags & IORESOURCE_MEM)
  1269. cmd |= PCI_COMMAND_MEMORY;
  1270. }
  1271. if (cmd != old_cmd) {
  1272. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  1273. pci_name(dev), old_cmd, cmd);
  1274. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1275. }
  1276. return 0;
  1277. }
  1278. struct pci_controller*
  1279. pci_bus_to_hose(int bus)
  1280. {
  1281. struct pci_controller* hose = hose_head;
  1282. for (; hose; hose = hose->next)
  1283. if (bus >= hose->first_busno && bus <= hose->last_busno)
  1284. return hose;
  1285. return NULL;
  1286. }
  1287. void __iomem *
  1288. pci_bus_io_base(unsigned int bus)
  1289. {
  1290. struct pci_controller *hose;
  1291. hose = pci_bus_to_hose(bus);
  1292. if (!hose)
  1293. return NULL;
  1294. return hose->io_base_virt;
  1295. }
  1296. unsigned long
  1297. pci_bus_io_base_phys(unsigned int bus)
  1298. {
  1299. struct pci_controller *hose;
  1300. hose = pci_bus_to_hose(bus);
  1301. if (!hose)
  1302. return 0;
  1303. return hose->io_base_phys;
  1304. }
  1305. unsigned long
  1306. pci_bus_mem_base_phys(unsigned int bus)
  1307. {
  1308. struct pci_controller *hose;
  1309. hose = pci_bus_to_hose(bus);
  1310. if (!hose)
  1311. return 0;
  1312. return hose->pci_mem_offset;
  1313. }
  1314. unsigned long
  1315. pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
  1316. {
  1317. /* Hack alert again ! See comments in chrp_pci.c
  1318. */
  1319. struct pci_controller* hose =
  1320. (struct pci_controller *)pdev->sysdata;
  1321. if (hose && res->flags & IORESOURCE_MEM)
  1322. return res->start - hose->pci_mem_offset;
  1323. /* We may want to do something with IOs here... */
  1324. return res->start;
  1325. }
  1326. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  1327. unsigned long *offset,
  1328. enum pci_mmap_state mmap_state)
  1329. {
  1330. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1331. unsigned long io_offset = 0;
  1332. int i, res_bit;
  1333. if (hose == 0)
  1334. return NULL; /* should never happen */
  1335. /* If memory, add on the PCI bridge address offset */
  1336. if (mmap_state == pci_mmap_mem) {
  1337. *offset += hose->pci_mem_offset;
  1338. res_bit = IORESOURCE_MEM;
  1339. } else {
  1340. io_offset = hose->io_base_virt - ___IO_BASE;
  1341. *offset += io_offset;
  1342. res_bit = IORESOURCE_IO;
  1343. }
  1344. /*
  1345. * Check that the offset requested corresponds to one of the
  1346. * resources of the device.
  1347. */
  1348. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1349. struct resource *rp = &dev->resource[i];
  1350. int flags = rp->flags;
  1351. /* treat ROM as memory (should be already) */
  1352. if (i == PCI_ROM_RESOURCE)
  1353. flags |= IORESOURCE_MEM;
  1354. /* Active and same type? */
  1355. if ((flags & res_bit) == 0)
  1356. continue;
  1357. /* In the range of this resource? */
  1358. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  1359. continue;
  1360. /* found it! construct the final physical address */
  1361. if (mmap_state == pci_mmap_io)
  1362. *offset += hose->io_base_phys - io_offset;
  1363. return rp;
  1364. }
  1365. return NULL;
  1366. }
  1367. /*
  1368. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  1369. * device mapping.
  1370. */
  1371. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  1372. pgprot_t protection,
  1373. enum pci_mmap_state mmap_state,
  1374. int write_combine)
  1375. {
  1376. unsigned long prot = pgprot_val(protection);
  1377. /* Write combine is always 0 on non-memory space mappings. On
  1378. * memory space, if the user didn't pass 1, we check for a
  1379. * "prefetchable" resource. This is a bit hackish, but we use
  1380. * this to workaround the inability of /sysfs to provide a write
  1381. * combine bit
  1382. */
  1383. if (mmap_state != pci_mmap_mem)
  1384. write_combine = 0;
  1385. else if (write_combine == 0) {
  1386. if (rp->flags & IORESOURCE_PREFETCH)
  1387. write_combine = 1;
  1388. }
  1389. /* XXX would be nice to have a way to ask for write-through */
  1390. prot |= _PAGE_NO_CACHE;
  1391. if (write_combine)
  1392. prot &= ~_PAGE_GUARDED;
  1393. else
  1394. prot |= _PAGE_GUARDED;
  1395. printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
  1396. prot);
  1397. return __pgprot(prot);
  1398. }
  1399. /*
  1400. * This one is used by /dev/mem and fbdev who have no clue about the
  1401. * PCI device, it tries to find the PCI device first and calls the
  1402. * above routine
  1403. */
  1404. pgprot_t pci_phys_mem_access_prot(struct file *file,
  1405. unsigned long pfn,
  1406. unsigned long size,
  1407. pgprot_t protection)
  1408. {
  1409. struct pci_dev *pdev = NULL;
  1410. struct resource *found = NULL;
  1411. unsigned long prot = pgprot_val(protection);
  1412. unsigned long offset = pfn << PAGE_SHIFT;
  1413. int i;
  1414. if (page_is_ram(pfn))
  1415. return prot;
  1416. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  1417. for_each_pci_dev(pdev) {
  1418. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1419. struct resource *rp = &pdev->resource[i];
  1420. int flags = rp->flags;
  1421. /* Active and same type? */
  1422. if ((flags & IORESOURCE_MEM) == 0)
  1423. continue;
  1424. /* In the range of this resource? */
  1425. if (offset < (rp->start & PAGE_MASK) ||
  1426. offset > rp->end)
  1427. continue;
  1428. found = rp;
  1429. break;
  1430. }
  1431. if (found)
  1432. break;
  1433. }
  1434. if (found) {
  1435. if (found->flags & IORESOURCE_PREFETCH)
  1436. prot &= ~_PAGE_GUARDED;
  1437. pci_dev_put(pdev);
  1438. }
  1439. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  1440. return __pgprot(prot);
  1441. }
  1442. /*
  1443. * Perform the actual remap of the pages for a PCI device mapping, as
  1444. * appropriate for this architecture. The region in the process to map
  1445. * is described by vm_start and vm_end members of VMA, the base physical
  1446. * address is found in vm_pgoff.
  1447. * The pci device structure is provided so that architectures may make mapping
  1448. * decisions on a per-device or per-bus basis.
  1449. *
  1450. * Returns a negative error code on failure, zero on success.
  1451. */
  1452. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  1453. enum pci_mmap_state mmap_state,
  1454. int write_combine)
  1455. {
  1456. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1457. struct resource *rp;
  1458. int ret;
  1459. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  1460. if (rp == NULL)
  1461. return -EINVAL;
  1462. vma->vm_pgoff = offset >> PAGE_SHIFT;
  1463. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  1464. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  1465. vma->vm_page_prot,
  1466. mmap_state, write_combine);
  1467. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  1468. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  1469. return ret;
  1470. }
  1471. /* Obsolete functions. Should be removed once the symbios driver
  1472. * is fixed
  1473. */
  1474. unsigned long
  1475. phys_to_bus(unsigned long pa)
  1476. {
  1477. struct pci_controller *hose;
  1478. int i;
  1479. for (hose = hose_head; hose; hose = hose->next) {
  1480. for (i = 0; i < 3; ++i) {
  1481. if (pa >= hose->mem_resources[i].start
  1482. && pa <= hose->mem_resources[i].end) {
  1483. /*
  1484. * XXX the hose->pci_mem_offset really
  1485. * only applies to mem_resources[0].
  1486. * We need a way to store an offset for
  1487. * the others. -- paulus
  1488. */
  1489. if (i == 0)
  1490. pa -= hose->pci_mem_offset;
  1491. return pa;
  1492. }
  1493. }
  1494. }
  1495. /* hmmm, didn't find it */
  1496. return 0;
  1497. }
  1498. unsigned long
  1499. pci_phys_to_bus(unsigned long pa, int busnr)
  1500. {
  1501. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1502. if (!hose)
  1503. return pa;
  1504. return pa - hose->pci_mem_offset;
  1505. }
  1506. unsigned long
  1507. pci_bus_to_phys(unsigned int ba, int busnr)
  1508. {
  1509. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1510. if (!hose)
  1511. return ba;
  1512. return ba + hose->pci_mem_offset;
  1513. }
  1514. /* Provide information on locations of various I/O regions in physical
  1515. * memory. Do this on a per-card basis so that we choose the right
  1516. * root bridge.
  1517. * Note that the returned IO or memory base is a physical address
  1518. */
  1519. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  1520. {
  1521. struct pci_controller* hose;
  1522. long result = -EOPNOTSUPP;
  1523. /* Argh ! Please forgive me for that hack, but that's the
  1524. * simplest way to get existing XFree to not lockup on some
  1525. * G5 machines... So when something asks for bus 0 io base
  1526. * (bus 0 is HT root), we return the AGP one instead.
  1527. */
  1528. #ifdef CONFIG_PPC_PMAC
  1529. if (_machine == _MACH_Pmac && machine_is_compatible("MacRISC4"))
  1530. if (bus == 0)
  1531. bus = 0xf0;
  1532. #endif /* CONFIG_PPC_PMAC */
  1533. hose = pci_bus_to_hose(bus);
  1534. if (!hose)
  1535. return -ENODEV;
  1536. switch (which) {
  1537. case IOBASE_BRIDGE_NUMBER:
  1538. return (long)hose->first_busno;
  1539. case IOBASE_MEMORY:
  1540. return (long)hose->pci_mem_offset;
  1541. case IOBASE_IO:
  1542. return (long)hose->io_base_phys;
  1543. case IOBASE_ISA_IO:
  1544. return (long)isa_io_base;
  1545. case IOBASE_ISA_MEM:
  1546. return (long)isa_mem_base;
  1547. }
  1548. return result;
  1549. }
  1550. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1551. const struct resource *rsrc,
  1552. u64 *start, u64 *end)
  1553. {
  1554. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1555. unsigned long offset = 0;
  1556. if (hose == NULL)
  1557. return;
  1558. if (rsrc->flags & IORESOURCE_IO)
  1559. offset = ___IO_BASE - hose->io_base_virt + hose->io_base_phys;
  1560. *start = rsrc->start + offset;
  1561. *end = rsrc->end + offset;
  1562. }
  1563. void __init
  1564. pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
  1565. int flags, char *name)
  1566. {
  1567. res->start = start;
  1568. res->end = end;
  1569. res->flags = flags;
  1570. res->name = name;
  1571. res->parent = NULL;
  1572. res->sibling = NULL;
  1573. res->child = NULL;
  1574. }
  1575. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
  1576. {
  1577. unsigned long start = pci_resource_start(dev, bar);
  1578. unsigned long len = pci_resource_len(dev, bar);
  1579. unsigned long flags = pci_resource_flags(dev, bar);
  1580. if (!len)
  1581. return NULL;
  1582. if (max && len > max)
  1583. len = max;
  1584. if (flags & IORESOURCE_IO)
  1585. return ioport_map(start, len);
  1586. if (flags & IORESOURCE_MEM)
  1587. /* Not checking IORESOURCE_CACHEABLE because PPC does
  1588. * not currently distinguish between ioremap and
  1589. * ioremap_nocache.
  1590. */
  1591. return ioremap(start, len);
  1592. /* What? */
  1593. return NULL;
  1594. }
  1595. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  1596. {
  1597. /* Nothing to do */
  1598. }
  1599. EXPORT_SYMBOL(pci_iomap);
  1600. EXPORT_SYMBOL(pci_iounmap);
  1601. /*
  1602. * Null PCI config access functions, for the case when we can't
  1603. * find a hose.
  1604. */
  1605. #define NULL_PCI_OP(rw, size, type) \
  1606. static int \
  1607. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1608. { \
  1609. return PCIBIOS_DEVICE_NOT_FOUND; \
  1610. }
  1611. static int
  1612. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1613. int len, u32 *val)
  1614. {
  1615. return PCIBIOS_DEVICE_NOT_FOUND;
  1616. }
  1617. static int
  1618. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1619. int len, u32 val)
  1620. {
  1621. return PCIBIOS_DEVICE_NOT_FOUND;
  1622. }
  1623. static struct pci_ops null_pci_ops =
  1624. {
  1625. null_read_config,
  1626. null_write_config
  1627. };
  1628. /*
  1629. * These functions are used early on before PCI scanning is done
  1630. * and all of the pci_dev and pci_bus structures have been created.
  1631. */
  1632. static struct pci_bus *
  1633. fake_pci_bus(struct pci_controller *hose, int busnr)
  1634. {
  1635. static struct pci_bus bus;
  1636. if (hose == 0) {
  1637. hose = pci_bus_to_hose(busnr);
  1638. if (hose == 0)
  1639. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1640. }
  1641. bus.number = busnr;
  1642. bus.sysdata = hose;
  1643. bus.ops = hose? hose->ops: &null_pci_ops;
  1644. return &bus;
  1645. }
  1646. #define EARLY_PCI_OP(rw, size, type) \
  1647. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1648. int devfn, int offset, type value) \
  1649. { \
  1650. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1651. devfn, offset, value); \
  1652. }
  1653. EARLY_PCI_OP(read, byte, u8 *)
  1654. EARLY_PCI_OP(read, word, u16 *)
  1655. EARLY_PCI_OP(read, dword, u32 *)
  1656. EARLY_PCI_OP(write, byte, u8)
  1657. EARLY_PCI_OP(write, word, u16)
  1658. EARLY_PCI_OP(write, dword, u32)