2017-11-07 23:30:07 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
/*
|
2018-06-13 00:01:45 +07:00
|
|
|
* arch-independent dma-mapping routines
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
*
|
|
|
|
* Copyright (c) 2006 SUSE Linux Products GmbH
|
|
|
|
* Copyright (c) 2006 Tejun Heo <teheo@suse.de>
|
|
|
|
*/
|
2018-12-07 03:25:54 +07:00
|
|
|
#include <linux/memblock.h> /* for max_pfn */
|
2017-04-10 18:21:01 +07:00
|
|
|
#include <linux/acpi.h>
|
2018-12-07 04:39:32 +07:00
|
|
|
#include <linux/dma-direct.h>
|
2018-09-11 13:55:28 +07:00
|
|
|
#include <linux/dma-noncoherent.h>
|
2011-05-27 18:12:15 +07:00
|
|
|
#include <linux/export.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 15:04:11 +07:00
|
|
|
#include <linux/gfp.h>
|
2017-04-10 18:21:01 +07:00
|
|
|
#include <linux/of_device.h>
|
2014-10-10 05:26:40 +07:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/vmalloc.h>
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Managed DMA API
|
|
|
|
*/
|
|
|
|
struct dma_devres {
|
|
|
|
size_t size;
|
|
|
|
void *vaddr;
|
|
|
|
dma_addr_t dma_handle;
|
2017-06-13 00:15:04 +07:00
|
|
|
unsigned long attrs;
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
};
|
|
|
|
|
2017-06-13 00:15:04 +07:00
|
|
|
static void dmam_release(struct device *dev, void *res)
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
{
|
|
|
|
struct dma_devres *this = res;
|
|
|
|
|
2017-06-13 00:15:04 +07:00
|
|
|
dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle,
|
|
|
|
this->attrs);
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dmam_match(struct device *dev, void *res, void *match_data)
|
|
|
|
{
|
|
|
|
struct dma_devres *this = res, *match = match_data;
|
|
|
|
|
|
|
|
if (this->vaddr == match->vaddr) {
|
|
|
|
WARN_ON(this->size != match->size ||
|
|
|
|
this->dma_handle != match->dma_handle);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dmam_free_coherent - Managed dma_free_coherent()
|
|
|
|
* @dev: Device to free coherent memory for
|
|
|
|
* @size: Size of allocation
|
|
|
|
* @vaddr: Virtual address of the memory to free
|
|
|
|
* @dma_handle: DMA handle of the memory to free
|
|
|
|
*
|
|
|
|
* Managed dma_free_coherent().
|
|
|
|
*/
|
|
|
|
void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
|
|
|
|
dma_addr_t dma_handle)
|
|
|
|
{
|
|
|
|
struct dma_devres match_data = { size, vaddr, dma_handle };
|
|
|
|
|
|
|
|
dma_free_coherent(dev, size, vaddr, dma_handle);
|
2017-06-13 00:15:04 +07:00
|
|
|
WARN_ON(devres_destroy(dev, dmam_release, dmam_match, &match_data));
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dmam_free_coherent);
|
|
|
|
|
|
|
|
/**
|
2017-06-13 00:15:04 +07:00
|
|
|
* dmam_alloc_attrs - Managed dma_alloc_attrs()
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
* @dev: Device to allocate non_coherent memory for
|
|
|
|
* @size: Size of allocation
|
|
|
|
* @dma_handle: Out argument for allocated DMA handle
|
|
|
|
* @gfp: Allocation flags
|
2017-06-13 00:15:04 +07:00
|
|
|
* @attrs: Flags in the DMA_ATTR_* namespace.
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
*
|
2017-06-13 00:15:04 +07:00
|
|
|
* Managed dma_alloc_attrs(). Memory allocated using this function will be
|
|
|
|
* automatically released on driver detach.
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* Pointer to allocated memory on success, NULL on failure.
|
|
|
|
*/
|
2017-06-13 00:15:04 +07:00
|
|
|
void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
|
|
|
|
gfp_t gfp, unsigned long attrs)
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
{
|
|
|
|
struct dma_devres *dr;
|
|
|
|
void *vaddr;
|
|
|
|
|
2017-06-13 00:15:04 +07:00
|
|
|
dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
if (!dr)
|
|
|
|
return NULL;
|
|
|
|
|
2017-06-13 00:15:04 +07:00
|
|
|
vaddr = dma_alloc_attrs(dev, size, dma_handle, gfp, attrs);
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
if (!vaddr) {
|
|
|
|
devres_free(dr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dr->vaddr = vaddr;
|
|
|
|
dr->dma_handle = *dma_handle;
|
|
|
|
dr->size = size;
|
2017-06-13 00:15:04 +07:00
|
|
|
dr->attrs = attrs;
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
|
|
|
|
devres_add(dev, dr);
|
|
|
|
|
|
|
|
return vaddr;
|
|
|
|
}
|
2017-06-13 00:15:04 +07:00
|
|
|
EXPORT_SYMBOL(dmam_alloc_attrs);
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
|
2012-06-13 15:05:52 +07:00
|
|
|
/*
|
|
|
|
* Create scatter-list for the already allocated DMA buffer.
|
|
|
|
*/
|
|
|
|
int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
|
2018-08-23 14:39:38 +07:00
|
|
|
void *cpu_addr, dma_addr_t dma_addr, size_t size,
|
|
|
|
unsigned long attrs)
|
2012-06-13 15:05:52 +07:00
|
|
|
{
|
2018-08-23 14:39:38 +07:00
|
|
|
struct page *page;
|
2012-06-13 15:05:52 +07:00
|
|
|
int ret;
|
|
|
|
|
2018-08-23 14:39:38 +07:00
|
|
|
if (!dev_is_dma_coherent(dev)) {
|
|
|
|
if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN))
|
|
|
|
return -ENXIO;
|
2012-06-13 15:05:52 +07:00
|
|
|
|
2018-08-23 14:39:38 +07:00
|
|
|
page = pfn_to_page(arch_dma_coherent_to_pfn(dev, cpu_addr,
|
|
|
|
dma_addr));
|
|
|
|
} else {
|
|
|
|
page = virt_to_page(cpu_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
|
|
|
|
if (!ret)
|
|
|
|
sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
|
|
|
|
return ret;
|
2012-06-13 15:05:52 +07:00
|
|
|
}
|
2018-12-07 03:43:30 +07:00
|
|
|
|
|
|
|
int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
|
|
|
|
void *cpu_addr, dma_addr_t dma_addr, size_t size,
|
|
|
|
unsigned long attrs)
|
|
|
|
{
|
|
|
|
const struct dma_map_ops *ops = get_dma_ops(dev);
|
2018-12-07 04:39:32 +07:00
|
|
|
|
|
|
|
if (!dma_is_direct(ops) && ops->get_sgtable)
|
2018-12-07 03:43:30 +07:00
|
|
|
return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
|
|
|
|
attrs);
|
|
|
|
return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
|
|
|
|
attrs);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_get_sgtable_attrs);
|
2012-06-13 15:05:52 +07:00
|
|
|
|
2012-06-14 18:03:04 +07:00
|
|
|
/*
|
|
|
|
* Create userspace mapping for the DMA-coherent memory.
|
|
|
|
*/
|
|
|
|
int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
|
2018-09-11 13:55:28 +07:00
|
|
|
void *cpu_addr, dma_addr_t dma_addr, size_t size,
|
|
|
|
unsigned long attrs)
|
2012-06-14 18:03:04 +07:00
|
|
|
{
|
2017-06-28 16:16:57 +07:00
|
|
|
#ifndef CONFIG_ARCH_NO_COHERENT_DMA_MMAP
|
2016-05-21 20:22:22 +07:00
|
|
|
unsigned long user_count = vma_pages(vma);
|
2012-06-14 18:03:04 +07:00
|
|
|
unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
|
|
|
|
unsigned long off = vma->vm_pgoff;
|
2018-09-11 13:55:28 +07:00
|
|
|
unsigned long pfn;
|
|
|
|
int ret = -ENXIO;
|
2012-06-14 18:03:04 +07:00
|
|
|
|
2018-09-11 13:55:28 +07:00
|
|
|
vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, attrs);
|
2012-06-14 18:03:04 +07:00
|
|
|
|
2017-07-20 17:19:58 +07:00
|
|
|
if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
|
2012-06-14 18:03:04 +07:00
|
|
|
return ret;
|
|
|
|
|
2018-09-11 13:55:28 +07:00
|
|
|
if (off >= count || user_count > count - off)
|
|
|
|
return -ENXIO;
|
|
|
|
|
|
|
|
if (!dev_is_dma_coherent(dev)) {
|
|
|
|
if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN))
|
|
|
|
return -ENXIO;
|
|
|
|
pfn = arch_dma_coherent_to_pfn(dev, cpu_addr, dma_addr);
|
|
|
|
} else {
|
|
|
|
pfn = page_to_pfn(virt_to_page(cpu_addr));
|
|
|
|
}
|
2012-06-14 18:03:04 +07:00
|
|
|
|
2018-09-11 13:55:28 +07:00
|
|
|
return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
|
|
|
|
user_count << PAGE_SHIFT, vma->vm_page_prot);
|
|
|
|
#else
|
|
|
|
return -ENXIO;
|
|
|
|
#endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
|
2012-06-14 18:03:04 +07:00
|
|
|
}
|
2018-12-07 03:43:30 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* dma_mmap_attrs - map a coherent DMA allocation into user space
|
|
|
|
* @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
|
|
|
|
* @vma: vm_area_struct describing requested user mapping
|
|
|
|
* @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
|
|
|
|
* @dma_addr: device-view address returned from dma_alloc_attrs
|
|
|
|
* @size: size of memory originally requested in dma_alloc_attrs
|
|
|
|
* @attrs: attributes of mapping properties requested in dma_alloc_attrs
|
|
|
|
*
|
|
|
|
* Map a coherent DMA buffer previously allocated by dma_alloc_attrs into user
|
|
|
|
* space. The coherent DMA buffer must not be freed by the driver until the
|
|
|
|
* user space mapping has been released.
|
|
|
|
*/
|
|
|
|
int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
|
|
|
|
void *cpu_addr, dma_addr_t dma_addr, size_t size,
|
|
|
|
unsigned long attrs)
|
|
|
|
{
|
|
|
|
const struct dma_map_ops *ops = get_dma_ops(dev);
|
2018-12-07 04:39:32 +07:00
|
|
|
|
|
|
|
if (!dma_is_direct(ops) && ops->mmap)
|
2018-12-07 03:43:30 +07:00
|
|
|
return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
|
|
|
|
return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_mmap_attrs);
|
2018-12-07 03:25:54 +07:00
|
|
|
|
|
|
|
#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
|
|
|
|
static u64 dma_default_get_required_mask(struct device *dev)
|
|
|
|
{
|
|
|
|
u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
|
|
|
|
u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
|
|
|
|
u64 mask;
|
|
|
|
|
|
|
|
if (!high_totalram) {
|
|
|
|
/* convert to mask just covering totalram */
|
|
|
|
low_totalram = (1 << (fls(low_totalram) - 1));
|
|
|
|
low_totalram += low_totalram - 1;
|
|
|
|
mask = low_totalram;
|
|
|
|
} else {
|
|
|
|
high_totalram = (1 << (fls(high_totalram) - 1));
|
|
|
|
high_totalram += high_totalram - 1;
|
|
|
|
mask = (((u64)high_totalram) << 32) + 0xffffffff;
|
|
|
|
}
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 dma_get_required_mask(struct device *dev)
|
|
|
|
{
|
|
|
|
const struct dma_map_ops *ops = get_dma_ops(dev);
|
|
|
|
|
2018-12-07 04:39:32 +07:00
|
|
|
if (dma_is_direct(ops))
|
|
|
|
return dma_direct_get_required_mask(dev);
|
2018-12-07 03:25:54 +07:00
|
|
|
if (ops->get_required_mask)
|
|
|
|
return ops->get_required_mask(dev);
|
|
|
|
return dma_default_get_required_mask(dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dma_get_required_mask);
|
|
|
|
#endif
|
|
|
|
|
2018-12-07 03:43:30 +07:00
|
|
|
#ifndef arch_dma_alloc_attrs
|
|
|
|
#define arch_dma_alloc_attrs(dev) (true)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
|
|
|
|
gfp_t flag, unsigned long attrs)
|
|
|
|
{
|
|
|
|
const struct dma_map_ops *ops = get_dma_ops(dev);
|
|
|
|
void *cpu_addr;
|
|
|
|
|
|
|
|
WARN_ON_ONCE(dev && !dev->coherent_dma_mask);
|
|
|
|
|
|
|
|
if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr))
|
|
|
|
return cpu_addr;
|
|
|
|
|
|
|
|
/* let the implementation decide on the zone to allocate from: */
|
|
|
|
flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
|
|
|
|
|
|
|
|
if (!arch_dma_alloc_attrs(&dev))
|
|
|
|
return NULL;
|
2018-12-07 04:39:32 +07:00
|
|
|
|
|
|
|
if (dma_is_direct(ops))
|
|
|
|
cpu_addr = dma_direct_alloc(dev, size, dma_handle, flag, attrs);
|
|
|
|
else if (ops->alloc)
|
|
|
|
cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
|
|
|
|
else
|
2018-12-07 03:43:30 +07:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
|
|
|
|
return cpu_addr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_alloc_attrs);
|
|
|
|
|
|
|
|
void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
|
|
|
|
dma_addr_t dma_handle, unsigned long attrs)
|
|
|
|
{
|
|
|
|
const struct dma_map_ops *ops = get_dma_ops(dev);
|
|
|
|
|
|
|
|
if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
|
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* On non-coherent platforms which implement DMA-coherent buffers via
|
|
|
|
* non-cacheable remaps, ops->free() may call vunmap(). Thus getting
|
|
|
|
* this far in IRQ context is a) at risk of a BUG_ON() or trying to
|
|
|
|
* sleep on some machines, and b) an indication that the driver is
|
|
|
|
* probably misusing the coherent API anyway.
|
|
|
|
*/
|
|
|
|
WARN_ON(irqs_disabled());
|
|
|
|
|
2018-12-07 04:39:32 +07:00
|
|
|
if (!cpu_addr)
|
2018-12-07 03:43:30 +07:00
|
|
|
return;
|
|
|
|
|
|
|
|
debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
|
2018-12-07 04:39:32 +07:00
|
|
|
if (dma_is_direct(ops))
|
|
|
|
dma_direct_free(dev, size, cpu_addr, dma_handle, attrs);
|
|
|
|
else if (ops->free)
|
|
|
|
ops->free(dev, size, cpu_addr, dma_handle, attrs);
|
2018-12-07 03:43:30 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_free_attrs);
|
|
|
|
|
|
|
|
static inline void dma_check_mask(struct device *dev, u64 mask)
|
|
|
|
{
|
|
|
|
if (sme_active() && (mask < (((u64)sme_get_me_mask() << 1) - 1)))
|
|
|
|
dev_warn(dev, "SME is active, device will require DMA bounce buffers\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int dma_supported(struct device *dev, u64 mask)
|
|
|
|
{
|
|
|
|
const struct dma_map_ops *ops = get_dma_ops(dev);
|
|
|
|
|
2018-12-07 04:39:32 +07:00
|
|
|
if (dma_is_direct(ops))
|
|
|
|
return dma_direct_supported(dev, mask);
|
2018-12-20 23:35:47 +07:00
|
|
|
if (!ops->dma_supported)
|
2018-12-07 03:43:30 +07:00
|
|
|
return 1;
|
|
|
|
return ops->dma_supported(dev, mask);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_supported);
|
|
|
|
|
|
|
|
#ifndef HAVE_ARCH_DMA_SET_MASK
|
|
|
|
int dma_set_mask(struct device *dev, u64 mask)
|
|
|
|
{
|
|
|
|
if (!dev->dma_mask || !dma_supported(dev, mask))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
dma_check_mask(dev, mask);
|
|
|
|
*dev->dma_mask = mask;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_set_mask);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
|
|
|
|
int dma_set_coherent_mask(struct device *dev, u64 mask)
|
|
|
|
{
|
|
|
|
if (!dma_supported(dev, mask))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
dma_check_mask(dev, mask);
|
|
|
|
dev->coherent_dma_mask = mask;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_set_coherent_mask);
|
|
|
|
#endif
|
2018-12-07 03:47:50 +07:00
|
|
|
|
|
|
|
void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
|
|
|
|
enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
const struct dma_map_ops *ops = get_dma_ops(dev);
|
|
|
|
|
|
|
|
BUG_ON(!valid_dma_direction(dir));
|
2018-12-07 04:39:32 +07:00
|
|
|
|
|
|
|
if (dma_is_direct(ops))
|
|
|
|
arch_dma_cache_sync(dev, vaddr, size, dir);
|
|
|
|
else if (ops->cache_sync)
|
2018-12-07 03:47:50 +07:00
|
|
|
ops->cache_sync(dev, vaddr, size, dir);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_cache_sync);
|