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