cxl: Add support for interrupts on the Mellanox CX4
[linux-2.6-block.git] / arch / powerpc / platforms / powernv / pci-cxl.c
1 /*
2  * Copyright 2014-2016 IBM Corp.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  */
9
10 #include <linux/module.h>
11 #include <linux/msi.h>
12 #include <asm/pci-bridge.h>
13 #include <asm/pnv-pci.h>
14 #include <asm/opal.h>
15 #include <misc/cxl.h>
16
17 #include "pci.h"
18
19 struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
20 {
21         struct pci_controller *hose = pci_bus_to_host(dev->bus);
22
23         return of_node_get(hose->dn);
24 }
25 EXPORT_SYMBOL(pnv_pci_get_phb_node);
26
27 int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
28 {
29         struct pci_controller *hose = pci_bus_to_host(dev->bus);
30         struct pnv_phb *phb = hose->private_data;
31         struct pnv_ioda_pe *pe;
32         int rc;
33
34         pe = pnv_ioda_get_pe(dev);
35         if (!pe)
36                 return -ENODEV;
37
38         pe_info(pe, "Switching PHB to CXL\n");
39
40         rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
41         if (rc == OPAL_UNSUPPORTED)
42                 dev_err(&dev->dev, "Required cxl mode not supported by firmware - update skiboot\n");
43         else if (rc)
44                 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
45
46         return rc;
47 }
48 EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
49
50 /* Find PHB for cxl dev and allocate MSI hwirqs?
51  * Returns the absolute hardware IRQ number
52  */
53 int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
54 {
55         struct pci_controller *hose = pci_bus_to_host(dev->bus);
56         struct pnv_phb *phb = hose->private_data;
57         int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
58
59         if (hwirq < 0) {
60                 dev_warn(&dev->dev, "Failed to find a free MSI\n");
61                 return -ENOSPC;
62         }
63
64         return phb->msi_base + hwirq;
65 }
66 EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
67
68 void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
69 {
70         struct pci_controller *hose = pci_bus_to_host(dev->bus);
71         struct pnv_phb *phb = hose->private_data;
72
73         msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
74 }
75 EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
76
77 void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
78                                   struct pci_dev *dev)
79 {
80         struct pci_controller *hose = pci_bus_to_host(dev->bus);
81         struct pnv_phb *phb = hose->private_data;
82         int i, hwirq;
83
84         for (i = 1; i < CXL_IRQ_RANGES; i++) {
85                 if (!irqs->range[i])
86                         continue;
87                 pr_devel("cxl release irq range 0x%x: offset: 0x%lx  limit: %ld\n",
88                          i, irqs->offset[i],
89                          irqs->range[i]);
90                 hwirq = irqs->offset[i] - phb->msi_base;
91                 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
92                                        irqs->range[i]);
93         }
94 }
95 EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
96
97 int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
98                                struct pci_dev *dev, int num)
99 {
100         struct pci_controller *hose = pci_bus_to_host(dev->bus);
101         struct pnv_phb *phb = hose->private_data;
102         int i, hwirq, try;
103
104         memset(irqs, 0, sizeof(struct cxl_irq_ranges));
105
106         /* 0 is reserved for the multiplexed PSL DSI interrupt */
107         for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
108                 try = num;
109                 while (try) {
110                         hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
111                         if (hwirq >= 0)
112                                 break;
113                         try /= 2;
114                 }
115                 if (!try)
116                         goto fail;
117
118                 irqs->offset[i] = phb->msi_base + hwirq;
119                 irqs->range[i] = try;
120                 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx  limit: %li\n",
121                          i, irqs->offset[i], irqs->range[i]);
122                 num -= try;
123         }
124         if (num)
125                 goto fail;
126
127         return 0;
128 fail:
129         pnv_cxl_release_hwirq_ranges(irqs, dev);
130         return -ENOSPC;
131 }
132 EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
133
134 int pnv_cxl_get_irq_count(struct pci_dev *dev)
135 {
136         struct pci_controller *hose = pci_bus_to_host(dev->bus);
137         struct pnv_phb *phb = hose->private_data;
138
139         return phb->msi_bmp.irq_count;
140 }
141 EXPORT_SYMBOL(pnv_cxl_get_irq_count);
142
143 int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
144                            unsigned int virq)
145 {
146         struct pci_controller *hose = pci_bus_to_host(dev->bus);
147         struct pnv_phb *phb = hose->private_data;
148         unsigned int xive_num = hwirq - phb->msi_base;
149         struct pnv_ioda_pe *pe;
150         int rc;
151
152         if (!(pe = pnv_ioda_get_pe(dev)))
153                 return -ENODEV;
154
155         /* Assign XIVE to PE */
156         rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
157         if (rc) {
158                 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
159                         "hwirq 0x%x XIVE 0x%x PE\n",
160                         pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
161                 return -EIO;
162         }
163         pnv_set_msi_irq_chip(phb, virq);
164
165         return 0;
166 }
167 EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
168
169 /*
170  * Sets flags and switches the controller ops to enable the cxl kernel api.
171  * Originally the cxl kernel API operated on a virtual PHB, but certain cards
172  * such as the Mellanox CX4 use a peer model instead and for these cards the
173  * cxl kernel api will operate on the real PHB.
174  */
175 int pnv_cxl_enable_phb_kernel_api(struct pci_controller *hose, bool enable)
176 {
177         struct pnv_phb *phb = hose->private_data;
178         struct module *cxl_module;
179
180         if (!enable) {
181                 /*
182                  * Once cxl mode is enabled on the PHB, there is currently no
183                  * known safe method to disable it again, and trying risks a
184                  * checkstop. If we can find a way to safely disable cxl mode
185                  * in the future we can revisit this, but for now the only sane
186                  * thing to do is to refuse to disable cxl mode:
187                  */
188                 return -EPERM;
189         }
190
191         /*
192          * Hold a reference to the cxl module since several PHB operations now
193          * depend on it, and it would be insane to allow it to be removed so
194          * long as we are in this mode (and since we can't safely disable this
195          * mode once enabled...).
196          */
197         mutex_lock(&module_mutex);
198         cxl_module = find_module("cxl");
199         if (cxl_module)
200                 __module_get(cxl_module);
201         mutex_unlock(&module_mutex);
202         if (!cxl_module)
203                 return -ENODEV;
204
205         phb->flags |= PNV_PHB_FLAG_CXL;
206         hose->controller_ops = pnv_cxl_cx4_ioda_controller_ops;
207
208         return 0;
209 }
210 EXPORT_SYMBOL_GPL(pnv_cxl_enable_phb_kernel_api);
211
212 bool pnv_pci_on_cxl_phb(struct pci_dev *dev)
213 {
214         struct pci_controller *hose = pci_bus_to_host(dev->bus);
215         struct pnv_phb *phb = hose->private_data;
216
217         return !!(phb->flags & PNV_PHB_FLAG_CXL);
218 }
219 EXPORT_SYMBOL_GPL(pnv_pci_on_cxl_phb);
220
221 struct cxl_afu *pnv_cxl_phb_to_afu(struct pci_controller *hose)
222 {
223         struct pnv_phb *phb = hose->private_data;
224
225         return (struct cxl_afu *)phb->cxl_afu;
226 }
227 EXPORT_SYMBOL_GPL(pnv_cxl_phb_to_afu);
228
229 void pnv_cxl_phb_set_peer_afu(struct pci_dev *dev, struct cxl_afu *afu)
230 {
231         struct pci_controller *hose = pci_bus_to_host(dev->bus);
232         struct pnv_phb *phb = hose->private_data;
233
234         phb->cxl_afu = afu;
235 }
236 EXPORT_SYMBOL_GPL(pnv_cxl_phb_set_peer_afu);
237
238 /*
239  * In the peer cxl model, the XSL/PSL is physical function 0, and will be used
240  * by other functions on the device for memory access and interrupts. When the
241  * other functions are enabled we explicitly take a reference on the cxl
242  * function since they will use it, and allocate a default context associated
243  * with that function just like the vPHB model of the cxl kernel API.
244  */
245 bool pnv_cxl_enable_device_hook(struct pci_dev *dev)
246 {
247         struct pci_controller *hose = pci_bus_to_host(dev->bus);
248         struct pnv_phb *phb = hose->private_data;
249         struct cxl_afu *afu = phb->cxl_afu;
250
251         if (!pnv_pci_enable_device_hook(dev))
252                 return false;
253
254
255         /* No special handling for the cxl function, which is always PF 0 */
256         if (PCI_FUNC(dev->devfn) == 0)
257                 return true;
258
259         if (!afu) {
260                 dev_WARN(&dev->dev, "Attempted to enable function > 0 on CXL PHB without a peer AFU\n");
261                 return false;
262         }
263
264         dev_info(&dev->dev, "Enabling function on CXL enabled PHB with peer AFU\n");
265
266         /* Make sure the peer AFU can't go away while this device is active */
267         cxl_afu_get(afu);
268
269         return cxl_pci_associate_default_context(dev, afu);
270 }
271
272 void pnv_cxl_disable_device(struct pci_dev *dev)
273 {
274         struct pci_controller *hose = pci_bus_to_host(dev->bus);
275         struct pnv_phb *phb = hose->private_data;
276         struct cxl_afu *afu = phb->cxl_afu;
277
278         /* No special handling for cxl function: */
279         if (PCI_FUNC(dev->devfn) == 0)
280                 return;
281
282         cxl_pci_disable_device(dev);
283         cxl_afu_put(afu);
284 }
285
286 /*
287  * This is a special version of pnv_setup_msi_irqs for cards in cxl mode. This
288  * function handles setting up the IVTE entries for the XSL to use.
289  *
290  * We are currently not filling out the MSIX table, since the only currently
291  * supported adapter (CX4) uses a custom MSIX table format in cxl mode and it
292  * is up to their driver to fill that out. In the future we may fill out the
293  * MSIX table (and change the IVTE entries to be an index to the MSIX table)
294  * for adapters implementing the Full MSI-X mode described in the CAIA.
295  */
296 int pnv_cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
297 {
298         struct pci_controller *hose = pci_bus_to_host(pdev->bus);
299         struct pnv_phb *phb = hose->private_data;
300         struct msi_desc *entry;
301         struct cxl_context *ctx = NULL;
302         unsigned int virq;
303         int hwirq;
304         int afu_irq = 0;
305         int rc;
306
307         if (WARN_ON(!phb) || !phb->msi_bmp.bitmap)
308                 return -ENODEV;
309
310         if (pdev->no_64bit_msi && !phb->msi32_support)
311                 return -ENODEV;
312
313         rc = cxl_cx4_setup_msi_irqs(pdev, nvec, type);
314         if (rc)
315                 return rc;
316
317         for_each_pci_msi_entry(entry, pdev) {
318                 if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
319                         pr_warn("%s: Supports only 64-bit MSIs\n",
320                                 pci_name(pdev));
321                         return -ENXIO;
322                 }
323
324                 hwirq = cxl_next_msi_hwirq(pdev, &ctx, &afu_irq);
325                 if (WARN_ON(hwirq <= 0))
326                         return (hwirq ? hwirq : -ENOMEM);
327
328                 virq = irq_create_mapping(NULL, hwirq);
329                 if (virq == NO_IRQ) {
330                         pr_warn("%s: Failed to map cxl mode MSI to linux irq\n",
331                                 pci_name(pdev));
332                         return -ENOMEM;
333                 }
334
335                 rc = pnv_cxl_ioda_msi_setup(pdev, hwirq, virq);
336                 if (rc) {
337                         pr_warn("%s: Failed to setup cxl mode MSI\n", pci_name(pdev));
338                         irq_dispose_mapping(virq);
339                         return rc;
340                 }
341
342                 irq_set_msi_desc(virq, entry);
343         }
344
345         return 0;
346 }
347
348 void pnv_cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev)
349 {
350         struct pci_controller *hose = pci_bus_to_host(pdev->bus);
351         struct pnv_phb *phb = hose->private_data;
352         struct msi_desc *entry;
353         irq_hw_number_t hwirq;
354
355         if (WARN_ON(!phb))
356                 return;
357
358         for_each_pci_msi_entry(entry, pdev) {
359                 if (entry->irq == NO_IRQ)
360                         continue;
361                 hwirq = virq_to_hw(entry->irq);
362                 irq_set_msi_desc(entry->irq, NULL);
363                 irq_dispose_mapping(entry->irq);
364         }
365
366         cxl_cx4_teardown_msi_irqs(pdev);
367 }