2 * Copyright (C) 2012 Samsung Electronics Co.Ltd
3 * Authors: Joonyoung Shim <jy0922.shim@samsung.com>
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
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>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
22 #include "exynos_drm.h"
23 #include "exynos_drm_drv.h"
24 #include "exynos_drm_gem.h"
26 #define G2D_HW_MAJOR_VER 4
27 #define G2D_HW_MINOR_VER 1
29 /* vaild register range set from user: 0x0104 ~ 0x0880 */
30 #define G2D_VALID_START 0x0104
31 #define G2D_VALID_END 0x0880
33 /* general registers */
34 #define G2D_SOFT_RESET 0x0000
35 #define G2D_INTEN 0x0004
36 #define G2D_INTC_PEND 0x000C
37 #define G2D_DMA_SFR_BASE_ADDR 0x0080
38 #define G2D_DMA_COMMAND 0x0084
39 #define G2D_DMA_STATUS 0x008C
40 #define G2D_DMA_HOLD_CMD 0x0090
42 /* command registers */
43 #define G2D_BITBLT_START 0x0100
45 /* registers for base address */
46 #define G2D_SRC_BASE_ADDR 0x0304
47 #define G2D_SRC_PLANE2_BASE_ADDR 0x0318
48 #define G2D_DST_BASE_ADDR 0x0404
49 #define G2D_DST_PLANE2_BASE_ADDR 0x0418
50 #define G2D_PAT_BASE_ADDR 0x0500
51 #define G2D_MSK_BASE_ADDR 0x0520
54 #define G2D_SFRCLEAR (1 << 1)
55 #define G2D_R (1 << 0)
58 #define G2D_INTEN_ACF (1 << 3)
59 #define G2D_INTEN_UCF (1 << 2)
60 #define G2D_INTEN_GCF (1 << 1)
61 #define G2D_INTEN_SCF (1 << 0)
64 #define G2D_INTP_ACMD_FIN (1 << 3)
65 #define G2D_INTP_UCMD_FIN (1 << 2)
66 #define G2D_INTP_GCMD_FIN (1 << 1)
67 #define G2D_INTP_SCMD_FIN (1 << 0)
70 #define G2D_DMA_HALT (1 << 2)
71 #define G2D_DMA_CONTINUE (1 << 1)
72 #define G2D_DMA_START (1 << 0)
75 #define G2D_DMA_LIST_DONE_COUNT (0xFF << 17)
76 #define G2D_DMA_BITBLT_DONE_COUNT (0xFFFF << 1)
77 #define G2D_DMA_DONE (1 << 0)
78 #define G2D_DMA_LIST_DONE_COUNT_OFFSET 17
80 /* G2D_DMA_HOLD_CMD */
81 #define G2D_USET_HOLD (1 << 2)
82 #define G2D_LIST_HOLD (1 << 1)
83 #define G2D_BITBLT_HOLD (1 << 0)
85 /* G2D_BITBLT_START */
86 #define G2D_START_CASESEL (1 << 2)
87 #define G2D_START_NHOLT (1 << 1)
88 #define G2D_START_BITBLT (1 << 0)
90 #define G2D_CMDLIST_SIZE (PAGE_SIZE / 4)
91 #define G2D_CMDLIST_NUM 64
92 #define G2D_CMDLIST_POOL_SIZE (G2D_CMDLIST_SIZE * G2D_CMDLIST_NUM)
93 #define G2D_CMDLIST_DATA_NUM (G2D_CMDLIST_SIZE / sizeof(u32) - 2)
95 /* cmdlist data structure */
98 u32 data
[G2D_CMDLIST_DATA_NUM
];
99 u32 last
; /* last data offset */
102 struct drm_exynos_pending_g2d_event
{
103 struct drm_pending_event base
;
104 struct drm_exynos_g2d_event event
;
107 struct g2d_gem_node
{
108 struct list_head list
;
112 struct g2d_cmdlist_node
{
113 struct list_head list
;
114 struct g2d_cmdlist
*cmdlist
;
118 struct drm_exynos_pending_g2d_event
*event
;
121 struct g2d_runqueue_node
{
122 struct list_head list
;
123 struct list_head run_cmdlist
;
124 struct list_head event_list
;
125 struct completion complete
;
131 struct clk
*gate_clk
;
132 struct resource
*regs_res
;
135 struct workqueue_struct
*g2d_workq
;
136 struct work_struct runqueue_work
;
137 struct exynos_drm_subdrv subdrv
;
141 struct g2d_cmdlist_node
*cmdlist_node
;
142 struct list_head free_cmdlist
;
143 struct mutex cmdlist_mutex
;
144 dma_addr_t cmdlist_pool
;
145 void *cmdlist_pool_virt
;
148 struct g2d_runqueue_node
*runqueue_node
;
149 struct list_head runqueue
;
150 struct mutex runqueue_mutex
;
151 struct kmem_cache
*runqueue_slab
;
154 static int g2d_init_cmdlist(struct g2d_data
*g2d
)
156 struct device
*dev
= g2d
->dev
;
157 struct g2d_cmdlist_node
*node
= g2d
->cmdlist_node
;
161 g2d
->cmdlist_pool_virt
= dma_alloc_coherent(dev
, G2D_CMDLIST_POOL_SIZE
,
162 &g2d
->cmdlist_pool
, GFP_KERNEL
);
163 if (!g2d
->cmdlist_pool_virt
) {
164 dev_err(dev
, "failed to allocate dma memory\n");
168 node
= kcalloc(G2D_CMDLIST_NUM
, G2D_CMDLIST_NUM
* sizeof(*node
),
171 dev_err(dev
, "failed to allocate memory\n");
176 for (nr
= 0; nr
< G2D_CMDLIST_NUM
; nr
++) {
178 g2d
->cmdlist_pool_virt
+ nr
* G2D_CMDLIST_SIZE
;
180 g2d
->cmdlist_pool
+ nr
* G2D_CMDLIST_SIZE
;
182 list_add_tail(&node
[nr
].list
, &g2d
->free_cmdlist
);
188 dma_free_coherent(dev
, G2D_CMDLIST_POOL_SIZE
, g2d
->cmdlist_pool_virt
,
193 static void g2d_fini_cmdlist(struct g2d_data
*g2d
)
195 struct device
*dev
= g2d
->dev
;
197 kfree(g2d
->cmdlist_node
);
198 dma_free_coherent(dev
, G2D_CMDLIST_POOL_SIZE
, g2d
->cmdlist_pool_virt
,
202 static struct g2d_cmdlist_node
*g2d_get_cmdlist(struct g2d_data
*g2d
)
204 struct device
*dev
= g2d
->dev
;
205 struct g2d_cmdlist_node
*node
;
207 mutex_lock(&g2d
->cmdlist_mutex
);
208 if (list_empty(&g2d
->free_cmdlist
)) {
209 dev_err(dev
, "there is no free cmdlist\n");
210 mutex_unlock(&g2d
->cmdlist_mutex
);
214 node
= list_first_entry(&g2d
->free_cmdlist
, struct g2d_cmdlist_node
,
216 list_del_init(&node
->list
);
217 mutex_unlock(&g2d
->cmdlist_mutex
);
222 static void g2d_put_cmdlist(struct g2d_data
*g2d
, struct g2d_cmdlist_node
*node
)
224 mutex_lock(&g2d
->cmdlist_mutex
);
225 list_move_tail(&node
->list
, &g2d
->free_cmdlist
);
226 mutex_unlock(&g2d
->cmdlist_mutex
);
229 static void g2d_add_cmdlist_to_inuse(struct exynos_drm_g2d_private
*g2d_priv
,
230 struct g2d_cmdlist_node
*node
)
232 struct g2d_cmdlist_node
*lnode
;
234 if (list_empty(&g2d_priv
->inuse_cmdlist
))
237 /* this links to base address of new cmdlist */
238 lnode
= list_entry(g2d_priv
->inuse_cmdlist
.prev
,
239 struct g2d_cmdlist_node
, list
);
240 lnode
->cmdlist
->data
[lnode
->cmdlist
->last
] = node
->dma_addr
;
243 list_add_tail(&node
->list
, &g2d_priv
->inuse_cmdlist
);
246 list_add_tail(&node
->event
->base
.link
, &g2d_priv
->event_list
);
249 static int g2d_get_cmdlist_gem(struct drm_device
*drm_dev
,
250 struct drm_file
*file
,
251 struct g2d_cmdlist_node
*node
)
253 struct drm_exynos_file_private
*file_priv
= file
->driver_priv
;
254 struct exynos_drm_g2d_private
*g2d_priv
= file_priv
->g2d_priv
;
255 struct g2d_cmdlist
*cmdlist
= node
->cmdlist
;
260 for (i
= 0; i
< node
->gem_nr
; i
++) {
261 struct g2d_gem_node
*gem_node
;
263 gem_node
= kzalloc(sizeof(*gem_node
), GFP_KERNEL
);
265 dev_err(g2d_priv
->dev
, "failed to allocate gem node\n");
269 offset
= cmdlist
->last
- (i
* 2 + 1);
270 gem_node
->handle
= cmdlist
->data
[offset
];
272 addr
= exynos_drm_gem_get_dma_addr(drm_dev
, gem_node
->handle
,
277 return PTR_ERR(addr
);
280 cmdlist
->data
[offset
] = *addr
;
281 list_add_tail(&gem_node
->list
, &g2d_priv
->gem_list
);
288 static void g2d_put_cmdlist_gem(struct drm_device
*drm_dev
,
289 struct drm_file
*file
,
292 struct drm_exynos_file_private
*file_priv
= file
->driver_priv
;
293 struct exynos_drm_g2d_private
*g2d_priv
= file_priv
->g2d_priv
;
294 struct g2d_gem_node
*node
, *n
;
296 list_for_each_entry_safe_reverse(node
, n
, &g2d_priv
->gem_list
, list
) {
300 exynos_drm_gem_put_dma_addr(drm_dev
, node
->handle
, file
);
301 list_del_init(&node
->list
);
307 static void g2d_dma_start(struct g2d_data
*g2d
,
308 struct g2d_runqueue_node
*runqueue_node
)
310 struct g2d_cmdlist_node
*node
=
311 list_first_entry(&runqueue_node
->run_cmdlist
,
312 struct g2d_cmdlist_node
, list
);
314 pm_runtime_get_sync(g2d
->dev
);
315 clk_enable(g2d
->gate_clk
);
317 /* interrupt enable */
318 writel_relaxed(G2D_INTEN_ACF
| G2D_INTEN_UCF
| G2D_INTEN_GCF
,
319 g2d
->regs
+ G2D_INTEN
);
321 writel_relaxed(node
->dma_addr
, g2d
->regs
+ G2D_DMA_SFR_BASE_ADDR
);
322 writel_relaxed(G2D_DMA_START
, g2d
->regs
+ G2D_DMA_COMMAND
);
325 static struct g2d_runqueue_node
*g2d_get_runqueue_node(struct g2d_data
*g2d
)
327 struct g2d_runqueue_node
*runqueue_node
;
329 if (list_empty(&g2d
->runqueue
))
332 runqueue_node
= list_first_entry(&g2d
->runqueue
,
333 struct g2d_runqueue_node
, list
);
334 list_del_init(&runqueue_node
->list
);
335 return runqueue_node
;
338 static void g2d_free_runqueue_node(struct g2d_data
*g2d
,
339 struct g2d_runqueue_node
*runqueue_node
)
344 mutex_lock(&g2d
->cmdlist_mutex
);
345 list_splice_tail_init(&runqueue_node
->run_cmdlist
, &g2d
->free_cmdlist
);
346 mutex_unlock(&g2d
->cmdlist_mutex
);
348 kmem_cache_free(g2d
->runqueue_slab
, runqueue_node
);
351 static void g2d_exec_runqueue(struct g2d_data
*g2d
)
353 g2d
->runqueue_node
= g2d_get_runqueue_node(g2d
);
354 if (g2d
->runqueue_node
)
355 g2d_dma_start(g2d
, g2d
->runqueue_node
);
358 static void g2d_runqueue_worker(struct work_struct
*work
)
360 struct g2d_data
*g2d
= container_of(work
, struct g2d_data
,
364 mutex_lock(&g2d
->runqueue_mutex
);
365 clk_disable(g2d
->gate_clk
);
366 pm_runtime_put_sync(g2d
->dev
);
368 complete(&g2d
->runqueue_node
->complete
);
369 if (g2d
->runqueue_node
->async
)
370 g2d_free_runqueue_node(g2d
, g2d
->runqueue_node
);
373 g2d
->runqueue_node
= NULL
;
375 g2d_exec_runqueue(g2d
);
376 mutex_unlock(&g2d
->runqueue_mutex
);
379 static void g2d_finish_event(struct g2d_data
*g2d
, u32 cmdlist_no
)
381 struct drm_device
*drm_dev
= g2d
->subdrv
.drm_dev
;
382 struct g2d_runqueue_node
*runqueue_node
= g2d
->runqueue_node
;
383 struct drm_exynos_pending_g2d_event
*e
;
387 if (list_empty(&runqueue_node
->event_list
))
390 e
= list_first_entry(&runqueue_node
->event_list
,
391 struct drm_exynos_pending_g2d_event
, base
.link
);
393 do_gettimeofday(&now
);
394 e
->event
.tv_sec
= now
.tv_sec
;
395 e
->event
.tv_usec
= now
.tv_usec
;
396 e
->event
.cmdlist_no
= cmdlist_no
;
398 spin_lock_irqsave(&drm_dev
->event_lock
, flags
);
399 list_move_tail(&e
->base
.link
, &e
->base
.file_priv
->event_list
);
400 wake_up_interruptible(&e
->base
.file_priv
->event_wait
);
401 spin_unlock_irqrestore(&drm_dev
->event_lock
, flags
);
404 static irqreturn_t
g2d_irq_handler(int irq
, void *dev_id
)
406 struct g2d_data
*g2d
= dev_id
;
409 pending
= readl_relaxed(g2d
->regs
+ G2D_INTC_PEND
);
411 writel_relaxed(pending
, g2d
->regs
+ G2D_INTC_PEND
);
413 if (pending
& G2D_INTP_GCMD_FIN
) {
414 u32 cmdlist_no
= readl_relaxed(g2d
->regs
+ G2D_DMA_STATUS
);
416 cmdlist_no
= (cmdlist_no
& G2D_DMA_LIST_DONE_COUNT
) >>
417 G2D_DMA_LIST_DONE_COUNT_OFFSET
;
419 g2d_finish_event(g2d
, cmdlist_no
);
421 writel_relaxed(0, g2d
->regs
+ G2D_DMA_HOLD_CMD
);
422 if (!(pending
& G2D_INTP_ACMD_FIN
)) {
423 writel_relaxed(G2D_DMA_CONTINUE
,
424 g2d
->regs
+ G2D_DMA_COMMAND
);
428 if (pending
& G2D_INTP_ACMD_FIN
)
429 queue_work(g2d
->g2d_workq
, &g2d
->runqueue_work
);
434 static int g2d_check_reg_offset(struct device
*dev
, struct g2d_cmdlist
*cmdlist
,
435 int nr
, bool for_addr
)
441 for (i
= 0; i
< nr
; i
++) {
442 index
= cmdlist
->last
- 2 * (i
+ 1);
443 reg_offset
= cmdlist
->data
[index
] & ~0xfffff000;
445 if (reg_offset
< G2D_VALID_START
|| reg_offset
> G2D_VALID_END
)
450 switch (reg_offset
) {
451 case G2D_SRC_BASE_ADDR
:
452 case G2D_SRC_PLANE2_BASE_ADDR
:
453 case G2D_DST_BASE_ADDR
:
454 case G2D_DST_PLANE2_BASE_ADDR
:
455 case G2D_PAT_BASE_ADDR
:
456 case G2D_MSK_BASE_ADDR
:
470 dev_err(dev
, "Bad register offset: 0x%x\n", cmdlist
->data
[index
]);
474 /* ioctl functions */
475 int exynos_g2d_get_ver_ioctl(struct drm_device
*drm_dev
, void *data
,
476 struct drm_file
*file
)
478 struct drm_exynos_g2d_get_ver
*ver
= data
;
480 ver
->major
= G2D_HW_MAJOR_VER
;
481 ver
->minor
= G2D_HW_MINOR_VER
;
485 EXPORT_SYMBOL_GPL(exynos_g2d_get_ver_ioctl
);
487 int exynos_g2d_set_cmdlist_ioctl(struct drm_device
*drm_dev
, void *data
,
488 struct drm_file
*file
)
490 struct drm_exynos_file_private
*file_priv
= file
->driver_priv
;
491 struct exynos_drm_g2d_private
*g2d_priv
= file_priv
->g2d_priv
;
492 struct device
*dev
= g2d_priv
->dev
;
493 struct g2d_data
*g2d
;
494 struct drm_exynos_g2d_set_cmdlist
*req
= data
;
495 struct drm_exynos_g2d_cmd
*cmd
;
496 struct drm_exynos_pending_g2d_event
*e
;
497 struct g2d_cmdlist_node
*node
;
498 struct g2d_cmdlist
*cmdlist
;
506 g2d
= dev_get_drvdata(dev
);
510 node
= g2d_get_cmdlist(g2d
);
516 if (req
->event_type
!= G2D_EVENT_NOT
) {
517 spin_lock_irqsave(&drm_dev
->event_lock
, flags
);
518 if (file
->event_space
< sizeof(e
->event
)) {
519 spin_unlock_irqrestore(&drm_dev
->event_lock
, flags
);
523 file
->event_space
-= sizeof(e
->event
);
524 spin_unlock_irqrestore(&drm_dev
->event_lock
, flags
);
526 e
= kzalloc(sizeof(*node
->event
), GFP_KERNEL
);
528 dev_err(dev
, "failed to allocate event\n");
530 spin_lock_irqsave(&drm_dev
->event_lock
, flags
);
531 file
->event_space
+= sizeof(e
->event
);
532 spin_unlock_irqrestore(&drm_dev
->event_lock
, flags
);
538 e
->event
.base
.type
= DRM_EXYNOS_G2D_EVENT
;
539 e
->event
.base
.length
= sizeof(e
->event
);
540 e
->event
.user_data
= req
->user_data
;
541 e
->base
.event
= &e
->event
.base
;
542 e
->base
.file_priv
= file
;
543 e
->base
.destroy
= (void (*) (struct drm_pending_event
*)) kfree
;
548 cmdlist
= node
->cmdlist
;
553 * If don't clear SFR registers, the cmdlist is affected by register
554 * values of previous cmdlist. G2D hw executes SFR clear command and
555 * a next command at the same time then the next command is ignored and
556 * is executed rightly from next next command, so needs a dummy command
557 * to next command of SFR clear command.
559 cmdlist
->data
[cmdlist
->last
++] = G2D_SOFT_RESET
;
560 cmdlist
->data
[cmdlist
->last
++] = G2D_SFRCLEAR
;
561 cmdlist
->data
[cmdlist
->last
++] = G2D_SRC_BASE_ADDR
;
562 cmdlist
->data
[cmdlist
->last
++] = 0;
565 cmdlist
->data
[cmdlist
->last
++] = G2D_DMA_HOLD_CMD
;
566 cmdlist
->data
[cmdlist
->last
++] = G2D_LIST_HOLD
;
569 /* Check size of cmdlist: last 2 is about G2D_BITBLT_START */
570 size
= cmdlist
->last
+ req
->cmd_nr
* 2 + req
->cmd_gem_nr
* 2 + 2;
571 if (size
> G2D_CMDLIST_DATA_NUM
) {
572 dev_err(dev
, "cmdlist size is too big\n");
577 cmd
= (struct drm_exynos_g2d_cmd
*)(uint32_t)req
->cmd
;
579 if (copy_from_user(cmdlist
->data
+ cmdlist
->last
,
581 sizeof(*cmd
) * req
->cmd_nr
)) {
585 cmdlist
->last
+= req
->cmd_nr
* 2;
587 ret
= g2d_check_reg_offset(dev
, cmdlist
, req
->cmd_nr
, false);
591 node
->gem_nr
= req
->cmd_gem_nr
;
592 if (req
->cmd_gem_nr
) {
593 struct drm_exynos_g2d_cmd
*cmd_gem
;
595 cmd_gem
= (struct drm_exynos_g2d_cmd
*)(uint32_t)req
->cmd_gem
;
597 if (copy_from_user(cmdlist
->data
+ cmdlist
->last
,
598 (void __user
*)cmd_gem
,
599 sizeof(*cmd_gem
) * req
->cmd_gem_nr
)) {
603 cmdlist
->last
+= req
->cmd_gem_nr
* 2;
605 ret
= g2d_check_reg_offset(dev
, cmdlist
, req
->cmd_gem_nr
, true);
609 ret
= g2d_get_cmdlist_gem(drm_dev
, file
, node
);
614 cmdlist
->data
[cmdlist
->last
++] = G2D_BITBLT_START
;
615 cmdlist
->data
[cmdlist
->last
++] = G2D_START_BITBLT
;
618 cmdlist
->head
= cmdlist
->last
/ 2;
621 cmdlist
->data
[cmdlist
->last
] = 0;
623 g2d_add_cmdlist_to_inuse(g2d_priv
, node
);
628 g2d_put_cmdlist_gem(drm_dev
, file
, node
->gem_nr
);
631 spin_lock_irqsave(&drm_dev
->event_lock
, flags
);
632 file
->event_space
+= sizeof(e
->event
);
633 spin_unlock_irqrestore(&drm_dev
->event_lock
, flags
);
637 g2d_put_cmdlist(g2d
, node
);
640 EXPORT_SYMBOL_GPL(exynos_g2d_set_cmdlist_ioctl
);
642 int exynos_g2d_exec_ioctl(struct drm_device
*drm_dev
, void *data
,
643 struct drm_file
*file
)
645 struct drm_exynos_file_private
*file_priv
= file
->driver_priv
;
646 struct exynos_drm_g2d_private
*g2d_priv
= file_priv
->g2d_priv
;
647 struct device
*dev
= g2d_priv
->dev
;
648 struct g2d_data
*g2d
;
649 struct drm_exynos_g2d_exec
*req
= data
;
650 struct g2d_runqueue_node
*runqueue_node
;
651 struct list_head
*run_cmdlist
;
652 struct list_head
*event_list
;
657 g2d
= dev_get_drvdata(dev
);
661 runqueue_node
= kmem_cache_alloc(g2d
->runqueue_slab
, GFP_KERNEL
);
662 if (!runqueue_node
) {
663 dev_err(dev
, "failed to allocate memory\n");
666 run_cmdlist
= &runqueue_node
->run_cmdlist
;
667 event_list
= &runqueue_node
->event_list
;
668 INIT_LIST_HEAD(run_cmdlist
);
669 INIT_LIST_HEAD(event_list
);
670 init_completion(&runqueue_node
->complete
);
671 runqueue_node
->async
= req
->async
;
673 list_splice_init(&g2d_priv
->inuse_cmdlist
, run_cmdlist
);
674 list_splice_init(&g2d_priv
->event_list
, event_list
);
676 if (list_empty(run_cmdlist
)) {
677 dev_err(dev
, "there is no inuse cmdlist\n");
678 kmem_cache_free(g2d
->runqueue_slab
, runqueue_node
);
682 mutex_lock(&g2d
->runqueue_mutex
);
683 list_add_tail(&runqueue_node
->list
, &g2d
->runqueue
);
684 if (!g2d
->runqueue_node
)
685 g2d_exec_runqueue(g2d
);
686 mutex_unlock(&g2d
->runqueue_mutex
);
688 if (runqueue_node
->async
)
691 wait_for_completion(&runqueue_node
->complete
);
692 g2d_free_runqueue_node(g2d
, runqueue_node
);
697 EXPORT_SYMBOL_GPL(exynos_g2d_exec_ioctl
);
699 static int g2d_open(struct drm_device
*drm_dev
, struct device
*dev
,
700 struct drm_file
*file
)
702 struct drm_exynos_file_private
*file_priv
= file
->driver_priv
;
703 struct exynos_drm_g2d_private
*g2d_priv
;
705 g2d_priv
= kzalloc(sizeof(*g2d_priv
), GFP_KERNEL
);
707 dev_err(dev
, "failed to allocate g2d private data\n");
712 file_priv
->g2d_priv
= g2d_priv
;
714 INIT_LIST_HEAD(&g2d_priv
->inuse_cmdlist
);
715 INIT_LIST_HEAD(&g2d_priv
->event_list
);
716 INIT_LIST_HEAD(&g2d_priv
->gem_list
);
721 static void g2d_close(struct drm_device
*drm_dev
, struct device
*dev
,
722 struct drm_file
*file
)
724 struct drm_exynos_file_private
*file_priv
= file
->driver_priv
;
725 struct exynos_drm_g2d_private
*g2d_priv
= file_priv
->g2d_priv
;
726 struct g2d_data
*g2d
;
727 struct g2d_cmdlist_node
*node
, *n
;
732 g2d
= dev_get_drvdata(dev
);
736 mutex_lock(&g2d
->cmdlist_mutex
);
737 list_for_each_entry_safe(node
, n
, &g2d_priv
->inuse_cmdlist
, list
)
738 list_move_tail(&node
->list
, &g2d
->free_cmdlist
);
739 mutex_unlock(&g2d
->cmdlist_mutex
);
741 g2d_put_cmdlist_gem(drm_dev
, file
, g2d_priv
->gem_nr
);
743 kfree(file_priv
->g2d_priv
);
746 static int __devinit
g2d_probe(struct platform_device
*pdev
)
748 struct device
*dev
= &pdev
->dev
;
749 struct resource
*res
;
750 struct g2d_data
*g2d
;
751 struct exynos_drm_subdrv
*subdrv
;
754 g2d
= kzalloc(sizeof(*g2d
), GFP_KERNEL
);
756 dev_err(dev
, "failed to allocate driver data\n");
760 g2d
->runqueue_slab
= kmem_cache_create("g2d_runqueue_slab",
761 sizeof(struct g2d_runqueue_node
), 0, 0, NULL
);
762 if (!g2d
->runqueue_slab
) {
769 g2d
->g2d_workq
= create_singlethread_workqueue("g2d");
770 if (!g2d
->g2d_workq
) {
771 dev_err(dev
, "failed to create workqueue\n");
773 goto err_destroy_slab
;
776 INIT_WORK(&g2d
->runqueue_work
, g2d_runqueue_worker
);
777 INIT_LIST_HEAD(&g2d
->free_cmdlist
);
778 INIT_LIST_HEAD(&g2d
->runqueue
);
780 mutex_init(&g2d
->cmdlist_mutex
);
781 mutex_init(&g2d
->runqueue_mutex
);
783 ret
= g2d_init_cmdlist(g2d
);
785 goto err_destroy_workqueue
;
787 g2d
->gate_clk
= clk_get(dev
, "fimg2d");
788 if (IS_ERR(g2d
->gate_clk
)) {
789 dev_err(dev
, "failed to get gate clock\n");
790 ret
= PTR_ERR(g2d
->gate_clk
);
791 goto err_fini_cmdlist
;
794 pm_runtime_enable(dev
);
796 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
798 dev_err(dev
, "failed to get I/O memory\n");
803 g2d
->regs_res
= request_mem_region(res
->start
, resource_size(res
),
805 if (!g2d
->regs_res
) {
806 dev_err(dev
, "failed to request I/O memory\n");
811 g2d
->regs
= ioremap(res
->start
, resource_size(res
));
813 dev_err(dev
, "failed to remap I/O memory\n");
815 goto err_release_res
;
818 g2d
->irq
= platform_get_irq(pdev
, 0);
820 dev_err(dev
, "failed to get irq\n");
825 ret
= request_irq(g2d
->irq
, g2d_irq_handler
, 0, "drm_g2d", g2d
);
827 dev_err(dev
, "irq request failed\n");
831 platform_set_drvdata(pdev
, g2d
);
833 subdrv
= &g2d
->subdrv
;
835 subdrv
->open
= g2d_open
;
836 subdrv
->close
= g2d_close
;
838 ret
= exynos_drm_subdrv_register(subdrv
);
840 dev_err(dev
, "failed to register drm g2d device\n");
844 dev_info(dev
, "The exynos g2d(ver %d.%d) successfully probed\n",
845 G2D_HW_MAJOR_VER
, G2D_HW_MINOR_VER
);
850 free_irq(g2d
->irq
, g2d
);
854 release_resource(g2d
->regs_res
);
855 kfree(g2d
->regs_res
);
857 pm_runtime_disable(dev
);
858 clk_put(g2d
->gate_clk
);
860 g2d_fini_cmdlist(g2d
);
861 err_destroy_workqueue
:
862 destroy_workqueue(g2d
->g2d_workq
);
864 kmem_cache_destroy(g2d
->runqueue_slab
);
870 static int __devexit
g2d_remove(struct platform_device
*pdev
)
872 struct g2d_data
*g2d
= platform_get_drvdata(pdev
);
874 cancel_work_sync(&g2d
->runqueue_work
);
875 exynos_drm_subdrv_unregister(&g2d
->subdrv
);
876 free_irq(g2d
->irq
, g2d
);
878 while (g2d
->runqueue_node
) {
879 g2d_free_runqueue_node(g2d
, g2d
->runqueue_node
);
880 g2d
->runqueue_node
= g2d_get_runqueue_node(g2d
);
884 release_resource(g2d
->regs_res
);
885 kfree(g2d
->regs_res
);
887 pm_runtime_disable(&pdev
->dev
);
888 clk_put(g2d
->gate_clk
);
890 g2d_fini_cmdlist(g2d
);
891 destroy_workqueue(g2d
->g2d_workq
);
892 kmem_cache_destroy(g2d
->runqueue_slab
);
898 #ifdef CONFIG_PM_SLEEP
899 static int g2d_suspend(struct device
*dev
)
901 struct g2d_data
*g2d
= dev_get_drvdata(dev
);
903 mutex_lock(&g2d
->runqueue_mutex
);
904 g2d
->suspended
= true;
905 mutex_unlock(&g2d
->runqueue_mutex
);
907 while (g2d
->runqueue_node
)
908 /* FIXME: good range? */
909 usleep_range(500, 1000);
911 flush_work_sync(&g2d
->runqueue_work
);
916 static int g2d_resume(struct device
*dev
)
918 struct g2d_data
*g2d
= dev_get_drvdata(dev
);
920 g2d
->suspended
= false;
921 g2d_exec_runqueue(g2d
);
927 SIMPLE_DEV_PM_OPS(g2d_pm_ops
, g2d_suspend
, g2d_resume
);
929 struct platform_driver g2d_driver
= {
931 .remove
= __devexit_p(g2d_remove
),
934 .owner
= THIS_MODULE
,