pci_32.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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. LIST_HEAD(hose_list);
  50. static int pci_bus_count;
  51. static void
  52. fixup_hide_host_resource_fsl(struct pci_dev* dev)
  53. {
  54. int i, class = dev->class >> 8;
  55. if ((class == PCI_CLASS_PROCESSOR_POWERPC) &&
  56. (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
  57. (dev->bus->parent == NULL)) {
  58. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  59. dev->resource[i].start = 0;
  60. dev->resource[i].end = 0;
  61. dev->resource[i].flags = 0;
  62. }
  63. }
  64. }
  65. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
  66. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
  67. static void
  68. fixup_broken_pcnet32(struct pci_dev* dev)
  69. {
  70. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  71. dev->vendor = PCI_VENDOR_ID_AMD;
  72. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  73. }
  74. }
  75. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  76. static void
  77. fixup_cpc710_pci64(struct pci_dev* dev)
  78. {
  79. /* Hide the PCI64 BARs from the kernel as their content doesn't
  80. * fit well in the resource management
  81. */
  82. dev->resource[0].start = dev->resource[0].end = 0;
  83. dev->resource[0].flags = 0;
  84. dev->resource[1].start = dev->resource[1].end = 0;
  85. dev->resource[1].flags = 0;
  86. }
  87. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
  88. static void
  89. pcibios_fixup_resources(struct pci_dev *dev)
  90. {
  91. struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
  92. int i;
  93. unsigned long offset;
  94. if (!hose) {
  95. printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
  96. return;
  97. }
  98. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  99. struct resource *res = dev->resource + i;
  100. if (!res->flags)
  101. continue;
  102. if (res->end == 0xffffffff) {
  103. DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n",
  104. pci_name(dev), i, (u64)res->start, (u64)res->end);
  105. res->end -= res->start;
  106. res->start = 0;
  107. res->flags |= IORESOURCE_UNSET;
  108. continue;
  109. }
  110. offset = 0;
  111. if (res->flags & IORESOURCE_MEM) {
  112. offset = hose->pci_mem_offset;
  113. } else if (res->flags & IORESOURCE_IO) {
  114. offset = (unsigned long) hose->io_base_virt
  115. - isa_io_base;
  116. }
  117. if (offset != 0) {
  118. res->start += offset;
  119. res->end += offset;
  120. DBG("Fixup res %d (%lx) of dev %s: %llx -> %llx\n",
  121. i, res->flags, pci_name(dev),
  122. (u64)res->start - offset, (u64)res->start);
  123. }
  124. }
  125. /* Call machine specific resource fixup */
  126. if (ppc_md.pcibios_fixup_resources)
  127. ppc_md.pcibios_fixup_resources(dev);
  128. }
  129. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  130. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  131. struct resource *res)
  132. {
  133. unsigned long offset = 0;
  134. struct pci_controller *hose = dev->sysdata;
  135. if (hose && res->flags & IORESOURCE_IO)
  136. offset = (unsigned long)hose->io_base_virt - isa_io_base;
  137. else if (hose && res->flags & IORESOURCE_MEM)
  138. offset = hose->pci_mem_offset;
  139. region->start = res->start - offset;
  140. region->end = res->end - offset;
  141. }
  142. EXPORT_SYMBOL(pcibios_resource_to_bus);
  143. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  144. struct pci_bus_region *region)
  145. {
  146. unsigned long offset = 0;
  147. struct pci_controller *hose = dev->sysdata;
  148. if (hose && res->flags & IORESOURCE_IO)
  149. offset = (unsigned long)hose->io_base_virt - isa_io_base;
  150. else if (hose && res->flags & IORESOURCE_MEM)
  151. offset = hose->pci_mem_offset;
  152. res->start = region->start + offset;
  153. res->end = region->end + offset;
  154. }
  155. EXPORT_SYMBOL(pcibios_bus_to_resource);
  156. /*
  157. * We need to avoid collisions with `mirrored' VGA ports
  158. * and other strange ISA hardware, so we always want the
  159. * addresses to be allocated in the 0x000-0x0ff region
  160. * modulo 0x400.
  161. *
  162. * Why? Because some silly external IO cards only decode
  163. * the low 10 bits of the IO address. The 0x00-0xff region
  164. * is reserved for motherboard devices that decode all 16
  165. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  166. * but we want to try to avoid allocating at 0x2900-0x2bff
  167. * which might have be mirrored at 0x0100-0x03ff..
  168. */
  169. void pcibios_align_resource(void *data, struct resource *res,
  170. resource_size_t size, resource_size_t align)
  171. {
  172. struct pci_dev *dev = data;
  173. if (res->flags & IORESOURCE_IO) {
  174. resource_size_t start = res->start;
  175. if (size > 0x100) {
  176. printk(KERN_ERR "PCI: I/O Region %s/%d too large"
  177. " (%lld bytes)\n", pci_name(dev),
  178. dev->resource - res, (unsigned long long)size);
  179. }
  180. if (start & 0x300) {
  181. start = (start + 0x3ff) & ~0x3ff;
  182. res->start = start;
  183. }
  184. }
  185. }
  186. EXPORT_SYMBOL(pcibios_align_resource);
  187. /*
  188. * Handle resources of PCI devices. If the world were perfect, we could
  189. * just allocate all the resource regions and do nothing more. It isn't.
  190. * On the other hand, we cannot just re-allocate all devices, as it would
  191. * require us to know lots of host bridge internals. So we attempt to
  192. * keep as much of the original configuration as possible, but tweak it
  193. * when it's found to be wrong.
  194. *
  195. * Known BIOS problems we have to work around:
  196. * - I/O or memory regions not configured
  197. * - regions configured, but not enabled in the command register
  198. * - bogus I/O addresses above 64K used
  199. * - expansion ROMs left enabled (this may sound harmless, but given
  200. * the fact the PCI specs explicitly allow address decoders to be
  201. * shared between expansion ROMs and other resource regions, it's
  202. * at least dangerous)
  203. *
  204. * Our solution:
  205. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  206. * This gives us fixed barriers on where we can allocate.
  207. * (2) Allocate resources for all enabled devices. If there is
  208. * a collision, just mark the resource as unallocated. Also
  209. * disable expansion ROMs during this step.
  210. * (3) Try to allocate resources for disabled devices. If the
  211. * resources were assigned correctly, everything goes well,
  212. * if they weren't, they won't disturb allocation of other
  213. * resources.
  214. * (4) Assign new addresses to resources which were either
  215. * not configured at all or misconfigured. If explicitly
  216. * requested by the user, configure expansion ROM address
  217. * as well.
  218. */
  219. static void __init
  220. pcibios_allocate_bus_resources(struct list_head *bus_list)
  221. {
  222. struct pci_bus *bus;
  223. int i;
  224. struct resource *res, *pr;
  225. /* Depth-First Search on bus tree */
  226. list_for_each_entry(bus, bus_list, node) {
  227. for (i = 0; i < 4; ++i) {
  228. if ((res = bus->resource[i]) == NULL || !res->flags
  229. || res->start > res->end)
  230. continue;
  231. if (bus->parent == NULL)
  232. pr = (res->flags & IORESOURCE_IO)?
  233. &ioport_resource: &iomem_resource;
  234. else {
  235. pr = pci_find_parent_resource(bus->self, res);
  236. if (pr == res) {
  237. /* this happens when the generic PCI
  238. * code (wrongly) decides that this
  239. * bridge is transparent -- paulus
  240. */
  241. continue;
  242. }
  243. }
  244. DBG("PCI: bridge rsrc %llx..%llx (%lx), parent %p\n",
  245. (u64)res->start, (u64)res->end, res->flags, pr);
  246. if (pr) {
  247. if (request_resource(pr, res) == 0)
  248. continue;
  249. /*
  250. * Must be a conflict with an existing entry.
  251. * Move that entry (or entries) under the
  252. * bridge resource and try again.
  253. */
  254. if (reparent_resources(pr, res) == 0)
  255. continue;
  256. }
  257. printk(KERN_ERR "PCI: Cannot allocate resource region "
  258. "%d of PCI bridge %d\n", i, bus->number);
  259. if (pci_relocate_bridge_resource(bus, i))
  260. bus->resource[i] = NULL;
  261. }
  262. pcibios_allocate_bus_resources(&bus->children);
  263. }
  264. }
  265. /*
  266. * Reparent resource children of pr that conflict with res
  267. * under res, and make res replace those children.
  268. */
  269. static int __init
  270. reparent_resources(struct resource *parent, struct resource *res)
  271. {
  272. struct resource *p, **pp;
  273. struct resource **firstpp = NULL;
  274. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  275. if (p->end < res->start)
  276. continue;
  277. if (res->end < p->start)
  278. break;
  279. if (p->start < res->start || p->end > res->end)
  280. return -1; /* not completely contained */
  281. if (firstpp == NULL)
  282. firstpp = pp;
  283. }
  284. if (firstpp == NULL)
  285. return -1; /* didn't find any conflicting entries? */
  286. res->parent = parent;
  287. res->child = *firstpp;
  288. res->sibling = *pp;
  289. *firstpp = res;
  290. *pp = NULL;
  291. for (p = res->child; p != NULL; p = p->sibling) {
  292. p->parent = res;
  293. DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
  294. p->name, (u64)p->start, (u64)p->end, res->name);
  295. }
  296. return 0;
  297. }
  298. /*
  299. * A bridge has been allocated a range which is outside the range
  300. * of its parent bridge, so it needs to be moved.
  301. */
  302. static int __init
  303. pci_relocate_bridge_resource(struct pci_bus *bus, int i)
  304. {
  305. struct resource *res, *pr, *conflict;
  306. unsigned long try, size;
  307. int j;
  308. struct pci_bus *parent = bus->parent;
  309. if (parent == NULL) {
  310. /* shouldn't ever happen */
  311. printk(KERN_ERR "PCI: can't move host bridge resource\n");
  312. return -1;
  313. }
  314. res = bus->resource[i];
  315. if (res == NULL)
  316. return -1;
  317. pr = NULL;
  318. for (j = 0; j < 4; j++) {
  319. struct resource *r = parent->resource[j];
  320. if (!r)
  321. continue;
  322. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  323. continue;
  324. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
  325. pr = r;
  326. break;
  327. }
  328. if (res->flags & IORESOURCE_PREFETCH)
  329. pr = r;
  330. }
  331. if (pr == NULL)
  332. return -1;
  333. size = res->end - res->start;
  334. if (pr->start > pr->end || size > pr->end - pr->start)
  335. return -1;
  336. try = pr->end;
  337. for (;;) {
  338. res->start = try - size;
  339. res->end = try;
  340. if (probe_resource(bus->parent, pr, res, &conflict) == 0)
  341. break;
  342. if (conflict->start <= pr->start + size)
  343. return -1;
  344. try = conflict->start - 1;
  345. }
  346. if (request_resource(pr, res)) {
  347. DBG(KERN_ERR "PCI: huh? couldn't move to %llx..%llx\n",
  348. (u64)res->start, (u64)res->end);
  349. return -1; /* "can't happen" */
  350. }
  351. update_bridge_base(bus, i);
  352. printk(KERN_INFO "PCI: bridge %d resource %d moved to %llx..%llx\n",
  353. bus->number, i, (unsigned long long)res->start,
  354. (unsigned long long)res->end);
  355. return 0;
  356. }
  357. static int __init
  358. probe_resource(struct pci_bus *parent, struct resource *pr,
  359. struct resource *res, struct resource **conflict)
  360. {
  361. struct pci_bus *bus;
  362. struct pci_dev *dev;
  363. struct resource *r;
  364. int i;
  365. for (r = pr->child; r != NULL; r = r->sibling) {
  366. if (r->end >= res->start && res->end >= r->start) {
  367. *conflict = r;
  368. return 1;
  369. }
  370. }
  371. list_for_each_entry(bus, &parent->children, node) {
  372. for (i = 0; i < 4; ++i) {
  373. if ((r = bus->resource[i]) == NULL)
  374. continue;
  375. if (!r->flags || r->start > r->end || r == res)
  376. continue;
  377. if (pci_find_parent_resource(bus->self, r) != pr)
  378. continue;
  379. if (r->end >= res->start && res->end >= r->start) {
  380. *conflict = r;
  381. return 1;
  382. }
  383. }
  384. }
  385. list_for_each_entry(dev, &parent->devices, bus_list) {
  386. for (i = 0; i < 6; ++i) {
  387. r = &dev->resource[i];
  388. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  389. continue;
  390. if (pci_find_parent_resource(dev, r) != pr)
  391. continue;
  392. if (r->end >= res->start && res->end >= r->start) {
  393. *conflict = r;
  394. return 1;
  395. }
  396. }
  397. }
  398. return 0;
  399. }
  400. void __init
  401. update_bridge_resource(struct pci_dev *dev, struct resource *res)
  402. {
  403. u8 io_base_lo, io_limit_lo;
  404. u16 mem_base, mem_limit;
  405. u16 cmd;
  406. unsigned long start, end, off;
  407. struct pci_controller *hose = dev->sysdata;
  408. if (!hose) {
  409. printk("update_bridge_base: no hose?\n");
  410. return;
  411. }
  412. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  413. pci_write_config_word(dev, PCI_COMMAND,
  414. cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
  415. if (res->flags & IORESOURCE_IO) {
  416. off = (unsigned long) hose->io_base_virt - isa_io_base;
  417. start = res->start - off;
  418. end = res->end - off;
  419. io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
  420. io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
  421. if (end > 0xffff)
  422. io_base_lo |= PCI_IO_RANGE_TYPE_32;
  423. else
  424. io_base_lo |= PCI_IO_RANGE_TYPE_16;
  425. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  426. start >> 16);
  427. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  428. end >> 16);
  429. pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
  430. pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
  431. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  432. == IORESOURCE_MEM) {
  433. off = hose->pci_mem_offset;
  434. mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  435. mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  436. pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
  437. pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
  438. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  439. == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
  440. off = hose->pci_mem_offset;
  441. mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
  442. mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
  443. pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
  444. pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
  445. } else {
  446. DBG(KERN_ERR "PCI: ugh, bridge %s res has flags=%lx\n",
  447. pci_name(dev), res->flags);
  448. }
  449. pci_write_config_word(dev, PCI_COMMAND, cmd);
  450. }
  451. static void __init
  452. update_bridge_base(struct pci_bus *bus, int i)
  453. {
  454. struct resource *res = bus->resource[i];
  455. struct pci_dev *dev = bus->self;
  456. update_bridge_resource(dev, res);
  457. }
  458. static inline void alloc_resource(struct pci_dev *dev, int idx)
  459. {
  460. struct resource *pr, *r = &dev->resource[idx];
  461. DBG("PCI:%s: Resource %d: %016llx-%016llx (f=%lx)\n",
  462. pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags);
  463. pr = pci_find_parent_resource(dev, r);
  464. if (!pr || request_resource(pr, r) < 0) {
  465. printk(KERN_ERR "PCI: Cannot allocate resource region %d"
  466. " of device %s\n", idx, pci_name(dev));
  467. if (pr)
  468. DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n",
  469. pr, (u64)pr->start, (u64)pr->end, pr->flags);
  470. /* We'll assign a new address later */
  471. r->flags |= IORESOURCE_UNSET;
  472. r->end -= r->start;
  473. r->start = 0;
  474. }
  475. }
  476. static void __init
  477. pcibios_allocate_resources(int pass)
  478. {
  479. struct pci_dev *dev = NULL;
  480. int idx, disabled;
  481. u16 command;
  482. struct resource *r;
  483. for_each_pci_dev(dev) {
  484. pci_read_config_word(dev, PCI_COMMAND, &command);
  485. for (idx = 0; idx < 6; idx++) {
  486. r = &dev->resource[idx];
  487. if (r->parent) /* Already allocated */
  488. continue;
  489. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  490. continue; /* Not assigned at all */
  491. if (r->flags & IORESOURCE_IO)
  492. disabled = !(command & PCI_COMMAND_IO);
  493. else
  494. disabled = !(command & PCI_COMMAND_MEMORY);
  495. if (pass == disabled)
  496. alloc_resource(dev, idx);
  497. }
  498. if (pass)
  499. continue;
  500. r = &dev->resource[PCI_ROM_RESOURCE];
  501. if (r->flags & IORESOURCE_ROM_ENABLE) {
  502. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  503. u32 reg;
  504. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  505. r->flags &= ~IORESOURCE_ROM_ENABLE;
  506. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  507. pci_write_config_dword(dev, dev->rom_base_reg,
  508. reg & ~PCI_ROM_ADDRESS_ENABLE);
  509. }
  510. }
  511. }
  512. static void __init
  513. pcibios_assign_resources(void)
  514. {
  515. struct pci_dev *dev = NULL;
  516. int idx;
  517. struct resource *r;
  518. for_each_pci_dev(dev) {
  519. int class = dev->class >> 8;
  520. /* Don't touch classless devices and host bridges */
  521. if (!class || class == PCI_CLASS_BRIDGE_HOST)
  522. continue;
  523. for (idx = 0; idx < 6; idx++) {
  524. r = &dev->resource[idx];
  525. /*
  526. * We shall assign a new address to this resource,
  527. * either because the BIOS (sic) forgot to do so
  528. * or because we have decided the old address was
  529. * unusable for some reason.
  530. */
  531. if ((r->flags & IORESOURCE_UNSET) && r->end &&
  532. (!ppc_md.pcibios_enable_device_hook ||
  533. !ppc_md.pcibios_enable_device_hook(dev, 1))) {
  534. int rc;
  535. r->flags &= ~IORESOURCE_UNSET;
  536. rc = pci_assign_resource(dev, idx);
  537. BUG_ON(rc);
  538. }
  539. }
  540. #if 0 /* don't assign ROMs */
  541. r = &dev->resource[PCI_ROM_RESOURCE];
  542. r->end -= r->start;
  543. r->start = 0;
  544. if (r->end)
  545. pci_assign_resource(dev, PCI_ROM_RESOURCE);
  546. #endif
  547. }
  548. }
  549. #ifdef CONFIG_PPC_OF
  550. /*
  551. * Functions below are used on OpenFirmware machines.
  552. */
  553. static void
  554. make_one_node_map(struct device_node* node, u8 pci_bus)
  555. {
  556. const int *bus_range;
  557. int len;
  558. if (pci_bus >= pci_bus_count)
  559. return;
  560. bus_range = of_get_property(node, "bus-range", &len);
  561. if (bus_range == NULL || len < 2 * sizeof(int)) {
  562. printk(KERN_WARNING "Can't get bus-range for %s, "
  563. "assuming it starts at 0\n", node->full_name);
  564. pci_to_OF_bus_map[pci_bus] = 0;
  565. } else
  566. pci_to_OF_bus_map[pci_bus] = bus_range[0];
  567. for (node=node->child; node != 0;node = node->sibling) {
  568. struct pci_dev* dev;
  569. const unsigned int *class_code, *reg;
  570. class_code = of_get_property(node, "class-code", NULL);
  571. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  572. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  573. continue;
  574. reg = of_get_property(node, "reg", NULL);
  575. if (!reg)
  576. continue;
  577. dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
  578. if (!dev || !dev->subordinate) {
  579. pci_dev_put(dev);
  580. continue;
  581. }
  582. make_one_node_map(node, dev->subordinate->number);
  583. pci_dev_put(dev);
  584. }
  585. }
  586. void
  587. pcibios_make_OF_bus_map(void)
  588. {
  589. int i;
  590. struct pci_controller *hose, *tmp;
  591. struct property *map_prop;
  592. struct device_node *dn;
  593. pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
  594. if (!pci_to_OF_bus_map) {
  595. printk(KERN_ERR "Can't allocate OF bus map !\n");
  596. return;
  597. }
  598. /* We fill the bus map with invalid values, that helps
  599. * debugging.
  600. */
  601. for (i=0; i<pci_bus_count; i++)
  602. pci_to_OF_bus_map[i] = 0xff;
  603. /* For each hose, we begin searching bridges */
  604. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  605. struct device_node* node;
  606. node = (struct device_node *)hose->arch_data;
  607. if (!node)
  608. continue;
  609. make_one_node_map(node, hose->first_busno);
  610. }
  611. dn = of_find_node_by_path("/");
  612. map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
  613. if (map_prop) {
  614. BUG_ON(pci_bus_count > map_prop->length);
  615. memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
  616. }
  617. of_node_put(dn);
  618. #ifdef DEBUG
  619. printk("PCI->OF bus map:\n");
  620. for (i=0; i<pci_bus_count; i++) {
  621. if (pci_to_OF_bus_map[i] == 0xff)
  622. continue;
  623. printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
  624. }
  625. #endif
  626. }
  627. typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
  628. static struct device_node*
  629. scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
  630. {
  631. struct device_node* sub_node;
  632. for (; node != 0;node = node->sibling) {
  633. const unsigned int *class_code;
  634. if (filter(node, data))
  635. return node;
  636. /* For PCI<->PCI bridges or CardBus bridges, we go down
  637. * Note: some OFs create a parent node "multifunc-device" as
  638. * a fake root for all functions of a multi-function device,
  639. * we go down them as well.
  640. */
  641. class_code = of_get_property(node, "class-code", NULL);
  642. if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  643. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
  644. strcmp(node->name, "multifunc-device"))
  645. continue;
  646. sub_node = scan_OF_pci_childs(node->child, filter, data);
  647. if (sub_node)
  648. return sub_node;
  649. }
  650. return NULL;
  651. }
  652. static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
  653. unsigned int devfn)
  654. {
  655. struct device_node *np = NULL;
  656. const u32 *reg;
  657. unsigned int psize;
  658. while ((np = of_get_next_child(parent, np)) != NULL) {
  659. reg = of_get_property(np, "reg", &psize);
  660. if (reg == NULL || psize < 4)
  661. continue;
  662. if (((reg[0] >> 8) & 0xff) == devfn)
  663. return np;
  664. }
  665. return NULL;
  666. }
  667. static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
  668. {
  669. struct device_node *parent, *np;
  670. /* Are we a root bus ? */
  671. if (bus->self == NULL || bus->parent == NULL) {
  672. struct pci_controller *hose = pci_bus_to_host(bus);
  673. if (hose == NULL)
  674. return NULL;
  675. return of_node_get(hose->arch_data);
  676. }
  677. /* not a root bus, we need to get our parent */
  678. parent = scan_OF_for_pci_bus(bus->parent);
  679. if (parent == NULL)
  680. return NULL;
  681. /* now iterate for children for a match */
  682. np = scan_OF_for_pci_dev(parent, bus->self->devfn);
  683. of_node_put(parent);
  684. return np;
  685. }
  686. /*
  687. * Scans the OF tree for a device node matching a PCI device
  688. */
  689. struct device_node *
  690. pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
  691. {
  692. struct device_node *parent, *np;
  693. if (!have_of)
  694. return NULL;
  695. DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
  696. parent = scan_OF_for_pci_bus(bus);
  697. if (parent == NULL)
  698. return NULL;
  699. DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
  700. np = scan_OF_for_pci_dev(parent, devfn);
  701. of_node_put(parent);
  702. DBG(" result is %s\n", np ? np->full_name : "<NULL>");
  703. /* XXX most callers don't release the returned node
  704. * mostly because ppc64 doesn't increase the refcount,
  705. * we need to fix that.
  706. */
  707. return np;
  708. }
  709. EXPORT_SYMBOL(pci_busdev_to_OF_node);
  710. struct device_node*
  711. pci_device_to_OF_node(struct pci_dev *dev)
  712. {
  713. return pci_busdev_to_OF_node(dev->bus, dev->devfn);
  714. }
  715. EXPORT_SYMBOL(pci_device_to_OF_node);
  716. static int
  717. find_OF_pci_device_filter(struct device_node* node, void* data)
  718. {
  719. return ((void *)node == data);
  720. }
  721. /*
  722. * Returns the PCI device matching a given OF node
  723. */
  724. int
  725. pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
  726. {
  727. const unsigned int *reg;
  728. struct pci_controller* hose;
  729. struct pci_dev* dev = NULL;
  730. if (!have_of)
  731. return -ENODEV;
  732. /* Make sure it's really a PCI device */
  733. hose = pci_find_hose_for_OF_device(node);
  734. if (!hose || !hose->arch_data)
  735. return -ENODEV;
  736. if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
  737. find_OF_pci_device_filter, (void *)node))
  738. return -ENODEV;
  739. reg = of_get_property(node, "reg", NULL);
  740. if (!reg)
  741. return -ENODEV;
  742. *bus = (reg[0] >> 16) & 0xff;
  743. *devfn = ((reg[0] >> 8) & 0xff);
  744. /* Ok, here we need some tweak. If we have already renumbered
  745. * all busses, we can't rely on the OF bus number any more.
  746. * the pci_to_OF_bus_map is not enough as several PCI busses
  747. * may match the same OF bus number.
  748. */
  749. if (!pci_to_OF_bus_map)
  750. return 0;
  751. for_each_pci_dev(dev)
  752. if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
  753. dev->devfn == *devfn) {
  754. *bus = dev->bus->number;
  755. pci_dev_put(dev);
  756. return 0;
  757. }
  758. return -ENODEV;
  759. }
  760. EXPORT_SYMBOL(pci_device_from_OF_node);
  761. void __init
  762. pci_process_bridge_OF_ranges(struct pci_controller *hose,
  763. struct device_node *dev, int primary)
  764. {
  765. static unsigned int static_lc_ranges[256] __initdata;
  766. const unsigned int *dt_ranges;
  767. unsigned int *lc_ranges, *ranges, *prev, size;
  768. int rlen = 0, orig_rlen;
  769. int memno = 0;
  770. struct resource *res;
  771. int np, na = of_n_addr_cells(dev);
  772. np = na + 5;
  773. /* First we try to merge ranges to fix a problem with some pmacs
  774. * that can have more than 3 ranges, fortunately using contiguous
  775. * addresses -- BenH
  776. */
  777. dt_ranges = of_get_property(dev, "ranges", &rlen);
  778. if (!dt_ranges)
  779. return;
  780. /* Sanity check, though hopefully that never happens */
  781. if (rlen > sizeof(static_lc_ranges)) {
  782. printk(KERN_WARNING "OF ranges property too large !\n");
  783. rlen = sizeof(static_lc_ranges);
  784. }
  785. lc_ranges = static_lc_ranges;
  786. memcpy(lc_ranges, dt_ranges, rlen);
  787. orig_rlen = rlen;
  788. /* Let's work on a copy of the "ranges" property instead of damaging
  789. * the device-tree image in memory
  790. */
  791. ranges = lc_ranges;
  792. prev = NULL;
  793. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  794. if (prev) {
  795. if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
  796. (prev[2] + prev[na+4]) == ranges[2] &&
  797. (prev[na+2] + prev[na+4]) == ranges[na+2]) {
  798. prev[na+4] += ranges[na+4];
  799. ranges[0] = 0;
  800. ranges += np;
  801. continue;
  802. }
  803. }
  804. prev = ranges;
  805. ranges += np;
  806. }
  807. /*
  808. * The ranges property is laid out as an array of elements,
  809. * each of which comprises:
  810. * cells 0 - 2: a PCI address
  811. * cells 3 or 3+4: a CPU physical address
  812. * (size depending on dev->n_addr_cells)
  813. * cells 4+5 or 5+6: the size of the range
  814. */
  815. ranges = lc_ranges;
  816. rlen = orig_rlen;
  817. while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
  818. res = NULL;
  819. size = ranges[na+4];
  820. switch ((ranges[0] >> 24) & 0x3) {
  821. case 1: /* I/O space */
  822. if (ranges[2] != 0)
  823. break;
  824. hose->io_base_phys = ranges[na+2];
  825. /* limit I/O space to 16MB */
  826. if (size > 0x01000000)
  827. size = 0x01000000;
  828. hose->io_base_virt = ioremap(ranges[na+2], size);
  829. if (primary)
  830. isa_io_base = (unsigned long) hose->io_base_virt;
  831. res = &hose->io_resource;
  832. res->flags = IORESOURCE_IO;
  833. res->start = ranges[2];
  834. DBG("PCI: IO 0x%llx -> 0x%llx\n",
  835. (u64)res->start, (u64)res->start + size - 1);
  836. break;
  837. case 2: /* memory space */
  838. memno = 0;
  839. if (ranges[1] == 0 && ranges[2] == 0
  840. && ranges[na+4] <= (16 << 20)) {
  841. /* 1st 16MB, i.e. ISA memory area */
  842. if (primary)
  843. isa_mem_base = ranges[na+2];
  844. memno = 1;
  845. }
  846. while (memno < 3 && hose->mem_resources[memno].flags)
  847. ++memno;
  848. if (memno == 0)
  849. hose->pci_mem_offset = ranges[na+2] - ranges[2];
  850. if (memno < 3) {
  851. res = &hose->mem_resources[memno];
  852. res->flags = IORESOURCE_MEM;
  853. if(ranges[0] & 0x40000000)
  854. res->flags |= IORESOURCE_PREFETCH;
  855. res->start = ranges[na+2];
  856. DBG("PCI: MEM[%d] 0x%llx -> 0x%llx\n", memno,
  857. (u64)res->start, (u64)res->start + size - 1);
  858. }
  859. break;
  860. }
  861. if (res != NULL) {
  862. res->name = dev->full_name;
  863. res->end = res->start + size - 1;
  864. res->parent = NULL;
  865. res->sibling = NULL;
  866. res->child = NULL;
  867. }
  868. ranges += np;
  869. }
  870. }
  871. /* We create the "pci-OF-bus-map" property now so it appears in the
  872. * /proc device tree
  873. */
  874. void __init
  875. pci_create_OF_bus_map(void)
  876. {
  877. struct property* of_prop;
  878. struct device_node *dn;
  879. of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
  880. if (!of_prop)
  881. return;
  882. dn = of_find_node_by_path("/");
  883. if (dn) {
  884. memset(of_prop, -1, sizeof(struct property) + 256);
  885. of_prop->name = "pci-OF-bus-map";
  886. of_prop->length = 256;
  887. of_prop->value = &of_prop[1];
  888. prom_add_property(dn, of_prop);
  889. of_node_put(dn);
  890. }
  891. }
  892. #else /* CONFIG_PPC_OF */
  893. void pcibios_make_OF_bus_map(void)
  894. {
  895. }
  896. #endif /* CONFIG_PPC_OF */
  897. #ifdef CONFIG_PPC_PMAC
  898. /*
  899. * This set of routines checks for PCI<->PCI bridges that have closed
  900. * IO resources and have child devices. It tries to re-open an IO
  901. * window on them.
  902. *
  903. * This is a _temporary_ fix to workaround a problem with Apple's OF
  904. * closing IO windows on P2P bridges when the OF drivers of cards
  905. * below this bridge don't claim any IO range (typically ATI or
  906. * Adaptec).
  907. *
  908. * A more complete fix would be to use drivers/pci/setup-bus.c, which
  909. * involves a working pcibios_fixup_pbus_ranges(), some more care about
  910. * ordering when creating the host bus resources, and maybe a few more
  911. * minor tweaks
  912. */
  913. /* Initialize bridges with base/limit values we have collected */
  914. static void __init
  915. do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
  916. {
  917. struct pci_dev *bridge = bus->self;
  918. struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
  919. u32 l;
  920. u16 w;
  921. struct resource res;
  922. if (bus->resource[0] == NULL)
  923. return;
  924. res = *(bus->resource[0]);
  925. DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
  926. res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
  927. res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
  928. DBG(" IO window: %016llx-%016llx\n", res.start, res.end);
  929. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  930. pci_read_config_dword(bridge, PCI_IO_BASE, &l);
  931. l &= 0xffff000f;
  932. l |= (res.start >> 8) & 0x00f0;
  933. l |= res.end & 0xf000;
  934. pci_write_config_dword(bridge, PCI_IO_BASE, l);
  935. if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  936. l = (res.start >> 16) | (res.end & 0xffff0000);
  937. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
  938. }
  939. pci_read_config_word(bridge, PCI_COMMAND, &w);
  940. w |= PCI_COMMAND_IO;
  941. pci_write_config_word(bridge, PCI_COMMAND, w);
  942. #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
  943. if (enable_vga) {
  944. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
  945. w |= PCI_BRIDGE_CTL_VGA;
  946. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
  947. }
  948. #endif
  949. }
  950. /* This function is pretty basic and actually quite broken for the
  951. * general case, it's enough for us right now though. It's supposed
  952. * to tell us if we need to open an IO range at all or not and what
  953. * size.
  954. */
  955. static int __init
  956. check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
  957. {
  958. struct pci_dev *dev;
  959. int i;
  960. int rc = 0;
  961. #define push_end(res, mask) do { \
  962. BUG_ON((mask+1) & mask); \
  963. res->end = (res->end + mask) | mask; \
  964. } while (0)
  965. list_for_each_entry(dev, &bus->devices, bus_list) {
  966. u16 class = dev->class >> 8;
  967. if (class == PCI_CLASS_DISPLAY_VGA ||
  968. class == PCI_CLASS_NOT_DEFINED_VGA)
  969. *found_vga = 1;
  970. if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
  971. rc |= check_for_io_childs(dev->subordinate, res, found_vga);
  972. if (class == PCI_CLASS_BRIDGE_CARDBUS)
  973. push_end(res, 0xfff);
  974. for (i=0; i<PCI_NUM_RESOURCES; i++) {
  975. struct resource *r;
  976. unsigned long r_size;
  977. if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
  978. && i >= PCI_BRIDGE_RESOURCES)
  979. continue;
  980. r = &dev->resource[i];
  981. r_size = r->end - r->start;
  982. if (r_size < 0xfff)
  983. r_size = 0xfff;
  984. if (r->flags & IORESOURCE_IO && (r_size) != 0) {
  985. rc = 1;
  986. push_end(res, r_size);
  987. }
  988. }
  989. }
  990. return rc;
  991. }
  992. /* Here we scan all P2P bridges of a given level that have a closed
  993. * IO window. Note that the test for the presence of a VGA card should
  994. * be improved to take into account already configured P2P bridges,
  995. * currently, we don't see them and might end up configuring 2 bridges
  996. * with VGA pass through enabled
  997. */
  998. static void __init
  999. do_fixup_p2p_level(struct pci_bus *bus)
  1000. {
  1001. struct pci_bus *b;
  1002. int i, parent_io;
  1003. int has_vga = 0;
  1004. for (parent_io=0; parent_io<4; parent_io++)
  1005. if (bus->resource[parent_io]
  1006. && bus->resource[parent_io]->flags & IORESOURCE_IO)
  1007. break;
  1008. if (parent_io >= 4)
  1009. return;
  1010. list_for_each_entry(b, &bus->children, node) {
  1011. struct pci_dev *d = b->self;
  1012. struct pci_controller* hose = (struct pci_controller *)d->sysdata;
  1013. struct resource *res = b->resource[0];
  1014. struct resource tmp_res;
  1015. unsigned long max;
  1016. int found_vga = 0;
  1017. memset(&tmp_res, 0, sizeof(tmp_res));
  1018. tmp_res.start = bus->resource[parent_io]->start;
  1019. /* We don't let low addresses go through that closed P2P bridge, well,
  1020. * that may not be necessary but I feel safer that way
  1021. */
  1022. if (tmp_res.start == 0)
  1023. tmp_res.start = 0x1000;
  1024. if (!list_empty(&b->devices) && res && res->flags == 0 &&
  1025. res != bus->resource[parent_io] &&
  1026. (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
  1027. check_for_io_childs(b, &tmp_res, &found_vga)) {
  1028. u8 io_base_lo;
  1029. printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
  1030. if (found_vga) {
  1031. if (has_vga) {
  1032. printk(KERN_WARNING "Skipping VGA, already active"
  1033. " on bus segment\n");
  1034. found_vga = 0;
  1035. } else
  1036. has_vga = 1;
  1037. }
  1038. pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
  1039. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
  1040. max = ((unsigned long) hose->io_base_virt
  1041. - isa_io_base) + 0xffffffff;
  1042. else
  1043. max = ((unsigned long) hose->io_base_virt
  1044. - isa_io_base) + 0xffff;
  1045. *res = tmp_res;
  1046. res->flags = IORESOURCE_IO;
  1047. res->name = b->name;
  1048. /* Find a resource in the parent where we can allocate */
  1049. for (i = 0 ; i < 4; i++) {
  1050. struct resource *r = bus->resource[i];
  1051. if (!r)
  1052. continue;
  1053. if ((r->flags & IORESOURCE_IO) == 0)
  1054. continue;
  1055. DBG("Trying to allocate from %016llx, size %016llx from parent"
  1056. " res %d: %016llx -> %016llx\n",
  1057. res->start, res->end, i, r->start, r->end);
  1058. if (allocate_resource(r, res, res->end + 1, res->start, max,
  1059. res->end + 1, NULL, NULL) < 0) {
  1060. DBG("Failed !\n");
  1061. continue;
  1062. }
  1063. do_update_p2p_io_resource(b, found_vga);
  1064. break;
  1065. }
  1066. }
  1067. do_fixup_p2p_level(b);
  1068. }
  1069. }
  1070. static void
  1071. pcibios_fixup_p2p_bridges(void)
  1072. {
  1073. struct pci_bus *b;
  1074. list_for_each_entry(b, &pci_root_buses, node)
  1075. do_fixup_p2p_level(b);
  1076. }
  1077. #endif /* CONFIG_PPC_PMAC */
  1078. static int __init
  1079. pcibios_init(void)
  1080. {
  1081. struct pci_controller *hose, *tmp;
  1082. struct pci_bus *bus;
  1083. int next_busno = 0;
  1084. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  1085. /* Scan all of the recorded PCI controllers. */
  1086. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1087. if (pci_assign_all_buses)
  1088. hose->first_busno = next_busno;
  1089. hose->last_busno = 0xff;
  1090. bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
  1091. hose->ops, hose);
  1092. if (bus)
  1093. pci_bus_add_devices(bus);
  1094. hose->last_busno = bus->subordinate;
  1095. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  1096. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  1097. }
  1098. pci_bus_count = next_busno;
  1099. /* OpenFirmware based machines need a map of OF bus
  1100. * numbers vs. kernel bus numbers since we may have to
  1101. * remap them.
  1102. */
  1103. if (pci_assign_all_buses && have_of)
  1104. pcibios_make_OF_bus_map();
  1105. /* Call machine dependent fixup */
  1106. if (ppc_md.pcibios_fixup)
  1107. ppc_md.pcibios_fixup();
  1108. /* Allocate and assign resources */
  1109. pcibios_allocate_bus_resources(&pci_root_buses);
  1110. pcibios_allocate_resources(0);
  1111. pcibios_allocate_resources(1);
  1112. #ifdef CONFIG_PPC_PMAC
  1113. pcibios_fixup_p2p_bridges();
  1114. #endif /* CONFIG_PPC_PMAC */
  1115. pcibios_assign_resources();
  1116. /* Call machine dependent post-init code */
  1117. if (ppc_md.pcibios_after_init)
  1118. ppc_md.pcibios_after_init();
  1119. return 0;
  1120. }
  1121. subsys_initcall(pcibios_init);
  1122. void pcibios_fixup_bus(struct pci_bus *bus)
  1123. {
  1124. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  1125. unsigned long io_offset;
  1126. struct resource *res;
  1127. struct pci_dev *dev;
  1128. int i;
  1129. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  1130. if (bus->parent == NULL) {
  1131. /* This is a host bridge - fill in its resources */
  1132. hose->bus = bus;
  1133. bus->resource[0] = res = &hose->io_resource;
  1134. if (!res->flags) {
  1135. if (io_offset)
  1136. printk(KERN_ERR "I/O resource not set for host"
  1137. " bridge %d\n", hose->global_number);
  1138. res->start = 0;
  1139. res->end = IO_SPACE_LIMIT;
  1140. res->flags = IORESOURCE_IO;
  1141. }
  1142. res->start += io_offset;
  1143. res->end += io_offset;
  1144. for (i = 0; i < 3; ++i) {
  1145. res = &hose->mem_resources[i];
  1146. if (!res->flags) {
  1147. if (i > 0)
  1148. continue;
  1149. printk(KERN_ERR "Memory resource not set for "
  1150. "host bridge %d\n", hose->global_number);
  1151. res->start = hose->pci_mem_offset;
  1152. res->end = ~0U;
  1153. res->flags = IORESOURCE_MEM;
  1154. }
  1155. bus->resource[i+1] = res;
  1156. }
  1157. } else {
  1158. /* This is a subordinate bridge */
  1159. pci_read_bridge_bases(bus);
  1160. for (i = 0; i < 4; ++i) {
  1161. if ((res = bus->resource[i]) == NULL)
  1162. continue;
  1163. if (!res->flags || bus->self->transparent)
  1164. continue;
  1165. if (io_offset && (res->flags & IORESOURCE_IO)) {
  1166. res->start += io_offset;
  1167. res->end += io_offset;
  1168. } else if (hose->pci_mem_offset
  1169. && (res->flags & IORESOURCE_MEM)) {
  1170. res->start += hose->pci_mem_offset;
  1171. res->end += hose->pci_mem_offset;
  1172. }
  1173. }
  1174. }
  1175. /* Platform specific bus fixups */
  1176. if (ppc_md.pcibios_fixup_bus)
  1177. ppc_md.pcibios_fixup_bus(bus);
  1178. /* Read default IRQs and fixup if necessary */
  1179. list_for_each_entry(dev, &bus->devices, bus_list) {
  1180. pci_read_irq_line(dev);
  1181. if (ppc_md.pci_irq_fixup)
  1182. ppc_md.pci_irq_fixup(dev);
  1183. }
  1184. }
  1185. /* the next one is stolen from the alpha port... */
  1186. void __init
  1187. pcibios_update_irq(struct pci_dev *dev, int irq)
  1188. {
  1189. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  1190. /* XXX FIXME - update OF device tree node interrupt property */
  1191. }
  1192. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1193. {
  1194. u16 cmd, old_cmd;
  1195. int idx;
  1196. struct resource *r;
  1197. if (ppc_md.pcibios_enable_device_hook)
  1198. if (ppc_md.pcibios_enable_device_hook(dev, 0))
  1199. return -EINVAL;
  1200. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1201. old_cmd = cmd;
  1202. for (idx=0; idx<6; idx++) {
  1203. r = &dev->resource[idx];
  1204. if (r->flags & IORESOURCE_UNSET) {
  1205. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  1206. return -EINVAL;
  1207. }
  1208. if (r->flags & IORESOURCE_IO)
  1209. cmd |= PCI_COMMAND_IO;
  1210. if (r->flags & IORESOURCE_MEM)
  1211. cmd |= PCI_COMMAND_MEMORY;
  1212. }
  1213. if (cmd != old_cmd) {
  1214. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  1215. pci_name(dev), old_cmd, cmd);
  1216. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1217. }
  1218. return 0;
  1219. }
  1220. static struct pci_controller*
  1221. pci_bus_to_hose(int bus)
  1222. {
  1223. struct pci_controller *hose, *tmp;
  1224. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1225. if (bus >= hose->first_busno && bus <= hose->last_busno)
  1226. return hose;
  1227. return NULL;
  1228. }
  1229. /* Provide information on locations of various I/O regions in physical
  1230. * memory. Do this on a per-card basis so that we choose the right
  1231. * root bridge.
  1232. * Note that the returned IO or memory base is a physical address
  1233. */
  1234. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  1235. {
  1236. struct pci_controller* hose;
  1237. long result = -EOPNOTSUPP;
  1238. /* Argh ! Please forgive me for that hack, but that's the
  1239. * simplest way to get existing XFree to not lockup on some
  1240. * G5 machines... So when something asks for bus 0 io base
  1241. * (bus 0 is HT root), we return the AGP one instead.
  1242. */
  1243. #ifdef CONFIG_PPC_PMAC
  1244. if (machine_is(powermac) && machine_is_compatible("MacRISC4"))
  1245. if (bus == 0)
  1246. bus = 0xf0;
  1247. #endif /* CONFIG_PPC_PMAC */
  1248. hose = pci_bus_to_hose(bus);
  1249. if (!hose)
  1250. return -ENODEV;
  1251. switch (which) {
  1252. case IOBASE_BRIDGE_NUMBER:
  1253. return (long)hose->first_busno;
  1254. case IOBASE_MEMORY:
  1255. return (long)hose->pci_mem_offset;
  1256. case IOBASE_IO:
  1257. return (long)hose->io_base_phys;
  1258. case IOBASE_ISA_IO:
  1259. return (long)isa_io_base;
  1260. case IOBASE_ISA_MEM:
  1261. return (long)isa_mem_base;
  1262. }
  1263. return result;
  1264. }
  1265. unsigned long pci_address_to_pio(phys_addr_t address)
  1266. {
  1267. struct pci_controller *hose, *tmp;
  1268. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1269. unsigned int size = hose->io_resource.end -
  1270. hose->io_resource.start + 1;
  1271. if (address >= hose->io_base_phys &&
  1272. address < (hose->io_base_phys + size)) {
  1273. unsigned long base =
  1274. (unsigned long)hose->io_base_virt - _IO_BASE;
  1275. return base + (address - hose->io_base_phys);
  1276. }
  1277. }
  1278. return (unsigned int)-1;
  1279. }
  1280. EXPORT_SYMBOL(pci_address_to_pio);
  1281. /*
  1282. * Null PCI config access functions, for the case when we can't
  1283. * find a hose.
  1284. */
  1285. #define NULL_PCI_OP(rw, size, type) \
  1286. static int \
  1287. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1288. { \
  1289. return PCIBIOS_DEVICE_NOT_FOUND; \
  1290. }
  1291. static int
  1292. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1293. int len, u32 *val)
  1294. {
  1295. return PCIBIOS_DEVICE_NOT_FOUND;
  1296. }
  1297. static int
  1298. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1299. int len, u32 val)
  1300. {
  1301. return PCIBIOS_DEVICE_NOT_FOUND;
  1302. }
  1303. static struct pci_ops null_pci_ops =
  1304. {
  1305. .read = null_read_config,
  1306. .write = null_write_config,
  1307. };
  1308. /*
  1309. * These functions are used early on before PCI scanning is done
  1310. * and all of the pci_dev and pci_bus structures have been created.
  1311. */
  1312. static struct pci_bus *
  1313. fake_pci_bus(struct pci_controller *hose, int busnr)
  1314. {
  1315. static struct pci_bus bus;
  1316. if (hose == 0) {
  1317. hose = pci_bus_to_hose(busnr);
  1318. if (hose == 0)
  1319. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1320. }
  1321. bus.number = busnr;
  1322. bus.sysdata = hose;
  1323. bus.ops = hose? hose->ops: &null_pci_ops;
  1324. return &bus;
  1325. }
  1326. #define EARLY_PCI_OP(rw, size, type) \
  1327. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1328. int devfn, int offset, type value) \
  1329. { \
  1330. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1331. devfn, offset, value); \
  1332. }
  1333. EARLY_PCI_OP(read, byte, u8 *)
  1334. EARLY_PCI_OP(read, word, u16 *)
  1335. EARLY_PCI_OP(read, dword, u32 *)
  1336. EARLY_PCI_OP(write, byte, u8)
  1337. EARLY_PCI_OP(write, word, u16)
  1338. EARLY_PCI_OP(write, dword, u32)
  1339. extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
  1340. int early_find_capability(struct pci_controller *hose, int bus, int devfn,
  1341. int cap)
  1342. {
  1343. return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
  1344. }