0001
0002
0003
0004
0005
0006 #ifndef BCM_VK_H
0007 #define BCM_VK_H
0008
0009 #include <linux/atomic.h>
0010 #include <linux/firmware.h>
0011 #include <linux/irq.h>
0012 #include <linux/kref.h>
0013 #include <linux/miscdevice.h>
0014 #include <linux/mutex.h>
0015 #include <linux/pci.h>
0016 #include <linux/poll.h>
0017 #include <linux/sched/signal.h>
0018 #include <linux/tty.h>
0019 #include <linux/uaccess.h>
0020 #include <uapi/linux/misc/bcm_vk.h>
0021
0022 #include "bcm_vk_msg.h"
0023
0024 #define DRV_MODULE_NAME "bcm-vk"
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 #define BAR_CODEPUSH_SBL 0x400
0044
0045 #define CODEPUSH_BOOT1_ENTRY 0x00400000
0046 #define CODEPUSH_MASK 0xfffff000
0047 #define CODEPUSH_BOOTSTART BIT(0)
0048
0049
0050 #define BAR_BOOT_STATUS 0x404
0051
0052 #define SRAM_OPEN BIT(16)
0053 #define DDR_OPEN BIT(17)
0054
0055
0056 #define FW_LOADER_ACK_SEND_MORE_DATA BIT(18)
0057 #define FW_LOADER_ACK_IN_PROGRESS BIT(19)
0058 #define FW_LOADER_ACK_RCVD_ALL_DATA BIT(20)
0059
0060
0061 #define BOOT_STDALONE_RUNNING BIT(21)
0062
0063
0064 #define BOOT_STATE_MASK (0xffffffff & \
0065 ~(FW_LOADER_ACK_SEND_MORE_DATA | \
0066 FW_LOADER_ACK_IN_PROGRESS | \
0067 BOOT_STDALONE_RUNNING))
0068
0069 #define BOOT_ERR_SHIFT 4
0070 #define BOOT_ERR_MASK (0xf << BOOT_ERR_SHIFT)
0071 #define BOOT_PROG_MASK 0xf
0072
0073 #define BROM_STATUS_NOT_RUN 0x2
0074 #define BROM_NOT_RUN (SRAM_OPEN | BROM_STATUS_NOT_RUN)
0075 #define BROM_STATUS_COMPLETE 0x6
0076 #define BROM_RUNNING (SRAM_OPEN | BROM_STATUS_COMPLETE)
0077 #define BOOT1_STATUS_COMPLETE 0x6
0078 #define BOOT1_RUNNING (DDR_OPEN | BOOT1_STATUS_COMPLETE)
0079 #define BOOT2_STATUS_COMPLETE 0x6
0080 #define BOOT2_RUNNING (FW_LOADER_ACK_RCVD_ALL_DATA | \
0081 BOOT2_STATUS_COMPLETE)
0082
0083
0084 #define BAR_CODEPUSH_SBI 0x408
0085
0086 #define CODEPUSH_BOOT2_ENTRY 0x60000000
0087
0088 #define BAR_CARD_STATUS 0x410
0089
0090 #define CARD_STATUS_TTYVK0_READY BIT(0)
0091 #define CARD_STATUS_TTYVK1_READY BIT(1)
0092
0093 #define BAR_BOOT1_STDALONE_PROGRESS 0x420
0094 #define BOOT1_STDALONE_SUCCESS (BIT(13) | BIT(14))
0095 #define BOOT1_STDALONE_PROGRESS_MASK BOOT1_STDALONE_SUCCESS
0096
0097 #define BAR_METADATA_VERSION 0x440
0098 #define BAR_OS_UPTIME 0x444
0099 #define BAR_CHIP_ID 0x448
0100 #define MAJOR_SOC_REV(_chip_id) (((_chip_id) >> 20) & 0xf)
0101
0102 #define BAR_CARD_TEMPERATURE 0x45c
0103
0104 #define BCM_VK_TEMP_FIELD_MASK 0xff
0105 #define BCM_VK_CPU_TEMP_SHIFT 0
0106 #define BCM_VK_DDR0_TEMP_SHIFT 8
0107 #define BCM_VK_DDR1_TEMP_SHIFT 16
0108
0109 #define BAR_CARD_VOLTAGE 0x460
0110
0111 #define BCM_VK_VOLT_RAIL_MASK 0xffff
0112 #define BCM_VK_3P3_VOLT_REG_SHIFT 16
0113
0114 #define BAR_CARD_ERR_LOG 0x464
0115
0116 #define ERR_LOG_UECC BIT(0)
0117 #define ERR_LOG_SSIM_BUSY BIT(1)
0118 #define ERR_LOG_AFBC_BUSY BIT(2)
0119 #define ERR_LOG_HIGH_TEMP_ERR BIT(3)
0120 #define ERR_LOG_WDOG_TIMEOUT BIT(4)
0121 #define ERR_LOG_SYS_FAULT BIT(5)
0122 #define ERR_LOG_RAMDUMP BIT(6)
0123 #define ERR_LOG_COP_WDOG_TIMEOUT BIT(7)
0124
0125 #define ERR_LOG_MEM_ALLOC_FAIL BIT(8)
0126 #define ERR_LOG_LOW_TEMP_WARN BIT(9)
0127 #define ERR_LOG_ECC BIT(10)
0128 #define ERR_LOG_IPC_DWN BIT(11)
0129
0130
0131 #define ERR_LOG_HOST_INTF_V_FAIL BIT(13)
0132 #define ERR_LOG_HOST_HB_FAIL BIT(14)
0133 #define ERR_LOG_HOST_PCIE_DWN BIT(15)
0134
0135 #define BAR_CARD_ERR_MEM 0x468
0136
0137 #define BCM_VK_MEM_ERR_FIELD_MASK 0xff
0138 #define BCM_VK_ECC_MEM_ERR_SHIFT 0
0139 #define BCM_VK_UECC_MEM_ERR_SHIFT 8
0140
0141 #define BCM_VK_ECC_THRESHOLD 10
0142 #define BCM_VK_UECC_THRESHOLD 1
0143
0144 #define BAR_CARD_PWR_AND_THRE 0x46c
0145
0146 #define BCM_VK_PWR_AND_THRE_FIELD_MASK 0xff
0147 #define BCM_VK_LOW_TEMP_THRE_SHIFT 0
0148 #define BCM_VK_HIGH_TEMP_THRE_SHIFT 8
0149 #define BCM_VK_PWR_STATE_SHIFT 16
0150
0151 #define BAR_CARD_STATIC_INFO 0x470
0152
0153 #define BAR_INTF_VER 0x47c
0154 #define BAR_INTF_VER_MAJOR_SHIFT 16
0155 #define BAR_INTF_VER_MASK 0xffff
0156
0157
0158
0159
0160 #define SEMANTIC_MAJOR 1
0161 #define SEMANTIC_MINOR 0
0162
0163
0164
0165
0166
0167 #define VK_BAR0_REGSEG_DB_BASE 0x484
0168 #define VK_BAR0_REGSEG_DB_REG_GAP 8
0169
0170
0171
0172
0173
0174 #define VK_BAR0_RESET_DB_NUM 3
0175 #define VK_BAR0_RESET_DB_SOFT 0xffffffff
0176 #define VK_BAR0_RESET_DB_HARD 0xfffffffd
0177 #define VK_BAR0_RESET_RAMPDUMP 0xa0000000
0178
0179 #define VK_BAR0_Q_DB_BASE(q_num) (VK_BAR0_REGSEG_DB_BASE + \
0180 ((q_num) * VK_BAR0_REGSEG_DB_REG_GAP))
0181 #define VK_BAR0_RESET_DB_BASE (VK_BAR0_REGSEG_DB_BASE + \
0182 (VK_BAR0_RESET_DB_NUM * VK_BAR0_REGSEG_DB_REG_GAP))
0183
0184 #define BAR_BOOTSRC_SELECT 0xc78
0185
0186 #define BOOTSRC_SOFT_ENABLE BIT(14)
0187
0188
0189 #define BAR_FIRMWARE_TAG_SIZE 50
0190 #define FIRMWARE_STATUS_PRE_INIT_DONE 0x1f
0191
0192
0193 #define VK_MSG_ID_BITMAP_SIZE 4096
0194 #define VK_MSG_ID_BITMAP_MASK (VK_MSG_ID_BITMAP_SIZE - 1)
0195 #define VK_MSG_ID_OVERFLOW 0xffff
0196
0197
0198
0199
0200
0201
0202
0203
0204 #define VK_BAR1_MSGQ_DEF_RDY 0x60c0
0205
0206 #define VK_BAR1_MSGQ_RDY_MARKER 0xbeefcafe
0207
0208 #define VK_BAR1_DIAG_RDY_MARKER 0xdeadcafe
0209
0210 #define VK_BAR1_MSGQ_NR 0x60c4
0211
0212 #define VK_BAR1_MSGQ_CTRL_OFF 0x60c8
0213
0214
0215 #define VK_BAR1_UCODE_VER_TAG 0x6170
0216 #define VK_BAR1_BOOT1_VER_TAG 0x61b0
0217 #define VK_BAR1_VER_TAG_SIZE 64
0218
0219
0220 #define VK_BAR1_DMA_BUF_OFF_HI 0x61e0
0221 #define VK_BAR1_DMA_BUF_OFF_LO (VK_BAR1_DMA_BUF_OFF_HI + 4)
0222 #define VK_BAR1_DMA_BUF_SZ (VK_BAR1_DMA_BUF_OFF_HI + 8)
0223
0224
0225 #define VK_BAR1_SCRATCH_OFF_HI 0x61f0
0226 #define VK_BAR1_SCRATCH_OFF_LO (VK_BAR1_SCRATCH_OFF_HI + 4)
0227 #define VK_BAR1_SCRATCH_SZ_ADDR (VK_BAR1_SCRATCH_OFF_HI + 8)
0228 #define VK_BAR1_SCRATCH_DEF_NR_PAGES 32
0229
0230
0231 #define VK_BAR1_DAUTH_BASE_ADDR 0x6200
0232 #define VK_BAR1_DAUTH_STORE_SIZE 0x48
0233 #define VK_BAR1_DAUTH_VALID_SIZE 0x8
0234 #define VK_BAR1_DAUTH_MAX 4
0235 #define VK_BAR1_DAUTH_STORE_ADDR(x) \
0236 (VK_BAR1_DAUTH_BASE_ADDR + \
0237 (x) * (VK_BAR1_DAUTH_STORE_SIZE + VK_BAR1_DAUTH_VALID_SIZE))
0238 #define VK_BAR1_DAUTH_VALID_ADDR(x) \
0239 (VK_BAR1_DAUTH_STORE_ADDR(x) + VK_BAR1_DAUTH_STORE_SIZE)
0240
0241
0242 #define VK_BAR1_SOTP_REVID_BASE_ADDR 0x6340
0243 #define VK_BAR1_SOTP_REVID_SIZE 0x10
0244 #define VK_BAR1_SOTP_REVID_MAX 2
0245 #define VK_BAR1_SOTP_REVID_ADDR(x) \
0246 (VK_BAR1_SOTP_REVID_BASE_ADDR + (x) * VK_BAR1_SOTP_REVID_SIZE)
0247
0248
0249 #define MAX_BAR 3
0250
0251
0252 #define BCM_VK_DEF_IB_SGL_BLK_LEN 16
0253 #define BCM_VK_IB_SGL_BLK_MAX 24
0254
0255 enum pci_barno {
0256 BAR_0 = 0,
0257 BAR_1,
0258 BAR_2
0259 };
0260
0261 #ifdef CONFIG_BCM_VK_TTY
0262 #define BCM_VK_NUM_TTY 2
0263 #else
0264 #define BCM_VK_NUM_TTY 0
0265 #endif
0266
0267 struct bcm_vk_tty {
0268 struct tty_port port;
0269 u32 to_offset;
0270 u32 to_size;
0271 u32 wr;
0272 u32 from_offset;
0273 u32 from_size;
0274 u32 rd;
0275 pid_t pid;
0276 bool irq_enabled;
0277 bool is_opened;
0278 };
0279
0280
0281 #define MAX_OPP 3
0282 #define MAX_CARD_INFO_TAG_SIZE 64
0283
0284 struct bcm_vk_card_info {
0285 u32 version;
0286 char os_tag[MAX_CARD_INFO_TAG_SIZE];
0287 char cmpt_tag[MAX_CARD_INFO_TAG_SIZE];
0288 u32 cpu_freq_mhz;
0289 u32 cpu_scale[MAX_OPP];
0290 u32 ddr_freq_mhz;
0291 u32 ddr_size_MB;
0292 u32 video_core_freq_mhz;
0293 };
0294
0295
0296 struct bcm_vk_dauth_key {
0297 char store[VK_BAR1_DAUTH_STORE_SIZE];
0298 char valid[VK_BAR1_DAUTH_VALID_SIZE];
0299 };
0300
0301 struct bcm_vk_dauth_info {
0302 struct bcm_vk_dauth_key keys[VK_BAR1_DAUTH_MAX];
0303 };
0304
0305
0306
0307
0308
0309 struct bcm_vk_peer_log {
0310 u32 rd_idx;
0311 u32 wr_idx;
0312 u32 buf_size;
0313 u32 mask;
0314 char data[];
0315 };
0316
0317
0318 #define BCM_VK_PEER_LOG_BUF_MAX SZ_16K
0319
0320 #define BCM_VK_PEER_LOG_LINE_MAX 256
0321
0322
0323
0324
0325 #define BCM_VK_PROC_TYPE_TAG_LEN 8
0326 struct bcm_vk_proc_mon_entry_t {
0327 char tag[BCM_VK_PROC_TYPE_TAG_LEN];
0328 u32 used;
0329 u32 max;
0330 };
0331
0332
0333
0334
0335 #define BCM_VK_PROC_MON_MAX 8
0336 struct bcm_vk_proc_mon_info {
0337 u32 num;
0338 u32 entry_size;
0339 struct bcm_vk_proc_mon_entry_t entries[BCM_VK_PROC_MON_MAX];
0340 };
0341
0342 struct bcm_vk_hb_ctrl {
0343 struct timer_list timer;
0344 u32 last_uptime;
0345 u32 lost_cnt;
0346 };
0347
0348 struct bcm_vk_alert {
0349 u16 flags;
0350 u16 notfs;
0351 };
0352
0353
0354 struct bcm_vk_alert_cnts {
0355 u16 ecc;
0356 u16 uecc;
0357 };
0358
0359 struct bcm_vk {
0360 struct pci_dev *pdev;
0361 void __iomem *bar[MAX_BAR];
0362 int num_irqs;
0363
0364 struct bcm_vk_card_info card_info;
0365 struct bcm_vk_proc_mon_info proc_mon_info;
0366 struct bcm_vk_dauth_info dauth_info;
0367
0368
0369 struct mutex mutex;
0370 struct miscdevice miscdev;
0371 int devid;
0372
0373 #ifdef CONFIG_BCM_VK_TTY
0374 struct tty_driver *tty_drv;
0375 struct timer_list serial_timer;
0376 struct bcm_vk_tty tty[BCM_VK_NUM_TTY];
0377 struct workqueue_struct *tty_wq_thread;
0378 struct work_struct tty_wq_work;
0379 #endif
0380
0381
0382 struct kref kref;
0383
0384 spinlock_t msg_id_lock;
0385 u16 msg_id;
0386 DECLARE_BITMAP(bmap, VK_MSG_ID_BITMAP_SIZE);
0387 spinlock_t ctx_lock;
0388 struct bcm_vk_ctx ctx[VK_CMPT_CTX_MAX];
0389 struct bcm_vk_ht_entry pid_ht[VK_PID_HT_SZ];
0390 pid_t reset_pid;
0391
0392 atomic_t msgq_inited;
0393 struct bcm_vk_msg_chan to_v_msg_chan;
0394 struct bcm_vk_msg_chan to_h_msg_chan;
0395
0396 struct workqueue_struct *wq_thread;
0397 struct work_struct wq_work;
0398 unsigned long wq_offload[1];
0399 void *tdma_vaddr;
0400 dma_addr_t tdma_addr;
0401
0402 struct notifier_block panic_nb;
0403 u32 ib_sgl_size;
0404
0405
0406 struct bcm_vk_hb_ctrl hb_ctrl;
0407
0408 spinlock_t host_alert_lock;
0409 struct bcm_vk_alert host_alert;
0410 struct bcm_vk_alert peer_alert;
0411 struct bcm_vk_alert_cnts alert_cnts;
0412
0413
0414 u32 peerlog_off;
0415 struct bcm_vk_peer_log peerlog_info;
0416
0417 u32 proc_mon_off;
0418 };
0419
0420
0421 enum bcm_vk_wq_offload_flags {
0422 BCM_VK_WQ_DWNLD_PEND = 0,
0423 BCM_VK_WQ_DWNLD_AUTO = 1,
0424 BCM_VK_WQ_NOTF_PEND = 2,
0425 };
0426
0427
0428 #define BCM_VK_EXTRACT_FIELD(_field, _reg, _mask, _shift) \
0429 (_field = (((_reg) >> (_shift)) & (_mask)))
0430
0431 struct bcm_vk_entry {
0432 const u32 mask;
0433 const u32 exp_val;
0434 const char *str;
0435 };
0436
0437
0438 #define BCM_VK_PEER_ERR_NUM 12
0439 extern struct bcm_vk_entry const bcm_vk_peer_err[BCM_VK_PEER_ERR_NUM];
0440
0441 #define BCM_VK_HOST_ERR_NUM 3
0442 extern struct bcm_vk_entry const bcm_vk_host_err[BCM_VK_HOST_ERR_NUM];
0443
0444
0445
0446
0447
0448 #define BCM_VK_INTF_IS_DOWN(val) ((val) == 0xffffffff)
0449
0450 static inline u32 vkread32(struct bcm_vk *vk, enum pci_barno bar, u64 offset)
0451 {
0452 return readl(vk->bar[bar] + offset);
0453 }
0454
0455 static inline void vkwrite32(struct bcm_vk *vk,
0456 u32 value,
0457 enum pci_barno bar,
0458 u64 offset)
0459 {
0460 writel(value, vk->bar[bar] + offset);
0461 }
0462
0463 static inline u8 vkread8(struct bcm_vk *vk, enum pci_barno bar, u64 offset)
0464 {
0465 return readb(vk->bar[bar] + offset);
0466 }
0467
0468 static inline void vkwrite8(struct bcm_vk *vk,
0469 u8 value,
0470 enum pci_barno bar,
0471 u64 offset)
0472 {
0473 writeb(value, vk->bar[bar] + offset);
0474 }
0475
0476 static inline bool bcm_vk_msgq_marker_valid(struct bcm_vk *vk)
0477 {
0478 u32 rdy_marker = 0;
0479 u32 fw_status;
0480
0481 fw_status = vkread32(vk, BAR_0, VK_BAR_FWSTS);
0482
0483 if ((fw_status & VK_FWSTS_READY) == VK_FWSTS_READY)
0484 rdy_marker = vkread32(vk, BAR_1, VK_BAR1_MSGQ_DEF_RDY);
0485
0486 return (rdy_marker == VK_BAR1_MSGQ_RDY_MARKER);
0487 }
0488
0489 int bcm_vk_open(struct inode *inode, struct file *p_file);
0490 ssize_t bcm_vk_read(struct file *p_file, char __user *buf, size_t count,
0491 loff_t *f_pos);
0492 ssize_t bcm_vk_write(struct file *p_file, const char __user *buf,
0493 size_t count, loff_t *f_pos);
0494 __poll_t bcm_vk_poll(struct file *p_file, struct poll_table_struct *wait);
0495 int bcm_vk_release(struct inode *inode, struct file *p_file);
0496 void bcm_vk_release_data(struct kref *kref);
0497 irqreturn_t bcm_vk_msgq_irqhandler(int irq, void *dev_id);
0498 irqreturn_t bcm_vk_notf_irqhandler(int irq, void *dev_id);
0499 irqreturn_t bcm_vk_tty_irqhandler(int irq, void *dev_id);
0500 int bcm_vk_msg_init(struct bcm_vk *vk);
0501 void bcm_vk_msg_remove(struct bcm_vk *vk);
0502 void bcm_vk_drain_msg_on_reset(struct bcm_vk *vk);
0503 int bcm_vk_sync_msgq(struct bcm_vk *vk, bool force_sync);
0504 void bcm_vk_blk_drv_access(struct bcm_vk *vk);
0505 s32 bcm_to_h_msg_dequeue(struct bcm_vk *vk);
0506 int bcm_vk_send_shutdown_msg(struct bcm_vk *vk, u32 shut_type,
0507 const pid_t pid, const u32 q_num);
0508 void bcm_to_v_q_doorbell(struct bcm_vk *vk, u32 q_num, u32 db_val);
0509 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
0510 void bcm_vk_hb_init(struct bcm_vk *vk);
0511 void bcm_vk_hb_deinit(struct bcm_vk *vk);
0512 void bcm_vk_handle_notf(struct bcm_vk *vk);
0513 bool bcm_vk_drv_access_ok(struct bcm_vk *vk);
0514 void bcm_vk_set_host_alert(struct bcm_vk *vk, u32 bit_mask);
0515
0516 #ifdef CONFIG_BCM_VK_TTY
0517 int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
0518 void bcm_vk_tty_exit(struct bcm_vk *vk);
0519 void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
0520 void bcm_vk_tty_wq_exit(struct bcm_vk *vk);
0521
0522 static inline void bcm_vk_tty_set_irq_enabled(struct bcm_vk *vk, int index)
0523 {
0524 vk->tty[index].irq_enabled = true;
0525 }
0526 #else
0527 static inline int bcm_vk_tty_init(struct bcm_vk *vk, char *name)
0528 {
0529 return 0;
0530 }
0531
0532 static inline void bcm_vk_tty_exit(struct bcm_vk *vk)
0533 {
0534 }
0535
0536 static inline void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk)
0537 {
0538 }
0539
0540 static inline void bcm_vk_tty_wq_exit(struct bcm_vk *vk)
0541 {
0542 }
0543
0544 static inline void bcm_vk_tty_set_irq_enabled(struct bcm_vk *vk, int index)
0545 {
0546 }
0547 #endif
0548
0549 #endif