10 #include <linux/kernel.h>
11 #include <linux/module.h>
18 #include <linux/slab.h>
22 #include <drm/exynos_drm.h>
26 #define G2D_HW_MAJOR_VER 4
27 #define G2D_HW_MINOR_VER 1
30 #define G2D_VALID_START 0x0104
31 #define G2D_VALID_END 0x0880
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
43 #define G2D_BITBLT_START 0x0100
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
81 #define G2D_USET_HOLD (1 << 2)
82 #define G2D_LIST_HOLD (1 << 1)
83 #define G2D_BITBLT_HOLD (1 << 0)
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)
154 static int g2d_init_cmdlist(
struct g2d_data *g2d)
164 dev_err(dev,
"failed to allocate dma memory\n");
170 dev_err(dev,
"failed to allocate memory\n");
192 static void g2d_fini_cmdlist(
struct g2d_data *g2d)
208 dev_err(dev,
"there is no free cmdlist\n");
215 list_del_init(&node->
list);
248 static int g2d_get_cmdlist_gem(
struct drm_device *drm_dev,
249 struct drm_file *
file,
259 for (i = 0; i < node->
gem_nr; i++) {
262 gem_node = kzalloc(
sizeof(*gem_node),
GFP_KERNEL);
264 dev_err(g2d_priv->
dev,
"failed to allocate gem node\n");
268 offset = cmdlist->
last - (i * 2 + 1);
276 return PTR_ERR(addr);
287 static void g2d_put_cmdlist_gem(
struct drm_device *drm_dev,
288 struct drm_file *file,
300 list_del_init(&node->
list);
306 static void g2d_dma_start(
struct g2d_data *g2d,
313 pm_runtime_get_sync(g2d->
dev);
333 list_del_init(&runqueue_node->
list);
334 return runqueue_node;
337 static void g2d_free_runqueue_node(
struct g2d_data *g2d,
350 static void g2d_exec_runqueue(
struct g2d_data *g2d)
365 pm_runtime_put_sync(g2d->
dev);
374 g2d_exec_runqueue(g2d);
378 static void g2d_finish_event(
struct g2d_data *g2d,
u32 cmdlist_no)
393 e->
event.tv_sec = now.tv_sec;
394 e->
event.tv_usec = now.tv_usec;
395 e->
event.cmdlist_no = cmdlist_no;
398 list_move_tail(&e->
base.link, &e->
base.file_priv->event_list);
400 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
418 g2d_finish_event(g2d, cmdlist_no);
434 int nr,
bool for_addr)
440 for (i = 0; i <
nr; i++) {
441 index = cmdlist->
last - 2 * (i + 1);
442 reg_offset = cmdlist->
data[
index] & ~0xfffff000;
444 if (reg_offset < G2D_VALID_START || reg_offset >
G2D_VALID_END)
449 switch (reg_offset) {
469 dev_err(dev,
"Bad register offset: 0x%x\n", cmdlist->
data[index]);
475 struct drm_file *file)
487 struct drm_file *file)
509 node = g2d_get_cmdlist(g2d);
517 if (file->event_space <
sizeof(e->
event)) {
518 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
522 file->event_space -=
sizeof(e->
event);
523 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
527 dev_err(dev,
"failed to allocate event\n");
530 file->event_space +=
sizeof(e->
event);
531 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
542 e->
base.destroy = (
void (*) (
struct drm_pending_event *))
kfree;
571 dev_err(dev,
"cmdlist size is too big\n");
580 sizeof(*cmd) * req->
cmd_nr)) {
586 ret = g2d_check_reg_offset(dev, cmdlist, req->
cmd_nr,
false);
604 ret = g2d_check_reg_offset(dev, cmdlist, req->
cmd_gem_nr,
true);
608 ret = g2d_get_cmdlist_gem(drm_dev, file, node);
622 g2d_add_cmdlist_to_inuse(g2d_priv, node);
627 g2d_put_cmdlist_gem(drm_dev, file, node->
gem_nr);
631 file->event_space +=
sizeof(e->
event);
632 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
636 g2d_put_cmdlist(g2d, node);
642 struct drm_file *file)
661 if (!runqueue_node) {
662 dev_err(dev,
"failed to allocate memory\n");
667 INIT_LIST_HEAD(run_cmdlist);
668 INIT_LIST_HEAD(event_list);
669 init_completion(&runqueue_node->
complete);
673 list_splice_init(&g2d_priv->
event_list, event_list);
675 if (list_empty(run_cmdlist)) {
676 dev_err(dev,
"there is no inuse cmdlist\n");
685 g2d_exec_runqueue(g2d);
688 if (runqueue_node->
async)
692 g2d_free_runqueue_node(g2d, runqueue_node);
700 struct drm_file *file)
705 g2d_priv = kzalloc(
sizeof(*g2d_priv),
GFP_KERNEL);
707 dev_err(dev,
"failed to allocate g2d private data\n");
716 INIT_LIST_HEAD(&g2d_priv->
gem_list);
722 struct drm_file *file)
738 list_move_tail(&node->
list, &g2d->free_cmdlist);
741 g2d_put_cmdlist_gem(drm_dev, file, g2d_priv->
gem_nr);
743 kfree(file_priv->g2d_priv);
748 struct device *dev = &pdev->dev;
756 dev_err(dev,
"failed to allocate driver data\n");
769 dev_err(dev,
"failed to create workqueue\n");
771 goto err_destroy_slab;
781 ret = g2d_init_cmdlist(g2d);
783 goto err_destroy_workqueue;
787 dev_err(dev,
"failed to get gate clock\n");
789 goto err_fini_cmdlist;
798 dev_err(dev,
"failed to remap I/O memory\n");
805 dev_err(dev,
"failed to get irq\n");
810 ret = devm_request_irq(&pdev->dev, g2d->
irq, g2d_irq_handler, 0,
813 dev_err(dev,
"irq request failed\n");
817 platform_set_drvdata(pdev, g2d);
821 subdrv->
open = g2d_open;
822 subdrv->
close = g2d_close;
826 dev_err(dev,
"failed to register drm g2d device\n");
830 dev_info(dev,
"The exynos g2d(ver %d.%d) successfully probed\n",
836 pm_runtime_disable(dev);
839 g2d_fini_cmdlist(g2d);
840 err_destroy_workqueue:
849 struct g2d_data *g2d = platform_get_drvdata(pdev);
859 pm_runtime_disable(&pdev->
dev);
862 g2d_fini_cmdlist(g2d);
869 #ifdef CONFIG_PM_SLEEP
870 static int g2d_suspend(
struct device *dev)
887 static int g2d_resume(
struct device *dev)
892 g2d_exec_runqueue(g2d);