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