Merge branches 'for-5.7/upstream-fixes', 'for-5.8/apple', 'for-5.8/asus', 'for-5...
[platform/kernel/linux-starfive.git] / drivers / pci / endpoint / pci-epf-core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /**
3  * PCI Endpoint *Function* (EPF) library
4  *
5  * Copyright (C) 2017 Texas Instruments
6  * Author: Kishon Vijay Abraham I <kishon@ti.com>
7  */
8
9 #include <linux/device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13
14 #include <linux/pci-epc.h>
15 #include <linux/pci-epf.h>
16 #include <linux/pci-ep-cfs.h>
17
18 static DEFINE_MUTEX(pci_epf_mutex);
19
20 static struct bus_type pci_epf_bus_type;
21 static const struct device_type pci_epf_type;
22
23 /**
24  * pci_epf_unbind() - Notify the function driver that the binding between the
25  *                    EPF device and EPC device has been lost
26  * @epf: the EPF device which has lost the binding with the EPC device
27  *
28  * Invoke to notify the function driver that the binding between the EPF device
29  * and EPC device has been lost.
30  */
31 void pci_epf_unbind(struct pci_epf *epf)
32 {
33         if (!epf->driver) {
34                 dev_WARN(&epf->dev, "epf device not bound to driver\n");
35                 return;
36         }
37
38         mutex_lock(&epf->lock);
39         epf->driver->ops->unbind(epf);
40         mutex_unlock(&epf->lock);
41         module_put(epf->driver->owner);
42 }
43 EXPORT_SYMBOL_GPL(pci_epf_unbind);
44
45 /**
46  * pci_epf_bind() - Notify the function driver that the EPF device has been
47  *                  bound to a EPC device
48  * @epf: the EPF device which has been bound to the EPC device
49  *
50  * Invoke to notify the function driver that it has been bound to a EPC device
51  */
52 int pci_epf_bind(struct pci_epf *epf)
53 {
54         int ret;
55
56         if (!epf->driver) {
57                 dev_WARN(&epf->dev, "epf device not bound to driver\n");
58                 return -EINVAL;
59         }
60
61         if (!try_module_get(epf->driver->owner))
62                 return -EAGAIN;
63
64         mutex_lock(&epf->lock);
65         ret = epf->driver->ops->bind(epf);
66         mutex_unlock(&epf->lock);
67
68         return ret;
69 }
70 EXPORT_SYMBOL_GPL(pci_epf_bind);
71
72 /**
73  * pci_epf_free_space() - free the allocated PCI EPF register space
74  * @addr: the virtual address of the PCI EPF register space
75  * @bar: the BAR number corresponding to the register space
76  *
77  * Invoke to free the allocated PCI EPF register space.
78  */
79 void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar)
80 {
81         struct device *dev = epf->epc->dev.parent;
82
83         if (!addr)
84                 return;
85
86         dma_free_coherent(dev, epf->bar[bar].size, addr,
87                           epf->bar[bar].phys_addr);
88
89         epf->bar[bar].phys_addr = 0;
90         epf->bar[bar].addr = NULL;
91         epf->bar[bar].size = 0;
92         epf->bar[bar].barno = 0;
93         epf->bar[bar].flags = 0;
94 }
95 EXPORT_SYMBOL_GPL(pci_epf_free_space);
96
97 /**
98  * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
99  * @size: the size of the memory that has to be allocated
100  * @bar: the BAR number corresponding to the allocated register space
101  * @align: alignment size for the allocation region
102  *
103  * Invoke to allocate memory for the PCI EPF register space.
104  */
105 void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
106                           size_t align)
107 {
108         void *space;
109         struct device *dev = epf->epc->dev.parent;
110         dma_addr_t phys_addr;
111
112         if (size < 128)
113                 size = 128;
114
115         if (align)
116                 size = ALIGN(size, align);
117         else
118                 size = roundup_pow_of_two(size);
119
120         space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
121         if (!space) {
122                 dev_err(dev, "failed to allocate mem space\n");
123                 return NULL;
124         }
125
126         epf->bar[bar].phys_addr = phys_addr;
127         epf->bar[bar].addr = space;
128         epf->bar[bar].size = size;
129         epf->bar[bar].barno = bar;
130         epf->bar[bar].flags |= upper_32_bits(size) ?
131                                 PCI_BASE_ADDRESS_MEM_TYPE_64 :
132                                 PCI_BASE_ADDRESS_MEM_TYPE_32;
133
134         return space;
135 }
136 EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
137
138 static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
139 {
140         struct config_group *group, *tmp;
141
142         if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
143                 return;
144
145         mutex_lock(&pci_epf_mutex);
146         list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
147                 pci_ep_cfs_remove_epf_group(group);
148         list_del(&driver->epf_group);
149         mutex_unlock(&pci_epf_mutex);
150 }
151
152 /**
153  * pci_epf_unregister_driver() - unregister the PCI EPF driver
154  * @driver: the PCI EPF driver that has to be unregistered
155  *
156  * Invoke to unregister the PCI EPF driver.
157  */
158 void pci_epf_unregister_driver(struct pci_epf_driver *driver)
159 {
160         pci_epf_remove_cfs(driver);
161         driver_unregister(&driver->driver);
162 }
163 EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
164
165 static int pci_epf_add_cfs(struct pci_epf_driver *driver)
166 {
167         struct config_group *group;
168         const struct pci_epf_device_id *id;
169
170         if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
171                 return 0;
172
173         INIT_LIST_HEAD(&driver->epf_group);
174
175         id = driver->id_table;
176         while (id->name[0]) {
177                 group = pci_ep_cfs_add_epf_group(id->name);
178                 if (IS_ERR(group)) {
179                         pci_epf_remove_cfs(driver);
180                         return PTR_ERR(group);
181                 }
182
183                 mutex_lock(&pci_epf_mutex);
184                 list_add_tail(&group->group_entry, &driver->epf_group);
185                 mutex_unlock(&pci_epf_mutex);
186                 id++;
187         }
188
189         return 0;
190 }
191
192 /**
193  * __pci_epf_register_driver() - register a new PCI EPF driver
194  * @driver: structure representing PCI EPF driver
195  * @owner: the owner of the module that registers the PCI EPF driver
196  *
197  * Invoke to register a new PCI EPF driver.
198  */
199 int __pci_epf_register_driver(struct pci_epf_driver *driver,
200                               struct module *owner)
201 {
202         int ret;
203
204         if (!driver->ops)
205                 return -EINVAL;
206
207         if (!driver->ops->bind || !driver->ops->unbind)
208                 return -EINVAL;
209
210         driver->driver.bus = &pci_epf_bus_type;
211         driver->driver.owner = owner;
212
213         ret = driver_register(&driver->driver);
214         if (ret)
215                 return ret;
216
217         pci_epf_add_cfs(driver);
218
219         return 0;
220 }
221 EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
222
223 /**
224  * pci_epf_destroy() - destroy the created PCI EPF device
225  * @epf: the PCI EPF device that has to be destroyed.
226  *
227  * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
228  */
229 void pci_epf_destroy(struct pci_epf *epf)
230 {
231         device_unregister(&epf->dev);
232 }
233 EXPORT_SYMBOL_GPL(pci_epf_destroy);
234
235 /**
236  * pci_epf_create() - create a new PCI EPF device
237  * @name: the name of the PCI EPF device. This name will be used to bind the
238  *        the EPF device to a EPF driver
239  *
240  * Invoke to create a new PCI EPF device by providing the name of the function
241  * device.
242  */
243 struct pci_epf *pci_epf_create(const char *name)
244 {
245         int ret;
246         struct pci_epf *epf;
247         struct device *dev;
248         int len;
249
250         epf = kzalloc(sizeof(*epf), GFP_KERNEL);
251         if (!epf)
252                 return ERR_PTR(-ENOMEM);
253
254         len = strchrnul(name, '.') - name;
255         epf->name = kstrndup(name, len, GFP_KERNEL);
256         if (!epf->name) {
257                 kfree(epf);
258                 return ERR_PTR(-ENOMEM);
259         }
260
261         dev = &epf->dev;
262         device_initialize(dev);
263         dev->bus = &pci_epf_bus_type;
264         dev->type = &pci_epf_type;
265         mutex_init(&epf->lock);
266
267         ret = dev_set_name(dev, "%s", name);
268         if (ret) {
269                 put_device(dev);
270                 return ERR_PTR(ret);
271         }
272
273         ret = device_add(dev);
274         if (ret) {
275                 put_device(dev);
276                 return ERR_PTR(ret);
277         }
278
279         return epf;
280 }
281 EXPORT_SYMBOL_GPL(pci_epf_create);
282
283 const struct pci_epf_device_id *
284 pci_epf_match_device(const struct pci_epf_device_id *id, struct pci_epf *epf)
285 {
286         if (!id || !epf)
287                 return NULL;
288
289         while (*id->name) {
290                 if (strcmp(epf->name, id->name) == 0)
291                         return id;
292                 id++;
293         }
294
295         return NULL;
296 }
297 EXPORT_SYMBOL_GPL(pci_epf_match_device);
298
299 static void pci_epf_dev_release(struct device *dev)
300 {
301         struct pci_epf *epf = to_pci_epf(dev);
302
303         kfree(epf->name);
304         kfree(epf);
305 }
306
307 static const struct device_type pci_epf_type = {
308         .release        = pci_epf_dev_release,
309 };
310
311 static int
312 pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
313 {
314         while (id->name[0]) {
315                 if (strcmp(epf->name, id->name) == 0)
316                         return true;
317                 id++;
318         }
319
320         return false;
321 }
322
323 static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
324 {
325         struct pci_epf *epf = to_pci_epf(dev);
326         struct pci_epf_driver *driver = to_pci_epf_driver(drv);
327
328         if (driver->id_table)
329                 return pci_epf_match_id(driver->id_table, epf);
330
331         return !strcmp(epf->name, drv->name);
332 }
333
334 static int pci_epf_device_probe(struct device *dev)
335 {
336         struct pci_epf *epf = to_pci_epf(dev);
337         struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
338
339         if (!driver->probe)
340                 return -ENODEV;
341
342         epf->driver = driver;
343
344         return driver->probe(epf);
345 }
346
347 static int pci_epf_device_remove(struct device *dev)
348 {
349         int ret = 0;
350         struct pci_epf *epf = to_pci_epf(dev);
351         struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
352
353         if (driver->remove)
354                 ret = driver->remove(epf);
355         epf->driver = NULL;
356
357         return ret;
358 }
359
360 static struct bus_type pci_epf_bus_type = {
361         .name           = "pci-epf",
362         .match          = pci_epf_device_match,
363         .probe          = pci_epf_device_probe,
364         .remove         = pci_epf_device_remove,
365 };
366
367 static int __init pci_epf_init(void)
368 {
369         int ret;
370
371         ret = bus_register(&pci_epf_bus_type);
372         if (ret) {
373                 pr_err("failed to register pci epf bus --> %d\n", ret);
374                 return ret;
375         }
376
377         return 0;
378 }
379 module_init(pci_epf_init);
380
381 static void __exit pci_epf_exit(void)
382 {
383         bus_unregister(&pci_epf_bus_type);
384 }
385 module_exit(pci_epf_exit);
386
387 MODULE_DESCRIPTION("PCI EPF Library");
388 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
389 MODULE_LICENSE("GPL v2");