drm/rockchip: add mm dump debugfs
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_drv.c
1 /*
2  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3  * Author:Mark Yao <mark.yao@rock-chips.com>
4  *
5  * based on exynos_drm_drv.c
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <drm/drmP.h>
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_crtc_helper.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_sync_helper.h>
22 #include <drm/rockchip_drm.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/dma-iommu.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/memblock.h>
27 #include <linux/module.h>
28 #include <linux/of_address.h>
29 #include <linux/of_graph.h>
30 #include <linux/component.h>
31 #include <linux/fence.h>
32 #include <linux/console.h>
33 #include <linux/iommu.h>
34
35 #include <drm/rockchip_drm.h>
36
37 #include "rockchip_drm_drv.h"
38 #include "rockchip_drm_fb.h"
39 #include "rockchip_drm_fbdev.h"
40 #include "rockchip_drm_gem.h"
41 #include "rockchip_drm_rga.h"
42
43 #define DRIVER_NAME     "rockchip"
44 #define DRIVER_DESC     "RockChip Soc DRM"
45 #define DRIVER_DATE     "20140818"
46 #define DRIVER_MAJOR    1
47 #define DRIVER_MINOR    0
48
49 static bool is_support_iommu = true;
50
51 static LIST_HEAD(rockchip_drm_subdrv_list);
52 static DEFINE_MUTEX(subdrv_list_mutex);
53 static struct drm_driver rockchip_drm_driver;
54
55 struct rockchip_drm_mode_set {
56         struct list_head head;
57         struct drm_framebuffer *fb;
58         struct drm_connector *connector;
59         struct drm_crtc *crtc;
60         struct drm_display_mode *mode;
61         int hdisplay;
62         int vdisplay;
63         int vrefresh;
64
65         bool mode_changed;
66         bool ymirror;
67         int ratio;
68 };
69
70 static struct drm_crtc *find_crtc_by_node(struct drm_device *drm_dev,
71                                           struct device_node *node)
72 {
73         struct device_node *np_crtc;
74         struct drm_crtc *crtc;
75
76         np_crtc = of_get_parent(node);
77         if (!np_crtc || !of_device_is_available(np_crtc))
78                 return NULL;
79
80         drm_for_each_crtc(crtc, drm_dev) {
81                 if (crtc->port == np_crtc)
82                         return crtc;
83         }
84
85         return NULL;
86 }
87
88 static struct drm_connector *find_connector_by_node(struct drm_device *drm_dev,
89                                                     struct device_node *node)
90 {
91         struct device_node *np_connector;
92         struct drm_connector *connector;
93
94         np_connector = of_graph_get_remote_port_parent(node);
95         if (!np_connector || !of_device_is_available(np_connector))
96                 return NULL;
97
98         drm_for_each_connector(connector, drm_dev) {
99                 if (connector->port == np_connector)
100                         return connector;
101         }
102
103         return NULL;
104 }
105
106 static int init_loader_memory(struct drm_device *drm_dev)
107 {
108         struct rockchip_drm_private *private = drm_dev->dev_private;
109         struct rockchip_logo *logo;
110         struct device_node *np = drm_dev->dev->of_node;
111         struct device_node *node;
112         unsigned long nr_pages;
113         struct page **pages;
114         struct sg_table *sgt;
115         phys_addr_t start, size;
116         struct resource res;
117         int i, ret;
118
119         logo = devm_kmalloc(drm_dev->dev, sizeof(*logo), GFP_KERNEL);
120         if (!logo)
121                 return -ENOMEM;
122
123         node = of_parse_phandle(np, "memory-region", 0);
124         if (!node)
125                 return -ENOMEM;
126
127         ret = of_address_to_resource(node, 0, &res);
128         if (ret)
129                 return ret;
130         start = res.start;
131         size = resource_size(&res);
132         if (!size)
133                 return -ENOMEM;
134
135         nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
136         pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL);
137         if (!pages)
138                 return -ENOMEM;
139         i = 0;
140         while (i < nr_pages) {
141                 pages[i] = phys_to_page(start);
142                 start += PAGE_SIZE;
143                 i++;
144         }
145         sgt = drm_prime_pages_to_sg(pages, nr_pages);
146         if (IS_ERR(sgt)) {
147                 ret = PTR_ERR(sgt);
148                 goto err_free_pages;
149         }
150
151         if (private->domain) {
152                 int prot = IOMMU_READ | IOMMU_WRITE;
153
154                 memset(&logo->mm, 0, sizeof(logo->mm));
155                 ret = drm_mm_insert_node_generic(&private->mm, &logo->mm,
156                                                  size, PAGE_SIZE,
157                                                  0, 0, 0);
158                 if (ret < 0) {
159                         DRM_ERROR("out of I/O virtual memory: %d\n", ret);
160                         goto err_free_pages;
161                 }
162
163                 logo->dma_addr = logo->mm.start;
164
165                 if (iommu_map_sg(private->domain, logo->dma_addr, sgt->sgl,
166                                  sgt->nents, prot) < size) {
167                         DRM_ERROR("failed to map buffer");
168                         ret = -ENOMEM;
169                         goto err_remove_node;
170                 }
171         } else {
172                 dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, DMA_TO_DEVICE);
173                 logo->dma_addr = sg_dma_address(sgt->sgl);
174         }
175
176         logo->sgt = sgt;
177         logo->start = res.start;
178         logo->size = size;
179         logo->count = 0;
180         private->logo = logo;
181
182         return 0;
183
184 err_remove_node:
185         drm_mm_remove_node(&logo->mm);
186 err_free_pages:
187         kfree(pages);
188
189         return ret;
190 }
191
192 static struct drm_framebuffer *
193 get_framebuffer_by_node(struct drm_device *drm_dev, struct device_node *node)
194 {
195         struct rockchip_drm_private *private = drm_dev->dev_private;
196         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
197         u32 val;
198         int bpp;
199
200         if (WARN_ON(!private->logo))
201                 return NULL;
202
203         if (of_property_read_u32(node, "logo,offset", &val)) {
204                 pr_err("%s: failed to get logo,offset\n", __func__);
205                 return NULL;
206         }
207         mode_cmd.offsets[0] = val;
208
209         if (of_property_read_u32(node, "logo,width", &val)) {
210                 pr_err("%s: failed to get logo,width\n", __func__);
211                 return NULL;
212         }
213         mode_cmd.width = val;
214
215         if (of_property_read_u32(node, "logo,height", &val)) {
216                 pr_err("%s: failed to get logo,height\n", __func__);
217                 return NULL;
218         }
219         mode_cmd.height = val;
220
221         if (of_property_read_u32(node, "logo,bpp", &val)) {
222                 pr_err("%s: failed to get logo,bpp\n", __func__);
223                 return NULL;
224         }
225         bpp = val;
226
227         mode_cmd.pitches[0] = mode_cmd.width * bpp / 8;
228
229         switch (bpp) {
230         case 16:
231                 mode_cmd.pixel_format = DRM_FORMAT_BGR565;
232                 break;
233         case 24:
234                 mode_cmd.pixel_format = DRM_FORMAT_BGR888;
235                 break;
236         case 32:
237                 mode_cmd.pixel_format = DRM_FORMAT_XBGR8888;
238                 break;
239         default:
240                 pr_err("%s: unsupport to logo bpp %d\n", __func__, bpp);
241                 return NULL;
242         }
243
244         return rockchip_fb_alloc(drm_dev, &mode_cmd, NULL, private->logo, 1);
245 }
246
247 static struct rockchip_drm_mode_set *
248 of_parse_display_resource(struct drm_device *drm_dev, struct device_node *route)
249 {
250         struct rockchip_drm_mode_set *set;
251         struct device_node *connect;
252         struct drm_framebuffer *fb;
253         struct drm_connector *connector;
254         struct drm_crtc *crtc;
255         const char *string;
256         u32 val;
257
258         connect = of_parse_phandle(route, "connect", 0);
259         if (!connect)
260                 return NULL;
261
262         fb = get_framebuffer_by_node(drm_dev, route);
263         if (IS_ERR_OR_NULL(fb))
264                 return NULL;
265
266         crtc = find_crtc_by_node(drm_dev, connect);
267         connector = find_connector_by_node(drm_dev, connect);
268         if (!crtc || !connector) {
269                 dev_warn(drm_dev->dev,
270                          "No available crtc or connector for display");
271                 drm_framebuffer_unreference(fb);
272                 return NULL;
273         }
274
275         set = kzalloc(sizeof(*set), GFP_KERNEL);
276         if (!set)
277                 return NULL;
278
279         if (!of_property_read_u32(route, "video,hdisplay", &val))
280                 set->hdisplay = val;
281
282         if (!of_property_read_u32(route, "video,vdisplay", &val))
283                 set->vdisplay = val;
284
285         if (!of_property_read_u32(route, "video,vrefresh", &val))
286                 set->vrefresh = val;
287
288         if (!of_property_read_u32(route, "logo,ymirror", &val))
289                 set->ymirror = val;
290
291         set->ratio = 1;
292         if (!of_property_read_string(route, "logo,mode", &string) &&
293             !strcmp(string, "fullscreen"))
294                         set->ratio = 0;
295
296         set->fb = fb;
297         set->crtc = crtc;
298         set->connector = connector;
299
300         return set;
301 }
302
303 int setup_initial_state(struct drm_device *drm_dev,
304                         struct drm_atomic_state *state,
305                         struct rockchip_drm_mode_set *set)
306 {
307         struct drm_connector *connector = set->connector;
308         struct drm_crtc *crtc = set->crtc;
309         struct drm_crtc_state *crtc_state;
310         struct drm_connector_state *conn_state;
311         struct drm_plane_state *primary_state;
312         struct drm_display_mode *mode = NULL;
313         const struct drm_connector_helper_funcs *funcs;
314         bool is_crtc_enabled = true;
315         int hdisplay, vdisplay;
316         int fb_width, fb_height;
317         int found = 0, match = 0;
318         int num_modes;
319         int ret = 0;
320
321         if (!set->hdisplay || !set->vdisplay || !set->vrefresh)
322                 is_crtc_enabled = false;
323
324         conn_state = drm_atomic_get_connector_state(state, connector);
325         if (IS_ERR(conn_state))
326                 return PTR_ERR(conn_state);
327
328         funcs = connector->helper_private;
329         conn_state->best_encoder = funcs->best_encoder(connector);
330         if (funcs->loader_protect)
331                 funcs->loader_protect(connector, true);
332         num_modes = connector->funcs->fill_modes(connector, 4096, 4096);
333         if (!num_modes) {
334                 dev_err(drm_dev->dev, "connector[%s] can't found any modes\n",
335                         connector->name);
336                 ret = -EINVAL;
337                 goto error;
338         }
339
340         list_for_each_entry(mode, &connector->modes, head) {
341                 if (mode->hdisplay == set->hdisplay &&
342                     mode->vdisplay == set->vdisplay &&
343                     drm_mode_vrefresh(mode) == set->vrefresh) {
344                         found = 1;
345                         match = 1;
346                         break;
347                 }
348         }
349
350         if (!found) {
351                 list_for_each_entry(mode, &connector->modes, head) {
352                         if (mode->type & DRM_MODE_TYPE_PREFERRED) {
353                                 found = 1;
354                                 break;
355                         }
356                 }
357
358                 if (!found) {
359                         mode = list_first_entry_or_null(&connector->modes,
360                                                         struct drm_display_mode,
361                                                         head);
362                         if (!mode) {
363                                 pr_err("failed to find available modes\n");
364                                 ret = -EINVAL;
365                                 goto error;
366                         }
367                 }
368         }
369
370         set->mode = mode;
371         crtc_state = drm_atomic_get_crtc_state(state, crtc);
372         if (IS_ERR(crtc_state)) {
373                 ret = PTR_ERR(crtc_state);
374                 goto error;
375         }
376
377         drm_mode_copy(&crtc_state->adjusted_mode, mode);
378         if (!match || !is_crtc_enabled) {
379                 set->mode_changed = true;
380         } else {
381                 ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
382                 if (ret)
383                         goto error;
384
385                 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
386                 if (ret)
387                         goto error;
388
389                 crtc_state->active = true;
390         }
391
392         if (!set->fb) {
393                 ret = 0;
394                 goto error;
395         }
396         primary_state = drm_atomic_get_plane_state(state, crtc->primary);
397         if (IS_ERR(primary_state)) {
398                 ret = PTR_ERR(primary_state);
399                 goto error;
400         }
401
402         hdisplay = mode->hdisplay;
403         vdisplay = mode->vdisplay;
404         fb_width = set->fb->width;
405         fb_height = set->fb->height;
406
407         primary_state->crtc = crtc;
408         primary_state->src_x = 0;
409         primary_state->src_y = 0;
410         primary_state->src_w = fb_width << 16;
411         primary_state->src_h = fb_height << 16;
412         if (set->ratio) {
413                 if (set->fb->width >= hdisplay) {
414                         primary_state->crtc_x = 0;
415                         primary_state->crtc_w = hdisplay;
416                 } else {
417                         primary_state->crtc_x = (hdisplay - fb_width) / 2;
418                         primary_state->crtc_w = set->fb->width;
419                 }
420
421                 if (set->fb->height >= vdisplay) {
422                         primary_state->crtc_y = 0;
423                         primary_state->crtc_h = vdisplay;
424                 } else {
425                         primary_state->crtc_y = (vdisplay - fb_height) / 2;
426                         primary_state->crtc_h = fb_height;
427                 }
428         } else {
429                 primary_state->crtc_x = 0;
430                 primary_state->crtc_y = 0;
431                 primary_state->crtc_w = hdisplay;
432                 primary_state->crtc_h = vdisplay;
433         }
434
435         return 0;
436
437 error:
438         if (funcs->loader_protect)
439                 funcs->loader_protect(connector, false);
440         return ret;
441 }
442
443 static int update_state(struct drm_device *drm_dev,
444                         struct drm_atomic_state *state,
445                         struct rockchip_drm_mode_set *set,
446                         unsigned int *plane_mask)
447 {
448         struct drm_mode_config *mode_config = &drm_dev->mode_config;
449         struct drm_crtc *crtc = set->crtc;
450         struct drm_connector *connector = set->connector;
451         struct drm_display_mode *mode = set->mode;
452         struct drm_plane_state *primary_state;
453         struct drm_crtc_state *crtc_state;
454         struct drm_connector_state *conn_state;
455         int ret;
456
457         crtc_state = drm_atomic_get_crtc_state(state, crtc);
458         if (IS_ERR(crtc_state))
459                 return PTR_ERR(crtc_state);
460         conn_state = drm_atomic_get_connector_state(state, connector);
461         if (IS_ERR(conn_state))
462                 return PTR_ERR(conn_state);
463
464         if (set->mode_changed) {
465                 ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
466                 if (ret)
467                         return ret;
468
469                 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
470                 if (ret)
471                         return ret;
472
473                 crtc_state->active = true;
474         } else {
475                 const struct drm_encoder_helper_funcs *encoder_helper_funcs;
476                 const struct drm_connector_helper_funcs *connector_helper_funcs;
477                 struct rockchip_drm_private *priv = drm_dev->dev_private;
478                 struct drm_encoder *encoder;
479                 int pipe = drm_crtc_index(crtc);
480
481                 connector_helper_funcs = connector->helper_private;
482                 if (!priv->crtc_funcs[pipe] ||
483                     !priv->crtc_funcs[pipe]->loader_protect ||
484                     !connector_helper_funcs ||
485                     !connector_helper_funcs->best_encoder)
486                         return -ENXIO;
487                 encoder = connector_helper_funcs->best_encoder(connector);
488                 encoder_helper_funcs = encoder->helper_private;
489                 if (!encoder || !encoder_helper_funcs->atomic_check)
490                         return -ENXIO;
491                 ret = encoder_helper_funcs->atomic_check(encoder, crtc->state,
492                                                          conn_state);
493                 if (ret)
494                         return ret;
495                 priv->crtc_funcs[pipe]->loader_protect(crtc, true);
496         }
497
498         primary_state = drm_atomic_get_plane_state(state, crtc->primary);
499         if (IS_ERR(primary_state))
500                 return PTR_ERR(primary_state);
501
502         crtc_state->plane_mask = 1 << drm_plane_index(crtc->primary);
503         *plane_mask |= crtc_state->plane_mask;
504
505         drm_atomic_set_fb_for_plane(primary_state, set->fb);
506         drm_framebuffer_unreference(set->fb);
507         ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
508
509         if (set->ymirror)
510                 /*
511                  * TODO:
512                  * some vop maybe not support ymirror, but force use it now.
513                  */
514                 drm_atomic_plane_set_property(crtc->primary, primary_state,
515                                               mode_config->rotation_property,
516                                               BIT(DRM_REFLECT_Y));
517
518         return ret;
519 }
520
521 static void show_loader_logo(struct drm_device *drm_dev)
522 {
523         struct drm_atomic_state *state;
524         struct device_node *np = drm_dev->dev->of_node;
525         struct drm_mode_config *mode_config = &drm_dev->mode_config;
526         struct device_node *root, *route;
527         struct rockchip_drm_mode_set *set, *tmp;
528         struct list_head mode_set_list;
529         unsigned plane_mask = 0;
530         int ret;
531
532         root = of_get_child_by_name(np, "route");
533         if (!root) {
534                 dev_warn(drm_dev->dev, "failed to parse display resources\n");
535                 return;
536         }
537
538         if (init_loader_memory(drm_dev)) {
539                 dev_warn(drm_dev->dev, "failed to parse loader memory\n");
540                 return;
541         }
542
543         INIT_LIST_HEAD(&mode_set_list);
544         drm_modeset_lock_all(drm_dev);
545         state = drm_atomic_state_alloc(drm_dev);
546         if (!state) {
547                 dev_err(drm_dev->dev, "failed to alloc atomic state\n");
548                 ret = -ENOMEM;
549                 goto err_unlock;
550         }
551
552         state->acquire_ctx = mode_config->acquire_ctx;
553
554         for_each_child_of_node(root, route) {
555                 if (!of_device_is_available(route))
556                         continue;
557
558                 set = of_parse_display_resource(drm_dev, route);
559                 if (!set)
560                         continue;
561
562                 if (setup_initial_state(drm_dev, state, set)) {
563                         drm_framebuffer_unreference(set->fb);
564                         kfree(set);
565                         continue;
566                 }
567                 INIT_LIST_HEAD(&set->head);
568                 list_add_tail(&set->head, &mode_set_list);
569         }
570
571         if (list_empty(&mode_set_list)) {
572                 dev_warn(drm_dev->dev, "can't not find any loader display\n");
573                 ret = -ENXIO;
574                 goto err_free_state;
575         }
576
577         /*
578          * The state save initial devices status, swap the state into
579          * drm deivces as old state, so if new state come, can compare
580          * with this state to judge which status need to update.
581          */
582         drm_atomic_helper_swap_state(drm_dev, state);
583         drm_atomic_state_free(state);
584         state = drm_atomic_helper_duplicate_state(drm_dev,
585                                                   mode_config->acquire_ctx);
586         if (IS_ERR(state)) {
587                 dev_err(drm_dev->dev, "failed to duplicate atomic state\n");
588                 ret = PTR_ERR_OR_ZERO(state);
589                 goto err_unlock;
590         }
591         state->acquire_ctx = mode_config->acquire_ctx;
592         list_for_each_entry(set, &mode_set_list, head)
593                 /*
594                  * We don't want to see any fail on update_state.
595                  */
596                 WARN_ON(update_state(drm_dev, state, set, &plane_mask));
597
598         ret = drm_atomic_commit(state);
599         drm_atomic_clean_old_fb(drm_dev, plane_mask, ret);
600
601         list_for_each_entry_safe(set, tmp, &mode_set_list, head) {
602                 struct drm_crtc *crtc = set->crtc;
603
604                 list_del(&set->head);
605                 kfree(set);
606
607                 /* FIXME:
608                  * primary plane state rotation is not BIT(0), but we only want
609                  * it effect on logo display, userspace may not known to clean
610                  * this property, would get unexpect display, so force set
611                  * primary rotation to BIT(0).
612                  */
613                 if (!crtc->primary || !crtc->primary->state)
614                         continue;
615
616                 drm_atomic_plane_set_property(crtc->primary,
617                                               crtc->primary->state,
618                                               mode_config->rotation_property,
619                                               BIT(0));
620         }
621
622         /*
623          * Is possible get deadlock here?
624          */
625         WARN_ON(ret == -EDEADLK);
626
627         if (ret)
628                 goto err_free_state;
629
630         drm_modeset_unlock_all(drm_dev);
631         return;
632
633 err_free_state:
634         drm_atomic_state_free(state);
635 err_unlock:
636         drm_modeset_unlock_all(drm_dev);
637         if (ret)
638                 dev_err(drm_dev->dev, "failed to show loader logo\n");
639 }
640
641 /*
642  * Attach a (component) device to the shared drm dma mapping from master drm
643  * device.  This is used by the VOPs to map GEM buffers to a common DMA
644  * mapping.
645  */
646 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
647                                    struct device *dev)
648 {
649         struct rockchip_drm_private *private = drm_dev->dev_private;
650         int ret;
651
652         if (!is_support_iommu)
653                 return 0;
654
655         ret = iommu_attach_device(private->domain, dev);
656         if (ret) {
657                 dev_err(dev, "Failed to attach iommu device\n");
658                 return ret;
659         }
660
661         return 0;
662 }
663
664 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
665                                     struct device *dev)
666 {
667         struct rockchip_drm_private *private = drm_dev->dev_private;
668         struct iommu_domain *domain = private->domain;
669
670         if (!is_support_iommu)
671                 return;
672
673         iommu_detach_device(domain, dev);
674 }
675
676 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
677                                  const struct rockchip_crtc_funcs *crtc_funcs)
678 {
679         int pipe = drm_crtc_index(crtc);
680         struct rockchip_drm_private *priv = crtc->dev->dev_private;
681
682         if (pipe > ROCKCHIP_MAX_CRTC)
683                 return -EINVAL;
684
685         priv->crtc_funcs[pipe] = crtc_funcs;
686
687         return 0;
688 }
689
690 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
691 {
692         int pipe = drm_crtc_index(crtc);
693         struct rockchip_drm_private *priv = crtc->dev->dev_private;
694
695         if (pipe > ROCKCHIP_MAX_CRTC)
696                 return;
697
698         priv->crtc_funcs[pipe] = NULL;
699 }
700
701 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
702                                                 int pipe)
703 {
704         struct drm_crtc *crtc;
705         int i = 0;
706
707         list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
708                 if (i++ == pipe)
709                         return crtc;
710
711         return NULL;
712 }
713
714 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
715                                            unsigned int pipe)
716 {
717         struct rockchip_drm_private *priv = dev->dev_private;
718         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
719
720         if (crtc && priv->crtc_funcs[pipe] &&
721             priv->crtc_funcs[pipe]->enable_vblank)
722                 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
723
724         return 0;
725 }
726
727 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
728                                              unsigned int pipe)
729 {
730         struct rockchip_drm_private *priv = dev->dev_private;
731         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
732
733         if (crtc && priv->crtc_funcs[pipe] &&
734             priv->crtc_funcs[pipe]->enable_vblank)
735                 priv->crtc_funcs[pipe]->disable_vblank(crtc);
736 }
737
738 static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
739 {
740         struct rockchip_drm_private *private = drm_dev->dev_private;
741         struct iommu_domain_geometry *geometry;
742         u64 start, end;
743
744         if (!is_support_iommu)
745                 return 0;
746
747         private->domain = iommu_domain_alloc(&platform_bus_type);
748         if (!private->domain)
749                 return -ENOMEM;
750
751         geometry = &private->domain->geometry;
752         start = geometry->aperture_start;
753         end = geometry->aperture_end;
754
755         DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
756                   start, end);
757         drm_mm_init(&private->mm, start, end - start + 1);
758         mutex_init(&private->mm_lock);
759
760         return 0;
761 }
762
763 static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
764 {
765         struct rockchip_drm_private *private = drm_dev->dev_private;
766
767         if (!is_support_iommu)
768                 return;
769
770         drm_mm_takedown(&private->mm);
771         iommu_domain_free(private->domain);
772 }
773
774 #ifdef CONFIG_DEBUG_FS
775 static int rockchip_drm_mm_dump(struct seq_file *s, void *data)
776 {
777         struct drm_info_node *node = s->private;
778         struct drm_minor *minor = node->minor;
779         struct drm_device *drm_dev = minor->dev;
780         struct rockchip_drm_private *priv = drm_dev->dev_private;
781         int ret;
782
783         mutex_lock(&priv->mm_lock);
784
785         ret = drm_mm_dump_table(s, &priv->mm);
786
787         mutex_unlock(&priv->mm_lock);
788
789         return ret;
790 }
791
792 static int rockchip_drm_summary_show(struct seq_file *s, void *data)
793 {
794         struct drm_info_node *node = s->private;
795         struct drm_minor *minor = node->minor;
796         struct drm_device *drm_dev = minor->dev;
797         struct rockchip_drm_private *priv = drm_dev->dev_private;
798         struct drm_crtc *crtc;
799
800         drm_for_each_crtc(crtc, drm_dev) {
801                 int pipe = drm_crtc_index(crtc);
802
803                 if (priv->crtc_funcs[pipe] &&
804                     priv->crtc_funcs[pipe]->debugfs_dump)
805                         priv->crtc_funcs[pipe]->debugfs_dump(crtc, s);
806         }
807
808         return 0;
809 }
810
811 static struct drm_info_list rockchip_debugfs_files[] = {
812         { "summary", rockchip_drm_summary_show, 0, NULL },
813         { "mm_dump", rockchip_drm_mm_dump, 0, NULL },
814 };
815
816 static int rockchip_drm_debugfs_init(struct drm_minor *minor)
817 {
818         struct drm_device *dev = minor->dev;
819         int ret;
820
821         ret = drm_debugfs_create_files(rockchip_debugfs_files,
822                                        ARRAY_SIZE(rockchip_debugfs_files),
823                                        minor->debugfs_root,
824                                        minor);
825         if (ret) {
826                 dev_err(dev->dev, "could not install rockchip_debugfs_list\n");
827                 return ret;
828         }
829
830         return 0;
831 }
832
833 static void rockchip_drm_debugfs_cleanup(struct drm_minor *minor)
834 {
835         drm_debugfs_remove_files(rockchip_debugfs_files,
836                                  ARRAY_SIZE(rockchip_debugfs_files), minor);
837 }
838 #endif
839
840 static int rockchip_drm_bind(struct device *dev)
841 {
842         struct drm_device *drm_dev;
843         struct rockchip_drm_private *private;
844         int ret;
845
846         drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
847         if (!drm_dev)
848                 return -ENOMEM;
849
850         dev_set_drvdata(dev, drm_dev);
851
852         private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
853         if (!private) {
854                 ret = -ENOMEM;
855                 goto err_free;
856         }
857
858         mutex_init(&private->commit.lock);
859         INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
860
861         drm_dev->dev_private = private;
862
863 #ifdef CONFIG_DRM_DMA_SYNC
864         private->cpu_fence_context = fence_context_alloc(1);
865         atomic_set(&private->cpu_fence_seqno, 0);
866 #endif
867
868         drm_mode_config_init(drm_dev);
869
870         rockchip_drm_mode_config_init(drm_dev);
871
872         ret = rockchip_drm_init_iommu(drm_dev);
873         if (ret)
874                 goto err_config_cleanup;
875
876         /* Try to bind all sub drivers. */
877         ret = component_bind_all(dev, drm_dev);
878         if (ret)
879                 goto err_iommu_cleanup;
880
881         /* init kms poll for handling hpd */
882         drm_kms_helper_poll_init(drm_dev);
883
884         /*
885          * enable drm irq mode.
886          * - with irq_enabled = true, we can use the vblank feature.
887          */
888         drm_dev->irq_enabled = true;
889
890         ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
891         if (ret)
892                 goto err_kms_helper_poll_fini;
893
894         /*
895          * with vblank_disable_allowed = true, vblank interrupt will be disabled
896          * by drm timer once a current process gives up ownership of
897          * vblank event.(after drm_vblank_put function is called)
898          */
899         drm_dev->vblank_disable_allowed = true;
900
901         drm_mode_config_reset(drm_dev);
902
903         show_loader_logo(drm_dev);
904
905         ret = rockchip_drm_fbdev_init(drm_dev);
906         if (ret)
907                 goto err_vblank_cleanup;
908
909         drm_dev->mode_config.allow_fb_modifiers = true;
910
911         ret = drm_dev_register(drm_dev, 0);
912         if (ret)
913                 goto err_fbdev_fini;
914
915         return 0;
916 err_fbdev_fini:
917         rockchip_drm_fbdev_fini(drm_dev);
918 err_vblank_cleanup:
919         drm_vblank_cleanup(drm_dev);
920 err_kms_helper_poll_fini:
921         drm_kms_helper_poll_fini(drm_dev);
922         component_unbind_all(dev, drm_dev);
923 err_iommu_cleanup:
924         rockchip_iommu_cleanup(drm_dev);
925 err_config_cleanup:
926         drm_mode_config_cleanup(drm_dev);
927         drm_dev->dev_private = NULL;
928 err_free:
929         drm_dev_unref(drm_dev);
930         return ret;
931 }
932
933 static void rockchip_drm_unbind(struct device *dev)
934 {
935         struct drm_device *drm_dev = dev_get_drvdata(dev);
936
937         rockchip_drm_fbdev_fini(drm_dev);
938         drm_vblank_cleanup(drm_dev);
939         drm_kms_helper_poll_fini(drm_dev);
940         component_unbind_all(dev, drm_dev);
941         rockchip_iommu_cleanup(drm_dev);
942         drm_mode_config_cleanup(drm_dev);
943         drm_dev->dev_private = NULL;
944         drm_dev_unregister(drm_dev);
945         drm_dev_unref(drm_dev);
946         dev_set_drvdata(dev, NULL);
947 }
948
949 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
950                                                     struct drm_file *file_priv)
951 {
952         struct rockchip_drm_private *priv = crtc->dev->dev_private;
953         int pipe = drm_crtc_index(crtc);
954
955         if (pipe < ROCKCHIP_MAX_CRTC &&
956             priv->crtc_funcs[pipe] &&
957             priv->crtc_funcs[pipe]->cancel_pending_vblank)
958                 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
959 }
960
961 int rockchip_drm_register_subdrv(struct drm_rockchip_subdrv *subdrv)
962 {
963         if (!subdrv)
964                 return -EINVAL;
965
966         mutex_lock(&subdrv_list_mutex);
967         list_add_tail(&subdrv->list, &rockchip_drm_subdrv_list);
968         mutex_unlock(&subdrv_list_mutex);
969
970         return 0;
971 }
972 EXPORT_SYMBOL_GPL(rockchip_drm_register_subdrv);
973
974 int rockchip_drm_unregister_subdrv(struct drm_rockchip_subdrv *subdrv)
975 {
976         if (!subdrv)
977                 return -EINVAL;
978
979         mutex_lock(&subdrv_list_mutex);
980         list_del(&subdrv->list);
981         mutex_unlock(&subdrv_list_mutex);
982
983         return 0;
984 }
985 EXPORT_SYMBOL_GPL(rockchip_drm_unregister_subdrv);
986
987 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
988 {
989         struct rockchip_drm_file_private *file_priv;
990         struct drm_rockchip_subdrv *subdrv;
991         int ret = 0;
992
993         file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
994         if (!file_priv)
995                 return -ENOMEM;
996         INIT_LIST_HEAD(&file_priv->gem_cpu_acquire_list);
997
998         file->driver_priv = file_priv;
999
1000         mutex_lock(&subdrv_list_mutex);
1001         list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list) {
1002                 ret = subdrv->open(dev, subdrv->dev, file);
1003                 if (ret) {
1004                         mutex_unlock(&subdrv_list_mutex);
1005                         goto err_free_file_priv;
1006                 }
1007         }
1008         mutex_unlock(&subdrv_list_mutex);
1009
1010         return 0;
1011
1012 err_free_file_priv:
1013         kfree(file_priv);
1014         file_priv = NULL;
1015
1016         return ret;
1017 }
1018
1019 static void rockchip_drm_preclose(struct drm_device *dev,
1020                                   struct drm_file *file_priv)
1021 {
1022         struct rockchip_drm_file_private *file_private = file_priv->driver_priv;
1023         struct rockchip_gem_object_node *cur, *d;
1024         struct drm_rockchip_subdrv *subdrv;
1025         struct drm_crtc *crtc;
1026
1027         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1028                 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
1029
1030         mutex_lock(&dev->struct_mutex);
1031         list_for_each_entry_safe(cur, d,
1032                         &file_private->gem_cpu_acquire_list, list) {
1033 #ifdef CONFIG_DRM_DMA_SYNC
1034                 BUG_ON(!cur->rockchip_gem_obj->acquire_fence);
1035                 drm_fence_signal_and_put(&cur->rockchip_gem_obj->acquire_fence);
1036 #endif
1037                 drm_gem_object_unreference(&cur->rockchip_gem_obj->base);
1038                 kfree(cur);
1039         }
1040         /* since we are deleting the whole list, just initialize the header
1041          * instead of calling list_del for every element
1042          */
1043         INIT_LIST_HEAD(&file_private->gem_cpu_acquire_list);
1044         mutex_unlock(&dev->struct_mutex);
1045
1046         mutex_lock(&subdrv_list_mutex);
1047         list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list)
1048                 subdrv->close(dev, subdrv->dev, file_priv);
1049         mutex_unlock(&subdrv_list_mutex);
1050 }
1051
1052 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
1053 {
1054         kfree(file->driver_priv);
1055         file->driver_priv = NULL;
1056 }
1057
1058 void rockchip_drm_lastclose(struct drm_device *dev)
1059 {
1060         struct rockchip_drm_private *priv = dev->dev_private;
1061
1062         drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev_helper);
1063 }
1064
1065 static const struct drm_ioctl_desc rockchip_ioctls[] = {
1066         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_gem_create_ioctl,
1067                           DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1068         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
1069                           rockchip_gem_map_offset_ioctl,
1070                           DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1071         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_ACQUIRE,
1072                           rockchip_gem_cpu_acquire_ioctl,
1073                           DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1074         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_RELEASE,
1075                           rockchip_gem_cpu_release_ioctl,
1076                           DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1077         DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_GET_VER, rockchip_rga_get_ver_ioctl,
1078                           DRM_AUTH | DRM_RENDER_ALLOW),
1079         DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_SET_CMDLIST,
1080                           rockchip_rga_set_cmdlist_ioctl,
1081                           DRM_AUTH | DRM_RENDER_ALLOW),
1082         DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_EXEC, rockchip_rga_exec_ioctl,
1083                           DRM_AUTH | DRM_RENDER_ALLOW),
1084 };
1085
1086 static const struct file_operations rockchip_drm_driver_fops = {
1087         .owner = THIS_MODULE,
1088         .open = drm_open,
1089         .mmap = rockchip_gem_mmap,
1090         .poll = drm_poll,
1091         .read = drm_read,
1092         .unlocked_ioctl = drm_ioctl,
1093 #ifdef CONFIG_COMPAT
1094         .compat_ioctl = drm_compat_ioctl,
1095 #endif
1096         .release = drm_release,
1097 };
1098
1099 const struct vm_operations_struct rockchip_drm_vm_ops = {
1100         .open = drm_gem_vm_open,
1101         .close = drm_gem_vm_close,
1102 };
1103
1104 static struct drm_driver rockchip_drm_driver = {
1105         .driver_features        = DRIVER_MODESET | DRIVER_GEM |
1106                                   DRIVER_PRIME | DRIVER_ATOMIC |
1107                                   DRIVER_RENDER,
1108         .preclose               = rockchip_drm_preclose,
1109         .lastclose              = rockchip_drm_lastclose,
1110         .get_vblank_counter     = drm_vblank_no_hw_counter,
1111         .open                   = rockchip_drm_open,
1112         .postclose              = rockchip_drm_postclose,
1113         .enable_vblank          = rockchip_drm_crtc_enable_vblank,
1114         .disable_vblank         = rockchip_drm_crtc_disable_vblank,
1115         .gem_vm_ops             = &rockchip_drm_vm_ops,
1116         .gem_free_object        = rockchip_gem_free_object,
1117         .dumb_create            = rockchip_gem_dumb_create,
1118         .dumb_map_offset        = rockchip_gem_dumb_map_offset,
1119         .dumb_destroy           = drm_gem_dumb_destroy,
1120         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
1121         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
1122         .gem_prime_import       = drm_gem_prime_import,
1123         .gem_prime_export       = drm_gem_prime_export,
1124         .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
1125         .gem_prime_import_sg_table      = rockchip_gem_prime_import_sg_table,
1126         .gem_prime_vmap         = rockchip_gem_prime_vmap,
1127         .gem_prime_vunmap       = rockchip_gem_prime_vunmap,
1128         .gem_prime_mmap         = rockchip_gem_mmap_buf,
1129 #ifdef CONFIG_DEBUG_FS
1130         .debugfs_init           = rockchip_drm_debugfs_init,
1131         .debugfs_cleanup        = rockchip_drm_debugfs_cleanup,
1132 #endif
1133         .ioctls                 = rockchip_ioctls,
1134         .num_ioctls             = ARRAY_SIZE(rockchip_ioctls),
1135         .fops                   = &rockchip_drm_driver_fops,
1136         .name   = DRIVER_NAME,
1137         .desc   = DRIVER_DESC,
1138         .date   = DRIVER_DATE,
1139         .major  = DRIVER_MAJOR,
1140         .minor  = DRIVER_MINOR,
1141 };
1142
1143 #ifdef CONFIG_PM_SLEEP
1144 void rockchip_drm_fb_suspend(struct drm_device *drm)
1145 {
1146         struct rockchip_drm_private *priv = drm->dev_private;
1147
1148         console_lock();
1149         drm_fb_helper_set_suspend(priv->fbdev_helper, 1);
1150         console_unlock();
1151 }
1152
1153 void rockchip_drm_fb_resume(struct drm_device *drm)
1154 {
1155         struct rockchip_drm_private *priv = drm->dev_private;
1156
1157         console_lock();
1158         drm_fb_helper_set_suspend(priv->fbdev_helper, 0);
1159         console_unlock();
1160 }
1161
1162 static int rockchip_drm_sys_suspend(struct device *dev)
1163 {
1164         struct drm_device *drm = dev_get_drvdata(dev);
1165         struct rockchip_drm_private *priv = drm->dev_private;
1166
1167         drm_kms_helper_poll_disable(drm);
1168         rockchip_drm_fb_suspend(drm);
1169
1170         priv->state = drm_atomic_helper_suspend(drm);
1171         if (IS_ERR(priv->state)) {
1172                 rockchip_drm_fb_resume(drm);
1173                 drm_kms_helper_poll_enable(drm);
1174                 return PTR_ERR(priv->state);
1175         }
1176
1177         return 0;
1178 }
1179
1180 static int rockchip_drm_sys_resume(struct device *dev)
1181 {
1182         struct drm_device *drm = dev_get_drvdata(dev);
1183         struct rockchip_drm_private *priv = drm->dev_private;
1184
1185         drm_atomic_helper_resume(drm, priv->state);
1186         rockchip_drm_fb_resume(drm);
1187         drm_kms_helper_poll_enable(drm);
1188
1189         return 0;
1190 }
1191 #endif
1192
1193 static const struct dev_pm_ops rockchip_drm_pm_ops = {
1194         SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
1195                                 rockchip_drm_sys_resume)
1196 };
1197
1198 static int compare_of(struct device *dev, void *data)
1199 {
1200         struct device_node *np = data;
1201
1202         return dev->of_node == np;
1203 }
1204
1205 static void rockchip_add_endpoints(struct device *dev,
1206                                    struct component_match **match,
1207                                    struct device_node *port)
1208 {
1209         struct device_node *ep, *remote;
1210
1211         for_each_child_of_node(port, ep) {
1212                 remote = of_graph_get_remote_port_parent(ep);
1213                 if (!remote || !of_device_is_available(remote)) {
1214                         of_node_put(remote);
1215                         continue;
1216                 } else if (!of_device_is_available(remote->parent)) {
1217                         dev_warn(dev, "parent device of %s is not available\n",
1218                                  remote->full_name);
1219                         of_node_put(remote);
1220                         continue;
1221                 }
1222
1223                 component_match_add(dev, match, compare_of, remote);
1224                 of_node_put(remote);
1225         }
1226 }
1227
1228 static const struct component_master_ops rockchip_drm_ops = {
1229         .bind = rockchip_drm_bind,
1230         .unbind = rockchip_drm_unbind,
1231 };
1232
1233 static int rockchip_drm_platform_probe(struct platform_device *pdev)
1234 {
1235         struct device *dev = &pdev->dev;
1236         struct component_match *match = NULL;
1237         struct device_node *np = dev->of_node;
1238         struct device_node *port;
1239         int i;
1240
1241         if (!np)
1242                 return -ENODEV;
1243         /*
1244          * Bind the crtc ports first, so that
1245          * drm_of_find_possible_crtcs called from encoder .bind callbacks
1246          * works as expected.
1247          */
1248         for (i = 0;; i++) {
1249                 struct device_node *iommu;
1250
1251                 port = of_parse_phandle(np, "ports", i);
1252                 if (!port)
1253                         break;
1254
1255                 if (!of_device_is_available(port->parent)) {
1256                         of_node_put(port);
1257                         continue;
1258                 }
1259
1260                 iommu = of_parse_phandle(port->parent, "iommus", 0);
1261                 if (!iommu || !of_device_is_available(iommu->parent)) {
1262                         dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
1263                                 port->parent->full_name);
1264                         /*
1265                          * if there is a crtc not support iommu, force set all
1266                          * crtc use non-iommu buffer.
1267                          */
1268                         is_support_iommu = false;
1269                 }
1270
1271                 component_match_add(dev, &match, compare_of, port->parent);
1272                 of_node_put(port);
1273         }
1274
1275         if (i == 0) {
1276                 dev_err(dev, "missing 'ports' property\n");
1277                 return -ENODEV;
1278         }
1279
1280         if (!match) {
1281                 dev_err(dev, "No available vop found for display-subsystem.\n");
1282                 return -ENODEV;
1283         }
1284         /*
1285          * For each bound crtc, bind the encoders attached to its
1286          * remote endpoint.
1287          */
1288         for (i = 0;; i++) {
1289                 port = of_parse_phandle(np, "ports", i);
1290                 if (!port)
1291                         break;
1292
1293                 if (!of_device_is_available(port->parent)) {
1294                         of_node_put(port);
1295                         continue;
1296                 }
1297
1298                 rockchip_add_endpoints(dev, &match, port);
1299                 of_node_put(port);
1300         }
1301
1302         return component_master_add_with_match(dev, &rockchip_drm_ops, match);
1303 }
1304
1305 static int rockchip_drm_platform_remove(struct platform_device *pdev)
1306 {
1307         component_master_del(&pdev->dev, &rockchip_drm_ops);
1308
1309         return 0;
1310 }
1311
1312 static const struct of_device_id rockchip_drm_dt_ids[] = {
1313         { .compatible = "rockchip,display-subsystem", },
1314         { /* sentinel */ },
1315 };
1316 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
1317
1318 static struct platform_driver rockchip_drm_platform_driver = {
1319         .probe = rockchip_drm_platform_probe,
1320         .remove = rockchip_drm_platform_remove,
1321         .driver = {
1322                 .name = "rockchip-drm",
1323                 .of_match_table = rockchip_drm_dt_ids,
1324                 .pm = &rockchip_drm_pm_ops,
1325         },
1326 };
1327
1328 module_platform_driver(rockchip_drm_platform_driver);
1329
1330 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
1331 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
1332 MODULE_LICENSE("GPL v2");