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 [%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, 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. " (%lld 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 %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. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  412. start >> 16);
  413. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  414. end >> 16);
  415. io_base_lo |= PCI_IO_RANGE_TYPE_32;
  416. } else
  417. io_base_lo |= PCI_IO_RANGE_TYPE_16;
  418. pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
  419. pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
  420. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  421. == IORESOURCE_MEM) {
  422. off = hose->pci_mem_offset;
  423. mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  424. mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  425. pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
  426. pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
  427. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  428. == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
  429. off = hose->pci_mem_offset;
  430. mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
  431. mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
  432. pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
  433. pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
  434. } else {
  435. DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
  436. pci_name(dev), i, res->flags);
  437. }
  438. pci_write_config_word(dev, PCI_COMMAND, cmd);
  439. }
  440. static inline void alloc_resource(struct pci_dev *dev, int idx)
  441. {
  442. struct resource *pr, *r = &dev->resource[idx];
  443. DBG("PCI:%s: Resource %d: %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. int *bus_range;
  579. int len;
  580. if (pci_bus >= pci_bus_count)
  581. return;
  582. bus_range = (int *) get_property(node, "bus-range", &len);
  583. if (bus_range == NULL || len < 2 * sizeof(int)) {
  584. printk(KERN_WARNING "Can't get bus-range for %s, "
  585. "assuming it starts at 0\n", node->full_name);
  586. pci_to_OF_bus_map[pci_bus] = 0;
  587. } else
  588. pci_to_OF_bus_map[pci_bus] = bus_range[0];
  589. for (node=node->child; node != 0;node = node->sibling) {
  590. struct pci_dev* dev;
  591. unsigned int *class_code, *reg;
  592. class_code = (unsigned int *) get_property(node, "class-code", NULL);
  593. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  594. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  595. continue;
  596. reg = (unsigned int *)get_property(node, "reg", NULL);
  597. if (!reg)
  598. continue;
  599. dev = pci_find_slot(pci_bus, ((reg[0] >> 8) & 0xff));
  600. if (!dev || !dev->subordinate)
  601. continue;
  602. make_one_node_map(node, dev->subordinate->number);
  603. }
  604. }
  605. void
  606. pcibios_make_OF_bus_map(void)
  607. {
  608. int i;
  609. struct pci_controller* hose;
  610. u8* of_prop_map;
  611. pci_to_OF_bus_map = (u8*)kmalloc(pci_bus_count, GFP_KERNEL);
  612. if (!pci_to_OF_bus_map) {
  613. printk(KERN_ERR "Can't allocate OF bus map !\n");
  614. return;
  615. }
  616. /* We fill the bus map with invalid values, that helps
  617. * debugging.
  618. */
  619. for (i=0; i<pci_bus_count; i++)
  620. pci_to_OF_bus_map[i] = 0xff;
  621. /* For each hose, we begin searching bridges */
  622. for(hose=hose_head; hose; hose=hose->next) {
  623. struct device_node* node;
  624. node = (struct device_node *)hose->arch_data;
  625. if (!node)
  626. continue;
  627. make_one_node_map(node, hose->first_busno);
  628. }
  629. of_prop_map = get_property(find_path_device("/"), "pci-OF-bus-map", NULL);
  630. if (of_prop_map)
  631. memcpy(of_prop_map, pci_to_OF_bus_map, pci_bus_count);
  632. #ifdef DEBUG
  633. printk("PCI->OF bus map:\n");
  634. for (i=0; i<pci_bus_count; i++) {
  635. if (pci_to_OF_bus_map[i] == 0xff)
  636. continue;
  637. printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
  638. }
  639. #endif
  640. }
  641. typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
  642. static struct device_node*
  643. scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
  644. {
  645. struct device_node* sub_node;
  646. for (; node != 0;node = node->sibling) {
  647. unsigned int *class_code;
  648. if (filter(node, data))
  649. return node;
  650. /* For PCI<->PCI bridges or CardBus bridges, we go down
  651. * Note: some OFs create a parent node "multifunc-device" as
  652. * a fake root for all functions of a multi-function device,
  653. * we go down them as well.
  654. */
  655. class_code = (unsigned int *) get_property(node, "class-code", NULL);
  656. if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  657. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
  658. strcmp(node->name, "multifunc-device"))
  659. continue;
  660. sub_node = scan_OF_pci_childs(node->child, filter, data);
  661. if (sub_node)
  662. return sub_node;
  663. }
  664. return NULL;
  665. }
  666. static int
  667. scan_OF_pci_childs_iterator(struct device_node* node, void* data)
  668. {
  669. unsigned int *reg;
  670. u8* fdata = (u8*)data;
  671. reg = (unsigned int *) get_property(node, "reg", NULL);
  672. if (reg && ((reg[0] >> 8) & 0xff) == fdata[1]
  673. && ((reg[0] >> 16) & 0xff) == fdata[0])
  674. return 1;
  675. return 0;
  676. }
  677. static struct device_node*
  678. scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
  679. {
  680. u8 filter_data[2] = {bus, dev_fn};
  681. return scan_OF_pci_childs(node, scan_OF_pci_childs_iterator, filter_data);
  682. }
  683. /*
  684. * Scans the OF tree for a device node matching a PCI device
  685. */
  686. struct device_node *
  687. pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
  688. {
  689. struct pci_controller *hose;
  690. struct device_node *node;
  691. int busnr;
  692. if (!have_of)
  693. return NULL;
  694. /* Lookup the hose */
  695. busnr = bus->number;
  696. hose = pci_bus_to_hose(busnr);
  697. if (!hose)
  698. return NULL;
  699. /* Check it has an OF node associated */
  700. node = (struct device_node *) hose->arch_data;
  701. if (!node)
  702. return NULL;
  703. /* Fixup bus number according to what OF think it is. */
  704. #ifdef CONFIG_PPC_PMAC
  705. /* The G5 need a special case here. Basically, we don't remap all
  706. * busses on it so we don't create the pci-OF-map. However, we do
  707. * remap the AGP bus and so have to deal with it. A future better
  708. * fix has to be done by making the remapping per-host and always
  709. * filling the pci_to_OF map. --BenH
  710. */
  711. if (machine_is(powermac) && busnr >= 0xf0)
  712. busnr -= 0xf0;
  713. else
  714. #endif
  715. if (pci_to_OF_bus_map)
  716. busnr = pci_to_OF_bus_map[busnr];
  717. if (busnr == 0xff)
  718. return NULL;
  719. /* Now, lookup childs of the hose */
  720. return scan_OF_childs_for_device(node->child, busnr, devfn);
  721. }
  722. EXPORT_SYMBOL(pci_busdev_to_OF_node);
  723. struct device_node*
  724. pci_device_to_OF_node(struct pci_dev *dev)
  725. {
  726. return pci_busdev_to_OF_node(dev->bus, dev->devfn);
  727. }
  728. EXPORT_SYMBOL(pci_device_to_OF_node);
  729. /* This routine is meant to be used early during boot, when the
  730. * PCI bus numbers have not yet been assigned, and you need to
  731. * issue PCI config cycles to an OF device.
  732. * It could also be used to "fix" RTAS config cycles if you want
  733. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  734. * config cycles.
  735. */
  736. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  737. {
  738. if (!have_of)
  739. return NULL;
  740. while(node) {
  741. struct pci_controller* hose;
  742. for (hose=hose_head;hose;hose=hose->next)
  743. if (hose->arch_data == node)
  744. return hose;
  745. node=node->parent;
  746. }
  747. return NULL;
  748. }
  749. static int
  750. find_OF_pci_device_filter(struct device_node* node, void* data)
  751. {
  752. return ((void *)node == data);
  753. }
  754. /*
  755. * Returns the PCI device matching a given OF node
  756. */
  757. int
  758. pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
  759. {
  760. unsigned int *reg;
  761. struct pci_controller* hose;
  762. struct pci_dev* dev = NULL;
  763. if (!have_of)
  764. return -ENODEV;
  765. /* Make sure it's really a PCI device */
  766. hose = pci_find_hose_for_OF_device(node);
  767. if (!hose || !hose->arch_data)
  768. return -ENODEV;
  769. if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
  770. find_OF_pci_device_filter, (void *)node))
  771. return -ENODEV;
  772. reg = (unsigned int *) get_property(node, "reg", NULL);
  773. if (!reg)
  774. return -ENODEV;
  775. *bus = (reg[0] >> 16) & 0xff;
  776. *devfn = ((reg[0] >> 8) & 0xff);
  777. /* Ok, here we need some tweak. If we have already renumbered
  778. * all busses, we can't rely on the OF bus number any more.
  779. * the pci_to_OF_bus_map is not enough as several PCI busses
  780. * may match the same OF bus number.
  781. */
  782. if (!pci_to_OF_bus_map)
  783. return 0;
  784. for_each_pci_dev(dev)
  785. if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
  786. dev->devfn == *devfn) {
  787. *bus = dev->bus->number;
  788. pci_dev_put(dev);
  789. return 0;
  790. }
  791. return -ENODEV;
  792. }
  793. EXPORT_SYMBOL(pci_device_from_OF_node);
  794. void __init
  795. pci_process_bridge_OF_ranges(struct pci_controller *hose,
  796. struct device_node *dev, int primary)
  797. {
  798. static unsigned int static_lc_ranges[256] __initdata;
  799. unsigned int *dt_ranges, *lc_ranges, *ranges, *prev;
  800. unsigned int size;
  801. int rlen = 0, orig_rlen;
  802. int memno = 0;
  803. struct resource *res;
  804. int np, na = prom_n_addr_cells(dev);
  805. np = na + 5;
  806. /* First we try to merge ranges to fix a problem with some pmacs
  807. * that can have more than 3 ranges, fortunately using contiguous
  808. * addresses -- BenH
  809. */
  810. dt_ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
  811. if (!dt_ranges)
  812. return;
  813. /* Sanity check, though hopefully that never happens */
  814. if (rlen > sizeof(static_lc_ranges)) {
  815. printk(KERN_WARNING "OF ranges property too large !\n");
  816. rlen = sizeof(static_lc_ranges);
  817. }
  818. lc_ranges = static_lc_ranges;
  819. memcpy(lc_ranges, dt_ranges, rlen);
  820. orig_rlen = rlen;
  821. /* Let's work on a copy of the "ranges" property instead of damaging
  822. * the device-tree image in memory
  823. */
  824. ranges = lc_ranges;
  825. prev = NULL;
  826. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  827. if (prev) {
  828. if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
  829. (prev[2] + prev[na+4]) == ranges[2] &&
  830. (prev[na+2] + prev[na+4]) == ranges[na+2]) {
  831. prev[na+4] += ranges[na+4];
  832. ranges[0] = 0;
  833. ranges += np;
  834. continue;
  835. }
  836. }
  837. prev = ranges;
  838. ranges += np;
  839. }
  840. /*
  841. * The ranges property is laid out as an array of elements,
  842. * each of which comprises:
  843. * cells 0 - 2: a PCI address
  844. * cells 3 or 3+4: a CPU physical address
  845. * (size depending on dev->n_addr_cells)
  846. * cells 4+5 or 5+6: the size of the range
  847. */
  848. ranges = lc_ranges;
  849. rlen = orig_rlen;
  850. while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
  851. res = NULL;
  852. size = ranges[na+4];
  853. switch ((ranges[0] >> 24) & 0x3) {
  854. case 1: /* I/O space */
  855. if (ranges[2] != 0)
  856. break;
  857. hose->io_base_phys = ranges[na+2];
  858. /* limit I/O space to 16MB */
  859. if (size > 0x01000000)
  860. size = 0x01000000;
  861. hose->io_base_virt = ioremap(ranges[na+2], size);
  862. if (primary)
  863. isa_io_base = (unsigned long) hose->io_base_virt;
  864. res = &hose->io_resource;
  865. res->flags = IORESOURCE_IO;
  866. res->start = ranges[2];
  867. DBG("PCI: IO 0x%llx -> 0x%llx\n",
  868. res->start, res->start + size - 1);
  869. break;
  870. case 2: /* memory space */
  871. memno = 0;
  872. if (ranges[1] == 0 && ranges[2] == 0
  873. && ranges[na+4] <= (16 << 20)) {
  874. /* 1st 16MB, i.e. ISA memory area */
  875. if (primary)
  876. isa_mem_base = ranges[na+2];
  877. memno = 1;
  878. }
  879. while (memno < 3 && hose->mem_resources[memno].flags)
  880. ++memno;
  881. if (memno == 0)
  882. hose->pci_mem_offset = ranges[na+2] - ranges[2];
  883. if (memno < 3) {
  884. res = &hose->mem_resources[memno];
  885. res->flags = IORESOURCE_MEM;
  886. if(ranges[0] & 0x40000000)
  887. res->flags |= IORESOURCE_PREFETCH;
  888. res->start = ranges[na+2];
  889. DBG("PCI: MEM[%d] 0x%llx -> 0x%llx\n", memno,
  890. res->start, res->start + size - 1);
  891. }
  892. break;
  893. }
  894. if (res != NULL) {
  895. res->name = dev->full_name;
  896. res->end = res->start + size - 1;
  897. res->parent = NULL;
  898. res->sibling = NULL;
  899. res->child = NULL;
  900. }
  901. ranges += np;
  902. }
  903. }
  904. /* We create the "pci-OF-bus-map" property now so it appears in the
  905. * /proc device tree
  906. */
  907. void __init
  908. pci_create_OF_bus_map(void)
  909. {
  910. struct property* of_prop;
  911. of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
  912. if (of_prop && find_path_device("/")) {
  913. memset(of_prop, -1, sizeof(struct property) + 256);
  914. of_prop->name = "pci-OF-bus-map";
  915. of_prop->length = 256;
  916. of_prop->value = (unsigned char *)&of_prop[1];
  917. prom_add_property(find_path_device("/"), of_prop);
  918. }
  919. }
  920. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  921. {
  922. struct pci_dev *pdev;
  923. struct device_node *np;
  924. pdev = to_pci_dev (dev);
  925. np = pci_device_to_OF_node(pdev);
  926. if (np == NULL || np->full_name == NULL)
  927. return 0;
  928. return sprintf(buf, "%s", np->full_name);
  929. }
  930. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  931. #else /* CONFIG_PPC_OF */
  932. void pcibios_make_OF_bus_map(void)
  933. {
  934. }
  935. #endif /* CONFIG_PPC_OF */
  936. /* Add sysfs properties */
  937. void pcibios_add_platform_entries(struct pci_dev *pdev)
  938. {
  939. #ifdef CONFIG_PPC_OF
  940. device_create_file(&pdev->dev, &dev_attr_devspec);
  941. #endif /* CONFIG_PPC_OF */
  942. }
  943. #ifdef CONFIG_PPC_PMAC
  944. /*
  945. * This set of routines checks for PCI<->PCI bridges that have closed
  946. * IO resources and have child devices. It tries to re-open an IO
  947. * window on them.
  948. *
  949. * This is a _temporary_ fix to workaround a problem with Apple's OF
  950. * closing IO windows on P2P bridges when the OF drivers of cards
  951. * below this bridge don't claim any IO range (typically ATI or
  952. * Adaptec).
  953. *
  954. * A more complete fix would be to use drivers/pci/setup-bus.c, which
  955. * involves a working pcibios_fixup_pbus_ranges(), some more care about
  956. * ordering when creating the host bus resources, and maybe a few more
  957. * minor tweaks
  958. */
  959. /* Initialize bridges with base/limit values we have collected */
  960. static void __init
  961. do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
  962. {
  963. struct pci_dev *bridge = bus->self;
  964. struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
  965. u32 l;
  966. u16 w;
  967. struct resource res;
  968. if (bus->resource[0] == NULL)
  969. return;
  970. res = *(bus->resource[0]);
  971. DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
  972. res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
  973. res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
  974. DBG(" IO window: %016llx-%016llx\n", res.start, res.end);
  975. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  976. pci_read_config_dword(bridge, PCI_IO_BASE, &l);
  977. l &= 0xffff000f;
  978. l |= (res.start >> 8) & 0x00f0;
  979. l |= res.end & 0xf000;
  980. pci_write_config_dword(bridge, PCI_IO_BASE, l);
  981. if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  982. l = (res.start >> 16) | (res.end & 0xffff0000);
  983. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
  984. }
  985. pci_read_config_word(bridge, PCI_COMMAND, &w);
  986. w |= PCI_COMMAND_IO;
  987. pci_write_config_word(bridge, PCI_COMMAND, w);
  988. #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
  989. if (enable_vga) {
  990. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
  991. w |= PCI_BRIDGE_CTL_VGA;
  992. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
  993. }
  994. #endif
  995. }
  996. /* This function is pretty basic and actually quite broken for the
  997. * general case, it's enough for us right now though. It's supposed
  998. * to tell us if we need to open an IO range at all or not and what
  999. * size.
  1000. */
  1001. static int __init
  1002. check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
  1003. {
  1004. struct pci_dev *dev;
  1005. int i;
  1006. int rc = 0;
  1007. #define push_end(res, mask) do { \
  1008. BUG_ON((mask+1) & mask); \
  1009. res->end = (res->end + mask) | mask; \
  1010. } while (0)
  1011. list_for_each_entry(dev, &bus->devices, bus_list) {
  1012. u16 class = dev->class >> 8;
  1013. if (class == PCI_CLASS_DISPLAY_VGA ||
  1014. class == PCI_CLASS_NOT_DEFINED_VGA)
  1015. *found_vga = 1;
  1016. if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
  1017. rc |= check_for_io_childs(dev->subordinate, res, found_vga);
  1018. if (class == PCI_CLASS_BRIDGE_CARDBUS)
  1019. push_end(res, 0xfff);
  1020. for (i=0; i<PCI_NUM_RESOURCES; i++) {
  1021. struct resource *r;
  1022. unsigned long r_size;
  1023. if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
  1024. && i >= PCI_BRIDGE_RESOURCES)
  1025. continue;
  1026. r = &dev->resource[i];
  1027. r_size = r->end - r->start;
  1028. if (r_size < 0xfff)
  1029. r_size = 0xfff;
  1030. if (r->flags & IORESOURCE_IO && (r_size) != 0) {
  1031. rc = 1;
  1032. push_end(res, r_size);
  1033. }
  1034. }
  1035. }
  1036. return rc;
  1037. }
  1038. /* Here we scan all P2P bridges of a given level that have a closed
  1039. * IO window. Note that the test for the presence of a VGA card should
  1040. * be improved to take into account already configured P2P bridges,
  1041. * currently, we don't see them and might end up configuring 2 bridges
  1042. * with VGA pass through enabled
  1043. */
  1044. static void __init
  1045. do_fixup_p2p_level(struct pci_bus *bus)
  1046. {
  1047. struct pci_bus *b;
  1048. int i, parent_io;
  1049. int has_vga = 0;
  1050. for (parent_io=0; parent_io<4; parent_io++)
  1051. if (bus->resource[parent_io]
  1052. && bus->resource[parent_io]->flags & IORESOURCE_IO)
  1053. break;
  1054. if (parent_io >= 4)
  1055. return;
  1056. list_for_each_entry(b, &bus->children, node) {
  1057. struct pci_dev *d = b->self;
  1058. struct pci_controller* hose = (struct pci_controller *)d->sysdata;
  1059. struct resource *res = b->resource[0];
  1060. struct resource tmp_res;
  1061. unsigned long max;
  1062. int found_vga = 0;
  1063. memset(&tmp_res, 0, sizeof(tmp_res));
  1064. tmp_res.start = bus->resource[parent_io]->start;
  1065. /* We don't let low addresses go through that closed P2P bridge, well,
  1066. * that may not be necessary but I feel safer that way
  1067. */
  1068. if (tmp_res.start == 0)
  1069. tmp_res.start = 0x1000;
  1070. if (!list_empty(&b->devices) && res && res->flags == 0 &&
  1071. res != bus->resource[parent_io] &&
  1072. (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
  1073. check_for_io_childs(b, &tmp_res, &found_vga)) {
  1074. u8 io_base_lo;
  1075. printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
  1076. if (found_vga) {
  1077. if (has_vga) {
  1078. printk(KERN_WARNING "Skipping VGA, already active"
  1079. " on bus segment\n");
  1080. found_vga = 0;
  1081. } else
  1082. has_vga = 1;
  1083. }
  1084. pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
  1085. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
  1086. max = ((unsigned long) hose->io_base_virt
  1087. - isa_io_base) + 0xffffffff;
  1088. else
  1089. max = ((unsigned long) hose->io_base_virt
  1090. - isa_io_base) + 0xffff;
  1091. *res = tmp_res;
  1092. res->flags = IORESOURCE_IO;
  1093. res->name = b->name;
  1094. /* Find a resource in the parent where we can allocate */
  1095. for (i = 0 ; i < 4; i++) {
  1096. struct resource *r = bus->resource[i];
  1097. if (!r)
  1098. continue;
  1099. if ((r->flags & IORESOURCE_IO) == 0)
  1100. continue;
  1101. DBG("Trying to allocate from %016llx, size %016llx from parent"
  1102. " res %d: %016llx -> %016llx\n",
  1103. res->start, res->end, i, r->start, r->end);
  1104. if (allocate_resource(r, res, res->end + 1, res->start, max,
  1105. res->end + 1, NULL, NULL) < 0) {
  1106. DBG("Failed !\n");
  1107. continue;
  1108. }
  1109. do_update_p2p_io_resource(b, found_vga);
  1110. break;
  1111. }
  1112. }
  1113. do_fixup_p2p_level(b);
  1114. }
  1115. }
  1116. static void
  1117. pcibios_fixup_p2p_bridges(void)
  1118. {
  1119. struct pci_bus *b;
  1120. list_for_each_entry(b, &pci_root_buses, node)
  1121. do_fixup_p2p_level(b);
  1122. }
  1123. #endif /* CONFIG_PPC_PMAC */
  1124. static int __init
  1125. pcibios_init(void)
  1126. {
  1127. struct pci_controller *hose;
  1128. struct pci_bus *bus;
  1129. int next_busno;
  1130. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  1131. /* Scan all of the recorded PCI controllers. */
  1132. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  1133. if (pci_assign_all_buses)
  1134. hose->first_busno = next_busno;
  1135. hose->last_busno = 0xff;
  1136. bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
  1137. hose->last_busno = bus->subordinate;
  1138. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  1139. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  1140. }
  1141. pci_bus_count = next_busno;
  1142. /* OpenFirmware based machines need a map of OF bus
  1143. * numbers vs. kernel bus numbers since we may have to
  1144. * remap them.
  1145. */
  1146. if (pci_assign_all_buses && have_of)
  1147. pcibios_make_OF_bus_map();
  1148. /* Do machine dependent PCI interrupt routing */
  1149. if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
  1150. pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
  1151. /* Call machine dependent fixup */
  1152. if (ppc_md.pcibios_fixup)
  1153. ppc_md.pcibios_fixup();
  1154. /* Allocate and assign resources */
  1155. pcibios_allocate_bus_resources(&pci_root_buses);
  1156. pcibios_allocate_resources(0);
  1157. pcibios_allocate_resources(1);
  1158. #ifdef CONFIG_PPC_PMAC
  1159. pcibios_fixup_p2p_bridges();
  1160. #endif /* CONFIG_PPC_PMAC */
  1161. pcibios_assign_resources();
  1162. /* Call machine dependent post-init code */
  1163. if (ppc_md.pcibios_after_init)
  1164. ppc_md.pcibios_after_init();
  1165. return 0;
  1166. }
  1167. subsys_initcall(pcibios_init);
  1168. unsigned char __init
  1169. common_swizzle(struct pci_dev *dev, unsigned char *pinp)
  1170. {
  1171. struct pci_controller *hose = dev->sysdata;
  1172. if (dev->bus->number != hose->first_busno) {
  1173. u8 pin = *pinp;
  1174. do {
  1175. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  1176. /* Move up the chain of bridges. */
  1177. dev = dev->bus->self;
  1178. } while (dev->bus->self);
  1179. *pinp = pin;
  1180. /* The slot is the idsel of the last bridge. */
  1181. }
  1182. return PCI_SLOT(dev->devfn);
  1183. }
  1184. unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
  1185. unsigned long start, unsigned long size)
  1186. {
  1187. return start;
  1188. }
  1189. void __init pcibios_fixup_bus(struct pci_bus *bus)
  1190. {
  1191. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  1192. unsigned long io_offset;
  1193. struct resource *res;
  1194. int i;
  1195. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  1196. if (bus->parent == NULL) {
  1197. /* This is a host bridge - fill in its resources */
  1198. hose->bus = bus;
  1199. bus->resource[0] = res = &hose->io_resource;
  1200. if (!res->flags) {
  1201. if (io_offset)
  1202. printk(KERN_ERR "I/O resource not set for host"
  1203. " bridge %d\n", hose->index);
  1204. res->start = 0;
  1205. res->end = IO_SPACE_LIMIT;
  1206. res->flags = IORESOURCE_IO;
  1207. }
  1208. res->start += io_offset;
  1209. res->end += io_offset;
  1210. for (i = 0; i < 3; ++i) {
  1211. res = &hose->mem_resources[i];
  1212. if (!res->flags) {
  1213. if (i > 0)
  1214. continue;
  1215. printk(KERN_ERR "Memory resource not set for "
  1216. "host bridge %d\n", hose->index);
  1217. res->start = hose->pci_mem_offset;
  1218. res->end = ~0U;
  1219. res->flags = IORESOURCE_MEM;
  1220. }
  1221. bus->resource[i+1] = res;
  1222. }
  1223. } else {
  1224. /* This is a subordinate bridge */
  1225. pci_read_bridge_bases(bus);
  1226. for (i = 0; i < 4; ++i) {
  1227. if ((res = bus->resource[i]) == NULL)
  1228. continue;
  1229. if (!res->flags)
  1230. continue;
  1231. if (io_offset && (res->flags & IORESOURCE_IO)) {
  1232. res->start += io_offset;
  1233. res->end += io_offset;
  1234. } else if (hose->pci_mem_offset
  1235. && (res->flags & IORESOURCE_MEM)) {
  1236. res->start += hose->pci_mem_offset;
  1237. res->end += hose->pci_mem_offset;
  1238. }
  1239. }
  1240. }
  1241. if (ppc_md.pcibios_fixup_bus)
  1242. ppc_md.pcibios_fixup_bus(bus);
  1243. }
  1244. char __init *pcibios_setup(char *str)
  1245. {
  1246. return str;
  1247. }
  1248. /* the next one is stolen from the alpha port... */
  1249. void __init
  1250. pcibios_update_irq(struct pci_dev *dev, int irq)
  1251. {
  1252. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  1253. /* XXX FIXME - update OF device tree node interrupt property */
  1254. }
  1255. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1256. {
  1257. u16 cmd, old_cmd;
  1258. int idx;
  1259. struct resource *r;
  1260. if (ppc_md.pcibios_enable_device_hook)
  1261. if (ppc_md.pcibios_enable_device_hook(dev, 0))
  1262. return -EINVAL;
  1263. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1264. old_cmd = cmd;
  1265. for (idx=0; idx<6; idx++) {
  1266. r = &dev->resource[idx];
  1267. if (r->flags & IORESOURCE_UNSET) {
  1268. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  1269. return -EINVAL;
  1270. }
  1271. if (r->flags & IORESOURCE_IO)
  1272. cmd |= PCI_COMMAND_IO;
  1273. if (r->flags & IORESOURCE_MEM)
  1274. cmd |= PCI_COMMAND_MEMORY;
  1275. }
  1276. if (cmd != old_cmd) {
  1277. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  1278. pci_name(dev), old_cmd, cmd);
  1279. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1280. }
  1281. return 0;
  1282. }
  1283. struct pci_controller*
  1284. pci_bus_to_hose(int bus)
  1285. {
  1286. struct pci_controller* hose = hose_head;
  1287. for (; hose; hose = hose->next)
  1288. if (bus >= hose->first_busno && bus <= hose->last_busno)
  1289. return hose;
  1290. return NULL;
  1291. }
  1292. void __iomem *
  1293. pci_bus_io_base(unsigned int bus)
  1294. {
  1295. struct pci_controller *hose;
  1296. hose = pci_bus_to_hose(bus);
  1297. if (!hose)
  1298. return NULL;
  1299. return hose->io_base_virt;
  1300. }
  1301. unsigned long
  1302. pci_bus_io_base_phys(unsigned int bus)
  1303. {
  1304. struct pci_controller *hose;
  1305. hose = pci_bus_to_hose(bus);
  1306. if (!hose)
  1307. return 0;
  1308. return hose->io_base_phys;
  1309. }
  1310. unsigned long
  1311. pci_bus_mem_base_phys(unsigned int bus)
  1312. {
  1313. struct pci_controller *hose;
  1314. hose = pci_bus_to_hose(bus);
  1315. if (!hose)
  1316. return 0;
  1317. return hose->pci_mem_offset;
  1318. }
  1319. unsigned long
  1320. pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
  1321. {
  1322. /* Hack alert again ! See comments in chrp_pci.c
  1323. */
  1324. struct pci_controller* hose =
  1325. (struct pci_controller *)pdev->sysdata;
  1326. if (hose && res->flags & IORESOURCE_MEM)
  1327. return res->start - hose->pci_mem_offset;
  1328. /* We may want to do something with IOs here... */
  1329. return res->start;
  1330. }
  1331. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  1332. unsigned long *offset,
  1333. enum pci_mmap_state mmap_state)
  1334. {
  1335. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1336. unsigned long io_offset = 0;
  1337. int i, res_bit;
  1338. if (hose == 0)
  1339. return NULL; /* should never happen */
  1340. /* If memory, add on the PCI bridge address offset */
  1341. if (mmap_state == pci_mmap_mem) {
  1342. *offset += hose->pci_mem_offset;
  1343. res_bit = IORESOURCE_MEM;
  1344. } else {
  1345. io_offset = hose->io_base_virt - ___IO_BASE;
  1346. *offset += io_offset;
  1347. res_bit = IORESOURCE_IO;
  1348. }
  1349. /*
  1350. * Check that the offset requested corresponds to one of the
  1351. * resources of the device.
  1352. */
  1353. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1354. struct resource *rp = &dev->resource[i];
  1355. int flags = rp->flags;
  1356. /* treat ROM as memory (should be already) */
  1357. if (i == PCI_ROM_RESOURCE)
  1358. flags |= IORESOURCE_MEM;
  1359. /* Active and same type? */
  1360. if ((flags & res_bit) == 0)
  1361. continue;
  1362. /* In the range of this resource? */
  1363. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  1364. continue;
  1365. /* found it! construct the final physical address */
  1366. if (mmap_state == pci_mmap_io)
  1367. *offset += hose->io_base_phys - io_offset;
  1368. return rp;
  1369. }
  1370. return NULL;
  1371. }
  1372. /*
  1373. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  1374. * device mapping.
  1375. */
  1376. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  1377. pgprot_t protection,
  1378. enum pci_mmap_state mmap_state,
  1379. int write_combine)
  1380. {
  1381. unsigned long prot = pgprot_val(protection);
  1382. /* Write combine is always 0 on non-memory space mappings. On
  1383. * memory space, if the user didn't pass 1, we check for a
  1384. * "prefetchable" resource. This is a bit hackish, but we use
  1385. * this to workaround the inability of /sysfs to provide a write
  1386. * combine bit
  1387. */
  1388. if (mmap_state != pci_mmap_mem)
  1389. write_combine = 0;
  1390. else if (write_combine == 0) {
  1391. if (rp->flags & IORESOURCE_PREFETCH)
  1392. write_combine = 1;
  1393. }
  1394. /* XXX would be nice to have a way to ask for write-through */
  1395. prot |= _PAGE_NO_CACHE;
  1396. if (write_combine)
  1397. prot &= ~_PAGE_GUARDED;
  1398. else
  1399. prot |= _PAGE_GUARDED;
  1400. printk("PCI map for %s:%llx, prot: %lx\n", pci_name(dev),
  1401. (unsigned long long)rp->start, prot);
  1402. return __pgprot(prot);
  1403. }
  1404. /*
  1405. * This one is used by /dev/mem and fbdev who have no clue about the
  1406. * PCI device, it tries to find the PCI device first and calls the
  1407. * above routine
  1408. */
  1409. pgprot_t pci_phys_mem_access_prot(struct file *file,
  1410. unsigned long pfn,
  1411. unsigned long size,
  1412. pgprot_t protection)
  1413. {
  1414. struct pci_dev *pdev = NULL;
  1415. struct resource *found = NULL;
  1416. unsigned long prot = pgprot_val(protection);
  1417. unsigned long offset = pfn << PAGE_SHIFT;
  1418. int i;
  1419. if (page_is_ram(pfn))
  1420. return prot;
  1421. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  1422. for_each_pci_dev(pdev) {
  1423. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1424. struct resource *rp = &pdev->resource[i];
  1425. int flags = rp->flags;
  1426. /* Active and same type? */
  1427. if ((flags & IORESOURCE_MEM) == 0)
  1428. continue;
  1429. /* In the range of this resource? */
  1430. if (offset < (rp->start & PAGE_MASK) ||
  1431. offset > rp->end)
  1432. continue;
  1433. found = rp;
  1434. break;
  1435. }
  1436. if (found)
  1437. break;
  1438. }
  1439. if (found) {
  1440. if (found->flags & IORESOURCE_PREFETCH)
  1441. prot &= ~_PAGE_GUARDED;
  1442. pci_dev_put(pdev);
  1443. }
  1444. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  1445. return __pgprot(prot);
  1446. }
  1447. /*
  1448. * Perform the actual remap of the pages for a PCI device mapping, as
  1449. * appropriate for this architecture. The region in the process to map
  1450. * is described by vm_start and vm_end members of VMA, the base physical
  1451. * address is found in vm_pgoff.
  1452. * The pci device structure is provided so that architectures may make mapping
  1453. * decisions on a per-device or per-bus basis.
  1454. *
  1455. * Returns a negative error code on failure, zero on success.
  1456. */
  1457. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  1458. enum pci_mmap_state mmap_state,
  1459. int write_combine)
  1460. {
  1461. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1462. struct resource *rp;
  1463. int ret;
  1464. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  1465. if (rp == NULL)
  1466. return -EINVAL;
  1467. vma->vm_pgoff = offset >> PAGE_SHIFT;
  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)