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_stop() - stop the PCI link
88  * @epc: the link of the EPC device that has to be stopped
89  *
90  * Invoke to stop the PCI link
91  */
92 void pci_epc_stop(struct pci_epc *epc)
93 {
94 	unsigned long flags;
95 
96 	if (IS_ERR(epc) || !epc->ops->stop)
97 		return;
98 
99 	spin_lock_irqsave(&epc->lock, flags);
100 	epc->ops->stop(epc);
101 	spin_unlock_irqrestore(&epc->lock, flags);
102 }
103 EXPORT_SYMBOL_GPL(pci_epc_stop);
104 
105 /**
106  * pci_epc_start() - start the PCI link
107  * @epc: the link of *this* EPC device has to be started
108  *
109  * Invoke to start the PCI link
110  */
111 int pci_epc_start(struct pci_epc *epc)
112 {
113 	int ret;
114 	unsigned long flags;
115 
116 	if (IS_ERR(epc))
117 		return -EINVAL;
118 
119 	if (!epc->ops->start)
120 		return 0;
121 
122 	spin_lock_irqsave(&epc->lock, flags);
123 	ret = epc->ops->start(epc);
124 	spin_unlock_irqrestore(&epc->lock, flags);
125 
126 	return ret;
127 }
128 EXPORT_SYMBOL_GPL(pci_epc_start);
129 
130 /**
131  * pci_epc_raise_irq() - interrupt the host system
132  * @epc: the EPC device which has to interrupt the host
133  * @func_no: the endpoint function number in the EPC device
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, u8 func_no,
140 		      enum pci_epc_irq_type type, u8 interrupt_num)
141 {
142 	int ret;
143 	unsigned long flags;
144 
145 	if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
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, func_no, 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  * @func_no: the endpoint function number in the EPC device
163  *
164  * Invoke to get the number of MSI interrupts allocated by the RC
165  */
166 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no)
167 {
168 	int interrupt;
169 	unsigned long flags;
170 
171 	if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
172 		return 0;
173 
174 	if (!epc->ops->get_msi)
175 		return 0;
176 
177 	spin_lock_irqsave(&epc->lock, flags);
178 	interrupt = epc->ops->get_msi(epc, func_no);
179 	spin_unlock_irqrestore(&epc->lock, flags);
180 
181 	if (interrupt < 0)
182 		return 0;
183 
184 	interrupt = 1 << interrupt;
185 
186 	return interrupt;
187 }
188 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
189 
190 /**
191  * pci_epc_set_msi() - set the number of MSI interrupt numbers required
192  * @epc: the EPC device on which MSI has to be configured
193  * @func_no: the endpoint function number in the EPC device
194  * @interrupts: number of MSI interrupts required by the EPF
195  *
196  * Invoke to set the required number of MSI interrupts.
197  */
198 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts)
199 {
200 	int ret;
201 	u8 encode_int;
202 	unsigned long flags;
203 
204 	if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
205 		return -EINVAL;
206 
207 	if (!epc->ops->set_msi)
208 		return 0;
209 
210 	encode_int = order_base_2(interrupts);
211 
212 	spin_lock_irqsave(&epc->lock, flags);
213 	ret = epc->ops->set_msi(epc, func_no, encode_int);
214 	spin_unlock_irqrestore(&epc->lock, flags);
215 
216 	return ret;
217 }
218 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
219 
220 /**
221  * pci_epc_unmap_addr() - unmap CPU address from PCI address
222  * @epc: the EPC device on which address is allocated
223  * @func_no: the endpoint function number in the EPC device
224  * @phys_addr: physical address of the local system
225  *
226  * Invoke to unmap the CPU address from PCI address.
227  */
228 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no,
229 			phys_addr_t phys_addr)
230 {
231 	unsigned long flags;
232 
233 	if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
234 		return;
235 
236 	if (!epc->ops->unmap_addr)
237 		return;
238 
239 	spin_lock_irqsave(&epc->lock, flags);
240 	epc->ops->unmap_addr(epc, func_no, phys_addr);
241 	spin_unlock_irqrestore(&epc->lock, flags);
242 }
243 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
244 
245 /**
246  * pci_epc_map_addr() - map CPU address to PCI address
247  * @epc: the EPC device on which address is allocated
248  * @func_no: the endpoint function number in the EPC device
249  * @phys_addr: physical address of the local system
250  * @pci_addr: PCI address to which the physical address should be mapped
251  * @size: the size of the allocation
252  *
253  * Invoke to map CPU address with PCI address.
254  */
255 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no,
256 		     phys_addr_t phys_addr, u64 pci_addr, size_t size)
257 {
258 	int ret;
259 	unsigned long flags;
260 
261 	if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
262 		return -EINVAL;
263 
264 	if (!epc->ops->map_addr)
265 		return 0;
266 
267 	spin_lock_irqsave(&epc->lock, flags);
268 	ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size);
269 	spin_unlock_irqrestore(&epc->lock, flags);
270 
271 	return ret;
272 }
273 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
274 
275 /**
276  * pci_epc_clear_bar() - reset the BAR
277  * @epc: the EPC device for which the BAR has to be cleared
278  * @func_no: the endpoint function number in the EPC device
279  * @bar: the BAR number that has to be reset
280  *
281  * Invoke to reset the BAR of the endpoint device.
282  */
283 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, int bar)
284 {
285 	unsigned long flags;
286 
287 	if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
288 		return;
289 
290 	if (!epc->ops->clear_bar)
291 		return;
292 
293 	spin_lock_irqsave(&epc->lock, flags);
294 	epc->ops->clear_bar(epc, func_no, bar);
295 	spin_unlock_irqrestore(&epc->lock, flags);
296 }
297 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
298 
299 /**
300  * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
301  * @epc: the EPC device on which BAR has to be configured
302  * @func_no: the endpoint function number in the EPC device
303  * @epf_bar: the struct epf_bar that contains the BAR information
304  *
305  * Invoke to configure the BAR of the endpoint device.
306  */
307 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no,
308 		    struct pci_epf_bar *epf_bar)
309 {
310 	int ret;
311 	unsigned long irq_flags;
312 
313 	if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
314 		return -EINVAL;
315 
316 	if (!epc->ops->set_bar)
317 		return 0;
318 
319 	spin_lock_irqsave(&epc->lock, irq_flags);
320 	ret = epc->ops->set_bar(epc, func_no, epf_bar);
321 	spin_unlock_irqrestore(&epc->lock, irq_flags);
322 
323 	return ret;
324 }
325 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
326 
327 /**
328  * pci_epc_write_header() - write standard configuration header
329  * @epc: the EPC device to which the configuration header should be written
330  * @func_no: the endpoint function number in the EPC device
331  * @header: standard configuration header fields
332  *
333  * Invoke to write the configuration header to the endpoint controller. Every
334  * endpoint controller will have a dedicated location to which the standard
335  * configuration header would be written. The callback function should write
336  * the header fields to this dedicated location.
337  */
338 int pci_epc_write_header(struct pci_epc *epc, u8 func_no,
339 			 struct pci_epf_header *header)
340 {
341 	int ret;
342 	unsigned long flags;
343 
344 	if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
345 		return -EINVAL;
346 
347 	if (!epc->ops->write_header)
348 		return 0;
349 
350 	spin_lock_irqsave(&epc->lock, flags);
351 	ret = epc->ops->write_header(epc, func_no, header);
352 	spin_unlock_irqrestore(&epc->lock, flags);
353 
354 	return ret;
355 }
356 EXPORT_SYMBOL_GPL(pci_epc_write_header);
357 
358 /**
359  * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
360  * @epc: the EPC device to which the endpoint function should be added
361  * @epf: the endpoint function to be added
362  *
363  * A PCI endpoint device can have one or more functions. In the case of PCIe,
364  * the specification allows up to 8 PCIe endpoint functions. Invoke
365  * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
366  */
367 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf)
368 {
369 	unsigned long flags;
370 
371 	if (epf->epc)
372 		return -EBUSY;
373 
374 	if (IS_ERR(epc))
375 		return -EINVAL;
376 
377 	if (epf->func_no > epc->max_functions - 1)
378 		return -EINVAL;
379 
380 	epf->epc = epc;
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 	epc->dev.class = pci_epc_class;
496 	epc->dev.parent = dev;
497 	epc->ops = ops;
498 
499 	ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
500 	if (ret)
501 		goto put_dev;
502 
503 	ret = device_add(&epc->dev);
504 	if (ret)
505 		goto put_dev;
506 
507 	epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
508 
509 	return epc;
510 
511 put_dev:
512 	put_device(&epc->dev);
513 	kfree(epc);
514 
515 err_ret:
516 	return ERR_PTR(ret);
517 }
518 EXPORT_SYMBOL_GPL(__pci_epc_create);
519 
520 /**
521  * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
522  * @dev: device that is creating the new EPC
523  * @ops: function pointers for performing EPC operations
524  * @owner: the owner of the module that creates the EPC device
525  *
526  * Invoke to create a new EPC device and add it to pci_epc class.
527  * While at that, it also associates the device with the pci_epc using devres.
528  * On driver detach, release function is invoked on the devres data,
529  * then, devres data is freed.
530  */
531 struct pci_epc *
532 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
533 		      struct module *owner)
534 {
535 	struct pci_epc **ptr, *epc;
536 
537 	ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
538 	if (!ptr)
539 		return ERR_PTR(-ENOMEM);
540 
541 	epc = __pci_epc_create(dev, ops, owner);
542 	if (!IS_ERR(epc)) {
543 		*ptr = epc;
544 		devres_add(dev, ptr);
545 	} else {
546 		devres_free(ptr);
547 	}
548 
549 	return epc;
550 }
551 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
552 
553 static int __init pci_epc_init(void)
554 {
555 	pci_epc_class = class_create(THIS_MODULE, "pci_epc");
556 	if (IS_ERR(pci_epc_class)) {
557 		pr_err("failed to create pci epc class --> %ld\n",
558 		       PTR_ERR(pci_epc_class));
559 		return PTR_ERR(pci_epc_class);
560 	}
561 
562 	return 0;
563 }
564 module_init(pci_epc_init);
565 
566 static void __exit pci_epc_exit(void)
567 {
568 	class_destroy(pci_epc_class);
569 }
570 module_exit(pci_epc_exit);
571 
572 MODULE_DESCRIPTION("PCI EPC Library");
573 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
574 MODULE_LICENSE("GPL v2");
575