diff options
author | Gary King <gking@nvidia.com> | 2010-06-25 18:39:58 -0700 |
---|---|---|
committer | Dan Willemsen <dwillemsen@nvidia.com> | 2011-11-30 21:34:51 -0800 |
commit | e8912a3d50cc0da8f99b3fb868a063ea1fed3193 (patch) | |
tree | d557dcc24befc06928ffb16aa8b829e0473fb738 /arch/arm/mach-tegra/include/mach/iovmm.h | |
parent | e2daf24d99c0975f332f364721aa2d00181b6c37 (diff) |
[ARM] tegra: add I/O virtual memory manager interface (iovmm)
The Tegra IOVMM is an interface to allow device drivers and subsystems in
the kernel to manage the virtual memory spaces visible to I/O devices.
The interface has been designed to be scalable to allow for I/O virtual
memory hardware which exists in one or more limited apertures of the address
space (e.g., a small aperture in physical address space which can perform
MMU-like remapping) up to complete virtual addressing with multiple
address spaces and memory protection.
The interface has been designed to be similar to the Linux virtual memory
system; however, operations which would be difficult to implement or
nonsensical for DMA devices (e.g., copy-on-write) are not present, and
APIs have been added to allow for management of multiple simultaneous
active address spaces.
The API is broken into four principal objects: areas, clients, domains and
devices.
Areas
=====
An area is a contiguous region of the virtual address space which can be
filled with virtual-to-physical translations (and, optionally, protection
attributes). The virtual address of the area can be queried and used for
DMA operations by the client which created it.
As with the Linux vm_area structures, it is the responsibility of whichever
code creates an area to ensure that it is populated with appropriate
translations.
Domains
=======
A domain in the IOVMM system is similar to a process in a standard CPU
virtual memory system; it represents the entire range of virtual addresses
which may be allocated and used for translation. Depending on hardware
capabilities, one or more domains may be resident and available for
translation. IOVMM areas are allocated from IOVMM domains.
Whenever a DMA operation is performed to or from an IOVMM area, its parent
domain must be made resident prior to commencing the operation.
Clients
=======
I/O VMM clients represent any entity which needs to be able to allocate
and map system memory into I/O virtual space. Clients are created by name
and may be created as part of a "share group," where all clients created
in the same share group will observe the same I/O virtual space (i.e., all
will use the same IOVMM domain). This is similar to threads inside a process
in the CPU virtual memory manager.
The callers of the I/O VMM system are responsible for deciding on the
granularity of client creation and share group definition; depending on the
specific usage model expected by the caller, it may be appropriate to create
an IOVMM client per task (if the caller represents an ioctl'able interface
to user land), an IOVMM client per driver instance, a common IOVMM client
for an entire bus, or a global IOVMM client for an OS subsystem (e.g., the DMA
mapping interface).
Each client is responsible for ensuring that its IOVMM client's translation is
resident on the system prior to performing DMA operations using the IOVMM
addresses. This is accomplished by preceding all DMA operations for the client
with a call to tegra_iovmm_client_lock (or tegra_iovmm_client_trylock),
and following all operations (once complete) with a call to
tegra_iovmm_client_unlock. In this regard, clients are cooperatively context-
switched, and are expected to behave appropriately.
Devices
=======
I/O VMM devices are the physical hardware which is responsible for performing
the I/O virtual-to-physical translation.
Devices are responsible for domain management: the mapping and unmapping
operations needed to make translations resident in the domain (including
any TLB shootdown or cache invalidation needed to ensure coherency), locking
and unlocking domains as they are made resident by clients into the devices'
address space(s), and allocating and deallocating the domain objects.
Devices are responsible for the allocation and deallocation of domains to
allow coalescing of multiple client share groups into a single domain. For
example, if the device's hardware only allows a single address space to
be translated system-wide, performing full flushes and invalidates of the
translation at every client switch may be prohibitively expensive. In these
circumstances, a legal implementation of the IOVMM interface includes
returning the same domain for all clients on the system (regardless of
the originally-specified share group).
In this respect, a client can be assured that it will share an address space
with all of the other clients in its share group; however, it may also share
this address space with other clients, too.
Multiple devices may be present in a system; a device should return a NULL
domain if it is incapable of servicing the client when it is asked to
allocate a domain.
----------------------------------------------------------------------------
IOVMM Client API
================
tegra_iovmm_alloc_client - Called to create a new IOVMM client object; the
implementation may create a new domain or return an existing one depending on
both the device and the share group.
tegra_iovmm_free_client - Frees a client.
tegra_iovmm_client_lock - Makes a client's translations resident in the IOVMM
device for subsequent DMA operations. May block if the device is incapable
of context-switching the client when it is called. Returns -EINTR if the
waiting thread is interrupted before the client is locked.
tegra_iovmm_client_trylock - Non-blocking version of tegra_iovmm_client_lock
tegra_iovmm_client_unlock - Called by clients after DMA operations on IOVMM-
translated addresses is complete; allows IOVMM system to context-switch the
current client out of the device if needed.
tegra_iovmm_create_vm - Called to allocate an IOVMM area. If
lazy / demand-loading of pages is desired, clients should supply a pointer
to a tegra_iovmm_area_ops structure providing callback functions to load, pin
and unpin the physical pages which will be mapped into this IOVMM region.
tegra_iovmm_get_vm_size - Called to query the total size of an IOVMM client
tegra_iovmm_free_vm - Called to free a IOVMM area, releasing any pinned
physical pages mapped by it and to decommit any resources (memory for
PTEs / PDEs) required by the VM area.
tegra_iovmm_vm_insert_pfn - Called to insert an exact pfn (system memory
physical page) into the area at a specific virtual address. Illegal to call
if the IOVMM area was originally created with lazy / demand-loading.
tegra_iovmm_zap_vm - Called to mark all mappings in the IOVMM area as
invalid / no-access, but continues to consume the I/O virtual address space.
For lazy / demand-loaded IOVMM areas, a zapped region will not be reloaded
until it has been unzapped; DMA operations using the affected translations
may fault (if supported by the device).
tegra_iovmm_unzap_vm - Called to re-enable lazy / demand-loading of pages
for a previously-zapped IOVMM area.
tegra_iovmm_find_area_get - Called to find the IOVMM area object
corresponding to the specified I/O virtual address, or NULL if the address
is not allocated in the client's address space. Increases the reference count
on the IOVMM area object
tegra_iovmm_area_get - Called to increase the reference count on the IOVMM
area object
tegra_iovmm_area_put - Called to decrease the reference count on the IOVMM
area object
IOVMM Device API
================
tegra_iovmm_register - Called to register a new IOVMM device with the IOVMM
manager
tegra_iovmm_unregister - Called to remove an IOVMM device from the IOVMM
manager (unspecified behavior if called while a translation is active and / or
in-use)
tegra_iovmm_domain_init - Called to initialize all of the IOVMM manager's
data structures (block trees, etc.) after allocating a new domain
IOVMM Device HAL
================
map - Called to inform the device about a new lazy-mapped IOVMM area. Devices
may load the entire VM area when this is called, or at any time prior to
the completion of the first read or write operation using the translation.
unmap - Called to zap or to decommit translations
map_pfn - Called to insert a specific virtual-to-physical translation in the
IOVMM area
lock_domain - Called to make a domain resident; should return 0 if the
domain was successfully context-switched, non-zero if the operation can
not be completed (e.g., all available simultaneous hardware translations are
locked). If the device can guarantee that every domain it allocates is
always usable, this function may be NULL.
unlock_domain - Releases a domain from residency, allows the hardware
translation to be used by other domains.
alloc_domain - Called to allocate a new domain; allowed to return an
existing domain
free_domain - Called to free a domain.
Change-Id: Ic65788777b7aba50ee323fe16fd553ce66c4b87c
Signed-off-by: Gary King <gking@nvidia.com>
Diffstat (limited to 'arch/arm/mach-tegra/include/mach/iovmm.h')
-rw-r--r-- | arch/arm/mach-tegra/include/mach/iovmm.h | 286 |
1 files changed, 286 insertions, 0 deletions
diff --git a/arch/arm/mach-tegra/include/mach/iovmm.h b/arch/arm/mach-tegra/include/mach/iovmm.h new file mode 100644 index 000000000000..8f111605e065 --- /dev/null +++ b/arch/arm/mach-tegra/include/mach/iovmm.h @@ -0,0 +1,286 @@ +/* + * arch/arm/mach-tegra/include/mach/iovmm.h + * + * Copyright (c) 2010, NVIDIA Corporation. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed i 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. + */ + +#include <linux/list.h> +#include <linux/platform_device.h> +#include <linux/rbtree.h> +#include <linux/rwsem.h> +#include <linux/spinlock.h> +#include <linux/types.h> + +#ifndef _MACH_TEGRA_IOVMM_H_ +#define _MACH_TEGRA_IOVMM_H_ + +#if defined(CONFIG_ARCH_TEGRA_2x_SOC) +typedef u32 tegra_iovmm_addr_t; +#else +#error "Unsupported tegra architecture family" +#endif + +struct tegra_iovmm_device_ops; + +/* each I/O virtual memory manager unit should register a device with + * the iovmm system + */ +struct tegra_iovmm_device { + struct tegra_iovmm_device_ops *ops; + const char *name; + struct list_head list; + int pgsize_bits; +}; + +/* tegra_iovmm_domain serves a purpose analagous to mm_struct as defined in + * <linux/mm_types.h> - it defines a virtual address space within which + * tegra_iovmm_areas can be created. + */ +struct tegra_iovmm_domain { + atomic_t clients; + atomic_t locks; + spinlock_t block_lock; + unsigned long flags; + wait_queue_head_t delay_lock; /* when lock_client fails */ + struct rw_semaphore map_lock; + struct rb_root all_blocks; /* ordered by address */ + struct rb_root free_blocks; /* ordered by size */ + struct tegra_iovmm_device *dev; +}; + +/* tegra_iovmm_client is analagous to an individual task in the task group + * which owns an mm_struct. + */ + +struct iovmm_share_group; + +struct tegra_iovmm_client { + const char *name; + unsigned long flags; + struct iovmm_share_group *group; + struct tegra_iovmm_domain *domain; + struct list_head list; +}; + +/* tegra_iovmm_area serves a purpose analagous to vm_area_struct as defined + * in <linux/mm_types.h> - it defines a virtual memory area which can be + * mapped to physical memory by a client-provided mapping function. */ + +struct tegra_iovmm_area { + struct tegra_iovmm_domain *domain; + tegra_iovmm_addr_t iovm_start; + tegra_iovmm_addr_t iovm_length; + pgprot_t pgprot; + struct tegra_iovmm_area_ops *ops; +}; + +struct tegra_iovmm_device_ops { + /* maps a VMA using the page residency functions provided by the VMA */ + int (*map)(struct tegra_iovmm_device *dev, + struct tegra_iovmm_area *io_vma); + /* marks all PTEs in a VMA as invalid; decommits the virtual addres + * space (potentially freeing PDEs when decommit is true.) */ + void (*unmap)(struct tegra_iovmm_device *dev, + struct tegra_iovmm_area *io_vma, bool decommit); + void (*map_pfn)(struct tegra_iovmm_device *dev, + struct tegra_iovmm_area *io_vma, + tegra_iovmm_addr_t offs, unsigned long pfn); + /* ensures that a domain is resident in the hardware's mapping region + * so that it may be used by a client */ + int (*lock_domain)(struct tegra_iovmm_device *dev, + struct tegra_iovmm_domain *domain); + void (*unlock_domain)(struct tegra_iovmm_device *dev, + struct tegra_iovmm_domain *domain); + /* allocates a vmm_domain for the specified client; may return the same + * domain for multiple clients */ + struct tegra_iovmm_domain* (*alloc_domain)( + struct tegra_iovmm_device *dev, + struct tegra_iovmm_client *client); + void (*free_domain)(struct tegra_iovmm_device *dev, + struct tegra_iovmm_domain *domain); + int (*suspend)(struct tegra_iovmm_device *dev); + void (*resume)(struct tegra_iovmm_device *dev); +}; + +struct tegra_iovmm_area_ops { + /* ensures that the page of data starting at the specified offset + * from the start of the iovma is resident and pinned for use by + * DMA, returns the system pfn, or an invalid pfn if the + * operation fails. */ + unsigned long (*lock_makeresident)(struct tegra_iovmm_area *area, + tegra_iovmm_addr_t offs); + /* called when the page is unmapped from the I/O VMA */ + void (*release)(struct tegra_iovmm_area *area, tegra_iovmm_addr_t offs); +}; + +#ifdef CONFIG_TEGRA_IOVMM +/* called by clients to allocate an I/O VMM client mapping context which + * will be shared by all clients in the same share_group */ +struct tegra_iovmm_client *tegra_iovmm_alloc_client(const char *name, + const char *share_group); + +size_t tegra_iovmm_get_vm_size(struct tegra_iovmm_client *client); + +void tegra_iovmm_free_client(struct tegra_iovmm_client *client); + +/* called by clients to ensure that their mapping context is resident + * before performing any DMA operations addressing I/O VMM regions. + * client_lock may return -EINTR. */ +int tegra_iovmm_client_lock(struct tegra_iovmm_client *client); +int tegra_iovmm_client_trylock(struct tegra_iovmm_client *client); + +/* called by clients after DMA operations are complete */ +void tegra_iovmm_client_unlock(struct tegra_iovmm_client *client); + +/* called by clients to allocate a new iovmm_area and reserve I/O virtual + * address space for it. if ops is NULL, clients should subsequently call + * tegra_iovmm_vm_map_pages and/or tegra_iovmm_vm_insert_pfn to explicitly + * map the I/O virtual address to an OS-allocated page or physical address, + * respectively. VM operations may be called before this call returns */ +struct tegra_iovmm_area *tegra_iovmm_create_vm( + struct tegra_iovmm_client *client, struct tegra_iovmm_area_ops *ops, + unsigned long size, pgprot_t pgprot); + +/* called by clients to "zap" an iovmm_area, and replace all mappings + * in it with invalid ones, without freeing the virtual address range */ +void tegra_iovmm_zap_vm(struct tegra_iovmm_area *vm); + +/* after zapping a demand-loaded iovmm_area, the client should unzap it + * to allow the VMM device to remap the page range. */ +void tegra_iovmm_unzap_vm(struct tegra_iovmm_area *vm); + +/* called by clients to return an iovmm_area to the free pool for the domain */ +void tegra_iovmm_free_vm(struct tegra_iovmm_area *vm); + +/* called by client software to map the page-aligned I/O address vaddr to + * a specific physical address pfn. I/O VMA should have been created with + * a NULL tegra_iovmm_area_ops structure. */ +void tegra_iovmm_vm_insert_pfn(struct tegra_iovmm_area *area, + tegra_iovmm_addr_t vaddr, unsigned long pfn); + +/* called by clients to return the iovmm_area containing addr, or NULL if + * addr has not been allocated. caller should call tegra_iovmm_put_area when + * finished using the returned pointer */ +struct tegra_iovmm_area *tegra_iovmm_find_area_get( + struct tegra_iovmm_client *client, tegra_iovmm_addr_t addr); + +struct tegra_iovmm_area *tegra_iovmm_area_get(struct tegra_iovmm_area *vm); +void tegra_iovmm_area_put(struct tegra_iovmm_area *vm); + +/* called by drivers to initialize a tegra_iovmm_domain structure */ +int tegra_iovmm_domain_init(struct tegra_iovmm_domain *domain, + struct tegra_iovmm_device *dev, tegra_iovmm_addr_t start, + tegra_iovmm_addr_t end); + +/* called by drivers to register an I/O VMM device with the system */ +int tegra_iovmm_register(struct tegra_iovmm_device *dev); + +/* called by drivers to remove an I/O VMM device from the system */ +int tegra_iovmm_unregister(struct tegra_iovmm_device *dev); + +/* called by platform suspend code to save IOVMM context */ +int tegra_iovmm_suspend(void); + +/* restores IOVMM context */ +void tegra_iovmm_resume(void); + +#else /* CONFIG_TEGRA_IOVMM */ + +static inline struct tegra_iovmm_client *tegra_iovmm_alloc_client( + const char *name, const char *share_group) +{ + return NULL; +} + +static inline size_t tegra_iovmm_get_vm_size(struct tegra_iovmm_client *client) +{ + return 0; +} + +static inline void tegra_iovmm_free_client(struct tegra_iovmm_client *client) +{} + +static inline int tegra_iovmm_client_lock(struct tegra_iovmm_client *client) +{ + return 0; +} + +static inline int tegra_iovmm_client_trylock(struct tegra_iovmm_client *client) +{ + return 0; +} + +static inline void tegra_iovmm_client_unlock(struct tegra_iovmm_client *client) +{} + +static inline struct tegra_iovmm_area *tegra_iovmm_create_vm( + struct tegra_iovmm_client *client, struct tegra_iovmm_area_ops *ops, + unsigned long size, pgprot_t pgprot) +{ + return NULL; +} + +static inline void tegra_iovmm_zap_vm(struct tegra_iovmm_area *vm) { } + +static inline void tegra_iovmm_unzap_vm(struct tegra_iovmm_area *vm) { } + +static inline void tegra_iovmm_free_vm(struct tegra_iovmm_area *vm) { } + +static inline void tegra_iovmm_vm_insert_pfn(struct tegra_iovmm_area *area, + tegra_iovmm_addr_t vaddr, unsigned long pfn) { } + +static inline struct tegra_iovmm_area *tegra_iovmm_find_area_get( + struct tegra_iovmm_client *client, tegra_iovmm_addr_t addr) +{ + return NULL; +} + +static inline struct tegra_iovmm_area *tegra_iovmm_area_get( + struct tegra_iovmm_area *vm) +{ + return NULL; +} + +static inline void tegra_iovmm_area_put(struct tegra_iovmm_area *vm) { } + +static inline int tegra_iovmm_domain_init(struct tegra_iovmm_domain *domain, + struct tegra_iovmm_device *dev, tegra_iovmm_addr_t start, + tegra_iovmm_addr_t end) +{ + return 0; +} + +static inline int tegra_iovmm_register(struct tegra_iovmm_device *dev) +{ + return 0; +} + +static inline int tegra_iovmm_unregister(struct tegra_iovmm_device *dev) +{ + return 0; +} + +static inline int tegra_iovmm_suspend(void) +{ + return 0; +} + +static inline void tegra_iovmm_resume(void) { } +#endif /* CONFIG_TEGRA_IOVMM */ + + +#endif |