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