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