xref: /openbmc/linux/drivers/accel/habanalabs/common/habanalabs_ioctl.c (revision 1188f7f111c61394ec56beb8e30322305a8220b6)
1e65e175bSOded Gabbay // SPDX-License-Identifier: GPL-2.0
2e65e175bSOded Gabbay 
3e65e175bSOded Gabbay /*
4e65e175bSOded Gabbay  * Copyright 2016-2022 HabanaLabs, Ltd.
5e65e175bSOded Gabbay  * All Rights Reserved.
6e65e175bSOded Gabbay  */
7e65e175bSOded Gabbay 
8e65e175bSOded Gabbay #define pr_fmt(fmt)	"habanalabs: " fmt
9e65e175bSOded Gabbay 
10e65e175bSOded Gabbay #include <uapi/drm/habanalabs_accel.h>
11e65e175bSOded Gabbay #include "habanalabs.h"
12e65e175bSOded Gabbay 
13e65e175bSOded Gabbay #include <linux/fs.h>
14e65e175bSOded Gabbay #include <linux/kernel.h>
15e65e175bSOded Gabbay #include <linux/pci.h>
16e65e175bSOded Gabbay #include <linux/slab.h>
17e65e175bSOded Gabbay #include <linux/uaccess.h>
18e65e175bSOded Gabbay #include <linux/vmalloc.h>
19e65e175bSOded Gabbay 
20e65e175bSOded Gabbay static u32 hl_debug_struct_size[HL_DEBUG_OP_TIMESTAMP + 1] = {
21e65e175bSOded Gabbay 	[HL_DEBUG_OP_ETR] = sizeof(struct hl_debug_params_etr),
22e65e175bSOded Gabbay 	[HL_DEBUG_OP_ETF] = sizeof(struct hl_debug_params_etf),
23e65e175bSOded Gabbay 	[HL_DEBUG_OP_STM] = sizeof(struct hl_debug_params_stm),
24e65e175bSOded Gabbay 	[HL_DEBUG_OP_FUNNEL] = 0,
25e65e175bSOded Gabbay 	[HL_DEBUG_OP_BMON] = sizeof(struct hl_debug_params_bmon),
26e65e175bSOded Gabbay 	[HL_DEBUG_OP_SPMU] = sizeof(struct hl_debug_params_spmu),
27e65e175bSOded Gabbay 	[HL_DEBUG_OP_TIMESTAMP] = 0
28e65e175bSOded Gabbay 
29e65e175bSOded Gabbay };
30e65e175bSOded Gabbay 
device_status_info(struct hl_device * hdev,struct hl_info_args * args)31e65e175bSOded Gabbay static int device_status_info(struct hl_device *hdev, struct hl_info_args *args)
32e65e175bSOded Gabbay {
33e65e175bSOded Gabbay 	struct hl_info_device_status dev_stat = {0};
34e65e175bSOded Gabbay 	u32 size = args->return_size;
35e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
36e65e175bSOded Gabbay 
37e65e175bSOded Gabbay 	if ((!size) || (!out))
38e65e175bSOded Gabbay 		return -EINVAL;
39e65e175bSOded Gabbay 
40e65e175bSOded Gabbay 	dev_stat.status = hl_device_status(hdev);
41e65e175bSOded Gabbay 
42e65e175bSOded Gabbay 	return copy_to_user(out, &dev_stat,
43e65e175bSOded Gabbay 			min((size_t)size, sizeof(dev_stat))) ? -EFAULT : 0;
44e65e175bSOded Gabbay }
45e65e175bSOded Gabbay 
hw_ip_info(struct hl_device * hdev,struct hl_info_args * args)46e65e175bSOded Gabbay static int hw_ip_info(struct hl_device *hdev, struct hl_info_args *args)
47e65e175bSOded Gabbay {
48e65e175bSOded Gabbay 	struct hl_info_hw_ip_info hw_ip = {0};
49e65e175bSOded Gabbay 	u32 size = args->return_size;
50e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
51e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
52e65e175bSOded Gabbay 	u64 sram_kmd_size, dram_kmd_size, dram_available_size;
53e65e175bSOded Gabbay 
54e65e175bSOded Gabbay 	if ((!size) || (!out))
55e65e175bSOded Gabbay 		return -EINVAL;
56e65e175bSOded Gabbay 
57e65e175bSOded Gabbay 	sram_kmd_size = (prop->sram_user_base_address -
58e65e175bSOded Gabbay 				prop->sram_base_address);
59e65e175bSOded Gabbay 	dram_kmd_size = (prop->dram_user_base_address -
60e65e175bSOded Gabbay 				prop->dram_base_address);
61e65e175bSOded Gabbay 
62e65e175bSOded Gabbay 	hw_ip.device_id = hdev->asic_funcs->get_pci_id(hdev);
63e65e175bSOded Gabbay 	hw_ip.sram_base_address = prop->sram_user_base_address;
64e65e175bSOded Gabbay 	hw_ip.dram_base_address =
65583f12a8SOfir Bitton 			prop->dram_supports_virtual_memory ?
66e65e175bSOded Gabbay 			prop->dmmu.start_addr : prop->dram_user_base_address;
67e65e175bSOded Gabbay 	hw_ip.tpc_enabled_mask = prop->tpc_enabled_mask & 0xFF;
68e65e175bSOded Gabbay 	hw_ip.tpc_enabled_mask_ext = prop->tpc_enabled_mask;
69e65e175bSOded Gabbay 
70e65e175bSOded Gabbay 	hw_ip.sram_size = prop->sram_size - sram_kmd_size;
71e65e175bSOded Gabbay 
72e65e175bSOded Gabbay 	dram_available_size = prop->dram_size - dram_kmd_size;
73e65e175bSOded Gabbay 
74583f12a8SOfir Bitton 	hw_ip.dram_size = DIV_ROUND_DOWN_ULL(dram_available_size, prop->dram_page_size) *
75583f12a8SOfir Bitton 				prop->dram_page_size;
76e65e175bSOded Gabbay 
77e65e175bSOded Gabbay 	if (hw_ip.dram_size > PAGE_SIZE)
78e65e175bSOded Gabbay 		hw_ip.dram_enabled = 1;
79e65e175bSOded Gabbay 
80e65e175bSOded Gabbay 	hw_ip.dram_page_size = prop->dram_page_size;
81e65e175bSOded Gabbay 	hw_ip.device_mem_alloc_default_page_size = prop->device_mem_alloc_default_page_size;
82e65e175bSOded Gabbay 	hw_ip.num_of_events = prop->num_of_events;
83e65e175bSOded Gabbay 
84e65e175bSOded Gabbay 	memcpy(hw_ip.cpucp_version, prop->cpucp_info.cpucp_version,
85e65e175bSOded Gabbay 		min(VERSION_MAX_LEN, HL_INFO_VERSION_MAX_LEN));
86e65e175bSOded Gabbay 
87e65e175bSOded Gabbay 	memcpy(hw_ip.card_name, prop->cpucp_info.card_name,
88e65e175bSOded Gabbay 		min(CARD_NAME_MAX_LEN, HL_INFO_CARD_NAME_MAX_LEN));
89e65e175bSOded Gabbay 
90e65e175bSOded Gabbay 	hw_ip.cpld_version = le32_to_cpu(prop->cpucp_info.cpld_version);
91e65e175bSOded Gabbay 	hw_ip.module_id = le32_to_cpu(prop->cpucp_info.card_location);
92e65e175bSOded Gabbay 
93e65e175bSOded Gabbay 	hw_ip.psoc_pci_pll_nr = prop->psoc_pci_pll_nr;
94e65e175bSOded Gabbay 	hw_ip.psoc_pci_pll_nf = prop->psoc_pci_pll_nf;
95e65e175bSOded Gabbay 	hw_ip.psoc_pci_pll_od = prop->psoc_pci_pll_od;
96e65e175bSOded Gabbay 	hw_ip.psoc_pci_pll_div_factor = prop->psoc_pci_pll_div_factor;
97e65e175bSOded Gabbay 
98e65e175bSOded Gabbay 	hw_ip.decoder_enabled_mask = prop->decoder_enabled_mask;
99e65e175bSOded Gabbay 	hw_ip.mme_master_slave_mode = prop->mme_master_slave_mode;
100e65e175bSOded Gabbay 	hw_ip.first_available_interrupt_id = prop->first_available_user_interrupt;
101e65e175bSOded Gabbay 	hw_ip.number_of_user_interrupts = prop->user_interrupt_count;
1024713ace3SOfir Bitton 	hw_ip.tpc_interrupt_id = prop->tpc_interrupt_id;
103e65e175bSOded Gabbay 
104e65e175bSOded Gabbay 	hw_ip.edma_enabled_mask = prop->edma_enabled_mask;
105e65e175bSOded Gabbay 	hw_ip.server_type = prop->server_type;
106e65e175bSOded Gabbay 	hw_ip.security_enabled = prop->fw_security_enabled;
107e65e175bSOded Gabbay 	hw_ip.revision_id = hdev->pdev->revision;
108958e4797SOfir Bitton 	hw_ip.rotator_enabled_mask = prop->rotator_enabled_mask;
1097fc0d011SOfir Bitton 	hw_ip.engine_core_interrupt_reg_addr = prop->engine_core_interrupt_reg_addr;
11076e1ff37SOfir Bitton 	hw_ip.reserved_dram_size = dram_kmd_size;
111e65e175bSOded Gabbay 
112e65e175bSOded Gabbay 	return copy_to_user(out, &hw_ip,
113e65e175bSOded Gabbay 		min((size_t) size, sizeof(hw_ip))) ? -EFAULT : 0;
114e65e175bSOded Gabbay }
115e65e175bSOded Gabbay 
hw_events_info(struct hl_device * hdev,bool aggregate,struct hl_info_args * args)116e65e175bSOded Gabbay static int hw_events_info(struct hl_device *hdev, bool aggregate,
117e65e175bSOded Gabbay 			struct hl_info_args *args)
118e65e175bSOded Gabbay {
119e65e175bSOded Gabbay 	u32 size, max_size = args->return_size;
120e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
121e65e175bSOded Gabbay 	void *arr;
122e65e175bSOded Gabbay 
123e65e175bSOded Gabbay 	if ((!max_size) || (!out))
124e65e175bSOded Gabbay 		return -EINVAL;
125e65e175bSOded Gabbay 
126e65e175bSOded Gabbay 	arr = hdev->asic_funcs->get_events_stat(hdev, aggregate, &size);
127e65e175bSOded Gabbay 	if (!arr) {
128e65e175bSOded Gabbay 		dev_err(hdev->dev, "Events info not supported\n");
129e65e175bSOded Gabbay 		return -EOPNOTSUPP;
130e65e175bSOded Gabbay 	}
131e65e175bSOded Gabbay 
132e65e175bSOded Gabbay 	return copy_to_user(out, arr, min(max_size, size)) ? -EFAULT : 0;
133e65e175bSOded Gabbay }
134e65e175bSOded Gabbay 
events_info(struct hl_fpriv * hpriv,struct hl_info_args * args)135e65e175bSOded Gabbay static int events_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
136e65e175bSOded Gabbay {
137e65e175bSOded Gabbay 	u32 max_size = args->return_size;
138e65e175bSOded Gabbay 	u64 events_mask;
139e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
140e65e175bSOded Gabbay 
141e65e175bSOded Gabbay 	if ((max_size < sizeof(u64)) || (!out))
142e65e175bSOded Gabbay 		return -EINVAL;
143e65e175bSOded Gabbay 
144e65e175bSOded Gabbay 	mutex_lock(&hpriv->notifier_event.lock);
145e65e175bSOded Gabbay 	events_mask = hpriv->notifier_event.events_mask;
146e65e175bSOded Gabbay 	hpriv->notifier_event.events_mask = 0;
147e65e175bSOded Gabbay 	mutex_unlock(&hpriv->notifier_event.lock);
148e65e175bSOded Gabbay 
149e65e175bSOded Gabbay 	return copy_to_user(out, &events_mask, sizeof(u64)) ? -EFAULT : 0;
150e65e175bSOded Gabbay }
151e65e175bSOded Gabbay 
dram_usage_info(struct hl_fpriv * hpriv,struct hl_info_args * args)152e65e175bSOded Gabbay static int dram_usage_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
153e65e175bSOded Gabbay {
154e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
155e65e175bSOded Gabbay 	struct hl_info_dram_usage dram_usage = {0};
156e65e175bSOded Gabbay 	u32 max_size = args->return_size;
157e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
158e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
159e65e175bSOded Gabbay 	u64 dram_kmd_size;
160e65e175bSOded Gabbay 
161e65e175bSOded Gabbay 	if ((!max_size) || (!out))
162e65e175bSOded Gabbay 		return -EINVAL;
163e65e175bSOded Gabbay 
164e65e175bSOded Gabbay 	dram_kmd_size = (prop->dram_user_base_address -
165e65e175bSOded Gabbay 				prop->dram_base_address);
166e65e175bSOded Gabbay 	dram_usage.dram_free_mem = (prop->dram_size - dram_kmd_size) -
167e65e175bSOded Gabbay 					atomic64_read(&hdev->dram_used_mem);
168e65e175bSOded Gabbay 	if (hpriv->ctx)
169e65e175bSOded Gabbay 		dram_usage.ctx_dram_mem =
170e65e175bSOded Gabbay 			atomic64_read(&hpriv->ctx->dram_phys_mem);
171e65e175bSOded Gabbay 
172e65e175bSOded Gabbay 	return copy_to_user(out, &dram_usage,
173e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(dram_usage))) ? -EFAULT : 0;
174e65e175bSOded Gabbay }
175e65e175bSOded Gabbay 
hw_idle(struct hl_device * hdev,struct hl_info_args * args)176e65e175bSOded Gabbay static int hw_idle(struct hl_device *hdev, struct hl_info_args *args)
177e65e175bSOded Gabbay {
178e65e175bSOded Gabbay 	struct hl_info_hw_idle hw_idle = {0};
179e65e175bSOded Gabbay 	u32 max_size = args->return_size;
180e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
181e65e175bSOded Gabbay 
182e65e175bSOded Gabbay 	if ((!max_size) || (!out))
183e65e175bSOded Gabbay 		return -EINVAL;
184e65e175bSOded Gabbay 
185e65e175bSOded Gabbay 	hw_idle.is_idle = hdev->asic_funcs->is_device_idle(hdev,
186e65e175bSOded Gabbay 					hw_idle.busy_engines_mask_ext,
187e65e175bSOded Gabbay 					HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL);
188e65e175bSOded Gabbay 	hw_idle.busy_engines_mask =
189e65e175bSOded Gabbay 			lower_32_bits(hw_idle.busy_engines_mask_ext[0]);
190e65e175bSOded Gabbay 
191e65e175bSOded Gabbay 	return copy_to_user(out, &hw_idle,
192e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(hw_idle))) ? -EFAULT : 0;
193e65e175bSOded Gabbay }
194e65e175bSOded Gabbay 
debug_coresight(struct hl_device * hdev,struct hl_ctx * ctx,struct hl_debug_args * args)195e65e175bSOded Gabbay static int debug_coresight(struct hl_device *hdev, struct hl_ctx *ctx, struct hl_debug_args *args)
196e65e175bSOded Gabbay {
197e65e175bSOded Gabbay 	struct hl_debug_params *params;
198e65e175bSOded Gabbay 	void *input = NULL, *output = NULL;
199e65e175bSOded Gabbay 	int rc;
200e65e175bSOded Gabbay 
201e65e175bSOded Gabbay 	params = kzalloc(sizeof(*params), GFP_KERNEL);
202e65e175bSOded Gabbay 	if (!params)
203e65e175bSOded Gabbay 		return -ENOMEM;
204e65e175bSOded Gabbay 
205e65e175bSOded Gabbay 	params->reg_idx = args->reg_idx;
206e65e175bSOded Gabbay 	params->enable = args->enable;
207e65e175bSOded Gabbay 	params->op = args->op;
208e65e175bSOded Gabbay 
209e65e175bSOded Gabbay 	if (args->input_ptr && args->input_size) {
210e65e175bSOded Gabbay 		input = kzalloc(hl_debug_struct_size[args->op], GFP_KERNEL);
211e65e175bSOded Gabbay 		if (!input) {
212e65e175bSOded Gabbay 			rc = -ENOMEM;
213e65e175bSOded Gabbay 			goto out;
214e65e175bSOded Gabbay 		}
215e65e175bSOded Gabbay 
216e65e175bSOded Gabbay 		if (copy_from_user(input, u64_to_user_ptr(args->input_ptr),
217e65e175bSOded Gabbay 					args->input_size)) {
218e65e175bSOded Gabbay 			rc = -EFAULT;
219e65e175bSOded Gabbay 			dev_err(hdev->dev, "failed to copy input debug data\n");
220e65e175bSOded Gabbay 			goto out;
221e65e175bSOded Gabbay 		}
222e65e175bSOded Gabbay 
223e65e175bSOded Gabbay 		params->input = input;
224e65e175bSOded Gabbay 	}
225e65e175bSOded Gabbay 
226e65e175bSOded Gabbay 	if (args->output_ptr && args->output_size) {
227e65e175bSOded Gabbay 		output = kzalloc(args->output_size, GFP_KERNEL);
228e65e175bSOded Gabbay 		if (!output) {
229e65e175bSOded Gabbay 			rc = -ENOMEM;
230e65e175bSOded Gabbay 			goto out;
231e65e175bSOded Gabbay 		}
232e65e175bSOded Gabbay 
233e65e175bSOded Gabbay 		params->output = output;
234e65e175bSOded Gabbay 		params->output_size = args->output_size;
235e65e175bSOded Gabbay 	}
236e65e175bSOded Gabbay 
237e65e175bSOded Gabbay 	rc = hdev->asic_funcs->debug_coresight(hdev, ctx, params);
238e65e175bSOded Gabbay 	if (rc) {
239e65e175bSOded Gabbay 		dev_err(hdev->dev,
240e65e175bSOded Gabbay 			"debug coresight operation failed %d\n", rc);
241e65e175bSOded Gabbay 		goto out;
242e65e175bSOded Gabbay 	}
243e65e175bSOded Gabbay 
244e65e175bSOded Gabbay 	if (output && copy_to_user((void __user *) (uintptr_t) args->output_ptr,
245e65e175bSOded Gabbay 					output, args->output_size)) {
246e65e175bSOded Gabbay 		dev_err(hdev->dev, "copy to user failed in debug ioctl\n");
247e65e175bSOded Gabbay 		rc = -EFAULT;
248e65e175bSOded Gabbay 		goto out;
249e65e175bSOded Gabbay 	}
250e65e175bSOded Gabbay 
251e65e175bSOded Gabbay 
252e65e175bSOded Gabbay out:
253e65e175bSOded Gabbay 	kfree(params);
254e65e175bSOded Gabbay 	kfree(output);
255e65e175bSOded Gabbay 	kfree(input);
256e65e175bSOded Gabbay 
257e65e175bSOded Gabbay 	return rc;
258e65e175bSOded Gabbay }
259e65e175bSOded Gabbay 
device_utilization(struct hl_device * hdev,struct hl_info_args * args)260e65e175bSOded Gabbay static int device_utilization(struct hl_device *hdev, struct hl_info_args *args)
261e65e175bSOded Gabbay {
262e65e175bSOded Gabbay 	struct hl_info_device_utilization device_util = {0};
263e65e175bSOded Gabbay 	u32 max_size = args->return_size;
264e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
265e65e175bSOded Gabbay 	int rc;
266e65e175bSOded Gabbay 
267e65e175bSOded Gabbay 	if ((!max_size) || (!out))
268e65e175bSOded Gabbay 		return -EINVAL;
269e65e175bSOded Gabbay 
270e65e175bSOded Gabbay 	rc = hl_device_utilization(hdev, &device_util.utilization);
271e65e175bSOded Gabbay 	if (rc)
272e65e175bSOded Gabbay 		return -EINVAL;
273e65e175bSOded Gabbay 
274e65e175bSOded Gabbay 	return copy_to_user(out, &device_util,
275e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(device_util))) ? -EFAULT : 0;
276e65e175bSOded Gabbay }
277e65e175bSOded Gabbay 
get_clk_rate(struct hl_device * hdev,struct hl_info_args * args)278e65e175bSOded Gabbay static int get_clk_rate(struct hl_device *hdev, struct hl_info_args *args)
279e65e175bSOded Gabbay {
280e65e175bSOded Gabbay 	struct hl_info_clk_rate clk_rate = {0};
281e65e175bSOded Gabbay 	u32 max_size = args->return_size;
282e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
283e65e175bSOded Gabbay 	int rc;
284e65e175bSOded Gabbay 
285e65e175bSOded Gabbay 	if ((!max_size) || (!out))
286e65e175bSOded Gabbay 		return -EINVAL;
287e65e175bSOded Gabbay 
288e65e175bSOded Gabbay 	rc = hl_fw_get_clk_rate(hdev, &clk_rate.cur_clk_rate_mhz, &clk_rate.max_clk_rate_mhz);
289e65e175bSOded Gabbay 	if (rc)
290e65e175bSOded Gabbay 		return rc;
291e65e175bSOded Gabbay 
292e65e175bSOded Gabbay 	return copy_to_user(out, &clk_rate, min_t(size_t, max_size, sizeof(clk_rate)))
293e65e175bSOded Gabbay 										? -EFAULT : 0;
294e65e175bSOded Gabbay }
295e65e175bSOded Gabbay 
get_reset_count(struct hl_device * hdev,struct hl_info_args * args)296e65e175bSOded Gabbay static int get_reset_count(struct hl_device *hdev, struct hl_info_args *args)
297e65e175bSOded Gabbay {
298e65e175bSOded Gabbay 	struct hl_info_reset_count reset_count = {0};
299e65e175bSOded Gabbay 	u32 max_size = args->return_size;
300e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
301e65e175bSOded Gabbay 
302e65e175bSOded Gabbay 	if ((!max_size) || (!out))
303e65e175bSOded Gabbay 		return -EINVAL;
304e65e175bSOded Gabbay 
305e65e175bSOded Gabbay 	reset_count.hard_reset_cnt = hdev->reset_info.hard_reset_cnt;
306e65e175bSOded Gabbay 	reset_count.soft_reset_cnt = hdev->reset_info.compute_reset_cnt;
307e65e175bSOded Gabbay 
308e65e175bSOded Gabbay 	return copy_to_user(out, &reset_count,
309e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(reset_count))) ? -EFAULT : 0;
310e65e175bSOded Gabbay }
311e65e175bSOded Gabbay 
time_sync_info(struct hl_device * hdev,struct hl_info_args * args)312e65e175bSOded Gabbay static int time_sync_info(struct hl_device *hdev, struct hl_info_args *args)
313e65e175bSOded Gabbay {
314e65e175bSOded Gabbay 	struct hl_info_time_sync time_sync = {0};
315e65e175bSOded Gabbay 	u32 max_size = args->return_size;
316e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
317e65e175bSOded Gabbay 
318e65e175bSOded Gabbay 	if ((!max_size) || (!out))
319e65e175bSOded Gabbay 		return -EINVAL;
320e65e175bSOded Gabbay 
321e65e175bSOded Gabbay 	time_sync.device_time = hdev->asic_funcs->get_device_time(hdev);
322e65e175bSOded Gabbay 	time_sync.host_time = ktime_get_raw_ns();
323e65e175bSOded Gabbay 
324e65e175bSOded Gabbay 	return copy_to_user(out, &time_sync,
325e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(time_sync))) ? -EFAULT : 0;
326e65e175bSOded Gabbay }
327e65e175bSOded Gabbay 
pci_counters_info(struct hl_fpriv * hpriv,struct hl_info_args * args)328e65e175bSOded Gabbay static int pci_counters_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
329e65e175bSOded Gabbay {
330e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
331e65e175bSOded Gabbay 	struct hl_info_pci_counters pci_counters = {0};
332e65e175bSOded Gabbay 	u32 max_size = args->return_size;
333e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
334e65e175bSOded Gabbay 	int rc;
335e65e175bSOded Gabbay 
336e65e175bSOded Gabbay 	if ((!max_size) || (!out))
337e65e175bSOded Gabbay 		return -EINVAL;
338e65e175bSOded Gabbay 
339e65e175bSOded Gabbay 	rc = hl_fw_cpucp_pci_counters_get(hdev, &pci_counters);
340e65e175bSOded Gabbay 	if (rc)
341e65e175bSOded Gabbay 		return rc;
342e65e175bSOded Gabbay 
343e65e175bSOded Gabbay 	return copy_to_user(out, &pci_counters,
344e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(pci_counters))) ? -EFAULT : 0;
345e65e175bSOded Gabbay }
346e65e175bSOded Gabbay 
clk_throttle_info(struct hl_fpriv * hpriv,struct hl_info_args * args)347e65e175bSOded Gabbay static int clk_throttle_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
348e65e175bSOded Gabbay {
349e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
350e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
351e65e175bSOded Gabbay 	struct hl_info_clk_throttle clk_throttle = {0};
352e65e175bSOded Gabbay 	ktime_t end_time, zero_time = ktime_set(0, 0);
353e65e175bSOded Gabbay 	u32 max_size = args->return_size;
354e65e175bSOded Gabbay 	int i;
355e65e175bSOded Gabbay 
356e65e175bSOded Gabbay 	if ((!max_size) || (!out))
357e65e175bSOded Gabbay 		return -EINVAL;
358e65e175bSOded Gabbay 
359e65e175bSOded Gabbay 	mutex_lock(&hdev->clk_throttling.lock);
360e65e175bSOded Gabbay 
361e65e175bSOded Gabbay 	clk_throttle.clk_throttling_reason = hdev->clk_throttling.current_reason;
362e65e175bSOded Gabbay 
363e65e175bSOded Gabbay 	for (i = 0 ; i < HL_CLK_THROTTLE_TYPE_MAX ; i++) {
364e65e175bSOded Gabbay 		if (!(hdev->clk_throttling.aggregated_reason & BIT(i)))
365e65e175bSOded Gabbay 			continue;
366e65e175bSOded Gabbay 
367e65e175bSOded Gabbay 		clk_throttle.clk_throttling_timestamp_us[i] =
368e65e175bSOded Gabbay 			ktime_to_us(hdev->clk_throttling.timestamp[i].start);
369e65e175bSOded Gabbay 
370e65e175bSOded Gabbay 		if (ktime_compare(hdev->clk_throttling.timestamp[i].end, zero_time))
371e65e175bSOded Gabbay 			end_time = hdev->clk_throttling.timestamp[i].end;
372e65e175bSOded Gabbay 		else
373e65e175bSOded Gabbay 			end_time = ktime_get();
374e65e175bSOded Gabbay 
375e65e175bSOded Gabbay 		clk_throttle.clk_throttling_duration_ns[i] =
376e65e175bSOded Gabbay 			ktime_to_ns(ktime_sub(end_time,
377e65e175bSOded Gabbay 				hdev->clk_throttling.timestamp[i].start));
378e65e175bSOded Gabbay 
379e65e175bSOded Gabbay 	}
380e65e175bSOded Gabbay 	mutex_unlock(&hdev->clk_throttling.lock);
381e65e175bSOded Gabbay 
382e65e175bSOded Gabbay 	return copy_to_user(out, &clk_throttle,
383e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(clk_throttle))) ? -EFAULT : 0;
384e65e175bSOded Gabbay }
385e65e175bSOded Gabbay 
cs_counters_info(struct hl_fpriv * hpriv,struct hl_info_args * args)386e65e175bSOded Gabbay static int cs_counters_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
387e65e175bSOded Gabbay {
388e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
389e65e175bSOded Gabbay 	struct hl_info_cs_counters cs_counters = {0};
390e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
391e65e175bSOded Gabbay 	struct hl_cs_counters_atomic *cntr;
392e65e175bSOded Gabbay 	u32 max_size = args->return_size;
393e65e175bSOded Gabbay 
394e65e175bSOded Gabbay 	cntr = &hdev->aggregated_cs_counters;
395e65e175bSOded Gabbay 
396e65e175bSOded Gabbay 	if ((!max_size) || (!out))
397e65e175bSOded Gabbay 		return -EINVAL;
398e65e175bSOded Gabbay 
399e65e175bSOded Gabbay 	cs_counters.total_out_of_mem_drop_cnt =
400e65e175bSOded Gabbay 			atomic64_read(&cntr->out_of_mem_drop_cnt);
401e65e175bSOded Gabbay 	cs_counters.total_parsing_drop_cnt =
402e65e175bSOded Gabbay 			atomic64_read(&cntr->parsing_drop_cnt);
403e65e175bSOded Gabbay 	cs_counters.total_queue_full_drop_cnt =
404e65e175bSOded Gabbay 			atomic64_read(&cntr->queue_full_drop_cnt);
405e65e175bSOded Gabbay 	cs_counters.total_device_in_reset_drop_cnt =
406e65e175bSOded Gabbay 			atomic64_read(&cntr->device_in_reset_drop_cnt);
407e65e175bSOded Gabbay 	cs_counters.total_max_cs_in_flight_drop_cnt =
408e65e175bSOded Gabbay 			atomic64_read(&cntr->max_cs_in_flight_drop_cnt);
409e65e175bSOded Gabbay 	cs_counters.total_validation_drop_cnt =
410e65e175bSOded Gabbay 			atomic64_read(&cntr->validation_drop_cnt);
411e65e175bSOded Gabbay 
412e65e175bSOded Gabbay 	if (hpriv->ctx) {
413e65e175bSOded Gabbay 		cs_counters.ctx_out_of_mem_drop_cnt =
414e65e175bSOded Gabbay 				atomic64_read(
415e65e175bSOded Gabbay 				&hpriv->ctx->cs_counters.out_of_mem_drop_cnt);
416e65e175bSOded Gabbay 		cs_counters.ctx_parsing_drop_cnt =
417e65e175bSOded Gabbay 				atomic64_read(
418e65e175bSOded Gabbay 				&hpriv->ctx->cs_counters.parsing_drop_cnt);
419e65e175bSOded Gabbay 		cs_counters.ctx_queue_full_drop_cnt =
420e65e175bSOded Gabbay 				atomic64_read(
421e65e175bSOded Gabbay 				&hpriv->ctx->cs_counters.queue_full_drop_cnt);
422e65e175bSOded Gabbay 		cs_counters.ctx_device_in_reset_drop_cnt =
423e65e175bSOded Gabbay 				atomic64_read(
424e65e175bSOded Gabbay 			&hpriv->ctx->cs_counters.device_in_reset_drop_cnt);
425e65e175bSOded Gabbay 		cs_counters.ctx_max_cs_in_flight_drop_cnt =
426e65e175bSOded Gabbay 				atomic64_read(
427e65e175bSOded Gabbay 			&hpriv->ctx->cs_counters.max_cs_in_flight_drop_cnt);
428e65e175bSOded Gabbay 		cs_counters.ctx_validation_drop_cnt =
429e65e175bSOded Gabbay 				atomic64_read(
430e65e175bSOded Gabbay 				&hpriv->ctx->cs_counters.validation_drop_cnt);
431e65e175bSOded Gabbay 	}
432e65e175bSOded Gabbay 
433e65e175bSOded Gabbay 	return copy_to_user(out, &cs_counters,
434e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(cs_counters))) ? -EFAULT : 0;
435e65e175bSOded Gabbay }
436e65e175bSOded Gabbay 
sync_manager_info(struct hl_fpriv * hpriv,struct hl_info_args * args)437e65e175bSOded Gabbay static int sync_manager_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
438e65e175bSOded Gabbay {
439e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
440e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
441e65e175bSOded Gabbay 	struct hl_info_sync_manager sm_info = {0};
442e65e175bSOded Gabbay 	u32 max_size = args->return_size;
443e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
444e65e175bSOded Gabbay 
445e65e175bSOded Gabbay 	if ((!max_size) || (!out))
446e65e175bSOded Gabbay 		return -EINVAL;
447e65e175bSOded Gabbay 
448e65e175bSOded Gabbay 	if (args->dcore_id >= HL_MAX_DCORES)
449e65e175bSOded Gabbay 		return -EINVAL;
450e65e175bSOded Gabbay 
451e65e175bSOded Gabbay 	sm_info.first_available_sync_object =
452e65e175bSOded Gabbay 			prop->first_available_user_sob[args->dcore_id];
453e65e175bSOded Gabbay 	sm_info.first_available_monitor =
454e65e175bSOded Gabbay 			prop->first_available_user_mon[args->dcore_id];
455e65e175bSOded Gabbay 	sm_info.first_available_cq =
456e65e175bSOded Gabbay 			prop->first_available_cq[args->dcore_id];
457e65e175bSOded Gabbay 
458e65e175bSOded Gabbay 	return copy_to_user(out, &sm_info, min_t(size_t, (size_t) max_size,
459e65e175bSOded Gabbay 			sizeof(sm_info))) ? -EFAULT : 0;
460e65e175bSOded Gabbay }
461e65e175bSOded Gabbay 
total_energy_consumption_info(struct hl_fpriv * hpriv,struct hl_info_args * args)462e65e175bSOded Gabbay static int total_energy_consumption_info(struct hl_fpriv *hpriv,
463e65e175bSOded Gabbay 			struct hl_info_args *args)
464e65e175bSOded Gabbay {
465e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
466e65e175bSOded Gabbay 	struct hl_info_energy total_energy = {0};
467e65e175bSOded Gabbay 	u32 max_size = args->return_size;
468e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
469e65e175bSOded Gabbay 	int rc;
470e65e175bSOded Gabbay 
471e65e175bSOded Gabbay 	if ((!max_size) || (!out))
472e65e175bSOded Gabbay 		return -EINVAL;
473e65e175bSOded Gabbay 
474e65e175bSOded Gabbay 	rc = hl_fw_cpucp_total_energy_get(hdev,
475e65e175bSOded Gabbay 			&total_energy.total_energy_consumption);
476e65e175bSOded Gabbay 	if (rc)
477e65e175bSOded Gabbay 		return rc;
478e65e175bSOded Gabbay 
479e65e175bSOded Gabbay 	return copy_to_user(out, &total_energy,
480e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(total_energy))) ? -EFAULT : 0;
481e65e175bSOded Gabbay }
482e65e175bSOded Gabbay 
pll_frequency_info(struct hl_fpriv * hpriv,struct hl_info_args * args)483e65e175bSOded Gabbay static int pll_frequency_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
484e65e175bSOded Gabbay {
485e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
486e65e175bSOded Gabbay 	struct hl_pll_frequency_info freq_info = { {0} };
487e65e175bSOded Gabbay 	u32 max_size = args->return_size;
488e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
489e65e175bSOded Gabbay 	int rc;
490e65e175bSOded Gabbay 
491e65e175bSOded Gabbay 	if ((!max_size) || (!out))
492e65e175bSOded Gabbay 		return -EINVAL;
493e65e175bSOded Gabbay 
494e65e175bSOded Gabbay 	rc = hl_fw_cpucp_pll_info_get(hdev, args->pll_index, freq_info.output);
495e65e175bSOded Gabbay 	if (rc)
496e65e175bSOded Gabbay 		return rc;
497e65e175bSOded Gabbay 
498e65e175bSOded Gabbay 	return copy_to_user(out, &freq_info,
499e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(freq_info))) ? -EFAULT : 0;
500e65e175bSOded Gabbay }
501e65e175bSOded Gabbay 
power_info(struct hl_fpriv * hpriv,struct hl_info_args * args)502e65e175bSOded Gabbay static int power_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
503e65e175bSOded Gabbay {
504e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
505e65e175bSOded Gabbay 	u32 max_size = args->return_size;
506e65e175bSOded Gabbay 	struct hl_power_info power_info = {0};
507e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
508e65e175bSOded Gabbay 	int rc;
509e65e175bSOded Gabbay 
510e65e175bSOded Gabbay 	if ((!max_size) || (!out))
511e65e175bSOded Gabbay 		return -EINVAL;
512e65e175bSOded Gabbay 
513e65e175bSOded Gabbay 	rc = hl_fw_cpucp_power_get(hdev, &power_info.power);
514e65e175bSOded Gabbay 	if (rc)
515e65e175bSOded Gabbay 		return rc;
516e65e175bSOded Gabbay 
517e65e175bSOded Gabbay 	return copy_to_user(out, &power_info,
518e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(power_info))) ? -EFAULT : 0;
519e65e175bSOded Gabbay }
520e65e175bSOded Gabbay 
open_stats_info(struct hl_fpriv * hpriv,struct hl_info_args * args)521e65e175bSOded Gabbay static int open_stats_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
522e65e175bSOded Gabbay {
523e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
524e65e175bSOded Gabbay 	u32 max_size = args->return_size;
525e65e175bSOded Gabbay 	struct hl_open_stats_info open_stats_info = {0};
526e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
527e65e175bSOded Gabbay 
528e65e175bSOded Gabbay 	if ((!max_size) || (!out))
529e65e175bSOded Gabbay 		return -EINVAL;
530e65e175bSOded Gabbay 
531e65e175bSOded Gabbay 	open_stats_info.last_open_period_ms = jiffies64_to_msecs(
532e65e175bSOded Gabbay 		hdev->last_open_session_duration_jif);
533e65e175bSOded Gabbay 	open_stats_info.open_counter = hdev->open_counter;
534e65e175bSOded Gabbay 	open_stats_info.is_compute_ctx_active = hdev->is_compute_ctx_active;
535e65e175bSOded Gabbay 	open_stats_info.compute_ctx_in_release = hdev->compute_ctx_in_release;
536e65e175bSOded Gabbay 
537e65e175bSOded Gabbay 	return copy_to_user(out, &open_stats_info,
538e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(open_stats_info))) ? -EFAULT : 0;
539e65e175bSOded Gabbay }
540e65e175bSOded Gabbay 
dram_pending_rows_info(struct hl_fpriv * hpriv,struct hl_info_args * args)541e65e175bSOded Gabbay static int dram_pending_rows_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
542e65e175bSOded Gabbay {
543e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
544e65e175bSOded Gabbay 	u32 max_size = args->return_size;
545e65e175bSOded Gabbay 	u32 pend_rows_num = 0;
546e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
547e65e175bSOded Gabbay 	int rc;
548e65e175bSOded Gabbay 
549e65e175bSOded Gabbay 	if ((!max_size) || (!out))
550e65e175bSOded Gabbay 		return -EINVAL;
551e65e175bSOded Gabbay 
552e65e175bSOded Gabbay 	rc = hl_fw_dram_pending_row_get(hdev, &pend_rows_num);
553e65e175bSOded Gabbay 	if (rc)
554e65e175bSOded Gabbay 		return rc;
555e65e175bSOded Gabbay 
556e65e175bSOded Gabbay 	return copy_to_user(out, &pend_rows_num,
557e65e175bSOded Gabbay 			min_t(size_t, max_size, sizeof(pend_rows_num))) ? -EFAULT : 0;
558e65e175bSOded Gabbay }
559e65e175bSOded Gabbay 
dram_replaced_rows_info(struct hl_fpriv * hpriv,struct hl_info_args * args)560e65e175bSOded Gabbay static int dram_replaced_rows_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
561e65e175bSOded Gabbay {
562e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
563e65e175bSOded Gabbay 	u32 max_size = args->return_size;
564e65e175bSOded Gabbay 	struct cpucp_hbm_row_info info = {0};
565e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
566e65e175bSOded Gabbay 	int rc;
567e65e175bSOded Gabbay 
568e65e175bSOded Gabbay 	if ((!max_size) || (!out))
569e65e175bSOded Gabbay 		return -EINVAL;
570e65e175bSOded Gabbay 
571e65e175bSOded Gabbay 	rc = hl_fw_dram_replaced_row_get(hdev, &info);
572e65e175bSOded Gabbay 	if (rc)
573e65e175bSOded Gabbay 		return rc;
574e65e175bSOded Gabbay 
575e65e175bSOded Gabbay 	return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
576e65e175bSOded Gabbay }
577e65e175bSOded Gabbay 
last_err_open_dev_info(struct hl_fpriv * hpriv,struct hl_info_args * args)578e65e175bSOded Gabbay static int last_err_open_dev_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
579e65e175bSOded Gabbay {
580e65e175bSOded Gabbay 	struct hl_info_last_err_open_dev_time info = {0};
581e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
582e65e175bSOded Gabbay 	u32 max_size = args->return_size;
583e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
584e65e175bSOded Gabbay 
585e65e175bSOded Gabbay 	if ((!max_size) || (!out))
586e65e175bSOded Gabbay 		return -EINVAL;
587e65e175bSOded Gabbay 
588e65e175bSOded Gabbay 	info.timestamp = ktime_to_ns(hdev->last_successful_open_ktime);
589e65e175bSOded Gabbay 
590e65e175bSOded Gabbay 	return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
591e65e175bSOded Gabbay }
592e65e175bSOded Gabbay 
cs_timeout_info(struct hl_fpriv * hpriv,struct hl_info_args * args)593e65e175bSOded Gabbay static int cs_timeout_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
594e65e175bSOded Gabbay {
595e65e175bSOded Gabbay 	struct hl_info_cs_timeout_event info = {0};
596e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
597e65e175bSOded Gabbay 	u32 max_size = args->return_size;
598e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
599e65e175bSOded Gabbay 
600e65e175bSOded Gabbay 	if ((!max_size) || (!out))
601e65e175bSOded Gabbay 		return -EINVAL;
602e65e175bSOded Gabbay 
603e65e175bSOded Gabbay 	info.seq = hdev->captured_err_info.cs_timeout.seq;
604e65e175bSOded Gabbay 	info.timestamp = ktime_to_ns(hdev->captured_err_info.cs_timeout.timestamp);
605e65e175bSOded Gabbay 
606e65e175bSOded Gabbay 	return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
607e65e175bSOded Gabbay }
608e65e175bSOded Gabbay 
razwi_info(struct hl_fpriv * hpriv,struct hl_info_args * args)609e65e175bSOded Gabbay static int razwi_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
610e65e175bSOded Gabbay {
611571d1a72SKoby Elbaz 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
612e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
613e65e175bSOded Gabbay 	u32 max_size = args->return_size;
614571d1a72SKoby Elbaz 	struct razwi_info *razwi_info;
615e65e175bSOded Gabbay 
616e65e175bSOded Gabbay 	if ((!max_size) || (!out))
617e65e175bSOded Gabbay 		return -EINVAL;
618e65e175bSOded Gabbay 
619571d1a72SKoby Elbaz 	razwi_info = &hdev->captured_err_info.razwi_info;
620571d1a72SKoby Elbaz 	if (!razwi_info->razwi_info_available)
621571d1a72SKoby Elbaz 		return 0;
622571d1a72SKoby Elbaz 
623571d1a72SKoby Elbaz 	return copy_to_user(out, &razwi_info->razwi,
624571d1a72SKoby Elbaz 			min_t(size_t, max_size, sizeof(struct hl_info_razwi_event))) ? -EFAULT : 0;
625e65e175bSOded Gabbay }
626e65e175bSOded Gabbay 
undefined_opcode_info(struct hl_fpriv * hpriv,struct hl_info_args * args)627e65e175bSOded Gabbay static int undefined_opcode_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
628e65e175bSOded Gabbay {
629e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
630e65e175bSOded Gabbay 	u32 max_size = args->return_size;
631e65e175bSOded Gabbay 	struct hl_info_undefined_opcode_event info = {0};
632e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
633e65e175bSOded Gabbay 
634e65e175bSOded Gabbay 	if ((!max_size) || (!out))
635e65e175bSOded Gabbay 		return -EINVAL;
636e65e175bSOded Gabbay 
637e65e175bSOded Gabbay 	info.timestamp = ktime_to_ns(hdev->captured_err_info.undef_opcode.timestamp);
638e65e175bSOded Gabbay 	info.engine_id = hdev->captured_err_info.undef_opcode.engine_id;
639e65e175bSOded Gabbay 	info.cq_addr = hdev->captured_err_info.undef_opcode.cq_addr;
640e65e175bSOded Gabbay 	info.cq_size = hdev->captured_err_info.undef_opcode.cq_size;
641e65e175bSOded Gabbay 	info.stream_id = hdev->captured_err_info.undef_opcode.stream_id;
642e65e175bSOded Gabbay 	info.cb_addr_streams_len = hdev->captured_err_info.undef_opcode.cb_addr_streams_len;
643e65e175bSOded Gabbay 	memcpy(info.cb_addr_streams, hdev->captured_err_info.undef_opcode.cb_addr_streams,
644e65e175bSOded Gabbay 			sizeof(info.cb_addr_streams));
645e65e175bSOded Gabbay 
646e65e175bSOded Gabbay 	return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
647e65e175bSOded Gabbay }
648e65e175bSOded Gabbay 
dev_mem_alloc_page_sizes_info(struct hl_fpriv * hpriv,struct hl_info_args * args)649e65e175bSOded Gabbay static int dev_mem_alloc_page_sizes_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
650e65e175bSOded Gabbay {
651e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
652e65e175bSOded Gabbay 	struct hl_info_dev_memalloc_page_sizes info = {0};
653e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
654e65e175bSOded Gabbay 	u32 max_size = args->return_size;
655e65e175bSOded Gabbay 
656e65e175bSOded Gabbay 	if ((!max_size) || (!out))
657e65e175bSOded Gabbay 		return -EINVAL;
658e65e175bSOded Gabbay 
659e65e175bSOded Gabbay 	/*
660e65e175bSOded Gabbay 	 * Future ASICs that will support multiple DRAM page sizes will support only "powers of 2"
661e65e175bSOded Gabbay 	 * pages (unlike some of the ASICs before supporting multiple page sizes).
662e65e175bSOded Gabbay 	 * For this reason for all ASICs that not support multiple page size the function will
663e65e175bSOded Gabbay 	 * return an empty bitmask indicating that multiple page sizes is not supported.
664e65e175bSOded Gabbay 	 */
665e65e175bSOded Gabbay 	info.page_order_bitmask = hdev->asic_prop.dmmu.supported_pages_mask;
666e65e175bSOded Gabbay 
667e65e175bSOded Gabbay 	return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
668e65e175bSOded Gabbay }
669e65e175bSOded Gabbay 
sec_attest_info(struct hl_fpriv * hpriv,struct hl_info_args * args)670e65e175bSOded Gabbay static int sec_attest_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
671e65e175bSOded Gabbay {
672e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
673e65e175bSOded Gabbay 	struct cpucp_sec_attest_info *sec_attest_info;
674e65e175bSOded Gabbay 	struct hl_info_sec_attest *info;
675e65e175bSOded Gabbay 	u32 max_size = args->return_size;
676e65e175bSOded Gabbay 	int rc;
677e65e175bSOded Gabbay 
678e65e175bSOded Gabbay 	if ((!max_size) || (!out))
679e65e175bSOded Gabbay 		return -EINVAL;
680e65e175bSOded Gabbay 
681e65e175bSOded Gabbay 	sec_attest_info = kmalloc(sizeof(*sec_attest_info), GFP_KERNEL);
682e65e175bSOded Gabbay 	if (!sec_attest_info)
683e65e175bSOded Gabbay 		return -ENOMEM;
684e65e175bSOded Gabbay 
685*975aaaddSXingyuan Mo 	info = kzalloc(sizeof(*info), GFP_KERNEL);
686e65e175bSOded Gabbay 	if (!info) {
687e65e175bSOded Gabbay 		rc = -ENOMEM;
688e65e175bSOded Gabbay 		goto free_sec_attest_info;
689e65e175bSOded Gabbay 	}
690e65e175bSOded Gabbay 
691e65e175bSOded Gabbay 	rc = hl_fw_get_sec_attest_info(hpriv->hdev, sec_attest_info, args->sec_attest_nonce);
692e65e175bSOded Gabbay 	if (rc)
693e65e175bSOded Gabbay 		goto free_info;
694e65e175bSOded Gabbay 
695e65e175bSOded Gabbay 	info->nonce = le32_to_cpu(sec_attest_info->nonce);
696e65e175bSOded Gabbay 	info->pcr_quote_len = le16_to_cpu(sec_attest_info->pcr_quote_len);
697e65e175bSOded Gabbay 	info->pub_data_len = le16_to_cpu(sec_attest_info->pub_data_len);
698e65e175bSOded Gabbay 	info->certificate_len = le16_to_cpu(sec_attest_info->certificate_len);
699e65e175bSOded Gabbay 	info->pcr_num_reg = sec_attest_info->pcr_num_reg;
700e65e175bSOded Gabbay 	info->pcr_reg_len = sec_attest_info->pcr_reg_len;
701e65e175bSOded Gabbay 	info->quote_sig_len = sec_attest_info->quote_sig_len;
702e65e175bSOded Gabbay 	memcpy(&info->pcr_data, &sec_attest_info->pcr_data, sizeof(info->pcr_data));
703e65e175bSOded Gabbay 	memcpy(&info->pcr_quote, &sec_attest_info->pcr_quote, sizeof(info->pcr_quote));
704e65e175bSOded Gabbay 	memcpy(&info->public_data, &sec_attest_info->public_data, sizeof(info->public_data));
705e65e175bSOded Gabbay 	memcpy(&info->certificate, &sec_attest_info->certificate, sizeof(info->certificate));
706e65e175bSOded Gabbay 	memcpy(&info->quote_sig, &sec_attest_info->quote_sig, sizeof(info->quote_sig));
707e65e175bSOded Gabbay 
708e65e175bSOded Gabbay 	rc = copy_to_user(out, info,
709e65e175bSOded Gabbay 				min_t(size_t, max_size, sizeof(*info))) ? -EFAULT : 0;
710e65e175bSOded Gabbay 
711e65e175bSOded Gabbay free_info:
712e65e175bSOded Gabbay 	kfree(info);
713e65e175bSOded Gabbay free_sec_attest_info:
714e65e175bSOded Gabbay 	kfree(sec_attest_info);
715e65e175bSOded Gabbay 
716e65e175bSOded Gabbay 	return rc;
717e65e175bSOded Gabbay }
718e65e175bSOded Gabbay 
eventfd_register(struct hl_fpriv * hpriv,struct hl_info_args * args)719e65e175bSOded Gabbay static int eventfd_register(struct hl_fpriv *hpriv, struct hl_info_args *args)
720e65e175bSOded Gabbay {
721e65e175bSOded Gabbay 	int rc;
722e65e175bSOded Gabbay 
723e65e175bSOded Gabbay 	/* check if there is already a registered on that process */
724e65e175bSOded Gabbay 	mutex_lock(&hpriv->notifier_event.lock);
725e65e175bSOded Gabbay 	if (hpriv->notifier_event.eventfd) {
726e65e175bSOded Gabbay 		mutex_unlock(&hpriv->notifier_event.lock);
727e65e175bSOded Gabbay 		return -EINVAL;
728e65e175bSOded Gabbay 	}
729e65e175bSOded Gabbay 
730e65e175bSOded Gabbay 	hpriv->notifier_event.eventfd = eventfd_ctx_fdget(args->eventfd);
731e65e175bSOded Gabbay 	if (IS_ERR(hpriv->notifier_event.eventfd)) {
732e65e175bSOded Gabbay 		rc = PTR_ERR(hpriv->notifier_event.eventfd);
733e65e175bSOded Gabbay 		hpriv->notifier_event.eventfd = NULL;
734e65e175bSOded Gabbay 		mutex_unlock(&hpriv->notifier_event.lock);
735e65e175bSOded Gabbay 		return rc;
736e65e175bSOded Gabbay 	}
737e65e175bSOded Gabbay 
738e65e175bSOded Gabbay 	mutex_unlock(&hpriv->notifier_event.lock);
739e65e175bSOded Gabbay 	return 0;
740e65e175bSOded Gabbay }
741e65e175bSOded Gabbay 
eventfd_unregister(struct hl_fpriv * hpriv,struct hl_info_args * args)742e65e175bSOded Gabbay static int eventfd_unregister(struct hl_fpriv *hpriv, struct hl_info_args *args)
743e65e175bSOded Gabbay {
744e65e175bSOded Gabbay 	mutex_lock(&hpriv->notifier_event.lock);
745e65e175bSOded Gabbay 	if (!hpriv->notifier_event.eventfd) {
746e65e175bSOded Gabbay 		mutex_unlock(&hpriv->notifier_event.lock);
747e65e175bSOded Gabbay 		return -EINVAL;
748e65e175bSOded Gabbay 	}
749e65e175bSOded Gabbay 
750e65e175bSOded Gabbay 	eventfd_ctx_put(hpriv->notifier_event.eventfd);
751e65e175bSOded Gabbay 	hpriv->notifier_event.eventfd = NULL;
752e65e175bSOded Gabbay 	mutex_unlock(&hpriv->notifier_event.lock);
753e65e175bSOded Gabbay 	return 0;
754e65e175bSOded Gabbay }
755e65e175bSOded Gabbay 
engine_status_info(struct hl_fpriv * hpriv,struct hl_info_args * args)756e65e175bSOded Gabbay static int engine_status_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
757e65e175bSOded Gabbay {
758e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
759e65e175bSOded Gabbay 	u32 status_buf_size = args->return_size;
760e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
761e65e175bSOded Gabbay 	struct engines_data eng_data;
762e65e175bSOded Gabbay 	int rc;
763e65e175bSOded Gabbay 
764e65e175bSOded Gabbay 	if ((status_buf_size < SZ_1K) || (status_buf_size > HL_ENGINES_DATA_MAX_SIZE) || (!out))
765e65e175bSOded Gabbay 		return -EINVAL;
766e65e175bSOded Gabbay 
767e65e175bSOded Gabbay 	eng_data.actual_size = 0;
768e65e175bSOded Gabbay 	eng_data.allocated_buf_size = status_buf_size;
769e65e175bSOded Gabbay 	eng_data.buf = vmalloc(status_buf_size);
770e65e175bSOded Gabbay 	if (!eng_data.buf)
771e65e175bSOded Gabbay 		return -ENOMEM;
772e65e175bSOded Gabbay 
773e65e175bSOded Gabbay 	hdev->asic_funcs->is_device_idle(hdev, NULL, 0, &eng_data);
774e65e175bSOded Gabbay 
775e65e175bSOded Gabbay 	if (eng_data.actual_size > eng_data.allocated_buf_size) {
776e65e175bSOded Gabbay 		dev_err(hdev->dev,
777e65e175bSOded Gabbay 			"Engines data size (%d Bytes) is bigger than allocated size (%u Bytes)\n",
778e65e175bSOded Gabbay 			eng_data.actual_size, status_buf_size);
779e65e175bSOded Gabbay 		vfree(eng_data.buf);
780e65e175bSOded Gabbay 		return -ENOMEM;
781e65e175bSOded Gabbay 	}
782e65e175bSOded Gabbay 
783e65e175bSOded Gabbay 	args->user_buffer_actual_size = eng_data.actual_size;
784e65e175bSOded Gabbay 	rc = copy_to_user(out, eng_data.buf, min_t(size_t, status_buf_size, eng_data.actual_size)) ?
785e65e175bSOded Gabbay 				-EFAULT : 0;
786e65e175bSOded Gabbay 
787e65e175bSOded Gabbay 	vfree(eng_data.buf);
788e65e175bSOded Gabbay 
789e65e175bSOded Gabbay 	return rc;
790e65e175bSOded Gabbay }
791e65e175bSOded Gabbay 
page_fault_info(struct hl_fpriv * hpriv,struct hl_info_args * args)792e65e175bSOded Gabbay static int page_fault_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
793e65e175bSOded Gabbay {
794571d1a72SKoby Elbaz 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
795e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
796e65e175bSOded Gabbay 	u32 max_size = args->return_size;
797571d1a72SKoby Elbaz 	struct page_fault_info *pgf_info;
798e65e175bSOded Gabbay 
799e65e175bSOded Gabbay 	if ((!max_size) || (!out))
800e65e175bSOded Gabbay 		return -EINVAL;
801e65e175bSOded Gabbay 
802571d1a72SKoby Elbaz 	pgf_info = &hdev->captured_err_info.page_fault_info;
803571d1a72SKoby Elbaz 	if (!pgf_info->page_fault_info_available)
804571d1a72SKoby Elbaz 		return 0;
805571d1a72SKoby Elbaz 
806571d1a72SKoby Elbaz 	return copy_to_user(out, &pgf_info->page_fault,
807571d1a72SKoby Elbaz 			min_t(size_t, max_size, sizeof(struct hl_page_fault_info))) ? -EFAULT : 0;
808e65e175bSOded Gabbay }
809e65e175bSOded Gabbay 
user_mappings_info(struct hl_fpriv * hpriv,struct hl_info_args * args)810e65e175bSOded Gabbay static int user_mappings_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
811e65e175bSOded Gabbay {
812e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
813e65e175bSOded Gabbay 	u32 user_buf_size = args->return_size;
814e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
815e65e175bSOded Gabbay 	struct page_fault_info *pgf_info;
816e65e175bSOded Gabbay 	u64 actual_size;
817e65e175bSOded Gabbay 
818e65e175bSOded Gabbay 	if (!out)
819e65e175bSOded Gabbay 		return -EINVAL;
820e65e175bSOded Gabbay 
821571d1a72SKoby Elbaz 	pgf_info = &hdev->captured_err_info.page_fault_info;
822571d1a72SKoby Elbaz 	if (!pgf_info->page_fault_info_available)
823571d1a72SKoby Elbaz 		return 0;
824571d1a72SKoby Elbaz 
825571d1a72SKoby Elbaz 	args->array_size = pgf_info->num_of_user_mappings;
826571d1a72SKoby Elbaz 
827e65e175bSOded Gabbay 	actual_size = pgf_info->num_of_user_mappings * sizeof(struct hl_user_mapping);
828e65e175bSOded Gabbay 	if (user_buf_size < actual_size)
829e65e175bSOded Gabbay 		return -ENOMEM;
830e65e175bSOded Gabbay 
831571d1a72SKoby Elbaz 	return copy_to_user(out, pgf_info->user_mappings, actual_size) ? -EFAULT : 0;
832e65e175bSOded Gabbay }
833e65e175bSOded Gabbay 
hw_err_info(struct hl_fpriv * hpriv,struct hl_info_args * args)834313e9f63SMoti Haimovski static int hw_err_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
835313e9f63SMoti Haimovski {
836313e9f63SMoti Haimovski 	void __user *user_buf = (void __user *) (uintptr_t) args->return_pointer;
837313e9f63SMoti Haimovski 	struct hl_device *hdev = hpriv->hdev;
838313e9f63SMoti Haimovski 	u32 user_buf_size = args->return_size;
839313e9f63SMoti Haimovski 	struct hw_err_info *info;
840313e9f63SMoti Haimovski 	int rc;
841313e9f63SMoti Haimovski 
84204729b41SMoti Haimovski 	if (!user_buf)
843313e9f63SMoti Haimovski 		return -EINVAL;
844313e9f63SMoti Haimovski 
845313e9f63SMoti Haimovski 	info = &hdev->captured_err_info.hw_err;
846313e9f63SMoti Haimovski 	if (!info->event_info_available)
84704729b41SMoti Haimovski 		return 0;
84804729b41SMoti Haimovski 
84904729b41SMoti Haimovski 	if (user_buf_size < sizeof(struct hl_info_hw_err_event))
85004729b41SMoti Haimovski 		return -ENOMEM;
851313e9f63SMoti Haimovski 
852313e9f63SMoti Haimovski 	rc = copy_to_user(user_buf, &info->event, sizeof(struct hl_info_hw_err_event));
853313e9f63SMoti Haimovski 	return rc ? -EFAULT : 0;
854313e9f63SMoti Haimovski }
855313e9f63SMoti Haimovski 
fw_err_info(struct hl_fpriv * hpriv,struct hl_info_args * args)856313e9f63SMoti Haimovski static int fw_err_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
857313e9f63SMoti Haimovski {
858313e9f63SMoti Haimovski 	void __user *user_buf = (void __user *) (uintptr_t) args->return_pointer;
859313e9f63SMoti Haimovski 	struct hl_device *hdev = hpriv->hdev;
860313e9f63SMoti Haimovski 	u32 user_buf_size = args->return_size;
861313e9f63SMoti Haimovski 	struct fw_err_info *info;
862313e9f63SMoti Haimovski 	int rc;
863313e9f63SMoti Haimovski 
86404729b41SMoti Haimovski 	if (!user_buf)
865313e9f63SMoti Haimovski 		return -EINVAL;
866313e9f63SMoti Haimovski 
867313e9f63SMoti Haimovski 	info = &hdev->captured_err_info.fw_err;
868313e9f63SMoti Haimovski 	if (!info->event_info_available)
86904729b41SMoti Haimovski 		return 0;
87004729b41SMoti Haimovski 
87104729b41SMoti Haimovski 	if (user_buf_size < sizeof(struct hl_info_fw_err_event))
87204729b41SMoti Haimovski 		return -ENOMEM;
873313e9f63SMoti Haimovski 
874313e9f63SMoti Haimovski 	rc = copy_to_user(user_buf, &info->event, sizeof(struct hl_info_fw_err_event));
875313e9f63SMoti Haimovski 	return rc ? -EFAULT : 0;
876313e9f63SMoti Haimovski }
877313e9f63SMoti Haimovski 
send_fw_generic_request(struct hl_device * hdev,struct hl_info_args * info_args)878e65e175bSOded Gabbay static int send_fw_generic_request(struct hl_device *hdev, struct hl_info_args *info_args)
879e65e175bSOded Gabbay {
880e65e175bSOded Gabbay 	void __user *buff = (void __user *) (uintptr_t) info_args->return_pointer;
881e65e175bSOded Gabbay 	u32 size = info_args->return_size;
882e65e175bSOded Gabbay 	dma_addr_t dma_handle;
883e65e175bSOded Gabbay 	bool need_input_buff;
884e65e175bSOded Gabbay 	void *fw_buff;
885e65e175bSOded Gabbay 	int rc = 0;
886e65e175bSOded Gabbay 
887e65e175bSOded Gabbay 	switch (info_args->fw_sub_opcode) {
888e65e175bSOded Gabbay 	case HL_PASSTHROUGH_VERSIONS:
889e65e175bSOded Gabbay 		need_input_buff = false;
890e65e175bSOded Gabbay 		break;
891e65e175bSOded Gabbay 	default:
892e65e175bSOded Gabbay 		return -EINVAL;
893e65e175bSOded Gabbay 	}
894e65e175bSOded Gabbay 
895e65e175bSOded Gabbay 	if (size > SZ_1M) {
896e65e175bSOded Gabbay 		dev_err(hdev->dev, "buffer size cannot exceed 1MB\n");
897e65e175bSOded Gabbay 		return -EINVAL;
898e65e175bSOded Gabbay 	}
899e65e175bSOded Gabbay 
900e65e175bSOded Gabbay 	fw_buff = hl_cpu_accessible_dma_pool_alloc(hdev, size, &dma_handle);
901e65e175bSOded Gabbay 	if (!fw_buff)
902e65e175bSOded Gabbay 		return -ENOMEM;
903e65e175bSOded Gabbay 
904e65e175bSOded Gabbay 
905e65e175bSOded Gabbay 	if (need_input_buff && copy_from_user(fw_buff, buff, size)) {
906e65e175bSOded Gabbay 		dev_dbg(hdev->dev, "Failed to copy from user FW buff\n");
907e65e175bSOded Gabbay 		rc = -EFAULT;
908e65e175bSOded Gabbay 		goto free_buff;
909e65e175bSOded Gabbay 	}
910e65e175bSOded Gabbay 
911e65e175bSOded Gabbay 	rc = hl_fw_send_generic_request(hdev, info_args->fw_sub_opcode, dma_handle, &size);
912e65e175bSOded Gabbay 	if (rc)
913e65e175bSOded Gabbay 		goto free_buff;
914e65e175bSOded Gabbay 
915e65e175bSOded Gabbay 	if (copy_to_user(buff, fw_buff, min(size, info_args->return_size))) {
916e65e175bSOded Gabbay 		dev_dbg(hdev->dev, "Failed to copy to user FW generic req output\n");
917e65e175bSOded Gabbay 		rc = -EFAULT;
918e65e175bSOded Gabbay 	}
919e65e175bSOded Gabbay 
920e65e175bSOded Gabbay free_buff:
921e65e175bSOded Gabbay 	hl_cpu_accessible_dma_pool_free(hdev, info_args->return_size, fw_buff);
922e65e175bSOded Gabbay 
923e65e175bSOded Gabbay 	return rc;
924e65e175bSOded Gabbay }
925e65e175bSOded Gabbay 
_hl_info_ioctl(struct hl_fpriv * hpriv,void * data,struct device * dev)926e65e175bSOded Gabbay static int _hl_info_ioctl(struct hl_fpriv *hpriv, void *data,
927e65e175bSOded Gabbay 				struct device *dev)
928e65e175bSOded Gabbay {
929e65e175bSOded Gabbay 	enum hl_device_status status;
930e65e175bSOded Gabbay 	struct hl_info_args *args = data;
931e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
932e65e175bSOded Gabbay 	int rc;
933e65e175bSOded Gabbay 
93472848de0Sfarah kassabri 	if (args->pad) {
93572848de0Sfarah kassabri 		dev_dbg(hdev->dev, "Padding bytes must be 0\n");
93672848de0Sfarah kassabri 		return -EINVAL;
93772848de0Sfarah kassabri 	}
93872848de0Sfarah kassabri 
939e65e175bSOded Gabbay 	/*
940e65e175bSOded Gabbay 	 * Information is returned for the following opcodes even if the device
941e65e175bSOded Gabbay 	 * is disabled or in reset.
942e65e175bSOded Gabbay 	 */
943e65e175bSOded Gabbay 	switch (args->op) {
944e65e175bSOded Gabbay 	case HL_INFO_HW_IP_INFO:
945e65e175bSOded Gabbay 		return hw_ip_info(hdev, args);
946e65e175bSOded Gabbay 
947e65e175bSOded Gabbay 	case HL_INFO_DEVICE_STATUS:
948e65e175bSOded Gabbay 		return device_status_info(hdev, args);
949e65e175bSOded Gabbay 
950e65e175bSOded Gabbay 	case HL_INFO_RESET_COUNT:
951e65e175bSOded Gabbay 		return get_reset_count(hdev, args);
952e65e175bSOded Gabbay 
953e65e175bSOded Gabbay 	case HL_INFO_HW_EVENTS:
954e65e175bSOded Gabbay 		return hw_events_info(hdev, false, args);
955e65e175bSOded Gabbay 
956e65e175bSOded Gabbay 	case HL_INFO_HW_EVENTS_AGGREGATE:
957e65e175bSOded Gabbay 		return hw_events_info(hdev, true, args);
958e65e175bSOded Gabbay 
959e65e175bSOded Gabbay 	case HL_INFO_CS_COUNTERS:
960e65e175bSOded Gabbay 		return cs_counters_info(hpriv, args);
961e65e175bSOded Gabbay 
962e65e175bSOded Gabbay 	case HL_INFO_CLK_THROTTLE_REASON:
963e65e175bSOded Gabbay 		return clk_throttle_info(hpriv, args);
964e65e175bSOded Gabbay 
965e65e175bSOded Gabbay 	case HL_INFO_SYNC_MANAGER:
966e65e175bSOded Gabbay 		return sync_manager_info(hpriv, args);
967e65e175bSOded Gabbay 
968e65e175bSOded Gabbay 	case HL_INFO_OPEN_STATS:
969e65e175bSOded Gabbay 		return open_stats_info(hpriv, args);
970e65e175bSOded Gabbay 
971e65e175bSOded Gabbay 	case HL_INFO_LAST_ERR_OPEN_DEV_TIME:
972e65e175bSOded Gabbay 		return last_err_open_dev_info(hpriv, args);
973e65e175bSOded Gabbay 
974e65e175bSOded Gabbay 	case HL_INFO_CS_TIMEOUT_EVENT:
975e65e175bSOded Gabbay 		return cs_timeout_info(hpriv, args);
976e65e175bSOded Gabbay 
977e65e175bSOded Gabbay 	case HL_INFO_RAZWI_EVENT:
978e65e175bSOded Gabbay 		return razwi_info(hpriv, args);
979e65e175bSOded Gabbay 
980e65e175bSOded Gabbay 	case HL_INFO_UNDEFINED_OPCODE_EVENT:
981e65e175bSOded Gabbay 		return undefined_opcode_info(hpriv, args);
982e65e175bSOded Gabbay 
983e65e175bSOded Gabbay 	case HL_INFO_DEV_MEM_ALLOC_PAGE_SIZES:
984e65e175bSOded Gabbay 		return dev_mem_alloc_page_sizes_info(hpriv, args);
985e65e175bSOded Gabbay 
986e65e175bSOded Gabbay 	case HL_INFO_GET_EVENTS:
987e65e175bSOded Gabbay 		return events_info(hpriv, args);
988e65e175bSOded Gabbay 
989e65e175bSOded Gabbay 	case HL_INFO_PAGE_FAULT_EVENT:
990e65e175bSOded Gabbay 		return page_fault_info(hpriv, args);
991e65e175bSOded Gabbay 
992e65e175bSOded Gabbay 	case HL_INFO_USER_MAPPINGS:
993e65e175bSOded Gabbay 		return user_mappings_info(hpriv, args);
994e65e175bSOded Gabbay 
995e65e175bSOded Gabbay 	case HL_INFO_UNREGISTER_EVENTFD:
996e65e175bSOded Gabbay 		return eventfd_unregister(hpriv, args);
997e65e175bSOded Gabbay 
998313e9f63SMoti Haimovski 	case HL_INFO_HW_ERR_EVENT:
999313e9f63SMoti Haimovski 		return hw_err_info(hpriv, args);
1000313e9f63SMoti Haimovski 
1001313e9f63SMoti Haimovski 	case HL_INFO_FW_ERR_EVENT:
1002313e9f63SMoti Haimovski 		return fw_err_info(hpriv, args);
1003313e9f63SMoti Haimovski 
1004c7ac65c8SDafna Hirschfeld 	case HL_INFO_DRAM_USAGE:
1005c7ac65c8SDafna Hirschfeld 		return dram_usage_info(hpriv, args);
1006e65e175bSOded Gabbay 	default:
1007e65e175bSOded Gabbay 		break;
1008e65e175bSOded Gabbay 	}
1009e65e175bSOded Gabbay 
1010e65e175bSOded Gabbay 	if (!hl_device_operational(hdev, &status)) {
1011e1e8e747SOded Gabbay 		dev_dbg_ratelimited(dev,
1012e65e175bSOded Gabbay 			"Device is %s. Can't execute INFO IOCTL\n",
1013e65e175bSOded Gabbay 			hdev->status[status]);
1014e65e175bSOded Gabbay 		return -EBUSY;
1015e65e175bSOded Gabbay 	}
1016e65e175bSOded Gabbay 
1017e65e175bSOded Gabbay 	switch (args->op) {
1018e65e175bSOded Gabbay 	case HL_INFO_HW_IDLE:
1019e65e175bSOded Gabbay 		rc = hw_idle(hdev, args);
1020e65e175bSOded Gabbay 		break;
1021e65e175bSOded Gabbay 
1022e65e175bSOded Gabbay 	case HL_INFO_DEVICE_UTILIZATION:
1023e65e175bSOded Gabbay 		rc = device_utilization(hdev, args);
1024e65e175bSOded Gabbay 		break;
1025e65e175bSOded Gabbay 
1026e65e175bSOded Gabbay 	case HL_INFO_CLK_RATE:
1027e65e175bSOded Gabbay 		rc = get_clk_rate(hdev, args);
1028e65e175bSOded Gabbay 		break;
1029e65e175bSOded Gabbay 
1030e65e175bSOded Gabbay 	case HL_INFO_TIME_SYNC:
1031e65e175bSOded Gabbay 		return time_sync_info(hdev, args);
1032e65e175bSOded Gabbay 
1033e65e175bSOded Gabbay 	case HL_INFO_PCI_COUNTERS:
1034e65e175bSOded Gabbay 		return pci_counters_info(hpriv, args);
1035e65e175bSOded Gabbay 
1036e65e175bSOded Gabbay 	case HL_INFO_TOTAL_ENERGY:
1037e65e175bSOded Gabbay 		return total_energy_consumption_info(hpriv, args);
1038e65e175bSOded Gabbay 
1039e65e175bSOded Gabbay 	case HL_INFO_PLL_FREQUENCY:
1040e65e175bSOded Gabbay 		return pll_frequency_info(hpriv, args);
1041e65e175bSOded Gabbay 
1042e65e175bSOded Gabbay 	case HL_INFO_POWER:
1043e65e175bSOded Gabbay 		return power_info(hpriv, args);
1044e65e175bSOded Gabbay 
1045e65e175bSOded Gabbay 
1046e65e175bSOded Gabbay 	case HL_INFO_DRAM_REPLACED_ROWS:
1047e65e175bSOded Gabbay 		return dram_replaced_rows_info(hpriv, args);
1048e65e175bSOded Gabbay 
1049e65e175bSOded Gabbay 	case HL_INFO_DRAM_PENDING_ROWS:
1050e65e175bSOded Gabbay 		return dram_pending_rows_info(hpriv, args);
1051e65e175bSOded Gabbay 
1052e65e175bSOded Gabbay 	case HL_INFO_SECURED_ATTESTATION:
1053e65e175bSOded Gabbay 		return sec_attest_info(hpriv, args);
1054e65e175bSOded Gabbay 
1055e65e175bSOded Gabbay 	case HL_INFO_REGISTER_EVENTFD:
1056e65e175bSOded Gabbay 		return eventfd_register(hpriv, args);
1057e65e175bSOded Gabbay 
1058e65e175bSOded Gabbay 	case HL_INFO_ENGINE_STATUS:
1059e65e175bSOded Gabbay 		return engine_status_info(hpriv, args);
1060e65e175bSOded Gabbay 
1061e65e175bSOded Gabbay 	case HL_INFO_FW_GENERIC_REQ:
1062e65e175bSOded Gabbay 		return send_fw_generic_request(hdev, args);
1063e65e175bSOded Gabbay 
1064e65e175bSOded Gabbay 	default:
1065e65e175bSOded Gabbay 		dev_err(dev, "Invalid request %d\n", args->op);
1066e65e175bSOded Gabbay 		rc = -EINVAL;
1067e65e175bSOded Gabbay 		break;
1068e65e175bSOded Gabbay 	}
1069e65e175bSOded Gabbay 
1070e65e175bSOded Gabbay 	return rc;
1071e65e175bSOded Gabbay }
1072e65e175bSOded Gabbay 
hl_info_ioctl(struct hl_fpriv * hpriv,void * data)1073e65e175bSOded Gabbay static int hl_info_ioctl(struct hl_fpriv *hpriv, void *data)
1074e65e175bSOded Gabbay {
1075e65e175bSOded Gabbay 	return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev);
1076e65e175bSOded Gabbay }
1077e65e175bSOded Gabbay 
hl_info_ioctl_control(struct hl_fpriv * hpriv,void * data)1078e65e175bSOded Gabbay static int hl_info_ioctl_control(struct hl_fpriv *hpriv, void *data)
1079e65e175bSOded Gabbay {
1080e65e175bSOded Gabbay 	return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev_ctrl);
1081e65e175bSOded Gabbay }
1082e65e175bSOded Gabbay 
hl_debug_ioctl(struct hl_fpriv * hpriv,void * data)1083e65e175bSOded Gabbay static int hl_debug_ioctl(struct hl_fpriv *hpriv, void *data)
1084e65e175bSOded Gabbay {
1085e65e175bSOded Gabbay 	struct hl_debug_args *args = data;
1086e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
1087e65e175bSOded Gabbay 	enum hl_device_status status;
1088e65e175bSOded Gabbay 
1089e65e175bSOded Gabbay 	int rc = 0;
1090e65e175bSOded Gabbay 
1091e65e175bSOded Gabbay 	if (!hl_device_operational(hdev, &status)) {
1092e1e8e747SOded Gabbay 		dev_dbg_ratelimited(hdev->dev,
1093e65e175bSOded Gabbay 			"Device is %s. Can't execute DEBUG IOCTL\n",
1094e65e175bSOded Gabbay 			hdev->status[status]);
1095e65e175bSOded Gabbay 		return -EBUSY;
1096e65e175bSOded Gabbay 	}
1097e65e175bSOded Gabbay 
1098e65e175bSOded Gabbay 	switch (args->op) {
1099e65e175bSOded Gabbay 	case HL_DEBUG_OP_ETR:
1100e65e175bSOded Gabbay 	case HL_DEBUG_OP_ETF:
1101e65e175bSOded Gabbay 	case HL_DEBUG_OP_STM:
1102e65e175bSOded Gabbay 	case HL_DEBUG_OP_FUNNEL:
1103e65e175bSOded Gabbay 	case HL_DEBUG_OP_BMON:
1104e65e175bSOded Gabbay 	case HL_DEBUG_OP_SPMU:
1105e65e175bSOded Gabbay 	case HL_DEBUG_OP_TIMESTAMP:
1106e65e175bSOded Gabbay 		if (!hdev->in_debug) {
1107e65e175bSOded Gabbay 			dev_err_ratelimited(hdev->dev,
1108e65e175bSOded Gabbay 				"Rejecting debug configuration request because device not in debug mode\n");
1109e65e175bSOded Gabbay 			return -EFAULT;
1110e65e175bSOded Gabbay 		}
1111e65e175bSOded Gabbay 		args->input_size = min(args->input_size, hl_debug_struct_size[args->op]);
1112e65e175bSOded Gabbay 		rc = debug_coresight(hdev, hpriv->ctx, args);
1113e65e175bSOded Gabbay 		break;
1114e65e175bSOded Gabbay 
1115e65e175bSOded Gabbay 	case HL_DEBUG_OP_SET_MODE:
1116e65e175bSOded Gabbay 		rc = hl_device_set_debug_mode(hdev, hpriv->ctx, (bool) args->enable);
1117e65e175bSOded Gabbay 		break;
1118e65e175bSOded Gabbay 
1119e65e175bSOded Gabbay 	default:
1120e65e175bSOded Gabbay 		dev_err(hdev->dev, "Invalid request %d\n", args->op);
1121e65e175bSOded Gabbay 		rc = -EINVAL;
1122e65e175bSOded Gabbay 		break;
1123e65e175bSOded Gabbay 	}
1124e65e175bSOded Gabbay 
1125e65e175bSOded Gabbay 	return rc;
1126e65e175bSOded Gabbay }
1127e65e175bSOded Gabbay 
1128e65e175bSOded Gabbay #define HL_IOCTL_DEF(ioctl, _func) \
1129e65e175bSOded Gabbay 	[_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}
1130e65e175bSOded Gabbay 
1131e65e175bSOded Gabbay static const struct hl_ioctl_desc hl_ioctls[] = {
1132e65e175bSOded Gabbay 	HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl),
1133e65e175bSOded Gabbay 	HL_IOCTL_DEF(HL_IOCTL_CB, hl_cb_ioctl),
1134e65e175bSOded Gabbay 	HL_IOCTL_DEF(HL_IOCTL_CS, hl_cs_ioctl),
1135e65e175bSOded Gabbay 	HL_IOCTL_DEF(HL_IOCTL_WAIT_CS, hl_wait_ioctl),
1136e65e175bSOded Gabbay 	HL_IOCTL_DEF(HL_IOCTL_MEMORY, hl_mem_ioctl),
1137e65e175bSOded Gabbay 	HL_IOCTL_DEF(HL_IOCTL_DEBUG, hl_debug_ioctl)
1138e65e175bSOded Gabbay };
1139e65e175bSOded Gabbay 
1140e65e175bSOded Gabbay static const struct hl_ioctl_desc hl_ioctls_control[] = {
1141e65e175bSOded Gabbay 	HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl_control)
1142e65e175bSOded Gabbay };
1143e65e175bSOded Gabbay 
_hl_ioctl(struct file * filep,unsigned int cmd,unsigned long arg,const struct hl_ioctl_desc * ioctl,struct device * dev)1144e65e175bSOded Gabbay static long _hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg,
1145e65e175bSOded Gabbay 		const struct hl_ioctl_desc *ioctl, struct device *dev)
1146e65e175bSOded Gabbay {
1147e65e175bSOded Gabbay 	struct hl_fpriv *hpriv = filep->private_data;
1148e65e175bSOded Gabbay 	unsigned int nr = _IOC_NR(cmd);
1149e65e175bSOded Gabbay 	char stack_kdata[128] = {0};
1150e65e175bSOded Gabbay 	char *kdata = NULL;
1151e65e175bSOded Gabbay 	unsigned int usize, asize;
1152e65e175bSOded Gabbay 	hl_ioctl_t *func;
1153e65e175bSOded Gabbay 	u32 hl_size;
1154e65e175bSOded Gabbay 	int retcode;
1155e65e175bSOded Gabbay 
1156e65e175bSOded Gabbay 	/* Do not trust userspace, use our own definition */
1157e65e175bSOded Gabbay 	func = ioctl->func;
1158e65e175bSOded Gabbay 
1159e65e175bSOded Gabbay 	if (unlikely(!func)) {
1160e65e175bSOded Gabbay 		dev_dbg(dev, "no function\n");
1161e65e175bSOded Gabbay 		retcode = -ENOTTY;
1162e65e175bSOded Gabbay 		goto out_err;
1163e65e175bSOded Gabbay 	}
1164e65e175bSOded Gabbay 
1165e65e175bSOded Gabbay 	hl_size = _IOC_SIZE(ioctl->cmd);
1166e65e175bSOded Gabbay 	usize = asize = _IOC_SIZE(cmd);
1167e65e175bSOded Gabbay 	if (hl_size > asize)
1168e65e175bSOded Gabbay 		asize = hl_size;
1169e65e175bSOded Gabbay 
1170e65e175bSOded Gabbay 	cmd = ioctl->cmd;
1171e65e175bSOded Gabbay 
1172e65e175bSOded Gabbay 	if (cmd & (IOC_IN | IOC_OUT)) {
1173e65e175bSOded Gabbay 		if (asize <= sizeof(stack_kdata)) {
1174e65e175bSOded Gabbay 			kdata = stack_kdata;
1175e65e175bSOded Gabbay 		} else {
1176e65e175bSOded Gabbay 			kdata = kzalloc(asize, GFP_KERNEL);
1177e65e175bSOded Gabbay 			if (!kdata) {
1178e65e175bSOded Gabbay 				retcode = -ENOMEM;
1179e65e175bSOded Gabbay 				goto out_err;
1180e65e175bSOded Gabbay 			}
1181e65e175bSOded Gabbay 		}
1182e65e175bSOded Gabbay 	}
1183e65e175bSOded Gabbay 
1184e65e175bSOded Gabbay 	if (cmd & IOC_IN) {
1185e65e175bSOded Gabbay 		if (copy_from_user(kdata, (void __user *)arg, usize)) {
1186e65e175bSOded Gabbay 			retcode = -EFAULT;
1187e65e175bSOded Gabbay 			goto out_err;
1188e65e175bSOded Gabbay 		}
1189e65e175bSOded Gabbay 	}
1190e65e175bSOded Gabbay 
1191e65e175bSOded Gabbay 	retcode = func(hpriv, kdata);
1192e65e175bSOded Gabbay 
1193e65e175bSOded Gabbay 	if ((cmd & IOC_OUT) && copy_to_user((void __user *)arg, kdata, usize))
1194e65e175bSOded Gabbay 		retcode = -EFAULT;
1195e65e175bSOded Gabbay 
1196e65e175bSOded Gabbay out_err:
1197e65e175bSOded Gabbay 	if (retcode)
1198ff5c7025SOded Gabbay 		dev_dbg_ratelimited(dev, "error in ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
1199e65e175bSOded Gabbay 			  task_pid_nr(current), cmd, nr);
1200e65e175bSOded Gabbay 
1201e65e175bSOded Gabbay 	if (kdata != stack_kdata)
1202e65e175bSOded Gabbay 		kfree(kdata);
1203e65e175bSOded Gabbay 
1204e65e175bSOded Gabbay 	return retcode;
1205e65e175bSOded Gabbay }
1206e65e175bSOded Gabbay 
hl_ioctl(struct file * filep,unsigned int cmd,unsigned long arg)1207e65e175bSOded Gabbay long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1208e65e175bSOded Gabbay {
1209e65e175bSOded Gabbay 	struct hl_fpriv *hpriv = filep->private_data;
1210e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
1211e65e175bSOded Gabbay 	const struct hl_ioctl_desc *ioctl = NULL;
1212e65e175bSOded Gabbay 	unsigned int nr = _IOC_NR(cmd);
1213e65e175bSOded Gabbay 
1214e65e175bSOded Gabbay 	if (!hdev) {
1215e65e175bSOded Gabbay 		pr_err_ratelimited("Sending ioctl after device was removed! Please close FD\n");
1216e65e175bSOded Gabbay 		return -ENODEV;
1217e65e175bSOded Gabbay 	}
1218e65e175bSOded Gabbay 
1219e65e175bSOded Gabbay 	if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {
1220e65e175bSOded Gabbay 		ioctl = &hl_ioctls[nr];
1221e65e175bSOded Gabbay 	} else {
1222ff5c7025SOded Gabbay 		dev_dbg_ratelimited(hdev->dev, "invalid ioctl: pid=%d, nr=0x%02x\n",
1223e65e175bSOded Gabbay 			task_pid_nr(current), nr);
1224e65e175bSOded Gabbay 		return -ENOTTY;
1225e65e175bSOded Gabbay 	}
1226e65e175bSOded Gabbay 
1227e65e175bSOded Gabbay 	return _hl_ioctl(filep, cmd, arg, ioctl, hdev->dev);
1228e65e175bSOded Gabbay }
1229e65e175bSOded Gabbay 
hl_ioctl_control(struct file * filep,unsigned int cmd,unsigned long arg)1230e65e175bSOded Gabbay long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg)
1231e65e175bSOded Gabbay {
1232e65e175bSOded Gabbay 	struct hl_fpriv *hpriv = filep->private_data;
1233e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
1234e65e175bSOded Gabbay 	const struct hl_ioctl_desc *ioctl = NULL;
1235e65e175bSOded Gabbay 	unsigned int nr = _IOC_NR(cmd);
1236e65e175bSOded Gabbay 
1237e65e175bSOded Gabbay 	if (!hdev) {
1238e65e175bSOded Gabbay 		pr_err_ratelimited("Sending ioctl after device was removed! Please close FD\n");
1239e65e175bSOded Gabbay 		return -ENODEV;
1240e65e175bSOded Gabbay 	}
1241e65e175bSOded Gabbay 
1242e65e175bSOded Gabbay 	if (nr == _IOC_NR(HL_IOCTL_INFO)) {
1243e65e175bSOded Gabbay 		ioctl = &hl_ioctls_control[nr];
1244e65e175bSOded Gabbay 	} else {
1245ff5c7025SOded Gabbay 		dev_dbg_ratelimited(hdev->dev_ctrl, "invalid ioctl: pid=%d, nr=0x%02x\n",
1246e65e175bSOded Gabbay 			task_pid_nr(current), nr);
1247e65e175bSOded Gabbay 		return -ENOTTY;
1248e65e175bSOded Gabbay 	}
1249e65e175bSOded Gabbay 
1250e65e175bSOded Gabbay 	return _hl_ioctl(filep, cmd, arg, ioctl, hdev->dev_ctrl);
1251e65e175bSOded Gabbay }
1252