pci-common.c 48 KB

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