1 // SPDX-License-Identifier: GPL-2.0-only
3 * Qualcomm Technologies HIDMA DMA engine Management interface
5 * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
8 #include <linux/dmaengine.h>
9 #include <linux/acpi.h>
11 #include <linux/property.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_platform.h>
15 #include <linux/module.h>
16 #include <linux/uaccess.h>
17 #include <linux/slab.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/bitops.h>
20 #include <linux/dma-mapping.h>
22 #include "hidma_mgmt.h"
24 #define HIDMA_QOS_N_OFFSET 0x700
25 #define HIDMA_CFG_OFFSET 0x400
26 #define HIDMA_MAX_BUS_REQ_LEN_OFFSET 0x41C
27 #define HIDMA_MAX_XACTIONS_OFFSET 0x420
28 #define HIDMA_HW_VERSION_OFFSET 0x424
29 #define HIDMA_CHRESET_TIMEOUT_OFFSET 0x418
31 #define HIDMA_MAX_WR_XACTIONS_MASK GENMASK(4, 0)
32 #define HIDMA_MAX_RD_XACTIONS_MASK GENMASK(4, 0)
33 #define HIDMA_WEIGHT_MASK GENMASK(6, 0)
34 #define HIDMA_MAX_BUS_REQ_LEN_MASK GENMASK(15, 0)
35 #define HIDMA_CHRESET_TIMEOUT_MASK GENMASK(19, 0)
37 #define HIDMA_MAX_WR_XACTIONS_BIT_POS 16
38 #define HIDMA_MAX_BUS_WR_REQ_BIT_POS 16
39 #define HIDMA_WRR_BIT_POS 8
40 #define HIDMA_PRIORITY_BIT_POS 15
42 #define HIDMA_AUTOSUSPEND_TIMEOUT 2000
43 #define HIDMA_MAX_CHANNEL_WEIGHT 15
45 static unsigned int max_write_request
;
46 module_param(max_write_request
, uint
, 0644);
47 MODULE_PARM_DESC(max_write_request
,
48 "maximum write burst (default: ACPI/DT value)");
50 static unsigned int max_read_request
;
51 module_param(max_read_request
, uint
, 0644);
52 MODULE_PARM_DESC(max_read_request
,
53 "maximum read burst (default: ACPI/DT value)");
55 static unsigned int max_wr_xactions
;
56 module_param(max_wr_xactions
, uint
, 0644);
57 MODULE_PARM_DESC(max_wr_xactions
,
58 "maximum number of write transactions (default: ACPI/DT value)");
60 static unsigned int max_rd_xactions
;
61 module_param(max_rd_xactions
, uint
, 0644);
62 MODULE_PARM_DESC(max_rd_xactions
,
63 "maximum number of read transactions (default: ACPI/DT value)");
65 int hidma_mgmt_setup(struct hidma_mgmt_dev
*mgmtdev
)
70 if (!is_power_of_2(mgmtdev
->max_write_request
) ||
71 (mgmtdev
->max_write_request
< 128) ||
72 (mgmtdev
->max_write_request
> 1024)) {
73 dev_err(&mgmtdev
->pdev
->dev
, "invalid write request %d\n",
74 mgmtdev
->max_write_request
);
78 if (!is_power_of_2(mgmtdev
->max_read_request
) ||
79 (mgmtdev
->max_read_request
< 128) ||
80 (mgmtdev
->max_read_request
> 1024)) {
81 dev_err(&mgmtdev
->pdev
->dev
, "invalid read request %d\n",
82 mgmtdev
->max_read_request
);
86 if (mgmtdev
->max_wr_xactions
> HIDMA_MAX_WR_XACTIONS_MASK
) {
87 dev_err(&mgmtdev
->pdev
->dev
,
88 "max_wr_xactions cannot be bigger than %ld\n",
89 HIDMA_MAX_WR_XACTIONS_MASK
);
93 if (mgmtdev
->max_rd_xactions
> HIDMA_MAX_RD_XACTIONS_MASK
) {
94 dev_err(&mgmtdev
->pdev
->dev
,
95 "max_rd_xactions cannot be bigger than %ld\n",
96 HIDMA_MAX_RD_XACTIONS_MASK
);
100 for (i
= 0; i
< mgmtdev
->dma_channels
; i
++) {
101 if (mgmtdev
->priority
[i
] > 1) {
102 dev_err(&mgmtdev
->pdev
->dev
,
103 "priority can be 0 or 1\n");
107 if (mgmtdev
->weight
[i
] > HIDMA_MAX_CHANNEL_WEIGHT
) {
108 dev_err(&mgmtdev
->pdev
->dev
,
109 "max value of weight can be %d.\n",
110 HIDMA_MAX_CHANNEL_WEIGHT
);
114 /* weight needs to be at least one */
115 if (mgmtdev
->weight
[i
] == 0)
116 mgmtdev
->weight
[i
] = 1;
119 pm_runtime_get_sync(&mgmtdev
->pdev
->dev
);
120 val
= readl(mgmtdev
->virtaddr
+ HIDMA_MAX_BUS_REQ_LEN_OFFSET
);
121 val
&= ~(HIDMA_MAX_BUS_REQ_LEN_MASK
<< HIDMA_MAX_BUS_WR_REQ_BIT_POS
);
122 val
|= mgmtdev
->max_write_request
<< HIDMA_MAX_BUS_WR_REQ_BIT_POS
;
123 val
&= ~HIDMA_MAX_BUS_REQ_LEN_MASK
;
124 val
|= mgmtdev
->max_read_request
;
125 writel(val
, mgmtdev
->virtaddr
+ HIDMA_MAX_BUS_REQ_LEN_OFFSET
);
127 val
= readl(mgmtdev
->virtaddr
+ HIDMA_MAX_XACTIONS_OFFSET
);
128 val
&= ~(HIDMA_MAX_WR_XACTIONS_MASK
<< HIDMA_MAX_WR_XACTIONS_BIT_POS
);
129 val
|= mgmtdev
->max_wr_xactions
<< HIDMA_MAX_WR_XACTIONS_BIT_POS
;
130 val
&= ~HIDMA_MAX_RD_XACTIONS_MASK
;
131 val
|= mgmtdev
->max_rd_xactions
;
132 writel(val
, mgmtdev
->virtaddr
+ HIDMA_MAX_XACTIONS_OFFSET
);
134 mgmtdev
->hw_version
=
135 readl(mgmtdev
->virtaddr
+ HIDMA_HW_VERSION_OFFSET
);
136 mgmtdev
->hw_version_major
= (mgmtdev
->hw_version
>> 28) & 0xF;
137 mgmtdev
->hw_version_minor
= (mgmtdev
->hw_version
>> 16) & 0xF;
139 for (i
= 0; i
< mgmtdev
->dma_channels
; i
++) {
140 u32 weight
= mgmtdev
->weight
[i
];
141 u32 priority
= mgmtdev
->priority
[i
];
143 val
= readl(mgmtdev
->virtaddr
+ HIDMA_QOS_N_OFFSET
+ (4 * i
));
144 val
&= ~(1 << HIDMA_PRIORITY_BIT_POS
);
145 val
|= (priority
& 0x1) << HIDMA_PRIORITY_BIT_POS
;
146 val
&= ~(HIDMA_WEIGHT_MASK
<< HIDMA_WRR_BIT_POS
);
147 val
|= (weight
& HIDMA_WEIGHT_MASK
) << HIDMA_WRR_BIT_POS
;
148 writel(val
, mgmtdev
->virtaddr
+ HIDMA_QOS_N_OFFSET
+ (4 * i
));
151 val
= readl(mgmtdev
->virtaddr
+ HIDMA_CHRESET_TIMEOUT_OFFSET
);
152 val
&= ~HIDMA_CHRESET_TIMEOUT_MASK
;
153 val
|= mgmtdev
->chreset_timeout_cycles
& HIDMA_CHRESET_TIMEOUT_MASK
;
154 writel(val
, mgmtdev
->virtaddr
+ HIDMA_CHRESET_TIMEOUT_OFFSET
);
156 pm_runtime_mark_last_busy(&mgmtdev
->pdev
->dev
);
157 pm_runtime_put_autosuspend(&mgmtdev
->pdev
->dev
);
160 EXPORT_SYMBOL_GPL(hidma_mgmt_setup
);
162 static int hidma_mgmt_probe(struct platform_device
*pdev
)
164 struct hidma_mgmt_dev
*mgmtdev
;
165 struct resource
*res
;
166 void __iomem
*virtaddr
;
171 pm_runtime_set_autosuspend_delay(&pdev
->dev
, HIDMA_AUTOSUSPEND_TIMEOUT
);
172 pm_runtime_use_autosuspend(&pdev
->dev
);
173 pm_runtime_set_active(&pdev
->dev
);
174 pm_runtime_enable(&pdev
->dev
);
175 pm_runtime_get_sync(&pdev
->dev
);
177 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
178 virtaddr
= devm_ioremap_resource(&pdev
->dev
, res
);
179 if (IS_ERR(virtaddr
)) {
184 irq
= platform_get_irq(pdev
, 0);
190 mgmtdev
= devm_kzalloc(&pdev
->dev
, sizeof(*mgmtdev
), GFP_KERNEL
);
196 mgmtdev
->pdev
= pdev
;
197 mgmtdev
->addrsize
= resource_size(res
);
198 mgmtdev
->virtaddr
= virtaddr
;
200 rc
= device_property_read_u32(&pdev
->dev
, "dma-channels",
201 &mgmtdev
->dma_channels
);
203 dev_err(&pdev
->dev
, "number of channels missing\n");
207 rc
= device_property_read_u32(&pdev
->dev
,
208 "channel-reset-timeout-cycles",
209 &mgmtdev
->chreset_timeout_cycles
);
211 dev_err(&pdev
->dev
, "channel reset timeout missing\n");
215 rc
= device_property_read_u32(&pdev
->dev
, "max-write-burst-bytes",
216 &mgmtdev
->max_write_request
);
218 dev_err(&pdev
->dev
, "max-write-burst-bytes missing\n");
222 if (max_write_request
&&
223 (max_write_request
!= mgmtdev
->max_write_request
)) {
224 dev_info(&pdev
->dev
, "overriding max-write-burst-bytes: %d\n",
226 mgmtdev
->max_write_request
= max_write_request
;
228 max_write_request
= mgmtdev
->max_write_request
;
230 rc
= device_property_read_u32(&pdev
->dev
, "max-read-burst-bytes",
231 &mgmtdev
->max_read_request
);
233 dev_err(&pdev
->dev
, "max-read-burst-bytes missing\n");
236 if (max_read_request
&&
237 (max_read_request
!= mgmtdev
->max_read_request
)) {
238 dev_info(&pdev
->dev
, "overriding max-read-burst-bytes: %d\n",
240 mgmtdev
->max_read_request
= max_read_request
;
242 max_read_request
= mgmtdev
->max_read_request
;
244 rc
= device_property_read_u32(&pdev
->dev
, "max-write-transactions",
245 &mgmtdev
->max_wr_xactions
);
247 dev_err(&pdev
->dev
, "max-write-transactions missing\n");
250 if (max_wr_xactions
&&
251 (max_wr_xactions
!= mgmtdev
->max_wr_xactions
)) {
252 dev_info(&pdev
->dev
, "overriding max-write-transactions: %d\n",
254 mgmtdev
->max_wr_xactions
= max_wr_xactions
;
256 max_wr_xactions
= mgmtdev
->max_wr_xactions
;
258 rc
= device_property_read_u32(&pdev
->dev
, "max-read-transactions",
259 &mgmtdev
->max_rd_xactions
);
261 dev_err(&pdev
->dev
, "max-read-transactions missing\n");
264 if (max_rd_xactions
&&
265 (max_rd_xactions
!= mgmtdev
->max_rd_xactions
)) {
266 dev_info(&pdev
->dev
, "overriding max-read-transactions: %d\n",
268 mgmtdev
->max_rd_xactions
= max_rd_xactions
;
270 max_rd_xactions
= mgmtdev
->max_rd_xactions
;
272 mgmtdev
->priority
= devm_kcalloc(&pdev
->dev
,
273 mgmtdev
->dma_channels
,
274 sizeof(*mgmtdev
->priority
),
276 if (!mgmtdev
->priority
) {
281 mgmtdev
->weight
= devm_kcalloc(&pdev
->dev
,
282 mgmtdev
->dma_channels
,
283 sizeof(*mgmtdev
->weight
), GFP_KERNEL
);
284 if (!mgmtdev
->weight
) {
289 rc
= hidma_mgmt_setup(mgmtdev
);
291 dev_err(&pdev
->dev
, "setup failed\n");
296 val
= readl(mgmtdev
->virtaddr
+ HIDMA_CFG_OFFSET
);
298 writel(val
, mgmtdev
->virtaddr
+ HIDMA_CFG_OFFSET
);
300 rc
= hidma_mgmt_init_sys(mgmtdev
);
302 dev_err(&pdev
->dev
, "sysfs setup failed\n");
307 "HW rev: %d.%d @ %pa with %d physical channels\n",
308 mgmtdev
->hw_version_major
, mgmtdev
->hw_version_minor
,
309 &res
->start
, mgmtdev
->dma_channels
);
311 platform_set_drvdata(pdev
, mgmtdev
);
312 pm_runtime_mark_last_busy(&pdev
->dev
);
313 pm_runtime_put_autosuspend(&pdev
->dev
);
316 pm_runtime_put_sync_suspend(&pdev
->dev
);
317 pm_runtime_disable(&pdev
->dev
);
321 #if IS_ENABLED(CONFIG_ACPI)
322 static const struct acpi_device_id hidma_mgmt_acpi_ids
[] = {
326 MODULE_DEVICE_TABLE(acpi
, hidma_mgmt_acpi_ids
);
329 static const struct of_device_id hidma_mgmt_match
[] = {
330 {.compatible
= "qcom,hidma-mgmt-1.0",},
333 MODULE_DEVICE_TABLE(of
, hidma_mgmt_match
);
335 static struct platform_driver hidma_mgmt_driver
= {
336 .probe
= hidma_mgmt_probe
,
338 .name
= "hidma-mgmt",
339 .of_match_table
= hidma_mgmt_match
,
340 .acpi_match_table
= ACPI_PTR(hidma_mgmt_acpi_ids
),
344 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
345 static int object_counter
;
347 static int __init
hidma_mgmt_of_populate_channels(struct device_node
*np
)
349 struct platform_device
*pdev_parent
= of_find_device_by_node(np
);
350 struct platform_device_info pdevinfo
;
351 struct device_node
*child
;
352 struct resource
*res
;
355 /* allocate a resource array */
356 res
= kcalloc(3, sizeof(*res
), GFP_KERNEL
);
360 for_each_available_child_of_node(np
, child
) {
361 struct platform_device
*new_pdev
;
363 ret
= of_address_to_resource(child
, 0, &res
[0]);
367 ret
= of_address_to_resource(child
, 1, &res
[1]);
371 ret
= of_irq_to_resource(child
, 0, &res
[2]);
375 memset(&pdevinfo
, 0, sizeof(pdevinfo
));
376 pdevinfo
.fwnode
= &child
->fwnode
;
377 pdevinfo
.parent
= pdev_parent
? &pdev_parent
->dev
: NULL
;
378 pdevinfo
.name
= child
->name
;
379 pdevinfo
.id
= object_counter
++;
381 pdevinfo
.num_res
= 3;
382 pdevinfo
.data
= NULL
;
383 pdevinfo
.size_data
= 0;
384 pdevinfo
.dma_mask
= DMA_BIT_MASK(64);
385 new_pdev
= platform_device_register_full(&pdevinfo
);
386 if (IS_ERR(new_pdev
)) {
387 ret
= PTR_ERR(new_pdev
);
390 new_pdev
->dev
.of_node
= child
;
391 of_dma_configure(&new_pdev
->dev
, child
, true);
393 * It is assumed that calling of_msi_configure is safe on
394 * platforms with or without MSI support.
396 of_msi_configure(&new_pdev
->dev
, child
);
411 static int __init
hidma_mgmt_init(void)
413 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
414 struct device_node
*child
;
416 for_each_matching_node(child
, hidma_mgmt_match
) {
417 /* device tree based firmware here */
418 hidma_mgmt_of_populate_channels(child
);
421 return platform_driver_register(&hidma_mgmt_driver
);
424 module_init(hidma_mgmt_init
);
425 MODULE_LICENSE("GPL v2");