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 
200648c4d0SHen Alon #include <asm/msr.h>
210648c4d0SHen Alon 
227259eb7bSMoti Haimovski /* make sure there is space for all the signed info */
237259eb7bSMoti Haimovski static_assert(sizeof(struct cpucp_info) <= SEC_DEV_INFO_BUF_SZ);
247259eb7bSMoti Haimovski 
25e65e175bSOded Gabbay static u32 hl_debug_struct_size[HL_DEBUG_OP_TIMESTAMP + 1] = {
26e65e175bSOded Gabbay 	[HL_DEBUG_OP_ETR] = sizeof(struct hl_debug_params_etr),
27e65e175bSOded Gabbay 	[HL_DEBUG_OP_ETF] = sizeof(struct hl_debug_params_etf),
28e65e175bSOded Gabbay 	[HL_DEBUG_OP_STM] = sizeof(struct hl_debug_params_stm),
29e65e175bSOded Gabbay 	[HL_DEBUG_OP_FUNNEL] = 0,
30e65e175bSOded Gabbay 	[HL_DEBUG_OP_BMON] = sizeof(struct hl_debug_params_bmon),
31e65e175bSOded Gabbay 	[HL_DEBUG_OP_SPMU] = sizeof(struct hl_debug_params_spmu),
32e65e175bSOded Gabbay 	[HL_DEBUG_OP_TIMESTAMP] = 0
33e65e175bSOded Gabbay 
34e65e175bSOded Gabbay };
35e65e175bSOded Gabbay 
device_status_info(struct hl_device * hdev,struct hl_info_args * args)36e65e175bSOded Gabbay static int device_status_info(struct hl_device *hdev, struct hl_info_args *args)
37e65e175bSOded Gabbay {
38e65e175bSOded Gabbay 	struct hl_info_device_status dev_stat = {0};
39e65e175bSOded Gabbay 	u32 size = args->return_size;
40e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
41e65e175bSOded Gabbay 
42e65e175bSOded Gabbay 	if ((!size) || (!out))
43e65e175bSOded Gabbay 		return -EINVAL;
44e65e175bSOded Gabbay 
45e65e175bSOded Gabbay 	dev_stat.status = hl_device_status(hdev);
46e65e175bSOded Gabbay 
47e65e175bSOded Gabbay 	return copy_to_user(out, &dev_stat,
48e65e175bSOded Gabbay 			min((size_t)size, sizeof(dev_stat))) ? -EFAULT : 0;
49e65e175bSOded Gabbay }
50e65e175bSOded Gabbay 
hw_ip_info(struct hl_device * hdev,struct hl_info_args * args)51e65e175bSOded Gabbay static int hw_ip_info(struct hl_device *hdev, struct hl_info_args *args)
52e65e175bSOded Gabbay {
53e65e175bSOded Gabbay 	struct hl_info_hw_ip_info hw_ip = {0};
54e65e175bSOded Gabbay 	u32 size = args->return_size;
55e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
56e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
57e65e175bSOded Gabbay 	u64 sram_kmd_size, dram_kmd_size, dram_available_size;
58e65e175bSOded Gabbay 
59e65e175bSOded Gabbay 	if ((!size) || (!out))
60e65e175bSOded Gabbay 		return -EINVAL;
61e65e175bSOded Gabbay 
62e65e175bSOded Gabbay 	sram_kmd_size = (prop->sram_user_base_address -
63e65e175bSOded Gabbay 				prop->sram_base_address);
64e65e175bSOded Gabbay 	dram_kmd_size = (prop->dram_user_base_address -
65e65e175bSOded Gabbay 				prop->dram_base_address);
66e65e175bSOded Gabbay 
67e65e175bSOded Gabbay 	hw_ip.device_id = hdev->asic_funcs->get_pci_id(hdev);
68e65e175bSOded Gabbay 	hw_ip.sram_base_address = prop->sram_user_base_address;
69e65e175bSOded Gabbay 	hw_ip.dram_base_address =
70583f12a8SOfir Bitton 			prop->dram_supports_virtual_memory ?
71e65e175bSOded Gabbay 			prop->dmmu.start_addr : prop->dram_user_base_address;
72e65e175bSOded Gabbay 	hw_ip.tpc_enabled_mask = prop->tpc_enabled_mask & 0xFF;
73e65e175bSOded Gabbay 	hw_ip.tpc_enabled_mask_ext = prop->tpc_enabled_mask;
74e65e175bSOded Gabbay 
75e65e175bSOded Gabbay 	hw_ip.sram_size = prop->sram_size - sram_kmd_size;
76e65e175bSOded Gabbay 
77e65e175bSOded Gabbay 	dram_available_size = prop->dram_size - dram_kmd_size;
78e65e175bSOded Gabbay 
79583f12a8SOfir Bitton 	hw_ip.dram_size = DIV_ROUND_DOWN_ULL(dram_available_size, prop->dram_page_size) *
80583f12a8SOfir Bitton 				prop->dram_page_size;
81e65e175bSOded Gabbay 
82e65e175bSOded Gabbay 	if (hw_ip.dram_size > PAGE_SIZE)
83e65e175bSOded Gabbay 		hw_ip.dram_enabled = 1;
84e65e175bSOded Gabbay 
85e65e175bSOded Gabbay 	hw_ip.dram_page_size = prop->dram_page_size;
86e65e175bSOded Gabbay 	hw_ip.device_mem_alloc_default_page_size = prop->device_mem_alloc_default_page_size;
87e65e175bSOded Gabbay 	hw_ip.num_of_events = prop->num_of_events;
88e65e175bSOded Gabbay 
89e65e175bSOded Gabbay 	memcpy(hw_ip.cpucp_version, prop->cpucp_info.cpucp_version,
90e65e175bSOded Gabbay 		min(VERSION_MAX_LEN, HL_INFO_VERSION_MAX_LEN));
91e65e175bSOded Gabbay 
92e65e175bSOded Gabbay 	memcpy(hw_ip.card_name, prop->cpucp_info.card_name,
93e65e175bSOded Gabbay 		min(CARD_NAME_MAX_LEN, HL_INFO_CARD_NAME_MAX_LEN));
94e65e175bSOded Gabbay 
95e65e175bSOded Gabbay 	hw_ip.cpld_version = le32_to_cpu(prop->cpucp_info.cpld_version);
96e65e175bSOded Gabbay 	hw_ip.module_id = le32_to_cpu(prop->cpucp_info.card_location);
97e65e175bSOded Gabbay 
98e65e175bSOded Gabbay 	hw_ip.psoc_pci_pll_nr = prop->psoc_pci_pll_nr;
99e65e175bSOded Gabbay 	hw_ip.psoc_pci_pll_nf = prop->psoc_pci_pll_nf;
100e65e175bSOded Gabbay 	hw_ip.psoc_pci_pll_od = prop->psoc_pci_pll_od;
101e65e175bSOded Gabbay 	hw_ip.psoc_pci_pll_div_factor = prop->psoc_pci_pll_div_factor;
102e65e175bSOded Gabbay 
103e65e175bSOded Gabbay 	hw_ip.decoder_enabled_mask = prop->decoder_enabled_mask;
104e65e175bSOded Gabbay 	hw_ip.mme_master_slave_mode = prop->mme_master_slave_mode;
105e65e175bSOded Gabbay 	hw_ip.first_available_interrupt_id = prop->first_available_user_interrupt;
106e65e175bSOded Gabbay 	hw_ip.number_of_user_interrupts = prop->user_interrupt_count;
1074713ace3SOfir Bitton 	hw_ip.tpc_interrupt_id = prop->tpc_interrupt_id;
108e65e175bSOded Gabbay 
109e65e175bSOded Gabbay 	hw_ip.edma_enabled_mask = prop->edma_enabled_mask;
110e65e175bSOded Gabbay 	hw_ip.server_type = prop->server_type;
111e65e175bSOded Gabbay 	hw_ip.security_enabled = prop->fw_security_enabled;
112e65e175bSOded Gabbay 	hw_ip.revision_id = hdev->pdev->revision;
113958e4797SOfir Bitton 	hw_ip.rotator_enabled_mask = prop->rotator_enabled_mask;
1147fc0d011SOfir Bitton 	hw_ip.engine_core_interrupt_reg_addr = prop->engine_core_interrupt_reg_addr;
11576e1ff37SOfir Bitton 	hw_ip.reserved_dram_size = dram_kmd_size;
116e65e175bSOded Gabbay 
117e65e175bSOded Gabbay 	return copy_to_user(out, &hw_ip,
118e65e175bSOded Gabbay 		min((size_t) size, sizeof(hw_ip))) ? -EFAULT : 0;
119e65e175bSOded Gabbay }
120e65e175bSOded Gabbay 
hw_events_info(struct hl_device * hdev,bool aggregate,struct hl_info_args * args)121e65e175bSOded Gabbay static int hw_events_info(struct hl_device *hdev, bool aggregate,
122e65e175bSOded Gabbay 			struct hl_info_args *args)
123e65e175bSOded Gabbay {
124e65e175bSOded Gabbay 	u32 size, max_size = args->return_size;
125e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
126e65e175bSOded Gabbay 	void *arr;
127e65e175bSOded Gabbay 
128e65e175bSOded Gabbay 	if ((!max_size) || (!out))
129e65e175bSOded Gabbay 		return -EINVAL;
130e65e175bSOded Gabbay 
131e65e175bSOded Gabbay 	arr = hdev->asic_funcs->get_events_stat(hdev, aggregate, &size);
132e65e175bSOded Gabbay 	if (!arr) {
133e65e175bSOded Gabbay 		dev_err(hdev->dev, "Events info not supported\n");
134e65e175bSOded Gabbay 		return -EOPNOTSUPP;
135e65e175bSOded Gabbay 	}
136e65e175bSOded Gabbay 
137e65e175bSOded Gabbay 	return copy_to_user(out, arr, min(max_size, size)) ? -EFAULT : 0;
138e65e175bSOded Gabbay }
139e65e175bSOded Gabbay 
events_info(struct hl_fpriv * hpriv,struct hl_info_args * args)140e65e175bSOded Gabbay static int events_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
141e65e175bSOded Gabbay {
142e65e175bSOded Gabbay 	u32 max_size = args->return_size;
143e65e175bSOded Gabbay 	u64 events_mask;
144e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
145e65e175bSOded Gabbay 
146e65e175bSOded Gabbay 	if ((max_size < sizeof(u64)) || (!out))
147e65e175bSOded Gabbay 		return -EINVAL;
148e65e175bSOded Gabbay 
149e65e175bSOded Gabbay 	mutex_lock(&hpriv->notifier_event.lock);
150e65e175bSOded Gabbay 	events_mask = hpriv->notifier_event.events_mask;
151e65e175bSOded Gabbay 	hpriv->notifier_event.events_mask = 0;
152e65e175bSOded Gabbay 	mutex_unlock(&hpriv->notifier_event.lock);
153e65e175bSOded Gabbay 
154e65e175bSOded Gabbay 	return copy_to_user(out, &events_mask, sizeof(u64)) ? -EFAULT : 0;
155e65e175bSOded Gabbay }
156e65e175bSOded Gabbay 
dram_usage_info(struct hl_fpriv * hpriv,struct hl_info_args * args)157e65e175bSOded Gabbay static int dram_usage_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
158e65e175bSOded Gabbay {
159e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
160e65e175bSOded Gabbay 	struct hl_info_dram_usage dram_usage = {0};
161e65e175bSOded Gabbay 	u32 max_size = args->return_size;
162e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
163e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
164e65e175bSOded Gabbay 	u64 dram_kmd_size;
165e65e175bSOded Gabbay 
166e65e175bSOded Gabbay 	if ((!max_size) || (!out))
167e65e175bSOded Gabbay 		return -EINVAL;
168e65e175bSOded Gabbay 
169e65e175bSOded Gabbay 	dram_kmd_size = (prop->dram_user_base_address -
170e65e175bSOded Gabbay 				prop->dram_base_address);
171e65e175bSOded Gabbay 	dram_usage.dram_free_mem = (prop->dram_size - dram_kmd_size) -
172e65e175bSOded Gabbay 					atomic64_read(&hdev->dram_used_mem);
173e65e175bSOded Gabbay 	if (hpriv->ctx)
174e65e175bSOded Gabbay 		dram_usage.ctx_dram_mem =
175e65e175bSOded Gabbay 			atomic64_read(&hpriv->ctx->dram_phys_mem);
176e65e175bSOded Gabbay 
177e65e175bSOded Gabbay 	return copy_to_user(out, &dram_usage,
178e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(dram_usage))) ? -EFAULT : 0;
179e65e175bSOded Gabbay }
180e65e175bSOded Gabbay 
hw_idle(struct hl_device * hdev,struct hl_info_args * args)181e65e175bSOded Gabbay static int hw_idle(struct hl_device *hdev, struct hl_info_args *args)
182e65e175bSOded Gabbay {
183e65e175bSOded Gabbay 	struct hl_info_hw_idle hw_idle = {0};
184e65e175bSOded Gabbay 	u32 max_size = args->return_size;
185e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
186e65e175bSOded Gabbay 
187e65e175bSOded Gabbay 	if ((!max_size) || (!out))
188e65e175bSOded Gabbay 		return -EINVAL;
189e65e175bSOded Gabbay 
190e65e175bSOded Gabbay 	hw_idle.is_idle = hdev->asic_funcs->is_device_idle(hdev,
191e65e175bSOded Gabbay 					hw_idle.busy_engines_mask_ext,
192e65e175bSOded Gabbay 					HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL);
193e65e175bSOded Gabbay 	hw_idle.busy_engines_mask =
194e65e175bSOded Gabbay 			lower_32_bits(hw_idle.busy_engines_mask_ext[0]);
195e65e175bSOded Gabbay 
196e65e175bSOded Gabbay 	return copy_to_user(out, &hw_idle,
197e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(hw_idle))) ? -EFAULT : 0;
198e65e175bSOded Gabbay }
199e65e175bSOded Gabbay 
debug_coresight(struct hl_device * hdev,struct hl_ctx * ctx,struct hl_debug_args * args)200e65e175bSOded Gabbay static int debug_coresight(struct hl_device *hdev, struct hl_ctx *ctx, struct hl_debug_args *args)
201e65e175bSOded Gabbay {
202e65e175bSOded Gabbay 	struct hl_debug_params *params;
203e65e175bSOded Gabbay 	void *input = NULL, *output = NULL;
204e65e175bSOded Gabbay 	int rc;
205e65e175bSOded Gabbay 
206e65e175bSOded Gabbay 	params = kzalloc(sizeof(*params), GFP_KERNEL);
207e65e175bSOded Gabbay 	if (!params)
208e65e175bSOded Gabbay 		return -ENOMEM;
209e65e175bSOded Gabbay 
210e65e175bSOded Gabbay 	params->reg_idx = args->reg_idx;
211e65e175bSOded Gabbay 	params->enable = args->enable;
212e65e175bSOded Gabbay 	params->op = args->op;
213e65e175bSOded Gabbay 
214e65e175bSOded Gabbay 	if (args->input_ptr && args->input_size) {
215e65e175bSOded Gabbay 		input = kzalloc(hl_debug_struct_size[args->op], GFP_KERNEL);
216e65e175bSOded Gabbay 		if (!input) {
217e65e175bSOded Gabbay 			rc = -ENOMEM;
218e65e175bSOded Gabbay 			goto out;
219e65e175bSOded Gabbay 		}
220e65e175bSOded Gabbay 
221e65e175bSOded Gabbay 		if (copy_from_user(input, u64_to_user_ptr(args->input_ptr),
222e65e175bSOded Gabbay 					args->input_size)) {
223e65e175bSOded Gabbay 			rc = -EFAULT;
224e65e175bSOded Gabbay 			dev_err(hdev->dev, "failed to copy input debug data\n");
225e65e175bSOded Gabbay 			goto out;
226e65e175bSOded Gabbay 		}
227e65e175bSOded Gabbay 
228e65e175bSOded Gabbay 		params->input = input;
229e65e175bSOded Gabbay 	}
230e65e175bSOded Gabbay 
231e65e175bSOded Gabbay 	if (args->output_ptr && args->output_size) {
232e65e175bSOded Gabbay 		output = kzalloc(args->output_size, GFP_KERNEL);
233e65e175bSOded Gabbay 		if (!output) {
234e65e175bSOded Gabbay 			rc = -ENOMEM;
235e65e175bSOded Gabbay 			goto out;
236e65e175bSOded Gabbay 		}
237e65e175bSOded Gabbay 
238e65e175bSOded Gabbay 		params->output = output;
239e65e175bSOded Gabbay 		params->output_size = args->output_size;
240e65e175bSOded Gabbay 	}
241e65e175bSOded Gabbay 
242e65e175bSOded Gabbay 	rc = hdev->asic_funcs->debug_coresight(hdev, ctx, params);
243e65e175bSOded Gabbay 	if (rc) {
244e65e175bSOded Gabbay 		dev_err(hdev->dev,
245e65e175bSOded Gabbay 			"debug coresight operation failed %d\n", rc);
246e65e175bSOded Gabbay 		goto out;
247e65e175bSOded Gabbay 	}
248e65e175bSOded Gabbay 
249e65e175bSOded Gabbay 	if (output && copy_to_user((void __user *) (uintptr_t) args->output_ptr,
250e65e175bSOded Gabbay 					output, args->output_size)) {
251e65e175bSOded Gabbay 		dev_err(hdev->dev, "copy to user failed in debug ioctl\n");
252e65e175bSOded Gabbay 		rc = -EFAULT;
253e65e175bSOded Gabbay 		goto out;
254e65e175bSOded Gabbay 	}
255e65e175bSOded Gabbay 
256e65e175bSOded Gabbay 
257e65e175bSOded Gabbay out:
258e65e175bSOded Gabbay 	kfree(params);
259e65e175bSOded Gabbay 	kfree(output);
260e65e175bSOded Gabbay 	kfree(input);
261e65e175bSOded Gabbay 
262e65e175bSOded Gabbay 	return rc;
263e65e175bSOded Gabbay }
264e65e175bSOded Gabbay 
device_utilization(struct hl_device * hdev,struct hl_info_args * args)265e65e175bSOded Gabbay static int device_utilization(struct hl_device *hdev, struct hl_info_args *args)
266e65e175bSOded Gabbay {
267e65e175bSOded Gabbay 	struct hl_info_device_utilization device_util = {0};
268e65e175bSOded Gabbay 	u32 max_size = args->return_size;
269e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
270e65e175bSOded Gabbay 	int rc;
271e65e175bSOded Gabbay 
272e65e175bSOded Gabbay 	if ((!max_size) || (!out))
273e65e175bSOded Gabbay 		return -EINVAL;
274e65e175bSOded Gabbay 
275e65e175bSOded Gabbay 	rc = hl_device_utilization(hdev, &device_util.utilization);
276e65e175bSOded Gabbay 	if (rc)
277e65e175bSOded Gabbay 		return -EINVAL;
278e65e175bSOded Gabbay 
279e65e175bSOded Gabbay 	return copy_to_user(out, &device_util,
280e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(device_util))) ? -EFAULT : 0;
281e65e175bSOded Gabbay }
282e65e175bSOded Gabbay 
get_clk_rate(struct hl_device * hdev,struct hl_info_args * args)283e65e175bSOded Gabbay static int get_clk_rate(struct hl_device *hdev, struct hl_info_args *args)
284e65e175bSOded Gabbay {
285e65e175bSOded Gabbay 	struct hl_info_clk_rate clk_rate = {0};
286e65e175bSOded Gabbay 	u32 max_size = args->return_size;
287e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
288e65e175bSOded Gabbay 	int rc;
289e65e175bSOded Gabbay 
290e65e175bSOded Gabbay 	if ((!max_size) || (!out))
291e65e175bSOded Gabbay 		return -EINVAL;
292e65e175bSOded Gabbay 
293e65e175bSOded Gabbay 	rc = hl_fw_get_clk_rate(hdev, &clk_rate.cur_clk_rate_mhz, &clk_rate.max_clk_rate_mhz);
294e65e175bSOded Gabbay 	if (rc)
295e65e175bSOded Gabbay 		return rc;
296e65e175bSOded Gabbay 
297e65e175bSOded Gabbay 	return copy_to_user(out, &clk_rate, min_t(size_t, max_size, sizeof(clk_rate)))
298e65e175bSOded Gabbay 										? -EFAULT : 0;
299e65e175bSOded Gabbay }
300e65e175bSOded Gabbay 
get_reset_count(struct hl_device * hdev,struct hl_info_args * args)301e65e175bSOded Gabbay static int get_reset_count(struct hl_device *hdev, struct hl_info_args *args)
302e65e175bSOded Gabbay {
303e65e175bSOded Gabbay 	struct hl_info_reset_count reset_count = {0};
304e65e175bSOded Gabbay 	u32 max_size = args->return_size;
305e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
306e65e175bSOded Gabbay 
307e65e175bSOded Gabbay 	if ((!max_size) || (!out))
308e65e175bSOded Gabbay 		return -EINVAL;
309e65e175bSOded Gabbay 
310e65e175bSOded Gabbay 	reset_count.hard_reset_cnt = hdev->reset_info.hard_reset_cnt;
311e65e175bSOded Gabbay 	reset_count.soft_reset_cnt = hdev->reset_info.compute_reset_cnt;
312e65e175bSOded Gabbay 
313e65e175bSOded Gabbay 	return copy_to_user(out, &reset_count,
314e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(reset_count))) ? -EFAULT : 0;
315e65e175bSOded Gabbay }
316e65e175bSOded Gabbay 
time_sync_info(struct hl_device * hdev,struct hl_info_args * args)317e65e175bSOded Gabbay static int time_sync_info(struct hl_device *hdev, struct hl_info_args *args)
318e65e175bSOded Gabbay {
319e65e175bSOded Gabbay 	struct hl_info_time_sync time_sync = {0};
320e65e175bSOded Gabbay 	u32 max_size = args->return_size;
321e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
322e65e175bSOded Gabbay 
323e65e175bSOded Gabbay 	if ((!max_size) || (!out))
324e65e175bSOded Gabbay 		return -EINVAL;
325e65e175bSOded Gabbay 
326e65e175bSOded Gabbay 	time_sync.device_time = hdev->asic_funcs->get_device_time(hdev);
327e65e175bSOded Gabbay 	time_sync.host_time = ktime_get_raw_ns();
3280648c4d0SHen Alon 	time_sync.tsc_time = rdtsc();
329e65e175bSOded Gabbay 
330e65e175bSOded Gabbay 	return copy_to_user(out, &time_sync,
331e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(time_sync))) ? -EFAULT : 0;
332e65e175bSOded Gabbay }
333e65e175bSOded Gabbay 
pci_counters_info(struct hl_fpriv * hpriv,struct hl_info_args * args)334e65e175bSOded Gabbay static int pci_counters_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
335e65e175bSOded Gabbay {
336e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
337e65e175bSOded Gabbay 	struct hl_info_pci_counters pci_counters = {0};
338e65e175bSOded Gabbay 	u32 max_size = args->return_size;
339e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
340e65e175bSOded Gabbay 	int rc;
341e65e175bSOded Gabbay 
342e65e175bSOded Gabbay 	if ((!max_size) || (!out))
343e65e175bSOded Gabbay 		return -EINVAL;
344e65e175bSOded Gabbay 
345e65e175bSOded Gabbay 	rc = hl_fw_cpucp_pci_counters_get(hdev, &pci_counters);
346e65e175bSOded Gabbay 	if (rc)
347e65e175bSOded Gabbay 		return rc;
348e65e175bSOded Gabbay 
349e65e175bSOded Gabbay 	return copy_to_user(out, &pci_counters,
350e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(pci_counters))) ? -EFAULT : 0;
351e65e175bSOded Gabbay }
352e65e175bSOded Gabbay 
clk_throttle_info(struct hl_fpriv * hpriv,struct hl_info_args * args)353e65e175bSOded Gabbay static int clk_throttle_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
354e65e175bSOded Gabbay {
355e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
356e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
357e65e175bSOded Gabbay 	struct hl_info_clk_throttle clk_throttle = {0};
358e65e175bSOded Gabbay 	ktime_t end_time, zero_time = ktime_set(0, 0);
359e65e175bSOded Gabbay 	u32 max_size = args->return_size;
360e65e175bSOded Gabbay 	int i;
361e65e175bSOded Gabbay 
362e65e175bSOded Gabbay 	if ((!max_size) || (!out))
363e65e175bSOded Gabbay 		return -EINVAL;
364e65e175bSOded Gabbay 
365e65e175bSOded Gabbay 	mutex_lock(&hdev->clk_throttling.lock);
366e65e175bSOded Gabbay 
367e65e175bSOded Gabbay 	clk_throttle.clk_throttling_reason = hdev->clk_throttling.current_reason;
368e65e175bSOded Gabbay 
369e65e175bSOded Gabbay 	for (i = 0 ; i < HL_CLK_THROTTLE_TYPE_MAX ; i++) {
370e65e175bSOded Gabbay 		if (!(hdev->clk_throttling.aggregated_reason & BIT(i)))
371e65e175bSOded Gabbay 			continue;
372e65e175bSOded Gabbay 
373e65e175bSOded Gabbay 		clk_throttle.clk_throttling_timestamp_us[i] =
374e65e175bSOded Gabbay 			ktime_to_us(hdev->clk_throttling.timestamp[i].start);
375e65e175bSOded Gabbay 
376e65e175bSOded Gabbay 		if (ktime_compare(hdev->clk_throttling.timestamp[i].end, zero_time))
377e65e175bSOded Gabbay 			end_time = hdev->clk_throttling.timestamp[i].end;
378e65e175bSOded Gabbay 		else
379e65e175bSOded Gabbay 			end_time = ktime_get();
380e65e175bSOded Gabbay 
381e65e175bSOded Gabbay 		clk_throttle.clk_throttling_duration_ns[i] =
382e65e175bSOded Gabbay 			ktime_to_ns(ktime_sub(end_time,
383e65e175bSOded Gabbay 				hdev->clk_throttling.timestamp[i].start));
384e65e175bSOded Gabbay 
385e65e175bSOded Gabbay 	}
386e65e175bSOded Gabbay 	mutex_unlock(&hdev->clk_throttling.lock);
387e65e175bSOded Gabbay 
388e65e175bSOded Gabbay 	return copy_to_user(out, &clk_throttle,
389e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(clk_throttle))) ? -EFAULT : 0;
390e65e175bSOded Gabbay }
391e65e175bSOded Gabbay 
cs_counters_info(struct hl_fpriv * hpriv,struct hl_info_args * args)392e65e175bSOded Gabbay static int cs_counters_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
393e65e175bSOded Gabbay {
394e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
395e65e175bSOded Gabbay 	struct hl_info_cs_counters cs_counters = {0};
396e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
397e65e175bSOded Gabbay 	struct hl_cs_counters_atomic *cntr;
398e65e175bSOded Gabbay 	u32 max_size = args->return_size;
399e65e175bSOded Gabbay 
400e65e175bSOded Gabbay 	cntr = &hdev->aggregated_cs_counters;
401e65e175bSOded Gabbay 
402e65e175bSOded Gabbay 	if ((!max_size) || (!out))
403e65e175bSOded Gabbay 		return -EINVAL;
404e65e175bSOded Gabbay 
405e65e175bSOded Gabbay 	cs_counters.total_out_of_mem_drop_cnt =
406e65e175bSOded Gabbay 			atomic64_read(&cntr->out_of_mem_drop_cnt);
407e65e175bSOded Gabbay 	cs_counters.total_parsing_drop_cnt =
408e65e175bSOded Gabbay 			atomic64_read(&cntr->parsing_drop_cnt);
409e65e175bSOded Gabbay 	cs_counters.total_queue_full_drop_cnt =
410e65e175bSOded Gabbay 			atomic64_read(&cntr->queue_full_drop_cnt);
411e65e175bSOded Gabbay 	cs_counters.total_device_in_reset_drop_cnt =
412e65e175bSOded Gabbay 			atomic64_read(&cntr->device_in_reset_drop_cnt);
413e65e175bSOded Gabbay 	cs_counters.total_max_cs_in_flight_drop_cnt =
414e65e175bSOded Gabbay 			atomic64_read(&cntr->max_cs_in_flight_drop_cnt);
415e65e175bSOded Gabbay 	cs_counters.total_validation_drop_cnt =
416e65e175bSOded Gabbay 			atomic64_read(&cntr->validation_drop_cnt);
417e65e175bSOded Gabbay 
418e65e175bSOded Gabbay 	if (hpriv->ctx) {
419e65e175bSOded Gabbay 		cs_counters.ctx_out_of_mem_drop_cnt =
420e65e175bSOded Gabbay 				atomic64_read(
421e65e175bSOded Gabbay 				&hpriv->ctx->cs_counters.out_of_mem_drop_cnt);
422e65e175bSOded Gabbay 		cs_counters.ctx_parsing_drop_cnt =
423e65e175bSOded Gabbay 				atomic64_read(
424e65e175bSOded Gabbay 				&hpriv->ctx->cs_counters.parsing_drop_cnt);
425e65e175bSOded Gabbay 		cs_counters.ctx_queue_full_drop_cnt =
426e65e175bSOded Gabbay 				atomic64_read(
427e65e175bSOded Gabbay 				&hpriv->ctx->cs_counters.queue_full_drop_cnt);
428e65e175bSOded Gabbay 		cs_counters.ctx_device_in_reset_drop_cnt =
429e65e175bSOded Gabbay 				atomic64_read(
430e65e175bSOded Gabbay 			&hpriv->ctx->cs_counters.device_in_reset_drop_cnt);
431e65e175bSOded Gabbay 		cs_counters.ctx_max_cs_in_flight_drop_cnt =
432e65e175bSOded Gabbay 				atomic64_read(
433e65e175bSOded Gabbay 			&hpriv->ctx->cs_counters.max_cs_in_flight_drop_cnt);
434e65e175bSOded Gabbay 		cs_counters.ctx_validation_drop_cnt =
435e65e175bSOded Gabbay 				atomic64_read(
436e65e175bSOded Gabbay 				&hpriv->ctx->cs_counters.validation_drop_cnt);
437e65e175bSOded Gabbay 	}
438e65e175bSOded Gabbay 
439e65e175bSOded Gabbay 	return copy_to_user(out, &cs_counters,
440e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(cs_counters))) ? -EFAULT : 0;
441e65e175bSOded Gabbay }
442e65e175bSOded Gabbay 
sync_manager_info(struct hl_fpriv * hpriv,struct hl_info_args * args)443e65e175bSOded Gabbay static int sync_manager_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
444e65e175bSOded Gabbay {
445e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
446e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
447e65e175bSOded Gabbay 	struct hl_info_sync_manager sm_info = {0};
448e65e175bSOded Gabbay 	u32 max_size = args->return_size;
449e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
450e65e175bSOded Gabbay 
451e65e175bSOded Gabbay 	if ((!max_size) || (!out))
452e65e175bSOded Gabbay 		return -EINVAL;
453e65e175bSOded Gabbay 
454e65e175bSOded Gabbay 	if (args->dcore_id >= HL_MAX_DCORES)
455e65e175bSOded Gabbay 		return -EINVAL;
456e65e175bSOded Gabbay 
457e65e175bSOded Gabbay 	sm_info.first_available_sync_object =
458e65e175bSOded Gabbay 			prop->first_available_user_sob[args->dcore_id];
459e65e175bSOded Gabbay 	sm_info.first_available_monitor =
460e65e175bSOded Gabbay 			prop->first_available_user_mon[args->dcore_id];
461e65e175bSOded Gabbay 	sm_info.first_available_cq =
462e65e175bSOded Gabbay 			prop->first_available_cq[args->dcore_id];
463e65e175bSOded Gabbay 
464e65e175bSOded Gabbay 	return copy_to_user(out, &sm_info, min_t(size_t, (size_t) max_size,
465e65e175bSOded Gabbay 			sizeof(sm_info))) ? -EFAULT : 0;
466e65e175bSOded Gabbay }
467e65e175bSOded Gabbay 
total_energy_consumption_info(struct hl_fpriv * hpriv,struct hl_info_args * args)468e65e175bSOded Gabbay static int total_energy_consumption_info(struct hl_fpriv *hpriv,
469e65e175bSOded Gabbay 			struct hl_info_args *args)
470e65e175bSOded Gabbay {
471e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
472e65e175bSOded Gabbay 	struct hl_info_energy total_energy = {0};
473e65e175bSOded Gabbay 	u32 max_size = args->return_size;
474e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
475e65e175bSOded Gabbay 	int rc;
476e65e175bSOded Gabbay 
477e65e175bSOded Gabbay 	if ((!max_size) || (!out))
478e65e175bSOded Gabbay 		return -EINVAL;
479e65e175bSOded Gabbay 
480e65e175bSOded Gabbay 	rc = hl_fw_cpucp_total_energy_get(hdev,
481e65e175bSOded Gabbay 			&total_energy.total_energy_consumption);
482e65e175bSOded Gabbay 	if (rc)
483e65e175bSOded Gabbay 		return rc;
484e65e175bSOded Gabbay 
485e65e175bSOded Gabbay 	return copy_to_user(out, &total_energy,
486e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(total_energy))) ? -EFAULT : 0;
487e65e175bSOded Gabbay }
488e65e175bSOded Gabbay 
pll_frequency_info(struct hl_fpriv * hpriv,struct hl_info_args * args)489e65e175bSOded Gabbay static int pll_frequency_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
490e65e175bSOded Gabbay {
491e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
492e65e175bSOded Gabbay 	struct hl_pll_frequency_info freq_info = { {0} };
493e65e175bSOded Gabbay 	u32 max_size = args->return_size;
494e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
495e65e175bSOded Gabbay 	int rc;
496e65e175bSOded Gabbay 
497e65e175bSOded Gabbay 	if ((!max_size) || (!out))
498e65e175bSOded Gabbay 		return -EINVAL;
499e65e175bSOded Gabbay 
500e65e175bSOded Gabbay 	rc = hl_fw_cpucp_pll_info_get(hdev, args->pll_index, freq_info.output);
501e65e175bSOded Gabbay 	if (rc)
502e65e175bSOded Gabbay 		return rc;
503e65e175bSOded Gabbay 
504e65e175bSOded Gabbay 	return copy_to_user(out, &freq_info,
505e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(freq_info))) ? -EFAULT : 0;
506e65e175bSOded Gabbay }
507e65e175bSOded Gabbay 
power_info(struct hl_fpriv * hpriv,struct hl_info_args * args)508e65e175bSOded Gabbay static int power_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
509e65e175bSOded Gabbay {
510e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
511e65e175bSOded Gabbay 	u32 max_size = args->return_size;
512e65e175bSOded Gabbay 	struct hl_power_info power_info = {0};
513e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
514e65e175bSOded Gabbay 	int rc;
515e65e175bSOded Gabbay 
516e65e175bSOded Gabbay 	if ((!max_size) || (!out))
517e65e175bSOded Gabbay 		return -EINVAL;
518e65e175bSOded Gabbay 
519e65e175bSOded Gabbay 	rc = hl_fw_cpucp_power_get(hdev, &power_info.power);
520e65e175bSOded Gabbay 	if (rc)
521e65e175bSOded Gabbay 		return rc;
522e65e175bSOded Gabbay 
523e65e175bSOded Gabbay 	return copy_to_user(out, &power_info,
524e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(power_info))) ? -EFAULT : 0;
525e65e175bSOded Gabbay }
526e65e175bSOded Gabbay 
open_stats_info(struct hl_fpriv * hpriv,struct hl_info_args * args)527e65e175bSOded Gabbay static int open_stats_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
528e65e175bSOded Gabbay {
529e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
530e65e175bSOded Gabbay 	u32 max_size = args->return_size;
531e65e175bSOded Gabbay 	struct hl_open_stats_info open_stats_info = {0};
532e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
533e65e175bSOded Gabbay 
534e65e175bSOded Gabbay 	if ((!max_size) || (!out))
535e65e175bSOded Gabbay 		return -EINVAL;
536e65e175bSOded Gabbay 
537e65e175bSOded Gabbay 	open_stats_info.last_open_period_ms = jiffies64_to_msecs(
538e65e175bSOded Gabbay 		hdev->last_open_session_duration_jif);
539e65e175bSOded Gabbay 	open_stats_info.open_counter = hdev->open_counter;
540e65e175bSOded Gabbay 	open_stats_info.is_compute_ctx_active = hdev->is_compute_ctx_active;
541e65e175bSOded Gabbay 	open_stats_info.compute_ctx_in_release = hdev->compute_ctx_in_release;
542e65e175bSOded Gabbay 
543e65e175bSOded Gabbay 	return copy_to_user(out, &open_stats_info,
544e65e175bSOded Gabbay 		min((size_t) max_size, sizeof(open_stats_info))) ? -EFAULT : 0;
545e65e175bSOded Gabbay }
546e65e175bSOded Gabbay 
dram_pending_rows_info(struct hl_fpriv * hpriv,struct hl_info_args * args)547e65e175bSOded Gabbay static int dram_pending_rows_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
548e65e175bSOded Gabbay {
549e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
550e65e175bSOded Gabbay 	u32 max_size = args->return_size;
551e65e175bSOded Gabbay 	u32 pend_rows_num = 0;
552e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
553e65e175bSOded Gabbay 	int rc;
554e65e175bSOded Gabbay 
555e65e175bSOded Gabbay 	if ((!max_size) || (!out))
556e65e175bSOded Gabbay 		return -EINVAL;
557e65e175bSOded Gabbay 
558e65e175bSOded Gabbay 	rc = hl_fw_dram_pending_row_get(hdev, &pend_rows_num);
559e65e175bSOded Gabbay 	if (rc)
560e65e175bSOded Gabbay 		return rc;
561e65e175bSOded Gabbay 
562e65e175bSOded Gabbay 	return copy_to_user(out, &pend_rows_num,
563e65e175bSOded Gabbay 			min_t(size_t, max_size, sizeof(pend_rows_num))) ? -EFAULT : 0;
564e65e175bSOded Gabbay }
565e65e175bSOded Gabbay 
dram_replaced_rows_info(struct hl_fpriv * hpriv,struct hl_info_args * args)566e65e175bSOded Gabbay static int dram_replaced_rows_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
567e65e175bSOded Gabbay {
568e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
569e65e175bSOded Gabbay 	u32 max_size = args->return_size;
570e65e175bSOded Gabbay 	struct cpucp_hbm_row_info info = {0};
571e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
572e65e175bSOded Gabbay 	int rc;
573e65e175bSOded Gabbay 
574e65e175bSOded Gabbay 	if ((!max_size) || (!out))
575e65e175bSOded Gabbay 		return -EINVAL;
576e65e175bSOded Gabbay 
577e65e175bSOded Gabbay 	rc = hl_fw_dram_replaced_row_get(hdev, &info);
578e65e175bSOded Gabbay 	if (rc)
579e65e175bSOded Gabbay 		return rc;
580e65e175bSOded Gabbay 
581e65e175bSOded Gabbay 	return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
582e65e175bSOded Gabbay }
583e65e175bSOded Gabbay 
last_err_open_dev_info(struct hl_fpriv * hpriv,struct hl_info_args * args)584e65e175bSOded Gabbay static int last_err_open_dev_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
585e65e175bSOded Gabbay {
586e65e175bSOded Gabbay 	struct hl_info_last_err_open_dev_time info = {0};
587e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
588e65e175bSOded Gabbay 	u32 max_size = args->return_size;
589e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
590e65e175bSOded Gabbay 
591e65e175bSOded Gabbay 	if ((!max_size) || (!out))
592e65e175bSOded Gabbay 		return -EINVAL;
593e65e175bSOded Gabbay 
594e65e175bSOded Gabbay 	info.timestamp = ktime_to_ns(hdev->last_successful_open_ktime);
595e65e175bSOded Gabbay 
596e65e175bSOded Gabbay 	return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
597e65e175bSOded Gabbay }
598e65e175bSOded Gabbay 
cs_timeout_info(struct hl_fpriv * hpriv,struct hl_info_args * args)599e65e175bSOded Gabbay static int cs_timeout_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
600e65e175bSOded Gabbay {
601e65e175bSOded Gabbay 	struct hl_info_cs_timeout_event info = {0};
602e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
603e65e175bSOded Gabbay 	u32 max_size = args->return_size;
604e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
605e65e175bSOded Gabbay 
606e65e175bSOded Gabbay 	if ((!max_size) || (!out))
607e65e175bSOded Gabbay 		return -EINVAL;
608e65e175bSOded Gabbay 
609e65e175bSOded Gabbay 	info.seq = hdev->captured_err_info.cs_timeout.seq;
610e65e175bSOded Gabbay 	info.timestamp = ktime_to_ns(hdev->captured_err_info.cs_timeout.timestamp);
611e65e175bSOded Gabbay 
612e65e175bSOded Gabbay 	return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
613e65e175bSOded Gabbay }
614e65e175bSOded Gabbay 
razwi_info(struct hl_fpriv * hpriv,struct hl_info_args * args)615e65e175bSOded Gabbay static int razwi_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
616e65e175bSOded Gabbay {
617571d1a72SKoby Elbaz 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
618e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
619e65e175bSOded Gabbay 	u32 max_size = args->return_size;
620571d1a72SKoby Elbaz 	struct razwi_info *razwi_info;
621e65e175bSOded Gabbay 
622e65e175bSOded Gabbay 	if ((!max_size) || (!out))
623e65e175bSOded Gabbay 		return -EINVAL;
624e65e175bSOded Gabbay 
625571d1a72SKoby Elbaz 	razwi_info = &hdev->captured_err_info.razwi_info;
626571d1a72SKoby Elbaz 	if (!razwi_info->razwi_info_available)
627571d1a72SKoby Elbaz 		return 0;
628571d1a72SKoby Elbaz 
629571d1a72SKoby Elbaz 	return copy_to_user(out, &razwi_info->razwi,
630571d1a72SKoby Elbaz 			min_t(size_t, max_size, sizeof(struct hl_info_razwi_event))) ? -EFAULT : 0;
631e65e175bSOded Gabbay }
632e65e175bSOded Gabbay 
undefined_opcode_info(struct hl_fpriv * hpriv,struct hl_info_args * args)633e65e175bSOded Gabbay static int undefined_opcode_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
634e65e175bSOded Gabbay {
635e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
636e65e175bSOded Gabbay 	u32 max_size = args->return_size;
637e65e175bSOded Gabbay 	struct hl_info_undefined_opcode_event info = {0};
638e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
639e65e175bSOded Gabbay 
640e65e175bSOded Gabbay 	if ((!max_size) || (!out))
641e65e175bSOded Gabbay 		return -EINVAL;
642e65e175bSOded Gabbay 
643e65e175bSOded Gabbay 	info.timestamp = ktime_to_ns(hdev->captured_err_info.undef_opcode.timestamp);
644e65e175bSOded Gabbay 	info.engine_id = hdev->captured_err_info.undef_opcode.engine_id;
645e65e175bSOded Gabbay 	info.cq_addr = hdev->captured_err_info.undef_opcode.cq_addr;
646e65e175bSOded Gabbay 	info.cq_size = hdev->captured_err_info.undef_opcode.cq_size;
647e65e175bSOded Gabbay 	info.stream_id = hdev->captured_err_info.undef_opcode.stream_id;
648e65e175bSOded Gabbay 	info.cb_addr_streams_len = hdev->captured_err_info.undef_opcode.cb_addr_streams_len;
649e65e175bSOded Gabbay 	memcpy(info.cb_addr_streams, hdev->captured_err_info.undef_opcode.cb_addr_streams,
650e65e175bSOded Gabbay 			sizeof(info.cb_addr_streams));
651e65e175bSOded Gabbay 
652e65e175bSOded Gabbay 	return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
653e65e175bSOded Gabbay }
654e65e175bSOded Gabbay 
dev_mem_alloc_page_sizes_info(struct hl_fpriv * hpriv,struct hl_info_args * args)655e65e175bSOded Gabbay static int dev_mem_alloc_page_sizes_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
656e65e175bSOded Gabbay {
657e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
658e65e175bSOded Gabbay 	struct hl_info_dev_memalloc_page_sizes info = {0};
659e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
660e65e175bSOded Gabbay 	u32 max_size = args->return_size;
661e65e175bSOded Gabbay 
662e65e175bSOded Gabbay 	if ((!max_size) || (!out))
663e65e175bSOded Gabbay 		return -EINVAL;
664e65e175bSOded Gabbay 
665e65e175bSOded Gabbay 	/*
666e65e175bSOded Gabbay 	 * Future ASICs that will support multiple DRAM page sizes will support only "powers of 2"
667e65e175bSOded Gabbay 	 * pages (unlike some of the ASICs before supporting multiple page sizes).
668e65e175bSOded Gabbay 	 * For this reason for all ASICs that not support multiple page size the function will
669e65e175bSOded Gabbay 	 * return an empty bitmask indicating that multiple page sizes is not supported.
670e65e175bSOded Gabbay 	 */
671e65e175bSOded Gabbay 	info.page_order_bitmask = hdev->asic_prop.dmmu.supported_pages_mask;
672e65e175bSOded Gabbay 
673e65e175bSOded Gabbay 	return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
674e65e175bSOded Gabbay }
675e65e175bSOded Gabbay 
sec_attest_info(struct hl_fpriv * hpriv,struct hl_info_args * args)676e65e175bSOded Gabbay static int sec_attest_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
677e65e175bSOded Gabbay {
678e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
679e65e175bSOded Gabbay 	struct cpucp_sec_attest_info *sec_attest_info;
680e65e175bSOded Gabbay 	struct hl_info_sec_attest *info;
681e65e175bSOded Gabbay 	u32 max_size = args->return_size;
682e65e175bSOded Gabbay 	int rc;
683e65e175bSOded Gabbay 
684e65e175bSOded Gabbay 	if ((!max_size) || (!out))
685e65e175bSOded Gabbay 		return -EINVAL;
686e65e175bSOded Gabbay 
687e65e175bSOded Gabbay 	sec_attest_info = kmalloc(sizeof(*sec_attest_info), GFP_KERNEL);
688e65e175bSOded Gabbay 	if (!sec_attest_info)
689e65e175bSOded Gabbay 		return -ENOMEM;
690e65e175bSOded Gabbay 
691*a9f07790SXingyuan Mo 	info = kzalloc(sizeof(*info), GFP_KERNEL);
692e65e175bSOded Gabbay 	if (!info) {
693e65e175bSOded Gabbay 		rc = -ENOMEM;
694e65e175bSOded Gabbay 		goto free_sec_attest_info;
695e65e175bSOded Gabbay 	}
696e65e175bSOded Gabbay 
697e65e175bSOded Gabbay 	rc = hl_fw_get_sec_attest_info(hpriv->hdev, sec_attest_info, args->sec_attest_nonce);
698e65e175bSOded Gabbay 	if (rc)
699e65e175bSOded Gabbay 		goto free_info;
700e65e175bSOded Gabbay 
701e65e175bSOded Gabbay 	info->nonce = le32_to_cpu(sec_attest_info->nonce);
702e65e175bSOded Gabbay 	info->pcr_quote_len = le16_to_cpu(sec_attest_info->pcr_quote_len);
703e65e175bSOded Gabbay 	info->pub_data_len = le16_to_cpu(sec_attest_info->pub_data_len);
704e65e175bSOded Gabbay 	info->certificate_len = le16_to_cpu(sec_attest_info->certificate_len);
705e65e175bSOded Gabbay 	info->pcr_num_reg = sec_attest_info->pcr_num_reg;
706e65e175bSOded Gabbay 	info->pcr_reg_len = sec_attest_info->pcr_reg_len;
707e65e175bSOded Gabbay 	info->quote_sig_len = sec_attest_info->quote_sig_len;
708e65e175bSOded Gabbay 	memcpy(&info->pcr_data, &sec_attest_info->pcr_data, sizeof(info->pcr_data));
709e65e175bSOded Gabbay 	memcpy(&info->pcr_quote, &sec_attest_info->pcr_quote, sizeof(info->pcr_quote));
710e65e175bSOded Gabbay 	memcpy(&info->public_data, &sec_attest_info->public_data, sizeof(info->public_data));
711e65e175bSOded Gabbay 	memcpy(&info->certificate, &sec_attest_info->certificate, sizeof(info->certificate));
712e65e175bSOded Gabbay 	memcpy(&info->quote_sig, &sec_attest_info->quote_sig, sizeof(info->quote_sig));
713e65e175bSOded Gabbay 
714e65e175bSOded Gabbay 	rc = copy_to_user(out, info,
715e65e175bSOded Gabbay 				min_t(size_t, max_size, sizeof(*info))) ? -EFAULT : 0;
716e65e175bSOded Gabbay 
717e65e175bSOded Gabbay free_info:
718e65e175bSOded Gabbay 	kfree(info);
719e65e175bSOded Gabbay free_sec_attest_info:
720e65e175bSOded Gabbay 	kfree(sec_attest_info);
721e65e175bSOded Gabbay 
722e65e175bSOded Gabbay 	return rc;
723e65e175bSOded Gabbay }
724e65e175bSOded Gabbay 
dev_info_signed(struct hl_fpriv * hpriv,struct hl_info_args * args)7257259eb7bSMoti Haimovski static int dev_info_signed(struct hl_fpriv *hpriv, struct hl_info_args *args)
7267259eb7bSMoti Haimovski {
7277259eb7bSMoti Haimovski 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
7287259eb7bSMoti Haimovski 	struct cpucp_dev_info_signed *dev_info_signed;
7297259eb7bSMoti Haimovski 	struct hl_info_signed *info;
7307259eb7bSMoti Haimovski 	u32 max_size = args->return_size;
7317259eb7bSMoti Haimovski 	int rc;
7327259eb7bSMoti Haimovski 
7337259eb7bSMoti Haimovski 	if ((!max_size) || (!out))
7347259eb7bSMoti Haimovski 		return -EINVAL;
7357259eb7bSMoti Haimovski 
7367259eb7bSMoti Haimovski 	dev_info_signed = kzalloc(sizeof(*dev_info_signed), GFP_KERNEL);
7377259eb7bSMoti Haimovski 	if (!dev_info_signed)
7387259eb7bSMoti Haimovski 		return -ENOMEM;
7397259eb7bSMoti Haimovski 
7407259eb7bSMoti Haimovski 	info = kzalloc(sizeof(*info), GFP_KERNEL);
7417259eb7bSMoti Haimovski 	if (!info) {
7427259eb7bSMoti Haimovski 		rc = -ENOMEM;
7437259eb7bSMoti Haimovski 		goto free_dev_info_signed;
7447259eb7bSMoti Haimovski 	}
7457259eb7bSMoti Haimovski 
7467259eb7bSMoti Haimovski 	rc = hl_fw_get_dev_info_signed(hpriv->hdev,
7477259eb7bSMoti Haimovski 					dev_info_signed, args->sec_attest_nonce);
7487259eb7bSMoti Haimovski 	if (rc)
7497259eb7bSMoti Haimovski 		goto free_info;
7507259eb7bSMoti Haimovski 
7517259eb7bSMoti Haimovski 	info->nonce = le32_to_cpu(dev_info_signed->nonce);
7527259eb7bSMoti Haimovski 	info->info_sig_len = dev_info_signed->info_sig_len;
7537259eb7bSMoti Haimovski 	info->pub_data_len = le16_to_cpu(dev_info_signed->pub_data_len);
7547259eb7bSMoti Haimovski 	info->certificate_len = le16_to_cpu(dev_info_signed->certificate_len);
7557259eb7bSMoti Haimovski 	info->dev_info_len = sizeof(struct cpucp_info);
7567259eb7bSMoti Haimovski 	memcpy(&info->info_sig, &dev_info_signed->info_sig, sizeof(info->info_sig));
7577259eb7bSMoti Haimovski 	memcpy(&info->public_data, &dev_info_signed->public_data, sizeof(info->public_data));
7587259eb7bSMoti Haimovski 	memcpy(&info->certificate, &dev_info_signed->certificate, sizeof(info->certificate));
7597259eb7bSMoti Haimovski 	memcpy(&info->dev_info, &dev_info_signed->info, info->dev_info_len);
7607259eb7bSMoti Haimovski 
7617259eb7bSMoti Haimovski 	rc = copy_to_user(out, info, min_t(size_t, max_size, sizeof(*info))) ? -EFAULT : 0;
7627259eb7bSMoti Haimovski 
7637259eb7bSMoti Haimovski free_info:
7647259eb7bSMoti Haimovski 	kfree(info);
7657259eb7bSMoti Haimovski free_dev_info_signed:
7667259eb7bSMoti Haimovski 	kfree(dev_info_signed);
7677259eb7bSMoti Haimovski 
7687259eb7bSMoti Haimovski 	return rc;
7697259eb7bSMoti Haimovski }
7707259eb7bSMoti Haimovski 
7717259eb7bSMoti Haimovski 
eventfd_register(struct hl_fpriv * hpriv,struct hl_info_args * args)772e65e175bSOded Gabbay static int eventfd_register(struct hl_fpriv *hpriv, struct hl_info_args *args)
773e65e175bSOded Gabbay {
774e65e175bSOded Gabbay 	int rc;
775e65e175bSOded Gabbay 
776e65e175bSOded Gabbay 	/* check if there is already a registered on that process */
777e65e175bSOded Gabbay 	mutex_lock(&hpriv->notifier_event.lock);
778e65e175bSOded Gabbay 	if (hpriv->notifier_event.eventfd) {
779e65e175bSOded Gabbay 		mutex_unlock(&hpriv->notifier_event.lock);
780e65e175bSOded Gabbay 		return -EINVAL;
781e65e175bSOded Gabbay 	}
782e65e175bSOded Gabbay 
783e65e175bSOded Gabbay 	hpriv->notifier_event.eventfd = eventfd_ctx_fdget(args->eventfd);
784e65e175bSOded Gabbay 	if (IS_ERR(hpriv->notifier_event.eventfd)) {
785e65e175bSOded Gabbay 		rc = PTR_ERR(hpriv->notifier_event.eventfd);
786e65e175bSOded Gabbay 		hpriv->notifier_event.eventfd = NULL;
787e65e175bSOded Gabbay 		mutex_unlock(&hpriv->notifier_event.lock);
788e65e175bSOded Gabbay 		return rc;
789e65e175bSOded Gabbay 	}
790e65e175bSOded Gabbay 
791e65e175bSOded Gabbay 	mutex_unlock(&hpriv->notifier_event.lock);
792e65e175bSOded Gabbay 	return 0;
793e65e175bSOded Gabbay }
794e65e175bSOded Gabbay 
eventfd_unregister(struct hl_fpriv * hpriv,struct hl_info_args * args)795e65e175bSOded Gabbay static int eventfd_unregister(struct hl_fpriv *hpriv, struct hl_info_args *args)
796e65e175bSOded Gabbay {
797e65e175bSOded Gabbay 	mutex_lock(&hpriv->notifier_event.lock);
798e65e175bSOded Gabbay 	if (!hpriv->notifier_event.eventfd) {
799e65e175bSOded Gabbay 		mutex_unlock(&hpriv->notifier_event.lock);
800e65e175bSOded Gabbay 		return -EINVAL;
801e65e175bSOded Gabbay 	}
802e65e175bSOded Gabbay 
803e65e175bSOded Gabbay 	eventfd_ctx_put(hpriv->notifier_event.eventfd);
804e65e175bSOded Gabbay 	hpriv->notifier_event.eventfd = NULL;
805e65e175bSOded Gabbay 	mutex_unlock(&hpriv->notifier_event.lock);
806e65e175bSOded Gabbay 	return 0;
807e65e175bSOded Gabbay }
808e65e175bSOded Gabbay 
engine_status_info(struct hl_fpriv * hpriv,struct hl_info_args * args)809e65e175bSOded Gabbay static int engine_status_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
810e65e175bSOded Gabbay {
811e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
812e65e175bSOded Gabbay 	u32 status_buf_size = args->return_size;
813e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
814e65e175bSOded Gabbay 	struct engines_data eng_data;
815e65e175bSOded Gabbay 	int rc;
816e65e175bSOded Gabbay 
817e65e175bSOded Gabbay 	if ((status_buf_size < SZ_1K) || (status_buf_size > HL_ENGINES_DATA_MAX_SIZE) || (!out))
818e65e175bSOded Gabbay 		return -EINVAL;
819e65e175bSOded Gabbay 
820e65e175bSOded Gabbay 	eng_data.actual_size = 0;
821e65e175bSOded Gabbay 	eng_data.allocated_buf_size = status_buf_size;
822e65e175bSOded Gabbay 	eng_data.buf = vmalloc(status_buf_size);
823e65e175bSOded Gabbay 	if (!eng_data.buf)
824e65e175bSOded Gabbay 		return -ENOMEM;
825e65e175bSOded Gabbay 
826e65e175bSOded Gabbay 	hdev->asic_funcs->is_device_idle(hdev, NULL, 0, &eng_data);
827e65e175bSOded Gabbay 
828e65e175bSOded Gabbay 	if (eng_data.actual_size > eng_data.allocated_buf_size) {
829e65e175bSOded Gabbay 		dev_err(hdev->dev,
830e65e175bSOded Gabbay 			"Engines data size (%d Bytes) is bigger than allocated size (%u Bytes)\n",
831e65e175bSOded Gabbay 			eng_data.actual_size, status_buf_size);
832e65e175bSOded Gabbay 		vfree(eng_data.buf);
833e65e175bSOded Gabbay 		return -ENOMEM;
834e65e175bSOded Gabbay 	}
835e65e175bSOded Gabbay 
836e65e175bSOded Gabbay 	args->user_buffer_actual_size = eng_data.actual_size;
837e65e175bSOded Gabbay 	rc = copy_to_user(out, eng_data.buf, min_t(size_t, status_buf_size, eng_data.actual_size)) ?
838e65e175bSOded Gabbay 				-EFAULT : 0;
839e65e175bSOded Gabbay 
840e65e175bSOded Gabbay 	vfree(eng_data.buf);
841e65e175bSOded Gabbay 
842e65e175bSOded Gabbay 	return rc;
843e65e175bSOded Gabbay }
844e65e175bSOded Gabbay 
page_fault_info(struct hl_fpriv * hpriv,struct hl_info_args * args)845e65e175bSOded Gabbay static int page_fault_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
846e65e175bSOded Gabbay {
847571d1a72SKoby Elbaz 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
848e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
849e65e175bSOded Gabbay 	u32 max_size = args->return_size;
850571d1a72SKoby Elbaz 	struct page_fault_info *pgf_info;
851e65e175bSOded Gabbay 
852e65e175bSOded Gabbay 	if ((!max_size) || (!out))
853e65e175bSOded Gabbay 		return -EINVAL;
854e65e175bSOded Gabbay 
855571d1a72SKoby Elbaz 	pgf_info = &hdev->captured_err_info.page_fault_info;
856571d1a72SKoby Elbaz 	if (!pgf_info->page_fault_info_available)
857571d1a72SKoby Elbaz 		return 0;
858571d1a72SKoby Elbaz 
859571d1a72SKoby Elbaz 	return copy_to_user(out, &pgf_info->page_fault,
860571d1a72SKoby Elbaz 			min_t(size_t, max_size, sizeof(struct hl_page_fault_info))) ? -EFAULT : 0;
861e65e175bSOded Gabbay }
862e65e175bSOded Gabbay 
user_mappings_info(struct hl_fpriv * hpriv,struct hl_info_args * args)863e65e175bSOded Gabbay static int user_mappings_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
864e65e175bSOded Gabbay {
865e65e175bSOded Gabbay 	void __user *out = (void __user *) (uintptr_t) args->return_pointer;
866e65e175bSOded Gabbay 	u32 user_buf_size = args->return_size;
867e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
868e65e175bSOded Gabbay 	struct page_fault_info *pgf_info;
869e65e175bSOded Gabbay 	u64 actual_size;
870e65e175bSOded Gabbay 
871e65e175bSOded Gabbay 	if (!out)
872e65e175bSOded Gabbay 		return -EINVAL;
873e65e175bSOded Gabbay 
874571d1a72SKoby Elbaz 	pgf_info = &hdev->captured_err_info.page_fault_info;
875571d1a72SKoby Elbaz 	if (!pgf_info->page_fault_info_available)
876571d1a72SKoby Elbaz 		return 0;
877571d1a72SKoby Elbaz 
878571d1a72SKoby Elbaz 	args->array_size = pgf_info->num_of_user_mappings;
879571d1a72SKoby Elbaz 
880e65e175bSOded Gabbay 	actual_size = pgf_info->num_of_user_mappings * sizeof(struct hl_user_mapping);
881e65e175bSOded Gabbay 	if (user_buf_size < actual_size)
882e65e175bSOded Gabbay 		return -ENOMEM;
883e65e175bSOded Gabbay 
884571d1a72SKoby Elbaz 	return copy_to_user(out, pgf_info->user_mappings, actual_size) ? -EFAULT : 0;
885e65e175bSOded Gabbay }
886e65e175bSOded Gabbay 
hw_err_info(struct hl_fpriv * hpriv,struct hl_info_args * args)887313e9f63SMoti Haimovski static int hw_err_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
888313e9f63SMoti Haimovski {
889313e9f63SMoti Haimovski 	void __user *user_buf = (void __user *) (uintptr_t) args->return_pointer;
890313e9f63SMoti Haimovski 	struct hl_device *hdev = hpriv->hdev;
891313e9f63SMoti Haimovski 	u32 user_buf_size = args->return_size;
892313e9f63SMoti Haimovski 	struct hw_err_info *info;
893313e9f63SMoti Haimovski 	int rc;
894313e9f63SMoti Haimovski 
89504729b41SMoti Haimovski 	if (!user_buf)
896313e9f63SMoti Haimovski 		return -EINVAL;
897313e9f63SMoti Haimovski 
898313e9f63SMoti Haimovski 	info = &hdev->captured_err_info.hw_err;
899313e9f63SMoti Haimovski 	if (!info->event_info_available)
90004729b41SMoti Haimovski 		return 0;
90104729b41SMoti Haimovski 
90204729b41SMoti Haimovski 	if (user_buf_size < sizeof(struct hl_info_hw_err_event))
90304729b41SMoti Haimovski 		return -ENOMEM;
904313e9f63SMoti Haimovski 
905313e9f63SMoti Haimovski 	rc = copy_to_user(user_buf, &info->event, sizeof(struct hl_info_hw_err_event));
906313e9f63SMoti Haimovski 	return rc ? -EFAULT : 0;
907313e9f63SMoti Haimovski }
908313e9f63SMoti Haimovski 
fw_err_info(struct hl_fpriv * hpriv,struct hl_info_args * args)909313e9f63SMoti Haimovski static int fw_err_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
910313e9f63SMoti Haimovski {
911313e9f63SMoti Haimovski 	void __user *user_buf = (void __user *) (uintptr_t) args->return_pointer;
912313e9f63SMoti Haimovski 	struct hl_device *hdev = hpriv->hdev;
913313e9f63SMoti Haimovski 	u32 user_buf_size = args->return_size;
914313e9f63SMoti Haimovski 	struct fw_err_info *info;
915313e9f63SMoti Haimovski 	int rc;
916313e9f63SMoti Haimovski 
91704729b41SMoti Haimovski 	if (!user_buf)
918313e9f63SMoti Haimovski 		return -EINVAL;
919313e9f63SMoti Haimovski 
920313e9f63SMoti Haimovski 	info = &hdev->captured_err_info.fw_err;
921313e9f63SMoti Haimovski 	if (!info->event_info_available)
92204729b41SMoti Haimovski 		return 0;
92304729b41SMoti Haimovski 
92404729b41SMoti Haimovski 	if (user_buf_size < sizeof(struct hl_info_fw_err_event))
92504729b41SMoti Haimovski 		return -ENOMEM;
926313e9f63SMoti Haimovski 
927313e9f63SMoti Haimovski 	rc = copy_to_user(user_buf, &info->event, sizeof(struct hl_info_fw_err_event));
928313e9f63SMoti Haimovski 	return rc ? -EFAULT : 0;
929313e9f63SMoti Haimovski }
930313e9f63SMoti Haimovski 
engine_err_info(struct hl_fpriv * hpriv,struct hl_info_args * args)931a8ab1a81SOfir Bitton static int engine_err_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
932a8ab1a81SOfir Bitton {
933a8ab1a81SOfir Bitton 	void __user *user_buf = (void __user *) (uintptr_t) args->return_pointer;
934a8ab1a81SOfir Bitton 	struct hl_device *hdev = hpriv->hdev;
935a8ab1a81SOfir Bitton 	u32 user_buf_size = args->return_size;
936a8ab1a81SOfir Bitton 	struct engine_err_info *info;
937a8ab1a81SOfir Bitton 	int rc;
938a8ab1a81SOfir Bitton 
939a8ab1a81SOfir Bitton 	if (!user_buf)
940a8ab1a81SOfir Bitton 		return -EINVAL;
941a8ab1a81SOfir Bitton 
942a8ab1a81SOfir Bitton 	info = &hdev->captured_err_info.engine_err;
943a8ab1a81SOfir Bitton 	if (!info->event_info_available)
944a8ab1a81SOfir Bitton 		return 0;
945a8ab1a81SOfir Bitton 
946a8ab1a81SOfir Bitton 	if (user_buf_size < sizeof(struct hl_info_engine_err_event))
947a8ab1a81SOfir Bitton 		return -ENOMEM;
948a8ab1a81SOfir Bitton 
949a8ab1a81SOfir Bitton 	rc = copy_to_user(user_buf, &info->event, sizeof(struct hl_info_engine_err_event));
950a8ab1a81SOfir Bitton 	return rc ? -EFAULT : 0;
951a8ab1a81SOfir Bitton }
952a8ab1a81SOfir Bitton 
send_fw_generic_request(struct hl_device * hdev,struct hl_info_args * info_args)953e65e175bSOded Gabbay static int send_fw_generic_request(struct hl_device *hdev, struct hl_info_args *info_args)
954e65e175bSOded Gabbay {
955e65e175bSOded Gabbay 	void __user *buff = (void __user *) (uintptr_t) info_args->return_pointer;
956e65e175bSOded Gabbay 	u32 size = info_args->return_size;
957e65e175bSOded Gabbay 	dma_addr_t dma_handle;
958e65e175bSOded Gabbay 	bool need_input_buff;
959e65e175bSOded Gabbay 	void *fw_buff;
960e65e175bSOded Gabbay 	int rc = 0;
961e65e175bSOded Gabbay 
962e65e175bSOded Gabbay 	switch (info_args->fw_sub_opcode) {
963e65e175bSOded Gabbay 	case HL_PASSTHROUGH_VERSIONS:
964e65e175bSOded Gabbay 		need_input_buff = false;
965e65e175bSOded Gabbay 		break;
966e65e175bSOded Gabbay 	default:
967e65e175bSOded Gabbay 		return -EINVAL;
968e65e175bSOded Gabbay 	}
969e65e175bSOded Gabbay 
970e65e175bSOded Gabbay 	if (size > SZ_1M) {
971e65e175bSOded Gabbay 		dev_err(hdev->dev, "buffer size cannot exceed 1MB\n");
972e65e175bSOded Gabbay 		return -EINVAL;
973e65e175bSOded Gabbay 	}
974e65e175bSOded Gabbay 
975e65e175bSOded Gabbay 	fw_buff = hl_cpu_accessible_dma_pool_alloc(hdev, size, &dma_handle);
976e65e175bSOded Gabbay 	if (!fw_buff)
977e65e175bSOded Gabbay 		return -ENOMEM;
978e65e175bSOded Gabbay 
979e65e175bSOded Gabbay 
980e65e175bSOded Gabbay 	if (need_input_buff && copy_from_user(fw_buff, buff, size)) {
981e65e175bSOded Gabbay 		dev_dbg(hdev->dev, "Failed to copy from user FW buff\n");
982e65e175bSOded Gabbay 		rc = -EFAULT;
983e65e175bSOded Gabbay 		goto free_buff;
984e65e175bSOded Gabbay 	}
985e65e175bSOded Gabbay 
986e65e175bSOded Gabbay 	rc = hl_fw_send_generic_request(hdev, info_args->fw_sub_opcode, dma_handle, &size);
987e65e175bSOded Gabbay 	if (rc)
988e65e175bSOded Gabbay 		goto free_buff;
989e65e175bSOded Gabbay 
990e65e175bSOded Gabbay 	if (copy_to_user(buff, fw_buff, min(size, info_args->return_size))) {
991e65e175bSOded Gabbay 		dev_dbg(hdev->dev, "Failed to copy to user FW generic req output\n");
992e65e175bSOded Gabbay 		rc = -EFAULT;
993e65e175bSOded Gabbay 	}
994e65e175bSOded Gabbay 
995e65e175bSOded Gabbay free_buff:
996e65e175bSOded Gabbay 	hl_cpu_accessible_dma_pool_free(hdev, info_args->return_size, fw_buff);
997e65e175bSOded Gabbay 
998e65e175bSOded Gabbay 	return rc;
999e65e175bSOded Gabbay }
1000e65e175bSOded Gabbay 
_hl_info_ioctl(struct hl_fpriv * hpriv,void * data,struct device * dev)1001e65e175bSOded Gabbay static int _hl_info_ioctl(struct hl_fpriv *hpriv, void *data,
1002e65e175bSOded Gabbay 				struct device *dev)
1003e65e175bSOded Gabbay {
1004e65e175bSOded Gabbay 	enum hl_device_status status;
1005e65e175bSOded Gabbay 	struct hl_info_args *args = data;
1006e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
1007e65e175bSOded Gabbay 	int rc;
1008e65e175bSOded Gabbay 
100972848de0Sfarah kassabri 	if (args->pad) {
101072848de0Sfarah kassabri 		dev_dbg(hdev->dev, "Padding bytes must be 0\n");
101172848de0Sfarah kassabri 		return -EINVAL;
101272848de0Sfarah kassabri 	}
101372848de0Sfarah kassabri 
1014e65e175bSOded Gabbay 	/*
1015e65e175bSOded Gabbay 	 * Information is returned for the following opcodes even if the device
1016e65e175bSOded Gabbay 	 * is disabled or in reset.
1017e65e175bSOded Gabbay 	 */
1018e65e175bSOded Gabbay 	switch (args->op) {
1019e65e175bSOded Gabbay 	case HL_INFO_HW_IP_INFO:
1020e65e175bSOded Gabbay 		return hw_ip_info(hdev, args);
1021e65e175bSOded Gabbay 
1022e65e175bSOded Gabbay 	case HL_INFO_DEVICE_STATUS:
1023e65e175bSOded Gabbay 		return device_status_info(hdev, args);
1024e65e175bSOded Gabbay 
1025e65e175bSOded Gabbay 	case HL_INFO_RESET_COUNT:
1026e65e175bSOded Gabbay 		return get_reset_count(hdev, args);
1027e65e175bSOded Gabbay 
1028e65e175bSOded Gabbay 	case HL_INFO_HW_EVENTS:
1029e65e175bSOded Gabbay 		return hw_events_info(hdev, false, args);
1030e65e175bSOded Gabbay 
1031e65e175bSOded Gabbay 	case HL_INFO_HW_EVENTS_AGGREGATE:
1032e65e175bSOded Gabbay 		return hw_events_info(hdev, true, args);
1033e65e175bSOded Gabbay 
1034e65e175bSOded Gabbay 	case HL_INFO_CS_COUNTERS:
1035e65e175bSOded Gabbay 		return cs_counters_info(hpriv, args);
1036e65e175bSOded Gabbay 
1037e65e175bSOded Gabbay 	case HL_INFO_CLK_THROTTLE_REASON:
1038e65e175bSOded Gabbay 		return clk_throttle_info(hpriv, args);
1039e65e175bSOded Gabbay 
1040e65e175bSOded Gabbay 	case HL_INFO_SYNC_MANAGER:
1041e65e175bSOded Gabbay 		return sync_manager_info(hpriv, args);
1042e65e175bSOded Gabbay 
1043e65e175bSOded Gabbay 	case HL_INFO_OPEN_STATS:
1044e65e175bSOded Gabbay 		return open_stats_info(hpriv, args);
1045e65e175bSOded Gabbay 
1046e65e175bSOded Gabbay 	case HL_INFO_LAST_ERR_OPEN_DEV_TIME:
1047e65e175bSOded Gabbay 		return last_err_open_dev_info(hpriv, args);
1048e65e175bSOded Gabbay 
1049e65e175bSOded Gabbay 	case HL_INFO_CS_TIMEOUT_EVENT:
1050e65e175bSOded Gabbay 		return cs_timeout_info(hpriv, args);
1051e65e175bSOded Gabbay 
1052e65e175bSOded Gabbay 	case HL_INFO_RAZWI_EVENT:
1053e65e175bSOded Gabbay 		return razwi_info(hpriv, args);
1054e65e175bSOded Gabbay 
1055e65e175bSOded Gabbay 	case HL_INFO_UNDEFINED_OPCODE_EVENT:
1056e65e175bSOded Gabbay 		return undefined_opcode_info(hpriv, args);
1057e65e175bSOded Gabbay 
1058e65e175bSOded Gabbay 	case HL_INFO_DEV_MEM_ALLOC_PAGE_SIZES:
1059e65e175bSOded Gabbay 		return dev_mem_alloc_page_sizes_info(hpriv, args);
1060e65e175bSOded Gabbay 
1061e65e175bSOded Gabbay 	case HL_INFO_GET_EVENTS:
1062e65e175bSOded Gabbay 		return events_info(hpriv, args);
1063e65e175bSOded Gabbay 
1064e65e175bSOded Gabbay 	case HL_INFO_PAGE_FAULT_EVENT:
1065e65e175bSOded Gabbay 		return page_fault_info(hpriv, args);
1066e65e175bSOded Gabbay 
1067e65e175bSOded Gabbay 	case HL_INFO_USER_MAPPINGS:
1068e65e175bSOded Gabbay 		return user_mappings_info(hpriv, args);
1069e65e175bSOded Gabbay 
1070e65e175bSOded Gabbay 	case HL_INFO_UNREGISTER_EVENTFD:
1071e65e175bSOded Gabbay 		return eventfd_unregister(hpriv, args);
1072e65e175bSOded Gabbay 
1073313e9f63SMoti Haimovski 	case HL_INFO_HW_ERR_EVENT:
1074313e9f63SMoti Haimovski 		return hw_err_info(hpriv, args);
1075313e9f63SMoti Haimovski 
1076313e9f63SMoti Haimovski 	case HL_INFO_FW_ERR_EVENT:
1077313e9f63SMoti Haimovski 		return fw_err_info(hpriv, args);
1078313e9f63SMoti Haimovski 
1079a8ab1a81SOfir Bitton 	case HL_INFO_USER_ENGINE_ERR_EVENT:
1080a8ab1a81SOfir Bitton 		return engine_err_info(hpriv, args);
1081a8ab1a81SOfir Bitton 
1082c7ac65c8SDafna Hirschfeld 	case HL_INFO_DRAM_USAGE:
1083c7ac65c8SDafna Hirschfeld 		return dram_usage_info(hpriv, args);
1084e65e175bSOded Gabbay 	default:
1085e65e175bSOded Gabbay 		break;
1086e65e175bSOded Gabbay 	}
1087e65e175bSOded Gabbay 
1088e65e175bSOded Gabbay 	if (!hl_device_operational(hdev, &status)) {
1089e1e8e747SOded Gabbay 		dev_dbg_ratelimited(dev,
1090e65e175bSOded Gabbay 			"Device is %s. Can't execute INFO IOCTL\n",
1091e65e175bSOded Gabbay 			hdev->status[status]);
1092e65e175bSOded Gabbay 		return -EBUSY;
1093e65e175bSOded Gabbay 	}
1094e65e175bSOded Gabbay 
1095e65e175bSOded Gabbay 	switch (args->op) {
1096e65e175bSOded Gabbay 	case HL_INFO_HW_IDLE:
1097e65e175bSOded Gabbay 		rc = hw_idle(hdev, args);
1098e65e175bSOded Gabbay 		break;
1099e65e175bSOded Gabbay 
1100e65e175bSOded Gabbay 	case HL_INFO_DEVICE_UTILIZATION:
1101e65e175bSOded Gabbay 		rc = device_utilization(hdev, args);
1102e65e175bSOded Gabbay 		break;
1103e65e175bSOded Gabbay 
1104e65e175bSOded Gabbay 	case HL_INFO_CLK_RATE:
1105e65e175bSOded Gabbay 		rc = get_clk_rate(hdev, args);
1106e65e175bSOded Gabbay 		break;
1107e65e175bSOded Gabbay 
1108e65e175bSOded Gabbay 	case HL_INFO_TIME_SYNC:
1109e65e175bSOded Gabbay 		return time_sync_info(hdev, args);
1110e65e175bSOded Gabbay 
1111e65e175bSOded Gabbay 	case HL_INFO_PCI_COUNTERS:
1112e65e175bSOded Gabbay 		return pci_counters_info(hpriv, args);
1113e65e175bSOded Gabbay 
1114e65e175bSOded Gabbay 	case HL_INFO_TOTAL_ENERGY:
1115e65e175bSOded Gabbay 		return total_energy_consumption_info(hpriv, args);
1116e65e175bSOded Gabbay 
1117e65e175bSOded Gabbay 	case HL_INFO_PLL_FREQUENCY:
1118e65e175bSOded Gabbay 		return pll_frequency_info(hpriv, args);
1119e65e175bSOded Gabbay 
1120e65e175bSOded Gabbay 	case HL_INFO_POWER:
1121e65e175bSOded Gabbay 		return power_info(hpriv, args);
1122e65e175bSOded Gabbay 
1123e65e175bSOded Gabbay 
1124e65e175bSOded Gabbay 	case HL_INFO_DRAM_REPLACED_ROWS:
1125e65e175bSOded Gabbay 		return dram_replaced_rows_info(hpriv, args);
1126e65e175bSOded Gabbay 
1127e65e175bSOded Gabbay 	case HL_INFO_DRAM_PENDING_ROWS:
1128e65e175bSOded Gabbay 		return dram_pending_rows_info(hpriv, args);
1129e65e175bSOded Gabbay 
1130e65e175bSOded Gabbay 	case HL_INFO_SECURED_ATTESTATION:
1131e65e175bSOded Gabbay 		return sec_attest_info(hpriv, args);
1132e65e175bSOded Gabbay 
1133e65e175bSOded Gabbay 	case HL_INFO_REGISTER_EVENTFD:
1134e65e175bSOded Gabbay 		return eventfd_register(hpriv, args);
1135e65e175bSOded Gabbay 
1136e65e175bSOded Gabbay 	case HL_INFO_ENGINE_STATUS:
1137e65e175bSOded Gabbay 		return engine_status_info(hpriv, args);
1138e65e175bSOded Gabbay 
1139e65e175bSOded Gabbay 	case HL_INFO_FW_GENERIC_REQ:
1140e65e175bSOded Gabbay 		return send_fw_generic_request(hdev, args);
1141e65e175bSOded Gabbay 
11427259eb7bSMoti Haimovski 	case HL_INFO_DEV_SIGNED:
11437259eb7bSMoti Haimovski 		return dev_info_signed(hpriv, args);
11447259eb7bSMoti Haimovski 
1145e65e175bSOded Gabbay 	default:
1146e65e175bSOded Gabbay 		dev_err(dev, "Invalid request %d\n", args->op);
1147e65e175bSOded Gabbay 		rc = -EINVAL;
1148e65e175bSOded Gabbay 		break;
1149e65e175bSOded Gabbay 	}
1150e65e175bSOded Gabbay 
1151e65e175bSOded Gabbay 	return rc;
1152e65e175bSOded Gabbay }
1153e65e175bSOded Gabbay 
hl_info_ioctl(struct drm_device * ddev,void * data,struct drm_file * file_priv)115457963ff8STomer Tayar int hl_info_ioctl(struct drm_device *ddev, void *data, struct drm_file *file_priv)
1155e65e175bSOded Gabbay {
115657963ff8STomer Tayar 	struct hl_fpriv *hpriv = file_priv->driver_priv;
115757963ff8STomer Tayar 
1158e65e175bSOded Gabbay 	return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev);
1159e65e175bSOded Gabbay }
1160e65e175bSOded Gabbay 
hl_info_ioctl_control(struct hl_fpriv * hpriv,void * data)1161e65e175bSOded Gabbay static int hl_info_ioctl_control(struct hl_fpriv *hpriv, void *data)
1162e65e175bSOded Gabbay {
1163ab574f6aSDafna Hirschfeld 	struct hl_info_args *args = data;
1164ab574f6aSDafna Hirschfeld 
1165ab574f6aSDafna Hirschfeld 	switch (args->op) {
1166ab574f6aSDafna Hirschfeld 	case HL_INFO_GET_EVENTS:
1167ab574f6aSDafna Hirschfeld 	case HL_INFO_UNREGISTER_EVENTFD:
1168ab574f6aSDafna Hirschfeld 	case HL_INFO_REGISTER_EVENTFD:
1169ab574f6aSDafna Hirschfeld 		return -EOPNOTSUPP;
1170ab574f6aSDafna Hirschfeld 	default:
1171ab574f6aSDafna Hirschfeld 		break;
1172ab574f6aSDafna Hirschfeld 	}
1173ab574f6aSDafna Hirschfeld 
1174e65e175bSOded Gabbay 	return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev_ctrl);
1175e65e175bSOded Gabbay }
1176e65e175bSOded Gabbay 
hl_debug_ioctl(struct drm_device * ddev,void * data,struct drm_file * file_priv)117757963ff8STomer Tayar int hl_debug_ioctl(struct drm_device *ddev, void *data, struct drm_file *file_priv)
1178e65e175bSOded Gabbay {
117957963ff8STomer Tayar 	struct hl_fpriv *hpriv = file_priv->driver_priv;
1180e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
118157963ff8STomer Tayar 	struct hl_debug_args *args = data;
1182e65e175bSOded Gabbay 	enum hl_device_status status;
1183e65e175bSOded Gabbay 
1184e65e175bSOded Gabbay 	int rc = 0;
1185e65e175bSOded Gabbay 
1186e65e175bSOded Gabbay 	if (!hl_device_operational(hdev, &status)) {
1187e1e8e747SOded Gabbay 		dev_dbg_ratelimited(hdev->dev,
1188e65e175bSOded Gabbay 			"Device is %s. Can't execute DEBUG IOCTL\n",
1189e65e175bSOded Gabbay 			hdev->status[status]);
1190e65e175bSOded Gabbay 		return -EBUSY;
1191e65e175bSOded Gabbay 	}
1192e65e175bSOded Gabbay 
1193e65e175bSOded Gabbay 	switch (args->op) {
1194e65e175bSOded Gabbay 	case HL_DEBUG_OP_ETR:
1195e65e175bSOded Gabbay 	case HL_DEBUG_OP_ETF:
1196e65e175bSOded Gabbay 	case HL_DEBUG_OP_STM:
1197e65e175bSOded Gabbay 	case HL_DEBUG_OP_FUNNEL:
1198e65e175bSOded Gabbay 	case HL_DEBUG_OP_BMON:
1199e65e175bSOded Gabbay 	case HL_DEBUG_OP_SPMU:
1200e65e175bSOded Gabbay 	case HL_DEBUG_OP_TIMESTAMP:
1201e65e175bSOded Gabbay 		if (!hdev->in_debug) {
1202e65e175bSOded Gabbay 			dev_err_ratelimited(hdev->dev,
1203e65e175bSOded Gabbay 				"Rejecting debug configuration request because device not in debug mode\n");
1204e65e175bSOded Gabbay 			return -EFAULT;
1205e65e175bSOded Gabbay 		}
1206e65e175bSOded Gabbay 		args->input_size = min(args->input_size, hl_debug_struct_size[args->op]);
1207e65e175bSOded Gabbay 		rc = debug_coresight(hdev, hpriv->ctx, args);
1208e65e175bSOded Gabbay 		break;
1209e65e175bSOded Gabbay 
1210e65e175bSOded Gabbay 	case HL_DEBUG_OP_SET_MODE:
1211e65e175bSOded Gabbay 		rc = hl_device_set_debug_mode(hdev, hpriv->ctx, (bool) args->enable);
1212e65e175bSOded Gabbay 		break;
1213e65e175bSOded Gabbay 
1214e65e175bSOded Gabbay 	default:
1215e65e175bSOded Gabbay 		dev_err(hdev->dev, "Invalid request %d\n", args->op);
1216e65e175bSOded Gabbay 		rc = -EINVAL;
1217e65e175bSOded Gabbay 		break;
1218e65e175bSOded Gabbay 	}
1219e65e175bSOded Gabbay 
1220e65e175bSOded Gabbay 	return rc;
1221e65e175bSOded Gabbay }
1222e65e175bSOded Gabbay 
1223e65e175bSOded Gabbay #define HL_IOCTL_DEF(ioctl, _func) \
122457963ff8STomer Tayar 	[_IOC_NR(ioctl) - HL_COMMAND_START] = {.cmd = ioctl, .func = _func}
1225e65e175bSOded Gabbay 
1226e65e175bSOded Gabbay static const struct hl_ioctl_desc hl_ioctls_control[] = {
122757963ff8STomer Tayar 	HL_IOCTL_DEF(DRM_IOCTL_HL_INFO, hl_info_ioctl_control)
1228e65e175bSOded Gabbay };
1229e65e175bSOded Gabbay 
_hl_ioctl(struct hl_fpriv * hpriv,unsigned int cmd,unsigned long arg,const struct hl_ioctl_desc * ioctl,struct device * dev)1230fe77368cSTomer Tayar static long _hl_ioctl(struct hl_fpriv *hpriv, unsigned int cmd, unsigned long arg,
1231e65e175bSOded Gabbay 			const struct hl_ioctl_desc *ioctl, struct device *dev)
1232e65e175bSOded Gabbay {
1233e65e175bSOded Gabbay 	unsigned int nr = _IOC_NR(cmd);
1234e65e175bSOded Gabbay 	char stack_kdata[128] = {0};
1235e65e175bSOded Gabbay 	char *kdata = NULL;
1236e65e175bSOded Gabbay 	unsigned int usize, asize;
1237e65e175bSOded Gabbay 	hl_ioctl_t *func;
1238e65e175bSOded Gabbay 	u32 hl_size;
1239e65e175bSOded Gabbay 	int retcode;
1240e65e175bSOded Gabbay 
1241e65e175bSOded Gabbay 	/* Do not trust userspace, use our own definition */
1242e65e175bSOded Gabbay 	func = ioctl->func;
1243e65e175bSOded Gabbay 
1244e65e175bSOded Gabbay 	if (unlikely(!func)) {
1245e65e175bSOded Gabbay 		dev_dbg(dev, "no function\n");
1246e65e175bSOded Gabbay 		retcode = -ENOTTY;
1247e65e175bSOded Gabbay 		goto out_err;
1248e65e175bSOded Gabbay 	}
1249e65e175bSOded Gabbay 
1250e65e175bSOded Gabbay 	hl_size = _IOC_SIZE(ioctl->cmd);
1251e65e175bSOded Gabbay 	usize = asize = _IOC_SIZE(cmd);
1252e65e175bSOded Gabbay 	if (hl_size > asize)
1253e65e175bSOded Gabbay 		asize = hl_size;
1254e65e175bSOded Gabbay 
1255e65e175bSOded Gabbay 	cmd = ioctl->cmd;
1256e65e175bSOded Gabbay 
1257e65e175bSOded Gabbay 	if (cmd & (IOC_IN | IOC_OUT)) {
1258e65e175bSOded Gabbay 		if (asize <= sizeof(stack_kdata)) {
1259e65e175bSOded Gabbay 			kdata = stack_kdata;
1260e65e175bSOded Gabbay 		} else {
1261e65e175bSOded Gabbay 			kdata = kzalloc(asize, GFP_KERNEL);
1262e65e175bSOded Gabbay 			if (!kdata) {
1263e65e175bSOded Gabbay 				retcode = -ENOMEM;
1264e65e175bSOded Gabbay 				goto out_err;
1265e65e175bSOded Gabbay 			}
1266e65e175bSOded Gabbay 		}
1267e65e175bSOded Gabbay 	}
1268e65e175bSOded Gabbay 
1269e65e175bSOded Gabbay 	if (cmd & IOC_IN) {
1270e65e175bSOded Gabbay 		if (copy_from_user(kdata, (void __user *)arg, usize)) {
1271e65e175bSOded Gabbay 			retcode = -EFAULT;
1272e65e175bSOded Gabbay 			goto out_err;
1273e65e175bSOded Gabbay 		}
1274e65e175bSOded Gabbay 	}
1275e65e175bSOded Gabbay 
1276e65e175bSOded Gabbay 	retcode = func(hpriv, kdata);
1277e65e175bSOded Gabbay 
1278e65e175bSOded Gabbay 	if ((cmd & IOC_OUT) && copy_to_user((void __user *)arg, kdata, usize))
1279e65e175bSOded Gabbay 		retcode = -EFAULT;
1280e65e175bSOded Gabbay 
1281e65e175bSOded Gabbay out_err:
12827dccb064STomer Tayar 	if (retcode) {
12837dccb064STomer Tayar 		char task_comm[TASK_COMM_LEN];
12847dccb064STomer Tayar 
12857dccb064STomer Tayar 		dev_dbg_ratelimited(dev,
12867dccb064STomer Tayar 				"error in ioctl: pid=%d, comm=\"%s\", cmd=%#010x, nr=%#04x\n",
12877dccb064STomer Tayar 				task_pid_nr(current), get_task_comm(task_comm, current), cmd, nr);
12887dccb064STomer Tayar 	}
1289e65e175bSOded Gabbay 
1290e65e175bSOded Gabbay 	if (kdata != stack_kdata)
1291e65e175bSOded Gabbay 		kfree(kdata);
1292e65e175bSOded Gabbay 
1293e65e175bSOded Gabbay 	return retcode;
1294e65e175bSOded Gabbay }
1295e65e175bSOded Gabbay 
hl_ioctl_control(struct file * filep,unsigned int cmd,unsigned long arg)1296e65e175bSOded Gabbay long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg)
1297e65e175bSOded Gabbay {
1298e65e175bSOded Gabbay 	struct hl_fpriv *hpriv = filep->private_data;
1299e65e175bSOded Gabbay 	struct hl_device *hdev = hpriv->hdev;
1300e65e175bSOded Gabbay 	const struct hl_ioctl_desc *ioctl = NULL;
1301e65e175bSOded Gabbay 	unsigned int nr = _IOC_NR(cmd);
1302e65e175bSOded Gabbay 
1303e65e175bSOded Gabbay 	if (!hdev) {
1304e65e175bSOded Gabbay 		pr_err_ratelimited("Sending ioctl after device was removed! Please close FD\n");
1305e65e175bSOded Gabbay 		return -ENODEV;
1306e65e175bSOded Gabbay 	}
1307e65e175bSOded Gabbay 
130857963ff8STomer Tayar 	if (nr == _IOC_NR(DRM_IOCTL_HL_INFO)) {
130957963ff8STomer Tayar 		ioctl = &hl_ioctls_control[nr - HL_COMMAND_START];
1310e65e175bSOded Gabbay 	} else {
13117dccb064STomer Tayar 		char task_comm[TASK_COMM_LEN];
13127dccb064STomer Tayar 
13137dccb064STomer Tayar 		dev_dbg_ratelimited(hdev->dev_ctrl,
13147dccb064STomer Tayar 				"invalid ioctl: pid=%d, comm=\"%s\", cmd=%#010x, nr=%#04x\n",
13157dccb064STomer Tayar 				task_pid_nr(current), get_task_comm(task_comm, current), cmd, nr);
1316e65e175bSOded Gabbay 		return -ENOTTY;
1317e65e175bSOded Gabbay 	}
1318e65e175bSOded Gabbay 
1319fe77368cSTomer Tayar 	return _hl_ioctl(hpriv, cmd, arg, ioctl, hdev->dev_ctrl);
1320e65e175bSOded Gabbay }
1321