pci_32.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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. static void __init
  385. update_bridge_base(struct pci_bus *bus, int i)
  386. {
  387. struct resource *res = bus->resource[i];
  388. u8 io_base_lo, io_limit_lo;
  389. u16 mem_base, mem_limit;
  390. u16 cmd;
  391. unsigned long start, end, off;
  392. struct pci_dev *dev = bus->self;
  393. struct pci_controller *hose = dev->sysdata;
  394. if (!hose) {
  395. printk("update_bridge_base: no hose?\n");
  396. return;
  397. }
  398. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  399. pci_write_config_word(dev, PCI_COMMAND,
  400. cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
  401. if (res->flags & IORESOURCE_IO) {
  402. off = (unsigned long) hose->io_base_virt - isa_io_base;
  403. start = res->start - off;
  404. end = res->end - off;
  405. io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
  406. io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
  407. if (end > 0xffff)
  408. io_base_lo |= PCI_IO_RANGE_TYPE_32;
  409. else
  410. io_base_lo |= PCI_IO_RANGE_TYPE_16;
  411. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  412. start >> 16);
  413. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  414. end >> 16);
  415. pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
  416. pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
  417. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  418. == IORESOURCE_MEM) {
  419. off = hose->pci_mem_offset;
  420. mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  421. mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  422. pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
  423. pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
  424. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  425. == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
  426. off = hose->pci_mem_offset;
  427. mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
  428. mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
  429. pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
  430. pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
  431. } else {
  432. DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
  433. pci_name(dev), i, res->flags);
  434. }
  435. pci_write_config_word(dev, PCI_COMMAND, cmd);
  436. }
  437. static inline void alloc_resource(struct pci_dev *dev, int idx)
  438. {
  439. struct resource *pr, *r = &dev->resource[idx];
  440. DBG("PCI:%s: Resource %d: %016llx-%016llx (f=%lx)\n",
  441. pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags);
  442. pr = pci_find_parent_resource(dev, r);
  443. if (!pr || request_resource(pr, r) < 0) {
  444. printk(KERN_ERR "PCI: Cannot allocate resource region %d"
  445. " of device %s\n", idx, pci_name(dev));
  446. if (pr)
  447. DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n",
  448. pr, (u64)pr->start, (u64)pr->end, pr->flags);
  449. /* We'll assign a new address later */
  450. r->flags |= IORESOURCE_UNSET;
  451. r->end -= r->start;
  452. r->start = 0;
  453. }
  454. }
  455. static void __init
  456. pcibios_allocate_resources(int pass)
  457. {
  458. struct pci_dev *dev = NULL;
  459. int idx, disabled;
  460. u16 command;
  461. struct resource *r;
  462. for_each_pci_dev(dev) {
  463. pci_read_config_word(dev, PCI_COMMAND, &command);
  464. for (idx = 0; idx < 6; idx++) {
  465. r = &dev->resource[idx];
  466. if (r->parent) /* Already allocated */
  467. continue;
  468. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  469. continue; /* Not assigned at all */
  470. if (r->flags & IORESOURCE_IO)
  471. disabled = !(command & PCI_COMMAND_IO);
  472. else
  473. disabled = !(command & PCI_COMMAND_MEMORY);
  474. if (pass == disabled)
  475. alloc_resource(dev, idx);
  476. }
  477. if (pass)
  478. continue;
  479. r = &dev->resource[PCI_ROM_RESOURCE];
  480. if (r->flags & IORESOURCE_ROM_ENABLE) {
  481. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  482. u32 reg;
  483. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  484. r->flags &= ~IORESOURCE_ROM_ENABLE;
  485. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  486. pci_write_config_dword(dev, dev->rom_base_reg,
  487. reg & ~PCI_ROM_ADDRESS_ENABLE);
  488. }
  489. }
  490. }
  491. static void __init
  492. pcibios_assign_resources(void)
  493. {
  494. struct pci_dev *dev = NULL;
  495. int idx;
  496. struct resource *r;
  497. for_each_pci_dev(dev) {
  498. int class = dev->class >> 8;
  499. /* Don't touch classless devices and host bridges */
  500. if (!class || class == PCI_CLASS_BRIDGE_HOST)
  501. continue;
  502. for (idx = 0; idx < 6; idx++) {
  503. r = &dev->resource[idx];
  504. /*
  505. * We shall assign a new address to this resource,
  506. * either because the BIOS (sic) forgot to do so
  507. * or because we have decided the old address was
  508. * unusable for some reason.
  509. */
  510. if ((r->flags & IORESOURCE_UNSET) && r->end &&
  511. (!ppc_md.pcibios_enable_device_hook ||
  512. !ppc_md.pcibios_enable_device_hook(dev, 1))) {
  513. r->flags &= ~IORESOURCE_UNSET;
  514. pci_assign_resource(dev, idx);
  515. }
  516. }
  517. #if 0 /* don't assign ROMs */
  518. r = &dev->resource[PCI_ROM_RESOURCE];
  519. r->end -= r->start;
  520. r->start = 0;
  521. if (r->end)
  522. pci_assign_resource(dev, PCI_ROM_RESOURCE);
  523. #endif
  524. }
  525. }
  526. #ifdef CONFIG_PPC_OF
  527. /*
  528. * Functions below are used on OpenFirmware machines.
  529. */
  530. static void
  531. make_one_node_map(struct device_node* node, u8 pci_bus)
  532. {
  533. const int *bus_range;
  534. int len;
  535. if (pci_bus >= pci_bus_count)
  536. return;
  537. bus_range = of_get_property(node, "bus-range", &len);
  538. if (bus_range == NULL || len < 2 * sizeof(int)) {
  539. printk(KERN_WARNING "Can't get bus-range for %s, "
  540. "assuming it starts at 0\n", node->full_name);
  541. pci_to_OF_bus_map[pci_bus] = 0;
  542. } else
  543. pci_to_OF_bus_map[pci_bus] = bus_range[0];
  544. for (node=node->child; node != 0;node = node->sibling) {
  545. struct pci_dev* dev;
  546. const unsigned int *class_code, *reg;
  547. class_code = of_get_property(node, "class-code", NULL);
  548. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  549. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  550. continue;
  551. reg = of_get_property(node, "reg", NULL);
  552. if (!reg)
  553. continue;
  554. dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
  555. if (!dev || !dev->subordinate) {
  556. pci_dev_put(dev);
  557. continue;
  558. }
  559. make_one_node_map(node, dev->subordinate->number);
  560. pci_dev_put(dev);
  561. }
  562. }
  563. void
  564. pcibios_make_OF_bus_map(void)
  565. {
  566. int i;
  567. struct pci_controller *hose, *tmp;
  568. struct property *map_prop;
  569. struct device_node *dn;
  570. pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
  571. if (!pci_to_OF_bus_map) {
  572. printk(KERN_ERR "Can't allocate OF bus map !\n");
  573. return;
  574. }
  575. /* We fill the bus map with invalid values, that helps
  576. * debugging.
  577. */
  578. for (i=0; i<pci_bus_count; i++)
  579. pci_to_OF_bus_map[i] = 0xff;
  580. /* For each hose, we begin searching bridges */
  581. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  582. struct device_node* node;
  583. node = (struct device_node *)hose->arch_data;
  584. if (!node)
  585. continue;
  586. make_one_node_map(node, hose->first_busno);
  587. }
  588. dn = of_find_node_by_path("/");
  589. map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
  590. if (map_prop) {
  591. BUG_ON(pci_bus_count > map_prop->length);
  592. memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
  593. }
  594. of_node_put(dn);
  595. #ifdef DEBUG
  596. printk("PCI->OF bus map:\n");
  597. for (i=0; i<pci_bus_count; i++) {
  598. if (pci_to_OF_bus_map[i] == 0xff)
  599. continue;
  600. printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
  601. }
  602. #endif
  603. }
  604. typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
  605. static struct device_node*
  606. scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
  607. {
  608. struct device_node* sub_node;
  609. for (; node != 0;node = node->sibling) {
  610. const unsigned int *class_code;
  611. if (filter(node, data))
  612. return node;
  613. /* For PCI<->PCI bridges or CardBus bridges, we go down
  614. * Note: some OFs create a parent node "multifunc-device" as
  615. * a fake root for all functions of a multi-function device,
  616. * we go down them as well.
  617. */
  618. class_code = of_get_property(node, "class-code", NULL);
  619. if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  620. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
  621. strcmp(node->name, "multifunc-device"))
  622. continue;
  623. sub_node = scan_OF_pci_childs(node->child, filter, data);
  624. if (sub_node)
  625. return sub_node;
  626. }
  627. return NULL;
  628. }
  629. static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
  630. unsigned int devfn)
  631. {
  632. struct device_node *np = NULL;
  633. const u32 *reg;
  634. unsigned int psize;
  635. while ((np = of_get_next_child(parent, np)) != NULL) {
  636. reg = of_get_property(np, "reg", &psize);
  637. if (reg == NULL || psize < 4)
  638. continue;
  639. if (((reg[0] >> 8) & 0xff) == devfn)
  640. return np;
  641. }
  642. return NULL;
  643. }
  644. static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
  645. {
  646. struct device_node *parent, *np;
  647. /* Are we a root bus ? */
  648. if (bus->self == NULL || bus->parent == NULL) {
  649. struct pci_controller *hose = pci_bus_to_host(bus);
  650. if (hose == NULL)
  651. return NULL;
  652. return of_node_get(hose->arch_data);
  653. }
  654. /* not a root bus, we need to get our parent */
  655. parent = scan_OF_for_pci_bus(bus->parent);
  656. if (parent == NULL)
  657. return NULL;
  658. /* now iterate for children for a match */
  659. np = scan_OF_for_pci_dev(parent, bus->self->devfn);
  660. of_node_put(parent);
  661. return np;
  662. }
  663. /*
  664. * Scans the OF tree for a device node matching a PCI device
  665. */
  666. struct device_node *
  667. pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
  668. {
  669. struct device_node *parent, *np;
  670. if (!have_of)
  671. return NULL;
  672. DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
  673. parent = scan_OF_for_pci_bus(bus);
  674. if (parent == NULL)
  675. return NULL;
  676. DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
  677. np = scan_OF_for_pci_dev(parent, devfn);
  678. of_node_put(parent);
  679. DBG(" result is %s\n", np ? np->full_name : "<NULL>");
  680. /* XXX most callers don't release the returned node
  681. * mostly because ppc64 doesn't increase the refcount,
  682. * we need to fix that.
  683. */
  684. return np;
  685. }
  686. EXPORT_SYMBOL(pci_busdev_to_OF_node);
  687. struct device_node*
  688. pci_device_to_OF_node(struct pci_dev *dev)
  689. {
  690. return pci_busdev_to_OF_node(dev->bus, dev->devfn);
  691. }
  692. EXPORT_SYMBOL(pci_device_to_OF_node);
  693. static int
  694. find_OF_pci_device_filter(struct device_node* node, void* data)
  695. {
  696. return ((void *)node == data);
  697. }
  698. /*
  699. * Returns the PCI device matching a given OF node
  700. */
  701. int
  702. pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
  703. {
  704. const unsigned int *reg;
  705. struct pci_controller* hose;
  706. struct pci_dev* dev = NULL;
  707. if (!have_of)
  708. return -ENODEV;
  709. /* Make sure it's really a PCI device */
  710. hose = pci_find_hose_for_OF_device(node);
  711. if (!hose || !hose->arch_data)
  712. return -ENODEV;
  713. if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
  714. find_OF_pci_device_filter, (void *)node))
  715. return -ENODEV;
  716. reg = of_get_property(node, "reg", NULL);
  717. if (!reg)
  718. return -ENODEV;
  719. *bus = (reg[0] >> 16) & 0xff;
  720. *devfn = ((reg[0] >> 8) & 0xff);
  721. /* Ok, here we need some tweak. If we have already renumbered
  722. * all busses, we can't rely on the OF bus number any more.
  723. * the pci_to_OF_bus_map is not enough as several PCI busses
  724. * may match the same OF bus number.
  725. */
  726. if (!pci_to_OF_bus_map)
  727. return 0;
  728. for_each_pci_dev(dev)
  729. if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
  730. dev->devfn == *devfn) {
  731. *bus = dev->bus->number;
  732. pci_dev_put(dev);
  733. return 0;
  734. }
  735. return -ENODEV;
  736. }
  737. EXPORT_SYMBOL(pci_device_from_OF_node);
  738. void __init
  739. pci_process_bridge_OF_ranges(struct pci_controller *hose,
  740. struct device_node *dev, int primary)
  741. {
  742. static unsigned int static_lc_ranges[256] __initdata;
  743. const unsigned int *dt_ranges;
  744. unsigned int *lc_ranges, *ranges, *prev, size;
  745. int rlen = 0, orig_rlen;
  746. int memno = 0;
  747. struct resource *res;
  748. int np, na = of_n_addr_cells(dev);
  749. np = na + 5;
  750. /* First we try to merge ranges to fix a problem with some pmacs
  751. * that can have more than 3 ranges, fortunately using contiguous
  752. * addresses -- BenH
  753. */
  754. dt_ranges = of_get_property(dev, "ranges", &rlen);
  755. if (!dt_ranges)
  756. return;
  757. /* Sanity check, though hopefully that never happens */
  758. if (rlen > sizeof(static_lc_ranges)) {
  759. printk(KERN_WARNING "OF ranges property too large !\n");
  760. rlen = sizeof(static_lc_ranges);
  761. }
  762. lc_ranges = static_lc_ranges;
  763. memcpy(lc_ranges, dt_ranges, rlen);
  764. orig_rlen = rlen;
  765. /* Let's work on a copy of the "ranges" property instead of damaging
  766. * the device-tree image in memory
  767. */
  768. ranges = lc_ranges;
  769. prev = NULL;
  770. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  771. if (prev) {
  772. if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
  773. (prev[2] + prev[na+4]) == ranges[2] &&
  774. (prev[na+2] + prev[na+4]) == ranges[na+2]) {
  775. prev[na+4] += ranges[na+4];
  776. ranges[0] = 0;
  777. ranges += np;
  778. continue;
  779. }
  780. }
  781. prev = ranges;
  782. ranges += np;
  783. }
  784. /*
  785. * The ranges property is laid out as an array of elements,
  786. * each of which comprises:
  787. * cells 0 - 2: a PCI address
  788. * cells 3 or 3+4: a CPU physical address
  789. * (size depending on dev->n_addr_cells)
  790. * cells 4+5 or 5+6: the size of the range
  791. */
  792. ranges = lc_ranges;
  793. rlen = orig_rlen;
  794. while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
  795. res = NULL;
  796. size = ranges[na+4];
  797. switch ((ranges[0] >> 24) & 0x3) {
  798. case 1: /* I/O space */
  799. if (ranges[2] != 0)
  800. break;
  801. hose->io_base_phys = ranges[na+2];
  802. /* limit I/O space to 16MB */
  803. if (size > 0x01000000)
  804. size = 0x01000000;
  805. hose->io_base_virt = ioremap(ranges[na+2], size);
  806. if (primary)
  807. isa_io_base = (unsigned long) hose->io_base_virt;
  808. res = &hose->io_resource;
  809. res->flags = IORESOURCE_IO;
  810. res->start = ranges[2];
  811. DBG("PCI: IO 0x%llx -> 0x%llx\n",
  812. (u64)res->start, (u64)res->start + size - 1);
  813. break;
  814. case 2: /* memory space */
  815. memno = 0;
  816. if (ranges[1] == 0 && ranges[2] == 0
  817. && ranges[na+4] <= (16 << 20)) {
  818. /* 1st 16MB, i.e. ISA memory area */
  819. if (primary)
  820. isa_mem_base = ranges[na+2];
  821. memno = 1;
  822. }
  823. while (memno < 3 && hose->mem_resources[memno].flags)
  824. ++memno;
  825. if (memno == 0)
  826. hose->pci_mem_offset = ranges[na+2] - ranges[2];
  827. if (memno < 3) {
  828. res = &hose->mem_resources[memno];
  829. res->flags = IORESOURCE_MEM;
  830. if(ranges[0] & 0x40000000)
  831. res->flags |= IORESOURCE_PREFETCH;
  832. res->start = ranges[na+2];
  833. DBG("PCI: MEM[%d] 0x%llx -> 0x%llx\n", memno,
  834. (u64)res->start, (u64)res->start + size - 1);
  835. }
  836. break;
  837. }
  838. if (res != NULL) {
  839. res->name = dev->full_name;
  840. res->end = res->start + size - 1;
  841. res->parent = NULL;
  842. res->sibling = NULL;
  843. res->child = NULL;
  844. }
  845. ranges += np;
  846. }
  847. }
  848. /* We create the "pci-OF-bus-map" property now so it appears in the
  849. * /proc device tree
  850. */
  851. void __init
  852. pci_create_OF_bus_map(void)
  853. {
  854. struct property* of_prop;
  855. struct device_node *dn;
  856. of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
  857. if (!of_prop)
  858. return;
  859. dn = of_find_node_by_path("/");
  860. if (dn) {
  861. memset(of_prop, -1, sizeof(struct property) + 256);
  862. of_prop->name = "pci-OF-bus-map";
  863. of_prop->length = 256;
  864. of_prop->value = &of_prop[1];
  865. prom_add_property(dn, of_prop);
  866. of_node_put(dn);
  867. }
  868. }
  869. #else /* CONFIG_PPC_OF */
  870. void pcibios_make_OF_bus_map(void)
  871. {
  872. }
  873. #endif /* CONFIG_PPC_OF */
  874. #ifdef CONFIG_PPC_PMAC
  875. /*
  876. * This set of routines checks for PCI<->PCI bridges that have closed
  877. * IO resources and have child devices. It tries to re-open an IO
  878. * window on them.
  879. *
  880. * This is a _temporary_ fix to workaround a problem with Apple's OF
  881. * closing IO windows on P2P bridges when the OF drivers of cards
  882. * below this bridge don't claim any IO range (typically ATI or
  883. * Adaptec).
  884. *
  885. * A more complete fix would be to use drivers/pci/setup-bus.c, which
  886. * involves a working pcibios_fixup_pbus_ranges(), some more care about
  887. * ordering when creating the host bus resources, and maybe a few more
  888. * minor tweaks
  889. */
  890. /* Initialize bridges with base/limit values we have collected */
  891. static void __init
  892. do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
  893. {
  894. struct pci_dev *bridge = bus->self;
  895. struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
  896. u32 l;
  897. u16 w;
  898. struct resource res;
  899. if (bus->resource[0] == NULL)
  900. return;
  901. res = *(bus->resource[0]);
  902. DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
  903. res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
  904. res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
  905. DBG(" IO window: %016llx-%016llx\n", res.start, res.end);
  906. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  907. pci_read_config_dword(bridge, PCI_IO_BASE, &l);
  908. l &= 0xffff000f;
  909. l |= (res.start >> 8) & 0x00f0;
  910. l |= res.end & 0xf000;
  911. pci_write_config_dword(bridge, PCI_IO_BASE, l);
  912. if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  913. l = (res.start >> 16) | (res.end & 0xffff0000);
  914. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
  915. }
  916. pci_read_config_word(bridge, PCI_COMMAND, &w);
  917. w |= PCI_COMMAND_IO;
  918. pci_write_config_word(bridge, PCI_COMMAND, w);
  919. #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
  920. if (enable_vga) {
  921. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
  922. w |= PCI_BRIDGE_CTL_VGA;
  923. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
  924. }
  925. #endif
  926. }
  927. /* This function is pretty basic and actually quite broken for the
  928. * general case, it's enough for us right now though. It's supposed
  929. * to tell us if we need to open an IO range at all or not and what
  930. * size.
  931. */
  932. static int __init
  933. check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
  934. {
  935. struct pci_dev *dev;
  936. int i;
  937. int rc = 0;
  938. #define push_end(res, mask) do { \
  939. BUG_ON((mask+1) & mask); \
  940. res->end = (res->end + mask) | mask; \
  941. } while (0)
  942. list_for_each_entry(dev, &bus->devices, bus_list) {
  943. u16 class = dev->class >> 8;
  944. if (class == PCI_CLASS_DISPLAY_VGA ||
  945. class == PCI_CLASS_NOT_DEFINED_VGA)
  946. *found_vga = 1;
  947. if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
  948. rc |= check_for_io_childs(dev->subordinate, res, found_vga);
  949. if (class == PCI_CLASS_BRIDGE_CARDBUS)
  950. push_end(res, 0xfff);
  951. for (i=0; i<PCI_NUM_RESOURCES; i++) {
  952. struct resource *r;
  953. unsigned long r_size;
  954. if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
  955. && i >= PCI_BRIDGE_RESOURCES)
  956. continue;
  957. r = &dev->resource[i];
  958. r_size = r->end - r->start;
  959. if (r_size < 0xfff)
  960. r_size = 0xfff;
  961. if (r->flags & IORESOURCE_IO && (r_size) != 0) {
  962. rc = 1;
  963. push_end(res, r_size);
  964. }
  965. }
  966. }
  967. return rc;
  968. }
  969. /* Here we scan all P2P bridges of a given level that have a closed
  970. * IO window. Note that the test for the presence of a VGA card should
  971. * be improved to take into account already configured P2P bridges,
  972. * currently, we don't see them and might end up configuring 2 bridges
  973. * with VGA pass through enabled
  974. */
  975. static void __init
  976. do_fixup_p2p_level(struct pci_bus *bus)
  977. {
  978. struct pci_bus *b;
  979. int i, parent_io;
  980. int has_vga = 0;
  981. for (parent_io=0; parent_io<4; parent_io++)
  982. if (bus->resource[parent_io]
  983. && bus->resource[parent_io]->flags & IORESOURCE_IO)
  984. break;
  985. if (parent_io >= 4)
  986. return;
  987. list_for_each_entry(b, &bus->children, node) {
  988. struct pci_dev *d = b->self;
  989. struct pci_controller* hose = (struct pci_controller *)d->sysdata;
  990. struct resource *res = b->resource[0];
  991. struct resource tmp_res;
  992. unsigned long max;
  993. int found_vga = 0;
  994. memset(&tmp_res, 0, sizeof(tmp_res));
  995. tmp_res.start = bus->resource[parent_io]->start;
  996. /* We don't let low addresses go through that closed P2P bridge, well,
  997. * that may not be necessary but I feel safer that way
  998. */
  999. if (tmp_res.start == 0)
  1000. tmp_res.start = 0x1000;
  1001. if (!list_empty(&b->devices) && res && res->flags == 0 &&
  1002. res != bus->resource[parent_io] &&
  1003. (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
  1004. check_for_io_childs(b, &tmp_res, &found_vga)) {
  1005. u8 io_base_lo;
  1006. printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
  1007. if (found_vga) {
  1008. if (has_vga) {
  1009. printk(KERN_WARNING "Skipping VGA, already active"
  1010. " on bus segment\n");
  1011. found_vga = 0;
  1012. } else
  1013. has_vga = 1;
  1014. }
  1015. pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
  1016. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
  1017. max = ((unsigned long) hose->io_base_virt
  1018. - isa_io_base) + 0xffffffff;
  1019. else
  1020. max = ((unsigned long) hose->io_base_virt
  1021. - isa_io_base) + 0xffff;
  1022. *res = tmp_res;
  1023. res->flags = IORESOURCE_IO;
  1024. res->name = b->name;
  1025. /* Find a resource in the parent where we can allocate */
  1026. for (i = 0 ; i < 4; i++) {
  1027. struct resource *r = bus->resource[i];
  1028. if (!r)
  1029. continue;
  1030. if ((r->flags & IORESOURCE_IO) == 0)
  1031. continue;
  1032. DBG("Trying to allocate from %016llx, size %016llx from parent"
  1033. " res %d: %016llx -> %016llx\n",
  1034. res->start, res->end, i, r->start, r->end);
  1035. if (allocate_resource(r, res, res->end + 1, res->start, max,
  1036. res->end + 1, NULL, NULL) < 0) {
  1037. DBG("Failed !\n");
  1038. continue;
  1039. }
  1040. do_update_p2p_io_resource(b, found_vga);
  1041. break;
  1042. }
  1043. }
  1044. do_fixup_p2p_level(b);
  1045. }
  1046. }
  1047. static void
  1048. pcibios_fixup_p2p_bridges(void)
  1049. {
  1050. struct pci_bus *b;
  1051. list_for_each_entry(b, &pci_root_buses, node)
  1052. do_fixup_p2p_level(b);
  1053. }
  1054. #endif /* CONFIG_PPC_PMAC */
  1055. static int __init
  1056. pcibios_init(void)
  1057. {
  1058. struct pci_controller *hose, *tmp;
  1059. struct pci_bus *bus;
  1060. int next_busno = 0;
  1061. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  1062. /* Scan all of the recorded PCI controllers. */
  1063. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1064. if (pci_assign_all_buses)
  1065. hose->first_busno = next_busno;
  1066. hose->last_busno = 0xff;
  1067. bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
  1068. hose->ops, hose);
  1069. if (bus)
  1070. pci_bus_add_devices(bus);
  1071. hose->last_busno = bus->subordinate;
  1072. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  1073. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  1074. }
  1075. pci_bus_count = next_busno;
  1076. /* OpenFirmware based machines need a map of OF bus
  1077. * numbers vs. kernel bus numbers since we may have to
  1078. * remap them.
  1079. */
  1080. if (pci_assign_all_buses && have_of)
  1081. pcibios_make_OF_bus_map();
  1082. /* Call machine dependent fixup */
  1083. if (ppc_md.pcibios_fixup)
  1084. ppc_md.pcibios_fixup();
  1085. /* Allocate and assign resources */
  1086. pcibios_allocate_bus_resources(&pci_root_buses);
  1087. pcibios_allocate_resources(0);
  1088. pcibios_allocate_resources(1);
  1089. #ifdef CONFIG_PPC_PMAC
  1090. pcibios_fixup_p2p_bridges();
  1091. #endif /* CONFIG_PPC_PMAC */
  1092. pcibios_assign_resources();
  1093. /* Call machine dependent post-init code */
  1094. if (ppc_md.pcibios_after_init)
  1095. ppc_md.pcibios_after_init();
  1096. return 0;
  1097. }
  1098. subsys_initcall(pcibios_init);
  1099. void __init pcibios_fixup_bus(struct pci_bus *bus)
  1100. {
  1101. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  1102. unsigned long io_offset;
  1103. struct resource *res;
  1104. struct pci_dev *dev;
  1105. int i;
  1106. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  1107. if (bus->parent == NULL) {
  1108. /* This is a host bridge - fill in its resources */
  1109. hose->bus = bus;
  1110. bus->resource[0] = res = &hose->io_resource;
  1111. if (!res->flags) {
  1112. if (io_offset)
  1113. printk(KERN_ERR "I/O resource not set for host"
  1114. " bridge %d\n", hose->global_number);
  1115. res->start = 0;
  1116. res->end = IO_SPACE_LIMIT;
  1117. res->flags = IORESOURCE_IO;
  1118. }
  1119. res->start += io_offset;
  1120. res->end += io_offset;
  1121. for (i = 0; i < 3; ++i) {
  1122. res = &hose->mem_resources[i];
  1123. if (!res->flags) {
  1124. if (i > 0)
  1125. continue;
  1126. printk(KERN_ERR "Memory resource not set for "
  1127. "host bridge %d\n", hose->global_number);
  1128. res->start = hose->pci_mem_offset;
  1129. res->end = ~0U;
  1130. res->flags = IORESOURCE_MEM;
  1131. }
  1132. bus->resource[i+1] = res;
  1133. }
  1134. } else {
  1135. /* This is a subordinate bridge */
  1136. pci_read_bridge_bases(bus);
  1137. for (i = 0; i < 4; ++i) {
  1138. if ((res = bus->resource[i]) == NULL)
  1139. continue;
  1140. if (!res->flags || bus->self->transparent)
  1141. continue;
  1142. if (io_offset && (res->flags & IORESOURCE_IO)) {
  1143. res->start += io_offset;
  1144. res->end += io_offset;
  1145. } else if (hose->pci_mem_offset
  1146. && (res->flags & IORESOURCE_MEM)) {
  1147. res->start += hose->pci_mem_offset;
  1148. res->end += hose->pci_mem_offset;
  1149. }
  1150. }
  1151. }
  1152. /* Platform specific bus fixups */
  1153. if (ppc_md.pcibios_fixup_bus)
  1154. ppc_md.pcibios_fixup_bus(bus);
  1155. /* Read default IRQs and fixup if necessary */
  1156. list_for_each_entry(dev, &bus->devices, bus_list) {
  1157. pci_read_irq_line(dev);
  1158. if (ppc_md.pci_irq_fixup)
  1159. ppc_md.pci_irq_fixup(dev);
  1160. }
  1161. }
  1162. /* the next one is stolen from the alpha port... */
  1163. void __init
  1164. pcibios_update_irq(struct pci_dev *dev, int irq)
  1165. {
  1166. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  1167. /* XXX FIXME - update OF device tree node interrupt property */
  1168. }
  1169. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1170. {
  1171. u16 cmd, old_cmd;
  1172. int idx;
  1173. struct resource *r;
  1174. if (ppc_md.pcibios_enable_device_hook)
  1175. if (ppc_md.pcibios_enable_device_hook(dev, 0))
  1176. return -EINVAL;
  1177. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1178. old_cmd = cmd;
  1179. for (idx=0; idx<6; idx++) {
  1180. r = &dev->resource[idx];
  1181. if (r->flags & IORESOURCE_UNSET) {
  1182. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  1183. return -EINVAL;
  1184. }
  1185. if (r->flags & IORESOURCE_IO)
  1186. cmd |= PCI_COMMAND_IO;
  1187. if (r->flags & IORESOURCE_MEM)
  1188. cmd |= PCI_COMMAND_MEMORY;
  1189. }
  1190. if (cmd != old_cmd) {
  1191. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  1192. pci_name(dev), old_cmd, cmd);
  1193. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1194. }
  1195. return 0;
  1196. }
  1197. static struct pci_controller*
  1198. pci_bus_to_hose(int bus)
  1199. {
  1200. struct pci_controller *hose, *tmp;
  1201. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1202. if (bus >= hose->first_busno && bus <= hose->last_busno)
  1203. return hose;
  1204. return NULL;
  1205. }
  1206. /* Provide information on locations of various I/O regions in physical
  1207. * memory. Do this on a per-card basis so that we choose the right
  1208. * root bridge.
  1209. * Note that the returned IO or memory base is a physical address
  1210. */
  1211. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  1212. {
  1213. struct pci_controller* hose;
  1214. long result = -EOPNOTSUPP;
  1215. /* Argh ! Please forgive me for that hack, but that's the
  1216. * simplest way to get existing XFree to not lockup on some
  1217. * G5 machines... So when something asks for bus 0 io base
  1218. * (bus 0 is HT root), we return the AGP one instead.
  1219. */
  1220. #ifdef CONFIG_PPC_PMAC
  1221. if (machine_is(powermac) && machine_is_compatible("MacRISC4"))
  1222. if (bus == 0)
  1223. bus = 0xf0;
  1224. #endif /* CONFIG_PPC_PMAC */
  1225. hose = pci_bus_to_hose(bus);
  1226. if (!hose)
  1227. return -ENODEV;
  1228. switch (which) {
  1229. case IOBASE_BRIDGE_NUMBER:
  1230. return (long)hose->first_busno;
  1231. case IOBASE_MEMORY:
  1232. return (long)hose->pci_mem_offset;
  1233. case IOBASE_IO:
  1234. return (long)hose->io_base_phys;
  1235. case IOBASE_ISA_IO:
  1236. return (long)isa_io_base;
  1237. case IOBASE_ISA_MEM:
  1238. return (long)isa_mem_base;
  1239. }
  1240. return result;
  1241. }
  1242. unsigned long pci_address_to_pio(phys_addr_t address)
  1243. {
  1244. struct pci_controller *hose, *tmp;
  1245. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1246. unsigned int size = hose->io_resource.end -
  1247. hose->io_resource.start + 1;
  1248. if (address >= hose->io_base_phys &&
  1249. address < (hose->io_base_phys + size)) {
  1250. unsigned long base =
  1251. (unsigned long)hose->io_base_virt - _IO_BASE;
  1252. return base + (address - hose->io_base_phys);
  1253. }
  1254. }
  1255. return (unsigned int)-1;
  1256. }
  1257. EXPORT_SYMBOL(pci_address_to_pio);
  1258. /*
  1259. * Null PCI config access functions, for the case when we can't
  1260. * find a hose.
  1261. */
  1262. #define NULL_PCI_OP(rw, size, type) \
  1263. static int \
  1264. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1265. { \
  1266. return PCIBIOS_DEVICE_NOT_FOUND; \
  1267. }
  1268. static int
  1269. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1270. int len, u32 *val)
  1271. {
  1272. return PCIBIOS_DEVICE_NOT_FOUND;
  1273. }
  1274. static int
  1275. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1276. int len, u32 val)
  1277. {
  1278. return PCIBIOS_DEVICE_NOT_FOUND;
  1279. }
  1280. static struct pci_ops null_pci_ops =
  1281. {
  1282. null_read_config,
  1283. null_write_config
  1284. };
  1285. /*
  1286. * These functions are used early on before PCI scanning is done
  1287. * and all of the pci_dev and pci_bus structures have been created.
  1288. */
  1289. static struct pci_bus *
  1290. fake_pci_bus(struct pci_controller *hose, int busnr)
  1291. {
  1292. static struct pci_bus bus;
  1293. if (hose == 0) {
  1294. hose = pci_bus_to_hose(busnr);
  1295. if (hose == 0)
  1296. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1297. }
  1298. bus.number = busnr;
  1299. bus.sysdata = hose;
  1300. bus.ops = hose? hose->ops: &null_pci_ops;
  1301. return &bus;
  1302. }
  1303. #define EARLY_PCI_OP(rw, size, type) \
  1304. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1305. int devfn, int offset, type value) \
  1306. { \
  1307. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1308. devfn, offset, value); \
  1309. }
  1310. EARLY_PCI_OP(read, byte, u8 *)
  1311. EARLY_PCI_OP(read, word, u16 *)
  1312. EARLY_PCI_OP(read, dword, u32 *)
  1313. EARLY_PCI_OP(write, byte, u8)
  1314. EARLY_PCI_OP(write, word, u16)
  1315. EARLY_PCI_OP(write, dword, u32)