xen: dom0 support for PCI access
Subsystems (5)
Hi Ingo,
This branch allows a Xen dom0 kernel to access PCI devices properly. Mostly this involves making sure that memory being used for DMA is both logically contigious in the kernel's pseudo-physical memory and physically contigious in machine memory.
Again, this is pretty much unchanged from the last time I posted it.
The following changes since commit 0c96e43850feb7c7c4a4950f24533491fbd63b5a:
Jeremy Fitzhardinge (1):
xen: checkpatch cleanups
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/jeremy/xen.git xen-tip/dom0/pci
Alex Nixon (7):
xen: Don't disable the I/O space
xen: Allow unprivileged Xen domains to create iomap pages
Xen: Rename the balloon lock
xen: Add xen_create_contiguous_region
x86/PCI: Clean up pci_cache_line_size
x86/PCI: Enable scanning of all pci functions Xen/x86/PCI: Add support for the Xen PCI subsytem
Jeremy Fitzhardinge (4):
xen/swiotlb: use dma_alloc_from_coherent to get device coherent memory x86/pci: make sure _PAGE_IOMAP it set on pci mappings xen/pci: clean up Kconfig a bit
xen: checkpatch cleanups
arch/x86/Kconfig | 4 + arch/x86/include/asm/io.h | 2 + arch/x86/include/asm/pci.h | 8 +- arch/x86/include/asm/pci_x86.h | 2 + arch/x86/include/asm/xen/iommu.h | 12 ++ arch/x86/kernel/pci-dma.c | 3 + arch/x86/pci/Makefile | 1 + arch/x86/pci/common.c | 18 ++- arch/x86/pci/i386.c | 3 + arch/x86/pci/init.c | 6 + arch/x86/pci/xen.c | 51 +++++++ arch/x86/xen/Kconfig | 2 + arch/x86/xen/enlighten.c | 6 +- arch/x86/xen/mmu.c | 225 ++++++++++++++++++++++++++++++- arch/x86/xen/setup.c | 3 - drivers/pci/Makefile | 2 + drivers/pci/xen-iommu.c | 279 ++++++++++++++++++++++++++++++++++++++ drivers/xen/balloon.c | 15 +-- include/xen/interface/memory.h | 50 +++++++ include/xen/xen-ops.h | 6 + 20 files changed, 671 insertions(+), 27 deletions(-) create mode 100644 arch/x86/include/asm/xen/iommu.h create mode 100644 arch/x86/pci/xen.c create mode 100644 drivers/pci/xen-iommu.c
Thanks,
J
PATCH 1/11 xen: Don't disable the I/O space
From: Alex Nixon <[email protected]>
Impact: prep for pcifront
If a guest domain wants to access PCI devices through the frontend driver (coming later in the patch series), it will need access to the I/O space.
--- arch/x86/xen/setup.c | 3 --- 1 files changed, 0 insertions(+), 3 deletions(-)
arch/x86/xen/setup.c +0 -3
@@ -230,8 +230,5 @@ void __init xen_arch_setup(void) pm_idle = xen_idle; -if (!xen_initial_domain ()) -paravirt_disable_iospace (); -fiddle_vdso (); }
Signature · 1 line
1.6.0.6
PATCH 2/11 xen: Allow unprivileged Xen domains to create iomap pages
In reply to Jeremy FitzhardingeFrom: Alex Nixon <[email protected]>
Impact: prep for pcifront
PV DomU domains are allowed to map hardware MFNs for PCI passthrough, but are not generally allowed to map raw machine pages. In particular, various pieces of code try to map DMI and ACPI tables in the ISA ROM range. We disallow _PAGE_IOMAP for those mappings, so that they are redirected to a set of local zeroed pages we reserve for that purpose.
--- arch/x86/xen/enlighten.c | 6 +++--- arch/x86/xen/mmu.c | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-)
arch/x86/xen/enlighten.c +3 -3
@@ -1000,11 +1000,11 @@ asmlinkage void __init xen_start_kernel(void)/* Prevent unwanted bits from being set in PTEs. */ __supported_pte_mask &= ~_PAGE_GLOBAL; -if (xen_initial_domain ()) - __supported_pte_mask |= _PAGE_IOMAP; -else +if (!xen_initial_domain ()) __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD); + __supported_pte_mask |= _PAGE_IOMAP; +#ifdef CONFIG_X86_64 /* Work out if we support NX */ check_efer ();
arch/x86/xen/mmu.c +15 -3
@@ -49,6 +49,7 @@#include <asm/mmu_context.h> #include <asm/setup.h> #include <asm/paravirt.h> +#include <asm/e820.h> #include <asm/linkage.h> #include <asm/xen/hypercall.h>
@@ -378,7 +379,7 @@ static bool xen_page_pinned(void *ptr)static bool xen_iomap_pte (pte_t pte) { -return xen_initial_domain () && (pte_flags (pte) & _PAGE_IOMAP); +return pte_flags (pte) & _PAGE_IOMAP; }static void xen_set_iomap_pte (pte_t *ptep,pte_t pteval)
@@ -580,10 +581,21 @@ PV_CALLEE_SAVE_REGS_THUNK(xen_pgd_val);pte_t xen_make_pte (pteval_t pte) { -if (unlikely (xen_initial_domain () && (pte & _PAGE_IOMAP))) +phys_addr_t addr = (pte &PTE_PFN_MASK ); + +/* +* Unprivileged domains are allowed to do IOMAPpings for +* PCI passthrough, but not map ISA space. The ISA +* mappings are just dummy local mappings to keep other +* parts of the kernel happy. +*/ +if (unlikely (pte & _PAGE_IOMAP) && + (xen_initial_domain () || addr >=ISA_END_ADDRESS )) { pte =iomap_pte (pte); -else + }else { + pte &= ~_PAGE_IOMAP; pte =pte_pfn_to_mfn (pte); + }return native_make_pte (pte); }
Signature · 1 line
1.6.0.6
PATCH 3/11 Xen: Rename the balloon lock
In reply to Jeremy FitzhardingeFrom: Alex Nixon <[email protected]>
Impact: cleanup
* xen_create_contiguous_region needs access to the balloon lock to ensure memory doesn't change under its feet, so expose the balloon lock
* Change the name of the lock to xen_reservation_lock, to imply it's now less-specific usage.
--- arch/x86/xen/mmu.c | 7 +++++++ drivers/xen/balloon.c | 15 ++++----------- include/xen/interface/memory.h | 8 ++++++++ 3 files changed, 19 insertions(+), 11 deletions(-)
arch/x86/xen/mmu.c +7 -0
@@ -67,6 +67,13 @@#define MMU_UPDATE_HISTO 30 +/* +* Protects atomic reservation decrease/increase against concurrent increases. +* Also protects non-atomic updates of current_pages and driver_pages, and +* balloon lists. +*/ +DEFINE_SPINLOCK (xen_reservation_lock); + #ifdef CONFIG_XEN_DEBUG_FS static struct {
drivers/xen/balloon.c +4 -11
@@ -84,13 +84,6 @@ static struct sys_device balloon_sysdev;static int register_balloon (struct sys_device *sysdev); -/* -* Protects atomic reservation decrease/increase against concurrent increases. -* Also protects non-atomic updates of current_pages and driver_pages, and -* balloon lists. -*/ -static DEFINE_SPINLOCK (balloon_lock); -static struct balloon_stats balloon_stats;/* We increase/decrease in batches which fit in a page */
@@ -209,7 +202,7 @@ static int increase_reservation(unsigned long nr_pages)if (nr_pages >ARRAY_SIZE (frame_list)) nr_pages =ARRAY_SIZE (frame_list); -spin_lock_irqsave (&balloon_lock, flags); +spin_lock_irqsave (&xen_reservation_lock, flags); page =balloon_first_page ();for (i =0 ; i < nr_pages; i++) {
@@ -267,7 +260,7 @@ static int increase_reservation(unsigned long nr_pages) totalram_pages = balloon_stats.current_pages; out: -spin_unlock_irqrestore (&balloon_lock, flags); +spin_unlock_irqrestore (&xen_reservation_lock, flags);return 0 ; }
@@ -312,7 +305,7 @@ static int decrease_reservation(unsigned long nr_pages)kmap_flush_unused ();flush_tlb_all (); -spin_lock_irqsave (&balloon_lock, flags); +spin_lock_irqsave (&xen_reservation_lock, flags);/* No more mappings: invalidate P2M and add to balloon. */ for (i =0 ; i < nr_pages; i++) {
@@ -329,7 +322,7 @@ static int decrease_reservation(unsigned long nr_pages) balloon_stats.current_pages -= nr_pages; totalram_pages = balloon_stats.current_pages; -spin_unlock_irqrestore (&balloon_lock, flags); +spin_unlock_irqrestore (&xen_reservation_lock, flags);return need_sleep; }
include/xen/interface/memory.h +8 -0
@@ -9,6 +9,8 @@#ifndef __XEN_PUBLIC_MEMORY_H__#define __XEN_PUBLIC_MEMORY_H__ +#include <linux/spinlock.h> +/* * Increase or decrease the specified domain's memory reservation. Returns a * -ve errcode on failure, or the # extents successfully allocated or freed.
@@ -184,4 +186,10 @@ DEFINE_GUEST_HANDLE_STRUCT(xen_memory_map);*/ #define XENMEM_machine_memory_map 10 +/* +* Prevent the balloon driver from changing the memory reservation during a driver +* critical region. +*/ +extern spinlock_t xen_reservation_lock; +#endif /* __XEN_PUBLIC_MEMORY_H__ */
Signature · 1 line
1.6.0.6
Re: [Xen-devel] [PATCH 03/11] Xen: Rename the balloon lock
Hi. Please please don't move the arch common variable into x86 specific file. It causes link error on ia64.
Before I sent the patches to fix it twice or so,
but it seems to be dropped.
Quoted 114 lines · Jeremy Fitzhardinge
From: Alex Nixon <[email protected]>
Impact: cleanup
*
xen_create_contiguous_regionneeds access to the balloon lock to ensure memory doesn't change under its feet, so expose the balloon lock
* Change the name of the lock toxen_reservation_lock, to imply it's now less-specific usage.Signed-off-by Alex Nixon <[email protected]>Signed-off-by Jeremy Fitzhardinge <[email protected]>--- arch/x86/xen/mmu.c | 7 +++++++ drivers/xen/balloon.c | 15 ++++----------- include/xen/interface/memory.h | 8 ++++++++ 3 files changed, 19 insertions(+), 11 deletions(-)arch/x86/xen/mmu.c +7
-0@@ -67,6 +67,13 @@#define MMU_UPDATE_HISTO 30 +/* +* Protects atomic reservation decrease/increase against concurrent increases. +* Also protects non-atomic updates of current_pages and driver_pages, and +* balloon lists. +*/ +DEFINE_SPINLOCK (xen_reservation_lock); + #ifdef CONFIG_XEN_DEBUG_FS static struct {drivers/xen/balloon.c +4
-11@@ -84,13 +84,6 @@ static struct sys_device balloon_sysdev;static int register_balloon (struct sys_device *sysdev); -/* -* Protects atomic reservation decrease/increase against concurrent increases. -* Also protects non-atomic updates of current_pages and driver_pages, and -* balloon lists. -*/ -static DEFINE_SPINLOCK (balloon_lock); -static struct balloon_stats balloon_stats;/* We increase/decrease in batches which fit in a page */ @@ -209,7 +202,7 @@ static int increase_reservation(unsigned long nr_pages)if (nr_pages >ARRAY_SIZE (frame_list)) nr_pages =ARRAY_SIZE (frame_list); -spin_lock_irqsave (&balloon_lock, flags); +spin_lock_irqsave (&xen_reservation_lock, flags); page =balloon_first_page ();for (i =0 ; i < nr_pages; i++) {@@ -267,7 +260,7 @@ static int increase_reservation(unsigned long nr_pages) totalram_pages = balloon_stats.current_pages; out: -spin_unlock_irqrestore (&balloon_lock, flags); +spin_unlock_irqrestore (&xen_reservation_lock, flags);return 0 ; }@@ -312,7 +305,7 @@ static int decrease_reservation(unsigned long nr_pages)kmap_flush_unused ();flush_tlb_all (); -spin_lock_irqsave (&balloon_lock, flags); +spin_lock_irqsave (&xen_reservation_lock, flags);/* No more mappings: invalidate P2M and add to balloon. */ for (i =0 ; i < nr_pages; i++) {@@ -329,7 +322,7 @@ static int decrease_reservation(unsigned long nr_pages) balloon_stats.current_pages -= nr_pages; totalram_pages = balloon_stats.current_pages; -spin_unlock_irqrestore (&balloon_lock, flags); +spin_unlock_irqrestore (&xen_reservation_lock, flags);return need_sleep; }include/xen/interface/memory.h +8
-0@@ -9,6 +9,8 @@#ifndef __XEN_PUBLIC_MEMORY_H__#define __XEN_PUBLIC_MEMORY_H__ +#include <linux/spinlock.h> +/* * Increase or decrease the specified domain's memory reservation. Returns a * -ve errcode on failure, or the # extents successfully allocated or freed. @@ -184,4 +186,10 @@ DEFINE_GUEST_HANDLE_STRUCT(xen_memory_map);*/ #define XENMEM_machine_memory_map 10 +/* +* Prevent the balloon driver from changing the memory reservation during a driver +* critical region. +*/ +extern spinlock_t xen_reservation_lock; +#endif /* __XEN_PUBLIC_MEMORY_H__ */
Signature · 1 line
yamahata
Re: [Xen-devel] [PATCH 03/11] Xen: Rename the balloon lock
Quoted 6 lines · Isaku Yamahata
Hi. Please please don't move the arch common variable into x86 specific file. It causes link error on ia64.
Before I sent the patches to fix it twice or so,
but it seems to be dropped.
Sorry, I can't seem to find them. Could you resend?
I guess there's the larger issue of whether the xen_create_contiguous_region() code is also useful on ia64. It seems to me the options are:
* it is, so we should move the code somewhere common (along with the lock)
* it isn't so ia64 will have its own version (in which case it can define its own version of the lock without much impact)
* or we could move the lock anyway, but there's no very obvious place to put it
J
Re: [Xen-devel] [PATCH 03/11] Xen: Rename the balloon lock
Quoted 14 lines · Jeremy Fitzhardinge
Quoted 6 lines · Isaku Yamahata
Hi. Please please don't move the arch common variable into x86 specific file. It causes link error on ia64.
Before I sent the patches to fix it twice or so,
but it seems to be dropped.Sorry, I can't seem to find them. Could you resend?
I guess there's the larger issue of whether the
xen_create_contiguous_region()code is also useful on ia64. It seems to me the options are:
Quoted 7 lines
* it is, so we should move the code somewhere common (along with the lock)
* it isn't so ia64 will have its own version (in which case it can define its own version of the lock without much impact)
* or we could move the lock anyway, but there's no very obvious place to put it
I see now the issue. My choice at the moment is the last option. The copyright notice was taken from drivers/xen/balloon.c. I don't have strong opinion about the place, so feel free to move it as long as it is linked on IA64.
Non-pv_ops ia64 xen kernel has its own xen_create_contiguous_region(). But I suppose the code can be consolidated if you accept sprinkle some if (xen_feature(XENFEAT_auto_translated_physmap)), so I'm willing to work on the first option once the x86 pv_ops dom0 is stabilized/committed.
thanks,
commit 2017dc931ca350eaad9cacd97e74b9c2eb4990b9
Author: Isaku Yamahata <[email protected]> Date: Tue May 12 11:22:05 2009 +0900
Impact: link fix on ia64
Move the variable, xen_reservation_lock, to common place. IA64 also needs it.
arch/x86/xen/mmu.c +0 -7
@@ -65,13 +65,6 @@#define MMU_UPDATE_HISTO 30 -/* -* Protects atomic reservation decrease/increase against concurrent increases. -* Also protects non-atomic updates of current_pages and driver_pages, and -* balloon lists. -*/ -DEFINE_SPINLOCK (xen_reservation_lock); - #ifdef CONFIG_XEN_DEBUG_FS static struct {
drivers/xen/Makefile +1 -1
@@ -1,4 +1,4 @@ -obj-y += grant-table.o features.o events.o manage.o +obj-y += grant-table.o features.o events.o manage.o reslock.o obj-y += xenbus/ obj-$(CONFIG_HOTPLUG_CPU) += cpu_hotplug.o
drivers/xen/reslock.c new file +36 -0
@@ -0,0 +1,36 @@ +/****************************************************************************** +* revlock.c +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License version 2 +* as published by the Free Software Foundation; or, when distributed +* separately from the Linux kernel or incorporated into other +* software packages, subject to the following license: +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this source file (the "Software"), to deal in the Software without +* restriction, including without limitation the rights to use, copy, modify, +* merge, publish, distribute, sublicense, and/or sell copies of the Software, +* and to permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +*/ + +#include <linux/spinlock.h> + +/* +* Protects atomic reservation decrease/increase against concurrent increases. +* Also protects non-atomic updates of current_pages and driver_pages, and +* balloon lists. +*/ +DEFINE_SPINLOCK (xen_reservation_lock);
Signature · 1 line
yamahata
PATCH 4/11 xen: Add xen_create_contiguous_region
In reply to Jeremy FitzhardingeFrom: Alex Nixon <[email protected]>
Impact: add Xen-internal API
A memory region must be physically contiguous in order to be accessed through DMA. This patch adds xen_create_contiguous_region, which ensures a region of contiguous virtual memory is also physically contiguous.
Based on Stephen Tweedie's port of the 2.6.18-xen version.
Remove contiguous_bitmap[] as it's no longer needed.
Ported from linux-2.6.18-xen.hg 707:e410857fd83c
--- arch/x86/xen/mmu.c | 200 ++++++++++++++++++++++++++++++++++++++++ include/xen/interface/memory.h | 42 +++++++++ include/xen/xen-ops.h | 6 + 3 files changed, 248 insertions(+), 0 deletions(-)
arch/x86/xen/mmu.c +200 -0
@@ -51,6 +51,7 @@#include <asm/paravirt.h> #include <asm/e820.h> #include <asm/linkage.h> +#include <asm/page.h> #include <asm/xen/hypercall.h> #include <asm/xen/hypervisor.h>
@@ -2087,6 +2088,205 @@ const struct pv_mmu_ops xen_mmu_ops __initdata = { }; +/* Protected by xen_reservation_lock. */ +#define MAX_CONTIG_ORDER 9/* 2MB */ +static unsigned long discontig_frames[1 <<MAX_CONTIG_ORDER ]; + +#define VOID_PTE (mfn_pte(0, __pgprot(0))) +static void xen_zap_pfn_range (unsigned long vaddr,unsigned int order, +unsigned long *in_frames, +unsigned long *out_frames) +{ +int i; +struct multicall_space mcs; + +xen_mc_batch (); +for (i =0 ; i < (1UL <<order); i++, vaddr +=PAGE_SIZE ) { + mcs =__xen_mc_entry (0 ); + +if (in_frames) + in_frames[i] =virt_to_mfn (vaddr); + +MULTI_update_va_mapping (mcs.mc, vaddr,VOID_PTE ,0 ); +set_phys_to_machine (virt_to_pfn (vaddr),INVALID_P2M_ENTRY ); + +if (out_frames) + out_frames[i] =virt_to_pfn (vaddr); + } +xen_mc_issue (0 ); +} + +/* +* Update the pfn-to-mfn mappings for a virtual address range, either to +* point to an array of mfns, or contiguously from a single starting +* mfn. +*/ +static void xen_remap_exchanged_ptes (unsigned long vaddr,int order, +unsigned long *mfns, +unsigned long first_mfn) +{ +unsigned i, limit; +unsigned long mfn; + +xen_mc_batch (); + + limit =1u << order; +for (i =0 ; i < limit; i++, vaddr +=PAGE_SIZE ) { +struct multicall_space mcs; +unsigned flags; + + mcs =__xen_mc_entry (0 ); +if (mfns) + mfn = mfns[i]; +else + mfn = first_mfn + i; + +if (i < (limit -1 )) + flags =0 ; +else { +if (order ==0 ) + flags =UVMF_INVLPG |UVMF_ALL ; +else + flags =UVMF_TLB_FLUSH |UVMF_ALL ; + } + +MULTI_update_va_mapping (mcs.mc, vaddr, +mfn_pte (mfn,PAGE_KERNEL ), flags); + +set_phys_to_machine (virt_to_pfn (vaddr), mfn); + } + +xen_mc_issue (0 ); +} + +/* +* Perform the hypercall to exchange a region of our pfns to point to +* memory with the required contiguous alignment. Takes the pfns as +* input, and populates mfns as output. +* +* Returns a success code indicating whether the hypervisor was able to +* satisfy the request or not. +*/ +static int xen_exchange_memory (unsigned long extents_in,unsigned int order_in, +unsigned long *pfns_in, +unsigned long extents_out,unsigned int order_out, +unsigned long *mfns_out, +unsigned int address_bits) +{ +long rc; +int success; + +struct xen_memory_exchange exchange = { + .in = { + .nr_extents = extents_in, + .extent_order = order_in, + .extent_start = pfns_in, + .domid =DOMID_SELF + }, + .out = { + .nr_extents = extents_out, + .extent_order = order_out, + .extent_start = mfns_out, + .address_bits = address_bits, + .domid =DOMID_SELF + } + }; + +BUG_ON (extents_in << order_in != extents_out << order_out); + + rc =HYPERVISOR_memory_op (XENMEM_exchange, &exchange); + success = (exchange.nr_exchanged == extents_in); + +BUG_ON (!success && ((exchange.nr_exchanged !=0 ) || (rc ==0 ))); +BUG_ON (success && (rc !=0 )); + +return success; +} + +int xen_create_contiguous_region (unsigned long vstart,unsigned int order, +unsigned int address_bits) +{ +unsigned long *in_frames = discontig_frames, out_frame; +unsigned long flags; +int success; + +/* +* Currently an auto-translated guest will not perform I/O, nor will +* it require PAE page directories below 4GB. Therefore any calls to +* this function are redundant and can be ignored. +*/ + +if (xen_feature (XENFEAT_auto_translated_physmap)) +return 0 ; + +if (unlikely (order >MAX_CONTIG_ORDER )) +return -ENOMEM ; + +memset ((void *) vstart,0 ,PAGE_SIZE << order); + +vm_unmap_aliases (); + +spin_lock_irqsave (&xen_reservation_lock, flags); + +/* 1. Zap current PTEs, remembering MFNs. */ +xen_zap_pfn_range (vstart, order, in_frames,NULL ); + +/* 2. Get a new contiguous memory extent. */ + out_frame =virt_to_pfn (vstart); + success =xen_exchange_memory (1UL << order,0 , in_frames, +1 , order, &out_frame, + address_bits); + +/* 3. Map the new extent in place of old pages. */ +if (success) +xen_remap_exchanged_ptes (vstart, order,NULL , out_frame); +else +xen_remap_exchanged_ptes (vstart, order, in_frames,0 ); + +spin_unlock_irqrestore (&xen_reservation_lock, flags); + +return success ?0 : -ENOMEM ; +} +EXPORT_SYMBOL_GPL (xen_create_contiguous_region); + +void xen_destroy_contiguous_region (unsigned long vstart,unsigned int order) +{ +unsigned long *out_frames = discontig_frames, in_frame; +unsigned long flags; +int success; + +if (xen_feature (XENFEAT_auto_translated_physmap)) +return ; + +if (unlikely (order >MAX_CONTIG_ORDER )) +return ; + +memset ((void *) vstart,0 ,PAGE_SIZE << order); + +vm_unmap_aliases (); + +spin_lock_irqsave (&xen_reservation_lock, flags); + +/* 1. Find start MFN of contiguous extent. */ + in_frame =virt_to_mfn (vstart); + +/* 2. Zap current PTEs. */ +xen_zap_pfn_range (vstart, order,NULL , out_frames); + +/* 3. Do the exchange for non-contiguous MFNs. */ + success =xen_exchange_memory (1 , order, &in_frame,1UL << order, +0 , out_frames,0 ); + +/* 4. Map new pages in place of old pages. */ +if (success) +xen_remap_exchanged_ptes (vstart, order, out_frames,0 ); +else +xen_remap_exchanged_ptes (vstart, order,NULL , in_frame); + +spin_unlock_irqrestore (&xen_reservation_lock, flags); +} +EXPORT_SYMBOL_GPL (xen_destroy_contiguous_region); +#ifdef CONFIG_XEN_DEBUG_FS static struct dentry *d_mmu_debug;
include/xen/interface/memory.h +42 -0
48 lines not on this page · raw message
include/xen/xen-ops.h +6 -0
@@ -14,4 +14,10 @@ void xen_mm_unpin_all(void);void xen_timer_resume (void );void xen_arch_resume (void ); +extern unsigned long *xen_contiguous_bitmap; +int xen_create_contiguous_region (unsigned long vstart,unsigned int order, +unsigned int address_bits); + +void xen_destroy_contiguous_region (unsigned long vstart,unsigned int order); +#endif /* INCLUDE_XEN_OPS_H */
Signature · 1 line
1.6.0.6
PATCH 5/11 x86/PCI: Clean up pci_cache_line_size
In reply to Jeremy FitzhardingeFrom: Alex Nixon <[email protected]>
Impact: cleanup
Separate out x86 cache_line_size initialisation code into its own function (so it can be shared by Xen later in this patch series)
--- arch/x86/include/asm/pci_x86.h | 1 + arch/x86/pci/common.c | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-)
arch/x86/include/asm/pci_x86.h +1 -0
@@ -45,6 +45,7 @@ enum pci_bf_sort_state {extern unsigned int pcibios_max_latency;void pcibios_resource_survey (void ); +void pcibios_set_cache_line_size (void );/* pci-pc.c */
arch/x86/pci/common.c +11 -6
@@ -409,26 +409,31 @@ struct pci_bus * __devinit pcibios_scan_root(int busnum)extern u8 pci_cache_line_size; -int __init pcibios_init (void ) +void pcibios_set_cache_line_size (void ) {struct cpuinfo_x86 *c = &boot_cpu_data; -if (!raw_pci_ops) { -printk (KERN_WARNING "PCI: System does not support PCI\n" ); -return 0 ; - } -/* * Assume PCI cacheline size of 32 bytes for all x86s except K7/K8 * and P4. It's also good for 386/486s (which actually have 16) * as quite a few PCI devices do not support smaller values. */ + pci_cache_line_size =32 >>2 ;if (c->x86 >=6 && c->x86_vendor ==X86_VENDOR_AMD ) pci_cache_line_size =64 >>2 ;/* K7 & K8 */ else if (c->x86 >6 && c->x86_vendor ==X86_VENDOR_INTEL ) pci_cache_line_size =128 >>2 ;/* P4 */ +} + +int __init pcibios_init (void ) +{ +if (!raw_pci_ops) { +printk (KERN_WARNING "PCI: System does not support PCI\n" ); +return 0 ; + } +pcibios_set_cache_line_size ();pcibios_resource_survey ();if (pci_bf_sort >= pci_force_bf)
Signature · 1 line
1.6.0.6
PATCH 6/11 x86/PCI: Enable scanning of all pci functions
In reply to Jeremy FitzhardingeFrom: Alex Nixon <[email protected]>
Impact: prep for pcifront
Xen may want to enable scanning of all pci functions - if for example the device at function 0 is not passed through to the guest, but the device at function 1 is.
--- arch/x86/include/asm/pci.h | 8 +++++++- arch/x86/pci/common.c | 1 + 2 files changed, 8 insertions(+), 1 deletions(-)
arch/x86/include/asm/pci.h +7 -1
@@ -21,6 +21,7 @@ struct pci_sysdata {extern int pci_routeirq;extern int noioapicquirk;extern int noioapicreroute; +extern int pci_scan_all_fns;/* scan a bus after allocating a pci_sysdata for it */ extern struct pci_bus *pci_scan_bus_on_node(int busno, struct pci_ops *ops,
@@ -48,7 +49,11 @@ extern unsigned int pcibios_assign_all_busses(void);#else #define pcibios_assign_all_busses () 0#endif -#define pcibios_scan_all_fns (a, b) 0 + +static inline int pcibios_scan_all_fns (struct pci_bus *bus,int devfn) +{ +return pci_scan_all_fns; +}extern unsigned long pci_mem_start;#define PCIBIOS_MIN_IO 0x1000
@@ -130,6 +135,7 @@ extern void pci_iommu_alloc(void);/* generic pci stuff */ #include <asm-generic/pci.h> +#undef pcibios_scan_all_fns#ifdef CONFIG_NUMA /* Returns the node based on pci bus */
arch/x86/pci/common.c +1 -0
@@ -22,6 +22,7 @@ unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2 |unsigned int pci_early_dump_regs;static int pci_bf_sort;int pci_routeirq; +int pci_scan_all_fns =0 ;int noioapicquirk;#ifdef CONFIG_X86_REROUTE_FOR_BROKEN_BOOT_IRQS int noioapicreroute =0 ;
Signature · 1 line
1.6.0.6
PATCH 7/11 Xen/x86/PCI: Add support for the Xen PCI subsytem
In reply to Jeremy FitzhardingeFrom: Alex Nixon <[email protected]>
Impact: add core of Xen PCI support
On boot, the system will search to see if a Xen iommu/pci subsystem is available. If the kernel detects it's running in a domain rather than on bare hardware, this subsystem will be used. Otherwise, it falls back to using hardware as usual.
The frontend stub lives in arch/x86/pci-xen.c, alongside other sub-arch PCI init code (e.g. olpc.c)
(All subsequent fixes, API changes and swiotlb operations folded in.)
--- arch/x86/Kconfig | 4 + arch/x86/include/asm/pci_x86.h | 1 + arch/x86/include/asm/xen/iommu.h | 12 ++ arch/x86/kernel/pci-dma.c | 3 + arch/x86/pci/Makefile | 1 + arch/x86/pci/init.c | 6 + arch/x86/pci/xen.c | 52 +++++++ drivers/pci/Makefile | 2 + drivers/pci/xen-iommu.c | 302 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 383 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/xen/iommu.h create mode 100644 arch/x86/pci/xen.c create mode 100644 drivers/pci/xen-iommu.c
arch/x86/Kconfig +4 -0
@@ -1830,6 +1830,10 @@ config PCI_OLPC def_bool y depends on PCI && OLPC && (PCI_GOOLPC || PCI_GOANY) +config PCI_XEN + def_bool y + depends on XEN_PCI_PASSTHROUGH || XEN_DOM0_PCI + config PCI_DOMAINS def_bool y depends on PCI
arch/x86/include/asm/pci_x86.h +1 -0
@@ -107,6 +107,7 @@ extern int pci_direct_probe(void);extern void pci_direct_init (int type);extern void pci_pcbios_init (void );extern int pci_olpc_init (void ); +extern int pci_xen_init (void );extern void __init dmi_check_pciprobe (void );extern void __init dmi_check_skip_isa_align (void );
arch/x86/include/asm/xen/iommu.h new file +12 -0
@@ -0,0 +1,12 @@ +#ifndef ASM_X86__XEN_IOMMU_H + +#ifdef CONFIG_PCI_XEN +extern void xen_iommu_init (void ); +#else +static inline void xen_iommu_init (void ) +{ +} +#endif + +#endif +
arch/x86/kernel/pci-dma.c +3 -0
@@ -10,6 +10,7 @@#include <asm/gart.h> #include <asm/calgary.h> #include <asm/amd_iommu.h> +#include <asm/xen/iommu.h> static int forbid_dac__read_mostly ;
@@ -275,6 +276,8 @@ static int __init pci_iommu_init(void)dma_debug_add_bus (&pci_bus_type);#endif +xen_iommu_init (); +calgary_iommu_init ();intel_iommu_init ();
arch/x86/pci/Makefile +1 -0
@@ -4,6 +4,7 @@ obj-$(CONFIG_PCI_BIOS) += pcbios.o obj-$(CONFIG_PCI_MMCONFIG) += mmconfig_$(BITS).o direct.o mmconfig-shared.o obj-$(CONFIG_PCI_DIRECT) += direct.o obj-$(CONFIG_PCI_OLPC) += olpc.o +obj-$(CONFIG_PCI_XEN) += xen.o obj-y += fixup.o obj-$(CONFIG_ACPI) += acpi.o
arch/x86/pci/init.c +6 -0
@@ -15,10 +15,16 @@ static __init int pci_arch_init(void)if (!(pci_probe &PCI_PROBE_NOEARLY ))pci_mmcfg_early_init (); +#ifdef CONFIG_PCI_XEN +if (!pci_xen_init ()) +return 0 ; +#endif +#ifdef CONFIG_PCI_OLPC if (!pci_olpc_init ())return 0 ;/* skip additional checks if it's an XO */ #endif +#ifdef CONFIG_PCI_BIOS pci_pcbios_init ();#endif
arch/x86/pci/xen.c new file +52 -0
52 lines not on this page · raw message
drivers/pci/Makefile +2 -0
@@ -27,6 +27,8 @@ obj-$(CONFIG_HT_IRQ) += htirq.o # Build Intel IOMMU support obj-$(CONFIG_DMAR) += dmar.o iova.o intel-iommu.o +# Build Xen IOMMU support +obj-$(CONFIG_PCI_XEN) += xen-iommu.o obj-$(CONFIG_INTR_REMAP) += dmar.o intr_remapping.o obj-$(CONFIG_PCI_IOV) += iov.o
drivers/pci/xen-iommu.c new file +302 -0
302 lines not on this page · raw message
Signature · 1 line
1.6.0.6
Re: [PATCH 07/11] Xen/x86/PCI: Add support for the Xen PCI subsytem
Quoted 247 lines · Jeremy Fitzhardinge
From: Alex Nixon <[email protected]>
Impact: add core of Xen PCI support
On boot, the system will search to see if a Xen iommu/pci subsystem is available. If the kernel detects it's running in a domain rather than on bare hardware, this subsystem will be used. Otherwise, it falls back to using hardware as usual.
The frontend stub lives in
arch/x86/pci-xen.c, alongside other sub-arch PCI init code (e.g.olpc.c)(All subsequent fixes, API changes and swiotlb operations folded in.)
Signed-off-by Alex Nixon <[email protected]>Signed-off-by Jeremy Fitzhardinge <[email protected]>Signed-off-by Ian Campbell <[email protected]>Reviewed-by "H. Peter Anvin" <[email protected]>--- arch/x86/Kconfig | 4 + arch/x86/include/asm/pci_x86.h | 1 + arch/x86/include/asm/xen/iommu.h | 12 ++ arch/x86/kernel/pci-dma.c | 3 + arch/x86/pci/Makefile | 1 + arch/x86/pci/init.c | 6 + arch/x86/pci/xen.c | 52 +++++++ drivers/pci/Makefile | 2 + drivers/pci/xen-iommu.c | 302 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 383 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/xen/iommu.h create mode 100644 arch/x86/pci/xen.c create mode 100644 drivers/pci/xen-iommu.carch/x86/Kconfig +4
-0@@ -1830,6 +1830,10 @@ config PCI_OLPC def_bool y depends on PCI && OLPC && (PCI_GOOLPC || PCI_GOANY) +config PCI_XEN + def_bool y + depends on XEN_PCI_PASSTHROUGH || XEN_DOM0_PCI + config PCI_DOMAINS def_bool y depends on PCIarch/x86/include/asm/pci_x86.h +1
-0@@ -107,6 +107,7 @@ extern int pci_direct_probe(void);extern void pci_direct_init (int type);extern void pci_pcbios_init (void );extern int pci_olpc_init (void ); +extern int pci_xen_init (void );extern void __init dmi_check_pciprobe (void );extern void __init dmi_check_skip_isa_align (void );arch/x86/include/asm/xen/iommu.h new file +12
-0@@ -0,0 +1,12 @@ +#ifndef ASM_X86__XEN_IOMMU_H + +#ifdef CONFIG_PCI_XEN +extern void xen_iommu_init (void ); +#else +static inline void xen_iommu_init (void ) +{ +} +#endif + +#endif +arch/x86/kernel/pci-dma.c +3
-0@@ -10,6 +10,7 @@#include <asm/gart.h> #include <asm/calgary.h> #include <asm/amd_iommu.h> +#include <asm/xen/iommu.h> static int forbid_dac__read_mostly ;@@ -275,6 +276,8 @@ static int __init pci_iommu_init(void)dma_debug_add_bus (&pci_bus_type);#endif +xen_iommu_init (); +calgary_iommu_init ();intel_iommu_init ();arch/x86/pci/Makefile +1
-0@@ -4,6 +4,7 @@ obj-$(CONFIG_PCI_BIOS) += pcbios.o obj-$(CONFIG_PCI_MMCONFIG) += mmconfig_$(BITS).o direct.o mmconfig-shared.o obj-$(CONFIG_PCI_DIRECT) += direct.o obj-$(CONFIG_PCI_OLPC) += olpc.o +obj-$(CONFIG_PCI_XEN) += xen.o obj-y += fixup.o obj-$(CONFIG_ACPI) += acpi.oarch/x86/pci/init.c +6
-0@@ -15,10 +15,16 @@ static __init int pci_arch_init(void)if (!(pci_probe &PCI_PROBE_NOEARLY ))pci_mmcfg_early_init (); +#ifdef CONFIG_PCI_XEN +if (!pci_xen_init ()) +return 0 ; +#endif +#ifdef CONFIG_PCI_OLPC if (!pci_olpc_init ())return 0 ;/* skip additional checks if it's an XO */ #endif +#ifdef CONFIG_PCI_BIOS pci_pcbios_init ();#endif arch/x86/pci/xen.c new file +52
-0@@ -0,0 +1,52 @@ +/* +* Xen PCI Frontend Stub - puts some "dummy" functions in to the Linux +* x86 PCI core to support the Xen PCI Frontend +* +* Author: Ryan Wilson <[email protected]> +*/ +#include <linux/module.h> +#include <linux/init.h> +#include <linux/pci.h> +#include <linux/acpi.h> + +#include <asm/pci_x86.h> + +#include <asm/xen/hypervisor.h> + +static int xen_pcifront_enable_irq (struct pci_dev *dev) +{ +return 0 ; +} + +extern int isapnp_disable; + +int __init pci_xen_init (void ) +{ +if (!xen_pv_domain () ||xen_initial_domain ()) +return -ENODEV ; + +printk (KERN_INFO "PCI: setting up Xen PCI frontend stub\n" ); + +pcibios_set_cache_line_size (); + + pcibios_enable_irq = xen_pcifront_enable_irq; + pcibios_disable_irq =NULL ; + +#ifdef CONFIG_ACPI +/* Keep ACPI out of the picture */ + acpi_noirq =1 ; +#endif + +#ifdef CONFIG_ISAPNP +/* Stop isapnp from probing */ + isapnp_disable =1 ; +#endif + +/* Ensure a device still gets scanned even if it's fn number +* is non-zero. +*/ + pci_scan_all_fns =1 ; + +return 0 ; +} +drivers/pci/Makefile +2
-0@@ -27,6 +27,8 @@ obj-$(CONFIG_HT_IRQ) += htirq.o # Build Intel IOMMU support obj-$(CONFIG_DMAR) += dmar.o iova.o intel-iommu.o +# Build Xen IOMMU support +obj-$(CONFIG_PCI_XEN) += xen-iommu.o obj-$(CONFIG_INTR_REMAP) += dmar.o intr_remapping.o obj-$(CONFIG_PCI_IOV) += iov.odrivers/pci/xen-iommu.c new file +38
-0@@ -0,0 +1,302 @@ +#include <linux/types.h> +#include <linux/mm.h> +#include <linux/string.h> +#include <linux/pci.h> +#include <linux/module.h> +#include <linux/version.h> +#include <linux/scatterlist.h> +#include <linux/io.h> +#include <linux/bug.h> + +#include <xen/interface/xen.h> +#include <xen/grant_table.h> +#include <xen/page.h> +#include <xen/xen-ops.h> + +#include <asm/iommu.h> +#include <asm/swiotlb.h> +#include <asm/tlbflush.h> + +#define IOMMU_BUG_ON (test) \ +do { \ + if (unlikely(test)) { \ + printk(KERN_ALERT "Fatal DMA error! " \ + "Please use 'swiotlb=force'\n"); \ + BUG(); \ + } \ +} while (0) + +/* Print address range with message */ +#define PAR (msg, addr, size) \ +do { \ + printk(msg "[%#llx - %#llx]\n", \ + (unsigned long long)addr, \ + (unsigned long long)addr + size); \ +} while (0) + +struct dma_coherent_mem { +void *virt_base;
drivers/pci/xen-iommu.c line 48
Quoted 10 lines
drivers/pci/xen-iommu.c new file +10
-0+u32 device_base; +int size; +int flags; +unsigned long *bitmap; +}; + +static inline int address_needs_mapping (struct device *hwdev, +dma_addr_t addr) +{ +dma_addr_t mask =0xffffffff ;
You can use DMA_32BIT_MASK here.
drivers/pci/xen-iommu.c line 53
Quoted 5 lines
drivers/pci/xen-iommu.c new file +5
-0+int ret; + +/* If the device has a mask, use it, otherwise default to 32 bits */ +if (hwdev && hwdev->dma_mask) + mask = *hwdev->dma_mask;
I think the check for a valid hwdev->dma_mask is not necessary. Other IOMMU drivers also don't check for this.
Quoted 25 lines
drivers/pci/xen-iommu.c new file +25
-0+ + ret = (addr & ~mask) !=0 ; + +if (ret) { +printk (KERN_ERR "dma address needs mapping\n" ); +printk (KERN_ERR "mask: %#llx\n address: [%#llx]\n" , mask, addr); + } +return ret; +} + +static int check_pages_physically_contiguous (unsigned long pfn, +unsigned int offset, +size_t length) +{ +unsigned long next_mfn; +int i; +int nr_pages; + + next_mfn =pfn_to_mfn (pfn); + nr_pages = (offset + length +PAGE_SIZE -1 ) >>PAGE_SHIFT ; + +for (i =1 ; i < nr_pages; i++) { +if (pfn_to_mfn (++pfn) != ++next_mfn) +return 0 ; + }
drivers/pci/xen-iommu.c line 88
Quoted 10 lines
drivers/pci/xen-iommu.c new file +10
-0+return 1 ; +} + +static int range_straddles_page_boundary (phys_addr_t p,size_t size) +{ +unsigned long pfn =PFN_DOWN (p); +unsigned int offset = p & ~PAGE_MASK ; + +if (offset + size <=PAGE_SIZE ) +return 0 ;
You can use iommu_num_pages here from lib/iommu_helpers.c
Quoted 30 lines
drivers/pci/xen-iommu.c new file +30
-0+if (check_pages_physically_contiguous (pfn, offset, size)) +return 0 ; +return 1 ; +} + +static inline void xen_dma_unmap_page (struct page *page) +{ +/* Xen TODO: 2.6.18 xen calls __gnttab_dma_unmap_page here +* to deal with foreign pages. We'll need similar logic here at +* some point. +*/ +} + +/* Gets dma address of a page */ +static inline dma_addr_t xen_dma_map_page (struct page *page) +{ +/* Xen TODO: 2.6.18 xen calls __gnttab_dma_map_page here to deal +* with foreign pages. We'll need similar logic here at some +* point. +*/ +return ((dma_addr_t )pfn_to_mfn (page_to_pfn (page))) <<PAGE_SHIFT ; +} + +static int xen_map_sg (struct device *hwdev,struct scatterlist *sg, +int nents,int direction) +{ +struct scatterlist *s; +struct page *page; +int i, rc; +
drivers/pci/xen-iommu.c line 128
Quoted 10 lines
drivers/pci/xen-iommu.c new file +10
-0+BUG_ON (direction ==DMA_NONE ); +WARN_ON (nents ==0 || sg[0 ].length ==0 ); + +for_each_sg (sg, s, nents, i) { +BUG_ON (!sg_page (s)); + page =sg_page (s); + s->dma_address =xen_dma_map_page (page) + s->offset; + s->dma_length = s->length; +IOMMU_BUG_ON (range_straddles_page_boundary ( +page_to_phys (page), s->length));
I have a question on this. How do you make sure that the memory to map does not cross page boundarys? I have a stats counter for x-page requests in amd iommu code and around 10% of the requests are actually x-page for me.
Quoted 26 lines
drivers/pci/xen-iommu.c new file +26
-0+ } + + rc = nents; + +flush_write_buffers (); +return rc; +} + +static void xen_unmap_sg (struct device *hwdev,struct scatterlist *sg, +int nents,int direction) +{ +struct scatterlist *s; +struct page *page; +int i; + +for_each_sg (sg, s, nents, i) { + page =pfn_to_page (mfn_to_pfn (PFN_DOWN (s->dma_address))); +xen_dma_unmap_page (page); + } +} + +static void *xen_alloc_coherent (struct device *dev,size_t size, +dma_addr_t *dma_handle,gfp_t gfp) +{ +void *ret; +struct dma_coherent_mem *mem = dev ? dev->dma_mem :NULL ;
drivers/pci/xen-iommu.c line 164
Quoted 10 lines
drivers/pci/xen-iommu.c new file +10
-0+unsigned int order =get_order (size); +unsigned long vstart; +u64 mask; + +/* ignore region specifiers */ + gfp &= ~(__GFP_DMA | __GFP_HIGHMEM); + +if (mem) { +int page =bitmap_find_free_region (mem->bitmap, mem->size, + order);
Can you use iommu_area_alloc here?
drivers/pci/xen-iommu.c line 175
Quoted 11 lines
drivers/pci/xen-iommu.c new file +11
-0+if (page >=0 ) { + *dma_handle = mem->device_base + (page <<PAGE_SHIFT ); + ret = mem->virt_base + (page <<PAGE_SHIFT ); +memset (ret,0 , size); +return ret; + } +if (mem->flags &DMA_MEMORY_EXCLUSIVE ) +return NULL ; + } + +if (dev ==NULL || (dev->coherent_dma_mask <0xffffffff ))
DMA_32BIT_MASK
drivers/pci/xen-iommu.c line 184
Quoted 9 lines
drivers/pci/xen-iommu.c new file +9
-0+ gfp |=GFP_DMA ; + + vstart =__get_free_pages (gfp, order); + ret = (void *)vstart; + +if (dev !=NULL && dev->coherent_dma_mask) + mask = dev->coherent_dma_mask; +else + mask =0xffffffff ;
DMA_32BIT_MASK
Quoted 13 lines
drivers/pci/xen-iommu.c new file +13
-0+ +if (ret !=NULL ) { +if (xen_create_contiguous_region (vstart, order, +fls64 (mask)) !=0 ) { +free_pages (vstart, order); +return NULL ; + } +memset (ret,0 , size); + *dma_handle =virt_to_machine (ret).maddr; + } +return ret; +} +
drivers/pci/xen-iommu.c line 207
Quoted 10 lines
drivers/pci/xen-iommu.c new file +10
-0+static void xen_free_coherent (struct device *dev,size_t size, +void *vaddr,dma_addr_t dma_addr) +{ +struct dma_coherent_mem *mem = dev ? dev->dma_mem :NULL ; +int order =get_order (size); + +if (mem && vaddr >= mem->virt_base && + vaddr < (mem->virt_base + (mem->size <<PAGE_SHIFT ))) { +int page = (vaddr - mem->virt_base) >>PAGE_SHIFT ; +bitmap_release_region (mem->bitmap, page, order);
iommu_area_free
Quoted 103 lines
drivers/pci/xen-iommu.c new file +95
-0+ }else { +xen_destroy_contiguous_region ((unsigned long )vaddr, order); +free_pages ((unsigned long )vaddr, order); + } +} + +static dma_addr_t xen_swiotlb_map_single (struct device *dev,phys_addr_t paddr, +size_t size,int direction) +{ +dma_addr_t dma; +BUG_ON (direction ==DMA_NONE ); + +WARN_ON (size ==0 ); + dma =swiotlb_map_single (dev,phys_to_virt (paddr), size, direction); + +flush_write_buffers (); +return dma; +} + +static dma_addr_t xen_map_single (struct device *dev,phys_addr_t paddr, +size_t size,int direction) +{ +struct page *page; +dma_addr_t dma; + +BUG_ON (direction ==DMA_NONE ); + +WARN_ON (size ==0 ); + page =pfn_to_page (PFN_DOWN (paddr)); + + dma =xen_dma_map_page (page) +offset_in_page (paddr); + +IOMMU_BUG_ON (address_needs_mapping (dev, dma)); +IOMMU_BUG_ON (range_straddles_page_boundary (paddr, size)); +flush_write_buffers (); +return dma; +} + +static void xen_unmap_single (struct device *dev,dma_addr_t dma_addr, +size_t size,int direction) +{ +BUG_ON (direction ==DMA_NONE ); +xen_dma_unmap_page (pfn_to_page (mfn_to_pfn (PFN_DOWN (dma_addr)))); +} + +static struct dma_mapping_ops xen_dma_ops = { + .dma_supported =NULL , + + .alloc_coherent = xen_alloc_coherent, + .free_coherent = xen_free_coherent, + + .map_single = xen_map_single, + .unmap_single = xen_unmap_single, + + .map_sg = xen_map_sg, + .unmap_sg = xen_unmap_sg, + + .mapping_error =NULL , + + .is_phys =0 , +}; + +static struct dma_mapping_ops xen_swiotlb_dma_ops = { + .dma_supported = swiotlb_dma_supported, + + .alloc_coherent = xen_alloc_coherent, + .free_coherent = xen_free_coherent, + + .map_single = xen_swiotlb_map_single,/* swiotlb_map_single has a different prototype */ + .unmap_single = swiotlb_unmap_single, + + .map_sg = swiotlb_map_sg, + .unmap_sg = swiotlb_unmap_sg, + + .mapping_error = swiotlb_dma_mapping_error, + + .is_phys =0 , +}; + +void __init xen_iommu_init (void ) +{ +if (!xen_pv_domain ()) +return ; + +printk (KERN_INFO "Xen: Initializing Xen DMA ops\n" ); + + force_iommu =0 ; + dma_ops = &xen_dma_ops; + +if (swiotlb) { +printk (KERN_INFO "Xen: Enabling DMA fallback to swiotlb\n" ); + dma_ops = &xen_swiotlb_dma_ops; + } +} +Signature · 7 lines
1.6.0.6 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH 07/11] Xen/x86/PCI: Add support for the Xen PCI subsytem
Quoted 8 lines · Joerg Roedel
drivers/pci/xen-iommu.c line 48
Quoted 5 lines
drivers/pci/xen-iommu.c new file +4
-0+static inline int address_needs_mapping (struct device *hwdev, +dma_addr_t addr) +{ +dma_addr_t mask =0xffffffff ;You can use
DMA_32BIT_MASKhere.
OK. Well, DMA_BIT_MASK(32), since I think DMA_XXBIT_MASK are considered deprecated.
Quoted 10 lines
drivers/pci/xen-iommu.c line 53
Quoted 6 lines
drivers/pci/xen-iommu.c new file +5
-0+int ret; + +/* If the device has a mask, use it, otherwise default to 32 bits */ +if (hwdev && hwdev->dma_mask) + mask = *hwdev->dma_mask;I think the check for a valid
hwdev->dma_maskis not necessary. Other IOMMU drivers also don't check for this.
OK.
Quoted 12 lines
drivers/pci/xen-iommu.c line 88
Quoted 9 lines
drivers/pci/xen-iommu.c new file +8
-0+ +static int range_straddles_page_boundary (phys_addr_t p,size_t size) +{ +unsigned long pfn =PFN_DOWN (p); +unsigned int offset = p & ~PAGE_MASK ; + +if (offset + size <=PAGE_SIZE ) +return 0 ;You can use
iommu_num_pageshere fromlib/iommu_helpers.c
Ah, useful. Hm, but iommu_num_pages() takes the addr as an unsigned long, which will fail on a 32-bit machine with a 64-bit phys addr.
Quoted 15 lines
Quoted 15 lines
drivers/pci/xen-iommu.c new file +15
-0+static int xen_map_sg (struct device *hwdev,struct scatterlist *sg, +int nents,int direction) +{ +struct scatterlist *s; +struct page *page; +int i, rc; + +BUG_ON (direction ==DMA_NONE ); +WARN_ON (nents ==0 || sg[0 ].length ==0 ); + +for_each_sg (sg, s, nents, i) { +BUG_ON (!sg_page (s)); + page =sg_page (s); + s->dma_address =xen_dma_map_page (page) + s->offset; + s->dma_length = s->length;
Quoted 9 lines
drivers/pci/xen-iommu.c line 128
Quoted 3 lines
drivers/pci/xen-iommu.c new file +2
-0+IOMMU_BUG_ON (range_straddles_page_boundary ( +page_to_phys (page), s->length));I have a question on this. How do you make sure that the memory to map does not cross page boundarys? I have a stats counter for x-page requests in amd iommu code and around 10% of the requests are actually x-page for me.
I define a BIOVEC_PHYS_MERGEABLE() which prevents BIOs from being merged across non-contiguous pages. Hm, wonder where that change has gone? It should probably be part of this series...
Quoted 31 lines
Quoted 31 lines
drivers/pci/xen-iommu.c new file +31
-0+ } + + rc = nents; + +flush_write_buffers (); +return rc; +} + +static void xen_unmap_sg (struct device *hwdev,struct scatterlist *sg, +int nents,int direction) +{ +struct scatterlist *s; +struct page *page; +int i; + +for_each_sg (sg, s, nents, i) { + page =pfn_to_page (mfn_to_pfn (PFN_DOWN (s->dma_address))); +xen_dma_unmap_page (page); + } +} + +static void *xen_alloc_coherent (struct device *dev,size_t size, +dma_addr_t *dma_handle,gfp_t gfp) +{ +void *ret; +struct dma_coherent_mem *mem = dev ? dev->dma_mem :NULL ; +unsigned int order =get_order (size); +unsigned long vstart; +u64 mask; + +/* ignore region specifiers */
Quoted 9 lines
drivers/pci/xen-iommu.c line 164
Quoted 6 lines
drivers/pci/xen-iommu.c new file +5
-0+ gfp &= ~(__GFP_DMA | __GFP_HIGHMEM); + +if (mem) { +int page =bitmap_find_free_region (mem->bitmap, mem->size, + order);Can you use
iommu_area_allochere?
There's a later patch in this series ("xen/swiotlb: use dma_alloc_from_coherent to get device coherent memory") which converts it to use dma_alloc_from_coherent(). I think that's the right thing to use here, rather than iommu_area_alloc().
Quoted 5 lines
Quoted 5 lines
drivers/pci/xen-iommu.c new file +5
-0+static void xen_free_coherent (struct device *dev,size_t size, +void *vaddr,dma_addr_t dma_addr) +{ +struct dma_coherent_mem *mem = dev ? dev->dma_mem :NULL ; +int order =get_order (size);
Quoted 9 lines
drivers/pci/xen-iommu.c line 207
Quoted 6 lines
drivers/pci/xen-iommu.c new file +5
-0+ +if (mem && vaddr >= mem->virt_base && + vaddr < (mem->virt_base + (mem->size <<PAGE_SHIFT ))) { +int page = (vaddr - mem->virt_base) >>PAGE_SHIFT ; +bitmap_release_region (mem->bitmap, page, order);
iommu_area_free
I use dma_release_from_coherent() in the later patch.
Thanks,
J
PATCH 8/11 xen/swiotlb: use dma_alloc_from_coherent to get device coherent memory
In reply to Jeremy FitzhardingeFrom: Jeremy Fitzhardinge <[email protected]>
Impact: cleanup
This avoids the previous hack of replicating struct dma_coherent_mem.
--- drivers/pci/xen-iommu.c | 39 ++++++++------------------------------- 1 files changed, 8 insertions(+), 31 deletions(-)
drivers/pci/xen-iommu.c +8 -31
@@ -34,14 +34,6 @@ do { \ (unsigned long long)addr + size); \ }while (0 ) -struct dma_coherent_mem { -void *virt_base; -u32 device_base; -int size; -int flags; -unsigned long *bitmap; -}; - static inline int address_needs_mapping(struct device *hwdev,dma_addr_t addr) {
@@ -151,7 +143,6 @@ static void *xen_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp) { void *ret; -struct dma_coherent_mem *mem = dev ? dev->dma_mem :NULL ;unsigned int order =get_order (size);unsigned long vstart;u64 mask;
@@ -159,18 +150,8 @@ static void *xen_alloc_coherent(struct device *dev, size_t size,/* ignore region specifiers */ gfp &= ~(__GFP_DMA | __GFP_HIGHMEM); -if (mem) { -int page =bitmap_find_free_region (mem->bitmap, mem->size, - order); -if (page >=0 ) { - *dma_handle = mem->device_base + (page <<PAGE_SHIFT ); - ret = mem->virt_base + (page <<PAGE_SHIFT ); -memset (ret,0 , size); -return ret; - } -if (mem->flags &DMA_MEMORY_EXCLUSIVE ) -return NULL ; - } +if (dma_alloc_from_coherent (dev, size, dma_handle, &ret)) +return ret;if (dev ==NULL || (dev->coherent_dma_mask <0xffffffff )) gfp |=GFP_DMA ;
@@ -196,19 +177,15 @@ static void *xen_alloc_coherent(struct device *dev, size_t size, }static void xen_free_coherent (struct device *dev,size_t size, -void *vaddr,dma_addr_t dma_addr) +void *vaddr,dma_addr_t dma_addr) { -struct dma_coherent_mem *mem = dev ? dev->dma_mem :NULL ;int order =get_order (size); -if (mem && vaddr >= mem->virt_base && - vaddr < (mem->virt_base + (mem->size <<PAGE_SHIFT ))) { -int page = (vaddr - mem->virt_base) >>PAGE_SHIFT ; -bitmap_release_region (mem->bitmap, page, order); - }else { -xen_destroy_contiguous_region ((unsigned long )vaddr, order); -free_pages ((unsigned long )vaddr, order); - } +if (dma_release_from_coherent (dev, order, vaddr)) +return ; + +xen_destroy_contiguous_region ((unsigned long )vaddr, order); +free_pages ((unsigned long )vaddr, order); } static dma_addr_t xen_swiotlb_map_single(struct device *dev, phys_addr_t paddr,
Signature · 1 line
1.6.0.6
PATCH 9/11 x86/pci: make sure _PAGE_IOMAP it set on pci mappings
In reply to Jeremy FitzhardingeFrom: Jeremy Fitzhardinge <[email protected]>
Impact: bugfix
When mapping pci space via /sys or /proc, make sure we're really doing a hardware mapping by setting _PAGE_IOMAP.
--- arch/x86/pci/i386.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
arch/x86/pci/i386.c +3 -0
@@ -274,6 +274,9 @@ int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,return -EINVAL ; prot =pgprot_val (vma->vm_page_prot); + + prot |= _PAGE_IOMAP;/* creating a mapping for IO */ +if (pat_enabled && write_combine) prot |= _PAGE_CACHE_WC;else if (pat_enabled || boot_cpu_data.x86 >3 )
Signature · 1 line
1.6.0.6
PATCH 10/11 xen/pci: clean up Kconfig a bit
In reply to Jeremy FitzhardingeImpact: cleanup
Cut down on the maze of PCI-related config options.
--- arch/x86/Kconfig | 4 ++-- arch/x86/xen/Kconfig | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-)
arch/x86/Kconfig +2 -2
@@ -1831,8 +1831,8 @@ config PCI_OLPC depends on PCI && OLPC && (PCI_GOOLPC || PCI_GOANY) config PCI_XEN - def_bool y - depends on XEN_PCI_PASSTHROUGH || XEN_DOM0_PCI + bool + select SWIOTLB config PCI_DOMAINS def_bool y
arch/x86/xen/Kconfig +2 -0
@@ -55,6 +55,7 @@ config XEN_PRIVILEGED_GUEST config XEN_PCI_PASSTHROUGH bool #"Enable support for Xen PCI passthrough devices" depends on XEN && PCI + select PCI_XEN help Enable support for passing PCI devices through to unprivileged domains. (COMPLETELY UNTESTED)
@@ -62,3 +63,4 @@ config XEN_PCI_PASSTHROUGH config XEN_DOM0_PCI def_bool y depends on XEN_DOM0 && PCI + select PCI_XEN
Signature · 1 line
1.6.0.6
PATCH 11/11 xen: checkpatch cleanups
In reply to Jeremy FitzhardingeFrom: Jeremy Fitzhardinge <[email protected]>
Mostly whitespace cleanups; one .c extern moved into a header.
--- arch/x86/include/asm/io.h | 2 ++ arch/x86/pci/common.c | 2 +- arch/x86/pci/xen.c | 3 +-- include/xen/interface/memory.h | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-)
arch/x86/include/asm/io.h +2 -0
@@ -7,6 +7,8 @@#include <asm-generic/int-ll64.h> #include <asm/page.h> +extern int isapnp_disable; +#define build_mmio_read (name, size, type, reg, barrier) \ static inline type name(const volatile void __iomem *addr) \ { type ret; asm volatile("mov" size " %1,%0":reg (ret) \
arch/x86/pci/common.c +1 -1
@@ -22,7 +22,7 @@ unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2 |unsigned int pci_early_dump_regs;static int pci_bf_sort;int pci_routeirq; -int pci_scan_all_fns =0 ; +int pci_scan_all_fns;int noioapicquirk;#ifdef CONFIG_X86_REROUTE_FOR_BROKEN_BOOT_IRQS int noioapicreroute =0 ;
arch/x86/pci/xen.c +1 -2
@@ -10,6 +10,7 @@#include <linux/acpi.h> #include <asm/pci_x86.h> +#include <asm/io.h> #include <asm/xen/hypervisor.h>
@@ -18,8 +19,6 @@ static int xen_pcifront_enable_irq(struct pci_dev *dev)return 0 ; } -extern int isapnp_disable; -int __init pci_xen_init (void ) {if (!xen_pv_domain () ||xen_initial_domain ())
include/xen/interface/memory.h +2 -2
@@ -229,8 +229,8 @@ DEFINE_GUEST_HANDLE_STRUCT(xen_memory_map);#define XENMEM_machine_memory_map 10/* -* Prevent the balloon driver from changing the memory reservation during a driver -* critical region. +* Prevent the balloon driver from changing the memory reservation +* during a driver critical region. */ extern spinlock_t xen_reservation_lock;
Signature · 1 line
1.6.0.6
( Cc:-ed PCI folks. Original thread with patches is on lkml. There's
a few details that affect non-Xen PCI code too - and it would be nice to see feedback on this general method as well, from the PCI experts . )
Quoted 60 lines · Jeremy Fitzhardinge
Hi Ingo,
This branch allows a Xen dom0 kernel to access PCI devices properly. Mostly this involves making sure that memory being used for DMA is both logically contigious in the kernel's pseudo-physical memory and physically contigious in machine memory.
Again, this is pretty much unchanged from the last time I posted it.
The following changes since commit
0c96e43850feb7c7c4a4950f24533491fbd63b5a:
Jeremy Fitzhardinge (1):
xen: checkpatch cleanupsare available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/jeremy/xen.gitxen-tip/dom0/pciAlex Nixon (7):
xen: Don't disable the I/O space
xen: Allow unprivileged Xen domains to create iomap pages
Xen: Rename the balloon lock
xen: Addxen_create_contiguous_region
x86/PCI: Clean uppci_cache_line_size
x86/PCI: Enable scanning of all pci functions Xen/x86/PCI: Add support for the Xen PCI subsytemJeremy Fitzhardinge (4):
xen/swiotlb: usedma_alloc_from_coherentto get device coherent memory x86/pci: make sure_PAGE_IOMAPit set on pci mappings xen/pci: clean upKconfiga bit
xen: checkpatch cleanupsarch/x86/Kconfig | 4 + arch/x86/include/asm/io.h | 2 + arch/x86/include/asm/pci.h | 8 +- arch/x86/include/asm/pci_x86.h | 2 + arch/x86/include/asm/xen/iommu.h | 12 ++ arch/x86/kernel/pci-dma.c | 3 + arch/x86/pci/Makefile | 1 + arch/x86/pci/common.c | 18 ++- arch/x86/pci/i386.c | 3 + arch/x86/pci/init.c | 6 + arch/x86/pci/xen.c | 51 +++++++ arch/x86/xen/Kconfig | 2 + arch/x86/xen/enlighten.c | 6 +- arch/x86/xen/mmu.c | 225 ++++++++++++++++++++++++++++++- arch/x86/xen/setup.c | 3 - drivers/pci/Makefile | 2 + drivers/pci/xen-iommu.c | 279 ++++++++++++++++++++++++++++++++++++++ drivers/xen/balloon.c | 15 +-- include/xen/interface/memory.h | 50 +++++++ include/xen/xen-ops.h | 6 + 20 files changed, 671 insertions(+), 27 deletions(-) create mode 100644 arch/x86/include/asm/xen/iommu.h create mode 100644 arch/x86/pci/xen.c create mode 100644 drivers/pci/xen-iommu.cThanks,
J
Quoted 4 lines · Ingo Molnar
( Cc:-ed PCI folks. Original thread with patches is on lkml. There's
a few details that affect non-Xen PCI code too - and it would be nice to see feedback on this general method as well, from the PCI experts . )
I read over the top seven commits here:
http://git.kernel.org/?p=linux/kernel/git/jeremy/xen.git;a=shortlog;h=xen-tip/dom0/pci
and they all seem reasonable to me as far as they affect other code. I'm assuming that Xen people know what they're doing with respect to the Xen-only code ;-)
Signature · 4 lines
Matthew Wilcox Intel Open Source Technology Centre "Bill, look, we understand that you're interested in selling us this operating system, but compare it to ours. We can't possibly take such a retrograde step."
Quoted 11 lines · Matthew Wilcox
Quoted 4 lines · Ingo Molnar
( Cc:-ed PCI folks. Original thread with patches is on lkml. There's
a few details that affect non-Xen PCI code too - and it would be nice to see feedback on this general method as well, from the PCI experts . )I read over the top seven commits here:
http://git.kernel.org/?p=linux/kernel/git/jeremy/xen.git;a=shortlog;h=xen-tip/dom0/pci
and they all seem reasonable to me as far as they affect other code. I'm assuming that Xen people know what they're doing with respect to the Xen-only code ;-)
1. pcibios_set_cache_line_size looks could be __init
2. pci_xen_init and some xen_ function all for domU aka pv? that naming looks confusing.
YH
Quoted 2 lines · Yinghai Lu
1.
pcibios_set_cache_line_sizelooks could be__init
Will double-check.
Quoted 2 lines
2.
pci_xen_initand somexen_function all for domU aka pv? that naming looks confusing.
No, both dom0 and domU kernels are running PV. The PCI stuff can be used in either case; dom0 for talking to devices as normal, and domU for PCI-passthrough.
Thanks,
J
Quoted 1 line · Yinghai Lu
1.
pcibios_set_cache_line_sizelooks could be__init
Yes, good catch. Agreed.
Signature · 4 lines
Matthew Wilcox Intel Open Source Technology Centre "Bill, look, we understand that you're interested in selling us this operating system, but compare it to ours. We can't possibly take such a retrograde step."
Quoted 8 lines · Matthew Wilcox
Quoted 5 lines · Ingo Molnar
( Cc:-ed PCI folks. Original thread with patches is on lkml. There's
a few details that affect non-Xen PCI code too - and it would be nice to see feedback on this general method as well, from the PCI experts . )
Quoted 6 lines
I read over the top seven commits here:
http://git.kernel.org/?p=linux/kernel/git/jeremy/xen.git;a=shortlog;h=xen-tip/dom0/pci
and they all seem reasonable to me as far as they affect other code. I'm assuming that Xen people know what they're doing with respect to the Xen-only code ;-)
Thanks. Can I put your acked-by on those 7 changes (after fixing the __init thing Yinghai mentioned)?
J
Matthew Wilcox
Reviewed-byQuoted 2 lines · Jeremy Fitzhardinge
Thanks. Can I put your acked-by on those 7 changes (after fixing the
__initthing Yinghai mentioned)?
Certainly, though I think Reviewed-by would be more appropriate?
Signature · 4 lines
Matthew Wilcox Intel Open Source Technology Centre "Bill, look, we understand that you're interested in selling us this operating system, but compare it to ours. We can't possibly take such a retrograde step."
Quoted 7 lines · Matthew Wilcox
Quoted 2 lines · Jeremy Fitzhardinge
Thanks. Can I put your acked-by on those 7 changes (after fixing the
__initthing Yinghai mentioned)?Certainly, though I think Reviewed-by would be more appropriate?
Reviewed-by Matthew Wilcox <[email protected]>
Yes, they are even more valuable.
Ingo