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
|
/* SPDX-License-Identifier: GPL-2.0-only */
#include <tests/test.h>
#include <device/device.h>
#include <device/resource.h>
#include <commonlib/helpers.h>
#include <memrange.h>
#define MEMRANGE_ALIGN (POWER_OF_2(12))
enum mem_types {
/* Avoid using 0 to verify that UUT really sets this memory,
but keep value small, as this will be an index in the table */
CACHEABLE_TAG = 0x10,
RESERVED_TAG,
READONLY_TAG,
INSERTED_TAG,
HOLE_TAG,
UNASSIGNED_TAG,
END_OF_RESOURCES
};
/* Indices of entries matters, since it must reflect mem_types enum */
struct resource res_mock_1[] = {
[UNASSIGNED_TAG] = {.base = 0x0,
.size = 0x8000,
.next = &res_mock_1[CACHEABLE_TAG],
.flags = IORESOURCE_MEM | IORESOURCE_PREFETCH},
[CACHEABLE_TAG] = {.base = 0xE000,
.size = 0xF2000,
.next = &res_mock_1[RESERVED_TAG],
.flags = IORESOURCE_CACHEABLE | IORESOURCE_MEM |
IORESOURCE_ASSIGNED },
[RESERVED_TAG] = {.base = 4ULL * GiB,
.size = 4ULL * KiB,
.next = &res_mock_1[READONLY_TAG],
.flags = IORESOURCE_RESERVE | IORESOURCE_MEM |
IORESOURCE_ASSIGNED },
[READONLY_TAG] = {.base = 0xFF0000,
.size = 0x10000,
.next = NULL,
.flags = IORESOURCE_READONLY | IORESOURCE_MEM |
IORESOURCE_ASSIGNED }
};
/* Boundary 1 byte below 4GiB and 1 byte above 4GiB. */
struct resource res_mock_2[] = {
[CACHEABLE_TAG] = {.base = 0x1000000,
.size = 4ULL * GiB - 0x1000001ULL,
.next = &res_mock_2[RESERVED_TAG],
.flags = IORESOURCE_CACHEABLE | IORESOURCE_MEM |
IORESOURCE_ASSIGNED },
[RESERVED_TAG] = {.base = 4ULL * GiB + 1ULL,
.size = 4ULL * GiB,
.next = &res_mock_2[READONLY_TAG],
.flags = IORESOURCE_RESERVE | IORESOURCE_MEM |
IORESOURCE_ASSIGNED },
[READONLY_TAG] = {.base = 0,
.size = 0x10000,
.next = NULL,
.flags = IORESOURCE_READONLY | IORESOURCE_MEM |
IORESOURCE_ASSIGNED }
};
/* Boundary crossing 4GiB. */
struct resource res_mock_3[] = {
[CACHEABLE_TAG] = {.base = 0xD000,
.size = 0xF3000,
.next = &res_mock_3[RESERVED_TAG],
.flags = IORESOURCE_CACHEABLE | IORESOURCE_MEM |
IORESOURCE_ASSIGNED },
[RESERVED_TAG] = {.base = 1ULL * GiB,
.size = 4ULL * GiB,
.next = &res_mock_3[READONLY_TAG],
.flags = IORESOURCE_RESERVE | IORESOURCE_MEM |
IORESOURCE_ASSIGNED },
[READONLY_TAG] = {.base = 0xFF0000,
.size = 0x10000,
.next = NULL,
.flags = IORESOURCE_READONLY | IORESOURCE_MEM |
IORESOURCE_ASSIGNED}
};
struct device mock_device = {.enabled = 1};
/* Fake memory devices handle */
struct device *all_devices = &mock_device;
int setup_test_1(void **state)
{
*state = res_mock_1;
mock_device.resource_list = &res_mock_1[UNASSIGNED_TAG];
return 0;
}
int setup_test_2(void **state)
{
*state = res_mock_2;
mock_device.resource_list = &res_mock_2[CACHEABLE_TAG];
return 0;
}
int setup_test_3(void **state)
{
*state = res_mock_3;
mock_device.resource_list = &res_mock_3[CACHEABLE_TAG];
return 0;
}
resource_t get_aligned_base(struct resource *res, struct range_entry *entry)
{
return ALIGN_DOWN(res[range_entry_tag(entry)].base, MEMRANGE_ALIGN);
}
resource_t get_aligned_end(struct resource *res, struct range_entry *entry)
{
resource_t end = res[range_entry_tag(entry)].base + res[range_entry_tag(entry)].size
+ (res[range_entry_tag(entry)].base - range_entry_base(entry));
return ALIGN_UP(end, MEMRANGE_ALIGN);
}
/*
* This test verifies memranges_init(), memranges_add_resources() and memranges_teardown()
* functions. It covers basic functionality of memrange library - implementation of creating
* memrange structure from resources available on the platform and method for free'ing
* allocated memory.
*
* Example memory ranges (res_mock1) for test_memrange_basic.
* Ranges marked with asterisks (***) are not added to the test_memrange.
*
* +-------UNASSIGNED_TAG--------+ <-0x0
* | |
* +-----------------------------+ <-0x8000
*
*
*
* +--------CACHEABLE_TAG--------+ <-0xE000
* | |
* | |
* | |
* +-----------------------------+ <-0x100000
*
*
*
* +-----***READONLY_TAG***------+ <-0xFF0000
* | |
* | |
* | |
* +-----------------------------+ <-0x1000000
*
*
* +--------RESERVED_TAG---------+ <-0x100000000
* | |
* +-----------------------------+ <-0x100001000
*/
static void test_memrange_basic(void **state)
{
int counter = 0;
const unsigned long cacheable = IORESOURCE_CACHEABLE;
const unsigned long reserved = IORESOURCE_RESERVE;
const unsigned long prefetchable = IORESOURCE_PREFETCH;
struct range_entry *ptr;
struct memranges test_memrange;
struct resource *res_mock = *state;
resource_t prev_base = 0;
memranges_init_empty(&test_memrange, NULL, 0);
memranges_add_resources(&test_memrange, prefetchable, prefetchable, UNASSIGNED_TAG);
memranges_add_resources(&test_memrange, cacheable, cacheable, CACHEABLE_TAG);
memranges_add_resources(&test_memrange, reserved, reserved, RESERVED_TAG);
/* There should be two entries, since cacheable and reserved regions are not neighbors.
Besides these two, a region with an unassigned tag is defined, to emulate an unmapped
PCI BAR resource. This resource is not mapped into host physical address and hence
should not be picked up by memranges_add_resources().*/
memranges_each_entry(ptr, &test_memrange)
{
assert_in_range(range_entry_tag(ptr), CACHEABLE_TAG, RESERVED_TAG);
assert_int_equal(range_entry_base(ptr), get_aligned_base(res_mock, ptr));
assert_int_equal(range_entry_end(ptr), get_aligned_end(res_mock, ptr));
/* Ranges have to be returned in increasing order */
assert_true(prev_base <= range_entry_base(ptr));
prev_base = range_entry_base(ptr);
counter++;
};
assert_int_equal(counter, 2);
counter = 0;
/* Remove initial memrange */
memranges_teardown(&test_memrange);
memranges_each_entry(ptr, &test_memrange) counter++;
assert_int_equal(counter, 0);
}
/*
* This test verifies memranges_clone(), memranges_insert() and memranges_update_tag()
* functions. All operations are performed on cloned memrange. One of the most important thing
* to check, is that memrange_insert() should remove all ranges which are covered by the newly
* inserted one.
*
* Example memory ranges (res_mock1) for test_memrange_clone_insert.
* Ranges marked with asterisks (***) are not added to the clone_memrange.
* Ranges marked with (^) have tag value changed during test.
*
* +--------CACHEABLE_TAG--------+ <-0xE000
* +------|----INSERTED_TAG----------+ | <-0xF000
* | | (^READONLY_TAG^) | |
* | | | |
* | +-----------------------------+ <-0x100000
* +---------------------------------+ <-0x101000
*
*
* +-----***READONLY_TAG***------+ <-0xFF0000
* | |
* | |
* | |
* +-----------------------------+ <-0x1000000
*
*
* +------+---------RESERVED_TAG-----+--+ <-0x100000000
* | | | |
* | +-----------------------------+ <-0x100001000
* +-----------INSERTED_TAG----------+ <-0x100002000
*/
static void test_memrange_clone_insert(void **state)
{
int counter = 0;
const unsigned long cacheable = IORESOURCE_CACHEABLE;
const unsigned long reserved = IORESOURCE_RESERVE;
struct range_entry *ptr;
struct memranges test_memrange, clone_memrange;
struct resource *res_mock = *state;
const resource_t new_range_begin_offset = 1ULL << 12;
memranges_init(&test_memrange, cacheable, cacheable, CACHEABLE_TAG);
memranges_add_resources(&test_memrange, reserved, reserved, RESERVED_TAG);
memranges_clone(&clone_memrange, &test_memrange);
memranges_teardown(&test_memrange);
/* Verify that new one is really a clone */
memranges_each_entry(ptr, &clone_memrange)
{
assert_in_range(range_entry_tag(ptr), CACHEABLE_TAG, END_OF_RESOURCES - 1);
assert_int_equal(range_entry_base(ptr), get_aligned_base(res_mock, ptr));
assert_int_equal(range_entry_end(ptr), get_aligned_end(res_mock, ptr));
counter++;
};
assert_int_equal(counter, 2);
counter = 0;
/* Insert new range, which will overlap with first region. */
memranges_insert(&clone_memrange, res_mock[CACHEABLE_TAG].base + new_range_begin_offset,
res_mock[CACHEABLE_TAG].size, INSERTED_TAG);
/* Three ranges should be there - CACHEABLE(shrunk), INSERTED and RESERVED */
memranges_each_entry(ptr, &clone_memrange)
{
resource_t expected_end;
if (range_entry_tag(ptr) == CACHEABLE_TAG) {
assert_int_equal(range_entry_base(ptr), res_mock[CACHEABLE_TAG].base);
expected_end = res_mock[CACHEABLE_TAG].base + new_range_begin_offset;
assert_int_equal(range_entry_end(ptr), expected_end);
}
if (range_entry_tag(ptr) == INSERTED_TAG) {
assert_int_equal(range_entry_base(ptr),
res_mock[CACHEABLE_TAG].base + new_range_begin_offset);
expected_end = res_mock[CACHEABLE_TAG].base + new_range_begin_offset
+ res_mock[CACHEABLE_TAG].size;
assert_int_equal(range_entry_end(ptr),
ALIGN_UP(expected_end, MEMRANGE_ALIGN));
}
counter++;
}
assert_int_equal(counter, 3);
counter = 0;
/* Insert new region, which will shadow readonly range.
* Additionally verify API for updating tags */
memranges_update_tag(&clone_memrange, INSERTED_TAG, READONLY_TAG);
memranges_each_entry(ptr, &clone_memrange)
{
resource_t expected_end;
assert_int_not_equal(range_entry_tag(ptr), INSERTED_TAG);
if (range_entry_tag(ptr) == READONLY_TAG) {
assert_int_equal(range_entry_base(ptr),
res_mock[CACHEABLE_TAG].base + new_range_begin_offset);
expected_end = res_mock[CACHEABLE_TAG].base + new_range_begin_offset
+ res_mock[CACHEABLE_TAG].size;
assert_int_equal(range_entry_end(ptr),
ALIGN_UP(expected_end, MEMRANGE_ALIGN));
}
};
/* Check if alignment (4KiB) is properly applied, that is begin - DOWN and end - UP */
memranges_insert(&clone_memrange, res_mock[RESERVED_TAG].base + 0xAD,
res_mock[RESERVED_TAG].size, INSERTED_TAG);
memranges_each_entry(ptr, &clone_memrange)
{
resource_t expected_end;
assert_int_not_equal(range_entry_tag(ptr), RESERVED_TAG);
if (range_entry_tag(ptr) == INSERTED_TAG) {
assert_int_equal(
range_entry_base(ptr),
ALIGN_DOWN(res_mock[RESERVED_TAG].base, MEMRANGE_ALIGN));
expected_end = ALIGN_DOWN(res_mock[RESERVED_TAG].base, MEMRANGE_ALIGN)
+ new_range_begin_offset + res_mock[RESERVED_TAG].size;
expected_end = ALIGN_UP(expected_end, MEMRANGE_ALIGN);
assert_int_equal(range_entry_end(ptr), expected_end);
}
counter++;
}
assert_int_equal(counter, 3);
/* Free clone */
memranges_teardown(&clone_memrange);
}
/*
* This test verifies memranges_fill_holes_up_to() and memranges_create_hole(). Idea of the test
* is to fill all holes, so that we end up with contiguous address space fully covered by
* entries. Then, holes are created on the border of two different regions
*
* Example memory ranges (res_mock1) for test_memrange_holes.
* Space marked with (/) is not covered by any region at the end of the test.
*
* +--------CACHEABLE_TAG--------+ <-0xE000
* | |
* | |
* //|/////////////////////////////| <-0xFF000
* //+-----------HOLE_TAG----------+ <-0x100000
* //|/////////////////////////////| <-0x101000
* | |
* | |
* | |
* | |
* +--------RESERVED_TAG---------+ <-0x100000000
* | |
* +-----------------------------+ <-0x100001000
*/
static void test_memrange_holes(void **state)
{
int counter = 0;
const unsigned long cacheable = IORESOURCE_CACHEABLE;
const unsigned long reserved = IORESOURCE_RESERVE;
struct range_entry *ptr;
struct range_entry *hole_ptr = NULL;
struct memranges test_memrange;
struct resource *res_mock = *state;
int holes_found = 0;
resource_t last_range_end = 0;
const resource_t holes_fill_end = res_mock[RESERVED_TAG].base;
memranges_init(&test_memrange, cacheable, cacheable, CACHEABLE_TAG);
memranges_add_resources(&test_memrange, reserved, reserved, RESERVED_TAG);
/* Count holes in ranges */
memranges_each_entry(ptr, &test_memrange)
{
if (!last_range_end) {
last_range_end = range_entry_end(ptr);
continue;
}
if (range_entry_base(ptr) != last_range_end) {
holes_found++;
last_range_end = range_entry_end(ptr);
}
if (range_entry_base(ptr) >= holes_fill_end)
break;
}
/* Create range entries which covers continuous memory range
(but with different tags) */
memranges_fill_holes_up_to(&test_memrange, holes_fill_end, HOLE_TAG);
memranges_each_entry(ptr, &test_memrange)
{
if (range_entry_tag(ptr) == HOLE_TAG) {
assert_int_equal(range_entry_base(ptr),
ALIGN_UP(res_mock[CACHEABLE_TAG].base
+ res_mock[CACHEABLE_TAG].size,
MEMRANGE_ALIGN));
assert_int_equal(range_entry_end(ptr), holes_fill_end);
/* Store pointer to HOLE_TAG region for future use */
hole_ptr = ptr;
}
counter++;
}
assert_int_equal(counter, 2 + holes_found);
/* If test data does not have any holes in it then terminate this test */
if (holes_found == 0)
return;
assert_non_null(hole_ptr);
counter = 0;
/* Create hole crossing the border of two range entries */
const resource_t new_cacheable_end = ALIGN_DOWN(
res_mock[CACHEABLE_TAG].base + res_mock[CACHEABLE_TAG].size - 4 * KiB,
MEMRANGE_ALIGN);
const resource_t new_hole_begin =
ALIGN_UP(range_entry_base(hole_ptr) + 4 * KiB, MEMRANGE_ALIGN);
const resource_t ranges_diff = new_hole_begin - new_cacheable_end;
memranges_create_hole(&test_memrange, new_cacheable_end, ranges_diff);
memranges_each_entry(ptr, &test_memrange)
{
switch (range_entry_tag(ptr)) {
case CACHEABLE_TAG:
assert_int_equal(range_entry_base(ptr), res_mock[CACHEABLE_TAG].base);
assert_int_equal(range_entry_end(ptr), new_cacheable_end);
break;
case RESERVED_TAG:
assert_int_equal(range_entry_base(ptr), res_mock[RESERVED_TAG].base);
assert_int_equal(range_entry_end(ptr),
res_mock[RESERVED_TAG].base
+ res_mock[RESERVED_TAG].size);
break;
case HOLE_TAG:
assert_int_equal(range_entry_base(ptr), new_hole_begin);
assert_int_equal(range_entry_end(ptr), res_mock[RESERVED_TAG].base);
break;
default:
break;
}
counter++;
}
assert_int_equal(counter, 3);
memranges_teardown(&test_memrange);
}
/*
* This test verifies memranges_steal() function. Simple check is done by attempt so steal some
* memory from region with READONLY_TAG.
*
* Example memory ranges (res_mock1) for test_memrange_steal.
* Space marked with (/) is not covered by any region at the end of the test.
*
* +--------CACHEABLE_TAG--------+ <-0xE000
* | |
* | |
* | |
* +-----------------------------+ <-0x100000
*
*
*
* +--------READONLY_TAG---------+ <-0xFF0000
* | |
* |/////////////////////////////| <-stolen_base
* |/////////////////////////////| <-stolen_base + 0x4000
* +-----------------------------+ <-0x1000000
*
*
* +--------RESERVED_TAG---------+ <-0x100000000
* | |
* +-----------------------------+ <-0x100001000
*/
static void test_memrange_steal(void **state)
{
bool status = false;
resource_t stolen;
const unsigned long cacheable = IORESOURCE_CACHEABLE;
const unsigned long reserved = IORESOURCE_RESERVE;
const unsigned long readonly = IORESOURCE_READONLY;
const resource_t stolen_range_size = 0x4000;
struct memranges test_memrange;
struct resource *res_mock = *state;
struct range_entry *ptr;
size_t count = 0;
memranges_init(&test_memrange, cacheable, cacheable, CACHEABLE_TAG);
memranges_add_resources(&test_memrange, reserved, reserved, RESERVED_TAG);
memranges_add_resources(&test_memrange, readonly, readonly, READONLY_TAG);
status = memranges_steal(&test_memrange,
res_mock[RESERVED_TAG].base + res_mock[RESERVED_TAG].size,
stolen_range_size, 12, READONLY_TAG, &stolen);
assert_true(status);
assert_in_range(stolen, res_mock[READONLY_TAG].base,
res_mock[READONLY_TAG].base + res_mock[READONLY_TAG].size);
memranges_each_entry(ptr, &test_memrange)
{
if (range_entry_tag(ptr) == READONLY_TAG) {
assert_int_equal(range_entry_base(ptr),
ALIGN_DOWN(res_mock[READONLY_TAG].base, MEMRANGE_ALIGN)
+ stolen_range_size);
}
count++;
}
assert_int_equal(count, 3);
count = 0;
/* Check if inserting range in previously stolen area will merge it. */
memranges_insert(&test_memrange, res_mock[READONLY_TAG].base + 0xCC, stolen_range_size,
READONLY_TAG);
memranges_each_entry(ptr, &test_memrange)
{
if (range_entry_tag(ptr) == READONLY_TAG) {
assert_int_equal(
range_entry_base(ptr),
ALIGN_DOWN(res_mock[READONLY_TAG].base, MEMRANGE_ALIGN));
assert_int_equal(
range_entry_end(ptr),
ALIGN_UP(range_entry_base(ptr) + res_mock[READONLY_TAG].size,
MEMRANGE_ALIGN));
}
count++;
}
assert_int_equal(count, 3);
count = 0;
memranges_teardown(&test_memrange);
}
/* Utility function checking number of entries and alignment of their base and end pointers */
static void check_range_entries_count_and_alignment(struct memranges *ranges,
size_t ranges_count, resource_t alignment)
{
size_t count = 0;
struct range_entry *ptr;
memranges_each_entry(ptr, ranges)
{
assert_true(IS_ALIGNED(range_entry_base(ptr), alignment));
assert_true(IS_ALIGNED(range_entry_end(ptr), alignment));
count++;
}
assert_int_equal(ranges_count, count);
}
/* This test verifies memranges_init*() and memranges_teardown() functions.
Added ranges are checked correct count and alignment. */
static void test_memrange_init_and_teardown(void **state)
{
const unsigned long cacheable = IORESOURCE_CACHEABLE;
const unsigned long reserved = IORESOURCE_RESERVE;
const unsigned long readonly = IORESOURCE_READONLY;
struct memranges test_memrange;
struct range_entry range_entries[4] = {0};
/* Test memranges_init() correctness */
memranges_init(&test_memrange, cacheable, cacheable, CACHEABLE_TAG);
memranges_add_resources(&test_memrange, reserved, reserved, RESERVED_TAG);
memranges_add_resources(&test_memrange, readonly, readonly, READONLY_TAG);
/* Expect all entries to be aligned to 4KiB (2^12) */
check_range_entries_count_and_alignment(&test_memrange, 3, MEMRANGE_ALIGN);
/* Expect ranges list to be empty after teardown */
memranges_teardown(&test_memrange);
assert_true(memranges_is_empty(&test_memrange));
/* Test memranges_init_with_alignment() correctness with alignment of 1KiB (2^10) */
memranges_init_with_alignment(&test_memrange, cacheable, cacheable, CACHEABLE_TAG, 10);
memranges_add_resources(&test_memrange, reserved, reserved, RESERVED_TAG);
memranges_add_resources(&test_memrange, readonly, readonly, READONLY_TAG);
check_range_entries_count_and_alignment(&test_memrange, 3, POWER_OF_2(10));
memranges_teardown(&test_memrange);
assert_true(memranges_is_empty(&test_memrange));
/* Test memranges_init_empty() correctness */
memranges_init_empty(&test_memrange, &range_entries[0], ARRAY_SIZE(range_entries));
assert_true(memranges_is_empty(&test_memrange));
memranges_add_resources(&test_memrange, cacheable, cacheable, CACHEABLE_TAG);
memranges_add_resources(&test_memrange, reserved, reserved, RESERVED_TAG);
memranges_add_resources(&test_memrange, readonly, readonly, READONLY_TAG);
check_range_entries_count_and_alignment(&test_memrange, 3, MEMRANGE_ALIGN);
memranges_teardown(&test_memrange);
assert_true(memranges_is_empty(&test_memrange));
/* Test memranges_init_with_alignment() correctness with alignment of 8KiB (2^13) */
memranges_init_empty_with_alignment(&test_memrange, &range_entries[0],
ARRAY_SIZE(range_entries), 13);
assert_true(memranges_is_empty(&test_memrange));
memranges_add_resources(&test_memrange, cacheable, cacheable, CACHEABLE_TAG);
memranges_add_resources(&test_memrange, reserved, reserved, RESERVED_TAG);
memranges_add_resources(&test_memrange, readonly, readonly, READONLY_TAG);
check_range_entries_count_and_alignment(&test_memrange, 3, POWER_OF_2(13));
memranges_teardown(&test_memrange);
assert_true(memranges_is_empty(&test_memrange));
}
/* Filter function accepting ranges having memory resource flag */
static int memrange_filter_mem_only(struct device *dev, struct resource *res)
{
/* Accept only memory resources */
return res->flags & IORESOURCE_MEM;
}
/* Filter function rejecting ranges having memory resource flag */
static int memrange_filter_non_mem(struct device *dev, struct resource *res)
{
/* Accept only memory resources */
return !(res->flags & IORESOURCE_MEM);
}
/* This test verifies memranges_add_resources_filter() function by providing filter functions
which accept or reject ranges. */
static void test_memrange_add_resources_filter(void **state)
{
const unsigned long cacheable = IORESOURCE_CACHEABLE;
const unsigned long reserved = IORESOURCE_RESERVE;
struct memranges test_memrange;
struct range_entry *ptr;
size_t count = 0;
size_t accepted_tags[] = {CACHEABLE_TAG, RESERVED_TAG};
/* Check if filter accepts range correctly */
memranges_init(&test_memrange, reserved, reserved, RESERVED_TAG);
memranges_add_resources_filter(&test_memrange, cacheable, cacheable, CACHEABLE_TAG,
memrange_filter_mem_only);
/* Check if filter accepted desired range. */
memranges_each_entry(ptr, &test_memrange)
{
assert_in_set(range_entry_tag(ptr), accepted_tags, ARRAY_SIZE(accepted_tags));
assert_true(IS_ALIGNED(range_entry_base(ptr), MEMRANGE_ALIGN));
assert_true(IS_ALIGNED(range_entry_end(ptr), MEMRANGE_ALIGN));
count++;
}
assert_int_equal(2, count);
count = 0;
memranges_teardown(&test_memrange);
/* Check if filter rejects range correctly */
memranges_init(&test_memrange, reserved, reserved, RESERVED_TAG);
memranges_add_resources_filter(&test_memrange, cacheable, cacheable, CACHEABLE_TAG,
memrange_filter_non_mem);
check_range_entries_count_and_alignment(&test_memrange, 1, MEMRANGE_ALIGN);
memranges_teardown(&test_memrange);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_memrange_basic),
cmocka_unit_test(test_memrange_clone_insert),
cmocka_unit_test(test_memrange_holes),
cmocka_unit_test(test_memrange_steal),
cmocka_unit_test(test_memrange_init_and_teardown),
cmocka_unit_test(test_memrange_add_resources_filter),
};
return cmocka_run_group_tests_name(__TEST_NAME__ "(Boundary on 4GiB)", tests,
setup_test_1, NULL)
+ cmocka_run_group_tests_name(__TEST_NAME__ "(Boundaries 1 byte from 4GiB)",
tests, setup_test_2, NULL)
+ cmocka_run_group_tests_name(__TEST_NAME__ "(Range over 4GiB boundary)", tests,
setup_test_3, NULL);
}
|