06ed814e1025bb0f16108595fafe5b5251e32b5c
[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(void)
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         return portal;
175 err:
176         if (portal)
177                 qman_free_global_portal(portal);
178         if (q_fd)
179                 close(q_fd);
180         process_portal_unmap(&q_map.addr);
181         kfree(q_pcfg);
182         return NULL;
183 }
184
185 int fsl_qman_fq_portal_init(struct qman_portal *qp)
186 {
187         struct qman_portal *res;
188
189         res = qman_init_portal(qp, NULL, NULL);
190         if (!res) {
191                 pr_err("Qman portal initialisation failed\n");
192                 return -1;
193         }
194
195         return 0;
196 }
197
198 int fsl_qman_fq_portal_destroy(struct qman_portal *qp)
199 {
200         const struct qm_portal_config *cfg;
201         struct dpaa_portal_map addr;
202         int ret;
203
204         cfg = qman_destroy_affine_portal(qp);
205
206         ret = qman_free_global_portal(qp);
207         if (ret)
208                 pr_err("qman_free_global_portal() (%d)\n", ret);
209
210         kfree(qp);
211
212         process_portal_irq_unmap(cfg->irq);
213
214         addr.cena = cfg->addr_virt[DPAA_PORTAL_CE];
215         addr.cinh = cfg->addr_virt[DPAA_PORTAL_CI];
216
217         ret = process_portal_unmap(&addr);
218         if (ret)
219                 pr_err("process_portal_unmap() (%d)\n", ret);
220
221         kfree((void *)cfg);
222
223         return ret;
224 }
225
226 int qman_global_init(void)
227 {
228         const struct device_node *dt_node;
229         size_t lenp;
230         const u32 *chanid;
231         static int ccsr_map_fd;
232         const uint32_t *qman_addr;
233         uint64_t phys_addr;
234         uint64_t regs_size;
235         const u32 *clk;
236
237         static int done;
238
239         if (done)
240                 return -EBUSY;
241
242         /* Use the device-tree to determine IP revision until something better
243          * is devised.
244          */
245         dt_node = of_find_compatible_node(NULL, NULL, "fsl,qman-portal");
246         if (!dt_node) {
247                 pr_err("No qman portals available for any CPU\n");
248                 return -ENODEV;
249         }
250         if (of_device_is_compatible(dt_node, "fsl,qman-portal-1.0") ||
251             of_device_is_compatible(dt_node, "fsl,qman-portal-1.0.0"))
252                 pr_err("QMan rev1.0 on P4080 rev1 is not supported!\n");
253         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-1.1") ||
254                  of_device_is_compatible(dt_node, "fsl,qman-portal-1.1.0"))
255                 qman_ip_rev = QMAN_REV11;
256         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-1.2") ||
257                  of_device_is_compatible(dt_node, "fsl,qman-portal-1.2.0"))
258                 qman_ip_rev = QMAN_REV12;
259         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-2.0") ||
260                  of_device_is_compatible(dt_node, "fsl,qman-portal-2.0.0"))
261                 qman_ip_rev = QMAN_REV20;
262         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-3.0.0") ||
263                  of_device_is_compatible(dt_node, "fsl,qman-portal-3.0.1"))
264                 qman_ip_rev = QMAN_REV30;
265         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.0") ||
266                  of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.1") ||
267                 of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.2") ||
268                 of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.3"))
269                 qman_ip_rev = QMAN_REV31;
270         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-3.2.0") ||
271                  of_device_is_compatible(dt_node, "fsl,qman-portal-3.2.1"))
272                 qman_ip_rev = QMAN_REV32;
273         else
274                 qman_ip_rev = QMAN_REV11;
275
276         if (!qman_ip_rev) {
277                 pr_err("Unknown qman portal version\n");
278                 return -ENODEV;
279         }
280         if ((qman_ip_rev & 0xFF00) >= QMAN_REV30) {
281                 qm_channel_pool1 = QMAN_CHANNEL_POOL1_REV3;
282                 qm_channel_caam = QMAN_CHANNEL_CAAM_REV3;
283                 qm_channel_pme = QMAN_CHANNEL_PME_REV3;
284         }
285
286         dt_node = of_find_compatible_node(NULL, NULL, "fsl,pool-channel-range");
287         if (!dt_node) {
288                 pr_err("No qman pool channel range available\n");
289                 return -ENODEV;
290         }
291         chanid = of_get_property(dt_node, "fsl,pool-channel-range", &lenp);
292         if (!chanid) {
293                 pr_err("Can not get pool-channel-range property\n");
294                 return -EINVAL;
295         }
296
297         /* get ccsr base */
298         dt_node = of_find_compatible_node(NULL, NULL, "fsl,qman");
299         if (!dt_node) {
300                 pr_err("No qman device node available\n");
301                 return -ENODEV;
302         }
303         qman_addr = of_get_address(dt_node, 0, &regs_size, NULL);
304         if (!qman_addr) {
305                 pr_err("of_get_address cannot return qman address\n");
306                 return -EINVAL;
307         }
308         phys_addr = of_translate_address(dt_node, qman_addr);
309         if (!phys_addr) {
310                 pr_err("of_translate_address failed\n");
311                 return -EINVAL;
312         }
313
314         ccsr_map_fd = open("/dev/mem", O_RDWR);
315         if (unlikely(ccsr_map_fd < 0)) {
316                 pr_err("Can not open /dev/mem for qman ccsr map\n");
317                 return ccsr_map_fd;
318         }
319
320         qman_ccsr_map = mmap(NULL, regs_size, PROT_READ | PROT_WRITE,
321                              MAP_SHARED, ccsr_map_fd, phys_addr);
322         if (qman_ccsr_map == MAP_FAILED) {
323                 pr_err("Can not map qman ccsr base\n");
324                 return -EINVAL;
325         }
326
327         clk = of_get_property(dt_node, "clock-frequency", NULL);
328         if (!clk)
329                 pr_warn("Can't find Qman clock frequency\n");
330         else
331                 qman_clk = be32_to_cpu(*clk);
332
333 #ifdef CONFIG_FSL_QMAN_FQ_LOOKUP
334         return qman_setup_fq_lookup_table(CONFIG_FSL_QMAN_FQ_LOOKUP_MAX);
335 #endif
336         return 0;
337 }