xref: /linux/drivers/pci/endpoint/pci-epc-core.c (revision fab183d632628381b466a41479489541ac0e29a0)
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 
13 #include <linux/pci-epc.h>
14 #include <linux/pci-epf.h>
15 #include <linux/pci-ep-cfs.h>
16 
17 static const struct class pci_epc_class = {
18 	.name = "pci_epc",
19 };
20 
devm_pci_epc_release(struct device * dev,void * res)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 /**
29  * pci_epc_put() - release the PCI endpoint controller
30  * @epc: epc returned by pci_epc_get()
31  *
32  * release the refcount the caller obtained by invoking pci_epc_get()
33  */
pci_epc_put(struct pci_epc * epc)34 void pci_epc_put(struct pci_epc *epc)
35 {
36 	if (IS_ERR_OR_NULL(epc))
37 		return;
38 
39 	module_put(epc->ops->owner);
40 	put_device(&epc->dev);
41 }
42 EXPORT_SYMBOL_GPL(pci_epc_put);
43 
44 /**
45  * pci_epc_get() - get the PCI endpoint controller
46  * @epc_name: device name of the endpoint controller
47  *
48  * Invoke to get struct pci_epc * corresponding to the device name of the
49  * endpoint controller
50  */
pci_epc_get(const char * epc_name)51 struct pci_epc *pci_epc_get(const char *epc_name)
52 {
53 	int ret = -EINVAL;
54 	struct pci_epc *epc;
55 	struct device *dev;
56 
57 	dev = class_find_device_by_name(&pci_epc_class, epc_name);
58 	if (!dev)
59 		goto err;
60 
61 	epc = to_pci_epc(dev);
62 	if (try_module_get(epc->ops->owner))
63 		return epc;
64 
65 err:
66 	put_device(dev);
67 	return ERR_PTR(ret);
68 }
69 EXPORT_SYMBOL_GPL(pci_epc_get);
70 
71 /**
72  * pci_epc_get_first_free_bar() - helper to get first unreserved BAR
73  * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
74  *
75  * Invoke to get the first unreserved BAR that can be used by the endpoint
76  * function.
77  */
78 enum pci_barno
pci_epc_get_first_free_bar(const struct pci_epc_features * epc_features)79 pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features)
80 {
81 	return pci_epc_get_next_free_bar(epc_features, BAR_0);
82 }
83 EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
84 
85 /**
86  * pci_epc_get_next_free_bar() - helper to get unreserved BAR starting from @bar
87  * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
88  * @bar: the starting BAR number from where unreserved BAR should be searched
89  *
90  * Invoke to get the next unreserved BAR starting from @bar that can be used
91  * for endpoint function.
92  */
pci_epc_get_next_free_bar(const struct pci_epc_features * epc_features,enum pci_barno bar)93 enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
94 					 *epc_features, enum pci_barno bar)
95 {
96 	int i;
97 
98 	if (!epc_features)
99 		return BAR_0;
100 
101 	/* If 'bar - 1' is a 64-bit BAR, move to the next BAR */
102 	if (bar > 0 && epc_features->bar[bar - 1].only_64bit)
103 		bar++;
104 
105 	for (i = bar; i < PCI_STD_NUM_BARS; i++) {
106 		/* If the BAR is not reserved or disabled, return it. */
107 		if (epc_features->bar[i].type != BAR_RESERVED &&
108 		    epc_features->bar[i].type != BAR_DISABLED)
109 			return i;
110 	}
111 
112 	return NO_BAR;
113 }
114 EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
115 
pci_epc_function_is_valid(struct pci_epc * epc,u8 func_no,u8 vfunc_no)116 static bool pci_epc_function_is_valid(struct pci_epc *epc,
117 				      u8 func_no, u8 vfunc_no)
118 {
119 	if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
120 		return false;
121 
122 	if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
123 		return false;
124 
125 	return true;
126 }
127 
128 /**
129  * pci_epc_get_features() - get the features supported by EPC
130  * @epc: the features supported by *this* EPC device will be returned
131  * @func_no: the features supported by the EPC device specific to the
132  *	     endpoint function with func_no will be returned
133  * @vfunc_no: the features supported by the EPC device specific to the
134  *	     virtual endpoint function with vfunc_no will be returned
135  *
136  * Invoke to get the features provided by the EPC which may be
137  * specific to an endpoint function. Returns pci_epc_features on success
138  * and NULL for any failures.
139  */
pci_epc_get_features(struct pci_epc * epc,u8 func_no,u8 vfunc_no)140 const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
141 						    u8 func_no, u8 vfunc_no)
142 {
143 	const struct pci_epc_features *epc_features;
144 
145 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
146 		return NULL;
147 
148 	if (!epc->ops->get_features)
149 		return NULL;
150 
151 	mutex_lock(&epc->lock);
152 	epc_features = epc->ops->get_features(epc, func_no, vfunc_no);
153 	mutex_unlock(&epc->lock);
154 
155 	return epc_features;
156 }
157 EXPORT_SYMBOL_GPL(pci_epc_get_features);
158 
159 /**
160  * pci_epc_get_aux_resources_count() - get the number of EPC-provided auxiliary resources
161  * @epc: EPC device
162  * @func_no: function number
163  * @vfunc_no: virtual function number
164  *
165  * Some EPC backends integrate auxiliary blocks (e.g. DMA engines) whose control
166  * registers and/or descriptor memories can be exposed to the host by mapping
167  * them into BAR space. This helper queries how many such resources the backend
168  * provides.
169  *
170  * Return: the number of available resources on success, -EOPNOTSUPP if the
171  * backend does not support auxiliary resource queries, or another -errno on
172  * failure.
173  */
pci_epc_get_aux_resources_count(struct pci_epc * epc,u8 func_no,u8 vfunc_no)174 int pci_epc_get_aux_resources_count(struct pci_epc *epc, u8 func_no,
175 				    u8 vfunc_no)
176 {
177 	int count;
178 
179 	if (!epc || !epc->ops)
180 		return -EINVAL;
181 
182 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
183 		return -EINVAL;
184 
185 	if (!epc->ops->get_aux_resources_count)
186 		return -EOPNOTSUPP;
187 
188 	mutex_lock(&epc->lock);
189 	count = epc->ops->get_aux_resources_count(epc, func_no,
190 						  vfunc_no);
191 	mutex_unlock(&epc->lock);
192 
193 	return count;
194 }
195 EXPORT_SYMBOL_GPL(pci_epc_get_aux_resources_count);
196 
197 /**
198  * pci_epc_get_aux_resources() - query EPC-provided auxiliary resources
199  * @epc: EPC device
200  * @func_no: function number
201  * @vfunc_no: virtual function number
202  * @resources: output array
203  * @num_resources: size of @resources array in entries
204  *
205  * Some EPC backends integrate auxiliary blocks (e.g. DMA engines) whose control
206  * registers and/or descriptor memories can be exposed to the host by mapping
207  * them into BAR space. This helper queries the backend for such resources.
208  *
209  * Return: 0 on success, -EOPNOTSUPP if the backend does not support auxiliary
210  * resource queries, or another -errno on failure.
211  */
pci_epc_get_aux_resources(struct pci_epc * epc,u8 func_no,u8 vfunc_no,struct pci_epc_aux_resource * resources,int num_resources)212 int pci_epc_get_aux_resources(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
213 			      struct pci_epc_aux_resource *resources,
214 			      int num_resources)
215 {
216 	int ret;
217 
218 	if (!resources || num_resources <= 0)
219 		return -EINVAL;
220 
221 	if (!epc || !epc->ops)
222 		return -EINVAL;
223 
224 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
225 		return -EINVAL;
226 
227 	if (!epc->ops->get_aux_resources)
228 		return -EOPNOTSUPP;
229 
230 	mutex_lock(&epc->lock);
231 	ret = epc->ops->get_aux_resources(epc, func_no, vfunc_no, resources,
232 					  num_resources);
233 	mutex_unlock(&epc->lock);
234 
235 	return ret;
236 }
237 EXPORT_SYMBOL_GPL(pci_epc_get_aux_resources);
238 
239 /**
240  * pci_epc_stop() - stop the PCI link
241  * @epc: the link of the EPC device that has to be stopped
242  *
243  * Invoke to stop the PCI link
244  */
pci_epc_stop(struct pci_epc * epc)245 void pci_epc_stop(struct pci_epc *epc)
246 {
247 	if (IS_ERR(epc) || !epc->ops->stop)
248 		return;
249 
250 	mutex_lock(&epc->lock);
251 	epc->ops->stop(epc);
252 	mutex_unlock(&epc->lock);
253 }
254 EXPORT_SYMBOL_GPL(pci_epc_stop);
255 
256 /**
257  * pci_epc_start() - start the PCI link
258  * @epc: the link of *this* EPC device has to be started
259  *
260  * Invoke to start the PCI link
261  */
pci_epc_start(struct pci_epc * epc)262 int pci_epc_start(struct pci_epc *epc)
263 {
264 	int ret;
265 
266 	if (IS_ERR(epc))
267 		return -EINVAL;
268 
269 	if (!epc->ops->start)
270 		return 0;
271 
272 	mutex_lock(&epc->lock);
273 	ret = epc->ops->start(epc);
274 	mutex_unlock(&epc->lock);
275 
276 	return ret;
277 }
278 EXPORT_SYMBOL_GPL(pci_epc_start);
279 
280 /**
281  * pci_epc_raise_irq() - interrupt the host system
282  * @epc: the EPC device which has to interrupt the host
283  * @func_no: the physical endpoint function number in the EPC device
284  * @vfunc_no: the virtual endpoint function number in the physical function
285  * @type: specify the type of interrupt; INTX, MSI or MSI-X
286  * @interrupt_num: the MSI or MSI-X interrupt number with range (1-N)
287  *
288  * Invoke to raise an INTX, MSI or MSI-X interrupt
289  */
pci_epc_raise_irq(struct pci_epc * epc,u8 func_no,u8 vfunc_no,unsigned int type,u16 interrupt_num)290 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
291 		      unsigned int type, u16 interrupt_num)
292 {
293 	int ret;
294 
295 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
296 		return -EINVAL;
297 
298 	if (!epc->ops->raise_irq)
299 		return 0;
300 
301 	mutex_lock(&epc->lock);
302 	ret = epc->ops->raise_irq(epc, func_no, vfunc_no, type, interrupt_num);
303 	mutex_unlock(&epc->lock);
304 
305 	return ret;
306 }
307 EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
308 
309 /**
310  * pci_epc_map_msi_irq() - Map physical address to MSI address and return
311  *                         MSI data
312  * @epc: the EPC device which has the MSI capability
313  * @func_no: the physical endpoint function number in the EPC device
314  * @vfunc_no: the virtual endpoint function number in the physical function
315  * @phys_addr: the physical address of the outbound region
316  * @interrupt_num: the MSI interrupt number with range (1-N)
317  * @entry_size: Size of Outbound address region for each interrupt
318  * @msi_data: the data that should be written in order to raise MSI interrupt
319  *            with interrupt number as 'interrupt num'
320  * @msi_addr_offset: Offset of MSI address from the aligned outbound address
321  *                   to which the MSI address is mapped
322  *
323  * Invoke to map physical address to MSI address and return MSI data. The
324  * physical address should be an address in the outbound region. This is
325  * required to implement doorbell functionality of NTB wherein EPC on either
326  * side of the interface (primary and secondary) can directly write to the
327  * physical address (in outbound region) of the other interface to ring
328  * doorbell.
329  */
pci_epc_map_msi_irq(struct pci_epc * epc,u8 func_no,u8 vfunc_no,phys_addr_t phys_addr,u8 interrupt_num,u32 entry_size,u32 * msi_data,u32 * msi_addr_offset)330 int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
331 			phys_addr_t phys_addr, u8 interrupt_num, u32 entry_size,
332 			u32 *msi_data, u32 *msi_addr_offset)
333 {
334 	int ret;
335 
336 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
337 		return -EINVAL;
338 
339 	if (!epc->ops->map_msi_irq)
340 		return -EINVAL;
341 
342 	mutex_lock(&epc->lock);
343 	ret = epc->ops->map_msi_irq(epc, func_no, vfunc_no, phys_addr,
344 				    interrupt_num, entry_size, msi_data,
345 				    msi_addr_offset);
346 	mutex_unlock(&epc->lock);
347 
348 	return ret;
349 }
350 EXPORT_SYMBOL_GPL(pci_epc_map_msi_irq);
351 
352 /**
353  * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
354  * @epc: the EPC device to which MSI interrupts was requested
355  * @func_no: the physical endpoint function number in the EPC device
356  * @vfunc_no: the virtual endpoint function number in the physical function
357  *
358  * Invoke to get the number of MSI interrupts allocated by the RC
359  */
pci_epc_get_msi(struct pci_epc * epc,u8 func_no,u8 vfunc_no)360 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
361 {
362 	int interrupt;
363 
364 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
365 		return 0;
366 
367 	if (!epc->ops->get_msi)
368 		return 0;
369 
370 	mutex_lock(&epc->lock);
371 	interrupt = epc->ops->get_msi(epc, func_no, vfunc_no);
372 	mutex_unlock(&epc->lock);
373 
374 	if (interrupt < 0)
375 		return 0;
376 
377 	return interrupt;
378 }
379 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
380 
381 /**
382  * pci_epc_set_msi() - set the number of MSI interrupt numbers required
383  * @epc: the EPC device on which MSI has to be configured
384  * @func_no: the physical endpoint function number in the EPC device
385  * @vfunc_no: the virtual endpoint function number in the physical function
386  * @nr_irqs: number of MSI interrupts required by the EPF
387  *
388  * Invoke to set the required number of MSI interrupts.
389  */
pci_epc_set_msi(struct pci_epc * epc,u8 func_no,u8 vfunc_no,u8 nr_irqs)390 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u8 nr_irqs)
391 {
392 	int ret;
393 
394 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
395 		return -EINVAL;
396 
397 	if (nr_irqs < 1 || nr_irqs > 32)
398 		return -EINVAL;
399 
400 	if (!epc->ops->set_msi)
401 		return 0;
402 
403 	mutex_lock(&epc->lock);
404 	ret = epc->ops->set_msi(epc, func_no, vfunc_no, nr_irqs);
405 	mutex_unlock(&epc->lock);
406 
407 	return ret;
408 }
409 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
410 
411 /**
412  * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
413  * @epc: the EPC device to which MSI-X interrupts was requested
414  * @func_no: the physical endpoint function number in the EPC device
415  * @vfunc_no: the virtual endpoint function number in the physical function
416  *
417  * Invoke to get the number of MSI-X interrupts allocated by the RC
418  */
pci_epc_get_msix(struct pci_epc * epc,u8 func_no,u8 vfunc_no)419 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
420 {
421 	int interrupt;
422 
423 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
424 		return 0;
425 
426 	if (!epc->ops->get_msix)
427 		return 0;
428 
429 	mutex_lock(&epc->lock);
430 	interrupt = epc->ops->get_msix(epc, func_no, vfunc_no);
431 	mutex_unlock(&epc->lock);
432 
433 	if (interrupt < 0)
434 		return 0;
435 
436 	return interrupt;
437 }
438 EXPORT_SYMBOL_GPL(pci_epc_get_msix);
439 
440 /**
441  * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
442  * @epc: the EPC device on which MSI-X has to be configured
443  * @func_no: the physical endpoint function number in the EPC device
444  * @vfunc_no: the virtual endpoint function number in the physical function
445  * @nr_irqs: number of MSI-X interrupts required by the EPF
446  * @bir: BAR where the MSI-X table resides
447  * @offset: Offset pointing to the start of MSI-X table
448  *
449  * Invoke to set the required number of MSI-X interrupts.
450  */
pci_epc_set_msix(struct pci_epc * epc,u8 func_no,u8 vfunc_no,u16 nr_irqs,enum pci_barno bir,u32 offset)451 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u16 nr_irqs,
452 		     enum pci_barno bir, u32 offset)
453 {
454 	int ret;
455 
456 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
457 		return -EINVAL;
458 
459 	if (nr_irqs < 1 || nr_irqs > 2048)
460 		return -EINVAL;
461 
462 	if (!epc->ops->set_msix)
463 		return 0;
464 
465 	mutex_lock(&epc->lock);
466 	ret = epc->ops->set_msix(epc, func_no, vfunc_no, nr_irqs, bir, offset);
467 	mutex_unlock(&epc->lock);
468 
469 	return ret;
470 }
471 EXPORT_SYMBOL_GPL(pci_epc_set_msix);
472 
473 /**
474  * pci_epc_unmap_addr() - unmap CPU address from PCI address
475  * @epc: the EPC device on which address is allocated
476  * @func_no: the physical endpoint function number in the EPC device
477  * @vfunc_no: the virtual endpoint function number in the physical function
478  * @phys_addr: physical address of the local system
479  *
480  * Invoke to unmap the CPU address from PCI address.
481  */
pci_epc_unmap_addr(struct pci_epc * epc,u8 func_no,u8 vfunc_no,phys_addr_t phys_addr)482 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
483 			phys_addr_t phys_addr)
484 {
485 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
486 		return;
487 
488 	if (!epc->ops->unmap_addr)
489 		return;
490 
491 	mutex_lock(&epc->lock);
492 	epc->ops->unmap_addr(epc, func_no, vfunc_no, phys_addr);
493 	mutex_unlock(&epc->lock);
494 }
495 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
496 
497 /**
498  * pci_epc_map_addr() - map CPU address to PCI address
499  * @epc: the EPC device on which address is allocated
500  * @func_no: the physical endpoint function number in the EPC device
501  * @vfunc_no: the virtual endpoint function number in the physical function
502  * @phys_addr: physical address of the local system
503  * @pci_addr: PCI address to which the physical address should be mapped
504  * @size: the size of the allocation
505  *
506  * Invoke to map CPU address with PCI address.
507  */
pci_epc_map_addr(struct pci_epc * epc,u8 func_no,u8 vfunc_no,phys_addr_t phys_addr,u64 pci_addr,size_t size)508 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
509 		     phys_addr_t phys_addr, u64 pci_addr, size_t size)
510 {
511 	int ret;
512 
513 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
514 		return -EINVAL;
515 
516 	if (!epc->ops->map_addr)
517 		return 0;
518 
519 	mutex_lock(&epc->lock);
520 	ret = epc->ops->map_addr(epc, func_no, vfunc_no, phys_addr, pci_addr,
521 				 size);
522 	mutex_unlock(&epc->lock);
523 
524 	return ret;
525 }
526 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
527 
528 /**
529  * pci_epc_mem_map() - allocate and map a PCI address to a CPU address
530  * @epc: the EPC device on which the CPU address is to be allocated and mapped
531  * @func_no: the physical endpoint function number in the EPC device
532  * @vfunc_no: the virtual endpoint function number in the physical function
533  * @pci_addr: PCI address to which the CPU address should be mapped
534  * @pci_size: the number of bytes to map starting from @pci_addr
535  * @map: where to return the mapping information
536  *
537  * Allocate a controller memory address region and map it to a RC PCI address
538  * region, taking into account the controller physical address mapping
539  * constraints using the controller operation align_addr(). If this operation is
540  * not defined, we assume that there are no alignment constraints for the
541  * mapping.
542  *
543  * The effective size of the PCI address range mapped from @pci_addr is
544  * indicated by @map->pci_size. This size may be less than the requested
545  * @pci_size. The local virtual CPU address for the mapping is indicated by
546  * @map->virt_addr (@map->phys_addr indicates the physical address).
547  * The size and CPU address of the controller memory allocated and mapped are
548  * respectively indicated by @map->map_size and @map->virt_base (and
549  * @map->phys_base for the physical address of @map->virt_base).
550  *
551  * Returns 0 on success and a negative error code in case of error.
552  */
pci_epc_mem_map(struct pci_epc * epc,u8 func_no,u8 vfunc_no,u64 pci_addr,size_t pci_size,struct pci_epc_map * map)553 int pci_epc_mem_map(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
554 		    u64 pci_addr, size_t pci_size, struct pci_epc_map *map)
555 {
556 	size_t map_size = pci_size;
557 	size_t map_offset = 0;
558 	int ret;
559 
560 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
561 		return -EINVAL;
562 
563 	if (!pci_size || !map)
564 		return -EINVAL;
565 
566 	/*
567 	 * Align the PCI address to map. If the controller defines the
568 	 * .align_addr() operation, use it to determine the PCI address to map
569 	 * and the size of the mapping. Otherwise, assume that the controller
570 	 * has no alignment constraint.
571 	 */
572 	memset(map, 0, sizeof(*map));
573 	map->pci_addr = pci_addr;
574 	if (epc->ops->align_addr)
575 		map->map_pci_addr =
576 			epc->ops->align_addr(epc, pci_addr,
577 					     &map_size, &map_offset);
578 	else
579 		map->map_pci_addr = pci_addr;
580 	map->map_size = map_size;
581 	if (map->map_pci_addr + map->map_size < pci_addr + pci_size)
582 		map->pci_size = map->map_pci_addr + map->map_size - pci_addr;
583 	else
584 		map->pci_size = pci_size;
585 
586 	map->virt_base = pci_epc_mem_alloc_addr(epc, &map->phys_base,
587 						map->map_size);
588 	if (!map->virt_base)
589 		return -ENOMEM;
590 
591 	map->phys_addr = map->phys_base + map_offset;
592 	map->virt_addr = map->virt_base + map_offset;
593 
594 	ret = pci_epc_map_addr(epc, func_no, vfunc_no, map->phys_base,
595 			       map->map_pci_addr, map->map_size);
596 	if (ret) {
597 		pci_epc_mem_free_addr(epc, map->phys_base, map->virt_base,
598 				      map->map_size);
599 		return ret;
600 	}
601 
602 	return 0;
603 }
604 EXPORT_SYMBOL_GPL(pci_epc_mem_map);
605 
606 /**
607  * pci_epc_mem_unmap() - unmap and free a CPU address region
608  * @epc: the EPC device on which the CPU address is allocated and mapped
609  * @func_no: the physical endpoint function number in the EPC device
610  * @vfunc_no: the virtual endpoint function number in the physical function
611  * @map: the mapping information
612  *
613  * Unmap and free a CPU address region that was allocated and mapped with
614  * pci_epc_mem_map().
615  */
pci_epc_mem_unmap(struct pci_epc * epc,u8 func_no,u8 vfunc_no,struct pci_epc_map * map)616 void pci_epc_mem_unmap(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
617 		       struct pci_epc_map *map)
618 {
619 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
620 		return;
621 
622 	if (!map || !map->virt_base)
623 		return;
624 
625 	pci_epc_unmap_addr(epc, func_no, vfunc_no, map->phys_base);
626 	pci_epc_mem_free_addr(epc, map->phys_base, map->virt_base,
627 			      map->map_size);
628 }
629 EXPORT_SYMBOL_GPL(pci_epc_mem_unmap);
630 
631 /**
632  * pci_epc_clear_bar() - reset the BAR
633  * @epc: the EPC device for which the BAR has to be cleared
634  * @func_no: the physical endpoint function number in the EPC device
635  * @vfunc_no: the virtual endpoint function number in the physical function
636  * @epf_bar: the struct epf_bar that contains the BAR information
637  *
638  * Invoke to reset the BAR of the endpoint device.
639  */
pci_epc_clear_bar(struct pci_epc * epc,u8 func_no,u8 vfunc_no,struct pci_epf_bar * epf_bar)640 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
641 		       struct pci_epf_bar *epf_bar)
642 {
643 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
644 		return;
645 
646 	if (epf_bar->barno == BAR_5 &&
647 	    epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
648 		return;
649 
650 	if (!epc->ops->clear_bar)
651 		return;
652 
653 	mutex_lock(&epc->lock);
654 	epc->ops->clear_bar(epc, func_no, vfunc_no, epf_bar);
655 	mutex_unlock(&epc->lock);
656 }
657 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
658 
659 /**
660  * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
661  * @epc: the EPC device on which BAR has to be configured
662  * @func_no: the physical endpoint function number in the EPC device
663  * @vfunc_no: the virtual endpoint function number in the physical function
664  * @epf_bar: the struct epf_bar that contains the BAR information
665  *
666  * Invoke to configure the BAR of the endpoint device.
667  */
pci_epc_set_bar(struct pci_epc * epc,u8 func_no,u8 vfunc_no,struct pci_epf_bar * epf_bar)668 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
669 		    struct pci_epf_bar *epf_bar)
670 {
671 	const struct pci_epc_features *epc_features;
672 	enum pci_barno bar = epf_bar->barno;
673 	int flags = epf_bar->flags;
674 	int ret;
675 
676 	epc_features = pci_epc_get_features(epc, func_no, vfunc_no);
677 	if (!epc_features)
678 		return -EINVAL;
679 
680 	if (epf_bar->num_submap && !epf_bar->submap)
681 		return -EINVAL;
682 
683 	if (epf_bar->num_submap &&
684 	    !(epc_features->dynamic_inbound_mapping &&
685 	      epc_features->subrange_mapping))
686 		return -EINVAL;
687 
688 	if (epc_features->bar[bar].type == BAR_RESIZABLE &&
689 	    (epf_bar->size < SZ_1M || (u64)epf_bar->size > (SZ_128G * 1024)))
690 		return -EINVAL;
691 
692 	if (epc_features->bar[bar].type == BAR_FIXED &&
693 	    (epc_features->bar[bar].fixed_size != epf_bar->size))
694 		return -EINVAL;
695 
696 	if (!is_power_of_2(epf_bar->size))
697 		return -EINVAL;
698 
699 	if ((epf_bar->barno == BAR_5 && flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
700 	    (flags & PCI_BASE_ADDRESS_SPACE_IO &&
701 	     flags & PCI_BASE_ADDRESS_IO_MASK) ||
702 	    (upper_32_bits(epf_bar->size) &&
703 	     !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
704 		return -EINVAL;
705 
706 	if (!epc->ops->set_bar)
707 		return 0;
708 
709 	mutex_lock(&epc->lock);
710 	ret = epc->ops->set_bar(epc, func_no, vfunc_no, epf_bar);
711 	mutex_unlock(&epc->lock);
712 
713 	return ret;
714 }
715 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
716 
717 /**
718  * pci_epc_bar_size_to_rebar_cap() - convert a size to the representation used
719  *				     by the Resizable BAR Capability Register
720  * @size: the size to convert
721  * @cap: where to store the result
722  *
723  * Returns 0 on success and a negative error code in case of error.
724  */
pci_epc_bar_size_to_rebar_cap(size_t size,u32 * cap)725 int pci_epc_bar_size_to_rebar_cap(size_t size, u32 *cap)
726 {
727 	/*
728 	 * As per PCIe r6.0, sec 7.8.6.2, min size for a resizable BAR is 1 MB,
729 	 * thus disallow a requested BAR size smaller than 1 MB.
730 	 * Disallow a requested BAR size larger than 128 TB.
731 	 */
732 	if (size < SZ_1M || (u64)size > (SZ_128G * 1024))
733 		return -EINVAL;
734 
735 	*cap = ilog2(size) - ilog2(SZ_1M);
736 
737 	/* Sizes in REBAR_CAP start at BIT(4). */
738 	*cap = BIT(*cap + 4);
739 
740 	return 0;
741 }
742 EXPORT_SYMBOL_GPL(pci_epc_bar_size_to_rebar_cap);
743 
744 /**
745  * pci_epc_write_header() - write standard configuration header
746  * @epc: the EPC device to which the configuration header should be written
747  * @func_no: the physical endpoint function number in the EPC device
748  * @vfunc_no: the virtual endpoint function number in the physical function
749  * @header: standard configuration header fields
750  *
751  * Invoke to write the configuration header to the endpoint controller. Every
752  * endpoint controller will have a dedicated location to which the standard
753  * configuration header would be written. The callback function should write
754  * the header fields to this dedicated location.
755  */
pci_epc_write_header(struct pci_epc * epc,u8 func_no,u8 vfunc_no,struct pci_epf_header * header)756 int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
757 			 struct pci_epf_header *header)
758 {
759 	int ret;
760 
761 	if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
762 		return -EINVAL;
763 
764 	/* Only Virtual Function #1 has deviceID */
765 	if (vfunc_no > 1)
766 		return -EINVAL;
767 
768 	if (!epc->ops->write_header)
769 		return 0;
770 
771 	mutex_lock(&epc->lock);
772 	ret = epc->ops->write_header(epc, func_no, vfunc_no, header);
773 	mutex_unlock(&epc->lock);
774 
775 	return ret;
776 }
777 EXPORT_SYMBOL_GPL(pci_epc_write_header);
778 
779 /**
780  * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
781  * @epc: the EPC device to which the endpoint function should be added
782  * @epf: the endpoint function to be added
783  * @type: Identifies if the EPC is connected to the primary or secondary
784  *        interface of EPF
785  *
786  * A PCI endpoint device can have one or more functions. In the case of PCIe,
787  * the specification allows up to 8 PCIe endpoint functions. Invoke
788  * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
789  */
pci_epc_add_epf(struct pci_epc * epc,struct pci_epf * epf,enum pci_epc_interface_type type)790 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
791 		    enum pci_epc_interface_type type)
792 {
793 	struct list_head *list;
794 	u32 func_no;
795 	int ret = 0;
796 
797 	if (IS_ERR_OR_NULL(epc) || epf->is_vf)
798 		return -EINVAL;
799 
800 	if (type == PRIMARY_INTERFACE && epf->epc)
801 		return -EBUSY;
802 
803 	if (type == SECONDARY_INTERFACE && epf->sec_epc)
804 		return -EBUSY;
805 
806 	mutex_lock(&epc->list_lock);
807 	func_no = find_first_zero_bit(&epc->function_num_map,
808 				      BITS_PER_LONG);
809 	if (func_no >= BITS_PER_LONG) {
810 		ret = -EINVAL;
811 		goto ret;
812 	}
813 
814 	if (func_no > epc->max_functions - 1) {
815 		dev_err(&epc->dev, "Exceeding max supported Function Number\n");
816 		ret = -EINVAL;
817 		goto ret;
818 	}
819 
820 	set_bit(func_no, &epc->function_num_map);
821 	if (type == PRIMARY_INTERFACE) {
822 		epf->func_no = func_no;
823 		epf->epc = epc;
824 		list = &epf->list;
825 	} else {
826 		epf->sec_epc_func_no = func_no;
827 		epf->sec_epc = epc;
828 		list = &epf->sec_epc_list;
829 	}
830 
831 	list_add_tail(list, &epc->pci_epf);
832 ret:
833 	mutex_unlock(&epc->list_lock);
834 
835 	return ret;
836 }
837 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
838 
839 /**
840  * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
841  * @epc: the EPC device from which the endpoint function should be removed
842  * @epf: the endpoint function to be removed
843  * @type: identifies if the EPC is connected to the primary or secondary
844  *        interface of EPF
845  *
846  * Invoke to remove PCI endpoint function from the endpoint controller.
847  */
pci_epc_remove_epf(struct pci_epc * epc,struct pci_epf * epf,enum pci_epc_interface_type type)848 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
849 			enum pci_epc_interface_type type)
850 {
851 	struct list_head *list;
852 	u32 func_no = 0;
853 
854 	if (IS_ERR_OR_NULL(epc) || !epf)
855 		return;
856 
857 	mutex_lock(&epc->list_lock);
858 	if (type == PRIMARY_INTERFACE) {
859 		func_no = epf->func_no;
860 		list = &epf->list;
861 		epf->epc = NULL;
862 	} else {
863 		func_no = epf->sec_epc_func_no;
864 		list = &epf->sec_epc_list;
865 		epf->sec_epc = NULL;
866 	}
867 	clear_bit(func_no, &epc->function_num_map);
868 	list_del(list);
869 	mutex_unlock(&epc->list_lock);
870 }
871 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
872 
873 /**
874  * pci_epc_linkup() - Notify the EPF device that EPC device has established a
875  *		      connection with the Root Complex.
876  * @epc: the EPC device which has established link with the host
877  *
878  * Invoke to Notify the EPF device that the EPC device has established a
879  * connection with the Root Complex.
880  */
pci_epc_linkup(struct pci_epc * epc)881 void pci_epc_linkup(struct pci_epc *epc)
882 {
883 	struct pci_epf *epf;
884 
885 	if (IS_ERR_OR_NULL(epc))
886 		return;
887 
888 	mutex_lock(&epc->list_lock);
889 	list_for_each_entry(epf, &epc->pci_epf, list) {
890 		mutex_lock(&epf->lock);
891 		if (epf->event_ops && epf->event_ops->link_up)
892 			epf->event_ops->link_up(epf);
893 		mutex_unlock(&epf->lock);
894 	}
895 	mutex_unlock(&epc->list_lock);
896 }
897 EXPORT_SYMBOL_GPL(pci_epc_linkup);
898 
899 /**
900  * pci_epc_linkdown() - Notify the EPF device that EPC device has dropped the
901  *			connection with the Root Complex.
902  * @epc: the EPC device which has dropped the link with the host
903  *
904  * Invoke to Notify the EPF device that the EPC device has dropped the
905  * connection with the Root Complex.
906  */
pci_epc_linkdown(struct pci_epc * epc)907 void pci_epc_linkdown(struct pci_epc *epc)
908 {
909 	struct pci_epf *epf;
910 
911 	if (IS_ERR_OR_NULL(epc))
912 		return;
913 
914 	mutex_lock(&epc->list_lock);
915 	list_for_each_entry(epf, &epc->pci_epf, list) {
916 		mutex_lock(&epf->lock);
917 		if (epf->event_ops && epf->event_ops->link_down)
918 			epf->event_ops->link_down(epf);
919 		mutex_unlock(&epf->lock);
920 	}
921 	mutex_unlock(&epc->list_lock);
922 }
923 EXPORT_SYMBOL_GPL(pci_epc_linkdown);
924 
925 /**
926  * pci_epc_init_notify() - Notify the EPF device that EPC device initialization
927  *                         is completed.
928  * @epc: the EPC device whose initialization is completed
929  *
930  * Invoke to Notify the EPF device that the EPC device's initialization
931  * is completed.
932  */
pci_epc_init_notify(struct pci_epc * epc)933 void pci_epc_init_notify(struct pci_epc *epc)
934 {
935 	struct pci_epf *epf;
936 
937 	if (IS_ERR_OR_NULL(epc))
938 		return;
939 
940 	mutex_lock(&epc->list_lock);
941 	list_for_each_entry(epf, &epc->pci_epf, list) {
942 		mutex_lock(&epf->lock);
943 		if (epf->event_ops && epf->event_ops->epc_init)
944 			epf->event_ops->epc_init(epf);
945 		mutex_unlock(&epf->lock);
946 	}
947 	epc->init_complete = true;
948 	mutex_unlock(&epc->list_lock);
949 }
950 EXPORT_SYMBOL_GPL(pci_epc_init_notify);
951 
952 /**
953  * pci_epc_notify_pending_init() - Notify the pending EPC device initialization
954  *                                 complete to the EPF device
955  * @epc: the EPC device whose initialization is pending to be notified
956  * @epf: the EPF device to be notified
957  *
958  * Invoke to notify the pending EPC device initialization complete to the EPF
959  * device. This is used to deliver the notification if the EPC initialization
960  * got completed before the EPF driver bind.
961  */
pci_epc_notify_pending_init(struct pci_epc * epc,struct pci_epf * epf)962 void pci_epc_notify_pending_init(struct pci_epc *epc, struct pci_epf *epf)
963 {
964 	if (epc->init_complete) {
965 		mutex_lock(&epf->lock);
966 		if (epf->event_ops && epf->event_ops->epc_init)
967 			epf->event_ops->epc_init(epf);
968 		mutex_unlock(&epf->lock);
969 	}
970 }
971 EXPORT_SYMBOL_GPL(pci_epc_notify_pending_init);
972 
973 /**
974  * pci_epc_deinit_notify() - Notify the EPF device about EPC deinitialization
975  * @epc: the EPC device whose deinitialization is completed
976  *
977  * Invoke to notify the EPF device that the EPC deinitialization is completed.
978  */
pci_epc_deinit_notify(struct pci_epc * epc)979 void pci_epc_deinit_notify(struct pci_epc *epc)
980 {
981 	struct pci_epf *epf;
982 
983 	if (IS_ERR_OR_NULL(epc))
984 		return;
985 
986 	mutex_lock(&epc->list_lock);
987 	list_for_each_entry(epf, &epc->pci_epf, list) {
988 		mutex_lock(&epf->lock);
989 		if (epf->event_ops && epf->event_ops->epc_deinit)
990 			epf->event_ops->epc_deinit(epf);
991 		mutex_unlock(&epf->lock);
992 	}
993 	epc->init_complete = false;
994 	mutex_unlock(&epc->list_lock);
995 }
996 EXPORT_SYMBOL_GPL(pci_epc_deinit_notify);
997 
998 /**
999  * pci_epc_bus_master_enable_notify() - Notify the EPF device that the EPC
1000  *					device has received the Bus Master
1001  *					Enable event from the Root complex
1002  * @epc: the EPC device that received the Bus Master Enable event
1003  *
1004  * Notify the EPF device that the EPC device has generated the Bus Master Enable
1005  * event due to host setting the Bus Master Enable bit in the Command register.
1006  */
pci_epc_bus_master_enable_notify(struct pci_epc * epc)1007 void pci_epc_bus_master_enable_notify(struct pci_epc *epc)
1008 {
1009 	struct pci_epf *epf;
1010 
1011 	if (IS_ERR_OR_NULL(epc))
1012 		return;
1013 
1014 	mutex_lock(&epc->list_lock);
1015 	list_for_each_entry(epf, &epc->pci_epf, list) {
1016 		mutex_lock(&epf->lock);
1017 		if (epf->event_ops && epf->event_ops->bus_master_enable)
1018 			epf->event_ops->bus_master_enable(epf);
1019 		mutex_unlock(&epf->lock);
1020 	}
1021 	mutex_unlock(&epc->list_lock);
1022 }
1023 EXPORT_SYMBOL_GPL(pci_epc_bus_master_enable_notify);
1024 
1025 /**
1026  * pci_epc_destroy() - destroy the EPC device
1027  * @epc: the EPC device that has to be destroyed
1028  *
1029  * Invoke to destroy the PCI EPC device
1030  */
pci_epc_destroy(struct pci_epc * epc)1031 void pci_epc_destroy(struct pci_epc *epc)
1032 {
1033 	pci_ep_cfs_remove_epc_group(epc->group);
1034 #ifdef CONFIG_PCI_DOMAINS_GENERIC
1035 	pci_bus_release_domain_nr(epc->dev.parent, epc->domain_nr);
1036 #endif
1037 	device_unregister(&epc->dev);
1038 }
1039 EXPORT_SYMBOL_GPL(pci_epc_destroy);
1040 
pci_epc_release(struct device * dev)1041 static void pci_epc_release(struct device *dev)
1042 {
1043 	kfree(to_pci_epc(dev));
1044 }
1045 
1046 /**
1047  * __pci_epc_create() - create a new endpoint controller (EPC) device
1048  * @dev: device that is creating the new EPC
1049  * @ops: function pointers for performing EPC operations
1050  * @owner: the owner of the module that creates the EPC device
1051  *
1052  * Invoke to create a new EPC device and add it to pci_epc class.
1053  */
1054 struct pci_epc *
__pci_epc_create(struct device * dev,const struct pci_epc_ops * ops,struct module * owner)1055 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
1056 		 struct module *owner)
1057 {
1058 	int ret;
1059 	struct pci_epc *epc;
1060 
1061 	if (WARN_ON(!dev)) {
1062 		ret = -EINVAL;
1063 		goto err_ret;
1064 	}
1065 
1066 	epc = kzalloc_obj(*epc);
1067 	if (!epc) {
1068 		ret = -ENOMEM;
1069 		goto err_ret;
1070 	}
1071 
1072 	mutex_init(&epc->lock);
1073 	mutex_init(&epc->list_lock);
1074 	INIT_LIST_HEAD(&epc->pci_epf);
1075 
1076 	device_initialize(&epc->dev);
1077 	epc->dev.class = &pci_epc_class;
1078 	epc->dev.parent = dev;
1079 	epc->dev.release = pci_epc_release;
1080 	epc->ops = ops;
1081 
1082 #ifdef CONFIG_PCI_DOMAINS_GENERIC
1083 	epc->domain_nr = pci_bus_find_domain_nr(NULL, dev);
1084 #else
1085 	/*
1086 	 * TODO: If the architecture doesn't support generic PCI
1087 	 * domains, then a custom implementation has to be used.
1088 	 */
1089 	WARN_ONCE(1, "This architecture doesn't support generic PCI domains\n");
1090 #endif
1091 
1092 	ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
1093 	if (ret)
1094 		goto put_dev;
1095 
1096 	ret = device_add(&epc->dev);
1097 	if (ret)
1098 		goto put_dev;
1099 
1100 	epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
1101 
1102 	return epc;
1103 
1104 put_dev:
1105 	put_device(&epc->dev);
1106 
1107 err_ret:
1108 	return ERR_PTR(ret);
1109 }
1110 EXPORT_SYMBOL_GPL(__pci_epc_create);
1111 
1112 /**
1113  * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
1114  * @dev: device that is creating the new EPC
1115  * @ops: function pointers for performing EPC operations
1116  * @owner: the owner of the module that creates the EPC device
1117  *
1118  * Invoke to create a new EPC device and add it to pci_epc class.
1119  * While at that, it also associates the device with the pci_epc using devres.
1120  * On driver detach, release function is invoked on the devres data,
1121  * then, devres data is freed.
1122  */
1123 struct pci_epc *
__devm_pci_epc_create(struct device * dev,const struct pci_epc_ops * ops,struct module * owner)1124 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
1125 		      struct module *owner)
1126 {
1127 	struct pci_epc **ptr, *epc;
1128 
1129 	ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
1130 	if (!ptr)
1131 		return ERR_PTR(-ENOMEM);
1132 
1133 	epc = __pci_epc_create(dev, ops, owner);
1134 	if (!IS_ERR(epc)) {
1135 		*ptr = epc;
1136 		devres_add(dev, ptr);
1137 	} else {
1138 		devres_free(ptr);
1139 	}
1140 
1141 	return epc;
1142 }
1143 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
1144 
pci_epc_init(void)1145 static int __init pci_epc_init(void)
1146 {
1147 	return class_register(&pci_epc_class);
1148 }
1149 module_init(pci_epc_init);
1150 
pci_epc_exit(void)1151 static void __exit pci_epc_exit(void)
1152 {
1153 	class_unregister(&pci_epc_class);
1154 }
1155 module_exit(pci_epc_exit);
1156 
1157 MODULE_DESCRIPTION("PCI EPC Library");
1158 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
1159