pci_32.c 40 KB

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