Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2//
  3// Copyright (c) 2018 MediaTek Inc.
  4
  5#include <linux/bitops.h>
  6#include <linux/clk.h>
  7#include <linux/clk-provider.h>
  8#include <linux/dma-mapping.h>
  9#include <linux/errno.h>
 10#include <linux/interrupt.h>
 11#include <linux/io.h>
 12#include <linux/iopoll.h>
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/platform_device.h>
 16#include <linux/mailbox_controller.h>
 17#include <linux/mailbox/mtk-cmdq-mailbox.h>
 18#include <linux/of_device.h>
 19
 20#define CMDQ_OP_CODE_MASK		(0xff << CMDQ_OP_CODE_SHIFT)
 21#define CMDQ_NUM_CMD(t)			(t->cmd_buf_size / CMDQ_INST_SIZE)
 22#define CMDQ_GCE_NUM_MAX		(2)
 23
 24#define CMDQ_CURR_IRQ_STATUS		0x10
 25#define CMDQ_SYNC_TOKEN_UPDATE		0x68
 26#define CMDQ_THR_SLOT_CYCLES		0x30
 27#define CMDQ_THR_BASE			0x100
 28#define CMDQ_THR_SIZE			0x80
 29#define CMDQ_THR_WARM_RESET		0x00
 30#define CMDQ_THR_ENABLE_TASK		0x04
 31#define CMDQ_THR_SUSPEND_TASK		0x08
 32#define CMDQ_THR_CURR_STATUS		0x0c
 33#define CMDQ_THR_IRQ_STATUS		0x10
 34#define CMDQ_THR_IRQ_ENABLE		0x14
 35#define CMDQ_THR_CURR_ADDR		0x20
 36#define CMDQ_THR_END_ADDR		0x24
 37#define CMDQ_THR_WAIT_TOKEN		0x30
 38#define CMDQ_THR_PRIORITY		0x40
 39
 40#define GCE_GCTL_VALUE			0x48
 41#define GCE_CTRL_BY_SW				GENMASK(2, 0)
 42#define GCE_DDR_EN				GENMASK(18, 16)
 43
 44#define CMDQ_THR_ACTIVE_SLOT_CYCLES	0x3200
 45#define CMDQ_THR_ENABLED		0x1
 46#define CMDQ_THR_DISABLED		0x0
 47#define CMDQ_THR_SUSPEND		0x1
 48#define CMDQ_THR_RESUME			0x0
 49#define CMDQ_THR_STATUS_SUSPENDED	BIT(1)
 50#define CMDQ_THR_DO_WARM_RESET		BIT(0)
 51#define CMDQ_THR_IRQ_DONE		0x1
 52#define CMDQ_THR_IRQ_ERROR		0x12
 53#define CMDQ_THR_IRQ_EN			(CMDQ_THR_IRQ_ERROR | CMDQ_THR_IRQ_DONE)
 54#define CMDQ_THR_IS_WAITING		BIT(31)
 55
 56#define CMDQ_JUMP_BY_OFFSET		0x10000000
 57#define CMDQ_JUMP_BY_PA			0x10000001
 58
 59struct cmdq_thread {
 60	struct mbox_chan	*chan;
 61	void __iomem		*base;
 62	struct list_head	task_busy_list;
 63	u32			priority;
 64};
 65
 66struct cmdq_task {
 67	struct cmdq		*cmdq;
 68	struct list_head	list_entry;
 69	dma_addr_t		pa_base;
 70	struct cmdq_thread	*thread;
 71	struct cmdq_pkt		*pkt; /* the packet sent from mailbox client */
 72};
 73
 74struct cmdq {
 75	struct mbox_controller	mbox;
 76	void __iomem		*base;
 77	int			irq;
 
 78	u32			irq_mask;
 79	const struct gce_plat	*pdata;
 80	struct cmdq_thread	*thread;
 81	struct clk_bulk_data	clocks[CMDQ_GCE_NUM_MAX];
 82	bool			suspended;
 
 83};
 84
 85struct gce_plat {
 86	u32 thread_nr;
 87	u8 shift;
 88	bool control_by_sw;
 89	bool sw_ddr_en;
 90	u32 gce_num;
 91};
 92
 93static void cmdq_sw_ddr_enable(struct cmdq *cmdq, bool enable)
 94{
 95	WARN_ON(clk_bulk_enable(cmdq->pdata->gce_num, cmdq->clocks));
 96
 97	if (enable)
 98		writel(GCE_DDR_EN | GCE_CTRL_BY_SW, cmdq->base + GCE_GCTL_VALUE);
 99	else
100		writel(GCE_CTRL_BY_SW, cmdq->base + GCE_GCTL_VALUE);
101
102	clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
103}
104
105u8 cmdq_get_shift_pa(struct mbox_chan *chan)
106{
107	struct cmdq *cmdq = container_of(chan->mbox, struct cmdq, mbox);
108
109	return cmdq->pdata->shift;
110}
111EXPORT_SYMBOL(cmdq_get_shift_pa);
112
113static int cmdq_thread_suspend(struct cmdq *cmdq, struct cmdq_thread *thread)
114{
115	u32 status;
116
117	writel(CMDQ_THR_SUSPEND, thread->base + CMDQ_THR_SUSPEND_TASK);
118
119	/* If already disabled, treat as suspended successful. */
120	if (!(readl(thread->base + CMDQ_THR_ENABLE_TASK) & CMDQ_THR_ENABLED))
121		return 0;
122
123	if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_CURR_STATUS,
124			status, status & CMDQ_THR_STATUS_SUSPENDED, 0, 10)) {
125		dev_err(cmdq->mbox.dev, "suspend GCE thread 0x%x failed\n",
126			(u32)(thread->base - cmdq->base));
127		return -EFAULT;
128	}
129
130	return 0;
131}
132
133static void cmdq_thread_resume(struct cmdq_thread *thread)
134{
135	writel(CMDQ_THR_RESUME, thread->base + CMDQ_THR_SUSPEND_TASK);
136}
137
138static void cmdq_init(struct cmdq *cmdq)
139{
140	int i;
141	u32 gctl_regval = 0;
142
143	WARN_ON(clk_bulk_enable(cmdq->pdata->gce_num, cmdq->clocks));
144	if (cmdq->pdata->control_by_sw)
145		gctl_regval = GCE_CTRL_BY_SW;
146	if (cmdq->pdata->sw_ddr_en)
147		gctl_regval |= GCE_DDR_EN;
148
149	if (gctl_regval)
150		writel(gctl_regval, cmdq->base + GCE_GCTL_VALUE);
151
 
152	writel(CMDQ_THR_ACTIVE_SLOT_CYCLES, cmdq->base + CMDQ_THR_SLOT_CYCLES);
153	for (i = 0; i <= CMDQ_MAX_EVENT; i++)
154		writel(i, cmdq->base + CMDQ_SYNC_TOKEN_UPDATE);
155	clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
156}
157
158static int cmdq_thread_reset(struct cmdq *cmdq, struct cmdq_thread *thread)
159{
160	u32 warm_reset;
161
162	writel(CMDQ_THR_DO_WARM_RESET, thread->base + CMDQ_THR_WARM_RESET);
163	if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_WARM_RESET,
164			warm_reset, !(warm_reset & CMDQ_THR_DO_WARM_RESET),
165			0, 10)) {
166		dev_err(cmdq->mbox.dev, "reset GCE thread 0x%x failed\n",
167			(u32)(thread->base - cmdq->base));
168		return -EFAULT;
169	}
170
171	return 0;
172}
173
174static void cmdq_thread_disable(struct cmdq *cmdq, struct cmdq_thread *thread)
175{
176	cmdq_thread_reset(cmdq, thread);
177	writel(CMDQ_THR_DISABLED, thread->base + CMDQ_THR_ENABLE_TASK);
178}
179
180/* notify GCE to re-fetch commands by setting GCE thread PC */
181static void cmdq_thread_invalidate_fetched_data(struct cmdq_thread *thread)
182{
183	writel(readl(thread->base + CMDQ_THR_CURR_ADDR),
184	       thread->base + CMDQ_THR_CURR_ADDR);
185}
186
187static void cmdq_task_insert_into_thread(struct cmdq_task *task)
188{
189	struct device *dev = task->cmdq->mbox.dev;
190	struct cmdq_thread *thread = task->thread;
191	struct cmdq_task *prev_task = list_last_entry(
192			&thread->task_busy_list, typeof(*task), list_entry);
193	u64 *prev_task_base = prev_task->pkt->va_base;
194
195	/* let previous task jump to this task */
196	dma_sync_single_for_cpu(dev, prev_task->pa_base,
197				prev_task->pkt->cmd_buf_size, DMA_TO_DEVICE);
198	prev_task_base[CMDQ_NUM_CMD(prev_task->pkt) - 1] =
199		(u64)CMDQ_JUMP_BY_PA << 32 |
200		(task->pa_base >> task->cmdq->pdata->shift);
201	dma_sync_single_for_device(dev, prev_task->pa_base,
202				   prev_task->pkt->cmd_buf_size, DMA_TO_DEVICE);
203
204	cmdq_thread_invalidate_fetched_data(thread);
205}
206
207static bool cmdq_thread_is_in_wfe(struct cmdq_thread *thread)
208{
209	return readl(thread->base + CMDQ_THR_WAIT_TOKEN) & CMDQ_THR_IS_WAITING;
210}
211
212static void cmdq_task_exec_done(struct cmdq_task *task, int sta)
213{
 
214	struct cmdq_cb_data data;
215
 
216	data.sta = sta;
 
217	data.pkt = task->pkt;
 
 
 
218	mbox_chan_received_data(task->thread->chan, &data);
219
220	list_del(&task->list_entry);
221}
222
223static void cmdq_task_handle_error(struct cmdq_task *task)
224{
225	struct cmdq_thread *thread = task->thread;
226	struct cmdq_task *next_task;
227	struct cmdq *cmdq = task->cmdq;
228
229	dev_err(cmdq->mbox.dev, "task 0x%p error\n", task);
230	WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
231	next_task = list_first_entry_or_null(&thread->task_busy_list,
232			struct cmdq_task, list_entry);
233	if (next_task)
234		writel(next_task->pa_base >> cmdq->pdata->shift,
235		       thread->base + CMDQ_THR_CURR_ADDR);
236	cmdq_thread_resume(thread);
237}
238
239static void cmdq_thread_irq_handler(struct cmdq *cmdq,
240				    struct cmdq_thread *thread)
241{
242	struct cmdq_task *task, *tmp, *curr_task = NULL;
243	u32 curr_pa, irq_flag, task_end_pa;
244	bool err;
245
246	irq_flag = readl(thread->base + CMDQ_THR_IRQ_STATUS);
247	writel(~irq_flag, thread->base + CMDQ_THR_IRQ_STATUS);
248
249	/*
250	 * When ISR call this function, another CPU core could run
251	 * "release task" right before we acquire the spin lock, and thus
252	 * reset / disable this GCE thread, so we need to check the enable
253	 * bit of this GCE thread.
254	 */
255	if (!(readl(thread->base + CMDQ_THR_ENABLE_TASK) & CMDQ_THR_ENABLED))
256		return;
257
258	if (irq_flag & CMDQ_THR_IRQ_ERROR)
259		err = true;
260	else if (irq_flag & CMDQ_THR_IRQ_DONE)
261		err = false;
262	else
263		return;
264
265	curr_pa = readl(thread->base + CMDQ_THR_CURR_ADDR) << cmdq->pdata->shift;
266
267	list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
268				 list_entry) {
269		task_end_pa = task->pa_base + task->pkt->cmd_buf_size;
270		if (curr_pa >= task->pa_base && curr_pa < task_end_pa)
271			curr_task = task;
272
273		if (!curr_task || curr_pa == task_end_pa - CMDQ_INST_SIZE) {
274			cmdq_task_exec_done(task, 0);
275			kfree(task);
276		} else if (err) {
277			cmdq_task_exec_done(task, -ENOEXEC);
278			cmdq_task_handle_error(curr_task);
279			kfree(task);
280		}
281
282		if (curr_task)
283			break;
284	}
285
286	if (list_empty(&thread->task_busy_list)) {
287		cmdq_thread_disable(cmdq, thread);
288		clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
289	}
290}
291
292static irqreturn_t cmdq_irq_handler(int irq, void *dev)
293{
294	struct cmdq *cmdq = dev;
295	unsigned long irq_status, flags = 0L;
296	int bit;
297
298	irq_status = readl(cmdq->base + CMDQ_CURR_IRQ_STATUS) & cmdq->irq_mask;
299	if (!(irq_status ^ cmdq->irq_mask))
300		return IRQ_NONE;
301
302	for_each_clear_bit(bit, &irq_status, cmdq->pdata->thread_nr) {
303		struct cmdq_thread *thread = &cmdq->thread[bit];
304
305		spin_lock_irqsave(&thread->chan->lock, flags);
306		cmdq_thread_irq_handler(cmdq, thread);
307		spin_unlock_irqrestore(&thread->chan->lock, flags);
308	}
309
310	return IRQ_HANDLED;
311}
312
313static int cmdq_suspend(struct device *dev)
314{
315	struct cmdq *cmdq = dev_get_drvdata(dev);
316	struct cmdq_thread *thread;
317	int i;
318	bool task_running = false;
319
320	cmdq->suspended = true;
321
322	for (i = 0; i < cmdq->pdata->thread_nr; i++) {
323		thread = &cmdq->thread[i];
324		if (!list_empty(&thread->task_busy_list)) {
325			task_running = true;
326			break;
327		}
328	}
329
330	if (task_running)
331		dev_warn(dev, "exist running task(s) in suspend\n");
332
333	if (cmdq->pdata->sw_ddr_en)
334		cmdq_sw_ddr_enable(cmdq, false);
335
336	clk_bulk_unprepare(cmdq->pdata->gce_num, cmdq->clocks);
337
338	return 0;
339}
340
341static int cmdq_resume(struct device *dev)
342{
343	struct cmdq *cmdq = dev_get_drvdata(dev);
344
345	WARN_ON(clk_bulk_prepare(cmdq->pdata->gce_num, cmdq->clocks));
346	cmdq->suspended = false;
347
348	if (cmdq->pdata->sw_ddr_en)
349		cmdq_sw_ddr_enable(cmdq, true);
350
351	return 0;
352}
353
354static int cmdq_remove(struct platform_device *pdev)
355{
356	struct cmdq *cmdq = platform_get_drvdata(pdev);
357
358	if (cmdq->pdata->sw_ddr_en)
359		cmdq_sw_ddr_enable(cmdq, false);
360
361	clk_bulk_unprepare(cmdq->pdata->gce_num, cmdq->clocks);
362	return 0;
363}
364
365static int cmdq_mbox_send_data(struct mbox_chan *chan, void *data)
366{
367	struct cmdq_pkt *pkt = (struct cmdq_pkt *)data;
368	struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
369	struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
370	struct cmdq_task *task;
371	unsigned long curr_pa, end_pa;
372
373	/* Client should not flush new tasks if suspended. */
374	WARN_ON(cmdq->suspended);
375
376	task = kzalloc(sizeof(*task), GFP_ATOMIC);
377	if (!task)
378		return -ENOMEM;
379
380	task->cmdq = cmdq;
381	INIT_LIST_HEAD(&task->list_entry);
382	task->pa_base = pkt->pa_base;
383	task->thread = thread;
384	task->pkt = pkt;
385
386	if (list_empty(&thread->task_busy_list)) {
387		WARN_ON(clk_bulk_enable(cmdq->pdata->gce_num, cmdq->clocks));
388
389		/*
390		 * The thread reset will clear thread related register to 0,
391		 * including pc, end, priority, irq, suspend and enable. Thus
392		 * set CMDQ_THR_ENABLED to CMDQ_THR_ENABLE_TASK will enable
393		 * thread and make it running.
394		 */
395		WARN_ON(cmdq_thread_reset(cmdq, thread) < 0);
396
397		writel(task->pa_base >> cmdq->pdata->shift,
398		       thread->base + CMDQ_THR_CURR_ADDR);
399		writel((task->pa_base + pkt->cmd_buf_size) >> cmdq->pdata->shift,
400		       thread->base + CMDQ_THR_END_ADDR);
401
402		writel(thread->priority, thread->base + CMDQ_THR_PRIORITY);
403		writel(CMDQ_THR_IRQ_EN, thread->base + CMDQ_THR_IRQ_ENABLE);
404		writel(CMDQ_THR_ENABLED, thread->base + CMDQ_THR_ENABLE_TASK);
405	} else {
406		WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
407		curr_pa = readl(thread->base + CMDQ_THR_CURR_ADDR) <<
408			cmdq->pdata->shift;
409		end_pa = readl(thread->base + CMDQ_THR_END_ADDR) <<
410			cmdq->pdata->shift;
411		/* check boundary */
412		if (curr_pa == end_pa - CMDQ_INST_SIZE ||
413		    curr_pa == end_pa) {
414			/* set to this task directly */
415			writel(task->pa_base >> cmdq->pdata->shift,
416			       thread->base + CMDQ_THR_CURR_ADDR);
417		} else {
418			cmdq_task_insert_into_thread(task);
419			smp_mb(); /* modify jump before enable thread */
420		}
421		writel((task->pa_base + pkt->cmd_buf_size) >> cmdq->pdata->shift,
422		       thread->base + CMDQ_THR_END_ADDR);
423		cmdq_thread_resume(thread);
424	}
425	list_move_tail(&task->list_entry, &thread->task_busy_list);
426
427	return 0;
428}
429
430static int cmdq_mbox_startup(struct mbox_chan *chan)
431{
432	return 0;
433}
434
435static void cmdq_mbox_shutdown(struct mbox_chan *chan)
436{
437	struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
438	struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
439	struct cmdq_task *task, *tmp;
440	unsigned long flags;
441
442	spin_lock_irqsave(&thread->chan->lock, flags);
443	if (list_empty(&thread->task_busy_list))
444		goto done;
445
446	WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
447
448	/* make sure executed tasks have success callback */
449	cmdq_thread_irq_handler(cmdq, thread);
450	if (list_empty(&thread->task_busy_list))
451		goto done;
452
453	list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
454				 list_entry) {
455		cmdq_task_exec_done(task, -ECONNABORTED);
456		kfree(task);
457	}
458
459	cmdq_thread_disable(cmdq, thread);
460	clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
461
462done:
463	/*
464	 * The thread->task_busy_list empty means thread already disable. The
465	 * cmdq_mbox_send_data() always reset thread which clear disable and
466	 * suspend statue when first pkt send to channel, so there is no need
467	 * to do any operation here, only unlock and leave.
468	 */
469	spin_unlock_irqrestore(&thread->chan->lock, flags);
470}
471
472static int cmdq_mbox_flush(struct mbox_chan *chan, unsigned long timeout)
473{
474	struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
 
475	struct cmdq_cb_data data;
476	struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
477	struct cmdq_task *task, *tmp;
478	unsigned long flags;
479	u32 enable;
480
481	spin_lock_irqsave(&thread->chan->lock, flags);
482	if (list_empty(&thread->task_busy_list))
483		goto out;
484
485	WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
486	if (!cmdq_thread_is_in_wfe(thread))
487		goto wait;
488
489	list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
490				 list_entry) {
 
491		data.sta = -ECONNABORTED;
 
492		data.pkt = task->pkt;
 
 
 
493		mbox_chan_received_data(task->thread->chan, &data);
494		list_del(&task->list_entry);
495		kfree(task);
496	}
497
498	cmdq_thread_resume(thread);
499	cmdq_thread_disable(cmdq, thread);
500	clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
501
502out:
503	spin_unlock_irqrestore(&thread->chan->lock, flags);
504	return 0;
505
506wait:
507	cmdq_thread_resume(thread);
508	spin_unlock_irqrestore(&thread->chan->lock, flags);
509	if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_ENABLE_TASK,
510				      enable, enable == 0, 1, timeout)) {
511		dev_err(cmdq->mbox.dev, "Fail to wait GCE thread 0x%x done\n",
512			(u32)(thread->base - cmdq->base));
513
514		return -EFAULT;
515	}
516	return 0;
517}
518
519static const struct mbox_chan_ops cmdq_mbox_chan_ops = {
520	.send_data = cmdq_mbox_send_data,
521	.startup = cmdq_mbox_startup,
522	.shutdown = cmdq_mbox_shutdown,
523	.flush = cmdq_mbox_flush,
524};
525
526static struct mbox_chan *cmdq_xlate(struct mbox_controller *mbox,
527		const struct of_phandle_args *sp)
528{
529	int ind = sp->args[0];
530	struct cmdq_thread *thread;
531
532	if (ind >= mbox->num_chans)
533		return ERR_PTR(-EINVAL);
534
535	thread = (struct cmdq_thread *)mbox->chans[ind].con_priv;
536	thread->priority = sp->args[1];
537	thread->chan = &mbox->chans[ind];
538
539	return &mbox->chans[ind];
540}
541
542static int cmdq_probe(struct platform_device *pdev)
543{
544	struct device *dev = &pdev->dev;
 
545	struct cmdq *cmdq;
546	int err, i;
547	struct device_node *phandle = dev->of_node;
548	struct device_node *node;
549	int alias_id = 0;
550	static const char * const clk_name = "gce";
551	static const char * const clk_names[] = { "gce0", "gce1" };
552
553	cmdq = devm_kzalloc(dev, sizeof(*cmdq), GFP_KERNEL);
554	if (!cmdq)
555		return -ENOMEM;
556
557	cmdq->base = devm_platform_ioremap_resource(pdev, 0);
 
558	if (IS_ERR(cmdq->base))
559		return PTR_ERR(cmdq->base);
560
561	cmdq->irq = platform_get_irq(pdev, 0);
562	if (cmdq->irq < 0)
563		return cmdq->irq;
564
565	cmdq->pdata = device_get_match_data(dev);
566	if (!cmdq->pdata) {
567		dev_err(dev, "failed to get match data\n");
568		return -EINVAL;
569	}
570
571	cmdq->irq_mask = GENMASK(cmdq->pdata->thread_nr - 1, 0);
 
 
 
 
 
 
 
 
572
573	dev_dbg(dev, "cmdq device: addr:0x%p, va:0x%p, irq:%d\n",
574		dev, cmdq->base, cmdq->irq);
575
576	if (cmdq->pdata->gce_num > 1) {
577		for_each_child_of_node(phandle->parent, node) {
578			alias_id = of_alias_get_id(node, clk_name);
579			if (alias_id >= 0 && alias_id < cmdq->pdata->gce_num) {
580				cmdq->clocks[alias_id].id = clk_names[alias_id];
581				cmdq->clocks[alias_id].clk = of_clk_get(node, 0);
582				if (IS_ERR(cmdq->clocks[alias_id].clk)) {
583					of_node_put(node);
584					return dev_err_probe(dev,
585							     PTR_ERR(cmdq->clocks[alias_id].clk),
586							     "failed to get gce clk: %d\n",
587							     alias_id);
588				}
589			}
590		}
591	} else {
592		cmdq->clocks[alias_id].id = clk_name;
593		cmdq->clocks[alias_id].clk = devm_clk_get(&pdev->dev, clk_name);
594		if (IS_ERR(cmdq->clocks[alias_id].clk)) {
595			return dev_err_probe(dev, PTR_ERR(cmdq->clocks[alias_id].clk),
596					     "failed to get gce clk\n");
597		}
598	}
599
600	cmdq->mbox.dev = dev;
601	cmdq->mbox.chans = devm_kcalloc(dev, cmdq->pdata->thread_nr,
602					sizeof(*cmdq->mbox.chans), GFP_KERNEL);
603	if (!cmdq->mbox.chans)
604		return -ENOMEM;
605
606	cmdq->mbox.num_chans = cmdq->pdata->thread_nr;
607	cmdq->mbox.ops = &cmdq_mbox_chan_ops;
608	cmdq->mbox.of_xlate = cmdq_xlate;
609
610	/* make use of TXDONE_BY_ACK */
611	cmdq->mbox.txdone_irq = false;
612	cmdq->mbox.txdone_poll = false;
613
614	cmdq->thread = devm_kcalloc(dev, cmdq->pdata->thread_nr,
615					sizeof(*cmdq->thread), GFP_KERNEL);
616	if (!cmdq->thread)
617		return -ENOMEM;
618
619	for (i = 0; i < cmdq->pdata->thread_nr; i++) {
620		cmdq->thread[i].base = cmdq->base + CMDQ_THR_BASE +
621				CMDQ_THR_SIZE * i;
622		INIT_LIST_HEAD(&cmdq->thread[i].task_busy_list);
623		cmdq->mbox.chans[i].con_priv = (void *)&cmdq->thread[i];
624	}
625
626	err = devm_mbox_controller_register(dev, &cmdq->mbox);
627	if (err < 0) {
628		dev_err(dev, "failed to register mailbox: %d\n", err);
629		return err;
630	}
631
632	platform_set_drvdata(pdev, cmdq);
633
634	WARN_ON(clk_bulk_prepare(cmdq->pdata->gce_num, cmdq->clocks));
635
636	cmdq_init(cmdq);
637
638	err = devm_request_irq(dev, cmdq->irq, cmdq_irq_handler, IRQF_SHARED,
639			       "mtk_cmdq", cmdq);
640	if (err < 0) {
641		dev_err(dev, "failed to register ISR (%d)\n", err);
642		return err;
643	}
644
645	return 0;
646}
647
648static const struct dev_pm_ops cmdq_pm_ops = {
649	.suspend = cmdq_suspend,
650	.resume = cmdq_resume,
651};
652
653static const struct gce_plat gce_plat_v2 = {
654	.thread_nr = 16,
655	.shift = 0,
656	.control_by_sw = false,
657	.gce_num = 1
658};
659
660static const struct gce_plat gce_plat_v3 = {
661	.thread_nr = 24,
662	.shift = 0,
663	.control_by_sw = false,
664	.gce_num = 1
665};
666
667static const struct gce_plat gce_plat_v4 = {
668	.thread_nr = 24,
669	.shift = 3,
670	.control_by_sw = false,
671	.gce_num = 1
672};
673
674static const struct gce_plat gce_plat_v5 = {
675	.thread_nr = 24,
676	.shift = 3,
677	.control_by_sw = true,
678	.gce_num = 1
679};
680
681static const struct gce_plat gce_plat_v6 = {
682	.thread_nr = 24,
683	.shift = 3,
684	.control_by_sw = true,
685	.gce_num = 2
686};
687
688static const struct gce_plat gce_plat_v7 = {
689	.thread_nr = 24,
690	.shift = 3,
691	.control_by_sw = true,
692	.sw_ddr_en = true,
693	.gce_num = 1
694};
695
696static const struct of_device_id cmdq_of_ids[] = {
697	{.compatible = "mediatek,mt8173-gce", .data = (void *)&gce_plat_v2},
698	{.compatible = "mediatek,mt8183-gce", .data = (void *)&gce_plat_v3},
699	{.compatible = "mediatek,mt8186-gce", .data = (void *)&gce_plat_v7},
700	{.compatible = "mediatek,mt6779-gce", .data = (void *)&gce_plat_v4},
701	{.compatible = "mediatek,mt8192-gce", .data = (void *)&gce_plat_v5},
702	{.compatible = "mediatek,mt8195-gce", .data = (void *)&gce_plat_v6},
703	{}
704};
705
706static struct platform_driver cmdq_drv = {
707	.probe = cmdq_probe,
708	.remove = cmdq_remove,
709	.driver = {
710		.name = "mtk_cmdq",
711		.pm = &cmdq_pm_ops,
712		.of_match_table = cmdq_of_ids,
713	}
714};
715
716static int __init cmdq_drv_init(void)
717{
718	return platform_driver_register(&cmdq_drv);
719}
720
721static void __exit cmdq_drv_exit(void)
722{
723	platform_driver_unregister(&cmdq_drv);
724}
725
726subsys_initcall(cmdq_drv_init);
727module_exit(cmdq_drv_exit);
728
729MODULE_LICENSE("GPL v2");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2//
  3// Copyright (c) 2018 MediaTek Inc.
  4
  5#include <linux/bitops.h>
  6#include <linux/clk.h>
  7#include <linux/clk-provider.h>
  8#include <linux/dma-mapping.h>
  9#include <linux/errno.h>
 10#include <linux/interrupt.h>
 11#include <linux/io.h>
 12#include <linux/iopoll.h>
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/platform_device.h>
 16#include <linux/mailbox_controller.h>
 17#include <linux/mailbox/mtk-cmdq-mailbox.h>
 18#include <linux/of_device.h>
 19
 20#define CMDQ_OP_CODE_MASK		(0xff << CMDQ_OP_CODE_SHIFT)
 21#define CMDQ_NUM_CMD(t)			(t->cmd_buf_size / CMDQ_INST_SIZE)
 
 22
 23#define CMDQ_CURR_IRQ_STATUS		0x10
 24#define CMDQ_SYNC_TOKEN_UPDATE		0x68
 25#define CMDQ_THR_SLOT_CYCLES		0x30
 26#define CMDQ_THR_BASE			0x100
 27#define CMDQ_THR_SIZE			0x80
 28#define CMDQ_THR_WARM_RESET		0x00
 29#define CMDQ_THR_ENABLE_TASK		0x04
 30#define CMDQ_THR_SUSPEND_TASK		0x08
 31#define CMDQ_THR_CURR_STATUS		0x0c
 32#define CMDQ_THR_IRQ_STATUS		0x10
 33#define CMDQ_THR_IRQ_ENABLE		0x14
 34#define CMDQ_THR_CURR_ADDR		0x20
 35#define CMDQ_THR_END_ADDR		0x24
 36#define CMDQ_THR_WAIT_TOKEN		0x30
 37#define CMDQ_THR_PRIORITY		0x40
 38
 
 
 
 
 39#define CMDQ_THR_ACTIVE_SLOT_CYCLES	0x3200
 40#define CMDQ_THR_ENABLED		0x1
 41#define CMDQ_THR_DISABLED		0x0
 42#define CMDQ_THR_SUSPEND		0x1
 43#define CMDQ_THR_RESUME			0x0
 44#define CMDQ_THR_STATUS_SUSPENDED	BIT(1)
 45#define CMDQ_THR_DO_WARM_RESET		BIT(0)
 46#define CMDQ_THR_IRQ_DONE		0x1
 47#define CMDQ_THR_IRQ_ERROR		0x12
 48#define CMDQ_THR_IRQ_EN			(CMDQ_THR_IRQ_ERROR | CMDQ_THR_IRQ_DONE)
 49#define CMDQ_THR_IS_WAITING		BIT(31)
 50
 51#define CMDQ_JUMP_BY_OFFSET		0x10000000
 52#define CMDQ_JUMP_BY_PA			0x10000001
 53
 54struct cmdq_thread {
 55	struct mbox_chan	*chan;
 56	void __iomem		*base;
 57	struct list_head	task_busy_list;
 58	u32			priority;
 59};
 60
 61struct cmdq_task {
 62	struct cmdq		*cmdq;
 63	struct list_head	list_entry;
 64	dma_addr_t		pa_base;
 65	struct cmdq_thread	*thread;
 66	struct cmdq_pkt		*pkt; /* the packet sent from mailbox client */
 67};
 68
 69struct cmdq {
 70	struct mbox_controller	mbox;
 71	void __iomem		*base;
 72	int			irq;
 73	u32			thread_nr;
 74	u32			irq_mask;
 
 75	struct cmdq_thread	*thread;
 76	struct clk		*clock;
 77	bool			suspended;
 78	u8			shift_pa;
 79};
 80
 81struct gce_plat {
 82	u32 thread_nr;
 83	u8 shift;
 
 
 
 84};
 85
 
 
 
 
 
 
 
 
 
 
 
 
 86u8 cmdq_get_shift_pa(struct mbox_chan *chan)
 87{
 88	struct cmdq *cmdq = container_of(chan->mbox, struct cmdq, mbox);
 89
 90	return cmdq->shift_pa;
 91}
 92EXPORT_SYMBOL(cmdq_get_shift_pa);
 93
 94static int cmdq_thread_suspend(struct cmdq *cmdq, struct cmdq_thread *thread)
 95{
 96	u32 status;
 97
 98	writel(CMDQ_THR_SUSPEND, thread->base + CMDQ_THR_SUSPEND_TASK);
 99
100	/* If already disabled, treat as suspended successful. */
101	if (!(readl(thread->base + CMDQ_THR_ENABLE_TASK) & CMDQ_THR_ENABLED))
102		return 0;
103
104	if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_CURR_STATUS,
105			status, status & CMDQ_THR_STATUS_SUSPENDED, 0, 10)) {
106		dev_err(cmdq->mbox.dev, "suspend GCE thread 0x%x failed\n",
107			(u32)(thread->base - cmdq->base));
108		return -EFAULT;
109	}
110
111	return 0;
112}
113
114static void cmdq_thread_resume(struct cmdq_thread *thread)
115{
116	writel(CMDQ_THR_RESUME, thread->base + CMDQ_THR_SUSPEND_TASK);
117}
118
119static void cmdq_init(struct cmdq *cmdq)
120{
121	int i;
 
 
 
 
 
 
 
 
 
 
122
123	WARN_ON(clk_enable(cmdq->clock) < 0);
124	writel(CMDQ_THR_ACTIVE_SLOT_CYCLES, cmdq->base + CMDQ_THR_SLOT_CYCLES);
125	for (i = 0; i <= CMDQ_MAX_EVENT; i++)
126		writel(i, cmdq->base + CMDQ_SYNC_TOKEN_UPDATE);
127	clk_disable(cmdq->clock);
128}
129
130static int cmdq_thread_reset(struct cmdq *cmdq, struct cmdq_thread *thread)
131{
132	u32 warm_reset;
133
134	writel(CMDQ_THR_DO_WARM_RESET, thread->base + CMDQ_THR_WARM_RESET);
135	if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_WARM_RESET,
136			warm_reset, !(warm_reset & CMDQ_THR_DO_WARM_RESET),
137			0, 10)) {
138		dev_err(cmdq->mbox.dev, "reset GCE thread 0x%x failed\n",
139			(u32)(thread->base - cmdq->base));
140		return -EFAULT;
141	}
142
143	return 0;
144}
145
146static void cmdq_thread_disable(struct cmdq *cmdq, struct cmdq_thread *thread)
147{
148	cmdq_thread_reset(cmdq, thread);
149	writel(CMDQ_THR_DISABLED, thread->base + CMDQ_THR_ENABLE_TASK);
150}
151
152/* notify GCE to re-fetch commands by setting GCE thread PC */
153static void cmdq_thread_invalidate_fetched_data(struct cmdq_thread *thread)
154{
155	writel(readl(thread->base + CMDQ_THR_CURR_ADDR),
156	       thread->base + CMDQ_THR_CURR_ADDR);
157}
158
159static void cmdq_task_insert_into_thread(struct cmdq_task *task)
160{
161	struct device *dev = task->cmdq->mbox.dev;
162	struct cmdq_thread *thread = task->thread;
163	struct cmdq_task *prev_task = list_last_entry(
164			&thread->task_busy_list, typeof(*task), list_entry);
165	u64 *prev_task_base = prev_task->pkt->va_base;
166
167	/* let previous task jump to this task */
168	dma_sync_single_for_cpu(dev, prev_task->pa_base,
169				prev_task->pkt->cmd_buf_size, DMA_TO_DEVICE);
170	prev_task_base[CMDQ_NUM_CMD(prev_task->pkt) - 1] =
171		(u64)CMDQ_JUMP_BY_PA << 32 |
172		(task->pa_base >> task->cmdq->shift_pa);
173	dma_sync_single_for_device(dev, prev_task->pa_base,
174				   prev_task->pkt->cmd_buf_size, DMA_TO_DEVICE);
175
176	cmdq_thread_invalidate_fetched_data(thread);
177}
178
179static bool cmdq_thread_is_in_wfe(struct cmdq_thread *thread)
180{
181	return readl(thread->base + CMDQ_THR_WAIT_TOKEN) & CMDQ_THR_IS_WAITING;
182}
183
184static void cmdq_task_exec_done(struct cmdq_task *task, int sta)
185{
186	struct cmdq_task_cb *cb = &task->pkt->async_cb;
187	struct cmdq_cb_data data;
188
189	WARN_ON(cb->cb == (cmdq_async_flush_cb)NULL);
190	data.sta = sta;
191	data.data = cb->data;
192	data.pkt = task->pkt;
193	if (cb->cb)
194		cb->cb(data);
195
196	mbox_chan_received_data(task->thread->chan, &data);
197
198	list_del(&task->list_entry);
199}
200
201static void cmdq_task_handle_error(struct cmdq_task *task)
202{
203	struct cmdq_thread *thread = task->thread;
204	struct cmdq_task *next_task;
205	struct cmdq *cmdq = task->cmdq;
206
207	dev_err(cmdq->mbox.dev, "task 0x%p error\n", task);
208	WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
209	next_task = list_first_entry_or_null(&thread->task_busy_list,
210			struct cmdq_task, list_entry);
211	if (next_task)
212		writel(next_task->pa_base >> cmdq->shift_pa,
213		       thread->base + CMDQ_THR_CURR_ADDR);
214	cmdq_thread_resume(thread);
215}
216
217static void cmdq_thread_irq_handler(struct cmdq *cmdq,
218				    struct cmdq_thread *thread)
219{
220	struct cmdq_task *task, *tmp, *curr_task = NULL;
221	u32 curr_pa, irq_flag, task_end_pa;
222	bool err;
223
224	irq_flag = readl(thread->base + CMDQ_THR_IRQ_STATUS);
225	writel(~irq_flag, thread->base + CMDQ_THR_IRQ_STATUS);
226
227	/*
228	 * When ISR call this function, another CPU core could run
229	 * "release task" right before we acquire the spin lock, and thus
230	 * reset / disable this GCE thread, so we need to check the enable
231	 * bit of this GCE thread.
232	 */
233	if (!(readl(thread->base + CMDQ_THR_ENABLE_TASK) & CMDQ_THR_ENABLED))
234		return;
235
236	if (irq_flag & CMDQ_THR_IRQ_ERROR)
237		err = true;
238	else if (irq_flag & CMDQ_THR_IRQ_DONE)
239		err = false;
240	else
241		return;
242
243	curr_pa = readl(thread->base + CMDQ_THR_CURR_ADDR) << cmdq->shift_pa;
244
245	list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
246				 list_entry) {
247		task_end_pa = task->pa_base + task->pkt->cmd_buf_size;
248		if (curr_pa >= task->pa_base && curr_pa < task_end_pa)
249			curr_task = task;
250
251		if (!curr_task || curr_pa == task_end_pa - CMDQ_INST_SIZE) {
252			cmdq_task_exec_done(task, 0);
253			kfree(task);
254		} else if (err) {
255			cmdq_task_exec_done(task, -ENOEXEC);
256			cmdq_task_handle_error(curr_task);
257			kfree(task);
258		}
259
260		if (curr_task)
261			break;
262	}
263
264	if (list_empty(&thread->task_busy_list)) {
265		cmdq_thread_disable(cmdq, thread);
266		clk_disable(cmdq->clock);
267	}
268}
269
270static irqreturn_t cmdq_irq_handler(int irq, void *dev)
271{
272	struct cmdq *cmdq = dev;
273	unsigned long irq_status, flags = 0L;
274	int bit;
275
276	irq_status = readl(cmdq->base + CMDQ_CURR_IRQ_STATUS) & cmdq->irq_mask;
277	if (!(irq_status ^ cmdq->irq_mask))
278		return IRQ_NONE;
279
280	for_each_clear_bit(bit, &irq_status, cmdq->thread_nr) {
281		struct cmdq_thread *thread = &cmdq->thread[bit];
282
283		spin_lock_irqsave(&thread->chan->lock, flags);
284		cmdq_thread_irq_handler(cmdq, thread);
285		spin_unlock_irqrestore(&thread->chan->lock, flags);
286	}
287
288	return IRQ_HANDLED;
289}
290
291static int cmdq_suspend(struct device *dev)
292{
293	struct cmdq *cmdq = dev_get_drvdata(dev);
294	struct cmdq_thread *thread;
295	int i;
296	bool task_running = false;
297
298	cmdq->suspended = true;
299
300	for (i = 0; i < cmdq->thread_nr; i++) {
301		thread = &cmdq->thread[i];
302		if (!list_empty(&thread->task_busy_list)) {
303			task_running = true;
304			break;
305		}
306	}
307
308	if (task_running)
309		dev_warn(dev, "exist running task(s) in suspend\n");
310
311	clk_unprepare(cmdq->clock);
 
 
 
312
313	return 0;
314}
315
316static int cmdq_resume(struct device *dev)
317{
318	struct cmdq *cmdq = dev_get_drvdata(dev);
319
320	WARN_ON(clk_prepare(cmdq->clock) < 0);
321	cmdq->suspended = false;
 
 
 
 
322	return 0;
323}
324
325static int cmdq_remove(struct platform_device *pdev)
326{
327	struct cmdq *cmdq = platform_get_drvdata(pdev);
328
329	clk_unprepare(cmdq->clock);
 
330
 
331	return 0;
332}
333
334static int cmdq_mbox_send_data(struct mbox_chan *chan, void *data)
335{
336	struct cmdq_pkt *pkt = (struct cmdq_pkt *)data;
337	struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
338	struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
339	struct cmdq_task *task;
340	unsigned long curr_pa, end_pa;
341
342	/* Client should not flush new tasks if suspended. */
343	WARN_ON(cmdq->suspended);
344
345	task = kzalloc(sizeof(*task), GFP_ATOMIC);
346	if (!task)
347		return -ENOMEM;
348
349	task->cmdq = cmdq;
350	INIT_LIST_HEAD(&task->list_entry);
351	task->pa_base = pkt->pa_base;
352	task->thread = thread;
353	task->pkt = pkt;
354
355	if (list_empty(&thread->task_busy_list)) {
356		WARN_ON(clk_enable(cmdq->clock) < 0);
 
357		/*
358		 * The thread reset will clear thread related register to 0,
359		 * including pc, end, priority, irq, suspend and enable. Thus
360		 * set CMDQ_THR_ENABLED to CMDQ_THR_ENABLE_TASK will enable
361		 * thread and make it running.
362		 */
363		WARN_ON(cmdq_thread_reset(cmdq, thread) < 0);
364
365		writel(task->pa_base >> cmdq->shift_pa,
366		       thread->base + CMDQ_THR_CURR_ADDR);
367		writel((task->pa_base + pkt->cmd_buf_size) >> cmdq->shift_pa,
368		       thread->base + CMDQ_THR_END_ADDR);
369
370		writel(thread->priority, thread->base + CMDQ_THR_PRIORITY);
371		writel(CMDQ_THR_IRQ_EN, thread->base + CMDQ_THR_IRQ_ENABLE);
372		writel(CMDQ_THR_ENABLED, thread->base + CMDQ_THR_ENABLE_TASK);
373	} else {
374		WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
375		curr_pa = readl(thread->base + CMDQ_THR_CURR_ADDR) <<
376			cmdq->shift_pa;
377		end_pa = readl(thread->base + CMDQ_THR_END_ADDR) <<
378			cmdq->shift_pa;
379		/* check boundary */
380		if (curr_pa == end_pa - CMDQ_INST_SIZE ||
381		    curr_pa == end_pa) {
382			/* set to this task directly */
383			writel(task->pa_base >> cmdq->shift_pa,
384			       thread->base + CMDQ_THR_CURR_ADDR);
385		} else {
386			cmdq_task_insert_into_thread(task);
387			smp_mb(); /* modify jump before enable thread */
388		}
389		writel((task->pa_base + pkt->cmd_buf_size) >> cmdq->shift_pa,
390		       thread->base + CMDQ_THR_END_ADDR);
391		cmdq_thread_resume(thread);
392	}
393	list_move_tail(&task->list_entry, &thread->task_busy_list);
394
395	return 0;
396}
397
398static int cmdq_mbox_startup(struct mbox_chan *chan)
399{
400	return 0;
401}
402
403static void cmdq_mbox_shutdown(struct mbox_chan *chan)
404{
405	struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
406	struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
407	struct cmdq_task *task, *tmp;
408	unsigned long flags;
409
410	spin_lock_irqsave(&thread->chan->lock, flags);
411	if (list_empty(&thread->task_busy_list))
412		goto done;
413
414	WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
415
416	/* make sure executed tasks have success callback */
417	cmdq_thread_irq_handler(cmdq, thread);
418	if (list_empty(&thread->task_busy_list))
419		goto done;
420
421	list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
422				 list_entry) {
423		cmdq_task_exec_done(task, -ECONNABORTED);
424		kfree(task);
425	}
426
427	cmdq_thread_disable(cmdq, thread);
428	clk_disable(cmdq->clock);
 
429done:
430	/*
431	 * The thread->task_busy_list empty means thread already disable. The
432	 * cmdq_mbox_send_data() always reset thread which clear disable and
433	 * suspend statue when first pkt send to channel, so there is no need
434	 * to do any operation here, only unlock and leave.
435	 */
436	spin_unlock_irqrestore(&thread->chan->lock, flags);
437}
438
439static int cmdq_mbox_flush(struct mbox_chan *chan, unsigned long timeout)
440{
441	struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
442	struct cmdq_task_cb *cb;
443	struct cmdq_cb_data data;
444	struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
445	struct cmdq_task *task, *tmp;
446	unsigned long flags;
447	u32 enable;
448
449	spin_lock_irqsave(&thread->chan->lock, flags);
450	if (list_empty(&thread->task_busy_list))
451		goto out;
452
453	WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
454	if (!cmdq_thread_is_in_wfe(thread))
455		goto wait;
456
457	list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
458				 list_entry) {
459		cb = &task->pkt->async_cb;
460		data.sta = -ECONNABORTED;
461		data.data = cb->data;
462		data.pkt = task->pkt;
463		if (cb->cb)
464			cb->cb(data);
465
466		mbox_chan_received_data(task->thread->chan, &data);
467		list_del(&task->list_entry);
468		kfree(task);
469	}
470
471	cmdq_thread_resume(thread);
472	cmdq_thread_disable(cmdq, thread);
473	clk_disable(cmdq->clock);
474
475out:
476	spin_unlock_irqrestore(&thread->chan->lock, flags);
477	return 0;
478
479wait:
480	cmdq_thread_resume(thread);
481	spin_unlock_irqrestore(&thread->chan->lock, flags);
482	if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_ENABLE_TASK,
483				      enable, enable == 0, 1, timeout)) {
484		dev_err(cmdq->mbox.dev, "Fail to wait GCE thread 0x%x done\n",
485			(u32)(thread->base - cmdq->base));
486
487		return -EFAULT;
488	}
489	return 0;
490}
491
492static const struct mbox_chan_ops cmdq_mbox_chan_ops = {
493	.send_data = cmdq_mbox_send_data,
494	.startup = cmdq_mbox_startup,
495	.shutdown = cmdq_mbox_shutdown,
496	.flush = cmdq_mbox_flush,
497};
498
499static struct mbox_chan *cmdq_xlate(struct mbox_controller *mbox,
500		const struct of_phandle_args *sp)
501{
502	int ind = sp->args[0];
503	struct cmdq_thread *thread;
504
505	if (ind >= mbox->num_chans)
506		return ERR_PTR(-EINVAL);
507
508	thread = (struct cmdq_thread *)mbox->chans[ind].con_priv;
509	thread->priority = sp->args[1];
510	thread->chan = &mbox->chans[ind];
511
512	return &mbox->chans[ind];
513}
514
515static int cmdq_probe(struct platform_device *pdev)
516{
517	struct device *dev = &pdev->dev;
518	struct resource *res;
519	struct cmdq *cmdq;
520	int err, i;
521	struct gce_plat *plat_data;
 
 
 
 
522
523	cmdq = devm_kzalloc(dev, sizeof(*cmdq), GFP_KERNEL);
524	if (!cmdq)
525		return -ENOMEM;
526
527	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
528	cmdq->base = devm_ioremap_resource(dev, res);
529	if (IS_ERR(cmdq->base))
530		return PTR_ERR(cmdq->base);
531
532	cmdq->irq = platform_get_irq(pdev, 0);
533	if (cmdq->irq < 0)
534		return cmdq->irq;
535
536	plat_data = (struct gce_plat *)of_device_get_match_data(dev);
537	if (!plat_data) {
538		dev_err(dev, "failed to get match data\n");
539		return -EINVAL;
540	}
541
542	cmdq->thread_nr = plat_data->thread_nr;
543	cmdq->shift_pa = plat_data->shift;
544	cmdq->irq_mask = GENMASK(cmdq->thread_nr - 1, 0);
545	err = devm_request_irq(dev, cmdq->irq, cmdq_irq_handler, IRQF_SHARED,
546			       "mtk_cmdq", cmdq);
547	if (err < 0) {
548		dev_err(dev, "failed to register ISR (%d)\n", err);
549		return err;
550	}
551
552	dev_dbg(dev, "cmdq device: addr:0x%p, va:0x%p, irq:%d\n",
553		dev, cmdq->base, cmdq->irq);
554
555	cmdq->clock = devm_clk_get(dev, "gce");
556	if (IS_ERR(cmdq->clock)) {
557		dev_err(dev, "failed to get gce clk\n");
558		return PTR_ERR(cmdq->clock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
559	}
560
561	cmdq->mbox.dev = dev;
562	cmdq->mbox.chans = devm_kcalloc(dev, cmdq->thread_nr,
563					sizeof(*cmdq->mbox.chans), GFP_KERNEL);
564	if (!cmdq->mbox.chans)
565		return -ENOMEM;
566
567	cmdq->mbox.num_chans = cmdq->thread_nr;
568	cmdq->mbox.ops = &cmdq_mbox_chan_ops;
569	cmdq->mbox.of_xlate = cmdq_xlate;
570
571	/* make use of TXDONE_BY_ACK */
572	cmdq->mbox.txdone_irq = false;
573	cmdq->mbox.txdone_poll = false;
574
575	cmdq->thread = devm_kcalloc(dev, cmdq->thread_nr,
576					sizeof(*cmdq->thread), GFP_KERNEL);
577	if (!cmdq->thread)
578		return -ENOMEM;
579
580	for (i = 0; i < cmdq->thread_nr; i++) {
581		cmdq->thread[i].base = cmdq->base + CMDQ_THR_BASE +
582				CMDQ_THR_SIZE * i;
583		INIT_LIST_HEAD(&cmdq->thread[i].task_busy_list);
584		cmdq->mbox.chans[i].con_priv = (void *)&cmdq->thread[i];
585	}
586
587	err = devm_mbox_controller_register(dev, &cmdq->mbox);
588	if (err < 0) {
589		dev_err(dev, "failed to register mailbox: %d\n", err);
590		return err;
591	}
592
593	platform_set_drvdata(pdev, cmdq);
594	WARN_ON(clk_prepare(cmdq->clock) < 0);
 
595
596	cmdq_init(cmdq);
597
 
 
 
 
 
 
 
598	return 0;
599}
600
601static const struct dev_pm_ops cmdq_pm_ops = {
602	.suspend = cmdq_suspend,
603	.resume = cmdq_resume,
604};
605
606static const struct gce_plat gce_plat_v2 = {.thread_nr = 16};
607static const struct gce_plat gce_plat_v3 = {.thread_nr = 24};
608static const struct gce_plat gce_plat_v4 = {.thread_nr = 24, .shift = 3};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
609
610static const struct of_device_id cmdq_of_ids[] = {
611	{.compatible = "mediatek,mt8173-gce", .data = (void *)&gce_plat_v2},
612	{.compatible = "mediatek,mt8183-gce", .data = (void *)&gce_plat_v3},
 
613	{.compatible = "mediatek,mt6779-gce", .data = (void *)&gce_plat_v4},
 
 
614	{}
615};
616
617static struct platform_driver cmdq_drv = {
618	.probe = cmdq_probe,
619	.remove = cmdq_remove,
620	.driver = {
621		.name = "mtk_cmdq",
622		.pm = &cmdq_pm_ops,
623		.of_match_table = cmdq_of_ids,
624	}
625};
626
627static int __init cmdq_drv_init(void)
628{
629	return platform_driver_register(&cmdq_drv);
630}
631
632static void __exit cmdq_drv_exit(void)
633{
634	platform_driver_unregister(&cmdq_drv);
635}
636
637subsys_initcall(cmdq_drv_init);
638module_exit(cmdq_drv_exit);
639
640MODULE_LICENSE("GPL v2");