pci_32.c 38 KB

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