1 /* SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020-21 Intel Corporation.
6 #ifndef IOSM_IPC_IMEM_H
7 #define IOSM_IPC_IMEM_H
9 #include <linux/skbuff.h>
12 #include "iosm_ipc_mmio.h"
13 #include "iosm_ipc_pcie.h"
14 #include "iosm_ipc_uevent.h"
15 #include "iosm_ipc_wwan.h"
16 #include "iosm_ipc_task_queue.h"
20 /* IRQ moderation in usec */
22 #define IRQ_MOD_NET 1000
23 #define IRQ_MOD_TRC 4000
25 /* Either the PSI image is accepted by CP or the suspended flash tool is waken,
26 * informed that the CP ROM driver is not ready to process the PSI image.
29 #define IPC_PSI_TRANSFER_TIMEOUT 3000
31 /* Timeout in 20 msec to wait for the modem to boot up to
32 * IPC_MEM_DEVICE_IPC_INIT state.
33 * unit : milliseconds (500 * ipc_util_msleep(20))
35 #define IPC_MODEM_BOOT_TIMEOUT 500
37 /* Wait timeout for ipc status reflects IPC_MEM_DEVICE_IPC_UNINIT
40 #define IPC_MODEM_UNINIT_TIMEOUT_MS 30
42 /* Pending time for processing data.
45 #define IPC_PEND_DATA_TIMEOUT 500
47 /* The timeout in milliseconds for application to wait for remote time. */
48 #define IPC_REMOTE_TS_TIMEOUT_MS 10
50 /* Timeout for TD allocation retry.
53 #define IPC_TD_ALLOC_TIMER_PERIOD_MS 100
55 /* Host sleep target is host */
56 #define IPC_HOST_SLEEP_HOST 0
58 /* Host sleep target is device */
59 #define IPC_HOST_SLEEP_DEVICE 1
61 /* Sleep message, target host: AP enters sleep / target device: CP is
62 * allowed to enter sleep and shall use the host sleep protocol
64 #define IPC_HOST_SLEEP_ENTER_SLEEP 0
66 /* Sleep_message, target host: AP exits sleep / target device: CP is
67 * NOT allowed to enter sleep
69 #define IPC_HOST_SLEEP_EXIT_SLEEP 1
71 #define IMEM_IRQ_DONT_CARE (-1)
73 #define IPC_MEM_MAX_CHANNELS 7
75 #define IPC_MEM_MUX_IP_SESSION_ENTRIES 8
77 #define IPC_MEM_MUX_IP_CH_IF_ID 0
79 #define TD_UPDATE_DEFAULT_TIMEOUT_USEC 1900
81 #define FORCE_UPDATE_DEFAULT_TIMEOUT_USEC 500
83 /* Sleep_message, target host: not applicable / target device: CP is
84 * allowed to enter sleep and shall NOT use the device sleep protocol
86 #define IPC_HOST_SLEEP_ENTER_SLEEP_NO_PROTOCOL 2
88 /* in_band_crash_signal IPC_MEM_INBAND_CRASH_SIG
89 * Modem crash notification configuration. If this value is non-zero then
90 * FEATURE_SET message will be sent to the Modem as a result the Modem will
91 * signal Crash via Execution Stage register. If this value is zero then Modem
92 * will use out-of-band method to notify about it's Crash.
94 #define IPC_MEM_INBAND_CRASH_SIG 1
96 /* Extra headroom to be allocated for DL SKBs to allow addition of Ethernet
99 #define IPC_MEM_DL_ETH_OFFSET 16
101 #define IPC_CB(skb) ((struct ipc_skb_cb *)((skb)->cb))
103 #define FULLY_FUNCTIONAL 0
105 /* List of the supported UL/DL pipes. */
134 /* Enum defining channel states. */
135 enum ipc_channel_state {
137 IMEM_CHANNEL_RESERVED,
139 IMEM_CHANNEL_CLOSING,
154 * enum ipc_ctype - Enum defining supported channel type needed for control
156 * @IPC_CTYPE_WWAN: Used for IP traffic
157 * @IPC_CTYPE_CTRL: Used for Control Communication
164 /* Pipe direction. */
165 enum ipc_mem_pipe_dir {
170 /* HP update identifier. To be used as data for ipc_cp_irq_hpda_update() */
171 enum ipc_hp_identifier {
174 IPC_HP_WAKEUP_SPEC_TMR,
175 IPC_HP_TD_UPD_TMR_START,
177 IPC_HP_FAST_TD_UPD_TMR,
180 IPC_HP_NET_CHANNEL_INIT,
185 * struct ipc_pipe - Structure for Pipe.
186 * @tdr_start: Ipc private protocol Transfer Descriptor Ring
187 * @channel: Id of the sio device, set by imem_sio_open,
188 * needed to pass DL char to the user terminal
189 * @skbr_start: Circular buffer for skbuf and the buffer
190 * reference in a tdr_start entry.
191 * @phy_tdr_start: Transfer descriptor start address
192 * @old_head: last head pointer reported to CP.
193 * @old_tail: AP read position before CP moves the read
194 * position to write/head. If CP has consumed the
195 * buffers, AP has to freed the skbuf starting at
196 * tdr_start[old_tail].
197 * @nr_of_entries: Number of elements of skb_start and tdr_start.
198 * @max_nr_of_queued_entries: Maximum number of queued entries in TDR
199 * @accumulation_backoff: Accumulation in usec for accumulation
200 * backoff (0 = no acc backoff)
201 * @irq_moderation: timer in usec for irq_moderation
202 * (0=no irq moderation)
203 * @pipe_nr: Pipe identification number
204 * @irq: Interrupt vector
205 * @dir: Direction of data stream in pipe
206 * @td_tag: Unique tag of the buffer queued
207 * @buf_size: Buffer size (in bytes) for preallocated
208 * buffers (for DL pipes)
209 * @nr_of_queued_entries: Aueued number of entries
210 * @is_open: Check for open pipe status
213 struct ipc_protocol_td *tdr_start;
214 struct ipc_mem_channel *channel;
215 struct sk_buff **skbr_start;
216 dma_addr_t phy_tdr_start;
220 u32 max_nr_of_queued_entries;
221 u32 accumulation_backoff;
225 enum ipc_mem_pipe_dir dir;
228 u16 nr_of_queued_entries;
233 * struct ipc_mem_channel - Structure for Channel.
234 * @channel_id: Instance of the channel list and is return to the user
235 * at the end of the open operation.
236 * @ctype: Control or netif channel.
237 * @index: unique index per ctype
238 * @ul_pipe: pipe objects
239 * @dl_pipe: pipe objects
240 * @if_id: Interface ID
241 * @net_err_count: Number of downlink errors returned by ipc_wwan_receive
242 * interface at the entry point of the IP stack.
243 * @state: Free, reserved or busy (in use).
244 * @ul_sem: Needed for the blocking write or uplink transfer.
245 * @ul_list: Uplink accumulator which is filled by the uplink
246 * char app or IP stack. The socket buffer pointer are
247 * added to the descriptor list in the kthread context.
249 struct ipc_mem_channel {
251 enum ipc_ctype ctype;
253 struct ipc_pipe ul_pipe;
254 struct ipc_pipe dl_pipe;
257 enum ipc_channel_state state;
258 struct completion ul_sem;
259 struct sk_buff_head ul_list;
263 * enum ipc_phase - Different AP and CP phases.
264 * The enums defined after "IPC_P_ROM" and before
265 * "IPC_P_RUN" indicates the operating state where CP can
266 * respond to any requests. So while introducing new phase
267 * this shall be taken into consideration.
268 * @IPC_P_OFF: On host PC, the PCIe device link settings are known
269 * about the combined power on. PC is running, the driver
270 * is loaded and CP is in power off mode. The PCIe bus
271 * driver call the device power mode D3hot. In this phase
272 * the driver the polls the device, until the device is in
273 * the power on state and signals the power mode D0.
274 * @IPC_P_OFF_REQ: The intermediate phase between cleanup activity starts
276 * @IPC_P_CRASH: The phase indicating CP crash
277 * @IPC_P_CD_READY: The phase indicating CP core dump is ready
278 * @IPC_P_ROM: After power on, CP starts in ROM mode and the IPC ROM
279 * driver is waiting 150 ms for the AP active notification
280 * saved in the PCI link status register.
281 * @IPC_P_PSI: Primary signed image download phase
282 * @IPC_P_EBL: Extended bootloader pahse
283 * @IPC_P_RUN: The phase after flashing to RAM is the RUNTIME phase.
297 * struct iosm_imem - Current state of the IPC shared memory.
298 * @mmio: mmio instance to access CP MMIO area /
299 * doorbell scratchpad.
300 * @ipc_protocol: IPC Protocol instance
301 * @ipc_task: Task for entry into ipc task queue
302 * @wwan: WWAN device pointer
303 * @mux: IP Data multiplexing state.
304 * @sio: IPC SIO data structure pointer
305 * @ipc_port: IPC PORT data structure pointer
307 * @dev: Pointer to device structure
308 * @flash_channel_id: Reserved channel id for flashing to RAM.
309 * @ipc_requested_state: Expected IPC state on CP.
310 * @channels: Channel list with UL/DL pipe pairs.
311 * @ipc_status: local ipc_status
312 * @nr_of_channels: number of configured channels
313 * @startup_timer: startup timer for NAND support.
314 * @hrtimer_period: Hr timer period
315 * @tdupdate_timer: Delay the TD update doorbell.
316 * @fast_update_timer: forced head pointer update delay timer.
317 * @td_alloc_timer: Timer for DL pipe TD allocation retry
318 * @rom_exit_code: Mapped boot rom exit code.
319 * @enter_runtime: 1 means the transition to runtime phase was
321 * @ul_pend_sem: Semaphore to wait/complete of UL TDs
322 * before closing pipe.
323 * @app_notify_ul_pend: Signal app if UL TD is pending
324 * @dl_pend_sem: Semaphore to wait/complete of DL TDs
325 * before closing pipe.
326 * @app_notify_dl_pend: Signal app if DL TD is pending
327 * @phase: Operating phase like runtime.
328 * @pci_device_id: Device ID
329 * @cp_version: CP version
330 * @device_sleep: Device sleep state
331 * @run_state_worker: Pointer to worker component for device
332 * setup operations to be called when modem
334 * @ev_irq_pending: 0 means inform the IPC tasklet to
335 * process the irq actions.
336 * @flag: Flag to monitor the state of driver
337 * @td_update_timer_suspended: if true then td update timer suspend
338 * @ev_cdev_write_pending: 0 means inform the IPC tasklet to pass
339 * the accumulated uplink buffers to CP.
340 * @ev_mux_net_transmit_pending:0 means inform the IPC tasklet to pass
341 * @reset_det_n: Reset detect flag
342 * @pcie_wake_n: Pcie wake flag
345 struct iosm_mmio *mmio;
346 struct iosm_protocol *ipc_protocol;
347 struct ipc_task *ipc_task;
348 struct iosm_wwan *wwan;
349 struct iosm_mux *mux;
350 struct iosm_cdev *ipc_port[IPC_MEM_MAX_CHANNELS];
351 struct iosm_pcie *pcie;
353 int flash_channel_id;
354 enum ipc_mem_device_ipc_state ipc_requested_state;
355 struct ipc_mem_channel channels[IPC_MEM_MAX_CHANNELS];
358 struct hrtimer startup_timer;
359 ktime_t hrtimer_period;
360 struct hrtimer tdupdate_timer;
361 struct hrtimer fast_update_timer;
362 struct hrtimer td_alloc_timer;
363 enum rom_exit_code rom_exit_code;
365 struct completion ul_pend_sem;
366 u32 app_notify_ul_pend;
367 struct completion dl_pend_sem;
368 u32 app_notify_dl_pend;
369 enum ipc_phase phase;
373 struct work_struct run_state_worker;
374 u8 ev_irq_pending[IPC_IRQ_VECTORS];
376 u8 td_update_timer_suspended:1,
377 ev_cdev_write_pending:1,
378 ev_mux_net_transmit_pending:1,
384 * ipc_imem_init - Initialize the shared memory region
385 * @pcie: Pointer to core driver data-struct
386 * @device_id: PCI device ID
387 * @mmio: Pointer to the mmio area
388 * @dev: Pointer to device structure
390 * Returns: Initialized imem pointer on success else NULL
392 struct iosm_imem *ipc_imem_init(struct iosm_pcie *pcie, unsigned int device_id,
393 void __iomem *mmio, struct device *dev);
396 * ipc_imem_pm_s2idle_sleep - Set PM variables to sleep/active for
397 * s2idle sleep/active
398 * @ipc_imem: Pointer to imem data-struct
399 * @sleep: Set PM Variable to sleep/active
401 void ipc_imem_pm_s2idle_sleep(struct iosm_imem *ipc_imem, bool sleep);
404 * ipc_imem_pm_suspend - The HAL shall ask the shared memory layer
405 * whether D3 is allowed.
406 * @ipc_imem: Pointer to imem data-struct
408 void ipc_imem_pm_suspend(struct iosm_imem *ipc_imem);
411 * ipc_imem_pm_resume - The HAL shall inform the shared memory layer
412 * that the device is active.
413 * @ipc_imem: Pointer to imem data-struct
415 void ipc_imem_pm_resume(struct iosm_imem *ipc_imem);
418 * ipc_imem_cleanup - Inform CP and free the shared memory resources.
419 * @ipc_imem: Pointer to imem data-struct
421 void ipc_imem_cleanup(struct iosm_imem *ipc_imem);
424 * ipc_imem_irq_process - Shift the IRQ actions to the IPC thread.
425 * @ipc_imem: Pointer to imem data-struct
428 void ipc_imem_irq_process(struct iosm_imem *ipc_imem, int irq);
431 * imem_get_device_sleep_state - Get the device sleep state value.
432 * @ipc_imem: Pointer to imem instance
434 * Returns: device sleep state
436 int imem_get_device_sleep_state(struct iosm_imem *ipc_imem);
439 * ipc_imem_td_update_timer_suspend - Updates the TD Update Timer suspend flag.
440 * @ipc_imem: Pointer to imem data-struct
441 * @suspend: Flag to update. If TRUE then HP update doorbell is triggered to
442 * device without any wait. If FALSE then HP update doorbell is
443 * delayed until timeout.
445 void ipc_imem_td_update_timer_suspend(struct iosm_imem *ipc_imem, bool suspend);
448 * ipc_imem_channel_close - Release the channel resources.
449 * @ipc_imem: Pointer to imem data-struct
450 * @channel_id: Channel ID to be cleaned up.
452 void ipc_imem_channel_close(struct iosm_imem *ipc_imem, int channel_id);
455 * ipc_imem_channel_alloc - Reserves a channel
456 * @ipc_imem: Pointer to imem data-struct
457 * @index: ID to lookup from the preallocated list.
458 * @ctype: Channel type.
460 * Returns: Index on success and failure value on error
462 int ipc_imem_channel_alloc(struct iosm_imem *ipc_imem, int index,
463 enum ipc_ctype ctype);
466 * ipc_imem_channel_open - Establish the pipes.
467 * @ipc_imem: Pointer to imem data-struct
468 * @channel_id: Channel ID returned during alloc.
469 * @db_id: Doorbell ID for trigger identifier.
471 * Returns: Pointer of ipc_mem_channel on success and NULL on failure.
473 struct ipc_mem_channel *ipc_imem_channel_open(struct iosm_imem *ipc_imem,
474 int channel_id, u32 db_id);
477 * ipc_imem_td_update_timer_start - Starts the TD Update Timer if not running.
478 * @ipc_imem: Pointer to imem data-struct
480 void ipc_imem_td_update_timer_start(struct iosm_imem *ipc_imem);
483 * ipc_imem_ul_write_td - Pass the channel UL list to protocol layer for TD
484 * preparation and sending them to the device.
485 * @ipc_imem: Pointer to imem data-struct
487 * Returns: TRUE of HP Doorbell trigger is pending. FALSE otherwise.
489 bool ipc_imem_ul_write_td(struct iosm_imem *ipc_imem);
492 * ipc_imem_ul_send - Dequeue SKB from channel list and start with
493 * the uplink transfer.If HP Doorbell is pending to be
494 * triggered then starts the TD Update Timer.
495 * @ipc_imem: Pointer to imem data-struct
497 void ipc_imem_ul_send(struct iosm_imem *ipc_imem);
500 * ipc_imem_channel_update - Set or modify pipe config of an existing channel
501 * @ipc_imem: Pointer to imem data-struct
502 * @id: Channel config index
503 * @chnl_cfg: Channel config struct
504 * @irq_moderation: Timer in usec for irq_moderation
506 void ipc_imem_channel_update(struct iosm_imem *ipc_imem, int id,
507 struct ipc_chnl_cfg chnl_cfg, u32 irq_moderation);
510 * ipc_imem_channel_free -Free an IPC channel.
511 * @channel: Channel to be freed
513 void ipc_imem_channel_free(struct ipc_mem_channel *channel);
516 * ipc_imem_hrtimer_stop - Stop the hrtimer
517 * @hr_timer: Pointer to hrtimer instance
519 void ipc_imem_hrtimer_stop(struct hrtimer *hr_timer);
522 * ipc_imem_pipe_cleanup - Reset volatile pipe content for all channels
523 * @ipc_imem: Pointer to imem data-struct
524 * @pipe: Pipe to cleaned up
526 void ipc_imem_pipe_cleanup(struct iosm_imem *ipc_imem, struct ipc_pipe *pipe);
529 * ipc_imem_pipe_close - Send msg to device to close pipe
530 * @ipc_imem: Pointer to imem data-struct
531 * @pipe: Pipe to be closed
533 void ipc_imem_pipe_close(struct iosm_imem *ipc_imem, struct ipc_pipe *pipe);
536 * ipc_imem_phase_update - Get the CP execution state
537 * and map it to the AP phase.
538 * @ipc_imem: Pointer to imem data-struct
540 * Returns: Current ap updated phase
542 enum ipc_phase ipc_imem_phase_update(struct iosm_imem *ipc_imem);
545 * ipc_imem_phase_get_string - Return the current operation
549 * Returns: AP phase string
551 const char *ipc_imem_phase_get_string(enum ipc_phase phase);
554 * ipc_imem_msg_send_feature_set - Send feature set message to modem
555 * @ipc_imem: Pointer to imem data-struct
556 * @reset_enable: 0 = out-of-band, 1 = in-band-crash notification
557 * @atomic_ctx: if disabled call in tasklet context
560 void ipc_imem_msg_send_feature_set(struct iosm_imem *ipc_imem,
561 unsigned int reset_enable, bool atomic_ctx);
564 * ipc_imem_ipc_init_check - Send the init event to CP, wait a certain time and
565 * set CP to runtime with the context information
566 * @ipc_imem: Pointer to imem data-struct
568 void ipc_imem_ipc_init_check(struct iosm_imem *ipc_imem);
571 * ipc_imem_channel_init - Initialize the channel list with UL/DL pipe pairs.
572 * @ipc_imem: Pointer to imem data-struct
573 * @ctype: Channel type
574 * @chnl_cfg: Channel configuration struct
575 * @irq_moderation: Timer in usec for irq_moderation
577 void ipc_imem_channel_init(struct iosm_imem *ipc_imem, enum ipc_ctype ctype,
578 struct ipc_chnl_cfg chnl_cfg, u32 irq_moderation);