pci_32.c 52 KB

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