pci.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
  4. * Copyright (C) 2003 Anton Blanchard <[email protected]>, IBM
  5. *
  6. * pSeries specific routines for PCI.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/ioport.h>
  10. #include <linux/kernel.h>
  11. #include <linux/pci.h>
  12. #include <linux/string.h>
  13. #include <asm/eeh.h>
  14. #include <asm/pci-bridge.h>
  15. #include <asm/ppc-pci.h>
  16. #include <asm/pci.h>
  17. #include "pseries.h"
  18. #if 0
  19. void pcibios_name_device(struct pci_dev *dev)
  20. {
  21. struct device_node *dn;
  22. /*
  23. * Add IBM loc code (slot) as a prefix to the device names for service
  24. */
  25. dn = pci_device_to_OF_node(dev);
  26. if (dn) {
  27. const char *loc_code = of_get_property(dn, "ibm,loc-code",
  28. NULL);
  29. if (loc_code) {
  30. int loc_len = strlen(loc_code);
  31. if (loc_len < sizeof(dev->dev.name)) {
  32. memmove(dev->dev.name+loc_len+1, dev->dev.name,
  33. sizeof(dev->dev.name)-loc_len-1);
  34. memcpy(dev->dev.name, loc_code, loc_len);
  35. dev->dev.name[loc_len] = ' ';
  36. dev->dev.name[sizeof(dev->dev.name)-1] = '\0';
  37. }
  38. }
  39. }
  40. }
  41. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_name_device);
  42. #endif
  43. #ifdef CONFIG_PCI_IOV
  44. #define MAX_VFS_FOR_MAP_PE 256
  45. struct pe_map_bar_entry {
  46. __be64 bar; /* Input: Virtual Function BAR */
  47. __be16 rid; /* Input: Virtual Function Router ID */
  48. __be16 pe_num; /* Output: Virtual Function PE Number */
  49. __be32 reserved; /* Reserved Space */
  50. };
  51. static int pseries_send_map_pe(struct pci_dev *pdev, u16 num_vfs,
  52. struct pe_map_bar_entry *vf_pe_array)
  53. {
  54. struct pci_dn *pdn;
  55. int rc;
  56. unsigned long buid, addr;
  57. int ibm_map_pes = rtas_token("ibm,open-sriov-map-pe-number");
  58. if (ibm_map_pes == RTAS_UNKNOWN_SERVICE)
  59. return -EINVAL;
  60. pdn = pci_get_pdn(pdev);
  61. addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
  62. buid = pdn->phb->buid;
  63. spin_lock(&rtas_data_buf_lock);
  64. memcpy(rtas_data_buf, vf_pe_array,
  65. RTAS_DATA_BUF_SIZE);
  66. rc = rtas_call(ibm_map_pes, 5, 1, NULL, addr,
  67. BUID_HI(buid), BUID_LO(buid),
  68. rtas_data_buf,
  69. num_vfs * sizeof(struct pe_map_bar_entry));
  70. memcpy(vf_pe_array, rtas_data_buf, RTAS_DATA_BUF_SIZE);
  71. spin_unlock(&rtas_data_buf_lock);
  72. if (rc)
  73. dev_err(&pdev->dev,
  74. "%s: Failed to associate pes PE#%lx, rc=%x\n",
  75. __func__, addr, rc);
  76. return rc;
  77. }
  78. static void pseries_set_pe_num(struct pci_dev *pdev, u16 vf_index, __be16 pe_num)
  79. {
  80. struct pci_dn *pdn;
  81. pdn = pci_get_pdn(pdev);
  82. pdn->pe_num_map[vf_index] = be16_to_cpu(pe_num);
  83. dev_dbg(&pdev->dev, "VF %04x:%02x:%02x.%x associated with PE#%x\n",
  84. pci_domain_nr(pdev->bus),
  85. pdev->bus->number,
  86. PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
  87. PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)),
  88. pdn->pe_num_map[vf_index]);
  89. }
  90. static int pseries_associate_pes(struct pci_dev *pdev, u16 num_vfs)
  91. {
  92. struct pci_dn *pdn;
  93. int i, rc, vf_index;
  94. struct pe_map_bar_entry *vf_pe_array;
  95. struct resource *res;
  96. u64 size;
  97. vf_pe_array = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  98. if (!vf_pe_array)
  99. return -ENOMEM;
  100. pdn = pci_get_pdn(pdev);
  101. /* create firmware structure to associate pes */
  102. for (vf_index = 0; vf_index < num_vfs; vf_index++) {
  103. pdn->pe_num_map[vf_index] = IODA_INVALID_PE;
  104. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  105. res = &pdev->resource[i + PCI_IOV_RESOURCES];
  106. if (!res->parent)
  107. continue;
  108. size = pcibios_iov_resource_alignment(pdev, i +
  109. PCI_IOV_RESOURCES);
  110. vf_pe_array[vf_index].bar =
  111. cpu_to_be64(res->start + size * vf_index);
  112. vf_pe_array[vf_index].rid =
  113. cpu_to_be16((pci_iov_virtfn_bus(pdev, vf_index)
  114. << 8) | pci_iov_virtfn_devfn(pdev,
  115. vf_index));
  116. vf_pe_array[vf_index].pe_num =
  117. cpu_to_be16(IODA_INVALID_PE);
  118. }
  119. }
  120. rc = pseries_send_map_pe(pdev, num_vfs, vf_pe_array);
  121. /* Only zero is success */
  122. if (!rc)
  123. for (vf_index = 0; vf_index < num_vfs; vf_index++)
  124. pseries_set_pe_num(pdev, vf_index,
  125. vf_pe_array[vf_index].pe_num);
  126. kfree(vf_pe_array);
  127. return rc;
  128. }
  129. static int pseries_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
  130. {
  131. struct pci_dn *pdn;
  132. int rc;
  133. const int *max_vfs;
  134. int max_config_vfs;
  135. struct device_node *dn = pci_device_to_OF_node(pdev);
  136. max_vfs = of_get_property(dn, "ibm,number-of-configurable-vfs", NULL);
  137. if (!max_vfs)
  138. return -EINVAL;
  139. /* First integer stores max config */
  140. max_config_vfs = of_read_number(&max_vfs[0], 1);
  141. if (max_config_vfs < num_vfs && num_vfs > MAX_VFS_FOR_MAP_PE) {
  142. dev_err(&pdev->dev,
  143. "Num VFs %x > %x Configurable VFs\n",
  144. num_vfs, (num_vfs > MAX_VFS_FOR_MAP_PE) ?
  145. MAX_VFS_FOR_MAP_PE : max_config_vfs);
  146. return -EINVAL;
  147. }
  148. pdn = pci_get_pdn(pdev);
  149. pdn->pe_num_map = kmalloc_array(num_vfs,
  150. sizeof(*pdn->pe_num_map),
  151. GFP_KERNEL);
  152. if (!pdn->pe_num_map)
  153. return -ENOMEM;
  154. rc = pseries_associate_pes(pdev, num_vfs);
  155. /* Anything other than zero is failure */
  156. if (rc) {
  157. dev_err(&pdev->dev, "Failure to enable sriov: %x\n", rc);
  158. kfree(pdn->pe_num_map);
  159. } else {
  160. pci_vf_drivers_autoprobe(pdev, false);
  161. }
  162. return rc;
  163. }
  164. static int pseries_pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
  165. {
  166. /* Allocate PCI data */
  167. add_sriov_vf_pdns(pdev);
  168. return pseries_pci_sriov_enable(pdev, num_vfs);
  169. }
  170. static int pseries_pcibios_sriov_disable(struct pci_dev *pdev)
  171. {
  172. struct pci_dn *pdn;
  173. pdn = pci_get_pdn(pdev);
  174. /* Releasing pe_num_map */
  175. kfree(pdn->pe_num_map);
  176. /* Release PCI data */
  177. remove_sriov_vf_pdns(pdev);
  178. pci_vf_drivers_autoprobe(pdev, true);
  179. return 0;
  180. }
  181. #endif
  182. static void __init pSeries_request_regions(void)
  183. {
  184. if (!isa_io_base)
  185. return;
  186. request_region(0x20,0x20,"pic1");
  187. request_region(0xa0,0x20,"pic2");
  188. request_region(0x00,0x20,"dma1");
  189. request_region(0x40,0x20,"timer");
  190. request_region(0x80,0x10,"dma page reg");
  191. request_region(0xc0,0x20,"dma2");
  192. }
  193. void __init pSeries_final_fixup(void)
  194. {
  195. pSeries_request_regions();
  196. eeh_show_enabled();
  197. #ifdef CONFIG_PCI_IOV
  198. ppc_md.pcibios_sriov_enable = pseries_pcibios_sriov_enable;
  199. ppc_md.pcibios_sriov_disable = pseries_pcibios_sriov_disable;
  200. #endif
  201. }
  202. /*
  203. * Assume the winbond 82c105 is the IDE controller on a
  204. * p610/p615/p630. We should probably be more careful in case
  205. * someone tries to plug in a similar adapter.
  206. */
  207. static void fixup_winbond_82c105(struct pci_dev* dev)
  208. {
  209. int i;
  210. unsigned int reg;
  211. if (!machine_is(pseries))
  212. return;
  213. printk("Using INTC for W82c105 IDE controller.\n");
  214. pci_read_config_dword(dev, 0x40, &reg);
  215. /* Enable LEGIRQ to use INTC instead of ISA interrupts */
  216. pci_write_config_dword(dev, 0x40, reg | (1<<11));
  217. for (i = 0; i < DEVICE_COUNT_RESOURCE; ++i) {
  218. /* zap the 2nd function of the winbond chip */
  219. if (dev->resource[i].flags & IORESOURCE_IO
  220. && dev->bus->number == 0 && dev->devfn == 0x81)
  221. dev->resource[i].flags &= ~IORESOURCE_IO;
  222. if (dev->resource[i].start == 0 && dev->resource[i].end) {
  223. dev->resource[i].flags = 0;
  224. dev->resource[i].end = 0;
  225. }
  226. }
  227. }
  228. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105,
  229. fixup_winbond_82c105);
  230. static enum pci_bus_speed prop_to_pci_speed(u32 prop)
  231. {
  232. switch (prop) {
  233. case 0x01:
  234. return PCIE_SPEED_2_5GT;
  235. case 0x02:
  236. return PCIE_SPEED_5_0GT;
  237. case 0x04:
  238. return PCIE_SPEED_8_0GT;
  239. case 0x08:
  240. return PCIE_SPEED_16_0GT;
  241. case 0x10:
  242. return PCIE_SPEED_32_0GT;
  243. default:
  244. pr_debug("Unexpected PCI link speed property value\n");
  245. return PCI_SPEED_UNKNOWN;
  246. }
  247. }
  248. int pseries_root_bridge_prepare(struct pci_host_bridge *bridge)
  249. {
  250. struct device_node *dn, *pdn;
  251. struct pci_bus *bus;
  252. u32 pcie_link_speed_stats[2];
  253. int rc;
  254. bus = bridge->bus;
  255. /* Rely on the pcibios_free_controller_deferred() callback. */
  256. pci_set_host_bridge_release(bridge, pcibios_free_controller_deferred,
  257. (void *) pci_bus_to_host(bus));
  258. dn = pcibios_get_phb_of_node(bus);
  259. if (!dn)
  260. return 0;
  261. for (pdn = dn; pdn != NULL; pdn = of_get_next_parent(pdn)) {
  262. rc = of_property_read_u32_array(pdn,
  263. "ibm,pcie-link-speed-stats",
  264. &pcie_link_speed_stats[0], 2);
  265. if (!rc)
  266. break;
  267. }
  268. of_node_put(pdn);
  269. if (rc) {
  270. pr_debug("no ibm,pcie-link-speed-stats property\n");
  271. return 0;
  272. }
  273. bus->max_bus_speed = prop_to_pci_speed(pcie_link_speed_stats[0]);
  274. bus->cur_bus_speed = prop_to_pci_speed(pcie_link_speed_stats[1]);
  275. return 0;
  276. }