^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) Rockchip Electronics Co.Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author: Felix Zeng <felix.zeng@rock-chips.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #ifndef __LINUX_RKNPU_DRV_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define __LINUX_RKNPU_DRV_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #ifndef FPGA_PLATFORM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <soc/rockchip/rockchip_opp_select.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "rknpu_job.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "rknpu_fence.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "rknpu_debugger.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "rknpu_mm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define DRIVER_NAME "rknpu"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define DRIVER_DESC "RKNPU driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define DRIVER_DATE "20220829"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define DRIVER_MAJOR 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define DRIVER_MINOR 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define DRIVER_PATCHLEVEL 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define LOG_TAG "RKNPU"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* sample interval: 1000ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define RKNPU_LOAD_INTERVAL 1000000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define LOG_INFO(fmt, args...) pr_info(LOG_TAG ": " fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #if KERNEL_VERSION(5, 5, 0) <= LINUX_VERSION_CODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define LOG_WARN(fmt, args...) pr_warn(LOG_TAG ": " fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define LOG_WARN(fmt, args...) pr_warning(LOG_TAG ": " fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define LOG_DEBUG(fmt, args...) pr_devel(LOG_TAG ": " fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define LOG_ERROR(fmt, args...) pr_err(LOG_TAG ": " fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define LOG_DEV_INFO(dev, fmt, args...) dev_info(dev, LOG_TAG ": " fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define LOG_DEV_WARN(dev, fmt, args...) dev_warn(dev, LOG_TAG ": " fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define LOG_DEV_DEBUG(dev, fmt, args...) dev_dbg(dev, LOG_TAG ": " fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define LOG_DEV_ERROR(dev, fmt, args...) dev_err(dev, LOG_TAG ": " fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct npu_reset_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) const char *srst_a_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) const char *srst_h_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct rknpu_config {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) __u32 bw_priority_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) __u32 bw_priority_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) __u64 dma_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) __u32 pc_data_amount_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) __u32 pc_task_number_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) __u32 pc_task_number_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) __u32 bw_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) const struct npu_irqs_data *irqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) const struct npu_reset_data *resets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int num_irqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int num_resets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct rknpu_timer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) __u32 busy_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) __u32 busy_time_record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct rknpu_subcore_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct list_head todo_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) wait_queue_head_t job_done_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct rknpu_job *job;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int64_t task_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct rknpu_timer timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * RKNPU device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * @base: IO mapped base address for device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * @dev: Device instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * @drm_dev: DRM device instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct rknpu_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) void __iomem *base[RKNPU_MAX_CORES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #ifdef CONFIG_ROCKCHIP_RKNPU_DRM_GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct drm_device *drm_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #ifdef CONFIG_ROCKCHIP_RKNPU_DMA_HEAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct miscdevice miscdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct rk_dma_heap *heap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) atomic_t sequence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) spinlock_t irq_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct mutex power_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct mutex reset_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct rknpu_subcore_data subcore_datas[RKNPU_MAX_CORES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) const struct rknpu_config *config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) void __iomem *bw_priority_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct rknpu_fence_context *fence_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) bool iommu_en;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct reset_control *srst_a[RKNPU_MAX_CORES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct reset_control *srst_h[RKNPU_MAX_CORES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct clk_bulk_data *clks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int num_clks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct regulator *vdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct regulator *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct monitor_dev_info *mdev_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct ipa_power_model_data *model_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct thermal_cooling_device *devfreq_cooling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct devfreq *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned long ondemand_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #ifndef FPGA_PLATFORM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct rockchip_opp_info opp_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) unsigned long current_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) unsigned long current_volt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int bypass_irq_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int bypass_soft_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) bool soft_reseting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct device *genpd_dev_npu0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct device *genpd_dev_npu1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct device *genpd_dev_npu2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) bool multiple_domains;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) atomic_t power_refcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) atomic_t cmdline_power_refcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct delayed_work power_off_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct workqueue_struct *power_off_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct rknpu_debugger debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct hrtimer timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) ktime_t kt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) phys_addr_t sram_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) phys_addr_t sram_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) uint32_t sram_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) void __iomem *sram_base_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct rknpu_mm *sram_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned long power_put_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int rknpu_power_get(struct rknpu_device *rknpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int rknpu_power_put(struct rknpu_device *rknpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #endif /* __LINUX_RKNPU_DRV_H_ */