Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (>4GB).

2012-08-15 Thread Huacai Chen
On Thu, Aug 16, 2012 at 4:24 AM, Ralf Baechle  wrote:
> On Mon, Aug 13, 2012 at 01:54:47PM -0400, Konrad Rzeszutek Wilk wrote:
>
>> > +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
>> > +   dma_addr_t *dma_handle, gfp_t gfp, struct 
>> > dma_attrs *attrs)
>> > +{
>> > +   void *ret;
>> > +
>> > +   if (dma_alloc_from_coherent(dev, size, dma_handle, ))
>> > +   return ret;
>> > +
>> > +   /* ignore region specifiers */
>> > +   gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
>> > +
>> > +#ifdef CONFIG_ZONE_DMA
>> > +   if (dev == NULL)
>> > +   gfp |= __GFP_DMA;
>>
>> When would this happen? dev == NULL?
>
> A legacy (ISA) device driver.  Some of the Loongsons have some kind of
> southbridge which incorporates legacy devices though of the top of my
> head I'm not sure which if any of these are actually being used.  Huacai?
ISA driver isn't used now, but I think keep "dev == NULL" here has no
side effect.
BTW, "dev == NULL" only happend in ISA case?  I use "grep
pci_alloc_consistent drivers/ -rwI | grep NULL" and also get some
lines.

>
>> > +   else if (dev->coherent_dma_mask <= DMA_BIT_MASK(24))
>> > +   gfp |= __GFP_DMA;
>> > +   else
>> > +#endif
>> > +#ifdef CONFIG_ZONE_DMA32
>> > +   if (dev->coherent_dma_mask <= DMA_BIT_MASK(32))
>> > +   gfp |= __GFP_DMA32;
>> > +   else
>>
>> Why the 'else'
>> > +#endif
>> > +   ;
>>
>> why?
>> > +   gfp |= __GFP_NORETRY;
>> > +
>> > +   ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
>> > +   mb();
>>
>> Why the 'mb()' ? Can you just do
>>   return swiotlb_alloc_coherent(...)
>>
>> > +   return ret;
>> > +}
>> > +
>> > +static void loongson_dma_free_coherent(struct device *dev, size_t size,
>> > +   void *vaddr, dma_addr_t dma_handle, struct 
>> > dma_attrs *attrs)
>> > +{
>> > +   int order = get_order(size);
>> > +
>> > +   if (dma_release_from_coherent(dev, order, vaddr))
>> > +   return;
>> > +
>> > +   swiotlb_free_coherent(dev, size, vaddr, dma_handle);
>> > +}
>> > +
>> > +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
>> > *page,
>> > +   unsigned long offset, size_t size,
>> > +   enum dma_data_direction dir,
>> > +   struct dma_attrs *attrs)
>> > +{
>> > +   dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
>> > +   dir, attrs);
>> > +   mb();
>>
>> Please do 'return swiotlb_map_page(..)'..
>>
>> But if you are doing that why don't you just set the dma_ops.map_page = 
>> swiotlb_map_page
>> ?
>>
>>
>> > +   return daddr;
>> > +}
>> > +
>> > +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
>> > +   int nents, enum dma_data_direction dir,
>> > +   struct dma_attrs *attrs)
>> > +{
>> > +   int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
>> > +   mb();
>> > +
>> > +   return r;
>> > +}
>> > +
>> > +static void loongson_dma_sync_single_for_device(struct device *dev,
>> > +   dma_addr_t dma_handle, size_t size,
>> > +   enum dma_data_direction dir)
>> > +{
>> > +   swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
>> > +   mb();
>> > +}
>> > +
>> > +static void loongson_dma_sync_sg_for_device(struct device *dev,
>> > +   struct scatterlist *sg, int nents,
>> > +   enum dma_data_direction dir)
>> > +{
>> > +   swiotlb_sync_sg_for_device(dev, sg, nents, dir);
>> > +   mb();
>> > +}
>> > +
>>
>> I am not really sure why you have these extra functions, when you could
>> just modify the dma_ops to point to the swiotlb ones
>>
>> > +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, 
>> > phys_addr_t paddr)
>> > +{
>> > +   return (paddr < 0x1000) ?
>> > +   (paddr | 0x8000) : paddr;
>> > +}
>> > +
>> > +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, 
>> > dma_addr_t daddr)
>> > +{
>> > +   return (daddr < 0x9000 && daddr >= 0x8000) ?
>> > +   (daddr & 0x0fff) : daddr;
>> > +}
>> > +
>> > +struct loongson_dma_map_ops {
>> > +   struct dma_map_ops dma_map_ops;
>> > +   dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
>> > +   phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
>> > +};
>> > +
>> > +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
>> > +{
>> > +   struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
>> > +   struct loongson_dma_map_ops, 
>> > dma_map_ops);
>> > +
>> > +   return ops->phys_to_dma(dev, paddr);
>> > +}
>> > +
>> > +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
>> > +{
>> > +   struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
>> > +   struct 

Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (>4GB).

2012-08-15 Thread Ralf Baechle
On Mon, Aug 13, 2012 at 01:54:47PM -0400, Konrad Rzeszutek Wilk wrote:

> > +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
> > +   dma_addr_t *dma_handle, gfp_t gfp, struct 
> > dma_attrs *attrs)
> > +{
> > +   void *ret;
> > +
> > +   if (dma_alloc_from_coherent(dev, size, dma_handle, ))
> > +   return ret;
> > +
> > +   /* ignore region specifiers */
> > +   gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
> > +
> > +#ifdef CONFIG_ZONE_DMA
> > +   if (dev == NULL)
> > +   gfp |= __GFP_DMA;
> 
> When would this happen? dev == NULL?

A legacy (ISA) device driver.  Some of the Loongsons have some kind of
southbridge which incorporates legacy devices though of the top of my
head I'm not sure which if any of these are actually being used.  Huacai?

> > +   else if (dev->coherent_dma_mask <= DMA_BIT_MASK(24))
> > +   gfp |= __GFP_DMA;
> > +   else
> > +#endif
> > +#ifdef CONFIG_ZONE_DMA32
> > +   if (dev->coherent_dma_mask <= DMA_BIT_MASK(32))
> > +   gfp |= __GFP_DMA32;
> > +   else
> 
> Why the 'else'
> > +#endif
> > +   ;
> 
> why?
> > +   gfp |= __GFP_NORETRY;
> > +
> > +   ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
> > +   mb();
> 
> Why the 'mb()' ? Can you just do
>   return swiotlb_alloc_coherent(...) 
> 
> > +   return ret;
> > +}
> > +
> > +static void loongson_dma_free_coherent(struct device *dev, size_t size,
> > +   void *vaddr, dma_addr_t dma_handle, struct 
> > dma_attrs *attrs)
> > +{
> > +   int order = get_order(size);
> > +
> > +   if (dma_release_from_coherent(dev, order, vaddr))
> > +   return;
> > +
> > +   swiotlb_free_coherent(dev, size, vaddr, dma_handle);
> > +}
> > +
> > +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
> > *page,
> > +   unsigned long offset, size_t size,
> > +   enum dma_data_direction dir,
> > +   struct dma_attrs *attrs)
> > +{
> > +   dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
> > +   dir, attrs);
> > +   mb();
> 
> Please do 'return swiotlb_map_page(..)'..
> 
> But if you are doing that why don't you just set the dma_ops.map_page = 
> swiotlb_map_page
> ?
> 
> 
> > +   return daddr;
> > +}
> > +
> > +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
> > +   int nents, enum dma_data_direction dir,
> > +   struct dma_attrs *attrs)
> > +{
> > +   int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
> > +   mb();
> > +
> > +   return r;
> > +}
> > +
> > +static void loongson_dma_sync_single_for_device(struct device *dev,
> > +   dma_addr_t dma_handle, size_t size,
> > +   enum dma_data_direction dir)
> > +{
> > +   swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
> > +   mb();
> > +}
> > +
> > +static void loongson_dma_sync_sg_for_device(struct device *dev,
> > +   struct scatterlist *sg, int nents,
> > +   enum dma_data_direction dir)
> > +{
> > +   swiotlb_sync_sg_for_device(dev, sg, nents, dir);
> > +   mb();
> > +}
> > +
> 
> I am not really sure why you have these extra functions, when you could
> just modify the dma_ops to point to the swiotlb ones
> 
> > +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, 
> > phys_addr_t paddr)
> > +{
> > +   return (paddr < 0x1000) ?
> > +   (paddr | 0x8000) : paddr;
> > +}
> > +
> > +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, 
> > dma_addr_t daddr)
> > +{
> > +   return (daddr < 0x9000 && daddr >= 0x8000) ?
> > +   (daddr & 0x0fff) : daddr;
> > +}
> > +
> > +struct loongson_dma_map_ops {
> > +   struct dma_map_ops dma_map_ops;
> > +   dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
> > +   phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
> > +};
> > +
> > +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
> > +{
> > +   struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
> > +   struct loongson_dma_map_ops, 
> > dma_map_ops);
> > +
> > +   return ops->phys_to_dma(dev, paddr);
> > +}
> > +
> > +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
> > +{
> > +   struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
> > +   struct loongson_dma_map_ops, 
> > dma_map_ops);
> > +
> > +   return ops->dma_to_phys(dev, daddr);
> > +}
> > +
> > +static int loongson_dma_set_mask(struct device *dev, u64 mask)
> > +{
> > +   /* Loongson doesn't support DMA above 32-bit */
> > +   if (mask > DMA_BIT_MASK(32))
> > +   return -EIO;
> > +
> > +   *dev->dma_mask = mask;
> > +
> > +   return 0;
> > +}
> > +
> > +static struct 

Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (4GB).

2012-08-15 Thread Ralf Baechle
On Mon, Aug 13, 2012 at 01:54:47PM -0400, Konrad Rzeszutek Wilk wrote:

  +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
  +   dma_addr_t *dma_handle, gfp_t gfp, struct 
  dma_attrs *attrs)
  +{
  +   void *ret;
  +
  +   if (dma_alloc_from_coherent(dev, size, dma_handle, ret))
  +   return ret;
  +
  +   /* ignore region specifiers */
  +   gfp = ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
  +
  +#ifdef CONFIG_ZONE_DMA
  +   if (dev == NULL)
  +   gfp |= __GFP_DMA;
 
 When would this happen? dev == NULL?

A legacy (ISA) device driver.  Some of the Loongsons have some kind of
southbridge which incorporates legacy devices though of the top of my
head I'm not sure which if any of these are actually being used.  Huacai?

  +   else if (dev-coherent_dma_mask = DMA_BIT_MASK(24))
  +   gfp |= __GFP_DMA;
  +   else
  +#endif
  +#ifdef CONFIG_ZONE_DMA32
  +   if (dev-coherent_dma_mask = DMA_BIT_MASK(32))
  +   gfp |= __GFP_DMA32;
  +   else
 
 Why the 'else'
  +#endif
  +   ;
 
 why?
  +   gfp |= __GFP_NORETRY;
  +
  +   ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
  +   mb();
 
 Why the 'mb()' ? Can you just do
   return swiotlb_alloc_coherent(...) 
 
  +   return ret;
  +}
  +
  +static void loongson_dma_free_coherent(struct device *dev, size_t size,
  +   void *vaddr, dma_addr_t dma_handle, struct 
  dma_attrs *attrs)
  +{
  +   int order = get_order(size);
  +
  +   if (dma_release_from_coherent(dev, order, vaddr))
  +   return;
  +
  +   swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  +}
  +
  +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
  *page,
  +   unsigned long offset, size_t size,
  +   enum dma_data_direction dir,
  +   struct dma_attrs *attrs)
  +{
  +   dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
  +   dir, attrs);
  +   mb();
 
 Please do 'return swiotlb_map_page(..)'..
 
 But if you are doing that why don't you just set the dma_ops.map_page = 
 swiotlb_map_page
 ?
 
 
  +   return daddr;
  +}
  +
  +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
  +   int nents, enum dma_data_direction dir,
  +   struct dma_attrs *attrs)
  +{
  +   int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
  +   mb();
  +
  +   return r;
  +}
  +
  +static void loongson_dma_sync_single_for_device(struct device *dev,
  +   dma_addr_t dma_handle, size_t size,
  +   enum dma_data_direction dir)
  +{
  +   swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
  +   mb();
  +}
  +
  +static void loongson_dma_sync_sg_for_device(struct device *dev,
  +   struct scatterlist *sg, int nents,
  +   enum dma_data_direction dir)
  +{
  +   swiotlb_sync_sg_for_device(dev, sg, nents, dir);
  +   mb();
  +}
  +
 
 I am not really sure why you have these extra functions, when you could
 just modify the dma_ops to point to the swiotlb ones
 
  +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, 
  phys_addr_t paddr)
  +{
  +   return (paddr  0x1000) ?
  +   (paddr | 0x8000) : paddr;
  +}
  +
  +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, 
  dma_addr_t daddr)
  +{
  +   return (daddr  0x9000  daddr = 0x8000) ?
  +   (daddr  0x0fff) : daddr;
  +}
  +
  +struct loongson_dma_map_ops {
  +   struct dma_map_ops dma_map_ops;
  +   dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
  +   phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
  +};
  +
  +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
  +{
  +   struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
  +   struct loongson_dma_map_ops, 
  dma_map_ops);
  +
  +   return ops-phys_to_dma(dev, paddr);
  +}
  +
  +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
  +{
  +   struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
  +   struct loongson_dma_map_ops, 
  dma_map_ops);
  +
  +   return ops-dma_to_phys(dev, daddr);
  +}
  +
  +static int loongson_dma_set_mask(struct device *dev, u64 mask)
  +{
  +   /* Loongson doesn't support DMA above 32-bit */
  +   if (mask  DMA_BIT_MASK(32))
  +   return -EIO;
  +
  +   *dev-dma_mask = mask;
  +
  +   return 0;
  +}
  +
  +static struct loongson_dma_map_ops loongson_linear_dma_map_ops = {
  +   .dma_map_ops = {
  +   .alloc = loongson_dma_alloc_coherent,
  +   .free = loongson_dma_free_coherent,
  +   .map_page = loongson_dma_map_page,
 
 But why not 'swiotlb_map_page'?
 
  +   .unmap_page = 

Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (4GB).

2012-08-15 Thread Huacai Chen
On Thu, Aug 16, 2012 at 4:24 AM, Ralf Baechle r...@linux-mips.org wrote:
 On Mon, Aug 13, 2012 at 01:54:47PM -0400, Konrad Rzeszutek Wilk wrote:

  +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
  +   dma_addr_t *dma_handle, gfp_t gfp, struct 
  dma_attrs *attrs)
  +{
  +   void *ret;
  +
  +   if (dma_alloc_from_coherent(dev, size, dma_handle, ret))
  +   return ret;
  +
  +   /* ignore region specifiers */
  +   gfp = ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
  +
  +#ifdef CONFIG_ZONE_DMA
  +   if (dev == NULL)
  +   gfp |= __GFP_DMA;

 When would this happen? dev == NULL?

 A legacy (ISA) device driver.  Some of the Loongsons have some kind of
 southbridge which incorporates legacy devices though of the top of my
 head I'm not sure which if any of these are actually being used.  Huacai?
ISA driver isn't used now, but I think keep dev == NULL here has no
side effect.
BTW, dev == NULL only happend in ISA case?  I use grep
pci_alloc_consistent drivers/ -rwI | grep NULL and also get some
lines.


  +   else if (dev-coherent_dma_mask = DMA_BIT_MASK(24))
  +   gfp |= __GFP_DMA;
  +   else
  +#endif
  +#ifdef CONFIG_ZONE_DMA32
  +   if (dev-coherent_dma_mask = DMA_BIT_MASK(32))
  +   gfp |= __GFP_DMA32;
  +   else

 Why the 'else'
  +#endif
  +   ;

 why?
  +   gfp |= __GFP_NORETRY;
  +
  +   ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
  +   mb();

 Why the 'mb()' ? Can you just do
   return swiotlb_alloc_coherent(...)

  +   return ret;
  +}
  +
  +static void loongson_dma_free_coherent(struct device *dev, size_t size,
  +   void *vaddr, dma_addr_t dma_handle, struct 
  dma_attrs *attrs)
  +{
  +   int order = get_order(size);
  +
  +   if (dma_release_from_coherent(dev, order, vaddr))
  +   return;
  +
  +   swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  +}
  +
  +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
  *page,
  +   unsigned long offset, size_t size,
  +   enum dma_data_direction dir,
  +   struct dma_attrs *attrs)
  +{
  +   dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
  +   dir, attrs);
  +   mb();

 Please do 'return swiotlb_map_page(..)'..

 But if you are doing that why don't you just set the dma_ops.map_page = 
 swiotlb_map_page
 ?


  +   return daddr;
  +}
  +
  +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
  +   int nents, enum dma_data_direction dir,
  +   struct dma_attrs *attrs)
  +{
  +   int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
  +   mb();
  +
  +   return r;
  +}
  +
  +static void loongson_dma_sync_single_for_device(struct device *dev,
  +   dma_addr_t dma_handle, size_t size,
  +   enum dma_data_direction dir)
  +{
  +   swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
  +   mb();
  +}
  +
  +static void loongson_dma_sync_sg_for_device(struct device *dev,
  +   struct scatterlist *sg, int nents,
  +   enum dma_data_direction dir)
  +{
  +   swiotlb_sync_sg_for_device(dev, sg, nents, dir);
  +   mb();
  +}
  +

 I am not really sure why you have these extra functions, when you could
 just modify the dma_ops to point to the swiotlb ones

  +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, 
  phys_addr_t paddr)
  +{
  +   return (paddr  0x1000) ?
  +   (paddr | 0x8000) : paddr;
  +}
  +
  +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, 
  dma_addr_t daddr)
  +{
  +   return (daddr  0x9000  daddr = 0x8000) ?
  +   (daddr  0x0fff) : daddr;
  +}
  +
  +struct loongson_dma_map_ops {
  +   struct dma_map_ops dma_map_ops;
  +   dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
  +   phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
  +};
  +
  +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
  +{
  +   struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
  +   struct loongson_dma_map_ops, 
  dma_map_ops);
  +
  +   return ops-phys_to_dma(dev, paddr);
  +}
  +
  +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
  +{
  +   struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
  +   struct loongson_dma_map_ops, 
  dma_map_ops);
  +
  +   return ops-dma_to_phys(dev, daddr);
  +}
  +
  +static int loongson_dma_set_mask(struct device *dev, u64 mask)
  +{
  +   /* Loongson doesn't support DMA above 32-bit */
  +   if (mask  DMA_BIT_MASK(32))
  +   return -EIO;
  +
  +   *dev-dma_mask = mask;
  +
  +   return 0;
  +}
  +
  +static struct loongson_dma_map_ops 

Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (>4GB).

2012-08-14 Thread Huacai Chen
On Tue, Aug 14, 2012 at 1:54 AM, Konrad Rzeszutek Wilk
 wrote:
>> +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
>> + dma_addr_t *dma_handle, gfp_t gfp, struct 
>> dma_attrs *attrs)
>> +{
>> + void *ret;
>> +
>> + if (dma_alloc_from_coherent(dev, size, dma_handle, ))
>> + return ret;
>> +
>> + /* ignore region specifiers */
>> + gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
>> +
>> +#ifdef CONFIG_ZONE_DMA
>> + if (dev == NULL)
>> + gfp |= __GFP_DMA;
>
> When would this happen? dev == NULL?
This can really happen, "grep dma_alloc_coherent drivers/ -rwI | grep
NULL" will get lots of information.

>
>> + else if (dev->coherent_dma_mask <= DMA_BIT_MASK(24))
>> + gfp |= __GFP_DMA;
>> + else
>> +#endif
>> +#ifdef CONFIG_ZONE_DMA32
>> + if (dev->coherent_dma_mask <= DMA_BIT_MASK(32))
>> + gfp |= __GFP_DMA32;
>> + else
>
> Why the 'else'
>> +#endif
>> + ;
>
> why?
>> + gfp |= __GFP_NORETRY;
>> +
>> + ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
>> + mb();
>
> Why the 'mb()' ? Can you just do
> return swiotlb_alloc_coherent(...)
>
>> + return ret;
>> +}
>> +
>> +static void loongson_dma_free_coherent(struct device *dev, size_t size,
>> + void *vaddr, dma_addr_t dma_handle, struct 
>> dma_attrs *attrs)
>> +{
>> + int order = get_order(size);
>> +
>> + if (dma_release_from_coherent(dev, order, vaddr))
>> + return;
>> +
>> + swiotlb_free_coherent(dev, size, vaddr, dma_handle);
>> +}
>> +
>> +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
>> *page,
>> + unsigned long offset, size_t size,
>> + enum dma_data_direction dir,
>> + struct dma_attrs *attrs)
>> +{
>> + dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
>> + dir, attrs);
>> + mb();
>
> Please do 'return swiotlb_map_page(..)'..
mb() is needed because of cache coherency (CPU write some data, then
map the page for a device, if without mb(), then device may read wrong
data.)

>
> But if you are doing that why don't you just set the dma_ops.map_page = 
> swiotlb_map_page
> ?
>
>
>> + return daddr;
>> +}
>> +
>> +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
>> + int nents, enum dma_data_direction dir,
>> + struct dma_attrs *attrs)
>> +{
>> + int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
>> + mb();
>> +
>> + return r;
>> +}
>> +
>> +static void loongson_dma_sync_single_for_device(struct device *dev,
>> + dma_addr_t dma_handle, size_t size,
>> + enum dma_data_direction dir)
>> +{
>> + swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
>> + mb();
>> +}
>> +
>> +static void loongson_dma_sync_sg_for_device(struct device *dev,
>> + struct scatterlist *sg, int nents,
>> + enum dma_data_direction dir)
>> +{
>> + swiotlb_sync_sg_for_device(dev, sg, nents, dir);
>> + mb();
>> +}
>> +
>
> I am not really sure why you have these extra functions, when you could
> just modify the dma_ops to point to the swiotlb ones
>
>> +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, 
>> phys_addr_t paddr)
>> +{
>> + return (paddr < 0x1000) ?
>> + (paddr | 0x8000) : paddr;
>> +}
>> +
>> +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, 
>> dma_addr_t daddr)
>> +{
>> + return (daddr < 0x9000 && daddr >= 0x8000) ?
>> + (daddr & 0x0fff) : daddr;
>> +}
>> +
>> +struct loongson_dma_map_ops {
>> + struct dma_map_ops dma_map_ops;
>> + dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
>> + phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
>> +};
>> +
>> +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
>> +{
>> + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
>> + struct loongson_dma_map_ops, 
>> dma_map_ops);
>> +
>> + return ops->phys_to_dma(dev, paddr);
>> +}
>> +
>> +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
>> +{
>> + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
>> + struct loongson_dma_map_ops, 
>> dma_map_ops);
>> +
>> + return ops->dma_to_phys(dev, daddr);
>> +}
>> +
>> +static int loongson_dma_set_mask(struct device *dev, u64 mask)
>> +{
>> + /* Loongson doesn't support DMA above 32-bit */
>> + if (mask > DMA_BIT_MASK(32))
>> + return -EIO;
>> +
>> + *dev->dma_mask = mask;
>> +
>> + return 0;
>> +}
>> +
>> +static 

Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (>4GB).

2012-08-14 Thread David Daney

On 08/13/2012 10:57 PM, Huacai Chen wrote:

Hi, David,

Seems like you are the original author of code in
arch/mips/cavium-octeon/dma-octeon.c. Could you please tell me why we
need mb() in alloc_coherent(), map_page(), map_sg()? It seems like
because of cache coherency (CPU write some data, then map the page for
a device, if without mb(), then device may read wrong data.) but I'm
not sure.



That is essentially correct.

The DMA API requires certain memory barrier semantics.  These are 
achieved with the mb() in the OCTEON code.



On Tue, Aug 14, 2012 at 1:54 AM, Konrad Rzeszutek Wilk
  wrote:

+static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
+ dma_addr_t *dma_handle, gfp_t gfp, struct 
dma_attrs *attrs)


I know nothing about Loongson, so I cannot comment on what is required 
for it.


David Daney

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (4GB).

2012-08-14 Thread David Daney

On 08/13/2012 10:57 PM, Huacai Chen wrote:

Hi, David,

Seems like you are the original author of code in
arch/mips/cavium-octeon/dma-octeon.c. Could you please tell me why we
need mb() in alloc_coherent(), map_page(), map_sg()? It seems like
because of cache coherency (CPU write some data, then map the page for
a device, if without mb(), then device may read wrong data.) but I'm
not sure.



That is essentially correct.

The DMA API requires certain memory barrier semantics.  These are 
achieved with the mb() in the OCTEON code.



On Tue, Aug 14, 2012 at 1:54 AM, Konrad Rzeszutek Wilk
konrad.w...@oracle.com  wrote:

+static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
+ dma_addr_t *dma_handle, gfp_t gfp, struct 
dma_attrs *attrs)


I know nothing about Loongson, so I cannot comment on what is required 
for it.


David Daney

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (4GB).

2012-08-14 Thread Huacai Chen
On Tue, Aug 14, 2012 at 1:54 AM, Konrad Rzeszutek Wilk
konrad.w...@oracle.com wrote:
 +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
 + dma_addr_t *dma_handle, gfp_t gfp, struct 
 dma_attrs *attrs)
 +{
 + void *ret;
 +
 + if (dma_alloc_from_coherent(dev, size, dma_handle, ret))
 + return ret;
 +
 + /* ignore region specifiers */
 + gfp = ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
 +
 +#ifdef CONFIG_ZONE_DMA
 + if (dev == NULL)
 + gfp |= __GFP_DMA;

 When would this happen? dev == NULL?
This can really happen, grep dma_alloc_coherent drivers/ -rwI | grep
NULL will get lots of information.


 + else if (dev-coherent_dma_mask = DMA_BIT_MASK(24))
 + gfp |= __GFP_DMA;
 + else
 +#endif
 +#ifdef CONFIG_ZONE_DMA32
 + if (dev-coherent_dma_mask = DMA_BIT_MASK(32))
 + gfp |= __GFP_DMA32;
 + else

 Why the 'else'
 +#endif
 + ;

 why?
 + gfp |= __GFP_NORETRY;
 +
 + ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
 + mb();

 Why the 'mb()' ? Can you just do
 return swiotlb_alloc_coherent(...)

 + return ret;
 +}
 +
 +static void loongson_dma_free_coherent(struct device *dev, size_t size,
 + void *vaddr, dma_addr_t dma_handle, struct 
 dma_attrs *attrs)
 +{
 + int order = get_order(size);
 +
 + if (dma_release_from_coherent(dev, order, vaddr))
 + return;
 +
 + swiotlb_free_coherent(dev, size, vaddr, dma_handle);
 +}
 +
 +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
 *page,
 + unsigned long offset, size_t size,
 + enum dma_data_direction dir,
 + struct dma_attrs *attrs)
 +{
 + dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
 + dir, attrs);
 + mb();

 Please do 'return swiotlb_map_page(..)'..
mb() is needed because of cache coherency (CPU write some data, then
map the page for a device, if without mb(), then device may read wrong
data.)


 But if you are doing that why don't you just set the dma_ops.map_page = 
 swiotlb_map_page
 ?


 + return daddr;
 +}
 +
 +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
 + int nents, enum dma_data_direction dir,
 + struct dma_attrs *attrs)
 +{
 + int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
 + mb();
 +
 + return r;
 +}
 +
 +static void loongson_dma_sync_single_for_device(struct device *dev,
 + dma_addr_t dma_handle, size_t size,
 + enum dma_data_direction dir)
 +{
 + swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
 + mb();
 +}
 +
 +static void loongson_dma_sync_sg_for_device(struct device *dev,
 + struct scatterlist *sg, int nents,
 + enum dma_data_direction dir)
 +{
 + swiotlb_sync_sg_for_device(dev, sg, nents, dir);
 + mb();
 +}
 +

 I am not really sure why you have these extra functions, when you could
 just modify the dma_ops to point to the swiotlb ones

 +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, 
 phys_addr_t paddr)
 +{
 + return (paddr  0x1000) ?
 + (paddr | 0x8000) : paddr;
 +}
 +
 +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, 
 dma_addr_t daddr)
 +{
 + return (daddr  0x9000  daddr = 0x8000) ?
 + (daddr  0x0fff) : daddr;
 +}
 +
 +struct loongson_dma_map_ops {
 + struct dma_map_ops dma_map_ops;
 + dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
 + phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
 +};
 +
 +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
 +{
 + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
 + struct loongson_dma_map_ops, 
 dma_map_ops);
 +
 + return ops-phys_to_dma(dev, paddr);
 +}
 +
 +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
 +{
 + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
 + struct loongson_dma_map_ops, 
 dma_map_ops);
 +
 + return ops-dma_to_phys(dev, daddr);
 +}
 +
 +static int loongson_dma_set_mask(struct device *dev, u64 mask)
 +{
 + /* Loongson doesn't support DMA above 32-bit */
 + if (mask  DMA_BIT_MASK(32))
 + return -EIO;
 +
 + *dev-dma_mask = mask;
 +
 + return 0;
 +}
 +
 +static struct loongson_dma_map_ops loongson_linear_dma_map_ops = {
 + .dma_map_ops = {
 + .alloc = loongson_dma_alloc_coherent,
 + .free = loongson_dma_free_coherent,
 + .map_page = loongson_dma_map_page,

 But why not 'swiotlb_map_page'?

 +

Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (>4GB).

2012-08-13 Thread Huacai Chen
Hi, David,

Seems like you are the original author of code in
arch/mips/cavium-octeon/dma-octeon.c. Could you please tell me why we
need mb() in alloc_coherent(), map_page(), map_sg()? It seems like
because of cache coherency (CPU write some data, then map the page for
a device, if without mb(), then device may read wrong data.) but I'm
not sure.

On Tue, Aug 14, 2012 at 1:54 AM, Konrad Rzeszutek Wilk
 wrote:
>> +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
>> + dma_addr_t *dma_handle, gfp_t gfp, struct 
>> dma_attrs *attrs)
>> +{
>> + void *ret;
>> +
>> + if (dma_alloc_from_coherent(dev, size, dma_handle, ))
>> + return ret;
>> +
>> + /* ignore region specifiers */
>> + gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
>> +
>> +#ifdef CONFIG_ZONE_DMA
>> + if (dev == NULL)
>> + gfp |= __GFP_DMA;
>
> When would this happen? dev == NULL?
>
>> + else if (dev->coherent_dma_mask <= DMA_BIT_MASK(24))
>> + gfp |= __GFP_DMA;
>> + else
>> +#endif
>> +#ifdef CONFIG_ZONE_DMA32
>> + if (dev->coherent_dma_mask <= DMA_BIT_MASK(32))
>> + gfp |= __GFP_DMA32;
>> + else
>
> Why the 'else'
>> +#endif
>> + ;
>
> why?
>> + gfp |= __GFP_NORETRY;
>> +
>> + ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
>> + mb();
>
> Why the 'mb()' ? Can you just do
> return swiotlb_alloc_coherent(...)
>
>> + return ret;
>> +}
>> +
>> +static void loongson_dma_free_coherent(struct device *dev, size_t size,
>> + void *vaddr, dma_addr_t dma_handle, struct 
>> dma_attrs *attrs)
>> +{
>> + int order = get_order(size);
>> +
>> + if (dma_release_from_coherent(dev, order, vaddr))
>> + return;
>> +
>> + swiotlb_free_coherent(dev, size, vaddr, dma_handle);
>> +}
>> +
>> +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
>> *page,
>> + unsigned long offset, size_t size,
>> + enum dma_data_direction dir,
>> + struct dma_attrs *attrs)
>> +{
>> + dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
>> + dir, attrs);
>> + mb();
>
> Please do 'return swiotlb_map_page(..)'..
>
> But if you are doing that why don't you just set the dma_ops.map_page = 
> swiotlb_map_page
> ?
>
>
>> + return daddr;
>> +}
>> +
>> +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
>> + int nents, enum dma_data_direction dir,
>> + struct dma_attrs *attrs)
>> +{
>> + int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
>> + mb();
>> +
>> + return r;
>> +}
>> +
>> +static void loongson_dma_sync_single_for_device(struct device *dev,
>> + dma_addr_t dma_handle, size_t size,
>> + enum dma_data_direction dir)
>> +{
>> + swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
>> + mb();
>> +}
>> +
>> +static void loongson_dma_sync_sg_for_device(struct device *dev,
>> + struct scatterlist *sg, int nents,
>> + enum dma_data_direction dir)
>> +{
>> + swiotlb_sync_sg_for_device(dev, sg, nents, dir);
>> + mb();
>> +}
>> +
>
> I am not really sure why you have these extra functions, when you could
> just modify the dma_ops to point to the swiotlb ones
>
>> +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, 
>> phys_addr_t paddr)
>> +{
>> + return (paddr < 0x1000) ?
>> + (paddr | 0x8000) : paddr;
>> +}
>> +
>> +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, 
>> dma_addr_t daddr)
>> +{
>> + return (daddr < 0x9000 && daddr >= 0x8000) ?
>> + (daddr & 0x0fff) : daddr;
>> +}
>> +
>> +struct loongson_dma_map_ops {
>> + struct dma_map_ops dma_map_ops;
>> + dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
>> + phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
>> +};
>> +
>> +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
>> +{
>> + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
>> + struct loongson_dma_map_ops, 
>> dma_map_ops);
>> +
>> + return ops->phys_to_dma(dev, paddr);
>> +}
>> +
>> +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
>> +{
>> + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
>> + struct loongson_dma_map_ops, 
>> dma_map_ops);
>> +
>> + return ops->dma_to_phys(dev, daddr);
>> +}
>> +
>> +static int loongson_dma_set_mask(struct device *dev, u64 mask)
>> +{
>> + /* Loongson doesn't support DMA above 32-bit */
>> + if (mask > DMA_BIT_MASK(32))
>> + 

Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (>4GB).

2012-08-13 Thread Huacai Chen
Most of the code are copied from arch/mips/cavium-octeon/dma-octeon.c
and they work well.
Anyway, I'll try your suggestions, thank you.

On Tue, Aug 14, 2012 at 1:54 AM, Konrad Rzeszutek Wilk
 wrote:
>> +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
>> + dma_addr_t *dma_handle, gfp_t gfp, struct 
>> dma_attrs *attrs)
>> +{
>> + void *ret;
>> +
>> + if (dma_alloc_from_coherent(dev, size, dma_handle, ))
>> + return ret;
>> +
>> + /* ignore region specifiers */
>> + gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
>> +
>> +#ifdef CONFIG_ZONE_DMA
>> + if (dev == NULL)
>> + gfp |= __GFP_DMA;
>
> When would this happen? dev == NULL?
>
>> + else if (dev->coherent_dma_mask <= DMA_BIT_MASK(24))
>> + gfp |= __GFP_DMA;
>> + else
>> +#endif
>> +#ifdef CONFIG_ZONE_DMA32
>> + if (dev->coherent_dma_mask <= DMA_BIT_MASK(32))
>> + gfp |= __GFP_DMA32;
>> + else
>
> Why the 'else'
>> +#endif
>> + ;
>
> why?
>> + gfp |= __GFP_NORETRY;
>> +
>> + ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
>> + mb();
>
> Why the 'mb()' ? Can you just do
> return swiotlb_alloc_coherent(...)
>
>> + return ret;
>> +}
>> +
>> +static void loongson_dma_free_coherent(struct device *dev, size_t size,
>> + void *vaddr, dma_addr_t dma_handle, struct 
>> dma_attrs *attrs)
>> +{
>> + int order = get_order(size);
>> +
>> + if (dma_release_from_coherent(dev, order, vaddr))
>> + return;
>> +
>> + swiotlb_free_coherent(dev, size, vaddr, dma_handle);
>> +}
>> +
>> +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
>> *page,
>> + unsigned long offset, size_t size,
>> + enum dma_data_direction dir,
>> + struct dma_attrs *attrs)
>> +{
>> + dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
>> + dir, attrs);
>> + mb();
>
> Please do 'return swiotlb_map_page(..)'..
>
> But if you are doing that why don't you just set the dma_ops.map_page = 
> swiotlb_map_page
> ?
>
>
>> + return daddr;
>> +}
>> +
>> +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
>> + int nents, enum dma_data_direction dir,
>> + struct dma_attrs *attrs)
>> +{
>> + int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
>> + mb();
>> +
>> + return r;
>> +}
>> +
>> +static void loongson_dma_sync_single_for_device(struct device *dev,
>> + dma_addr_t dma_handle, size_t size,
>> + enum dma_data_direction dir)
>> +{
>> + swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
>> + mb();
>> +}
>> +
>> +static void loongson_dma_sync_sg_for_device(struct device *dev,
>> + struct scatterlist *sg, int nents,
>> + enum dma_data_direction dir)
>> +{
>> + swiotlb_sync_sg_for_device(dev, sg, nents, dir);
>> + mb();
>> +}
>> +
>
> I am not really sure why you have these extra functions, when you could
> just modify the dma_ops to point to the swiotlb ones
>
>> +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, 
>> phys_addr_t paddr)
>> +{
>> + return (paddr < 0x1000) ?
>> + (paddr | 0x8000) : paddr;
>> +}
>> +
>> +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, 
>> dma_addr_t daddr)
>> +{
>> + return (daddr < 0x9000 && daddr >= 0x8000) ?
>> + (daddr & 0x0fff) : daddr;
>> +}
>> +
>> +struct loongson_dma_map_ops {
>> + struct dma_map_ops dma_map_ops;
>> + dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
>> + phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
>> +};
>> +
>> +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
>> +{
>> + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
>> + struct loongson_dma_map_ops, 
>> dma_map_ops);
>> +
>> + return ops->phys_to_dma(dev, paddr);
>> +}
>> +
>> +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
>> +{
>> + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
>> + struct loongson_dma_map_ops, 
>> dma_map_ops);
>> +
>> + return ops->dma_to_phys(dev, daddr);
>> +}
>> +
>> +static int loongson_dma_set_mask(struct device *dev, u64 mask)
>> +{
>> + /* Loongson doesn't support DMA above 32-bit */
>> + if (mask > DMA_BIT_MASK(32))
>> + return -EIO;
>> +
>> + *dev->dma_mask = mask;
>> +
>> + return 0;
>> +}
>> +
>> +static struct loongson_dma_map_ops loongson_linear_dma_map_ops = {
>> + .dma_map_ops = {
>> + .alloc = 

Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (>4GB).

2012-08-13 Thread Konrad Rzeszutek Wilk
> +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
> + dma_addr_t *dma_handle, gfp_t gfp, struct 
> dma_attrs *attrs)
> +{
> + void *ret;
> +
> + if (dma_alloc_from_coherent(dev, size, dma_handle, ))
> + return ret;
> +
> + /* ignore region specifiers */
> + gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
> +
> +#ifdef CONFIG_ZONE_DMA
> + if (dev == NULL)
> + gfp |= __GFP_DMA;

When would this happen? dev == NULL?

> + else if (dev->coherent_dma_mask <= DMA_BIT_MASK(24))
> + gfp |= __GFP_DMA;
> + else
> +#endif
> +#ifdef CONFIG_ZONE_DMA32
> + if (dev->coherent_dma_mask <= DMA_BIT_MASK(32))
> + gfp |= __GFP_DMA32;
> + else

Why the 'else'
> +#endif
> + ;

why?
> + gfp |= __GFP_NORETRY;
> +
> + ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
> + mb();

Why the 'mb()' ? Can you just do
return swiotlb_alloc_coherent(...) 

> + return ret;
> +}
> +
> +static void loongson_dma_free_coherent(struct device *dev, size_t size,
> + void *vaddr, dma_addr_t dma_handle, struct 
> dma_attrs *attrs)
> +{
> + int order = get_order(size);
> +
> + if (dma_release_from_coherent(dev, order, vaddr))
> + return;
> +
> + swiotlb_free_coherent(dev, size, vaddr, dma_handle);
> +}
> +
> +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
> *page,
> + unsigned long offset, size_t size,
> + enum dma_data_direction dir,
> + struct dma_attrs *attrs)
> +{
> + dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
> + dir, attrs);
> + mb();

Please do 'return swiotlb_map_page(..)'..

But if you are doing that why don't you just set the dma_ops.map_page = 
swiotlb_map_page
?


> + return daddr;
> +}
> +
> +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
> + int nents, enum dma_data_direction dir,
> + struct dma_attrs *attrs)
> +{
> + int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
> + mb();
> +
> + return r;
> +}
> +
> +static void loongson_dma_sync_single_for_device(struct device *dev,
> + dma_addr_t dma_handle, size_t size,
> + enum dma_data_direction dir)
> +{
> + swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
> + mb();
> +}
> +
> +static void loongson_dma_sync_sg_for_device(struct device *dev,
> + struct scatterlist *sg, int nents,
> + enum dma_data_direction dir)
> +{
> + swiotlb_sync_sg_for_device(dev, sg, nents, dir);
> + mb();
> +}
> +

I am not really sure why you have these extra functions, when you could
just modify the dma_ops to point to the swiotlb ones

> +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, phys_addr_t 
> paddr)
> +{
> + return (paddr < 0x1000) ?
> + (paddr | 0x8000) : paddr;
> +}
> +
> +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, dma_addr_t 
> daddr)
> +{
> + return (daddr < 0x9000 && daddr >= 0x8000) ?
> + (daddr & 0x0fff) : daddr;
> +}
> +
> +struct loongson_dma_map_ops {
> + struct dma_map_ops dma_map_ops;
> + dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
> + phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
> +};
> +
> +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
> +{
> + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
> + struct loongson_dma_map_ops, 
> dma_map_ops);
> +
> + return ops->phys_to_dma(dev, paddr);
> +}
> +
> +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
> +{
> + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
> + struct loongson_dma_map_ops, 
> dma_map_ops);
> +
> + return ops->dma_to_phys(dev, daddr);
> +}
> +
> +static int loongson_dma_set_mask(struct device *dev, u64 mask)
> +{
> + /* Loongson doesn't support DMA above 32-bit */
> + if (mask > DMA_BIT_MASK(32))
> + return -EIO;
> +
> + *dev->dma_mask = mask;
> +
> + return 0;
> +}
> +
> +static struct loongson_dma_map_ops loongson_linear_dma_map_ops = {
> + .dma_map_ops = {
> + .alloc = loongson_dma_alloc_coherent,
> + .free = loongson_dma_free_coherent,
> + .map_page = loongson_dma_map_page,

But why not 'swiotlb_map_page'?

> + .unmap_page = swiotlb_unmap_page,
> + .map_sg = loongson_dma_map_sg,
> + .unmap_sg = swiotlb_unmap_sg_attrs,
> + .sync_single_for_cpu = 

Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (4GB).

2012-08-13 Thread Konrad Rzeszutek Wilk
 +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
 + dma_addr_t *dma_handle, gfp_t gfp, struct 
 dma_attrs *attrs)
 +{
 + void *ret;
 +
 + if (dma_alloc_from_coherent(dev, size, dma_handle, ret))
 + return ret;
 +
 + /* ignore region specifiers */
 + gfp = ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
 +
 +#ifdef CONFIG_ZONE_DMA
 + if (dev == NULL)
 + gfp |= __GFP_DMA;

When would this happen? dev == NULL?

 + else if (dev-coherent_dma_mask = DMA_BIT_MASK(24))
 + gfp |= __GFP_DMA;
 + else
 +#endif
 +#ifdef CONFIG_ZONE_DMA32
 + if (dev-coherent_dma_mask = DMA_BIT_MASK(32))
 + gfp |= __GFP_DMA32;
 + else

Why the 'else'
 +#endif
 + ;

why?
 + gfp |= __GFP_NORETRY;
 +
 + ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
 + mb();

Why the 'mb()' ? Can you just do
return swiotlb_alloc_coherent(...) 

 + return ret;
 +}
 +
 +static void loongson_dma_free_coherent(struct device *dev, size_t size,
 + void *vaddr, dma_addr_t dma_handle, struct 
 dma_attrs *attrs)
 +{
 + int order = get_order(size);
 +
 + if (dma_release_from_coherent(dev, order, vaddr))
 + return;
 +
 + swiotlb_free_coherent(dev, size, vaddr, dma_handle);
 +}
 +
 +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
 *page,
 + unsigned long offset, size_t size,
 + enum dma_data_direction dir,
 + struct dma_attrs *attrs)
 +{
 + dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
 + dir, attrs);
 + mb();

Please do 'return swiotlb_map_page(..)'..

But if you are doing that why don't you just set the dma_ops.map_page = 
swiotlb_map_page
?


 + return daddr;
 +}
 +
 +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
 + int nents, enum dma_data_direction dir,
 + struct dma_attrs *attrs)
 +{
 + int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
 + mb();
 +
 + return r;
 +}
 +
 +static void loongson_dma_sync_single_for_device(struct device *dev,
 + dma_addr_t dma_handle, size_t size,
 + enum dma_data_direction dir)
 +{
 + swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
 + mb();
 +}
 +
 +static void loongson_dma_sync_sg_for_device(struct device *dev,
 + struct scatterlist *sg, int nents,
 + enum dma_data_direction dir)
 +{
 + swiotlb_sync_sg_for_device(dev, sg, nents, dir);
 + mb();
 +}
 +

I am not really sure why you have these extra functions, when you could
just modify the dma_ops to point to the swiotlb ones

 +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, phys_addr_t 
 paddr)
 +{
 + return (paddr  0x1000) ?
 + (paddr | 0x8000) : paddr;
 +}
 +
 +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, dma_addr_t 
 daddr)
 +{
 + return (daddr  0x9000  daddr = 0x8000) ?
 + (daddr  0x0fff) : daddr;
 +}
 +
 +struct loongson_dma_map_ops {
 + struct dma_map_ops dma_map_ops;
 + dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
 + phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
 +};
 +
 +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
 +{
 + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
 + struct loongson_dma_map_ops, 
 dma_map_ops);
 +
 + return ops-phys_to_dma(dev, paddr);
 +}
 +
 +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
 +{
 + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
 + struct loongson_dma_map_ops, 
 dma_map_ops);
 +
 + return ops-dma_to_phys(dev, daddr);
 +}
 +
 +static int loongson_dma_set_mask(struct device *dev, u64 mask)
 +{
 + /* Loongson doesn't support DMA above 32-bit */
 + if (mask  DMA_BIT_MASK(32))
 + return -EIO;
 +
 + *dev-dma_mask = mask;
 +
 + return 0;
 +}
 +
 +static struct loongson_dma_map_ops loongson_linear_dma_map_ops = {
 + .dma_map_ops = {
 + .alloc = loongson_dma_alloc_coherent,
 + .free = loongson_dma_free_coherent,
 + .map_page = loongson_dma_map_page,

But why not 'swiotlb_map_page'?

 + .unmap_page = swiotlb_unmap_page,
 + .map_sg = loongson_dma_map_sg,
 + .unmap_sg = swiotlb_unmap_sg_attrs,
 + .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
 + .sync_single_for_device = loongson_dma_sync_single_for_device,
 + .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
 +   

Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (4GB).

2012-08-13 Thread Huacai Chen
Most of the code are copied from arch/mips/cavium-octeon/dma-octeon.c
and they work well.
Anyway, I'll try your suggestions, thank you.

On Tue, Aug 14, 2012 at 1:54 AM, Konrad Rzeszutek Wilk
konrad.w...@oracle.com wrote:
 +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
 + dma_addr_t *dma_handle, gfp_t gfp, struct 
 dma_attrs *attrs)
 +{
 + void *ret;
 +
 + if (dma_alloc_from_coherent(dev, size, dma_handle, ret))
 + return ret;
 +
 + /* ignore region specifiers */
 + gfp = ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
 +
 +#ifdef CONFIG_ZONE_DMA
 + if (dev == NULL)
 + gfp |= __GFP_DMA;

 When would this happen? dev == NULL?

 + else if (dev-coherent_dma_mask = DMA_BIT_MASK(24))
 + gfp |= __GFP_DMA;
 + else
 +#endif
 +#ifdef CONFIG_ZONE_DMA32
 + if (dev-coherent_dma_mask = DMA_BIT_MASK(32))
 + gfp |= __GFP_DMA32;
 + else

 Why the 'else'
 +#endif
 + ;

 why?
 + gfp |= __GFP_NORETRY;
 +
 + ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
 + mb();

 Why the 'mb()' ? Can you just do
 return swiotlb_alloc_coherent(...)

 + return ret;
 +}
 +
 +static void loongson_dma_free_coherent(struct device *dev, size_t size,
 + void *vaddr, dma_addr_t dma_handle, struct 
 dma_attrs *attrs)
 +{
 + int order = get_order(size);
 +
 + if (dma_release_from_coherent(dev, order, vaddr))
 + return;
 +
 + swiotlb_free_coherent(dev, size, vaddr, dma_handle);
 +}
 +
 +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
 *page,
 + unsigned long offset, size_t size,
 + enum dma_data_direction dir,
 + struct dma_attrs *attrs)
 +{
 + dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
 + dir, attrs);
 + mb();

 Please do 'return swiotlb_map_page(..)'..

 But if you are doing that why don't you just set the dma_ops.map_page = 
 swiotlb_map_page
 ?


 + return daddr;
 +}
 +
 +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
 + int nents, enum dma_data_direction dir,
 + struct dma_attrs *attrs)
 +{
 + int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
 + mb();
 +
 + return r;
 +}
 +
 +static void loongson_dma_sync_single_for_device(struct device *dev,
 + dma_addr_t dma_handle, size_t size,
 + enum dma_data_direction dir)
 +{
 + swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
 + mb();
 +}
 +
 +static void loongson_dma_sync_sg_for_device(struct device *dev,
 + struct scatterlist *sg, int nents,
 + enum dma_data_direction dir)
 +{
 + swiotlb_sync_sg_for_device(dev, sg, nents, dir);
 + mb();
 +}
 +

 I am not really sure why you have these extra functions, when you could
 just modify the dma_ops to point to the swiotlb ones

 +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, 
 phys_addr_t paddr)
 +{
 + return (paddr  0x1000) ?
 + (paddr | 0x8000) : paddr;
 +}
 +
 +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, 
 dma_addr_t daddr)
 +{
 + return (daddr  0x9000  daddr = 0x8000) ?
 + (daddr  0x0fff) : daddr;
 +}
 +
 +struct loongson_dma_map_ops {
 + struct dma_map_ops dma_map_ops;
 + dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
 + phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
 +};
 +
 +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
 +{
 + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
 + struct loongson_dma_map_ops, 
 dma_map_ops);
 +
 + return ops-phys_to_dma(dev, paddr);
 +}
 +
 +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
 +{
 + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
 + struct loongson_dma_map_ops, 
 dma_map_ops);
 +
 + return ops-dma_to_phys(dev, daddr);
 +}
 +
 +static int loongson_dma_set_mask(struct device *dev, u64 mask)
 +{
 + /* Loongson doesn't support DMA above 32-bit */
 + if (mask  DMA_BIT_MASK(32))
 + return -EIO;
 +
 + *dev-dma_mask = mask;
 +
 + return 0;
 +}
 +
 +static struct loongson_dma_map_ops loongson_linear_dma_map_ops = {
 + .dma_map_ops = {
 + .alloc = loongson_dma_alloc_coherent,
 + .free = loongson_dma_free_coherent,
 + .map_page = loongson_dma_map_page,

 But why not 'swiotlb_map_page'?

 + .unmap_page = swiotlb_unmap_page,
 + .map_sg = loongson_dma_map_sg,
 + .unmap_sg = 

Re: [PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (4GB).

2012-08-13 Thread Huacai Chen
Hi, David,

Seems like you are the original author of code in
arch/mips/cavium-octeon/dma-octeon.c. Could you please tell me why we
need mb() in alloc_coherent(), map_page(), map_sg()? It seems like
because of cache coherency (CPU write some data, then map the page for
a device, if without mb(), then device may read wrong data.) but I'm
not sure.

On Tue, Aug 14, 2012 at 1:54 AM, Konrad Rzeszutek Wilk
konrad.w...@oracle.com wrote:
 +static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
 + dma_addr_t *dma_handle, gfp_t gfp, struct 
 dma_attrs *attrs)
 +{
 + void *ret;
 +
 + if (dma_alloc_from_coherent(dev, size, dma_handle, ret))
 + return ret;
 +
 + /* ignore region specifiers */
 + gfp = ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
 +
 +#ifdef CONFIG_ZONE_DMA
 + if (dev == NULL)
 + gfp |= __GFP_DMA;

 When would this happen? dev == NULL?

 + else if (dev-coherent_dma_mask = DMA_BIT_MASK(24))
 + gfp |= __GFP_DMA;
 + else
 +#endif
 +#ifdef CONFIG_ZONE_DMA32
 + if (dev-coherent_dma_mask = DMA_BIT_MASK(32))
 + gfp |= __GFP_DMA32;
 + else

 Why the 'else'
 +#endif
 + ;

 why?
 + gfp |= __GFP_NORETRY;
 +
 + ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
 + mb();

 Why the 'mb()' ? Can you just do
 return swiotlb_alloc_coherent(...)

 + return ret;
 +}
 +
 +static void loongson_dma_free_coherent(struct device *dev, size_t size,
 + void *vaddr, dma_addr_t dma_handle, struct 
 dma_attrs *attrs)
 +{
 + int order = get_order(size);
 +
 + if (dma_release_from_coherent(dev, order, vaddr))
 + return;
 +
 + swiotlb_free_coherent(dev, size, vaddr, dma_handle);
 +}
 +
 +static dma_addr_t loongson_dma_map_page(struct device *dev, struct page 
 *page,
 + unsigned long offset, size_t size,
 + enum dma_data_direction dir,
 + struct dma_attrs *attrs)
 +{
 + dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
 + dir, attrs);
 + mb();

 Please do 'return swiotlb_map_page(..)'..

 But if you are doing that why don't you just set the dma_ops.map_page = 
 swiotlb_map_page
 ?


 + return daddr;
 +}
 +
 +static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
 + int nents, enum dma_data_direction dir,
 + struct dma_attrs *attrs)
 +{
 + int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
 + mb();
 +
 + return r;
 +}
 +
 +static void loongson_dma_sync_single_for_device(struct device *dev,
 + dma_addr_t dma_handle, size_t size,
 + enum dma_data_direction dir)
 +{
 + swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
 + mb();
 +}
 +
 +static void loongson_dma_sync_sg_for_device(struct device *dev,
 + struct scatterlist *sg, int nents,
 + enum dma_data_direction dir)
 +{
 + swiotlb_sync_sg_for_device(dev, sg, nents, dir);
 + mb();
 +}
 +

 I am not really sure why you have these extra functions, when you could
 just modify the dma_ops to point to the swiotlb ones

 +static dma_addr_t loongson_unity_phys_to_dma(struct device *dev, 
 phys_addr_t paddr)
 +{
 + return (paddr  0x1000) ?
 + (paddr | 0x8000) : paddr;
 +}
 +
 +static phys_addr_t loongson_unity_dma_to_phys(struct device *dev, 
 dma_addr_t daddr)
 +{
 + return (daddr  0x9000  daddr = 0x8000) ?
 + (daddr  0x0fff) : daddr;
 +}
 +
 +struct loongson_dma_map_ops {
 + struct dma_map_ops dma_map_ops;
 + dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
 + phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
 +};
 +
 +dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
 +{
 + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
 + struct loongson_dma_map_ops, 
 dma_map_ops);
 +
 + return ops-phys_to_dma(dev, paddr);
 +}
 +
 +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
 +{
 + struct loongson_dma_map_ops *ops = container_of(get_dma_ops(dev),
 + struct loongson_dma_map_ops, 
 dma_map_ops);
 +
 + return ops-dma_to_phys(dev, daddr);
 +}
 +
 +static int loongson_dma_set_mask(struct device *dev, u64 mask)
 +{
 + /* Loongson doesn't support DMA above 32-bit */
 + if (mask  DMA_BIT_MASK(32))
 + return -EIO;
 +
 + *dev-dma_mask = mask;
 +
 + return 0;
 +}
 +
 +static struct loongson_dma_map_ops loongson_linear_dma_map_ops = {
 + .dma_map_ops = {
 + .alloc = loongson_dma_alloc_coherent,
 + .free = 

[PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (>4GB).

2012-08-11 Thread Huacai Chen
This is probably a workaround because Loongson doesn't support DMA
address above 4GB. If memory is more than 4GB, CONFIG_SWIOTLB and
ZONE_DMA32 should be selected. In this way, DMA pages are allocated
below 4GB preferably.

However, CONFIG_SWIOTLB+ZONE_DMA32 is not enough, so, we provide a
platform-specific dma_map_ops::set_dma_mask() to make sure each
driver's dma_mask and coherent_dma_mask is below 32-bit.

Signed-off-by: Huacai Chen 
Signed-off-by: Hongliang Tao 
Signed-off-by: Hua Yan 
---
 arch/mips/include/asm/dma-mapping.h|5 +
 .../mips/include/asm/mach-loongson/dma-coherence.h |   25 +++-
 arch/mips/loongson/common/Makefile |5 +
 arch/mips/loongson/common/dma-swiotlb.c|  159 
 arch/mips/mm/dma-default.c |   13 ++-
 5 files changed, 202 insertions(+), 5 deletions(-)
 create mode 100644 arch/mips/loongson/common/dma-swiotlb.c

diff --git a/arch/mips/include/asm/dma-mapping.h 
b/arch/mips/include/asm/dma-mapping.h
index be39a12..35f91bc 100644
--- a/arch/mips/include/asm/dma-mapping.h
+++ b/arch/mips/include/asm/dma-mapping.h
@@ -46,9 +46,14 @@ static inline int dma_mapping_error(struct device *dev, u64 
mask)
 static inline int
 dma_set_mask(struct device *dev, u64 mask)
 {
+   struct dma_map_ops *ops = get_dma_ops(dev);
+
if(!dev->dma_mask || !dma_supported(dev, mask))
return -EIO;
 
+   if (ops->set_dma_mask)
+   return ops->set_dma_mask(dev, mask);
+
*dev->dma_mask = mask;
 
return 0;
diff --git a/arch/mips/include/asm/mach-loongson/dma-coherence.h 
b/arch/mips/include/asm/mach-loongson/dma-coherence.h
index e143305..b1dc286 100644
--- a/arch/mips/include/asm/mach-loongson/dma-coherence.h
+++ b/arch/mips/include/asm/mach-loongson/dma-coherence.h
@@ -13,26 +13,43 @@
 
 struct device;
 
+extern dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr);
+extern phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr);
 static inline dma_addr_t plat_map_dma_mem(struct device *dev, void *addr,
  size_t size)
 {
+#ifdef CONFIG_CPU_LOONGSON3
+   return virt_to_phys(addr) < 0x1000 ?
+   (virt_to_phys(addr) | 0x8000) : 
virt_to_phys(addr);
+#else
return virt_to_phys(addr) | 0x8000;
+#endif
 }
 
 static inline dma_addr_t plat_map_dma_mem_page(struct device *dev,
   struct page *page)
 {
+#ifdef CONFIG_CPU_LOONGSON3
+   return page_to_phys(page) < 0x1000 ?
+   (page_to_phys(page) | 0x8000) : 
page_to_phys(page);
+#else
return page_to_phys(page) | 0x8000;
+#endif
 }
 
 static inline unsigned long plat_dma_addr_to_phys(struct device *dev,
dma_addr_t dma_addr)
 {
-#if defined(CONFIG_CPU_LOONGSON2F) && defined(CONFIG_64BIT)
+#if defined(CONFIG_64BIT)
+#if defined(CONFIG_CPU_LOONGSON3)
+   return (dma_addr < 0x9000 && dma_addr >= 0x8000) ?
+   (dma_addr & 0x0fff) : dma_addr;
+#elif defined(CONFIG_CPU_LOONGSON2F)
return (dma_addr > 0x8fff) ? dma_addr : (dma_addr & 0x0fff);
+#endif /* CONFIG_CPU_LOONGSON3 */
 #else
return dma_addr & 0x7fff;
-#endif
+#endif /* CONFIG_64BIT */
 }
 
 static inline void plat_unmap_dma_mem(struct device *dev, dma_addr_t dma_addr,
@@ -65,7 +82,11 @@ static inline int plat_dma_mapping_error(struct device *dev,
 
 static inline int plat_device_is_coherent(struct device *dev)
 {
+#ifdef CONFIG_DMA_NONCOHERENT
return 0;
+#else
+   return 1;
+#endif /* CONFIG_DMA_NONCOHERENT */
 }
 
 #endif /* __ASM_MACH_LOONGSON_DMA_COHERENCE_H */
diff --git a/arch/mips/loongson/common/Makefile 
b/arch/mips/loongson/common/Makefile
index e526488..3a26109 100644
--- a/arch/mips/loongson/common/Makefile
+++ b/arch/mips/loongson/common/Makefile
@@ -25,3 +25,8 @@ obj-$(CONFIG_CS5536) += cs5536/
 #
 
 obj-$(CONFIG_LOONGSON_SUSPEND) += pm.o
+
+#
+# Big Memory Support
+#
+obj-$(CONFIG_LOONGSON_BIGMEM) += dma-swiotlb.o
diff --git a/arch/mips/loongson/common/dma-swiotlb.c 
b/arch/mips/loongson/common/dma-swiotlb.c
new file mode 100644
index 000..b87a21e
--- /dev/null
+++ b/arch/mips/loongson/common/dma-swiotlb.c
@@ -0,0 +1,159 @@
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
+   dma_addr_t *dma_handle, gfp_t gfp, struct 
dma_attrs *attrs)
+{
+   void *ret;
+
+   if (dma_alloc_from_coherent(dev, size, dma_handle, ))
+   return ret;
+
+   /* ignore region specifiers */
+   gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
+
+#ifdef CONFIG_ZONE_DMA
+   if (dev == NULL)
+   gfp |= __GFP_DMA;
+   else if (dev->coherent_dma_mask <= DMA_BIT_MASK(24))
+   gfp |= __GFP_DMA;
+   else

[PATCH V5 09/18] MIPS: Loongson: Add swiotlb to support big memory (4GB).

2012-08-11 Thread Huacai Chen
This is probably a workaround because Loongson doesn't support DMA
address above 4GB. If memory is more than 4GB, CONFIG_SWIOTLB and
ZONE_DMA32 should be selected. In this way, DMA pages are allocated
below 4GB preferably.

However, CONFIG_SWIOTLB+ZONE_DMA32 is not enough, so, we provide a
platform-specific dma_map_ops::set_dma_mask() to make sure each
driver's dma_mask and coherent_dma_mask is below 32-bit.

Signed-off-by: Huacai Chen che...@lemote.com
Signed-off-by: Hongliang Tao ta...@lemote.com
Signed-off-by: Hua Yan y...@lemote.com
---
 arch/mips/include/asm/dma-mapping.h|5 +
 .../mips/include/asm/mach-loongson/dma-coherence.h |   25 +++-
 arch/mips/loongson/common/Makefile |5 +
 arch/mips/loongson/common/dma-swiotlb.c|  159 
 arch/mips/mm/dma-default.c |   13 ++-
 5 files changed, 202 insertions(+), 5 deletions(-)
 create mode 100644 arch/mips/loongson/common/dma-swiotlb.c

diff --git a/arch/mips/include/asm/dma-mapping.h 
b/arch/mips/include/asm/dma-mapping.h
index be39a12..35f91bc 100644
--- a/arch/mips/include/asm/dma-mapping.h
+++ b/arch/mips/include/asm/dma-mapping.h
@@ -46,9 +46,14 @@ static inline int dma_mapping_error(struct device *dev, u64 
mask)
 static inline int
 dma_set_mask(struct device *dev, u64 mask)
 {
+   struct dma_map_ops *ops = get_dma_ops(dev);
+
if(!dev-dma_mask || !dma_supported(dev, mask))
return -EIO;
 
+   if (ops-set_dma_mask)
+   return ops-set_dma_mask(dev, mask);
+
*dev-dma_mask = mask;
 
return 0;
diff --git a/arch/mips/include/asm/mach-loongson/dma-coherence.h 
b/arch/mips/include/asm/mach-loongson/dma-coherence.h
index e143305..b1dc286 100644
--- a/arch/mips/include/asm/mach-loongson/dma-coherence.h
+++ b/arch/mips/include/asm/mach-loongson/dma-coherence.h
@@ -13,26 +13,43 @@
 
 struct device;
 
+extern dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr);
+extern phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr);
 static inline dma_addr_t plat_map_dma_mem(struct device *dev, void *addr,
  size_t size)
 {
+#ifdef CONFIG_CPU_LOONGSON3
+   return virt_to_phys(addr)  0x1000 ?
+   (virt_to_phys(addr) | 0x8000) : 
virt_to_phys(addr);
+#else
return virt_to_phys(addr) | 0x8000;
+#endif
 }
 
 static inline dma_addr_t plat_map_dma_mem_page(struct device *dev,
   struct page *page)
 {
+#ifdef CONFIG_CPU_LOONGSON3
+   return page_to_phys(page)  0x1000 ?
+   (page_to_phys(page) | 0x8000) : 
page_to_phys(page);
+#else
return page_to_phys(page) | 0x8000;
+#endif
 }
 
 static inline unsigned long plat_dma_addr_to_phys(struct device *dev,
dma_addr_t dma_addr)
 {
-#if defined(CONFIG_CPU_LOONGSON2F)  defined(CONFIG_64BIT)
+#if defined(CONFIG_64BIT)
+#if defined(CONFIG_CPU_LOONGSON3)
+   return (dma_addr  0x9000  dma_addr = 0x8000) ?
+   (dma_addr  0x0fff) : dma_addr;
+#elif defined(CONFIG_CPU_LOONGSON2F)
return (dma_addr  0x8fff) ? dma_addr : (dma_addr  0x0fff);
+#endif /* CONFIG_CPU_LOONGSON3 */
 #else
return dma_addr  0x7fff;
-#endif
+#endif /* CONFIG_64BIT */
 }
 
 static inline void plat_unmap_dma_mem(struct device *dev, dma_addr_t dma_addr,
@@ -65,7 +82,11 @@ static inline int plat_dma_mapping_error(struct device *dev,
 
 static inline int plat_device_is_coherent(struct device *dev)
 {
+#ifdef CONFIG_DMA_NONCOHERENT
return 0;
+#else
+   return 1;
+#endif /* CONFIG_DMA_NONCOHERENT */
 }
 
 #endif /* __ASM_MACH_LOONGSON_DMA_COHERENCE_H */
diff --git a/arch/mips/loongson/common/Makefile 
b/arch/mips/loongson/common/Makefile
index e526488..3a26109 100644
--- a/arch/mips/loongson/common/Makefile
+++ b/arch/mips/loongson/common/Makefile
@@ -25,3 +25,8 @@ obj-$(CONFIG_CS5536) += cs5536/
 #
 
 obj-$(CONFIG_LOONGSON_SUSPEND) += pm.o
+
+#
+# Big Memory Support
+#
+obj-$(CONFIG_LOONGSON_BIGMEM) += dma-swiotlb.o
diff --git a/arch/mips/loongson/common/dma-swiotlb.c 
b/arch/mips/loongson/common/dma-swiotlb.c
new file mode 100644
index 000..b87a21e
--- /dev/null
+++ b/arch/mips/loongson/common/dma-swiotlb.c
@@ -0,0 +1,159 @@
+#include linux/mm.h
+#include linux/init.h
+#include linux/dma-mapping.h
+#include linux/scatterlist.h
+#include linux/swiotlb.h
+#include linux/bootmem.h
+
+#include asm/bootinfo.h
+#include dma-coherence.h
+
+static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
+   dma_addr_t *dma_handle, gfp_t gfp, struct 
dma_attrs *attrs)
+{
+   void *ret;
+
+   if (dma_alloc_from_coherent(dev, size, dma_handle, ret))
+   return ret;
+
+   /* ignore region specifiers */
+   gfp = ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
+
+#ifdef CONFIG_ZONE_DMA
+   if