hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/pci/endpoint/pci-epc-core.c
....@@ -1,5 +1,5 @@
11 // SPDX-License-Identifier: GPL-2.0
2
-/**
2
+/*
33 * PCI Endpoint *Controller* (EPC) library
44 *
55 * Copyright (C) 2017 Texas Instruments
....@@ -84,6 +84,84 @@
8484 EXPORT_SYMBOL_GPL(pci_epc_get);
8585
8686 /**
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 by the endpoint
91
+ * function. For any incorrect value in reserved_bar return '0'.
92
+ */
93
+enum pci_barno
94
+pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features)
95
+{
96
+ return pci_epc_get_next_free_bar(epc_features, BAR_0);
97
+}
98
+EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
99
+
100
+/**
101
+ * pci_epc_get_next_free_bar() - helper to get unreserved BAR starting from @bar
102
+ * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
103
+ * @bar: the starting BAR number from where unreserved BAR should be searched
104
+ *
105
+ * Invoke to get the next unreserved BAR starting from @bar that can be used
106
+ * for endpoint function. For any incorrect value in reserved_bar return '0'.
107
+ */
108
+enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
109
+ *epc_features, enum pci_barno bar)
110
+{
111
+ unsigned long free_bar;
112
+
113
+ if (!epc_features)
114
+ return BAR_0;
115
+
116
+ /* If 'bar - 1' is a 64-bit BAR, move to the next BAR */
117
+ if ((epc_features->bar_fixed_64bit << 1) & 1 << bar)
118
+ bar++;
119
+
120
+ /* Find if the reserved BAR is also a 64-bit BAR */
121
+ free_bar = epc_features->reserved_bar & epc_features->bar_fixed_64bit;
122
+
123
+ /* Set the adjacent bit if the reserved BAR is also a 64-bit BAR */
124
+ free_bar <<= 1;
125
+ free_bar |= epc_features->reserved_bar;
126
+
127
+ free_bar = find_next_zero_bit(&free_bar, 6, bar);
128
+ if (free_bar > 5)
129
+ return NO_BAR;
130
+
131
+ return free_bar;
132
+}
133
+EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
134
+
135
+/**
136
+ * pci_epc_get_features() - get the features supported by EPC
137
+ * @epc: the features supported by *this* EPC device will be returned
138
+ * @func_no: the features supported by the EPC device specific to the
139
+ * endpoint function with func_no will be returned
140
+ *
141
+ * Invoke to get the features provided by the EPC which may be
142
+ * specific to an endpoint function. Returns pci_epc_features on success
143
+ * and NULL for any failures.
144
+ */
145
+const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
146
+ u8 func_no)
147
+{
148
+ const struct pci_epc_features *epc_features;
149
+
150
+ if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
151
+ return NULL;
152
+
153
+ if (!epc->ops->get_features)
154
+ return NULL;
155
+
156
+ mutex_lock(&epc->lock);
157
+ epc_features = epc->ops->get_features(epc, func_no);
158
+ mutex_unlock(&epc->lock);
159
+
160
+ return epc_features;
161
+}
162
+EXPORT_SYMBOL_GPL(pci_epc_get_features);
163
+
164
+/**
87165 * pci_epc_stop() - stop the PCI link
88166 * @epc: the link of the EPC device that has to be stopped
89167 *
....@@ -91,14 +169,12 @@
91169 */
92170 void pci_epc_stop(struct pci_epc *epc)
93171 {
94
- unsigned long flags;
95
-
96172 if (IS_ERR(epc) || !epc->ops->stop)
97173 return;
98174
99
- spin_lock_irqsave(&epc->lock, flags);
175
+ mutex_lock(&epc->lock);
100176 epc->ops->stop(epc);
101
- spin_unlock_irqrestore(&epc->lock, flags);
177
+ mutex_unlock(&epc->lock);
102178 }
103179 EXPORT_SYMBOL_GPL(pci_epc_stop);
104180
....@@ -111,7 +187,6 @@
111187 int pci_epc_start(struct pci_epc *epc)
112188 {
113189 int ret;
114
- unsigned long flags;
115190
116191 if (IS_ERR(epc))
117192 return -EINVAL;
....@@ -119,9 +194,9 @@
119194 if (!epc->ops->start)
120195 return 0;
121196
122
- spin_lock_irqsave(&epc->lock, flags);
197
+ mutex_lock(&epc->lock);
123198 ret = epc->ops->start(epc);
124
- spin_unlock_irqrestore(&epc->lock, flags);
199
+ mutex_unlock(&epc->lock);
125200
126201 return ret;
127202 }
....@@ -140,7 +215,6 @@
140215 enum pci_epc_irq_type type, u16 interrupt_num)
141216 {
142217 int ret;
143
- unsigned long flags;
144218
145219 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
146220 return -EINVAL;
....@@ -148,9 +222,9 @@
148222 if (!epc->ops->raise_irq)
149223 return 0;
150224
151
- spin_lock_irqsave(&epc->lock, flags);
225
+ mutex_lock(&epc->lock);
152226 ret = epc->ops->raise_irq(epc, func_no, type, interrupt_num);
153
- spin_unlock_irqrestore(&epc->lock, flags);
227
+ mutex_unlock(&epc->lock);
154228
155229 return ret;
156230 }
....@@ -166,7 +240,6 @@
166240 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no)
167241 {
168242 int interrupt;
169
- unsigned long flags;
170243
171244 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
172245 return 0;
....@@ -174,9 +247,9 @@
174247 if (!epc->ops->get_msi)
175248 return 0;
176249
177
- spin_lock_irqsave(&epc->lock, flags);
250
+ mutex_lock(&epc->lock);
178251 interrupt = epc->ops->get_msi(epc, func_no);
179
- spin_unlock_irqrestore(&epc->lock, flags);
252
+ mutex_unlock(&epc->lock);
180253
181254 if (interrupt < 0)
182255 return 0;
....@@ -199,7 +272,6 @@
199272 {
200273 int ret;
201274 u8 encode_int;
202
- unsigned long flags;
203275
204276 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
205277 interrupts > 32)
....@@ -210,9 +282,9 @@
210282
211283 encode_int = order_base_2(interrupts);
212284
213
- spin_lock_irqsave(&epc->lock, flags);
285
+ mutex_lock(&epc->lock);
214286 ret = epc->ops->set_msi(epc, func_no, encode_int);
215
- spin_unlock_irqrestore(&epc->lock, flags);
287
+ mutex_unlock(&epc->lock);
216288
217289 return ret;
218290 }
....@@ -228,7 +300,6 @@
228300 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no)
229301 {
230302 int interrupt;
231
- unsigned long flags;
232303
233304 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
234305 return 0;
....@@ -236,9 +307,9 @@
236307 if (!epc->ops->get_msix)
237308 return 0;
238309
239
- spin_lock_irqsave(&epc->lock, flags);
310
+ mutex_lock(&epc->lock);
240311 interrupt = epc->ops->get_msix(epc, func_no);
241
- spin_unlock_irqrestore(&epc->lock, flags);
312
+ mutex_unlock(&epc->lock);
242313
243314 if (interrupt < 0)
244315 return 0;
....@@ -252,13 +323,15 @@
252323 * @epc: the EPC device on which MSI-X has to be configured
253324 * @func_no: the endpoint function number in the EPC device
254325 * @interrupts: number of MSI-X interrupts required by the EPF
326
+ * @bir: BAR where the MSI-X table resides
327
+ * @offset: Offset pointing to the start of MSI-X table
255328 *
256329 * Invoke to set the required number of MSI-X interrupts.
257330 */
258
-int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts)
331
+int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts,
332
+ enum pci_barno bir, u32 offset)
259333 {
260334 int ret;
261
- unsigned long flags;
262335
263336 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
264337 interrupts < 1 || interrupts > 2048)
....@@ -267,9 +340,9 @@
267340 if (!epc->ops->set_msix)
268341 return 0;
269342
270
- spin_lock_irqsave(&epc->lock, flags);
271
- ret = epc->ops->set_msix(epc, func_no, interrupts - 1);
272
- spin_unlock_irqrestore(&epc->lock, flags);
343
+ mutex_lock(&epc->lock);
344
+ ret = epc->ops->set_msix(epc, func_no, interrupts - 1, bir, offset);
345
+ mutex_unlock(&epc->lock);
273346
274347 return ret;
275348 }
....@@ -286,17 +359,15 @@
286359 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no,
287360 phys_addr_t phys_addr)
288361 {
289
- unsigned long flags;
290
-
291362 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
292363 return;
293364
294365 if (!epc->ops->unmap_addr)
295366 return;
296367
297
- spin_lock_irqsave(&epc->lock, flags);
368
+ mutex_lock(&epc->lock);
298369 epc->ops->unmap_addr(epc, func_no, phys_addr);
299
- spin_unlock_irqrestore(&epc->lock, flags);
370
+ mutex_unlock(&epc->lock);
300371 }
301372 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
302373
....@@ -314,7 +385,6 @@
314385 phys_addr_t phys_addr, u64 pci_addr, size_t size)
315386 {
316387 int ret;
317
- unsigned long flags;
318388
319389 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
320390 return -EINVAL;
....@@ -322,9 +392,9 @@
322392 if (!epc->ops->map_addr)
323393 return 0;
324394
325
- spin_lock_irqsave(&epc->lock, flags);
395
+ mutex_lock(&epc->lock);
326396 ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size);
327
- spin_unlock_irqrestore(&epc->lock, flags);
397
+ mutex_unlock(&epc->lock);
328398
329399 return ret;
330400 }
....@@ -341,8 +411,6 @@
341411 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no,
342412 struct pci_epf_bar *epf_bar)
343413 {
344
- unsigned long flags;
345
-
346414 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
347415 (epf_bar->barno == BAR_5 &&
348416 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
....@@ -351,9 +419,9 @@
351419 if (!epc->ops->clear_bar)
352420 return;
353421
354
- spin_lock_irqsave(&epc->lock, flags);
422
+ mutex_lock(&epc->lock);
355423 epc->ops->clear_bar(epc, func_no, epf_bar);
356
- spin_unlock_irqrestore(&epc->lock, flags);
424
+ mutex_unlock(&epc->lock);
357425 }
358426 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
359427
....@@ -369,7 +437,6 @@
369437 struct pci_epf_bar *epf_bar)
370438 {
371439 int ret;
372
- unsigned long irq_flags;
373440 int flags = epf_bar->flags;
374441
375442 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
....@@ -384,9 +451,9 @@
384451 if (!epc->ops->set_bar)
385452 return 0;
386453
387
- spin_lock_irqsave(&epc->lock, irq_flags);
454
+ mutex_lock(&epc->lock);
388455 ret = epc->ops->set_bar(epc, func_no, epf_bar);
389
- spin_unlock_irqrestore(&epc->lock, irq_flags);
456
+ mutex_unlock(&epc->lock);
390457
391458 return ret;
392459 }
....@@ -407,7 +474,6 @@
407474 struct pci_epf_header *header)
408475 {
409476 int ret;
410
- unsigned long flags;
411477
412478 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
413479 return -EINVAL;
....@@ -415,9 +481,9 @@
415481 if (!epc->ops->write_header)
416482 return 0;
417483
418
- spin_lock_irqsave(&epc->lock, flags);
484
+ mutex_lock(&epc->lock);
419485 ret = epc->ops->write_header(epc, func_no, header);
420
- spin_unlock_irqrestore(&epc->lock, flags);
486
+ mutex_unlock(&epc->lock);
421487
422488 return ret;
423489 }
....@@ -434,7 +500,8 @@
434500 */
435501 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf)
436502 {
437
- unsigned long flags;
503
+ u32 func_no;
504
+ int ret = 0;
438505
439506 if (epf->epc)
440507 return -EBUSY;
....@@ -442,16 +509,30 @@
442509 if (IS_ERR(epc))
443510 return -EINVAL;
444511
445
- if (epf->func_no > epc->max_functions - 1)
446
- return -EINVAL;
512
+ mutex_lock(&epc->lock);
513
+ func_no = find_first_zero_bit(&epc->function_num_map,
514
+ BITS_PER_LONG);
515
+ if (func_no >= BITS_PER_LONG) {
516
+ ret = -EINVAL;
517
+ goto ret;
518
+ }
447519
520
+ if (func_no > epc->max_functions - 1) {
521
+ dev_err(&epc->dev, "Exceeding max supported Function Number\n");
522
+ ret = -EINVAL;
523
+ goto ret;
524
+ }
525
+
526
+ set_bit(func_no, &epc->function_num_map);
527
+ epf->func_no = func_no;
448528 epf->epc = epc;
449529
450
- spin_lock_irqsave(&epc->lock, flags);
451530 list_add_tail(&epf->list, &epc->pci_epf);
452
- spin_unlock_irqrestore(&epc->lock, flags);
453531
454
- return 0;
532
+ret:
533
+ mutex_unlock(&epc->lock);
534
+
535
+ return ret;
455536 }
456537 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
457538
....@@ -464,14 +545,14 @@
464545 */
465546 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf)
466547 {
467
- unsigned long flags;
468
-
469
- if (!epc || IS_ERR(epc))
548
+ if (!epc || IS_ERR(epc) || !epf)
470549 return;
471550
472
- spin_lock_irqsave(&epc->lock, flags);
551
+ mutex_lock(&epc->lock);
552
+ clear_bit(epf->func_no, &epc->function_num_map);
473553 list_del(&epf->list);
474
- spin_unlock_irqrestore(&epc->lock, flags);
554
+ epf->epc = NULL;
555
+ mutex_unlock(&epc->lock);
475556 }
476557 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
477558
....@@ -485,18 +566,29 @@
485566 */
486567 void pci_epc_linkup(struct pci_epc *epc)
487568 {
488
- unsigned long flags;
489
- struct pci_epf *epf;
490
-
491569 if (!epc || IS_ERR(epc))
492570 return;
493571
494
- spin_lock_irqsave(&epc->lock, flags);
495
- list_for_each_entry(epf, &epc->pci_epf, list)
496
- pci_epf_linkup(epf);
497
- spin_unlock_irqrestore(&epc->lock, flags);
572
+ atomic_notifier_call_chain(&epc->notifier, LINK_UP, NULL);
498573 }
499574 EXPORT_SYMBOL_GPL(pci_epc_linkup);
575
+
576
+/**
577
+ * pci_epc_init_notify() - Notify the EPF device that EPC device's core
578
+ * initialization is completed.
579
+ * @epc: the EPC device whose core initialization is completeds
580
+ *
581
+ * Invoke to Notify the EPF device that the EPC device's initialization
582
+ * is completed.
583
+ */
584
+void pci_epc_init_notify(struct pci_epc *epc)
585
+{
586
+ if (!epc || IS_ERR(epc))
587
+ return;
588
+
589
+ atomic_notifier_call_chain(&epc->notifier, CORE_INIT, NULL);
590
+}
591
+EXPORT_SYMBOL_GPL(pci_epc_init_notify);
500592
501593 /**
502594 * pci_epc_destroy() - destroy the EPC device
....@@ -556,8 +648,9 @@
556648 goto err_ret;
557649 }
558650
559
- spin_lock_init(&epc->lock);
651
+ mutex_init(&epc->lock);
560652 INIT_LIST_HEAD(&epc->pci_epf);
653
+ ATOMIC_INIT_NOTIFIER_HEAD(&epc->notifier);
561654
562655 device_initialize(&epc->dev);
563656 epc->dev.class = pci_epc_class;