pci_32.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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. /* Default PCI flags is 0 */
  33. unsigned int ppc_pci_flags;
  34. void pcibios_make_OF_bus_map(void);
  35. static void fixup_broken_pcnet32(struct pci_dev* dev);
  36. static int reparent_resources(struct resource *parent, struct resource *res);
  37. static void fixup_cpc710_pci64(struct pci_dev* dev);
  38. #ifdef CONFIG_PPC_OF
  39. static u8* pci_to_OF_bus_map;
  40. #endif
  41. /* By default, we don't re-assign bus numbers. We do this only on
  42. * some pmacs
  43. */
  44. static int pci_assign_all_buses;
  45. LIST_HEAD(hose_list);
  46. static int pci_bus_count;
  47. static void
  48. fixup_hide_host_resource_fsl(struct pci_dev* dev)
  49. {
  50. int i, class = dev->class >> 8;
  51. if ((class == PCI_CLASS_PROCESSOR_POWERPC) &&
  52. (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
  53. (dev->bus->parent == NULL)) {
  54. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  55. dev->resource[i].start = 0;
  56. dev->resource[i].end = 0;
  57. dev->resource[i].flags = 0;
  58. }
  59. }
  60. }
  61. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
  62. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
  63. static void
  64. fixup_broken_pcnet32(struct pci_dev* dev)
  65. {
  66. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  67. dev->vendor = PCI_VENDOR_ID_AMD;
  68. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  69. }
  70. }
  71. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  72. static void
  73. fixup_cpc710_pci64(struct pci_dev* dev)
  74. {
  75. /* Hide the PCI64 BARs from the kernel as their content doesn't
  76. * fit well in the resource management
  77. */
  78. dev->resource[0].start = dev->resource[0].end = 0;
  79. dev->resource[0].flags = 0;
  80. dev->resource[1].start = dev->resource[1].end = 0;
  81. dev->resource[1].flags = 0;
  82. }
  83. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
  84. static int skip_isa_ioresource_align(struct pci_dev *dev)
  85. {
  86. if ((ppc_pci_flags & PPC_PCI_CAN_SKIP_ISA_ALIGN) &&
  87. !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
  88. return 1;
  89. return 0;
  90. }
  91. /*
  92. * We need to avoid collisions with `mirrored' VGA ports
  93. * and other strange ISA hardware, so we always want the
  94. * addresses to be allocated in the 0x000-0x0ff region
  95. * modulo 0x400.
  96. *
  97. * Why? Because some silly external IO cards only decode
  98. * the low 10 bits of the IO address. The 0x00-0xff region
  99. * is reserved for motherboard devices that decode all 16
  100. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  101. * but we want to try to avoid allocating at 0x2900-0x2bff
  102. * which might have be mirrored at 0x0100-0x03ff..
  103. */
  104. void pcibios_align_resource(void *data, struct resource *res,
  105. resource_size_t size, resource_size_t align)
  106. {
  107. struct pci_dev *dev = data;
  108. if (res->flags & IORESOURCE_IO) {
  109. resource_size_t start = res->start;
  110. if (skip_isa_ioresource_align(dev))
  111. return;
  112. if (start & 0x300) {
  113. start = (start + 0x3ff) & ~0x3ff;
  114. res->start = start;
  115. }
  116. }
  117. }
  118. EXPORT_SYMBOL(pcibios_align_resource);
  119. /*
  120. * Handle resources of PCI devices. If the world were perfect, we could
  121. * just allocate all the resource regions and do nothing more. It isn't.
  122. * On the other hand, we cannot just re-allocate all devices, as it would
  123. * require us to know lots of host bridge internals. So we attempt to
  124. * keep as much of the original configuration as possible, but tweak it
  125. * when it's found to be wrong.
  126. *
  127. * Known BIOS problems we have to work around:
  128. * - I/O or memory regions not configured
  129. * - regions configured, but not enabled in the command register
  130. * - bogus I/O addresses above 64K used
  131. * - expansion ROMs left enabled (this may sound harmless, but given
  132. * the fact the PCI specs explicitly allow address decoders to be
  133. * shared between expansion ROMs and other resource regions, it's
  134. * at least dangerous)
  135. *
  136. * Our solution:
  137. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  138. * This gives us fixed barriers on where we can allocate.
  139. * (2) Allocate resources for all enabled devices. If there is
  140. * a collision, just mark the resource as unallocated. Also
  141. * disable expansion ROMs during this step.
  142. * (3) Try to allocate resources for disabled devices. If the
  143. * resources were assigned correctly, everything goes well,
  144. * if they weren't, they won't disturb allocation of other
  145. * resources.
  146. * (4) Assign new addresses to resources which were either
  147. * not configured at all or misconfigured. If explicitly
  148. * requested by the user, configure expansion ROM address
  149. * as well.
  150. */
  151. static void __init
  152. pcibios_allocate_bus_resources(struct list_head *bus_list)
  153. {
  154. struct pci_bus *bus;
  155. int i;
  156. struct resource *res, *pr;
  157. /* Depth-First Search on bus tree */
  158. list_for_each_entry(bus, bus_list, node) {
  159. for (i = 0; i < 4; ++i) {
  160. if ((res = bus->resource[i]) == NULL || !res->flags
  161. || res->start > res->end)
  162. continue;
  163. if (bus->parent == NULL)
  164. pr = (res->flags & IORESOURCE_IO)?
  165. &ioport_resource : &iomem_resource;
  166. else {
  167. /* Don't bother with non-root busses when
  168. * re-assigning all resources.
  169. */
  170. if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)
  171. continue;
  172. pr = pci_find_parent_resource(bus->self, res);
  173. if (pr == res) {
  174. /* this happens when the generic PCI
  175. * code (wrongly) decides that this
  176. * bridge is transparent -- paulus
  177. */
  178. continue;
  179. }
  180. }
  181. DBG("PCI: dev %s (bus 0x%02x) bridge rsrc %d: %016llx..%016llx "
  182. "(f:0x%08lx), parent %p\n",
  183. bus->self ? pci_name(bus->self) : "PHB", bus->number, i,
  184. (u64)res->start, (u64)res->end, res->flags, pr);
  185. if (pr && !(pr->flags & IORESOURCE_UNSET)) {
  186. if (request_resource(pr, res) == 0)
  187. continue;
  188. /*
  189. * Must be a conflict with an existing entry.
  190. * Move that entry (or entries) under the
  191. * bridge resource and try again.
  192. */
  193. if (reparent_resources(pr, res) == 0)
  194. continue;
  195. }
  196. printk(KERN_WARNING
  197. "PCI: Cannot allocate resource region "
  198. "%d of PCI bridge %d, will remap\n",
  199. i, bus->number);
  200. res->flags |= IORESOURCE_UNSET;
  201. }
  202. pcibios_allocate_bus_resources(&bus->children);
  203. }
  204. }
  205. /*
  206. * Reparent resource children of pr that conflict with res
  207. * under res, and make res replace those children.
  208. */
  209. static int __init
  210. reparent_resources(struct resource *parent, struct resource *res)
  211. {
  212. struct resource *p, **pp;
  213. struct resource **firstpp = NULL;
  214. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  215. if (p->end < res->start)
  216. continue;
  217. if (res->end < p->start)
  218. break;
  219. if (p->start < res->start || p->end > res->end)
  220. return -1; /* not completely contained */
  221. if (firstpp == NULL)
  222. firstpp = pp;
  223. }
  224. if (firstpp == NULL)
  225. return -1; /* didn't find any conflicting entries? */
  226. res->parent = parent;
  227. res->child = *firstpp;
  228. res->sibling = *pp;
  229. *firstpp = res;
  230. *pp = NULL;
  231. for (p = res->child; p != NULL; p = p->sibling) {
  232. p->parent = res;
  233. DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
  234. p->name, (u64)p->start, (u64)p->end, res->name);
  235. }
  236. return 0;
  237. }
  238. void __init
  239. update_bridge_resource(struct pci_dev *dev, struct resource *res)
  240. {
  241. u8 io_base_lo, io_limit_lo;
  242. u16 mem_base, mem_limit;
  243. u16 cmd;
  244. resource_size_t start, end, off;
  245. struct pci_controller *hose = dev->sysdata;
  246. if (!hose) {
  247. printk("update_bridge_base: no hose?\n");
  248. return;
  249. }
  250. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  251. pci_write_config_word(dev, PCI_COMMAND,
  252. cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
  253. if (res->flags & IORESOURCE_IO) {
  254. off = (unsigned long) hose->io_base_virt - isa_io_base;
  255. start = res->start - off;
  256. end = res->end - off;
  257. io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
  258. io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
  259. if (end > 0xffff)
  260. io_base_lo |= PCI_IO_RANGE_TYPE_32;
  261. else
  262. io_base_lo |= PCI_IO_RANGE_TYPE_16;
  263. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  264. start >> 16);
  265. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  266. end >> 16);
  267. pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
  268. pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
  269. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  270. == IORESOURCE_MEM) {
  271. off = hose->pci_mem_offset;
  272. mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  273. mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  274. pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
  275. pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
  276. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  277. == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
  278. off = hose->pci_mem_offset;
  279. mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
  280. mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
  281. pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
  282. pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
  283. } else {
  284. DBG(KERN_ERR "PCI: ugh, bridge %s res has flags=%lx\n",
  285. pci_name(dev), res->flags);
  286. }
  287. pci_write_config_word(dev, PCI_COMMAND, cmd);
  288. }
  289. static inline void alloc_resource(struct pci_dev *dev, int idx)
  290. {
  291. struct resource *pr, *r = &dev->resource[idx];
  292. DBG("PCI: Allocating %s: Resource %d: %016llx..%016llx (f=%lx)\n",
  293. pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags);
  294. pr = pci_find_parent_resource(dev, r);
  295. if (!pr || (pr->flags & IORESOURCE_UNSET) || request_resource(pr, r) < 0) {
  296. printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
  297. " of device %s, will remap\n", idx, pci_name(dev));
  298. if (pr)
  299. DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n",
  300. pr, (u64)pr->start, (u64)pr->end, pr->flags);
  301. /* We'll assign a new address later */
  302. r->flags |= IORESOURCE_UNSET;
  303. r->end -= r->start;
  304. r->start = 0;
  305. }
  306. }
  307. static void __init
  308. pcibios_allocate_resources(int pass)
  309. {
  310. struct pci_dev *dev = NULL;
  311. int idx, disabled;
  312. u16 command;
  313. struct resource *r;
  314. for_each_pci_dev(dev) {
  315. pci_read_config_word(dev, PCI_COMMAND, &command);
  316. for (idx = 0; idx < 6; idx++) {
  317. r = &dev->resource[idx];
  318. if (r->parent) /* Already allocated */
  319. continue;
  320. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  321. continue; /* Not assigned at all */
  322. if (r->flags & IORESOURCE_IO)
  323. disabled = !(command & PCI_COMMAND_IO);
  324. else
  325. disabled = !(command & PCI_COMMAND_MEMORY);
  326. if (pass == disabled)
  327. alloc_resource(dev, idx);
  328. }
  329. if (pass)
  330. continue;
  331. r = &dev->resource[PCI_ROM_RESOURCE];
  332. if (r->flags & IORESOURCE_ROM_ENABLE) {
  333. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  334. u32 reg;
  335. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  336. r->flags &= ~IORESOURCE_ROM_ENABLE;
  337. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  338. pci_write_config_dword(dev, dev->rom_base_reg,
  339. reg & ~PCI_ROM_ADDRESS_ENABLE);
  340. }
  341. }
  342. }
  343. #ifdef CONFIG_PPC_OF
  344. /*
  345. * Functions below are used on OpenFirmware machines.
  346. */
  347. static void
  348. make_one_node_map(struct device_node* node, u8 pci_bus)
  349. {
  350. const int *bus_range;
  351. int len;
  352. if (pci_bus >= pci_bus_count)
  353. return;
  354. bus_range = of_get_property(node, "bus-range", &len);
  355. if (bus_range == NULL || len < 2 * sizeof(int)) {
  356. printk(KERN_WARNING "Can't get bus-range for %s, "
  357. "assuming it starts at 0\n", node->full_name);
  358. pci_to_OF_bus_map[pci_bus] = 0;
  359. } else
  360. pci_to_OF_bus_map[pci_bus] = bus_range[0];
  361. for (node=node->child; node != 0;node = node->sibling) {
  362. struct pci_dev* dev;
  363. const unsigned int *class_code, *reg;
  364. class_code = of_get_property(node, "class-code", NULL);
  365. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  366. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  367. continue;
  368. reg = of_get_property(node, "reg", NULL);
  369. if (!reg)
  370. continue;
  371. dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
  372. if (!dev || !dev->subordinate) {
  373. pci_dev_put(dev);
  374. continue;
  375. }
  376. make_one_node_map(node, dev->subordinate->number);
  377. pci_dev_put(dev);
  378. }
  379. }
  380. void
  381. pcibios_make_OF_bus_map(void)
  382. {
  383. int i;
  384. struct pci_controller *hose, *tmp;
  385. struct property *map_prop;
  386. struct device_node *dn;
  387. pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
  388. if (!pci_to_OF_bus_map) {
  389. printk(KERN_ERR "Can't allocate OF bus map !\n");
  390. return;
  391. }
  392. /* We fill the bus map with invalid values, that helps
  393. * debugging.
  394. */
  395. for (i=0; i<pci_bus_count; i++)
  396. pci_to_OF_bus_map[i] = 0xff;
  397. /* For each hose, we begin searching bridges */
  398. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  399. struct device_node* node = hose->dn;
  400. if (!node)
  401. continue;
  402. make_one_node_map(node, hose->first_busno);
  403. }
  404. dn = of_find_node_by_path("/");
  405. map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
  406. if (map_prop) {
  407. BUG_ON(pci_bus_count > map_prop->length);
  408. memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
  409. }
  410. of_node_put(dn);
  411. #ifdef DEBUG
  412. printk("PCI->OF bus map:\n");
  413. for (i=0; i<pci_bus_count; i++) {
  414. if (pci_to_OF_bus_map[i] == 0xff)
  415. continue;
  416. printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
  417. }
  418. #endif
  419. }
  420. typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
  421. static struct device_node*
  422. scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
  423. {
  424. struct device_node* sub_node;
  425. for (; node != 0;node = node->sibling) {
  426. const unsigned int *class_code;
  427. if (filter(node, data))
  428. return node;
  429. /* For PCI<->PCI bridges or CardBus bridges, we go down
  430. * Note: some OFs create a parent node "multifunc-device" as
  431. * a fake root for all functions of a multi-function device,
  432. * we go down them as well.
  433. */
  434. class_code = of_get_property(node, "class-code", NULL);
  435. if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  436. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
  437. strcmp(node->name, "multifunc-device"))
  438. continue;
  439. sub_node = scan_OF_pci_childs(node->child, filter, data);
  440. if (sub_node)
  441. return sub_node;
  442. }
  443. return NULL;
  444. }
  445. static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
  446. unsigned int devfn)
  447. {
  448. struct device_node *np = NULL;
  449. const u32 *reg;
  450. unsigned int psize;
  451. while ((np = of_get_next_child(parent, np)) != NULL) {
  452. reg = of_get_property(np, "reg", &psize);
  453. if (reg == NULL || psize < 4)
  454. continue;
  455. if (((reg[0] >> 8) & 0xff) == devfn)
  456. return np;
  457. }
  458. return NULL;
  459. }
  460. static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
  461. {
  462. struct device_node *parent, *np;
  463. /* Are we a root bus ? */
  464. if (bus->self == NULL || bus->parent == NULL) {
  465. struct pci_controller *hose = pci_bus_to_host(bus);
  466. if (hose == NULL)
  467. return NULL;
  468. return of_node_get(hose->dn);
  469. }
  470. /* not a root bus, we need to get our parent */
  471. parent = scan_OF_for_pci_bus(bus->parent);
  472. if (parent == NULL)
  473. return NULL;
  474. /* now iterate for children for a match */
  475. np = scan_OF_for_pci_dev(parent, bus->self->devfn);
  476. of_node_put(parent);
  477. return np;
  478. }
  479. /*
  480. * Scans the OF tree for a device node matching a PCI device
  481. */
  482. struct device_node *
  483. pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
  484. {
  485. struct device_node *parent, *np;
  486. if (!have_of)
  487. return NULL;
  488. DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
  489. parent = scan_OF_for_pci_bus(bus);
  490. if (parent == NULL)
  491. return NULL;
  492. DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
  493. np = scan_OF_for_pci_dev(parent, devfn);
  494. of_node_put(parent);
  495. DBG(" result is %s\n", np ? np->full_name : "<NULL>");
  496. /* XXX most callers don't release the returned node
  497. * mostly because ppc64 doesn't increase the refcount,
  498. * we need to fix that.
  499. */
  500. return np;
  501. }
  502. EXPORT_SYMBOL(pci_busdev_to_OF_node);
  503. struct device_node*
  504. pci_device_to_OF_node(struct pci_dev *dev)
  505. {
  506. return pci_busdev_to_OF_node(dev->bus, dev->devfn);
  507. }
  508. EXPORT_SYMBOL(pci_device_to_OF_node);
  509. static int
  510. find_OF_pci_device_filter(struct device_node* node, void* data)
  511. {
  512. return ((void *)node == data);
  513. }
  514. /*
  515. * Returns the PCI device matching a given OF node
  516. */
  517. int
  518. pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
  519. {
  520. const unsigned int *reg;
  521. struct pci_controller* hose;
  522. struct pci_dev* dev = NULL;
  523. if (!have_of)
  524. return -ENODEV;
  525. /* Make sure it's really a PCI device */
  526. hose = pci_find_hose_for_OF_device(node);
  527. if (!hose || !hose->dn)
  528. return -ENODEV;
  529. if (!scan_OF_pci_childs(hose->dn->child,
  530. find_OF_pci_device_filter, (void *)node))
  531. return -ENODEV;
  532. reg = of_get_property(node, "reg", NULL);
  533. if (!reg)
  534. return -ENODEV;
  535. *bus = (reg[0] >> 16) & 0xff;
  536. *devfn = ((reg[0] >> 8) & 0xff);
  537. /* Ok, here we need some tweak. If we have already renumbered
  538. * all busses, we can't rely on the OF bus number any more.
  539. * the pci_to_OF_bus_map is not enough as several PCI busses
  540. * may match the same OF bus number.
  541. */
  542. if (!pci_to_OF_bus_map)
  543. return 0;
  544. for_each_pci_dev(dev)
  545. if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
  546. dev->devfn == *devfn) {
  547. *bus = dev->bus->number;
  548. pci_dev_put(dev);
  549. return 0;
  550. }
  551. return -ENODEV;
  552. }
  553. EXPORT_SYMBOL(pci_device_from_OF_node);
  554. /* We create the "pci-OF-bus-map" property now so it appears in the
  555. * /proc device tree
  556. */
  557. void __init
  558. pci_create_OF_bus_map(void)
  559. {
  560. struct property* of_prop;
  561. struct device_node *dn;
  562. of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
  563. if (!of_prop)
  564. return;
  565. dn = of_find_node_by_path("/");
  566. if (dn) {
  567. memset(of_prop, -1, sizeof(struct property) + 256);
  568. of_prop->name = "pci-OF-bus-map";
  569. of_prop->length = 256;
  570. of_prop->value = &of_prop[1];
  571. prom_add_property(dn, of_prop);
  572. of_node_put(dn);
  573. }
  574. }
  575. #else /* CONFIG_PPC_OF */
  576. void pcibios_make_OF_bus_map(void)
  577. {
  578. }
  579. #endif /* CONFIG_PPC_OF */
  580. static int __init
  581. pcibios_init(void)
  582. {
  583. struct pci_controller *hose, *tmp;
  584. struct pci_bus *bus;
  585. int next_busno = 0;
  586. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  587. if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
  588. pci_assign_all_buses = 1;
  589. /* Scan all of the recorded PCI controllers. */
  590. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  591. if (pci_assign_all_buses)
  592. hose->first_busno = next_busno;
  593. hose->last_busno = 0xff;
  594. bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
  595. hose->ops, hose);
  596. if (bus)
  597. pci_bus_add_devices(bus);
  598. hose->last_busno = bus->subordinate;
  599. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  600. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  601. }
  602. pci_bus_count = next_busno;
  603. /* OpenFirmware based machines need a map of OF bus
  604. * numbers vs. kernel bus numbers since we may have to
  605. * remap them.
  606. */
  607. if (pci_assign_all_buses && have_of)
  608. pcibios_make_OF_bus_map();
  609. /* Call machine dependent fixup */
  610. if (ppc_md.pcibios_fixup)
  611. ppc_md.pcibios_fixup();
  612. /* Allocate and assign resources. If we re-assign everything, then
  613. * we skip the allocate phase
  614. */
  615. pcibios_allocate_bus_resources(&pci_root_buses);
  616. if (!(ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)) {
  617. pcibios_allocate_resources(0);
  618. pcibios_allocate_resources(1);
  619. }
  620. if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
  621. DBG("PCI: Assigning unassigned resouces...\n");
  622. pci_assign_unassigned_resources();
  623. }
  624. /* Call machine dependent post-init code */
  625. if (ppc_md.pcibios_after_init)
  626. ppc_md.pcibios_after_init();
  627. return 0;
  628. }
  629. subsys_initcall(pcibios_init);
  630. void __devinit pcibios_do_bus_setup(struct pci_bus *bus)
  631. {
  632. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  633. unsigned long io_offset;
  634. struct resource *res;
  635. int i;
  636. /* Hookup PHB resources */
  637. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  638. if (bus->parent == NULL) {
  639. /* This is a host bridge - fill in its resources */
  640. hose->bus = bus;
  641. bus->resource[0] = res = &hose->io_resource;
  642. if (!res->flags) {
  643. if (io_offset)
  644. printk(KERN_ERR "I/O resource not set for host"
  645. " bridge %d\n", hose->global_number);
  646. res->start = 0;
  647. res->end = IO_SPACE_LIMIT;
  648. res->flags = IORESOURCE_IO;
  649. }
  650. res->start = (res->start + io_offset) & 0xffffffffu;
  651. res->end = (res->end + io_offset) & 0xffffffffu;
  652. for (i = 0; i < 3; ++i) {
  653. res = &hose->mem_resources[i];
  654. if (!res->flags) {
  655. if (i > 0)
  656. continue;
  657. printk(KERN_ERR "Memory resource not set for "
  658. "host bridge %d\n", hose->global_number);
  659. res->start = hose->pci_mem_offset;
  660. res->end = ~0U;
  661. res->flags = IORESOURCE_MEM;
  662. }
  663. bus->resource[i+1] = res;
  664. }
  665. }
  666. }
  667. /* the next one is stolen from the alpha port... */
  668. void __init
  669. pcibios_update_irq(struct pci_dev *dev, int irq)
  670. {
  671. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  672. /* XXX FIXME - update OF device tree node interrupt property */
  673. }
  674. int pcibios_enable_device(struct pci_dev *dev, int mask)
  675. {
  676. u16 cmd, old_cmd;
  677. int idx;
  678. struct resource *r;
  679. if (ppc_md.pcibios_enable_device_hook)
  680. if (ppc_md.pcibios_enable_device_hook(dev, 0))
  681. return -EINVAL;
  682. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  683. old_cmd = cmd;
  684. for (idx=0; idx<6; idx++) {
  685. r = &dev->resource[idx];
  686. if (r->flags & IORESOURCE_UNSET) {
  687. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  688. return -EINVAL;
  689. }
  690. if (r->flags & IORESOURCE_IO)
  691. cmd |= PCI_COMMAND_IO;
  692. if (r->flags & IORESOURCE_MEM)
  693. cmd |= PCI_COMMAND_MEMORY;
  694. }
  695. if (cmd != old_cmd) {
  696. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  697. pci_name(dev), old_cmd, cmd);
  698. pci_write_config_word(dev, PCI_COMMAND, cmd);
  699. }
  700. return 0;
  701. }
  702. static struct pci_controller*
  703. pci_bus_to_hose(int bus)
  704. {
  705. struct pci_controller *hose, *tmp;
  706. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  707. if (bus >= hose->first_busno && bus <= hose->last_busno)
  708. return hose;
  709. return NULL;
  710. }
  711. /* Provide information on locations of various I/O regions in physical
  712. * memory. Do this on a per-card basis so that we choose the right
  713. * root bridge.
  714. * Note that the returned IO or memory base is a physical address
  715. */
  716. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  717. {
  718. struct pci_controller* hose;
  719. long result = -EOPNOTSUPP;
  720. hose = pci_bus_to_hose(bus);
  721. if (!hose)
  722. return -ENODEV;
  723. switch (which) {
  724. case IOBASE_BRIDGE_NUMBER:
  725. return (long)hose->first_busno;
  726. case IOBASE_MEMORY:
  727. return (long)hose->pci_mem_offset;
  728. case IOBASE_IO:
  729. return (long)hose->io_base_phys;
  730. case IOBASE_ISA_IO:
  731. return (long)isa_io_base;
  732. case IOBASE_ISA_MEM:
  733. return (long)isa_mem_base;
  734. }
  735. return result;
  736. }
  737. unsigned long pci_address_to_pio(phys_addr_t address)
  738. {
  739. struct pci_controller *hose, *tmp;
  740. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  741. unsigned int size = hose->io_resource.end -
  742. hose->io_resource.start + 1;
  743. if (address >= hose->io_base_phys &&
  744. address < (hose->io_base_phys + size)) {
  745. unsigned long base =
  746. (unsigned long)hose->io_base_virt - _IO_BASE;
  747. return base + (address - hose->io_base_phys);
  748. }
  749. }
  750. return (unsigned int)-1;
  751. }
  752. EXPORT_SYMBOL(pci_address_to_pio);
  753. /*
  754. * Null PCI config access functions, for the case when we can't
  755. * find a hose.
  756. */
  757. #define NULL_PCI_OP(rw, size, type) \
  758. static int \
  759. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  760. { \
  761. return PCIBIOS_DEVICE_NOT_FOUND; \
  762. }
  763. static int
  764. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  765. int len, u32 *val)
  766. {
  767. return PCIBIOS_DEVICE_NOT_FOUND;
  768. }
  769. static int
  770. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  771. int len, u32 val)
  772. {
  773. return PCIBIOS_DEVICE_NOT_FOUND;
  774. }
  775. static struct pci_ops null_pci_ops =
  776. {
  777. .read = null_read_config,
  778. .write = null_write_config,
  779. };
  780. /*
  781. * These functions are used early on before PCI scanning is done
  782. * and all of the pci_dev and pci_bus structures have been created.
  783. */
  784. static struct pci_bus *
  785. fake_pci_bus(struct pci_controller *hose, int busnr)
  786. {
  787. static struct pci_bus bus;
  788. if (hose == 0) {
  789. hose = pci_bus_to_hose(busnr);
  790. if (hose == 0)
  791. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  792. }
  793. bus.number = busnr;
  794. bus.sysdata = hose;
  795. bus.ops = hose? hose->ops: &null_pci_ops;
  796. return &bus;
  797. }
  798. #define EARLY_PCI_OP(rw, size, type) \
  799. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  800. int devfn, int offset, type value) \
  801. { \
  802. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  803. devfn, offset, value); \
  804. }
  805. EARLY_PCI_OP(read, byte, u8 *)
  806. EARLY_PCI_OP(read, word, u16 *)
  807. EARLY_PCI_OP(read, dword, u32 *)
  808. EARLY_PCI_OP(write, byte, u8)
  809. EARLY_PCI_OP(write, word, u16)
  810. EARLY_PCI_OP(write, dword, u32)
  811. extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
  812. int early_find_capability(struct pci_controller *hose, int bus, int devfn,
  813. int cap)
  814. {
  815. return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
  816. }