OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [drm/] [sis_ds.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* sis_ds.c -- Private header for Direct Rendering Manager -*- linux-c -*-
2
 * Created: Mon Jan  4 10:05:05 1999 by sclin@sis.com.tw
3
 *
4
 * Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
5
 * All rights reserved.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a
8
 * copy of this software and associated documentation files (the "Software"),
9
 * to deal in the Software without restriction, including without limitation
10
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
 * and/or sell copies of the Software, and to permit persons to whom the
12
 * Software is furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice (including the next
15
 * paragraph) shall be included in all copies or substantial portions of the
16
 * Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
 * DEALINGS IN THE SOFTWARE.
25
 *
26
 * Authors:
27
 *    Sung-Ching Lin <sclin@sis.com.tw>
28
 *
29
 */
30
 
31
#include <linux/module.h>
32
#include <linux/delay.h>
33
#include <linux/errno.h>
34
#include <linux/kernel.h>
35
#include <linux/slab.h>
36
#include <linux/poll.h>
37
#include <asm/io.h>
38
#include <linux/pci.h>
39
 
40
#include "sis_ds.h"
41
 
42
/* Set Data Structure, not check repeated value
43
 * temporarily used
44
 */
45
 
46
set_t *setInit(void)
47
{
48
  int i;
49
  set_t *set;
50
 
51
  set = (set_t *)MALLOC(sizeof(set_t));
52
  if(set)
53
  {
54
    for(i = 0; i < SET_SIZE; i++){
55
      set->list[i].free_next = i+1;
56
      set->list[i].alloc_next = -1;
57
    }
58
 
59
    set->list[SET_SIZE-1].free_next = -1;
60
    set->free = 0;
61
    set->alloc = -1;
62
    set->trace = -1;
63
  }
64
 
65
  return set;
66
}
67
 
68
int setAdd(set_t *set, ITEM_TYPE item)
69
{
70
  int free = set->free;
71
 
72
  if(free != -1){
73
    set->list[free].val = item;
74
    set->free = set->list[free].free_next;
75
  }
76
  else{
77
    return 0;
78
  }
79
 
80
  set->list[free].alloc_next = set->alloc;
81
  set->alloc = free;
82
  set->list[free].free_next = -1;
83
 
84
  return 1;
85
}
86
 
87
int setDel(set_t *set, ITEM_TYPE item)
88
{
89
  int alloc = set->alloc;
90
  int prev = -1;
91
 
92
  while(alloc != -1){
93
    if(set->list[alloc].val == item){
94
      if(prev != -1)
95
        set->list[prev].alloc_next = set->list[alloc].alloc_next;
96
      else
97
        set->alloc = set->list[alloc].alloc_next;
98
      break;
99
    }
100
    prev = alloc;
101
    alloc = set->list[alloc].alloc_next;
102
  }
103
 
104
  if(alloc == -1)
105
    return 0;
106
 
107
  set->list[alloc].free_next = set->free;
108
  set->free = alloc;
109
  set->list[alloc].alloc_next = -1;
110
 
111
  return 1;
112
}
113
 
114
/* setFirst -> setAdd -> setNext is wrong */
115
 
116
int setFirst(set_t *set, ITEM_TYPE *item)
117
{
118
  if(set->alloc == -1)
119
    return 0;
120
 
121
  *item = set->list[set->alloc].val;
122
  set->trace = set->list[set->alloc].alloc_next;
123
 
124
  return 1;
125
}
126
 
127
int setNext(set_t *set, ITEM_TYPE *item)
128
{
129
  if(set->trace == -1)
130
    return 0;
131
 
132
  *item = set->list[set->trace].val;
133
  set->trace = set->list[set->trace].alloc_next;
134
 
135
  return 1;
136
}
137
 
138
int setDestroy(set_t *set)
139
{
140
  FREE(set);
141
 
142
  return 1;
143
}
144
 
145
/*
146
 * GLX Hardware Device Driver common code
147
 * Copyright (C) 1999 Keith Whitwell
148
 *
149
 * Permission is hereby granted, free of charge, to any person obtaining a
150
 * copy of this software and associated documentation files (the "Software"),
151
 * to deal in the Software without restriction, including without limitation
152
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
153
 * and/or sell copies of the Software, and to permit persons to whom the
154
 * Software is furnished to do so, subject to the following conditions:
155
 *
156
 * The above copyright notice and this permission notice shall be included
157
 * in all copies or substantial portions of the Software.
158
 *
159
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
160
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
162
 * KEITH WHITWELL, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
163
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
164
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
165
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
166
 *
167
 */
168
 
169
#define ISFREE(bptr) ((bptr)->free)
170
 
171
#define PRINTF(fmt, arg...) do{}while(0)
172
#define fprintf(fmt, arg...) do{}while(0)
173
 
174
static void *calloc(size_t nmemb, size_t size)
175
{
176
  void *addr;
177
  addr = kmalloc(nmemb*size, GFP_KERNEL);
178
  if (addr)
179
    memset(addr, 0, nmemb*size);
180
  return addr;
181
}
182
#define free(n) kfree(n)
183
 
184
void mmDumpMemInfo( memHeap_t *heap )
185
{
186
  TMemBlock *p;
187
 
188
  PRINTF ("Memory heap %p:\n", heap);
189
  if (heap == 0) {
190
    PRINTF ("  heap == 0\n");
191
  } else {
192
    p = (TMemBlock *)heap;
193
    while (p) {
194
      PRINTF ("  Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size,
195
             p->free ? '.':'U',
196
             p->reserved ? 'R':'.');
197
      p = p->next;
198
    }
199
  }
200
  PRINTF ("End of memory blocks\n");
201
}
202
 
203
memHeap_t *mmInit(int ofs,
204
                  int size)
205
{
206
   PMemBlock blocks;
207
 
208
   if (size <= 0) {
209
      return 0;
210
   }
211
   blocks = (TMemBlock *) calloc(1,sizeof(TMemBlock));
212
   if (blocks) {
213
      blocks->ofs = ofs;
214
      blocks->size = size;
215
      blocks->free = 1;
216
      return (memHeap_t *)blocks;
217
   } else
218
      return 0;
219
}
220
 
221
/* Kludgey workaround for existing i810 server.  Remove soon.
222
 */
223
memHeap_t *mmAddRange( memHeap_t *heap,
224
                       int ofs,
225
                       int size )
226
{
227
   PMemBlock blocks;
228
   blocks = (TMemBlock *) calloc(2,sizeof(TMemBlock));
229
   if (blocks) {
230
      blocks[0].size = size;
231
      blocks[0].free = 1;
232
      blocks[0].ofs = ofs;
233
      blocks[0].next = &blocks[1];
234
 
235
      /* Discontinuity - stops JoinBlock from trying to join non-adjacent
236
       * ranges.
237
       */
238
      blocks[1].size = 0;
239
      blocks[1].free = 0;
240
      blocks[1].ofs = ofs+size;
241
      blocks[1].next = (PMemBlock) heap;
242
      return (memHeap_t *)blocks;
243
   }
244
   else
245
      return heap;
246
}
247
 
248
static TMemBlock* SliceBlock(TMemBlock *p,
249
                             int startofs, int size,
250
                             int reserved, int alignment)
251
{
252
  TMemBlock *newblock;
253
 
254
  /* break left */
255
  if (startofs > p->ofs) {
256
    newblock = (TMemBlock*) calloc(1,sizeof(TMemBlock));
257
    newblock->ofs = startofs;
258
    newblock->size = p->size - (startofs - p->ofs);
259
    newblock->free = 1;
260
    newblock->next = p->next;
261
    p->size -= newblock->size;
262
    p->next = newblock;
263
    p = newblock;
264
  }
265
 
266
  /* break right */
267
  if (size < p->size) {
268
    newblock = (TMemBlock*) calloc(1,sizeof(TMemBlock));
269
    newblock->ofs = startofs + size;
270
    newblock->size = p->size - size;
271
    newblock->free = 1;
272
    newblock->next = p->next;
273
    p->size = size;
274
    p->next = newblock;
275
  }
276
 
277
  /* p = middle block */
278
  p->align = alignment;
279
  p->free = 0;
280
  p->reserved = reserved;
281
  return p;
282
}
283
 
284
PMemBlock mmAllocMem( memHeap_t *heap, int size, int align2, int startSearch)
285
{
286
  int mask,startofs,endofs;
287
  TMemBlock *p;
288
 
289
  if (!heap || align2 < 0 || size <= 0)
290
    return NULL;
291
  mask = (1 << align2)-1;
292
  startofs = 0;
293
  p = (TMemBlock *)heap;
294
  while (p) {
295
    if (ISFREE(p)) {
296
      startofs = (p->ofs + mask) & ~mask;
297
      if ( startofs < startSearch ) {
298
        startofs = startSearch;
299
      }
300
      endofs = startofs+size;
301
      if (endofs <= (p->ofs+p->size))
302
        break;
303
    }
304
    p = p->next;
305
  }
306
  if (!p)
307
    return NULL;
308
  p = SliceBlock(p,startofs,size,0,mask+1);
309
  p->heap = heap;
310
  return p;
311
}
312
 
313
static __inline__ int Join2Blocks(TMemBlock *p)
314
{
315
  if (p->free && p->next && p->next->free) {
316
    TMemBlock *q = p->next;
317
    p->size += q->size;
318
    p->next = q->next;
319
    free(q);
320
    return 1;
321
  }
322
  return 0;
323
}
324
 
325
int mmFreeMem(PMemBlock b)
326
{
327
  TMemBlock *p,*prev;
328
 
329
  if (!b)
330
    return 0;
331
  if (!b->heap) {
332
     fprintf(stderr, "no heap\n");
333
     return -1;
334
  }
335
  p = b->heap;
336
  prev = NULL;
337
  while (p && p != b) {
338
    prev = p;
339
    p = p->next;
340
  }
341
  if (!p || p->free || p->reserved) {
342
     if (!p)
343
        fprintf(stderr, "block not found in heap\n");
344
     else if (p->free)
345
        fprintf(stderr, "block already free\n");
346
     else
347
        fprintf(stderr, "block is reserved\n");
348
    return -1;
349
  }
350
  p->free = 1;
351
  Join2Blocks(p);
352
  if (prev)
353
    Join2Blocks(prev);
354
  return 0;
355
}
356
 
357
int mmReserveMem(memHeap_t *heap, int offset,int size)
358
{
359
  int endofs;
360
  TMemBlock *p;
361
 
362
  if (!heap || size <= 0)
363
    return -1;
364
  endofs = offset+size;
365
  p = (TMemBlock *)heap;
366
  while (p && p->ofs <= offset) {
367
    if (ISFREE(p) && endofs <= (p->ofs+p->size)) {
368
      SliceBlock(p,offset,size,1,1);
369
      return 0;
370
    }
371
    p = p->next;
372
  }
373
  return -1;
374
}
375
 
376
int mmFreeReserved(memHeap_t *heap, int offset)
377
{
378
  TMemBlock *p,*prev;
379
 
380
  if (!heap)
381
    return -1;
382
  p = (TMemBlock *)heap;
383
  prev = NULL;
384
  while (p && p->ofs != offset) {
385
    prev = p;
386
    p = p->next;
387
  }
388
  if (!p || !p->reserved)
389
    return -1;
390
  p->free = 1;
391
  p->reserved = 0;
392
  Join2Blocks(p);
393
  if (prev)
394
    Join2Blocks(prev);
395
  return 0;
396
}
397
 
398
void mmDestroy(memHeap_t *heap)
399
{
400
  TMemBlock *p,*q;
401
 
402
  if (!heap)
403
    return;
404
  p = (TMemBlock *)heap;
405
  while (p) {
406
    q = p->next;
407
    free(p);
408
    p = q;
409
  }
410
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.