blob: 012b6aa9fb3ec07cea94cbc4f7fed54a139aefd6 [file] [log] [blame]
Michael Neuling6f7f0b32015-05-27 16:07:18 +10001/*
2 * Copyright 2014 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/pci.h>
11#include <misc/cxl.h>
12#include "cxl.h"
13
14static int cxl_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
15{
16 if (dma_mask < DMA_BIT_MASK(64)) {
17 pr_info("%s only 64bit DMA supported on CXL", __func__);
18 return -EIO;
19 }
20
21 *(pdev->dev.dma_mask) = dma_mask;
22 return 0;
23}
24
25static int cxl_pci_probe_mode(struct pci_bus *bus)
26{
27 return PCI_PROBE_NORMAL;
28}
29
30static int cxl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
31{
32 return -ENODEV;
33}
34
35static void cxl_teardown_msi_irqs(struct pci_dev *pdev)
36{
37 /*
38 * MSI should never be set but need still need to provide this call
39 * back.
40 */
41}
42
43static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
44{
45 struct pci_controller *phb;
46 struct cxl_afu *afu;
47 struct cxl_context *ctx;
48
49 phb = pci_bus_to_host(dev->bus);
50 afu = (struct cxl_afu *)phb->private_data;
Andrew Donnellan7d1647d2015-09-07 10:52:58 +100051
Christophe Lombard0d400f72016-03-04 12:26:41 +010052 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Andrew Donnellan7d1647d2015-09-07 10:52:58 +100053 dev_warn(&dev->dev, "%s: Device link is down, refusing to enable AFU\n", __func__);
54 return false;
55 }
56
Michael Neuling6f7f0b32015-05-27 16:07:18 +100057 set_dma_ops(&dev->dev, &dma_direct_ops);
58 set_dma_offset(&dev->dev, PAGE_OFFSET);
59
60 /*
61 * Allocate a context to do cxl things too. If we eventually do real
62 * DMA ops, we'll need a default context to attach them to
63 */
64 ctx = cxl_dev_context_init(dev);
65 if (!ctx)
66 return false;
67 dev->dev.archdata.cxl_ctx = ctx;
68
Frederic Barrat5be587b2016-03-04 12:26:28 +010069 return (cxl_ops->afu_check_and_enable(afu) == 0);
Michael Neuling6f7f0b32015-05-27 16:07:18 +100070}
71
72static void cxl_pci_disable_device(struct pci_dev *dev)
73{
74 struct cxl_context *ctx = cxl_get_context(dev);
75
76 if (ctx) {
77 if (ctx->status == STARTED) {
78 dev_err(&dev->dev, "Default context started\n");
79 return;
80 }
Michael Neulingf67b4932015-06-05 14:38:26 +100081 dev->dev.archdata.cxl_ctx = NULL;
Michael Neuling6f7f0b32015-05-27 16:07:18 +100082 cxl_release_context(ctx);
83 }
84}
85
86static resource_size_t cxl_pci_window_alignment(struct pci_bus *bus,
87 unsigned long type)
88{
89 return 1;
90}
91
92static void cxl_pci_reset_secondary_bus(struct pci_dev *dev)
93{
94 /* Should we do an AFU reset here ? */
95}
96
97static int cxl_pcie_cfg_record(u8 bus, u8 devfn)
98{
99 return (bus << 8) + devfn;
100}
101
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000102static int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
Frederic Barratd601ea92016-03-04 12:26:40 +0100103 struct cxl_afu **_afu, int *_record)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000104{
105 struct pci_controller *phb;
106 struct cxl_afu *afu;
Frederic Barratd601ea92016-03-04 12:26:40 +0100107 int record;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000108
109 phb = pci_bus_to_host(bus);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000110 if (phb == NULL)
111 return PCIBIOS_DEVICE_NOT_FOUND;
Frederic Barratd601ea92016-03-04 12:26:40 +0100112
Maninder Singh14f21182015-06-29 16:05:11 +0530113 afu = (struct cxl_afu *)phb->private_data;
Frederic Barratd601ea92016-03-04 12:26:40 +0100114 record = cxl_pcie_cfg_record(bus->number, devfn);
115 if (record > afu->crs_num)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000116 return PCIBIOS_DEVICE_NOT_FOUND;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000117
Frederic Barratd601ea92016-03-04 12:26:40 +0100118 *_afu = afu;
119 *_record = record;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000120 return 0;
121}
122
123static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
124 int offset, int len, u32 *val)
125{
Frederic Barratd601ea92016-03-04 12:26:40 +0100126 int rc, record;
127 struct cxl_afu *afu;
128 u8 val8;
129 u16 val16;
130 u32 val32;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000131
Frederic Barratd601ea92016-03-04 12:26:40 +0100132 rc = cxl_pcie_config_info(bus, devfn, &afu, &record);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000133 if (rc)
134 return rc;
135
Frederic Barratd601ea92016-03-04 12:26:40 +0100136 switch (len) {
137 case 1:
138 rc = cxl_ops->afu_cr_read8(afu, record, offset, &val8);
139 *val = val8;
140 break;
141 case 2:
142 rc = cxl_ops->afu_cr_read16(afu, record, offset, &val16);
143 *val = val16;
144 break;
145 case 4:
146 rc = cxl_ops->afu_cr_read32(afu, record, offset, &val32);
147 *val = val32;
148 break;
149 default:
150 WARN_ON(1);
151 }
152
153 if (rc)
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000154 return PCIBIOS_DEVICE_NOT_FOUND;
155
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000156 return PCIBIOS_SUCCESSFUL;
157}
158
159static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
160 int offset, int len, u32 val)
161{
Frederic Barratd601ea92016-03-04 12:26:40 +0100162 int rc, record;
163 struct cxl_afu *afu;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000164
Frederic Barratd601ea92016-03-04 12:26:40 +0100165 rc = cxl_pcie_config_info(bus, devfn, &afu, &record);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000166 if (rc)
167 return rc;
168
Frederic Barratd601ea92016-03-04 12:26:40 +0100169 switch (len) {
170 case 1:
171 rc = cxl_ops->afu_cr_write8(afu, record, offset, val & 0xff);
172 break;
173 case 2:
174 rc = cxl_ops->afu_cr_write16(afu, record, offset, val & 0xffff);
175 break;
176 case 4:
177 rc = cxl_ops->afu_cr_write32(afu, record, offset, val);
178 break;
179 default:
180 WARN_ON(1);
181 }
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000182
Frederic Barratd601ea92016-03-04 12:26:40 +0100183 if (rc)
184 return PCIBIOS_SET_FAILED;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000185
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000186 return PCIBIOS_SUCCESSFUL;
187}
188
189static struct pci_ops cxl_pcie_pci_ops =
190{
191 .read = cxl_pcie_read_config,
192 .write = cxl_pcie_write_config,
193};
194
195
196static struct pci_controller_ops cxl_pci_controller_ops =
197{
198 .probe_mode = cxl_pci_probe_mode,
199 .enable_device_hook = cxl_pci_enable_device_hook,
200 .disable_device = cxl_pci_disable_device,
201 .release_device = cxl_pci_disable_device,
202 .window_alignment = cxl_pci_window_alignment,
203 .reset_secondary_bus = cxl_pci_reset_secondary_bus,
204 .setup_msi_irqs = cxl_setup_msi_irqs,
205 .teardown_msi_irqs = cxl_teardown_msi_irqs,
206 .dma_set_mask = cxl_dma_set_mask,
207};
208
209int cxl_pci_vphb_add(struct cxl_afu *afu)
210{
Frederic Barrata4307392016-06-15 16:42:16 +0200211 struct pci_controller *phb;
Frederic Barratd601ea92016-03-04 12:26:40 +0100212 struct device_node *vphb_dn;
213 struct device *parent;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000214
Frederic Barrata4307392016-06-15 16:42:16 +0200215 /* The parent device is the adapter. Reuse the device node of
216 * the adapter.
217 * We don't seem to care what device node is used for the vPHB,
218 * but tools such as lsvpd walk up the device parents looking
219 * for a valid location code, so we might as well show devices
220 * attached to the adapter as being located on that adapter.
221 */
222 parent = afu->adapter->dev.parent;
223 vphb_dn = parent->of_node;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000224
225 /* Alloc and setup PHB data structure */
Frederic Barratd601ea92016-03-04 12:26:40 +0100226 phb = pcibios_alloc_controller(vphb_dn);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000227 if (!phb)
228 return -ENODEV;
229
230 /* Setup parent in sysfs */
Frederic Barratd601ea92016-03-04 12:26:40 +0100231 phb->parent = parent;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000232
233 /* Setup the PHB using arch provided callback */
234 phb->ops = &cxl_pcie_pci_ops;
Frederic Barratd601ea92016-03-04 12:26:40 +0100235 phb->cfg_addr = NULL;
236 phb->cfg_data = 0;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000237 phb->private_data = afu;
238 phb->controller_ops = cxl_pci_controller_ops;
239
240 /* Scan the bus */
241 pcibios_scan_phb(phb);
242 if (phb->bus == NULL)
243 return -ENXIO;
244
245 /* Claim resources. This might need some rework as well depending
246 * whether we are doing probe-only or not, like assigning unassigned
247 * resources etc...
248 */
249 pcibios_claim_one_bus(phb->bus);
250
251 /* Add probed PCI devices to the device model */
252 pci_bus_add_devices(phb->bus);
253
254 afu->phb = phb;
255
256 return 0;
257}
258
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000259void cxl_pci_vphb_remove(struct cxl_afu *afu)
260{
261 struct pci_controller *phb;
262
263 /* If there is no configuration record we won't have one of these */
264 if (!afu || !afu->phb)
265 return;
266
267 phb = afu->phb;
Andrew Donnellan2e1a2552015-10-13 15:09:44 +1100268 afu->phb = NULL;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000269
270 pci_remove_root_bus(phb->bus);
Andrew Donnellan2e1a2552015-10-13 15:09:44 +1100271 pcibios_free_controller(phb);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000272}
273
Vaibhav Jain17eb3ee2016-02-29 11:10:53 +0530274bool cxl_pci_is_vphb_device(struct pci_dev *dev)
275{
276 struct pci_controller *phb;
277
278 phb = pci_bus_to_host(dev->bus);
279
280 return (phb->ops == &cxl_pcie_pci_ops);
281}
282
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000283struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
284{
285 struct pci_controller *phb;
286
287 phb = pci_bus_to_host(dev->bus);
288
289 return (struct cxl_afu *)phb->private_data;
290}
291EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
292
293unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
294{
295 return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
296}
297EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);