xref: /openbmc/linux/drivers/gpu/drm/exynos/exynos_drm_g2d.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
19f06080fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d7f1642cSJoonyoung Shim /*
3d7f1642cSJoonyoung Shim  * Copyright (C) 2012 Samsung Electronics Co.Ltd
4d7f1642cSJoonyoung Shim  * Authors: Joonyoung Shim <jy0922.shim@samsung.com>
5d7f1642cSJoonyoung Shim  */
6d7f1642cSJoonyoung Shim 
78c27cc5bSXiyu Yang #include <linux/refcount.h>
8d7f1642cSJoonyoung Shim #include <linux/clk.h>
9eb4d9796SMarek Szyprowski #include <linux/component.h>
102bda34d7SSam Ravnborg #include <linux/delay.h>
112bda34d7SSam Ravnborg #include <linux/dma-mapping.h>
12d7f1642cSJoonyoung Shim #include <linux/err.h>
13d7f1642cSJoonyoung Shim #include <linux/interrupt.h>
14d7f1642cSJoonyoung Shim #include <linux/io.h>
152bda34d7SSam Ravnborg #include <linux/kernel.h>
162bda34d7SSam Ravnborg #include <linux/of.h>
17d7f1642cSJoonyoung Shim #include <linux/platform_device.h>
18d7f1642cSJoonyoung Shim #include <linux/pm_runtime.h>
19d7f1642cSJoonyoung Shim #include <linux/slab.h>
202bda34d7SSam Ravnborg #include <linux/uaccess.h>
21d7f1642cSJoonyoung Shim #include <linux/workqueue.h>
22d7f1642cSJoonyoung Shim 
232bda34d7SSam Ravnborg #include <drm/drm_file.h>
24760285e7SDavid Howells #include <drm/exynos_drm.h>
252bda34d7SSam Ravnborg 
26d7f1642cSJoonyoung Shim #include "exynos_drm_drv.h"
27e30655d0SMark Brown #include "exynos_drm_g2d.h"
28d7f1642cSJoonyoung Shim #include "exynos_drm_gem.h"
29d7f1642cSJoonyoung Shim 
30d7f1642cSJoonyoung Shim #define G2D_HW_MAJOR_VER		4
31d7f1642cSJoonyoung Shim #define G2D_HW_MINOR_VER		1
32d7f1642cSJoonyoung Shim 
33d7f1642cSJoonyoung Shim /* vaild register range set from user: 0x0104 ~ 0x0880 */
34d7f1642cSJoonyoung Shim #define G2D_VALID_START			0x0104
35d7f1642cSJoonyoung Shim #define G2D_VALID_END			0x0880
36d7f1642cSJoonyoung Shim 
37d7f1642cSJoonyoung Shim /* general registers */
38d7f1642cSJoonyoung Shim #define G2D_SOFT_RESET			0x0000
39d7f1642cSJoonyoung Shim #define G2D_INTEN			0x0004
40d7f1642cSJoonyoung Shim #define G2D_INTC_PEND			0x000C
41d7f1642cSJoonyoung Shim #define G2D_DMA_SFR_BASE_ADDR		0x0080
42d7f1642cSJoonyoung Shim #define G2D_DMA_COMMAND			0x0084
43d7f1642cSJoonyoung Shim #define G2D_DMA_STATUS			0x008C
44d7f1642cSJoonyoung Shim #define G2D_DMA_HOLD_CMD		0x0090
45d7f1642cSJoonyoung Shim 
46d7f1642cSJoonyoung Shim /* command registers */
47d7f1642cSJoonyoung Shim #define G2D_BITBLT_START		0x0100
48d7f1642cSJoonyoung Shim 
49d7f1642cSJoonyoung Shim /* registers for base address */
50d7f1642cSJoonyoung Shim #define G2D_SRC_BASE_ADDR		0x0304
51ed4dc271STobias Jakobi #define G2D_SRC_STRIDE			0x0308
522dec17c7SYoungJun Cho #define G2D_SRC_COLOR_MODE		0x030C
532dec17c7SYoungJun Cho #define G2D_SRC_LEFT_TOP		0x0310
542dec17c7SYoungJun Cho #define G2D_SRC_RIGHT_BOTTOM		0x0314
55d7f1642cSJoonyoung Shim #define G2D_SRC_PLANE2_BASE_ADDR	0x0318
56d7f1642cSJoonyoung Shim #define G2D_DST_BASE_ADDR		0x0404
57ed4dc271STobias Jakobi #define G2D_DST_STRIDE			0x0408
582dec17c7SYoungJun Cho #define G2D_DST_COLOR_MODE		0x040C
592dec17c7SYoungJun Cho #define G2D_DST_LEFT_TOP		0x0410
602dec17c7SYoungJun Cho #define G2D_DST_RIGHT_BOTTOM		0x0414
61d7f1642cSJoonyoung Shim #define G2D_DST_PLANE2_BASE_ADDR	0x0418
62d7f1642cSJoonyoung Shim #define G2D_PAT_BASE_ADDR		0x0500
63d7f1642cSJoonyoung Shim #define G2D_MSK_BASE_ADDR		0x0520
64d7f1642cSJoonyoung Shim 
65d7f1642cSJoonyoung Shim /* G2D_SOFT_RESET */
66d7f1642cSJoonyoung Shim #define G2D_SFRCLEAR			(1 << 1)
67d7f1642cSJoonyoung Shim #define G2D_R				(1 << 0)
68d7f1642cSJoonyoung Shim 
69d7f1642cSJoonyoung Shim /* G2D_INTEN */
70d7f1642cSJoonyoung Shim #define G2D_INTEN_ACF			(1 << 3)
71d7f1642cSJoonyoung Shim #define G2D_INTEN_UCF			(1 << 2)
72d7f1642cSJoonyoung Shim #define G2D_INTEN_GCF			(1 << 1)
73d7f1642cSJoonyoung Shim #define G2D_INTEN_SCF			(1 << 0)
74d7f1642cSJoonyoung Shim 
75d7f1642cSJoonyoung Shim /* G2D_INTC_PEND */
76d7f1642cSJoonyoung Shim #define G2D_INTP_ACMD_FIN		(1 << 3)
77d7f1642cSJoonyoung Shim #define G2D_INTP_UCMD_FIN		(1 << 2)
78d7f1642cSJoonyoung Shim #define G2D_INTP_GCMD_FIN		(1 << 1)
79d7f1642cSJoonyoung Shim #define G2D_INTP_SCMD_FIN		(1 << 0)
80d7f1642cSJoonyoung Shim 
81d7f1642cSJoonyoung Shim /* G2D_DMA_COMMAND */
82d7f1642cSJoonyoung Shim #define G2D_DMA_HALT			(1 << 2)
83d7f1642cSJoonyoung Shim #define G2D_DMA_CONTINUE		(1 << 1)
84d7f1642cSJoonyoung Shim #define G2D_DMA_START			(1 << 0)
85d7f1642cSJoonyoung Shim 
86d7f1642cSJoonyoung Shim /* G2D_DMA_STATUS */
87d7f1642cSJoonyoung Shim #define G2D_DMA_LIST_DONE_COUNT		(0xFF << 17)
88d7f1642cSJoonyoung Shim #define G2D_DMA_BITBLT_DONE_COUNT	(0xFFFF << 1)
89d7f1642cSJoonyoung Shim #define G2D_DMA_DONE			(1 << 0)
90d7f1642cSJoonyoung Shim #define G2D_DMA_LIST_DONE_COUNT_OFFSET	17
91d7f1642cSJoonyoung Shim 
92d7f1642cSJoonyoung Shim /* G2D_DMA_HOLD_CMD */
937ad01814SYoungJun Cho #define G2D_USER_HOLD			(1 << 2)
94d7f1642cSJoonyoung Shim #define G2D_LIST_HOLD			(1 << 1)
95d7f1642cSJoonyoung Shim #define G2D_BITBLT_HOLD			(1 << 0)
96d7f1642cSJoonyoung Shim 
97d7f1642cSJoonyoung Shim /* G2D_BITBLT_START */
98d7f1642cSJoonyoung Shim #define G2D_START_CASESEL		(1 << 2)
99d7f1642cSJoonyoung Shim #define G2D_START_NHOLT			(1 << 1)
100d7f1642cSJoonyoung Shim #define G2D_START_BITBLT		(1 << 0)
101d7f1642cSJoonyoung Shim 
1022dec17c7SYoungJun Cho /* buffer color format */
1032dec17c7SYoungJun Cho #define G2D_FMT_XRGB8888		0
1042dec17c7SYoungJun Cho #define G2D_FMT_ARGB8888		1
1052dec17c7SYoungJun Cho #define G2D_FMT_RGB565			2
1062dec17c7SYoungJun Cho #define G2D_FMT_XRGB1555		3
1072dec17c7SYoungJun Cho #define G2D_FMT_ARGB1555		4
1082dec17c7SYoungJun Cho #define G2D_FMT_XRGB4444		5
1092dec17c7SYoungJun Cho #define G2D_FMT_ARGB4444		6
1102dec17c7SYoungJun Cho #define G2D_FMT_PACKED_RGB888		7
1112dec17c7SYoungJun Cho #define G2D_FMT_A8			11
1122dec17c7SYoungJun Cho #define G2D_FMT_L8			12
1132dec17c7SYoungJun Cho 
1142dec17c7SYoungJun Cho /* buffer valid length */
1152dec17c7SYoungJun Cho #define G2D_LEN_MIN			1
1162dec17c7SYoungJun Cho #define G2D_LEN_MAX			8000
1172dec17c7SYoungJun Cho 
118d7f1642cSJoonyoung Shim #define G2D_CMDLIST_SIZE		(PAGE_SIZE / 4)
119d7f1642cSJoonyoung Shim #define G2D_CMDLIST_NUM			64
120d7f1642cSJoonyoung Shim #define G2D_CMDLIST_POOL_SIZE		(G2D_CMDLIST_SIZE * G2D_CMDLIST_NUM)
121d7f1642cSJoonyoung Shim #define G2D_CMDLIST_DATA_NUM		(G2D_CMDLIST_SIZE / sizeof(u32) - 2)
122d7f1642cSJoonyoung Shim 
1232a3098ffSInki Dae /* maximum buffer pool size of userptr is 64MB as default */
1242a3098ffSInki Dae #define MAX_POOL		(64 * 1024 * 1024)
1252a3098ffSInki Dae 
1262a3098ffSInki Dae enum {
1272a3098ffSInki Dae 	BUF_TYPE_GEM = 1,
1282a3098ffSInki Dae 	BUF_TYPE_USERPTR,
1292a3098ffSInki Dae };
1302a3098ffSInki Dae 
1319963cb6eSYoungJun Cho enum g2d_reg_type {
1329963cb6eSYoungJun Cho 	REG_TYPE_NONE = -1,
1339963cb6eSYoungJun Cho 	REG_TYPE_SRC,
1349963cb6eSYoungJun Cho 	REG_TYPE_SRC_PLANE2,
1359963cb6eSYoungJun Cho 	REG_TYPE_DST,
1369963cb6eSYoungJun Cho 	REG_TYPE_DST_PLANE2,
1379963cb6eSYoungJun Cho 	REG_TYPE_PAT,
1389963cb6eSYoungJun Cho 	REG_TYPE_MSK,
1399963cb6eSYoungJun Cho 	MAX_REG_TYPE_NR
1409963cb6eSYoungJun Cho };
1419963cb6eSYoungJun Cho 
14222d6704dSTobias Jakobi enum g2d_flag_bits {
14322d6704dSTobias Jakobi 	/*
14422d6704dSTobias Jakobi 	 * If set, suspends the runqueue worker after the currently
14522d6704dSTobias Jakobi 	 * processed node is finished.
14622d6704dSTobias Jakobi 	 */
14722d6704dSTobias Jakobi 	G2D_BIT_SUSPEND_RUNQUEUE,
14822d6704dSTobias Jakobi 	/*
14922d6704dSTobias Jakobi 	 * If set, indicates that the engine is currently busy.
15022d6704dSTobias Jakobi 	 */
15122d6704dSTobias Jakobi 	G2D_BIT_ENGINE_BUSY,
15222d6704dSTobias Jakobi };
15322d6704dSTobias Jakobi 
154d7f1642cSJoonyoung Shim /* cmdlist data structure */
155d7f1642cSJoonyoung Shim struct g2d_cmdlist {
156d7f1642cSJoonyoung Shim 	u32		head;
1572a3098ffSInki Dae 	unsigned long	data[G2D_CMDLIST_DATA_NUM];
158d7f1642cSJoonyoung Shim 	u32		last;	/* last data offset */
159d7f1642cSJoonyoung Shim };
160d7f1642cSJoonyoung Shim 
1619963cb6eSYoungJun Cho /*
1622dec17c7SYoungJun Cho  * A structure of buffer description
1632dec17c7SYoungJun Cho  *
1642dec17c7SYoungJun Cho  * @format: color format
165179239a7STobias Jakobi  * @stride: buffer stride/pitch in bytes
1662dec17c7SYoungJun Cho  * @left_x: the x coordinates of left top corner
1672dec17c7SYoungJun Cho  * @top_y: the y coordinates of left top corner
1682dec17c7SYoungJun Cho  * @right_x: the x coordinates of right bottom corner
1692dec17c7SYoungJun Cho  * @bottom_y: the y coordinates of right bottom corner
1702dec17c7SYoungJun Cho  *
1712dec17c7SYoungJun Cho  */
1722dec17c7SYoungJun Cho struct g2d_buf_desc {
1732dec17c7SYoungJun Cho 	unsigned int	format;
174179239a7STobias Jakobi 	unsigned int	stride;
1752dec17c7SYoungJun Cho 	unsigned int	left_x;
1762dec17c7SYoungJun Cho 	unsigned int	top_y;
1772dec17c7SYoungJun Cho 	unsigned int	right_x;
1782dec17c7SYoungJun Cho 	unsigned int	bottom_y;
1792dec17c7SYoungJun Cho };
1802dec17c7SYoungJun Cho 
1812dec17c7SYoungJun Cho /*
1829963cb6eSYoungJun Cho  * A structure of buffer information
1839963cb6eSYoungJun Cho  *
1849963cb6eSYoungJun Cho  * @map_nr: manages the number of mapped buffers
1859963cb6eSYoungJun Cho  * @reg_types: stores regitster type in the order of requested command
1869963cb6eSYoungJun Cho  * @handles: stores buffer handle in its reg_type position
1879963cb6eSYoungJun Cho  * @types: stores buffer type in its reg_type position
1882dec17c7SYoungJun Cho  * @descs: stores buffer description in its reg_type position
1899963cb6eSYoungJun Cho  *
1909963cb6eSYoungJun Cho  */
1919963cb6eSYoungJun Cho struct g2d_buf_info {
1929963cb6eSYoungJun Cho 	unsigned int		map_nr;
1939963cb6eSYoungJun Cho 	enum g2d_reg_type	reg_types[MAX_REG_TYPE_NR];
1943aa2a5c1SMarek Szyprowski 	void			*obj[MAX_REG_TYPE_NR];
1959963cb6eSYoungJun Cho 	unsigned int		types[MAX_REG_TYPE_NR];
1962dec17c7SYoungJun Cho 	struct g2d_buf_desc	descs[MAX_REG_TYPE_NR];
1979963cb6eSYoungJun Cho };
1989963cb6eSYoungJun Cho 
199d7f1642cSJoonyoung Shim struct drm_exynos_pending_g2d_event {
200d7f1642cSJoonyoung Shim 	struct drm_pending_event	base;
201d7f1642cSJoonyoung Shim 	struct drm_exynos_g2d_event	event;
202d7f1642cSJoonyoung Shim };
203d7f1642cSJoonyoung Shim 
2042a3098ffSInki Dae struct g2d_cmdlist_userptr {
2052a3098ffSInki Dae 	struct list_head	list;
2062a3098ffSInki Dae 	dma_addr_t		dma_addr;
2072a3098ffSInki Dae 	unsigned long		userptr;
2082a3098ffSInki Dae 	unsigned long		size;
2092c8c08f3SDaniel Vetter 	struct page		**pages;
2102c8c08f3SDaniel Vetter 	unsigned int		npages;
2112a3098ffSInki Dae 	struct sg_table		*sgt;
2128c27cc5bSXiyu Yang 	refcount_t		refcount;
2132a3098ffSInki Dae 	bool			in_pool;
2142a3098ffSInki Dae 	bool			out_of_list;
2152a3098ffSInki Dae };
216d7f1642cSJoonyoung Shim struct g2d_cmdlist_node {
217d7f1642cSJoonyoung Shim 	struct list_head	list;
218d7f1642cSJoonyoung Shim 	struct g2d_cmdlist	*cmdlist;
219d7f1642cSJoonyoung Shim 	dma_addr_t		dma_addr;
2209963cb6eSYoungJun Cho 	struct g2d_buf_info	buf_info;
221d7f1642cSJoonyoung Shim 
222d7f1642cSJoonyoung Shim 	struct drm_exynos_pending_g2d_event	*event;
223d7f1642cSJoonyoung Shim };
224d7f1642cSJoonyoung Shim 
225d7f1642cSJoonyoung Shim struct g2d_runqueue_node {
226d7f1642cSJoonyoung Shim 	struct list_head	list;
227d7f1642cSJoonyoung Shim 	struct list_head	run_cmdlist;
228d7f1642cSJoonyoung Shim 	struct list_head	event_list;
229d87342c1SInki Dae 	struct drm_file		*filp;
2306b6bae24SInki Dae 	pid_t			pid;
231d7f1642cSJoonyoung Shim 	struct completion	complete;
232d7f1642cSJoonyoung Shim 	int			async;
233d7f1642cSJoonyoung Shim };
234d7f1642cSJoonyoung Shim 
235d7f1642cSJoonyoung Shim struct g2d_data {
236d7f1642cSJoonyoung Shim 	struct device			*dev;
23707dc3678SMarek Szyprowski 	void				*dma_priv;
238d7f1642cSJoonyoung Shim 	struct clk			*gate_clk;
239d7f1642cSJoonyoung Shim 	void __iomem			*regs;
240d7f1642cSJoonyoung Shim 	int				irq;
241d7f1642cSJoonyoung Shim 	struct workqueue_struct		*g2d_workq;
242d7f1642cSJoonyoung Shim 	struct work_struct		runqueue_work;
243eb4d9796SMarek Szyprowski 	struct drm_device		*drm_dev;
24422d6704dSTobias Jakobi 	unsigned long			flags;
245d7f1642cSJoonyoung Shim 
246d7f1642cSJoonyoung Shim 	/* cmdlist */
247d7f1642cSJoonyoung Shim 	struct g2d_cmdlist_node		*cmdlist_node;
248d7f1642cSJoonyoung Shim 	struct list_head		free_cmdlist;
249d7f1642cSJoonyoung Shim 	struct mutex			cmdlist_mutex;
250d7f1642cSJoonyoung Shim 	dma_addr_t			cmdlist_pool;
251d7f1642cSJoonyoung Shim 	void				*cmdlist_pool_virt;
25200085f1eSKrzysztof Kozlowski 	unsigned long			cmdlist_dma_attrs;
253d7f1642cSJoonyoung Shim 
254d7f1642cSJoonyoung Shim 	/* runqueue*/
255d7f1642cSJoonyoung Shim 	struct g2d_runqueue_node	*runqueue_node;
256d7f1642cSJoonyoung Shim 	struct list_head		runqueue;
257d7f1642cSJoonyoung Shim 	struct mutex			runqueue_mutex;
258d7f1642cSJoonyoung Shim 	struct kmem_cache		*runqueue_slab;
2592a3098ffSInki Dae 
2602a3098ffSInki Dae 	unsigned long			current_pool;
2612a3098ffSInki Dae 	unsigned long			max_pool;
262d7f1642cSJoonyoung Shim };
263d7f1642cSJoonyoung Shim 
g2d_hw_reset(struct g2d_data * g2d)264134a0fe9STobias Jakobi static inline void g2d_hw_reset(struct g2d_data *g2d)
265134a0fe9STobias Jakobi {
266134a0fe9STobias Jakobi 	writel(G2D_R | G2D_SFRCLEAR, g2d->regs + G2D_SOFT_RESET);
267134a0fe9STobias Jakobi 	clear_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
268134a0fe9STobias Jakobi }
269134a0fe9STobias Jakobi 
g2d_init_cmdlist(struct g2d_data * g2d)270d7f1642cSJoonyoung Shim static int g2d_init_cmdlist(struct g2d_data *g2d)
271d7f1642cSJoonyoung Shim {
272d7f1642cSJoonyoung Shim 	struct device *dev = g2d->dev;
27359d43174SColin Ian King 	struct g2d_cmdlist_node *node;
274d7f1642cSJoonyoung Shim 	int nr;
275d7f1642cSJoonyoung Shim 	int ret;
2769963cb6eSYoungJun Cho 	struct g2d_buf_info *buf_info;
277d7f1642cSJoonyoung Shim 
27800085f1eSKrzysztof Kozlowski 	g2d->cmdlist_dma_attrs = DMA_ATTR_WRITE_COMBINE;
279d87342c1SInki Dae 
280eb4d9796SMarek Szyprowski 	g2d->cmdlist_pool_virt = dma_alloc_attrs(to_dma_dev(g2d->drm_dev),
281d87342c1SInki Dae 						G2D_CMDLIST_POOL_SIZE,
282d87342c1SInki Dae 						&g2d->cmdlist_pool, GFP_KERNEL,
28300085f1eSKrzysztof Kozlowski 						g2d->cmdlist_dma_attrs);
284d7f1642cSJoonyoung Shim 	if (!g2d->cmdlist_pool_virt) {
285d7f1642cSJoonyoung Shim 		dev_err(dev, "failed to allocate dma memory\n");
286d7f1642cSJoonyoung Shim 		return -ENOMEM;
287d7f1642cSJoonyoung Shim 	}
288d7f1642cSJoonyoung Shim 
289fab9f8d0SJoonyoung Shim 	node = kcalloc(G2D_CMDLIST_NUM, sizeof(*node), GFP_KERNEL);
290d7f1642cSJoonyoung Shim 	if (!node) {
291d7f1642cSJoonyoung Shim 		ret = -ENOMEM;
292d7f1642cSJoonyoung Shim 		goto err;
293d7f1642cSJoonyoung Shim 	}
294d7f1642cSJoonyoung Shim 
295d7f1642cSJoonyoung Shim 	for (nr = 0; nr < G2D_CMDLIST_NUM; nr++) {
2969963cb6eSYoungJun Cho 		unsigned int i;
2979963cb6eSYoungJun Cho 
298d7f1642cSJoonyoung Shim 		node[nr].cmdlist =
299d7f1642cSJoonyoung Shim 			g2d->cmdlist_pool_virt + nr * G2D_CMDLIST_SIZE;
300d7f1642cSJoonyoung Shim 		node[nr].dma_addr =
301d7f1642cSJoonyoung Shim 			g2d->cmdlist_pool + nr * G2D_CMDLIST_SIZE;
302d7f1642cSJoonyoung Shim 
3039963cb6eSYoungJun Cho 		buf_info = &node[nr].buf_info;
3049963cb6eSYoungJun Cho 		for (i = 0; i < MAX_REG_TYPE_NR; i++)
3059963cb6eSYoungJun Cho 			buf_info->reg_types[i] = REG_TYPE_NONE;
3069963cb6eSYoungJun Cho 
307d7f1642cSJoonyoung Shim 		list_add_tail(&node[nr].list, &g2d->free_cmdlist);
308d7f1642cSJoonyoung Shim 	}
309d7f1642cSJoonyoung Shim 
310d7f1642cSJoonyoung Shim 	return 0;
311d7f1642cSJoonyoung Shim 
312d7f1642cSJoonyoung Shim err:
313eb4d9796SMarek Szyprowski 	dma_free_attrs(to_dma_dev(g2d->drm_dev), G2D_CMDLIST_POOL_SIZE,
314d87342c1SInki Dae 			g2d->cmdlist_pool_virt,
31500085f1eSKrzysztof Kozlowski 			g2d->cmdlist_pool, g2d->cmdlist_dma_attrs);
316d7f1642cSJoonyoung Shim 	return ret;
317d7f1642cSJoonyoung Shim }
318d7f1642cSJoonyoung Shim 
g2d_fini_cmdlist(struct g2d_data * g2d)319d7f1642cSJoonyoung Shim static void g2d_fini_cmdlist(struct g2d_data *g2d)
320d7f1642cSJoonyoung Shim {
321d7f1642cSJoonyoung Shim 	kfree(g2d->cmdlist_node);
3229ad703e9SInki Dae 
3239ad703e9SInki Dae 	if (g2d->cmdlist_pool_virt && g2d->cmdlist_pool) {
324eb4d9796SMarek Szyprowski 		dma_free_attrs(to_dma_dev(g2d->drm_dev),
325f43c3596SMarek Szyprowski 				G2D_CMDLIST_POOL_SIZE,
326d87342c1SInki Dae 				g2d->cmdlist_pool_virt,
32700085f1eSKrzysztof Kozlowski 				g2d->cmdlist_pool, g2d->cmdlist_dma_attrs);
328d7f1642cSJoonyoung Shim 	}
3299ad703e9SInki Dae }
330d7f1642cSJoonyoung Shim 
g2d_get_cmdlist(struct g2d_data * g2d)331d7f1642cSJoonyoung Shim static struct g2d_cmdlist_node *g2d_get_cmdlist(struct g2d_data *g2d)
332d7f1642cSJoonyoung Shim {
333d7f1642cSJoonyoung Shim 	struct device *dev = g2d->dev;
334d7f1642cSJoonyoung Shim 	struct g2d_cmdlist_node *node;
335d7f1642cSJoonyoung Shim 
336d7f1642cSJoonyoung Shim 	mutex_lock(&g2d->cmdlist_mutex);
337d7f1642cSJoonyoung Shim 	if (list_empty(&g2d->free_cmdlist)) {
338d7f1642cSJoonyoung Shim 		dev_err(dev, "there is no free cmdlist\n");
339d7f1642cSJoonyoung Shim 		mutex_unlock(&g2d->cmdlist_mutex);
340d7f1642cSJoonyoung Shim 		return NULL;
341d7f1642cSJoonyoung Shim 	}
342d7f1642cSJoonyoung Shim 
343d7f1642cSJoonyoung Shim 	node = list_first_entry(&g2d->free_cmdlist, struct g2d_cmdlist_node,
344d7f1642cSJoonyoung Shim 				list);
345d7f1642cSJoonyoung Shim 	list_del_init(&node->list);
346d7f1642cSJoonyoung Shim 	mutex_unlock(&g2d->cmdlist_mutex);
347d7f1642cSJoonyoung Shim 
348d7f1642cSJoonyoung Shim 	return node;
349d7f1642cSJoonyoung Shim }
350d7f1642cSJoonyoung Shim 
g2d_put_cmdlist(struct g2d_data * g2d,struct g2d_cmdlist_node * node)351d7f1642cSJoonyoung Shim static void g2d_put_cmdlist(struct g2d_data *g2d, struct g2d_cmdlist_node *node)
352d7f1642cSJoonyoung Shim {
353d7f1642cSJoonyoung Shim 	mutex_lock(&g2d->cmdlist_mutex);
354d7f1642cSJoonyoung Shim 	list_move_tail(&node->list, &g2d->free_cmdlist);
355d7f1642cSJoonyoung Shim 	mutex_unlock(&g2d->cmdlist_mutex);
356d7f1642cSJoonyoung Shim }
357d7f1642cSJoonyoung Shim 
g2d_add_cmdlist_to_inuse(struct drm_exynos_file_private * file_priv,struct g2d_cmdlist_node * node)358eb4d9796SMarek Szyprowski static void g2d_add_cmdlist_to_inuse(struct drm_exynos_file_private *file_priv,
359d7f1642cSJoonyoung Shim 				     struct g2d_cmdlist_node *node)
360d7f1642cSJoonyoung Shim {
361d7f1642cSJoonyoung Shim 	struct g2d_cmdlist_node *lnode;
362d7f1642cSJoonyoung Shim 
363eb4d9796SMarek Szyprowski 	if (list_empty(&file_priv->inuse_cmdlist))
364d7f1642cSJoonyoung Shim 		goto add_to_list;
365d7f1642cSJoonyoung Shim 
366d7f1642cSJoonyoung Shim 	/* this links to base address of new cmdlist */
367eb4d9796SMarek Szyprowski 	lnode = list_entry(file_priv->inuse_cmdlist.prev,
368d7f1642cSJoonyoung Shim 				struct g2d_cmdlist_node, list);
369d7f1642cSJoonyoung Shim 	lnode->cmdlist->data[lnode->cmdlist->last] = node->dma_addr;
370d7f1642cSJoonyoung Shim 
371d7f1642cSJoonyoung Shim add_to_list:
372eb4d9796SMarek Szyprowski 	list_add_tail(&node->list, &file_priv->inuse_cmdlist);
373d7f1642cSJoonyoung Shim 
374d7f1642cSJoonyoung Shim 	if (node->event)
375eb4d9796SMarek Szyprowski 		list_add_tail(&node->event->base.link, &file_priv->event_list);
376d7f1642cSJoonyoung Shim }
377d7f1642cSJoonyoung Shim 
g2d_userptr_put_dma_addr(struct g2d_data * g2d,void * obj,bool force)378eb4d9796SMarek Szyprowski static void g2d_userptr_put_dma_addr(struct g2d_data *g2d,
3793aa2a5c1SMarek Szyprowski 					void *obj,
3802a3098ffSInki Dae 					bool force)
3812a3098ffSInki Dae {
3823aa2a5c1SMarek Szyprowski 	struct g2d_cmdlist_userptr *g2d_userptr = obj;
3832a3098ffSInki Dae 
3842a3098ffSInki Dae 	if (!obj)
3852a3098ffSInki Dae 		return;
3862a3098ffSInki Dae 
3872a3098ffSInki Dae 	if (force)
3882a3098ffSInki Dae 		goto out;
3892a3098ffSInki Dae 
3908c27cc5bSXiyu Yang 	refcount_dec(&g2d_userptr->refcount);
3912a3098ffSInki Dae 
3928c27cc5bSXiyu Yang 	if (refcount_read(&g2d_userptr->refcount) > 0)
3932a3098ffSInki Dae 		return;
3942a3098ffSInki Dae 
3952a3098ffSInki Dae 	if (g2d_userptr->in_pool)
3962a3098ffSInki Dae 		return;
3972a3098ffSInki Dae 
3982a3098ffSInki Dae out:
39984404614SMarek Szyprowski 	dma_unmap_sgtable(to_dma_dev(g2d->drm_dev), g2d_userptr->sgt,
40084404614SMarek Szyprowski 			  DMA_BIDIRECTIONAL, 0);
4012a3098ffSInki Dae 
4022c8c08f3SDaniel Vetter 	unpin_user_pages_dirty_lock(g2d_userptr->pages, g2d_userptr->npages,
4032c8c08f3SDaniel Vetter 				    true);
4042c8c08f3SDaniel Vetter 	kvfree(g2d_userptr->pages);
405c3bddbdaSInki Dae 
4062a3098ffSInki Dae 	if (!g2d_userptr->out_of_list)
4072a3098ffSInki Dae 		list_del_init(&g2d_userptr->list);
4082a3098ffSInki Dae 
4092a3098ffSInki Dae 	sg_free_table(g2d_userptr->sgt);
4102a3098ffSInki Dae 	kfree(g2d_userptr->sgt);
411df3d90e5SSachin Kamat 	kfree(g2d_userptr);
4122a3098ffSInki Dae }
4132a3098ffSInki Dae 
g2d_userptr_get_dma_addr(struct g2d_data * g2d,unsigned long userptr,unsigned long size,struct drm_file * filp,void ** obj)414eb4d9796SMarek Szyprowski static dma_addr_t *g2d_userptr_get_dma_addr(struct g2d_data *g2d,
4152a3098ffSInki Dae 					unsigned long userptr,
4162a3098ffSInki Dae 					unsigned long size,
4172a3098ffSInki Dae 					struct drm_file *filp,
4183aa2a5c1SMarek Szyprowski 					void **obj)
4192a3098ffSInki Dae {
4202a3098ffSInki Dae 	struct drm_exynos_file_private *file_priv = filp->driver_priv;
4212a3098ffSInki Dae 	struct g2d_cmdlist_userptr *g2d_userptr;
4222a3098ffSInki Dae 	struct sg_table	*sgt;
4232a3098ffSInki Dae 	unsigned long start, end;
4242a3098ffSInki Dae 	unsigned int npages, offset;
4252a3098ffSInki Dae 	int ret;
4262a3098ffSInki Dae 
4272a3098ffSInki Dae 	if (!size) {
4286f83d208SInki Dae 		DRM_DEV_ERROR(g2d->dev, "invalid userptr size.\n");
4292a3098ffSInki Dae 		return ERR_PTR(-EINVAL);
4302a3098ffSInki Dae 	}
4312a3098ffSInki Dae 
4322a3098ffSInki Dae 	/* check if userptr already exists in userptr_list. */
433eb4d9796SMarek Szyprowski 	list_for_each_entry(g2d_userptr, &file_priv->userptr_list, list) {
4342a3098ffSInki Dae 		if (g2d_userptr->userptr == userptr) {
4352a3098ffSInki Dae 			/*
4362a3098ffSInki Dae 			 * also check size because there could be same address
4372a3098ffSInki Dae 			 * and different size.
4382a3098ffSInki Dae 			 */
4392a3098ffSInki Dae 			if (g2d_userptr->size == size) {
4408c27cc5bSXiyu Yang 				refcount_inc(&g2d_userptr->refcount);
4413aa2a5c1SMarek Szyprowski 				*obj = g2d_userptr;
4422a3098ffSInki Dae 
4432a3098ffSInki Dae 				return &g2d_userptr->dma_addr;
4442a3098ffSInki Dae 			}
4452a3098ffSInki Dae 
4462a3098ffSInki Dae 			/*
4472a3098ffSInki Dae 			 * at this moment, maybe g2d dma is accessing this
4482a3098ffSInki Dae 			 * g2d_userptr memory region so just remove this
4492a3098ffSInki Dae 			 * g2d_userptr object from userptr_list not to be
4502a3098ffSInki Dae 			 * referred again and also except it the userptr
4512a3098ffSInki Dae 			 * pool to be released after the dma access completion.
4522a3098ffSInki Dae 			 */
4532a3098ffSInki Dae 			g2d_userptr->out_of_list = true;
4542a3098ffSInki Dae 			g2d_userptr->in_pool = false;
4552a3098ffSInki Dae 			list_del_init(&g2d_userptr->list);
4562a3098ffSInki Dae 
4572a3098ffSInki Dae 			break;
4582a3098ffSInki Dae 		}
4592a3098ffSInki Dae 	}
4602a3098ffSInki Dae 
4612a3098ffSInki Dae 	g2d_userptr = kzalloc(sizeof(*g2d_userptr), GFP_KERNEL);
46238bb5253SSachin Kamat 	if (!g2d_userptr)
4632a3098ffSInki Dae 		return ERR_PTR(-ENOMEM);
4642a3098ffSInki Dae 
4658c27cc5bSXiyu Yang 	refcount_set(&g2d_userptr->refcount, 1);
46663540f01SJan Kara 	g2d_userptr->size = size;
4672a3098ffSInki Dae 
4682a3098ffSInki Dae 	start = userptr & PAGE_MASK;
4692a3098ffSInki Dae 	offset = userptr & ~PAGE_MASK;
4702a3098ffSInki Dae 	end = PAGE_ALIGN(userptr + size);
4712a3098ffSInki Dae 	npages = (end - start) >> PAGE_SHIFT;
4722c8c08f3SDaniel Vetter 	g2d_userptr->pages = kvmalloc_array(npages, sizeof(*g2d_userptr->pages),
4732c8c08f3SDaniel Vetter 					    GFP_KERNEL);
4742c8c08f3SDaniel Vetter 	if (!g2d_userptr->pages) {
4754bb615c5SSeung-Woo Kim 		ret = -ENOMEM;
4764bb615c5SSeung-Woo Kim 		goto err_free;
4772a3098ffSInki Dae 	}
4782a3098ffSInki Dae 
4799fcac0f1SDaniel Vetter 	ret = pin_user_pages_fast(start, npages,
480c098ce73SDavid Hildenbrand 				  FOLL_WRITE | FOLL_LONGTERM,
4812c8c08f3SDaniel Vetter 				  g2d_userptr->pages);
48263540f01SJan Kara 	if (ret != npages) {
4836f83d208SInki Dae 		DRM_DEV_ERROR(g2d->dev,
4846f83d208SInki Dae 			      "failed to get user pages from userptr.\n");
48563540f01SJan Kara 		if (ret < 0)
4862c8c08f3SDaniel Vetter 			goto err_destroy_pages;
4872c8c08f3SDaniel Vetter 		npages = ret;
48863540f01SJan Kara 		ret = -EFAULT;
4892c8c08f3SDaniel Vetter 		goto err_unpin_pages;
4902a3098ffSInki Dae 	}
4912c8c08f3SDaniel Vetter 	g2d_userptr->npages = npages;
4922a3098ffSInki Dae 
493e44a5c00SSachin Kamat 	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
4942a3098ffSInki Dae 	if (!sgt) {
4952a3098ffSInki Dae 		ret = -ENOMEM;
4962c8c08f3SDaniel Vetter 		goto err_unpin_pages;
4972a3098ffSInki Dae 	}
4982a3098ffSInki Dae 
49963540f01SJan Kara 	ret = sg_alloc_table_from_pages(sgt,
5002c8c08f3SDaniel Vetter 					g2d_userptr->pages,
50163540f01SJan Kara 					npages, offset, size, GFP_KERNEL);
5022a3098ffSInki Dae 	if (ret < 0) {
5036f83d208SInki Dae 		DRM_DEV_ERROR(g2d->dev, "failed to get sgt from pages.\n");
5042a3098ffSInki Dae 		goto err_free_sgt;
5052a3098ffSInki Dae 	}
5062a3098ffSInki Dae 
5072a3098ffSInki Dae 	g2d_userptr->sgt = sgt;
5082a3098ffSInki Dae 
50984404614SMarek Szyprowski 	ret = dma_map_sgtable(to_dma_dev(g2d->drm_dev), sgt,
51084404614SMarek Szyprowski 			      DMA_BIDIRECTIONAL, 0);
51184404614SMarek Szyprowski 	if (ret) {
5126f83d208SInki Dae 		DRM_DEV_ERROR(g2d->dev, "failed to map sgt with dma region.\n");
513067ed331SYoungJun Cho 		goto err_sg_free_table;
5142a3098ffSInki Dae 	}
5152a3098ffSInki Dae 
5162a3098ffSInki Dae 	g2d_userptr->dma_addr = sgt->sgl[0].dma_address;
5172a3098ffSInki Dae 	g2d_userptr->userptr = userptr;
5182a3098ffSInki Dae 
519eb4d9796SMarek Szyprowski 	list_add_tail(&g2d_userptr->list, &file_priv->userptr_list);
5202a3098ffSInki Dae 
5212a3098ffSInki Dae 	if (g2d->current_pool + (npages << PAGE_SHIFT) < g2d->max_pool) {
5222a3098ffSInki Dae 		g2d->current_pool += npages << PAGE_SHIFT;
5232a3098ffSInki Dae 		g2d_userptr->in_pool = true;
5242a3098ffSInki Dae 	}
5252a3098ffSInki Dae 
5263aa2a5c1SMarek Szyprowski 	*obj = g2d_userptr;
5272a3098ffSInki Dae 
5282a3098ffSInki Dae 	return &g2d_userptr->dma_addr;
5292a3098ffSInki Dae 
530067ed331SYoungJun Cho err_sg_free_table:
5312a3098ffSInki Dae 	sg_free_table(sgt);
532067ed331SYoungJun Cho 
533067ed331SYoungJun Cho err_free_sgt:
5342a3098ffSInki Dae 	kfree(sgt);
5352a3098ffSInki Dae 
5362c8c08f3SDaniel Vetter err_unpin_pages:
5372c8c08f3SDaniel Vetter 	unpin_user_pages(g2d_userptr->pages, npages);
5382a3098ffSInki Dae 
5392c8c08f3SDaniel Vetter err_destroy_pages:
5402c8c08f3SDaniel Vetter 	kvfree(g2d_userptr->pages);
5414bb615c5SSeung-Woo Kim 
5424bb615c5SSeung-Woo Kim err_free:
5432a3098ffSInki Dae 	kfree(g2d_userptr);
5442a3098ffSInki Dae 
5452a3098ffSInki Dae 	return ERR_PTR(ret);
5462a3098ffSInki Dae }
5472a3098ffSInki Dae 
g2d_userptr_free_all(struct g2d_data * g2d,struct drm_file * filp)548eb4d9796SMarek Szyprowski static void g2d_userptr_free_all(struct g2d_data *g2d, struct drm_file *filp)
5492a3098ffSInki Dae {
5502a3098ffSInki Dae 	struct drm_exynos_file_private *file_priv = filp->driver_priv;
5512a3098ffSInki Dae 	struct g2d_cmdlist_userptr *g2d_userptr, *n;
5522a3098ffSInki Dae 
553eb4d9796SMarek Szyprowski 	list_for_each_entry_safe(g2d_userptr, n, &file_priv->userptr_list, list)
5542a3098ffSInki Dae 		if (g2d_userptr->in_pool)
5553aa2a5c1SMarek Szyprowski 			g2d_userptr_put_dma_addr(g2d, g2d_userptr, true);
5562a3098ffSInki Dae 
5572a3098ffSInki Dae 	g2d->current_pool = 0;
5582a3098ffSInki Dae }
5592a3098ffSInki Dae 
g2d_get_reg_type(struct g2d_data * g2d,int reg_offset)5606f83d208SInki Dae static enum g2d_reg_type g2d_get_reg_type(struct g2d_data *g2d, int reg_offset)
5619963cb6eSYoungJun Cho {
5629963cb6eSYoungJun Cho 	enum g2d_reg_type reg_type;
5639963cb6eSYoungJun Cho 
5649963cb6eSYoungJun Cho 	switch (reg_offset) {
5659963cb6eSYoungJun Cho 	case G2D_SRC_BASE_ADDR:
566ed4dc271STobias Jakobi 	case G2D_SRC_STRIDE:
5672dec17c7SYoungJun Cho 	case G2D_SRC_COLOR_MODE:
5682dec17c7SYoungJun Cho 	case G2D_SRC_LEFT_TOP:
5692dec17c7SYoungJun Cho 	case G2D_SRC_RIGHT_BOTTOM:
5709963cb6eSYoungJun Cho 		reg_type = REG_TYPE_SRC;
5719963cb6eSYoungJun Cho 		break;
5729963cb6eSYoungJun Cho 	case G2D_SRC_PLANE2_BASE_ADDR:
5739963cb6eSYoungJun Cho 		reg_type = REG_TYPE_SRC_PLANE2;
5749963cb6eSYoungJun Cho 		break;
5759963cb6eSYoungJun Cho 	case G2D_DST_BASE_ADDR:
576ed4dc271STobias Jakobi 	case G2D_DST_STRIDE:
5772dec17c7SYoungJun Cho 	case G2D_DST_COLOR_MODE:
5782dec17c7SYoungJun Cho 	case G2D_DST_LEFT_TOP:
5792dec17c7SYoungJun Cho 	case G2D_DST_RIGHT_BOTTOM:
5809963cb6eSYoungJun Cho 		reg_type = REG_TYPE_DST;
5819963cb6eSYoungJun Cho 		break;
5829963cb6eSYoungJun Cho 	case G2D_DST_PLANE2_BASE_ADDR:
5839963cb6eSYoungJun Cho 		reg_type = REG_TYPE_DST_PLANE2;
5849963cb6eSYoungJun Cho 		break;
5859963cb6eSYoungJun Cho 	case G2D_PAT_BASE_ADDR:
5869963cb6eSYoungJun Cho 		reg_type = REG_TYPE_PAT;
5879963cb6eSYoungJun Cho 		break;
5889963cb6eSYoungJun Cho 	case G2D_MSK_BASE_ADDR:
5899963cb6eSYoungJun Cho 		reg_type = REG_TYPE_MSK;
5909963cb6eSYoungJun Cho 		break;
5919963cb6eSYoungJun Cho 	default:
5929963cb6eSYoungJun Cho 		reg_type = REG_TYPE_NONE;
5936f83d208SInki Dae 		DRM_DEV_ERROR(g2d->dev, "Unknown register offset![%d]\n",
5946f83d208SInki Dae 			      reg_offset);
5959963cb6eSYoungJun Cho 		break;
5965cdbc8d9SSachin Kamat 	}
5979963cb6eSYoungJun Cho 
5989963cb6eSYoungJun Cho 	return reg_type;
5999963cb6eSYoungJun Cho }
6009963cb6eSYoungJun Cho 
g2d_get_buf_bpp(unsigned int format)6012dec17c7SYoungJun Cho static unsigned long g2d_get_buf_bpp(unsigned int format)
6022dec17c7SYoungJun Cho {
6032dec17c7SYoungJun Cho 	unsigned long bpp;
6042dec17c7SYoungJun Cho 
6052dec17c7SYoungJun Cho 	switch (format) {
6062dec17c7SYoungJun Cho 	case G2D_FMT_XRGB8888:
6072dec17c7SYoungJun Cho 	case G2D_FMT_ARGB8888:
6082dec17c7SYoungJun Cho 		bpp = 4;
6092dec17c7SYoungJun Cho 		break;
6102dec17c7SYoungJun Cho 	case G2D_FMT_RGB565:
6112dec17c7SYoungJun Cho 	case G2D_FMT_XRGB1555:
6122dec17c7SYoungJun Cho 	case G2D_FMT_ARGB1555:
6132dec17c7SYoungJun Cho 	case G2D_FMT_XRGB4444:
6142dec17c7SYoungJun Cho 	case G2D_FMT_ARGB4444:
6152dec17c7SYoungJun Cho 		bpp = 2;
6162dec17c7SYoungJun Cho 		break;
6172dec17c7SYoungJun Cho 	case G2D_FMT_PACKED_RGB888:
6182dec17c7SYoungJun Cho 		bpp = 3;
6192dec17c7SYoungJun Cho 		break;
6202dec17c7SYoungJun Cho 	default:
6212dec17c7SYoungJun Cho 		bpp = 1;
6222dec17c7SYoungJun Cho 		break;
6232dec17c7SYoungJun Cho 	}
6242dec17c7SYoungJun Cho 
6252dec17c7SYoungJun Cho 	return bpp;
6262dec17c7SYoungJun Cho }
6272dec17c7SYoungJun Cho 
g2d_check_buf_desc_is_valid(struct g2d_data * g2d,struct g2d_buf_desc * buf_desc,enum g2d_reg_type reg_type,unsigned long size)6286f83d208SInki Dae static bool g2d_check_buf_desc_is_valid(struct g2d_data *g2d,
6296f83d208SInki Dae 					struct g2d_buf_desc *buf_desc,
6302dec17c7SYoungJun Cho 					enum g2d_reg_type reg_type,
6312dec17c7SYoungJun Cho 					unsigned long size)
6322dec17c7SYoungJun Cho {
633179239a7STobias Jakobi 	int width, height;
634179239a7STobias Jakobi 	unsigned long bpp, last_pos;
6352dec17c7SYoungJun Cho 
6362dec17c7SYoungJun Cho 	/*
6372dec17c7SYoungJun Cho 	 * check source and destination buffers only.
6382dec17c7SYoungJun Cho 	 * so the others are always valid.
6392dec17c7SYoungJun Cho 	 */
6402dec17c7SYoungJun Cho 	if (reg_type != REG_TYPE_SRC && reg_type != REG_TYPE_DST)
6412dec17c7SYoungJun Cho 		return true;
6422dec17c7SYoungJun Cho 
643179239a7STobias Jakobi 	/* This check also makes sure that right_x > left_x. */
644179239a7STobias Jakobi 	width = (int)buf_desc->right_x - (int)buf_desc->left_x;
6452dec17c7SYoungJun Cho 	if (width < G2D_LEN_MIN || width > G2D_LEN_MAX) {
6466f83d208SInki Dae 		DRM_DEV_ERROR(g2d->dev, "width[%d] is out of range!\n", width);
6472dec17c7SYoungJun Cho 		return false;
6482dec17c7SYoungJun Cho 	}
6492dec17c7SYoungJun Cho 
650179239a7STobias Jakobi 	/* This check also makes sure that bottom_y > top_y. */
651179239a7STobias Jakobi 	height = (int)buf_desc->bottom_y - (int)buf_desc->top_y;
6522dec17c7SYoungJun Cho 	if (height < G2D_LEN_MIN || height > G2D_LEN_MAX) {
6536f83d208SInki Dae 		DRM_DEV_ERROR(g2d->dev,
6546f83d208SInki Dae 			      "height[%d] is out of range!\n", height);
6552dec17c7SYoungJun Cho 		return false;
6562dec17c7SYoungJun Cho 	}
6572dec17c7SYoungJun Cho 
658179239a7STobias Jakobi 	bpp = g2d_get_buf_bpp(buf_desc->format);
659179239a7STobias Jakobi 
660179239a7STobias Jakobi 	/* Compute the position of the last byte that the engine accesses. */
661179239a7STobias Jakobi 	last_pos = ((unsigned long)buf_desc->bottom_y - 1) *
662179239a7STobias Jakobi 		(unsigned long)buf_desc->stride +
663179239a7STobias Jakobi 		(unsigned long)buf_desc->right_x * bpp - 1;
664179239a7STobias Jakobi 
665179239a7STobias Jakobi 	/*
666179239a7STobias Jakobi 	 * Since right_x > left_x and bottom_y > top_y we already know
667179239a7STobias Jakobi 	 * that the first_pos < last_pos (first_pos being the position
668179239a7STobias Jakobi 	 * of the first byte the engine accesses), it just remains to
669179239a7STobias Jakobi 	 * check if last_pos is smaller then the buffer size.
670179239a7STobias Jakobi 	 */
671179239a7STobias Jakobi 
672179239a7STobias Jakobi 	if (last_pos >= size) {
6736f83d208SInki Dae 		DRM_DEV_ERROR(g2d->dev, "last engine access position [%lu] "
674179239a7STobias Jakobi 			      "is out of range [%lu]!\n", last_pos, size);
6752dec17c7SYoungJun Cho 		return false;
6762dec17c7SYoungJun Cho 	}
6772dec17c7SYoungJun Cho 
6782dec17c7SYoungJun Cho 	return true;
6792dec17c7SYoungJun Cho }
6802dec17c7SYoungJun Cho 
g2d_map_cmdlist_gem(struct g2d_data * g2d,struct g2d_cmdlist_node * node,struct drm_device * drm_dev,struct drm_file * file)681d87342c1SInki Dae static int g2d_map_cmdlist_gem(struct g2d_data *g2d,
682d87342c1SInki Dae 				struct g2d_cmdlist_node *node,
683d87342c1SInki Dae 				struct drm_device *drm_dev,
684d87342c1SInki Dae 				struct drm_file *file)
685d7f1642cSJoonyoung Shim {
686d7f1642cSJoonyoung Shim 	struct g2d_cmdlist *cmdlist = node->cmdlist;
6879963cb6eSYoungJun Cho 	struct g2d_buf_info *buf_info = &node->buf_info;
688d7f1642cSJoonyoung Shim 	int offset;
6899963cb6eSYoungJun Cho 	int ret;
690d7f1642cSJoonyoung Shim 	int i;
691d7f1642cSJoonyoung Shim 
6929963cb6eSYoungJun Cho 	for (i = 0; i < buf_info->map_nr; i++) {
6932dec17c7SYoungJun Cho 		struct g2d_buf_desc *buf_desc;
6949963cb6eSYoungJun Cho 		enum g2d_reg_type reg_type;
6959963cb6eSYoungJun Cho 		int reg_pos;
696d87342c1SInki Dae 		unsigned long handle;
697d87342c1SInki Dae 		dma_addr_t *addr;
698d7f1642cSJoonyoung Shim 
6999963cb6eSYoungJun Cho 		reg_pos = cmdlist->last - 2 * (i + 1);
700d7f1642cSJoonyoung Shim 
7019963cb6eSYoungJun Cho 		offset = cmdlist->data[reg_pos];
7029963cb6eSYoungJun Cho 		handle = cmdlist->data[reg_pos + 1];
7039963cb6eSYoungJun Cho 
7046f83d208SInki Dae 		reg_type = g2d_get_reg_type(g2d, offset);
7059963cb6eSYoungJun Cho 		if (reg_type == REG_TYPE_NONE) {
7069963cb6eSYoungJun Cho 			ret = -EFAULT;
7079963cb6eSYoungJun Cho 			goto err;
7089963cb6eSYoungJun Cho 		}
7099963cb6eSYoungJun Cho 
7102dec17c7SYoungJun Cho 		buf_desc = &buf_info->descs[reg_type];
7112dec17c7SYoungJun Cho 
7129963cb6eSYoungJun Cho 		if (buf_info->types[reg_type] == BUF_TYPE_GEM) {
7133aa2a5c1SMarek Szyprowski 			struct exynos_drm_gem *exynos_gem;
7142dec17c7SYoungJun Cho 
7153aa2a5c1SMarek Szyprowski 			exynos_gem = exynos_drm_gem_get(file, handle);
7163aa2a5c1SMarek Szyprowski 			if (!exynos_gem) {
7172dec17c7SYoungJun Cho 				ret = -EFAULT;
7182dec17c7SYoungJun Cho 				goto err;
7192dec17c7SYoungJun Cho 			}
7202dec17c7SYoungJun Cho 
7216f83d208SInki Dae 			if (!g2d_check_buf_desc_is_valid(g2d, buf_desc,
7223aa2a5c1SMarek Szyprowski 							 reg_type, exynos_gem->size)) {
7233aa2a5c1SMarek Szyprowski 				exynos_drm_gem_put(exynos_gem);
7242dec17c7SYoungJun Cho 				ret = -EFAULT;
7252dec17c7SYoungJun Cho 				goto err;
7262dec17c7SYoungJun Cho 			}
7272dec17c7SYoungJun Cho 
7283aa2a5c1SMarek Szyprowski 			addr = &exynos_gem->dma_addr;
7293aa2a5c1SMarek Szyprowski 			buf_info->obj[reg_type] = exynos_gem;
7302a3098ffSInki Dae 		} else {
7312a3098ffSInki Dae 			struct drm_exynos_g2d_userptr g2d_userptr;
7322a3098ffSInki Dae 
7332a3098ffSInki Dae 			if (copy_from_user(&g2d_userptr, (void __user *)handle,
7342a3098ffSInki Dae 				sizeof(struct drm_exynos_g2d_userptr))) {
7359963cb6eSYoungJun Cho 				ret = -EFAULT;
7369963cb6eSYoungJun Cho 				goto err;
7372a3098ffSInki Dae 			}
7382a3098ffSInki Dae 
7396f83d208SInki Dae 			if (!g2d_check_buf_desc_is_valid(g2d, buf_desc,
7406f83d208SInki Dae 							 reg_type,
7412dec17c7SYoungJun Cho 							 g2d_userptr.size)) {
7422dec17c7SYoungJun Cho 				ret = -EFAULT;
7432dec17c7SYoungJun Cho 				goto err;
7442dec17c7SYoungJun Cho 			}
7452dec17c7SYoungJun Cho 
746eb4d9796SMarek Szyprowski 			addr = g2d_userptr_get_dma_addr(g2d,
7472a3098ffSInki Dae 							g2d_userptr.userptr,
7482a3098ffSInki Dae 							g2d_userptr.size,
7492a3098ffSInki Dae 							file,
7503aa2a5c1SMarek Szyprowski 							&buf_info->obj[reg_type]);
7512a3098ffSInki Dae 			if (IS_ERR(addr)) {
7529963cb6eSYoungJun Cho 				ret = -EFAULT;
7539963cb6eSYoungJun Cho 				goto err;
7542a3098ffSInki Dae 			}
7552a3098ffSInki Dae 		}
756d7f1642cSJoonyoung Shim 
7579963cb6eSYoungJun Cho 		cmdlist->data[reg_pos + 1] = *addr;
7589963cb6eSYoungJun Cho 		buf_info->reg_types[i] = reg_type;
759d7f1642cSJoonyoung Shim 	}
760d7f1642cSJoonyoung Shim 
761d7f1642cSJoonyoung Shim 	return 0;
7629963cb6eSYoungJun Cho 
7639963cb6eSYoungJun Cho err:
7649963cb6eSYoungJun Cho 	buf_info->map_nr = i;
7659963cb6eSYoungJun Cho 	return ret;
766d7f1642cSJoonyoung Shim }
767d7f1642cSJoonyoung Shim 
g2d_unmap_cmdlist_gem(struct g2d_data * g2d,struct g2d_cmdlist_node * node,struct drm_file * filp)768d87342c1SInki Dae static void g2d_unmap_cmdlist_gem(struct g2d_data *g2d,
769d87342c1SInki Dae 				  struct g2d_cmdlist_node *node,
770d87342c1SInki Dae 				  struct drm_file *filp)
771d7f1642cSJoonyoung Shim {
7729963cb6eSYoungJun Cho 	struct g2d_buf_info *buf_info = &node->buf_info;
773d87342c1SInki Dae 	int i;
774d7f1642cSJoonyoung Shim 
7759963cb6eSYoungJun Cho 	for (i = 0; i < buf_info->map_nr; i++) {
7762dec17c7SYoungJun Cho 		struct g2d_buf_desc *buf_desc;
7779963cb6eSYoungJun Cho 		enum g2d_reg_type reg_type;
7783aa2a5c1SMarek Szyprowski 		void *obj;
779d7f1642cSJoonyoung Shim 
7809963cb6eSYoungJun Cho 		reg_type = buf_info->reg_types[i];
7819963cb6eSYoungJun Cho 
7822dec17c7SYoungJun Cho 		buf_desc = &buf_info->descs[reg_type];
7833aa2a5c1SMarek Szyprowski 		obj = buf_info->obj[reg_type];
7849963cb6eSYoungJun Cho 
7859963cb6eSYoungJun Cho 		if (buf_info->types[reg_type] == BUF_TYPE_GEM)
7863aa2a5c1SMarek Szyprowski 			exynos_drm_gem_put(obj);
7872a3098ffSInki Dae 		else
7883aa2a5c1SMarek Szyprowski 			g2d_userptr_put_dma_addr(g2d, obj, false);
789d87342c1SInki Dae 
7909963cb6eSYoungJun Cho 		buf_info->reg_types[i] = REG_TYPE_NONE;
7913aa2a5c1SMarek Szyprowski 		buf_info->obj[reg_type] = NULL;
7929963cb6eSYoungJun Cho 		buf_info->types[reg_type] = 0;
7932dec17c7SYoungJun Cho 		memset(buf_desc, 0x00, sizeof(*buf_desc));
794d7f1642cSJoonyoung Shim 	}
795d87342c1SInki Dae 
7969963cb6eSYoungJun Cho 	buf_info->map_nr = 0;
797d7f1642cSJoonyoung Shim }
798d7f1642cSJoonyoung Shim 
g2d_dma_start(struct g2d_data * g2d,struct g2d_runqueue_node * runqueue_node)799d7f1642cSJoonyoung Shim static void g2d_dma_start(struct g2d_data *g2d,
800d7f1642cSJoonyoung Shim 			  struct g2d_runqueue_node *runqueue_node)
801d7f1642cSJoonyoung Shim {
802d7f1642cSJoonyoung Shim 	struct g2d_cmdlist_node *node =
803d7f1642cSJoonyoung Shim 				list_first_entry(&runqueue_node->run_cmdlist,
804d7f1642cSJoonyoung Shim 						struct g2d_cmdlist_node, list);
805d7f1642cSJoonyoung Shim 
80622d6704dSTobias Jakobi 	set_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
807d7f1642cSJoonyoung Shim 	writel_relaxed(node->dma_addr, g2d->regs + G2D_DMA_SFR_BASE_ADDR);
808d7f1642cSJoonyoung Shim 	writel_relaxed(G2D_DMA_START, g2d->regs + G2D_DMA_COMMAND);
809d7f1642cSJoonyoung Shim }
810d7f1642cSJoonyoung Shim 
g2d_get_runqueue_node(struct g2d_data * g2d)811d7f1642cSJoonyoung Shim static struct g2d_runqueue_node *g2d_get_runqueue_node(struct g2d_data *g2d)
812d7f1642cSJoonyoung Shim {
813d7f1642cSJoonyoung Shim 	struct g2d_runqueue_node *runqueue_node;
814d7f1642cSJoonyoung Shim 
815d7f1642cSJoonyoung Shim 	if (list_empty(&g2d->runqueue))
816d7f1642cSJoonyoung Shim 		return NULL;
817d7f1642cSJoonyoung Shim 
818d7f1642cSJoonyoung Shim 	runqueue_node = list_first_entry(&g2d->runqueue,
819d7f1642cSJoonyoung Shim 					 struct g2d_runqueue_node, list);
820d7f1642cSJoonyoung Shim 	list_del_init(&runqueue_node->list);
821d7f1642cSJoonyoung Shim 	return runqueue_node;
822d7f1642cSJoonyoung Shim }
823d7f1642cSJoonyoung Shim 
g2d_free_runqueue_node(struct g2d_data * g2d,struct g2d_runqueue_node * runqueue_node)824d7f1642cSJoonyoung Shim static void g2d_free_runqueue_node(struct g2d_data *g2d,
825d7f1642cSJoonyoung Shim 				   struct g2d_runqueue_node *runqueue_node)
826d7f1642cSJoonyoung Shim {
827d87342c1SInki Dae 	struct g2d_cmdlist_node *node;
828d87342c1SInki Dae 
829d7f1642cSJoonyoung Shim 	mutex_lock(&g2d->cmdlist_mutex);
830d87342c1SInki Dae 	/*
831d87342c1SInki Dae 	 * commands in run_cmdlist have been completed so unmap all gem
832d87342c1SInki Dae 	 * objects in each command node so that they are unreferenced.
833d87342c1SInki Dae 	 */
834d87342c1SInki Dae 	list_for_each_entry(node, &runqueue_node->run_cmdlist, list)
835d87342c1SInki Dae 		g2d_unmap_cmdlist_gem(g2d, node, runqueue_node->filp);
836d7f1642cSJoonyoung Shim 	list_splice_tail_init(&runqueue_node->run_cmdlist, &g2d->free_cmdlist);
837d7f1642cSJoonyoung Shim 	mutex_unlock(&g2d->cmdlist_mutex);
838d7f1642cSJoonyoung Shim 
839d7f1642cSJoonyoung Shim 	kmem_cache_free(g2d->runqueue_slab, runqueue_node);
840d7f1642cSJoonyoung Shim }
841d7f1642cSJoonyoung Shim 
84253327374STobias Jakobi /**
84353327374STobias Jakobi  * g2d_remove_runqueue_nodes - remove items from the list of runqueue nodes
84453327374STobias Jakobi  * @g2d: G2D state object
84553327374STobias Jakobi  * @file: if not zero, only remove items with this DRM file
84653327374STobias Jakobi  *
84753327374STobias Jakobi  * Has to be called under runqueue lock.
84853327374STobias Jakobi  */
g2d_remove_runqueue_nodes(struct g2d_data * g2d,struct drm_file * file)84953327374STobias Jakobi static void g2d_remove_runqueue_nodes(struct g2d_data *g2d, struct drm_file *file)
850d7f1642cSJoonyoung Shim {
85153327374STobias Jakobi 	struct g2d_runqueue_node *node, *n;
85253327374STobias Jakobi 
85353327374STobias Jakobi 	if (list_empty(&g2d->runqueue))
85453327374STobias Jakobi 		return;
85553327374STobias Jakobi 
85653327374STobias Jakobi 	list_for_each_entry_safe(node, n, &g2d->runqueue, list) {
85753327374STobias Jakobi 		if (file && node->filp != file)
85853327374STobias Jakobi 			continue;
85953327374STobias Jakobi 
86053327374STobias Jakobi 		list_del_init(&node->list);
86153327374STobias Jakobi 		g2d_free_runqueue_node(g2d, node);
86253327374STobias Jakobi 	}
863d7f1642cSJoonyoung Shim }
864d7f1642cSJoonyoung Shim 
g2d_runqueue_worker(struct work_struct * work)865d7f1642cSJoonyoung Shim static void g2d_runqueue_worker(struct work_struct *work)
866d7f1642cSJoonyoung Shim {
867d7f1642cSJoonyoung Shim 	struct g2d_data *g2d = container_of(work, struct g2d_data,
868d7f1642cSJoonyoung Shim 					    runqueue_work);
86922d6704dSTobias Jakobi 	struct g2d_runqueue_node *runqueue_node;
87022d6704dSTobias Jakobi 
87122d6704dSTobias Jakobi 	/*
87222d6704dSTobias Jakobi 	 * The engine is busy and the completion of the current node is going
87322d6704dSTobias Jakobi 	 * to poke the runqueue worker, so nothing to do here.
87422d6704dSTobias Jakobi 	 */
87522d6704dSTobias Jakobi 	if (test_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags))
87622d6704dSTobias Jakobi 		return;
877d7f1642cSJoonyoung Shim 
878d7f1642cSJoonyoung Shim 	mutex_lock(&g2d->runqueue_mutex);
879d7f1642cSJoonyoung Shim 
88022d6704dSTobias Jakobi 	runqueue_node = g2d->runqueue_node;
881d7f1642cSJoonyoung Shim 	g2d->runqueue_node = NULL;
88222d6704dSTobias Jakobi 
88322d6704dSTobias Jakobi 	if (runqueue_node) {
8847c3fc2b5STobias Jakobi 		pm_runtime_mark_last_busy(g2d->dev);
8857c3fc2b5STobias Jakobi 		pm_runtime_put_autosuspend(g2d->dev);
88622d6704dSTobias Jakobi 
88722d6704dSTobias Jakobi 		complete(&runqueue_node->complete);
88822d6704dSTobias Jakobi 		if (runqueue_node->async)
88922d6704dSTobias Jakobi 			g2d_free_runqueue_node(g2d, runqueue_node);
89022d6704dSTobias Jakobi 	}
89122d6704dSTobias Jakobi 
89222d6704dSTobias Jakobi 	if (!test_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags)) {
89322d6704dSTobias Jakobi 		g2d->runqueue_node = g2d_get_runqueue_node(g2d);
89422d6704dSTobias Jakobi 
89522d6704dSTobias Jakobi 		if (g2d->runqueue_node) {
896445d3bedSInki Dae 			int ret;
897445d3bedSInki Dae 
898445d3bedSInki Dae 			ret = pm_runtime_resume_and_get(g2d->dev);
899445d3bedSInki Dae 			if (ret < 0) {
900445d3bedSInki Dae 				dev_err(g2d->dev, "failed to enable G2D device.\n");
901b74a29faSWei Yongjun 				goto out;
902445d3bedSInki Dae 			}
903445d3bedSInki Dae 
90422d6704dSTobias Jakobi 			g2d_dma_start(g2d, g2d->runqueue_node);
90522d6704dSTobias Jakobi 		}
90622d6704dSTobias Jakobi 	}
90722d6704dSTobias Jakobi 
908b74a29faSWei Yongjun out:
909d7f1642cSJoonyoung Shim 	mutex_unlock(&g2d->runqueue_mutex);
910d7f1642cSJoonyoung Shim }
911d7f1642cSJoonyoung Shim 
g2d_finish_event(struct g2d_data * g2d,u32 cmdlist_no)912d7f1642cSJoonyoung Shim static void g2d_finish_event(struct g2d_data *g2d, u32 cmdlist_no)
913d7f1642cSJoonyoung Shim {
914eb4d9796SMarek Szyprowski 	struct drm_device *drm_dev = g2d->drm_dev;
915d7f1642cSJoonyoung Shim 	struct g2d_runqueue_node *runqueue_node = g2d->runqueue_node;
916d7f1642cSJoonyoung Shim 	struct drm_exynos_pending_g2d_event *e;
917a588a8bbSArnd Bergmann 	struct timespec64 now;
918d7f1642cSJoonyoung Shim 
919d7f1642cSJoonyoung Shim 	if (list_empty(&runqueue_node->event_list))
920d7f1642cSJoonyoung Shim 		return;
921d7f1642cSJoonyoung Shim 
922d7f1642cSJoonyoung Shim 	e = list_first_entry(&runqueue_node->event_list,
923d7f1642cSJoonyoung Shim 			     struct drm_exynos_pending_g2d_event, base.link);
924d7f1642cSJoonyoung Shim 
925a588a8bbSArnd Bergmann 	ktime_get_ts64(&now);
926d7f1642cSJoonyoung Shim 	e->event.tv_sec = now.tv_sec;
927a588a8bbSArnd Bergmann 	e->event.tv_usec = now.tv_nsec / NSEC_PER_USEC;
928d7f1642cSJoonyoung Shim 	e->event.cmdlist_no = cmdlist_no;
929d7f1642cSJoonyoung Shim 
930fb740cf2SDaniel Vetter 	drm_send_event(drm_dev, &e->base);
931d7f1642cSJoonyoung Shim }
932d7f1642cSJoonyoung Shim 
g2d_irq_handler(int irq,void * dev_id)933d7f1642cSJoonyoung Shim static irqreturn_t g2d_irq_handler(int irq, void *dev_id)
934d7f1642cSJoonyoung Shim {
935d7f1642cSJoonyoung Shim 	struct g2d_data *g2d = dev_id;
936d7f1642cSJoonyoung Shim 	u32 pending;
937d7f1642cSJoonyoung Shim 
938d7f1642cSJoonyoung Shim 	pending = readl_relaxed(g2d->regs + G2D_INTC_PEND);
939d7f1642cSJoonyoung Shim 	if (pending)
940d7f1642cSJoonyoung Shim 		writel_relaxed(pending, g2d->regs + G2D_INTC_PEND);
941d7f1642cSJoonyoung Shim 
942d7f1642cSJoonyoung Shim 	if (pending & G2D_INTP_GCMD_FIN) {
943d7f1642cSJoonyoung Shim 		u32 cmdlist_no = readl_relaxed(g2d->regs + G2D_DMA_STATUS);
944d7f1642cSJoonyoung Shim 
945d7f1642cSJoonyoung Shim 		cmdlist_no = (cmdlist_no & G2D_DMA_LIST_DONE_COUNT) >>
946d7f1642cSJoonyoung Shim 						G2D_DMA_LIST_DONE_COUNT_OFFSET;
947d7f1642cSJoonyoung Shim 
948d7f1642cSJoonyoung Shim 		g2d_finish_event(g2d, cmdlist_no);
949d7f1642cSJoonyoung Shim 
950d7f1642cSJoonyoung Shim 		writel_relaxed(0, g2d->regs + G2D_DMA_HOLD_CMD);
951d7f1642cSJoonyoung Shim 		if (!(pending & G2D_INTP_ACMD_FIN)) {
952d7f1642cSJoonyoung Shim 			writel_relaxed(G2D_DMA_CONTINUE,
953d7f1642cSJoonyoung Shim 					g2d->regs + G2D_DMA_COMMAND);
954d7f1642cSJoonyoung Shim 		}
955d7f1642cSJoonyoung Shim 	}
956d7f1642cSJoonyoung Shim 
95722d6704dSTobias Jakobi 	if (pending & G2D_INTP_ACMD_FIN) {
95822d6704dSTobias Jakobi 		clear_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
959d7f1642cSJoonyoung Shim 		queue_work(g2d->g2d_workq, &g2d->runqueue_work);
96022d6704dSTobias Jakobi 	}
961d7f1642cSJoonyoung Shim 
962d7f1642cSJoonyoung Shim 	return IRQ_HANDLED;
963d7f1642cSJoonyoung Shim }
964d7f1642cSJoonyoung Shim 
965134a0fe9STobias Jakobi /**
966134a0fe9STobias Jakobi  * g2d_wait_finish - wait for the G2D engine to finish the current runqueue node
967134a0fe9STobias Jakobi  * @g2d: G2D state object
968134a0fe9STobias Jakobi  * @file: if not zero, only wait if the current runqueue node belongs
969134a0fe9STobias Jakobi  *        to the DRM file
970134a0fe9STobias Jakobi  *
971134a0fe9STobias Jakobi  * Should the engine not become idle after a 100ms timeout, a hardware
972134a0fe9STobias Jakobi  * reset is issued.
973134a0fe9STobias Jakobi  */
g2d_wait_finish(struct g2d_data * g2d,struct drm_file * file)974134a0fe9STobias Jakobi static void g2d_wait_finish(struct g2d_data *g2d, struct drm_file *file)
975134a0fe9STobias Jakobi {
976134a0fe9STobias Jakobi 	struct device *dev = g2d->dev;
977134a0fe9STobias Jakobi 
978134a0fe9STobias Jakobi 	struct g2d_runqueue_node *runqueue_node = NULL;
979134a0fe9STobias Jakobi 	unsigned int tries = 10;
980134a0fe9STobias Jakobi 
981134a0fe9STobias Jakobi 	mutex_lock(&g2d->runqueue_mutex);
982134a0fe9STobias Jakobi 
983134a0fe9STobias Jakobi 	/* If no node is currently processed, we have nothing to do. */
984134a0fe9STobias Jakobi 	if (!g2d->runqueue_node)
985134a0fe9STobias Jakobi 		goto out;
986134a0fe9STobias Jakobi 
987134a0fe9STobias Jakobi 	runqueue_node = g2d->runqueue_node;
988134a0fe9STobias Jakobi 
989134a0fe9STobias Jakobi 	/* Check if the currently processed item belongs to us. */
990134a0fe9STobias Jakobi 	if (file && runqueue_node->filp != file)
991134a0fe9STobias Jakobi 		goto out;
992134a0fe9STobias Jakobi 
993134a0fe9STobias Jakobi 	mutex_unlock(&g2d->runqueue_mutex);
994134a0fe9STobias Jakobi 
995134a0fe9STobias Jakobi 	/* Wait for the G2D engine to finish. */
996134a0fe9STobias Jakobi 	while (tries-- && (g2d->runqueue_node == runqueue_node))
997134a0fe9STobias Jakobi 		mdelay(10);
998134a0fe9STobias Jakobi 
999134a0fe9STobias Jakobi 	mutex_lock(&g2d->runqueue_mutex);
1000134a0fe9STobias Jakobi 
1001134a0fe9STobias Jakobi 	if (g2d->runqueue_node != runqueue_node)
1002134a0fe9STobias Jakobi 		goto out;
1003134a0fe9STobias Jakobi 
1004134a0fe9STobias Jakobi 	dev_err(dev, "wait timed out, resetting engine...\n");
1005134a0fe9STobias Jakobi 	g2d_hw_reset(g2d);
1006134a0fe9STobias Jakobi 
1007134a0fe9STobias Jakobi 	/*
1008134a0fe9STobias Jakobi 	 * After the hardware reset of the engine we are going to loose
1009134a0fe9STobias Jakobi 	 * the IRQ which triggers the PM runtime put().
1010134a0fe9STobias Jakobi 	 * So do this manually here.
1011134a0fe9STobias Jakobi 	 */
10127c3fc2b5STobias Jakobi 	pm_runtime_mark_last_busy(dev);
10137c3fc2b5STobias Jakobi 	pm_runtime_put_autosuspend(dev);
1014134a0fe9STobias Jakobi 
1015134a0fe9STobias Jakobi 	complete(&runqueue_node->complete);
1016134a0fe9STobias Jakobi 	if (runqueue_node->async)
1017134a0fe9STobias Jakobi 		g2d_free_runqueue_node(g2d, runqueue_node);
1018134a0fe9STobias Jakobi 
1019134a0fe9STobias Jakobi out:
1020134a0fe9STobias Jakobi 	mutex_unlock(&g2d->runqueue_mutex);
1021134a0fe9STobias Jakobi }
1022134a0fe9STobias Jakobi 
g2d_check_reg_offset(struct g2d_data * g2d,struct g2d_cmdlist_node * node,int nr,bool for_addr)1023eb4d9796SMarek Szyprowski static int g2d_check_reg_offset(struct g2d_data *g2d,
10242a3098ffSInki Dae 				struct g2d_cmdlist_node *node,
1025d7f1642cSJoonyoung Shim 				int nr, bool for_addr)
1026d7f1642cSJoonyoung Shim {
10272a3098ffSInki Dae 	struct g2d_cmdlist *cmdlist = node->cmdlist;
1028d7f1642cSJoonyoung Shim 	int reg_offset;
1029d7f1642cSJoonyoung Shim 	int index;
1030d7f1642cSJoonyoung Shim 	int i;
1031d7f1642cSJoonyoung Shim 
1032d7f1642cSJoonyoung Shim 	for (i = 0; i < nr; i++) {
10339963cb6eSYoungJun Cho 		struct g2d_buf_info *buf_info = &node->buf_info;
10342dec17c7SYoungJun Cho 		struct g2d_buf_desc *buf_desc;
10359963cb6eSYoungJun Cho 		enum g2d_reg_type reg_type;
10362dec17c7SYoungJun Cho 		unsigned long value;
10379963cb6eSYoungJun Cho 
1038d7f1642cSJoonyoung Shim 		index = cmdlist->last - 2 * (i + 1);
10392a3098ffSInki Dae 
1040d7f1642cSJoonyoung Shim 		reg_offset = cmdlist->data[index] & ~0xfffff000;
1041d7f1642cSJoonyoung Shim 		if (reg_offset < G2D_VALID_START || reg_offset > G2D_VALID_END)
1042d7f1642cSJoonyoung Shim 			goto err;
1043d7f1642cSJoonyoung Shim 		if (reg_offset % 4)
1044d7f1642cSJoonyoung Shim 			goto err;
1045d7f1642cSJoonyoung Shim 
1046d7f1642cSJoonyoung Shim 		switch (reg_offset) {
1047d7f1642cSJoonyoung Shim 		case G2D_SRC_BASE_ADDR:
1048d7f1642cSJoonyoung Shim 		case G2D_SRC_PLANE2_BASE_ADDR:
1049d7f1642cSJoonyoung Shim 		case G2D_DST_BASE_ADDR:
1050d7f1642cSJoonyoung Shim 		case G2D_DST_PLANE2_BASE_ADDR:
1051d7f1642cSJoonyoung Shim 		case G2D_PAT_BASE_ADDR:
1052d7f1642cSJoonyoung Shim 		case G2D_MSK_BASE_ADDR:
1053d7f1642cSJoonyoung Shim 			if (!for_addr)
1054d7f1642cSJoonyoung Shim 				goto err;
10552a3098ffSInki Dae 
10566f83d208SInki Dae 			reg_type = g2d_get_reg_type(g2d, reg_offset);
10579963cb6eSYoungJun Cho 
10589963cb6eSYoungJun Cho 			/* check userptr buffer type. */
10599963cb6eSYoungJun Cho 			if ((cmdlist->data[index] & ~0x7fffffff) >> 31) {
10609963cb6eSYoungJun Cho 				buf_info->types[reg_type] = BUF_TYPE_USERPTR;
10619963cb6eSYoungJun Cho 				cmdlist->data[index] &= ~G2D_BUF_USERPTR;
10629963cb6eSYoungJun Cho 			} else
10639963cb6eSYoungJun Cho 				buf_info->types[reg_type] = BUF_TYPE_GEM;
1064d7f1642cSJoonyoung Shim 			break;
1065ed4dc271STobias Jakobi 		case G2D_SRC_STRIDE:
1066ed4dc271STobias Jakobi 		case G2D_DST_STRIDE:
1067179239a7STobias Jakobi 			if (for_addr)
1068179239a7STobias Jakobi 				goto err;
1069179239a7STobias Jakobi 
10706f83d208SInki Dae 			reg_type = g2d_get_reg_type(g2d, reg_offset);
1071179239a7STobias Jakobi 
1072179239a7STobias Jakobi 			buf_desc = &buf_info->descs[reg_type];
1073179239a7STobias Jakobi 			buf_desc->stride = cmdlist->data[index + 1];
1074179239a7STobias Jakobi 			break;
10752dec17c7SYoungJun Cho 		case G2D_SRC_COLOR_MODE:
10762dec17c7SYoungJun Cho 		case G2D_DST_COLOR_MODE:
10772dec17c7SYoungJun Cho 			if (for_addr)
10782dec17c7SYoungJun Cho 				goto err;
10792dec17c7SYoungJun Cho 
10806f83d208SInki Dae 			reg_type = g2d_get_reg_type(g2d, reg_offset);
10812dec17c7SYoungJun Cho 
10822dec17c7SYoungJun Cho 			buf_desc = &buf_info->descs[reg_type];
10832dec17c7SYoungJun Cho 			value = cmdlist->data[index + 1];
10842dec17c7SYoungJun Cho 
10852dec17c7SYoungJun Cho 			buf_desc->format = value & 0xf;
10862dec17c7SYoungJun Cho 			break;
10872dec17c7SYoungJun Cho 		case G2D_SRC_LEFT_TOP:
10882dec17c7SYoungJun Cho 		case G2D_DST_LEFT_TOP:
10892dec17c7SYoungJun Cho 			if (for_addr)
10902dec17c7SYoungJun Cho 				goto err;
10912dec17c7SYoungJun Cho 
10926f83d208SInki Dae 			reg_type = g2d_get_reg_type(g2d, reg_offset);
10932dec17c7SYoungJun Cho 
10942dec17c7SYoungJun Cho 			buf_desc = &buf_info->descs[reg_type];
10952dec17c7SYoungJun Cho 			value = cmdlist->data[index + 1];
10962dec17c7SYoungJun Cho 
10972dec17c7SYoungJun Cho 			buf_desc->left_x = value & 0x1fff;
10982dec17c7SYoungJun Cho 			buf_desc->top_y = (value & 0x1fff0000) >> 16;
10992dec17c7SYoungJun Cho 			break;
11002dec17c7SYoungJun Cho 		case G2D_SRC_RIGHT_BOTTOM:
11012dec17c7SYoungJun Cho 		case G2D_DST_RIGHT_BOTTOM:
11022dec17c7SYoungJun Cho 			if (for_addr)
11032dec17c7SYoungJun Cho 				goto err;
11042dec17c7SYoungJun Cho 
11056f83d208SInki Dae 			reg_type = g2d_get_reg_type(g2d, reg_offset);
11062dec17c7SYoungJun Cho 
11072dec17c7SYoungJun Cho 			buf_desc = &buf_info->descs[reg_type];
11082dec17c7SYoungJun Cho 			value = cmdlist->data[index + 1];
11092dec17c7SYoungJun Cho 
11102dec17c7SYoungJun Cho 			buf_desc->right_x = value & 0x1fff;
11112dec17c7SYoungJun Cho 			buf_desc->bottom_y = (value & 0x1fff0000) >> 16;
11122dec17c7SYoungJun Cho 			break;
1113d7f1642cSJoonyoung Shim 		default:
1114d7f1642cSJoonyoung Shim 			if (for_addr)
1115d7f1642cSJoonyoung Shim 				goto err;
1116d7f1642cSJoonyoung Shim 			break;
1117d7f1642cSJoonyoung Shim 		}
1118d7f1642cSJoonyoung Shim 	}
1119d7f1642cSJoonyoung Shim 
1120d7f1642cSJoonyoung Shim 	return 0;
1121d7f1642cSJoonyoung Shim 
1122d7f1642cSJoonyoung Shim err:
1123eb4d9796SMarek Szyprowski 	dev_err(g2d->dev, "Bad register offset: 0x%lx\n", cmdlist->data[index]);
1124d7f1642cSJoonyoung Shim 	return -EINVAL;
1125d7f1642cSJoonyoung Shim }
1126d7f1642cSJoonyoung Shim 
1127d7f1642cSJoonyoung Shim /* ioctl functions */
exynos_g2d_get_ver_ioctl(struct drm_device * drm_dev,void * data,struct drm_file * file)1128d7f1642cSJoonyoung Shim int exynos_g2d_get_ver_ioctl(struct drm_device *drm_dev, void *data,
1129d7f1642cSJoonyoung Shim 			     struct drm_file *file)
1130d7f1642cSJoonyoung Shim {
1131d7f1642cSJoonyoung Shim 	struct drm_exynos_g2d_get_ver *ver = data;
1132d7f1642cSJoonyoung Shim 
1133d7f1642cSJoonyoung Shim 	ver->major = G2D_HW_MAJOR_VER;
1134d7f1642cSJoonyoung Shim 	ver->minor = G2D_HW_MINOR_VER;
1135d7f1642cSJoonyoung Shim 
1136d7f1642cSJoonyoung Shim 	return 0;
1137d7f1642cSJoonyoung Shim }
1138d7f1642cSJoonyoung Shim 
exynos_g2d_set_cmdlist_ioctl(struct drm_device * drm_dev,void * data,struct drm_file * file)1139d7f1642cSJoonyoung Shim int exynos_g2d_set_cmdlist_ioctl(struct drm_device *drm_dev, void *data,
1140d7f1642cSJoonyoung Shim 				 struct drm_file *file)
1141d7f1642cSJoonyoung Shim {
1142d7f1642cSJoonyoung Shim 	struct drm_exynos_file_private *file_priv = file->driver_priv;
1143eb4d9796SMarek Szyprowski 	struct exynos_drm_private *priv = drm_dev->dev_private;
1144eb4d9796SMarek Szyprowski 	struct g2d_data *g2d = dev_get_drvdata(priv->g2d_dev);
1145d7f1642cSJoonyoung Shim 	struct drm_exynos_g2d_set_cmdlist *req = data;
1146d7f1642cSJoonyoung Shim 	struct drm_exynos_g2d_cmd *cmd;
1147d7f1642cSJoonyoung Shim 	struct drm_exynos_pending_g2d_event *e;
1148d7f1642cSJoonyoung Shim 	struct g2d_cmdlist_node *node;
1149d7f1642cSJoonyoung Shim 	struct g2d_cmdlist *cmdlist;
1150d7f1642cSJoonyoung Shim 	int size;
1151d7f1642cSJoonyoung Shim 	int ret;
1152d7f1642cSJoonyoung Shim 
1153d7f1642cSJoonyoung Shim 	node = g2d_get_cmdlist(g2d);
1154d7f1642cSJoonyoung Shim 	if (!node)
1155d7f1642cSJoonyoung Shim 		return -ENOMEM;
1156d7f1642cSJoonyoung Shim 
1157e41456bfSJoonyoung Shim 	/*
1158e41456bfSJoonyoung Shim 	 * To avoid an integer overflow for the later size computations, we
1159e41456bfSJoonyoung Shim 	 * enforce a maximum number of submitted commands here. This limit is
1160e41456bfSJoonyoung Shim 	 * sufficient for all conceivable usage cases of the G2D.
1161e41456bfSJoonyoung Shim 	 */
1162e41456bfSJoonyoung Shim 	if (req->cmd_nr > G2D_CMDLIST_DATA_NUM ||
1163e41456bfSJoonyoung Shim 	    req->cmd_buf_nr > G2D_CMDLIST_DATA_NUM) {
1164eb4d9796SMarek Szyprowski 		dev_err(g2d->dev, "number of submitted G2D commands exceeds limit\n");
1165e41456bfSJoonyoung Shim 		return -EINVAL;
1166e41456bfSJoonyoung Shim 	}
1167e41456bfSJoonyoung Shim 
1168d7f1642cSJoonyoung Shim 	node->event = NULL;
1169d7f1642cSJoonyoung Shim 
1170d7f1642cSJoonyoung Shim 	if (req->event_type != G2D_EVENT_NOT) {
1171d7f1642cSJoonyoung Shim 		e = kzalloc(sizeof(*node->event), GFP_KERNEL);
1172d7f1642cSJoonyoung Shim 		if (!e) {
1173d7f1642cSJoonyoung Shim 			ret = -ENOMEM;
1174d7f1642cSJoonyoung Shim 			goto err;
1175d7f1642cSJoonyoung Shim 		}
1176d7f1642cSJoonyoung Shim 
1177d7f1642cSJoonyoung Shim 		e->event.base.type = DRM_EXYNOS_G2D_EVENT;
1178d7f1642cSJoonyoung Shim 		e->event.base.length = sizeof(e->event);
1179d7f1642cSJoonyoung Shim 		e->event.user_data = req->user_data;
11807142a348SDaniel Vetter 
11817142a348SDaniel Vetter 		ret = drm_event_reserve_init(drm_dev, file, &e->base, &e->event.base);
11827142a348SDaniel Vetter 		if (ret) {
11837142a348SDaniel Vetter 			kfree(e);
11847142a348SDaniel Vetter 			goto err;
11857142a348SDaniel Vetter 		}
1186d7f1642cSJoonyoung Shim 
1187d7f1642cSJoonyoung Shim 		node->event = e;
1188d7f1642cSJoonyoung Shim 	}
1189d7f1642cSJoonyoung Shim 
1190d7f1642cSJoonyoung Shim 	cmdlist = node->cmdlist;
1191d7f1642cSJoonyoung Shim 
1192d7f1642cSJoonyoung Shim 	cmdlist->last = 0;
1193d7f1642cSJoonyoung Shim 
1194d7f1642cSJoonyoung Shim 	/*
1195d7f1642cSJoonyoung Shim 	 * If don't clear SFR registers, the cmdlist is affected by register
1196d7f1642cSJoonyoung Shim 	 * values of previous cmdlist. G2D hw executes SFR clear command and
1197d7f1642cSJoonyoung Shim 	 * a next command at the same time then the next command is ignored and
1198d7f1642cSJoonyoung Shim 	 * is executed rightly from next next command, so needs a dummy command
1199d7f1642cSJoonyoung Shim 	 * to next command of SFR clear command.
1200d7f1642cSJoonyoung Shim 	 */
1201d7f1642cSJoonyoung Shim 	cmdlist->data[cmdlist->last++] = G2D_SOFT_RESET;
1202d7f1642cSJoonyoung Shim 	cmdlist->data[cmdlist->last++] = G2D_SFRCLEAR;
1203d7f1642cSJoonyoung Shim 	cmdlist->data[cmdlist->last++] = G2D_SRC_BASE_ADDR;
1204d7f1642cSJoonyoung Shim 	cmdlist->data[cmdlist->last++] = 0;
1205d7f1642cSJoonyoung Shim 
12067ad01814SYoungJun Cho 	/*
12077ad01814SYoungJun Cho 	 * 'LIST_HOLD' command should be set to the DMA_HOLD_CMD_REG
12087ad01814SYoungJun Cho 	 * and GCF bit should be set to INTEN register if user wants
12097ad01814SYoungJun Cho 	 * G2D interrupt event once current command list execution is
12107ad01814SYoungJun Cho 	 * finished.
12117ad01814SYoungJun Cho 	 * Otherwise only ACF bit should be set to INTEN register so
1212c6b78bc8SMasanari Iida 	 * that one interrupt is occurred after all command lists
12137ad01814SYoungJun Cho 	 * have been completed.
12147ad01814SYoungJun Cho 	 */
1215d7f1642cSJoonyoung Shim 	if (node->event) {
12167ad01814SYoungJun Cho 		cmdlist->data[cmdlist->last++] = G2D_INTEN;
12177ad01814SYoungJun Cho 		cmdlist->data[cmdlist->last++] = G2D_INTEN_ACF | G2D_INTEN_GCF;
1218d7f1642cSJoonyoung Shim 		cmdlist->data[cmdlist->last++] = G2D_DMA_HOLD_CMD;
1219d7f1642cSJoonyoung Shim 		cmdlist->data[cmdlist->last++] = G2D_LIST_HOLD;
12207ad01814SYoungJun Cho 	} else {
12217ad01814SYoungJun Cho 		cmdlist->data[cmdlist->last++] = G2D_INTEN;
12227ad01814SYoungJun Cho 		cmdlist->data[cmdlist->last++] = G2D_INTEN_ACF;
1223d7f1642cSJoonyoung Shim 	}
1224d7f1642cSJoonyoung Shim 
1225e41456bfSJoonyoung Shim 	/*
1226e41456bfSJoonyoung Shim 	 * Check the size of cmdlist. The 2 that is added last comes from
1227e41456bfSJoonyoung Shim 	 * the implicit G2D_BITBLT_START that is appended once we have
1228e41456bfSJoonyoung Shim 	 * checked all the submitted commands.
1229e41456bfSJoonyoung Shim 	 */
12302a3098ffSInki Dae 	size = cmdlist->last + req->cmd_nr * 2 + req->cmd_buf_nr * 2 + 2;
1231d7f1642cSJoonyoung Shim 	if (size > G2D_CMDLIST_DATA_NUM) {
1232eb4d9796SMarek Szyprowski 		dev_err(g2d->dev, "cmdlist size is too big\n");
1233d7f1642cSJoonyoung Shim 		ret = -EINVAL;
1234d7f1642cSJoonyoung Shim 		goto err_free_event;
1235d7f1642cSJoonyoung Shim 	}
1236d7f1642cSJoonyoung Shim 
1237c5f2f0c4SMarek Szyprowski 	cmd = (struct drm_exynos_g2d_cmd *)(unsigned long)req->cmd;
1238d7f1642cSJoonyoung Shim 
1239d7f1642cSJoonyoung Shim 	if (copy_from_user(cmdlist->data + cmdlist->last,
1240d7f1642cSJoonyoung Shim 				(void __user *)cmd,
1241d7f1642cSJoonyoung Shim 				sizeof(*cmd) * req->cmd_nr)) {
1242d7f1642cSJoonyoung Shim 		ret = -EFAULT;
1243d7f1642cSJoonyoung Shim 		goto err_free_event;
1244d7f1642cSJoonyoung Shim 	}
1245d7f1642cSJoonyoung Shim 	cmdlist->last += req->cmd_nr * 2;
1246d7f1642cSJoonyoung Shim 
1247eb4d9796SMarek Szyprowski 	ret = g2d_check_reg_offset(g2d, node, req->cmd_nr, false);
1248d7f1642cSJoonyoung Shim 	if (ret < 0)
1249d7f1642cSJoonyoung Shim 		goto err_free_event;
1250d7f1642cSJoonyoung Shim 
12519963cb6eSYoungJun Cho 	node->buf_info.map_nr = req->cmd_buf_nr;
12522a3098ffSInki Dae 	if (req->cmd_buf_nr) {
12532a3098ffSInki Dae 		struct drm_exynos_g2d_cmd *cmd_buf;
1254d7f1642cSJoonyoung Shim 
1255c5f2f0c4SMarek Szyprowski 		cmd_buf = (struct drm_exynos_g2d_cmd *)
1256c5f2f0c4SMarek Szyprowski 				(unsigned long)req->cmd_buf;
1257d7f1642cSJoonyoung Shim 
1258d7f1642cSJoonyoung Shim 		if (copy_from_user(cmdlist->data + cmdlist->last,
12592a3098ffSInki Dae 					(void __user *)cmd_buf,
12602a3098ffSInki Dae 					sizeof(*cmd_buf) * req->cmd_buf_nr)) {
1261d7f1642cSJoonyoung Shim 			ret = -EFAULT;
1262d7f1642cSJoonyoung Shim 			goto err_free_event;
1263d7f1642cSJoonyoung Shim 		}
12642a3098ffSInki Dae 		cmdlist->last += req->cmd_buf_nr * 2;
1265d7f1642cSJoonyoung Shim 
1266eb4d9796SMarek Szyprowski 		ret = g2d_check_reg_offset(g2d, node, req->cmd_buf_nr, true);
1267d7f1642cSJoonyoung Shim 		if (ret < 0)
1268d7f1642cSJoonyoung Shim 			goto err_free_event;
1269d7f1642cSJoonyoung Shim 
1270d87342c1SInki Dae 		ret = g2d_map_cmdlist_gem(g2d, node, drm_dev, file);
1271d7f1642cSJoonyoung Shim 		if (ret < 0)
1272d7f1642cSJoonyoung Shim 			goto err_unmap;
1273d7f1642cSJoonyoung Shim 	}
1274d7f1642cSJoonyoung Shim 
1275d7f1642cSJoonyoung Shim 	cmdlist->data[cmdlist->last++] = G2D_BITBLT_START;
1276d7f1642cSJoonyoung Shim 	cmdlist->data[cmdlist->last++] = G2D_START_BITBLT;
1277d7f1642cSJoonyoung Shim 
1278d7f1642cSJoonyoung Shim 	/* head */
1279d7f1642cSJoonyoung Shim 	cmdlist->head = cmdlist->last / 2;
1280d7f1642cSJoonyoung Shim 
1281d7f1642cSJoonyoung Shim 	/* tail */
1282d7f1642cSJoonyoung Shim 	cmdlist->data[cmdlist->last] = 0;
1283d7f1642cSJoonyoung Shim 
1284eb4d9796SMarek Szyprowski 	g2d_add_cmdlist_to_inuse(file_priv, node);
1285d7f1642cSJoonyoung Shim 
1286d7f1642cSJoonyoung Shim 	return 0;
1287d7f1642cSJoonyoung Shim 
1288d7f1642cSJoonyoung Shim err_unmap:
1289d87342c1SInki Dae 	g2d_unmap_cmdlist_gem(g2d, node, file);
1290d7f1642cSJoonyoung Shim err_free_event:
12917142a348SDaniel Vetter 	if (node->event)
12927142a348SDaniel Vetter 		drm_event_cancel_free(drm_dev, &node->event->base);
1293d7f1642cSJoonyoung Shim err:
1294d7f1642cSJoonyoung Shim 	g2d_put_cmdlist(g2d, node);
1295d7f1642cSJoonyoung Shim 	return ret;
1296d7f1642cSJoonyoung Shim }
1297d7f1642cSJoonyoung Shim 
exynos_g2d_exec_ioctl(struct drm_device * drm_dev,void * data,struct drm_file * file)1298d7f1642cSJoonyoung Shim int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data,
1299d7f1642cSJoonyoung Shim 			  struct drm_file *file)
1300d7f1642cSJoonyoung Shim {
1301d7f1642cSJoonyoung Shim 	struct drm_exynos_file_private *file_priv = file->driver_priv;
1302eb4d9796SMarek Szyprowski 	struct exynos_drm_private *priv = drm_dev->dev_private;
1303eb4d9796SMarek Szyprowski 	struct g2d_data *g2d = dev_get_drvdata(priv->g2d_dev);
1304d7f1642cSJoonyoung Shim 	struct drm_exynos_g2d_exec *req = data;
1305d7f1642cSJoonyoung Shim 	struct g2d_runqueue_node *runqueue_node;
1306d7f1642cSJoonyoung Shim 	struct list_head *run_cmdlist;
1307d7f1642cSJoonyoung Shim 	struct list_head *event_list;
1308d7f1642cSJoonyoung Shim 
1309d7f1642cSJoonyoung Shim 	runqueue_node = kmem_cache_alloc(g2d->runqueue_slab, GFP_KERNEL);
13106f0a6029SMarkus Elfring 	if (!runqueue_node)
1311d7f1642cSJoonyoung Shim 		return -ENOMEM;
13126f0a6029SMarkus Elfring 
1313d7f1642cSJoonyoung Shim 	run_cmdlist = &runqueue_node->run_cmdlist;
1314d7f1642cSJoonyoung Shim 	event_list = &runqueue_node->event_list;
1315d7f1642cSJoonyoung Shim 	INIT_LIST_HEAD(run_cmdlist);
1316d7f1642cSJoonyoung Shim 	INIT_LIST_HEAD(event_list);
1317d7f1642cSJoonyoung Shim 	init_completion(&runqueue_node->complete);
1318d7f1642cSJoonyoung Shim 	runqueue_node->async = req->async;
1319d7f1642cSJoonyoung Shim 
1320eb4d9796SMarek Szyprowski 	list_splice_init(&file_priv->inuse_cmdlist, run_cmdlist);
1321eb4d9796SMarek Szyprowski 	list_splice_init(&file_priv->event_list, event_list);
1322d7f1642cSJoonyoung Shim 
1323d7f1642cSJoonyoung Shim 	if (list_empty(run_cmdlist)) {
1324eb4d9796SMarek Szyprowski 		dev_err(g2d->dev, "there is no inuse cmdlist\n");
1325d7f1642cSJoonyoung Shim 		kmem_cache_free(g2d->runqueue_slab, runqueue_node);
1326d7f1642cSJoonyoung Shim 		return -EPERM;
1327d7f1642cSJoonyoung Shim 	}
1328d7f1642cSJoonyoung Shim 
1329d7f1642cSJoonyoung Shim 	mutex_lock(&g2d->runqueue_mutex);
13306b6bae24SInki Dae 	runqueue_node->pid = current->pid;
1331d87342c1SInki Dae 	runqueue_node->filp = file;
1332d7f1642cSJoonyoung Shim 	list_add_tail(&runqueue_node->list, &g2d->runqueue);
1333d7f1642cSJoonyoung Shim 	mutex_unlock(&g2d->runqueue_mutex);
1334d7f1642cSJoonyoung Shim 
133522d6704dSTobias Jakobi 	/* Let the runqueue know that there is work to do. */
133622d6704dSTobias Jakobi 	queue_work(g2d->g2d_workq, &g2d->runqueue_work);
133722d6704dSTobias Jakobi 
1338*48bfd025SMin Li 	if (req->async)
1339d7f1642cSJoonyoung Shim 		goto out;
1340d7f1642cSJoonyoung Shim 
1341d7f1642cSJoonyoung Shim 	wait_for_completion(&runqueue_node->complete);
1342d7f1642cSJoonyoung Shim 	g2d_free_runqueue_node(g2d, runqueue_node);
1343d7f1642cSJoonyoung Shim 
1344d7f1642cSJoonyoung Shim out:
1345d7f1642cSJoonyoung Shim 	return 0;
1346d7f1642cSJoonyoung Shim }
1347d7f1642cSJoonyoung Shim 
g2d_open(struct drm_device * drm_dev,struct drm_file * file)1348eb4d9796SMarek Szyprowski int g2d_open(struct drm_device *drm_dev, struct drm_file *file)
1349d7f1642cSJoonyoung Shim {
1350d7f1642cSJoonyoung Shim 	struct drm_exynos_file_private *file_priv = file->driver_priv;
1351d7f1642cSJoonyoung Shim 
1352eb4d9796SMarek Szyprowski 	INIT_LIST_HEAD(&file_priv->inuse_cmdlist);
1353eb4d9796SMarek Szyprowski 	INIT_LIST_HEAD(&file_priv->event_list);
1354eb4d9796SMarek Szyprowski 	INIT_LIST_HEAD(&file_priv->userptr_list);
1355d7f1642cSJoonyoung Shim 
1356d7f1642cSJoonyoung Shim 	return 0;
1357d7f1642cSJoonyoung Shim }
1358d7f1642cSJoonyoung Shim 
g2d_close(struct drm_device * drm_dev,struct drm_file * file)1359eb4d9796SMarek Szyprowski void g2d_close(struct drm_device *drm_dev, struct drm_file *file)
1360d7f1642cSJoonyoung Shim {
1361d7f1642cSJoonyoung Shim 	struct drm_exynos_file_private *file_priv = file->driver_priv;
1362eb4d9796SMarek Szyprowski 	struct exynos_drm_private *priv = drm_dev->dev_private;
1363d7f1642cSJoonyoung Shim 	struct g2d_data *g2d;
1364d7f1642cSJoonyoung Shim 	struct g2d_cmdlist_node *node, *n;
1365d7f1642cSJoonyoung Shim 
1366eb4d9796SMarek Szyprowski 	if (!priv->g2d_dev)
1367d7f1642cSJoonyoung Shim 		return;
1368d7f1642cSJoonyoung Shim 
1369eb4d9796SMarek Szyprowski 	g2d = dev_get_drvdata(priv->g2d_dev);
1370d7f1642cSJoonyoung Shim 
137153327374STobias Jakobi 	/* Remove the runqueue nodes that belong to us. */
137253327374STobias Jakobi 	mutex_lock(&g2d->runqueue_mutex);
137353327374STobias Jakobi 	g2d_remove_runqueue_nodes(g2d, file);
137453327374STobias Jakobi 	mutex_unlock(&g2d->runqueue_mutex);
137553327374STobias Jakobi 
137653327374STobias Jakobi 	/*
1377134a0fe9STobias Jakobi 	 * Wait for the runqueue worker to finish its current node.
1378134a0fe9STobias Jakobi 	 * After this the engine should no longer be accessing any
1379134a0fe9STobias Jakobi 	 * memory belonging to us.
1380134a0fe9STobias Jakobi 	 */
1381134a0fe9STobias Jakobi 	g2d_wait_finish(g2d, file);
1382134a0fe9STobias Jakobi 
1383134a0fe9STobias Jakobi 	/*
138453327374STobias Jakobi 	 * Even after the engine is idle, there might still be stale cmdlists
138553327374STobias Jakobi 	 * (i.e. cmdlisst which we submitted but never executed) around, with
138653327374STobias Jakobi 	 * their corresponding GEM/userptr buffers.
138753327374STobias Jakobi 	 * Properly unmap these buffers here.
138853327374STobias Jakobi 	 */
1389d7f1642cSJoonyoung Shim 	mutex_lock(&g2d->cmdlist_mutex);
1390eb4d9796SMarek Szyprowski 	list_for_each_entry_safe(node, n, &file_priv->inuse_cmdlist, list) {
1391d87342c1SInki Dae 		g2d_unmap_cmdlist_gem(g2d, node, file);
1392d7f1642cSJoonyoung Shim 		list_move_tail(&node->list, &g2d->free_cmdlist);
1393d87342c1SInki Dae 	}
1394d7f1642cSJoonyoung Shim 	mutex_unlock(&g2d->cmdlist_mutex);
1395d7f1642cSJoonyoung Shim 
13962a3098ffSInki Dae 	/* release all g2d_userptr in pool. */
1397eb4d9796SMarek Szyprowski 	g2d_userptr_free_all(g2d, file);
1398d7f1642cSJoonyoung Shim }
1399d7f1642cSJoonyoung Shim 
g2d_bind(struct device * dev,struct device * master,void * data)1400eb4d9796SMarek Szyprowski static int g2d_bind(struct device *dev, struct device *master, void *data)
1401eb4d9796SMarek Szyprowski {
1402eb4d9796SMarek Szyprowski 	struct g2d_data *g2d = dev_get_drvdata(dev);
1403eb4d9796SMarek Szyprowski 	struct drm_device *drm_dev = data;
1404eb4d9796SMarek Szyprowski 	struct exynos_drm_private *priv = drm_dev->dev_private;
1405eb4d9796SMarek Szyprowski 	int ret;
1406eb4d9796SMarek Szyprowski 
1407eb4d9796SMarek Szyprowski 	g2d->drm_dev = drm_dev;
1408eb4d9796SMarek Szyprowski 
1409eb4d9796SMarek Szyprowski 	/* allocate dma-aware cmdlist buffer. */
1410eb4d9796SMarek Szyprowski 	ret = g2d_init_cmdlist(g2d);
1411eb4d9796SMarek Szyprowski 	if (ret < 0) {
1412eb4d9796SMarek Szyprowski 		dev_err(dev, "cmdlist init failed\n");
1413eb4d9796SMarek Szyprowski 		return ret;
1414eb4d9796SMarek Szyprowski 	}
1415eb4d9796SMarek Szyprowski 
141607dc3678SMarek Szyprowski 	ret = exynos_drm_register_dma(drm_dev, dev, &g2d->dma_priv);
1417eb4d9796SMarek Szyprowski 	if (ret < 0) {
1418eb4d9796SMarek Szyprowski 		dev_err(dev, "failed to enable iommu.\n");
1419eb4d9796SMarek Szyprowski 		g2d_fini_cmdlist(g2d);
1420eb4d9796SMarek Szyprowski 		return ret;
1421eb4d9796SMarek Szyprowski 	}
1422eb4d9796SMarek Szyprowski 	priv->g2d_dev = dev;
1423eb4d9796SMarek Szyprowski 
1424eb4d9796SMarek Szyprowski 	dev_info(dev, "The Exynos G2D (ver %d.%d) successfully registered.\n",
1425eb4d9796SMarek Szyprowski 			G2D_HW_MAJOR_VER, G2D_HW_MINOR_VER);
1426eb4d9796SMarek Szyprowski 	return 0;
1427eb4d9796SMarek Szyprowski }
1428eb4d9796SMarek Szyprowski 
g2d_unbind(struct device * dev,struct device * master,void * data)1429eb4d9796SMarek Szyprowski static void g2d_unbind(struct device *dev, struct device *master, void *data)
1430eb4d9796SMarek Szyprowski {
1431eb4d9796SMarek Szyprowski 	struct g2d_data *g2d = dev_get_drvdata(dev);
1432eb4d9796SMarek Szyprowski 	struct drm_device *drm_dev = data;
1433eb4d9796SMarek Szyprowski 	struct exynos_drm_private *priv = drm_dev->dev_private;
1434eb4d9796SMarek Szyprowski 
1435eb4d9796SMarek Szyprowski 	/* Suspend operation and wait for engine idle. */
1436eb4d9796SMarek Szyprowski 	set_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
1437eb4d9796SMarek Szyprowski 	g2d_wait_finish(g2d, NULL);
1438eb4d9796SMarek Szyprowski 	priv->g2d_dev = NULL;
1439eb4d9796SMarek Szyprowski 
1440eb4d9796SMarek Szyprowski 	cancel_work_sync(&g2d->runqueue_work);
144107dc3678SMarek Szyprowski 	exynos_drm_unregister_dma(g2d->drm_dev, dev, &g2d->dma_priv);
1442eb4d9796SMarek Szyprowski }
1443eb4d9796SMarek Szyprowski 
1444eb4d9796SMarek Szyprowski static const struct component_ops g2d_component_ops = {
1445eb4d9796SMarek Szyprowski 	.bind	= g2d_bind,
1446eb4d9796SMarek Szyprowski 	.unbind = g2d_unbind,
1447eb4d9796SMarek Szyprowski };
1448eb4d9796SMarek Szyprowski 
g2d_probe(struct platform_device * pdev)144956550d94SGreg Kroah-Hartman static int g2d_probe(struct platform_device *pdev)
1450d7f1642cSJoonyoung Shim {
1451d7f1642cSJoonyoung Shim 	struct device *dev = &pdev->dev;
1452d7f1642cSJoonyoung Shim 	struct g2d_data *g2d;
1453d7f1642cSJoonyoung Shim 	int ret;
1454d7f1642cSJoonyoung Shim 
1455d873ab99SSeung-Woo Kim 	g2d = devm_kzalloc(dev, sizeof(*g2d), GFP_KERNEL);
145638bb5253SSachin Kamat 	if (!g2d)
1457d7f1642cSJoonyoung Shim 		return -ENOMEM;
1458d7f1642cSJoonyoung Shim 
1459d7f1642cSJoonyoung Shim 	g2d->runqueue_slab = kmem_cache_create("g2d_runqueue_slab",
1460d7f1642cSJoonyoung Shim 			sizeof(struct g2d_runqueue_node), 0, 0, NULL);
1461b7675933SSachin Kamat 	if (!g2d->runqueue_slab)
1462b7675933SSachin Kamat 		return -ENOMEM;
1463d7f1642cSJoonyoung Shim 
1464d7f1642cSJoonyoung Shim 	g2d->dev = dev;
1465d7f1642cSJoonyoung Shim 
1466d7f1642cSJoonyoung Shim 	g2d->g2d_workq = create_singlethread_workqueue("g2d");
1467d7f1642cSJoonyoung Shim 	if (!g2d->g2d_workq) {
1468d7f1642cSJoonyoung Shim 		dev_err(dev, "failed to create workqueue\n");
1469d7f1642cSJoonyoung Shim 		ret = -EINVAL;
1470d7f1642cSJoonyoung Shim 		goto err_destroy_slab;
1471d7f1642cSJoonyoung Shim 	}
1472d7f1642cSJoonyoung Shim 
1473d7f1642cSJoonyoung Shim 	INIT_WORK(&g2d->runqueue_work, g2d_runqueue_worker);
1474d7f1642cSJoonyoung Shim 	INIT_LIST_HEAD(&g2d->free_cmdlist);
1475d7f1642cSJoonyoung Shim 	INIT_LIST_HEAD(&g2d->runqueue);
1476d7f1642cSJoonyoung Shim 
1477d7f1642cSJoonyoung Shim 	mutex_init(&g2d->cmdlist_mutex);
1478d7f1642cSJoonyoung Shim 	mutex_init(&g2d->runqueue_mutex);
1479d7f1642cSJoonyoung Shim 
1480dc625537SSachin Kamat 	g2d->gate_clk = devm_clk_get(dev, "fimg2d");
1481d7f1642cSJoonyoung Shim 	if (IS_ERR(g2d->gate_clk)) {
1482d7f1642cSJoonyoung Shim 		dev_err(dev, "failed to get gate clock\n");
1483d7f1642cSJoonyoung Shim 		ret = PTR_ERR(g2d->gate_clk);
1484d87342c1SInki Dae 		goto err_destroy_workqueue;
1485d7f1642cSJoonyoung Shim 	}
1486d7f1642cSJoonyoung Shim 
14877c3fc2b5STobias Jakobi 	pm_runtime_use_autosuspend(dev);
14887c3fc2b5STobias Jakobi 	pm_runtime_set_autosuspend_delay(dev, 2000);
1489d7f1642cSJoonyoung Shim 	pm_runtime_enable(dev);
149022d6704dSTobias Jakobi 	clear_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
149122d6704dSTobias Jakobi 	clear_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
1492d7f1642cSJoonyoung Shim 
149317ac76e0SCai Huoqing 	g2d->regs = devm_platform_ioremap_resource(pdev, 0);
1494d4ed6025SThierry Reding 	if (IS_ERR(g2d->regs)) {
1495d4ed6025SThierry Reding 		ret = PTR_ERR(g2d->regs);
1496b7675933SSachin Kamat 		goto err_put_clk;
1497d7f1642cSJoonyoung Shim 	}
1498d7f1642cSJoonyoung Shim 
1499d7f1642cSJoonyoung Shim 	g2d->irq = platform_get_irq(pdev, 0);
1500d7f1642cSJoonyoung Shim 	if (g2d->irq < 0) {
1501d7f1642cSJoonyoung Shim 		ret = g2d->irq;
1502b7675933SSachin Kamat 		goto err_put_clk;
1503d7f1642cSJoonyoung Shim 	}
1504d7f1642cSJoonyoung Shim 
1505d873ab99SSeung-Woo Kim 	ret = devm_request_irq(dev, g2d->irq, g2d_irq_handler, 0,
1506b7675933SSachin Kamat 								"drm_g2d", g2d);
1507d7f1642cSJoonyoung Shim 	if (ret < 0) {
1508d7f1642cSJoonyoung Shim 		dev_err(dev, "irq request failed\n");
1509b7675933SSachin Kamat 		goto err_put_clk;
1510d7f1642cSJoonyoung Shim 	}
1511d7f1642cSJoonyoung Shim 
15122a3098ffSInki Dae 	g2d->max_pool = MAX_POOL;
15132a3098ffSInki Dae 
1514d7f1642cSJoonyoung Shim 	platform_set_drvdata(pdev, g2d);
1515d7f1642cSJoonyoung Shim 
1516eb4d9796SMarek Szyprowski 	ret = component_add(dev, &g2d_component_ops);
1517d7f1642cSJoonyoung Shim 	if (ret < 0) {
1518d7f1642cSJoonyoung Shim 		dev_err(dev, "failed to register drm g2d device\n");
1519b7675933SSachin Kamat 		goto err_put_clk;
1520d7f1642cSJoonyoung Shim 	}
1521d7f1642cSJoonyoung Shim 
1522d7f1642cSJoonyoung Shim 	return 0;
1523d7f1642cSJoonyoung Shim 
1524d7f1642cSJoonyoung Shim err_put_clk:
1525d7f1642cSJoonyoung Shim 	pm_runtime_disable(dev);
1526d7f1642cSJoonyoung Shim err_destroy_workqueue:
1527d7f1642cSJoonyoung Shim 	destroy_workqueue(g2d->g2d_workq);
1528d7f1642cSJoonyoung Shim err_destroy_slab:
1529d7f1642cSJoonyoung Shim 	kmem_cache_destroy(g2d->runqueue_slab);
1530d7f1642cSJoonyoung Shim 	return ret;
1531d7f1642cSJoonyoung Shim }
1532d7f1642cSJoonyoung Shim 
g2d_remove(struct platform_device * pdev)153356550d94SGreg Kroah-Hartman static int g2d_remove(struct platform_device *pdev)
1534d7f1642cSJoonyoung Shim {
1535d7f1642cSJoonyoung Shim 	struct g2d_data *g2d = platform_get_drvdata(pdev);
1536d7f1642cSJoonyoung Shim 
1537eb4d9796SMarek Szyprowski 	component_del(&pdev->dev, &g2d_component_ops);
1538d7f1642cSJoonyoung Shim 
153953327374STobias Jakobi 	/* There should be no locking needed here. */
154053327374STobias Jakobi 	g2d_remove_runqueue_nodes(g2d, NULL);
1541d7f1642cSJoonyoung Shim 
15427c3fc2b5STobias Jakobi 	pm_runtime_dont_use_autosuspend(&pdev->dev);
1543d7f1642cSJoonyoung Shim 	pm_runtime_disable(&pdev->dev);
1544d7f1642cSJoonyoung Shim 
1545d7f1642cSJoonyoung Shim 	g2d_fini_cmdlist(g2d);
1546d7f1642cSJoonyoung Shim 	destroy_workqueue(g2d->g2d_workq);
1547d7f1642cSJoonyoung Shim 	kmem_cache_destroy(g2d->runqueue_slab);
1548d7f1642cSJoonyoung Shim 
1549d7f1642cSJoonyoung Shim 	return 0;
1550d7f1642cSJoonyoung Shim }
1551d7f1642cSJoonyoung Shim 
g2d_suspend(struct device * dev)155205e2e466STobias Jakobi static int g2d_suspend(struct device *dev)
1553d7f1642cSJoonyoung Shim {
1554d7f1642cSJoonyoung Shim 	struct g2d_data *g2d = dev_get_drvdata(dev);
1555d7f1642cSJoonyoung Shim 
1556134a0fe9STobias Jakobi 	/*
1557134a0fe9STobias Jakobi 	 * Suspend the runqueue worker operation and wait until the G2D
1558134a0fe9STobias Jakobi 	 * engine is idle.
1559134a0fe9STobias Jakobi 	 */
156022d6704dSTobias Jakobi 	set_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
1561134a0fe9STobias Jakobi 	g2d_wait_finish(g2d, NULL);
156243829731STejun Heo 	flush_work(&g2d->runqueue_work);
1563d7f1642cSJoonyoung Shim 
156405e2e466STobias Jakobi 	return 0;
156505e2e466STobias Jakobi }
156605e2e466STobias Jakobi 
g2d_resume(struct device * dev)156705e2e466STobias Jakobi static int g2d_resume(struct device *dev)
156805e2e466STobias Jakobi {
156905e2e466STobias Jakobi 	struct g2d_data *g2d = dev_get_drvdata(dev);
157005e2e466STobias Jakobi 
157122d6704dSTobias Jakobi 	clear_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
157222d6704dSTobias Jakobi 	queue_work(g2d->g2d_workq, &g2d->runqueue_work);
157305e2e466STobias Jakobi 
157405e2e466STobias Jakobi 	return 0;
157505e2e466STobias Jakobi }
157605e2e466STobias Jakobi 
g2d_runtime_suspend(struct device * dev)157705e2e466STobias Jakobi static int g2d_runtime_suspend(struct device *dev)
157805e2e466STobias Jakobi {
157905e2e466STobias Jakobi 	struct g2d_data *g2d = dev_get_drvdata(dev);
158005e2e466STobias Jakobi 
1581b10d6350SInki Dae 	clk_disable_unprepare(g2d->gate_clk);
1582b10d6350SInki Dae 
1583b10d6350SInki Dae 	return 0;
1584b10d6350SInki Dae }
1585b10d6350SInki Dae 
g2d_runtime_resume(struct device * dev)1586b10d6350SInki Dae static int g2d_runtime_resume(struct device *dev)
1587b10d6350SInki Dae {
1588b10d6350SInki Dae 	struct g2d_data *g2d = dev_get_drvdata(dev);
1589b10d6350SInki Dae 	int ret;
1590b10d6350SInki Dae 
1591b10d6350SInki Dae 	ret = clk_prepare_enable(g2d->gate_clk);
1592b10d6350SInki Dae 	if (ret < 0)
1593b10d6350SInki Dae 		dev_warn(dev, "failed to enable clock.\n");
1594b10d6350SInki Dae 
1595b10d6350SInki Dae 	return ret;
1596b10d6350SInki Dae }
1597b10d6350SInki Dae 
1598b10d6350SInki Dae static const struct dev_pm_ops g2d_pm_ops = {
15991d9e6664SPaul Cercueil 	SYSTEM_SLEEP_PM_OPS(g2d_suspend, g2d_resume)
16001d9e6664SPaul Cercueil 	RUNTIME_PM_OPS(g2d_runtime_suspend, g2d_runtime_resume, NULL)
1601b10d6350SInki Dae };
1602d7f1642cSJoonyoung Shim 
160395fc6337SAjay Kumar static const struct of_device_id exynos_g2d_match[] = {
160495fc6337SAjay Kumar 	{ .compatible = "samsung,exynos5250-g2d" },
16056411fe3cSAlban Browaeys 	{ .compatible = "samsung,exynos4212-g2d" },
160695fc6337SAjay Kumar 	{},
160795fc6337SAjay Kumar };
16080262ceebSSjoerd Simons MODULE_DEVICE_TABLE(of, exynos_g2d_match);
160995fc6337SAjay Kumar 
1610d7f1642cSJoonyoung Shim struct platform_driver g2d_driver = {
1611d7f1642cSJoonyoung Shim 	.probe		= g2d_probe,
161256550d94SGreg Kroah-Hartman 	.remove		= g2d_remove,
1613d7f1642cSJoonyoung Shim 	.driver		= {
1614d796ddc9STobias Jakobi 		.name	= "exynos-drm-g2d",
1615d7f1642cSJoonyoung Shim 		.owner	= THIS_MODULE,
16161d9e6664SPaul Cercueil 		.pm	= pm_ptr(&g2d_pm_ops),
161761c48fbfSSachin Kamat 		.of_match_table = exynos_g2d_match,
1618d7f1642cSJoonyoung Shim 	},
1619d7f1642cSJoonyoung Shim };
1620