pci_32.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  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, mask) do { \
  1007. BUG_ON((mask+1) & mask); \
  1008. res->end = (res->end + mask) | mask; \
  1009. } while (0)
  1010. list_for_each_entry(dev, &bus->devices, bus_list) {
  1011. u16 class = dev->class >> 8;
  1012. if (class == PCI_CLASS_DISPLAY_VGA ||
  1013. class == PCI_CLASS_NOT_DEFINED_VGA)
  1014. *found_vga = 1;
  1015. if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
  1016. rc |= check_for_io_childs(dev->subordinate, res, found_vga);
  1017. if (class == PCI_CLASS_BRIDGE_CARDBUS)
  1018. push_end(res, 0xfff);
  1019. for (i=0; i<PCI_NUM_RESOURCES; i++) {
  1020. struct resource *r;
  1021. unsigned long r_size;
  1022. if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
  1023. && i >= PCI_BRIDGE_RESOURCES)
  1024. continue;
  1025. r = &dev->resource[i];
  1026. r_size = r->end - r->start;
  1027. if (r_size < 0xfff)
  1028. r_size = 0xfff;
  1029. if (r->flags & IORESOURCE_IO && (r_size) != 0) {
  1030. rc = 1;
  1031. push_end(res, r_size);
  1032. }
  1033. }
  1034. }
  1035. return rc;
  1036. }
  1037. /* Here we scan all P2P bridges of a given level that have a closed
  1038. * IO window. Note that the test for the presence of a VGA card should
  1039. * be improved to take into account already configured P2P bridges,
  1040. * currently, we don't see them and might end up configuring 2 bridges
  1041. * with VGA pass through enabled
  1042. */
  1043. static void __init
  1044. do_fixup_p2p_level(struct pci_bus *bus)
  1045. {
  1046. struct pci_bus *b;
  1047. int i, parent_io;
  1048. int has_vga = 0;
  1049. for (parent_io=0; parent_io<4; parent_io++)
  1050. if (bus->resource[parent_io]
  1051. && bus->resource[parent_io]->flags & IORESOURCE_IO)
  1052. break;
  1053. if (parent_io >= 4)
  1054. return;
  1055. list_for_each_entry(b, &bus->children, node) {
  1056. struct pci_dev *d = b->self;
  1057. struct pci_controller* hose = (struct pci_controller *)d->sysdata;
  1058. struct resource *res = b->resource[0];
  1059. struct resource tmp_res;
  1060. unsigned long max;
  1061. int found_vga = 0;
  1062. memset(&tmp_res, 0, sizeof(tmp_res));
  1063. tmp_res.start = bus->resource[parent_io]->start;
  1064. /* We don't let low addresses go through that closed P2P bridge, well,
  1065. * that may not be necessary but I feel safer that way
  1066. */
  1067. if (tmp_res.start == 0)
  1068. tmp_res.start = 0x1000;
  1069. if (!list_empty(&b->devices) && res && res->flags == 0 &&
  1070. res != bus->resource[parent_io] &&
  1071. (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
  1072. check_for_io_childs(b, &tmp_res, &found_vga)) {
  1073. u8 io_base_lo;
  1074. printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
  1075. if (found_vga) {
  1076. if (has_vga) {
  1077. printk(KERN_WARNING "Skipping VGA, already active"
  1078. " on bus segment\n");
  1079. found_vga = 0;
  1080. } else
  1081. has_vga = 1;
  1082. }
  1083. pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
  1084. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
  1085. max = ((unsigned long) hose->io_base_virt
  1086. - isa_io_base) + 0xffffffff;
  1087. else
  1088. max = ((unsigned long) hose->io_base_virt
  1089. - isa_io_base) + 0xffff;
  1090. *res = tmp_res;
  1091. res->flags = IORESOURCE_IO;
  1092. res->name = b->name;
  1093. /* Find a resource in the parent where we can allocate */
  1094. for (i = 0 ; i < 4; i++) {
  1095. struct resource *r = bus->resource[i];
  1096. if (!r)
  1097. continue;
  1098. if ((r->flags & IORESOURCE_IO) == 0)
  1099. continue;
  1100. DBG("Trying to allocate from %08lx, size %08lx from parent"
  1101. " res %d: %08lx -> %08lx\n",
  1102. res->start, res->end, i, r->start, r->end);
  1103. if (allocate_resource(r, res, res->end + 1, res->start, max,
  1104. res->end + 1, NULL, NULL) < 0) {
  1105. DBG("Failed !\n");
  1106. continue;
  1107. }
  1108. do_update_p2p_io_resource(b, found_vga);
  1109. break;
  1110. }
  1111. }
  1112. do_fixup_p2p_level(b);
  1113. }
  1114. }
  1115. static void
  1116. pcibios_fixup_p2p_bridges(void)
  1117. {
  1118. struct pci_bus *b;
  1119. list_for_each_entry(b, &pci_root_buses, node)
  1120. do_fixup_p2p_level(b);
  1121. }
  1122. #endif /* CONFIG_PPC_PMAC */
  1123. static int __init
  1124. pcibios_init(void)
  1125. {
  1126. struct pci_controller *hose;
  1127. struct pci_bus *bus;
  1128. int next_busno;
  1129. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  1130. /* Scan all of the recorded PCI controllers. */
  1131. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  1132. if (pci_assign_all_buses)
  1133. hose->first_busno = next_busno;
  1134. hose->last_busno = 0xff;
  1135. bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
  1136. hose->last_busno = bus->subordinate;
  1137. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  1138. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  1139. }
  1140. pci_bus_count = next_busno;
  1141. /* OpenFirmware based machines need a map of OF bus
  1142. * numbers vs. kernel bus numbers since we may have to
  1143. * remap them.
  1144. */
  1145. if (pci_assign_all_buses && have_of)
  1146. pcibios_make_OF_bus_map();
  1147. /* Do machine dependent PCI interrupt routing */
  1148. if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
  1149. pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
  1150. /* Call machine dependent fixup */
  1151. if (ppc_md.pcibios_fixup)
  1152. ppc_md.pcibios_fixup();
  1153. /* Allocate and assign resources */
  1154. pcibios_allocate_bus_resources(&pci_root_buses);
  1155. pcibios_allocate_resources(0);
  1156. pcibios_allocate_resources(1);
  1157. #ifdef CONFIG_PPC_PMAC
  1158. pcibios_fixup_p2p_bridges();
  1159. #endif /* CONFIG_PPC_PMAC */
  1160. pcibios_assign_resources();
  1161. /* Call machine dependent post-init code */
  1162. if (ppc_md.pcibios_after_init)
  1163. ppc_md.pcibios_after_init();
  1164. return 0;
  1165. }
  1166. subsys_initcall(pcibios_init);
  1167. unsigned char __init
  1168. common_swizzle(struct pci_dev *dev, unsigned char *pinp)
  1169. {
  1170. struct pci_controller *hose = dev->sysdata;
  1171. if (dev->bus->number != hose->first_busno) {
  1172. u8 pin = *pinp;
  1173. do {
  1174. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  1175. /* Move up the chain of bridges. */
  1176. dev = dev->bus->self;
  1177. } while (dev->bus->self);
  1178. *pinp = pin;
  1179. /* The slot is the idsel of the last bridge. */
  1180. }
  1181. return PCI_SLOT(dev->devfn);
  1182. }
  1183. unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
  1184. unsigned long start, unsigned long size)
  1185. {
  1186. return start;
  1187. }
  1188. void __init pcibios_fixup_bus(struct pci_bus *bus)
  1189. {
  1190. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  1191. unsigned long io_offset;
  1192. struct resource *res;
  1193. int i;
  1194. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  1195. if (bus->parent == NULL) {
  1196. /* This is a host bridge - fill in its resources */
  1197. hose->bus = bus;
  1198. bus->resource[0] = res = &hose->io_resource;
  1199. if (!res->flags) {
  1200. if (io_offset)
  1201. printk(KERN_ERR "I/O resource not set for host"
  1202. " bridge %d\n", hose->index);
  1203. res->start = 0;
  1204. res->end = IO_SPACE_LIMIT;
  1205. res->flags = IORESOURCE_IO;
  1206. }
  1207. res->start += io_offset;
  1208. res->end += io_offset;
  1209. for (i = 0; i < 3; ++i) {
  1210. res = &hose->mem_resources[i];
  1211. if (!res->flags) {
  1212. if (i > 0)
  1213. continue;
  1214. printk(KERN_ERR "Memory resource not set for "
  1215. "host bridge %d\n", hose->index);
  1216. res->start = hose->pci_mem_offset;
  1217. res->end = ~0U;
  1218. res->flags = IORESOURCE_MEM;
  1219. }
  1220. bus->resource[i+1] = res;
  1221. }
  1222. } else {
  1223. /* This is a subordinate bridge */
  1224. pci_read_bridge_bases(bus);
  1225. for (i = 0; i < 4; ++i) {
  1226. if ((res = bus->resource[i]) == NULL)
  1227. continue;
  1228. if (!res->flags)
  1229. continue;
  1230. if (io_offset && (res->flags & IORESOURCE_IO)) {
  1231. res->start += io_offset;
  1232. res->end += io_offset;
  1233. } else if (hose->pci_mem_offset
  1234. && (res->flags & IORESOURCE_MEM)) {
  1235. res->start += hose->pci_mem_offset;
  1236. res->end += hose->pci_mem_offset;
  1237. }
  1238. }
  1239. }
  1240. if (ppc_md.pcibios_fixup_bus)
  1241. ppc_md.pcibios_fixup_bus(bus);
  1242. }
  1243. char __init *pcibios_setup(char *str)
  1244. {
  1245. return str;
  1246. }
  1247. /* the next one is stolen from the alpha port... */
  1248. void __init
  1249. pcibios_update_irq(struct pci_dev *dev, int irq)
  1250. {
  1251. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  1252. /* XXX FIXME - update OF device tree node interrupt property */
  1253. }
  1254. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1255. {
  1256. u16 cmd, old_cmd;
  1257. int idx;
  1258. struct resource *r;
  1259. if (ppc_md.pcibios_enable_device_hook)
  1260. if (ppc_md.pcibios_enable_device_hook(dev, 0))
  1261. return -EINVAL;
  1262. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1263. old_cmd = cmd;
  1264. for (idx=0; idx<6; idx++) {
  1265. r = &dev->resource[idx];
  1266. if (r->flags & IORESOURCE_UNSET) {
  1267. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  1268. return -EINVAL;
  1269. }
  1270. if (r->flags & IORESOURCE_IO)
  1271. cmd |= PCI_COMMAND_IO;
  1272. if (r->flags & IORESOURCE_MEM)
  1273. cmd |= PCI_COMMAND_MEMORY;
  1274. }
  1275. if (cmd != old_cmd) {
  1276. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  1277. pci_name(dev), old_cmd, cmd);
  1278. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1279. }
  1280. return 0;
  1281. }
  1282. struct pci_controller*
  1283. pci_bus_to_hose(int bus)
  1284. {
  1285. struct pci_controller* hose = hose_head;
  1286. for (; hose; hose = hose->next)
  1287. if (bus >= hose->first_busno && bus <= hose->last_busno)
  1288. return hose;
  1289. return NULL;
  1290. }
  1291. void __iomem *
  1292. pci_bus_io_base(unsigned int bus)
  1293. {
  1294. struct pci_controller *hose;
  1295. hose = pci_bus_to_hose(bus);
  1296. if (!hose)
  1297. return NULL;
  1298. return hose->io_base_virt;
  1299. }
  1300. unsigned long
  1301. pci_bus_io_base_phys(unsigned int bus)
  1302. {
  1303. struct pci_controller *hose;
  1304. hose = pci_bus_to_hose(bus);
  1305. if (!hose)
  1306. return 0;
  1307. return hose->io_base_phys;
  1308. }
  1309. unsigned long
  1310. pci_bus_mem_base_phys(unsigned int bus)
  1311. {
  1312. struct pci_controller *hose;
  1313. hose = pci_bus_to_hose(bus);
  1314. if (!hose)
  1315. return 0;
  1316. return hose->pci_mem_offset;
  1317. }
  1318. unsigned long
  1319. pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
  1320. {
  1321. /* Hack alert again ! See comments in chrp_pci.c
  1322. */
  1323. struct pci_controller* hose =
  1324. (struct pci_controller *)pdev->sysdata;
  1325. if (hose && res->flags & IORESOURCE_MEM)
  1326. return res->start - hose->pci_mem_offset;
  1327. /* We may want to do something with IOs here... */
  1328. return res->start;
  1329. }
  1330. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  1331. unsigned long *offset,
  1332. enum pci_mmap_state mmap_state)
  1333. {
  1334. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1335. unsigned long io_offset = 0;
  1336. int i, res_bit;
  1337. if (hose == 0)
  1338. return NULL; /* should never happen */
  1339. /* If memory, add on the PCI bridge address offset */
  1340. if (mmap_state == pci_mmap_mem) {
  1341. *offset += hose->pci_mem_offset;
  1342. res_bit = IORESOURCE_MEM;
  1343. } else {
  1344. io_offset = hose->io_base_virt - ___IO_BASE;
  1345. *offset += io_offset;
  1346. res_bit = IORESOURCE_IO;
  1347. }
  1348. /*
  1349. * Check that the offset requested corresponds to one of the
  1350. * resources of the device.
  1351. */
  1352. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1353. struct resource *rp = &dev->resource[i];
  1354. int flags = rp->flags;
  1355. /* treat ROM as memory (should be already) */
  1356. if (i == PCI_ROM_RESOURCE)
  1357. flags |= IORESOURCE_MEM;
  1358. /* Active and same type? */
  1359. if ((flags & res_bit) == 0)
  1360. continue;
  1361. /* In the range of this resource? */
  1362. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  1363. continue;
  1364. /* found it! construct the final physical address */
  1365. if (mmap_state == pci_mmap_io)
  1366. *offset += hose->io_base_phys - io_offset;
  1367. return rp;
  1368. }
  1369. return NULL;
  1370. }
  1371. /*
  1372. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  1373. * device mapping.
  1374. */
  1375. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  1376. pgprot_t protection,
  1377. enum pci_mmap_state mmap_state,
  1378. int write_combine)
  1379. {
  1380. unsigned long prot = pgprot_val(protection);
  1381. /* Write combine is always 0 on non-memory space mappings. On
  1382. * memory space, if the user didn't pass 1, we check for a
  1383. * "prefetchable" resource. This is a bit hackish, but we use
  1384. * this to workaround the inability of /sysfs to provide a write
  1385. * combine bit
  1386. */
  1387. if (mmap_state != pci_mmap_mem)
  1388. write_combine = 0;
  1389. else if (write_combine == 0) {
  1390. if (rp->flags & IORESOURCE_PREFETCH)
  1391. write_combine = 1;
  1392. }
  1393. /* XXX would be nice to have a way to ask for write-through */
  1394. prot |= _PAGE_NO_CACHE;
  1395. if (write_combine)
  1396. prot &= ~_PAGE_GUARDED;
  1397. else
  1398. prot |= _PAGE_GUARDED;
  1399. printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
  1400. prot);
  1401. return __pgprot(prot);
  1402. }
  1403. /*
  1404. * This one is used by /dev/mem and fbdev who have no clue about the
  1405. * PCI device, it tries to find the PCI device first and calls the
  1406. * above routine
  1407. */
  1408. pgprot_t pci_phys_mem_access_prot(struct file *file,
  1409. unsigned long pfn,
  1410. unsigned long size,
  1411. pgprot_t protection)
  1412. {
  1413. struct pci_dev *pdev = NULL;
  1414. struct resource *found = NULL;
  1415. unsigned long prot = pgprot_val(protection);
  1416. unsigned long offset = pfn << PAGE_SHIFT;
  1417. int i;
  1418. if (page_is_ram(pfn))
  1419. return prot;
  1420. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  1421. for_each_pci_dev(pdev) {
  1422. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1423. struct resource *rp = &pdev->resource[i];
  1424. int flags = rp->flags;
  1425. /* Active and same type? */
  1426. if ((flags & IORESOURCE_MEM) == 0)
  1427. continue;
  1428. /* In the range of this resource? */
  1429. if (offset < (rp->start & PAGE_MASK) ||
  1430. offset > rp->end)
  1431. continue;
  1432. found = rp;
  1433. break;
  1434. }
  1435. if (found)
  1436. break;
  1437. }
  1438. if (found) {
  1439. if (found->flags & IORESOURCE_PREFETCH)
  1440. prot &= ~_PAGE_GUARDED;
  1441. pci_dev_put(pdev);
  1442. }
  1443. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  1444. return __pgprot(prot);
  1445. }
  1446. /*
  1447. * Perform the actual remap of the pages for a PCI device mapping, as
  1448. * appropriate for this architecture. The region in the process to map
  1449. * is described by vm_start and vm_end members of VMA, the base physical
  1450. * address is found in vm_pgoff.
  1451. * The pci device structure is provided so that architectures may make mapping
  1452. * decisions on a per-device or per-bus basis.
  1453. *
  1454. * Returns a negative error code on failure, zero on success.
  1455. */
  1456. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  1457. enum pci_mmap_state mmap_state,
  1458. int write_combine)
  1459. {
  1460. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1461. struct resource *rp;
  1462. int ret;
  1463. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  1464. if (rp == NULL)
  1465. return -EINVAL;
  1466. vma->vm_pgoff = offset >> PAGE_SHIFT;
  1467. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  1468. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  1469. vma->vm_page_prot,
  1470. mmap_state, write_combine);
  1471. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  1472. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  1473. return ret;
  1474. }
  1475. /* Obsolete functions. Should be removed once the symbios driver
  1476. * is fixed
  1477. */
  1478. unsigned long
  1479. phys_to_bus(unsigned long pa)
  1480. {
  1481. struct pci_controller *hose;
  1482. int i;
  1483. for (hose = hose_head; hose; hose = hose->next) {
  1484. for (i = 0; i < 3; ++i) {
  1485. if (pa >= hose->mem_resources[i].start
  1486. && pa <= hose->mem_resources[i].end) {
  1487. /*
  1488. * XXX the hose->pci_mem_offset really
  1489. * only applies to mem_resources[0].
  1490. * We need a way to store an offset for
  1491. * the others. -- paulus
  1492. */
  1493. if (i == 0)
  1494. pa -= hose->pci_mem_offset;
  1495. return pa;
  1496. }
  1497. }
  1498. }
  1499. /* hmmm, didn't find it */
  1500. return 0;
  1501. }
  1502. unsigned long
  1503. pci_phys_to_bus(unsigned long pa, int busnr)
  1504. {
  1505. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1506. if (!hose)
  1507. return pa;
  1508. return pa - hose->pci_mem_offset;
  1509. }
  1510. unsigned long
  1511. pci_bus_to_phys(unsigned int ba, int busnr)
  1512. {
  1513. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1514. if (!hose)
  1515. return ba;
  1516. return ba + hose->pci_mem_offset;
  1517. }
  1518. /* Provide information on locations of various I/O regions in physical
  1519. * memory. Do this on a per-card basis so that we choose the right
  1520. * root bridge.
  1521. * Note that the returned IO or memory base is a physical address
  1522. */
  1523. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  1524. {
  1525. struct pci_controller* hose;
  1526. long result = -EOPNOTSUPP;
  1527. /* Argh ! Please forgive me for that hack, but that's the
  1528. * simplest way to get existing XFree to not lockup on some
  1529. * G5 machines... So when something asks for bus 0 io base
  1530. * (bus 0 is HT root), we return the AGP one instead.
  1531. */
  1532. #ifdef CONFIG_PPC_PMAC
  1533. if (machine_is(powermac) && machine_is_compatible("MacRISC4"))
  1534. if (bus == 0)
  1535. bus = 0xf0;
  1536. #endif /* CONFIG_PPC_PMAC */
  1537. hose = pci_bus_to_hose(bus);
  1538. if (!hose)
  1539. return -ENODEV;
  1540. switch (which) {
  1541. case IOBASE_BRIDGE_NUMBER:
  1542. return (long)hose->first_busno;
  1543. case IOBASE_MEMORY:
  1544. return (long)hose->pci_mem_offset;
  1545. case IOBASE_IO:
  1546. return (long)hose->io_base_phys;
  1547. case IOBASE_ISA_IO:
  1548. return (long)isa_io_base;
  1549. case IOBASE_ISA_MEM:
  1550. return (long)isa_mem_base;
  1551. }
  1552. return result;
  1553. }
  1554. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1555. const struct resource *rsrc,
  1556. u64 *start, u64 *end)
  1557. {
  1558. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1559. unsigned long offset = 0;
  1560. if (hose == NULL)
  1561. return;
  1562. if (rsrc->flags & IORESOURCE_IO)
  1563. offset = ___IO_BASE - hose->io_base_virt + hose->io_base_phys;
  1564. *start = rsrc->start + offset;
  1565. *end = rsrc->end + offset;
  1566. }
  1567. void __init
  1568. pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
  1569. int flags, char *name)
  1570. {
  1571. res->start = start;
  1572. res->end = end;
  1573. res->flags = flags;
  1574. res->name = name;
  1575. res->parent = NULL;
  1576. res->sibling = NULL;
  1577. res->child = NULL;
  1578. }
  1579. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
  1580. {
  1581. unsigned long start = pci_resource_start(dev, bar);
  1582. unsigned long len = pci_resource_len(dev, bar);
  1583. unsigned long flags = pci_resource_flags(dev, bar);
  1584. if (!len)
  1585. return NULL;
  1586. if (max && len > max)
  1587. len = max;
  1588. if (flags & IORESOURCE_IO)
  1589. return ioport_map(start, len);
  1590. if (flags & IORESOURCE_MEM)
  1591. /* Not checking IORESOURCE_CACHEABLE because PPC does
  1592. * not currently distinguish between ioremap and
  1593. * ioremap_nocache.
  1594. */
  1595. return ioremap(start, len);
  1596. /* What? */
  1597. return NULL;
  1598. }
  1599. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  1600. {
  1601. /* Nothing to do */
  1602. }
  1603. EXPORT_SYMBOL(pci_iomap);
  1604. EXPORT_SYMBOL(pci_iounmap);
  1605. unsigned long pci_address_to_pio(phys_addr_t address)
  1606. {
  1607. struct pci_controller* hose = hose_head;
  1608. for (; hose; hose = hose->next) {
  1609. unsigned int size = hose->io_resource.end -
  1610. hose->io_resource.start + 1;
  1611. if (address >= hose->io_base_phys &&
  1612. address < (hose->io_base_phys + size)) {
  1613. unsigned long base =
  1614. (unsigned long)hose->io_base_virt - _IO_BASE;
  1615. return base + (address - hose->io_base_phys);
  1616. }
  1617. }
  1618. return (unsigned int)-1;
  1619. }
  1620. EXPORT_SYMBOL(pci_address_to_pio);
  1621. /*
  1622. * Null PCI config access functions, for the case when we can't
  1623. * find a hose.
  1624. */
  1625. #define NULL_PCI_OP(rw, size, type) \
  1626. static int \
  1627. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1628. { \
  1629. return PCIBIOS_DEVICE_NOT_FOUND; \
  1630. }
  1631. static int
  1632. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1633. int len, u32 *val)
  1634. {
  1635. return PCIBIOS_DEVICE_NOT_FOUND;
  1636. }
  1637. static int
  1638. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1639. int len, u32 val)
  1640. {
  1641. return PCIBIOS_DEVICE_NOT_FOUND;
  1642. }
  1643. static struct pci_ops null_pci_ops =
  1644. {
  1645. null_read_config,
  1646. null_write_config
  1647. };
  1648. /*
  1649. * These functions are used early on before PCI scanning is done
  1650. * and all of the pci_dev and pci_bus structures have been created.
  1651. */
  1652. static struct pci_bus *
  1653. fake_pci_bus(struct pci_controller *hose, int busnr)
  1654. {
  1655. static struct pci_bus bus;
  1656. if (hose == 0) {
  1657. hose = pci_bus_to_hose(busnr);
  1658. if (hose == 0)
  1659. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1660. }
  1661. bus.number = busnr;
  1662. bus.sysdata = hose;
  1663. bus.ops = hose? hose->ops: &null_pci_ops;
  1664. return &bus;
  1665. }
  1666. #define EARLY_PCI_OP(rw, size, type) \
  1667. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1668. int devfn, int offset, type value) \
  1669. { \
  1670. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1671. devfn, offset, value); \
  1672. }
  1673. EARLY_PCI_OP(read, byte, u8 *)
  1674. EARLY_PCI_OP(read, word, u16 *)
  1675. EARLY_PCI_OP(read, dword, u32 *)
  1676. EARLY_PCI_OP(write, byte, u8)
  1677. EARLY_PCI_OP(write, word, u16)
  1678. EARLY_PCI_OP(write, dword, u32)