Skip to content

Commit b087e61

Browse files
Ian Munsiempe
authored andcommitted
cxl: Export optional AFU configuration record in sysfs
An AFU may optionally contain one or more PCIe like configuration records, which can be used to identify the AFU. This patch adds support for exposing the raw config space and the vendor, device and class code under sysfs. These will appear in a subdirectory of the AFU device corresponding with the configuration record number, e.g. cat /sys/class/cxl/afu0.0/cr0/vendor 0x1014 cat /sys/class/cxl/afu0.0/cr0/device 0x4350 cat /sys/class/cxl/afu0.0/cr0/class 0x120000 hexdump -C /sys/class/cxl/afu0.0/cr0/config 00000000 14 10 50 43 00 00 00 00 06 00 00 12 00 00 00 00 |..PC............| 00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 00000100 These files behave in much the same way as the equivalent files for PCI devices, with one exception being that the config file is currently read-only and restricted to the root user. It is not necessarily required to be this strict, but we currently do not have a compelling use-case to make it writable and/or world-readable, so I erred on the side of being restrictive. Signed-off-by: Ian Munsie <imunsie@au1.ibm.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent c2c896b commit b087e61

4 files changed

Lines changed: 242 additions & 10 deletions

File tree

‎Documentation/ABI/testing/sysfs-class-cxl‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,43 @@ Description: read only
8181
this this kernel supports.
8282

8383

84+
AFU configuration records (eg. /sys/class/cxl/afu0.0/cr0):
85+
86+
An AFU may optionally export one or more PCIe like configuration records, known
87+
as AFU configuration records, which will show up here (if present).
88+
89+
What: /sys/class/cxl/<afu>/cr<config num>/vendor
90+
Date: February 2015
91+
Contact: linuxppc-dev@lists.ozlabs.org
92+
Description: read only
93+
Hexadecimal value of the vendor ID found in this AFU
94+
configuration record.
95+
96+
What: /sys/class/cxl/<afu>/cr<config num>/device
97+
Date: February 2015
98+
Contact: linuxppc-dev@lists.ozlabs.org
99+
Description: read only
100+
Hexadecimal value of the device ID found in this AFU
101+
configuration record.
102+
103+
What: /sys/class/cxl/<afu>/cr<config num>/vendor
104+
Date: February 2015
105+
Contact: linuxppc-dev@lists.ozlabs.org
106+
Description: read only
107+
Hexadecimal value of the class code found in this AFU
108+
configuration record.
109+
110+
What: /sys/class/cxl/<afu>/cr<config num>/config
111+
Date: February 2015
112+
Contact: linuxppc-dev@lists.ozlabs.org
113+
Description: read only
114+
This binary file provides raw access to the AFU configuration
115+
record. The format is expected to match the either the standard
116+
or extended configuration space defined by the PCIe
117+
specification.
118+
119+
120+
84121
Master contexts (eg. /sys/class/cxl/afu0.0m)
85122

86123
What: /sys/class/cxl/<afu>m/mmio_size

‎drivers/misc/cxl/cxl.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ struct cxl_afu {
382382
int slice;
383383
int modes_supported;
384384
int current_mode;
385+
int crs_num;
386+
u64 crs_len;
387+
u64 crs_offset;
388+
struct list_head crs;
385389
enum prefault_modes prefault_mode;
386390
bool psa;
387391
bool pp_psa;
@@ -551,6 +555,15 @@ static inline void __iomem *_cxl_p2n_addr(struct cxl_afu *afu, cxl_p2n_reg_t reg
551555
#define cxl_p2n_read(afu, reg) \
552556
in_be64(_cxl_p2n_addr(afu, reg))
553557

558+
559+
#define cxl_afu_cr_read64(afu, cr, off) \
560+
in_le64((afu)->afu_desc_mmio + (afu)->crs_offset + ((cr) * (afu)->crs_len) + (off))
561+
#define cxl_afu_cr_read32(afu, cr, off) \
562+
in_le32((afu)->afu_desc_mmio + (afu)->crs_offset + ((cr) * (afu)->crs_len) + (off))
563+
u16 cxl_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off);
564+
u8 cxl_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off);
565+
566+
554567
struct cxl_calls {
555568
void (*cxl_slbia)(struct mm_struct *mm);
556569
struct module *owner;

‎drivers/misc/cxl/pci.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@
114114
#define AFUD_EB_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
115115
#define AFUD_READ_EB_OFF(afu) AFUD_READ(afu, 0x48)
116116

117+
u16 cxl_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off)
118+
{
119+
u64 aligned_off = off & ~0x3L;
120+
u32 val;
121+
122+
val = cxl_afu_cr_read32(afu, cr, aligned_off);
123+
return (val >> ((off & 0x2) * 8)) & 0xffff;
124+
}
125+
126+
u8 cxl_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off)
127+
{
128+
u64 aligned_off = off & ~0x3L;
129+
u32 val;
130+
131+
val = cxl_afu_cr_read32(afu, cr, aligned_off);
132+
return (val >> ((off & 0x3) * 8)) & 0xff;
133+
}
134+
117135
static DEFINE_PCI_DEVICE_TABLE(cxl_pci_tbl) = {
118136
{ PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0477), },
119137
{ PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x044b), },
@@ -556,6 +574,7 @@ static int cxl_read_afu_descriptor(struct cxl_afu *afu)
556574
val = AFUD_READ_INFO(afu);
557575
afu->pp_irqs = AFUD_NUM_INTS_PER_PROC(val);
558576
afu->max_procs_virtualised = AFUD_NUM_PROCS(val);
577+
afu->crs_num = AFUD_NUM_CRS(val);
559578

560579
if (AFUD_AFU_DIRECTED(val))
561580
afu->modes_supported |= CXL_MODE_DIRECTED;
@@ -570,6 +589,10 @@ static int cxl_read_afu_descriptor(struct cxl_afu *afu)
570589
if ((afu->pp_psa = AFUD_PPPSA_PP(val)))
571590
afu->pp_offset = AFUD_READ_PPPSA_OFF(afu);
572591

592+
val = AFUD_READ_CR(afu);
593+
afu->crs_len = AFUD_CR_LEN(val) * 256;
594+
afu->crs_offset = AFUD_READ_CR_OFF(afu);
595+
573596
return 0;
574597
}
575598

‎drivers/misc/cxl/sysfs.c‎

Lines changed: 169 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/kernel.h>
1111
#include <linux/device.h>
1212
#include <linux/sysfs.h>
13+
#include <linux/pci_regs.h>
1314

1415
#include "cxl.h"
1516

@@ -367,8 +368,6 @@ static struct device_attribute afu_attrs[] = {
367368
__ATTR(reset, S_IWUSR, NULL, reset_store_afu),
368369
};
369370

370-
371-
372371
int cxl_sysfs_adapter_add(struct cxl *adapter)
373372
{
374373
int i, rc;
@@ -391,31 +390,191 @@ void cxl_sysfs_adapter_remove(struct cxl *adapter)
391390
device_remove_file(&adapter->dev, &adapter_attrs[i]);
392391
}
393392

393+
struct afu_config_record {
394+
struct kobject kobj;
395+
struct bin_attribute config_attr;
396+
struct list_head list;
397+
int cr;
398+
u16 device;
399+
u16 vendor;
400+
u32 class;
401+
};
402+
403+
#define to_cr(obj) container_of(obj, struct afu_config_record, kobj)
404+
405+
static ssize_t vendor_show(struct kobject *kobj,
406+
struct kobj_attribute *attr, char *buf)
407+
{
408+
struct afu_config_record *cr = to_cr(kobj);
409+
410+
return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->vendor);
411+
}
412+
413+
static ssize_t device_show(struct kobject *kobj,
414+
struct kobj_attribute *attr, char *buf)
415+
{
416+
struct afu_config_record *cr = to_cr(kobj);
417+
418+
return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->device);
419+
}
420+
421+
static ssize_t class_show(struct kobject *kobj,
422+
struct kobj_attribute *attr, char *buf)
423+
{
424+
struct afu_config_record *cr = to_cr(kobj);
425+
426+
return scnprintf(buf, PAGE_SIZE, "0x%.6x\n", cr->class);
427+
}
428+
429+
static ssize_t afu_read_config(struct file *filp, struct kobject *kobj,
430+
struct bin_attribute *bin_attr, char *buf,
431+
loff_t off, size_t count)
432+
{
433+
struct afu_config_record *cr = to_cr(kobj);
434+
struct cxl_afu *afu = to_cxl_afu(container_of(kobj->parent, struct device, kobj));
435+
436+
u64 i, j, val, size = afu->crs_len;
437+
438+
if (off > size)
439+
return 0;
440+
if (off + count > size)
441+
count = size - off;
442+
443+
for (i = 0; i < count;) {
444+
val = cxl_afu_cr_read64(afu, cr->cr, off & ~0x7);
445+
for (j = off & 0x7; j < 8 && i < count; i++, j++, off++)
446+
buf[i] = (val >> (j * 8)) & 0xff;
447+
}
448+
449+
return count;
450+
}
451+
452+
static struct kobj_attribute vendor_attribute =
453+
__ATTR_RO(vendor);
454+
static struct kobj_attribute device_attribute =
455+
__ATTR_RO(device);
456+
static struct kobj_attribute class_attribute =
457+
__ATTR_RO(class);
458+
459+
static struct attribute *afu_cr_attrs[] = {
460+
&vendor_attribute.attr,
461+
&device_attribute.attr,
462+
&class_attribute.attr,
463+
NULL,
464+
};
465+
466+
static void release_afu_config_record(struct kobject *kobj)
467+
{
468+
struct afu_config_record *cr = to_cr(kobj);
469+
470+
kfree(cr);
471+
}
472+
473+
static struct kobj_type afu_config_record_type = {
474+
.sysfs_ops = &kobj_sysfs_ops,
475+
.release = release_afu_config_record,
476+
.default_attrs = afu_cr_attrs,
477+
};
478+
479+
static struct afu_config_record *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int cr_idx)
480+
{
481+
struct afu_config_record *cr;
482+
int rc;
483+
484+
cr = kzalloc(sizeof(struct afu_config_record), GFP_KERNEL);
485+
if (!cr)
486+
return ERR_PTR(-ENOMEM);
487+
488+
cr->cr = cr_idx;
489+
cr->device = cxl_afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID);
490+
cr->vendor = cxl_afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID);
491+
cr->class = cxl_afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION) >> 8;
492+
493+
/*
494+
* Export raw AFU PCIe like config record. For now this is read only by
495+
* root - we can expand that later to be readable by non-root and maybe
496+
* even writable provided we have a good use-case. Once we suport
497+
* exposing AFUs through a virtual PHB they will get that for free from
498+
* Linux' PCI infrastructure, but until then it's not clear that we
499+
* need it for anything since the main use case is just identifying
500+
* AFUs, which can be done via the vendor, device and class attributes.
501+
*/
502+
sysfs_bin_attr_init(&cr->config_attr);
503+
cr->config_attr.attr.name = "config";
504+
cr->config_attr.attr.mode = S_IRUSR;
505+
cr->config_attr.size = afu->crs_len;
506+
cr->config_attr.read = afu_read_config;
507+
508+
rc = kobject_init_and_add(&cr->kobj, &afu_config_record_type,
509+
&afu->dev.kobj, "cr%i", cr->cr);
510+
if (rc)
511+
goto err;
512+
513+
rc = sysfs_create_bin_file(&cr->kobj, &cr->config_attr);
514+
if (rc)
515+
goto err1;
516+
517+
rc = kobject_uevent(&cr->kobj, KOBJ_ADD);
518+
if (rc)
519+
goto err2;
520+
521+
return cr;
522+
err2:
523+
sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
524+
err1:
525+
kobject_put(&cr->kobj);
526+
return ERR_PTR(rc);
527+
err:
528+
kfree(cr);
529+
return ERR_PTR(rc);
530+
}
531+
532+
void cxl_sysfs_afu_remove(struct cxl_afu *afu)
533+
{
534+
struct afu_config_record *cr, *tmp;
535+
int i;
536+
537+
for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
538+
device_remove_file(&afu->dev, &afu_attrs[i]);
539+
540+
list_for_each_entry_safe(cr, tmp, &afu->crs, list) {
541+
sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
542+
kobject_put(&cr->kobj);
543+
}
544+
}
545+
394546
int cxl_sysfs_afu_add(struct cxl_afu *afu)
395547
{
548+
struct afu_config_record *cr;
396549
int i, rc;
397550

551+
INIT_LIST_HEAD(&afu->crs);
552+
398553
for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
399554
if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
400555
goto err;
401556
}
402557

558+
for (i = 0; i < afu->crs_num; i++) {
559+
cr = cxl_sysfs_afu_new_cr(afu, i);
560+
if (IS_ERR(cr)) {
561+
rc = PTR_ERR(cr);
562+
goto err1;
563+
}
564+
list_add(&cr->list, &afu->crs);
565+
}
566+
403567
return 0;
404568

569+
err1:
570+
cxl_sysfs_afu_remove(afu);
571+
return rc;
405572
err:
406573
for (i--; i >= 0; i--)
407574
device_remove_file(&afu->dev, &afu_attrs[i]);
408575
return rc;
409576
}
410577

411-
void cxl_sysfs_afu_remove(struct cxl_afu *afu)
412-
{
413-
int i;
414-
415-
for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
416-
device_remove_file(&afu->dev, &afu_attrs[i]);
417-
}
418-
419578
int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
420579
{
421580
int i, rc;

0 commit comments

Comments
 (0)