forked from RT-Thread/rt-thread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.c
1982 lines (1632 loc) · 44.2 KB
/
base.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
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-08-25 GuEe-GUI first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <drivers/ofw.h>
#include <drivers/ofw_io.h>
#include <drivers/ofw_fdt.h>
#include <drivers/ofw_raw.h>
#define DBG_TAG "rtdm.ofw"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#include "ofw_internal.h"
struct rt_ofw_node *ofw_node_root = RT_NULL;
struct rt_ofw_node *ofw_node_cpus = RT_NULL;
struct rt_ofw_node *ofw_node_chosen = RT_NULL;
struct rt_ofw_node *ofw_node_aliases = RT_NULL;
struct rt_ofw_node *ofw_node_reserved_memory = RT_NULL;
static rt_phandle _phandle_range[2] = { 1, 1 }, _phandle_next = 1;
static struct rt_ofw_node **_phandle_hash = RT_NULL;
static rt_list_t _aliases_nodes = RT_LIST_OBJECT_INIT(_aliases_nodes);
rt_err_t ofw_phandle_hash_reset(rt_phandle min, rt_phandle max)
{
rt_err_t err = RT_EOK;
rt_phandle next = max;
struct rt_ofw_node **hash_ptr = RT_NULL;
max = RT_ALIGN(max, OFW_NODE_MIN_HASH);
if (max > _phandle_range[1])
{
rt_size_t size = sizeof(*_phandle_hash) * (max - min);
if (!_phandle_hash)
{
hash_ptr = rt_calloc(1, size);
}
else
{
hash_ptr = rt_realloc(_phandle_hash, size);
if (hash_ptr)
{
rt_size_t old_max = _phandle_range[1];
rt_memset(&hash_ptr[old_max], 0, sizeof(_phandle_hash) * (max - old_max));
}
}
}
if (hash_ptr)
{
/* We always reset min value only once */
if (min)
{
_phandle_range[0] = min;
}
_phandle_range[1] = max;
_phandle_next = next + 1;
_phandle_hash = hash_ptr;
}
else
{
err = -RT_ENOMEM;
}
return err;
}
static rt_phandle ofw_phandle_next(void)
{
rt_phandle next;
static struct rt_spinlock op_lock = {};
rt_hw_spin_lock(&op_lock.lock);
RT_ASSERT(_phandle_next != OFW_PHANDLE_MAX);
if (_phandle_next <= _phandle_range[1])
{
next = _phandle_next++;
}
else
{
rt_err_t err = ofw_phandle_hash_reset(_phandle_range[0], _phandle_next);
if (!err)
{
next = _phandle_next++;
}
else
{
next = 0;
LOG_E("Expanded phandle hash[%u, %u] fail error = %s",
_phandle_range[0], _phandle_next + 1, rt_strerror(err));
}
}
rt_hw_spin_unlock(&op_lock.lock);
return next;
}
static void ofw_prop_destroy(struct rt_ofw_prop *prop)
{
struct rt_ofw_prop *next;
while (prop)
{
next = prop->next;
rt_free(prop);
prop = next;
}
}
static struct rt_ofw_node *ofw_get_next_node(struct rt_ofw_node *prev)
{
struct rt_ofw_node *np;
/*
* Walk:
*
* / { ------------------------ [0] (START) has child, goto child.
*
* node0 { ---------------- [1] has child, goto child.
*
* node0_0 { ---------- [2] no child, has sibling, goto sibling.
* };
*
* node0_1 { ---------- [3] no sibling now.
* upward while the parent has sibling.
* };
* };
*
* node1 { ---------------- [4] come from node0 who find the sibling:
* node1, node1 has child, goto child.
*
* node1_0 { ---------- [5] has child, goto child.
*
* node1_0_0 { ---- [6] no sibling now.
* upward while the parent has sibling.
* (END) in the root.
* };
* };
* };
* };
*/
if (!prev)
{
np = ofw_node_root;
}
else if (prev->child)
{
np = prev->child;
}
else
{
np = prev;
while (np->parent && !np->sibling)
{
np = np->parent;
}
np = np->sibling;
}
return np;
}
static void ofw_node_destroy(struct rt_ofw_node *np)
{
struct rt_ofw_node *prev;
if (np->parent)
{
/* Ask parent and prev sibling we are destroy. */
prev = np->parent->child;
if (prev == np)
{
np->parent->child = RT_NULL;
}
else
{
while (prev->sibling != np)
{
prev = prev->sibling;
}
prev->sibling = np->sibling;
}
}
while (np)
{
if (rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM) == RT_FALSE)
{
LOG_E("%s is system node", np->full_name);
RT_ASSERT(0);
}
prev = np;
np = ofw_get_next_node(np);
ofw_prop_destroy(prev->props);
rt_free(prev);
}
}
rt_err_t rt_ofw_node_destroy(struct rt_ofw_node *np)
{
rt_err_t err = RT_EOK;
if (np)
{
if (rt_ref_read(&np->ref) <= 1)
{
ofw_node_destroy(np);
}
else
{
err = -RT_EBUSY;
}
}
else
{
err = -RT_EINVAL;
}
return err;
}
struct rt_ofw_node *rt_ofw_node_get(struct rt_ofw_node *np)
{
if (np)
{
LOG_D("%s get ref = %d", np->full_name, rt_ref_read(&np->ref));
rt_ref_get(&np->ref);
}
return np;
}
static void ofw_node_release(struct rt_ref *r)
{
struct rt_ofw_node *np = rt_container_of(r, struct rt_ofw_node, ref);
LOG_E("%s is release", np->full_name);
(void)np;
RT_ASSERT(0);
}
void rt_ofw_node_put(struct rt_ofw_node *np)
{
if (np)
{
LOG_D("%s put ref = %d", np->full_name, rt_ref_read(&np->ref));
rt_ref_put(&np->ref, &ofw_node_release);
}
}
rt_bool_t rt_ofw_node_tag_equ(const struct rt_ofw_node *np, const char *tag)
{
rt_bool_t ret = RT_FALSE;
if (np && tag)
{
const char *node_name = rt_fdt_node_name(np->full_name);
rt_size_t tag_len = strchrnul(node_name, '@') - node_name;
ret = (rt_strlen(tag) == tag_len && !rt_strncmp(node_name, tag, tag_len));
}
return ret;
}
rt_bool_t rt_ofw_node_tag_prefix(const struct rt_ofw_node *np, const char *prefix)
{
rt_bool_t ret = RT_FALSE;
if (np && prefix)
{
ret = !rt_strncmp(rt_fdt_node_name(np->full_name), prefix, rt_strlen(prefix));
}
return ret;
}
static int ofw_prop_index_of_string(struct rt_ofw_prop *prop, const char *string,
rt_int32_t (*cmp)(const char *cs, const char *ct))
{
int index = -1;
rt_size_t len = prop->length, slen = 0;
const char *value = prop->value;
for (int idx = 0; len > 0; ++idx)
{
/* Add '\0' */
slen = rt_strlen(value) + 1;
if (!cmp(value, string))
{
index = idx;
break;
}
len -= slen;
value += slen;
}
return index;
}
static rt_int32_t ofw_strcasecmp(const char *cs, const char *ct)
{
extern rt_int32_t strcasecmp(const char *cs, const char *ct);
return rt_strcasecmp(cs, ct);
}
static int ofw_prop_index_of_compatible(struct rt_ofw_prop *prop, const char *compatible)
{
return ofw_prop_index_of_string(prop, compatible, ofw_strcasecmp);
}
static int ofw_node_index_of_compatible(const struct rt_ofw_node *np, const char *compatible)
{
int idx = -1;
struct rt_ofw_prop *prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
if (prop)
{
idx = ofw_prop_index_of_compatible(prop, compatible);
}
return idx;
}
rt_bool_t rt_ofw_machine_is_compatible(const char *compatible)
{
return ofw_node_index_of_compatible(ofw_node_root, compatible) >= 0;
}
/*
* Property status:
*
* "okay" or "ok":
* Indicates the device is operational.
*
* "disabled":
* Indicates that the device is not presently operational, but it might
* become operational in the future (for example, something is not
* plugged in, or switched off).
* Refer to the device binding for details on what disabled means for a
* given device.
*
* "reserved":
* Indicates that the device is operational, but should not be used.
* Typically this is used for devices that are controlled by another
* software component, such as platform firmware.
*
* "fail":
* Indicates that the device is not operational. A serious error was
* detected in the device, and it is unlikely to become operational
* without repair.
*
* "fail-sss":
* Indicates that the device is not operational. A serious error was
* detected in the device and it is unlikely to become operational
* without repair. The sss portion of the value is specific to the
* device and indicates the error condition detected.
*/
static rt_bool_t ofw_node_is_fail(const struct rt_ofw_node *np)
{
rt_bool_t res = RT_FALSE;
const char *status = rt_ofw_prop_read_raw(np, "status", RT_NULL);
if (status)
{
res = !rt_strcmp(status, "fail") || !rt_strncmp(status, "fail-", 5);
}
return res;
}
static rt_bool_t ofw_node_is_available(const struct rt_ofw_node *np)
{
rt_bool_t res = RT_TRUE;
const char *status = rt_ofw_prop_read_raw(np, "status", RT_NULL);
if (status)
{
res = !rt_strcmp(status, "okay") || !rt_strcmp(status, "ok");
}
return res;
}
rt_bool_t rt_ofw_node_is_available(const struct rt_ofw_node *np)
{
return np ? ofw_node_is_available(np) : RT_FALSE;
}
rt_bool_t rt_ofw_node_is_compatible(const struct rt_ofw_node *np, const char *compatible)
{
rt_bool_t res = RT_FALSE;
if (np)
{
res = ofw_node_index_of_compatible(np, compatible) >= 0;
}
return res;
}
static struct rt_ofw_node_id *ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids)
{
int best_index = RT_UINT32_MAX >> 1, index;
struct rt_ofw_node_id *found_id = RT_NULL, *id;
for (id = (struct rt_ofw_node_id *)ids; id->compatible[0]; ++id)
{
index = ofw_prop_index_of_compatible(prop, id->compatible);
if (index >= 0 && index < best_index)
{
found_id = id;
best_index = index;
}
}
return found_id;
}
struct rt_ofw_node_id *rt_ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids)
{
struct rt_ofw_node_id *id = RT_NULL;
if (prop && ids && !rt_strcmp(prop->name, "compatible"))
{
id = ofw_prop_match(prop, ids);
}
return id;
}
struct rt_ofw_node_id *rt_ofw_node_match(struct rt_ofw_node *np, const struct rt_ofw_node_id *ids)
{
struct rt_ofw_prop *prop;
struct rt_ofw_node_id *id = RT_NULL;
if (np && ids && (prop = rt_ofw_get_prop(np, "compatible", RT_NULL)))
{
id = ofw_prop_match(prop, ids);
}
return id;
}
struct rt_ofw_node *rt_ofw_find_node_by_tag(struct rt_ofw_node *from, const char *tag)
{
struct rt_ofw_node *np = RT_NULL;
if (tag)
{
rt_ofw_foreach_nodes(from, np)
{
if (rt_ofw_node_tag_equ(np, tag))
{
break;
}
}
}
return np;
}
struct rt_ofw_node *rt_ofw_find_node_by_prop_r(struct rt_ofw_node *from, const char *propname,
const struct rt_ofw_prop **out_prop)
{
struct rt_ofw_node *np = RT_NULL;
if (propname)
{
rt_ofw_foreach_nodes(from, np)
{
struct rt_ofw_prop *prop = rt_ofw_get_prop(np, propname, RT_NULL);
if (prop)
{
if (out_prop)
{
*out_prop = prop;
}
break;
}
}
}
return np;
}
struct rt_ofw_node *rt_ofw_find_node_by_name(struct rt_ofw_node *from, const char *name)
{
struct rt_ofw_node *np = RT_NULL;
if (name)
{
rt_ofw_foreach_nodes(from, np)
{
if (np->name && !rt_strcmp(np->name, name))
{
np = rt_ofw_node_get(np);
break;
}
}
}
return np;
}
struct rt_ofw_node *rt_ofw_find_node_by_type(struct rt_ofw_node *from, const char *type)
{
struct rt_ofw_node *np = RT_NULL;
if (type)
{
rt_ofw_foreach_nodes(from, np)
{
if (rt_ofw_node_is_type(np, type))
{
break;
}
}
}
return np;
}
struct rt_ofw_node *rt_ofw_find_node_by_compatible(struct rt_ofw_node *from, const char *compatible)
{
struct rt_ofw_node *np = RT_NULL;
if (compatible)
{
rt_ofw_foreach_nodes(from, np)
{
if (ofw_node_index_of_compatible(np, compatible) >= 0)
{
break;
}
}
}
return np;
}
struct rt_ofw_node *rt_ofw_find_node_by_ids_r(struct rt_ofw_node *from, const struct rt_ofw_node_id *ids,
const struct rt_ofw_node_id **out_id)
{
struct rt_ofw_node *np = RT_NULL;
if (ids)
{
rt_ofw_foreach_nodes(from, np)
{
struct rt_ofw_node_id *id = rt_ofw_node_match(np, ids);
if (id)
{
if (out_id)
{
*out_id = id;
}
break;
}
}
}
return np;
}
struct rt_ofw_node *rt_ofw_find_node_by_path(const char *path)
{
struct rt_ofw_node *np = RT_NULL, *parent, *tmp = RT_NULL;
if (path)
{
if (!rt_strcmp(path, "/"))
{
np = ofw_node_root;
}
else
{
++path;
parent = rt_ofw_node_get(ofw_node_root);
while (*path)
{
const char *next = strchrnul(path, '/');
rt_size_t len = next - path;
tmp = RT_NULL;
rt_ofw_foreach_child_node(parent, np)
{
if (!rt_strncmp(np->full_name, path, len))
{
rt_ofw_node_put(parent);
parent = np;
tmp = np;
break;
}
}
if (!tmp)
{
rt_ofw_node_put(parent);
break;
}
path += len + !!*next;
}
np = tmp;
}
rt_ofw_node_get(np);
}
return np;
}
struct rt_ofw_node *rt_ofw_find_node_by_phandle(rt_phandle phandle)
{
struct rt_ofw_node *np = RT_NULL;
if (phandle >= OFW_PHANDLE_MIN && phandle <= OFW_PHANDLE_MAX)
{
/* rebase from zero */
rt_phandle poff = phandle - _phandle_range[0];
np = _phandle_hash[poff];
if (!np)
{
rt_ofw_foreach_allnodes(np)
{
if (np->phandle == phandle)
{
_phandle_hash[poff] = np;
break;
}
}
}
else
{
rt_ofw_node_get(np);
}
}
return np;
}
struct rt_ofw_node *rt_ofw_get_parent(const struct rt_ofw_node *np)
{
if (np)
{
np = rt_ofw_node_get(np->parent);
}
return (struct rt_ofw_node *)np;
}
struct rt_ofw_node *rt_ofw_get_child_by_tag(const struct rt_ofw_node *parent, const char *tag)
{
struct rt_ofw_node *child = RT_NULL;
if (parent && tag)
{
rt_ofw_foreach_child_node(parent, child)
{
if (rt_ofw_node_tag_equ(child, tag))
{
break;
}
}
}
return child;
}
struct rt_ofw_node *rt_ofw_get_child_by_compatible(const struct rt_ofw_node *parent, const char *compatible)
{
struct rt_ofw_node *child = RT_NULL;
if (parent && compatible)
{
rt_ofw_foreach_child_node(parent, child)
{
if (ofw_node_index_of_compatible(child, compatible) >= 0)
{
break;
}
}
}
return child;
}
int rt_ofw_get_child_count(const struct rt_ofw_node *np)
{
int nr;
if (np)
{
struct rt_ofw_node *child;
nr = 0;
rt_ofw_foreach_child_node(np, child)
{
++nr;
}
}
else
{
nr = -RT_EINVAL;
}
return nr;
}
int rt_ofw_get_available_child_count(const struct rt_ofw_node *np)
{
int nr;
if (np)
{
struct rt_ofw_node *child;
nr = 0;
rt_ofw_foreach_available_child_node(np, child)
{
++nr;
}
}
else
{
nr = -RT_EINVAL;
}
return nr;
}
struct rt_ofw_node *rt_ofw_get_next_node(struct rt_ofw_node *prev)
{
struct rt_ofw_node *np;
np = rt_ofw_node_get(ofw_get_next_node(prev));
rt_ofw_node_put(prev);
return np;
}
struct rt_ofw_node *rt_ofw_get_next_parent(struct rt_ofw_node *prev)
{
struct rt_ofw_node *next = RT_NULL;
if (prev)
{
next = rt_ofw_node_get(prev->parent);
rt_ofw_node_put(prev);
}
return next;
}
struct rt_ofw_node *rt_ofw_get_next_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev)
{
struct rt_ofw_node *next = RT_NULL;
if (parent)
{
next = prev ? prev->sibling : parent->child;
rt_ofw_node_put(prev);
rt_ofw_node_get(next);
}
return next;
}
struct rt_ofw_node *rt_ofw_get_next_available_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev)
{
struct rt_ofw_node *next = RT_NULL;
if (parent)
{
next = prev;
do {
next = rt_ofw_get_next_child(parent, next);
} while (next && !ofw_node_is_available(next));
}
return next;
}
struct rt_ofw_node *rt_ofw_get_cpu_node(int cpu, int *thread, rt_bool_t (*match_cpu_hwid)(int cpu, rt_uint64_t hwid))
{
const char *propname = "reg";
struct rt_ofw_node *cpu_np = RT_NULL;
/*
* "reg" (some of the obsolete arch may be other names):
* The value of reg is a <prop-encoded-array> that defines a unique
* CPU/thread id for the CPU/threads represented by the CPU node.
*
* If a CPU supports more than one thread (i.e. multiple streams of
* execution) the reg property is an array with 1 element per thread. The
* #address-cells on the /cpus node specifies how many cells each element
* of the array takes. Software can determine the number of threads by
* dividing the size of reg by the parent node’s #address-cells:
*
* thread-number = reg-cells / address-cells
*
* If a CPU/thread can be the target of an external interrupt the reg
* property value must be a unique CPU/thread id that is addressable by the
* interrupt controller.
*
* If a CPU/thread cannot be the target of an external interrupt, then reg
* must be unique and out of bounds of the range addressed by the interrupt
* controller
*
* If a CPU/thread’s PIR (pending interrupt register) is modifiable, a
* client program should modify PIR to match the reg property value. If PIR
* cannot be modified and the PIR value is distinct from the interrupt
* controller number space, the CPUs binding may define a binding-specific
* representation of PIR values if desired.
*/
rt_ofw_foreach_cpu_node(cpu_np)
{
rt_ssize_t prop_len = 0;
rt_bool_t is_end = RT_FALSE;
int tid, addr_cells = rt_ofw_io_addr_cells(cpu_np);
const fdt32_t *cell = rt_ofw_prop_read_raw(cpu_np, propname, &prop_len);
if (!cell && !addr_cells)
{
if (match_cpu_hwid && match_cpu_hwid(cpu, 0))
{
break;
}
continue;
}
if (!match_cpu_hwid)
{
continue;
}
prop_len /= sizeof(*cell) * addr_cells;
for (tid = 0; tid < prop_len; ++tid)
{
rt_uint64_t hwid = rt_fdt_read_number(cell, addr_cells);
if (match_cpu_hwid(cpu, hwid))
{
if (thread)
{
*thread = tid;
}
is_end = RT_TRUE;
break;
}
cell += addr_cells;
}
if (is_end)
{
break;
}
}
return cpu_np;
}
struct rt_ofw_node *rt_ofw_get_next_cpu_node(struct rt_ofw_node *prev)
{
struct rt_ofw_node *cpu_np;
if (prev)
{
cpu_np = prev->sibling;
rt_ofw_node_put(prev);
}
else
{
cpu_np = ofw_node_cpus->child;
}
for (; cpu_np; cpu_np = cpu_np->sibling)
{
if (ofw_node_is_fail(cpu_np))
{
continue;
}
if (!(rt_ofw_node_tag_equ(cpu_np, "cpu") || rt_ofw_node_is_type(cpu_np, "cpu")))
{
continue;
}
if (rt_ofw_node_get(cpu_np))
{
break;
}
}
return cpu_np;
}
struct rt_ofw_node *rt_ofw_get_cpu_state_node(struct rt_ofw_node *cpu_np, int index)
{
struct rt_ofw_cell_args args;
struct rt_ofw_node *np = RT_NULL, *state_np;
rt_err_t err = rt_ofw_parse_phandle_cells(cpu_np, "power-domains", "#power-domain-cells", 0, &args);
if (!err)
{
state_np = rt_ofw_parse_phandle(args.data, "domain-idle-states", index);
rt_ofw_node_put(args.data);
if (state_np)
{
np = state_np;
}
}
if (!np)
{
int count = 0;
rt_uint32_t phandle;
const fdt32_t *cell;
struct rt_ofw_prop *prop;
rt_ofw_foreach_prop_u32(cpu_np, "cpu-idle-states", prop, cell, phandle)
{
if (count == index)
{
np = rt_ofw_find_node_by_phandle((rt_phandle)phandle);
break;
}
++count;
}
}
return np;
}
rt_uint64_t rt_ofw_get_cpu_id(struct rt_ofw_node *cpu_np)
{