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_MEM_SZ 0x3C
24 #define OCXL_DVSEC_TEMPL_WWID 0x40
26 #define OCXL_MAX_AFU_PER_FUNCTION 64
27 #define OCXL_TEMPL_LEN 0x58
28 #define OCXL_TEMPL_NAME_LEN 24
29 #define OCXL_CFG_TIMEOUT 3
31 static int find_dvsec(struct pci_dev *dev, int dvsec_id)
36 while ((vsec = pci_find_next_ext_capability(dev, vsec,
37 OCXL_EXT_CAP_ID_DVSEC))) {
38 pci_read_config_word(dev, vsec + OCXL_DVSEC_VENDOR_OFFSET,
40 pci_read_config_word(dev, vsec + OCXL_DVSEC_ID_OFFSET, &id);
41 if (vendor == PCI_VENDOR_ID_IBM && id == dvsec_id)
47 static int find_dvsec_afu_ctrl(struct pci_dev *dev, u8 afu_idx)
53 while ((vsec = pci_find_next_ext_capability(dev, vsec,
54 OCXL_EXT_CAP_ID_DVSEC))) {
55 pci_read_config_word(dev, vsec + OCXL_DVSEC_VENDOR_OFFSET,
57 pci_read_config_word(dev, vsec + OCXL_DVSEC_ID_OFFSET, &id);
59 if (vendor == PCI_VENDOR_ID_IBM &&
60 id == OCXL_DVSEC_AFU_CTRL_ID) {
61 pci_read_config_byte(dev,
62 vsec + OCXL_DVSEC_AFU_CTRL_AFU_IDX,
71 static void read_pasid(struct pci_dev *dev, struct ocxl_fn_config *fn)
76 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_PASID);
79 * PASID capability is not mandatory, but there
80 * shouldn't be any AFU
82 dev_dbg(&dev->dev, "Function doesn't require any PASID\n");
83 fn->max_pasid_log = -1;
86 pci_read_config_word(dev, pos + PCI_PASID_CAP, &val);
87 fn->max_pasid_log = EXTRACT_BITS(val, 8, 12);
90 dev_dbg(&dev->dev, "PASID capability:\n");
91 dev_dbg(&dev->dev, " Max PASID log = %d\n", fn->max_pasid_log);
94 static int read_dvsec_tl(struct pci_dev *dev, struct ocxl_fn_config *fn)
98 pos = find_dvsec(dev, OCXL_DVSEC_TL_ID);
99 if (!pos && PCI_FUNC(dev->devfn) == 0) {
100 dev_err(&dev->dev, "Can't find TL DVSEC\n");
103 if (pos && PCI_FUNC(dev->devfn) != 0) {
104 dev_err(&dev->dev, "TL DVSEC is only allowed on function 0\n");
107 fn->dvsec_tl_pos = pos;
111 static int read_dvsec_function(struct pci_dev *dev, struct ocxl_fn_config *fn)
113 int pos, afu_present;
116 pos = find_dvsec(dev, OCXL_DVSEC_FUNC_ID);
118 dev_err(&dev->dev, "Can't find function DVSEC\n");
121 fn->dvsec_function_pos = pos;
123 pci_read_config_dword(dev, pos + OCXL_DVSEC_FUNC_OFF_INDEX, &val);
124 afu_present = EXTRACT_BIT(val, 31);
126 fn->max_afu_index = -1;
127 dev_dbg(&dev->dev, "Function doesn't define any AFU\n");
130 fn->max_afu_index = EXTRACT_BITS(val, 24, 29);
133 dev_dbg(&dev->dev, "Function DVSEC:\n");
134 dev_dbg(&dev->dev, " Max AFU index = %d\n", fn->max_afu_index);
138 static int read_dvsec_afu_info(struct pci_dev *dev, struct ocxl_fn_config *fn)
142 if (fn->max_afu_index < 0) {
143 fn->dvsec_afu_info_pos = -1;
147 pos = find_dvsec(dev, OCXL_DVSEC_AFU_INFO_ID);
149 dev_err(&dev->dev, "Can't find AFU information DVSEC\n");
152 fn->dvsec_afu_info_pos = pos;
156 static int read_dvsec_vendor(struct pci_dev *dev)
162 * vendor specific DVSEC is optional
164 * It's currently only used on function 0 to specify the
165 * version of some logic blocks. Some older images may not
166 * even have it so we ignore any errors
168 if (PCI_FUNC(dev->devfn) != 0)
171 pos = find_dvsec(dev, OCXL_DVSEC_VENDOR_ID);
175 pci_read_config_dword(dev, pos + OCXL_DVSEC_VENDOR_CFG_VERS, &cfg);
176 pci_read_config_dword(dev, pos + OCXL_DVSEC_VENDOR_TLX_VERS, &tlx);
177 pci_read_config_dword(dev, pos + OCXL_DVSEC_VENDOR_DLX_VERS, &dlx);
179 dev_dbg(&dev->dev, "Vendor specific DVSEC:\n");
180 dev_dbg(&dev->dev, " CFG version = 0x%x\n", cfg);
181 dev_dbg(&dev->dev, " TLX version = 0x%x\n", tlx);
182 dev_dbg(&dev->dev, " DLX version = 0x%x\n", dlx);
186 static int validate_function(struct pci_dev *dev, struct ocxl_fn_config *fn)
188 if (fn->max_pasid_log == -1 && fn->max_afu_index >= 0) {
190 "AFUs are defined but no PASIDs are requested\n");
194 if (fn->max_afu_index > OCXL_MAX_AFU_PER_FUNCTION) {
196 "Max AFU index out of architectural limit (%d vs %d)\n",
197 fn->max_afu_index, OCXL_MAX_AFU_PER_FUNCTION);
203 int ocxl_config_read_function(struct pci_dev *dev, struct ocxl_fn_config *fn)
209 rc = read_dvsec_tl(dev, fn);
212 "Invalid Transaction Layer DVSEC configuration: %d\n",
217 rc = read_dvsec_function(dev, fn);
220 "Invalid Function DVSEC configuration: %d\n", rc);
224 rc = read_dvsec_afu_info(dev, fn);
226 dev_err(&dev->dev, "Invalid AFU configuration: %d\n", rc);
230 rc = read_dvsec_vendor(dev);
233 "Invalid vendor specific DVSEC configuration: %d\n",
238 rc = validate_function(dev, fn);
241 EXPORT_SYMBOL_GPL(ocxl_config_read_function);
243 static int read_afu_info(struct pci_dev *dev, struct ocxl_fn_config *fn,
244 int offset, u32 *data)
247 unsigned long timeout = jiffies + (HZ * OCXL_CFG_TIMEOUT);
248 int pos = fn->dvsec_afu_info_pos;
250 /* Protect 'data valid' bit */
251 if (EXTRACT_BIT(offset, 31)) {
252 dev_err(&dev->dev, "Invalid offset in AFU info DVSEC\n");
256 pci_write_config_dword(dev, pos + OCXL_DVSEC_AFU_INFO_OFF, offset);
257 pci_read_config_dword(dev, pos + OCXL_DVSEC_AFU_INFO_OFF, &val);
258 while (!EXTRACT_BIT(val, 31)) {
259 if (time_after_eq(jiffies, timeout)) {
261 "Timeout while reading AFU info DVSEC (offset=%d)\n",
266 pci_read_config_dword(dev, pos + OCXL_DVSEC_AFU_INFO_OFF, &val);
268 pci_read_config_dword(dev, pos + OCXL_DVSEC_AFU_INFO_DATA, data);
272 int ocxl_config_check_afu_index(struct pci_dev *dev,
273 struct ocxl_fn_config *fn, int afu_idx)
276 int rc, templ_major, templ_minor, len;
278 pci_write_config_byte(dev,
279 fn->dvsec_afu_info_pos + OCXL_DVSEC_AFU_INFO_AFU_IDX,
281 rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_VERSION, &val);
285 /* AFU index map can have holes */
289 templ_major = EXTRACT_BITS(val, 8, 15);
290 templ_minor = EXTRACT_BITS(val, 0, 7);
291 dev_dbg(&dev->dev, "AFU descriptor template version %d.%d\n",
292 templ_major, templ_minor);
294 len = EXTRACT_BITS(val, 16, 31);
295 if (len != OCXL_TEMPL_LEN) {
297 "Unexpected template length in AFU information (%#x)\n",
303 static int read_afu_name(struct pci_dev *dev, struct ocxl_fn_config *fn,
304 struct ocxl_afu_config *afu)
309 BUILD_BUG_ON(OCXL_AFU_NAME_SZ < OCXL_TEMPL_NAME_LEN);
310 for (i = 0; i < OCXL_TEMPL_NAME_LEN; i += 4) {
311 rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_NAME + i, &val);
314 ptr = (u32 *) &afu->name[i];
315 *ptr = le32_to_cpu((__force __le32) val);
317 afu->name[OCXL_AFU_NAME_SZ - 1] = '\0'; /* play safe */
321 static int read_afu_mmio(struct pci_dev *dev, struct ocxl_fn_config *fn,
322 struct ocxl_afu_config *afu)
330 rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_GLOBAL, &val);
333 afu->global_mmio_bar = EXTRACT_BITS(val, 0, 2);
334 afu->global_mmio_offset = EXTRACT_BITS(val, 16, 31) << 16;
336 rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_GLOBAL + 4, &val);
339 afu->global_mmio_offset += (u64) val << 32;
341 rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_GLOBAL_SZ, &val);
344 afu->global_mmio_size = val;
349 rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_PP, &val);
352 afu->pp_mmio_bar = EXTRACT_BITS(val, 0, 2);
353 afu->pp_mmio_offset = EXTRACT_BITS(val, 16, 31) << 16;
355 rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_PP + 4, &val);
358 afu->pp_mmio_offset += (u64) val << 32;
360 rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_PP_SZ, &val);
363 afu->pp_mmio_stride = val;
368 static int read_afu_control(struct pci_dev *dev, struct ocxl_afu_config *afu)
374 pos = find_dvsec_afu_ctrl(dev, afu->idx);
376 dev_err(&dev->dev, "Can't find AFU control DVSEC for AFU %d\n",
380 afu->dvsec_afu_control_pos = pos;
382 pci_read_config_byte(dev, pos + OCXL_DVSEC_AFU_CTRL_PASID_SUP, &val8);
383 afu->pasid_supported_log = EXTRACT_BITS(val8, 0, 4);
385 pci_read_config_word(dev, pos + OCXL_DVSEC_AFU_CTRL_ACTAG_SUP, &val16);
386 afu->actag_supported = EXTRACT_BITS(val16, 0, 11);
390 static bool char_allowed(int c)
393 * Permitted Characters : Alphanumeric, hyphen, underscore, comma
395 if ((c >= 0x30 && c <= 0x39) /* digits */ ||
396 (c >= 0x41 && c <= 0x5A) /* upper case */ ||
397 (c >= 0x61 && c <= 0x7A) /* lower case */ ||
406 static int validate_afu(struct pci_dev *dev, struct ocxl_afu_config *afu)
411 dev_err(&dev->dev, "Empty AFU name\n");
414 for (i = 0; i < OCXL_TEMPL_NAME_LEN; i++) {
415 if (!char_allowed(afu->name[i])) {
417 "Invalid character in AFU name\n");
422 if (afu->global_mmio_bar != 0 &&
423 afu->global_mmio_bar != 2 &&
424 afu->global_mmio_bar != 4) {
425 dev_err(&dev->dev, "Invalid global MMIO bar number\n");
428 if (afu->pp_mmio_bar != 0 &&
429 afu->pp_mmio_bar != 2 &&
430 afu->pp_mmio_bar != 4) {
431 dev_err(&dev->dev, "Invalid per-process MMIO bar number\n");
437 int ocxl_config_read_afu(struct pci_dev *dev, struct ocxl_fn_config *fn,
438 struct ocxl_afu_config *afu, u8 afu_idx)
444 * First, we need to write the AFU idx for the AFU we want to
447 WARN_ON((afu_idx & OCXL_DVSEC_AFU_IDX_MASK) != afu_idx);
449 pci_write_config_byte(dev,
450 fn->dvsec_afu_info_pos + OCXL_DVSEC_AFU_INFO_AFU_IDX,
453 rc = read_afu_name(dev, fn, afu);
457 rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_AFU_VERSION, &val32);
460 afu->version_major = EXTRACT_BITS(val32, 24, 31);
461 afu->version_minor = EXTRACT_BITS(val32, 16, 23);
462 afu->afuc_type = EXTRACT_BITS(val32, 14, 15);
463 afu->afum_type = EXTRACT_BITS(val32, 12, 13);
464 afu->profile = EXTRACT_BITS(val32, 0, 7);
466 rc = read_afu_mmio(dev, fn, afu);
470 rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MEM_SZ, &val32);
473 afu->log_mem_size = EXTRACT_BITS(val32, 0, 7);
475 rc = read_afu_control(dev, afu);
479 dev_dbg(&dev->dev, "AFU configuration:\n");
480 dev_dbg(&dev->dev, " name = %s\n", afu->name);
481 dev_dbg(&dev->dev, " version = %d.%d\n", afu->version_major,
483 dev_dbg(&dev->dev, " global mmio bar = %hhu\n", afu->global_mmio_bar);
484 dev_dbg(&dev->dev, " global mmio offset = %#llx\n",
485 afu->global_mmio_offset);
486 dev_dbg(&dev->dev, " global mmio size = %#x\n", afu->global_mmio_size);
487 dev_dbg(&dev->dev, " pp mmio bar = %hhu\n", afu->pp_mmio_bar);
488 dev_dbg(&dev->dev, " pp mmio offset = %#llx\n", afu->pp_mmio_offset);
489 dev_dbg(&dev->dev, " pp mmio stride = %#x\n", afu->pp_mmio_stride);
490 dev_dbg(&dev->dev, " mem size (log) = %hhu\n", afu->log_mem_size);
491 dev_dbg(&dev->dev, " pasid supported (log) = %u\n",
492 afu->pasid_supported_log);
493 dev_dbg(&dev->dev, " actag supported = %u\n",
494 afu->actag_supported);
496 rc = validate_afu(dev, afu);
499 EXPORT_SYMBOL_GPL(ocxl_config_read_afu);
501 int ocxl_config_get_actag_info(struct pci_dev *dev, u16 *base, u16 *enabled,
507 * This is really a simple wrapper for the kernel API, to
508 * avoid an external driver using ocxl as a library to call
509 * platform-dependent code
511 rc = pnv_ocxl_get_actag(dev, base, enabled, supported);
513 dev_err(&dev->dev, "Can't get actag for device: %d\n", rc);
518 EXPORT_SYMBOL_GPL(ocxl_config_get_actag_info);
520 void ocxl_config_set_afu_actag(struct pci_dev *dev, int pos, int actag_base,
525 val = actag_count & OCXL_DVSEC_ACTAG_MASK;
526 pci_write_config_byte(dev, pos + OCXL_DVSEC_AFU_CTRL_ACTAG_EN, val);
528 val = actag_base & OCXL_DVSEC_ACTAG_MASK;
529 pci_write_config_dword(dev, pos + OCXL_DVSEC_AFU_CTRL_ACTAG_BASE, val);
531 EXPORT_SYMBOL_GPL(ocxl_config_set_afu_actag);
533 int ocxl_config_get_pasid_info(struct pci_dev *dev, int *count)
535 return pnv_ocxl_get_pasid_count(dev, count);
538 void ocxl_config_set_afu_pasid(struct pci_dev *dev, int pos, int pasid_base,
544 val8 = pasid_count_log & OCXL_DVSEC_PASID_LOG_MASK;
545 pci_write_config_byte(dev, pos + OCXL_DVSEC_AFU_CTRL_PASID_EN, val8);
547 pci_read_config_dword(dev, pos + OCXL_DVSEC_AFU_CTRL_PASID_BASE,
549 val32 &= ~OCXL_DVSEC_PASID_MASK;
550 val32 |= pasid_base & OCXL_DVSEC_PASID_MASK;
551 pci_write_config_dword(dev, pos + OCXL_DVSEC_AFU_CTRL_PASID_BASE,
554 EXPORT_SYMBOL_GPL(ocxl_config_set_afu_pasid);
556 void ocxl_config_set_afu_state(struct pci_dev *dev, int pos, int enable)
560 pci_read_config_byte(dev, pos + OCXL_DVSEC_AFU_CTRL_ENABLE, &val);
565 pci_write_config_byte(dev, pos + OCXL_DVSEC_AFU_CTRL_ENABLE, val);
567 EXPORT_SYMBOL_GPL(ocxl_config_set_afu_state);
569 int ocxl_config_set_TL(struct pci_dev *dev, int tl_dvsec)
579 * Skip on function != 0, as the TL can only be defined on 0
581 if (PCI_FUNC(dev->devfn) != 0)
584 recv_rate = kzalloc(PNV_OCXL_TL_RATE_BUF_SIZE, GFP_KERNEL);
588 * The spec defines 64 templates for messages in the
589 * Transaction Layer (TL).
591 * The host and device each support a subset, so we need to
592 * configure the transmitters on each side to send only
593 * templates the receiver understands, at a rate the receiver
594 * can process. Per the spec, template 0 must be supported by
595 * everybody. That's the template which has been used by the
596 * host and device so far.
598 * The sending rate limit must be set before the template is
605 rc = pnv_ocxl_get_tl_cap(dev, &recv_cap, recv_rate,
606 PNV_OCXL_TL_RATE_BUF_SIZE);
610 for (i = 0; i < PNV_OCXL_TL_RATE_BUF_SIZE; i += 4) {
611 be32ptr = (__be32 *) &recv_rate[i];
612 pci_write_config_dword(dev,
613 tl_dvsec + OCXL_DVSEC_TL_SEND_RATE + i,
614 be32_to_cpu(*be32ptr));
616 val = recv_cap >> 32;
617 pci_write_config_dword(dev, tl_dvsec + OCXL_DVSEC_TL_SEND_CAP, val);
618 val = recv_cap & GENMASK(31, 0);
619 pci_write_config_dword(dev, tl_dvsec + OCXL_DVSEC_TL_SEND_CAP + 4, val);
624 for (i = 0; i < PNV_OCXL_TL_RATE_BUF_SIZE; i += 4) {
625 pci_read_config_dword(dev,
626 tl_dvsec + OCXL_DVSEC_TL_RECV_RATE + i,
628 be32ptr = (__be32 *) &recv_rate[i];
629 *be32ptr = cpu_to_be32(val);
631 pci_read_config_dword(dev, tl_dvsec + OCXL_DVSEC_TL_RECV_CAP, &val);
632 recv_cap = (long) val << 32;
633 pci_read_config_dword(dev, tl_dvsec + OCXL_DVSEC_TL_RECV_CAP + 4, &val);
636 rc = pnv_ocxl_set_tl_conf(dev, recv_cap, __pa(recv_rate),
637 PNV_OCXL_TL_RATE_BUF_SIZE);
642 * Opencapi commands needing to be retried are classified per
643 * the TL in 2 groups: short and long commands.
645 * The short back off timer it not used for now. It will be
648 * The long back off timer is typically used when an AFU hits
649 * a page fault but the NPU is already processing one. So the
650 * AFU needs to wait before it can resubmit. Having a value
651 * too low doesn't break anything, but can generate extra
652 * traffic on the link.
653 * We set it to 1.6 us for now. It's shorter than, but in the
654 * same order of magnitude as the time spent to process a page
657 timers = 0x2 << 4; /* long timer = 1.6 us */
658 pci_write_config_byte(dev, tl_dvsec + OCXL_DVSEC_TL_BACKOFF_TIMERS,
666 EXPORT_SYMBOL_GPL(ocxl_config_set_TL);
668 int ocxl_config_terminate_pasid(struct pci_dev *dev, int afu_control, int pasid)
671 unsigned long timeout;
673 pci_read_config_dword(dev, afu_control + OCXL_DVSEC_AFU_CTRL_TERM_PASID,
675 if (EXTRACT_BIT(val, 20)) {
677 "Can't terminate PASID %#x, previous termination didn't complete\n",
682 val &= ~OCXL_DVSEC_PASID_MASK;
683 val |= pasid & OCXL_DVSEC_PASID_MASK;
685 pci_write_config_dword(dev,
686 afu_control + OCXL_DVSEC_AFU_CTRL_TERM_PASID,
689 timeout = jiffies + (HZ * OCXL_CFG_TIMEOUT);
690 pci_read_config_dword(dev, afu_control + OCXL_DVSEC_AFU_CTRL_TERM_PASID,
692 while (EXTRACT_BIT(val, 20)) {
693 if (time_after_eq(jiffies, timeout)) {
695 "Timeout while waiting for AFU to terminate PASID %#x\n",
700 pci_read_config_dword(dev,
701 afu_control + OCXL_DVSEC_AFU_CTRL_TERM_PASID,
706 EXPORT_SYMBOL_GPL(ocxl_config_terminate_pasid);
708 void ocxl_config_set_actag(struct pci_dev *dev, int func_dvsec, u32 tag_first,
713 val = (tag_first & OCXL_DVSEC_ACTAG_MASK) << 16;
714 val |= tag_count & OCXL_DVSEC_ACTAG_MASK;
715 pci_write_config_dword(dev, func_dvsec + OCXL_DVSEC_FUNC_OFF_ACTAG,
718 EXPORT_SYMBOL_GPL(ocxl_config_set_actag);