pci-common.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. /*
  2. * Contains common pci routines for ALL ppc platform
  3. * (based on pci_32.c and pci_64.c)
  4. *
  5. * Port for PPC64 David Engebretsen, IBM Corp.
  6. * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
  7. *
  8. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  9. * Rework, based on alpha PCI code.
  10. *
  11. * Common pmac/prep/chrp pci routines. -- Cort
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/pci.h>
  20. #include <linux/string.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/export.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_pci.h>
  26. #include <linux/mm.h>
  27. #include <linux/list.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/irq.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <asm/processor.h>
  34. #include <asm/io.h>
  35. #include <asm/prom.h>
  36. #include <asm/pci-bridge.h>
  37. #include <asm/byteorder.h>
  38. #include <asm/machdep.h>
  39. #include <asm/ppc-pci.h>
  40. #include <asm/eeh.h>
  41. /* hose_spinlock protects accesses to the the phb_bitmap. */
  42. static DEFINE_SPINLOCK(hose_spinlock);
  43. LIST_HEAD(hose_list);
  44. /* For dynamic PHB numbering on get_phb_number(): max number of PHBs. */
  45. #define MAX_PHBS 0x10000
  46. /*
  47. * For dynamic PHB numbering: used/free PHBs tracking bitmap.
  48. * Accesses to this bitmap should be protected by hose_spinlock.
  49. */
  50. static DECLARE_BITMAP(phb_bitmap, MAX_PHBS);
  51. /* ISA Memory physical address */
  52. resource_size_t isa_mem_base;
  53. static struct dma_map_ops *pci_dma_ops = &dma_direct_ops;
  54. void set_pci_dma_ops(struct dma_map_ops *dma_ops)
  55. {
  56. pci_dma_ops = dma_ops;
  57. }
  58. struct dma_map_ops *get_pci_dma_ops(void)
  59. {
  60. return pci_dma_ops;
  61. }
  62. EXPORT_SYMBOL(get_pci_dma_ops);
  63. /*
  64. * This function should run under locking protection, specifically
  65. * hose_spinlock.
  66. */
  67. static int get_phb_number(struct device_node *dn)
  68. {
  69. int ret, phb_id = -1;
  70. u32 prop_32;
  71. u64 prop;
  72. /*
  73. * Try fixed PHB numbering first, by checking archs and reading
  74. * the respective device-tree properties. Firstly, try powernv by
  75. * reading "ibm,opal-phbid", only present in OPAL environment.
  76. */
  77. ret = of_property_read_u64(dn, "ibm,opal-phbid", &prop);
  78. if (ret) {
  79. ret = of_property_read_u32_index(dn, "reg", 1, &prop_32);
  80. prop = prop_32;
  81. }
  82. if (!ret)
  83. phb_id = (int)(prop & (MAX_PHBS - 1));
  84. /* We need to be sure to not use the same PHB number twice. */
  85. if ((phb_id >= 0) && !test_and_set_bit(phb_id, phb_bitmap))
  86. return phb_id;
  87. /*
  88. * If not pseries nor powernv, or if fixed PHB numbering tried to add
  89. * the same PHB number twice, then fallback to dynamic PHB numbering.
  90. */
  91. phb_id = find_first_zero_bit(phb_bitmap, MAX_PHBS);
  92. BUG_ON(phb_id >= MAX_PHBS);
  93. set_bit(phb_id, phb_bitmap);
  94. return phb_id;
  95. }
  96. struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
  97. {
  98. struct pci_controller *phb;
  99. phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
  100. if (phb == NULL)
  101. return NULL;
  102. spin_lock(&hose_spinlock);
  103. phb->global_number = get_phb_number(dev);
  104. list_add_tail(&phb->list_node, &hose_list);
  105. spin_unlock(&hose_spinlock);
  106. phb->dn = dev;
  107. phb->is_dynamic = slab_is_available();
  108. #ifdef CONFIG_PPC64
  109. if (dev) {
  110. int nid = of_node_to_nid(dev);
  111. if (nid < 0 || !node_online(nid))
  112. nid = -1;
  113. PHB_SET_NODE(phb, nid);
  114. }
  115. #endif
  116. return phb;
  117. }
  118. EXPORT_SYMBOL_GPL(pcibios_alloc_controller);
  119. void pcibios_free_controller(struct pci_controller *phb)
  120. {
  121. spin_lock(&hose_spinlock);
  122. /* Clear bit of phb_bitmap to allow reuse of this PHB number. */
  123. if (phb->global_number < MAX_PHBS)
  124. clear_bit(phb->global_number, phb_bitmap);
  125. list_del(&phb->list_node);
  126. spin_unlock(&hose_spinlock);
  127. if (phb->is_dynamic)
  128. kfree(phb);
  129. }
  130. EXPORT_SYMBOL_GPL(pcibios_free_controller);
  131. /*
  132. * This function is used to call pcibios_free_controller()
  133. * in a deferred manner: a callback from the PCI subsystem.
  134. *
  135. * _*DO NOT*_ call pcibios_free_controller() explicitly if
  136. * this is used (or it may access an invalid *phb pointer).
  137. *
  138. * The callback occurs when all references to the root bus
  139. * are dropped (e.g., child buses/devices and their users).
  140. *
  141. * It's called as .release_fn() of 'struct pci_host_bridge'
  142. * which is associated with the 'struct pci_controller.bus'
  143. * (root bus) - it expects .release_data to hold a pointer
  144. * to 'struct pci_controller'.
  145. *
  146. * In order to use it, register .release_fn()/release_data
  147. * like this:
  148. *
  149. * pci_set_host_bridge_release(bridge,
  150. * pcibios_free_controller_deferred
  151. * (void *) phb);
  152. *
  153. * e.g. in the pcibios_root_bridge_prepare() callback from
  154. * pci_create_root_bus().
  155. */
  156. void pcibios_free_controller_deferred(struct pci_host_bridge *bridge)
  157. {
  158. struct pci_controller *phb = (struct pci_controller *)
  159. bridge->release_data;
  160. pr_debug("domain %d, dynamic %d\n", phb->global_number, phb->is_dynamic);
  161. pcibios_free_controller(phb);
  162. }
  163. EXPORT_SYMBOL_GPL(pcibios_free_controller_deferred);
  164. /*
  165. * The function is used to return the minimal alignment
  166. * for memory or I/O windows of the associated P2P bridge.
  167. * By default, 4KiB alignment for I/O windows and 1MiB for
  168. * memory windows.
  169. */
  170. resource_size_t pcibios_window_alignment(struct pci_bus *bus,
  171. unsigned long type)
  172. {
  173. struct pci_controller *phb = pci_bus_to_host(bus);
  174. if (phb->controller_ops.window_alignment)
  175. return phb->controller_ops.window_alignment(bus, type);
  176. /*
  177. * PCI core will figure out the default
  178. * alignment: 4KiB for I/O and 1MiB for
  179. * memory window.
  180. */
  181. return 1;
  182. }
  183. void pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
  184. {
  185. struct pci_controller *hose = pci_bus_to_host(bus);
  186. if (hose->controller_ops.setup_bridge)
  187. hose->controller_ops.setup_bridge(bus, type);
  188. }
  189. void pcibios_reset_secondary_bus(struct pci_dev *dev)
  190. {
  191. struct pci_controller *phb = pci_bus_to_host(dev->bus);
  192. if (phb->controller_ops.reset_secondary_bus) {
  193. phb->controller_ops.reset_secondary_bus(dev);
  194. return;
  195. }
  196. pci_reset_secondary_bus(dev);
  197. }
  198. #ifdef CONFIG_PCI_IOV
  199. resource_size_t pcibios_iov_resource_alignment(struct pci_dev *pdev, int resno)
  200. {
  201. if (ppc_md.pcibios_iov_resource_alignment)
  202. return ppc_md.pcibios_iov_resource_alignment(pdev, resno);
  203. return pci_iov_resource_size(pdev, resno);
  204. }
  205. #endif /* CONFIG_PCI_IOV */
  206. static resource_size_t pcibios_io_size(const struct pci_controller *hose)
  207. {
  208. #ifdef CONFIG_PPC64
  209. return hose->pci_io_size;
  210. #else
  211. return resource_size(&hose->io_resource);
  212. #endif
  213. }
  214. int pcibios_vaddr_is_ioport(void __iomem *address)
  215. {
  216. int ret = 0;
  217. struct pci_controller *hose;
  218. resource_size_t size;
  219. spin_lock(&hose_spinlock);
  220. list_for_each_entry(hose, &hose_list, list_node) {
  221. size = pcibios_io_size(hose);
  222. if (address >= hose->io_base_virt &&
  223. address < (hose->io_base_virt + size)) {
  224. ret = 1;
  225. break;
  226. }
  227. }
  228. spin_unlock(&hose_spinlock);
  229. return ret;
  230. }
  231. unsigned long pci_address_to_pio(phys_addr_t address)
  232. {
  233. struct pci_controller *hose;
  234. resource_size_t size;
  235. unsigned long ret = ~0;
  236. spin_lock(&hose_spinlock);
  237. list_for_each_entry(hose, &hose_list, list_node) {
  238. size = pcibios_io_size(hose);
  239. if (address >= hose->io_base_phys &&
  240. address < (hose->io_base_phys + size)) {
  241. unsigned long base =
  242. (unsigned long)hose->io_base_virt - _IO_BASE;
  243. ret = base + (address - hose->io_base_phys);
  244. break;
  245. }
  246. }
  247. spin_unlock(&hose_spinlock);
  248. return ret;
  249. }
  250. EXPORT_SYMBOL_GPL(pci_address_to_pio);
  251. /*
  252. * Return the domain number for this bus.
  253. */
  254. int pci_domain_nr(struct pci_bus *bus)
  255. {
  256. struct pci_controller *hose = pci_bus_to_host(bus);
  257. return hose->global_number;
  258. }
  259. EXPORT_SYMBOL(pci_domain_nr);
  260. /* This routine is meant to be used early during boot, when the
  261. * PCI bus numbers have not yet been assigned, and you need to
  262. * issue PCI config cycles to an OF device.
  263. * It could also be used to "fix" RTAS config cycles if you want
  264. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  265. * config cycles.
  266. */
  267. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  268. {
  269. while(node) {
  270. struct pci_controller *hose, *tmp;
  271. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  272. if (hose->dn == node)
  273. return hose;
  274. node = node->parent;
  275. }
  276. return NULL;
  277. }
  278. /*
  279. * Reads the interrupt pin to determine if interrupt is use by card.
  280. * If the interrupt is used, then gets the interrupt line from the
  281. * openfirmware and sets it in the pci_dev and pci_config line.
  282. */
  283. static int pci_read_irq_line(struct pci_dev *pci_dev)
  284. {
  285. struct of_phandle_args oirq;
  286. unsigned int virq;
  287. pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev));
  288. #ifdef DEBUG
  289. memset(&oirq, 0xff, sizeof(oirq));
  290. #endif
  291. /* Try to get a mapping from the device-tree */
  292. if (of_irq_parse_pci(pci_dev, &oirq)) {
  293. u8 line, pin;
  294. /* If that fails, lets fallback to what is in the config
  295. * space and map that through the default controller. We
  296. * also set the type to level low since that's what PCI
  297. * interrupts are. If your platform does differently, then
  298. * either provide a proper interrupt tree or don't use this
  299. * function.
  300. */
  301. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
  302. return -1;
  303. if (pin == 0)
  304. return -1;
  305. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
  306. line == 0xff || line == 0) {
  307. return -1;
  308. }
  309. pr_debug(" No map ! Using line %d (pin %d) from PCI config\n",
  310. line, pin);
  311. virq = irq_create_mapping(NULL, line);
  312. if (virq != NO_IRQ)
  313. irq_set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
  314. } else {
  315. pr_debug(" Got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
  316. oirq.args_count, oirq.args[0], oirq.args[1],
  317. of_node_full_name(oirq.np));
  318. virq = irq_create_of_mapping(&oirq);
  319. }
  320. if(virq == NO_IRQ) {
  321. pr_debug(" Failed to map !\n");
  322. return -1;
  323. }
  324. pr_debug(" Mapped to linux irq %d\n", virq);
  325. pci_dev->irq = virq;
  326. return 0;
  327. }
  328. /*
  329. * Platform support for /proc/bus/pci/X/Y mmap()s,
  330. * modelled on the sparc64 implementation by Dave Miller.
  331. * -- paulus.
  332. */
  333. /*
  334. * Adjust vm_pgoff of VMA such that it is the physical page offset
  335. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  336. *
  337. * Basically, the user finds the base address for his device which he wishes
  338. * to mmap. They read the 32-bit value from the config space base register,
  339. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  340. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  341. *
  342. * Returns negative error code on failure, zero on success.
  343. */
  344. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  345. resource_size_t *offset,
  346. enum pci_mmap_state mmap_state)
  347. {
  348. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  349. unsigned long io_offset = 0;
  350. int i, res_bit;
  351. if (hose == NULL)
  352. return NULL; /* should never happen */
  353. /* If memory, add on the PCI bridge address offset */
  354. if (mmap_state == pci_mmap_mem) {
  355. #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
  356. *offset += hose->pci_mem_offset;
  357. #endif
  358. res_bit = IORESOURCE_MEM;
  359. } else {
  360. io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  361. *offset += io_offset;
  362. res_bit = IORESOURCE_IO;
  363. }
  364. /*
  365. * Check that the offset requested corresponds to one of the
  366. * resources of the device.
  367. */
  368. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  369. struct resource *rp = &dev->resource[i];
  370. int flags = rp->flags;
  371. /* treat ROM as memory (should be already) */
  372. if (i == PCI_ROM_RESOURCE)
  373. flags |= IORESOURCE_MEM;
  374. /* Active and same type? */
  375. if ((flags & res_bit) == 0)
  376. continue;
  377. /* In the range of this resource? */
  378. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  379. continue;
  380. /* found it! construct the final physical address */
  381. if (mmap_state == pci_mmap_io)
  382. *offset += hose->io_base_phys - io_offset;
  383. return rp;
  384. }
  385. return NULL;
  386. }
  387. /*
  388. * This one is used by /dev/mem and fbdev who have no clue about the
  389. * PCI device, it tries to find the PCI device first and calls the
  390. * above routine
  391. */
  392. pgprot_t pci_phys_mem_access_prot(struct file *file,
  393. unsigned long pfn,
  394. unsigned long size,
  395. pgprot_t prot)
  396. {
  397. struct pci_dev *pdev = NULL;
  398. struct resource *found = NULL;
  399. resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
  400. int i;
  401. if (page_is_ram(pfn))
  402. return prot;
  403. prot = pgprot_noncached(prot);
  404. for_each_pci_dev(pdev) {
  405. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  406. struct resource *rp = &pdev->resource[i];
  407. int flags = rp->flags;
  408. /* Active and same type? */
  409. if ((flags & IORESOURCE_MEM) == 0)
  410. continue;
  411. /* In the range of this resource? */
  412. if (offset < (rp->start & PAGE_MASK) ||
  413. offset > rp->end)
  414. continue;
  415. found = rp;
  416. break;
  417. }
  418. if (found)
  419. break;
  420. }
  421. if (found) {
  422. if (found->flags & IORESOURCE_PREFETCH)
  423. prot = pgprot_noncached_wc(prot);
  424. pci_dev_put(pdev);
  425. }
  426. pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
  427. (unsigned long long)offset, pgprot_val(prot));
  428. return prot;
  429. }
  430. /*
  431. * Perform the actual remap of the pages for a PCI device mapping, as
  432. * appropriate for this architecture. The region in the process to map
  433. * is described by vm_start and vm_end members of VMA, the base physical
  434. * address is found in vm_pgoff.
  435. * The pci device structure is provided so that architectures may make mapping
  436. * decisions on a per-device or per-bus basis.
  437. *
  438. * Returns a negative error code on failure, zero on success.
  439. */
  440. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  441. enum pci_mmap_state mmap_state, int write_combine)
  442. {
  443. resource_size_t offset =
  444. ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
  445. struct resource *rp;
  446. int ret;
  447. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  448. if (rp == NULL)
  449. return -EINVAL;
  450. vma->vm_pgoff = offset >> PAGE_SHIFT;
  451. if (write_combine)
  452. vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
  453. else
  454. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  455. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  456. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  457. return ret;
  458. }
  459. /* This provides legacy IO read access on a bus */
  460. int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
  461. {
  462. unsigned long offset;
  463. struct pci_controller *hose = pci_bus_to_host(bus);
  464. struct resource *rp = &hose->io_resource;
  465. void __iomem *addr;
  466. /* Check if port can be supported by that bus. We only check
  467. * the ranges of the PHB though, not the bus itself as the rules
  468. * for forwarding legacy cycles down bridges are not our problem
  469. * here. So if the host bridge supports it, we do it.
  470. */
  471. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  472. offset += port;
  473. if (!(rp->flags & IORESOURCE_IO))
  474. return -ENXIO;
  475. if (offset < rp->start || (offset + size) > rp->end)
  476. return -ENXIO;
  477. addr = hose->io_base_virt + port;
  478. switch(size) {
  479. case 1:
  480. *((u8 *)val) = in_8(addr);
  481. return 1;
  482. case 2:
  483. if (port & 1)
  484. return -EINVAL;
  485. *((u16 *)val) = in_le16(addr);
  486. return 2;
  487. case 4:
  488. if (port & 3)
  489. return -EINVAL;
  490. *((u32 *)val) = in_le32(addr);
  491. return 4;
  492. }
  493. return -EINVAL;
  494. }
  495. /* This provides legacy IO write access on a bus */
  496. int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
  497. {
  498. unsigned long offset;
  499. struct pci_controller *hose = pci_bus_to_host(bus);
  500. struct resource *rp = &hose->io_resource;
  501. void __iomem *addr;
  502. /* Check if port can be supported by that bus. We only check
  503. * the ranges of the PHB though, not the bus itself as the rules
  504. * for forwarding legacy cycles down bridges are not our problem
  505. * here. So if the host bridge supports it, we do it.
  506. */
  507. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  508. offset += port;
  509. if (!(rp->flags & IORESOURCE_IO))
  510. return -ENXIO;
  511. if (offset < rp->start || (offset + size) > rp->end)
  512. return -ENXIO;
  513. addr = hose->io_base_virt + port;
  514. /* WARNING: The generic code is idiotic. It gets passed a pointer
  515. * to what can be a 1, 2 or 4 byte quantity and always reads that
  516. * as a u32, which means that we have to correct the location of
  517. * the data read within those 32 bits for size 1 and 2
  518. */
  519. switch(size) {
  520. case 1:
  521. out_8(addr, val >> 24);
  522. return 1;
  523. case 2:
  524. if (port & 1)
  525. return -EINVAL;
  526. out_le16(addr, val >> 16);
  527. return 2;
  528. case 4:
  529. if (port & 3)
  530. return -EINVAL;
  531. out_le32(addr, val);
  532. return 4;
  533. }
  534. return -EINVAL;
  535. }
  536. /* This provides legacy IO or memory mmap access on a bus */
  537. int pci_mmap_legacy_page_range(struct pci_bus *bus,
  538. struct vm_area_struct *vma,
  539. enum pci_mmap_state mmap_state)
  540. {
  541. struct pci_controller *hose = pci_bus_to_host(bus);
  542. resource_size_t offset =
  543. ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
  544. resource_size_t size = vma->vm_end - vma->vm_start;
  545. struct resource *rp;
  546. pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
  547. pci_domain_nr(bus), bus->number,
  548. mmap_state == pci_mmap_mem ? "MEM" : "IO",
  549. (unsigned long long)offset,
  550. (unsigned long long)(offset + size - 1));
  551. if (mmap_state == pci_mmap_mem) {
  552. /* Hack alert !
  553. *
  554. * Because X is lame and can fail starting if it gets an error trying
  555. * to mmap legacy_mem (instead of just moving on without legacy memory
  556. * access) we fake it here by giving it anonymous memory, effectively
  557. * behaving just like /dev/zero
  558. */
  559. if ((offset + size) > hose->isa_mem_size) {
  560. printk(KERN_DEBUG
  561. "Process %s (pid:%d) mapped non-existing PCI legacy memory for 0%04x:%02x\n",
  562. current->comm, current->pid, pci_domain_nr(bus), bus->number);
  563. if (vma->vm_flags & VM_SHARED)
  564. return shmem_zero_setup(vma);
  565. return 0;
  566. }
  567. offset += hose->isa_mem_phys;
  568. } else {
  569. unsigned long io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  570. unsigned long roffset = offset + io_offset;
  571. rp = &hose->io_resource;
  572. if (!(rp->flags & IORESOURCE_IO))
  573. return -ENXIO;
  574. if (roffset < rp->start || (roffset + size) > rp->end)
  575. return -ENXIO;
  576. offset += hose->io_base_phys;
  577. }
  578. pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
  579. vma->vm_pgoff = offset >> PAGE_SHIFT;
  580. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  581. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  582. vma->vm_end - vma->vm_start,
  583. vma->vm_page_prot);
  584. }
  585. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  586. const struct resource *rsrc,
  587. resource_size_t *start, resource_size_t *end)
  588. {
  589. struct pci_bus_region region;
  590. if (rsrc->flags & IORESOURCE_IO) {
  591. pcibios_resource_to_bus(dev->bus, &region,
  592. (struct resource *) rsrc);
  593. *start = region.start;
  594. *end = region.end;
  595. return;
  596. }
  597. /* We pass a CPU physical address to userland for MMIO instead of a
  598. * BAR value because X is lame and expects to be able to use that
  599. * to pass to /dev/mem!
  600. *
  601. * That means we may have 64-bit values where some apps only expect
  602. * 32 (like X itself since it thinks only Sparc has 64-bit MMIO).
  603. */
  604. *start = rsrc->start;
  605. *end = rsrc->end;
  606. }
  607. /**
  608. * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
  609. * @hose: newly allocated pci_controller to be setup
  610. * @dev: device node of the host bridge
  611. * @primary: set if primary bus (32 bits only, soon to be deprecated)
  612. *
  613. * This function will parse the "ranges" property of a PCI host bridge device
  614. * node and setup the resource mapping of a pci controller based on its
  615. * content.
  616. *
  617. * Life would be boring if it wasn't for a few issues that we have to deal
  618. * with here:
  619. *
  620. * - We can only cope with one IO space range and up to 3 Memory space
  621. * ranges. However, some machines (thanks Apple !) tend to split their
  622. * space into lots of small contiguous ranges. So we have to coalesce.
  623. *
  624. * - Some busses have IO space not starting at 0, which causes trouble with
  625. * the way we do our IO resource renumbering. The code somewhat deals with
  626. * it for 64 bits but I would expect problems on 32 bits.
  627. *
  628. * - Some 32 bits platforms such as 4xx can have physical space larger than
  629. * 32 bits so we need to use 64 bits values for the parsing
  630. */
  631. void pci_process_bridge_OF_ranges(struct pci_controller *hose,
  632. struct device_node *dev, int primary)
  633. {
  634. int memno = 0;
  635. struct resource *res;
  636. struct of_pci_range range;
  637. struct of_pci_range_parser parser;
  638. printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
  639. dev->full_name, primary ? "(primary)" : "");
  640. /* Check for ranges property */
  641. if (of_pci_range_parser_init(&parser, dev))
  642. return;
  643. /* Parse it */
  644. for_each_of_pci_range(&parser, &range) {
  645. /* If we failed translation or got a zero-sized region
  646. * (some FW try to feed us with non sensical zero sized regions
  647. * such as power3 which look like some kind of attempt at exposing
  648. * the VGA memory hole)
  649. */
  650. if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  651. continue;
  652. /* Act based on address space type */
  653. res = NULL;
  654. switch (range.flags & IORESOURCE_TYPE_BITS) {
  655. case IORESOURCE_IO:
  656. printk(KERN_INFO
  657. " IO 0x%016llx..0x%016llx -> 0x%016llx\n",
  658. range.cpu_addr, range.cpu_addr + range.size - 1,
  659. range.pci_addr);
  660. /* We support only one IO range */
  661. if (hose->pci_io_size) {
  662. printk(KERN_INFO
  663. " \\--> Skipped (too many) !\n");
  664. continue;
  665. }
  666. #ifdef CONFIG_PPC32
  667. /* On 32 bits, limit I/O space to 16MB */
  668. if (range.size > 0x01000000)
  669. range.size = 0x01000000;
  670. /* 32 bits needs to map IOs here */
  671. hose->io_base_virt = ioremap(range.cpu_addr,
  672. range.size);
  673. /* Expect trouble if pci_addr is not 0 */
  674. if (primary)
  675. isa_io_base =
  676. (unsigned long)hose->io_base_virt;
  677. #endif /* CONFIG_PPC32 */
  678. /* pci_io_size and io_base_phys always represent IO
  679. * space starting at 0 so we factor in pci_addr
  680. */
  681. hose->pci_io_size = range.pci_addr + range.size;
  682. hose->io_base_phys = range.cpu_addr - range.pci_addr;
  683. /* Build resource */
  684. res = &hose->io_resource;
  685. range.cpu_addr = range.pci_addr;
  686. break;
  687. case IORESOURCE_MEM:
  688. printk(KERN_INFO
  689. " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
  690. range.cpu_addr, range.cpu_addr + range.size - 1,
  691. range.pci_addr,
  692. (range.pci_space & 0x40000000) ?
  693. "Prefetch" : "");
  694. /* We support only 3 memory ranges */
  695. if (memno >= 3) {
  696. printk(KERN_INFO
  697. " \\--> Skipped (too many) !\n");
  698. continue;
  699. }
  700. /* Handles ISA memory hole space here */
  701. if (range.pci_addr == 0) {
  702. if (primary || isa_mem_base == 0)
  703. isa_mem_base = range.cpu_addr;
  704. hose->isa_mem_phys = range.cpu_addr;
  705. hose->isa_mem_size = range.size;
  706. }
  707. /* Build resource */
  708. hose->mem_offset[memno] = range.cpu_addr -
  709. range.pci_addr;
  710. res = &hose->mem_resources[memno++];
  711. break;
  712. }
  713. if (res != NULL) {
  714. res->name = dev->full_name;
  715. res->flags = range.flags;
  716. res->start = range.cpu_addr;
  717. res->end = range.cpu_addr + range.size - 1;
  718. res->parent = res->child = res->sibling = NULL;
  719. }
  720. }
  721. }
  722. /* Decide whether to display the domain number in /proc */
  723. int pci_proc_domain(struct pci_bus *bus)
  724. {
  725. struct pci_controller *hose = pci_bus_to_host(bus);
  726. if (!pci_has_flag(PCI_ENABLE_PROC_DOMAINS))
  727. return 0;
  728. if (pci_has_flag(PCI_COMPAT_DOMAIN_0))
  729. return hose->global_number != 0;
  730. return 1;
  731. }
  732. int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  733. {
  734. if (ppc_md.pcibios_root_bridge_prepare)
  735. return ppc_md.pcibios_root_bridge_prepare(bridge);
  736. return 0;
  737. }
  738. /* This header fixup will do the resource fixup for all devices as they are
  739. * probed, but not for bridge ranges
  740. */
  741. static void pcibios_fixup_resources(struct pci_dev *dev)
  742. {
  743. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  744. int i;
  745. if (!hose) {
  746. printk(KERN_ERR "No host bridge for PCI dev %s !\n",
  747. pci_name(dev));
  748. return;
  749. }
  750. if (dev->is_virtfn)
  751. return;
  752. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  753. struct resource *res = dev->resource + i;
  754. struct pci_bus_region reg;
  755. if (!res->flags)
  756. continue;
  757. /* If we're going to re-assign everything, we mark all resources
  758. * as unset (and 0-base them). In addition, we mark BARs starting
  759. * at 0 as unset as well, except if PCI_PROBE_ONLY is also set
  760. * since in that case, we don't want to re-assign anything
  761. */
  762. pcibios_resource_to_bus(dev->bus, &reg, res);
  763. if (pci_has_flag(PCI_REASSIGN_ALL_RSRC) ||
  764. (reg.start == 0 && !pci_has_flag(PCI_PROBE_ONLY))) {
  765. /* Only print message if not re-assigning */
  766. if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC))
  767. pr_debug("PCI:%s Resource %d %pR is unassigned\n",
  768. pci_name(dev), i, res);
  769. res->end -= res->start;
  770. res->start = 0;
  771. res->flags |= IORESOURCE_UNSET;
  772. continue;
  773. }
  774. pr_debug("PCI:%s Resource %d %pR\n", pci_name(dev), i, res);
  775. }
  776. /* Call machine specific resource fixup */
  777. if (ppc_md.pcibios_fixup_resources)
  778. ppc_md.pcibios_fixup_resources(dev);
  779. }
  780. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  781. /* This function tries to figure out if a bridge resource has been initialized
  782. * by the firmware or not. It doesn't have to be absolutely bullet proof, but
  783. * things go more smoothly when it gets it right. It should covers cases such
  784. * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
  785. */
  786. static int pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
  787. struct resource *res)
  788. {
  789. struct pci_controller *hose = pci_bus_to_host(bus);
  790. struct pci_dev *dev = bus->self;
  791. resource_size_t offset;
  792. struct pci_bus_region region;
  793. u16 command;
  794. int i;
  795. /* We don't do anything if PCI_PROBE_ONLY is set */
  796. if (pci_has_flag(PCI_PROBE_ONLY))
  797. return 0;
  798. /* Job is a bit different between memory and IO */
  799. if (res->flags & IORESOURCE_MEM) {
  800. pcibios_resource_to_bus(dev->bus, &region, res);
  801. /* If the BAR is non-0 then it's probably been initialized */
  802. if (region.start != 0)
  803. return 0;
  804. /* The BAR is 0, let's check if memory decoding is enabled on
  805. * the bridge. If not, we consider it unassigned
  806. */
  807. pci_read_config_word(dev, PCI_COMMAND, &command);
  808. if ((command & PCI_COMMAND_MEMORY) == 0)
  809. return 1;
  810. /* Memory decoding is enabled and the BAR is 0. If any of the bridge
  811. * resources covers that starting address (0 then it's good enough for
  812. * us for memory space)
  813. */
  814. for (i = 0; i < 3; i++) {
  815. if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
  816. hose->mem_resources[i].start == hose->mem_offset[i])
  817. return 0;
  818. }
  819. /* Well, it starts at 0 and we know it will collide so we may as
  820. * well consider it as unassigned. That covers the Apple case.
  821. */
  822. return 1;
  823. } else {
  824. /* If the BAR is non-0, then we consider it assigned */
  825. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  826. if (((res->start - offset) & 0xfffffffful) != 0)
  827. return 0;
  828. /* Here, we are a bit different than memory as typically IO space
  829. * starting at low addresses -is- valid. What we do instead if that
  830. * we consider as unassigned anything that doesn't have IO enabled
  831. * in the PCI command register, and that's it.
  832. */
  833. pci_read_config_word(dev, PCI_COMMAND, &command);
  834. if (command & PCI_COMMAND_IO)
  835. return 0;
  836. /* It's starting at 0 and IO is disabled in the bridge, consider
  837. * it unassigned
  838. */
  839. return 1;
  840. }
  841. }
  842. /* Fixup resources of a PCI<->PCI bridge */
  843. static void pcibios_fixup_bridge(struct pci_bus *bus)
  844. {
  845. struct resource *res;
  846. int i;
  847. struct pci_dev *dev = bus->self;
  848. pci_bus_for_each_resource(bus, res, i) {
  849. if (!res || !res->flags)
  850. continue;
  851. if (i >= 3 && bus->self->transparent)
  852. continue;
  853. /* If we're going to reassign everything, we can
  854. * shrink the P2P resource to have size as being
  855. * of 0 in order to save space.
  856. */
  857. if (pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
  858. res->flags |= IORESOURCE_UNSET;
  859. res->start = 0;
  860. res->end = -1;
  861. continue;
  862. }
  863. pr_debug("PCI:%s Bus rsrc %d %pR\n", pci_name(dev), i, res);
  864. /* Try to detect uninitialized P2P bridge resources,
  865. * and clear them out so they get re-assigned later
  866. */
  867. if (pcibios_uninitialized_bridge_resource(bus, res)) {
  868. res->flags = 0;
  869. pr_debug("PCI:%s (unassigned)\n", pci_name(dev));
  870. }
  871. }
  872. }
  873. void pcibios_setup_bus_self(struct pci_bus *bus)
  874. {
  875. struct pci_controller *phb;
  876. /* Fix up the bus resources for P2P bridges */
  877. if (bus->self != NULL)
  878. pcibios_fixup_bridge(bus);
  879. /* Platform specific bus fixups. This is currently only used
  880. * by fsl_pci and I'm hoping to get rid of it at some point
  881. */
  882. if (ppc_md.pcibios_fixup_bus)
  883. ppc_md.pcibios_fixup_bus(bus);
  884. /* Setup bus DMA mappings */
  885. phb = pci_bus_to_host(bus);
  886. if (phb->controller_ops.dma_bus_setup)
  887. phb->controller_ops.dma_bus_setup(bus);
  888. }
  889. static void pcibios_setup_device(struct pci_dev *dev)
  890. {
  891. struct pci_controller *phb;
  892. /* Fixup NUMA node as it may not be setup yet by the generic
  893. * code and is needed by the DMA init
  894. */
  895. set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
  896. /* Hook up default DMA ops */
  897. set_dma_ops(&dev->dev, pci_dma_ops);
  898. set_dma_offset(&dev->dev, PCI_DRAM_OFFSET);
  899. /* Additional platform DMA/iommu setup */
  900. phb = pci_bus_to_host(dev->bus);
  901. if (phb->controller_ops.dma_dev_setup)
  902. phb->controller_ops.dma_dev_setup(dev);
  903. /* Read default IRQs and fixup if necessary */
  904. pci_read_irq_line(dev);
  905. if (ppc_md.pci_irq_fixup)
  906. ppc_md.pci_irq_fixup(dev);
  907. }
  908. int pcibios_add_device(struct pci_dev *dev)
  909. {
  910. /*
  911. * We can only call pcibios_setup_device() after bus setup is complete,
  912. * since some of the platform specific DMA setup code depends on it.
  913. */
  914. if (dev->bus->is_added)
  915. pcibios_setup_device(dev);
  916. #ifdef CONFIG_PCI_IOV
  917. if (ppc_md.pcibios_fixup_sriov)
  918. ppc_md.pcibios_fixup_sriov(dev);
  919. #endif /* CONFIG_PCI_IOV */
  920. return 0;
  921. }
  922. void pcibios_setup_bus_devices(struct pci_bus *bus)
  923. {
  924. struct pci_dev *dev;
  925. pr_debug("PCI: Fixup bus devices %d (%s)\n",
  926. bus->number, bus->self ? pci_name(bus->self) : "PHB");
  927. list_for_each_entry(dev, &bus->devices, bus_list) {
  928. /* Cardbus can call us to add new devices to a bus, so ignore
  929. * those who are already fully discovered
  930. */
  931. if (dev->is_added)
  932. continue;
  933. pcibios_setup_device(dev);
  934. }
  935. }
  936. void pcibios_set_master(struct pci_dev *dev)
  937. {
  938. /* No special bus mastering setup handling */
  939. }
  940. void pcibios_fixup_bus(struct pci_bus *bus)
  941. {
  942. /* When called from the generic PCI probe, read PCI<->PCI bridge
  943. * bases. This is -not- called when generating the PCI tree from
  944. * the OF device-tree.
  945. */
  946. pci_read_bridge_bases(bus);
  947. /* Now fixup the bus bus */
  948. pcibios_setup_bus_self(bus);
  949. /* Now fixup devices on that bus */
  950. pcibios_setup_bus_devices(bus);
  951. }
  952. EXPORT_SYMBOL(pcibios_fixup_bus);
  953. void pci_fixup_cardbus(struct pci_bus *bus)
  954. {
  955. /* Now fixup devices on that bus */
  956. pcibios_setup_bus_devices(bus);
  957. }
  958. static int skip_isa_ioresource_align(struct pci_dev *dev)
  959. {
  960. if (pci_has_flag(PCI_CAN_SKIP_ISA_ALIGN) &&
  961. !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
  962. return 1;
  963. return 0;
  964. }
  965. /*
  966. * We need to avoid collisions with `mirrored' VGA ports
  967. * and other strange ISA hardware, so we always want the
  968. * addresses to be allocated in the 0x000-0x0ff region
  969. * modulo 0x400.
  970. *
  971. * Why? Because some silly external IO cards only decode
  972. * the low 10 bits of the IO address. The 0x00-0xff region
  973. * is reserved for motherboard devices that decode all 16
  974. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  975. * but we want to try to avoid allocating at 0x2900-0x2bff
  976. * which might have be mirrored at 0x0100-0x03ff..
  977. */
  978. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  979. resource_size_t size, resource_size_t align)
  980. {
  981. struct pci_dev *dev = data;
  982. resource_size_t start = res->start;
  983. if (res->flags & IORESOURCE_IO) {
  984. if (skip_isa_ioresource_align(dev))
  985. return start;
  986. if (start & 0x300)
  987. start = (start + 0x3ff) & ~0x3ff;
  988. }
  989. return start;
  990. }
  991. EXPORT_SYMBOL(pcibios_align_resource);
  992. /*
  993. * Reparent resource children of pr that conflict with res
  994. * under res, and make res replace those children.
  995. */
  996. static int reparent_resources(struct resource *parent,
  997. struct resource *res)
  998. {
  999. struct resource *p, **pp;
  1000. struct resource **firstpp = NULL;
  1001. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  1002. if (p->end < res->start)
  1003. continue;
  1004. if (res->end < p->start)
  1005. break;
  1006. if (p->start < res->start || p->end > res->end)
  1007. return -1; /* not completely contained */
  1008. if (firstpp == NULL)
  1009. firstpp = pp;
  1010. }
  1011. if (firstpp == NULL)
  1012. return -1; /* didn't find any conflicting entries? */
  1013. res->parent = parent;
  1014. res->child = *firstpp;
  1015. res->sibling = *pp;
  1016. *firstpp = res;
  1017. *pp = NULL;
  1018. for (p = res->child; p != NULL; p = p->sibling) {
  1019. p->parent = res;
  1020. pr_debug("PCI: Reparented %s %pR under %s\n",
  1021. p->name, p, res->name);
  1022. }
  1023. return 0;
  1024. }
  1025. /*
  1026. * Handle resources of PCI devices. If the world were perfect, we could
  1027. * just allocate all the resource regions and do nothing more. It isn't.
  1028. * On the other hand, we cannot just re-allocate all devices, as it would
  1029. * require us to know lots of host bridge internals. So we attempt to
  1030. * keep as much of the original configuration as possible, but tweak it
  1031. * when it's found to be wrong.
  1032. *
  1033. * Known BIOS problems we have to work around:
  1034. * - I/O or memory regions not configured
  1035. * - regions configured, but not enabled in the command register
  1036. * - bogus I/O addresses above 64K used
  1037. * - expansion ROMs left enabled (this may sound harmless, but given
  1038. * the fact the PCI specs explicitly allow address decoders to be
  1039. * shared between expansion ROMs and other resource regions, it's
  1040. * at least dangerous)
  1041. *
  1042. * Our solution:
  1043. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  1044. * This gives us fixed barriers on where we can allocate.
  1045. * (2) Allocate resources for all enabled devices. If there is
  1046. * a collision, just mark the resource as unallocated. Also
  1047. * disable expansion ROMs during this step.
  1048. * (3) Try to allocate resources for disabled devices. If the
  1049. * resources were assigned correctly, everything goes well,
  1050. * if they weren't, they won't disturb allocation of other
  1051. * resources.
  1052. * (4) Assign new addresses to resources which were either
  1053. * not configured at all or misconfigured. If explicitly
  1054. * requested by the user, configure expansion ROM address
  1055. * as well.
  1056. */
  1057. static void pcibios_allocate_bus_resources(struct pci_bus *bus)
  1058. {
  1059. struct pci_bus *b;
  1060. int i;
  1061. struct resource *res, *pr;
  1062. pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
  1063. pci_domain_nr(bus), bus->number);
  1064. pci_bus_for_each_resource(bus, res, i) {
  1065. if (!res || !res->flags || res->start > res->end || res->parent)
  1066. continue;
  1067. /* If the resource was left unset at this point, we clear it */
  1068. if (res->flags & IORESOURCE_UNSET)
  1069. goto clear_resource;
  1070. if (bus->parent == NULL)
  1071. pr = (res->flags & IORESOURCE_IO) ?
  1072. &ioport_resource : &iomem_resource;
  1073. else {
  1074. pr = pci_find_parent_resource(bus->self, res);
  1075. if (pr == res) {
  1076. /* this happens when the generic PCI
  1077. * code (wrongly) decides that this
  1078. * bridge is transparent -- paulus
  1079. */
  1080. continue;
  1081. }
  1082. }
  1083. pr_debug("PCI: %s (bus %d) bridge rsrc %d: %pR, parent %p (%s)\n",
  1084. bus->self ? pci_name(bus->self) : "PHB", bus->number,
  1085. i, res, pr, (pr && pr->name) ? pr->name : "nil");
  1086. if (pr && !(pr->flags & IORESOURCE_UNSET)) {
  1087. struct pci_dev *dev = bus->self;
  1088. if (request_resource(pr, res) == 0)
  1089. continue;
  1090. /*
  1091. * Must be a conflict with an existing entry.
  1092. * Move that entry (or entries) under the
  1093. * bridge resource and try again.
  1094. */
  1095. if (reparent_resources(pr, res) == 0)
  1096. continue;
  1097. if (dev && i < PCI_BRIDGE_RESOURCE_NUM &&
  1098. pci_claim_bridge_resource(dev,
  1099. i + PCI_BRIDGE_RESOURCES) == 0)
  1100. continue;
  1101. }
  1102. pr_warning("PCI: Cannot allocate resource region "
  1103. "%d of PCI bridge %d, will remap\n", i, bus->number);
  1104. clear_resource:
  1105. /* The resource might be figured out when doing
  1106. * reassignment based on the resources required
  1107. * by the downstream PCI devices. Here we set
  1108. * the size of the resource to be 0 in order to
  1109. * save more space.
  1110. */
  1111. res->start = 0;
  1112. res->end = -1;
  1113. res->flags = 0;
  1114. }
  1115. list_for_each_entry(b, &bus->children, node)
  1116. pcibios_allocate_bus_resources(b);
  1117. }
  1118. static inline void alloc_resource(struct pci_dev *dev, int idx)
  1119. {
  1120. struct resource *pr, *r = &dev->resource[idx];
  1121. pr_debug("PCI: Allocating %s: Resource %d: %pR\n",
  1122. pci_name(dev), idx, r);
  1123. pr = pci_find_parent_resource(dev, r);
  1124. if (!pr || (pr->flags & IORESOURCE_UNSET) ||
  1125. request_resource(pr, r) < 0) {
  1126. printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
  1127. " of device %s, will remap\n", idx, pci_name(dev));
  1128. if (pr)
  1129. pr_debug("PCI: parent is %p: %pR\n", pr, pr);
  1130. /* We'll assign a new address later */
  1131. r->flags |= IORESOURCE_UNSET;
  1132. r->end -= r->start;
  1133. r->start = 0;
  1134. }
  1135. }
  1136. static void __init pcibios_allocate_resources(int pass)
  1137. {
  1138. struct pci_dev *dev = NULL;
  1139. int idx, disabled;
  1140. u16 command;
  1141. struct resource *r;
  1142. for_each_pci_dev(dev) {
  1143. pci_read_config_word(dev, PCI_COMMAND, &command);
  1144. for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
  1145. r = &dev->resource[idx];
  1146. if (r->parent) /* Already allocated */
  1147. continue;
  1148. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  1149. continue; /* Not assigned at all */
  1150. /* We only allocate ROMs on pass 1 just in case they
  1151. * have been screwed up by firmware
  1152. */
  1153. if (idx == PCI_ROM_RESOURCE )
  1154. disabled = 1;
  1155. if (r->flags & IORESOURCE_IO)
  1156. disabled = !(command & PCI_COMMAND_IO);
  1157. else
  1158. disabled = !(command & PCI_COMMAND_MEMORY);
  1159. if (pass == disabled)
  1160. alloc_resource(dev, idx);
  1161. }
  1162. if (pass)
  1163. continue;
  1164. r = &dev->resource[PCI_ROM_RESOURCE];
  1165. if (r->flags) {
  1166. /* Turn the ROM off, leave the resource region,
  1167. * but keep it unregistered.
  1168. */
  1169. u32 reg;
  1170. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  1171. if (reg & PCI_ROM_ADDRESS_ENABLE) {
  1172. pr_debug("PCI: Switching off ROM of %s\n",
  1173. pci_name(dev));
  1174. r->flags &= ~IORESOURCE_ROM_ENABLE;
  1175. pci_write_config_dword(dev, dev->rom_base_reg,
  1176. reg & ~PCI_ROM_ADDRESS_ENABLE);
  1177. }
  1178. }
  1179. }
  1180. }
  1181. static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
  1182. {
  1183. struct pci_controller *hose = pci_bus_to_host(bus);
  1184. resource_size_t offset;
  1185. struct resource *res, *pres;
  1186. int i;
  1187. pr_debug("Reserving legacy ranges for domain %04x\n", pci_domain_nr(bus));
  1188. /* Check for IO */
  1189. if (!(hose->io_resource.flags & IORESOURCE_IO))
  1190. goto no_io;
  1191. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  1192. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  1193. BUG_ON(res == NULL);
  1194. res->name = "Legacy IO";
  1195. res->flags = IORESOURCE_IO;
  1196. res->start = offset;
  1197. res->end = (offset + 0xfff) & 0xfffffffful;
  1198. pr_debug("Candidate legacy IO: %pR\n", res);
  1199. if (request_resource(&hose->io_resource, res)) {
  1200. printk(KERN_DEBUG
  1201. "PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
  1202. pci_domain_nr(bus), bus->number, res);
  1203. kfree(res);
  1204. }
  1205. no_io:
  1206. /* Check for memory */
  1207. for (i = 0; i < 3; i++) {
  1208. pres = &hose->mem_resources[i];
  1209. offset = hose->mem_offset[i];
  1210. if (!(pres->flags & IORESOURCE_MEM))
  1211. continue;
  1212. pr_debug("hose mem res: %pR\n", pres);
  1213. if ((pres->start - offset) <= 0xa0000 &&
  1214. (pres->end - offset) >= 0xbffff)
  1215. break;
  1216. }
  1217. if (i >= 3)
  1218. return;
  1219. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  1220. BUG_ON(res == NULL);
  1221. res->name = "Legacy VGA memory";
  1222. res->flags = IORESOURCE_MEM;
  1223. res->start = 0xa0000 + offset;
  1224. res->end = 0xbffff + offset;
  1225. pr_debug("Candidate VGA memory: %pR\n", res);
  1226. if (request_resource(pres, res)) {
  1227. printk(KERN_DEBUG
  1228. "PCI %04x:%02x Cannot reserve VGA memory %pR\n",
  1229. pci_domain_nr(bus), bus->number, res);
  1230. kfree(res);
  1231. }
  1232. }
  1233. void __init pcibios_resource_survey(void)
  1234. {
  1235. struct pci_bus *b;
  1236. /* Allocate and assign resources */
  1237. list_for_each_entry(b, &pci_root_buses, node)
  1238. pcibios_allocate_bus_resources(b);
  1239. if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
  1240. pcibios_allocate_resources(0);
  1241. pcibios_allocate_resources(1);
  1242. }
  1243. /* Before we start assigning unassigned resource, we try to reserve
  1244. * the low IO area and the VGA memory area if they intersect the
  1245. * bus available resources to avoid allocating things on top of them
  1246. */
  1247. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  1248. list_for_each_entry(b, &pci_root_buses, node)
  1249. pcibios_reserve_legacy_regions(b);
  1250. }
  1251. /* Now, if the platform didn't decide to blindly trust the firmware,
  1252. * we proceed to assigning things that were left unassigned
  1253. */
  1254. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  1255. pr_debug("PCI: Assigning unassigned resources...\n");
  1256. pci_assign_unassigned_resources();
  1257. }
  1258. /* Call machine dependent fixup */
  1259. if (ppc_md.pcibios_fixup)
  1260. ppc_md.pcibios_fixup();
  1261. }
  1262. /* This is used by the PCI hotplug driver to allocate resource
  1263. * of newly plugged busses. We can try to consolidate with the
  1264. * rest of the code later, for now, keep it as-is as our main
  1265. * resource allocation function doesn't deal with sub-trees yet.
  1266. */
  1267. void pcibios_claim_one_bus(struct pci_bus *bus)
  1268. {
  1269. struct pci_dev *dev;
  1270. struct pci_bus *child_bus;
  1271. list_for_each_entry(dev, &bus->devices, bus_list) {
  1272. int i;
  1273. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  1274. struct resource *r = &dev->resource[i];
  1275. if (r->parent || !r->start || !r->flags)
  1276. continue;
  1277. pr_debug("PCI: Claiming %s: Resource %d: %pR\n",
  1278. pci_name(dev), i, r);
  1279. if (pci_claim_resource(dev, i) == 0)
  1280. continue;
  1281. pci_claim_bridge_resource(dev, i);
  1282. }
  1283. }
  1284. list_for_each_entry(child_bus, &bus->children, node)
  1285. pcibios_claim_one_bus(child_bus);
  1286. }
  1287. EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
  1288. /* pcibios_finish_adding_to_bus
  1289. *
  1290. * This is to be called by the hotplug code after devices have been
  1291. * added to a bus, this include calling it for a PHB that is just
  1292. * being added
  1293. */
  1294. void pcibios_finish_adding_to_bus(struct pci_bus *bus)
  1295. {
  1296. pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
  1297. pci_domain_nr(bus), bus->number);
  1298. /* Allocate bus and devices resources */
  1299. pcibios_allocate_bus_resources(bus);
  1300. pcibios_claim_one_bus(bus);
  1301. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  1302. if (bus->self)
  1303. pci_assign_unassigned_bridge_resources(bus->self);
  1304. else
  1305. pci_assign_unassigned_bus_resources(bus);
  1306. }
  1307. /* Fixup EEH */
  1308. eeh_add_device_tree_late(bus);
  1309. /* Add new devices to global lists. Register in proc, sysfs. */
  1310. pci_bus_add_devices(bus);
  1311. /* sysfs files should only be added after devices are added */
  1312. eeh_add_sysfs_files(bus);
  1313. }
  1314. EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
  1315. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1316. {
  1317. struct pci_controller *phb = pci_bus_to_host(dev->bus);
  1318. if (phb->controller_ops.enable_device_hook)
  1319. if (!phb->controller_ops.enable_device_hook(dev))
  1320. return -EINVAL;
  1321. return pci_enable_resources(dev, mask);
  1322. }
  1323. void pcibios_disable_device(struct pci_dev *dev)
  1324. {
  1325. struct pci_controller *phb = pci_bus_to_host(dev->bus);
  1326. if (phb->controller_ops.disable_device)
  1327. phb->controller_ops.disable_device(dev);
  1328. }
  1329. resource_size_t pcibios_io_space_offset(struct pci_controller *hose)
  1330. {
  1331. return (unsigned long) hose->io_base_virt - _IO_BASE;
  1332. }
  1333. static void pcibios_setup_phb_resources(struct pci_controller *hose,
  1334. struct list_head *resources)
  1335. {
  1336. struct resource *res;
  1337. resource_size_t offset;
  1338. int i;
  1339. /* Hookup PHB IO resource */
  1340. res = &hose->io_resource;
  1341. if (!res->flags) {
  1342. pr_debug("PCI: I/O resource not set for host"
  1343. " bridge %s (domain %d)\n",
  1344. hose->dn->full_name, hose->global_number);
  1345. } else {
  1346. offset = pcibios_io_space_offset(hose);
  1347. pr_debug("PCI: PHB IO resource = %pR off 0x%08llx\n",
  1348. res, (unsigned long long)offset);
  1349. pci_add_resource_offset(resources, res, offset);
  1350. }
  1351. /* Hookup PHB Memory resources */
  1352. for (i = 0; i < 3; ++i) {
  1353. res = &hose->mem_resources[i];
  1354. if (!res->flags) {
  1355. if (i == 0)
  1356. printk(KERN_ERR "PCI: Memory resource 0 not set for "
  1357. "host bridge %s (domain %d)\n",
  1358. hose->dn->full_name, hose->global_number);
  1359. continue;
  1360. }
  1361. offset = hose->mem_offset[i];
  1362. pr_debug("PCI: PHB MEM resource %d = %pR off 0x%08llx\n", i,
  1363. res, (unsigned long long)offset);
  1364. pci_add_resource_offset(resources, res, offset);
  1365. }
  1366. }
  1367. /*
  1368. * Null PCI config access functions, for the case when we can't
  1369. * find a hose.
  1370. */
  1371. #define NULL_PCI_OP(rw, size, type) \
  1372. static int \
  1373. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1374. { \
  1375. return PCIBIOS_DEVICE_NOT_FOUND; \
  1376. }
  1377. static int
  1378. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1379. int len, u32 *val)
  1380. {
  1381. return PCIBIOS_DEVICE_NOT_FOUND;
  1382. }
  1383. static int
  1384. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1385. int len, u32 val)
  1386. {
  1387. return PCIBIOS_DEVICE_NOT_FOUND;
  1388. }
  1389. static struct pci_ops null_pci_ops =
  1390. {
  1391. .read = null_read_config,
  1392. .write = null_write_config,
  1393. };
  1394. /*
  1395. * These functions are used early on before PCI scanning is done
  1396. * and all of the pci_dev and pci_bus structures have been created.
  1397. */
  1398. static struct pci_bus *
  1399. fake_pci_bus(struct pci_controller *hose, int busnr)
  1400. {
  1401. static struct pci_bus bus;
  1402. if (hose == NULL) {
  1403. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1404. }
  1405. bus.number = busnr;
  1406. bus.sysdata = hose;
  1407. bus.ops = hose? hose->ops: &null_pci_ops;
  1408. return &bus;
  1409. }
  1410. #define EARLY_PCI_OP(rw, size, type) \
  1411. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1412. int devfn, int offset, type value) \
  1413. { \
  1414. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1415. devfn, offset, value); \
  1416. }
  1417. EARLY_PCI_OP(read, byte, u8 *)
  1418. EARLY_PCI_OP(read, word, u16 *)
  1419. EARLY_PCI_OP(read, dword, u32 *)
  1420. EARLY_PCI_OP(write, byte, u8)
  1421. EARLY_PCI_OP(write, word, u16)
  1422. EARLY_PCI_OP(write, dword, u32)
  1423. int early_find_capability(struct pci_controller *hose, int bus, int devfn,
  1424. int cap)
  1425. {
  1426. return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
  1427. }
  1428. struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
  1429. {
  1430. struct pci_controller *hose = bus->sysdata;
  1431. return of_node_get(hose->dn);
  1432. }
  1433. /**
  1434. * pci_scan_phb - Given a pci_controller, setup and scan the PCI bus
  1435. * @hose: Pointer to the PCI host controller instance structure
  1436. */
  1437. void pcibios_scan_phb(struct pci_controller *hose)
  1438. {
  1439. LIST_HEAD(resources);
  1440. struct pci_bus *bus;
  1441. struct device_node *node = hose->dn;
  1442. int mode;
  1443. pr_debug("PCI: Scanning PHB %s\n", of_node_full_name(node));
  1444. /* Get some IO space for the new PHB */
  1445. pcibios_setup_phb_io_space(hose);
  1446. /* Wire up PHB bus resources */
  1447. pcibios_setup_phb_resources(hose, &resources);
  1448. hose->busn.start = hose->first_busno;
  1449. hose->busn.end = hose->last_busno;
  1450. hose->busn.flags = IORESOURCE_BUS;
  1451. pci_add_resource(&resources, &hose->busn);
  1452. /* Create an empty bus for the toplevel */
  1453. bus = pci_create_root_bus(hose->parent, hose->first_busno,
  1454. hose->ops, hose, &resources);
  1455. if (bus == NULL) {
  1456. pr_err("Failed to create bus for PCI domain %04x\n",
  1457. hose->global_number);
  1458. pci_free_resource_list(&resources);
  1459. return;
  1460. }
  1461. hose->bus = bus;
  1462. /* Get probe mode and perform scan */
  1463. mode = PCI_PROBE_NORMAL;
  1464. if (node && hose->controller_ops.probe_mode)
  1465. mode = hose->controller_ops.probe_mode(bus);
  1466. pr_debug(" probe mode: %d\n", mode);
  1467. if (mode == PCI_PROBE_DEVTREE)
  1468. of_scan_bus(node, bus);
  1469. if (mode == PCI_PROBE_NORMAL) {
  1470. pci_bus_update_busn_res_end(bus, 255);
  1471. hose->last_busno = pci_scan_child_bus(bus);
  1472. pci_bus_update_busn_res_end(bus, hose->last_busno);
  1473. }
  1474. /* Platform gets a chance to do some global fixups before
  1475. * we proceed to resource allocation
  1476. */
  1477. if (ppc_md.pcibios_fixup_phb)
  1478. ppc_md.pcibios_fixup_phb(hose);
  1479. /* Configure PCI Express settings */
  1480. if (bus && !pci_has_flag(PCI_PROBE_ONLY)) {
  1481. struct pci_bus *child;
  1482. list_for_each_entry(child, &bus->children, node)
  1483. pcie_bus_configure_settings(child);
  1484. }
  1485. }
  1486. EXPORT_SYMBOL_GPL(pcibios_scan_phb);
  1487. static void fixup_hide_host_resource_fsl(struct pci_dev *dev)
  1488. {
  1489. int i, class = dev->class >> 8;
  1490. /* When configured as agent, programing interface = 1 */
  1491. int prog_if = dev->class & 0xf;
  1492. if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
  1493. class == PCI_CLASS_BRIDGE_OTHER) &&
  1494. (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
  1495. (prog_if == 0) &&
  1496. (dev->bus->parent == NULL)) {
  1497. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1498. dev->resource[i].start = 0;
  1499. dev->resource[i].end = 0;
  1500. dev->resource[i].flags = 0;
  1501. }
  1502. }
  1503. }
  1504. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
  1505. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
  1506. static void fixup_vga(struct pci_dev *pdev)
  1507. {
  1508. u16 cmd;
  1509. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  1510. if ((cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) || !vga_default_device())
  1511. vga_set_default_device(pdev);
  1512. }
  1513. DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_ANY_ID, PCI_ANY_ID,
  1514. PCI_CLASS_DISPLAY_VGA, 8, fixup_vga);