1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * bitext.c: kernel little helper (of bit shuffling variety).
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2002 Pete Zaitcev <zaitcev@yahoo.com>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * The algorithm to search a zero bit string is geared towards its application.
8*4882a593Smuzhiyun * We expect a couple of fixed sizes of requests, so a rotating counter, reset
9*4882a593Smuzhiyun * by align size, should provide fast enough search while maintaining low
10*4882a593Smuzhiyun * fragmentation.
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/string.h>
14*4882a593Smuzhiyun #include <linux/bitmap.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <asm/bitext.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /**
19*4882a593Smuzhiyun * bit_map_string_get - find and set a bit string in bit map.
20*4882a593Smuzhiyun * @t: the bit map.
21*4882a593Smuzhiyun * @len: requested string length
22*4882a593Smuzhiyun * @align: requested alignment
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Returns offset in the map or -1 if out of space.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Not safe to call from an interrupt (uses spin_lock).
27*4882a593Smuzhiyun */
bit_map_string_get(struct bit_map * t,int len,int align)28*4882a593Smuzhiyun int bit_map_string_get(struct bit_map *t, int len, int align)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun int offset, count; /* siamese twins */
31*4882a593Smuzhiyun int off_new;
32*4882a593Smuzhiyun int align1;
33*4882a593Smuzhiyun int i, color;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun if (t->num_colors) {
36*4882a593Smuzhiyun /* align is overloaded to be the page color */
37*4882a593Smuzhiyun color = align;
38*4882a593Smuzhiyun align = t->num_colors;
39*4882a593Smuzhiyun } else {
40*4882a593Smuzhiyun color = 0;
41*4882a593Smuzhiyun if (align == 0)
42*4882a593Smuzhiyun align = 1;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun align1 = align - 1;
45*4882a593Smuzhiyun if ((align & align1) != 0)
46*4882a593Smuzhiyun BUG();
47*4882a593Smuzhiyun if (align < 0 || align >= t->size)
48*4882a593Smuzhiyun BUG();
49*4882a593Smuzhiyun if (len <= 0 || len > t->size)
50*4882a593Smuzhiyun BUG();
51*4882a593Smuzhiyun color &= align1;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun spin_lock(&t->lock);
54*4882a593Smuzhiyun if (len < t->last_size)
55*4882a593Smuzhiyun offset = t->first_free;
56*4882a593Smuzhiyun else
57*4882a593Smuzhiyun offset = t->last_off & ~align1;
58*4882a593Smuzhiyun count = 0;
59*4882a593Smuzhiyun for (;;) {
60*4882a593Smuzhiyun off_new = find_next_zero_bit(t->map, t->size, offset);
61*4882a593Smuzhiyun off_new = ((off_new + align1) & ~align1) + color;
62*4882a593Smuzhiyun count += off_new - offset;
63*4882a593Smuzhiyun offset = off_new;
64*4882a593Smuzhiyun if (offset >= t->size)
65*4882a593Smuzhiyun offset = 0;
66*4882a593Smuzhiyun if (count + len > t->size) {
67*4882a593Smuzhiyun spin_unlock(&t->lock);
68*4882a593Smuzhiyun /* P3 */ printk(KERN_ERR
69*4882a593Smuzhiyun "bitmap out: size %d used %d off %d len %d align %d count %d\n",
70*4882a593Smuzhiyun t->size, t->used, offset, len, align, count);
71*4882a593Smuzhiyun return -1;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun if (offset + len > t->size) {
75*4882a593Smuzhiyun count += t->size - offset;
76*4882a593Smuzhiyun offset = 0;
77*4882a593Smuzhiyun continue;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun i = 0;
81*4882a593Smuzhiyun while (test_bit(offset + i, t->map) == 0) {
82*4882a593Smuzhiyun i++;
83*4882a593Smuzhiyun if (i == len) {
84*4882a593Smuzhiyun bitmap_set(t->map, offset, len);
85*4882a593Smuzhiyun if (offset == t->first_free)
86*4882a593Smuzhiyun t->first_free = find_next_zero_bit
87*4882a593Smuzhiyun (t->map, t->size,
88*4882a593Smuzhiyun t->first_free + len);
89*4882a593Smuzhiyun if ((t->last_off = offset + len) >= t->size)
90*4882a593Smuzhiyun t->last_off = 0;
91*4882a593Smuzhiyun t->used += len;
92*4882a593Smuzhiyun t->last_size = len;
93*4882a593Smuzhiyun spin_unlock(&t->lock);
94*4882a593Smuzhiyun return offset;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun count += i + 1;
98*4882a593Smuzhiyun if ((offset += i + 1) >= t->size)
99*4882a593Smuzhiyun offset = 0;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
bit_map_clear(struct bit_map * t,int offset,int len)103*4882a593Smuzhiyun void bit_map_clear(struct bit_map *t, int offset, int len)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun int i;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun if (t->used < len)
108*4882a593Smuzhiyun BUG(); /* Much too late to do any good, but alas... */
109*4882a593Smuzhiyun spin_lock(&t->lock);
110*4882a593Smuzhiyun for (i = 0; i < len; i++) {
111*4882a593Smuzhiyun if (test_bit(offset + i, t->map) == 0)
112*4882a593Smuzhiyun BUG();
113*4882a593Smuzhiyun __clear_bit(offset + i, t->map);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun if (offset < t->first_free)
116*4882a593Smuzhiyun t->first_free = offset;
117*4882a593Smuzhiyun t->used -= len;
118*4882a593Smuzhiyun spin_unlock(&t->lock);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
bit_map_init(struct bit_map * t,unsigned long * map,int size)121*4882a593Smuzhiyun void bit_map_init(struct bit_map *t, unsigned long *map, int size)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun bitmap_zero(map, size);
124*4882a593Smuzhiyun memset(t, 0, sizeof *t);
125*4882a593Smuzhiyun spin_lock_init(&t->lock);
126*4882a593Smuzhiyun t->map = map;
127*4882a593Smuzhiyun t->size = size;
128*4882a593Smuzhiyun }
129