Skip to contentSkip to conversation

powerpc/powernv: Add a debugfs file to read the firmware console

PATCH In review Benjamin Herrenschmidt 2 participants 5 messages Oct 8, 2013 – Oct 9, 2013 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/powernv: Add a debugfs file to read the firmware console

With OPALv3, the firmware can provide the address of it's internal console to Linux, which we can then display using debugfs. This is handy for diagnostics and debugging.

---

arch/powerpc/platforms/powernv/opal.c +104 -0
index 2911abe..10d7894 100644
@@ -17,6 +17,8 @@
 #include <linux/interrupt.h>
 #include <linux/notifier.h>
 #include <linux/slab.h>
+#include <linux/debugfs.h>
+#include <linux/uaccess.h>
 #include <asm/opal.h>
 #include <asm/firmware.h>
 
@@ -27,6 +29,21 @@ struct opal {
 	u64 entry;
 } opal;
 
+/* OPAL in-memory console */
+struct memcons {
+	uint64_t magic;
+#define MEMCONS_MAGIC	0x6630696567726173
+	uint64_t obuf_phys;
+	uint64_t ibuf_phys;
+	uint32_t obuf_size;
+	uint32_t ibuf_size;
+	uint32_t out_pos;
+#define MEMCONS_OUT_POS_WRAP	0x80000000u
+#define MEMCONS_OUT_POS_MASK	0x00ffffffu
+	uint32_t in_prod;
+	uint32_t in_cons;
+};
+
 static struct device_node *opal_node;
 static DEFINE_SPINLOCK(opal_write_lock);
 extern u64 opal_mc_secondary_handler[];
@@ -369,6 +386,90 @@ static irqreturn_t opal_interrupt(int irq, void *data)
 	return IRQ_HANDLED;
 }
 
+#ifdef CONFIG_DEBUG_FS
+static ssize_t opal_memcons_read(struct file *file, char __user *to,
+				 size_t count, loff_t *ppos)
+{
+	struct memcons *mc = file->private_data;
+	size_t available, ret, chunk0, chunk1, lcount;
+	const char *start, *conbuf = __va(mc->obuf_phys);
+	loff_t opos, pos;
+
+	/*
+	 * Find out how much is in the buffer. If it has wrapped
+	 * the whole buffer, else just the beginning. It has wrapped
+	 * if the next character is not \0
+	 */
+	if (mc->out_pos & MEMCONS_OUT_POS_WRAP) {
+		available = mc->obuf_size;
+		chunk1 = mc->out_pos & MEMCONS_OUT_POS_MASK;
+		start = conbuf + chunk1;
+		chunk0 = mc->obuf_size - chunk1;
+	} else {
+		available = mc->out_pos;
+		start = conbuf;
+		chunk0 = available;
+		chunk1 = 0;
+	}
+
+	opos = pos = *ppos;
+
+	/* Sanity check arguments */
+	if (pos < 0)
+		return -EINVAL;
+	if (pos >= available || !count)
+		return 0;
+	if (count > available - pos)
+		count = available - pos;
+
+	/* Handle first chunk */
+	if (pos < chunk0) {
+		lcount = min(count, chunk0 - (size_t)pos);
+		ret = copy_to_user(to, start + pos, lcount);
+		if (ret == lcount)
+			return -EFAULT;
+		lcount -= ret;
+		count -= lcount;
+		*ppos += lcount;
+		to += lcount;
+		pos = 0;
+	} else {
+		*ppos += chunk0;
+		pos -= chunk0;
+		ret = 0;
+	}
+
+	/* Handle second chunk */
+	if (count && chunk1 && ret == 0) {
+		lcount = min(count, chunk1 - (size_t)pos);
+		ret = copy_to_user(to, conbuf + pos, lcount);
+		if (ret == lcount)
+			return -EFAULT;
+		lcount -= ret;
+		*ppos += lcount;
+	}
+	return *ppos - opos;
+
+}
+
+static const struct file_operations opal_fops_memcons = {
+	.read =		opal_memcons_read,
+	.open =		simple_open,
+	.llseek =	default_llseek,
+};
+
+static void opal_init_debugfs(void)
+{
+	u64 mcaddr;
+
+	if (of_property_read_u64(opal_node, "ibm,opal-memcons", &mcaddr) == 0)
+		debugfs_create_file("opal-log", 0400, powerpc_debugfs_root,
+				    __va(mcaddr), &opal_fops_memcons);
+}
+#else
+static void opal_init_debugfs(void) { }
+#endif /* CONFIG_DEBUG_FS */
+
 static int __init opal_init(void)
 {
 	struct device_node *np, *consoles;
@@ -414,6 +515,9 @@ static int __init opal_init(void)
 				   " (0x%x)\n", rc, irq, hwirq);
 		opal_irqs[i] = irq;
 	}
+
+	opal_init_debugfs();
+
 	return 0;
 }
 subsys_initcall(opal_init);
#1 lore raw
Quoted 21 lines · Benjamin Herrenschmidt

With OPALv3, the firmware can provide the address of it's internal console to Linux, which we can then display using debugfs. This is handy for diagnostics and debugging.

---

arch/powerpc/platforms/powernv/opal.c +2 -0
index 2911abe..10d7894 100644
@@ -17,6 +17,8 @@
 #include <linux/interrupt.h>
 #include <linux/notifier.h>
 #include <linux/slab.h>
+#include <linux/debugfs.h>
+#include <linux/uaccess.h>
 #include <asm/opal.h>
 #include <asm/firmware.h>
 

arch/powerpc/platforms/powernv/opal.c struct opal { · line 32

Quoted 4 lines
arch/powerpc/platforms/powernv/opal.c +1 -0
@@ -27,6 +29,21 @@ struct opal {
 	u64 entry;
 } opal;
 
+/* OPAL in-memory console */

It might be nice to point out that the format of the struct is defined by OPAL and must be in sync with what OPAL is using.

arch/powerpc/platforms/powernv/opal.c struct opal { · line 34

Quoted 2 lines
arch/powerpc/platforms/powernv/opal.c +2 -0
+struct memcons {
+	uint64_t magic;

u64 ?

arch/powerpc/platforms/powernv/opal.c struct opal { · line 42

Quoted 8 lines
arch/powerpc/platforms/powernv/opal.c +8 -0
+#define MEMCONS_MAGIC	0x6630696567726173
+	uint64_t obuf_phys;
+	uint64_t ibuf_phys;
+	uint32_t obuf_size;
+	uint32_t ibuf_size;
+	uint32_t out_pos;
+#define MEMCONS_OUT_POS_WRAP	0x80000000u
+#define MEMCONS_OUT_POS_MASK	0x00ffffffu

Where does this come from?

arch/powerpc/platforms/powernv/opal.c struct opal { · line 45

Quoted 3 lines
arch/powerpc/platforms/powernv/opal.c +3 -0
+	uint32_t in_prod;
+	uint32_t in_cons;
+};

Should it be packed?

Quoted 19 lines
arch/powerpc/platforms/powernv/opal.c +15 -0
@@ -369,6 +386,90 @@ static irqreturn_t opal_interrupt(int irq, void *data)
 	return IRQ_HANDLED;
 }
 
+#ifdef CONFIG_DEBUG_FS
+static ssize_t opal_memcons_read(struct file *file, char __user *to,
+				 size_t count, loff_t *ppos)
+{
+	struct memcons *mc = file->private_data;
+	size_t available, ret, chunk0, chunk1, lcount;
+	const char *start, *conbuf = __va(mc->obuf_phys);
+	loff_t opos, pos;
+
+	/*
+	 * Find out how much is in the buffer. If it has wrapped
+	 * the whole buffer, else just the beginning. It has wrapped
+	 * if the next character is not \0
+	 */
+	if (mc->out_pos & MEMCONS_OUT_POS_WRAP) {

arch/powerpc/platforms/powernv/opal.c static irqreturn_t opal_interrupt(int irq, void *data) · line 413

Quoted 10 lines
arch/powerpc/platforms/powernv/opal.c +10 -0
+		available = mc->obuf_size;
+		chunk1 = mc->out_pos & MEMCONS_OUT_POS_MASK;
+		start = conbuf + chunk1;
+		chunk0 = mc->obuf_size - chunk1;
+	} else {
+		available = mc->out_pos;
+		start = conbuf;
+		chunk0 = available;
+		chunk1 = 0;
+	}

Surely simple_read_from_buffer() could make some of this simpler?

cheers

#2 lore raw
Quoted 21 lines · Michael Ellerman
Quoted 20 lines · Benjamin Herrenschmidt

With OPALv3, the firmware can provide the address of it's internal console to Linux, which we can then display using debugfs. This is handy for diagnostics and debugging.

---

arch/powerpc/platforms/powernv/opal.c +2 -0
index 2911abe..10d7894 100644
@@ -17,6 +17,8 @@
 #include <linux/interrupt.h>
 #include <linux/notifier.h>
 #include <linux/slab.h>
+#include <linux/debugfs.h>
+#include <linux/uaccess.h>
 #include <asm/opal.h>
 #include <asm/firmware.h>
 
Quoted 8 lines

arch/powerpc/platforms/powernv/opal.c struct opal { · line 32

Quoted 5 lines
arch/powerpc/platforms/powernv/opal.c +1 -0
@@ -27,6 +29,21 @@ struct opal {
 	u64 entry;
 } opal;
 
+/* OPAL in-memory console */

It might be nice to point out that the format of the struct is defined by OPAL and must be in sync with what OPAL is using.

Yes, we could move the structure definition to opal.h...

Quoted 4 lines

arch/powerpc/platforms/powernv/opal.c struct opal { · line 34

Quoted 2 lines
arch/powerpc/platforms/powernv/opal.c +2 -0
+struct memcons {
+	uint64_t magic;

u64 ?

Who cares ? Especially if it goes into opal.h it should stick to the types used in that file.

Quoted 10 lines

arch/powerpc/platforms/powernv/opal.c struct opal { · line 42

Quoted 8 lines
arch/powerpc/platforms/powernv/opal.c +8 -0
+#define MEMCONS_MAGIC	0x6630696567726173
+	uint64_t obuf_phys;
+	uint64_t ibuf_phys;
+	uint32_t obuf_size;
+	uint32_t ibuf_size;
+	uint32_t out_pos;
+#define MEMCONS_OUT_POS_WRAP	0x80000000u
+#define MEMCONS_OUT_POS_MASK	0x00ffffffu

Where does this come from?

My a** :-) I made it up as I wrote the OPAL side one, why ?

Quoted 5 lines

arch/powerpc/platforms/powernv/opal.c struct opal { · line 45

Quoted 3 lines
arch/powerpc/platforms/powernv/opal.c +3 -0
+	uint32_t in_prod;
+	uint32_t in_cons;
+};

Should it be packed?

Nope, no need. It's all nice and naturally aligned.

Quoted 23 lines
Quoted 23 lines
arch/powerpc/platforms/powernv/opal.c +19 -0
@@ -369,6 +386,90 @@ static irqreturn_t opal_interrupt(int irq, void *data)
 	return IRQ_HANDLED;
 }
 
+#ifdef CONFIG_DEBUG_FS
+static ssize_t opal_memcons_read(struct file *file, char __user *to,
+				 size_t count, loff_t *ppos)
+{
+	struct memcons *mc = file->private_data;
+	size_t available, ret, chunk0, chunk1, lcount;
+	const char *start, *conbuf = __va(mc->obuf_phys);
+	loff_t opos, pos;
+
+	/*
+	 * Find out how much is in the buffer. If it has wrapped
+	 * the whole buffer, else just the beginning. It has wrapped
+	 * if the next character is not \0
+	 */
+	if (mc->out_pos & MEMCONS_OUT_POS_WRAP) {
+		available = mc->obuf_size;
+		chunk1 = mc->out_pos & MEMCONS_OUT_POS_MASK;
+		start = conbuf + chunk1;
+		chunk0 = mc->obuf_size - chunk1;
Quoted 8 lines

arch/powerpc/platforms/powernv/opal.c static irqreturn_t opal_interrupt(int irq, void *data) · line 413

Quoted 6 lines
arch/powerpc/platforms/powernv/opal.c +6 -0
+	} else {
+		available = mc->out_pos;
+		start = conbuf;
+		chunk0 = available;
+		chunk1 = 0;
+	}

Surely simple_read_from_buffer() could make some of this simpler?

If you can find a way to make it deal with a ring buffer...

Cheers,
Ben.

#3 lore raw
Quoted 31 lines · Benjamin Herrenschmidt
Quoted 21 lines · Michael Ellerman
Quoted 20 lines · Benjamin Herrenschmidt

With OPALv3, the firmware can provide the address of it's internal console to Linux, which we can then display using debugfs. This is handy for diagnostics and debugging.

---

arch/powerpc/platforms/powernv/opal.c +2 -0
index 2911abe..10d7894 100644
@@ -17,6 +17,8 @@
 #include <linux/interrupt.h>
 #include <linux/notifier.h>
 #include <linux/slab.h>
+#include <linux/debugfs.h>
+#include <linux/uaccess.h>
 #include <asm/opal.h>
 #include <asm/firmware.h>
 
Quoted 8 lines

arch/powerpc/platforms/powernv/opal.c struct opal { · line 32

Quoted 5 lines
arch/powerpc/platforms/powernv/opal.c +1 -0
@@ -27,6 +29,21 @@ struct opal {
 	u64 entry;
 } opal;
 
+/* OPAL in-memory console */

It might be nice to point out that the format of the struct is defined by OPAL and must be in sync with what OPAL is using.

Quoted 8 lines

Yes, we could move the structure definition to opal.h...

Quoted 4 lines

arch/powerpc/platforms/powernv/opal.c struct opal { · line 34

Quoted 2 lines
arch/powerpc/platforms/powernv/opal.c +2 -0
+struct memcons {
+	uint64_t magic;

u64 ?

Who cares ?

That's what we use in the kernel. But no I don't care.

Quoted 12 lines
Quoted 10 lines

arch/powerpc/platforms/powernv/opal.c struct opal { · line 42

Quoted 8 lines
arch/powerpc/platforms/powernv/opal.c +8 -0
+#define MEMCONS_MAGIC	0x6630696567726173
+	uint64_t obuf_phys;
+	uint64_t ibuf_phys;
+	uint32_t obuf_size;
+	uint32_t ibuf_size;
+	uint32_t out_pos;
+#define MEMCONS_OUT_POS_WRAP	0x80000000u
+#define MEMCONS_OUT_POS_MASK	0x00ffffffu

Where does this come from?

My a** :-) I made it up as I wrote the OPAL side one, why ?

...

Quoted 7 lines
Quoted 5 lines

arch/powerpc/platforms/powernv/opal.c struct opal { · line 45

Quoted 3 lines
arch/powerpc/platforms/powernv/opal.c +3 -0
+	uint32_t in_prod;
+	uint32_t in_cons;
+};

Should it be packed?

Nope, no need. It's all nice and naturally aligned.

Sure, that's obvious.

Quoted 32 lines
Quoted 23 lines
Quoted 23 lines
arch/powerpc/platforms/powernv/opal.c +19 -0
@@ -369,6 +386,90 @@ static irqreturn_t opal_interrupt(int irq, void *data)
 	return IRQ_HANDLED;
 }
 
+#ifdef CONFIG_DEBUG_FS
+static ssize_t opal_memcons_read(struct file *file, char __user *to,
+				 size_t count, loff_t *ppos)
+{
+	struct memcons *mc = file->private_data;
+	size_t available, ret, chunk0, chunk1, lcount;
+	const char *start, *conbuf = __va(mc->obuf_phys);
+	loff_t opos, pos;
+
+	/*
+	 * Find out how much is in the buffer. If it has wrapped
+	 * the whole buffer, else just the beginning. It has wrapped
+	 * if the next character is not \0
+	 */
+	if (mc->out_pos & MEMCONS_OUT_POS_WRAP) {
+		available = mc->obuf_size;
+		chunk1 = mc->out_pos & MEMCONS_OUT_POS_MASK;
+		start = conbuf + chunk1;
+		chunk0 = mc->obuf_size - chunk1;
Quoted 8 lines

arch/powerpc/platforms/powernv/opal.c static irqreturn_t opal_interrupt(int irq, void *data) · line 413

Quoted 6 lines
arch/powerpc/platforms/powernv/opal.c +6 -0
+	} else {
+		available = mc->out_pos;
+		start = conbuf;
+		chunk0 = available;
+		chunk1 = 0;
+	}

Surely simple_read_from_buffer() could make some of this simpler?

Quoted 1 line

If you can find a way to make it deal with a ring buffer...

Call it twice.

And you wonder why no one reviews your patches?

cheers

#4 lore raw
Quoted 3 lines · Michael Ellerman

Call it twice.

And you wonder why no one reviews your patches?

Not that easy :-) I had a look at using it and unless I did something stupid, it wasn't actually that trivial to figure out what arg to pass it for calling it twice, ie, it didn't simplify the code significantly ... I had another problem too but I don't remember what it was. But it could be that I had a very dumb moment...

Cheers,
Ben.