1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright 2018-2020 Broadcom. 4 */ 5 6 #include <linux/delay.h> 7 #include <linux/dma-mapping.h> 8 #include <linux/firmware.h> 9 #include <linux/fs.h> 10 #include <linux/idr.h> 11 #include <linux/interrupt.h> 12 #include <linux/kref.h> 13 #include <linux/module.h> 14 #include <linux/mutex.h> 15 #include <linux/pci.h> 16 #include <linux/pci_regs.h> 17 #include <uapi/linux/misc/bcm_vk.h> 18 19 #include "bcm_vk.h" 20 21 #define PCI_DEVICE_ID_VALKYRIE 0x5e87 22 #define PCI_DEVICE_ID_VIPER 0x5e88 23 24 static DEFINE_IDA(bcm_vk_ida); 25 26 enum soc_idx { 27 VALKYRIE_A0 = 0, 28 VALKYRIE_B0, 29 VIPER, 30 VK_IDX_INVALID 31 }; 32 33 enum img_idx { 34 IMG_PRI = 0, 35 IMG_SEC, 36 IMG_PER_TYPE_MAX 37 }; 38 39 struct load_image_entry { 40 const u32 image_type; 41 const char *image_name[IMG_PER_TYPE_MAX]; 42 }; 43 44 #define NUM_BOOT_STAGES 2 45 /* default firmware images names */ 46 static const struct load_image_entry image_tab[][NUM_BOOT_STAGES] = { 47 [VALKYRIE_A0] = { 48 {VK_IMAGE_TYPE_BOOT1, {"vk_a0-boot1.bin", "vk-boot1.bin"}}, 49 {VK_IMAGE_TYPE_BOOT2, {"vk_a0-boot2.bin", "vk-boot2.bin"}} 50 }, 51 [VALKYRIE_B0] = { 52 {VK_IMAGE_TYPE_BOOT1, {"vk_b0-boot1.bin", "vk-boot1.bin"}}, 53 {VK_IMAGE_TYPE_BOOT2, {"vk_b0-boot2.bin", "vk-boot2.bin"}} 54 }, 55 56 [VIPER] = { 57 {VK_IMAGE_TYPE_BOOT1, {"vp-boot1.bin", ""}}, 58 {VK_IMAGE_TYPE_BOOT2, {"vp-boot2.bin", ""}} 59 }, 60 }; 61 62 /* Location of memory base addresses of interest in BAR1 */ 63 /* Load Boot1 to start of ITCM */ 64 #define BAR1_CODEPUSH_BASE_BOOT1 0x100000 65 66 /* Allow minimum 1s for Load Image timeout responses */ 67 #define LOAD_IMAGE_TIMEOUT_MS (1 * MSEC_PER_SEC) 68 69 /* Image startup timeouts */ 70 #define BOOT1_STARTUP_TIMEOUT_MS (5 * MSEC_PER_SEC) 71 #define BOOT2_STARTUP_TIMEOUT_MS (10 * MSEC_PER_SEC) 72 73 /* 1ms wait for checking the transfer complete status */ 74 #define TXFR_COMPLETE_TIMEOUT_MS 1 75 76 /* MSIX usages */ 77 #define VK_MSIX_MSGQ_MAX 3 78 #define VK_MSIX_NOTF_MAX 1 79 #define VK_MSIX_TTY_MAX BCM_VK_NUM_TTY 80 #define VK_MSIX_IRQ_MAX (VK_MSIX_MSGQ_MAX + VK_MSIX_NOTF_MAX + \ 81 VK_MSIX_TTY_MAX) 82 #define VK_MSIX_IRQ_MIN_REQ (VK_MSIX_MSGQ_MAX + VK_MSIX_NOTF_MAX) 83 84 /* Number of bits set in DMA mask*/ 85 #define BCM_VK_DMA_BITS 64 86 87 /* Ucode boot wait time */ 88 #define BCM_VK_UCODE_BOOT_US (100 * USEC_PER_MSEC) 89 /* 50% margin */ 90 #define BCM_VK_UCODE_BOOT_MAX_US ((BCM_VK_UCODE_BOOT_US * 3) >> 1) 91 92 /* deinit time for the card os after receiving doorbell */ 93 #define BCM_VK_DEINIT_TIME_MS (2 * MSEC_PER_SEC) 94 95 /* 96 * module parameters 97 */ 98 static bool auto_load = true; 99 module_param(auto_load, bool, 0444); 100 MODULE_PARM_DESC(auto_load, 101 "Load images automatically at PCIe probe time.\n"); 102 static uint nr_scratch_pages = VK_BAR1_SCRATCH_DEF_NR_PAGES; 103 module_param(nr_scratch_pages, uint, 0444); 104 MODULE_PARM_DESC(nr_scratch_pages, 105 "Number of pre allocated DMAable coherent pages.\n"); 106 static uint nr_ib_sgl_blk = BCM_VK_DEF_IB_SGL_BLK_LEN; 107 module_param(nr_ib_sgl_blk, uint, 0444); 108 MODULE_PARM_DESC(nr_ib_sgl_blk, 109 "Number of in-band msg blks for short SGL.\n"); 110 111 /* 112 * alerts that could be generated from peer 113 */ 114 const struct bcm_vk_entry bcm_vk_peer_err[BCM_VK_PEER_ERR_NUM] = { 115 {ERR_LOG_UECC, ERR_LOG_UECC, "uecc"}, 116 {ERR_LOG_SSIM_BUSY, ERR_LOG_SSIM_BUSY, "ssim_busy"}, 117 {ERR_LOG_AFBC_BUSY, ERR_LOG_AFBC_BUSY, "afbc_busy"}, 118 {ERR_LOG_HIGH_TEMP_ERR, ERR_LOG_HIGH_TEMP_ERR, "high_temp"}, 119 {ERR_LOG_WDOG_TIMEOUT, ERR_LOG_WDOG_TIMEOUT, "wdog_timeout"}, 120 {ERR_LOG_SYS_FAULT, ERR_LOG_SYS_FAULT, "sys_fault"}, 121 {ERR_LOG_RAMDUMP, ERR_LOG_RAMDUMP, "ramdump"}, 122 {ERR_LOG_COP_WDOG_TIMEOUT, ERR_LOG_COP_WDOG_TIMEOUT, 123 "cop_wdog_timeout"}, 124 {ERR_LOG_MEM_ALLOC_FAIL, ERR_LOG_MEM_ALLOC_FAIL, "malloc_fail warn"}, 125 {ERR_LOG_LOW_TEMP_WARN, ERR_LOG_LOW_TEMP_WARN, "low_temp warn"}, 126 {ERR_LOG_ECC, ERR_LOG_ECC, "ecc"}, 127 {ERR_LOG_IPC_DWN, ERR_LOG_IPC_DWN, "ipc_down"}, 128 }; 129 130 /* alerts detected by the host */ 131 const struct bcm_vk_entry bcm_vk_host_err[BCM_VK_HOST_ERR_NUM] = { 132 {ERR_LOG_HOST_PCIE_DWN, ERR_LOG_HOST_PCIE_DWN, "PCIe_down"}, 133 {ERR_LOG_HOST_HB_FAIL, ERR_LOG_HOST_HB_FAIL, "hb_fail"}, 134 {ERR_LOG_HOST_INTF_V_FAIL, ERR_LOG_HOST_INTF_V_FAIL, "intf_ver_fail"}, 135 }; 136 137 irqreturn_t bcm_vk_notf_irqhandler(int irq, void *dev_id) 138 { 139 struct bcm_vk *vk = dev_id; 140 141 if (!bcm_vk_drv_access_ok(vk)) { 142 dev_err(&vk->pdev->dev, 143 "Interrupt %d received when msgq not inited\n", irq); 144 goto skip_schedule_work; 145 } 146 147 /* if notification is not pending, set bit and schedule work */ 148 if (test_and_set_bit(BCM_VK_WQ_NOTF_PEND, vk->wq_offload) == 0) 149 queue_work(vk->wq_thread, &vk->wq_work); 150 151 skip_schedule_work: 152 return IRQ_HANDLED; 153 } 154 155 static int bcm_vk_intf_ver_chk(struct bcm_vk *vk) 156 { 157 struct device *dev = &vk->pdev->dev; 158 u32 reg; 159 u16 major, minor; 160 int ret = 0; 161 162 /* read interface register */ 163 reg = vkread32(vk, BAR_0, BAR_INTF_VER); 164 major = (reg >> BAR_INTF_VER_MAJOR_SHIFT) & BAR_INTF_VER_MASK; 165 minor = reg & BAR_INTF_VER_MASK; 166 167 /* 168 * if major number is 0, it is pre-release and it would be allowed 169 * to continue, else, check versions accordingly 170 */ 171 if (!major) { 172 dev_warn(dev, "Pre-release major.minor=%d.%d - drv %d.%d\n", 173 major, minor, SEMANTIC_MAJOR, SEMANTIC_MINOR); 174 } else if (major != SEMANTIC_MAJOR) { 175 dev_err(dev, 176 "Intf major.minor=%d.%d rejected - drv %d.%d\n", 177 major, minor, SEMANTIC_MAJOR, SEMANTIC_MINOR); 178 bcm_vk_set_host_alert(vk, ERR_LOG_HOST_INTF_V_FAIL); 179 ret = -EPFNOSUPPORT; 180 } else { 181 dev_dbg(dev, 182 "Intf major.minor=%d.%d passed - drv %d.%d\n", 183 major, minor, SEMANTIC_MAJOR, SEMANTIC_MINOR); 184 } 185 return ret; 186 } 187 188 static void bcm_vk_log_notf(struct bcm_vk *vk, 189 struct bcm_vk_alert *alert, 190 struct bcm_vk_entry const *entry_tab, 191 const u32 table_size) 192 { 193 u32 i; 194 u32 masked_val, latched_val; 195 struct bcm_vk_entry const *entry; 196 u32 reg; 197 u16 ecc_mem_err, uecc_mem_err; 198 struct device *dev = &vk->pdev->dev; 199 200 for (i = 0; i < table_size; i++) { 201 entry = &entry_tab[i]; 202 masked_val = entry->mask & alert->notfs; 203 latched_val = entry->mask & alert->flags; 204 205 if (masked_val == ERR_LOG_UECC) { 206 /* 207 * if there is difference between stored cnt and it 208 * is greater than threshold, log it. 209 */ 210 reg = vkread32(vk, BAR_0, BAR_CARD_ERR_MEM); 211 BCM_VK_EXTRACT_FIELD(uecc_mem_err, reg, 212 BCM_VK_MEM_ERR_FIELD_MASK, 213 BCM_VK_UECC_MEM_ERR_SHIFT); 214 if ((uecc_mem_err != vk->alert_cnts.uecc) && 215 (uecc_mem_err >= BCM_VK_UECC_THRESHOLD)) 216 dev_info(dev, 217 "ALERT! %s.%d uecc RAISED - ErrCnt %d\n", 218 DRV_MODULE_NAME, vk->devid, 219 uecc_mem_err); 220 vk->alert_cnts.uecc = uecc_mem_err; 221 } else if (masked_val == ERR_LOG_ECC) { 222 reg = vkread32(vk, BAR_0, BAR_CARD_ERR_MEM); 223 BCM_VK_EXTRACT_FIELD(ecc_mem_err, reg, 224 BCM_VK_MEM_ERR_FIELD_MASK, 225 BCM_VK_ECC_MEM_ERR_SHIFT); 226 if ((ecc_mem_err != vk->alert_cnts.ecc) && 227 (ecc_mem_err >= BCM_VK_ECC_THRESHOLD)) 228 dev_info(dev, "ALERT! %s.%d ecc RAISED - ErrCnt %d\n", 229 DRV_MODULE_NAME, vk->devid, 230 ecc_mem_err); 231 vk->alert_cnts.ecc = ecc_mem_err; 232 } else if (masked_val != latched_val) { 233 /* print a log as info */ 234 dev_info(dev, "ALERT! %s.%d %s %s\n", 235 DRV_MODULE_NAME, vk->devid, entry->str, 236 masked_val ? "RAISED" : "CLEARED"); 237 } 238 } 239 } 240 241 static void bcm_vk_dump_peer_log(struct bcm_vk *vk) 242 { 243 struct bcm_vk_peer_log log; 244 struct bcm_vk_peer_log *log_info = &vk->peerlog_info; 245 char loc_buf[BCM_VK_PEER_LOG_LINE_MAX]; 246 int cnt; 247 struct device *dev = &vk->pdev->dev; 248 unsigned int data_offset; 249 250 memcpy_fromio(&log, vk->bar[BAR_2] + vk->peerlog_off, sizeof(log)); 251 252 dev_dbg(dev, "Peer PANIC: Size 0x%x(0x%x), [Rd Wr] = [%d %d]\n", 253 log.buf_size, log.mask, log.rd_idx, log.wr_idx); 254 255 if (!log_info->buf_size) { 256 dev_err(dev, "Peer log dump disabled - skipped!\n"); 257 return; 258 } 259 260 /* perform range checking for rd/wr idx */ 261 if ((log.rd_idx > log_info->mask) || 262 (log.wr_idx > log_info->mask) || 263 (log.buf_size != log_info->buf_size) || 264 (log.mask != log_info->mask)) { 265 dev_err(dev, 266 "Corrupted Ptrs: Size 0x%x(0x%x) Mask 0x%x(0x%x) [Rd Wr] = [%d %d], skip log dump.\n", 267 log_info->buf_size, log.buf_size, 268 log_info->mask, log.mask, 269 log.rd_idx, log.wr_idx); 270 return; 271 } 272 273 cnt = 0; 274 data_offset = vk->peerlog_off + sizeof(struct bcm_vk_peer_log); 275 loc_buf[BCM_VK_PEER_LOG_LINE_MAX - 1] = '\0'; 276 while (log.rd_idx != log.wr_idx) { 277 loc_buf[cnt] = vkread8(vk, BAR_2, data_offset + log.rd_idx); 278 279 if ((loc_buf[cnt] == '\0') || 280 (cnt == (BCM_VK_PEER_LOG_LINE_MAX - 1))) { 281 dev_err(dev, "%s", loc_buf); 282 cnt = 0; 283 } else { 284 cnt++; 285 } 286 log.rd_idx = (log.rd_idx + 1) & log.mask; 287 } 288 /* update rd idx at the end */ 289 vkwrite32(vk, log.rd_idx, BAR_2, 290 vk->peerlog_off + offsetof(struct bcm_vk_peer_log, rd_idx)); 291 } 292 293 void bcm_vk_handle_notf(struct bcm_vk *vk) 294 { 295 u32 reg; 296 struct bcm_vk_alert alert; 297 bool intf_down; 298 unsigned long flags; 299 300 /* handle peer alerts and then locally detected ones */ 301 reg = vkread32(vk, BAR_0, BAR_CARD_ERR_LOG); 302 intf_down = BCM_VK_INTF_IS_DOWN(reg); 303 if (!intf_down) { 304 vk->peer_alert.notfs = reg; 305 bcm_vk_log_notf(vk, &vk->peer_alert, bcm_vk_peer_err, 306 ARRAY_SIZE(bcm_vk_peer_err)); 307 vk->peer_alert.flags = vk->peer_alert.notfs; 308 } else { 309 /* turn off access */ 310 bcm_vk_blk_drv_access(vk); 311 } 312 313 /* check and make copy of alert with lock and then free lock */ 314 spin_lock_irqsave(&vk->host_alert_lock, flags); 315 if (intf_down) 316 vk->host_alert.notfs |= ERR_LOG_HOST_PCIE_DWN; 317 318 alert = vk->host_alert; 319 vk->host_alert.flags = vk->host_alert.notfs; 320 spin_unlock_irqrestore(&vk->host_alert_lock, flags); 321 322 /* call display with copy */ 323 bcm_vk_log_notf(vk, &alert, bcm_vk_host_err, 324 ARRAY_SIZE(bcm_vk_host_err)); 325 326 /* 327 * If it is a sys fault or heartbeat timeout, we would like extract 328 * log msg from the card so that we would know what is the last fault 329 */ 330 if (!intf_down && 331 ((vk->host_alert.flags & ERR_LOG_HOST_HB_FAIL) || 332 (vk->peer_alert.flags & ERR_LOG_SYS_FAULT))) 333 bcm_vk_dump_peer_log(vk); 334 } 335 336 static inline int bcm_vk_wait(struct bcm_vk *vk, enum pci_barno bar, 337 u64 offset, u32 mask, u32 value, 338 unsigned long timeout_ms) 339 { 340 struct device *dev = &vk->pdev->dev; 341 unsigned long start_time; 342 unsigned long timeout; 343 u32 rd_val, boot_status; 344 345 start_time = jiffies; 346 timeout = start_time + msecs_to_jiffies(timeout_ms); 347 348 do { 349 rd_val = vkread32(vk, bar, offset); 350 dev_dbg(dev, "BAR%d Offset=0x%llx: 0x%x\n", 351 bar, offset, rd_val); 352 353 /* check for any boot err condition */ 354 boot_status = vkread32(vk, BAR_0, BAR_BOOT_STATUS); 355 if (boot_status & BOOT_ERR_MASK) { 356 dev_err(dev, "Boot Err 0x%x, progress 0x%x after %d ms\n", 357 (boot_status & BOOT_ERR_MASK) >> BOOT_ERR_SHIFT, 358 boot_status & BOOT_PROG_MASK, 359 jiffies_to_msecs(jiffies - start_time)); 360 return -EFAULT; 361 } 362 363 if (time_after(jiffies, timeout)) 364 return -ETIMEDOUT; 365 366 cpu_relax(); 367 cond_resched(); 368 } while ((rd_val & mask) != value); 369 370 return 0; 371 } 372 373 static void bcm_vk_get_card_info(struct bcm_vk *vk) 374 { 375 struct device *dev = &vk->pdev->dev; 376 u32 offset; 377 int i; 378 u8 *dst; 379 struct bcm_vk_card_info *info = &vk->card_info; 380 381 /* first read the offset from spare register */ 382 offset = vkread32(vk, BAR_0, BAR_CARD_STATIC_INFO); 383 offset &= (pci_resource_len(vk->pdev, BAR_2 * 2) - 1); 384 385 /* based on the offset, read info to internal card info structure */ 386 dst = (u8 *)info; 387 for (i = 0; i < sizeof(*info); i++) 388 *dst++ = vkread8(vk, BAR_2, offset++); 389 390 #define CARD_INFO_LOG_FMT "version : %x\n" \ 391 "os_tag : %s\n" \ 392 "cmpt_tag : %s\n" \ 393 "cpu_freq : %d MHz\n" \ 394 "cpu_scale : %d full, %d lowest\n" \ 395 "ddr_freq : %d MHz\n" \ 396 "ddr_size : %d MB\n" \ 397 "video_freq: %d MHz\n" 398 dev_dbg(dev, CARD_INFO_LOG_FMT, info->version, info->os_tag, 399 info->cmpt_tag, info->cpu_freq_mhz, info->cpu_scale[0], 400 info->cpu_scale[MAX_OPP - 1], info->ddr_freq_mhz, 401 info->ddr_size_MB, info->video_core_freq_mhz); 402 403 /* 404 * get the peer log pointer, only need the offset, and get record 405 * of the log buffer information which would be used for checking 406 * before dump, in case the BAR2 memory has been corrupted. 407 */ 408 vk->peerlog_off = offset; 409 memcpy_fromio(&vk->peerlog_info, vk->bar[BAR_2] + vk->peerlog_off, 410 sizeof(vk->peerlog_info)); 411 412 /* 413 * Do a range checking and if out of bound, the record will be zeroed 414 * which guarantees that nothing would be dumped. In other words, 415 * peer dump is disabled. 416 */ 417 if ((vk->peerlog_info.buf_size > BCM_VK_PEER_LOG_BUF_MAX) || 418 (vk->peerlog_info.mask != (vk->peerlog_info.buf_size - 1)) || 419 (vk->peerlog_info.rd_idx > vk->peerlog_info.mask) || 420 (vk->peerlog_info.wr_idx > vk->peerlog_info.mask)) { 421 dev_err(dev, "Peer log disabled - range error: Size 0x%x(0x%x), [Rd Wr] = [%d %d]\n", 422 vk->peerlog_info.buf_size, 423 vk->peerlog_info.mask, 424 vk->peerlog_info.rd_idx, 425 vk->peerlog_info.wr_idx); 426 memset(&vk->peerlog_info, 0, sizeof(vk->peerlog_info)); 427 } else { 428 dev_dbg(dev, "Peer log: Size 0x%x(0x%x), [Rd Wr] = [%d %d]\n", 429 vk->peerlog_info.buf_size, 430 vk->peerlog_info.mask, 431 vk->peerlog_info.rd_idx, 432 vk->peerlog_info.wr_idx); 433 } 434 } 435 436 static void bcm_vk_get_proc_mon_info(struct bcm_vk *vk) 437 { 438 struct device *dev = &vk->pdev->dev; 439 struct bcm_vk_proc_mon_info *mon = &vk->proc_mon_info; 440 u32 num, entry_size, offset, buf_size; 441 u8 *dst; 442 443 /* calculate offset which is based on peerlog offset */ 444 buf_size = vkread32(vk, BAR_2, 445 vk->peerlog_off 446 + offsetof(struct bcm_vk_peer_log, buf_size)); 447 offset = vk->peerlog_off + sizeof(struct bcm_vk_peer_log) 448 + buf_size; 449 450 /* first read the num and entry size */ 451 num = vkread32(vk, BAR_2, offset); 452 entry_size = vkread32(vk, BAR_2, offset + sizeof(num)); 453 454 /* check for max allowed */ 455 if (num > BCM_VK_PROC_MON_MAX) { 456 dev_err(dev, "Processing monitoring entry %d exceeds max %d\n", 457 num, BCM_VK_PROC_MON_MAX); 458 return; 459 } 460 mon->num = num; 461 mon->entry_size = entry_size; 462 463 vk->proc_mon_off = offset; 464 465 /* read it once that will capture those static info */ 466 dst = (u8 *)&mon->entries[0]; 467 offset += sizeof(num) + sizeof(entry_size); 468 memcpy_fromio(dst, vk->bar[BAR_2] + offset, num * entry_size); 469 } 470 471 static int bcm_vk_sync_card_info(struct bcm_vk *vk) 472 { 473 u32 rdy_marker = vkread32(vk, BAR_1, VK_BAR1_MSGQ_DEF_RDY); 474 475 /* check for marker, but allow diags mode to skip sync */ 476 if (!bcm_vk_msgq_marker_valid(vk)) 477 return (rdy_marker == VK_BAR1_DIAG_RDY_MARKER ? 0 : -EINVAL); 478 479 /* 480 * Write down scratch addr which is used for DMA. For 481 * signed part, BAR1 is accessible only after boot2 has come 482 * up 483 */ 484 if (vk->tdma_addr) { 485 vkwrite32(vk, (u64)vk->tdma_addr >> 32, BAR_1, 486 VK_BAR1_SCRATCH_OFF_HI); 487 vkwrite32(vk, (u32)vk->tdma_addr, BAR_1, 488 VK_BAR1_SCRATCH_OFF_LO); 489 vkwrite32(vk, nr_scratch_pages * PAGE_SIZE, BAR_1, 490 VK_BAR1_SCRATCH_SZ_ADDR); 491 } 492 493 /* get static card info, only need to read once */ 494 bcm_vk_get_card_info(vk); 495 496 /* get the proc mon info once */ 497 bcm_vk_get_proc_mon_info(vk); 498 499 return 0; 500 } 501 502 void bcm_vk_blk_drv_access(struct bcm_vk *vk) 503 { 504 int i; 505 506 /* 507 * kill all the apps except for the process that is resetting. 508 * If not called during reset, reset_pid will be 0, and all will be 509 * killed. 510 */ 511 spin_lock(&vk->ctx_lock); 512 513 /* set msgq_inited to 0 so that all rd/wr will be blocked */ 514 atomic_set(&vk->msgq_inited, 0); 515 516 for (i = 0; i < VK_PID_HT_SZ; i++) { 517 struct bcm_vk_ctx *ctx; 518 519 list_for_each_entry(ctx, &vk->pid_ht[i].head, node) { 520 if (ctx->pid != vk->reset_pid) { 521 dev_dbg(&vk->pdev->dev, 522 "Send kill signal to pid %d\n", 523 ctx->pid); 524 kill_pid(find_vpid(ctx->pid), SIGKILL, 1); 525 } 526 } 527 } 528 bcm_vk_tty_terminate_tty_user(vk); 529 spin_unlock(&vk->ctx_lock); 530 } 531 532 static void bcm_vk_buf_notify(struct bcm_vk *vk, void *bufp, 533 dma_addr_t host_buf_addr, u32 buf_size) 534 { 535 /* update the dma address to the card */ 536 vkwrite32(vk, (u64)host_buf_addr >> 32, BAR_1, 537 VK_BAR1_DMA_BUF_OFF_HI); 538 vkwrite32(vk, (u32)host_buf_addr, BAR_1, 539 VK_BAR1_DMA_BUF_OFF_LO); 540 vkwrite32(vk, buf_size, BAR_1, VK_BAR1_DMA_BUF_SZ); 541 } 542 543 static int bcm_vk_load_image_by_type(struct bcm_vk *vk, u32 load_type, 544 const char *filename) 545 { 546 struct device *dev = &vk->pdev->dev; 547 const struct firmware *fw = NULL; 548 void *bufp = NULL; 549 size_t max_buf, offset; 550 int ret; 551 u64 offset_codepush; 552 u32 codepush; 553 u32 value; 554 dma_addr_t boot_dma_addr; 555 bool is_stdalone; 556 557 if (load_type == VK_IMAGE_TYPE_BOOT1) { 558 /* 559 * After POR, enable VK soft BOOTSRC so bootrom do not clear 560 * the pushed image (the TCM memories). 561 */ 562 value = vkread32(vk, BAR_0, BAR_BOOTSRC_SELECT); 563 value |= BOOTSRC_SOFT_ENABLE; 564 vkwrite32(vk, value, BAR_0, BAR_BOOTSRC_SELECT); 565 566 codepush = CODEPUSH_BOOTSTART + CODEPUSH_BOOT1_ENTRY; 567 offset_codepush = BAR_CODEPUSH_SBL; 568 569 /* Write a 1 to request SRAM open bit */ 570 vkwrite32(vk, CODEPUSH_BOOTSTART, BAR_0, offset_codepush); 571 572 /* Wait for VK to respond */ 573 ret = bcm_vk_wait(vk, BAR_0, BAR_BOOT_STATUS, SRAM_OPEN, 574 SRAM_OPEN, LOAD_IMAGE_TIMEOUT_MS); 575 if (ret < 0) { 576 dev_err(dev, "boot1 wait SRAM err - ret(%d)\n", ret); 577 goto err_buf_out; 578 } 579 580 max_buf = SZ_256K; 581 bufp = dma_alloc_coherent(dev, 582 max_buf, 583 &boot_dma_addr, GFP_KERNEL); 584 if (!bufp) { 585 dev_err(dev, "Error allocating 0x%zx\n", max_buf); 586 ret = -ENOMEM; 587 goto err_buf_out; 588 } 589 } else if (load_type == VK_IMAGE_TYPE_BOOT2) { 590 codepush = CODEPUSH_BOOT2_ENTRY; 591 offset_codepush = BAR_CODEPUSH_SBI; 592 593 /* Wait for VK to respond */ 594 ret = bcm_vk_wait(vk, BAR_0, BAR_BOOT_STATUS, DDR_OPEN, 595 DDR_OPEN, LOAD_IMAGE_TIMEOUT_MS); 596 if (ret < 0) { 597 dev_err(dev, "boot2 wait DDR open error - ret(%d)\n", 598 ret); 599 goto err_buf_out; 600 } 601 602 max_buf = SZ_4M; 603 bufp = dma_alloc_coherent(dev, 604 max_buf, 605 &boot_dma_addr, GFP_KERNEL); 606 if (!bufp) { 607 dev_err(dev, "Error allocating 0x%zx\n", max_buf); 608 ret = -ENOMEM; 609 goto err_buf_out; 610 } 611 612 bcm_vk_buf_notify(vk, bufp, boot_dma_addr, max_buf); 613 } else { 614 dev_err(dev, "Error invalid image type 0x%x\n", load_type); 615 ret = -EINVAL; 616 goto err_buf_out; 617 } 618 619 offset = 0; 620 ret = request_partial_firmware_into_buf(&fw, filename, dev, 621 bufp, max_buf, offset); 622 if (ret) { 623 dev_err(dev, "Error %d requesting firmware file: %s\n", 624 ret, filename); 625 goto err_firmware_out; 626 } 627 dev_dbg(dev, "size=0x%zx\n", fw->size); 628 if (load_type == VK_IMAGE_TYPE_BOOT1) 629 memcpy_toio(vk->bar[BAR_1] + BAR1_CODEPUSH_BASE_BOOT1, 630 bufp, 631 fw->size); 632 633 dev_dbg(dev, "Signaling 0x%x to 0x%llx\n", codepush, offset_codepush); 634 vkwrite32(vk, codepush, BAR_0, offset_codepush); 635 636 if (load_type == VK_IMAGE_TYPE_BOOT1) { 637 u32 boot_status; 638 639 /* wait until done */ 640 ret = bcm_vk_wait(vk, BAR_0, BAR_BOOT_STATUS, 641 BOOT1_RUNNING, 642 BOOT1_RUNNING, 643 BOOT1_STARTUP_TIMEOUT_MS); 644 645 boot_status = vkread32(vk, BAR_0, BAR_BOOT_STATUS); 646 is_stdalone = !BCM_VK_INTF_IS_DOWN(boot_status) && 647 (boot_status & BOOT_STDALONE_RUNNING); 648 if (ret && !is_stdalone) { 649 dev_err(dev, 650 "Timeout %ld ms waiting for boot1 to come up - ret(%d)\n", 651 BOOT1_STARTUP_TIMEOUT_MS, ret); 652 goto err_firmware_out; 653 } else if (is_stdalone) { 654 u32 reg; 655 656 reg = vkread32(vk, BAR_0, BAR_BOOT1_STDALONE_PROGRESS); 657 if ((reg & BOOT1_STDALONE_PROGRESS_MASK) == 658 BOOT1_STDALONE_SUCCESS) { 659 dev_info(dev, "Boot1 standalone success\n"); 660 ret = 0; 661 } else { 662 dev_err(dev, "Timeout %ld ms - Boot1 standalone failure\n", 663 BOOT1_STARTUP_TIMEOUT_MS); 664 ret = -EINVAL; 665 goto err_firmware_out; 666 } 667 } 668 } else if (load_type == VK_IMAGE_TYPE_BOOT2) { 669 unsigned long timeout; 670 671 timeout = jiffies + msecs_to_jiffies(LOAD_IMAGE_TIMEOUT_MS); 672 673 /* To send more data to VK than max_buf allowed at a time */ 674 do { 675 /* 676 * Check for ack from card. when Ack is received, 677 * it means all the data is received by card. 678 * Exit the loop after ack is received. 679 */ 680 ret = bcm_vk_wait(vk, BAR_0, BAR_BOOT_STATUS, 681 FW_LOADER_ACK_RCVD_ALL_DATA, 682 FW_LOADER_ACK_RCVD_ALL_DATA, 683 TXFR_COMPLETE_TIMEOUT_MS); 684 if (ret == 0) { 685 dev_dbg(dev, "Exit boot2 download\n"); 686 break; 687 } else if (ret == -EFAULT) { 688 dev_err(dev, "Error detected during ACK waiting"); 689 goto err_firmware_out; 690 } 691 692 /* exit the loop, if there is no response from card */ 693 if (time_after(jiffies, timeout)) { 694 dev_err(dev, "Error. No reply from card\n"); 695 ret = -ETIMEDOUT; 696 goto err_firmware_out; 697 } 698 699 /* Wait for VK to open BAR space to copy new data */ 700 ret = bcm_vk_wait(vk, BAR_0, offset_codepush, 701 codepush, 0, 702 TXFR_COMPLETE_TIMEOUT_MS); 703 if (ret == 0) { 704 offset += max_buf; 705 ret = request_partial_firmware_into_buf 706 (&fw, 707 filename, 708 dev, bufp, 709 max_buf, 710 offset); 711 if (ret) { 712 dev_err(dev, 713 "Error %d requesting firmware file: %s offset: 0x%zx\n", 714 ret, filename, offset); 715 goto err_firmware_out; 716 } 717 dev_dbg(dev, "size=0x%zx\n", fw->size); 718 dev_dbg(dev, "Signaling 0x%x to 0x%llx\n", 719 codepush, offset_codepush); 720 vkwrite32(vk, codepush, BAR_0, offset_codepush); 721 /* reload timeout after every codepush */ 722 timeout = jiffies + 723 msecs_to_jiffies(LOAD_IMAGE_TIMEOUT_MS); 724 } else if (ret == -EFAULT) { 725 dev_err(dev, "Error detected waiting for transfer\n"); 726 goto err_firmware_out; 727 } 728 } while (1); 729 730 /* wait for fw status bits to indicate app ready */ 731 ret = bcm_vk_wait(vk, BAR_0, VK_BAR_FWSTS, 732 VK_FWSTS_READY, 733 VK_FWSTS_READY, 734 BOOT2_STARTUP_TIMEOUT_MS); 735 if (ret < 0) { 736 dev_err(dev, "Boot2 not ready - ret(%d)\n", ret); 737 goto err_firmware_out; 738 } 739 740 is_stdalone = vkread32(vk, BAR_0, BAR_BOOT_STATUS) & 741 BOOT_STDALONE_RUNNING; 742 if (!is_stdalone) { 743 ret = bcm_vk_intf_ver_chk(vk); 744 if (ret) { 745 dev_err(dev, "failure in intf version check\n"); 746 goto err_firmware_out; 747 } 748 749 /* 750 * Next, initialize Message Q if we are loading boot2. 751 * Do a force sync 752 */ 753 ret = bcm_vk_sync_msgq(vk, true); 754 if (ret) { 755 dev_err(dev, "Boot2 Error reading comm msg Q info\n"); 756 ret = -EIO; 757 goto err_firmware_out; 758 } 759 760 /* sync & channel other info */ 761 ret = bcm_vk_sync_card_info(vk); 762 if (ret) { 763 dev_err(dev, "Syncing Card Info failure\n"); 764 goto err_firmware_out; 765 } 766 } 767 } 768 769 err_firmware_out: 770 release_firmware(fw); 771 772 err_buf_out: 773 if (bufp) 774 dma_free_coherent(dev, max_buf, bufp, boot_dma_addr); 775 776 return ret; 777 } 778 779 static u32 bcm_vk_next_boot_image(struct bcm_vk *vk) 780 { 781 u32 boot_status; 782 u32 fw_status; 783 u32 load_type = 0; /* default for unknown */ 784 785 boot_status = vkread32(vk, BAR_0, BAR_BOOT_STATUS); 786 fw_status = vkread32(vk, BAR_0, VK_BAR_FWSTS); 787 788 if (!BCM_VK_INTF_IS_DOWN(boot_status) && (boot_status & SRAM_OPEN)) 789 load_type = VK_IMAGE_TYPE_BOOT1; 790 else if (boot_status == BOOT1_RUNNING) 791 load_type = VK_IMAGE_TYPE_BOOT2; 792 793 /* Log status so that we know different stages */ 794 dev_info(&vk->pdev->dev, 795 "boot-status value for next image: 0x%x : fw-status 0x%x\n", 796 boot_status, fw_status); 797 798 return load_type; 799 } 800 801 static enum soc_idx get_soc_idx(struct bcm_vk *vk) 802 { 803 struct pci_dev *pdev = vk->pdev; 804 enum soc_idx idx = VK_IDX_INVALID; 805 u32 rev; 806 static enum soc_idx const vk_soc_tab[] = { VALKYRIE_A0, VALKYRIE_B0 }; 807 808 switch (pdev->device) { 809 case PCI_DEVICE_ID_VALKYRIE: 810 /* get the chip id to decide sub-class */ 811 rev = MAJOR_SOC_REV(vkread32(vk, BAR_0, BAR_CHIP_ID)); 812 if (rev < ARRAY_SIZE(vk_soc_tab)) { 813 idx = vk_soc_tab[rev]; 814 } else { 815 /* Default to A0 firmware for all other chip revs */ 816 idx = VALKYRIE_A0; 817 dev_warn(&pdev->dev, 818 "Rev %d not in image lookup table, default to idx=%d\n", 819 rev, idx); 820 } 821 break; 822 823 case PCI_DEVICE_ID_VIPER: 824 idx = VIPER; 825 break; 826 827 default: 828 dev_err(&pdev->dev, "no images for 0x%x\n", pdev->device); 829 } 830 return idx; 831 } 832 833 static const char *get_load_fw_name(struct bcm_vk *vk, 834 const struct load_image_entry *entry) 835 { 836 const struct firmware *fw; 837 struct device *dev = &vk->pdev->dev; 838 int ret; 839 unsigned long dummy; 840 int i; 841 842 for (i = 0; i < IMG_PER_TYPE_MAX; i++) { 843 fw = NULL; 844 ret = request_partial_firmware_into_buf(&fw, 845 entry->image_name[i], 846 dev, &dummy, 847 sizeof(dummy), 848 0); 849 release_firmware(fw); 850 if (!ret) 851 return entry->image_name[i]; 852 } 853 return NULL; 854 } 855 856 int bcm_vk_auto_load_all_images(struct bcm_vk *vk) 857 { 858 int i, ret = -1; 859 enum soc_idx idx; 860 struct device *dev = &vk->pdev->dev; 861 u32 curr_type; 862 const char *curr_name; 863 864 idx = get_soc_idx(vk); 865 if (idx == VK_IDX_INVALID) 866 goto auto_load_all_exit; 867 868 /* log a message to know the relative loading order */ 869 dev_dbg(dev, "Load All for device %d\n", vk->devid); 870 871 for (i = 0; i < NUM_BOOT_STAGES; i++) { 872 curr_type = image_tab[idx][i].image_type; 873 if (bcm_vk_next_boot_image(vk) == curr_type) { 874 curr_name = get_load_fw_name(vk, &image_tab[idx][i]); 875 if (!curr_name) { 876 dev_err(dev, "No suitable firmware exists for type %d", 877 curr_type); 878 ret = -ENOENT; 879 goto auto_load_all_exit; 880 } 881 ret = bcm_vk_load_image_by_type(vk, curr_type, 882 curr_name); 883 dev_info(dev, "Auto load %s, ret %d\n", 884 curr_name, ret); 885 886 if (ret) { 887 dev_err(dev, "Error loading default %s\n", 888 curr_name); 889 goto auto_load_all_exit; 890 } 891 } 892 } 893 894 auto_load_all_exit: 895 return ret; 896 } 897 898 static int bcm_vk_trigger_autoload(struct bcm_vk *vk) 899 { 900 if (test_and_set_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload) != 0) 901 return -EPERM; 902 903 set_bit(BCM_VK_WQ_DWNLD_AUTO, vk->wq_offload); 904 queue_work(vk->wq_thread, &vk->wq_work); 905 906 return 0; 907 } 908 909 /* 910 * deferred work queue for draining and auto download. 911 */ 912 static void bcm_vk_wq_handler(struct work_struct *work) 913 { 914 struct bcm_vk *vk = container_of(work, struct bcm_vk, wq_work); 915 struct device *dev = &vk->pdev->dev; 916 s32 ret; 917 918 /* check wq offload bit map to perform various operations */ 919 if (test_bit(BCM_VK_WQ_NOTF_PEND, vk->wq_offload)) { 920 /* clear bit right the way for notification */ 921 clear_bit(BCM_VK_WQ_NOTF_PEND, vk->wq_offload); 922 bcm_vk_handle_notf(vk); 923 } 924 if (test_bit(BCM_VK_WQ_DWNLD_AUTO, vk->wq_offload)) { 925 bcm_vk_auto_load_all_images(vk); 926 927 /* 928 * at the end of operation, clear AUTO bit and pending 929 * bit 930 */ 931 clear_bit(BCM_VK_WQ_DWNLD_AUTO, vk->wq_offload); 932 clear_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload); 933 } 934 935 /* next, try to drain */ 936 ret = bcm_to_h_msg_dequeue(vk); 937 938 if (ret == 0) 939 dev_dbg(dev, "Spurious trigger for workqueue\n"); 940 else if (ret < 0) 941 bcm_vk_blk_drv_access(vk); 942 } 943 944 static long bcm_vk_load_image(struct bcm_vk *vk, 945 const struct vk_image __user *arg) 946 { 947 struct device *dev = &vk->pdev->dev; 948 const char *image_name; 949 struct vk_image image; 950 u32 next_loadable; 951 enum soc_idx idx; 952 int image_idx; 953 int ret = -EPERM; 954 955 if (copy_from_user(&image, arg, sizeof(image))) 956 return -EACCES; 957 958 if ((image.type != VK_IMAGE_TYPE_BOOT1) && 959 (image.type != VK_IMAGE_TYPE_BOOT2)) { 960 dev_err(dev, "invalid image.type %u\n", image.type); 961 return ret; 962 } 963 964 next_loadable = bcm_vk_next_boot_image(vk); 965 if (next_loadable != image.type) { 966 dev_err(dev, "Next expected image %u, Loading %u\n", 967 next_loadable, image.type); 968 return ret; 969 } 970 971 /* 972 * if something is pending download already. This could only happen 973 * for now when the driver is being loaded, or if someone has issued 974 * another download command in another shell. 975 */ 976 if (test_and_set_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload) != 0) { 977 dev_err(dev, "Download operation already pending.\n"); 978 return ret; 979 } 980 981 image_name = image.filename; 982 if (image_name[0] == '\0') { 983 /* Use default image name if NULL */ 984 idx = get_soc_idx(vk); 985 if (idx == VK_IDX_INVALID) 986 goto err_idx; 987 988 /* Image idx starts with boot1 */ 989 image_idx = image.type - VK_IMAGE_TYPE_BOOT1; 990 image_name = get_load_fw_name(vk, &image_tab[idx][image_idx]); 991 if (!image_name) { 992 dev_err(dev, "No suitable image found for type %d", 993 image.type); 994 ret = -ENOENT; 995 goto err_idx; 996 } 997 } else { 998 /* Ensure filename is NULL terminated */ 999 image.filename[sizeof(image.filename) - 1] = '\0'; 1000 } 1001 ret = bcm_vk_load_image_by_type(vk, image.type, image_name); 1002 dev_info(dev, "Load %s, ret %d\n", image_name, ret); 1003 err_idx: 1004 clear_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload); 1005 1006 return ret; 1007 } 1008 1009 static int bcm_vk_reset_successful(struct bcm_vk *vk) 1010 { 1011 struct device *dev = &vk->pdev->dev; 1012 u32 fw_status, reset_reason; 1013 int ret = -EAGAIN; 1014 1015 /* 1016 * Reset could be triggered when the card in several state: 1017 * i) in bootROM 1018 * ii) after boot1 1019 * iii) boot2 running 1020 * 1021 * i) & ii) - no status bits will be updated. If vkboot1 1022 * runs automatically after reset, it will update the reason 1023 * to be unknown reason 1024 * iii) - reboot reason match + deinit done. 1025 */ 1026 fw_status = vkread32(vk, BAR_0, VK_BAR_FWSTS); 1027 /* immediate exit if interface goes down */ 1028 if (BCM_VK_INTF_IS_DOWN(fw_status)) { 1029 dev_err(dev, "PCIe Intf Down!\n"); 1030 goto reset_exit; 1031 } 1032 1033 reset_reason = (fw_status & VK_FWSTS_RESET_REASON_MASK); 1034 if ((reset_reason == VK_FWSTS_RESET_MBOX_DB) || 1035 (reset_reason == VK_FWSTS_RESET_UNKNOWN)) 1036 ret = 0; 1037 1038 /* 1039 * if some of the deinit bits are set, but done 1040 * bit is not, this is a failure if triggered while boot2 is running 1041 */ 1042 if ((fw_status & VK_FWSTS_DEINIT_TRIGGERED) && 1043 !(fw_status & VK_FWSTS_RESET_DONE)) 1044 ret = -EAGAIN; 1045 1046 reset_exit: 1047 dev_dbg(dev, "FW status = 0x%x ret %d\n", fw_status, ret); 1048 1049 return ret; 1050 } 1051 1052 static void bcm_to_v_reset_doorbell(struct bcm_vk *vk, u32 db_val) 1053 { 1054 vkwrite32(vk, db_val, BAR_0, VK_BAR0_RESET_DB_BASE); 1055 } 1056 1057 static int bcm_vk_trigger_reset(struct bcm_vk *vk) 1058 { 1059 u32 i; 1060 u32 value, boot_status; 1061 bool is_stdalone, is_boot2; 1062 static const u32 bar0_reg_clr_list[] = { BAR_OS_UPTIME, 1063 BAR_INTF_VER, 1064 BAR_CARD_VOLTAGE, 1065 BAR_CARD_TEMPERATURE, 1066 BAR_CARD_PWR_AND_THRE }; 1067 1068 /* clean up before pressing the door bell */ 1069 bcm_vk_drain_msg_on_reset(vk); 1070 vkwrite32(vk, 0, BAR_1, VK_BAR1_MSGQ_DEF_RDY); 1071 /* make tag '\0' terminated */ 1072 vkwrite32(vk, 0, BAR_1, VK_BAR1_BOOT1_VER_TAG); 1073 1074 for (i = 0; i < VK_BAR1_DAUTH_MAX; i++) { 1075 vkwrite32(vk, 0, BAR_1, VK_BAR1_DAUTH_STORE_ADDR(i)); 1076 vkwrite32(vk, 0, BAR_1, VK_BAR1_DAUTH_VALID_ADDR(i)); 1077 } 1078 for (i = 0; i < VK_BAR1_SOTP_REVID_MAX; i++) 1079 vkwrite32(vk, 0, BAR_1, VK_BAR1_SOTP_REVID_ADDR(i)); 1080 1081 memset(&vk->card_info, 0, sizeof(vk->card_info)); 1082 memset(&vk->peerlog_info, 0, sizeof(vk->peerlog_info)); 1083 memset(&vk->proc_mon_info, 0, sizeof(vk->proc_mon_info)); 1084 memset(&vk->alert_cnts, 0, sizeof(vk->alert_cnts)); 1085 1086 /* 1087 * When boot request fails, the CODE_PUSH_OFFSET stays persistent. 1088 * Allowing us to debug the failure. When we call reset, 1089 * we should clear CODE_PUSH_OFFSET so ROM does not execute 1090 * boot again (and fails again) and instead waits for a new 1091 * codepush. And, if previous boot has encountered error, need 1092 * to clear the entry values 1093 */ 1094 boot_status = vkread32(vk, BAR_0, BAR_BOOT_STATUS); 1095 if (boot_status & BOOT_ERR_MASK) { 1096 dev_info(&vk->pdev->dev, 1097 "Card in boot error 0x%x, clear CODEPUSH val\n", 1098 boot_status); 1099 value = 0; 1100 } else { 1101 value = vkread32(vk, BAR_0, BAR_CODEPUSH_SBL); 1102 value &= CODEPUSH_MASK; 1103 } 1104 vkwrite32(vk, value, BAR_0, BAR_CODEPUSH_SBL); 1105 1106 /* special reset handling */ 1107 is_stdalone = boot_status & BOOT_STDALONE_RUNNING; 1108 is_boot2 = (boot_status & BOOT_STATE_MASK) == BOOT2_RUNNING; 1109 if (vk->peer_alert.flags & ERR_LOG_RAMDUMP) { 1110 /* 1111 * if card is in ramdump mode, it is hitting an error. Don't 1112 * reset the reboot reason as it will contain valid info that 1113 * is important - simply use special reset 1114 */ 1115 vkwrite32(vk, VK_BAR0_RESET_RAMPDUMP, BAR_0, VK_BAR_FWSTS); 1116 return VK_BAR0_RESET_RAMPDUMP; 1117 } else if (is_stdalone && !is_boot2) { 1118 dev_info(&vk->pdev->dev, "Hard reset on Standalone mode"); 1119 bcm_to_v_reset_doorbell(vk, VK_BAR0_RESET_DB_HARD); 1120 return VK_BAR0_RESET_DB_HARD; 1121 } 1122 1123 /* reset fw_status with proper reason, and press db */ 1124 vkwrite32(vk, VK_FWSTS_RESET_MBOX_DB, BAR_0, VK_BAR_FWSTS); 1125 bcm_to_v_reset_doorbell(vk, VK_BAR0_RESET_DB_SOFT); 1126 1127 /* clear other necessary registers and alert records */ 1128 for (i = 0; i < ARRAY_SIZE(bar0_reg_clr_list); i++) 1129 vkwrite32(vk, 0, BAR_0, bar0_reg_clr_list[i]); 1130 memset(&vk->host_alert, 0, sizeof(vk->host_alert)); 1131 memset(&vk->peer_alert, 0, sizeof(vk->peer_alert)); 1132 /* clear 4096 bits of bitmap */ 1133 bitmap_clear(vk->bmap, 0, VK_MSG_ID_BITMAP_SIZE); 1134 1135 return 0; 1136 } 1137 1138 static long bcm_vk_reset(struct bcm_vk *vk, struct vk_reset __user *arg) 1139 { 1140 struct device *dev = &vk->pdev->dev; 1141 struct vk_reset reset; 1142 int ret = 0; 1143 u32 ramdump_reset; 1144 int special_reset; 1145 1146 if (copy_from_user(&reset, arg, sizeof(struct vk_reset))) 1147 return -EFAULT; 1148 1149 /* check if any download is in-progress, if so return error */ 1150 if (test_and_set_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload) != 0) { 1151 dev_err(dev, "Download operation pending - skip reset.\n"); 1152 return -EPERM; 1153 } 1154 1155 ramdump_reset = vk->peer_alert.flags & ERR_LOG_RAMDUMP; 1156 dev_info(dev, "Issue Reset %s\n", 1157 ramdump_reset ? "in ramdump mode" : ""); 1158 1159 /* 1160 * The following is the sequence of reset: 1161 * - send card level graceful shut down 1162 * - wait enough time for VK to handle its business, stopping DMA etc 1163 * - kill host apps 1164 * - Trigger interrupt with DB 1165 */ 1166 bcm_vk_send_shutdown_msg(vk, VK_SHUTDOWN_GRACEFUL, 0, 0); 1167 1168 spin_lock(&vk->ctx_lock); 1169 if (!vk->reset_pid) { 1170 vk->reset_pid = task_pid_nr(current); 1171 } else { 1172 dev_err(dev, "Reset already launched by process pid %d\n", 1173 vk->reset_pid); 1174 ret = -EACCES; 1175 } 1176 spin_unlock(&vk->ctx_lock); 1177 if (ret) 1178 goto err_exit; 1179 1180 bcm_vk_blk_drv_access(vk); 1181 special_reset = bcm_vk_trigger_reset(vk); 1182 1183 /* 1184 * Wait enough time for card os to deinit 1185 * and populate the reset reason. 1186 */ 1187 msleep(BCM_VK_DEINIT_TIME_MS); 1188 1189 if (special_reset) { 1190 /* if it is special ramdump reset, return the type to user */ 1191 reset.arg2 = special_reset; 1192 if (copy_to_user(arg, &reset, sizeof(reset))) 1193 ret = -EFAULT; 1194 } else { 1195 ret = bcm_vk_reset_successful(vk); 1196 } 1197 1198 err_exit: 1199 clear_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload); 1200 return ret; 1201 } 1202 1203 static int bcm_vk_mmap(struct file *file, struct vm_area_struct *vma) 1204 { 1205 struct bcm_vk_ctx *ctx = file->private_data; 1206 struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev); 1207 unsigned long pg_size; 1208 1209 /* only BAR2 is mmap possible, which is bar num 4 due to 64bit */ 1210 #define VK_MMAPABLE_BAR 4 1211 1212 pg_size = ((pci_resource_len(vk->pdev, VK_MMAPABLE_BAR) - 1) 1213 >> PAGE_SHIFT) + 1; 1214 if (vma->vm_pgoff + vma_pages(vma) > pg_size) 1215 return -EINVAL; 1216 1217 vma->vm_pgoff += (pci_resource_start(vk->pdev, VK_MMAPABLE_BAR) 1218 >> PAGE_SHIFT); 1219 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1220 1221 return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, 1222 vma->vm_end - vma->vm_start, 1223 vma->vm_page_prot); 1224 } 1225 1226 static long bcm_vk_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 1227 { 1228 long ret = -EINVAL; 1229 struct bcm_vk_ctx *ctx = file->private_data; 1230 struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev); 1231 void __user *argp = (void __user *)arg; 1232 1233 dev_dbg(&vk->pdev->dev, 1234 "ioctl, cmd=0x%02x, arg=0x%02lx\n", 1235 cmd, arg); 1236 1237 mutex_lock(&vk->mutex); 1238 1239 switch (cmd) { 1240 case VK_IOCTL_LOAD_IMAGE: 1241 ret = bcm_vk_load_image(vk, argp); 1242 break; 1243 1244 case VK_IOCTL_RESET: 1245 ret = bcm_vk_reset(vk, argp); 1246 break; 1247 1248 default: 1249 break; 1250 } 1251 1252 mutex_unlock(&vk->mutex); 1253 1254 return ret; 1255 } 1256 1257 static const struct file_operations bcm_vk_fops = { 1258 .owner = THIS_MODULE, 1259 .open = bcm_vk_open, 1260 .read = bcm_vk_read, 1261 .write = bcm_vk_write, 1262 .poll = bcm_vk_poll, 1263 .release = bcm_vk_release, 1264 .mmap = bcm_vk_mmap, 1265 .unlocked_ioctl = bcm_vk_ioctl, 1266 }; 1267 1268 static int bcm_vk_on_panic(struct notifier_block *nb, 1269 unsigned long e, void *p) 1270 { 1271 struct bcm_vk *vk = container_of(nb, struct bcm_vk, panic_nb); 1272 1273 bcm_to_v_reset_doorbell(vk, VK_BAR0_RESET_DB_HARD); 1274 1275 return 0; 1276 } 1277 1278 static int bcm_vk_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 1279 { 1280 int err; 1281 int i; 1282 int id; 1283 int irq; 1284 char name[20]; 1285 struct bcm_vk *vk; 1286 struct device *dev = &pdev->dev; 1287 struct miscdevice *misc_device; 1288 u32 boot_status; 1289 1290 /* allocate vk structure which is tied to kref for freeing */ 1291 vk = kzalloc(sizeof(*vk), GFP_KERNEL); 1292 if (!vk) 1293 return -ENOMEM; 1294 1295 kref_init(&vk->kref); 1296 if (nr_ib_sgl_blk > BCM_VK_IB_SGL_BLK_MAX) { 1297 dev_warn(dev, "Inband SGL blk %d limited to max %d\n", 1298 nr_ib_sgl_blk, BCM_VK_IB_SGL_BLK_MAX); 1299 nr_ib_sgl_blk = BCM_VK_IB_SGL_BLK_MAX; 1300 } 1301 vk->ib_sgl_size = nr_ib_sgl_blk * VK_MSGQ_BLK_SIZE; 1302 mutex_init(&vk->mutex); 1303 1304 err = pci_enable_device(pdev); 1305 if (err) { 1306 dev_err(dev, "Cannot enable PCI device\n"); 1307 goto err_free_exit; 1308 } 1309 vk->pdev = pci_dev_get(pdev); 1310 1311 err = pci_request_regions(pdev, DRV_MODULE_NAME); 1312 if (err) { 1313 dev_err(dev, "Cannot obtain PCI resources\n"); 1314 goto err_disable_pdev; 1315 } 1316 1317 /* make sure DMA is good */ 1318 err = dma_set_mask_and_coherent(&pdev->dev, 1319 DMA_BIT_MASK(BCM_VK_DMA_BITS)); 1320 if (err) { 1321 dev_err(dev, "failed to set DMA mask\n"); 1322 goto err_disable_pdev; 1323 } 1324 1325 /* The tdma is a scratch area for some DMA testings. */ 1326 if (nr_scratch_pages) { 1327 vk->tdma_vaddr = dma_alloc_coherent 1328 (dev, 1329 nr_scratch_pages * PAGE_SIZE, 1330 &vk->tdma_addr, GFP_KERNEL); 1331 if (!vk->tdma_vaddr) { 1332 err = -ENOMEM; 1333 goto err_disable_pdev; 1334 } 1335 } 1336 1337 pci_set_master(pdev); 1338 pci_set_drvdata(pdev, vk); 1339 1340 irq = pci_alloc_irq_vectors(pdev, 1341 1, 1342 VK_MSIX_IRQ_MAX, 1343 PCI_IRQ_MSI | PCI_IRQ_MSIX); 1344 1345 if (irq < VK_MSIX_IRQ_MIN_REQ) { 1346 dev_err(dev, "failed to get min %d MSIX interrupts, irq(%d)\n", 1347 VK_MSIX_IRQ_MIN_REQ, irq); 1348 err = (irq >= 0) ? -EINVAL : irq; 1349 goto err_disable_pdev; 1350 } 1351 1352 if (irq != VK_MSIX_IRQ_MAX) 1353 dev_warn(dev, "Number of IRQs %d allocated - requested(%d).\n", 1354 irq, VK_MSIX_IRQ_MAX); 1355 1356 for (i = 0; i < MAX_BAR; i++) { 1357 /* multiple by 2 for 64 bit BAR mapping */ 1358 vk->bar[i] = pci_ioremap_bar(pdev, i * 2); 1359 if (!vk->bar[i]) { 1360 dev_err(dev, "failed to remap BAR%d\n", i); 1361 err = -ENOMEM; 1362 goto err_iounmap; 1363 } 1364 } 1365 1366 for (vk->num_irqs = 0; 1367 vk->num_irqs < VK_MSIX_MSGQ_MAX; 1368 vk->num_irqs++) { 1369 err = devm_request_irq(dev, pci_irq_vector(pdev, vk->num_irqs), 1370 bcm_vk_msgq_irqhandler, 1371 IRQF_SHARED, DRV_MODULE_NAME, vk); 1372 if (err) { 1373 dev_err(dev, "failed to request msgq IRQ %d for MSIX %d\n", 1374 pdev->irq + vk->num_irqs, vk->num_irqs + 1); 1375 goto err_irq; 1376 } 1377 } 1378 /* one irq for notification from VK */ 1379 err = devm_request_irq(dev, pci_irq_vector(pdev, vk->num_irqs), 1380 bcm_vk_notf_irqhandler, 1381 IRQF_SHARED, DRV_MODULE_NAME, vk); 1382 if (err) { 1383 dev_err(dev, "failed to request notf IRQ %d for MSIX %d\n", 1384 pdev->irq + vk->num_irqs, vk->num_irqs + 1); 1385 goto err_irq; 1386 } 1387 vk->num_irqs++; 1388 1389 for (i = 0; 1390 (i < VK_MSIX_TTY_MAX) && (vk->num_irqs < irq); 1391 i++, vk->num_irqs++) { 1392 err = devm_request_irq(dev, pci_irq_vector(pdev, vk->num_irqs), 1393 bcm_vk_tty_irqhandler, 1394 IRQF_SHARED, DRV_MODULE_NAME, vk); 1395 if (err) { 1396 dev_err(dev, "failed request tty IRQ %d for MSIX %d\n", 1397 pdev->irq + vk->num_irqs, vk->num_irqs + 1); 1398 goto err_irq; 1399 } 1400 bcm_vk_tty_set_irq_enabled(vk, i); 1401 } 1402 1403 id = ida_simple_get(&bcm_vk_ida, 0, 0, GFP_KERNEL); 1404 if (id < 0) { 1405 err = id; 1406 dev_err(dev, "unable to get id\n"); 1407 goto err_irq; 1408 } 1409 1410 vk->devid = id; 1411 snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id); 1412 misc_device = &vk->miscdev; 1413 misc_device->minor = MISC_DYNAMIC_MINOR; 1414 misc_device->name = kstrdup(name, GFP_KERNEL); 1415 if (!misc_device->name) { 1416 err = -ENOMEM; 1417 goto err_ida_remove; 1418 } 1419 misc_device->fops = &bcm_vk_fops, 1420 1421 err = misc_register(misc_device); 1422 if (err) { 1423 dev_err(dev, "failed to register device\n"); 1424 goto err_kfree_name; 1425 } 1426 1427 INIT_WORK(&vk->wq_work, bcm_vk_wq_handler); 1428 1429 /* create dedicated workqueue */ 1430 vk->wq_thread = create_singlethread_workqueue(name); 1431 if (!vk->wq_thread) { 1432 dev_err(dev, "Fail to create workqueue thread\n"); 1433 err = -ENOMEM; 1434 goto err_misc_deregister; 1435 } 1436 1437 err = bcm_vk_msg_init(vk); 1438 if (err) { 1439 dev_err(dev, "failed to init msg queue info\n"); 1440 goto err_destroy_workqueue; 1441 } 1442 1443 /* sync other info */ 1444 bcm_vk_sync_card_info(vk); 1445 1446 /* register for panic notifier */ 1447 vk->panic_nb.notifier_call = bcm_vk_on_panic; 1448 err = atomic_notifier_chain_register(&panic_notifier_list, 1449 &vk->panic_nb); 1450 if (err) { 1451 dev_err(dev, "Fail to register panic notifier\n"); 1452 goto err_destroy_workqueue; 1453 } 1454 1455 snprintf(name, sizeof(name), KBUILD_MODNAME ".%d_ttyVK", id); 1456 err = bcm_vk_tty_init(vk, name); 1457 if (err) 1458 goto err_unregister_panic_notifier; 1459 1460 /* 1461 * lets trigger an auto download. We don't want to do it serially here 1462 * because at probing time, it is not supposed to block for a long time. 1463 */ 1464 boot_status = vkread32(vk, BAR_0, BAR_BOOT_STATUS); 1465 if (auto_load) { 1466 if ((boot_status & BOOT_STATE_MASK) == BROM_RUNNING) { 1467 err = bcm_vk_trigger_autoload(vk); 1468 if (err) 1469 goto err_bcm_vk_tty_exit; 1470 } else { 1471 dev_err(dev, 1472 "Auto-load skipped - BROM not in proper state (0x%x)\n", 1473 boot_status); 1474 } 1475 } 1476 1477 /* enable hb */ 1478 bcm_vk_hb_init(vk); 1479 1480 dev_dbg(dev, "BCM-VK:%u created\n", id); 1481 1482 return 0; 1483 1484 err_bcm_vk_tty_exit: 1485 bcm_vk_tty_exit(vk); 1486 1487 err_unregister_panic_notifier: 1488 atomic_notifier_chain_unregister(&panic_notifier_list, 1489 &vk->panic_nb); 1490 1491 err_destroy_workqueue: 1492 destroy_workqueue(vk->wq_thread); 1493 1494 err_misc_deregister: 1495 misc_deregister(misc_device); 1496 1497 err_kfree_name: 1498 kfree(misc_device->name); 1499 misc_device->name = NULL; 1500 1501 err_ida_remove: 1502 ida_simple_remove(&bcm_vk_ida, id); 1503 1504 err_irq: 1505 for (i = 0; i < vk->num_irqs; i++) 1506 devm_free_irq(dev, pci_irq_vector(pdev, i), vk); 1507 1508 pci_disable_msix(pdev); 1509 pci_disable_msi(pdev); 1510 1511 err_iounmap: 1512 for (i = 0; i < MAX_BAR; i++) { 1513 if (vk->bar[i]) 1514 pci_iounmap(pdev, vk->bar[i]); 1515 } 1516 pci_release_regions(pdev); 1517 1518 err_disable_pdev: 1519 if (vk->tdma_vaddr) 1520 dma_free_coherent(&pdev->dev, nr_scratch_pages * PAGE_SIZE, 1521 vk->tdma_vaddr, vk->tdma_addr); 1522 1523 pci_free_irq_vectors(pdev); 1524 pci_disable_device(pdev); 1525 pci_dev_put(pdev); 1526 1527 err_free_exit: 1528 kfree(vk); 1529 1530 return err; 1531 } 1532 1533 void bcm_vk_release_data(struct kref *kref) 1534 { 1535 struct bcm_vk *vk = container_of(kref, struct bcm_vk, kref); 1536 struct pci_dev *pdev = vk->pdev; 1537 1538 dev_dbg(&pdev->dev, "BCM-VK:%d release data 0x%p\n", vk->devid, vk); 1539 pci_dev_put(pdev); 1540 kfree(vk); 1541 } 1542 1543 static void bcm_vk_remove(struct pci_dev *pdev) 1544 { 1545 int i; 1546 struct bcm_vk *vk = pci_get_drvdata(pdev); 1547 struct miscdevice *misc_device = &vk->miscdev; 1548 1549 bcm_vk_hb_deinit(vk); 1550 1551 /* 1552 * Trigger a reset to card and wait enough time for UCODE to rerun, 1553 * which re-initialize the card into its default state. 1554 * This ensures when driver is re-enumerated it will start from 1555 * a completely clean state. 1556 */ 1557 bcm_vk_trigger_reset(vk); 1558 usleep_range(BCM_VK_UCODE_BOOT_US, BCM_VK_UCODE_BOOT_MAX_US); 1559 1560 /* unregister panic notifier */ 1561 atomic_notifier_chain_unregister(&panic_notifier_list, 1562 &vk->panic_nb); 1563 1564 bcm_vk_msg_remove(vk); 1565 bcm_vk_tty_exit(vk); 1566 1567 if (vk->tdma_vaddr) 1568 dma_free_coherent(&pdev->dev, nr_scratch_pages * PAGE_SIZE, 1569 vk->tdma_vaddr, vk->tdma_addr); 1570 1571 /* remove if name is set which means misc dev registered */ 1572 if (misc_device->name) { 1573 misc_deregister(misc_device); 1574 kfree(misc_device->name); 1575 ida_simple_remove(&bcm_vk_ida, vk->devid); 1576 } 1577 for (i = 0; i < vk->num_irqs; i++) 1578 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), vk); 1579 1580 pci_disable_msix(pdev); 1581 pci_disable_msi(pdev); 1582 1583 cancel_work_sync(&vk->wq_work); 1584 destroy_workqueue(vk->wq_thread); 1585 bcm_vk_tty_wq_exit(vk); 1586 1587 for (i = 0; i < MAX_BAR; i++) { 1588 if (vk->bar[i]) 1589 pci_iounmap(pdev, vk->bar[i]); 1590 } 1591 1592 dev_dbg(&pdev->dev, "BCM-VK:%d released\n", vk->devid); 1593 1594 pci_release_regions(pdev); 1595 pci_free_irq_vectors(pdev); 1596 pci_disable_device(pdev); 1597 1598 kref_put(&vk->kref, bcm_vk_release_data); 1599 } 1600 1601 static void bcm_vk_shutdown(struct pci_dev *pdev) 1602 { 1603 struct bcm_vk *vk = pci_get_drvdata(pdev); 1604 u32 reg, boot_stat; 1605 1606 reg = vkread32(vk, BAR_0, BAR_BOOT_STATUS); 1607 boot_stat = reg & BOOT_STATE_MASK; 1608 1609 if (boot_stat == BOOT1_RUNNING) { 1610 /* simply trigger a reset interrupt to park it */ 1611 bcm_vk_trigger_reset(vk); 1612 } else if (boot_stat == BROM_NOT_RUN) { 1613 int err; 1614 u16 lnksta; 1615 1616 /* 1617 * The boot status only reflects boot condition since last reset 1618 * As ucode will run only once to configure pcie, if multiple 1619 * resets happen, we lost track if ucode has run or not. 1620 * Here, read the current link speed and use that to 1621 * sync up the bootstatus properly so that on reboot-back-up, 1622 * it has the proper state to start with autoload 1623 */ 1624 err = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta); 1625 if (!err && 1626 (lnksta & PCI_EXP_LNKSTA_CLS) != PCI_EXP_LNKSTA_CLS_2_5GB) { 1627 reg |= BROM_STATUS_COMPLETE; 1628 vkwrite32(vk, reg, BAR_0, BAR_BOOT_STATUS); 1629 } 1630 } 1631 } 1632 1633 static const struct pci_device_id bcm_vk_ids[] = { 1634 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_VALKYRIE), }, 1635 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_VIPER), }, 1636 { } 1637 }; 1638 MODULE_DEVICE_TABLE(pci, bcm_vk_ids); 1639 1640 static struct pci_driver pci_driver = { 1641 .name = DRV_MODULE_NAME, 1642 .id_table = bcm_vk_ids, 1643 .probe = bcm_vk_probe, 1644 .remove = bcm_vk_remove, 1645 .shutdown = bcm_vk_shutdown, 1646 }; 1647 module_pci_driver(pci_driver); 1648 1649 MODULE_DESCRIPTION("Broadcom VK Host Driver"); 1650 MODULE_AUTHOR("Scott Branden <scott.branden@broadcom.com>"); 1651 MODULE_LICENSE("GPL v2"); 1652 MODULE_VERSION("1.0"); 1653