pci_32.c 50 KB

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