net/dpaa: support Rx interrupt handler
[dpdk.git] / drivers / bus / dpaa / base / qbman / qman_driver.c
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2008-2016 Freescale Semiconductor Inc.
4  * Copyright 2017,2019 NXP
5  *
6  */
7
8 #include <fsl_usd.h>
9 #include <process.h>
10 #include "qman_priv.h"
11 #include <sys/ioctl.h>
12 #include <rte_branch_prediction.h>
13
14 /* Global variable containing revision id (even on non-control plane systems
15  * where CCSR isn't available).
16  */
17 u16 qman_ip_rev;
18 u16 qm_channel_pool1 = QMAN_CHANNEL_POOL1;
19 u16 qm_channel_caam = QMAN_CHANNEL_CAAM;
20 u16 qm_channel_pme = QMAN_CHANNEL_PME;
21
22 /* Ccsr map address to access ccsrbased register */
23 static void *qman_ccsr_map;
24 /* The qman clock frequency */
25 static u32 qman_clk;
26
27 static __thread int qmfd = -1;
28 static __thread struct qm_portal_config qpcfg;
29 static __thread struct dpaa_ioctl_portal_map map = {
30         .type = dpaa_portal_qman
31 };
32
33 static int fsl_qman_portal_init(uint32_t index, int is_shared)
34 {
35         struct qman_portal *portal;
36         struct dpaa_ioctl_irq_map irq_map;
37         int ret;
38
39         /* Allocate and map a qman portal */
40         map.index = index;
41         ret = process_portal_map(&map);
42         if (ret) {
43                 error(0, ret, "process_portal_map()");
44                 return ret;
45         }
46         qpcfg.channel = map.channel;
47         qpcfg.pools = map.pools;
48         qpcfg.index = map.index;
49
50         /* Make the portal's cache-[enabled|inhibited] regions */
51         qpcfg.addr_virt[DPAA_PORTAL_CE] = map.addr.cena;
52         qpcfg.addr_virt[DPAA_PORTAL_CI] = map.addr.cinh;
53
54         qmfd = open(QMAN_PORTAL_IRQ_PATH, O_RDONLY);
55         if (qmfd == -1) {
56                 pr_err("QMan irq init failed\n");
57                 process_portal_unmap(&map.addr);
58                 return -EBUSY;
59         }
60
61         qpcfg.is_shared = is_shared;
62         qpcfg.node = NULL;
63         qpcfg.irq = qmfd;
64
65         portal = qman_create_affine_portal(&qpcfg, NULL);
66         if (!portal) {
67                 pr_err("Qman portal initialisation failed (%d)\n",
68                        qpcfg.cpu);
69                 process_portal_unmap(&map.addr);
70                 return -EBUSY;
71         }
72
73         irq_map.type = dpaa_portal_qman;
74         irq_map.portal_cinh = map.addr.cinh;
75         process_portal_irq_map(qmfd, &irq_map);
76         return 0;
77 }
78
79 static int fsl_qman_portal_finish(void)
80 {
81         __maybe_unused const struct qm_portal_config *cfg;
82         int ret;
83
84         process_portal_irq_unmap(qmfd);
85
86         cfg = qman_destroy_affine_portal(NULL);
87         DPAA_BUG_ON(cfg != &qpcfg);
88         ret = process_portal_unmap(&map.addr);
89         if (ret)
90                 error(0, ret, "process_portal_unmap()");
91         return ret;
92 }
93
94 int qman_thread_fd(void)
95 {
96         return qmfd;
97 }
98
99 int qman_thread_init(void)
100 {
101         /* Convert from contiguous/virtual cpu numbering to real cpu when
102          * calling into the code that is dependent on the device naming.
103          */
104         return fsl_qman_portal_init(QBMAN_ANY_PORTAL_IDX, 0);
105 }
106
107 int qman_thread_finish(void)
108 {
109         return fsl_qman_portal_finish();
110 }
111
112 void qman_thread_irq(void)
113 {
114         qbman_invoke_irq(qpcfg.irq);
115
116         /* Now we need to uninhibit interrupts. This is the only code outside
117          * the regular portal driver that manipulates any portal register, so
118          * rather than breaking that encapsulation I am simply hard-coding the
119          * offset to the inhibit register here.
120          */
121         out_be32(qpcfg.addr_virt[DPAA_PORTAL_CI] + 0x36C0, 0);
122 }
123
124 struct qman_portal *fsl_qman_fq_portal_create(int *fd)
125 {
126         struct qman_portal *portal = NULL;
127         struct qm_portal_config *q_pcfg;
128         struct dpaa_ioctl_irq_map irq_map;
129         struct dpaa_ioctl_portal_map q_map = {0};
130         int q_fd = 0, ret;
131
132         q_pcfg = kzalloc((sizeof(struct qm_portal_config)), 0);
133         if (!q_pcfg) {
134                 error(0, -1, "q_pcfg kzalloc failed");
135                 return NULL;
136         }
137
138         /* Allocate and map a qman portal */
139         q_map.type = dpaa_portal_qman;
140         q_map.index = QBMAN_ANY_PORTAL_IDX;
141         ret = process_portal_map(&q_map);
142         if (ret) {
143                 error(0, ret, "process_portal_map()");
144                 kfree(q_pcfg);
145                 return NULL;
146         }
147         q_pcfg->channel = q_map.channel;
148         q_pcfg->pools = q_map.pools;
149         q_pcfg->index = q_map.index;
150
151         /* Make the portal's cache-[enabled|inhibited] regions */
152         q_pcfg->addr_virt[DPAA_PORTAL_CE] = q_map.addr.cena;
153         q_pcfg->addr_virt[DPAA_PORTAL_CI] = q_map.addr.cinh;
154
155         q_fd = open(QMAN_PORTAL_IRQ_PATH, O_RDONLY);
156         if (q_fd == -1) {
157                 pr_err("QMan irq init failed\n");
158                 goto err;
159         }
160
161         q_pcfg->irq = q_fd;
162
163         portal = qman_alloc_global_portal(q_pcfg);
164         if (!portal) {
165                 pr_err("Qman portal initialisation failed (%d)\n",
166                        q_pcfg->cpu);
167                 goto err;
168         }
169
170         irq_map.type = dpaa_portal_qman;
171         irq_map.portal_cinh = q_map.addr.cinh;
172         process_portal_irq_map(q_fd, &irq_map);
173
174         *fd = q_fd;
175         return portal;
176 err:
177         if (portal)
178                 qman_free_global_portal(portal);
179         if (q_fd)
180                 close(q_fd);
181         process_portal_unmap(&q_map.addr);
182         kfree(q_pcfg);
183         return NULL;
184 }
185
186 int fsl_qman_fq_portal_init(struct qman_portal *qp)
187 {
188         struct qman_portal *res;
189
190         res = qman_init_portal(qp, NULL, NULL);
191         if (!res) {
192                 pr_err("Qman portal initialisation failed\n");
193                 return -1;
194         }
195
196         return 0;
197 }
198
199 int fsl_qman_fq_portal_destroy(struct qman_portal *qp)
200 {
201         const struct qm_portal_config *cfg;
202         struct dpaa_portal_map addr;
203         int ret;
204
205         cfg = qman_destroy_affine_portal(qp);
206
207         ret = qman_free_global_portal(qp);
208         if (ret)
209                 pr_err("qman_free_global_portal() (%d)\n", ret);
210
211         kfree(qp);
212
213         process_portal_irq_unmap(cfg->irq);
214
215         addr.cena = cfg->addr_virt[DPAA_PORTAL_CE];
216         addr.cinh = cfg->addr_virt[DPAA_PORTAL_CI];
217
218         ret = process_portal_unmap(&addr);
219         if (ret)
220                 pr_err("process_portal_unmap() (%d)\n", ret);
221
222         kfree((void *)cfg);
223
224         return ret;
225 }
226
227 int qman_global_init(void)
228 {
229         const struct device_node *dt_node;
230         size_t lenp;
231         const u32 *chanid;
232         static int ccsr_map_fd;
233         const uint32_t *qman_addr;
234         uint64_t phys_addr;
235         uint64_t regs_size;
236         const u32 *clk;
237
238         static int done;
239
240         if (done)
241                 return -EBUSY;
242
243         /* Use the device-tree to determine IP revision until something better
244          * is devised.
245          */
246         dt_node = of_find_compatible_node(NULL, NULL, "fsl,qman-portal");
247         if (!dt_node) {
248                 pr_err("No qman portals available for any CPU\n");
249                 return -ENODEV;
250         }
251         if (of_device_is_compatible(dt_node, "fsl,qman-portal-1.0") ||
252             of_device_is_compatible(dt_node, "fsl,qman-portal-1.0.0"))
253                 pr_err("QMan rev1.0 on P4080 rev1 is not supported!\n");
254         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-1.1") ||
255                  of_device_is_compatible(dt_node, "fsl,qman-portal-1.1.0"))
256                 qman_ip_rev = QMAN_REV11;
257         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-1.2") ||
258                  of_device_is_compatible(dt_node, "fsl,qman-portal-1.2.0"))
259                 qman_ip_rev = QMAN_REV12;
260         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-2.0") ||
261                  of_device_is_compatible(dt_node, "fsl,qman-portal-2.0.0"))
262                 qman_ip_rev = QMAN_REV20;
263         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-3.0.0") ||
264                  of_device_is_compatible(dt_node, "fsl,qman-portal-3.0.1"))
265                 qman_ip_rev = QMAN_REV30;
266         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.0") ||
267                  of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.1") ||
268                 of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.2") ||
269                 of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.3"))
270                 qman_ip_rev = QMAN_REV31;
271         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-3.2.0") ||
272                  of_device_is_compatible(dt_node, "fsl,qman-portal-3.2.1"))
273                 qman_ip_rev = QMAN_REV32;
274         else
275                 qman_ip_rev = QMAN_REV11;
276
277         if (!qman_ip_rev) {
278                 pr_err("Unknown qman portal version\n");
279                 return -ENODEV;
280         }
281         if ((qman_ip_rev & 0xFF00) >= QMAN_REV30) {
282                 qm_channel_pool1 = QMAN_CHANNEL_POOL1_REV3;
283                 qm_channel_caam = QMAN_CHANNEL_CAAM_REV3;
284                 qm_channel_pme = QMAN_CHANNEL_PME_REV3;
285         }
286
287         dt_node = of_find_compatible_node(NULL, NULL, "fsl,pool-channel-range");
288         if (!dt_node) {
289                 pr_err("No qman pool channel range available\n");
290                 return -ENODEV;
291         }
292         chanid = of_get_property(dt_node, "fsl,pool-channel-range", &lenp);
293         if (!chanid) {
294                 pr_err("Can not get pool-channel-range property\n");
295                 return -EINVAL;
296         }
297
298         /* get ccsr base */
299         dt_node = of_find_compatible_node(NULL, NULL, "fsl,qman");
300         if (!dt_node) {
301                 pr_err("No qman device node available\n");
302                 return -ENODEV;
303         }
304         qman_addr = of_get_address(dt_node, 0, &regs_size, NULL);
305         if (!qman_addr) {
306                 pr_err("of_get_address cannot return qman address\n");
307                 return -EINVAL;
308         }
309         phys_addr = of_translate_address(dt_node, qman_addr);
310         if (!phys_addr) {
311                 pr_err("of_translate_address failed\n");
312                 return -EINVAL;
313         }
314
315         ccsr_map_fd = open("/dev/mem", O_RDWR);
316         if (unlikely(ccsr_map_fd < 0)) {
317                 pr_err("Can not open /dev/mem for qman ccsr map\n");
318                 return ccsr_map_fd;
319         }
320
321         qman_ccsr_map = mmap(NULL, regs_size, PROT_READ | PROT_WRITE,
322                              MAP_SHARED, ccsr_map_fd, phys_addr);
323         if (qman_ccsr_map == MAP_FAILED) {
324                 pr_err("Can not map qman ccsr base\n");
325                 return -EINVAL;
326         }
327
328         clk = of_get_property(dt_node, "clock-frequency", NULL);
329         if (!clk)
330                 pr_warn("Can't find Qman clock frequency\n");
331         else
332                 qman_clk = be32_to_cpu(*clk);
333
334 #ifdef CONFIG_FSL_QMAN_FQ_LOOKUP
335         return qman_setup_fq_lookup_table(CONFIG_FSL_QMAN_FQ_LOOKUP_MAX);
336 #endif
337         return 0;
338 }