pci-cxl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2014-2016 IBM Corp.
  4. */
  5. #include <linux/module.h>
  6. #include <misc/cxl-base.h>
  7. #include <asm/pnv-pci.h>
  8. #include <asm/opal.h>
  9. #include "pci.h"
  10. int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
  11. {
  12. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  13. struct pnv_phb *phb = hose->private_data;
  14. struct pnv_ioda_pe *pe;
  15. int rc;
  16. pe = pnv_ioda_get_pe(dev);
  17. if (!pe)
  18. return -ENODEV;
  19. pe_info(pe, "Switching PHB to CXL\n");
  20. rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
  21. if (rc == OPAL_UNSUPPORTED)
  22. dev_err(&dev->dev, "Required cxl mode not supported by firmware - update skiboot\n");
  23. else if (rc)
  24. dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
  25. return rc;
  26. }
  27. EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
  28. /* Find PHB for cxl dev and allocate MSI hwirqs?
  29. * Returns the absolute hardware IRQ number
  30. */
  31. int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
  32. {
  33. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  34. struct pnv_phb *phb = hose->private_data;
  35. int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
  36. if (hwirq < 0) {
  37. dev_warn(&dev->dev, "Failed to find a free MSI\n");
  38. return -ENOSPC;
  39. }
  40. return phb->msi_base + hwirq;
  41. }
  42. EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
  43. void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
  44. {
  45. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  46. struct pnv_phb *phb = hose->private_data;
  47. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
  48. }
  49. EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
  50. void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
  51. struct pci_dev *dev)
  52. {
  53. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  54. struct pnv_phb *phb = hose->private_data;
  55. int i, hwirq;
  56. for (i = 1; i < CXL_IRQ_RANGES; i++) {
  57. if (!irqs->range[i])
  58. continue;
  59. pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
  60. i, irqs->offset[i],
  61. irqs->range[i]);
  62. hwirq = irqs->offset[i] - phb->msi_base;
  63. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
  64. irqs->range[i]);
  65. }
  66. }
  67. EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
  68. int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
  69. struct pci_dev *dev, int num)
  70. {
  71. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  72. struct pnv_phb *phb = hose->private_data;
  73. int i, hwirq, try;
  74. memset(irqs, 0, sizeof(struct cxl_irq_ranges));
  75. /* 0 is reserved for the multiplexed PSL DSI interrupt */
  76. for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
  77. try = num;
  78. while (try) {
  79. hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
  80. if (hwirq >= 0)
  81. break;
  82. try /= 2;
  83. }
  84. if (!try)
  85. goto fail;
  86. irqs->offset[i] = phb->msi_base + hwirq;
  87. irqs->range[i] = try;
  88. pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
  89. i, irqs->offset[i], irqs->range[i]);
  90. num -= try;
  91. }
  92. if (num)
  93. goto fail;
  94. return 0;
  95. fail:
  96. pnv_cxl_release_hwirq_ranges(irqs, dev);
  97. return -ENOSPC;
  98. }
  99. EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
  100. int pnv_cxl_get_irq_count(struct pci_dev *dev)
  101. {
  102. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  103. struct pnv_phb *phb = hose->private_data;
  104. return phb->msi_bmp.irq_count;
  105. }
  106. EXPORT_SYMBOL(pnv_cxl_get_irq_count);
  107. int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
  108. unsigned int virq)
  109. {
  110. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  111. struct pnv_phb *phb = hose->private_data;
  112. unsigned int xive_num = hwirq - phb->msi_base;
  113. struct pnv_ioda_pe *pe;
  114. int rc;
  115. if (!(pe = pnv_ioda_get_pe(dev)))
  116. return -ENODEV;
  117. /* Assign XIVE to PE */
  118. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  119. if (rc) {
  120. pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
  121. "hwirq 0x%x XIVE 0x%x PE\n",
  122. pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
  123. return -EIO;
  124. }
  125. pnv_set_msi_irq_chip(phb, virq);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);