New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / raw / ifpga_rawdev / base / ifpga_port.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include "ifpga_feature_dev.h"
6
7 int port_get_prop(struct ifpga_port_hw *port, struct feature_prop *prop)
8 {
9         struct feature *feature;
10
11         if (!port)
12                 return -ENOENT;
13
14         feature = get_port_feature_by_id(port, prop->feature_id);
15
16         if (feature && feature->ops && feature->ops->get_prop)
17                 return feature->ops->get_prop(feature, prop);
18
19         return -ENOENT;
20 }
21
22 int port_set_prop(struct ifpga_port_hw *port, struct feature_prop *prop)
23 {
24         struct feature *feature;
25
26         if (!port)
27                 return -ENOENT;
28
29         feature = get_port_feature_by_id(port, prop->feature_id);
30
31         if (feature && feature->ops && feature->ops->set_prop)
32                 return feature->ops->set_prop(feature, prop);
33
34         return -ENOENT;
35 }
36
37 int port_set_irq(struct ifpga_port_hw *port, u32 feature_id, void *irq_set)
38 {
39         struct feature *feature;
40
41         if (!port)
42                 return -ENOENT;
43
44         feature = get_port_feature_by_id(port, feature_id);
45
46         if (feature && feature->ops && feature->ops->set_irq)
47                 return feature->ops->set_irq(feature, irq_set);
48
49         return -ENOENT;
50 }
51
52 static int port_get_revision(struct ifpga_port_hw *port, u64 *revision)
53 {
54         struct feature_port_header *port_hdr
55                 = get_port_feature_ioaddr_by_index(port,
56                                                    PORT_FEATURE_ID_HEADER);
57         struct feature_header header;
58
59         header.csr = readq(&port_hdr->header);
60
61         *revision = header.revision;
62
63         return 0;
64 }
65
66 static int port_get_portidx(struct ifpga_port_hw *port, u64 *idx)
67 {
68         struct feature_port_header *port_hdr;
69         struct feature_port_capability capability;
70
71         port_hdr = get_port_feature_ioaddr_by_index(port,
72                                                     PORT_FEATURE_ID_HEADER);
73
74         capability.csr = readq(&port_hdr->capability);
75         *idx = capability.port_number;
76
77         return 0;
78 }
79
80 static int port_get_latency_tolerance(struct ifpga_port_hw *port, u64 *val)
81 {
82         struct feature_port_header *port_hdr;
83         struct feature_port_control control;
84
85         port_hdr = get_port_feature_ioaddr_by_index(port,
86                                                     PORT_FEATURE_ID_HEADER);
87
88         control.csr = readq(&port_hdr->control);
89         *val = control.latency_tolerance;
90
91         return 0;
92 }
93
94 static int port_get_ap1_event(struct ifpga_port_hw *port, u64 *val)
95 {
96         struct feature_port_header *port_hdr;
97         struct feature_port_status status;
98
99         port_hdr = get_port_feature_ioaddr_by_index(port,
100                                                     PORT_FEATURE_ID_HEADER);
101
102         spinlock_lock(&port->lock);
103         status.csr = readq(&port_hdr->status);
104         spinlock_unlock(&port->lock);
105
106         *val = status.ap1_event;
107
108         return 0;
109 }
110
111 static int port_set_ap1_event(struct ifpga_port_hw *port, u64 val)
112 {
113         struct feature_port_header *port_hdr;
114         struct feature_port_status status;
115
116         port_hdr = get_port_feature_ioaddr_by_index(port,
117                                                     PORT_FEATURE_ID_HEADER);
118
119         spinlock_lock(&port->lock);
120         status.csr = readq(&port_hdr->status);
121         status.ap1_event = val;
122         writeq(status.csr, &port_hdr->status);
123         spinlock_unlock(&port->lock);
124
125         return 0;
126 }
127
128 static int port_get_ap2_event(struct ifpga_port_hw *port, u64 *val)
129 {
130         struct feature_port_header *port_hdr;
131         struct feature_port_status status;
132
133         port_hdr = get_port_feature_ioaddr_by_index(port,
134                                                     PORT_FEATURE_ID_HEADER);
135
136         spinlock_lock(&port->lock);
137         status.csr = readq(&port_hdr->status);
138         spinlock_unlock(&port->lock);
139
140         *val = status.ap2_event;
141
142         return 0;
143 }
144
145 static int port_set_ap2_event(struct ifpga_port_hw *port, u64 val)
146 {
147         struct feature_port_header *port_hdr;
148         struct feature_port_status status;
149
150         port_hdr = get_port_feature_ioaddr_by_index(port,
151                                                     PORT_FEATURE_ID_HEADER);
152
153         spinlock_lock(&port->lock);
154         status.csr = readq(&port_hdr->status);
155         status.ap2_event = val;
156         writeq(status.csr, &port_hdr->status);
157         spinlock_unlock(&port->lock);
158
159         return 0;
160 }
161
162 static int port_get_power_state(struct ifpga_port_hw *port, u64 *val)
163 {
164         struct feature_port_header *port_hdr;
165         struct feature_port_status status;
166
167         port_hdr = get_port_feature_ioaddr_by_index(port,
168                                                     PORT_FEATURE_ID_HEADER);
169
170         spinlock_lock(&port->lock);
171         status.csr = readq(&port_hdr->status);
172         spinlock_unlock(&port->lock);
173
174         *val = status.power_state;
175
176         return 0;
177 }
178
179 static int port_get_userclk_freqcmd(struct ifpga_port_hw *port, u64 *val)
180 {
181         struct feature_port_header *port_hdr;
182
183         port_hdr = get_port_feature_ioaddr_by_index(port,
184                                                     PORT_FEATURE_ID_HEADER);
185
186         spinlock_lock(&port->lock);
187         *val = readq(&port_hdr->user_clk_freq_cmd0);
188         spinlock_unlock(&port->lock);
189
190         return 0;
191 }
192
193 static int port_set_userclk_freqcmd(struct ifpga_port_hw *port, u64 val)
194 {
195         struct feature_port_header *port_hdr;
196
197         port_hdr = get_port_feature_ioaddr_by_index(port,
198                                                     PORT_FEATURE_ID_HEADER);
199
200         spinlock_lock(&port->lock);
201         writeq(val, &port_hdr->user_clk_freq_cmd0);
202         spinlock_unlock(&port->lock);
203
204         return 0;
205 }
206
207 static int port_get_userclk_freqcntrcmd(struct ifpga_port_hw *port, u64 *val)
208 {
209         struct feature_port_header *port_hdr;
210
211         port_hdr = get_port_feature_ioaddr_by_index(port,
212                                                     PORT_FEATURE_ID_HEADER);
213
214         spinlock_lock(&port->lock);
215         *val = readq(&port_hdr->user_clk_freq_cmd1);
216         spinlock_unlock(&port->lock);
217
218         return 0;
219 }
220
221 static int port_set_userclk_freqcntrcmd(struct ifpga_port_hw *port, u64 val)
222 {
223         struct feature_port_header *port_hdr;
224
225         port_hdr = get_port_feature_ioaddr_by_index(port,
226                                                     PORT_FEATURE_ID_HEADER);
227
228         spinlock_lock(&port->lock);
229         writeq(val, &port_hdr->user_clk_freq_cmd1);
230         spinlock_unlock(&port->lock);
231
232         return 0;
233 }
234
235 static int port_get_userclk_freqsts(struct ifpga_port_hw *port, u64 *val)
236 {
237         struct feature_port_header *port_hdr;
238
239         port_hdr = get_port_feature_ioaddr_by_index(port,
240                                                     PORT_FEATURE_ID_HEADER);
241
242         spinlock_lock(&port->lock);
243         *val = readq(&port_hdr->user_clk_freq_sts0);
244         spinlock_unlock(&port->lock);
245
246         return 0;
247 }
248
249 static int port_get_userclk_freqcntrsts(struct ifpga_port_hw *port, u64 *val)
250 {
251         struct feature_port_header *port_hdr;
252
253         port_hdr = get_port_feature_ioaddr_by_index(port,
254                                                     PORT_FEATURE_ID_HEADER);
255
256         spinlock_lock(&port->lock);
257         *val = readq(&port_hdr->user_clk_freq_sts1);
258         spinlock_unlock(&port->lock);
259
260         return 0;
261 }
262
263 static int port_hdr_init(struct feature *feature)
264 {
265         struct ifpga_port_hw *port = feature->parent;
266
267         dev_info(NULL, "port hdr Init.\n");
268
269         fpga_port_reset(port);
270
271         return 0;
272 }
273
274 static void port_hdr_uinit(struct feature *feature)
275 {
276         UNUSED(feature);
277
278         dev_info(NULL, "port hdr uinit.\n");
279 }
280
281 static int port_hdr_get_prop(struct feature *feature, struct feature_prop *prop)
282 {
283         struct ifpga_port_hw *port = feature->parent;
284
285         switch (prop->prop_id) {
286         case PORT_HDR_PROP_REVISION:
287                 return port_get_revision(port, &prop->data);
288         case PORT_HDR_PROP_PORTIDX:
289                 return port_get_portidx(port, &prop->data);
290         case PORT_HDR_PROP_LATENCY_TOLERANCE:
291                 return port_get_latency_tolerance(port, &prop->data);
292         case PORT_HDR_PROP_AP1_EVENT:
293                 return port_get_ap1_event(port, &prop->data);
294         case PORT_HDR_PROP_AP2_EVENT:
295                 return port_get_ap2_event(port, &prop->data);
296         case PORT_HDR_PROP_POWER_STATE:
297                 return port_get_power_state(port, &prop->data);
298         case PORT_HDR_PROP_USERCLK_FREQCMD:
299                 return port_get_userclk_freqcmd(port, &prop->data);
300         case PORT_HDR_PROP_USERCLK_FREQCNTRCMD:
301                 return port_get_userclk_freqcntrcmd(port, &prop->data);
302         case PORT_HDR_PROP_USERCLK_FREQSTS:
303                 return port_get_userclk_freqsts(port, &prop->data);
304         case PORT_HDR_PROP_USERCLK_CNTRSTS:
305                 return port_get_userclk_freqcntrsts(port, &prop->data);
306         }
307
308         return -ENOENT;
309 }
310
311 static int port_hdr_set_prop(struct feature *feature, struct feature_prop *prop)
312 {
313         struct ifpga_port_hw *port = feature->parent;
314
315         switch (prop->prop_id) {
316         case PORT_HDR_PROP_AP1_EVENT:
317                 return port_set_ap1_event(port, prop->data);
318         case PORT_HDR_PROP_AP2_EVENT:
319                 return port_set_ap2_event(port, prop->data);
320         case PORT_HDR_PROP_USERCLK_FREQCMD:
321                 return port_set_userclk_freqcmd(port, prop->data);
322         case PORT_HDR_PROP_USERCLK_FREQCNTRCMD:
323                 return port_set_userclk_freqcntrcmd(port, prop->data);
324         }
325
326         return -ENOENT;
327 }
328
329 struct feature_ops ifpga_rawdev_port_hdr_ops = {
330         .init = port_hdr_init,
331         .uinit = port_hdr_uinit,
332         .get_prop = port_hdr_get_prop,
333         .set_prop = port_hdr_set_prop,
334 };
335
336 static int port_stp_init(struct feature *feature)
337 {
338         struct ifpga_port_hw *port = feature->parent;
339
340         dev_info(NULL, "port stp Init.\n");
341
342         spinlock_lock(&port->lock);
343         port->stp_addr = feature->addr;
344         port->stp_size = feature->size;
345         spinlock_unlock(&port->lock);
346
347         return 0;
348 }
349
350 static void port_stp_uinit(struct feature *feature)
351 {
352         UNUSED(feature);
353
354         dev_info(NULL, "port stp uinit.\n");
355 }
356
357 struct feature_ops ifpga_rawdev_port_stp_ops = {
358         .init = port_stp_init,
359         .uinit = port_stp_uinit,
360 };
361
362 static int port_uint_init(struct feature *feature)
363 {
364         struct ifpga_port_hw *port = feature->parent;
365
366         dev_info(NULL, "PORT UINT Init.\n");
367
368         spinlock_lock(&port->lock);
369         if (feature->ctx_num) {
370                 port->capability |= FPGA_PORT_CAP_UAFU_IRQ;
371                 port->num_uafu_irqs = feature->ctx_num;
372         }
373         spinlock_unlock(&port->lock);
374
375         return 0;
376 }
377
378 static void port_uint_uinit(struct feature *feature)
379 {
380         UNUSED(feature);
381
382         dev_info(NULL, "PORT UINT UInit.\n");
383 }
384
385 struct feature_ops ifpga_rawdev_port_uint_ops = {
386         .init = port_uint_init,
387         .uinit = port_uint_uinit,
388 };