grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / devs / AHI / Drivers / EMU10kx / emuadxmg.c
blob8e3b21887dd926a7be46bb3a7175914c2594c13d
2 /*
3 **********************************************************************
4 * emuadxmg.c - Address space manager for emu10k1 driver
5 * Copyright 1999, 2000 Creative Labs, Inc.
6 *
7 **********************************************************************
8 *
9 * Date Author Summary of changes
10 * ---- ------ ------------------
11 * October 20, 1999 Bertrand Lee base code release
13 **********************************************************************
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public
26 * License along with this program; if not, write to the Free
27 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
28 * USA.
30 **********************************************************************
33 #ifdef AHI
34 #include <config.h>
35 #endif
37 #include "hwaccess.h"
39 /* Allocates emu address space */
41 int emu10k1_addxmgr_alloc(u32 size, struct emu10k1_card *card)
43 u16 *pagetable = card->emupagetable;
44 u16 index = 0;
45 u16 numpages;
46 #ifndef AHI
47 unsigned long flags;
48 #endif
50 /* Convert bytes to pages */
51 numpages = (size / EMUPAGESIZE) + ((size % EMUPAGESIZE) ? 1 : 0);
53 spin_lock_irqsave(&card->lock, flags);
55 while (index < (MAXPAGES - 1)) {
56 if (pagetable[index] & 0x8000) {
57 /* This block of pages is in use, jump to the start of the next block. */
58 index += (pagetable[index] & 0x7fff);
59 } else {
60 /* Found free block */
61 if (pagetable[index] >= numpages) {
63 /* Block is large enough */
65 /* If free block is larger than the block requested
66 * then adjust the size of the block remaining */
67 if (pagetable[index] > numpages)
68 pagetable[index + numpages] = pagetable[index] - numpages;
70 pagetable[index] = (numpages | 0x8000); /* Mark block as used */
72 spin_unlock_irqrestore(&card->lock, flags);
74 return index;
75 } else {
76 /* Block too small, jump to the start of the next block */
77 index += pagetable[index];
82 spin_unlock_irqrestore(&card->lock, flags);
84 return -1;
87 /* Frees a previously allocated emu address space. */
89 void emu10k1_addxmgr_free(struct emu10k1_card *card, int index)
91 u16 *pagetable = card->emupagetable;
92 u16 origsize = 0;
93 #ifndef AHI
94 unsigned long flags;
95 #endif
97 spin_lock_irqsave(&card->lock, flags);
99 if (pagetable[index] & 0x8000) {
100 /* Block is allocated - mark block as free */
101 origsize = pagetable[index] & 0x7fff;
102 pagetable[index] = origsize;
104 /* If next block is free, we concat both blocks */
105 if (!(pagetable[index + origsize] & 0x8000))
106 pagetable[index] += pagetable[index + origsize] & 0x7fff;
109 spin_unlock_irqrestore(&card->lock, flags);
111 return;