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