pci_64.c 37 KB

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