pci.c 50 KB

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