New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / bus / dpaa / base / qbman / bman_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 <rte_branch_prediction.h>
42
43 #include <fsl_usd.h>
44 #include <process.h>
45 #include "bman_priv.h"
46 #include <sys/ioctl.h>
47
48 /*
49  * Global variables of the max portal/pool number this bman version supported
50  */
51 u16 bman_ip_rev;
52 u16 bman_pool_max;
53 void *bman_ccsr_map;
54
55 /*****************/
56 /* Portal driver */
57 /*****************/
58
59 static __thread int fd = -1;
60 static __thread struct bm_portal_config pcfg;
61 static __thread struct dpaa_ioctl_portal_map map = {
62         .type = dpaa_portal_bman
63 };
64
65 static int fsl_bman_portal_init(uint32_t idx, int is_shared)
66 {
67         cpu_set_t cpuset;
68         struct bman_portal *portal;
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");
84                                 return -EINVAL;
85                         }
86                         pcfg.cpu = loop;
87                 }
88         if (pcfg.cpu == -1) {
89                 pr_err("Bug in getaffinity handling!");
90                 return -EINVAL;
91         }
92         /* Allocate and map a bman portal */
93         map.index = idx;
94         ret = process_portal_map(&map);
95         if (ret) {
96                 error(0, ret, "process_portal_map()");
97                 return ret;
98         }
99         /* Make the portal's cache-[enabled|inhibited] regions */
100         pcfg.addr_virt[DPAA_PORTAL_CE] = map.addr.cena;
101         pcfg.addr_virt[DPAA_PORTAL_CI] = map.addr.cinh;
102         pcfg.is_shared = is_shared;
103         pcfg.index = map.index;
104         bman_depletion_fill(&pcfg.mask);
105
106         fd = open(BMAN_PORTAL_IRQ_PATH, O_RDONLY);
107         if (fd == -1) {
108                 pr_err("BMan irq init failed");
109                 process_portal_unmap(&map.addr);
110                 return -EBUSY;
111         }
112         /* Use the IRQ FD as a unique IRQ number */
113         pcfg.irq = fd;
114
115         portal = bman_create_affine_portal(&pcfg);
116         if (!portal) {
117                 pr_err("Bman portal initialisation failed (%d)",
118                        pcfg.cpu);
119                 process_portal_unmap(&map.addr);
120                 return -EBUSY;
121         }
122
123         /* Set the IRQ number */
124         irq_map.type = dpaa_portal_bman;
125         irq_map.portal_cinh = map.addr.cinh;
126         process_portal_irq_map(fd, &irq_map);
127         return 0;
128 }
129
130 static int fsl_bman_portal_finish(void)
131 {
132         __maybe_unused const struct bm_portal_config *cfg;
133         int ret;
134
135         process_portal_irq_unmap(fd);
136
137         cfg = bman_destroy_affine_portal();
138         DPAA_BUG_ON(cfg != &pcfg);
139         ret = process_portal_unmap(&map.addr);
140         if (ret)
141                 error(0, ret, "process_portal_unmap()");
142         return ret;
143 }
144
145 int bman_thread_init(void)
146 {
147         /* Convert from contiguous/virtual cpu numbering to real cpu when
148          * calling into the code that is dependent on the device naming.
149          */
150         return fsl_bman_portal_init(QBMAN_ANY_PORTAL_IDX, 0);
151 }
152
153 int bman_thread_finish(void)
154 {
155         return fsl_bman_portal_finish();
156 }
157
158 void bman_thread_irq(void)
159 {
160         qbman_invoke_irq(pcfg.irq);
161         /* Now we need to uninhibit interrupts. This is the only code outside
162          * the regular portal driver that manipulates any portal register, so
163          * rather than breaking that encapsulation I am simply hard-coding the
164          * offset to the inhibit register here.
165          */
166         out_be32(pcfg.addr_virt[DPAA_PORTAL_CI] + 0xe0c, 0);
167 }
168
169 int bman_init_ccsr(const struct device_node *node)
170 {
171         static int ccsr_map_fd;
172         uint64_t phys_addr;
173         const uint32_t *bman_addr;
174         uint64_t regs_size;
175
176         bman_addr = of_get_address(node, 0, &regs_size, NULL);
177         if (!bman_addr) {
178                 pr_err("of_get_address cannot return BMan address");
179                 return -EINVAL;
180         }
181         phys_addr = of_translate_address(node, bman_addr);
182         if (!phys_addr) {
183                 pr_err("of_translate_address failed");
184                 return -EINVAL;
185         }
186
187         ccsr_map_fd = open(BMAN_CCSR_MAP, O_RDWR);
188         if (unlikely(ccsr_map_fd < 0)) {
189                 pr_err("Can not open /dev/mem for BMan CCSR map");
190                 return ccsr_map_fd;
191         }
192
193         bman_ccsr_map = mmap(NULL, regs_size, PROT_READ |
194                              PROT_WRITE, MAP_SHARED, ccsr_map_fd, phys_addr);
195         if (bman_ccsr_map == MAP_FAILED) {
196                 pr_err("Can not map BMan CCSR base Bman: "
197                        "0x%x Phys: 0x%lx size 0x%lx",
198                        *bman_addr, phys_addr, regs_size);
199                 return -EINVAL;
200         }
201
202         return 0;
203 }
204
205 int bman_global_init(void)
206 {
207         const struct device_node *dt_node;
208         static int done;
209
210         if (done)
211                 return -EBUSY;
212         /* Use the device-tree to determine IP revision until something better
213          * is devised.
214          */
215         dt_node = of_find_compatible_node(NULL, NULL, "fsl,bman-portal");
216         if (!dt_node) {
217                 pr_err("No bman portals available for any CPU\n");
218                 return -ENODEV;
219         }
220         if (of_device_is_compatible(dt_node, "fsl,bman-portal-1.0") ||
221             of_device_is_compatible(dt_node, "fsl,bman-portal-1.0.0")) {
222                 bman_ip_rev = BMAN_REV10;
223                 bman_pool_max = 64;
224         } else if (of_device_is_compatible(dt_node, "fsl,bman-portal-2.0") ||
225                 of_device_is_compatible(dt_node, "fsl,bman-portal-2.0.8")) {
226                 bman_ip_rev = BMAN_REV20;
227                 bman_pool_max = 8;
228         } else if (of_device_is_compatible(dt_node, "fsl,bman-portal-2.1.0") ||
229                 of_device_is_compatible(dt_node, "fsl,bman-portal-2.1.1") ||
230                 of_device_is_compatible(dt_node, "fsl,bman-portal-2.1.2") ||
231                 of_device_is_compatible(dt_node, "fsl,bman-portal-2.1.3")) {
232                 bman_ip_rev = BMAN_REV21;
233                 bman_pool_max = 64;
234         } else {
235                 pr_warn("unknown BMan version in portal node,default "
236                         "to rev1.0");
237                 bman_ip_rev = BMAN_REV10;
238                 bman_pool_max = 64;
239         }
240
241         if (!bman_ip_rev) {
242                 pr_err("Unknown bman portal version\n");
243                 return -ENODEV;
244         }
245         {
246                 const struct device_node *dn = of_find_compatible_node(NULL,
247                                                         NULL, "fsl,bman");
248                 if (!dn)
249                         pr_err("No bman device node available");
250
251                 if (bman_init_ccsr(dn))
252                         pr_err("BMan CCSR map failed.");
253         }
254
255         done = 1;
256         return 0;
257 }
258
259 #define BMAN_POOL_CONTENT(n) (0x0600 + ((n) * 0x04))
260 u32 bm_pool_free_buffers(u32 bpid)
261 {
262         return in_be32(bman_ccsr_map + BMAN_POOL_CONTENT(bpid));
263 }
264
265 static u32 __generate_thresh(u32 val, int roundup)
266 {
267         u32 e = 0;      /* co-efficient, exponent */
268         int oddbit = 0;
269
270         while (val > 0xff) {
271                 oddbit = val & 1;
272                 val >>= 1;
273                 e++;
274                 if (roundup && oddbit)
275                         val++;
276         }
277         DPAA_ASSERT(e < 0x10);
278         return (val | (e << 8));
279 }
280
281 #define POOL_SWDET(n)       (0x0000 + ((n) * 0x04))
282 #define POOL_HWDET(n)       (0x0100 + ((n) * 0x04))
283 #define POOL_SWDXT(n)       (0x0200 + ((n) * 0x04))
284 #define POOL_HWDXT(n)       (0x0300 + ((n) * 0x04))
285 int bm_pool_set(u32 bpid, const u32 *thresholds)
286 {
287         if (!bman_ccsr_map)
288                 return -ENODEV;
289         if (bpid >= bman_pool_max)
290                 return -EINVAL;
291         out_be32(bman_ccsr_map + POOL_SWDET(bpid),
292                  __generate_thresh(thresholds[0], 0));
293         out_be32(bman_ccsr_map + POOL_SWDXT(bpid),
294                  __generate_thresh(thresholds[1], 1));
295         out_be32(bman_ccsr_map + POOL_HWDET(bpid),
296                  __generate_thresh(thresholds[2], 0));
297         out_be32(bman_ccsr_map + POOL_HWDXT(bpid),
298                  __generate_thresh(thresholds[3], 1));
299         return 0;
300 }
301
302 #define BMAN_LOW_DEFAULT_THRESH         0x40
303 #define BMAN_HIGH_DEFAULT_THRESH                0x80
304 int bm_pool_set_hw_threshold(u32 bpid, const u32 low_thresh,
305                              const u32 high_thresh)
306 {
307         if (!bman_ccsr_map)
308                 return -ENODEV;
309         if (bpid >= bman_pool_max)
310                 return -EINVAL;
311         if (low_thresh && high_thresh) {
312                 out_be32(bman_ccsr_map + POOL_HWDET(bpid),
313                          __generate_thresh(low_thresh, 0));
314                 out_be32(bman_ccsr_map + POOL_HWDXT(bpid),
315                          __generate_thresh(high_thresh, 1));
316         } else {
317                 out_be32(bman_ccsr_map + POOL_HWDET(bpid),
318                          __generate_thresh(BMAN_LOW_DEFAULT_THRESH, 0));
319                 out_be32(bman_ccsr_map + POOL_HWDXT(bpid),
320                          __generate_thresh(BMAN_HIGH_DEFAULT_THRESH, 1));
321         }
322         return 0;
323 }