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>
10 #include <linux/property.h>
11 #include <linux/platform_device.h>
12 #include <linux/module.h>
13 #include <linux/uaccess.h>
14 #include <linux/slab.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/bitops.h>
17 #include <linux/dma-mapping.h>
19 #include "hidma_mgmt.h"
21 #define HIDMA_QOS_N_OFFSET 0x700
22 #define HIDMA_CFG_OFFSET 0x400
23 #define HIDMA_MAX_BUS_REQ_LEN_OFFSET 0x41C
24 #define HIDMA_MAX_XACTIONS_OFFSET 0x420
25 #define HIDMA_HW_VERSION_OFFSET 0x424
26 #define HIDMA_CHRESET_TIMEOUT_OFFSET 0x418
28 #define HIDMA_MAX_WR_XACTIONS_MASK GENMASK(4, 0)
29 #define HIDMA_MAX_RD_XACTIONS_MASK GENMASK(4, 0)
30 #define HIDMA_WEIGHT_MASK GENMASK(6, 0)
31 #define HIDMA_MAX_BUS_REQ_LEN_MASK GENMASK(15, 0)
32 #define HIDMA_CHRESET_TIMEOUT_MASK GENMASK(19, 0)
34 #define HIDMA_MAX_WR_XACTIONS_BIT_POS 16
35 #define HIDMA_MAX_BUS_WR_REQ_BIT_POS 16
36 #define HIDMA_WRR_BIT_POS 8
37 #define HIDMA_PRIORITY_BIT_POS 15
39 #define HIDMA_AUTOSUSPEND_TIMEOUT 2000
40 #define HIDMA_MAX_CHANNEL_WEIGHT 15
42 static unsigned int max_write_request
;
43 module_param(max_write_request
, uint
, 0644);
44 MODULE_PARM_DESC(max_write_request
,
45 "maximum write burst (default: ACPI/DT value)");
47 static unsigned int max_read_request
;
48 module_param(max_read_request
, uint
, 0644);
49 MODULE_PARM_DESC(max_read_request
,
50 "maximum read burst (default: ACPI/DT value)");
52 static unsigned int max_wr_xactions
;
53 module_param(max_wr_xactions
, uint
, 0644);
54 MODULE_PARM_DESC(max_wr_xactions
,
55 "maximum number of write transactions (default: ACPI/DT value)");
57 static unsigned int max_rd_xactions
;
58 module_param(max_rd_xactions
, uint
, 0644);
59 MODULE_PARM_DESC(max_rd_xactions
,
60 "maximum number of read transactions (default: ACPI/DT value)");
62 int hidma_mgmt_setup(struct hidma_mgmt_dev
*mgmtdev
)
67 if (!is_power_of_2(mgmtdev
->max_write_request
) ||
68 (mgmtdev
->max_write_request
< 128) ||
69 (mgmtdev
->max_write_request
> 1024)) {
70 dev_err(&mgmtdev
->pdev
->dev
, "invalid write request %d\n",
71 mgmtdev
->max_write_request
);
75 if (!is_power_of_2(mgmtdev
->max_read_request
) ||
76 (mgmtdev
->max_read_request
< 128) ||
77 (mgmtdev
->max_read_request
> 1024)) {
78 dev_err(&mgmtdev
->pdev
->dev
, "invalid read request %d\n",
79 mgmtdev
->max_read_request
);
83 if (mgmtdev
->max_wr_xactions
> HIDMA_MAX_WR_XACTIONS_MASK
) {
84 dev_err(&mgmtdev
->pdev
->dev
,
85 "max_wr_xactions cannot be bigger than %ld\n",
86 HIDMA_MAX_WR_XACTIONS_MASK
);
90 if (mgmtdev
->max_rd_xactions
> HIDMA_MAX_RD_XACTIONS_MASK
) {
91 dev_err(&mgmtdev
->pdev
->dev
,
92 "max_rd_xactions cannot be bigger than %ld\n",
93 HIDMA_MAX_RD_XACTIONS_MASK
);
97 for (i
= 0; i
< mgmtdev
->dma_channels
; i
++) {
98 if (mgmtdev
->priority
[i
] > 1) {
99 dev_err(&mgmtdev
->pdev
->dev
,
100 "priority can be 0 or 1\n");
104 if (mgmtdev
->weight
[i
] > HIDMA_MAX_CHANNEL_WEIGHT
) {
105 dev_err(&mgmtdev
->pdev
->dev
,
106 "max value of weight can be %d.\n",
107 HIDMA_MAX_CHANNEL_WEIGHT
);
111 /* weight needs to be at least one */
112 if (mgmtdev
->weight
[i
] == 0)
113 mgmtdev
->weight
[i
] = 1;
116 pm_runtime_get_sync(&mgmtdev
->pdev
->dev
);
117 val
= readl(mgmtdev
->virtaddr
+ HIDMA_MAX_BUS_REQ_LEN_OFFSET
);
118 val
&= ~(HIDMA_MAX_BUS_REQ_LEN_MASK
<< HIDMA_MAX_BUS_WR_REQ_BIT_POS
);
119 val
|= mgmtdev
->max_write_request
<< HIDMA_MAX_BUS_WR_REQ_BIT_POS
;
120 val
&= ~HIDMA_MAX_BUS_REQ_LEN_MASK
;
121 val
|= mgmtdev
->max_read_request
;
122 writel(val
, mgmtdev
->virtaddr
+ HIDMA_MAX_BUS_REQ_LEN_OFFSET
);
124 val
= readl(mgmtdev
->virtaddr
+ HIDMA_MAX_XACTIONS_OFFSET
);
125 val
&= ~(HIDMA_MAX_WR_XACTIONS_MASK
<< HIDMA_MAX_WR_XACTIONS_BIT_POS
);
126 val
|= mgmtdev
->max_wr_xactions
<< HIDMA_MAX_WR_XACTIONS_BIT_POS
;
127 val
&= ~HIDMA_MAX_RD_XACTIONS_MASK
;
128 val
|= mgmtdev
->max_rd_xactions
;
129 writel(val
, mgmtdev
->virtaddr
+ HIDMA_MAX_XACTIONS_OFFSET
);
131 mgmtdev
->hw_version
=
132 readl(mgmtdev
->virtaddr
+ HIDMA_HW_VERSION_OFFSET
);
133 mgmtdev
->hw_version_major
= (mgmtdev
->hw_version
>> 28) & 0xF;
134 mgmtdev
->hw_version_minor
= (mgmtdev
->hw_version
>> 16) & 0xF;
136 for (i
= 0; i
< mgmtdev
->dma_channels
; i
++) {
137 u32 weight
= mgmtdev
->weight
[i
];
138 u32 priority
= mgmtdev
->priority
[i
];
140 val
= readl(mgmtdev
->virtaddr
+ HIDMA_QOS_N_OFFSET
+ (4 * i
));
141 val
&= ~(1 << HIDMA_PRIORITY_BIT_POS
);
142 val
|= (priority
& 0x1) << HIDMA_PRIORITY_BIT_POS
;
143 val
&= ~(HIDMA_WEIGHT_MASK
<< HIDMA_WRR_BIT_POS
);
144 val
|= (weight
& HIDMA_WEIGHT_MASK
) << HIDMA_WRR_BIT_POS
;
145 writel(val
, mgmtdev
->virtaddr
+ HIDMA_QOS_N_OFFSET
+ (4 * i
));
148 val
= readl(mgmtdev
->virtaddr
+ HIDMA_CHRESET_TIMEOUT_OFFSET
);
149 val
&= ~HIDMA_CHRESET_TIMEOUT_MASK
;
150 val
|= mgmtdev
->chreset_timeout_cycles
& HIDMA_CHRESET_TIMEOUT_MASK
;
151 writel(val
, mgmtdev
->virtaddr
+ HIDMA_CHRESET_TIMEOUT_OFFSET
);
153 pm_runtime_mark_last_busy(&mgmtdev
->pdev
->dev
);
154 pm_runtime_put_autosuspend(&mgmtdev
->pdev
->dev
);
157 EXPORT_SYMBOL_GPL(hidma_mgmt_setup
);
159 static int hidma_mgmt_probe(struct platform_device
*pdev
)
161 struct hidma_mgmt_dev
*mgmtdev
;
162 struct resource
*res
;
163 void __iomem
*virtaddr
;
168 pm_runtime_set_autosuspend_delay(&pdev
->dev
, HIDMA_AUTOSUSPEND_TIMEOUT
);
169 pm_runtime_use_autosuspend(&pdev
->dev
);
170 pm_runtime_set_active(&pdev
->dev
);
171 pm_runtime_enable(&pdev
->dev
);
172 pm_runtime_get_sync(&pdev
->dev
);
174 virtaddr
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
175 if (IS_ERR(virtaddr
)) {
176 rc
= PTR_ERR(virtaddr
);
180 irq
= platform_get_irq(pdev
, 0);
186 mgmtdev
= devm_kzalloc(&pdev
->dev
, sizeof(*mgmtdev
), GFP_KERNEL
);
192 mgmtdev
->pdev
= pdev
;
193 mgmtdev
->addrsize
= resource_size(res
);
194 mgmtdev
->virtaddr
= virtaddr
;
196 rc
= device_property_read_u32(&pdev
->dev
, "dma-channels",
197 &mgmtdev
->dma_channels
);
199 dev_err(&pdev
->dev
, "number of channels missing\n");
203 rc
= device_property_read_u32(&pdev
->dev
,
204 "channel-reset-timeout-cycles",
205 &mgmtdev
->chreset_timeout_cycles
);
207 dev_err(&pdev
->dev
, "channel reset timeout missing\n");
211 rc
= device_property_read_u32(&pdev
->dev
, "max-write-burst-bytes",
212 &mgmtdev
->max_write_request
);
214 dev_err(&pdev
->dev
, "max-write-burst-bytes missing\n");
218 if (max_write_request
&&
219 (max_write_request
!= mgmtdev
->max_write_request
)) {
220 dev_info(&pdev
->dev
, "overriding max-write-burst-bytes: %d\n",
222 mgmtdev
->max_write_request
= max_write_request
;
224 max_write_request
= mgmtdev
->max_write_request
;
226 rc
= device_property_read_u32(&pdev
->dev
, "max-read-burst-bytes",
227 &mgmtdev
->max_read_request
);
229 dev_err(&pdev
->dev
, "max-read-burst-bytes missing\n");
232 if (max_read_request
&&
233 (max_read_request
!= mgmtdev
->max_read_request
)) {
234 dev_info(&pdev
->dev
, "overriding max-read-burst-bytes: %d\n",
236 mgmtdev
->max_read_request
= max_read_request
;
238 max_read_request
= mgmtdev
->max_read_request
;
240 rc
= device_property_read_u32(&pdev
->dev
, "max-write-transactions",
241 &mgmtdev
->max_wr_xactions
);
243 dev_err(&pdev
->dev
, "max-write-transactions missing\n");
246 if (max_wr_xactions
&&
247 (max_wr_xactions
!= mgmtdev
->max_wr_xactions
)) {
248 dev_info(&pdev
->dev
, "overriding max-write-transactions: %d\n",
250 mgmtdev
->max_wr_xactions
= max_wr_xactions
;
252 max_wr_xactions
= mgmtdev
->max_wr_xactions
;
254 rc
= device_property_read_u32(&pdev
->dev
, "max-read-transactions",
255 &mgmtdev
->max_rd_xactions
);
257 dev_err(&pdev
->dev
, "max-read-transactions missing\n");
260 if (max_rd_xactions
&&
261 (max_rd_xactions
!= mgmtdev
->max_rd_xactions
)) {
262 dev_info(&pdev
->dev
, "overriding max-read-transactions: %d\n",
264 mgmtdev
->max_rd_xactions
= max_rd_xactions
;
266 max_rd_xactions
= mgmtdev
->max_rd_xactions
;
268 mgmtdev
->priority
= devm_kcalloc(&pdev
->dev
,
269 mgmtdev
->dma_channels
,
270 sizeof(*mgmtdev
->priority
),
272 if (!mgmtdev
->priority
) {
277 mgmtdev
->weight
= devm_kcalloc(&pdev
->dev
,
278 mgmtdev
->dma_channels
,
279 sizeof(*mgmtdev
->weight
), GFP_KERNEL
);
280 if (!mgmtdev
->weight
) {
285 rc
= hidma_mgmt_setup(mgmtdev
);
287 dev_err(&pdev
->dev
, "setup failed\n");
292 val
= readl(mgmtdev
->virtaddr
+ HIDMA_CFG_OFFSET
);
294 writel(val
, mgmtdev
->virtaddr
+ HIDMA_CFG_OFFSET
);
296 rc
= hidma_mgmt_init_sys(mgmtdev
);
298 dev_err(&pdev
->dev
, "sysfs setup failed\n");
303 "HW rev: %d.%d @ %pa with %d physical channels\n",
304 mgmtdev
->hw_version_major
, mgmtdev
->hw_version_minor
,
305 &res
->start
, mgmtdev
->dma_channels
);
307 platform_set_drvdata(pdev
, mgmtdev
);
308 pm_runtime_mark_last_busy(&pdev
->dev
);
309 pm_runtime_put_autosuspend(&pdev
->dev
);
312 pm_runtime_put_sync_suspend(&pdev
->dev
);
313 pm_runtime_disable(&pdev
->dev
);
317 #if IS_ENABLED(CONFIG_ACPI)
318 static const struct acpi_device_id hidma_mgmt_acpi_ids
[] = {
322 MODULE_DEVICE_TABLE(acpi
, hidma_mgmt_acpi_ids
);
325 static struct platform_driver hidma_mgmt_driver
= {
326 .probe
= hidma_mgmt_probe
,
328 .name
= "hidma-mgmt",
329 .acpi_match_table
= ACPI_PTR(hidma_mgmt_acpi_ids
),
333 module_platform_driver(hidma_mgmt_driver
);
334 MODULE_DESCRIPTION("Qualcomm Technologies HIDMA DMA engine interface");
335 MODULE_LICENSE("GPL v2");