blob: 1f0bcff697371fbb7d2bbfe607031b22b3b5bc4d [file] [log] [blame]
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001/*
2 * Copyright (C) 2012 Samsung Electronics Co.Ltd
3 * Authors: Joonyoung Shim <jy0922.shim@samsung.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundationr
8 */
9
10#include <linux/kernel.h>
Joonyoung Shimd7f16422012-05-17 20:06:32 +090011#include <linux/clk.h>
12#include <linux/err.h>
13#include <linux/interrupt.h>
14#include <linux/io.h>
15#include <linux/platform_device.h>
16#include <linux/pm_runtime.h>
17#include <linux/slab.h>
18#include <linux/workqueue.h>
Inki Daed87342c2012-11-03 21:53:24 -070019#include <linux/dma-mapping.h>
Ajay Kumar95fc6332013-02-06 10:59:44 +053020#include <linux/of.h>
Joonyoung Shimd7f16422012-05-17 20:06:32 +090021
David Howells760285e2012-10-02 18:01:07 +010022#include <drm/drmP.h>
23#include <drm/exynos_drm.h>
Joonyoung Shimd7f16422012-05-17 20:06:32 +090024#include "exynos_drm_drv.h"
Mark Browne30655d2013-08-13 00:46:40 +010025#include "exynos_drm_g2d.h"
Joonyoung Shimd7f16422012-05-17 20:06:32 +090026#include "exynos_drm_gem.h"
Inki Daed87342c2012-11-03 21:53:24 -070027#include "exynos_drm_iommu.h"
Joonyoung Shimd7f16422012-05-17 20:06:32 +090028
29#define G2D_HW_MAJOR_VER 4
30#define G2D_HW_MINOR_VER 1
31
32/* vaild register range set from user: 0x0104 ~ 0x0880 */
33#define G2D_VALID_START 0x0104
34#define G2D_VALID_END 0x0880
35
36/* general registers */
37#define G2D_SOFT_RESET 0x0000
38#define G2D_INTEN 0x0004
39#define G2D_INTC_PEND 0x000C
40#define G2D_DMA_SFR_BASE_ADDR 0x0080
41#define G2D_DMA_COMMAND 0x0084
42#define G2D_DMA_STATUS 0x008C
43#define G2D_DMA_HOLD_CMD 0x0090
44
45/* command registers */
46#define G2D_BITBLT_START 0x0100
47
48/* registers for base address */
49#define G2D_SRC_BASE_ADDR 0x0304
Tobias Jakobied4dc272016-05-25 14:42:56 +020050#define G2D_SRC_STRIDE 0x0308
YoungJun Cho2dec17c2013-03-11 21:17:52 +090051#define G2D_SRC_COLOR_MODE 0x030C
52#define G2D_SRC_LEFT_TOP 0x0310
53#define G2D_SRC_RIGHT_BOTTOM 0x0314
Joonyoung Shimd7f16422012-05-17 20:06:32 +090054#define G2D_SRC_PLANE2_BASE_ADDR 0x0318
55#define G2D_DST_BASE_ADDR 0x0404
Tobias Jakobied4dc272016-05-25 14:42:56 +020056#define G2D_DST_STRIDE 0x0408
YoungJun Cho2dec17c2013-03-11 21:17:52 +090057#define G2D_DST_COLOR_MODE 0x040C
58#define G2D_DST_LEFT_TOP 0x0410
59#define G2D_DST_RIGHT_BOTTOM 0x0414
Joonyoung Shimd7f16422012-05-17 20:06:32 +090060#define G2D_DST_PLANE2_BASE_ADDR 0x0418
61#define G2D_PAT_BASE_ADDR 0x0500
62#define G2D_MSK_BASE_ADDR 0x0520
63
64/* G2D_SOFT_RESET */
65#define G2D_SFRCLEAR (1 << 1)
66#define G2D_R (1 << 0)
67
68/* G2D_INTEN */
69#define G2D_INTEN_ACF (1 << 3)
70#define G2D_INTEN_UCF (1 << 2)
71#define G2D_INTEN_GCF (1 << 1)
72#define G2D_INTEN_SCF (1 << 0)
73
74/* G2D_INTC_PEND */
75#define G2D_INTP_ACMD_FIN (1 << 3)
76#define G2D_INTP_UCMD_FIN (1 << 2)
77#define G2D_INTP_GCMD_FIN (1 << 1)
78#define G2D_INTP_SCMD_FIN (1 << 0)
79
80/* G2D_DMA_COMMAND */
81#define G2D_DMA_HALT (1 << 2)
82#define G2D_DMA_CONTINUE (1 << 1)
83#define G2D_DMA_START (1 << 0)
84
85/* G2D_DMA_STATUS */
86#define G2D_DMA_LIST_DONE_COUNT (0xFF << 17)
87#define G2D_DMA_BITBLT_DONE_COUNT (0xFFFF << 1)
88#define G2D_DMA_DONE (1 << 0)
89#define G2D_DMA_LIST_DONE_COUNT_OFFSET 17
90
91/* G2D_DMA_HOLD_CMD */
YoungJun Cho7ad01812013-03-13 16:44:37 +090092#define G2D_USER_HOLD (1 << 2)
Joonyoung Shimd7f16422012-05-17 20:06:32 +090093#define G2D_LIST_HOLD (1 << 1)
94#define G2D_BITBLT_HOLD (1 << 0)
95
96/* G2D_BITBLT_START */
97#define G2D_START_CASESEL (1 << 2)
98#define G2D_START_NHOLT (1 << 1)
99#define G2D_START_BITBLT (1 << 0)
100
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900101/* buffer color format */
102#define G2D_FMT_XRGB8888 0
103#define G2D_FMT_ARGB8888 1
104#define G2D_FMT_RGB565 2
105#define G2D_FMT_XRGB1555 3
106#define G2D_FMT_ARGB1555 4
107#define G2D_FMT_XRGB4444 5
108#define G2D_FMT_ARGB4444 6
109#define G2D_FMT_PACKED_RGB888 7
110#define G2D_FMT_A8 11
111#define G2D_FMT_L8 12
112
113/* buffer valid length */
114#define G2D_LEN_MIN 1
115#define G2D_LEN_MAX 8000
116
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900117#define G2D_CMDLIST_SIZE (PAGE_SIZE / 4)
118#define G2D_CMDLIST_NUM 64
119#define G2D_CMDLIST_POOL_SIZE (G2D_CMDLIST_SIZE * G2D_CMDLIST_NUM)
120#define G2D_CMDLIST_DATA_NUM (G2D_CMDLIST_SIZE / sizeof(u32) - 2)
121
Inki Dae2a3098f2012-11-04 05:48:52 -0800122/* maximum buffer pool size of userptr is 64MB as default */
123#define MAX_POOL (64 * 1024 * 1024)
124
125enum {
126 BUF_TYPE_GEM = 1,
127 BUF_TYPE_USERPTR,
128};
129
YoungJun Cho9963cb62013-03-13 17:10:08 +0900130enum g2d_reg_type {
131 REG_TYPE_NONE = -1,
132 REG_TYPE_SRC,
133 REG_TYPE_SRC_PLANE2,
134 REG_TYPE_DST,
135 REG_TYPE_DST_PLANE2,
136 REG_TYPE_PAT,
137 REG_TYPE_MSK,
138 MAX_REG_TYPE_NR
139};
140
Tobias Jakobi22d67042016-09-27 17:50:07 +0200141enum g2d_flag_bits {
142 /*
143 * If set, suspends the runqueue worker after the currently
144 * processed node is finished.
145 */
146 G2D_BIT_SUSPEND_RUNQUEUE,
147 /*
148 * If set, indicates that the engine is currently busy.
149 */
150 G2D_BIT_ENGINE_BUSY,
151};
152
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900153/* cmdlist data structure */
154struct g2d_cmdlist {
Inki Dae2a3098f2012-11-04 05:48:52 -0800155 u32 head;
156 unsigned long data[G2D_CMDLIST_DATA_NUM];
157 u32 last; /* last data offset */
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900158};
159
YoungJun Cho9963cb62013-03-13 17:10:08 +0900160/*
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900161 * A structure of buffer description
162 *
163 * @format: color format
Tobias Jakobi179239a2015-08-18 00:51:24 +0200164 * @stride: buffer stride/pitch in bytes
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900165 * @left_x: the x coordinates of left top corner
166 * @top_y: the y coordinates of left top corner
167 * @right_x: the x coordinates of right bottom corner
168 * @bottom_y: the y coordinates of right bottom corner
169 *
170 */
171struct g2d_buf_desc {
172 unsigned int format;
Tobias Jakobi179239a2015-08-18 00:51:24 +0200173 unsigned int stride;
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900174 unsigned int left_x;
175 unsigned int top_y;
176 unsigned int right_x;
177 unsigned int bottom_y;
178};
179
180/*
YoungJun Cho9963cb62013-03-13 17:10:08 +0900181 * A structure of buffer information
182 *
183 * @map_nr: manages the number of mapped buffers
184 * @reg_types: stores regitster type in the order of requested command
185 * @handles: stores buffer handle in its reg_type position
186 * @types: stores buffer type in its reg_type position
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900187 * @descs: stores buffer description in its reg_type position
YoungJun Cho9963cb62013-03-13 17:10:08 +0900188 *
189 */
190struct g2d_buf_info {
191 unsigned int map_nr;
192 enum g2d_reg_type reg_types[MAX_REG_TYPE_NR];
193 unsigned long handles[MAX_REG_TYPE_NR];
194 unsigned int types[MAX_REG_TYPE_NR];
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900195 struct g2d_buf_desc descs[MAX_REG_TYPE_NR];
YoungJun Cho9963cb62013-03-13 17:10:08 +0900196};
197
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900198struct drm_exynos_pending_g2d_event {
199 struct drm_pending_event base;
200 struct drm_exynos_g2d_event event;
201};
202
Inki Dae2a3098f2012-11-04 05:48:52 -0800203struct g2d_cmdlist_userptr {
204 struct list_head list;
205 dma_addr_t dma_addr;
206 unsigned long userptr;
207 unsigned long size;
Jan Kara63540f02015-07-20 05:03:35 -0300208 struct frame_vector *vec;
Inki Dae2a3098f2012-11-04 05:48:52 -0800209 struct sg_table *sgt;
Inki Dae2a3098f2012-11-04 05:48:52 -0800210 atomic_t refcount;
211 bool in_pool;
212 bool out_of_list;
213};
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900214struct g2d_cmdlist_node {
215 struct list_head list;
216 struct g2d_cmdlist *cmdlist;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900217 dma_addr_t dma_addr;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900218 struct g2d_buf_info buf_info;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900219
220 struct drm_exynos_pending_g2d_event *event;
221};
222
223struct g2d_runqueue_node {
224 struct list_head list;
225 struct list_head run_cmdlist;
226 struct list_head event_list;
Inki Daed87342c2012-11-03 21:53:24 -0700227 struct drm_file *filp;
Inki Dae6b6bae22012-09-11 10:45:36 +0900228 pid_t pid;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900229 struct completion complete;
230 int async;
231};
232
233struct g2d_data {
234 struct device *dev;
235 struct clk *gate_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900236 void __iomem *regs;
237 int irq;
238 struct workqueue_struct *g2d_workq;
239 struct work_struct runqueue_work;
240 struct exynos_drm_subdrv subdrv;
Tobias Jakobi22d67042016-09-27 17:50:07 +0200241 unsigned long flags;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900242
243 /* cmdlist */
244 struct g2d_cmdlist_node *cmdlist_node;
245 struct list_head free_cmdlist;
246 struct mutex cmdlist_mutex;
247 dma_addr_t cmdlist_pool;
248 void *cmdlist_pool_virt;
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700249 unsigned long cmdlist_dma_attrs;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900250
251 /* runqueue*/
252 struct g2d_runqueue_node *runqueue_node;
253 struct list_head runqueue;
254 struct mutex runqueue_mutex;
255 struct kmem_cache *runqueue_slab;
Inki Dae2a3098f2012-11-04 05:48:52 -0800256
257 unsigned long current_pool;
258 unsigned long max_pool;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900259};
260
Tobias Jakobi134a0fe2016-09-27 17:50:09 +0200261static inline void g2d_hw_reset(struct g2d_data *g2d)
262{
263 writel(G2D_R | G2D_SFRCLEAR, g2d->regs + G2D_SOFT_RESET);
264 clear_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
265}
266
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900267static int g2d_init_cmdlist(struct g2d_data *g2d)
268{
269 struct device *dev = g2d->dev;
270 struct g2d_cmdlist_node *node = g2d->cmdlist_node;
Inki Daed87342c2012-11-03 21:53:24 -0700271 struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900272 int nr;
273 int ret;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900274 struct g2d_buf_info *buf_info;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900275
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700276 g2d->cmdlist_dma_attrs = DMA_ATTR_WRITE_COMBINE;
Inki Daed87342c2012-11-03 21:53:24 -0700277
Marek Szyprowskif43c3592016-02-29 17:50:53 +0900278 g2d->cmdlist_pool_virt = dma_alloc_attrs(to_dma_dev(subdrv->drm_dev),
Inki Daed87342c2012-11-03 21:53:24 -0700279 G2D_CMDLIST_POOL_SIZE,
280 &g2d->cmdlist_pool, GFP_KERNEL,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700281 g2d->cmdlist_dma_attrs);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900282 if (!g2d->cmdlist_pool_virt) {
283 dev_err(dev, "failed to allocate dma memory\n");
284 return -ENOMEM;
285 }
286
Joonyoung Shimfab9f8d2012-09-27 19:26:03 +0900287 node = kcalloc(G2D_CMDLIST_NUM, sizeof(*node), GFP_KERNEL);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900288 if (!node) {
289 dev_err(dev, "failed to allocate memory\n");
290 ret = -ENOMEM;
291 goto err;
292 }
293
294 for (nr = 0; nr < G2D_CMDLIST_NUM; nr++) {
YoungJun Cho9963cb62013-03-13 17:10:08 +0900295 unsigned int i;
296
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900297 node[nr].cmdlist =
298 g2d->cmdlist_pool_virt + nr * G2D_CMDLIST_SIZE;
299 node[nr].dma_addr =
300 g2d->cmdlist_pool + nr * G2D_CMDLIST_SIZE;
301
YoungJun Cho9963cb62013-03-13 17:10:08 +0900302 buf_info = &node[nr].buf_info;
303 for (i = 0; i < MAX_REG_TYPE_NR; i++)
304 buf_info->reg_types[i] = REG_TYPE_NONE;
305
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900306 list_add_tail(&node[nr].list, &g2d->free_cmdlist);
307 }
308
309 return 0;
310
311err:
Marek Szyprowskif43c3592016-02-29 17:50:53 +0900312 dma_free_attrs(to_dma_dev(subdrv->drm_dev), G2D_CMDLIST_POOL_SIZE,
Inki Daed87342c2012-11-03 21:53:24 -0700313 g2d->cmdlist_pool_virt,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700314 g2d->cmdlist_pool, g2d->cmdlist_dma_attrs);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900315 return ret;
316}
317
318static void g2d_fini_cmdlist(struct g2d_data *g2d)
319{
Inki Daed87342c2012-11-03 21:53:24 -0700320 struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900321
322 kfree(g2d->cmdlist_node);
Inki Dae9ad703e2014-11-07 20:31:08 +0900323
324 if (g2d->cmdlist_pool_virt && g2d->cmdlist_pool) {
Marek Szyprowskif43c3592016-02-29 17:50:53 +0900325 dma_free_attrs(to_dma_dev(subdrv->drm_dev),
326 G2D_CMDLIST_POOL_SIZE,
Inki Dae9ad703e2014-11-07 20:31:08 +0900327 g2d->cmdlist_pool_virt,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700328 g2d->cmdlist_pool, g2d->cmdlist_dma_attrs);
Inki Dae9ad703e2014-11-07 20:31:08 +0900329 }
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900330}
331
332static struct g2d_cmdlist_node *g2d_get_cmdlist(struct g2d_data *g2d)
333{
334 struct device *dev = g2d->dev;
335 struct g2d_cmdlist_node *node;
336
337 mutex_lock(&g2d->cmdlist_mutex);
338 if (list_empty(&g2d->free_cmdlist)) {
339 dev_err(dev, "there is no free cmdlist\n");
340 mutex_unlock(&g2d->cmdlist_mutex);
341 return NULL;
342 }
343
344 node = list_first_entry(&g2d->free_cmdlist, struct g2d_cmdlist_node,
345 list);
346 list_del_init(&node->list);
347 mutex_unlock(&g2d->cmdlist_mutex);
348
349 return node;
350}
351
352static void g2d_put_cmdlist(struct g2d_data *g2d, struct g2d_cmdlist_node *node)
353{
354 mutex_lock(&g2d->cmdlist_mutex);
355 list_move_tail(&node->list, &g2d->free_cmdlist);
356 mutex_unlock(&g2d->cmdlist_mutex);
357}
358
359static void g2d_add_cmdlist_to_inuse(struct exynos_drm_g2d_private *g2d_priv,
360 struct g2d_cmdlist_node *node)
361{
362 struct g2d_cmdlist_node *lnode;
363
364 if (list_empty(&g2d_priv->inuse_cmdlist))
365 goto add_to_list;
366
367 /* this links to base address of new cmdlist */
368 lnode = list_entry(g2d_priv->inuse_cmdlist.prev,
369 struct g2d_cmdlist_node, list);
370 lnode->cmdlist->data[lnode->cmdlist->last] = node->dma_addr;
371
372add_to_list:
373 list_add_tail(&node->list, &g2d_priv->inuse_cmdlist);
374
375 if (node->event)
376 list_add_tail(&node->event->base.link, &g2d_priv->event_list);
377}
378
Inki Dae2a3098f2012-11-04 05:48:52 -0800379static void g2d_userptr_put_dma_addr(struct drm_device *drm_dev,
380 unsigned long obj,
381 bool force)
382{
383 struct g2d_cmdlist_userptr *g2d_userptr =
384 (struct g2d_cmdlist_userptr *)obj;
Jan Kara63540f02015-07-20 05:03:35 -0300385 struct page **pages;
Inki Dae2a3098f2012-11-04 05:48:52 -0800386
387 if (!obj)
388 return;
389
390 if (force)
391 goto out;
392
393 atomic_dec(&g2d_userptr->refcount);
394
395 if (atomic_read(&g2d_userptr->refcount) > 0)
396 return;
397
398 if (g2d_userptr->in_pool)
399 return;
400
401out:
Joonyoung Shim6aa5e852016-04-22 16:34:07 +0900402 dma_unmap_sg(to_dma_dev(drm_dev), g2d_userptr->sgt->sgl,
403 g2d_userptr->sgt->nents, DMA_BIDIRECTIONAL);
Inki Dae2a3098f2012-11-04 05:48:52 -0800404
Jan Kara63540f02015-07-20 05:03:35 -0300405 pages = frame_vector_pages(g2d_userptr->vec);
406 if (!IS_ERR(pages)) {
407 int i;
Inki Dae2a3098f2012-11-04 05:48:52 -0800408
Jan Kara63540f02015-07-20 05:03:35 -0300409 for (i = 0; i < frame_vector_count(g2d_userptr->vec); i++)
410 set_page_dirty_lock(pages[i]);
411 }
412 put_vaddr_frames(g2d_userptr->vec);
413 frame_vector_destroy(g2d_userptr->vec);
Inki Daec3bddbd2013-11-21 12:09:51 +0900414
Inki Dae2a3098f2012-11-04 05:48:52 -0800415 if (!g2d_userptr->out_of_list)
416 list_del_init(&g2d_userptr->list);
417
418 sg_free_table(g2d_userptr->sgt);
419 kfree(g2d_userptr->sgt);
Sachin Kamatdf3d90e2012-11-23 09:11:59 +0530420 kfree(g2d_userptr);
Inki Dae2a3098f2012-11-04 05:48:52 -0800421}
422
Sachin Kamatb7848c72013-01-14 12:29:09 +0530423static dma_addr_t *g2d_userptr_get_dma_addr(struct drm_device *drm_dev,
Inki Dae2a3098f2012-11-04 05:48:52 -0800424 unsigned long userptr,
425 unsigned long size,
426 struct drm_file *filp,
427 unsigned long *obj)
428{
429 struct drm_exynos_file_private *file_priv = filp->driver_priv;
430 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
431 struct g2d_cmdlist_userptr *g2d_userptr;
432 struct g2d_data *g2d;
Inki Dae2a3098f2012-11-04 05:48:52 -0800433 struct sg_table *sgt;
Inki Dae2a3098f2012-11-04 05:48:52 -0800434 unsigned long start, end;
435 unsigned int npages, offset;
436 int ret;
437
438 if (!size) {
439 DRM_ERROR("invalid userptr size.\n");
440 return ERR_PTR(-EINVAL);
441 }
442
443 g2d = dev_get_drvdata(g2d_priv->dev);
444
445 /* check if userptr already exists in userptr_list. */
446 list_for_each_entry(g2d_userptr, &g2d_priv->userptr_list, list) {
447 if (g2d_userptr->userptr == userptr) {
448 /*
449 * also check size because there could be same address
450 * and different size.
451 */
452 if (g2d_userptr->size == size) {
453 atomic_inc(&g2d_userptr->refcount);
454 *obj = (unsigned long)g2d_userptr;
455
456 return &g2d_userptr->dma_addr;
457 }
458
459 /*
460 * at this moment, maybe g2d dma is accessing this
461 * g2d_userptr memory region so just remove this
462 * g2d_userptr object from userptr_list not to be
463 * referred again and also except it the userptr
464 * pool to be released after the dma access completion.
465 */
466 g2d_userptr->out_of_list = true;
467 g2d_userptr->in_pool = false;
468 list_del_init(&g2d_userptr->list);
469
470 break;
471 }
472 }
473
474 g2d_userptr = kzalloc(sizeof(*g2d_userptr), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900475 if (!g2d_userptr)
Inki Dae2a3098f2012-11-04 05:48:52 -0800476 return ERR_PTR(-ENOMEM);
Inki Dae2a3098f2012-11-04 05:48:52 -0800477
478 atomic_set(&g2d_userptr->refcount, 1);
Jan Kara63540f02015-07-20 05:03:35 -0300479 g2d_userptr->size = size;
Inki Dae2a3098f2012-11-04 05:48:52 -0800480
481 start = userptr & PAGE_MASK;
482 offset = userptr & ~PAGE_MASK;
483 end = PAGE_ALIGN(userptr + size);
484 npages = (end - start) >> PAGE_SHIFT;
Jan Kara63540f02015-07-20 05:03:35 -0300485 g2d_userptr->vec = frame_vector_create(npages);
486 if (!g2d_userptr->vec) {
Seung-Woo Kim4bb615c2013-07-03 17:09:21 +0900487 ret = -ENOMEM;
488 goto err_free;
Inki Dae2a3098f2012-11-04 05:48:52 -0800489 }
490
Jan Kara63540f02015-07-20 05:03:35 -0300491 ret = get_vaddr_frames(start, npages, true, true, g2d_userptr->vec);
492 if (ret != npages) {
Inki Dae2a3098f2012-11-04 05:48:52 -0800493 DRM_ERROR("failed to get user pages from userptr.\n");
Jan Kara63540f02015-07-20 05:03:35 -0300494 if (ret < 0)
495 goto err_destroy_framevec;
496 ret = -EFAULT;
497 goto err_put_framevec;
Inki Dae2a3098f2012-11-04 05:48:52 -0800498 }
Jan Kara63540f02015-07-20 05:03:35 -0300499 if (frame_vector_to_pages(g2d_userptr->vec) < 0) {
500 ret = -EFAULT;
501 goto err_put_framevec;
502 }
Inki Dae2a3098f2012-11-04 05:48:52 -0800503
Sachin Kamate44a5c02013-01-25 14:45:42 +0530504 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
Inki Dae2a3098f2012-11-04 05:48:52 -0800505 if (!sgt) {
Inki Dae2a3098f2012-11-04 05:48:52 -0800506 ret = -ENOMEM;
Jan Kara63540f02015-07-20 05:03:35 -0300507 goto err_put_framevec;
Inki Dae2a3098f2012-11-04 05:48:52 -0800508 }
509
Jan Kara63540f02015-07-20 05:03:35 -0300510 ret = sg_alloc_table_from_pages(sgt,
511 frame_vector_pages(g2d_userptr->vec),
512 npages, offset, size, GFP_KERNEL);
Inki Dae2a3098f2012-11-04 05:48:52 -0800513 if (ret < 0) {
514 DRM_ERROR("failed to get sgt from pages.\n");
515 goto err_free_sgt;
516 }
517
518 g2d_userptr->sgt = sgt;
519
Joonyoung Shim6aa5e852016-04-22 16:34:07 +0900520 if (!dma_map_sg(to_dma_dev(drm_dev), sgt->sgl, sgt->nents,
521 DMA_BIDIRECTIONAL)) {
Inki Dae2a3098f2012-11-04 05:48:52 -0800522 DRM_ERROR("failed to map sgt with dma region.\n");
Joonyoung Shim6aa5e852016-04-22 16:34:07 +0900523 ret = -ENOMEM;
YoungJun Cho067ed332013-03-11 19:48:05 +0900524 goto err_sg_free_table;
Inki Dae2a3098f2012-11-04 05:48:52 -0800525 }
526
527 g2d_userptr->dma_addr = sgt->sgl[0].dma_address;
528 g2d_userptr->userptr = userptr;
529
530 list_add_tail(&g2d_userptr->list, &g2d_priv->userptr_list);
531
532 if (g2d->current_pool + (npages << PAGE_SHIFT) < g2d->max_pool) {
533 g2d->current_pool += npages << PAGE_SHIFT;
534 g2d_userptr->in_pool = true;
535 }
536
537 *obj = (unsigned long)g2d_userptr;
538
539 return &g2d_userptr->dma_addr;
540
YoungJun Cho067ed332013-03-11 19:48:05 +0900541err_sg_free_table:
Inki Dae2a3098f2012-11-04 05:48:52 -0800542 sg_free_table(sgt);
YoungJun Cho067ed332013-03-11 19:48:05 +0900543
544err_free_sgt:
Inki Dae2a3098f2012-11-04 05:48:52 -0800545 kfree(sgt);
Inki Dae2a3098f2012-11-04 05:48:52 -0800546
Jan Kara63540f02015-07-20 05:03:35 -0300547err_put_framevec:
548 put_vaddr_frames(g2d_userptr->vec);
Inki Dae2a3098f2012-11-04 05:48:52 -0800549
Jan Kara63540f02015-07-20 05:03:35 -0300550err_destroy_framevec:
551 frame_vector_destroy(g2d_userptr->vec);
Seung-Woo Kim4bb615c2013-07-03 17:09:21 +0900552
553err_free:
Inki Dae2a3098f2012-11-04 05:48:52 -0800554 kfree(g2d_userptr);
Inki Dae2a3098f2012-11-04 05:48:52 -0800555
556 return ERR_PTR(ret);
557}
558
559static void g2d_userptr_free_all(struct drm_device *drm_dev,
560 struct g2d_data *g2d,
561 struct drm_file *filp)
562{
563 struct drm_exynos_file_private *file_priv = filp->driver_priv;
564 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
565 struct g2d_cmdlist_userptr *g2d_userptr, *n;
566
567 list_for_each_entry_safe(g2d_userptr, n, &g2d_priv->userptr_list, list)
568 if (g2d_userptr->in_pool)
569 g2d_userptr_put_dma_addr(drm_dev,
570 (unsigned long)g2d_userptr,
571 true);
572
573 g2d->current_pool = 0;
574}
575
YoungJun Cho9963cb62013-03-13 17:10:08 +0900576static enum g2d_reg_type g2d_get_reg_type(int reg_offset)
577{
578 enum g2d_reg_type reg_type;
579
580 switch (reg_offset) {
581 case G2D_SRC_BASE_ADDR:
Tobias Jakobied4dc272016-05-25 14:42:56 +0200582 case G2D_SRC_STRIDE:
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900583 case G2D_SRC_COLOR_MODE:
584 case G2D_SRC_LEFT_TOP:
585 case G2D_SRC_RIGHT_BOTTOM:
YoungJun Cho9963cb62013-03-13 17:10:08 +0900586 reg_type = REG_TYPE_SRC;
587 break;
588 case G2D_SRC_PLANE2_BASE_ADDR:
589 reg_type = REG_TYPE_SRC_PLANE2;
590 break;
591 case G2D_DST_BASE_ADDR:
Tobias Jakobied4dc272016-05-25 14:42:56 +0200592 case G2D_DST_STRIDE:
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900593 case G2D_DST_COLOR_MODE:
594 case G2D_DST_LEFT_TOP:
595 case G2D_DST_RIGHT_BOTTOM:
YoungJun Cho9963cb62013-03-13 17:10:08 +0900596 reg_type = REG_TYPE_DST;
597 break;
598 case G2D_DST_PLANE2_BASE_ADDR:
599 reg_type = REG_TYPE_DST_PLANE2;
600 break;
601 case G2D_PAT_BASE_ADDR:
602 reg_type = REG_TYPE_PAT;
603 break;
604 case G2D_MSK_BASE_ADDR:
605 reg_type = REG_TYPE_MSK;
606 break;
607 default:
608 reg_type = REG_TYPE_NONE;
609 DRM_ERROR("Unknown register offset![%d]\n", reg_offset);
610 break;
Sachin Kamat5cdbc8d2014-01-16 10:00:22 +0530611 }
YoungJun Cho9963cb62013-03-13 17:10:08 +0900612
613 return reg_type;
614}
615
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900616static unsigned long g2d_get_buf_bpp(unsigned int format)
617{
618 unsigned long bpp;
619
620 switch (format) {
621 case G2D_FMT_XRGB8888:
622 case G2D_FMT_ARGB8888:
623 bpp = 4;
624 break;
625 case G2D_FMT_RGB565:
626 case G2D_FMT_XRGB1555:
627 case G2D_FMT_ARGB1555:
628 case G2D_FMT_XRGB4444:
629 case G2D_FMT_ARGB4444:
630 bpp = 2;
631 break;
632 case G2D_FMT_PACKED_RGB888:
633 bpp = 3;
634 break;
635 default:
636 bpp = 1;
637 break;
638 }
639
640 return bpp;
641}
642
643static bool g2d_check_buf_desc_is_valid(struct g2d_buf_desc *buf_desc,
644 enum g2d_reg_type reg_type,
645 unsigned long size)
646{
Tobias Jakobi179239a2015-08-18 00:51:24 +0200647 int width, height;
648 unsigned long bpp, last_pos;
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900649
650 /*
651 * check source and destination buffers only.
652 * so the others are always valid.
653 */
654 if (reg_type != REG_TYPE_SRC && reg_type != REG_TYPE_DST)
655 return true;
656
Tobias Jakobi179239a2015-08-18 00:51:24 +0200657 /* This check also makes sure that right_x > left_x. */
658 width = (int)buf_desc->right_x - (int)buf_desc->left_x;
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900659 if (width < G2D_LEN_MIN || width > G2D_LEN_MAX) {
Tobias Jakobi179239a2015-08-18 00:51:24 +0200660 DRM_ERROR("width[%d] is out of range!\n", width);
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900661 return false;
662 }
663
Tobias Jakobi179239a2015-08-18 00:51:24 +0200664 /* This check also makes sure that bottom_y > top_y. */
665 height = (int)buf_desc->bottom_y - (int)buf_desc->top_y;
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900666 if (height < G2D_LEN_MIN || height > G2D_LEN_MAX) {
Tobias Jakobi179239a2015-08-18 00:51:24 +0200667 DRM_ERROR("height[%d] is out of range!\n", height);
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900668 return false;
669 }
670
Tobias Jakobi179239a2015-08-18 00:51:24 +0200671 bpp = g2d_get_buf_bpp(buf_desc->format);
672
673 /* Compute the position of the last byte that the engine accesses. */
674 last_pos = ((unsigned long)buf_desc->bottom_y - 1) *
675 (unsigned long)buf_desc->stride +
676 (unsigned long)buf_desc->right_x * bpp - 1;
677
678 /*
679 * Since right_x > left_x and bottom_y > top_y we already know
680 * that the first_pos < last_pos (first_pos being the position
681 * of the first byte the engine accesses), it just remains to
682 * check if last_pos is smaller then the buffer size.
683 */
684
685 if (last_pos >= size) {
686 DRM_ERROR("last engine access position [%lu] "
687 "is out of range [%lu]!\n", last_pos, size);
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900688 return false;
689 }
690
691 return true;
692}
693
Inki Daed87342c2012-11-03 21:53:24 -0700694static int g2d_map_cmdlist_gem(struct g2d_data *g2d,
695 struct g2d_cmdlist_node *node,
696 struct drm_device *drm_dev,
697 struct drm_file *file)
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900698{
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900699 struct g2d_cmdlist *cmdlist = node->cmdlist;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900700 struct g2d_buf_info *buf_info = &node->buf_info;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900701 int offset;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900702 int ret;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900703 int i;
704
YoungJun Cho9963cb62013-03-13 17:10:08 +0900705 for (i = 0; i < buf_info->map_nr; i++) {
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900706 struct g2d_buf_desc *buf_desc;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900707 enum g2d_reg_type reg_type;
708 int reg_pos;
Inki Daed87342c2012-11-03 21:53:24 -0700709 unsigned long handle;
710 dma_addr_t *addr;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900711
YoungJun Cho9963cb62013-03-13 17:10:08 +0900712 reg_pos = cmdlist->last - 2 * (i + 1);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900713
YoungJun Cho9963cb62013-03-13 17:10:08 +0900714 offset = cmdlist->data[reg_pos];
715 handle = cmdlist->data[reg_pos + 1];
716
717 reg_type = g2d_get_reg_type(offset);
718 if (reg_type == REG_TYPE_NONE) {
719 ret = -EFAULT;
720 goto err;
721 }
722
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900723 buf_desc = &buf_info->descs[reg_type];
724
YoungJun Cho9963cb62013-03-13 17:10:08 +0900725 if (buf_info->types[reg_type] == BUF_TYPE_GEM) {
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900726 unsigned long size;
727
728 size = exynos_drm_gem_get_size(drm_dev, handle, file);
729 if (!size) {
730 ret = -EFAULT;
731 goto err;
732 }
733
734 if (!g2d_check_buf_desc_is_valid(buf_desc, reg_type,
735 size)) {
736 ret = -EFAULT;
737 goto err;
738 }
739
Inki Dae2a3098f2012-11-04 05:48:52 -0800740 addr = exynos_drm_gem_get_dma_addr(drm_dev, handle,
741 file);
742 if (IS_ERR(addr)) {
YoungJun Cho9963cb62013-03-13 17:10:08 +0900743 ret = -EFAULT;
744 goto err;
Inki Dae2a3098f2012-11-04 05:48:52 -0800745 }
746 } else {
747 struct drm_exynos_g2d_userptr g2d_userptr;
748
749 if (copy_from_user(&g2d_userptr, (void __user *)handle,
750 sizeof(struct drm_exynos_g2d_userptr))) {
YoungJun Cho9963cb62013-03-13 17:10:08 +0900751 ret = -EFAULT;
752 goto err;
Inki Dae2a3098f2012-11-04 05:48:52 -0800753 }
754
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900755 if (!g2d_check_buf_desc_is_valid(buf_desc, reg_type,
756 g2d_userptr.size)) {
757 ret = -EFAULT;
758 goto err;
759 }
760
Inki Dae2a3098f2012-11-04 05:48:52 -0800761 addr = g2d_userptr_get_dma_addr(drm_dev,
762 g2d_userptr.userptr,
763 g2d_userptr.size,
764 file,
765 &handle);
766 if (IS_ERR(addr)) {
YoungJun Cho9963cb62013-03-13 17:10:08 +0900767 ret = -EFAULT;
768 goto err;
Inki Dae2a3098f2012-11-04 05:48:52 -0800769 }
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900770 }
771
YoungJun Cho9963cb62013-03-13 17:10:08 +0900772 cmdlist->data[reg_pos + 1] = *addr;
773 buf_info->reg_types[i] = reg_type;
774 buf_info->handles[reg_type] = handle;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900775 }
776
777 return 0;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900778
779err:
780 buf_info->map_nr = i;
781 return ret;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900782}
783
Inki Daed87342c2012-11-03 21:53:24 -0700784static void g2d_unmap_cmdlist_gem(struct g2d_data *g2d,
785 struct g2d_cmdlist_node *node,
786 struct drm_file *filp)
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900787{
Inki Daed87342c2012-11-03 21:53:24 -0700788 struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900789 struct g2d_buf_info *buf_info = &node->buf_info;
Inki Daed87342c2012-11-03 21:53:24 -0700790 int i;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900791
YoungJun Cho9963cb62013-03-13 17:10:08 +0900792 for (i = 0; i < buf_info->map_nr; i++) {
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900793 struct g2d_buf_desc *buf_desc;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900794 enum g2d_reg_type reg_type;
795 unsigned long handle;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900796
YoungJun Cho9963cb62013-03-13 17:10:08 +0900797 reg_type = buf_info->reg_types[i];
798
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900799 buf_desc = &buf_info->descs[reg_type];
YoungJun Cho9963cb62013-03-13 17:10:08 +0900800 handle = buf_info->handles[reg_type];
801
802 if (buf_info->types[reg_type] == BUF_TYPE_GEM)
Inki Dae2a3098f2012-11-04 05:48:52 -0800803 exynos_drm_gem_put_dma_addr(subdrv->drm_dev, handle,
804 filp);
805 else
806 g2d_userptr_put_dma_addr(subdrv->drm_dev, handle,
807 false);
Inki Daed87342c2012-11-03 21:53:24 -0700808
YoungJun Cho9963cb62013-03-13 17:10:08 +0900809 buf_info->reg_types[i] = REG_TYPE_NONE;
810 buf_info->handles[reg_type] = 0;
811 buf_info->types[reg_type] = 0;
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900812 memset(buf_desc, 0x00, sizeof(*buf_desc));
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900813 }
Inki Daed87342c2012-11-03 21:53:24 -0700814
YoungJun Cho9963cb62013-03-13 17:10:08 +0900815 buf_info->map_nr = 0;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900816}
817
818static void g2d_dma_start(struct g2d_data *g2d,
819 struct g2d_runqueue_node *runqueue_node)
820{
821 struct g2d_cmdlist_node *node =
822 list_first_entry(&runqueue_node->run_cmdlist,
823 struct g2d_cmdlist_node, list);
824
Tobias Jakobi22d67042016-09-27 17:50:07 +0200825 set_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900826 writel_relaxed(node->dma_addr, g2d->regs + G2D_DMA_SFR_BASE_ADDR);
827 writel_relaxed(G2D_DMA_START, g2d->regs + G2D_DMA_COMMAND);
828}
829
830static struct g2d_runqueue_node *g2d_get_runqueue_node(struct g2d_data *g2d)
831{
832 struct g2d_runqueue_node *runqueue_node;
833
834 if (list_empty(&g2d->runqueue))
835 return NULL;
836
837 runqueue_node = list_first_entry(&g2d->runqueue,
838 struct g2d_runqueue_node, list);
839 list_del_init(&runqueue_node->list);
840 return runqueue_node;
841}
842
843static void g2d_free_runqueue_node(struct g2d_data *g2d,
844 struct g2d_runqueue_node *runqueue_node)
845{
Inki Daed87342c2012-11-03 21:53:24 -0700846 struct g2d_cmdlist_node *node;
847
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900848 if (!runqueue_node)
849 return;
850
851 mutex_lock(&g2d->cmdlist_mutex);
Inki Daed87342c2012-11-03 21:53:24 -0700852 /*
853 * commands in run_cmdlist have been completed so unmap all gem
854 * objects in each command node so that they are unreferenced.
855 */
856 list_for_each_entry(node, &runqueue_node->run_cmdlist, list)
857 g2d_unmap_cmdlist_gem(g2d, node, runqueue_node->filp);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900858 list_splice_tail_init(&runqueue_node->run_cmdlist, &g2d->free_cmdlist);
859 mutex_unlock(&g2d->cmdlist_mutex);
860
861 kmem_cache_free(g2d->runqueue_slab, runqueue_node);
862}
863
Tobias Jakobi53327372016-09-27 17:50:08 +0200864/**
865 * g2d_remove_runqueue_nodes - remove items from the list of runqueue nodes
866 * @g2d: G2D state object
867 * @file: if not zero, only remove items with this DRM file
868 *
869 * Has to be called under runqueue lock.
870 */
871static void g2d_remove_runqueue_nodes(struct g2d_data *g2d, struct drm_file* file)
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900872{
Tobias Jakobi53327372016-09-27 17:50:08 +0200873 struct g2d_runqueue_node *node, *n;
874
875 if (list_empty(&g2d->runqueue))
876 return;
877
878 list_for_each_entry_safe(node, n, &g2d->runqueue, list) {
879 if (file && node->filp != file)
880 continue;
881
882 list_del_init(&node->list);
883 g2d_free_runqueue_node(g2d, node);
884 }
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900885}
886
887static void g2d_runqueue_worker(struct work_struct *work)
888{
889 struct g2d_data *g2d = container_of(work, struct g2d_data,
890 runqueue_work);
Tobias Jakobi22d67042016-09-27 17:50:07 +0200891 struct g2d_runqueue_node *runqueue_node;
892
893 /*
894 * The engine is busy and the completion of the current node is going
895 * to poke the runqueue worker, so nothing to do here.
896 */
897 if (test_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags))
898 return;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900899
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900900 mutex_lock(&g2d->runqueue_mutex);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900901
Tobias Jakobi22d67042016-09-27 17:50:07 +0200902 runqueue_node = g2d->runqueue_node;
903 g2d->runqueue_node = NULL;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900904
Tobias Jakobi22d67042016-09-27 17:50:07 +0200905 if (runqueue_node) {
Tobias Jakobi7c3fc2b2016-09-27 17:59:56 +0200906 pm_runtime_mark_last_busy(g2d->dev);
907 pm_runtime_put_autosuspend(g2d->dev);
Tobias Jakobi22d67042016-09-27 17:50:07 +0200908
909 complete(&runqueue_node->complete);
910 if (runqueue_node->async)
911 g2d_free_runqueue_node(g2d, runqueue_node);
912 }
913
914 if (!test_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags)) {
915 g2d->runqueue_node = g2d_get_runqueue_node(g2d);
916
917 if (g2d->runqueue_node) {
918 pm_runtime_get_sync(g2d->dev);
919 g2d_dma_start(g2d, g2d->runqueue_node);
920 }
921 }
922
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900923 mutex_unlock(&g2d->runqueue_mutex);
924}
925
926static void g2d_finish_event(struct g2d_data *g2d, u32 cmdlist_no)
927{
928 struct drm_device *drm_dev = g2d->subdrv.drm_dev;
929 struct g2d_runqueue_node *runqueue_node = g2d->runqueue_node;
930 struct drm_exynos_pending_g2d_event *e;
931 struct timeval now;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900932
933 if (list_empty(&runqueue_node->event_list))
934 return;
935
936 e = list_first_entry(&runqueue_node->event_list,
937 struct drm_exynos_pending_g2d_event, base.link);
938
939 do_gettimeofday(&now);
940 e->event.tv_sec = now.tv_sec;
941 e->event.tv_usec = now.tv_usec;
942 e->event.cmdlist_no = cmdlist_no;
943
Daniel Vetterfb740cf2016-01-11 22:40:59 +0100944 drm_send_event(drm_dev, &e->base);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900945}
946
947static irqreturn_t g2d_irq_handler(int irq, void *dev_id)
948{
949 struct g2d_data *g2d = dev_id;
950 u32 pending;
951
952 pending = readl_relaxed(g2d->regs + G2D_INTC_PEND);
953 if (pending)
954 writel_relaxed(pending, g2d->regs + G2D_INTC_PEND);
955
956 if (pending & G2D_INTP_GCMD_FIN) {
957 u32 cmdlist_no = readl_relaxed(g2d->regs + G2D_DMA_STATUS);
958
959 cmdlist_no = (cmdlist_no & G2D_DMA_LIST_DONE_COUNT) >>
960 G2D_DMA_LIST_DONE_COUNT_OFFSET;
961
962 g2d_finish_event(g2d, cmdlist_no);
963
964 writel_relaxed(0, g2d->regs + G2D_DMA_HOLD_CMD);
965 if (!(pending & G2D_INTP_ACMD_FIN)) {
966 writel_relaxed(G2D_DMA_CONTINUE,
967 g2d->regs + G2D_DMA_COMMAND);
968 }
969 }
970
Tobias Jakobi22d67042016-09-27 17:50:07 +0200971 if (pending & G2D_INTP_ACMD_FIN) {
972 clear_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900973 queue_work(g2d->g2d_workq, &g2d->runqueue_work);
Tobias Jakobi22d67042016-09-27 17:50:07 +0200974 }
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900975
976 return IRQ_HANDLED;
977}
978
Tobias Jakobi134a0fe2016-09-27 17:50:09 +0200979/**
980 * g2d_wait_finish - wait for the G2D engine to finish the current runqueue node
981 * @g2d: G2D state object
982 * @file: if not zero, only wait if the current runqueue node belongs
983 * to the DRM file
984 *
985 * Should the engine not become idle after a 100ms timeout, a hardware
986 * reset is issued.
987 */
988static void g2d_wait_finish(struct g2d_data *g2d, struct drm_file *file)
989{
990 struct device *dev = g2d->dev;
991
992 struct g2d_runqueue_node *runqueue_node = NULL;
993 unsigned int tries = 10;
994
995 mutex_lock(&g2d->runqueue_mutex);
996
997 /* If no node is currently processed, we have nothing to do. */
998 if (!g2d->runqueue_node)
999 goto out;
1000
1001 runqueue_node = g2d->runqueue_node;
1002
1003 /* Check if the currently processed item belongs to us. */
1004 if (file && runqueue_node->filp != file)
1005 goto out;
1006
1007 mutex_unlock(&g2d->runqueue_mutex);
1008
1009 /* Wait for the G2D engine to finish. */
1010 while (tries-- && (g2d->runqueue_node == runqueue_node))
1011 mdelay(10);
1012
1013 mutex_lock(&g2d->runqueue_mutex);
1014
1015 if (g2d->runqueue_node != runqueue_node)
1016 goto out;
1017
1018 dev_err(dev, "wait timed out, resetting engine...\n");
1019 g2d_hw_reset(g2d);
1020
1021 /*
1022 * After the hardware reset of the engine we are going to loose
1023 * the IRQ which triggers the PM runtime put().
1024 * So do this manually here.
1025 */
Tobias Jakobi7c3fc2b2016-09-27 17:59:56 +02001026 pm_runtime_mark_last_busy(dev);
1027 pm_runtime_put_autosuspend(dev);
Tobias Jakobi134a0fe2016-09-27 17:50:09 +02001028
1029 complete(&runqueue_node->complete);
1030 if (runqueue_node->async)
1031 g2d_free_runqueue_node(g2d, runqueue_node);
1032
1033out:
1034 mutex_unlock(&g2d->runqueue_mutex);
1035}
1036
Inki Dae2a3098f2012-11-04 05:48:52 -08001037static int g2d_check_reg_offset(struct device *dev,
1038 struct g2d_cmdlist_node *node,
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001039 int nr, bool for_addr)
1040{
Inki Dae2a3098f2012-11-04 05:48:52 -08001041 struct g2d_cmdlist *cmdlist = node->cmdlist;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001042 int reg_offset;
1043 int index;
1044 int i;
1045
1046 for (i = 0; i < nr; i++) {
YoungJun Cho9963cb62013-03-13 17:10:08 +09001047 struct g2d_buf_info *buf_info = &node->buf_info;
YoungJun Cho2dec17c2013-03-11 21:17:52 +09001048 struct g2d_buf_desc *buf_desc;
YoungJun Cho9963cb62013-03-13 17:10:08 +09001049 enum g2d_reg_type reg_type;
YoungJun Cho2dec17c2013-03-11 21:17:52 +09001050 unsigned long value;
YoungJun Cho9963cb62013-03-13 17:10:08 +09001051
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001052 index = cmdlist->last - 2 * (i + 1);
Inki Dae2a3098f2012-11-04 05:48:52 -08001053
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001054 reg_offset = cmdlist->data[index] & ~0xfffff000;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001055 if (reg_offset < G2D_VALID_START || reg_offset > G2D_VALID_END)
1056 goto err;
1057 if (reg_offset % 4)
1058 goto err;
1059
1060 switch (reg_offset) {
1061 case G2D_SRC_BASE_ADDR:
1062 case G2D_SRC_PLANE2_BASE_ADDR:
1063 case G2D_DST_BASE_ADDR:
1064 case G2D_DST_PLANE2_BASE_ADDR:
1065 case G2D_PAT_BASE_ADDR:
1066 case G2D_MSK_BASE_ADDR:
1067 if (!for_addr)
1068 goto err;
Inki Dae2a3098f2012-11-04 05:48:52 -08001069
YoungJun Cho9963cb62013-03-13 17:10:08 +09001070 reg_type = g2d_get_reg_type(reg_offset);
YoungJun Cho9963cb62013-03-13 17:10:08 +09001071
1072 /* check userptr buffer type. */
1073 if ((cmdlist->data[index] & ~0x7fffffff) >> 31) {
1074 buf_info->types[reg_type] = BUF_TYPE_USERPTR;
1075 cmdlist->data[index] &= ~G2D_BUF_USERPTR;
1076 } else
1077 buf_info->types[reg_type] = BUF_TYPE_GEM;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001078 break;
Tobias Jakobied4dc272016-05-25 14:42:56 +02001079 case G2D_SRC_STRIDE:
1080 case G2D_DST_STRIDE:
Tobias Jakobi179239a2015-08-18 00:51:24 +02001081 if (for_addr)
1082 goto err;
1083
1084 reg_type = g2d_get_reg_type(reg_offset);
1085
1086 buf_desc = &buf_info->descs[reg_type];
1087 buf_desc->stride = cmdlist->data[index + 1];
1088 break;
YoungJun Cho2dec17c2013-03-11 21:17:52 +09001089 case G2D_SRC_COLOR_MODE:
1090 case G2D_DST_COLOR_MODE:
1091 if (for_addr)
1092 goto err;
1093
1094 reg_type = g2d_get_reg_type(reg_offset);
YoungJun Cho2dec17c2013-03-11 21:17:52 +09001095
1096 buf_desc = &buf_info->descs[reg_type];
1097 value = cmdlist->data[index + 1];
1098
1099 buf_desc->format = value & 0xf;
1100 break;
1101 case G2D_SRC_LEFT_TOP:
1102 case G2D_DST_LEFT_TOP:
1103 if (for_addr)
1104 goto err;
1105
1106 reg_type = g2d_get_reg_type(reg_offset);
YoungJun Cho2dec17c2013-03-11 21:17:52 +09001107
1108 buf_desc = &buf_info->descs[reg_type];
1109 value = cmdlist->data[index + 1];
1110
1111 buf_desc->left_x = value & 0x1fff;
1112 buf_desc->top_y = (value & 0x1fff0000) >> 16;
1113 break;
1114 case G2D_SRC_RIGHT_BOTTOM:
1115 case G2D_DST_RIGHT_BOTTOM:
1116 if (for_addr)
1117 goto err;
1118
1119 reg_type = g2d_get_reg_type(reg_offset);
YoungJun Cho2dec17c2013-03-11 21:17:52 +09001120
1121 buf_desc = &buf_info->descs[reg_type];
1122 value = cmdlist->data[index + 1];
1123
1124 buf_desc->right_x = value & 0x1fff;
1125 buf_desc->bottom_y = (value & 0x1fff0000) >> 16;
1126 break;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001127 default:
1128 if (for_addr)
1129 goto err;
1130 break;
1131 }
1132 }
1133
1134 return 0;
1135
1136err:
Inki Dae2a3098f2012-11-04 05:48:52 -08001137 dev_err(dev, "Bad register offset: 0x%lx\n", cmdlist->data[index]);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001138 return -EINVAL;
1139}
1140
1141/* ioctl functions */
1142int exynos_g2d_get_ver_ioctl(struct drm_device *drm_dev, void *data,
1143 struct drm_file *file)
1144{
Tobias Jakobief7ce052014-07-23 16:57:13 +02001145 struct drm_exynos_file_private *file_priv = file->driver_priv;
1146 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
1147 struct device *dev;
1148 struct g2d_data *g2d;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001149 struct drm_exynos_g2d_get_ver *ver = data;
1150
Tobias Jakobief7ce052014-07-23 16:57:13 +02001151 if (!g2d_priv)
1152 return -ENODEV;
1153
1154 dev = g2d_priv->dev;
1155 if (!dev)
1156 return -ENODEV;
1157
1158 g2d = dev_get_drvdata(dev);
1159 if (!g2d)
1160 return -EFAULT;
1161
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001162 ver->major = G2D_HW_MAJOR_VER;
1163 ver->minor = G2D_HW_MINOR_VER;
1164
1165 return 0;
1166}
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001167
1168int exynos_g2d_set_cmdlist_ioctl(struct drm_device *drm_dev, void *data,
1169 struct drm_file *file)
1170{
1171 struct drm_exynos_file_private *file_priv = file->driver_priv;
1172 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
Tobias Jakobi1cd1ea52014-07-23 16:57:12 +02001173 struct device *dev;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001174 struct g2d_data *g2d;
1175 struct drm_exynos_g2d_set_cmdlist *req = data;
1176 struct drm_exynos_g2d_cmd *cmd;
1177 struct drm_exynos_pending_g2d_event *e;
1178 struct g2d_cmdlist_node *node;
1179 struct g2d_cmdlist *cmdlist;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001180 int size;
1181 int ret;
1182
Tobias Jakobi1cd1ea52014-07-23 16:57:12 +02001183 if (!g2d_priv)
1184 return -ENODEV;
1185
1186 dev = g2d_priv->dev;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001187 if (!dev)
1188 return -ENODEV;
1189
1190 g2d = dev_get_drvdata(dev);
1191 if (!g2d)
1192 return -EFAULT;
1193
1194 node = g2d_get_cmdlist(g2d);
1195 if (!node)
1196 return -ENOMEM;
1197
1198 node->event = NULL;
1199
1200 if (req->event_type != G2D_EVENT_NOT) {
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001201 e = kzalloc(sizeof(*node->event), GFP_KERNEL);
1202 if (!e) {
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001203 ret = -ENOMEM;
1204 goto err;
1205 }
1206
1207 e->event.base.type = DRM_EXYNOS_G2D_EVENT;
1208 e->event.base.length = sizeof(e->event);
1209 e->event.user_data = req->user_data;
Daniel Vetter7142a342016-01-11 22:40:57 +01001210
1211 ret = drm_event_reserve_init(drm_dev, file, &e->base, &e->event.base);
1212 if (ret) {
1213 kfree(e);
1214 goto err;
1215 }
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001216
1217 node->event = e;
1218 }
1219
1220 cmdlist = node->cmdlist;
1221
1222 cmdlist->last = 0;
1223
1224 /*
1225 * If don't clear SFR registers, the cmdlist is affected by register
1226 * values of previous cmdlist. G2D hw executes SFR clear command and
1227 * a next command at the same time then the next command is ignored and
1228 * is executed rightly from next next command, so needs a dummy command
1229 * to next command of SFR clear command.
1230 */
1231 cmdlist->data[cmdlist->last++] = G2D_SOFT_RESET;
1232 cmdlist->data[cmdlist->last++] = G2D_SFRCLEAR;
1233 cmdlist->data[cmdlist->last++] = G2D_SRC_BASE_ADDR;
1234 cmdlist->data[cmdlist->last++] = 0;
1235
YoungJun Cho7ad01812013-03-13 16:44:37 +09001236 /*
1237 * 'LIST_HOLD' command should be set to the DMA_HOLD_CMD_REG
1238 * and GCF bit should be set to INTEN register if user wants
1239 * G2D interrupt event once current command list execution is
1240 * finished.
1241 * Otherwise only ACF bit should be set to INTEN register so
Masanari Iidac6b78bc2013-10-24 16:02:57 +09001242 * that one interrupt is occurred after all command lists
YoungJun Cho7ad01812013-03-13 16:44:37 +09001243 * have been completed.
1244 */
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001245 if (node->event) {
YoungJun Cho7ad01812013-03-13 16:44:37 +09001246 cmdlist->data[cmdlist->last++] = G2D_INTEN;
1247 cmdlist->data[cmdlist->last++] = G2D_INTEN_ACF | G2D_INTEN_GCF;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001248 cmdlist->data[cmdlist->last++] = G2D_DMA_HOLD_CMD;
1249 cmdlist->data[cmdlist->last++] = G2D_LIST_HOLD;
YoungJun Cho7ad01812013-03-13 16:44:37 +09001250 } else {
1251 cmdlist->data[cmdlist->last++] = G2D_INTEN;
1252 cmdlist->data[cmdlist->last++] = G2D_INTEN_ACF;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001253 }
1254
1255 /* Check size of cmdlist: last 2 is about G2D_BITBLT_START */
Inki Dae2a3098f2012-11-04 05:48:52 -08001256 size = cmdlist->last + req->cmd_nr * 2 + req->cmd_buf_nr * 2 + 2;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001257 if (size > G2D_CMDLIST_DATA_NUM) {
1258 dev_err(dev, "cmdlist size is too big\n");
1259 ret = -EINVAL;
1260 goto err_free_event;
1261 }
1262
Marek Szyprowskic5f2f0c2016-02-03 13:42:47 +01001263 cmd = (struct drm_exynos_g2d_cmd *)(unsigned long)req->cmd;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001264
1265 if (copy_from_user(cmdlist->data + cmdlist->last,
1266 (void __user *)cmd,
1267 sizeof(*cmd) * req->cmd_nr)) {
1268 ret = -EFAULT;
1269 goto err_free_event;
1270 }
1271 cmdlist->last += req->cmd_nr * 2;
1272
Inki Dae2a3098f2012-11-04 05:48:52 -08001273 ret = g2d_check_reg_offset(dev, node, req->cmd_nr, false);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001274 if (ret < 0)
1275 goto err_free_event;
1276
YoungJun Cho9963cb62013-03-13 17:10:08 +09001277 node->buf_info.map_nr = req->cmd_buf_nr;
Inki Dae2a3098f2012-11-04 05:48:52 -08001278 if (req->cmd_buf_nr) {
1279 struct drm_exynos_g2d_cmd *cmd_buf;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001280
Marek Szyprowskic5f2f0c2016-02-03 13:42:47 +01001281 cmd_buf = (struct drm_exynos_g2d_cmd *)
1282 (unsigned long)req->cmd_buf;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001283
1284 if (copy_from_user(cmdlist->data + cmdlist->last,
Inki Dae2a3098f2012-11-04 05:48:52 -08001285 (void __user *)cmd_buf,
1286 sizeof(*cmd_buf) * req->cmd_buf_nr)) {
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001287 ret = -EFAULT;
1288 goto err_free_event;
1289 }
Inki Dae2a3098f2012-11-04 05:48:52 -08001290 cmdlist->last += req->cmd_buf_nr * 2;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001291
Inki Dae2a3098f2012-11-04 05:48:52 -08001292 ret = g2d_check_reg_offset(dev, node, req->cmd_buf_nr, true);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001293 if (ret < 0)
1294 goto err_free_event;
1295
Inki Daed87342c2012-11-03 21:53:24 -07001296 ret = g2d_map_cmdlist_gem(g2d, node, drm_dev, file);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001297 if (ret < 0)
1298 goto err_unmap;
1299 }
1300
1301 cmdlist->data[cmdlist->last++] = G2D_BITBLT_START;
1302 cmdlist->data[cmdlist->last++] = G2D_START_BITBLT;
1303
1304 /* head */
1305 cmdlist->head = cmdlist->last / 2;
1306
1307 /* tail */
1308 cmdlist->data[cmdlist->last] = 0;
1309
1310 g2d_add_cmdlist_to_inuse(g2d_priv, node);
1311
1312 return 0;
1313
1314err_unmap:
Inki Daed87342c2012-11-03 21:53:24 -07001315 g2d_unmap_cmdlist_gem(g2d, node, file);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001316err_free_event:
Daniel Vetter7142a342016-01-11 22:40:57 +01001317 if (node->event)
1318 drm_event_cancel_free(drm_dev, &node->event->base);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001319err:
1320 g2d_put_cmdlist(g2d, node);
1321 return ret;
1322}
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001323
1324int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data,
1325 struct drm_file *file)
1326{
1327 struct drm_exynos_file_private *file_priv = file->driver_priv;
1328 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
Tobias Jakobi1cd1ea52014-07-23 16:57:12 +02001329 struct device *dev;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001330 struct g2d_data *g2d;
1331 struct drm_exynos_g2d_exec *req = data;
1332 struct g2d_runqueue_node *runqueue_node;
1333 struct list_head *run_cmdlist;
1334 struct list_head *event_list;
1335
Tobias Jakobi1cd1ea52014-07-23 16:57:12 +02001336 if (!g2d_priv)
1337 return -ENODEV;
1338
1339 dev = g2d_priv->dev;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001340 if (!dev)
1341 return -ENODEV;
1342
1343 g2d = dev_get_drvdata(dev);
1344 if (!g2d)
1345 return -EFAULT;
1346
1347 runqueue_node = kmem_cache_alloc(g2d->runqueue_slab, GFP_KERNEL);
1348 if (!runqueue_node) {
1349 dev_err(dev, "failed to allocate memory\n");
1350 return -ENOMEM;
1351 }
1352 run_cmdlist = &runqueue_node->run_cmdlist;
1353 event_list = &runqueue_node->event_list;
1354 INIT_LIST_HEAD(run_cmdlist);
1355 INIT_LIST_HEAD(event_list);
1356 init_completion(&runqueue_node->complete);
1357 runqueue_node->async = req->async;
1358
1359 list_splice_init(&g2d_priv->inuse_cmdlist, run_cmdlist);
1360 list_splice_init(&g2d_priv->event_list, event_list);
1361
1362 if (list_empty(run_cmdlist)) {
1363 dev_err(dev, "there is no inuse cmdlist\n");
1364 kmem_cache_free(g2d->runqueue_slab, runqueue_node);
1365 return -EPERM;
1366 }
1367
1368 mutex_lock(&g2d->runqueue_mutex);
Inki Dae6b6bae22012-09-11 10:45:36 +09001369 runqueue_node->pid = current->pid;
Inki Daed87342c2012-11-03 21:53:24 -07001370 runqueue_node->filp = file;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001371 list_add_tail(&runqueue_node->list, &g2d->runqueue);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001372 mutex_unlock(&g2d->runqueue_mutex);
1373
Tobias Jakobi22d67042016-09-27 17:50:07 +02001374 /* Let the runqueue know that there is work to do. */
1375 queue_work(g2d->g2d_workq, &g2d->runqueue_work);
1376
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001377 if (runqueue_node->async)
1378 goto out;
1379
1380 wait_for_completion(&runqueue_node->complete);
1381 g2d_free_runqueue_node(g2d, runqueue_node);
1382
1383out:
1384 return 0;
1385}
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001386
Inki Daed87342c2012-11-03 21:53:24 -07001387static int g2d_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1388{
1389 struct g2d_data *g2d;
1390 int ret;
1391
1392 g2d = dev_get_drvdata(dev);
1393 if (!g2d)
1394 return -EFAULT;
1395
1396 /* allocate dma-aware cmdlist buffer. */
1397 ret = g2d_init_cmdlist(g2d);
1398 if (ret < 0) {
1399 dev_err(dev, "cmdlist init failed\n");
1400 return ret;
1401 }
1402
Inki Daed87342c2012-11-03 21:53:24 -07001403 ret = drm_iommu_attach_device(drm_dev, dev);
1404 if (ret < 0) {
1405 dev_err(dev, "failed to enable iommu.\n");
1406 g2d_fini_cmdlist(g2d);
1407 }
1408
1409 return ret;
1410
1411}
1412
1413static void g2d_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1414{
Inki Daed87342c2012-11-03 21:53:24 -07001415 drm_iommu_detach_device(drm_dev, dev);
1416}
1417
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001418static int g2d_open(struct drm_device *drm_dev, struct device *dev,
1419 struct drm_file *file)
1420{
1421 struct drm_exynos_file_private *file_priv = file->driver_priv;
1422 struct exynos_drm_g2d_private *g2d_priv;
1423
1424 g2d_priv = kzalloc(sizeof(*g2d_priv), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +09001425 if (!g2d_priv)
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001426 return -ENOMEM;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001427
1428 g2d_priv->dev = dev;
1429 file_priv->g2d_priv = g2d_priv;
1430
1431 INIT_LIST_HEAD(&g2d_priv->inuse_cmdlist);
1432 INIT_LIST_HEAD(&g2d_priv->event_list);
Inki Dae2a3098f2012-11-04 05:48:52 -08001433 INIT_LIST_HEAD(&g2d_priv->userptr_list);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001434
1435 return 0;
1436}
1437
1438static void g2d_close(struct drm_device *drm_dev, struct device *dev,
1439 struct drm_file *file)
1440{
1441 struct drm_exynos_file_private *file_priv = file->driver_priv;
1442 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
1443 struct g2d_data *g2d;
1444 struct g2d_cmdlist_node *node, *n;
1445
1446 if (!dev)
1447 return;
1448
1449 g2d = dev_get_drvdata(dev);
1450 if (!g2d)
1451 return;
1452
Tobias Jakobi53327372016-09-27 17:50:08 +02001453 /* Remove the runqueue nodes that belong to us. */
1454 mutex_lock(&g2d->runqueue_mutex);
1455 g2d_remove_runqueue_nodes(g2d, file);
1456 mutex_unlock(&g2d->runqueue_mutex);
1457
1458 /*
Tobias Jakobi134a0fe2016-09-27 17:50:09 +02001459 * Wait for the runqueue worker to finish its current node.
1460 * After this the engine should no longer be accessing any
1461 * memory belonging to us.
1462 */
1463 g2d_wait_finish(g2d, file);
1464
1465 /*
Tobias Jakobi53327372016-09-27 17:50:08 +02001466 * Even after the engine is idle, there might still be stale cmdlists
1467 * (i.e. cmdlisst which we submitted but never executed) around, with
1468 * their corresponding GEM/userptr buffers.
1469 * Properly unmap these buffers here.
1470 */
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001471 mutex_lock(&g2d->cmdlist_mutex);
Inki Daed87342c2012-11-03 21:53:24 -07001472 list_for_each_entry_safe(node, n, &g2d_priv->inuse_cmdlist, list) {
Inki Daed87342c2012-11-03 21:53:24 -07001473 g2d_unmap_cmdlist_gem(g2d, node, file);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001474 list_move_tail(&node->list, &g2d->free_cmdlist);
Inki Daed87342c2012-11-03 21:53:24 -07001475 }
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001476 mutex_unlock(&g2d->cmdlist_mutex);
1477
Inki Dae2a3098f2012-11-04 05:48:52 -08001478 /* release all g2d_userptr in pool. */
1479 g2d_userptr_free_all(drm_dev, g2d, file);
1480
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001481 kfree(file_priv->g2d_priv);
1482}
1483
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001484static int g2d_probe(struct platform_device *pdev)
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001485{
1486 struct device *dev = &pdev->dev;
1487 struct resource *res;
1488 struct g2d_data *g2d;
1489 struct exynos_drm_subdrv *subdrv;
1490 int ret;
1491
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001492 g2d = devm_kzalloc(dev, sizeof(*g2d), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +09001493 if (!g2d)
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001494 return -ENOMEM;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001495
1496 g2d->runqueue_slab = kmem_cache_create("g2d_runqueue_slab",
1497 sizeof(struct g2d_runqueue_node), 0, 0, NULL);
Sachin Kamatb7675932012-08-06 12:16:20 +05301498 if (!g2d->runqueue_slab)
1499 return -ENOMEM;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001500
1501 g2d->dev = dev;
1502
1503 g2d->g2d_workq = create_singlethread_workqueue("g2d");
1504 if (!g2d->g2d_workq) {
1505 dev_err(dev, "failed to create workqueue\n");
1506 ret = -EINVAL;
1507 goto err_destroy_slab;
1508 }
1509
1510 INIT_WORK(&g2d->runqueue_work, g2d_runqueue_worker);
1511 INIT_LIST_HEAD(&g2d->free_cmdlist);
1512 INIT_LIST_HEAD(&g2d->runqueue);
1513
1514 mutex_init(&g2d->cmdlist_mutex);
1515 mutex_init(&g2d->runqueue_mutex);
1516
Sachin Kamatdc625532012-11-23 09:11:58 +05301517 g2d->gate_clk = devm_clk_get(dev, "fimg2d");
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001518 if (IS_ERR(g2d->gate_clk)) {
1519 dev_err(dev, "failed to get gate clock\n");
1520 ret = PTR_ERR(g2d->gate_clk);
Inki Daed87342c2012-11-03 21:53:24 -07001521 goto err_destroy_workqueue;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001522 }
1523
Tobias Jakobi7c3fc2b2016-09-27 17:59:56 +02001524 pm_runtime_use_autosuspend(dev);
1525 pm_runtime_set_autosuspend_delay(dev, 2000);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001526 pm_runtime_enable(dev);
Tobias Jakobi22d67042016-09-27 17:50:07 +02001527 clear_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
1528 clear_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001529
1530 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001531
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001532 g2d->regs = devm_ioremap_resource(dev, res);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001533 if (IS_ERR(g2d->regs)) {
1534 ret = PTR_ERR(g2d->regs);
Sachin Kamatb7675932012-08-06 12:16:20 +05301535 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001536 }
1537
1538 g2d->irq = platform_get_irq(pdev, 0);
1539 if (g2d->irq < 0) {
1540 dev_err(dev, "failed to get irq\n");
1541 ret = g2d->irq;
Sachin Kamatb7675932012-08-06 12:16:20 +05301542 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001543 }
1544
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001545 ret = devm_request_irq(dev, g2d->irq, g2d_irq_handler, 0,
Sachin Kamatb7675932012-08-06 12:16:20 +05301546 "drm_g2d", g2d);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001547 if (ret < 0) {
1548 dev_err(dev, "irq request failed\n");
Sachin Kamatb7675932012-08-06 12:16:20 +05301549 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001550 }
1551
Inki Dae2a3098f2012-11-04 05:48:52 -08001552 g2d->max_pool = MAX_POOL;
1553
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001554 platform_set_drvdata(pdev, g2d);
1555
1556 subdrv = &g2d->subdrv;
1557 subdrv->dev = dev;
Inki Daed87342c2012-11-03 21:53:24 -07001558 subdrv->probe = g2d_subdrv_probe;
1559 subdrv->remove = g2d_subdrv_remove;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001560 subdrv->open = g2d_open;
1561 subdrv->close = g2d_close;
1562
1563 ret = exynos_drm_subdrv_register(subdrv);
1564 if (ret < 0) {
1565 dev_err(dev, "failed to register drm g2d device\n");
Sachin Kamatb7675932012-08-06 12:16:20 +05301566 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001567 }
1568
Tobias Jakobi61865b52016-09-22 16:57:20 +02001569 dev_info(dev, "The Exynos G2D (ver %d.%d) successfully probed.\n",
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001570 G2D_HW_MAJOR_VER, G2D_HW_MINOR_VER);
1571
1572 return 0;
1573
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001574err_put_clk:
1575 pm_runtime_disable(dev);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001576err_destroy_workqueue:
1577 destroy_workqueue(g2d->g2d_workq);
1578err_destroy_slab:
1579 kmem_cache_destroy(g2d->runqueue_slab);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001580 return ret;
1581}
1582
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001583static int g2d_remove(struct platform_device *pdev)
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001584{
1585 struct g2d_data *g2d = platform_get_drvdata(pdev);
1586
Tobias Jakobi134a0fe2016-09-27 17:50:09 +02001587 /* Suspend operation and wait for engine idle. */
Tobias Jakobi22d67042016-09-27 17:50:07 +02001588 set_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
Tobias Jakobi134a0fe2016-09-27 17:50:09 +02001589 g2d_wait_finish(g2d, NULL);
Tobias Jakobi22d67042016-09-27 17:50:07 +02001590
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001591 cancel_work_sync(&g2d->runqueue_work);
1592 exynos_drm_subdrv_unregister(&g2d->subdrv);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001593
Tobias Jakobi53327372016-09-27 17:50:08 +02001594 /* There should be no locking needed here. */
1595 g2d_remove_runqueue_nodes(g2d, NULL);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001596
Tobias Jakobi7c3fc2b2016-09-27 17:59:56 +02001597 pm_runtime_dont_use_autosuspend(&pdev->dev);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001598 pm_runtime_disable(&pdev->dev);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001599
1600 g2d_fini_cmdlist(g2d);
1601 destroy_workqueue(g2d->g2d_workq);
1602 kmem_cache_destroy(g2d->runqueue_slab);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001603
1604 return 0;
1605}
1606
Tobias Jakobi05e2e462016-09-27 17:50:06 +02001607#ifdef CONFIG_PM_SLEEP
1608static int g2d_suspend(struct device *dev)
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001609{
1610 struct g2d_data *g2d = dev_get_drvdata(dev);
1611
Tobias Jakobi134a0fe2016-09-27 17:50:09 +02001612 /*
1613 * Suspend the runqueue worker operation and wait until the G2D
1614 * engine is idle.
1615 */
Tobias Jakobi22d67042016-09-27 17:50:07 +02001616 set_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
Tobias Jakobi134a0fe2016-09-27 17:50:09 +02001617 g2d_wait_finish(g2d, NULL);
Tejun Heo43829732012-08-20 14:51:24 -07001618 flush_work(&g2d->runqueue_work);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001619
Tobias Jakobi05e2e462016-09-27 17:50:06 +02001620 return 0;
1621}
1622
1623static int g2d_resume(struct device *dev)
1624{
1625 struct g2d_data *g2d = dev_get_drvdata(dev);
1626
Tobias Jakobi22d67042016-09-27 17:50:07 +02001627 clear_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
1628 queue_work(g2d->g2d_workq, &g2d->runqueue_work);
Tobias Jakobi05e2e462016-09-27 17:50:06 +02001629
1630 return 0;
1631}
1632#endif
1633
1634#ifdef CONFIG_PM
1635static int g2d_runtime_suspend(struct device *dev)
1636{
1637 struct g2d_data *g2d = dev_get_drvdata(dev);
1638
Inki Daeb10d6352013-07-24 15:44:30 +09001639 clk_disable_unprepare(g2d->gate_clk);
1640
1641 return 0;
1642}
1643
1644static int g2d_runtime_resume(struct device *dev)
1645{
1646 struct g2d_data *g2d = dev_get_drvdata(dev);
1647 int ret;
1648
1649 ret = clk_prepare_enable(g2d->gate_clk);
1650 if (ret < 0)
1651 dev_warn(dev, "failed to enable clock.\n");
1652
1653 return ret;
1654}
1655#endif
1656
1657static const struct dev_pm_ops g2d_pm_ops = {
Tobias Jakobi05e2e462016-09-27 17:50:06 +02001658 SET_SYSTEM_SLEEP_PM_OPS(g2d_suspend, g2d_resume)
Inki Daeb10d6352013-07-24 15:44:30 +09001659 SET_RUNTIME_PM_OPS(g2d_runtime_suspend, g2d_runtime_resume, NULL)
1660};
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001661
Ajay Kumar95fc6332013-02-06 10:59:44 +05301662static const struct of_device_id exynos_g2d_match[] = {
1663 { .compatible = "samsung,exynos5250-g2d" },
Alban Browaeys6411fe32014-07-19 22:53:03 +02001664 { .compatible = "samsung,exynos4212-g2d" },
Ajay Kumar95fc6332013-02-06 10:59:44 +05301665 {},
1666};
Sjoerd Simons0262cee2014-07-30 11:28:31 +09001667MODULE_DEVICE_TABLE(of, exynos_g2d_match);
Ajay Kumar95fc6332013-02-06 10:59:44 +05301668
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001669struct platform_driver g2d_driver = {
1670 .probe = g2d_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001671 .remove = g2d_remove,
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001672 .driver = {
1673 .name = "s5p-g2d",
1674 .owner = THIS_MODULE,
1675 .pm = &g2d_pm_ops,
Sachin Kamat61c48fb2013-08-28 10:47:56 +05301676 .of_match_table = exynos_g2d_match,
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001677 },
1678};