|
7 | 7 | * 2 of the License, or (at your option) any later version. |
8 | 8 | */ |
9 | 9 |
|
| 10 | +#include <linux/module.h> |
| 11 | +#include <asm/pci-bridge.h> |
10 | 12 | #include <asm/pnv-pci.h> |
11 | 13 | #include <asm/opal.h> |
| 14 | +#include <misc/cxl.h> |
12 | 15 |
|
13 | 16 | #include "pci.h" |
14 | 17 |
|
@@ -161,3 +164,120 @@ int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq, |
161 | 164 | return 0; |
162 | 165 | } |
163 | 166 | EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup); |
| 167 | + |
| 168 | +/* |
| 169 | + * Sets flags and switches the controller ops to enable the cxl kernel api. |
| 170 | + * Originally the cxl kernel API operated on a virtual PHB, but certain cards |
| 171 | + * such as the Mellanox CX4 use a peer model instead and for these cards the |
| 172 | + * cxl kernel api will operate on the real PHB. |
| 173 | + */ |
| 174 | +int pnv_cxl_enable_phb_kernel_api(struct pci_controller *hose, bool enable) |
| 175 | +{ |
| 176 | + struct pnv_phb *phb = hose->private_data; |
| 177 | + struct module *cxl_module; |
| 178 | + |
| 179 | + if (!enable) { |
| 180 | + /* |
| 181 | + * Once cxl mode is enabled on the PHB, there is currently no |
| 182 | + * known safe method to disable it again, and trying risks a |
| 183 | + * checkstop. If we can find a way to safely disable cxl mode |
| 184 | + * in the future we can revisit this, but for now the only sane |
| 185 | + * thing to do is to refuse to disable cxl mode: |
| 186 | + */ |
| 187 | + return -EPERM; |
| 188 | + } |
| 189 | + |
| 190 | + /* |
| 191 | + * Hold a reference to the cxl module since several PHB operations now |
| 192 | + * depend on it, and it would be insane to allow it to be removed so |
| 193 | + * long as we are in this mode (and since we can't safely disable this |
| 194 | + * mode once enabled...). |
| 195 | + */ |
| 196 | + mutex_lock(&module_mutex); |
| 197 | + cxl_module = find_module("cxl"); |
| 198 | + if (cxl_module) |
| 199 | + __module_get(cxl_module); |
| 200 | + mutex_unlock(&module_mutex); |
| 201 | + if (!cxl_module) |
| 202 | + return -ENODEV; |
| 203 | + |
| 204 | + phb->flags |= PNV_PHB_FLAG_CXL; |
| 205 | + hose->controller_ops = pnv_cxl_cx4_ioda_controller_ops; |
| 206 | + |
| 207 | + return 0; |
| 208 | +} |
| 209 | +EXPORT_SYMBOL_GPL(pnv_cxl_enable_phb_kernel_api); |
| 210 | + |
| 211 | +bool pnv_pci_on_cxl_phb(struct pci_dev *dev) |
| 212 | +{ |
| 213 | + struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 214 | + struct pnv_phb *phb = hose->private_data; |
| 215 | + |
| 216 | + return !!(phb->flags & PNV_PHB_FLAG_CXL); |
| 217 | +} |
| 218 | +EXPORT_SYMBOL_GPL(pnv_pci_on_cxl_phb); |
| 219 | + |
| 220 | +struct cxl_afu *pnv_cxl_phb_to_afu(struct pci_controller *hose) |
| 221 | +{ |
| 222 | + struct pnv_phb *phb = hose->private_data; |
| 223 | + |
| 224 | + return (struct cxl_afu *)phb->cxl_afu; |
| 225 | +} |
| 226 | +EXPORT_SYMBOL_GPL(pnv_cxl_phb_to_afu); |
| 227 | + |
| 228 | +void pnv_cxl_phb_set_peer_afu(struct pci_dev *dev, struct cxl_afu *afu) |
| 229 | +{ |
| 230 | + struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 231 | + struct pnv_phb *phb = hose->private_data; |
| 232 | + |
| 233 | + phb->cxl_afu = afu; |
| 234 | +} |
| 235 | +EXPORT_SYMBOL_GPL(pnv_cxl_phb_set_peer_afu); |
| 236 | + |
| 237 | +/* |
| 238 | + * In the peer cxl model, the XSL/PSL is physical function 0, and will be used |
| 239 | + * by other functions on the device for memory access and interrupts. When the |
| 240 | + * other functions are enabled we explicitly take a reference on the cxl |
| 241 | + * function since they will use it, and allocate a default context associated |
| 242 | + * with that function just like the vPHB model of the cxl kernel API. |
| 243 | + */ |
| 244 | +bool pnv_cxl_enable_device_hook(struct pci_dev *dev) |
| 245 | +{ |
| 246 | + struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 247 | + struct pnv_phb *phb = hose->private_data; |
| 248 | + struct cxl_afu *afu = phb->cxl_afu; |
| 249 | + |
| 250 | + if (!pnv_pci_enable_device_hook(dev)) |
| 251 | + return false; |
| 252 | + |
| 253 | + |
| 254 | + /* No special handling for the cxl function, which is always PF 0 */ |
| 255 | + if (PCI_FUNC(dev->devfn) == 0) |
| 256 | + return true; |
| 257 | + |
| 258 | + if (!afu) { |
| 259 | + dev_WARN(&dev->dev, "Attempted to enable function > 0 on CXL PHB without a peer AFU\n"); |
| 260 | + return false; |
| 261 | + } |
| 262 | + |
| 263 | + dev_info(&dev->dev, "Enabling function on CXL enabled PHB with peer AFU\n"); |
| 264 | + |
| 265 | + /* Make sure the peer AFU can't go away while this device is active */ |
| 266 | + cxl_afu_get(afu); |
| 267 | + |
| 268 | + return cxl_pci_associate_default_context(dev, afu); |
| 269 | +} |
| 270 | + |
| 271 | +void pnv_cxl_disable_device(struct pci_dev *dev) |
| 272 | +{ |
| 273 | + struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 274 | + struct pnv_phb *phb = hose->private_data; |
| 275 | + struct cxl_afu *afu = phb->cxl_afu; |
| 276 | + |
| 277 | + /* No special handling for cxl function: */ |
| 278 | + if (PCI_FUNC(dev->devfn) == 0) |
| 279 | + return; |
| 280 | + |
| 281 | + cxl_pci_disable_device(dev); |
| 282 | + cxl_afu_put(afu); |
| 283 | +} |
0 commit comments