blob: 7584834a53c9b62a0538dab1244f9d57b3bdda3b [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>
20#include <linux/dma-attrs.h>
Ajay Kumar95fc6332013-02-06 10:59:44 +053021#include <linux/of.h>
Joonyoung Shimd7f16422012-05-17 20:06:32 +090022
David Howells760285e2012-10-02 18:01:07 +010023#include <drm/drmP.h>
24#include <drm/exynos_drm.h>
Joonyoung Shimd7f16422012-05-17 20:06:32 +090025#include "exynos_drm_drv.h"
Mark Browne30655d2013-08-13 00:46:40 +010026#include "exynos_drm_g2d.h"
Joonyoung Shimd7f16422012-05-17 20:06:32 +090027#include "exynos_drm_gem.h"
Inki Daed87342c2012-11-03 21:53:24 -070028#include "exynos_drm_iommu.h"
Joonyoung Shimd7f16422012-05-17 20:06:32 +090029
30#define G2D_HW_MAJOR_VER 4
31#define G2D_HW_MINOR_VER 1
32
33/* vaild register range set from user: 0x0104 ~ 0x0880 */
34#define G2D_VALID_START 0x0104
35#define G2D_VALID_END 0x0880
36
37/* general registers */
38#define G2D_SOFT_RESET 0x0000
39#define G2D_INTEN 0x0004
40#define G2D_INTC_PEND 0x000C
41#define G2D_DMA_SFR_BASE_ADDR 0x0080
42#define G2D_DMA_COMMAND 0x0084
43#define G2D_DMA_STATUS 0x008C
44#define G2D_DMA_HOLD_CMD 0x0090
45
46/* command registers */
47#define G2D_BITBLT_START 0x0100
48
49/* registers for base address */
50#define G2D_SRC_BASE_ADDR 0x0304
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
YoungJun Cho2dec17c2013-03-11 21:17:52 +090056#define G2D_DST_COLOR_MODE 0x040C
57#define G2D_DST_LEFT_TOP 0x0410
58#define G2D_DST_RIGHT_BOTTOM 0x0414
Joonyoung Shimd7f16422012-05-17 20:06:32 +090059#define G2D_DST_PLANE2_BASE_ADDR 0x0418
60#define G2D_PAT_BASE_ADDR 0x0500
61#define G2D_MSK_BASE_ADDR 0x0520
62
63/* G2D_SOFT_RESET */
64#define G2D_SFRCLEAR (1 << 1)
65#define G2D_R (1 << 0)
66
67/* G2D_INTEN */
68#define G2D_INTEN_ACF (1 << 3)
69#define G2D_INTEN_UCF (1 << 2)
70#define G2D_INTEN_GCF (1 << 1)
71#define G2D_INTEN_SCF (1 << 0)
72
73/* G2D_INTC_PEND */
74#define G2D_INTP_ACMD_FIN (1 << 3)
75#define G2D_INTP_UCMD_FIN (1 << 2)
76#define G2D_INTP_GCMD_FIN (1 << 1)
77#define G2D_INTP_SCMD_FIN (1 << 0)
78
79/* G2D_DMA_COMMAND */
80#define G2D_DMA_HALT (1 << 2)
81#define G2D_DMA_CONTINUE (1 << 1)
82#define G2D_DMA_START (1 << 0)
83
84/* G2D_DMA_STATUS */
85#define G2D_DMA_LIST_DONE_COUNT (0xFF << 17)
86#define G2D_DMA_BITBLT_DONE_COUNT (0xFFFF << 1)
87#define G2D_DMA_DONE (1 << 0)
88#define G2D_DMA_LIST_DONE_COUNT_OFFSET 17
89
90/* G2D_DMA_HOLD_CMD */
YoungJun Cho7ad01812013-03-13 16:44:37 +090091#define G2D_USER_HOLD (1 << 2)
Joonyoung Shimd7f16422012-05-17 20:06:32 +090092#define G2D_LIST_HOLD (1 << 1)
93#define G2D_BITBLT_HOLD (1 << 0)
94
95/* G2D_BITBLT_START */
96#define G2D_START_CASESEL (1 << 2)
97#define G2D_START_NHOLT (1 << 1)
98#define G2D_START_BITBLT (1 << 0)
99
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900100/* buffer color format */
101#define G2D_FMT_XRGB8888 0
102#define G2D_FMT_ARGB8888 1
103#define G2D_FMT_RGB565 2
104#define G2D_FMT_XRGB1555 3
105#define G2D_FMT_ARGB1555 4
106#define G2D_FMT_XRGB4444 5
107#define G2D_FMT_ARGB4444 6
108#define G2D_FMT_PACKED_RGB888 7
109#define G2D_FMT_A8 11
110#define G2D_FMT_L8 12
111
112/* buffer valid length */
113#define G2D_LEN_MIN 1
114#define G2D_LEN_MAX 8000
115
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900116#define G2D_CMDLIST_SIZE (PAGE_SIZE / 4)
117#define G2D_CMDLIST_NUM 64
118#define G2D_CMDLIST_POOL_SIZE (G2D_CMDLIST_SIZE * G2D_CMDLIST_NUM)
119#define G2D_CMDLIST_DATA_NUM (G2D_CMDLIST_SIZE / sizeof(u32) - 2)
120
Inki Dae2a3098f2012-11-04 05:48:52 -0800121/* maximum buffer pool size of userptr is 64MB as default */
122#define MAX_POOL (64 * 1024 * 1024)
123
124enum {
125 BUF_TYPE_GEM = 1,
126 BUF_TYPE_USERPTR,
127};
128
YoungJun Cho9963cb62013-03-13 17:10:08 +0900129enum g2d_reg_type {
130 REG_TYPE_NONE = -1,
131 REG_TYPE_SRC,
132 REG_TYPE_SRC_PLANE2,
133 REG_TYPE_DST,
134 REG_TYPE_DST_PLANE2,
135 REG_TYPE_PAT,
136 REG_TYPE_MSK,
137 MAX_REG_TYPE_NR
138};
139
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900140/* cmdlist data structure */
141struct g2d_cmdlist {
Inki Dae2a3098f2012-11-04 05:48:52 -0800142 u32 head;
143 unsigned long data[G2D_CMDLIST_DATA_NUM];
144 u32 last; /* last data offset */
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900145};
146
YoungJun Cho9963cb62013-03-13 17:10:08 +0900147/*
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900148 * A structure of buffer description
149 *
150 * @format: color format
151 * @left_x: the x coordinates of left top corner
152 * @top_y: the y coordinates of left top corner
153 * @right_x: the x coordinates of right bottom corner
154 * @bottom_y: the y coordinates of right bottom corner
155 *
156 */
157struct g2d_buf_desc {
158 unsigned int format;
159 unsigned int left_x;
160 unsigned int top_y;
161 unsigned int right_x;
162 unsigned int bottom_y;
163};
164
165/*
YoungJun Cho9963cb62013-03-13 17:10:08 +0900166 * A structure of buffer information
167 *
168 * @map_nr: manages the number of mapped buffers
169 * @reg_types: stores regitster type in the order of requested command
170 * @handles: stores buffer handle in its reg_type position
171 * @types: stores buffer type in its reg_type position
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900172 * @descs: stores buffer description in its reg_type position
YoungJun Cho9963cb62013-03-13 17:10:08 +0900173 *
174 */
175struct g2d_buf_info {
176 unsigned int map_nr;
177 enum g2d_reg_type reg_types[MAX_REG_TYPE_NR];
178 unsigned long handles[MAX_REG_TYPE_NR];
179 unsigned int types[MAX_REG_TYPE_NR];
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900180 struct g2d_buf_desc descs[MAX_REG_TYPE_NR];
YoungJun Cho9963cb62013-03-13 17:10:08 +0900181};
182
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900183struct drm_exynos_pending_g2d_event {
184 struct drm_pending_event base;
185 struct drm_exynos_g2d_event event;
186};
187
Inki Dae2a3098f2012-11-04 05:48:52 -0800188struct g2d_cmdlist_userptr {
189 struct list_head list;
190 dma_addr_t dma_addr;
191 unsigned long userptr;
192 unsigned long size;
Jan Kara63540f02015-07-20 05:03:35 -0300193 struct frame_vector *vec;
Inki Dae2a3098f2012-11-04 05:48:52 -0800194 struct sg_table *sgt;
Inki Dae2a3098f2012-11-04 05:48:52 -0800195 atomic_t refcount;
196 bool in_pool;
197 bool out_of_list;
198};
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900199struct g2d_cmdlist_node {
200 struct list_head list;
201 struct g2d_cmdlist *cmdlist;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900202 dma_addr_t dma_addr;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900203 struct g2d_buf_info buf_info;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900204
205 struct drm_exynos_pending_g2d_event *event;
206};
207
208struct g2d_runqueue_node {
209 struct list_head list;
210 struct list_head run_cmdlist;
211 struct list_head event_list;
Inki Daed87342c2012-11-03 21:53:24 -0700212 struct drm_file *filp;
Inki Dae6b6bae22012-09-11 10:45:36 +0900213 pid_t pid;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900214 struct completion complete;
215 int async;
216};
217
218struct g2d_data {
219 struct device *dev;
220 struct clk *gate_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900221 void __iomem *regs;
222 int irq;
223 struct workqueue_struct *g2d_workq;
224 struct work_struct runqueue_work;
225 struct exynos_drm_subdrv subdrv;
226 bool suspended;
227
228 /* cmdlist */
229 struct g2d_cmdlist_node *cmdlist_node;
230 struct list_head free_cmdlist;
231 struct mutex cmdlist_mutex;
232 dma_addr_t cmdlist_pool;
233 void *cmdlist_pool_virt;
Inki Daed87342c2012-11-03 21:53:24 -0700234 struct dma_attrs cmdlist_dma_attrs;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900235
236 /* runqueue*/
237 struct g2d_runqueue_node *runqueue_node;
238 struct list_head runqueue;
239 struct mutex runqueue_mutex;
240 struct kmem_cache *runqueue_slab;
Inki Dae2a3098f2012-11-04 05:48:52 -0800241
242 unsigned long current_pool;
243 unsigned long max_pool;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900244};
245
246static int g2d_init_cmdlist(struct g2d_data *g2d)
247{
248 struct device *dev = g2d->dev;
249 struct g2d_cmdlist_node *node = g2d->cmdlist_node;
Inki Daed87342c2012-11-03 21:53:24 -0700250 struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900251 int nr;
252 int ret;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900253 struct g2d_buf_info *buf_info;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900254
Inki Daed87342c2012-11-03 21:53:24 -0700255 init_dma_attrs(&g2d->cmdlist_dma_attrs);
256 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &g2d->cmdlist_dma_attrs);
257
258 g2d->cmdlist_pool_virt = dma_alloc_attrs(subdrv->drm_dev->dev,
259 G2D_CMDLIST_POOL_SIZE,
260 &g2d->cmdlist_pool, GFP_KERNEL,
261 &g2d->cmdlist_dma_attrs);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900262 if (!g2d->cmdlist_pool_virt) {
263 dev_err(dev, "failed to allocate dma memory\n");
264 return -ENOMEM;
265 }
266
Joonyoung Shimfab9f8d2012-09-27 19:26:03 +0900267 node = kcalloc(G2D_CMDLIST_NUM, sizeof(*node), GFP_KERNEL);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900268 if (!node) {
269 dev_err(dev, "failed to allocate memory\n");
270 ret = -ENOMEM;
271 goto err;
272 }
273
274 for (nr = 0; nr < G2D_CMDLIST_NUM; nr++) {
YoungJun Cho9963cb62013-03-13 17:10:08 +0900275 unsigned int i;
276
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900277 node[nr].cmdlist =
278 g2d->cmdlist_pool_virt + nr * G2D_CMDLIST_SIZE;
279 node[nr].dma_addr =
280 g2d->cmdlist_pool + nr * G2D_CMDLIST_SIZE;
281
YoungJun Cho9963cb62013-03-13 17:10:08 +0900282 buf_info = &node[nr].buf_info;
283 for (i = 0; i < MAX_REG_TYPE_NR; i++)
284 buf_info->reg_types[i] = REG_TYPE_NONE;
285
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900286 list_add_tail(&node[nr].list, &g2d->free_cmdlist);
287 }
288
289 return 0;
290
291err:
Inki Daed87342c2012-11-03 21:53:24 -0700292 dma_free_attrs(subdrv->drm_dev->dev, G2D_CMDLIST_POOL_SIZE,
293 g2d->cmdlist_pool_virt,
294 g2d->cmdlist_pool, &g2d->cmdlist_dma_attrs);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900295 return ret;
296}
297
298static void g2d_fini_cmdlist(struct g2d_data *g2d)
299{
Inki Daed87342c2012-11-03 21:53:24 -0700300 struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900301
302 kfree(g2d->cmdlist_node);
Inki Dae9ad703e2014-11-07 20:31:08 +0900303
304 if (g2d->cmdlist_pool_virt && g2d->cmdlist_pool) {
305 dma_free_attrs(subdrv->drm_dev->dev, G2D_CMDLIST_POOL_SIZE,
306 g2d->cmdlist_pool_virt,
307 g2d->cmdlist_pool, &g2d->cmdlist_dma_attrs);
308 }
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900309}
310
311static struct g2d_cmdlist_node *g2d_get_cmdlist(struct g2d_data *g2d)
312{
313 struct device *dev = g2d->dev;
314 struct g2d_cmdlist_node *node;
315
316 mutex_lock(&g2d->cmdlist_mutex);
317 if (list_empty(&g2d->free_cmdlist)) {
318 dev_err(dev, "there is no free cmdlist\n");
319 mutex_unlock(&g2d->cmdlist_mutex);
320 return NULL;
321 }
322
323 node = list_first_entry(&g2d->free_cmdlist, struct g2d_cmdlist_node,
324 list);
325 list_del_init(&node->list);
326 mutex_unlock(&g2d->cmdlist_mutex);
327
328 return node;
329}
330
331static void g2d_put_cmdlist(struct g2d_data *g2d, struct g2d_cmdlist_node *node)
332{
333 mutex_lock(&g2d->cmdlist_mutex);
334 list_move_tail(&node->list, &g2d->free_cmdlist);
335 mutex_unlock(&g2d->cmdlist_mutex);
336}
337
338static void g2d_add_cmdlist_to_inuse(struct exynos_drm_g2d_private *g2d_priv,
339 struct g2d_cmdlist_node *node)
340{
341 struct g2d_cmdlist_node *lnode;
342
343 if (list_empty(&g2d_priv->inuse_cmdlist))
344 goto add_to_list;
345
346 /* this links to base address of new cmdlist */
347 lnode = list_entry(g2d_priv->inuse_cmdlist.prev,
348 struct g2d_cmdlist_node, list);
349 lnode->cmdlist->data[lnode->cmdlist->last] = node->dma_addr;
350
351add_to_list:
352 list_add_tail(&node->list, &g2d_priv->inuse_cmdlist);
353
354 if (node->event)
355 list_add_tail(&node->event->base.link, &g2d_priv->event_list);
356}
357
Inki Dae2a3098f2012-11-04 05:48:52 -0800358static void g2d_userptr_put_dma_addr(struct drm_device *drm_dev,
359 unsigned long obj,
360 bool force)
361{
362 struct g2d_cmdlist_userptr *g2d_userptr =
363 (struct g2d_cmdlist_userptr *)obj;
Jan Kara63540f02015-07-20 05:03:35 -0300364 struct page **pages;
Inki Dae2a3098f2012-11-04 05:48:52 -0800365
366 if (!obj)
367 return;
368
369 if (force)
370 goto out;
371
372 atomic_dec(&g2d_userptr->refcount);
373
374 if (atomic_read(&g2d_userptr->refcount) > 0)
375 return;
376
377 if (g2d_userptr->in_pool)
378 return;
379
380out:
381 exynos_gem_unmap_sgt_from_dma(drm_dev, g2d_userptr->sgt,
382 DMA_BIDIRECTIONAL);
383
Jan Kara63540f02015-07-20 05:03:35 -0300384 pages = frame_vector_pages(g2d_userptr->vec);
385 if (!IS_ERR(pages)) {
386 int i;
Inki Dae2a3098f2012-11-04 05:48:52 -0800387
Jan Kara63540f02015-07-20 05:03:35 -0300388 for (i = 0; i < frame_vector_count(g2d_userptr->vec); i++)
389 set_page_dirty_lock(pages[i]);
390 }
391 put_vaddr_frames(g2d_userptr->vec);
392 frame_vector_destroy(g2d_userptr->vec);
Inki Daec3bddbd2013-11-21 12:09:51 +0900393
Inki Dae2a3098f2012-11-04 05:48:52 -0800394 if (!g2d_userptr->out_of_list)
395 list_del_init(&g2d_userptr->list);
396
397 sg_free_table(g2d_userptr->sgt);
398 kfree(g2d_userptr->sgt);
Sachin Kamatdf3d90e2012-11-23 09:11:59 +0530399 kfree(g2d_userptr);
Inki Dae2a3098f2012-11-04 05:48:52 -0800400}
401
Sachin Kamatb7848c72013-01-14 12:29:09 +0530402static dma_addr_t *g2d_userptr_get_dma_addr(struct drm_device *drm_dev,
Inki Dae2a3098f2012-11-04 05:48:52 -0800403 unsigned long userptr,
404 unsigned long size,
405 struct drm_file *filp,
406 unsigned long *obj)
407{
408 struct drm_exynos_file_private *file_priv = filp->driver_priv;
409 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
410 struct g2d_cmdlist_userptr *g2d_userptr;
411 struct g2d_data *g2d;
Inki Dae2a3098f2012-11-04 05:48:52 -0800412 struct sg_table *sgt;
Inki Dae2a3098f2012-11-04 05:48:52 -0800413 unsigned long start, end;
414 unsigned int npages, offset;
415 int ret;
416
417 if (!size) {
418 DRM_ERROR("invalid userptr size.\n");
419 return ERR_PTR(-EINVAL);
420 }
421
422 g2d = dev_get_drvdata(g2d_priv->dev);
423
424 /* check if userptr already exists in userptr_list. */
425 list_for_each_entry(g2d_userptr, &g2d_priv->userptr_list, list) {
426 if (g2d_userptr->userptr == userptr) {
427 /*
428 * also check size because there could be same address
429 * and different size.
430 */
431 if (g2d_userptr->size == size) {
432 atomic_inc(&g2d_userptr->refcount);
433 *obj = (unsigned long)g2d_userptr;
434
435 return &g2d_userptr->dma_addr;
436 }
437
438 /*
439 * at this moment, maybe g2d dma is accessing this
440 * g2d_userptr memory region so just remove this
441 * g2d_userptr object from userptr_list not to be
442 * referred again and also except it the userptr
443 * pool to be released after the dma access completion.
444 */
445 g2d_userptr->out_of_list = true;
446 g2d_userptr->in_pool = false;
447 list_del_init(&g2d_userptr->list);
448
449 break;
450 }
451 }
452
453 g2d_userptr = kzalloc(sizeof(*g2d_userptr), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900454 if (!g2d_userptr)
Inki Dae2a3098f2012-11-04 05:48:52 -0800455 return ERR_PTR(-ENOMEM);
Inki Dae2a3098f2012-11-04 05:48:52 -0800456
457 atomic_set(&g2d_userptr->refcount, 1);
Jan Kara63540f02015-07-20 05:03:35 -0300458 g2d_userptr->size = size;
Inki Dae2a3098f2012-11-04 05:48:52 -0800459
460 start = userptr & PAGE_MASK;
461 offset = userptr & ~PAGE_MASK;
462 end = PAGE_ALIGN(userptr + size);
463 npages = (end - start) >> PAGE_SHIFT;
Jan Kara63540f02015-07-20 05:03:35 -0300464 g2d_userptr->vec = frame_vector_create(npages);
465 if (!g2d_userptr->vec) {
Seung-Woo Kim4bb615c2013-07-03 17:09:21 +0900466 ret = -ENOMEM;
467 goto err_free;
Inki Dae2a3098f2012-11-04 05:48:52 -0800468 }
469
Jan Kara63540f02015-07-20 05:03:35 -0300470 ret = get_vaddr_frames(start, npages, true, true, g2d_userptr->vec);
471 if (ret != npages) {
Inki Dae2a3098f2012-11-04 05:48:52 -0800472 DRM_ERROR("failed to get user pages from userptr.\n");
Jan Kara63540f02015-07-20 05:03:35 -0300473 if (ret < 0)
474 goto err_destroy_framevec;
475 ret = -EFAULT;
476 goto err_put_framevec;
Inki Dae2a3098f2012-11-04 05:48:52 -0800477 }
Jan Kara63540f02015-07-20 05:03:35 -0300478 if (frame_vector_to_pages(g2d_userptr->vec) < 0) {
479 ret = -EFAULT;
480 goto err_put_framevec;
481 }
Inki Dae2a3098f2012-11-04 05:48:52 -0800482
Sachin Kamate44a5c02013-01-25 14:45:42 +0530483 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
Inki Dae2a3098f2012-11-04 05:48:52 -0800484 if (!sgt) {
Inki Dae2a3098f2012-11-04 05:48:52 -0800485 ret = -ENOMEM;
Jan Kara63540f02015-07-20 05:03:35 -0300486 goto err_put_framevec;
Inki Dae2a3098f2012-11-04 05:48:52 -0800487 }
488
Jan Kara63540f02015-07-20 05:03:35 -0300489 ret = sg_alloc_table_from_pages(sgt,
490 frame_vector_pages(g2d_userptr->vec),
491 npages, offset, size, GFP_KERNEL);
Inki Dae2a3098f2012-11-04 05:48:52 -0800492 if (ret < 0) {
493 DRM_ERROR("failed to get sgt from pages.\n");
494 goto err_free_sgt;
495 }
496
497 g2d_userptr->sgt = sgt;
498
499 ret = exynos_gem_map_sgt_with_dma(drm_dev, g2d_userptr->sgt,
500 DMA_BIDIRECTIONAL);
501 if (ret < 0) {
502 DRM_ERROR("failed to map sgt with dma region.\n");
YoungJun Cho067ed332013-03-11 19:48:05 +0900503 goto err_sg_free_table;
Inki Dae2a3098f2012-11-04 05:48:52 -0800504 }
505
506 g2d_userptr->dma_addr = sgt->sgl[0].dma_address;
507 g2d_userptr->userptr = userptr;
508
509 list_add_tail(&g2d_userptr->list, &g2d_priv->userptr_list);
510
511 if (g2d->current_pool + (npages << PAGE_SHIFT) < g2d->max_pool) {
512 g2d->current_pool += npages << PAGE_SHIFT;
513 g2d_userptr->in_pool = true;
514 }
515
516 *obj = (unsigned long)g2d_userptr;
517
518 return &g2d_userptr->dma_addr;
519
YoungJun Cho067ed332013-03-11 19:48:05 +0900520err_sg_free_table:
Inki Dae2a3098f2012-11-04 05:48:52 -0800521 sg_free_table(sgt);
YoungJun Cho067ed332013-03-11 19:48:05 +0900522
523err_free_sgt:
Inki Dae2a3098f2012-11-04 05:48:52 -0800524 kfree(sgt);
Inki Dae2a3098f2012-11-04 05:48:52 -0800525
Jan Kara63540f02015-07-20 05:03:35 -0300526err_put_framevec:
527 put_vaddr_frames(g2d_userptr->vec);
Inki Dae2a3098f2012-11-04 05:48:52 -0800528
Jan Kara63540f02015-07-20 05:03:35 -0300529err_destroy_framevec:
530 frame_vector_destroy(g2d_userptr->vec);
Seung-Woo Kim4bb615c2013-07-03 17:09:21 +0900531
532err_free:
Inki Dae2a3098f2012-11-04 05:48:52 -0800533 kfree(g2d_userptr);
Inki Dae2a3098f2012-11-04 05:48:52 -0800534
535 return ERR_PTR(ret);
536}
537
538static void g2d_userptr_free_all(struct drm_device *drm_dev,
539 struct g2d_data *g2d,
540 struct drm_file *filp)
541{
542 struct drm_exynos_file_private *file_priv = filp->driver_priv;
543 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
544 struct g2d_cmdlist_userptr *g2d_userptr, *n;
545
546 list_for_each_entry_safe(g2d_userptr, n, &g2d_priv->userptr_list, list)
547 if (g2d_userptr->in_pool)
548 g2d_userptr_put_dma_addr(drm_dev,
549 (unsigned long)g2d_userptr,
550 true);
551
552 g2d->current_pool = 0;
553}
554
YoungJun Cho9963cb62013-03-13 17:10:08 +0900555static enum g2d_reg_type g2d_get_reg_type(int reg_offset)
556{
557 enum g2d_reg_type reg_type;
558
559 switch (reg_offset) {
560 case G2D_SRC_BASE_ADDR:
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900561 case G2D_SRC_COLOR_MODE:
562 case G2D_SRC_LEFT_TOP:
563 case G2D_SRC_RIGHT_BOTTOM:
YoungJun Cho9963cb62013-03-13 17:10:08 +0900564 reg_type = REG_TYPE_SRC;
565 break;
566 case G2D_SRC_PLANE2_BASE_ADDR:
567 reg_type = REG_TYPE_SRC_PLANE2;
568 break;
569 case G2D_DST_BASE_ADDR:
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900570 case G2D_DST_COLOR_MODE:
571 case G2D_DST_LEFT_TOP:
572 case G2D_DST_RIGHT_BOTTOM:
YoungJun Cho9963cb62013-03-13 17:10:08 +0900573 reg_type = REG_TYPE_DST;
574 break;
575 case G2D_DST_PLANE2_BASE_ADDR:
576 reg_type = REG_TYPE_DST_PLANE2;
577 break;
578 case G2D_PAT_BASE_ADDR:
579 reg_type = REG_TYPE_PAT;
580 break;
581 case G2D_MSK_BASE_ADDR:
582 reg_type = REG_TYPE_MSK;
583 break;
584 default:
585 reg_type = REG_TYPE_NONE;
586 DRM_ERROR("Unknown register offset![%d]\n", reg_offset);
587 break;
Sachin Kamat5cdbc8d2014-01-16 10:00:22 +0530588 }
YoungJun Cho9963cb62013-03-13 17:10:08 +0900589
590 return reg_type;
591}
592
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900593static unsigned long g2d_get_buf_bpp(unsigned int format)
594{
595 unsigned long bpp;
596
597 switch (format) {
598 case G2D_FMT_XRGB8888:
599 case G2D_FMT_ARGB8888:
600 bpp = 4;
601 break;
602 case G2D_FMT_RGB565:
603 case G2D_FMT_XRGB1555:
604 case G2D_FMT_ARGB1555:
605 case G2D_FMT_XRGB4444:
606 case G2D_FMT_ARGB4444:
607 bpp = 2;
608 break;
609 case G2D_FMT_PACKED_RGB888:
610 bpp = 3;
611 break;
612 default:
613 bpp = 1;
614 break;
615 }
616
617 return bpp;
618}
619
620static bool g2d_check_buf_desc_is_valid(struct g2d_buf_desc *buf_desc,
621 enum g2d_reg_type reg_type,
622 unsigned long size)
623{
624 unsigned int width, height;
625 unsigned long area;
626
627 /*
628 * check source and destination buffers only.
629 * so the others are always valid.
630 */
631 if (reg_type != REG_TYPE_SRC && reg_type != REG_TYPE_DST)
632 return true;
633
634 width = buf_desc->right_x - buf_desc->left_x;
635 if (width < G2D_LEN_MIN || width > G2D_LEN_MAX) {
636 DRM_ERROR("width[%u] is out of range!\n", width);
637 return false;
638 }
639
640 height = buf_desc->bottom_y - buf_desc->top_y;
641 if (height < G2D_LEN_MIN || height > G2D_LEN_MAX) {
642 DRM_ERROR("height[%u] is out of range!\n", height);
643 return false;
644 }
645
646 area = (unsigned long)width * (unsigned long)height *
647 g2d_get_buf_bpp(buf_desc->format);
648 if (area > size) {
649 DRM_ERROR("area[%lu] is out of range[%lu]!\n", area, size);
650 return false;
651 }
652
653 return true;
654}
655
Inki Daed87342c2012-11-03 21:53:24 -0700656static int g2d_map_cmdlist_gem(struct g2d_data *g2d,
657 struct g2d_cmdlist_node *node,
658 struct drm_device *drm_dev,
659 struct drm_file *file)
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900660{
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900661 struct g2d_cmdlist *cmdlist = node->cmdlist;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900662 struct g2d_buf_info *buf_info = &node->buf_info;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900663 int offset;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900664 int ret;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900665 int i;
666
YoungJun Cho9963cb62013-03-13 17:10:08 +0900667 for (i = 0; i < buf_info->map_nr; i++) {
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900668 struct g2d_buf_desc *buf_desc;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900669 enum g2d_reg_type reg_type;
670 int reg_pos;
Inki Daed87342c2012-11-03 21:53:24 -0700671 unsigned long handle;
672 dma_addr_t *addr;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900673
YoungJun Cho9963cb62013-03-13 17:10:08 +0900674 reg_pos = cmdlist->last - 2 * (i + 1);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900675
YoungJun Cho9963cb62013-03-13 17:10:08 +0900676 offset = cmdlist->data[reg_pos];
677 handle = cmdlist->data[reg_pos + 1];
678
679 reg_type = g2d_get_reg_type(offset);
680 if (reg_type == REG_TYPE_NONE) {
681 ret = -EFAULT;
682 goto err;
683 }
684
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900685 buf_desc = &buf_info->descs[reg_type];
686
YoungJun Cho9963cb62013-03-13 17:10:08 +0900687 if (buf_info->types[reg_type] == BUF_TYPE_GEM) {
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900688 unsigned long size;
689
690 size = exynos_drm_gem_get_size(drm_dev, handle, file);
691 if (!size) {
692 ret = -EFAULT;
693 goto err;
694 }
695
696 if (!g2d_check_buf_desc_is_valid(buf_desc, reg_type,
697 size)) {
698 ret = -EFAULT;
699 goto err;
700 }
701
Inki Dae2a3098f2012-11-04 05:48:52 -0800702 addr = exynos_drm_gem_get_dma_addr(drm_dev, handle,
703 file);
704 if (IS_ERR(addr)) {
YoungJun Cho9963cb62013-03-13 17:10:08 +0900705 ret = -EFAULT;
706 goto err;
Inki Dae2a3098f2012-11-04 05:48:52 -0800707 }
708 } else {
709 struct drm_exynos_g2d_userptr g2d_userptr;
710
711 if (copy_from_user(&g2d_userptr, (void __user *)handle,
712 sizeof(struct drm_exynos_g2d_userptr))) {
YoungJun Cho9963cb62013-03-13 17:10:08 +0900713 ret = -EFAULT;
714 goto err;
Inki Dae2a3098f2012-11-04 05:48:52 -0800715 }
716
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900717 if (!g2d_check_buf_desc_is_valid(buf_desc, reg_type,
718 g2d_userptr.size)) {
719 ret = -EFAULT;
720 goto err;
721 }
722
Inki Dae2a3098f2012-11-04 05:48:52 -0800723 addr = g2d_userptr_get_dma_addr(drm_dev,
724 g2d_userptr.userptr,
725 g2d_userptr.size,
726 file,
727 &handle);
728 if (IS_ERR(addr)) {
YoungJun Cho9963cb62013-03-13 17:10:08 +0900729 ret = -EFAULT;
730 goto err;
Inki Dae2a3098f2012-11-04 05:48:52 -0800731 }
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900732 }
733
YoungJun Cho9963cb62013-03-13 17:10:08 +0900734 cmdlist->data[reg_pos + 1] = *addr;
735 buf_info->reg_types[i] = reg_type;
736 buf_info->handles[reg_type] = handle;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900737 }
738
739 return 0;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900740
741err:
742 buf_info->map_nr = i;
743 return ret;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900744}
745
Inki Daed87342c2012-11-03 21:53:24 -0700746static void g2d_unmap_cmdlist_gem(struct g2d_data *g2d,
747 struct g2d_cmdlist_node *node,
748 struct drm_file *filp)
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900749{
Inki Daed87342c2012-11-03 21:53:24 -0700750 struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900751 struct g2d_buf_info *buf_info = &node->buf_info;
Inki Daed87342c2012-11-03 21:53:24 -0700752 int i;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900753
YoungJun Cho9963cb62013-03-13 17:10:08 +0900754 for (i = 0; i < buf_info->map_nr; i++) {
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900755 struct g2d_buf_desc *buf_desc;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900756 enum g2d_reg_type reg_type;
757 unsigned long handle;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900758
YoungJun Cho9963cb62013-03-13 17:10:08 +0900759 reg_type = buf_info->reg_types[i];
760
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900761 buf_desc = &buf_info->descs[reg_type];
YoungJun Cho9963cb62013-03-13 17:10:08 +0900762 handle = buf_info->handles[reg_type];
763
764 if (buf_info->types[reg_type] == BUF_TYPE_GEM)
Inki Dae2a3098f2012-11-04 05:48:52 -0800765 exynos_drm_gem_put_dma_addr(subdrv->drm_dev, handle,
766 filp);
767 else
768 g2d_userptr_put_dma_addr(subdrv->drm_dev, handle,
769 false);
Inki Daed87342c2012-11-03 21:53:24 -0700770
YoungJun Cho9963cb62013-03-13 17:10:08 +0900771 buf_info->reg_types[i] = REG_TYPE_NONE;
772 buf_info->handles[reg_type] = 0;
773 buf_info->types[reg_type] = 0;
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900774 memset(buf_desc, 0x00, sizeof(*buf_desc));
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900775 }
Inki Daed87342c2012-11-03 21:53:24 -0700776
YoungJun Cho9963cb62013-03-13 17:10:08 +0900777 buf_info->map_nr = 0;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900778}
779
780static void g2d_dma_start(struct g2d_data *g2d,
781 struct g2d_runqueue_node *runqueue_node)
782{
783 struct g2d_cmdlist_node *node =
784 list_first_entry(&runqueue_node->run_cmdlist,
785 struct g2d_cmdlist_node, list);
Inki Dae89f8b852013-07-24 13:40:12 +0900786 int ret;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900787
Inki Dae89f8b852013-07-24 13:40:12 +0900788 ret = pm_runtime_get_sync(g2d->dev);
Inki Daeb10d6352013-07-24 15:44:30 +0900789 if (ret < 0)
Inki Dae89f8b852013-07-24 13:40:12 +0900790 return;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900791
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900792 writel_relaxed(node->dma_addr, g2d->regs + G2D_DMA_SFR_BASE_ADDR);
793 writel_relaxed(G2D_DMA_START, g2d->regs + G2D_DMA_COMMAND);
794}
795
796static struct g2d_runqueue_node *g2d_get_runqueue_node(struct g2d_data *g2d)
797{
798 struct g2d_runqueue_node *runqueue_node;
799
800 if (list_empty(&g2d->runqueue))
801 return NULL;
802
803 runqueue_node = list_first_entry(&g2d->runqueue,
804 struct g2d_runqueue_node, list);
805 list_del_init(&runqueue_node->list);
806 return runqueue_node;
807}
808
809static void g2d_free_runqueue_node(struct g2d_data *g2d,
810 struct g2d_runqueue_node *runqueue_node)
811{
Inki Daed87342c2012-11-03 21:53:24 -0700812 struct g2d_cmdlist_node *node;
813
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900814 if (!runqueue_node)
815 return;
816
817 mutex_lock(&g2d->cmdlist_mutex);
Inki Daed87342c2012-11-03 21:53:24 -0700818 /*
819 * commands in run_cmdlist have been completed so unmap all gem
820 * objects in each command node so that they are unreferenced.
821 */
822 list_for_each_entry(node, &runqueue_node->run_cmdlist, list)
823 g2d_unmap_cmdlist_gem(g2d, node, runqueue_node->filp);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900824 list_splice_tail_init(&runqueue_node->run_cmdlist, &g2d->free_cmdlist);
825 mutex_unlock(&g2d->cmdlist_mutex);
826
827 kmem_cache_free(g2d->runqueue_slab, runqueue_node);
828}
829
830static void g2d_exec_runqueue(struct g2d_data *g2d)
831{
832 g2d->runqueue_node = g2d_get_runqueue_node(g2d);
833 if (g2d->runqueue_node)
834 g2d_dma_start(g2d, g2d->runqueue_node);
835}
836
837static void g2d_runqueue_worker(struct work_struct *work)
838{
839 struct g2d_data *g2d = container_of(work, struct g2d_data,
840 runqueue_work);
841
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900842 mutex_lock(&g2d->runqueue_mutex);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900843 pm_runtime_put_sync(g2d->dev);
844
845 complete(&g2d->runqueue_node->complete);
846 if (g2d->runqueue_node->async)
847 g2d_free_runqueue_node(g2d, g2d->runqueue_node);
848
849 if (g2d->suspended)
850 g2d->runqueue_node = NULL;
851 else
852 g2d_exec_runqueue(g2d);
853 mutex_unlock(&g2d->runqueue_mutex);
854}
855
856static void g2d_finish_event(struct g2d_data *g2d, u32 cmdlist_no)
857{
858 struct drm_device *drm_dev = g2d->subdrv.drm_dev;
859 struct g2d_runqueue_node *runqueue_node = g2d->runqueue_node;
860 struct drm_exynos_pending_g2d_event *e;
861 struct timeval now;
862 unsigned long flags;
863
864 if (list_empty(&runqueue_node->event_list))
865 return;
866
867 e = list_first_entry(&runqueue_node->event_list,
868 struct drm_exynos_pending_g2d_event, base.link);
869
870 do_gettimeofday(&now);
871 e->event.tv_sec = now.tv_sec;
872 e->event.tv_usec = now.tv_usec;
873 e->event.cmdlist_no = cmdlist_no;
874
875 spin_lock_irqsave(&drm_dev->event_lock, flags);
876 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
877 wake_up_interruptible(&e->base.file_priv->event_wait);
878 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
879}
880
881static irqreturn_t g2d_irq_handler(int irq, void *dev_id)
882{
883 struct g2d_data *g2d = dev_id;
884 u32 pending;
885
886 pending = readl_relaxed(g2d->regs + G2D_INTC_PEND);
887 if (pending)
888 writel_relaxed(pending, g2d->regs + G2D_INTC_PEND);
889
890 if (pending & G2D_INTP_GCMD_FIN) {
891 u32 cmdlist_no = readl_relaxed(g2d->regs + G2D_DMA_STATUS);
892
893 cmdlist_no = (cmdlist_no & G2D_DMA_LIST_DONE_COUNT) >>
894 G2D_DMA_LIST_DONE_COUNT_OFFSET;
895
896 g2d_finish_event(g2d, cmdlist_no);
897
898 writel_relaxed(0, g2d->regs + G2D_DMA_HOLD_CMD);
899 if (!(pending & G2D_INTP_ACMD_FIN)) {
900 writel_relaxed(G2D_DMA_CONTINUE,
901 g2d->regs + G2D_DMA_COMMAND);
902 }
903 }
904
905 if (pending & G2D_INTP_ACMD_FIN)
906 queue_work(g2d->g2d_workq, &g2d->runqueue_work);
907
908 return IRQ_HANDLED;
909}
910
Inki Dae2a3098f2012-11-04 05:48:52 -0800911static int g2d_check_reg_offset(struct device *dev,
912 struct g2d_cmdlist_node *node,
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900913 int nr, bool for_addr)
914{
Inki Dae2a3098f2012-11-04 05:48:52 -0800915 struct g2d_cmdlist *cmdlist = node->cmdlist;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900916 int reg_offset;
917 int index;
918 int i;
919
920 for (i = 0; i < nr; i++) {
YoungJun Cho9963cb62013-03-13 17:10:08 +0900921 struct g2d_buf_info *buf_info = &node->buf_info;
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900922 struct g2d_buf_desc *buf_desc;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900923 enum g2d_reg_type reg_type;
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900924 unsigned long value;
YoungJun Cho9963cb62013-03-13 17:10:08 +0900925
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900926 index = cmdlist->last - 2 * (i + 1);
Inki Dae2a3098f2012-11-04 05:48:52 -0800927
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900928 reg_offset = cmdlist->data[index] & ~0xfffff000;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900929 if (reg_offset < G2D_VALID_START || reg_offset > G2D_VALID_END)
930 goto err;
931 if (reg_offset % 4)
932 goto err;
933
934 switch (reg_offset) {
935 case G2D_SRC_BASE_ADDR:
936 case G2D_SRC_PLANE2_BASE_ADDR:
937 case G2D_DST_BASE_ADDR:
938 case G2D_DST_PLANE2_BASE_ADDR:
939 case G2D_PAT_BASE_ADDR:
940 case G2D_MSK_BASE_ADDR:
941 if (!for_addr)
942 goto err;
Inki Dae2a3098f2012-11-04 05:48:52 -0800943
YoungJun Cho9963cb62013-03-13 17:10:08 +0900944 reg_type = g2d_get_reg_type(reg_offset);
945 if (reg_type == REG_TYPE_NONE)
946 goto err;
947
948 /* check userptr buffer type. */
949 if ((cmdlist->data[index] & ~0x7fffffff) >> 31) {
950 buf_info->types[reg_type] = BUF_TYPE_USERPTR;
951 cmdlist->data[index] &= ~G2D_BUF_USERPTR;
952 } else
953 buf_info->types[reg_type] = BUF_TYPE_GEM;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900954 break;
YoungJun Cho2dec17c2013-03-11 21:17:52 +0900955 case G2D_SRC_COLOR_MODE:
956 case G2D_DST_COLOR_MODE:
957 if (for_addr)
958 goto err;
959
960 reg_type = g2d_get_reg_type(reg_offset);
961 if (reg_type == REG_TYPE_NONE)
962 goto err;
963
964 buf_desc = &buf_info->descs[reg_type];
965 value = cmdlist->data[index + 1];
966
967 buf_desc->format = value & 0xf;
968 break;
969 case G2D_SRC_LEFT_TOP:
970 case G2D_DST_LEFT_TOP:
971 if (for_addr)
972 goto err;
973
974 reg_type = g2d_get_reg_type(reg_offset);
975 if (reg_type == REG_TYPE_NONE)
976 goto err;
977
978 buf_desc = &buf_info->descs[reg_type];
979 value = cmdlist->data[index + 1];
980
981 buf_desc->left_x = value & 0x1fff;
982 buf_desc->top_y = (value & 0x1fff0000) >> 16;
983 break;
984 case G2D_SRC_RIGHT_BOTTOM:
985 case G2D_DST_RIGHT_BOTTOM:
986 if (for_addr)
987 goto err;
988
989 reg_type = g2d_get_reg_type(reg_offset);
990 if (reg_type == REG_TYPE_NONE)
991 goto err;
992
993 buf_desc = &buf_info->descs[reg_type];
994 value = cmdlist->data[index + 1];
995
996 buf_desc->right_x = value & 0x1fff;
997 buf_desc->bottom_y = (value & 0x1fff0000) >> 16;
998 break;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900999 default:
1000 if (for_addr)
1001 goto err;
1002 break;
1003 }
1004 }
1005
1006 return 0;
1007
1008err:
Inki Dae2a3098f2012-11-04 05:48:52 -08001009 dev_err(dev, "Bad register offset: 0x%lx\n", cmdlist->data[index]);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001010 return -EINVAL;
1011}
1012
1013/* ioctl functions */
1014int exynos_g2d_get_ver_ioctl(struct drm_device *drm_dev, void *data,
1015 struct drm_file *file)
1016{
Tobias Jakobief7ce052014-07-23 16:57:13 +02001017 struct drm_exynos_file_private *file_priv = file->driver_priv;
1018 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
1019 struct device *dev;
1020 struct g2d_data *g2d;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001021 struct drm_exynos_g2d_get_ver *ver = data;
1022
Tobias Jakobief7ce052014-07-23 16:57:13 +02001023 if (!g2d_priv)
1024 return -ENODEV;
1025
1026 dev = g2d_priv->dev;
1027 if (!dev)
1028 return -ENODEV;
1029
1030 g2d = dev_get_drvdata(dev);
1031 if (!g2d)
1032 return -EFAULT;
1033
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001034 ver->major = G2D_HW_MAJOR_VER;
1035 ver->minor = G2D_HW_MINOR_VER;
1036
1037 return 0;
1038}
1039EXPORT_SYMBOL_GPL(exynos_g2d_get_ver_ioctl);
1040
1041int exynos_g2d_set_cmdlist_ioctl(struct drm_device *drm_dev, void *data,
1042 struct drm_file *file)
1043{
1044 struct drm_exynos_file_private *file_priv = file->driver_priv;
1045 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
Tobias Jakobi1cd1ea52014-07-23 16:57:12 +02001046 struct device *dev;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001047 struct g2d_data *g2d;
1048 struct drm_exynos_g2d_set_cmdlist *req = data;
1049 struct drm_exynos_g2d_cmd *cmd;
1050 struct drm_exynos_pending_g2d_event *e;
1051 struct g2d_cmdlist_node *node;
1052 struct g2d_cmdlist *cmdlist;
1053 unsigned long flags;
1054 int size;
1055 int ret;
1056
Tobias Jakobi1cd1ea52014-07-23 16:57:12 +02001057 if (!g2d_priv)
1058 return -ENODEV;
1059
1060 dev = g2d_priv->dev;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001061 if (!dev)
1062 return -ENODEV;
1063
1064 g2d = dev_get_drvdata(dev);
1065 if (!g2d)
1066 return -EFAULT;
1067
1068 node = g2d_get_cmdlist(g2d);
1069 if (!node)
1070 return -ENOMEM;
1071
1072 node->event = NULL;
1073
1074 if (req->event_type != G2D_EVENT_NOT) {
1075 spin_lock_irqsave(&drm_dev->event_lock, flags);
1076 if (file->event_space < sizeof(e->event)) {
1077 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
1078 ret = -ENOMEM;
1079 goto err;
1080 }
1081 file->event_space -= sizeof(e->event);
1082 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
1083
1084 e = kzalloc(sizeof(*node->event), GFP_KERNEL);
1085 if (!e) {
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001086 spin_lock_irqsave(&drm_dev->event_lock, flags);
1087 file->event_space += sizeof(e->event);
1088 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
1089
1090 ret = -ENOMEM;
1091 goto err;
1092 }
1093
1094 e->event.base.type = DRM_EXYNOS_G2D_EVENT;
1095 e->event.base.length = sizeof(e->event);
1096 e->event.user_data = req->user_data;
1097 e->base.event = &e->event.base;
1098 e->base.file_priv = file;
1099 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1100
1101 node->event = e;
1102 }
1103
1104 cmdlist = node->cmdlist;
1105
1106 cmdlist->last = 0;
1107
1108 /*
1109 * If don't clear SFR registers, the cmdlist is affected by register
1110 * values of previous cmdlist. G2D hw executes SFR clear command and
1111 * a next command at the same time then the next command is ignored and
1112 * is executed rightly from next next command, so needs a dummy command
1113 * to next command of SFR clear command.
1114 */
1115 cmdlist->data[cmdlist->last++] = G2D_SOFT_RESET;
1116 cmdlist->data[cmdlist->last++] = G2D_SFRCLEAR;
1117 cmdlist->data[cmdlist->last++] = G2D_SRC_BASE_ADDR;
1118 cmdlist->data[cmdlist->last++] = 0;
1119
YoungJun Cho7ad01812013-03-13 16:44:37 +09001120 /*
1121 * 'LIST_HOLD' command should be set to the DMA_HOLD_CMD_REG
1122 * and GCF bit should be set to INTEN register if user wants
1123 * G2D interrupt event once current command list execution is
1124 * finished.
1125 * Otherwise only ACF bit should be set to INTEN register so
Masanari Iidac6b78bc2013-10-24 16:02:57 +09001126 * that one interrupt is occurred after all command lists
YoungJun Cho7ad01812013-03-13 16:44:37 +09001127 * have been completed.
1128 */
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001129 if (node->event) {
YoungJun Cho7ad01812013-03-13 16:44:37 +09001130 cmdlist->data[cmdlist->last++] = G2D_INTEN;
1131 cmdlist->data[cmdlist->last++] = G2D_INTEN_ACF | G2D_INTEN_GCF;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001132 cmdlist->data[cmdlist->last++] = G2D_DMA_HOLD_CMD;
1133 cmdlist->data[cmdlist->last++] = G2D_LIST_HOLD;
YoungJun Cho7ad01812013-03-13 16:44:37 +09001134 } else {
1135 cmdlist->data[cmdlist->last++] = G2D_INTEN;
1136 cmdlist->data[cmdlist->last++] = G2D_INTEN_ACF;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001137 }
1138
1139 /* Check size of cmdlist: last 2 is about G2D_BITBLT_START */
Inki Dae2a3098f2012-11-04 05:48:52 -08001140 size = cmdlist->last + req->cmd_nr * 2 + req->cmd_buf_nr * 2 + 2;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001141 if (size > G2D_CMDLIST_DATA_NUM) {
1142 dev_err(dev, "cmdlist size is too big\n");
1143 ret = -EINVAL;
1144 goto err_free_event;
1145 }
1146
1147 cmd = (struct drm_exynos_g2d_cmd *)(uint32_t)req->cmd;
1148
1149 if (copy_from_user(cmdlist->data + cmdlist->last,
1150 (void __user *)cmd,
1151 sizeof(*cmd) * req->cmd_nr)) {
1152 ret = -EFAULT;
1153 goto err_free_event;
1154 }
1155 cmdlist->last += req->cmd_nr * 2;
1156
Inki Dae2a3098f2012-11-04 05:48:52 -08001157 ret = g2d_check_reg_offset(dev, node, req->cmd_nr, false);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001158 if (ret < 0)
1159 goto err_free_event;
1160
YoungJun Cho9963cb62013-03-13 17:10:08 +09001161 node->buf_info.map_nr = req->cmd_buf_nr;
Inki Dae2a3098f2012-11-04 05:48:52 -08001162 if (req->cmd_buf_nr) {
1163 struct drm_exynos_g2d_cmd *cmd_buf;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001164
Inki Dae2a3098f2012-11-04 05:48:52 -08001165 cmd_buf = (struct drm_exynos_g2d_cmd *)(uint32_t)req->cmd_buf;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001166
1167 if (copy_from_user(cmdlist->data + cmdlist->last,
Inki Dae2a3098f2012-11-04 05:48:52 -08001168 (void __user *)cmd_buf,
1169 sizeof(*cmd_buf) * req->cmd_buf_nr)) {
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001170 ret = -EFAULT;
1171 goto err_free_event;
1172 }
Inki Dae2a3098f2012-11-04 05:48:52 -08001173 cmdlist->last += req->cmd_buf_nr * 2;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001174
Inki Dae2a3098f2012-11-04 05:48:52 -08001175 ret = g2d_check_reg_offset(dev, node, req->cmd_buf_nr, true);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001176 if (ret < 0)
1177 goto err_free_event;
1178
Inki Daed87342c2012-11-03 21:53:24 -07001179 ret = g2d_map_cmdlist_gem(g2d, node, drm_dev, file);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001180 if (ret < 0)
1181 goto err_unmap;
1182 }
1183
1184 cmdlist->data[cmdlist->last++] = G2D_BITBLT_START;
1185 cmdlist->data[cmdlist->last++] = G2D_START_BITBLT;
1186
1187 /* head */
1188 cmdlist->head = cmdlist->last / 2;
1189
1190 /* tail */
1191 cmdlist->data[cmdlist->last] = 0;
1192
1193 g2d_add_cmdlist_to_inuse(g2d_priv, node);
1194
1195 return 0;
1196
1197err_unmap:
Inki Daed87342c2012-11-03 21:53:24 -07001198 g2d_unmap_cmdlist_gem(g2d, node, file);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001199err_free_event:
1200 if (node->event) {
1201 spin_lock_irqsave(&drm_dev->event_lock, flags);
1202 file->event_space += sizeof(e->event);
1203 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
1204 kfree(node->event);
1205 }
1206err:
1207 g2d_put_cmdlist(g2d, node);
1208 return ret;
1209}
1210EXPORT_SYMBOL_GPL(exynos_g2d_set_cmdlist_ioctl);
1211
1212int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data,
1213 struct drm_file *file)
1214{
1215 struct drm_exynos_file_private *file_priv = file->driver_priv;
1216 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
Tobias Jakobi1cd1ea52014-07-23 16:57:12 +02001217 struct device *dev;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001218 struct g2d_data *g2d;
1219 struct drm_exynos_g2d_exec *req = data;
1220 struct g2d_runqueue_node *runqueue_node;
1221 struct list_head *run_cmdlist;
1222 struct list_head *event_list;
1223
Tobias Jakobi1cd1ea52014-07-23 16:57:12 +02001224 if (!g2d_priv)
1225 return -ENODEV;
1226
1227 dev = g2d_priv->dev;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001228 if (!dev)
1229 return -ENODEV;
1230
1231 g2d = dev_get_drvdata(dev);
1232 if (!g2d)
1233 return -EFAULT;
1234
1235 runqueue_node = kmem_cache_alloc(g2d->runqueue_slab, GFP_KERNEL);
1236 if (!runqueue_node) {
1237 dev_err(dev, "failed to allocate memory\n");
1238 return -ENOMEM;
1239 }
1240 run_cmdlist = &runqueue_node->run_cmdlist;
1241 event_list = &runqueue_node->event_list;
1242 INIT_LIST_HEAD(run_cmdlist);
1243 INIT_LIST_HEAD(event_list);
1244 init_completion(&runqueue_node->complete);
1245 runqueue_node->async = req->async;
1246
1247 list_splice_init(&g2d_priv->inuse_cmdlist, run_cmdlist);
1248 list_splice_init(&g2d_priv->event_list, event_list);
1249
1250 if (list_empty(run_cmdlist)) {
1251 dev_err(dev, "there is no inuse cmdlist\n");
1252 kmem_cache_free(g2d->runqueue_slab, runqueue_node);
1253 return -EPERM;
1254 }
1255
1256 mutex_lock(&g2d->runqueue_mutex);
Inki Dae6b6bae22012-09-11 10:45:36 +09001257 runqueue_node->pid = current->pid;
Inki Daed87342c2012-11-03 21:53:24 -07001258 runqueue_node->filp = file;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001259 list_add_tail(&runqueue_node->list, &g2d->runqueue);
1260 if (!g2d->runqueue_node)
1261 g2d_exec_runqueue(g2d);
1262 mutex_unlock(&g2d->runqueue_mutex);
1263
1264 if (runqueue_node->async)
1265 goto out;
1266
1267 wait_for_completion(&runqueue_node->complete);
1268 g2d_free_runqueue_node(g2d, runqueue_node);
1269
1270out:
1271 return 0;
1272}
1273EXPORT_SYMBOL_GPL(exynos_g2d_exec_ioctl);
1274
Inki Daed87342c2012-11-03 21:53:24 -07001275static int g2d_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1276{
1277 struct g2d_data *g2d;
1278 int ret;
1279
1280 g2d = dev_get_drvdata(dev);
1281 if (!g2d)
1282 return -EFAULT;
1283
1284 /* allocate dma-aware cmdlist buffer. */
1285 ret = g2d_init_cmdlist(g2d);
1286 if (ret < 0) {
1287 dev_err(dev, "cmdlist init failed\n");
1288 return ret;
1289 }
1290
1291 if (!is_drm_iommu_supported(drm_dev))
1292 return 0;
1293
1294 ret = drm_iommu_attach_device(drm_dev, dev);
1295 if (ret < 0) {
1296 dev_err(dev, "failed to enable iommu.\n");
1297 g2d_fini_cmdlist(g2d);
1298 }
1299
1300 return ret;
1301
1302}
1303
1304static void g2d_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1305{
1306 if (!is_drm_iommu_supported(drm_dev))
1307 return;
1308
1309 drm_iommu_detach_device(drm_dev, dev);
1310}
1311
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001312static int g2d_open(struct drm_device *drm_dev, struct device *dev,
1313 struct drm_file *file)
1314{
1315 struct drm_exynos_file_private *file_priv = file->driver_priv;
1316 struct exynos_drm_g2d_private *g2d_priv;
1317
1318 g2d_priv = kzalloc(sizeof(*g2d_priv), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +09001319 if (!g2d_priv)
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001320 return -ENOMEM;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001321
1322 g2d_priv->dev = dev;
1323 file_priv->g2d_priv = g2d_priv;
1324
1325 INIT_LIST_HEAD(&g2d_priv->inuse_cmdlist);
1326 INIT_LIST_HEAD(&g2d_priv->event_list);
Inki Dae2a3098f2012-11-04 05:48:52 -08001327 INIT_LIST_HEAD(&g2d_priv->userptr_list);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001328
1329 return 0;
1330}
1331
1332static void g2d_close(struct drm_device *drm_dev, struct device *dev,
1333 struct drm_file *file)
1334{
1335 struct drm_exynos_file_private *file_priv = file->driver_priv;
1336 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
1337 struct g2d_data *g2d;
1338 struct g2d_cmdlist_node *node, *n;
1339
1340 if (!dev)
1341 return;
1342
1343 g2d = dev_get_drvdata(dev);
1344 if (!g2d)
1345 return;
1346
1347 mutex_lock(&g2d->cmdlist_mutex);
Inki Daed87342c2012-11-03 21:53:24 -07001348 list_for_each_entry_safe(node, n, &g2d_priv->inuse_cmdlist, list) {
1349 /*
1350 * unmap all gem objects not completed.
1351 *
1352 * P.S. if current process was terminated forcely then
1353 * there may be some commands in inuse_cmdlist so unmap
1354 * them.
1355 */
1356 g2d_unmap_cmdlist_gem(g2d, node, file);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001357 list_move_tail(&node->list, &g2d->free_cmdlist);
Inki Daed87342c2012-11-03 21:53:24 -07001358 }
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001359 mutex_unlock(&g2d->cmdlist_mutex);
1360
Inki Dae2a3098f2012-11-04 05:48:52 -08001361 /* release all g2d_userptr in pool. */
1362 g2d_userptr_free_all(drm_dev, g2d, file);
1363
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001364 kfree(file_priv->g2d_priv);
1365}
1366
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001367static int g2d_probe(struct platform_device *pdev)
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001368{
1369 struct device *dev = &pdev->dev;
1370 struct resource *res;
1371 struct g2d_data *g2d;
1372 struct exynos_drm_subdrv *subdrv;
1373 int ret;
1374
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001375 g2d = devm_kzalloc(dev, sizeof(*g2d), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +09001376 if (!g2d)
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001377 return -ENOMEM;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001378
1379 g2d->runqueue_slab = kmem_cache_create("g2d_runqueue_slab",
1380 sizeof(struct g2d_runqueue_node), 0, 0, NULL);
Sachin Kamatb7675932012-08-06 12:16:20 +05301381 if (!g2d->runqueue_slab)
1382 return -ENOMEM;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001383
1384 g2d->dev = dev;
1385
1386 g2d->g2d_workq = create_singlethread_workqueue("g2d");
1387 if (!g2d->g2d_workq) {
1388 dev_err(dev, "failed to create workqueue\n");
1389 ret = -EINVAL;
1390 goto err_destroy_slab;
1391 }
1392
1393 INIT_WORK(&g2d->runqueue_work, g2d_runqueue_worker);
1394 INIT_LIST_HEAD(&g2d->free_cmdlist);
1395 INIT_LIST_HEAD(&g2d->runqueue);
1396
1397 mutex_init(&g2d->cmdlist_mutex);
1398 mutex_init(&g2d->runqueue_mutex);
1399
Sachin Kamatdc625532012-11-23 09:11:58 +05301400 g2d->gate_clk = devm_clk_get(dev, "fimg2d");
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001401 if (IS_ERR(g2d->gate_clk)) {
1402 dev_err(dev, "failed to get gate clock\n");
1403 ret = PTR_ERR(g2d->gate_clk);
Inki Daed87342c2012-11-03 21:53:24 -07001404 goto err_destroy_workqueue;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001405 }
1406
1407 pm_runtime_enable(dev);
1408
1409 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001410
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001411 g2d->regs = devm_ioremap_resource(dev, res);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001412 if (IS_ERR(g2d->regs)) {
1413 ret = PTR_ERR(g2d->regs);
Sachin Kamatb7675932012-08-06 12:16:20 +05301414 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001415 }
1416
1417 g2d->irq = platform_get_irq(pdev, 0);
1418 if (g2d->irq < 0) {
1419 dev_err(dev, "failed to get irq\n");
1420 ret = g2d->irq;
Sachin Kamatb7675932012-08-06 12:16:20 +05301421 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001422 }
1423
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001424 ret = devm_request_irq(dev, g2d->irq, g2d_irq_handler, 0,
Sachin Kamatb7675932012-08-06 12:16:20 +05301425 "drm_g2d", g2d);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001426 if (ret < 0) {
1427 dev_err(dev, "irq request failed\n");
Sachin Kamatb7675932012-08-06 12:16:20 +05301428 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001429 }
1430
Inki Dae2a3098f2012-11-04 05:48:52 -08001431 g2d->max_pool = MAX_POOL;
1432
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001433 platform_set_drvdata(pdev, g2d);
1434
1435 subdrv = &g2d->subdrv;
1436 subdrv->dev = dev;
Inki Daed87342c2012-11-03 21:53:24 -07001437 subdrv->probe = g2d_subdrv_probe;
1438 subdrv->remove = g2d_subdrv_remove;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001439 subdrv->open = g2d_open;
1440 subdrv->close = g2d_close;
1441
1442 ret = exynos_drm_subdrv_register(subdrv);
1443 if (ret < 0) {
1444 dev_err(dev, "failed to register drm g2d device\n");
Sachin Kamatb7675932012-08-06 12:16:20 +05301445 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001446 }
1447
1448 dev_info(dev, "The exynos g2d(ver %d.%d) successfully probed\n",
1449 G2D_HW_MAJOR_VER, G2D_HW_MINOR_VER);
1450
1451 return 0;
1452
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001453err_put_clk:
1454 pm_runtime_disable(dev);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001455err_destroy_workqueue:
1456 destroy_workqueue(g2d->g2d_workq);
1457err_destroy_slab:
1458 kmem_cache_destroy(g2d->runqueue_slab);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001459 return ret;
1460}
1461
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001462static int g2d_remove(struct platform_device *pdev)
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001463{
1464 struct g2d_data *g2d = platform_get_drvdata(pdev);
1465
1466 cancel_work_sync(&g2d->runqueue_work);
1467 exynos_drm_subdrv_unregister(&g2d->subdrv);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001468
1469 while (g2d->runqueue_node) {
1470 g2d_free_runqueue_node(g2d, g2d->runqueue_node);
1471 g2d->runqueue_node = g2d_get_runqueue_node(g2d);
1472 }
1473
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001474 pm_runtime_disable(&pdev->dev);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001475
1476 g2d_fini_cmdlist(g2d);
1477 destroy_workqueue(g2d->g2d_workq);
1478 kmem_cache_destroy(g2d->runqueue_slab);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001479
1480 return 0;
1481}
1482
1483#ifdef CONFIG_PM_SLEEP
1484static int g2d_suspend(struct device *dev)
1485{
1486 struct g2d_data *g2d = dev_get_drvdata(dev);
1487
1488 mutex_lock(&g2d->runqueue_mutex);
1489 g2d->suspended = true;
1490 mutex_unlock(&g2d->runqueue_mutex);
1491
1492 while (g2d->runqueue_node)
1493 /* FIXME: good range? */
1494 usleep_range(500, 1000);
1495
Tejun Heo43829732012-08-20 14:51:24 -07001496 flush_work(&g2d->runqueue_work);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001497
1498 return 0;
1499}
1500
1501static int g2d_resume(struct device *dev)
1502{
1503 struct g2d_data *g2d = dev_get_drvdata(dev);
1504
1505 g2d->suspended = false;
1506 g2d_exec_runqueue(g2d);
1507
1508 return 0;
1509}
1510#endif
1511
Rafael J. Wysocki06453ed2014-12-04 01:04:55 +01001512#ifdef CONFIG_PM
Inki Daeb10d6352013-07-24 15:44:30 +09001513static int g2d_runtime_suspend(struct device *dev)
1514{
1515 struct g2d_data *g2d = dev_get_drvdata(dev);
1516
1517 clk_disable_unprepare(g2d->gate_clk);
1518
1519 return 0;
1520}
1521
1522static int g2d_runtime_resume(struct device *dev)
1523{
1524 struct g2d_data *g2d = dev_get_drvdata(dev);
1525 int ret;
1526
1527 ret = clk_prepare_enable(g2d->gate_clk);
1528 if (ret < 0)
1529 dev_warn(dev, "failed to enable clock.\n");
1530
1531 return ret;
1532}
1533#endif
1534
1535static const struct dev_pm_ops g2d_pm_ops = {
1536 SET_SYSTEM_SLEEP_PM_OPS(g2d_suspend, g2d_resume)
1537 SET_RUNTIME_PM_OPS(g2d_runtime_suspend, g2d_runtime_resume, NULL)
1538};
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001539
Ajay Kumar95fc6332013-02-06 10:59:44 +05301540static const struct of_device_id exynos_g2d_match[] = {
1541 { .compatible = "samsung,exynos5250-g2d" },
Alban Browaeys6411fe32014-07-19 22:53:03 +02001542 { .compatible = "samsung,exynos4212-g2d" },
Ajay Kumar95fc6332013-02-06 10:59:44 +05301543 {},
1544};
Sjoerd Simons0262cee2014-07-30 11:28:31 +09001545MODULE_DEVICE_TABLE(of, exynos_g2d_match);
Ajay Kumar95fc6332013-02-06 10:59:44 +05301546
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001547struct platform_driver g2d_driver = {
1548 .probe = g2d_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001549 .remove = g2d_remove,
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001550 .driver = {
1551 .name = "s5p-g2d",
1552 .owner = THIS_MODULE,
1553 .pm = &g2d_pm_ops,
Sachin Kamat61c48fb2013-08-28 10:47:56 +05301554 .of_match_table = exynos_g2d_match,
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001555 },
1556};