pci_64.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. /*
  2. * Port for PPC64 David Engebretsen, IBM Corp.
  3. * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
  4. *
  5. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  6. * Rework, based on alpha PCI code.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #undef DEBUG
  14. #include <linux/config.h>
  15. #include <linux/kernel.h>
  16. #include <linux/pci.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/mm.h>
  21. #include <linux/list.h>
  22. #include <linux/syscalls.h>
  23. #include <asm/processor.h>
  24. #include <asm/io.h>
  25. #include <asm/prom.h>
  26. #include <asm/pci-bridge.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/irq.h>
  29. #include <asm/machdep.h>
  30. #include <asm/ppc-pci.h>
  31. #ifdef DEBUG
  32. #include <asm/udbg.h>
  33. #define DBG(fmt...) printk(fmt)
  34. #else
  35. #define DBG(fmt...)
  36. #endif
  37. unsigned long pci_probe_only = 1;
  38. int pci_assign_all_buses = 0;
  39. /*
  40. * legal IO pages under MAX_ISA_PORT. This is to ensure we don't touch
  41. * devices we don't have access to.
  42. */
  43. unsigned long io_page_mask;
  44. EXPORT_SYMBOL(io_page_mask);
  45. #ifdef CONFIG_PPC_MULTIPLATFORM
  46. static void fixup_resource(struct resource *res, struct pci_dev *dev);
  47. static void do_bus_setup(struct pci_bus *bus);
  48. #endif
  49. /* pci_io_base -- the base address from which io bars are offsets.
  50. * This is the lowest I/O base address (so bar values are always positive),
  51. * and it *must* be the start of ISA space if an ISA bus exists because
  52. * ISA drivers use hard coded offsets. If no ISA bus exists a dummy
  53. * page is mapped and isa_io_limit prevents access to it.
  54. */
  55. unsigned long isa_io_base; /* NULL if no ISA bus */
  56. EXPORT_SYMBOL(isa_io_base);
  57. unsigned long pci_io_base;
  58. EXPORT_SYMBOL(pci_io_base);
  59. void iSeries_pcibios_init(void);
  60. LIST_HEAD(hose_list);
  61. struct dma_mapping_ops pci_dma_ops;
  62. EXPORT_SYMBOL(pci_dma_ops);
  63. int global_phb_number; /* Global phb counter */
  64. /* Cached ISA bridge dev. */
  65. struct pci_dev *ppc64_isabridge_dev = NULL;
  66. static void fixup_broken_pcnet32(struct pci_dev* dev)
  67. {
  68. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  69. dev->vendor = PCI_VENDOR_ID_AMD;
  70. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  71. }
  72. }
  73. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  74. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  75. struct resource *res)
  76. {
  77. unsigned long offset = 0;
  78. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  79. if (!hose)
  80. return;
  81. if (res->flags & IORESOURCE_IO)
  82. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  83. if (res->flags & IORESOURCE_MEM)
  84. offset = hose->pci_mem_offset;
  85. region->start = res->start - offset;
  86. region->end = res->end - offset;
  87. }
  88. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  89. struct pci_bus_region *region)
  90. {
  91. unsigned long offset = 0;
  92. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  93. if (!hose)
  94. return;
  95. if (res->flags & IORESOURCE_IO)
  96. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  97. if (res->flags & IORESOURCE_MEM)
  98. offset = hose->pci_mem_offset;
  99. res->start = region->start + offset;
  100. res->end = region->end + offset;
  101. }
  102. #ifdef CONFIG_HOTPLUG
  103. EXPORT_SYMBOL(pcibios_resource_to_bus);
  104. EXPORT_SYMBOL(pcibios_bus_to_resource);
  105. #endif
  106. /*
  107. * We need to avoid collisions with `mirrored' VGA ports
  108. * and other strange ISA hardware, so we always want the
  109. * addresses to be allocated in the 0x000-0x0ff region
  110. * modulo 0x400.
  111. *
  112. * Why? Because some silly external IO cards only decode
  113. * the low 10 bits of the IO address. The 0x00-0xff region
  114. * is reserved for motherboard devices that decode all 16
  115. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  116. * but we want to try to avoid allocating at 0x2900-0x2bff
  117. * which might have be mirrored at 0x0100-0x03ff..
  118. */
  119. void pcibios_align_resource(void *data, struct resource *res,
  120. unsigned long size, unsigned long align)
  121. {
  122. struct pci_dev *dev = data;
  123. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  124. unsigned long start = res->start;
  125. unsigned long alignto;
  126. if (res->flags & IORESOURCE_IO) {
  127. unsigned long offset = (unsigned long)hose->io_base_virt -
  128. pci_io_base;
  129. /* Make sure we start at our min on all hoses */
  130. if (start - offset < PCIBIOS_MIN_IO)
  131. start = PCIBIOS_MIN_IO + offset;
  132. /*
  133. * Put everything into 0x00-0xff region modulo 0x400
  134. */
  135. if (start & 0x300)
  136. start = (start + 0x3ff) & ~0x3ff;
  137. } else if (res->flags & IORESOURCE_MEM) {
  138. /* Make sure we start at our min on all hoses */
  139. if (start - hose->pci_mem_offset < PCIBIOS_MIN_MEM)
  140. start = PCIBIOS_MIN_MEM + hose->pci_mem_offset;
  141. /* Align to multiple of size of minimum base. */
  142. alignto = max(0x1000UL, align);
  143. start = ALIGN(start, alignto);
  144. }
  145. res->start = start;
  146. }
  147. static DEFINE_SPINLOCK(hose_spinlock);
  148. /*
  149. * pci_controller(phb) initialized common variables.
  150. */
  151. static void __devinit pci_setup_pci_controller(struct pci_controller *hose)
  152. {
  153. memset(hose, 0, sizeof(struct pci_controller));
  154. spin_lock(&hose_spinlock);
  155. hose->global_number = global_phb_number++;
  156. list_add_tail(&hose->list_node, &hose_list);
  157. spin_unlock(&hose_spinlock);
  158. }
  159. static void add_linux_pci_domain(struct device_node *dev,
  160. struct pci_controller *phb)
  161. {
  162. struct property *of_prop;
  163. unsigned int size;
  164. of_prop = (struct property *)
  165. get_property(dev, "linux,pci-domain", &size);
  166. if (of_prop != NULL)
  167. return;
  168. WARN_ON(of_prop && size < sizeof(int));
  169. if (of_prop && size < sizeof(int))
  170. of_prop = NULL;
  171. size = sizeof(struct property) + sizeof(int);
  172. if (of_prop == NULL) {
  173. if (mem_init_done)
  174. of_prop = kmalloc(size, GFP_KERNEL);
  175. else
  176. of_prop = alloc_bootmem(size);
  177. }
  178. memset(of_prop, 0, sizeof(struct property));
  179. of_prop->name = "linux,pci-domain";
  180. of_prop->length = sizeof(int);
  181. of_prop->value = (unsigned char *)&of_prop[1];
  182. *((int *)of_prop->value) = phb->global_number;
  183. prom_add_property(dev, of_prop);
  184. }
  185. struct pci_controller * pcibios_alloc_controller(struct device_node *dev)
  186. {
  187. struct pci_controller *phb;
  188. if (mem_init_done)
  189. phb = kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
  190. else
  191. phb = alloc_bootmem(sizeof (struct pci_controller));
  192. if (phb == NULL)
  193. return NULL;
  194. pci_setup_pci_controller(phb);
  195. phb->arch_data = dev;
  196. phb->is_dynamic = mem_init_done;
  197. if (dev)
  198. add_linux_pci_domain(dev, phb);
  199. return phb;
  200. }
  201. void pcibios_free_controller(struct pci_controller *phb)
  202. {
  203. if (phb->arch_data) {
  204. struct device_node *np = phb->arch_data;
  205. int *domain = (int *)get_property(np,
  206. "linux,pci-domain", NULL);
  207. if (domain)
  208. *domain = -1;
  209. }
  210. if (phb->is_dynamic)
  211. kfree(phb);
  212. }
  213. void __devinit pcibios_claim_one_bus(struct pci_bus *b)
  214. {
  215. struct pci_dev *dev;
  216. struct pci_bus *child_bus;
  217. list_for_each_entry(dev, &b->devices, bus_list) {
  218. int i;
  219. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  220. struct resource *r = &dev->resource[i];
  221. if (r->parent || !r->start || !r->flags)
  222. continue;
  223. pci_claim_resource(dev, i);
  224. }
  225. }
  226. list_for_each_entry(child_bus, &b->children, node)
  227. pcibios_claim_one_bus(child_bus);
  228. }
  229. #ifdef CONFIG_HOTPLUG
  230. EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
  231. #endif
  232. #ifndef CONFIG_PPC_ISERIES
  233. static void __init pcibios_claim_of_setup(void)
  234. {
  235. struct pci_bus *b;
  236. list_for_each_entry(b, &pci_root_buses, node)
  237. pcibios_claim_one_bus(b);
  238. }
  239. #endif
  240. #ifdef CONFIG_PPC_MULTIPLATFORM
  241. static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
  242. {
  243. u32 *prop;
  244. int len;
  245. prop = (u32 *) get_property(np, name, &len);
  246. if (prop && len >= 4)
  247. return *prop;
  248. return def;
  249. }
  250. static unsigned int pci_parse_of_flags(u32 addr0)
  251. {
  252. unsigned int flags = 0;
  253. if (addr0 & 0x02000000) {
  254. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  255. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  256. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  257. if (addr0 & 0x40000000)
  258. flags |= IORESOURCE_PREFETCH
  259. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  260. } else if (addr0 & 0x01000000)
  261. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  262. return flags;
  263. }
  264. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  265. static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev)
  266. {
  267. u64 base, size;
  268. unsigned int flags;
  269. struct resource *res;
  270. u32 *addrs, i;
  271. int proplen;
  272. addrs = (u32 *) get_property(node, "assigned-addresses", &proplen);
  273. if (!addrs)
  274. return;
  275. DBG(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
  276. for (; proplen >= 20; proplen -= 20, addrs += 5) {
  277. flags = pci_parse_of_flags(addrs[0]);
  278. if (!flags)
  279. continue;
  280. base = GET_64BIT(addrs, 1);
  281. size = GET_64BIT(addrs, 3);
  282. if (!size)
  283. continue;
  284. i = addrs[0] & 0xff;
  285. DBG(" base: %llx, size: %llx, i: %x\n",
  286. (unsigned long long)base, (unsigned long long)size, i);
  287. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  288. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  289. } else if (i == dev->rom_base_reg) {
  290. res = &dev->resource[PCI_ROM_RESOURCE];
  291. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  292. } else {
  293. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  294. continue;
  295. }
  296. res->start = base;
  297. res->end = base + size - 1;
  298. res->flags = flags;
  299. res->name = pci_name(dev);
  300. fixup_resource(res, dev);
  301. }
  302. }
  303. struct pci_dev *of_create_pci_dev(struct device_node *node,
  304. struct pci_bus *bus, int devfn)
  305. {
  306. struct pci_dev *dev;
  307. const char *type;
  308. dev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
  309. if (!dev)
  310. return NULL;
  311. type = get_property(node, "device_type", NULL);
  312. if (type == NULL)
  313. type = "";
  314. DBG(" create device, devfn: %x, type: %s\n", devfn, type);
  315. memset(dev, 0, sizeof(struct pci_dev));
  316. dev->bus = bus;
  317. dev->sysdata = node;
  318. dev->dev.parent = bus->bridge;
  319. dev->dev.bus = &pci_bus_type;
  320. dev->devfn = devfn;
  321. dev->multifunction = 0; /* maybe a lie? */
  322. dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
  323. dev->device = get_int_prop(node, "device-id", 0xffff);
  324. dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
  325. dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
  326. dev->cfg_size = pci_cfg_space_size(dev);
  327. sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  328. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  329. dev->class = get_int_prop(node, "class-code", 0);
  330. DBG(" class: 0x%x\n", dev->class);
  331. dev->current_state = 4; /* unknown power state */
  332. if (!strcmp(type, "pci")) {
  333. /* a PCI-PCI bridge */
  334. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  335. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  336. } else if (!strcmp(type, "cardbus")) {
  337. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  338. } else {
  339. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  340. dev->rom_base_reg = PCI_ROM_ADDRESS;
  341. dev->irq = NO_IRQ;
  342. if (node->n_intrs > 0) {
  343. dev->irq = node->intrs[0].line;
  344. pci_write_config_byte(dev, PCI_INTERRUPT_LINE,
  345. dev->irq);
  346. }
  347. }
  348. pci_parse_of_addrs(node, dev);
  349. DBG(" adding to system ...\n");
  350. pci_device_add(dev, bus);
  351. /* XXX pci_scan_msi_device(dev); */
  352. return dev;
  353. }
  354. EXPORT_SYMBOL(of_create_pci_dev);
  355. void __devinit of_scan_bus(struct device_node *node,
  356. struct pci_bus *bus)
  357. {
  358. struct device_node *child = NULL;
  359. u32 *reg;
  360. int reglen, devfn;
  361. struct pci_dev *dev;
  362. DBG("of_scan_bus(%s) bus no %d... \n", node->full_name, bus->number);
  363. while ((child = of_get_next_child(node, child)) != NULL) {
  364. DBG(" * %s\n", child->full_name);
  365. reg = (u32 *) get_property(child, "reg", &reglen);
  366. if (reg == NULL || reglen < 20)
  367. continue;
  368. devfn = (reg[0] >> 8) & 0xff;
  369. /* create a new pci_dev for this device */
  370. dev = of_create_pci_dev(child, bus, devfn);
  371. if (!dev)
  372. continue;
  373. DBG("dev header type: %x\n", dev->hdr_type);
  374. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  375. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  376. of_scan_pci_bridge(child, dev);
  377. }
  378. do_bus_setup(bus);
  379. }
  380. EXPORT_SYMBOL(of_scan_bus);
  381. void __devinit of_scan_pci_bridge(struct device_node *node,
  382. struct pci_dev *dev)
  383. {
  384. struct pci_bus *bus;
  385. u32 *busrange, *ranges;
  386. int len, i, mode;
  387. struct resource *res;
  388. unsigned int flags;
  389. u64 size;
  390. DBG("of_scan_pci_bridge(%s)\n", node->full_name);
  391. /* parse bus-range property */
  392. busrange = (u32 *) get_property(node, "bus-range", &len);
  393. if (busrange == NULL || len != 8) {
  394. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  395. node->full_name);
  396. return;
  397. }
  398. ranges = (u32 *) get_property(node, "ranges", &len);
  399. if (ranges == NULL) {
  400. printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
  401. node->full_name);
  402. return;
  403. }
  404. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  405. if (!bus) {
  406. printk(KERN_ERR "Failed to create pci bus for %s\n",
  407. node->full_name);
  408. return;
  409. }
  410. bus->primary = dev->bus->number;
  411. bus->subordinate = busrange[1];
  412. bus->bridge_ctl = 0;
  413. bus->sysdata = node;
  414. /* parse ranges property */
  415. /* PCI #address-cells == 3 and #size-cells == 2 always */
  416. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  417. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  418. res->flags = 0;
  419. bus->resource[i] = res;
  420. ++res;
  421. }
  422. i = 1;
  423. for (; len >= 32; len -= 32, ranges += 8) {
  424. flags = pci_parse_of_flags(ranges[0]);
  425. size = GET_64BIT(ranges, 6);
  426. if (flags == 0 || size == 0)
  427. continue;
  428. if (flags & IORESOURCE_IO) {
  429. res = bus->resource[0];
  430. if (res->flags) {
  431. printk(KERN_ERR "PCI: ignoring extra I/O range"
  432. " for bridge %s\n", node->full_name);
  433. continue;
  434. }
  435. } else {
  436. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  437. printk(KERN_ERR "PCI: too many memory ranges"
  438. " for bridge %s\n", node->full_name);
  439. continue;
  440. }
  441. res = bus->resource[i];
  442. ++i;
  443. }
  444. res->start = GET_64BIT(ranges, 1);
  445. res->end = res->start + size - 1;
  446. res->flags = flags;
  447. fixup_resource(res, dev);
  448. }
  449. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  450. bus->number);
  451. DBG(" bus name: %s\n", bus->name);
  452. mode = PCI_PROBE_NORMAL;
  453. if (ppc_md.pci_probe_mode)
  454. mode = ppc_md.pci_probe_mode(bus);
  455. DBG(" probe mode: %d\n", mode);
  456. if (mode == PCI_PROBE_DEVTREE)
  457. of_scan_bus(node, bus);
  458. else if (mode == PCI_PROBE_NORMAL)
  459. pci_scan_child_bus(bus);
  460. }
  461. EXPORT_SYMBOL(of_scan_pci_bridge);
  462. #endif /* CONFIG_PPC_MULTIPLATFORM */
  463. void __devinit scan_phb(struct pci_controller *hose)
  464. {
  465. struct pci_bus *bus;
  466. struct device_node *node = hose->arch_data;
  467. int i, mode;
  468. struct resource *res;
  469. DBG("Scanning PHB %s\n", node ? node->full_name : "<NO NAME>");
  470. bus = pci_create_bus(NULL, hose->first_busno, hose->ops, node);
  471. if (bus == NULL) {
  472. printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
  473. hose->global_number);
  474. return;
  475. }
  476. bus->secondary = hose->first_busno;
  477. hose->bus = bus;
  478. bus->resource[0] = res = &hose->io_resource;
  479. if (res->flags && request_resource(&ioport_resource, res))
  480. printk(KERN_ERR "Failed to request PCI IO region "
  481. "on PCI domain %04x\n", hose->global_number);
  482. for (i = 0; i < 3; ++i) {
  483. res = &hose->mem_resources[i];
  484. bus->resource[i+1] = res;
  485. if (res->flags && request_resource(&iomem_resource, res))
  486. printk(KERN_ERR "Failed to request PCI memory region "
  487. "on PCI domain %04x\n", hose->global_number);
  488. }
  489. mode = PCI_PROBE_NORMAL;
  490. #ifdef CONFIG_PPC_MULTIPLATFORM
  491. if (node && ppc_md.pci_probe_mode)
  492. mode = ppc_md.pci_probe_mode(bus);
  493. DBG(" probe mode: %d\n", mode);
  494. if (mode == PCI_PROBE_DEVTREE) {
  495. bus->subordinate = hose->last_busno;
  496. of_scan_bus(node, bus);
  497. }
  498. #endif /* CONFIG_PPC_MULTIPLATFORM */
  499. if (mode == PCI_PROBE_NORMAL)
  500. hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
  501. pci_bus_add_devices(bus);
  502. }
  503. static int __init pcibios_init(void)
  504. {
  505. struct pci_controller *hose, *tmp;
  506. /* For now, override phys_mem_access_prot. If we need it,
  507. * later, we may move that initialization to each ppc_md
  508. */
  509. ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
  510. #ifdef CONFIG_PPC_ISERIES
  511. iSeries_pcibios_init();
  512. #endif
  513. printk("PCI: Probing PCI hardware\n");
  514. /* Scan all of the recorded PCI controllers. */
  515. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  516. scan_phb(hose);
  517. #ifndef CONFIG_PPC_ISERIES
  518. if (pci_probe_only)
  519. pcibios_claim_of_setup();
  520. else
  521. /* FIXME: `else' will be removed when
  522. pci_assign_unassigned_resources() is able to work
  523. correctly with [partially] allocated PCI tree. */
  524. pci_assign_unassigned_resources();
  525. #endif /* !CONFIG_PPC_ISERIES */
  526. /* Call machine dependent final fixup */
  527. if (ppc_md.pcibios_fixup)
  528. ppc_md.pcibios_fixup();
  529. /* Cache the location of the ISA bridge (if we have one) */
  530. ppc64_isabridge_dev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
  531. if (ppc64_isabridge_dev != NULL)
  532. printk("ISA bridge at %s\n", pci_name(ppc64_isabridge_dev));
  533. #ifdef CONFIG_PPC_MULTIPLATFORM
  534. /* map in PCI I/O space */
  535. phbs_remap_io();
  536. #endif
  537. printk("PCI: Probing PCI hardware done\n");
  538. return 0;
  539. }
  540. subsys_initcall(pcibios_init);
  541. char __init *pcibios_setup(char *str)
  542. {
  543. return str;
  544. }
  545. int pcibios_enable_device(struct pci_dev *dev, int mask)
  546. {
  547. u16 cmd, oldcmd;
  548. int i;
  549. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  550. oldcmd = cmd;
  551. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  552. struct resource *res = &dev->resource[i];
  553. /* Only set up the requested stuff */
  554. if (!(mask & (1<<i)))
  555. continue;
  556. if (res->flags & IORESOURCE_IO)
  557. cmd |= PCI_COMMAND_IO;
  558. if (res->flags & IORESOURCE_MEM)
  559. cmd |= PCI_COMMAND_MEMORY;
  560. }
  561. if (cmd != oldcmd) {
  562. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  563. pci_name(dev), cmd);
  564. /* Enable the appropriate bits in the PCI command register. */
  565. pci_write_config_word(dev, PCI_COMMAND, cmd);
  566. }
  567. return 0;
  568. }
  569. /*
  570. * Return the domain number for this bus.
  571. */
  572. int pci_domain_nr(struct pci_bus *bus)
  573. {
  574. #ifdef CONFIG_PPC_ISERIES
  575. return 0;
  576. #else
  577. struct pci_controller *hose = pci_bus_to_host(bus);
  578. return hose->global_number;
  579. #endif
  580. }
  581. EXPORT_SYMBOL(pci_domain_nr);
  582. /* Decide whether to display the domain number in /proc */
  583. int pci_proc_domain(struct pci_bus *bus)
  584. {
  585. #ifdef CONFIG_PPC_ISERIES
  586. return 0;
  587. #else
  588. struct pci_controller *hose = pci_bus_to_host(bus);
  589. return hose->buid;
  590. #endif
  591. }
  592. /*
  593. * Platform support for /proc/bus/pci/X/Y mmap()s,
  594. * modelled on the sparc64 implementation by Dave Miller.
  595. * -- paulus.
  596. */
  597. /*
  598. * Adjust vm_pgoff of VMA such that it is the physical page offset
  599. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  600. *
  601. * Basically, the user finds the base address for his device which he wishes
  602. * to mmap. They read the 32-bit value from the config space base register,
  603. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  604. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  605. *
  606. * Returns negative error code on failure, zero on success.
  607. */
  608. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  609. unsigned long *offset,
  610. enum pci_mmap_state mmap_state)
  611. {
  612. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  613. unsigned long io_offset = 0;
  614. int i, res_bit;
  615. if (hose == 0)
  616. return NULL; /* should never happen */
  617. /* If memory, add on the PCI bridge address offset */
  618. if (mmap_state == pci_mmap_mem) {
  619. *offset += hose->pci_mem_offset;
  620. res_bit = IORESOURCE_MEM;
  621. } else {
  622. io_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  623. *offset += io_offset;
  624. res_bit = IORESOURCE_IO;
  625. }
  626. /*
  627. * Check that the offset requested corresponds to one of the
  628. * resources of the device.
  629. */
  630. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  631. struct resource *rp = &dev->resource[i];
  632. int flags = rp->flags;
  633. /* treat ROM as memory (should be already) */
  634. if (i == PCI_ROM_RESOURCE)
  635. flags |= IORESOURCE_MEM;
  636. /* Active and same type? */
  637. if ((flags & res_bit) == 0)
  638. continue;
  639. /* In the range of this resource? */
  640. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  641. continue;
  642. /* found it! construct the final physical address */
  643. if (mmap_state == pci_mmap_io)
  644. *offset += hose->io_base_phys - io_offset;
  645. return rp;
  646. }
  647. return NULL;
  648. }
  649. /*
  650. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  651. * device mapping.
  652. */
  653. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  654. pgprot_t protection,
  655. enum pci_mmap_state mmap_state,
  656. int write_combine)
  657. {
  658. unsigned long prot = pgprot_val(protection);
  659. /* Write combine is always 0 on non-memory space mappings. On
  660. * memory space, if the user didn't pass 1, we check for a
  661. * "prefetchable" resource. This is a bit hackish, but we use
  662. * this to workaround the inability of /sysfs to provide a write
  663. * combine bit
  664. */
  665. if (mmap_state != pci_mmap_mem)
  666. write_combine = 0;
  667. else if (write_combine == 0) {
  668. if (rp->flags & IORESOURCE_PREFETCH)
  669. write_combine = 1;
  670. }
  671. /* XXX would be nice to have a way to ask for write-through */
  672. prot |= _PAGE_NO_CACHE;
  673. if (write_combine)
  674. prot &= ~_PAGE_GUARDED;
  675. else
  676. prot |= _PAGE_GUARDED;
  677. printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
  678. prot);
  679. return __pgprot(prot);
  680. }
  681. /*
  682. * This one is used by /dev/mem and fbdev who have no clue about the
  683. * PCI device, it tries to find the PCI device first and calls the
  684. * above routine
  685. */
  686. pgprot_t pci_phys_mem_access_prot(struct file *file,
  687. unsigned long pfn,
  688. unsigned long size,
  689. pgprot_t protection)
  690. {
  691. struct pci_dev *pdev = NULL;
  692. struct resource *found = NULL;
  693. unsigned long prot = pgprot_val(protection);
  694. unsigned long offset = pfn << PAGE_SHIFT;
  695. int i;
  696. if (page_is_ram(pfn))
  697. return __pgprot(prot);
  698. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  699. for_each_pci_dev(pdev) {
  700. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  701. struct resource *rp = &pdev->resource[i];
  702. int flags = rp->flags;
  703. /* Active and same type? */
  704. if ((flags & IORESOURCE_MEM) == 0)
  705. continue;
  706. /* In the range of this resource? */
  707. if (offset < (rp->start & PAGE_MASK) ||
  708. offset > rp->end)
  709. continue;
  710. found = rp;
  711. break;
  712. }
  713. if (found)
  714. break;
  715. }
  716. if (found) {
  717. if (found->flags & IORESOURCE_PREFETCH)
  718. prot &= ~_PAGE_GUARDED;
  719. pci_dev_put(pdev);
  720. }
  721. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  722. return __pgprot(prot);
  723. }
  724. /*
  725. * Perform the actual remap of the pages for a PCI device mapping, as
  726. * appropriate for this architecture. The region in the process to map
  727. * is described by vm_start and vm_end members of VMA, the base physical
  728. * address is found in vm_pgoff.
  729. * The pci device structure is provided so that architectures may make mapping
  730. * decisions on a per-device or per-bus basis.
  731. *
  732. * Returns a negative error code on failure, zero on success.
  733. */
  734. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  735. enum pci_mmap_state mmap_state, int write_combine)
  736. {
  737. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  738. struct resource *rp;
  739. int ret;
  740. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  741. if (rp == NULL)
  742. return -EINVAL;
  743. vma->vm_pgoff = offset >> PAGE_SHIFT;
  744. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  745. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  746. vma->vm_page_prot,
  747. mmap_state, write_combine);
  748. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  749. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  750. return ret;
  751. }
  752. #ifdef CONFIG_PPC_MULTIPLATFORM
  753. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  754. {
  755. struct pci_dev *pdev;
  756. struct device_node *np;
  757. pdev = to_pci_dev (dev);
  758. np = pci_device_to_OF_node(pdev);
  759. if (np == NULL || np->full_name == NULL)
  760. return 0;
  761. return sprintf(buf, "%s", np->full_name);
  762. }
  763. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  764. #endif /* CONFIG_PPC_MULTIPLATFORM */
  765. void pcibios_add_platform_entries(struct pci_dev *pdev)
  766. {
  767. #ifdef CONFIG_PPC_MULTIPLATFORM
  768. device_create_file(&pdev->dev, &dev_attr_devspec);
  769. #endif /* CONFIG_PPC_MULTIPLATFORM */
  770. }
  771. #ifdef CONFIG_PPC_MULTIPLATFORM
  772. #define ISA_SPACE_MASK 0x1
  773. #define ISA_SPACE_IO 0x1
  774. static void __devinit pci_process_ISA_OF_ranges(struct device_node *isa_node,
  775. unsigned long phb_io_base_phys,
  776. void __iomem * phb_io_base_virt)
  777. {
  778. /* Remove these asap */
  779. struct pci_address {
  780. u32 a_hi;
  781. u32 a_mid;
  782. u32 a_lo;
  783. };
  784. struct isa_address {
  785. u32 a_hi;
  786. u32 a_lo;
  787. };
  788. struct isa_range {
  789. struct isa_address isa_addr;
  790. struct pci_address pci_addr;
  791. unsigned int size;
  792. };
  793. struct isa_range *range;
  794. unsigned long pci_addr;
  795. unsigned int isa_addr;
  796. unsigned int size;
  797. int rlen = 0;
  798. range = (struct isa_range *) get_property(isa_node, "ranges", &rlen);
  799. if (range == NULL || (rlen < sizeof(struct isa_range))) {
  800. printk(KERN_ERR "no ISA ranges or unexpected isa range size,"
  801. "mapping 64k\n");
  802. __ioremap_explicit(phb_io_base_phys,
  803. (unsigned long)phb_io_base_virt,
  804. 0x10000, _PAGE_NO_CACHE | _PAGE_GUARDED);
  805. return;
  806. }
  807. /* From "ISA Binding to 1275"
  808. * The ranges property is laid out as an array of elements,
  809. * each of which comprises:
  810. * cells 0 - 1: an ISA address
  811. * cells 2 - 4: a PCI address
  812. * (size depending on dev->n_addr_cells)
  813. * cell 5: the size of the range
  814. */
  815. if ((range->isa_addr.a_hi && ISA_SPACE_MASK) == ISA_SPACE_IO) {
  816. isa_addr = range->isa_addr.a_lo;
  817. pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
  818. range->pci_addr.a_lo;
  819. /* Assume these are both zero */
  820. if ((pci_addr != 0) || (isa_addr != 0)) {
  821. printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
  822. __FUNCTION__);
  823. return;
  824. }
  825. size = PAGE_ALIGN(range->size);
  826. __ioremap_explicit(phb_io_base_phys,
  827. (unsigned long) phb_io_base_virt,
  828. size, _PAGE_NO_CACHE | _PAGE_GUARDED);
  829. }
  830. }
  831. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  832. struct device_node *dev, int prim)
  833. {
  834. unsigned int *ranges, pci_space;
  835. unsigned long size;
  836. int rlen = 0;
  837. int memno = 0;
  838. struct resource *res;
  839. int np, na = prom_n_addr_cells(dev);
  840. unsigned long pci_addr, cpu_phys_addr;
  841. np = na + 5;
  842. /* From "PCI Binding to 1275"
  843. * The ranges property is laid out as an array of elements,
  844. * each of which comprises:
  845. * cells 0 - 2: a PCI address
  846. * cells 3 or 3+4: a CPU physical address
  847. * (size depending on dev->n_addr_cells)
  848. * cells 4+5 or 5+6: the size of the range
  849. */
  850. ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
  851. if (ranges == NULL)
  852. return;
  853. hose->io_base_phys = 0;
  854. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  855. res = NULL;
  856. pci_space = ranges[0];
  857. pci_addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  858. cpu_phys_addr = ranges[3];
  859. if (na >= 2)
  860. cpu_phys_addr = (cpu_phys_addr << 32) | ranges[4];
  861. size = ((unsigned long)ranges[na+3] << 32) | ranges[na+4];
  862. ranges += np;
  863. if (size == 0)
  864. continue;
  865. /* Now consume following elements while they are contiguous */
  866. while (rlen >= np * sizeof(unsigned int)) {
  867. unsigned long addr, phys;
  868. if (ranges[0] != pci_space)
  869. break;
  870. addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  871. phys = ranges[3];
  872. if (na >= 2)
  873. phys = (phys << 32) | ranges[4];
  874. if (addr != pci_addr + size ||
  875. phys != cpu_phys_addr + size)
  876. break;
  877. size += ((unsigned long)ranges[na+3] << 32)
  878. | ranges[na+4];
  879. ranges += np;
  880. rlen -= np * sizeof(unsigned int);
  881. }
  882. switch ((pci_space >> 24) & 0x3) {
  883. case 1: /* I/O space */
  884. hose->io_base_phys = cpu_phys_addr;
  885. hose->pci_io_size = size;
  886. res = &hose->io_resource;
  887. res->flags = IORESOURCE_IO;
  888. res->start = pci_addr;
  889. DBG("phb%d: IO 0x%lx -> 0x%lx\n", hose->global_number,
  890. res->start, res->start + size - 1);
  891. break;
  892. case 2: /* memory space */
  893. memno = 0;
  894. while (memno < 3 && hose->mem_resources[memno].flags)
  895. ++memno;
  896. if (memno == 0)
  897. hose->pci_mem_offset = cpu_phys_addr - pci_addr;
  898. if (memno < 3) {
  899. res = &hose->mem_resources[memno];
  900. res->flags = IORESOURCE_MEM;
  901. res->start = cpu_phys_addr;
  902. DBG("phb%d: MEM 0x%lx -> 0x%lx\n", hose->global_number,
  903. res->start, res->start + size - 1);
  904. }
  905. break;
  906. }
  907. if (res != NULL) {
  908. res->name = dev->full_name;
  909. res->end = res->start + size - 1;
  910. res->parent = NULL;
  911. res->sibling = NULL;
  912. res->child = NULL;
  913. }
  914. }
  915. }
  916. void __init pci_setup_phb_io(struct pci_controller *hose, int primary)
  917. {
  918. unsigned long size = hose->pci_io_size;
  919. unsigned long io_virt_offset;
  920. struct resource *res;
  921. struct device_node *isa_dn;
  922. hose->io_base_virt = reserve_phb_iospace(size);
  923. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  924. hose->global_number, hose->io_base_phys,
  925. (unsigned long) hose->io_base_virt);
  926. if (primary) {
  927. pci_io_base = (unsigned long)hose->io_base_virt;
  928. isa_dn = of_find_node_by_type(NULL, "isa");
  929. if (isa_dn) {
  930. isa_io_base = pci_io_base;
  931. pci_process_ISA_OF_ranges(isa_dn, hose->io_base_phys,
  932. hose->io_base_virt);
  933. of_node_put(isa_dn);
  934. /* Allow all IO */
  935. io_page_mask = -1;
  936. }
  937. }
  938. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  939. res = &hose->io_resource;
  940. res->start += io_virt_offset;
  941. res->end += io_virt_offset;
  942. }
  943. void __devinit pci_setup_phb_io_dynamic(struct pci_controller *hose,
  944. int primary)
  945. {
  946. unsigned long size = hose->pci_io_size;
  947. unsigned long io_virt_offset;
  948. struct resource *res;
  949. hose->io_base_virt = __ioremap(hose->io_base_phys, size,
  950. _PAGE_NO_CACHE | _PAGE_GUARDED);
  951. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  952. hose->global_number, hose->io_base_phys,
  953. (unsigned long) hose->io_base_virt);
  954. if (primary)
  955. pci_io_base = (unsigned long)hose->io_base_virt;
  956. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  957. res = &hose->io_resource;
  958. res->start += io_virt_offset;
  959. res->end += io_virt_offset;
  960. }
  961. static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
  962. unsigned long *start_virt, unsigned long *size)
  963. {
  964. struct pci_controller *hose = pci_bus_to_host(bus);
  965. struct pci_bus_region region;
  966. struct resource *res;
  967. if (bus->self) {
  968. res = bus->resource[0];
  969. pcibios_resource_to_bus(bus->self, &region, res);
  970. *start_phys = hose->io_base_phys + region.start;
  971. *start_virt = (unsigned long) hose->io_base_virt +
  972. region.start;
  973. if (region.end > region.start)
  974. *size = region.end - region.start + 1;
  975. else {
  976. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  977. __FUNCTION__, region.start, region.end);
  978. return 1;
  979. }
  980. } else {
  981. /* Root Bus */
  982. res = &hose->io_resource;
  983. *start_phys = hose->io_base_phys;
  984. *start_virt = (unsigned long) hose->io_base_virt;
  985. if (res->end > res->start)
  986. *size = res->end - res->start + 1;
  987. else {
  988. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  989. __FUNCTION__, res->start, res->end);
  990. return 1;
  991. }
  992. }
  993. return 0;
  994. }
  995. int unmap_bus_range(struct pci_bus *bus)
  996. {
  997. unsigned long start_phys;
  998. unsigned long start_virt;
  999. unsigned long size;
  1000. if (!bus) {
  1001. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  1002. return 1;
  1003. }
  1004. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  1005. return 1;
  1006. if (iounmap_explicit((void __iomem *) start_virt, size))
  1007. return 1;
  1008. return 0;
  1009. }
  1010. EXPORT_SYMBOL(unmap_bus_range);
  1011. int remap_bus_range(struct pci_bus *bus)
  1012. {
  1013. unsigned long start_phys;
  1014. unsigned long start_virt;
  1015. unsigned long size;
  1016. if (!bus) {
  1017. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  1018. return 1;
  1019. }
  1020. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  1021. return 1;
  1022. if (start_phys == 0)
  1023. return 1;
  1024. printk("mapping IO %lx -> %lx, size: %lx\n", start_phys, start_virt, size);
  1025. if (__ioremap_explicit(start_phys, start_virt, size,
  1026. _PAGE_NO_CACHE | _PAGE_GUARDED))
  1027. return 1;
  1028. return 0;
  1029. }
  1030. EXPORT_SYMBOL(remap_bus_range);
  1031. void phbs_remap_io(void)
  1032. {
  1033. struct pci_controller *hose, *tmp;
  1034. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1035. remap_bus_range(hose->bus);
  1036. }
  1037. static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
  1038. {
  1039. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1040. unsigned long start, end, mask, offset;
  1041. if (res->flags & IORESOURCE_IO) {
  1042. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  1043. start = res->start += offset;
  1044. end = res->end += offset;
  1045. /* Need to allow IO access to pages that are in the
  1046. ISA range */
  1047. if (start < MAX_ISA_PORT) {
  1048. if (end > MAX_ISA_PORT)
  1049. end = MAX_ISA_PORT;
  1050. start >>= PAGE_SHIFT;
  1051. end >>= PAGE_SHIFT;
  1052. /* get the range of pages for the map */
  1053. mask = ((1 << (end+1)) - 1) ^ ((1 << start) - 1);
  1054. io_page_mask |= mask;
  1055. }
  1056. } else if (res->flags & IORESOURCE_MEM) {
  1057. res->start += hose->pci_mem_offset;
  1058. res->end += hose->pci_mem_offset;
  1059. }
  1060. }
  1061. void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
  1062. struct pci_bus *bus)
  1063. {
  1064. /* Update device resources. */
  1065. int i;
  1066. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  1067. if (dev->resource[i].flags)
  1068. fixup_resource(&dev->resource[i], dev);
  1069. }
  1070. EXPORT_SYMBOL(pcibios_fixup_device_resources);
  1071. static void __devinit do_bus_setup(struct pci_bus *bus)
  1072. {
  1073. struct pci_dev *dev;
  1074. ppc_md.iommu_bus_setup(bus);
  1075. list_for_each_entry(dev, &bus->devices, bus_list)
  1076. ppc_md.iommu_dev_setup(dev);
  1077. if (ppc_md.irq_bus_setup)
  1078. ppc_md.irq_bus_setup(bus);
  1079. }
  1080. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  1081. {
  1082. struct pci_dev *dev = bus->self;
  1083. if (dev && pci_probe_only &&
  1084. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1085. /* This is a subordinate bridge */
  1086. pci_read_bridge_bases(bus);
  1087. pcibios_fixup_device_resources(dev, bus);
  1088. }
  1089. do_bus_setup(bus);
  1090. if (!pci_probe_only)
  1091. return;
  1092. list_for_each_entry(dev, &bus->devices, bus_list)
  1093. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1094. pcibios_fixup_device_resources(dev, bus);
  1095. }
  1096. EXPORT_SYMBOL(pcibios_fixup_bus);
  1097. /*
  1098. * Reads the interrupt pin to determine if interrupt is use by card.
  1099. * If the interrupt is used, then gets the interrupt line from the
  1100. * openfirmware and sets it in the pci_dev and pci_config line.
  1101. */
  1102. int pci_read_irq_line(struct pci_dev *pci_dev)
  1103. {
  1104. u8 intpin;
  1105. struct device_node *node;
  1106. pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &intpin);
  1107. if (intpin == 0)
  1108. return 0;
  1109. node = pci_device_to_OF_node(pci_dev);
  1110. if (node == NULL)
  1111. return -1;
  1112. if (node->n_intrs == 0)
  1113. return -1;
  1114. pci_dev->irq = node->intrs[0].line;
  1115. pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
  1116. return 0;
  1117. }
  1118. EXPORT_SYMBOL(pci_read_irq_line);
  1119. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1120. const struct resource *rsrc,
  1121. u64 *start, u64 *end)
  1122. {
  1123. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1124. unsigned long offset = 0;
  1125. if (hose == NULL)
  1126. return;
  1127. if (rsrc->flags & IORESOURCE_IO)
  1128. offset = pci_io_base - (unsigned long)hose->io_base_virt +
  1129. hose->io_base_phys;
  1130. *start = rsrc->start + offset;
  1131. *end = rsrc->end + offset;
  1132. }
  1133. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  1134. {
  1135. if (!have_of)
  1136. return NULL;
  1137. while(node) {
  1138. struct pci_controller *hose, *tmp;
  1139. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1140. if (hose->arch_data == node)
  1141. return hose;
  1142. node = node->parent;
  1143. }
  1144. return NULL;
  1145. }
  1146. #endif /* CONFIG_PPC_MULTIPLATFORM */
  1147. unsigned long pci_address_to_pio(phys_addr_t address)
  1148. {
  1149. struct pci_controller *hose, *tmp;
  1150. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1151. if (address >= hose->io_base_phys &&
  1152. address < (hose->io_base_phys + hose->pci_io_size)) {
  1153. unsigned long base =
  1154. (unsigned long)hose->io_base_virt - pci_io_base;
  1155. return base + (address - hose->io_base_phys);
  1156. }
  1157. }
  1158. return (unsigned int)-1;
  1159. }
  1160. EXPORT_SYMBOL_GPL(pci_address_to_pio);
  1161. #define IOBASE_BRIDGE_NUMBER 0
  1162. #define IOBASE_MEMORY 1
  1163. #define IOBASE_IO 2
  1164. #define IOBASE_ISA_IO 3
  1165. #define IOBASE_ISA_MEM 4
  1166. long sys_pciconfig_iobase(long which, unsigned long in_bus,
  1167. unsigned long in_devfn)
  1168. {
  1169. struct pci_controller* hose;
  1170. struct list_head *ln;
  1171. struct pci_bus *bus = NULL;
  1172. struct device_node *hose_node;
  1173. /* Argh ! Please forgive me for that hack, but that's the
  1174. * simplest way to get existing XFree to not lockup on some
  1175. * G5 machines... So when something asks for bus 0 io base
  1176. * (bus 0 is HT root), we return the AGP one instead.
  1177. */
  1178. if (machine_is_compatible("MacRISC4"))
  1179. if (in_bus == 0)
  1180. in_bus = 0xf0;
  1181. /* That syscall isn't quite compatible with PCI domains, but it's
  1182. * used on pre-domains setup. We return the first match
  1183. */
  1184. for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
  1185. bus = pci_bus_b(ln);
  1186. if (in_bus >= bus->number && in_bus < (bus->number + bus->subordinate))
  1187. break;
  1188. bus = NULL;
  1189. }
  1190. if (bus == NULL || bus->sysdata == NULL)
  1191. return -ENODEV;
  1192. hose_node = (struct device_node *)bus->sysdata;
  1193. hose = PCI_DN(hose_node)->phb;
  1194. switch (which) {
  1195. case IOBASE_BRIDGE_NUMBER:
  1196. return (long)hose->first_busno;
  1197. case IOBASE_MEMORY:
  1198. return (long)hose->pci_mem_offset;
  1199. case IOBASE_IO:
  1200. return (long)hose->io_base_phys;
  1201. case IOBASE_ISA_IO:
  1202. return (long)isa_io_base;
  1203. case IOBASE_ISA_MEM:
  1204. return -EINVAL;
  1205. }
  1206. return -EOPNOTSUPP;
  1207. }