All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] Amend tty Goldfish driver functionality
@ 2017-08-22 15:56 Aleksandar Markovic
  2017-08-22 15:56 ` [PATCH 1/2] tty: goldfish: Use streaming DMA for r/w operations on Ranchu platforms Aleksandar Markovic
  2017-08-22 15:56 ` [PATCH 2/2] tty: goldfish: Implement support for kernel 'earlycon' parameter Aleksandar Markovic
  0 siblings, 2 replies; 5+ messages in thread
From: Aleksandar Markovic @ 2017-08-22 15:56 UTC (permalink / raw)
  To: linux-kernel
  Cc: gregkh, jslaby, alan, jinqian, dan.carpenter,
	aleksandar.markovic, miodrag.dinic, petar.jovanovic,
	raghu.gandham, james.hogan, ralf

From: Aleksandar Markovic <aleksandar.markovic@imgtec.com>

This series adds two features to the tty goldfish driver that are
crucial for the operation of the new generation of Android emulator
for Mips.

The backward compatibility issues are all taken care of.

Miodrag Dinic (2):
  tty: goldfish: Use streaming DMA for r/w operations on Ranchu
    platforms
  tty: goldfish: Implement support for kernel 'earlycon' parameter

 drivers/tty/Kconfig    |   3 +
 drivers/tty/goldfish.c | 146 +++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 137 insertions(+), 12 deletions(-)

-- 
2.9.3

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] tty: goldfish: Use streaming DMA for r/w operations on Ranchu platforms
  2017-08-22 15:56 [PATCH 0/2] Amend tty Goldfish driver functionality Aleksandar Markovic
@ 2017-08-22 15:56 ` Aleksandar Markovic
  2017-08-22 19:00   ` Dan Carpenter
  2017-08-22 15:56 ` [PATCH 2/2] tty: goldfish: Implement support for kernel 'earlycon' parameter Aleksandar Markovic
  1 sibling, 1 reply; 5+ messages in thread
From: Aleksandar Markovic @ 2017-08-22 15:56 UTC (permalink / raw)
  To: linux-kernel
  Cc: gregkh, jslaby, alan, jinqian, dan.carpenter,
	aleksandar.markovic, miodrag.dinic, petar.jovanovic,
	raghu.gandham, james.hogan, ralf

From: Miodrag Dinic <miodrag.dinic@imgtec.com>

Implement tty r/w operations using streaming DMA.

Goldfish tty for Ranchu platforms has been modified to use
streaming DMA mappings for read/write operations. This change
eliminates the need for snooping through the TLB in QEMU using
cpu_get_phys_page_debug() which does not guarantee that it will
return the valid va -> pa mapping.

The streaming DMA mapping is implemented using dma_map_single() per
transfer, while dma_unmap_single() is used for unmapping right after
the DMA transfer.

Using DMA API is the proper way for handling r/w transfers and
makes this driver more portable, thus effectively eliminating
the need for virt_to_page() and page_to_phys() conversions.

This change does not affect the old style Goldfish tty behaviour
which is still used by the Goldfish emulator. Version register has
been added and probed to see which platform is running this driver.
Reading from the new GOLDFISH_TTY_VERSION register using the Goldfish
emulator will return 0 and driver will work with virtual addresses.
Whereas if run on Ranchu it returns 1, and thus DMA is used.

(Goldfish and Ranchu are code names for the first and the second
generation of virtual boards used by Android emulator.)

Signed-off-by: Miodrag Dinic <miodrag.dinic@imgtec.com>
Signed-off-by: Goran Ferenc <goran.ferenc@imgtec.com>
Signed-off-by: Aleksandar Markovic <aleksandar.markovic@imgtech.com>
---
 drivers/tty/goldfish.c | 120 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 12 deletions(-)

diff --git a/drivers/tty/goldfish.c b/drivers/tty/goldfish.c
index 996bd47..ac98d5a 100644
--- a/drivers/tty/goldfish.c
+++ b/drivers/tty/goldfish.c
@@ -22,6 +22,8 @@
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/goldfish.h>
+#include <linux/mm.h>
+#include <linux/dma-mapping.h>
 
 enum {
 	GOLDFISH_TTY_PUT_CHAR       = 0x00,
@@ -32,6 +34,8 @@ enum {
 	GOLDFISH_TTY_DATA_LEN       = 0x14,
 	GOLDFISH_TTY_DATA_PTR_HIGH  = 0x18,
 
+	GOLDFISH_TTY_VERSION		= 0x20,
+
 	GOLDFISH_TTY_CMD_INT_DISABLE    = 0,
 	GOLDFISH_TTY_CMD_INT_ENABLE     = 1,
 	GOLDFISH_TTY_CMD_WRITE_BUFFER   = 2,
@@ -45,6 +49,8 @@ struct goldfish_tty {
 	u32 irq;
 	int opencount;
 	struct console console;
+	u32 version;
+	struct device *dev;
 };
 
 static DEFINE_MUTEX(goldfish_tty_lock);
@@ -53,24 +59,93 @@ static u32 goldfish_tty_line_count = 8;
 static u32 goldfish_tty_current_line_count;
 static struct goldfish_tty *goldfish_ttys;
 
-static void goldfish_tty_do_write(int line, const char *buf, unsigned count)
+static inline void do_rw_io(struct goldfish_tty *qtty,
+			    unsigned long address,
+			    unsigned int count,
+			    int is_write)
 {
 	unsigned long irq_flags;
-	struct goldfish_tty *qtty = &goldfish_ttys[line];
 	void __iomem *base = qtty->base;
+
 	spin_lock_irqsave(&qtty->lock, irq_flags);
-	gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
-				base + GOLDFISH_TTY_DATA_PTR_HIGH);
+	gf_write_ptr((void *)address, base + GOLDFISH_TTY_DATA_PTR,
+		     base + GOLDFISH_TTY_DATA_PTR_HIGH);
 	writel(count, base + GOLDFISH_TTY_DATA_LEN);
-	writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD);
+
+	if (is_write)
+		writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD);
+	else
+		writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD);
+
 	spin_unlock_irqrestore(&qtty->lock, irq_flags);
 }
 
+static inline void goldfish_tty_rw(struct goldfish_tty *qtty,
+				   unsigned long addr,
+				   unsigned int count,
+				   int is_write)
+{
+	dma_addr_t dma_handle;
+	enum dma_data_direction dma_dir;
+
+	dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
+	if (qtty->version) {
+		/*
+		 * Goldfish TTY for Ranchu platform uses
+		 * physical addresses and DMA for read/write operations
+		 */
+		unsigned long addr_end = addr + count;
+
+		while (addr < addr_end) {
+			unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
+			unsigned long next =
+					pg_end < addr_end ? pg_end : addr_end;
+			unsigned long avail = next - addr;
+
+			/*
+			 * Map the buffer's virtual address to the DMA address
+			 * so the buffer can be accessed by the device.
+			 */
+			dma_handle = dma_map_single(qtty->dev, (void *)addr,
+						    avail, dma_dir);
+
+			if (dma_mapping_error(qtty->dev, dma_handle)) {
+				dev_err(qtty->dev, "tty: DMA mapping error.\n");
+				return;
+			}
+			do_rw_io(qtty, dma_handle, avail, is_write);
+
+			/*
+			 * Unmap the previously mapped region after
+			 * the completion of the read/write operation.
+			 */
+			dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
+
+			addr += avail;
+		}
+	} else {
+		/*
+		 * Old style Goldfish TTY used on the Goldfish platform
+		 * uses virtual addresses.
+		 */
+		do_rw_io(qtty, addr, count, is_write);
+	}
+}
+
+static void goldfish_tty_do_write(int line, const char *buf,
+				  unsigned int count)
+{
+	struct goldfish_tty *qtty = &goldfish_ttys[line];
+	unsigned long address = (unsigned long)(void *)buf;
+
+	goldfish_tty_rw(qtty, address, count, 1);
+}
+
 static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
 {
 	struct goldfish_tty *qtty = dev_id;
 	void __iomem *base = qtty->base;
-	unsigned long irq_flags;
+	unsigned long address;
 	unsigned char *buf;
 	u32 count;
 
@@ -79,12 +154,10 @@ static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
 		return IRQ_NONE;
 
 	count = tty_prepare_flip_string(&qtty->port, &buf, count);
-	spin_lock_irqsave(&qtty->lock, irq_flags);
-	gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
-				base + GOLDFISH_TTY_DATA_PTR_HIGH);
-	writel(count, base + GOLDFISH_TTY_DATA_LEN);
-	writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD);
-	spin_unlock_irqrestore(&qtty->lock, irq_flags);
+
+	address = (unsigned long)(void *)buf;
+	goldfish_tty_rw(qtty, address, count, 0);
+
 	tty_schedule_flip(&qtty->port);
 	return IRQ_HANDLED;
 }
@@ -271,6 +344,29 @@ static int goldfish_tty_probe(struct platform_device *pdev)
 	qtty->port.ops = &goldfish_port_ops;
 	qtty->base = base;
 	qtty->irq = irq;
+	qtty->dev = &pdev->dev;
+
+	/* Goldfish TTY device used by the Goldfish emulator
+	 * should identify itself with 0, forcing the driver
+	 * to use virtual addresses. Goldfish TTY device
+	 * on Ranchu emulator (qemu2) returns 1 here and
+	 * driver will use physical addresses.
+	 */
+	qtty->version = readl(base + GOLDFISH_TTY_VERSION);
+
+	/* Goldfish TTY device on Ranchu emulator (qemu2)
+	 * will use DMA for read/write IO operations.
+	 */
+	if (qtty->version > 0) {
+		/* Initialize dma_mask to 32-bits.
+		 */
+		if (!pdev->dev.dma_mask)
+			pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
+		if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
+			dev_err(&pdev->dev, "No suitable DMA available.\n");
+			goto err_create_driver_failed;
+		}
+	}
 
 	writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD);
 
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] tty: goldfish: Implement support for kernel 'earlycon' parameter
  2017-08-22 15:56 [PATCH 0/2] Amend tty Goldfish driver functionality Aleksandar Markovic
  2017-08-22 15:56 ` [PATCH 1/2] tty: goldfish: Use streaming DMA for r/w operations on Ranchu platforms Aleksandar Markovic
@ 2017-08-22 15:56 ` Aleksandar Markovic
  1 sibling, 0 replies; 5+ messages in thread
From: Aleksandar Markovic @ 2017-08-22 15:56 UTC (permalink / raw)
  To: linux-kernel
  Cc: gregkh, jslaby, alan, jinqian, dan.carpenter,
	aleksandar.markovic, miodrag.dinic, petar.jovanovic,
	raghu.gandham, james.hogan, ralf

From: Miodrag Dinic <miodrag.dinic@imgtec.com>

Add early console functionality to the Goldfish tty driver.

When 'earlycon' kernel command line parameter is used with no options,
the early console is determined by the 'stdout-path' property in device
tree's 'chosen' node. This is illustrated in the following device tree
source example:

Device tree example:

    chosen {
        stdout-path = "/goldfish_tty@1f004000";
    };

    goldfish_tty@1f004000 {
        interrupts = <0xc>;
        reg = <0x1f004000 0x0 0x1000>;
        compatible = "google,goldfish-tty";
    };

Signed-off-by: Miodrag Dinic <miodrag.dinic@imgtec.com>
Signed-off-by: Goran Ferenc <goran.ferenc@imgtec.com>
Signed-off-by: Aleksandar Markovic <aleksandar.markovic@imgtec.com>
---
 drivers/tty/Kconfig    |  3 +++
 drivers/tty/goldfish.c | 26 ++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/drivers/tty/Kconfig b/drivers/tty/Kconfig
index 9510305..873e0ba 100644
--- a/drivers/tty/Kconfig
+++ b/drivers/tty/Kconfig
@@ -392,6 +392,9 @@ config PPC_EARLY_DEBUG_EHV_BC_HANDLE
 config GOLDFISH_TTY
 	tristate "Goldfish TTY Driver"
 	depends on GOLDFISH
+	select SERIAL_CORE
+	select SERIAL_CORE_CONSOLE
+	select SERIAL_EARLYCON
 	help
 	  Console and system TTY driver for the Goldfish virtual platform.
 
diff --git a/drivers/tty/goldfish.c b/drivers/tty/goldfish.c
index ac98d5a..92d115f 100644
--- a/drivers/tty/goldfish.c
+++ b/drivers/tty/goldfish.c
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2007 Google, Inc.
  * Copyright (C) 2012 Intel, Inc.
+ * Copyright (C) 2017 Imagination Technologies Ltd.
  *
  * This software is licensed under the terms of the GNU General Public
  * License version 2, as published by the Free Software Foundation, and
@@ -24,6 +25,7 @@
 #include <linux/goldfish.h>
 #include <linux/mm.h>
 #include <linux/dma-mapping.h>
+#include <linux/serial_core.h>
 
 enum {
 	GOLDFISH_TTY_PUT_CHAR       = 0x00,
@@ -426,6 +428,30 @@ static int goldfish_tty_remove(struct platform_device *pdev)
 	return 0;
 }
 
+static void gf_early_console_putchar(struct uart_port *port, int ch)
+{
+	__raw_writel(ch, port->membase);
+}
+
+static void gf_early_write(struct console *con, const char *s, unsigned int n)
+{
+	struct earlycon_device *dev = con->data;
+
+	uart_console_write(&dev->port, s, n, gf_early_console_putchar);
+}
+
+static int __init gf_earlycon_setup(struct earlycon_device *device,
+				    const char *opt)
+{
+	if (!device->port.membase)
+		return -ENODEV;
+
+	device->con->write = gf_early_write;
+	return 0;
+}
+
+OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
+
 static const struct of_device_id goldfish_tty_of_match[] = {
 	{ .compatible = "google,goldfish-tty", },
 	{},
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] tty: goldfish: Use streaming DMA for r/w operations on Ranchu platforms
  2017-08-22 15:56 ` [PATCH 1/2] tty: goldfish: Use streaming DMA for r/w operations on Ranchu platforms Aleksandar Markovic
@ 2017-08-22 19:00   ` Dan Carpenter
  2017-08-23 10:46     ` Miodrag Dinic
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2017-08-22 19:00 UTC (permalink / raw)
  To: Aleksandar Markovic
  Cc: linux-kernel, gregkh, jslaby, alan, jinqian, aleksandar.markovic,
	miodrag.dinic, petar.jovanovic, raghu.gandham, james.hogan, ralf

On Tue, Aug 22, 2017 at 05:56:36PM +0200, Aleksandar Markovic wrote:
>  drivers/tty/goldfish.c | 120 ++++++++++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 108 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/tty/goldfish.c b/drivers/tty/goldfish.c
> index 996bd47..ac98d5a 100644
> --- a/drivers/tty/goldfish.c
> +++ b/drivers/tty/goldfish.c
> @@ -22,6 +22,8 @@
>  #include <linux/io.h>
>  #include <linux/module.h>
>  #include <linux/goldfish.h>
> +#include <linux/mm.h>
> +#include <linux/dma-mapping.h>
>  
>  enum {
>  	GOLDFISH_TTY_PUT_CHAR       = 0x00,
> @@ -32,6 +34,8 @@ enum {
>  	GOLDFISH_TTY_DATA_LEN       = 0x14,
>  	GOLDFISH_TTY_DATA_PTR_HIGH  = 0x18,
>  
> +	GOLDFISH_TTY_VERSION		= 0x20,

This is an offset, right?  It's not version 32?  The name is misleading.
Maybe call it GOLDFISH_VERSION_REG.

> +
>  	GOLDFISH_TTY_CMD_INT_DISABLE    = 0,
>  	GOLDFISH_TTY_CMD_INT_ENABLE     = 1,
>  	GOLDFISH_TTY_CMD_WRITE_BUFFER   = 2,
> @@ -45,6 +49,8 @@ struct goldfish_tty {
>  	u32 irq;
>  	int opencount;
>  	struct console console;
> +	u32 version;
> +	struct device *dev;
>  };
>  
>  static DEFINE_MUTEX(goldfish_tty_lock);
> @@ -53,24 +59,93 @@ static u32 goldfish_tty_line_count = 8;
>  static u32 goldfish_tty_current_line_count;
>  static struct goldfish_tty *goldfish_ttys;
>  
> -static void goldfish_tty_do_write(int line, const char *buf, unsigned count)
> +static inline void do_rw_io(struct goldfish_tty *qtty,
          ^^^^^^
Don't make functions inline.  Leave that for the compiler to decide.
It's smarter than we are and it ignores our input anyway.

> +			    unsigned long address,
> +			    unsigned int count,
> +			    int is_write)
>  {
>  	unsigned long irq_flags;
> -	struct goldfish_tty *qtty = &goldfish_ttys[line];
>  	void __iomem *base = qtty->base;
> +
>  	spin_lock_irqsave(&qtty->lock, irq_flags);
> -	gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
> -				base + GOLDFISH_TTY_DATA_PTR_HIGH);
> +	gf_write_ptr((void *)address, base + GOLDFISH_TTY_DATA_PTR,
> +		     base + GOLDFISH_TTY_DATA_PTR_HIGH);
>  	writel(count, base + GOLDFISH_TTY_DATA_LEN);
> -	writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD);
> +
> +	if (is_write)
> +		writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD);
> +	else
> +		writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD);
> +
>  	spin_unlock_irqrestore(&qtty->lock, irq_flags);
>  }
>  
> +static inline void goldfish_tty_rw(struct goldfish_tty *qtty,
> +				   unsigned long addr,
> +				   unsigned int count,
> +				   int is_write)
> +{
> +	dma_addr_t dma_handle;
> +	enum dma_data_direction dma_dir;
> +
> +	dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
> +	if (qtty->version) {

It's cleaner to write be "if (qtty->version > 0)" because the version
numbers are numbers and not booleans.

> +		/*
> +		 * Goldfish TTY for Ranchu platform uses
> +		 * physical addresses and DMA for read/write operations
> +		 */
> +		unsigned long addr_end = addr + count;
> +
> +		while (addr < addr_end) {
> +			unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
> +			unsigned long next =
> +					pg_end < addr_end ? pg_end : addr_end;
> +			unsigned long avail = next - addr;
> +
> +			/*
> +			 * Map the buffer's virtual address to the DMA address
> +			 * so the buffer can be accessed by the device.
> +			 */
> +			dma_handle = dma_map_single(qtty->dev, (void *)addr,
> +						    avail, dma_dir);
> +
> +			if (dma_mapping_error(qtty->dev, dma_handle)) {
> +				dev_err(qtty->dev, "tty: DMA mapping error.\n");
> +				return;
> +			}
> +			do_rw_io(qtty, dma_handle, avail, is_write);
> +
> +			/*
> +			 * Unmap the previously mapped region after
> +			 * the completion of the read/write operation.
> +			 */
> +			dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
> +
> +			addr += avail;
> +		}
> +	} else {
> +		/*
> +		 * Old style Goldfish TTY used on the Goldfish platform
> +		 * uses virtual addresses.
> +		 */
> +		do_rw_io(qtty, addr, count, is_write);
> +	}
> +}
> +
> +static void goldfish_tty_do_write(int line, const char *buf,
> +				  unsigned int count)
> +{
> +	struct goldfish_tty *qtty = &goldfish_ttys[line];
> +	unsigned long address = (unsigned long)(void *)buf;
> +
> +	goldfish_tty_rw(qtty, address, count, 1);
> +}
> +
>  static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
>  {
>  	struct goldfish_tty *qtty = dev_id;
>  	void __iomem *base = qtty->base;
> -	unsigned long irq_flags;
> +	unsigned long address;
>  	unsigned char *buf;
>  	u32 count;
>  
> @@ -79,12 +154,10 @@ static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
>  		return IRQ_NONE;
>  
>  	count = tty_prepare_flip_string(&qtty->port, &buf, count);
> -	spin_lock_irqsave(&qtty->lock, irq_flags);
> -	gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
> -				base + GOLDFISH_TTY_DATA_PTR_HIGH);
> -	writel(count, base + GOLDFISH_TTY_DATA_LEN);
> -	writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD);
> -	spin_unlock_irqrestore(&qtty->lock, irq_flags);
> +
> +	address = (unsigned long)(void *)buf;
> +	goldfish_tty_rw(qtty, address, count, 0);
> +
>  	tty_schedule_flip(&qtty->port);
>  	return IRQ_HANDLED;
>  }
> @@ -271,6 +344,29 @@ static int goldfish_tty_probe(struct platform_device *pdev)
>  	qtty->port.ops = &goldfish_port_ops;
>  	qtty->base = base;
>  	qtty->irq = irq;
> +	qtty->dev = &pdev->dev;
> +
> +	/* Goldfish TTY device used by the Goldfish emulator
> +	 * should identify itself with 0, forcing the driver
> +	 * to use virtual addresses. Goldfish TTY device
> +	 * on Ranchu emulator (qemu2) returns 1 here and
> +	 * driver will use physical addresses.
> +	 */
> +	qtty->version = readl(base + GOLDFISH_TTY_VERSION);
> +
> +	/* Goldfish TTY device on Ranchu emulator (qemu2)
> +	 * will use DMA for read/write IO operations.
> +	 */
> +	if (qtty->version > 0) {
> +		/* Initialize dma_mask to 32-bits.
> +		 */
> +		if (!pdev->dev.dma_mask)
> +			pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
> +		if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
> +			dev_err(&pdev->dev, "No suitable DMA available.\n");
> +			goto err_create_driver_failed;

This goto is wrong and also "ret" isn't necessarily set correctly.
It's better to preserve the error code from dma_set_mask().

"Goto err_create_driver_failed" basically says that we just called
create_driver().  It's a come-from label name which is rubbish because
I can see just from looking at the patch, without any other context,
that actually dma_set_mask() failed and not create_driver()... Label
names should say what the goto does.  In this case what we want to do is
decrement goldfish_tty_current_line_count and call
goldfish_tty_delete_driver() if we're the last user.  A good label name
for this would be "goto dec_line_count;".  But actually the label which
does this is called "goto err_request_irq_failed;"...


regards,
dan carpenter

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH 1/2] tty: goldfish: Use streaming DMA for r/w operations on Ranchu platforms
  2017-08-22 19:00   ` Dan Carpenter
@ 2017-08-23 10:46     ` Miodrag Dinic
  0 siblings, 0 replies; 5+ messages in thread
From: Miodrag Dinic @ 2017-08-23 10:46 UTC (permalink / raw)
  To: Dan Carpenter, Aleksandar Markovic
  Cc: linux-kernel, gregkh, jslaby, alan, jinqian, Aleksandar Markovic,
	Petar Jovanovic, Raghu Gandham, James Hogan, ralf

Hello Dan,

thank you for your comments. Please find the answers inline:

> >  
> > +     GOLDFISH_TTY_VERSION            = 0x20,
> 
> This is an offset, right?  It's not version 32?  The name is misleading.
> Maybe call it GOLDFISH_VERSION_REG.> -static void goldfish_tty_do_write(int line, const char *buf, unsigned count)

Yes I guess it is a little bit misleading.
We will refactor this according to your suggestion in the next version.
Thanks.

> > +static inline void do_rw_io(struct goldfish_tty *qtty,
>           ^^^^^^
> Don't make functions inline.  Leave that for the compiler to decide.
> It's smarter than we are and it ignores our input anyway.

It will be fixed in next version.

> +     if (qtty->version) {
> 
> It's cleaner to write be "if (qtty->version > 0)" because the version
> numbers are numbers and not booleans.

Ditto.

> > +             if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
> > +                     dev_err(&pdev->dev, "No suitable DMA available.\n");
> > +                     goto err_create_driver_failed;
> 
> This goto is wrong and also "ret" isn't necessarily set correctly.
> It's better to preserve the error code from dma_set_mask().
> 
> "Goto err_create_driver_failed" basically says that we just called
> create_driver().  It's a come-from label name which is rubbish because
> I can see just from looking at the patch, without any other context,
> that actually dma_set_mask() failed and not create_driver()... Label
> names should say what the goto does.  In this case what we want to do is
> decrement goldfish_tty_current_line_count and call
> goldfish_tty_delete_driver() if we're the last user.  A good label name
> for this would be "goto dec_line_count;".  But actually the label which
> does this is called "goto err_request_irq_failed;"...

Thank you for the suggestions. We will make appropriate changes and submit
the next patch version soon.

Kind regards,
Miodrag
________________________________________
From: Dan Carpenter [dan.carpenter@oracle.com]
Sent: Tuesday, August 22, 2017 9:00 PM
To: Aleksandar Markovic
Cc: linux-kernel@vger.kernel.org; gregkh@linuxfoundation.org; jslaby@suse.com; alan@linux.intel.com; jinqian@android.com; Aleksandar Markovic; Miodrag Dinic; Petar Jovanovic; Raghu Gandham; James Hogan; ralf@linux-mips.org
Subject: Re: [PATCH 1/2] tty: goldfish: Use streaming DMA for r/w operations on Ranchu platforms

On Tue, Aug 22, 2017 at 05:56:36PM +0200, Aleksandar Markovic wrote:
>  drivers/tty/goldfish.c | 120 ++++++++++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 108 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/tty/goldfish.c b/drivers/tty/goldfish.c
> index 996bd47..ac98d5a 100644
> --- a/drivers/tty/goldfish.c
> +++ b/drivers/tty/goldfish.c
> @@ -22,6 +22,8 @@
>  #include <linux/io.h>
>  #include <linux/module.h>
>  #include <linux/goldfish.h>
> +#include <linux/mm.h>
> +#include <linux/dma-mapping.h>
>
>  enum {
>       GOLDFISH_TTY_PUT_CHAR       = 0x00,
> @@ -32,6 +34,8 @@ enum {
>       GOLDFISH_TTY_DATA_LEN       = 0x14,
>       GOLDFISH_TTY_DATA_PTR_HIGH  = 0x18,
>
> +     GOLDFISH_TTY_VERSION            = 0x20,

This is an offset, right?  It's not version 32?  The name is misleading.
Maybe call it GOLDFISH_VERSION_REG.

> +
>       GOLDFISH_TTY_CMD_INT_DISABLE    = 0,
>       GOLDFISH_TTY_CMD_INT_ENABLE     = 1,
>       GOLDFISH_TTY_CMD_WRITE_BUFFER   = 2,
> @@ -45,6 +49,8 @@ struct goldfish_tty {
>       u32 irq;
>       int opencount;
>       struct console console;
> +     u32 version;
> +     struct device *dev;
>  };
>
>  static DEFINE_MUTEX(goldfish_tty_lock);
> @@ -53,24 +59,93 @@ static u32 goldfish_tty_line_count = 8;
>  static u32 goldfish_tty_current_line_count;
>  static struct goldfish_tty *goldfish_ttys;
>
> -static void goldfish_tty_do_write(int line, const char *buf, unsigned count)
> +static inline void do_rw_io(struct goldfish_tty *qtty,
          ^^^^^^
Don't make functions inline.  Leave that for the compiler to decide.
It's smarter than we are and it ignores our input anyway.

> +                         unsigned long address,
> +                         unsigned int count,
> +                         int is_write)
>  {
>       unsigned long irq_flags;
> -     struct goldfish_tty *qtty = &goldfish_ttys[line];
>       void __iomem *base = qtty->base;
> +
>       spin_lock_irqsave(&qtty->lock, irq_flags);
> -     gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
> -                             base + GOLDFISH_TTY_DATA_PTR_HIGH);
> +     gf_write_ptr((void *)address, base + GOLDFISH_TTY_DATA_PTR,
> +                  base + GOLDFISH_TTY_DATA_PTR_HIGH);
>       writel(count, base + GOLDFISH_TTY_DATA_LEN);
> -     writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD);
> +
> +     if (is_write)
> +             writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD);
> +     else
> +             writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD);
> +
>       spin_unlock_irqrestore(&qtty->lock, irq_flags);
>  }
>
> +static inline void goldfish_tty_rw(struct goldfish_tty *qtty,
> +                                unsigned long addr,
> +                                unsigned int count,
> +                                int is_write)
> +{
> +     dma_addr_t dma_handle;
> +     enum dma_data_direction dma_dir;
> +
> +     dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
> +     if (qtty->version) {

It's cleaner to write be "if (qtty->version > 0)" because the version
numbers are numbers and not booleans.

> +             /*
> +              * Goldfish TTY for Ranchu platform uses
> +              * physical addresses and DMA for read/write operations
> +              */
> +             unsigned long addr_end = addr + count;
> +
> +             while (addr < addr_end) {
> +                     unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
> +                     unsigned long next =
> +                                     pg_end < addr_end ? pg_end : addr_end;
> +                     unsigned long avail = next - addr;
> +
> +                     /*
> +                      * Map the buffer's virtual address to the DMA address
> +                      * so the buffer can be accessed by the device.
> +                      */
> +                     dma_handle = dma_map_single(qtty->dev, (void *)addr,
> +                                                 avail, dma_dir);
> +
> +                     if (dma_mapping_error(qtty->dev, dma_handle)) {
> +                             dev_err(qtty->dev, "tty: DMA mapping error.\n");
> +                             return;
> +                     }
> +                     do_rw_io(qtty, dma_handle, avail, is_write);
> +
> +                     /*
> +                      * Unmap the previously mapped region after
> +                      * the completion of the read/write operation.
> +                      */
> +                     dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
> +
> +                     addr += avail;
> +             }
> +     } else {
> +             /*
> +              * Old style Goldfish TTY used on the Goldfish platform
> +              * uses virtual addresses.
> +              */
> +             do_rw_io(qtty, addr, count, is_write);
> +     }
> +}
> +
> +static void goldfish_tty_do_write(int line, const char *buf,
> +                               unsigned int count)
> +{
> +     struct goldfish_tty *qtty = &goldfish_ttys[line];
> +     unsigned long address = (unsigned long)(void *)buf;
> +
> +     goldfish_tty_rw(qtty, address, count, 1);
> +}
> +
>  static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
>  {
>       struct goldfish_tty *qtty = dev_id;
>       void __iomem *base = qtty->base;
> -     unsigned long irq_flags;
> +     unsigned long address;
>       unsigned char *buf;
>       u32 count;
>
> @@ -79,12 +154,10 @@ static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
>               return IRQ_NONE;
>
>       count = tty_prepare_flip_string(&qtty->port, &buf, count);
> -     spin_lock_irqsave(&qtty->lock, irq_flags);
> -     gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
> -                             base + GOLDFISH_TTY_DATA_PTR_HIGH);
> -     writel(count, base + GOLDFISH_TTY_DATA_LEN);
> -     writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD);
> -     spin_unlock_irqrestore(&qtty->lock, irq_flags);
> +
> +     address = (unsigned long)(void *)buf;
> +     goldfish_tty_rw(qtty, address, count, 0);
> +
>       tty_schedule_flip(&qtty->port);
>       return IRQ_HANDLED;
>  }
> @@ -271,6 +344,29 @@ static int goldfish_tty_probe(struct platform_device *pdev)
>       qtty->port.ops = &goldfish_port_ops;
>       qtty->base = base;
>       qtty->irq = irq;
> +     qtty->dev = &pdev->dev;
> +
> +     /* Goldfish TTY device used by the Goldfish emulator
> +      * should identify itself with 0, forcing the driver
> +      * to use virtual addresses. Goldfish TTY device
> +      * on Ranchu emulator (qemu2) returns 1 here and
> +      * driver will use physical addresses.
> +      */
> +     qtty->version = readl(base + GOLDFISH_TTY_VERSION);
> +
> +     /* Goldfish TTY device on Ranchu emulator (qemu2)
> +      * will use DMA for read/write IO operations.
> +      */
> +     if (qtty->version > 0) {
> +             /* Initialize dma_mask to 32-bits.
> +              */
> +             if (!pdev->dev.dma_mask)
> +                     pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
> +             if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
> +                     dev_err(&pdev->dev, "No suitable DMA available.\n");
> +                     goto err_create_driver_failed;

This goto is wrong and also "ret" isn't necessarily set correctly.
It's better to preserve the error code from dma_set_mask().

"Goto err_create_driver_failed" basically says that we just called
create_driver().  It's a come-from label name which is rubbish because
I can see just from looking at the patch, without any other context,
that actually dma_set_mask() failed and not create_driver()... Label
names should say what the goto does.  In this case what we want to do is
decrement goldfish_tty_current_line_count and call
goldfish_tty_delete_driver() if we're the last user.  A good label name
for this would be "goto dec_line_count;".  But actually the label which
does this is called "goto err_request_irq_failed;"...


regards,
dan carpenter

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-08-23 11:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-22 15:56 [PATCH 0/2] Amend tty Goldfish driver functionality Aleksandar Markovic
2017-08-22 15:56 ` [PATCH 1/2] tty: goldfish: Use streaming DMA for r/w operations on Ranchu platforms Aleksandar Markovic
2017-08-22 19:00   ` Dan Carpenter
2017-08-23 10:46     ` Miodrag Dinic
2017-08-22 15:56 ` [PATCH 2/2] tty: goldfish: Implement support for kernel 'earlycon' parameter Aleksandar Markovic

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.