diff mbox series

[v3,5/5] staging: vchiq: Combine vchiq platform code into single file

Message ID b2e9eaee3e6d8f278a3277aaa284c5ca8b76d756.1625401928.git.ojaswin98@gmail.com (mailing list archive)
State New, archived
Headers show
Series vchiq: Patch to separate platform and cdev code | expand

Commit Message

Ojaswin Mujoo July 4, 2021, 3:59 p.m. UTC
Combine the vchiq platform initialization code into a single file by
merging vchiq_2835_arm.c into vchiq_arm.c

Signed-off-by: Ojaswin Mujoo <ojaswin98@gmail.com>
---
 drivers/staging/vc04_services/Makefile        |   1 -
 .../interface/vchiq_arm/vchiq_2835_arm.c      | 564 ------------------
 .../interface/vchiq_arm/vchiq_arm.c           | 549 +++++++++++++++++
 3 files changed, 549 insertions(+), 565 deletions(-)
 delete mode 100644 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c

Comments

kernel test robot July 4, 2021, 6:07 p.m. UTC | #1
Hi Ojaswin,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on staging/staging-testing]
[also build test WARNING on next-20210701]
[cannot apply to v5.13]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Ojaswin-Mujoo/vchiq-Patch-to-separate-platform-and-cdev-code/20210705-000124
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git 77ad1f0e99bd00af024e650b862cfda3137af660
config: x86_64-randconfig-a004-20210704 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 89c1c64cc3170a05a881bb9954feafc3edca6704)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install x86_64 cross compiling tool for clang build
        # apt-get install binutils-x86-64-linux-gnu
        # https://github.com/0day-ci/linux/commit/6e25765ec91ba341b2c85fdd00da9ef4c2ed737c
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Ojaswin-Mujoo/vchiq-Patch-to-separate-platform-and-cdev-code/20210705-000124
        git checkout 6e25765ec91ba341b2c85fdd00da9ef4c2ed737c
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c:378:16: warning: result of comparison of constant 419244183493398898 with expression of type 'unsigned int' is always false [-Wtautological-constant-out-of-range-compare]
           if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
               ~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   1 warning generated.


vim +378 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c

   345	
   346	/* There is a potential problem with partial cache lines (pages?)
   347	 * at the ends of the block when reading. If the CPU accessed anything in
   348	 * the same line (page?) then it may have pulled old data into the cache,
   349	 * obscuring the new data underneath. We can solve this by transferring the
   350	 * partial cache lines separately, and allowing the ARM to copy into the
   351	 * cached area.
   352	 */
   353	
   354	static struct vchiq_pagelist_info *
   355	create_pagelist(char *buf, char __user *ubuf,
   356			size_t count, unsigned short type)
   357	{
   358		struct pagelist *pagelist;
   359		struct vchiq_pagelist_info *pagelistinfo;
   360		struct page **pages;
   361		u32 *addrs;
   362		unsigned int num_pages, offset, i, k;
   363		int actual_pages;
   364		size_t pagelist_size;
   365		struct scatterlist *scatterlist, *sg;
   366		int dma_buffers;
   367		dma_addr_t dma_addr;
   368	
   369		if (count >= INT_MAX - PAGE_SIZE)
   370			return NULL;
   371	
   372		if (buf)
   373			offset = (uintptr_t)buf & (PAGE_SIZE - 1);
   374		else
   375			offset = (uintptr_t)ubuf & (PAGE_SIZE - 1);
   376		num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
   377	
 > 378		if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
   379				 sizeof(struct vchiq_pagelist_info)) /
   380				(sizeof(u32) + sizeof(pages[0]) +
   381				 sizeof(struct scatterlist)))
   382			return NULL;
   383	
   384		pagelist_size = sizeof(struct pagelist) +
   385				(num_pages * sizeof(u32)) +
   386				(num_pages * sizeof(pages[0]) +
   387				(num_pages * sizeof(struct scatterlist))) +
   388				sizeof(struct vchiq_pagelist_info);
   389	
   390		/* Allocate enough storage to hold the page pointers and the page
   391		 * list
   392		 */
   393		pagelist = dma_alloc_coherent(g_dev, pagelist_size, &dma_addr,
   394					      GFP_KERNEL);
   395	
   396		vchiq_log_trace(vchiq_arm_log_level, "%s - %pK", __func__, pagelist);
   397	
   398		if (!pagelist)
   399			return NULL;
   400	
   401		addrs		= pagelist->addrs;
   402		pages		= (struct page **)(addrs + num_pages);
   403		scatterlist	= (struct scatterlist *)(pages + num_pages);
   404		pagelistinfo	= (struct vchiq_pagelist_info *)
   405				  (scatterlist + num_pages);
   406	
   407		pagelist->length = count;
   408		pagelist->type = type;
   409		pagelist->offset = offset;
   410	
   411		/* Populate the fields of the pagelistinfo structure */
   412		pagelistinfo->pagelist = pagelist;
   413		pagelistinfo->pagelist_buffer_size = pagelist_size;
   414		pagelistinfo->dma_addr = dma_addr;
   415		pagelistinfo->dma_dir =  (type == PAGELIST_WRITE) ?
   416					  DMA_TO_DEVICE : DMA_FROM_DEVICE;
   417		pagelistinfo->num_pages = num_pages;
   418		pagelistinfo->pages_need_release = 0;
   419		pagelistinfo->pages = pages;
   420		pagelistinfo->scatterlist = scatterlist;
   421		pagelistinfo->scatterlist_mapped = 0;
   422	
   423		if (buf) {
   424			unsigned long length = count;
   425			unsigned int off = offset;
   426	
   427			for (actual_pages = 0; actual_pages < num_pages;
   428			     actual_pages++) {
   429				struct page *pg =
   430					vmalloc_to_page((buf +
   431							 (actual_pages * PAGE_SIZE)));
   432				size_t bytes = PAGE_SIZE - off;
   433	
   434				if (!pg) {
   435					cleanup_pagelistinfo(pagelistinfo);
   436					return NULL;
   437				}
   438	
   439				if (bytes > length)
   440					bytes = length;
   441				pages[actual_pages] = pg;
   442				length -= bytes;
   443				off = 0;
   444			}
   445			/* do not try and release vmalloc pages */
   446		} else {
   447			actual_pages = pin_user_pages_fast(
   448						  (unsigned long)ubuf & PAGE_MASK,
   449						  num_pages,
   450						  type == PAGELIST_READ,
   451						  pages);
   452	
   453			if (actual_pages != num_pages) {
   454				vchiq_log_info(vchiq_arm_log_level,
   455					       "%s - only %d/%d pages locked",
   456					       __func__, actual_pages, num_pages);
   457	
   458				/* This is probably due to the process being killed */
   459				if (actual_pages > 0)
   460					unpin_user_pages(pages, actual_pages);
   461				cleanup_pagelistinfo(pagelistinfo);
   462				return NULL;
   463			}
   464			 /* release user pages */
   465			pagelistinfo->pages_need_release = 1;
   466		}
   467	
   468		/*
   469		 * Initialize the scatterlist so that the magic cookie
   470		 *  is filled if debugging is enabled
   471		 */
   472		sg_init_table(scatterlist, num_pages);
   473		/* Now set the pages for each scatterlist */
   474		for (i = 0; i < num_pages; i++)	{
   475			unsigned int len = PAGE_SIZE - offset;
   476	
   477			if (len > count)
   478				len = count;
   479			sg_set_page(scatterlist + i, pages[i], len, offset);
   480			offset = 0;
   481			count -= len;
   482		}
   483	
   484		dma_buffers = dma_map_sg(g_dev,
   485					 scatterlist,
   486					 num_pages,
   487					 pagelistinfo->dma_dir);
   488	
   489		if (dma_buffers == 0) {
   490			cleanup_pagelistinfo(pagelistinfo);
   491			return NULL;
   492		}
   493	
   494		pagelistinfo->scatterlist_mapped = 1;
   495	
   496		/* Combine adjacent blocks for performance */
   497		k = 0;
   498		for_each_sg(scatterlist, sg, dma_buffers, i) {
   499			u32 len = sg_dma_len(sg);
   500			u32 addr = sg_dma_address(sg);
   501	
   502			/* Note: addrs is the address + page_count - 1
   503			 * The firmware expects blocks after the first to be page-
   504			 * aligned and a multiple of the page size
   505			 */
   506			WARN_ON(len == 0);
   507			WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
   508			WARN_ON(i && (addr & ~PAGE_MASK));
   509			if (k > 0 &&
   510			    ((addrs[k - 1] & PAGE_MASK) +
   511			     (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
   512			    == (addr & PAGE_MASK))
   513				addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
   514			else
   515				addrs[k++] = (addr & PAGE_MASK) |
   516					(((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
   517		}
   518	
   519		/* Partial cache lines (fragments) require special measures */
   520		if ((type == PAGELIST_READ) &&
   521			((pagelist->offset & (g_cache_line_size - 1)) ||
   522			((pagelist->offset + pagelist->length) &
   523			(g_cache_line_size - 1)))) {
   524			char *fragments;
   525	
   526			if (down_interruptible(&g_free_fragments_sema)) {
   527				cleanup_pagelistinfo(pagelistinfo);
   528				return NULL;
   529			}
   530	
   531			WARN_ON(!g_free_fragments);
   532	
   533			down(&g_free_fragments_mutex);
   534			fragments = g_free_fragments;
   535			WARN_ON(!fragments);
   536			g_free_fragments = *(char **) g_free_fragments;
   537			up(&g_free_fragments_mutex);
   538			pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
   539				(fragments - g_fragments_base) / g_fragments_size;
   540		}
   541	
   542		return pagelistinfo;
   543	}
   544	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
Stefan Wahren July 11, 2021, 10:49 a.m. UTC | #2
Am 04.07.21 um 17:59 schrieb Ojaswin Mujoo:
> Combine the vchiq platform initialization code into a single file by
> merging vchiq_2835_arm.c into vchiq_arm.c
>
> Signed-off-by: Ojaswin Mujoo <ojaswin98@gmail.com>
> ---
>  drivers/staging/vc04_services/Makefile        |   1 -
>  .../interface/vchiq_arm/vchiq_2835_arm.c      | 564 ------------------
>  .../interface/vchiq_arm/vchiq_arm.c           | 549 +++++++++++++++++
>  3 files changed, 549 insertions(+), 565 deletions(-)
>  delete mode 100644 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
>
> diff --git a/drivers/staging/vc04_services/Makefile b/drivers/staging/vc04_services/Makefile
> index 0a04338fc962..1fd191e2e2a5 100644
> --- a/drivers/staging/vc04_services/Makefile
> +++ b/drivers/staging/vc04_services/Makefile
> @@ -4,7 +4,6 @@ obj-$(CONFIG_BCM2835_VCHIQ)	+= vchiq.o
>  vchiq-objs := \
>     interface/vchiq_arm/vchiq_core.o  \
>     interface/vchiq_arm/vchiq_arm.o \
> -   interface/vchiq_arm/vchiq_2835_arm.o \
>     interface/vchiq_arm/vchiq_debugfs.o \
>     interface/vchiq_arm/vchiq_connected.o \
>  
...
> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> index 0f2de571eba7..9057d01ffd48 100644
> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> @@ -25,15 +25,32 @@
>  #include <linux/rcupdate.h>
>  #include <linux/delay.h>
>  #include <linux/slab.h>
> +#include <linux/interrupt.h>
> +#include <linux/io.h>
> +#include <linux/uaccess.h>
>  #include <soc/bcm2835/raspberrypi-firmware.h>
>  
>  #include "vchiq_core.h"
>  #include "vchiq_ioctl.h"
>  #include "vchiq_arm.h"
>  #include "vchiq_debugfs.h"
> +#include "vchiq_connected.h"
> +#include "vchiq_pagelist.h"
>  
>  #define DEVICE_NAME "vchiq"
>  
> +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
> +
> +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
> +
> +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
> +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX  1
> +
> +#define BELL0	0x00
> +#define BELL2	0x08
> +
> +#define ARM_DS_ACTIVE	BIT(2)
> +
>  /* Override the default prefix, which would be vchiq_arm (from the filename) */
>  #undef MODULE_PARAM_PREFIX
>  #define MODULE_PARAM_PREFIX DEVICE_NAME "."
> @@ -59,10 +76,542 @@ static struct vchiq_drvdata bcm2836_drvdata = {
>  	.cache_line_size = 64,
>  };
>  
> +struct vchiq_2835_state {
> +	int inited;
> +	struct vchiq_arm_state arm_state;
> +};
> +
> +struct vchiq_pagelist_info {
> +	struct pagelist *pagelist;
> +	size_t pagelist_buffer_size;
> +	dma_addr_t dma_addr;
> +	enum dma_data_direction dma_dir;
> +	unsigned int num_pages;
> +	unsigned int pages_need_release;
> +	struct page **pages;
> +	struct scatterlist *scatterlist;
> +	unsigned int scatterlist_mapped;
> +};
> +
> +static void __iomem *g_regs;
> +/* This value is the size of the L2 cache lines as understood by the
> + * VPU firmware, which determines the required alignment of the
> + * offsets/sizes in pagelists.
> + *
> + * Modern VPU firmware looks for a DT "cache-line-size" property in
> + * the VCHIQ node and will overwrite it with the actual L2 cache size,
> + * which the kernel must then respect.  That property was rejected
> + * upstream, so we have to use the VPU firmware's compatibility value
> + * of 32.
> + */
> +static unsigned int g_cache_line_size = 32;
> +static unsigned int g_fragments_size;
> +static char *g_fragments_base;
> +static char *g_free_fragments;
> +static struct semaphore g_free_fragments_sema;
> +static struct device *g_dev;
> +
> +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
> +
> +static irqreturn_t
> +vchiq_doorbell_irq(int irq, void *dev_id);
> +
> +static struct vchiq_pagelist_info *
> +create_pagelist(char *buf, char __user *ubuf, size_t count, unsigned short type);
> +
> +static void
> +free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
> +	      int actual);

please no forward declarations of these 3 functions. Put them into the
right order instead ...

Since this patch is independent from the other ones from the series,
maybe Greg can merg the rest of the series.
Ojaswin Mujoo July 11, 2021, 11:28 a.m. UTC | #3
On Sun, Jul 11, 2021 at 12:49:35PM +0200, Stefan Wahren wrote:
> Am 04.07.21 um 17:59 schrieb Ojaswin Mujoo:
> > Combine the vchiq platform initialization code into a single file by
> > merging vchiq_2835_arm.c into vchiq_arm.c
> >
> > Signed-off-by: Ojaswin Mujoo <ojaswin98@gmail.com>
> > ---
> >  drivers/staging/vc04_services/Makefile        |   1 -
> >  .../interface/vchiq_arm/vchiq_2835_arm.c      | 564 ------------------
> >  .../interface/vchiq_arm/vchiq_arm.c           | 549 +++++++++++++++++
> >  3 files changed, 549 insertions(+), 565 deletions(-)
> >  delete mode 100644 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
> >
> > diff --git a/drivers/staging/vc04_services/Makefile b/drivers/staging/vc04_services/Makefile
> > index 0a04338fc962..1fd191e2e2a5 100644
> > --- a/drivers/staging/vc04_services/Makefile
> > +++ b/drivers/staging/vc04_services/Makefile
> > @@ -4,7 +4,6 @@ obj-$(CONFIG_BCM2835_VCHIQ)	+= vchiq.o
> >  vchiq-objs := \
> >     interface/vchiq_arm/vchiq_core.o  \
> >     interface/vchiq_arm/vchiq_arm.o \
> > -   interface/vchiq_arm/vchiq_2835_arm.o \
> >     interface/vchiq_arm/vchiq_debugfs.o \
> >     interface/vchiq_arm/vchiq_connected.o \
> >  
> ...
> > diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> > index 0f2de571eba7..9057d01ffd48 100644
> > --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> > +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> > @@ -25,15 +25,32 @@
> >  #include <linux/rcupdate.h>
> >  #include <linux/delay.h>
> >  #include <linux/slab.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/io.h>
> > +#include <linux/uaccess.h>
> >  #include <soc/bcm2835/raspberrypi-firmware.h>
> >  
> >  #include "vchiq_core.h"
> >  #include "vchiq_ioctl.h"
> >  #include "vchiq_arm.h"
> >  #include "vchiq_debugfs.h"
> > +#include "vchiq_connected.h"
> > +#include "vchiq_pagelist.h"
> >  
> >  #define DEVICE_NAME "vchiq"
> >  
> > +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
> > +
> > +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
> > +
> > +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
> > +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX  1
> > +
> > +#define BELL0	0x00
> > +#define BELL2	0x08
> > +
> > +#define ARM_DS_ACTIVE	BIT(2)
> > +
> >  /* Override the default prefix, which would be vchiq_arm (from the filename) */
> >  #undef MODULE_PARAM_PREFIX
> >  #define MODULE_PARAM_PREFIX DEVICE_NAME "."
> > @@ -59,10 +76,542 @@ static struct vchiq_drvdata bcm2836_drvdata = {
> >  	.cache_line_size = 64,
> >  };
> >  
> > +struct vchiq_2835_state {
> > +	int inited;
> > +	struct vchiq_arm_state arm_state;
> > +};
> > +
> > +struct vchiq_pagelist_info {
> > +	struct pagelist *pagelist;
> > +	size_t pagelist_buffer_size;
> > +	dma_addr_t dma_addr;
> > +	enum dma_data_direction dma_dir;
> > +	unsigned int num_pages;
> > +	unsigned int pages_need_release;
> > +	struct page **pages;
> > +	struct scatterlist *scatterlist;
> > +	unsigned int scatterlist_mapped;
> > +};
> > +
> > +static void __iomem *g_regs;
> > +/* This value is the size of the L2 cache lines as understood by the
> > + * VPU firmware, which determines the required alignment of the
> > + * offsets/sizes in pagelists.
> > + *
> > + * Modern VPU firmware looks for a DT "cache-line-size" property in
> > + * the VCHIQ node and will overwrite it with the actual L2 cache size,
> > + * which the kernel must then respect.  That property was rejected
> > + * upstream, so we have to use the VPU firmware's compatibility value
> > + * of 32.
> > + */
> > +static unsigned int g_cache_line_size = 32;
> > +static unsigned int g_fragments_size;
> > +static char *g_fragments_base;
> > +static char *g_free_fragments;
> > +static struct semaphore g_free_fragments_sema;
> > +static struct device *g_dev;
> > +
> > +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
> > +
> > +static irqreturn_t
> > +vchiq_doorbell_irq(int irq, void *dev_id);
> > +
> > +static struct vchiq_pagelist_info *
> > +create_pagelist(char *buf, char __user *ubuf, size_t count, unsigned short type);
> > +
> > +static void
> > +free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
> > +	      int actual);
> 
> please no forward declarations of these 3 functions. Put them into the
> right order instead ...
> 
> Since this patch is independent from the other ones from the series,
> maybe Greg can merg the rest of the series.
> 
> 
> 

Hello Stefan,

Thanks for the review. As for the forward declerations, sure I can fix
these 3 functions and send an independent patch for this.

Regards,
Ojaswin
Greg KH July 21, 2021, 8:22 a.m. UTC | #4
On Sun, Jul 11, 2021 at 04:58:21PM +0530, Ojaswin Mujoo wrote:
> On Sun, Jul 11, 2021 at 12:49:35PM +0200, Stefan Wahren wrote:
> > Am 04.07.21 um 17:59 schrieb Ojaswin Mujoo:
> > > Combine the vchiq platform initialization code into a single file by
> > > merging vchiq_2835_arm.c into vchiq_arm.c
> > >
> > > Signed-off-by: Ojaswin Mujoo <ojaswin98@gmail.com>
> > > ---
> > >  drivers/staging/vc04_services/Makefile        |   1 -
> > >  .../interface/vchiq_arm/vchiq_2835_arm.c      | 564 ------------------
> > >  .../interface/vchiq_arm/vchiq_arm.c           | 549 +++++++++++++++++
> > >  3 files changed, 549 insertions(+), 565 deletions(-)
> > >  delete mode 100644 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
> > >
> > > diff --git a/drivers/staging/vc04_services/Makefile b/drivers/staging/vc04_services/Makefile
> > > index 0a04338fc962..1fd191e2e2a5 100644
> > > --- a/drivers/staging/vc04_services/Makefile
> > > +++ b/drivers/staging/vc04_services/Makefile
> > > @@ -4,7 +4,6 @@ obj-$(CONFIG_BCM2835_VCHIQ)	+= vchiq.o
> > >  vchiq-objs := \
> > >     interface/vchiq_arm/vchiq_core.o  \
> > >     interface/vchiq_arm/vchiq_arm.o \
> > > -   interface/vchiq_arm/vchiq_2835_arm.o \
> > >     interface/vchiq_arm/vchiq_debugfs.o \
> > >     interface/vchiq_arm/vchiq_connected.o \
> > >  
> > ...
> > > diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> > > index 0f2de571eba7..9057d01ffd48 100644
> > > --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> > > +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> > > @@ -25,15 +25,32 @@
> > >  #include <linux/rcupdate.h>
> > >  #include <linux/delay.h>
> > >  #include <linux/slab.h>
> > > +#include <linux/interrupt.h>
> > > +#include <linux/io.h>
> > > +#include <linux/uaccess.h>
> > >  #include <soc/bcm2835/raspberrypi-firmware.h>
> > >  
> > >  #include "vchiq_core.h"
> > >  #include "vchiq_ioctl.h"
> > >  #include "vchiq_arm.h"
> > >  #include "vchiq_debugfs.h"
> > > +#include "vchiq_connected.h"
> > > +#include "vchiq_pagelist.h"
> > >  
> > >  #define DEVICE_NAME "vchiq"
> > >  
> > > +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
> > > +
> > > +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
> > > +
> > > +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
> > > +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX  1
> > > +
> > > +#define BELL0	0x00
> > > +#define BELL2	0x08
> > > +
> > > +#define ARM_DS_ACTIVE	BIT(2)
> > > +
> > >  /* Override the default prefix, which would be vchiq_arm (from the filename) */
> > >  #undef MODULE_PARAM_PREFIX
> > >  #define MODULE_PARAM_PREFIX DEVICE_NAME "."
> > > @@ -59,10 +76,542 @@ static struct vchiq_drvdata bcm2836_drvdata = {
> > >  	.cache_line_size = 64,
> > >  };
> > >  
> > > +struct vchiq_2835_state {
> > > +	int inited;
> > > +	struct vchiq_arm_state arm_state;
> > > +};
> > > +
> > > +struct vchiq_pagelist_info {
> > > +	struct pagelist *pagelist;
> > > +	size_t pagelist_buffer_size;
> > > +	dma_addr_t dma_addr;
> > > +	enum dma_data_direction dma_dir;
> > > +	unsigned int num_pages;
> > > +	unsigned int pages_need_release;
> > > +	struct page **pages;
> > > +	struct scatterlist *scatterlist;
> > > +	unsigned int scatterlist_mapped;
> > > +};
> > > +
> > > +static void __iomem *g_regs;
> > > +/* This value is the size of the L2 cache lines as understood by the
> > > + * VPU firmware, which determines the required alignment of the
> > > + * offsets/sizes in pagelists.
> > > + *
> > > + * Modern VPU firmware looks for a DT "cache-line-size" property in
> > > + * the VCHIQ node and will overwrite it with the actual L2 cache size,
> > > + * which the kernel must then respect.  That property was rejected
> > > + * upstream, so we have to use the VPU firmware's compatibility value
> > > + * of 32.
> > > + */
> > > +static unsigned int g_cache_line_size = 32;
> > > +static unsigned int g_fragments_size;
> > > +static char *g_fragments_base;
> > > +static char *g_free_fragments;
> > > +static struct semaphore g_free_fragments_sema;
> > > +static struct device *g_dev;
> > > +
> > > +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
> > > +
> > > +static irqreturn_t
> > > +vchiq_doorbell_irq(int irq, void *dev_id);
> > > +
> > > +static struct vchiq_pagelist_info *
> > > +create_pagelist(char *buf, char __user *ubuf, size_t count, unsigned short type);
> > > +
> > > +static void
> > > +free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
> > > +	      int actual);
> > 
> > please no forward declarations of these 3 functions. Put them into the
> > right order instead ...
> > 
> > Since this patch is independent from the other ones from the series,
> > maybe Greg can merg the rest of the series.
> > 
> > 
> > 
> 
> Hello Stefan,
> 
> Thanks for the review. As for the forward declerations, sure I can fix
> these 3 functions and send an independent patch for this.

Please fix up and resend the whole series properly so that I can apply
them that way.

thanks,

greg k-h
Ojaswin Mujoo July 21, 2021, 4:28 p.m. UTC | #5
On Wed, Jul 21, 2021 at 10:22:51AM +0200, Greg KH wrote:
> On Sun, Jul 11, 2021 at 04:58:21PM +0530, Ojaswin Mujoo wrote:
> > On Sun, Jul 11, 2021 at 12:49:35PM +0200, Stefan Wahren wrote:
> > > Am 04.07.21 um 17:59 schrieb Ojaswin Mujoo:
> > > > Combine the vchiq platform initialization code into a single file by
> > > > merging vchiq_2835_arm.c into vchiq_arm.c
> > > >
> > > > Signed-off-by: Ojaswin Mujoo <ojaswin98@gmail.com>
> > > > ---
> > > >  drivers/staging/vc04_services/Makefile        |   1 -
> > > >  .../interface/vchiq_arm/vchiq_2835_arm.c      | 564 ------------------
> > > >  .../interface/vchiq_arm/vchiq_arm.c           | 549 +++++++++++++++++
> > > >  3 files changed, 549 insertions(+), 565 deletions(-)
> > > >  delete mode 100644 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
> > > >
> > > > diff --git a/drivers/staging/vc04_services/Makefile b/drivers/staging/vc04_services/Makefile
> > > > index 0a04338fc962..1fd191e2e2a5 100644
> > > > --- a/drivers/staging/vc04_services/Makefile
> > > > +++ b/drivers/staging/vc04_services/Makefile
> > > > @@ -4,7 +4,6 @@ obj-$(CONFIG_BCM2835_VCHIQ)	+= vchiq.o
> > > >  vchiq-objs := \
> > > >     interface/vchiq_arm/vchiq_core.o  \
> > > >     interface/vchiq_arm/vchiq_arm.o \
> > > > -   interface/vchiq_arm/vchiq_2835_arm.o \
> > > >     interface/vchiq_arm/vchiq_debugfs.o \
> > > >     interface/vchiq_arm/vchiq_connected.o \
> > > >  
> > > ...
> > > > diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> > > > index 0f2de571eba7..9057d01ffd48 100644
> > > > --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> > > > +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> > > > @@ -25,15 +25,32 @@
> > > >  #include <linux/rcupdate.h>
> > > >  #include <linux/delay.h>
> > > >  #include <linux/slab.h>
> > > > +#include <linux/interrupt.h>
> > > > +#include <linux/io.h>
> > > > +#include <linux/uaccess.h>
> > > >  #include <soc/bcm2835/raspberrypi-firmware.h>
> > > >  
> > > >  #include "vchiq_core.h"
> > > >  #include "vchiq_ioctl.h"
> > > >  #include "vchiq_arm.h"
> > > >  #include "vchiq_debugfs.h"
> > > > +#include "vchiq_connected.h"
> > > > +#include "vchiq_pagelist.h"
> > > >  
> > > >  #define DEVICE_NAME "vchiq"
> > > >  
> > > > +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
> > > > +
> > > > +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
> > > > +
> > > > +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
> > > > +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX  1
> > > > +
> > > > +#define BELL0	0x00
> > > > +#define BELL2	0x08
> > > > +
> > > > +#define ARM_DS_ACTIVE	BIT(2)
> > > > +
> > > >  /* Override the default prefix, which would be vchiq_arm (from the filename) */
> > > >  #undef MODULE_PARAM_PREFIX
> > > >  #define MODULE_PARAM_PREFIX DEVICE_NAME "."
> > > > @@ -59,10 +76,542 @@ static struct vchiq_drvdata bcm2836_drvdata = {
> > > >  	.cache_line_size = 64,
> > > >  };
> > > >  
> > > > +struct vchiq_2835_state {
> > > > +	int inited;
> > > > +	struct vchiq_arm_state arm_state;
> > > > +};
> > > > +
> > > > +struct vchiq_pagelist_info {
> > > > +	struct pagelist *pagelist;
> > > > +	size_t pagelist_buffer_size;
> > > > +	dma_addr_t dma_addr;
> > > > +	enum dma_data_direction dma_dir;
> > > > +	unsigned int num_pages;
> > > > +	unsigned int pages_need_release;
> > > > +	struct page **pages;
> > > > +	struct scatterlist *scatterlist;
> > > > +	unsigned int scatterlist_mapped;
> > > > +};
> > > > +
> > > > +static void __iomem *g_regs;
> > > > +/* This value is the size of the L2 cache lines as understood by the
> > > > + * VPU firmware, which determines the required alignment of the
> > > > + * offsets/sizes in pagelists.
> > > > + *
> > > > + * Modern VPU firmware looks for a DT "cache-line-size" property in
> > > > + * the VCHIQ node and will overwrite it with the actual L2 cache size,
> > > > + * which the kernel must then respect.  That property was rejected
> > > > + * upstream, so we have to use the VPU firmware's compatibility value
> > > > + * of 32.
> > > > + */
> > > > +static unsigned int g_cache_line_size = 32;
> > > > +static unsigned int g_fragments_size;
> > > > +static char *g_fragments_base;
> > > > +static char *g_free_fragments;
> > > > +static struct semaphore g_free_fragments_sema;
> > > > +static struct device *g_dev;
> > > > +
> > > > +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
> > > > +
> > > > +static irqreturn_t
> > > > +vchiq_doorbell_irq(int irq, void *dev_id);
> > > > +
> > > > +static struct vchiq_pagelist_info *
> > > > +create_pagelist(char *buf, char __user *ubuf, size_t count, unsigned short type);
> > > > +
> > > > +static void
> > > > +free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
> > > > +	      int actual);
> > > 
> > > please no forward declarations of these 3 functions. Put them into the
> > > right order instead ...
> > > 
> > > Since this patch is independent from the other ones from the series,
> > > maybe Greg can merg the rest of the series.
> > > 
> > > 
> > > 
> > 
> > Hello Stefan,
> > 
> > Thanks for the review. As for the forward declerations, sure I can fix
> > these 3 functions and send an independent patch for this.
> 
> Please fix up and resend the whole series properly so that I can apply
> them that way.
> 
> thanks,
> 
> greg k-h
Hello,

I have just sent a revised patchset with requested changes. [1]

[1] https://lore.kernel.org/patchwork/cover/1465322/

Thank you,
Ojaswin
diff mbox series

Patch

diff --git a/drivers/staging/vc04_services/Makefile b/drivers/staging/vc04_services/Makefile
index 0a04338fc962..1fd191e2e2a5 100644
--- a/drivers/staging/vc04_services/Makefile
+++ b/drivers/staging/vc04_services/Makefile
@@ -4,7 +4,6 @@  obj-$(CONFIG_BCM2835_VCHIQ)	+= vchiq.o
 vchiq-objs := \
    interface/vchiq_arm/vchiq_core.o  \
    interface/vchiq_arm/vchiq_arm.o \
-   interface/vchiq_arm/vchiq_2835_arm.o \
    interface/vchiq_arm/vchiq_debugfs.o \
    interface/vchiq_arm/vchiq_connected.o \
 
diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
deleted file mode 100644
index 30d6f1a404ba..000000000000
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
+++ /dev/null
@@ -1,564 +0,0 @@ 
-// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
-/* Copyright (c) 2010-2012 Broadcom. All rights reserved. */
-
-#include <linux/kernel.h>
-#include <linux/types.h>
-#include <linux/errno.h>
-#include <linux/interrupt.h>
-#include <linux/pagemap.h>
-#include <linux/dma-mapping.h>
-#include <linux/io.h>
-#include <linux/platform_device.h>
-#include <linux/uaccess.h>
-#include <linux/mm.h>
-#include <linux/of.h>
-#include <linux/slab.h>
-#include <soc/bcm2835/raspberrypi-firmware.h>
-
-#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
-
-#include "vchiq_arm.h"
-#include "vchiq_connected.h"
-#include "vchiq_pagelist.h"
-
-#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
-
-#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
-#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX  1
-
-#define BELL0	0x00
-#define BELL2	0x08
-
-#define ARM_DS_ACTIVE	BIT(2)
-
-struct vchiq_2835_state {
-	int inited;
-	struct vchiq_arm_state arm_state;
-};
-
-struct vchiq_pagelist_info {
-	struct pagelist *pagelist;
-	size_t pagelist_buffer_size;
-	dma_addr_t dma_addr;
-	enum dma_data_direction dma_dir;
-	unsigned int num_pages;
-	unsigned int pages_need_release;
-	struct page **pages;
-	struct scatterlist *scatterlist;
-	unsigned int scatterlist_mapped;
-};
-
-static void __iomem *g_regs;
-/* This value is the size of the L2 cache lines as understood by the
- * VPU firmware, which determines the required alignment of the
- * offsets/sizes in pagelists.
- *
- * Modern VPU firmware looks for a DT "cache-line-size" property in
- * the VCHIQ node and will overwrite it with the actual L2 cache size,
- * which the kernel must then respect.  That property was rejected
- * upstream, so we have to use the VPU firmware's compatibility value
- * of 32.
- */
-static unsigned int g_cache_line_size = 32;
-static unsigned int g_fragments_size;
-static char *g_fragments_base;
-static char *g_free_fragments;
-static struct semaphore g_free_fragments_sema;
-static struct device *g_dev;
-
-static DEFINE_SEMAPHORE(g_free_fragments_mutex);
-
-static irqreturn_t
-vchiq_doorbell_irq(int irq, void *dev_id);
-
-static struct vchiq_pagelist_info *
-create_pagelist(char *buf, char __user *ubuf, size_t count, unsigned short type);
-
-static void
-free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
-	      int actual);
-
-int vchiq_platform_init(struct platform_device *pdev, struct vchiq_state *state)
-{
-	struct device *dev = &pdev->dev;
-	struct vchiq_drvdata *drvdata = platform_get_drvdata(pdev);
-	struct rpi_firmware *fw = drvdata->fw;
-	struct vchiq_slot_zero *vchiq_slot_zero;
-	void *slot_mem;
-	dma_addr_t slot_phys;
-	u32 channelbase;
-	int slot_mem_size, frag_mem_size;
-	int err, irq, i;
-
-	/*
-	 * VCHI messages between the CPU and firmware use
-	 * 32-bit bus addresses.
-	 */
-	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
-
-	if (err < 0)
-		return err;
-
-	g_cache_line_size = drvdata->cache_line_size;
-	g_fragments_size = 2 * g_cache_line_size;
-
-	/* Allocate space for the channels in coherent memory */
-	slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
-	frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
-
-	slot_mem = dmam_alloc_coherent(dev, slot_mem_size + frag_mem_size,
-				       &slot_phys, GFP_KERNEL);
-	if (!slot_mem) {
-		dev_err(dev, "could not allocate DMA memory\n");
-		return -ENOMEM;
-	}
-
-	WARN_ON(((unsigned long)slot_mem & (PAGE_SIZE - 1)) != 0);
-
-	vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
-	if (!vchiq_slot_zero)
-		return -EINVAL;
-
-	vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
-		(int)slot_phys + slot_mem_size;
-	vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
-		MAX_FRAGMENTS;
-
-	g_fragments_base = (char *)slot_mem + slot_mem_size;
-
-	g_free_fragments = g_fragments_base;
-	for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
-		*(char **)&g_fragments_base[i*g_fragments_size] =
-			&g_fragments_base[(i + 1)*g_fragments_size];
-	}
-	*(char **)&g_fragments_base[i * g_fragments_size] = NULL;
-	sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
-
-	err = vchiq_init_state(state, vchiq_slot_zero);
-	if (err)
-		return err;
-
-	g_regs = devm_platform_ioremap_resource(pdev, 0);
-	if (IS_ERR(g_regs))
-		return PTR_ERR(g_regs);
-
-	irq = platform_get_irq(pdev, 0);
-	if (irq <= 0)
-		return irq;
-
-	err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
-			       "VCHIQ doorbell", state);
-	if (err) {
-		dev_err(dev, "failed to register irq=%d\n", irq);
-		return err;
-	}
-
-	/* Send the base address of the slots to VideoCore */
-	channelbase = slot_phys;
-	err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
-				    &channelbase, sizeof(channelbase));
-	if (err || channelbase) {
-		dev_err(dev, "failed to set channelbase\n");
-		return err ? : -ENXIO;
-	}
-
-	g_dev = dev;
-	vchiq_log_info(vchiq_arm_log_level,
-		"vchiq_init - done (slots %pK, phys %pad)",
-		vchiq_slot_zero, &slot_phys);
-
-	vchiq_call_connected_callbacks();
-
-	return 0;
-}
-
-int
-vchiq_platform_init_state(struct vchiq_state *state)
-{
-	struct vchiq_2835_state *platform_state;
-
-	state->platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
-	if (!state->platform_state)
-		return -ENOMEM;
-
-	platform_state = (struct vchiq_2835_state *)state->platform_state;
-
-	platform_state->inited = 1;
-	vchiq_arm_init_state(state, &platform_state->arm_state);
-
-	return 0;
-}
-
-struct vchiq_arm_state*
-vchiq_platform_get_arm_state(struct vchiq_state *state)
-{
-	struct vchiq_2835_state *platform_state;
-
-	platform_state   = (struct vchiq_2835_state *)state->platform_state;
-
-	WARN_ON_ONCE(!platform_state->inited);
-
-	return &platform_state->arm_state;
-}
-
-void
-remote_event_signal(struct remote_event *event)
-{
-	wmb();
-
-	event->fired = 1;
-
-	dsb(sy);         /* data barrier operation */
-
-	if (event->armed)
-		writel(0, g_regs + BELL2); /* trigger vc interrupt */
-}
-
-int
-vchiq_prepare_bulk_data(struct vchiq_bulk *bulk, void *offset,
-			void __user *uoffset, int size, int dir)
-{
-	struct vchiq_pagelist_info *pagelistinfo;
-
-	pagelistinfo = create_pagelist(offset, uoffset, size,
-				       (dir == VCHIQ_BULK_RECEIVE)
-				       ? PAGELIST_READ
-				       : PAGELIST_WRITE);
-
-	if (!pagelistinfo)
-		return -ENOMEM;
-
-	bulk->data = pagelistinfo->dma_addr;
-
-	/*
-	 * Store the pagelistinfo address in remote_data,
-	 * which isn't used by the slave.
-	 */
-	bulk->remote_data = pagelistinfo;
-
-	return 0;
-}
-
-void
-vchiq_complete_bulk(struct vchiq_bulk *bulk)
-{
-	if (bulk && bulk->remote_data && bulk->actual)
-		free_pagelist((struct vchiq_pagelist_info *)bulk->remote_data,
-			      bulk->actual);
-}
-
-int vchiq_dump_platform_state(void *dump_context)
-{
-	char buf[80];
-	int len;
-
-	len = snprintf(buf, sizeof(buf),
-		"  Platform: 2835 (VC master)");
-	return vchiq_dump(dump_context, buf, len + 1);
-}
-
-/*
- * Local functions
- */
-
-static irqreturn_t
-vchiq_doorbell_irq(int irq, void *dev_id)
-{
-	struct vchiq_state *state = dev_id;
-	irqreturn_t ret = IRQ_NONE;
-	unsigned int status;
-
-	/* Read (and clear) the doorbell */
-	status = readl(g_regs + BELL0);
-
-	if (status & ARM_DS_ACTIVE) {  /* Was the doorbell rung? */
-		remote_event_pollall(state);
-		ret = IRQ_HANDLED;
-	}
-
-	return ret;
-}
-
-static void
-cleanup_pagelistinfo(struct vchiq_pagelist_info *pagelistinfo)
-{
-	if (pagelistinfo->scatterlist_mapped) {
-		dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
-			     pagelistinfo->num_pages, pagelistinfo->dma_dir);
-	}
-
-	if (pagelistinfo->pages_need_release)
-		unpin_user_pages(pagelistinfo->pages, pagelistinfo->num_pages);
-
-	dma_free_coherent(g_dev, pagelistinfo->pagelist_buffer_size,
-			  pagelistinfo->pagelist, pagelistinfo->dma_addr);
-}
-
-/* There is a potential problem with partial cache lines (pages?)
- * at the ends of the block when reading. If the CPU accessed anything in
- * the same line (page?) then it may have pulled old data into the cache,
- * obscuring the new data underneath. We can solve this by transferring the
- * partial cache lines separately, and allowing the ARM to copy into the
- * cached area.
- */
-
-static struct vchiq_pagelist_info *
-create_pagelist(char *buf, char __user *ubuf,
-		size_t count, unsigned short type)
-{
-	struct pagelist *pagelist;
-	struct vchiq_pagelist_info *pagelistinfo;
-	struct page **pages;
-	u32 *addrs;
-	unsigned int num_pages, offset, i, k;
-	int actual_pages;
-	size_t pagelist_size;
-	struct scatterlist *scatterlist, *sg;
-	int dma_buffers;
-	dma_addr_t dma_addr;
-
-	if (count >= INT_MAX - PAGE_SIZE)
-		return NULL;
-
-	if (buf)
-		offset = (uintptr_t)buf & (PAGE_SIZE - 1);
-	else
-		offset = (uintptr_t)ubuf & (PAGE_SIZE - 1);
-	num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
-
-	if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
-			 sizeof(struct vchiq_pagelist_info)) /
-			(sizeof(u32) + sizeof(pages[0]) +
-			 sizeof(struct scatterlist)))
-		return NULL;
-
-	pagelist_size = sizeof(struct pagelist) +
-			(num_pages * sizeof(u32)) +
-			(num_pages * sizeof(pages[0]) +
-			(num_pages * sizeof(struct scatterlist))) +
-			sizeof(struct vchiq_pagelist_info);
-
-	/* Allocate enough storage to hold the page pointers and the page
-	 * list
-	 */
-	pagelist = dma_alloc_coherent(g_dev, pagelist_size, &dma_addr,
-				      GFP_KERNEL);
-
-	vchiq_log_trace(vchiq_arm_log_level, "%s - %pK", __func__, pagelist);
-
-	if (!pagelist)
-		return NULL;
-
-	addrs		= pagelist->addrs;
-	pages		= (struct page **)(addrs + num_pages);
-	scatterlist	= (struct scatterlist *)(pages + num_pages);
-	pagelistinfo	= (struct vchiq_pagelist_info *)
-			  (scatterlist + num_pages);
-
-	pagelist->length = count;
-	pagelist->type = type;
-	pagelist->offset = offset;
-
-	/* Populate the fields of the pagelistinfo structure */
-	pagelistinfo->pagelist = pagelist;
-	pagelistinfo->pagelist_buffer_size = pagelist_size;
-	pagelistinfo->dma_addr = dma_addr;
-	pagelistinfo->dma_dir =  (type == PAGELIST_WRITE) ?
-				  DMA_TO_DEVICE : DMA_FROM_DEVICE;
-	pagelistinfo->num_pages = num_pages;
-	pagelistinfo->pages_need_release = 0;
-	pagelistinfo->pages = pages;
-	pagelistinfo->scatterlist = scatterlist;
-	pagelistinfo->scatterlist_mapped = 0;
-
-	if (buf) {
-		unsigned long length = count;
-		unsigned int off = offset;
-
-		for (actual_pages = 0; actual_pages < num_pages;
-		     actual_pages++) {
-			struct page *pg =
-				vmalloc_to_page((buf +
-						 (actual_pages * PAGE_SIZE)));
-			size_t bytes = PAGE_SIZE - off;
-
-			if (!pg) {
-				cleanup_pagelistinfo(pagelistinfo);
-				return NULL;
-			}
-
-			if (bytes > length)
-				bytes = length;
-			pages[actual_pages] = pg;
-			length -= bytes;
-			off = 0;
-		}
-		/* do not try and release vmalloc pages */
-	} else {
-		actual_pages = pin_user_pages_fast(
-					  (unsigned long)ubuf & PAGE_MASK,
-					  num_pages,
-					  type == PAGELIST_READ,
-					  pages);
-
-		if (actual_pages != num_pages) {
-			vchiq_log_info(vchiq_arm_log_level,
-				       "%s - only %d/%d pages locked",
-				       __func__, actual_pages, num_pages);
-
-			/* This is probably due to the process being killed */
-			if (actual_pages > 0)
-				unpin_user_pages(pages, actual_pages);
-			cleanup_pagelistinfo(pagelistinfo);
-			return NULL;
-		}
-		 /* release user pages */
-		pagelistinfo->pages_need_release = 1;
-	}
-
-	/*
-	 * Initialize the scatterlist so that the magic cookie
-	 *  is filled if debugging is enabled
-	 */
-	sg_init_table(scatterlist, num_pages);
-	/* Now set the pages for each scatterlist */
-	for (i = 0; i < num_pages; i++)	{
-		unsigned int len = PAGE_SIZE - offset;
-
-		if (len > count)
-			len = count;
-		sg_set_page(scatterlist + i, pages[i], len, offset);
-		offset = 0;
-		count -= len;
-	}
-
-	dma_buffers = dma_map_sg(g_dev,
-				 scatterlist,
-				 num_pages,
-				 pagelistinfo->dma_dir);
-
-	if (dma_buffers == 0) {
-		cleanup_pagelistinfo(pagelistinfo);
-		return NULL;
-	}
-
-	pagelistinfo->scatterlist_mapped = 1;
-
-	/* Combine adjacent blocks for performance */
-	k = 0;
-	for_each_sg(scatterlist, sg, dma_buffers, i) {
-		u32 len = sg_dma_len(sg);
-		u32 addr = sg_dma_address(sg);
-
-		/* Note: addrs is the address + page_count - 1
-		 * The firmware expects blocks after the first to be page-
-		 * aligned and a multiple of the page size
-		 */
-		WARN_ON(len == 0);
-		WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
-		WARN_ON(i && (addr & ~PAGE_MASK));
-		if (k > 0 &&
-		    ((addrs[k - 1] & PAGE_MASK) +
-		     (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
-		    == (addr & PAGE_MASK))
-			addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
-		else
-			addrs[k++] = (addr & PAGE_MASK) |
-				(((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
-	}
-
-	/* Partial cache lines (fragments) require special measures */
-	if ((type == PAGELIST_READ) &&
-		((pagelist->offset & (g_cache_line_size - 1)) ||
-		((pagelist->offset + pagelist->length) &
-		(g_cache_line_size - 1)))) {
-		char *fragments;
-
-		if (down_interruptible(&g_free_fragments_sema)) {
-			cleanup_pagelistinfo(pagelistinfo);
-			return NULL;
-		}
-
-		WARN_ON(!g_free_fragments);
-
-		down(&g_free_fragments_mutex);
-		fragments = g_free_fragments;
-		WARN_ON(!fragments);
-		g_free_fragments = *(char **) g_free_fragments;
-		up(&g_free_fragments_mutex);
-		pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
-			(fragments - g_fragments_base) / g_fragments_size;
-	}
-
-	return pagelistinfo;
-}
-
-static void
-free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
-	      int actual)
-{
-	struct pagelist *pagelist = pagelistinfo->pagelist;
-	struct page **pages = pagelistinfo->pages;
-	unsigned int num_pages = pagelistinfo->num_pages;
-
-	vchiq_log_trace(vchiq_arm_log_level, "%s - %pK, %d",
-			__func__, pagelistinfo->pagelist, actual);
-
-	/*
-	 * NOTE: dma_unmap_sg must be called before the
-	 * cpu can touch any of the data/pages.
-	 */
-	dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
-		     pagelistinfo->num_pages, pagelistinfo->dma_dir);
-	pagelistinfo->scatterlist_mapped = 0;
-
-	/* Deal with any partial cache lines (fragments) */
-	if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
-		char *fragments = g_fragments_base +
-			(pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
-			g_fragments_size;
-		int head_bytes, tail_bytes;
-
-		head_bytes = (g_cache_line_size - pagelist->offset) &
-			(g_cache_line_size - 1);
-		tail_bytes = (pagelist->offset + actual) &
-			(g_cache_line_size - 1);
-
-		if ((actual >= 0) && (head_bytes != 0)) {
-			if (head_bytes > actual)
-				head_bytes = actual;
-
-			memcpy((char *)kmap(pages[0]) +
-				pagelist->offset,
-				fragments,
-				head_bytes);
-			kunmap(pages[0]);
-		}
-		if ((actual >= 0) && (head_bytes < actual) &&
-			(tail_bytes != 0)) {
-			memcpy((char *)kmap(pages[num_pages - 1]) +
-				((pagelist->offset + actual) &
-				(PAGE_SIZE - 1) & ~(g_cache_line_size - 1)),
-				fragments + g_cache_line_size,
-				tail_bytes);
-			kunmap(pages[num_pages - 1]);
-		}
-
-		down(&g_free_fragments_mutex);
-		*(char **)fragments = g_free_fragments;
-		g_free_fragments = fragments;
-		up(&g_free_fragments_mutex);
-		up(&g_free_fragments_sema);
-	}
-
-	/* Need to mark all the pages dirty. */
-	if (pagelist->type != PAGELIST_WRITE &&
-	    pagelistinfo->pages_need_release) {
-		unsigned int i;
-
-		for (i = 0; i < num_pages; i++)
-			set_page_dirty(pages[i]);
-	}
-
-	cleanup_pagelistinfo(pagelistinfo);
-}
diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
index 0f2de571eba7..9057d01ffd48 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
@@ -25,15 +25,32 @@ 
 #include <linux/rcupdate.h>
 #include <linux/delay.h>
 #include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/uaccess.h>
 #include <soc/bcm2835/raspberrypi-firmware.h>
 
 #include "vchiq_core.h"
 #include "vchiq_ioctl.h"
 #include "vchiq_arm.h"
 #include "vchiq_debugfs.h"
+#include "vchiq_connected.h"
+#include "vchiq_pagelist.h"
 
 #define DEVICE_NAME "vchiq"
 
+#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
+
+#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
+
+#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
+#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX  1
+
+#define BELL0	0x00
+#define BELL2	0x08
+
+#define ARM_DS_ACTIVE	BIT(2)
+
 /* Override the default prefix, which would be vchiq_arm (from the filename) */
 #undef MODULE_PARAM_PREFIX
 #define MODULE_PARAM_PREFIX DEVICE_NAME "."
@@ -59,10 +76,542 @@  static struct vchiq_drvdata bcm2836_drvdata = {
 	.cache_line_size = 64,
 };
 
+struct vchiq_2835_state {
+	int inited;
+	struct vchiq_arm_state arm_state;
+};
+
+struct vchiq_pagelist_info {
+	struct pagelist *pagelist;
+	size_t pagelist_buffer_size;
+	dma_addr_t dma_addr;
+	enum dma_data_direction dma_dir;
+	unsigned int num_pages;
+	unsigned int pages_need_release;
+	struct page **pages;
+	struct scatterlist *scatterlist;
+	unsigned int scatterlist_mapped;
+};
+
+static void __iomem *g_regs;
+/* This value is the size of the L2 cache lines as understood by the
+ * VPU firmware, which determines the required alignment of the
+ * offsets/sizes in pagelists.
+ *
+ * Modern VPU firmware looks for a DT "cache-line-size" property in
+ * the VCHIQ node and will overwrite it with the actual L2 cache size,
+ * which the kernel must then respect.  That property was rejected
+ * upstream, so we have to use the VPU firmware's compatibility value
+ * of 32.
+ */
+static unsigned int g_cache_line_size = 32;
+static unsigned int g_fragments_size;
+static char *g_fragments_base;
+static char *g_free_fragments;
+static struct semaphore g_free_fragments_sema;
+static struct device *g_dev;
+
+static DEFINE_SEMAPHORE(g_free_fragments_mutex);
+
+static irqreturn_t
+vchiq_doorbell_irq(int irq, void *dev_id);
+
+static struct vchiq_pagelist_info *
+create_pagelist(char *buf, char __user *ubuf, size_t count, unsigned short type);
+
+static void
+free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
+	      int actual);
+
 static enum vchiq_status
 vchiq_blocking_bulk_transfer(unsigned int handle, void *data,
 	unsigned int size, enum vchiq_bulk_dir dir);
 
+int vchiq_platform_init(struct platform_device *pdev, struct vchiq_state *state)
+{
+	struct device *dev = &pdev->dev;
+	struct vchiq_drvdata *drvdata = platform_get_drvdata(pdev);
+	struct rpi_firmware *fw = drvdata->fw;
+	struct vchiq_slot_zero *vchiq_slot_zero;
+	void *slot_mem;
+	dma_addr_t slot_phys;
+	u32 channelbase;
+	int slot_mem_size, frag_mem_size;
+	int err, irq, i;
+
+	/*
+	 * VCHI messages between the CPU and firmware use
+	 * 32-bit bus addresses.
+	 */
+	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
+
+	if (err < 0)
+		return err;
+
+	g_cache_line_size = drvdata->cache_line_size;
+	g_fragments_size = 2 * g_cache_line_size;
+
+	/* Allocate space for the channels in coherent memory */
+	slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
+	frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
+
+	slot_mem = dmam_alloc_coherent(dev, slot_mem_size + frag_mem_size,
+				       &slot_phys, GFP_KERNEL);
+	if (!slot_mem) {
+		dev_err(dev, "could not allocate DMA memory\n");
+		return -ENOMEM;
+	}
+
+	WARN_ON(((unsigned long)slot_mem & (PAGE_SIZE - 1)) != 0);
+
+	vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
+	if (!vchiq_slot_zero)
+		return -EINVAL;
+
+	vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
+		(int)slot_phys + slot_mem_size;
+	vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
+		MAX_FRAGMENTS;
+
+	g_fragments_base = (char *)slot_mem + slot_mem_size;
+
+	g_free_fragments = g_fragments_base;
+	for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
+		*(char **)&g_fragments_base[i*g_fragments_size] =
+			&g_fragments_base[(i + 1)*g_fragments_size];
+	}
+	*(char **)&g_fragments_base[i * g_fragments_size] = NULL;
+	sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
+
+	err = vchiq_init_state(state, vchiq_slot_zero);
+	if (err)
+		return err;
+
+	g_regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(g_regs))
+		return PTR_ERR(g_regs);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq <= 0)
+		return irq;
+
+	err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
+			       "VCHIQ doorbell", state);
+	if (err) {
+		dev_err(dev, "failed to register irq=%d\n", irq);
+		return err;
+	}
+
+	/* Send the base address of the slots to VideoCore */
+	channelbase = slot_phys;
+	err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
+				    &channelbase, sizeof(channelbase));
+	if (err || channelbase) {
+		dev_err(dev, "failed to set channelbase\n");
+		return err ? : -ENXIO;
+	}
+
+	g_dev = dev;
+	vchiq_log_info(vchiq_arm_log_level,
+		"vchiq_init - done (slots %pK, phys %pad)",
+		vchiq_slot_zero, &slot_phys);
+
+	vchiq_call_connected_callbacks();
+
+	return 0;
+}
+
+int
+vchiq_platform_init_state(struct vchiq_state *state)
+{
+	struct vchiq_2835_state *platform_state;
+
+	state->platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
+	if (!state->platform_state)
+		return -ENOMEM;
+
+	platform_state = (struct vchiq_2835_state *)state->platform_state;
+
+	platform_state->inited = 1;
+	vchiq_arm_init_state(state, &platform_state->arm_state);
+
+	return 0;
+}
+
+struct vchiq_arm_state*
+vchiq_platform_get_arm_state(struct vchiq_state *state)
+{
+	struct vchiq_2835_state *platform_state;
+
+	platform_state   = (struct vchiq_2835_state *)state->platform_state;
+
+	WARN_ON_ONCE(!platform_state->inited);
+
+	return &platform_state->arm_state;
+}
+
+void
+remote_event_signal(struct remote_event *event)
+{
+	wmb();
+
+	event->fired = 1;
+
+	dsb(sy);         /* data barrier operation */
+
+	if (event->armed)
+		writel(0, g_regs + BELL2); /* trigger vc interrupt */
+}
+
+int
+vchiq_prepare_bulk_data(struct vchiq_bulk *bulk, void *offset,
+			void __user *uoffset, int size, int dir)
+{
+	struct vchiq_pagelist_info *pagelistinfo;
+
+	pagelistinfo = create_pagelist(offset, uoffset, size,
+				       (dir == VCHIQ_BULK_RECEIVE)
+				       ? PAGELIST_READ
+				       : PAGELIST_WRITE);
+
+	if (!pagelistinfo)
+		return -ENOMEM;
+
+	bulk->data = pagelistinfo->dma_addr;
+
+	/*
+	 * Store the pagelistinfo address in remote_data,
+	 * which isn't used by the slave.
+	 */
+	bulk->remote_data = pagelistinfo;
+
+	return 0;
+}
+
+void
+vchiq_complete_bulk(struct vchiq_bulk *bulk)
+{
+	if (bulk && bulk->remote_data && bulk->actual)
+		free_pagelist((struct vchiq_pagelist_info *)bulk->remote_data,
+			      bulk->actual);
+}
+
+int vchiq_dump_platform_state(void *dump_context)
+{
+	char buf[80];
+	int len;
+
+	len = snprintf(buf, sizeof(buf),
+		"  Platform: 2835 (VC master)");
+	return vchiq_dump(dump_context, buf, len + 1);
+}
+
+/*
+ * Local functions
+ */
+
+static irqreturn_t
+vchiq_doorbell_irq(int irq, void *dev_id)
+{
+	struct vchiq_state *state = dev_id;
+	irqreturn_t ret = IRQ_NONE;
+	unsigned int status;
+
+	/* Read (and clear) the doorbell */
+	status = readl(g_regs + BELL0);
+
+	if (status & ARM_DS_ACTIVE) {  /* Was the doorbell rung? */
+		remote_event_pollall(state);
+		ret = IRQ_HANDLED;
+	}
+
+	return ret;
+}
+
+static void
+cleanup_pagelistinfo(struct vchiq_pagelist_info *pagelistinfo)
+{
+	if (pagelistinfo->scatterlist_mapped) {
+		dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
+			     pagelistinfo->num_pages, pagelistinfo->dma_dir);
+	}
+
+	if (pagelistinfo->pages_need_release)
+		unpin_user_pages(pagelistinfo->pages, pagelistinfo->num_pages);
+
+	dma_free_coherent(g_dev, pagelistinfo->pagelist_buffer_size,
+			  pagelistinfo->pagelist, pagelistinfo->dma_addr);
+}
+
+/* There is a potential problem with partial cache lines (pages?)
+ * at the ends of the block when reading. If the CPU accessed anything in
+ * the same line (page?) then it may have pulled old data into the cache,
+ * obscuring the new data underneath. We can solve this by transferring the
+ * partial cache lines separately, and allowing the ARM to copy into the
+ * cached area.
+ */
+
+static struct vchiq_pagelist_info *
+create_pagelist(char *buf, char __user *ubuf,
+		size_t count, unsigned short type)
+{
+	struct pagelist *pagelist;
+	struct vchiq_pagelist_info *pagelistinfo;
+	struct page **pages;
+	u32 *addrs;
+	unsigned int num_pages, offset, i, k;
+	int actual_pages;
+	size_t pagelist_size;
+	struct scatterlist *scatterlist, *sg;
+	int dma_buffers;
+	dma_addr_t dma_addr;
+
+	if (count >= INT_MAX - PAGE_SIZE)
+		return NULL;
+
+	if (buf)
+		offset = (uintptr_t)buf & (PAGE_SIZE - 1);
+	else
+		offset = (uintptr_t)ubuf & (PAGE_SIZE - 1);
+	num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
+
+	if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
+			 sizeof(struct vchiq_pagelist_info)) /
+			(sizeof(u32) + sizeof(pages[0]) +
+			 sizeof(struct scatterlist)))
+		return NULL;
+
+	pagelist_size = sizeof(struct pagelist) +
+			(num_pages * sizeof(u32)) +
+			(num_pages * sizeof(pages[0]) +
+			(num_pages * sizeof(struct scatterlist))) +
+			sizeof(struct vchiq_pagelist_info);
+
+	/* Allocate enough storage to hold the page pointers and the page
+	 * list
+	 */
+	pagelist = dma_alloc_coherent(g_dev, pagelist_size, &dma_addr,
+				      GFP_KERNEL);
+
+	vchiq_log_trace(vchiq_arm_log_level, "%s - %pK", __func__, pagelist);
+
+	if (!pagelist)
+		return NULL;
+
+	addrs		= pagelist->addrs;
+	pages		= (struct page **)(addrs + num_pages);
+	scatterlist	= (struct scatterlist *)(pages + num_pages);
+	pagelistinfo	= (struct vchiq_pagelist_info *)
+			  (scatterlist + num_pages);
+
+	pagelist->length = count;
+	pagelist->type = type;
+	pagelist->offset = offset;
+
+	/* Populate the fields of the pagelistinfo structure */
+	pagelistinfo->pagelist = pagelist;
+	pagelistinfo->pagelist_buffer_size = pagelist_size;
+	pagelistinfo->dma_addr = dma_addr;
+	pagelistinfo->dma_dir =  (type == PAGELIST_WRITE) ?
+				  DMA_TO_DEVICE : DMA_FROM_DEVICE;
+	pagelistinfo->num_pages = num_pages;
+	pagelistinfo->pages_need_release = 0;
+	pagelistinfo->pages = pages;
+	pagelistinfo->scatterlist = scatterlist;
+	pagelistinfo->scatterlist_mapped = 0;
+
+	if (buf) {
+		unsigned long length = count;
+		unsigned int off = offset;
+
+		for (actual_pages = 0; actual_pages < num_pages;
+		     actual_pages++) {
+			struct page *pg =
+				vmalloc_to_page((buf +
+						 (actual_pages * PAGE_SIZE)));
+			size_t bytes = PAGE_SIZE - off;
+
+			if (!pg) {
+				cleanup_pagelistinfo(pagelistinfo);
+				return NULL;
+			}
+
+			if (bytes > length)
+				bytes = length;
+			pages[actual_pages] = pg;
+			length -= bytes;
+			off = 0;
+		}
+		/* do not try and release vmalloc pages */
+	} else {
+		actual_pages = pin_user_pages_fast(
+					  (unsigned long)ubuf & PAGE_MASK,
+					  num_pages,
+					  type == PAGELIST_READ,
+					  pages);
+
+		if (actual_pages != num_pages) {
+			vchiq_log_info(vchiq_arm_log_level,
+				       "%s - only %d/%d pages locked",
+				       __func__, actual_pages, num_pages);
+
+			/* This is probably due to the process being killed */
+			if (actual_pages > 0)
+				unpin_user_pages(pages, actual_pages);
+			cleanup_pagelistinfo(pagelistinfo);
+			return NULL;
+		}
+		 /* release user pages */
+		pagelistinfo->pages_need_release = 1;
+	}
+
+	/*
+	 * Initialize the scatterlist so that the magic cookie
+	 *  is filled if debugging is enabled
+	 */
+	sg_init_table(scatterlist, num_pages);
+	/* Now set the pages for each scatterlist */
+	for (i = 0; i < num_pages; i++)	{
+		unsigned int len = PAGE_SIZE - offset;
+
+		if (len > count)
+			len = count;
+		sg_set_page(scatterlist + i, pages[i], len, offset);
+		offset = 0;
+		count -= len;
+	}
+
+	dma_buffers = dma_map_sg(g_dev,
+				 scatterlist,
+				 num_pages,
+				 pagelistinfo->dma_dir);
+
+	if (dma_buffers == 0) {
+		cleanup_pagelistinfo(pagelistinfo);
+		return NULL;
+	}
+
+	pagelistinfo->scatterlist_mapped = 1;
+
+	/* Combine adjacent blocks for performance */
+	k = 0;
+	for_each_sg(scatterlist, sg, dma_buffers, i) {
+		u32 len = sg_dma_len(sg);
+		u32 addr = sg_dma_address(sg);
+
+		/* Note: addrs is the address + page_count - 1
+		 * The firmware expects blocks after the first to be page-
+		 * aligned and a multiple of the page size
+		 */
+		WARN_ON(len == 0);
+		WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
+		WARN_ON(i && (addr & ~PAGE_MASK));
+		if (k > 0 &&
+		    ((addrs[k - 1] & PAGE_MASK) +
+		     (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
+		    == (addr & PAGE_MASK))
+			addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
+		else
+			addrs[k++] = (addr & PAGE_MASK) |
+				(((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
+	}
+
+	/* Partial cache lines (fragments) require special measures */
+	if ((type == PAGELIST_READ) &&
+		((pagelist->offset & (g_cache_line_size - 1)) ||
+		((pagelist->offset + pagelist->length) &
+		(g_cache_line_size - 1)))) {
+		char *fragments;
+
+		if (down_interruptible(&g_free_fragments_sema)) {
+			cleanup_pagelistinfo(pagelistinfo);
+			return NULL;
+		}
+
+		WARN_ON(!g_free_fragments);
+
+		down(&g_free_fragments_mutex);
+		fragments = g_free_fragments;
+		WARN_ON(!fragments);
+		g_free_fragments = *(char **) g_free_fragments;
+		up(&g_free_fragments_mutex);
+		pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
+			(fragments - g_fragments_base) / g_fragments_size;
+	}
+
+	return pagelistinfo;
+}
+
+static void
+free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
+	      int actual)
+{
+	struct pagelist *pagelist = pagelistinfo->pagelist;
+	struct page **pages = pagelistinfo->pages;
+	unsigned int num_pages = pagelistinfo->num_pages;
+
+	vchiq_log_trace(vchiq_arm_log_level, "%s - %pK, %d",
+			__func__, pagelistinfo->pagelist, actual);
+
+	/*
+	 * NOTE: dma_unmap_sg must be called before the
+	 * cpu can touch any of the data/pages.
+	 */
+	dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
+		     pagelistinfo->num_pages, pagelistinfo->dma_dir);
+	pagelistinfo->scatterlist_mapped = 0;
+
+	/* Deal with any partial cache lines (fragments) */
+	if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
+		char *fragments = g_fragments_base +
+			(pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
+			g_fragments_size;
+		int head_bytes, tail_bytes;
+
+		head_bytes = (g_cache_line_size - pagelist->offset) &
+			(g_cache_line_size - 1);
+		tail_bytes = (pagelist->offset + actual) &
+			(g_cache_line_size - 1);
+
+		if ((actual >= 0) && (head_bytes != 0)) {
+			if (head_bytes > actual)
+				head_bytes = actual;
+
+			memcpy((char *)kmap(pages[0]) +
+				pagelist->offset,
+				fragments,
+				head_bytes);
+			kunmap(pages[0]);
+		}
+		if ((actual >= 0) && (head_bytes < actual) &&
+			(tail_bytes != 0)) {
+			memcpy((char *)kmap(pages[num_pages - 1]) +
+				((pagelist->offset + actual) &
+				(PAGE_SIZE - 1) & ~(g_cache_line_size - 1)),
+				fragments + g_cache_line_size,
+				tail_bytes);
+			kunmap(pages[num_pages - 1]);
+		}
+
+		down(&g_free_fragments_mutex);
+		*(char **)fragments = g_free_fragments;
+		g_free_fragments = fragments;
+		up(&g_free_fragments_mutex);
+		up(&g_free_fragments_sema);
+	}
+
+	/* Need to mark all the pages dirty. */
+	if (pagelist->type != PAGELIST_WRITE &&
+	    pagelistinfo->pages_need_release) {
+		unsigned int i;
+
+		for (i = 0; i < num_pages; i++)
+			set_page_dirty(pages[i]);
+	}
+
+	cleanup_pagelistinfo(pagelistinfo);
+}
+
 #define VCHIQ_INIT_RETRIES 10
 int vchiq_initialise(struct vchiq_instance **instance_out)
 {