hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
 * Author:Mark Yao <mark.yao@rock-chips.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
 
#include <linux/kernel.h>
#include <drm/drm.h>
#include <drm/drmP.h>
#include <drm/drm_atomic.h>
#include <drm/drm_fb_helper.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_gem_framebuffer_helper.h>
#include <linux/memblock.h>
#include <soc/rockchip/rockchip_dmc.h>
 
#include "rockchip_drm_drv.h"
#include "rockchip_drm_fb.h"
#include "rockchip_drm_gem.h"
#include "rockchip_drm_psr.h"
 
bool rockchip_fb_is_logo(struct drm_framebuffer *fb)
{
   struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
 
   return rk_fb && rk_fb->logo;
}
 
dma_addr_t rockchip_fb_get_dma_addr(struct drm_framebuffer *fb,
                   unsigned int plane)
{
   struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
 
   if (WARN_ON(plane >= ROCKCHIP_MAX_FB_BUFFER))
       return 0;
 
   return rk_fb->dma_addr[plane];
}
 
void *rockchip_fb_get_kvaddr(struct drm_framebuffer *fb, unsigned int plane)
{
   struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
 
   if (WARN_ON(plane >= ROCKCHIP_MAX_FB_BUFFER))
       return 0;
 
   return rk_fb->kvaddr[plane];
}
 
static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
{
   struct drm_gem_object *obj;
   int i;
   struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
 
   for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) {
       obj = rockchip_fb->obj[i];
       if (obj)
           drm_gem_object_unreference_unlocked(obj);
   }
 
#ifndef MODULE
   if (rockchip_fb->logo)
       rockchip_free_loader_memory(fb->dev);
#else
   WARN_ON(rockchip_fb->logo);
#endif
 
   drm_framebuffer_cleanup(fb);
   kfree(rockchip_fb);
}
 
static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
                    struct drm_file *file_priv,
                    unsigned int *handle)
{
   struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
 
   if (rockchip_fb_is_logo(fb))
       return -EOPNOTSUPP;
 
   return drm_gem_handle_create(file_priv,
                    rockchip_fb->obj[0], handle);
}
 
static int rockchip_drm_fb_dirty(struct drm_framebuffer *fb,
                struct drm_file *file,
                unsigned int flags, unsigned int color,
                struct drm_clip_rect *clips,
                unsigned int num_clips)
{
   rockchip_drm_psr_flush_all(fb->dev);
   return 0;
}
 
static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
   .destroy       = rockchip_drm_fb_destroy,
   .create_handle = rockchip_drm_fb_create_handle,
   .dirty           = rockchip_drm_fb_dirty,
};
 
struct drm_framebuffer *
rockchip_fb_alloc(struct drm_device *dev, const struct drm_mode_fb_cmd2 *mode_cmd,
         struct drm_gem_object **obj, struct rockchip_logo *logo,
         unsigned int num_planes)
{
   struct rockchip_drm_fb *rockchip_fb;
   struct rockchip_gem_object *rk_obj;
   struct rockchip_drm_private *private = dev->dev_private;
   struct drm_fb_helper *fb_helper = private->fbdev_helper;
   int ret = 0;
   int i;
 
   rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
   if (!rockchip_fb)
       return ERR_PTR(-ENOMEM);
 
   drm_helper_mode_fill_fb_struct(dev, &rockchip_fb->fb, mode_cmd);
 
   ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
                  &rockchip_drm_fb_funcs);
   if (ret) {
       dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
           ret);
       goto err_free_fb;
   }
 
   if (obj) {
       for (i = 0; i < num_planes; i++)
           rockchip_fb->obj[i] = obj[i];
 
       for (i = 0; i < num_planes; i++) {
           rk_obj = to_rockchip_obj(obj[i]);
           rockchip_fb->dma_addr[i] = rk_obj->dma_addr;
           rockchip_fb->kvaddr[i] = rk_obj->kvaddr;
           private->fbdev_bo = &rk_obj->base;
           if (fb_helper && fb_helper->fbdev && rk_obj->kvaddr)
               fb_helper->fbdev->screen_base = rk_obj->kvaddr;
       }
#ifndef MODULE
   } else if (logo) {
       rockchip_fb->dma_addr[0] = logo->dma_addr;
       rockchip_fb->kvaddr[0] = logo->kvaddr;
       rockchip_fb->logo = logo;
       logo->count++;
#endif
   } else {
       ret = -EINVAL;
       dev_err(dev->dev, "Failed to find available buffer\n");
       goto err_deinit_drm_fb;
   }
 
   return &rockchip_fb->fb;
 
err_deinit_drm_fb:
   drm_framebuffer_cleanup(&rockchip_fb->fb);
err_free_fb:
   kfree(rockchip_fb);
   return ERR_PTR(ret);
}
 
static struct drm_framebuffer *
rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
           const struct drm_mode_fb_cmd2 *mode_cmd)
{
   struct drm_framebuffer *fb;
   struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
   struct drm_gem_object *obj;
   unsigned int hsub;
   unsigned int vsub;
   int num_planes;
   int ret;
   int i;
 
   hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
   vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
   num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
            ROCKCHIP_MAX_FB_BUFFER);
 
   for (i = 0; i < num_planes; ++i) {
       if (mode_cmd->pitches[i] % 4) {
           DRM_DEV_ERROR_RATELIMITED(dev->dev,
               "fb pitch[%d] must be 4 byte aligned: %d\n", i, mode_cmd->pitches[i]);
           return ERR_PTR(-EINVAL);
       }
   }
 
   for (i = 0; i < num_planes; i++) {
       unsigned int width = mode_cmd->width / (i ? hsub : 1);
       unsigned int height = mode_cmd->height / (i ? vsub : 1);
       unsigned int min_size;
 
       obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
       if (!obj) {
           DRM_DEV_ERROR(dev->dev,
                     "Failed to lookup GEM object\n");
           ret = -ENXIO;
           goto err_gem_object_unreference;
       }
 
       min_size = (height - 1) * mode_cmd->pitches[i] +
           mode_cmd->offsets[i] +
           width * drm_format_plane_cpp(mode_cmd->pixel_format, i);
 
       if (obj->size < min_size) {
           drm_gem_object_put_unlocked(obj);
           ret = -EINVAL;
           goto err_gem_object_unreference;
       }
       objs[i] = obj;
   }
 
   fb = rockchip_fb_alloc(dev, mode_cmd, objs, NULL, i);
   if (IS_ERR(fb)) {
       ret = PTR_ERR(fb);
       goto err_gem_object_unreference;
   }
 
   return fb;
 
err_gem_object_unreference:
   for (i--; i >= 0; i--)
       drm_gem_object_put_unlocked(objs[i]);
   return ERR_PTR(ret);
}
 
static void rockchip_drm_output_poll_changed(struct drm_device *dev)
{
   struct rockchip_drm_private *private = dev->dev_private;
   struct drm_fb_helper *fb_helper = private->fbdev_helper;
 
   if (fb_helper && dev->mode_config.poll_enabled && !private->loader_protect)
       drm_fb_helper_hotplug_event(fb_helper);
}
 
static int rockchip_drm_bandwidth_atomic_check(struct drm_device *dev,
                          struct drm_atomic_state *state,
                          size_t *line_bw_mbyte,
                          size_t *frame_bw_mbyte,
                          unsigned int *plane_num)
{
   struct rockchip_drm_private *priv = dev->dev_private;
   struct drm_crtc_state *crtc_state;
   const struct rockchip_crtc_funcs *funcs;
   struct drm_crtc *crtc;
   int i, ret = 0;
 
   *line_bw_mbyte = 0;
   *frame_bw_mbyte = 0;
   *plane_num = 0;
   for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
       funcs = priv->crtc_funcs[drm_crtc_index(crtc)];
 
       if (funcs && funcs->bandwidth)
           *line_bw_mbyte += funcs->bandwidth(crtc, crtc_state,
                              frame_bw_mbyte,
                              plane_num);
   }
 
   /*
    * Check ddr frequency support here here.
    */
   if (priv->dmc_support && !priv->devfreq) {
       priv->devfreq = devfreq_get_devfreq_by_phandle(dev->dev, 0);
       if (IS_ERR(priv->devfreq))
           priv->devfreq = NULL;
   }
 
   if (priv->devfreq)
       ret = rockchip_dmcfreq_vop_bandwidth_request(priv->devfreq,
                                *line_bw_mbyte);
 
   return ret;
}
 
static void rockchip_drm_psr_inhibit_get_state(struct drm_atomic_state *state)
{
   struct drm_crtc *crtc;
   struct drm_crtc_state *crtc_state;
   struct drm_encoder *encoder;
   u32 encoder_mask = 0;
   int i;
 
   for_each_old_crtc_in_state(state, crtc, crtc_state, i) {
       encoder_mask |= crtc_state->encoder_mask;
       encoder_mask |= crtc->state->encoder_mask;
   }
 
   drm_for_each_encoder_mask(encoder, state->dev, encoder_mask)
       rockchip_drm_psr_inhibit_get(encoder);
}
 
static void
rockchip_drm_psr_inhibit_put_state(struct drm_atomic_state *state)
{
   struct drm_crtc *crtc;
   struct drm_crtc_state *crtc_state;
   struct drm_encoder *encoder;
   u32 encoder_mask = 0;
   int i;
 
   for_each_old_crtc_in_state(state, crtc, crtc_state, i) {
       encoder_mask |= crtc_state->encoder_mask;
       encoder_mask |= crtc->state->encoder_mask;
   }
 
   drm_for_each_encoder_mask(encoder, state->dev, encoder_mask)
       rockchip_drm_psr_inhibit_put(encoder);
}
 
static void
rockchip_drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
                       struct drm_atomic_state *old_state)
{
   struct drm_crtc *crtc;
   struct drm_crtc_state *old_crtc_state, *new_crtc_state;
   int i, ret;
   unsigned int crtc_mask = 0;
   struct rockchip_crtc_state *s;
 
    /*
     * Legacy cursor ioctls are completely unsynced, and userspace
     * relies on that (by doing tons of cursor updates).
     */
   if (old_state->legacy_cursor_update)
       return;
 
   for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
       if (!new_crtc_state->active)
           continue;
 
       ret = drm_crtc_vblank_get(crtc);
       if (ret != 0)
           continue;
 
       crtc_mask |= drm_crtc_mask(crtc);
       old_state->crtcs[i].last_vblank_count =
                       drm_crtc_vblank_count(crtc);
   }
 
   for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
       if (!(crtc_mask & drm_crtc_mask(crtc)))
           continue;
 
       ret = wait_event_timeout(dev->vblank[i].queue,
               old_state->crtcs[i].last_vblank_count !=
                   drm_crtc_vblank_count(crtc),
               msecs_to_jiffies(50));
 
       s = to_rockchip_crtc_state(crtc->state);
 
       if (!s->mode_update && !ret)
           DRM_WARN("[CRTC:%d:%s] state:%d, vblank wait timed out\n",
                crtc->base.id, crtc->name, old_crtc_state->active);
 
       drm_crtc_vblank_put(crtc);
       s->mode_update = false;
   }
}
 
static void
rockchip_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
{
   struct drm_device *dev = old_state->dev;
 
   rockchip_drm_psr_inhibit_get_state(old_state);
 
   drm_atomic_helper_commit_modeset_disables(dev, old_state);
 
   drm_atomic_helper_commit_modeset_enables(dev, old_state);
 
   drm_atomic_helper_commit_planes(dev, old_state,
                   DRM_PLANE_COMMIT_ACTIVE_ONLY);
 
   rockchip_drm_psr_inhibit_put_state(old_state);
 
   drm_atomic_helper_commit_hw_done(old_state);
 
   rockchip_drm_atomic_helper_wait_for_vblanks(dev, old_state);
 
   drm_atomic_helper_cleanup_planes(dev, old_state);
}
 
static const struct drm_mode_config_helper_funcs rockchip_mode_config_helpers = {
   .atomic_commit_tail = rockchip_atomic_helper_commit_tail_rpm,
};
 
static void
rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
{
   struct drm_atomic_state *state = commit->state;
   struct drm_device *dev = commit->dev;
   struct rockchip_drm_private *prv = dev->dev_private;
   size_t line_bw_mbyte = commit->line_bw_mbyte;
   size_t frame_bw_mbyte = commit->frame_bw_mbyte;
   unsigned int plane_num = commit->plane_num;
 
   /*
    * TODO: do fence wait here.
    */
 
   /*
    * Rockchip crtc support runtime PM, can't update display planes
    * when crtc is disabled.
    *
    * drm_atomic_helper_commit comments detail that:
    *     For drivers supporting runtime PM the recommended sequence is
    *
    *     drm_atomic_helper_commit_modeset_disables(dev, state);
    *
    *     drm_atomic_helper_commit_modeset_enables(dev, state);
    *
    *     drm_atomic_helper_commit_planes(dev, state, true);
    *
    * See the kerneldoc entries for these three functions for more details.
    */
   drm_atomic_helper_wait_for_dependencies(state);
 
   rockchip_drm_psr_inhibit_get_state(state);
 
   drm_atomic_helper_commit_modeset_disables(dev, state);
 
   drm_atomic_helper_commit_modeset_enables(dev, state);
 
   if (prv->dmc_support && !prv->devfreq) {
       prv->devfreq = devfreq_get_devfreq_by_phandle(dev->dev, 0);
       if (IS_ERR(prv->devfreq))
           prv->devfreq = NULL;
   }
   if (prv->devfreq)
       rockchip_dmcfreq_vop_bandwidth_update(prv->devfreq, line_bw_mbyte, frame_bw_mbyte,
                             plane_num);
 
   mutex_lock(&prv->ovl_lock);
   drm_atomic_helper_commit_planes(dev, state, true);
   mutex_unlock(&prv->ovl_lock);
 
   rockchip_drm_psr_inhibit_put_state(state);
 
   drm_atomic_helper_commit_hw_done(state);
 
   rockchip_drm_atomic_helper_wait_for_vblanks(dev, state);
 
   drm_atomic_helper_cleanup_planes(dev, state);
 
   drm_atomic_helper_commit_cleanup_done(state);
 
   drm_atomic_state_put(state);
 
   kfree(commit);
}
 
void rockchip_drm_atomic_work(struct work_struct *work)
{
   struct rockchip_drm_private *private = container_of(work,
               struct rockchip_drm_private, commit_work);
 
   rockchip_atomic_commit_complete(private->commit);
   private->commit = NULL;
}
 
static int rockchip_drm_atomic_commit(struct drm_device *dev,
                     struct drm_atomic_state *state,
                     bool async)
{
   struct rockchip_drm_private *private = dev->dev_private;
   struct rockchip_atomic_commit *commit;
   size_t line_bw_mbyte;
   size_t frame_bw_mbyte;
   unsigned int plane_num;
   int ret;
 
   ret = drm_atomic_helper_setup_commit(state, false);
   if (ret)
       return ret;
 
   ret = drm_atomic_helper_prepare_planes(dev, state);
   if (ret)
       return ret;
 
   ret = rockchip_drm_bandwidth_atomic_check(dev, state,
                         &line_bw_mbyte,
                         &frame_bw_mbyte,
                         &plane_num);
   if (ret) {
       /*
        * TODO:
        * Just report bandwidth can't support now.
        */
       DRM_ERROR("vop bandwidth too large %zd\n", line_bw_mbyte);
   }
 
   ret = drm_atomic_helper_swap_state(state, true);
   if (ret < 0) {
       DRM_ERROR("swap atomic state for %s failed: %d\n", current->comm, ret);
       drm_atomic_helper_cleanup_planes(dev, state);
       return ret;
   }
 
   drm_atomic_state_get(state);
   commit = kmalloc(sizeof(*commit), GFP_KERNEL);
   if (!commit)
       return -ENOMEM;
 
   commit->dev = dev;
   commit->state = state;
   commit->line_bw_mbyte = line_bw_mbyte;
   commit->frame_bw_mbyte = frame_bw_mbyte;
   commit->plane_num = plane_num;
 
   if (async) {
       mutex_lock(&private->commit_lock);
 
       flush_work(&private->commit_work);
       WARN_ON(private->commit);
       private->commit = commit;
       schedule_work(&private->commit_work);
 
       mutex_unlock(&private->commit_lock);
   } else {
       rockchip_atomic_commit_complete(commit);
   }
 
   return 0;
}
 
static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
   .fb_create = rockchip_user_fb_create,
   .output_poll_changed = rockchip_drm_output_poll_changed,
   .atomic_check = drm_atomic_helper_check,
   .atomic_commit = rockchip_drm_atomic_commit,
};
 
struct drm_framebuffer *
rockchip_drm_framebuffer_init(struct drm_device *dev,
                 const struct drm_mode_fb_cmd2 *mode_cmd,
                 struct drm_gem_object *obj)
{
   struct drm_framebuffer *fb;
 
   fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
   if (IS_ERR(fb))
       return ERR_CAST(fb);
 
   return fb;
}
 
void rockchip_drm_mode_config_init(struct drm_device *dev)
{
   dev->mode_config.min_width = 0;
   dev->mode_config.min_height = 0;
 
   /*
    * set max width and height as default value(4096x4096).
    * this value would be used to check framebuffer size limitation
    * at drm_mode_addfb().
    */
   dev->mode_config.max_width = 8192;
   dev->mode_config.max_height = 8192;
   dev->mode_config.async_page_flip = true;
 
   dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
   dev->mode_config.helper_private = &rockchip_mode_config_helpers;
}