Skip to content

Commit a8135d0

Browse files
Peter UjfalusiVinod Koul
authored andcommitted
dmaengine: core: Introduce new, universal API to request a channel
The two API function can cover most, if not all current APIs used to request a channel. With minimal effort dmaengine drivers, platforms and dmaengine user drivers can be converted to use the two function. struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask); To request any channel matching with the requested capabilities, can be used to request channel for memcpy, memset, xor, etc where no hardware synchronization is needed. struct dma_chan *dma_request_chan(struct device *dev, const char *name); To request a slave channel. The dma_request_chan() will try to find the channel via DT, ACPI or in case if the kernel booted in non DT/ACPI mode it will use a filter lookup table and retrieves the needed information from the dma_slave_map provided by the DMA drivers. This legacy mode needs changes in platform code, in dmaengine drivers and finally the dmaengine user drivers can be converted: For each dmaengine driver an array of DMA device, slave and the parameter for the filter function needs to be added: static const struct dma_slave_map da830_edma_map[] = { { "davinci-mcasp.0", "rx", EDMA_FILTER_PARAM(0, 0) }, { "davinci-mcasp.0", "tx", EDMA_FILTER_PARAM(0, 1) }, { "davinci-mcasp.1", "rx", EDMA_FILTER_PARAM(0, 2) }, { "davinci-mcasp.1", "tx", EDMA_FILTER_PARAM(0, 3) }, { "davinci-mcasp.2", "rx", EDMA_FILTER_PARAM(0, 4) }, { "davinci-mcasp.2", "tx", EDMA_FILTER_PARAM(0, 5) }, { "spi_davinci.0", "rx", EDMA_FILTER_PARAM(0, 14) }, { "spi_davinci.0", "tx", EDMA_FILTER_PARAM(0, 15) }, { "da830-mmc.0", "rx", EDMA_FILTER_PARAM(0, 16) }, { "da830-mmc.0", "tx", EDMA_FILTER_PARAM(0, 17) }, { "spi_davinci.1", "rx", EDMA_FILTER_PARAM(0, 18) }, { "spi_davinci.1", "tx", EDMA_FILTER_PARAM(0, 19) }, }; This information is going to be needed by the dmaengine driver, so modification to the platform_data is needed, and the driver map should be added to the pdata of the DMA driver: da8xx_edma0_pdata.slave_map = da830_edma_map; da8xx_edma0_pdata.slavecnt = ARRAY_SIZE(da830_edma_map); The DMA driver then needs to configure the needed device -> filter_fn mapping before it registers with dma_async_device_register() : ecc->dma_slave.filter_map.map = info->slave_map; ecc->dma_slave.filter_map.mapcnt = info->slavecnt; ecc->dma_slave.filter_map.fn = edma_filter_fn; When neither DT or ACPI lookup is available the dma_request_chan() will try to match the requester's device name with the filter_map's list of device names, when a match found it will use the information from the dma_slave_map to get the channel with the dma_get_channel() internal function. Signed-off-by: Peter Ujfalusi <peter.ujfalusi@ti.com> Reviewed-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Vinod Koul <vinod.koul@intel.com>
1 parent 7bd903c commit a8135d0

3 files changed

Lines changed: 127 additions & 36 deletions

File tree

‎Documentation/dmaengine/client.txt‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,14 @@ The slave DMA usage consists of following steps:
2222
Channel allocation is slightly different in the slave DMA context,
2323
client drivers typically need a channel from a particular DMA
2424
controller only and even in some cases a specific channel is desired.
25-
To request a channel dma_request_channel() API is used.
25+
To request a channel dma_request_chan() API is used.
2626

2727
Interface:
28-
struct dma_chan *dma_request_channel(dma_cap_mask_t mask,
29-
dma_filter_fn filter_fn,
30-
void *filter_param);
31-
where dma_filter_fn is defined as:
32-
typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
33-
34-
The 'filter_fn' parameter is optional, but highly recommended for
35-
slave and cyclic channels as they typically need to obtain a specific
36-
DMA channel.
37-
38-
When the optional 'filter_fn' parameter is NULL, dma_request_channel()
39-
simply returns the first channel that satisfies the capability mask.
40-
41-
Otherwise, the 'filter_fn' routine will be called once for each free
42-
channel which has a capability in 'mask'. 'filter_fn' is expected to
43-
return 'true' when the desired DMA channel is found.
28+
struct dma_chan *dma_request_chan(struct device *dev, const char *name);
29+
30+
Which will find and return the 'name' DMA channel associated with the 'dev'
31+
device. The association is done via DT, ACPI or board file based
32+
dma_slave_map matching table.
4433

4534
A channel allocated via this interface is exclusive to the caller,
4635
until dma_release_channel() is called.

‎drivers/dma/dmaengine.c‎

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4545

46+
#include <linux/platform_device.h>
4647
#include <linux/dma-mapping.h>
4748
#include <linux/init.h>
4849
#include <linux/module.h>
@@ -665,27 +666,73 @@ struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
665666
}
666667
EXPORT_SYMBOL_GPL(__dma_request_channel);
667668

669+
static const struct dma_slave_map *dma_filter_match(struct dma_device *device,
670+
const char *name,
671+
struct device *dev)
672+
{
673+
int i;
674+
675+
if (!device->filter.mapcnt)
676+
return NULL;
677+
678+
for (i = 0; i < device->filter.mapcnt; i++) {
679+
const struct dma_slave_map *map = &device->filter.map[i];
680+
681+
if (!strcmp(map->devname, dev_name(dev)) &&
682+
!strcmp(map->slave, name))
683+
return map;
684+
}
685+
686+
return NULL;
687+
}
688+
668689
/**
669-
* dma_request_slave_channel_reason - try to allocate an exclusive slave channel
690+
* dma_request_chan - try to allocate an exclusive slave channel
670691
* @dev: pointer to client device structure
671692
* @name: slave channel name
672693
*
673694
* Returns pointer to appropriate DMA channel on success or an error pointer.
674695
*/
675-
struct dma_chan *dma_request_slave_channel_reason(struct device *dev,
676-
const char *name)
696+
struct dma_chan *dma_request_chan(struct device *dev, const char *name)
677697
{
698+
struct dma_device *d, *_d;
699+
struct dma_chan *chan = NULL;
700+
678701
/* If device-tree is present get slave info from here */
679702
if (dev->of_node)
680-
return of_dma_request_slave_channel(dev->of_node, name);
703+
chan = of_dma_request_slave_channel(dev->of_node, name);
681704

682705
/* If device was enumerated by ACPI get slave info from here */
683-
if (ACPI_HANDLE(dev))
684-
return acpi_dma_request_slave_chan_by_name(dev, name);
706+
if (has_acpi_companion(dev) && !chan)
707+
chan = acpi_dma_request_slave_chan_by_name(dev, name);
708+
709+
if (chan) {
710+
/* Valid channel found or requester need to be deferred */
711+
if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
712+
return chan;
713+
}
714+
715+
/* Try to find the channel via the DMA filter map(s) */
716+
mutex_lock(&dma_list_mutex);
717+
list_for_each_entry_safe(d, _d, &dma_device_list, global_node) {
718+
dma_cap_mask_t mask;
719+
const struct dma_slave_map *map = dma_filter_match(d, name, dev);
720+
721+
if (!map)
722+
continue;
723+
724+
dma_cap_zero(mask);
725+
dma_cap_set(DMA_SLAVE, mask);
685726

686-
return ERR_PTR(-ENODEV);
727+
chan = find_candidate(d, &mask, d->filter.fn, map->param);
728+
if (!IS_ERR(chan))
729+
break;
730+
}
731+
mutex_unlock(&dma_list_mutex);
732+
733+
return chan ? chan : ERR_PTR(-EPROBE_DEFER);
687734
}
688-
EXPORT_SYMBOL_GPL(dma_request_slave_channel_reason);
735+
EXPORT_SYMBOL_GPL(dma_request_chan);
689736

690737
/**
691738
* dma_request_slave_channel - try to allocate an exclusive slave channel
@@ -697,17 +744,35 @@ EXPORT_SYMBOL_GPL(dma_request_slave_channel_reason);
697744
struct dma_chan *dma_request_slave_channel(struct device *dev,
698745
const char *name)
699746
{
700-
struct dma_chan *ch = dma_request_slave_channel_reason(dev, name);
747+
struct dma_chan *ch = dma_request_chan(dev, name);
701748
if (IS_ERR(ch))
702749
return NULL;
703750

704-
dma_cap_set(DMA_PRIVATE, ch->device->cap_mask);
705-
ch->device->privatecnt++;
706-
707751
return ch;
708752
}
709753
EXPORT_SYMBOL_GPL(dma_request_slave_channel);
710754

755+
/**
756+
* dma_request_chan_by_mask - allocate a channel satisfying certain capabilities
757+
* @mask: capabilities that the channel must satisfy
758+
*
759+
* Returns pointer to appropriate DMA channel on success or an error pointer.
760+
*/
761+
struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask)
762+
{
763+
struct dma_chan *chan;
764+
765+
if (!mask)
766+
return ERR_PTR(-ENODEV);
767+
768+
chan = __dma_request_channel(mask, NULL, NULL);
769+
if (!chan)
770+
chan = ERR_PTR(-ENODEV);
771+
772+
return chan;
773+
}
774+
EXPORT_SYMBOL_GPL(dma_request_chan_by_mask);
775+
711776
void dma_release_channel(struct dma_chan *chan)
712777
{
713778
mutex_lock(&dma_list_mutex);

‎include/linux/dmaengine.h‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,39 @@ enum dmaengine_alignment {
606606
DMAENGINE_ALIGN_64_BYTES = 6,
607607
};
608608

609+
/**
610+
* struct dma_slave_map - associates slave device and it's slave channel with
611+
* parameter to be used by a filter function
612+
* @devname: name of the device
613+
* @slave: slave channel name
614+
* @param: opaque parameter to pass to struct dma_filter.fn
615+
*/
616+
struct dma_slave_map {
617+
const char *devname;
618+
const char *slave;
619+
void *param;
620+
};
621+
622+
/**
623+
* struct dma_filter - information for slave device/channel to filter_fn/param
624+
* mapping
625+
* @fn: filter function callback
626+
* @mapcnt: number of slave device/channel in the map
627+
* @map: array of channel to filter mapping data
628+
*/
629+
struct dma_filter {
630+
dma_filter_fn fn;
631+
int mapcnt;
632+
const struct dma_slave_map *map;
633+
};
634+
609635
/**
610636
* struct dma_device - info on the entity supplying DMA services
611637
* @chancnt: how many DMA channels are supported
612638
* @privatecnt: how many DMA channels are requested by dma_request_channel
613639
* @channels: the list of struct dma_chan
614640
* @global_node: list_head for global dma_device_list
641+
* @filter: information for device/slave to filter function/param mapping
615642
* @cap_mask: one or more dma_capability flags
616643
* @max_xor: maximum number of xor sources, 0 if no capability
617644
* @max_pq: maximum number of PQ sources and PQ-continue capability
@@ -666,6 +693,7 @@ struct dma_device {
666693
unsigned int privatecnt;
667694
struct list_head channels;
668695
struct list_head global_node;
696+
struct dma_filter filter;
669697
dma_cap_mask_t cap_mask;
670698
unsigned short max_xor;
671699
unsigned short max_pq;
@@ -1140,9 +1168,11 @@ enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
11401168
void dma_issue_pending_all(void);
11411169
struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
11421170
dma_filter_fn fn, void *fn_param);
1143-
struct dma_chan *dma_request_slave_channel_reason(struct device *dev,
1144-
const char *name);
11451171
struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name);
1172+
1173+
struct dma_chan *dma_request_chan(struct device *dev, const char *name);
1174+
struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask);
1175+
11461176
void dma_release_channel(struct dma_chan *chan);
11471177
int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
11481178
#else
@@ -1166,16 +1196,21 @@ static inline struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
11661196
{
11671197
return NULL;
11681198
}
1169-
static inline struct dma_chan *dma_request_slave_channel_reason(
1170-
struct device *dev, const char *name)
1171-
{
1172-
return ERR_PTR(-ENODEV);
1173-
}
11741199
static inline struct dma_chan *dma_request_slave_channel(struct device *dev,
11751200
const char *name)
11761201
{
11771202
return NULL;
11781203
}
1204+
static inline struct dma_chan *dma_request_chan(struct device *dev,
1205+
const char *name)
1206+
{
1207+
return ERR_PTR(-ENODEV);
1208+
}
1209+
static inline struct dma_chan *dma_request_chan_by_mask(
1210+
const dma_cap_mask_t *mask)
1211+
{
1212+
return ERR_PTR(-ENODEV);
1213+
}
11791214
static inline void dma_release_channel(struct dma_chan *chan)
11801215
{
11811216
}
@@ -1186,6 +1221,8 @@ static inline int dma_get_slave_caps(struct dma_chan *chan,
11861221
}
11871222
#endif
11881223

1224+
#define dma_request_slave_channel_reason(dev, name) dma_request_chan(dev, name)
1225+
11891226
static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx)
11901227
{
11911228
struct dma_slave_caps caps;

0 commit comments

Comments
 (0)