summaryrefslogtreecommitdiff
path: root/components/drivers/dma/dma_pool.c
blob: 75e28cced694248912e7695e6523fa3b6a3dbffa (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-25     GuEe-GUI     the first version
 */

/**
 * @file dma_pool.c
 * @brief DMA memory pool and buffer allocation framework
 *
 * Manages DMA-capable memory regions (coherent pool and CMA) for
 * allocating DMA buffers. Provides coherent and non-coherent buffer
 * allocation with optional device tree "memory-region" integration,
 * cache synchronization via the rt_dma_map_ops interface, and a
 * bitmap-based page allocator within DMA pools.
 *
 * Pools are organized as:
 * - coherent-pool: smaller pool for cache-coherent DMA (ioremap_cached)
 * - CMA (Contiguous Memory Allocator): larger pool for general DMA
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "dma.pool"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#include <mmu.h>
#include <mm_aspace.h>
#include <mm_memblock.h>
#include <dt-bindings/size.h>

static RT_DEFINE_SPINLOCK(dma_pools_lock);
static rt_list_t dma_pool_nodes = RT_LIST_OBJECT_INIT(dma_pool_nodes);

static struct rt_dma_pool *dma_pool_install(rt_region_t *region);
static rt_err_t dma_pool_map_linear(struct rt_dma_pool *pool);

static void *dma_alloc(struct rt_device *dev, rt_size_t size,
                       rt_ubase_t *dma_handle, rt_ubase_t flags);
static void dma_free(struct rt_device *dev, rt_size_t size,
                     void *cpu_addr, rt_ubase_t dma_handle, rt_ubase_t flags);

/** @brief Acquire the DMA pool spinlock */
rt_inline void region_pool_lock(void)
{
    rt_hw_spin_lock(&dma_pools_lock.lock);
}

/** @brief Release the DMA pool spinlock */
rt_inline void region_pool_unlock(void)
{
    rt_hw_spin_unlock(&dma_pools_lock.lock);
}

/**
 * @brief Synchronize output data for a coherent DMA device
 *
 * Converts the virtual address to a physical DMA handle and flushes
 * the CPU data cache to ensure the DMA engine sees the latest data.
 *
 * @param[in]  dev        DMA-capable device
 * @param[in]  data       Virtual address of the data buffer
 * @param[in]  size       Buffer size in bytes
 * @param[out] dma_handle Physical DMA address (filled if non-NULL)
 * @param[in]  flags      Allocation flags (unused)
 *
 * @return RT_EOK
 */
static rt_err_t dma_map_coherent_sync_out_data(struct rt_device *dev,
                                               void *data, rt_size_t size, rt_ubase_t *dma_handle, rt_ubase_t flags)
{
    if (dma_handle)
    {
        *dma_handle = (rt_ubase_t)rt_kmem_v2p(data);
    }
    rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, data, size);

    return RT_EOK;
}

/**
 * @brief Synchronize input data for a coherent DMA device
 *
 * Invalidates the CPU data cache so the CPU sees the data written
 * by the DMA engine.
 *
 * @param[in] dev        DMA-capable device
 * @param[in] out_data   Virtual address of the data buffer
 * @param[in] size       Buffer size in bytes
 * @param[in] dma_handle Physical DMA address (unused)
 * @param[in] flags      Allocation flags (unused)
 *
 * @return RT_EOK
 */
static rt_err_t dma_map_coherent_sync_in_data(struct rt_device *dev,
                                              void *out_data, rt_size_t size, rt_ubase_t dma_handle, rt_ubase_t flags)
{
    rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, out_data, size);

    return RT_EOK;
}

/** @brief DMA map operations for cache-coherent devices */
static const struct rt_dma_map_ops dma_map_coherent_ops = {
    .sync_out_data = dma_map_coherent_sync_out_data,
    .sync_in_data = dma_map_coherent_sync_in_data,
};

/**
 * @brief Synchronize output data for a non-coherent DMA device
 *
 * Converts virtual address to physical DMA handle. No cache flush
 * is performed since the memory is non-cacheable.
 *
 * @param[in]  dev        DMA-capable device
 * @param[in]  data       Virtual address of the data buffer
 * @param[in]  size       Buffer size in bytes
 * @param[out] dma_handle Physical DMA address (filled if non-NULL)
 * @param[in]  flags      Allocation flags (unused)
 *
 * @return RT_EOK
 */
static rt_err_t dma_map_nocoherent_sync_out_data(struct rt_device *dev,
                                                 void *data, rt_size_t size, rt_ubase_t *dma_handle, rt_ubase_t flags)
{
    if (dma_handle)
    {
        *dma_handle = (rt_ubase_t)rt_kmem_v2p(data);
    }

    return RT_EOK;
}

/**
 * @brief Synchronize input data for a non-coherent DMA device
 *
 * No cache operations needed for non-cacheable memory.
 *
 * @param[in] dev        DMA-capable device
 * @param[in] out_data   Virtual address of the data buffer
 * @param[in] size       Buffer size in bytes
 * @param[in] dma_handle Physical DMA address (unused)
 * @param[in] flags      Allocation flags (unused)
 *
 * @return RT_EOK
 */
static rt_err_t dma_map_nocoherent_sync_in_data(struct rt_device *dev,
                                                void *out_data, rt_size_t size, rt_ubase_t dma_handle, rt_ubase_t flags)
{
    return RT_EOK;
}

/** @brief DMA map operations for non-cache-coherent devices */
static const struct rt_dma_map_ops dma_map_nocoherent_ops = {
    .sync_out_data = dma_map_nocoherent_sync_out_data,
    .sync_in_data = dma_map_nocoherent_sync_in_data,
};

#ifdef RT_USING_OFW

/**
 * @brief Translate a CPU address to a DMA address via device tree
 *
 * @param[in] dev  Device with an ofw_node
 * @param[in] addr CPU physical address
 *
 * @return DMA address
 */
rt_inline rt_ubase_t ofw_addr_cpu2dma(struct rt_device *dev, rt_ubase_t addr)
{
    return (rt_ubase_t)rt_ofw_translate_cpu2dma(dev->ofw_node, addr);
}

/**
 * @brief Translate a DMA address to a CPU address via device tree
 *
 * @param[in] dev  Device with an ofw_node
 * @param[in] addr DMA address
 *
 * @return CPU physical address
 */
rt_inline rt_ubase_t ofw_addr_dma2cpu(struct rt_device *dev, rt_ubase_t addr)
{
    return (rt_ubase_t)rt_ofw_translate_dma2cpu(dev->ofw_node, addr);
}

/**
 * @brief Allocate a DMA buffer and translate to DMA address space
 *
 * @param[in]  dev        DMA-capable device
 * @param[in]  size       Requested size in bytes
 * @param[out] dma_handle DMA address (device tree translated)
 * @param[in]  flags      Allocation flags
 *
 * @return Virtual address of the buffer, or RT_NULL on failure
 */
static void *ofw_dma_map_alloc(struct rt_device *dev, rt_size_t size,
                               rt_ubase_t *dma_handle, rt_ubase_t flags)
{
    void *cpu_addr;

    flags |= RT_DMA_F_32BITS;
    cpu_addr = dma_alloc(dev, size, dma_handle, flags);

    if (cpu_addr && dma_handle)
    {
        *dma_handle = ofw_addr_cpu2dma(dev, *dma_handle);
    }

    return cpu_addr;
}

/**
 * @brief Free a DMA buffer, translating DMA address back to CPU space
 *
 * @param[in] dev        DMA-capable device
 * @param[in] size       Buffer size in bytes
 * @param[in] cpu_addr   Virtual address of the buffer
 * @param[in] dma_handle DMA address (will be translated back to CPU)
 * @param[in] flags      Allocation flags
 */
static void ofw_dma_map_free(struct rt_device *dev, rt_size_t size,
                             void *cpu_addr, rt_ubase_t dma_handle, rt_ubase_t flags)
{
    dma_handle = ofw_addr_dma2cpu(dev, dma_handle);

    dma_free(dev, size, cpu_addr, dma_handle, flags);
}

/**
 * @brief Sync output data with device tree DMA address translation
 *
 * Chooses coherent or non-coherent sync based on RT_DMA_F_NOCACHE flag,
 * then translates the DMA handle to device bus address space.
 *
 * @param[in]  dev        DMA-capable device
 * @param[in]  data       Virtual address of the data buffer
 * @param[in]  size       Buffer size in bytes
 * @param[out] dma_handle DMA address (translated)
 * @param[in]  flags      Sync flags
 *
 * @return RT_EOK on success
 */
static rt_err_t ofw_dma_map_sync_out_data(struct rt_device *dev,
                                          void *data, rt_size_t size,
                                          rt_ubase_t *dma_handle, rt_ubase_t flags)
{
    rt_err_t err;

    if (flags & RT_DMA_F_NOCACHE)
    {
        err = dma_map_nocoherent_sync_out_data(dev, data, size, dma_handle, flags);
    }
    else
    {
        err = dma_map_coherent_sync_out_data(dev, data, size, dma_handle, flags);
    }

    if (!err && dma_handle)
    {
        *dma_handle = ofw_addr_cpu2dma(dev, *dma_handle);
    }

    return err;
}

/**
 * @brief Sync input data with device tree DMA address back-translation
 *
 * Translates the DMA handle from bus address space back to CPU space,
 * then performs coherent or non-coherent cache invalidation.
 *
 * @param[in] dev        DMA-capable device
 * @param[in] out_data   Virtual address of the data buffer
 * @param[in] size       Buffer size in bytes
 * @param[in] dma_handle DMA address (translated back from bus space)
 * @param[in] flags      Sync flags
 *
 * @return RT_EOK on success
 */
static rt_err_t ofw_dma_map_sync_in_data(struct rt_device *dev,
                                         void *out_data, rt_size_t size,
                                         rt_ubase_t dma_handle, rt_ubase_t flags)
{
    dma_handle = ofw_addr_dma2cpu(dev, dma_handle);

    if (flags & RT_DMA_F_NOCACHE)
    {
        return dma_map_nocoherent_sync_in_data(dev, out_data, size, dma_handle, flags);
    }

    return dma_map_coherent_sync_in_data(dev, out_data, size, dma_handle, flags);
}

/** @brief DMA map operations with device tree address translation */
static const struct rt_dma_map_ops ofw_dma_map_ops = {
    .alloc = ofw_dma_map_alloc,
    .free = ofw_dma_map_free,
    .sync_out_data = ofw_dma_map_sync_out_data,
    .sync_in_data = ofw_dma_map_sync_in_data,
};

static rt_bool_t ofw_node_has_dma_ranges(struct rt_ofw_node *np)
{
    struct rt_ofw_node *bus;

    for (bus = np ? np->parent : RT_NULL; bus; bus = bus->parent)
    {
        rt_ssize_t len;

        if (rt_ofw_get_prop(bus, "dma-ranges", &len) && len)
        {
            return RT_TRUE;
        }
    }

    return RT_FALSE;
}

/**
 * @brief Determine DMA operations for a device tree device
 *
 * Parses the "memory-region" property to install DMA pools from
 * reserved-memory nodes. Sets RT_DMA_F_NOMAP if "no-map" is set,
 * and RT_DMA_F_NOCACHE if the device is not DMA-coherent.
 *
 * Also installs ofw_dma_map_ops when the parent bus chain provides
 * "dma-ranges", so CPU physical addresses are translated for DMA.
 *
 * @param[in] dev  Device to configure DMA operations for
 *
 * @return Pointer to DMA map ops (ofw_dma_map_ops) when translation
 *         or memory-region pools apply, RT_NULL otherwise
 */
static const struct rt_dma_map_ops *ofw_device_dma_ops(struct rt_device *dev)
{
    rt_err_t err;
    int region_nr = 0;
    const fdt32_t *cell;
    rt_phandle phandle;
    rt_region_t region;
    struct rt_ofw_prop *prop;
    struct rt_dma_pool *dma_pool;
    const struct rt_dma_map_ops *ops = RT_NULL;
    struct rt_ofw_node *mem_np, *np = dev->ofw_node;

    rt_ofw_foreach_prop_u32(np, "memory-region", prop, cell, phandle)
    {
        rt_uint64_t addr, size;

        if (!(mem_np = rt_ofw_find_node_by_phandle(phandle)))
        {
            continue;
        }

        if ((err = rt_ofw_get_address(mem_np, 0, &addr, &size)))
        {
            LOG_E("%s: Read '%s' error = %s", rt_ofw_node_full_name(mem_np),
                  "memory-region", rt_strerror(err));

            rt_ofw_node_put(mem_np);
            continue;
        }

        region.start = addr;
        region.end = addr + size;
        region.name = rt_dm_dev_get_name(dev);

        rt_ofw_node_put(mem_np);

        if (!(dma_pool = dma_pool_install(&region)))
        {
            return RT_NULL;
        }

        if (rt_ofw_prop_read_bool(mem_np, "no-map"))
        {
            dma_pool->flags |= RT_DMA_F_NOMAP;
        }

        if (!rt_dma_device_is_coherent(dev))
        {
            dma_pool->flags |= RT_DMA_F_NOCACHE;
        }

        dma_pool->dev = dev;
        ++region_nr;
    }

    if (region_nr || ofw_node_has_dma_ranges(np))
    {
        ops = &ofw_dma_map_ops;
    }

    return ops;
}
#endif /* RT_USING_OFW */

#ifdef RT_USING_PCI
static struct rt_pci_host_bridge *pci_device_host_bridge(struct rt_device *dev)
{
    struct rt_pci_device *pdev = rt_container_of(dev, struct rt_pci_device, parent);

    return rt_pci_find_host_bridge(pdev->bus);
}

static rt_uint64_t pci_host_cpu_to_bus(struct rt_pci_host_bridge *host_bridge, rt_uint64_t cpu_addr)
{
    if (!host_bridge || !host_bridge->dma_regions)
    {
        return ~0ULL;
    }

    for (int i = 0; i < host_bridge->dma_regions_nr; ++i)
    {
        struct rt_pci_bus_region *region = &host_bridge->dma_regions[i];

        if (cpu_addr >= region->cpu_addr &&
            cpu_addr < region->cpu_addr + region->size)
        {
            return region->phy_addr + (cpu_addr - region->cpu_addr);
        }
    }

    return ~0ULL;
}

static rt_uint64_t pci_host_bus_to_cpu(struct rt_pci_host_bridge *host_bridge, rt_uint64_t pci_addr)
{
    if (!host_bridge || !host_bridge->dma_regions)
    {
        return ~0ULL;
    }

    for (int i = 0; i < host_bridge->dma_regions_nr; ++i)
    {
        struct rt_pci_bus_region *region = &host_bridge->dma_regions[i];

        if (pci_addr >= region->phy_addr && pci_addr < region->phy_addr + region->size)
        {
            return region->cpu_addr + (pci_addr - region->phy_addr);
        }
    }

    return ~0ULL;
}

static rt_ubase_t pci_addr_cpu2dma(struct rt_device *dev, rt_ubase_t addr)
{
    rt_uint64_t pci_dma;
    struct rt_pci_host_bridge *host_bridge = pci_device_host_bridge(dev);

    if (!host_bridge)
    {
        return addr;
    }

    pci_dma = pci_host_cpu_to_bus(host_bridge, addr);

    return pci_dma == ~0ULL ? addr : (rt_ubase_t)pci_dma;
}

static rt_ubase_t pci_addr_dma2cpu(struct rt_device *dev, rt_ubase_t addr)
{
    rt_uint64_t cpu_addr;
    struct rt_pci_host_bridge *host_bridge = pci_device_host_bridge(dev);

    if (!host_bridge)
    {
        return addr;
    }

    cpu_addr = pci_host_bus_to_cpu(host_bridge, addr);

    return cpu_addr == ~0ULL ? addr : (rt_ubase_t)cpu_addr;
}

static void *pci_dma_map_alloc(struct rt_device *dev, rt_size_t size,
                               rt_ubase_t *dma_handle, rt_ubase_t flags)
{
    void *cpu_addr;

    flags |= RT_DMA_F_32BITS;
    cpu_addr = dma_alloc(dev, size, dma_handle, flags);

    if (cpu_addr && dma_handle)
    {
        *dma_handle = pci_addr_cpu2dma(dev, *dma_handle);
    }

    return cpu_addr;
}

static void pci_dma_map_free(struct rt_device *dev, rt_size_t size,
                             void *cpu_addr, rt_ubase_t dma_handle, rt_ubase_t flags)
{
    dma_handle = pci_addr_dma2cpu(dev, dma_handle);

    dma_free(dev, size, cpu_addr, dma_handle, flags);
}

static rt_err_t pci_dma_map_sync_out_data(struct rt_device *dev,
                                          void *data, rt_size_t size,
                                          rt_ubase_t *dma_handle, rt_ubase_t flags)
{
    rt_err_t err;

    if (flags & RT_DMA_F_NOCACHE)
    {
        err = dma_map_nocoherent_sync_out_data(dev, data, size, dma_handle, flags);
    }
    else
    {
        err = dma_map_coherent_sync_out_data(dev, data, size, dma_handle, flags);
    }

    if (!err && dma_handle)
    {
        *dma_handle = pci_addr_cpu2dma(dev, *dma_handle);
    }

    return err;
}

static rt_err_t pci_dma_map_sync_in_data(struct rt_device *dev,
                                         void *out_data, rt_size_t size,
                                         rt_ubase_t dma_handle, rt_ubase_t flags)
{
    dma_handle = pci_addr_dma2cpu(dev, dma_handle);

    if (flags & RT_DMA_F_NOCACHE)
    {
        return dma_map_nocoherent_sync_in_data(dev, out_data, size, dma_handle, flags);
    }

    return dma_map_coherent_sync_in_data(dev, out_data, size, dma_handle, flags);
}

static const struct rt_dma_map_ops pci_dma_map_ops = {
    .alloc = pci_dma_map_alloc,
    .free = pci_dma_map_free,
    .sync_out_data = pci_dma_map_sync_out_data,
    .sync_in_data = pci_dma_map_sync_in_data,
};

static const struct rt_dma_map_ops *pci_device_dma_ops(struct rt_device *dev)
{
    struct rt_bus *bus = dev->bus;
    struct rt_pci_host_bridge *host_bridge;

    if (!bus || rt_strcmp(bus->name, "pci"))
    {
        return RT_NULL;
    }

    host_bridge = pci_device_host_bridge(dev);

    if (!host_bridge || !host_bridge->dma_regions_nr)
    {
        return RT_NULL;
    }

    return &pci_dma_map_ops;
}
#endif /* RT_USING_PCI */

#ifdef RT_USING_OFW
const struct rt_dma_map_ops *rt_dma_ofw_device_map_ops(struct rt_device *dev)
{
    if (!dev || !dev->ofw_node)
    {
        return RT_NULL;
    }

    return ofw_device_dma_ops(dev);
}
#endif /* RT_USING_OFW */

/**
 * @brief Select DMA operations for a device
 *
 * Priority:
 * 1. Device-specific dma_ops (if already set)
 * 2. Device tree ops (memory-region pools and/or dma-ranges translation)
 * 3. Coherent or non-coherent fallback based on device property
 *
 * PCI devices with OFW bind dma_ops early in rt_pci_ofw_device_init();
 * other PCI devices fall back to pci_device_dma_ops() here.
 *
 * The result is cached in dev->dma_ops for subsequent calls.
 *
 * @param[in] dev  Device to query
 *
 * @return DMA map operations for this device
 */
static const struct rt_dma_map_ops *device_dma_ops(struct rt_device *dev)
{
    const struct rt_dma_map_ops *ops = dev->dma_ops;

    if (ops)
    {
        return ops;
    }

#ifdef RT_USING_OFW
    if (dev->ofw_node && (ops = ofw_device_dma_ops(dev)))
    {
        dev->dma_ops = ops;
        return ops;
    }
#endif

#ifdef RT_USING_PCI
    if ((ops = pci_device_dma_ops(dev)))
    {
        dev->dma_ops = ops;
        return ops;
    }
#endif

    if (rt_dma_device_is_coherent(dev))
    {
        ops = &dma_map_coherent_ops;
    }
    else
    {
        ops = &dma_map_nocoherent_ops;
    }

    dev->dma_ops = ops;

    return ops;
}

/**
 * @brief Allocate a contiguous range of pages from a DMA pool
 *
 * Uses a first-fit bitmap scan to find a contiguous block of free pages.
 * Pages are tracked by individual bits; ARCH_PAGE_SIZE is the granularity.
 *
 * @param[in] pool  DMA memory pool
 * @param[in] size  Requested size in bytes
 *
 * @return Physical offset within the pool, or RT_NULL on failure
 */
static rt_ubase_t dma_pool_alloc(struct rt_dma_pool *pool, rt_size_t size)
{
    rt_size_t bit, next_bit, end_bit, max_bits;

    size = RT_DIV_ROUND_UP(size, ARCH_PAGE_SIZE);

    if (!size || size > pool->bits)
    {
        LOG_W("%s: request %u page(s) exceeds pool capacity %u page(s)",
              pool->region.name ? pool->region.name : "<anon>",
              (unsigned)size, (unsigned)pool->bits);
        return RT_NULL;
    }

    max_bits = pool->bits - size;

    rt_bitmap_for_each_clear_bit(pool->map, bit, max_bits)
    {
        end_bit = bit + size;

        for (next_bit = bit + 1; next_bit < end_bit; ++next_bit)
        {
            if (rt_bitmap_test_bit(pool->map, next_bit))
            {
                bit = next_bit;
                goto _next;
            }
        }

        if (next_bit == end_bit)
        {
            while (next_bit-- > bit)
            {
                rt_bitmap_set_bit(pool->map, next_bit);
            }

            LOG_D("%s offset = %p, pages = %d", "Alloc",
                  pool->start + bit * ARCH_PAGE_SIZE, size);

            return pool->start + bit * ARCH_PAGE_SIZE;
        }
    _next:;
    }

    return RT_NULL;
}

/**
 * @brief Free a range of pages back to the DMA pool
 *
 * @param[in] pool   DMA memory pool
 * @param[in] offset Physical offset within the pool (from dma_pool_alloc)
 * @param[in] size   Size in bytes
 */
static void dma_pool_free(struct rt_dma_pool *pool, rt_ubase_t offset, rt_size_t size)
{
    rt_size_t bit = (offset - pool->start) / ARCH_PAGE_SIZE, end_bit;

    size = RT_DIV_ROUND_UP(size, ARCH_PAGE_SIZE);
    end_bit = bit + size;

    for (; bit < end_bit; ++bit)
    {
        rt_bitmap_clear_bit(pool->map, bit);
    }

    LOG_D("%s offset = %p, pages = %d", "Free", offset, size);
}

/**
 * @brief Internal DMA buffer allocator (page-based from pools)
 *
 * Iterates through all registered DMA pools, matching the requested flags
 * against pool capabilities (RT_DMA_F_DEVICE, RT_DMA_F_NOMAP, RT_DMA_F_32BITS).
 * On success, remaps the physical pages to a virtual address with the
 * appropriate cache attributes.
 *
 * @param[in]  dev        DMA-capable device
 * @param[in]  size       Requested size in bytes
 * @param[out] dma_handle Physical DMA address of the allocation
 * @param[in]  flags      Allocation flags (RT_DMA_F_*)
 *
 * @return Kernel virtual address, or RT_NULL on failure
 */
static void *dma_alloc(struct rt_device *dev, rt_size_t size,
                       rt_ubase_t *dma_handle, rt_ubase_t flags)
{
    void *dma_buffer = RT_NULL;
    struct rt_dma_pool *pool;

    region_pool_lock();

    rt_list_for_each_entry(pool, &dma_pool_nodes, list)
    {
        if (pool->flags & RT_DMA_F_DEVICE)
        {
            if (!(flags & RT_DMA_F_DEVICE) || pool->dev != dev)
            {
                continue;
            }
        }
        else if ((flags & RT_DMA_F_DEVICE))
        {
            continue;
        }

        if ((flags & RT_DMA_F_NOMAP) && !((pool->flags & RT_DMA_F_NOMAP)))
        {
            continue;
        }

        if ((flags & RT_DMA_F_32BITS) && !((pool->flags & RT_DMA_F_32BITS)))
        {
            continue;
        }

        *dma_handle = dma_pool_alloc(pool, size);

        if (*dma_handle && !(flags & RT_DMA_F_NOMAP))
        {
            if (pool->flags & RT_DMA_F_NOCACHE)
            {
                dma_buffer = rt_ioremap_nocache((void *)*dma_handle, size);
            }
            else if (flags & RT_DMA_F_NOCACHE)
            {
                /* Honor caller NOCACHE before LINEAR pool shortcut. */
                dma_buffer = rt_ioremap_nocache((void *)*dma_handle, size);
            }
            else if (pool->flags & RT_DMA_F_LINEAR)
            {
                dma_buffer = (void *)(*dma_handle - PV_OFFSET);
            }
            else if (flags & RT_DMA_F_WT)
            {
                dma_buffer = rt_ioremap_wt((void *)*dma_handle, size);
            }
            else
            {
                dma_buffer = rt_ioremap_cached((void *)*dma_handle, size);
            }

            if (!dma_buffer)
            {
                dma_pool_free(pool, *dma_handle, size);

                continue;
            }

            break;
        }
        else if (*dma_handle)
        {
            dma_buffer = (void *)*dma_handle;

            break;
        }
    }

    region_pool_unlock();

    return dma_buffer;
}

/**
 * @brief Internal DMA buffer free (returns pages to the pool)
 *
 * Finds the pool containing the DMA handle and frees the pages.
 *
 * @param[in] dev        DMA-capable device
 * @param[in] size       Buffer size in bytes
 * @param[in] cpu_addr   Kernel virtual address (unmapped)
 * @param[in] dma_handle Physical DMA address
 * @param[in] flags      Allocation flags
 */
static void dma_free(struct rt_device *dev, rt_size_t size,
                     void *cpu_addr, rt_ubase_t dma_handle, rt_ubase_t flags)
{
    struct rt_dma_pool *pool;

    region_pool_lock();

    rt_list_for_each_entry(pool, &dma_pool_nodes, list)
    {
        if (dma_handle >= pool->region.start &&
            dma_handle <= pool->region.end)
        {
            if (pool->flags & RT_DMA_F_NOCACHE || !(pool->flags & RT_DMA_F_LINEAR))
            {
                rt_iounmap(cpu_addr);
            }

            dma_pool_free(pool, dma_handle, size);

            break;
        }
    }

    region_pool_unlock();
}

/**
 * @brief Allocate a DMA-capable buffer
 *
 * Public API for allocating memory suitable for DMA transfers.
 * Uses device-specific DMA operations when available.
 *
 * @param[in]  dev        DMA-capable device
 * @param[in]  size       Requested buffer size in bytes
 * @param[out] dma_handle Physical DMA address (can be NULL if not needed)
 * @param[in]  flags      Allocation flags (RT_DMA_F_NOCACHE, RT_DMA_F_NOMAP, etc.)
 *
 * @return Kernel virtual address of the buffer, or RT_NULL on failure
 */
void *rt_dma_alloc(struct rt_device *dev, rt_size_t size,
                   rt_ubase_t *dma_handle, rt_ubase_t flags)
{
    void *dma_buffer = RT_NULL;
    rt_ubase_t dma_handle_s = 0;
    const struct rt_dma_map_ops *ops;

    if (!dev || !size)
    {
        return RT_NULL;
    }

    ops = device_dma_ops(dev);

    if (ops->alloc)
    {
        dma_buffer = ops->alloc(dev, size, &dma_handle_s, flags);
    }
    else
    {
        dma_buffer = dma_alloc(dev, size, &dma_handle_s, flags);
    }

    if (!dma_buffer)
    {
        return dma_buffer;
    }

    if (dma_handle)
    {
        *dma_handle = dma_handle_s;
    }

    return dma_buffer;
}

/**
 * @brief Free a DMA buffer previously allocated with rt_dma_alloc()
 *
 * @param[in] dev        DMA-capable device
 * @param[in] size       Buffer size in bytes
 * @param[in] cpu_addr   Kernel virtual address of the buffer
 * @param[in] dma_handle Physical DMA address from rt_dma_alloc()
 * @param[in] flags      Allocation flags used when allocating
 */
void rt_dma_free(struct rt_device *dev, rt_size_t size,
                 void *cpu_addr, rt_ubase_t dma_handle, rt_ubase_t flags)
{
    const struct rt_dma_map_ops *ops;

    if (!dev || !size || !cpu_addr)
    {
        return;
    }

    ops = device_dma_ops(dev);

    if (ops->free)
    {
        ops->free(dev, size, cpu_addr, dma_handle, flags);
    }
    else
    {
        dma_free(dev, size, cpu_addr, dma_handle, flags);
    }
}

/**
 * @brief Synchronize data for a DMA output transfer (CPU→device)
 *
 * Ensures the DMA engine sees the latest CPU-written data by performing
 * any necessary cache flushes and returning the DMA bus address.
 *
 * @param[in]  dev        DMA-capable device
 * @param[in]  data       Virtual address of the data buffer
 * @param[in]  size       Buffer size in bytes
 * @param[out] dma_handle DMA bus address (can be NULL)
 * @param[in]  flags      Sync flags (RT_DMA_F_NOCACHE, etc.)
 *
 * @return RT_EOK on success, -RT_EINVAL if data or size is 0
 */
rt_err_t rt_dma_sync_out_data(struct rt_device *dev, void *data, rt_size_t size,
                              rt_ubase_t *dma_handle, rt_ubase_t flags)
{
    rt_err_t err;
    rt_ubase_t dma_handle_s = 0;
    const struct rt_dma_map_ops *ops;

    if (!data || !size)
    {
        return -RT_EINVAL;
    }

    ops = device_dma_ops(dev);
    err = ops->sync_out_data(dev, data, size, &dma_handle_s, flags);

    if (dma_handle)
    {
        *dma_handle = dma_handle_s;
    }

    return err;
}

/**
 * @brief Synchronize data for a DMA input transfer (device→CPU)
 *
 * Ensures the CPU sees the latest DMA engine data by performing
 * any necessary cache invalidations.
 *
 * @param[in] dev        DMA-capable device
 * @param[in] out_data   Virtual address of the data buffer
 * @param[in] size       Buffer size in bytes
 * @param[in] dma_handle DMA bus address from the transfer
 * @param[in] flags      Sync flags
 *
 * @return RT_EOK on success, -RT_EINVAL if out_data or size is 0
 */
rt_err_t rt_dma_sync_in_data(struct rt_device *dev, void *out_data, rt_size_t size,
                             rt_ubase_t dma_handle, rt_ubase_t flags)
{
    rt_err_t err;
    const struct rt_dma_map_ops *ops;

    if (!out_data || !size)
    {
        return -RT_EINVAL;
    }

    ops = device_dma_ops(dev);
    err = ops->sync_in_data(dev, out_data, size, dma_handle, flags);

    return err;
}

/**
 * @brief Map a linear DMA pool into the kernel address space
 *
 * Reserved pool memory is excluded from rt_memblock_setup_memory_environment().
 * Install a cached linear mapping so RT_DMA_F_LINEAR pools are CPU-accessible.
 */
static rt_err_t dma_pool_map_linear(struct rt_dma_pool *pool)
{
    rt_region_t *region = &pool->region;
    rt_size_t start = RT_ALIGN_DOWN(region->start, ARCH_PAGE_SIZE);
    rt_size_t end = RT_ALIGN(region->end, ARCH_PAGE_SIZE);
    void *va;

    if (!(pool->flags & RT_DMA_F_LINEAR) || start >= end)
    {
        return RT_EOK;
    }

    struct rt_mm_va_hint hint = {
        .flags = MMF_MAP_FIXED,
        .limit_start = rt_kernel_space.start,
        .limit_range_size = rt_kernel_space.size,
        .map_size = end - start,
        .prefer = (void *)(start - PV_OFFSET),
    };

    if (rt_aspace_map_phy(&rt_kernel_space, &hint, MMU_MAP_K_RWCB,
                          start >> MM_PAGE_SHIFT, &va))
    {
        LOG_E("map %s [0x%lx, 0x%lx] failed", region->name,
              (unsigned long)start, (unsigned long)end);
        return -RT_ERROR;
    }

    return RT_EOK;
}

/**
 * @brief Install a DMA memory pool from a region descriptor
 *
 * Creates a new DMA pool covering the given memory region. Allocates
 * a bitmap for page tracking. Pools under 4GB are marked RT_DMA_F_32BITS.
 * Pools are automatically marked RT_DMA_F_LINEAR and mapped into kernel VA.
 *
 * Must be called with the DMA pools lock NOT held.
 *
 * @param[in] region  Memory region descriptor (start, end, name)
 *
 * @return New DMA pool pointer, or RT_NULL on failure
 */
static struct rt_dma_pool *dma_pool_install(rt_region_t *region)
{
    rt_err_t err;
    struct rt_dma_pool *pool;

    if (!(pool = rt_calloc(1, sizeof(*pool))))
    {
        LOG_E("Install pool[%p, %p] error = %s",
              region->start, region->end, rt_strerror(-RT_ENOMEM));

        return RT_NULL;
    }

    rt_memcpy(&pool->region, region, sizeof(*region));

    pool->flags |= RT_DMA_F_LINEAR;

    if (region->end < 4UL * SIZE_GB)
    {
        pool->flags |= RT_DMA_F_32BITS;
    }

    pool->start = RT_ALIGN(pool->region.start, ARCH_PAGE_SIZE);
    pool->bits = (pool->region.end - pool->start) / ARCH_PAGE_SIZE;

    if (!pool->bits)
    {
        err = -RT_EINVAL;
        goto _fail;
    }

    pool->map = rt_calloc(RT_BITMAP_LEN(pool->bits), sizeof(*pool->map));

    if (!pool->map)
    {
        err = -RT_ENOMEM;
        goto _fail;
    }

    rt_list_init(&pool->list);

    region_pool_lock();
    rt_list_insert_before(&dma_pool_nodes, &pool->list);
    region_pool_unlock();

    if (dma_pool_map_linear(pool))
    {
        LOG_W("linear map failed for pool %s", region->name);
    }

    return pool;

_fail:
    rt_free(pool);

    LOG_E("Install pool[%p, %p] error = %s",
          region->start, region->end, rt_strerror(err));

    return RT_NULL;
}

/**
 * @brief Public API: install a DMA memory pool
 *
 * Wrapper around dma_pool_install() with logging of the reserved region.
 *
 * @param[in] region  Memory region descriptor
 *
 * @return New DMA pool pointer, or RT_NULL on failure
 */
struct rt_dma_pool *rt_dma_pool_install(rt_region_t *region)
{
    struct rt_dma_pool *pool;

    if (!region)
    {
        return RT_NULL;
    }

    if ((pool = dma_pool_install(region)))
    {
        region = &pool->region;

        LOG_I("%s: Reserved %u.%u MiB at %p",
              region->name,
              (region->end - region->start) / SIZE_MB,
              (region->end - region->start) / SIZE_KB & (SIZE_KB - 1),
              region->start);
    }

    return pool;
}

/**
 * @brief Extract DMA pools from memblock reserved memory
 *
 * Creates two pools from the "dma-pool" reserved memory region:
 * 1. coherent-pool: for coherent DMA allocations
 * 2. CMA (Contiguous Memory Allocator): remaining space for general DMA
 *
 * Prefers regions below 4GB for 32-bit DMA compatibility.
 *
 * @param[in] cma_size           Total CMA region size in bytes
 * @param[in] coherent_pool_size Size of the coherent pool in bytes (must
 *                               be <= cma_size)
 *
 * @return RT_EOK on success, -RT_EINVAL if sizes are invalid,
 *         -RT_ENOSYS if memblock is not available,
 *         -RT_EEMPTY if no suitable dma-pool region found
 */
rt_err_t rt_dma_pool_extract(rt_size_t cma_size, rt_size_t coherent_pool_size)
{
    struct rt_dma_pool *pool;
    struct rt_mmblk_reg *reg, *reg_high;
    struct rt_memblock *memblock = rt_memblock_get_reserved();
    rt_region_t *region, *region_high = RT_NULL, cma, coherent_pool;

    if (!memblock)
    {
        return -RT_ENOSYS;
    }

    /* Coherent pool is included in CMA */
    if (cma_size < coherent_pool_size)
    {
        return -RT_EINVAL;
    }

    rt_slist_for_each_entry(reg, &memblock->reg_list, node)
    {
        if (!reg->alloc || (reg->flags & MEMBLOCK_HOTPLUG))
        {
            continue;
        }

        region = &reg->memreg;
        if (rt_strcmp(region->name, "dma-pool") || !reg->memreg.name)
        {
            continue;
        }

        /* Always use low address in 4G */
        if (region->end - region->start >= cma_size)
        {
            if ((rt_ssize_t)((4UL * SIZE_GB) - region->start) < cma_size)
            {
                region_high = region;
                reg_high = reg;
                continue;
            }

            goto _found;
        }
    }

    if (region_high)
    {
        region = region_high;
        reg = reg_high;
        LOG_W("No available DMA zone in 4G");

        goto _found;
    }

    return -RT_EEMPTY;

_found:
    if (region->end - region->start != cma_size)
    {
        cma.start = region->start;
        cma.end = cma.start + cma_size;
    }
    else
    {
        rt_memcpy(&cma, region, sizeof(cma));
    }

    coherent_pool.name = "coherent-pool";
    coherent_pool.start = cma.start;
    coherent_pool.end = coherent_pool.start + coherent_pool_size;

    cma.name = "cma";
    cma.start += coherent_pool_size;

    if (!(pool = rt_dma_pool_install(&coherent_pool)))
    {
        return -RT_ENOMEM;
    }

    /* Use: CMA > coherent-pool */
    if (!(pool = rt_dma_pool_install(&cma)))
    {
        return -RT_ENOMEM;
    }

    reg->alloc = RT_FALSE;

    return RT_EOK;
}

#if defined(RT_USING_CONSOLE) && defined(RT_USING_MSH)
/**
 * @brief MSH command: list all DMA memory pools
 *
 * Displays each pool's name, start, and end address.
 *
 * @param[in] argc  Argument count (unused)
 * @param[in] argv  Argument values (unused)
 *
 * @return 0
 */
static int list_dma_pool(int argc, char **argv)
{
    int count = 0;
    rt_region_t *region;
    struct rt_dma_pool *pool;

    rt_kprintf("%-*.s Region\n", RT_NAME_MAX, "Name");

    region_pool_lock();

    rt_list_for_each_entry(pool, &dma_pool_nodes, list)
    {
        region = &pool->region;

        rt_kprintf("%-*.s [%p, %p]\n", RT_NAME_MAX, region->name,
                   region->start, region->end);

        ++count;
    }

    rt_kprintf("%d DMA memory found\n", count);

    region_pool_unlock();

    return 0;
}
MSH_CMD_EXPORT(list_dma_pool, dump all dma memory pool);
#endif /* RT_USING_CONSOLE && RT_USING_MSH */