GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / pci / endpoint / pci-epc-core.c
1 /**
2  * PCI Endpoint *Controller* (EPC) library
3  *
4  * Copyright (C) 2017 Texas Instruments
5  * Author: Kishon Vijay Abraham I <kishon@ti.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 of
9  * the License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/device.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
24
25 #include <linux/pci-epc.h>
26 #include <linux/pci-epf.h>
27 #include <linux/pci-ep-cfs.h>
28
29 static struct class *pci_epc_class;
30
31 static void devm_pci_epc_release(struct device *dev, void *res)
32 {
33         struct pci_epc *epc = *(struct pci_epc **)res;
34
35         pci_epc_destroy(epc);
36 }
37
38 static int devm_pci_epc_match(struct device *dev, void *res, void *match_data)
39 {
40         struct pci_epc **epc = res;
41
42         return *epc == match_data;
43 }
44
45 /**
46  * pci_epc_put() - release the PCI endpoint controller
47  * @epc: epc returned by pci_epc_get()
48  *
49  * release the refcount the caller obtained by invoking pci_epc_get()
50  */
51 void pci_epc_put(struct pci_epc *epc)
52 {
53         if (!epc || IS_ERR(epc))
54                 return;
55
56         module_put(epc->ops->owner);
57         put_device(&epc->dev);
58 }
59 EXPORT_SYMBOL_GPL(pci_epc_put);
60
61 /**
62  * pci_epc_get() - get the PCI endpoint controller
63  * @epc_name: device name of the endpoint controller
64  *
65  * Invoke to get struct pci_epc * corresponding to the device name of the
66  * endpoint controller
67  */
68 struct pci_epc *pci_epc_get(const char *epc_name)
69 {
70         int ret = -EINVAL;
71         struct pci_epc *epc;
72         struct device *dev;
73         struct class_dev_iter iter;
74
75         class_dev_iter_init(&iter, pci_epc_class, NULL, NULL);
76         while ((dev = class_dev_iter_next(&iter))) {
77                 if (strcmp(epc_name, dev_name(dev)))
78                         continue;
79
80                 epc = to_pci_epc(dev);
81                 if (!try_module_get(epc->ops->owner)) {
82                         ret = -EINVAL;
83                         goto err;
84                 }
85
86                 class_dev_iter_exit(&iter);
87                 get_device(&epc->dev);
88                 return epc;
89         }
90
91 err:
92         class_dev_iter_exit(&iter);
93         return ERR_PTR(ret);
94 }
95 EXPORT_SYMBOL_GPL(pci_epc_get);
96
97 /**
98  * pci_epc_stop() - stop the PCI link
99  * @epc: the link of the EPC device that has to be stopped
100  *
101  * Invoke to stop the PCI link
102  */
103 void pci_epc_stop(struct pci_epc *epc)
104 {
105         unsigned long flags;
106
107         if (IS_ERR(epc) || !epc->ops->stop)
108                 return;
109
110         spin_lock_irqsave(&epc->lock, flags);
111         epc->ops->stop(epc);
112         spin_unlock_irqrestore(&epc->lock, flags);
113 }
114 EXPORT_SYMBOL_GPL(pci_epc_stop);
115
116 /**
117  * pci_epc_start() - start the PCI link
118  * @epc: the link of *this* EPC device has to be started
119  *
120  * Invoke to start the PCI link
121  */
122 int pci_epc_start(struct pci_epc *epc)
123 {
124         int ret;
125         unsigned long flags;
126
127         if (IS_ERR(epc))
128                 return -EINVAL;
129
130         if (!epc->ops->start)
131                 return 0;
132
133         spin_lock_irqsave(&epc->lock, flags);
134         ret = epc->ops->start(epc);
135         spin_unlock_irqrestore(&epc->lock, flags);
136
137         return ret;
138 }
139 EXPORT_SYMBOL_GPL(pci_epc_start);
140
141 /**
142  * pci_epc_raise_irq() - interrupt the host system
143  * @epc: the EPC device which has to interrupt the host
144  * @type: specify the type of interrupt; legacy or MSI
145  * @interrupt_num: the MSI interrupt number
146  *
147  * Invoke to raise an MSI or legacy interrupt
148  */
149 int pci_epc_raise_irq(struct pci_epc *epc, enum pci_epc_irq_type type,
150                       u8 interrupt_num)
151 {
152         int ret;
153         unsigned long flags;
154
155         if (IS_ERR(epc))
156                 return -EINVAL;
157
158         if (!epc->ops->raise_irq)
159                 return 0;
160
161         spin_lock_irqsave(&epc->lock, flags);
162         ret = epc->ops->raise_irq(epc, type, interrupt_num);
163         spin_unlock_irqrestore(&epc->lock, flags);
164
165         return ret;
166 }
167 EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
168
169 /**
170  * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
171  * @epc: the EPC device to which MSI interrupts was requested
172  *
173  * Invoke to get the number of MSI interrupts allocated by the RC
174  */
175 int pci_epc_get_msi(struct pci_epc *epc)
176 {
177         int interrupt;
178         unsigned long flags;
179
180         if (IS_ERR(epc))
181                 return 0;
182
183         if (!epc->ops->get_msi)
184                 return 0;
185
186         spin_lock_irqsave(&epc->lock, flags);
187         interrupt = epc->ops->get_msi(epc);
188         spin_unlock_irqrestore(&epc->lock, flags);
189
190         if (interrupt < 0)
191                 return 0;
192
193         interrupt = 1 << interrupt;
194
195         return interrupt;
196 }
197 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
198
199 /**
200  * pci_epc_set_msi() - set the number of MSI interrupt numbers required
201  * @epc: the EPC device on which MSI has to be configured
202  * @interrupts: number of MSI interrupts required by the EPF
203  *
204  * Invoke to set the required number of MSI interrupts.
205  */
206 int pci_epc_set_msi(struct pci_epc *epc, u8 interrupts)
207 {
208         int ret;
209         u8 encode_int;
210         unsigned long flags;
211
212         if (IS_ERR(epc))
213                 return -EINVAL;
214
215         if (!epc->ops->set_msi)
216                 return 0;
217
218         encode_int = order_base_2(interrupts);
219
220         spin_lock_irqsave(&epc->lock, flags);
221         ret = epc->ops->set_msi(epc, encode_int);
222         spin_unlock_irqrestore(&epc->lock, flags);
223
224         return ret;
225 }
226 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
227
228 /**
229  * pci_epc_unmap_addr() - unmap CPU address from PCI address
230  * @epc: the EPC device on which address is allocated
231  * @phys_addr: physical address of the local system
232  *
233  * Invoke to unmap the CPU address from PCI address.
234  */
235 void pci_epc_unmap_addr(struct pci_epc *epc, phys_addr_t phys_addr)
236 {
237         unsigned long flags;
238
239         if (IS_ERR(epc))
240                 return;
241
242         if (!epc->ops->unmap_addr)
243                 return;
244
245         spin_lock_irqsave(&epc->lock, flags);
246         epc->ops->unmap_addr(epc, phys_addr);
247         spin_unlock_irqrestore(&epc->lock, flags);
248 }
249 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
250
251 /**
252  * pci_epc_map_addr() - map CPU address to PCI address
253  * @epc: the EPC device on which address is allocated
254  * @phys_addr: physical address of the local system
255  * @pci_addr: PCI address to which the physical address should be mapped
256  * @size: the size of the allocation
257  *
258  * Invoke to map CPU address with PCI address.
259  */
260 int pci_epc_map_addr(struct pci_epc *epc, phys_addr_t phys_addr,
261                      u64 pci_addr, size_t size)
262 {
263         int ret;
264         unsigned long flags;
265
266         if (IS_ERR(epc))
267                 return -EINVAL;
268
269         if (!epc->ops->map_addr)
270                 return 0;
271
272         spin_lock_irqsave(&epc->lock, flags);
273         ret = epc->ops->map_addr(epc, phys_addr, pci_addr, size);
274         spin_unlock_irqrestore(&epc->lock, flags);
275
276         return ret;
277 }
278 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
279
280 /**
281  * pci_epc_clear_bar() - reset the BAR
282  * @epc: the EPC device for which the BAR has to be cleared
283  * @bar: the BAR number that has to be reset
284  *
285  * Invoke to reset the BAR of the endpoint device.
286  */
287 void pci_epc_clear_bar(struct pci_epc *epc, int bar)
288 {
289         unsigned long flags;
290
291         if (IS_ERR(epc))
292                 return;
293
294         if (!epc->ops->clear_bar)
295                 return;
296
297         spin_lock_irqsave(&epc->lock, flags);
298         epc->ops->clear_bar(epc, bar);
299         spin_unlock_irqrestore(&epc->lock, flags);
300 }
301 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
302
303 /**
304  * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
305  * @epc: the EPC device on which BAR has to be configured
306  * @bar: the BAR number that has to be configured
307  * @size: the size of the addr space
308  * @flags: specify memory allocation/io allocation/32bit address/64 bit address
309  *
310  * Invoke to configure the BAR of the endpoint device.
311  */
312 int pci_epc_set_bar(struct pci_epc *epc, enum pci_barno bar,
313                     dma_addr_t bar_phys, size_t size, int flags)
314 {
315         int ret;
316         unsigned long irq_flags;
317
318         if (IS_ERR(epc))
319                 return -EINVAL;
320
321         if (!epc->ops->set_bar)
322                 return 0;
323
324         spin_lock_irqsave(&epc->lock, irq_flags);
325         ret = epc->ops->set_bar(epc, bar, bar_phys, size, flags);
326         spin_unlock_irqrestore(&epc->lock, irq_flags);
327
328         return ret;
329 }
330 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
331
332 /**
333  * pci_epc_write_header() - write standard configuration header
334  * @epc: the EPC device to which the configuration header should be written
335  * @header: standard configuration header fields
336  *
337  * Invoke to write the configuration header to the endpoint controller. Every
338  * endpoint controller will have a dedicated location to which the standard
339  * configuration header would be written. The callback function should write
340  * the header fields to this dedicated location.
341  */
342 int pci_epc_write_header(struct pci_epc *epc, struct pci_epf_header *header)
343 {
344         int ret;
345         unsigned long flags;
346
347         if (IS_ERR(epc))
348                 return -EINVAL;
349
350         if (!epc->ops->write_header)
351                 return 0;
352
353         spin_lock_irqsave(&epc->lock, flags);
354         ret = epc->ops->write_header(epc, header);
355         spin_unlock_irqrestore(&epc->lock, flags);
356
357         return ret;
358 }
359 EXPORT_SYMBOL_GPL(pci_epc_write_header);
360
361 /**
362  * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
363  * @epc: the EPC device to which the endpoint function should be added
364  * @epf: the endpoint function to be added
365  *
366  * A PCI endpoint device can have one or more functions. In the case of PCIe,
367  * the specification allows up to 8 PCIe endpoint functions. Invoke
368  * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
369  */
370 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf)
371 {
372         unsigned long flags;
373
374         if (epf->epc)
375                 return -EBUSY;
376
377         if (IS_ERR(epc))
378                 return -EINVAL;
379
380         if (epf->func_no > epc->max_functions - 1)
381                 return -EINVAL;
382
383         epf->epc = epc;
384
385         spin_lock_irqsave(&epc->lock, flags);
386         list_add_tail(&epf->list, &epc->pci_epf);
387         spin_unlock_irqrestore(&epc->lock, flags);
388
389         return 0;
390 }
391 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
392
393 /**
394  * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
395  * @epc: the EPC device from which the endpoint function should be removed
396  * @epf: the endpoint function to be removed
397  *
398  * Invoke to remove PCI endpoint function from the endpoint controller.
399  */
400 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf)
401 {
402         unsigned long flags;
403
404         if (!epc || IS_ERR(epc))
405                 return;
406
407         spin_lock_irqsave(&epc->lock, flags);
408         list_del(&epf->list);
409         spin_unlock_irqrestore(&epc->lock, flags);
410 }
411 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
412
413 /**
414  * pci_epc_linkup() - Notify the EPF device that EPC device has established a
415  *                    connection with the Root Complex.
416  * @epc: the EPC device which has established link with the host
417  *
418  * Invoke to Notify the EPF device that the EPC device has established a
419  * connection with the Root Complex.
420  */
421 void pci_epc_linkup(struct pci_epc *epc)
422 {
423         unsigned long flags;
424         struct pci_epf *epf;
425
426         if (!epc || IS_ERR(epc))
427                 return;
428
429         spin_lock_irqsave(&epc->lock, flags);
430         list_for_each_entry(epf, &epc->pci_epf, list)
431                 pci_epf_linkup(epf);
432         spin_unlock_irqrestore(&epc->lock, flags);
433 }
434 EXPORT_SYMBOL_GPL(pci_epc_linkup);
435
436 /**
437  * pci_epc_destroy() - destroy the EPC device
438  * @epc: the EPC device that has to be destroyed
439  *
440  * Invoke to destroy the PCI EPC device
441  */
442 void pci_epc_destroy(struct pci_epc *epc)
443 {
444         pci_ep_cfs_remove_epc_group(epc->group);
445         device_unregister(&epc->dev);
446         kfree(epc);
447 }
448 EXPORT_SYMBOL_GPL(pci_epc_destroy);
449
450 /**
451  * devm_pci_epc_destroy() - destroy the EPC device
452  * @dev: device that wants to destroy the EPC
453  * @epc: the EPC device that has to be destroyed
454  *
455  * Invoke to destroy the devres associated with this
456  * pci_epc and destroy the EPC device.
457  */
458 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
459 {
460         int r;
461
462         r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
463                            epc);
464         dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
465 }
466 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
467
468 /**
469  * __pci_epc_create() - create a new endpoint controller (EPC) device
470  * @dev: device that is creating the new EPC
471  * @ops: function pointers for performing EPC operations
472  * @owner: the owner of the module that creates the EPC device
473  *
474  * Invoke to create a new EPC device and add it to pci_epc class.
475  */
476 struct pci_epc *
477 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
478                  struct module *owner)
479 {
480         int ret;
481         struct pci_epc *epc;
482
483         if (WARN_ON(!dev)) {
484                 ret = -EINVAL;
485                 goto err_ret;
486         }
487
488         epc = kzalloc(sizeof(*epc), GFP_KERNEL);
489         if (!epc) {
490                 ret = -ENOMEM;
491                 goto err_ret;
492         }
493
494         spin_lock_init(&epc->lock);
495         INIT_LIST_HEAD(&epc->pci_epf);
496
497         device_initialize(&epc->dev);
498         epc->dev.class = pci_epc_class;
499         epc->dev.parent = dev;
500         epc->ops = ops;
501
502         ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
503         if (ret)
504                 goto put_dev;
505
506         ret = device_add(&epc->dev);
507         if (ret)
508                 goto put_dev;
509
510         epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
511
512         return epc;
513
514 put_dev:
515         put_device(&epc->dev);
516         kfree(epc);
517
518 err_ret:
519         return ERR_PTR(ret);
520 }
521 EXPORT_SYMBOL_GPL(__pci_epc_create);
522
523 /**
524  * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
525  * @dev: device that is creating the new EPC
526  * @ops: function pointers for performing EPC operations
527  * @owner: the owner of the module that creates the EPC device
528  *
529  * Invoke to create a new EPC device and add it to pci_epc class.
530  * While at that, it also associates the device with the pci_epc using devres.
531  * On driver detach, release function is invoked on the devres data,
532  * then, devres data is freed.
533  */
534 struct pci_epc *
535 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
536                       struct module *owner)
537 {
538         struct pci_epc **ptr, *epc;
539
540         ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
541         if (!ptr)
542                 return ERR_PTR(-ENOMEM);
543
544         epc = __pci_epc_create(dev, ops, owner);
545         if (!IS_ERR(epc)) {
546                 *ptr = epc;
547                 devres_add(dev, ptr);
548         } else {
549                 devres_free(ptr);
550         }
551
552         return epc;
553 }
554 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
555
556 static int __init pci_epc_init(void)
557 {
558         pci_epc_class = class_create(THIS_MODULE, "pci_epc");
559         if (IS_ERR(pci_epc_class)) {
560                 pr_err("failed to create pci epc class --> %ld\n",
561                        PTR_ERR(pci_epc_class));
562                 return PTR_ERR(pci_epc_class);
563         }
564
565         return 0;
566 }
567 module_init(pci_epc_init);
568
569 static void __exit pci_epc_exit(void)
570 {
571         class_destroy(pci_epc_class);
572 }
573 module_exit(pci_epc_exit);
574
575 MODULE_DESCRIPTION("PCI EPC Library");
576 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
577 MODULE_LICENSE("GPL v2");