Commit 111d746b authored by Scott Branden's avatar Scott Branden Committed by Greg Kroah-Hartman

misc: bcm-vk: add VK messaging support

Add message support in order to be able to communicate
to VK card via message queues.

This info is used for debug purposes via collection of logs via direct
read of BAR space and by sysfs access (in a follow on commit).
Co-developed-by: default avatarDesmond Yan <desmond.yan@broadcom.com>
Acked-by: default avatarOlof Johansson <olof@lixom.net>
Signed-off-by: default avatarDesmond Yan <desmond.yan@broadcom.com>
Signed-off-by: default avatarScott Branden <scott.branden@broadcom.com>
Link: https://lore.kernel.org/r/20210120175827.14820-10-scott.branden@broadcom.comSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent ff428d05
...@@ -6,5 +6,6 @@ ...@@ -6,5 +6,6 @@
obj-$(CONFIG_BCM_VK) += bcm_vk.o obj-$(CONFIG_BCM_VK) += bcm_vk.o
bcm_vk-objs := \ bcm_vk-objs := \
bcm_vk_dev.o \ bcm_vk_dev.o \
bcm_vk_msg.o bcm_vk_msg.o \
bcm_vk_sg.o
...@@ -6,11 +6,13 @@ ...@@ -6,11 +6,13 @@
#ifndef BCM_VK_H #ifndef BCM_VK_H
#define BCM_VK_H #define BCM_VK_H
#include <linux/atomic.h>
#include <linux/firmware.h> #include <linux/firmware.h>
#include <linux/kref.h> #include <linux/kref.h>
#include <linux/miscdevice.h> #include <linux/miscdevice.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/pci.h> #include <linux/pci.h>
#include <linux/poll.h>
#include <linux/sched/signal.h> #include <linux/sched/signal.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <uapi/linux/misc/bcm_vk.h> #include <uapi/linux/misc/bcm_vk.h>
...@@ -93,14 +95,53 @@ ...@@ -93,14 +95,53 @@
#define MAJOR_SOC_REV(_chip_id) (((_chip_id) >> 20) & 0xf) #define MAJOR_SOC_REV(_chip_id) (((_chip_id) >> 20) & 0xf)
#define BAR_CARD_TEMPERATURE 0x45c #define BAR_CARD_TEMPERATURE 0x45c
/* defines for all temperature sensor */
#define BCM_VK_TEMP_FIELD_MASK 0xff
#define BCM_VK_CPU_TEMP_SHIFT 0
#define BCM_VK_DDR0_TEMP_SHIFT 8
#define BCM_VK_DDR1_TEMP_SHIFT 16
#define BAR_CARD_VOLTAGE 0x460 #define BAR_CARD_VOLTAGE 0x460
/* defines for voltage rail conversion */
#define BCM_VK_VOLT_RAIL_MASK 0xffff
#define BCM_VK_3P3_VOLT_REG_SHIFT 16
#define BAR_CARD_ERR_LOG 0x464 #define BAR_CARD_ERR_LOG 0x464
/* Error log register bit definition - register for error alerts */
#define ERR_LOG_UECC BIT(0)
#define ERR_LOG_SSIM_BUSY BIT(1)
#define ERR_LOG_AFBC_BUSY BIT(2)
#define ERR_LOG_HIGH_TEMP_ERR BIT(3)
#define ERR_LOG_WDOG_TIMEOUT BIT(4)
#define ERR_LOG_SYS_FAULT BIT(5)
#define ERR_LOG_RAMDUMP BIT(6)
#define ERR_LOG_COP_WDOG_TIMEOUT BIT(7)
/* warnings */
#define ERR_LOG_MEM_ALLOC_FAIL BIT(8)
#define ERR_LOG_LOW_TEMP_WARN BIT(9)
#define ERR_LOG_ECC BIT(10)
#define ERR_LOG_IPC_DWN BIT(11)
/* Alert bit definitions detectd on host */
#define ERR_LOG_HOST_INTF_V_FAIL BIT(13)
#define ERR_LOG_HOST_HB_FAIL BIT(14)
#define ERR_LOG_HOST_PCIE_DWN BIT(15)
#define BAR_CARD_ERR_MEM 0x468 #define BAR_CARD_ERR_MEM 0x468
/* defines for mem err, all fields have same width */
#define BCM_VK_MEM_ERR_FIELD_MASK 0xff
#define BCM_VK_ECC_MEM_ERR_SHIFT 0
#define BCM_VK_UECC_MEM_ERR_SHIFT 8
/* threshold of event occurrence and logs start to come out */
#define BCM_VK_ECC_THRESHOLD 10
#define BCM_VK_UECC_THRESHOLD 1
#define BAR_CARD_PWR_AND_THRE 0x46c #define BAR_CARD_PWR_AND_THRE 0x46c
/* defines for power and temp threshold, all fields have same width */
#define BCM_VK_PWR_AND_THRE_FIELD_MASK 0xff
#define BCM_VK_LOW_TEMP_THRE_SHIFT 0
#define BCM_VK_HIGH_TEMP_THRE_SHIFT 8
#define BCM_VK_PWR_STATE_SHIFT 16
#define BAR_CARD_STATIC_INFO 0x470 #define BAR_CARD_STATIC_INFO 0x470
...@@ -143,6 +184,11 @@ ...@@ -143,6 +184,11 @@
#define BAR_FIRMWARE_TAG_SIZE 50 #define BAR_FIRMWARE_TAG_SIZE 50
#define FIRMWARE_STATUS_PRE_INIT_DONE 0x1f #define FIRMWARE_STATUS_PRE_INIT_DONE 0x1f
/* VK MSG_ID defines */
#define VK_MSG_ID_BITMAP_SIZE 4096
#define VK_MSG_ID_BITMAP_MASK (VK_MSG_ID_BITMAP_SIZE - 1)
#define VK_MSG_ID_OVERFLOW 0xffff
/* /*
* BAR1 * BAR1
*/ */
...@@ -197,6 +243,10 @@ ...@@ -197,6 +243,10 @@
/* VK device supports a maximum of 3 bars */ /* VK device supports a maximum of 3 bars */
#define MAX_BAR 3 #define MAX_BAR 3
/* default number of msg blk for inband SGL */
#define BCM_VK_DEF_IB_SGL_BLK_LEN 16
#define BCM_VK_IB_SGL_BLK_MAX 24
enum pci_barno { enum pci_barno {
BAR_0 = 0, BAR_0 = 0,
BAR_1, BAR_1,
...@@ -267,9 +317,27 @@ struct bcm_vk_proc_mon_info { ...@@ -267,9 +317,27 @@ struct bcm_vk_proc_mon_info {
struct bcm_vk_proc_mon_entry_t entries[BCM_VK_PROC_MON_MAX]; struct bcm_vk_proc_mon_entry_t entries[BCM_VK_PROC_MON_MAX];
}; };
struct bcm_vk_hb_ctrl {
struct timer_list timer;
u32 last_uptime;
u32 lost_cnt;
};
struct bcm_vk_alert {
u16 flags;
u16 notfs;
};
/* some alert counters that the driver will keep track */
struct bcm_vk_alert_cnts {
u16 ecc;
u16 uecc;
};
struct bcm_vk { struct bcm_vk {
struct pci_dev *pdev; struct pci_dev *pdev;
void __iomem *bar[MAX_BAR]; void __iomem *bar[MAX_BAR];
int num_irqs;
struct bcm_vk_card_info card_info; struct bcm_vk_card_info card_info;
struct bcm_vk_proc_mon_info proc_mon_info; struct bcm_vk_proc_mon_info proc_mon_info;
...@@ -283,9 +351,17 @@ struct bcm_vk { ...@@ -283,9 +351,17 @@ struct bcm_vk {
/* Reference-counting to handle file operations */ /* Reference-counting to handle file operations */
struct kref kref; struct kref kref;
spinlock_t msg_id_lock; /* Spinlock for msg_id */
u16 msg_id;
DECLARE_BITMAP(bmap, VK_MSG_ID_BITMAP_SIZE);
spinlock_t ctx_lock; /* Spinlock for component context */ spinlock_t ctx_lock; /* Spinlock for component context */
struct bcm_vk_ctx ctx[VK_CMPT_CTX_MAX]; struct bcm_vk_ctx ctx[VK_CMPT_CTX_MAX];
struct bcm_vk_ht_entry pid_ht[VK_PID_HT_SZ]; struct bcm_vk_ht_entry pid_ht[VK_PID_HT_SZ];
pid_t reset_pid; /* process that issue reset */
atomic_t msgq_inited; /* indicate if info has been synced with vk */
struct bcm_vk_msg_chan to_v_msg_chan;
struct bcm_vk_msg_chan to_h_msg_chan;
struct workqueue_struct *wq_thread; struct workqueue_struct *wq_thread;
struct work_struct wq_work; /* work queue for deferred job */ struct work_struct wq_work; /* work queue for deferred job */
...@@ -294,6 +370,15 @@ struct bcm_vk { ...@@ -294,6 +370,15 @@ struct bcm_vk {
dma_addr_t tdma_addr; /* test dma segment bus addr */ dma_addr_t tdma_addr; /* test dma segment bus addr */
struct notifier_block panic_nb; struct notifier_block panic_nb;
u32 ib_sgl_size; /* size allocated for inband sgl insertion */
/* heart beat mechanism control structure */
struct bcm_vk_hb_ctrl hb_ctrl;
/* house-keeping variable of error logs */
spinlock_t host_alert_lock; /* protection to access host_alert struct */
struct bcm_vk_alert host_alert;
struct bcm_vk_alert peer_alert; /* bits set by the card */
struct bcm_vk_alert_cnts alert_cnts;
/* offset of the peer log control in BAR2 */ /* offset of the peer log control in BAR2 */
u32 peerlog_off; u32 peerlog_off;
...@@ -306,8 +391,26 @@ struct bcm_vk { ...@@ -306,8 +391,26 @@ struct bcm_vk {
enum bcm_vk_wq_offload_flags { enum bcm_vk_wq_offload_flags {
BCM_VK_WQ_DWNLD_PEND = 0, BCM_VK_WQ_DWNLD_PEND = 0,
BCM_VK_WQ_DWNLD_AUTO = 1, BCM_VK_WQ_DWNLD_AUTO = 1,
BCM_VK_WQ_NOTF_PEND = 2,
}; };
/* a macro to get an individual field with mask and shift */
#define BCM_VK_EXTRACT_FIELD(_field, _reg, _mask, _shift) \
(_field = (((_reg) >> (_shift)) & (_mask)))
struct bcm_vk_entry {
const u32 mask;
const u32 exp_val;
const char *str;
};
/* alerts that could be generated from peer */
#define BCM_VK_PEER_ERR_NUM 12
extern struct bcm_vk_entry const bcm_vk_peer_err[BCM_VK_PEER_ERR_NUM];
/* alerts detected by the host */
#define BCM_VK_HOST_ERR_NUM 3
extern struct bcm_vk_entry const bcm_vk_host_err[BCM_VK_HOST_ERR_NUM];
/* /*
* check if PCIe interface is down on read. Use it when it is * check if PCIe interface is down on read. Use it when it is
* certain that _val should never be all ones. * certain that _val should never be all ones.
...@@ -354,8 +457,28 @@ static inline bool bcm_vk_msgq_marker_valid(struct bcm_vk *vk) ...@@ -354,8 +457,28 @@ static inline bool bcm_vk_msgq_marker_valid(struct bcm_vk *vk)
} }
int bcm_vk_open(struct inode *inode, struct file *p_file); int bcm_vk_open(struct inode *inode, struct file *p_file);
ssize_t bcm_vk_read(struct file *p_file, char __user *buf, size_t count,
loff_t *f_pos);
ssize_t bcm_vk_write(struct file *p_file, const char __user *buf,
size_t count, loff_t *f_pos);
__poll_t bcm_vk_poll(struct file *p_file, struct poll_table_struct *wait);
int bcm_vk_release(struct inode *inode, struct file *p_file); int bcm_vk_release(struct inode *inode, struct file *p_file);
void bcm_vk_release_data(struct kref *kref); void bcm_vk_release_data(struct kref *kref);
irqreturn_t bcm_vk_msgq_irqhandler(int irq, void *dev_id);
irqreturn_t bcm_vk_notf_irqhandler(int irq, void *dev_id);
int bcm_vk_msg_init(struct bcm_vk *vk);
void bcm_vk_msg_remove(struct bcm_vk *vk);
int bcm_vk_sync_msgq(struct bcm_vk *vk, bool force_sync);
void bcm_vk_blk_drv_access(struct bcm_vk *vk);
s32 bcm_to_h_msg_dequeue(struct bcm_vk *vk);
int bcm_vk_send_shutdown_msg(struct bcm_vk *vk, u32 shut_type,
const pid_t pid, const u32 q_num);
void bcm_to_v_q_doorbell(struct bcm_vk *vk, u32 q_num, u32 db_val);
int bcm_vk_auto_load_all_images(struct bcm_vk *vk); int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
void bcm_vk_hb_init(struct bcm_vk *vk);
void bcm_vk_hb_deinit(struct bcm_vk *vk);
void bcm_vk_handle_notf(struct bcm_vk *vk);
bool bcm_vk_drv_access_ok(struct bcm_vk *vk);
void bcm_vk_set_host_alert(struct bcm_vk *vk, u32 bit_mask);
#endif #endif
This diff is collapsed.
This diff is collapsed.
...@@ -6,6 +6,78 @@ ...@@ -6,6 +6,78 @@
#ifndef BCM_VK_MSG_H #ifndef BCM_VK_MSG_H
#define BCM_VK_MSG_H #define BCM_VK_MSG_H
#include <uapi/linux/misc/bcm_vk.h>
#include "bcm_vk_sg.h"
/* Single message queue control structure */
struct bcm_vk_msgq {
u16 type; /* queue type */
u16 num; /* queue number */
u32 start; /* offset in BAR1 where the queue memory starts */
u32 rd_idx; /* read idx */
u32 wr_idx; /* write idx */
u32 size; /*
* size, which is in number of 16byte blocks,
* to align with the message data structure.
*/
u32 nxt; /*
* nxt offset to the next msg queue struct.
* This is to provide flexibity for alignment purposes.
*/
/* Least significant 16 bits in below field hold doorbell register offset */
#define DB_SHIFT 16
u32 db_offset; /* queue doorbell register offset in BAR0 */
u32 rsvd;
};
/*
* Structure to record static info from the msgq sync. We keep local copy
* for some of these variables for both performance + checking purpose.
*/
struct bcm_vk_sync_qinfo {
void __iomem *q_start;
u32 q_size;
u32 q_mask;
u32 q_low;
u32 q_db_offset;
};
#define VK_MSGQ_MAX_NR 4 /* Maximum number of message queues */
/*
* message block - basic unit in the message where a message's size is always
* N x sizeof(basic_block)
*/
struct vk_msg_blk {
u8 function_id;
#define VK_FID_TRANS_BUF 5
#define VK_FID_SHUTDOWN 8
#define VK_FID_INIT 9
u8 size; /* size of the message in number of vk_msg_blk's */
u16 trans_id; /* transport id, queue & msg_id */
u32 context_id;
#define VK_NEW_CTX 0
u32 cmd;
#define VK_CMD_PLANES_MASK 0x000f /* number of planes to up/download */
#define VK_CMD_UPLOAD 0x0400 /* memory transfer to vk */
#define VK_CMD_DOWNLOAD 0x0500 /* memory transfer from vk */
#define VK_CMD_MASK 0x0f00 /* command mask */
u32 arg;
};
/* vk_msg_blk is 16 bytes fixed */
#define VK_MSGQ_BLK_SIZE (sizeof(struct vk_msg_blk))
/* shift for fast division of basic msg blk size */
#define VK_MSGQ_BLK_SZ_SHIFT 4
/* use msg_id 0 for any simplex host2vk communication */
#define VK_SIMPLEX_MSG_ID 0
/* context per session opening of sysfs */ /* context per session opening of sysfs */
struct bcm_vk_ctx { struct bcm_vk_ctx {
struct list_head node; /* use for linkage in Hash Table */ struct list_head node; /* use for linkage in Hash Table */
...@@ -13,7 +85,11 @@ struct bcm_vk_ctx { ...@@ -13,7 +85,11 @@ struct bcm_vk_ctx {
bool in_use; bool in_use;
pid_t pid; pid_t pid;
u32 hash_idx; u32 hash_idx;
u32 q_num; /* queue number used by the stream */
struct miscdevice *miscdev; struct miscdevice *miscdev;
atomic_t pend_cnt; /* number of items pending to be read from host */
atomic_t dma_cnt; /* any dma transaction outstanding */
wait_queue_head_t rd_wq;
}; };
/* pid hash table entry */ /* pid hash table entry */
...@@ -21,6 +97,55 @@ struct bcm_vk_ht_entry { ...@@ -21,6 +97,55 @@ struct bcm_vk_ht_entry {
struct list_head head; struct list_head head;
}; };
#define VK_DMA_MAX_ADDRS 4 /* Max 4 DMA Addresses */
/* structure for house keeping a single work entry */
struct bcm_vk_wkent {
struct list_head node; /* for linking purpose */
struct bcm_vk_ctx *ctx;
/* Store up to 4 dma pointers */
struct bcm_vk_dma dma[VK_DMA_MAX_ADDRS];
u32 to_h_blks; /* response */
struct vk_msg_blk *to_h_msg;
/*
* put the to_v_msg at the end so that we could simply append to_v msg
* to the end of the allocated block
*/
u32 usr_msg_id;
u32 to_v_blks;
u32 seq_num;
struct vk_msg_blk to_v_msg[0];
};
/* queue stats counters */
struct bcm_vk_qs_cnts {
u32 cnt; /* general counter, used to limit output */
u32 acc_sum;
u32 max_occ; /* max during a sampling period */
u32 max_abs; /* the abs max since reset */
};
/* control channel structure for either to_v or to_h communication */
struct bcm_vk_msg_chan {
u32 q_nr;
/* Mutex to access msgq */
struct mutex msgq_mutex;
/* pointing to BAR locations */
struct bcm_vk_msgq __iomem *msgq[VK_MSGQ_MAX_NR];
/* Spinlock to access pending queue */
spinlock_t pendq_lock;
/* for temporary storing pending items, one for each queue */
struct list_head pendq[VK_MSGQ_MAX_NR];
/* static queue info from the sync */
struct bcm_vk_sync_qinfo sync_qinfo[VK_MSGQ_MAX_NR];
};
/* totol number of message q allowed by the driver */
#define VK_MSGQ_PER_CHAN_MAX 3
#define VK_MSGQ_NUM_DEFAULT (VK_MSGQ_PER_CHAN_MAX - 1)
/* total number of supported ctx, 32 ctx each for 5 components */ /* total number of supported ctx, 32 ctx each for 5 components */
#define VK_CMPT_CTX_MAX (32 * 5) #define VK_CMPT_CTX_MAX (32 * 5)
...@@ -28,4 +153,11 @@ struct bcm_vk_ht_entry { ...@@ -28,4 +153,11 @@ struct bcm_vk_ht_entry {
#define VK_PID_HT_SHIFT_BIT 7 /* 128 */ #define VK_PID_HT_SHIFT_BIT 7 /* 128 */
#define VK_PID_HT_SZ BIT(VK_PID_HT_SHIFT_BIT) #define VK_PID_HT_SZ BIT(VK_PID_HT_SHIFT_BIT)
/* The following are offsets of DDR info provided by the vk card */
#define VK_BAR0_SEG_SIZE (4 * SZ_1K) /* segment size for BAR0 */
/* shutdown types supported */
#define VK_SHUTDOWN_PID 1
#define VK_SHUTDOWN_GRACEFUL 2
#endif #endif
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright 2018-2020 Broadcom.
*/
#include <linux/dma-mapping.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
#include <linux/pgtable.h>
#include <linux/vmalloc.h>
#include <asm/page.h>
#include <asm/unaligned.h>
#include <uapi/linux/misc/bcm_vk.h>
#include "bcm_vk.h"
#include "bcm_vk_msg.h"
#include "bcm_vk_sg.h"
/*
* Valkyrie has a hardware limitation of 16M transfer size.
* So limit the SGL chunks to 16M.
*/
#define BCM_VK_MAX_SGL_CHUNK SZ_16M
static int bcm_vk_dma_alloc(struct device *dev,
struct bcm_vk_dma *dma,
int dir,
struct _vk_data *vkdata);
static int bcm_vk_dma_free(struct device *dev, struct bcm_vk_dma *dma);
/* Uncomment to dump SGLIST */
/* #define BCM_VK_DUMP_SGLIST */
static int bcm_vk_dma_alloc(struct device *dev,
struct bcm_vk_dma *dma,
int direction,
struct _vk_data *vkdata)
{
dma_addr_t addr, sg_addr;
int err;
int i;
int offset;
u32 size;
u32 remaining_size;
u32 transfer_size;
u64 data;
unsigned long first, last;
struct _vk_data *sgdata;
/* Get 64-bit user address */
data = get_unaligned(&vkdata->address);
/* offset into first page */
offset = offset_in_page(data);
/* Calculate number of pages */
first = (data & PAGE_MASK) >> PAGE_SHIFT;
last = ((data + vkdata->size - 1) & PAGE_MASK) >> PAGE_SHIFT;
dma->nr_pages = last - first + 1;
/* Allocate DMA pages */
dma->pages = kmalloc_array(dma->nr_pages,
sizeof(struct page *),
GFP_KERNEL);
if (!dma->pages)
return -ENOMEM;
dev_dbg(dev, "Alloc DMA Pages [0x%llx+0x%x => %d pages]\n",
data, vkdata->size, dma->nr_pages);
dma->direction = direction;
/* Get user pages into memory */
err = get_user_pages_fast(data & PAGE_MASK,
dma->nr_pages,
direction == DMA_FROM_DEVICE,
dma->pages);
if (err != dma->nr_pages) {
dma->nr_pages = (err >= 0) ? err : 0;
dev_err(dev, "get_user_pages_fast, err=%d [%d]\n",
err, dma->nr_pages);
return err < 0 ? err : -EINVAL;
}
/* Max size of sg list is 1 per mapped page + fields at start */
dma->sglen = (dma->nr_pages * sizeof(*sgdata)) +
(sizeof(u32) * SGLIST_VKDATA_START);
/* Allocate sglist */
dma->sglist = dma_alloc_coherent(dev,
dma->sglen,
&dma->handle,
GFP_KERNEL);
if (!dma->sglist)
return -ENOMEM;
dma->sglist[SGLIST_NUM_SG] = 0;
dma->sglist[SGLIST_TOTALSIZE] = vkdata->size;
remaining_size = vkdata->size;
sgdata = (struct _vk_data *)&dma->sglist[SGLIST_VKDATA_START];
/* Map all pages into DMA */
size = min_t(size_t, PAGE_SIZE - offset, remaining_size);
remaining_size -= size;
sg_addr = dma_map_page(dev,
dma->pages[0],
offset,
size,
dma->direction);
transfer_size = size;
if (unlikely(dma_mapping_error(dev, sg_addr))) {
__free_page(dma->pages[0]);
return -EIO;
}
for (i = 1; i < dma->nr_pages; i++) {
size = min_t(size_t, PAGE_SIZE, remaining_size);
remaining_size -= size;
addr = dma_map_page(dev,
dma->pages[i],
0,
size,
dma->direction);
if (unlikely(dma_mapping_error(dev, addr))) {
__free_page(dma->pages[i]);
return -EIO;
}
/*
* Compress SG list entry when pages are contiguous
* and transfer size less or equal to BCM_VK_MAX_SGL_CHUNK
*/
if ((addr == (sg_addr + transfer_size)) &&
((transfer_size + size) <= BCM_VK_MAX_SGL_CHUNK)) {
/* pages are contiguous, add to same sg entry */
transfer_size += size;
} else {
/* pages are not contiguous, write sg entry */
sgdata->size = transfer_size;
put_unaligned(sg_addr, (u64 *)&sgdata->address);
dma->sglist[SGLIST_NUM_SG]++;
/* start new sg entry */
sgdata++;
sg_addr = addr;
transfer_size = size;
}
}
/* Write last sg list entry */
sgdata->size = transfer_size;
put_unaligned(sg_addr, (u64 *)&sgdata->address);
dma->sglist[SGLIST_NUM_SG]++;
/* Update pointers and size field to point to sglist */
put_unaligned((u64)dma->handle, &vkdata->address);
vkdata->size = (dma->sglist[SGLIST_NUM_SG] * sizeof(*sgdata)) +
(sizeof(u32) * SGLIST_VKDATA_START);
#ifdef BCM_VK_DUMP_SGLIST
dev_dbg(dev,
"sgl 0x%llx handle 0x%llx, sglen: 0x%x sgsize: 0x%x\n",
(u64)dma->sglist,
dma->handle,
dma->sglen,
vkdata->size);
for (i = 0; i < vkdata->size / sizeof(u32); i++)
dev_dbg(dev, "i:0x%x 0x%x\n", i, dma->sglist[i]);
#endif
return 0;
}
int bcm_vk_sg_alloc(struct device *dev,
struct bcm_vk_dma *dma,
int dir,
struct _vk_data *vkdata,
int num)
{
int i;
int rc = -EINVAL;
/* Convert user addresses to DMA SG List */
for (i = 0; i < num; i++) {
if (vkdata[i].size && vkdata[i].address) {
/*
* If both size and address are non-zero
* then DMA alloc.
*/
rc = bcm_vk_dma_alloc(dev,
&dma[i],
dir,
&vkdata[i]);
} else if (vkdata[i].size ||
vkdata[i].address) {
/*
* If one of size and address are zero
* there is a problem.
*/
dev_err(dev,
"Invalid vkdata %x 0x%x 0x%llx\n",
i, vkdata[i].size, vkdata[i].address);
rc = -EINVAL;
} else {
/*
* If size and address are both zero
* don't convert, but return success.
*/
rc = 0;
}
if (rc)
goto fail_alloc;
}
return rc;
fail_alloc:
while (i > 0) {
i--;
if (dma[i].sglist)
bcm_vk_dma_free(dev, &dma[i]);
}
return rc;
}
static int bcm_vk_dma_free(struct device *dev, struct bcm_vk_dma *dma)
{
dma_addr_t addr;
int i;
int num_sg;
u32 size;
struct _vk_data *vkdata;
dev_dbg(dev, "free sglist=%p sglen=0x%x\n", dma->sglist, dma->sglen);
/* Unmap all pages in the sglist */
num_sg = dma->sglist[SGLIST_NUM_SG];
vkdata = (struct _vk_data *)&dma->sglist[SGLIST_VKDATA_START];
for (i = 0; i < num_sg; i++) {
size = vkdata[i].size;
addr = get_unaligned(&vkdata[i].address);
dma_unmap_page(dev, addr, size, dma->direction);
}
/* Free allocated sglist */
dma_free_coherent(dev, dma->sglen, dma->sglist, dma->handle);
/* Release lock on all pages */
for (i = 0; i < dma->nr_pages; i++)
put_page(dma->pages[i]);
/* Free allocated dma pages */
kfree(dma->pages);
dma->sglist = NULL;
return 0;
}
int bcm_vk_sg_free(struct device *dev, struct bcm_vk_dma *dma, int num,
int *proc_cnt)
{
int i;
*proc_cnt = 0;
/* Unmap and free all pages and sglists */
for (i = 0; i < num; i++) {
if (dma[i].sglist) {
bcm_vk_dma_free(dev, &dma[i]);
*proc_cnt += 1;
}
}
return 0;
}
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright 2018-2020 Broadcom.
*/
#ifndef BCM_VK_SG_H
#define BCM_VK_SG_H
#include <linux/dma-mapping.h>
struct bcm_vk_dma {
/* for userland buffer */
struct page **pages;
int nr_pages;
/* common */
dma_addr_t handle;
/*
* sglist is of the following LE format
* [U32] num_sg = number of sg addresses (N)
* [U32] totalsize = totalsize of data being transferred in sglist
* [U32] size[0] = size of data in address0
* [U32] addr_l[0] = lower 32-bits of address0
* [U32] addr_h[0] = higher 32-bits of address0
* ..
* [U32] size[N-1] = size of data in addressN-1
* [U32] addr_l[N-1] = lower 32-bits of addressN-1
* [U32] addr_h[N-1] = higher 32-bits of addressN-1
*/
u32 *sglist;
#define SGLIST_NUM_SG 0
#define SGLIST_TOTALSIZE 1
#define SGLIST_VKDATA_START 2
int sglen; /* Length (bytes) of sglist */
int direction;
};
struct _vk_data {
u32 size; /* data size in bytes */
u64 address; /* Pointer to data */
} __packed;
/*
* Scatter-gather DMA buffer API.
*
* These functions provide a simple way to create a page list and a
* scatter-gather list from userspace address and map the memory
* for DMA operation.
*/
int bcm_vk_sg_alloc(struct device *dev,
struct bcm_vk_dma *dma,
int dir,
struct _vk_data *vkdata,
int num);
int bcm_vk_sg_free(struct device *dev, struct bcm_vk_dma *dma, int num,
int *proc_cnt);
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment