1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
4 #include <asm/pnv-ocxl.h>
5 #include <misc/ocxl-config.h>
6 #include "ocxl_internal.h"
8 #define EXTRACT_BIT(val, bit) (!!(val & BIT(bit)))
9 #define EXTRACT_BITS(val, s, e) ((val & GENMASK(e, s)) >> s)
11 #define OCXL_DVSEC_AFU_IDX_MASK GENMASK(5, 0)
12 #define OCXL_DVSEC_ACTAG_MASK GENMASK(11, 0)
13 #define OCXL_DVSEC_PASID_MASK GENMASK(19, 0)
14 #define OCXL_DVSEC_PASID_LOG_MASK GENMASK(4, 0)
16 #define OCXL_DVSEC_TEMPL_VERSION 0x0
17 #define OCXL_DVSEC_TEMPL_NAME 0x4
18 #define OCXL_DVSEC_TEMPL_AFU_VERSION 0x1C
19 #define OCXL_DVSEC_TEMPL_MMIO_GLOBAL 0x20
20 #define OCXL_DVSEC_TEMPL_MMIO_GLOBAL_SZ 0x28
21 #define OCXL_DVSEC_TEMPL_MMIO_PP 0x30
22 #define OCXL_DVSEC_TEMPL_MMIO_PP_SZ 0x38
23 #define OCXL_DVSEC_TEMPL_ALL_MEM_SZ 0x3C
24 #define OCXL_DVSEC_TEMPL_LPC_MEM_START 0x40
25 #define OCXL_DVSEC_TEMPL_WWID 0x48
26 #define OCXL_DVSEC_TEMPL_LPC_MEM_SZ 0x58
28 #define OCXL_MAX_AFU_PER_FUNCTION 64
29 #define OCXL_TEMPL_LEN_1_0 0x58
30 #define OCXL_TEMPL_LEN_1_1 0x60
31 #define OCXL_TEMPL_NAME_LEN 24
32 #define OCXL_CFG_TIMEOUT 3
34 static int find_dvsec(struct pci_dev
*dev
, int dvsec_id
)
36 return pci_find_dvsec_capability(dev
, PCI_VENDOR_ID_IBM
, dvsec_id
);
39 static int find_dvsec_afu_ctrl(struct pci_dev
*dev
, u8 afu_idx
)
45 while ((vsec
= pci_find_next_ext_capability(dev
, vsec
,
46 OCXL_EXT_CAP_ID_DVSEC
))) {
47 pci_read_config_word(dev
, vsec
+ OCXL_DVSEC_VENDOR_OFFSET
,
49 pci_read_config_word(dev
, vsec
+ OCXL_DVSEC_ID_OFFSET
, &id
);
51 if (vendor
== PCI_VENDOR_ID_IBM
&&
52 id
== OCXL_DVSEC_AFU_CTRL_ID
) {
53 pci_read_config_byte(dev
,
54 vsec
+ OCXL_DVSEC_AFU_CTRL_AFU_IDX
,
64 * get_function_0() - Find a related PCI device (function 0)
65 * @dev: PCI device to match
67 * Returns a pointer to the related device, or null if not found
69 static struct pci_dev
*get_function_0(struct pci_dev
*dev
)
71 unsigned int devfn
= PCI_DEVFN(PCI_SLOT(dev
->devfn
), 0);
73 return pci_get_domain_bus_and_slot(pci_domain_nr(dev
->bus
),
74 dev
->bus
->number
, devfn
);
77 static void read_pasid(struct pci_dev
*dev
, struct ocxl_fn_config
*fn
)
82 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_PASID
);
85 * PASID capability is not mandatory, but there
86 * shouldn't be any AFU
88 dev_dbg(&dev
->dev
, "Function doesn't require any PASID\n");
89 fn
->max_pasid_log
= -1;
92 pci_read_config_word(dev
, pos
+ PCI_PASID_CAP
, &val
);
93 fn
->max_pasid_log
= EXTRACT_BITS(val
, 8, 12);
96 dev_dbg(&dev
->dev
, "PASID capability:\n");
97 dev_dbg(&dev
->dev
, " Max PASID log = %d\n", fn
->max_pasid_log
);
100 static int read_dvsec_tl(struct pci_dev
*dev
, struct ocxl_fn_config
*fn
)
104 pos
= find_dvsec(dev
, OCXL_DVSEC_TL_ID
);
105 if (!pos
&& PCI_FUNC(dev
->devfn
) == 0) {
106 dev_err(&dev
->dev
, "Can't find TL DVSEC\n");
109 if (pos
&& PCI_FUNC(dev
->devfn
) != 0) {
110 dev_err(&dev
->dev
, "TL DVSEC is only allowed on function 0\n");
113 fn
->dvsec_tl_pos
= pos
;
117 static int read_dvsec_function(struct pci_dev
*dev
, struct ocxl_fn_config
*fn
)
119 int pos
, afu_present
;
122 pos
= find_dvsec(dev
, OCXL_DVSEC_FUNC_ID
);
124 dev_err(&dev
->dev
, "Can't find function DVSEC\n");
127 fn
->dvsec_function_pos
= pos
;
129 pci_read_config_dword(dev
, pos
+ OCXL_DVSEC_FUNC_OFF_INDEX
, &val
);
130 afu_present
= EXTRACT_BIT(val
, 31);
132 fn
->max_afu_index
= -1;
133 dev_dbg(&dev
->dev
, "Function doesn't define any AFU\n");
136 fn
->max_afu_index
= EXTRACT_BITS(val
, 24, 29);
139 dev_dbg(&dev
->dev
, "Function DVSEC:\n");
140 dev_dbg(&dev
->dev
, " Max AFU index = %d\n", fn
->max_afu_index
);
144 static int read_dvsec_afu_info(struct pci_dev
*dev
, struct ocxl_fn_config
*fn
)
148 if (fn
->max_afu_index
< 0) {
149 fn
->dvsec_afu_info_pos
= -1;
153 pos
= find_dvsec(dev
, OCXL_DVSEC_AFU_INFO_ID
);
155 dev_err(&dev
->dev
, "Can't find AFU information DVSEC\n");
158 fn
->dvsec_afu_info_pos
= pos
;
162 static int read_dvsec_vendor(struct pci_dev
*dev
)
165 u32 cfg
, tlx
, dlx
, reset_reload
;
168 * vendor specific DVSEC, for IBM images only. Some older
169 * images may not have it
171 * It's only used on function 0 to specify the version of some
172 * logic blocks and to give access to special registers to
173 * enable host-based flashing.
175 if (PCI_FUNC(dev
->devfn
) != 0)
178 pos
= find_dvsec(dev
, OCXL_DVSEC_VENDOR_ID
);
182 pci_read_config_dword(dev
, pos
+ OCXL_DVSEC_VENDOR_CFG_VERS
, &cfg
);
183 pci_read_config_dword(dev
, pos
+ OCXL_DVSEC_VENDOR_TLX_VERS
, &tlx
);
184 pci_read_config_dword(dev
, pos
+ OCXL_DVSEC_VENDOR_DLX_VERS
, &dlx
);
185 pci_read_config_dword(dev
, pos
+ OCXL_DVSEC_VENDOR_RESET_RELOAD
,
188 dev_dbg(&dev
->dev
, "Vendor specific DVSEC:\n");
189 dev_dbg(&dev
->dev
, " CFG version = 0x%x\n", cfg
);
190 dev_dbg(&dev
->dev
, " TLX version = 0x%x\n", tlx
);
191 dev_dbg(&dev
->dev
, " DLX version = 0x%x\n", dlx
);
192 dev_dbg(&dev
->dev
, " ResetReload = 0x%x\n", reset_reload
);
197 * get_dvsec_vendor0() - Find a related PCI device (function 0)
198 * @dev: PCI device to match
199 * @dev0: The PCI device (function 0) found
200 * @out_pos: The position of PCI device (function 0)
202 * Returns 0 on success, negative on failure.
204 * NOTE: If it's successful, the reference of dev0 is increased,
205 * so after using it, the callers must call pci_dev_put() to give
208 static int get_dvsec_vendor0(struct pci_dev
*dev
, struct pci_dev
**dev0
,
213 if (PCI_FUNC(dev
->devfn
) != 0) {
214 dev
= get_function_0(dev
);
218 dev
= pci_dev_get(dev
);
220 pos
= find_dvsec(dev
, OCXL_DVSEC_VENDOR_ID
);
230 int ocxl_config_get_reset_reload(struct pci_dev
*dev
, int *val
)
232 struct pci_dev
*dev0
;
236 if (get_dvsec_vendor0(dev
, &dev0
, &pos
))
239 pci_read_config_dword(dev0
, pos
+ OCXL_DVSEC_VENDOR_RESET_RELOAD
,
242 *val
= !!(reset_reload
& BIT(0));
246 int ocxl_config_set_reset_reload(struct pci_dev
*dev
, int val
)
248 struct pci_dev
*dev0
;
252 if (get_dvsec_vendor0(dev
, &dev0
, &pos
))
255 pci_read_config_dword(dev0
, pos
+ OCXL_DVSEC_VENDOR_RESET_RELOAD
,
258 reset_reload
|= BIT(0);
260 reset_reload
&= ~BIT(0);
261 pci_write_config_dword(dev0
, pos
+ OCXL_DVSEC_VENDOR_RESET_RELOAD
,
267 static int validate_function(struct pci_dev
*dev
, struct ocxl_fn_config
*fn
)
269 if (fn
->max_pasid_log
== -1 && fn
->max_afu_index
>= 0) {
271 "AFUs are defined but no PASIDs are requested\n");
275 if (fn
->max_afu_index
> OCXL_MAX_AFU_PER_FUNCTION
) {
277 "Max AFU index out of architectural limit (%d vs %d)\n",
278 fn
->max_afu_index
, OCXL_MAX_AFU_PER_FUNCTION
);
284 int ocxl_config_read_function(struct pci_dev
*dev
, struct ocxl_fn_config
*fn
)
290 rc
= read_dvsec_tl(dev
, fn
);
293 "Invalid Transaction Layer DVSEC configuration: %d\n",
298 rc
= read_dvsec_function(dev
, fn
);
301 "Invalid Function DVSEC configuration: %d\n", rc
);
305 rc
= read_dvsec_afu_info(dev
, fn
);
307 dev_err(&dev
->dev
, "Invalid AFU configuration: %d\n", rc
);
311 rc
= read_dvsec_vendor(dev
);
314 "Invalid vendor specific DVSEC configuration: %d\n",
319 rc
= validate_function(dev
, fn
);
322 EXPORT_SYMBOL_GPL(ocxl_config_read_function
);
324 static int read_afu_info(struct pci_dev
*dev
, struct ocxl_fn_config
*fn
,
325 int offset
, u32
*data
)
328 unsigned long timeout
= jiffies
+ (HZ
* OCXL_CFG_TIMEOUT
);
329 int pos
= fn
->dvsec_afu_info_pos
;
331 /* Protect 'data valid' bit */
332 if (EXTRACT_BIT(offset
, 31)) {
333 dev_err(&dev
->dev
, "Invalid offset in AFU info DVSEC\n");
337 pci_write_config_dword(dev
, pos
+ OCXL_DVSEC_AFU_INFO_OFF
, offset
);
338 pci_read_config_dword(dev
, pos
+ OCXL_DVSEC_AFU_INFO_OFF
, &val
);
339 while (!EXTRACT_BIT(val
, 31)) {
340 if (time_after_eq(jiffies
, timeout
)) {
342 "Timeout while reading AFU info DVSEC (offset=%d)\n",
347 pci_read_config_dword(dev
, pos
+ OCXL_DVSEC_AFU_INFO_OFF
, &val
);
349 pci_read_config_dword(dev
, pos
+ OCXL_DVSEC_AFU_INFO_DATA
, data
);
354 * read_template_version() - Read the template version from the AFU
355 * @dev: the device for the AFU
356 * @fn: the AFU offsets
357 * @len: outputs the template length
358 * @version: outputs the major<<8,minor version
360 * Returns 0 on success, negative on failure
362 static int read_template_version(struct pci_dev
*dev
, struct ocxl_fn_config
*fn
,
363 u16
*len
, u16
*version
)
369 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_VERSION
, &val32
);
373 *len
= EXTRACT_BITS(val32
, 16, 31);
374 major
= EXTRACT_BITS(val32
, 8, 15);
375 minor
= EXTRACT_BITS(val32
, 0, 7);
376 *version
= (major
<< 8) + minor
;
380 int ocxl_config_check_afu_index(struct pci_dev
*dev
,
381 struct ocxl_fn_config
*fn
, int afu_idx
)
385 u16 len
, expected_len
;
387 pci_write_config_byte(dev
,
388 fn
->dvsec_afu_info_pos
+ OCXL_DVSEC_AFU_INFO_AFU_IDX
,
391 rc
= read_template_version(dev
, fn
, &len
, &templ_version
);
395 /* AFU index map can have holes, in which case we read all 0's */
396 if (!templ_version
&& !len
)
399 dev_dbg(&dev
->dev
, "AFU descriptor template version %d.%d\n",
400 templ_version
>> 8, templ_version
& 0xFF);
402 switch (templ_version
) {
403 case 0x0005: // v0.5 was used prior to the spec approval
405 expected_len
= OCXL_TEMPL_LEN_1_0
;
408 expected_len
= OCXL_TEMPL_LEN_1_1
;
411 dev_warn(&dev
->dev
, "Unknown AFU template version %#x\n",
415 if (len
!= expected_len
)
417 "Unexpected template length %#x in AFU information, expected %#x for version %#x\n",
418 len
, expected_len
, templ_version
);
422 static int read_afu_name(struct pci_dev
*dev
, struct ocxl_fn_config
*fn
,
423 struct ocxl_afu_config
*afu
)
428 BUILD_BUG_ON(OCXL_AFU_NAME_SZ
< OCXL_TEMPL_NAME_LEN
);
429 for (i
= 0; i
< OCXL_TEMPL_NAME_LEN
; i
+= 4) {
430 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_NAME
+ i
, &val
);
433 ptr
= (u32
*) &afu
->name
[i
];
434 *ptr
= le32_to_cpu((__force __le32
) val
);
436 afu
->name
[OCXL_AFU_NAME_SZ
- 1] = '\0'; /* play safe */
440 static int read_afu_mmio(struct pci_dev
*dev
, struct ocxl_fn_config
*fn
,
441 struct ocxl_afu_config
*afu
)
449 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_MMIO_GLOBAL
, &val
);
452 afu
->global_mmio_bar
= EXTRACT_BITS(val
, 0, 2);
453 afu
->global_mmio_offset
= EXTRACT_BITS(val
, 16, 31) << 16;
455 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_MMIO_GLOBAL
+ 4, &val
);
458 afu
->global_mmio_offset
+= (u64
) val
<< 32;
460 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_MMIO_GLOBAL_SZ
, &val
);
463 afu
->global_mmio_size
= val
;
468 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_MMIO_PP
, &val
);
471 afu
->pp_mmio_bar
= EXTRACT_BITS(val
, 0, 2);
472 afu
->pp_mmio_offset
= EXTRACT_BITS(val
, 16, 31) << 16;
474 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_MMIO_PP
+ 4, &val
);
477 afu
->pp_mmio_offset
+= (u64
) val
<< 32;
479 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_MMIO_PP_SZ
, &val
);
482 afu
->pp_mmio_stride
= val
;
487 static int read_afu_control(struct pci_dev
*dev
, struct ocxl_afu_config
*afu
)
493 pos
= find_dvsec_afu_ctrl(dev
, afu
->idx
);
495 dev_err(&dev
->dev
, "Can't find AFU control DVSEC for AFU %d\n",
499 afu
->dvsec_afu_control_pos
= pos
;
501 pci_read_config_byte(dev
, pos
+ OCXL_DVSEC_AFU_CTRL_PASID_SUP
, &val8
);
502 afu
->pasid_supported_log
= EXTRACT_BITS(val8
, 0, 4);
504 pci_read_config_word(dev
, pos
+ OCXL_DVSEC_AFU_CTRL_ACTAG_SUP
, &val16
);
505 afu
->actag_supported
= EXTRACT_BITS(val16
, 0, 11);
509 static bool char_allowed(int c
)
512 * Permitted Characters : Alphanumeric, hyphen, underscore, comma
514 if ((c
>= 0x30 && c
<= 0x39) /* digits */ ||
515 (c
>= 0x41 && c
<= 0x5A) /* upper case */ ||
516 (c
>= 0x61 && c
<= 0x7A) /* lower case */ ||
525 static int validate_afu(struct pci_dev
*dev
, struct ocxl_afu_config
*afu
)
530 dev_err(&dev
->dev
, "Empty AFU name\n");
533 for (i
= 0; i
< OCXL_TEMPL_NAME_LEN
; i
++) {
534 if (!char_allowed(afu
->name
[i
])) {
536 "Invalid character in AFU name\n");
541 if (afu
->global_mmio_bar
!= 0 &&
542 afu
->global_mmio_bar
!= 2 &&
543 afu
->global_mmio_bar
!= 4) {
544 dev_err(&dev
->dev
, "Invalid global MMIO bar number\n");
547 if (afu
->pp_mmio_bar
!= 0 &&
548 afu
->pp_mmio_bar
!= 2 &&
549 afu
->pp_mmio_bar
!= 4) {
550 dev_err(&dev
->dev
, "Invalid per-process MMIO bar number\n");
557 * read_afu_lpc_memory_info() - Populate AFU metadata regarding LPC memory
558 * @dev: the device for the AFU
559 * @fn: the AFU offsets
560 * @afu: the AFU struct to populate the LPC metadata into
562 * Returns 0 on success, negative on failure
564 static int read_afu_lpc_memory_info(struct pci_dev
*dev
,
565 struct ocxl_fn_config
*fn
,
566 struct ocxl_afu_config
*afu
)
572 u64 total_mem_size
= 0;
573 u64 lpc_mem_size
= 0;
575 afu
->lpc_mem_offset
= 0;
576 afu
->lpc_mem_size
= 0;
577 afu
->special_purpose_mem_offset
= 0;
578 afu
->special_purpose_mem_size
= 0;
580 * For AFUs following template v1.0, the LPC memory covers the
581 * total memory. Its size is a power of 2.
583 * For AFUs with template >= v1.01, the total memory size is
584 * still a power of 2, but it is split in 2 parts:
585 * - the LPC memory, whose size can now be anything
586 * - the remainder memory is a special purpose memory, whose
587 * definition is AFU-dependent. It is not accessible through
588 * the usual commands for LPC memory
590 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_ALL_MEM_SZ
, &val32
);
594 val32
= EXTRACT_BITS(val32
, 0, 7);
596 return 0; /* No LPC memory */
599 * The configuration space spec allows for a memory size of up
602 * Current generation hardware uses 56-bit physical addresses,
603 * but we won't be able to get near close to that, as we won't
604 * have a hole big enough in the memory map. Let it pass in
605 * the driver for now. We'll get an error from the firmware
606 * when trying to configure something too big.
608 total_mem_size
= 1ull << val32
;
610 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_LPC_MEM_START
, &val32
);
614 afu
->lpc_mem_offset
= val32
;
616 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_LPC_MEM_START
+ 4, &val32
);
620 afu
->lpc_mem_offset
|= (u64
) val32
<< 32;
622 rc
= read_template_version(dev
, fn
, &templ_len
, &templ_version
);
626 if (templ_version
>= 0x0101) {
627 rc
= read_afu_info(dev
, fn
,
628 OCXL_DVSEC_TEMPL_LPC_MEM_SZ
, &val32
);
631 lpc_mem_size
= val32
;
633 rc
= read_afu_info(dev
, fn
,
634 OCXL_DVSEC_TEMPL_LPC_MEM_SZ
+ 4, &val32
);
637 lpc_mem_size
|= (u64
) val32
<< 32;
639 lpc_mem_size
= total_mem_size
;
641 afu
->lpc_mem_size
= lpc_mem_size
;
643 if (lpc_mem_size
< total_mem_size
) {
644 afu
->special_purpose_mem_offset
=
645 afu
->lpc_mem_offset
+ lpc_mem_size
;
646 afu
->special_purpose_mem_size
=
647 total_mem_size
- lpc_mem_size
;
652 int ocxl_config_read_afu(struct pci_dev
*dev
, struct ocxl_fn_config
*fn
,
653 struct ocxl_afu_config
*afu
, u8 afu_idx
)
659 * First, we need to write the AFU idx for the AFU we want to
662 WARN_ON((afu_idx
& OCXL_DVSEC_AFU_IDX_MASK
) != afu_idx
);
664 pci_write_config_byte(dev
,
665 fn
->dvsec_afu_info_pos
+ OCXL_DVSEC_AFU_INFO_AFU_IDX
,
668 rc
= read_afu_name(dev
, fn
, afu
);
672 rc
= read_afu_info(dev
, fn
, OCXL_DVSEC_TEMPL_AFU_VERSION
, &val32
);
675 afu
->version_major
= EXTRACT_BITS(val32
, 24, 31);
676 afu
->version_minor
= EXTRACT_BITS(val32
, 16, 23);
677 afu
->afuc_type
= EXTRACT_BITS(val32
, 14, 15);
678 afu
->afum_type
= EXTRACT_BITS(val32
, 12, 13);
679 afu
->profile
= EXTRACT_BITS(val32
, 0, 7);
681 rc
= read_afu_mmio(dev
, fn
, afu
);
685 rc
= read_afu_lpc_memory_info(dev
, fn
, afu
);
689 rc
= read_afu_control(dev
, afu
);
693 dev_dbg(&dev
->dev
, "AFU configuration:\n");
694 dev_dbg(&dev
->dev
, " name = %s\n", afu
->name
);
695 dev_dbg(&dev
->dev
, " version = %d.%d\n", afu
->version_major
,
697 dev_dbg(&dev
->dev
, " global mmio bar = %hhu\n", afu
->global_mmio_bar
);
698 dev_dbg(&dev
->dev
, " global mmio offset = %#llx\n",
699 afu
->global_mmio_offset
);
700 dev_dbg(&dev
->dev
, " global mmio size = %#x\n", afu
->global_mmio_size
);
701 dev_dbg(&dev
->dev
, " pp mmio bar = %hhu\n", afu
->pp_mmio_bar
);
702 dev_dbg(&dev
->dev
, " pp mmio offset = %#llx\n", afu
->pp_mmio_offset
);
703 dev_dbg(&dev
->dev
, " pp mmio stride = %#x\n", afu
->pp_mmio_stride
);
704 dev_dbg(&dev
->dev
, " lpc_mem offset = %#llx\n", afu
->lpc_mem_offset
);
705 dev_dbg(&dev
->dev
, " lpc_mem size = %#llx\n", afu
->lpc_mem_size
);
706 dev_dbg(&dev
->dev
, " special purpose mem offset = %#llx\n",
707 afu
->special_purpose_mem_offset
);
708 dev_dbg(&dev
->dev
, " special purpose mem size = %#llx\n",
709 afu
->special_purpose_mem_size
);
710 dev_dbg(&dev
->dev
, " pasid supported (log) = %u\n",
711 afu
->pasid_supported_log
);
712 dev_dbg(&dev
->dev
, " actag supported = %u\n",
713 afu
->actag_supported
);
715 rc
= validate_afu(dev
, afu
);
718 EXPORT_SYMBOL_GPL(ocxl_config_read_afu
);
720 int ocxl_config_get_actag_info(struct pci_dev
*dev
, u16
*base
, u16
*enabled
,
726 * This is really a simple wrapper for the kernel API, to
727 * avoid an external driver using ocxl as a library to call
728 * platform-dependent code
730 rc
= pnv_ocxl_get_actag(dev
, base
, enabled
, supported
);
732 dev_err(&dev
->dev
, "Can't get actag for device: %d\n", rc
);
737 EXPORT_SYMBOL_GPL(ocxl_config_get_actag_info
);
739 void ocxl_config_set_afu_actag(struct pci_dev
*dev
, int pos
, int actag_base
,
744 val
= actag_count
& OCXL_DVSEC_ACTAG_MASK
;
745 pci_write_config_byte(dev
, pos
+ OCXL_DVSEC_AFU_CTRL_ACTAG_EN
, val
);
747 val
= actag_base
& OCXL_DVSEC_ACTAG_MASK
;
748 pci_write_config_dword(dev
, pos
+ OCXL_DVSEC_AFU_CTRL_ACTAG_BASE
, val
);
750 EXPORT_SYMBOL_GPL(ocxl_config_set_afu_actag
);
752 int ocxl_config_get_pasid_info(struct pci_dev
*dev
, int *count
)
754 return pnv_ocxl_get_pasid_count(dev
, count
);
757 void ocxl_config_set_afu_pasid(struct pci_dev
*dev
, int pos
, int pasid_base
,
763 val8
= pasid_count_log
& OCXL_DVSEC_PASID_LOG_MASK
;
764 pci_write_config_byte(dev
, pos
+ OCXL_DVSEC_AFU_CTRL_PASID_EN
, val8
);
766 pci_read_config_dword(dev
, pos
+ OCXL_DVSEC_AFU_CTRL_PASID_BASE
,
768 val32
&= ~OCXL_DVSEC_PASID_MASK
;
769 val32
|= pasid_base
& OCXL_DVSEC_PASID_MASK
;
770 pci_write_config_dword(dev
, pos
+ OCXL_DVSEC_AFU_CTRL_PASID_BASE
,
773 EXPORT_SYMBOL_GPL(ocxl_config_set_afu_pasid
);
775 void ocxl_config_set_afu_state(struct pci_dev
*dev
, int pos
, int enable
)
779 pci_read_config_byte(dev
, pos
+ OCXL_DVSEC_AFU_CTRL_ENABLE
, &val
);
784 pci_write_config_byte(dev
, pos
+ OCXL_DVSEC_AFU_CTRL_ENABLE
, val
);
786 EXPORT_SYMBOL_GPL(ocxl_config_set_afu_state
);
788 int ocxl_config_set_TL(struct pci_dev
*dev
, int tl_dvsec
)
798 * Skip on function != 0, as the TL can only be defined on 0
800 if (PCI_FUNC(dev
->devfn
) != 0)
803 recv_rate
= kzalloc(PNV_OCXL_TL_RATE_BUF_SIZE
, GFP_KERNEL
);
807 * The spec defines 64 templates for messages in the
808 * Transaction Layer (TL).
810 * The host and device each support a subset, so we need to
811 * configure the transmitters on each side to send only
812 * templates the receiver understands, at a rate the receiver
813 * can process. Per the spec, template 0 must be supported by
814 * everybody. That's the template which has been used by the
815 * host and device so far.
817 * The sending rate limit must be set before the template is
824 rc
= pnv_ocxl_get_tl_cap(dev
, &recv_cap
, recv_rate
,
825 PNV_OCXL_TL_RATE_BUF_SIZE
);
829 for (i
= 0; i
< PNV_OCXL_TL_RATE_BUF_SIZE
; i
+= 4) {
830 be32ptr
= (__be32
*) &recv_rate
[i
];
831 pci_write_config_dword(dev
,
832 tl_dvsec
+ OCXL_DVSEC_TL_SEND_RATE
+ i
,
833 be32_to_cpu(*be32ptr
));
835 val
= recv_cap
>> 32;
836 pci_write_config_dword(dev
, tl_dvsec
+ OCXL_DVSEC_TL_SEND_CAP
, val
);
837 val
= recv_cap
& GENMASK(31, 0);
838 pci_write_config_dword(dev
, tl_dvsec
+ OCXL_DVSEC_TL_SEND_CAP
+ 4, val
);
843 for (i
= 0; i
< PNV_OCXL_TL_RATE_BUF_SIZE
; i
+= 4) {
844 pci_read_config_dword(dev
,
845 tl_dvsec
+ OCXL_DVSEC_TL_RECV_RATE
+ i
,
847 be32ptr
= (__be32
*) &recv_rate
[i
];
848 *be32ptr
= cpu_to_be32(val
);
850 pci_read_config_dword(dev
, tl_dvsec
+ OCXL_DVSEC_TL_RECV_CAP
, &val
);
851 recv_cap
= (long) val
<< 32;
852 pci_read_config_dword(dev
, tl_dvsec
+ OCXL_DVSEC_TL_RECV_CAP
+ 4, &val
);
855 rc
= pnv_ocxl_set_tl_conf(dev
, recv_cap
, __pa(recv_rate
),
856 PNV_OCXL_TL_RATE_BUF_SIZE
);
861 * Opencapi commands needing to be retried are classified per
862 * the TL in 2 groups: short and long commands.
864 * The short back off timer it not used for now. It will be
867 * The long back off timer is typically used when an AFU hits
868 * a page fault but the NPU is already processing one. So the
869 * AFU needs to wait before it can resubmit. Having a value
870 * too low doesn't break anything, but can generate extra
871 * traffic on the link.
872 * We set it to 1.6 us for now. It's shorter than, but in the
873 * same order of magnitude as the time spent to process a page
876 timers
= 0x2 << 4; /* long timer = 1.6 us */
877 pci_write_config_byte(dev
, tl_dvsec
+ OCXL_DVSEC_TL_BACKOFF_TIMERS
,
885 EXPORT_SYMBOL_GPL(ocxl_config_set_TL
);
887 int ocxl_config_terminate_pasid(struct pci_dev
*dev
, int afu_control
, int pasid
)
890 unsigned long timeout
;
892 pci_read_config_dword(dev
, afu_control
+ OCXL_DVSEC_AFU_CTRL_TERM_PASID
,
894 if (EXTRACT_BIT(val
, 20)) {
896 "Can't terminate PASID %#x, previous termination didn't complete\n",
901 val
&= ~OCXL_DVSEC_PASID_MASK
;
902 val
|= pasid
& OCXL_DVSEC_PASID_MASK
;
904 pci_write_config_dword(dev
,
905 afu_control
+ OCXL_DVSEC_AFU_CTRL_TERM_PASID
,
908 timeout
= jiffies
+ (HZ
* OCXL_CFG_TIMEOUT
);
909 pci_read_config_dword(dev
, afu_control
+ OCXL_DVSEC_AFU_CTRL_TERM_PASID
,
911 while (EXTRACT_BIT(val
, 20)) {
912 if (time_after_eq(jiffies
, timeout
)) {
914 "Timeout while waiting for AFU to terminate PASID %#x\n",
919 pci_read_config_dword(dev
,
920 afu_control
+ OCXL_DVSEC_AFU_CTRL_TERM_PASID
,
925 EXPORT_SYMBOL_GPL(ocxl_config_terminate_pasid
);
927 void ocxl_config_set_actag(struct pci_dev
*dev
, int func_dvsec
, u32 tag_first
,
932 val
= (tag_first
& OCXL_DVSEC_ACTAG_MASK
) << 16;
933 val
|= tag_count
& OCXL_DVSEC_ACTAG_MASK
;
934 pci_write_config_dword(dev
, func_dvsec
+ OCXL_DVSEC_FUNC_OFF_ACTAG
,
937 EXPORT_SYMBOL_GPL(ocxl_config_set_actag
);