pci_64.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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. }
  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. pci_bus_add_devices(hose->bus);
  518. }
  519. #ifndef CONFIG_PPC_ISERIES
  520. if (pci_probe_only)
  521. pcibios_claim_of_setup();
  522. else
  523. /* FIXME: `else' will be removed when
  524. pci_assign_unassigned_resources() is able to work
  525. correctly with [partially] allocated PCI tree. */
  526. pci_assign_unassigned_resources();
  527. #endif /* !CONFIG_PPC_ISERIES */
  528. /* Call machine dependent final fixup */
  529. if (ppc_md.pcibios_fixup)
  530. ppc_md.pcibios_fixup();
  531. /* Cache the location of the ISA bridge (if we have one) */
  532. ppc64_isabridge_dev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
  533. if (ppc64_isabridge_dev != NULL)
  534. printk("ISA bridge at %s\n", pci_name(ppc64_isabridge_dev));
  535. #ifdef CONFIG_PPC_MULTIPLATFORM
  536. /* map in PCI I/O space */
  537. phbs_remap_io();
  538. #endif
  539. printk("PCI: Probing PCI hardware done\n");
  540. return 0;
  541. }
  542. subsys_initcall(pcibios_init);
  543. char __init *pcibios_setup(char *str)
  544. {
  545. return str;
  546. }
  547. int pcibios_enable_device(struct pci_dev *dev, int mask)
  548. {
  549. u16 cmd, oldcmd;
  550. int i;
  551. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  552. oldcmd = cmd;
  553. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  554. struct resource *res = &dev->resource[i];
  555. /* Only set up the requested stuff */
  556. if (!(mask & (1<<i)))
  557. continue;
  558. if (res->flags & IORESOURCE_IO)
  559. cmd |= PCI_COMMAND_IO;
  560. if (res->flags & IORESOURCE_MEM)
  561. cmd |= PCI_COMMAND_MEMORY;
  562. }
  563. if (cmd != oldcmd) {
  564. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  565. pci_name(dev), cmd);
  566. /* Enable the appropriate bits in the PCI command register. */
  567. pci_write_config_word(dev, PCI_COMMAND, cmd);
  568. }
  569. return 0;
  570. }
  571. /*
  572. * Return the domain number for this bus.
  573. */
  574. int pci_domain_nr(struct pci_bus *bus)
  575. {
  576. #ifdef CONFIG_PPC_ISERIES
  577. return 0;
  578. #else
  579. struct pci_controller *hose = pci_bus_to_host(bus);
  580. return hose->global_number;
  581. #endif
  582. }
  583. EXPORT_SYMBOL(pci_domain_nr);
  584. /* Decide whether to display the domain number in /proc */
  585. int pci_proc_domain(struct pci_bus *bus)
  586. {
  587. #ifdef CONFIG_PPC_ISERIES
  588. return 0;
  589. #else
  590. struct pci_controller *hose = pci_bus_to_host(bus);
  591. return hose->buid;
  592. #endif
  593. }
  594. /*
  595. * Platform support for /proc/bus/pci/X/Y mmap()s,
  596. * modelled on the sparc64 implementation by Dave Miller.
  597. * -- paulus.
  598. */
  599. /*
  600. * Adjust vm_pgoff of VMA such that it is the physical page offset
  601. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  602. *
  603. * Basically, the user finds the base address for his device which he wishes
  604. * to mmap. They read the 32-bit value from the config space base register,
  605. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  606. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  607. *
  608. * Returns negative error code on failure, zero on success.
  609. */
  610. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  611. unsigned long *offset,
  612. enum pci_mmap_state mmap_state)
  613. {
  614. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  615. unsigned long io_offset = 0;
  616. int i, res_bit;
  617. if (hose == 0)
  618. return NULL; /* should never happen */
  619. /* If memory, add on the PCI bridge address offset */
  620. if (mmap_state == pci_mmap_mem) {
  621. *offset += hose->pci_mem_offset;
  622. res_bit = IORESOURCE_MEM;
  623. } else {
  624. io_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  625. *offset += io_offset;
  626. res_bit = IORESOURCE_IO;
  627. }
  628. /*
  629. * Check that the offset requested corresponds to one of the
  630. * resources of the device.
  631. */
  632. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  633. struct resource *rp = &dev->resource[i];
  634. int flags = rp->flags;
  635. /* treat ROM as memory (should be already) */
  636. if (i == PCI_ROM_RESOURCE)
  637. flags |= IORESOURCE_MEM;
  638. /* Active and same type? */
  639. if ((flags & res_bit) == 0)
  640. continue;
  641. /* In the range of this resource? */
  642. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  643. continue;
  644. /* found it! construct the final physical address */
  645. if (mmap_state == pci_mmap_io)
  646. *offset += hose->io_base_phys - io_offset;
  647. return rp;
  648. }
  649. return NULL;
  650. }
  651. /*
  652. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  653. * device mapping.
  654. */
  655. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  656. pgprot_t protection,
  657. enum pci_mmap_state mmap_state,
  658. int write_combine)
  659. {
  660. unsigned long prot = pgprot_val(protection);
  661. /* Write combine is always 0 on non-memory space mappings. On
  662. * memory space, if the user didn't pass 1, we check for a
  663. * "prefetchable" resource. This is a bit hackish, but we use
  664. * this to workaround the inability of /sysfs to provide a write
  665. * combine bit
  666. */
  667. if (mmap_state != pci_mmap_mem)
  668. write_combine = 0;
  669. else if (write_combine == 0) {
  670. if (rp->flags & IORESOURCE_PREFETCH)
  671. write_combine = 1;
  672. }
  673. /* XXX would be nice to have a way to ask for write-through */
  674. prot |= _PAGE_NO_CACHE;
  675. if (write_combine)
  676. prot &= ~_PAGE_GUARDED;
  677. else
  678. prot |= _PAGE_GUARDED;
  679. printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
  680. prot);
  681. return __pgprot(prot);
  682. }
  683. /*
  684. * This one is used by /dev/mem and fbdev who have no clue about the
  685. * PCI device, it tries to find the PCI device first and calls the
  686. * above routine
  687. */
  688. pgprot_t pci_phys_mem_access_prot(struct file *file,
  689. unsigned long pfn,
  690. unsigned long size,
  691. pgprot_t protection)
  692. {
  693. struct pci_dev *pdev = NULL;
  694. struct resource *found = NULL;
  695. unsigned long prot = pgprot_val(protection);
  696. unsigned long offset = pfn << PAGE_SHIFT;
  697. int i;
  698. if (page_is_ram(pfn))
  699. return __pgprot(prot);
  700. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  701. for_each_pci_dev(pdev) {
  702. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  703. struct resource *rp = &pdev->resource[i];
  704. int flags = rp->flags;
  705. /* Active and same type? */
  706. if ((flags & IORESOURCE_MEM) == 0)
  707. continue;
  708. /* In the range of this resource? */
  709. if (offset < (rp->start & PAGE_MASK) ||
  710. offset > rp->end)
  711. continue;
  712. found = rp;
  713. break;
  714. }
  715. if (found)
  716. break;
  717. }
  718. if (found) {
  719. if (found->flags & IORESOURCE_PREFETCH)
  720. prot &= ~_PAGE_GUARDED;
  721. pci_dev_put(pdev);
  722. }
  723. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  724. return __pgprot(prot);
  725. }
  726. /*
  727. * Perform the actual remap of the pages for a PCI device mapping, as
  728. * appropriate for this architecture. The region in the process to map
  729. * is described by vm_start and vm_end members of VMA, the base physical
  730. * address is found in vm_pgoff.
  731. * The pci device structure is provided so that architectures may make mapping
  732. * decisions on a per-device or per-bus basis.
  733. *
  734. * Returns a negative error code on failure, zero on success.
  735. */
  736. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  737. enum pci_mmap_state mmap_state, int write_combine)
  738. {
  739. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  740. struct resource *rp;
  741. int ret;
  742. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  743. if (rp == NULL)
  744. return -EINVAL;
  745. vma->vm_pgoff = offset >> PAGE_SHIFT;
  746. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  747. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  748. vma->vm_page_prot,
  749. mmap_state, write_combine);
  750. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  751. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  752. return ret;
  753. }
  754. #ifdef CONFIG_PPC_MULTIPLATFORM
  755. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  756. {
  757. struct pci_dev *pdev;
  758. struct device_node *np;
  759. pdev = to_pci_dev (dev);
  760. np = pci_device_to_OF_node(pdev);
  761. if (np == NULL || np->full_name == NULL)
  762. return 0;
  763. return sprintf(buf, "%s", np->full_name);
  764. }
  765. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  766. #endif /* CONFIG_PPC_MULTIPLATFORM */
  767. void pcibios_add_platform_entries(struct pci_dev *pdev)
  768. {
  769. #ifdef CONFIG_PPC_MULTIPLATFORM
  770. device_create_file(&pdev->dev, &dev_attr_devspec);
  771. #endif /* CONFIG_PPC_MULTIPLATFORM */
  772. }
  773. #ifdef CONFIG_PPC_MULTIPLATFORM
  774. #define ISA_SPACE_MASK 0x1
  775. #define ISA_SPACE_IO 0x1
  776. static void __devinit pci_process_ISA_OF_ranges(struct device_node *isa_node,
  777. unsigned long phb_io_base_phys,
  778. void __iomem * phb_io_base_virt)
  779. {
  780. /* Remove these asap */
  781. struct pci_address {
  782. u32 a_hi;
  783. u32 a_mid;
  784. u32 a_lo;
  785. };
  786. struct isa_address {
  787. u32 a_hi;
  788. u32 a_lo;
  789. };
  790. struct isa_range {
  791. struct isa_address isa_addr;
  792. struct pci_address pci_addr;
  793. unsigned int size;
  794. };
  795. struct isa_range *range;
  796. unsigned long pci_addr;
  797. unsigned int isa_addr;
  798. unsigned int size;
  799. int rlen = 0;
  800. range = (struct isa_range *) get_property(isa_node, "ranges", &rlen);
  801. if (range == NULL || (rlen < sizeof(struct isa_range))) {
  802. printk(KERN_ERR "no ISA ranges or unexpected isa range size,"
  803. "mapping 64k\n");
  804. __ioremap_explicit(phb_io_base_phys,
  805. (unsigned long)phb_io_base_virt,
  806. 0x10000, _PAGE_NO_CACHE | _PAGE_GUARDED);
  807. return;
  808. }
  809. /* From "ISA Binding to 1275"
  810. * The ranges property is laid out as an array of elements,
  811. * each of which comprises:
  812. * cells 0 - 1: an ISA address
  813. * cells 2 - 4: a PCI address
  814. * (size depending on dev->n_addr_cells)
  815. * cell 5: the size of the range
  816. */
  817. if ((range->isa_addr.a_hi && ISA_SPACE_MASK) == ISA_SPACE_IO) {
  818. isa_addr = range->isa_addr.a_lo;
  819. pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
  820. range->pci_addr.a_lo;
  821. /* Assume these are both zero */
  822. if ((pci_addr != 0) || (isa_addr != 0)) {
  823. printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
  824. __FUNCTION__);
  825. return;
  826. }
  827. size = PAGE_ALIGN(range->size);
  828. __ioremap_explicit(phb_io_base_phys,
  829. (unsigned long) phb_io_base_virt,
  830. size, _PAGE_NO_CACHE | _PAGE_GUARDED);
  831. }
  832. }
  833. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  834. struct device_node *dev, int prim)
  835. {
  836. unsigned int *ranges, pci_space;
  837. unsigned long size;
  838. int rlen = 0;
  839. int memno = 0;
  840. struct resource *res;
  841. int np, na = prom_n_addr_cells(dev);
  842. unsigned long pci_addr, cpu_phys_addr;
  843. np = na + 5;
  844. /* From "PCI Binding to 1275"
  845. * The ranges property is laid out as an array of elements,
  846. * each of which comprises:
  847. * cells 0 - 2: a PCI address
  848. * cells 3 or 3+4: a CPU physical address
  849. * (size depending on dev->n_addr_cells)
  850. * cells 4+5 or 5+6: the size of the range
  851. */
  852. ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
  853. if (ranges == NULL)
  854. return;
  855. hose->io_base_phys = 0;
  856. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  857. res = NULL;
  858. pci_space = ranges[0];
  859. pci_addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  860. cpu_phys_addr = ranges[3];
  861. if (na >= 2)
  862. cpu_phys_addr = (cpu_phys_addr << 32) | ranges[4];
  863. size = ((unsigned long)ranges[na+3] << 32) | ranges[na+4];
  864. ranges += np;
  865. if (size == 0)
  866. continue;
  867. /* Now consume following elements while they are contiguous */
  868. while (rlen >= np * sizeof(unsigned int)) {
  869. unsigned long addr, phys;
  870. if (ranges[0] != pci_space)
  871. break;
  872. addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  873. phys = ranges[3];
  874. if (na >= 2)
  875. phys = (phys << 32) | ranges[4];
  876. if (addr != pci_addr + size ||
  877. phys != cpu_phys_addr + size)
  878. break;
  879. size += ((unsigned long)ranges[na+3] << 32)
  880. | ranges[na+4];
  881. ranges += np;
  882. rlen -= np * sizeof(unsigned int);
  883. }
  884. switch ((pci_space >> 24) & 0x3) {
  885. case 1: /* I/O space */
  886. hose->io_base_phys = cpu_phys_addr;
  887. hose->pci_io_size = size;
  888. res = &hose->io_resource;
  889. res->flags = IORESOURCE_IO;
  890. res->start = pci_addr;
  891. DBG("phb%d: IO 0x%lx -> 0x%lx\n", hose->global_number,
  892. res->start, res->start + size - 1);
  893. break;
  894. case 2: /* memory space */
  895. memno = 0;
  896. while (memno < 3 && hose->mem_resources[memno].flags)
  897. ++memno;
  898. if (memno == 0)
  899. hose->pci_mem_offset = cpu_phys_addr - pci_addr;
  900. if (memno < 3) {
  901. res = &hose->mem_resources[memno];
  902. res->flags = IORESOURCE_MEM;
  903. res->start = cpu_phys_addr;
  904. DBG("phb%d: MEM 0x%lx -> 0x%lx\n", hose->global_number,
  905. res->start, res->start + size - 1);
  906. }
  907. break;
  908. }
  909. if (res != NULL) {
  910. res->name = dev->full_name;
  911. res->end = res->start + size - 1;
  912. res->parent = NULL;
  913. res->sibling = NULL;
  914. res->child = NULL;
  915. }
  916. }
  917. }
  918. void __init pci_setup_phb_io(struct pci_controller *hose, int primary)
  919. {
  920. unsigned long size = hose->pci_io_size;
  921. unsigned long io_virt_offset;
  922. struct resource *res;
  923. struct device_node *isa_dn;
  924. hose->io_base_virt = reserve_phb_iospace(size);
  925. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  926. hose->global_number, hose->io_base_phys,
  927. (unsigned long) hose->io_base_virt);
  928. if (primary) {
  929. pci_io_base = (unsigned long)hose->io_base_virt;
  930. isa_dn = of_find_node_by_type(NULL, "isa");
  931. if (isa_dn) {
  932. isa_io_base = pci_io_base;
  933. pci_process_ISA_OF_ranges(isa_dn, hose->io_base_phys,
  934. hose->io_base_virt);
  935. of_node_put(isa_dn);
  936. /* Allow all IO */
  937. io_page_mask = -1;
  938. }
  939. }
  940. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  941. res = &hose->io_resource;
  942. res->start += io_virt_offset;
  943. res->end += io_virt_offset;
  944. }
  945. void __devinit pci_setup_phb_io_dynamic(struct pci_controller *hose,
  946. int primary)
  947. {
  948. unsigned long size = hose->pci_io_size;
  949. unsigned long io_virt_offset;
  950. struct resource *res;
  951. hose->io_base_virt = __ioremap(hose->io_base_phys, size,
  952. _PAGE_NO_CACHE | _PAGE_GUARDED);
  953. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  954. hose->global_number, hose->io_base_phys,
  955. (unsigned long) hose->io_base_virt);
  956. if (primary)
  957. pci_io_base = (unsigned long)hose->io_base_virt;
  958. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  959. res = &hose->io_resource;
  960. res->start += io_virt_offset;
  961. res->end += io_virt_offset;
  962. }
  963. static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
  964. unsigned long *start_virt, unsigned long *size)
  965. {
  966. struct pci_controller *hose = pci_bus_to_host(bus);
  967. struct pci_bus_region region;
  968. struct resource *res;
  969. if (bus->self) {
  970. res = bus->resource[0];
  971. pcibios_resource_to_bus(bus->self, &region, res);
  972. *start_phys = hose->io_base_phys + region.start;
  973. *start_virt = (unsigned long) hose->io_base_virt +
  974. region.start;
  975. if (region.end > region.start)
  976. *size = region.end - region.start + 1;
  977. else {
  978. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  979. __FUNCTION__, region.start, region.end);
  980. return 1;
  981. }
  982. } else {
  983. /* Root Bus */
  984. res = &hose->io_resource;
  985. *start_phys = hose->io_base_phys;
  986. *start_virt = (unsigned long) hose->io_base_virt;
  987. if (res->end > res->start)
  988. *size = res->end - res->start + 1;
  989. else {
  990. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  991. __FUNCTION__, res->start, res->end);
  992. return 1;
  993. }
  994. }
  995. return 0;
  996. }
  997. int unmap_bus_range(struct pci_bus *bus)
  998. {
  999. unsigned long start_phys;
  1000. unsigned long start_virt;
  1001. unsigned long size;
  1002. if (!bus) {
  1003. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  1004. return 1;
  1005. }
  1006. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  1007. return 1;
  1008. if (iounmap_explicit((void __iomem *) start_virt, size))
  1009. return 1;
  1010. return 0;
  1011. }
  1012. EXPORT_SYMBOL(unmap_bus_range);
  1013. int remap_bus_range(struct pci_bus *bus)
  1014. {
  1015. unsigned long start_phys;
  1016. unsigned long start_virt;
  1017. unsigned long size;
  1018. if (!bus) {
  1019. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  1020. return 1;
  1021. }
  1022. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  1023. return 1;
  1024. if (start_phys == 0)
  1025. return 1;
  1026. printk("mapping IO %lx -> %lx, size: %lx\n", start_phys, start_virt, size);
  1027. if (__ioremap_explicit(start_phys, start_virt, size,
  1028. _PAGE_NO_CACHE | _PAGE_GUARDED))
  1029. return 1;
  1030. return 0;
  1031. }
  1032. EXPORT_SYMBOL(remap_bus_range);
  1033. static void phbs_remap_io(void)
  1034. {
  1035. struct pci_controller *hose, *tmp;
  1036. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1037. remap_bus_range(hose->bus);
  1038. }
  1039. static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
  1040. {
  1041. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1042. unsigned long start, end, mask, offset;
  1043. if (res->flags & IORESOURCE_IO) {
  1044. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  1045. start = res->start += offset;
  1046. end = res->end += offset;
  1047. /* Need to allow IO access to pages that are in the
  1048. ISA range */
  1049. if (start < MAX_ISA_PORT) {
  1050. if (end > MAX_ISA_PORT)
  1051. end = MAX_ISA_PORT;
  1052. start >>= PAGE_SHIFT;
  1053. end >>= PAGE_SHIFT;
  1054. /* get the range of pages for the map */
  1055. mask = ((1 << (end+1)) - 1) ^ ((1 << start) - 1);
  1056. io_page_mask |= mask;
  1057. }
  1058. } else if (res->flags & IORESOURCE_MEM) {
  1059. res->start += hose->pci_mem_offset;
  1060. res->end += hose->pci_mem_offset;
  1061. }
  1062. }
  1063. void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
  1064. struct pci_bus *bus)
  1065. {
  1066. /* Update device resources. */
  1067. int i;
  1068. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  1069. if (dev->resource[i].flags)
  1070. fixup_resource(&dev->resource[i], dev);
  1071. }
  1072. EXPORT_SYMBOL(pcibios_fixup_device_resources);
  1073. static void __devinit do_bus_setup(struct pci_bus *bus)
  1074. {
  1075. struct pci_dev *dev;
  1076. ppc_md.iommu_bus_setup(bus);
  1077. list_for_each_entry(dev, &bus->devices, bus_list)
  1078. ppc_md.iommu_dev_setup(dev);
  1079. if (ppc_md.irq_bus_setup)
  1080. ppc_md.irq_bus_setup(bus);
  1081. }
  1082. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  1083. {
  1084. struct pci_dev *dev = bus->self;
  1085. if (dev && pci_probe_only &&
  1086. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1087. /* This is a subordinate bridge */
  1088. pci_read_bridge_bases(bus);
  1089. pcibios_fixup_device_resources(dev, bus);
  1090. }
  1091. do_bus_setup(bus);
  1092. if (!pci_probe_only)
  1093. return;
  1094. list_for_each_entry(dev, &bus->devices, bus_list)
  1095. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1096. pcibios_fixup_device_resources(dev, bus);
  1097. }
  1098. EXPORT_SYMBOL(pcibios_fixup_bus);
  1099. /*
  1100. * Reads the interrupt pin to determine if interrupt is use by card.
  1101. * If the interrupt is used, then gets the interrupt line from the
  1102. * openfirmware and sets it in the pci_dev and pci_config line.
  1103. */
  1104. int pci_read_irq_line(struct pci_dev *pci_dev)
  1105. {
  1106. u8 intpin;
  1107. struct device_node *node;
  1108. pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &intpin);
  1109. if (intpin == 0)
  1110. return 0;
  1111. node = pci_device_to_OF_node(pci_dev);
  1112. if (node == NULL)
  1113. return -1;
  1114. if (node->n_intrs == 0)
  1115. return -1;
  1116. pci_dev->irq = node->intrs[0].line;
  1117. pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
  1118. return 0;
  1119. }
  1120. EXPORT_SYMBOL(pci_read_irq_line);
  1121. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1122. const struct resource *rsrc,
  1123. u64 *start, u64 *end)
  1124. {
  1125. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1126. unsigned long offset = 0;
  1127. if (hose == NULL)
  1128. return;
  1129. if (rsrc->flags & IORESOURCE_IO)
  1130. offset = pci_io_base - (unsigned long)hose->io_base_virt +
  1131. hose->io_base_phys;
  1132. *start = rsrc->start + offset;
  1133. *end = rsrc->end + offset;
  1134. }
  1135. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  1136. {
  1137. if (!have_of)
  1138. return NULL;
  1139. while(node) {
  1140. struct pci_controller *hose, *tmp;
  1141. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1142. if (hose->arch_data == node)
  1143. return hose;
  1144. node = node->parent;
  1145. }
  1146. return NULL;
  1147. }
  1148. #endif /* CONFIG_PPC_MULTIPLATFORM */
  1149. unsigned long pci_address_to_pio(phys_addr_t address)
  1150. {
  1151. struct pci_controller *hose, *tmp;
  1152. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1153. if (address >= hose->io_base_phys &&
  1154. address < (hose->io_base_phys + hose->pci_io_size)) {
  1155. unsigned long base =
  1156. (unsigned long)hose->io_base_virt - pci_io_base;
  1157. return base + (address - hose->io_base_phys);
  1158. }
  1159. }
  1160. return (unsigned int)-1;
  1161. }
  1162. EXPORT_SYMBOL_GPL(pci_address_to_pio);
  1163. #define IOBASE_BRIDGE_NUMBER 0
  1164. #define IOBASE_MEMORY 1
  1165. #define IOBASE_IO 2
  1166. #define IOBASE_ISA_IO 3
  1167. #define IOBASE_ISA_MEM 4
  1168. long sys_pciconfig_iobase(long which, unsigned long in_bus,
  1169. unsigned long in_devfn)
  1170. {
  1171. struct pci_controller* hose;
  1172. struct list_head *ln;
  1173. struct pci_bus *bus = NULL;
  1174. struct device_node *hose_node;
  1175. /* Argh ! Please forgive me for that hack, but that's the
  1176. * simplest way to get existing XFree to not lockup on some
  1177. * G5 machines... So when something asks for bus 0 io base
  1178. * (bus 0 is HT root), we return the AGP one instead.
  1179. */
  1180. if (machine_is_compatible("MacRISC4"))
  1181. if (in_bus == 0)
  1182. in_bus = 0xf0;
  1183. /* That syscall isn't quite compatible with PCI domains, but it's
  1184. * used on pre-domains setup. We return the first match
  1185. */
  1186. for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
  1187. bus = pci_bus_b(ln);
  1188. if (in_bus >= bus->number && in_bus < (bus->number + bus->subordinate))
  1189. break;
  1190. bus = NULL;
  1191. }
  1192. if (bus == NULL || bus->sysdata == NULL)
  1193. return -ENODEV;
  1194. hose_node = (struct device_node *)bus->sysdata;
  1195. hose = PCI_DN(hose_node)->phb;
  1196. switch (which) {
  1197. case IOBASE_BRIDGE_NUMBER:
  1198. return (long)hose->first_busno;
  1199. case IOBASE_MEMORY:
  1200. return (long)hose->pci_mem_offset;
  1201. case IOBASE_IO:
  1202. return (long)hose->io_base_phys;
  1203. case IOBASE_ISA_IO:
  1204. return (long)isa_io_base;
  1205. case IOBASE_ISA_MEM:
  1206. return -EINVAL;
  1207. }
  1208. return -EOPNOTSUPP;
  1209. }