2019-05-27 06:55:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-08-29 06:57:33 +00:00
|
|
|
/*
|
2019-05-09 05:11:17 +00:00
|
|
|
* PowerNV SCOM bus debugfs interface
|
2013-08-29 06:57:33 +00:00
|
|
|
*
|
2019-05-09 05:11:17 +00:00
|
|
|
* Copyright 2010 Benjamin Herrenschmidt, IBM Corp
|
|
|
|
* <benh@kernel.crashing.org>
|
|
|
|
* and David Gibson, IBM Corporation.
|
2013-08-29 06:57:33 +00:00
|
|
|
* Copyright 2013 IBM Corp.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/bug.h>
|
|
|
|
#include <linux/gfp.h>
|
|
|
|
#include <linux/slab.h>
|
2019-05-09 05:11:17 +00:00
|
|
|
#include <linux/uaccess.h>
|
2021-08-12 13:28:31 +00:00
|
|
|
#include <linux/debugfs.h>
|
2013-08-29 06:57:33 +00:00
|
|
|
|
|
|
|
#include <asm/machdep.h>
|
|
|
|
#include <asm/firmware.h>
|
|
|
|
#include <asm/opal.h>
|
2019-05-09 05:11:17 +00:00
|
|
|
#include <asm/prom.h>
|
2013-08-29 06:57:33 +00:00
|
|
|
|
2014-02-28 05:20:38 +00:00
|
|
|
static u64 opal_scom_unmangle(u64 addr)
|
2013-10-10 08:19:15 +00:00
|
|
|
{
|
2017-03-24 10:20:56 +00:00
|
|
|
u64 tmp;
|
|
|
|
|
2013-10-10 08:19:15 +00:00
|
|
|
/*
|
2017-03-24 10:20:56 +00:00
|
|
|
* XSCOM addresses use the top nibble to set indirect mode and
|
|
|
|
* its form. Bits 4-11 are always 0.
|
2013-10-10 08:19:15 +00:00
|
|
|
*
|
|
|
|
* Because the debugfs interface uses signed offsets and shifts
|
|
|
|
* the address left by 3, we basically cannot use the top 4 bits
|
|
|
|
* of the 64-bit address, and thus cannot use the indirect bit.
|
|
|
|
*
|
2017-03-24 10:20:56 +00:00
|
|
|
* To deal with that, we support the indirect bits being in
|
|
|
|
* bits 4-7 (IBM notation) instead of bit 0-3 in this API, we
|
|
|
|
* do the conversion here.
|
2013-10-10 08:19:15 +00:00
|
|
|
*
|
2017-03-24 10:20:56 +00:00
|
|
|
* For in-kernel use, we don't need to do this mangling. In
|
|
|
|
* kernel won't have bits 4-7 set.
|
2014-02-28 05:20:38 +00:00
|
|
|
*
|
2017-03-24 10:20:56 +00:00
|
|
|
* So:
|
|
|
|
* debugfs will always set 0-3 = 0 and clear 4-7
|
|
|
|
* kernel will always clear 0-3 = 0 and set 4-7
|
2013-10-10 08:19:15 +00:00
|
|
|
*/
|
2017-03-24 10:20:56 +00:00
|
|
|
tmp = addr;
|
|
|
|
tmp &= 0x0f00000000000000;
|
|
|
|
addr &= 0xf0ffffffffffffff;
|
|
|
|
addr |= tmp << 4;
|
|
|
|
|
2014-02-28 05:20:38 +00:00
|
|
|
return addr;
|
2013-10-10 08:19:15 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 05:11:17 +00:00
|
|
|
static int opal_scom_read(uint32_t chip, uint64_t addr, u64 reg, u64 *value)
|
2013-08-29 06:57:33 +00:00
|
|
|
{
|
|
|
|
int64_t rc;
|
2013-12-13 04:53:43 +00:00
|
|
|
__be64 v;
|
2013-08-29 06:57:33 +00:00
|
|
|
|
2019-05-09 05:11:17 +00:00
|
|
|
reg = opal_scom_unmangle(addr + reg);
|
|
|
|
rc = opal_xscom_read(chip, reg, (__be64 *)__pa(&v));
|
|
|
|
if (rc) {
|
|
|
|
*value = 0xfffffffffffffffful;
|
|
|
|
return -EIO;
|
|
|
|
}
|
2013-12-13 04:53:43 +00:00
|
|
|
*value = be64_to_cpu(v);
|
2019-05-09 05:11:17 +00:00
|
|
|
return 0;
|
2013-08-29 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 05:11:17 +00:00
|
|
|
static int opal_scom_write(uint32_t chip, uint64_t addr, u64 reg, u64 value)
|
2013-08-29 06:57:33 +00:00
|
|
|
{
|
|
|
|
int64_t rc;
|
|
|
|
|
2019-05-09 05:11:17 +00:00
|
|
|
reg = opal_scom_unmangle(addr + reg);
|
|
|
|
rc = opal_xscom_write(chip, reg, value);
|
|
|
|
if (rc)
|
|
|
|
return -EIO;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct scom_debug_entry {
|
|
|
|
u32 chip;
|
|
|
|
struct debugfs_blob_wrapper path;
|
|
|
|
char name[16];
|
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t scom_debug_read(struct file *filp, char __user *ubuf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct scom_debug_entry *ent = filp->private_data;
|
|
|
|
u64 __user *ubuf64 = (u64 __user *)ubuf;
|
|
|
|
loff_t off = *ppos;
|
2019-05-09 05:11:18 +00:00
|
|
|
ssize_t done = 0;
|
2019-05-09 05:11:17 +00:00
|
|
|
u64 reg, reg_base, reg_cnt, val;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (off < 0 || (off & 7) || (count & 7))
|
|
|
|
return -EINVAL;
|
|
|
|
reg_base = off >> 3;
|
|
|
|
reg_cnt = count >> 3;
|
|
|
|
|
|
|
|
for (reg = 0; reg < reg_cnt; reg++) {
|
|
|
|
rc = opal_scom_read(ent->chip, reg_base, reg, &val);
|
|
|
|
if (!rc)
|
|
|
|
rc = put_user(val, ubuf64);
|
|
|
|
if (rc) {
|
|
|
|
if (!done)
|
|
|
|
done = rc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ubuf64++;
|
|
|
|
*ppos += 8;
|
|
|
|
done += 8;
|
|
|
|
}
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2019-05-09 05:11:18 +00:00
|
|
|
static ssize_t scom_debug_write(struct file *filp, const char __user *ubuf,
|
2019-05-09 05:11:17 +00:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct scom_debug_entry *ent = filp->private_data;
|
|
|
|
u64 __user *ubuf64 = (u64 __user *)ubuf;
|
|
|
|
loff_t off = *ppos;
|
2019-05-09 05:11:18 +00:00
|
|
|
ssize_t done = 0;
|
2019-05-09 05:11:17 +00:00
|
|
|
u64 reg, reg_base, reg_cnt, val;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (off < 0 || (off & 7) || (count & 7))
|
|
|
|
return -EINVAL;
|
|
|
|
reg_base = off >> 3;
|
|
|
|
reg_cnt = count >> 3;
|
|
|
|
|
|
|
|
for (reg = 0; reg < reg_cnt; reg++) {
|
|
|
|
rc = get_user(val, ubuf64);
|
|
|
|
if (!rc)
|
|
|
|
rc = opal_scom_write(ent->chip, reg_base, reg, val);
|
|
|
|
if (rc) {
|
|
|
|
if (!done)
|
|
|
|
done = rc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ubuf64++;
|
|
|
|
done += 8;
|
|
|
|
}
|
|
|
|
return done;
|
2013-08-29 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 05:11:17 +00:00
|
|
|
static const struct file_operations scom_debug_fops = {
|
|
|
|
.read = scom_debug_read,
|
|
|
|
.write = scom_debug_write,
|
|
|
|
.open = simple_open,
|
|
|
|
.llseek = default_llseek,
|
2013-08-29 06:57:33 +00:00
|
|
|
};
|
|
|
|
|
2019-05-09 05:11:17 +00:00
|
|
|
static int scom_debug_init_one(struct dentry *root, struct device_node *dn,
|
|
|
|
int chip)
|
2013-08-29 06:57:33 +00:00
|
|
|
{
|
2019-05-09 05:11:17 +00:00
|
|
|
struct scom_debug_entry *ent;
|
|
|
|
struct dentry *dir;
|
|
|
|
|
|
|
|
ent = kzalloc(sizeof(*ent), GFP_KERNEL);
|
|
|
|
if (!ent)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ent->chip = chip;
|
|
|
|
snprintf(ent->name, 16, "%08x", chip);
|
2019-05-09 05:11:18 +00:00
|
|
|
ent->path.data = (void *)kasprintf(GFP_KERNEL, "%pOF", dn);
|
2019-05-09 05:11:17 +00:00
|
|
|
ent->path.size = strlen((char *)ent->path.data);
|
|
|
|
|
|
|
|
dir = debugfs_create_dir(ent->name, root);
|
|
|
|
if (!dir) {
|
|
|
|
kfree(ent->path.data);
|
|
|
|
kfree(ent);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
debugfs_create_blob("devspec", 0400, dir, &ent->path);
|
|
|
|
debugfs_create_file("access", 0600, dir, ent, &scom_debug_fops);
|
|
|
|
|
2013-08-29 06:57:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2019-05-09 05:11:17 +00:00
|
|
|
|
|
|
|
static int scom_debug_init(void)
|
|
|
|
{
|
|
|
|
struct device_node *dn;
|
|
|
|
struct dentry *root;
|
|
|
|
int chip, rc;
|
|
|
|
|
|
|
|
if (!firmware_has_feature(FW_FEATURE_OPAL))
|
|
|
|
return 0;
|
|
|
|
|
2021-08-12 13:28:31 +00:00
|
|
|
root = debugfs_create_dir("scom", arch_debugfs_dir);
|
2019-05-09 05:11:17 +00:00
|
|
|
if (!root)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
for_each_node_with_property(dn, "scom-controller") {
|
|
|
|
chip = of_get_ibm_chip_id(dn);
|
|
|
|
WARN_ON(chip == -1);
|
|
|
|
rc |= scom_debug_init_one(root, dn, chip);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
device_initcall(scom_debug_init);
|