pci_64.c 38 KB

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