irq.c 7.1 KB
Newer Older
O
Oded Gabbay 已提交
1 2 3 4 5 6 7 8 9
// SPDX-License-Identifier: GPL-2.0

/*
 * Copyright 2016-2019 HabanaLabs, Ltd.
 * All Rights Reserved.
 */

#include "habanalabs.h"

10 11 12
#include <linux/slab.h>

/**
13
 * struct hl_eqe_work - This structure is used to schedule work of EQ
14
 *                      entry and cpucp_reset event
15
 *
16 17 18
 * @eq_work:          workqueue object to run when EQ entry is received
 * @hdev:             pointer to device structure
 * @eq_entry:         copy of the EQ entry
19 20 21 22 23 24
 */
struct hl_eqe_work {
	struct work_struct	eq_work;
	struct hl_device	*hdev;
	struct hl_eq_entry	eq_entry;
};
O
Oded Gabbay 已提交
25

26
/**
O
Oded Gabbay 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 * hl_cq_inc_ptr - increment ci or pi of cq
 *
 * @ptr: the current ci or pi value of the completion queue
 *
 * Increment ptr by 1. If it reaches the number of completion queue
 * entries, set it to 0
 */
inline u32 hl_cq_inc_ptr(u32 ptr)
{
	ptr++;
	if (unlikely(ptr == HL_CQ_LENGTH))
		ptr = 0;
	return ptr;
}

42
/**
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
 * hl_eq_inc_ptr - increment ci of eq
 *
 * @ptr: the current ci value of the event queue
 *
 * Increment ptr by 1. If it reaches the number of event queue
 * entries, set it to 0
 */
inline u32 hl_eq_inc_ptr(u32 ptr)
{
	ptr++;
	if (unlikely(ptr == HL_EQ_LENGTH))
		ptr = 0;
	return ptr;
}

static void irq_handle_eqe(struct work_struct *work)
{
	struct hl_eqe_work *eqe_work = container_of(work, struct hl_eqe_work,
							eq_work);
	struct hl_device *hdev = eqe_work->hdev;

	hdev->asic_funcs->handle_eqe(hdev, &eqe_work->eq_entry);

	kfree(eqe_work);
}

69
/**
O
Oded Gabbay 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83
 * hl_irq_handler_cq - irq handler for completion queue
 *
 * @irq: irq number
 * @arg: pointer to completion queue structure
 *
 */
irqreturn_t hl_irq_handler_cq(int irq, void *arg)
{
	struct hl_cq *cq = arg;
	struct hl_device *hdev = cq->hdev;
	struct hl_hw_queue *queue;
	struct hl_cs_job *job;
	bool shadow_index_valid;
	u16 shadow_index;
84
	struct hl_cq_entry *cq_entry, *cq_base;
O
Oded Gabbay 已提交
85 86 87 88 89 90 91 92

	if (hdev->disabled) {
		dev_dbg(hdev->dev,
			"Device disabled but received IRQ %d for CQ %d\n",
			irq, cq->hw_queue_id);
		return IRQ_HANDLED;
	}

93
	cq_base = cq->kernel_address;
O
Oded Gabbay 已提交
94 95

	while (1) {
96 97
		bool entry_ready = ((le32_to_cpu(cq_base[cq->ci].data) &
					CQ_ENTRY_READY_MASK)
O
Oded Gabbay 已提交
98 99 100 101 102
						>> CQ_ENTRY_READY_SHIFT);

		if (!entry_ready)
			break;

103
		cq_entry = (struct hl_cq_entry *) &cq_base[cq->ci];
O
Oded Gabbay 已提交
104

105
		/* Make sure we read CQ entry contents after we've
O
Oded Gabbay 已提交
106 107 108 109
		 * checked the ownership bit.
		 */
		dma_rmb();

110 111
		shadow_index_valid = ((le32_to_cpu(cq_entry->data) &
					CQ_ENTRY_SHADOW_INDEX_VALID_MASK)
O
Oded Gabbay 已提交
112 113
					>> CQ_ENTRY_SHADOW_INDEX_VALID_SHIFT);

114 115
		shadow_index = (u16) ((le32_to_cpu(cq_entry->data) &
					CQ_ENTRY_SHADOW_INDEX_MASK)
O
Oded Gabbay 已提交
116 117 118 119 120 121
					>> CQ_ENTRY_SHADOW_INDEX_SHIFT);

		queue = &hdev->kernel_queues[cq->hw_queue_id];

		if ((shadow_index_valid) && (!hdev->disabled)) {
			job = queue->shadow_queue[hl_pi_2_offset(shadow_index)];
122
			queue_work(hdev->cq_wq[cq->cq_idx], &job->finish_work);
O
Oded Gabbay 已提交
123 124
		}

125
		atomic_inc(&queue->ci);
O
Oded Gabbay 已提交
126 127

		/* Clear CQ entry ready bit */
128 129
		cq_entry->data = cpu_to_le32(le32_to_cpu(cq_entry->data) &
						~CQ_ENTRY_READY_MASK);
O
Oded Gabbay 已提交
130 131 132 133 134 135 136 137 138 139

		cq->ci = hl_cq_inc_ptr(cq->ci);

		/* Increment free slots */
		atomic_inc(&cq->free_slots_cnt);
	}

	return IRQ_HANDLED;
}

140
/**
141 142 143 144 145 146 147 148 149 150 151 152 153 154
 * hl_irq_handler_eq - irq handler for event queue
 *
 * @irq: irq number
 * @arg: pointer to event queue structure
 *
 */
irqreturn_t hl_irq_handler_eq(int irq, void *arg)
{
	struct hl_eq *eq = arg;
	struct hl_device *hdev = eq->hdev;
	struct hl_eq_entry *eq_entry;
	struct hl_eq_entry *eq_base;
	struct hl_eqe_work *handle_eqe_work;

155
	eq_base = eq->kernel_address;
156 157 158

	while (1) {
		bool entry_ready =
159
			((le32_to_cpu(eq_base[eq->ci].hdr.ctl) &
160
				EQ_CTL_READY_MASK) >> EQ_CTL_READY_SHIFT);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

		if (!entry_ready)
			break;

		eq_entry = &eq_base[eq->ci];

		/*
		 * Make sure we read EQ entry contents after we've
		 * checked the ownership bit.
		 */
		dma_rmb();

		if (hdev->disabled) {
			dev_warn(hdev->dev,
				"Device disabled but received IRQ %d for EQ\n",
					irq);
			goto skip_irq;
		}

		handle_eqe_work = kmalloc(sizeof(*handle_eqe_work), GFP_ATOMIC);
		if (handle_eqe_work) {
			INIT_WORK(&handle_eqe_work->eq_work, irq_handle_eqe);
			handle_eqe_work->hdev = hdev;

			memcpy(&handle_eqe_work->eq_entry, eq_entry,
					sizeof(*eq_entry));

			queue_work(hdev->eq_wq, &handle_eqe_work->eq_work);
		}
skip_irq:
		/* Clear EQ entry ready bit */
192
		eq_entry->hdr.ctl =
193
			cpu_to_le32(le32_to_cpu(eq_entry->hdr.ctl) &
194
							~EQ_CTL_READY_MASK);
195 196 197 198 199 200 201 202 203

		eq->ci = hl_eq_inc_ptr(eq->ci);

		hdev->asic_funcs->update_eq_ci(hdev, eq->ci);
	}

	return IRQ_HANDLED;
}

204
/**
O
Oded Gabbay 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217
 * hl_cq_init - main initialization function for an cq object
 *
 * @hdev: pointer to device structure
 * @q: pointer to cq structure
 * @hw_queue_id: The H/W queue ID this completion queue belongs to
 *
 * Allocate dma-able memory for the completion queue and initialize fields
 * Returns 0 on success
 */
int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id)
{
	void *p;

218
	p = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, HL_CQ_SIZE_IN_BYTES,
O
Oded Gabbay 已提交
219 220 221 222 223
				&q->bus_address, GFP_KERNEL | __GFP_ZERO);
	if (!p)
		return -ENOMEM;

	q->hdev = hdev;
224
	q->kernel_address = p;
O
Oded Gabbay 已提交
225 226 227 228 229 230 231 232 233
	q->hw_queue_id = hw_queue_id;
	q->ci = 0;
	q->pi = 0;

	atomic_set(&q->free_slots_cnt, HL_CQ_LENGTH);

	return 0;
}

234
/**
O
Oded Gabbay 已提交
235 236 237 238 239 240 241 242 243
 * hl_cq_fini - destroy completion queue
 *
 * @hdev: pointer to device structure
 * @q: pointer to cq structure
 *
 * Free the completion queue memory
 */
void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q)
{
244
	hdev->asic_funcs->asic_dma_free_coherent(hdev, HL_CQ_SIZE_IN_BYTES,
245 246
						 q->kernel_address,
						 q->bus_address);
O
Oded Gabbay 已提交
247
}
248

249 250 251 252 253 254 255 256 257 258 259 260 261 262
void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q)
{
	q->ci = 0;
	q->pi = 0;

	atomic_set(&q->free_slots_cnt, HL_CQ_LENGTH);

	/*
	 * It's not enough to just reset the PI/CI because the H/W may have
	 * written valid completion entries before it was halted and therefore
	 * we need to clean the actual queues so we won't process old entries
	 * when the device is operational again
	 */

263
	memset(q->kernel_address, 0, HL_CQ_SIZE_IN_BYTES);
264 265
}

266
/**
267 268 269 270 271 272 273 274 275 276 277 278
 * hl_eq_init - main initialization function for an event queue object
 *
 * @hdev: pointer to device structure
 * @q: pointer to eq structure
 *
 * Allocate dma-able memory for the event queue and initialize fields
 * Returns 0 on success
 */
int hl_eq_init(struct hl_device *hdev, struct hl_eq *q)
{
	void *p;

279 280 281
	p = hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
							HL_EQ_SIZE_IN_BYTES,
							&q->bus_address);
282 283 284 285
	if (!p)
		return -ENOMEM;

	q->hdev = hdev;
286
	q->kernel_address = p;
287 288 289 290 291
	q->ci = 0;

	return 0;
}

292
/**
293 294 295 296 297 298 299 300 301 302 303
 * hl_eq_fini - destroy event queue
 *
 * @hdev: pointer to device structure
 * @q: pointer to eq structure
 *
 * Free the event queue memory
 */
void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q)
{
	flush_workqueue(hdev->eq_wq);

304 305
	hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev,
					HL_EQ_SIZE_IN_BYTES,
306
					q->kernel_address);
307
}
308 309 310 311 312 313 314 315 316 317 318 319

void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q)
{
	q->ci = 0;

	/*
	 * It's not enough to just reset the PI/CI because the H/W may have
	 * written valid completion entries before it was halted and therefore
	 * we need to clean the actual queues so we won't process old entries
	 * when the device is operational again
	 */

320
	memset(q->kernel_address, 0, HL_EQ_SIZE_IN_BYTES);
321
}