pci_32.c 42 KB

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