summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/loongson/lsdc_plane.c
blob: d227a2c1dcf16a3d5190de3893a55228ec70b254 (plain)
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2023 Loongson Technology Corporation Limited
 */

#include <linux/delay.h>

#include <drm/drm_atomic.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_gem_atomic_helper.h>

#include "lsdc_drv.h"
#include "lsdc_regs.h"
#include "lsdc_ttm.h"

static const u32 lsdc_primary_formats[] = {
	DRM_FORMAT_XRGB8888,
};

static const u32 lsdc_cursor_formats[] = {
	DRM_FORMAT_ARGB8888,
};

static const u64 lsdc_fb_format_modifiers[] = {
	DRM_FORMAT_MOD_LINEAR,
	DRM_FORMAT_MOD_INVALID
};

static unsigned int lsdc_get_fb_offset(struct drm_framebuffer *fb,
				       struct drm_plane_state *state)
{
	unsigned int offset = fb->offsets[0];

	offset += fb->format->cpp[0] * (state->src_x >> 16);
	offset += fb->pitches[0] * (state->src_y >> 16);

	return offset;
}

static u64 lsdc_fb_base_addr(struct drm_framebuffer *fb)
{
	struct lsdc_device *ldev = to_lsdc(fb->dev);
	struct lsdc_bo *lbo = gem_to_lsdc_bo(fb->obj[0]);

	return lsdc_bo_gpu_offset(lbo) + ldev->vram_base;
}

static int lsdc_primary_atomic_check(struct drm_plane *plane,
				     struct drm_atomic_state *state)
{
	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
	struct drm_crtc *crtc = new_plane_state->crtc;
	struct drm_crtc_state *new_crtc_state;

	if (!crtc)
		return 0;

	new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);

	return drm_atomic_helper_check_plane_state(new_plane_state,
						   new_crtc_state,
						   DRM_PLANE_NO_SCALING,
						   DRM_PLANE_NO_SCALING,
						   false, true);
}

static void lsdc_primary_atomic_update(struct drm_plane *plane,
				       struct drm_atomic_state *state)
{
	struct lsdc_primary *primary = to_lsdc_primary(plane);
	const struct lsdc_primary_plane_ops *ops = primary->ops;
	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
	struct drm_framebuffer *new_fb = new_plane_state->fb;
	struct drm_framebuffer *old_fb = old_plane_state->fb;
	u64 fb_addr = lsdc_fb_base_addr(new_fb);

	fb_addr += lsdc_get_fb_offset(new_fb, new_plane_state);

	ops->update_fb_addr(primary, fb_addr);
	ops->update_fb_stride(primary, new_fb->pitches[0]);

	if (!old_fb || old_fb->format != new_fb->format)
		ops->update_fb_format(primary, new_fb->format);
}

static void lsdc_primary_atomic_disable(struct drm_plane *plane,
					struct drm_atomic_state *state)
{
	/*
	 * Do nothing, just prevent call into atomic_update().
	 * Writing the format as LSDC_PF_NONE can disable the primary,
	 * But it seems not necessary...
	 */
	drm_dbg(plane->dev, "%s disabled\n", plane->name);
}

static int lsdc_plane_prepare_fb(struct drm_plane *plane,
				 struct drm_plane_state *new_state)
{
	struct drm_framebuffer *fb = new_state->fb;
	struct lsdc_bo *lbo;
	u64 gpu_vaddr;
	int ret;

	if (!fb)
		return 0;

	lbo = gem_to_lsdc_bo(fb->obj[0]);

	ret = lsdc_bo_reserve(lbo);
	if (unlikely(ret)) {
		drm_err(plane->dev, "bo %p reserve failed\n", lbo);
		return ret;
	}

	ret = lsdc_bo_pin(lbo, LSDC_GEM_DOMAIN_VRAM, &gpu_vaddr);

	lsdc_bo_unreserve(lbo);

	if (unlikely(ret)) {
		drm_err(plane->dev, "bo %p pin failed\n", lbo);
		return ret;
	}

	lsdc_bo_ref(lbo);

	if (plane->type != DRM_PLANE_TYPE_CURSOR)
		drm_dbg(plane->dev,
			"%s[%p] pin at 0x%llx, bo size: %zu\n",
			plane->name, lbo, gpu_vaddr, lsdc_bo_size(lbo));

	return drm_gem_plane_helper_prepare_fb(plane, new_state);
}

static void lsdc_plane_cleanup_fb(struct drm_plane *plane,
				  struct drm_plane_state *old_state)
{
	struct drm_framebuffer *fb = old_state->fb;
	struct lsdc_bo *lbo;
	int ret;

	if (!fb)
		return;

	lbo = gem_to_lsdc_bo(fb->obj[0]);

	ret = lsdc_bo_reserve(lbo);
	if (unlikely(ret)) {
		drm_err(plane->dev, "%p reserve failed\n", lbo);
		return;
	}

	lsdc_bo_unpin(lbo);

	lsdc_bo_unreserve(lbo);

	lsdc_bo_unref(lbo);

	if (plane->type != DRM_PLANE_TYPE_CURSOR)
		drm_dbg(plane->dev, "%s unpin\n", plane->name);
}

static const struct drm_plane_helper_funcs lsdc_primary_helper_funcs = {
	.prepare_fb = lsdc_plane_prepare_fb,
	.cleanup_fb = lsdc_plane_cleanup_fb,
	.atomic_check = lsdc_primary_atomic_check,
	.atomic_update = lsdc_primary_atomic_update,
	.atomic_disable = lsdc_primary_atomic_disable,
};

static int lsdc_cursor_plane_atomic_async_check(struct drm_plane *plane,
						struct drm_atomic_state *state)
{
	struct drm_plane_state *new_state;
	struct drm_crtc_state *crtc_state;

	new_state = drm_atomic_get_new_plane_state(state, plane);

	if (!plane->state || !plane->state->fb) {
		drm_dbg(plane->dev, "%s: state is NULL\n", plane->name);
		return -EINVAL;
	}

	if (new_state->crtc_w != new_state->crtc_h) {
		drm_dbg(plane->dev, "unsupported cursor size: %ux%u\n",
			new_state->crtc_w, new_state->crtc_h);
		return -EINVAL;
	}

	if (new_state->crtc_w != 64 && new_state->crtc_w != 32) {
		drm_dbg(plane->dev, "unsupported cursor size: %ux%u\n",
			new_state->crtc_w, new_state->crtc_h);
		return -EINVAL;
	}

	crtc_state = drm_atomic_get_existing_crtc_state(state, new_state->crtc);
	if (!crtc_state->active)
		return -EINVAL;

	if (plane->state->crtc != new_state->crtc ||
	    plane->state->src_w != new_state->src_w ||
	    plane->state->src_h != new_state->src_h ||
	    plane->state->crtc_w != new_state->crtc_w ||
	    plane->state->crtc_h != new_state->crtc_h)
		return -EINVAL;

	if (new_state->visible != plane->state->visible)
		return -EINVAL;

	return drm_atomic_helper_check_plane_state(plane->state,
						   crtc_state,
						   DRM_PLANE_NO_SCALING,
						   DRM_PLANE_NO_SCALING,
						   true, true);
}

static void lsdc_cursor_plane_atomic_async_update(struct drm_plane *plane,
						  struct drm_atomic_state *state)
{
	struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
	const struct lsdc_cursor_plane_ops *ops = cursor->ops;
	struct drm_framebuffer *old_fb = plane->state->fb;
	struct drm_framebuffer *new_fb;
	struct drm_plane_state *new_state;

	new_state = drm_atomic_get_new_plane_state(state, plane);

	new_fb = plane->state->fb;

	plane->state->crtc_x = new_state->crtc_x;
	plane->state->crtc_y = new_state->crtc_y;
	plane->state->crtc_h = new_state->crtc_h;
	plane->state->crtc_w = new_state->crtc_w;
	plane->state->src_x = new_state->src_x;
	plane->state->src_y = new_state->src_y;
	plane->state->src_h = new_state->src_h;
	plane->state->src_w = new_state->src_w;
	swap(plane->state->fb, new_state->fb);

	if (new_state->visible) {
		enum lsdc_cursor_size cursor_size;

		switch (new_state->crtc_w) {
		case 64:
			cursor_size = CURSOR_SIZE_64X64;
			break;
		case 32:
			cursor_size = CURSOR_SIZE_32X32;
			break;
		default:
			cursor_size = CURSOR_SIZE_32X32;
			break;
		}

		ops->update_position(cursor, new_state->crtc_x, new_state->crtc_y);

		ops->update_cfg(cursor, cursor_size, CURSOR_FORMAT_ARGB8888);

		if (!old_fb || old_fb != new_fb)
			ops->update_bo_addr(cursor, lsdc_fb_base_addr(new_fb));
	}
}

/* ls7a1000 cursor plane helpers */

static int ls7a1000_cursor_plane_atomic_check(struct drm_plane *plane,
					      struct drm_atomic_state *state)
{
	struct drm_plane_state *new_plane_state;
	struct drm_crtc_state *new_crtc_state;
	struct drm_crtc *crtc;

	new_plane_state = drm_atomic_get_new_plane_state(state, plane);

	crtc = new_plane_state->crtc;
	if (!crtc) {
		drm_dbg(plane->dev, "%s is not bind to a crtc\n", plane->name);
		return 0;
	}

	if (new_plane_state->crtc_w != 32 || new_plane_state->crtc_h != 32) {
		drm_dbg(plane->dev, "unsupported cursor size: %ux%u\n",
			new_plane_state->crtc_w, new_plane_state->crtc_h);
		return -EINVAL;
	}

	new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);

	return drm_atomic_helper_check_plane_state(new_plane_state,
						   new_crtc_state,
						   DRM_PLANE_NO_SCALING,
						   DRM_PLANE_NO_SCALING,
						   true, true);
}

static void ls7a1000_cursor_plane_atomic_update(struct drm_plane *plane,
						struct drm_atomic_state *state)
{
	struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
	struct drm_framebuffer *new_fb = new_plane_state->fb;
	struct drm_framebuffer *old_fb = old_plane_state->fb;
	const struct lsdc_cursor_plane_ops *ops = cursor->ops;
	u64 addr = lsdc_fb_base_addr(new_fb);

	if (!new_plane_state->visible)
		return;

	ops->update_position(cursor, new_plane_state->crtc_x, new_plane_state->crtc_y);

	if (!old_fb || old_fb != new_fb)
		ops->update_bo_addr(cursor, addr);

	ops->update_cfg(cursor, CURSOR_SIZE_32X32, CURSOR_FORMAT_ARGB8888);
}

static void ls7a1000_cursor_plane_atomic_disable(struct drm_plane *plane,
						 struct drm_atomic_state *state)
{
	struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
	const struct lsdc_cursor_plane_ops *ops = cursor->ops;

	ops->update_cfg(cursor, CURSOR_SIZE_32X32, CURSOR_FORMAT_DISABLE);
}

static const struct drm_plane_helper_funcs ls7a1000_cursor_plane_helper_funcs = {
	.prepare_fb = lsdc_plane_prepare_fb,
	.cleanup_fb = lsdc_plane_cleanup_fb,
	.atomic_check = ls7a1000_cursor_plane_atomic_check,
	.atomic_update = ls7a1000_cursor_plane_atomic_update,
	.atomic_disable = ls7a1000_cursor_plane_atomic_disable,
	.atomic_async_check = lsdc_cursor_plane_atomic_async_check,
	.atomic_async_update = lsdc_cursor_plane_atomic_async_update,
};

/* ls7a2000 cursor plane helpers */

static int ls7a2000_cursor_plane_atomic_check(struct drm_plane *plane,
					      struct drm_atomic_state *state)
{
	struct drm_plane_state *new_plane_state;
	struct drm_crtc_state *new_crtc_state;
	struct drm_crtc *crtc;

	new_plane_state = drm_atomic_get_new_plane_state(state, plane);

	crtc = new_plane_state->crtc;
	if (!crtc) {
		drm_dbg(plane->dev, "%s is not bind to a crtc\n", plane->name);
		return 0;
	}

	if (new_plane_state->crtc_w != new_plane_state->crtc_h) {
		drm_dbg(plane->dev, "unsupported cursor size: %ux%u\n",
			new_plane_state->crtc_w, new_plane_state->crtc_h);
		return -EINVAL;
	}

	if (new_plane_state->crtc_w != 64 && new_plane_state->crtc_w != 32) {
		drm_dbg(plane->dev, "unsupported cursor size: %ux%u\n",
			new_plane_state->crtc_w, new_plane_state->crtc_h);
		return -EINVAL;
	}

	new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);

	return drm_atomic_helper_check_plane_state(new_plane_state,
						   new_crtc_state,
						   DRM_PLANE_NO_SCALING,
						   DRM_PLANE_NO_SCALING,
						   true, true);
}

/* Update the format, size and location of the cursor */

static void ls7a2000_cursor_plane_atomic_update(struct drm_plane *plane,
						struct drm_atomic_state *state)
{
	struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
	struct drm_framebuffer *new_fb = new_plane_state->fb;
	struct drm_framebuffer *old_fb = old_plane_state->fb;
	const struct lsdc_cursor_plane_ops *ops = cursor->ops;
	enum lsdc_cursor_size cursor_size;

	if (!new_plane_state->visible)
		return;

	ops->update_position(cursor, new_plane_state->crtc_x, new_plane_state->crtc_y);

	if (!old_fb || new_fb != old_fb) {
		u64 addr = lsdc_fb_base_addr(new_fb);

		ops->update_bo_addr(cursor, addr);
	}

	switch (new_plane_state->crtc_w) {
	case 64:
		cursor_size = CURSOR_SIZE_64X64;
		break;
	case 32:
		cursor_size = CURSOR_SIZE_32X32;
		break;
	default:
		cursor_size = CURSOR_SIZE_64X64;
		break;
	}

	ops->update_cfg(cursor, cursor_size, CURSOR_FORMAT_ARGB8888);
}

static void ls7a2000_cursor_plane_atomic_disable(struct drm_plane *plane,
						 struct drm_atomic_state *state)
{
	struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
	const struct lsdc_cursor_plane_ops *hw_ops = cursor->ops;

	hw_ops->update_cfg(cursor, CURSOR_SIZE_64X64, CURSOR_FORMAT_DISABLE);
}

static const struct drm_plane_helper_funcs ls7a2000_cursor_plane_helper_funcs = {
	.prepare_fb = lsdc_plane_prepare_fb,
	.cleanup_fb = lsdc_plane_cleanup_fb,
	.atomic_check = ls7a2000_cursor_plane_atomic_check,
	.atomic_update = ls7a2000_cursor_plane_atomic_update,
	.atomic_disable = ls7a2000_cursor_plane_atomic_disable,
	.atomic_async_check = lsdc_cursor_plane_atomic_async_check,
	.atomic_async_update = lsdc_cursor_plane_atomic_async_update,
};

static void lsdc_plane_atomic_print_state(struct drm_printer *p,
					  const struct drm_plane_state *state)
{
	struct drm_framebuffer *fb = state->fb;
	u64 addr;

	if (!fb)
		return;

	addr = lsdc_fb_base_addr(fb);

	drm_printf(p, "\tdma addr=%llx\n", addr);
}

static const struct drm_plane_funcs lsdc_plane_funcs = {
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
	.destroy = drm_plane_cleanup,
	.reset = drm_atomic_helper_plane_reset,
	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
	.atomic_print_state = lsdc_plane_atomic_print_state,
};

/* Primary plane 0 hardware related ops  */

static void lsdc_primary0_update_fb_addr(struct lsdc_primary *primary, u64 addr)
{
	struct lsdc_device *ldev = primary->ldev;
	u32 status;
	u32 lo, hi;

	/* 40-bit width physical address bus */
	lo = addr & 0xFFFFFFFF;
	hi = (addr >> 32) & 0xFF;

	status = lsdc_rreg32(ldev, LSDC_CRTC0_CFG_REG);
	if (status & FB_REG_IN_USING) {
		lsdc_wreg32(ldev, LSDC_CRTC0_FB1_ADDR_LO_REG, lo);
		lsdc_wreg32(ldev, LSDC_CRTC0_FB1_ADDR_HI_REG, hi);
	} else {
		lsdc_wreg32(ldev, LSDC_CRTC0_FB0_ADDR_LO_REG, lo);
		lsdc_wreg32(ldev, LSDC_CRTC0_FB0_ADDR_HI_REG, hi);
	}
}

static void lsdc_primary0_update_fb_stride(struct lsdc_primary *primary, u32 stride)
{
	struct lsdc_device *ldev = primary->ldev;

	lsdc_wreg32(ldev, LSDC_CRTC0_STRIDE_REG, stride);
}

static void lsdc_primary0_update_fb_format(struct lsdc_primary *primary,
					   const struct drm_format_info *format)
{
	struct lsdc_device *ldev = primary->ldev;
	u32 status;

	status = lsdc_rreg32(ldev, LSDC_CRTC0_CFG_REG);

	/*
	 * TODO: add RGB565 support, only support XRBG8888 at present
	 */
	status &= ~CFG_PIX_FMT_MASK;
	status |= LSDC_PF_XRGB8888;

	lsdc_wreg32(ldev, LSDC_CRTC0_CFG_REG, status);
}

/* Primary plane 1 hardware related ops */

static void lsdc_primary1_update_fb_addr(struct lsdc_primary *primary, u64 addr)
{
	struct lsdc_device *ldev = primary->ldev;
	u32 status;
	u32 lo, hi;

	/* 40-bit width physical address bus */
	lo = addr & 0xFFFFFFFF;
	hi = (addr >> 32) & 0xFF;

	status = lsdc_rreg32(ldev, LSDC_CRTC1_CFG_REG);
	if (status & FB_REG_IN_USING) {
		lsdc_wreg32(ldev, LSDC_CRTC1_FB1_ADDR_LO_REG, lo);
		lsdc_wreg32(ldev, LSDC_CRTC1_FB1_ADDR_HI_REG, hi);
	} else {
		lsdc_wreg32(ldev, LSDC_CRTC1_FB0_ADDR_LO_REG, lo);
		lsdc_wreg32(ldev, LSDC_CRTC1_FB0_ADDR_HI_REG, hi);
	}
}

static void lsdc_primary1_update_fb_stride(struct lsdc_primary *primary, u32 stride)
{
	struct lsdc_device *ldev = primary->ldev;

	lsdc_wreg32(ldev, LSDC_CRTC1_STRIDE_REG, stride);
}

static void lsdc_primary1_update_fb_format(struct lsdc_primary *primary,
					   const struct drm_format_info *format)
{
	struct lsdc_device *ldev = primary->ldev;
	u32 status;

	status = lsdc_rreg32(ldev, LSDC_CRTC1_CFG_REG);

	/*
	 * TODO: add RGB565 support, only support XRBG8888 at present
	 */
	status &= ~CFG_PIX_FMT_MASK;
	status |= LSDC_PF_XRGB8888;

	lsdc_wreg32(ldev, LSDC_CRTC1_CFG_REG, status);
}

static const struct lsdc_primary_plane_ops lsdc_primary_plane_hw_ops[2] = {
	{
		.update_fb_addr = lsdc_primary0_update_fb_addr,
		.update_fb_stride = lsdc_primary0_update_fb_stride,
		.update_fb_format = lsdc_primary0_update_fb_format,
	},
	{
		.update_fb_addr = lsdc_primary1_update_fb_addr,
		.update_fb_stride = lsdc_primary1_update_fb_stride,
		.update_fb_format = lsdc_primary1_update_fb_format,
	},
};

/*
 * Update location, format, enable and disable state of the cursor,
 * For those who have two hardware cursor, let cursor 0 is attach to CRTC-0,
 * cursor 1 is attach to CRTC-1. Compositing the primary plane and cursor
 * plane is automatically done by hardware, the cursor is alway on the top of
 * the primary plane. In other word, z-order is fixed in hardware and cannot
 * be changed. For those old DC who has only one hardware cursor, we made it
 * shared by the two screen, this works on extend screen mode.
 */

/* cursor plane 0 (for pipe 0) related hardware ops */

static void lsdc_cursor0_update_bo_addr(struct lsdc_cursor *cursor, u64 addr)
{
	struct lsdc_device *ldev = cursor->ldev;

	/* 40-bit width physical address bus */
	lsdc_wreg32(ldev, LSDC_CURSOR0_ADDR_HI_REG, (addr >> 32) & 0xFF);
	lsdc_wreg32(ldev, LSDC_CURSOR0_ADDR_LO_REG, addr);
}

static void lsdc_cursor0_update_position(struct lsdc_cursor *cursor, int x, int y)
{
	struct lsdc_device *ldev = cursor->ldev;

	if (x < 0)
		x = 0;

	if (y < 0)
		y = 0;

	lsdc_wreg32(ldev, LSDC_CURSOR0_POSITION_REG, (y << 16) | x);
}

static void lsdc_cursor0_update_cfg(struct lsdc_cursor *cursor,
				    enum lsdc_cursor_size cursor_size,
				    enum lsdc_cursor_format fmt)
{
	struct lsdc_device *ldev = cursor->ldev;
	u32 cfg;

	cfg = CURSOR_ON_CRTC0 << CURSOR_LOCATION_SHIFT |
	      cursor_size << CURSOR_SIZE_SHIFT |
	      fmt << CURSOR_FORMAT_SHIFT;

	lsdc_wreg32(ldev, LSDC_CURSOR0_CFG_REG, cfg);
}

/* cursor plane 1 (for pipe 1) related hardware ops */

static void lsdc_cursor1_update_bo_addr(struct lsdc_cursor *cursor, u64 addr)
{
	struct lsdc_device *ldev = cursor->ldev;

	/* 40-bit width physical address bus */
	lsdc_wreg32(ldev, LSDC_CURSOR1_ADDR_HI_REG, (addr >> 32) & 0xFF);
	lsdc_wreg32(ldev, LSDC_CURSOR1_ADDR_LO_REG, addr);
}

static void lsdc_cursor1_update_position(struct lsdc_cursor *cursor, int x, int y)
{
	struct lsdc_device *ldev = cursor->ldev;

	if (x < 0)
		x = 0;

	if (y < 0)
		y = 0;

	lsdc_wreg32(ldev, LSDC_CURSOR1_POSITION_REG, (y << 16) | x);
}

static void lsdc_cursor1_update_cfg(struct lsdc_cursor *cursor,
				    enum lsdc_cursor_size cursor_size,
				    enum lsdc_cursor_format fmt)
{
	struct lsdc_device *ldev = cursor->ldev;
	u32 cfg;

	cfg = CURSOR_ON_CRTC1 << CURSOR_LOCATION_SHIFT |
	      cursor_size << CURSOR_SIZE_SHIFT |
	      fmt << CURSOR_FORMAT_SHIFT;

	lsdc_wreg32(ldev, LSDC_CURSOR1_CFG_REG, cfg);
}

/* The hardware cursors become normal since ls7a2000/ls2k2000 */

static const struct lsdc_cursor_plane_ops ls7a2000_cursor_hw_ops[2] = {
	{
		.update_bo_addr = lsdc_cursor0_update_bo_addr,
		.update_cfg = lsdc_cursor0_update_cfg,
		.update_position = lsdc_cursor0_update_position,
	},
	{
		.update_bo_addr = lsdc_cursor1_update_bo_addr,
		.update_cfg = lsdc_cursor1_update_cfg,
		.update_position = lsdc_cursor1_update_position,
	},
};

/* Quirks for cursor 1, only for old loongson display controller */

static void lsdc_cursor1_update_bo_addr_quirk(struct lsdc_cursor *cursor, u64 addr)
{
	struct lsdc_device *ldev = cursor->ldev;

	/* 40-bit width physical address bus */
	lsdc_wreg32(ldev, LSDC_CURSOR0_ADDR_HI_REG, (addr >> 32) & 0xFF);
	lsdc_wreg32(ldev, LSDC_CURSOR0_ADDR_LO_REG, addr);
}

static void lsdc_cursor1_update_position_quirk(struct lsdc_cursor *cursor, int x, int y)
{
	struct lsdc_device *ldev = cursor->ldev;

	if (x < 0)
		x = 0;

	if (y < 0)
		y = 0;

	lsdc_wreg32(ldev, LSDC_CURSOR0_POSITION_REG, (y << 16) | x);
}

static void lsdc_cursor1_update_cfg_quirk(struct lsdc_cursor *cursor,
					  enum lsdc_cursor_size cursor_size,
					  enum lsdc_cursor_format fmt)
{
	struct lsdc_device *ldev = cursor->ldev;
	u32 cfg;

	cfg = CURSOR_ON_CRTC1 << CURSOR_LOCATION_SHIFT |
	      cursor_size << CURSOR_SIZE_SHIFT |
	      fmt << CURSOR_FORMAT_SHIFT;

	lsdc_wreg32(ldev, LSDC_CURSOR0_CFG_REG, cfg);
}

/*
 * The unforgiving LS7A1000/LS2K1000 has only one hardware cursors plane
 */
static const struct lsdc_cursor_plane_ops ls7a1000_cursor_hw_ops[2] = {
	{
		.update_bo_addr = lsdc_cursor0_update_bo_addr,
		.update_cfg = lsdc_cursor0_update_cfg,
		.update_position = lsdc_cursor0_update_position,
	},
	{
		.update_bo_addr = lsdc_cursor1_update_bo_addr_quirk,
		.update_cfg = lsdc_cursor1_update_cfg_quirk,
		.update_position = lsdc_cursor1_update_position_quirk,
	},
};

int lsdc_primary_plane_init(struct drm_device *ddev,
			    struct drm_plane *plane,
			    unsigned int index)
{
	struct lsdc_primary *primary = to_lsdc_primary(plane);
	int ret;

	ret = drm_universal_plane_init(ddev, plane, 1 << index,
				       &lsdc_plane_funcs,
				       lsdc_primary_formats,
				       ARRAY_SIZE(lsdc_primary_formats),
				       lsdc_fb_format_modifiers,
				       DRM_PLANE_TYPE_PRIMARY,
				       "ls-primary-plane-%u", index);
	if (ret)
		return ret;

	drm_plane_helper_add(plane, &lsdc_primary_helper_funcs);

	primary->ldev = to_lsdc(ddev);
	primary->ops = &lsdc_primary_plane_hw_ops[index];

	return 0;
}

int ls7a1000_cursor_plane_init(struct drm_device *ddev,
			       struct drm_plane *plane,
			       unsigned int index)
{
	struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
	int ret;

	ret = drm_universal_plane_init(ddev, plane, 1 << index,
				       &lsdc_plane_funcs,
				       lsdc_cursor_formats,
				       ARRAY_SIZE(lsdc_cursor_formats),
				       lsdc_fb_format_modifiers,
				       DRM_PLANE_TYPE_CURSOR,
				       "ls-cursor-plane-%u", index);
	if (ret)
		return ret;

	cursor->ldev = to_lsdc(ddev);
	cursor->ops = &ls7a1000_cursor_hw_ops[index];

	drm_plane_helper_add(plane, &ls7a1000_cursor_plane_helper_funcs);

	return 0;
}

int ls7a2000_cursor_plane_init(struct drm_device *ddev,
			       struct drm_plane *plane,
			       unsigned int index)
{
	struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
	int ret;

	ret = drm_universal_plane_init(ddev, plane, 1 << index,
				       &lsdc_plane_funcs,
				       lsdc_cursor_formats,
				       ARRAY_SIZE(lsdc_cursor_formats),
				       lsdc_fb_format_modifiers,
				       DRM_PLANE_TYPE_CURSOR,
				       "ls-cursor-plane-%u", index);
	if (ret)
		return ret;

	cursor->ldev = to_lsdc(ddev);
	cursor->ops = &ls7a2000_cursor_hw_ops[index];

	drm_plane_helper_add(plane, &ls7a2000_cursor_plane_helper_funcs);

	return 0;
}