blob: 1ff11443f55271d3702039b983092558edfb51f2 [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>
11#include <linux/module.h>
12#include <linux/clk.h>
13#include <linux/err.h>
14#include <linux/interrupt.h>
15#include <linux/io.h>
16#include <linux/platform_device.h>
17#include <linux/pm_runtime.h>
18#include <linux/slab.h>
19#include <linux/workqueue.h>
Inki Daed87342c2012-11-03 21:53:24 -070020#include <linux/dma-mapping.h>
21#include <linux/dma-attrs.h>
Ajay Kumar95fc6332013-02-06 10:59:44 +053022#include <linux/of.h>
Joonyoung Shimd7f16422012-05-17 20:06:32 +090023
David Howells760285e2012-10-02 18:01:07 +010024#include <drm/drmP.h>
25#include <drm/exynos_drm.h>
Joonyoung Shimd7f16422012-05-17 20:06:32 +090026#include "exynos_drm_drv.h"
27#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
51#define G2D_SRC_PLANE2_BASE_ADDR 0x0318
52#define G2D_DST_BASE_ADDR 0x0404
53#define G2D_DST_PLANE2_BASE_ADDR 0x0418
54#define G2D_PAT_BASE_ADDR 0x0500
55#define G2D_MSK_BASE_ADDR 0x0520
56
57/* G2D_SOFT_RESET */
58#define G2D_SFRCLEAR (1 << 1)
59#define G2D_R (1 << 0)
60
61/* G2D_INTEN */
62#define G2D_INTEN_ACF (1 << 3)
63#define G2D_INTEN_UCF (1 << 2)
64#define G2D_INTEN_GCF (1 << 1)
65#define G2D_INTEN_SCF (1 << 0)
66
67/* G2D_INTC_PEND */
68#define G2D_INTP_ACMD_FIN (1 << 3)
69#define G2D_INTP_UCMD_FIN (1 << 2)
70#define G2D_INTP_GCMD_FIN (1 << 1)
71#define G2D_INTP_SCMD_FIN (1 << 0)
72
73/* G2D_DMA_COMMAND */
74#define G2D_DMA_HALT (1 << 2)
75#define G2D_DMA_CONTINUE (1 << 1)
76#define G2D_DMA_START (1 << 0)
77
78/* G2D_DMA_STATUS */
79#define G2D_DMA_LIST_DONE_COUNT (0xFF << 17)
80#define G2D_DMA_BITBLT_DONE_COUNT (0xFFFF << 1)
81#define G2D_DMA_DONE (1 << 0)
82#define G2D_DMA_LIST_DONE_COUNT_OFFSET 17
83
84/* G2D_DMA_HOLD_CMD */
YoungJun Cho7ad01812013-03-13 16:44:37 +090085#define G2D_USER_HOLD (1 << 2)
Joonyoung Shimd7f16422012-05-17 20:06:32 +090086#define G2D_LIST_HOLD (1 << 1)
87#define G2D_BITBLT_HOLD (1 << 0)
88
89/* G2D_BITBLT_START */
90#define G2D_START_CASESEL (1 << 2)
91#define G2D_START_NHOLT (1 << 1)
92#define G2D_START_BITBLT (1 << 0)
93
94#define G2D_CMDLIST_SIZE (PAGE_SIZE / 4)
95#define G2D_CMDLIST_NUM 64
96#define G2D_CMDLIST_POOL_SIZE (G2D_CMDLIST_SIZE * G2D_CMDLIST_NUM)
97#define G2D_CMDLIST_DATA_NUM (G2D_CMDLIST_SIZE / sizeof(u32) - 2)
98
Inki Daed87342c2012-11-03 21:53:24 -070099#define MAX_BUF_ADDR_NR 6
100
Inki Dae2a3098f2012-11-04 05:48:52 -0800101/* maximum buffer pool size of userptr is 64MB as default */
102#define MAX_POOL (64 * 1024 * 1024)
103
104enum {
105 BUF_TYPE_GEM = 1,
106 BUF_TYPE_USERPTR,
107};
108
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900109/* cmdlist data structure */
110struct g2d_cmdlist {
Inki Dae2a3098f2012-11-04 05:48:52 -0800111 u32 head;
112 unsigned long data[G2D_CMDLIST_DATA_NUM];
113 u32 last; /* last data offset */
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900114};
115
116struct drm_exynos_pending_g2d_event {
117 struct drm_pending_event base;
118 struct drm_exynos_g2d_event event;
119};
120
Inki Dae2a3098f2012-11-04 05:48:52 -0800121struct g2d_cmdlist_userptr {
122 struct list_head list;
123 dma_addr_t dma_addr;
124 unsigned long userptr;
125 unsigned long size;
126 struct page **pages;
127 unsigned int npages;
128 struct sg_table *sgt;
129 struct vm_area_struct *vma;
130 atomic_t refcount;
131 bool in_pool;
132 bool out_of_list;
133};
134
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900135struct g2d_cmdlist_node {
136 struct list_head list;
137 struct g2d_cmdlist *cmdlist;
Inki Daed87342c2012-11-03 21:53:24 -0700138 unsigned int map_nr;
Inki Dae2a3098f2012-11-04 05:48:52 -0800139 unsigned long handles[MAX_BUF_ADDR_NR];
140 unsigned int obj_type[MAX_BUF_ADDR_NR];
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900141 dma_addr_t dma_addr;
142
143 struct drm_exynos_pending_g2d_event *event;
144};
145
146struct g2d_runqueue_node {
147 struct list_head list;
148 struct list_head run_cmdlist;
149 struct list_head event_list;
Inki Daed87342c2012-11-03 21:53:24 -0700150 struct drm_file *filp;
Inki Dae6b6bae22012-09-11 10:45:36 +0900151 pid_t pid;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900152 struct completion complete;
153 int async;
154};
155
156struct g2d_data {
157 struct device *dev;
158 struct clk *gate_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900159 void __iomem *regs;
160 int irq;
161 struct workqueue_struct *g2d_workq;
162 struct work_struct runqueue_work;
163 struct exynos_drm_subdrv subdrv;
164 bool suspended;
165
166 /* cmdlist */
167 struct g2d_cmdlist_node *cmdlist_node;
168 struct list_head free_cmdlist;
169 struct mutex cmdlist_mutex;
170 dma_addr_t cmdlist_pool;
171 void *cmdlist_pool_virt;
Inki Daed87342c2012-11-03 21:53:24 -0700172 struct dma_attrs cmdlist_dma_attrs;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900173
174 /* runqueue*/
175 struct g2d_runqueue_node *runqueue_node;
176 struct list_head runqueue;
177 struct mutex runqueue_mutex;
178 struct kmem_cache *runqueue_slab;
Inki Dae2a3098f2012-11-04 05:48:52 -0800179
180 unsigned long current_pool;
181 unsigned long max_pool;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900182};
183
184static int g2d_init_cmdlist(struct g2d_data *g2d)
185{
186 struct device *dev = g2d->dev;
187 struct g2d_cmdlist_node *node = g2d->cmdlist_node;
Inki Daed87342c2012-11-03 21:53:24 -0700188 struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900189 int nr;
190 int ret;
191
Inki Daed87342c2012-11-03 21:53:24 -0700192 init_dma_attrs(&g2d->cmdlist_dma_attrs);
193 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &g2d->cmdlist_dma_attrs);
194
195 g2d->cmdlist_pool_virt = dma_alloc_attrs(subdrv->drm_dev->dev,
196 G2D_CMDLIST_POOL_SIZE,
197 &g2d->cmdlist_pool, GFP_KERNEL,
198 &g2d->cmdlist_dma_attrs);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900199 if (!g2d->cmdlist_pool_virt) {
200 dev_err(dev, "failed to allocate dma memory\n");
201 return -ENOMEM;
202 }
203
Joonyoung Shimfab9f8d2012-09-27 19:26:03 +0900204 node = kcalloc(G2D_CMDLIST_NUM, sizeof(*node), GFP_KERNEL);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900205 if (!node) {
206 dev_err(dev, "failed to allocate memory\n");
207 ret = -ENOMEM;
208 goto err;
209 }
210
211 for (nr = 0; nr < G2D_CMDLIST_NUM; nr++) {
212 node[nr].cmdlist =
213 g2d->cmdlist_pool_virt + nr * G2D_CMDLIST_SIZE;
214 node[nr].dma_addr =
215 g2d->cmdlist_pool + nr * G2D_CMDLIST_SIZE;
216
217 list_add_tail(&node[nr].list, &g2d->free_cmdlist);
218 }
219
220 return 0;
221
222err:
Inki Daed87342c2012-11-03 21:53:24 -0700223 dma_free_attrs(subdrv->drm_dev->dev, G2D_CMDLIST_POOL_SIZE,
224 g2d->cmdlist_pool_virt,
225 g2d->cmdlist_pool, &g2d->cmdlist_dma_attrs);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900226 return ret;
227}
228
229static void g2d_fini_cmdlist(struct g2d_data *g2d)
230{
Inki Daed87342c2012-11-03 21:53:24 -0700231 struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900232
233 kfree(g2d->cmdlist_node);
Inki Daed87342c2012-11-03 21:53:24 -0700234 dma_free_attrs(subdrv->drm_dev->dev, G2D_CMDLIST_POOL_SIZE,
235 g2d->cmdlist_pool_virt,
236 g2d->cmdlist_pool, &g2d->cmdlist_dma_attrs);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900237}
238
239static struct g2d_cmdlist_node *g2d_get_cmdlist(struct g2d_data *g2d)
240{
241 struct device *dev = g2d->dev;
242 struct g2d_cmdlist_node *node;
243
244 mutex_lock(&g2d->cmdlist_mutex);
245 if (list_empty(&g2d->free_cmdlist)) {
246 dev_err(dev, "there is no free cmdlist\n");
247 mutex_unlock(&g2d->cmdlist_mutex);
248 return NULL;
249 }
250
251 node = list_first_entry(&g2d->free_cmdlist, struct g2d_cmdlist_node,
252 list);
253 list_del_init(&node->list);
254 mutex_unlock(&g2d->cmdlist_mutex);
255
256 return node;
257}
258
259static void g2d_put_cmdlist(struct g2d_data *g2d, struct g2d_cmdlist_node *node)
260{
261 mutex_lock(&g2d->cmdlist_mutex);
262 list_move_tail(&node->list, &g2d->free_cmdlist);
263 mutex_unlock(&g2d->cmdlist_mutex);
264}
265
266static void g2d_add_cmdlist_to_inuse(struct exynos_drm_g2d_private *g2d_priv,
267 struct g2d_cmdlist_node *node)
268{
269 struct g2d_cmdlist_node *lnode;
270
271 if (list_empty(&g2d_priv->inuse_cmdlist))
272 goto add_to_list;
273
274 /* this links to base address of new cmdlist */
275 lnode = list_entry(g2d_priv->inuse_cmdlist.prev,
276 struct g2d_cmdlist_node, list);
277 lnode->cmdlist->data[lnode->cmdlist->last] = node->dma_addr;
278
279add_to_list:
280 list_add_tail(&node->list, &g2d_priv->inuse_cmdlist);
281
282 if (node->event)
283 list_add_tail(&node->event->base.link, &g2d_priv->event_list);
284}
285
Inki Dae2a3098f2012-11-04 05:48:52 -0800286static void g2d_userptr_put_dma_addr(struct drm_device *drm_dev,
287 unsigned long obj,
288 bool force)
289{
290 struct g2d_cmdlist_userptr *g2d_userptr =
291 (struct g2d_cmdlist_userptr *)obj;
292
293 if (!obj)
294 return;
295
296 if (force)
297 goto out;
298
299 atomic_dec(&g2d_userptr->refcount);
300
301 if (atomic_read(&g2d_userptr->refcount) > 0)
302 return;
303
304 if (g2d_userptr->in_pool)
305 return;
306
307out:
308 exynos_gem_unmap_sgt_from_dma(drm_dev, g2d_userptr->sgt,
309 DMA_BIDIRECTIONAL);
310
311 exynos_gem_put_pages_to_userptr(g2d_userptr->pages,
312 g2d_userptr->npages,
313 g2d_userptr->vma);
314
315 if (!g2d_userptr->out_of_list)
316 list_del_init(&g2d_userptr->list);
317
318 sg_free_table(g2d_userptr->sgt);
319 kfree(g2d_userptr->sgt);
320 g2d_userptr->sgt = NULL;
321
322 kfree(g2d_userptr->pages);
Inki Dae2a3098f2012-11-04 05:48:52 -0800323 g2d_userptr->pages = NULL;
Sachin Kamatdf3d90e2012-11-23 09:11:59 +0530324 kfree(g2d_userptr);
Inki Dae2a3098f2012-11-04 05:48:52 -0800325 g2d_userptr = NULL;
326}
327
Sachin Kamatb7848c72013-01-14 12:29:09 +0530328static dma_addr_t *g2d_userptr_get_dma_addr(struct drm_device *drm_dev,
Inki Dae2a3098f2012-11-04 05:48:52 -0800329 unsigned long userptr,
330 unsigned long size,
331 struct drm_file *filp,
332 unsigned long *obj)
333{
334 struct drm_exynos_file_private *file_priv = filp->driver_priv;
335 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
336 struct g2d_cmdlist_userptr *g2d_userptr;
337 struct g2d_data *g2d;
338 struct page **pages;
339 struct sg_table *sgt;
340 struct vm_area_struct *vma;
341 unsigned long start, end;
342 unsigned int npages, offset;
343 int ret;
344
345 if (!size) {
346 DRM_ERROR("invalid userptr size.\n");
347 return ERR_PTR(-EINVAL);
348 }
349
350 g2d = dev_get_drvdata(g2d_priv->dev);
351
352 /* check if userptr already exists in userptr_list. */
353 list_for_each_entry(g2d_userptr, &g2d_priv->userptr_list, list) {
354 if (g2d_userptr->userptr == userptr) {
355 /*
356 * also check size because there could be same address
357 * and different size.
358 */
359 if (g2d_userptr->size == size) {
360 atomic_inc(&g2d_userptr->refcount);
361 *obj = (unsigned long)g2d_userptr;
362
363 return &g2d_userptr->dma_addr;
364 }
365
366 /*
367 * at this moment, maybe g2d dma is accessing this
368 * g2d_userptr memory region so just remove this
369 * g2d_userptr object from userptr_list not to be
370 * referred again and also except it the userptr
371 * pool to be released after the dma access completion.
372 */
373 g2d_userptr->out_of_list = true;
374 g2d_userptr->in_pool = false;
375 list_del_init(&g2d_userptr->list);
376
377 break;
378 }
379 }
380
381 g2d_userptr = kzalloc(sizeof(*g2d_userptr), GFP_KERNEL);
382 if (!g2d_userptr) {
383 DRM_ERROR("failed to allocate g2d_userptr.\n");
384 return ERR_PTR(-ENOMEM);
385 }
386
387 atomic_set(&g2d_userptr->refcount, 1);
388
389 start = userptr & PAGE_MASK;
390 offset = userptr & ~PAGE_MASK;
391 end = PAGE_ALIGN(userptr + size);
392 npages = (end - start) >> PAGE_SHIFT;
393 g2d_userptr->npages = npages;
394
395 pages = kzalloc(npages * sizeof(struct page *), GFP_KERNEL);
396 if (!pages) {
397 DRM_ERROR("failed to allocate pages.\n");
398 kfree(g2d_userptr);
399 return ERR_PTR(-ENOMEM);
400 }
401
402 vma = find_vma(current->mm, userptr);
403 if (!vma) {
404 DRM_ERROR("failed to get vm region.\n");
405 ret = -EFAULT;
406 goto err_free_pages;
407 }
408
409 if (vma->vm_end < userptr + size) {
410 DRM_ERROR("vma is too small.\n");
411 ret = -EFAULT;
412 goto err_free_pages;
413 }
414
415 g2d_userptr->vma = exynos_gem_get_vma(vma);
416 if (!g2d_userptr->vma) {
417 DRM_ERROR("failed to copy vma.\n");
418 ret = -ENOMEM;
419 goto err_free_pages;
420 }
421
422 g2d_userptr->size = size;
423
424 ret = exynos_gem_get_pages_from_userptr(start & PAGE_MASK,
425 npages, pages, vma);
426 if (ret < 0) {
427 DRM_ERROR("failed to get user pages from userptr.\n");
428 goto err_put_vma;
429 }
430
431 g2d_userptr->pages = pages;
432
Sachin Kamate44a5c02013-01-25 14:45:42 +0530433 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
Inki Dae2a3098f2012-11-04 05:48:52 -0800434 if (!sgt) {
435 DRM_ERROR("failed to allocate sg table.\n");
436 ret = -ENOMEM;
437 goto err_free_userptr;
438 }
439
440 ret = sg_alloc_table_from_pages(sgt, pages, npages, offset,
441 size, GFP_KERNEL);
442 if (ret < 0) {
443 DRM_ERROR("failed to get sgt from pages.\n");
444 goto err_free_sgt;
445 }
446
447 g2d_userptr->sgt = sgt;
448
449 ret = exynos_gem_map_sgt_with_dma(drm_dev, g2d_userptr->sgt,
450 DMA_BIDIRECTIONAL);
451 if (ret < 0) {
452 DRM_ERROR("failed to map sgt with dma region.\n");
YoungJun Cho067ed332013-03-11 19:48:05 +0900453 goto err_sg_free_table;
Inki Dae2a3098f2012-11-04 05:48:52 -0800454 }
455
456 g2d_userptr->dma_addr = sgt->sgl[0].dma_address;
457 g2d_userptr->userptr = userptr;
458
459 list_add_tail(&g2d_userptr->list, &g2d_priv->userptr_list);
460
461 if (g2d->current_pool + (npages << PAGE_SHIFT) < g2d->max_pool) {
462 g2d->current_pool += npages << PAGE_SHIFT;
463 g2d_userptr->in_pool = true;
464 }
465
466 *obj = (unsigned long)g2d_userptr;
467
468 return &g2d_userptr->dma_addr;
469
YoungJun Cho067ed332013-03-11 19:48:05 +0900470err_sg_free_table:
Inki Dae2a3098f2012-11-04 05:48:52 -0800471 sg_free_table(sgt);
YoungJun Cho067ed332013-03-11 19:48:05 +0900472
473err_free_sgt:
Inki Dae2a3098f2012-11-04 05:48:52 -0800474 kfree(sgt);
475 sgt = NULL;
476
477err_free_userptr:
478 exynos_gem_put_pages_to_userptr(g2d_userptr->pages,
479 g2d_userptr->npages,
480 g2d_userptr->vma);
481
482err_put_vma:
483 exynos_gem_put_vma(g2d_userptr->vma);
484
485err_free_pages:
486 kfree(pages);
487 kfree(g2d_userptr);
488 pages = NULL;
489 g2d_userptr = NULL;
490
491 return ERR_PTR(ret);
492}
493
494static void g2d_userptr_free_all(struct drm_device *drm_dev,
495 struct g2d_data *g2d,
496 struct drm_file *filp)
497{
498 struct drm_exynos_file_private *file_priv = filp->driver_priv;
499 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
500 struct g2d_cmdlist_userptr *g2d_userptr, *n;
501
502 list_for_each_entry_safe(g2d_userptr, n, &g2d_priv->userptr_list, list)
503 if (g2d_userptr->in_pool)
504 g2d_userptr_put_dma_addr(drm_dev,
505 (unsigned long)g2d_userptr,
506 true);
507
508 g2d->current_pool = 0;
509}
510
Inki Daed87342c2012-11-03 21:53:24 -0700511static int g2d_map_cmdlist_gem(struct g2d_data *g2d,
512 struct g2d_cmdlist_node *node,
513 struct drm_device *drm_dev,
514 struct drm_file *file)
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900515{
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900516 struct g2d_cmdlist *cmdlist = node->cmdlist;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900517 int offset;
518 int i;
519
Inki Daed87342c2012-11-03 21:53:24 -0700520 for (i = 0; i < node->map_nr; i++) {
521 unsigned long handle;
522 dma_addr_t *addr;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900523
524 offset = cmdlist->last - (i * 2 + 1);
Inki Daed87342c2012-11-03 21:53:24 -0700525 handle = cmdlist->data[offset];
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900526
Inki Dae2a3098f2012-11-04 05:48:52 -0800527 if (node->obj_type[i] == BUF_TYPE_GEM) {
528 addr = exynos_drm_gem_get_dma_addr(drm_dev, handle,
529 file);
530 if (IS_ERR(addr)) {
531 node->map_nr = i;
532 return -EFAULT;
533 }
534 } else {
535 struct drm_exynos_g2d_userptr g2d_userptr;
536
537 if (copy_from_user(&g2d_userptr, (void __user *)handle,
538 sizeof(struct drm_exynos_g2d_userptr))) {
539 node->map_nr = i;
540 return -EFAULT;
541 }
542
543 addr = g2d_userptr_get_dma_addr(drm_dev,
544 g2d_userptr.userptr,
545 g2d_userptr.size,
546 file,
547 &handle);
548 if (IS_ERR(addr)) {
549 node->map_nr = i;
550 return -EFAULT;
551 }
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900552 }
553
554 cmdlist->data[offset] = *addr;
Inki Daed87342c2012-11-03 21:53:24 -0700555 node->handles[i] = handle;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900556 }
557
558 return 0;
559}
560
Inki Daed87342c2012-11-03 21:53:24 -0700561static void g2d_unmap_cmdlist_gem(struct g2d_data *g2d,
562 struct g2d_cmdlist_node *node,
563 struct drm_file *filp)
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900564{
Inki Daed87342c2012-11-03 21:53:24 -0700565 struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
566 int i;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900567
Inki Daed87342c2012-11-03 21:53:24 -0700568 for (i = 0; i < node->map_nr; i++) {
Inki Dae2a3098f2012-11-04 05:48:52 -0800569 unsigned long handle = node->handles[i];
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900570
Inki Dae2a3098f2012-11-04 05:48:52 -0800571 if (node->obj_type[i] == BUF_TYPE_GEM)
572 exynos_drm_gem_put_dma_addr(subdrv->drm_dev, handle,
573 filp);
574 else
575 g2d_userptr_put_dma_addr(subdrv->drm_dev, handle,
576 false);
Inki Daed87342c2012-11-03 21:53:24 -0700577
578 node->handles[i] = 0;
YoungJun Cho5efc1d12013-03-11 19:56:17 +0900579 node->obj_type[i] = 0;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900580 }
Inki Daed87342c2012-11-03 21:53:24 -0700581
582 node->map_nr = 0;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900583}
584
585static void g2d_dma_start(struct g2d_data *g2d,
586 struct g2d_runqueue_node *runqueue_node)
587{
588 struct g2d_cmdlist_node *node =
589 list_first_entry(&runqueue_node->run_cmdlist,
590 struct g2d_cmdlist_node, list);
591
592 pm_runtime_get_sync(g2d->dev);
593 clk_enable(g2d->gate_clk);
594
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900595 writel_relaxed(node->dma_addr, g2d->regs + G2D_DMA_SFR_BASE_ADDR);
596 writel_relaxed(G2D_DMA_START, g2d->regs + G2D_DMA_COMMAND);
597}
598
599static struct g2d_runqueue_node *g2d_get_runqueue_node(struct g2d_data *g2d)
600{
601 struct g2d_runqueue_node *runqueue_node;
602
603 if (list_empty(&g2d->runqueue))
604 return NULL;
605
606 runqueue_node = list_first_entry(&g2d->runqueue,
607 struct g2d_runqueue_node, list);
608 list_del_init(&runqueue_node->list);
609 return runqueue_node;
610}
611
612static void g2d_free_runqueue_node(struct g2d_data *g2d,
613 struct g2d_runqueue_node *runqueue_node)
614{
Inki Daed87342c2012-11-03 21:53:24 -0700615 struct g2d_cmdlist_node *node;
616
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900617 if (!runqueue_node)
618 return;
619
620 mutex_lock(&g2d->cmdlist_mutex);
Inki Daed87342c2012-11-03 21:53:24 -0700621 /*
622 * commands in run_cmdlist have been completed so unmap all gem
623 * objects in each command node so that they are unreferenced.
624 */
625 list_for_each_entry(node, &runqueue_node->run_cmdlist, list)
626 g2d_unmap_cmdlist_gem(g2d, node, runqueue_node->filp);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900627 list_splice_tail_init(&runqueue_node->run_cmdlist, &g2d->free_cmdlist);
628 mutex_unlock(&g2d->cmdlist_mutex);
629
630 kmem_cache_free(g2d->runqueue_slab, runqueue_node);
631}
632
633static void g2d_exec_runqueue(struct g2d_data *g2d)
634{
635 g2d->runqueue_node = g2d_get_runqueue_node(g2d);
636 if (g2d->runqueue_node)
637 g2d_dma_start(g2d, g2d->runqueue_node);
638}
639
640static void g2d_runqueue_worker(struct work_struct *work)
641{
642 struct g2d_data *g2d = container_of(work, struct g2d_data,
643 runqueue_work);
644
645
646 mutex_lock(&g2d->runqueue_mutex);
647 clk_disable(g2d->gate_clk);
648 pm_runtime_put_sync(g2d->dev);
649
650 complete(&g2d->runqueue_node->complete);
651 if (g2d->runqueue_node->async)
652 g2d_free_runqueue_node(g2d, g2d->runqueue_node);
653
654 if (g2d->suspended)
655 g2d->runqueue_node = NULL;
656 else
657 g2d_exec_runqueue(g2d);
658 mutex_unlock(&g2d->runqueue_mutex);
659}
660
661static void g2d_finish_event(struct g2d_data *g2d, u32 cmdlist_no)
662{
663 struct drm_device *drm_dev = g2d->subdrv.drm_dev;
664 struct g2d_runqueue_node *runqueue_node = g2d->runqueue_node;
665 struct drm_exynos_pending_g2d_event *e;
666 struct timeval now;
667 unsigned long flags;
668
669 if (list_empty(&runqueue_node->event_list))
670 return;
671
672 e = list_first_entry(&runqueue_node->event_list,
673 struct drm_exynos_pending_g2d_event, base.link);
674
675 do_gettimeofday(&now);
676 e->event.tv_sec = now.tv_sec;
677 e->event.tv_usec = now.tv_usec;
678 e->event.cmdlist_no = cmdlist_no;
679
680 spin_lock_irqsave(&drm_dev->event_lock, flags);
681 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
682 wake_up_interruptible(&e->base.file_priv->event_wait);
683 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
684}
685
686static irqreturn_t g2d_irq_handler(int irq, void *dev_id)
687{
688 struct g2d_data *g2d = dev_id;
689 u32 pending;
690
691 pending = readl_relaxed(g2d->regs + G2D_INTC_PEND);
692 if (pending)
693 writel_relaxed(pending, g2d->regs + G2D_INTC_PEND);
694
695 if (pending & G2D_INTP_GCMD_FIN) {
696 u32 cmdlist_no = readl_relaxed(g2d->regs + G2D_DMA_STATUS);
697
698 cmdlist_no = (cmdlist_no & G2D_DMA_LIST_DONE_COUNT) >>
699 G2D_DMA_LIST_DONE_COUNT_OFFSET;
700
701 g2d_finish_event(g2d, cmdlist_no);
702
703 writel_relaxed(0, g2d->regs + G2D_DMA_HOLD_CMD);
704 if (!(pending & G2D_INTP_ACMD_FIN)) {
705 writel_relaxed(G2D_DMA_CONTINUE,
706 g2d->regs + G2D_DMA_COMMAND);
707 }
708 }
709
710 if (pending & G2D_INTP_ACMD_FIN)
711 queue_work(g2d->g2d_workq, &g2d->runqueue_work);
712
713 return IRQ_HANDLED;
714}
715
Inki Dae2a3098f2012-11-04 05:48:52 -0800716static int g2d_check_reg_offset(struct device *dev,
717 struct g2d_cmdlist_node *node,
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900718 int nr, bool for_addr)
719{
Inki Dae2a3098f2012-11-04 05:48:52 -0800720 struct g2d_cmdlist *cmdlist = node->cmdlist;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900721 int reg_offset;
722 int index;
723 int i;
724
725 for (i = 0; i < nr; i++) {
726 index = cmdlist->last - 2 * (i + 1);
Inki Dae2a3098f2012-11-04 05:48:52 -0800727
728 if (for_addr) {
729 /* check userptr buffer type. */
730 reg_offset = (cmdlist->data[index] &
731 ~0x7fffffff) >> 31;
732 if (reg_offset) {
733 node->obj_type[i] = BUF_TYPE_USERPTR;
734 cmdlist->data[index] &= ~G2D_BUF_USERPTR;
735 }
736 }
737
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900738 reg_offset = cmdlist->data[index] & ~0xfffff000;
739
740 if (reg_offset < G2D_VALID_START || reg_offset > G2D_VALID_END)
741 goto err;
742 if (reg_offset % 4)
743 goto err;
744
745 switch (reg_offset) {
746 case G2D_SRC_BASE_ADDR:
747 case G2D_SRC_PLANE2_BASE_ADDR:
748 case G2D_DST_BASE_ADDR:
749 case G2D_DST_PLANE2_BASE_ADDR:
750 case G2D_PAT_BASE_ADDR:
751 case G2D_MSK_BASE_ADDR:
752 if (!for_addr)
753 goto err;
Inki Dae2a3098f2012-11-04 05:48:52 -0800754
755 if (node->obj_type[i] != BUF_TYPE_USERPTR)
756 node->obj_type[i] = BUF_TYPE_GEM;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900757 break;
758 default:
759 if (for_addr)
760 goto err;
761 break;
762 }
763 }
764
765 return 0;
766
767err:
Inki Dae2a3098f2012-11-04 05:48:52 -0800768 dev_err(dev, "Bad register offset: 0x%lx\n", cmdlist->data[index]);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900769 return -EINVAL;
770}
771
772/* ioctl functions */
773int exynos_g2d_get_ver_ioctl(struct drm_device *drm_dev, void *data,
774 struct drm_file *file)
775{
776 struct drm_exynos_g2d_get_ver *ver = data;
777
778 ver->major = G2D_HW_MAJOR_VER;
779 ver->minor = G2D_HW_MINOR_VER;
780
781 return 0;
782}
783EXPORT_SYMBOL_GPL(exynos_g2d_get_ver_ioctl);
784
785int exynos_g2d_set_cmdlist_ioctl(struct drm_device *drm_dev, void *data,
786 struct drm_file *file)
787{
788 struct drm_exynos_file_private *file_priv = file->driver_priv;
789 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
790 struct device *dev = g2d_priv->dev;
791 struct g2d_data *g2d;
792 struct drm_exynos_g2d_set_cmdlist *req = data;
793 struct drm_exynos_g2d_cmd *cmd;
794 struct drm_exynos_pending_g2d_event *e;
795 struct g2d_cmdlist_node *node;
796 struct g2d_cmdlist *cmdlist;
797 unsigned long flags;
798 int size;
799 int ret;
800
801 if (!dev)
802 return -ENODEV;
803
804 g2d = dev_get_drvdata(dev);
805 if (!g2d)
806 return -EFAULT;
807
808 node = g2d_get_cmdlist(g2d);
809 if (!node)
810 return -ENOMEM;
811
812 node->event = NULL;
813
814 if (req->event_type != G2D_EVENT_NOT) {
815 spin_lock_irqsave(&drm_dev->event_lock, flags);
816 if (file->event_space < sizeof(e->event)) {
817 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
818 ret = -ENOMEM;
819 goto err;
820 }
821 file->event_space -= sizeof(e->event);
822 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
823
824 e = kzalloc(sizeof(*node->event), GFP_KERNEL);
825 if (!e) {
826 dev_err(dev, "failed to allocate event\n");
827
828 spin_lock_irqsave(&drm_dev->event_lock, flags);
829 file->event_space += sizeof(e->event);
830 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
831
832 ret = -ENOMEM;
833 goto err;
834 }
835
836 e->event.base.type = DRM_EXYNOS_G2D_EVENT;
837 e->event.base.length = sizeof(e->event);
838 e->event.user_data = req->user_data;
839 e->base.event = &e->event.base;
840 e->base.file_priv = file;
841 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
842
843 node->event = e;
844 }
845
846 cmdlist = node->cmdlist;
847
848 cmdlist->last = 0;
849
850 /*
851 * If don't clear SFR registers, the cmdlist is affected by register
852 * values of previous cmdlist. G2D hw executes SFR clear command and
853 * a next command at the same time then the next command is ignored and
854 * is executed rightly from next next command, so needs a dummy command
855 * to next command of SFR clear command.
856 */
857 cmdlist->data[cmdlist->last++] = G2D_SOFT_RESET;
858 cmdlist->data[cmdlist->last++] = G2D_SFRCLEAR;
859 cmdlist->data[cmdlist->last++] = G2D_SRC_BASE_ADDR;
860 cmdlist->data[cmdlist->last++] = 0;
861
YoungJun Cho7ad01812013-03-13 16:44:37 +0900862 /*
863 * 'LIST_HOLD' command should be set to the DMA_HOLD_CMD_REG
864 * and GCF bit should be set to INTEN register if user wants
865 * G2D interrupt event once current command list execution is
866 * finished.
867 * Otherwise only ACF bit should be set to INTEN register so
868 * that one interrupt is occured after all command lists
869 * have been completed.
870 */
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900871 if (node->event) {
YoungJun Cho7ad01812013-03-13 16:44:37 +0900872 cmdlist->data[cmdlist->last++] = G2D_INTEN;
873 cmdlist->data[cmdlist->last++] = G2D_INTEN_ACF | G2D_INTEN_GCF;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900874 cmdlist->data[cmdlist->last++] = G2D_DMA_HOLD_CMD;
875 cmdlist->data[cmdlist->last++] = G2D_LIST_HOLD;
YoungJun Cho7ad01812013-03-13 16:44:37 +0900876 } else {
877 cmdlist->data[cmdlist->last++] = G2D_INTEN;
878 cmdlist->data[cmdlist->last++] = G2D_INTEN_ACF;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900879 }
880
881 /* Check size of cmdlist: last 2 is about G2D_BITBLT_START */
Inki Dae2a3098f2012-11-04 05:48:52 -0800882 size = cmdlist->last + req->cmd_nr * 2 + req->cmd_buf_nr * 2 + 2;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900883 if (size > G2D_CMDLIST_DATA_NUM) {
884 dev_err(dev, "cmdlist size is too big\n");
885 ret = -EINVAL;
886 goto err_free_event;
887 }
888
889 cmd = (struct drm_exynos_g2d_cmd *)(uint32_t)req->cmd;
890
891 if (copy_from_user(cmdlist->data + cmdlist->last,
892 (void __user *)cmd,
893 sizeof(*cmd) * req->cmd_nr)) {
894 ret = -EFAULT;
895 goto err_free_event;
896 }
897 cmdlist->last += req->cmd_nr * 2;
898
Inki Dae2a3098f2012-11-04 05:48:52 -0800899 ret = g2d_check_reg_offset(dev, node, req->cmd_nr, false);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900900 if (ret < 0)
901 goto err_free_event;
902
Inki Dae2a3098f2012-11-04 05:48:52 -0800903 node->map_nr = req->cmd_buf_nr;
904 if (req->cmd_buf_nr) {
905 struct drm_exynos_g2d_cmd *cmd_buf;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900906
Inki Dae2a3098f2012-11-04 05:48:52 -0800907 cmd_buf = (struct drm_exynos_g2d_cmd *)(uint32_t)req->cmd_buf;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900908
909 if (copy_from_user(cmdlist->data + cmdlist->last,
Inki Dae2a3098f2012-11-04 05:48:52 -0800910 (void __user *)cmd_buf,
911 sizeof(*cmd_buf) * req->cmd_buf_nr)) {
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900912 ret = -EFAULT;
913 goto err_free_event;
914 }
Inki Dae2a3098f2012-11-04 05:48:52 -0800915 cmdlist->last += req->cmd_buf_nr * 2;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900916
Inki Dae2a3098f2012-11-04 05:48:52 -0800917 ret = g2d_check_reg_offset(dev, node, req->cmd_buf_nr, true);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900918 if (ret < 0)
919 goto err_free_event;
920
Inki Daed87342c2012-11-03 21:53:24 -0700921 ret = g2d_map_cmdlist_gem(g2d, node, drm_dev, file);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900922 if (ret < 0)
923 goto err_unmap;
924 }
925
926 cmdlist->data[cmdlist->last++] = G2D_BITBLT_START;
927 cmdlist->data[cmdlist->last++] = G2D_START_BITBLT;
928
929 /* head */
930 cmdlist->head = cmdlist->last / 2;
931
932 /* tail */
933 cmdlist->data[cmdlist->last] = 0;
934
935 g2d_add_cmdlist_to_inuse(g2d_priv, node);
936
937 return 0;
938
939err_unmap:
Inki Daed87342c2012-11-03 21:53:24 -0700940 g2d_unmap_cmdlist_gem(g2d, node, file);
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900941err_free_event:
942 if (node->event) {
943 spin_lock_irqsave(&drm_dev->event_lock, flags);
944 file->event_space += sizeof(e->event);
945 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
946 kfree(node->event);
947 }
948err:
949 g2d_put_cmdlist(g2d, node);
950 return ret;
951}
952EXPORT_SYMBOL_GPL(exynos_g2d_set_cmdlist_ioctl);
953
954int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data,
955 struct drm_file *file)
956{
957 struct drm_exynos_file_private *file_priv = file->driver_priv;
958 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
959 struct device *dev = g2d_priv->dev;
960 struct g2d_data *g2d;
961 struct drm_exynos_g2d_exec *req = data;
962 struct g2d_runqueue_node *runqueue_node;
963 struct list_head *run_cmdlist;
964 struct list_head *event_list;
965
966 if (!dev)
967 return -ENODEV;
968
969 g2d = dev_get_drvdata(dev);
970 if (!g2d)
971 return -EFAULT;
972
973 runqueue_node = kmem_cache_alloc(g2d->runqueue_slab, GFP_KERNEL);
974 if (!runqueue_node) {
975 dev_err(dev, "failed to allocate memory\n");
976 return -ENOMEM;
977 }
978 run_cmdlist = &runqueue_node->run_cmdlist;
979 event_list = &runqueue_node->event_list;
980 INIT_LIST_HEAD(run_cmdlist);
981 INIT_LIST_HEAD(event_list);
982 init_completion(&runqueue_node->complete);
983 runqueue_node->async = req->async;
984
985 list_splice_init(&g2d_priv->inuse_cmdlist, run_cmdlist);
986 list_splice_init(&g2d_priv->event_list, event_list);
987
988 if (list_empty(run_cmdlist)) {
989 dev_err(dev, "there is no inuse cmdlist\n");
990 kmem_cache_free(g2d->runqueue_slab, runqueue_node);
991 return -EPERM;
992 }
993
994 mutex_lock(&g2d->runqueue_mutex);
Inki Dae6b6bae22012-09-11 10:45:36 +0900995 runqueue_node->pid = current->pid;
Inki Daed87342c2012-11-03 21:53:24 -0700996 runqueue_node->filp = file;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900997 list_add_tail(&runqueue_node->list, &g2d->runqueue);
998 if (!g2d->runqueue_node)
999 g2d_exec_runqueue(g2d);
1000 mutex_unlock(&g2d->runqueue_mutex);
1001
1002 if (runqueue_node->async)
1003 goto out;
1004
1005 wait_for_completion(&runqueue_node->complete);
1006 g2d_free_runqueue_node(g2d, runqueue_node);
1007
1008out:
1009 return 0;
1010}
1011EXPORT_SYMBOL_GPL(exynos_g2d_exec_ioctl);
1012
Inki Daed87342c2012-11-03 21:53:24 -07001013static int g2d_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1014{
1015 struct g2d_data *g2d;
1016 int ret;
1017
1018 g2d = dev_get_drvdata(dev);
1019 if (!g2d)
1020 return -EFAULT;
1021
1022 /* allocate dma-aware cmdlist buffer. */
1023 ret = g2d_init_cmdlist(g2d);
1024 if (ret < 0) {
1025 dev_err(dev, "cmdlist init failed\n");
1026 return ret;
1027 }
1028
1029 if (!is_drm_iommu_supported(drm_dev))
1030 return 0;
1031
1032 ret = drm_iommu_attach_device(drm_dev, dev);
1033 if (ret < 0) {
1034 dev_err(dev, "failed to enable iommu.\n");
1035 g2d_fini_cmdlist(g2d);
1036 }
1037
1038 return ret;
1039
1040}
1041
1042static void g2d_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1043{
1044 if (!is_drm_iommu_supported(drm_dev))
1045 return;
1046
1047 drm_iommu_detach_device(drm_dev, dev);
1048}
1049
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001050static int g2d_open(struct drm_device *drm_dev, struct device *dev,
1051 struct drm_file *file)
1052{
1053 struct drm_exynos_file_private *file_priv = file->driver_priv;
1054 struct exynos_drm_g2d_private *g2d_priv;
1055
1056 g2d_priv = kzalloc(sizeof(*g2d_priv), GFP_KERNEL);
1057 if (!g2d_priv) {
1058 dev_err(dev, "failed to allocate g2d private data\n");
1059 return -ENOMEM;
1060 }
1061
1062 g2d_priv->dev = dev;
1063 file_priv->g2d_priv = g2d_priv;
1064
1065 INIT_LIST_HEAD(&g2d_priv->inuse_cmdlist);
1066 INIT_LIST_HEAD(&g2d_priv->event_list);
Inki Dae2a3098f2012-11-04 05:48:52 -08001067 INIT_LIST_HEAD(&g2d_priv->userptr_list);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001068
1069 return 0;
1070}
1071
1072static void g2d_close(struct drm_device *drm_dev, struct device *dev,
1073 struct drm_file *file)
1074{
1075 struct drm_exynos_file_private *file_priv = file->driver_priv;
1076 struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
1077 struct g2d_data *g2d;
1078 struct g2d_cmdlist_node *node, *n;
1079
1080 if (!dev)
1081 return;
1082
1083 g2d = dev_get_drvdata(dev);
1084 if (!g2d)
1085 return;
1086
1087 mutex_lock(&g2d->cmdlist_mutex);
Inki Daed87342c2012-11-03 21:53:24 -07001088 list_for_each_entry_safe(node, n, &g2d_priv->inuse_cmdlist, list) {
1089 /*
1090 * unmap all gem objects not completed.
1091 *
1092 * P.S. if current process was terminated forcely then
1093 * there may be some commands in inuse_cmdlist so unmap
1094 * them.
1095 */
1096 g2d_unmap_cmdlist_gem(g2d, node, file);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001097 list_move_tail(&node->list, &g2d->free_cmdlist);
Inki Daed87342c2012-11-03 21:53:24 -07001098 }
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001099 mutex_unlock(&g2d->cmdlist_mutex);
1100
Inki Dae2a3098f2012-11-04 05:48:52 -08001101 /* release all g2d_userptr in pool. */
1102 g2d_userptr_free_all(drm_dev, g2d, file);
1103
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001104 kfree(file_priv->g2d_priv);
1105}
1106
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001107static int g2d_probe(struct platform_device *pdev)
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001108{
1109 struct device *dev = &pdev->dev;
1110 struct resource *res;
1111 struct g2d_data *g2d;
1112 struct exynos_drm_subdrv *subdrv;
1113 int ret;
1114
Sachin Kamatb7675932012-08-06 12:16:20 +05301115 g2d = devm_kzalloc(&pdev->dev, sizeof(*g2d), GFP_KERNEL);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001116 if (!g2d) {
1117 dev_err(dev, "failed to allocate driver data\n");
1118 return -ENOMEM;
1119 }
1120
1121 g2d->runqueue_slab = kmem_cache_create("g2d_runqueue_slab",
1122 sizeof(struct g2d_runqueue_node), 0, 0, NULL);
Sachin Kamatb7675932012-08-06 12:16:20 +05301123 if (!g2d->runqueue_slab)
1124 return -ENOMEM;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001125
1126 g2d->dev = dev;
1127
1128 g2d->g2d_workq = create_singlethread_workqueue("g2d");
1129 if (!g2d->g2d_workq) {
1130 dev_err(dev, "failed to create workqueue\n");
1131 ret = -EINVAL;
1132 goto err_destroy_slab;
1133 }
1134
1135 INIT_WORK(&g2d->runqueue_work, g2d_runqueue_worker);
1136 INIT_LIST_HEAD(&g2d->free_cmdlist);
1137 INIT_LIST_HEAD(&g2d->runqueue);
1138
1139 mutex_init(&g2d->cmdlist_mutex);
1140 mutex_init(&g2d->runqueue_mutex);
1141
Sachin Kamatdc625532012-11-23 09:11:58 +05301142 g2d->gate_clk = devm_clk_get(dev, "fimg2d");
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001143 if (IS_ERR(g2d->gate_clk)) {
1144 dev_err(dev, "failed to get gate clock\n");
1145 ret = PTR_ERR(g2d->gate_clk);
Inki Daed87342c2012-11-03 21:53:24 -07001146 goto err_destroy_workqueue;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001147 }
1148
1149 pm_runtime_enable(dev);
1150
1151 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001152
Thierry Redingd4ed6022013-01-21 11:09:02 +01001153 g2d->regs = devm_ioremap_resource(&pdev->dev, res);
1154 if (IS_ERR(g2d->regs)) {
1155 ret = PTR_ERR(g2d->regs);
Sachin Kamatb7675932012-08-06 12:16:20 +05301156 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001157 }
1158
1159 g2d->irq = platform_get_irq(pdev, 0);
1160 if (g2d->irq < 0) {
1161 dev_err(dev, "failed to get irq\n");
1162 ret = g2d->irq;
Sachin Kamatb7675932012-08-06 12:16:20 +05301163 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001164 }
1165
Sachin Kamatb7675932012-08-06 12:16:20 +05301166 ret = devm_request_irq(&pdev->dev, g2d->irq, g2d_irq_handler, 0,
1167 "drm_g2d", g2d);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001168 if (ret < 0) {
1169 dev_err(dev, "irq request failed\n");
Sachin Kamatb7675932012-08-06 12:16:20 +05301170 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001171 }
1172
Inki Dae2a3098f2012-11-04 05:48:52 -08001173 g2d->max_pool = MAX_POOL;
1174
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001175 platform_set_drvdata(pdev, g2d);
1176
1177 subdrv = &g2d->subdrv;
1178 subdrv->dev = dev;
Inki Daed87342c2012-11-03 21:53:24 -07001179 subdrv->probe = g2d_subdrv_probe;
1180 subdrv->remove = g2d_subdrv_remove;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001181 subdrv->open = g2d_open;
1182 subdrv->close = g2d_close;
1183
1184 ret = exynos_drm_subdrv_register(subdrv);
1185 if (ret < 0) {
1186 dev_err(dev, "failed to register drm g2d device\n");
Sachin Kamatb7675932012-08-06 12:16:20 +05301187 goto err_put_clk;
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001188 }
1189
1190 dev_info(dev, "The exynos g2d(ver %d.%d) successfully probed\n",
1191 G2D_HW_MAJOR_VER, G2D_HW_MINOR_VER);
1192
1193 return 0;
1194
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001195err_put_clk:
1196 pm_runtime_disable(dev);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001197err_destroy_workqueue:
1198 destroy_workqueue(g2d->g2d_workq);
1199err_destroy_slab:
1200 kmem_cache_destroy(g2d->runqueue_slab);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001201 return ret;
1202}
1203
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001204static int g2d_remove(struct platform_device *pdev)
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001205{
1206 struct g2d_data *g2d = platform_get_drvdata(pdev);
1207
1208 cancel_work_sync(&g2d->runqueue_work);
1209 exynos_drm_subdrv_unregister(&g2d->subdrv);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001210
1211 while (g2d->runqueue_node) {
1212 g2d_free_runqueue_node(g2d, g2d->runqueue_node);
1213 g2d->runqueue_node = g2d_get_runqueue_node(g2d);
1214 }
1215
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001216 pm_runtime_disable(&pdev->dev);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001217
1218 g2d_fini_cmdlist(g2d);
1219 destroy_workqueue(g2d->g2d_workq);
1220 kmem_cache_destroy(g2d->runqueue_slab);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001221
1222 return 0;
1223}
1224
1225#ifdef CONFIG_PM_SLEEP
1226static int g2d_suspend(struct device *dev)
1227{
1228 struct g2d_data *g2d = dev_get_drvdata(dev);
1229
1230 mutex_lock(&g2d->runqueue_mutex);
1231 g2d->suspended = true;
1232 mutex_unlock(&g2d->runqueue_mutex);
1233
1234 while (g2d->runqueue_node)
1235 /* FIXME: good range? */
1236 usleep_range(500, 1000);
1237
Tejun Heo43829732012-08-20 14:51:24 -07001238 flush_work(&g2d->runqueue_work);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001239
1240 return 0;
1241}
1242
1243static int g2d_resume(struct device *dev)
1244{
1245 struct g2d_data *g2d = dev_get_drvdata(dev);
1246
1247 g2d->suspended = false;
1248 g2d_exec_runqueue(g2d);
1249
1250 return 0;
1251}
1252#endif
1253
Sachin Kamat9e1355e2012-08-28 14:11:41 +05301254static SIMPLE_DEV_PM_OPS(g2d_pm_ops, g2d_suspend, g2d_resume);
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001255
Ajay Kumar95fc6332013-02-06 10:59:44 +05301256#ifdef CONFIG_OF
1257static const struct of_device_id exynos_g2d_match[] = {
1258 { .compatible = "samsung,exynos5250-g2d" },
1259 {},
1260};
1261MODULE_DEVICE_TABLE(of, exynos_g2d_match);
1262#endif
1263
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001264struct platform_driver g2d_driver = {
1265 .probe = g2d_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001266 .remove = g2d_remove,
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001267 .driver = {
1268 .name = "s5p-g2d",
1269 .owner = THIS_MODULE,
1270 .pm = &g2d_pm_ops,
Ajay Kumar95fc6332013-02-06 10:59:44 +05301271 .of_match_table = of_match_ptr(exynos_g2d_match),
Joonyoung Shimd7f16422012-05-17 20:06:32 +09001272 },
1273};