pci_32.c 28 KB

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