pci.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  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. #undef DEBUG
  23. #ifdef DEBUG
  24. #define DBG(x...) printk(x)
  25. #else
  26. #define DBG(x...)
  27. #endif
  28. unsigned long isa_io_base = 0;
  29. unsigned long isa_mem_base = 0;
  30. unsigned long pci_dram_offset = 0;
  31. int pcibios_assign_bus_offset = 1;
  32. void pcibios_make_OF_bus_map(void);
  33. static int pci_relocate_bridge_resource(struct pci_bus *bus, int i);
  34. static int probe_resource(struct pci_bus *parent, struct resource *pr,
  35. struct resource *res, struct resource **conflict);
  36. static void update_bridge_base(struct pci_bus *bus, int i);
  37. static void pcibios_fixup_resources(struct pci_dev* dev);
  38. static void fixup_broken_pcnet32(struct pci_dev* dev);
  39. static int reparent_resources(struct resource *parent, struct resource *res);
  40. static void fixup_rev1_53c810(struct pci_dev* dev);
  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_busses;
  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_rev1_53c810(struct pci_dev* dev)
  54. {
  55. /* rev 1 ncr53c810 chips don't set the class at all which means
  56. * they don't get their resources remapped. Fix that here.
  57. */
  58. if ((dev->class == PCI_CLASS_NOT_DEFINED)) {
  59. printk("NCR 53c810 rev 1 detected, setting PCI class.\n");
  60. dev->class = PCI_CLASS_STORAGE_SCSI;
  61. }
  62. }
  63. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_NCR, PCI_DEVICE_ID_NCR_53C810, fixup_rev1_53c810);
  64. static void
  65. fixup_broken_pcnet32(struct pci_dev* dev)
  66. {
  67. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  68. dev->vendor = PCI_VENDOR_ID_AMD;
  69. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  70. }
  71. }
  72. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  73. static void
  74. fixup_cpc710_pci64(struct pci_dev* dev)
  75. {
  76. /* Hide the PCI64 BARs from the kernel as their content doesn't
  77. * fit well in the resource management
  78. */
  79. dev->resource[0].start = dev->resource[0].end = 0;
  80. dev->resource[0].flags = 0;
  81. dev->resource[1].start = dev->resource[1].end = 0;
  82. dev->resource[1].flags = 0;
  83. }
  84. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
  85. static void
  86. pcibios_fixup_resources(struct pci_dev *dev)
  87. {
  88. struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
  89. int i;
  90. unsigned long offset;
  91. if (!hose) {
  92. printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
  93. return;
  94. }
  95. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  96. struct resource *res = dev->resource + i;
  97. if (!res->flags)
  98. continue;
  99. if (res->end == 0xffffffff) {
  100. DBG("PCI:%s Resource %d [%08lx-%08lx] is unassigned\n",
  101. pci_name(dev), i, res->start, res->end);
  102. res->end -= res->start;
  103. res->start = 0;
  104. res->flags |= IORESOURCE_UNSET;
  105. continue;
  106. }
  107. offset = 0;
  108. if (res->flags & IORESOURCE_MEM) {
  109. offset = hose->pci_mem_offset;
  110. } else if (res->flags & IORESOURCE_IO) {
  111. offset = (unsigned long) hose->io_base_virt
  112. - isa_io_base;
  113. }
  114. if (offset != 0) {
  115. res->start += offset;
  116. res->end += offset;
  117. #ifdef DEBUG
  118. printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n",
  119. i, res->flags, pci_name(dev),
  120. res->start - offset, res->start);
  121. #endif
  122. }
  123. }
  124. /* Call machine specific resource fixup */
  125. if (ppc_md.pcibios_fixup_resources)
  126. ppc_md.pcibios_fixup_resources(dev);
  127. }
  128. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  129. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  130. struct resource *res)
  131. {
  132. unsigned long offset = 0;
  133. struct pci_controller *hose = dev->sysdata;
  134. if (hose && res->flags & IORESOURCE_IO)
  135. offset = (unsigned long)hose->io_base_virt - isa_io_base;
  136. else if (hose && res->flags & IORESOURCE_MEM)
  137. offset = hose->pci_mem_offset;
  138. region->start = res->start - offset;
  139. region->end = res->end - offset;
  140. }
  141. EXPORT_SYMBOL(pcibios_resource_to_bus);
  142. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  143. struct pci_bus_region *region)
  144. {
  145. unsigned long offset = 0;
  146. struct pci_controller *hose = dev->sysdata;
  147. if (hose && res->flags & IORESOURCE_IO)
  148. offset = (unsigned long)hose->io_base_virt - isa_io_base;
  149. else if (hose && res->flags & IORESOURCE_MEM)
  150. offset = hose->pci_mem_offset;
  151. res->start = region->start + offset;
  152. res->end = region->end + offset;
  153. }
  154. EXPORT_SYMBOL(pcibios_bus_to_resource);
  155. /*
  156. * We need to avoid collisions with `mirrored' VGA ports
  157. * and other strange ISA hardware, so we always want the
  158. * addresses to be allocated in the 0x000-0x0ff region
  159. * modulo 0x400.
  160. *
  161. * Why? Because some silly external IO cards only decode
  162. * the low 10 bits of the IO address. The 0x00-0xff region
  163. * is reserved for motherboard devices that decode all 16
  164. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  165. * but we want to try to avoid allocating at 0x2900-0x2bff
  166. * which might have be mirrored at 0x0100-0x03ff..
  167. */
  168. void pcibios_align_resource(void *data, struct resource *res, unsigned long size,
  169. unsigned long align)
  170. {
  171. struct pci_dev *dev = data;
  172. if (res->flags & IORESOURCE_IO) {
  173. unsigned long start = res->start;
  174. if (size > 0x100) {
  175. printk(KERN_ERR "PCI: I/O Region %s/%d too large"
  176. " (%ld bytes)\n", pci_name(dev),
  177. dev->resource - res, size);
  178. }
  179. if (start & 0x300) {
  180. start = (start + 0x3ff) & ~0x3ff;
  181. res->start = start;
  182. }
  183. }
  184. }
  185. EXPORT_SYMBOL(pcibios_align_resource);
  186. /*
  187. * Handle resources of PCI devices. If the world were perfect, we could
  188. * just allocate all the resource regions and do nothing more. It isn't.
  189. * On the other hand, we cannot just re-allocate all devices, as it would
  190. * require us to know lots of host bridge internals. So we attempt to
  191. * keep as much of the original configuration as possible, but tweak it
  192. * when it's found to be wrong.
  193. *
  194. * Known BIOS problems we have to work around:
  195. * - I/O or memory regions not configured
  196. * - regions configured, but not enabled in the command register
  197. * - bogus I/O addresses above 64K used
  198. * - expansion ROMs left enabled (this may sound harmless, but given
  199. * the fact the PCI specs explicitly allow address decoders to be
  200. * shared between expansion ROMs and other resource regions, it's
  201. * at least dangerous)
  202. *
  203. * Our solution:
  204. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  205. * This gives us fixed barriers on where we can allocate.
  206. * (2) Allocate resources for all enabled devices. If there is
  207. * a collision, just mark the resource as unallocated. Also
  208. * disable expansion ROMs during this step.
  209. * (3) Try to allocate resources for disabled devices. If the
  210. * resources were assigned correctly, everything goes well,
  211. * if they weren't, they won't disturb allocation of other
  212. * resources.
  213. * (4) Assign new addresses to resources which were either
  214. * not configured at all or misconfigured. If explicitly
  215. * requested by the user, configure expansion ROM address
  216. * as well.
  217. */
  218. static void __init
  219. pcibios_allocate_bus_resources(struct list_head *bus_list)
  220. {
  221. struct pci_bus *bus;
  222. int i;
  223. struct resource *res, *pr;
  224. /* Depth-First Search on bus tree */
  225. list_for_each_entry(bus, bus_list, node) {
  226. for (i = 0; i < 4; ++i) {
  227. if ((res = bus->resource[i]) == NULL || !res->flags
  228. || res->start > res->end)
  229. continue;
  230. if (bus->parent == NULL)
  231. pr = (res->flags & IORESOURCE_IO)?
  232. &ioport_resource: &iomem_resource;
  233. else {
  234. pr = pci_find_parent_resource(bus->self, res);
  235. if (pr == res) {
  236. /* this happens when the generic PCI
  237. * code (wrongly) decides that this
  238. * bridge is transparent -- paulus
  239. */
  240. continue;
  241. }
  242. }
  243. DBG("PCI: bridge rsrc %lx..%lx (%lx), parent %p\n",
  244. res->start, res->end, res->flags, pr);
  245. if (pr) {
  246. if (request_resource(pr, res) == 0)
  247. continue;
  248. /*
  249. * Must be a conflict with an existing entry.
  250. * Move that entry (or entries) under the
  251. * bridge resource and try again.
  252. */
  253. if (reparent_resources(pr, res) == 0)
  254. continue;
  255. }
  256. printk(KERN_ERR "PCI: Cannot allocate resource region "
  257. "%d of PCI bridge %d\n", i, bus->number);
  258. if (pci_relocate_bridge_resource(bus, i))
  259. bus->resource[i] = NULL;
  260. }
  261. pcibios_allocate_bus_resources(&bus->children);
  262. }
  263. }
  264. /*
  265. * Reparent resource children of pr that conflict with res
  266. * under res, and make res replace those children.
  267. */
  268. static int __init
  269. reparent_resources(struct resource *parent, struct resource *res)
  270. {
  271. struct resource *p, **pp;
  272. struct resource **firstpp = NULL;
  273. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  274. if (p->end < res->start)
  275. continue;
  276. if (res->end < p->start)
  277. break;
  278. if (p->start < res->start || p->end > res->end)
  279. return -1; /* not completely contained */
  280. if (firstpp == NULL)
  281. firstpp = pp;
  282. }
  283. if (firstpp == NULL)
  284. return -1; /* didn't find any conflicting entries? */
  285. res->parent = parent;
  286. res->child = *firstpp;
  287. res->sibling = *pp;
  288. *firstpp = res;
  289. *pp = NULL;
  290. for (p = res->child; p != NULL; p = p->sibling) {
  291. p->parent = res;
  292. DBG(KERN_INFO "PCI: reparented %s [%lx..%lx] under %s\n",
  293. p->name, p->start, p->end, res->name);
  294. }
  295. return 0;
  296. }
  297. /*
  298. * A bridge has been allocated a range which is outside the range
  299. * of its parent bridge, so it needs to be moved.
  300. */
  301. static int __init
  302. pci_relocate_bridge_resource(struct pci_bus *bus, int i)
  303. {
  304. struct resource *res, *pr, *conflict;
  305. unsigned long try, size;
  306. int j;
  307. struct pci_bus *parent = bus->parent;
  308. if (parent == NULL) {
  309. /* shouldn't ever happen */
  310. printk(KERN_ERR "PCI: can't move host bridge resource\n");
  311. return -1;
  312. }
  313. res = bus->resource[i];
  314. if (res == NULL)
  315. return -1;
  316. pr = NULL;
  317. for (j = 0; j < 4; j++) {
  318. struct resource *r = parent->resource[j];
  319. if (!r)
  320. continue;
  321. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  322. continue;
  323. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
  324. pr = r;
  325. break;
  326. }
  327. if (res->flags & IORESOURCE_PREFETCH)
  328. pr = r;
  329. }
  330. if (pr == NULL)
  331. return -1;
  332. size = res->end - res->start;
  333. if (pr->start > pr->end || size > pr->end - pr->start)
  334. return -1;
  335. try = pr->end;
  336. for (;;) {
  337. res->start = try - size;
  338. res->end = try;
  339. if (probe_resource(bus->parent, pr, res, &conflict) == 0)
  340. break;
  341. if (conflict->start <= pr->start + size)
  342. return -1;
  343. try = conflict->start - 1;
  344. }
  345. if (request_resource(pr, res)) {
  346. DBG(KERN_ERR "PCI: huh? couldn't move to %lx..%lx\n",
  347. res->start, res->end);
  348. return -1; /* "can't happen" */
  349. }
  350. update_bridge_base(bus, i);
  351. printk(KERN_INFO "PCI: bridge %d resource %d moved to %lx..%lx\n",
  352. bus->number, i, res->start, res->end);
  353. return 0;
  354. }
  355. static int __init
  356. probe_resource(struct pci_bus *parent, struct resource *pr,
  357. struct resource *res, struct resource **conflict)
  358. {
  359. struct pci_bus *bus;
  360. struct pci_dev *dev;
  361. struct resource *r;
  362. int i;
  363. for (r = pr->child; r != NULL; r = r->sibling) {
  364. if (r->end >= res->start && res->end >= r->start) {
  365. *conflict = r;
  366. return 1;
  367. }
  368. }
  369. list_for_each_entry(bus, &parent->children, node) {
  370. for (i = 0; i < 4; ++i) {
  371. if ((r = bus->resource[i]) == NULL)
  372. continue;
  373. if (!r->flags || r->start > r->end || r == res)
  374. continue;
  375. if (pci_find_parent_resource(bus->self, r) != pr)
  376. continue;
  377. if (r->end >= res->start && res->end >= r->start) {
  378. *conflict = r;
  379. return 1;
  380. }
  381. }
  382. }
  383. list_for_each_entry(dev, &parent->devices, bus_list) {
  384. for (i = 0; i < 6; ++i) {
  385. r = &dev->resource[i];
  386. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  387. continue;
  388. if (pci_find_parent_resource(dev, r) != pr)
  389. continue;
  390. if (r->end >= res->start && res->end >= r->start) {
  391. *conflict = r;
  392. return 1;
  393. }
  394. }
  395. }
  396. return 0;
  397. }
  398. static void __init
  399. update_bridge_base(struct pci_bus *bus, int i)
  400. {
  401. struct resource *res = bus->resource[i];
  402. u8 io_base_lo, io_limit_lo;
  403. u16 mem_base, mem_limit;
  404. u16 cmd;
  405. unsigned long start, end, off;
  406. struct pci_dev *dev = bus->self;
  407. struct pci_controller *hose = dev->sysdata;
  408. if (!hose) {
  409. printk("update_bridge_base: no hose?\n");
  410. return;
  411. }
  412. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  413. pci_write_config_word(dev, PCI_COMMAND,
  414. cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
  415. if (res->flags & IORESOURCE_IO) {
  416. off = (unsigned long) hose->io_base_virt - isa_io_base;
  417. start = res->start - off;
  418. end = res->end - off;
  419. io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
  420. io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
  421. if (end > 0xffff) {
  422. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  423. start >> 16);
  424. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  425. end >> 16);
  426. io_base_lo |= PCI_IO_RANGE_TYPE_32;
  427. } else
  428. io_base_lo |= PCI_IO_RANGE_TYPE_16;
  429. pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
  430. pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
  431. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  432. == IORESOURCE_MEM) {
  433. off = hose->pci_mem_offset;
  434. mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  435. mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  436. pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
  437. pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
  438. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  439. == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
  440. off = hose->pci_mem_offset;
  441. mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
  442. mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
  443. pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
  444. pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
  445. } else {
  446. DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
  447. pci_name(dev), i, res->flags);
  448. }
  449. pci_write_config_word(dev, PCI_COMMAND, cmd);
  450. }
  451. static inline void alloc_resource(struct pci_dev *dev, int idx)
  452. {
  453. struct resource *pr, *r = &dev->resource[idx];
  454. DBG("PCI:%s: Resource %d: %08lx-%08lx (f=%lx)\n",
  455. pci_name(dev), idx, r->start, r->end, r->flags);
  456. pr = pci_find_parent_resource(dev, r);
  457. if (!pr || request_resource(pr, r) < 0) {
  458. printk(KERN_ERR "PCI: Cannot allocate resource region %d"
  459. " of device %s\n", idx, pci_name(dev));
  460. if (pr)
  461. DBG("PCI: parent is %p: %08lx-%08lx (f=%lx)\n",
  462. pr, pr->start, pr->end, pr->flags);
  463. /* We'll assign a new address later */
  464. r->flags |= IORESOURCE_UNSET;
  465. r->end -= r->start;
  466. r->start = 0;
  467. }
  468. }
  469. static void __init
  470. pcibios_allocate_resources(int pass)
  471. {
  472. struct pci_dev *dev = NULL;
  473. int idx, disabled;
  474. u16 command;
  475. struct resource *r;
  476. while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  477. pci_read_config_word(dev, PCI_COMMAND, &command);
  478. for (idx = 0; idx < 6; idx++) {
  479. r = &dev->resource[idx];
  480. if (r->parent) /* Already allocated */
  481. continue;
  482. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  483. continue; /* Not assigned at all */
  484. if (r->flags & IORESOURCE_IO)
  485. disabled = !(command & PCI_COMMAND_IO);
  486. else
  487. disabled = !(command & PCI_COMMAND_MEMORY);
  488. if (pass == disabled)
  489. alloc_resource(dev, idx);
  490. }
  491. if (pass)
  492. continue;
  493. r = &dev->resource[PCI_ROM_RESOURCE];
  494. if (r->flags & IORESOURCE_ROM_ENABLE) {
  495. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  496. u32 reg;
  497. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  498. r->flags &= ~IORESOURCE_ROM_ENABLE;
  499. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  500. pci_write_config_dword(dev, dev->rom_base_reg,
  501. reg & ~PCI_ROM_ADDRESS_ENABLE);
  502. }
  503. }
  504. }
  505. static void __init
  506. pcibios_assign_resources(void)
  507. {
  508. struct pci_dev *dev = NULL;
  509. int idx;
  510. struct resource *r;
  511. while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  512. int class = dev->class >> 8;
  513. /* Don't touch classless devices and host bridges */
  514. if (!class || class == PCI_CLASS_BRIDGE_HOST)
  515. continue;
  516. for (idx = 0; idx < 6; idx++) {
  517. r = &dev->resource[idx];
  518. /*
  519. * We shall assign a new address to this resource,
  520. * either because the BIOS (sic) forgot to do so
  521. * or because we have decided the old address was
  522. * unusable for some reason.
  523. */
  524. if ((r->flags & IORESOURCE_UNSET) && r->end &&
  525. (!ppc_md.pcibios_enable_device_hook ||
  526. !ppc_md.pcibios_enable_device_hook(dev, 1))) {
  527. r->flags &= ~IORESOURCE_UNSET;
  528. pci_assign_resource(dev, idx);
  529. }
  530. }
  531. #if 0 /* don't assign ROMs */
  532. r = &dev->resource[PCI_ROM_RESOURCE];
  533. r->end -= r->start;
  534. r->start = 0;
  535. if (r->end)
  536. pci_assign_resource(dev, PCI_ROM_RESOURCE);
  537. #endif
  538. }
  539. }
  540. int
  541. pcibios_enable_resources(struct pci_dev *dev, int mask)
  542. {
  543. u16 cmd, old_cmd;
  544. int idx;
  545. struct resource *r;
  546. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  547. old_cmd = cmd;
  548. for (idx=0; idx<6; idx++) {
  549. /* Only set up the requested stuff */
  550. if (!(mask & (1<<idx)))
  551. continue;
  552. r = &dev->resource[idx];
  553. if (r->flags & IORESOURCE_UNSET) {
  554. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  555. return -EINVAL;
  556. }
  557. if (r->flags & IORESOURCE_IO)
  558. cmd |= PCI_COMMAND_IO;
  559. if (r->flags & IORESOURCE_MEM)
  560. cmd |= PCI_COMMAND_MEMORY;
  561. }
  562. if (dev->resource[PCI_ROM_RESOURCE].start)
  563. cmd |= PCI_COMMAND_MEMORY;
  564. if (cmd != old_cmd) {
  565. printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  566. pci_write_config_word(dev, PCI_COMMAND, cmd);
  567. }
  568. return 0;
  569. }
  570. static int next_controller_index;
  571. struct pci_controller * __init
  572. pcibios_alloc_controller(void)
  573. {
  574. struct pci_controller *hose;
  575. hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
  576. memset(hose, 0, sizeof(struct pci_controller));
  577. *hose_tail = hose;
  578. hose_tail = &hose->next;
  579. hose->index = next_controller_index++;
  580. return hose;
  581. }
  582. #ifdef CONFIG_PPC_OF
  583. /*
  584. * Functions below are used on OpenFirmware machines.
  585. */
  586. static void __openfirmware
  587. make_one_node_map(struct device_node* node, u8 pci_bus)
  588. {
  589. int *bus_range;
  590. int len;
  591. if (pci_bus >= pci_bus_count)
  592. return;
  593. bus_range = (int *) get_property(node, "bus-range", &len);
  594. if (bus_range == NULL || len < 2 * sizeof(int)) {
  595. printk(KERN_WARNING "Can't get bus-range for %s, "
  596. "assuming it starts at 0\n", node->full_name);
  597. pci_to_OF_bus_map[pci_bus] = 0;
  598. } else
  599. pci_to_OF_bus_map[pci_bus] = bus_range[0];
  600. for (node=node->child; node != 0;node = node->sibling) {
  601. struct pci_dev* dev;
  602. unsigned int *class_code, *reg;
  603. class_code = (unsigned int *) get_property(node, "class-code", NULL);
  604. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  605. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  606. continue;
  607. reg = (unsigned int *)get_property(node, "reg", NULL);
  608. if (!reg)
  609. continue;
  610. dev = pci_find_slot(pci_bus, ((reg[0] >> 8) & 0xff));
  611. if (!dev || !dev->subordinate)
  612. continue;
  613. make_one_node_map(node, dev->subordinate->number);
  614. }
  615. }
  616. void __openfirmware
  617. pcibios_make_OF_bus_map(void)
  618. {
  619. int i;
  620. struct pci_controller* hose;
  621. u8* of_prop_map;
  622. pci_to_OF_bus_map = (u8*)kmalloc(pci_bus_count, GFP_KERNEL);
  623. if (!pci_to_OF_bus_map) {
  624. printk(KERN_ERR "Can't allocate OF bus map !\n");
  625. return;
  626. }
  627. /* We fill the bus map with invalid values, that helps
  628. * debugging.
  629. */
  630. for (i=0; i<pci_bus_count; i++)
  631. pci_to_OF_bus_map[i] = 0xff;
  632. /* For each hose, we begin searching bridges */
  633. for(hose=hose_head; hose; hose=hose->next) {
  634. struct device_node* node;
  635. node = (struct device_node *)hose->arch_data;
  636. if (!node)
  637. continue;
  638. make_one_node_map(node, hose->first_busno);
  639. }
  640. of_prop_map = get_property(find_path_device("/"), "pci-OF-bus-map", NULL);
  641. if (of_prop_map)
  642. memcpy(of_prop_map, pci_to_OF_bus_map, pci_bus_count);
  643. #ifdef DEBUG
  644. printk("PCI->OF bus map:\n");
  645. for (i=0; i<pci_bus_count; i++) {
  646. if (pci_to_OF_bus_map[i] == 0xff)
  647. continue;
  648. printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
  649. }
  650. #endif
  651. }
  652. typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
  653. static struct device_node* __openfirmware
  654. scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
  655. {
  656. struct device_node* sub_node;
  657. for (; node != 0;node = node->sibling) {
  658. unsigned int *class_code;
  659. if (filter(node, data))
  660. return node;
  661. /* For PCI<->PCI bridges or CardBus bridges, we go down
  662. * Note: some OFs create a parent node "multifunc-device" as
  663. * a fake root for all functions of a multi-function device,
  664. * we go down them as well.
  665. */
  666. class_code = (unsigned int *) get_property(node, "class-code", NULL);
  667. if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  668. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
  669. strcmp(node->name, "multifunc-device"))
  670. continue;
  671. sub_node = scan_OF_pci_childs(node->child, filter, data);
  672. if (sub_node)
  673. return sub_node;
  674. }
  675. return NULL;
  676. }
  677. static int
  678. scan_OF_pci_childs_iterator(struct device_node* node, void* data)
  679. {
  680. unsigned int *reg;
  681. u8* fdata = (u8*)data;
  682. reg = (unsigned int *) get_property(node, "reg", NULL);
  683. if (reg && ((reg[0] >> 8) & 0xff) == fdata[1]
  684. && ((reg[0] >> 16) & 0xff) == fdata[0])
  685. return 1;
  686. return 0;
  687. }
  688. static struct device_node* __openfirmware
  689. scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
  690. {
  691. u8 filter_data[2] = {bus, dev_fn};
  692. return scan_OF_pci_childs(node, scan_OF_pci_childs_iterator, filter_data);
  693. }
  694. /*
  695. * Scans the OF tree for a device node matching a PCI device
  696. */
  697. struct device_node *
  698. pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
  699. {
  700. struct pci_controller *hose;
  701. struct device_node *node;
  702. int busnr;
  703. if (!have_of)
  704. return NULL;
  705. /* Lookup the hose */
  706. busnr = bus->number;
  707. hose = pci_bus_to_hose(busnr);
  708. if (!hose)
  709. return NULL;
  710. /* Check it has an OF node associated */
  711. node = (struct device_node *) hose->arch_data;
  712. if (!node)
  713. return NULL;
  714. /* Fixup bus number according to what OF think it is. */
  715. #ifdef CONFIG_PPC_PMAC
  716. /* The G5 need a special case here. Basically, we don't remap all
  717. * busses on it so we don't create the pci-OF-map. However, we do
  718. * remap the AGP bus and so have to deal with it. A future better
  719. * fix has to be done by making the remapping per-host and always
  720. * filling the pci_to_OF map. --BenH
  721. */
  722. if (_machine == _MACH_Pmac && busnr >= 0xf0)
  723. busnr -= 0xf0;
  724. else
  725. #endif
  726. if (pci_to_OF_bus_map)
  727. busnr = pci_to_OF_bus_map[busnr];
  728. if (busnr == 0xff)
  729. return NULL;
  730. /* Now, lookup childs of the hose */
  731. return scan_OF_childs_for_device(node->child, busnr, devfn);
  732. }
  733. struct device_node*
  734. pci_device_to_OF_node(struct pci_dev *dev)
  735. {
  736. return pci_busdev_to_OF_node(dev->bus, dev->devfn);
  737. }
  738. /* This routine is meant to be used early during boot, when the
  739. * PCI bus numbers have not yet been assigned, and you need to
  740. * issue PCI config cycles to an OF device.
  741. * It could also be used to "fix" RTAS config cycles if you want
  742. * to set pci_assign_all_busses to 1 and still use RTAS for PCI
  743. * config cycles.
  744. */
  745. struct pci_controller*
  746. pci_find_hose_for_OF_device(struct device_node* node)
  747. {
  748. if (!have_of)
  749. return NULL;
  750. while(node) {
  751. struct pci_controller* hose;
  752. for (hose=hose_head;hose;hose=hose->next)
  753. if (hose->arch_data == node)
  754. return hose;
  755. node=node->parent;
  756. }
  757. return NULL;
  758. }
  759. static int __openfirmware
  760. find_OF_pci_device_filter(struct device_node* node, void* data)
  761. {
  762. return ((void *)node == data);
  763. }
  764. /*
  765. * Returns the PCI device matching a given OF node
  766. */
  767. int
  768. pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
  769. {
  770. unsigned int *reg;
  771. struct pci_controller* hose;
  772. struct pci_dev* dev = NULL;
  773. if (!have_of)
  774. return -ENODEV;
  775. /* Make sure it's really a PCI device */
  776. hose = pci_find_hose_for_OF_device(node);
  777. if (!hose || !hose->arch_data)
  778. return -ENODEV;
  779. if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
  780. find_OF_pci_device_filter, (void *)node))
  781. return -ENODEV;
  782. reg = (unsigned int *) get_property(node, "reg", NULL);
  783. if (!reg)
  784. return -ENODEV;
  785. *bus = (reg[0] >> 16) & 0xff;
  786. *devfn = ((reg[0] >> 8) & 0xff);
  787. /* Ok, here we need some tweak. If we have already renumbered
  788. * all busses, we can't rely on the OF bus number any more.
  789. * the pci_to_OF_bus_map is not enough as several PCI busses
  790. * may match the same OF bus number.
  791. */
  792. if (!pci_to_OF_bus_map)
  793. return 0;
  794. while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  795. if (pci_to_OF_bus_map[dev->bus->number] != *bus)
  796. continue;
  797. if (dev->devfn != *devfn)
  798. continue;
  799. *bus = dev->bus->number;
  800. return 0;
  801. }
  802. return -ENODEV;
  803. }
  804. void __init
  805. pci_process_bridge_OF_ranges(struct pci_controller *hose,
  806. struct device_node *dev, int primary)
  807. {
  808. static unsigned int static_lc_ranges[256] __initdata;
  809. unsigned int *dt_ranges, *lc_ranges, *ranges, *prev;
  810. unsigned int size;
  811. int rlen = 0, orig_rlen;
  812. int memno = 0;
  813. struct resource *res;
  814. int np, na = prom_n_addr_cells(dev);
  815. np = na + 5;
  816. /* First we try to merge ranges to fix a problem with some pmacs
  817. * that can have more than 3 ranges, fortunately using contiguous
  818. * addresses -- BenH
  819. */
  820. dt_ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
  821. if (!dt_ranges)
  822. return;
  823. /* Sanity check, though hopefully that never happens */
  824. if (rlen > sizeof(static_lc_ranges)) {
  825. printk(KERN_WARNING "OF ranges property too large !\n");
  826. rlen = sizeof(static_lc_ranges);
  827. }
  828. lc_ranges = static_lc_ranges;
  829. memcpy(lc_ranges, dt_ranges, rlen);
  830. orig_rlen = rlen;
  831. /* Let's work on a copy of the "ranges" property instead of damaging
  832. * the device-tree image in memory
  833. */
  834. ranges = lc_ranges;
  835. prev = NULL;
  836. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  837. if (prev) {
  838. if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
  839. (prev[2] + prev[na+4]) == ranges[2] &&
  840. (prev[na+2] + prev[na+4]) == ranges[na+2]) {
  841. prev[na+4] += ranges[na+4];
  842. ranges[0] = 0;
  843. ranges += np;
  844. continue;
  845. }
  846. }
  847. prev = ranges;
  848. ranges += np;
  849. }
  850. /*
  851. * The ranges property is laid out as an array of elements,
  852. * each of which comprises:
  853. * cells 0 - 2: a PCI address
  854. * cells 3 or 3+4: a CPU physical address
  855. * (size depending on dev->n_addr_cells)
  856. * cells 4+5 or 5+6: the size of the range
  857. */
  858. ranges = lc_ranges;
  859. rlen = orig_rlen;
  860. while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
  861. res = NULL;
  862. size = ranges[na+4];
  863. switch (ranges[0] >> 24) {
  864. case 1: /* I/O space */
  865. if (ranges[2] != 0)
  866. break;
  867. hose->io_base_phys = ranges[na+2];
  868. /* limit I/O space to 16MB */
  869. if (size > 0x01000000)
  870. size = 0x01000000;
  871. hose->io_base_virt = ioremap(ranges[na+2], size);
  872. if (primary)
  873. isa_io_base = (unsigned long) hose->io_base_virt;
  874. res = &hose->io_resource;
  875. res->flags = IORESOURCE_IO;
  876. res->start = ranges[2];
  877. break;
  878. case 2: /* memory space */
  879. memno = 0;
  880. if (ranges[1] == 0 && ranges[2] == 0
  881. && ranges[na+4] <= (16 << 20)) {
  882. /* 1st 16MB, i.e. ISA memory area */
  883. if (primary)
  884. isa_mem_base = ranges[na+2];
  885. memno = 1;
  886. }
  887. while (memno < 3 && hose->mem_resources[memno].flags)
  888. ++memno;
  889. if (memno == 0)
  890. hose->pci_mem_offset = ranges[na+2] - ranges[2];
  891. if (memno < 3) {
  892. res = &hose->mem_resources[memno];
  893. res->flags = IORESOURCE_MEM;
  894. res->start = ranges[na+2];
  895. }
  896. break;
  897. }
  898. if (res != NULL) {
  899. res->name = dev->full_name;
  900. res->end = res->start + size - 1;
  901. res->parent = NULL;
  902. res->sibling = NULL;
  903. res->child = NULL;
  904. }
  905. ranges += np;
  906. }
  907. }
  908. /* We create the "pci-OF-bus-map" property now so it appears in the
  909. * /proc device tree
  910. */
  911. void __init
  912. pci_create_OF_bus_map(void)
  913. {
  914. struct property* of_prop;
  915. of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
  916. if (of_prop && find_path_device("/")) {
  917. memset(of_prop, -1, sizeof(struct property) + 256);
  918. of_prop->name = "pci-OF-bus-map";
  919. of_prop->length = 256;
  920. of_prop->value = (unsigned char *)&of_prop[1];
  921. prom_add_property(find_path_device("/"), of_prop);
  922. }
  923. }
  924. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  925. {
  926. struct pci_dev *pdev;
  927. struct device_node *np;
  928. pdev = to_pci_dev (dev);
  929. np = pci_device_to_OF_node(pdev);
  930. if (np == NULL || np->full_name == NULL)
  931. return 0;
  932. return sprintf(buf, "%s", np->full_name);
  933. }
  934. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  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: %08lx-%08lx\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, size) do { unsigned long __sz = (size) ; \
  1008. res->end = ((res->end + __sz) / (__sz + 1)) * (__sz + 1) + __sz; \
  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_busses)
  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_busses || 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_busses && 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 offset,
  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. int i;
  1417. if (page_is_ram(offset >> PAGE_SHIFT))
  1418. return prot;
  1419. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  1420. for_each_pci_dev(pdev) {
  1421. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1422. struct resource *rp = &pdev->resource[i];
  1423. int flags = rp->flags;
  1424. /* Active and same type? */
  1425. if ((flags & IORESOURCE_MEM) == 0)
  1426. continue;
  1427. /* In the range of this resource? */
  1428. if (offset < (rp->start & PAGE_MASK) ||
  1429. offset > rp->end)
  1430. continue;
  1431. found = rp;
  1432. break;
  1433. }
  1434. if (found)
  1435. break;
  1436. }
  1437. if (found) {
  1438. if (found->flags & IORESOURCE_PREFETCH)
  1439. prot &= ~_PAGE_GUARDED;
  1440. pci_dev_put(pdev);
  1441. }
  1442. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  1443. return __pgprot(prot);
  1444. }
  1445. /*
  1446. * Perform the actual remap of the pages for a PCI device mapping, as
  1447. * appropriate for this architecture. The region in the process to map
  1448. * is described by vm_start and vm_end members of VMA, the base physical
  1449. * address is found in vm_pgoff.
  1450. * The pci device structure is provided so that architectures may make mapping
  1451. * decisions on a per-device or per-bus basis.
  1452. *
  1453. * Returns a negative error code on failure, zero on success.
  1454. */
  1455. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  1456. enum pci_mmap_state mmap_state,
  1457. int write_combine)
  1458. {
  1459. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1460. struct resource *rp;
  1461. int ret;
  1462. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  1463. if (rp == NULL)
  1464. return -EINVAL;
  1465. vma->vm_pgoff = offset >> PAGE_SHIFT;
  1466. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  1467. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  1468. vma->vm_page_prot,
  1469. mmap_state, write_combine);
  1470. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  1471. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  1472. return ret;
  1473. }
  1474. /* Obsolete functions. Should be removed once the symbios driver
  1475. * is fixed
  1476. */
  1477. unsigned long
  1478. phys_to_bus(unsigned long pa)
  1479. {
  1480. struct pci_controller *hose;
  1481. int i;
  1482. for (hose = hose_head; hose; hose = hose->next) {
  1483. for (i = 0; i < 3; ++i) {
  1484. if (pa >= hose->mem_resources[i].start
  1485. && pa <= hose->mem_resources[i].end) {
  1486. /*
  1487. * XXX the hose->pci_mem_offset really
  1488. * only applies to mem_resources[0].
  1489. * We need a way to store an offset for
  1490. * the others. -- paulus
  1491. */
  1492. if (i == 0)
  1493. pa -= hose->pci_mem_offset;
  1494. return pa;
  1495. }
  1496. }
  1497. }
  1498. /* hmmm, didn't find it */
  1499. return 0;
  1500. }
  1501. unsigned long
  1502. pci_phys_to_bus(unsigned long pa, int busnr)
  1503. {
  1504. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1505. if (!hose)
  1506. return pa;
  1507. return pa - hose->pci_mem_offset;
  1508. }
  1509. unsigned long
  1510. pci_bus_to_phys(unsigned int ba, int busnr)
  1511. {
  1512. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1513. if (!hose)
  1514. return ba;
  1515. return ba + hose->pci_mem_offset;
  1516. }
  1517. /* Provide information on locations of various I/O regions in physical
  1518. * memory. Do this on a per-card basis so that we choose the right
  1519. * root bridge.
  1520. * Note that the returned IO or memory base is a physical address
  1521. */
  1522. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  1523. {
  1524. struct pci_controller* hose;
  1525. long result = -EOPNOTSUPP;
  1526. /* Argh ! Please forgive me for that hack, but that's the
  1527. * simplest way to get existing XFree to not lockup on some
  1528. * G5 machines... So when something asks for bus 0 io base
  1529. * (bus 0 is HT root), we return the AGP one instead.
  1530. */
  1531. #ifdef CONFIG_PPC_PMAC
  1532. if (_machine == _MACH_Pmac && machine_is_compatible("MacRISC4"))
  1533. if (bus == 0)
  1534. bus = 0xf0;
  1535. #endif /* CONFIG_PPC_PMAC */
  1536. hose = pci_bus_to_hose(bus);
  1537. if (!hose)
  1538. return -ENODEV;
  1539. switch (which) {
  1540. case IOBASE_BRIDGE_NUMBER:
  1541. return (long)hose->first_busno;
  1542. case IOBASE_MEMORY:
  1543. return (long)hose->pci_mem_offset;
  1544. case IOBASE_IO:
  1545. return (long)hose->io_base_phys;
  1546. case IOBASE_ISA_IO:
  1547. return (long)isa_io_base;
  1548. case IOBASE_ISA_MEM:
  1549. return (long)isa_mem_base;
  1550. }
  1551. return result;
  1552. }
  1553. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1554. const struct resource *rsrc,
  1555. u64 *start, u64 *end)
  1556. {
  1557. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1558. unsigned long offset = 0;
  1559. if (hose == NULL)
  1560. return;
  1561. if (rsrc->flags & IORESOURCE_IO)
  1562. offset = ___IO_BASE - hose->io_base_virt + hose->io_base_phys;
  1563. *start = rsrc->start + offset;
  1564. *end = rsrc->end + offset;
  1565. }
  1566. void __init
  1567. pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
  1568. int flags, char *name)
  1569. {
  1570. res->start = start;
  1571. res->end = end;
  1572. res->flags = flags;
  1573. res->name = name;
  1574. res->parent = NULL;
  1575. res->sibling = NULL;
  1576. res->child = NULL;
  1577. }
  1578. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
  1579. {
  1580. unsigned long start = pci_resource_start(dev, bar);
  1581. unsigned long len = pci_resource_len(dev, bar);
  1582. unsigned long flags = pci_resource_flags(dev, bar);
  1583. if (!len)
  1584. return NULL;
  1585. if (max && len > max)
  1586. len = max;
  1587. if (flags & IORESOURCE_IO)
  1588. return ioport_map(start, len);
  1589. if (flags & IORESOURCE_MEM)
  1590. /* Not checking IORESOURCE_CACHEABLE because PPC does
  1591. * not currently distinguish between ioremap and
  1592. * ioremap_nocache.
  1593. */
  1594. return ioremap(start, len);
  1595. /* What? */
  1596. return NULL;
  1597. }
  1598. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  1599. {
  1600. /* Nothing to do */
  1601. }
  1602. EXPORT_SYMBOL(pci_iomap);
  1603. EXPORT_SYMBOL(pci_iounmap);
  1604. /*
  1605. * Null PCI config access functions, for the case when we can't
  1606. * find a hose.
  1607. */
  1608. #define NULL_PCI_OP(rw, size, type) \
  1609. static int \
  1610. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1611. { \
  1612. return PCIBIOS_DEVICE_NOT_FOUND; \
  1613. }
  1614. static int
  1615. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1616. int len, u32 *val)
  1617. {
  1618. return PCIBIOS_DEVICE_NOT_FOUND;
  1619. }
  1620. static int
  1621. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1622. int len, u32 val)
  1623. {
  1624. return PCIBIOS_DEVICE_NOT_FOUND;
  1625. }
  1626. static struct pci_ops null_pci_ops =
  1627. {
  1628. null_read_config,
  1629. null_write_config
  1630. };
  1631. /*
  1632. * These functions are used early on before PCI scanning is done
  1633. * and all of the pci_dev and pci_bus structures have been created.
  1634. */
  1635. static struct pci_bus *
  1636. fake_pci_bus(struct pci_controller *hose, int busnr)
  1637. {
  1638. static struct pci_bus bus;
  1639. if (hose == 0) {
  1640. hose = pci_bus_to_hose(busnr);
  1641. if (hose == 0)
  1642. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1643. }
  1644. bus.number = busnr;
  1645. bus.sysdata = hose;
  1646. bus.ops = hose? hose->ops: &null_pci_ops;
  1647. return &bus;
  1648. }
  1649. #define EARLY_PCI_OP(rw, size, type) \
  1650. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1651. int devfn, int offset, type value) \
  1652. { \
  1653. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1654. devfn, offset, value); \
  1655. }
  1656. EARLY_PCI_OP(read, byte, u8 *)
  1657. EARLY_PCI_OP(read, word, u16 *)
  1658. EARLY_PCI_OP(read, dword, u32 *)
  1659. EARLY_PCI_OP(write, byte, u8)
  1660. EARLY_PCI_OP(write, word, u16)
  1661. EARLY_PCI_OP(write, dword, u32)