arm: msm: Add MSM IOMMU support.
Subsystems (3)
PATCH 1/1 arm: msm: Add MSM IOMMU support.
Add support for the IOMMUs found on the upcoming Qualcomm MSM8x60 chips. These IOMMUs allow virtualization of the address space used by most of the multimedia cores on these chips. The driver implements the kernel's IOMMU API.
--- arch/arm/mach-msm/include/mach/iommu.h | 105 ++ arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h | 1848 ++++++++++++++++++++++++ arch/arm/mach-msm/iommu.c | 620 ++++++++ 3 files changed, 2573 insertions(+), 0 deletions(-) create mode 100644 arch/arm/mach-msm/include/mach/iommu.h create mode 100644 arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h create mode 100644 arch/arm/mach-msm/iommu.c
arch/arm/mach-msm/include/mach/iommu.h new file +105 -0
@@ -0,0 +1,105 @@ +/* Copyright (c) 2010, Code Aurora Forum. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 and +* only version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +* 02110-1301, USA. +*/ + +#ifndef MSM_IOMMU_H +#define MSM_IOMMU_H + +#include <linux/interrupt.h> + +/* Maximum number of Machine IDs that we are allowing to be mapped to the same +* context bank. The number of MIDs mapped to the same CB does not affect +* performance, but there is a practical limit on how many distinct MIDs may +* be present. These mappings are typically determined at design time and are +* not expected to change at run time. +*/ +#define MAX_NUM_MIDS 16 + +/** +* struct msm_iommu_dev - a single IOMMU hardware instance +* name Human-readable name given to this IOMMU HW instance +* clk Name of the AXI clock used for the config space of this IOMMU +* clk_rate Rate to set for the AXI clock. 0 means don't set a rate +* num_ctx Number of context banks on this IOMMU device +* +*/ +struct msm_iommu_dev { +char *name; +char *clk; +unsigned long clk_rate; +unsigned int num_ctx; +}; + +/** +* struct msm_iommu_ctx_dev - an IOMMU context bank instance +* name Human-readable name given to this context bank +* num Index of this context bank within the hardware +* mids List of Machine IDs that are to be mapped into this context +* bank, terminated by -1. The MID is a set of signals on the +* AXI bus that identifies the function associated with a specific +* memory request. (See ARM spec). +*/ +struct msm_iommu_ctx_dev { +char *name; +int num; +int mids[MAX_NUM_MIDS ]; +}; + + +/** +* struct msm_iommu_drvdata - A single IOMMU hardware instance +* @base: IOMMU config port base address (VA) +* @irq: Interrupt number +* +* A msm_iommu_drvdata holds the global driver data about a single piece +* of an IOMMU hardware instance. +*/ +struct msm_iommu_drvdata { +unsigned long base; +int irq; +}; + +/** +* struct msm_iommu_ctx_drvdata - an IOMMU context bank instance +* @num: Hardware context number of this context +* @pdev: Platform device associated wit this HW instance +* @attached_elm: List element for domains to track which devices are +* attached to them +* +* A msm_iommu_ctx_drvdata holds the driver data for a single context bank +* within each IOMMU hardware instance +*/ +struct msm_iommu_ctx_drvdata { +int num; +struct platform_device *pdev; +struct list_head attached_elm; +}; + +/* +* Look up an IOMMU context device by its context name. NULL if none found. +* Useful for testing and drivers that do not yet fully have IOMMU stuff in +* their platform devices. +*/ +struct device *msm_iommu_get_ctx (char *ctx_name); + +/* +* Interrupt handler for the IOMMU context fault interrupt. Hooking the +* interrupt is not supported in the API yet, but this will print an error +* message and dump useful IOMMU registers. +*/ +irqreturn_t msm_iommu_fault_handler (int irq,void *dev_id); +int v7_flush_kern_cache_all (void ); +#endif
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h new file +1848 -0
1,848 lines not on this page · raw message
arch/arm/mach-msm/iommu.c new file +620 -0
620 lines not on this page · raw message
--
1.7.0.2
Sent by an employee of the Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.
arch/arm/mach-msm/include/mach/iommu.h line 40
Quoted 10 lines · Stepan Moskovchenko
arch/arm/mach-msm/include/mach/iommu.h new file +10
-0+/** +* struct msm_iommu_dev - a single IOMMU hardware instance +* name Human-readable name given to this IOMMU HW instance +* clk Name of the AXI clock used for the config space of this IOMMU +* clk_rate Rate to set for the AXI clock. 0 means don't set a rate +* num_ctx Number of context banks on this IOMMU device +* +*/ +struct msm_iommu_dev { +char *name;
const?
arch/arm/mach-msm/include/mach/iommu.h line 41
Quoted 1 line
arch/arm/mach-msm/include/mach/iommu.h new file +1
-0+char *clk;
const? Any reason you can't have some kind of struct device instead?
Quoted 5 lines
arch/arm/mach-msm/include/mach/iommu.h new file +5
-0+unsigned long clk_rate; +unsigned int num_ctx; +}; + +/**
arch/arm/mach-msm/include/mach/iommu.h line 56
Quoted 10 lines
arch/arm/mach-msm/include/mach/iommu.h new file +10
-0+* struct msm_iommu_ctx_dev - an IOMMU context bank instance +* name Human-readable name given to this context bank +* num Index of this context bank within the hardware +* mids List of Machine IDs that are to be mapped into this context +* bank, terminated by -1. The MID is a set of signals on the +* AXI bus that identifies the function associated with a specific +* memory request. (See ARM spec). +*/ +struct msm_iommu_ctx_dev { +char *name;
const?
Quoted 4 lines
arch/arm/mach-msm/include/mach/iommu.h new file +4
-0+int num; +int mids[MAX_NUM_MIDS ]; +}; +
...
Quoted 1 line
+int
v7_flush_kern_cache_all(void);
No - don't use internal functions directly. Use flush_cache_all() instead.
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h line 24
Quoted 7 lines
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h new file +7
-0+#ifndef __ARCH_ARM_MACH_MSM_IOMMU_HW_8XXX_H +#define __ARCH_ARM_MACH_MSM_IOMMU_HW_8XXX_H + +#define CTX_SHIFT 12 + +#define GET_GLOBAL_REG (reg, base) (readl((base)|(reg))) +#define GET_CTX_REG (reg, base, ctx) (readl((base)|(reg)|((ctx)<<CTX_SHIFT)))
This doesn't work if 'base' is correctly typed. Use addition please. Spacces around operators please.
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h line 29
Quoted 5 lines
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h new file +5
-0+ +#define SET_GLOBAL_REG (reg, base, val) (writel((val), (base) | (reg))) + +#define SET_CTX_REG (reg, base, ctx, val) \ + (writel((val), (base) | (reg) | ((ctx)<<CTX_SHIFT)))
Addition.
Quoted 12 lines
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h new file +12
-0+ +/* Wrappers for numbered registers */ +#define SET_GLOBAL_REG_N (b, n, r, v) SET_GLOBAL_REG(b, ((r)|(n<<2)), (v)) +#define GET_GLOBAL_REG_N (b, n, r) GET_GLOBAL_REG(b, ((r)|(n<<2))) + +/* Field wrappers */ +#define GET_GLOBAL_FIELD (b, r,F ) GET_FIELD(((b)|(r)), F##_MASK, F##_SHIFT) +#define GET_CONTEXT_FIELD (b, c, r,F ) \ + GET_FIELD(((b)|(r)|((c)<<CTX_SHIFT)), F##_MASK, F##_SHIFT) + +#define SET_GLOBAL_FIELD (b, r,F , v) \ + SET_FIELD(((b)|(r)), F##_MASK, F##_SHIFT, (v))
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h line 51
Quoted 10 lines
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h new file +10
-0+#define SET_CONTEXT_FIELD (b, c, r,F , v) \ + SET_FIELD(((b)|(r)|((c)<<CTX_SHIFT)), F##_MASK, F##_SHIFT, (v)) + +#define GET_FIELD (addr, mask, shift) ((readl(addr) >> (shift) & (mask))) + +#define SET_FIELD (addr, mask, shift, v) \ +do { \ + int t = readl(addr); mb(); \ + writel((t & ~((mask)<<(shift))) | (((v) & (mask)) << (shift)), addr);\ + mb();\
readl/writel have barriers now, so these probably aren't required. Please check.
...
arch/arm/mach-msm/iommu.c line 335
Quoted 6 lines
arch/arm/mach-msm/iommu.c new file +6
-0+if (len ==SZ_16M ) { +int i =0 ; +for (i =0 ; i <16 ; i++) + *(fl_pte+i) = (pa &0xFF000000 ) |PMD_SECT_SUPER | +PMD_SECT_AP_READ |PMD_SECT_AP_WRITE | +PMD_TYPE_SECT |PMD_SECT_S ;
Don't reuse the kernel's definitions for its own page tables for IOMMU page tables.
arch/arm/mach-msm/iommu.c line 341
Quoted 6 lines
arch/arm/mach-msm/iommu.c new file +6
-0+ } + +if (len ==SZ_1M ) + *fl_pte = (pa &0xFFF00000 ) |PMD_SECT_AP_READ | +PMD_SECT_AP_WRITE |PMD_TYPE_SECT | +PMD_SECT_S ;
Ditto.
arch/arm/mach-msm/iommu.c line 354
Quoted 13 lines
arch/arm/mach-msm/iommu.c new file +13
-0+ +/* Need a 2nd level table */ +if ((len ==SZ_4K || len ==SZ_64K ) && (*fl_pte) ==0 ) { +unsigned long *sl; + sl = (unsigned long *)__get_free_pages (GFP_KERNEL ,0 ); + +if (!sl) { +pr_err ("could not allocate second level table\n" ); + goto fail_nomem; + } + +memset (sl,0 ,SZ_4K ); + *fl_pte = ((((int )__pa (sl)) &0xFFFFFC00 ) |PMD_TYPE_TABLE );
Ditto.
arch/arm/mach-msm/iommu.c line 364
Quoted 10 lines
arch/arm/mach-msm/iommu.c new file +10
-0+ } + + sl_table = (unsigned long *)__va (((*fl_pte) &0xFFFFFC00 )); + sl_offset =SL_OFFSET (va); + sl_pte = sl_table + sl_offset; + + +if (len ==SZ_4K ) + *sl_pte = (pa &0xFFFFF000 ) |PTE_EXT_AP0 |PTE_EXT_AP1 | +PTE_EXT_SHARED |PTE_TYPE_SMALL ;
Ditto.
arch/arm/mach-msm/iommu.c line 371
Quoted 7 lines
arch/arm/mach-msm/iommu.c new file +7
-0+ +if (len ==SZ_64K ) { +int i; + +for (i =0 ; i <16 ; i++) + *(sl_pte+i) = (pa &0xFFFF0000 ) |PTE_EXT_AP0 | +PTE_EXT_AP1 |PTE_EXT_SHARED |PTE_TYPE_LARGE ;
Ditto.
arch/arm/mach-msm/iommu.c line 376
Quoted 5 lines
arch/arm/mach-msm/iommu.c new file +5
-0+ } + +#ifndef CONFIG_IOMMU_PGTABLES_L2 +v7_flush_kern_cache_all (); +#endif
What has L2 got to do with whether you flush the L1 cache?
Quoted 154 lines · Stepan Moskovchenko
arch/arm/mach-msm/include/mach/iommu.h line 40
Quoted 10 lines
arch/arm/mach-msm/include/mach/iommu.h new file +10
-0+/** +* struct msm_iommu_dev - a single IOMMU hardware instance +* name Human-readable name given to this IOMMU HW instance +* clk Name of the AXI clock used for the config space of this IOMMU +* clk_rate Rate to set for the AXI clock. 0 means don't set a rate +* num_ctx Number of context banks on this IOMMU device +* +*/ +struct msm_iommu_dev { +char *name;const?
arch/arm/mach-msm/include/mach/iommu.h line 41
Quoted 1 line
arch/arm/mach-msm/include/mach/iommu.h new file +1
-0+char *clk;const? Any reason you can't have some kind of struct device instead?
Quoted 5 lines
arch/arm/mach-msm/include/mach/iommu.h new file +5
-0+unsigned long clk_rate; +unsigned int num_ctx; +}; + +/** arch/arm/mach-msm/include/mach/iommu.h line 56
Quoted 10 lines
arch/arm/mach-msm/include/mach/iommu.h new file +10
-0+* struct msm_iommu_ctx_dev - an IOMMU context bank instance +* name Human-readable name given to this context bank +* num Index of this context bank within the hardware +* mids List of Machine IDs that are to be mapped into this context +* bank, terminated by -1. The MID is a set of signals on the +* AXI bus that identifies the function associated with a specific +* memory request. (See ARM spec). +*/ +struct msm_iommu_ctx_dev { +char *name;const?
Quoted 4 lines
arch/arm/mach-msm/include/mach/iommu.h new file +4
-0+int num; +int mids[MAX_NUM_MIDS ]; +}; +...
Quoted 1 line
+int
v7_flush_kern_cache_all(void);No - don't use internal functions directly. Use
flush_cache_all()instead.Quoted 8 lines
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h new file +7
-0+#ifndef __ARCH_ARM_MACH_MSM_IOMMU_HW_8XXX_H +#define __ARCH_ARM_MACH_MSM_IOMMU_HW_8XXX_H + +#define CTX_SHIFT 12 + +#define GET_GLOBAL_REG (reg, base) (readl((base)|(reg))) +#define GET_CTX_REG(reg, base,ctx) (readl((base)|(reg)|((ctx)<<CTX_SHIFT)))
This doesn't work if 'base' is correctly typed. Use addition please. Spacces around operators please.
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h line 29
Quoted 5 lines
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h new file +5
-0+ +#define SET_GLOBAL_REG (reg, base, val) (writel((val), (base) | (reg))) + +#define SET_CTX_REG (reg, base, ctx, val) \ + (writel((val), (base) | (reg) | ((ctx)<<CTX_SHIFT)))Addition.
Quoted 19 lines
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h new file +3
-0+ +/* Wrappers for numbered registers */ +#define SET_GLOBAL_REG_N (b, n, r, v) SET_GLOBAL_REG(b, ((r)|(n<<2)),(v))
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h new file +4
-0+#define GET_GLOBAL_REG_N (b, n, r) GET_GLOBAL_REG(b, ((r)|(n<<2))) + +/* Field wrappers */ +#define GET_GLOBAL_FIELD (b, r,F ) GET_FIELD(((b)|(r)), F##_MASK,F##_SHIFT)
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h new file +9
-0+#define GET_CONTEXT_FIELD (b, c, r,F ) \ + GET_FIELD(((b)|(r)|((c)<<CTX_SHIFT)), F##_MASK, F##_SHIFT) + +#define SET_GLOBAL_FIELD (b, r,F , v) \ + SET_FIELD(((b)|(r)), F##_MASK, F##_SHIFT, (v)) +#define SET_CONTEXT_FIELD (b, c, r,F , v) \ + SET_FIELD(((b)|(r)|((c)<<CTX_SHIFT)), F##_MASK, F##_SHIFT, (v)) + +#define GET_FIELD (addr, mask, shift) ((readl(addr) >> (shift) &(mask)))
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h line 51
Quoted 6 lines
arch/arm/mach-msm/include/mach/iommu_hw-8xxx.h new file +6
-0+ +#define SET_FIELD (addr, mask, shift, v) \ +do { \ + int t = readl(addr); mb(); \ + writel((t & ~((mask)<<(shift))) | (((v) & (mask)) << (shift)), addr);\ + mb();\readl/writel have barriers now, so these probably aren't required. Please check.
...
arch/arm/mach-msm/iommu.c line 335
Quoted 6 lines
arch/arm/mach-msm/iommu.c new file +6
-0+if (len ==SZ_16M ) { +int i =0 ; +for (i =0 ; i <16 ; i++) + *(fl_pte+i) = (pa &0xFF000000 ) |PMD_SECT_SUPER | +PMD_SECT_AP_READ |PMD_SECT_AP_WRITE | +PMD_TYPE_SECT |PMD_SECT_S ;Don't reuse the kernel's definitions for its own page tables for IOMMU page tables.
arch/arm/mach-msm/iommu.c line 341
Quoted 6 lines
arch/arm/mach-msm/iommu.c new file +6
-0+ } + +if (len ==SZ_1M ) + *fl_pte = (pa &0xFFF00000 ) |PMD_SECT_AP_READ | +PMD_SECT_AP_WRITE |PMD_TYPE_SECT | +PMD_SECT_S ;Ditto.
arch/arm/mach-msm/iommu.c line 354
Quoted 13 lines
arch/arm/mach-msm/iommu.c new file +13
-0+ +/* Need a 2nd level table */ +if ((len ==SZ_4K || len ==SZ_64K ) && (*fl_pte) ==0 ) { +unsigned long *sl; + sl = (unsigned long *)__get_free_pages (GFP_KERNEL ,0 ); + +if (!sl) { +pr_err ("could not allocate second level table\n" ); + goto fail_nomem; + } + +memset (sl,0 ,SZ_4K ); + *fl_pte = ((((int )__pa (sl)) &0xFFFFFC00 ) |PMD_TYPE_TABLE );Ditto.
arch/arm/mach-msm/iommu.c line 364
Quoted 10 lines
arch/arm/mach-msm/iommu.c new file +10
-0+ } + + sl_table = (unsigned long *)__va (((*fl_pte) &0xFFFFFC00 )); + sl_offset =SL_OFFSET (va); + sl_pte = sl_table + sl_offset; + + +if (len ==SZ_4K ) + *sl_pte = (pa &0xFFFFF000 ) |PTE_EXT_AP0 |PTE_EXT_AP1 | +PTE_EXT_SHARED |PTE_TYPE_SMALL ;Ditto.
arch/arm/mach-msm/iommu.c line 371
Quoted 7 lines
arch/arm/mach-msm/iommu.c new file +7
-0+ +if (len ==SZ_64K ) { +int i; + +for (i =0 ; i <16 ; i++) + *(sl_pte+i) = (pa &0xFFFF0000 ) |PTE_EXT_AP0 | +PTE_EXT_AP1 |PTE_EXT_SHARED |PTE_TYPE_LARGE ;Ditto.
Quoted 7 lines
arch/arm/mach-msm/iommu.c line 376
Quoted 5 lines
arch/arm/mach-msm/iommu.c new file +5
-0+ } + +#ifndef CONFIG_IOMMU_PGTABLES_L2 +v7_flush_kern_cache_all (); +#endif What has L2 got to do with whether you flush the L1 cache?
Hello
Thank you for your comments. I will apply the fixes you have mentioned. I am flushing the cache when I update the page table because the page table lives in regular RAM that is mapped as cacheable on the CPU side, and in the default configuration the IOMMU reads the page table from RAM. So, I have put in the flush call to give the IOMMU a coherent view of the page tables. I realize it is more efficient to just flush that specific part of L2, but this is just the basic version of the driver and that optimization will be eventually put in. The hardware is actually capable of reading the IOMMU page table straight out of the L2 cache, but I haven't figured out how much control the API should give over this feature... initially I was thinking of a Kconfig "sub-option" for it but maybe more fine-grained control is in order, depending on the access pattern of each IOMMU. I can add this functionality in a subsequent patch, once the basic driver is in. If flushing the cache in this manner is not the right way to handle IOMMU page tables, can you please suggest what would be more appropriate?
What did you mean by:
Quoted 1 line
Any reason you can't have some kind of struct device instead?
Are you referring to the clock line? As far as I understand it, the kernel tracks the clocks by a string name, but I can look into it further. Only a few of the IOMMUs even have clocks that I need to control in this manner, and these are only accessed at init time. From what I remember, it is possible to "get" a clock device, but I am not sure these can be statically added to the struct like that.
I will make the changes you suggested and post another version soon.
Thanks
Steve
Quoted 4 lines · stepanm at codeaurora.org
Thank you for your comments. I will apply the fixes you have mentioned. I am flushing the cache when I update the page table because the page table lives in regular RAM that is mapped as cacheable on the CPU side, and in the default configuration the IOMMU reads the page table from RAM.
That's fine, but use the right function.
Quoted 5 lines
So, I
have put in the flush call to give the IOMMU a coherent view of the page tables. I realize it is more efficient to just flush that specific part of L2, but this is just the basic version of the driver and that optimization will be eventually put in.
Here, you're talking about L2 again, but your code only touches the L1 cache. I think you're confused about caching.
Quoted 5 lines
What did you mean by:
Quoted 1 line
Any reason you can't have some kind of struct device instead?
Are you referring to the clock line? As far as I understand it, the kernel tracks the clocks by a string name, but I can look into it further.
No.
/**
* clk_get - lookup and obtain a reference to a clock producer.
* @dev: device for clock "consumer"
* @id: clock comsumer ID
*
* Returns a struct clk corresponding to the clock producer, or
* valid IS_ERR() condition containing errno. The implementation
* uses @dev and @id to determine the clock consumer, and thereby
* the clock producer. (IOW, @id may be identical strings, but
* clk_get may return different clock producers depending on @dev.)
The intention is that 'id' is specific to each 'dev', and identifies the connection on 'dev' that you want the clock for. It's the _consumer's_ name, nothing to do with the clock producer.
There's also clk_get_sys() if you don't have a struct device.
Quoted 36 lines · Russell King - ARM Linux
Quoted 4 lines · stepanm at codeaurora.org
Thank you for your comments. I will apply the fixes you have mentioned. I am flushing the cache when I update the page table because the page table lives in regular RAM that is mapped as cacheable on the CPU side, and in the default configuration the IOMMU reads the page table from RAM.
That's fine, but use the right function.
Quoted 5 lines
So, I
have put in the flush call to give the IOMMU a coherent view of the page tables. I realize it is more efficient to just flush that specific part of L2, but this is just the basic version of the driver and that optimization will be eventually put in.Here, you're talking about L2 again, but your code only touches the L1 cache. I think you're confused about caching.
Quoted 4 lines
What did you mean by:
Quoted 1 line
Any reason you can't have some kind of struct device instead?
Are you referring to the clock line? As far as I understand it, the kernel tracks the clocks by a string name, but I can look into it further.
No.
/**
*clk_get- lookup and obtain a reference to a clock producer.
* @dev: device for clock "consumer"
* @id: clock comsumer ID
*
* Returns a struct clk corresponding to the clock producer, or
* validIS_ERR()condition containing errno. The implementation
* uses @dev and @id to determine the clock consumer, and thereby
* the clock producer. (IOW, @id may be identical strings, but
*clk_getmay return different clock producers depending on @dev.)The intention is that 'id' is specific to each 'dev', and identifies the connection on 'dev' that you want the clock for. It's the _consumer's_ name, nothing to do with the clock producer.
Quoted 5 lines
There's also
clk_get_sys()if you don't have a struct device. --
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Russell,
Thanks for the information. Getting the clock by the device as you suggest should be fine. I believe I have addressed your other comments as well. I've sent out a v2 patch. Please let me know what you think.
Thanks
Steve