pci_32.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  1. /*
  2. * Common pmac/prep/chrp pci routines. -- Cort
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/pci.h>
  6. #include <linux/delay.h>
  7. #include <linux/string.h>
  8. #include <linux/init.h>
  9. #include <linux/capability.h>
  10. #include <linux/sched.h>
  11. #include <linux/errno.h>
  12. #include <linux/bootmem.h>
  13. #include <linux/irq.h>
  14. #include <linux/list.h>
  15. #include <asm/processor.h>
  16. #include <asm/io.h>
  17. #include <asm/prom.h>
  18. #include <asm/sections.h>
  19. #include <asm/pci-bridge.h>
  20. #include <asm/byteorder.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 [%016llx-%016llx] 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: %llx -> %llx\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,
  157. resource_size_t size, resource_size_t align)
  158. {
  159. struct pci_dev *dev = data;
  160. if (res->flags & IORESOURCE_IO) {
  161. resource_size_t start = res->start;
  162. if (size > 0x100) {
  163. printk(KERN_ERR "PCI: I/O Region %s/%d too large"
  164. " (%lld bytes)\n", pci_name(dev),
  165. dev->resource - res, (unsigned long long)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 %llx..%llx (%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 [%llx..%llx] 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 %llx..%llx\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 %llx..%llx\n",
  340. bus->number, i, (unsigned long long)res->start,
  341. (unsigned long long)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. io_base_lo |= PCI_IO_RANGE_TYPE_32;
  412. else
  413. io_base_lo |= PCI_IO_RANGE_TYPE_16;
  414. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  415. start >> 16);
  416. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  417. end >> 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: %016llx-%016llx (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: %016llx-%016llx (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. for_each_pci_dev(dev) {
  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. for_each_pci_dev(dev) {
  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. const int *bus_range;
  579. int len;
  580. if (pci_bus >= pci_bus_count)
  581. return;
  582. bus_range = 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. const unsigned int *class_code, *reg;
  592. class_code = 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 = 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. struct property *map_prop;
  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. map_prop = of_find_property(find_path_device("/"),
  630. "pci-OF-bus-map", NULL);
  631. if (map_prop) {
  632. BUG_ON(pci_bus_count > map_prop->length);
  633. memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
  634. }
  635. #ifdef DEBUG
  636. printk("PCI->OF bus map:\n");
  637. for (i=0; i<pci_bus_count; i++) {
  638. if (pci_to_OF_bus_map[i] == 0xff)
  639. continue;
  640. printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
  641. }
  642. #endif
  643. }
  644. typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
  645. static struct device_node*
  646. scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
  647. {
  648. struct device_node* sub_node;
  649. for (; node != 0;node = node->sibling) {
  650. const unsigned int *class_code;
  651. if (filter(node, data))
  652. return node;
  653. /* For PCI<->PCI bridges or CardBus bridges, we go down
  654. * Note: some OFs create a parent node "multifunc-device" as
  655. * a fake root for all functions of a multi-function device,
  656. * we go down them as well.
  657. */
  658. class_code = get_property(node, "class-code", NULL);
  659. if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  660. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
  661. strcmp(node->name, "multifunc-device"))
  662. continue;
  663. sub_node = scan_OF_pci_childs(node->child, filter, data);
  664. if (sub_node)
  665. return sub_node;
  666. }
  667. return NULL;
  668. }
  669. static int
  670. scan_OF_pci_childs_iterator(struct device_node* node, void* data)
  671. {
  672. const unsigned int *reg;
  673. u8* fdata = (u8*)data;
  674. reg = get_property(node, "reg", NULL);
  675. if (reg && ((reg[0] >> 8) & 0xff) == fdata[1]
  676. && ((reg[0] >> 16) & 0xff) == fdata[0])
  677. return 1;
  678. return 0;
  679. }
  680. static struct device_node*
  681. scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
  682. {
  683. u8 filter_data[2] = {bus, dev_fn};
  684. return scan_OF_pci_childs(node, scan_OF_pci_childs_iterator, filter_data);
  685. }
  686. /*
  687. * Scans the OF tree for a device node matching a PCI device
  688. */
  689. struct device_node *
  690. pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
  691. {
  692. struct pci_controller *hose;
  693. struct device_node *node;
  694. int busnr;
  695. if (!have_of)
  696. return NULL;
  697. /* Lookup the hose */
  698. busnr = bus->number;
  699. hose = pci_bus_to_hose(busnr);
  700. if (!hose)
  701. return NULL;
  702. /* Check it has an OF node associated */
  703. node = (struct device_node *) hose->arch_data;
  704. if (!node)
  705. return NULL;
  706. /* Fixup bus number according to what OF think it is. */
  707. #ifdef CONFIG_PPC_PMAC
  708. /* The G5 need a special case here. Basically, we don't remap all
  709. * busses on it so we don't create the pci-OF-map. However, we do
  710. * remap the AGP bus and so have to deal with it. A future better
  711. * fix has to be done by making the remapping per-host and always
  712. * filling the pci_to_OF map. --BenH
  713. */
  714. if (machine_is(powermac) && busnr >= 0xf0)
  715. busnr -= 0xf0;
  716. else
  717. #endif
  718. if (pci_to_OF_bus_map)
  719. busnr = pci_to_OF_bus_map[busnr];
  720. if (busnr == 0xff)
  721. return NULL;
  722. /* Now, lookup childs of the hose */
  723. return scan_OF_childs_for_device(node->child, busnr, devfn);
  724. }
  725. EXPORT_SYMBOL(pci_busdev_to_OF_node);
  726. struct device_node*
  727. pci_device_to_OF_node(struct pci_dev *dev)
  728. {
  729. return pci_busdev_to_OF_node(dev->bus, dev->devfn);
  730. }
  731. EXPORT_SYMBOL(pci_device_to_OF_node);
  732. /* This routine is meant to be used early during boot, when the
  733. * PCI bus numbers have not yet been assigned, and you need to
  734. * issue PCI config cycles to an OF device.
  735. * It could also be used to "fix" RTAS config cycles if you want
  736. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  737. * config cycles.
  738. */
  739. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  740. {
  741. if (!have_of)
  742. return NULL;
  743. while(node) {
  744. struct pci_controller* hose;
  745. for (hose=hose_head;hose;hose=hose->next)
  746. if (hose->arch_data == node)
  747. return hose;
  748. node=node->parent;
  749. }
  750. return NULL;
  751. }
  752. static int
  753. find_OF_pci_device_filter(struct device_node* node, void* data)
  754. {
  755. return ((void *)node == data);
  756. }
  757. /*
  758. * Returns the PCI device matching a given OF node
  759. */
  760. int
  761. pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
  762. {
  763. const unsigned int *reg;
  764. struct pci_controller* hose;
  765. struct pci_dev* dev = NULL;
  766. if (!have_of)
  767. return -ENODEV;
  768. /* Make sure it's really a PCI device */
  769. hose = pci_find_hose_for_OF_device(node);
  770. if (!hose || !hose->arch_data)
  771. return -ENODEV;
  772. if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
  773. find_OF_pci_device_filter, (void *)node))
  774. return -ENODEV;
  775. reg = get_property(node, "reg", NULL);
  776. if (!reg)
  777. return -ENODEV;
  778. *bus = (reg[0] >> 16) & 0xff;
  779. *devfn = ((reg[0] >> 8) & 0xff);
  780. /* Ok, here we need some tweak. If we have already renumbered
  781. * all busses, we can't rely on the OF bus number any more.
  782. * the pci_to_OF_bus_map is not enough as several PCI busses
  783. * may match the same OF bus number.
  784. */
  785. if (!pci_to_OF_bus_map)
  786. return 0;
  787. for_each_pci_dev(dev)
  788. if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
  789. dev->devfn == *devfn) {
  790. *bus = dev->bus->number;
  791. pci_dev_put(dev);
  792. return 0;
  793. }
  794. return -ENODEV;
  795. }
  796. EXPORT_SYMBOL(pci_device_from_OF_node);
  797. void __init
  798. pci_process_bridge_OF_ranges(struct pci_controller *hose,
  799. struct device_node *dev, int primary)
  800. {
  801. static unsigned int static_lc_ranges[256] __initdata;
  802. const unsigned int *dt_ranges;
  803. unsigned int *lc_ranges, *ranges, *prev, size;
  804. int rlen = 0, orig_rlen;
  805. int memno = 0;
  806. struct resource *res;
  807. int np, na = prom_n_addr_cells(dev);
  808. np = na + 5;
  809. /* First we try to merge ranges to fix a problem with some pmacs
  810. * that can have more than 3 ranges, fortunately using contiguous
  811. * addresses -- BenH
  812. */
  813. dt_ranges = get_property(dev, "ranges", &rlen);
  814. if (!dt_ranges)
  815. return;
  816. /* Sanity check, though hopefully that never happens */
  817. if (rlen > sizeof(static_lc_ranges)) {
  818. printk(KERN_WARNING "OF ranges property too large !\n");
  819. rlen = sizeof(static_lc_ranges);
  820. }
  821. lc_ranges = static_lc_ranges;
  822. memcpy(lc_ranges, dt_ranges, rlen);
  823. orig_rlen = rlen;
  824. /* Let's work on a copy of the "ranges" property instead of damaging
  825. * the device-tree image in memory
  826. */
  827. ranges = lc_ranges;
  828. prev = NULL;
  829. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  830. if (prev) {
  831. if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
  832. (prev[2] + prev[na+4]) == ranges[2] &&
  833. (prev[na+2] + prev[na+4]) == ranges[na+2]) {
  834. prev[na+4] += ranges[na+4];
  835. ranges[0] = 0;
  836. ranges += np;
  837. continue;
  838. }
  839. }
  840. prev = ranges;
  841. ranges += np;
  842. }
  843. /*
  844. * The ranges property is laid out as an array of elements,
  845. * each of which comprises:
  846. * cells 0 - 2: a PCI address
  847. * cells 3 or 3+4: a CPU physical address
  848. * (size depending on dev->n_addr_cells)
  849. * cells 4+5 or 5+6: the size of the range
  850. */
  851. ranges = lc_ranges;
  852. rlen = orig_rlen;
  853. while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
  854. res = NULL;
  855. size = ranges[na+4];
  856. switch ((ranges[0] >> 24) & 0x3) {
  857. case 1: /* I/O space */
  858. if (ranges[2] != 0)
  859. break;
  860. hose->io_base_phys = ranges[na+2];
  861. /* limit I/O space to 16MB */
  862. if (size > 0x01000000)
  863. size = 0x01000000;
  864. hose->io_base_virt = ioremap(ranges[na+2], size);
  865. if (primary)
  866. isa_io_base = (unsigned long) hose->io_base_virt;
  867. res = &hose->io_resource;
  868. res->flags = IORESOURCE_IO;
  869. res->start = ranges[2];
  870. DBG("PCI: IO 0x%llx -> 0x%llx\n",
  871. res->start, res->start + size - 1);
  872. break;
  873. case 2: /* memory space */
  874. memno = 0;
  875. if (ranges[1] == 0 && ranges[2] == 0
  876. && ranges[na+4] <= (16 << 20)) {
  877. /* 1st 16MB, i.e. ISA memory area */
  878. if (primary)
  879. isa_mem_base = ranges[na+2];
  880. memno = 1;
  881. }
  882. while (memno < 3 && hose->mem_resources[memno].flags)
  883. ++memno;
  884. if (memno == 0)
  885. hose->pci_mem_offset = ranges[na+2] - ranges[2];
  886. if (memno < 3) {
  887. res = &hose->mem_resources[memno];
  888. res->flags = IORESOURCE_MEM;
  889. if(ranges[0] & 0x40000000)
  890. res->flags |= IORESOURCE_PREFETCH;
  891. res->start = ranges[na+2];
  892. DBG("PCI: MEM[%d] 0x%llx -> 0x%llx\n", memno,
  893. res->start, res->start + size - 1);
  894. }
  895. break;
  896. }
  897. if (res != NULL) {
  898. res->name = dev->full_name;
  899. res->end = res->start + size - 1;
  900. res->parent = NULL;
  901. res->sibling = NULL;
  902. res->child = NULL;
  903. }
  904. ranges += np;
  905. }
  906. }
  907. /* We create the "pci-OF-bus-map" property now so it appears in the
  908. * /proc device tree
  909. */
  910. void __init
  911. pci_create_OF_bus_map(void)
  912. {
  913. struct property* of_prop;
  914. of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
  915. if (of_prop && find_path_device("/")) {
  916. memset(of_prop, -1, sizeof(struct property) + 256);
  917. of_prop->name = "pci-OF-bus-map";
  918. of_prop->length = 256;
  919. of_prop->value = (unsigned char *)&of_prop[1];
  920. prom_add_property(find_path_device("/"), of_prop);
  921. }
  922. }
  923. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  924. {
  925. struct pci_dev *pdev;
  926. struct device_node *np;
  927. pdev = to_pci_dev (dev);
  928. np = pci_device_to_OF_node(pdev);
  929. if (np == NULL || np->full_name == NULL)
  930. return 0;
  931. return sprintf(buf, "%s", np->full_name);
  932. }
  933. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  934. #else /* CONFIG_PPC_OF */
  935. void pcibios_make_OF_bus_map(void)
  936. {
  937. }
  938. #endif /* CONFIG_PPC_OF */
  939. /* Add sysfs properties */
  940. void pcibios_add_platform_entries(struct pci_dev *pdev)
  941. {
  942. #ifdef CONFIG_PPC_OF
  943. device_create_file(&pdev->dev, &dev_attr_devspec);
  944. #endif /* CONFIG_PPC_OF */
  945. }
  946. #ifdef CONFIG_PPC_PMAC
  947. /*
  948. * This set of routines checks for PCI<->PCI bridges that have closed
  949. * IO resources and have child devices. It tries to re-open an IO
  950. * window on them.
  951. *
  952. * This is a _temporary_ fix to workaround a problem with Apple's OF
  953. * closing IO windows on P2P bridges when the OF drivers of cards
  954. * below this bridge don't claim any IO range (typically ATI or
  955. * Adaptec).
  956. *
  957. * A more complete fix would be to use drivers/pci/setup-bus.c, which
  958. * involves a working pcibios_fixup_pbus_ranges(), some more care about
  959. * ordering when creating the host bus resources, and maybe a few more
  960. * minor tweaks
  961. */
  962. /* Initialize bridges with base/limit values we have collected */
  963. static void __init
  964. do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
  965. {
  966. struct pci_dev *bridge = bus->self;
  967. struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
  968. u32 l;
  969. u16 w;
  970. struct resource res;
  971. if (bus->resource[0] == NULL)
  972. return;
  973. res = *(bus->resource[0]);
  974. DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
  975. res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
  976. res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
  977. DBG(" IO window: %016llx-%016llx\n", res.start, res.end);
  978. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  979. pci_read_config_dword(bridge, PCI_IO_BASE, &l);
  980. l &= 0xffff000f;
  981. l |= (res.start >> 8) & 0x00f0;
  982. l |= res.end & 0xf000;
  983. pci_write_config_dword(bridge, PCI_IO_BASE, l);
  984. if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  985. l = (res.start >> 16) | (res.end & 0xffff0000);
  986. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
  987. }
  988. pci_read_config_word(bridge, PCI_COMMAND, &w);
  989. w |= PCI_COMMAND_IO;
  990. pci_write_config_word(bridge, PCI_COMMAND, w);
  991. #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
  992. if (enable_vga) {
  993. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
  994. w |= PCI_BRIDGE_CTL_VGA;
  995. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
  996. }
  997. #endif
  998. }
  999. /* This function is pretty basic and actually quite broken for the
  1000. * general case, it's enough for us right now though. It's supposed
  1001. * to tell us if we need to open an IO range at all or not and what
  1002. * size.
  1003. */
  1004. static int __init
  1005. check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
  1006. {
  1007. struct pci_dev *dev;
  1008. int i;
  1009. int rc = 0;
  1010. #define push_end(res, mask) do { \
  1011. BUG_ON((mask+1) & mask); \
  1012. res->end = (res->end + mask) | mask; \
  1013. } while (0)
  1014. list_for_each_entry(dev, &bus->devices, bus_list) {
  1015. u16 class = dev->class >> 8;
  1016. if (class == PCI_CLASS_DISPLAY_VGA ||
  1017. class == PCI_CLASS_NOT_DEFINED_VGA)
  1018. *found_vga = 1;
  1019. if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
  1020. rc |= check_for_io_childs(dev->subordinate, res, found_vga);
  1021. if (class == PCI_CLASS_BRIDGE_CARDBUS)
  1022. push_end(res, 0xfff);
  1023. for (i=0; i<PCI_NUM_RESOURCES; i++) {
  1024. struct resource *r;
  1025. unsigned long r_size;
  1026. if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
  1027. && i >= PCI_BRIDGE_RESOURCES)
  1028. continue;
  1029. r = &dev->resource[i];
  1030. r_size = r->end - r->start;
  1031. if (r_size < 0xfff)
  1032. r_size = 0xfff;
  1033. if (r->flags & IORESOURCE_IO && (r_size) != 0) {
  1034. rc = 1;
  1035. push_end(res, r_size);
  1036. }
  1037. }
  1038. }
  1039. return rc;
  1040. }
  1041. /* Here we scan all P2P bridges of a given level that have a closed
  1042. * IO window. Note that the test for the presence of a VGA card should
  1043. * be improved to take into account already configured P2P bridges,
  1044. * currently, we don't see them and might end up configuring 2 bridges
  1045. * with VGA pass through enabled
  1046. */
  1047. static void __init
  1048. do_fixup_p2p_level(struct pci_bus *bus)
  1049. {
  1050. struct pci_bus *b;
  1051. int i, parent_io;
  1052. int has_vga = 0;
  1053. for (parent_io=0; parent_io<4; parent_io++)
  1054. if (bus->resource[parent_io]
  1055. && bus->resource[parent_io]->flags & IORESOURCE_IO)
  1056. break;
  1057. if (parent_io >= 4)
  1058. return;
  1059. list_for_each_entry(b, &bus->children, node) {
  1060. struct pci_dev *d = b->self;
  1061. struct pci_controller* hose = (struct pci_controller *)d->sysdata;
  1062. struct resource *res = b->resource[0];
  1063. struct resource tmp_res;
  1064. unsigned long max;
  1065. int found_vga = 0;
  1066. memset(&tmp_res, 0, sizeof(tmp_res));
  1067. tmp_res.start = bus->resource[parent_io]->start;
  1068. /* We don't let low addresses go through that closed P2P bridge, well,
  1069. * that may not be necessary but I feel safer that way
  1070. */
  1071. if (tmp_res.start == 0)
  1072. tmp_res.start = 0x1000;
  1073. if (!list_empty(&b->devices) && res && res->flags == 0 &&
  1074. res != bus->resource[parent_io] &&
  1075. (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
  1076. check_for_io_childs(b, &tmp_res, &found_vga)) {
  1077. u8 io_base_lo;
  1078. printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
  1079. if (found_vga) {
  1080. if (has_vga) {
  1081. printk(KERN_WARNING "Skipping VGA, already active"
  1082. " on bus segment\n");
  1083. found_vga = 0;
  1084. } else
  1085. has_vga = 1;
  1086. }
  1087. pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
  1088. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
  1089. max = ((unsigned long) hose->io_base_virt
  1090. - isa_io_base) + 0xffffffff;
  1091. else
  1092. max = ((unsigned long) hose->io_base_virt
  1093. - isa_io_base) + 0xffff;
  1094. *res = tmp_res;
  1095. res->flags = IORESOURCE_IO;
  1096. res->name = b->name;
  1097. /* Find a resource in the parent where we can allocate */
  1098. for (i = 0 ; i < 4; i++) {
  1099. struct resource *r = bus->resource[i];
  1100. if (!r)
  1101. continue;
  1102. if ((r->flags & IORESOURCE_IO) == 0)
  1103. continue;
  1104. DBG("Trying to allocate from %016llx, size %016llx from parent"
  1105. " res %d: %016llx -> %016llx\n",
  1106. res->start, res->end, i, r->start, r->end);
  1107. if (allocate_resource(r, res, res->end + 1, res->start, max,
  1108. res->end + 1, NULL, NULL) < 0) {
  1109. DBG("Failed !\n");
  1110. continue;
  1111. }
  1112. do_update_p2p_io_resource(b, found_vga);
  1113. break;
  1114. }
  1115. }
  1116. do_fixup_p2p_level(b);
  1117. }
  1118. }
  1119. static void
  1120. pcibios_fixup_p2p_bridges(void)
  1121. {
  1122. struct pci_bus *b;
  1123. list_for_each_entry(b, &pci_root_buses, node)
  1124. do_fixup_p2p_level(b);
  1125. }
  1126. #endif /* CONFIG_PPC_PMAC */
  1127. static int __init
  1128. pcibios_init(void)
  1129. {
  1130. struct pci_controller *hose;
  1131. struct pci_bus *bus;
  1132. int next_busno;
  1133. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  1134. /* Scan all of the recorded PCI controllers. */
  1135. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  1136. if (pci_assign_all_buses)
  1137. hose->first_busno = next_busno;
  1138. hose->last_busno = 0xff;
  1139. bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
  1140. hose->ops, hose);
  1141. if (bus)
  1142. pci_bus_add_devices(bus);
  1143. hose->last_busno = bus->subordinate;
  1144. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  1145. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  1146. }
  1147. pci_bus_count = next_busno;
  1148. /* OpenFirmware based machines need a map of OF bus
  1149. * numbers vs. kernel bus numbers since we may have to
  1150. * remap them.
  1151. */
  1152. if (pci_assign_all_buses && have_of)
  1153. pcibios_make_OF_bus_map();
  1154. /* Call machine dependent fixup */
  1155. if (ppc_md.pcibios_fixup)
  1156. ppc_md.pcibios_fixup();
  1157. /* Allocate and assign resources */
  1158. pcibios_allocate_bus_resources(&pci_root_buses);
  1159. pcibios_allocate_resources(0);
  1160. pcibios_allocate_resources(1);
  1161. #ifdef CONFIG_PPC_PMAC
  1162. pcibios_fixup_p2p_bridges();
  1163. #endif /* CONFIG_PPC_PMAC */
  1164. pcibios_assign_resources();
  1165. /* Call machine dependent post-init code */
  1166. if (ppc_md.pcibios_after_init)
  1167. ppc_md.pcibios_after_init();
  1168. return 0;
  1169. }
  1170. subsys_initcall(pcibios_init);
  1171. unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
  1172. unsigned long start, unsigned long size)
  1173. {
  1174. return start;
  1175. }
  1176. void __init pcibios_fixup_bus(struct pci_bus *bus)
  1177. {
  1178. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  1179. unsigned long io_offset;
  1180. struct resource *res;
  1181. struct pci_dev *dev;
  1182. int i;
  1183. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  1184. if (bus->parent == NULL) {
  1185. /* This is a host bridge - fill in its resources */
  1186. hose->bus = bus;
  1187. bus->resource[0] = res = &hose->io_resource;
  1188. if (!res->flags) {
  1189. if (io_offset)
  1190. printk(KERN_ERR "I/O resource not set for host"
  1191. " bridge %d\n", hose->index);
  1192. res->start = 0;
  1193. res->end = IO_SPACE_LIMIT;
  1194. res->flags = IORESOURCE_IO;
  1195. }
  1196. res->start += io_offset;
  1197. res->end += io_offset;
  1198. for (i = 0; i < 3; ++i) {
  1199. res = &hose->mem_resources[i];
  1200. if (!res->flags) {
  1201. if (i > 0)
  1202. continue;
  1203. printk(KERN_ERR "Memory resource not set for "
  1204. "host bridge %d\n", hose->index);
  1205. res->start = hose->pci_mem_offset;
  1206. res->end = ~0U;
  1207. res->flags = IORESOURCE_MEM;
  1208. }
  1209. bus->resource[i+1] = res;
  1210. }
  1211. } else {
  1212. /* This is a subordinate bridge */
  1213. pci_read_bridge_bases(bus);
  1214. for (i = 0; i < 4; ++i) {
  1215. if ((res = bus->resource[i]) == NULL)
  1216. continue;
  1217. if (!res->flags)
  1218. continue;
  1219. if (io_offset && (res->flags & IORESOURCE_IO)) {
  1220. res->start += io_offset;
  1221. res->end += io_offset;
  1222. } else if (hose->pci_mem_offset
  1223. && (res->flags & IORESOURCE_MEM)) {
  1224. res->start += hose->pci_mem_offset;
  1225. res->end += hose->pci_mem_offset;
  1226. }
  1227. }
  1228. }
  1229. /* Platform specific bus fixups */
  1230. if (ppc_md.pcibios_fixup_bus)
  1231. ppc_md.pcibios_fixup_bus(bus);
  1232. /* Read default IRQs and fixup if necessary */
  1233. list_for_each_entry(dev, &bus->devices, bus_list) {
  1234. pci_read_irq_line(dev);
  1235. if (ppc_md.pci_irq_fixup)
  1236. ppc_md.pci_irq_fixup(dev);
  1237. }
  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. #ifdef CONFIG_PPC_MERGE
  1251. /* XXX This is a copy of the ppc64 version. This is temporary until we start
  1252. * merging the 2 PCI layers
  1253. */
  1254. /*
  1255. * Reads the interrupt pin to determine if interrupt is use by card.
  1256. * If the interrupt is used, then gets the interrupt line from the
  1257. * openfirmware and sets it in the pci_dev and pci_config line.
  1258. */
  1259. int pci_read_irq_line(struct pci_dev *pci_dev)
  1260. {
  1261. struct of_irq oirq;
  1262. unsigned int virq;
  1263. DBG("Try to map irq for %s...\n", pci_name(pci_dev));
  1264. /* Try to get a mapping from the device-tree */
  1265. if (of_irq_map_pci(pci_dev, &oirq)) {
  1266. u8 line, pin;
  1267. /* If that fails, lets fallback to what is in the config
  1268. * space and map that through the default controller. We
  1269. * also set the type to level low since that's what PCI
  1270. * interrupts are. If your platform does differently, then
  1271. * either provide a proper interrupt tree or don't use this
  1272. * function.
  1273. */
  1274. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
  1275. return -1;
  1276. if (pin == 0)
  1277. return -1;
  1278. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
  1279. line == 0xff) {
  1280. return -1;
  1281. }
  1282. DBG(" -> no map ! Using irq line %d from PCI config\n", line);
  1283. virq = irq_create_mapping(NULL, line);
  1284. if (virq != NO_IRQ)
  1285. set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
  1286. } else {
  1287. DBG(" -> got one, spec %d cells (0x%08x...) on %s\n",
  1288. oirq.size, oirq.specifier[0], oirq.controller->full_name);
  1289. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  1290. oirq.size);
  1291. }
  1292. if(virq == NO_IRQ) {
  1293. DBG(" -> failed to map !\n");
  1294. return -1;
  1295. }
  1296. pci_dev->irq = virq;
  1297. pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, virq);
  1298. return 0;
  1299. }
  1300. EXPORT_SYMBOL(pci_read_irq_line);
  1301. #endif /* CONFIG_PPC_MERGE */
  1302. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1303. {
  1304. u16 cmd, old_cmd;
  1305. int idx;
  1306. struct resource *r;
  1307. if (ppc_md.pcibios_enable_device_hook)
  1308. if (ppc_md.pcibios_enable_device_hook(dev, 0))
  1309. return -EINVAL;
  1310. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1311. old_cmd = cmd;
  1312. for (idx=0; idx<6; idx++) {
  1313. r = &dev->resource[idx];
  1314. if (r->flags & IORESOURCE_UNSET) {
  1315. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  1316. return -EINVAL;
  1317. }
  1318. if (r->flags & IORESOURCE_IO)
  1319. cmd |= PCI_COMMAND_IO;
  1320. if (r->flags & IORESOURCE_MEM)
  1321. cmd |= PCI_COMMAND_MEMORY;
  1322. }
  1323. if (cmd != old_cmd) {
  1324. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  1325. pci_name(dev), old_cmd, cmd);
  1326. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1327. }
  1328. return 0;
  1329. }
  1330. struct pci_controller*
  1331. pci_bus_to_hose(int bus)
  1332. {
  1333. struct pci_controller* hose = hose_head;
  1334. for (; hose; hose = hose->next)
  1335. if (bus >= hose->first_busno && bus <= hose->last_busno)
  1336. return hose;
  1337. return NULL;
  1338. }
  1339. void __iomem *
  1340. pci_bus_io_base(unsigned int bus)
  1341. {
  1342. struct pci_controller *hose;
  1343. hose = pci_bus_to_hose(bus);
  1344. if (!hose)
  1345. return NULL;
  1346. return hose->io_base_virt;
  1347. }
  1348. unsigned long
  1349. pci_bus_io_base_phys(unsigned int bus)
  1350. {
  1351. struct pci_controller *hose;
  1352. hose = pci_bus_to_hose(bus);
  1353. if (!hose)
  1354. return 0;
  1355. return hose->io_base_phys;
  1356. }
  1357. unsigned long
  1358. pci_bus_mem_base_phys(unsigned int bus)
  1359. {
  1360. struct pci_controller *hose;
  1361. hose = pci_bus_to_hose(bus);
  1362. if (!hose)
  1363. return 0;
  1364. return hose->pci_mem_offset;
  1365. }
  1366. unsigned long
  1367. pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
  1368. {
  1369. /* Hack alert again ! See comments in chrp_pci.c
  1370. */
  1371. struct pci_controller* hose =
  1372. (struct pci_controller *)pdev->sysdata;
  1373. if (hose && res->flags & IORESOURCE_MEM)
  1374. return res->start - hose->pci_mem_offset;
  1375. /* We may want to do something with IOs here... */
  1376. return res->start;
  1377. }
  1378. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  1379. unsigned long *offset,
  1380. enum pci_mmap_state mmap_state)
  1381. {
  1382. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1383. unsigned long io_offset = 0;
  1384. int i, res_bit;
  1385. if (hose == 0)
  1386. return NULL; /* should never happen */
  1387. /* If memory, add on the PCI bridge address offset */
  1388. if (mmap_state == pci_mmap_mem) {
  1389. *offset += hose->pci_mem_offset;
  1390. res_bit = IORESOURCE_MEM;
  1391. } else {
  1392. io_offset = hose->io_base_virt - (void __iomem *)_IO_BASE;
  1393. *offset += io_offset;
  1394. res_bit = IORESOURCE_IO;
  1395. }
  1396. /*
  1397. * Check that the offset requested corresponds to one of the
  1398. * resources of the device.
  1399. */
  1400. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1401. struct resource *rp = &dev->resource[i];
  1402. int flags = rp->flags;
  1403. /* treat ROM as memory (should be already) */
  1404. if (i == PCI_ROM_RESOURCE)
  1405. flags |= IORESOURCE_MEM;
  1406. /* Active and same type? */
  1407. if ((flags & res_bit) == 0)
  1408. continue;
  1409. /* In the range of this resource? */
  1410. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  1411. continue;
  1412. /* found it! construct the final physical address */
  1413. if (mmap_state == pci_mmap_io)
  1414. *offset += hose->io_base_phys - io_offset;
  1415. return rp;
  1416. }
  1417. return NULL;
  1418. }
  1419. /*
  1420. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  1421. * device mapping.
  1422. */
  1423. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  1424. pgprot_t protection,
  1425. enum pci_mmap_state mmap_state,
  1426. int write_combine)
  1427. {
  1428. unsigned long prot = pgprot_val(protection);
  1429. /* Write combine is always 0 on non-memory space mappings. On
  1430. * memory space, if the user didn't pass 1, we check for a
  1431. * "prefetchable" resource. This is a bit hackish, but we use
  1432. * this to workaround the inability of /sysfs to provide a write
  1433. * combine bit
  1434. */
  1435. if (mmap_state != pci_mmap_mem)
  1436. write_combine = 0;
  1437. else if (write_combine == 0) {
  1438. if (rp->flags & IORESOURCE_PREFETCH)
  1439. write_combine = 1;
  1440. }
  1441. /* XXX would be nice to have a way to ask for write-through */
  1442. prot |= _PAGE_NO_CACHE;
  1443. if (write_combine)
  1444. prot &= ~_PAGE_GUARDED;
  1445. else
  1446. prot |= _PAGE_GUARDED;
  1447. printk("PCI map for %s:%llx, prot: %lx\n", pci_name(dev),
  1448. (unsigned long long)rp->start, prot);
  1449. return __pgprot(prot);
  1450. }
  1451. /*
  1452. * This one is used by /dev/mem and fbdev who have no clue about the
  1453. * PCI device, it tries to find the PCI device first and calls the
  1454. * above routine
  1455. */
  1456. pgprot_t pci_phys_mem_access_prot(struct file *file,
  1457. unsigned long pfn,
  1458. unsigned long size,
  1459. pgprot_t protection)
  1460. {
  1461. struct pci_dev *pdev = NULL;
  1462. struct resource *found = NULL;
  1463. unsigned long prot = pgprot_val(protection);
  1464. unsigned long offset = pfn << PAGE_SHIFT;
  1465. int i;
  1466. if (page_is_ram(pfn))
  1467. return prot;
  1468. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  1469. for_each_pci_dev(pdev) {
  1470. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1471. struct resource *rp = &pdev->resource[i];
  1472. int flags = rp->flags;
  1473. /* Active and same type? */
  1474. if ((flags & IORESOURCE_MEM) == 0)
  1475. continue;
  1476. /* In the range of this resource? */
  1477. if (offset < (rp->start & PAGE_MASK) ||
  1478. offset > rp->end)
  1479. continue;
  1480. found = rp;
  1481. break;
  1482. }
  1483. if (found)
  1484. break;
  1485. }
  1486. if (found) {
  1487. if (found->flags & IORESOURCE_PREFETCH)
  1488. prot &= ~_PAGE_GUARDED;
  1489. pci_dev_put(pdev);
  1490. }
  1491. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  1492. return __pgprot(prot);
  1493. }
  1494. /*
  1495. * Perform the actual remap of the pages for a PCI device mapping, as
  1496. * appropriate for this architecture. The region in the process to map
  1497. * is described by vm_start and vm_end members of VMA, the base physical
  1498. * address is found in vm_pgoff.
  1499. * The pci device structure is provided so that architectures may make mapping
  1500. * decisions on a per-device or per-bus basis.
  1501. *
  1502. * Returns a negative error code on failure, zero on success.
  1503. */
  1504. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  1505. enum pci_mmap_state mmap_state,
  1506. int write_combine)
  1507. {
  1508. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1509. struct resource *rp;
  1510. int ret;
  1511. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  1512. if (rp == NULL)
  1513. return -EINVAL;
  1514. vma->vm_pgoff = offset >> PAGE_SHIFT;
  1515. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  1516. vma->vm_page_prot,
  1517. mmap_state, write_combine);
  1518. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  1519. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  1520. return ret;
  1521. }
  1522. /* Obsolete functions. Should be removed once the symbios driver
  1523. * is fixed
  1524. */
  1525. unsigned long
  1526. phys_to_bus(unsigned long pa)
  1527. {
  1528. struct pci_controller *hose;
  1529. int i;
  1530. for (hose = hose_head; hose; hose = hose->next) {
  1531. for (i = 0; i < 3; ++i) {
  1532. if (pa >= hose->mem_resources[i].start
  1533. && pa <= hose->mem_resources[i].end) {
  1534. /*
  1535. * XXX the hose->pci_mem_offset really
  1536. * only applies to mem_resources[0].
  1537. * We need a way to store an offset for
  1538. * the others. -- paulus
  1539. */
  1540. if (i == 0)
  1541. pa -= hose->pci_mem_offset;
  1542. return pa;
  1543. }
  1544. }
  1545. }
  1546. /* hmmm, didn't find it */
  1547. return 0;
  1548. }
  1549. unsigned long
  1550. pci_phys_to_bus(unsigned long pa, int busnr)
  1551. {
  1552. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1553. if (!hose)
  1554. return pa;
  1555. return pa - hose->pci_mem_offset;
  1556. }
  1557. unsigned long
  1558. pci_bus_to_phys(unsigned int ba, int busnr)
  1559. {
  1560. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1561. if (!hose)
  1562. return ba;
  1563. return ba + hose->pci_mem_offset;
  1564. }
  1565. /* Provide information on locations of various I/O regions in physical
  1566. * memory. Do this on a per-card basis so that we choose the right
  1567. * root bridge.
  1568. * Note that the returned IO or memory base is a physical address
  1569. */
  1570. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  1571. {
  1572. struct pci_controller* hose;
  1573. long result = -EOPNOTSUPP;
  1574. /* Argh ! Please forgive me for that hack, but that's the
  1575. * simplest way to get existing XFree to not lockup on some
  1576. * G5 machines... So when something asks for bus 0 io base
  1577. * (bus 0 is HT root), we return the AGP one instead.
  1578. */
  1579. #ifdef CONFIG_PPC_PMAC
  1580. if (machine_is(powermac) && machine_is_compatible("MacRISC4"))
  1581. if (bus == 0)
  1582. bus = 0xf0;
  1583. #endif /* CONFIG_PPC_PMAC */
  1584. hose = pci_bus_to_hose(bus);
  1585. if (!hose)
  1586. return -ENODEV;
  1587. switch (which) {
  1588. case IOBASE_BRIDGE_NUMBER:
  1589. return (long)hose->first_busno;
  1590. case IOBASE_MEMORY:
  1591. return (long)hose->pci_mem_offset;
  1592. case IOBASE_IO:
  1593. return (long)hose->io_base_phys;
  1594. case IOBASE_ISA_IO:
  1595. return (long)isa_io_base;
  1596. case IOBASE_ISA_MEM:
  1597. return (long)isa_mem_base;
  1598. }
  1599. return result;
  1600. }
  1601. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1602. const struct resource *rsrc,
  1603. resource_size_t *start, resource_size_t *end)
  1604. {
  1605. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1606. unsigned long offset = 0;
  1607. if (hose == NULL)
  1608. return;
  1609. if (rsrc->flags & IORESOURCE_IO)
  1610. offset = (void __iomem *)_IO_BASE - hose->io_base_virt
  1611. + hose->io_base_phys;
  1612. *start = rsrc->start + offset;
  1613. *end = rsrc->end + offset;
  1614. }
  1615. void __init
  1616. pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
  1617. int flags, char *name)
  1618. {
  1619. res->start = start;
  1620. res->end = end;
  1621. res->flags = flags;
  1622. res->name = name;
  1623. res->parent = NULL;
  1624. res->sibling = NULL;
  1625. res->child = NULL;
  1626. }
  1627. unsigned long pci_address_to_pio(phys_addr_t address)
  1628. {
  1629. struct pci_controller* hose = hose_head;
  1630. for (; hose; hose = hose->next) {
  1631. unsigned int size = hose->io_resource.end -
  1632. hose->io_resource.start + 1;
  1633. if (address >= hose->io_base_phys &&
  1634. address < (hose->io_base_phys + size)) {
  1635. unsigned long base =
  1636. (unsigned long)hose->io_base_virt - _IO_BASE;
  1637. return base + (address - hose->io_base_phys);
  1638. }
  1639. }
  1640. return (unsigned int)-1;
  1641. }
  1642. EXPORT_SYMBOL(pci_address_to_pio);
  1643. /*
  1644. * Null PCI config access functions, for the case when we can't
  1645. * find a hose.
  1646. */
  1647. #define NULL_PCI_OP(rw, size, type) \
  1648. static int \
  1649. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1650. { \
  1651. return PCIBIOS_DEVICE_NOT_FOUND; \
  1652. }
  1653. static int
  1654. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1655. int len, u32 *val)
  1656. {
  1657. return PCIBIOS_DEVICE_NOT_FOUND;
  1658. }
  1659. static int
  1660. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1661. int len, u32 val)
  1662. {
  1663. return PCIBIOS_DEVICE_NOT_FOUND;
  1664. }
  1665. static struct pci_ops null_pci_ops =
  1666. {
  1667. null_read_config,
  1668. null_write_config
  1669. };
  1670. /*
  1671. * These functions are used early on before PCI scanning is done
  1672. * and all of the pci_dev and pci_bus structures have been created.
  1673. */
  1674. static struct pci_bus *
  1675. fake_pci_bus(struct pci_controller *hose, int busnr)
  1676. {
  1677. static struct pci_bus bus;
  1678. if (hose == 0) {
  1679. hose = pci_bus_to_hose(busnr);
  1680. if (hose == 0)
  1681. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1682. }
  1683. bus.number = busnr;
  1684. bus.sysdata = hose;
  1685. bus.ops = hose? hose->ops: &null_pci_ops;
  1686. return &bus;
  1687. }
  1688. #define EARLY_PCI_OP(rw, size, type) \
  1689. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1690. int devfn, int offset, type value) \
  1691. { \
  1692. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1693. devfn, offset, value); \
  1694. }
  1695. EARLY_PCI_OP(read, byte, u8 *)
  1696. EARLY_PCI_OP(read, word, u16 *)
  1697. EARLY_PCI_OP(read, dword, u32 *)
  1698. EARLY_PCI_OP(write, byte, u8)
  1699. EARLY_PCI_OP(write, word, u16)
  1700. EARLY_PCI_OP(write, dword, u32)