/* SPDX-License-Identifier: GPL-2.0-only1*2* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.3* Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.4*/56#ifndef _QAIC_H_7#define _QAIC_H_89#include <linux/interrupt.h>10#include <linux/kref.h>11#include <linux/mhi.h>12#include <linux/mutex.h>13#include <linux/pci.h>14#include <linux/spinlock.h>15#include <linux/srcu.h>16#include <linux/wait.h>17#include <linux/workqueue.h>18#include <drm/drm_device.h>19#include <drm/drm_gem.h>2021#define QAIC_DBC_BASE SZ_128K22#define QAIC_DBC_SIZE SZ_4K23#define QAIC_SSR_DBC_SENTINEL U32_MAX /* No ongoing SSR sentinel */2425#define QAIC_NO_PARTITION -12627#define QAIC_DBC_OFF(i) ((i) * QAIC_DBC_SIZE + QAIC_DBC_BASE)2829#define to_qaic_bo(obj) container_of(obj, struct qaic_bo, base)30#define to_qaic_drm_device(dev) container_of(dev, struct qaic_drm_device, drm)31#define to_drm(qddev) (&(qddev)->drm)32#define to_accel_kdev(qddev) (to_drm(qddev)->accel->kdev) /* Return Linux device of accel node */33#define to_qaic_device(dev) (to_qaic_drm_device((dev))->qdev)3435enum aic_families {36FAMILY_AIC100,37FAMILY_AIC200,38FAMILY_MAX,39};4041enum __packed dev_states {42/* Device is offline or will be very soon */43QAIC_OFFLINE,44/* Device is booting, not clear if it's in a usable state */45QAIC_BOOT,46/* Device is fully operational */47QAIC_ONLINE,48};4950enum dbc_states {51/* DBC is free and can be activated */52DBC_STATE_IDLE,53/* DBC is activated and a workload is running on device */54DBC_STATE_ASSIGNED,55/* Sub-system associated with this workload has crashed and it will shutdown soon */56DBC_STATE_BEFORE_SHUTDOWN,57/* Sub-system associated with this workload has crashed and it has shutdown */58DBC_STATE_AFTER_SHUTDOWN,59/* Sub-system associated with this workload is shutdown and it will be powered up soon */60DBC_STATE_BEFORE_POWER_UP,61/* Sub-system associated with this workload is now powered up */62DBC_STATE_AFTER_POWER_UP,63DBC_STATE_MAX,64};6566extern bool datapath_polling;6768struct qaic_user {69/* Uniquely identifies this user for the device */70int handle;71struct kref ref_count;72/* Char device opened by this user */73struct qaic_drm_device *qddev;74/* Node in list of users that opened this drm device */75struct list_head node;76/* SRCU used to synchronize this user during cleanup */77struct srcu_struct qddev_lock;78atomic_t chunk_id;79};8081struct dma_bridge_chan {82/* Pointer to device strcut maintained by driver */83struct qaic_device *qdev;84/* ID of this DMA bridge channel(DBC) */85unsigned int id;86/* Synchronizes access to xfer_list */87spinlock_t xfer_lock;88/* Base address of request queue */89void *req_q_base;90/* Base address of response queue */91void *rsp_q_base;92/*93* Base bus address of request queue. Response queue bus address can be94* calculated by adding request queue size to this variable95*/96dma_addr_t dma_addr;97/* Total size of request and response queue in byte */98u32 total_size;99/* Capacity of request/response queue */100u32 nelem;101/* The user that opened this DBC */102struct qaic_user *usr;103/*104* Request ID of next memory handle that goes in request queue. One105* memory handle can enqueue more than one request elements, all106* this requests that belong to same memory handle have same request ID107*/108u16 next_req_id;109/* true: DBC is in use; false: DBC not in use */110bool in_use;111/*112* Base address of device registers. Used to read/write request and113* response queue's head and tail pointer of this DBC.114*/115void __iomem *dbc_base;116/* Synchronizes access to Request queue's head and tail pointer */117struct mutex req_lock;118/* Head of list where each node is a memory handle queued in request queue */119struct list_head xfer_list;120/* Synchronizes DBC readers during cleanup */121struct srcu_struct ch_lock;122/*123* When this DBC is released, any thread waiting on this wait queue is124* woken up125*/126wait_queue_head_t dbc_release;127/* Head of list where each node is a bo associated with this DBC */128struct list_head bo_lists;129/* The irq line for this DBC. Used for polling */130unsigned int irq;131/* Polling work item to simulate interrupts */132struct work_struct poll_work;133/* Represents various states of this DBC from enum dbc_states */134unsigned int state;135};136137struct qaic_device {138/* Pointer to base PCI device struct of our physical device */139struct pci_dev *pdev;140/* Req. ID of request that will be queued next in MHI control device */141u32 next_seq_num;142/* Base address of the MHI bar */143void __iomem *bar_mhi;144/* Base address of the DBCs bar */145void __iomem *bar_dbc;146/* Controller structure for MHI devices */147struct mhi_controller *mhi_cntrl;148/* MHI control channel device */149struct mhi_device *cntl_ch;150/* List of requests queued in MHI control device */151struct list_head cntl_xfer_list;152/* Synchronizes MHI control device transactions and its xfer list */153struct mutex cntl_mutex;154/* Array of DBC struct of this device */155struct dma_bridge_chan *dbc;156/* Work queue for tasks related to MHI control device */157struct workqueue_struct *cntl_wq;158/* Synchronizes all the users of device during cleanup */159struct srcu_struct dev_lock;160/* Track the state of the device during resets */161enum dev_states dev_state;162/* true: single MSI is used to operate device */163bool single_msi;164/*165* true: A tx MHI transaction has failed and a rx buffer is still queued166* in control device. Such a buffer is considered lost rx buffer167* false: No rx buffer is lost in control device168*/169bool cntl_lost_buf;170/* Maximum number of DBC supported by this device */171u32 num_dbc;172/* Reference to the drm_device for this device when it is created */173struct qaic_drm_device *qddev;174/* Generate the CRC of a control message */175u32 (*gen_crc)(void *msg);176/* Validate the CRC of a control message */177bool (*valid_crc)(void *msg);178/* MHI "QAIC_TIMESYNC" channel device */179struct mhi_device *qts_ch;180/* Work queue for tasks related to MHI "QAIC_TIMESYNC" channel */181struct workqueue_struct *qts_wq;182/* MHI "QAIC_TIMESYNC_PERIODIC" channel device */183struct mhi_device *mqts_ch;184/* Head of list of page allocated by MHI bootlog device */185struct list_head bootlog;186/* MHI bootlog channel device */187struct mhi_device *bootlog_ch;188/* Work queue for tasks related to MHI bootlog device */189struct workqueue_struct *bootlog_wq;190/* Synchronizes access of pages in MHI bootlog device */191struct mutex bootlog_mutex;192/* MHI RAS channel device */193struct mhi_device *ras_ch;194/* Correctable error count */195unsigned int ce_count;196/* Un-correctable error count */197unsigned int ue_count;198/* Un-correctable non-fatal error count */199unsigned int ue_nf_count;200/* MHI SSR channel device */201struct mhi_device *ssr_ch;202/* Work queue for tasks related to MHI SSR device */203struct workqueue_struct *ssr_wq;204/* Buffer to collect SSR crashdump via SSR MHI channel */205void *ssr_mhi_buf;206/* DBC which is under SSR. Sentinel U32_MAX would mean that no SSR in progress */207u32 ssr_dbc;208};209210struct qaic_drm_device {211/* The drm device struct of this drm device */212struct drm_device drm;213/* Pointer to the root device struct driven by this driver */214struct qaic_device *qdev;215/*216* The physical device can be partition in number of logical devices.217* And each logical device is given a partition id. This member stores218* that id. QAIC_NO_PARTITION is a sentinel used to mark that this drm219* device is the actual physical device220*/221s32 partition_id;222/* Head in list of users who have opened this drm device */223struct list_head users;224/* Synchronizes access to users list */225struct mutex users_mutex;226/* Pointer to array of DBC sysfs attributes */227void *sysfs_attrs;228};229230struct qaic_bo {231struct drm_gem_object base;232/* Scatter/gather table for allocate/imported BO */233struct sg_table *sgt;234/* Head in list of slices of this BO */235struct list_head slices;236/* Total nents, for all slices of this BO */237int total_slice_nents;238/*239* Direction of transfer. It can assume only two value DMA_TO_DEVICE and240* DMA_FROM_DEVICE.241*/242int dir;243/* The pointer of the DBC which operates on this BO */244struct dma_bridge_chan *dbc;245/* Number of slice that belongs to this buffer */246u32 nr_slice;247/* Number of slice that have been transferred by DMA engine */248u32 nr_slice_xfer_done;249/*250* If true then user has attached slicing information to this BO by251* calling DRM_IOCTL_QAIC_ATTACH_SLICE_BO ioctl.252*/253bool sliced;254/* Request ID of this BO if it is queued for execution */255u16 req_id;256/* Wait on this for completion of DMA transfer of this BO */257struct completion xfer_done;258/*259* Node in linked list where head is dbc->xfer_list.260* This link list contain BO's that are queued for DMA transfer.261*/262struct list_head xfer_list;263/*264* Node in linked list where head is dbc->bo_lists.265* This link list contain BO's that are associated with the DBC it is266* linked to.267*/268struct list_head bo_list;269struct {270/*271* Latest timestamp(ns) at which kernel received a request to272* execute this BO273*/274u64 req_received_ts;275/*276* Latest timestamp(ns) at which kernel enqueued requests of277* this BO for execution in DMA queue278*/279u64 req_submit_ts;280/*281* Latest timestamp(ns) at which kernel received a completion282* interrupt for requests of this BO283*/284u64 req_processed_ts;285/*286* Number of elements already enqueued in DMA queue before287* enqueuing requests of this BO288*/289u32 queue_level_before;290} perf_stats;291/* Synchronizes BO operations */292struct mutex lock;293};294295struct bo_slice {296/* Mapped pages */297struct sg_table *sgt;298/* Number of requests required to queue in DMA queue */299int nents;300/* See enum dma_data_direction */301int dir;302/* Actual requests that will be copied in DMA queue */303struct dbc_req *reqs;304struct kref ref_count;305/* true: No DMA transfer required */306bool no_xfer;307/* Pointer to the parent BO handle */308struct qaic_bo *bo;309/* Node in list of slices maintained by parent BO */310struct list_head slice;311/* Size of this slice in bytes */312u64 size;313/* Offset of this slice in buffer */314u64 offset;315};316317int get_dbc_req_elem_size(void);318int get_dbc_rsp_elem_size(void);319int get_cntl_version(struct qaic_device *qdev, struct qaic_user *usr, u16 *major, u16 *minor);320int qaic_manage_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);321void qaic_mhi_ul_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result);322323void qaic_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result);324325int qaic_control_open(struct qaic_device *qdev);326void qaic_control_close(struct qaic_device *qdev);327void qaic_release_usr(struct qaic_device *qdev, struct qaic_user *usr);328329irqreturn_t dbc_irq_threaded_fn(int irq, void *data);330irqreturn_t dbc_irq_handler(int irq, void *data);331int disable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr);332void enable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr);333void wakeup_dbc(struct qaic_device *qdev, u32 dbc_id);334void release_dbc(struct qaic_device *qdev, u32 dbc_id);335void qaic_data_get_fifo_info(struct dma_bridge_chan *dbc, u32 *head, u32 *tail);336337void wake_all_cntl(struct qaic_device *qdev);338void qaic_dev_reset_clean_local_state(struct qaic_device *qdev);339340struct drm_gem_object *qaic_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf);341342int qaic_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);343int qaic_mmap_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);344int qaic_attach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);345int qaic_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);346int qaic_partial_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);347int qaic_wait_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);348int qaic_perf_stats_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);349int qaic_detach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);350void qaic_irq_polling_work(struct work_struct *work);351void qaic_dbc_enter_ssr(struct qaic_device *qdev, u32 dbc_id);352void qaic_dbc_exit_ssr(struct qaic_device *qdev);353354/* qaic_sysfs.c */355int qaic_sysfs_init(struct qaic_drm_device *qddev);356void qaic_sysfs_remove(struct qaic_drm_device *qddev);357void set_dbc_state(struct qaic_device *qdev, u32 dbc_id, unsigned int state);358359#endif /* _QAIC_H_ */360361362