pci_32.c 51 KB

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