blob: 1e19160281dd35dcf2b6e96bfb903ef97d8d9dc3 [file] [log] [blame]
Alex Elder650d1602020-03-05 22:28:21 -06001// SPDX-License-Identifier: GPL-2.0
2
3/* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2020 Linaro Ltd.
5 */
6
7#include <linux/types.h>
8#include <linux/bits.h>
9#include <linux/bitfield.h>
10#include <linux/mutex.h>
11#include <linux/completion.h>
12#include <linux/io.h>
13#include <linux/bug.h>
14#include <linux/interrupt.h>
15#include <linux/platform_device.h>
16#include <linux/netdevice.h>
17
18#include "gsi.h"
19#include "gsi_reg.h"
20#include "gsi_private.h"
21#include "gsi_trans.h"
22#include "ipa_gsi.h"
23#include "ipa_data.h"
Alex Elder1d0c09d2020-11-02 11:53:55 -060024#include "ipa_version.h"
Alex Elder650d1602020-03-05 22:28:21 -060025
26/**
27 * DOC: The IPA Generic Software Interface
28 *
29 * The generic software interface (GSI) is an integral component of the IPA,
30 * providing a well-defined communication layer between the AP subsystem
31 * and the IPA core. The modem uses the GSI layer as well.
32 *
33 * -------- ---------
34 * | | | |
35 * | AP +<---. .----+ Modem |
36 * | +--. | | .->+ |
37 * | | | | | | | |
38 * -------- | | | | ---------
39 * v | v |
40 * --+-+---+-+--
41 * | GSI |
42 * |-----------|
43 * | |
44 * | IPA |
45 * | |
46 * -------------
47 *
48 * In the above diagram, the AP and Modem represent "execution environments"
49 * (EEs), which are independent operating environments that use the IPA for
50 * data transfer.
51 *
52 * Each EE uses a set of unidirectional GSI "channels," which allow transfer
53 * of data to or from the IPA. A channel is implemented as a ring buffer,
54 * with a DRAM-resident array of "transfer elements" (TREs) available to
55 * describe transfers to or from other EEs through the IPA. A transfer
56 * element can also contain an immediate command, requesting the IPA perform
57 * actions other than data transfer.
58 *
59 * Each TRE refers to a block of data--also located DRAM. After writing one
60 * or more TREs to a channel, the writer (either the IPA or an EE) writes a
61 * doorbell register to inform the receiving side how many elements have
62 * been written.
63 *
64 * Each channel has a GSI "event ring" associated with it. An event ring
65 * is implemented very much like a channel ring, but is always directed from
66 * the IPA to an EE. The IPA notifies an EE (such as the AP) about channel
67 * events by adding an entry to the event ring associated with the channel.
68 * The GSI then writes its doorbell for the event ring, causing the target
69 * EE to be interrupted. Each entry in an event ring contains a pointer
70 * to the channel TRE whose completion the event represents.
71 *
72 * Each TRE in a channel ring has a set of flags. One flag indicates whether
73 * the completion of the transfer operation generates an entry (and possibly
74 * an interrupt) in the channel's event ring. Other flags allow transfer
75 * elements to be chained together, forming a single logical transaction.
76 * TRE flags are used to control whether and when interrupts are generated
77 * to signal completion of channel transfers.
78 *
79 * Elements in channel and event rings are completed (or consumed) strictly
80 * in order. Completion of one entry implies the completion of all preceding
81 * entries. A single completion interrupt can therefore communicate the
82 * completion of many transfers.
83 *
84 * Note that all GSI registers are little-endian, which is the assumed
85 * endianness of I/O space accesses. The accessor functions perform byte
86 * swapping if needed (i.e., for a big endian CPU).
87 */
88
89/* Delay period for interrupt moderation (in 32KHz IPA internal timer ticks) */
90#define GSI_EVT_RING_INT_MODT (32 * 1) /* 1ms under 32KHz clock */
91
92#define GSI_CMD_TIMEOUT 5 /* seconds */
93
94#define GSI_CHANNEL_STOP_RX_RETRIES 10
95
96#define GSI_MHI_EVENT_ID_START 10 /* 1st reserved event id */
97#define GSI_MHI_EVENT_ID_END 16 /* Last reserved event id */
98
99#define GSI_ISR_MAX_ITER 50 /* Detect interrupt storms */
100
101/* An entry in an event ring */
102struct gsi_event {
103 __le64 xfer_ptr;
104 __le16 len;
105 u8 reserved1;
106 u8 code;
107 __le16 reserved2;
108 u8 type;
109 u8 chid;
110};
111
112/* Hardware values from the error log register error code field */
113enum gsi_err_code {
114 GSI_INVALID_TRE_ERR = 0x1,
115 GSI_OUT_OF_BUFFERS_ERR = 0x2,
116 GSI_OUT_OF_RESOURCES_ERR = 0x3,
117 GSI_UNSUPPORTED_INTER_EE_OP_ERR = 0x4,
118 GSI_EVT_RING_EMPTY_ERR = 0x5,
119 GSI_NON_ALLOCATED_EVT_ACCESS_ERR = 0x6,
120 GSI_HWO_1_ERR = 0x8,
121};
122
123/* Hardware values from the error log register error type field */
124enum gsi_err_type {
125 GSI_ERR_TYPE_GLOB = 0x1,
126 GSI_ERR_TYPE_CHAN = 0x2,
127 GSI_ERR_TYPE_EVT = 0x3,
128};
129
130/* Hardware values used when programming an event ring */
131enum gsi_evt_chtype {
132 GSI_EVT_CHTYPE_MHI_EV = 0x0,
133 GSI_EVT_CHTYPE_XHCI_EV = 0x1,
134 GSI_EVT_CHTYPE_GPI_EV = 0x2,
135 GSI_EVT_CHTYPE_XDCI_EV = 0x3,
136};
137
138/* Hardware values used when programming a channel */
139enum gsi_channel_protocol {
140 GSI_CHANNEL_PROTOCOL_MHI = 0x0,
141 GSI_CHANNEL_PROTOCOL_XHCI = 0x1,
142 GSI_CHANNEL_PROTOCOL_GPI = 0x2,
143 GSI_CHANNEL_PROTOCOL_XDCI = 0x3,
144};
145
146/* Hardware values representing an event ring immediate command opcode */
147enum gsi_evt_cmd_opcode {
148 GSI_EVT_ALLOCATE = 0x0,
149 GSI_EVT_RESET = 0x9,
150 GSI_EVT_DE_ALLOC = 0xa,
151};
152
153/* Hardware values representing a generic immediate command opcode */
154enum gsi_generic_cmd_opcode {
155 GSI_GENERIC_HALT_CHANNEL = 0x1,
156 GSI_GENERIC_ALLOCATE_CHANNEL = 0x2,
157};
158
159/* Hardware values representing a channel immediate command opcode */
160enum gsi_ch_cmd_opcode {
161 GSI_CH_ALLOCATE = 0x0,
162 GSI_CH_START = 0x1,
163 GSI_CH_STOP = 0x2,
164 GSI_CH_RESET = 0x9,
165 GSI_CH_DE_ALLOC = 0xa,
166};
167
168/** gsi_channel_scratch_gpi - GPI protocol scratch register
169 * @max_outstanding_tre:
170 * Defines the maximum number of TREs allowed in a single transaction
171 * on a channel (in bytes). This determines the amount of prefetch
172 * performed by the hardware. We configure this to equal the size of
173 * the TLV FIFO for the channel.
174 * @outstanding_threshold:
175 * Defines the threshold (in bytes) determining when the sequencer
176 * should update the channel doorbell. We configure this to equal
177 * the size of two TREs.
178 */
179struct gsi_channel_scratch_gpi {
180 u64 reserved1;
181 u16 reserved2;
182 u16 max_outstanding_tre;
183 u16 reserved3;
184 u16 outstanding_threshold;
185};
186
187/** gsi_channel_scratch - channel scratch configuration area
188 *
189 * The exact interpretation of this register is protocol-specific.
190 * We only use GPI channels; see struct gsi_channel_scratch_gpi, above.
191 */
192union gsi_channel_scratch {
193 struct gsi_channel_scratch_gpi gpi;
194 struct {
195 u32 word1;
196 u32 word2;
197 u32 word3;
198 u32 word4;
199 } data;
200};
201
202/* Check things that can be validated at build time. */
203static void gsi_validate_build(void)
204{
205 /* This is used as a divisor */
206 BUILD_BUG_ON(!GSI_RING_ELEMENT_SIZE);
207
208 /* Code assumes the size of channel and event ring element are
209 * the same (and fixed). Make sure the size of an event ring
210 * element is what's expected.
211 */
212 BUILD_BUG_ON(sizeof(struct gsi_event) != GSI_RING_ELEMENT_SIZE);
213
214 /* Hardware requires a 2^n ring size. We ensure the number of
215 * elements in an event ring is a power of 2 elsewhere; this
216 * ensure the elements themselves meet the requirement.
217 */
218 BUILD_BUG_ON(!is_power_of_2(GSI_RING_ELEMENT_SIZE));
219
220 /* The channel element size must fit in this field */
221 BUILD_BUG_ON(GSI_RING_ELEMENT_SIZE > field_max(ELEMENT_SIZE_FMASK));
222
223 /* The event ring element size must fit in this field */
224 BUILD_BUG_ON(GSI_RING_ELEMENT_SIZE > field_max(EV_ELEMENT_SIZE_FMASK));
225}
226
227/* Return the channel id associated with a given channel */
228static u32 gsi_channel_id(struct gsi_channel *channel)
229{
230 return channel - &channel->gsi->channel[0];
231}
232
233static void gsi_irq_ieob_enable(struct gsi *gsi, u32 evt_ring_id)
234{
235 u32 val;
236
237 gsi->event_enable_bitmap |= BIT(evt_ring_id);
238 val = gsi->event_enable_bitmap;
239 iowrite32(val, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
240}
241
Alex Elder650d1602020-03-05 22:28:21 -0600242static void gsi_irq_ieob_disable(struct gsi *gsi, u32 evt_ring_id)
243{
244 u32 val;
245
246 gsi->event_enable_bitmap &= ~BIT(evt_ring_id);
247 val = gsi->event_enable_bitmap;
248 iowrite32(val, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
249}
250
251/* Enable all GSI_interrupt types */
252static void gsi_irq_enable(struct gsi *gsi)
253{
254 u32 val;
255
256 /* We don't use inter-EE channel or event interrupts */
257 val = GSI_CNTXT_TYPE_IRQ_MSK_ALL;
Alex Eldere6580d02020-09-28 18:04:41 -0500258 val &= ~INTER_EE_CH_CTRL_FMASK;
259 val &= ~INTER_EE_EV_CTRL_FMASK;
Alex Elder650d1602020-03-05 22:28:21 -0600260 iowrite32(val, gsi->virt + GSI_CNTXT_TYPE_IRQ_MSK_OFFSET);
261
262 val = GENMASK(gsi->channel_count - 1, 0);
263 iowrite32(val, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET);
264
265 val = GENMASK(gsi->evt_ring_count - 1, 0);
266 iowrite32(val, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET);
267
268 /* Each IEOB interrupt is enabled (later) as needed by channels */
269 iowrite32(0, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
270
271 val = GSI_CNTXT_GLOB_IRQ_ALL;
272 iowrite32(val, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
273
274 /* Never enable GSI_BREAK_POINT */
Alex Elderfb980ef2020-09-28 18:04:43 -0500275 val = GSI_CNTXT_GSI_IRQ_ALL & ~BREAK_POINT_FMASK;
Alex Elder650d1602020-03-05 22:28:21 -0600276 iowrite32(val, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET);
277}
278
279/* Disable all GSI_interrupt types */
280static void gsi_irq_disable(struct gsi *gsi)
281{
282 iowrite32(0, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET);
283 iowrite32(0, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
284 iowrite32(0, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
285 iowrite32(0, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET);
286 iowrite32(0, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET);
287 iowrite32(0, gsi->virt + GSI_CNTXT_TYPE_IRQ_MSK_OFFSET);
288}
289
290/* Return the virtual address associated with a ring index */
291void *gsi_ring_virt(struct gsi_ring *ring, u32 index)
292{
293 /* Note: index *must* be used modulo the ring count here */
294 return ring->virt + (index % ring->count) * GSI_RING_ELEMENT_SIZE;
295}
296
297/* Return the 32-bit DMA address associated with a ring index */
298static u32 gsi_ring_addr(struct gsi_ring *ring, u32 index)
299{
300 return (ring->addr & GENMASK(31, 0)) + index * GSI_RING_ELEMENT_SIZE;
301}
302
303/* Return the ring index of a 32-bit ring offset */
304static u32 gsi_ring_index(struct gsi_ring *ring, u32 offset)
305{
306 return (offset - gsi_ring_addr(ring, 0)) / GSI_RING_ELEMENT_SIZE;
307}
308
309/* Issue a GSI command by writing a value to a register, then wait for
310 * completion to be signaled. Returns true if the command completes
311 * or false if it times out.
312 */
313static bool
314gsi_command(struct gsi *gsi, u32 reg, u32 val, struct completion *completion)
315{
316 reinit_completion(completion);
317
318 iowrite32(val, gsi->virt + reg);
319
320 return !!wait_for_completion_timeout(completion, GSI_CMD_TIMEOUT * HZ);
321}
322
323/* Return the hardware's notion of the current state of an event ring */
324static enum gsi_evt_ring_state
325gsi_evt_ring_state(struct gsi *gsi, u32 evt_ring_id)
326{
327 u32 val;
328
329 val = ioread32(gsi->virt + GSI_EV_CH_E_CNTXT_0_OFFSET(evt_ring_id));
330
331 return u32_get_bits(val, EV_CHSTATE_FMASK);
332}
333
334/* Issue an event ring command and wait for it to complete */
335static int evt_ring_command(struct gsi *gsi, u32 evt_ring_id,
336 enum gsi_evt_cmd_opcode opcode)
337{
338 struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
339 struct completion *completion = &evt_ring->completion;
Alex Elder84634882020-06-30 07:58:45 -0500340 struct device *dev = gsi->dev;
Alex Elder650d1602020-03-05 22:28:21 -0600341 u32 val;
342
343 val = u32_encode_bits(evt_ring_id, EV_CHID_FMASK);
344 val |= u32_encode_bits(opcode, EV_OPCODE_FMASK);
345
346 if (gsi_command(gsi, GSI_EV_CH_CMD_OFFSET, val, completion))
347 return 0; /* Success! */
348
Alex Elder84634882020-06-30 07:58:45 -0500349 dev_err(dev, "GSI command %u for event ring %u timed out, state %u\n",
350 opcode, evt_ring_id, evt_ring->state);
Alex Elder650d1602020-03-05 22:28:21 -0600351
352 return -ETIMEDOUT;
353}
354
355/* Allocate an event ring in NOT_ALLOCATED state */
356static int gsi_evt_ring_alloc_command(struct gsi *gsi, u32 evt_ring_id)
357{
358 struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
359 int ret;
360
361 /* Get initial event ring state */
362 evt_ring->state = gsi_evt_ring_state(gsi, evt_ring_id);
Alex Eldera442b3c2020-06-30 07:58:44 -0500363 if (evt_ring->state != GSI_EVT_RING_STATE_NOT_ALLOCATED) {
364 dev_err(gsi->dev, "bad event ring state %u before alloc\n",
365 evt_ring->state);
Alex Elder650d1602020-03-05 22:28:21 -0600366 return -EINVAL;
Alex Eldera442b3c2020-06-30 07:58:44 -0500367 }
Alex Elder650d1602020-03-05 22:28:21 -0600368
369 ret = evt_ring_command(gsi, evt_ring_id, GSI_EVT_ALLOCATE);
370 if (!ret && evt_ring->state != GSI_EVT_RING_STATE_ALLOCATED) {
Alex Eldera442b3c2020-06-30 07:58:44 -0500371 dev_err(gsi->dev, "bad event ring state %u after alloc\n",
Alex Elder650d1602020-03-05 22:28:21 -0600372 evt_ring->state);
373 ret = -EIO;
374 }
375
376 return ret;
377}
378
379/* Reset a GSI event ring in ALLOCATED or ERROR state. */
380static void gsi_evt_ring_reset_command(struct gsi *gsi, u32 evt_ring_id)
381{
382 struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
383 enum gsi_evt_ring_state state = evt_ring->state;
384 int ret;
385
386 if (state != GSI_EVT_RING_STATE_ALLOCATED &&
387 state != GSI_EVT_RING_STATE_ERROR) {
Alex Eldera442b3c2020-06-30 07:58:44 -0500388 dev_err(gsi->dev, "bad event ring state %u before reset\n",
Alex Elder650d1602020-03-05 22:28:21 -0600389 evt_ring->state);
390 return;
391 }
392
393 ret = evt_ring_command(gsi, evt_ring_id, GSI_EVT_RESET);
394 if (!ret && evt_ring->state != GSI_EVT_RING_STATE_ALLOCATED)
Alex Eldera442b3c2020-06-30 07:58:44 -0500395 dev_err(gsi->dev, "bad event ring state %u after reset\n",
Alex Elder650d1602020-03-05 22:28:21 -0600396 evt_ring->state);
397}
398
399/* Issue a hardware de-allocation request for an allocated event ring */
400static void gsi_evt_ring_de_alloc_command(struct gsi *gsi, u32 evt_ring_id)
401{
402 struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
403 int ret;
404
405 if (evt_ring->state != GSI_EVT_RING_STATE_ALLOCATED) {
Alex Eldera442b3c2020-06-30 07:58:44 -0500406 dev_err(gsi->dev, "bad event ring state %u before dealloc\n",
Alex Elder650d1602020-03-05 22:28:21 -0600407 evt_ring->state);
408 return;
409 }
410
411 ret = evt_ring_command(gsi, evt_ring_id, GSI_EVT_DE_ALLOC);
412 if (!ret && evt_ring->state != GSI_EVT_RING_STATE_NOT_ALLOCATED)
Alex Eldera442b3c2020-06-30 07:58:44 -0500413 dev_err(gsi->dev, "bad event ring state %u after dealloc\n",
Alex Elder650d1602020-03-05 22:28:21 -0600414 evt_ring->state);
415}
416
Alex Eldera2003b32020-04-30 17:13:23 -0500417/* Fetch the current state of a channel from hardware */
Alex Elderaba79242020-04-30 17:13:22 -0500418static enum gsi_channel_state gsi_channel_state(struct gsi_channel *channel)
Alex Elder650d1602020-03-05 22:28:21 -0600419{
Alex Elderaba79242020-04-30 17:13:22 -0500420 u32 channel_id = gsi_channel_id(channel);
421 void *virt = channel->gsi->virt;
Alex Elder650d1602020-03-05 22:28:21 -0600422 u32 val;
423
Alex Elderaba79242020-04-30 17:13:22 -0500424 val = ioread32(virt + GSI_CH_C_CNTXT_0_OFFSET(channel_id));
Alex Elder650d1602020-03-05 22:28:21 -0600425
426 return u32_get_bits(val, CHSTATE_FMASK);
427}
428
429/* Issue a channel command and wait for it to complete */
430static int
431gsi_channel_command(struct gsi_channel *channel, enum gsi_ch_cmd_opcode opcode)
432{
433 struct completion *completion = &channel->completion;
434 u32 channel_id = gsi_channel_id(channel);
Alex Eldera2003b32020-04-30 17:13:23 -0500435 struct gsi *gsi = channel->gsi;
Alex Elder84634882020-06-30 07:58:45 -0500436 struct device *dev = gsi->dev;
Alex Elder650d1602020-03-05 22:28:21 -0600437 u32 val;
438
439 val = u32_encode_bits(channel_id, CH_CHID_FMASK);
440 val |= u32_encode_bits(opcode, CH_OPCODE_FMASK);
441
Alex Eldera2003b32020-04-30 17:13:23 -0500442 if (gsi_command(gsi, GSI_CH_CMD_OFFSET, val, completion))
Alex Elder650d1602020-03-05 22:28:21 -0600443 return 0; /* Success! */
444
Alex Elder84634882020-06-30 07:58:45 -0500445 dev_err(dev, "GSI command %u for channel %u timed out, state %u\n",
Alex Eldera2003b32020-04-30 17:13:23 -0500446 opcode, channel_id, gsi_channel_state(channel));
Alex Elder650d1602020-03-05 22:28:21 -0600447
448 return -ETIMEDOUT;
449}
450
451/* Allocate GSI channel in NOT_ALLOCATED state */
452static int gsi_channel_alloc_command(struct gsi *gsi, u32 channel_id)
453{
454 struct gsi_channel *channel = &gsi->channel[channel_id];
Alex Eldera442b3c2020-06-30 07:58:44 -0500455 struct device *dev = gsi->dev;
Alex Eldera2003b32020-04-30 17:13:23 -0500456 enum gsi_channel_state state;
Alex Elder650d1602020-03-05 22:28:21 -0600457 int ret;
458
459 /* Get initial channel state */
Alex Eldera2003b32020-04-30 17:13:23 -0500460 state = gsi_channel_state(channel);
Alex Eldera442b3c2020-06-30 07:58:44 -0500461 if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED) {
462 dev_err(dev, "bad channel state %u before alloc\n", state);
Alex Elder650d1602020-03-05 22:28:21 -0600463 return -EINVAL;
Alex Eldera442b3c2020-06-30 07:58:44 -0500464 }
Alex Elder650d1602020-03-05 22:28:21 -0600465
466 ret = gsi_channel_command(channel, GSI_CH_ALLOCATE);
Alex Eldera2003b32020-04-30 17:13:23 -0500467
468 /* Channel state will normally have been updated */
469 state = gsi_channel_state(channel);
470 if (!ret && state != GSI_CHANNEL_STATE_ALLOCATED) {
Alex Eldera442b3c2020-06-30 07:58:44 -0500471 dev_err(dev, "bad channel state %u after alloc\n", state);
Alex Elder650d1602020-03-05 22:28:21 -0600472 ret = -EIO;
473 }
474
475 return ret;
476}
477
478/* Start an ALLOCATED channel */
479static int gsi_channel_start_command(struct gsi_channel *channel)
480{
Alex Eldera442b3c2020-06-30 07:58:44 -0500481 struct device *dev = channel->gsi->dev;
Alex Eldera2003b32020-04-30 17:13:23 -0500482 enum gsi_channel_state state;
Alex Elder650d1602020-03-05 22:28:21 -0600483 int ret;
484
Alex Eldera2003b32020-04-30 17:13:23 -0500485 state = gsi_channel_state(channel);
Alex Elder650d1602020-03-05 22:28:21 -0600486 if (state != GSI_CHANNEL_STATE_ALLOCATED &&
Alex Eldera442b3c2020-06-30 07:58:44 -0500487 state != GSI_CHANNEL_STATE_STOPPED) {
488 dev_err(dev, "bad channel state %u before start\n", state);
Alex Elder650d1602020-03-05 22:28:21 -0600489 return -EINVAL;
Alex Eldera442b3c2020-06-30 07:58:44 -0500490 }
Alex Elder650d1602020-03-05 22:28:21 -0600491
492 ret = gsi_channel_command(channel, GSI_CH_START);
Alex Eldera2003b32020-04-30 17:13:23 -0500493
494 /* Channel state will normally have been updated */
495 state = gsi_channel_state(channel);
496 if (!ret && state != GSI_CHANNEL_STATE_STARTED) {
Alex Eldera442b3c2020-06-30 07:58:44 -0500497 dev_err(dev, "bad channel state %u after start\n", state);
Alex Elder650d1602020-03-05 22:28:21 -0600498 ret = -EIO;
499 }
500
501 return ret;
502}
503
504/* Stop a GSI channel in STARTED state */
505static int gsi_channel_stop_command(struct gsi_channel *channel)
506{
Alex Eldera442b3c2020-06-30 07:58:44 -0500507 struct device *dev = channel->gsi->dev;
Alex Eldera2003b32020-04-30 17:13:23 -0500508 enum gsi_channel_state state;
Alex Elder650d1602020-03-05 22:28:21 -0600509 int ret;
510
Alex Eldera2003b32020-04-30 17:13:23 -0500511 state = gsi_channel_state(channel);
Alex Elder5468cbc2020-06-30 07:44:42 -0500512
513 /* Channel could have entered STOPPED state since last call
514 * if it timed out. If so, we're done.
515 */
516 if (state == GSI_CHANNEL_STATE_STOPPED)
517 return 0;
518
Alex Elder650d1602020-03-05 22:28:21 -0600519 if (state != GSI_CHANNEL_STATE_STARTED &&
Alex Eldera442b3c2020-06-30 07:58:44 -0500520 state != GSI_CHANNEL_STATE_STOP_IN_PROC) {
521 dev_err(dev, "bad channel state %u before stop\n", state);
Alex Elder650d1602020-03-05 22:28:21 -0600522 return -EINVAL;
Alex Eldera442b3c2020-06-30 07:58:44 -0500523 }
Alex Elder650d1602020-03-05 22:28:21 -0600524
525 ret = gsi_channel_command(channel, GSI_CH_STOP);
Alex Eldera2003b32020-04-30 17:13:23 -0500526
527 /* Channel state will normally have been updated */
528 state = gsi_channel_state(channel);
529 if (ret || state == GSI_CHANNEL_STATE_STOPPED)
Alex Elder650d1602020-03-05 22:28:21 -0600530 return ret;
531
532 /* We may have to try again if stop is in progress */
Alex Eldera2003b32020-04-30 17:13:23 -0500533 if (state == GSI_CHANNEL_STATE_STOP_IN_PROC)
Alex Elder650d1602020-03-05 22:28:21 -0600534 return -EAGAIN;
535
Alex Eldera442b3c2020-06-30 07:58:44 -0500536 dev_err(dev, "bad channel state %u after stop\n", state);
Alex Elder650d1602020-03-05 22:28:21 -0600537
538 return -EIO;
539}
540
541/* Reset a GSI channel in ALLOCATED or ERROR state. */
542static void gsi_channel_reset_command(struct gsi_channel *channel)
543{
Alex Eldera442b3c2020-06-30 07:58:44 -0500544 struct device *dev = channel->gsi->dev;
Alex Eldera2003b32020-04-30 17:13:23 -0500545 enum gsi_channel_state state;
Alex Elder650d1602020-03-05 22:28:21 -0600546 int ret;
547
548 msleep(1); /* A short delay is required before a RESET command */
549
Alex Eldera2003b32020-04-30 17:13:23 -0500550 state = gsi_channel_state(channel);
551 if (state != GSI_CHANNEL_STATE_STOPPED &&
552 state != GSI_CHANNEL_STATE_ERROR) {
Alex Eldera442b3c2020-06-30 07:58:44 -0500553 dev_err(dev, "bad channel state %u before reset\n", state);
Alex Elder650d1602020-03-05 22:28:21 -0600554 return;
555 }
556
557 ret = gsi_channel_command(channel, GSI_CH_RESET);
Alex Eldera2003b32020-04-30 17:13:23 -0500558
559 /* Channel state will normally have been updated */
560 state = gsi_channel_state(channel);
561 if (!ret && state != GSI_CHANNEL_STATE_ALLOCATED)
Alex Eldera442b3c2020-06-30 07:58:44 -0500562 dev_err(dev, "bad channel state %u after reset\n", state);
Alex Elder650d1602020-03-05 22:28:21 -0600563}
564
565/* Deallocate an ALLOCATED GSI channel */
566static void gsi_channel_de_alloc_command(struct gsi *gsi, u32 channel_id)
567{
568 struct gsi_channel *channel = &gsi->channel[channel_id];
Alex Eldera442b3c2020-06-30 07:58:44 -0500569 struct device *dev = gsi->dev;
Alex Eldera2003b32020-04-30 17:13:23 -0500570 enum gsi_channel_state state;
Alex Elder650d1602020-03-05 22:28:21 -0600571 int ret;
572
Alex Eldera2003b32020-04-30 17:13:23 -0500573 state = gsi_channel_state(channel);
574 if (state != GSI_CHANNEL_STATE_ALLOCATED) {
Alex Eldera442b3c2020-06-30 07:58:44 -0500575 dev_err(dev, "bad channel state %u before dealloc\n", state);
Alex Elder650d1602020-03-05 22:28:21 -0600576 return;
577 }
578
579 ret = gsi_channel_command(channel, GSI_CH_DE_ALLOC);
Alex Eldera2003b32020-04-30 17:13:23 -0500580
581 /* Channel state will normally have been updated */
582 state = gsi_channel_state(channel);
583 if (!ret && state != GSI_CHANNEL_STATE_NOT_ALLOCATED)
Alex Eldera442b3c2020-06-30 07:58:44 -0500584 dev_err(dev, "bad channel state %u after dealloc\n", state);
Alex Elder650d1602020-03-05 22:28:21 -0600585}
586
587/* Ring an event ring doorbell, reporting the last entry processed by the AP.
588 * The index argument (modulo the ring count) is the first unfilled entry, so
589 * we supply one less than that with the doorbell. Update the event ring
590 * index field with the value provided.
591 */
592static void gsi_evt_ring_doorbell(struct gsi *gsi, u32 evt_ring_id, u32 index)
593{
594 struct gsi_ring *ring = &gsi->evt_ring[evt_ring_id].ring;
595 u32 val;
596
597 ring->index = index; /* Next unused entry */
598
599 /* Note: index *must* be used modulo the ring count here */
600 val = gsi_ring_addr(ring, (index - 1) % ring->count);
601 iowrite32(val, gsi->virt + GSI_EV_CH_E_DOORBELL_0_OFFSET(evt_ring_id));
602}
603
604/* Program an event ring for use */
605static void gsi_evt_ring_program(struct gsi *gsi, u32 evt_ring_id)
606{
607 struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
608 size_t size = evt_ring->ring.count * GSI_RING_ELEMENT_SIZE;
609 u32 val;
610
611 val = u32_encode_bits(GSI_EVT_CHTYPE_GPI_EV, EV_CHTYPE_FMASK);
612 val |= EV_INTYPE_FMASK;
613 val |= u32_encode_bits(GSI_RING_ELEMENT_SIZE, EV_ELEMENT_SIZE_FMASK);
614 iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_0_OFFSET(evt_ring_id));
615
616 val = u32_encode_bits(size, EV_R_LENGTH_FMASK);
617 iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_1_OFFSET(evt_ring_id));
618
619 /* The context 2 and 3 registers store the low-order and
620 * high-order 32 bits of the address of the event ring,
621 * respectively.
622 */
623 val = evt_ring->ring.addr & GENMASK(31, 0);
624 iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_2_OFFSET(evt_ring_id));
625
626 val = evt_ring->ring.addr >> 32;
627 iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_3_OFFSET(evt_ring_id));
628
629 /* Enable interrupt moderation by setting the moderation delay */
630 val = u32_encode_bits(GSI_EVT_RING_INT_MODT, MODT_FMASK);
631 val |= u32_encode_bits(1, MODC_FMASK); /* comes from channel */
632 iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_8_OFFSET(evt_ring_id));
633
634 /* No MSI write data, and MSI address high and low address is 0 */
635 iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_9_OFFSET(evt_ring_id));
636 iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_10_OFFSET(evt_ring_id));
637 iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_11_OFFSET(evt_ring_id));
638
639 /* We don't need to get event read pointer updates */
640 iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_12_OFFSET(evt_ring_id));
641 iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_13_OFFSET(evt_ring_id));
642
643 /* Finally, tell the hardware we've completed event 0 (arbitrary) */
644 gsi_evt_ring_doorbell(gsi, evt_ring_id, 0);
645}
646
647/* Return the last (most recent) transaction completed on a channel. */
648static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel)
649{
650 struct gsi_trans_info *trans_info = &channel->trans_info;
651 struct gsi_trans *trans;
652
653 spin_lock_bh(&trans_info->spinlock);
654
655 if (!list_empty(&trans_info->complete))
656 trans = list_last_entry(&trans_info->complete,
657 struct gsi_trans, links);
658 else if (!list_empty(&trans_info->polled))
659 trans = list_last_entry(&trans_info->polled,
660 struct gsi_trans, links);
661 else
662 trans = NULL;
663
664 /* Caller will wait for this, so take a reference */
665 if (trans)
666 refcount_inc(&trans->refcount);
667
668 spin_unlock_bh(&trans_info->spinlock);
669
670 return trans;
671}
672
673/* Wait for transaction activity on a channel to complete */
674static void gsi_channel_trans_quiesce(struct gsi_channel *channel)
675{
676 struct gsi_trans *trans;
677
678 /* Get the last transaction, and wait for it to complete */
679 trans = gsi_channel_trans_last(channel);
680 if (trans) {
681 wait_for_completion(&trans->completion);
682 gsi_trans_free(trans);
683 }
684}
685
686/* Stop channel activity. Transactions may not be allocated until thawed. */
687static void gsi_channel_freeze(struct gsi_channel *channel)
688{
689 gsi_channel_trans_quiesce(channel);
690
691 napi_disable(&channel->napi);
692
693 gsi_irq_ieob_disable(channel->gsi, channel->evt_ring_id);
694}
695
696/* Allow transactions to be used on the channel again. */
697static void gsi_channel_thaw(struct gsi_channel *channel)
698{
699 gsi_irq_ieob_enable(channel->gsi, channel->evt_ring_id);
700
701 napi_enable(&channel->napi);
702}
703
704/* Program a channel for use */
705static void gsi_channel_program(struct gsi_channel *channel, bool doorbell)
706{
707 size_t size = channel->tre_ring.count * GSI_RING_ELEMENT_SIZE;
708 u32 channel_id = gsi_channel_id(channel);
709 union gsi_channel_scratch scr = { };
710 struct gsi_channel_scratch_gpi *gpi;
711 struct gsi *gsi = channel->gsi;
712 u32 wrr_weight = 0;
713 u32 val;
714
715 /* Arbitrarily pick TRE 0 as the first channel element to use */
716 channel->tre_ring.index = 0;
717
718 /* We program all channels to use GPI protocol */
719 val = u32_encode_bits(GSI_CHANNEL_PROTOCOL_GPI, CHTYPE_PROTOCOL_FMASK);
720 if (channel->toward_ipa)
721 val |= CHTYPE_DIR_FMASK;
722 val |= u32_encode_bits(channel->evt_ring_id, ERINDEX_FMASK);
723 val |= u32_encode_bits(GSI_RING_ELEMENT_SIZE, ELEMENT_SIZE_FMASK);
724 iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_0_OFFSET(channel_id));
725
726 val = u32_encode_bits(size, R_LENGTH_FMASK);
727 iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_1_OFFSET(channel_id));
728
729 /* The context 2 and 3 registers store the low-order and
730 * high-order 32 bits of the address of the channel ring,
731 * respectively.
732 */
733 val = channel->tre_ring.addr & GENMASK(31, 0);
734 iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_2_OFFSET(channel_id));
735
736 val = channel->tre_ring.addr >> 32;
737 iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_3_OFFSET(channel_id));
738
739 /* Command channel gets low weighted round-robin priority */
740 if (channel->command)
741 wrr_weight = field_max(WRR_WEIGHT_FMASK);
742 val = u32_encode_bits(wrr_weight, WRR_WEIGHT_FMASK);
743
744 /* Max prefetch is 1 segment (do not set MAX_PREFETCH_FMASK) */
745
746 /* Enable the doorbell engine if requested */
747 if (doorbell)
748 val |= USE_DB_ENG_FMASK;
749
750 if (!channel->use_prefetch)
751 val |= USE_ESCAPE_BUF_ONLY_FMASK;
752
753 iowrite32(val, gsi->virt + GSI_CH_C_QOS_OFFSET(channel_id));
754
755 /* Now update the scratch registers for GPI protocol */
756 gpi = &scr.gpi;
757 gpi->max_outstanding_tre = gsi_channel_trans_tre_max(gsi, channel_id) *
758 GSI_RING_ELEMENT_SIZE;
759 gpi->outstanding_threshold = 2 * GSI_RING_ELEMENT_SIZE;
760
761 val = scr.data.word1;
762 iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_0_OFFSET(channel_id));
763
764 val = scr.data.word2;
765 iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_1_OFFSET(channel_id));
766
767 val = scr.data.word3;
768 iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_2_OFFSET(channel_id));
769
770 /* We must preserve the upper 16 bits of the last scratch register.
771 * The next sequence assumes those bits remain unchanged between the
772 * read and the write.
773 */
774 val = ioread32(gsi->virt + GSI_CH_C_SCRATCH_3_OFFSET(channel_id));
775 val = (scr.data.word4 & GENMASK(31, 16)) | (val & GENMASK(15, 0));
776 iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_3_OFFSET(channel_id));
777
778 /* All done! */
779}
780
781static void gsi_channel_deprogram(struct gsi_channel *channel)
782{
783 /* Nothing to do */
784}
785
786/* Start an allocated GSI channel */
787int gsi_channel_start(struct gsi *gsi, u32 channel_id)
788{
789 struct gsi_channel *channel = &gsi->channel[channel_id];
Alex Elder650d1602020-03-05 22:28:21 -0600790 int ret;
791
792 mutex_lock(&gsi->mutex);
793
794 ret = gsi_channel_start_command(channel);
795
796 mutex_unlock(&gsi->mutex);
797
Alex Elder650d1602020-03-05 22:28:21 -0600798 gsi_channel_thaw(channel);
799
800 return ret;
801}
802
803/* Stop a started channel */
804int gsi_channel_stop(struct gsi *gsi, u32 channel_id)
805{
806 struct gsi_channel *channel = &gsi->channel[channel_id];
807 u32 retries;
808 int ret;
809
810 gsi_channel_freeze(channel);
811
Alex Elder650d1602020-03-05 22:28:21 -0600812 /* RX channels might require a little time to enter STOPPED state */
813 retries = channel->toward_ipa ? 0 : GSI_CHANNEL_STOP_RX_RETRIES;
814
815 mutex_lock(&gsi->mutex);
816
817 do {
818 ret = gsi_channel_stop_command(channel);
819 if (ret != -EAGAIN)
820 break;
821 msleep(1);
822 } while (retries--);
823
824 mutex_unlock(&gsi->mutex);
825
826 /* Thaw the channel if we need to retry (or on error) */
827 if (ret)
828 gsi_channel_thaw(channel);
829
830 return ret;
831}
832
833/* Reset and reconfigure a channel (possibly leaving doorbell disabled) */
Alex Elderf86a1902020-05-04 18:30:02 -0500834void gsi_channel_reset(struct gsi *gsi, u32 channel_id, bool legacy)
Alex Elder650d1602020-03-05 22:28:21 -0600835{
836 struct gsi_channel *channel = &gsi->channel[channel_id];
837
838 mutex_lock(&gsi->mutex);
839
Alex Elder650d1602020-03-05 22:28:21 -0600840 gsi_channel_reset_command(channel);
Alex Eldera3f24052020-05-04 18:30:03 -0500841 /* Due to a hardware quirk we may need to reset RX channels twice. */
842 if (legacy && !channel->toward_ipa)
Alex Elder650d1602020-03-05 22:28:21 -0600843 gsi_channel_reset_command(channel);
844
Alex Elderf86a1902020-05-04 18:30:02 -0500845 gsi_channel_program(channel, legacy);
Alex Elder650d1602020-03-05 22:28:21 -0600846 gsi_channel_trans_cancel_pending(channel);
847
848 mutex_unlock(&gsi->mutex);
849}
850
851/* Stop a STARTED channel for suspend (using stop if requested) */
852int gsi_channel_suspend(struct gsi *gsi, u32 channel_id, bool stop)
853{
854 struct gsi_channel *channel = &gsi->channel[channel_id];
855
856 if (stop)
857 return gsi_channel_stop(gsi, channel_id);
858
859 gsi_channel_freeze(channel);
860
861 return 0;
862}
863
864/* Resume a suspended channel (starting will be requested if STOPPED) */
865int gsi_channel_resume(struct gsi *gsi, u32 channel_id, bool start)
866{
867 struct gsi_channel *channel = &gsi->channel[channel_id];
868
869 if (start)
870 return gsi_channel_start(gsi, channel_id);
871
872 gsi_channel_thaw(channel);
873
874 return 0;
875}
876
877/**
878 * gsi_channel_tx_queued() - Report queued TX transfers for a channel
879 * @channel: Channel for which to report
880 *
881 * Report to the network stack the number of bytes and transactions that
882 * have been queued to hardware since last call. This and the next function
883 * supply information used by the network stack for throttling.
884 *
885 * For each channel we track the number of transactions used and bytes of
886 * data those transactions represent. We also track what those values are
887 * each time this function is called. Subtracting the two tells us
888 * the number of bytes and transactions that have been added between
889 * successive calls.
890 *
891 * Calling this each time we ring the channel doorbell allows us to
892 * provide accurate information to the network stack about how much
893 * work we've given the hardware at any point in time.
894 */
895void gsi_channel_tx_queued(struct gsi_channel *channel)
896{
897 u32 trans_count;
898 u32 byte_count;
899
900 byte_count = channel->byte_count - channel->queued_byte_count;
901 trans_count = channel->trans_count - channel->queued_trans_count;
902 channel->queued_byte_count = channel->byte_count;
903 channel->queued_trans_count = channel->trans_count;
904
905 ipa_gsi_channel_tx_queued(channel->gsi, gsi_channel_id(channel),
906 trans_count, byte_count);
907}
908
909/**
910 * gsi_channel_tx_update() - Report completed TX transfers
911 * @channel: Channel that has completed transmitting packets
912 * @trans: Last transation known to be complete
913 *
914 * Compute the number of transactions and bytes that have been transferred
915 * over a TX channel since the given transaction was committed. Report this
916 * information to the network stack.
917 *
918 * At the time a transaction is committed, we record its channel's
919 * committed transaction and byte counts *in the transaction*.
920 * Completions are signaled by the hardware with an interrupt, and
921 * we can determine the latest completed transaction at that time.
922 *
923 * The difference between the byte/transaction count recorded in
924 * the transaction and the count last time we recorded a completion
925 * tells us exactly how much data has been transferred between
926 * completions.
927 *
928 * Calling this each time we learn of a newly-completed transaction
929 * allows us to provide accurate information to the network stack
930 * about how much work has been completed by the hardware at a given
931 * point in time.
932 */
933static void
934gsi_channel_tx_update(struct gsi_channel *channel, struct gsi_trans *trans)
935{
936 u64 byte_count = trans->byte_count + trans->len;
937 u64 trans_count = trans->trans_count + 1;
938
939 byte_count -= channel->compl_byte_count;
940 channel->compl_byte_count += byte_count;
941 trans_count -= channel->compl_trans_count;
942 channel->compl_trans_count += trans_count;
943
944 ipa_gsi_channel_tx_completed(channel->gsi, gsi_channel_id(channel),
945 trans_count, byte_count);
946}
947
948/* Channel control interrupt handler */
949static void gsi_isr_chan_ctrl(struct gsi *gsi)
950{
951 u32 channel_mask;
952
953 channel_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_CH_IRQ_OFFSET);
954 iowrite32(channel_mask, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_CLR_OFFSET);
955
956 while (channel_mask) {
957 u32 channel_id = __ffs(channel_mask);
958 struct gsi_channel *channel;
959
960 channel_mask ^= BIT(channel_id);
961
962 channel = &gsi->channel[channel_id];
Alex Elder650d1602020-03-05 22:28:21 -0600963
964 complete(&channel->completion);
965 }
966}
967
968/* Event ring control interrupt handler */
969static void gsi_isr_evt_ctrl(struct gsi *gsi)
970{
971 u32 event_mask;
972
973 event_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_OFFSET);
974 iowrite32(event_mask, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_CLR_OFFSET);
975
976 while (event_mask) {
977 u32 evt_ring_id = __ffs(event_mask);
978 struct gsi_evt_ring *evt_ring;
979
980 event_mask ^= BIT(evt_ring_id);
981
982 evt_ring = &gsi->evt_ring[evt_ring_id];
983 evt_ring->state = gsi_evt_ring_state(gsi, evt_ring_id);
984
985 complete(&evt_ring->completion);
986 }
987}
988
989/* Global channel error interrupt handler */
990static void
991gsi_isr_glob_chan_err(struct gsi *gsi, u32 err_ee, u32 channel_id, u32 code)
992{
993 if (code == GSI_OUT_OF_RESOURCES_ERR) {
994 dev_err(gsi->dev, "channel %u out of resources\n", channel_id);
995 complete(&gsi->channel[channel_id].completion);
996 return;
997 }
998
999 /* Report, but otherwise ignore all other error codes */
1000 dev_err(gsi->dev, "channel %u global error ee 0x%08x code 0x%08x\n",
1001 channel_id, err_ee, code);
1002}
1003
1004/* Global event error interrupt handler */
1005static void
1006gsi_isr_glob_evt_err(struct gsi *gsi, u32 err_ee, u32 evt_ring_id, u32 code)
1007{
1008 if (code == GSI_OUT_OF_RESOURCES_ERR) {
1009 struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
1010 u32 channel_id = gsi_channel_id(evt_ring->channel);
1011
1012 complete(&evt_ring->completion);
1013 dev_err(gsi->dev, "evt_ring for channel %u out of resources\n",
1014 channel_id);
1015 return;
1016 }
1017
1018 /* Report, but otherwise ignore all other error codes */
1019 dev_err(gsi->dev, "event ring %u global error ee %u code 0x%08x\n",
1020 evt_ring_id, err_ee, code);
1021}
1022
1023/* Global error interrupt handler */
1024static void gsi_isr_glob_err(struct gsi *gsi)
1025{
1026 enum gsi_err_type type;
1027 enum gsi_err_code code;
1028 u32 which;
1029 u32 val;
1030 u32 ee;
1031
1032 /* Get the logged error, then reinitialize the log */
1033 val = ioread32(gsi->virt + GSI_ERROR_LOG_OFFSET);
1034 iowrite32(0, gsi->virt + GSI_ERROR_LOG_OFFSET);
1035 iowrite32(~0, gsi->virt + GSI_ERROR_LOG_CLR_OFFSET);
1036
1037 ee = u32_get_bits(val, ERR_EE_FMASK);
1038 which = u32_get_bits(val, ERR_VIRT_IDX_FMASK);
1039 type = u32_get_bits(val, ERR_TYPE_FMASK);
1040 code = u32_get_bits(val, ERR_CODE_FMASK);
1041
1042 if (type == GSI_ERR_TYPE_CHAN)
1043 gsi_isr_glob_chan_err(gsi, ee, which, code);
1044 else if (type == GSI_ERR_TYPE_EVT)
1045 gsi_isr_glob_evt_err(gsi, ee, which, code);
1046 else /* type GSI_ERR_TYPE_GLOB should be fatal */
1047 dev_err(gsi->dev, "unexpected global error 0x%08x\n", type);
1048}
1049
1050/* Generic EE interrupt handler */
1051static void gsi_isr_gp_int1(struct gsi *gsi)
1052{
1053 u32 result;
1054 u32 val;
1055
1056 val = ioread32(gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
1057 result = u32_get_bits(val, GENERIC_EE_RESULT_FMASK);
1058 if (result != GENERIC_EE_SUCCESS_FVAL)
1059 dev_err(gsi->dev, "global INT1 generic result %u\n", result);
1060
1061 complete(&gsi->completion);
1062}
Alex Elder0b1ba182020-04-30 16:35:12 -05001063
Alex Elder650d1602020-03-05 22:28:21 -06001064/* Inter-EE interrupt handler */
1065static void gsi_isr_glob_ee(struct gsi *gsi)
1066{
1067 u32 val;
1068
1069 val = ioread32(gsi->virt + GSI_CNTXT_GLOB_IRQ_STTS_OFFSET);
1070
1071 if (val & ERROR_INT_FMASK)
1072 gsi_isr_glob_err(gsi);
1073
1074 iowrite32(val, gsi->virt + GSI_CNTXT_GLOB_IRQ_CLR_OFFSET);
1075
1076 val &= ~ERROR_INT_FMASK;
1077
Alex Elderd61bb712020-09-28 18:04:42 -05001078 if (val & GP_INT1_FMASK) {
1079 val ^= GP_INT1_FMASK;
Alex Elder650d1602020-03-05 22:28:21 -06001080 gsi_isr_gp_int1(gsi);
1081 }
1082
1083 if (val)
1084 dev_err(gsi->dev, "unexpected global interrupt 0x%08x\n", val);
1085}
1086
1087/* I/O completion interrupt event */
1088static void gsi_isr_ieob(struct gsi *gsi)
1089{
1090 u32 event_mask;
1091
1092 event_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_OFFSET);
Alex Elder195ef572020-05-15 15:07:31 -05001093 iowrite32(event_mask, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_CLR_OFFSET);
Alex Elder650d1602020-03-05 22:28:21 -06001094
1095 while (event_mask) {
1096 u32 evt_ring_id = __ffs(event_mask);
1097
1098 event_mask ^= BIT(evt_ring_id);
1099
1100 gsi_irq_ieob_disable(gsi, evt_ring_id);
1101 napi_schedule(&gsi->evt_ring[evt_ring_id].channel->napi);
1102 }
1103}
1104
1105/* General event interrupts represent serious problems, so report them */
1106static void gsi_isr_general(struct gsi *gsi)
1107{
1108 struct device *dev = gsi->dev;
1109 u32 val;
1110
1111 val = ioread32(gsi->virt + GSI_CNTXT_GSI_IRQ_STTS_OFFSET);
1112 iowrite32(val, gsi->virt + GSI_CNTXT_GSI_IRQ_CLR_OFFSET);
1113
1114 if (val)
1115 dev_err(dev, "unexpected general interrupt 0x%08x\n", val);
1116}
1117
1118/**
1119 * gsi_isr() - Top level GSI interrupt service routine
1120 * @irq: Interrupt number (ignored)
1121 * @dev_id: GSI pointer supplied to request_irq()
1122 *
1123 * This is the main handler function registered for the GSI IRQ. Each type
1124 * of interrupt has a separate handler function that is called from here.
1125 */
1126static irqreturn_t gsi_isr(int irq, void *dev_id)
1127{
1128 struct gsi *gsi = dev_id;
1129 u32 intr_mask;
1130 u32 cnt = 0;
1131
1132 while ((intr_mask = ioread32(gsi->virt + GSI_CNTXT_TYPE_IRQ_OFFSET))) {
1133 /* intr_mask contains bitmask of pending GSI interrupts */
1134 do {
1135 u32 gsi_intr = BIT(__ffs(intr_mask));
1136
1137 intr_mask ^= gsi_intr;
1138
1139 switch (gsi_intr) {
1140 case CH_CTRL_FMASK:
1141 gsi_isr_chan_ctrl(gsi);
1142 break;
1143 case EV_CTRL_FMASK:
1144 gsi_isr_evt_ctrl(gsi);
1145 break;
1146 case GLOB_EE_FMASK:
1147 gsi_isr_glob_ee(gsi);
1148 break;
1149 case IEOB_FMASK:
1150 gsi_isr_ieob(gsi);
1151 break;
1152 case GENERAL_FMASK:
1153 gsi_isr_general(gsi);
1154 break;
1155 default:
1156 dev_err(gsi->dev,
Alex Elder84634882020-06-30 07:58:45 -05001157 "unrecognized interrupt type 0x%08x\n",
1158 gsi_intr);
Alex Elder650d1602020-03-05 22:28:21 -06001159 break;
1160 }
1161 } while (intr_mask);
1162
1163 if (++cnt > GSI_ISR_MAX_ITER) {
1164 dev_err(gsi->dev, "interrupt flood\n");
1165 break;
1166 }
1167 }
1168
1169 return IRQ_HANDLED;
1170}
1171
1172/* Return the transaction associated with a transfer completion event */
1173static struct gsi_trans *gsi_event_trans(struct gsi_channel *channel,
1174 struct gsi_event *event)
1175{
1176 u32 tre_offset;
1177 u32 tre_index;
1178
1179 /* Event xfer_ptr records the TRE it's associated with */
1180 tre_offset = le64_to_cpu(event->xfer_ptr) & GENMASK(31, 0);
1181 tre_index = gsi_ring_index(&channel->tre_ring, tre_offset);
1182
1183 return gsi_channel_trans_mapped(channel, tre_index);
1184}
1185
1186/**
1187 * gsi_evt_ring_rx_update() - Record lengths of received data
1188 * @evt_ring: Event ring associated with channel that received packets
1189 * @index: Event index in ring reported by hardware
1190 *
1191 * Events for RX channels contain the actual number of bytes received into
1192 * the buffer. Every event has a transaction associated with it, and here
1193 * we update transactions to record their actual received lengths.
1194 *
1195 * This function is called whenever we learn that the GSI hardware has filled
1196 * new events since the last time we checked. The ring's index field tells
1197 * the first entry in need of processing. The index provided is the
1198 * first *unfilled* event in the ring (following the last filled one).
1199 *
1200 * Events are sequential within the event ring, and transactions are
1201 * sequential within the transaction pool.
1202 *
1203 * Note that @index always refers to an element *within* the event ring.
1204 */
1205static void gsi_evt_ring_rx_update(struct gsi_evt_ring *evt_ring, u32 index)
1206{
1207 struct gsi_channel *channel = evt_ring->channel;
1208 struct gsi_ring *ring = &evt_ring->ring;
1209 struct gsi_trans_info *trans_info;
1210 struct gsi_event *event_done;
1211 struct gsi_event *event;
1212 struct gsi_trans *trans;
1213 u32 byte_count = 0;
1214 u32 old_index;
1215 u32 event_avail;
1216
1217 trans_info = &channel->trans_info;
1218
1219 /* We'll start with the oldest un-processed event. RX channels
1220 * replenish receive buffers in single-TRE transactions, so we
1221 * can just map that event to its transaction. Transactions
1222 * associated with completion events are consecutive.
1223 */
1224 old_index = ring->index;
1225 event = gsi_ring_virt(ring, old_index);
1226 trans = gsi_event_trans(channel, event);
1227
1228 /* Compute the number of events to process before we wrap,
1229 * and determine when we'll be done processing events.
1230 */
1231 event_avail = ring->count - old_index % ring->count;
1232 event_done = gsi_ring_virt(ring, index);
1233 do {
1234 trans->len = __le16_to_cpu(event->len);
1235 byte_count += trans->len;
1236
1237 /* Move on to the next event and transaction */
1238 if (--event_avail)
1239 event++;
1240 else
1241 event = gsi_ring_virt(ring, 0);
1242 trans = gsi_trans_pool_next(&trans_info->pool, trans);
1243 } while (event != event_done);
1244
1245 /* We record RX bytes when they are received */
1246 channel->byte_count += byte_count;
1247 channel->trans_count++;
1248}
1249
1250/* Initialize a ring, including allocating DMA memory for its entries */
1251static int gsi_ring_alloc(struct gsi *gsi, struct gsi_ring *ring, u32 count)
1252{
1253 size_t size = count * GSI_RING_ELEMENT_SIZE;
1254 struct device *dev = gsi->dev;
1255 dma_addr_t addr;
1256
1257 /* Hardware requires a 2^n ring size, with alignment equal to size */
1258 ring->virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
1259 if (ring->virt && addr % size) {
1260 dma_free_coherent(dev, size, ring->virt, ring->addr);
1261 dev_err(dev, "unable to alloc 0x%zx-aligned ring buffer\n",
Alex Elder84634882020-06-30 07:58:45 -05001262 size);
Alex Elder650d1602020-03-05 22:28:21 -06001263 return -EINVAL; /* Not a good error value, but distinct */
1264 } else if (!ring->virt) {
1265 return -ENOMEM;
1266 }
1267 ring->addr = addr;
1268 ring->count = count;
1269
1270 return 0;
1271}
1272
1273/* Free a previously-allocated ring */
1274static void gsi_ring_free(struct gsi *gsi, struct gsi_ring *ring)
1275{
1276 size_t size = ring->count * GSI_RING_ELEMENT_SIZE;
1277
1278 dma_free_coherent(gsi->dev, size, ring->virt, ring->addr);
1279}
1280
1281/* Allocate an available event ring id */
1282static int gsi_evt_ring_id_alloc(struct gsi *gsi)
1283{
1284 u32 evt_ring_id;
1285
1286 if (gsi->event_bitmap == ~0U) {
1287 dev_err(gsi->dev, "event rings exhausted\n");
1288 return -ENOSPC;
1289 }
1290
1291 evt_ring_id = ffz(gsi->event_bitmap);
1292 gsi->event_bitmap |= BIT(evt_ring_id);
1293
1294 return (int)evt_ring_id;
1295}
1296
1297/* Free a previously-allocated event ring id */
1298static void gsi_evt_ring_id_free(struct gsi *gsi, u32 evt_ring_id)
1299{
1300 gsi->event_bitmap &= ~BIT(evt_ring_id);
1301}
1302
1303/* Ring a channel doorbell, reporting the first un-filled entry */
1304void gsi_channel_doorbell(struct gsi_channel *channel)
1305{
1306 struct gsi_ring *tre_ring = &channel->tre_ring;
1307 u32 channel_id = gsi_channel_id(channel);
1308 struct gsi *gsi = channel->gsi;
1309 u32 val;
1310
1311 /* Note: index *must* be used modulo the ring count here */
1312 val = gsi_ring_addr(tre_ring, tre_ring->index % tre_ring->count);
1313 iowrite32(val, gsi->virt + GSI_CH_C_DOORBELL_0_OFFSET(channel_id));
1314}
1315
1316/* Consult hardware, move any newly completed transactions to completed list */
1317static void gsi_channel_update(struct gsi_channel *channel)
1318{
1319 u32 evt_ring_id = channel->evt_ring_id;
1320 struct gsi *gsi = channel->gsi;
1321 struct gsi_evt_ring *evt_ring;
1322 struct gsi_trans *trans;
1323 struct gsi_ring *ring;
1324 u32 offset;
1325 u32 index;
1326
1327 evt_ring = &gsi->evt_ring[evt_ring_id];
1328 ring = &evt_ring->ring;
1329
1330 /* See if there's anything new to process; if not, we're done. Note
1331 * that index always refers to an entry *within* the event ring.
1332 */
1333 offset = GSI_EV_CH_E_CNTXT_4_OFFSET(evt_ring_id);
1334 index = gsi_ring_index(ring, ioread32(gsi->virt + offset));
1335 if (index == ring->index % ring->count)
1336 return;
1337
1338 /* Get the transaction for the latest completed event. Take a
1339 * reference to keep it from completing before we give the events
1340 * for this and previous transactions back to the hardware.
1341 */
1342 trans = gsi_event_trans(channel, gsi_ring_virt(ring, index - 1));
1343 refcount_inc(&trans->refcount);
1344
1345 /* For RX channels, update each completed transaction with the number
1346 * of bytes that were actually received. For TX channels, report
1347 * the number of transactions and bytes this completion represents
1348 * up the network stack.
1349 */
1350 if (channel->toward_ipa)
1351 gsi_channel_tx_update(channel, trans);
1352 else
1353 gsi_evt_ring_rx_update(evt_ring, index);
1354
1355 gsi_trans_move_complete(trans);
1356
1357 /* Tell the hardware we've handled these events */
1358 gsi_evt_ring_doorbell(channel->gsi, channel->evt_ring_id, index);
1359
1360 gsi_trans_free(trans);
1361}
1362
1363/**
1364 * gsi_channel_poll_one() - Return a single completed transaction on a channel
1365 * @channel: Channel to be polled
1366 *
Alex Eldere3eea082020-07-13 07:24:18 -05001367 * Return: Transaction pointer, or null if none are available
Alex Elder650d1602020-03-05 22:28:21 -06001368 *
1369 * This function returns the first entry on a channel's completed transaction
1370 * list. If that list is empty, the hardware is consulted to determine
1371 * whether any new transactions have completed. If so, they're moved to the
1372 * completed list and the new first entry is returned. If there are no more
1373 * completed transactions, a null pointer is returned.
1374 */
1375static struct gsi_trans *gsi_channel_poll_one(struct gsi_channel *channel)
1376{
1377 struct gsi_trans *trans;
1378
1379 /* Get the first transaction from the completed list */
1380 trans = gsi_channel_trans_complete(channel);
1381 if (!trans) {
1382 /* List is empty; see if there's more to do */
1383 gsi_channel_update(channel);
1384 trans = gsi_channel_trans_complete(channel);
1385 }
1386
1387 if (trans)
1388 gsi_trans_move_polled(trans);
1389
1390 return trans;
1391}
1392
1393/**
1394 * gsi_channel_poll() - NAPI poll function for a channel
1395 * @napi: NAPI structure for the channel
1396 * @budget: Budget supplied by NAPI core
Alex Eldere3eea082020-07-13 07:24:18 -05001397 *
1398 * Return: Number of items polled (<= budget)
Alex Elder650d1602020-03-05 22:28:21 -06001399 *
1400 * Single transactions completed by hardware are polled until either
1401 * the budget is exhausted, or there are no more. Each transaction
1402 * polled is passed to gsi_trans_complete(), to perform remaining
1403 * completion processing and retire/free the transaction.
1404 */
1405static int gsi_channel_poll(struct napi_struct *napi, int budget)
1406{
1407 struct gsi_channel *channel;
1408 int count = 0;
1409
1410 channel = container_of(napi, struct gsi_channel, napi);
1411 while (count < budget) {
1412 struct gsi_trans *trans;
1413
Alex Elderf45a7bc2020-05-15 14:52:03 -05001414 count++;
Alex Elder650d1602020-03-05 22:28:21 -06001415 trans = gsi_channel_poll_one(channel);
1416 if (!trans)
1417 break;
1418 gsi_trans_complete(trans);
1419 }
1420
1421 if (count < budget) {
1422 napi_complete(&channel->napi);
1423 gsi_irq_ieob_enable(channel->gsi, channel->evt_ring_id);
1424 }
1425
1426 return count;
1427}
1428
1429/* The event bitmap represents which event ids are available for allocation.
1430 * Set bits are not available, clear bits can be used. This function
1431 * initializes the map so all events supported by the hardware are available,
1432 * then precludes any reserved events from being allocated.
1433 */
1434static u32 gsi_event_bitmap_init(u32 evt_ring_max)
1435{
1436 u32 event_bitmap = GENMASK(BITS_PER_LONG - 1, evt_ring_max);
1437
1438 event_bitmap |= GENMASK(GSI_MHI_EVENT_ID_END, GSI_MHI_EVENT_ID_START);
1439
1440 return event_bitmap;
1441}
1442
1443/* Setup function for event rings */
1444static void gsi_evt_ring_setup(struct gsi *gsi)
1445{
1446 /* Nothing to do */
1447}
1448
1449/* Inverse of gsi_evt_ring_setup() */
1450static void gsi_evt_ring_teardown(struct gsi *gsi)
1451{
1452 /* Nothing to do */
1453}
1454
1455/* Setup function for a single channel */
1456static int gsi_channel_setup_one(struct gsi *gsi, u32 channel_id,
Alex Elderf86a1902020-05-04 18:30:02 -05001457 bool legacy)
Alex Elder650d1602020-03-05 22:28:21 -06001458{
1459 struct gsi_channel *channel = &gsi->channel[channel_id];
1460 u32 evt_ring_id = channel->evt_ring_id;
1461 int ret;
1462
1463 if (!channel->gsi)
1464 return 0; /* Ignore uninitialized channels */
1465
1466 ret = gsi_evt_ring_alloc_command(gsi, evt_ring_id);
1467 if (ret)
1468 return ret;
1469
1470 gsi_evt_ring_program(gsi, evt_ring_id);
1471
1472 ret = gsi_channel_alloc_command(gsi, channel_id);
1473 if (ret)
1474 goto err_evt_ring_de_alloc;
1475
Alex Elderf86a1902020-05-04 18:30:02 -05001476 gsi_channel_program(channel, legacy);
Alex Elder650d1602020-03-05 22:28:21 -06001477
1478 if (channel->toward_ipa)
1479 netif_tx_napi_add(&gsi->dummy_dev, &channel->napi,
1480 gsi_channel_poll, NAPI_POLL_WEIGHT);
1481 else
1482 netif_napi_add(&gsi->dummy_dev, &channel->napi,
1483 gsi_channel_poll, NAPI_POLL_WEIGHT);
1484
1485 return 0;
1486
1487err_evt_ring_de_alloc:
1488 /* We've done nothing with the event ring yet so don't reset */
1489 gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1490
1491 return ret;
1492}
1493
1494/* Inverse of gsi_channel_setup_one() */
1495static void gsi_channel_teardown_one(struct gsi *gsi, u32 channel_id)
1496{
1497 struct gsi_channel *channel = &gsi->channel[channel_id];
1498 u32 evt_ring_id = channel->evt_ring_id;
1499
1500 if (!channel->gsi)
1501 return; /* Ignore uninitialized channels */
1502
1503 netif_napi_del(&channel->napi);
1504
1505 gsi_channel_deprogram(channel);
1506 gsi_channel_de_alloc_command(gsi, channel_id);
1507 gsi_evt_ring_reset_command(gsi, evt_ring_id);
1508 gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1509}
1510
1511static int gsi_generic_command(struct gsi *gsi, u32 channel_id,
1512 enum gsi_generic_cmd_opcode opcode)
1513{
1514 struct completion *completion = &gsi->completion;
1515 u32 val;
1516
Alex Elder0b1ba182020-04-30 16:35:12 -05001517 /* First zero the result code field */
1518 val = ioread32(gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
1519 val &= ~GENERIC_EE_RESULT_FMASK;
1520 iowrite32(val, gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
1521
1522 /* Now issue the command */
Alex Elder650d1602020-03-05 22:28:21 -06001523 val = u32_encode_bits(opcode, GENERIC_OPCODE_FMASK);
1524 val |= u32_encode_bits(channel_id, GENERIC_CHID_FMASK);
1525 val |= u32_encode_bits(GSI_EE_MODEM, GENERIC_EE_FMASK);
1526
1527 if (gsi_command(gsi, GSI_GENERIC_CMD_OFFSET, val, completion))
1528 return 0; /* Success! */
1529
1530 dev_err(gsi->dev, "GSI generic command %u to channel %u timed out\n",
1531 opcode, channel_id);
1532
1533 return -ETIMEDOUT;
1534}
1535
1536static int gsi_modem_channel_alloc(struct gsi *gsi, u32 channel_id)
1537{
1538 return gsi_generic_command(gsi, channel_id,
1539 GSI_GENERIC_ALLOCATE_CHANNEL);
1540}
1541
1542static void gsi_modem_channel_halt(struct gsi *gsi, u32 channel_id)
1543{
1544 int ret;
1545
1546 ret = gsi_generic_command(gsi, channel_id, GSI_GENERIC_HALT_CHANNEL);
1547 if (ret)
1548 dev_err(gsi->dev, "error %d halting modem channel %u\n",
1549 ret, channel_id);
1550}
1551
1552/* Setup function for channels */
Alex Elderf86a1902020-05-04 18:30:02 -05001553static int gsi_channel_setup(struct gsi *gsi, bool legacy)
Alex Elder650d1602020-03-05 22:28:21 -06001554{
1555 u32 channel_id = 0;
1556 u32 mask;
1557 int ret;
1558
1559 gsi_evt_ring_setup(gsi);
1560 gsi_irq_enable(gsi);
1561
1562 mutex_lock(&gsi->mutex);
1563
1564 do {
Alex Elderf86a1902020-05-04 18:30:02 -05001565 ret = gsi_channel_setup_one(gsi, channel_id, legacy);
Alex Elder650d1602020-03-05 22:28:21 -06001566 if (ret)
1567 goto err_unwind;
1568 } while (++channel_id < gsi->channel_count);
1569
1570 /* Make sure no channels were defined that hardware does not support */
1571 while (channel_id < GSI_CHANNEL_COUNT_MAX) {
1572 struct gsi_channel *channel = &gsi->channel[channel_id++];
1573
1574 if (!channel->gsi)
1575 continue; /* Ignore uninitialized channels */
1576
1577 dev_err(gsi->dev, "channel %u not supported by hardware\n",
1578 channel_id - 1);
1579 channel_id = gsi->channel_count;
1580 goto err_unwind;
1581 }
1582
1583 /* Allocate modem channels if necessary */
1584 mask = gsi->modem_channel_bitmap;
1585 while (mask) {
1586 u32 modem_channel_id = __ffs(mask);
1587
1588 ret = gsi_modem_channel_alloc(gsi, modem_channel_id);
1589 if (ret)
1590 goto err_unwind_modem;
1591
1592 /* Clear bit from mask only after success (for unwind) */
1593 mask ^= BIT(modem_channel_id);
1594 }
1595
1596 mutex_unlock(&gsi->mutex);
1597
1598 return 0;
1599
1600err_unwind_modem:
1601 /* Compute which modem channels need to be deallocated */
1602 mask ^= gsi->modem_channel_bitmap;
1603 while (mask) {
Alex Elder993cac12020-09-28 18:04:44 -05001604 channel_id = __fls(mask);
Alex Elder650d1602020-03-05 22:28:21 -06001605
1606 mask ^= BIT(channel_id);
1607
1608 gsi_modem_channel_halt(gsi, channel_id);
1609 }
1610
1611err_unwind:
1612 while (channel_id--)
1613 gsi_channel_teardown_one(gsi, channel_id);
1614
1615 mutex_unlock(&gsi->mutex);
1616
1617 gsi_irq_disable(gsi);
1618 gsi_evt_ring_teardown(gsi);
1619
1620 return ret;
1621}
1622
1623/* Inverse of gsi_channel_setup() */
1624static void gsi_channel_teardown(struct gsi *gsi)
1625{
1626 u32 mask = gsi->modem_channel_bitmap;
1627 u32 channel_id;
1628
1629 mutex_lock(&gsi->mutex);
1630
1631 while (mask) {
Alex Elder993cac12020-09-28 18:04:44 -05001632 channel_id = __fls(mask);
Alex Elder650d1602020-03-05 22:28:21 -06001633
1634 mask ^= BIT(channel_id);
1635
1636 gsi_modem_channel_halt(gsi, channel_id);
1637 }
1638
1639 channel_id = gsi->channel_count - 1;
1640 do
1641 gsi_channel_teardown_one(gsi, channel_id);
1642 while (channel_id--);
1643
1644 mutex_unlock(&gsi->mutex);
1645
1646 gsi_irq_disable(gsi);
1647 gsi_evt_ring_teardown(gsi);
1648}
1649
1650/* Setup function for GSI. GSI firmware must be loaded and initialized */
Alex Elderf86a1902020-05-04 18:30:02 -05001651int gsi_setup(struct gsi *gsi, bool legacy)
Alex Elder650d1602020-03-05 22:28:21 -06001652{
Alex Elder84634882020-06-30 07:58:45 -05001653 struct device *dev = gsi->dev;
Alex Elder650d1602020-03-05 22:28:21 -06001654 u32 val;
1655
1656 /* Here is where we first touch the GSI hardware */
1657 val = ioread32(gsi->virt + GSI_GSI_STATUS_OFFSET);
1658 if (!(val & ENABLED_FMASK)) {
Alex Elder84634882020-06-30 07:58:45 -05001659 dev_err(dev, "GSI has not been enabled\n");
Alex Elder650d1602020-03-05 22:28:21 -06001660 return -EIO;
1661 }
1662
1663 val = ioread32(gsi->virt + GSI_GSI_HW_PARAM_2_OFFSET);
1664
1665 gsi->channel_count = u32_get_bits(val, NUM_CH_PER_EE_FMASK);
1666 if (!gsi->channel_count) {
Alex Elder84634882020-06-30 07:58:45 -05001667 dev_err(dev, "GSI reports zero channels supported\n");
Alex Elder650d1602020-03-05 22:28:21 -06001668 return -EINVAL;
1669 }
1670 if (gsi->channel_count > GSI_CHANNEL_COUNT_MAX) {
Alex Elder84634882020-06-30 07:58:45 -05001671 dev_warn(dev,
1672 "limiting to %u channels; hardware supports %u\n",
Alex Elder650d1602020-03-05 22:28:21 -06001673 GSI_CHANNEL_COUNT_MAX, gsi->channel_count);
1674 gsi->channel_count = GSI_CHANNEL_COUNT_MAX;
1675 }
1676
1677 gsi->evt_ring_count = u32_get_bits(val, NUM_EV_PER_EE_FMASK);
1678 if (!gsi->evt_ring_count) {
Alex Elder84634882020-06-30 07:58:45 -05001679 dev_err(dev, "GSI reports zero event rings supported\n");
Alex Elder650d1602020-03-05 22:28:21 -06001680 return -EINVAL;
1681 }
1682 if (gsi->evt_ring_count > GSI_EVT_RING_COUNT_MAX) {
Alex Elder84634882020-06-30 07:58:45 -05001683 dev_warn(dev,
1684 "limiting to %u event rings; hardware supports %u\n",
Alex Elder650d1602020-03-05 22:28:21 -06001685 GSI_EVT_RING_COUNT_MAX, gsi->evt_ring_count);
1686 gsi->evt_ring_count = GSI_EVT_RING_COUNT_MAX;
1687 }
1688
1689 /* Initialize the error log */
1690 iowrite32(0, gsi->virt + GSI_ERROR_LOG_OFFSET);
1691
1692 /* Writing 1 indicates IRQ interrupts; 0 would be MSI */
1693 iowrite32(1, gsi->virt + GSI_CNTXT_INTSET_OFFSET);
1694
Alex Elderf86a1902020-05-04 18:30:02 -05001695 return gsi_channel_setup(gsi, legacy);
Alex Elder650d1602020-03-05 22:28:21 -06001696}
1697
1698/* Inverse of gsi_setup() */
1699void gsi_teardown(struct gsi *gsi)
1700{
1701 gsi_channel_teardown(gsi);
1702}
1703
1704/* Initialize a channel's event ring */
1705static int gsi_channel_evt_ring_init(struct gsi_channel *channel)
1706{
1707 struct gsi *gsi = channel->gsi;
1708 struct gsi_evt_ring *evt_ring;
1709 int ret;
1710
1711 ret = gsi_evt_ring_id_alloc(gsi);
1712 if (ret < 0)
1713 return ret;
1714 channel->evt_ring_id = ret;
1715
1716 evt_ring = &gsi->evt_ring[channel->evt_ring_id];
1717 evt_ring->channel = channel;
1718
1719 ret = gsi_ring_alloc(gsi, &evt_ring->ring, channel->event_count);
1720 if (!ret)
1721 return 0; /* Success! */
1722
1723 dev_err(gsi->dev, "error %d allocating channel %u event ring\n",
1724 ret, gsi_channel_id(channel));
1725
1726 gsi_evt_ring_id_free(gsi, channel->evt_ring_id);
1727
1728 return ret;
1729}
1730
1731/* Inverse of gsi_channel_evt_ring_init() */
1732static void gsi_channel_evt_ring_exit(struct gsi_channel *channel)
1733{
1734 u32 evt_ring_id = channel->evt_ring_id;
1735 struct gsi *gsi = channel->gsi;
1736 struct gsi_evt_ring *evt_ring;
1737
1738 evt_ring = &gsi->evt_ring[evt_ring_id];
1739 gsi_ring_free(gsi, &evt_ring->ring);
1740 gsi_evt_ring_id_free(gsi, evt_ring_id);
1741}
1742
1743/* Init function for event rings */
1744static void gsi_evt_ring_init(struct gsi *gsi)
1745{
1746 u32 evt_ring_id = 0;
1747
1748 gsi->event_bitmap = gsi_event_bitmap_init(GSI_EVT_RING_COUNT_MAX);
1749 gsi->event_enable_bitmap = 0;
1750 do
1751 init_completion(&gsi->evt_ring[evt_ring_id].completion);
1752 while (++evt_ring_id < GSI_EVT_RING_COUNT_MAX);
1753}
1754
1755/* Inverse of gsi_evt_ring_init() */
1756static void gsi_evt_ring_exit(struct gsi *gsi)
1757{
1758 /* Nothing to do */
1759}
1760
1761static bool gsi_channel_data_valid(struct gsi *gsi,
1762 const struct ipa_gsi_endpoint_data *data)
1763{
1764#ifdef IPA_VALIDATION
1765 u32 channel_id = data->channel_id;
1766 struct device *dev = gsi->dev;
1767
1768 /* Make sure channel ids are in the range driver supports */
1769 if (channel_id >= GSI_CHANNEL_COUNT_MAX) {
Alex Elder84634882020-06-30 07:58:45 -05001770 dev_err(dev, "bad channel id %u; must be less than %u\n",
Alex Elder650d1602020-03-05 22:28:21 -06001771 channel_id, GSI_CHANNEL_COUNT_MAX);
1772 return false;
1773 }
1774
1775 if (data->ee_id != GSI_EE_AP && data->ee_id != GSI_EE_MODEM) {
Alex Elder84634882020-06-30 07:58:45 -05001776 dev_err(dev, "bad EE id %u; not AP or modem\n", data->ee_id);
Alex Elder650d1602020-03-05 22:28:21 -06001777 return false;
1778 }
1779
1780 if (!data->channel.tlv_count ||
1781 data->channel.tlv_count > GSI_TLV_MAX) {
Alex Elder84634882020-06-30 07:58:45 -05001782 dev_err(dev, "channel %u bad tlv_count %u; must be 1..%u\n",
Alex Elder650d1602020-03-05 22:28:21 -06001783 channel_id, data->channel.tlv_count, GSI_TLV_MAX);
1784 return false;
1785 }
1786
1787 /* We have to allow at least one maximally-sized transaction to
1788 * be outstanding (which would use tlv_count TREs). Given how
1789 * gsi_channel_tre_max() is computed, tre_count has to be almost
1790 * twice the TLV FIFO size to satisfy this requirement.
1791 */
1792 if (data->channel.tre_count < 2 * data->channel.tlv_count - 1) {
1793 dev_err(dev, "channel %u TLV count %u exceeds TRE count %u\n",
1794 channel_id, data->channel.tlv_count,
1795 data->channel.tre_count);
1796 return false;
1797 }
1798
1799 if (!is_power_of_2(data->channel.tre_count)) {
Alex Elder84634882020-06-30 07:58:45 -05001800 dev_err(dev, "channel %u bad tre_count %u; not power of 2\n",
Alex Elder650d1602020-03-05 22:28:21 -06001801 channel_id, data->channel.tre_count);
1802 return false;
1803 }
1804
1805 if (!is_power_of_2(data->channel.event_count)) {
Alex Elder84634882020-06-30 07:58:45 -05001806 dev_err(dev, "channel %u bad event_count %u; not power of 2\n",
Alex Elder650d1602020-03-05 22:28:21 -06001807 channel_id, data->channel.event_count);
1808 return false;
1809 }
1810#endif /* IPA_VALIDATION */
1811
1812 return true;
1813}
1814
1815/* Init function for a single channel */
1816static int gsi_channel_init_one(struct gsi *gsi,
1817 const struct ipa_gsi_endpoint_data *data,
1818 bool command, bool prefetch)
1819{
1820 struct gsi_channel *channel;
1821 u32 tre_count;
1822 int ret;
1823
1824 if (!gsi_channel_data_valid(gsi, data))
1825 return -EINVAL;
1826
1827 /* Worst case we need an event for every outstanding TRE */
1828 if (data->channel.tre_count > data->channel.event_count) {
Alex Elder650d1602020-03-05 22:28:21 -06001829 tre_count = data->channel.event_count;
Alex Elder07219992020-04-30 16:35:11 -05001830 dev_warn(gsi->dev, "channel %u limited to %u TREs\n",
1831 data->channel_id, tre_count);
Alex Elder650d1602020-03-05 22:28:21 -06001832 } else {
1833 tre_count = data->channel.tre_count;
1834 }
1835
1836 channel = &gsi->channel[data->channel_id];
1837 memset(channel, 0, sizeof(*channel));
1838
1839 channel->gsi = gsi;
1840 channel->toward_ipa = data->toward_ipa;
1841 channel->command = command;
1842 channel->use_prefetch = command && prefetch;
1843 channel->tlv_count = data->channel.tlv_count;
1844 channel->tre_count = tre_count;
1845 channel->event_count = data->channel.event_count;
1846 init_completion(&channel->completion);
1847
1848 ret = gsi_channel_evt_ring_init(channel);
1849 if (ret)
1850 goto err_clear_gsi;
1851
1852 ret = gsi_ring_alloc(gsi, &channel->tre_ring, data->channel.tre_count);
1853 if (ret) {
1854 dev_err(gsi->dev, "error %d allocating channel %u ring\n",
1855 ret, data->channel_id);
1856 goto err_channel_evt_ring_exit;
1857 }
1858
1859 ret = gsi_channel_trans_init(gsi, data->channel_id);
1860 if (ret)
1861 goto err_ring_free;
1862
1863 if (command) {
1864 u32 tre_max = gsi_channel_tre_max(gsi, data->channel_id);
1865
1866 ret = ipa_cmd_pool_init(channel, tre_max);
1867 }
1868 if (!ret)
1869 return 0; /* Success! */
1870
1871 gsi_channel_trans_exit(channel);
1872err_ring_free:
1873 gsi_ring_free(gsi, &channel->tre_ring);
1874err_channel_evt_ring_exit:
1875 gsi_channel_evt_ring_exit(channel);
1876err_clear_gsi:
1877 channel->gsi = NULL; /* Mark it not (fully) initialized */
1878
1879 return ret;
1880}
1881
1882/* Inverse of gsi_channel_init_one() */
1883static void gsi_channel_exit_one(struct gsi_channel *channel)
1884{
1885 if (!channel->gsi)
1886 return; /* Ignore uninitialized channels */
1887
1888 if (channel->command)
1889 ipa_cmd_pool_exit(channel);
1890 gsi_channel_trans_exit(channel);
1891 gsi_ring_free(channel->gsi, &channel->tre_ring);
1892 gsi_channel_evt_ring_exit(channel);
1893}
1894
1895/* Init function for channels */
1896static int gsi_channel_init(struct gsi *gsi, bool prefetch, u32 count,
1897 const struct ipa_gsi_endpoint_data *data,
1898 bool modem_alloc)
1899{
1900 int ret = 0;
1901 u32 i;
1902
1903 gsi_evt_ring_init(gsi);
1904
1905 /* The endpoint data array is indexed by endpoint name */
1906 for (i = 0; i < count; i++) {
1907 bool command = i == IPA_ENDPOINT_AP_COMMAND_TX;
1908
1909 if (ipa_gsi_endpoint_data_empty(&data[i]))
1910 continue; /* Skip over empty slots */
1911
1912 /* Mark modem channels to be allocated (hardware workaround) */
1913 if (data[i].ee_id == GSI_EE_MODEM) {
1914 if (modem_alloc)
1915 gsi->modem_channel_bitmap |=
1916 BIT(data[i].channel_id);
1917 continue;
1918 }
1919
1920 ret = gsi_channel_init_one(gsi, &data[i], command, prefetch);
1921 if (ret)
1922 goto err_unwind;
1923 }
1924
1925 return ret;
1926
1927err_unwind:
1928 while (i--) {
1929 if (ipa_gsi_endpoint_data_empty(&data[i]))
1930 continue;
1931 if (modem_alloc && data[i].ee_id == GSI_EE_MODEM) {
1932 gsi->modem_channel_bitmap &= ~BIT(data[i].channel_id);
1933 continue;
1934 }
1935 gsi_channel_exit_one(&gsi->channel[data->channel_id]);
1936 }
1937 gsi_evt_ring_exit(gsi);
1938
1939 return ret;
1940}
1941
1942/* Inverse of gsi_channel_init() */
1943static void gsi_channel_exit(struct gsi *gsi)
1944{
1945 u32 channel_id = GSI_CHANNEL_COUNT_MAX - 1;
1946
1947 do
1948 gsi_channel_exit_one(&gsi->channel[channel_id]);
1949 while (channel_id--);
1950 gsi->modem_channel_bitmap = 0;
1951
1952 gsi_evt_ring_exit(gsi);
1953}
1954
1955/* Init function for GSI. GSI hardware does not need to be "ready" */
Alex Elder1d0c09d2020-11-02 11:53:55 -06001956int gsi_init(struct gsi *gsi, struct platform_device *pdev,
1957 enum ipa_version version, u32 count,
1958 const struct ipa_gsi_endpoint_data *data)
Alex Elder650d1602020-03-05 22:28:21 -06001959{
Alex Elder84634882020-06-30 07:58:45 -05001960 struct device *dev = &pdev->dev;
Alex Elder650d1602020-03-05 22:28:21 -06001961 struct resource *res;
1962 resource_size_t size;
1963 unsigned int irq;
Alex Elder1d0c09d2020-11-02 11:53:55 -06001964 bool modem_alloc;
1965 bool prefetch;
Alex Elder650d1602020-03-05 22:28:21 -06001966 int ret;
1967
1968 gsi_validate_build();
1969
Alex Elder1d0c09d2020-11-02 11:53:55 -06001970 /* IPA v4.0+ (GSI v2.0+) uses prefetch for the command channel */
1971 prefetch = version != IPA_VERSION_3_5_1;
1972 /* IPA v4.2 requires the AP to allocate channels for the modem */
1973 modem_alloc = version == IPA_VERSION_4_2;
1974
Alex Elder84634882020-06-30 07:58:45 -05001975 gsi->dev = dev;
Alex Elder650d1602020-03-05 22:28:21 -06001976
1977 /* The GSI layer performs NAPI on all endpoints. NAPI requires a
1978 * network device structure, but the GSI layer does not have one,
1979 * so we must create a dummy network device for this purpose.
1980 */
1981 init_dummy_netdev(&gsi->dummy_dev);
1982
Alex Elder650d1602020-03-05 22:28:21 -06001983 ret = platform_get_irq_byname(pdev, "gsi");
1984 if (ret <= 0) {
Alex Elder84634882020-06-30 07:58:45 -05001985 dev_err(dev, "DT error %d getting \"gsi\" IRQ property\n", ret);
Alex Elder650d1602020-03-05 22:28:21 -06001986 return ret ? : -EINVAL;
1987 }
1988 irq = ret;
1989
1990 ret = request_irq(irq, gsi_isr, 0, "gsi", gsi);
1991 if (ret) {
Alex Elder84634882020-06-30 07:58:45 -05001992 dev_err(dev, "error %d requesting \"gsi\" IRQ\n", ret);
Alex Elder650d1602020-03-05 22:28:21 -06001993 return ret;
1994 }
1995 gsi->irq = irq;
1996
Alex Elder650d1602020-03-05 22:28:21 -06001997 /* Get GSI memory range and map it */
1998 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "gsi");
1999 if (!res) {
Alex Elder84634882020-06-30 07:58:45 -05002000 dev_err(dev, "DT error getting \"gsi\" memory property\n");
Alex Elder650d1602020-03-05 22:28:21 -06002001 ret = -ENODEV;
Alex Elder54f7e442020-09-17 12:39:26 -05002002 goto err_free_irq;
Alex Elder650d1602020-03-05 22:28:21 -06002003 }
2004
2005 size = resource_size(res);
2006 if (res->start > U32_MAX || size > U32_MAX - res->start) {
Alex Elder84634882020-06-30 07:58:45 -05002007 dev_err(dev, "DT memory resource \"gsi\" out of range\n");
Alex Elder650d1602020-03-05 22:28:21 -06002008 ret = -EINVAL;
Alex Elder54f7e442020-09-17 12:39:26 -05002009 goto err_free_irq;
Alex Elder650d1602020-03-05 22:28:21 -06002010 }
2011
2012 gsi->virt = ioremap(res->start, size);
2013 if (!gsi->virt) {
Alex Elder84634882020-06-30 07:58:45 -05002014 dev_err(dev, "unable to remap \"gsi\" memory\n");
Alex Elder650d1602020-03-05 22:28:21 -06002015 ret = -ENOMEM;
Alex Elder54f7e442020-09-17 12:39:26 -05002016 goto err_free_irq;
Alex Elder650d1602020-03-05 22:28:21 -06002017 }
2018
2019 ret = gsi_channel_init(gsi, prefetch, count, data, modem_alloc);
2020 if (ret)
2021 goto err_iounmap;
2022
2023 mutex_init(&gsi->mutex);
2024 init_completion(&gsi->completion);
2025
2026 return 0;
2027
2028err_iounmap:
2029 iounmap(gsi->virt);
Alex Elder54f7e442020-09-17 12:39:26 -05002030err_free_irq:
Alex Elder650d1602020-03-05 22:28:21 -06002031 free_irq(gsi->irq, gsi);
2032
2033 return ret;
2034}
2035
2036/* Inverse of gsi_init() */
2037void gsi_exit(struct gsi *gsi)
2038{
2039 mutex_destroy(&gsi->mutex);
2040 gsi_channel_exit(gsi);
Alex Elder650d1602020-03-05 22:28:21 -06002041 free_irq(gsi->irq, gsi);
2042 iounmap(gsi->virt);
2043}
2044
2045/* The maximum number of outstanding TREs on a channel. This limits
2046 * a channel's maximum number of transactions outstanding (worst case
2047 * is one TRE per transaction).
2048 *
2049 * The absolute limit is the number of TREs in the channel's TRE ring,
2050 * and in theory we should be able use all of them. But in practice,
2051 * doing that led to the hardware reporting exhaustion of event ring
2052 * slots for writing completion information. So the hardware limit
2053 * would be (tre_count - 1).
2054 *
2055 * We reduce it a bit further though. Transaction resource pools are
2056 * sized to be a little larger than this maximum, to allow resource
2057 * allocations to always be contiguous. The number of entries in a
2058 * TRE ring buffer is a power of 2, and the extra resources in a pool
2059 * tends to nearly double the memory allocated for it. Reducing the
2060 * maximum number of outstanding TREs allows the number of entries in
2061 * a pool to avoid crossing that power-of-2 boundary, and this can
2062 * substantially reduce pool memory requirements. The number we
2063 * reduce it by matches the number added in gsi_trans_pool_init().
2064 */
2065u32 gsi_channel_tre_max(struct gsi *gsi, u32 channel_id)
2066{
2067 struct gsi_channel *channel = &gsi->channel[channel_id];
2068
2069 /* Hardware limit is channel->tre_count - 1 */
2070 return channel->tre_count - (channel->tlv_count - 1);
2071}
2072
2073/* Returns the maximum number of TREs in a single transaction for a channel */
2074u32 gsi_channel_trans_tre_max(struct gsi *gsi, u32 channel_id)
2075{
2076 struct gsi_channel *channel = &gsi->channel[channel_id];
2077
2078 return channel->tlv_count;
2079}