forked from torvalds/linux
/
dcp.c
1536 lines (1254 loc) · 42.9 KB
/
dcp.c
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright 2021 Alyssa Rosenzweig <alyssa@rosenzweig.io> */
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/of_device.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/iommu.h>
#include <linux/align.h>
#include <linux/apple-mailbox.h>
#include <linux/soc/apple/rtkit.h>
#include <drm/drm_fb_cma_helper.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_probe_helper.h>
#include <drm/drm_vblank.h>
#include "dcpep.h"
#include "dcp.h"
#include "parser.h"
struct apple_dcp;
/* Register defines used in bandwidth setup structure */
#define REG_SCRATCH (0x14)
#define REG_SCRATCH_T600X (0x988)
#define REG_DOORBELL (0x0)
#define REG_DOORBELL_BIT (2)
#define DCP_BOOT_TIMEOUT msecs_to_jiffies(1000)
/* Limit on call stack depth (arbitrary). Some nesting is required */
#define DCP_MAX_CALL_DEPTH 8
typedef void (*dcp_callback_t)(struct apple_dcp *, void *, void *);
struct dcp_call_channel {
dcp_callback_t callbacks[DCP_MAX_CALL_DEPTH];
void *cookies[DCP_MAX_CALL_DEPTH];
void *output[DCP_MAX_CALL_DEPTH];
u16 end[DCP_MAX_CALL_DEPTH];
/* Current depth of the call stack. Less than DCP_MAX_CALL_DEPTH */
u8 depth;
};
struct dcp_cb_channel {
u8 depth;
void *output[DCP_MAX_CALL_DEPTH];
};
/* Temporary backing for a chunked transfer via setDCPAVPropStart/Chunk/End */
struct dcp_chunks {
size_t length;
void *data;
};
#define DCP_MAX_MAPPINGS (128) /* should be enough */
#define MAX_DISP_REGISTERS (7)
struct apple_dcp {
struct device *dev;
struct platform_device *piodma;
struct apple_rtkit *rtk;
struct apple_crtc *crtc;
struct apple_connector *connector;
/* mask for DCP IO virtual addresses shared over rtkit */
u64 asc_dram_mask;
/* DCP shared memory */
void *shmem;
/* Display registers mappable to the DCP */
struct resource *disp_registers[MAX_DISP_REGISTERS];
unsigned int nr_disp_registers;
/* Number of memory mappings made by the DCP, used as an ID */
u32 nr_mappings;
/* Indexed table of mappings */
struct sg_table mappings[DCP_MAX_MAPPINGS];
struct dcp_call_channel ch_cmd, ch_oobcmd;
struct dcp_cb_channel ch_cb, ch_oobcb, ch_async;
/* Active chunked transfer. There can only be one at a time. */
struct dcp_chunks chunks;
/* Queued swap. Owned by the DCP to avoid per-swap memory allocation */
struct dcp_swap_submit_req swap;
/* Current display mode */
bool valid_mode;
struct dcp_set_digital_out_mode_req mode;
/* Is the DCP booted? */
bool active;
/* Modes valid for the connected display */
struct dcp_display_mode *modes;
unsigned int nr_modes;
/* Attributes of the connected display */
int width_mm, height_mm;
/* Workqueue for sending vblank events when a dcp swap is not possible */
struct work_struct vblank_wq;
/* List of referenced drm_framebuffers which can be unreferenced
* on the next successfully completed swap.
*/
struct list_head swapped_out_fbs;
};
struct dcp_fb_reference {
struct list_head head;
struct drm_framebuffer *fb;
};
/*
* A channel is busy if we have sent a message that has yet to be
* acked. The driver must not sent a message to a busy channel.
*/
static bool dcp_channel_busy(struct dcp_call_channel *ch)
{
return (ch->depth != 0);
}
/* Get a call channel for a context */
static struct dcp_call_channel *
dcp_get_call_channel(struct apple_dcp *dcp, enum dcp_context_id context)
{
switch (context) {
case DCP_CONTEXT_CMD:
case DCP_CONTEXT_CB:
return &dcp->ch_cmd;
case DCP_CONTEXT_OOBCMD:
case DCP_CONTEXT_OOBCB:
return &dcp->ch_oobcmd;
default:
return NULL;
}
}
/*
* Get the context ID passed to the DCP for a command we push. The rule is
* simple: callback contexts are used when replying to the DCP, command
* contexts are used otherwise. That corresponds to a non/zero call stack
* depth. This rule frees the caller from tracking the call context manually.
*/
static enum dcp_context_id dcp_call_context(struct apple_dcp *dcp, bool oob)
{
u8 depth = oob ? dcp->ch_oobcmd.depth : dcp->ch_cmd.depth;
if (depth)
return oob ? DCP_CONTEXT_OOBCB : DCP_CONTEXT_CB;
else
return oob ? DCP_CONTEXT_OOBCMD : DCP_CONTEXT_CMD;
}
/* Get a callback channel for a context */
struct dcp_cb_channel *dcp_get_cb_channel(struct apple_dcp *dcp,
enum dcp_context_id context)
{
switch (context) {
case DCP_CONTEXT_CB:
return &dcp->ch_cb;
case DCP_CONTEXT_OOBCB:
return &dcp->ch_oobcb;
case DCP_CONTEXT_ASYNC:
return &dcp->ch_async;
default:
return NULL;
}
}
/* Get the start of a packet: after the end of the previous packet */
static u16 dcp_packet_start(struct dcp_call_channel *ch, u8 depth)
{
if (depth > 0)
return ch->end[depth - 1];
else
return 0;
}
/* Pushes and pops the depth of the call stack with safety checks */
static u8 dcp_push_depth(u8 *depth)
{
u8 ret = (*depth)++;
WARN_ON(ret >= DCP_MAX_CALL_DEPTH);
return ret;
}
static u8 dcp_pop_depth(u8 *depth)
{
WARN_ON((*depth) == 0);
return --(*depth);
}
#define DCP_METHOD(tag, name) [name] = { #name, tag }
const struct dcp_method_entry dcp_methods[dcpep_num_methods] = {
DCP_METHOD("A000", dcpep_late_init_signal),
DCP_METHOD("A029", dcpep_setup_video_limits),
DCP_METHOD("A034", dcpep_update_notify_clients_dcp),
DCP_METHOD("A357", dcpep_set_create_dfb),
DCP_METHOD("A401", dcpep_start_signal),
DCP_METHOD("A407", dcpep_swap_start),
DCP_METHOD("A408", dcpep_swap_submit),
DCP_METHOD("A410", dcpep_set_display_device),
DCP_METHOD("A412", dcpep_set_digital_out_mode),
DCP_METHOD("A443", dcpep_create_default_fb),
DCP_METHOD("A454", dcpep_first_client_open),
DCP_METHOD("A460", dcpep_set_display_refresh_properties),
DCP_METHOD("A463", dcpep_flush_supports_power),
DCP_METHOD("A468", dcpep_set_power_state),
};
/* Call a DCP function given by a tag */
void dcp_push(struct apple_dcp *dcp, bool oob, enum dcpep_method method,
u32 in_len, u32 out_len, void *data, dcp_callback_t cb,
void *cookie)
{
struct dcp_call_channel *ch = oob ? &dcp->ch_oobcmd : &dcp->ch_cmd;
enum dcp_context_id context = dcp_call_context(dcp, oob);
struct dcp_packet_header header = {
.in_len = in_len,
.out_len = out_len,
/* Tag is reversed due to endianness of the fourcc */
.tag[0] = dcp_methods[method].tag[3],
.tag[1] = dcp_methods[method].tag[2],
.tag[2] = dcp_methods[method].tag[1],
.tag[3] = dcp_methods[method].tag[0],
};
u8 depth = dcp_push_depth(&ch->depth);
u16 offset = dcp_packet_start(ch, depth);
void *out = dcp->shmem + dcp_tx_offset(context) + offset;
void *out_data = out + sizeof(header);
size_t data_len = sizeof(header) + in_len + out_len;
memcpy(out, &header, sizeof(header));
if (in_len > 0)
memcpy(out_data, data, in_len);
dev_dbg(dcp->dev, "---> %s: context %u, offset %u, depth %u\n",
dcp_methods[method].name, context, offset, depth);
ch->callbacks[depth] = cb;
ch->cookies[depth] = cookie;
ch->output[depth] = out + sizeof(header) + in_len;
ch->end[depth] = offset + ALIGN(data_len, DCP_PACKET_ALIGNMENT);
apple_rtkit_send_message(dcp->rtk, DCP_ENDPOINT,
dcpep_msg(context, data_len, offset));
}
#define DCP_THUNK_VOID(func, handle) \
static void func(struct apple_dcp *dcp, bool oob, dcp_callback_t cb, \
void *cookie) \
{ \
dcp_push(dcp, oob, handle, 0, 0, NULL, cb, cookie); \
}
#define DCP_THUNK_OUT(func, handle, T) \
static void func(struct apple_dcp *dcp, bool oob, dcp_callback_t cb, \
void *cookie) \
{ \
dcp_push(dcp, oob, handle, 0, sizeof(T), NULL, cb, cookie); \
}
#define DCP_THUNK_IN(func, handle, T) \
static void func(struct apple_dcp *dcp, bool oob, T *data, \
dcp_callback_t cb, void *cookie) \
{ \
dcp_push(dcp, oob, handle, sizeof(T), 0, data, cb, cookie); \
}
#define DCP_THUNK_INOUT(func, handle, T_in, T_out) \
static void func(struct apple_dcp *dcp, bool oob, T_in *data, \
dcp_callback_t cb, void *cookie) \
{ \
dcp_push(dcp, oob, handle, sizeof(T_in), sizeof(T_out), data, \
cb, cookie); \
}
DCP_THUNK_INOUT(dcp_swap_submit, dcpep_swap_submit, struct dcp_swap_submit_req,
struct dcp_swap_submit_resp);
DCP_THUNK_INOUT(dcp_swap_start, dcpep_swap_start, struct dcp_swap_start_req,
struct dcp_swap_start_resp);
DCP_THUNK_INOUT(dcp_set_power_state, dcpep_set_power_state,
struct dcp_set_power_state_req,
struct dcp_set_power_state_resp);
DCP_THUNK_INOUT(dcp_set_digital_out_mode, dcpep_set_digital_out_mode,
struct dcp_set_digital_out_mode_req, u32);
DCP_THUNK_INOUT(dcp_set_display_device, dcpep_set_display_device, u32, u32);
DCP_THUNK_OUT(dcp_set_display_refresh_properties,
dcpep_set_display_refresh_properties, u32);
DCP_THUNK_OUT(dcp_late_init_signal, dcpep_late_init_signal, u32);
DCP_THUNK_IN(dcp_flush_supports_power, dcpep_flush_supports_power, u32);
DCP_THUNK_OUT(dcp_create_default_fb, dcpep_create_default_fb, u32);
DCP_THUNK_OUT(dcp_start_signal, dcpep_start_signal, u32);
DCP_THUNK_VOID(dcp_setup_video_limits, dcpep_setup_video_limits);
DCP_THUNK_VOID(dcp_set_create_dfb, dcpep_set_create_dfb);
DCP_THUNK_VOID(dcp_first_client_open, dcpep_first_client_open);
DCP_THUNK_IN(dcp_update_notify_clients_dcp, dcpep_update_notify_clients_dcp,
struct dcp_update_notify_clients_dcp);
/* Parse a callback tag "D123" into the ID 123. Returns -EINVAL on failure. */
static int dcp_parse_tag(char tag[4])
{
u32 d[3];
int i;
if (tag[3] != 'D')
return -EINVAL;
for (i = 0; i < 3; ++i) {
d[i] = (u32)(tag[i] - '0');
if (d[i] > 9)
return -EINVAL;
}
return d[0] + (d[1] * 10) + (d[2] * 100);
}
/* Ack a callback from the DCP */
void dcp_ack(struct apple_dcp *dcp, enum dcp_context_id context)
{
struct dcp_cb_channel *ch = dcp_get_cb_channel(dcp, context);
dcp_pop_depth(&ch->depth);
apple_rtkit_send_message(dcp->rtk, DCP_ENDPOINT, dcpep_ack(context));
}
/* DCP callback handlers */
static void dcpep_cb_nop(struct apple_dcp *dcp)
{
/* No operation */
}
static u8 dcpep_cb_true(struct apple_dcp *dcp)
{
return true;
}
static u8 dcpep_cb_false(struct apple_dcp *dcp)
{
return false;
}
static u32 dcpep_cb_zero(struct apple_dcp *dcp)
{
return 0;
}
/* HACK: moved here to avoid circular dependency between apple_drv and dcp */
void dcp_drm_crtc_vblank(struct apple_crtc *crtc)
{
unsigned long flags;
if (crtc->vsync_disabled)
return;
drm_crtc_handle_vblank(&crtc->base);
spin_lock_irqsave(&crtc->base.dev->event_lock, flags);
if (crtc->event) {
drm_crtc_send_vblank_event(&crtc->base, crtc->event);
drm_crtc_vblank_put(&crtc->base);
crtc->event = NULL;
}
spin_unlock_irqrestore(&crtc->base.dev->event_lock, flags);
}
static void dcpep_cb_swap_complete(struct apple_dcp *dcp)
{
dcp_drm_crtc_vblank(dcp->crtc);
}
static struct dcp_get_uint_prop_resp
dcpep_cb_get_uint_prop(struct apple_dcp *dcp, struct dcp_get_uint_prop_req *req)
{
/* unimplemented for now */
return (struct dcp_get_uint_prop_resp) {
.value = 0
};
}
/*
* Callback to map a buffer allocated with allocate_buf for PIODMA usage.
* PIODMA is separate from the main DCP and uses own IOVA space on a dedicated
* stream of the display DART, rather than the expected DCP DART.
*
* XXX: This relies on dma_get_sgtable in concert with dma_map_sgtable, which
* is a "fundamentally unsafe" operation according to the docs. And yet
* everyone does it...
*/
static struct dcp_map_buf_resp
dcpep_cb_map_piodma(struct apple_dcp *dcp, struct dcp_map_buf_req *req)
{
struct sg_table *map;
int ret;
if (req->buffer >= ARRAY_SIZE(dcp->mappings))
goto reject;
map = &dcp->mappings[req->buffer];
if (!map->sgl)
goto reject;
/* Use PIODMA device instead of DCP to map against the right IOMMU. */
ret = dma_map_sgtable(&dcp->piodma->dev, map, DMA_BIDIRECTIONAL, 0);
if (ret)
goto reject;
return (struct dcp_map_buf_resp) {
.dva = sg_dma_address(map->sgl)
};
reject:
dev_err(dcp->dev, "denying map of invalid buffer %llx for pidoma\n",
req->buffer);
return (struct dcp_map_buf_resp) {
.ret = EINVAL
};
}
/*
* Allocate an IOVA contiguous buffer mapped to the DCP. The buffer need not be
* physically contigiuous, however we should save the sgtable in case the
* buffer needs to be later mapped for PIODMA.
*/
static struct dcp_allocate_buffer_resp
dcpep_cb_allocate_buffer(struct apple_dcp *dcp, struct dcp_allocate_buffer_req *req)
{
struct dcp_allocate_buffer_resp resp = { 0 };
void *buf;
resp.dva_size = ALIGN(req->size, 4096);
resp.mem_desc_id = ++dcp->nr_mappings;
if (resp.mem_desc_id >= ARRAY_SIZE(dcp->mappings)) {
dev_warn(dcp->dev, "DCP overflowed mapping table, ignoring");
return resp;
}
buf = dma_alloc_coherent(dcp->dev, resp.dva_size, &resp.dva,
GFP_KERNEL);
dma_get_sgtable(dcp->dev, &dcp->mappings[resp.mem_desc_id], buf,
resp.dva, resp.dva_size);
resp.dva |= dcp->asc_dram_mask;
return resp;
}
/* Validate that the specified region is a display register */
static bool is_disp_register(struct apple_dcp *dcp, u64 start, u64 end)
{
int i;
for (i = 0; i < dcp->nr_disp_registers; ++i) {
struct resource *r = dcp->disp_registers[i];
if ((start >= r->start) && (end <= r->end))
return true;
}
return false;
}
/*
* Map contiguous physical memory into the DCP's address space. The firmware
* uses this to map the display registers we advertise in
* sr_map_device_memory_with_index, so we bounds check against that to guard
* safe against malicious coprocessors.
*/
static struct dcp_map_physical_resp
dcpep_cb_map_physical(struct apple_dcp *dcp, struct dcp_map_physical_req *req)
{
int size = ALIGN(req->size, 4096);
if (!is_disp_register(dcp, req->paddr, req->paddr + size - 1)) {
dev_err(dcp->dev, "refusing to map phys address %llx size %llx",
req->paddr, req->size);
return (struct dcp_map_physical_resp) { };
}
return (struct dcp_map_physical_resp) {
.dva_size = size,
.mem_desc_id = ++dcp->nr_mappings,
.dva = dma_map_resource(dcp->dev, req->paddr, size,
DMA_BIDIRECTIONAL, 0) | dcp->asc_dram_mask,
};
}
static u64 dcpep_cb_get_frequency(struct apple_dcp *dcp)
{
/* Pixel clock frequency in Hz (compare: 4K@60 VGA clock 533.250 MHz) */
return 533333328;
}
static struct dcp_map_reg_resp
dcpep_cb_map_reg(struct apple_dcp *dcp, struct dcp_map_reg_req *req)
{
if (req->index >= dcp->nr_disp_registers) {
dev_warn(dcp->dev, "attempted to read invalid reg index %u",
req->index);
return (struct dcp_map_reg_resp) {
.ret = 1
};
} else {
struct resource *rsrc = dcp->disp_registers[req->index];
return (struct dcp_map_reg_resp) {
.addr = rsrc->start,
.length = resource_size(rsrc)
};
}
}
/* Chunked data transfer for property dictionaries */
static u8 dcpep_cb_prop_start(struct apple_dcp *dcp, u32 *length)
{
if (dcp->chunks.data != NULL) {
dev_warn(dcp->dev, "ignoring spurious transfer start\n");
return false;
}
dcp->chunks.length = *length;
dcp->chunks.data = devm_kzalloc(dcp->dev, *length, GFP_KERNEL);
if (!dcp->chunks.data) {
dev_warn(dcp->dev, "failed to allocate chunks\n");
return false;
}
return true;
}
static u8 dcpep_cb_prop_chunk(struct apple_dcp *dcp,
struct dcp_set_dcpav_prop_chunk_req *req)
{
if (!dcp->chunks.data) {
dev_warn(dcp->dev, "ignoring spurious chunk\n");
return false;
}
if (req->offset + req->length > dcp->chunks.length) {
dev_warn(dcp->dev, "ignoring overflowing chunk\n");
return false;
}
memcpy(dcp->chunks.data + req->offset, req->data, req->length);
return true;
}
static void dcp_set_dimensions(struct apple_dcp *dcp)
{
int i;
/* Set the connector info */
if (dcp->connector) {
struct drm_connector *connector = &dcp->connector->base;
mutex_lock(&connector->dev->mode_config.mutex);
connector->display_info.width_mm = dcp->width_mm;
connector->display_info.height_mm = dcp->height_mm;
mutex_unlock(&connector->dev->mode_config.mutex);
}
/*
* Fix up any probed modes. Modes are created when parsing
* TimingElements, dimensions are calculated when parsing
* DisplayAttributes, and TimingElements may be sent first
*/
for (i = 0; i < dcp->nr_modes; ++i) {
dcp->modes[i].mode.width_mm = dcp->width_mm;
dcp->modes[i].mode.height_mm = dcp->height_mm;
}
}
static bool dcpep_process_chunks(struct apple_dcp *dcp,
struct dcp_set_dcpav_prop_end_req *req)
{
struct dcp_parse_ctx ctx;
int ret;
if (!dcp->chunks.data) {
dev_warn(dcp->dev, "ignoring spurious end\n");
return false;
}
ret = parse(dcp->chunks.data, dcp->chunks.length, &ctx);
if (ret) {
dev_warn(dcp->dev, "bad header on dcpav props\n");
return false;
}
if (!strcmp(req->key, "TimingElements")) {
dcp->modes = enumerate_modes(&ctx, &dcp->nr_modes,
dcp->width_mm, dcp->height_mm);
if (IS_ERR(dcp->modes)) {
dev_warn(dcp->dev, "failed to parse modes\n");
dcp->modes = NULL;
dcp->nr_modes = 0;
return false;
}
} else if (!strcmp(req->key, "DisplayAttributes")) {
ret = parse_display_attributes(&ctx, &dcp->width_mm,
&dcp->height_mm);
if (ret) {
dev_warn(dcp->dev, "failed to parse display attribs\n");
return false;
}
dcp_set_dimensions(dcp);
}
return true;
}
static u8 dcpep_cb_prop_end(struct apple_dcp *dcp,
struct dcp_set_dcpav_prop_end_req *req)
{
u8 resp = dcpep_process_chunks(dcp, req);
/* Reset for the next transfer */
devm_kfree(dcp->dev, dcp->chunks.data);
dcp->chunks.data = NULL;
return resp;
}
/* Boot sequence */
static void boot_done(struct apple_dcp *dcp, void *out, void *cookie)
{
struct dcp_cb_channel *ch = &dcp->ch_cb;
u8 *succ = ch->output[ch->depth - 1];
*succ = true;
dcp_ack(dcp, DCP_CONTEXT_CB);
}
static void boot_5(struct apple_dcp *dcp, void *out, void *cookie)
{
dcp_set_display_refresh_properties(dcp, false, boot_done, NULL);
}
static void boot_4(struct apple_dcp *dcp, void *out, void *cookie)
{
dcp_late_init_signal(dcp, false, boot_5, NULL);
}
static void boot_3(struct apple_dcp *dcp, void *out, void *cookie)
{
u32 v_true = true;
dcp_flush_supports_power(dcp, false, &v_true, boot_4, NULL);
}
static void boot_2(struct apple_dcp *dcp, void *out, void *cookie)
{
dcp_setup_video_limits(dcp, false, boot_3, NULL);
}
static void boot_1_5(struct apple_dcp *dcp, void *out, void *cookie)
{
dcp_create_default_fb(dcp, false, boot_2, NULL);
}
/* Use special function signature to defer the ACK */
static bool dcpep_cb_boot_1(struct apple_dcp *dcp, void *out, void *in)
{
dcp_set_create_dfb(dcp, false, boot_1_5, NULL);
return false;
}
static struct dcp_rt_bandwidth dcpep_cb_rt_bandwidth(struct apple_dcp *dcp)
{
if (dcp->disp_registers[5] && dcp->disp_registers[6])
return (struct dcp_rt_bandwidth) {
.reg_scratch = dcp->disp_registers[5]->start + REG_SCRATCH,
.reg_doorbell = dcp->disp_registers[6]->start + REG_DOORBELL,
.doorbell_bit = REG_DOORBELL_BIT,
.padding[3] = 0x4, // XXX: required by 11.x firmware
};
else if (dcp->disp_registers[4])
return (struct dcp_rt_bandwidth) {
.reg_scratch = dcp->disp_registers[4]->start + REG_SCRATCH_T600X,
.reg_doorbell = 0,
.doorbell_bit = 0,
};
else
return (struct dcp_rt_bandwidth) {
.reg_scratch = 0,
.reg_doorbell = 0,
.doorbell_bit = 0,
};
}
/* Callback to get the current time as milliseconds since the UNIX epoch */
static u64 dcpep_cb_get_time(struct apple_dcp *dcp)
{
return ktime_to_ms(ktime_get_real());
}
/*
* Helper to send a DRM hotplug event. The DCP is accessed from a single
* (RTKit) thread. To handle hotplug callbacks, we need to call
* drm_kms_helper_hotplug_event, which does an atomic commit (via DCP) and
* waits for vblank (a DCP callback). That means we deadlock if we call from
* the RTKit thread! Instead, move the call to another thread via a workqueue.
*/
void dcp_hotplug(struct work_struct *work)
{
struct apple_connector *connector;
struct drm_device *dev;
connector = container_of(work, struct apple_connector, hotplug_wq);
dev = connector->base.dev;
/*
* DCP defers link training until we set a display mode. But we set
* display modes from atomic_flush, so userspace needs to trigger a
* flush, or the CRTC gets no signal.
*/
if (connector->connected) {
drm_connector_set_link_status_property(
&connector->base, DRM_MODE_LINK_STATUS_BAD);
}
if (dev && dev->registered)
drm_kms_helper_hotplug_event(dev);
}
EXPORT_SYMBOL_GPL(dcp_hotplug);
static void dcpep_cb_hotplug(struct apple_dcp *dcp, u64 *connected)
{
struct apple_connector *connector = dcp->connector;
/* Hotplug invalidates mode. DRM doesn't always handle this. */
dcp->valid_mode = false;
if (connector) {
connector->connected = !!(*connected);
schedule_work(&connector->hotplug_wq);
}
}
/*
* Helper to send a DRM vblank event. We do not know how call swap_submit_dcp
* without surfaces. To avoid timeouts in drm_atomic_helper_wait_for_vblanks
* send a vblank event via a workqueue.
*/
void dcp_delayed_vblank(struct work_struct *work)
{
struct apple_dcp *dcp;
dcp = container_of(work, struct apple_dcp, vblank_wq);
mdelay(5);
dcp_drm_crtc_vblank(dcp->crtc);
}
#define DCPEP_MAX_CB (1000)
/*
* Define type-safe trampolines. Define typedefs to enforce type-safety on the
* input data (so if the types don't match, gcc errors out).
*/
#define TRAMPOLINE_VOID(func, handler) \
static bool func(struct apple_dcp *dcp, void *out, void *in) \
{ \
dev_dbg(dcp->dev, "received callback %s\n", #handler); \
handler(dcp); \
return true; \
}
#define TRAMPOLINE_IN(func, handler, T_in) \
typedef void (*callback_##name)(struct apple_dcp *, T_in *); \
\
static bool func(struct apple_dcp *dcp, void *out, void *in) \
{ \
callback_##name cb = handler; \
\
dev_dbg(dcp->dev, "received callback %s\n", #handler); \
cb(dcp, in); \
return true; \
}
#define TRAMPOLINE_INOUT(func, handler, T_in, T_out) \
typedef T_out (*callback_##handler)(struct apple_dcp *, T_in *); \
\
static bool func(struct apple_dcp *dcp, void *out, void *in) \
{ \
T_out *typed_out = out; \
callback_##handler cb = handler; \
\
dev_dbg(dcp->dev, "received callback %s\n", #handler); \
*typed_out = cb(dcp, in); \
return true; \
}
#define TRAMPOLINE_OUT(func, handler, T_out) \
static bool func(struct apple_dcp *dcp, void *out, void *in) \
{ \
T_out *typed_out = out; \
\
dev_dbg(dcp->dev, "received callback %s\n", #handler); \
*typed_out = handler(dcp); \
return true; \
}
TRAMPOLINE_VOID(trampoline_nop, dcpep_cb_nop);
TRAMPOLINE_OUT(trampoline_true, dcpep_cb_true, u8);
TRAMPOLINE_OUT(trampoline_false, dcpep_cb_false, u8);
TRAMPOLINE_OUT(trampoline_zero, dcpep_cb_zero, u32);
TRAMPOLINE_VOID(trampoline_swap_complete, dcpep_cb_swap_complete);
TRAMPOLINE_INOUT(trampoline_get_uint_prop, dcpep_cb_get_uint_prop,
struct dcp_get_uint_prop_req, struct dcp_get_uint_prop_resp);
TRAMPOLINE_INOUT(trampoline_map_piodma, dcpep_cb_map_piodma,
struct dcp_map_buf_req, struct dcp_map_buf_resp);
TRAMPOLINE_INOUT(trampoline_allocate_buffer, dcpep_cb_allocate_buffer,
struct dcp_allocate_buffer_req,
struct dcp_allocate_buffer_resp);
TRAMPOLINE_INOUT(trampoline_map_physical, dcpep_cb_map_physical,
struct dcp_map_physical_req, struct dcp_map_physical_resp);
TRAMPOLINE_INOUT(trampoline_map_reg, dcpep_cb_map_reg, struct dcp_map_reg_req,
struct dcp_map_reg_resp);
TRAMPOLINE_INOUT(trampoline_prop_start, dcpep_cb_prop_start, u32, u8);
TRAMPOLINE_INOUT(trampoline_prop_chunk, dcpep_cb_prop_chunk,
struct dcp_set_dcpav_prop_chunk_req, u8);
TRAMPOLINE_INOUT(trampoline_prop_end, dcpep_cb_prop_end,
struct dcp_set_dcpav_prop_end_req, u8);
TRAMPOLINE_OUT(trampoline_rt_bandwidth, dcpep_cb_rt_bandwidth,
struct dcp_rt_bandwidth);
TRAMPOLINE_OUT(trampoline_get_frequency, dcpep_cb_get_frequency, u64);
TRAMPOLINE_OUT(trampoline_get_time, dcpep_cb_get_time, u64);
TRAMPOLINE_IN(trampoline_hotplug, dcpep_cb_hotplug, u64);
bool (*const dcpep_cb_handlers[DCPEP_MAX_CB])(struct apple_dcp *, void *, void *) = {
[0] = trampoline_true, /* did_boot_signal */
[1] = trampoline_true, /* did_power_on_signal */
[2] = trampoline_nop, /* will_power_off_signal */
[3] = trampoline_rt_bandwidth,
[100] = trampoline_nop, /* match_pmu_service */
[101] = trampoline_zero, /* get_display_default_stride */
[103] = trampoline_nop, /* set_boolean_property */
[106] = trampoline_nop, /* remove_property */
[107] = trampoline_true, /* create_provider_service */
[108] = trampoline_true, /* create_product_service */
[109] = trampoline_true, /* create_pmu_service */
[110] = trampoline_true, /* create_iomfb_service */
[111] = trampoline_false, /* create_backlight_service */
[116] = dcpep_cb_boot_1,
[118] = trampoline_false, /* is_dark_boot / is_waking_from_hibernate*/
[120] = trampoline_false, /* read_edt_data */
[122] = trampoline_prop_start,
[123] = trampoline_prop_chunk,
[124] = trampoline_prop_end,
[201] = trampoline_map_piodma,
[206] = trampoline_true, /* match_pmu_service_2 */
[207] = trampoline_true, /* match_backlight_service */
[208] = trampoline_get_time,
[211] = trampoline_nop, /* update_backlight_factor_prop */
[300] = trampoline_nop, /* pr_publish */
[401] = trampoline_get_uint_prop,
[404] = trampoline_nop, /* sr_set_uint_prop */
[406] = trampoline_nop, /* set_fx_prop */
[408] = trampoline_get_frequency,
[411] = trampoline_map_reg,
[413] = trampoline_true, /* sr_set_property_dict */
[414] = trampoline_true, /* sr_set_property_int */
[415] = trampoline_true, /* sr_set_property_bool */
[451] = trampoline_allocate_buffer,
[452] = trampoline_map_physical,
[552] = trampoline_true, /* set_property_dict_0 */
[561] = trampoline_true, /* set_property_dict */
[563] = trampoline_true, /* set_property_int */
[565] = trampoline_true, /* set_property_bool */
[567] = trampoline_true, /* set_property_str */
[574] = trampoline_zero, /* power_up_dart */
[576] = trampoline_hotplug,
[577] = trampoline_nop, /* powerstate_notify */
[582] = trampoline_true, /* create_default_fb_surface */
[589] = trampoline_swap_complete,
[591] = trampoline_nop, /* swap_complete_intent_gated */
[598] = trampoline_nop, /* find_swap_function_gated */
};
static void dcpep_handle_cb(struct apple_dcp *dcp, enum dcp_context_id context,
void *data, u32 length)
{
struct device *dev = dcp->dev;
struct dcp_packet_header *hdr = data;
void *in, *out;
int tag = dcp_parse_tag(hdr->tag);
struct dcp_cb_channel *ch = dcp_get_cb_channel(dcp, context);
u8 depth;
if (tag < 0 || tag >= DCPEP_MAX_CB || !dcpep_cb_handlers[tag]) {
dev_warn(dev, "received unknown callback %c%c%c%c\n",
hdr->tag[3], hdr->tag[2], hdr->tag[1], hdr->tag[0]);
return;
}
in = data + sizeof(*hdr);
out = in + hdr->in_len;
depth = dcp_push_depth(&ch->depth);
ch->output[depth] = out;
if (dcpep_cb_handlers[tag](dcp, out, in))
dcp_ack(dcp, context);
}
static void dcpep_handle_ack(struct apple_dcp *dcp, enum dcp_context_id context,
void *data, u32 length)
{
struct dcp_packet_header *header = data;
struct dcp_call_channel *ch = dcp_get_call_channel(dcp, context);
void *cookie;
dcp_callback_t cb;
if (!ch) {
dev_warn(dcp->dev, "ignoring ack on context %X\n", context);
return;
}
dcp_pop_depth(&ch->depth);
cb = ch->callbacks[ch->depth];
cookie = ch->cookies[ch->depth];
if (cb)
cb(dcp, data + sizeof(*header) + header->in_len, cookie);
}
static void dcpep_got_msg(struct apple_dcp *dcp, u64 message)
{
enum dcp_context_id ctx_id;
u16 offset;
u32 length;
int channel_offset;
void *data;
ctx_id = (message & DCPEP_CONTEXT_MASK) >> DCPEP_CONTEXT_SHIFT;
offset = (message & DCPEP_OFFSET_MASK) >> DCPEP_OFFSET_SHIFT;
length = (message >> DCPEP_LENGTH_SHIFT);
channel_offset = dcp_channel_offset(ctx_id);
if (channel_offset < 0) {
dev_warn(dcp->dev, "invalid context received %u", ctx_id);
return;
}
data = dcp->shmem + channel_offset + offset;
if (message & DCPEP_ACK)
dcpep_handle_ack(dcp, ctx_id, data, length);
else
dcpep_handle_cb(dcp, ctx_id, data, length);
}
/*
* Callback for swap requests. If a swap failed, we'll never get a swap
* complete event so we need to fake a vblank event early to avoid a hang.
*/
static void dcp_swapped(struct apple_dcp *dcp, void *data, void *cookie)
{
struct dcp_swap_submit_resp *resp = data;
if (resp->ret) {