]> Git Repo - linux.git/blob - drivers/misc/cxl/vphb.c
Merge tag 'docs-4.11' of git://git.lwn.net/linux
[linux.git] / drivers / misc / cxl / vphb.c
1 /*
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 <asm/pnv-pci.h>
13 #include "cxl.h"
14
15 static int cxl_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
16 {
17         if (dma_mask < DMA_BIT_MASK(64)) {
18                 pr_info("%s only 64bit DMA supported on CXL", __func__);
19                 return -EIO;
20         }
21
22         *(pdev->dev.dma_mask) = dma_mask;
23         return 0;
24 }
25
26 static int cxl_pci_probe_mode(struct pci_bus *bus)
27 {
28         return PCI_PROBE_NORMAL;
29 }
30
31 static int cxl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
32 {
33         return -ENODEV;
34 }
35
36 static void cxl_teardown_msi_irqs(struct pci_dev *pdev)
37 {
38         /*
39          * MSI should never be set but need still need to provide this call
40          * back.
41          */
42 }
43
44 static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
45 {
46         struct pci_controller *phb;
47         struct cxl_afu *afu;
48
49         phb = pci_bus_to_host(dev->bus);
50         afu = (struct cxl_afu *)phb->private_data;
51
52         if (!cxl_ops->link_ok(afu->adapter, afu)) {
53                 dev_warn(&dev->dev, "%s: Device link is down, refusing to enable AFU\n", __func__);
54                 return false;
55         }
56
57         set_dma_ops(&dev->dev, &dma_direct_ops);
58         set_dma_offset(&dev->dev, PAGE_OFFSET);
59
60         return _cxl_pci_associate_default_context(dev, afu);
61 }
62
63 static resource_size_t cxl_pci_window_alignment(struct pci_bus *bus,
64                                                 unsigned long type)
65 {
66         return 1;
67 }
68
69 static void cxl_pci_reset_secondary_bus(struct pci_dev *dev)
70 {
71         /* Should we do an AFU reset here ? */
72 }
73
74 static int cxl_pcie_cfg_record(u8 bus, u8 devfn)
75 {
76         return (bus << 8) + devfn;
77 }
78
79 static inline struct cxl_afu *pci_bus_to_afu(struct pci_bus *bus)
80 {
81         struct pci_controller *phb = bus ? pci_bus_to_host(bus) : NULL;
82
83         return phb ? phb->private_data : NULL;
84 }
85
86 static inline int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
87                                        struct cxl_afu *afu, int *_record)
88 {
89         int record;
90
91         record = cxl_pcie_cfg_record(bus->number, devfn);
92         if (record > afu->crs_num)
93                 return PCIBIOS_DEVICE_NOT_FOUND;
94
95         *_record = record;
96         return 0;
97 }
98
99 static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
100                                 int offset, int len, u32 *val)
101 {
102         int rc, record;
103         struct cxl_afu *afu;
104         u8 val8;
105         u16 val16;
106         u32 val32;
107
108         afu = pci_bus_to_afu(bus);
109         /* Grab a reader lock on afu. */
110         if (afu == NULL || !down_read_trylock(&afu->configured_rwsem))
111                 return PCIBIOS_DEVICE_NOT_FOUND;
112
113         rc = cxl_pcie_config_info(bus, devfn, afu, &record);
114         if (rc)
115                 goto out;
116
117         switch (len) {
118         case 1:
119                 rc = cxl_ops->afu_cr_read8(afu, record, offset, &val8);
120                 *val = val8;
121                 break;
122         case 2:
123                 rc = cxl_ops->afu_cr_read16(afu, record, offset, &val16);
124                 *val = val16;
125                 break;
126         case 4:
127                 rc = cxl_ops->afu_cr_read32(afu, record, offset, &val32);
128                 *val = val32;
129                 break;
130         default:
131                 WARN_ON(1);
132         }
133
134 out:
135         up_read(&afu->configured_rwsem);
136         return rc ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
137 }
138
139 static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
140                                  int offset, int len, u32 val)
141 {
142         int rc, record;
143         struct cxl_afu *afu;
144
145         afu = pci_bus_to_afu(bus);
146         /* Grab a reader lock on afu. */
147         if (afu == NULL || !down_read_trylock(&afu->configured_rwsem))
148                 return PCIBIOS_DEVICE_NOT_FOUND;
149
150         rc = cxl_pcie_config_info(bus, devfn, afu, &record);
151         if (rc)
152                 goto out;
153
154         switch (len) {
155         case 1:
156                 rc = cxl_ops->afu_cr_write8(afu, record, offset, val & 0xff);
157                 break;
158         case 2:
159                 rc = cxl_ops->afu_cr_write16(afu, record, offset, val & 0xffff);
160                 break;
161         case 4:
162                 rc = cxl_ops->afu_cr_write32(afu, record, offset, val);
163                 break;
164         default:
165                 WARN_ON(1);
166         }
167
168 out:
169         up_read(&afu->configured_rwsem);
170         return rc ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL;
171 }
172
173 static struct pci_ops cxl_pcie_pci_ops =
174 {
175         .read = cxl_pcie_read_config,
176         .write = cxl_pcie_write_config,
177 };
178
179
180 static struct pci_controller_ops cxl_pci_controller_ops =
181 {
182         .probe_mode = cxl_pci_probe_mode,
183         .enable_device_hook = cxl_pci_enable_device_hook,
184         .disable_device = _cxl_pci_disable_device,
185         .release_device = _cxl_pci_disable_device,
186         .window_alignment = cxl_pci_window_alignment,
187         .reset_secondary_bus = cxl_pci_reset_secondary_bus,
188         .setup_msi_irqs = cxl_setup_msi_irqs,
189         .teardown_msi_irqs = cxl_teardown_msi_irqs,
190         .dma_set_mask = cxl_dma_set_mask,
191 };
192
193 int cxl_pci_vphb_add(struct cxl_afu *afu)
194 {
195         struct pci_controller *phb;
196         struct device_node *vphb_dn;
197         struct device *parent;
198
199         /*
200          * If there are no AFU configuration records we won't have anything to
201          * expose under the vPHB, so skip creating one, returning success since
202          * this is still a valid case. This will also opt us out of EEH
203          * handling since we won't have anything special to do if there are no
204          * kernel drivers attached to the vPHB, and EEH handling is not yet
205          * supported in the peer model.
206          */
207         if (!afu->crs_num)
208                 return 0;
209
210         /* The parent device is the adapter. Reuse the device node of
211          * the adapter.
212          * We don't seem to care what device node is used for the vPHB,
213          * but tools such as lsvpd walk up the device parents looking
214          * for a valid location code, so we might as well show devices
215          * attached to the adapter as being located on that adapter.
216          */
217         parent = afu->adapter->dev.parent;
218         vphb_dn = parent->of_node;
219
220         /* Alloc and setup PHB data structure */
221         phb = pcibios_alloc_controller(vphb_dn);
222         if (!phb)
223                 return -ENODEV;
224
225         /* Setup parent in sysfs */
226         phb->parent = parent;
227
228         /* Setup the PHB using arch provided callback */
229         phb->ops = &cxl_pcie_pci_ops;
230         phb->cfg_addr = NULL;
231         phb->cfg_data = NULL;
232         phb->private_data = afu;
233         phb->controller_ops = cxl_pci_controller_ops;
234
235         /* Scan the bus */
236         pcibios_scan_phb(phb);
237         if (phb->bus == NULL)
238                 return -ENXIO;
239
240         /* Set release hook on root bus */
241         pci_set_host_bridge_release(to_pci_host_bridge(phb->bus->bridge),
242                                     pcibios_free_controller_deferred,
243                                     (void *) phb);
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
259 void 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;
268         afu->phb = NULL;
269
270         pci_remove_root_bus(phb->bus);
271         /*
272          * We don't free phb here - that's handled by
273          * pcibios_free_controller_deferred()
274          */
275 }
276
277 static bool _cxl_pci_is_vphb_device(struct pci_controller *phb)
278 {
279         return (phb->ops == &cxl_pcie_pci_ops);
280 }
281
282 bool cxl_pci_is_vphb_device(struct pci_dev *dev)
283 {
284         struct pci_controller *phb;
285
286         phb = pci_bus_to_host(dev->bus);
287
288         return _cxl_pci_is_vphb_device(phb);
289 }
290
291 struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
292 {
293         struct pci_controller *phb;
294
295         phb = pci_bus_to_host(dev->bus);
296
297         if (_cxl_pci_is_vphb_device(phb))
298                 return (struct cxl_afu *)phb->private_data;
299
300         if (pnv_pci_on_cxl_phb(dev))
301                 return pnv_cxl_phb_to_afu(phb);
302
303         return ERR_PTR(-ENODEV);
304 }
305 EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
306
307 unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
308 {
309         return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
310 }
311 EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);
This page took 0.049618 seconds and 4 git commands to generate.