80dde2028bc7af5aa6a8c612668532a7582894de
[dpdk.git] / drivers / bus / dpaa / base / qbman / qman_driver.c
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license. When using or
3  * redistributing this file, you may do so under either license.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2008-2016 Freescale Semiconductor Inc.
8  * Copyright 2017 NXP.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the above-listed copyright holders nor the
18  * names of any contributors may be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  *   GPL LICENSE SUMMARY
22  *
23  * ALTERNATIVELY, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") as published by the Free Software
25  * Foundation, either version 2 of that License or (at your option) any
26  * later version.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 #include <fsl_usd.h>
42 #include <process.h>
43 #include "qman_priv.h"
44 #include <sys/ioctl.h>
45 #include <rte_branch_prediction.h>
46
47 /* Global variable containing revision id (even on non-control plane systems
48  * where CCSR isn't available).
49  */
50 u16 qman_ip_rev;
51 u16 qm_channel_pool1 = QMAN_CHANNEL_POOL1;
52 u16 qm_channel_caam = QMAN_CHANNEL_CAAM;
53 u16 qm_channel_pme = QMAN_CHANNEL_PME;
54
55 /* Ccsr map address to access ccsrbased register */
56 void *qman_ccsr_map;
57 /* The qman clock frequency */
58 u32 qman_clk;
59
60 static __thread int fd = -1;
61 static __thread struct qm_portal_config pcfg;
62 static __thread struct dpaa_ioctl_portal_map map = {
63         .type = dpaa_portal_qman
64 };
65
66 static int fsl_qman_portal_init(uint32_t index, int is_shared)
67 {
68         cpu_set_t cpuset;
69         int loop, ret;
70         struct dpaa_ioctl_irq_map irq_map;
71
72         /* Verify the thread's cpu-affinity */
73         ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t),
74                                      &cpuset);
75         if (ret) {
76                 error(0, ret, "pthread_getaffinity_np()");
77                 return ret;
78         }
79         pcfg.cpu = -1;
80         for (loop = 0; loop < CPU_SETSIZE; loop++)
81                 if (CPU_ISSET(loop, &cpuset)) {
82                         if (pcfg.cpu != -1) {
83                                 pr_err("Thread is not affine to 1 cpu\n");
84                                 return -EINVAL;
85                         }
86                         pcfg.cpu = loop;
87                 }
88         if (pcfg.cpu == -1) {
89                 pr_err("Bug in getaffinity handling!\n");
90                 return -EINVAL;
91         }
92
93         /* Allocate and map a qman portal */
94         map.index = index;
95         ret = process_portal_map(&map);
96         if (ret) {
97                 error(0, ret, "process_portal_map()");
98                 return ret;
99         }
100         pcfg.channel = map.channel;
101         pcfg.pools = map.pools;
102         pcfg.index = map.index;
103
104         /* Make the portal's cache-[enabled|inhibited] regions */
105         pcfg.addr_virt[DPAA_PORTAL_CE] = map.addr.cena;
106         pcfg.addr_virt[DPAA_PORTAL_CI] = map.addr.cinh;
107
108         fd = open(QMAN_PORTAL_IRQ_PATH, O_RDONLY);
109         if (fd == -1) {
110                 pr_err("QMan irq init failed\n");
111                 process_portal_unmap(&map.addr);
112                 return -EBUSY;
113         }
114
115         pcfg.is_shared = is_shared;
116         pcfg.node = NULL;
117         pcfg.irq = fd;
118
119         irq_map.type = dpaa_portal_qman;
120         irq_map.portal_cinh = map.addr.cinh;
121         process_portal_irq_map(fd, &irq_map);
122         return 0;
123 }
124
125 static int fsl_qman_portal_finish(void)
126 {
127         int ret;
128
129         process_portal_irq_unmap(fd);
130
131         ret = process_portal_unmap(&map.addr);
132         if (ret)
133                 error(0, ret, "process_portal_unmap()");
134         return ret;
135 }
136
137 int qman_thread_init(void)
138 {
139         /* Convert from contiguous/virtual cpu numbering to real cpu when
140          * calling into the code that is dependent on the device naming.
141          */
142         return fsl_qman_portal_init(QBMAN_ANY_PORTAL_IDX, 0);
143 }
144
145 int qman_thread_finish(void)
146 {
147         return fsl_qman_portal_finish();
148 }
149
150 void qman_thread_irq(void)
151 {
152         qbman_invoke_irq(pcfg.irq);
153
154         /* Now we need to uninhibit interrupts. This is the only code outside
155          * the regular portal driver that manipulates any portal register, so
156          * rather than breaking that encapsulation I am simply hard-coding the
157          * offset to the inhibit register here.
158          */
159         out_be32(pcfg.addr_virt[DPAA_PORTAL_CI] + 0xe0c, 0);
160 }
161
162 int qman_global_init(void)
163 {
164         const struct device_node *dt_node;
165         int ret = 0;
166         size_t lenp;
167         const u32 *chanid;
168         static int ccsr_map_fd;
169         const uint32_t *qman_addr;
170         uint64_t phys_addr;
171         uint64_t regs_size;
172         const u32 *clk;
173
174         static int done;
175
176         if (done)
177                 return -EBUSY;
178
179         /* Use the device-tree to determine IP revision until something better
180          * is devised.
181          */
182         dt_node = of_find_compatible_node(NULL, NULL, "fsl,qman-portal");
183         if (!dt_node) {
184                 pr_err("No qman portals available for any CPU\n");
185                 return -ENODEV;
186         }
187         if (of_device_is_compatible(dt_node, "fsl,qman-portal-1.0") ||
188             of_device_is_compatible(dt_node, "fsl,qman-portal-1.0.0"))
189                 pr_err("QMan rev1.0 on P4080 rev1 is not supported!\n");
190         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-1.1") ||
191                  of_device_is_compatible(dt_node, "fsl,qman-portal-1.1.0"))
192                 qman_ip_rev = QMAN_REV11;
193         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-1.2") ||
194                  of_device_is_compatible(dt_node, "fsl,qman-portal-1.2.0"))
195                 qman_ip_rev = QMAN_REV12;
196         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-2.0") ||
197                  of_device_is_compatible(dt_node, "fsl,qman-portal-2.0.0"))
198                 qman_ip_rev = QMAN_REV20;
199         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-3.0.0") ||
200                  of_device_is_compatible(dt_node, "fsl,qman-portal-3.0.1"))
201                 qman_ip_rev = QMAN_REV30;
202         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.0") ||
203                  of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.1") ||
204                 of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.2") ||
205                 of_device_is_compatible(dt_node, "fsl,qman-portal-3.1.3"))
206                 qman_ip_rev = QMAN_REV31;
207         else if (of_device_is_compatible(dt_node, "fsl,qman-portal-3.2.0") ||
208                  of_device_is_compatible(dt_node, "fsl,qman-portal-3.2.1"))
209                 qman_ip_rev = QMAN_REV32;
210         else
211                 qman_ip_rev = QMAN_REV11;
212
213         if (!qman_ip_rev) {
214                 pr_err("Unknown qman portal version\n");
215                 return -ENODEV;
216         }
217         if ((qman_ip_rev & 0xFF00) >= QMAN_REV30) {
218                 qm_channel_pool1 = QMAN_CHANNEL_POOL1_REV3;
219                 qm_channel_caam = QMAN_CHANNEL_CAAM_REV3;
220                 qm_channel_pme = QMAN_CHANNEL_PME_REV3;
221         }
222
223         dt_node = of_find_compatible_node(NULL, NULL, "fsl,pool-channel-range");
224         if (!dt_node) {
225                 pr_err("No qman pool channel range available\n");
226                 return -ENODEV;
227         }
228         chanid = of_get_property(dt_node, "fsl,pool-channel-range", &lenp);
229         if (!chanid) {
230                 pr_err("Can not get pool-channel-range property\n");
231                 return -EINVAL;
232         }
233
234         /* get ccsr base */
235         dt_node = of_find_compatible_node(NULL, NULL, "fsl,qman");
236         if (!dt_node) {
237                 pr_err("No qman device node available\n");
238                 return -ENODEV;
239         }
240         qman_addr = of_get_address(dt_node, 0, &regs_size, NULL);
241         if (!qman_addr) {
242                 pr_err("of_get_address cannot return qman address\n");
243                 return -EINVAL;
244         }
245         phys_addr = of_translate_address(dt_node, qman_addr);
246         if (!phys_addr) {
247                 pr_err("of_translate_address failed\n");
248                 return -EINVAL;
249         }
250
251         ccsr_map_fd = open("/dev/mem", O_RDWR);
252         if (unlikely(ccsr_map_fd < 0)) {
253                 pr_err("Can not open /dev/mem for qman ccsr map\n");
254                 return ccsr_map_fd;
255         }
256
257         qman_ccsr_map = mmap(NULL, regs_size, PROT_READ | PROT_WRITE,
258                              MAP_SHARED, ccsr_map_fd, phys_addr);
259         if (qman_ccsr_map == MAP_FAILED) {
260                 pr_err("Can not map qman ccsr base\n");
261                 return -EINVAL;
262         }
263
264         clk = of_get_property(dt_node, "clock-frequency", NULL);
265         if (!clk)
266                 pr_warn("Can't find Qman clock frequency\n");
267         else
268                 qman_clk = be32_to_cpu(*clk);
269
270         return ret;
271 }