Skip to contentSkip to conversation

Habana Labs kernel driver

PATCH Superseded Oded Gabbay 17 participants 103 messages Jan 23, 2019 – Feb 24, 2019 Page 1 of 3

Subsystems (7)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover Habana Labs kernel driver

Hello,

For those who don't know me, my name is Oded Gabbay (Kernel Maintainer for AMD's amdkfd driver, worked at RedHat's Desktop group) and I work at Habana Labs since its inception two and a half years ago.

Habana is a leading startup in the emerging AI processor space and we have already started production of our first Goya inference processor PCIe card and delivered it to customers. The Goya processor silicon has been tested since June of 2018 and is production-qualified by now. The Gaudi training processor solution is slated to sample in the second quarter of 2019.

This patch-set contains the kernel driver for Habana's AI Processors (AIP) that are designed to accelerate Deep Learning inference and training workloads. The current version supports only the Goya processor and support for Gaudi will be upstreamed after the ASIC will be available to customers.

The Goya processor has been designed from the ground up for deep learning inference workloads. It comprises a cluster of eight fully programmable Tensor Processing Cores (TPC). The TPC core is a VLIW SIMD vector processor with ISA and hardware that was tailored to serve deep learning workloads efficiently.

In addition, Goya contains software-managed, on-die memory along with five separate DMA channels, a PCIe Gen4 x16 system interface and 4/8/16GB of DDR4 memory.

Goya has 3 PCI bars (64-bit), which are not exposed to user-space. They map the on-chip memory and configuration space (bar 0-1), MSI-X table (bar 2-3) and DDR4 memory (bar 4-5).

Each TPC engine and DMA channel has a H/W queue attached to it, called QMAN. The S/W provides command buffers to the H/W queues (through the kernel driver) and the H/W consumes the command buffers. To prevent malicious users from stealing data from other users through the Host or Device memory, Goya has an internal MMU and a security protection scheme. In addition, The kernel driver parses the command buffer and rejects it if it contains disallowed commands.

The QMANs are triggered by a write to a PI (producer index) register. The QMAN H/W logic maintains a CI (consumer index) register. When PI==CI, the queue is empty. When PI+1==CI, the queue is full (note the queue is cyclic). Each entry in the H/W queue is 16-bytes, and contains a pointer and length of a variable-size command buffer, which the user fills with specific commands that the H/W logic can read and execute.

For each DMA QMAN, there is a completion queue that the QMAN writes to when it finishes the execution of the command buffer. The QMAN also sends an MSI-X interrupt after writing the completion entry.

Inference workloads running on Goya are associated with an address space through the ASID (address-space ID) property. Goya supports up to 1024 ASIDs. The ASID value is updated by the kernel driver in the relevant registers before scheduling a workload.

During its initialization, the driver registers itself to the PCI subsystem. For each Habana PCI device found, a char device node (/dev/hlX) is created.

The driver currently exposes a total of five IOCTLs. One IOCTL allows the application to submit workloads to the device, and another to wait on completion of submitted workloads. The other three IOCTLs are used for memory management, command buffer creation and information/status retrieval.

In addition, the driver exposes several sensors through the hwmon subsystem and provides various system-level information in sysfs for system administrators.

The first step for an application process is to open the correct hlX device it wants to work with. Calls to open create a new "context" for that application in the driver's internal structures and a unique ASID is assigned to that context. The context object lives until the process releases the file descriptor AND its command submissions have finished executing on the device.

Next step is for the application to request information about the device, such as amount of DDR4 memory. The application then can go on to create command buffers for its command submissions and allocate and map device or host memory (host memory can only be mapped) to the internal device's MMU subsystem.

At this point the application can load various deep learning topologies to the device DDR memory. After that, it can start to submit inference workloads using those topologies. For each workload, the the application receives a sequence number that represents the workload. The application can then query the driver regarding the status of the workload using that sequence number.

In case a workload didn't finish execution after 5 seconds (configurable using a kernel module parameter) from the time it was scheduled to run, a TDR (timeout detection & recovery) event occurs in the driver. The driver will then mark that workload as "timed out", perform a minimal reset of the device (DMA and compute units only) and abort all other workloads of that context that were already submitted to the H/W queues.

I would appricate any feedback, question and/or review.

p.s. for those who prefer to clone the tree instead of looking at the emails, you can grab a copy from our company's page in GitHub:

https://github.com/HabanaAI/linux/releases/tag/hl_patchset_v1

Thanks,
Oded

Oded Gabbay (14):
habanalabs: add skeleton driver
habanalabs: add Goya registers header files
habanalabs: add basic Goya support
habanalabs: add context and ASID modules
habanalabs: add command buffer module
habanalabs: add basic Goya h/w initialization
habanalabs: add h/w queues module
habanalabs: add event queue and interrupts
habanalabs: add sysfs and hwmon support
habanalabs: add device reset support
habanalabs: add command submission module
habanalabs: implement INFO IOCTL
habanalabs: add debugfs support
Update MAINTAINERS and CREDITS with habanalabs info

Omer Shpigelman (1):
habanalabs: add virtual memory and MMU modules

 CREDITS                                       |    2 +-
 .../ABI/testing/debugfs-driver-habanalabs     |  127 +
 .../ABI/testing/sysfs-driver-habanalabs       |  190 +
 MAINTAINERS                                   |    9 +
 drivers/misc/Kconfig                          |    1 +
 drivers/misc/Makefile                         |    1 +
 drivers/misc/habanalabs/Kconfig               |   22 +
 drivers/misc/habanalabs/Makefile              |   14 +
 drivers/misc/habanalabs/asid.c                |   58 +
 drivers/misc/habanalabs/command_buffer.c      |  425 +
 drivers/misc/habanalabs/command_submission.c  |  799 ++
 drivers/misc/habanalabs/context.c             |  216 +
 drivers/misc/habanalabs/debugfs.c             | 1069 ++
 drivers/misc/habanalabs/device.c              | 1097 ++
 drivers/misc/habanalabs/goya/Makefile         |    3 +
 drivers/misc/habanalabs/goya/goya.c           | 6347 ++++++++++++
 drivers/misc/habanalabs/goya/goyaP.h          |  161 +
 drivers/misc/habanalabs/goya/goya_hwmgr.c     |  306 +
 drivers/misc/habanalabs/goya/goya_security.c  | 2999 ++++++
 drivers/misc/habanalabs/habanalabs.h          | 1464 +++
 drivers/misc/habanalabs/habanalabs_drv.c      |  474 +
 drivers/misc/habanalabs/habanalabs_ioctl.c    |  237 +
 drivers/misc/habanalabs/hw_queue.c            |  654 ++
 drivers/misc/habanalabs/hwmon.c               |  449 +
 .../include/goya/asic_reg/cpu_ca53_cfg_regs.h |  213 +
 .../include/goya/asic_reg/cpu_if_regs.h       |  110 +
 .../include/goya/asic_reg/cpu_pll_regs.h      |  186 +
 .../include/goya/asic_reg/ddr_mc_ch0_regs.h   | 1158 +++
 .../include/goya/asic_reg/ddr_mc_ch1_regs.h   | 1158 +++
 .../include/goya/asic_reg/ddr_misc_ch0_regs.h |  156 +
 .../include/goya/asic_reg/ddr_misc_ch1_regs.h |  156 +
 .../include/goya/asic_reg/dma_ch_0_regs.h     |  512 +
 .../include/goya/asic_reg/dma_ch_1_regs.h     |  512 +
 .../include/goya/asic_reg/dma_ch_2_regs.h     |  512 +
 .../include/goya/asic_reg/dma_ch_3_regs.h     |  512 +
 .../include/goya/asic_reg/dma_ch_4_regs.h     |  512 +
 .../include/goya/asic_reg/dma_macro_regs.h    |  242 +
 .../include/goya/asic_reg/dma_nrtr_regs.h     |  380 +
 .../include/goya/asic_reg/dma_qm_0_regs.h     |  543 +
 .../include/goya/asic_reg/dma_qm_1_regs.h     |  543 +
 .../include/goya/asic_reg/dma_qm_2_regs.h     |  543 +
 .../include/goya/asic_reg/dma_qm_3_regs.h     |  543 +
 .../include/goya/asic_reg/dma_qm_4_regs.h     |  543 +
 .../include/goya/asic_reg/gic_regs.h          | 9079 +++++++++++++++++
 .../include/goya/asic_reg/goya_blocks.h       | 1372 +++
 .../include/goya/asic_reg/goya_masks.h        |  262 +
 .../include/goya/asic_reg/goya_regs.h         |  119 +
 .../include/goya/asic_reg/ic_pll_regs.h       |  186 +
 .../include/goya/asic_reg/mc_pll_regs.h       |  186 +
 .../include/goya/asic_reg/mme1_rtr_regs.h     |  876 ++
 .../include/goya/asic_reg/mme2_rtr_regs.h     |  876 ++
 .../include/goya/asic_reg/mme3_rtr_regs.h     |  876 ++
 .../include/goya/asic_reg/mme4_rtr_regs.h     |  876 ++
 .../include/goya/asic_reg/mme5_rtr_regs.h     |  876 ++
 .../include/goya/asic_reg/mme6_rtr_regs.h     |  876 ++
 .../include/goya/asic_reg/mme_cmdq_regs.h     |  431 +
 .../include/goya/asic_reg/mme_qm_regs.h       |  543 +
 .../include/goya/asic_reg/mme_regs.h          | 2422 +++++
 .../include/goya/asic_reg/mmu_regs.h          |  158 +
 .../include/goya/asic_reg/pci_nrtr_regs.h     |  380 +
 .../include/goya/asic_reg/pcie_aux_regs.h     |  476 +
 .../include/goya/asic_reg/pcie_dbi_regs.h     | 2909 ++++++
 .../goya/asic_reg/psoc_emmc_pll_regs.h        |  186 +
 .../goya/asic_reg/psoc_global_conf_regs.h     | 1119 ++
 .../include/goya/asic_reg/psoc_mme_pll_regs.h |  186 +
 .../include/goya/asic_reg/psoc_pci_pll_regs.h |  186 +
 .../include/goya/asic_reg/psoc_spi_regs.h     |  427 +
 .../goya/asic_reg/sram_y0_x0_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y0_x1_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y0_x2_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y0_x3_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y0_x4_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y1_x0_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y1_x1_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y1_x2_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y1_x3_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y1_x4_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y2_x0_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y2_x1_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y2_x2_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y2_x3_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y2_x4_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y3_x0_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y3_x1_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y3_x2_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y3_x3_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y3_x4_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y4_x0_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y4_x1_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y4_x2_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y4_x3_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y4_x4_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y5_x0_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y5_x1_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y5_x2_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y5_x3_rtr_regs.h       |  215 +
 .../goya/asic_reg/sram_y5_x4_rtr_regs.h       |  215 +
 .../include/goya/asic_reg/stlb_regs.h         |  133 +
 .../include/goya/asic_reg/sync_mngr_regs.h    | 4930 +++++++++
 .../include/goya/asic_reg/tpc0_cfg_regs.h     | 2110 ++++
 .../include/goya/asic_reg/tpc0_cmdq_regs.h    |  431 +
 .../include/goya/asic_reg/tpc0_eml_cfg_regs.h |  580 ++
 .../include/goya/asic_reg/tpc0_nrtr_regs.h    |  380 +
 .../include/goya/asic_reg/tpc0_qm_regs.h      |  543 +
 .../include/goya/asic_reg/tpc1_cfg_regs.h     | 2110 ++++
 .../include/goya/asic_reg/tpc1_cmdq_regs.h    |  431 +
 .../include/goya/asic_reg/tpc1_qm_regs.h      |  543 +
 .../include/goya/asic_reg/tpc1_rtr_regs.h     |  848 ++
 .../include/goya/asic_reg/tpc2_cfg_regs.h     | 2110 ++++
 .../include/goya/asic_reg/tpc2_cmdq_regs.h    |  431 +
 .../include/goya/asic_reg/tpc2_qm_regs.h      |  543 +
 .../include/goya/asic_reg/tpc2_rtr_regs.h     |  848 ++
 .../include/goya/asic_reg/tpc3_cfg_regs.h     | 2110 ++++
 .../include/goya/asic_reg/tpc3_cmdq_regs.h    |  431 +
 .../include/goya/asic_reg/tpc3_qm_regs.h      |  543 +
 .../include/goya/asic_reg/tpc3_rtr_regs.h     |  848 ++
 .../include/goya/asic_reg/tpc4_cfg_regs.h     | 2110 ++++
 .../include/goya/asic_reg/tpc4_cmdq_regs.h    |  431 +
 .../include/goya/asic_reg/tpc4_qm_regs.h      |  543 +
 .../include/goya/asic_reg/tpc4_rtr_regs.h     |  848 ++
 .../include/goya/asic_reg/tpc5_cfg_regs.h     | 2110 ++++
 .../include/goya/asic_reg/tpc5_cmdq_regs.h    |  431 +
 .../include/goya/asic_reg/tpc5_qm_regs.h      |  543 +
 .../include/goya/asic_reg/tpc5_rtr_regs.h     |  848 ++
 .../include/goya/asic_reg/tpc6_cfg_regs.h     | 2110 ++++
 .../include/goya/asic_reg/tpc6_cmdq_regs.h    |  431 +
 .../include/goya/asic_reg/tpc6_qm_regs.h      |  543 +
 .../include/goya/asic_reg/tpc6_rtr_regs.h     |  848 ++
 .../include/goya/asic_reg/tpc7_cfg_regs.h     | 2110 ++++
 .../include/goya/asic_reg/tpc7_cmdq_regs.h    |  431 +
 .../include/goya/asic_reg/tpc7_nrtr_regs.h    |  380 +
 .../include/goya/asic_reg/tpc7_qm_regs.h      |  543 +
 .../include/goya/asic_reg/tpc_pll_regs.h      |  186 +
 drivers/misc/habanalabs/include/goya/goya.h   |  117 +
 .../include/goya/goya_async_events.h          |  186 +
 .../habanalabs/include/goya/goya_boot_if.h    |   32 +
 .../habanalabs/include/goya/goya_packets.h    |  234 +
 .../habanalabs/include/habanalabs_device_if.h |  397 +
 .../include/hw_ip/mmu/mmu_general.h           |   45 +
 .../habanalabs/include/hw_ip/mmu/mmu_v1_0.h   |   15 +
 drivers/misc/habanalabs/irq.c                 |  325 +
 drivers/misc/habanalabs/memory.c              | 1714 ++++
 drivers/misc/habanalabs/mmu.c                 |  604 ++
 drivers/misc/habanalabs/sysfs.c               |  690 ++
 include/uapi/misc/habanalabs.h                |  412 +
 145 files changed, 99610 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/debugfs-driver-habanalabs
 create mode 100644 Documentation/ABI/testing/sysfs-driver-habanalabs
 create mode 100644 drivers/misc/habanalabs/Kconfig
 create mode 100644 drivers/misc/habanalabs/Makefile
 create mode 100644 drivers/misc/habanalabs/asid.c
 create mode 100644 drivers/misc/habanalabs/command_buffer.c
 create mode 100644 drivers/misc/habanalabs/command_submission.c
 create mode 100644 drivers/misc/habanalabs/context.c
 create mode 100644 drivers/misc/habanalabs/debugfs.c
 create mode 100644 drivers/misc/habanalabs/device.c
 create mode 100644 drivers/misc/habanalabs/goya/Makefile
 create mode 100644 drivers/misc/habanalabs/goya/goya.c
 create mode 100644 drivers/misc/habanalabs/goya/goyaP.h
 create mode 100644 drivers/misc/habanalabs/goya/goya_hwmgr.c
 create mode 100644 drivers/misc/habanalabs/goya/goya_security.c
 create mode 100644 drivers/misc/habanalabs/habanalabs.h
 create mode 100644 drivers/misc/habanalabs/habanalabs_drv.c
 create mode 100644 drivers/misc/habanalabs/habanalabs_ioctl.c
 create mode 100644 drivers/misc/habanalabs/hw_queue.c
 create mode 100644 drivers/misc/habanalabs/hwmon.c
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/cpu_ca53_cfg_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/cpu_if_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/cpu_pll_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/ddr_mc_ch0_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/ddr_mc_ch1_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/ddr_misc_ch0_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/ddr_misc_ch1_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_ch_0_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_ch_1_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_ch_2_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_ch_3_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_ch_4_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_macro_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_nrtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_qm_0_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_qm_1_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_qm_2_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_qm_3_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/dma_qm_4_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/gic_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/goya_blocks.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/goya_masks.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/goya_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/ic_pll_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/mc_pll_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/mme1_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/mme2_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/mme3_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/mme4_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/mme5_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/mme6_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/mme_cmdq_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/mme_qm_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/mme_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/mmu_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/pci_nrtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/pcie_aux_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/pcie_dbi_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/psoc_emmc_pll_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/psoc_global_conf_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/psoc_mme_pll_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/psoc_pci_pll_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/psoc_spi_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y0_x0_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y0_x1_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y0_x2_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y0_x3_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y0_x4_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y1_x0_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y1_x1_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y1_x2_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y1_x3_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y1_x4_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y2_x0_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y2_x1_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y2_x2_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y2_x3_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y2_x4_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y3_x0_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y3_x1_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y3_x2_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y3_x3_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y3_x4_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y4_x0_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y4_x1_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y4_x2_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y4_x3_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y4_x4_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y5_x0_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y5_x1_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y5_x2_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y5_x3_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sram_y5_x4_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/stlb_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/sync_mngr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc0_cfg_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc0_cmdq_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc0_eml_cfg_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc0_nrtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc0_qm_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc1_cfg_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc1_cmdq_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc1_qm_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc1_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc2_cfg_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc2_cmdq_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc2_qm_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc2_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc3_cfg_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc3_cmdq_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc3_qm_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc3_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc4_cfg_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc4_cmdq_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc4_qm_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc4_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc5_cfg_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc5_cmdq_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc5_qm_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc5_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc6_cfg_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc6_cmdq_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc6_qm_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc6_rtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc7_cfg_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc7_cmdq_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc7_nrtr_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc7_qm_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/asic_reg/tpc_pll_regs.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya_async_events.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya_boot_if.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya_packets.h
 create mode 100644 drivers/misc/habanalabs/include/habanalabs_device_if.h
 create mode 100644 drivers/misc/habanalabs/include/hw_ip/mmu/mmu_general.h
 create mode 100644 drivers/misc/habanalabs/include/hw_ip/mmu/mmu_v1_0.h
 create mode 100644 drivers/misc/habanalabs/irq.c
 create mode 100644 drivers/misc/habanalabs/memory.c
 create mode 100644 drivers/misc/habanalabs/mmu.c
 create mode 100644 drivers/misc/habanalabs/sysfs.c
 create mode 100644 include/uapi/misc/habanalabs.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/15 habanalabs: add skeleton driver

This patch adds the habanalabs skeleton driver. The driver does nothing at this stage except very basic operations. It contains the minimal code to insmod and rmmod the driver and to create a /dev/hlX file per PCI device.

---
 drivers/misc/Kconfig                          |   1 +
 drivers/misc/Makefile                         |   1 +
 drivers/misc/habanalabs/Kconfig               |  22 ++
 drivers/misc/habanalabs/Makefile              |   7 +
 drivers/misc/habanalabs/device.c              | 331 ++++++++++++++++
 drivers/misc/habanalabs/habanalabs.h          | 149 +++++++
 drivers/misc/habanalabs/habanalabs_drv.c      | 366 ++++++++++++++++++
 .../habanalabs/include/habanalabs_device_if.h | 125 ++++++
 8 files changed, 1002 insertions(+)
 create mode 100644 drivers/misc/habanalabs/Kconfig
 create mode 100644 drivers/misc/habanalabs/Makefile
 create mode 100644 drivers/misc/habanalabs/device.c
 create mode 100644 drivers/misc/habanalabs/habanalabs.h
 create mode 100644 drivers/misc/habanalabs/habanalabs_drv.c
 create mode 100644 drivers/misc/habanalabs/include/habanalabs_device_if.h
drivers/misc/Kconfig +1 -0
index f417b06e11c5..fecab53c4f21 100644
@@ -535,4 +535,5 @@ source "drivers/misc/echo/Kconfig"
 source "drivers/misc/cxl/Kconfig"
 source "drivers/misc/ocxl/Kconfig"
 source "drivers/misc/cardreader/Kconfig"
+source "drivers/misc/habanalabs/Kconfig"
 endmenu
drivers/misc/Makefile +1 -0
index e39ccbbc1b3a..ae77dfd790a4 100644
@@ -59,3 +59,4 @@ obj-$(CONFIG_PCI_ENDPOINT_TEST)	+= pci_endpoint_test.o
 obj-$(CONFIG_OCXL)		+= ocxl/
 obj-y				+= cardreader/
 obj-$(CONFIG_PVPANIC)   	+= pvpanic.o
+obj-$(CONFIG_HABANA_AI)		+= habanalabs/
drivers/misc/habanalabs/Kconfig new file +22 -0
new file mode 100644
index 000000000000..b7f38a14caf5
@@ -0,0 +1,22 @@
+#
+# HabanaLabs AI accelerators driver
+#
+
+config HABANA_AI
+	tristate "HabanaAI accelerators (habanalabs)"
+	depends on PCI
+	select FRAME_VECTOR
+	help
+	  Enables PCIe card driver for Habana's AI Processors (AIP) that are
+	  designed to accelerate Deep Learning inference and training workloads.
+
+	  The driver manages the PCIe devices and provides IOCTL interface for
+	  the user to submit workloads to the devices.
+
+	  The user-space interface is described in
+	  include/uapi/misc/habanalabs.h
+
+	  If unsure, say N.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called habanalabs.
drivers/misc/habanalabs/Makefile new file +7 -0
new file mode 100644
index 000000000000..b41433a09e02
@@ -0,0 +1,7 @@
+#
+# Makefile for HabanaLabs AI accelerators driver
+#
+
+obj-m	:= habanalabs.o
+
+habanalabs-y := habanalabs_drv.o device.o
\ No newline at end of file
drivers/misc/habanalabs/device.c new file +331 -0

331 lines not on this page · raw message

drivers/misc/habanalabs/habanalabs.h new file +149 -0

149 lines not on this page · raw message

drivers/misc/habanalabs/habanalabs_drv.c new file +366 -0

366 lines not on this page · raw message

drivers/misc/habanalabs/include/habanalabs_device_if.h new file +125 -0

125 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

Quoted 3 lines · Oded Gabbay

This patch adds the habanalabs skeleton driver. The driver does nothing at this stage except very basic operations. It contains the minimal code to insmod and rmmod the driver and to create a /dev/hlX file per PCI device.

trivial notes:

drivers/misc/habanalabs/Makefile

Quoted 2 lines
drivers/misc/habanalabs/Makefile +0 -0

[]

Quoted 1 line

\ No newline at end of file

You should fixes these. There are a least a couple of them.

drivers/misc/habanalabs/device.c

Quoted 1 line
drivers/misc/habanalabs/device.c +0 -0

[]

drivers/misc/habanalabs/device.c line 6

Quoted 7 lines
drivers/misc/habanalabs/device.c +6 -0
@@ -0,0 +1,331 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */

Add #define pr_fmt(fmt) "habanalabs: " fmt

drivers/misc/habanalabs/device.c line 8

Quoted 2 lines
drivers/misc/habanalabs/device.c +2 -0
+
+#include "habanalabs.h"

or add it in this file

drivers/misc/habanalabs/device.c line 82

Quoted 8 lines
drivers/misc/habanalabs/device.c +8 -0
+static int device_setup_cdev(struct hl_device *hdev, struct class *hclass,
+				int minor, const struct file_operations *fops)
+{
+	int err, devno = MKDEV(hdev->major, minor);
+	struct cdev *hdev_cdev = &hdev->cdev;
+	char name[8];
+
+	sprintf(name, "hl%d", hdev->id);

Might overflow name one day

drivers/misc/habanalabs/device.c line 88

Quoted 6 lines
drivers/misc/habanalabs/device.c +6 -0
+
+	cdev_init(hdev_cdev, fops);
+	hdev_cdev->owner = THIS_MODULE;
+	err = cdev_add(hdev_cdev, devno, 1);
+	if (err) {
+		pr_err("habanalabs: Failed to add char device %s", name);

So #define pr_fmt can auto prefix these and this would be

		pr_err("Failed to add char device %s\n", name);

missing terminating '\n' btw

drivers/misc/habanalabs/device.c line 94

Quoted 6 lines
drivers/misc/habanalabs/device.c +6 -0
+		goto err_cdev_add;
+	}
+
+	hdev->dev = device_create(hclass, NULL, devno, NULL, "%s", name);
+	if (IS_ERR(hdev->dev)) {
+		pr_err("habanalabs: Failed to create device %s\n", name);

And this would be:

		pr_err("Failed to create device %s\n", name);

etc...

drivers/misc/habanalabs/device.c line 121

Quoted 5 lines
drivers/misc/habanalabs/device.c +5 -0
+static int device_early_init(struct hl_device *hdev)
+{
+	switch (hdev->asic_type) {
+	case ASIC_GOYA:
+		sprintf(hdev->asic_name, "GOYA");

strcpy or perhaps better still as strlcpy

drivers/misc/habanalabs/device.c line 35

Quoted 2 lines
drivers/misc/habanalabs/device.c +2 -0
+int hl_device_init(struct hl_device *hdev, struct class *hclass)
+{

[]

drivers/misc/habanalabs/device.c line 212

Quoted 2 lines
drivers/misc/habanalabs/device.c +2 -0
+	dev_notice(hdev->dev,
+		"Successfully added device to habanalabs driver\n");

This is mostly aligned to open parenthesis, but perhaps it could check with scripts/checkpatch.pl --strict and see if you agree with anything it bleats.

drivers/misc/habanalabs/device.c line 278

Quoted 9 lines
drivers/misc/habanalabs/device.c +9 -0
+int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr,
+				u32 timeout_us, u32 *val)
+{
+	/*
+	 * pReturnVal is defined as volatile because it points to HOST memory,
+	 * which is being written to by the device. Therefore, we can't use
+	 * locks to synchronize it and it is not a memory-mapped register space
+	 */
+	volatile u32 *pReturnVal = (volatile u32 *) addr;

It'd be nice to avoid hungarian and camelcase

Quoted 6 lines
drivers/misc/habanalabs/device.c +6 -0
+	ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
+
+	might_sleep();
+
+	for (;;) {
+		*val = *pReturnVal;

drivers/misc/habanalabs/device.c line 294

Quoted 10 lines
drivers/misc/habanalabs/device.c +10 -0
+		if (*val)
+			break;
+		if (ktime_compare(ktime_get(), timeout) > 0) {
+			*val = *pReturnVal;
+			break;
+		}
+		usleep_range((100 >> 2) + 1, 100);
+	}
+
+	return (*val ? 0 : -ETIMEDOUT);

Unnecessary parentheses

drivers/misc/habanalabs/habanalabs_drv.c

Quoted 1 line
drivers/misc/habanalabs/habanalabs_drv.c +0 -0

[]

drivers/misc/habanalabs/habanalabs_drv.c line 40

Quoted 4 lines
drivers/misc/habanalabs/habanalabs_drv.c +4 -0
+static struct pci_device_id ids[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
+	{ 0, }
+};

static const?

drivers/misc/habanalabs/include/habanalabs_device_if.h

Quoted 1 line
drivers/misc/habanalabs/include/habanalabs_device_if.h +0 -0

[]

drivers/misc/habanalabs/include/habanalabs_device_if.h line 29

Quoted 13 lines
drivers/misc/habanalabs/include/habanalabs_device_if.h +13 -0
+struct hl_bd {
+	__u64	ptr;
+	__u32	len;
+	union {
+		struct {
+			__u32	repeat:16;
+			__u32	res1:8;
+			__u32	repeat_valid:1;
+			__u32	res2:7;
+		};
+		__u32	ctl;
+	};
+};

Maybe use the appropriate bit-endian __le<size> instead of __u<size> with whatever cpu_to_le<size> / le<size>_to_cpu bits are necessary.

#3 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

Quoted 130 lines · Joe Perches
Quoted 3 lines · Oded Gabbay

This patch adds the habanalabs skeleton driver. The driver does nothing at this stage except very basic operations. It contains the minimal code to insmod and rmmod the driver and to create a /dev/hlX file per PCI device.

trivial notes:

drivers/misc/habanalabs/Makefile

Quoted 2 lines
drivers/misc/habanalabs/Makefile +0 -0

[]

Quoted 1 line

\ No newline at end of file

You should fixes these. There are a least a couple of them.

drivers/misc/habanalabs/device.c

Quoted 1 line
drivers/misc/habanalabs/device.c +0 -0

[]

drivers/misc/habanalabs/device.c line 6

Quoted 7 lines
drivers/misc/habanalabs/device.c +6 -0
@@ -0,0 +1,331 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */

Add #define pr_fmt(fmt) "habanalabs: " fmt

drivers/misc/habanalabs/device.c line 8

Quoted 2 lines
drivers/misc/habanalabs/device.c +2 -0
+
+#include "habanalabs.h"

or add it in this file

drivers/misc/habanalabs/device.c line 82

Quoted 8 lines
drivers/misc/habanalabs/device.c +8 -0
+static int device_setup_cdev(struct hl_device *hdev, struct class *hclass,
+                             int minor, const struct file_operations *fops)
+{
+     int err, devno = MKDEV(hdev->major, minor);
+     struct cdev *hdev_cdev = &hdev->cdev;
+     char name[8];
+
+     sprintf(name, "hl%d", hdev->id);

Might overflow name one day

drivers/misc/habanalabs/device.c line 88

Quoted 6 lines
drivers/misc/habanalabs/device.c +6 -0
+
+     cdev_init(hdev_cdev, fops);
+     hdev_cdev->owner = THIS_MODULE;
+     err = cdev_add(hdev_cdev, devno, 1);
+     if (err) {
+             pr_err("habanalabs: Failed to add char device %s", name);

So #define pr_fmt can auto prefix these and this would be

                pr_err("Failed to add char device %s\n", name);

missing terminating '\n' btw

drivers/misc/habanalabs/device.c line 94

Quoted 6 lines
drivers/misc/habanalabs/device.c +6 -0
+             goto err_cdev_add;
+     }
+
+     hdev->dev = device_create(hclass, NULL, devno, NULL, "%s", name);
+     if (IS_ERR(hdev->dev)) {
+             pr_err("habanalabs: Failed to create device %s\n", name);

And this would be:

                pr_err("Failed to create device %s\n", name);

etc...

drivers/misc/habanalabs/device.c line 121

Quoted 5 lines
drivers/misc/habanalabs/device.c +5 -0
+static int device_early_init(struct hl_device *hdev)
+{
+     switch (hdev->asic_type) {
+     case ASIC_GOYA:
+             sprintf(hdev->asic_name, "GOYA");

strcpy or perhaps better still as strlcpy

drivers/misc/habanalabs/device.c line 35

Quoted 2 lines
drivers/misc/habanalabs/device.c +2 -0
+int hl_device_init(struct hl_device *hdev, struct class *hclass)
+{

[]

drivers/misc/habanalabs/device.c line 212

Quoted 2 lines
drivers/misc/habanalabs/device.c +2 -0
+     dev_notice(hdev->dev,
+             "Successfully added device to habanalabs driver\n");

This is mostly aligned to open parenthesis, but perhaps it could check with scripts/checkpatch.pl --strict and see if you agree with anything it bleats.

drivers/misc/habanalabs/device.c line 278

Quoted 9 lines
drivers/misc/habanalabs/device.c +9 -0
+int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr,
+                             u32 timeout_us, u32 *val)
+{
+     /*
+      * pReturnVal is defined as volatile because it points to HOST memory,
+      * which is being written to by the device. Therefore, we can't use
+      * locks to synchronize it and it is not a memory-mapped register space
+      */
+     volatile u32 *pReturnVal = (volatile u32 *) addr;

It'd be nice to avoid hungarian and camelcase

Quoted 6 lines
drivers/misc/habanalabs/device.c +6 -0
+     ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
+
+     might_sleep();
+
+     for (;;) {
+             *val = *pReturnVal;

drivers/misc/habanalabs/device.c line 294

Quoted 10 lines
drivers/misc/habanalabs/device.c +10 -0
+             if (*val)
+                     break;
+             if (ktime_compare(ktime_get(), timeout) > 0) {
+                     *val = *pReturnVal;
+                     break;
+             }
+             usleep_range((100 >> 2) + 1, 100);
+     }
+
+     return (*val ? 0 : -ETIMEDOUT);

Unnecessary parentheses

drivers/misc/habanalabs/habanalabs_drv.c

Quoted 1 line
drivers/misc/habanalabs/habanalabs_drv.c +0 -0

[]

drivers/misc/habanalabs/habanalabs_drv.c line 40

Quoted 4 lines
drivers/misc/habanalabs/habanalabs_drv.c +4 -0
+static struct pci_device_id ids[] = {
+     { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
+     { 0, }
+};

static const?

drivers/misc/habanalabs/include/habanalabs_device_if.h

Quoted 1 line
drivers/misc/habanalabs/include/habanalabs_device_if.h +0 -0

[]

Quoted 18 lines

drivers/misc/habanalabs/include/habanalabs_device_if.h line 29

Quoted 13 lines
drivers/misc/habanalabs/include/habanalabs_device_if.h +13 -0
+struct hl_bd {
+     __u64   ptr;
+     __u32   len;
+     union {
+             struct {
+                     __u32   repeat:16;
+                     __u32   res1:8;
+                     __u32   repeat_valid:1;
+                     __u32   res2:7;
+             };
+             __u32   ctl;
+     };
+};

Maybe use the appropriate bit-endian __le<size> instead of __u<size> with whatever cpu_to_le<size> / le<size>_to_cpu bits are necessary.

Hi Joe,
Thanks for the review.
I fixed everything except for two things:
1. Alignment to open parenthesis. I never code like that in the kernel and I don't really believe in anything that requires to combine spaces and tabs.
2. The bit-endian format. We don't support big-endian architecture (what's left after POWER moved to support little endian ?). And in any case, our software stack is so big that this minor change in the driver won't have any impact.

Thanks,
Oded

#4 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

In reply to Oded Gabbay
Quoted 124 lines · Oded Gabbay

This patch adds the habanalabs skeleton driver. The driver does nothing at this stage except very basic operations. It contains the minimal code to insmod and rmmod the driver and to create a /dev/hlX file per PCI device.

---
 drivers/misc/Kconfig                          |   1 +
 drivers/misc/Makefile                         |   1 +
 drivers/misc/habanalabs/Kconfig               |  22 ++
 drivers/misc/habanalabs/Makefile              |   7 +
 drivers/misc/habanalabs/device.c              | 331 ++++++++++++++++
 drivers/misc/habanalabs/habanalabs.h          | 149 +++++++
 drivers/misc/habanalabs/habanalabs_drv.c      | 366 ++++++++++++++++++
 .../habanalabs/include/habanalabs_device_if.h | 125 ++++++
 8 files changed, 1002 insertions(+)
 create mode 100644 drivers/misc/habanalabs/Kconfig
 create mode 100644 drivers/misc/habanalabs/Makefile
 create mode 100644 drivers/misc/habanalabs/device.c
 create mode 100644 drivers/misc/habanalabs/habanalabs.h
 create mode 100644 drivers/misc/habanalabs/habanalabs_drv.c
 create mode 100644 drivers/misc/habanalabs/include/habanalabs_device_if.h
drivers/misc/Kconfig +1 -0
index f417b06e11c5..fecab53c4f21 100644
@@ -535,4 +535,5 @@ source "drivers/misc/echo/Kconfig"
 source "drivers/misc/cxl/Kconfig"
 source "drivers/misc/ocxl/Kconfig"
 source "drivers/misc/cardreader/Kconfig"
+source "drivers/misc/habanalabs/Kconfig"
 endmenu
drivers/misc/Makefile +1 -0
index e39ccbbc1b3a..ae77dfd790a4 100644
@@ -59,3 +59,4 @@ obj-$(CONFIG_PCI_ENDPOINT_TEST)	+= pci_endpoint_test.o
 obj-$(CONFIG_OCXL)		+= ocxl/
 obj-y				+= cardreader/
 obj-$(CONFIG_PVPANIC)   	+= pvpanic.o
+obj-$(CONFIG_HABANA_AI)		+= habanalabs/
drivers/misc/habanalabs/Kconfig new file +22 -0
new file mode 100644
index 000000000000..b7f38a14caf5
@@ -0,0 +1,22 @@
+#
+# HabanaLabs AI accelerators driver
+#
+
+config HABANA_AI
+	tristate "HabanaAI accelerators (habanalabs)"
+	depends on PCI
+	select FRAME_VECTOR
+	help
+	  Enables PCIe card driver for Habana's AI Processors (AIP) that are
+	  designed to accelerate Deep Learning inference and training workloads.
+
+	  The driver manages the PCIe devices and provides IOCTL interface for
+	  the user to submit workloads to the devices.
+
+	  The user-space interface is described in
+	  include/uapi/misc/habanalabs.h
+
+	  If unsure, say N.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called habanalabs.
drivers/misc/habanalabs/Makefile new file +7 -0
new file mode 100644
index 000000000000..b41433a09e02
@@ -0,0 +1,7 @@
+#
+# Makefile for HabanaLabs AI accelerators driver
+#
+
+obj-m	:= habanalabs.o
+
+habanalabs-y := habanalabs_drv.o device.o
\ No newline at end of file
drivers/misc/habanalabs/device.c new file +35 -0
new file mode 100644
index 000000000000..376b55eb73d4
@@ -0,0 +1,331 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "habanalabs.h"
+
+#include <linux/fs.h>
+#include <linux/kthread.h>
+#include <linux/sched/signal.h>
+
+static void hpriv_release(struct kref *ref)
+{
+	struct hl_fpriv *hpriv;
+	struct hl_device *hdev;
+
+	hpriv = container_of(ref, struct hl_fpriv, refcount);
+
+	hdev = hpriv->hdev;
+
+	put_pid(hpriv->taskpid);
+
+	kfree(hpriv);
+}
+
+void hl_hpriv_get(struct hl_fpriv *hpriv)
+{
+	kref_get(&hpriv->refcount);
+}
+
+void hl_hpriv_put(struct hl_fpriv *hpriv)
+{
+	kref_put(&hpriv->refcount, hpriv_release);

drivers/misc/habanalabs/device.c line 45

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+}
+
+/**
+ * hl_device_release - release function for habanalabs device
+ *
+ * @inode: pointer to inode structure
+ * @filp: pointer to file structure
+ *
+ * Called when process closes an habanalabs device
+ */

It's nice to see docs coming along with the codei
I have some comments for the formatting.

kernel-doc won't be happy about missing return value descriptions, and although they are sometimes redundant or too obvious their absence makes 'make V=1 htmldocs' really noisy.

In general, it would be nice if you could link hanabnalabs driver kernel-doc somewhere in Documentation/ run 'make V=1 htmldocs'.

Quoted 78 lines
drivers/misc/habanalabs/device.c new file +78 -0
+static int hl_device_release(struct inode *inode, struct file *filp)
+{
+	struct hl_fpriv *hpriv = filp->private_data;
+
+	filp->private_data = NULL;
+
+	hl_hpriv_put(hpriv);
+
+	return 0;
+}
+
+static const struct file_operations hl_ops = {
+	.owner = THIS_MODULE,
+	.open = hl_device_open,
+	.release = hl_device_release
+};
+
+/**
+ * device_setup_cdev - setup cdev and device for habanalabs device
+ *
+ * @hdev: pointer to habanalabs device structure
+ * @hclass: pointer to the class object of the device
+ * @minor: minor number of the specific device
+ * @fpos : file operations to install for this device
+ *
+ * Create a cdev and a Linux device for habanalabs's device. Need to be
+ * called at the end of the habanalabs device initialization process,
+ * because this function exposes the device to the user
+ */
+static int device_setup_cdev(struct hl_device *hdev, struct class *hclass,
+				int minor, const struct file_operations *fops)
+{
+	int err, devno = MKDEV(hdev->major, minor);
+	struct cdev *hdev_cdev = &hdev->cdev;
+	char name[8];
+
+	sprintf(name, "hl%d", hdev->id);
+
+	cdev_init(hdev_cdev, fops);
+	hdev_cdev->owner = THIS_MODULE;
+	err = cdev_add(hdev_cdev, devno, 1);
+	if (err) {
+		pr_err("habanalabs: Failed to add char device %s", name);
+		goto err_cdev_add;
+	}
+
+	hdev->dev = device_create(hclass, NULL, devno, NULL, "%s", name);
+	if (IS_ERR(hdev->dev)) {
+		pr_err("habanalabs: Failed to create device %s\n", name);
+		err = PTR_ERR(hdev->dev);
+		goto err_device_create;
+	}
+
+	dev_set_drvdata(hdev->dev, hdev);
+
+	return 0;
+
+err_device_create:
+	cdev_del(hdev_cdev);
+err_cdev_add:
+	return err;
+}
+
+/**
+ * device_early_init - do some early initialization for the habanalabs device
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ * Install the relevant function pointers and call the early_init function,
+ * if such a function exists
+ */
+static int device_early_init(struct hl_device *hdev)
+{
+	switch (hdev->asic_type) {
+	case ASIC_GOYA:
+		sprintf(hdev->asic_name, "GOYA");
+		break;
+	default:

drivers/misc/habanalabs/device.c line 133

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+		dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
+			hdev->asic_type);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+/**
+ * device_early_fini - finalize all that was done in device_early_fini

^init

Quoted 5 lines
drivers/misc/habanalabs/device.c new file +5 -0
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ */
+static void device_early_fini(struct hl_device *hdev)

drivers/misc/habanalabs/device.c line 148

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+{
+}
+
+/**
+ * hl_device_suspend - initiate device suspend
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ * Puts the hw in the suspend state (all asics).
+ * Returns 0 for success or an error on failure.

Should be Return: or Returns: for kernel-doc to understand it.

drivers/misc/habanalabs/device.c line 149

Quoted 1 line
drivers/misc/habanalabs/device.c new file +1 -0
+ * Called at driver suspend.

This probably should be marked as Context:

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+ */
+int hl_device_suspend(struct hl_device *hdev)
+{
+	pci_save_state(hdev->pdev);
+
+	/* Shut down the device */
+	pci_disable_device(hdev->pdev);
+	pci_set_power_state(hdev->pdev, PCI_D3hot);
+
+	return 0;

drivers/misc/habanalabs/device.c line 169

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+}
+
+/**
+ * hl_device_resume - initiate device resume
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ * Bring the hw back to operating state (all asics).
+ * Returns 0 for success or an error on failure.
+ * Called at driver resume.

Same comments as for the previous functions.

Quoted 48 lines
drivers/misc/habanalabs/device.c new file +48 -0
+ */
+int hl_device_resume(struct hl_device *hdev)
+{
+	int rc;
+
+	pci_set_power_state(hdev->pdev, PCI_D0);
+	pci_restore_state(hdev->pdev);
+	rc = pci_enable_device(hdev->pdev);
+	if (rc) {
+		dev_err(hdev->dev,
+			"Failed to enable PCI device in resume\n");
+		return rc;
+	}
+
+	return 0;
+}
+
+/**
+ * hl_device_init - main initialization function for habanalabs device
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ * Allocate an id for the device, do early initialization and then call the
+ * ASIC specific initialization functions. Finally, create the cdev and the
+ * Linux device to expose it to the user
+ */
+int hl_device_init(struct hl_device *hdev, struct class *hclass)
+{
+	int rc;
+
+	/* Create device */
+	rc = device_setup_cdev(hdev, hclass, hdev->id, &hl_ops);
+
+	if (rc)
+		goto out_disabled;
+
+	/* Initialize ASIC function pointers and perform early init */
+	rc = device_early_init(hdev);
+	if (rc)
+		goto release_device;
+
+	dev_notice(hdev->dev,
+		"Successfully added device to habanalabs driver\n");
+
+	return 0;
+
+release_device:
+	device_destroy(hclass, hdev->dev->devt);

drivers/misc/habanalabs/device.c line 227

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+	cdev_del(&hdev->cdev);
+out_disabled:
+	hdev->disabled = true;
+	if (hdev->pdev)
+		dev_err(&hdev->pdev->dev,
+			"Failed to initialize hl%d. Device is NOT usable !!!\n",
+			hdev->id);
+	else
+		pr_err("habanalabs: Failed to initialize hl%d. Device is NOT usable !!!\n",
+			hdev->id);

Maybe three exclamation marks would be too much?

Quoted 130 lines
drivers/misc/habanalabs/device.c new file +104 -0
+
+	return rc;
+}
+
+/**
+ * hl_device_fini - main tear-down function for habanalabs device
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ * Destroy the device, call ASIC fini functions and release the id
+ */
+void hl_device_fini(struct hl_device *hdev)
+{
+	dev_info(hdev->dev, "Removing device\n");
+
+	/* Mark device as disabled */
+	hdev->disabled = true;
+
+	device_early_fini(hdev);
+
+	/* Hide device from user */
+	device_destroy(hdev->dev->class, hdev->dev->devt);
+	cdev_del(&hdev->cdev);
+
+	pr_info("habanalabs: removed device successfully\n");
+}
+
+/**
+ * hl_poll_timeout_memory - Periodically poll a host memory address
+ *                              until it is not zero or a timeout occurs
+ * @hdev: pointer to habanalabs device structure
+ * @addr: Address to poll
+ * @timeout_us: timeout in us
+ * @val: Variable to read the value into
+ *
+ * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
+ * case, the last read value at @addr is stored in @val. Must not
+ * be called from atomic context if sleep_us or timeout_us are used.
+ *
+ * The function sleeps for 100us with timeout value of
+ * timeout_us
+ */
+int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr,
+				u32 timeout_us, u32 *val)
+{
+	/*
+	 * pReturnVal is defined as volatile because it points to HOST memory,
+	 * which is being written to by the device. Therefore, we can't use
+	 * locks to synchronize it and it is not a memory-mapped register space
+	 */
+	volatile u32 *pReturnVal = (volatile u32 *) addr;
+	ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
+
+	might_sleep();
+
+	for (;;) {
+		*val = *pReturnVal;
+		if (*val)
+			break;
+		if (ktime_compare(ktime_get(), timeout) > 0) {
+			*val = *pReturnVal;
+			break;
+		}
+		usleep_range((100 >> 2) + 1, 100);
+	}
+
+	return (*val ? 0 : -ETIMEDOUT);
+}
+
+/**
+ * hl_poll_timeout_devicememory - Periodically poll a device memory address
+ *                                until it is not zero or a timeout occurs
+ * @hdev: pointer to habanalabs device structure
+ * @addr: Device address to poll
+ * @timeout_us: timeout in us
+ * @val: Variable to read the value into
+ *
+ * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
+ * case, the last read value at @addr is stored in @val. Must not
+ * be called from atomic context if sleep_us or timeout_us are used.
+ *
+ * The function sleeps for 100us with timeout value of
+ * timeout_us
+ */
+int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
+				u32 timeout_us, u32 *val)
+{
+	ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
+
+	might_sleep();
+
+	for (;;) {
+		*val = readl(addr);
+		if (*val)
+			break;
+		if (ktime_compare(ktime_get(), timeout) > 0) {
+			*val = readl(addr);
+			break;
+		}
+		usleep_range((100 >> 2) + 1, 100);
+	}
+
+	return (*val ? 0 : -ETIMEDOUT);
+}
drivers/misc/habanalabs/habanalabs.h new file +20 -0
new file mode 100644
index 000000000000..7e1b088b677c
@@ -0,0 +1,149 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ */
+
+#ifndef HABANALABSP_H_
+#define HABANALABSP_H_
+
+#include "include/habanalabs_device_if.h"
+
+#include <linux/pci.h>
+#include <linux/types.h>
+#include <linux/cdev.h>
+#include <linux/interrupt.h>
+#include <linux/iopoll.h>
+#include <linux/dma-fence.h>
+#include <linux/hashtable.h>
+#include <linux/hwmon.h>

drivers/misc/habanalabs/habanalabs.h line 30

Quoted 10 lines
drivers/misc/habanalabs/habanalabs.h new file +10 -0
+
+#define HL_NAME				"habanalabs"
+
+struct hl_device;
+
+
+
+
+
+

Too many blank lines, IMHO.

Quoted 167 lines
drivers/misc/habanalabs/habanalabs.h new file +119 -0
+/*
+ * ASICs
+ */
+
+/**
+ * enum hl_asic_type - supported ASIC types.
+ * @ASIC_AUTO_DETECT: ASIC type will be automatically set.
+ * @ASIC_GOYA: Goya device.
+ * @ASIC_LAST: last ASIC type.
+ */
+enum hl_asic_type {
+	ASIC_AUTO_DETECT,
+	ASIC_GOYA,
+	ASIC_LAST
+};
+
+
+
+
+
+/*
+ * FILE PRIVATE STRUCTURE
+ */
+
+/**
+ * struct hl_fpriv - process information stored in FD private data.
+ * @hdev: habanalabs device structure.
+ * @filp: pointer to the given file structure.
+ * @taskpid: current process ID.
+ * @refcount: number of related contexts.
+ */
+struct hl_fpriv {
+	struct hl_device	*hdev;
+	struct file		*filp;
+	struct pid		*taskpid;
+	struct kref		refcount;
+};
+
+
+
+
+/*
+ * DEVICES
+ */
+
+/* Theoretical limit only. A single host can only contain up to 4 or 8 PCIe
+ * x16 cards. In extereme cases, there are hosts that can accommodate 16 cards
+ */
+#define HL_MAX_MINORS	256
+
+/**
+ * struct hl_device - habanalabs device structure.
+ * @pdev: pointer to PCI device, can be NULL in case of simulator device.
+ * @cdev: related char device.
+ * @dev: realted kernel basic device structure.
+ * @asic_name: ASIC specific nmae.
+ * @asic_type: ASIC specific type.
+ * @major: habanalabs KMD major.
+ * @id: device minor.
+ * @disabled: is device disabled.
+ */
+struct hl_device {
+	struct pci_dev			*pdev;
+	struct cdev			cdev;
+	struct device			*dev;
+	char				asic_name[16];
+	enum hl_asic_type		asic_type;
+	u32				major;
+	u16				id;
+	u8				disabled;
+};
+
+/*
+ * IOCTLs
+ */
+
+/**
+ * typedef hl_ioctl_t - typedef for ioctl function in the driver
+ * @hpriv: pointer to the FD's private data, which contains state of
+ *		user process
+ * @data: pointer to the input/output arguments structure of the IOCTL
+ *
+ * Return: 0 for success, negative value for error
+ */
+typedef int hl_ioctl_t(struct hl_fpriv *hpriv, void *data);
+
+/**
+ * struct hl_ioctl_desc - describes an IOCTL entry of the driver.
+ * @cmd: the IOCTL code as created by the kernel macros.
+ * @func: pointer to the driver's function that should be called for this IOCTL.
+ */
+struct hl_ioctl_desc {
+	unsigned int cmd;
+	hl_ioctl_t *func;
+};
+
+
+
+
+
+/*
+ * Kernel module functions that can be accessed by entire module
+ */
+
+int hl_device_open(struct inode *inode, struct file *filp);
+int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
+		enum hl_asic_type asic_type, int minor);
+void destroy_hdev(struct hl_device *hdev);
+int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr, u32 timeout_us,
+				u32 *val);
+int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
+				u32 timeout_us, u32 *val);
+
+int hl_device_init(struct hl_device *hdev, struct class *hclass);
+void hl_device_fini(struct hl_device *hdev);
+int hl_device_suspend(struct hl_device *hdev);
+int hl_device_resume(struct hl_device *hdev);
+
+#endif /* HABANALABSP_H_ */
drivers/misc/habanalabs/habanalabs_drv.c new file +42 -0
new file mode 100644
index 000000000000..15217975327b
@@ -0,0 +1,366 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ * Author: Oded Gabbay <[email protected]>
+ *
+ */
+
+#include "habanalabs.h"
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/kthread.h>
+
+#include <linux/fs.h>
+
+#define HL_DRIVER_AUTHOR	"HabanaLabs Kernel Driver Team"
+
+#define HL_DRIVER_DESC		"Driver for HabanaLabs's AI Accelerators"
+
+MODULE_AUTHOR(HL_DRIVER_AUTHOR);
+MODULE_DESCRIPTION(HL_DRIVER_DESC);
+MODULE_LICENSE("GPL v2");
+
+static int hl_major;
+static struct class *hl_class;
+DEFINE_IDR(hl_devs_idr);
+DEFINE_MUTEX(hl_devs_idr_lock);
+
+#define PCI_VENDOR_ID_HABANALABS	0x1da3
+
+#define PCI_IDS_GOYA			0x0001
+
+static struct pci_device_id ids[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
+	{ 0, }
+};
+MODULE_DEVICE_TABLE(pci, ids);
+

drivers/misc/habanalabs/habanalabs_drv.c line 52

Quoted 10 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +10 -0
+/**
+ * get_asic_type - translate device id to asic type
+ *
+ * @device: id of the PCI device
+ * @asic_type: pointer that will be filled by the asic type
+ *
+ * Translate device id to asic type.
+ * In case of unidentified device, return -1
+ */
+static int get_asic_type(u16 device, enum hl_asic_type *asic_type)

This can simply return the hl_asic_type, see also a comment in create_hdev(().

Quoted 81 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +81 -0
+{
+	int rc = 0;
+
+	switch (device) {
+	case PCI_IDS_GOYA:
+		*asic_type = ASIC_GOYA;
+		break;
+	default:
+		*asic_type = rc = -1;
+		break;
+	}
+
+	return rc;
+}
+
+/**
+ * hl_device_open - open function for habanalabs device
+ *
+ * @inode: pointer to inode structure
+ * @filp: pointer to file structure
+ *
+ * Called when process opens an habanalabs device.
+ */
+int hl_device_open(struct inode *inode, struct file *filp)
+{
+	struct hl_device *hdev;
+	struct hl_fpriv *hpriv;
+
+	mutex_lock(&hl_devs_idr_lock);
+	hdev = idr_find(&hl_devs_idr, iminor(inode));
+	mutex_unlock(&hl_devs_idr_lock);
+
+	if (!hdev) {
+		pr_err("habanalabs: Couldn't find device %d:%d\n",
+			imajor(inode), iminor(inode));
+		return -ENXIO;
+	}
+
+	hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
+	if (!hpriv)
+		return -ENOMEM;
+
+	hpriv->hdev = hdev;
+	filp->private_data = hpriv;
+	hpriv->filp = filp;
+	kref_init(&hpriv->refcount);
+	nonseekable_open(inode, filp);
+
+	hpriv->taskpid = find_get_pid(current->pid);
+
+	return 0;
+}
+
+/**
+ * create_hdev - create habanalabs device instance
+ *
+ * @dev: will hold the pointer to the new habanalabs device structure
+ * @pdev: pointer to the pci device
+ * @asic_type: in case of simulator device, which device is it
+ * @minor: in case of simulator device, the minor of the device
+ *
+ * Allocate memory for habanalabs device and initialize basic fields
+ * Identify the ASIC type
+ * Allocate ID (minor) for the device (only for real devices)
+ */
+int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
+		enum hl_asic_type asic_type, int minor)
+{
+	struct hl_device *hdev;
+	int rc;
+
+	*dev = NULL;
+
+	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
+	if (!hdev) {
+		if (pdev)
+			dev_err(&pdev->dev,
+				"Not enough memory for habanalabs device\n");
+		else
+			pr_err("habanalabs: Not enough memory for  device\n");
+

drivers/misc/habanalabs/habanalabs_drv.c line 143

Quoted 10 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +10 -0
+		return -ENOMEM;
+	}
+
+	hdev->major = hl_major;
+
+	hdev->disabled = true;
+	hdev->pdev = pdev; /* can be NULL in case of simulator device */
+
+	if (asic_type == ASIC_AUTO_DETECT) {
+		rc = get_asic_type(pdev->device, &hdev->asic_type);

You can just make it

		&hdev->asic_type = get_asic_type(pdev->device);

drivers/misc/habanalabs/habanalabs_drv.c line 151

Quoted 8 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +8 -0
+		if (rc) {
+			dev_err(&pdev->dev, "Unsupported ASIC\n");
+			rc = -ENODEV;
+			goto free_hdev;
+		}
+	} else {
+		hdev->asic_type = asic_type;
+	}

In the current version create_hdev() is always called with ASIC_AUTO_DETECT, what are the usecases for other types?

drivers/misc/habanalabs/habanalabs_drv.c line 159

Quoted 8 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +8 -0
+
+	mutex_lock(&hl_devs_idr_lock);
+
+	if (minor == -1) {
+		rc = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
+				GFP_KERNEL);
+	} else {
+		idr_replace(&hl_devs_idr, hdev, minor);

idr_replace can fail, can't it?

Quoted 341 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +207 -0
+		rc = minor;
+	}
+
+	mutex_unlock(&hl_devs_idr_lock);
+
+	if (rc < 0) {
+		if (rc == -ENOSPC) {
+			pr_err("habanalabs: too many devices in the system\n");
+			rc = -EBUSY;
+		}
+		goto free_hdev;
+	}
+
+	hdev->id = rc;
+
+	*dev = hdev;
+
+	return 0;
+
+free_hdev:
+	kfree(hdev);
+	return rc;
+}
+
+/**
+ * destroy_hdev - destroy habanalabs device instance
+ *
+ * @dev: pointer to the habanalabs device structure
+ *
+ */
+void destroy_hdev(struct hl_device *hdev)
+{
+	/* Remove device from the device list */
+	mutex_lock(&hl_devs_idr_lock);
+	idr_remove(&hl_devs_idr, hdev->id);
+	mutex_unlock(&hl_devs_idr_lock);
+
+	kfree(hdev);
+}
+
+static int hl_pmops_suspend(struct device *dev)
+{
+	struct pci_dev *pdev = to_pci_dev(dev);
+	struct hl_device *hdev = pci_get_drvdata(pdev);
+
+	pr_debug("habanalabs: Going to suspend PCI device\n");
+
+	if (!hdev) {
+		pr_err("habanalabs: device pointer is NULL in suspend\n");
+		return 0;
+	}
+
+	return hl_device_suspend(hdev);
+}
+
+static int hl_pmops_resume(struct device *dev)
+{
+	struct pci_dev *pdev = to_pci_dev(dev);
+	struct hl_device *hdev = pci_get_drvdata(pdev);
+
+	pr_debug("habanalabs: Going to resume PCI device\n");
+
+	if (!hdev) {
+		pr_err("habanalabs: device pointer is NULL in resume\n");
+		return 0;
+	}
+
+	return hl_device_resume(hdev);
+}
+
+/**
+ * hl_pci_probe - probe PCI habanalabs devices
+ *
+ * @pdev: pointer to pci device
+ * @id: pointer to pci device id structure
+ *
+ * Standard PCI probe function for habanalabs device.
+ * Create a new habanalabs device and initialize it according to the
+ * device's type
+ */
+static int hl_pci_probe(struct pci_dev *pdev,
+				const struct pci_device_id *id)
+{
+	struct hl_device *hdev;
+	int rc;
+
+	dev_info(&pdev->dev, HL_NAME
+		 " device found [%04x:%04x] (rev %x)\n",
+		 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
+
+	rc = create_hdev(&hdev, pdev, ASIC_AUTO_DETECT, -1);
+	if (rc)
+		return rc;
+
+	pci_set_drvdata(pdev, hdev);
+
+	rc = hl_device_init(hdev, hl_class);
+	if (rc) {
+		dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
+		rc = -ENODEV;
+		goto disable_device;
+	}
+
+	return 0;
+
+disable_device:
+	pci_set_drvdata(pdev, NULL);
+	destroy_hdev(hdev);
+
+	return rc;
+}
+
+/**
+ * hl_pci_remove - remove PCI habanalabs devices
+ *
+ * @pdev: pointer to pci device
+ *
+ * Standard PCI remove function for habanalabs device
+ */
+static void hl_pci_remove(struct pci_dev *pdev)
+{
+	struct hl_device *hdev;
+
+	hdev = pci_get_drvdata(pdev);
+	if (!hdev)
+		return;
+
+	hl_device_fini(hdev);
+	pci_set_drvdata(pdev, NULL);
+
+	destroy_hdev(hdev);
+}
+
+static const struct dev_pm_ops hl_pm_ops = {
+	.suspend = hl_pmops_suspend,
+	.resume = hl_pmops_resume,
+};
+
+static struct pci_driver hl_pci_driver = {
+	.name = HL_NAME,
+	.id_table = ids,
+	.probe = hl_pci_probe,
+	.remove = hl_pci_remove,
+	.driver.pm = &hl_pm_ops,
+};
+
+/**
+ * hl_init - Initialize the habanalabs kernel driver
+ *
+ */
+static int __init hl_init(void)
+{
+	int rc;
+	dev_t dev;
+
+	pr_info("habanalabs: loading driver\n");
+
+	rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
+	if (rc < 0) {
+		pr_err("habanalabs: unable to get major\n");
+		return rc;
+	}
+
+	hl_major = MAJOR(dev);
+
+	hl_class = class_create(THIS_MODULE, HL_NAME);
+	if (IS_ERR(hl_class)) {
+		pr_err("habanalabs: failed to allocate class\n");
+		rc = PTR_ERR(hl_class);
+		goto remove_major;
+	}
+
+	rc = pci_register_driver(&hl_pci_driver);
+	if (rc) {
+		pr_err("habanalabs: failed to register pci device\n");
+		goto remove_class;
+	}
+
+	pr_debug("habanalabs: driver loaded\n");
+
+	return 0;
+
+remove_class:
+	class_destroy(hl_class);
+remove_major:
+	unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
+	return rc;
+}
+
+/**
+ * hl_exit - Release all resources of the habanalabs kernel driver
+ *
+ */
+static void __exit hl_exit(void)
+{
+	pci_unregister_driver(&hl_pci_driver);
+
+	class_destroy(hl_class);
+	unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
+
+	idr_destroy(&hl_devs_idr);
+
+	pr_debug("habanalabs: driver removed\n");
+}
+
+module_init(hl_init);
+module_exit(hl_exit);
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +125 -0
new file mode 100644
index 000000000000..9dbb7077eabd
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ */
+
+#ifndef HABANALABS_DEVICE_IF_H
+#define HABANALABS_DEVICE_IF_H
+
+#include <linux/types.h>
+
+/*
+ * PRIMARY QUEUE
+ */
+
+struct hl_bd {
+	__u64	ptr;
+	__u32	len;
+	union {
+		struct {
+			__u32	repeat:16;
+			__u32	res1:8;
+			__u32	repeat_valid:1;
+			__u32	res2:7;
+		};
+		__u32	ctl;
+	};
+};
+
+#define HL_BD_SIZE			sizeof(struct hl_bd)
+
+/*
+ * BD_CTL_REPEAT_VALID tells the CP whether the repeat field in the BD CTL is
+ * valid. 1 means the repeat field is valid, 0 means not-valid,
+ * i.e. repeat == 1
+ */
+#define BD_CTL_REPEAT_VALID_SHIFT	24
+#define BD_CTL_REPEAT_VALID_MASK	0x01000000
+
+#define BD_CTL_SHADOW_INDEX_SHIFT	0
+#define BD_CTL_SHADOW_INDEX_MASK	0x00000FFF
+
+/*
+ * COMPLETION QUEUE
+ */
+
+struct hl_cq_entry {
+	__u32	data;
+};
+
+#define HL_CQ_ENTRY_SIZE		sizeof(struct hl_cq_entry)
+
+#define CQ_ENTRY_READY_SHIFT			31
+#define CQ_ENTRY_READY_MASK			0x80000000
+
+#define CQ_ENTRY_SHADOW_INDEX_VALID_SHIFT	30
+#define CQ_ENTRY_SHADOW_INDEX_VALID_MASK	0x40000000
+
+#define CQ_ENTRY_SHADOW_INDEX_SHIFT		BD_CTL_SHADOW_INDEX_SHIFT
+#define CQ_ENTRY_SHADOW_INDEX_MASK		BD_CTL_SHADOW_INDEX_MASK
+
+/*
+ * EVENT QUEUE
+ */
+
+struct hl_eq_header {
+	__u32 reserved;
+	union {
+		struct {
+			__u32 ctx_id :10;
+			__u32:6;
+			__u32 opcode :10;
+			__u32:5;
+			__u32 ready :1;
+		};
+		__u32 ctl;
+	};
+};
+
+struct hl_eq_entry {
+	struct hl_eq_header hdr;
+	__u64 data[7];
+};
+
+#define HL_EQ_ENTRY_SIZE		sizeof(struct hl_eq_entry)
+
+#define EQ_CTL_READY_SHIFT		31
+#define EQ_CTL_READY_MASK		0x80000000
+
+#define EQ_CTL_EVENT_TYPE_SHIFT		16
+#define EQ_CTL_EVENT_TYPE_MASK		0x03FF0000
+
+enum pq_init_status {
+	PQ_INIT_STATUS_NA = 0,
+	PQ_INIT_STATUS_READY_FOR_CP,
+	PQ_INIT_STATUS_READY_FOR_HOST
+};
+
+/*
+ * ArmCP info
+ */
+
+#define VERSION_MAX_LEN			128
+#define ARMCP_MAX_SENSORS		128
+
+struct armcp_sensor {
+	__u32 type;
+	__u32 flags;
+};
+
+/* must be aligned to 4 bytes */
+struct armcp_info {
+	struct armcp_sensor sensors[ARMCP_MAX_SENSORS];
+	__u8 kernel_version[VERSION_MAX_LEN];
+	__u32 reserved[3];
+	__u32 cpld_version;
+	__u32 infineon_version;
+	__u8 fuse_version[VERSION_MAX_LEN];
+	__u8 thermal_version[VERSION_MAX_LEN];
+	__u8 armcp_version[VERSION_MAX_LEN];
+	__u64 dram_size;
+};
+
+#endif /* HABANALABS_DEVICE_IF_H */
Signature · 1 line
2.17.1
Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#5 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

Quoted 13 lines · Mike Rapoport
Quoted 8 lines · Oded Gabbay

+/**
+ * hl_device_release - release function for habanalabs device
+ *
+ * @inode: pointer to inode structure
+ * @filp: pointer to file structure
+ *
+ * Called when process closes an habanalabs device
+ */

It's nice to see docs coming along with the codei
I have some comments for the formatting.

Quoted 8 lines

kernel-doc won't be happy about missing return value descriptions, and although they are sometimes redundant or too obvious their absence makes 'make V=1 htmldocs' really noisy.

In general, it would be nice if you could link hanabnalabs driver kernel-doc somewhere in Documentation/ run 'make V=1 htmldocs'.

Quoted 1 line

+static int hl_device_release(struct inode *inode, struct file *filp)

There's no need for kerneldoc comments for static functions, as no one can call them and they are not part of any api.

So what would be better here is to just drop the /** line and use /*

thanks,

greg k-h

#6 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

Quoted 21 lines · Greg KH
Quoted 20 lines · Mike Rapoport
Quoted 8 lines · Oded Gabbay

+/**
+ * hl_device_release - release function for habanalabs device
+ *
+ * @inode: pointer to inode structure
+ * @filp: pointer to file structure
+ *
+ * Called when process closes an habanalabs device
+ */

It's nice to see docs coming along with the codei
I have some comments for the formatting.

kernel-doc won't be happy about missing return value descriptions, and although they are sometimes redundant or too obvious their absence makes 'make V=1 htmldocs' really noisy.

In general, it would be nice if you could link hanabnalabs driver kernel-doc somewhere in Documentation/ run 'make V=1 htmldocs'.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

+static int hl_device_release(struct inode *inode, struct file *filp)

There's no need for kerneldoc comments for static functions, as no one can call them and they are not part of any api.

So what would be better here is to just drop the /** line and use /*

Maybe it'd make sense to use /* for most of the comments in this driver as there are kernel-doc formatting issues in non-static functions as well, I was just too lazy to go over all of them.

Quoted 4 lines

thanks,

greg k-h

Signature · 2 lines
Sincerely yours,
Mike.
#7 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

Quoted 25 lines · Mike Rapoport
Quoted 23 lines · Greg KH
Quoted 21 lines · Mike Rapoport
Quoted 8 lines · Oded Gabbay

+/**
+ * hl_device_release - release function for habanalabs device
+ *
+ * @inode: pointer to inode structure
+ * @filp: pointer to file structure
+ *
+ * Called when process closes an habanalabs device
+ */

It's nice to see docs coming along with the codei
I have some comments for the formatting.

kernel-doc won't be happy about missing return value descriptions, and although they are sometimes redundant or too obvious their absence makes 'make V=1 htmldocs' really noisy.

In general, it would be nice if you could link hanabnalabs driver kernel-doc somewhere in Documentation/ run 'make V=1 htmldocs'.

Quoted 1 line

+static int hl_device_release(struct inode *inode, struct file *filp)

Quoted 9 lines
Quoted 4 lines

There's no need for kerneldoc comments for static functions, as no one can call them and they are not part of any api.

So what would be better here is to just drop the /** line and use /*

Maybe it'd make sense to use /* for most of the comments in this driver as there are kernel-doc formatting issues in non-static functions as well, I was just too lazy to go over all of them.

Hi Mike,
That's what I'm going to do for v2, except for the comments in habanalabs.h, which we made sure are written according to kernel-doc. I promise that we will go over the entire code and make everything kernel-doc compatible (I'm opening a jira ticket now :) ) but it may take a couple of weeks or more.

In any case, I fixed all of your other comments for this patch. Thanks,
Oded

Quoted 9 lines
Quoted 4 lines

thanks,

greg k-h

--
Sincerely yours,
Mike.

#8 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

In reply to Mike Rapoport
Quoted 772 lines · Mike Rapoport
Quoted 124 lines · Oded Gabbay

This patch adds the habanalabs skeleton driver. The driver does nothing at this stage except very basic operations. It contains the minimal code to insmod and rmmod the driver and to create a /dev/hlX file per PCI device.

---
 drivers/misc/Kconfig                          |   1 +
 drivers/misc/Makefile                         |   1 +
 drivers/misc/habanalabs/Kconfig               |  22 ++
 drivers/misc/habanalabs/Makefile              |   7 +
 drivers/misc/habanalabs/device.c              | 331 ++++++++++++++++
 drivers/misc/habanalabs/habanalabs.h          | 149 +++++++
 drivers/misc/habanalabs/habanalabs_drv.c      | 366 ++++++++++++++++++
 .../habanalabs/include/habanalabs_device_if.h | 125 ++++++
 8 files changed, 1002 insertions(+)
 create mode 100644 drivers/misc/habanalabs/Kconfig
 create mode 100644 drivers/misc/habanalabs/Makefile
 create mode 100644 drivers/misc/habanalabs/device.c
 create mode 100644 drivers/misc/habanalabs/habanalabs.h
 create mode 100644 drivers/misc/habanalabs/habanalabs_drv.c
 create mode 100644 drivers/misc/habanalabs/include/habanalabs_device_if.h
drivers/misc/Kconfig +1 -0
index f417b06e11c5..fecab53c4f21 100644
@@ -535,4 +535,5 @@ source "drivers/misc/echo/Kconfig"
 source "drivers/misc/cxl/Kconfig"
 source "drivers/misc/ocxl/Kconfig"
 source "drivers/misc/cardreader/Kconfig"
+source "drivers/misc/habanalabs/Kconfig"
 endmenu
drivers/misc/Makefile +1 -0
index e39ccbbc1b3a..ae77dfd790a4 100644
@@ -59,3 +59,4 @@ obj-$(CONFIG_PCI_ENDPOINT_TEST)     += pci_endpoint_test.o
 obj-$(CONFIG_OCXL)           += ocxl/
 obj-y                                += cardreader/
 obj-$(CONFIG_PVPANIC)        += pvpanic.o
+obj-$(CONFIG_HABANA_AI)              += habanalabs/
drivers/misc/habanalabs/Kconfig new file +22 -0
new file mode 100644
index 000000000000..b7f38a14caf5
@@ -0,0 +1,22 @@
+#
+# HabanaLabs AI accelerators driver
+#
+
+config HABANA_AI
+     tristate "HabanaAI accelerators (habanalabs)"
+     depends on PCI
+     select FRAME_VECTOR
+     help
+       Enables PCIe card driver for Habana's AI Processors (AIP) that are
+       designed to accelerate Deep Learning inference and training workloads.
+
+       The driver manages the PCIe devices and provides IOCTL interface for
+       the user to submit workloads to the devices.
+
+       The user-space interface is described in
+       include/uapi/misc/habanalabs.h
+
+       If unsure, say N.
+
+       To compile this driver as a module, choose M here: the
+       module will be called habanalabs.
drivers/misc/habanalabs/Makefile new file +7 -0
new file mode 100644
index 000000000000..b41433a09e02
@@ -0,0 +1,7 @@
+#
+# Makefile for HabanaLabs AI accelerators driver
+#
+
+obj-m        := habanalabs.o
+
+habanalabs-y := habanalabs_drv.o device.o
\ No newline at end of file
drivers/misc/habanalabs/device.c new file +35 -0
new file mode 100644
index 000000000000..376b55eb73d4
@@ -0,0 +1,331 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "habanalabs.h"
+
+#include <linux/fs.h>
+#include <linux/kthread.h>
+#include <linux/sched/signal.h>
+
+static void hpriv_release(struct kref *ref)
+{
+     struct hl_fpriv *hpriv;
+     struct hl_device *hdev;
+
+     hpriv = container_of(ref, struct hl_fpriv, refcount);
+
+     hdev = hpriv->hdev;
+
+     put_pid(hpriv->taskpid);
+
+     kfree(hpriv);
+}
+
+void hl_hpriv_get(struct hl_fpriv *hpriv)
+{
+     kref_get(&hpriv->refcount);
+}
+
+void hl_hpriv_put(struct hl_fpriv *hpriv)
+{
+     kref_put(&hpriv->refcount, hpriv_release);

drivers/misc/habanalabs/device.c line 45

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+}
+
+/**
+ * hl_device_release - release function for habanalabs device
+ *
+ * @inode: pointer to inode structure
+ * @filp: pointer to file structure
+ *
+ * Called when process closes an habanalabs device
+ */

It's nice to see docs coming along with the codei
I have some comments for the formatting.

kernel-doc won't be happy about missing return value descriptions, and although they are sometimes redundant or too obvious their absence makes 'make V=1 htmldocs' really noisy.

In general, it would be nice if you could link hanabnalabs driver kernel-doc somewhere in Documentation/ run 'make V=1 htmldocs'.

Quoted 78 lines
drivers/misc/habanalabs/device.c new file +78 -0
+static int hl_device_release(struct inode *inode, struct file *filp)
+{
+     struct hl_fpriv *hpriv = filp->private_data;
+
+     filp->private_data = NULL;
+
+     hl_hpriv_put(hpriv);
+
+     return 0;
+}
+
+static const struct file_operations hl_ops = {
+     .owner = THIS_MODULE,
+     .open = hl_device_open,
+     .release = hl_device_release
+};
+
+/**
+ * device_setup_cdev - setup cdev and device for habanalabs device
+ *
+ * @hdev: pointer to habanalabs device structure
+ * @hclass: pointer to the class object of the device
+ * @minor: minor number of the specific device
+ * @fpos : file operations to install for this device
+ *
+ * Create a cdev and a Linux device for habanalabs's device. Need to be
+ * called at the end of the habanalabs device initialization process,
+ * because this function exposes the device to the user
+ */
+static int device_setup_cdev(struct hl_device *hdev, struct class *hclass,
+                             int minor, const struct file_operations *fops)
+{
+     int err, devno = MKDEV(hdev->major, minor);
+     struct cdev *hdev_cdev = &hdev->cdev;
+     char name[8];
+
+     sprintf(name, "hl%d", hdev->id);
+
+     cdev_init(hdev_cdev, fops);
+     hdev_cdev->owner = THIS_MODULE;
+     err = cdev_add(hdev_cdev, devno, 1);
+     if (err) {
+             pr_err("habanalabs: Failed to add char device %s", name);
+             goto err_cdev_add;
+     }
+
+     hdev->dev = device_create(hclass, NULL, devno, NULL, "%s", name);
+     if (IS_ERR(hdev->dev)) {
+             pr_err("habanalabs: Failed to create device %s\n", name);
+             err = PTR_ERR(hdev->dev);
+             goto err_device_create;
+     }
+
+     dev_set_drvdata(hdev->dev, hdev);
+
+     return 0;
+
+err_device_create:
+     cdev_del(hdev_cdev);
+err_cdev_add:
+     return err;
+}
+
+/**
+ * device_early_init - do some early initialization for the habanalabs device
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ * Install the relevant function pointers and call the early_init function,
+ * if such a function exists
+ */
+static int device_early_init(struct hl_device *hdev)
+{
+     switch (hdev->asic_type) {
+     case ASIC_GOYA:
+             sprintf(hdev->asic_name, "GOYA");
+             break;
+     default:

drivers/misc/habanalabs/device.c line 133

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+             dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
+                     hdev->asic_type);
+             return -EINVAL;
+     }
+
+     return 0;
+}
+
+/**
+ * device_early_fini - finalize all that was done in device_early_fini

^init

Quoted 5 lines
drivers/misc/habanalabs/device.c new file +5 -0
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ */
+static void device_early_fini(struct hl_device *hdev)

drivers/misc/habanalabs/device.c line 148

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+{
+}
+
+/**
+ * hl_device_suspend - initiate device suspend
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ * Puts the hw in the suspend state (all asics).
+ * Returns 0 for success or an error on failure.

Should be Return: or Returns: for kernel-doc to understand it.

drivers/misc/habanalabs/device.c line 149

Quoted 1 line
drivers/misc/habanalabs/device.c new file +1 -0
+ * Called at driver suspend.

This probably should be marked as Context:

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+ */
+int hl_device_suspend(struct hl_device *hdev)
+{
+     pci_save_state(hdev->pdev);
+
+     /* Shut down the device */
+     pci_disable_device(hdev->pdev);
+     pci_set_power_state(hdev->pdev, PCI_D3hot);
+
+     return 0;

drivers/misc/habanalabs/device.c line 169

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+}
+
+/**
+ * hl_device_resume - initiate device resume
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ * Bring the hw back to operating state (all asics).
+ * Returns 0 for success or an error on failure.
+ * Called at driver resume.

Same comments as for the previous functions.

Quoted 48 lines
drivers/misc/habanalabs/device.c new file +48 -0
+ */
+int hl_device_resume(struct hl_device *hdev)
+{
+     int rc;
+
+     pci_set_power_state(hdev->pdev, PCI_D0);
+     pci_restore_state(hdev->pdev);
+     rc = pci_enable_device(hdev->pdev);
+     if (rc) {
+             dev_err(hdev->dev,
+                     "Failed to enable PCI device in resume\n");
+             return rc;
+     }
+
+     return 0;
+}
+
+/**
+ * hl_device_init - main initialization function for habanalabs device
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ * Allocate an id for the device, do early initialization and then call the
+ * ASIC specific initialization functions. Finally, create the cdev and the
+ * Linux device to expose it to the user
+ */
+int hl_device_init(struct hl_device *hdev, struct class *hclass)
+{
+     int rc;
+
+     /* Create device */
+     rc = device_setup_cdev(hdev, hclass, hdev->id, &hl_ops);
+
+     if (rc)
+             goto out_disabled;
+
+     /* Initialize ASIC function pointers and perform early init */
+     rc = device_early_init(hdev);
+     if (rc)
+             goto release_device;
+
+     dev_notice(hdev->dev,
+             "Successfully added device to habanalabs driver\n");
+
+     return 0;
+
+release_device:
+     device_destroy(hclass, hdev->dev->devt);

drivers/misc/habanalabs/device.c line 227

Quoted 10 lines
drivers/misc/habanalabs/device.c new file +10 -0
+     cdev_del(&hdev->cdev);
+out_disabled:
+     hdev->disabled = true;
+     if (hdev->pdev)
+             dev_err(&hdev->pdev->dev,
+                     "Failed to initialize hl%d. Device is NOT usable !!!\n",
+                     hdev->id);
+     else
+             pr_err("habanalabs: Failed to initialize hl%d. Device is NOT usable !!!\n",
+                     hdev->id);

Maybe three exclamation marks would be too much?

Quoted 130 lines
drivers/misc/habanalabs/device.c new file +104 -0
+
+     return rc;
+}
+
+/**
+ * hl_device_fini - main tear-down function for habanalabs device
+ *
+ * @hdev: pointer to habanalabs device structure
+ *
+ * Destroy the device, call ASIC fini functions and release the id
+ */
+void hl_device_fini(struct hl_device *hdev)
+{
+     dev_info(hdev->dev, "Removing device\n");
+
+     /* Mark device as disabled */
+     hdev->disabled = true;
+
+     device_early_fini(hdev);
+
+     /* Hide device from user */
+     device_destroy(hdev->dev->class, hdev->dev->devt);
+     cdev_del(&hdev->cdev);
+
+     pr_info("habanalabs: removed device successfully\n");
+}
+
+/**
+ * hl_poll_timeout_memory - Periodically poll a host memory address
+ *                              until it is not zero or a timeout occurs
+ * @hdev: pointer to habanalabs device structure
+ * @addr: Address to poll
+ * @timeout_us: timeout in us
+ * @val: Variable to read the value into
+ *
+ * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
+ * case, the last read value at @addr is stored in @val. Must not
+ * be called from atomic context if sleep_us or timeout_us are used.
+ *
+ * The function sleeps for 100us with timeout value of
+ * timeout_us
+ */
+int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr,
+                             u32 timeout_us, u32 *val)
+{
+     /*
+      * pReturnVal is defined as volatile because it points to HOST memory,
+      * which is being written to by the device. Therefore, we can't use
+      * locks to synchronize it and it is not a memory-mapped register space
+      */
+     volatile u32 *pReturnVal = (volatile u32 *) addr;
+     ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
+
+     might_sleep();
+
+     for (;;) {
+             *val = *pReturnVal;
+             if (*val)
+                     break;
+             if (ktime_compare(ktime_get(), timeout) > 0) {
+                     *val = *pReturnVal;
+                     break;
+             }
+             usleep_range((100 >> 2) + 1, 100);
+     }
+
+     return (*val ? 0 : -ETIMEDOUT);
+}
+
+/**
+ * hl_poll_timeout_devicememory - Periodically poll a device memory address
+ *                                until it is not zero or a timeout occurs
+ * @hdev: pointer to habanalabs device structure
+ * @addr: Device address to poll
+ * @timeout_us: timeout in us
+ * @val: Variable to read the value into
+ *
+ * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
+ * case, the last read value at @addr is stored in @val. Must not
+ * be called from atomic context if sleep_us or timeout_us are used.
+ *
+ * The function sleeps for 100us with timeout value of
+ * timeout_us
+ */
+int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
+                             u32 timeout_us, u32 *val)
+{
+     ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
+
+     might_sleep();
+
+     for (;;) {
+             *val = readl(addr);
+             if (*val)
+                     break;
+             if (ktime_compare(ktime_get(), timeout) > 0) {
+                     *val = readl(addr);
+                     break;
+             }
+             usleep_range((100 >> 2) + 1, 100);
+     }
+
+     return (*val ? 0 : -ETIMEDOUT);
+}
drivers/misc/habanalabs/habanalabs.h new file +20 -0
new file mode 100644
index 000000000000..7e1b088b677c
@@ -0,0 +1,149 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ */
+
+#ifndef HABANALABSP_H_
+#define HABANALABSP_H_
+
+#include "include/habanalabs_device_if.h"
+
+#include <linux/pci.h>
+#include <linux/types.h>
+#include <linux/cdev.h>
+#include <linux/interrupt.h>
+#include <linux/iopoll.h>
+#include <linux/dma-fence.h>
+#include <linux/hashtable.h>
+#include <linux/hwmon.h>

drivers/misc/habanalabs/habanalabs.h line 30

Quoted 10 lines
drivers/misc/habanalabs/habanalabs.h new file +10 -0
+
+#define HL_NAME                              "habanalabs"
+
+struct hl_device;
+
+
+
+
+
+

Too many blank lines, IMHO.

Quoted 167 lines
drivers/misc/habanalabs/habanalabs.h new file +119 -0
+/*
+ * ASICs
+ */
+
+/**
+ * enum hl_asic_type - supported ASIC types.
+ * @ASIC_AUTO_DETECT: ASIC type will be automatically set.
+ * @ASIC_GOYA: Goya device.
+ * @ASIC_LAST: last ASIC type.
+ */
+enum hl_asic_type {
+     ASIC_AUTO_DETECT,
+     ASIC_GOYA,
+     ASIC_LAST
+};
+
+
+
+
+
+/*
+ * FILE PRIVATE STRUCTURE
+ */
+
+/**
+ * struct hl_fpriv - process information stored in FD private data.
+ * @hdev: habanalabs device structure.
+ * @filp: pointer to the given file structure.
+ * @taskpid: current process ID.
+ * @refcount: number of related contexts.
+ */
+struct hl_fpriv {
+     struct hl_device        *hdev;
+     struct file             *filp;
+     struct pid              *taskpid;
+     struct kref             refcount;
+};
+
+
+
+
+/*
+ * DEVICES
+ */
+
+/* Theoretical limit only. A single host can only contain up to 4 or 8 PCIe
+ * x16 cards. In extereme cases, there are hosts that can accommodate 16 cards
+ */
+#define HL_MAX_MINORS        256
+
+/**
+ * struct hl_device - habanalabs device structure.
+ * @pdev: pointer to PCI device, can be NULL in case of simulator device.
+ * @cdev: related char device.
+ * @dev: realted kernel basic device structure.
+ * @asic_name: ASIC specific nmae.
+ * @asic_type: ASIC specific type.
+ * @major: habanalabs KMD major.
+ * @id: device minor.
+ * @disabled: is device disabled.
+ */
+struct hl_device {
+     struct pci_dev                  *pdev;
+     struct cdev                     cdev;
+     struct device                   *dev;
+     char                            asic_name[16];
+     enum hl_asic_type               asic_type;
+     u32                             major;
+     u16                             id;
+     u8                              disabled;
+};
+
+/*
+ * IOCTLs
+ */
+
+/**
+ * typedef hl_ioctl_t - typedef for ioctl function in the driver
+ * @hpriv: pointer to the FD's private data, which contains state of
+ *           user process
+ * @data: pointer to the input/output arguments structure of the IOCTL
+ *
+ * Return: 0 for success, negative value for error
+ */
+typedef int hl_ioctl_t(struct hl_fpriv *hpriv, void *data);
+
+/**
+ * struct hl_ioctl_desc - describes an IOCTL entry of the driver.
+ * @cmd: the IOCTL code as created by the kernel macros.
+ * @func: pointer to the driver's function that should be called for this IOCTL.
+ */
+struct hl_ioctl_desc {
+     unsigned int cmd;
+     hl_ioctl_t *func;
+};
+
+
+
+
+
+/*
+ * Kernel module functions that can be accessed by entire module
+ */
+
+int hl_device_open(struct inode *inode, struct file *filp);
+int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
+             enum hl_asic_type asic_type, int minor);
+void destroy_hdev(struct hl_device *hdev);
+int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr, u32 timeout_us,
+                             u32 *val);
+int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
+                             u32 timeout_us, u32 *val);
+
+int hl_device_init(struct hl_device *hdev, struct class *hclass);
+void hl_device_fini(struct hl_device *hdev);
+int hl_device_suspend(struct hl_device *hdev);
+int hl_device_resume(struct hl_device *hdev);
+
+#endif /* HABANALABSP_H_ */
drivers/misc/habanalabs/habanalabs_drv.c new file +42 -0
new file mode 100644
index 000000000000..15217975327b
@@ -0,0 +1,366 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ * Author: Oded Gabbay <[email protected]>
+ *
+ */
+
+#include "habanalabs.h"
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/kthread.h>
+
+#include <linux/fs.h>
+
+#define HL_DRIVER_AUTHOR     "HabanaLabs Kernel Driver Team"
+
+#define HL_DRIVER_DESC               "Driver for HabanaLabs's AI Accelerators"
+
+MODULE_AUTHOR(HL_DRIVER_AUTHOR);
+MODULE_DESCRIPTION(HL_DRIVER_DESC);
+MODULE_LICENSE("GPL v2");
+
+static int hl_major;
+static struct class *hl_class;
+DEFINE_IDR(hl_devs_idr);
+DEFINE_MUTEX(hl_devs_idr_lock);
+
+#define PCI_VENDOR_ID_HABANALABS     0x1da3
+
+#define PCI_IDS_GOYA                 0x0001
+
+static struct pci_device_id ids[] = {
+     { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
+     { 0, }
+};
+MODULE_DEVICE_TABLE(pci, ids);
+

drivers/misc/habanalabs/habanalabs_drv.c line 52

Quoted 10 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +10 -0
+/**
+ * get_asic_type - translate device id to asic type
+ *
+ * @device: id of the PCI device
+ * @asic_type: pointer that will be filled by the asic type
+ *
+ * Translate device id to asic type.
+ * In case of unidentified device, return -1
+ */
+static int get_asic_type(u16 device, enum hl_asic_type *asic_type)

This can simply return the hl_asic_type, see also a comment in create_hdev(().

Quoted 81 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +81 -0
+{
+     int rc = 0;
+
+     switch (device) {
+     case PCI_IDS_GOYA:
+             *asic_type = ASIC_GOYA;
+             break;
+     default:
+             *asic_type = rc = -1;
+             break;
+     }
+
+     return rc;
+}
+
+/**
+ * hl_device_open - open function for habanalabs device
+ *
+ * @inode: pointer to inode structure
+ * @filp: pointer to file structure
+ *
+ * Called when process opens an habanalabs device.
+ */
+int hl_device_open(struct inode *inode, struct file *filp)
+{
+     struct hl_device *hdev;
+     struct hl_fpriv *hpriv;
+
+     mutex_lock(&hl_devs_idr_lock);
+     hdev = idr_find(&hl_devs_idr, iminor(inode));
+     mutex_unlock(&hl_devs_idr_lock);
+
+     if (!hdev) {
+             pr_err("habanalabs: Couldn't find device %d:%d\n",
+                     imajor(inode), iminor(inode));
+             return -ENXIO;
+     }
+
+     hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
+     if (!hpriv)
+             return -ENOMEM;
+
+     hpriv->hdev = hdev;
+     filp->private_data = hpriv;
+     hpriv->filp = filp;
+     kref_init(&hpriv->refcount);
+     nonseekable_open(inode, filp);
+
+     hpriv->taskpid = find_get_pid(current->pid);
+
+     return 0;
+}
+
+/**
+ * create_hdev - create habanalabs device instance
+ *
+ * @dev: will hold the pointer to the new habanalabs device structure
+ * @pdev: pointer to the pci device
+ * @asic_type: in case of simulator device, which device is it
+ * @minor: in case of simulator device, the minor of the device
+ *
+ * Allocate memory for habanalabs device and initialize basic fields
+ * Identify the ASIC type
+ * Allocate ID (minor) for the device (only for real devices)
+ */
+int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
+             enum hl_asic_type asic_type, int minor)
+{
+     struct hl_device *hdev;
+     int rc;
+
+     *dev = NULL;
+
+     hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
+     if (!hdev) {
+             if (pdev)
+                     dev_err(&pdev->dev,
+                             "Not enough memory for habanalabs device\n");
+             else
+                     pr_err("habanalabs: Not enough memory for  device\n");
+

drivers/misc/habanalabs/habanalabs_drv.c line 143

Quoted 10 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +10 -0
+             return -ENOMEM;
+     }
+
+     hdev->major = hl_major;
+
+     hdev->disabled = true;
+     hdev->pdev = pdev; /* can be NULL in case of simulator device */
+
+     if (asic_type == ASIC_AUTO_DETECT) {
+             rc = get_asic_type(pdev->device, &hdev->asic_type);

You can just make it

                &hdev->asic_type = get_asic_type(pdev->device);

drivers/misc/habanalabs/habanalabs_drv.c line 151

Quoted 8 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +8 -0
+             if (rc) {
+                     dev_err(&pdev->dev, "Unsupported ASIC\n");
+                     rc = -ENODEV;
+                     goto free_hdev;
+             }
+     } else {
+             hdev->asic_type = asic_type;
+     }
Quoted 3 lines

In the current version create_hdev() is always called with ASIC_AUTO_DETECT, what are the usecases for other types?

So I don't think I mentioned this, but we have a software simulator that we wrote for our ASICs.
To support that, you can load the driver in simulation mode. Most of the simulation code is in an asic-specific file (goya_simulator.c) which I don't intend to upstream because:
1. It does really nasty things to make the simulator work and those nasty things are totally un-upstreamable :)
2. We don't intend to open source the simulator, nor give it to customers, so there is no need to upstream that code.

Having said that, there are very few places in the common code, which I think all of them are in habanalabs_drv.c, that contain code which is for simulation mode.
One of those places is:
hdev->asic_type = asic_type;

Another place is the idr_replace below.

I hope that because we are talking about a couple of lines in the entire driver, and because by themselves they are totally valid, I could upstream them even if that path will never be taken. If even those few lines are problematic, I will remove them, but it will just make my life a bit harder.

Oded

Quoted 357 lines

drivers/misc/habanalabs/habanalabs_drv.c line 159

Quoted 8 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +8 -0
+
+     mutex_lock(&hl_devs_idr_lock);
+
+     if (minor == -1) {
+             rc = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
+                             GFP_KERNEL);
+     } else {
+             idr_replace(&hl_devs_idr, hdev, minor);

idr_replace can fail, can't it?

Quoted 339 lines
drivers/misc/habanalabs/habanalabs_drv.c new file +207 -0
+             rc = minor;
+     }
+
+     mutex_unlock(&hl_devs_idr_lock);
+
+     if (rc < 0) {
+             if (rc == -ENOSPC) {
+                     pr_err("habanalabs: too many devices in the system\n");
+                     rc = -EBUSY;
+             }
+             goto free_hdev;
+     }
+
+     hdev->id = rc;
+
+     *dev = hdev;
+
+     return 0;
+
+free_hdev:
+     kfree(hdev);
+     return rc;
+}
+
+/**
+ * destroy_hdev - destroy habanalabs device instance
+ *
+ * @dev: pointer to the habanalabs device structure
+ *
+ */
+void destroy_hdev(struct hl_device *hdev)
+{
+     /* Remove device from the device list */
+     mutex_lock(&hl_devs_idr_lock);
+     idr_remove(&hl_devs_idr, hdev->id);
+     mutex_unlock(&hl_devs_idr_lock);
+
+     kfree(hdev);
+}
+
+static int hl_pmops_suspend(struct device *dev)
+{
+     struct pci_dev *pdev = to_pci_dev(dev);
+     struct hl_device *hdev = pci_get_drvdata(pdev);
+
+     pr_debug("habanalabs: Going to suspend PCI device\n");
+
+     if (!hdev) {
+             pr_err("habanalabs: device pointer is NULL in suspend\n");
+             return 0;
+     }
+
+     return hl_device_suspend(hdev);
+}
+
+static int hl_pmops_resume(struct device *dev)
+{
+     struct pci_dev *pdev = to_pci_dev(dev);
+     struct hl_device *hdev = pci_get_drvdata(pdev);
+
+     pr_debug("habanalabs: Going to resume PCI device\n");
+
+     if (!hdev) {
+             pr_err("habanalabs: device pointer is NULL in resume\n");
+             return 0;
+     }
+
+     return hl_device_resume(hdev);
+}
+
+/**
+ * hl_pci_probe - probe PCI habanalabs devices
+ *
+ * @pdev: pointer to pci device
+ * @id: pointer to pci device id structure
+ *
+ * Standard PCI probe function for habanalabs device.
+ * Create a new habanalabs device and initialize it according to the
+ * device's type
+ */
+static int hl_pci_probe(struct pci_dev *pdev,
+                             const struct pci_device_id *id)
+{
+     struct hl_device *hdev;
+     int rc;
+
+     dev_info(&pdev->dev, HL_NAME
+              " device found [%04x:%04x] (rev %x)\n",
+              (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
+
+     rc = create_hdev(&hdev, pdev, ASIC_AUTO_DETECT, -1);
+     if (rc)
+             return rc;
+
+     pci_set_drvdata(pdev, hdev);
+
+     rc = hl_device_init(hdev, hl_class);
+     if (rc) {
+             dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
+             rc = -ENODEV;
+             goto disable_device;
+     }
+
+     return 0;
+
+disable_device:
+     pci_set_drvdata(pdev, NULL);
+     destroy_hdev(hdev);
+
+     return rc;
+}
+
+/**
+ * hl_pci_remove - remove PCI habanalabs devices
+ *
+ * @pdev: pointer to pci device
+ *
+ * Standard PCI remove function for habanalabs device
+ */
+static void hl_pci_remove(struct pci_dev *pdev)
+{
+     struct hl_device *hdev;
+
+     hdev = pci_get_drvdata(pdev);
+     if (!hdev)
+             return;
+
+     hl_device_fini(hdev);
+     pci_set_drvdata(pdev, NULL);
+
+     destroy_hdev(hdev);
+}
+
+static const struct dev_pm_ops hl_pm_ops = {
+     .suspend = hl_pmops_suspend,
+     .resume = hl_pmops_resume,
+};
+
+static struct pci_driver hl_pci_driver = {
+     .name = HL_NAME,
+     .id_table = ids,
+     .probe = hl_pci_probe,
+     .remove = hl_pci_remove,
+     .driver.pm = &hl_pm_ops,
+};
+
+/**
+ * hl_init - Initialize the habanalabs kernel driver
+ *
+ */
+static int __init hl_init(void)
+{
+     int rc;
+     dev_t dev;
+
+     pr_info("habanalabs: loading driver\n");
+
+     rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
+     if (rc < 0) {
+             pr_err("habanalabs: unable to get major\n");
+             return rc;
+     }
+
+     hl_major = MAJOR(dev);
+
+     hl_class = class_create(THIS_MODULE, HL_NAME);
+     if (IS_ERR(hl_class)) {
+             pr_err("habanalabs: failed to allocate class\n");
+             rc = PTR_ERR(hl_class);
+             goto remove_major;
+     }
+
+     rc = pci_register_driver(&hl_pci_driver);
+     if (rc) {
+             pr_err("habanalabs: failed to register pci device\n");
+             goto remove_class;
+     }
+
+     pr_debug("habanalabs: driver loaded\n");
+
+     return 0;
+
+remove_class:
+     class_destroy(hl_class);
+remove_major:
+     unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
+     return rc;
+}
+
+/**
+ * hl_exit - Release all resources of the habanalabs kernel driver
+ *
+ */
+static void __exit hl_exit(void)
+{
+     pci_unregister_driver(&hl_pci_driver);
+
+     class_destroy(hl_class);
+     unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
+
+     idr_destroy(&hl_devs_idr);
+
+     pr_debug("habanalabs: driver removed\n");
+}
+
+module_init(hl_init);
+module_exit(hl_exit);
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +125 -1
new file mode 100644
index 000000000000..9dbb7077eabd
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ */
+
+#ifndef HABANALABS_DEVICE_IF_H
+#define HABANALABS_DEVICE_IF_H
+
+#include <linux/types.h>
+
+/*
+ * PRIMARY QUEUE
+ */
+
+struct hl_bd {
+     __u64   ptr;
+     __u32   len;
+     union {
+             struct {
+                     __u32   repeat:16;
+                     __u32   res1:8;
+                     __u32   repeat_valid:1;
+                     __u32   res2:7;
+             };
+             __u32   ctl;
+     };
+};
+
+#define HL_BD_SIZE                   sizeof(struct hl_bd)
+
+/*
+ * BD_CTL_REPEAT_VALID tells the CP whether the repeat field in the BD CTL is
+ * valid. 1 means the repeat field is valid, 0 means not-valid,
+ * i.e. repeat == 1
+ */
+#define BD_CTL_REPEAT_VALID_SHIFT    24
+#define BD_CTL_REPEAT_VALID_MASK     0x01000000
+
+#define BD_CTL_SHADOW_INDEX_SHIFT    0
+#define BD_CTL_SHADOW_INDEX_MASK     0x00000FFF
+
+/*
+ * COMPLETION QUEUE
+ */
+
+struct hl_cq_entry {
+     __u32   data;
+};
+
+#define HL_CQ_ENTRY_SIZE             sizeof(struct hl_cq_entry)
+
+#define CQ_ENTRY_READY_SHIFT                 31
+#define CQ_ENTRY_READY_MASK                  0x80000000
+
+#define CQ_ENTRY_SHADOW_INDEX_VALID_SHIFT    30
+#define CQ_ENTRY_SHADOW_INDEX_VALID_MASK     0x40000000
+
+#define CQ_ENTRY_SHADOW_INDEX_SHIFT          BD_CTL_SHADOW_INDEX_SHIFT
+#define CQ_ENTRY_SHADOW_INDEX_MASK           BD_CTL_SHADOW_INDEX_MASK
+
+/*
+ * EVENT QUEUE
+ */
+
+struct hl_eq_header {
+     __u32 reserved;
+     union {
+             struct {
+                     __u32 ctx_id :10;
+                     __u32:6;
+                     __u32 opcode :10;
+                     __u32:5;
+                     __u32 ready :1;
+             };
+             __u32 ctl;
+     };
+};
+
+struct hl_eq_entry {
+     struct hl_eq_header hdr;
+     __u64 data[7];
+};
+
+#define HL_EQ_ENTRY_SIZE             sizeof(struct hl_eq_entry)
+
+#define EQ_CTL_READY_SHIFT           31
+#define EQ_CTL_READY_MASK            0x80000000
+
+#define EQ_CTL_EVENT_TYPE_SHIFT              16
+#define EQ_CTL_EVENT_TYPE_MASK               0x03FF0000
+
+enum pq_init_status {
+     PQ_INIT_STATUS_NA = 0,
+     PQ_INIT_STATUS_READY_FOR_CP,
+     PQ_INIT_STATUS_READY_FOR_HOST
+};
+
+/*
+ * ArmCP info
+ */
+
+#define VERSION_MAX_LEN                      128
+#define ARMCP_MAX_SENSORS            128
+
+struct armcp_sensor {
+     __u32 type;
+     __u32 flags;
+};
+
+/* must be aligned to 4 bytes */
+struct armcp_info {
+     struct armcp_sensor sensors[ARMCP_MAX_SENSORS];
+     __u8 kernel_version[VERSION_MAX_LEN];
+     __u32 reserved[3];
+     __u32 cpld_version;
+     __u32 infineon_version;
+     __u8 fuse_version[VERSION_MAX_LEN];
+     __u8 thermal_version[VERSION_MAX_LEN];
+     __u8 armcp_version[VERSION_MAX_LEN];
+     __u64 dram_size;
+};
+
+#endif /* HABANALABS_DEVICE_IF_H */
--
Quoted 2 lines

2.17.1

--
Sincerely yours,
Mike.

maintainer
#9 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

In reply to Oded Gabbay

drivers/misc/habanalabs/include/habanalabs_device_if.h

Quoted 5 lines · Oded Gabbay
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +0 -0
new file mode 100644
index 000000000000..9dbb7077eabd

Since this is a apparently a user space ABI, the file should be in include/uapi/linux/,
not in the driver directory.

drivers/misc/habanalabs/include/habanalabs_device_if.h line 123

Quoted 12 lines
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +12 -0
+/* must be aligned to 4 bytes */
+struct armcp_info {
+       struct armcp_sensor sensors[ARMCP_MAX_SENSORS];
+       __u8 kernel_version[VERSION_MAX_LEN];
+       __u32 reserved[3];
+       __u32 cpld_version;
+       __u32 infineon_version;
+       __u8 fuse_version[VERSION_MAX_LEN];
+       __u8 thermal_version[VERSION_MAX_LEN];
+       __u8 armcp_version[VERSION_MAX_LEN];
+       __u64 dram_size;
+};

The compiler will align this to 8 bytes on most architectures, and add another padding field before dram_size. Better remove the 'reserved' fields, or make them an even number.

Arnd

#10 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

Quoted 12 lines · Arnd Bergmann

drivers/misc/habanalabs/include/habanalabs_device_if.h

Quoted 5 lines · Oded Gabbay
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +0 -0
new file mode 100644
index 000000000000..9dbb7077eabd

Since this is a apparently a user space ABI, the file should be in include/uapi/linux/,
not in the driver directory.

This is not a user space ABI. This is the ABI between the driver and the F/W.

Quoted 17 lines

drivers/misc/habanalabs/include/habanalabs_device_if.h line 123

Quoted 13 lines
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +12 -0
+/* must be aligned to 4 bytes */
+struct armcp_info {
+       struct armcp_sensor sensors[ARMCP_MAX_SENSORS];
+       __u8 kernel_version[VERSION_MAX_LEN];
+       __u32 reserved[3];
+       __u32 cpld_version;
+       __u32 infineon_version;
+       __u8 fuse_version[VERSION_MAX_LEN];
+       __u8 thermal_version[VERSION_MAX_LEN];
+       __u8 armcp_version[VERSION_MAX_LEN];
+       __u64 dram_size;
+};

The compiler will align this to 8 bytes on most architectures, and add another padding field before dram_size. Better remove the 'reserved' fields, or make them an even number.

I can't do that, because those fields were once used by the F/W and if I will change the order here, or add/remove those fields then it will break compatibility with old F/W.

Thanks,
Oded

Quoted 2 lines

Arnd

maintainer
#11 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

Quoted 15 lines · Oded Gabbay
Quoted 12 lines · Arnd Bergmann

drivers/misc/habanalabs/include/habanalabs_device_if.h

Quoted 5 lines · Oded Gabbay
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +0 -0
new file mode 100644
index 000000000000..9dbb7077eabd

Since this is a apparently a user space ABI, the file should be in include/uapi/linux/,
not in the driver directory.

Quoted 1 line

This is not a user space ABI. This is the ABI between the driver and the F/W.

Ah, I see. In that case, you should get rid of all the bitfields and make the struct members all __le32/__le64/... to make it work on big-endian kernels.

Quoted 17 lines
Quoted 17 lines

drivers/misc/habanalabs/include/habanalabs_device_if.h line 123

Quoted 13 lines
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +12 -0
+/* must be aligned to 4 bytes */
+struct armcp_info {
+       struct armcp_sensor sensors[ARMCP_MAX_SENSORS];
+       __u8 kernel_version[VERSION_MAX_LEN];
+       __u32 reserved[3];
+       __u32 cpld_version;
+       __u32 infineon_version;
+       __u8 fuse_version[VERSION_MAX_LEN];
+       __u8 thermal_version[VERSION_MAX_LEN];
+       __u8 armcp_version[VERSION_MAX_LEN];
+       __u64 dram_size;
+};

The compiler will align this to 8 bytes on most architectures, and add another padding field before dram_size. Better remove the 'reserved' fields, or make them an even number.

Quoted 3 lines

I can't do that, because those fields were once used by the F/W and if I will change the order here, or add/remove those fields then it will break compatibility with old F/W.

Ok, I see. Then you should add an explicit padding field and fix the comment to make the structure match the actual interface.

Arnd

#12 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

Quoted 17 lines · Arnd Bergmann
Quoted 15 lines · Oded Gabbay
Quoted 12 lines · Arnd Bergmann

drivers/misc/habanalabs/include/habanalabs_device_if.h

Quoted 5 lines · Oded Gabbay
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +0 -0
new file mode 100644
index 000000000000..9dbb7077eabd

Since this is a apparently a user space ABI, the file should be in include/uapi/linux/,
not in the driver directory.

Quoted 5 lines
Quoted 1 line

This is not a user space ABI. This is the ABI between the driver and the F/W.

Ah, I see. In that case, you should get rid of all the bitfields and make the struct members all __le32/__le64/... to make it work on big-endian kernels.

I really don't want to start converting bitfields and structures to use __le32/64.
As I wrote in one of the previous reviews, we don't support big-endian architecture (what's left after POWER moved to support little endian ?). We actually do run on POWER9 but with ppc64le architecture In any case, our software stack is so big that this minor change in the driver won't have any impact on the overall ability to run something on our H/W

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

drivers/misc/habanalabs/include/habanalabs_device_if.h line 123

Quoted 13 lines
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +12 -0
+/* must be aligned to 4 bytes */
+struct armcp_info {
+       struct armcp_sensor sensors[ARMCP_MAX_SENSORS];
+       __u8 kernel_version[VERSION_MAX_LEN];
+       __u32 reserved[3];
+       __u32 cpld_version;
+       __u32 infineon_version;
+       __u8 fuse_version[VERSION_MAX_LEN];
+       __u8 thermal_version[VERSION_MAX_LEN];
+       __u8 armcp_version[VERSION_MAX_LEN];
+       __u64 dram_size;
+};

The compiler will align this to 8 bytes on most architectures, and add another padding field before dram_size. Better remove the 'reserved' fields, or make them an even number.

Quoted 8 lines
Quoted 3 lines

I can't do that, because those fields were once used by the F/W and if I will change the order here, or add/remove those fields then it will break compatibility with old F/W.

Ok, I see. Then you should add an explicit padding field and fix the comment to make the structure match the actual interface.

Arnd

Understood, will be fixed.
Thanks,
Oded

maintainer
#13 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

Quoted 23 lines · Oded Gabbay
Quoted 17 lines · Arnd Bergmann
Quoted 15 lines · Oded Gabbay
Quoted 12 lines · Arnd Bergmann

drivers/misc/habanalabs/include/habanalabs_device_if.h

Quoted 5 lines · Oded Gabbay
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +0 -0
new file mode 100644
index 000000000000..9dbb7077eabd

Since this is a apparently a user space ABI, the file should be in include/uapi/linux/,
not in the driver directory.

Quoted 5 lines
Quoted 1 line

This is not a user space ABI. This is the ABI between the driver and the F/W.

Ah, I see. In that case, you should get rid of all the bitfields and make the struct members all __le32/__le64/... to make it work on big-endian kernels.

Quoted 8 lines

I really don't want to start converting bitfields and structures to use __le32/64.
As I wrote in one of the previous reviews, we don't support big-endian architecture (what's left after POWER moved to support little endian ?). We actually do run on POWER9 but with ppc64le architecture In any case, our software stack is so big that this minor change in the driver won't have any impact on the overall ability to run something on our H/W

You don't have to do anything at the moment to "convert" to use a specific endian, but you do have to always mark such variables that are in a specific endian that this is the format they are expected in.

Then, when you run a tool like sparse, you will be notified if you happen to be making any assumptions that might not be correct about those variables, and it's trivial to usually fix it up at that time.

hope this helps,

greg k-h

#14 lore raw

Re: [PATCH 01/15] habanalabs: add skeleton driver

Quoted 38 lines · gregkh
Quoted 23 lines · Oded Gabbay
Quoted 17 lines · Arnd Bergmann
Quoted 15 lines · Oded Gabbay
Quoted 12 lines · Arnd Bergmann

drivers/misc/habanalabs/include/habanalabs_device_if.h

Quoted 5 lines · Oded Gabbay
drivers/misc/habanalabs/include/habanalabs_device_if.h new file +0 -0
new file mode 100644
index 000000000000..9dbb7077eabd

Since this is a apparently a user space ABI, the file should be in include/uapi/linux/,
not in the driver directory.

Quoted 5 lines
Quoted 1 line

This is not a user space ABI. This is the ABI between the driver and the F/W.

Ah, I see. In that case, you should get rid of all the bitfields and make the struct members all __le32/__le64/... to make it work on big-endian kernels.

Quoted 8 lines

I really don't want to start converting bitfields and structures to use __le32/64.
As I wrote in one of the previous reviews, we don't support big-endian architecture (what's left after POWER moved to support little endian ?). We actually do run on POWER9 but with ppc64le architecture In any case, our software stack is so big that this minor change in the driver won't have any impact on the overall ability to run something on our H/W

You don't have to do anything at the moment to "convert" to use a specific endian, but you do have to always mark such variables that are in a specific endian that this is the format they are expected in.

Quoted 7 lines

Then, when you run a tool like sparse, you will be notified if you happen to be making any assumptions that might not be correct about those variables, and it's trivial to usually fix it up at that time.

hope this helps,

greg k-h

ok, understood, fixed.
Thanks,
Oded

#15 lore raw

PATCH 3/15 habanalabs: add basic Goya support

In reply to Oded Gabbay

This patch adds a basic support for the Goya device. The code initializes the device's PCI controller and PCI bars. It also initializes various S/W structures and adds some basic helper functions.

---
 drivers/misc/habanalabs/Makefile            |   5 +-
 drivers/misc/habanalabs/device.c            |  71 +++
 drivers/misc/habanalabs/goya/Makefile       |   3 +
 drivers/misc/habanalabs/goya/goya.c         | 633 ++++++++++++++++++++
 drivers/misc/habanalabs/goya/goyaP.h        | 125 ++++
 drivers/misc/habanalabs/habanalabs.h        | 131 ++++
 drivers/misc/habanalabs/habanalabs_drv.c    |   3 +
 drivers/misc/habanalabs/include/goya/goya.h | 115 ++++
 8 files changed, 1085 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/habanalabs/goya/Makefile
 create mode 100644 drivers/misc/habanalabs/goya/goya.c
 create mode 100644 drivers/misc/habanalabs/goya/goyaP.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya.h
drivers/misc/habanalabs/Makefile +4 -1
index b41433a09e02..6f1ead69bd77 100644
@@ -4,4 +4,7 @@
 
 obj-m	:= habanalabs.o
 
-habanalabs-y := habanalabs_drv.o device.o
\ No newline at end of file
+habanalabs-y := habanalabs_drv.o device.o
+
+include $(src)/goya/Makefile
+habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/device.c +71 -0

125 lines not on this page · raw message

drivers/misc/habanalabs/goya/Makefile new file +3 -0
new file mode 100644
index 000000000000..5ebf3d0d5794
@@ -0,0 +1,3 @@
+subdir-ccflags-y += -I$(src)
+
+HL_GOYA_FILES :=  goya/goya.o
\ No newline at end of file
drivers/misc/habanalabs/goya/goya.c new file +633 -0

633 lines not on this page · raw message

drivers/misc/habanalabs/goya/goyaP.h new file +125 -0

125 lines not on this page · raw message

drivers/misc/habanalabs/habanalabs.h +131 -0

178 lines not on this page · raw message

drivers/misc/habanalabs/habanalabs_drv.c +3 -0
index 15217975327b..79545003b7c2 100644
@@ -136,6 +136,9 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
 
 	hdev->major = hl_major;
 
+	/* Parameters for bring-up - set them to defaults */
+	hdev->reset_pcilink = 0;
+
 	hdev->disabled = true;
 	hdev->pdev = pdev; /* can be NULL in case of simulator device */
 
drivers/misc/habanalabs/include/goya/goya.h new file +115 -0

115 lines not on this page · raw message

Signature · 1 line
2.17.1
#16 lore raw

Re: [PATCH 03/15] habanalabs: add basic Goya support

Quoted 313 lines · Oded Gabbay

This patch adds a basic support for the Goya device. The code initializes the device's PCI controller and PCI bars. It also initializes various S/W structures and adds some basic helper functions.

---
 drivers/misc/habanalabs/Makefile            |   5 +-
 drivers/misc/habanalabs/device.c            |  71 +++
 drivers/misc/habanalabs/goya/Makefile       |   3 +
 drivers/misc/habanalabs/goya/goya.c         | 633 ++++++++++++++++++++
 drivers/misc/habanalabs/goya/goyaP.h        | 125 ++++
 drivers/misc/habanalabs/habanalabs.h        | 131 ++++
 drivers/misc/habanalabs/habanalabs_drv.c    |   3 +
 drivers/misc/habanalabs/include/goya/goya.h | 115 ++++
 8 files changed, 1085 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/habanalabs/goya/Makefile
 create mode 100644 drivers/misc/habanalabs/goya/goya.c
 create mode 100644 drivers/misc/habanalabs/goya/goyaP.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya.h
drivers/misc/habanalabs/Makefile +4 -1
index b41433a09e02..6f1ead69bd77 100644
@@ -4,4 +4,7 @@
 
 obj-m	:= habanalabs.o
 
-habanalabs-y := habanalabs_drv.o device.o
\ No newline at end of file
+habanalabs-y := habanalabs_drv.o device.o
+
+include $(src)/goya/Makefile
+habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/device.c +71 -0
index 376b55eb73d4..a4276ef559b3 100644
@@ -116,8 +116,11 @@ static int device_setup_cdev(struct hl_device *hdev, struct class *hclass,
  */
 static int device_early_init(struct hl_device *hdev)
 {
+	int rc;
+
 	switch (hdev->asic_type) {
 	case ASIC_GOYA:
+		goya_set_asic_funcs(hdev);
 		sprintf(hdev->asic_name, "GOYA");
 		break;
 	default:
@@ -126,6 +129,10 @@ static int device_early_init(struct hl_device *hdev)
 		return -EINVAL;
 	}
 
+	rc = hdev->asic_funcs->early_init(hdev);
+	if (rc)
+		return rc;
+
 	return 0;
 }
 
@@ -137,6 +144,10 @@ static int device_early_init(struct hl_device *hdev)
  */
 static void device_early_fini(struct hl_device *hdev)
 {
+
+	if (hdev->asic_funcs->early_fini)
+		hdev->asic_funcs->early_fini(hdev);
+
 }
 
 /**
@@ -150,8 +161,15 @@ static void device_early_fini(struct hl_device *hdev)
  */
 int hl_device_suspend(struct hl_device *hdev)
 {
+	int rc;
+
 	pci_save_state(hdev->pdev);
 
+	rc = hdev->asic_funcs->suspend(hdev);
+	if (rc)
+		dev_err(hdev->dev,
+			"Failed to disable PCI access of device CPU\n");
+
 	/* Shut down the device */
 	pci_disable_device(hdev->pdev);
 	pci_set_power_state(hdev->pdev, PCI_D3hot);
@@ -181,6 +199,13 @@ int hl_device_resume(struct hl_device *hdev)
 		return rc;
 	}
 
+	rc = hdev->asic_funcs->resume(hdev);
+	if (rc) {
+		dev_err(hdev->dev,
+			"Failed to enable PCI access from device CPU\n");
+		return rc;
+	}
+
 	return 0;
 }
 
@@ -208,11 +233,21 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)
 	if (rc)
 		goto release_device;
 
+	/*
+	 * Start calling ASIC initialization. First S/W then H/W and finally
+	 * late init
+	 */
+	rc = hdev->asic_funcs->sw_init(hdev);
+	if (rc)
+		goto early_fini;
+
 	dev_notice(hdev->dev,
 		"Successfully added device to habanalabs driver\n");
 
 	return 0;
 
+early_fini:
+	device_early_fini(hdev);
 release_device:
 	device_destroy(hclass, hdev->dev->devt);
 	cdev_del(&hdev->cdev);
@@ -243,6 +278,9 @@ void hl_device_fini(struct hl_device *hdev)
 	/* Mark device as disabled */
 	hdev->disabled = true;
 
+	/* Call ASIC S/W finalize function */
+	hdev->asic_funcs->sw_fini(hdev);
+
 	device_early_fini(hdev);
 
 	/* Hide device from user */
@@ -329,3 +367,36 @@ int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
 
 	return (*val ? 0 : -ETIMEDOUT);
 }
+
+/*
+ * MMIO register access helper functions.
+ */
+
+/**
+ * hl_rreg - Read an MMIO register
+ *
+ * @hdev: pointer to habanalabs device structure
+ * @reg: MMIO register offset (in bytes)
+ *
+ * Returns the value of the MMIO register we are asked to read
+ *
+ */
+inline u32 hl_rreg(struct hl_device *hdev, u32 reg)
+{
+	return readl(hdev->rmmio + reg);
+}
+
+/**
+ * hl_wreg - Write to an MMIO register
+ *
+ * @hdev: pointer to habanalabs device structure
+ * @reg: MMIO register offset (in bytes)
+ * @val: 32-bit value
+ *
+ * Writes the 32-bit value into the MMIO register
+ *
+ */
+inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val)
+{
+	writel(val, hdev->rmmio + reg);
+}
drivers/misc/habanalabs/goya/Makefile new file +3 -0
new file mode 100644
index 000000000000..5ebf3d0d5794
@@ -0,0 +1,3 @@
+subdir-ccflags-y += -I$(src)
+
+HL_GOYA_FILES :=  goya/goya.o
\ No newline at end of file
drivers/misc/habanalabs/goya/goya.c new file +126 -0
new file mode 100644
index 000000000000..b2952296b890
@@ -0,0 +1,633 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "goyaP.h"
+#include "include/goya/asic_reg/goya_masks.h"
+
+#include <linux/fs.h>
+#include <linux/delay.h>
+#include <linux/vmalloc.h>
+#include <linux/sched.h>
+#include <linux/genalloc.h>
+#include <linux/sysfs.h>
+#include <linux/kfifo.h>
+#include <linux/dma-mapping.h>
+#include <linux/firmware.h>
+#include <linux/log2.h>
+#include <linux/hwmon.h>
+#include <linux/string.h>
+#include <linux/io.h>
+
+/*
+ * GOYA security scheme:
+ *
+ * 1. Host is protected by:
+ *        - Range registers (When MMU is enabled, DMA RR does NOT protect host)
+ *        - MMU
+ *
+ * 2. DRAM is protected by:
+ *        - Range registers (protect the first 512MB)
+ *        - MMU (isolation between users)
+ *
+ * 3. Configuration is protected by:
+ *        - Range registers
+ *        - Protection bits
+ *
+ * When MMU is disabled:
+ *
+ * QMAN DMA: PQ, CQ, CP, DMA are secured.
+ * PQ, CB and the data are on the host.
+ *
+ * QMAN TPC/MME:
+ * PQ, CQ and CP are not secured.
+ * PQ, CB and the data are on the SRAM/DRAM.
+ *
+ * Since QMAN DMA is secured, KMD is parsing the DMA CB:
+ *     - KMD checks DMA pointer
+ *     - WREG, MSG_PROT are not allowed.
+ *     - MSG_LONG/SHORT are allowed.
+ *
+ * A read/write transaction by the QMAN to a protected area will succeed if
+ * and only if the QMAN's CP is secured and MSG_PROT is used
+ *
+ *
+ * When MMU is enabled:
+ *
+ * QMAN DMA: PQ, CQ and CP are secured.
+ * MMU is set to bypass on the Secure props register of the QMAN.
+ * The reasons we don't enable MMU for PQ, CQ and CP are:
+ *     - PQ entry is in kernel address space and KMD doesn't map it.
+ *     - CP writes to MSIX register and to kernel address space (completion
+ *       queue).
+ *
+ * DMA is not secured but because CP is secured, KMD still needs to parse the
+ * CB, but doesn't need to check the DMA addresses.
+ *
+ * For QMAN DMA 0, DMA is also secured because only KMD uses this DMA and KMD
+ * doesn't map memory in MMU.
+ *
+ * QMAN TPC/MME: PQ, CQ and CP aren't secured (no change from MMU disabled mode)
+ *
+ * DMA RR does NOT protect host because DMA is not secured
+ *
+ */
+
+#define GOYA_MMU_REGS_NUM		61
+
+#define GOYA_DMA_POOL_BLK_SIZE		0x100		/* 256 bytes */
+
+#define GOYA_RESET_TIMEOUT_MSEC		500		/* 500ms */
+#define GOYA_PLDM_RESET_TIMEOUT_MSEC	20000		/* 20s */
+#define GOYA_RESET_WAIT_MSEC		1		/* 1ms */
+#define GOYA_CPU_RESET_WAIT_MSEC	100		/* 100ms */
+#define GOYA_PLDM_RESET_WAIT_MSEC	1000		/* 1s */
+#define GOYA_CPU_TIMEOUT_USEC		10000000	/* 10s */
+#define GOYA_TEST_QUEUE_WAIT_USEC	100000		/* 100ms */
+
+#define GOYA_QMAN0_FENCE_VAL		0xD169B243
+
+#define GOYA_MAX_INITIATORS		20
+
+static void goya_get_fixed_properties(struct hl_device *hdev)
+{
+	struct asic_fixed_properties *prop = &hdev->asic_prop;
+
+	prop->completion_queues_count = NUMBER_OF_CMPLT_QUEUES;
+
+	prop->dram_base_address = DRAM_PHYS_BASE;
+	prop->dram_size = DRAM_PHYS_DEFAULT_SIZE;
+	prop->dram_end_address = prop->dram_base_address + prop->dram_size;
+	prop->dram_user_base_address = DRAM_BASE_ADDR_USER;
+
+	prop->sram_base_address = SRAM_BASE_ADDR;
+	prop->sram_size = SRAM_SIZE;
+	prop->sram_end_address = prop->sram_base_address + prop->sram_size;
+	prop->sram_user_base_address = prop->sram_base_address +
+						SRAM_USER_BASE_OFFSET;
+
+	prop->host_phys_base_address = HOST_PHYS_BASE;
+	prop->va_space_host_start_address = VA_HOST_SPACE_START;
+	prop->va_space_host_end_address = VA_HOST_SPACE_END;
+	prop->va_space_dram_start_address = VA_DDR_SPACE_START;
+	prop->va_space_dram_end_address = VA_DDR_SPACE_END;
+	prop->cfg_size = CFG_SIZE;
+	prop->max_asid = MAX_ASID;
+	prop->tpc_enabled_mask = TPC_ENABLED_MASK;
+
+	prop->high_pll = PLL_HIGH_DEFAULT;
+}
+
+/**
+ * goya_pci_bars_map - Map PCI BARS of Goya device
+ *

drivers/misc/habanalabs/goya/goya.c line 136

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c new file +10 -0
+ * @hdev: pointer to hl_device structure
+ *
+ * Request PCI regions and map them to kernel virtual addresses.
+ * Returns 0 on success
+ *
+ */
+int goya_pci_bars_map(struct hl_device *hdev)
+{
+	struct pci_dev *pdev = hdev->pdev;
+	int rc;

You could just init rc= -ENODEV here and avoid the hassle below.

Quoted 93 lines
drivers/misc/habanalabs/goya/goya.c new file +93 -0
+
+	rc = pci_request_regions(pdev, HL_NAME);
+	if (rc) {
+		dev_err(hdev->dev, "Cannot obtain PCI resources\n");
+		return rc;
+	}
+
+	hdev->pcie_bar[SRAM_CFG_BAR_ID] =
+			pci_ioremap_bar(pdev, SRAM_CFG_BAR_ID);
+	if (!hdev->pcie_bar[SRAM_CFG_BAR_ID]) {
+		dev_err(hdev->dev, "pci_ioremap_bar failed for CFG\n");
+		rc = -ENODEV;
+		goto err_release_regions;
+	}
+
+	hdev->pcie_bar[MSIX_BAR_ID] = pci_ioremap_bar(pdev, MSIX_BAR_ID);
+	if (!hdev->pcie_bar[MSIX_BAR_ID]) {
+		dev_err(hdev->dev, "pci_ioremap_bar failed for MSIX\n");
+		rc = -ENODEV;
+		goto err_unmap_sram_cfg;
+	}
+
+	hdev->pcie_bar[DDR_BAR_ID] = pci_ioremap_wc_bar(pdev, DDR_BAR_ID);
+	if (!hdev->pcie_bar[DDR_BAR_ID]) {
+		dev_err(hdev->dev, "pci_ioremap_bar failed for DDR\n");
+		rc = -ENODEV;
+		goto err_unmap_msix;
+	}
+
+	hdev->rmmio = hdev->pcie_bar[SRAM_CFG_BAR_ID] +
+				(CFG_BASE - SRAM_BASE_ADDR);
+
+	return 0;
+
+err_unmap_msix:
+	iounmap(hdev->pcie_bar[MSIX_BAR_ID]);
+err_unmap_sram_cfg:
+	iounmap(hdev->pcie_bar[SRAM_CFG_BAR_ID]);
+err_release_regions:
+	pci_release_regions(pdev);
+
+	return rc;
+}
+
+/**
+ * goya_pci_bars_unmap - Unmap PCI BARS of Goya device
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Release all PCI BARS and unmap their virtual addresses
+ *
+ */
+static void goya_pci_bars_unmap(struct hl_device *hdev)
+{
+	struct pci_dev *pdev = hdev->pdev;
+
+	iounmap(hdev->pcie_bar[DDR_BAR_ID]);
+	iounmap(hdev->pcie_bar[MSIX_BAR_ID]);
+	iounmap(hdev->pcie_bar[SRAM_CFG_BAR_ID]);
+	pci_release_regions(pdev);
+}
+
+/**
+ * goya_elbi_write - Write through the ELBI interface
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * return 0 on success, -1 on failure
+ *
+ */
+static int goya_elbi_write(struct hl_device *hdev, u64 addr, u32 data)
+{
+	struct pci_dev *pdev = hdev->pdev;
+	ktime_t timeout;
+	u32 val;
+
+	/* Clear previous status */
+	pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, 0);
+
+	pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_ADDR, (u32) addr);
+	pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_DATA, data);
+	pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_CTRL,
+				PCI_CONFIG_ELBI_CTRL_WRITE);
+
+	timeout = ktime_add_ms(ktime_get(), 10);
+	for (;;) {
+		pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, &val);
+		if (val & PCI_CONFIG_ELBI_STS_MASK)
+			break;
+		if (ktime_compare(ktime_get(), timeout) > 0) {
+			pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS,
+						&val);
+			break;

drivers/misc/habanalabs/goya/goya.c line 239

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c new file +10 -0
+		}
+		usleep_range(300, 500);
+	}
+
+	if ((val & PCI_CONFIG_ELBI_STS_MASK) == PCI_CONFIG_ELBI_STS_DONE)
+		return 0;
+
+	if (val & PCI_CONFIG_ELBI_STS_ERR) {
+		dev_err(hdev->dev, "Error writing to ELBI\n");o
+		return -1;

Please change -1 to an error code, say -EIO...

Quoted 16 lines
drivers/misc/habanalabs/goya/goya.c new file +16 -0
+	}
+
+	if (!(val & PCI_CONFIG_ELBI_STS_MASK)) {
+		dev_err(hdev->dev, "ELBI write didn't finish in time\n");
+		return -1;
+	}
+
+	dev_err(hdev->dev, "ELBI write has undefined bits in status\n");
+	return -1;
+}
+
+/**
+ * goya_iatu_write - iatu write routine
+ *
+ * @hdev: pointer to hl_device structure
+ *

drivers/misc/habanalabs/goya/goya.c line 265

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c new file +10 -0
+ */
+static int goya_iatu_write(struct hl_device *hdev, u32 addr, u32 data)
+{
+	u32 dbi_offset;
+	int rc;
+
+	dbi_offset = addr & 0xFFF;
+
+	rc = goya_elbi_write(hdev, CFG_BASE + mmPCIE_AUX_DBI, 0x00300000);
+	rc |= goya_elbi_write(hdev, mmPCIE_DBI_BASE + dbi_offset, data);

hmm, error code in goya_elbi_write probably won't work... Any reason to try the second write if the first failed?

Quoted 39 lines
drivers/misc/habanalabs/goya/goya.c new file +39 -0
+
+	return rc;
+}
+
+void goya_reset_link_through_bridge(struct hl_device *hdev)
+{
+	struct pci_dev *pdev = hdev->pdev;
+	struct pci_dev *parent_port;
+	u16 val;
+
+	parent_port = pdev->bus->self;
+	pci_read_config_word(parent_port, PCI_BRIDGE_CONTROL, &val);
+	val |= PCI_BRIDGE_CTL_BUS_RESET;
+	pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
+	ssleep(1);
+
+	val &= ~(PCI_BRIDGE_CTL_BUS_RESET);
+	pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
+	ssleep(3);
+}
+
+/**
+ * goya_set_ddr_bar_base - set DDR bar to map specific device address
+ *
+ * @hdev: pointer to hl_device structure
+ * @addr: address in DDR. Must be aligned to DDR bar size
+ *
+ * This function configures the iATU so that the DDR bar will start at the
+ * specified addr.
+ *
+ */
+static int goya_set_ddr_bar_base(struct hl_device *hdev, u64 addr)
+{
+	struct goya_device *goya = hdev->asic_specific;
+	int rc;
+
+	if ((goya) && (goya->ddr_bar_cur_addr == addr))
+		return 0;
+

drivers/misc/habanalabs/goya/goya.c line 314

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c new file +10 -0
+	/* Inbound Region 1 - Bar 4 - Point to DDR */
+	rc = goya_iatu_write(hdev, 0x314, lower_32_bits(addr));
+	rc |= goya_iatu_write(hdev, 0x318, upper_32_bits(addr));
+	rc |= goya_iatu_write(hdev, 0x300, 0);
+	/* Enable + Bar match + match enable + Bar 4 */
+	rc |= goya_iatu_write(hdev, 0x304, 0xC0080400);
+
+	/* Return the DBI window to the default location */
+	rc |= goya_elbi_write(hdev, CFG_BASE + mmPCIE_AUX_DBI, 0);
+	rc |= goya_elbi_write(hdev, CFG_BASE + mmPCIE_AUX_DBI_32, 0);

And here as well.

drivers/misc/habanalabs/goya/goya.c line 319

Quoted 5 lines
drivers/misc/habanalabs/goya/goya.c new file +5 -0
+
+	if (rc) {
+		dev_err(hdev->dev, "failed to map DDR bar to 0x%08llx\n", addr);
+		return rc;
+	}

I believe that at least here you'd want to return an error code.

Quoted 38 lines
drivers/misc/habanalabs/goya/goya.c new file +38 -0
+
+	if (goya)
+		goya->ddr_bar_cur_addr = addr;
+
+	return 0;
+}
+
+/**
+ * goya_init_iatu - Initialize the iATU unit inside the PCI controller
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * This is needed in case the firmware doesn't initialize the iATU
+ *
+ */
+static int goya_init_iatu(struct hl_device *hdev)
+{
+	int rc;
+
+	/* Inbound Region 0 - Bar 0 - Point to SRAM_BASE_ADDR */
+	rc  = goya_iatu_write(hdev, 0x114, lower_32_bits(SRAM_BASE_ADDR));
+	rc |= goya_iatu_write(hdev, 0x118, upper_32_bits(SRAM_BASE_ADDR));
+	rc |= goya_iatu_write(hdev, 0x100, 0);
+	/* Enable + Bar match + match enable */
+	rc |= goya_iatu_write(hdev, 0x104, 0xC0080000);
+
+	/* Inbound Region 1 - Bar 4 - Point to DDR */
+	rc |= goya_set_ddr_bar_base(hdev, DRAM_PHYS_BASE);
+
+	/* Outbound Region 0 - Point to Host */
+	rc |= goya_iatu_write(hdev, 0x008, lower_32_bits(HOST_PHYS_BASE));
+	rc |= goya_iatu_write(hdev, 0x00C, upper_32_bits(HOST_PHYS_BASE));
+	rc |= goya_iatu_write(hdev, 0x010,
+		lower_32_bits(HOST_PHYS_BASE + HOST_PHYS_SIZE - 1));
+	rc |= goya_iatu_write(hdev, 0x014, 0);
+	rc |= goya_iatu_write(hdev, 0x018, 0);
+	rc |= goya_iatu_write(hdev, 0x020,
+		upper_32_bits(HOST_PHYS_BASE + HOST_PHYS_SIZE - 1));

drivers/misc/habanalabs/goya/goya.c line 367

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c new file +10 -0
+	/* Increase region size */
+	rc |= goya_iatu_write(hdev, 0x000, 0x00002000);
+	/* Enable */
+	rc |= goya_iatu_write(hdev, 0x004, 0x80000000);
+
+	/* Return the DBI window to the default location */
+	rc |= goya_elbi_write(hdev, CFG_BASE + mmPCIE_AUX_DBI, 0);
+	rc |= goya_elbi_write(hdev, CFG_BASE + mmPCIE_AUX_DBI_32, 0);
+
+	return rc;

Ditto

Quoted 124 lines
drivers/misc/habanalabs/goya/goya.c new file +124 -0
+}
+
+/**
+ * goya_early_init - GOYA early initialization code
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Verify PCI bars
+ * Set DMA masks
+ * PCI controller initialization
+ * Map PCI bars
+ *
+ */
+static int goya_early_init(struct hl_device *hdev)
+{
+	struct asic_fixed_properties *prop = &hdev->asic_prop;
+	struct pci_dev *pdev = hdev->pdev;
+	u32 val;
+	int rc;
+
+	goya_get_fixed_properties(hdev);
+
+	/* Check BAR sizes */
+	if (pci_resource_len(pdev, SRAM_CFG_BAR_ID) != CFG_BAR_SIZE) {
+		dev_err(hdev->dev,
+			"Not " HL_NAME "? BAR %d size %llu, expecting %llu\n",
+			SRAM_CFG_BAR_ID,
+			pci_resource_len(pdev, SRAM_CFG_BAR_ID),
+			CFG_BAR_SIZE);
+		return -ENODEV;
+	}
+
+	if (pci_resource_len(pdev, MSIX_BAR_ID) != MSIX_BAR_SIZE) {
+		dev_err(hdev->dev,
+			"Not " HL_NAME "? BAR %d size %llu, expecting %llu\n",
+			MSIX_BAR_ID, pci_resource_len(pdev, MSIX_BAR_ID),
+			MSIX_BAR_SIZE);
+		return -ENODEV;
+	}
+
+	prop->dram_pci_bar_size = pci_resource_len(pdev, DDR_BAR_ID);
+
+	/* set DMA mask for GOYA */
+	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(39));
+	if (rc) {
+		dev_warn(hdev->dev, "Unable to set pci dma mask to 39 bits\n");
+		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
+		if (rc) {
+			dev_err(hdev->dev,
+				"Unable to set pci dma mask to 32 bits\n");
+			return rc;
+		}
+	}
+
+	rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
+	if (rc) {
+		dev_warn(hdev->dev,
+			"Unable to set pci consistent dma mask to 39 bits\n");
+		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
+		if (rc) {
+			dev_err(hdev->dev,
+				"Unable to set pci consistent dma mask to 32 bits\n");
+			return rc;
+		}
+	}
+
+	if (hdev->reset_pcilink)
+		goya_reset_link_through_bridge(hdev);
+
+	rc = pci_enable_device_mem(pdev);
+	if (rc) {
+		dev_err(hdev->dev, "can't enable PCI device\n");
+		return rc;
+	}
+
+	pci_set_master(pdev);
+
+	rc = goya_init_iatu(hdev);
+	if (rc) {
+		dev_err(hdev->dev, "Failed to initialize iATU\n");
+		goto disable_device;
+	}
+
+	rc = goya_pci_bars_map(hdev);
+	if (rc) {
+		dev_err(hdev->dev, "Failed to initialize PCI BARS\n");
+		goto disable_device;
+	}
+
+	val = RREG32(mmPSOC_GLOBAL_CONF_BOOT_STRAP_PINS);
+	if (val & PSOC_GLOBAL_CONF_BOOT_STRAP_PINS_SRIOV_EN_MASK)
+		dev_warn(hdev->dev,
+			"PCI strap is not configured correctly, PCI bus errors may occur\n");
+
+	return 0;
+
+disable_device:
+	pci_clear_master(pdev);
+	pci_disable_device(pdev);
+
+	return rc;
+}
+
+/**
+ * goya_early_fini - GOYA early finalization code
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Unmap PCI bars
+ *
+ */
+int goya_early_fini(struct hl_device *hdev)
+{
+	goya_pci_bars_unmap(hdev);
+
+	pci_clear_master(hdev->pdev);
+	pci_disable_device(hdev->pdev);
+
+	return 0;
+}
+
+/**
+ * goya_sw_init - Goya software initialization code
+ *

drivers/misc/habanalabs/goya/goya.c line 501

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c new file +10 -0
+ * @hdev: pointer to hl_device structure
+ *
+ */
+static int goya_sw_init(struct hl_device *hdev)
+{
+	struct goya_device *goya;
+	int rc;
+
+	/* Allocate device structure */
+	goya = kzalloc(sizeof(*goya), GFP_KERNEL);

Consider using devm_k[mz]alloc() for memory allocations throughout the driver. I didn't check all the spots where it can be applicable.

Quoted 25 lines
drivers/misc/habanalabs/goya/goya.c new file +25 -0
+	if (!goya)
+		return -ENOMEM;
+
+	/* according to goya_init_iatu */
+	goya->ddr_bar_cur_addr = DRAM_PHYS_BASE;
+	hdev->asic_specific = goya;
+
+	/* Create DMA pool for small allocations */
+	hdev->dma_pool = dma_pool_create(dev_name(hdev->dev),
+			&hdev->pdev->dev, GOYA_DMA_POOL_BLK_SIZE, 8, 0);
+	if (!hdev->dma_pool) {
+		dev_err(hdev->dev, "failed to create DMA pool\n");
+		rc = -ENOMEM;
+		goto free_goya_device;
+	}
+
+	hdev->cpu_accessible_dma_mem =
+			hdev->asic_funcs->dma_alloc_coherent(hdev,
+					CPU_ACCESSIBLE_MEM_SIZE,
+					&hdev->cpu_accessible_dma_address,
+					GFP_KERNEL | __GFP_ZERO);
+
+	if (!hdev->cpu_accessible_dma_mem) {
+		dev_err(hdev->dev,
+			"failed to allocate %d of dma memory for CPU accessible memory space\n",

drivers/misc/habanalabs/goya/goya.c line 536

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c new file +10 -0
+			CPU_ACCESSIBLE_MEM_SIZE);
+		rc = -ENOMEM;
+		goto free_dma_pool;
+	}
+
+	hdev->cpu_accessible_dma_pool = gen_pool_create(CPU_PKT_SHIFT, -1);
+	if (!hdev->cpu_accessible_dma_pool) {
+		dev_err(hdev->dev,
+			"Failed to create CPU accessible DMA pool\n");
+		rc = -ENOMEM;

You could init rc = -ENOMEM at the beginning and save the duplication.

Quoted 75 lines
drivers/misc/habanalabs/goya/goya.c new file +75 -0
+		goto free_cpu_pq_dma_mem;
+	}
+
+	rc = gen_pool_add(hdev->cpu_accessible_dma_pool,
+				(u64) hdev->cpu_accessible_dma_mem,
+				CPU_ACCESSIBLE_MEM_SIZE, -1);
+	if (rc) {
+		dev_err(hdev->dev,
+			"Failed to add memory to CPU accessible DMA pool\n");
+		rc = -EFAULT;
+		goto free_cpu_pq_pool;
+	}
+
+	spin_lock_init(&goya->hw_queues_lock);
+
+	return 0;
+
+free_cpu_pq_pool:
+	gen_pool_destroy(hdev->cpu_accessible_dma_pool);
+free_cpu_pq_dma_mem:
+	hdev->asic_funcs->dma_free_coherent(hdev, CPU_ACCESSIBLE_MEM_SIZE,
+			hdev->cpu_accessible_dma_mem,
+			hdev->cpu_accessible_dma_address);
+free_dma_pool:
+	dma_pool_destroy(hdev->dma_pool);
+free_goya_device:
+	kfree(goya);
+
+	return rc;
+}
+
+/**
+ * goya_sw_fini - Goya software tear-down code
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+int goya_sw_fini(struct hl_device *hdev)
+{
+	struct goya_device *goya = hdev->asic_specific;
+
+	gen_pool_destroy(hdev->cpu_accessible_dma_pool);
+
+	hdev->asic_funcs->dma_free_coherent(hdev, CPU_ACCESSIBLE_MEM_SIZE,
+			hdev->cpu_accessible_dma_mem,
+			hdev->cpu_accessible_dma_address);
+
+	dma_pool_destroy(hdev->dma_pool);
+
+	kfree(goya);
+
+	return 0;
+}
+
+int goya_suspend(struct hl_device *hdev)
+{
+	return 0;
+}
+
+int goya_resume(struct hl_device *hdev)
+{
+	return 0;
+}
+
+void *goya_dma_alloc_coherent(struct hl_device *hdev, size_t size,
+					dma_addr_t *dma_handle, gfp_t flags)
+{
+	return dma_alloc_coherent(&hdev->pdev->dev, size, dma_handle, flags);
+}
+
+void goya_dma_free_coherent(struct hl_device *hdev, size_t size, void *cpu_addr,
+				dma_addr_t dma_handle)
+{
+	dma_free_coherent(&hdev->pdev->dev, size, cpu_addr, dma_handle);
+}

drivers/misc/habanalabs/goya/goya.c line 621

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c new file +10 -0
+
+static const struct hl_asic_funcs goya_funcs = {
+	.early_init = goya_early_init,
+	.early_fini = goya_early_fini,
+	.sw_init = goya_sw_init,
+	.sw_fini = goya_sw_fini,
+	.suspend = goya_suspend,
+	.resume = goya_resume,
+	.dma_alloc_coherent = goya_dma_alloc_coherent,
+	.dma_free_coherent = goya_dma_free_coherent,

Is there any additional functionality that is planned in goya or gaudi in these two functions?
It seems like they are not really needed, at least at the moment and for sure that don't need to be part of ASIC ops.

Quoted 467 lines
drivers/misc/habanalabs/goya/goya.c new file +12 -0
+};
+
+/**
+ * goya_set_asic_funcs - set Goya function pointers
+ *
+ * @*hdev: pointer to hl_device structure
+ *
+ */
+void goya_set_asic_funcs(struct hl_device *hdev)
+{
+	hdev->asic_funcs = &goya_funcs;
+}
drivers/misc/habanalabs/goya/goyaP.h new file +125 -0
new file mode 100644
index 000000000000..0e12c56472bd
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ */
+
+#ifndef GOYAP_H_
+#define GOYAP_H_
+
+#include "habanalabs.h"
+#include "include/goya/goya.h"
+
+#define NUMBER_OF_CMPLT_QUEUES		5
+#define NUMBER_OF_EXT_HW_QUEUES		5
+#define NUMBER_OF_CPU_HW_QUEUES		1
+#define NUMBER_OF_INT_HW_QUEUES		9
+#define NUMBER_OF_HW_QUEUES		(NUMBER_OF_EXT_HW_QUEUES + \
+					NUMBER_OF_CPU_HW_QUEUES + \
+					NUMBER_OF_INT_HW_QUEUES)
+
+/*
+ * Number of MSIX interrupts IDS:
+ * Each completion queue has 1 ID
+ * The event queue has 1 ID
+ * ArmCP reset has 1 ID
+ */
+#define NUMBER_OF_INTERRUPTS		(NUMBER_OF_CMPLT_QUEUES + 2)
+
+#if (NUMBER_OF_HW_QUEUES >= HL_MAX_QUEUES)
+#error "Number of H/W queues must be smaller than HL_MAX_QUEUES"
+#endif
+
+#if (NUMBER_OF_INTERRUPTS > GOYA_MSIX_ENTRIES)
+#error "Number of MSIX interrupts must be smaller or equal to GOYA_MSIX_ENTRIES"
+#endif
+
+#define QMAN_FENCE_TIMEOUT_USEC		10000	/* 10 ms */
+
+#define QMAN_STOP_TIMEOUT_USEC		100000	/* 100 ms */
+
+#define TPC_MAX_NUM			8
+#define TPC_ENABLED_MASK		0xFF
+
+#define DMA_MAX_NUM			5
+
+#define PLL_HIGH_DEFAULT		1575000000	/* 1.575 GHz */
+
+#define GOYA_ARMCP_INFO_TIMEOUT		10000000	/* 10s */
+
+#define DRAM_PHYS_DEFAULT_SIZE		0x100000000ull	/* 4GB */
+
+/*
+ * SRAM Memory Map for KMD
+ *
+ * KMD occupies KMD_SRAM_SIZE bytes from the start of SRAM. It is used for
+ * MME/TPC QMANs
+ *
+ */
+
+#define MME_QMAN_BASE_OFFSET	0x000000	/* Must be 0 */
+#define MME_QMAN_LENGTH		64
+#define TPC_QMAN_LENGTH		64
+
+#define TPC0_QMAN_BASE_OFFSET	(MME_QMAN_BASE_OFFSET + \
+				(MME_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC1_QMAN_BASE_OFFSET	(TPC0_QMAN_BASE_OFFSET + \
+				(TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC2_QMAN_BASE_OFFSET	(TPC1_QMAN_BASE_OFFSET + \
+				(TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC3_QMAN_BASE_OFFSET	(TPC2_QMAN_BASE_OFFSET + \
+				(TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC4_QMAN_BASE_OFFSET	(TPC3_QMAN_BASE_OFFSET + \
+				(TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC5_QMAN_BASE_OFFSET	(TPC4_QMAN_BASE_OFFSET + \
+				(TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC6_QMAN_BASE_OFFSET	(TPC5_QMAN_BASE_OFFSET + \
+				(TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC7_QMAN_BASE_OFFSET	(TPC6_QMAN_BASE_OFFSET + \
+				(TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+
+#define SRAM_KMD_RES_OFFSET	(TPC7_QMAN_BASE_OFFSET + \
+				(TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+
+#if (SRAM_KMD_RES_OFFSET >= KMD_SRAM_RESERVED_SIZE)
+#error "MME/TPC QMANs SRAM space exceeds limit"
+#endif
+
+#define SRAM_USER_BASE_OFFSET	KMD_SRAM_RESERVED_SIZE
+
+#define DMA_MAX_TRANSFER_SIZE	0xFFFFFFFF
+
+#define HW_CAP_PLL		0x00000001
+#define HW_CAP_DDR_0		0x00000002
+#define HW_CAP_DDR_1		0x00000004
+#define HW_CAP_MME		0x00000008
+#define HW_CAP_CPU		0x00000010
+#define HW_CAP_DMA		0x00000020
+#define HW_CAP_MSIX		0x00000040
+#define HW_CAP_CPU_Q		0x00000080
+#define HW_CAP_MMU		0x00000100
+#define HW_CAP_TPC_MBIST	0x00000200
+#define HW_CAP_GOLDEN		0x00000400
+#define HW_CAP_TPC		0x00000800
+
+#define CPU_PKT_SHIFT		5
+#define CPU_PKT_SIZE		(1 << CPU_PKT_SHIFT)
+#define CPU_PKT_MASK		(~((1 << CPU_PKT_SHIFT) - 1))
+#define CPU_MAX_PKTS_IN_CB	32
+#define CPU_CB_SIZE		(CPU_PKT_SIZE * CPU_MAX_PKTS_IN_CB)
+#define CPU_ACCESSIBLE_MEM_SIZE	(HL_QUEUE_LENGTH * CPU_CB_SIZE)
+
+enum goya_fw_component {
+	FW_COMP_UBOOT,
+	FW_COMP_PREBOOT
+};
+
+struct goya_device {
+	/* TODO: remove hw_queues_lock after moving to scheduler code */
+	spinlock_t	hw_queues_lock;
+	u64		ddr_bar_cur_addr;
+	u32		hw_cap_initialized;
+};
+
+#endif /* GOYAP_H_ */
drivers/misc/habanalabs/habanalabs.h +131 -0
index 7e1b088b677c..97844825f7a8 100644
@@ -21,11 +21,64 @@
 
 #define HL_NAME				"habanalabs"
 
+#define HL_MAX_QUEUES			128
+
 struct hl_device;
 
 
 
 
+/**
+ * struct asic_fixed_properties - ASIC specific immutable properties.
+ * @sram_base_address: SRAM physical start address.
+ * @sram_end_address: SRAM physical end address.
+ * @sram_user_base_address - SRAM physical start address for user access.
+ * @dram_base_address: DRAM physical start address.
+ * @dram_end_address: DRAM physical end address.
+ * @dram_user_base_address: DRAM physical start address for user access.
+ * @dram_size: DRAM total size.
+ * @dram_pci_bar_size: size of PCI bar towards DRAM.
+ * @host_phys_base_address: base physical address of host memory for
+ *				transactions that the device generates.
+ * @va_space_host_start_address: base address of virtual memory range for
+ *                               mapping host memory.
+ * @va_space_host_end_address: end address of virtual memory range for
+ *                             mapping host memory.
+ * @va_space_dram_start_address: base address of virtual memory range for
+ *                               mapping DRAM memory.
+ * @va_space_dram_end_address: end address of virtual memory range for
+ *                             mapping DRAM memory.
+ * @cfg_size: configuration space size on SRAM.
+ * @sram_size: total size of SRAM.
+ * @max_asid: maximum number of open contexts (ASIDs).
+ * @completion_queues_count: number of completion queues.
+ * @high_pll: high PLL frequency used by the device.
+ * @tpc_enabled_mask: which TPCs are enabled.
+ */
+struct asic_fixed_properties {
+	u64			sram_base_address;
+	u64			sram_end_address;
+	u64			sram_user_base_address;
+	u64			dram_base_address;
+	u64			dram_end_address;
+	u64			dram_user_base_address;
+	u64			dram_size;
+	u64			dram_pci_bar_size;
+	u64			host_phys_base_address;
+	u64			va_space_host_start_address;
+	u64			va_space_host_end_address;
+	u64			va_space_dram_start_address;
+	u64			va_space_dram_end_address;
+	u32			cfg_size;
+	u32			sram_size;
+	u32			max_asid;
+	u32			high_pll;
+	u8			completion_queues_count;
+	u8			tpc_enabled_mask;
+};
+
+
+#define HL_QUEUE_LENGTH			256
 
 
 /*
@@ -47,6 +100,30 @@ enum hl_asic_type {
 
 
 
+/**
+ * struct hl_asic_funcs - ASIC specific functions that are can be called from
+ *                        common code.
+ * @early_init: sets up early driver state (pre sw_init), doesn't configure H/W.
+ * @early_fini: tears down what was done in early_init.
+ * @sw_init: sets up driver state, does not configure H/W.
+ * @sw_fini: tears down driver state, does not configure H/W.
+ * @suspend: handles IP specific H/W or SW changes for suspend.
+ * @resume: handles IP specific H/W or SW changes for resume.
+ * @dma_alloc_coherent: DMA allocate coherent memory.
+ * @dma_free_coherent: free DMA allocation.
+ */
+struct hl_asic_funcs {
+	int (*early_init)(struct hl_device *hdev);
+	int (*early_fini)(struct hl_device *hdev);
+	int (*sw_init)(struct hl_device *hdev);
+	int (*sw_fini)(struct hl_device *hdev);
+	int (*suspend)(struct hl_device *hdev);
+	int (*resume)(struct hl_device *hdev);
+	void* (*dma_alloc_coherent)(struct hl_device *hdev, size_t size,
+					dma_addr_t *dma_handle, gfp_t flag);
+	void (*dma_free_coherent)(struct hl_device *hdev, size_t size,
+					void *cpu_addr, dma_addr_t dma_handle);
+};
 
 /*
  * FILE PRIVATE STRUCTURE
@@ -78,26 +155,78 @@ struct hl_fpriv {
  */
 #define HL_MAX_MINORS	256
 
+/*
+ * Registers read & write functions.
+ */
+
+u32 hl_rreg(struct hl_device *hdev, u32 reg);
+void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
+
+#define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
+	readl_poll_timeout(hdev->rmmio + addr, val, cond, sleep_us, timeout_us)
+
+#define RREG32(reg) hl_rreg(hdev, (reg))
+#define WREG32(reg, v) hl_wreg(hdev, (reg), (v))
+#define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n",	\
+				hl_rreg(hdev, (reg)))
+
+#define WREG32_P(reg, val, mask)				\
+	do {							\
+		u32 tmp_ = RREG32(reg);				\
+		tmp_ &= (mask);					\
+		tmp_ |= ((val) & ~(mask));			\
+		WREG32(reg, tmp_);				\
+	} while (0)
+#define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
+#define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))
+
+#define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
+#define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
+#define WREG32_FIELD(reg, field, val)	\
+	WREG32(mm##reg, (RREG32(mm##reg) & ~REG_FIELD_MASK(reg, field)) | \
+			(val) << REG_FIELD_SHIFT(reg, field))
+
 /**
  * struct hl_device - habanalabs device structure.
  * @pdev: pointer to PCI device, can be NULL in case of simulator device.
+ * @pcie_bar: array of available PCIe bars.
+ * @rmmio: configuration area address on SRAM.
  * @cdev: related char device.
  * @dev: realted kernel basic device structure.
  * @asic_name: ASIC specific nmae.
  * @asic_type: ASIC specific type.
+ * @dma_pool: DMA pool for small allocations.
+ * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address.
+ * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address.
+ * @cpu_accessible_dma_pool: KMD <-> ArmCP shared memory pool.
+ * @asic_prop: ASIC specific immutable properties.
+ * @asic_funcs: ASIC specific functions.
+ * @asic_specific: ASIC specific information to use only from ASIC files.
  * @major: habanalabs KMD major.
  * @id: device minor.
  * @disabled: is device disabled.
  */
 struct hl_device {
 	struct pci_dev			*pdev;
+	void __iomem			*pcie_bar[6];
+	void __iomem			*rmmio;
 	struct cdev			cdev;
 	struct device			*dev;
 	char				asic_name[16];
 	enum hl_asic_type		asic_type;
+	struct dma_pool			*dma_pool;
+	void				*cpu_accessible_dma_mem;
+	dma_addr_t			cpu_accessible_dma_address;
+	struct gen_pool			*cpu_accessible_dma_pool;
+	struct asic_fixed_properties	asic_prop;
+	const struct hl_asic_funcs	*asic_funcs;
+	void				*asic_specific;
 	u32				major;
 	u16				id;
 	u8				disabled;
+
+	/* Parameters for bring-up */
+	u8				reset_pcilink;
 };
 
 /*
@@ -146,4 +275,6 @@ void hl_device_fini(struct hl_device *hdev);
 int hl_device_suspend(struct hl_device *hdev);
 int hl_device_resume(struct hl_device *hdev);
 
+void goya_set_asic_funcs(struct hl_device *hdev);
+
 #endif /* HABANALABSP_H_ */
drivers/misc/habanalabs/habanalabs_drv.c +3 -0
index 15217975327b..79545003b7c2 100644
@@ -136,6 +136,9 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
 
 	hdev->major = hl_major;
 
+	/* Parameters for bring-up - set them to defaults */
+	hdev->reset_pcilink = 0;
+
 	hdev->disabled = true;
 	hdev->pdev = pdev; /* can be NULL in case of simulator device */
 
drivers/misc/habanalabs/include/goya/goya.h new file +115 -0
new file mode 100644
index 000000000000..192a1450cbb1
@@ -0,0 +1,115 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ * Author: Oded Gabbay <[email protected]>
+ *
+ */
+
+#ifndef GOYA_H
+#define GOYA_H
+
+#include "asic_reg/goya_regs.h"
+
+#include <linux/types.h>
+
+#define SRAM_CFG_BAR_ID		0
+#define MSIX_BAR_ID		2
+#define DDR_BAR_ID		4
+
+#define CFG_BAR_SIZE		0x10000000ull		/* 256MB */
+#define MSIX_BAR_SIZE		0x1000ull		/* 4KB */
+
+#define CFG_BASE		0x7FFC000000ull
+#define CFG_SIZE		0x4000000		/* 32MB CFG + 32MB DBG*/
+
+#define SRAM_BASE_ADDR		0x7FF0000000ull
+#define SRAM_SIZE		0x32A0000		/* 50.625MB */
+#define KMD_SRAM_RESERVED_SIZE	0x8000			/* 32KB */
+
+#define SRAM_BASE_ADDR_USER	(0x7FF0000000ull + KMD_SRAM_RESERVED_SIZE)
+#define SRAM_SIZE_USER		(SRAM_SIZE - KMD_SRAM_RESERVED_SIZE)
+
+#define DRAM_PHYS_BASE		0x0ull
+
+#define CPU_FW_IMAGE_SIZE	0x10000000	/* 256MB */
+#define MMU_PAGE_TABLES_SIZE	0x0E000000	/* 224MB */
+#define CPU_PQ_PKT_SIZE		0x00001000	/* 4KB */
+#define CPU_PQ_DATA_SIZE	0x01FFF000	/* 32MB - 4KB  */
+
+#define CPU_FW_IMAGE_ADDR	DRAM_PHYS_BASE
+#define MMU_PAGE_TABLES_ADDR	(CPU_FW_IMAGE_ADDR + CPU_FW_IMAGE_SIZE)
+#define CPU_PQ_PKT_ADDR		(MMU_PAGE_TABLES_ADDR + MMU_PAGE_TABLES_SIZE)
+#define CPU_PQ_DATA_ADDR	(CPU_PQ_PKT_ADDR + CPU_PQ_PKT_SIZE)
+#define DRAM_BASE_ADDR_USER	(CPU_PQ_DATA_ADDR + CPU_PQ_DATA_SIZE)
+
+#define HOST_PHYS_BASE		0x8000000000ull		/* 0.5TB */
+#define HOST_PHYS_SIZE		0x1000000000000ull	/* 0.25PB (48 bits) */
+
+#define VA_HOST_SPACE_START	0x1000000000000ull	/* 256TB */
+#define VA_HOST_SPACE_END	0x3FF8000000000ull	/* 1PB - 1TB */
+#define VA_HOST_SPACE_SIZE	(VA_HOST_SPACE_END - \
+					VA_HOST_SPACE_START) /* 767TB */
+
+#define VA_DDR_SPACE_START	0x800000000ull		/* 32GB */
+#define VA_DDR_SPACE_END	0x2000000000ull		/* 128GB */
+#define VA_DDR_SPACE_SIZE	(VA_DDR_SPACE_END - \
+					VA_DDR_SPACE_START)	/* 128GB */
+
+#define CPU_BOOT_ADDR		0x7FF8040000ull
+
+#define UBOOT_FW_OFFSET		0x100000		/* 1MB in SRAM */
+#define LINUX_FW_OFFSET		0x800000		/* 8BM in DDR */
+
+#define GOYA_MSIX_ENTRIES	8
+#define EVENT_QUEUE_MSIX_IDX	5
+#define ARMCP_RESET_MSIX_IDX	6
+
+#define QMAN_PQ_ENTRY_SIZE	16			/* Bytes */
+
+#define MAX_ASID		1024
+
+#define PROT_BITS_OFFS		0xF80
+
+/*
+ * Queue Numbering
+ *
+ * The external queues (DMA channels + CPU) MUST be before the internal queues
+ * and each group (DMA channels + CPU and internal) must be contiguous inside
+ * itself but there can be a gap between the two groups (although not
+ * recommended)
+ */
+
+enum goya_queue_id {
+	GOYA_QUEUE_ID_DMA_0 = 0,
+	GOYA_QUEUE_ID_DMA_1,
+	GOYA_QUEUE_ID_DMA_2,
+	GOYA_QUEUE_ID_DMA_3,
+	GOYA_QUEUE_ID_DMA_4,
+	GOYA_QUEUE_ID_CPU_PQ,
+	GOYA_QUEUE_ID_MME,
+	GOYA_QUEUE_ID_TPC0,
+	GOYA_QUEUE_ID_TPC1,
+	GOYA_QUEUE_ID_TPC2,
+	GOYA_QUEUE_ID_TPC3,
+	GOYA_QUEUE_ID_TPC4,
+	GOYA_QUEUE_ID_TPC5,
+	GOYA_QUEUE_ID_TPC6,
+	GOYA_QUEUE_ID_TPC7,
+	GOYA_QUEUE_ID_SIZE
+};
+
+enum goya_pll_index {
+	CPU_PLL = 0,
+	IC_PLL,
+	MC_PLL,
+	MME_PLL,
+	PCI_PLL,
+	EMMC_PLL,
+	TPC_PLL
+};
+
+#define GOYA_PLL_FREQ_LOW		50000000 /* 50 MHz */
+
+#endif /* GOYA_H */
Signature · 1 line
2.17.1
Signature · 2 lines
Sincerely yours,
Mike.
#17 lore raw

Re: [PATCH 03/15] habanalabs: add basic Goya support

Quoted 319 lines · Mike Rapoport
Quoted 317 lines · Oded Gabbay

This patch adds a basic support for the Goya device. The code initializes the device's PCI controller and PCI bars. It also initializes various S/W structures and adds some basic helper functions.

---
 drivers/misc/habanalabs/Makefile            |   5 +-
 drivers/misc/habanalabs/device.c            |  71 +++
 drivers/misc/habanalabs/goya/Makefile       |   3 +
 drivers/misc/habanalabs/goya/goya.c         | 633 ++++++++++++++++++++
 drivers/misc/habanalabs/goya/goyaP.h        | 125 ++++
 drivers/misc/habanalabs/habanalabs.h        | 131 ++++
 drivers/misc/habanalabs/habanalabs_drv.c    |   3 +
 drivers/misc/habanalabs/include/goya/goya.h | 115 ++++
 8 files changed, 1085 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/habanalabs/goya/Makefile
 create mode 100644 drivers/misc/habanalabs/goya/goya.c
 create mode 100644 drivers/misc/habanalabs/goya/goyaP.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya.h
drivers/misc/habanalabs/Makefile +4 -1
index b41433a09e02..6f1ead69bd77 100644
@@ -4,4 +4,7 @@
 
 obj-m        := habanalabs.o
 
-habanalabs-y := habanalabs_drv.o device.o
\ No newline at end of file
+habanalabs-y := habanalabs_drv.o device.o
+
+include $(src)/goya/Makefile
+habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/device.c +71 -0
index 376b55eb73d4..a4276ef559b3 100644
@@ -116,8 +116,11 @@ static int device_setup_cdev(struct hl_device *hdev, struct class *hclass,
  */
 static int device_early_init(struct hl_device *hdev)
 {
+     int rc;
+
      switch (hdev->asic_type) {
      case ASIC_GOYA:
+             goya_set_asic_funcs(hdev);
              sprintf(hdev->asic_name, "GOYA");
              break;
      default:
@@ -126,6 +129,10 @@ static int device_early_init(struct hl_device *hdev)
              return -EINVAL;
      }
 
+     rc = hdev->asic_funcs->early_init(hdev);
+     if (rc)
+             return rc;
+
      return 0;
 }
 
@@ -137,6 +144,10 @@ static int device_early_init(struct hl_device *hdev)
  */
 static void device_early_fini(struct hl_device *hdev)
 {
+
+     if (hdev->asic_funcs->early_fini)
+             hdev->asic_funcs->early_fini(hdev);
+
 }
 
 /**
@@ -150,8 +161,15 @@ static void device_early_fini(struct hl_device *hdev)
  */
 int hl_device_suspend(struct hl_device *hdev)
 {
+     int rc;
+
      pci_save_state(hdev->pdev);
 
+     rc = hdev->asic_funcs->suspend(hdev);
+     if (rc)
+             dev_err(hdev->dev,
+                     "Failed to disable PCI access of device CPU\n");
+
      /* Shut down the device */
      pci_disable_device(hdev->pdev);
      pci_set_power_state(hdev->pdev, PCI_D3hot);
@@ -181,6 +199,13 @@ int hl_device_resume(struct hl_device *hdev)
              return rc;
      }
 
+     rc = hdev->asic_funcs->resume(hdev);
+     if (rc) {
+             dev_err(hdev->dev,
+                     "Failed to enable PCI access from device CPU\n");
+             return rc;
+     }
+
      return 0;
 }
 
@@ -208,11 +233,21 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)
      if (rc)
              goto release_device;
 
+     /*
+      * Start calling ASIC initialization. First S/W then H/W and finally
+      * late init
+      */
+     rc = hdev->asic_funcs->sw_init(hdev);
+     if (rc)
+             goto early_fini;
+
      dev_notice(hdev->dev,
              "Successfully added device to habanalabs driver\n");
 
      return 0;
 
+early_fini:
+     device_early_fini(hdev);
 release_device:
      device_destroy(hclass, hdev->dev->devt);
      cdev_del(&hdev->cdev);
@@ -243,6 +278,9 @@ void hl_device_fini(struct hl_device *hdev)
      /* Mark device as disabled */
      hdev->disabled = true;
 
+     /* Call ASIC S/W finalize function */
+     hdev->asic_funcs->sw_fini(hdev);
+
      device_early_fini(hdev);
 
      /* Hide device from user */
@@ -329,3 +367,36 @@ int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
 
      return (*val ? 0 : -ETIMEDOUT);
 }
+
+/*
+ * MMIO register access helper functions.
+ */
+
+/**
+ * hl_rreg - Read an MMIO register
+ *
+ * @hdev: pointer to habanalabs device structure
+ * @reg: MMIO register offset (in bytes)
+ *
+ * Returns the value of the MMIO register we are asked to read
+ *
+ */
+inline u32 hl_rreg(struct hl_device *hdev, u32 reg)
+{
+     return readl(hdev->rmmio + reg);
+}
+
+/**
+ * hl_wreg - Write to an MMIO register
+ *
+ * @hdev: pointer to habanalabs device structure
+ * @reg: MMIO register offset (in bytes)
+ * @val: 32-bit value
+ *
+ * Writes the 32-bit value into the MMIO register
+ *
+ */
+inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val)
+{
+     writel(val, hdev->rmmio + reg);
+}
drivers/misc/habanalabs/goya/Makefile new file +3 -0
new file mode 100644
index 000000000000..5ebf3d0d5794
@@ -0,0 +1,3 @@
+subdir-ccflags-y += -I$(src)
+
+HL_GOYA_FILES :=  goya/goya.o
\ No newline at end of file
drivers/misc/habanalabs/goya/goya.c new file +130 -0
new file mode 100644
index 000000000000..b2952296b890
@@ -0,0 +1,633 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "goyaP.h"
+#include "include/goya/asic_reg/goya_masks.h"
+
+#include <linux/fs.h>
+#include <linux/delay.h>
+#include <linux/vmalloc.h>
+#include <linux/sched.h>
+#include <linux/genalloc.h>
+#include <linux/sysfs.h>
+#include <linux/kfifo.h>
+#include <linux/dma-mapping.h>
+#include <linux/firmware.h>
+#include <linux/log2.h>
+#include <linux/hwmon.h>
+#include <linux/string.h>
+#include <linux/io.h>
+
+/*
+ * GOYA security scheme:
+ *
+ * 1. Host is protected by:
+ *        - Range registers (When MMU is enabled, DMA RR does NOT protect host)
+ *        - MMU
+ *
+ * 2. DRAM is protected by:
+ *        - Range registers (protect the first 512MB)
+ *        - MMU (isolation between users)
+ *
+ * 3. Configuration is protected by:
+ *        - Range registers
+ *        - Protection bits
+ *
+ * When MMU is disabled:
+ *
+ * QMAN DMA: PQ, CQ, CP, DMA are secured.
+ * PQ, CB and the data are on the host.
+ *
+ * QMAN TPC/MME:
+ * PQ, CQ and CP are not secured.
+ * PQ, CB and the data are on the SRAM/DRAM.
+ *
+ * Since QMAN DMA is secured, KMD is parsing the DMA CB:
+ *     - KMD checks DMA pointer
+ *     - WREG, MSG_PROT are not allowed.
+ *     - MSG_LONG/SHORT are allowed.
+ *
+ * A read/write transaction by the QMAN to a protected area will succeed if
+ * and only if the QMAN's CP is secured and MSG_PROT is used
+ *
+ *
+ * When MMU is enabled:
+ *
+ * QMAN DMA: PQ, CQ and CP are secured.
+ * MMU is set to bypass on the Secure props register of the QMAN.
+ * The reasons we don't enable MMU for PQ, CQ and CP are:
+ *     - PQ entry is in kernel address space and KMD doesn't map it.
+ *     - CP writes to MSIX register and to kernel address space (completion
+ *       queue).
+ *
+ * DMA is not secured but because CP is secured, KMD still needs to parse the
+ * CB, but doesn't need to check the DMA addresses.
+ *
+ * For QMAN DMA 0, DMA is also secured because only KMD uses this DMA and KMD
+ * doesn't map memory in MMU.
+ *
+ * QMAN TPC/MME: PQ, CQ and CP aren't secured (no change from MMU disabled mode)
+ *
+ * DMA RR does NOT protect host because DMA is not secured
+ *
+ */
+
+#define GOYA_MMU_REGS_NUM            61
+
+#define GOYA_DMA_POOL_BLK_SIZE               0x100           /* 256 bytes */
+
+#define GOYA_RESET_TIMEOUT_MSEC              500             /* 500ms */
+#define GOYA_PLDM_RESET_TIMEOUT_MSEC 20000           /* 20s */
+#define GOYA_RESET_WAIT_MSEC         1               /* 1ms */
+#define GOYA_CPU_RESET_WAIT_MSEC     100             /* 100ms */
+#define GOYA_PLDM_RESET_WAIT_MSEC    1000            /* 1s */
+#define GOYA_CPU_TIMEOUT_USEC                10000000        /* 10s */
+#define GOYA_TEST_QUEUE_WAIT_USEC    100000          /* 100ms */
+
+#define GOYA_QMAN0_FENCE_VAL         0xD169B243
+
+#define GOYA_MAX_INITIATORS          20
+
+static void goya_get_fixed_properties(struct hl_device *hdev)
+{
+     struct asic_fixed_properties *prop = &hdev->asic_prop;
+
+     prop->completion_queues_count = NUMBER_OF_CMPLT_QUEUES;
+
+     prop->dram_base_address = DRAM_PHYS_BASE;
+     prop->dram_size = DRAM_PHYS_DEFAULT_SIZE;
+     prop->dram_end_address = prop->dram_base_address + prop->dram_size;
+     prop->dram_user_base_address = DRAM_BASE_ADDR_USER;
+
+     prop->sram_base_address = SRAM_BASE_ADDR;
+     prop->sram_size = SRAM_SIZE;
+     prop->sram_end_address = prop->sram_base_address + prop->sram_size;
+     prop->sram_user_base_address = prop->sram_base_address +
+                                             SRAM_USER_BASE_OFFSET;
+
+     prop->host_phys_base_address = HOST_PHYS_BASE;
+     prop->va_space_host_start_address = VA_HOST_SPACE_START;
+     prop->va_space_host_end_address = VA_HOST_SPACE_END;
+     prop->va_space_dram_start_address = VA_DDR_SPACE_START;
+     prop->va_space_dram_end_address = VA_DDR_SPACE_END;
+     prop->cfg_size = CFG_SIZE;
+     prop->max_asid = MAX_ASID;
+     prop->tpc_enabled_mask = TPC_ENABLED_MASK;
+
+     prop->high_pll = PLL_HIGH_DEFAULT;
+}
+
+/**
+ * goya_pci_bars_map - Map PCI BARS of Goya device
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Request PCI regions and map them to kernel virtual addresses.
+ * Returns 0 on success
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c line 136

Quoted 6 lines
drivers/misc/habanalabs/goya/goya.c new file +6 -0
+ *
+ */
+int goya_pci_bars_map(struct hl_device *hdev)
+{
+     struct pci_dev *pdev = hdev->pdev;
+     int rc;

You could just init rc= -ENODEV here and avoid the hassle below.

But the next line assigns rc the return value of pci_request_regions... I could do rc= -ENODEV before the calls to pci_ioremapbar but then if this function will change in the future and I will have another possibility of a different error, it will seem strange.
I honestly prefer to write code in drivers as explicitly as possible, even if that means a bit more code.

Quoted 97 lines
Quoted 97 lines
drivers/misc/habanalabs/goya/goya.c new file +97 -0
+
+     rc = pci_request_regions(pdev, HL_NAME);
+     if (rc) {
+             dev_err(hdev->dev, "Cannot obtain PCI resources\n");
+             return rc;
+     }
+
+     hdev->pcie_bar[SRAM_CFG_BAR_ID] =
+                     pci_ioremap_bar(pdev, SRAM_CFG_BAR_ID);
+     if (!hdev->pcie_bar[SRAM_CFG_BAR_ID]) {
+             dev_err(hdev->dev, "pci_ioremap_bar failed for CFG\n");
+             rc = -ENODEV;
+             goto err_release_regions;
+     }
+
+     hdev->pcie_bar[MSIX_BAR_ID] = pci_ioremap_bar(pdev, MSIX_BAR_ID);
+     if (!hdev->pcie_bar[MSIX_BAR_ID]) {
+             dev_err(hdev->dev, "pci_ioremap_bar failed for MSIX\n");
+             rc = -ENODEV;
+             goto err_unmap_sram_cfg;
+     }
+
+     hdev->pcie_bar[DDR_BAR_ID] = pci_ioremap_wc_bar(pdev, DDR_BAR_ID);
+     if (!hdev->pcie_bar[DDR_BAR_ID]) {
+             dev_err(hdev->dev, "pci_ioremap_bar failed for DDR\n");
+             rc = -ENODEV;
+             goto err_unmap_msix;
+     }
+
+     hdev->rmmio = hdev->pcie_bar[SRAM_CFG_BAR_ID] +
+                             (CFG_BASE - SRAM_BASE_ADDR);
+
+     return 0;
+
+err_unmap_msix:
+     iounmap(hdev->pcie_bar[MSIX_BAR_ID]);
+err_unmap_sram_cfg:
+     iounmap(hdev->pcie_bar[SRAM_CFG_BAR_ID]);
+err_release_regions:
+     pci_release_regions(pdev);
+
+     return rc;
+}
+
+/**
+ * goya_pci_bars_unmap - Unmap PCI BARS of Goya device
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Release all PCI BARS and unmap their virtual addresses
+ *
+ */
+static void goya_pci_bars_unmap(struct hl_device *hdev)
+{
+     struct pci_dev *pdev = hdev->pdev;
+
+     iounmap(hdev->pcie_bar[DDR_BAR_ID]);
+     iounmap(hdev->pcie_bar[MSIX_BAR_ID]);
+     iounmap(hdev->pcie_bar[SRAM_CFG_BAR_ID]);
+     pci_release_regions(pdev);
+}
+
+/**
+ * goya_elbi_write - Write through the ELBI interface
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * return 0 on success, -1 on failure
+ *
+ */
+static int goya_elbi_write(struct hl_device *hdev, u64 addr, u32 data)
+{
+     struct pci_dev *pdev = hdev->pdev;
+     ktime_t timeout;
+     u32 val;
+
+     /* Clear previous status */
+     pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, 0);
+
+     pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_ADDR, (u32) addr);
+     pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_DATA, data);
+     pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_CTRL,
+                             PCI_CONFIG_ELBI_CTRL_WRITE);
+
+     timeout = ktime_add_ms(ktime_get(), 10);
+     for (;;) {
+             pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, &val);
+             if (val & PCI_CONFIG_ELBI_STS_MASK)
+                     break;
+             if (ktime_compare(ktime_get(), timeout) > 0) {
+                     pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS,
+                                             &val);
+                     break;
+             }
+             usleep_range(300, 500);
+     }
+
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c line 239

Quoted 6 lines
drivers/misc/habanalabs/goya/goya.c new file +6 -0
+     if ((val & PCI_CONFIG_ELBI_STS_MASK) == PCI_CONFIG_ELBI_STS_DONE)
+             return 0;
+
+     if (val & PCI_CONFIG_ELBI_STS_ERR) {
+             dev_err(hdev->dev, "Error writing to ELBI\n");o
+             return -1;

Please change -1 to an error code, say -EIO...

Of course, done.

Quoted 22 lines
Quoted 22 lines
drivers/misc/habanalabs/goya/goya.c new file +21 -0
+     }
+
+     if (!(val & PCI_CONFIG_ELBI_STS_MASK)) {
+             dev_err(hdev->dev, "ELBI write didn't finish in time\n");
+             return -1;
+     }
+
+     dev_err(hdev->dev, "ELBI write has undefined bits in status\n");
+     return -1;
+}
+
+/**
+ * goya_iatu_write - iatu write routine
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+static int goya_iatu_write(struct hl_device *hdev, u32 addr, u32 data)
+{
+     u32 dbi_offset;
+     int rc;
Quoted 9 lines

drivers/misc/habanalabs/goya/goya.c line 265

Quoted 5 lines
drivers/misc/habanalabs/goya/goya.c new file +5 -0
+
+     dbi_offset = addr & 0xFFF;
+
+     rc = goya_elbi_write(hdev, CFG_BASE + mmPCIE_AUX_DBI, 0x00300000);
+     rc |= goya_elbi_write(hdev, mmPCIE_DBI_BASE + dbi_offset, data);

hmm, error code in goya_elbi_write probably won't work... Any reason to try the second write if the first failed?

You are correct it definitely won't work. But I didn't want to put an if() after each call to that function - it happens a few more times in the code.
And because the second write won't do any harm either, I thought this is a more elegant solution to make the code more readable.

Quoted 43 lines
Quoted 43 lines
drivers/misc/habanalabs/goya/goya.c new file +43 -0
+
+     return rc;
+}
+
+void goya_reset_link_through_bridge(struct hl_device *hdev)
+{
+     struct pci_dev *pdev = hdev->pdev;
+     struct pci_dev *parent_port;
+     u16 val;
+
+     parent_port = pdev->bus->self;
+     pci_read_config_word(parent_port, PCI_BRIDGE_CONTROL, &val);
+     val |= PCI_BRIDGE_CTL_BUS_RESET;
+     pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
+     ssleep(1);
+
+     val &= ~(PCI_BRIDGE_CTL_BUS_RESET);
+     pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
+     ssleep(3);
+}
+
+/**
+ * goya_set_ddr_bar_base - set DDR bar to map specific device address
+ *
+ * @hdev: pointer to hl_device structure
+ * @addr: address in DDR. Must be aligned to DDR bar size
+ *
+ * This function configures the iATU so that the DDR bar will start at the
+ * specified addr.
+ *
+ */
+static int goya_set_ddr_bar_base(struct hl_device *hdev, u64 addr)
+{
+     struct goya_device *goya = hdev->asic_specific;
+     int rc;
+
+     if ((goya) && (goya->ddr_bar_cur_addr == addr))
+             return 0;
+
+     /* Inbound Region 1 - Bar 4 - Point to DDR */
+     rc = goya_iatu_write(hdev, 0x314, lower_32_bits(addr));
+     rc |= goya_iatu_write(hdev, 0x318, upper_32_bits(addr));
+     rc |= goya_iatu_write(hdev, 0x300, 0);
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c line 314

Quoted 6 lines
drivers/misc/habanalabs/goya/goya.c new file +6 -0
+     /* Enable + Bar match + match enable + Bar 4 */
+     rc |= goya_iatu_write(hdev, 0x304, 0xC0080400);
+
+     /* Return the DBI window to the default location */
+     rc |= goya_elbi_write(hdev, CFG_BASE + mmPCIE_AUX_DBI, 0);
+     rc |= goya_elbi_write(hdev, CFG_BASE + mmPCIE_AUX_DBI_32, 0);

And here as well.

Same remark as the previous one

Quoted 7 lines

drivers/misc/habanalabs/goya/goya.c line 319

Quoted 5 lines
drivers/misc/habanalabs/goya/goya.c new file +5 -0
+
+     if (rc) {
+             dev_err(hdev->dev, "failed to map DDR bar to 0x%08llx\n", addr);
+             return rc;
+     }

I believe that at least here you'd want to return an error code.

Fixed

Quoted 43 lines
Quoted 43 lines
drivers/misc/habanalabs/goya/goya.c new file +42 -0
+
+     if (goya)
+             goya->ddr_bar_cur_addr = addr;
+
+     return 0;
+}
+
+/**
+ * goya_init_iatu - Initialize the iATU unit inside the PCI controller
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * This is needed in case the firmware doesn't initialize the iATU
+ *
+ */
+static int goya_init_iatu(struct hl_device *hdev)
+{
+     int rc;
+
+     /* Inbound Region 0 - Bar 0 - Point to SRAM_BASE_ADDR */
+     rc  = goya_iatu_write(hdev, 0x114, lower_32_bits(SRAM_BASE_ADDR));
+     rc |= goya_iatu_write(hdev, 0x118, upper_32_bits(SRAM_BASE_ADDR));
+     rc |= goya_iatu_write(hdev, 0x100, 0);
+     /* Enable + Bar match + match enable */
+     rc |= goya_iatu_write(hdev, 0x104, 0xC0080000);
+
+     /* Inbound Region 1 - Bar 4 - Point to DDR */
+     rc |= goya_set_ddr_bar_base(hdev, DRAM_PHYS_BASE);
+
+     /* Outbound Region 0 - Point to Host */
+     rc |= goya_iatu_write(hdev, 0x008, lower_32_bits(HOST_PHYS_BASE));
+     rc |= goya_iatu_write(hdev, 0x00C, upper_32_bits(HOST_PHYS_BASE));
+     rc |= goya_iatu_write(hdev, 0x010,
+             lower_32_bits(HOST_PHYS_BASE + HOST_PHYS_SIZE - 1));
+     rc |= goya_iatu_write(hdev, 0x014, 0);
+     rc |= goya_iatu_write(hdev, 0x018, 0);
+     rc |= goya_iatu_write(hdev, 0x020,
+             upper_32_bits(HOST_PHYS_BASE + HOST_PHYS_SIZE - 1));
+     /* Increase region size */
+     rc |= goya_iatu_write(hdev, 0x000, 0x00002000);
+     /* Enable */
+     rc |= goya_iatu_write(hdev, 0x004, 0x80000000);
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c line 318

Quoted 6 lines
drivers/misc/habanalabs/goya/goya.c new file +6 -0
+
+     /* Return the DBI window to the default location */
+     rc |= goya_elbi_write(hdev, CFG_BASE + mmPCIE_AUX_DBI, 0);
+     rc |= goya_elbi_write(hdev, CFG_BASE + mmPCIE_AUX_DBI_32, 0);
+
+     return rc;

Ditto

Fixed

Quoted 130 lines
Quoted 130 lines
drivers/misc/habanalabs/goya/goya.c new file +129 -0
+}
+
+/**
+ * goya_early_init - GOYA early initialization code
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Verify PCI bars
+ * Set DMA masks
+ * PCI controller initialization
+ * Map PCI bars
+ *
+ */
+static int goya_early_init(struct hl_device *hdev)
+{
+     struct asic_fixed_properties *prop = &hdev->asic_prop;
+     struct pci_dev *pdev = hdev->pdev;
+     u32 val;
+     int rc;
+
+     goya_get_fixed_properties(hdev);
+
+     /* Check BAR sizes */
+     if (pci_resource_len(pdev, SRAM_CFG_BAR_ID) != CFG_BAR_SIZE) {
+             dev_err(hdev->dev,
+                     "Not " HL_NAME "? BAR %d size %llu, expecting %llu\n",
+                     SRAM_CFG_BAR_ID,
+                     pci_resource_len(pdev, SRAM_CFG_BAR_ID),
+                     CFG_BAR_SIZE);
+             return -ENODEV;
+     }
+
+     if (pci_resource_len(pdev, MSIX_BAR_ID) != MSIX_BAR_SIZE) {
+             dev_err(hdev->dev,
+                     "Not " HL_NAME "? BAR %d size %llu, expecting %llu\n",
+                     MSIX_BAR_ID, pci_resource_len(pdev, MSIX_BAR_ID),
+                     MSIX_BAR_SIZE);
+             return -ENODEV;
+     }
+
+     prop->dram_pci_bar_size = pci_resource_len(pdev, DDR_BAR_ID);
+
+     /* set DMA mask for GOYA */
+     rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(39));
+     if (rc) {
+             dev_warn(hdev->dev, "Unable to set pci dma mask to 39 bits\n");
+             rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
+             if (rc) {
+                     dev_err(hdev->dev,
+                             "Unable to set pci dma mask to 32 bits\n");
+                     return rc;
+             }
+     }
+
+     rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
+     if (rc) {
+             dev_warn(hdev->dev,
+                     "Unable to set pci consistent dma mask to 39 bits\n");
+             rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
+             if (rc) {
+                     dev_err(hdev->dev,
+                             "Unable to set pci consistent dma mask to 32 bits\n");
+                     return rc;
+             }
+     }
+
+     if (hdev->reset_pcilink)
+             goya_reset_link_through_bridge(hdev);
+
+     rc = pci_enable_device_mem(pdev);
+     if (rc) {
+             dev_err(hdev->dev, "can't enable PCI device\n");
+             return rc;
+     }
+
+     pci_set_master(pdev);
+
+     rc = goya_init_iatu(hdev);
+     if (rc) {
+             dev_err(hdev->dev, "Failed to initialize iATU\n");
+             goto disable_device;
+     }
+
+     rc = goya_pci_bars_map(hdev);
+     if (rc) {
+             dev_err(hdev->dev, "Failed to initialize PCI BARS\n");
+             goto disable_device;
+     }
+
+     val = RREG32(mmPSOC_GLOBAL_CONF_BOOT_STRAP_PINS);
+     if (val & PSOC_GLOBAL_CONF_BOOT_STRAP_PINS_SRIOV_EN_MASK)
+             dev_warn(hdev->dev,
+                     "PCI strap is not configured correctly, PCI bus errors may occur\n");
+
+     return 0;
+
+disable_device:
+     pci_clear_master(pdev);
+     pci_disable_device(pdev);
+
+     return rc;
+}
+
+/**
+ * goya_early_fini - GOYA early finalization code
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Unmap PCI bars
+ *
+ */
+int goya_early_fini(struct hl_device *hdev)
+{
+     goya_pci_bars_unmap(hdev);
+
+     pci_clear_master(hdev->pdev);
+     pci_disable_device(hdev->pdev);
+
+     return 0;
+}
+
+/**
+ * goya_sw_init - Goya software initialization code
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+static int goya_sw_init(struct hl_device *hdev)
+{
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c line 501

Quoted 5 lines
drivers/misc/habanalabs/goya/goya.c new file +5 -0
+     struct goya_device *goya;
+     int rc;
+
+     /* Allocate device structure */
+     goya = kzalloc(sizeof(*goya), GFP_KERNEL);

Consider using devm_k[mz]alloc() for memory allocations throughout the driver. I didn't check all the spots where it can be applicable.

I honestly wasn't aware of that. We never used that in AMD drivers (which where I spent most of my kernel time).
I'll look into that offline but for now I don't really want to change into it blindly in all locations, unless there is some hard kernel rule for using that in drivers.

Quoted 30 lines
Quoted 30 lines
drivers/misc/habanalabs/goya/goya.c new file +29 -0
+     if (!goya)
+             return -ENOMEM;
+
+     /* according to goya_init_iatu */
+     goya->ddr_bar_cur_addr = DRAM_PHYS_BASE;
+     hdev->asic_specific = goya;
+
+     /* Create DMA pool for small allocations */
+     hdev->dma_pool = dma_pool_create(dev_name(hdev->dev),
+                     &hdev->pdev->dev, GOYA_DMA_POOL_BLK_SIZE, 8, 0);
+     if (!hdev->dma_pool) {
+             dev_err(hdev->dev, "failed to create DMA pool\n");
+             rc = -ENOMEM;
+             goto free_goya_device;
+     }
+
+     hdev->cpu_accessible_dma_mem =
+                     hdev->asic_funcs->dma_alloc_coherent(hdev,
+                                     CPU_ACCESSIBLE_MEM_SIZE,
+                                     &hdev->cpu_accessible_dma_address,
+                                     GFP_KERNEL | __GFP_ZERO);
+
+     if (!hdev->cpu_accessible_dma_mem) {
+             dev_err(hdev->dev,
+                     "failed to allocate %d of dma memory for CPU accessible memory space\n",
+                     CPU_ACCESSIBLE_MEM_SIZE);
+             rc = -ENOMEM;
+             goto free_dma_pool;
+     }
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c line 536

Quoted 6 lines
drivers/misc/habanalabs/goya/goya.c new file +6 -0
+
+     hdev->cpu_accessible_dma_pool = gen_pool_create(CPU_PKT_SHIFT, -1);
+     if (!hdev->cpu_accessible_dma_pool) {
+             dev_err(hdev->dev,
+                     "Failed to create CPU accessible DMA pool\n");
+             rc = -ENOMEM;

You could init rc = -ENOMEM at the beginning and save the duplication.

Again, I don't agree with that programming paradigm. If I do that, and then I'll add code at the beginning of the function in the likes of:
rc = foo()
I will insert a bug.
So I prefer the duplication and make the code more robust to future changes.

Quoted 83 lines
Quoted 83 lines
drivers/misc/habanalabs/goya/goya.c new file +82 -0
+             goto free_cpu_pq_dma_mem;
+     }
+
+     rc = gen_pool_add(hdev->cpu_accessible_dma_pool,
+                             (u64) hdev->cpu_accessible_dma_mem,
+                             CPU_ACCESSIBLE_MEM_SIZE, -1);
+     if (rc) {
+             dev_err(hdev->dev,
+                     "Failed to add memory to CPU accessible DMA pool\n");
+             rc = -EFAULT;
+             goto free_cpu_pq_pool;
+     }
+
+     spin_lock_init(&goya->hw_queues_lock);
+
+     return 0;
+
+free_cpu_pq_pool:
+     gen_pool_destroy(hdev->cpu_accessible_dma_pool);
+free_cpu_pq_dma_mem:
+     hdev->asic_funcs->dma_free_coherent(hdev, CPU_ACCESSIBLE_MEM_SIZE,
+                     hdev->cpu_accessible_dma_mem,
+                     hdev->cpu_accessible_dma_address);
+free_dma_pool:
+     dma_pool_destroy(hdev->dma_pool);
+free_goya_device:
+     kfree(goya);
+
+     return rc;
+}
+
+/**
+ * goya_sw_fini - Goya software tear-down code
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+int goya_sw_fini(struct hl_device *hdev)
+{
+     struct goya_device *goya = hdev->asic_specific;
+
+     gen_pool_destroy(hdev->cpu_accessible_dma_pool);
+
+     hdev->asic_funcs->dma_free_coherent(hdev, CPU_ACCESSIBLE_MEM_SIZE,
+                     hdev->cpu_accessible_dma_mem,
+                     hdev->cpu_accessible_dma_address);
+
+     dma_pool_destroy(hdev->dma_pool);
+
+     kfree(goya);
+
+     return 0;
+}
+
+int goya_suspend(struct hl_device *hdev)
+{
+     return 0;
+}
+
+int goya_resume(struct hl_device *hdev)
+{
+     return 0;
+}
+
+void *goya_dma_alloc_coherent(struct hl_device *hdev, size_t size,
+                                     dma_addr_t *dma_handle, gfp_t flags)
+{
+     return dma_alloc_coherent(&hdev->pdev->dev, size, dma_handle, flags);
+}
+
+void goya_dma_free_coherent(struct hl_device *hdev, size_t size, void *cpu_addr,
+                             dma_addr_t dma_handle)
+{
+     dma_free_coherent(&hdev->pdev->dev, size, cpu_addr, dma_handle);
+}
+
+static const struct hl_asic_funcs goya_funcs = {
+     .early_init = goya_early_init,
+     .early_fini = goya_early_fini,
+     .sw_init = goya_sw_init,
+     .sw_fini = goya_sw_fini,
+     .suspend = goya_suspend,
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c line 621

Quoted 3 lines
drivers/misc/habanalabs/goya/goya.c new file +3 -0
+     .resume = goya_resume,
+     .dma_alloc_coherent = goya_dma_alloc_coherent,
+     .dma_free_coherent = goya_dma_free_coherent,

Is there any additional functionality that is planned in goya or gaudi in these two functions?
It seems like they are not really needed, at least at the moment and for sure that don't need to be part of ASIC ops.

So this relates to the simulator support, because there the implementation of these two functions is totally different as I don't have pci device.

Quoted 473 lines
Quoted 466 lines
drivers/misc/habanalabs/goya/goya.c new file +12 -0
+};
+
+/**
+ * goya_set_asic_funcs - set Goya function pointers
+ *
+ * @*hdev: pointer to hl_device structure
+ *
+ */
+void goya_set_asic_funcs(struct hl_device *hdev)
+{
+     hdev->asic_funcs = &goya_funcs;
+}
drivers/misc/habanalabs/goya/goyaP.h new file +125 -0
new file mode 100644
index 000000000000..0e12c56472bd
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ */
+
+#ifndef GOYAP_H_
+#define GOYAP_H_
+
+#include "habanalabs.h"
+#include "include/goya/goya.h"
+
+#define NUMBER_OF_CMPLT_QUEUES               5
+#define NUMBER_OF_EXT_HW_QUEUES              5
+#define NUMBER_OF_CPU_HW_QUEUES              1
+#define NUMBER_OF_INT_HW_QUEUES              9
+#define NUMBER_OF_HW_QUEUES          (NUMBER_OF_EXT_HW_QUEUES + \
+                                     NUMBER_OF_CPU_HW_QUEUES + \
+                                     NUMBER_OF_INT_HW_QUEUES)
+
+/*
+ * Number of MSIX interrupts IDS:
+ * Each completion queue has 1 ID
+ * The event queue has 1 ID
+ * ArmCP reset has 1 ID
+ */
+#define NUMBER_OF_INTERRUPTS         (NUMBER_OF_CMPLT_QUEUES + 2)
+
+#if (NUMBER_OF_HW_QUEUES >= HL_MAX_QUEUES)
+#error "Number of H/W queues must be smaller than HL_MAX_QUEUES"
+#endif
+
+#if (NUMBER_OF_INTERRUPTS > GOYA_MSIX_ENTRIES)
+#error "Number of MSIX interrupts must be smaller or equal to GOYA_MSIX_ENTRIES"
+#endif
+
+#define QMAN_FENCE_TIMEOUT_USEC              10000   /* 10 ms */
+
+#define QMAN_STOP_TIMEOUT_USEC               100000  /* 100 ms */
+
+#define TPC_MAX_NUM                  8
+#define TPC_ENABLED_MASK             0xFF
+
+#define DMA_MAX_NUM                  5
+
+#define PLL_HIGH_DEFAULT             1575000000      /* 1.575 GHz */
+
+#define GOYA_ARMCP_INFO_TIMEOUT              10000000        /* 10s */
+
+#define DRAM_PHYS_DEFAULT_SIZE               0x100000000ull  /* 4GB */
+
+/*
+ * SRAM Memory Map for KMD
+ *
+ * KMD occupies KMD_SRAM_SIZE bytes from the start of SRAM. It is used for
+ * MME/TPC QMANs
+ *
+ */
+
+#define MME_QMAN_BASE_OFFSET 0x000000        /* Must be 0 */
+#define MME_QMAN_LENGTH              64
+#define TPC_QMAN_LENGTH              64
+
+#define TPC0_QMAN_BASE_OFFSET        (MME_QMAN_BASE_OFFSET + \
+                             (MME_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC1_QMAN_BASE_OFFSET        (TPC0_QMAN_BASE_OFFSET + \
+                             (TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC2_QMAN_BASE_OFFSET        (TPC1_QMAN_BASE_OFFSET + \
+                             (TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC3_QMAN_BASE_OFFSET        (TPC2_QMAN_BASE_OFFSET + \
+                             (TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC4_QMAN_BASE_OFFSET        (TPC3_QMAN_BASE_OFFSET + \
+                             (TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC5_QMAN_BASE_OFFSET        (TPC4_QMAN_BASE_OFFSET + \
+                             (TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC6_QMAN_BASE_OFFSET        (TPC5_QMAN_BASE_OFFSET + \
+                             (TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+#define TPC7_QMAN_BASE_OFFSET        (TPC6_QMAN_BASE_OFFSET + \
+                             (TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+
+#define SRAM_KMD_RES_OFFSET  (TPC7_QMAN_BASE_OFFSET + \
+                             (TPC_QMAN_LENGTH * QMAN_PQ_ENTRY_SIZE))
+
+#if (SRAM_KMD_RES_OFFSET >= KMD_SRAM_RESERVED_SIZE)
+#error "MME/TPC QMANs SRAM space exceeds limit"
+#endif
+
+#define SRAM_USER_BASE_OFFSET        KMD_SRAM_RESERVED_SIZE
+
+#define DMA_MAX_TRANSFER_SIZE        0xFFFFFFFF
+
+#define HW_CAP_PLL           0x00000001
+#define HW_CAP_DDR_0         0x00000002
+#define HW_CAP_DDR_1         0x00000004
+#define HW_CAP_MME           0x00000008
+#define HW_CAP_CPU           0x00000010
+#define HW_CAP_DMA           0x00000020
+#define HW_CAP_MSIX          0x00000040
+#define HW_CAP_CPU_Q         0x00000080
+#define HW_CAP_MMU           0x00000100
+#define HW_CAP_TPC_MBIST     0x00000200
+#define HW_CAP_GOLDEN                0x00000400
+#define HW_CAP_TPC           0x00000800
+
+#define CPU_PKT_SHIFT                5
+#define CPU_PKT_SIZE         (1 << CPU_PKT_SHIFT)
+#define CPU_PKT_MASK         (~((1 << CPU_PKT_SHIFT) - 1))
+#define CPU_MAX_PKTS_IN_CB   32
+#define CPU_CB_SIZE          (CPU_PKT_SIZE * CPU_MAX_PKTS_IN_CB)
+#define CPU_ACCESSIBLE_MEM_SIZE      (HL_QUEUE_LENGTH * CPU_CB_SIZE)
+
+enum goya_fw_component {
+     FW_COMP_UBOOT,
+     FW_COMP_PREBOOT
+};
+
+struct goya_device {
+     /* TODO: remove hw_queues_lock after moving to scheduler code */
+     spinlock_t      hw_queues_lock;
+     u64             ddr_bar_cur_addr;
+     u32             hw_cap_initialized;
+};
+
+#endif /* GOYAP_H_ */
drivers/misc/habanalabs/habanalabs.h +131 -0
index 7e1b088b677c..97844825f7a8 100644
@@ -21,11 +21,64 @@
 
 #define HL_NAME                              "habanalabs"
 
+#define HL_MAX_QUEUES                        128
+
 struct hl_device;
 
 
 
 
+/**
+ * struct asic_fixed_properties - ASIC specific immutable properties.
+ * @sram_base_address: SRAM physical start address.
+ * @sram_end_address: SRAM physical end address.
+ * @sram_user_base_address - SRAM physical start address for user access.
+ * @dram_base_address: DRAM physical start address.
+ * @dram_end_address: DRAM physical end address.
+ * @dram_user_base_address: DRAM physical start address for user access.
+ * @dram_size: DRAM total size.
+ * @dram_pci_bar_size: size of PCI bar towards DRAM.
+ * @host_phys_base_address: base physical address of host memory for
+ *                           transactions that the device generates.
+ * @va_space_host_start_address: base address of virtual memory range for
+ *                               mapping host memory.
+ * @va_space_host_end_address: end address of virtual memory range for
+ *                             mapping host memory.
+ * @va_space_dram_start_address: base address of virtual memory range for
+ *                               mapping DRAM memory.
+ * @va_space_dram_end_address: end address of virtual memory range for
+ *                             mapping DRAM memory.
+ * @cfg_size: configuration space size on SRAM.
+ * @sram_size: total size of SRAM.
+ * @max_asid: maximum number of open contexts (ASIDs).
+ * @completion_queues_count: number of completion queues.
+ * @high_pll: high PLL frequency used by the device.
+ * @tpc_enabled_mask: which TPCs are enabled.
+ */
+struct asic_fixed_properties {
+     u64                     sram_base_address;
+     u64                     sram_end_address;
+     u64                     sram_user_base_address;
+     u64                     dram_base_address;
+     u64                     dram_end_address;
+     u64                     dram_user_base_address;
+     u64                     dram_size;
+     u64                     dram_pci_bar_size;
+     u64                     host_phys_base_address;
+     u64                     va_space_host_start_address;
+     u64                     va_space_host_end_address;
+     u64                     va_space_dram_start_address;
+     u64                     va_space_dram_end_address;
+     u32                     cfg_size;
+     u32                     sram_size;
+     u32                     max_asid;
+     u32                     high_pll;
+     u8                      completion_queues_count;
+     u8                      tpc_enabled_mask;
+};
+
+
+#define HL_QUEUE_LENGTH                      256
 
 
 /*
@@ -47,6 +100,30 @@ enum hl_asic_type {
 
 
 
+/**
+ * struct hl_asic_funcs - ASIC specific functions that are can be called from
+ *                        common code.
+ * @early_init: sets up early driver state (pre sw_init), doesn't configure H/W.
+ * @early_fini: tears down what was done in early_init.
+ * @sw_init: sets up driver state, does not configure H/W.
+ * @sw_fini: tears down driver state, does not configure H/W.
+ * @suspend: handles IP specific H/W or SW changes for suspend.
+ * @resume: handles IP specific H/W or SW changes for resume.
+ * @dma_alloc_coherent: DMA allocate coherent memory.
+ * @dma_free_coherent: free DMA allocation.
+ */
+struct hl_asic_funcs {
+     int (*early_init)(struct hl_device *hdev);
+     int (*early_fini)(struct hl_device *hdev);
+     int (*sw_init)(struct hl_device *hdev);
+     int (*sw_fini)(struct hl_device *hdev);
+     int (*suspend)(struct hl_device *hdev);
+     int (*resume)(struct hl_device *hdev);
+     void* (*dma_alloc_coherent)(struct hl_device *hdev, size_t size,
+                                     dma_addr_t *dma_handle, gfp_t flag);
+     void (*dma_free_coherent)(struct hl_device *hdev, size_t size,
+                                     void *cpu_addr, dma_addr_t dma_handle);
+};
 
 /*
  * FILE PRIVATE STRUCTURE
@@ -78,26 +155,78 @@ struct hl_fpriv {
  */
 #define HL_MAX_MINORS        256
 
+/*
+ * Registers read & write functions.
+ */
+
+u32 hl_rreg(struct hl_device *hdev, u32 reg);
+void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
+
+#define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
+     readl_poll_timeout(hdev->rmmio + addr, val, cond, sleep_us, timeout_us)
+
+#define RREG32(reg) hl_rreg(hdev, (reg))
+#define WREG32(reg, v) hl_wreg(hdev, (reg), (v))
+#define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n", \
+                             hl_rreg(hdev, (reg)))
+
+#define WREG32_P(reg, val, mask)                             \
+     do {                                                    \
+             u32 tmp_ = RREG32(reg);                         \
+             tmp_ &= (mask);                                 \
+             tmp_ |= ((val) & ~(mask));                      \
+             WREG32(reg, tmp_);                              \
+     } while (0)
+#define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
+#define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))
+
+#define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
+#define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
+#define WREG32_FIELD(reg, field, val)        \
+     WREG32(mm##reg, (RREG32(mm##reg) & ~REG_FIELD_MASK(reg, field)) | \
+                     (val) << REG_FIELD_SHIFT(reg, field))
+
 /**
  * struct hl_device - habanalabs device structure.
  * @pdev: pointer to PCI device, can be NULL in case of simulator device.
+ * @pcie_bar: array of available PCIe bars.
+ * @rmmio: configuration area address on SRAM.
  * @cdev: related char device.
  * @dev: realted kernel basic device structure.
  * @asic_name: ASIC specific nmae.
  * @asic_type: ASIC specific type.
+ * @dma_pool: DMA pool for small allocations.
+ * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address.
+ * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address.
+ * @cpu_accessible_dma_pool: KMD <-> ArmCP shared memory pool.
+ * @asic_prop: ASIC specific immutable properties.
+ * @asic_funcs: ASIC specific functions.
+ * @asic_specific: ASIC specific information to use only from ASIC files.
  * @major: habanalabs KMD major.
  * @id: device minor.
  * @disabled: is device disabled.
  */
 struct hl_device {
      struct pci_dev                  *pdev;
+     void __iomem                    *pcie_bar[6];
+     void __iomem                    *rmmio;
      struct cdev                     cdev;
      struct device                   *dev;
      char                            asic_name[16];
      enum hl_asic_type               asic_type;
+     struct dma_pool                 *dma_pool;
+     void                            *cpu_accessible_dma_mem;
+     dma_addr_t                      cpu_accessible_dma_address;
+     struct gen_pool                 *cpu_accessible_dma_pool;
+     struct asic_fixed_properties    asic_prop;
+     const struct hl_asic_funcs      *asic_funcs;
+     void                            *asic_specific;
      u32                             major;
      u16                             id;
      u8                              disabled;
+
+     /* Parameters for bring-up */
+     u8                              reset_pcilink;
 };
 
 /*
@@ -146,4 +275,6 @@ void hl_device_fini(struct hl_device *hdev);
 int hl_device_suspend(struct hl_device *hdev);
 int hl_device_resume(struct hl_device *hdev);
 
+void goya_set_asic_funcs(struct hl_device *hdev);
+
 #endif /* HABANALABSP_H_ */
drivers/misc/habanalabs/habanalabs_drv.c +3 -0
index 15217975327b..79545003b7c2 100644
@@ -136,6 +136,9 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
 
      hdev->major = hl_major;
 
+     /* Parameters for bring-up - set them to defaults */
+     hdev->reset_pcilink = 0;
+
      hdev->disabled = true;
      hdev->pdev = pdev; /* can be NULL in case of simulator device */
 
drivers/misc/habanalabs/include/goya/goya.h new file +115 -1
new file mode 100644
index 000000000000..192a1450cbb1
@@ -0,0 +1,115 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ * Author: Oded Gabbay <[email protected]>
+ *
+ */
+
+#ifndef GOYA_H
+#define GOYA_H
+
+#include "asic_reg/goya_regs.h"
+
+#include <linux/types.h>
+
+#define SRAM_CFG_BAR_ID              0
+#define MSIX_BAR_ID          2
+#define DDR_BAR_ID           4
+
+#define CFG_BAR_SIZE         0x10000000ull           /* 256MB */
+#define MSIX_BAR_SIZE                0x1000ull               /* 4KB */
+
+#define CFG_BASE             0x7FFC000000ull
+#define CFG_SIZE             0x4000000               /* 32MB CFG + 32MB DBG*/
+
+#define SRAM_BASE_ADDR               0x7FF0000000ull
+#define SRAM_SIZE            0x32A0000               /* 50.625MB */
+#define KMD_SRAM_RESERVED_SIZE       0x8000                  /* 32KB */
+
+#define SRAM_BASE_ADDR_USER  (0x7FF0000000ull + KMD_SRAM_RESERVED_SIZE)
+#define SRAM_SIZE_USER               (SRAM_SIZE - KMD_SRAM_RESERVED_SIZE)
+
+#define DRAM_PHYS_BASE               0x0ull
+
+#define CPU_FW_IMAGE_SIZE    0x10000000      /* 256MB */
+#define MMU_PAGE_TABLES_SIZE 0x0E000000      /* 224MB */
+#define CPU_PQ_PKT_SIZE              0x00001000      /* 4KB */
+#define CPU_PQ_DATA_SIZE     0x01FFF000      /* 32MB - 4KB  */
+
+#define CPU_FW_IMAGE_ADDR    DRAM_PHYS_BASE
+#define MMU_PAGE_TABLES_ADDR (CPU_FW_IMAGE_ADDR + CPU_FW_IMAGE_SIZE)
+#define CPU_PQ_PKT_ADDR              (MMU_PAGE_TABLES_ADDR + MMU_PAGE_TABLES_SIZE)
+#define CPU_PQ_DATA_ADDR     (CPU_PQ_PKT_ADDR + CPU_PQ_PKT_SIZE)
+#define DRAM_BASE_ADDR_USER  (CPU_PQ_DATA_ADDR + CPU_PQ_DATA_SIZE)
+
+#define HOST_PHYS_BASE               0x8000000000ull         /* 0.5TB */
+#define HOST_PHYS_SIZE               0x1000000000000ull      /* 0.25PB (48 bits) */
+
+#define VA_HOST_SPACE_START  0x1000000000000ull      /* 256TB */
+#define VA_HOST_SPACE_END    0x3FF8000000000ull      /* 1PB - 1TB */
+#define VA_HOST_SPACE_SIZE   (VA_HOST_SPACE_END - \
+                                     VA_HOST_SPACE_START) /* 767TB */
+
+#define VA_DDR_SPACE_START   0x800000000ull          /* 32GB */
+#define VA_DDR_SPACE_END     0x2000000000ull         /* 128GB */
+#define VA_DDR_SPACE_SIZE    (VA_DDR_SPACE_END - \
+                                     VA_DDR_SPACE_START)     /* 128GB */
+
+#define CPU_BOOT_ADDR                0x7FF8040000ull
+
+#define UBOOT_FW_OFFSET              0x100000                /* 1MB in SRAM */
+#define LINUX_FW_OFFSET              0x800000                /* 8BM in DDR */
+
+#define GOYA_MSIX_ENTRIES    8
+#define EVENT_QUEUE_MSIX_IDX 5
+#define ARMCP_RESET_MSIX_IDX 6
+
+#define QMAN_PQ_ENTRY_SIZE   16                      /* Bytes */
+
+#define MAX_ASID             1024
+
+#define PROT_BITS_OFFS               0xF80
+
+/*
+ * Queue Numbering
+ *
+ * The external queues (DMA channels + CPU) MUST be before the internal queues
+ * and each group (DMA channels + CPU and internal) must be contiguous inside
+ * itself but there can be a gap between the two groups (although not
+ * recommended)
+ */
+
+enum goya_queue_id {
+     GOYA_QUEUE_ID_DMA_0 = 0,
+     GOYA_QUEUE_ID_DMA_1,
+     GOYA_QUEUE_ID_DMA_2,
+     GOYA_QUEUE_ID_DMA_3,
+     GOYA_QUEUE_ID_DMA_4,
+     GOYA_QUEUE_ID_CPU_PQ,
+     GOYA_QUEUE_ID_MME,
+     GOYA_QUEUE_ID_TPC0,
+     GOYA_QUEUE_ID_TPC1,
+     GOYA_QUEUE_ID_TPC2,
+     GOYA_QUEUE_ID_TPC3,
+     GOYA_QUEUE_ID_TPC4,
+     GOYA_QUEUE_ID_TPC5,
+     GOYA_QUEUE_ID_TPC6,
+     GOYA_QUEUE_ID_TPC7,
+     GOYA_QUEUE_ID_SIZE
+};
+
+enum goya_pll_index {
+     CPU_PLL = 0,
+     IC_PLL,
+     MC_PLL,
+     MME_PLL,
+     PCI_PLL,
+     EMMC_PLL,
+     TPC_PLL
+};
+
+#define GOYA_PLL_FREQ_LOW            50000000 /* 50 MHz */
+
+#endif /* GOYA_H */
--
Quoted 2 lines

2.17.1

--
Sincerely yours,
Mike.

#18 lore raw

Re: [PATCH 03/15] habanalabs: add basic Goya support

Quoted 22 lines · Oded Gabbay
Quoted 21 lines · Mike Rapoport
Quoted 19 lines · Oded Gabbay

This patch adds a basic support for the Goya device. The code initializes the device's PCI controller and PCI bars. It also initializes various S/W structures and adds some basic helper functions.

---
 drivers/misc/habanalabs/Makefile            |   5 +-
 drivers/misc/habanalabs/device.c            |  71 +++
 drivers/misc/habanalabs/goya/Makefile       |   3 +
 drivers/misc/habanalabs/goya/goya.c         | 633 ++++++++++++++++++++
 drivers/misc/habanalabs/goya/goyaP.h        | 125 ++++
 drivers/misc/habanalabs/habanalabs.h        | 131 ++++
 drivers/misc/habanalabs/habanalabs_drv.c    |   3 +
 drivers/misc/habanalabs/include/goya/goya.h | 115 ++++
 8 files changed, 1085 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/habanalabs/goya/Makefile
 create mode 100644 drivers/misc/habanalabs/goya/goya.c
 create mode 100644 drivers/misc/habanalabs/goya/goyaP.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya.h

[ ... ]

Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
drivers/misc/habanalabs/goya/goya.c new file +9 -0
+
+/**
+ * goya_sw_init - Goya software initialization code
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+static int goya_sw_init(struct hl_device *hdev)
+{
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c line 501

Quoted 5 lines
drivers/misc/habanalabs/goya/goya.c new file +5 -0
+     struct goya_device *goya;
+     int rc;
+
+     /* Allocate device structure */
+     goya = kzalloc(sizeof(*goya), GFP_KERNEL);

Consider using devm_k[mz]alloc() for memory allocations throughout the driver. I didn't check all the spots where it can be applicable.

Quoted 5 lines

I honestly wasn't aware of that. We never used that in AMD drivers (which where I spent most of my kernel time).
I'll look into that offline but for now I don't really want to change into it blindly in all locations, unless there is some hard kernel rule for using that in drivers.

AFAIK, there's no such rule. It's just supposed to make driver developer/maintainer life easier ;-)

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
drivers/misc/habanalabs/goya/goya.c new file +16 -0
+     if (!goya)
+             return -ENOMEM;
+
+     /* according to goya_init_iatu */
+     goya->ddr_bar_cur_addr = DRAM_PHYS_BASE;
+     hdev->asic_specific = goya;
+
+     /* Create DMA pool for small allocations */
+     hdev->dma_pool = dma_pool_create(dev_name(hdev->dev),
+                     &hdev->pdev->dev, GOYA_DMA_POOL_BLK_SIZE, 8, 0);
+     if (!hdev->dma_pool) {
+             dev_err(hdev->dev, "failed to create DMA pool\n");
+             rc = -ENOMEM;
+             goto free_goya_device;
+     }
+

[ ... ]

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
drivers/misc/habanalabs/goya/goya.c new file +7 -0
+
+static const struct hl_asic_funcs goya_funcs = {
+     .early_init = goya_early_init,
+     .early_fini = goya_early_fini,
+     .sw_init = goya_sw_init,
+     .sw_fini = goya_sw_fini,
+     .suspend = goya_suspend,
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c line 621

Quoted 3 lines
drivers/misc/habanalabs/goya/goya.c new file +3 -0
+     .resume = goya_resume,
+     .dma_alloc_coherent = goya_dma_alloc_coherent,
+     .dma_free_coherent = goya_dma_free_coherent,

Is there any additional functionality that is planned in goya or gaudi in these two functions?
It seems like they are not really needed, at least at the moment and for sure that don't need to be part of ASIC ops.

Quoted 3 lines

So this relates to the simulator support, because there the implementation of these two functions is totally different as I don't have pci device.

Can you please add a comment about it here?

Signature · 2 lines
Sincerely yours,
Mike.
#19 lore raw

Re: [PATCH 03/15] habanalabs: add basic Goya support

Quoted 89 lines · Mike Rapoport
Quoted 22 lines · Oded Gabbay
Quoted 21 lines · Mike Rapoport
Quoted 19 lines · Oded Gabbay

This patch adds a basic support for the Goya device. The code initializes the device's PCI controller and PCI bars. It also initializes various S/W structures and adds some basic helper functions.

---
 drivers/misc/habanalabs/Makefile            |   5 +-
 drivers/misc/habanalabs/device.c            |  71 +++
 drivers/misc/habanalabs/goya/Makefile       |   3 +
 drivers/misc/habanalabs/goya/goya.c         | 633 ++++++++++++++++++++
 drivers/misc/habanalabs/goya/goyaP.h        | 125 ++++
 drivers/misc/habanalabs/habanalabs.h        | 131 ++++
 drivers/misc/habanalabs/habanalabs_drv.c    |   3 +
 drivers/misc/habanalabs/include/goya/goya.h | 115 ++++
 8 files changed, 1085 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/habanalabs/goya/Makefile
 create mode 100644 drivers/misc/habanalabs/goya/goya.c
 create mode 100644 drivers/misc/habanalabs/goya/goyaP.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya.h

[ ... ]

Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
drivers/misc/habanalabs/goya/goya.c new file +9 -0
+
+/**
+ * goya_sw_init - Goya software initialization code
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+static int goya_sw_init(struct hl_device *hdev)
+{
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c line 501

Quoted 5 lines
drivers/misc/habanalabs/goya/goya.c new file +5 -0
+     struct goya_device *goya;
+     int rc;
+
+     /* Allocate device structure */
+     goya = kzalloc(sizeof(*goya), GFP_KERNEL);

Consider using devm_k[mz]alloc() for memory allocations throughout the driver. I didn't check all the spots where it can be applicable.

Quoted 5 lines

I honestly wasn't aware of that. We never used that in AMD drivers (which where I spent most of my kernel time).
I'll look into that offline but for now I don't really want to change into it blindly in all locations, unless there is some hard kernel rule for using that in drivers.

AFAIK, there's no such rule. It's just supposed to make driver developer/maintainer life easier ;-)

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
drivers/misc/habanalabs/goya/goya.c new file +16 -0
+     if (!goya)
+             return -ENOMEM;
+
+     /* according to goya_init_iatu */
+     goya->ddr_bar_cur_addr = DRAM_PHYS_BASE;
+     hdev->asic_specific = goya;
+
+     /* Create DMA pool for small allocations */
+     hdev->dma_pool = dma_pool_create(dev_name(hdev->dev),
+                     &hdev->pdev->dev, GOYA_DMA_POOL_BLK_SIZE, 8, 0);
+     if (!hdev->dma_pool) {
+             dev_err(hdev->dev, "failed to create DMA pool\n");
+             rc = -ENOMEM;
+             goto free_goya_device;
+     }
+

[ ... ]

Quoted 16 lines
Quoted 15 lines

drivers/misc/habanalabs/goya/goya.c line 621

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c new file +10 -0
+
+static const struct hl_asic_funcs goya_funcs = {
+     .early_init = goya_early_init,
+     .early_fini = goya_early_fini,
+     .sw_init = goya_sw_init,
+     .sw_fini = goya_sw_fini,
+     .suspend = goya_suspend,
+     .resume = goya_resume,
+     .dma_alloc_coherent = goya_dma_alloc_coherent,
+     .dma_free_coherent = goya_dma_free_coherent,

Is there any additional functionality that is planned in goya or gaudi in these two functions?
It seems like they are not really needed, at least at the moment and for sure that don't need to be part of ASIC ops.

Quoted 5 lines
Quoted 3 lines

So this relates to the simulator support, because there the implementation of these two functions is totally different as I don't have pci device.

Can you please add a comment about it here?

Of course, done.
Thanks,
Oded

Quoted 5 lines

--
Sincerely yours,
Mike.

#20 lore raw

PATCH 4/15 habanalabs: add context and ASID modules

In reply to Oded Gabbay

This patch adds two modules - ASID and context.

Each user process the opens a device's file must have at least one context before it is able to "work" with the device. Each context has its own device address-space and contains information about its runtime state (its active command submissions).

To have address-space separation between contexts, each context is assigned a unique ASID, which stands for "address-space id". Goya supports up to 1024 ASIDs.

Currently, the driver doesn't support multiple contexts. Therefore, the user doesn't need to actively create a context. A "primary context" is created automatically when the user opens the device's file.

---
 drivers/misc/habanalabs/Makefile         |   2 +-
 drivers/misc/habanalabs/asid.c           |  58 +++++++++
 drivers/misc/habanalabs/context.c        | 155 +++++++++++++++++++++++
 drivers/misc/habanalabs/device.c         |  47 +++++++
 drivers/misc/habanalabs/habanalabs.h     |  70 ++++++++++
 drivers/misc/habanalabs/habanalabs_drv.c |  46 ++++++-
 6 files changed, 375 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/habanalabs/asid.c
 create mode 100644 drivers/misc/habanalabs/context.c
drivers/misc/habanalabs/Makefile +1 -1
index 6f1ead69bd77..3ffbadc2ca01 100644
@@ -4,7 +4,7 @@
 
 obj-m	:= habanalabs.o
 
-habanalabs-y := habanalabs_drv.o device.o
+habanalabs-y := habanalabs_drv.o device.o context.o asid.o
 
 include $(src)/goya/Makefile
 habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/asid.c new file +58 -0

58 lines not on this page · raw message

drivers/misc/habanalabs/context.c new file +155 -0

155 lines not on this page · raw message

drivers/misc/habanalabs/device.c +47 -0

92 lines not on this page · raw message

drivers/misc/habanalabs/habanalabs.h +70 -0

124 lines not on this page · raw message

drivers/misc/habanalabs/habanalabs_drv.c +44 -2

68 lines not on this page · raw message

Signature · 1 line
2.17.1
#21 lore raw

Re: [PATCH 04/15] habanalabs: add context and ASID modules

Quoted 3 lines · Oded Gabbay

This patch adds two modules - ASID and context.

Each user process the opens a device's file must have at least one context

^that

Quoted 433 lines

before it is able to "work" with the device. Each context has its own device address-space and contains information about its runtime state (its active command submissions).

To have address-space separation between contexts, each context is assigned a unique ASID, which stands for "address-space id". Goya supports up to 1024 ASIDs.

Currently, the driver doesn't support multiple contexts. Therefore, the user doesn't need to actively create a context. A "primary context" is created automatically when the user opens the device's file.

---
 drivers/misc/habanalabs/Makefile         |   2 +-
 drivers/misc/habanalabs/asid.c           |  58 +++++++++
 drivers/misc/habanalabs/context.c        | 155 +++++++++++++++++++++++
 drivers/misc/habanalabs/device.c         |  47 +++++++
 drivers/misc/habanalabs/habanalabs.h     |  70 ++++++++++
 drivers/misc/habanalabs/habanalabs_drv.c |  46 ++++++-
 6 files changed, 375 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/habanalabs/asid.c
 create mode 100644 drivers/misc/habanalabs/context.c
drivers/misc/habanalabs/Makefile +1 -1
index 6f1ead69bd77..3ffbadc2ca01 100644
@@ -4,7 +4,7 @@
 
 obj-m	:= habanalabs.o
 
-habanalabs-y := habanalabs_drv.o device.o
+habanalabs-y := habanalabs_drv.o device.o context.o asid.o
 
 include $(src)/goya/Makefile
 habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/asid.c new file +58 -0
new file mode 100644
index 000000000000..0ce84c8f5a47
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "habanalabs.h"
+
+#include <linux/slab.h>
+#include <linux/types.h>
+
+int hl_asid_init(struct hl_device *hdev)
+{
+	hdev->asid_bitmap = kcalloc(BITS_TO_LONGS(hdev->asic_prop.max_asid),
+					sizeof(*hdev->asid_bitmap), GFP_KERNEL);
+	if (!hdev->asid_bitmap)
+		return -ENOMEM;
+
+	mutex_init(&hdev->asid_mutex);
+
+	/* ASID 0 is reserved for KMD */
+	set_bit(0, hdev->asid_bitmap);
+
+	return 0;
+}
+
+void hl_asid_fini(struct hl_device *hdev)
+{
+	mutex_destroy(&hdev->asid_mutex);
+	kfree(hdev->asid_bitmap);
+}
+
+unsigned long hl_asid_alloc(struct hl_device *hdev)
+{
+	unsigned long found;
+
+	mutex_lock(&hdev->asid_mutex);
+
+	found = find_first_zero_bit(hdev->asid_bitmap,
+					hdev->asic_prop.max_asid);
+	if (found == hdev->asic_prop.max_asid)
+		found = 0;
+	else
+		set_bit(found, hdev->asid_bitmap);
+
+	mutex_unlock(&hdev->asid_mutex);
+
+	return found;
+}
+
+void hl_asid_free(struct hl_device *hdev, unsigned long asid)
+{
+	if (WARN((asid == 0 || asid >= hdev->asic_prop.max_asid),
+						"Invalid ASID %lu", asid))
+		return;
+	clear_bit(asid, hdev->asid_bitmap);
+}
drivers/misc/habanalabs/context.c new file +155 -0
new file mode 100644
index 000000000000..cdcad077e5cf
@@ -0,0 +1,155 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "habanalabs.h"
+
+#include <linux/sched.h>
+#include <linux/delay.h>
+
+static void hl_ctx_fini(struct hl_ctx *ctx)
+{
+	struct hl_device *hdev = ctx->hdev;
+
+	if (ctx->asid != HL_KERNEL_ASID_ID)
+		hl_asid_free(hdev, ctx->asid);
+}
+
+void hl_ctx_do_release(struct kref *ref)
+{
+	struct hl_ctx *ctx;
+
+	ctx = container_of(ref, struct hl_ctx, refcount);
+
+	dev_dbg(ctx->hdev->dev, "Now really releasing context %d\n", ctx->asid);
+
+	hl_ctx_fini(ctx);
+
+	if (ctx->hpriv)
+		hl_hpriv_put(ctx->hpriv);
+
+	kfree(ctx);
+}
+
+int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv)
+{
+	struct hl_ctx_mgr *mgr = &hpriv->ctx_mgr;
+	struct hl_ctx *ctx;
+	int rc;
+
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+	if (!ctx) {
+		rc = -ENOMEM;
+		goto out_err;
+	}
+
+	rc = hl_ctx_init(hdev, ctx, false);
+	if (rc)
+		goto free_ctx;
+
+	hl_hpriv_get(hpriv);
+	ctx->hpriv = hpriv;
+
+	/* TODO: remove for multiple contexts */
+	hpriv->ctx = ctx;
+	hdev->user_ctx = ctx;
+
+	mutex_lock(&mgr->ctx_lock);
+	rc = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
+	mutex_unlock(&mgr->ctx_lock);
+
+	if (rc < 0) {
+		dev_err(hdev->dev, "Failed to allocate IDR for a new CTX\n");
+		hl_ctx_free(hdev, ctx);
+		goto out_err;
+	}
+
+	return 0;
+
+free_ctx:
+	kfree(ctx);
+out_err:
+	return rc;
+}
+
+void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx)
+{
+	if (kref_put(&ctx->refcount, hl_ctx_do_release) == 1)
+		return;
+
+	dev_warn(hdev->dev,
+		"Context %d closed or terminated but its CS are executing\n",
+		ctx->asid);
+}
+
+int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx)
+{
+	ctx->hdev = hdev;
+
+	kref_init(&ctx->refcount);
+
+	if (is_kernel_ctx) {
+		ctx->asid = HL_KERNEL_ASID_ID; /* KMD gets ASID 0 */
+	} else {
+		ctx->asid = hl_asid_alloc(hdev);
+		if (!ctx->asid) {
+			dev_err(hdev->dev, "No free ASID, failed to create context\n");
+			return -ENOMEM;
+		}
+	}
+
+	dev_dbg(hdev->dev, "Created context with ASID %u\n", ctx->asid);
+
+	return 0;
+}
+
+void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx)
+{
+	kref_get(&ctx->refcount);
+}
+
+int hl_ctx_put(struct hl_ctx *ctx)
+{
+	return kref_put(&ctx->refcount, hl_ctx_do_release);
+}
+
+/**
+ * hl_ctx_mgr_init - initialize the context manager
+ *
+ * @mgr: pointer to context manager structure
+ *
+ * This manager is an object inside the hpriv object of the user process.
+ * The function is called when a user process opens the FD.
+ */
+void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr)
+{
+	mutex_init(&mgr->ctx_lock);
+	idr_init(&mgr->ctx_handles);
+}
+
+/**
+ * hl_ctx_mgr_fini - finalize the context manager
+ *
+ * @hdev: pointer to device structure
+ * @mgr: pointer to context manager structure
+ *
+ * This function goes over all the contexts in the manager and frees them.
+ * It is called when a process closes the FD.
+ */
+void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr)
+{
+	struct hl_ctx *ctx;
+	struct idr *idp;
+	u32 id;
+
+	idp = &mgr->ctx_handles;
+
+	idr_for_each_entry(idp, ctx, id)
+		hl_ctx_free(hdev, ctx);
+
+	idr_destroy(&mgr->ctx_handles);
+	mutex_destroy(&mgr->ctx_lock);
+}
drivers/misc/habanalabs/device.c +47 -0
index a4276ef559b3..84ce9fcb52da 100644
@@ -23,6 +23,12 @@ static void hpriv_release(struct kref *ref)
 	put_pid(hpriv->taskpid);
 
 	kfree(hpriv);
+
+	/* Now the FD is really closed */
+	atomic_dec(&hdev->fd_open_cnt);
+
+	/* This allows a new user context to open the device */
+	hdev->user_ctx = NULL;
 }
 
 void hl_hpriv_get(struct hl_fpriv *hpriv)
@@ -47,6 +53,8 @@ static int hl_device_release(struct inode *inode, struct file *filp)
 {
 	struct hl_fpriv *hpriv = filp->private_data;
 
+	hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
+
 	filp->private_data = NULL;
 
 	hl_hpriv_put(hpriv);
@@ -133,7 +141,20 @@ static int device_early_init(struct hl_device *hdev)
 	if (rc)
 		return rc;
 
+	rc = hl_asid_init(hdev);
+	if (rc)
+		goto early_fini;
+
+	mutex_init(&hdev->device_open);
+	atomic_set(&hdev->fd_open_cnt, 0);
+
 	return 0;
+
+early_fini:
+	if (hdev->asic_funcs->early_fini)
+		hdev->asic_funcs->early_fini(hdev);
+
+	return rc;
 }
 
 /**
@@ -145,9 +166,12 @@ static int device_early_init(struct hl_device *hdev)
 static void device_early_fini(struct hl_device *hdev)
 {
 
+	hl_asid_fini(hdev);
+
 	if (hdev->asic_funcs->early_fini)
 		hdev->asic_funcs->early_fini(hdev);
 
+	mutex_destroy(&hdev->device_open);
 }
 
 /**
@@ -241,11 +265,30 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)
 	if (rc)
 		goto early_fini;
 
+	/* Allocate the kernel context */
+	hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL);
+	if (!hdev->kernel_ctx) {
+		rc = -ENOMEM;
+		goto sw_fini;
+	}
+
+	hdev->user_ctx = NULL;
+
+	rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
+	if (rc) {
+		dev_err(hdev->dev, "failed to initialize kernel context\n");
+		goto free_ctx;
+	}
+
 	dev_notice(hdev->dev,
 		"Successfully added device to habanalabs driver\n");
 
 	return 0;
 
+free_ctx:
+	kfree(hdev->kernel_ctx);
+sw_fini:
+	hdev->asic_funcs->sw_fini(hdev);
 early_fini:
 	device_early_fini(hdev);
 release_device:
@@ -278,6 +321,10 @@ void hl_device_fini(struct hl_device *hdev)
 	/* Mark device as disabled */
 	hdev->disabled = true;
 
+	/* Release kernel context */
+	if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
+		dev_err(hdev->dev, "kernel ctx is still alive\n");
+
 	/* Call ASIC S/W finalize function */
 	hdev->asic_funcs->sw_fini(hdev);
 
drivers/misc/habanalabs/habanalabs.h +43 -0
index 97844825f7a8..d003a6af2131 100644
@@ -125,6 +125,45 @@ struct hl_asic_funcs {
 					void *cpu_addr, dma_addr_t dma_handle);
 };
 
+
+
+
+
+/*
+ * CONTEXTS
+ */
+
+#define HL_KERNEL_ASID_ID	0
+
+/**
+ * struct hl_ctx - user/kernel context.
+ * @hpriv: pointer to the private (KMD) data of the process (fd).
+ * @hdev: pointer to the device structure.
+ * @refcount: reference counter for the context. Context is released only when
+ *		this hits 0l. It is incremented on CS and CS_WAIT.
+ * @asid: context's unique address space ID in the device's MMU.
+ */
+struct hl_ctx {
+	struct hl_fpriv		*hpriv;
+	struct hl_device	*hdev;
+	struct kref		refcount;
+	u32			asid;
+};
+
+/**
+ * struct hl_ctx_mgr - for handling multiple contexts.
+ * @ctx_lock: protects ctx_handles.
+ * @ctx_handles: idr to hold all ctx handles.
+ */
+struct hl_ctx_mgr {
+	struct mutex		ctx_lock;
+	struct idr		ctx_handles;
+};
+
+
+
+
+
 /*
  * FILE PRIVATE STRUCTURE
  */
@@ -134,12 +173,16 @@ struct hl_asic_funcs {
  * @hdev: habanalabs device structure.
  * @filp: pointer to the given file structure.
  * @taskpid: current process ID.
+ * @ctx: current executing context.
+ * @ctx_mgr: context manager to handle multiple context for this FD.
  * @refcount: number of related contexts.
  */
 struct hl_fpriv {
 	struct hl_device	*hdev;
 	struct file		*filp;
 	struct pid		*taskpid;
+	struct hl_ctx		*ctx; /* TODO: remove for multiple ctx */
+	struct hl_ctx_mgr	ctx_mgr;
 	struct kref		refcount;
 };
 
@@ -195,13 +238,19 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
  * @dev: realted kernel basic device structure.

drivers/misc/habanalabs/habanalabs.h void hl_wreg(struct hl_device *hdev, u32 reg, u32 val); · line 248

Quoted 10 lines
drivers/misc/habanalabs/habanalabs.h +4 -0
  * @asic_name: ASIC specific nmae.
  * @asic_type: ASIC specific type.
+ * @kernel_ctx: KMD context structure.
  * @dma_pool: DMA pool for small allocations.
  * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address.
  * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address.
  * @cpu_accessible_dma_pool: KMD <-> ArmCP shared memory pool.
+ * @asid_bitmap: holds used/available ASIDs.
+ * @asid_mutex: protects asid_bitmap.
+ * @device_open: lock for sanity checks upon FD open.

device_open is an ambiguous name for a lock

Quoted 132 lines
drivers/misc/habanalabs/habanalabs.h +23 -0
  * @asic_prop: ASIC specific immutable properties.
  * @asic_funcs: ASIC specific functions.
  * @asic_specific: ASIC specific information to use only from ASIC files.
+ * @user_ctx: current user context executing.
+ * @fd_open_cnt: number of open context executing.
  * @major: habanalabs KMD major.
  * @id: device minor.
  * @disabled: is device disabled.
@@ -214,13 +263,21 @@ struct hl_device {
 	struct device			*dev;
 	char				asic_name[16];
 	enum hl_asic_type		asic_type;
+	struct hl_ctx			*kernel_ctx;
 	struct dma_pool			*dma_pool;
 	void				*cpu_accessible_dma_mem;
 	dma_addr_t			cpu_accessible_dma_address;
 	struct gen_pool			*cpu_accessible_dma_pool;
+	unsigned long			*asid_bitmap;
+	struct mutex			asid_mutex;
+	/* TODO: change to rw_sem for multiple contexts (same as other IOCTL) */
+	struct mutex			device_open;
 	struct asic_fixed_properties	asic_prop;
 	const struct hl_asic_funcs	*asic_funcs;
 	void				*asic_specific;
+	/* TODO: The following fields should be moved for multi-context */
+	struct hl_ctx			*user_ctx;
+	atomic_t			fd_open_cnt;
 	u32				major;
 	u16				id;
 	u8				disabled;
@@ -270,10 +327,23 @@ int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr, u32 timeout_us,
 int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
 				u32 timeout_us, u32 *val);
 
+int hl_asid_init(struct hl_device *hdev);
+void hl_asid_fini(struct hl_device *hdev);
+unsigned long hl_asid_alloc(struct hl_device *hdev);
+void hl_asid_free(struct hl_device *hdev, unsigned long asid);
+
+int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv);
+void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx);
+int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx);
+int hl_ctx_put(struct hl_ctx *ctx);
+void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
+void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
 int hl_device_init(struct hl_device *hdev, struct class *hclass);
 void hl_device_fini(struct hl_device *hdev);
 int hl_device_suspend(struct hl_device *hdev);
 int hl_device_resume(struct hl_device *hdev);
+void hl_hpriv_get(struct hl_fpriv *hpriv);
+void hl_hpriv_put(struct hl_fpriv *hpriv);
 
 void goya_set_asic_funcs(struct hl_device *hdev);
 
drivers/misc/habanalabs/habanalabs_drv.c +44 -2
index 79545003b7c2..0646da83eb53 100644
@@ -77,6 +77,7 @@ int hl_device_open(struct inode *inode, struct file *filp)
 {
 	struct hl_device *hdev;
 	struct hl_fpriv *hpriv;
+	int rc;
 
 	mutex_lock(&hl_devs_idr_lock);
 	hdev = idr_find(&hl_devs_idr, iminor(inode));
@@ -88,9 +89,33 @@ int hl_device_open(struct inode *inode, struct file *filp)
 		return -ENXIO;
 	}
 
+	mutex_lock(&hdev->device_open);
+
+	if (hdev->disabled) {
+		dev_err_ratelimited(hdev->dev,
+			"Can't open %s because it is disabled\n",
+			dev_name(hdev->dev));
+		mutex_unlock(&hdev->device_open);
+		return -EPERM;
+	}
+
+	if (hdev->user_ctx) {
+		dev_info_ratelimited(hdev->dev,
+			"Device %s is already attached to application\n",
+			dev_name(hdev->dev));
+		mutex_unlock(&hdev->device_open);
+		return -EBUSY;
+	}
+
+	atomic_inc(&hdev->fd_open_cnt);
+
+	mutex_unlock(&hdev->device_open);
+
 	hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
-	if (!hpriv)
-		return -ENOMEM;
+	if (!hpriv) {
+		rc = -ENOMEM;
+		goto close_device;
+	}
 
 	hpriv->hdev = hdev;
 	filp->private_data = hpriv;
@@ -98,9 +123,26 @@ int hl_device_open(struct inode *inode, struct file *filp)
 	kref_init(&hpriv->refcount);
 	nonseekable_open(inode, filp);
 
+	hl_ctx_mgr_init(&hpriv->ctx_mgr);
+
+	rc = hl_ctx_create(hdev, hpriv);
+	if (rc) {
+		dev_err(hdev->dev, "Failed to open FD (CTX fail)\n");
+		goto out_err;
+	}
+
 	hpriv->taskpid = find_get_pid(current->pid);
 
 	return 0;
+
+out_err:
+	filp->private_data = NULL;
+	hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
+	kfree(hpriv);
+
+close_device:
+	atomic_dec(&hdev->fd_open_cnt);
+	return rc;
 }
 
 /**
Signature · 1 line
2.17.1
Signature · 2 lines
Sincerely yours,
Mike.
#22 lore raw

Re: [PATCH 04/15] habanalabs: add context and ASID modules

Quoted 582 lines · Mike Rapoport
Quoted 3 lines · Oded Gabbay

This patch adds two modules - ASID and context.

Each user process the opens a device's file must have at least one context

^that

Quoted 433 lines

before it is able to "work" with the device. Each context has its own device address-space and contains information about its runtime state (its active command submissions).

To have address-space separation between contexts, each context is assigned a unique ASID, which stands for "address-space id". Goya supports up to 1024 ASIDs.

Currently, the driver doesn't support multiple contexts. Therefore, the user doesn't need to actively create a context. A "primary context" is created automatically when the user opens the device's file.

---
 drivers/misc/habanalabs/Makefile         |   2 +-
 drivers/misc/habanalabs/asid.c           |  58 +++++++++
 drivers/misc/habanalabs/context.c        | 155 +++++++++++++++++++++++
 drivers/misc/habanalabs/device.c         |  47 +++++++
 drivers/misc/habanalabs/habanalabs.h     |  70 ++++++++++
 drivers/misc/habanalabs/habanalabs_drv.c |  46 ++++++-
 6 files changed, 375 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/habanalabs/asid.c
 create mode 100644 drivers/misc/habanalabs/context.c
drivers/misc/habanalabs/Makefile +1 -1
index 6f1ead69bd77..3ffbadc2ca01 100644
@@ -4,7 +4,7 @@
 
 obj-m        := habanalabs.o
 
-habanalabs-y := habanalabs_drv.o device.o
+habanalabs-y := habanalabs_drv.o device.o context.o asid.o
 
 include $(src)/goya/Makefile
 habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/asid.c new file +58 -0
new file mode 100644
index 000000000000..0ce84c8f5a47
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "habanalabs.h"
+
+#include <linux/slab.h>
+#include <linux/types.h>
+
+int hl_asid_init(struct hl_device *hdev)
+{
+     hdev->asid_bitmap = kcalloc(BITS_TO_LONGS(hdev->asic_prop.max_asid),
+                                     sizeof(*hdev->asid_bitmap), GFP_KERNEL);
+     if (!hdev->asid_bitmap)
+             return -ENOMEM;
+
+     mutex_init(&hdev->asid_mutex);
+
+     /* ASID 0 is reserved for KMD */
+     set_bit(0, hdev->asid_bitmap);
+
+     return 0;
+}
+
+void hl_asid_fini(struct hl_device *hdev)
+{
+     mutex_destroy(&hdev->asid_mutex);
+     kfree(hdev->asid_bitmap);
+}
+
+unsigned long hl_asid_alloc(struct hl_device *hdev)
+{
+     unsigned long found;
+
+     mutex_lock(&hdev->asid_mutex);
+
+     found = find_first_zero_bit(hdev->asid_bitmap,
+                                     hdev->asic_prop.max_asid);
+     if (found == hdev->asic_prop.max_asid)
+             found = 0;
+     else
+             set_bit(found, hdev->asid_bitmap);
+
+     mutex_unlock(&hdev->asid_mutex);
+
+     return found;
+}
+
+void hl_asid_free(struct hl_device *hdev, unsigned long asid)
+{
+     if (WARN((asid == 0 || asid >= hdev->asic_prop.max_asid),
+                                             "Invalid ASID %lu", asid))
+             return;
+     clear_bit(asid, hdev->asid_bitmap);
+}
drivers/misc/habanalabs/context.c new file +155 -0
new file mode 100644
index 000000000000..cdcad077e5cf
@@ -0,0 +1,155 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "habanalabs.h"
+
+#include <linux/sched.h>
+#include <linux/delay.h>
+
+static void hl_ctx_fini(struct hl_ctx *ctx)
+{
+     struct hl_device *hdev = ctx->hdev;
+
+     if (ctx->asid != HL_KERNEL_ASID_ID)
+             hl_asid_free(hdev, ctx->asid);
+}
+
+void hl_ctx_do_release(struct kref *ref)
+{
+     struct hl_ctx *ctx;
+
+     ctx = container_of(ref, struct hl_ctx, refcount);
+
+     dev_dbg(ctx->hdev->dev, "Now really releasing context %d\n", ctx->asid);
+
+     hl_ctx_fini(ctx);
+
+     if (ctx->hpriv)
+             hl_hpriv_put(ctx->hpriv);
+
+     kfree(ctx);
+}
+
+int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv)
+{
+     struct hl_ctx_mgr *mgr = &hpriv->ctx_mgr;
+     struct hl_ctx *ctx;
+     int rc;
+
+     ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+     if (!ctx) {
+             rc = -ENOMEM;
+             goto out_err;
+     }
+
+     rc = hl_ctx_init(hdev, ctx, false);
+     if (rc)
+             goto free_ctx;
+
+     hl_hpriv_get(hpriv);
+     ctx->hpriv = hpriv;
+
+     /* TODO: remove for multiple contexts */
+     hpriv->ctx = ctx;
+     hdev->user_ctx = ctx;
+
+     mutex_lock(&mgr->ctx_lock);
+     rc = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
+     mutex_unlock(&mgr->ctx_lock);
+
+     if (rc < 0) {
+             dev_err(hdev->dev, "Failed to allocate IDR for a new CTX\n");
+             hl_ctx_free(hdev, ctx);
+             goto out_err;
+     }
+
+     return 0;
+
+free_ctx:
+     kfree(ctx);
+out_err:
+     return rc;
+}
+
+void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx)
+{
+     if (kref_put(&ctx->refcount, hl_ctx_do_release) == 1)
+             return;
+
+     dev_warn(hdev->dev,
+             "Context %d closed or terminated but its CS are executing\n",
+             ctx->asid);
+}
+
+int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx)
+{
+     ctx->hdev = hdev;
+
+     kref_init(&ctx->refcount);
+
+     if (is_kernel_ctx) {
+             ctx->asid = HL_KERNEL_ASID_ID; /* KMD gets ASID 0 */
+     } else {
+             ctx->asid = hl_asid_alloc(hdev);
+             if (!ctx->asid) {
+                     dev_err(hdev->dev, "No free ASID, failed to create context\n");
+                     return -ENOMEM;
+             }
+     }
+
+     dev_dbg(hdev->dev, "Created context with ASID %u\n", ctx->asid);
+
+     return 0;
+}
+
+void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx)
+{
+     kref_get(&ctx->refcount);
+}
+
+int hl_ctx_put(struct hl_ctx *ctx)
+{
+     return kref_put(&ctx->refcount, hl_ctx_do_release);
+}
+
+/**
+ * hl_ctx_mgr_init - initialize the context manager
+ *
+ * @mgr: pointer to context manager structure
+ *
+ * This manager is an object inside the hpriv object of the user process.
+ * The function is called when a user process opens the FD.
+ */
+void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr)
+{
+     mutex_init(&mgr->ctx_lock);
+     idr_init(&mgr->ctx_handles);
+}
+
+/**
+ * hl_ctx_mgr_fini - finalize the context manager
+ *
+ * @hdev: pointer to device structure
+ * @mgr: pointer to context manager structure
+ *
+ * This function goes over all the contexts in the manager and frees them.
+ * It is called when a process closes the FD.
+ */
+void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr)
+{
+     struct hl_ctx *ctx;
+     struct idr *idp;
+     u32 id;
+
+     idp = &mgr->ctx_handles;
+
+     idr_for_each_entry(idp, ctx, id)
+             hl_ctx_free(hdev, ctx);
+
+     idr_destroy(&mgr->ctx_handles);
+     mutex_destroy(&mgr->ctx_lock);
+}
drivers/misc/habanalabs/device.c +47 -0
index a4276ef559b3..84ce9fcb52da 100644
@@ -23,6 +23,12 @@ static void hpriv_release(struct kref *ref)
      put_pid(hpriv->taskpid);
 
      kfree(hpriv);
+
+     /* Now the FD is really closed */
+     atomic_dec(&hdev->fd_open_cnt);
+
+     /* This allows a new user context to open the device */
+     hdev->user_ctx = NULL;
 }
 
 void hl_hpriv_get(struct hl_fpriv *hpriv)
@@ -47,6 +53,8 @@ static int hl_device_release(struct inode *inode, struct file *filp)
 {
      struct hl_fpriv *hpriv = filp->private_data;
 
+     hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
+
      filp->private_data = NULL;
 
      hl_hpriv_put(hpriv);
@@ -133,7 +141,20 @@ static int device_early_init(struct hl_device *hdev)
      if (rc)
              return rc;
 
+     rc = hl_asid_init(hdev);
+     if (rc)
+             goto early_fini;
+
+     mutex_init(&hdev->device_open);
+     atomic_set(&hdev->fd_open_cnt, 0);
+
      return 0;
+
+early_fini:
+     if (hdev->asic_funcs->early_fini)
+             hdev->asic_funcs->early_fini(hdev);
+
+     return rc;
 }
 
 /**
@@ -145,9 +166,12 @@ static int device_early_init(struct hl_device *hdev)
 static void device_early_fini(struct hl_device *hdev)
 {
 
+     hl_asid_fini(hdev);
+
      if (hdev->asic_funcs->early_fini)
              hdev->asic_funcs->early_fini(hdev);
 
+     mutex_destroy(&hdev->device_open);
 }
 
 /**
@@ -241,11 +265,30 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)
      if (rc)
              goto early_fini;
 
+     /* Allocate the kernel context */
+     hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL);
+     if (!hdev->kernel_ctx) {
+             rc = -ENOMEM;
+             goto sw_fini;
+     }
+
+     hdev->user_ctx = NULL;
+
+     rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
+     if (rc) {
+             dev_err(hdev->dev, "failed to initialize kernel context\n");
+             goto free_ctx;
+     }
+
      dev_notice(hdev->dev,
              "Successfully added device to habanalabs driver\n");
 
      return 0;
 
+free_ctx:
+     kfree(hdev->kernel_ctx);
+sw_fini:
+     hdev->asic_funcs->sw_fini(hdev);
 early_fini:
      device_early_fini(hdev);
 release_device:
@@ -278,6 +321,10 @@ void hl_device_fini(struct hl_device *hdev)
      /* Mark device as disabled */
      hdev->disabled = true;
 
+     /* Release kernel context */
+     if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
+             dev_err(hdev->dev, "kernel ctx is still alive\n");
+
      /* Call ASIC S/W finalize function */
      hdev->asic_funcs->sw_fini(hdev);
 
drivers/misc/habanalabs/habanalabs.h +43 -0
index 97844825f7a8..d003a6af2131 100644
@@ -125,6 +125,45 @@ struct hl_asic_funcs {
                                      void *cpu_addr, dma_addr_t dma_handle);
 };
 
+
+
+
+
+/*
+ * CONTEXTS
+ */
+
+#define HL_KERNEL_ASID_ID    0
+
+/**
+ * struct hl_ctx - user/kernel context.
+ * @hpriv: pointer to the private (KMD) data of the process (fd).
+ * @hdev: pointer to the device structure.
+ * @refcount: reference counter for the context. Context is released only when
+ *           this hits 0l. It is incremented on CS and CS_WAIT.
+ * @asid: context's unique address space ID in the device's MMU.
+ */
+struct hl_ctx {
+     struct hl_fpriv         *hpriv;
+     struct hl_device        *hdev;
+     struct kref             refcount;
+     u32                     asid;
+};
+
+/**
+ * struct hl_ctx_mgr - for handling multiple contexts.
+ * @ctx_lock: protects ctx_handles.
+ * @ctx_handles: idr to hold all ctx handles.
+ */
+struct hl_ctx_mgr {
+     struct mutex            ctx_lock;
+     struct idr              ctx_handles;
+};
+
+
+
+
+
 /*
  * FILE PRIVATE STRUCTURE
  */
@@ -134,12 +173,16 @@ struct hl_asic_funcs {
  * @hdev: habanalabs device structure.
  * @filp: pointer to the given file structure.
  * @taskpid: current process ID.
+ * @ctx: current executing context.
+ * @ctx_mgr: context manager to handle multiple context for this FD.
  * @refcount: number of related contexts.
  */
 struct hl_fpriv {
      struct hl_device        *hdev;
      struct file             *filp;
      struct pid              *taskpid;
+     struct hl_ctx           *ctx; /* TODO: remove for multiple ctx */
+     struct hl_ctx_mgr       ctx_mgr;
      struct kref             refcount;
 };
 
@@ -195,13 +238,19 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
  * @dev: realted kernel basic device structure.

drivers/misc/habanalabs/habanalabs.h void hl_wreg(struct hl_device *hdev, u32 reg, u32 val); · line 248

Quoted 10 lines
drivers/misc/habanalabs/habanalabs.h +4 -0
  * @asic_name: ASIC specific nmae.
  * @asic_type: ASIC specific type.
+ * @kernel_ctx: KMD context structure.
  * @dma_pool: DMA pool for small allocations.
  * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address.
  * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address.
  * @cpu_accessible_dma_pool: KMD <-> ArmCP shared memory pool.
+ * @asid_bitmap: holds used/available ASIDs.
+ * @asid_mutex: protects asid_bitmap.
+ * @device_open: lock for sanity checks upon FD open.

device_open is an ambiguous name for a lock

Quoted 128 lines
drivers/misc/habanalabs/habanalabs.h +23 -0
  * @asic_prop: ASIC specific immutable properties.
  * @asic_funcs: ASIC specific functions.
  * @asic_specific: ASIC specific information to use only from ASIC files.
+ * @user_ctx: current user context executing.
+ * @fd_open_cnt: number of open context executing.
  * @major: habanalabs KMD major.
  * @id: device minor.
  * @disabled: is device disabled.
@@ -214,13 +263,21 @@ struct hl_device {
      struct device                   *dev;
      char                            asic_name[16];
      enum hl_asic_type               asic_type;
+     struct hl_ctx                   *kernel_ctx;
      struct dma_pool                 *dma_pool;
      void                            *cpu_accessible_dma_mem;
      dma_addr_t                      cpu_accessible_dma_address;
      struct gen_pool                 *cpu_accessible_dma_pool;
+     unsigned long                   *asid_bitmap;
+     struct mutex                    asid_mutex;
+     /* TODO: change to rw_sem for multiple contexts (same as other IOCTL) */
+     struct mutex                    device_open;
      struct asic_fixed_properties    asic_prop;
      const struct hl_asic_funcs      *asic_funcs;
      void                            *asic_specific;
+     /* TODO: The following fields should be moved for multi-context */
+     struct hl_ctx                   *user_ctx;
+     atomic_t                        fd_open_cnt;
      u32                             major;
      u16                             id;
      u8                              disabled;
@@ -270,10 +327,23 @@ int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr, u32 timeout_us,
 int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
                              u32 timeout_us, u32 *val);
 
+int hl_asid_init(struct hl_device *hdev);
+void hl_asid_fini(struct hl_device *hdev);
+unsigned long hl_asid_alloc(struct hl_device *hdev);
+void hl_asid_free(struct hl_device *hdev, unsigned long asid);
+
+int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv);
+void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx);
+int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx);
+int hl_ctx_put(struct hl_ctx *ctx);
+void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
+void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
 int hl_device_init(struct hl_device *hdev, struct class *hclass);
 void hl_device_fini(struct hl_device *hdev);
 int hl_device_suspend(struct hl_device *hdev);
 int hl_device_resume(struct hl_device *hdev);
+void hl_hpriv_get(struct hl_fpriv *hpriv);
+void hl_hpriv_put(struct hl_fpriv *hpriv);
 
 void goya_set_asic_funcs(struct hl_device *hdev);
 
drivers/misc/habanalabs/habanalabs_drv.c +44 -2
index 79545003b7c2..0646da83eb53 100644
@@ -77,6 +77,7 @@ int hl_device_open(struct inode *inode, struct file *filp)
 {
      struct hl_device *hdev;
      struct hl_fpriv *hpriv;
+     int rc;
 
      mutex_lock(&hl_devs_idr_lock);
      hdev = idr_find(&hl_devs_idr, iminor(inode));
@@ -88,9 +89,33 @@ int hl_device_open(struct inode *inode, struct file *filp)
              return -ENXIO;
      }
 
+     mutex_lock(&hdev->device_open);
+
+     if (hdev->disabled) {
+             dev_err_ratelimited(hdev->dev,
+                     "Can't open %s because it is disabled\n",
+                     dev_name(hdev->dev));
+             mutex_unlock(&hdev->device_open);
+             return -EPERM;
+     }
+
+     if (hdev->user_ctx) {
+             dev_info_ratelimited(hdev->dev,
+                     "Device %s is already attached to application\n",
+                     dev_name(hdev->dev));
+             mutex_unlock(&hdev->device_open);
+             return -EBUSY;
+     }
+
+     atomic_inc(&hdev->fd_open_cnt);
+
+     mutex_unlock(&hdev->device_open);
+
      hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
-     if (!hpriv)
-             return -ENOMEM;
+     if (!hpriv) {
+             rc = -ENOMEM;
+             goto close_device;
+     }
 
      hpriv->hdev = hdev;
      filp->private_data = hpriv;
@@ -98,9 +123,26 @@ int hl_device_open(struct inode *inode, struct file *filp)
      kref_init(&hpriv->refcount);
      nonseekable_open(inode, filp);
 
+     hl_ctx_mgr_init(&hpriv->ctx_mgr);
+
+     rc = hl_ctx_create(hdev, hpriv);
+     if (rc) {
+             dev_err(hdev->dev, "Failed to open FD (CTX fail)\n");
+             goto out_err;
+     }
+
      hpriv->taskpid = find_get_pid(current->pid);
 
      return 0;
+
+out_err:
+     filp->private_data = NULL;
+     hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
+     kfree(hpriv);
+
+close_device:
+     atomic_dec(&hdev->fd_open_cnt);
+     return rc;
 }
 
Quoted 9 lines
Quoted 4 lines
drivers/misc/habanalabs/habanalabs_drv.c +0 -1
 /**
--

2.17.1

--
Sincerely yours,
Mike.

Fixed, thanks.
Oded

#23 lore raw

PATCH 5/15 habanalabs: add command buffer module

In reply to Oded Gabbay

This patch adds the CB module, which allows the user to create and destroy CBs and to map them to the user's process address-space.

A command buffer is a memory blocks that reside in DMA-able address-space and is physically contiguous so it can be accessed by the device without MMU translation. The command buffer memory is allocated using the coherent DMA API.

When creating a new CB, the IOCTL returns a handle of it, and the user-space process needs to use that handle to mmap the buffer to get a VA in the user's address-space.

Before destroying (freeing) a CB, the user must unmap the CB's VA using the CB handle.

Each CB has a reference counter, which tracks its usage in command submissions and also its mmaps (only a single mmap is allowed).

The driver maintains a pool of pre-allocated CBs in order to reduce latency during command submissions. In case the pool is empty, the driver will go to the slow-path of allocating a new CB, i.e. calling dma_alloc_coherent.

---
 drivers/misc/habanalabs/Makefile           |   3 +-
 drivers/misc/habanalabs/command_buffer.c   | 414 +++++++++++++++++++++
 drivers/misc/habanalabs/device.c           |  43 ++-
 drivers/misc/habanalabs/goya/goya.c        |  28 ++
 drivers/misc/habanalabs/habanalabs.h       |  95 ++++-
 drivers/misc/habanalabs/habanalabs_drv.c   |   2 +
 drivers/misc/habanalabs/habanalabs_ioctl.c | 102 +++++
 include/uapi/misc/habanalabs.h             |  62 +++
 8 files changed, 746 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/habanalabs/command_buffer.c
 create mode 100644 drivers/misc/habanalabs/habanalabs_ioctl.c
 create mode 100644 include/uapi/misc/habanalabs.h
drivers/misc/habanalabs/Makefile +2 -1
index 3ffbadc2ca01..2530c9b78ca4 100644
@@ -4,7 +4,8 @@
 
 obj-m	:= habanalabs.o
 
-habanalabs-y := habanalabs_drv.o device.o context.o asid.o
+habanalabs-y := habanalabs_drv.o device.o context.o asid.o habanalabs_ioctl.o \
+		command_buffer.o
 
 include $(src)/goya/Makefile
 habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/command_buffer.c new file +414 -0

414 lines not on this page · raw message

drivers/misc/habanalabs/device.c +42 -1

87 lines not on this page · raw message

drivers/misc/habanalabs/goya/goya.c +28 -0
index b2952296b890..341ac085af82 100644
@@ -92,6 +92,9 @@
 
 #define GOYA_MAX_INITIATORS		20
 
+#define GOYA_CB_POOL_CB_CNT		512
+#define GOYA_CB_POOL_CB_SIZE		0x20000		/* 128KB */
+
 static void goya_get_fixed_properties(struct hl_device *hdev)
 {
 	struct asic_fixed_properties *prop = &hdev->asic_prop;
@@ -119,6 +122,8 @@ static void goya_get_fixed_properties(struct hl_device *hdev)
 	prop->tpc_enabled_mask = TPC_ENABLED_MASK;
 
 	prop->high_pll = PLL_HIGH_DEFAULT;
+	prop->cb_pool_cb_cnt = GOYA_CB_POOL_CB_CNT;
+	prop->cb_pool_cb_size = GOYA_CB_POOL_CB_SIZE;
 }
 
 /**
@@ -598,6 +603,27 @@ int goya_resume(struct hl_device *hdev)
 	return 0;
 }
 
+int goya_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma)
+{
+	return -EINVAL;
+}
+
+int goya_cb_mmap(struct hl_device *hdev, struct vm_area_struct *vma,
+		u64 kaddress, phys_addr_t paddress, u32 size)
+{
+	int rc;
+
+	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP |
+			VM_DONTCOPY | VM_NORESERVE;
+
+	rc = remap_pfn_range(vma, vma->vm_start, paddress >> PAGE_SHIFT,
+				size, vma->vm_page_prot);
+	if (rc)
+		dev_err(hdev->dev, "remap_pfn_range error %d", rc);
+
+	return rc;
+}
+
 void *goya_dma_alloc_coherent(struct hl_device *hdev, size_t size,
 					dma_addr_t *dma_handle, gfp_t flags)
 {
@@ -617,6 +643,8 @@ static const struct hl_asic_funcs goya_funcs = {
 	.sw_fini = goya_sw_fini,
 	.suspend = goya_suspend,
 	.resume = goya_resume,
+	.mmap = goya_mmap,
+	.cb_mmap = goya_cb_mmap,
 	.dma_alloc_coherent = goya_dma_alloc_coherent,
 	.dma_free_coherent = goya_dma_free_coherent,
 };
drivers/misc/habanalabs/habanalabs.h +94 -1

175 lines not on this page · raw message

drivers/misc/habanalabs/habanalabs_drv.c +2 -0
index 0646da83eb53..5c312dd3aa50 100644
@@ -123,6 +123,7 @@ int hl_device_open(struct inode *inode, struct file *filp)
 	kref_init(&hpriv->refcount);
 	nonseekable_open(inode, filp);
 
+	hl_cb_mgr_init(&hpriv->cb_mgr);
 	hl_ctx_mgr_init(&hpriv->ctx_mgr);
 
 	rc = hl_ctx_create(hdev, hpriv);
@@ -138,6 +139,7 @@ int hl_device_open(struct inode *inode, struct file *filp)
 out_err:
 	filp->private_data = NULL;
 	hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
+	hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
 	kfree(hpriv);
 
 close_device:
drivers/misc/habanalabs/habanalabs_ioctl.c new file +102 -0

102 lines not on this page · raw message

include/uapi/misc/habanalabs.h new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.17.1
#24 lore raw

Re: [PATCH 05/15] habanalabs: add command buffer module

Quoted 2 lines · Oded Gabbay

This patch adds the CB module, which allows the user to create and destroy CBs and to map them to the user's process address-space.

Can you please spell "command buffer" at least first time it's mentioned?

Quoted 61 lines

A command buffer is a memory blocks that reside in DMA-able address-space and is physically contiguous so it can be accessed by the device without MMU translation. The command buffer memory is allocated using the coherent DMA API.

When creating a new CB, the IOCTL returns a handle of it, and the user-space process needs to use that handle to mmap the buffer to get a VA in the user's address-space.

Before destroying (freeing) a CB, the user must unmap the CB's VA using the CB handle.

Each CB has a reference counter, which tracks its usage in command submissions and also its mmaps (only a single mmap is allowed).

The driver maintains a pool of pre-allocated CBs in order to reduce latency during command submissions. In case the pool is empty, the driver will go to the slow-path of allocating a new CB, i.e. calling dma_alloc_coherent.

---
 drivers/misc/habanalabs/Makefile           |   3 +-
 drivers/misc/habanalabs/command_buffer.c   | 414 +++++++++++++++++++++
 drivers/misc/habanalabs/device.c           |  43 ++-
 drivers/misc/habanalabs/goya/goya.c        |  28 ++
 drivers/misc/habanalabs/habanalabs.h       |  95 ++++-
 drivers/misc/habanalabs/habanalabs_drv.c   |   2 +
 drivers/misc/habanalabs/habanalabs_ioctl.c | 102 +++++
 include/uapi/misc/habanalabs.h             |  62 +++
 8 files changed, 746 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/habanalabs/command_buffer.c
 create mode 100644 drivers/misc/habanalabs/habanalabs_ioctl.c
 create mode 100644 include/uapi/misc/habanalabs.h
drivers/misc/habanalabs/Makefile +2 -1
index 3ffbadc2ca01..2530c9b78ca4 100644
@@ -4,7 +4,8 @@
 
 obj-m	:= habanalabs.o
 
-habanalabs-y := habanalabs_drv.o device.o context.o asid.o
+habanalabs-y := habanalabs_drv.o device.o context.o asid.o habanalabs_ioctl.o \
+		command_buffer.o
 
 include $(src)/goya/Makefile
 habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/command_buffer.c new file +6 -0
new file mode 100644
index 000000000000..535ed6cc5bda
@@ -0,0 +1,414 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */

drivers/misc/habanalabs/command_buffer.c line 16

Quoted 10 lines
drivers/misc/habanalabs/command_buffer.c new file +10 -0
+
+#include <uapi/misc/habanalabs.h>
+#include "habanalabs.h"
+
+#include <linux/dma-mapping.h>
+
+static void cb_fini(struct hl_device *hdev, struct hl_cb *cb)
+{
+	hdev->asic_funcs->dma_free_coherent(hdev, cb->size,
+			(void *) cb->kernel_address, cb->bus_address);

As it seems, ASIC specific dma_free_coherent is a shortcut for a generic dma_free_coherent. Why not use it directly?

Quoted 23 lines
drivers/misc/habanalabs/command_buffer.c new file +23 -0
+	kfree(cb);
+}
+
+static void cb_do_release(struct hl_device *hdev, struct hl_cb *cb)
+{
+	if (cb->is_pool) {
+		spin_lock(&hdev->cb_pool_lock);
+		list_add(&cb->pool_list, &hdev->cb_pool);
+		spin_unlock(&hdev->cb_pool_lock);
+	} else {
+		cb_fini(hdev, cb);
+	}
+}
+
+static void cb_release(struct kref *ref)
+{
+	struct hl_device *hdev;
+	struct hl_cb *cb;
+
+	cb = container_of(ref, struct hl_cb, refcount);
+	hdev = cb->hdev;
+
+	cb_do_release(hdev, cb);

drivers/misc/habanalabs/command_buffer.c line 49

Quoted 10 lines
drivers/misc/habanalabs/command_buffer.c new file +10 -0
+}
+
+static struct hl_cb *hl_cb_alloc(struct hl_device *hdev, u32 cb_size,
+					int ctx_id)
+{
+	struct hl_cb *cb;
+	void *p;
+
+	if (ctx_id == HL_KERNEL_ASID_ID)
+		cb = kzalloc(sizeof(*cb), GFP_ATOMIC);

The GFP_ATOMIC should be used when the caller cannot tolerate reclaim or sleep and it does not seem to be the case here.

drivers/misc/habanalabs/command_buffer.c line 58

Quoted 9 lines
drivers/misc/habanalabs/command_buffer.c new file +9 -0
+	else
+		cb = kzalloc(sizeof(*cb), GFP_KERNEL);
+
+	if (!cb)
+		return NULL;
+
+	if (ctx_id == HL_KERNEL_ASID_ID)
+		p = hdev->asic_funcs->dma_alloc_coherent(hdev, cb_size,
+						&cb->bus_address, GFP_ATOMIC);

GFP_KERNEL?

Quoted 39 lines
drivers/misc/habanalabs/command_buffer.c new file +39 -0
+	else
+		p = hdev->asic_funcs->dma_alloc_coherent(hdev, cb_size,
+						&cb->bus_address,
+						GFP_USER | __GFP_ZERO);
+	if (!p) {
+		dev_err(hdev->dev,
+			"failed to allocate %d of dma memory for CB\n",
+			cb_size);
+		kfree(cb);
+		return NULL;
+	}
+
+	cb->kernel_address = (u64) p;
+	cb->size = cb_size;
+
+	return cb;
+}
+
+int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
+			u32 cb_size, u64 *handle, int ctx_id)
+{
+	struct hl_cb *cb;
+	bool alloc_new_cb = true;
+	int rc;
+
+	if (hdev->disabled) {
+		dev_warn_ratelimited(hdev->dev,
+			"Device is disabled !!! Can't create new CBs\n");
+		rc = -EBUSY;
+		goto out_err;
+	}
+
+	/* Minimum allocation must be PAGE SIZE */
+	if (cb_size < PAGE_SIZE)
+		cb_size = PAGE_SIZE;
+
+	if (ctx_id == HL_KERNEL_ASID_ID &&
+			cb_size <= hdev->asic_prop.cb_pool_cb_size) {
+

drivers/misc/habanalabs/command_buffer.c line 107

Quoted 10 lines
drivers/misc/habanalabs/command_buffer.c new file +10 -0
+		spin_lock(&hdev->cb_pool_lock);
+		if (!list_empty(&hdev->cb_pool)) {
+			cb = list_first_entry(&hdev->cb_pool, typeof(*cb),
+					pool_list);
+			list_del(&cb->pool_list);
+			spin_unlock(&hdev->cb_pool_lock);
+			alloc_new_cb = false;
+		} else {
+			spin_unlock(&hdev->cb_pool_lock);
+			dev_warn_once(hdev->dev, "CB pool is empty\n");

Isn't it going to be a false alarm when you allocate the cb for the first time?

Quoted 6 lines
drivers/misc/habanalabs/command_buffer.c new file +6 -0
+		}
+	}
+
+	if (alloc_new_cb) {
+		cb = hl_cb_alloc(hdev, cb_size, ctx_id);
+		if (!cb) {

drivers/misc/habanalabs/command_buffer.c line 123

Quoted 10 lines
drivers/misc/habanalabs/command_buffer.c new file +10 -0
+			rc = -ENOMEM;
+			goto out_err;
+		}
+	}
+
+	cb->hdev = hdev;
+	cb->ctx_id = ctx_id;
+
+	spin_lock(&mgr->cb_lock);
+	rc = idr_alloc(&mgr->cb_handles, cb, 1, 0, GFP_ATOMIC);

It seems the ID will remain dangling if the cb is reused.

Quoted 111 lines
drivers/misc/habanalabs/command_buffer.c new file +111 -0
+	spin_unlock(&mgr->cb_lock);
+
+	if (rc < 0) {
+		dev_err(hdev->dev, "Failed to allocate IDR for a new CB\n");
+		goto release_cb;
+	}
+
+	cb->id = rc;
+
+	kref_init(&cb->refcount);
+	spin_lock_init(&cb->lock);
+
+	/*
+	 * idr is 32-bit so we can safely OR it with a mask that is above
+	 * 32 bit
+	 */
+	*handle = cb->id | HL_MMAP_CB_MASK;
+	*handle <<= PAGE_SHIFT;
+
+	return 0;
+
+release_cb:
+	cb_do_release(hdev, cb);
+out_err:
+	*handle = 0;
+
+	return rc;
+}
+
+int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle)
+{
+	struct hl_cb *cb;
+	u32 handle;
+	int rc = 0;
+
+	/*
+	 * handle was given to user to do mmap, I need to shift it back to
+	 * how the idr module gave it to me
+	 */
+	cb_handle >>= PAGE_SHIFT;
+	handle = (u32) cb_handle;
+
+	spin_lock(&mgr->cb_lock);
+
+	cb = idr_find(&mgr->cb_handles, handle);
+	if (cb) {
+		idr_remove(&mgr->cb_handles, handle);
+		spin_unlock(&mgr->cb_lock);
+		kref_put(&cb->refcount, cb_release);
+	} else {
+		spin_unlock(&mgr->cb_lock);
+		dev_err(hdev->dev,
+			"CB destroy failed, no match to handle 0x%x\n", handle);
+		rc = -EINVAL;
+	}
+
+	return rc;
+}
+
+int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)
+{
+	union hl_cb_args *args = data;
+	struct hl_device *hdev = hpriv->hdev;
+	u64 handle;
+	int rc;
+
+	switch (args->in.op) {
+	case HL_CB_OP_CREATE:
+		rc = hl_cb_create(hdev, &hpriv->cb_mgr, args->in.cb_size,
+					&handle, hpriv->ctx->asid);
+		memset(args, 0, sizeof(*args));
+		args->out.cb_handle = handle;
+		break;
+	case HL_CB_OP_DESTROY:
+		rc = hl_cb_destroy(hdev, &hpriv->cb_mgr,
+					args->in.cb_handle);
+		memset(args, 0, sizeof(*args));
+		break;
+	default:
+		rc = -EINVAL;
+		break;
+	}
+
+	return rc;
+}
+
+static void cb_vm_close(struct vm_area_struct *vma)
+{
+	struct hl_cb *cb = (struct hl_cb *) vma->vm_private_data;
+
+	hl_cb_put(cb);
+
+	spin_lock(&cb->lock);
+	cb->mmap = false;
+	cb->vm_start = 0;
+	cb->vm_end = 0;
+	spin_unlock(&cb->lock);
+
+	vma->vm_private_data = NULL;
+}
+
+static const struct vm_operations_struct cb_vm_ops = {
+	.close = cb_vm_close
+};
+
+int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma)
+{
+	struct hl_device *hdev = hpriv->hdev;
+	struct hl_cb *cb;
+	phys_addr_t address;
+	u32 handle;

drivers/misc/habanalabs/command_buffer.c line 244

Quoted 10 lines
drivers/misc/habanalabs/command_buffer.c new file +10 -0
+	int rc;
+
+	handle = vma->vm_pgoff;
+
+	/* reference was taken here */
+	cb = hl_cb_get(hdev, &hpriv->cb_mgr, handle);
+	if (!cb) {
+		dev_err(hdev->dev,
+			"CB mmap failed, no match to handle %d\n", handle);
+		goto err_out;

why no simply return -EINVAL?

Quoted 570 lines
drivers/misc/habanalabs/command_buffer.c new file +170 -0
+	}
+
+	/* Validation check */
+	if (vma->vm_end - vma->vm_start != cb->size) {
+		dev_err(hdev->dev,
+			"CB mmap failed, mmap size 0x%lx != 0x%x cb size\n",
+			vma->vm_end - vma->vm_start, cb->size);
+		goto put_cb;
+	}
+
+	spin_lock(&cb->lock);
+
+	if (cb->mmap) {
+		dev_err(hdev->dev,
+			"CB mmap failed, CB already mmaped to user\n");
+		goto release_lock;
+	}
+
+	cb->mmap = true;
+
+	spin_unlock(&cb->lock);
+
+	vma->vm_ops = &cb_vm_ops;
+
+	/*
+	 * Note: We're transferring the cb reference to
+	 * vma->vm_private_data here.
+	 */
+
+	vma->vm_private_data = cb;
+
+	/* Calculate address for CB */
+	address = virt_to_phys((void *) cb->kernel_address);
+
+	rc = hdev->asic_funcs->cb_mmap(hdev, vma, cb->kernel_address,
+					address, cb->size);
+
+	if (rc) {
+		spin_lock(&cb->lock);
+		cb->mmap = false;
+		goto release_lock;
+	}
+
+	cb->vm_start = vma->vm_start;
+	cb->vm_end = vma->vm_end;
+
+	return 0;
+
+release_lock:
+	spin_unlock(&cb->lock);
+put_cb:
+	hl_cb_put(cb);
+err_out:
+	return -EINVAL;
+}
+
+struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
+			u32 handle)
+{
+	struct hl_cb *cb;
+
+	spin_lock(&mgr->cb_lock);
+	cb = idr_find(&mgr->cb_handles, handle);
+
+	if (!cb) {
+		spin_unlock(&mgr->cb_lock);
+		dev_warn(hdev->dev,
+			"CB get failed, no match to handle %d\n", handle);
+		return NULL;
+	}
+
+	kref_get(&cb->refcount);
+
+	spin_unlock(&mgr->cb_lock);
+
+	return cb;
+
+}
+
+void hl_cb_put(struct hl_cb *cb)
+{
+	kref_put(&cb->refcount, cb_release);
+}
+
+void hl_cb_mgr_init(struct hl_cb_mgr *mgr)
+{
+	spin_lock_init(&mgr->cb_lock);
+	idr_init(&mgr->cb_handles);
+}
+
+void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr)
+{
+	struct hl_cb *cb;
+	struct idr *idp;
+	u32 id;
+
+	idp = &mgr->cb_handles;
+
+	idr_for_each_entry(idp, cb, id) {
+		if (kref_put(&cb->refcount, cb_release) != 1)
+			dev_err(hdev->dev,
+				"CB %d for CTX ID %d is still alive\n",
+				id, cb->ctx_id);
+	}
+
+	idr_destroy(&mgr->cb_handles);
+}
+
+struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size)
+{
+	u64 cb_handle;
+	struct hl_cb *cb;
+	int rc;
+
+	rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr, cb_size, &cb_handle,
+			HL_KERNEL_ASID_ID);
+	if (rc) {
+		dev_err(hdev->dev, "Failed to allocate CB for KMD %d\n", rc);
+		return NULL;
+	}
+
+	cb_handle >>= PAGE_SHIFT;
+	cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr, (u32) cb_handle);
+	/* hl_cb_get should never fail here so use kernel WARN */
+	WARN(!cb, "Kernel CB handle invalid 0x%x\n", (u32) cb_handle);
+	if (!cb)
+		goto destroy_cb;
+
+	return cb;
+
+destroy_cb:
+	hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb_handle << PAGE_SHIFT);
+
+	return NULL;
+}
+
+int hl_cb_pool_init(struct hl_device *hdev)
+{
+	struct hl_cb *cb;
+	int i;
+
+	INIT_LIST_HEAD(&hdev->cb_pool);
+	spin_lock_init(&hdev->cb_pool_lock);
+
+	for (i = 0 ; i < hdev->asic_prop.cb_pool_cb_cnt ; i++) {
+		cb = hl_cb_alloc(hdev, hdev->asic_prop.cb_pool_cb_size,
+				HL_KERNEL_ASID_ID);
+		if (cb) {
+			cb->is_pool = true;
+			list_add(&cb->pool_list, &hdev->cb_pool);
+		} else {
+			hl_cb_pool_fini(hdev);
+			return -ENOMEM;
+		}
+	}
+
+	return 0;
+}
+
+int hl_cb_pool_fini(struct hl_device *hdev)
+{
+	struct hl_cb *cb, *tmp;
+
+	list_for_each_entry_safe(cb, tmp, &hdev->cb_pool, pool_list) {
+		list_del(&cb->pool_list);
+		cb_fini(hdev, cb);
+	}
+
+	return 0;
+}
drivers/misc/habanalabs/device.c +42 -1
index 84ce9fcb52da..0bd86a7d34db 100644
@@ -53,6 +53,7 @@ static int hl_device_release(struct inode *inode, struct file *filp)
 {
 	struct hl_fpriv *hpriv = filp->private_data;
 
+	hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
 	hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
 
 	filp->private_data = NULL;
@@ -62,10 +63,34 @@ static int hl_device_release(struct inode *inode, struct file *filp)
 	return 0;
 }
 
+/**
+ * hl_mmap - mmap function for habanalabs device
+ *
+ * @*filp: pointer to file structure
+ * @*vma: pointer to vm_area_struct of the process
+ *
+ * Called when process does an mmap on habanalabs device. Call the device's mmap
+ * function at the end of the common code.
+ */
+static int hl_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	struct hl_fpriv *hpriv = filp->private_data;
+
+	if ((vma->vm_pgoff & HL_MMAP_CB_MASK) == HL_MMAP_CB_MASK) {
+		vma->vm_pgoff ^= HL_MMAP_CB_MASK;
+		return hl_cb_mmap(hpriv, vma);
+	}
+
+	return hpriv->hdev->asic_funcs->mmap(hpriv, vma);
+}
+
 static const struct file_operations hl_ops = {
 	.owner = THIS_MODULE,
 	.open = hl_device_open,
-	.release = hl_device_release
+	.release = hl_device_release,
+	.mmap = hl_mmap,
+	.unlocked_ioctl = hl_ioctl,
+	.compat_ioctl = hl_ioctl
 };
 
 /**
@@ -145,6 +170,8 @@ static int device_early_init(struct hl_device *hdev)
 	if (rc)
 		goto early_fini;
 
+	hl_cb_mgr_init(&hdev->kernel_cb_mgr);
+
 	mutex_init(&hdev->device_open);
 	atomic_set(&hdev->fd_open_cnt, 0);
 
@@ -166,6 +193,8 @@ static int device_early_init(struct hl_device *hdev)
 static void device_early_fini(struct hl_device *hdev)
 {
 
+	hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
+
 	hl_asid_fini(hdev);
 
 	if (hdev->asic_funcs->early_fini)
@@ -280,11 +309,21 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)
 		goto free_ctx;
 	}
 
+	rc = hl_cb_pool_init(hdev);
+	if (rc) {
+		dev_err(hdev->dev, "failed to initialize CB pool\n");
+		goto release_ctx;
+	}
+
 	dev_notice(hdev->dev,
 		"Successfully added device to habanalabs driver\n");
 
 	return 0;
 
+release_ctx:
+	if (hl_ctx_put(hdev->kernel_ctx) != 1)
+		dev_err(hdev->dev,
+			"kernel ctx is still alive on initialization failure\n");
 free_ctx:
 	kfree(hdev->kernel_ctx);
 sw_fini:
@@ -321,6 +360,8 @@ void hl_device_fini(struct hl_device *hdev)
 	/* Mark device as disabled */
 	hdev->disabled = true;
 
+	hl_cb_pool_fini(hdev);
+
 	/* Release kernel context */
 	if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
 		dev_err(hdev->dev, "kernel ctx is still alive\n");
drivers/misc/habanalabs/goya/goya.c +28 -0
index b2952296b890..341ac085af82 100644
@@ -92,6 +92,9 @@
 
 #define GOYA_MAX_INITIATORS		20
 
+#define GOYA_CB_POOL_CB_CNT		512
+#define GOYA_CB_POOL_CB_SIZE		0x20000		/* 128KB */
+
 static void goya_get_fixed_properties(struct hl_device *hdev)
 {
 	struct asic_fixed_properties *prop = &hdev->asic_prop;
@@ -119,6 +122,8 @@ static void goya_get_fixed_properties(struct hl_device *hdev)
 	prop->tpc_enabled_mask = TPC_ENABLED_MASK;
 
 	prop->high_pll = PLL_HIGH_DEFAULT;
+	prop->cb_pool_cb_cnt = GOYA_CB_POOL_CB_CNT;
+	prop->cb_pool_cb_size = GOYA_CB_POOL_CB_SIZE;
 }
 
 /**
@@ -598,6 +603,27 @@ int goya_resume(struct hl_device *hdev)
 	return 0;
 }
 
+int goya_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma)
+{
+	return -EINVAL;
+}
+
+int goya_cb_mmap(struct hl_device *hdev, struct vm_area_struct *vma,
+		u64 kaddress, phys_addr_t paddress, u32 size)
+{
+	int rc;
+
+	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP |
+			VM_DONTCOPY | VM_NORESERVE;
+
+	rc = remap_pfn_range(vma, vma->vm_start, paddress >> PAGE_SHIFT,
+				size, vma->vm_page_prot);
+	if (rc)
+		dev_err(hdev->dev, "remap_pfn_range error %d", rc);
+
+	return rc;
+}
+
 void *goya_dma_alloc_coherent(struct hl_device *hdev, size_t size,
 					dma_addr_t *dma_handle, gfp_t flags)
 {
@@ -617,6 +643,8 @@ static const struct hl_asic_funcs goya_funcs = {
 	.sw_fini = goya_sw_fini,
 	.suspend = goya_suspend,
 	.resume = goya_resume,
+	.mmap = goya_mmap,
+	.cb_mmap = goya_cb_mmap,
 	.dma_alloc_coherent = goya_dma_alloc_coherent,
 	.dma_free_coherent = goya_dma_free_coherent,
 };
drivers/misc/habanalabs/habanalabs.h +94 -1
index d003a6af2131..6ad476df65b0 100644
@@ -21,10 +21,12 @@
 
 #define HL_NAME				"habanalabs"
 
+#define HL_MMAP_CB_MASK			(0x8000000000000000ull >> PAGE_SHIFT)
+
 #define HL_MAX_QUEUES			128
 
 struct hl_device;
-
+struct hl_fpriv;
 
 
 
@@ -53,6 +55,8 @@ struct hl_device;
  * @max_asid: maximum number of open contexts (ASIDs).
  * @completion_queues_count: number of completion queues.
  * @high_pll: high PLL frequency used by the device.
+ * @cb_pool_cb_cnt: number of CBs in the CB pool.
+ * @cb_pool_cb_size: size of each CB in the CB pool.
  * @tpc_enabled_mask: which TPCs are enabled.
  */
 struct asic_fixed_properties {
@@ -73,11 +77,68 @@ struct asic_fixed_properties {
 	u32			sram_size;
 	u32			max_asid;
 	u32			high_pll;
+	u32			cb_pool_cb_cnt;
+	u32			cb_pool_cb_size;
 	u8			completion_queues_count;
 	u8			tpc_enabled_mask;
 };
 
 
+
+
+
+
+/*
+ * Command Buffers
+ */
+
+/**
+ * struct hl_cb_mgr - describes a Command Buffer Manager.
+ * @cb_lock: protects cb_handles.
+ * @cb_handles: an idr to hold all command buffer handles.
+ */
+struct hl_cb_mgr {
+	spinlock_t		cb_lock;
+	struct idr		cb_handles; /* protected by cb_lock */
+};
+
+/**
+ * struct hl_cb - describes a Command Buffer.
+ * @refcount: reference counter for usage of the CB.
+ * @hdev: pointer to device this CB belongs to.
+ * @lock: spinlock to protect mmap/cs flows.
+ * @pool_list: node in pool list of command buffers.
+ * @kernel_address: Holds the CB's kernel virtual address.
+ * @bus_address: Holds the CB's DMA address.
+ * @vm_start: Holds the CB's user start virtual address (when mmaped).
+ * @vm_end: Holds the CB's user end virtual address (when mmaped).
+ * @size: holds the CB's size.
+ * @id: the CB's ID.
+ * @ctx_id: holds the ID of the owner's context.
+ * @mmap: true if the CB is currently mmaped to user.
+ * @is_pool: true if CB was acquired from the pool, false otherwise.
+ */
+struct hl_cb {
+	struct kref		refcount;
+	struct hl_device	*hdev;
+	spinlock_t		lock;
+	struct list_head	pool_list;
+	u64			kernel_address;
+	dma_addr_t		bus_address;
+	u64			vm_start;
+	u64			vm_end;
+	u32			size;
+	u32			id;
+	u32			ctx_id;
+	u8			mmap;
+	u8			is_pool;
+};
+
+
+
+
+
+
 #define HL_QUEUE_LENGTH			256
 
 
@@ -109,6 +170,8 @@ enum hl_asic_type {
  * @sw_fini: tears down driver state, does not configure H/W.
  * @suspend: handles IP specific H/W or SW changes for suspend.
  * @resume: handles IP specific H/W or SW changes for resume.
+ * @mmap: mmap function, does nothing.
+ * @cb_mmap: maps a CB.
  * @dma_alloc_coherent: DMA allocate coherent memory.
  * @dma_free_coherent: free DMA allocation.
  */
@@ -119,6 +182,9 @@ struct hl_asic_funcs {
 	int (*sw_fini)(struct hl_device *hdev);
 	int (*suspend)(struct hl_device *hdev);
 	int (*resume)(struct hl_device *hdev);
+	int (*mmap)(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
+	int (*cb_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
+			u64 kaddress, phys_addr_t paddress, u32 size);
 	void* (*dma_alloc_coherent)(struct hl_device *hdev, size_t size,
 					dma_addr_t *dma_handle, gfp_t flag);
 	void (*dma_free_coherent)(struct hl_device *hdev, size_t size,
@@ -175,6 +241,7 @@ struct hl_ctx_mgr {
  * @taskpid: current process ID.
  * @ctx: current executing context.
  * @ctx_mgr: context manager to handle multiple context for this FD.
+ * @cb_mgr: command buffer manager to handle multiple buffers for this FD.
  * @refcount: number of related contexts.
  */
 struct hl_fpriv {
@@ -183,6 +250,7 @@ struct hl_fpriv {
 	struct pid		*taskpid;
 	struct hl_ctx		*ctx; /* TODO: remove for multiple ctx */
 	struct hl_ctx_mgr	ctx_mgr;
+	struct hl_cb_mgr	cb_mgr;
 	struct kref		refcount;
 };
 
@@ -239,6 +307,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
  * @asic_name: ASIC specific nmae.
  * @asic_type: ASIC specific type.
  * @kernel_ctx: KMD context structure.
+ * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs.
  * @dma_pool: DMA pool for small allocations.
  * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address.
  * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address.
@@ -249,6 +318,8 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
  * @asic_prop: ASIC specific immutable properties.
  * @asic_funcs: ASIC specific functions.
  * @asic_specific: ASIC specific information to use only from ASIC files.
+ * @cb_pool: list of preallocated CBs.
+ * @cb_pool_lock: protects the CB pool.
  * @user_ctx: current user context executing.
  * @fd_open_cnt: number of open context executing.
  * @major: habanalabs KMD major.
@@ -264,6 +335,7 @@ struct hl_device {
 	char				asic_name[16];
 	enum hl_asic_type		asic_type;
 	struct hl_ctx			*kernel_ctx;
+	struct hl_cb_mgr		kernel_cb_mgr;
 	struct dma_pool			*dma_pool;
 	void				*cpu_accessible_dma_mem;
 	dma_addr_t			cpu_accessible_dma_address;
@@ -275,6 +347,10 @@ struct hl_device {
 	struct asic_fixed_properties	asic_prop;
 	const struct hl_asic_funcs	*asic_funcs;
 	void				*asic_specific;
+
+	struct list_head		cb_pool;
+	spinlock_t			cb_pool_lock;
+
 	/* TODO: The following fields should be moved for multi-context */
 	struct hl_ctx			*user_ctx;
 	atomic_t			fd_open_cnt;
@@ -345,6 +421,23 @@ int hl_device_resume(struct hl_device *hdev);
 void hl_hpriv_get(struct hl_fpriv *hpriv);
 void hl_hpriv_put(struct hl_fpriv *hpriv);
 
+int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr, u32 cb_size,
+		u64 *handle, int ctx_id);
+int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle);
+int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
+struct hl_cb *hl_cb_get(struct hl_device *hdev,	struct hl_cb_mgr *mgr,
+			u32 handle);
+void hl_cb_put(struct hl_cb *cb);
+void hl_cb_mgr_init(struct hl_cb_mgr *mgr);
+void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr);
+struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size);
+int hl_cb_pool_init(struct hl_device *hdev);
+int hl_cb_pool_fini(struct hl_device *hdev);
+
 void goya_set_asic_funcs(struct hl_device *hdev);
 
+/* IOCTLs */
+long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
+int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
+
 #endif /* HABANALABSP_H_ */
drivers/misc/habanalabs/habanalabs_drv.c +2 -0
index 0646da83eb53..5c312dd3aa50 100644
@@ -123,6 +123,7 @@ int hl_device_open(struct inode *inode, struct file *filp)
 	kref_init(&hpriv->refcount);
 	nonseekable_open(inode, filp);
 
+	hl_cb_mgr_init(&hpriv->cb_mgr);
 	hl_ctx_mgr_init(&hpriv->ctx_mgr);
 
 	rc = hl_ctx_create(hdev, hpriv);
@@ -138,6 +139,7 @@ int hl_device_open(struct inode *inode, struct file *filp)
 out_err:
 	filp->private_data = NULL;
 	hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
+	hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
 	kfree(hpriv);
 
 close_device:
drivers/misc/habanalabs/habanalabs_ioctl.c new file +26 -0
new file mode 100644
index 000000000000..fa2287569e0e
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include <uapi/misc/habanalabs.h>
+#include "habanalabs.h"
+
+#include <linux/fs.h>
+#include <linux/uaccess.h>
+#include <linux/cred.h>
+
+#define HL_IOCTL_DEF(ioctl, _func) \
+	[_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}
+
+static const struct hl_ioctl_desc hl_ioctls[] = {
+	HL_IOCTL_DEF(HL_IOCTL_CB, hl_cb_ioctl)
+};
+
+#define HL_CORE_IOCTL_COUNT	ARRAY_SIZE(hl_ioctls)
+
+long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
+{
+	struct hl_fpriv *hpriv = filep->private_data;

drivers/misc/habanalabs/habanalabs_ioctl.c line 36

Quoted 10 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +10 -0
+	struct hl_device *hdev = hpriv->hdev;
+	hl_ioctl_t *func;
+	const struct hl_ioctl_desc *ioctl = NULL;
+	unsigned int nr = _IOC_NR(cmd);
+	char stack_kdata[128];
+	char *kdata = NULL;
+	unsigned int usize, asize;
+	int retcode = -EINVAL;
+
+	if (nr >= HL_CORE_IOCTL_COUNT)

nr > HL_CORE_IOCTL_COUNT, isn't it?

drivers/misc/habanalabs/habanalabs_ioctl.c line 37

Quoted 1 line
drivers/misc/habanalabs/habanalabs_ioctl.c new file +1 -0
+		goto err_i1;

err_i1 is not very meaningfull. Maybe invalid_ioctl?

drivers/misc/habanalabs/habanalabs_ioctl.c line 39

Quoted 2 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +2 -0
+
+	if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {

The HL_COMMAND_{START,END} do not seem to be defined. Besides, this check seem to be overlapped with

if (nr > HL_CORE_IOCTL_COUNT)

Quoted 25 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +25 -0
+		u32 hl_size;
+
+		ioctl = &hl_ioctls[nr];
+
+		hl_size = _IOC_SIZE(ioctl->cmd);
+		usize = asize = _IOC_SIZE(cmd);
+		if (hl_size > asize)
+			asize = hl_size;
+
+		cmd = ioctl->cmd;
+	} else {
+		goto err_i1;
+	}
+
+	/* Do not trust userspace, use our own definition */
+	func = ioctl->func;
+
+	if (unlikely(!func)) {
+		dev_dbg(hdev->dev, "no function\n");
+		retcode = -EINVAL;
+		goto err_i1;
+	}
+
+	if (cmd & (IOC_IN | IOC_OUT)) {
+		if (asize <= sizeof(stack_kdata)) {

drivers/misc/habanalabs/habanalabs_ioctl.c line 74

Quoted 10 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +10 -0
+			kdata = stack_kdata;
+		} else {
+			kdata = kmalloc(asize, GFP_KERNEL);
+			if (!kdata) {
+				retcode = -ENOMEM;
+				goto err_i1;
+			}
+		}
+		if (asize > usize)
+			memset(kdata + usize, 0, asize - usize);

Just init stack_kdata to 0 and use kzalloc instead of malloc.

Quoted 12 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +12 -0
+	}
+
+	if (cmd & IOC_IN) {
+		if (copy_from_user(kdata, (void __user *)arg, usize)) {
+			retcode = -EFAULT;
+			goto err_i1;
+		}
+	} else if (cmd & IOC_OUT) {
+		memset(kdata, 0, usize);
+	}
+
+	retcode = func(hpriv, kdata);

drivers/misc/habanalabs/habanalabs_ioctl.c line 96

Quoted 10 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +10 -0
+
+	if (cmd & IOC_OUT)
+		if (copy_to_user((void __user *)arg, kdata, usize))
+			retcode = -EFAULT;
+
+err_i1:
+	if (!ioctl)
+		dev_dbg(hdev->dev,
+			"invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
+			  task_pid_nr(current), cmd, nr);

I think this can move right after the 'nr' sanity check and there you can simple return -EINVAL after dev_dbg().

Quoted 77 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +6 -0
+
+	if (kdata != stack_kdata)
+		kfree(kdata);
+
+	return retcode;
+}
include/uapi/misc/habanalabs.h new file +62 -0
new file mode 100644
index 000000000000..b3f9213d4709
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
+ *
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ * Author: Oded Gabbay <[email protected]>
+ *
+ */
+
+#ifndef HABANALABS_H_
+#define HABANALABS_H_
+
+#include <linux/types.h>
+#include <linux/ioctl.h>
+
+/* Opcode to create a new command buffer */
+#define HL_CB_OP_CREATE		0
+/* Opcode to destroy previously created command buffer */
+#define HL_CB_OP_DESTROY	1
+
+struct hl_cb_in {
+	/* Handle of CB or 0 if we want to create one */
+	__u64 cb_handle;
+	/* HL_CB_OP_* */
+	__u32 op;
+	/* Size of CB. Minimum requested size must be PAGE_SIZE */
+	__u32 cb_size;
+	/* Context ID - Currently not in use */
+	__u32 ctx_id;
+	__u32 pad;
+};
+
+struct hl_cb_out {
+	/* Handle of CB */
+	__u64 cb_handle;
+};
+
+union hl_cb_args {
+	struct hl_cb_in in;
+	struct hl_cb_out out;
+};
+
+/*
+ * Command Buffer
+ * - Request a Command Buffer
+ * - Destroy a Command Buffer
+ *
+ * The command buffers are memory blocks that reside in DMA-able address
+ * space and are physically contiguous so they can be accessed by the device
+ * directly. They are allocated using the coherent DMA API.
+ *
+ * When creating a new CB, the IOCTL returns a handle of it, and the user-space
+ * process needs to use that handle to mmap the buffer so it can access them.
+ *
+ */
+#define HL_IOCTL_CB		\
+		_IOWR('H', 0x02, union hl_cb_args)
+
+#define HL_COMMAND_START	0x02
+#define HL_COMMAND_END		0x03
+
+#endif /* HABANALABS_H_ */
Signature · 1 line
2.17.1
Signature · 2 lines
Sincerely yours,
Mike.
#25 lore raw

Re: [PATCH 05/15] habanalabs: add command buffer module

Quoted 6 lines · Mike Rapoport
Quoted 2 lines · Oded Gabbay

This patch adds the CB module, which allows the user to create and destroy CBs and to map them to the user's process address-space.

Can you please spell "command buffer" at least first time it's mentioned?

fixed

Quoted 67 lines
Quoted 67 lines

A command buffer is a memory blocks that reside in DMA-able address-space and is physically contiguous so it can be accessed by the device without MMU translation. The command buffer memory is allocated using the coherent DMA API.

When creating a new CB, the IOCTL returns a handle of it, and the user-space process needs to use that handle to mmap the buffer to get a VA in the user's address-space.

Before destroying (freeing) a CB, the user must unmap the CB's VA using the CB handle.

Each CB has a reference counter, which tracks its usage in command submissions and also its mmaps (only a single mmap is allowed).

The driver maintains a pool of pre-allocated CBs in order to reduce latency during command submissions. In case the pool is empty, the driver will go to the slow-path of allocating a new CB, i.e. calling dma_alloc_coherent.

---
 drivers/misc/habanalabs/Makefile           |   3 +-
 drivers/misc/habanalabs/command_buffer.c   | 414 +++++++++++++++++++++
 drivers/misc/habanalabs/device.c           |  43 ++-
 drivers/misc/habanalabs/goya/goya.c        |  28 ++
 drivers/misc/habanalabs/habanalabs.h       |  95 ++++-
 drivers/misc/habanalabs/habanalabs_drv.c   |   2 +
 drivers/misc/habanalabs/habanalabs_ioctl.c | 102 +++++
 include/uapi/misc/habanalabs.h             |  62 +++
 8 files changed, 746 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/habanalabs/command_buffer.c
 create mode 100644 drivers/misc/habanalabs/habanalabs_ioctl.c
 create mode 100644 include/uapi/misc/habanalabs.h
drivers/misc/habanalabs/Makefile +2 -1
index 3ffbadc2ca01..2530c9b78ca4 100644
@@ -4,7 +4,8 @@
 
 obj-m        := habanalabs.o
 
-habanalabs-y := habanalabs_drv.o device.o context.o asid.o
+habanalabs-y := habanalabs_drv.o device.o context.o asid.o habanalabs_ioctl.o \
+             command_buffer.o
 
 include $(src)/goya/Makefile
 habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/command_buffer.c new file +11 -0
new file mode 100644
index 000000000000..535ed6cc5bda
@@ -0,0 +1,414 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include <uapi/misc/habanalabs.h>
+#include "habanalabs.h"
+
+#include <linux/dma-mapping.h>
Quoted 9 lines

drivers/misc/habanalabs/command_buffer.c line 16

Quoted 5 lines
drivers/misc/habanalabs/command_buffer.c new file +5 -0
+
+static void cb_fini(struct hl_device *hdev, struct hl_cb *cb)
+{
+     hdev->asic_funcs->dma_free_coherent(hdev, cb->size,
+                     (void *) cb->kernel_address, cb->bus_address);

As it seems, ASIC specific dma_free_coherent is a shortcut for a generic dma_free_coherent. Why not use it directly?

As I explained in a previous patch review, there is a different implementation when I'm working with a simulator.

Quoted 28 lines
Quoted 28 lines
drivers/misc/habanalabs/command_buffer.c new file +28 -0
+     kfree(cb);
+}
+
+static void cb_do_release(struct hl_device *hdev, struct hl_cb *cb)
+{
+     if (cb->is_pool) {
+             spin_lock(&hdev->cb_pool_lock);
+             list_add(&cb->pool_list, &hdev->cb_pool);
+             spin_unlock(&hdev->cb_pool_lock);
+     } else {
+             cb_fini(hdev, cb);
+     }
+}
+
+static void cb_release(struct kref *ref)
+{
+     struct hl_device *hdev;
+     struct hl_cb *cb;
+
+     cb = container_of(ref, struct hl_cb, refcount);
+     hdev = cb->hdev;
+
+     cb_do_release(hdev, cb);
+}
+
+static struct hl_cb *hl_cb_alloc(struct hl_device *hdev, u32 cb_size,
+                                     int ctx_id)
+{
Quoted 9 lines

drivers/misc/habanalabs/command_buffer.c line 49

Quoted 5 lines
drivers/misc/habanalabs/command_buffer.c new file +5 -0
+     struct hl_cb *cb;
+     void *p;
+
+     if (ctx_id == HL_KERNEL_ASID_ID)
+             cb = kzalloc(sizeof(*cb), GFP_ATOMIC);

The GFP_ATOMIC should be used when the caller cannot tolerate reclaim or sleep and it does not seem to be the case here.

Yes, I can see why this is misleading. The call of this function from latency-sensitive code comes in a much later patch (command submission).
In short, due to H/W limitations, I must copy the CB of the user to a kernel allocated CB (in case of an external queue) and this must be done during command submission. Hence the GFP_ATOMIC flag in all memory allocations in this function.
I will add a comment in this patch explaining this.

btw, we solved this problem in future ASICs.

Quoted 11 lines

drivers/misc/habanalabs/command_buffer.c line 58

Quoted 9 lines
drivers/misc/habanalabs/command_buffer.c new file +9 -0
+     else
+             cb = kzalloc(sizeof(*cb), GFP_KERNEL);
+
+     if (!cb)
+             return NULL;
+
+     if (ctx_id == HL_KERNEL_ASID_ID)
+             p = hdev->asic_funcs->dma_alloc_coherent(hdev, cb_size,
+                                             &cb->bus_address, GFP_ATOMIC);

GFP_KERNEL?

Same explanation as above.

Quoted 45 lines
Quoted 45 lines
drivers/misc/habanalabs/command_buffer.c new file +44 -0
+     else
+             p = hdev->asic_funcs->dma_alloc_coherent(hdev, cb_size,
+                                             &cb->bus_address,
+                                             GFP_USER | __GFP_ZERO);
+     if (!p) {
+             dev_err(hdev->dev,
+                     "failed to allocate %d of dma memory for CB\n",
+                     cb_size);
+             kfree(cb);
+             return NULL;
+     }
+
+     cb->kernel_address = (u64) p;
+     cb->size = cb_size;
+
+     return cb;
+}
+
+int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
+                     u32 cb_size, u64 *handle, int ctx_id)
+{
+     struct hl_cb *cb;
+     bool alloc_new_cb = true;
+     int rc;
+
+     if (hdev->disabled) {
+             dev_warn_ratelimited(hdev->dev,
+                     "Device is disabled !!! Can't create new CBs\n");
+             rc = -EBUSY;
+             goto out_err;
+     }
+
+     /* Minimum allocation must be PAGE SIZE */
+     if (cb_size < PAGE_SIZE)
+             cb_size = PAGE_SIZE;
+
+     if (ctx_id == HL_KERNEL_ASID_ID &&
+                     cb_size <= hdev->asic_prop.cb_pool_cb_size) {
+
+             spin_lock(&hdev->cb_pool_lock);
+             if (!list_empty(&hdev->cb_pool)) {
+                     cb = list_first_entry(&hdev->cb_pool, typeof(*cb),
+                                     pool_list);
+                     list_del(&cb->pool_list);
Quoted 8 lines

drivers/misc/habanalabs/command_buffer.c line 107

Quoted 5 lines
drivers/misc/habanalabs/command_buffer.c new file +5 -0
+                     spin_unlock(&hdev->cb_pool_lock);
+                     alloc_new_cb = false;
+             } else {
+                     spin_unlock(&hdev->cb_pool_lock);
+                     dev_warn_once(hdev->dev, "CB pool is empty\n");

Isn't it going to be a false alarm when you allocate the cb for the first time?

Why ?
The cb_pool list holds a list of available CBs. See hl_cb_pool_init()
- it adds newly allocated CBs to this pool list.

if (!list_empty(&hdev->cb_pool)) { - this checks whether the
pool is not empty so we can take an available CB from it. If the list is empty (hence the pool is empty), we print the warning.

Quoted 11 lines
Quoted 11 lines
drivers/misc/habanalabs/command_buffer.c new file +10 -0
+             }
+     }
+
+     if (alloc_new_cb) {
+             cb = hl_cb_alloc(hdev, cb_size, ctx_id);
+             if (!cb) {
+                     rc = -ENOMEM;
+                     goto out_err;
+             }
+     }
Quoted 8 lines

drivers/misc/habanalabs/command_buffer.c line 123

Quoted 6 lines
drivers/misc/habanalabs/command_buffer.c new file +6 -0
+
+     cb->hdev = hdev;
+     cb->ctx_id = ctx_id;
+
+     spin_lock(&mgr->cb_lock);
+     rc = idr_alloc(&mgr->cb_handles, cb, 1, 0, GFP_ATOMIC);

It seems the ID will remain dangling if the cb is reused.

I'm not sure what you mean by this comment. Reused by whom ? in how fashion it is reused ?

Quoted 116 lines
Quoted 116 lines
drivers/misc/habanalabs/command_buffer.c new file +115 -0
+     spin_unlock(&mgr->cb_lock);
+
+     if (rc < 0) {
+             dev_err(hdev->dev, "Failed to allocate IDR for a new CB\n");
+             goto release_cb;
+     }
+
+     cb->id = rc;
+
+     kref_init(&cb->refcount);
+     spin_lock_init(&cb->lock);
+
+     /*
+      * idr is 32-bit so we can safely OR it with a mask that is above
+      * 32 bit
+      */
+     *handle = cb->id | HL_MMAP_CB_MASK;
+     *handle <<= PAGE_SHIFT;
+
+     return 0;
+
+release_cb:
+     cb_do_release(hdev, cb);
+out_err:
+     *handle = 0;
+
+     return rc;
+}
+
+int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle)
+{
+     struct hl_cb *cb;
+     u32 handle;
+     int rc = 0;
+
+     /*
+      * handle was given to user to do mmap, I need to shift it back to
+      * how the idr module gave it to me
+      */
+     cb_handle >>= PAGE_SHIFT;
+     handle = (u32) cb_handle;
+
+     spin_lock(&mgr->cb_lock);
+
+     cb = idr_find(&mgr->cb_handles, handle);
+     if (cb) {
+             idr_remove(&mgr->cb_handles, handle);
+             spin_unlock(&mgr->cb_lock);
+             kref_put(&cb->refcount, cb_release);
+     } else {
+             spin_unlock(&mgr->cb_lock);
+             dev_err(hdev->dev,
+                     "CB destroy failed, no match to handle 0x%x\n", handle);
+             rc = -EINVAL;
+     }
+
+     return rc;
+}
+
+int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)
+{
+     union hl_cb_args *args = data;
+     struct hl_device *hdev = hpriv->hdev;
+     u64 handle;
+     int rc;
+
+     switch (args->in.op) {
+     case HL_CB_OP_CREATE:
+             rc = hl_cb_create(hdev, &hpriv->cb_mgr, args->in.cb_size,
+                                     &handle, hpriv->ctx->asid);
+             memset(args, 0, sizeof(*args));
+             args->out.cb_handle = handle;
+             break;
+     case HL_CB_OP_DESTROY:
+             rc = hl_cb_destroy(hdev, &hpriv->cb_mgr,
+                                     args->in.cb_handle);
+             memset(args, 0, sizeof(*args));
+             break;
+     default:
+             rc = -EINVAL;
+             break;
+     }
+
+     return rc;
+}
+
+static void cb_vm_close(struct vm_area_struct *vma)
+{
+     struct hl_cb *cb = (struct hl_cb *) vma->vm_private_data;
+
+     hl_cb_put(cb);
+
+     spin_lock(&cb->lock);
+     cb->mmap = false;
+     cb->vm_start = 0;
+     cb->vm_end = 0;
+     spin_unlock(&cb->lock);
+
+     vma->vm_private_data = NULL;
+}
+
+static const struct vm_operations_struct cb_vm_ops = {
+     .close = cb_vm_close
+};
+
+int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma)
+{
+     struct hl_device *hdev = hpriv->hdev;
+     struct hl_cb *cb;
+     phys_addr_t address;
+     u32 handle;
+     int rc;
+
+     handle = vma->vm_pgoff;
+
Quoted 9 lines

drivers/misc/habanalabs/command_buffer.c line 244

Quoted 6 lines
drivers/misc/habanalabs/command_buffer.c new file +6 -0
+     /* reference was taken here */
+     cb = hl_cb_get(hdev, &hpriv->cb_mgr, handle);
+     if (!cb) {
+             dev_err(hdev->dev,
+                     "CB mmap failed, no match to handle %d\n", handle);
+             goto err_out;

why no simply return -EINVAL?

fixed

Quoted 583 lines
Quoted 570 lines
drivers/misc/habanalabs/command_buffer.c new file +170 -0
+     }
+
+     /* Validation check */
+     if (vma->vm_end - vma->vm_start != cb->size) {
+             dev_err(hdev->dev,
+                     "CB mmap failed, mmap size 0x%lx != 0x%x cb size\n",
+                     vma->vm_end - vma->vm_start, cb->size);
+             goto put_cb;
+     }
+
+     spin_lock(&cb->lock);
+
+     if (cb->mmap) {
+             dev_err(hdev->dev,
+                     "CB mmap failed, CB already mmaped to user\n");
+             goto release_lock;
+     }
+
+     cb->mmap = true;
+
+     spin_unlock(&cb->lock);
+
+     vma->vm_ops = &cb_vm_ops;
+
+     /*
+      * Note: We're transferring the cb reference to
+      * vma->vm_private_data here.
+      */
+
+     vma->vm_private_data = cb;
+
+     /* Calculate address for CB */
+     address = virt_to_phys((void *) cb->kernel_address);
+
+     rc = hdev->asic_funcs->cb_mmap(hdev, vma, cb->kernel_address,
+                                     address, cb->size);
+
+     if (rc) {
+             spin_lock(&cb->lock);
+             cb->mmap = false;
+             goto release_lock;
+     }
+
+     cb->vm_start = vma->vm_start;
+     cb->vm_end = vma->vm_end;
+
+     return 0;
+
+release_lock:
+     spin_unlock(&cb->lock);
+put_cb:
+     hl_cb_put(cb);
+err_out:
+     return -EINVAL;
+}
+
+struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
+                     u32 handle)
+{
+     struct hl_cb *cb;
+
+     spin_lock(&mgr->cb_lock);
+     cb = idr_find(&mgr->cb_handles, handle);
+
+     if (!cb) {
+             spin_unlock(&mgr->cb_lock);
+             dev_warn(hdev->dev,
+                     "CB get failed, no match to handle %d\n", handle);
+             return NULL;
+     }
+
+     kref_get(&cb->refcount);
+
+     spin_unlock(&mgr->cb_lock);
+
+     return cb;
+
+}
+
+void hl_cb_put(struct hl_cb *cb)
+{
+     kref_put(&cb->refcount, cb_release);
+}
+
+void hl_cb_mgr_init(struct hl_cb_mgr *mgr)
+{
+     spin_lock_init(&mgr->cb_lock);
+     idr_init(&mgr->cb_handles);
+}
+
+void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr)
+{
+     struct hl_cb *cb;
+     struct idr *idp;
+     u32 id;
+
+     idp = &mgr->cb_handles;
+
+     idr_for_each_entry(idp, cb, id) {
+             if (kref_put(&cb->refcount, cb_release) != 1)
+                     dev_err(hdev->dev,
+                             "CB %d for CTX ID %d is still alive\n",
+                             id, cb->ctx_id);
+     }
+
+     idr_destroy(&mgr->cb_handles);
+}
+
+struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size)
+{
+     u64 cb_handle;
+     struct hl_cb *cb;
+     int rc;
+
+     rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr, cb_size, &cb_handle,
+                     HL_KERNEL_ASID_ID);
+     if (rc) {
+             dev_err(hdev->dev, "Failed to allocate CB for KMD %d\n", rc);
+             return NULL;
+     }
+
+     cb_handle >>= PAGE_SHIFT;
+     cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr, (u32) cb_handle);
+     /* hl_cb_get should never fail here so use kernel WARN */
+     WARN(!cb, "Kernel CB handle invalid 0x%x\n", (u32) cb_handle);
+     if (!cb)
+             goto destroy_cb;
+
+     return cb;
+
+destroy_cb:
+     hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb_handle << PAGE_SHIFT);
+
+     return NULL;
+}
+
+int hl_cb_pool_init(struct hl_device *hdev)
+{
+     struct hl_cb *cb;
+     int i;
+
+     INIT_LIST_HEAD(&hdev->cb_pool);
+     spin_lock_init(&hdev->cb_pool_lock);
+
+     for (i = 0 ; i < hdev->asic_prop.cb_pool_cb_cnt ; i++) {
+             cb = hl_cb_alloc(hdev, hdev->asic_prop.cb_pool_cb_size,
+                             HL_KERNEL_ASID_ID);
+             if (cb) {
+                     cb->is_pool = true;
+                     list_add(&cb->pool_list, &hdev->cb_pool);
+             } else {
+                     hl_cb_pool_fini(hdev);
+                     return -ENOMEM;
+             }
+     }
+
+     return 0;
+}
+
+int hl_cb_pool_fini(struct hl_device *hdev)
+{
+     struct hl_cb *cb, *tmp;
+
+     list_for_each_entry_safe(cb, tmp, &hdev->cb_pool, pool_list) {
+             list_del(&cb->pool_list);
+             cb_fini(hdev, cb);
+     }
+
+     return 0;
+}
drivers/misc/habanalabs/device.c +42 -1
index 84ce9fcb52da..0bd86a7d34db 100644
@@ -53,6 +53,7 @@ static int hl_device_release(struct inode *inode, struct file *filp)
 {
      struct hl_fpriv *hpriv = filp->private_data;
 
+     hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
      hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
 
      filp->private_data = NULL;
@@ -62,10 +63,34 @@ static int hl_device_release(struct inode *inode, struct file *filp)
      return 0;
 }
 
+/**
+ * hl_mmap - mmap function for habanalabs device
+ *
+ * @*filp: pointer to file structure
+ * @*vma: pointer to vm_area_struct of the process
+ *
+ * Called when process does an mmap on habanalabs device. Call the device's mmap
+ * function at the end of the common code.
+ */
+static int hl_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+     struct hl_fpriv *hpriv = filp->private_data;
+
+     if ((vma->vm_pgoff & HL_MMAP_CB_MASK) == HL_MMAP_CB_MASK) {
+             vma->vm_pgoff ^= HL_MMAP_CB_MASK;
+             return hl_cb_mmap(hpriv, vma);
+     }
+
+     return hpriv->hdev->asic_funcs->mmap(hpriv, vma);
+}
+
 static const struct file_operations hl_ops = {
      .owner = THIS_MODULE,
      .open = hl_device_open,
-     .release = hl_device_release
+     .release = hl_device_release,
+     .mmap = hl_mmap,
+     .unlocked_ioctl = hl_ioctl,
+     .compat_ioctl = hl_ioctl
 };
 
 /**
@@ -145,6 +170,8 @@ static int device_early_init(struct hl_device *hdev)
      if (rc)
              goto early_fini;
 
+     hl_cb_mgr_init(&hdev->kernel_cb_mgr);
+
      mutex_init(&hdev->device_open);
      atomic_set(&hdev->fd_open_cnt, 0);
 
@@ -166,6 +193,8 @@ static int device_early_init(struct hl_device *hdev)
 static void device_early_fini(struct hl_device *hdev)
 {
 
+     hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
+
      hl_asid_fini(hdev);
 
      if (hdev->asic_funcs->early_fini)
@@ -280,11 +309,21 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)
              goto free_ctx;
      }
 
+     rc = hl_cb_pool_init(hdev);
+     if (rc) {
+             dev_err(hdev->dev, "failed to initialize CB pool\n");
+             goto release_ctx;
+     }
+
      dev_notice(hdev->dev,
              "Successfully added device to habanalabs driver\n");
 
      return 0;
 
+release_ctx:
+     if (hl_ctx_put(hdev->kernel_ctx) != 1)
+             dev_err(hdev->dev,
+                     "kernel ctx is still alive on initialization failure\n");
 free_ctx:
      kfree(hdev->kernel_ctx);
 sw_fini:
@@ -321,6 +360,8 @@ void hl_device_fini(struct hl_device *hdev)
      /* Mark device as disabled */
      hdev->disabled = true;
 
+     hl_cb_pool_fini(hdev);
+
      /* Release kernel context */
      if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
              dev_err(hdev->dev, "kernel ctx is still alive\n");
drivers/misc/habanalabs/goya/goya.c +28 -0
index b2952296b890..341ac085af82 100644
@@ -92,6 +92,9 @@
 
 #define GOYA_MAX_INITIATORS          20
 
+#define GOYA_CB_POOL_CB_CNT          512
+#define GOYA_CB_POOL_CB_SIZE         0x20000         /* 128KB */
+
 static void goya_get_fixed_properties(struct hl_device *hdev)
 {
      struct asic_fixed_properties *prop = &hdev->asic_prop;
@@ -119,6 +122,8 @@ static void goya_get_fixed_properties(struct hl_device *hdev)
      prop->tpc_enabled_mask = TPC_ENABLED_MASK;
 
      prop->high_pll = PLL_HIGH_DEFAULT;
+     prop->cb_pool_cb_cnt = GOYA_CB_POOL_CB_CNT;
+     prop->cb_pool_cb_size = GOYA_CB_POOL_CB_SIZE;
 }
 
 /**
@@ -598,6 +603,27 @@ int goya_resume(struct hl_device *hdev)
      return 0;
 }
 
+int goya_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma)
+{
+     return -EINVAL;
+}
+
+int goya_cb_mmap(struct hl_device *hdev, struct vm_area_struct *vma,
+             u64 kaddress, phys_addr_t paddress, u32 size)
+{
+     int rc;
+
+     vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP |
+                     VM_DONTCOPY | VM_NORESERVE;
+
+     rc = remap_pfn_range(vma, vma->vm_start, paddress >> PAGE_SHIFT,
+                             size, vma->vm_page_prot);
+     if (rc)
+             dev_err(hdev->dev, "remap_pfn_range error %d", rc);
+
+     return rc;
+}
+
 void *goya_dma_alloc_coherent(struct hl_device *hdev, size_t size,
                                      dma_addr_t *dma_handle, gfp_t flags)
 {
@@ -617,6 +643,8 @@ static const struct hl_asic_funcs goya_funcs = {
      .sw_fini = goya_sw_fini,
      .suspend = goya_suspend,
      .resume = goya_resume,
+     .mmap = goya_mmap,
+     .cb_mmap = goya_cb_mmap,
      .dma_alloc_coherent = goya_dma_alloc_coherent,
      .dma_free_coherent = goya_dma_free_coherent,
 };
drivers/misc/habanalabs/habanalabs.h +94 -1
index d003a6af2131..6ad476df65b0 100644
@@ -21,10 +21,12 @@
 
 #define HL_NAME                              "habanalabs"
 
+#define HL_MMAP_CB_MASK                      (0x8000000000000000ull >> PAGE_SHIFT)
+
 #define HL_MAX_QUEUES                        128
 
 struct hl_device;
-
+struct hl_fpriv;
 
 
 
@@ -53,6 +55,8 @@ struct hl_device;
  * @max_asid: maximum number of open contexts (ASIDs).
  * @completion_queues_count: number of completion queues.
  * @high_pll: high PLL frequency used by the device.
+ * @cb_pool_cb_cnt: number of CBs in the CB pool.
+ * @cb_pool_cb_size: size of each CB in the CB pool.
  * @tpc_enabled_mask: which TPCs are enabled.
  */
 struct asic_fixed_properties {
@@ -73,11 +77,68 @@ struct asic_fixed_properties {
      u32                     sram_size;
      u32                     max_asid;
      u32                     high_pll;
+     u32                     cb_pool_cb_cnt;
+     u32                     cb_pool_cb_size;
      u8                      completion_queues_count;
      u8                      tpc_enabled_mask;
 };
 
 
+
+
+
+
+/*
+ * Command Buffers
+ */
+
+/**
+ * struct hl_cb_mgr - describes a Command Buffer Manager.
+ * @cb_lock: protects cb_handles.
+ * @cb_handles: an idr to hold all command buffer handles.
+ */
+struct hl_cb_mgr {
+     spinlock_t              cb_lock;
+     struct idr              cb_handles; /* protected by cb_lock */
+};
+
+/**
+ * struct hl_cb - describes a Command Buffer.
+ * @refcount: reference counter for usage of the CB.
+ * @hdev: pointer to device this CB belongs to.
+ * @lock: spinlock to protect mmap/cs flows.
+ * @pool_list: node in pool list of command buffers.
+ * @kernel_address: Holds the CB's kernel virtual address.
+ * @bus_address: Holds the CB's DMA address.
+ * @vm_start: Holds the CB's user start virtual address (when mmaped).
+ * @vm_end: Holds the CB's user end virtual address (when mmaped).
+ * @size: holds the CB's size.
+ * @id: the CB's ID.
+ * @ctx_id: holds the ID of the owner's context.
+ * @mmap: true if the CB is currently mmaped to user.
+ * @is_pool: true if CB was acquired from the pool, false otherwise.
+ */
+struct hl_cb {
+     struct kref             refcount;
+     struct hl_device        *hdev;
+     spinlock_t              lock;
+     struct list_head        pool_list;
+     u64                     kernel_address;
+     dma_addr_t              bus_address;
+     u64                     vm_start;
+     u64                     vm_end;
+     u32                     size;
+     u32                     id;
+     u32                     ctx_id;
+     u8                      mmap;
+     u8                      is_pool;
+};
+
+
+
+
+
+
 #define HL_QUEUE_LENGTH                      256
 
 
@@ -109,6 +170,8 @@ enum hl_asic_type {
  * @sw_fini: tears down driver state, does not configure H/W.
  * @suspend: handles IP specific H/W or SW changes for suspend.
  * @resume: handles IP specific H/W or SW changes for resume.
+ * @mmap: mmap function, does nothing.
+ * @cb_mmap: maps a CB.
  * @dma_alloc_coherent: DMA allocate coherent memory.
  * @dma_free_coherent: free DMA allocation.
  */
@@ -119,6 +182,9 @@ struct hl_asic_funcs {
      int (*sw_fini)(struct hl_device *hdev);
      int (*suspend)(struct hl_device *hdev);
      int (*resume)(struct hl_device *hdev);
+     int (*mmap)(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
+     int (*cb_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
+                     u64 kaddress, phys_addr_t paddress, u32 size);
      void* (*dma_alloc_coherent)(struct hl_device *hdev, size_t size,
                                      dma_addr_t *dma_handle, gfp_t flag);
      void (*dma_free_coherent)(struct hl_device *hdev, size_t size,
@@ -175,6 +241,7 @@ struct hl_ctx_mgr {
  * @taskpid: current process ID.
  * @ctx: current executing context.
  * @ctx_mgr: context manager to handle multiple context for this FD.
+ * @cb_mgr: command buffer manager to handle multiple buffers for this FD.
  * @refcount: number of related contexts.
  */
 struct hl_fpriv {
@@ -183,6 +250,7 @@ struct hl_fpriv {
      struct pid              *taskpid;
      struct hl_ctx           *ctx; /* TODO: remove for multiple ctx */
      struct hl_ctx_mgr       ctx_mgr;
+     struct hl_cb_mgr        cb_mgr;
      struct kref             refcount;
 };
 
@@ -239,6 +307,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
  * @asic_name: ASIC specific nmae.
  * @asic_type: ASIC specific type.
  * @kernel_ctx: KMD context structure.
+ * @kernel_cb_mgr: command buffer manager for creating/destroying/handling CGs.
  * @dma_pool: DMA pool for small allocations.
  * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address.
  * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address.
@@ -249,6 +318,8 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
  * @asic_prop: ASIC specific immutable properties.
  * @asic_funcs: ASIC specific functions.
  * @asic_specific: ASIC specific information to use only from ASIC files.
+ * @cb_pool: list of preallocated CBs.
+ * @cb_pool_lock: protects the CB pool.
  * @user_ctx: current user context executing.
  * @fd_open_cnt: number of open context executing.
  * @major: habanalabs KMD major.
@@ -264,6 +335,7 @@ struct hl_device {
      char                            asic_name[16];
      enum hl_asic_type               asic_type;
      struct hl_ctx                   *kernel_ctx;
+     struct hl_cb_mgr                kernel_cb_mgr;
      struct dma_pool                 *dma_pool;
      void                            *cpu_accessible_dma_mem;
      dma_addr_t                      cpu_accessible_dma_address;
@@ -275,6 +347,10 @@ struct hl_device {
      struct asic_fixed_properties    asic_prop;
      const struct hl_asic_funcs      *asic_funcs;
      void                            *asic_specific;
+
+     struct list_head                cb_pool;
+     spinlock_t                      cb_pool_lock;
+
      /* TODO: The following fields should be moved for multi-context */
      struct hl_ctx                   *user_ctx;
      atomic_t                        fd_open_cnt;
@@ -345,6 +421,23 @@ int hl_device_resume(struct hl_device *hdev);
 void hl_hpriv_get(struct hl_fpriv *hpriv);
 void hl_hpriv_put(struct hl_fpriv *hpriv);
 
+int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr, u32 cb_size,
+             u64 *handle, int ctx_id);
+int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle);
+int hl_cb_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
+struct hl_cb *hl_cb_get(struct hl_device *hdev,      struct hl_cb_mgr *mgr,
+                     u32 handle);
+void hl_cb_put(struct hl_cb *cb);
+void hl_cb_mgr_init(struct hl_cb_mgr *mgr);
+void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr);
+struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size);
+int hl_cb_pool_init(struct hl_device *hdev);
+int hl_cb_pool_fini(struct hl_device *hdev);
+
 void goya_set_asic_funcs(struct hl_device *hdev);
 
+/* IOCTLs */
+long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
+int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
+
 #endif /* HABANALABSP_H_ */
drivers/misc/habanalabs/habanalabs_drv.c +2 -0
index 0646da83eb53..5c312dd3aa50 100644
@@ -123,6 +123,7 @@ int hl_device_open(struct inode *inode, struct file *filp)
      kref_init(&hpriv->refcount);
      nonseekable_open(inode, filp);
 
+     hl_cb_mgr_init(&hpriv->cb_mgr);
      hl_ctx_mgr_init(&hpriv->ctx_mgr);
 
      rc = hl_ctx_create(hdev, hpriv);
@@ -138,6 +139,7 @@ int hl_device_open(struct inode *inode, struct file *filp)
 out_err:
      filp->private_data = NULL;
      hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
+     hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
      kfree(hpriv);
 
 close_device:
drivers/misc/habanalabs/habanalabs_ioctl.c new file +26 -0
new file mode 100644
index 000000000000..fa2287569e0e
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include <uapi/misc/habanalabs.h>
+#include "habanalabs.h"
+
+#include <linux/fs.h>
+#include <linux/uaccess.h>
+#include <linux/cred.h>
+
+#define HL_IOCTL_DEF(ioctl, _func) \
+     [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}
+
+static const struct hl_ioctl_desc hl_ioctls[] = {
+     HL_IOCTL_DEF(HL_IOCTL_CB, hl_cb_ioctl)
+};
+
+#define HL_CORE_IOCTL_COUNT  ARRAY_SIZE(hl_ioctls)
+
+long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
+{
+     struct hl_fpriv *hpriv = filep->private_data;

drivers/misc/habanalabs/habanalabs_ioctl.c line 36

Quoted 10 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +10 -0
+     struct hl_device *hdev = hpriv->hdev;
+     hl_ioctl_t *func;
+     const struct hl_ioctl_desc *ioctl = NULL;
+     unsigned int nr = _IOC_NR(cmd);
+     char stack_kdata[128];
+     char *kdata = NULL;
+     unsigned int usize, asize;
+     int retcode = -EINVAL;
+
+     if (nr >= HL_CORE_IOCTL_COUNT)

nr > HL_CORE_IOCTL_COUNT, isn't it?

Quoted 3 lines

drivers/misc/habanalabs/habanalabs_ioctl.c line 37

Quoted 1 line
drivers/misc/habanalabs/habanalabs_ioctl.c new file +1 -0
+             goto err_i1;

err_i1 is not very meaningfull. Maybe invalid_ioctl?

Changed to out_err as this is used from other places in this function

Quoted 5 lines

drivers/misc/habanalabs/habanalabs_ioctl.c line 39

Quoted 3 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +2 -0
+
+     if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {

The HL_COMMAND_{START,END} do not seem to be defined.

They are defined in uapi/misc/habanalabs.h

Quoted 3 lines

Besides, this check seem to be overlapped with

if (nr > HL_CORE_IOCTL_COUNT)

Correct, removed the first if (HL_CORE_IOCTL_COUNT)

Quoted 30 lines
Quoted 30 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +29 -0
+             u32 hl_size;
+
+             ioctl = &hl_ioctls[nr];
+
+             hl_size = _IOC_SIZE(ioctl->cmd);
+             usize = asize = _IOC_SIZE(cmd);
+             if (hl_size > asize)
+                     asize = hl_size;
+
+             cmd = ioctl->cmd;
+     } else {
+             goto err_i1;
+     }
+
+     /* Do not trust userspace, use our own definition */
+     func = ioctl->func;
+
+     if (unlikely(!func)) {
+             dev_dbg(hdev->dev, "no function\n");
+             retcode = -EINVAL;
+             goto err_i1;
+     }
+
+     if (cmd & (IOC_IN | IOC_OUT)) {
+             if (asize <= sizeof(stack_kdata)) {
+                     kdata = stack_kdata;
+             } else {
+                     kdata = kmalloc(asize, GFP_KERNEL);
+                     if (!kdata) {
Quoted 8 lines

drivers/misc/habanalabs/habanalabs_ioctl.c line 74

Quoted 6 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +6 -0
+                             retcode = -ENOMEM;
+                             goto err_i1;
+                     }
+             }
+             if (asize > usize)
+                     memset(kdata + usize, 0, asize - usize);

Just init stack_kdata to 0 and use kzalloc instead of malloc.

fixed

Quoted 18 lines
Quoted 18 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +17 -0
+     }
+
+     if (cmd & IOC_IN) {
+             if (copy_from_user(kdata, (void __user *)arg, usize)) {
+                     retcode = -EFAULT;
+                     goto err_i1;
+             }
+     } else if (cmd & IOC_OUT) {
+             memset(kdata, 0, usize);
+     }
+
+     retcode = func(hpriv, kdata);
+
+     if (cmd & IOC_OUT)
+             if (copy_to_user((void __user *)arg, kdata, usize))
+                     retcode = -EFAULT;
+
Quoted 8 lines

drivers/misc/habanalabs/habanalabs_ioctl.c line 96

Quoted 5 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +5 -0
+err_i1:
+     if (!ioctl)
+             dev_dbg(hdev->dev,
+                     "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
+                       task_pid_nr(current), cmd, nr);

I think this can move right after the 'nr' sanity check and there you can simple return -EINVAL after dev_dbg().

But you reach here from many different places and I want to print the ioctl information for each case.
I think the real mistake here is if(!ioctl). This should actually be if (ioctl) because only then it is relevant for most of the places in this function that has an error.
And I'll add a dedicated print if the nr is not correct.

Quoted 83 lines
Quoted 76 lines
drivers/misc/habanalabs/habanalabs_ioctl.c new file +6 -0
+
+     if (kdata != stack_kdata)
+             kfree(kdata);
+
+     return retcode;
+}
include/uapi/misc/habanalabs.h new file +62 -1
new file mode 100644
index 000000000000..b3f9213d4709
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
+ *
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ * Author: Oded Gabbay <[email protected]>
+ *
+ */
+
+#ifndef HABANALABS_H_
+#define HABANALABS_H_
+
+#include <linux/types.h>
+#include <linux/ioctl.h>
+
+/* Opcode to create a new command buffer */
+#define HL_CB_OP_CREATE              0
+/* Opcode to destroy previously created command buffer */
+#define HL_CB_OP_DESTROY     1
+
+struct hl_cb_in {
+     /* Handle of CB or 0 if we want to create one */
+     __u64 cb_handle;
+     /* HL_CB_OP_* */
+     __u32 op;
+     /* Size of CB. Minimum requested size must be PAGE_SIZE */
+     __u32 cb_size;
+     /* Context ID - Currently not in use */
+     __u32 ctx_id;
+     __u32 pad;
+};
+
+struct hl_cb_out {
+     /* Handle of CB */
+     __u64 cb_handle;
+};
+
+union hl_cb_args {
+     struct hl_cb_in in;
+     struct hl_cb_out out;
+};
+
+/*
+ * Command Buffer
+ * - Request a Command Buffer
+ * - Destroy a Command Buffer
+ *
+ * The command buffers are memory blocks that reside in DMA-able address
+ * space and are physically contiguous so they can be accessed by the device
+ * directly. They are allocated using the coherent DMA API.
+ *
+ * When creating a new CB, the IOCTL returns a handle of it, and the user-space
+ * process needs to use that handle to mmap the buffer so it can access them.
+ *
+ */
+#define HL_IOCTL_CB          \
+             _IOWR('H', 0x02, union hl_cb_args)
+
+#define HL_COMMAND_START     0x02
+#define HL_COMMAND_END               0x03
+
+#endif /* HABANALABS_H_ */
--
Quoted 2 lines

2.17.1

--
Sincerely yours,
Mike.

#26 lore raw

Re: [PATCH 05/15] habanalabs: add command buffer module

Quoted 43 lines · Oded Gabbay
Quoted 6 lines · Mike Rapoport
Quoted 2 lines · Oded Gabbay

This patch adds the CB module, which allows the user to create and destroy CBs and to map them to the user's process address-space.

Can you please spell "command buffer" at least first time it's mentioned?

fixed

Quoted 35 lines
Quoted 35 lines

A command buffer is a memory blocks that reside in DMA-able address-space and is physically contiguous so it can be accessed by the device without MMU translation. The command buffer memory is allocated using the coherent DMA API.

When creating a new CB, the IOCTL returns a handle of it, and the user-space process needs to use that handle to mmap the buffer to get a VA in the user's address-space.

Before destroying (freeing) a CB, the user must unmap the CB's VA using the CB handle.

Each CB has a reference counter, which tracks its usage in command submissions and also its mmaps (only a single mmap is allowed).

The driver maintains a pool of pre-allocated CBs in order to reduce latency during command submissions. In case the pool is empty, the driver will go to the slow-path of allocating a new CB, i.e. calling dma_alloc_coherent.

---
 drivers/misc/habanalabs/Makefile           |   3 +-
 drivers/misc/habanalabs/command_buffer.c   | 414 +++++++++++++++++++++
 drivers/misc/habanalabs/device.c           |  43 ++-
 drivers/misc/habanalabs/goya/goya.c        |  28 ++
 drivers/misc/habanalabs/habanalabs.h       |  95 ++++-
 drivers/misc/habanalabs/habanalabs_drv.c   |   2 +
 drivers/misc/habanalabs/habanalabs_ioctl.c | 102 +++++
 include/uapi/misc/habanalabs.h             |  62 +++
 8 files changed, 746 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/habanalabs/command_buffer.c
 create mode 100644 drivers/misc/habanalabs/habanalabs_ioctl.c
 create mode 100644 include/uapi/misc/habanalabs.h

[ ... ]

Quoted 34 lines
Quoted 26 lines
Quoted 26 lines
drivers/misc/habanalabs/command_buffer.c new file +26 -0
+int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
+                     u32 cb_size, u64 *handle, int ctx_id)
+{
+     struct hl_cb *cb;
+     bool alloc_new_cb = true;
+     int rc;
+
+     if (hdev->disabled) {
+             dev_warn_ratelimited(hdev->dev,
+                     "Device is disabled !!! Can't create new CBs\n");
+             rc = -EBUSY;
+             goto out_err;
+     }
+
+     /* Minimum allocation must be PAGE SIZE */
+     if (cb_size < PAGE_SIZE)
+             cb_size = PAGE_SIZE;
+
+     if (ctx_id == HL_KERNEL_ASID_ID &&
+                     cb_size <= hdev->asic_prop.cb_pool_cb_size) {
+
+             spin_lock(&hdev->cb_pool_lock);
+             if (!list_empty(&hdev->cb_pool)) {
+                     cb = list_first_entry(&hdev->cb_pool, typeof(*cb),
+                                     pool_list);
+                     list_del(&cb->pool_list);
Quoted 8 lines

drivers/misc/habanalabs/command_buffer.c line 107

Quoted 5 lines
drivers/misc/habanalabs/command_buffer.c new file +5 -0
+                     spin_unlock(&hdev->cb_pool_lock);
+                     alloc_new_cb = false;
+             } else {
+                     spin_unlock(&hdev->cb_pool_lock);
+                     dev_warn_once(hdev->dev, "CB pool is empty\n");

Isn't it going to be a false alarm when you allocate the cb for the first time?

Quoted 7 lines

Why ?
The cb_pool list holds a list of available CBs. See hl_cb_pool_init()
- it adds newly allocated CBs to this pool list.

if (!list_empty(&hdev->cb_pool)) { - this checks whether the
pool is not empty so we can take an available CB from it. If the list is empty (hence the pool is empty), we print the warning.

Sorry if it's too much nitpicking, but why the allocation of the first cb should be a warning? There's nothing wrong there... Maybe dev_dbg() instead?

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
drivers/misc/habanalabs/command_buffer.c new file +10 -0
+             }
+     }
+
+     if (alloc_new_cb) {
+             cb = hl_cb_alloc(hdev, cb_size, ctx_id);
+             if (!cb) {
+                     rc = -ENOMEM;
+                     goto out_err;
+             }
+     }
Quoted 8 lines

drivers/misc/habanalabs/command_buffer.c line 123

Quoted 6 lines
drivers/misc/habanalabs/command_buffer.c new file +6 -0
+
+     cb->hdev = hdev;
+     cb->ctx_id = ctx_id;
+
+     spin_lock(&mgr->cb_lock);
+     rc = idr_alloc(&mgr->cb_handles, cb, 1, 0, GFP_ATOMIC);

It seems the ID will remain dangling if the cb is reused.

Quoted 2 lines

I'm not sure what you mean by this comment. Reused by whom ? in how fashion it is reused ?

Sorry if I didn't explain it more clearly.
If the case the cb is reused, you anyway call idr_alloc() and overwrite the previous value of cb->id and it never gets idr_remove()'ed

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
drivers/misc/habanalabs/command_buffer.c new file +29 -0
+     spin_unlock(&mgr->cb_lock);
+
+     if (rc < 0) {
+             dev_err(hdev->dev, "Failed to allocate IDR for a new CB\n");
+             goto release_cb;
+     }
+
+     cb->id = rc;
+
+     kref_init(&cb->refcount);
+     spin_lock_init(&cb->lock);
+
+     /*
+      * idr is 32-bit so we can safely OR it with a mask that is above
+      * 32 bit
+      */
+     *handle = cb->id | HL_MMAP_CB_MASK;
+     *handle <<= PAGE_SHIFT;
+
+     return 0;
+
+release_cb:
+     cb_do_release(hdev, cb);
+out_err:
+     *handle = 0;
+
+     return rc;
+}
+
Signature · 2 lines
Sincerely yours,
Mike.
#27 lore raw

Re: [PATCH 05/15] habanalabs: add command buffer module

Quoted 86 lines · Mike Rapoport
Quoted 43 lines · Oded Gabbay
Quoted 6 lines · Mike Rapoport
Quoted 2 lines · Oded Gabbay

This patch adds the CB module, which allows the user to create and destroy CBs and to map them to the user's process address-space.

Can you please spell "command buffer" at least first time it's mentioned?

fixed

Quoted 35 lines
Quoted 35 lines

A command buffer is a memory blocks that reside in DMA-able address-space and is physically contiguous so it can be accessed by the device without MMU translation. The command buffer memory is allocated using the coherent DMA API.

When creating a new CB, the IOCTL returns a handle of it, and the user-space process needs to use that handle to mmap the buffer to get a VA in the user's address-space.

Before destroying (freeing) a CB, the user must unmap the CB's VA using the CB handle.

Each CB has a reference counter, which tracks its usage in command submissions and also its mmaps (only a single mmap is allowed).

The driver maintains a pool of pre-allocated CBs in order to reduce latency during command submissions. In case the pool is empty, the driver will go to the slow-path of allocating a new CB, i.e. calling dma_alloc_coherent.

---
 drivers/misc/habanalabs/Makefile           |   3 +-
 drivers/misc/habanalabs/command_buffer.c   | 414 +++++++++++++++++++++
 drivers/misc/habanalabs/device.c           |  43 ++-
 drivers/misc/habanalabs/goya/goya.c        |  28 ++
 drivers/misc/habanalabs/habanalabs.h       |  95 ++++-
 drivers/misc/habanalabs/habanalabs_drv.c   |   2 +
 drivers/misc/habanalabs/habanalabs_ioctl.c | 102 +++++
 include/uapi/misc/habanalabs.h             |  62 +++
 8 files changed, 746 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/habanalabs/command_buffer.c
 create mode 100644 drivers/misc/habanalabs/habanalabs_ioctl.c
 create mode 100644 include/uapi/misc/habanalabs.h

[ ... ]

Quoted 38 lines
Quoted 26 lines
Quoted 26 lines
drivers/misc/habanalabs/command_buffer.c new file +26 -0
+int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
+                     u32 cb_size, u64 *handle, int ctx_id)
+{
+     struct hl_cb *cb;
+     bool alloc_new_cb = true;
+     int rc;
+
+     if (hdev->disabled) {
+             dev_warn_ratelimited(hdev->dev,
+                     "Device is disabled !!! Can't create new CBs\n");
+             rc = -EBUSY;
+             goto out_err;
+     }
+
+     /* Minimum allocation must be PAGE SIZE */
+     if (cb_size < PAGE_SIZE)
+             cb_size = PAGE_SIZE;
+
+     if (ctx_id == HL_KERNEL_ASID_ID &&
+                     cb_size <= hdev->asic_prop.cb_pool_cb_size) {
+
+             spin_lock(&hdev->cb_pool_lock);
+             if (!list_empty(&hdev->cb_pool)) {
+                     cb = list_first_entry(&hdev->cb_pool, typeof(*cb),
+                                     pool_list);
+                     list_del(&cb->pool_list);
Quoted 8 lines

drivers/misc/habanalabs/command_buffer.c line 107

Quoted 5 lines
drivers/misc/habanalabs/command_buffer.c new file +5 -0
+                     spin_unlock(&hdev->cb_pool_lock);
+                     alloc_new_cb = false;
+             } else {
+                     spin_unlock(&hdev->cb_pool_lock);
+                     dev_warn_once(hdev->dev, "CB pool is empty\n");

Isn't it going to be a false alarm when you allocate the cb for the first time?

Why ?
The cb_pool list holds a list of available CBs. See hl_cb_pool_init()
- it adds newly allocated CBs to this pool list.

Quoted 7 lines
Quoted 3 lines

if (!list_empty(&hdev->cb_pool)) { - this checks whether the
pool is not empty so we can take an available CB from it. If the list is empty (hence the pool is empty), we print the warning.

Sorry if it's too much nitpicking, but why the allocation of the first cb should be a warning? There's nothing wrong there... Maybe dev_dbg() instead?

Yeah, that's a fair point. The issue is I would like to know if we reach to this state and dev_dbg isn't usually enabled. Still, I get what you are saying and I'll change this to dev_dbg.

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 7 lines
drivers/misc/habanalabs/command_buffer.c new file +6 -0
+             }
+     }
+
+     if (alloc_new_cb) {
+             cb = hl_cb_alloc(hdev, cb_size, ctx_id);
+             if (!cb) {

drivers/misc/habanalabs/command_buffer.c line 123

Quoted 10 lines
drivers/misc/habanalabs/command_buffer.c new file +10 -0
+                     rc = -ENOMEM;
+                     goto out_err;
+             }
+     }
+
+     cb->hdev = hdev;
+     cb->ctx_id = ctx_id;
+
+     spin_lock(&mgr->cb_lock);
+     rc = idr_alloc(&mgr->cb_handles, cb, 1, 0, GFP_ATOMIC);

It seems the ID will remain dangling if the cb is reused.

Quoted 6 lines
Quoted 2 lines

I'm not sure what you mean by this comment. Reused by whom ? in how fashion it is reused ?

Sorry if I didn't explain it more clearly.
If the case the cb is reused, you anyway call idr_alloc() and overwrite the previous value of cb->id and it never gets idr_remove()'ed

I don't think that is the case.
Please look at hl_cb_destroy(). There, we do the idr_remove and then we kref_put the CB. In it's release code path, we check if this is a CB from pool, and if so, we return it to the pool. When it will be alloc'ed again, it will get a new id.
The problem in this patch is that hl_cb_destroy is not used yet for CB's from the pool because the command submission code which use that comes at a later patch, so indeed it might be confusing. But if you will take a look at the entire code and check when hl_cb_destroy is called I think you will agree with me.
But if you still think otherwise, please tell me. I might be missing something here.

Thanks,
Oded

Quoted 36 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
drivers/misc/habanalabs/command_buffer.c new file +21 -0
+     spin_unlock(&mgr->cb_lock);
+
+     if (rc < 0) {
+             dev_err(hdev->dev, "Failed to allocate IDR for a new CB\n");
+             goto release_cb;
+     }
+
+     cb->id = rc;
+
+     kref_init(&cb->refcount);
+     spin_lock_init(&cb->lock);
+
+     /*
+      * idr is 32-bit so we can safely OR it with a mask that is above
+      * 32 bit
+      */
+     *handle = cb->id | HL_MMAP_CB_MASK;
+     *handle <<= PAGE_SHIFT;
+
+     return 0;
+
Quoted 8 lines
Quoted 8 lines

drivers/misc/habanalabs/command_buffer.c line 152

Quoted 8 lines
drivers/misc/habanalabs/command_buffer.c new file +8 -0
+release_cb:
+     cb_do_release(hdev, cb);
+out_err:
+     *handle = 0;
+
+     return rc;
+}
+

--
Sincerely yours,
Mike.

#28 lore raw

Re: [PATCH 05/15] habanalabs: add command buffer module

Quoted 124 lines · Oded Gabbay
Quoted 86 lines · Mike Rapoport
Quoted 43 lines · Oded Gabbay
Quoted 6 lines · Mike Rapoport
Quoted 2 lines · Oded Gabbay

This patch adds the CB module, which allows the user to create and destroy CBs and to map them to the user's process address-space.

Can you please spell "command buffer" at least first time it's mentioned?

fixed

Quoted 35 lines
Quoted 35 lines

A command buffer is a memory blocks that reside in DMA-able address-space and is physically contiguous so it can be accessed by the device without MMU translation. The command buffer memory is allocated using the coherent DMA API.

When creating a new CB, the IOCTL returns a handle of it, and the user-space process needs to use that handle to mmap the buffer to get a VA in the user's address-space.

Before destroying (freeing) a CB, the user must unmap the CB's VA using the CB handle.

Each CB has a reference counter, which tracks its usage in command submissions and also its mmaps (only a single mmap is allowed).

The driver maintains a pool of pre-allocated CBs in order to reduce latency during command submissions. In case the pool is empty, the driver will go to the slow-path of allocating a new CB, i.e. calling dma_alloc_coherent.

---
 drivers/misc/habanalabs/Makefile           |   3 +-
 drivers/misc/habanalabs/command_buffer.c   | 414 +++++++++++++++++++++
 drivers/misc/habanalabs/device.c           |  43 ++-
 drivers/misc/habanalabs/goya/goya.c        |  28 ++
 drivers/misc/habanalabs/habanalabs.h       |  95 ++++-
 drivers/misc/habanalabs/habanalabs_drv.c   |   2 +
 drivers/misc/habanalabs/habanalabs_ioctl.c | 102 +++++
 include/uapi/misc/habanalabs.h             |  62 +++
 8 files changed, 746 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/habanalabs/command_buffer.c
 create mode 100644 drivers/misc/habanalabs/habanalabs_ioctl.c
 create mode 100644 include/uapi/misc/habanalabs.h

[ ... ]

Quoted 38 lines
Quoted 26 lines
Quoted 26 lines
drivers/misc/habanalabs/command_buffer.c new file +26 -0
+int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
+                     u32 cb_size, u64 *handle, int ctx_id)
+{
+     struct hl_cb *cb;
+     bool alloc_new_cb = true;
+     int rc;
+
+     if (hdev->disabled) {
+             dev_warn_ratelimited(hdev->dev,
+                     "Device is disabled !!! Can't create new CBs\n");
+             rc = -EBUSY;
+             goto out_err;
+     }
+
+     /* Minimum allocation must be PAGE SIZE */
+     if (cb_size < PAGE_SIZE)
+             cb_size = PAGE_SIZE;
+
+     if (ctx_id == HL_KERNEL_ASID_ID &&
+                     cb_size <= hdev->asic_prop.cb_pool_cb_size) {
+
+             spin_lock(&hdev->cb_pool_lock);
+             if (!list_empty(&hdev->cb_pool)) {
+                     cb = list_first_entry(&hdev->cb_pool, typeof(*cb),
+                                     pool_list);
+                     list_del(&cb->pool_list);
Quoted 8 lines

drivers/misc/habanalabs/command_buffer.c line 107

Quoted 5 lines
drivers/misc/habanalabs/command_buffer.c new file +5 -0
+                     spin_unlock(&hdev->cb_pool_lock);
+                     alloc_new_cb = false;
+             } else {
+                     spin_unlock(&hdev->cb_pool_lock);
+                     dev_warn_once(hdev->dev, "CB pool is empty\n");

Isn't it going to be a false alarm when you allocate the cb for the first time?

Why ?
The cb_pool list holds a list of available CBs. See hl_cb_pool_init()
- it adds newly allocated CBs to this pool list.

Quoted 7 lines
Quoted 3 lines

if (!list_empty(&hdev->cb_pool)) { - this checks whether the
pool is not empty so we can take an available CB from it. If the list is empty (hence the pool is empty), we print the warning.

Sorry if it's too much nitpicking, but why the allocation of the first cb should be a warning? There's nothing wrong there... Maybe dev_dbg() instead?

Yeah, that's a fair point. The issue is I would like to know if we reach to this state and dev_dbg isn't usually enabled. Still, I get what you are saying and I'll change this to dev_dbg.

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 7 lines
drivers/misc/habanalabs/command_buffer.c new file +6 -0
+             }
+     }
+
+     if (alloc_new_cb) {
+             cb = hl_cb_alloc(hdev, cb_size, ctx_id);
+             if (!cb) {

drivers/misc/habanalabs/command_buffer.c line 123

Quoted 10 lines
drivers/misc/habanalabs/command_buffer.c new file +10 -0
+                     rc = -ENOMEM;
+                     goto out_err;
+             }
+     }
+
+     cb->hdev = hdev;
+     cb->ctx_id = ctx_id;
+
+     spin_lock(&mgr->cb_lock);
+     rc = idr_alloc(&mgr->cb_handles, cb, 1, 0, GFP_ATOMIC);

It seems the ID will remain dangling if the cb is reused.

Quoted 6 lines
Quoted 2 lines

I'm not sure what you mean by this comment. Reused by whom ? in how fashion it is reused ?

Sorry if I didn't explain it more clearly.
If the case the cb is reused, you anyway call idr_alloc() and overwrite the previous value of cb->id and it never gets idr_remove()'ed

Quoted 12 lines

I don't think that is the case.
Please look at hl_cb_destroy(). There, we do the idr_remove and then we kref_put the CB. In it's release code path, we check if this is a CB from pool, and if so, we return it to the pool. When it will be alloc'ed again, it will get a new id.
The problem in this patch is that hl_cb_destroy is not used yet for CB's from the pool because the command submission code which use that comes at a later patch, so indeed it might be confusing. But if you will take a look at the entire code and check when hl_cb_destroy is called I think you will agree with me.
But if you still think otherwise, please tell me. I might be missing something here.

Right, hl_cb_create and hl_cb_destroy are indeed paired. Frankly, I was too lazy to thoroughly check hl_device_release() case when userspace didn't free all the cb's, but, apparently it also does the required cleanup.

Quoted 40 lines

Thanks,
Oded

Quoted 36 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
drivers/misc/habanalabs/command_buffer.c new file +21 -0
+     spin_unlock(&mgr->cb_lock);
+
+     if (rc < 0) {
+             dev_err(hdev->dev, "Failed to allocate IDR for a new CB\n");
+             goto release_cb;
+     }
+
+     cb->id = rc;
+
+     kref_init(&cb->refcount);
+     spin_lock_init(&cb->lock);
+
+     /*
+      * idr is 32-bit so we can safely OR it with a mask that is above
+      * 32 bit
+      */
+     *handle = cb->id | HL_MMAP_CB_MASK;
+     *handle <<= PAGE_SHIFT;
+
+     return 0;
+
Quoted 8 lines
Quoted 8 lines

drivers/misc/habanalabs/command_buffer.c line 152

Quoted 8 lines
drivers/misc/habanalabs/command_buffer.c new file +8 -0
+release_cb:
+     cb_do_release(hdev, cb);
+out_err:
+     *handle = 0;
+
+     return rc;
+}
+

--
Sincerely yours,
Mike.

Signature · 2 lines
Sincerely yours,
Mike.
#29 lore raw

PATCH 6/15 habanalabs: add basic Goya h/w initialization

In reply to Oded Gabbay

This patch adds the basic part of Goya's H/W initialization. It adds code that initializes Goya's internal CPU, various registers that are related to internal routing, scrambling, workarounds for H/W bugs, etc.

It also initializes Goya's security scheme that prevents the user from abusing Goya to steal data from the host, crash the host, change Goya's F/W, etc.

---
 drivers/misc/habanalabs/device.c              |   12 +
 drivers/misc/habanalabs/goya/Makefile         |    2 +-
 drivers/misc/habanalabs/goya/goya.c           | 1892 ++++++++++-
 drivers/misc/habanalabs/goya/goyaP.h          |    3 +
 drivers/misc/habanalabs/goya/goya_security.c  | 2999 +++++++++++++++++
 drivers/misc/habanalabs/habanalabs.h          |   16 +
 drivers/misc/habanalabs/habanalabs_drv.c      |    8 +
 drivers/misc/habanalabs/include/goya/goya.h   |    1 +
 .../include/goya/goya_async_events.h          |  186 +
 .../habanalabs/include/goya/goya_boot_if.h    |   32 +
 10 files changed, 5144 insertions(+), 7 deletions(-)
 create mode 100644 drivers/misc/habanalabs/goya/goya_security.c
 create mode 100644 drivers/misc/habanalabs/include/goya/goya_async_events.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya_boot_if.h
drivers/misc/habanalabs/device.c +12 -0
index 0bd86a7d34db..9fc7218a973c 100644
@@ -315,6 +315,15 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)
 		goto release_ctx;
 	}
 
+	rc = hdev->asic_funcs->hw_init(hdev);
+	if (rc) {
+		dev_err(hdev->dev, "failed to initialize the H/W\n");
+		rc = 0;
+		goto out_disabled;
+	}
+
+	hdev->disabled = false;
+
 	dev_notice(hdev->dev,
 		"Successfully added device to habanalabs driver\n");
 
@@ -366,6 +375,9 @@ void hl_device_fini(struct hl_device *hdev)
 	if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
 		dev_err(hdev->dev, "kernel ctx is still alive\n");
 
+	/* Reset the H/W. It will be in idle state after this returns */
+	hdev->asic_funcs->hw_fini(hdev, true);
+
 	/* Call ASIC S/W finalize function */
 	hdev->asic_funcs->sw_fini(hdev);
 
drivers/misc/habanalabs/goya/Makefile +1 -1
index 5ebf3d0d5794..a57096fa41b6 100644
@@ -1,3 +1,3 @@
 subdir-ccflags-y += -I$(src)
 
-HL_GOYA_FILES :=  goya/goya.o
\ No newline at end of file
+HL_GOYA_FILES :=  goya/goya.o goya/goya_security.o
\ No newline at end of file
drivers/misc/habanalabs/goya/goya.c +1886 -6

1,919 lines not on this page · raw message

drivers/misc/habanalabs/goya/goyaP.h +3 -0
index 0e12c56472bd..45a6d2ca2752 100644
@@ -9,6 +9,7 @@
 #define GOYAP_H_
 
 #include "habanalabs.h"
+#include "include/goya/goya_boot_if.h"
 #include "include/goya/goya.h"
 
 #define NUMBER_OF_CMPLT_QUEUES		5
@@ -122,4 +123,6 @@ struct goya_device {
 	u32		hw_cap_initialized;
 };
 
+void goya_init_security(struct hl_device *hdev);
+
 #endif /* GOYAP_H_ */
drivers/misc/habanalabs/goya/goya_security.c new file +2999 -0

1,038 lines not on this page · raw message

drivers/misc/habanalabs/habanalabs.h +16 -0
index 6ad476df65b0..adda281ec2af 100644

Lines of this file are not shown. View the raw message.

drivers/misc/habanalabs/habanalabs_drv.c +8 -0
index 5c312dd3aa50..bd80683118d3 100644

Lines of this file are not shown. View the raw message.

drivers/misc/habanalabs/include/goya/goya.h +1 -0
index 192a1450cbb1..2d0efb7b44bb 100644

Lines of this file are not shown. View the raw message.

drivers/misc/habanalabs/include/goya/goya_async_events.h new file +186 -0

186 lines not on this page · raw message

drivers/misc/habanalabs/include/goya/goya_boot_if.h new file +32 -0
new file mode 100644
index 000000000000..2e39578ec795

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.17.1

Part of the diff is not shown here. View the raw message.

#30 lore raw

Re: [PATCH 06/15] habanalabs: add basic Goya h/w initialization

Hi,

This starts the 6-9 review :)

These were more difficult to review because small pieces of code are interleaved with large sequences of register writes. Probably making these register data rather than code can help.

Quoted 30 lines · Oded Gabbay

This patch adds the basic part of Goya's H/W initialization. It adds code that initializes Goya's internal CPU, various registers that are related to internal routing, scrambling, workarounds for H/W bugs, etc.

It also initializes Goya's security scheme that prevents the user from abusing Goya to steal data from the host, crash the host, change Goya's F/W, etc.

---
 drivers/misc/habanalabs/device.c              |   12 +
 drivers/misc/habanalabs/goya/Makefile         |    2 +-
 drivers/misc/habanalabs/goya/goya.c           | 1892 ++++++++++-
 drivers/misc/habanalabs/goya/goyaP.h          |    3 +
 drivers/misc/habanalabs/goya/goya_security.c  | 2999 +++++++++++++++++
 drivers/misc/habanalabs/habanalabs.h          |   16 +
 drivers/misc/habanalabs/habanalabs_drv.c      |    8 +
 drivers/misc/habanalabs/include/goya/goya.h   |    1 +
 .../include/goya/goya_async_events.h          |  186 +
 .../habanalabs/include/goya/goya_boot_if.h    |   32 +
 10 files changed, 5144 insertions(+), 7 deletions(-)
 create mode 100644 drivers/misc/habanalabs/goya/goya_security.c
 create mode 100644 drivers/misc/habanalabs/include/goya/goya_async_events.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya_boot_if.h
drivers/misc/habanalabs/device.c +0 -0
index 0bd86a7d34db..9fc7218a973c 100644

drivers/misc/habanalabs/device.c int hl_device_init(struct hl_device *hdev, struct class *hclass) · line 321

Quoted 7 lines
drivers/misc/habanalabs/device.c +4 -0
@@ -315,6 +315,15 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)
 		goto release_ctx;
 	}
 
+	rc = hdev->asic_funcs->hw_init(hdev);
+	if (rc) {
+		dev_err(hdev->dev, "failed to initialize the H/W\n");
+		rc = 0;

Mistype, I suppose.

Quoted 48 lines
drivers/misc/habanalabs/device.c +8 -0
+		goto out_disabled;
+	}
+
+	hdev->disabled = false;
+
 	dev_notice(hdev->dev,
 		"Successfully added device to habanalabs driver\n");
 
@@ -366,6 +375,9 @@ void hl_device_fini(struct hl_device *hdev)
 	if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
 		dev_err(hdev->dev, "kernel ctx is still alive\n");
 
+	/* Reset the H/W. It will be in idle state after this returns */
+	hdev->asic_funcs->hw_fini(hdev, true);
+
 	/* Call ASIC S/W finalize function */
 	hdev->asic_funcs->sw_fini(hdev);
 
drivers/misc/habanalabs/goya/Makefile +1 -1
index 5ebf3d0d5794..a57096fa41b6 100644
@@ -1,3 +1,3 @@
 subdir-ccflags-y += -I$(src)
 
-HL_GOYA_FILES :=  goya/goya.o
\ No newline at end of file
+HL_GOYA_FILES :=  goya/goya.o goya/goya_security.o
\ No newline at end of file
drivers/misc/habanalabs/goya/goya.c +2 -2
index 341ac085af82..f715e01838b3 100644
@@ -119,11 +119,11 @@ static void goya_get_fixed_properties(struct hl_device *hdev)
 	prop->va_space_dram_end_address = VA_DDR_SPACE_END;
 	prop->cfg_size = CFG_SIZE;
 	prop->max_asid = MAX_ASID;
+	prop->cb_pool_cb_cnt = GOYA_CB_POOL_CB_CNT;
+	prop->cb_pool_cb_size = GOYA_CB_POOL_CB_SIZE;
 	prop->tpc_enabled_mask = TPC_ENABLED_MASK;
 
 	prop->high_pll = PLL_HIGH_DEFAULT;
-	prop->cb_pool_cb_cnt = GOYA_CB_POOL_CB_CNT;
-	prop->cb_pool_cb_size = GOYA_CB_POOL_CB_SIZE;
 }
 
 /**

drivers/misc/habanalabs/goya/goya.c static int goya_early_init(struct hl_device *hdev) · line 463

Quoted 9 lines
drivers/misc/habanalabs/goya/goya.c +2 -4
@@ -459,10 +459,12 @@ static int goya_early_init(struct hl_device *hdev)
 		goto disable_device;
 	}
 
-	val = RREG32(mmPSOC_GLOBAL_CONF_BOOT_STRAP_PINS);
-	if (val & PSOC_GLOBAL_CONF_BOOT_STRAP_PINS_SRIOV_EN_MASK)
-		dev_warn(hdev->dev,
-			"PCI strap is not configured correctly, PCI bus errors may occur\n");
+	if (!hdev->pldm) {
+		val = RREG32(mmPSOC_GLOBAL_CONF_BOOT_STRAP_PINS);

What is the purpose of the 'mm' prefix in register names?

Quoted 61 lines
drivers/misc/habanalabs/goya/goya.c +54 -0
+		if (val & PSOC_GLOBAL_CONF_BOOT_STRAP_PINS_SRIOV_EN_MASK)
+			dev_warn(hdev->dev,
+				"PCI strap is not configured correctly, PCI bus errors may occur\n");
+	}
 
 	return 0;
 
@@ -593,6 +595,1882 @@ int goya_sw_fini(struct hl_device *hdev)
 	return 0;
 }
 
+/**
+ * goya_init_pll - Initialize pll registers
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+static void goya_init_pll(struct hl_device *hdev)
+{
+	struct goya_device *goya = hdev->asic_specific;
+	u16 hbw_nr, hbw_nf, hbw_od, hbw_nb;
+	u16 cpu_nr, cpu_nf, cpu_od, cpu_nb;
+	u16 mc_nr, mc_nf, mc_od, mc_nb;
+	u16 pci_nr, pci_nf, pci_od, pci_nb;
+	u16 emmc_nr, emmc_nf, emmc_od, emmc_nb;
+
+	if (!hdev->config_pll)
+		return;
+
+	if (goya->hw_cap_initialized & HW_CAP_PLL)
+		return;
+
+	if (hdev->cpu_enable) {
+		dev_info(hdev->dev,
+			"Waiting 5s for u-boot before configuring PLLs\n");
+		ssleep(5);
+	}
+
+/*
+ * PLL possible configuration values:
+	{50000000,1,16,16,8},
+	{100000000,1,32,16,16},
+	{150000000,1,48,16,24},
+	{200000000,1,64,16,32},
+	{250000000,1,70,14,35},
+	{300000000,1,60,10,30},
+	{350000000,1,70,10,35},
+	{400000000,1,64,8,32},
+	{450000000,1,54,6,27},
+	{500000000,1,60,6,30},
+	{550000000,1,66,6,33},
+	{600000000,1,48,4,24},
+	{650000000,1,52,4,26},
+	{700000000,1,56,4,28},
+	{750000000,1,60,4,30},
+	{800000000,1,64,4,32},
+	{850000000,1,68,4,34},
+	{900000000,1,36,2,18},
+	{950000000,1,38,2,19},
+	{1000000000,1,40,2,20},
+	{1050000000,1,42,2,21},

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 657

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+	{1100000000,1,44,2,22},
+	{1150000000,1,46,2,23},
+	{1200000000,1,48,2,24},
+	{1250000000,1,50,2,25},
+	{1300000000,1,52,2,26},
+	{1350000000,1,54,2,27},
+	{1400000000,1,56,2,28},
+	{1450000000,1,58,2,29},
+	{1500000000,1,60,2,30},
+	{1550000000,1,62,2,31},

Some explanation about the correspondence of these values to _nr, _nf, _od and _nb would be helpfull.

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 660

Quoted 3 lines
drivers/misc/habanalabs/goya/goya.c +3 -0
+*/
+
+	if (hdev->pldm) {
                /* ? MHz */

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 670

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+		hbw_nr  = 4, hbw_nf  = 302, hbw_od  = 1, hbw_nb  = 151;
+		cpu_nr  = 0, cpu_nf  = 47, cpu_od  = 1, cpu_nb  = 32;
+		mc_nr   = 1, mc_nf   = 159, mc_od   = 9, mc_nb   = 79;
+		pci_nr  = 4, pci_nf  = 343, pci_od  = 3, pci_nb  = 171;
+		emmc_nr = 24, emmc_nf = 415, emmc_od = 15, emmc_nb = 207;
+	} else {
+		/* 200MHz */
+		hbw_nr  = 0, hbw_nf  = 63, hbw_od  = 15, hbw_nb  = 31;
+		cpu_nr  = 0, cpu_nf  = 47, cpu_od  = 1, cpu_nb  = 23;
+		mc_nr   = 2, mc_nf   = 0x9f, mc_od   = 3, mc_nb   = 0x4f;

The hex here looks inconsistent.

Quoted 17 lines
drivers/misc/habanalabs/goya/goya.c +17 -0
+		pci_nr  = 4, pci_nf  = 343, pci_od  = 3, pci_nb  = 171;
+		emmc_nr = 24, emmc_nf = 415, emmc_od = 15, emmc_nb = 207;
+	}
+
+	/* Adjust divider for SPI */
+	WREG32(mmPSOC_SPI_BAUDR, 8);
+
+	WREG32(mmCPU_PLL_RST, 1);
+	WREG32(mmCPU_PLL_NR, cpu_nr);
+	WREG32(mmCPU_PLL_NF, cpu_nf);
+	WREG32(mmCPU_PLL_OD, cpu_od);
+	WREG32(mmCPU_PLL_NB, cpu_nb);
+	WREG32(mmCPU_PLL_DATA_CHNG, 0x11);
+
+	/* delay before taking PLL out of reset */
+	udelay(100);
+

[ ... ]

Quoted 334 lines
drivers/misc/habanalabs/goya/goya.c +334 -0
+
+	goya->hw_cap_initialized |= HW_CAP_PLL;
+}
+
+static void goya_set_pll_refclk(struct hl_device *hdev)
+{
+	WREG32(mmCPU_PLL_DIV_SEL_0, 0x0);
+	WREG32(mmCPU_PLL_DIV_SEL_1, 0x0);
+	WREG32(mmCPU_PLL_DIV_SEL_2, 0x0);
+	WREG32(mmCPU_PLL_DIV_SEL_3, 0x0);
+
+	WREG32(mmIC_PLL_DIV_SEL_0, 0x0);
+	WREG32(mmIC_PLL_DIV_SEL_1, 0x0);
+	WREG32(mmIC_PLL_DIV_SEL_2, 0x0);
+	WREG32(mmIC_PLL_DIV_SEL_3, 0x0);
+
+	WREG32(mmMC_PLL_DIV_SEL_0, 0x0);
+	WREG32(mmMC_PLL_DIV_SEL_1, 0x0);
+	WREG32(mmMC_PLL_DIV_SEL_2, 0x0);
+	WREG32(mmMC_PLL_DIV_SEL_3, 0x0);
+
+	WREG32(mmPSOC_MME_PLL_DIV_SEL_0, 0x0);
+	WREG32(mmPSOC_MME_PLL_DIV_SEL_1, 0x0);
+	WREG32(mmPSOC_MME_PLL_DIV_SEL_2, 0x0);
+	WREG32(mmPSOC_MME_PLL_DIV_SEL_3, 0x0);
+
+	WREG32(mmPSOC_PCI_PLL_DIV_SEL_0, 0x0);
+	WREG32(mmPSOC_PCI_PLL_DIV_SEL_1, 0x0);
+	WREG32(mmPSOC_PCI_PLL_DIV_SEL_2, 0x0);
+	WREG32(mmPSOC_PCI_PLL_DIV_SEL_3, 0x0);
+
+	WREG32(mmPSOC_EMMC_PLL_DIV_SEL_0, 0x0);
+	WREG32(mmPSOC_EMMC_PLL_DIV_SEL_1, 0x0);
+	WREG32(mmPSOC_EMMC_PLL_DIV_SEL_2, 0x0);
+	WREG32(mmPSOC_EMMC_PLL_DIV_SEL_3, 0x0);
+
+	WREG32(mmTPC_PLL_DIV_SEL_0, 0x0);
+	WREG32(mmTPC_PLL_DIV_SEL_1, 0x0);
+	WREG32(mmTPC_PLL_DIV_SEL_2, 0x0);
+	WREG32(mmTPC_PLL_DIV_SEL_3, 0x0);
+}
+
+static void goya_disable_clk_rlx(struct hl_device *hdev)
+{
+	WREG32(mmPSOC_MME_PLL_CLK_RLX_0, 0x100010);
+	WREG32(mmIC_PLL_CLK_RLX_0, 0x100010);
+}
+
+/**
+ * goya_init_ddr_ch0 - Initialize DDR CH0 controller of the chip
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+static void goya_init_ddr_ch0(struct hl_device *hdev)
+{
+	struct goya_device *goya = hdev->asic_specific;
+	u32 val;
+
+	if (goya->hw_cap_initialized & HW_CAP_DDR_0)
+		return;
+
+	val = RREG32(mmDDR_MISC_CH0_CFG_DONE);
+	if (val & DDR_MISC_CH0_CFG_DONE_CFG_DONE_MASK) {
+		goya->hw_cap_initialized |= HW_CAP_DDR_0;
+		return;
+	}
+
+	WREG32(mmDDR_MC_CH0_DBG1, 0x00000001);
+	WREG32(mmDDR_MC_CH0_PWRCTL, 0x00000001);
+
+	val = RREG32(mmDDR_MC_CH0_STAT);
+
+	WREG32(mmDDR_MC_CH0_MSTR, 0x81040210);
+	WREG32(mmDDR_MC_CH0_MRCTRL0, 0x4000a0f0);
+	WREG32(mmDDR_MC_CH0_MRCTRL1, 0x00022ad0);
+	WREG32(mmDDR_MC_CH0_MRCTRL2, 0x091629e1);
+	WREG32(mmDDR_MC_CH0_PWRCTL, 0x00000008);
+	WREG32(mmDDR_MC_CH0_PWRTMG, 0x00040002);
+	WREG32(mmDDR_MC_CH0_HWLPCTL, 0x00be0002);
+	WREG32(mmDDR_MC_CH0_RFSHCTL0, 0x0091f020);
+	WREG32(mmDDR_MC_CH0_RFSHCTL1, 0x00120018);
+	WREG32((mmDDR_MC_CH0_MSTR + 0x00000058), 0x00160005);
+	WREG32(mmDDR_MC_CH0_RFSHCTL3, 0x00000020);
+	WREG32(mmDDR_MC_CH0_RFSHTMG, 0x003000d0);
+	WREG32(mmDDR_MC_CH0_ECCCFG0, 0x00000010);
+	WREG32(mmDDR_MC_CH0_ECCCFG1, 0x00000002);
+	WREG32(mmDDR_MC_CH0_ECCCTL, 0x00000300);
+	WREG32(mmDDR_MC_CH0_ECCPOISONADDR0, 0x00000078);
+	WREG32(mmDDR_MC_CH0_ECCPOISONADDR1, 0x100062f7);
+	WREG32(mmDDR_MC_CH0_CRCPARCTL0, 0x00008000);
+	WREG32(mmDDR_MC_CH0_CRCPARCTL1, 0x0e088301);
+	WREG32(mmDDR_MC_CH0_CRCPARCTL2, 0x00600527);
+	WREG32(mmDDR_MC_CH0_INIT0, 0x00070002);
+	WREG32(mmDDR_MC_CH0_INIT1, 0x0001000e);
+	WREG32(mmDDR_MC_CH0_INIT3, 0x0c510001);
+	WREG32(mmDDR_MC_CH0_INIT4, 0x00280400);
+	WREG32(mmDDR_MC_CH0_INIT5, 0x00110000);
+	WREG32(mmDDR_MC_CH0_INIT6, 0x02000643);
+	WREG32(mmDDR_MC_CH0_INIT7, 0x00001000);
+	WREG32(mmDDR_MC_CH0_DIMMCTL, 0x00000000);
+	WREG32(mmDDR_MC_CH0_RANKCTL, 0x000009a0);
+	WREG32(mmDDR_MC_CH0_DRAMTMG0, 0x1918361a);
+	WREG32(mmDDR_MC_CH0_DRAMTMG1, 0x00080724);
+	WREG32(mmDDR_MC_CH0_DRAMTMG2, 0x080d0713);
+	WREG32(mmDDR_MC_CH0_DRAMTMG3, 0x00012012);
+	WREG32(mmDDR_MC_CH0_DRAMTMG4, 0x0b04060b);
+	WREG32(mmDDR_MC_CH0_DRAMTMG5, 0x0a0c0804);
+	WREG32(mmDDR_MC_CH0_DRAMTMG8, 0x0606490c);
+	WREG32(mmDDR_MC_CH0_DRAMTMG9, 0x0002050f);
+	WREG32(mmDDR_MC_CH0_DRAMTMG10, 0x000e0d0f);
+	WREG32(mmDDR_MC_CH0_DRAMTMG11, 0x270b011f);
+	WREG32(mmDDR_MC_CH0_DRAMTMG12, 0x00000010);
+	WREG32(mmDDR_MC_CH0_DRAMTMG15, 0x00000000);
+	WREG32(mmDDR_MC_CH0_ZQCTL0, 0x31000040);
+	WREG32(mmDDR_MC_CH0_ZQCTL1, 0x00000070);
+	WREG32(mmDDR_MC_CH0_DFITMG0, 0x05978211);
+	WREG32(mmDDR_MC_CH0_DFITMG1, 0x00080101);
+	WREG32(mmDDR_MC_CH0_DFILPCFG0, 0x07006031);
+	WREG32(mmDDR_MC_CH0_DFILPCFG1, 0x00000010);
+	WREG32(mmDDR_MC_CH0_DFIUPD0, 0x40400018);
+	WREG32(mmDDR_MC_CH0_DFIUPD1, 0x000b0046);
+	WREG32(mmDDR_MC_CH0_DFIUPD2, 0x00000000);
+	WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000041);
+	WREG32(mmDDR_MC_CH0_DFITMG2, 0x00001711);
+	WREG32(mmDDR_MC_CH0_DFITMG3, 0x0000001e);
+	WREG32(mmDDR_MC_CH0_DBICTL, 0x00000001);
+	WREG32(mmDDR_MC_CH0_DFIPHYMSTR, 0x00000000);
+	WREG32(mmDDR_MC_CH0_ADDRMAP0, 0x00001f1f);
+	WREG32(mmDDR_MC_CH0_ADDRMAP1, 0x003f1503);
+	WREG32(mmDDR_MC_CH0_ADDRMAP2, 0x01000400);
+	WREG32(mmDDR_MC_CH0_ADDRMAP3, 0x04000505);
+	WREG32(mmDDR_MC_CH0_ADDRMAP4, 0x00001f1f);
+	WREG32(mmDDR_MC_CH0_ADDRMAP5, 0x06060303);
+	WREG32(mmDDR_MC_CH0_ADDRMAP6, 0x0f050709);
+	WREG32(mmDDR_MC_CH0_ADDRMAP7, 0x00000f0f);
+	WREG32(mmDDR_MC_CH0_ADDRMAP8, 0x00003f01);
+	WREG32(mmDDR_MC_CH0_ADDRMAP9, 0x09000606);
+	WREG32(mmDDR_MC_CH0_ADDRMAP10, 0x02090105);
+	WREG32(mmDDR_MC_CH0_ADDRMAP11, 0x0000000a);
+	WREG32(mmDDR_MC_CH0_ODTCFG, 0x09090a08);
+	WREG32(mmDDR_MC_CH0_ODTMAP, 0x9ae1b5fe);
+	WREG32(mmDDR_MC_CH0_SCHED, 0x664d3700);
+	WREG32(mmDDR_MC_CH0_SCHED1, 0x00000000);
+	WREG32(mmDDR_MC_CH0_PERFHPR1, 0x1700e024);
+	WREG32(mmDDR_MC_CH0_PERFLPR1, 0x1e00836c);
+	WREG32(mmDDR_MC_CH0_PERFWR1, 0x260046c9);
+	WREG32(mmDDR_MC_CH0_DQMAP0, 0x0d2b3503);
+	WREG32(mmDDR_MC_CH0_DQMAP1, 0x042a0537);
+	WREG32(mmDDR_MC_CH0_DQMAP2, 0x330b2806);
+	WREG32(mmDDR_MC_CH0_DQMAP3, 0x27013803);
+	WREG32(mmDDR_MC_CH0_DQMAP4, 0x0000022c);
+	WREG32(mmDDR_MC_CH0_DQMAP5, 0x00000001);
+	WREG32(mmDDR_MC_CH0_DBG0, 0x00000001);
+	WREG32(mmDDR_MC_CH0_DBG1, 0x00000000);
+	WREG32(mmDDR_MC_CH0_DBGCMD, 0x00000000);
+	WREG32(mmDDR_MC_CH0_SWCTL, 0x00000001);
+	WREG32(mmDDR_MC_CH0_POISONCFG, 0x00000001);
+	WREG32(mmDDR_MC_CH0_ADVECCINDEX, 0x00000004);
+	WREG32(mmDDR_MC_CH0_ECCPOISONPAT0, 0x00000000);
+	WREG32(mmDDR_MC_CH0_ECCPOISONPAT1, 0x00000000);
+	WREG32(mmDDR_MC_CH0_ECCPOISONPAT2, 0x00000000);
+	WREG32(mmDDR_MC_CH0_CAPARPOISONCTL, 0x00000000);
+	WREG32(mmDDR_MC_CH0_PCCFG, 0x00000011);
+	WREG32(mmDDR_MC_CH0_PCFGR_0, 0x0000518c);
+	WREG32(mmDDR_MC_CH0_PCFGW_0, 0x00001263);
+	WREG32(mmDDR_MC_CH0_PCTRL_0, 0x00000001);
+	WREG32(mmDDR_MC_CH0_PCFGQOS0_0, 0x0011000e);
+	WREG32(mmDDR_MC_CH0_SBRCTL, 0x0016b540);
+	WREG32(mmDDR_MC_CH0_SBRWDATA0, 0x8c1d1786);
+	WREG32(mmDDR_MC_CH0_SBRWDATA1, 0x265f03dd);
+
+	val = RREG32(mmDDR_MC_CH0_RFSHCTL3);
+
+	WREG32(mmDDR_MISC_CH0_CFG_DONE, 0x00000001);
+
+	WREG32(mmDDR_MC_CH0_DBG1, 0x00000000);
+
+	val = RREG32(mmDDR_MC_CH0_PWRCTL);
+
+	WREG32(mmDDR_MC_CH0_PWRCTL, 0x00000002);
+
+	val = RREG32(mmDDR_MC_CH0_PWRCTL);
+
+	WREG32(mmDDR_MC_CH0_PWRCTL, 0x00000000);
+	WREG32(mmDDR_MC_CH0_SWCTL, 0x00000000);
+	WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000041);
+	WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000040);
+	WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000040);
+	WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000060);
+	WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000040);
+	WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000041);
+	WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000041);
+	WREG32(mmDDR_MC_CH0_PCTRL_0, 0x00000001);
+
+	goya->hw_cap_initialized |= HW_CAP_DDR_0;
+}
+
+/**
+ * goya_init_ddr_ch1 - Initialize DDR CH1 controller of the chip
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+static void goya_init_ddr_ch1(struct hl_device *hdev)
+{
+	struct goya_device *goya = hdev->asic_specific;
+	u32 val;
+
+	if (goya->hw_cap_initialized & HW_CAP_DDR_1)
+		return;
+
+	val = RREG32(mmDDR_MISC_CH1_CFG_DONE);
+	if (val & DDR_MISC_CH1_CFG_DONE_CFG_DONE_MASK) {
+		goya->hw_cap_initialized |= HW_CAP_DDR_1;
+		return;
+	}
+
+	WREG32(mmDDR_MC_CH1_DBG1, 0x00000001);
+	WREG32(mmDDR_MC_CH1_PWRCTL, 0x00000001);
+
+	val = RREG32(mmDDR_MC_CH1_STAT);
+
+	WREG32(mmDDR_MC_CH1_MSTR, 0x81040210);
+	WREG32(mmDDR_MC_CH1_MRCTRL0, 0x4000a0f0);
+	WREG32(mmDDR_MC_CH1_MRCTRL1, 0x00022ad0);
+	WREG32(mmDDR_MC_CH1_MRCTRL2, 0x091629e1);
+	WREG32(mmDDR_MC_CH1_PWRCTL, 0x00000008);
+	WREG32(mmDDR_MC_CH1_PWRTMG, 0x00040002);
+	WREG32(mmDDR_MC_CH1_HWLPCTL, 0x00be0002);
+	WREG32(mmDDR_MC_CH1_RFSHCTL0, 0x0091f020);
+	WREG32(mmDDR_MC_CH1_RFSHCTL1, 0x00120018);
+	WREG32((mmDDR_MC_CH1_MSTR + 0x00000058), 0x00160005);
+	WREG32(mmDDR_MC_CH1_RFSHCTL3, 0x00000020);
+	WREG32(mmDDR_MC_CH1_RFSHTMG, 0x003000d0);
+	WREG32(mmDDR_MC_CH1_ECCCFG0, 0x00000010);
+	WREG32(mmDDR_MC_CH1_ECCCFG1, 0x00000002);
+	WREG32(mmDDR_MC_CH1_ECCCTL, 0x00000300);
+	WREG32(mmDDR_MC_CH1_ECCPOISONADDR0, 0x00000078);
+	WREG32(mmDDR_MC_CH1_ECCPOISONADDR1, 0x100062f7);
+	WREG32(mmDDR_MC_CH1_CRCPARCTL0, 0x00008000);
+	WREG32(mmDDR_MC_CH1_CRCPARCTL1, 0x0e088301);
+	WREG32(mmDDR_MC_CH1_CRCPARCTL2, 0x00600527);
+	WREG32(mmDDR_MC_CH1_INIT0, 0x00070002);
+	WREG32(mmDDR_MC_CH1_INIT1, 0x0001000e);
+	WREG32(mmDDR_MC_CH1_INIT3, 0x0c510001);
+	WREG32(mmDDR_MC_CH1_INIT4, 0x00280400);
+	WREG32(mmDDR_MC_CH1_INIT5, 0x00110000);
+	WREG32(mmDDR_MC_CH1_INIT6, 0x02000643);
+	WREG32(mmDDR_MC_CH1_INIT7, 0x00001000);
+	WREG32(mmDDR_MC_CH1_DIMMCTL, 0x00000000);
+	WREG32(mmDDR_MC_CH1_RANKCTL, 0x000009a0);
+	WREG32(mmDDR_MC_CH1_DRAMTMG0, 0x1918361a);
+	WREG32(mmDDR_MC_CH1_DRAMTMG1, 0x00080724);
+	WREG32(mmDDR_MC_CH1_DRAMTMG2, 0x080d0713);
+	WREG32(mmDDR_MC_CH1_DRAMTMG3, 0x00012012);
+	WREG32(mmDDR_MC_CH1_DRAMTMG4, 0x0b04060b);
+	WREG32(mmDDR_MC_CH1_DRAMTMG5, 0x0a0c0804);
+	WREG32(mmDDR_MC_CH1_DRAMTMG8, 0x0606490c);
+	WREG32(mmDDR_MC_CH1_DRAMTMG9, 0x0002050f);
+	WREG32(mmDDR_MC_CH1_DRAMTMG10, 0x000e0d0f);
+	WREG32(mmDDR_MC_CH1_DRAMTMG11, 0x270b011f);
+	WREG32(mmDDR_MC_CH1_DRAMTMG12, 0x00000010);
+	WREG32(mmDDR_MC_CH1_DRAMTMG15, 0x00000000);
+	WREG32(mmDDR_MC_CH1_ZQCTL0, 0x31000040);
+	WREG32(mmDDR_MC_CH1_ZQCTL1, 0x00000070);
+	WREG32(mmDDR_MC_CH1_DFITMG0, 0x05978211);
+	WREG32(mmDDR_MC_CH1_DFITMG1, 0x00080101);
+	WREG32(mmDDR_MC_CH1_DFILPCFG0, 0x07006031);
+	WREG32(mmDDR_MC_CH1_DFILPCFG1, 0x00000010);
+	WREG32(mmDDR_MC_CH1_DFIUPD0, 0x40400018);
+	WREG32(mmDDR_MC_CH1_DFIUPD1, 0x000b0046);
+	WREG32(mmDDR_MC_CH1_DFIUPD2, 0x00000000);
+	WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000041);
+	WREG32(mmDDR_MC_CH1_DFITMG2, 0x00001711);
+	WREG32(mmDDR_MC_CH1_DFITMG3, 0x0000001e);
+	WREG32(mmDDR_MC_CH1_DBICTL, 0x00000001);
+	WREG32(mmDDR_MC_CH1_DFIPHYMSTR, 0x00000000);
+	WREG32(mmDDR_MC_CH1_ADDRMAP0, 0x00001f1f);
+	WREG32(mmDDR_MC_CH1_ADDRMAP1, 0x003f1503);
+	WREG32(mmDDR_MC_CH1_ADDRMAP2, 0x01000400);
+	WREG32(mmDDR_MC_CH1_ADDRMAP3, 0x04000505);
+	WREG32(mmDDR_MC_CH1_ADDRMAP4, 0x00001f1f);
+	WREG32(mmDDR_MC_CH1_ADDRMAP5, 0x06060303);
+	WREG32(mmDDR_MC_CH1_ADDRMAP6, 0x0f050709);
+	WREG32(mmDDR_MC_CH1_ADDRMAP7, 0x00000f0f);
+	WREG32(mmDDR_MC_CH1_ADDRMAP8, 0x00003f01);
+	WREG32(mmDDR_MC_CH1_ADDRMAP9, 0x09000606);
+	WREG32(mmDDR_MC_CH1_ADDRMAP10, 0x02090105);
+	WREG32(mmDDR_MC_CH1_ADDRMAP11, 0x0000000a);
+	WREG32(mmDDR_MC_CH1_ODTCFG, 0x09090a08);
+	WREG32(mmDDR_MC_CH1_ODTMAP, 0x9ae1b5fe);
+	WREG32(mmDDR_MC_CH1_SCHED, 0x664d3700);
+	WREG32(mmDDR_MC_CH1_SCHED1, 0x00000000);
+	WREG32(mmDDR_MC_CH1_PERFHPR1, 0x1700e024);
+	WREG32(mmDDR_MC_CH1_PERFLPR1, 0x1e00836c);
+	WREG32(mmDDR_MC_CH1_PERFWR1, 0x260046c9);
+	WREG32(mmDDR_MC_CH1_DQMAP0, 0x0d2b3503);
+	WREG32(mmDDR_MC_CH1_DQMAP1, 0x042a0537);
+	WREG32(mmDDR_MC_CH1_DQMAP2, 0x330b2806);
+	WREG32(mmDDR_MC_CH1_DQMAP3, 0x27013803);
+	WREG32(mmDDR_MC_CH1_DQMAP4, 0x0000022c);
+	WREG32(mmDDR_MC_CH1_DQMAP5, 0x00000001);
+	WREG32(mmDDR_MC_CH1_DBG0, 0x00000001);
+	WREG32(mmDDR_MC_CH1_DBG1, 0x00000000);
+	WREG32(mmDDR_MC_CH1_DBGCMD, 0x00000000);
+	WREG32(mmDDR_MC_CH1_SWCTL, 0x00000001);
+	WREG32(mmDDR_MC_CH1_POISONCFG, 0x00000001);
+	WREG32(mmDDR_MC_CH1_ADVECCINDEX, 0x00000004);
+	WREG32(mmDDR_MC_CH1_ECCPOISONPAT0, 0x00000000);
+	WREG32(mmDDR_MC_CH1_ECCPOISONPAT1, 0x00000000);
+	WREG32(mmDDR_MC_CH1_ECCPOISONPAT2, 0x00000000);
+	WREG32(mmDDR_MC_CH1_CAPARPOISONCTL, 0x00000000);
+	WREG32(mmDDR_MC_CH1_PCCFG, 0x00000011);
+	WREG32(mmDDR_MC_CH1_PCFGR_0, 0x0000518c);
+	WREG32(mmDDR_MC_CH1_PCFGW_0, 0x00001263);
+	WREG32(mmDDR_MC_CH1_PCTRL_0, 0x00000001);
+	WREG32(mmDDR_MC_CH1_PCFGQOS0_0, 0x0011000e);
+	WREG32(mmDDR_MC_CH1_SBRCTL, 0x0016b540);
+	WREG32(mmDDR_MC_CH1_SBRWDATA0, 0x8c1d1786);
+	WREG32(mmDDR_MC_CH1_SBRWDATA1, 0x265f03dd);
+
+	val = RREG32(mmDDR_MC_CH1_RFSHCTL3);
+
+	WREG32(mmDDR_MISC_CH1_CFG_DONE, 0x00000001);
+
+	WREG32(mmDDR_MC_CH1_DBG1, 0x00000000);
+
+	val = RREG32(mmDDR_MC_CH1_PWRCTL);
+
+	WREG32(mmDDR_MC_CH1_PWRCTL, 0x00000002);
+
+	val = RREG32(mmDDR_MC_CH1_PWRCTL);
+

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1191

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+	WREG32(mmDDR_MC_CH1_PWRCTL, 0x00000000);
+	WREG32(mmDDR_MC_CH1_SWCTL, 0x00000000);
+	WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000041);
+	WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000040);
+	WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000040);
+	WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000060);
+	WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000040);
+	WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000041);
+	WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000041);
+	WREG32(mmDDR_MC_CH1_PCTRL_0, 0x00000001);

The initialization sequence for the second DDR channel looks really similar to that of the first channel.
I would guess their control registers have identical offsets from some base address. If this is the case the DDR initialization can be factored out and get that base address as a parameter.

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1202

Quoted 11 lines
drivers/misc/habanalabs/goya/goya.c +11 -0
+
+	goya->hw_cap_initialized |= HW_CAP_DDR_1;
+}
+
+static void _goya_tpc_mbist_workaround(struct hl_device *hdev, u8 tpc_id)
+{
+	u64 tpc_eml_address;
+	u32 val, tpc_offset, tpc_eml_offset, tpc_slm_offset;
+	int err, slm_index;
+
+	WARN_ON(tpc_id >= TPC_MAX_NUM);

Is it safe to continue if tpc_id >= TPC_MAX_NUM?

Quoted 145 lines
drivers/misc/habanalabs/goya/goya.c +145 -0
+	tpc_offset = tpc_id * 0x40000;
+	tpc_eml_offset = tpc_id * 0x200000;
+	tpc_eml_address = (mmTPC0_EML_CFG_BASE + tpc_eml_offset - CFG_BASE);
+	tpc_slm_offset = tpc_eml_address + 0x100000;
+
+	/*
+	 * Workaround for Bug H2 #2443 :
+	 * "TPC SB is not initialized on chip reset"
+	 */
+
+	val = RREG32(mmTPC0_CFG_FUNC_MBIST_CNTRL + tpc_offset);
+	if (val & TPC0_CFG_FUNC_MBIST_CNTRL_MBIST_ACTIVE_MASK)
+		dev_warn(hdev->dev, "TPC%d MBIST ACTIVE is not cleared\n",
+			tpc_id);
+
+	WREG32(mmTPC0_CFG_FUNC_MBIST_PAT + tpc_offset, val & 0xFFFFF000);
+
+	WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_0 + tpc_offset, 0x37FF);
+	WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_1 + tpc_offset, 0x303F);
+	WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_2 + tpc_offset, 0x71FF);
+	WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_3 + tpc_offset, 0x71FF);
+	WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_4 + tpc_offset, 0x70FF);
+	WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_5 + tpc_offset, 0x70FF);
+	WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_6 + tpc_offset, 0x70FF);
+	WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_7 + tpc_offset, 0x70FF);
+	WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_8 + tpc_offset, 0x70FF);
+	WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_9 + tpc_offset, 0x70FF);
+
+	WREG32_OR(mmTPC0_CFG_FUNC_MBIST_CNTRL + tpc_offset,
+		1 << TPC0_CFG_FUNC_MBIST_CNTRL_MBIST_START_SHIFT);
+
+	err = hl_poll_timeout(
+		hdev,
+		mmTPC0_CFG_FUNC_MBIST_CNTRL + tpc_offset,
+		val,
+		(val & TPC0_CFG_FUNC_MBIST_CNTRL_MBIST_DONE_MASK),
+		1000,
+		HL_DEVICE_TIMEOUT_USEC);
+
+	if (err)
+		dev_err(hdev->dev,
+			"Timeout while waiting for TPC%d MBIST DONE\n", tpc_id);
+
+	WREG32_OR(mmTPC0_EML_CFG_DBG_CNT + tpc_eml_offset,
+		1 << TPC0_EML_CFG_DBG_CNT_CORE_RST_SHIFT);
+
+	msleep(GOYA_RESET_WAIT_MSEC);
+
+	WREG32_AND(mmTPC0_EML_CFG_DBG_CNT + tpc_eml_offset,
+		~(1 << TPC0_EML_CFG_DBG_CNT_CORE_RST_SHIFT));
+
+	msleep(GOYA_RESET_WAIT_MSEC);
+
+	for (slm_index = 0 ; slm_index < 256 ; slm_index++)
+		WREG32(tpc_slm_offset + (slm_index << 2), 0);
+
+	val = RREG32(tpc_slm_offset);
+
+	WREG32(mmTPC0_CFG_BASE + tpc_offset + 0xF40 - CFG_BASE, 0x100);
+}
+
+static void goya_tpc_mbist_workaround(struct hl_device *hdev)
+{
+	struct goya_device *goya = hdev->asic_specific;
+	int i;
+
+	if (hdev->pldm)
+		return;
+
+	if (goya->hw_cap_initialized & HW_CAP_TPC_MBIST)
+		return;
+
+	/* Workaround for H2 #2443 */
+
+	for (i = 0 ; i < TPC_MAX_NUM ; i++)
+		_goya_tpc_mbist_workaround(hdev, i);
+
+	goya->hw_cap_initialized |= HW_CAP_TPC_MBIST;
+}
+
+/**
+ * goya_init_golden_registers - Initialize golden registers
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Initialize the H/W registers of the device
+ *
+ */
+static void goya_init_golden_registers(struct hl_device *hdev)
+{
+	struct goya_device *goya = hdev->asic_specific;
+	u32 polynom[10], tpc_intr_mask;
+
+	if (goya->hw_cap_initialized & HW_CAP_GOLDEN)
+		return;
+
+	polynom[0] = 0x00020080;
+	polynom[1] = 0x00401000;
+	polynom[2] = 0x00200800;
+	polynom[3] = 0x00002000;
+	polynom[4] = 0x00080200;
+	polynom[5] = 0x00040100;
+	polynom[6] = 0x00100400;
+	polynom[7] = 0x00004000;
+	polynom[8] = 0x00010000;
+	polynom[9] = 0x00008000;
+
+	/* Mask all arithmetic interrupts from TPC */
+	tpc_intr_mask = 0x7FFF;
+
+	WREG32(mmDMA_NRTR_SPLIT_COEF_0, polynom[0] >> 7);
+	WREG32(mmDMA_NRTR_SPLIT_COEF_1, polynom[1] >> 7);
+	WREG32(mmDMA_NRTR_SPLIT_COEF_2, polynom[2] >> 7);
+	WREG32(mmDMA_NRTR_SPLIT_COEF_3, polynom[3] >> 7);
+	WREG32(mmDMA_NRTR_SPLIT_COEF_4, polynom[4] >> 7);
+	WREG32(mmDMA_NRTR_SPLIT_COEF_5, polynom[5] >> 7);
+	WREG32(mmDMA_NRTR_SPLIT_COEF_6, polynom[6] >> 7);
+	WREG32(mmDMA_NRTR_SPLIT_COEF_7, polynom[7] >> 7);
+	WREG32(mmDMA_NRTR_SPLIT_COEF_8, polynom[8] >> 7);
+	WREG32(mmDMA_NRTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+	WREG32(mmDMA_NRTR_SCRAMB_EN, 1 << DMA_NRTR_SCRAMB_EN_VAL_SHIFT);
+	WREG32(mmDMA_NRTR_NON_LIN_SCRAMB,
+			1 << DMA_NRTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+	WREG32(mmSRAM_Y5_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y4_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y3_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y2_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y1_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y0_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y5_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y4_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y3_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y2_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y1_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y0_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y5_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y4_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y3_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y2_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y1_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y0_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y5_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y4_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1358

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+	WREG32(mmSRAM_Y3_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y2_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y1_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y0_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y5_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y4_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y3_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y2_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y1_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);
+	WREG32(mmSRAM_Y0_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);

Any chance this can be done in a loop?

Quoted 20 lines
drivers/misc/habanalabs/goya/goya.c +20 -0
+	WREG32(mmSRAM_Y5_X0_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y4_X0_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y3_X0_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y2_X0_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y1_X0_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y0_X0_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y5_X1_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y4_X1_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y3_X1_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y2_X1_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y1_X1_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y0_X1_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y5_X2_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y4_X2_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y3_X2_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y2_X2_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y1_X2_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y0_X2_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y5_X3_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y4_X3_RTR_HBW_DATA_L_ARB, 0x204);

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1388

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+	WREG32(mmSRAM_Y3_X3_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y2_X3_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y1_X3_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y0_X3_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y5_X4_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y4_X4_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y3_X4_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y2_X4_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y1_X4_RTR_HBW_DATA_L_ARB, 0x204);
+	WREG32(mmSRAM_Y0_X4_RTR_HBW_DATA_L_ARB, 0x204);

Ditto.

Quoted 8 lines
drivers/misc/habanalabs/goya/goya.c +8 -0
+	WREG32(mmSRAM_Y5_X0_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y4_X0_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y3_X0_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y2_X0_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y1_X0_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y0_X0_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y5_X1_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y4_X1_RTR_HBW_DATA_E_ARB, 0x206);

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1406

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+	WREG32(mmSRAM_Y3_X1_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y2_X1_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y1_X1_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y0_X1_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y5_X2_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y4_X2_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y3_X2_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y2_X2_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y1_X2_RTR_HBW_DATA_E_ARB, 0x206);
+	WREG32(mmSRAM_Y0_X2_RTR_HBW_DATA_E_ARB, 0x206);

And here and below as well.

Quoted 12 lines
drivers/misc/habanalabs/goya/goya.c +12 -0
+	WREG32(mmSRAM_Y5_X3_RTR_HBW_DATA_E_ARB, 0x207);
+	WREG32(mmSRAM_Y4_X3_RTR_HBW_DATA_E_ARB, 0x207);
+	WREG32(mmSRAM_Y3_X3_RTR_HBW_DATA_E_ARB, 0x207);
+	WREG32(mmSRAM_Y2_X3_RTR_HBW_DATA_E_ARB, 0x207);
+	WREG32(mmSRAM_Y1_X3_RTR_HBW_DATA_E_ARB, 0x207);
+	WREG32(mmSRAM_Y0_X3_RTR_HBW_DATA_E_ARB, 0x207);
+	WREG32(mmSRAM_Y5_X4_RTR_HBW_DATA_E_ARB, 0x207);
+	WREG32(mmSRAM_Y4_X4_RTR_HBW_DATA_E_ARB, 0x207);
+	WREG32(mmSRAM_Y3_X4_RTR_HBW_DATA_E_ARB, 0x207);
+	WREG32(mmSRAM_Y2_X4_RTR_HBW_DATA_E_ARB, 0x207);
+	WREG32(mmSRAM_Y1_X4_RTR_HBW_DATA_E_ARB, 0x207);
+	WREG32(mmSRAM_Y0_X4_RTR_HBW_DATA_E_ARB, 0x207);

[ ... ]

Quoted 55 lines
drivers/misc/habanalabs/goya/goya.c +55 -0
+	WREG32(mmMME1_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+	WREG32(mmMME1_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+	WREG32(mmMME1_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+	WREG32(mmMME1_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+	WREG32(mmMME1_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+	WREG32(mmMME1_RTR_SPLIT_COEF_5, polynom[5] >> 7);
+	WREG32(mmMME1_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+	WREG32(mmMME1_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+	WREG32(mmMME1_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+	WREG32(mmMME1_RTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+	WREG32(mmMME2_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+	WREG32(mmMME2_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+	WREG32(mmMME2_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+	WREG32(mmMME2_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+	WREG32(mmMME2_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+	WREG32(mmMME2_RTR_SPLIT_COEF_5, polynom[5] >> 7);
+	WREG32(mmMME2_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+	WREG32(mmMME2_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+	WREG32(mmMME2_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+	WREG32(mmMME2_RTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+	WREG32(mmMME3_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+	WREG32(mmMME3_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+	WREG32(mmMME3_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+	WREG32(mmMME3_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+	WREG32(mmMME3_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+	WREG32(mmMME3_RTR_SPLIT_COEF_5, polynom[5] >> 7);
+	WREG32(mmMME3_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+	WREG32(mmMME3_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+	WREG32(mmMME3_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+	WREG32(mmMME3_RTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+	WREG32(mmMME4_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+	WREG32(mmMME4_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+	WREG32(mmMME4_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+	WREG32(mmMME4_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+	WREG32(mmMME4_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+	WREG32(mmMME4_RTR_SPLIT_COEF_5, polynom[5] >> 7);
+	WREG32(mmMME4_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+	WREG32(mmMME4_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+	WREG32(mmMME4_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+	WREG32(mmMME4_RTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+	WREG32(mmMME5_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+	WREG32(mmMME5_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+	WREG32(mmMME5_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+	WREG32(mmMME5_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+	WREG32(mmMME5_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+	WREG32(mmMME5_RTR_SPLIT_COEF_5, polynom[5] >> 7);
+	WREG32(mmMME5_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+	WREG32(mmMME5_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+	WREG32(mmMME5_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+	WREG32(mmMME5_RTR_SPLIT_COEF_9, polynom[9] >> 7);
+

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1663

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+	WREG32(mmMME6_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+	WREG32(mmMME6_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+	WREG32(mmMME6_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+	WREG32(mmMME6_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+	WREG32(mmMME6_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+	WREG32(mmMME6_RTR_SPLIT_COEF_5, polynom[5] >> 7);
+	WREG32(mmMME6_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+	WREG32(mmMME6_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+	WREG32(mmMME6_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+	WREG32(mmMME6_RTR_SPLIT_COEF_9, polynom[9] >> 7);

This sequence seem to repeat itself. If the register map permits I'd suggest splitting writes of the polynom[] to registers into a helper function.

Quoted 39 lines
drivers/misc/habanalabs/goya/goya.c +39 -0
+
+	WREG32(mmMME1_RTR_SCRAMB_EN, 1 << MME1_RTR_SCRAMB_EN_VAL_SHIFT);
+	WREG32(mmMME1_RTR_NON_LIN_SCRAMB,
+			1 << MME1_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+	WREG32(mmMME2_RTR_SCRAMB_EN, 1 << MME2_RTR_SCRAMB_EN_VAL_SHIFT);
+	WREG32(mmMME2_RTR_NON_LIN_SCRAMB,
+			1 << MME2_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+	WREG32(mmMME3_RTR_SCRAMB_EN, 1 << MME3_RTR_SCRAMB_EN_VAL_SHIFT);
+	WREG32(mmMME3_RTR_NON_LIN_SCRAMB,
+			1 << MME3_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+	WREG32(mmMME4_RTR_SCRAMB_EN, 1 << MME4_RTR_SCRAMB_EN_VAL_SHIFT);
+	WREG32(mmMME4_RTR_NON_LIN_SCRAMB,
+			1 << MME4_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+	WREG32(mmMME5_RTR_SCRAMB_EN, 1 << MME5_RTR_SCRAMB_EN_VAL_SHIFT);
+	WREG32(mmMME5_RTR_NON_LIN_SCRAMB,
+			1 << MME5_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+	WREG32(mmMME6_RTR_SCRAMB_EN, 1 << MME6_RTR_SCRAMB_EN_VAL_SHIFT);
+	WREG32(mmMME6_RTR_NON_LIN_SCRAMB,
+			1 << MME6_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+	WREG32(mmTPC0_NRTR_SPLIT_COEF_0, polynom[0] >> 7);
+	WREG32(mmTPC0_NRTR_SPLIT_COEF_1, polynom[1] >> 7);
+	WREG32(mmTPC0_NRTR_SPLIT_COEF_2, polynom[2] >> 7);
+	WREG32(mmTPC0_NRTR_SPLIT_COEF_3, polynom[3] >> 7);
+	WREG32(mmTPC0_NRTR_SPLIT_COEF_4, polynom[4] >> 7);
+	WREG32(mmTPC0_NRTR_SPLIT_COEF_5, polynom[5] >> 7);
+	WREG32(mmTPC0_NRTR_SPLIT_COEF_6, polynom[6] >> 7);
+	WREG32(mmTPC0_NRTR_SPLIT_COEF_7, polynom[7] >> 7);
+	WREG32(mmTPC0_NRTR_SPLIT_COEF_8, polynom[8] >> 7);
+	WREG32(mmTPC0_NRTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+	WREG32(mmTPC0_NRTR_SCRAMB_EN, 1 << TPC0_NRTR_SCRAMB_EN_VAL_SHIFT);
+	WREG32(mmTPC0_NRTR_NON_LIN_SCRAMB,
+			1 << TPC0_NRTR_NON_LIN_SCRAMB_EN_SHIFT);

[ ... ]

Quoted 32 lines
drivers/misc/habanalabs/goya/goya.c +32 -0
+	/*
+	 * Workaround for Bug H2 #2441 :
+	 * "ST.NOP set trace event illegal opcode"
+	 */
+	WREG32(mmTPC6_CFG_TPC_INTR_MASK, tpc_intr_mask);
+
+	WREG32(mmTPC7_NRTR_SPLIT_COEF_0, polynom[0] >> 7);
+	WREG32(mmTPC7_NRTR_SPLIT_COEF_1, polynom[1] >> 7);
+	WREG32(mmTPC7_NRTR_SPLIT_COEF_2, polynom[2] >> 7);
+	WREG32(mmTPC7_NRTR_SPLIT_COEF_3, polynom[3] >> 7);
+	WREG32(mmTPC7_NRTR_SPLIT_COEF_4, polynom[4] >> 7);
+	WREG32(mmTPC7_NRTR_SPLIT_COEF_5, polynom[5] >> 7);
+	WREG32(mmTPC7_NRTR_SPLIT_COEF_6, polynom[6] >> 7);
+	WREG32(mmTPC7_NRTR_SPLIT_COEF_7, polynom[7] >> 7);
+	WREG32(mmTPC7_NRTR_SPLIT_COEF_8, polynom[8] >> 7);
+	WREG32(mmTPC7_NRTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+	WREG32(mmTPC7_NRTR_SCRAMB_EN, 1 << TPC7_NRTR_SCRAMB_EN_VAL_SHIFT);
+	WREG32(mmTPC7_NRTR_NON_LIN_SCRAMB,
+			1 << TPC7_NRTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+	/*
+	 * Workaround for Bug H2 #2441 :
+	 * "ST.NOP set trace event illegal opcode"
+	 */
+	WREG32(mmTPC7_CFG_TPC_INTR_MASK, tpc_intr_mask);
+
+	WREG32(mmPCI_NRTR_SPLIT_COEF_0, polynom[0] >> 7);
+	WREG32(mmPCI_NRTR_SPLIT_COEF_1, polynom[1] >> 7);
+	WREG32(mmPCI_NRTR_SPLIT_COEF_2, polynom[2] >> 7);
+	WREG32(mmPCI_NRTR_SPLIT_COEF_3, polynom[3] >> 7);
+	WREG32(mmPCI_NRTR_SPLIT_COEF_4, polynom[4] >> 7);

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1949

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+	WREG32(mmPCI_NRTR_SPLIT_COEF_5, polynom[5] >> 7);
+	WREG32(mmPCI_NRTR_SPLIT_COEF_6, polynom[6] >> 7);
+	WREG32(mmPCI_NRTR_SPLIT_COEF_7, polynom[7] >> 7);
+	WREG32(mmPCI_NRTR_SPLIT_COEF_8, polynom[8] >> 7);
+	WREG32(mmPCI_NRTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+	WREG32(mmPCI_NRTR_SCRAMB_EN, 1 << PCI_NRTR_SCRAMB_EN_VAL_SHIFT);
+	WREG32(mmPCI_NRTR_NON_LIN_SCRAMB,
+			1 << PCI_NRTR_NON_LIN_SCRAMB_EN_SHIFT);
+

I think all these long sequences of register writes could be grouped into something like

struct regs_write_seq {
	unsigned long addr;
	unsigned long val;
};
const struct regs_write_seq golden_regs1 [] {
	...
};
const struct regs_write_seq workaround_bug_2411 [] {
	...
};

and written with a helper function looping over such array.

Quoted 69 lines
drivers/misc/habanalabs/goya/goya.c +69 -0
+	/*
+	 * Workaround for H2 #HW-23 bug
+	 * Set DMA max outstanding read requests to 240 on DMA CH 1. Set it
+	 * to 16 on KMD DMA
+	 * We need to limit only these DMAs because the user can only read
+	 * from Host using DMA CH 1
+	 */
+	WREG32(mmDMA_CH_0_CFG0, 0x0fff0010);
+	WREG32(mmDMA_CH_1_CFG0, 0x0fff00F0);
+
+	goya->hw_cap_initialized |= HW_CAP_GOLDEN;
+}
+
+
+/**
+ * goya_push_uboot_to_device - Push u-boot FW code to device
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Copy u-boot fw code from firmware file to SRAM BAR.
+ * Returns 0 on success
+ *
+ */
+static int goya_push_uboot_to_device(struct hl_device *hdev)
+{
+	char fw_name[200];
+	const u64 *fw_data;
+	void __iomem *dst;
+	size_t fw_size, i;
+	int rc;
+
+	snprintf(fw_name, sizeof(fw_name), "habanalabs/goya/goya-u-boot.bin");
+
+	rc = request_firmware(&hdev->spl_fw, fw_name, hdev->dev);
+
+	if (rc) {
+		dev_err(hdev->dev, "Failed to request u-boot fw image\n");
+		goto out;
+	}
+
+	fw_size = hdev->spl_fw->size;
+	if ((fw_size % 4) != 0) {
+		dev_err(hdev->dev, "illegal u-boot firmware size %lu\n",
+			fw_size);
+		rc = -EINVAL;
+		goto out;
+	}
+
+	dev_dbg(hdev->dev, "u-boot firmware size == %lu\n", fw_size);
+
+	fw_data = (const u64 *) hdev->spl_fw->data;
+	dst = hdev->pcie_bar[SRAM_CFG_BAR_ID] + UBOOT_FW_OFFSET;
+
+	if ((hdev->spl_fw->size % 8) != 0)
+		fw_size -= 8;
+
+	for (i = 0 ; i < fw_size ; i += 8, fw_data++, dst += 8) {
+		if (!(i & (0x80000 - 1)))
+			dev_dbg(hdev->dev,
+				"u-boot copied so far %lu out of %lu",
+				i, fw_size);
+
+		writeq(*fw_data, dst);
+	}
+
+	if ((hdev->spl_fw->size % 8) != 0)
+		writel(*(const u32 *) fw_data, dst);
+
+out:

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 2028

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+	release_firmware(hdev->spl_fw);
+	return rc;
+}
+
+/**
+ * goya_push_linux_to_device - Push LINUX FW code to device
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Copy LINXU fw code from firmware file to DDR BAR.

^ Linux

Quoted 42 lines

+ * Returns 0 on success
+ *
+ */

drivers/misc/habanalabs/goya/goya.c +39 -0
+static int goya_push_linux_to_device(struct hl_device *hdev)
+{
+	char fw_name[200];
+	const u64 *fw_data;
+	void __iomem *dst;
+	size_t fw_size, i;
+	int rc;
+
+	snprintf(fw_name, sizeof(fw_name), "habanalabs/goya/goya-fit.itb");
+
+	rc = request_firmware(&hdev->spl_fw, fw_name, hdev->dev);
+
+	if (rc) {
+		dev_err(hdev->dev, "Failed to request Linux fw image\n");
+		goto out;
+	}
+
+	fw_size = hdev->spl_fw->size;
+	if ((fw_size % 4) != 0) {
+		dev_err(hdev->dev, "illegal Linux firmware size %lu\n",
+			fw_size);
+		rc = -EINVAL;
+		goto out;
+	}
+
+	dev_dbg(hdev->dev, "Linux firmware size == %lu\n", fw_size);
+
+	fw_data = (const u64 *) hdev->spl_fw->data;
+	dst = hdev->pcie_bar[DDR_BAR_ID] + LINUX_FW_OFFSET;
+
+	if ((hdev->spl_fw->size % 8) != 0)
+		fw_size -= 8;
+
+	for (i = 0 ; i < fw_size ; i += 8, fw_data++, dst += 8) {
+		if (!(i & (0x80000 - 1))) {
+			dev_dbg(hdev->dev,
+				"Linux copied so far %lu out of %lu",
+				i, fw_size);
+			usleep_range(20, 100);

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 2080

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+		}
+		writeq(*fw_data, dst);
+	}
+
+	if ((hdev->spl_fw->size % 8) != 0)
+		writel(*(const u32 *) fw_data, dst);
+
+out:
+	release_firmware(hdev->spl_fw);
+	return rc;

The U-Boot and Linux loading to the device seem almost identical. I think it can be declared as

static int goya_push_fw_to_device(struct hl_device *hdev, const char *name,
void __iomem *dst)

and called twice.

Quoted 499 lines
drivers/misc/habanalabs/goya/goya.c +395 -0
+}
+
+static int goya_pldm_init_cpu(struct hl_device *hdev)
+{
+	u32 val, unit_rst_val;
+	int rc;
+
+	/* Must initialize SRAM scrambler before pushing u-boot to SRAM */
+	goya_init_golden_registers(hdev);
+
+	/* Put ARM cores into reset */
+	WREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL, CPU_RESET_ASSERT);
+	val = RREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL);
+
+	/* Reset the CA53 MACRO */
+	unit_rst_val = RREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N);
+	WREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N, CA53_RESET);
+	val = RREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N);
+	WREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N, unit_rst_val);
+	val = RREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N);
+
+	rc = goya_push_uboot_to_device(hdev);
+	if (rc)
+		return rc;
+
+	rc = goya_push_linux_to_device(hdev);
+	if (rc)
+		return rc;
+
+	WREG32(mmPSOC_GLOBAL_CONF_UBOOT_MAGIC, KMD_MSG_FIT_RDY);
+	WREG32(mmPSOC_GLOBAL_CONF_WARM_REBOOT, CPU_BOOT_STATUS_NA);
+
+	WREG32(mmCPU_CA53_CFG_RST_ADDR_LSB_0,
+		lower_32_bits(SRAM_BASE_ADDR + UBOOT_FW_OFFSET));
+	WREG32(mmCPU_CA53_CFG_RST_ADDR_MSB_0,
+		upper_32_bits(SRAM_BASE_ADDR + UBOOT_FW_OFFSET));
+
+	/* Release ARM core 0 from reset */
+	WREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL,
+					CPU_RESET_CORE0_DEASSERT);
+	val = RREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL);
+
+	return 0;
+}
+
+/*
+ * FW component passes an offset from SRAM_BASE_ADDR in SCRATCHPAD_xx.
+ * The version string should be located by that offset.
+ */
+static void goya_read_device_fw_version(struct hl_device *hdev,
+					enum goya_fw_component fwc)
+{
+	const char *name;
+	u32 ver_off;
+	char *dest;
+
+	switch (fwc) {
+	case FW_COMP_UBOOT:
+		ver_off = RREG32(mmPSOC_GLOBAL_CONF_SCRATCHPAD_29);
+		dest = hdev->asic_prop.uboot_ver;
+		name = "U-Boot";
+		break;
+	case FW_COMP_PREBOOT:
+		ver_off = RREG32(mmPSOC_GLOBAL_CONF_SCRATCHPAD_28);
+		dest = hdev->asic_prop.preboot_ver;
+		name = "Preboot";
+		break;
+	default:
+		dev_warn(hdev->dev, "Undefined FW component: %d\n", fwc);
+		return;
+	}
+
+	ver_off &= ~((u32)SRAM_BASE_ADDR);
+
+	if (ver_off < SRAM_SIZE - VERSION_MAX_LEN) {
+		memcpy_fromio(dest, hdev->pcie_bar[SRAM_CFG_BAR_ID] + ver_off,
+							VERSION_MAX_LEN);
+	} else {
+		dev_err(hdev->dev, "%s version offset (0x%x) is above SRAM\n",
+								name, ver_off);
+		strcpy(dest, "unavailable");
+	}
+}
+
+static int goya_init_cpu(struct hl_device *hdev, u32 cpu_timeout)
+{
+	struct goya_device *goya = hdev->asic_specific;
+	u32 status;
+	int rc;
+
+	if (!hdev->cpu_enable)
+		return 0;
+
+	if (goya->hw_cap_initialized & HW_CAP_CPU)
+		return 0;
+
+	/*
+	 * Before pushing u-boot/linux to device, need to set the ddr bar to
+	 * base address of dram
+	 */
+	rc = goya_set_ddr_bar_base(hdev, DRAM_PHYS_BASE);
+	if (rc) {
+		dev_err(hdev->dev,
+			"failed to map DDR bar to DRAM base address\n");
+		return rc;
+	}
+
+	if (hdev->pldm) {
+		rc = goya_pldm_init_cpu(hdev);
+		if (rc)
+			return rc;
+
+		goto out;
+	}
+
+	/* Make sure CPU boot-loader is running */
+	rc = hl_poll_timeout(
+		hdev,
+		mmPSOC_GLOBAL_CONF_WARM_REBOOT,
+		status,
+		(status == CPU_BOOT_STATUS_DRAM_RDY) ||
+		(status == CPU_BOOT_STATUS_SRAM_AVAIL),
+		10000,
+		cpu_timeout);
+
+	if (rc) {
+		dev_err(hdev->dev, "Error in ARM u-boot !!!");
+		switch (status) {
+		case CPU_BOOT_STATUS_NA:
+			dev_err(hdev->dev,
+				"ARM status %d - BTL did NOT run\n", status);
+			break;
+		case CPU_BOOT_STATUS_IN_WFE:
+			dev_err(hdev->dev,
+				"ARM status %d - Inside WFE loop\n", status);
+			break;
+		case CPU_BOOT_STATUS_IN_BTL:
+			dev_err(hdev->dev,
+				"ARM status %d - Stuck in BTL\n", status);
+			break;
+		case CPU_BOOT_STATUS_IN_PREBOOT:
+			dev_err(hdev->dev,
+				"ARM status %d - Stuck in Preboot\n", status);
+			break;
+		case CPU_BOOT_STATUS_IN_SPL:
+			dev_err(hdev->dev,
+				"ARM status %d - Stuck in SPL\n", status);
+			break;
+		case CPU_BOOT_STATUS_IN_UBOOT:
+			dev_err(hdev->dev,
+				"ARM status %d - Stuck in u-boot\n", status);
+			break;
+		case CPU_BOOT_STATUS_DRAM_INIT_FAIL:
+			dev_err(hdev->dev,
+				"ARM status %d - DDR initialization failed\n",
+				status);
+			break;
+		default:
+			dev_err(hdev->dev,
+				"ARM status %d - Invalid status code\n",
+				status);
+			break;
+		}
+		return -EIO;
+	}
+
+	/* Read U-Boot version now in case we will later fail */
+	goya_read_device_fw_version(hdev, FW_COMP_UBOOT);
+	goya_read_device_fw_version(hdev, FW_COMP_PREBOOT);
+
+	if (status == CPU_BOOT_STATUS_SRAM_AVAIL)
+		goto out;
+
+	if (!hdev->fw_loading) {
+		dev_info(hdev->dev, "Skip loading FW\n");
+		goto out;
+	}
+
+	rc = goya_push_linux_to_device(hdev);
+	if (rc)
+		return rc;
+
+	WREG32(mmPSOC_GLOBAL_CONF_UBOOT_MAGIC, KMD_MSG_FIT_RDY);
+
+	rc = hl_poll_timeout(
+		hdev,
+		mmPSOC_GLOBAL_CONF_WARM_REBOOT,
+		status,
+		(status == CPU_BOOT_STATUS_SRAM_AVAIL),
+		10000,
+		cpu_timeout);
+
+	if (rc) {
+		if (status == CPU_BOOT_STATUS_FIT_CORRUPTED)
+			dev_err(hdev->dev,
+				"ARM u-boot reports FIT image is corrupted\n");
+		else
+			dev_err(hdev->dev,
+				"ARM Linux failed to load, %d\n", status);
+		WREG32(mmPSOC_GLOBAL_CONF_UBOOT_MAGIC, KMD_MSG_NA);
+		return -EIO;
+	}
+
+	dev_info(hdev->dev, "Successfully loaded firmware to device\n");
+
+out:
+	goya->hw_cap_initialized |= HW_CAP_CPU;
+
+	return 0;
+}
+
+/**
+ * goya_hw_init - Goya hardware initialization code
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Returns 0 on success
+ *
+ */
+static int goya_hw_init(struct hl_device *hdev)
+{
+	struct asic_fixed_properties *prop = &hdev->asic_prop;
+	u32 val;
+	int rc;
+
+	dev_info(hdev->dev, "Starting initialization of H/W\n");
+
+	/* Perform read from the device to make sure device is up */
+	val = RREG32(mmPCIE_DBI_DEVICE_ID_VENDOR_ID_REG);
+
+	goya_init_pll(hdev);
+
+	if (hdev->pldm) {
+		goya_init_ddr_ch0(hdev);
+		goya_init_ddr_ch1(hdev);
+	}
+
+	rc = goya_init_cpu(hdev, GOYA_CPU_TIMEOUT_USEC);
+	if (rc) {
+		dev_err(hdev->dev, "failed to initialize CPU\n");
+		return rc;
+	}
+
+	goya_tpc_mbist_workaround(hdev);
+
+	goya_init_golden_registers(hdev);
+
+	/*
+	 * After CPU initialization is finished, change DDR bar mapping inside
+	 * iATU to point to the start address of the MMU page tables
+	 */
+	rc = goya_set_ddr_bar_base(hdev, DRAM_PHYS_BASE +
+		(MMU_PAGE_TABLES_ADDR & ~(prop->dram_pci_bar_size - 0x1ull)));
+	if (rc) {
+		dev_err(hdev->dev,
+			"failed to map DDR bar to MMU page tables\n");
+		return rc;
+	}
+
+	goya_init_security(hdev);
+
+	/* CPU initialization is finished, we can now move to 48 bit DMA mask */
+	rc = pci_set_dma_mask(hdev->pdev, DMA_BIT_MASK(48));
+	if (rc) {
+		dev_warn(hdev->dev, "Unable to set pci dma mask to 48 bits\n");
+		rc = pci_set_dma_mask(hdev->pdev, DMA_BIT_MASK(32));
+		if (rc) {
+			dev_err(hdev->dev,
+				"Unable to set pci dma mask to 32 bits\n");
+			return rc;
+		}
+	}
+
+	rc = pci_set_consistent_dma_mask(hdev->pdev, DMA_BIT_MASK(48));
+	if (rc) {
+		dev_warn(hdev->dev,
+			"Unable to set pci consistent dma mask to 48 bits\n");
+		rc = pci_set_consistent_dma_mask(hdev->pdev, DMA_BIT_MASK(32));
+		if (rc) {
+			dev_err(hdev->dev,
+				"Unable to set pci consistent dma mask to 32 bits\n");
+			return rc;
+		}
+	}
+
+	/* Perform read from the device to flush all MSI-X configuration */
+	val = RREG32(mmPCIE_DBI_DEVICE_ID_VENDOR_ID_REG);
+
+	return 0;
+}
+
+/**
+ * goya_hw_fini - Goya hardware tear-down code
+ *
+ * @hdev: pointer to hl_device structure
+ * @hard_reset: should we do hard reset to all engines or just reset the
+ *              compute/dma engines
+ *
+ * The function does the following:
+ * - Send interrupt to CPU to go into "quiet" mode
+ * - Stall MME, TPC
+ * - Stop External, Internal QMANs
+ * - Disable MSI-X
+ * - Issue reset command
+ * - Wait until reset is done
+ * - Start device BTL
+ *
+ */
+static void goya_hw_fini(struct hl_device *hdev, bool hard_reset)
+{
+	struct goya_device *goya = hdev->asic_specific;
+	u32 reset_timeout_ms, status;
+
+	if (hdev->pldm)
+		reset_timeout_ms = GOYA_PLDM_RESET_TIMEOUT_MSEC;
+	else
+		reset_timeout_ms = GOYA_RESET_TIMEOUT_MSEC;
+
+	if (hard_reset) {
+		goya_set_ddr_bar_base(hdev, DRAM_PHYS_BASE);
+		goya_disable_clk_rlx(hdev);
+		goya_set_pll_refclk(hdev);
+
+		WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG, RESET_ALL);
+		dev_info(hdev->dev,
+			"Issued HARD reset command, going to wait %dms\n",
+			reset_timeout_ms);
+	} else {
+		WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG, DMA_MME_TPC_RESET);
+		dev_info(hdev->dev,
+			"Issued SOFT reset command, going to wait %dms\n",
+			reset_timeout_ms);
+	}
+
+	/*
+	 * After hard reset, we can't poll the BTM_FSM register because the PSOC
+	 * itself is in reset. In either reset we need to wait until the reset
+	 * is deasserted
+	 */
+	msleep(reset_timeout_ms);
+
+	status = RREG32(mmPSOC_GLOBAL_CONF_BTM_FSM);
+	if (status & PSOC_GLOBAL_CONF_BTM_FSM_STATE_MASK)
+		dev_err(hdev->dev,
+			"Timeout while waiting for device to reset 0x%x\n",
+			status);
+
+	if (!hard_reset) {
+		goya->hw_cap_initialized &= ~(HW_CAP_DMA | HW_CAP_MME |
+						HW_CAP_GOLDEN | HW_CAP_TPC);
+		WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR,
+				GOYA_ASYNC_EVENT_ID_SOFT_RESET);
+		return;
+	}
+
+	/* Chicken bit to re-initiate boot sequencer flow */
+	WREG32(mmPSOC_GLOBAL_CONF_BOOT_SEQ_RE_START,
+		1 << PSOC_GLOBAL_CONF_BOOT_SEQ_RE_START_IND_SHIFT);
+	/* Move boot manager FSM to pre boot sequencer init state */
+	WREG32(mmPSOC_GLOBAL_CONF_SW_BTM_FSM,
+			0xA << PSOC_GLOBAL_CONF_SW_BTM_FSM_CTRL_SHIFT);
+
+	goya->hw_cap_initialized &= ~(HW_CAP_CPU | HW_CAP_CPU_Q |
+					HW_CAP_DDR_0 | HW_CAP_DDR_1 |
+					HW_CAP_DMA | HW_CAP_MME |
+					HW_CAP_MMU | HW_CAP_TPC_MBIST |
+					HW_CAP_GOLDEN | HW_CAP_TPC);
+
+	if (!hdev->pldm) {
+		int rc;
+		/* In case we are running inside VM and the VM is
+		 * shutting down, we need to make sure CPU boot-loader
+		 * is running before we can continue the VM shutdown.
+		 * That is because the VM will send an FLR signal that
+		 * we must answer
+		 */
+		dev_info(hdev->dev,
+			"Going to wait up to %ds for CPU boot loader\n",
+			GOYA_CPU_TIMEOUT_USEC / 1000 / 1000);
+
+		rc = hl_poll_timeout(
+			hdev,
+			mmPSOC_GLOBAL_CONF_WARM_REBOOT,
+			status,
+			(status == CPU_BOOT_STATUS_DRAM_RDY),
+			10000,
+			GOYA_CPU_TIMEOUT_USEC);
+		if (rc)
+			dev_err(hdev->dev,
+				"failed to wait for CPU boot loader\n");
+	}
+}
+
 int goya_suspend(struct hl_device *hdev)
 {
 	return 0;
@@ -641,6 +2519,8 @@ static const struct hl_asic_funcs goya_funcs = {
 	.early_fini = goya_early_fini,
 	.sw_init = goya_sw_init,
 	.sw_fini = goya_sw_fini,
+	.hw_init = goya_hw_init,
+	.hw_fini = goya_hw_fini,
 	.suspend = goya_suspend,
 	.resume = goya_resume,
 	.mmap = goya_mmap,
drivers/misc/habanalabs/goya/goyaP.h +3 -0
index 0e12c56472bd..45a6d2ca2752 100644
@@ -9,6 +9,7 @@
 #define GOYAP_H_
 
 #include "habanalabs.h"
+#include "include/goya/goya_boot_if.h"
 #include "include/goya/goya.h"
 
 #define NUMBER_OF_CMPLT_QUEUES		5
@@ -122,4 +123,6 @@ struct goya_device {
 	u32		hw_cap_initialized;
 };
 
+void goya_init_security(struct hl_device *hdev);
+
 #endif /* GOYAP_H_ */
drivers/misc/habanalabs/goya/goya_security.c new file +69 -0
new file mode 100644
index 000000000000..99ad9aacf49e
@@ -0,0 +1,2999 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "goyaP.h"
+
+/**
+ * goya_set_block_as_protected - set the given block as protected
+ *
+ * @hdev: pointer to hl_device structure
+ * @block: block base address
+ *
+ */
+static void goya_pb_set_block(struct hl_device *hdev, u64 base)
+{
+	u32 pb_addr = base - CFG_BASE + PROT_BITS_OFFS;
+
+	while (pb_addr & 0xFFF) {
+		WREG32(pb_addr, 0);
+		pb_addr += 4;
+	}
+}
+
+static void goya_init_mme_protection_bits(struct hl_device *hdev)
+{
+	u32 pb_addr, mask;
+	u8 word_offset;
+
+	/* TODO: change to real reg name when Soc Online is updated */
+	u64 mmMME_SBB_POWER_ECO1 = 0xDFF60,
+		mmMME_SBB_POWER_ECO2 = 0xDFF64;
+
+	goya_pb_set_block(hdev, mmACC_MS_ECC_MEM_0_BASE);
+	goya_pb_set_block(hdev, mmACC_MS_ECC_MEM_1_BASE);
+	goya_pb_set_block(hdev, mmACC_MS_ECC_MEM_2_BASE);
+	goya_pb_set_block(hdev, mmACC_MS_ECC_MEM_3_BASE);
+
+	goya_pb_set_block(hdev, mmSBA_ECC_MEM_BASE);
+	goya_pb_set_block(hdev, mmSBB_ECC_MEM_BASE);
+
+	goya_pb_set_block(hdev, mmMME1_RTR_BASE);
+	goya_pb_set_block(hdev, mmMME1_RD_REGULATOR_BASE);
+	goya_pb_set_block(hdev, mmMME1_WR_REGULATOR_BASE);
+	goya_pb_set_block(hdev, mmMME2_RTR_BASE);
+	goya_pb_set_block(hdev, mmMME2_RD_REGULATOR_BASE);
+	goya_pb_set_block(hdev, mmMME2_WR_REGULATOR_BASE);
+	goya_pb_set_block(hdev, mmMME3_RTR_BASE);
+	goya_pb_set_block(hdev, mmMME3_RD_REGULATOR_BASE);
+	goya_pb_set_block(hdev, mmMME3_WR_REGULATOR_BASE);
+
+	goya_pb_set_block(hdev, mmMME4_RTR_BASE);
+	goya_pb_set_block(hdev, mmMME4_RD_REGULATOR_BASE);
+	goya_pb_set_block(hdev, mmMME4_WR_REGULATOR_BASE);
+
+	goya_pb_set_block(hdev, mmMME5_RTR_BASE);
+	goya_pb_set_block(hdev, mmMME5_RD_REGULATOR_BASE);
+	goya_pb_set_block(hdev, mmMME5_WR_REGULATOR_BASE);
+
+	goya_pb_set_block(hdev, mmMME6_RTR_BASE);
+	goya_pb_set_block(hdev, mmMME6_RD_REGULATOR_BASE);
+	goya_pb_set_block(hdev, mmMME6_WR_REGULATOR_BASE);
+
+	pb_addr = (mmMME_DUMMY & ~0xFFF) + PROT_BITS_OFFS;
+	word_offset = ((mmMME_DUMMY & PROT_BITS_OFFS) >> 7) << 2;
+	mask = 1 << ((mmMME_DUMMY & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_RESET & 0x7F) >> 2);

drivers/misc/habanalabs/goya/goya_security.c line 79

Quoted 10 lines
drivers/misc/habanalabs/goya/goya_security.c new file +10 -0
+	mask |= 1 << ((mmMME_STALL & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SM_BASE_ADDRESS_LOW & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SM_BASE_ADDRESS_HIGH & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_DBGMEM_ADD & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_DBGMEM_DATA_WR & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_DBGMEM_DATA_RD & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_DBGMEM_CTRL & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_DBGMEM_RC & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_LOG_SHADOW & 0x7F) >> 2);
+

The mask here and below seems to be a constant.
A #define could suffice, no?

Quoted 25 lines
drivers/misc/habanalabs/goya/goya_security.c new file +25 -0
+	WREG32(pb_addr + word_offset, ~mask);
+
+	pb_addr = (mmMME_STORE_MAX_CREDIT & ~0xFFF) + PROT_BITS_OFFS;
+	word_offset = ((mmMME_STORE_MAX_CREDIT & PROT_BITS_OFFS) >> 7) << 2;
+	mask = 1 << ((mmMME_STORE_MAX_CREDIT & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_AGU & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SBA & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SBB & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SBC & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_WBC & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SBA_CONTROL_DATA & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SBB_CONTROL_DATA & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SBC_CONTROL_DATA & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_WBC_CONTROL_DATA & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_TE & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_TE2DEC & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_REI_STATUS & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_REI_MASK & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SEI_STATUS & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SEI_MASK & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SPI_STATUS & 0x7F) >> 2);
+	mask |= 1 << ((mmMME_SPI_MASK & 0x7F) >> 2);
+
+	WREG32(pb_addr + word_offset, ~mask);
+

[ ... ]

Quoted 35 lines
drivers/misc/habanalabs/goya/goya_security.c new file +35 -0
+
+/**
+ * goya_init_protection_bits - Initialize protection bits for specific registers
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * All protection bits are 1 by default, means not protected. Need to set to 0
+ * each bit that belongs to a protected register.
+ *
+ */
+static void goya_init_protection_bits(struct hl_device *hdev)
+{
+	/*
+	 * In each 4K block of registers, the last 128 bytes are protection
+	 * bits - total of 1024 bits, one for each register. Each bit is related
+	 * to a specific register, by the order of the registers.
+	 * So in order to calculate the bit that is related to a given register,
+	 * we need to calculate its word offset and then the exact bit inside
+	 * the word (which is 4 bytes).
+	 *
+	 * Register address:
+	 *
+	 * 31                 12 11           7   6             2  1      0
+	 * -----------------------------------------------------------------
+	 * |      Don't         |    word       |  bit location  |    0    |
+	 * |      care          |   offset      |  inside word   |         |
+	 * -----------------------------------------------------------------
+	 *
+	 * Bits 7-11 represents the word offset inside the 128 bytes.
+	 * Bits 2-6 represents the bit location inside the word.
+	 */
+
+	goya_pb_set_block(hdev, mmPCI_NRTR_BASE);
+	goya_pb_set_block(hdev, mmPCI_RD_REGULATOR_BASE);
+	goya_pb_set_block(hdev, mmPCI_WR_REGULATOR_BASE);

[ ... ]

Quoted 15 lines
drivers/misc/habanalabs/goya/goya_security.c new file +15 -0
+	goya_init_mme_protection_bits(hdev);
+
+	goya_init_dma_protection_bits(hdev);
+
+	goya_init_tpc_protection_bits(hdev);
+}
+
+/**
+ * goya_init_security - Initialize security model
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Initialize the security model of the device
+ * That includes range registers and protection bit per register
+ *

drivers/misc/habanalabs/goya/goya_security.c line 2265

Quoted 10 lines
drivers/misc/habanalabs/goya/goya_security.c new file +10 -0
+ */
+void goya_init_security(struct hl_device *hdev)
+{
+	struct goya_device *goya = hdev->asic_specific;
+
+	u32 dram_addr_lo = lower_32_bits(DRAM_PHYS_BASE);
+	u32 dram_addr_hi = upper_32_bits(DRAM_PHYS_BASE);
+
+	u32 lbw_rng0_base = 0xFC440000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng0_mask = 0xFFFF0000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;

These are anyway magic numbers, why not include the mask in them directly? BTW, I couldn't fine DMA_MACRO_LBW_RANGE_BASE_R_MASK anywhere in the driver.

Quoted 72 lines
drivers/misc/habanalabs/goya/goya_security.c new file +72 -0
+
+	u32 lbw_rng1_base = 0xFC480000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng1_mask = 0xFFF80000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng2_base = 0xFC600000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng2_mask = 0xFFE00000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng3_base = 0xFC800000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng3_mask = 0xFFF00000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng4_base = 0xFCC02000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng4_mask = 0xFFFFF000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng5_base = 0xFCC40000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng5_mask = 0xFFFF8000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng6_base = 0xFCC48000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng6_mask = 0xFFFFF000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng7_base = 0xFCC4A000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng7_mask = 0xFFFFE000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng8_base = 0xFCC4C000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng8_mask = 0xFFFFC000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng9_base = 0xFCC50000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng9_mask = 0xFFFF0000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng10_base = 0xFCC60000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng10_mask = 0xFFFE0000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng11_base = 0xFCE00000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng11_mask = 0xFFFFC000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng12_base = 0xFE484000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng12_mask = 0xFFFFF000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	u32 lbw_rng13_base = 0xFEC43000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+	u32 lbw_rng13_mask = 0xFFFFF000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+	WREG32(mmDMA_MACRO_LBW_RANGE_HIT_BLOCK, 0xFFFF);
+	WREG32(mmDMA_MACRO_HBW_RANGE_HIT_BLOCK, 0xFF);
+
+	if (!(goya->hw_cap_initialized & HW_CAP_MMU)) {
+		WREG32(mmDMA_MACRO_HBW_RANGE_HIT_BLOCK, 0xFE);
+
+		/* Protect HOST */
+		WREG32(mmDMA_MACRO_HBW_RANGE_BASE_31_0_0, 0);
+		WREG32(mmDMA_MACRO_HBW_RANGE_BASE_49_32_0, 0);
+		WREG32(mmDMA_MACRO_HBW_RANGE_MASK_31_0_0, 0);
+		WREG32(mmDMA_MACRO_HBW_RANGE_MASK_49_32_0, 0xFFF80);
+	}
+
+	/*
+	 * Protect DDR @
+	 * DRAM_VIRT_BASE : DRAM_VIRT_BASE + DRAM_VIRT_END
+	 * The mask protects the first 512MB
+	 */
+	WREG32(mmDMA_MACRO_HBW_RANGE_BASE_31_0_1, dram_addr_lo);
+	WREG32(mmDMA_MACRO_HBW_RANGE_BASE_49_32_1, dram_addr_hi);
+	WREG32(mmDMA_MACRO_HBW_RANGE_MASK_31_0_1, 0xE0000000);
+	WREG32(mmDMA_MACRO_HBW_RANGE_MASK_49_32_1, 0x3FFFF);
+
+	/* Protect registers */
+
+	WREG32(mmDMA_MACRO_LBW_RANGE_BASE_0, lbw_rng0_base);
+	WREG32(mmDMA_MACRO_LBW_RANGE_MASK_0, lbw_rng0_mask);
+	WREG32(mmDMA_MACRO_LBW_RANGE_BASE_1, lbw_rng1_base);
+	WREG32(mmDMA_MACRO_LBW_RANGE_MASK_1, lbw_rng1_mask);
+	WREG32(mmDMA_MACRO_LBW_RANGE_BASE_2, lbw_rng2_base);
+	WREG32(mmDMA_MACRO_LBW_RANGE_MASK_2, lbw_rng2_mask);
+	WREG32(mmDMA_MACRO_LBW_RANGE_BASE_3, lbw_rng3_base);

[ ... ]

Quoted 86 lines
drivers/misc/habanalabs/goya/goya_security.c new file +2 -0
+	goya_init_protection_bits(hdev);
+}
drivers/misc/habanalabs/habanalabs.h +16 -0
index 6ad476df65b0..adda281ec2af 100644
@@ -23,6 +23,8 @@
 
 #define HL_MMAP_CB_MASK			(0x8000000000000000ull >> PAGE_SHIFT)
 
+#define HL_DEVICE_TIMEOUT_USEC		1000000 /* 1 s */
+
 #define HL_MAX_QUEUES			128
 
 struct hl_device;
@@ -32,6 +34,8 @@ struct hl_fpriv;
 
 /**
  * struct asic_fixed_properties - ASIC specific immutable properties.
+ * @uboot_ver: F/W U-boot version.
+ * @preboot_ver: F/W Preboot version.
  * @sram_base_address: SRAM physical start address.
  * @sram_end_address: SRAM physical end address.
  * @sram_user_base_address - SRAM physical start address for user access.
@@ -60,6 +64,8 @@ struct hl_fpriv;
  * @tpc_enabled_mask: which TPCs are enabled.
  */
 struct asic_fixed_properties {
+	char			uboot_ver[VERSION_MAX_LEN];
+	char			preboot_ver[VERSION_MAX_LEN];
 	u64			sram_base_address;
 	u64			sram_end_address;
 	u64			sram_user_base_address;
@@ -168,6 +174,8 @@ enum hl_asic_type {
  * @early_fini: tears down what was done in early_init.
  * @sw_init: sets up driver state, does not configure H/W.
  * @sw_fini: tears down driver state, does not configure H/W.
+ * @hw_init: sets up the H/W state.
+ * @hw_fini: tears down the H/W state.
  * @suspend: handles IP specific H/W or SW changes for suspend.
  * @resume: handles IP specific H/W or SW changes for resume.
  * @mmap: mmap function, does nothing.
@@ -180,6 +188,8 @@ struct hl_asic_funcs {
 	int (*early_fini)(struct hl_device *hdev);
 	int (*sw_init)(struct hl_device *hdev);
 	int (*sw_fini)(struct hl_device *hdev);
+	int (*hw_init)(struct hl_device *hdev);
+	void (*hw_fini)(struct hl_device *hdev, bool hard_reset);
 	int (*suspend)(struct hl_device *hdev);
 	int (*resume)(struct hl_device *hdev);
 	int (*mmap)(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
@@ -312,6 +322,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
  * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address.
  * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address.
  * @cpu_accessible_dma_pool: KMD <-> ArmCP shared memory pool.
+ * @spl_fw: image to load to ArmCP.
  * @asid_bitmap: holds used/available ASIDs.
  * @asid_mutex: protects asid_bitmap.
  * @device_open: lock for sanity checks upon FD open.
@@ -340,6 +351,7 @@ struct hl_device {
 	void				*cpu_accessible_dma_mem;
 	dma_addr_t			cpu_accessible_dma_address;
 	struct gen_pool			*cpu_accessible_dma_pool;
+	const struct firmware		*spl_fw;
 	unsigned long			*asid_bitmap;
 	struct mutex			asid_mutex;
 	/* TODO: change to rw_sem for multiple contexts (same as other IOCTL) */
@@ -359,7 +371,11 @@ struct hl_device {
 	u8				disabled;
 
 	/* Parameters for bring-up */
+	u8				cpu_enable;
 	u8				reset_pcilink;
+	u8				config_pll;
+	u8				fw_loading;
+	u8				pldm;
 };
 
 /*
drivers/misc/habanalabs/habanalabs_drv.c +0 -0
index 5c312dd3aa50..bd80683118d3 100644
@@ -181,7 +181,15 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
 	hdev->major = hl_major;
 

drivers/misc/habanalabs/habanalabs_drv.c int create_hdev(struct hl_device **dev, struct pci_dev *pdev, · line 192

Quoted 10 lines
drivers/misc/habanalabs/habanalabs_drv.c +8 -0
 	/* Parameters for bring-up - set them to defaults */
+	hdev->cpu_enable = 1;
 	hdev->reset_pcilink = 0;
+	hdev->config_pll = 0;
+	hdev->fw_loading = 1;
+	hdev->pldm = 0;
+
+	/* If CPU is disabled, no point in loading FW */
+	if (!hdev->cpu_enable)
+		hdev->fw_loading = 0;

The CPU was enabled just a couple of lines above, wasn't it? I've noticed there are a lot of checks for hdev->cpu_enabled and hdev->pldm but I didn't see them ever change.

drivers/misc/habanalabs/include/goya/goya_async_events.h

Quoted 20 lines
drivers/misc/habanalabs/habanalabs_drv.c +0 -0
 	hdev->disabled = true;
 	hdev->pdev = pdev; /* can be NULL in case of simulator device */
drivers/misc/habanalabs/include/goya/goya.h +1 -0
index 192a1450cbb1..2d0efb7b44bb 100644
@@ -11,6 +11,7 @@
 #define GOYA_H
 
 #include "asic_reg/goya_regs.h"
+#include "goya_async_events.h"
 
 #include <linux/types.h>
 
drivers/misc/habanalabs/include/goya/goya_async_events.h new file +0 -0
new file mode 100644
index 000000000000..497937a17ee9

This, apparently, should have been a part of patch 8 (habanalabs: add event queue and interrupts)

Quoted 228 lines
drivers/misc/habanalabs/include/goya/goya_async_events.h new file +186 -0
@@ -0,0 +1,186 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ */
+
+#ifndef __GOYA_ASYNC_EVENTS_H_
+#define __GOYA_ASYNC_EVENTS_H_
+
+enum goya_async_event_id {
+	GOYA_ASYNC_EVENT_ID_PCIE_IF = 33,
+	GOYA_ASYNC_EVENT_ID_TPC0_ECC = 36,
+	GOYA_ASYNC_EVENT_ID_TPC1_ECC = 39,
+	GOYA_ASYNC_EVENT_ID_TPC2_ECC = 42,
+	GOYA_ASYNC_EVENT_ID_TPC3_ECC = 45,
+	GOYA_ASYNC_EVENT_ID_TPC4_ECC = 48,
+	GOYA_ASYNC_EVENT_ID_TPC5_ECC = 51,
+	GOYA_ASYNC_EVENT_ID_TPC6_ECC = 54,
+	GOYA_ASYNC_EVENT_ID_TPC7_ECC = 57,
+	GOYA_ASYNC_EVENT_ID_MME_ECC = 60,
+	GOYA_ASYNC_EVENT_ID_MME_ECC_EXT = 61,
+	GOYA_ASYNC_EVENT_ID_MMU_ECC = 63,
+	GOYA_ASYNC_EVENT_ID_DMA_MACRO = 64,
+	GOYA_ASYNC_EVENT_ID_DMA_ECC = 66,
+	GOYA_ASYNC_EVENT_ID_CPU_IF_ECC = 75,
+	GOYA_ASYNC_EVENT_ID_PSOC_MEM = 78,
+	GOYA_ASYNC_EVENT_ID_PSOC_CORESIGHT = 79,
+	GOYA_ASYNC_EVENT_ID_SRAM0 = 81,
+	GOYA_ASYNC_EVENT_ID_SRAM1 = 82,
+	GOYA_ASYNC_EVENT_ID_SRAM2 = 83,
+	GOYA_ASYNC_EVENT_ID_SRAM3 = 84,
+	GOYA_ASYNC_EVENT_ID_SRAM4 = 85,
+	GOYA_ASYNC_EVENT_ID_SRAM5 = 86,
+	GOYA_ASYNC_EVENT_ID_SRAM6 = 87,
+	GOYA_ASYNC_EVENT_ID_SRAM7 = 88,
+	GOYA_ASYNC_EVENT_ID_SRAM8 = 89,
+	GOYA_ASYNC_EVENT_ID_SRAM9 = 90,
+	GOYA_ASYNC_EVENT_ID_SRAM10 = 91,
+	GOYA_ASYNC_EVENT_ID_SRAM11 = 92,
+	GOYA_ASYNC_EVENT_ID_SRAM12 = 93,
+	GOYA_ASYNC_EVENT_ID_SRAM13 = 94,
+	GOYA_ASYNC_EVENT_ID_SRAM14 = 95,
+	GOYA_ASYNC_EVENT_ID_SRAM15 = 96,
+	GOYA_ASYNC_EVENT_ID_SRAM16 = 97,
+	GOYA_ASYNC_EVENT_ID_SRAM17 = 98,
+	GOYA_ASYNC_EVENT_ID_SRAM18 = 99,
+	GOYA_ASYNC_EVENT_ID_SRAM19 = 100,
+	GOYA_ASYNC_EVENT_ID_SRAM20 = 101,
+	GOYA_ASYNC_EVENT_ID_SRAM21 = 102,
+	GOYA_ASYNC_EVENT_ID_SRAM22 = 103,
+	GOYA_ASYNC_EVENT_ID_SRAM23 = 104,
+	GOYA_ASYNC_EVENT_ID_SRAM24 = 105,
+	GOYA_ASYNC_EVENT_ID_SRAM25 = 106,
+	GOYA_ASYNC_EVENT_ID_SRAM26 = 107,
+	GOYA_ASYNC_EVENT_ID_SRAM27 = 108,
+	GOYA_ASYNC_EVENT_ID_SRAM28 = 109,
+	GOYA_ASYNC_EVENT_ID_SRAM29 = 110,
+	GOYA_ASYNC_EVENT_ID_GIC500 = 112,
+	GOYA_ASYNC_EVENT_ID_PCIE_DEC = 115,
+	GOYA_ASYNC_EVENT_ID_TPC0_DEC = 117,
+	GOYA_ASYNC_EVENT_ID_TPC1_DEC = 120,
+	GOYA_ASYNC_EVENT_ID_TPC2_DEC = 123,
+	GOYA_ASYNC_EVENT_ID_TPC3_DEC = 126,
+	GOYA_ASYNC_EVENT_ID_TPC4_DEC = 129,
+	GOYA_ASYNC_EVENT_ID_TPC5_DEC = 132,
+	GOYA_ASYNC_EVENT_ID_TPC6_DEC = 135,
+	GOYA_ASYNC_EVENT_ID_TPC7_DEC = 138,
+	GOYA_ASYNC_EVENT_ID_AXI_ECC = 139,
+	GOYA_ASYNC_EVENT_ID_L2_RAM_ECC = 140,
+	GOYA_ASYNC_EVENT_ID_MME_WACS = 141,
+	GOYA_ASYNC_EVENT_ID_MME_WACSD = 142,
+	GOYA_ASYNC_EVENT_ID_PLL0 = 143,
+	GOYA_ASYNC_EVENT_ID_PLL1 = 144,
+	GOYA_ASYNC_EVENT_ID_PLL3 = 146,
+	GOYA_ASYNC_EVENT_ID_PLL4 = 147,
+	GOYA_ASYNC_EVENT_ID_PLL5 = 148,
+	GOYA_ASYNC_EVENT_ID_PLL6 = 149,
+	GOYA_ASYNC_EVENT_ID_CPU_AXI_SPLITTER = 155,
+	GOYA_ASYNC_EVENT_ID_PSOC_AXI_DEC = 159,
+	GOYA_ASYNC_EVENT_ID_PSOC = 160,
+	GOYA_ASYNC_EVENT_ID_PCIE_FLR = 171,
+	GOYA_ASYNC_EVENT_ID_PCIE_HOT_RESET = 172,
+	GOYA_ASYNC_EVENT_ID_PCIE_QID0_ENG0 = 174,
+	GOYA_ASYNC_EVENT_ID_PCIE_QID0_ENG1 = 175,
+	GOYA_ASYNC_EVENT_ID_PCIE_QID0_ENG2 = 176,
+	GOYA_ASYNC_EVENT_ID_PCIE_QID0_ENG3 = 177,
+	GOYA_ASYNC_EVENT_ID_PCIE_QID1_ENG0 = 178,
+	GOYA_ASYNC_EVENT_ID_PCIE_QID1_ENG1 = 179,
+	GOYA_ASYNC_EVENT_ID_PCIE_QID1_ENG2 = 180,
+	GOYA_ASYNC_EVENT_ID_PCIE_QID1_ENG3 = 181,
+	GOYA_ASYNC_EVENT_ID_PCIE_APB = 182,
+	GOYA_ASYNC_EVENT_ID_PCIE_QDB = 183,
+	GOYA_ASYNC_EVENT_ID_PCIE_BM_D_P_WR = 184,
+	GOYA_ASYNC_EVENT_ID_PCIE_BM_D_RD = 185,
+	GOYA_ASYNC_EVENT_ID_PCIE_BM_U_P_WR = 186,
+	GOYA_ASYNC_EVENT_ID_PCIE_BM_U_RD = 187,
+	GOYA_ASYNC_EVENT_ID_TPC0_BMON_SPMU = 190,
+	GOYA_ASYNC_EVENT_ID_TPC0_KRN_ERR = 191,
+	GOYA_ASYNC_EVENT_ID_TPC1_BMON_SPMU = 200,
+	GOYA_ASYNC_EVENT_ID_TPC1_KRN_ERR = 201,
+	GOYA_ASYNC_EVENT_ID_TPC2_BMON_SPMU = 210,
+	GOYA_ASYNC_EVENT_ID_TPC2_KRN_ERR = 211,
+	GOYA_ASYNC_EVENT_ID_TPC3_BMON_SPMU = 220,
+	GOYA_ASYNC_EVENT_ID_TPC3_KRN_ERR = 221,
+	GOYA_ASYNC_EVENT_ID_TPC4_BMON_SPMU = 230,
+	GOYA_ASYNC_EVENT_ID_TPC4_KRN_ERR = 231,
+	GOYA_ASYNC_EVENT_ID_TPC5_BMON_SPMU = 240,
+	GOYA_ASYNC_EVENT_ID_TPC5_KRN_ERR = 241,
+	GOYA_ASYNC_EVENT_ID_TPC6_BMON_SPMU = 250,
+	GOYA_ASYNC_EVENT_ID_TPC6_KRN_ERR = 251,
+	GOYA_ASYNC_EVENT_ID_TPC7_BMON_SPMU = 260,
+	GOYA_ASYNC_EVENT_ID_TPC7_KRN_ERR = 261,
+	GOYA_ASYNC_EVENT_ID_MMU_SBA_SPMU0 = 270,
+	GOYA_ASYNC_EVENT_ID_MMU_SBA_SPMU1 = 271,
+	GOYA_ASYNC_EVENT_ID_MME_WACS_UP = 272,
+	GOYA_ASYNC_EVENT_ID_MME_WACS_DOWN = 273,
+	GOYA_ASYNC_EVENT_ID_MMU_PAGE_FAULT = 280,
+	GOYA_ASYNC_EVENT_ID_MMU_WR_PERM = 281,
+	GOYA_ASYNC_EVENT_ID_MMU_DBG_BM = 282,
+	GOYA_ASYNC_EVENT_ID_DMA_BM_CH0 = 290,
+	GOYA_ASYNC_EVENT_ID_DMA_BM_CH1 = 291,
+	GOYA_ASYNC_EVENT_ID_DMA_BM_CH2 = 292,
+	GOYA_ASYNC_EVENT_ID_DMA_BM_CH3 = 293,
+	GOYA_ASYNC_EVENT_ID_DMA_BM_CH4 = 294,
+	GOYA_ASYNC_EVENT_ID_DDR0_PHY_DFI = 300,
+	GOYA_ASYNC_EVENT_ID_DDR0_ECC_SCRUB = 301,
+	GOYA_ASYNC_EVENT_ID_DDR0_DB_ECC = 302,
+	GOYA_ASYNC_EVENT_ID_DDR0_SB_ECC = 303,
+	GOYA_ASYNC_EVENT_ID_DDR0_SB_ECC_MC = 304,
+	GOYA_ASYNC_EVENT_ID_DDR0_AXI_RD = 305,
+	GOYA_ASYNC_EVENT_ID_DDR0_AXI_WR = 306,
+	GOYA_ASYNC_EVENT_ID_DDR1_PHY_DFI = 310,
+	GOYA_ASYNC_EVENT_ID_DDR1_ECC_SCRUB = 311,
+	GOYA_ASYNC_EVENT_ID_DDR1_DB_ECC = 312,
+	GOYA_ASYNC_EVENT_ID_DDR1_SB_ECC = 313,
+	GOYA_ASYNC_EVENT_ID_DDR1_SB_ECC_MC = 314,
+	GOYA_ASYNC_EVENT_ID_DDR1_AXI_RD = 315,
+	GOYA_ASYNC_EVENT_ID_DDR1_AXI_WR = 316,
+	GOYA_ASYNC_EVENT_ID_CPU_BMON = 320,
+	GOYA_ASYNC_EVENT_ID_TS_EAST = 322,
+	GOYA_ASYNC_EVENT_ID_TS_WEST = 323,
+	GOYA_ASYNC_EVENT_ID_TS_NORTH = 324,
+	GOYA_ASYNC_EVENT_ID_PSOC_GPIO_U16_0 = 330,
+	GOYA_ASYNC_EVENT_ID_PSOC_GPIO_U16_1 = 331,
+	GOYA_ASYNC_EVENT_ID_PSOC_GPIO_U16_2 = 332,
+	GOYA_ASYNC_EVENT_ID_PSOC_GPIO_05_SW_RESET = 356,
+	GOYA_ASYNC_EVENT_ID_PSOC_GPIO_10_VRHOT_ICRIT = 361,
+	GOYA_ASYNC_EVENT_ID_TPC0_CMDQ = 430,
+	GOYA_ASYNC_EVENT_ID_TPC1_CMDQ = 431,
+	GOYA_ASYNC_EVENT_ID_TPC2_CMDQ = 432,
+	GOYA_ASYNC_EVENT_ID_TPC3_CMDQ = 433,
+	GOYA_ASYNC_EVENT_ID_TPC4_CMDQ = 434,
+	GOYA_ASYNC_EVENT_ID_TPC5_CMDQ = 435,
+	GOYA_ASYNC_EVENT_ID_TPC6_CMDQ = 436,
+	GOYA_ASYNC_EVENT_ID_TPC7_CMDQ = 437,
+	GOYA_ASYNC_EVENT_ID_TPC0_QM = 438,
+	GOYA_ASYNC_EVENT_ID_TPC1_QM = 439,
+	GOYA_ASYNC_EVENT_ID_TPC2_QM = 440,
+	GOYA_ASYNC_EVENT_ID_TPC3_QM = 441,
+	GOYA_ASYNC_EVENT_ID_TPC4_QM = 442,
+	GOYA_ASYNC_EVENT_ID_TPC5_QM = 443,
+	GOYA_ASYNC_EVENT_ID_TPC6_QM = 444,
+	GOYA_ASYNC_EVENT_ID_TPC7_QM = 445,
+	GOYA_ASYNC_EVENT_ID_MME_QM = 447,
+	GOYA_ASYNC_EVENT_ID_MME_CMDQ = 448,
+	GOYA_ASYNC_EVENT_ID_DMA0_QM = 449,
+	GOYA_ASYNC_EVENT_ID_DMA1_QM = 450,
+	GOYA_ASYNC_EVENT_ID_DMA2_QM = 451,
+	GOYA_ASYNC_EVENT_ID_DMA3_QM = 452,
+	GOYA_ASYNC_EVENT_ID_DMA4_QM = 453,
+	GOYA_ASYNC_EVENT_ID_DMA_ON_HBW = 454,
+	GOYA_ASYNC_EVENT_ID_DMA0_CH = 455,
+	GOYA_ASYNC_EVENT_ID_DMA1_CH = 456,
+	GOYA_ASYNC_EVENT_ID_DMA2_CH = 457,
+	GOYA_ASYNC_EVENT_ID_DMA3_CH = 458,
+	GOYA_ASYNC_EVENT_ID_DMA4_CH = 459,
+	GOYA_ASYNC_EVENT_ID_PI_UPDATE = 484,
+	GOYA_ASYNC_EVENT_ID_HALT_MACHINE = 485,
+	GOYA_ASYNC_EVENT_ID_INTS_REGISTER = 486,
+	GOYA_ASYNC_EVENT_ID_SOFT_RESET = 487,
+	GOYA_ASYNC_EVENT_ID_LAST_VALID_ID = 1023,
+	GOYA_ASYNC_EVENT_ID_SIZE
+};
+
+#endif /* __GOYA_ASYNC_EVENTS_H_ */
drivers/misc/habanalabs/include/goya/goya_boot_if.h new file +32 -0
new file mode 100644
index 000000000000..2e39578ec795
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ * Author: Oded Gabbay <[email protected]>
+ *
+ */
+
+#ifndef GOYA_BOOT_IF_H
+#define GOYA_BOOT_IF_H
+
+enum cpu_boot_status {
+	CPU_BOOT_STATUS_NA = 0,		/* Default value after reset of chip */
+	CPU_BOOT_STATUS_IN_WFE,
+	CPU_BOOT_STATUS_DRAM_RDY,
+	CPU_BOOT_STATUS_SRAM_AVAIL,
+	CPU_BOOT_STATUS_IN_BTL,		/* BTL is H/W FSM */
+	CPU_BOOT_STATUS_IN_PREBOOT,
+	CPU_BOOT_STATUS_IN_SPL,
+	CPU_BOOT_STATUS_IN_UBOOT,
+	CPU_BOOT_STATUS_DRAM_INIT_FAIL,
+	CPU_BOOT_STATUS_FIT_CORRUPTED
+};
+
+enum kmd_msg {
+	KMD_MSG_NA = 0,
+	KMD_MSG_GOTO_WFE,
+	KMD_MSG_FIT_RDY
+};
+
+#endif /* GOYA_BOOT_IF_H */
Signature · 1 line
2.17.1
Signature · 2 lines
Sincerely yours,
Mike.
#31 lore raw

Re: [PATCH 06/15] habanalabs: add basic Goya h/w initialization

Quoted 43 lines · Mike Rapoport

Hi,

This starts the 6-9 review :)

These were more difficult to review because small pieces of code are interleaved with large sequences of register writes. Probably making these register data rather than code can help.

Quoted 33 lines · Oded Gabbay

This patch adds the basic part of Goya's H/W initialization. It adds code that initializes Goya's internal CPU, various registers that are related to internal routing, scrambling, workarounds for H/W bugs, etc.

It also initializes Goya's security scheme that prevents the user from abusing Goya to steal data from the host, crash the host, change Goya's F/W, etc.

---
 drivers/misc/habanalabs/device.c              |   12 +
 drivers/misc/habanalabs/goya/Makefile         |    2 +-
 drivers/misc/habanalabs/goya/goya.c           | 1892 ++++++++++-
 drivers/misc/habanalabs/goya/goyaP.h          |    3 +
 drivers/misc/habanalabs/goya/goya_security.c  | 2999 +++++++++++++++++
 drivers/misc/habanalabs/habanalabs.h          |   16 +
 drivers/misc/habanalabs/habanalabs_drv.c      |    8 +
 drivers/misc/habanalabs/include/goya/goya.h   |    1 +
 .../include/goya/goya_async_events.h          |  186 +
 .../habanalabs/include/goya/goya_boot_if.h    |   32 +
 10 files changed, 5144 insertions(+), 7 deletions(-)
 create mode 100644 drivers/misc/habanalabs/goya/goya_security.c
 create mode 100644 drivers/misc/habanalabs/include/goya/goya_async_events.h
 create mode 100644 drivers/misc/habanalabs/include/goya/goya_boot_if.h
drivers/misc/habanalabs/device.c +0 -0
index 0bd86a7d34db..9fc7218a973c 100644
@@ -315,6 +315,15 @@ int hl_device_init(struct hl_device *hdev, struct class *hclass)
              goto release_ctx;
      }
 
Quoted 6 lines

drivers/misc/habanalabs/device.c int hl_device_init(struct hl_device *hdev, struct class *hclass) · line 321

Quoted 4 lines
drivers/misc/habanalabs/device.c +4 -0
+     rc = hdev->asic_funcs->hw_init(hdev);
+     if (rc) {
+             dev_err(hdev->dev, "failed to initialize the H/W\n");
+             rc = 0;

Mistype, I suppose.

Actually no :) From certain point in the init process, I would like the device to stay present with its sysfs/debugfs interface, but it will be in disabled ("malfunctioned" in sysfs) state so the user can't submit workloads. The user/sysadmin will be able to try to reset the device to make it work again, or read registers/memory through debugfs interface. So I need to "cheat" the return code to 0 to make that work.

Quoted 52 lines
Quoted 52 lines
drivers/misc/habanalabs/device.c +8 -0
+             goto out_disabled;
+     }
+
+     hdev->disabled = false;
+
      dev_notice(hdev->dev,
              "Successfully added device to habanalabs driver\n");
 
@@ -366,6 +375,9 @@ void hl_device_fini(struct hl_device *hdev)
      if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
              dev_err(hdev->dev, "kernel ctx is still alive\n");
 
+     /* Reset the H/W. It will be in idle state after this returns */
+     hdev->asic_funcs->hw_fini(hdev, true);
+
      /* Call ASIC S/W finalize function */
      hdev->asic_funcs->sw_fini(hdev);
 
drivers/misc/habanalabs/goya/Makefile +1 -1
index 5ebf3d0d5794..a57096fa41b6 100644
@@ -1,3 +1,3 @@
 subdir-ccflags-y += -I$(src)
 
-HL_GOYA_FILES :=  goya/goya.o
\ No newline at end of file
+HL_GOYA_FILES :=  goya/goya.o goya/goya_security.o
\ No newline at end of file
drivers/misc/habanalabs/goya/goya.c +2 -2
index 341ac085af82..f715e01838b3 100644
@@ -119,11 +119,11 @@ static void goya_get_fixed_properties(struct hl_device *hdev)
      prop->va_space_dram_end_address = VA_DDR_SPACE_END;
      prop->cfg_size = CFG_SIZE;
      prop->max_asid = MAX_ASID;
+     prop->cb_pool_cb_cnt = GOYA_CB_POOL_CB_CNT;
+     prop->cb_pool_cb_size = GOYA_CB_POOL_CB_SIZE;
      prop->tpc_enabled_mask = TPC_ENABLED_MASK;
 
      prop->high_pll = PLL_HIGH_DEFAULT;
-     prop->cb_pool_cb_cnt = GOYA_CB_POOL_CB_CNT;
-     prop->cb_pool_cb_size = GOYA_CB_POOL_CB_SIZE;
 }
 
 /**
@@ -459,10 +459,12 @@ static int goya_early_init(struct hl_device *hdev)
              goto disable_device;
      }
 
Quoted 9 lines

drivers/misc/habanalabs/goya/goya.c static int goya_early_init(struct hl_device *hdev) · line 463

Quoted 6 lines
drivers/misc/habanalabs/goya/goya.c +2 -4
-     val = RREG32(mmPSOC_GLOBAL_CONF_BOOT_STRAP_PINS);
-     if (val & PSOC_GLOBAL_CONF_BOOT_STRAP_PINS_SRIOV_EN_MASK)
-             dev_warn(hdev->dev,
-                     "PCI strap is not configured correctly, PCI bus errors may occur\n");
+     if (!hdev->pldm) {
+             val = RREG32(mmPSOC_GLOBAL_CONF_BOOT_STRAP_PINS);

What is the purpose of the 'mm' prefix in register names?

memory-mapped. It is a convention that I like (taken from AMD - see registers file of amdgpu code)

Quoted 66 lines
Quoted 66 lines
drivers/misc/habanalabs/goya/goya.c +59 -0
+             if (val & PSOC_GLOBAL_CONF_BOOT_STRAP_PINS_SRIOV_EN_MASK)
+                     dev_warn(hdev->dev,
+                             "PCI strap is not configured correctly, PCI bus errors may occur\n");
+     }
 
      return 0;
 
@@ -593,6 +595,1882 @@ int goya_sw_fini(struct hl_device *hdev)
      return 0;
 }
 
+/**
+ * goya_init_pll - Initialize pll registers
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+static void goya_init_pll(struct hl_device *hdev)
+{
+     struct goya_device *goya = hdev->asic_specific;
+     u16 hbw_nr, hbw_nf, hbw_od, hbw_nb;
+     u16 cpu_nr, cpu_nf, cpu_od, cpu_nb;
+     u16 mc_nr, mc_nf, mc_od, mc_nb;
+     u16 pci_nr, pci_nf, pci_od, pci_nb;
+     u16 emmc_nr, emmc_nf, emmc_od, emmc_nb;
+
+     if (!hdev->config_pll)
+             return;
+
+     if (goya->hw_cap_initialized & HW_CAP_PLL)
+             return;
+
+     if (hdev->cpu_enable) {
+             dev_info(hdev->dev,
+                     "Waiting 5s for u-boot before configuring PLLs\n");
+             ssleep(5);
+     }
+
+/*
+ * PLL possible configuration values:
+     {50000000,1,16,16,8},
+     {100000000,1,32,16,16},
+     {150000000,1,48,16,24},
+     {200000000,1,64,16,32},
+     {250000000,1,70,14,35},
+     {300000000,1,60,10,30},
+     {350000000,1,70,10,35},
+     {400000000,1,64,8,32},
+     {450000000,1,54,6,27},
+     {500000000,1,60,6,30},
+     {550000000,1,66,6,33},
+     {600000000,1,48,4,24},
+     {650000000,1,52,4,26},
+     {700000000,1,56,4,28},
+     {750000000,1,60,4,30},
+     {800000000,1,64,4,32},
+     {850000000,1,68,4,34},
+     {900000000,1,36,2,18},
+     {950000000,1,38,2,19},
+     {1000000000,1,40,2,20},
+     {1050000000,1,42,2,21},
+     {1100000000,1,44,2,22},
+     {1150000000,1,46,2,23},
+     {1200000000,1,48,2,24},
+     {1250000000,1,50,2,25},
+     {1300000000,1,52,2,26},
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 657

Quoted 5 lines
drivers/misc/habanalabs/goya/goya.c +5 -0
+     {1350000000,1,54,2,27},
+     {1400000000,1,56,2,28},
+     {1450000000,1,58,2,29},
+     {1500000000,1,60,2,30},
+     {1550000000,1,62,2,31},

Some explanation about the correspondence of these values to _nr, _nf, _od and _nb would be helpfull.

So actually this function is only relevant for working in Palladium. I think I will just remove it.
PLLs are initialized and maintained by F/W

Quoted 382 lines

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 660

Quoted 4 lines
drivers/misc/habanalabs/goya/goya.c +3 -0
+*/
+
+     if (hdev->pldm) {
                /* ? MHz */

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 670

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+             hbw_nr  = 4, hbw_nf  = 302, hbw_od  = 1, hbw_nb  = 151;
+             cpu_nr  = 0, cpu_nf  = 47, cpu_od  = 1, cpu_nb  = 32;
+             mc_nr   = 1, mc_nf   = 159, mc_od   = 9, mc_nb   = 79;
+             pci_nr  = 4, pci_nf  = 343, pci_od  = 3, pci_nb  = 171;
+             emmc_nr = 24, emmc_nf = 415, emmc_od = 15, emmc_nb = 207;
+     } else {
+             /* 200MHz */
+             hbw_nr  = 0, hbw_nf  = 63, hbw_od  = 15, hbw_nb  = 31;
+             cpu_nr  = 0, cpu_nf  = 47, cpu_od  = 1, cpu_nb  = 23;
+             mc_nr   = 2, mc_nf   = 0x9f, mc_od   = 3, mc_nb   = 0x4f;

The hex here looks inconsistent.

Quoted 17 lines
drivers/misc/habanalabs/goya/goya.c +17 -0
+             pci_nr  = 4, pci_nf  = 343, pci_od  = 3, pci_nb  = 171;
+             emmc_nr = 24, emmc_nf = 415, emmc_od = 15, emmc_nb = 207;
+     }
+
+     /* Adjust divider for SPI */
+     WREG32(mmPSOC_SPI_BAUDR, 8);
+
+     WREG32(mmCPU_PLL_RST, 1);
+     WREG32(mmCPU_PLL_NR, cpu_nr);
+     WREG32(mmCPU_PLL_NF, cpu_nf);
+     WREG32(mmCPU_PLL_OD, cpu_od);
+     WREG32(mmCPU_PLL_NB, cpu_nb);
+     WREG32(mmCPU_PLL_DATA_CHNG, 0x11);
+
+     /* delay before taking PLL out of reset */
+     udelay(100);
+

[ ... ]

Quoted 342 lines
drivers/misc/habanalabs/goya/goya.c +342 -0
+
+     goya->hw_cap_initialized |= HW_CAP_PLL;
+}
+
+static void goya_set_pll_refclk(struct hl_device *hdev)
+{
+     WREG32(mmCPU_PLL_DIV_SEL_0, 0x0);
+     WREG32(mmCPU_PLL_DIV_SEL_1, 0x0);
+     WREG32(mmCPU_PLL_DIV_SEL_2, 0x0);
+     WREG32(mmCPU_PLL_DIV_SEL_3, 0x0);
+
+     WREG32(mmIC_PLL_DIV_SEL_0, 0x0);
+     WREG32(mmIC_PLL_DIV_SEL_1, 0x0);
+     WREG32(mmIC_PLL_DIV_SEL_2, 0x0);
+     WREG32(mmIC_PLL_DIV_SEL_3, 0x0);
+
+     WREG32(mmMC_PLL_DIV_SEL_0, 0x0);
+     WREG32(mmMC_PLL_DIV_SEL_1, 0x0);
+     WREG32(mmMC_PLL_DIV_SEL_2, 0x0);
+     WREG32(mmMC_PLL_DIV_SEL_3, 0x0);
+
+     WREG32(mmPSOC_MME_PLL_DIV_SEL_0, 0x0);
+     WREG32(mmPSOC_MME_PLL_DIV_SEL_1, 0x0);
+     WREG32(mmPSOC_MME_PLL_DIV_SEL_2, 0x0);
+     WREG32(mmPSOC_MME_PLL_DIV_SEL_3, 0x0);
+
+     WREG32(mmPSOC_PCI_PLL_DIV_SEL_0, 0x0);
+     WREG32(mmPSOC_PCI_PLL_DIV_SEL_1, 0x0);
+     WREG32(mmPSOC_PCI_PLL_DIV_SEL_2, 0x0);
+     WREG32(mmPSOC_PCI_PLL_DIV_SEL_3, 0x0);
+
+     WREG32(mmPSOC_EMMC_PLL_DIV_SEL_0, 0x0);
+     WREG32(mmPSOC_EMMC_PLL_DIV_SEL_1, 0x0);
+     WREG32(mmPSOC_EMMC_PLL_DIV_SEL_2, 0x0);
+     WREG32(mmPSOC_EMMC_PLL_DIV_SEL_3, 0x0);
+
+     WREG32(mmTPC_PLL_DIV_SEL_0, 0x0);
+     WREG32(mmTPC_PLL_DIV_SEL_1, 0x0);
+     WREG32(mmTPC_PLL_DIV_SEL_2, 0x0);
+     WREG32(mmTPC_PLL_DIV_SEL_3, 0x0);
+}
+
+static void goya_disable_clk_rlx(struct hl_device *hdev)
+{
+     WREG32(mmPSOC_MME_PLL_CLK_RLX_0, 0x100010);
+     WREG32(mmIC_PLL_CLK_RLX_0, 0x100010);
+}
+
+/**
+ * goya_init_ddr_ch0 - Initialize DDR CH0 controller of the chip
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+static void goya_init_ddr_ch0(struct hl_device *hdev)
+{
+     struct goya_device *goya = hdev->asic_specific;
+     u32 val;
+
+     if (goya->hw_cap_initialized & HW_CAP_DDR_0)
+             return;
+
+     val = RREG32(mmDDR_MISC_CH0_CFG_DONE);
+     if (val & DDR_MISC_CH0_CFG_DONE_CFG_DONE_MASK) {
+             goya->hw_cap_initialized |= HW_CAP_DDR_0;
+             return;
+     }
+
+     WREG32(mmDDR_MC_CH0_DBG1, 0x00000001);
+     WREG32(mmDDR_MC_CH0_PWRCTL, 0x00000001);
+
+     val = RREG32(mmDDR_MC_CH0_STAT);
+
+     WREG32(mmDDR_MC_CH0_MSTR, 0x81040210);
+     WREG32(mmDDR_MC_CH0_MRCTRL0, 0x4000a0f0);
+     WREG32(mmDDR_MC_CH0_MRCTRL1, 0x00022ad0);
+     WREG32(mmDDR_MC_CH0_MRCTRL2, 0x091629e1);
+     WREG32(mmDDR_MC_CH0_PWRCTL, 0x00000008);
+     WREG32(mmDDR_MC_CH0_PWRTMG, 0x00040002);
+     WREG32(mmDDR_MC_CH0_HWLPCTL, 0x00be0002);
+     WREG32(mmDDR_MC_CH0_RFSHCTL0, 0x0091f020);
+     WREG32(mmDDR_MC_CH0_RFSHCTL1, 0x00120018);
+     WREG32((mmDDR_MC_CH0_MSTR + 0x00000058), 0x00160005);
+     WREG32(mmDDR_MC_CH0_RFSHCTL3, 0x00000020);
+     WREG32(mmDDR_MC_CH0_RFSHTMG, 0x003000d0);
+     WREG32(mmDDR_MC_CH0_ECCCFG0, 0x00000010);
+     WREG32(mmDDR_MC_CH0_ECCCFG1, 0x00000002);
+     WREG32(mmDDR_MC_CH0_ECCCTL, 0x00000300);
+     WREG32(mmDDR_MC_CH0_ECCPOISONADDR0, 0x00000078);
+     WREG32(mmDDR_MC_CH0_ECCPOISONADDR1, 0x100062f7);
+     WREG32(mmDDR_MC_CH0_CRCPARCTL0, 0x00008000);
+     WREG32(mmDDR_MC_CH0_CRCPARCTL1, 0x0e088301);
+     WREG32(mmDDR_MC_CH0_CRCPARCTL2, 0x00600527);
+     WREG32(mmDDR_MC_CH0_INIT0, 0x00070002);
+     WREG32(mmDDR_MC_CH0_INIT1, 0x0001000e);
+     WREG32(mmDDR_MC_CH0_INIT3, 0x0c510001);
+     WREG32(mmDDR_MC_CH0_INIT4, 0x00280400);
+     WREG32(mmDDR_MC_CH0_INIT5, 0x00110000);
+     WREG32(mmDDR_MC_CH0_INIT6, 0x02000643);
+     WREG32(mmDDR_MC_CH0_INIT7, 0x00001000);
+     WREG32(mmDDR_MC_CH0_DIMMCTL, 0x00000000);
+     WREG32(mmDDR_MC_CH0_RANKCTL, 0x000009a0);
+     WREG32(mmDDR_MC_CH0_DRAMTMG0, 0x1918361a);
+     WREG32(mmDDR_MC_CH0_DRAMTMG1, 0x00080724);
+     WREG32(mmDDR_MC_CH0_DRAMTMG2, 0x080d0713);
+     WREG32(mmDDR_MC_CH0_DRAMTMG3, 0x00012012);
+     WREG32(mmDDR_MC_CH0_DRAMTMG4, 0x0b04060b);
+     WREG32(mmDDR_MC_CH0_DRAMTMG5, 0x0a0c0804);
+     WREG32(mmDDR_MC_CH0_DRAMTMG8, 0x0606490c);
+     WREG32(mmDDR_MC_CH0_DRAMTMG9, 0x0002050f);
+     WREG32(mmDDR_MC_CH0_DRAMTMG10, 0x000e0d0f);
+     WREG32(mmDDR_MC_CH0_DRAMTMG11, 0x270b011f);
+     WREG32(mmDDR_MC_CH0_DRAMTMG12, 0x00000010);
+     WREG32(mmDDR_MC_CH0_DRAMTMG15, 0x00000000);
+     WREG32(mmDDR_MC_CH0_ZQCTL0, 0x31000040);
+     WREG32(mmDDR_MC_CH0_ZQCTL1, 0x00000070);
+     WREG32(mmDDR_MC_CH0_DFITMG0, 0x05978211);
+     WREG32(mmDDR_MC_CH0_DFITMG1, 0x00080101);
+     WREG32(mmDDR_MC_CH0_DFILPCFG0, 0x07006031);
+     WREG32(mmDDR_MC_CH0_DFILPCFG1, 0x00000010);
+     WREG32(mmDDR_MC_CH0_DFIUPD0, 0x40400018);
+     WREG32(mmDDR_MC_CH0_DFIUPD1, 0x000b0046);
+     WREG32(mmDDR_MC_CH0_DFIUPD2, 0x00000000);
+     WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000041);
+     WREG32(mmDDR_MC_CH0_DFITMG2, 0x00001711);
+     WREG32(mmDDR_MC_CH0_DFITMG3, 0x0000001e);
+     WREG32(mmDDR_MC_CH0_DBICTL, 0x00000001);
+     WREG32(mmDDR_MC_CH0_DFIPHYMSTR, 0x00000000);
+     WREG32(mmDDR_MC_CH0_ADDRMAP0, 0x00001f1f);
+     WREG32(mmDDR_MC_CH0_ADDRMAP1, 0x003f1503);
+     WREG32(mmDDR_MC_CH0_ADDRMAP2, 0x01000400);
+     WREG32(mmDDR_MC_CH0_ADDRMAP3, 0x04000505);
+     WREG32(mmDDR_MC_CH0_ADDRMAP4, 0x00001f1f);
+     WREG32(mmDDR_MC_CH0_ADDRMAP5, 0x06060303);
+     WREG32(mmDDR_MC_CH0_ADDRMAP6, 0x0f050709);
+     WREG32(mmDDR_MC_CH0_ADDRMAP7, 0x00000f0f);
+     WREG32(mmDDR_MC_CH0_ADDRMAP8, 0x00003f01);
+     WREG32(mmDDR_MC_CH0_ADDRMAP9, 0x09000606);
+     WREG32(mmDDR_MC_CH0_ADDRMAP10, 0x02090105);
+     WREG32(mmDDR_MC_CH0_ADDRMAP11, 0x0000000a);
+     WREG32(mmDDR_MC_CH0_ODTCFG, 0x09090a08);
+     WREG32(mmDDR_MC_CH0_ODTMAP, 0x9ae1b5fe);
+     WREG32(mmDDR_MC_CH0_SCHED, 0x664d3700);
+     WREG32(mmDDR_MC_CH0_SCHED1, 0x00000000);
+     WREG32(mmDDR_MC_CH0_PERFHPR1, 0x1700e024);
+     WREG32(mmDDR_MC_CH0_PERFLPR1, 0x1e00836c);
+     WREG32(mmDDR_MC_CH0_PERFWR1, 0x260046c9);
+     WREG32(mmDDR_MC_CH0_DQMAP0, 0x0d2b3503);
+     WREG32(mmDDR_MC_CH0_DQMAP1, 0x042a0537);
+     WREG32(mmDDR_MC_CH0_DQMAP2, 0x330b2806);
+     WREG32(mmDDR_MC_CH0_DQMAP3, 0x27013803);
+     WREG32(mmDDR_MC_CH0_DQMAP4, 0x0000022c);
+     WREG32(mmDDR_MC_CH0_DQMAP5, 0x00000001);
+     WREG32(mmDDR_MC_CH0_DBG0, 0x00000001);
+     WREG32(mmDDR_MC_CH0_DBG1, 0x00000000);
+     WREG32(mmDDR_MC_CH0_DBGCMD, 0x00000000);
+     WREG32(mmDDR_MC_CH0_SWCTL, 0x00000001);
+     WREG32(mmDDR_MC_CH0_POISONCFG, 0x00000001);
+     WREG32(mmDDR_MC_CH0_ADVECCINDEX, 0x00000004);
+     WREG32(mmDDR_MC_CH0_ECCPOISONPAT0, 0x00000000);
+     WREG32(mmDDR_MC_CH0_ECCPOISONPAT1, 0x00000000);
+     WREG32(mmDDR_MC_CH0_ECCPOISONPAT2, 0x00000000);
+     WREG32(mmDDR_MC_CH0_CAPARPOISONCTL, 0x00000000);
+     WREG32(mmDDR_MC_CH0_PCCFG, 0x00000011);
+     WREG32(mmDDR_MC_CH0_PCFGR_0, 0x0000518c);
+     WREG32(mmDDR_MC_CH0_PCFGW_0, 0x00001263);
+     WREG32(mmDDR_MC_CH0_PCTRL_0, 0x00000001);
+     WREG32(mmDDR_MC_CH0_PCFGQOS0_0, 0x0011000e);
+     WREG32(mmDDR_MC_CH0_SBRCTL, 0x0016b540);
+     WREG32(mmDDR_MC_CH0_SBRWDATA0, 0x8c1d1786);
+     WREG32(mmDDR_MC_CH0_SBRWDATA1, 0x265f03dd);
+
+     val = RREG32(mmDDR_MC_CH0_RFSHCTL3);
+
+     WREG32(mmDDR_MISC_CH0_CFG_DONE, 0x00000001);
+
+     WREG32(mmDDR_MC_CH0_DBG1, 0x00000000);
+
+     val = RREG32(mmDDR_MC_CH0_PWRCTL);
+
+     WREG32(mmDDR_MC_CH0_PWRCTL, 0x00000002);
+
+     val = RREG32(mmDDR_MC_CH0_PWRCTL);
+
+     WREG32(mmDDR_MC_CH0_PWRCTL, 0x00000000);
+     WREG32(mmDDR_MC_CH0_SWCTL, 0x00000000);
+     WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000041);
+     WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000040);
+     WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000040);
+     WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000060);
+     WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000040);
+     WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000041);
+     WREG32(mmDDR_MC_CH0_DFIMISC, 0x00000041);
+     WREG32(mmDDR_MC_CH0_PCTRL_0, 0x00000001);
+
+     goya->hw_cap_initialized |= HW_CAP_DDR_0;
+}
+
+/**
+ * goya_init_ddr_ch1 - Initialize DDR CH1 controller of the chip
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ */
+static void goya_init_ddr_ch1(struct hl_device *hdev)
+{
+     struct goya_device *goya = hdev->asic_specific;
+     u32 val;
+
+     if (goya->hw_cap_initialized & HW_CAP_DDR_1)
+             return;
+
+     val = RREG32(mmDDR_MISC_CH1_CFG_DONE);
+     if (val & DDR_MISC_CH1_CFG_DONE_CFG_DONE_MASK) {
+             goya->hw_cap_initialized |= HW_CAP_DDR_1;
+             return;
+     }
+
+     WREG32(mmDDR_MC_CH1_DBG1, 0x00000001);
+     WREG32(mmDDR_MC_CH1_PWRCTL, 0x00000001);
+
+     val = RREG32(mmDDR_MC_CH1_STAT);
+
+     WREG32(mmDDR_MC_CH1_MSTR, 0x81040210);
+     WREG32(mmDDR_MC_CH1_MRCTRL0, 0x4000a0f0);
+     WREG32(mmDDR_MC_CH1_MRCTRL1, 0x00022ad0);
+     WREG32(mmDDR_MC_CH1_MRCTRL2, 0x091629e1);
+     WREG32(mmDDR_MC_CH1_PWRCTL, 0x00000008);
+     WREG32(mmDDR_MC_CH1_PWRTMG, 0x00040002);
+     WREG32(mmDDR_MC_CH1_HWLPCTL, 0x00be0002);
+     WREG32(mmDDR_MC_CH1_RFSHCTL0, 0x0091f020);
+     WREG32(mmDDR_MC_CH1_RFSHCTL1, 0x00120018);
+     WREG32((mmDDR_MC_CH1_MSTR + 0x00000058), 0x00160005);
+     WREG32(mmDDR_MC_CH1_RFSHCTL3, 0x00000020);
+     WREG32(mmDDR_MC_CH1_RFSHTMG, 0x003000d0);
+     WREG32(mmDDR_MC_CH1_ECCCFG0, 0x00000010);
+     WREG32(mmDDR_MC_CH1_ECCCFG1, 0x00000002);
+     WREG32(mmDDR_MC_CH1_ECCCTL, 0x00000300);
+     WREG32(mmDDR_MC_CH1_ECCPOISONADDR0, 0x00000078);
+     WREG32(mmDDR_MC_CH1_ECCPOISONADDR1, 0x100062f7);
+     WREG32(mmDDR_MC_CH1_CRCPARCTL0, 0x00008000);
+     WREG32(mmDDR_MC_CH1_CRCPARCTL1, 0x0e088301);
+     WREG32(mmDDR_MC_CH1_CRCPARCTL2, 0x00600527);
+     WREG32(mmDDR_MC_CH1_INIT0, 0x00070002);
+     WREG32(mmDDR_MC_CH1_INIT1, 0x0001000e);
+     WREG32(mmDDR_MC_CH1_INIT3, 0x0c510001);
+     WREG32(mmDDR_MC_CH1_INIT4, 0x00280400);
+     WREG32(mmDDR_MC_CH1_INIT5, 0x00110000);
+     WREG32(mmDDR_MC_CH1_INIT6, 0x02000643);
+     WREG32(mmDDR_MC_CH1_INIT7, 0x00001000);
+     WREG32(mmDDR_MC_CH1_DIMMCTL, 0x00000000);
+     WREG32(mmDDR_MC_CH1_RANKCTL, 0x000009a0);
+     WREG32(mmDDR_MC_CH1_DRAMTMG0, 0x1918361a);
+     WREG32(mmDDR_MC_CH1_DRAMTMG1, 0x00080724);
+     WREG32(mmDDR_MC_CH1_DRAMTMG2, 0x080d0713);
+     WREG32(mmDDR_MC_CH1_DRAMTMG3, 0x00012012);
+     WREG32(mmDDR_MC_CH1_DRAMTMG4, 0x0b04060b);
+     WREG32(mmDDR_MC_CH1_DRAMTMG5, 0x0a0c0804);
+     WREG32(mmDDR_MC_CH1_DRAMTMG8, 0x0606490c);
+     WREG32(mmDDR_MC_CH1_DRAMTMG9, 0x0002050f);
+     WREG32(mmDDR_MC_CH1_DRAMTMG10, 0x000e0d0f);
+     WREG32(mmDDR_MC_CH1_DRAMTMG11, 0x270b011f);
+     WREG32(mmDDR_MC_CH1_DRAMTMG12, 0x00000010);
+     WREG32(mmDDR_MC_CH1_DRAMTMG15, 0x00000000);
+     WREG32(mmDDR_MC_CH1_ZQCTL0, 0x31000040);
+     WREG32(mmDDR_MC_CH1_ZQCTL1, 0x00000070);
+     WREG32(mmDDR_MC_CH1_DFITMG0, 0x05978211);
+     WREG32(mmDDR_MC_CH1_DFITMG1, 0x00080101);
+     WREG32(mmDDR_MC_CH1_DFILPCFG0, 0x07006031);
+     WREG32(mmDDR_MC_CH1_DFILPCFG1, 0x00000010);
+     WREG32(mmDDR_MC_CH1_DFIUPD0, 0x40400018);
+     WREG32(mmDDR_MC_CH1_DFIUPD1, 0x000b0046);
+     WREG32(mmDDR_MC_CH1_DFIUPD2, 0x00000000);
+     WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000041);
+     WREG32(mmDDR_MC_CH1_DFITMG2, 0x00001711);
+     WREG32(mmDDR_MC_CH1_DFITMG3, 0x0000001e);
+     WREG32(mmDDR_MC_CH1_DBICTL, 0x00000001);
+     WREG32(mmDDR_MC_CH1_DFIPHYMSTR, 0x00000000);
+     WREG32(mmDDR_MC_CH1_ADDRMAP0, 0x00001f1f);
+     WREG32(mmDDR_MC_CH1_ADDRMAP1, 0x003f1503);
+     WREG32(mmDDR_MC_CH1_ADDRMAP2, 0x01000400);
+     WREG32(mmDDR_MC_CH1_ADDRMAP3, 0x04000505);
+     WREG32(mmDDR_MC_CH1_ADDRMAP4, 0x00001f1f);
+     WREG32(mmDDR_MC_CH1_ADDRMAP5, 0x06060303);
+     WREG32(mmDDR_MC_CH1_ADDRMAP6, 0x0f050709);
+     WREG32(mmDDR_MC_CH1_ADDRMAP7, 0x00000f0f);
+     WREG32(mmDDR_MC_CH1_ADDRMAP8, 0x00003f01);
+     WREG32(mmDDR_MC_CH1_ADDRMAP9, 0x09000606);
+     WREG32(mmDDR_MC_CH1_ADDRMAP10, 0x02090105);
+     WREG32(mmDDR_MC_CH1_ADDRMAP11, 0x0000000a);
+     WREG32(mmDDR_MC_CH1_ODTCFG, 0x09090a08);
+     WREG32(mmDDR_MC_CH1_ODTMAP, 0x9ae1b5fe);
+     WREG32(mmDDR_MC_CH1_SCHED, 0x664d3700);
+     WREG32(mmDDR_MC_CH1_SCHED1, 0x00000000);
+     WREG32(mmDDR_MC_CH1_PERFHPR1, 0x1700e024);
+     WREG32(mmDDR_MC_CH1_PERFLPR1, 0x1e00836c);
+     WREG32(mmDDR_MC_CH1_PERFWR1, 0x260046c9);
+     WREG32(mmDDR_MC_CH1_DQMAP0, 0x0d2b3503);
+     WREG32(mmDDR_MC_CH1_DQMAP1, 0x042a0537);
+     WREG32(mmDDR_MC_CH1_DQMAP2, 0x330b2806);
+     WREG32(mmDDR_MC_CH1_DQMAP3, 0x27013803);
+     WREG32(mmDDR_MC_CH1_DQMAP4, 0x0000022c);
+     WREG32(mmDDR_MC_CH1_DQMAP5, 0x00000001);
+     WREG32(mmDDR_MC_CH1_DBG0, 0x00000001);
+     WREG32(mmDDR_MC_CH1_DBG1, 0x00000000);
+     WREG32(mmDDR_MC_CH1_DBGCMD, 0x00000000);
+     WREG32(mmDDR_MC_CH1_SWCTL, 0x00000001);
+     WREG32(mmDDR_MC_CH1_POISONCFG, 0x00000001);
+     WREG32(mmDDR_MC_CH1_ADVECCINDEX, 0x00000004);
+     WREG32(mmDDR_MC_CH1_ECCPOISONPAT0, 0x00000000);
+     WREG32(mmDDR_MC_CH1_ECCPOISONPAT1, 0x00000000);
+     WREG32(mmDDR_MC_CH1_ECCPOISONPAT2, 0x00000000);
+     WREG32(mmDDR_MC_CH1_CAPARPOISONCTL, 0x00000000);
+     WREG32(mmDDR_MC_CH1_PCCFG, 0x00000011);
+     WREG32(mmDDR_MC_CH1_PCFGR_0, 0x0000518c);
+     WREG32(mmDDR_MC_CH1_PCFGW_0, 0x00001263);
+     WREG32(mmDDR_MC_CH1_PCTRL_0, 0x00000001);
+     WREG32(mmDDR_MC_CH1_PCFGQOS0_0, 0x0011000e);
+     WREG32(mmDDR_MC_CH1_SBRCTL, 0x0016b540);
+     WREG32(mmDDR_MC_CH1_SBRWDATA0, 0x8c1d1786);
+     WREG32(mmDDR_MC_CH1_SBRWDATA1, 0x265f03dd);
+
+     val = RREG32(mmDDR_MC_CH1_RFSHCTL3);
+
+     WREG32(mmDDR_MISC_CH1_CFG_DONE, 0x00000001);
+
+     WREG32(mmDDR_MC_CH1_DBG1, 0x00000000);
+
+     val = RREG32(mmDDR_MC_CH1_PWRCTL);
+
+     WREG32(mmDDR_MC_CH1_PWRCTL, 0x00000002);
+
+     val = RREG32(mmDDR_MC_CH1_PWRCTL);
+
+     WREG32(mmDDR_MC_CH1_PWRCTL, 0x00000000);
+     WREG32(mmDDR_MC_CH1_SWCTL, 0x00000000);
+     WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000041);
+     WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000040);
+     WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000040);
+     WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000060);
+     WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000040);
+     WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000041);
Quoted 9 lines

drivers/misc/habanalabs/goya/goya.c

Quoted 2 lines
drivers/misc/habanalabs/goya/goya.c +2 -0
+     WREG32(mmDDR_MC_CH1_DFIMISC, 0x00000041);
+     WREG32(mmDDR_MC_CH1_PCTRL_0, 0x00000001);

The initialization sequence for the second DDR channel looks really similar to that of the first channel.
I would guess their control registers have identical offsets from some base address. If this is the case the DDR initialization can be factored out and get that base address as a parameter.

Again, this function is only relevant for working in Palladium. I will just remove it.
DDR is initialized by F/W

Quoted 5 lines
Quoted 5 lines
drivers/misc/habanalabs/goya/goya.c +5 -0
+
+     goya->hw_cap_initialized |= HW_CAP_DDR_1;
+}
+
+static void _goya_tpc_mbist_workaround(struct hl_device *hdev, u8 tpc_id)
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1202

Quoted 6 lines
drivers/misc/habanalabs/goya/goya.c +6 -0
+{
+     u64 tpc_eml_address;
+     u32 val, tpc_offset, tpc_eml_offset, tpc_slm_offset;
+     int err, slm_index;
+
+     WARN_ON(tpc_id >= TPC_MAX_NUM);

Is it safe to continue if tpc_id >= TPC_MAX_NUM?

no, but I also think this is not needed because this is a static function that is called from only one place with a well defined for loop. If I will check this parameter I will need to check every parameter for every static function. Bottom line, I will remove this line.

Quoted 150 lines
Quoted 150 lines
drivers/misc/habanalabs/goya/goya.c +149 -0
+     tpc_offset = tpc_id * 0x40000;
+     tpc_eml_offset = tpc_id * 0x200000;
+     tpc_eml_address = (mmTPC0_EML_CFG_BASE + tpc_eml_offset - CFG_BASE);
+     tpc_slm_offset = tpc_eml_address + 0x100000;
+
+     /*
+      * Workaround for Bug H2 #2443 :
+      * "TPC SB is not initialized on chip reset"
+      */
+
+     val = RREG32(mmTPC0_CFG_FUNC_MBIST_CNTRL + tpc_offset);
+     if (val & TPC0_CFG_FUNC_MBIST_CNTRL_MBIST_ACTIVE_MASK)
+             dev_warn(hdev->dev, "TPC%d MBIST ACTIVE is not cleared\n",
+                     tpc_id);
+
+     WREG32(mmTPC0_CFG_FUNC_MBIST_PAT + tpc_offset, val & 0xFFFFF000);
+
+     WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_0 + tpc_offset, 0x37FF);
+     WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_1 + tpc_offset, 0x303F);
+     WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_2 + tpc_offset, 0x71FF);
+     WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_3 + tpc_offset, 0x71FF);
+     WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_4 + tpc_offset, 0x70FF);
+     WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_5 + tpc_offset, 0x70FF);
+     WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_6 + tpc_offset, 0x70FF);
+     WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_7 + tpc_offset, 0x70FF);
+     WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_8 + tpc_offset, 0x70FF);
+     WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_9 + tpc_offset, 0x70FF);
+
+     WREG32_OR(mmTPC0_CFG_FUNC_MBIST_CNTRL + tpc_offset,
+             1 << TPC0_CFG_FUNC_MBIST_CNTRL_MBIST_START_SHIFT);
+
+     err = hl_poll_timeout(
+             hdev,
+             mmTPC0_CFG_FUNC_MBIST_CNTRL + tpc_offset,
+             val,
+             (val & TPC0_CFG_FUNC_MBIST_CNTRL_MBIST_DONE_MASK),
+             1000,
+             HL_DEVICE_TIMEOUT_USEC);
+
+     if (err)
+             dev_err(hdev->dev,
+                     "Timeout while waiting for TPC%d MBIST DONE\n", tpc_id);
+
+     WREG32_OR(mmTPC0_EML_CFG_DBG_CNT + tpc_eml_offset,
+             1 << TPC0_EML_CFG_DBG_CNT_CORE_RST_SHIFT);
+
+     msleep(GOYA_RESET_WAIT_MSEC);
+
+     WREG32_AND(mmTPC0_EML_CFG_DBG_CNT + tpc_eml_offset,
+             ~(1 << TPC0_EML_CFG_DBG_CNT_CORE_RST_SHIFT));
+
+     msleep(GOYA_RESET_WAIT_MSEC);
+
+     for (slm_index = 0 ; slm_index < 256 ; slm_index++)
+             WREG32(tpc_slm_offset + (slm_index << 2), 0);
+
+     val = RREG32(tpc_slm_offset);
+
+     WREG32(mmTPC0_CFG_BASE + tpc_offset + 0xF40 - CFG_BASE, 0x100);
+}
+
+static void goya_tpc_mbist_workaround(struct hl_device *hdev)
+{
+     struct goya_device *goya = hdev->asic_specific;
+     int i;
+
+     if (hdev->pldm)
+             return;
+
+     if (goya->hw_cap_initialized & HW_CAP_TPC_MBIST)
+             return;
+
+     /* Workaround for H2 #2443 */
+
+     for (i = 0 ; i < TPC_MAX_NUM ; i++)
+             _goya_tpc_mbist_workaround(hdev, i);
+
+     goya->hw_cap_initialized |= HW_CAP_TPC_MBIST;
+}
+
+/**
+ * goya_init_golden_registers - Initialize golden registers
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Initialize the H/W registers of the device
+ *
+ */
+static void goya_init_golden_registers(struct hl_device *hdev)
+{
+     struct goya_device *goya = hdev->asic_specific;
+     u32 polynom[10], tpc_intr_mask;
+
+     if (goya->hw_cap_initialized & HW_CAP_GOLDEN)
+             return;
+
+     polynom[0] = 0x00020080;
+     polynom[1] = 0x00401000;
+     polynom[2] = 0x00200800;
+     polynom[3] = 0x00002000;
+     polynom[4] = 0x00080200;
+     polynom[5] = 0x00040100;
+     polynom[6] = 0x00100400;
+     polynom[7] = 0x00004000;
+     polynom[8] = 0x00010000;
+     polynom[9] = 0x00008000;
+
+     /* Mask all arithmetic interrupts from TPC */
+     tpc_intr_mask = 0x7FFF;
+
+     WREG32(mmDMA_NRTR_SPLIT_COEF_0, polynom[0] >> 7);
+     WREG32(mmDMA_NRTR_SPLIT_COEF_1, polynom[1] >> 7);
+     WREG32(mmDMA_NRTR_SPLIT_COEF_2, polynom[2] >> 7);
+     WREG32(mmDMA_NRTR_SPLIT_COEF_3, polynom[3] >> 7);
+     WREG32(mmDMA_NRTR_SPLIT_COEF_4, polynom[4] >> 7);
+     WREG32(mmDMA_NRTR_SPLIT_COEF_5, polynom[5] >> 7);
+     WREG32(mmDMA_NRTR_SPLIT_COEF_6, polynom[6] >> 7);
+     WREG32(mmDMA_NRTR_SPLIT_COEF_7, polynom[7] >> 7);
+     WREG32(mmDMA_NRTR_SPLIT_COEF_8, polynom[8] >> 7);
+     WREG32(mmDMA_NRTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+     WREG32(mmDMA_NRTR_SCRAMB_EN, 1 << DMA_NRTR_SCRAMB_EN_VAL_SHIFT);
+     WREG32(mmDMA_NRTR_NON_LIN_SCRAMB,
+                     1 << DMA_NRTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+     WREG32(mmSRAM_Y5_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y4_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y3_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y2_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y1_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y0_X0_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y5_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y4_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y3_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y2_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y1_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y0_X1_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y5_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y4_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y3_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y2_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y1_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y0_X2_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y5_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y4_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y3_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y2_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y1_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y0_X3_RTR_HBW_RD_RQ_L_ARB, 0x302);
Quoted 8 lines

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1358

Quoted 6 lines
drivers/misc/habanalabs/goya/goya.c +6 -0
+     WREG32(mmSRAM_Y5_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y4_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y3_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y2_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y1_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);
+     WREG32(mmSRAM_Y0_X4_RTR_HBW_RD_RQ_L_ARB, 0x302);

Any chance this can be done in a loop?

fixed

Quoted 32 lines
Quoted 21 lines
drivers/misc/habanalabs/goya/goya.c +20 -0
+     WREG32(mmSRAM_Y5_X0_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y4_X0_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y3_X0_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y2_X0_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y1_X0_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y0_X0_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y5_X1_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y4_X1_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y3_X1_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y2_X1_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y1_X1_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y0_X1_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y5_X2_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y4_X2_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y3_X2_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y2_X2_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y1_X2_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y0_X2_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y5_X3_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y4_X3_RTR_HBW_DATA_L_ARB, 0x204);

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1388

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+     WREG32(mmSRAM_Y3_X3_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y2_X3_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y1_X3_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y0_X3_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y5_X4_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y4_X4_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y3_X4_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y2_X4_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y1_X4_RTR_HBW_DATA_L_ARB, 0x204);
+     WREG32(mmSRAM_Y0_X4_RTR_HBW_DATA_L_ARB, 0x204);
Quoted 1 line

Ditto.

fixed

Quoted 20 lines
Quoted 9 lines
drivers/misc/habanalabs/goya/goya.c +8 -0
+     WREG32(mmSRAM_Y5_X0_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y4_X0_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y3_X0_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y2_X0_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y1_X0_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y0_X0_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y5_X1_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y4_X1_RTR_HBW_DATA_E_ARB, 0x206);

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1406

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+     WREG32(mmSRAM_Y3_X1_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y2_X1_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y1_X1_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y0_X1_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y5_X2_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y4_X2_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y3_X2_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y2_X2_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y1_X2_RTR_HBW_DATA_E_ARB, 0x206);
+     WREG32(mmSRAM_Y0_X2_RTR_HBW_DATA_E_ARB, 0x206);
Quoted 1 line

And here and below as well.

fixed

Quoted 14 lines

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1418

Quoted 13 lines
drivers/misc/habanalabs/goya/goya.c +12 -0
+     WREG32(mmSRAM_Y5_X3_RTR_HBW_DATA_E_ARB, 0x207);
+     WREG32(mmSRAM_Y4_X3_RTR_HBW_DATA_E_ARB, 0x207);
+     WREG32(mmSRAM_Y3_X3_RTR_HBW_DATA_E_ARB, 0x207);
+     WREG32(mmSRAM_Y2_X3_RTR_HBW_DATA_E_ARB, 0x207);
+     WREG32(mmSRAM_Y1_X3_RTR_HBW_DATA_E_ARB, 0x207);
+     WREG32(mmSRAM_Y0_X3_RTR_HBW_DATA_E_ARB, 0x207);
+     WREG32(mmSRAM_Y5_X4_RTR_HBW_DATA_E_ARB, 0x207);
+     WREG32(mmSRAM_Y4_X4_RTR_HBW_DATA_E_ARB, 0x207);
+     WREG32(mmSRAM_Y3_X4_RTR_HBW_DATA_E_ARB, 0x207);
+     WREG32(mmSRAM_Y2_X4_RTR_HBW_DATA_E_ARB, 0x207);
+     WREG32(mmSRAM_Y1_X4_RTR_HBW_DATA_E_ARB, 0x207);
+     WREG32(mmSRAM_Y0_X4_RTR_HBW_DATA_E_ARB, 0x207);

fixed

Quoted 63 lines

[ ... ]

Quoted 61 lines
drivers/misc/habanalabs/goya/goya.c +61 -0
+     WREG32(mmMME1_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+     WREG32(mmMME1_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+     WREG32(mmMME1_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+     WREG32(mmMME1_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+     WREG32(mmMME1_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+     WREG32(mmMME1_RTR_SPLIT_COEF_5, polynom[5] >> 7);
+     WREG32(mmMME1_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+     WREG32(mmMME1_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+     WREG32(mmMME1_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+     WREG32(mmMME1_RTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+     WREG32(mmMME2_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+     WREG32(mmMME2_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+     WREG32(mmMME2_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+     WREG32(mmMME2_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+     WREG32(mmMME2_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+     WREG32(mmMME2_RTR_SPLIT_COEF_5, polynom[5] >> 7);
+     WREG32(mmMME2_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+     WREG32(mmMME2_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+     WREG32(mmMME2_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+     WREG32(mmMME2_RTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+     WREG32(mmMME3_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+     WREG32(mmMME3_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+     WREG32(mmMME3_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+     WREG32(mmMME3_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+     WREG32(mmMME3_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+     WREG32(mmMME3_RTR_SPLIT_COEF_5, polynom[5] >> 7);
+     WREG32(mmMME3_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+     WREG32(mmMME3_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+     WREG32(mmMME3_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+     WREG32(mmMME3_RTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+     WREG32(mmMME4_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+     WREG32(mmMME4_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+     WREG32(mmMME4_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+     WREG32(mmMME4_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+     WREG32(mmMME4_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+     WREG32(mmMME4_RTR_SPLIT_COEF_5, polynom[5] >> 7);
+     WREG32(mmMME4_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+     WREG32(mmMME4_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+     WREG32(mmMME4_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+     WREG32(mmMME4_RTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+     WREG32(mmMME5_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+     WREG32(mmMME5_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+     WREG32(mmMME5_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+     WREG32(mmMME5_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+     WREG32(mmMME5_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+     WREG32(mmMME5_RTR_SPLIT_COEF_5, polynom[5] >> 7);
+     WREG32(mmMME5_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+     WREG32(mmMME5_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+     WREG32(mmMME5_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+     WREG32(mmMME5_RTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+     WREG32(mmMME6_RTR_SPLIT_COEF_0, polynom[0] >> 7);
+     WREG32(mmMME6_RTR_SPLIT_COEF_1, polynom[1] >> 7);
+     WREG32(mmMME6_RTR_SPLIT_COEF_2, polynom[2] >> 7);
+     WREG32(mmMME6_RTR_SPLIT_COEF_3, polynom[3] >> 7);
+     WREG32(mmMME6_RTR_SPLIT_COEF_4, polynom[4] >> 7);
+     WREG32(mmMME6_RTR_SPLIT_COEF_5, polynom[5] >> 7);
Quoted 9 lines

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1663

Quoted 4 lines
drivers/misc/habanalabs/goya/goya.c +4 -0
+     WREG32(mmMME6_RTR_SPLIT_COEF_6, polynom[6] >> 7);
+     WREG32(mmMME6_RTR_SPLIT_COEF_7, polynom[7] >> 7);
+     WREG32(mmMME6_RTR_SPLIT_COEF_8, polynom[8] >> 7);
+     WREG32(mmMME6_RTR_SPLIT_COEF_9, polynom[9] >> 7);

This sequence seem to repeat itself. If the register map permits I'd suggest splitting writes of the polynom[] to registers into a helper function.

fixed with a loop

Quoted 31 lines
Quoted 31 lines
drivers/misc/habanalabs/goya/goya.c +31 -0
+
+     WREG32(mmMME1_RTR_SCRAMB_EN, 1 << MME1_RTR_SCRAMB_EN_VAL_SHIFT);
+     WREG32(mmMME1_RTR_NON_LIN_SCRAMB,
+                     1 << MME1_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+     WREG32(mmMME2_RTR_SCRAMB_EN, 1 << MME2_RTR_SCRAMB_EN_VAL_SHIFT);
+     WREG32(mmMME2_RTR_NON_LIN_SCRAMB,
+                     1 << MME2_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+     WREG32(mmMME3_RTR_SCRAMB_EN, 1 << MME3_RTR_SCRAMB_EN_VAL_SHIFT);
+     WREG32(mmMME3_RTR_NON_LIN_SCRAMB,
+                     1 << MME3_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+     WREG32(mmMME4_RTR_SCRAMB_EN, 1 << MME4_RTR_SCRAMB_EN_VAL_SHIFT);
+     WREG32(mmMME4_RTR_NON_LIN_SCRAMB,
+                     1 << MME4_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+     WREG32(mmMME5_RTR_SCRAMB_EN, 1 << MME5_RTR_SCRAMB_EN_VAL_SHIFT);
+     WREG32(mmMME5_RTR_NON_LIN_SCRAMB,
+                     1 << MME5_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+     WREG32(mmMME6_RTR_SCRAMB_EN, 1 << MME6_RTR_SCRAMB_EN_VAL_SHIFT);
+     WREG32(mmMME6_RTR_NON_LIN_SCRAMB,
+                     1 << MME6_RTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+     WREG32(mmTPC0_NRTR_SPLIT_COEF_0, polynom[0] >> 7);
+     WREG32(mmTPC0_NRTR_SPLIT_COEF_1, polynom[1] >> 7);
+     WREG32(mmTPC0_NRTR_SPLIT_COEF_2, polynom[2] >> 7);
+     WREG32(mmTPC0_NRTR_SPLIT_COEF_3, polynom[3] >> 7);
+     WREG32(mmTPC0_NRTR_SPLIT_COEF_4, polynom[4] >> 7);
+     WREG32(mmTPC0_NRTR_SPLIT_COEF_5, polynom[5] >> 7);
Quoted 9 lines

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1702

Quoted 8 lines
drivers/misc/habanalabs/goya/goya.c +8 -0
+     WREG32(mmTPC0_NRTR_SPLIT_COEF_6, polynom[6] >> 7);
+     WREG32(mmTPC0_NRTR_SPLIT_COEF_7, polynom[7] >> 7);
+     WREG32(mmTPC0_NRTR_SPLIT_COEF_8, polynom[8] >> 7);
+     WREG32(mmTPC0_NRTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+     WREG32(mmTPC0_NRTR_SCRAMB_EN, 1 << TPC0_NRTR_SCRAMB_EN_VAL_SHIFT);
+     WREG32(mmTPC0_NRTR_NON_LIN_SCRAMB,
+                     1 << TPC0_NRTR_NON_LIN_SCRAMB_EN_SHIFT);

fixed

Quoted 57 lines

[ ... ]

Quoted 32 lines
drivers/misc/habanalabs/goya/goya.c +32 -0
+     /*
+      * Workaround for Bug H2 #2441 :
+      * "ST.NOP set trace event illegal opcode"
+      */
+     WREG32(mmTPC6_CFG_TPC_INTR_MASK, tpc_intr_mask);
+
+     WREG32(mmTPC7_NRTR_SPLIT_COEF_0, polynom[0] >> 7);
+     WREG32(mmTPC7_NRTR_SPLIT_COEF_1, polynom[1] >> 7);
+     WREG32(mmTPC7_NRTR_SPLIT_COEF_2, polynom[2] >> 7);
+     WREG32(mmTPC7_NRTR_SPLIT_COEF_3, polynom[3] >> 7);
+     WREG32(mmTPC7_NRTR_SPLIT_COEF_4, polynom[4] >> 7);
+     WREG32(mmTPC7_NRTR_SPLIT_COEF_5, polynom[5] >> 7);
+     WREG32(mmTPC7_NRTR_SPLIT_COEF_6, polynom[6] >> 7);
+     WREG32(mmTPC7_NRTR_SPLIT_COEF_7, polynom[7] >> 7);
+     WREG32(mmTPC7_NRTR_SPLIT_COEF_8, polynom[8] >> 7);
+     WREG32(mmTPC7_NRTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+     WREG32(mmTPC7_NRTR_SCRAMB_EN, 1 << TPC7_NRTR_SCRAMB_EN_VAL_SHIFT);
+     WREG32(mmTPC7_NRTR_NON_LIN_SCRAMB,
+                     1 << TPC7_NRTR_NON_LIN_SCRAMB_EN_SHIFT);
+
+     /*
+      * Workaround for Bug H2 #2441 :
+      * "ST.NOP set trace event illegal opcode"
+      */
+     WREG32(mmTPC7_CFG_TPC_INTR_MASK, tpc_intr_mask);
+
+     WREG32(mmPCI_NRTR_SPLIT_COEF_0, polynom[0] >> 7);
+     WREG32(mmPCI_NRTR_SPLIT_COEF_1, polynom[1] >> 7);
+     WREG32(mmPCI_NRTR_SPLIT_COEF_2, polynom[2] >> 7);
+     WREG32(mmPCI_NRTR_SPLIT_COEF_3, polynom[3] >> 7);
+     WREG32(mmPCI_NRTR_SPLIT_COEF_4, polynom[4] >> 7);

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 1949

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+     WREG32(mmPCI_NRTR_SPLIT_COEF_5, polynom[5] >> 7);
+     WREG32(mmPCI_NRTR_SPLIT_COEF_6, polynom[6] >> 7);
+     WREG32(mmPCI_NRTR_SPLIT_COEF_7, polynom[7] >> 7);
+     WREG32(mmPCI_NRTR_SPLIT_COEF_8, polynom[8] >> 7);
+     WREG32(mmPCI_NRTR_SPLIT_COEF_9, polynom[9] >> 7);
+
+     WREG32(mmPCI_NRTR_SCRAMB_EN, 1 << PCI_NRTR_SCRAMB_EN_VAL_SHIFT);
+     WREG32(mmPCI_NRTR_NON_LIN_SCRAMB,
+                     1 << PCI_NRTR_NON_LIN_SCRAMB_EN_SHIFT);
+

I think all these long sequences of register writes could be grouped into something like

struct regs_write_seq {
        unsigned long addr;
        unsigned long val;
};
const struct regs_write_seq golden_regs1 [] {
        ...
};
Quoted 6 lines
const struct regs_write_seq workaround_bug_2411 [] {
        ...
};

and written with a helper function looping over such array.

I personally don't like so much this method. I combined it to a loop whenever possible. I hope that is good enough.

Quoted 73 lines
Quoted 73 lines
drivers/misc/habanalabs/goya/goya.c +73 -0
+     /*
+      * Workaround for H2 #HW-23 bug
+      * Set DMA max outstanding read requests to 240 on DMA CH 1. Set it
+      * to 16 on KMD DMA
+      * We need to limit only these DMAs because the user can only read
+      * from Host using DMA CH 1
+      */
+     WREG32(mmDMA_CH_0_CFG0, 0x0fff0010);
+     WREG32(mmDMA_CH_1_CFG0, 0x0fff00F0);
+
+     goya->hw_cap_initialized |= HW_CAP_GOLDEN;
+}
+
+
+/**
+ * goya_push_uboot_to_device - Push u-boot FW code to device
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Copy u-boot fw code from firmware file to SRAM BAR.
+ * Returns 0 on success
+ *
+ */
+static int goya_push_uboot_to_device(struct hl_device *hdev)
+{
+     char fw_name[200];
+     const u64 *fw_data;
+     void __iomem *dst;
+     size_t fw_size, i;
+     int rc;
+
+     snprintf(fw_name, sizeof(fw_name), "habanalabs/goya/goya-u-boot.bin");
+
+     rc = request_firmware(&hdev->spl_fw, fw_name, hdev->dev);
+
+     if (rc) {
+             dev_err(hdev->dev, "Failed to request u-boot fw image\n");
+             goto out;
+     }
+
+     fw_size = hdev->spl_fw->size;
+     if ((fw_size % 4) != 0) {
+             dev_err(hdev->dev, "illegal u-boot firmware size %lu\n",
+                     fw_size);
+             rc = -EINVAL;
+             goto out;
+     }
+
+     dev_dbg(hdev->dev, "u-boot firmware size == %lu\n", fw_size);
+
+     fw_data = (const u64 *) hdev->spl_fw->data;
+     dst = hdev->pcie_bar[SRAM_CFG_BAR_ID] + UBOOT_FW_OFFSET;
+
+     if ((hdev->spl_fw->size % 8) != 0)
+             fw_size -= 8;
+
+     for (i = 0 ; i < fw_size ; i += 8, fw_data++, dst += 8) {
+             if (!(i & (0x80000 - 1)))
+                     dev_dbg(hdev->dev,
+                             "u-boot copied so far %lu out of %lu",
+                             i, fw_size);
+
+             writeq(*fw_data, dst);
+     }
+
+     if ((hdev->spl_fw->size % 8) != 0)
+             writel(*(const u32 *) fw_data, dst);
+
+out:
+     release_firmware(hdev->spl_fw);
+     return rc;
+}
+
Quoted 9 lines
Quoted 6 lines

+/**
+ * goya_push_linux_to_device - Push LINUX FW code to device
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Copy LINXU fw code from firmware file to DDR BAR.

^ Linux

fixed

Quoted 56 lines
Quoted 42 lines

+ * Returns 0 on success
+ *
+ */

drivers/misc/habanalabs/goya/goya.c +39 -0
+static int goya_push_linux_to_device(struct hl_device *hdev)
+{
+     char fw_name[200];
+     const u64 *fw_data;
+     void __iomem *dst;
+     size_t fw_size, i;
+     int rc;
+
+     snprintf(fw_name, sizeof(fw_name), "habanalabs/goya/goya-fit.itb");
+
+     rc = request_firmware(&hdev->spl_fw, fw_name, hdev->dev);
+
+     if (rc) {
+             dev_err(hdev->dev, "Failed to request Linux fw image\n");
+             goto out;
+     }
+
+     fw_size = hdev->spl_fw->size;
+     if ((fw_size % 4) != 0) {
+             dev_err(hdev->dev, "illegal Linux firmware size %lu\n",
+                     fw_size);
+             rc = -EINVAL;
+             goto out;
+     }
+
+     dev_dbg(hdev->dev, "Linux firmware size == %lu\n", fw_size);
+
+     fw_data = (const u64 *) hdev->spl_fw->data;
+     dst = hdev->pcie_bar[DDR_BAR_ID] + LINUX_FW_OFFSET;
+
+     if ((hdev->spl_fw->size % 8) != 0)
+             fw_size -= 8;
+
+     for (i = 0 ; i < fw_size ; i += 8, fw_data++, dst += 8) {
+             if (!(i & (0x80000 - 1))) {
+                     dev_dbg(hdev->dev,
+                             "Linux copied so far %lu out of %lu",
+                             i, fw_size);
+                     usleep_range(20, 100);

drivers/misc/habanalabs/goya/goya.c int goya_sw_fini(struct hl_device *hdev) · line 2080

Quoted 10 lines
drivers/misc/habanalabs/goya/goya.c +10 -0
+             }
+             writeq(*fw_data, dst);
+     }
+
+     if ((hdev->spl_fw->size % 8) != 0)
+             writel(*(const u32 *) fw_data, dst);
+
+out:
+     release_firmware(hdev->spl_fw);
+     return rc;

The U-Boot and Linux loading to the device seem almost identical. I think it can be declared as

Quoted 5 lines

static int goya_push_fw_to_device(struct hl_device *hdev, const char *name,
void __iomem *dst)

and called twice.

fixed

Quoted 600 lines
Quoted 499 lines
drivers/misc/habanalabs/goya/goya.c +395 -0
+}
+
+static int goya_pldm_init_cpu(struct hl_device *hdev)
+{
+     u32 val, unit_rst_val;
+     int rc;
+
+     /* Must initialize SRAM scrambler before pushing u-boot to SRAM */
+     goya_init_golden_registers(hdev);
+
+     /* Put ARM cores into reset */
+     WREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL, CPU_RESET_ASSERT);
+     val = RREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL);
+
+     /* Reset the CA53 MACRO */
+     unit_rst_val = RREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N);
+     WREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N, CA53_RESET);
+     val = RREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N);
+     WREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N, unit_rst_val);
+     val = RREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N);
+
+     rc = goya_push_uboot_to_device(hdev);
+     if (rc)
+             return rc;
+
+     rc = goya_push_linux_to_device(hdev);
+     if (rc)
+             return rc;
+
+     WREG32(mmPSOC_GLOBAL_CONF_UBOOT_MAGIC, KMD_MSG_FIT_RDY);
+     WREG32(mmPSOC_GLOBAL_CONF_WARM_REBOOT, CPU_BOOT_STATUS_NA);
+
+     WREG32(mmCPU_CA53_CFG_RST_ADDR_LSB_0,
+             lower_32_bits(SRAM_BASE_ADDR + UBOOT_FW_OFFSET));
+     WREG32(mmCPU_CA53_CFG_RST_ADDR_MSB_0,
+             upper_32_bits(SRAM_BASE_ADDR + UBOOT_FW_OFFSET));
+
+     /* Release ARM core 0 from reset */
+     WREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL,
+                                     CPU_RESET_CORE0_DEASSERT);
+     val = RREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL);
+
+     return 0;
+}
+
+/*
+ * FW component passes an offset from SRAM_BASE_ADDR in SCRATCHPAD_xx.
+ * The version string should be located by that offset.
+ */
+static void goya_read_device_fw_version(struct hl_device *hdev,
+                                     enum goya_fw_component fwc)
+{
+     const char *name;
+     u32 ver_off;
+     char *dest;
+
+     switch (fwc) {
+     case FW_COMP_UBOOT:
+             ver_off = RREG32(mmPSOC_GLOBAL_CONF_SCRATCHPAD_29);
+             dest = hdev->asic_prop.uboot_ver;
+             name = "U-Boot";
+             break;
+     case FW_COMP_PREBOOT:
+             ver_off = RREG32(mmPSOC_GLOBAL_CONF_SCRATCHPAD_28);
+             dest = hdev->asic_prop.preboot_ver;
+             name = "Preboot";
+             break;
+     default:
+             dev_warn(hdev->dev, "Undefined FW component: %d\n", fwc);
+             return;
+     }
+
+     ver_off &= ~((u32)SRAM_BASE_ADDR);
+
+     if (ver_off < SRAM_SIZE - VERSION_MAX_LEN) {
+             memcpy_fromio(dest, hdev->pcie_bar[SRAM_CFG_BAR_ID] + ver_off,
+                                                     VERSION_MAX_LEN);
+     } else {
+             dev_err(hdev->dev, "%s version offset (0x%x) is above SRAM\n",
+                                                             name, ver_off);
+             strcpy(dest, "unavailable");
+     }
+}
+
+static int goya_init_cpu(struct hl_device *hdev, u32 cpu_timeout)
+{
+     struct goya_device *goya = hdev->asic_specific;
+     u32 status;
+     int rc;
+
+     if (!hdev->cpu_enable)
+             return 0;
+
+     if (goya->hw_cap_initialized & HW_CAP_CPU)
+             return 0;
+
+     /*
+      * Before pushing u-boot/linux to device, need to set the ddr bar to
+      * base address of dram
+      */
+     rc = goya_set_ddr_bar_base(hdev, DRAM_PHYS_BASE);
+     if (rc) {
+             dev_err(hdev->dev,
+                     "failed to map DDR bar to DRAM base address\n");
+             return rc;
+     }
+
+     if (hdev->pldm) {
+             rc = goya_pldm_init_cpu(hdev);
+             if (rc)
+                     return rc;
+
+             goto out;
+     }
+
+     /* Make sure CPU boot-loader is running */
+     rc = hl_poll_timeout(
+             hdev,
+             mmPSOC_GLOBAL_CONF_WARM_REBOOT,
+             status,
+             (status == CPU_BOOT_STATUS_DRAM_RDY) ||
+             (status == CPU_BOOT_STATUS_SRAM_AVAIL),
+             10000,
+             cpu_timeout);
+
+     if (rc) {
+             dev_err(hdev->dev, "Error in ARM u-boot !!!");
+             switch (status) {
+             case CPU_BOOT_STATUS_NA:
+                     dev_err(hdev->dev,
+                             "ARM status %d - BTL did NOT run\n", status);
+                     break;
+             case CPU_BOOT_STATUS_IN_WFE:
+                     dev_err(hdev->dev,
+                             "ARM status %d - Inside WFE loop\n", status);
+                     break;
+             case CPU_BOOT_STATUS_IN_BTL:
+                     dev_err(hdev->dev,
+                             "ARM status %d - Stuck in BTL\n", status);
+                     break;
+             case CPU_BOOT_STATUS_IN_PREBOOT:
+                     dev_err(hdev->dev,
+                             "ARM status %d - Stuck in Preboot\n", status);
+                     break;
+             case CPU_BOOT_STATUS_IN_SPL:
+                     dev_err(hdev->dev,
+                             "ARM status %d - Stuck in SPL\n", status);
+                     break;
+             case CPU_BOOT_STATUS_IN_UBOOT:
+                     dev_err(hdev->dev,
+                             "ARM status %d - Stuck in u-boot\n", status);
+                     break;
+             case CPU_BOOT_STATUS_DRAM_INIT_FAIL:
+                     dev_err(hdev->dev,
+                             "ARM status %d - DDR initialization failed\n",
+                             status);
+                     break;
+             default:
+                     dev_err(hdev->dev,
+                             "ARM status %d - Invalid status code\n",
+                             status);
+                     break;
+             }
+             return -EIO;
+     }
+
+     /* Read U-Boot version now in case we will later fail */
+     goya_read_device_fw_version(hdev, FW_COMP_UBOOT);
+     goya_read_device_fw_version(hdev, FW_COMP_PREBOOT);
+
+     if (status == CPU_BOOT_STATUS_SRAM_AVAIL)
+             goto out;
+
+     if (!hdev->fw_loading) {
+             dev_info(hdev->dev, "Skip loading FW\n");
+             goto out;
+     }
+
+     rc = goya_push_linux_to_device(hdev);
+     if (rc)
+             return rc;
+
+     WREG32(mmPSOC_GLOBAL_CONF_UBOOT_MAGIC, KMD_MSG_FIT_RDY);
+
+     rc = hl_poll_timeout(
+             hdev,
+             mmPSOC_GLOBAL_CONF_WARM_REBOOT,
+             status,
+             (status == CPU_BOOT_STATUS_SRAM_AVAIL),
+             10000,
+             cpu_timeout);
+
+     if (rc) {
+             if (status == CPU_BOOT_STATUS_FIT_CORRUPTED)
+                     dev_err(hdev->dev,
+                             "ARM u-boot reports FIT image is corrupted\n");
+             else
+                     dev_err(hdev->dev,
+                             "ARM Linux failed to load, %d\n", status);
+             WREG32(mmPSOC_GLOBAL_CONF_UBOOT_MAGIC, KMD_MSG_NA);
+             return -EIO;
+     }
+
+     dev_info(hdev->dev, "Successfully loaded firmware to device\n");
+
+out:
+     goya->hw_cap_initialized |= HW_CAP_CPU;
+
+     return 0;
+}
+
+/**
+ * goya_hw_init - Goya hardware initialization code
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Returns 0 on success
+ *
+ */
+static int goya_hw_init(struct hl_device *hdev)
+{
+     struct asic_fixed_properties *prop = &hdev->asic_prop;
+     u32 val;
+     int rc;
+
+     dev_info(hdev->dev, "Starting initialization of H/W\n");
+
+     /* Perform read from the device to make sure device is up */
+     val = RREG32(mmPCIE_DBI_DEVICE_ID_VENDOR_ID_REG);
+
+     goya_init_pll(hdev);
+
+     if (hdev->pldm) {
+             goya_init_ddr_ch0(hdev);
+             goya_init_ddr_ch1(hdev);
+     }
+
+     rc = goya_init_cpu(hdev, GOYA_CPU_TIMEOUT_USEC);
+     if (rc) {
+             dev_err(hdev->dev, "failed to initialize CPU\n");
+             return rc;
+     }
+
+     goya_tpc_mbist_workaround(hdev);
+
+     goya_init_golden_registers(hdev);
+
+     /*
+      * After CPU initialization is finished, change DDR bar mapping inside
+      * iATU to point to the start address of the MMU page tables
+      */
+     rc = goya_set_ddr_bar_base(hdev, DRAM_PHYS_BASE +
+             (MMU_PAGE_TABLES_ADDR & ~(prop->dram_pci_bar_size - 0x1ull)));
+     if (rc) {
+             dev_err(hdev->dev,
+                     "failed to map DDR bar to MMU page tables\n");
+             return rc;
+     }
+
+     goya_init_security(hdev);
+
+     /* CPU initialization is finished, we can now move to 48 bit DMA mask */
+     rc = pci_set_dma_mask(hdev->pdev, DMA_BIT_MASK(48));
+     if (rc) {
+             dev_warn(hdev->dev, "Unable to set pci dma mask to 48 bits\n");
+             rc = pci_set_dma_mask(hdev->pdev, DMA_BIT_MASK(32));
+             if (rc) {
+                     dev_err(hdev->dev,
+                             "Unable to set pci dma mask to 32 bits\n");
+                     return rc;
+             }
+     }
+
+     rc = pci_set_consistent_dma_mask(hdev->pdev, DMA_BIT_MASK(48));
+     if (rc) {
+             dev_warn(hdev->dev,
+                     "Unable to set pci consistent dma mask to 48 bits\n");
+             rc = pci_set_consistent_dma_mask(hdev->pdev, DMA_BIT_MASK(32));
+             if (rc) {
+                     dev_err(hdev->dev,
+                             "Unable to set pci consistent dma mask to 32 bits\n");
+                     return rc;
+             }
+     }
+
+     /* Perform read from the device to flush all MSI-X configuration */
+     val = RREG32(mmPCIE_DBI_DEVICE_ID_VENDOR_ID_REG);
+
+     return 0;
+}
+
+/**
+ * goya_hw_fini - Goya hardware tear-down code
+ *
+ * @hdev: pointer to hl_device structure
+ * @hard_reset: should we do hard reset to all engines or just reset the
+ *              compute/dma engines
+ *
+ * The function does the following:
+ * - Send interrupt to CPU to go into "quiet" mode
+ * - Stall MME, TPC
+ * - Stop External, Internal QMANs
+ * - Disable MSI-X
+ * - Issue reset command
+ * - Wait until reset is done
+ * - Start device BTL
+ *
+ */
+static void goya_hw_fini(struct hl_device *hdev, bool hard_reset)
+{
+     struct goya_device *goya = hdev->asic_specific;
+     u32 reset_timeout_ms, status;
+
+     if (hdev->pldm)
+             reset_timeout_ms = GOYA_PLDM_RESET_TIMEOUT_MSEC;
+     else
+             reset_timeout_ms = GOYA_RESET_TIMEOUT_MSEC;
+
+     if (hard_reset) {
+             goya_set_ddr_bar_base(hdev, DRAM_PHYS_BASE);
+             goya_disable_clk_rlx(hdev);
+             goya_set_pll_refclk(hdev);
+
+             WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG, RESET_ALL);
+             dev_info(hdev->dev,
+                     "Issued HARD reset command, going to wait %dms\n",
+                     reset_timeout_ms);
+     } else {
+             WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG, DMA_MME_TPC_RESET);
+             dev_info(hdev->dev,
+                     "Issued SOFT reset command, going to wait %dms\n",
+                     reset_timeout_ms);
+     }
+
+     /*
+      * After hard reset, we can't poll the BTM_FSM register because the PSOC
+      * itself is in reset. In either reset we need to wait until the reset
+      * is deasserted
+      */
+     msleep(reset_timeout_ms);
+
+     status = RREG32(mmPSOC_GLOBAL_CONF_BTM_FSM);
+     if (status & PSOC_GLOBAL_CONF_BTM_FSM_STATE_MASK)
+             dev_err(hdev->dev,
+                     "Timeout while waiting for device to reset 0x%x\n",
+                     status);
+
+     if (!hard_reset) {
+             goya->hw_cap_initialized &= ~(HW_CAP_DMA | HW_CAP_MME |
+                                             HW_CAP_GOLDEN | HW_CAP_TPC);
+             WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR,
+                             GOYA_ASYNC_EVENT_ID_SOFT_RESET);
+             return;
+     }
+
+     /* Chicken bit to re-initiate boot sequencer flow */
+     WREG32(mmPSOC_GLOBAL_CONF_BOOT_SEQ_RE_START,
+             1 << PSOC_GLOBAL_CONF_BOOT_SEQ_RE_START_IND_SHIFT);
+     /* Move boot manager FSM to pre boot sequencer init state */
+     WREG32(mmPSOC_GLOBAL_CONF_SW_BTM_FSM,
+                     0xA << PSOC_GLOBAL_CONF_SW_BTM_FSM_CTRL_SHIFT);
+
+     goya->hw_cap_initialized &= ~(HW_CAP_CPU | HW_CAP_CPU_Q |
+                                     HW_CAP_DDR_0 | HW_CAP_DDR_1 |
+                                     HW_CAP_DMA | HW_CAP_MME |
+                                     HW_CAP_MMU | HW_CAP_TPC_MBIST |
+                                     HW_CAP_GOLDEN | HW_CAP_TPC);
+
+     if (!hdev->pldm) {
+             int rc;
+             /* In case we are running inside VM and the VM is
+              * shutting down, we need to make sure CPU boot-loader
+              * is running before we can continue the VM shutdown.
+              * That is because the VM will send an FLR signal that
+              * we must answer
+              */
+             dev_info(hdev->dev,
+                     "Going to wait up to %ds for CPU boot loader\n",
+                     GOYA_CPU_TIMEOUT_USEC / 1000 / 1000);
+
+             rc = hl_poll_timeout(
+                     hdev,
+                     mmPSOC_GLOBAL_CONF_WARM_REBOOT,
+                     status,
+                     (status == CPU_BOOT_STATUS_DRAM_RDY),
+                     10000,
+                     GOYA_CPU_TIMEOUT_USEC);
+             if (rc)
+                     dev_err(hdev->dev,
+                             "failed to wait for CPU boot loader\n");
+     }
+}
+
 int goya_suspend(struct hl_device *hdev)
 {
      return 0;
@@ -641,6 +2519,8 @@ static const struct hl_asic_funcs goya_funcs = {
      .early_fini = goya_early_fini,
      .sw_init = goya_sw_init,
      .sw_fini = goya_sw_fini,
+     .hw_init = goya_hw_init,
+     .hw_fini = goya_hw_fini,
      .suspend = goya_suspend,
      .resume = goya_resume,
      .mmap = goya_mmap,
drivers/misc/habanalabs/goya/goyaP.h +3 -0
index 0e12c56472bd..45a6d2ca2752 100644
@@ -9,6 +9,7 @@
 #define GOYAP_H_
 
 #include "habanalabs.h"
+#include "include/goya/goya_boot_if.h"
 #include "include/goya/goya.h"
 
 #define NUMBER_OF_CMPLT_QUEUES               5
@@ -122,4 +123,6 @@ struct goya_device {
      u32             hw_cap_initialized;
 };
 
+void goya_init_security(struct hl_device *hdev);
+
 #endif /* GOYAP_H_ */
drivers/misc/habanalabs/goya/goya_security.c new file +69 -0
new file mode 100644
index 000000000000..99ad9aacf49e
@@ -0,0 +1,2999 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "goyaP.h"
+
+/**
+ * goya_set_block_as_protected - set the given block as protected
+ *
+ * @hdev: pointer to hl_device structure
+ * @block: block base address
+ *
+ */
+static void goya_pb_set_block(struct hl_device *hdev, u64 base)
+{
+     u32 pb_addr = base - CFG_BASE + PROT_BITS_OFFS;
+
+     while (pb_addr & 0xFFF) {
+             WREG32(pb_addr, 0);
+             pb_addr += 4;
+     }
+}
+
+static void goya_init_mme_protection_bits(struct hl_device *hdev)
+{
+     u32 pb_addr, mask;
+     u8 word_offset;
+
+     /* TODO: change to real reg name when Soc Online is updated */
+     u64 mmMME_SBB_POWER_ECO1 = 0xDFF60,
+             mmMME_SBB_POWER_ECO2 = 0xDFF64;
+
+     goya_pb_set_block(hdev, mmACC_MS_ECC_MEM_0_BASE);
+     goya_pb_set_block(hdev, mmACC_MS_ECC_MEM_1_BASE);
+     goya_pb_set_block(hdev, mmACC_MS_ECC_MEM_2_BASE);
+     goya_pb_set_block(hdev, mmACC_MS_ECC_MEM_3_BASE);
+
+     goya_pb_set_block(hdev, mmSBA_ECC_MEM_BASE);
+     goya_pb_set_block(hdev, mmSBB_ECC_MEM_BASE);
+
+     goya_pb_set_block(hdev, mmMME1_RTR_BASE);
+     goya_pb_set_block(hdev, mmMME1_RD_REGULATOR_BASE);
+     goya_pb_set_block(hdev, mmMME1_WR_REGULATOR_BASE);
+     goya_pb_set_block(hdev, mmMME2_RTR_BASE);
+     goya_pb_set_block(hdev, mmMME2_RD_REGULATOR_BASE);
+     goya_pb_set_block(hdev, mmMME2_WR_REGULATOR_BASE);
+     goya_pb_set_block(hdev, mmMME3_RTR_BASE);
+     goya_pb_set_block(hdev, mmMME3_RD_REGULATOR_BASE);
+     goya_pb_set_block(hdev, mmMME3_WR_REGULATOR_BASE);
+
+     goya_pb_set_block(hdev, mmMME4_RTR_BASE);
+     goya_pb_set_block(hdev, mmMME4_RD_REGULATOR_BASE);
+     goya_pb_set_block(hdev, mmMME4_WR_REGULATOR_BASE);
+
+     goya_pb_set_block(hdev, mmMME5_RTR_BASE);
+     goya_pb_set_block(hdev, mmMME5_RD_REGULATOR_BASE);
+     goya_pb_set_block(hdev, mmMME5_WR_REGULATOR_BASE);
+
+     goya_pb_set_block(hdev, mmMME6_RTR_BASE);
+     goya_pb_set_block(hdev, mmMME6_RD_REGULATOR_BASE);
+     goya_pb_set_block(hdev, mmMME6_WR_REGULATOR_BASE);
+
+     pb_addr = (mmMME_DUMMY & ~0xFFF) + PROT_BITS_OFFS;
+     word_offset = ((mmMME_DUMMY & PROT_BITS_OFFS) >> 7) << 2;
+     mask = 1 << ((mmMME_DUMMY & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_RESET & 0x7F) >> 2);

drivers/misc/habanalabs/goya/goya_security.c line 79

Quoted 10 lines
drivers/misc/habanalabs/goya/goya_security.c new file +10 -0
+     mask |= 1 << ((mmMME_STALL & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SM_BASE_ADDRESS_LOW & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SM_BASE_ADDRESS_HIGH & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_DBGMEM_ADD & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_DBGMEM_DATA_WR & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_DBGMEM_DATA_RD & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_DBGMEM_CTRL & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_DBGMEM_RC & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_LOG_SHADOW & 0x7F) >> 2);
+

The mask here and below seems to be a constant.
A #define could suffice, no?

Quoted 25 lines
drivers/misc/habanalabs/goya/goya_security.c new file +25 -0
+     WREG32(pb_addr + word_offset, ~mask);
+
+     pb_addr = (mmMME_STORE_MAX_CREDIT & ~0xFFF) + PROT_BITS_OFFS;
+     word_offset = ((mmMME_STORE_MAX_CREDIT & PROT_BITS_OFFS) >> 7) << 2;
+     mask = 1 << ((mmMME_STORE_MAX_CREDIT & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_AGU & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SBA & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SBB & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SBC & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_WBC & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SBA_CONTROL_DATA & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SBB_CONTROL_DATA & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SBC_CONTROL_DATA & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_WBC_CONTROL_DATA & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_TE & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_TE2DEC & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_REI_STATUS & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_REI_MASK & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SEI_STATUS & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SEI_MASK & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SPI_STATUS & 0x7F) >> 2);
+     mask |= 1 << ((mmMME_SPI_MASK & 0x7F) >> 2);
+
+     WREG32(pb_addr + word_offset, ~mask);
+

[ ... ]

Quoted 35 lines
drivers/misc/habanalabs/goya/goya_security.c new file +35 -0
+
+/**
+ * goya_init_protection_bits - Initialize protection bits for specific registers
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * All protection bits are 1 by default, means not protected. Need to set to 0
+ * each bit that belongs to a protected register.
+ *
+ */
+static void goya_init_protection_bits(struct hl_device *hdev)
+{
+     /*
+      * In each 4K block of registers, the last 128 bytes are protection
+      * bits - total of 1024 bits, one for each register. Each bit is related
+      * to a specific register, by the order of the registers.
+      * So in order to calculate the bit that is related to a given register,
+      * we need to calculate its word offset and then the exact bit inside
+      * the word (which is 4 bytes).
+      *
+      * Register address:
+      *
+      * 31                 12 11           7   6             2  1      0
+      * -----------------------------------------------------------------
+      * |      Don't         |    word       |  bit location  |    0    |
+      * |      care          |   offset      |  inside word   |         |
+      * -----------------------------------------------------------------
+      *
+      * Bits 7-11 represents the word offset inside the 128 bytes.
+      * Bits 2-6 represents the bit location inside the word.
+      */
+
+     goya_pb_set_block(hdev, mmPCI_NRTR_BASE);
+     goya_pb_set_block(hdev, mmPCI_RD_REGULATOR_BASE);
+     goya_pb_set_block(hdev, mmPCI_WR_REGULATOR_BASE);

[ ... ]

Quoted 21 lines
drivers/misc/habanalabs/goya/goya_security.c new file +21 -0
+     goya_init_mme_protection_bits(hdev);
+
+     goya_init_dma_protection_bits(hdev);
+
+     goya_init_tpc_protection_bits(hdev);
+}
+
+/**
+ * goya_init_security - Initialize security model
+ *
+ * @hdev: pointer to hl_device structure
+ *
+ * Initialize the security model of the device
+ * That includes range registers and protection bit per register
+ *
+ */
+void goya_init_security(struct hl_device *hdev)
+{
+     struct goya_device *goya = hdev->asic_specific;
+
+     u32 dram_addr_lo = lower_32_bits(DRAM_PHYS_BASE);
Quoted 8 lines

drivers/misc/habanalabs/goya/goya_security.c line 2265

Quoted 4 lines
drivers/misc/habanalabs/goya/goya_security.c new file +4 -0
+     u32 dram_addr_hi = upper_32_bits(DRAM_PHYS_BASE);
+
+     u32 lbw_rng0_base = 0xFC440000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng0_mask = 0xFFFF0000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;

These are anyway magic numbers, why not include the mask in them directly? BTW, I couldn't fine DMA_MACRO_LBW_RANGE_BASE_R_MASK anywhere in the driver.

The define is at drivers/misc/habanalabs/include/goya/asic_reg/dma_macro_regs.h

Because I prefer to see the ranges here so in case we ever need to change it is easy to understand the real address inside our chip and what this range covers.
So it's for the sake of readability.

Quoted 162 lines
Quoted 73 lines
drivers/misc/habanalabs/goya/goya_security.c new file +72 -0
+
+     u32 lbw_rng1_base = 0xFC480000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng1_mask = 0xFFF80000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng2_base = 0xFC600000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng2_mask = 0xFFE00000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng3_base = 0xFC800000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng3_mask = 0xFFF00000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng4_base = 0xFCC02000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng4_mask = 0xFFFFF000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng5_base = 0xFCC40000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng5_mask = 0xFFFF8000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng6_base = 0xFCC48000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng6_mask = 0xFFFFF000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng7_base = 0xFCC4A000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng7_mask = 0xFFFFE000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng8_base = 0xFCC4C000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng8_mask = 0xFFFFC000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng9_base = 0xFCC50000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng9_mask = 0xFFFF0000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng10_base = 0xFCC60000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng10_mask = 0xFFFE0000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng11_base = 0xFCE00000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng11_mask = 0xFFFFC000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng12_base = 0xFE484000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng12_mask = 0xFFFFF000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     u32 lbw_rng13_base = 0xFEC43000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+     u32 lbw_rng13_mask = 0xFFFFF000 & DMA_MACRO_LBW_RANGE_BASE_R_MASK;
+
+     WREG32(mmDMA_MACRO_LBW_RANGE_HIT_BLOCK, 0xFFFF);
+     WREG32(mmDMA_MACRO_HBW_RANGE_HIT_BLOCK, 0xFF);
+
+     if (!(goya->hw_cap_initialized & HW_CAP_MMU)) {
+             WREG32(mmDMA_MACRO_HBW_RANGE_HIT_BLOCK, 0xFE);
+
+             /* Protect HOST */
+             WREG32(mmDMA_MACRO_HBW_RANGE_BASE_31_0_0, 0);
+             WREG32(mmDMA_MACRO_HBW_RANGE_BASE_49_32_0, 0);
+             WREG32(mmDMA_MACRO_HBW_RANGE_MASK_31_0_0, 0);
+             WREG32(mmDMA_MACRO_HBW_RANGE_MASK_49_32_0, 0xFFF80);
+     }
+
+     /*
+      * Protect DDR @
+      * DRAM_VIRT_BASE : DRAM_VIRT_BASE + DRAM_VIRT_END
+      * The mask protects the first 512MB
+      */
+     WREG32(mmDMA_MACRO_HBW_RANGE_BASE_31_0_1, dram_addr_lo);
+     WREG32(mmDMA_MACRO_HBW_RANGE_BASE_49_32_1, dram_addr_hi);
+     WREG32(mmDMA_MACRO_HBW_RANGE_MASK_31_0_1, 0xE0000000);
+     WREG32(mmDMA_MACRO_HBW_RANGE_MASK_49_32_1, 0x3FFFF);
+
+     /* Protect registers */
+
+     WREG32(mmDMA_MACRO_LBW_RANGE_BASE_0, lbw_rng0_base);
+     WREG32(mmDMA_MACRO_LBW_RANGE_MASK_0, lbw_rng0_mask);
+     WREG32(mmDMA_MACRO_LBW_RANGE_BASE_1, lbw_rng1_base);
+     WREG32(mmDMA_MACRO_LBW_RANGE_MASK_1, lbw_rng1_mask);
+     WREG32(mmDMA_MACRO_LBW_RANGE_BASE_2, lbw_rng2_base);
+     WREG32(mmDMA_MACRO_LBW_RANGE_MASK_2, lbw_rng2_mask);
+     WREG32(mmDMA_MACRO_LBW_RANGE_BASE_3, lbw_rng3_base);

[ ... ]

Quoted 86 lines
drivers/misc/habanalabs/goya/goya_security.c new file +2 -0
+     goya_init_protection_bits(hdev);
+}
drivers/misc/habanalabs/habanalabs.h +16 -0
index 6ad476df65b0..adda281ec2af 100644
@@ -23,6 +23,8 @@
 
 #define HL_MMAP_CB_MASK                      (0x8000000000000000ull >> PAGE_SHIFT)
 
+#define HL_DEVICE_TIMEOUT_USEC               1000000 /* 1 s */
+
 #define HL_MAX_QUEUES                        128
 
 struct hl_device;
@@ -32,6 +34,8 @@ struct hl_fpriv;
 
 /**
  * struct asic_fixed_properties - ASIC specific immutable properties.
+ * @uboot_ver: F/W U-boot version.
+ * @preboot_ver: F/W Preboot version.
  * @sram_base_address: SRAM physical start address.
  * @sram_end_address: SRAM physical end address.
  * @sram_user_base_address - SRAM physical start address for user access.
@@ -60,6 +64,8 @@ struct hl_fpriv;
  * @tpc_enabled_mask: which TPCs are enabled.
  */
 struct asic_fixed_properties {
+     char                    uboot_ver[VERSION_MAX_LEN];
+     char                    preboot_ver[VERSION_MAX_LEN];
      u64                     sram_base_address;
      u64                     sram_end_address;
      u64                     sram_user_base_address;
@@ -168,6 +174,8 @@ enum hl_asic_type {
  * @early_fini: tears down what was done in early_init.
  * @sw_init: sets up driver state, does not configure H/W.
  * @sw_fini: tears down driver state, does not configure H/W.
+ * @hw_init: sets up the H/W state.
+ * @hw_fini: tears down the H/W state.
  * @suspend: handles IP specific H/W or SW changes for suspend.
  * @resume: handles IP specific H/W or SW changes for resume.
  * @mmap: mmap function, does nothing.
@@ -180,6 +188,8 @@ struct hl_asic_funcs {
      int (*early_fini)(struct hl_device *hdev);
      int (*sw_init)(struct hl_device *hdev);
      int (*sw_fini)(struct hl_device *hdev);
+     int (*hw_init)(struct hl_device *hdev);
+     void (*hw_fini)(struct hl_device *hdev, bool hard_reset);
      int (*suspend)(struct hl_device *hdev);
      int (*resume)(struct hl_device *hdev);
      int (*mmap)(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
@@ -312,6 +322,7 @@ void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
  * @cpu_accessible_dma_mem: KMD <-> ArmCP shared memory CPU address.
  * @cpu_accessible_dma_address: KMD <-> ArmCP shared memory DMA address.
  * @cpu_accessible_dma_pool: KMD <-> ArmCP shared memory pool.
+ * @spl_fw: image to load to ArmCP.
  * @asid_bitmap: holds used/available ASIDs.
  * @asid_mutex: protects asid_bitmap.
  * @device_open: lock for sanity checks upon FD open.
@@ -340,6 +351,7 @@ struct hl_device {
      void                            *cpu_accessible_dma_mem;
      dma_addr_t                      cpu_accessible_dma_address;
      struct gen_pool                 *cpu_accessible_dma_pool;
+     const struct firmware           *spl_fw;
      unsigned long                   *asid_bitmap;
      struct mutex                    asid_mutex;
      /* TODO: change to rw_sem for multiple contexts (same as other IOCTL) */
@@ -359,7 +371,11 @@ struct hl_device {
      u8                              disabled;
 
      /* Parameters for bring-up */
+     u8                              cpu_enable;
      u8                              reset_pcilink;
+     u8                              config_pll;
+     u8                              fw_loading;
+     u8                              pldm;
 };
 
 /*
drivers/misc/habanalabs/habanalabs_drv.c +0 -0
index 5c312dd3aa50..bd80683118d3 100644
@@ -181,7 +181,15 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
      hdev->major = hl_major;
 
Quoted 14 lines

drivers/misc/habanalabs/habanalabs_drv.c int create_hdev(struct hl_device **dev, struct pci_dev *pdev, · line 192

Quoted 10 lines
drivers/misc/habanalabs/habanalabs_drv.c +8 -0
      /* Parameters for bring-up - set them to defaults */
+     hdev->cpu_enable = 1;
      hdev->reset_pcilink = 0;
+     hdev->config_pll = 0;
+     hdev->fw_loading = 1;
+     hdev->pldm = 0;
+
+     /* If CPU is disabled, no point in loading FW */
+     if (!hdev->cpu_enable)
+             hdev->fw_loading = 0;

The CPU was enabled just a couple of lines above, wasn't it? I've noticed there are a lot of checks for hdev->cpu_enabled and hdev->pldm but I didn't see them ever change.

Nope, CPU is enabled in goya_hw_init.

All the parameters that are in hl_device under the /* Parameters for bring-up */ comment are hard-coded in the upstream version. If I will need to remove them completely from the code it would make my life harder when trying to bring code from our internal driver to the open source one.
I removed most of that code but some of them I left as they have minimal "signature".
These parameters are actuall kernel mode parameters in our internal driver but here I hard-code them to the correct values.

Quoted 16 lines
Quoted 16 lines
drivers/misc/habanalabs/habanalabs_drv.c +0 -0
      hdev->disabled = true;
      hdev->pdev = pdev; /* can be NULL in case of simulator device */
drivers/misc/habanalabs/include/goya/goya.h +1 -0
index 192a1450cbb1..2d0efb7b44bb 100644
@@ -11,6 +11,7 @@
 #define GOYA_H
 
 #include "asic_reg/goya_regs.h"
+#include "goya_async_events.h"
 
 #include <linux/types.h>
 
Quoted 8 lines

drivers/misc/habanalabs/include/goya/goya_async_events.h

Quoted 5 lines
drivers/misc/habanalabs/include/goya/goya_async_events.h new file +0 -0
new file mode 100644
index 000000000000..497937a17ee9

This, apparently, should have been a part of patch 8 (habanalabs: add event queue and interrupts)

Fixed

Quoted 234 lines
Quoted 227 lines
drivers/misc/habanalabs/include/goya/goya_async_events.h new file +186 -0
@@ -0,0 +1,186 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ */
+
+#ifndef __GOYA_ASYNC_EVENTS_H_
+#define __GOYA_ASYNC_EVENTS_H_
+
+enum goya_async_event_id {
+     GOYA_ASYNC_EVENT_ID_PCIE_IF = 33,
+     GOYA_ASYNC_EVENT_ID_TPC0_ECC = 36,
+     GOYA_ASYNC_EVENT_ID_TPC1_ECC = 39,
+     GOYA_ASYNC_EVENT_ID_TPC2_ECC = 42,
+     GOYA_ASYNC_EVENT_ID_TPC3_ECC = 45,
+     GOYA_ASYNC_EVENT_ID_TPC4_ECC = 48,
+     GOYA_ASYNC_EVENT_ID_TPC5_ECC = 51,
+     GOYA_ASYNC_EVENT_ID_TPC6_ECC = 54,
+     GOYA_ASYNC_EVENT_ID_TPC7_ECC = 57,
+     GOYA_ASYNC_EVENT_ID_MME_ECC = 60,
+     GOYA_ASYNC_EVENT_ID_MME_ECC_EXT = 61,
+     GOYA_ASYNC_EVENT_ID_MMU_ECC = 63,
+     GOYA_ASYNC_EVENT_ID_DMA_MACRO = 64,
+     GOYA_ASYNC_EVENT_ID_DMA_ECC = 66,
+     GOYA_ASYNC_EVENT_ID_CPU_IF_ECC = 75,
+     GOYA_ASYNC_EVENT_ID_PSOC_MEM = 78,
+     GOYA_ASYNC_EVENT_ID_PSOC_CORESIGHT = 79,
+     GOYA_ASYNC_EVENT_ID_SRAM0 = 81,
+     GOYA_ASYNC_EVENT_ID_SRAM1 = 82,
+     GOYA_ASYNC_EVENT_ID_SRAM2 = 83,
+     GOYA_ASYNC_EVENT_ID_SRAM3 = 84,
+     GOYA_ASYNC_EVENT_ID_SRAM4 = 85,
+     GOYA_ASYNC_EVENT_ID_SRAM5 = 86,
+     GOYA_ASYNC_EVENT_ID_SRAM6 = 87,
+     GOYA_ASYNC_EVENT_ID_SRAM7 = 88,
+     GOYA_ASYNC_EVENT_ID_SRAM8 = 89,
+     GOYA_ASYNC_EVENT_ID_SRAM9 = 90,
+     GOYA_ASYNC_EVENT_ID_SRAM10 = 91,
+     GOYA_ASYNC_EVENT_ID_SRAM11 = 92,
+     GOYA_ASYNC_EVENT_ID_SRAM12 = 93,
+     GOYA_ASYNC_EVENT_ID_SRAM13 = 94,
+     GOYA_ASYNC_EVENT_ID_SRAM14 = 95,
+     GOYA_ASYNC_EVENT_ID_SRAM15 = 96,
+     GOYA_ASYNC_EVENT_ID_SRAM16 = 97,
+     GOYA_ASYNC_EVENT_ID_SRAM17 = 98,
+     GOYA_ASYNC_EVENT_ID_SRAM18 = 99,
+     GOYA_ASYNC_EVENT_ID_SRAM19 = 100,
+     GOYA_ASYNC_EVENT_ID_SRAM20 = 101,
+     GOYA_ASYNC_EVENT_ID_SRAM21 = 102,
+     GOYA_ASYNC_EVENT_ID_SRAM22 = 103,
+     GOYA_ASYNC_EVENT_ID_SRAM23 = 104,
+     GOYA_ASYNC_EVENT_ID_SRAM24 = 105,
+     GOYA_ASYNC_EVENT_ID_SRAM25 = 106,
+     GOYA_ASYNC_EVENT_ID_SRAM26 = 107,
+     GOYA_ASYNC_EVENT_ID_SRAM27 = 108,
+     GOYA_ASYNC_EVENT_ID_SRAM28 = 109,
+     GOYA_ASYNC_EVENT_ID_SRAM29 = 110,
+     GOYA_ASYNC_EVENT_ID_GIC500 = 112,
+     GOYA_ASYNC_EVENT_ID_PCIE_DEC = 115,
+     GOYA_ASYNC_EVENT_ID_TPC0_DEC = 117,
+     GOYA_ASYNC_EVENT_ID_TPC1_DEC = 120,
+     GOYA_ASYNC_EVENT_ID_TPC2_DEC = 123,
+     GOYA_ASYNC_EVENT_ID_TPC3_DEC = 126,
+     GOYA_ASYNC_EVENT_ID_TPC4_DEC = 129,
+     GOYA_ASYNC_EVENT_ID_TPC5_DEC = 132,
+     GOYA_ASYNC_EVENT_ID_TPC6_DEC = 135,
+     GOYA_ASYNC_EVENT_ID_TPC7_DEC = 138,
+     GOYA_ASYNC_EVENT_ID_AXI_ECC = 139,
+     GOYA_ASYNC_EVENT_ID_L2_RAM_ECC = 140,
+     GOYA_ASYNC_EVENT_ID_MME_WACS = 141,
+     GOYA_ASYNC_EVENT_ID_MME_WACSD = 142,
+     GOYA_ASYNC_EVENT_ID_PLL0 = 143,
+     GOYA_ASYNC_EVENT_ID_PLL1 = 144,
+     GOYA_ASYNC_EVENT_ID_PLL3 = 146,
+     GOYA_ASYNC_EVENT_ID_PLL4 = 147,
+     GOYA_ASYNC_EVENT_ID_PLL5 = 148,
+     GOYA_ASYNC_EVENT_ID_PLL6 = 149,
+     GOYA_ASYNC_EVENT_ID_CPU_AXI_SPLITTER = 155,
+     GOYA_ASYNC_EVENT_ID_PSOC_AXI_DEC = 159,
+     GOYA_ASYNC_EVENT_ID_PSOC = 160,
+     GOYA_ASYNC_EVENT_ID_PCIE_FLR = 171,
+     GOYA_ASYNC_EVENT_ID_PCIE_HOT_RESET = 172,
+     GOYA_ASYNC_EVENT_ID_PCIE_QID0_ENG0 = 174,
+     GOYA_ASYNC_EVENT_ID_PCIE_QID0_ENG1 = 175,
+     GOYA_ASYNC_EVENT_ID_PCIE_QID0_ENG2 = 176,
+     GOYA_ASYNC_EVENT_ID_PCIE_QID0_ENG3 = 177,
+     GOYA_ASYNC_EVENT_ID_PCIE_QID1_ENG0 = 178,
+     GOYA_ASYNC_EVENT_ID_PCIE_QID1_ENG1 = 179,
+     GOYA_ASYNC_EVENT_ID_PCIE_QID1_ENG2 = 180,
+     GOYA_ASYNC_EVENT_ID_PCIE_QID1_ENG3 = 181,
+     GOYA_ASYNC_EVENT_ID_PCIE_APB = 182,
+     GOYA_ASYNC_EVENT_ID_PCIE_QDB = 183,
+     GOYA_ASYNC_EVENT_ID_PCIE_BM_D_P_WR = 184,
+     GOYA_ASYNC_EVENT_ID_PCIE_BM_D_RD = 185,
+     GOYA_ASYNC_EVENT_ID_PCIE_BM_U_P_WR = 186,
+     GOYA_ASYNC_EVENT_ID_PCIE_BM_U_RD = 187,
+     GOYA_ASYNC_EVENT_ID_TPC0_BMON_SPMU = 190,
+     GOYA_ASYNC_EVENT_ID_TPC0_KRN_ERR = 191,
+     GOYA_ASYNC_EVENT_ID_TPC1_BMON_SPMU = 200,
+     GOYA_ASYNC_EVENT_ID_TPC1_KRN_ERR = 201,
+     GOYA_ASYNC_EVENT_ID_TPC2_BMON_SPMU = 210,
+     GOYA_ASYNC_EVENT_ID_TPC2_KRN_ERR = 211,
+     GOYA_ASYNC_EVENT_ID_TPC3_BMON_SPMU = 220,
+     GOYA_ASYNC_EVENT_ID_TPC3_KRN_ERR = 221,
+     GOYA_ASYNC_EVENT_ID_TPC4_BMON_SPMU = 230,
+     GOYA_ASYNC_EVENT_ID_TPC4_KRN_ERR = 231,
+     GOYA_ASYNC_EVENT_ID_TPC5_BMON_SPMU = 240,
+     GOYA_ASYNC_EVENT_ID_TPC5_KRN_ERR = 241,
+     GOYA_ASYNC_EVENT_ID_TPC6_BMON_SPMU = 250,
+     GOYA_ASYNC_EVENT_ID_TPC6_KRN_ERR = 251,
+     GOYA_ASYNC_EVENT_ID_TPC7_BMON_SPMU = 260,
+     GOYA_ASYNC_EVENT_ID_TPC7_KRN_ERR = 261,
+     GOYA_ASYNC_EVENT_ID_MMU_SBA_SPMU0 = 270,
+     GOYA_ASYNC_EVENT_ID_MMU_SBA_SPMU1 = 271,
+     GOYA_ASYNC_EVENT_ID_MME_WACS_UP = 272,
+     GOYA_ASYNC_EVENT_ID_MME_WACS_DOWN = 273,
+     GOYA_ASYNC_EVENT_ID_MMU_PAGE_FAULT = 280,
+     GOYA_ASYNC_EVENT_ID_MMU_WR_PERM = 281,
+     GOYA_ASYNC_EVENT_ID_MMU_DBG_BM = 282,
+     GOYA_ASYNC_EVENT_ID_DMA_BM_CH0 = 290,
+     GOYA_ASYNC_EVENT_ID_DMA_BM_CH1 = 291,
+     GOYA_ASYNC_EVENT_ID_DMA_BM_CH2 = 292,
+     GOYA_ASYNC_EVENT_ID_DMA_BM_CH3 = 293,
+     GOYA_ASYNC_EVENT_ID_DMA_BM_CH4 = 294,
+     GOYA_ASYNC_EVENT_ID_DDR0_PHY_DFI = 300,
+     GOYA_ASYNC_EVENT_ID_DDR0_ECC_SCRUB = 301,
+     GOYA_ASYNC_EVENT_ID_DDR0_DB_ECC = 302,
+     GOYA_ASYNC_EVENT_ID_DDR0_SB_ECC = 303,
+     GOYA_ASYNC_EVENT_ID_DDR0_SB_ECC_MC = 304,
+     GOYA_ASYNC_EVENT_ID_DDR0_AXI_RD = 305,
+     GOYA_ASYNC_EVENT_ID_DDR0_AXI_WR = 306,
+     GOYA_ASYNC_EVENT_ID_DDR1_PHY_DFI = 310,
+     GOYA_ASYNC_EVENT_ID_DDR1_ECC_SCRUB = 311,
+     GOYA_ASYNC_EVENT_ID_DDR1_DB_ECC = 312,
+     GOYA_ASYNC_EVENT_ID_DDR1_SB_ECC = 313,
+     GOYA_ASYNC_EVENT_ID_DDR1_SB_ECC_MC = 314,
+     GOYA_ASYNC_EVENT_ID_DDR1_AXI_RD = 315,
+     GOYA_ASYNC_EVENT_ID_DDR1_AXI_WR = 316,
+     GOYA_ASYNC_EVENT_ID_CPU_BMON = 320,
+     GOYA_ASYNC_EVENT_ID_TS_EAST = 322,
+     GOYA_ASYNC_EVENT_ID_TS_WEST = 323,
+     GOYA_ASYNC_EVENT_ID_TS_NORTH = 324,
+     GOYA_ASYNC_EVENT_ID_PSOC_GPIO_U16_0 = 330,
+     GOYA_ASYNC_EVENT_ID_PSOC_GPIO_U16_1 = 331,
+     GOYA_ASYNC_EVENT_ID_PSOC_GPIO_U16_2 = 332,
+     GOYA_ASYNC_EVENT_ID_PSOC_GPIO_05_SW_RESET = 356,
+     GOYA_ASYNC_EVENT_ID_PSOC_GPIO_10_VRHOT_ICRIT = 361,
+     GOYA_ASYNC_EVENT_ID_TPC0_CMDQ = 430,
+     GOYA_ASYNC_EVENT_ID_TPC1_CMDQ = 431,
+     GOYA_ASYNC_EVENT_ID_TPC2_CMDQ = 432,
+     GOYA_ASYNC_EVENT_ID_TPC3_CMDQ = 433,
+     GOYA_ASYNC_EVENT_ID_TPC4_CMDQ = 434,
+     GOYA_ASYNC_EVENT_ID_TPC5_CMDQ = 435,
+     GOYA_ASYNC_EVENT_ID_TPC6_CMDQ = 436,
+     GOYA_ASYNC_EVENT_ID_TPC7_CMDQ = 437,
+     GOYA_ASYNC_EVENT_ID_TPC0_QM = 438,
+     GOYA_ASYNC_EVENT_ID_TPC1_QM = 439,
+     GOYA_ASYNC_EVENT_ID_TPC2_QM = 440,
+     GOYA_ASYNC_EVENT_ID_TPC3_QM = 441,
+     GOYA_ASYNC_EVENT_ID_TPC4_QM = 442,
+     GOYA_ASYNC_EVENT_ID_TPC5_QM = 443,
+     GOYA_ASYNC_EVENT_ID_TPC6_QM = 444,
+     GOYA_ASYNC_EVENT_ID_TPC7_QM = 445,
+     GOYA_ASYNC_EVENT_ID_MME_QM = 447,
+     GOYA_ASYNC_EVENT_ID_MME_CMDQ = 448,
+     GOYA_ASYNC_EVENT_ID_DMA0_QM = 449,
+     GOYA_ASYNC_EVENT_ID_DMA1_QM = 450,
+     GOYA_ASYNC_EVENT_ID_DMA2_QM = 451,
+     GOYA_ASYNC_EVENT_ID_DMA3_QM = 452,
+     GOYA_ASYNC_EVENT_ID_DMA4_QM = 453,
+     GOYA_ASYNC_EVENT_ID_DMA_ON_HBW = 454,
+     GOYA_ASYNC_EVENT_ID_DMA0_CH = 455,
+     GOYA_ASYNC_EVENT_ID_DMA1_CH = 456,
+     GOYA_ASYNC_EVENT_ID_DMA2_CH = 457,
+     GOYA_ASYNC_EVENT_ID_DMA3_CH = 458,
+     GOYA_ASYNC_EVENT_ID_DMA4_CH = 459,
+     GOYA_ASYNC_EVENT_ID_PI_UPDATE = 484,
+     GOYA_ASYNC_EVENT_ID_HALT_MACHINE = 485,
+     GOYA_ASYNC_EVENT_ID_INTS_REGISTER = 486,
+     GOYA_ASYNC_EVENT_ID_SOFT_RESET = 487,
+     GOYA_ASYNC_EVENT_ID_LAST_VALID_ID = 1023,
+     GOYA_ASYNC_EVENT_ID_SIZE
+};
+
+#endif /* __GOYA_ASYNC_EVENTS_H_ */
drivers/misc/habanalabs/include/goya/goya_boot_if.h new file +32 -1
new file mode 100644
index 000000000000..2e39578ec795
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright 2018 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ *
+ * Author: Oded Gabbay <[email protected]>
+ *
+ */
+
+#ifndef GOYA_BOOT_IF_H
+#define GOYA_BOOT_IF_H
+
+enum cpu_boot_status {
+     CPU_BOOT_STATUS_NA = 0,         /* Default value after reset of chip */
+     CPU_BOOT_STATUS_IN_WFE,
+     CPU_BOOT_STATUS_DRAM_RDY,
+     CPU_BOOT_STATUS_SRAM_AVAIL,
+     CPU_BOOT_STATUS_IN_BTL,         /* BTL is H/W FSM */
+     CPU_BOOT_STATUS_IN_PREBOOT,
+     CPU_BOOT_STATUS_IN_SPL,
+     CPU_BOOT_STATUS_IN_UBOOT,
+     CPU_BOOT_STATUS_DRAM_INIT_FAIL,
+     CPU_BOOT_STATUS_FIT_CORRUPTED
+};
+
+enum kmd_msg {
+     KMD_MSG_NA = 0,
+     KMD_MSG_GOTO_WFE,
+     KMD_MSG_FIT_RDY
+};
+
+#endif /* GOYA_BOOT_IF_H */
--
Quoted 2 lines

2.17.1

--
Sincerely yours,
Mike.

#32 lore raw

PATCH 7/15 habanalabs: add h/w queues module

In reply to Oded Gabbay

This patch adds the H/W queues module and the code to initialize Goya's various compute and DMA engines and their queues.

Goya has 5 DMA channels, 8 TPC engines and a single MME engine. For each channel/engine, there is a H/W queue logic which is used to pass commands from the user to the H/W. That logic is called QMAN.

There are two types of QMANs: external and internal. The DMA QMANs are considered external while the TPC and MME QMANs are considered internal. For each external queue there is a completion queue, which is located on the Host memory.

The differences between external and internal QMANs are:

1. The location of the queue's memory. External QMANs are located on the Host memory while internal QMANs are located on the on-chip memory.

2. The external QMAN write an entry to a completion queue and sends an MSI-X interrupt upon completion of a command buffer that was given to it. The internal QMAN doesn't do that.

---
 drivers/misc/habanalabs/Makefile              |    2 +-
 drivers/misc/habanalabs/device.c              |   74 +-
 drivers/misc/habanalabs/goya/goya.c           | 1518 +++++++++++++++--
 drivers/misc/habanalabs/goya/goyaP.h          |    6 +
 drivers/misc/habanalabs/habanalabs.h          |  176 +-
 drivers/misc/habanalabs/habanalabs_drv.c      |    6 +
 drivers/misc/habanalabs/hw_queue.c            |  404 +++++
 .../habanalabs/include/goya/goya_packets.h    |  234 +++
 .../habanalabs/include/habanalabs_device_if.h |  272 +++
 drivers/misc/habanalabs/irq.c                 |  150 ++
 10 files changed, 2721 insertions(+), 121 deletions(-)
 create mode 100644 drivers/misc/habanalabs/hw_queue.c
 create mode 100644 drivers/misc/habanalabs/include/goya/goya_packets.h
 create mode 100644 drivers/misc/habanalabs/irq.c
drivers/misc/habanalabs/Makefile +1 -1
index 2530c9b78ca4..c07f3ccb57dc 100644
@@ -5,7 +5,7 @@
 obj-m	:= habanalabs.o
 
 habanalabs-y := habanalabs_drv.o device.o context.o asid.o habanalabs_ioctl.o \
-		command_buffer.o
+		command_buffer.o hw_queue.o irq.o
 
 include $(src)/goya/Makefile
 habanalabs-y += $(HL_GOYA_FILES)
drivers/misc/habanalabs/device.c +72 -2

136 lines not on this page · raw message

drivers/misc/habanalabs/goya/goya.c +1401 -117

1,635 lines not on this page · raw message

drivers/misc/habanalabs/goya/goyaP.h +6 -0
index 45a6d2ca2752..598a718d3df1 100644
@@ -9,6 +9,7 @@
 #define GOYAP_H_
 
 #include "habanalabs.h"
+#include "include/goya/goya_packets.h"
 #include "include/goya/goya_boot_if.h"
 #include "include/goya/goya.h"
 
@@ -117,12 +118,17 @@ enum goya_fw_component {
 };
 
 struct goya_device {
+	int (*test_cpu_queue)(struct hl_device *hdev);
+
 	/* TODO: remove hw_queues_lock after moving to scheduler code */
 	spinlock_t	hw_queues_lock;
 	u64		ddr_bar_cur_addr;
 	u32		hw_cap_initialized;
 };
 
+int goya_test_cpu_queue(struct hl_device *hdev);
+int goya_send_cpu_message(struct hl_device *hdev, u32 *msg, u16 len,
+				u32 timeout, long *result);
 void goya_init_security(struct hl_device *hdev);
 
 #endif /* GOYAP_H_ */
drivers/misc/habanalabs/habanalabs.h +175 -1

262 lines not on this page · raw message

drivers/misc/habanalabs/habanalabs_drv.c +6 -0
index bd80683118d3..b64f58ad0f5d 100644
@@ -184,13 +184,19 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
 	hdev->cpu_enable = 1;
 	hdev->reset_pcilink = 0;
 	hdev->config_pll = 0;
+	hdev->cpu_queues_enable = 1;
 	hdev->fw_loading = 1;
+	hdev->ifh = 0;
 	hdev->pldm = 0;
 
 	/* If CPU is disabled, no point in loading FW */
 	if (!hdev->cpu_enable)
 		hdev->fw_loading = 0;
 
+	/* If we don't load FW, no need to initialize CPU queues */
+	if (!hdev->fw_loading)
+		hdev->cpu_queues_enable = 0;
+
 	hdev->disabled = true;
 	hdev->pdev = pdev; /* can be NULL in case of simulator device */
 
drivers/misc/habanalabs/hw_queue.c new file +404 -0

404 lines not on this page · raw message

drivers/misc/habanalabs/include/goya/goya_packets.h new file +234 -0

234 lines not on this page · raw message

drivers/misc/habanalabs/include/habanalabs_device_if.h +272 -0

278 lines not on this page · raw message

drivers/misc/habanalabs/irq.c new file +150 -0

150 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.17.1

Part of the diff is not shown here. View the raw message.