Skip to content

Commit 70fa02c

Browse files
shimodaykwilczynski
authored andcommitted
PCI: dwc: Add dw_pcie_ep_{read,write}_dbi[2] helpers
The current code calculated some dbi[2] registers' offset by calling dw_pcie_ep_get_dbi[2]_offset() in each function. To improve the code readability, add dw_pcie_ep_{read,write}_dbi[2} and some data-width related helpers. Link: https://lore.kernel.org/linux-pci/[email protected] Signed-off-by: Yoshihiro Shimoda <[email protected]> Signed-off-by: Krzysztof Wilczyński <[email protected]> Reviewed-by: Serge Semin <[email protected]> Reviewed-by: Manivannan Sadhasivam <[email protected]>
1 parent 641f79b commit 70fa02c

File tree

2 files changed

+153
-124
lines changed

2 files changed

+153
-124
lines changed

drivers/pci/controller/dwc/pcie-designware-ep.c

Lines changed: 60 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,19 @@ dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep *ep, u8 func_no)
4343
return NULL;
4444
}
4545

46-
static unsigned int dw_pcie_ep_get_dbi_offset(struct dw_pcie_ep *ep, u8 func_no)
47-
{
48-
unsigned int dbi_offset = 0;
49-
50-
if (ep->ops->get_dbi_offset)
51-
dbi_offset = ep->ops->get_dbi_offset(ep, func_no);
52-
53-
return dbi_offset;
54-
}
55-
56-
static unsigned int dw_pcie_ep_get_dbi2_offset(struct dw_pcie_ep *ep, u8 func_no)
57-
{
58-
unsigned int dbi2_offset = 0;
59-
60-
if (ep->ops->get_dbi2_offset)
61-
dbi2_offset = ep->ops->get_dbi2_offset(ep, func_no);
62-
else if (ep->ops->get_dbi_offset) /* for backward compatibility */
63-
dbi2_offset = ep->ops->get_dbi_offset(ep, func_no);
64-
65-
return dbi2_offset;
66-
}
67-
6846
static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, u8 func_no,
6947
enum pci_barno bar, int flags)
7048
{
71-
unsigned int dbi_offset, dbi2_offset;
7249
struct dw_pcie_ep *ep = &pci->ep;
73-
u32 reg, reg_dbi2;
74-
75-
dbi_offset = dw_pcie_ep_get_dbi_offset(ep, func_no);
76-
dbi2_offset = dw_pcie_ep_get_dbi2_offset(ep, func_no);
50+
u32 reg;
7751

78-
reg = dbi_offset + PCI_BASE_ADDRESS_0 + (4 * bar);
79-
reg_dbi2 = dbi2_offset + PCI_BASE_ADDRESS_0 + (4 * bar);
52+
reg = PCI_BASE_ADDRESS_0 + (4 * bar);
8053
dw_pcie_dbi_ro_wr_en(pci);
81-
dw_pcie_writel_dbi2(pci, reg_dbi2, 0x0);
82-
dw_pcie_writel_dbi(pci, reg, 0x0);
54+
dw_pcie_ep_writel_dbi2(ep, func_no, reg, 0x0);
55+
dw_pcie_ep_writel_dbi(ep, func_no, reg, 0x0);
8356
if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
84-
dw_pcie_writel_dbi2(pci, reg_dbi2 + 4, 0x0);
85-
dw_pcie_writel_dbi(pci, reg + 4, 0x0);
57+
dw_pcie_ep_writel_dbi2(ep, func_no, reg + 4, 0x0);
58+
dw_pcie_ep_writel_dbi(ep, func_no, reg + 4, 0x0);
8659
}
8760
dw_pcie_dbi_ro_wr_dis(pci);
8861
}
@@ -99,19 +72,15 @@ void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
9972
EXPORT_SYMBOL_GPL(dw_pcie_ep_reset_bar);
10073

10174
static u8 __dw_pcie_ep_find_next_cap(struct dw_pcie_ep *ep, u8 func_no,
102-
u8 cap_ptr, u8 cap)
75+
u8 cap_ptr, u8 cap)
10376
{
104-
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
105-
unsigned int dbi_offset = 0;
10677
u8 cap_id, next_cap_ptr;
10778
u16 reg;
10879

10980
if (!cap_ptr)
11081
return 0;
11182

112-
dbi_offset = dw_pcie_ep_get_dbi_offset(ep, func_no);
113-
114-
reg = dw_pcie_readw_dbi(pci, dbi_offset + cap_ptr);
83+
reg = dw_pcie_ep_readw_dbi(ep, func_no, cap_ptr);
11584
cap_id = (reg & 0x00ff);
11685

11786
if (cap_id > PCI_CAP_ID_MAX)
@@ -126,14 +95,10 @@ static u8 __dw_pcie_ep_find_next_cap(struct dw_pcie_ep *ep, u8 func_no,
12695

12796
static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap)
12897
{
129-
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
130-
unsigned int dbi_offset = 0;
13198
u8 next_cap_ptr;
13299
u16 reg;
133100

134-
dbi_offset = dw_pcie_ep_get_dbi_offset(ep, func_no);
135-
136-
reg = dw_pcie_readw_dbi(pci, dbi_offset + PCI_CAPABILITY_LIST);
101+
reg = dw_pcie_ep_readw_dbi(ep, func_no, PCI_CAPABILITY_LIST);
137102
next_cap_ptr = (reg & 0x00ff);
138103

139104
return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
@@ -144,24 +109,21 @@ static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
144109
{
145110
struct dw_pcie_ep *ep = epc_get_drvdata(epc);
146111
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
147-
unsigned int dbi_offset = 0;
148-
149-
dbi_offset = dw_pcie_ep_get_dbi_offset(ep, func_no);
150112

151113
dw_pcie_dbi_ro_wr_en(pci);
152-
dw_pcie_writew_dbi(pci, dbi_offset + PCI_VENDOR_ID, hdr->vendorid);
153-
dw_pcie_writew_dbi(pci, dbi_offset + PCI_DEVICE_ID, hdr->deviceid);
154-
dw_pcie_writeb_dbi(pci, dbi_offset + PCI_REVISION_ID, hdr->revid);
155-
dw_pcie_writeb_dbi(pci, dbi_offset + PCI_CLASS_PROG, hdr->progif_code);
156-
dw_pcie_writew_dbi(pci, dbi_offset + PCI_CLASS_DEVICE,
157-
hdr->subclass_code | hdr->baseclass_code << 8);
158-
dw_pcie_writeb_dbi(pci, dbi_offset + PCI_CACHE_LINE_SIZE,
159-
hdr->cache_line_size);
160-
dw_pcie_writew_dbi(pci, dbi_offset + PCI_SUBSYSTEM_VENDOR_ID,
161-
hdr->subsys_vendor_id);
162-
dw_pcie_writew_dbi(pci, dbi_offset + PCI_SUBSYSTEM_ID, hdr->subsys_id);
163-
dw_pcie_writeb_dbi(pci, dbi_offset + PCI_INTERRUPT_PIN,
164-
hdr->interrupt_pin);
114+
dw_pcie_ep_writew_dbi(ep, func_no, PCI_VENDOR_ID, hdr->vendorid);
115+
dw_pcie_ep_writew_dbi(ep, func_no, PCI_DEVICE_ID, hdr->deviceid);
116+
dw_pcie_ep_writeb_dbi(ep, func_no, PCI_REVISION_ID, hdr->revid);
117+
dw_pcie_ep_writeb_dbi(ep, func_no, PCI_CLASS_PROG, hdr->progif_code);
118+
dw_pcie_ep_writew_dbi(ep, func_no, PCI_CLASS_DEVICE,
119+
hdr->subclass_code | hdr->baseclass_code << 8);
120+
dw_pcie_ep_writeb_dbi(ep, func_no, PCI_CACHE_LINE_SIZE,
121+
hdr->cache_line_size);
122+
dw_pcie_ep_writew_dbi(ep, func_no, PCI_SUBSYSTEM_VENDOR_ID,
123+
hdr->subsys_vendor_id);
124+
dw_pcie_ep_writew_dbi(ep, func_no, PCI_SUBSYSTEM_ID, hdr->subsys_id);
125+
dw_pcie_ep_writeb_dbi(ep, func_no, PCI_INTERRUPT_PIN,
126+
hdr->interrupt_pin);
165127
dw_pcie_dbi_ro_wr_dis(pci);
166128

167129
return 0;
@@ -243,18 +205,13 @@ static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
243205
{
244206
struct dw_pcie_ep *ep = epc_get_drvdata(epc);
245207
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
246-
unsigned int dbi_offset, dbi2_offset;
247208
enum pci_barno bar = epf_bar->barno;
248209
size_t size = epf_bar->size;
249210
int flags = epf_bar->flags;
250-
u32 reg, reg_dbi2;
251211
int ret, type;
212+
u32 reg;
252213

253-
dbi_offset = dw_pcie_ep_get_dbi_offset(ep, func_no);
254-
dbi2_offset = dw_pcie_ep_get_dbi2_offset(ep, func_no);
255-
256-
reg = PCI_BASE_ADDRESS_0 + (4 * bar) + dbi_offset;
257-
reg_dbi2 = PCI_BASE_ADDRESS_0 + (4 * bar) + dbi2_offset;
214+
reg = PCI_BASE_ADDRESS_0 + (4 * bar);
258215

259216
if (!(flags & PCI_BASE_ADDRESS_SPACE))
260217
type = PCIE_ATU_TYPE_MEM;
@@ -270,12 +227,12 @@ static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
270227

271228
dw_pcie_dbi_ro_wr_en(pci);
272229

273-
dw_pcie_writel_dbi2(pci, reg_dbi2, lower_32_bits(size - 1));
274-
dw_pcie_writel_dbi(pci, reg, flags);
230+
dw_pcie_ep_writel_dbi2(ep, func_no, reg, lower_32_bits(size - 1));
231+
dw_pcie_ep_writel_dbi(ep, func_no, reg, flags);
275232

276233
if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
277-
dw_pcie_writel_dbi2(pci, reg_dbi2 + 4, upper_32_bits(size - 1));
278-
dw_pcie_writel_dbi(pci, reg + 4, 0);
234+
dw_pcie_ep_writel_dbi2(ep, func_no, reg + 4, upper_32_bits(size - 1));
235+
dw_pcie_ep_writel_dbi(ep, func_no, reg + 4, 0);
279236
}
280237

281238
ep->epf_bar[bar] = epf_bar;
@@ -335,19 +292,15 @@ static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
335292
static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
336293
{
337294
struct dw_pcie_ep *ep = epc_get_drvdata(epc);
338-
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
339-
u32 val, reg;
340-
unsigned int dbi_offset = 0;
341295
struct dw_pcie_ep_func *ep_func;
296+
u32 val, reg;
342297

343298
ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
344299
if (!ep_func || !ep_func->msi_cap)
345300
return -EINVAL;
346301

347-
dbi_offset = dw_pcie_ep_get_dbi_offset(ep, func_no);
348-
349-
reg = ep_func->msi_cap + dbi_offset + PCI_MSI_FLAGS;
350-
val = dw_pcie_readw_dbi(pci, reg);
302+
reg = ep_func->msi_cap + PCI_MSI_FLAGS;
303+
val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
351304
if (!(val & PCI_MSI_FLAGS_ENABLE))
352305
return -EINVAL;
353306

@@ -361,22 +314,19 @@ static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
361314
{
362315
struct dw_pcie_ep *ep = epc_get_drvdata(epc);
363316
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
364-
u32 val, reg;
365-
unsigned int dbi_offset = 0;
366317
struct dw_pcie_ep_func *ep_func;
318+
u32 val, reg;
367319

368320
ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
369321
if (!ep_func || !ep_func->msi_cap)
370322
return -EINVAL;
371323

372-
dbi_offset = dw_pcie_ep_get_dbi_offset(ep, func_no);
373-
374-
reg = ep_func->msi_cap + dbi_offset + PCI_MSI_FLAGS;
375-
val = dw_pcie_readw_dbi(pci, reg);
324+
reg = ep_func->msi_cap + PCI_MSI_FLAGS;
325+
val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
376326
val &= ~PCI_MSI_FLAGS_QMASK;
377327
val |= FIELD_PREP(PCI_MSI_FLAGS_QMASK, interrupts);
378328
dw_pcie_dbi_ro_wr_en(pci);
379-
dw_pcie_writew_dbi(pci, reg, val);
329+
dw_pcie_ep_writew_dbi(ep, func_no, reg, val);
380330
dw_pcie_dbi_ro_wr_dis(pci);
381331

382332
return 0;
@@ -385,19 +335,15 @@ static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
385335
static int dw_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
386336
{
387337
struct dw_pcie_ep *ep = epc_get_drvdata(epc);
388-
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
389-
u32 val, reg;
390-
unsigned int dbi_offset = 0;
391338
struct dw_pcie_ep_func *ep_func;
339+
u32 val, reg;
392340

393341
ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
394342
if (!ep_func || !ep_func->msix_cap)
395343
return -EINVAL;
396344

397-
dbi_offset = dw_pcie_ep_get_dbi_offset(ep, func_no);
398-
399-
reg = ep_func->msix_cap + dbi_offset + PCI_MSIX_FLAGS;
400-
val = dw_pcie_readw_dbi(pci, reg);
345+
reg = ep_func->msix_cap + PCI_MSIX_FLAGS;
346+
val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
401347
if (!(val & PCI_MSIX_FLAGS_ENABLE))
402348
return -EINVAL;
403349

@@ -411,31 +357,28 @@ static int dw_pcie_ep_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
411357
{
412358
struct dw_pcie_ep *ep = epc_get_drvdata(epc);
413359
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
414-
u32 val, reg;
415-
unsigned int dbi_offset = 0;
416360
struct dw_pcie_ep_func *ep_func;
361+
u32 val, reg;
417362

418363
ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
419364
if (!ep_func || !ep_func->msix_cap)
420365
return -EINVAL;
421366

422367
dw_pcie_dbi_ro_wr_en(pci);
423368

424-
dbi_offset = dw_pcie_ep_get_dbi_offset(ep, func_no);
425-
426-
reg = ep_func->msix_cap + dbi_offset + PCI_MSIX_FLAGS;
427-
val = dw_pcie_readw_dbi(pci, reg);
369+
reg = ep_func->msix_cap + PCI_MSIX_FLAGS;
370+
val = dw_pcie_ep_readw_dbi(ep, func_no, reg);
428371
val &= ~PCI_MSIX_FLAGS_QSIZE;
429372
val |= interrupts;
430373
dw_pcie_writew_dbi(pci, reg, val);
431374

432-
reg = ep_func->msix_cap + dbi_offset + PCI_MSIX_TABLE;
375+
reg = ep_func->msix_cap + PCI_MSIX_TABLE;
433376
val = offset | bir;
434-
dw_pcie_writel_dbi(pci, reg, val);
377+
dw_pcie_ep_writel_dbi(ep, func_no, reg, val);
435378

436-
reg = ep_func->msix_cap + dbi_offset + PCI_MSIX_PBA;
379+
reg = ep_func->msix_cap + PCI_MSIX_PBA;
437380
val = (offset + (interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
438-
dw_pcie_writel_dbi(pci, reg, val);
381+
dw_pcie_ep_writel_dbi(ep, func_no, reg, val);
439382

440383
dw_pcie_dbi_ro_wr_dis(pci);
441384

@@ -510,38 +453,34 @@ EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_legacy_irq);
510453
int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
511454
u8 interrupt_num)
512455
{
513-
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
456+
u32 msg_addr_lower, msg_addr_upper, reg;
514457
struct dw_pcie_ep_func *ep_func;
515458
struct pci_epc *epc = ep->epc;
516459
unsigned int aligned_offset;
517-
unsigned int dbi_offset = 0;
518460
u16 msg_ctrl, msg_data;
519-
u32 msg_addr_lower, msg_addr_upper, reg;
520-
u64 msg_addr;
521461
bool has_upper;
462+
u64 msg_addr;
522463
int ret;
523464

524465
ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
525466
if (!ep_func || !ep_func->msi_cap)
526467
return -EINVAL;
527468

528-
dbi_offset = dw_pcie_ep_get_dbi_offset(ep, func_no);
529-
530469
/* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
531-
reg = ep_func->msi_cap + dbi_offset + PCI_MSI_FLAGS;
532-
msg_ctrl = dw_pcie_readw_dbi(pci, reg);
470+
reg = ep_func->msi_cap + PCI_MSI_FLAGS;
471+
msg_ctrl = dw_pcie_ep_readw_dbi(ep, func_no, reg);
533472
has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
534-
reg = ep_func->msi_cap + dbi_offset + PCI_MSI_ADDRESS_LO;
535-
msg_addr_lower = dw_pcie_readl_dbi(pci, reg);
473+
reg = ep_func->msi_cap + PCI_MSI_ADDRESS_LO;
474+
msg_addr_lower = dw_pcie_ep_readl_dbi(ep, func_no, reg);
536475
if (has_upper) {
537-
reg = ep_func->msi_cap + dbi_offset + PCI_MSI_ADDRESS_HI;
538-
msg_addr_upper = dw_pcie_readl_dbi(pci, reg);
539-
reg = ep_func->msi_cap + dbi_offset + PCI_MSI_DATA_64;
540-
msg_data = dw_pcie_readw_dbi(pci, reg);
476+
reg = ep_func->msi_cap + PCI_MSI_ADDRESS_HI;
477+
msg_addr_upper = dw_pcie_ep_readl_dbi(ep, func_no, reg);
478+
reg = ep_func->msi_cap + PCI_MSI_DATA_64;
479+
msg_data = dw_pcie_ep_readw_dbi(ep, func_no, reg);
541480
} else {
542481
msg_addr_upper = 0;
543-
reg = ep_func->msi_cap + dbi_offset + PCI_MSI_DATA_32;
544-
msg_data = dw_pcie_readw_dbi(pci, reg);
482+
reg = ep_func->msi_cap + PCI_MSI_DATA_32;
483+
msg_data = dw_pcie_ep_readw_dbi(ep, func_no, reg);
545484
}
546485
aligned_offset = msg_addr_lower & (epc->mem->window.page_size - 1);
547486
msg_addr = ((u64)msg_addr_upper) << 32 |
@@ -582,10 +521,9 @@ int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
582521
u16 interrupt_num)
583522
{
584523
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
585-
struct dw_pcie_ep_func *ep_func;
586524
struct pci_epf_msix_tbl *msix_tbl;
525+
struct dw_pcie_ep_func *ep_func;
587526
struct pci_epc *epc = ep->epc;
588-
unsigned int dbi_offset = 0;
589527
u32 reg, msg_data, vec_ctrl;
590528
unsigned int aligned_offset;
591529
u32 tbl_offset;
@@ -597,10 +535,8 @@ int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
597535
if (!ep_func || !ep_func->msix_cap)
598536
return -EINVAL;
599537

600-
dbi_offset = dw_pcie_ep_get_dbi_offset(ep, func_no);
601-
602-
reg = ep_func->msix_cap + dbi_offset + PCI_MSIX_TABLE;
603-
tbl_offset = dw_pcie_readl_dbi(pci, reg);
538+
reg = ep_func->msix_cap + PCI_MSIX_TABLE;
539+
tbl_offset = dw_pcie_ep_readl_dbi(ep, func_no, reg);
604540
bir = FIELD_GET(PCI_MSIX_TABLE_BIR, tbl_offset);
605541
tbl_offset &= PCI_MSIX_TABLE_OFFSET;
606542

0 commit comments

Comments
 (0)